Hack is gone
[llpp.git] / main.ml
blob89fe054027f032cda5091444aef110389ce52e25
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 let _debugl l =
78 dolog "l %d dim=%d {" l.pageno l.pagedimno;
79 dolog " WxH %dx%d" l.pagew l.pageh;
80 dolog " vWxH %dx%d" l.pagevw l.pagevh;
81 dolog " pagex,y %d,%d" l.pagex l.pagey;
82 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
83 dolog " column %d" l.pagecol;
84 dolog "}";
87 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
88 dolog "rect {";
89 dolog " x0,y0=(% f, % f)" x0 y0;
90 dolog " x1,y1=(% f, % f)" x1 y1;
91 dolog " x2,y2=(% f, % f)" x2 y2;
92 dolog " x3,y3=(% f, % f)" x3 y3;
93 dolog "}";
96 let isbirdseye = function
97 | Birdseye _ -> true
98 | Textentry _ | View | LinkNav _ -> false
101 let istextentry = function
102 | Textentry _ -> true
103 | Birdseye _ | View | LinkNav _ -> false
106 let wtmode = ref false;;
107 let cxack = ref false;;
109 let pgscale h = truncate (float h *. conf.pgscale);;
111 let hscrollh () =
112 if state.uioh#alwaysscrolly || ((conf.scrollb land scrollbhv != 0)
113 && (state.w > state.winw))
114 then conf.scrollbw
115 else 0
118 let vscrollw () =
119 if state.uioh#alwaysscrolly || ((conf.scrollb land scrollbvv != 0)
120 && (state.maxy > state.winh))
121 then conf.scrollbw
122 else 0
125 let vscrollhit x =
126 if conf.leftscroll
127 then x < vscrollw ()
128 else x > state.winw - vscrollw ()
131 let setfontsize n =
132 fstate.fontsize <- n;
133 fstate.wwidth <- measurestr fstate.fontsize "w";
134 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
137 let vlog fmt =
138 if conf.verbose
139 then dolog fmt
140 else Printf.kprintf ignore fmt
143 let launchpath () =
144 if emptystr conf.pathlauncher
145 then dolog "%s" state.path
146 else (
147 let command = Str.global_replace percentsre state.path conf.pathlauncher in
148 match spawn command [] with
149 | _pid -> ()
150 | (exception exn) ->
151 dolog "failed to execute `%s': %s" command @@ exntos exn
155 module G =
156 struct
157 let postRedisplay who =
158 vlog "redisplay for [%S]" who;
159 state.redisplay <- true;
161 end;;
163 let getopaque pageno =
164 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
165 with Not_found -> None
168 let pagetranslatepoint l x y =
169 let dy = y - l.pagedispy in
170 let y = dy + l.pagey in
171 let dx = x - l.pagedispx in
172 let x = dx + l.pagex in
173 (x, y);
176 let onppundermouse g x y d =
177 let rec f = function
178 | l :: rest ->
179 begin match getopaque l.pageno with
180 | Some opaque ->
181 let x0 = l.pagedispx in
182 let x1 = x0 + l.pagevw in
183 let y0 = l.pagedispy in
184 let y1 = y0 + l.pagevh in
185 if y >= y0 && y <= y1 && x >= x0 && x <= x1
186 then
187 let px, py = pagetranslatepoint l x y in
188 match g opaque l px py with
189 | Some res -> res
190 | None -> f rest
191 else f rest
192 | _ ->
193 f rest
195 | [] -> d
197 f state.layout
200 let getunder x y =
201 let g opaque l px py =
202 if state.bzoom
203 then (
204 match rectofblock opaque px py with
205 | Some [|x0;x1;y0;y1|] ->
206 let rect = (x0, y0, x1, y0, x1, y1, x0, y1) in
207 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
208 state.rects <- [l.pageno, color, rect];
209 G.postRedisplay "getunder";
210 | _otherwise -> ()
212 let under = whatsunder opaque px py in
213 if under = Unone then None else Some under
215 onppundermouse g x y Unone
218 let unproject x y =
219 let g opaque l x y =
220 match unproject opaque x y with
221 | Some (x, y) -> Some (Some (opaque, l.pageno, x, y))
222 | None -> None
224 onppundermouse g x y None;
227 let showtext c s =
228 state.text <- Printf.sprintf "%c%s" c s;
229 G.postRedisplay "showtext";
232 let impmsg fmt =
233 Format.ksprintf (fun s -> showtext '!' s) fmt;
236 let pipesel opaque cmd =
237 if hassel opaque
238 then
239 match Unix.pipe () with
240 | (exception exn) -> dolog "pipesel cannot create pipe: %S" @@ exntos exn;
241 | (r, w) ->
242 let doclose what fd =
243 Ne.clo fd (fun msg -> dolog "%s close failed: %s" what msg)
245 let pid =
246 try spawn cmd [r, 0; w, -1]
247 with exn ->
248 dolog "cannot execute %S: %s" cmd @@ exntos exn;
251 if pid > 0
252 then (
253 copysel w opaque;
254 G.postRedisplay "pipesel";
256 else doclose "pipesel pipe/w" w;
257 doclose "pipesel pipe/r" r;
260 let paxunder x y =
261 let g opaque l px py =
262 if markunder opaque px py conf.paxmark
263 then (
264 Some (fun () ->
265 match getopaque l.pageno with
266 | None -> ()
267 | Some opaque -> pipesel opaque conf.paxcmd
270 else None
272 G.postRedisplay "paxunder";
273 if conf.paxmark = Mark_page
274 then
275 List.iter (fun l ->
276 match getopaque l.pageno with
277 | None -> ()
278 | Some opaque -> clearmark opaque) state.layout;
279 state.roam <- onppundermouse g x y (fun () -> impmsg "whoopsie daisy");
282 let selstring s =
283 match Unix.pipe () with
284 | (exception exn) -> impmsg "pipe failed: %s" @@ exntos exn
285 | (r, w) ->
286 let clo cap fd =
287 Ne.clo fd (fun msg -> impmsg "failed to close %s: %s" cap msg)
289 let pid =
290 try spawn conf.selcmd [r, 0; w, -1]
291 with exn ->
292 impmsg "failed to execute %s: %s" conf.selcmd @@ exntos exn;
295 if pid > 0
296 then (
298 let l = String.length s in
299 let bytes = Bytes.unsafe_of_string s in
300 let n = tempfailureretry (Unix.write w bytes 0) l in
301 if n != l
302 then impmsg "failed to write %d characters to sel pipe, wrote %d"
304 with exn ->
305 impmsg "failed to write to sel pipe: %s" @@ exntos exn
307 else dolog "%s" s;
308 clo "selstring pipe/r" r;
309 clo "selstring pipe/w" w;
312 let undertext = function
313 | Unone -> "none"
314 | Ulinkuri s -> s
315 | Utext s -> "font: " ^ s
316 | Uannotation (opaque, slinkindex) ->
317 "annotation: " ^ getannotcontents opaque slinkindex
320 let updateunder x y =
321 match getunder x y with
322 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
323 | Ulinkuri uri ->
324 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
325 Wsi.setcursor Wsi.CURSOR_INFO
326 | Utext s ->
327 if conf.underinfo then showtext 'f' ("ont: " ^ s);
328 Wsi.setcursor Wsi.CURSOR_TEXT
329 | Uannotation _ ->
330 if conf.underinfo then showtext 'a' "nnotation";
331 Wsi.setcursor Wsi.CURSOR_INFO
334 let showlinktype under =
335 if conf.underinfo && under != Unone
336 then showtext ' ' @@ undertext under
339 let intentry_with_suffix text key =
340 let text =
341 if key >= 32 && key < 127
342 then
343 let c = Char.chr key in
344 match c with
345 | '0' .. '9' ->
346 addchar text c
348 | 'k' | 'm' | 'g' | 'K' | 'M' | 'G' ->
349 addchar text @@ asciilower c
350 | _ ->
351 state.text <- Printf.sprintf "invalid key (%d, `%c')" key c;
352 text
353 else (
354 state.text <- Printf.sprintf "invalid key %d" key;
355 text
358 TEcont text
361 let wcmd fmt =
362 let b = Buffer.create 16 in
363 Printf.kbprintf
364 (fun b ->
365 let b = Buffer.to_bytes b in
366 wcmd state.ss b @@ Bytes.length b
367 ) b fmt
370 let nogeomcmds cmds =
371 match cmds with
372 | s, [] -> emptystr s
373 | _ -> false
376 let layoutN ((columns, coverA, coverB), b) x y sw sh =
377 let rec fold accu n =
378 if n = Array.length b
379 then accu
380 else
381 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
382 if (vy - y) > sh &&
383 (n = coverA - 1
384 || n = state.pagecount - coverB
385 || (n - coverA) mod columns = columns - 1)
386 then accu
387 else
388 let accu =
389 if vy + h > y
390 then
391 let pagey = max 0 (y - vy) in
392 let pagedispy = if pagey > 0 then 0 else vy - y in
393 let pagedispx, pagex =
394 let pdx =
395 if n = coverA - 1 || n = state.pagecount - coverB
396 then x + (sw - w) / 2
397 else dx + xoff + x
399 if pdx < 0
400 then 0, -pdx
401 else pdx, 0
403 let pagevw =
404 let vw = sw - pagedispx in
405 let pw = w - pagex in
406 min vw pw
408 let pagevh = min (h - pagey) (sh - pagedispy) in
409 if pagevw > 0 && pagevh > 0
410 then
411 let e =
412 { pageno = n
413 ; pagedimno = pdimno
414 ; pagew = w
415 ; pageh = h
416 ; pagex = pagex
417 ; pagey = pagey
418 ; pagevw = pagevw
419 ; pagevh = pagevh
420 ; pagedispx = pagedispx
421 ; pagedispy = pagedispy
422 ; pagecol = 0
425 e :: accu
426 else
427 accu
428 else
429 accu
431 fold accu (n+1)
433 if Array.length b = 0
434 then []
435 else List.rev (fold [] (page_of_y y))
438 let layoutS (columns, b) x y sw sh =
439 let rec fold accu n =
440 if n = Array.length b
441 then accu
442 else
443 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
444 if (vy - y) > sh
445 then accu
446 else
447 let accu =
448 if vy + pageh > y
449 then
450 let x = xoff + x in
451 let pagey = max 0 (y - vy) in
452 let pagedispy = if pagey > 0 then 0 else vy - y in
453 let pagedispx, pagex =
454 if px = 0
455 then (
456 if x < 0
457 then 0, -x
458 else x, 0
460 else (
461 let px = px - x in
462 if px < 0
463 then -px, 0
464 else 0, px
467 let pagecolw = pagew/columns in
468 let pagedispx =
469 if pagecolw < sw
470 then pagedispx + ((sw - pagecolw) / 2)
471 else pagedispx
473 let pagevw =
474 let vw = sw - pagedispx in
475 let pw = pagew - pagex in
476 min vw pw
478 let pagevw = min pagevw pagecolw in
479 let pagevh = min (pageh - pagey) (sh - pagedispy) in
480 if pagevw > 0 && pagevh > 0
481 then
482 let e =
483 { pageno = n/columns
484 ; pagedimno = pdimno
485 ; pagew = pagew
486 ; pageh = pageh
487 ; pagex = pagex
488 ; pagey = pagey
489 ; pagevw = pagevw
490 ; pagevh = pagevh
491 ; pagedispx = pagedispx
492 ; pagedispy = pagedispy
493 ; pagecol = n mod columns
496 e :: accu
497 else
498 accu
499 else
500 accu
502 fold accu (n+1)
504 List.rev (fold [] 0)
507 let layout x y sw sh =
508 if nogeomcmds state.geomcmds
509 then
510 match conf.columns with
511 | Csingle b -> layoutN ((1, 0, 0), b) x y sw sh
512 | Cmulti c -> layoutN c x y sw sh
513 | Csplit s -> layoutS s x y sw sh
514 else []
517 let clamp incr =
518 let y = state.y + incr in
519 let y = max 0 y in
520 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
524 let itertiles l f =
525 let tilex = l.pagex mod conf.tilew in
526 let tiley = l.pagey mod conf.tileh in
528 let col = l.pagex / conf.tilew in
529 let row = l.pagey / conf.tileh in
531 let rec rowloop row y0 dispy h =
532 if h = 0
533 then ()
534 else (
535 let dh = conf.tileh - y0 in
536 let dh = min h dh in
537 let rec colloop col x0 dispx w =
538 if w = 0
539 then ()
540 else (
541 let dw = conf.tilew - x0 in
542 let dw = min w dw in
543 f col row dispx dispy x0 y0 dw dh;
544 colloop (col+1) 0 (dispx+dw) (w-dw)
547 colloop col tilex l.pagedispx l.pagevw;
548 rowloop (row+1) 0 (dispy+dh) (h-dh)
551 if l.pagevw > 0 && l.pagevh > 0
552 then rowloop row tiley l.pagedispy l.pagevh;
555 let gettileopaque l col row =
556 let key =
557 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
559 try Some (Hashtbl.find state.tilemap key)
560 with Not_found -> None
563 let puttileopaque l col row gen colorspace angle opaque size elapsed =
564 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
565 Hashtbl.add state.tilemap key (opaque, size, elapsed)
568 let filledrect2 x0 y0 x1 y1 x2 y2 x3 y3 =
569 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x1; y1; x2; y2; x3; y3 |];
570 GlArray.vertex `two state.vraw;
571 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
574 let filledrect1 x0 y0 x1 y1 = filledrect2 x0 y0 x0 y1 x1 y0 x1 y1;;
576 let filledrect x0 y0 x1 y1 =
577 GlArray.disable `texture_coord;
578 filledrect1 x0 y0 x1 y1;
579 GlArray.enable `texture_coord;
582 let linerect x0 y0 x1 y1 =
583 GlArray.disable `texture_coord;
584 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y1; x1; y0 |];
585 GlArray.vertex `two state.vraw;
586 GlArray.draw_arrays `line_loop ~first:0 ~count:4;
587 GlArray.enable `texture_coord;
590 let drawtiles l color =
591 GlDraw.color color;
592 begintiles ();
593 let f col row x y tilex tiley w h =
594 match gettileopaque l col row with
595 | Some (opaque, _, t) ->
596 let params = x, y, w, h, tilex, tiley in
597 if conf.invert
598 then GlTex.env (`mode `blend);
599 drawtile params opaque;
600 if conf.invert
601 then GlTex.env (`mode `modulate);
602 if conf.debug
603 then (
604 endtiles ();
605 let s = Printf.sprintf
606 "%d[%d,%d] %f sec"
607 l.pageno col row t
609 let w = measurestr fstate.fontsize s in
610 GlDraw.color (0.0, 0.0, 0.0);
611 filledrect (float (x-2))
612 (float (y-2))
613 (float (x+2) +. w)
614 (float (y + fstate.fontsize + 2));
615 GlDraw.color color;
616 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
617 begintiles ();
620 | None ->
621 endtiles ();
622 let w =
623 let lw = state.winw - x in
624 min lw w
625 and h =
626 let lh = state.winh - y in
627 min lh h
629 if conf.invert
630 then GlTex.env (`mode `blend);
631 begin match state.checkerstexid with
632 | Some id ->
633 Gl.enable `texture_2d;
634 GlTex.bind_texture ~target:`texture_2d id;
635 let x0 = float x
636 and y0 = float y
637 and x1 = float (x+w)
638 and y1 = float (y+h) in
640 let tw = float w /. 16.0
641 and th = float h /. 16.0 in
642 let tx0 = float tilex /. 16.0
643 and ty0 = float tiley /. 16.0 in
644 let tx1 = tx0 +. tw
645 and ty1 = ty0 +. th in
646 Raw.sets_float state.vraw ~pos:0
647 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
648 Raw.sets_float state.traw ~pos:0
649 [| tx0; ty0; tx0; ty1; tx1; ty0; tx1; ty1 |];
650 GlArray.vertex `two state.vraw;
651 GlArray.tex_coord `two state.traw;
652 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
653 Gl.disable `texture_2d;
655 | None ->
656 GlDraw.color (1.0, 1.0, 1.0);
657 filledrect (float x) (float y) (float (x+w)) (float (y+h));
658 end;
659 if conf.invert
660 then GlTex.env (`mode `modulate);
661 if w > 128 && h > fstate.fontsize + 10
662 then (
663 let c = if conf.invert then 1.0 else 0.0 in
664 GlDraw.color (c, c, c);
665 let c, r =
666 if conf.verbose
667 then (col*conf.tilew, row*conf.tileh)
668 else col, row
670 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
672 GlDraw.color color;
673 begintiles ();
675 itertiles l f;
676 endtiles ();
679 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
681 let tilevisible1 l x y =
682 let ax0 = l.pagex
683 and ax1 = l.pagex + l.pagevw
684 and ay0 = l.pagey
685 and ay1 = l.pagey + l.pagevh in
687 let bx0 = x
688 and by0 = y in
689 let bx1 = min (bx0 + conf.tilew) l.pagew
690 and by1 = min (by0 + conf.tileh) l.pageh in
692 let rx0 = max ax0 bx0
693 and ry0 = max ay0 by0
694 and rx1 = min ax1 bx1
695 and ry1 = min ay1 by1 in
697 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
698 nonemptyintersection
701 let tilevisible layout n x y =
702 let rec findpageinlayout m = function
703 | l :: rest when l.pageno = n ->
704 tilevisible1 l x y || (
705 match conf.columns with
706 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
707 | Csplit _
708 | Csingle _
709 | 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 _
886 | Textentry _
887 | View -> ()
888 end;
889 begin match state.mode with
890 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
891 if not (pagevisible layout pageno)
892 then (
893 match state.layout with
894 | [] -> ()
895 | l :: _ ->
896 state.mode <- Birdseye (
897 conf, leftx, l.pageno, hooverpageno, anchor
900 | LinkNav lt ->
901 begin match lt with
902 | Ltnotready (_, dir)
903 | Ltgendir dir ->
904 let linknav =
905 let rec loop = function
906 | [] -> lt
907 | l :: rest ->
908 match getopaque l.pageno with
909 | None -> Ltnotready (l.pageno, dir)
910 | Some opaque ->
911 let link =
912 let ld =
913 if dir = 0
914 then LDfirstvisible (l.pagex, l.pagey, dir)
915 else (
916 if dir > 0 then LDfirst else LDlast
919 findlink opaque ld
921 match link with
922 | Lnotfound -> loop rest
923 | Lfound n ->
924 showlinktype (getlink opaque n);
925 Ltexact (l.pageno, n)
927 loop state.layout
929 state.mode <- LinkNav linknav
930 | Ltexact _ -> ()
932 | Textentry _
933 | View -> ()
934 end;
935 preload layout;
937 state.ghyll <- noghyll;
938 if conf.updatecurs
939 then (
940 let mx, my = state.mpos in
941 updateunder mx my;
945 let conttiling pageno opaque =
946 tilepage pageno opaque
947 (if conf.preload
948 then preloadlayout state.x state.y state.winw state.winh
949 else state.layout)
952 let gotoxy_and_clear_text x y =
953 if not conf.verbose then state.text <- E.s;
954 gotoxy x y;
957 let getanchory (n, top, dtop) =
958 let y, h = getpageyh n in
959 if conf.presentation
960 then
961 let ips = calcips h in
962 y + truncate (top*.float h -. dtop*.float ips) + ips;
963 else
964 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
967 let gotoanchor anchor =
968 gotoxy state.x (getanchory anchor);
971 let addnav () =
972 cbput state.hists.nav (getanchor ());
975 let getnav dir =
976 let anchor = cbgetc state.hists.nav dir in
977 getanchory anchor;
980 let gotoghyll1 single y =
981 let scroll f n a b =
982 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
983 let snake f a b =
984 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
985 if f < a
986 then s (float f /. float a)
987 else (
988 if f > b
989 then 1.0 -. s ((float (f-b) /. float (n-b)))
990 else 1.0
993 snake f a b
994 and summa n a b =
995 let ins = float a *. 0.5
996 and outs = float (n-b) *. 0.5 in
997 let ones = b - a in
998 ins +. outs +. float ones
1000 let rec set nab y sy =
1001 let (_N, _A, _B), y =
1002 if single
1003 then
1004 let scl = if y > sy then 2 else -2 in
1005 let _N, _, _ = nab in
1006 (_N,0,_N), y+conf.scrollstep*scl
1007 else nab,y in
1008 let sum = summa _N _A _B in
1009 let dy = float (y - sy) in
1010 state.ghyll <- (
1011 let rec gf n y1 o =
1012 if n >= _N
1013 then state.ghyll <- noghyll
1014 else
1015 let go n =
1016 let s = scroll n _N _A _B in
1017 let y1 = y1 +. ((s *. dy) /. sum) in
1018 gotoxy_and_clear_text state.x (truncate y1);
1019 state.ghyll <- gf (n+1) y1;
1021 match o with
1022 | None -> go n
1023 | Some y' when single -> set nab y' state.y
1024 | Some y' -> set (_N/2, 1, 1) y' state.y
1026 gf 0 (float state.y)
1029 match conf.ghyllscroll with
1030 | Some nab when not conf.presentation ->
1031 if state.ghyll == noghyll
1032 then set nab y state.y
1033 else state.ghyll (Some y)
1034 | _ ->
1035 gotoxy_and_clear_text state.x y
1038 let gotoghyll = gotoghyll1 false;;
1040 let gotopage n top =
1041 let y, h = getpageyh n in
1042 let y = y + (truncate (top *. float h)) in
1043 gotoghyll y
1046 let gotopage1 n top =
1047 let y = getpagey n in
1048 let y = y + top in
1049 gotoghyll y
1052 let invalidate s f =
1053 state.redisplay <- false;
1054 state.layout <- [];
1055 state.pdims <- [];
1056 state.rects <- [];
1057 state.rects1 <- [];
1058 match state.geomcmds with
1059 | ps, [] when emptystr ps ->
1060 f ();
1061 state.geomcmds <- s, [];
1063 | ps, [] ->
1064 state.geomcmds <- ps, [s, f];
1066 | ps, (s', _) :: rest when s' = s ->
1067 state.geomcmds <- ps, ((s, f) :: rest);
1069 | ps, cmds ->
1070 state.geomcmds <- ps, ((s, f) :: cmds);
1073 let flushpages () =
1074 Hashtbl.iter (fun _ opaque ->
1075 wcmd "freepage %s" (~> opaque);
1076 ) state.pagemap;
1077 Hashtbl.clear state.pagemap;
1080 let flushtiles () =
1081 if not (Queue.is_empty state.tilelru)
1082 then (
1083 Queue.iter (fun (k, p, s) ->
1084 wcmd "freetile %s" (~> p);
1085 state.memused <- state.memused - s;
1086 Hashtbl.remove state.tilemap k;
1087 ) state.tilelru;
1088 state.uioh#infochanged Memused;
1089 Queue.clear state.tilelru;
1091 load state.layout;
1094 let stateh h =
1095 let h = truncate (float h*.conf.zoom) in
1096 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
1097 h - d
1100 let opendoc path password =
1101 state.path <- path;
1102 state.password <- password;
1103 state.gen <- state.gen + 1;
1104 state.docinfo <- [];
1105 state.outlines <- [||];
1107 flushpages ();
1108 setaalevel conf.aalevel;
1109 let titlepath =
1110 if emptystr state.origin
1111 then path
1112 else state.origin
1114 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
1115 wcmd "open %d %d %d %s\000%s\000%s\000"
1116 (btod !wtmode) (btod !cxack) (btod conf.usedoccss)
1117 path password conf.css;
1118 invalidate "reqlayout"
1119 (fun () ->
1120 wcmd "reqlayout %d %d %d %s\000"
1121 conf.angle (FMTE.to_int conf.fitmodel)
1122 (stateh state.winh) state.nameddest
1124 state.help <-
1125 let sl = keystostrlist conf in
1126 let rec loop accu =
1127 function | [] -> accu
1128 | s :: rest -> loop ((s, 0, Noaction) :: accu) rest
1129 in makehelp () @ (("", 0, Noaction) :: loop [] sl) |> Array.of_list
1132 let reload () =
1133 state.anchor <- getanchor ();
1134 opendoc state.path state.password;
1137 let scalecolor c =
1138 let c = c *. conf.colorscale in
1139 (c, c, c);
1142 let scalecolor2 (r, g, b) =
1143 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1146 let docolumns columns =
1147 match columns with
1148 | Csingle _ ->
1149 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1150 let rec loop pageno pdimno pdim y ph pdims =
1151 if pageno = state.pagecount
1152 then ()
1153 else
1154 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1155 match pdims with
1156 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1157 pdimno+1, pdim, rest
1158 | _ ->
1159 pdimno, pdim, pdims
1161 let x = max 0 (((state.winw - w) / 2) - xoff) in
1162 let y =
1163 y + (if conf.presentation
1164 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1165 else (if pageno = 0 then 0 else conf.interpagespace)
1168 a.(pageno) <- (pdimno, x, y, pdim);
1169 loop (pageno+1) pdimno pdim (y + h) h pdims
1171 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1172 conf.columns <- Csingle a;
1174 | Cmulti ((columns, coverA, coverB), _) ->
1175 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1176 let rec loop pageno pdimno pdim x y rowh pdims =
1177 let rec fixrow m =
1178 if m = pageno then () else
1179 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1180 if h < rowh
1181 then (
1182 let y = y + (rowh - h) / 2 in
1183 a.(m) <- (pdimno, x, y, pdim);
1185 fixrow (m+1)
1187 if pageno = state.pagecount
1188 then fixrow (((pageno - 1) / columns) * columns)
1189 else
1190 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1191 match pdims with
1192 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1193 pdimno+1, pdim, rest
1194 | _ ->
1195 pdimno, pdim, pdims
1197 let x, y, rowh' =
1198 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1199 then (
1200 let x = (state.winw - w) / 2 in
1201 let ips =
1202 if conf.presentation then calcips h else conf.interpagespace in
1203 x, y + ips + rowh, h
1205 else (
1206 if (pageno - coverA) mod columns = 0
1207 then (
1208 let x = max 0 (state.winw - state.w) / 2 in
1209 let y =
1210 if conf.presentation
1211 then
1212 let ips = calcips h in
1213 y + (if pageno = 0 then 0 else calcips rowh + ips)
1214 else
1215 y + (if pageno = 0 then 0 else conf.interpagespace)
1217 x, y + rowh, h
1219 else x, y, max rowh h
1222 let y =
1223 if pageno > 1 && (pageno - coverA) mod columns = 0
1224 then (
1225 let y =
1226 if pageno = columns && conf.presentation
1227 then (
1228 let ips = calcips rowh in
1229 for i = 0 to pred columns
1231 let (pdimno, x, y, pdim) = a.(i) in
1232 a.(i) <- (pdimno, x, y+ips, pdim)
1233 done;
1234 y+ips;
1236 else y
1238 fixrow (pageno - columns);
1241 else y
1243 a.(pageno) <- (pdimno, x, y, pdim);
1244 let x = x + w + xoff*2 + conf.interpagespace in
1245 loop (pageno+1) pdimno pdim x y rowh' pdims
1247 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1248 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1250 | Csplit (c, _) ->
1251 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1252 let rec loop pageno pdimno pdim y pdims =
1253 if pageno = state.pagecount
1254 then ()
1255 else
1256 let pdimno, ((_, w, h, _) as pdim), pdims =
1257 match pdims with
1258 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1259 pdimno+1, pdim, rest
1260 | _ ->
1261 pdimno, pdim, pdims
1263 let cw = w / c in
1264 let rec loop1 n x y =
1265 if n = c then y else (
1266 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1267 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1270 let y = loop1 0 0 y in
1271 loop (pageno+1) pdimno pdim y pdims
1273 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1274 conf.columns <- Csplit (c, a);
1277 let represent () =
1278 docolumns conf.columns;
1279 state.maxy <- calcheight ();
1280 if state.reprf == noreprf
1281 then (
1282 match state.mode with
1283 | Birdseye (_, _, pageno, _, _) ->
1284 let y, h = getpageyh pageno in
1285 let top = (state.winh - h) / 2 in
1286 gotoxy state.x (max 0 (y - top))
1287 | Textentry _
1288 | View
1289 | LinkNav _ ->
1290 let y = getanchory state.anchor in
1291 let y = min y (state.maxy - state.winh) in
1292 gotoxy state.x y;
1294 else (
1295 state.reprf ();
1296 state.reprf <- noreprf;
1300 let reshape ?(firsttime=false) w h =
1301 GlDraw.viewport ~x:0 ~y:0 ~w ~h;
1302 if not firsttime && nogeomcmds state.geomcmds
1303 then state.anchor <- getanchor ();
1305 state.winw <- w;
1306 let w = truncate (float w *. conf.zoom) in
1307 let w = max w 2 in
1308 state.winh <- h;
1309 setfontsize fstate.fontsize;
1310 GlMat.mode `modelview;
1311 GlMat.load_identity ();
1313 GlMat.mode `projection;
1314 GlMat.load_identity ();
1315 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1316 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1317 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
1319 let relx =
1320 if conf.zoom <= 1.0
1321 then 0.0
1322 else float state.x /. float state.w
1324 invalidate "geometry"
1325 (fun () ->
1326 state.w <- w;
1327 if not firsttime
1328 then state.x <- truncate (relx *. float w);
1329 let w =
1330 match conf.columns with
1331 | Csingle _ -> w
1332 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1333 | Csplit (c, _) -> w * c
1335 wcmd "geometry %d %d %d"
1336 w (stateh h) (FMTE.to_int conf.fitmodel)
1340 let enttext () =
1341 let len = String.length state.text in
1342 let x0 = if conf.leftscroll then vscrollw () else 0 in
1343 let drawstring s =
1344 let hscrollh =
1345 match state.mode with
1346 | Textentry _ | View | LinkNav _ ->
1347 let h, _, _ = state.uioh#scrollpw in
1349 | Birdseye _ -> 0
1351 let rect x w =
1352 filledrect x (float (state.winh - (fstate.fontsize + 4) - hscrollh))
1353 (x+.w) (float (state.winh - hscrollh))
1356 let w = float (state.winw - 1 - vscrollw ()) in
1357 if state.progress >= 0.0 && state.progress < 1.0
1358 then (
1359 GlDraw.color (0.3, 0.3, 0.3);
1360 let w1 = w *. state.progress in
1361 rect (float x0) w1;
1362 GlDraw.color (0.0, 0.0, 0.0);
1363 rect (float x0+.w1) (float x0+.w-.w1)
1365 else (
1366 GlDraw.color (0.0, 0.0, 0.0);
1367 rect (float x0) w;
1370 GlDraw.color (1.0, 1.0, 1.0);
1371 drawstring
1372 fstate.fontsize
1373 (if conf.leftscroll then x0 + 2 else x0 + if len > 0 then 8 else 2)
1374 (state.winh - hscrollh - 5) s;
1376 let s =
1377 match state.mode with
1378 | Textentry ((prefix, text, _, _, _, _), _) ->
1379 let s =
1380 if len > 0
1381 then
1382 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1383 else
1384 Printf.sprintf "%s%s_" prefix text
1388 | Birdseye _
1389 | View
1390 | LinkNav _ -> state.text
1392 let s =
1393 if state.newerrmsgs
1394 then (
1395 if not (istextentry state.mode) && state.uioh#eformsgs
1396 then
1397 let s1 = "(press 'e' to review error messasges)" in
1398 if nonemptystr s then s ^ " " ^ s1 else s1
1399 else s
1401 else s
1403 if nonemptystr s
1404 then drawstring s
1407 let gctiles () =
1408 let len = Queue.length state.tilelru in
1409 let layout = lazy (
1410 match state.throttle with
1411 | None ->
1412 if conf.preload
1413 then preloadlayout state.x state.y state.winw state.winh
1414 else state.layout
1415 | Some (layout, _, _) ->
1416 layout
1417 ) in
1418 let rec loop qpos =
1419 if state.memused <= conf.memlimit
1420 then ()
1421 else (
1422 if qpos < len
1423 then
1424 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1425 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1426 let (_, pw, ph, _) = getpagedim n in
1427 if gen = state.gen
1428 && colorspace = conf.colorspace
1429 && angle = conf.angle
1430 && pagew = pw
1431 && pageh = ph
1432 && (
1433 let x = col*conf.tilew
1434 and y = row*conf.tileh in
1435 tilevisible (Lazy.force_val layout) n x y
1437 then Queue.push lruitem state.tilelru
1438 else (
1439 freepbo p;
1440 wcmd "freetile %s" (~> p);
1441 state.memused <- state.memused - s;
1442 state.uioh#infochanged Memused;
1443 Hashtbl.remove state.tilemap k;
1445 loop (qpos+1)
1448 loop 0
1451 let onpagerect pageno f =
1452 let b =
1453 match conf.columns with
1454 | Cmulti (_, b) -> b
1455 | Csingle b -> b
1456 | Csplit (_, b) -> b
1458 if pageno >= 0 && pageno < Array.length b
1459 then
1460 let (_, _, _, (_, w, h, _)) = b.(pageno) in
1461 f w h
1464 let gotopagexy1 wtmode pageno x y =
1465 let _,w1,h1,leftx = getpagedim pageno in
1466 let top = y /. (float h1) in
1467 let left = x /. (float w1) in
1468 let py, w, h = getpageywh pageno in
1469 let wh = state.winh in
1470 let x = left *. (float w) in
1471 let x = leftx + state.x + truncate x in
1472 let sx =
1473 if x < 0 || x >= state.winw
1474 then state.x - x
1475 else state.x
1477 let pdy = truncate (top *. float h) in
1478 let y' = py + pdy in
1479 let dy = y' - state.y in
1480 let sy =
1481 if x != state.x || not (dy > 0 && dy < wh)
1482 then (
1483 if conf.presentation
1484 then
1485 if abs (py - y') > wh
1486 then y'
1487 else py
1488 else y';
1490 else state.y
1492 if state.x != sx || state.y != sy
1493 then (
1494 let x, y =
1495 if wtmode
1496 then (
1497 let ww = state.winw in
1498 let qx = sx / ww
1499 and qy = pdy / wh in
1500 let x = qx * ww
1501 and y = py + qy * wh in
1502 let x = if -x + ww > w1 then -(w1-ww) else x
1503 and y' = if y + wh > state.maxy then state.maxy - wh else y in
1504 let y =
1505 if conf.presentation
1506 then
1507 if abs (py - y') > wh
1508 then y'
1509 else py
1510 else y';
1512 (x, y)
1514 else (sx, sy)
1516 gotoxy_and_clear_text x y;
1518 else gotoxy_and_clear_text state.x state.y;
1521 let gotopagexy wtmode pageno x y =
1522 match state.mode with
1523 | Birdseye _ -> gotopage pageno 0.0
1524 | Textentry _
1525 | View
1526 | LinkNav _ -> gotopagexy1 wtmode pageno x y
1529 let getpassword () =
1530 let passcmd = getenvwithdef "LLPP_ASKPASS" conf.passcmd in
1531 if emptystr passcmd
1532 then E.s
1533 else getcmdoutput
1534 (fun s ->
1535 impmsg "error getting password: %s" s;
1536 dolog "%s" s) passcmd;
1539 let pgoto opaque pageno x y =
1540 let pdimno = getpdimno pageno in
1541 let x, y = project opaque pageno pdimno x y in
1542 gotopagexy false pageno x y;
1545 let act cmds =
1546 (* dolog "%S" cmds; *)
1547 let spl = splitatchar cmds ' ' in
1548 let scan s fmt f =
1549 try Scanf.sscanf s fmt f
1550 with exn ->
1551 dolog "error processing '%S': %s" cmds @@ exntos exn;
1552 exit 1
1554 let addoutline outline =
1555 match state.currently with
1556 | Outlining outlines ->
1557 state.currently <- Outlining (outline :: outlines)
1558 | Idle -> state.currently <- Outlining [outline]
1559 | Loading _
1560 | Tiling _ ->
1561 dolog "invalid outlining state";
1562 logcurrently state.currently
1564 match spl with
1565 | "clear", "" ->
1566 state.pdims <- [];
1567 state.uioh#infochanged Pdim;
1569 | "clearrects", "" ->
1570 state.rects <- state.rects1;
1571 G.postRedisplay "clearrects";
1573 | "continue", args ->
1574 let n = scan args "%u" (fun n -> n) in
1575 state.pagecount <- n;
1576 begin match state.currently with
1577 | Outlining l ->
1578 state.currently <- Idle;
1579 state.outlines <- Array.of_list (List.rev l)
1580 | Idle
1581 | Loading _
1582 | Tiling _ -> ()
1583 end;
1585 let cur, cmds = state.geomcmds in
1586 if emptystr cur
1587 then failwith "umpossible";
1589 begin match List.rev cmds with
1590 | [] ->
1591 state.geomcmds <- E.s, [];
1592 state.throttle <- None;
1593 represent ();
1594 | (s, f) :: rest ->
1595 f ();
1596 state.geomcmds <- s, List.rev rest;
1597 end;
1598 if conf.maxwait = None && not !wtmode
1599 then G.postRedisplay "continue";
1601 | "msg", args ->
1602 showtext ' ' args
1604 | "vmsg", args ->
1605 if conf.verbose
1606 then showtext ' ' args
1608 | "emsg", args ->
1609 Buffer.add_string state.errmsgs args;
1610 state.newerrmsgs <- true;
1611 G.postRedisplay "error message"
1613 | "progress", args ->
1614 let progress, text =
1615 scan args "%f %n"
1616 (fun f pos ->
1617 f, String.sub args pos (String.length args - pos))
1619 state.text <- text;
1620 state.progress <- progress;
1621 G.postRedisplay "progress"
1623 | "firstmatch", args ->
1624 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1625 scan args "%u %d %f %f %f %f %f %f %f %f"
1626 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1627 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1629 let y = (getpagey pageno) + truncate y0 in
1630 let x =
1631 if conf.zoom > 1.0
1632 then state.winw/2
1633 else state.x
1635 addnav ();
1636 gotoxy x y;
1637 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1638 state.rects1 <- [pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)]
1640 | "match", args ->
1641 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1642 scan args "%u %d %f %f %f %f %f %f %f %f"
1643 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1644 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1646 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1647 state.rects1 <-
1648 (pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1650 | "page", args ->
1651 let pageopaques, t = scan args "%s %f" (fun p t -> p, t) in
1652 let pageopaque = ~< pageopaques in
1653 begin match state.currently with
1654 | Loading (l, gen) ->
1655 vlog "page %d took %f sec" l.pageno t;
1656 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1657 begin match state.throttle with
1658 | None ->
1659 let preloadedpages =
1660 if conf.preload
1661 then preloadlayout state.x state.y state.winw state.winh
1662 else state.layout
1664 let evict () =
1665 let set =
1666 List.fold_left (fun s l -> IntSet.add l.pageno s)
1667 IntSet.empty preloadedpages
1669 let evictedpages =
1670 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1671 if not (IntSet.mem pageno set)
1672 then (
1673 wcmd "freepage %s" (~> opaque);
1674 key :: accu
1676 else accu
1677 ) state.pagemap []
1679 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1681 evict ();
1682 state.currently <- Idle;
1683 if gen = state.gen
1684 then (
1685 tilepage l.pageno pageopaque state.layout;
1686 load state.layout;
1687 load preloadedpages;
1688 let visible = pagevisible state.layout l.pageno in
1689 if visible
1690 then (
1691 match state.mode with
1692 | LinkNav (Ltnotready (pageno, dir)) ->
1693 if pageno = l.pageno
1694 then (
1695 let link =
1696 let ld =
1697 if dir = 0
1698 then LDfirstvisible (l.pagex, l.pagey, dir)
1699 else (
1700 if dir > 0 then LDfirst else LDlast
1703 findlink pageopaque ld
1705 match link with
1706 | Lnotfound -> ()
1707 | Lfound n ->
1708 showlinktype (getlink pageopaque n);
1709 state.mode <- LinkNav (Ltexact (l.pageno, n))
1711 | LinkNav (Ltgendir _)
1712 | LinkNav (Ltexact _)
1713 | View
1714 | Birdseye _
1715 | Textentry _ -> ()
1718 if visible && layoutready state.layout
1719 then (
1720 G.postRedisplay "page";
1724 | Some (layout, _, _) ->
1725 state.currently <- Idle;
1726 tilepage l.pageno pageopaque layout;
1727 load state.layout
1728 end;
1730 | Idle
1731 | Tiling _
1732 | Outlining _ ->
1733 dolog "Inconsistent loading state";
1734 logcurrently state.currently;
1735 exit 1
1738 | "tile" , args ->
1739 let (x, y, opaques, size, t) =
1740 scan args "%u %u %s %u %f"
1741 (fun x y p size t -> (x, y, p, size, t))
1743 let opaque = ~< opaques in
1744 begin match state.currently with
1745 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1746 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1748 unmappbo opaque;
1749 if tilew != conf.tilew || tileh != conf.tileh
1750 then (
1751 wcmd "freetile %s" (~> opaque);
1752 state.currently <- Idle;
1753 load state.layout;
1755 else (
1756 puttileopaque l col row gen cs angle opaque size t;
1757 state.memused <- state.memused + size;
1758 state.uioh#infochanged Memused;
1759 gctiles ();
1760 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1761 opaque, size) state.tilelru;
1763 let layout =
1764 match state.throttle with
1765 | None -> state.layout
1766 | Some (layout, _, _) -> layout
1769 state.currently <- Idle;
1770 if gen = state.gen
1771 && conf.colorspace = cs
1772 && conf.angle = angle
1773 && tilevisible layout l.pageno x y
1774 then conttiling l.pageno pageopaque;
1776 begin match state.throttle with
1777 | None ->
1778 preload state.layout;
1779 if gen = state.gen
1780 && conf.colorspace = cs
1781 && conf.angle = angle
1782 && tilevisible state.layout l.pageno x y
1783 && (not !wtmode || layoutready state.layout)
1784 then G.postRedisplay "tile nothrottle";
1786 | Some (layout, y, _) ->
1787 let ready = layoutready layout in
1788 if ready
1789 then (
1790 state.y <- y;
1791 state.layout <- layout;
1792 state.throttle <- None;
1793 G.postRedisplay "throttle";
1795 else load layout;
1796 end;
1799 | Idle
1800 | Loading _
1801 | Outlining _ ->
1802 dolog "Inconsistent tiling state";
1803 logcurrently state.currently;
1804 exit 1
1807 | "pdim", args ->
1808 let (n, w, h, _) as pdim =
1809 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1811 let pdim =
1812 match conf.fitmodel with
1813 | FitWidth -> pdim
1814 | FitPage | FitProportional ->
1815 match conf.columns with
1816 | Csplit _ -> (n, w, h, 0)
1817 | Csingle _ | Cmulti _ -> pdim
1819 state.pdims <- pdim :: state.pdims;
1820 state.uioh#infochanged Pdim
1822 | "o", args ->
1823 let (l, n, t, h, pos) =
1824 scan args "%u %u %d %u %n"
1825 (fun l n t h pos -> l, n, t, h, pos)
1827 let s = String.sub args pos (String.length args - pos) in
1828 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
1830 | "ou", args ->
1831 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
1832 let s = String.sub args pos len in
1833 let pos2 = pos + len + 1 in
1834 let uri = String.sub args pos2 (String.length args - pos2) in
1835 addoutline (s, l, Ouri uri)
1837 | "on", args ->
1838 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
1839 let s = String.sub args pos (String.length args - pos) in
1840 addoutline (s, l, Onone)
1842 | "a", args ->
1843 let (n, l, t) =
1844 scan args "%u %d %d" (fun n l t -> n, l, t)
1846 state.reprf <- (fun () -> gotopagexy !wtmode n (float l) (float t))
1848 | "info", args ->
1849 let pos = nindex args '\t' in
1850 let s =
1851 if pos >= 0
1852 then
1853 if substratis args 0 "Title"
1854 then
1855 let s = String.sub args (pos+1) @@ String.length args - pos - 1 in
1856 conf.title <- s;
1857 Wsi.settitle s;
1858 args
1859 else
1860 if substratis args 0 "CreationDate"
1861 then (
1862 if String.length args >= pos + 7
1863 && args.[pos+1] = 'D' && args.[pos+2] = ':'
1864 then
1865 let b = Buffer.create 18 in
1866 Buffer.add_string b "CreationDate\t";
1867 let sub p l c =
1869 Buffer.add_substring b args (pos+p+1) l;
1870 Buffer.add_char b c;
1871 with exn -> Buffer.add_string b @@ exntos exn
1873 sub 2 4 '/';
1874 sub 6 2 '/';
1875 sub 8 2 ' ';
1876 sub 10 2 ':';
1877 sub 12 2 ':';
1878 sub 14 2 ' ';
1879 Buffer.add_char b '[';
1880 Buffer.add_substring b args (pos+1)
1881 (String.length args - pos - 1);
1882 Buffer.add_char b ']';
1883 Buffer.contents b
1884 else args
1886 else args
1887 else args
1889 state.docinfo <- (1, s) :: state.docinfo
1891 | "infoend", "" ->
1892 state.docinfo <- List.rev state.docinfo;
1893 state.uioh#infochanged Docinfo
1895 | "pass", args ->
1896 if args = "fail"
1897 then Wsi.settitle "Wrong password";
1898 let password = getpassword () in
1899 if emptystr password
1900 then error "document is password protected"
1901 else opendoc state.path password
1903 | _ ->
1904 error "unknown cmd `%S'" cmds
1907 let onhist cb =
1908 let rc = cb.rc in
1909 let action = function
1910 | HCprev -> cbget cb ~-1
1911 | HCnext -> cbget cb 1
1912 | HCfirst -> cbget cb ~-(cb.rc)
1913 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1914 and cancel () = cb.rc <- rc
1915 in (action, cancel)
1918 let search pattern forward =
1919 match conf.columns with
1920 | Csplit _ -> impmsg "searching does not work properly in split columns mode"
1921 | Csingle _
1922 | Cmulti _ ->
1923 if nonemptystr pattern
1924 then
1925 let pn, py =
1926 match state.layout with
1927 | [] -> 0, 0
1928 | l :: _ ->
1929 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1931 wcmd "search %d %d %d %d,%s\000"
1932 (btod conf.icase) pn py (btod forward) pattern;
1935 let intentry text key =
1936 let text =
1937 if key >= 32 && key < 127
1938 then
1939 let c = Char.chr key in
1940 match c with
1941 | '0' .. '9' -> addchar text c
1942 | _ ->
1943 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1944 text
1945 else (
1946 state.text <- Printf.sprintf "invalid key (%d)" key;
1947 text
1950 TEcont text
1953 let linknact f s =
1954 if nonemptystr s
1955 then (
1956 let n =
1957 let l = String.length s in
1958 let rec loop pos n =
1959 if pos = l
1960 then n
1961 else
1962 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
1963 loop (pos+1) (n*26 + m)
1964 in loop 0 0
1966 let rec loop n = function
1967 | [] -> ()
1968 | l :: rest ->
1969 match getopaque l.pageno with
1970 | None -> loop n rest
1971 | Some opaque ->
1972 let m = getlinkcount opaque in
1973 if n < m
1974 then (
1975 let under = getlink opaque n in
1976 f under
1978 else loop (n-m) rest
1980 loop n state.layout;
1984 let linknentry text key =
1985 if key >= 32 && key < 127
1986 then
1987 let text = addchar text (Char.chr key) in
1988 linknact (fun under -> state.text <- undertext under) text;
1989 TEcont text
1990 else (
1991 state.text <- Printf.sprintf "invalid key %d" key;
1992 TEcont text
1996 let textentry text key =
1997 if Wsi.isspecialkey key
1998 then TEcont text
1999 else TEcont (text ^ toutf8 key)
2002 let reqlayout angle fitmodel =
2003 match state.throttle with
2004 | None ->
2005 if nogeomcmds state.geomcmds
2006 then state.anchor <- getanchor ();
2007 conf.angle <- angle mod 360;
2008 if conf.angle != 0
2009 then (
2010 match state.mode with
2011 | LinkNav _ -> state.mode <- View
2012 | Birdseye _
2013 | Textentry _
2014 | View -> ()
2016 conf.fitmodel <- fitmodel;
2017 invalidate
2018 "reqlayout"
2019 (fun () ->
2020 wcmd "reqlayout %d %d %d"
2021 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
2023 | _ -> ()
2026 let settrim trimmargins trimfuzz =
2027 if nogeomcmds state.geomcmds
2028 then state.anchor <- getanchor ();
2029 conf.trimmargins <- trimmargins;
2030 conf.trimfuzz <- trimfuzz;
2031 let x0, y0, x1, y1 = trimfuzz in
2032 invalidate
2033 "settrim" (fun () ->
2034 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2035 flushpages ();
2038 let setzoom zoom =
2039 match state.throttle with
2040 | None ->
2041 let zoom = max 0.0001 zoom in
2042 if zoom <> conf.zoom
2043 then (
2044 state.prevzoom <- (conf.zoom, state.x);
2045 conf.zoom <- zoom;
2046 reshape state.winw state.winh;
2047 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2050 | Some (layout, y, started) ->
2051 let time =
2052 match conf.maxwait with
2053 | None -> 0.0
2054 | Some t -> t
2056 let dt = now () -. started in
2057 if dt > time
2058 then (
2059 state.y <- y;
2060 load layout;
2064 let pivotzoom ?(vw=min state.w state.winw)
2065 ?(vh=min (state.maxy-state.y) state.winh)
2066 ?(x=vw/2) ?(y=vh/2) zoom =
2067 let w = float state.w /. zoom in
2068 let hw = w /. 2.0 in
2069 let ratio = float vh /. float vw in
2070 let hh = hw *. ratio in
2071 let x0 = if zoom < 1.0 then 0.0 else float x -. hw in
2072 let y0 = float y -. hh in
2073 gotoxy (state.x - truncate x0) (state.y + truncate y0);
2074 setzoom zoom;
2077 let pivotzoom ?vw ?vh ?x ?y zoom =
2078 if nogeomcmds state.geomcmds then pivotzoom ?vw ?vh ?x ?y zoom
2081 let setcolumns mode columns coverA coverB =
2082 state.prevcolumns <- Some (conf.columns, conf.zoom);
2083 if columns < 0
2084 then (
2085 if isbirdseye mode
2086 then impmsg "split mode doesn't work in bird's eye"
2087 else (
2088 conf.columns <- Csplit (-columns, E.a);
2089 state.x <- 0;
2090 conf.zoom <- 1.0;
2093 else (
2094 if columns < 2
2095 then (
2096 conf.columns <- Csingle E.a;
2097 state.x <- 0;
2098 setzoom 1.0;
2100 else (
2101 conf.columns <- Cmulti ((columns, coverA, coverB), E.a);
2102 conf.zoom <- 1.0;
2105 reshape state.winw state.winh;
2108 let resetmstate () =
2109 state.mstate <- Mnone;
2110 Wsi.setcursor Wsi.CURSOR_INHERIT;
2113 let enterbirdseye () =
2114 let zoom = float conf.thumbw /. float state.winw in
2115 let birdseyepageno =
2116 let cy = state.winh / 2 in
2117 let fold = function
2118 | [] -> 0
2119 | l :: rest ->
2120 let rec fold best = function
2121 | [] -> best.pageno
2122 | l :: rest ->
2123 let d = cy - (l.pagedispy + l.pagevh/2)
2124 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2125 if abs d < abs dbest
2126 then fold l rest
2127 else best.pageno
2128 in fold l rest
2130 fold state.layout
2132 state.mode <-
2133 Birdseye (
2134 { conf with zoom = conf.zoom },
2135 state.x, birdseyepageno, -1, getanchor ()
2137 resetmstate ();
2138 conf.zoom <- zoom;
2139 conf.presentation <- false;
2140 conf.interpagespace <- 10;
2141 conf.hlinks <- false;
2142 conf.fitmodel <- FitPage;
2143 state.x <- 0;
2144 conf.maxwait <- None;
2145 conf.columns <- (
2146 match conf.beyecolumns with
2147 | Some c ->
2148 conf.zoom <- 1.0;
2149 Cmulti ((c, 0, 0), E.a)
2150 | None -> Csingle E.a
2152 if conf.verbose
2153 then
2154 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2155 (100.0*.zoom)
2156 else
2157 state.text <- E.s
2159 reshape state.winw state.winh;
2162 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2163 state.mode <- View;
2164 conf.zoom <- c.zoom;
2165 conf.presentation <- c.presentation;
2166 conf.interpagespace <- c.interpagespace;
2167 conf.maxwait <- c.maxwait;
2168 conf.hlinks <- c.hlinks;
2169 conf.fitmodel <- c.fitmodel;
2170 conf.beyecolumns <- (
2171 match conf.columns with
2172 | Cmulti ((c, _, _), _) -> Some c
2173 | Csingle _ -> None
2174 | Csplit _ -> failwith "leaving bird's eye split mode"
2176 conf.columns <- (
2177 match c.columns with
2178 | Cmulti (c, _) -> Cmulti (c, E.a)
2179 | Csingle _ -> Csingle E.a
2180 | Csplit (c, _) -> Csplit (c, E.a)
2182 if conf.verbose
2183 then
2184 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2185 (100.0*.conf.zoom)
2187 reshape state.winw state.winh;
2188 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2189 state.x <- leftx;
2192 let togglebirdseye () =
2193 match state.mode with
2194 | Birdseye vals -> leavebirdseye vals true
2195 | View -> enterbirdseye ()
2196 | Textentry _ | LinkNav _ -> ()
2199 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2200 let pageno = max 0 (pageno - incr) in
2201 let rec loop = function
2202 | [] -> gotopage1 pageno 0
2203 | l :: _ when l.pageno = pageno ->
2204 if l.pagedispy >= 0 && l.pagey = 0
2205 then G.postRedisplay "upbirdseye"
2206 else gotopage1 pageno 0
2207 | _ :: rest -> loop rest
2209 loop state.layout;
2210 state.text <- E.s;
2211 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2214 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2215 let pageno = min (state.pagecount - 1) (pageno + incr) in
2216 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2217 let rec loop = function
2218 | [] ->
2219 let y, h = getpageyh pageno in
2220 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2221 gotoxy state.x (clamp dy)
2222 | l :: _ when l.pageno = pageno ->
2223 if l.pagevh != l.pageh
2224 then gotoxy state.x (clamp (l.pageh - l.pagevh + conf.interpagespace))
2225 else G.postRedisplay "downbirdseye"
2226 | _ :: rest -> loop rest
2228 loop state.layout;
2229 state.text <- E.s;
2232 let optentry mode _ key =
2233 let btos b = if b then "on" else "off" in
2234 if key >= 32 && key < 127
2235 then
2236 let c = Char.chr key in
2237 match c with
2238 | 's' ->
2239 let ondone s =
2240 try conf.scrollstep <- int_of_string s with exn ->
2241 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2243 TEswitch ("scroll step: ", E.s, None, intentry, ondone, true)
2245 | 'A' ->
2246 let ondone s =
2248 conf.autoscrollstep <- boundastep state.winh (int_of_string s);
2249 if state.autoscroll <> None
2250 then state.autoscroll <- Some conf.autoscrollstep
2251 with exn ->
2252 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2254 TEswitch ("auto scroll step: ", E.s, None, intentry, ondone, true)
2256 | 'C' ->
2257 let ondone s =
2259 let n, a, b = multicolumns_of_string s in
2260 setcolumns mode n a b;
2261 with exn ->
2262 state.text <- Printf.sprintf "bad columns `%s': %s" s @@ exntos exn
2264 TEswitch ("columns: ", E.s, None, textentry, ondone, true)
2266 | 'Z' ->
2267 let ondone s =
2269 let zoom = float (int_of_string s) /. 100.0 in
2270 pivotzoom zoom
2271 with exn ->
2272 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2274 TEswitch ("zoom: ", E.s, None, intentry, ondone, true)
2276 | 't' ->
2277 let ondone s =
2279 conf.thumbw <- bound (int_of_string s) 2 4096;
2280 state.text <-
2281 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2282 begin match mode with
2283 | Birdseye beye ->
2284 leavebirdseye beye false;
2285 enterbirdseye ();
2286 | Textentry _
2287 | View
2288 | LinkNav _ -> ();
2290 with exn ->
2291 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2293 TEswitch ("thumbnail width: ", E.s, None, intentry, ondone, true)
2295 | 'R' ->
2296 let ondone s =
2297 match int_of_string s with
2298 | angle -> reqlayout angle conf.fitmodel
2299 | exception exn ->
2300 state.text <-
2301 Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2303 TEswitch ("rotation: ", E.s, None, intentry, ondone, true)
2305 | 'i' ->
2306 conf.icase <- not conf.icase;
2307 TEdone ("case insensitive search " ^ (btos conf.icase))
2309 | 'p' ->
2310 conf.preload <- not conf.preload;
2311 gotoxy state.x state.y;
2312 TEdone ("preload " ^ (btos conf.preload))
2314 | 'v' ->
2315 conf.verbose <- not conf.verbose;
2316 TEdone ("verbose " ^ (btos conf.verbose))
2318 | 'd' ->
2319 conf.debug <- not conf.debug;
2320 TEdone ("debug " ^ (btos conf.debug))
2322 | 'h' ->
2323 conf.maxhfit <- not conf.maxhfit;
2324 state.maxy <- calcheight ();
2325 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2327 | 'c' ->
2328 conf.crophack <- not conf.crophack;
2329 TEdone ("crophack " ^ btos conf.crophack)
2331 | 'a' ->
2332 let s =
2333 match conf.maxwait with
2334 | None ->
2335 conf.maxwait <- Some infinity;
2336 "always wait for page to complete"
2337 | Some _ ->
2338 conf.maxwait <- None;
2339 "show placeholder if page is not ready"
2341 TEdone s
2343 | 'f' ->
2344 conf.underinfo <- not conf.underinfo;
2345 TEdone ("underinfo " ^ btos conf.underinfo)
2347 | 'P' ->
2348 conf.savebmarks <- not conf.savebmarks;
2349 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2351 | 'S' ->
2352 let ondone s =
2354 let pageno, py =
2355 match state.layout with
2356 | [] -> 0, 0
2357 | l :: _ ->
2358 l.pageno, l.pagey
2360 conf.interpagespace <- int_of_string s;
2361 docolumns conf.columns;
2362 state.maxy <- calcheight ();
2363 let y = getpagey pageno in
2364 gotoxy state.x (y + py)
2365 with exn ->
2366 state.text <-
2367 Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2369 TEswitch ("vertical margin: ", E.s, None, intentry, ondone, true)
2371 | 'l' ->
2372 let fm =
2373 match conf.fitmodel with
2374 | FitProportional -> FitWidth
2375 | FitWidth | FitPage -> FitProportional
2377 reqlayout conf.angle fm;
2378 TEdone ("proportional display " ^ btos (fm == FitProportional))
2380 | 'T' ->
2381 settrim (not conf.trimmargins) conf.trimfuzz;
2382 TEdone ("trim margins " ^ btos conf.trimmargins)
2384 | 'I' ->
2385 conf.invert <- not conf.invert;
2386 TEdone ("invert colors " ^ btos conf.invert)
2388 | 'x' ->
2389 let ondone s =
2390 cbput state.hists.sel s;
2391 conf.selcmd <- s;
2393 TEswitch ("selection command: ", E.s, Some (onhist state.hists.sel),
2394 textentry, ondone, true)
2396 | 'M' ->
2397 if conf.pax == None
2398 then conf.pax <- Some (ref (0.0, 0, 0))
2399 else conf.pax <- None;
2400 TEdone ("PAX " ^ btos (conf.pax != None))
2402 | _ ->
2403 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2404 TEstop
2405 else
2406 TEcont state.text
2409 class type lvsource =
2410 object
2411 method getitemcount : int
2412 method getitem : int -> (string * int)
2413 method hasaction : int -> bool
2414 method exit :
2415 uioh:uioh ->
2416 cancel:bool ->
2417 active:int ->
2418 first:int ->
2419 pan:int ->
2420 uioh option
2421 method getactive : int
2422 method getfirst : int
2423 method getpan : int
2424 method getminfo : (int * int) array
2425 end;;
2427 class virtual lvsourcebase = object
2428 val mutable m_active = 0
2429 val mutable m_first = 0
2430 val mutable m_pan = 0
2431 method getactive = m_active
2432 method getfirst = m_first
2433 method getpan = m_pan
2434 method getminfo : (int * int) array = E.a
2435 end;;
2437 let textentrykeyboard
2438 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2439 state.text <- E.s;
2440 let key = Wsi.keypadtodigitkey key in
2441 let enttext te =
2442 state.mode <- Textentry (te, onleave);
2443 enttext ();
2444 G.postRedisplay "textentrykeyboard enttext";
2446 let histaction cmd =
2447 match opthist with
2448 | None -> ()
2449 | Some (action, _) ->
2450 state.mode <-
2451 Textentry (
2452 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
2454 G.postRedisplay "textentry histaction"
2456 match key with
2457 | @backspace ->
2458 if emptystr text && cancelonempty
2459 then (
2460 onleave Cancel;
2461 G.postRedisplay "textentrykeyboard after cancel";
2463 else
2464 let s = withoutlastutf8 text in
2465 enttext (c, s, opthist, onkey, ondone, cancelonempty)
2467 | @enter | @kpenter ->
2468 ondone text;
2469 onleave Confirm;
2470 G.postRedisplay "textentrykeyboard after confirm"
2472 | @up | @kpup -> histaction HCprev
2473 | @down | @kpdown -> histaction HCnext
2474 | @home | @kphome -> histaction HCfirst
2475 | @jend | @kpend -> histaction HClast
2477 | @escape ->
2478 if emptystr text
2479 then (
2480 begin match opthist with
2481 | None -> ()
2482 | Some (_, onhistcancel) -> onhistcancel ()
2483 end;
2484 onleave Cancel;
2485 state.text <- E.s;
2486 G.postRedisplay "textentrykeyboard after cancel2"
2488 else (
2489 enttext (c, E.s, opthist, onkey, ondone, cancelonempty)
2492 | @delete | @kpdelete -> ()
2494 | _ when key != 0 && not (Wsi.isspecialkey key) ->
2495 begin match onkey text key with
2496 | TEdone text ->
2497 ondone text;
2498 onleave Confirm;
2499 G.postRedisplay "textentrykeyboard after confirm2";
2501 | TEcont text ->
2502 enttext (c, text, opthist, onkey, ondone, cancelonempty);
2504 | TEstop ->
2505 onleave Cancel;
2506 G.postRedisplay "textentrykeyboard after cancel3"
2508 | TEswitch te ->
2509 state.mode <- Textentry (te, onleave);
2510 G.postRedisplay "textentrykeyboard switch";
2511 end;
2513 | _ ->
2514 vlog "unhandled key %s" (Wsi.keyname key)
2517 let firstof first active =
2518 if first > active || abs (first - active) > fstate.maxrows - 1
2519 then max 0 (active - (fstate.maxrows/2))
2520 else first
2523 let calcfirst first active =
2524 if active > first
2525 then
2526 let rows = active - first in
2527 if rows > fstate.maxrows then active - fstate.maxrows else first
2528 else active
2531 let scrollph y maxy =
2532 let sh = float (maxy + state.winh) /. float state.winh in
2533 let sh = float state.winh /. sh in
2534 let sh = max sh (float conf.scrollh) in
2536 let percent = float y /. float maxy in
2537 let position = (float state.winh -. sh) *. percent in
2539 let position =
2540 if position +. sh > float state.winh
2541 then float state.winh -. sh
2542 else position
2544 position, sh;
2547 let adderrmsg src msg =
2548 Buffer.add_string state.errmsgs msg;
2549 state.newerrmsgs <- true;
2550 G.postRedisplay src
2553 let adderrfmt src fmt =
2554 Format.ksprintf (fun s -> adderrmsg src s) fmt;
2557 let coe s = (s :> uioh);;
2559 class listview ~zebra ~helpmode ~(source:lvsource) ~trusted ~modehash =
2560 object (self)
2561 val m_pan = source#getpan
2562 val m_first = source#getfirst
2563 val m_active = source#getactive
2564 val m_qsearch = E.s
2565 val m_prev_uioh = state.uioh
2567 method private elemunder y =
2568 if y < 0
2569 then None
2570 else
2571 let n = y / (fstate.fontsize+1) in
2572 if m_first + n < source#getitemcount
2573 then (
2574 if source#hasaction (m_first + n)
2575 then Some (m_first + n)
2576 else None
2578 else None
2580 method display =
2581 Gl.enable `blend;
2582 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
2583 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2584 filledrect 0. 0. (float state.winw) (float state.winh);
2585 GlDraw.color (1., 1., 1.);
2586 Gl.enable `texture_2d;
2587 let fs = fstate.fontsize in
2588 let nfs = fs + 1 in
2589 let hw = state.winw/3 in
2590 let ww = fstate.wwidth in
2591 let tabw = 17.0*.ww in
2592 let itemcount = source#getitemcount in
2593 let minfo = source#getminfo in
2594 if conf.leftscroll
2595 then (
2596 GlMat.push ();
2597 GlMat.translate ~x:(float conf.scrollbw) ();
2599 let x0 = 0.0 and x1 = float (state.winw - conf.scrollbw - 1) in
2600 let rec loop row =
2601 if (row - m_first) > fstate.maxrows
2602 then ()
2603 else (
2604 if row >= 0 && row < itemcount
2605 then (
2606 let (s, level) = source#getitem row in
2607 let y = (row - m_first) * nfs in
2608 let x = 5.0 +. (float (level + m_pan)) *. ww in
2609 if helpmode
2610 then GlDraw.color
2611 (let c = if row land 1 = 0 then 1.0 else 0.92 in (c,c,c));
2613 if row = m_active
2614 then (
2615 Gl.disable `texture_2d;
2616 let alpha = if source#hasaction row then 0.9 else 0.3 in
2617 GlDraw.color (1., 1., 1.) ~alpha;
2618 linerect (x0 +. 1.) (float (y + 1)) (x1) (float (y + fs + 3));
2619 Gl.enable `texture_2d;
2621 let c =
2622 if zebra && row land 1 = 1
2623 then 0.8
2624 else 1.0
2626 GlDraw.color (c,c,c);
2627 let drawtabularstring s =
2628 let drawstr x s =
2629 let x' = truncate (x0 +. x) in
2630 let pos = nindex s '\000' in
2631 if pos = -1
2632 then drawstring1 fs x' (y+nfs) s
2633 else
2634 let s1 = String.sub s 0 pos
2635 and s2 = String.sub s (pos+1) (String.length s - pos - 1) in
2636 let rec e s =
2637 if emptystr s
2638 then s
2639 else
2640 let s' = withoutlastutf8 s in
2641 let s = s' ^ "@Uellipsis" in
2642 let w = measurestr fs s in
2643 if float x' +. w +. ww < float (hw + x')
2644 then s
2645 else e s'
2647 let s1 =
2648 if float x' +. ww +. measurestr fs s1 > float (hw + x')
2649 then e s1
2650 else s1
2652 ignore (drawstring1 fs x' (y+nfs) s1);
2653 drawstring1 fs (hw + x') (y+nfs) s2
2655 if trusted
2656 then
2657 let x = if helpmode && row > 0 then x +. ww else x in
2658 let tabpos = nindex s '\t' in
2659 if tabpos > 0
2660 then
2661 let len = String.length s - tabpos - 1 in
2662 let s1 = String.sub s 0 tabpos
2663 and s2 = String.sub s (tabpos + 1) len in
2664 let nx = drawstr x s1 in
2665 let sw = nx -. x in
2666 let x = x +. (max tabw sw) in
2667 drawstr x s2
2668 else
2669 let len = String.length s - 2 in
2670 if len > 0 && s.[0] = '\xc2' && s.[1] = '\xb7'
2671 then
2672 let s = String.sub s 2 len in
2673 let x = if not helpmode then x +. ww else x in
2674 GlDraw.color (1.2, 1.2, 1.2);
2675 let vinc = drawstring1 (fs+fs/4)
2676 (truncate (x -. ww)) (y+nfs) s in
2677 GlDraw.color (1., 1., 1.);
2678 vinc +. (float fs *. 0.8)
2679 else
2680 drawstr x s
2681 else
2682 drawstr x s
2684 ignore (drawtabularstring s);
2685 loop (row+1)
2689 loop m_first;
2690 GlDraw.color (1.0, 1.0, 1.0) ~alpha:0.5;
2691 let xadj = 5.0 in
2692 let rec loop row =
2693 if (row - m_first) > fstate.maxrows
2694 then ()
2695 else (
2696 if row >= 0 && row < itemcount
2697 then (
2698 let (s, level) = source#getitem row in
2699 let pos0 = nindex s '\000' in
2700 let y = (row - m_first) * nfs in
2701 let x = float (level + m_pan) *. ww in
2702 let (first, last) = minfo.(row) in
2703 let prefix =
2704 if pos0 > 0 && first > pos0
2705 then String.sub s (pos0+1) (first-pos0-1)
2706 else String.sub s 0 first
2708 let suffix = String.sub s first (last - first) in
2709 let w1 = measurestr fstate.fontsize prefix in
2710 let w2 = measurestr fstate.fontsize suffix in
2711 let x = x +. if conf.leftscroll then xadj else 5.0 in
2712 let x = if pos0 > 0 && first > pos0 then x +. float hw else x in
2713 let x0 = x +. w1
2714 and y0 = float (y+2) in
2715 let x1 = x0 +. w2
2716 and y1 = float (y+fs+3) in
2717 filledrect x0 y0 x1 y1;
2718 loop (row+1)
2722 Gl.disable `texture_2d;
2723 if Array.length minfo > 0 then loop m_first;
2724 Gl.disable `blend;
2725 if conf.leftscroll
2726 then GlMat.pop ();
2728 method updownlevel incr =
2729 let len = source#getitemcount in
2730 let curlevel =
2731 if m_active >= 0 && m_active < len
2732 then snd (source#getitem m_active)
2733 else -1
2735 let rec flow i =
2736 if i = len then i-1 else if i = -1 then 0 else
2737 let _, l = source#getitem i in
2738 if l != curlevel then i else flow (i+incr)
2740 let active = flow m_active in
2741 let first = calcfirst m_first active in
2742 G.postRedisplay "outline updownlevel";
2743 {< m_active = active; m_first = first >}
2745 method private key1 key mask =
2746 let set1 active first qsearch =
2747 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2749 let search active pattern incr =
2750 let active = if active = -1 then m_first else active in
2751 let dosearch re =
2752 let rec loop n =
2753 if n >= 0 && n < source#getitemcount
2754 then (
2755 let s, _ = source#getitem n in
2756 match Str.search_forward re s 0 with
2757 | (exception Not_found) -> loop (n + incr)
2758 | _ -> Some n
2760 else None
2762 loop active
2764 let qpat = Str.quote pattern in
2765 match Str.regexp_case_fold qpat with
2766 | s -> dosearch s
2767 | exception exn ->
2768 adderrfmt "listview key1" "regexp_case_fold for `%S' failed: %S\n"
2769 qpat @@ Printexc.to_string exn;
2770 None
2772 let itemcount = source#getitemcount in
2773 let find start incr =
2774 let rec find i =
2775 if i = -1 || i = itemcount
2776 then -1
2777 else (
2778 if source#hasaction i
2779 then i
2780 else find (i + incr)
2783 find start
2785 let set active first =
2786 let first = bound first 0 (itemcount - fstate.maxrows) in
2787 state.text <- E.s;
2788 coe {< m_active = active; m_first = first; m_qsearch = E.s >}
2790 let navigate incr =
2791 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2792 let active, first =
2793 let incr1 = if incr > 0 then 1 else -1 in
2794 if isvisible m_first m_active
2795 then
2796 let next =
2797 let next = m_active + incr in
2798 let next =
2799 if next < 0 || next >= itemcount
2800 then -1
2801 else find next incr1
2803 if abs (m_active - next) > fstate.maxrows
2804 then -1
2805 else next
2807 if next = -1
2808 then
2809 let first = m_first + incr in
2810 let first = bound first 0 (itemcount - fstate.maxrows) in
2811 let next =
2812 let next = m_active + incr in
2813 let next = bound next 0 (itemcount - 1) in
2814 find next ~-incr1
2816 let active =
2817 if next = -1
2818 then m_active
2819 else (
2820 if isvisible first next
2821 then next
2822 else m_active
2825 active, first
2826 else
2827 let first = min next m_first in
2828 let first =
2829 if abs (next - first) > fstate.maxrows
2830 then first + incr
2831 else first
2833 next, first
2834 else
2835 let first = m_first + incr in
2836 let first = bound first 0 (itemcount - 1) in
2837 let active =
2838 let next = m_active + incr in
2839 let next = bound next 0 (itemcount - 1) in
2840 let next = find next incr1 in
2841 let active =
2842 if next = -1 || abs (m_active - first) > fstate.maxrows
2843 then (
2844 let active = if m_active = -1 then next else m_active in
2845 active
2847 else next
2849 if isvisible first active
2850 then active
2851 else -1
2853 active, first
2855 G.postRedisplay "listview navigate";
2856 set active first;
2858 match key with
2859 | (@r|@s) when Wsi.withctrl mask ->
2860 let incr = if key = @r then -1 else 1 in
2861 let active, first =
2862 match search (m_active + incr) m_qsearch incr with
2863 | None ->
2864 state.text <- m_qsearch ^ " [not found]";
2865 m_active, m_first
2866 | Some active ->
2867 state.text <- m_qsearch;
2868 active, firstof m_first active
2870 G.postRedisplay "listview ctrl-r/s";
2871 set1 active first m_qsearch;
2873 | @insert when Wsi.withctrl mask ->
2874 if m_active >= 0 && m_active < source#getitemcount
2875 then (
2876 let s, _ = source#getitem m_active in
2877 selstring s;
2879 coe self
2881 | @backspace ->
2882 if emptystr m_qsearch
2883 then coe self
2884 else (
2885 let qsearch = withoutlastutf8 m_qsearch in
2886 if emptystr qsearch
2887 then (
2888 state.text <- E.s;
2889 G.postRedisplay "listview empty qsearch";
2890 set1 m_active m_first E.s;
2892 else
2893 let active, first =
2894 match search m_active qsearch ~-1 with
2895 | None ->
2896 state.text <- qsearch ^ " [not found]";
2897 m_active, m_first
2898 | Some active ->
2899 state.text <- qsearch;
2900 active, firstof m_first active
2902 G.postRedisplay "listview backspace qsearch";
2903 set1 active first qsearch
2906 | key when (key != 0 && not (Wsi.isspecialkey key)) ->
2907 let pattern = m_qsearch ^ toutf8 key in
2908 let active, first =
2909 match search m_active pattern 1 with
2910 | None ->
2911 state.text <- pattern ^ " [not found]";
2912 m_active, m_first
2913 | Some active ->
2914 state.text <- pattern;
2915 active, firstof m_first active
2917 G.postRedisplay "listview qsearch add";
2918 set1 active first pattern;
2920 | @escape ->
2921 state.text <- E.s;
2922 if emptystr m_qsearch
2923 then (
2924 G.postRedisplay "list view escape";
2925 let mx, my = state.mpos in
2926 updateunder mx my;
2927 begin
2928 match
2929 source#exit ~uioh:(coe self)
2930 ~cancel:true ~active:m_active ~first:m_first ~pan:m_pan
2931 with
2932 | None -> m_prev_uioh
2933 | Some uioh -> uioh
2936 else (
2937 G.postRedisplay "list view kill qsearch";
2938 coe {< m_qsearch = E.s >}
2941 | @enter | @kpenter ->
2942 state.text <- E.s;
2943 let self = {< m_qsearch = E.s >} in
2944 let opt =
2945 G.postRedisplay "listview enter";
2946 if m_active >= 0 && m_active < source#getitemcount
2947 then (
2948 source#exit ~uioh:(coe self) ~cancel:false
2949 ~active:m_active ~first:m_first ~pan:m_pan;
2951 else (
2952 source#exit ~uioh:(coe self) ~cancel:true
2953 ~active:m_active ~first:m_first ~pan:m_pan;
2956 begin match opt with
2957 | None -> m_prev_uioh
2958 | Some uioh -> uioh
2961 | @delete | @kpdelete ->
2962 coe self
2964 | @up | @kpup -> navigate ~-1
2965 | @down | @kpdown -> navigate 1
2966 | @prior | @kpprior -> navigate ~-(fstate.maxrows)
2967 | @next | @kpnext -> navigate fstate.maxrows
2969 | @right | @kpright ->
2970 state.text <- E.s;
2971 G.postRedisplay "listview right";
2972 coe {< m_pan = m_pan - 1 >}
2974 | @left | @kpleft ->
2975 state.text <- E.s;
2976 G.postRedisplay "listview left";
2977 coe {< m_pan = m_pan + 1 >}
2979 | @home | @kphome ->
2980 let active = find 0 1 in
2981 G.postRedisplay "listview home";
2982 set active 0;
2984 | @jend | @kpend ->
2985 let first = max 0 (itemcount - fstate.maxrows) in
2986 let active = find (itemcount - 1) ~-1 in
2987 G.postRedisplay "listview end";
2988 set active first;
2990 | key when (key = 0 || Wsi.isspecialkey key) ->
2991 coe self
2993 | _ ->
2994 dolog "listview unknown key %#x" key; coe self
2996 method key key mask =
2997 match state.mode with
2998 | Textentry te -> textentrykeyboard key mask te; coe self
2999 | Birdseye _
3000 | View
3001 | LinkNav _ -> self#key1 key mask
3003 method button button down x y _ =
3004 let opt =
3005 match button with
3006 | 1 when vscrollhit x ->
3007 G.postRedisplay "listview scroll";
3008 if down
3009 then
3010 let _, position, sh = self#scrollph in
3011 if y > truncate position && y < truncate (position +. sh)
3012 then (
3013 state.mstate <- Mscrolly;
3014 Some (coe self)
3016 else
3017 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3018 let first = truncate (s *. float source#getitemcount) in
3019 let first = min source#getitemcount first in
3020 Some (coe {< m_first = first; m_active = first >})
3021 else (
3022 state.mstate <- Mnone;
3023 Some (coe self);
3025 | 1 when down ->
3026 begin match self#elemunder y with
3027 | Some n ->
3028 G.postRedisplay "listview click";
3029 source#exit ~uioh:(coe {< m_active = n >})
3030 ~cancel:false ~active:n ~first:m_first ~pan:m_pan
3031 | _ ->
3032 Some (coe self)
3034 | n when (n == 4 || n == 5) && not down ->
3035 let len = source#getitemcount in
3036 let first =
3037 if n = 5 && m_first + fstate.maxrows >= len
3038 then
3039 m_first
3040 else
3041 let first = m_first + (if n == 4 then -1 else 1) in
3042 bound first 0 (len - 1)
3044 G.postRedisplay "listview wheel";
3045 Some (coe {< m_first = first >})
3046 | n when (n = 6 || n = 7) && not down ->
3047 let inc = if n = 7 then -1 else 1 in
3048 G.postRedisplay "listview hwheel";
3049 Some (coe {< m_pan = m_pan + inc >})
3050 | _ ->
3051 Some (coe self)
3053 match opt with
3054 | None -> m_prev_uioh
3055 | Some uioh -> uioh
3057 method multiclick _ x y = self#button 1 true x y
3059 method motion _ y =
3060 match state.mstate with
3061 | Mscrolly ->
3062 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3063 let first = truncate (s *. float source#getitemcount) in
3064 let first = min source#getitemcount first in
3065 G.postRedisplay "listview motion";
3066 coe {< m_first = first; m_active = first >}
3067 | Msel _
3068 | Mpan _
3069 | Mscrollx
3070 | Mzoom _
3071 | Mzoomrect _
3072 | Mnone -> coe self
3074 method pmotion x y =
3075 if x < state.winw - conf.scrollbw
3076 then
3077 let n =
3078 match self#elemunder y with
3079 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3080 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3082 let o =
3083 if n != m_active
3084 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3085 else self
3087 coe o
3088 else (
3089 Wsi.setcursor Wsi.CURSOR_INHERIT;
3090 coe self
3093 method infochanged _ = ()
3095 method scrollpw = (0, 0.0, 0.0)
3096 method scrollph =
3097 let nfs = fstate.fontsize + 1 in
3098 let y = m_first * nfs in
3099 let itemcount = source#getitemcount in
3100 let maxi = max 0 (itemcount - fstate.maxrows) in
3101 let maxy = maxi * nfs in
3102 let p, h = scrollph y maxy in
3103 conf.scrollbw, p, h
3105 method modehash = modehash
3106 method eformsgs = false
3107 method alwaysscrolly = true
3108 end;;
3110 class outlinelistview ~zebra ~source =
3111 let settext autonarrow s =
3112 if autonarrow
3113 then
3114 let ss = source#statestr in
3115 state.text <-
3116 if emptystr ss
3117 then "[" ^ s ^ "]"
3118 else "{" ^ ss ^ "} [" ^ s ^ "]"
3119 else state.text <- s
3121 object (self)
3122 inherit listview
3123 ~zebra
3124 ~helpmode:false
3125 ~source:(source :> lvsource)
3126 ~trusted:false
3127 ~modehash:(findkeyhash conf "outline")
3128 as super
3130 val m_autonarrow = false
3132 method! key key mask =
3133 let maxrows =
3134 if emptystr state.text
3135 then fstate.maxrows
3136 else fstate.maxrows - 2
3138 let calcfirst first active =
3139 if active > first
3140 then
3141 let rows = active - first in
3142 if rows > maxrows then active - maxrows else first
3143 else active
3145 let navigate incr =
3146 let active = m_active + incr in
3147 let active = bound active 0 (source#getitemcount - 1) in
3148 let first = calcfirst m_first active in
3149 G.postRedisplay "outline navigate";
3150 coe {< m_active = active; m_first = first >}
3152 let navscroll first =
3153 let active =
3154 let dist = m_active - first in
3155 if dist < 0
3156 then first
3157 else (
3158 if dist < maxrows
3159 then m_active
3160 else first + maxrows
3163 G.postRedisplay "outline navscroll";
3164 coe {< m_first = first; m_active = active >}
3166 let ctrl = Wsi.withctrl mask in
3167 match key with
3168 | @a when ctrl ->
3169 let text =
3170 if m_autonarrow
3171 then (source#denarrow; E.s)
3172 else (
3173 let pattern = source#renarrow in
3174 if nonemptystr m_qsearch
3175 then (source#narrow m_qsearch; m_qsearch)
3176 else pattern
3179 settext (not m_autonarrow) text;
3180 G.postRedisplay "toggle auto narrowing";
3181 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
3183 | @slash when emptystr m_qsearch && not m_autonarrow ->
3184 settext true E.s;
3185 G.postRedisplay "toggle auto narrowing";
3186 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
3188 | @n when ctrl ->
3189 source#narrow m_qsearch;
3190 if not m_autonarrow
3191 then source#add_narrow_pattern m_qsearch;
3192 G.postRedisplay "outline ctrl-n";
3193 coe {< m_first = 0; m_active = 0 >}
3195 | @S when ctrl ->
3196 let active = source#calcactive (getanchor ()) in
3197 let first = firstof m_first active in
3198 G.postRedisplay "outline ctrl-s";
3199 coe {< m_first = first; m_active = active >}
3201 | @u when ctrl ->
3202 G.postRedisplay "outline ctrl-u";
3203 if m_autonarrow && nonemptystr m_qsearch
3204 then (
3205 ignore (source#renarrow);
3206 settext m_autonarrow E.s;
3207 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3209 else (
3210 source#del_narrow_pattern;
3211 let pattern = source#renarrow in
3212 let text =
3213 if emptystr pattern then E.s else "Narrowed to " ^ pattern
3215 settext m_autonarrow text;
3216 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3219 | @l when ctrl ->
3220 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3221 G.postRedisplay "outline ctrl-l";
3222 coe {< m_first = first >}
3224 | @tab when m_autonarrow ->
3225 if nonemptystr m_qsearch
3226 then (
3227 G.postRedisplay "outline list view tab";
3228 source#add_narrow_pattern m_qsearch;
3229 settext true E.s;
3230 coe {< m_qsearch = E.s >}
3232 else coe self
3234 | @escape when m_autonarrow ->
3235 if nonemptystr m_qsearch
3236 then source#add_narrow_pattern m_qsearch;
3237 super#key key mask
3239 | @enter | @kpenter when m_autonarrow ->
3240 if nonemptystr m_qsearch
3241 then source#add_narrow_pattern m_qsearch;
3242 super#key key mask
3244 | key when m_autonarrow && (not (Wsi.isspecialkey key)) ->
3245 let pattern = m_qsearch ^ toutf8 key in
3246 G.postRedisplay "outlinelistview autonarrow add";
3247 source#narrow pattern;
3248 settext true pattern;
3249 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3251 | key when m_autonarrow && key = @backspace ->
3252 if emptystr m_qsearch
3253 then coe self
3254 else
3255 let pattern = withoutlastutf8 m_qsearch in
3256 G.postRedisplay "outlinelistview autonarrow backspace";
3257 ignore (source#renarrow);
3258 source#narrow pattern;
3259 settext true pattern;
3260 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3262 | @up | @kpup when ctrl ->
3263 navscroll (max 0 (m_first - 1))
3265 | @down | @kpdown when ctrl ->
3266 navscroll (min (source#getitemcount - 1) (m_first + 1))
3268 | @up | @kpup -> navigate ~-1
3269 | @down | @kpdown -> navigate 1
3270 | @prior | @kpprior -> navigate ~-(fstate.maxrows)
3271 | @next | @kpnext -> navigate fstate.maxrows
3273 | @right | @kpright ->
3274 let o =
3275 if ctrl
3276 then (
3277 G.postRedisplay "outline ctrl right";
3278 {< m_pan = m_pan + 1 >}
3280 else self#updownlevel 1
3282 coe o
3284 | @left | @kpleft ->
3285 let o =
3286 if ctrl
3287 then (
3288 G.postRedisplay "outline ctrl left";
3289 {< m_pan = m_pan - 1 >}
3291 else self#updownlevel ~-1
3293 coe o
3295 | @home | @kphome ->
3296 G.postRedisplay "outline home";
3297 coe {< m_first = 0; m_active = 0 >}
3299 | @jend | @kpend ->
3300 let active = source#getitemcount - 1 in
3301 let first = max 0 (active - fstate.maxrows) in
3302 G.postRedisplay "outline end";
3303 coe {< m_active = active; m_first = first >}
3305 | _ -> super#key key mask
3306 end;;
3308 let genhistoutlines () =
3309 Config.gethist ()
3310 |> List.sort (fun (_, c1, _, _, _, _) (_, c2, _, _, _, _) ->
3311 compare c2.lastvisit c1.lastvisit)
3312 |> List.map
3313 (fun ((path, c, _, _, _, origin) as hist) ->
3314 let path = if nonemptystr origin then origin else path in
3315 let base = mbtoutf8 @@ Filename.basename path in
3316 (base ^ "\000" ^ c.title, 1, Ohistory hist)
3318 |> Array.of_list
3321 let gotohist (path, c, bookmarks, x, anchor, origin) =
3322 Config.save leavebirdseye;
3323 state.anchor <- anchor;
3324 state.bookmarks <- bookmarks;
3325 state.origin <- origin;
3326 state.x <- x;
3327 setconf conf c;
3328 let x0, y0, x1, y1 = conf.trimfuzz in
3329 wcmd "trimset %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1;
3330 reshape ~firsttime:true state.winw state.winh;
3331 opendoc path origin;
3332 setzoom c.zoom;
3335 let makecheckers () =
3336 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3337 following to say:
3338 converted by Issac Trotts. July 25, 2002 *)
3339 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3340 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3341 let id = GlTex.gen_texture () in
3342 GlTex.bind_texture ~target:`texture_2d id;
3343 GlPix.store (`unpack_alignment 1);
3344 GlTex.image2d image;
3345 List.iter (GlTex.parameter ~target:`texture_2d)
3346 [ `mag_filter `nearest; `min_filter `nearest ];
3350 let setcheckers enabled =
3351 match state.checkerstexid with
3352 | None ->
3353 if enabled then state.checkerstexid <- Some (makecheckers ())
3355 | Some checkerstexid ->
3356 if not enabled
3357 then (
3358 GlTex.delete_texture checkerstexid;
3359 state.checkerstexid <- None;
3363 let describe_location () =
3364 let fn = page_of_y state.y in
3365 let ln = page_of_y (state.y + state.winh - 1) in
3366 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
3367 let percent =
3368 if maxy <= 0
3369 then 100.
3370 else (100. *. (float state.y /. float maxy))
3372 if fn = ln
3373 then
3374 Printf.sprintf "page %d of %d [%.2f%%]"
3375 (fn+1) state.pagecount percent
3376 else
3377 Printf.sprintf
3378 "pages %d-%d of %d [%.2f%%]"
3379 (fn+1) (ln+1) state.pagecount percent
3382 let setpresentationmode v =
3383 let n = page_of_y state.y in
3384 state.anchor <- (n, 0.0, 1.0);
3385 conf.presentation <- v;
3386 if conf.fitmodel = FitPage
3387 then reqlayout conf.angle conf.fitmodel;
3388 represent ();
3391 let setbgcol (r, g, b) =
3392 let col =
3393 let r = r *. 255.0 |> truncate
3394 and g = g *. 255.0 |> truncate
3395 and b = b *. 255.0 |> truncate in
3396 r lsl 16 |> (lor) (g lsl 8) |> (lor) b
3398 Wsi.setwinbgcol col;
3401 let enterinfomode =
3402 let btos b = if b then "@Uradical" else E.s in
3403 let showextended = ref false in
3404 let leave mode _ = state.mode <- mode in
3405 let src =
3406 (object
3407 val mutable m_l = []
3408 val mutable m_a = E.a
3409 val mutable m_prev_uioh = nouioh
3410 val mutable m_prev_mode = View
3412 inherit lvsourcebase
3414 method reset prev_mode prev_uioh =
3415 m_a <- Array.of_list (List.rev m_l);
3416 m_l <- [];
3417 m_prev_mode <- prev_mode;
3418 m_prev_uioh <- prev_uioh;
3420 method int name get set =
3421 m_l <-
3422 (name, `int get, 1,
3423 Action (
3424 fun u ->
3425 let ondone s =
3426 try set (int_of_string s)
3427 with exn ->
3428 state.text <- Printf.sprintf "bad integer `%s': %s"
3429 s @@ exntos exn
3431 state.text <- E.s;
3432 let te = name ^ ": ", E.s, None, intentry, ondone, true in
3433 state.mode <- Textentry (te, leave m_prev_mode);
3435 )) :: m_l
3437 method int_with_suffix name get set =
3438 m_l <-
3439 (name, `intws get, 1,
3440 Action (
3441 fun u ->
3442 let ondone s =
3443 try set (int_of_string_with_suffix s)
3444 with exn ->
3445 state.text <- Printf.sprintf "bad integer `%s': %s"
3446 s @@ exntos exn
3448 state.text <- E.s;
3449 let te =
3450 name ^ ": ", E.s, None, intentry_with_suffix, ondone, true
3452 state.mode <- Textentry (te, leave m_prev_mode);
3454 )) :: m_l
3456 method bool ?(offset=1) ?(btos=btos) name get set =
3457 m_l <-
3458 (name, `bool (btos, get), offset, Action (
3459 fun u ->
3460 let v = get () in
3461 set (not v);
3463 )) :: m_l
3465 method color name get set =
3466 m_l <-
3467 (name, `color get, 1,
3468 Action (
3469 fun u ->
3470 let invalid = (nan, nan, nan) in
3471 let ondone s =
3472 let c =
3473 try color_of_string s
3474 with exn ->
3475 state.text <- Printf.sprintf "bad color `%s': %s"
3476 s @@ exntos exn;
3477 invalid
3479 if c <> invalid
3480 then set c;
3482 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3483 state.text <- color_to_string (get ());
3484 state.mode <- Textentry (te, leave m_prev_mode);
3486 )) :: m_l
3488 method string name get set =
3489 m_l <-
3490 (name, `string get, 1,
3491 Action (
3492 fun u ->
3493 let ondone s = set s in
3494 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3495 state.mode <- Textentry (te, leave m_prev_mode);
3497 )) :: m_l
3499 method colorspace name get set =
3500 m_l <-
3501 (name, `string get, 1,
3502 Action (
3503 fun _ ->
3504 let source =
3505 (object
3506 inherit lvsourcebase
3508 initializer
3509 m_active <- CSTE.to_int conf.colorspace;
3510 m_first <- 0;
3512 method getitemcount =
3513 Array.length CSTE.names
3514 method getitem n =
3515 (CSTE.names.(n), 0)
3516 method exit ~uioh ~cancel ~active ~first ~pan =
3517 ignore (uioh, first, pan);
3518 if not cancel then set active;
3519 None
3520 method hasaction _ = true
3521 end)
3523 state.text <- E.s;
3524 let modehash = findkeyhash conf "info" in
3525 coe (new listview ~zebra:false ~helpmode:false
3526 ~source ~trusted:true ~modehash)
3527 )) :: m_l
3529 method paxmark name get set =
3530 m_l <-
3531 (name, `string get, 1,
3532 Action (
3533 fun _ ->
3534 let source =
3535 (object
3536 inherit lvsourcebase
3538 initializer
3539 m_active <- MTE.to_int conf.paxmark;
3540 m_first <- 0;
3542 method getitemcount = Array.length MTE.names
3543 method getitem n = (MTE.names.(n), 0)
3544 method exit ~uioh ~cancel ~active ~first ~pan =
3545 ignore (uioh, first, pan);
3546 if not cancel then set active;
3547 None
3548 method hasaction _ = true
3549 end)
3551 state.text <- E.s;
3552 let modehash = findkeyhash conf "info" in
3553 coe (new listview ~zebra:false ~helpmode:false
3554 ~source ~trusted:true ~modehash)
3555 )) :: m_l
3557 method fitmodel name get set =
3558 m_l <-
3559 (name, `string get, 1,
3560 Action (
3561 fun _ ->
3562 let source =
3563 (object
3564 inherit lvsourcebase
3566 initializer
3567 m_active <- FMTE.to_int conf.fitmodel;
3568 m_first <- 0;
3570 method getitemcount = Array.length FMTE.names
3571 method getitem n = (FMTE.names.(n), 0)
3572 method exit ~uioh ~cancel ~active ~first ~pan =
3573 ignore (uioh, first, pan);
3574 if not cancel then set active;
3575 None
3576 method hasaction _ = true
3577 end)
3579 state.text <- E.s;
3580 let modehash = findkeyhash conf "info" in
3581 coe (new listview ~zebra:false ~helpmode:false
3582 ~source ~trusted:true ~modehash)
3583 )) :: m_l
3585 method caption s offset =
3586 m_l <- (s, `empty, offset, Noaction) :: m_l
3588 method caption2 s f offset =
3589 m_l <- (s, `string f, offset, Noaction) :: m_l
3591 method getitemcount = Array.length m_a
3593 method getitem n =
3594 let tostr = function
3595 | `int f -> string_of_int (f ())
3596 | `intws f -> string_with_suffix_of_int (f ())
3597 | `string f -> f ()
3598 | `color f -> color_to_string (f ())
3599 | `bool (btos, f) -> btos (f ())
3600 | `empty -> E.s
3602 let name, t, offset, _ = m_a.(n) in
3603 ((let s = tostr t in
3604 if nonemptystr s
3605 then Printf.sprintf "%s\t%s" name s
3606 else name),
3607 offset)
3609 method exit ~uioh ~cancel ~active ~first ~pan =
3610 let uiohopt =
3611 if not cancel
3612 then (
3613 let uioh =
3614 match m_a.(active) with
3615 | _, _, _, Action f -> f uioh
3616 | _, _, _, Noaction -> uioh
3618 Some uioh
3620 else None
3622 m_active <- active;
3623 m_first <- first;
3624 m_pan <- pan;
3625 uiohopt
3627 method hasaction n =
3628 match m_a.(n) with
3629 | _, _, _, Action _ -> true
3630 | _, _, _, Noaction -> false
3632 initializer m_active <- 1
3633 end)
3635 let rec fillsrc prevmode prevuioh =
3636 let sep () = src#caption E.s 0 in
3637 let colorp name get set =
3638 src#string name
3639 (fun () -> color_to_string (get ()))
3640 (fun v ->
3642 let c = color_of_string v in
3643 set c
3644 with exn ->
3645 state.text <-
3646 Printf.sprintf "bad color `%s': %s" v @@ exntos exn
3649 let oldmode = state.mode in
3650 let birdseye = isbirdseye state.mode in
3652 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3654 src#bool "presentation mode"
3655 (fun () -> conf.presentation)
3656 (fun v -> setpresentationmode v);
3658 src#bool "ignore case in searches"
3659 (fun () -> conf.icase)
3660 (fun v -> conf.icase <- v);
3662 src#bool "preload"
3663 (fun () -> conf.preload)
3664 (fun v -> conf.preload <- v);
3666 src#bool "highlight links"
3667 (fun () -> conf.hlinks)
3668 (fun v -> conf.hlinks <- v);
3670 src#bool "under info"
3671 (fun () -> conf.underinfo)
3672 (fun v -> conf.underinfo <- v);
3674 src#bool "persistent bookmarks"
3675 (fun () -> conf.savebmarks)
3676 (fun v -> conf.savebmarks <- v);
3678 src#fitmodel "fit model"
3679 (fun () -> FMTE.to_string conf.fitmodel)
3680 (fun v -> reqlayout conf.angle (FMTE.of_int v));
3682 src#bool "trim margins"
3683 (fun () -> conf.trimmargins)
3684 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3686 src#bool "persistent location"
3687 (fun () -> conf.jumpback)
3688 (fun v -> conf.jumpback <- v);
3690 sep ();
3691 src#int "inter-page space"
3692 (fun () -> conf.interpagespace)
3693 (fun n ->
3694 conf.interpagespace <- n;
3695 docolumns conf.columns;
3696 let pageno, py =
3697 match state.layout with
3698 | [] -> 0, 0
3699 | l :: _ ->
3700 l.pageno, l.pagey
3702 state.maxy <- calcheight ();
3703 let y = getpagey pageno in
3704 gotoxy state.x (y + py)
3707 src#int "page bias"
3708 (fun () -> conf.pagebias)
3709 (fun v -> conf.pagebias <- v);
3711 src#int "scroll step"
3712 (fun () -> conf.scrollstep)
3713 (fun n -> conf.scrollstep <- n);
3715 src#int "horizontal scroll step"
3716 (fun () -> conf.hscrollstep)
3717 (fun v -> conf.hscrollstep <- v);
3719 src#int "auto scroll step"
3720 (fun () ->
3721 match state.autoscroll with
3722 | Some step -> step
3723 | _ -> conf.autoscrollstep)
3724 (fun n ->
3725 let n = boundastep state.winh n in
3726 if state.autoscroll <> None
3727 then state.autoscroll <- Some n;
3728 conf.autoscrollstep <- n);
3730 src#int "zoom"
3731 (fun () -> truncate (conf.zoom *. 100.))
3732 (fun v -> pivotzoom ((float v) /. 100.));
3734 src#int "rotation"
3735 (fun () -> conf.angle)
3736 (fun v -> reqlayout v conf.fitmodel);
3738 src#int "scroll bar width"
3739 (fun () -> conf.scrollbw)
3740 (fun v ->
3741 conf.scrollbw <- v;
3742 reshape state.winw state.winh;
3745 src#int "scroll handle height"
3746 (fun () -> conf.scrollh)
3747 (fun v -> conf.scrollh <- v;);
3749 src#int "thumbnail width"
3750 (fun () -> conf.thumbw)
3751 (fun v ->
3752 conf.thumbw <- min 4096 v;
3753 match oldmode with
3754 | Birdseye beye ->
3755 leavebirdseye beye false;
3756 enterbirdseye ()
3757 | Textentry _
3758 | View
3759 | LinkNav _ -> ()
3762 let mode = state.mode in
3763 src#string "columns"
3764 (fun () ->
3765 match conf.columns with
3766 | Csingle _ -> "1"
3767 | Cmulti (multi, _) -> multicolumns_to_string multi
3768 | Csplit (count, _) -> "-" ^ string_of_int count
3770 (fun v ->
3771 let n, a, b = multicolumns_of_string v in
3772 setcolumns mode n a b);
3774 sep ();
3775 src#caption "Pixmap cache" 0;
3776 src#int_with_suffix "size (advisory)"
3777 (fun () -> conf.memlimit)
3778 (fun v -> conf.memlimit <- v);
3780 src#caption2 "used"
3781 (fun () ->
3782 Printf.sprintf "%s bytes, %d tiles"
3783 (string_with_suffix_of_int state.memused)
3784 (Hashtbl.length state.tilemap)) 1;
3786 sep ();
3787 src#caption "Layout" 0;
3788 src#caption2 "Dimension"
3789 (fun () ->
3790 Printf.sprintf "%dx%d (virtual %dx%d)"
3791 state.winw state.winh
3792 state.w state.maxy)
3794 if conf.debug
3795 then
3796 src#caption2 "Position" (fun () ->
3797 Printf.sprintf "%dx%d" state.x state.y
3799 else
3800 src#caption2 "Position" (fun () -> describe_location ()) 1
3803 sep ();
3804 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3805 "Save these parameters as global defaults at exit"
3806 (fun () -> conf.bedefault)
3807 (fun v -> conf.bedefault <- v)
3810 sep ();
3811 let btos b = if b then "@Ulguillemet" else "@Urguillemet" in
3812 src#bool ~offset:0 ~btos "Extended parameters"
3813 (fun () -> !showextended)
3814 (fun v -> showextended := v; fillsrc prevmode prevuioh);
3815 if !showextended
3816 then (
3817 src#bool "checkers"
3818 (fun () -> conf.checkers)
3819 (fun v -> conf.checkers <- v; setcheckers v);
3820 src#bool "update cursor"
3821 (fun () -> conf.updatecurs)
3822 (fun v -> conf.updatecurs <- v);
3823 src#bool "scroll-bar on the left"
3824 (fun () -> conf.leftscroll)
3825 (fun v -> conf.leftscroll <- v);
3826 src#bool "verbose"
3827 (fun () -> conf.verbose)
3828 (fun v -> conf.verbose <- v);
3829 src#bool "invert colors"
3830 (fun () -> conf.invert)
3831 (fun v -> conf.invert <- v);
3832 src#bool "max fit"
3833 (fun () -> conf.maxhfit)
3834 (fun v -> conf.maxhfit <- v);
3835 src#bool "pax mode"
3836 (fun () -> conf.pax != None)
3837 (fun v ->
3838 if v
3839 then conf.pax <- Some (ref (now (), 0, 0))
3840 else conf.pax <- None);
3841 src#string "uri launcher"
3842 (fun () -> conf.urilauncher)
3843 (fun v -> conf.urilauncher <- v);
3844 src#string "path launcher"
3845 (fun () -> conf.pathlauncher)
3846 (fun v -> conf.pathlauncher <- v);
3847 src#string "tile size"
3848 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3849 (fun v ->
3851 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
3852 conf.tilew <- max 64 w;
3853 conf.tileh <- max 64 h;
3854 flushtiles ();
3855 with exn ->
3856 state.text <- Printf.sprintf "bad tile size `%s': %s"
3857 v @@ exntos exn
3859 src#int "texture count"
3860 (fun () -> conf.texcount)
3861 (fun v ->
3862 if realloctexts v
3863 then conf.texcount <- v
3864 else impmsg "failed to set texture count please retry later"
3866 src#int "slice height"
3867 (fun () -> conf.sliceheight)
3868 (fun v ->
3869 conf.sliceheight <- v;
3870 wcmd "sliceh %d" conf.sliceheight;
3872 src#int "anti-aliasing level"
3873 (fun () -> conf.aalevel)
3874 (fun v ->
3875 conf.aalevel <- bound v 0 8;
3876 state.anchor <- getanchor ();
3877 opendoc state.path state.password;
3879 src#string "page scroll scaling factor"
3880 (fun () -> string_of_float conf.pgscale)
3881 (fun v ->
3883 let s = float_of_string v in
3884 conf.pgscale <- s
3885 with exn ->
3886 state.text <- Printf.sprintf
3887 "bad page scroll scaling factor `%s': %s" v
3888 @@ exntos exn
3891 src#int "ui font size"
3892 (fun () -> fstate.fontsize)
3893 (fun v -> setfontsize (bound v 5 100));
3894 src#int "hint font size"
3895 (fun () -> conf.hfsize)
3896 (fun v -> conf.hfsize <- bound v 5 100);
3897 colorp "background color"
3898 (fun () -> conf.bgcolor)
3899 (fun v -> conf.bgcolor <- v; setbgcol v);
3900 src#bool "crop hack"
3901 (fun () -> conf.crophack)
3902 (fun v -> conf.crophack <- v);
3903 src#string "trim fuzz"
3904 (fun () -> irect_to_string conf.trimfuzz)
3905 (fun v ->
3907 conf.trimfuzz <- irect_of_string v;
3908 if conf.trimmargins
3909 then settrim true conf.trimfuzz;
3910 with exn ->
3911 state.text <- Printf.sprintf "bad irect `%s': %s" v
3912 @@ exntos exn
3914 src#string "throttle"
3915 (fun () ->
3916 match conf.maxwait with
3917 | None -> "show place holder if page is not ready"
3918 | Some time ->
3919 if time = infinity
3920 then "wait for page to fully render"
3921 else
3922 "wait " ^ string_of_float time
3923 ^ " seconds before showing placeholder"
3925 (fun v ->
3927 let f = float_of_string v in
3928 if f <= 0.0
3929 then conf.maxwait <- None
3930 else conf.maxwait <- Some f
3931 with exn ->
3932 state.text <- Printf.sprintf "bad time `%s': %s" v
3933 @@ exntos exn
3935 src#string "ghyll scroll"
3936 (fun () ->
3937 match conf.ghyllscroll with
3938 | None -> E.s
3939 | Some nab -> ghyllscroll_to_string nab
3941 (fun v ->
3942 try conf.ghyllscroll <- ghyllscroll_of_string v
3943 with
3944 | Failure msg ->
3945 state.text <- Printf.sprintf "bad ghyll `%s': %s" v msg
3946 | exn ->
3947 state.text <- Printf.sprintf "bad ghyll `%s': %s" v
3948 @@ exntos exn
3950 src#string "selection command"
3951 (fun () -> conf.selcmd)
3952 (fun v -> conf.selcmd <- v);
3953 src#string "synctex command"
3954 (fun () -> conf.stcmd)
3955 (fun v -> conf.stcmd <- v);
3956 src#string "pax command"
3957 (fun () -> conf.paxcmd)
3958 (fun v -> conf.paxcmd <- v);
3959 src#string "ask password command"
3960 (fun () -> conf.passcmd)
3961 (fun v -> conf.passcmd <- v);
3962 src#string "save path command"
3963 (fun () -> conf.savecmd)
3964 (fun v -> conf.savecmd <- v);
3965 src#colorspace "color space"
3966 (fun () -> CSTE.to_string conf.colorspace)
3967 (fun v ->
3968 conf.colorspace <- CSTE.of_int v;
3969 wcmd "cs %d" v;
3970 load state.layout;
3972 src#paxmark "pax mark method"
3973 (fun () -> MTE.to_string conf.paxmark)
3974 (fun v -> conf.paxmark <- MTE.of_int v);
3975 if bousable () && !opengl_has_pbo
3976 then
3977 src#bool "use PBO"
3978 (fun () -> conf.usepbo)
3979 (fun v -> conf.usepbo <- v);
3980 src#bool "mouse wheel scrolls pages"
3981 (fun () -> conf.wheelbypage)
3982 (fun v -> conf.wheelbypage <- v);
3983 src#bool "open remote links in a new instance"
3984 (fun () -> conf.riani)
3985 (fun v -> conf.riani <- v);
3986 src#bool "edit annotations inline"
3987 (fun () -> conf.annotinline)
3988 (fun v -> conf.annotinline <- v);
3989 src#bool "coarse positioning in presentation mode"
3990 (fun () -> conf.coarseprespos)
3991 (fun v -> conf.coarseprespos <- v);
3992 src#bool "use document css"
3993 (fun () -> conf.usedoccss)
3994 (fun v ->
3995 conf.usedoccss <- v;
3996 state.anchor <- getanchor ();
3997 opendoc state.path state.password;
4001 sep ();
4002 src#caption "Document" 0;
4003 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4004 src#caption2 "Pages"
4005 (fun () -> string_of_int state.pagecount) 1;
4006 src#caption2 "Dimensions"
4007 (fun () -> string_of_int (List.length state.pdims)) 1;
4008 if nonemptystr conf.css
4009 then src#caption2 "CSS" (fun () -> conf.css) 1;
4010 if conf.trimmargins
4011 then (
4012 sep ();
4013 src#caption "Trimmed margins" 0;
4014 src#caption2 "Dimensions"
4015 (fun () -> string_of_int (List.length state.pdims)) 1;
4018 sep ();
4019 src#caption "OpenGL" 0;
4020 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4021 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4023 sep ();
4024 src#caption "Location" 0;
4025 if nonemptystr state.origin
4026 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
4027 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
4029 src#reset prevmode prevuioh;
4031 fun () ->
4032 state.text <- E.s;
4033 resetmstate ();
4034 let prevmode = state.mode
4035 and prevuioh = state.uioh in
4036 fillsrc prevmode prevuioh;
4037 let source = (src :> lvsource) in
4038 let modehash = findkeyhash conf "info" in
4039 state.uioh <-
4040 coe (object (self)
4041 inherit listview ~zebra:false ~helpmode:false
4042 ~source ~trusted:true ~modehash as super
4043 val mutable m_prevmemused = 0
4044 method! infochanged = function
4045 | Memused ->
4046 if m_prevmemused != state.memused
4047 then (
4048 m_prevmemused <- state.memused;
4049 G.postRedisplay "memusedchanged";
4051 | Pdim -> G.postRedisplay "pdimchanged"
4052 | Docinfo -> fillsrc prevmode prevuioh
4054 method! key key mask =
4055 if not (Wsi.withctrl mask)
4056 then
4057 match key with
4058 | @left | @kpleft -> coe (self#updownlevel ~-1)
4059 | @right | @kpright -> coe (self#updownlevel 1)
4060 | _ -> super#key key mask
4061 else super#key key mask
4062 end);
4063 G.postRedisplay "info";
4066 let enterhelpmode =
4067 let source =
4068 (object
4069 inherit lvsourcebase
4070 method getitemcount = Array.length state.help
4071 method getitem n =
4072 let s, l, _ = state.help.(n) in
4073 (s, l)
4075 method exit ~uioh ~cancel ~active ~first ~pan =
4076 let optuioh =
4077 if not cancel
4078 then (
4079 match state.help.(active) with
4080 | _, _, Action f -> Some (f uioh)
4081 | _, _, Noaction -> Some uioh
4083 else None
4085 m_active <- active;
4086 m_first <- first;
4087 m_pan <- pan;
4088 optuioh
4090 method hasaction n =
4091 match state.help.(n) with
4092 | _, _, Action _ -> true
4093 | _, _, Noaction -> false
4095 initializer
4096 m_active <- -1
4097 end)
4098 in fun () ->
4099 let modehash = findkeyhash conf "help" in
4100 resetmstate ();
4101 state.uioh <- coe (new listview
4102 ~zebra:false ~helpmode:true
4103 ~source ~trusted:true ~modehash);
4104 G.postRedisplay "help";
4107 let entermsgsmode =
4108 let msgsource =
4109 (object
4110 inherit lvsourcebase
4111 val mutable m_items = E.a
4113 method getitemcount = 1 + Array.length m_items
4115 method getitem n =
4116 if n = 0
4117 then "[Clear]", 0
4118 else m_items.(n-1), 0
4120 method exit ~uioh ~cancel ~active ~first ~pan =
4121 ignore uioh;
4122 if not cancel
4123 then (
4124 if active = 0
4125 then Buffer.clear state.errmsgs;
4127 m_active <- active;
4128 m_first <- first;
4129 m_pan <- pan;
4130 None
4132 method hasaction n =
4133 n = 0
4135 method reset =
4136 state.newerrmsgs <- false;
4137 let l = Str.split newlinere (Buffer.contents state.errmsgs) in
4138 m_items <- Array.of_list l
4140 initializer
4141 m_active <- 0
4142 end)
4143 in fun () ->
4144 state.text <- E.s;
4145 resetmstate ();
4146 msgsource#reset;
4147 let source = (msgsource :> lvsource) in
4148 let modehash = findkeyhash conf "listview" in
4149 state.uioh <-
4150 coe (object
4151 inherit listview ~zebra:false ~helpmode:false
4152 ~source ~trusted:false ~modehash as super
4153 method! display =
4154 if state.newerrmsgs
4155 then msgsource#reset;
4156 super#display
4157 end);
4158 G.postRedisplay "msgs";
4161 let getusertext s =
4162 let editor = getenvwithdef "EDITOR" E.s in
4163 if emptystr editor
4164 then E.s
4165 else
4166 let tmppath = Filename.temp_file "llpp" "note" in
4167 if nonemptystr s
4168 then (
4169 let oc = open_out tmppath in
4170 output_string oc s;
4171 close_out oc;
4173 let execstr = editor ^ " " ^ tmppath in
4174 let s =
4175 match spawn execstr [] with
4176 | (exception exn) ->
4177 impmsg "spawn(%S) failed: %s" execstr @@ exntos exn;
4179 | pid ->
4180 match Unix.waitpid [] pid with
4181 | (exception exn) ->
4182 impmsg "waitpid(%d) failed: %s" pid @@ exntos exn;
4184 | (_pid, status) ->
4185 match status with
4186 | Unix.WEXITED 0 -> filecontents tmppath
4187 | Unix.WEXITED n ->
4188 impmsg "editor process(%s) exited abnormally: %d" execstr n;
4190 | Unix.WSIGNALED n ->
4191 impmsg "editor process(%s) was killed by signal %d" execstr n;
4193 | Unix.WSTOPPED n ->
4194 impmsg "editor(%s) process was stopped by signal %d" execstr n;
4197 match Unix.unlink tmppath with
4198 | (exception exn) ->
4199 impmsg "failed to ulink %S: %s" tmppath @@ exntos exn;
4201 | () -> s
4204 let enterannotmode opaque slinkindex =
4205 let msgsource =
4206 (object
4207 inherit lvsourcebase
4208 val mutable m_text = E.s
4209 val mutable m_items = E.a
4211 method getitemcount = Array.length m_items
4213 method getitem n =
4214 let label, _func = m_items.(n) in
4215 label, 0
4217 method exit ~uioh ~cancel ~active ~first ~pan =
4218 ignore (uioh, first, pan);
4219 if not cancel
4220 then (
4221 let _label, func = m_items.(active) in
4222 func ()
4224 None
4226 method hasaction n = nonemptystr @@ fst m_items.(n)
4228 method reset s =
4229 let rec split accu b i =
4230 let p = b+i in
4231 if p = String.length s
4232 then (String.sub s b (p-b), unit) :: accu
4233 else
4234 if (i > 70 && s.[p] = ' ') || s.[p] = '\r' || s.[p] = '\n'
4235 then
4236 let ss = if i = 0 then E.s else String.sub s b i in
4237 split ((ss, unit)::accu) (p+1) 0
4238 else
4239 split accu b (i+1)
4241 let cleanup () =
4242 wcmd "freepage %s" (~> opaque);
4243 let keys =
4244 Hashtbl.fold (fun key opaque' accu ->
4245 if opaque' = opaque'
4246 then key :: accu else accu) state.pagemap []
4248 List.iter (Hashtbl.remove state.pagemap) keys;
4249 flushtiles ();
4250 gotoxy state.x state.y
4252 let dele () =
4253 delannot opaque slinkindex;
4254 cleanup ();
4256 let edit inline () =
4257 let update s =
4258 if emptystr s
4259 then dele ()
4260 else (
4261 modannot opaque slinkindex s;
4262 cleanup ();
4265 if inline
4266 then
4267 let mode = state.mode in
4268 state.mode <-
4269 Textentry (
4270 ("annotation: ", m_text, None, textentry, update, true),
4271 fun _ -> state.mode <- mode);
4272 state.text <- E.s;
4273 enttext ();
4274 else
4275 let s = getusertext m_text in
4276 update s
4278 m_text <- s;
4279 m_items <-
4280 ( "[Copy]", fun () -> selstring m_text)
4281 :: ("[Delete]", dele)
4282 :: ("[Edit]", edit conf.annotinline)
4283 :: (E.s, unit)
4284 :: split [] 0 0 |> List.rev |> Array.of_list
4286 initializer
4287 m_active <- 0
4288 end)
4290 state.text <- E.s;
4291 let s = getannotcontents opaque slinkindex in
4292 resetmstate ();
4293 msgsource#reset s;
4294 let source = (msgsource :> lvsource) in
4295 let modehash = findkeyhash conf "listview" in
4296 state.uioh <- coe (object
4297 inherit listview ~zebra:false ~helpmode:false
4298 ~source ~trusted:false ~modehash
4299 end);
4300 G.postRedisplay "enterannotmode";
4303 let gotoremote spec =
4304 let filename, dest = splitatchar spec '#' in
4305 let getpath filename =
4306 let path =
4307 if nonemptystr filename
4308 then
4309 if Filename.is_relative filename
4310 then
4311 let dir = Filename.dirname state.path in
4312 let dir =
4313 if Filename.is_implicit dir
4314 then Filename.concat (Sys.getcwd ()) dir
4315 else dir
4317 Filename.concat dir filename
4318 else filename
4319 else E.s
4321 if Sys.file_exists path
4322 then path
4323 else E.s
4325 let path = getpath filename in
4326 let dospawn lcmd =
4327 if conf.riani
4328 then
4329 let cmd = Lazy.force_val lcmd in
4330 match spawn cmd with
4331 | _pid -> ()
4332 | (exception exn) ->
4333 dolog "failed to execute `%s': %s" cmd @@ exntos exn
4334 else
4335 let anchor = getanchor () in
4336 let ranchor = state.path, state.password, anchor, state.origin in
4337 state.origin <- E.s;
4338 state.ranchors <- ranchor :: state.ranchors;
4339 opendoc path E.s;
4341 if substratis spec 0 "page="
4342 then
4343 match Scanf.sscanf spec "page=%d" (fun n -> n) with
4344 | pageno ->
4345 state.anchor <- (pageno, 0.0, 0.0);
4346 dospawn @@ lazy (Printf.sprintf "%s -page %d %S" !selfexec pageno path);
4347 | exception exn ->
4348 adderrfmt "error parsing remote destination" "page: %s" @@ exntos exn
4349 else (
4350 state.nameddest <- dest;
4351 dospawn @@ lazy (!selfexec ^ " " ^ path ^ " -dest " ^ dest)
4355 let gotounder = function
4356 | Ulinkuri s when isexternallink s ->
4357 if substratis s 0 "file://"
4358 then gotoremote @@ String.sub s 7 (String.length s - 7)
4359 else gotouri s
4360 | Ulinkuri s ->
4361 let pageno, x, y = uritolocation s in
4362 addnav ();
4363 gotopagexy !wtmode pageno x y
4364 | Utext _ | Unone -> ()
4365 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
4368 let gotooutline (_, _, kind) =
4369 match kind with
4370 | Onone -> ()
4371 | Oanchor anchor ->
4372 let (pageno, y, _) = anchor in
4373 let y = getanchory
4374 (if conf.presentation then (pageno, y, 1.0) else anchor)
4376 addnav ();
4377 gotoghyll y
4378 | Ouri uri -> gotounder (Ulinkuri uri)
4379 | Olaunch _cmd -> failwith "gotounder (Ulaunch cmd)"
4380 | Oremote _remote -> failwith "gotounder (Uremote remote)"
4381 | Ohistory hist -> gotohist hist
4382 | Oremotedest _remotedest -> failwith "gotounder (Uremotedest remotedest)"
4385 class outlinesoucebase fetchoutlines = object (self)
4386 inherit lvsourcebase
4387 val mutable m_items = E.a
4388 val mutable m_minfo = E.a
4389 val mutable m_orig_items = E.a
4390 val mutable m_orig_minfo = E.a
4391 val mutable m_narrow_patterns = []
4392 val mutable m_gen = -1
4394 method getitemcount = Array.length m_items
4396 method getitem n =
4397 let s, n, _ = m_items.(n) in
4398 (s, n+0)
4400 method exit ~(uioh:uioh) ~cancel ~active ~(first:int) ~pan :
4401 uioh option =
4402 ignore (uioh, first);
4403 let items, minfo =
4404 if m_narrow_patterns = []
4405 then m_orig_items, m_orig_minfo
4406 else m_items, m_minfo
4408 m_pan <- pan;
4409 if not cancel
4410 then (
4411 m_items <- items;
4412 m_minfo <- minfo;
4413 gotooutline m_items.(active);
4415 else (
4416 m_items <- items;
4417 m_minfo <- minfo;
4419 None
4421 method hasaction (_:int) = true
4423 method greetmsg =
4424 if Array.length m_items != Array.length m_orig_items
4425 then
4426 let s =
4427 match m_narrow_patterns with
4428 | one :: [] -> one
4429 | many -> String.concat "@Uellipsis" (List.rev many)
4431 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
4432 else E.s
4434 method statestr =
4435 match m_narrow_patterns with
4436 | [] -> E.s
4437 | one :: [] -> one
4438 | head :: _ -> "@Uellipsis" ^ head
4440 method narrow pattern =
4441 match Str.regexp_case_fold pattern with
4442 | (exception _) -> ()
4443 | re ->
4444 let rec loop accu minfo n =
4445 if n = -1
4446 then (
4447 m_items <- Array.of_list accu;
4448 m_minfo <- Array.of_list minfo;
4450 else
4451 let (s, _, _) as o = m_items.(n) in
4452 let accu, minfo =
4453 match Str.search_forward re s 0 with
4454 | (exception Not_found) -> accu, minfo
4455 | first -> o :: accu, (first, Str.match_end ()) :: minfo
4457 loop accu minfo (n-1)
4459 loop [] [] (Array.length m_items - 1)
4461 method! getminfo = m_minfo
4463 method denarrow =
4464 m_orig_items <- fetchoutlines ();
4465 m_minfo <- m_orig_minfo;
4466 m_items <- m_orig_items
4468 method add_narrow_pattern pattern =
4469 m_narrow_patterns <- pattern :: m_narrow_patterns
4471 method del_narrow_pattern =
4472 match m_narrow_patterns with
4473 | _ :: rest -> m_narrow_patterns <- rest
4474 | [] -> ()
4476 method renarrow =
4477 self#denarrow;
4478 match m_narrow_patterns with
4479 | pattern :: [] -> self#narrow pattern; pattern
4480 | list ->
4481 List.fold_left (fun accu pattern ->
4482 self#narrow pattern;
4483 pattern ^ "@Uellipsis" ^ accu) E.s list
4485 method calcactive (_:anchor) = 0
4487 method reset anchor items =
4488 if state.gen != m_gen
4489 then (
4490 m_orig_items <- items;
4491 m_items <- items;
4492 m_narrow_patterns <- [];
4493 m_minfo <- E.a;
4494 m_orig_minfo <- E.a;
4495 m_gen <- state.gen;
4497 else (
4498 if items != m_orig_items
4499 then (
4500 m_orig_items <- items;
4501 if m_narrow_patterns == []
4502 then m_items <- items;
4505 let active = self#calcactive anchor in
4506 m_active <- active;
4507 m_first <- firstof m_first active
4511 let outlinesource fetchoutlines =
4512 (object
4513 inherit outlinesoucebase fetchoutlines
4514 method! calcactive anchor =
4515 let rely = getanchory anchor in
4516 let rec loop n best bestd =
4517 if n = Array.length m_items
4518 then best
4519 else
4520 let _, _, kind = m_items.(n) in
4521 match kind with
4522 | Oanchor anchor ->
4523 let orely = getanchory anchor in
4524 let d = abs (orely - rely) in
4525 if d < bestd
4526 then loop (n+1) n d
4527 else loop (n+1) best bestd
4528 | Onone | Oremote _ | Olaunch _
4529 | Oremotedest _ | Ouri _ | Ohistory _ ->
4530 loop (n+1) best bestd
4532 loop 0 ~-1 max_int
4533 end)
4536 let enteroutlinemode, enterbookmarkmode, enterhistmode =
4537 let mkselector sourcetype =
4538 let fetchoutlines () =
4539 match sourcetype with
4540 | `bookmarks -> Array.of_list state.bookmarks
4541 | `outlines -> state.outlines
4542 | `history -> genhistoutlines ()
4544 let source =
4545 if sourcetype = `history
4546 then new outlinesoucebase fetchoutlines
4547 else outlinesource fetchoutlines
4549 fun errmsg ->
4550 let outlines = fetchoutlines () in
4551 if Array.length outlines = 0
4552 then (
4553 showtext ' ' errmsg;
4555 else (
4556 resetmstate ();
4557 Wsi.setcursor Wsi.CURSOR_INHERIT;
4558 let anchor = getanchor () in
4559 source#reset anchor outlines;
4560 state.text <- source#greetmsg;
4561 state.uioh <-
4562 coe (new outlinelistview ~zebra:(sourcetype=`history) ~source);
4563 G.postRedisplay "enter selector";
4566 let mkenter sourcetype errmsg =
4567 let enter = mkselector sourcetype in
4568 fun () -> enter errmsg
4570 mkenter `outlines "document has no outline"
4571 , mkenter `bookmarks "document has no bookmarks (yet)"
4572 , mkenter `history "history is empty"
4575 let quickbookmark ?title () =
4576 match state.layout with
4577 | [] -> ()
4578 | l :: _ ->
4579 let title =
4580 match title with
4581 | None ->
4582 let tm = Unix.localtime (now ()) in
4583 Printf.sprintf
4584 "Quick (page %d) (bookmarked at %02d/%02d/%d %02d:%02d)"
4585 (l.pageno+1)
4586 tm.Unix.tm_mday
4587 (tm.Unix.tm_mon+1)
4588 (tm.Unix.tm_year + 1900)
4589 tm.Unix.tm_hour
4590 tm.Unix.tm_min
4591 | Some title -> title
4593 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4596 let setautoscrollspeed step goingdown =
4597 let incr = max 1 ((abs step) / 2) in
4598 let incr = if goingdown then incr else -incr in
4599 let astep = boundastep state.winh (step + incr) in
4600 state.autoscroll <- Some astep;
4603 let canpan () =
4604 match conf.columns with
4605 | Csplit _ -> true
4606 | Csingle _ | Cmulti _ -> state.x != 0 || conf.zoom > 1.0
4609 let panbound x = bound x (-state.w) state.winw;;
4611 let existsinrow pageno (columns, coverA, coverB) p =
4612 let last = ((pageno - coverA) mod columns) + columns in
4613 let rec any = function
4614 | [] -> false
4615 | l :: rest ->
4616 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4617 then p l
4618 else (
4619 if not (p l)
4620 then (if l.pageno = last then false else any rest)
4621 else true
4624 any state.layout
4627 let nextpage () =
4628 match state.layout with
4629 | [] ->
4630 let pageno = page_of_y state.y in
4631 gotoghyll (getpagey (pageno+1))
4632 | l :: rest ->
4633 match conf.columns with
4634 | Csingle _ ->
4635 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4636 then
4637 let y = clamp (pgscale state.winh) in
4638 gotoghyll y
4639 else
4640 let pageno = min (l.pageno+1) (state.pagecount-1) in
4641 gotoghyll (getpagey pageno)
4642 | Cmulti ((c, _, _) as cl, _) ->
4643 if conf.presentation
4644 && (existsinrow l.pageno cl
4645 (fun l -> l.pageh > l.pagey + l.pagevh))
4646 then
4647 let y = clamp (pgscale state.winh) in
4648 gotoghyll y
4649 else
4650 let pageno = min (l.pageno+c) (state.pagecount-1) in
4651 gotoghyll (getpagey pageno)
4652 | Csplit (n, _) ->
4653 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4654 then
4655 let pagey, pageh = getpageyh l.pageno in
4656 let pagey = pagey + pageh * l.pagecol in
4657 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4658 gotoghyll (pagey + pageh + ips)
4661 let prevpage () =
4662 match state.layout with
4663 | [] ->
4664 let pageno = page_of_y state.y in
4665 gotoghyll (getpagey (pageno-1))
4666 | l :: _ ->
4667 match conf.columns with
4668 | Csingle _ ->
4669 if conf.presentation && l.pagey != 0
4670 then
4671 gotoghyll (clamp (pgscale ~-(state.winh)))
4672 else
4673 let pageno = max 0 (l.pageno-1) in
4674 gotoghyll (getpagey pageno)
4675 | Cmulti ((c, _, coverB) as cl, _) ->
4676 if conf.presentation &&
4677 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4678 then
4679 gotoghyll (clamp (pgscale ~-(state.winh)))
4680 else
4681 let decr =
4682 if l.pageno = state.pagecount - coverB
4683 then 1
4684 else c
4686 let pageno = max 0 (l.pageno-decr) in
4687 gotoghyll (getpagey pageno)
4688 | Csplit (n, _) ->
4689 let y =
4690 if l.pagecol = 0
4691 then
4692 if l.pageno = 0
4693 then l.pagey
4694 else
4695 let pageno = max 0 (l.pageno-1) in
4696 let pagey, pageh = getpageyh pageno in
4697 pagey + (n-1)*pageh
4698 else
4699 let pagey, pageh = getpageyh l.pageno in
4700 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4702 gotoghyll y
4705 let save () =
4706 if emptystr conf.savecmd
4707 then error "don't know where to save modified document"
4708 else
4709 let savecmd = Str.global_replace percentsre state.path conf.savecmd in
4710 let path =
4711 getcmdoutput
4712 (fun s -> error "failed to obtain path to the saved copy: %s" s)
4713 savecmd
4715 if nonemptystr path
4716 then
4717 let tmp = path ^ ".tmp" in
4718 savedoc tmp;
4719 Unix.rename tmp path;
4722 let viewkeyboard key mask =
4723 let enttext te =
4724 let mode = state.mode in
4725 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4726 state.text <- E.s;
4727 enttext ();
4728 G.postRedisplay "view:enttext"
4730 let ctrl = Wsi.withctrl mask in
4731 let key = Wsi.keypadtodigitkey key in
4732 match key with
4733 | @Q -> exit 0
4735 | @W ->
4736 if hasunsavedchanges ()
4737 then save ()
4739 | @insert ->
4740 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4741 then (
4742 state.mode <- (
4743 match state.lnava with
4744 | None -> LinkNav (Ltgendir 0)
4745 | Some pn -> LinkNav (Ltexact pn)
4747 gotoxy state.x state.y;
4749 else impmsg "keyboard link navigation does not work under rotation"
4751 | @escape | @q ->
4752 begin match state.mstate with
4753 | Mzoomrect _ ->
4754 resetmstate ();
4755 G.postRedisplay "kill rect";
4756 | Msel _
4757 | Mpan _
4758 | Mscrolly | Mscrollx
4759 | Mzoom _
4760 | Mnone ->
4761 begin match state.mode with
4762 | LinkNav _ ->
4763 state.mode <- View;
4764 G.postRedisplay "esc leave linknav"
4765 | Birdseye _
4766 | Textentry _
4767 | View ->
4768 match state.ranchors with
4769 | [] -> raise Quit
4770 | (path, password, anchor, origin) :: rest ->
4771 state.ranchors <- rest;
4772 state.anchor <- anchor;
4773 state.origin <- origin;
4774 state.nameddest <- E.s;
4775 opendoc path password
4776 end;
4777 end;
4779 | @backspace ->
4780 gotoghyll (getnav ~-1)
4782 | @o ->
4783 enteroutlinemode ()
4785 | @H ->
4786 enterhistmode ()
4788 | @u ->
4789 state.rects <- [];
4790 state.text <- E.s;
4791 Hashtbl.iter (fun _ opaque ->
4792 clearmark opaque;
4793 Hashtbl.clear state.prects) state.pagemap;
4794 G.postRedisplay "dehighlight";
4796 | @slash | @question ->
4797 let ondone isforw s =
4798 cbput state.hists.pat s;
4799 state.searchpattern <- s;
4800 search s isforw
4802 let s = String.make 1 (Char.chr key) in
4803 enttext (s, E.s, Some (onhist state.hists.pat),
4804 textentry, ondone (key = @slash), true)
4806 | @plus | @kpplus | @equals when ctrl ->
4807 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4808 pivotzoom (conf.zoom +. incr)
4810 | @plus | @kpplus ->
4811 let ondone s =
4812 let n =
4813 try int_of_string s with exn ->
4814 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn;
4815 max_int
4817 if n != max_int
4818 then (
4819 conf.pagebias <- n;
4820 state.text <- "page bias is now " ^ string_of_int n;
4823 enttext ("page bias: ", E.s, None, intentry, ondone, true)
4825 | @minus | @kpminus when ctrl ->
4826 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4827 pivotzoom (max 0.01 (conf.zoom -. decr))
4829 | @minus | @kpminus ->
4830 let ondone msg = state.text <- msg in
4831 enttext (
4832 "option [acfhilpstvxACFPRSZTISM]: ", E.s, None,
4833 optentry state.mode, ondone, true
4836 | @0 when ctrl ->
4837 if conf.zoom = 1.0
4838 then gotoxy 0 state.y
4839 else setzoom 1.0
4841 | (@1 | @2) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
4842 let cols =
4843 match conf.columns with
4844 | Csingle _ | Cmulti _ -> 1
4845 | Csplit (n, _) -> n
4847 let h = state.winh -
4848 conf.interpagespace lsl (if conf.presentation then 1 else 0)
4850 let zoom = zoomforh state.winw h 0 cols in
4851 if zoom > 0.0 && (key = @2 || zoom < 1.0)
4852 then setzoom zoom
4854 | @3 when ctrl ->
4855 let fm =
4856 match conf.fitmodel with
4857 | FitWidth -> FitProportional
4858 | FitProportional -> FitPage
4859 | FitPage -> FitWidth
4861 state.text <- "fit model: " ^ FMTE.to_string fm;
4862 reqlayout conf.angle fm
4864 | @4 when ctrl -> (* ctrl-4 *)
4865 let zoom = getmaxw () /. float state.winw in
4866 if zoom > 0.0 then setzoom zoom
4868 | @F9 ->
4869 togglebirdseye ()
4871 | @9 when ctrl ->
4872 togglebirdseye ()
4874 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4875 when not ctrl -> (* 0..9 *)
4876 let ondone s =
4877 let n =
4878 try int_of_string s with exn ->
4879 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn;
4882 if n >= 0
4883 then (
4884 addnav ();
4885 cbput state.hists.pag (string_of_int n);
4886 gotopage1 (n + conf.pagebias - 1) 0;
4889 let pageentry text key =
4890 match Char.unsafe_chr key with
4891 | 'g' -> TEdone text
4892 | _ -> intentry text key
4894 let text = String.make 1 (Char.chr key) in
4895 enttext (":", text, Some (onhist state.hists.pag),
4896 pageentry, ondone, true)
4898 | @b ->
4899 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
4900 G.postRedisplay "toggle scrollbar";
4902 | @B ->
4903 state.bzoom <- not state.bzoom;
4904 state.rects <- [];
4905 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
4907 | @l ->
4908 conf.hlinks <- not conf.hlinks;
4909 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4910 G.postRedisplay "toggle highlightlinks";
4912 | @F ->
4913 if conf.angle mod 360 = 0
4914 then (
4915 state.glinks <- true;
4916 let mode = state.mode in
4917 state.mode <-
4918 Textentry (
4919 (":", E.s, None, linknentry, linknact gotounder, false),
4920 (fun _ ->
4921 state.glinks <- false;
4922 state.mode <- mode)
4924 state.text <- E.s;
4925 G.postRedisplay "view:linkent(F)"
4927 else impmsg "hint mode does not work under rotation"
4929 | @y ->
4930 state.glinks <- true;
4931 let mode = state.mode in
4932 state.mode <-
4933 Textentry (
4935 ":", E.s, None, linknentry, linknact (fun under ->
4936 selstring (undertext under);
4937 ), false
4939 fun _ ->
4940 state.glinks <- false;
4941 state.mode <- mode
4943 state.text <- E.s;
4944 G.postRedisplay "view:linkent"
4946 | @a ->
4947 begin match state.autoscroll with
4948 | Some step ->
4949 conf.autoscrollstep <- step;
4950 state.autoscroll <- None
4951 | None ->
4952 if conf.autoscrollstep = 0
4953 then state.autoscroll <- Some 1
4954 else state.autoscroll <- Some conf.autoscrollstep
4957 | @p when ctrl ->
4958 launchpath () (* XXX where do error messages go? *)
4960 | @P ->
4961 setpresentationmode (not conf.presentation);
4962 showtext ' ' ("presentation mode " ^
4963 if conf.presentation then "on" else "off");
4965 | @f ->
4966 if List.mem Wsi.Fullscreen state.winstate
4967 then Wsi.reshape conf.cwinw conf.cwinh
4968 else Wsi.fullscreen ()
4970 | @p | @N ->
4971 search state.searchpattern false
4973 | @n | @F3 ->
4974 search state.searchpattern true
4976 | @t ->
4977 begin match state.layout with
4978 | [] -> ()
4979 | l :: _ ->
4980 gotoghyll (getpagey l.pageno)
4983 | @space ->
4984 nextpage ()
4986 | @delete | @kpdelete -> (* delete *)
4987 prevpage ()
4989 | @equals ->
4990 showtext ' ' (describe_location ());
4992 | @w ->
4993 begin match state.layout with
4994 | [] -> ()
4995 | l :: _ ->
4996 Wsi.reshape l.pagew l.pageh;
4997 G.postRedisplay "w"
5000 | @apos ->
5001 enterbookmarkmode ()
5003 | @h | @F1 ->
5004 enterhelpmode ()
5006 | @i ->
5007 enterinfomode ()
5009 | @e when Buffer.length state.errmsgs > 0 ->
5010 entermsgsmode ()
5012 | @m ->
5013 let ondone s =
5014 match state.layout with
5015 | l :: _ ->
5016 if nonemptystr s
5017 then
5018 state.bookmarks <-
5019 (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
5020 | _ -> ()
5022 enttext ("bookmark: ", E.s, None, textentry, ondone, true)
5024 | @tilde ->
5025 quickbookmark ();
5026 showtext ' ' "Quick bookmark added";
5028 | @z ->
5029 begin match state.layout with
5030 | l :: _ ->
5031 let rect = getpdimrect l.pagedimno in
5032 let w, h =
5033 if conf.crophack
5034 then
5035 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5036 truncate (1.2 *. (rect.(3) -. rect.(0))))
5037 else
5038 (truncate (rect.(1) -. rect.(0)),
5039 truncate (rect.(3) -. rect.(0)))
5041 let w = truncate ((float w)*.conf.zoom)
5042 and h = truncate ((float h)*.conf.zoom) in
5043 if w != 0 && h != 0
5044 then (
5045 state.anchor <- getanchor ();
5046 Wsi.reshape w (h + conf.interpagespace)
5048 G.postRedisplay "z";
5050 | [] -> ()
5053 | @x -> state.roam ()
5055 | @Lt | @Gt ->
5056 reqlayout (conf.angle +
5057 (if key = @Gt then 30 else -30)) conf.fitmodel
5059 | @Lb | @Rb ->
5060 conf.colorscale <-
5061 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5063 G.postRedisplay "brightness";
5065 | @c when state.mode = View ->
5066 if Wsi.withalt mask
5067 then (
5068 if conf.zoom > 1.0
5069 then
5070 let m = (state.winw - state.w) / 2 in
5071 gotoxy_and_clear_text m state.y
5073 else
5074 let (c, a, b), z =
5075 match state.prevcolumns with
5076 | None -> (1, 0, 0), 1.0
5077 | Some (columns, z) ->
5078 let cab =
5079 match columns with
5080 | Csplit (c, _) -> -c, 0, 0
5081 | Cmulti ((c, a, b), _) -> c, a, b
5082 | Csingle _ -> 1, 0, 0
5084 cab, z
5086 setcolumns View c a b;
5087 setzoom z
5089 | @down | @up when ctrl && Wsi.withshift mask ->
5090 let zoom, x = state.prevzoom in
5091 setzoom zoom;
5092 state.x <- x;
5094 | @k | @up | @kpup ->
5095 begin match state.autoscroll with
5096 | None ->
5097 begin match state.mode with
5098 | Birdseye beye -> upbirdseye 1 beye
5099 | Textentry _
5100 | View
5101 | LinkNav _ ->
5102 if ctrl
5103 then gotoxy_and_clear_text state.x (clamp ~-(state.winh/2))
5104 else (
5105 if not (Wsi.withshift mask) && conf.presentation
5106 then prevpage ()
5107 else gotoghyll1 true (clamp (-conf.scrollstep))
5110 | Some n ->
5111 setautoscrollspeed n false
5114 | @j | @down | @kpdown ->
5115 begin match state.autoscroll with
5116 | None ->
5117 begin match state.mode with
5118 | Birdseye beye -> downbirdseye 1 beye
5119 | Textentry _
5120 | View
5121 | LinkNav _ ->
5122 if ctrl
5123 then gotoxy_and_clear_text state.x (clamp (state.winh/2))
5124 else (
5125 if not (Wsi.withshift mask) && conf.presentation
5126 then nextpage ()
5127 else gotoghyll1 true (clamp (conf.scrollstep))
5130 | Some n ->
5131 setautoscrollspeed n true
5134 | @left | @right | @kpleft | @kpright when not (Wsi.withalt mask) ->
5135 if canpan ()
5136 then
5137 let dx =
5138 if ctrl
5139 then state.winw / 2
5140 else conf.hscrollstep
5142 let dx = if key = @left || key = @kpleft then dx else -dx in
5143 gotoxy_and_clear_text (panbound (state.x + dx)) state.y
5144 else (
5145 state.text <- E.s;
5146 G.postRedisplay "left/right"
5149 | @prior | @kpprior ->
5150 let y =
5151 if ctrl
5152 then
5153 match state.layout with
5154 | [] -> state.y
5155 | l :: _ -> state.y - l.pagey
5156 else
5157 clamp (pgscale (-state.winh))
5159 gotoghyll y
5161 | @next | @kpnext ->
5162 let y =
5163 if ctrl
5164 then
5165 match List.rev state.layout with
5166 | [] -> state.y
5167 | l :: _ -> getpagey l.pageno
5168 else
5169 clamp (pgscale state.winh)
5171 gotoghyll y
5173 | @g | @home | @kphome ->
5174 addnav ();
5175 gotoghyll 0
5176 | @G | @jend | @kpend ->
5177 addnav ();
5178 gotoghyll (clamp state.maxy)
5180 | @right | @kpright when Wsi.withalt mask ->
5181 gotoghyll (getnav 1)
5182 | @left | @kpleft when Wsi.withalt mask ->
5183 gotoghyll (getnav ~-1)
5185 | @r ->
5186 reload ()
5188 | @v when conf.debug ->
5189 state.rects <- [];
5190 List.iter (fun l ->
5191 match getopaque l.pageno with
5192 | None -> ()
5193 | Some opaque ->
5194 let x0, y0, x1, y1 = pagebbox opaque in
5195 let rect = (float x0, float y0,
5196 float x1, float y0,
5197 float x1, float y1,
5198 float x0, float y1) in
5199 debugrect rect;
5200 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
5201 state.rects <- (l.pageno, color, rect) :: state.rects;
5202 ) state.layout;
5203 G.postRedisplay "v";
5205 | @pipe ->
5206 let mode = state.mode in
5207 let cmd = ref E.s in
5208 let onleave = function
5209 | Cancel -> state.mode <- mode
5210 | Confirm ->
5211 List.iter (fun l ->
5212 match getopaque l.pageno with
5213 | Some opaque -> pipesel opaque !cmd
5214 | None -> ()) state.layout;
5215 state.mode <- mode
5217 let ondone s =
5218 cbput state.hists.sel s;
5219 cmd := s
5221 let te =
5222 "| ", !cmd, Some (onhist state.hists.sel), textentry, ondone, true
5224 G.postRedisplay "|";
5225 state.mode <- Textentry (te, onleave);
5227 | _ ->
5228 vlog "huh? %s" (Wsi.keyname key)
5231 let linknavkeyboard key mask linknav =
5232 let getpage pageno =
5233 let rec loop = function
5234 | [] -> None
5235 | l :: _ when l.pageno = pageno -> Some l
5236 | _ :: rest -> loop rest
5237 in loop state.layout
5239 let doexact (pageno, n) =
5240 match getopaque pageno, getpage pageno with
5241 | Some opaque, Some l ->
5242 if key = @enter || key = @kpenter
5243 then
5244 let under = getlink opaque n in
5245 G.postRedisplay "link gotounder";
5246 gotounder under;
5247 state.mode <- View;
5248 else
5249 let opt, dir =
5250 match key with
5251 | @home ->
5252 Some (findlink opaque LDfirst), -1
5254 | @jend ->
5255 Some (findlink opaque LDlast), 1
5257 | @left ->
5258 Some (findlink opaque (LDleft n)), -1
5260 | @right ->
5261 Some (findlink opaque (LDright n)), 1
5263 | @up ->
5264 Some (findlink opaque (LDup n)), -1
5266 | @down ->
5267 Some (findlink opaque (LDdown n)), 1
5269 | _ -> None, 0
5271 let pwl l dir =
5272 begin match findpwl l.pageno dir with
5273 | Pwlnotfound -> ()
5274 | Pwl pageno ->
5275 let notfound dir =
5276 state.mode <- LinkNav (Ltgendir dir);
5277 let y, h = getpageyh pageno in
5278 let y =
5279 if dir < 0
5280 then y + h - state.winh
5281 else y
5283 gotoxy state.x y
5285 begin match getopaque pageno, getpage pageno with
5286 | Some opaque, Some _ ->
5287 let link =
5288 let ld = if dir > 0 then LDfirst else LDlast in
5289 findlink opaque ld
5291 begin match link with
5292 | Lfound m ->
5293 showlinktype (getlink opaque m);
5294 state.mode <- LinkNav (Ltexact (pageno, m));
5295 G.postRedisplay "linknav jpage";
5296 | Lnotfound -> notfound dir
5297 end;
5298 | _ -> notfound dir
5299 end;
5300 end;
5302 begin match opt with
5303 | Some Lnotfound -> pwl l dir;
5304 | Some (Lfound m) ->
5305 if m = n
5306 then pwl l dir
5307 else (
5308 let _, y0, _, y1 = getlinkrect opaque m in
5309 if y0 < l.pagey
5310 then gotopage1 l.pageno y0
5311 else (
5312 let d = fstate.fontsize + 1 in
5313 if y1 - l.pagey > l.pagevh - d
5314 then gotopage1 l.pageno (y1 - state.winh + d)
5315 else G.postRedisplay "linknav";
5317 showlinktype (getlink opaque m);
5318 state.mode <- LinkNav (Ltexact (l.pageno, m));
5321 | None -> viewkeyboard key mask
5322 end;
5323 | _ -> viewkeyboard key mask
5325 if key = @insert
5326 then (
5327 begin match linknav with
5328 | Ltexact pa -> state.lnava <- Some pa
5329 | Ltgendir _ | Ltnotready _ -> ()
5330 end;
5331 state.mode <- View;
5332 G.postRedisplay "leave linknav"
5334 else
5335 match linknav with
5336 | Ltgendir _ | Ltnotready _ -> viewkeyboard key mask
5337 | Ltexact exact -> doexact exact
5340 let keyboard key mask =
5341 if (key = @g && Wsi.withctrl mask) && not (istextentry state.mode)
5342 then wcmd "interrupt"
5343 else state.uioh <- state.uioh#key key mask
5346 let birdseyekeyboard key mask
5347 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5348 let incr =
5349 match conf.columns with
5350 | Csingle _ -> 1
5351 | Cmulti ((c, _, _), _) -> c
5352 | Csplit _ -> failwith "bird's eye split mode"
5354 let pgh layout = List.fold_left
5355 (fun m l -> max l.pageh m) state.winh layout in
5356 match key with
5357 | @l when Wsi.withctrl mask ->
5358 let y, h = getpageyh pageno in
5359 let top = (state.winh - h) / 2 in
5360 gotoxy state.x (max 0 (y - top))
5361 | @enter | @kpenter -> leavebirdseye beye false
5362 | @escape -> leavebirdseye beye true
5363 | @up -> upbirdseye incr beye
5364 | @down -> downbirdseye incr beye
5365 | @left -> upbirdseye 1 beye
5366 | @right -> downbirdseye 1 beye
5368 | @prior ->
5369 begin match state.layout with
5370 | l :: _ ->
5371 if l.pagey != 0
5372 then (
5373 state.mode <- Birdseye (
5374 oconf, leftx, l.pageno, hooverpageno, anchor
5376 gotopage1 l.pageno 0;
5378 else (
5379 let layout = layout state.x (state.y-state.winh)
5380 state.winw
5381 (pgh state.layout) in
5382 match layout with
5383 | [] -> gotoxy state.x (clamp (-state.winh))
5384 | l :: _ ->
5385 state.mode <- Birdseye (
5386 oconf, leftx, l.pageno, hooverpageno, anchor
5388 gotopage1 l.pageno 0
5391 | [] -> gotoxy state.x (clamp (-state.winh))
5392 end;
5394 | @next ->
5395 begin match List.rev state.layout with
5396 | l :: _ ->
5397 let layout = layout state.x
5398 (state.y + (pgh state.layout))
5399 state.winw state.winh in
5400 begin match layout with
5401 | [] ->
5402 let incr = l.pageh - l.pagevh in
5403 if incr = 0
5404 then (
5405 state.mode <-
5406 Birdseye (
5407 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5409 G.postRedisplay "birdseye pagedown";
5411 else gotoxy state.x (clamp (incr + conf.interpagespace*2));
5413 | l :: _ ->
5414 state.mode <-
5415 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5416 gotopage1 l.pageno 0;
5419 | [] -> gotoxy state.x (clamp state.winh)
5420 end;
5422 | @home ->
5423 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5424 gotopage1 0 0
5426 | @jend ->
5427 let pageno = state.pagecount - 1 in
5428 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5429 if not (pagevisible state.layout pageno)
5430 then
5431 let h =
5432 match List.rev state.pdims with
5433 | [] -> state.winh
5434 | (_, _, h, _) :: _ -> h
5436 gotoxy
5437 state.x
5438 (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5439 else G.postRedisplay "birdseye end";
5441 | _ -> viewkeyboard key mask
5444 let drawpage l =
5445 let color =
5446 match state.mode with
5447 | Textentry _ -> scalecolor 0.4
5448 | LinkNav _
5449 | View -> scalecolor 1.0
5450 | Birdseye (_, _, pageno, hooverpageno, _) ->
5451 if l.pageno = hooverpageno
5452 then scalecolor 0.9
5453 else (
5454 if l.pageno = pageno
5455 then (
5456 let c = scalecolor 1.0 in
5457 GlDraw.color c;
5458 GlDraw.line_width 3.0;
5459 let dispx = l.pagedispx in
5460 linerect
5461 (float (dispx-1)) (float (l.pagedispy-1))
5462 (float (dispx+l.pagevw+1))
5463 (float (l.pagedispy+l.pagevh+1))
5465 GlDraw.line_width 1.0;
5468 else scalecolor 0.8
5471 drawtiles l color;
5474 let postdrawpage l linkindexbase =
5475 match getopaque l.pageno with
5476 | Some opaque ->
5477 if tileready l l.pagex l.pagey
5478 then
5479 let x = l.pagedispx - l.pagex
5480 and y = l.pagedispy - l.pagey in
5481 let hlmask =
5482 match conf.columns with
5483 | Csingle _ | Cmulti _ ->
5484 (if conf.hlinks then 1 else 0)
5485 + (if state.glinks
5486 && not (isbirdseye state.mode) then 2 else 0)
5487 | Csplit _ -> 0
5489 let s =
5490 match state.mode with
5491 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5492 | Textentry _
5493 | Birdseye _
5494 | View
5495 | LinkNav _ -> E.s
5497 Hashtbl.find_all state.prects l.pageno |>
5498 List.iter (fun vals -> drawprect opaque x y vals);
5499 let n = postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize) in
5500 if n < 0
5501 then (state.redisplay <- true; 0)
5502 else n
5503 else 0
5504 | _ -> 0
5507 let scrollindicator () =
5508 let sbw, ph, sh = state.uioh#scrollph in
5509 let sbh, pw, sw = state.uioh#scrollpw in
5511 let x0,x1,hx0 =
5512 if conf.leftscroll
5513 then (0, sbw, sbw)
5514 else ((state.winw - sbw), state.winw, 0)
5517 Gl.enable `blend;
5518 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5519 GlDraw.color (0.64, 0.64, 0.64) ~alpha:0.7;
5520 filledrect (float x0) 0. (float x1) (float state.winh);
5521 filledrect
5522 (float hx0) (float (state.winh - sbh))
5523 (float (hx0 + state.winw)) (float state.winh)
5525 GlDraw.color (0.0, 0.0, 0.0) ~alpha:0.7;
5527 filledrect (float x0) ph (float x1) (ph +. sh);
5528 let pw = pw +. float hx0 in
5529 filledrect pw (float (state.winh - sbh)) (pw +. sw) (float state.winh);
5530 Gl.disable `blend;
5533 let showsel () =
5534 match state.mstate with
5535 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5538 | Msel ((x0, y0), (x1, y1)) ->
5539 let identify opaque l px py = Some (opaque, l.pageno, px, py) in
5540 let o0,n0,px0,py0 = onppundermouse identify x0 y0 (~< E.s, -1, 0, 0) in
5541 let _o1,n1,px1,py1 = onppundermouse identify x1 y1 (~< E.s, -1, 0, 0) in
5542 if n0 != -1 && n0 = n1 then seltext o0 (px0, py0, px1, py1);
5545 let showrects =
5546 function [] -> ()
5547 | rects ->
5548 Gl.enable `blend;
5549 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5550 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5551 List.iter
5552 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5553 List.iter (fun l ->
5554 if l.pageno = pageno
5555 then (
5556 let dx = float (l.pagedispx - l.pagex) in
5557 let dy = float (l.pagedispy - l.pagey) in
5558 let r, g, b, alpha = c in
5559 GlDraw.color (r, g, b) ~alpha;
5560 filledrect2 (x0+.dx) (y0+.dy)
5561 (x1+.dx) (y1+.dy)
5562 (x3+.dx) (y3+.dy)
5563 (x2+.dx) (y2+.dy);
5565 ) state.layout
5566 ) rects
5568 Gl.disable `blend;
5571 let display () =
5572 begin match conf.columns, state.layout with
5573 | Csingle _, _ :: _ ->
5574 GlDraw.color (scalecolor2 conf.bgcolor);
5575 let y =
5576 List.fold_left (fun y l ->
5577 let x0 = 0 in
5578 let y0 = y in
5579 let x1 = l.pagedispx in
5580 let y1 = (l.pagedispy + l.pagevh) in
5581 filledrect (float x0) (float y0) (float x1) (float y1);
5582 let x0 = x1 + l.pagevw in
5583 let x1 = state.winw in
5584 filledrect1 (float x0) (float y0) (float x1) (float y1);
5585 if y != l.pagedispy
5586 then (
5587 let x0 = 0
5588 and x1 = state.winw in
5589 let y0 = y
5590 and y1 = l.pagedispy in
5591 filledrect1 (float x0) (float y0) (float x1) (float y1);
5593 l.pagedispy + l.pagevh) 0 state.layout
5595 let x0 = 0
5596 and x1 = state.winw in
5597 let y0 = y
5598 and y1 = state.winh in
5599 filledrect1 (float x0) (float y0) (float x1) (float y1)
5600 | (Cmulti _ | Csplit _), _ | Csingle _, [] ->
5601 GlClear.color (scalecolor2 conf.bgcolor);
5602 GlClear.clear [`color];
5603 end;
5604 List.iter drawpage state.layout;
5605 let rects =
5606 match state.mode with
5607 | LinkNav (Ltexact (pageno, linkno)) ->
5608 begin match getopaque pageno with
5609 | Some opaque ->
5610 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5611 let color = (0.0, 0.0, 0.5, 0.5) in
5612 (pageno, color, (
5613 float x0, float y0,
5614 float x1, float y0,
5615 float x1, float y1,
5616 float x0, float y1)
5617 ) :: state.rects
5618 | None -> state.rects
5620 | LinkNav (Ltgendir _) | LinkNav (Ltnotready _)
5621 | Birdseye _
5622 | Textentry _
5623 | View -> state.rects
5625 showrects rects;
5626 let rec postloop linkindexbase = function
5627 | l :: rest ->
5628 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5629 postloop linkindexbase rest
5630 | [] -> ()
5632 showsel ();
5633 postloop 0 state.layout;
5634 state.uioh#display;
5635 begin match state.mstate with
5636 | Mzoomrect ((x0, y0), (x1, y1)) ->
5637 Gl.enable `blend;
5638 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5639 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5640 filledrect (float x0) (float y0) (float x1) (float y1);
5641 Gl.disable `blend;
5642 | Msel _
5643 | Mpan _
5644 | Mscrolly | Mscrollx
5645 | Mzoom _
5646 | Mnone -> ()
5647 end;
5648 enttext ();
5649 scrollindicator ();
5650 Wsi.swapb ();
5653 let zoomrect x y x1 y1 =
5654 let x0 = min x x1
5655 and x1 = max x x1
5656 and y0 = min y y1 in
5657 let zoom = (float state.w) /. float (x1 - x0) in
5658 let margin =
5659 let simple () =
5660 if state.w < state.winw
5661 then (state.winw - state.w) / 2
5662 else 0
5664 match conf.fitmodel with
5665 | FitWidth | FitProportional -> simple ()
5666 | FitPage ->
5667 match conf.columns with
5668 | Csplit _ ->
5669 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
5670 | Cmulti _ | Csingle _ -> simple ()
5672 gotoxy ((state.x + margin) - x0) (state.y + y0);
5673 state.anchor <- getanchor ();
5674 setzoom zoom;
5675 resetmstate ();
5678 let annot inline x y =
5679 match unproject x y with
5680 | Some (opaque, n, ux, uy) ->
5681 let add text =
5682 addannot opaque ux uy text;
5683 wcmd "freepage %s" (~> opaque);
5684 Hashtbl.remove state.pagemap (n, state.gen);
5685 flushtiles ();
5686 gotoxy state.x state.y
5688 if inline
5689 then
5690 let ondone s = add s in
5691 let mode = state.mode in
5692 state.mode <- Textentry (
5693 ("annotation: ", E.s, None, textentry, ondone, true),
5694 fun _ -> state.mode <- mode);
5695 state.text <- E.s;
5696 enttext ();
5697 G.postRedisplay "annot"
5698 else
5699 add @@ getusertext E.s
5700 | _ -> ()
5703 let zoomblock x y =
5704 let g opaque l px py =
5705 match rectofblock opaque px py with
5706 | Some a ->
5707 let x0 = a.(0) -. 20. in
5708 let x1 = a.(1) +. 20. in
5709 let y0 = a.(2) -. 20. in
5710 let zoom = (float state.w) /. (x1 -. x0) in
5711 let pagey = getpagey l.pageno in
5712 let margin = (state.w - l.pagew)/2 in
5713 let nx = -truncate x0 - margin in
5714 gotoxy_and_clear_text nx (pagey + truncate y0);
5715 state.anchor <- getanchor ();
5716 setzoom zoom;
5717 None
5718 | None -> None
5720 match conf.columns with
5721 | Csplit _ ->
5722 impmsg "block zooming does not work properly in split columns mode"
5723 | Cmulti _ | Csingle _ -> onppundermouse g x y ()
5726 let scrollx x =
5727 let winw = state.winw - 1 in
5728 let s = float x /. float winw in
5729 let destx = truncate (float (state.w + winw) *. s) in
5730 gotoxy_and_clear_text (winw - destx) state.y;
5731 state.mstate <- Mscrollx;
5734 let scrolly y =
5735 let s = float y /. float state.winh in
5736 let desty = truncate (float (state.maxy -
5737 (if conf.maxhfit then state.winh else 0))
5738 *. s) in
5739 gotoxy_and_clear_text state.x desty;
5740 state.mstate <- Mscrolly;
5743 let viewmulticlick clicks x y mask =
5744 let g opaque l px py =
5745 let mark =
5746 match clicks with
5747 | 2 -> Mark_word
5748 | 3 -> Mark_line
5749 | 4 -> Mark_block
5750 | _ -> Mark_page
5752 if markunder opaque px py mark
5753 then (
5754 Some (fun () ->
5755 let dopipe cmd =
5756 match getopaque l.pageno with
5757 | None -> ()
5758 | Some opaque -> pipesel opaque cmd
5760 state.roam <- (fun () -> dopipe conf.paxcmd);
5761 if not (Wsi.withctrl mask) then dopipe conf.selcmd;
5764 else None
5766 G.postRedisplay "viewmulticlick";
5767 onppundermouse g x y (fun () -> impmsg "nothing to select") ();
5770 let canselect () =
5771 match conf.columns with
5772 | Csplit _ -> false
5773 | Csingle _ | Cmulti _ -> conf.angle mod 360 = 0
5776 let viewmouse button down x y mask =
5777 match button with
5778 | n when (n == 4 || n == 5) && not down ->
5779 if Wsi.withctrl mask
5780 then (
5781 match state.mstate with
5782 | Mzoom (oldn, i, (ftx, fty)) ->
5783 let recenter =
5784 if false
5785 then abs (ftx - x) > 5 || abs (fty - y) > 5
5786 else false
5788 if oldn = n
5789 then (
5790 if i = 2
5791 then
5792 let incr =
5793 match n with
5794 | 5 ->
5795 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5796 | _ ->
5797 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5799 let zoom = conf.zoom -. incr in
5800 if recenter
5801 then pivotzoom ~x ~y zoom
5802 else pivotzoom zoom;
5803 state.mstate <- Mzoom (n, 0, (x, y));
5804 else
5805 state.mstate <- Mzoom (n, i+1, (ftx, fty));
5807 else state.mstate <- Mzoom (n, 0, (ftx, fty))
5809 | Msel _
5810 | Mpan _
5811 | Mscrolly | Mscrollx
5812 | Mzoomrect _
5813 | Mnone -> state.mstate <- Mzoom (n, 0, (0, 0))
5815 else (
5816 match state.autoscroll with
5817 | Some step -> setautoscrollspeed step (n=4)
5818 | None ->
5819 if conf.wheelbypage || conf.presentation
5820 then (
5821 if n = 4
5822 then prevpage ()
5823 else nextpage ()
5825 else
5826 let incr =
5827 if n = 4
5828 then -conf.scrollstep
5829 else conf.scrollstep
5831 let incr = incr * 2 in
5832 let y = clamp incr in
5833 gotoxy_and_clear_text state.x y
5836 | n when (n = 6 || n = 7) && not down && canpan () ->
5837 let x =
5838 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep) in
5839 gotoxy_and_clear_text x state.y
5841 | 1 when Wsi.withshift mask ->
5842 state.mstate <- Mnone;
5843 if not down
5844 then (
5845 match unproject x y with
5846 | None -> ()
5847 | Some (_, pageno, ux, uy) ->
5848 let cmd = Printf.sprintf
5849 "%s %s %d %d %d"
5850 conf.stcmd state.path pageno ux uy
5852 match spawn cmd [] with
5853 | (exception exn) ->
5854 impmsg "execution of synctex command(%S) failed: %S"
5855 conf.stcmd @@ exntos exn
5856 | _pid -> ()
5859 | 1 when Wsi.withctrl mask ->
5860 if down
5861 then (
5862 Wsi.setcursor Wsi.CURSOR_FLEUR;
5863 state.mstate <- Mpan (x, y)
5865 else
5866 state.mstate <- Mnone
5868 | 3 ->
5869 if down
5870 then (
5871 if Wsi.withshift mask
5872 then (
5873 annot conf.annotinline x y;
5874 G.postRedisplay "addannot"
5876 else
5877 let p = (x, y) in
5878 Wsi.setcursor Wsi.CURSOR_CYCLE;
5879 state.mstate <- Mzoomrect (p, p)
5881 else (
5882 match state.mstate with
5883 | Mzoomrect ((x0, y0), _) ->
5884 if abs (x-x0) > 10 && abs (y - y0) > 10
5885 then zoomrect x0 y0 x y
5886 else (
5887 resetmstate ();
5888 G.postRedisplay "kill accidental zoom rect";
5890 | Msel _
5891 | Mpan _
5892 | Mscrolly | Mscrollx
5893 | Mzoom _
5894 | Mnone ->
5895 resetmstate ()
5898 | 1 when vscrollhit x ->
5899 if down
5900 then
5901 let _, position, sh = state.uioh#scrollph in
5902 if y > truncate position && y < truncate (position +. sh)
5903 then state.mstate <- Mscrolly
5904 else scrolly y
5905 else
5906 state.mstate <- Mnone
5908 | 1 when y > state.winh - hscrollh () ->
5909 if down
5910 then
5911 let _, position, sw = state.uioh#scrollpw in
5912 if x > truncate position && x < truncate (position +. sw)
5913 then state.mstate <- Mscrollx
5914 else scrollx x
5915 else
5916 state.mstate <- Mnone
5918 | 1 when state.bzoom -> if not down then zoomblock x y
5920 | 1 ->
5921 let dest = if down then getunder x y else Unone in
5922 begin match dest with
5923 | Ulinkuri _ ->
5924 gotounder dest
5926 | Unone when down ->
5927 Wsi.setcursor Wsi.CURSOR_FLEUR;
5928 state.mstate <- Mpan (x, y);
5930 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
5932 | Unone | Utext _ ->
5933 if down
5934 then (
5935 if canselect ()
5936 then (
5937 state.mstate <- Msel ((x, y), (x, y));
5938 G.postRedisplay "mouse select";
5941 else (
5942 match state.mstate with
5943 | Mnone -> ()
5945 | Mzoom _ | Mscrollx | Mscrolly ->
5946 state.mstate <- Mnone
5948 | Mzoomrect ((x0, y0), _) ->
5949 zoomrect x0 y0 x y
5951 | Mpan _ ->
5952 Wsi.setcursor Wsi.CURSOR_INHERIT;
5953 state.mstate <- Mnone
5955 | Msel ((x0, y0), (x1, y1)) ->
5956 let rec loop = function
5957 | [] -> ()
5958 | l :: rest ->
5959 let inside =
5960 let a0 = l.pagedispy in
5961 let a1 = a0 + l.pagevh in
5962 let b0 = l.pagedispx in
5963 let b1 = b0 + l.pagevw in
5964 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5965 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5967 if inside
5968 then
5969 match getopaque l.pageno with
5970 | Some opaque ->
5971 let dosel cmd () =
5972 match Unix.pipe () with
5973 | (exception exn) ->
5974 impmsg "cannot create sel pipe: %s" @@
5975 exntos exn;
5976 | (r, w) ->
5977 let clo what fd =
5978 Ne.clo fd (fun msg ->
5979 dolog "%s close failed: %s" what msg)
5981 let pid =
5982 try spawn cmd [r, 0; w, -1]
5983 with exn ->
5984 dolog "cannot execute %S: %s"
5985 cmd @@ exntos exn;
5988 if pid > 0
5989 then (
5990 copysel w opaque;
5991 G.postRedisplay "copysel";
5993 else clo "Msel pipe/w" w;
5994 clo "Msel pipe/r" r;
5996 dosel conf.selcmd ();
5997 state.roam <- dosel conf.paxcmd;
5998 | None -> ()
5999 else loop rest
6001 loop state.layout;
6002 resetmstate ();
6006 | _ -> ()
6009 let birdseyemouse button down x y mask
6010 (conf, leftx, _, hooverpageno, anchor) =
6011 match button with
6012 | 1 when down ->
6013 let rec loop = function
6014 | [] -> ()
6015 | l :: rest ->
6016 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6017 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6018 then (
6019 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
6021 else loop rest
6023 loop state.layout
6024 | 3 -> ()
6025 | _ -> viewmouse button down x y mask
6028 let uioh = object
6029 method display = ()
6031 method key key mask =
6032 begin match state.mode with
6033 | Textentry textentry -> textentrykeyboard key mask textentry
6034 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6035 | View -> viewkeyboard key mask
6036 | LinkNav linknav -> linknavkeyboard key mask linknav
6037 end;
6038 state.uioh
6040 method button button bstate x y mask =
6041 begin match state.mode with
6042 | LinkNav _
6043 | View -> viewmouse button bstate x y mask
6044 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6045 | Textentry _ -> ()
6046 end;
6047 state.uioh
6049 method multiclick clicks x y mask =
6050 begin match state.mode with
6051 | LinkNav _
6052 | View -> viewmulticlick clicks x y mask
6053 | Birdseye _
6054 | Textentry _ -> ()
6055 end;
6056 state.uioh
6058 method motion x y =
6059 begin match state.mode with
6060 | Textentry _ -> ()
6061 | View | Birdseye _ | LinkNav _ ->
6062 match state.mstate with
6063 | Mzoom _ | Mnone -> ()
6065 | Mpan (x0, y0) ->
6066 let dx = x - x0
6067 and dy = y0 - y in
6068 state.mstate <- Mpan (x, y);
6069 let x = if canpan () then panbound (state.x + dx) else state.x in
6070 let y = clamp dy in
6071 gotoxy_and_clear_text x y
6073 | Msel (a, _) ->
6074 state.mstate <- Msel (a, (x, y));
6075 G.postRedisplay "motion select";
6077 | Mscrolly ->
6078 let y = min state.winh (max 0 y) in
6079 scrolly y
6081 | Mscrollx ->
6082 let x = min state.winw (max 0 x) in
6083 scrollx x
6085 | Mzoomrect (p0, _) ->
6086 state.mstate <- Mzoomrect (p0, (x, y));
6087 G.postRedisplay "motion zoomrect";
6088 end;
6089 state.uioh
6091 method pmotion x y =
6092 begin match state.mode with
6093 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6094 let rec loop = function
6095 | [] ->
6096 if hooverpageno != -1
6097 then (
6098 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6099 G.postRedisplay "pmotion birdseye no hoover";
6101 | l :: rest ->
6102 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6103 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6104 then (
6105 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6106 G.postRedisplay "pmotion birdseye hoover";
6108 else loop rest
6110 loop state.layout
6112 | Textentry _ -> ()
6114 | LinkNav _
6115 | View ->
6116 match state.mstate with
6117 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ -> ()
6118 | Mnone ->
6119 updateunder x y;
6120 if canselect ()
6121 then
6122 match conf.pax with
6123 | None -> ()
6124 | Some r ->
6125 let past, _, _ = !r in
6126 let now = now () in
6127 let delta = now -. past in
6128 if delta > 0.01
6129 then paxunder x y
6130 else r := (now, x, y)
6131 end;
6132 state.uioh
6134 method infochanged _ = ()
6136 method scrollph =
6137 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6138 let p, h =
6139 if maxy = 0
6140 then 0.0, float state.winh
6141 else scrollph state.y maxy
6143 vscrollw (), p, h
6145 method scrollpw =
6146 let fwinw = float (state.winw - vscrollw ()) in
6147 let sw =
6148 let sw = fwinw /. float state.w in
6149 let sw = fwinw *. sw in
6150 max sw (float conf.scrollh)
6152 let position =
6153 let maxx = state.w + state.winw in
6154 let x = state.winw - state.x in
6155 let percent = float x /. float maxx in
6156 (fwinw -. sw) *. percent
6158 hscrollh (), position, sw
6160 method modehash =
6161 let modename =
6162 match state.mode with
6163 | LinkNav _ -> "links"
6164 | Textentry _ -> "textentry"
6165 | Birdseye _ -> "birdseye"
6166 | View -> "view"
6168 findkeyhash conf modename
6170 method eformsgs = true
6171 method alwaysscrolly = false
6172 end;;
6174 let addrect pageno r g b a x0 y0 x1 y1 =
6175 Hashtbl.add state.prects pageno [|r; g; b; a; x0; y0; x1; y1|];
6178 let ract cmds =
6179 let cl = splitatchar cmds ' ' in
6180 let scan s fmt f =
6181 try Scanf.sscanf s fmt f
6182 with exn ->
6183 adderrfmt "remote exec"
6184 "error processing '%S': %s\n" cmds @@ exntos exn
6186 let rectx s pageno (r, g, b, a) x0 y0 x1 y1 =
6187 vlog "%s page %d color (%f %f %f %f) x0,y0,x1,y1 = %f %f %f %f"
6188 s pageno r g b a x0 y0 x1 y1;
6189 onpagerect
6190 pageno
6191 (fun w h ->
6192 let _,w1,h1,_ = getpagedim pageno in
6193 let sw = float w1 /. float w
6194 and sh = float h1 /. float h in
6195 let x0s = x0 *. sw
6196 and x1s = x1 *. sw
6197 and y0s = y0 *. sh
6198 and y1s = y1 *. sh in
6199 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
6200 let color = (r, g, b, a) in
6201 if conf.verbose then debugrect rect;
6202 state.rects <- (pageno, color, rect) :: state.rects;
6203 G.postRedisplay s;
6206 match cl with
6207 | "reload", "" -> reload ()
6208 | "goto", args ->
6209 scan args "%u %f %f"
6210 (fun pageno x y ->
6211 let cmd, _ = state.geomcmds in
6212 if emptystr cmd
6213 then gotopagexy !wtmode pageno x y
6214 else
6215 let f prevf () =
6216 gotopagexy !wtmode pageno x y;
6217 prevf ()
6219 state.reprf <- f state.reprf
6221 | "goto1", args -> scan args "%u %f" gotopage
6222 | "gotor", args ->
6223 scan args "%S %u"
6224 (fun _filename _pageno ->
6225 failwith "gotounder (Uremote (filename, pageno))")
6226 | "gotord", args ->
6227 scan args "%S %S"
6228 (fun _filename _dest ->
6229 failwith "gotounder (Uremotedest (filename, dest))")
6230 | "rect", args ->
6231 scan args "%u %u %f %f %f %f"
6232 (fun pageno c x0 y0 x1 y1 ->
6233 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
6234 rectx "rect" pageno color x0 y0 x1 y1;
6236 | "prect", args ->
6237 scan args "%u %f %f %f %f %f %f %f %f"
6238 (fun pageno r g b alpha x0 y0 x1 y1 ->
6239 addrect pageno r g b alpha x0 y0 x1 y1;
6240 G.postRedisplay "prect"
6242 | "pgoto", args ->
6243 scan args "%u %f %f"
6244 (fun pageno x y ->
6245 let optopaque =
6246 match getopaque pageno with
6247 | Some opaque -> opaque
6248 | None -> ~< E.s
6250 pgoto optopaque pageno x y;
6251 let rec fixx = function
6252 | [] -> ()
6253 | l :: rest ->
6254 if l.pageno = pageno
6255 then gotoxy (state.x - l.pagedispx) state.y
6256 else fixx rest
6258 let layout =
6259 let mult =
6260 match conf.columns with
6261 | Csingle _ | Csplit _ -> 1
6262 | Cmulti ((n, _, _), _) -> n
6264 layout 0 state.y (state.winw * mult) state.winh
6266 fixx layout
6268 | "activatewin", "" -> Wsi.activatewin ()
6269 | "quit", "" -> raise Quit
6270 | "keys", keys ->
6271 begin try
6272 let l = Config.keys_of_string keys in
6273 List.iter (fun (k, m) -> keyboard k m) l
6274 with exn ->
6275 adderrfmt "error processing keys" "`%S': %s\n" cmds @@ exntos exn
6277 | "clearrects", "" ->
6278 Hashtbl.clear state.prects;
6279 G.postRedisplay "clearrects"
6280 | _ ->
6281 adderrfmt "remote command"
6282 "error processing remote command: %S\n" cmds;
6285 let remote =
6286 let scratch = Bytes.create 80 in
6287 let buf = Buffer.create 80 in
6288 fun fd ->
6289 match tempfailureretry (Unix.read fd scratch 0) 80 with
6290 | (exception Unix.Unix_error (Unix.EAGAIN, _, _)) -> None
6291 | 0 ->
6292 Unix.close fd;
6293 if Buffer.length buf > 0
6294 then (
6295 let s = Buffer.contents buf in
6296 Buffer.clear buf;
6297 ract s;
6299 None
6300 | n ->
6301 let rec eat ppos =
6302 let nlpos =
6303 match Bytes.index_from scratch ppos '\n' with
6304 | pos -> if pos >= n then -1 else pos
6305 | (exception Not_found) -> -1
6307 if nlpos >= 0
6308 then (
6309 Buffer.add_subbytes buf scratch ppos (nlpos-ppos);
6310 let s = Buffer.contents buf in
6311 Buffer.clear buf;
6312 ract s;
6313 eat (nlpos+1);
6315 else (
6316 Buffer.add_subbytes buf scratch ppos (n-ppos);
6317 Some fd
6319 in eat 0
6322 let remoteopen path =
6323 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
6324 with exn ->
6325 adderrfmt "remoteopen" "error opening %S: %s" path @@ exntos exn;
6326 None
6329 let () =
6330 let gcconfig = ref E.s in
6331 let trimcachepath = ref E.s in
6332 let rcmdpath = ref E.s in
6333 let pageno = ref None in
6334 let rootwid = ref 0 in
6335 let openlast = ref false in
6336 let nofc = ref false in
6337 let doreap = ref false in
6338 let csspath = ref None in
6339 selfexec := Sys.executable_name;
6340 Arg.parse
6341 (Arg.align
6342 [("-p", Arg.String (fun s -> state.password <- s),
6343 "<password> Set password");
6345 ("-f", Arg.String
6346 (fun s ->
6347 Config.fontpath := s;
6348 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
6350 "<path> Set path to the user interface font");
6352 ("-c", Arg.String
6353 (fun s ->
6354 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
6355 Config.confpath := s),
6356 "<path> Set path to the configuration file");
6358 ("-last", Arg.Set openlast, " Open last document");
6360 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
6361 "<page-number> Jump to page");
6363 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6364 "<path> Set path to the trim cache file");
6366 ("-dest", Arg.String (fun s -> state.nameddest <- s),
6367 "<named-destination> Set named destination");
6369 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
6370 ("-cxack", Arg.Set cxack, " Cut corners");
6372 ("-remote", Arg.String (fun s -> rcmdpath := s),
6373 "<path> Set path to the remote commands source");
6375 ("-origin", Arg.String (fun s -> state.origin <- s),
6376 "<original-path> Set original path");
6378 ("-gc", Arg.Set_string gcconfig,
6379 "<script-path> Collect garbage with the help of a script");
6381 ("-nofc", Arg.Set nofc, " Do not use fontconfig");
6383 ("-v", Arg.Unit (fun () ->
6384 Printf.printf
6385 "%s\nconfiguration path: %s\n"
6386 (version ())
6387 Config.defconfpath;
6388 exit 0), " Print version and exit");
6390 ("-css", Arg.String (fun s -> csspath := Some s),
6391 "<css-path> Style sheet to use for EPUB/HTML");
6393 ("-embed", Arg.Set_int rootwid,
6394 "<window-id> Embed into window")
6397 (fun s -> state.path <- s)
6398 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:");
6400 if !wtmode
6401 then selfexec := !selfexec ^ " -wtmode";
6403 let histmode = emptystr state.path && not !openlast in
6405 if not (Config.load !openlast)
6406 then dolog "failed to load configuration";
6408 begin match !pageno with
6409 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
6410 | None -> ()
6411 end;
6413 if nonemptystr !gcconfig
6414 then (
6415 let (c, s) =
6416 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6417 | (exception exn) -> error "socketpair for gc failed: %s" @@ exntos exn
6418 | fds -> fds
6420 match spawn !gcconfig [(c, 0); (c, 1); (s, -1)] with
6421 | (exception exn) -> error "failed to execute gc script: %s" @@ exntos exn
6422 | _pid ->
6423 Ne.clo c @@ (fun s -> error "failed to close gc fd %s" s);
6424 Config.gc s;
6425 exit 0
6428 let mu =
6429 object (self)
6430 val mutable m_clicks = 0
6431 val mutable m_click_x = 0
6432 val mutable m_click_y = 0
6433 val mutable m_lastclicktime = infinity
6435 method private cleanup =
6436 state.roam <- noroam;
6437 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap
6438 method expose = G.postRedisplay "expose"
6439 method visible v =
6440 let name =
6441 match v with
6442 | Wsi.Unobscured -> "unobscured"
6443 | Wsi.PartiallyObscured -> "partiallyobscured"
6444 | Wsi.FullyObscured -> "fullyobscured"
6446 vlog "visibility change %s" name
6447 method display = display ()
6448 method map mapped = vlog "mapped %b" mapped
6449 method reshape w h =
6450 self#cleanup;
6451 reshape w h
6452 method mouse b d x y m =
6453 if d && canselect ()
6454 then (
6456 * http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx
6458 m_click_x <- x;
6459 m_click_y <- y;
6460 if b = 1
6461 then (
6462 let t = now () in
6463 if abs x - m_click_x > 10
6464 || abs y - m_click_y > 10
6465 || abs_float (t -. m_lastclicktime) > 0.3
6466 then m_clicks <- 0;
6467 m_clicks <- m_clicks + 1;
6468 m_lastclicktime <- t;
6469 if m_clicks = 1
6470 then (
6471 self#cleanup;
6472 G.postRedisplay "cleanup";
6473 state.uioh <- state.uioh#button b d x y m;
6475 else state.uioh <- state.uioh#multiclick m_clicks x y m
6477 else (
6478 self#cleanup;
6479 m_clicks <- 0;
6480 m_lastclicktime <- infinity;
6481 state.uioh <- state.uioh#button b d x y m
6484 else (
6485 state.uioh <- state.uioh#button b d x y m
6487 method motion x y =
6488 state.mpos <- (x, y);
6489 state.uioh <- state.uioh#motion x y
6490 method pmotion x y =
6491 state.mpos <- (x, y);
6492 state.uioh <- state.uioh#pmotion x y
6493 method key k m =
6494 let mascm = m land (
6495 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6496 ) in
6497 let keyboard k m =
6498 let x = state.x and y = state.y in
6499 keyboard k m;
6500 if x != state.x || y != state.y then self#cleanup
6502 match state.keystate with
6503 | KSnone ->
6504 let km = k, mascm in
6505 begin
6506 match
6507 let modehash = state.uioh#modehash in
6508 try Hashtbl.find modehash km
6509 with Not_found ->
6510 try Hashtbl.find (findkeyhash conf "global") km
6511 with Not_found -> KMinsrt (k, m)
6512 with
6513 | KMinsrt (k, m) -> keyboard k m
6514 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6515 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6517 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6518 List.iter (fun (k, m) -> keyboard k m) insrt;
6519 state.keystate <- KSnone
6520 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6521 state.keystate <- KSinto (keys, insrt)
6522 | KSinto _ -> state.keystate <- KSnone
6524 method enter x y =
6525 state.mpos <- (x, y);
6526 state.uioh <- state.uioh#pmotion x y
6527 method leave = state.mpos <- (-1, -1)
6528 method winstate wsl = state.winstate <- wsl
6529 method quit = raise Quit
6532 let wsfd, winw, winh = Wsi.init mu !rootwid conf.cwinw conf.cwinh platform in
6534 setbgcol conf.bgcolor;
6535 state.wsfd <- wsfd;
6537 if not @@ List.exists GlMisc.check_extension
6538 [ "GL_ARB_texture_rectangle"
6539 ; "GL_EXT_texture_recangle"
6540 ; "GL_NV_texture_rectangle" ]
6541 then (dolog "OpenGL does not suppport rectangular textures"; exit 1);
6543 if substratis (GlMisc.get_string `renderer) 0 "Mesa DRI Intel("
6544 then (
6545 defconf.sliceheight <- 1024;
6546 defconf.texcount <- 32;
6547 defconf.usepbo <- true;
6550 let cs, ss =
6551 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6552 | (exception exn) ->
6553 dolog "socketpair failed: %s" @@ exntos exn;
6554 exit 1
6555 | (r, w) ->
6556 cloexec r;
6557 cloexec w;
6558 r, w
6561 setcheckers conf.checkers;
6563 opengl_has_pbo := GlMisc.check_extension "GL_ARB_pixel_buffer_object";
6565 begin match !csspath with
6566 | None -> ()
6567 | Some "" -> conf.css <- E.s
6568 | Some path ->
6569 let css = filecontents path in
6570 let l = String.length css in
6571 conf.css <-
6572 if substratis css (l-2) "\r\n"
6573 then String.sub css 0 (l-2)
6574 else (if css.[l-1] = '\n'
6575 then String.sub css 0 (l-1)
6576 else css);
6577 end;
6578 init cs (
6579 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
6580 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6581 !Config.fontpath, !trimcachepath, !opengl_has_pbo, not !nofc
6583 List.iter GlArray.enable [`texture_coord; `vertex];
6584 state.ss <- ss;
6585 reshape ~firsttime:true winw winh;
6586 state.uioh <- uioh;
6587 if histmode
6588 then (
6589 Wsi.settitle "llpp (history)";
6590 enterhistmode ();
6592 else (
6593 state.text <- "Opening " ^ (mbtoutf8 state.path);
6594 opendoc state.path state.password;
6596 display ();
6597 Wsi.mapwin ();
6598 Wsi.setcursor Wsi.CURSOR_INHERIT;
6599 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
6601 let rec reap () =
6602 match Unix.waitpid [Unix.WNOHANG] ~-1 with
6603 | (exception (Unix.Unix_error (Unix.ECHILD, _, _))) -> ()
6604 | (exception exn) -> dolog "Unix.waitpid: %s" @@ exntos exn
6605 | 0, _ -> ()
6606 | _pid, _status -> reap ()
6608 Sys.set_signal Sys.sigchld (Sys.Signal_handle (fun _ -> doreap := true));
6610 let optrfd =
6611 ref (
6612 if nonemptystr !rcmdpath
6613 then remoteopen !rcmdpath
6614 else None
6618 let rec loop deadline =
6619 if !doreap
6620 then (
6621 doreap := false;
6622 reap ()
6624 let r = [state.ss; state.wsfd] in
6625 let r =
6626 match !optrfd with
6627 | None -> r
6628 | Some fd -> fd :: r
6630 if state.redisplay
6631 then (
6632 state.redisplay <- false;
6633 display ();
6635 let timeout =
6636 let now = now () in
6637 if deadline > now
6638 then (
6639 if deadline = infinity
6640 then ~-.1.0
6641 else max 0.0 (deadline -. now)
6643 else 0.0
6645 let r, _, _ =
6646 try Unix.select r [] [] timeout
6647 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6649 begin match r with
6650 | [] ->
6651 state.ghyll None;
6652 let newdeadline =
6653 if state.ghyll == noghyll
6654 then
6655 match state.autoscroll with
6656 | Some step when step != 0 ->
6657 let y = state.y + step in
6658 let fy = if conf.maxhfit then state.winh else 0 in
6659 let y =
6660 if y < 0
6661 then state.maxy - fy
6662 else if y >= state.maxy - fy then 0 else y
6664 if state.mode = View
6665 then gotoxy_and_clear_text state.x y
6666 else gotoxy state.x y;
6667 deadline +. 0.01
6668 | _ -> infinity
6669 else deadline +. 0.01
6671 loop newdeadline
6673 | l ->
6674 let rec checkfds = function
6675 | [] -> ()
6676 | fd :: rest when fd = state.ss ->
6677 let cmd = rcmd state.ss in
6678 act cmd;
6679 checkfds rest
6681 | fd :: rest when fd = state.wsfd ->
6682 Wsi.readresp fd;
6683 checkfds rest
6685 | fd :: rest when Some fd = !optrfd ->
6686 begin match remote fd with
6687 | None -> optrfd := remoteopen !rcmdpath;
6688 | opt -> optrfd := opt
6689 end;
6690 checkfds rest
6692 | _ :: rest ->
6693 dolog "select returned unknown descriptor";
6694 checkfds rest
6696 checkfds l;
6697 let newdeadline =
6698 let deadline1 =
6699 if deadline = infinity
6700 then now () +. 0.01
6701 else deadline
6703 match state.autoscroll with
6704 | Some step when step != 0 -> deadline1
6705 | _ -> if state.ghyll == noghyll then infinity else deadline1
6707 loop newdeadline
6708 end;
6710 match loop infinity with
6711 | exception Quit ->
6712 Config.save leavebirdseye;
6713 if hasunsavedchanges ()
6714 then save ()
6715 | _ -> error "umpossible - infinity reached"