Add an option to disable document style
[llpp.git] / main.ml
blobfe2253d701b70be77cbb2b52071a5c179361dcf4
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
1126 let reload () =
1127 state.anchor <- getanchor ();
1128 opendoc state.path state.password;
1131 let scalecolor c =
1132 let c = c *. conf.colorscale in
1133 (c, c, c);
1136 let scalecolor2 (r, g, b) =
1137 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1140 let docolumns columns =
1141 match columns with
1142 | Csingle _ ->
1143 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1144 let rec loop pageno pdimno pdim y ph pdims =
1145 if pageno = state.pagecount
1146 then ()
1147 else
1148 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1149 match pdims with
1150 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1151 pdimno+1, pdim, rest
1152 | _ ->
1153 pdimno, pdim, pdims
1155 let x = max 0 (((state.winw - w) / 2) - xoff) in
1156 let y =
1157 y + (if conf.presentation
1158 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1159 else (if pageno = 0 then 0 else conf.interpagespace)
1162 a.(pageno) <- (pdimno, x, y, pdim);
1163 loop (pageno+1) pdimno pdim (y + h) h pdims
1165 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1166 conf.columns <- Csingle a;
1168 | Cmulti ((columns, coverA, coverB), _) ->
1169 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1170 let rec loop pageno pdimno pdim x y rowh pdims =
1171 let rec fixrow m =
1172 if m = pageno then () else
1173 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1174 if h < rowh
1175 then (
1176 let y = y + (rowh - h) / 2 in
1177 a.(m) <- (pdimno, x, y, pdim);
1179 fixrow (m+1)
1181 if pageno = state.pagecount
1182 then fixrow (((pageno - 1) / columns) * columns)
1183 else
1184 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1185 match pdims with
1186 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1187 pdimno+1, pdim, rest
1188 | _ ->
1189 pdimno, pdim, pdims
1191 let x, y, rowh' =
1192 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1193 then (
1194 let x = (state.winw - w) / 2 in
1195 let ips =
1196 if conf.presentation then calcips h else conf.interpagespace in
1197 x, y + ips + rowh, h
1199 else (
1200 if (pageno - coverA) mod columns = 0
1201 then (
1202 let x = max 0 (state.winw - state.w) / 2 in
1203 let y =
1204 if conf.presentation
1205 then
1206 let ips = calcips h in
1207 y + (if pageno = 0 then 0 else calcips rowh + ips)
1208 else
1209 y + (if pageno = 0 then 0 else conf.interpagespace)
1211 x, y + rowh, h
1213 else x, y, max rowh h
1216 let y =
1217 if pageno > 1 && (pageno - coverA) mod columns = 0
1218 then (
1219 let y =
1220 if pageno = columns && conf.presentation
1221 then (
1222 let ips = calcips rowh in
1223 for i = 0 to pred columns
1225 let (pdimno, x, y, pdim) = a.(i) in
1226 a.(i) <- (pdimno, x, y+ips, pdim)
1227 done;
1228 y+ips;
1230 else y
1232 fixrow (pageno - columns);
1235 else y
1237 a.(pageno) <- (pdimno, x, y, pdim);
1238 let x = x + w + xoff*2 + conf.interpagespace in
1239 loop (pageno+1) pdimno pdim x y rowh' pdims
1241 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1242 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1244 | Csplit (c, _) ->
1245 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1246 let rec loop pageno pdimno pdim y pdims =
1247 if pageno = state.pagecount
1248 then ()
1249 else
1250 let pdimno, ((_, w, h, _) as pdim), pdims =
1251 match pdims with
1252 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1253 pdimno+1, pdim, rest
1254 | _ ->
1255 pdimno, pdim, pdims
1257 let cw = w / c in
1258 let rec loop1 n x y =
1259 if n = c then y else (
1260 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1261 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1264 let y = loop1 0 0 y in
1265 loop (pageno+1) pdimno pdim y pdims
1267 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1268 conf.columns <- Csplit (c, a);
1271 let represent () =
1272 docolumns conf.columns;
1273 state.maxy <- calcheight ();
1274 if state.reprf == noreprf
1275 then (
1276 match state.mode with
1277 | Birdseye (_, _, pageno, _, _) ->
1278 let y, h = getpageyh pageno in
1279 let top = (state.winh - h) / 2 in
1280 gotoxy state.x (max 0 (y - top))
1281 | Textentry _
1282 | View
1283 | LinkNav _ ->
1284 let y = getanchory state.anchor in
1285 let y = min y (state.maxy - state.winh) in
1286 gotoxy state.x y;
1288 else (
1289 state.reprf ();
1290 state.reprf <- noreprf;
1294 let reshape ?(firsttime=false) w h =
1295 GlDraw.viewport ~x:0 ~y:0 ~w ~h;
1296 if not firsttime && nogeomcmds state.geomcmds
1297 then state.anchor <- getanchor ();
1299 state.winw <- w;
1300 let w = truncate (float w *. conf.zoom) in
1301 let w = max w 2 in
1302 state.winh <- h;
1303 setfontsize fstate.fontsize;
1304 GlMat.mode `modelview;
1305 GlMat.load_identity ();
1307 GlMat.mode `projection;
1308 GlMat.load_identity ();
1309 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1310 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1311 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
1313 let relx =
1314 if conf.zoom <= 1.0
1315 then 0.0
1316 else float state.x /. float state.w
1318 invalidate "geometry"
1319 (fun () ->
1320 state.w <- w;
1321 if not firsttime
1322 then state.x <- truncate (relx *. float w);
1323 let w =
1324 match conf.columns with
1325 | Csingle _ -> w
1326 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1327 | Csplit (c, _) -> w * c
1329 wcmd "geometry %d %d %d"
1330 w (stateh h) (FMTE.to_int conf.fitmodel)
1334 let enttext () =
1335 let len = String.length state.text in
1336 let x0 = if conf.leftscroll then vscrollw () else 0 in
1337 let drawstring s =
1338 let hscrollh =
1339 match state.mode with
1340 | Textentry _ | View | LinkNav _ ->
1341 let h, _, _ = state.uioh#scrollpw in
1343 | Birdseye _ -> 0
1345 let rect x w =
1346 filledrect x (float (state.winh - (fstate.fontsize + 4) - hscrollh))
1347 (x+.w) (float (state.winh - hscrollh))
1350 let w = float (state.winw - 1 - vscrollw ()) in
1351 if state.progress >= 0.0 && state.progress < 1.0
1352 then (
1353 GlDraw.color (0.3, 0.3, 0.3);
1354 let w1 = w *. state.progress in
1355 rect (float x0) w1;
1356 GlDraw.color (0.0, 0.0, 0.0);
1357 rect (float x0+.w1) (float x0+.w-.w1)
1359 else (
1360 GlDraw.color (0.0, 0.0, 0.0);
1361 rect (float x0) w;
1364 GlDraw.color (1.0, 1.0, 1.0);
1365 drawstring
1366 fstate.fontsize
1367 (if conf.leftscroll then x0 + 2 else x0 + if len > 0 then 8 else 2)
1368 (state.winh - hscrollh - 5) s;
1370 let s =
1371 match state.mode with
1372 | Textentry ((prefix, text, _, _, _, _), _) ->
1373 let s =
1374 if len > 0
1375 then
1376 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1377 else
1378 Printf.sprintf "%s%s_" prefix text
1382 | Birdseye _
1383 | View
1384 | LinkNav _ -> state.text
1386 let s =
1387 if state.newerrmsgs
1388 then (
1389 if not (istextentry state.mode) && state.uioh#eformsgs
1390 then
1391 let s1 = "(press 'e' to review error messasges)" in
1392 if nonemptystr s then s ^ " " ^ s1 else s1
1393 else s
1395 else s
1397 if nonemptystr s
1398 then drawstring s
1401 let gctiles () =
1402 let len = Queue.length state.tilelru in
1403 let layout = lazy (
1404 match state.throttle with
1405 | None ->
1406 if conf.preload
1407 then preloadlayout state.x state.y state.winw state.winh
1408 else state.layout
1409 | Some (layout, _, _) ->
1410 layout
1411 ) in
1412 let rec loop qpos =
1413 if state.memused <= conf.memlimit
1414 then ()
1415 else (
1416 if qpos < len
1417 then
1418 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1419 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1420 let (_, pw, ph, _) = getpagedim n in
1421 if gen = state.gen
1422 && colorspace = conf.colorspace
1423 && angle = conf.angle
1424 && pagew = pw
1425 && pageh = ph
1426 && (
1427 let x = col*conf.tilew
1428 and y = row*conf.tileh in
1429 tilevisible (Lazy.force_val layout) n x y
1431 then Queue.push lruitem state.tilelru
1432 else (
1433 freepbo p;
1434 wcmd "freetile %s" (~> p);
1435 state.memused <- state.memused - s;
1436 state.uioh#infochanged Memused;
1437 Hashtbl.remove state.tilemap k;
1439 loop (qpos+1)
1442 loop 0
1445 let onpagerect pageno f =
1446 let b =
1447 match conf.columns with
1448 | Cmulti (_, b) -> b
1449 | Csingle b -> b
1450 | Csplit (_, b) -> b
1452 if pageno >= 0 && pageno < Array.length b
1453 then
1454 let (_, _, _, (_, w, h, _)) = b.(pageno) in
1455 f w h
1458 let gotopagexy1 wtmode pageno x y =
1459 let _,w1,h1,leftx = getpagedim pageno in
1460 let top = y /. (float h1) in
1461 let left = x /. (float w1) in
1462 let py, w, h = getpageywh pageno in
1463 let wh = state.winh in
1464 let x = left *. (float w) in
1465 let x = leftx + state.x + truncate x in
1466 let sx =
1467 if x < 0 || x >= state.winw
1468 then state.x - x
1469 else state.x
1471 let pdy = truncate (top *. float h) in
1472 let y' = py + pdy in
1473 let dy = y' - state.y in
1474 let sy =
1475 if x != state.x || not (dy > 0 && dy < wh)
1476 then (
1477 if conf.presentation
1478 then
1479 if abs (py - y') > wh
1480 then y'
1481 else py
1482 else y';
1484 else state.y
1486 if state.x != sx || state.y != sy
1487 then (
1488 let x, y =
1489 if wtmode
1490 then (
1491 let ww = state.winw in
1492 let qx = sx / ww
1493 and qy = pdy / wh in
1494 let x = qx * ww
1495 and y = py + qy * wh in
1496 let x = if -x + ww > w1 then -(w1-ww) else x
1497 and y' = if y + wh > state.maxy then state.maxy - wh else y in
1498 let y =
1499 if conf.presentation
1500 then
1501 if abs (py - y') > wh
1502 then y'
1503 else py
1504 else y';
1506 (x, y)
1508 else (sx, sy)
1510 gotoxy_and_clear_text x y;
1512 else gotoxy_and_clear_text state.x state.y;
1515 let gotopagexy wtmode pageno x y =
1516 match state.mode with
1517 | Birdseye _ -> gotopage pageno 0.0
1518 | Textentry _
1519 | View
1520 | LinkNav _ -> gotopagexy1 wtmode pageno x y
1523 let getpassword () =
1524 let passcmd = getenvwithdef "LLPP_ASKPASS" conf.passcmd in
1525 if emptystr passcmd
1526 then E.s
1527 else getcmdoutput
1528 (fun s ->
1529 impmsg "error getting password: %s" s;
1530 dolog "%s" s) passcmd;
1533 let pgoto opaque pageno x y =
1534 let pdimno = getpdimno pageno in
1535 let x, y = project opaque pageno pdimno x y in
1536 gotopagexy false pageno x y;
1539 let act cmds =
1540 (* dolog "%S" cmds; *)
1541 let spl = splitatchar cmds ' ' in
1542 let scan s fmt f =
1543 try Scanf.sscanf s fmt f
1544 with exn ->
1545 dolog "error processing '%S': %s" cmds @@ exntos exn;
1546 exit 1
1548 let addoutline outline =
1549 match state.currently with
1550 | Outlining outlines ->
1551 state.currently <- Outlining (outline :: outlines)
1552 | Idle -> state.currently <- Outlining [outline]
1553 | Loading _
1554 | Tiling _ ->
1555 dolog "invalid outlining state";
1556 logcurrently state.currently
1558 match spl with
1559 | "clear", "" ->
1560 state.pdims <- [];
1561 state.uioh#infochanged Pdim;
1563 | "clearrects", "" ->
1564 state.rects <- state.rects1;
1565 G.postRedisplay "clearrects";
1567 | "continue", args ->
1568 let n = scan args "%u" (fun n -> n) in
1569 state.pagecount <- n;
1570 begin match state.currently with
1571 | Outlining l ->
1572 state.currently <- Idle;
1573 state.outlines <- Array.of_list (List.rev l)
1574 | Idle
1575 | Loading _
1576 | Tiling _ -> ()
1577 end;
1579 let cur, cmds = state.geomcmds in
1580 if emptystr cur
1581 then failwith "umpossible";
1583 begin match List.rev cmds with
1584 | [] ->
1585 state.geomcmds <- E.s, [];
1586 state.throttle <- None;
1587 represent ();
1588 | (s, f) :: rest ->
1589 f ();
1590 state.geomcmds <- s, List.rev rest;
1591 end;
1592 if conf.maxwait = None && not !wtmode
1593 then G.postRedisplay "continue";
1595 | "msg", args ->
1596 showtext ' ' args
1598 | "vmsg", args ->
1599 if conf.verbose
1600 then showtext ' ' args
1602 | "emsg", args ->
1603 Buffer.add_string state.errmsgs args;
1604 state.newerrmsgs <- true;
1605 G.postRedisplay "error message"
1607 | "progress", args ->
1608 let progress, text =
1609 scan args "%f %n"
1610 (fun f pos ->
1611 f, String.sub args pos (String.length args - pos))
1613 state.text <- text;
1614 state.progress <- progress;
1615 G.postRedisplay "progress"
1617 | "firstmatch", args ->
1618 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1619 scan args "%u %d %f %f %f %f %f %f %f %f"
1620 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1621 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1623 let y = (getpagey pageno) + truncate y0 in
1624 let x =
1625 if conf.zoom > 1.0
1626 then state.winw/2
1627 else state.x
1629 addnav ();
1630 gotoxy x y;
1631 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1632 state.rects1 <- [pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)]
1634 | "match", args ->
1635 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1636 scan args "%u %d %f %f %f %f %f %f %f %f"
1637 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1638 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1640 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1641 state.rects1 <-
1642 (pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1644 | "page", args ->
1645 let pageopaques, t = scan args "%s %f" (fun p t -> p, t) in
1646 let pageopaque = ~< pageopaques in
1647 begin match state.currently with
1648 | Loading (l, gen) ->
1649 vlog "page %d took %f sec" l.pageno t;
1650 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1651 begin match state.throttle with
1652 | None ->
1653 let preloadedpages =
1654 if conf.preload
1655 then preloadlayout state.x state.y state.winw state.winh
1656 else state.layout
1658 let evict () =
1659 let set =
1660 List.fold_left (fun s l -> IntSet.add l.pageno s)
1661 IntSet.empty preloadedpages
1663 let evictedpages =
1664 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1665 if not (IntSet.mem pageno set)
1666 then (
1667 wcmd "freepage %s" (~> opaque);
1668 key :: accu
1670 else accu
1671 ) state.pagemap []
1673 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1675 evict ();
1676 state.currently <- Idle;
1677 if gen = state.gen
1678 then (
1679 tilepage l.pageno pageopaque state.layout;
1680 load state.layout;
1681 load preloadedpages;
1682 let visible = pagevisible state.layout l.pageno in
1683 if visible
1684 then (
1685 match state.mode with
1686 | LinkNav (Ltnotready (pageno, dir)) ->
1687 if pageno = l.pageno
1688 then (
1689 let link =
1690 let ld =
1691 if dir = 0
1692 then LDfirstvisible (l.pagex, l.pagey, dir)
1693 else (
1694 if dir > 0 then LDfirst else LDlast
1697 findlink pageopaque ld
1699 match link with
1700 | Lnotfound -> ()
1701 | Lfound n ->
1702 showlinktype (getlink pageopaque n);
1703 state.mode <- LinkNav (Ltexact (l.pageno, n))
1705 | LinkNav (Ltgendir _)
1706 | LinkNav (Ltexact _)
1707 | View
1708 | Birdseye _
1709 | Textentry _ -> ()
1712 if visible && layoutready state.layout
1713 then (
1714 G.postRedisplay "page";
1718 | Some (layout, _, _) ->
1719 state.currently <- Idle;
1720 tilepage l.pageno pageopaque layout;
1721 load state.layout
1722 end;
1724 | Idle
1725 | Tiling _
1726 | Outlining _ ->
1727 dolog "Inconsistent loading state";
1728 logcurrently state.currently;
1729 exit 1
1732 | "tile" , args ->
1733 let (x, y, opaques, size, t) =
1734 scan args "%u %u %s %u %f"
1735 (fun x y p size t -> (x, y, p, size, t))
1737 let opaque = ~< opaques in
1738 begin match state.currently with
1739 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1740 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1742 unmappbo opaque;
1743 if tilew != conf.tilew || tileh != conf.tileh
1744 then (
1745 wcmd "freetile %s" (~> opaque);
1746 state.currently <- Idle;
1747 load state.layout;
1749 else (
1750 puttileopaque l col row gen cs angle opaque size t;
1751 state.memused <- state.memused + size;
1752 state.uioh#infochanged Memused;
1753 gctiles ();
1754 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1755 opaque, size) state.tilelru;
1757 let layout =
1758 match state.throttle with
1759 | None -> state.layout
1760 | Some (layout, _, _) -> layout
1763 state.currently <- Idle;
1764 if gen = state.gen
1765 && conf.colorspace = cs
1766 && conf.angle = angle
1767 && tilevisible layout l.pageno x y
1768 then conttiling l.pageno pageopaque;
1770 begin match state.throttle with
1771 | None ->
1772 preload state.layout;
1773 if gen = state.gen
1774 && conf.colorspace = cs
1775 && conf.angle = angle
1776 && tilevisible state.layout l.pageno x y
1777 && (not !wtmode || layoutready state.layout)
1778 then G.postRedisplay "tile nothrottle";
1780 | Some (layout, y, _) ->
1781 let ready = layoutready layout in
1782 if ready
1783 then (
1784 state.y <- y;
1785 state.layout <- layout;
1786 state.throttle <- None;
1787 G.postRedisplay "throttle";
1789 else load layout;
1790 end;
1793 | Idle
1794 | Loading _
1795 | Outlining _ ->
1796 dolog "Inconsistent tiling state";
1797 logcurrently state.currently;
1798 exit 1
1801 | "pdim", args ->
1802 let (n, w, h, _) as pdim =
1803 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1805 let pdim =
1806 match conf.fitmodel with
1807 | FitWidth -> pdim
1808 | FitPage | FitProportional ->
1809 match conf.columns with
1810 | Csplit _ -> (n, w, h, 0)
1811 | Csingle _ | Cmulti _ -> pdim
1813 state.pdims <- pdim :: state.pdims;
1814 state.uioh#infochanged Pdim
1816 | "o", args ->
1817 let (l, n, t, h, pos) =
1818 scan args "%u %u %d %u %n"
1819 (fun l n t h pos -> l, n, t, h, pos)
1821 let s = String.sub args pos (String.length args - pos) in
1822 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
1824 | "ou", args ->
1825 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
1826 let s = String.sub args pos len in
1827 let pos2 = pos + len + 1 in
1828 let uri = String.sub args pos2 (String.length args - pos2) in
1829 addoutline (s, l, Ouri uri)
1831 | "on", args ->
1832 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
1833 let s = String.sub args pos (String.length args - pos) in
1834 addoutline (s, l, Onone)
1836 | "a", args ->
1837 let (n, l, t) =
1838 scan args "%u %d %d" (fun n l t -> n, l, t)
1840 state.reprf <- (fun () -> gotopagexy !wtmode n (float l) (float t))
1842 | "info", args ->
1843 let pos = nindex args '\t' in
1844 let s =
1845 if pos >= 0
1846 then
1847 if substratis args 0 "Title"
1848 then
1849 let s = String.sub args (pos+1) @@ String.length args - pos - 1 in
1850 conf.title <- s;
1851 Wsi.settitle s;
1852 args
1853 else
1854 if substratis args 0 "CreationDate"
1855 then (
1856 if String.length args >= pos + 7
1857 && args.[pos+1] = 'D' && args.[pos+2] = ':'
1858 then
1859 let b = Buffer.create 18 in
1860 Buffer.add_string b "CreationDate\t";
1861 let sub p l c =
1863 Buffer.add_substring b args (pos+p+1) l;
1864 Buffer.add_char b c;
1865 with exn -> Buffer.add_string b @@ exntos exn
1867 sub 2 4 '/';
1868 sub 6 2 '/';
1869 sub 8 2 ' ';
1870 sub 10 2 ':';
1871 sub 12 2 ':';
1872 sub 14 2 ' ';
1873 Buffer.add_char b '[';
1874 Buffer.add_substring b args (pos+1)
1875 (String.length args - pos - 1);
1876 Buffer.add_char b ']';
1877 Buffer.contents b
1878 else args
1880 else args
1881 else args
1883 state.docinfo <- (1, s) :: state.docinfo
1885 | "infoend", "" ->
1886 state.docinfo <- List.rev state.docinfo;
1887 state.uioh#infochanged Docinfo
1889 | "pass", args ->
1890 if args = "fail"
1891 then Wsi.settitle "Wrong password";
1892 let password = getpassword () in
1893 if emptystr password
1894 then error "document is password protected"
1895 else opendoc state.path password
1897 | _ ->
1898 error "unknown cmd `%S'" cmds
1901 let onhist cb =
1902 let rc = cb.rc in
1903 let action = function
1904 | HCprev -> cbget cb ~-1
1905 | HCnext -> cbget cb 1
1906 | HCfirst -> cbget cb ~-(cb.rc)
1907 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1908 and cancel () = cb.rc <- rc
1909 in (action, cancel)
1912 let search pattern forward =
1913 match conf.columns with
1914 | Csplit _ -> impmsg "searching does not work properly in split columns mode"
1915 | Csingle _
1916 | Cmulti _ ->
1917 if nonemptystr pattern
1918 then
1919 let pn, py =
1920 match state.layout with
1921 | [] -> 0, 0
1922 | l :: _ ->
1923 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1925 wcmd "search %d %d %d %d,%s\000"
1926 (btod conf.icase) pn py (btod forward) pattern;
1929 let intentry text key =
1930 let text =
1931 if key >= 32 && key < 127
1932 then
1933 let c = Char.chr key in
1934 match c with
1935 | '0' .. '9' -> addchar text c
1936 | _ ->
1937 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1938 text
1939 else (
1940 state.text <- Printf.sprintf "invalid key (%d)" key;
1941 text
1944 TEcont text
1947 let linknact f s =
1948 if nonemptystr s
1949 then (
1950 let n =
1951 let l = String.length s in
1952 let rec loop pos n =
1953 if pos = l
1954 then n
1955 else
1956 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
1957 loop (pos+1) (n*26 + m)
1958 in loop 0 0
1960 let rec loop n = function
1961 | [] -> ()
1962 | l :: rest ->
1963 match getopaque l.pageno with
1964 | None -> loop n rest
1965 | Some opaque ->
1966 let m = getlinkcount opaque in
1967 if n < m
1968 then (
1969 let under = getlink opaque n in
1970 f under
1972 else loop (n-m) rest
1974 loop n state.layout;
1978 let linknentry text key =
1979 if key >= 32 && key < 127
1980 then
1981 let text = addchar text (Char.chr key) in
1982 linknact (fun under -> state.text <- undertext under) text;
1983 TEcont text
1984 else (
1985 state.text <- Printf.sprintf "invalid key %d" key;
1986 TEcont text
1990 let textentry text key =
1991 if Wsi.isspecialkey key
1992 then TEcont text
1993 else TEcont (text ^ toutf8 key)
1996 let reqlayout angle fitmodel =
1997 match state.throttle with
1998 | None ->
1999 if nogeomcmds state.geomcmds
2000 then state.anchor <- getanchor ();
2001 conf.angle <- angle mod 360;
2002 if conf.angle != 0
2003 then (
2004 match state.mode with
2005 | LinkNav _ -> state.mode <- View
2006 | Birdseye _
2007 | Textentry _
2008 | View -> ()
2010 conf.fitmodel <- fitmodel;
2011 invalidate
2012 "reqlayout"
2013 (fun () ->
2014 wcmd "reqlayout %d %d %d"
2015 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
2017 | _ -> ()
2020 let settrim trimmargins trimfuzz =
2021 if nogeomcmds state.geomcmds
2022 then state.anchor <- getanchor ();
2023 conf.trimmargins <- trimmargins;
2024 conf.trimfuzz <- trimfuzz;
2025 let x0, y0, x1, y1 = trimfuzz in
2026 invalidate
2027 "settrim" (fun () ->
2028 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2029 flushpages ();
2032 let setzoom zoom =
2033 match state.throttle with
2034 | None ->
2035 let zoom = max 0.0001 zoom in
2036 if zoom <> conf.zoom
2037 then (
2038 state.prevzoom <- (conf.zoom, state.x);
2039 conf.zoom <- zoom;
2040 reshape state.winw state.winh;
2041 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2044 | Some (layout, y, started) ->
2045 let time =
2046 match conf.maxwait with
2047 | None -> 0.0
2048 | Some t -> t
2050 let dt = now () -. started in
2051 if dt > time
2052 then (
2053 state.y <- y;
2054 load layout;
2058 let pivotzoom ?(vw=min state.w state.winw)
2059 ?(vh=min (state.maxy-state.y) state.winh)
2060 ?(x=vw/2) ?(y=vh/2) zoom =
2061 let w = float state.w /. zoom in
2062 let hw = w /. 2.0 in
2063 let ratio = float vh /. float vw in
2064 let hh = hw *. ratio in
2065 let x0 = if zoom < 1.0 then 0.0 else float x -. hw in
2066 let y0 = float y -. hh in
2067 gotoxy (state.x - truncate x0) (state.y + truncate y0);
2068 setzoom zoom;
2071 let pivotzoom ?vw ?vh ?x ?y zoom =
2072 if nogeomcmds state.geomcmds then pivotzoom ?vw ?vh ?x ?y zoom
2075 let setcolumns mode columns coverA coverB =
2076 state.prevcolumns <- Some (conf.columns, conf.zoom);
2077 if columns < 0
2078 then (
2079 if isbirdseye mode
2080 then impmsg "split mode doesn't work in bird's eye"
2081 else (
2082 conf.columns <- Csplit (-columns, E.a);
2083 state.x <- 0;
2084 conf.zoom <- 1.0;
2087 else (
2088 if columns < 2
2089 then (
2090 conf.columns <- Csingle E.a;
2091 state.x <- 0;
2092 setzoom 1.0;
2094 else (
2095 conf.columns <- Cmulti ((columns, coverA, coverB), E.a);
2096 conf.zoom <- 1.0;
2099 reshape state.winw state.winh;
2102 let resetmstate () =
2103 state.mstate <- Mnone;
2104 Wsi.setcursor Wsi.CURSOR_INHERIT;
2107 let enterbirdseye () =
2108 let zoom = float conf.thumbw /. float state.winw in
2109 let birdseyepageno =
2110 let cy = state.winh / 2 in
2111 let fold = function
2112 | [] -> 0
2113 | l :: rest ->
2114 let rec fold best = function
2115 | [] -> best.pageno
2116 | l :: rest ->
2117 let d = cy - (l.pagedispy + l.pagevh/2)
2118 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2119 if abs d < abs dbest
2120 then fold l rest
2121 else best.pageno
2122 in fold l rest
2124 fold state.layout
2126 state.mode <-
2127 Birdseye (
2128 { conf with zoom = conf.zoom },
2129 state.x, birdseyepageno, -1, getanchor ()
2131 resetmstate ();
2132 conf.zoom <- zoom;
2133 conf.presentation <- false;
2134 conf.interpagespace <- 10;
2135 conf.hlinks <- false;
2136 conf.fitmodel <- FitPage;
2137 state.x <- 0;
2138 conf.maxwait <- None;
2139 conf.columns <- (
2140 match conf.beyecolumns with
2141 | Some c ->
2142 conf.zoom <- 1.0;
2143 Cmulti ((c, 0, 0), E.a)
2144 | None -> Csingle E.a
2146 if conf.verbose
2147 then
2148 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2149 (100.0*.zoom)
2150 else
2151 state.text <- E.s
2153 reshape state.winw state.winh;
2156 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2157 state.mode <- View;
2158 conf.zoom <- c.zoom;
2159 conf.presentation <- c.presentation;
2160 conf.interpagespace <- c.interpagespace;
2161 conf.maxwait <- c.maxwait;
2162 conf.hlinks <- c.hlinks;
2163 conf.fitmodel <- c.fitmodel;
2164 conf.beyecolumns <- (
2165 match conf.columns with
2166 | Cmulti ((c, _, _), _) -> Some c
2167 | Csingle _ -> None
2168 | Csplit _ -> failwith "leaving bird's eye split mode"
2170 conf.columns <- (
2171 match c.columns with
2172 | Cmulti (c, _) -> Cmulti (c, E.a)
2173 | Csingle _ -> Csingle E.a
2174 | Csplit (c, _) -> Csplit (c, E.a)
2176 if conf.verbose
2177 then
2178 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2179 (100.0*.conf.zoom)
2181 reshape state.winw state.winh;
2182 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2183 state.x <- leftx;
2186 let togglebirdseye () =
2187 match state.mode with
2188 | Birdseye vals -> leavebirdseye vals true
2189 | View -> enterbirdseye ()
2190 | Textentry _ | LinkNav _ -> ()
2193 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2194 let pageno = max 0 (pageno - incr) in
2195 let rec loop = function
2196 | [] -> gotopage1 pageno 0
2197 | l :: _ when l.pageno = pageno ->
2198 if l.pagedispy >= 0 && l.pagey = 0
2199 then G.postRedisplay "upbirdseye"
2200 else gotopage1 pageno 0
2201 | _ :: rest -> loop rest
2203 loop state.layout;
2204 state.text <- E.s;
2205 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2208 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2209 let pageno = min (state.pagecount - 1) (pageno + incr) in
2210 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2211 let rec loop = function
2212 | [] ->
2213 let y, h = getpageyh pageno in
2214 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2215 gotoxy state.x (clamp dy)
2216 | l :: _ when l.pageno = pageno ->
2217 if l.pagevh != l.pageh
2218 then gotoxy state.x (clamp (l.pageh - l.pagevh + conf.interpagespace))
2219 else G.postRedisplay "downbirdseye"
2220 | _ :: rest -> loop rest
2222 loop state.layout;
2223 state.text <- E.s;
2226 let optentry mode _ key =
2227 let btos b = if b then "on" else "off" in
2228 if key >= 32 && key < 127
2229 then
2230 let c = Char.chr key in
2231 match c with
2232 | 's' ->
2233 let ondone s =
2234 try conf.scrollstep <- int_of_string s with exn ->
2235 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2237 TEswitch ("scroll step: ", E.s, None, intentry, ondone, true)
2239 | 'A' ->
2240 let ondone s =
2242 conf.autoscrollstep <- boundastep state.winh (int_of_string s);
2243 if state.autoscroll <> None
2244 then state.autoscroll <- Some conf.autoscrollstep
2245 with exn ->
2246 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2248 TEswitch ("auto scroll step: ", E.s, None, intentry, ondone, true)
2250 | 'C' ->
2251 let ondone s =
2253 let n, a, b = multicolumns_of_string s in
2254 setcolumns mode n a b;
2255 with exn ->
2256 state.text <- Printf.sprintf "bad columns `%s': %s" s @@ exntos exn
2258 TEswitch ("columns: ", E.s, None, textentry, ondone, true)
2260 | 'Z' ->
2261 let ondone s =
2263 let zoom = float (int_of_string s) /. 100.0 in
2264 pivotzoom zoom
2265 with exn ->
2266 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2268 TEswitch ("zoom: ", E.s, None, intentry, ondone, true)
2270 | 't' ->
2271 let ondone s =
2273 conf.thumbw <- bound (int_of_string s) 2 4096;
2274 state.text <-
2275 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2276 begin match mode with
2277 | Birdseye beye ->
2278 leavebirdseye beye false;
2279 enterbirdseye ();
2280 | Textentry _
2281 | View
2282 | LinkNav _ -> ();
2284 with exn ->
2285 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2287 TEswitch ("thumbnail width: ", E.s, None, intentry, ondone, true)
2289 | 'R' ->
2290 let ondone s =
2291 match int_of_string s with
2292 | angle -> reqlayout angle conf.fitmodel
2293 | exception exn ->
2294 state.text <-
2295 Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2297 TEswitch ("rotation: ", E.s, None, intentry, ondone, true)
2299 | 'i' ->
2300 conf.icase <- not conf.icase;
2301 TEdone ("case insensitive search " ^ (btos conf.icase))
2303 | 'p' ->
2304 conf.preload <- not conf.preload;
2305 gotoxy state.x state.y;
2306 TEdone ("preload " ^ (btos conf.preload))
2308 | 'v' ->
2309 conf.verbose <- not conf.verbose;
2310 TEdone ("verbose " ^ (btos conf.verbose))
2312 | 'd' ->
2313 conf.debug <- not conf.debug;
2314 TEdone ("debug " ^ (btos conf.debug))
2316 | 'h' ->
2317 conf.maxhfit <- not conf.maxhfit;
2318 state.maxy <- calcheight ();
2319 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2321 | 'c' ->
2322 conf.crophack <- not conf.crophack;
2323 TEdone ("crophack " ^ btos conf.crophack)
2325 | 'a' ->
2326 let s =
2327 match conf.maxwait with
2328 | None ->
2329 conf.maxwait <- Some infinity;
2330 "always wait for page to complete"
2331 | Some _ ->
2332 conf.maxwait <- None;
2333 "show placeholder if page is not ready"
2335 TEdone s
2337 | 'f' ->
2338 conf.underinfo <- not conf.underinfo;
2339 TEdone ("underinfo " ^ btos conf.underinfo)
2341 | 'P' ->
2342 conf.savebmarks <- not conf.savebmarks;
2343 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2345 | 'S' ->
2346 let ondone s =
2348 let pageno, py =
2349 match state.layout with
2350 | [] -> 0, 0
2351 | l :: _ ->
2352 l.pageno, l.pagey
2354 conf.interpagespace <- int_of_string s;
2355 docolumns conf.columns;
2356 state.maxy <- calcheight ();
2357 let y = getpagey pageno in
2358 gotoxy state.x (y + py)
2359 with exn ->
2360 state.text <-
2361 Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2363 TEswitch ("vertical margin: ", E.s, None, intentry, ondone, true)
2365 | 'l' ->
2366 let fm =
2367 match conf.fitmodel with
2368 | FitProportional -> FitWidth
2369 | FitWidth | FitPage -> FitProportional
2371 reqlayout conf.angle fm;
2372 TEdone ("proportional display " ^ btos (fm == FitProportional))
2374 | 'T' ->
2375 settrim (not conf.trimmargins) conf.trimfuzz;
2376 TEdone ("trim margins " ^ btos conf.trimmargins)
2378 | 'I' ->
2379 conf.invert <- not conf.invert;
2380 TEdone ("invert colors " ^ btos conf.invert)
2382 | 'x' ->
2383 let ondone s =
2384 cbput state.hists.sel s;
2385 conf.selcmd <- s;
2387 TEswitch ("selection command: ", E.s, Some (onhist state.hists.sel),
2388 textentry, ondone, true)
2390 | 'M' ->
2391 if conf.pax == None
2392 then conf.pax <- Some (ref (0.0, 0, 0))
2393 else conf.pax <- None;
2394 TEdone ("PAX " ^ btos (conf.pax != None))
2396 | _ ->
2397 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2398 TEstop
2399 else
2400 TEcont state.text
2403 class type lvsource =
2404 object
2405 method getitemcount : int
2406 method getitem : int -> (string * int)
2407 method hasaction : int -> bool
2408 method exit :
2409 uioh:uioh ->
2410 cancel:bool ->
2411 active:int ->
2412 first:int ->
2413 pan:int ->
2414 uioh option
2415 method getactive : int
2416 method getfirst : int
2417 method getpan : int
2418 method getminfo : (int * int) array
2419 end;;
2421 class virtual lvsourcebase = object
2422 val mutable m_active = 0
2423 val mutable m_first = 0
2424 val mutable m_pan = 0
2425 method getactive = m_active
2426 method getfirst = m_first
2427 method getpan = m_pan
2428 method getminfo : (int * int) array = E.a
2429 end;;
2431 let textentrykeyboard
2432 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2433 state.text <- E.s;
2434 let key = Wsi.keypadtodigitkey key in
2435 let enttext te =
2436 state.mode <- Textentry (te, onleave);
2437 enttext ();
2438 G.postRedisplay "textentrykeyboard enttext";
2440 let histaction cmd =
2441 match opthist with
2442 | None -> ()
2443 | Some (action, _) ->
2444 state.mode <-
2445 Textentry (
2446 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
2448 G.postRedisplay "textentry histaction"
2450 match key with
2451 | @backspace ->
2452 if emptystr text && cancelonempty
2453 then (
2454 onleave Cancel;
2455 G.postRedisplay "textentrykeyboard after cancel";
2457 else
2458 let s = withoutlastutf8 text in
2459 enttext (c, s, opthist, onkey, ondone, cancelonempty)
2461 | @enter | @kpenter ->
2462 ondone text;
2463 onleave Confirm;
2464 G.postRedisplay "textentrykeyboard after confirm"
2466 | @up | @kpup -> histaction HCprev
2467 | @down | @kpdown -> histaction HCnext
2468 | @home | @kphome -> histaction HCfirst
2469 | @jend | @kpend -> histaction HClast
2471 | @escape ->
2472 if emptystr text
2473 then (
2474 begin match opthist with
2475 | None -> ()
2476 | Some (_, onhistcancel) -> onhistcancel ()
2477 end;
2478 onleave Cancel;
2479 state.text <- E.s;
2480 G.postRedisplay "textentrykeyboard after cancel2"
2482 else (
2483 enttext (c, E.s, opthist, onkey, ondone, cancelonempty)
2486 | @delete | @kpdelete -> ()
2488 | _ when key != 0 && not (Wsi.isspecialkey key) ->
2489 begin match onkey text key with
2490 | TEdone text ->
2491 ondone text;
2492 onleave Confirm;
2493 G.postRedisplay "textentrykeyboard after confirm2";
2495 | TEcont text ->
2496 enttext (c, text, opthist, onkey, ondone, cancelonempty);
2498 | TEstop ->
2499 onleave Cancel;
2500 G.postRedisplay "textentrykeyboard after cancel3"
2502 | TEswitch te ->
2503 state.mode <- Textentry (te, onleave);
2504 G.postRedisplay "textentrykeyboard switch";
2505 end;
2507 | _ ->
2508 vlog "unhandled key %s" (Wsi.keyname key)
2511 let firstof first active =
2512 if first > active || abs (first - active) > fstate.maxrows - 1
2513 then max 0 (active - (fstate.maxrows/2))
2514 else first
2517 let calcfirst first active =
2518 if active > first
2519 then
2520 let rows = active - first in
2521 if rows > fstate.maxrows then active - fstate.maxrows else first
2522 else active
2525 let scrollph y maxy =
2526 let sh = float (maxy + state.winh) /. float state.winh in
2527 let sh = float state.winh /. sh in
2528 let sh = max sh (float conf.scrollh) in
2530 let percent = float y /. float maxy in
2531 let position = (float state.winh -. sh) *. percent in
2533 let position =
2534 if position +. sh > float state.winh
2535 then float state.winh -. sh
2536 else position
2538 position, sh;
2541 let adderrmsg src msg =
2542 Buffer.add_string state.errmsgs msg;
2543 state.newerrmsgs <- true;
2544 G.postRedisplay src
2547 let adderrfmt src fmt =
2548 Format.ksprintf (fun s -> adderrmsg src s) fmt;
2551 let coe s = (s :> uioh);;
2553 class listview ~zebra ~helpmode ~(source:lvsource) ~trusted ~modehash =
2554 object (self)
2555 val m_pan = source#getpan
2556 val m_first = source#getfirst
2557 val m_active = source#getactive
2558 val m_qsearch = E.s
2559 val m_prev_uioh = state.uioh
2561 method private elemunder y =
2562 if y < 0
2563 then None
2564 else
2565 let n = y / (fstate.fontsize+1) in
2566 if m_first + n < source#getitemcount
2567 then (
2568 if source#hasaction (m_first + n)
2569 then Some (m_first + n)
2570 else None
2572 else None
2574 method display =
2575 Gl.enable `blend;
2576 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
2577 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2578 filledrect 0. 0. (float state.winw) (float state.winh);
2579 GlDraw.color (1., 1., 1.);
2580 Gl.enable `texture_2d;
2581 let fs = fstate.fontsize in
2582 let nfs = fs + 1 in
2583 let hw = state.winw/3 in
2584 let ww = fstate.wwidth in
2585 let tabw = 17.0*.ww in
2586 let itemcount = source#getitemcount in
2587 let minfo = source#getminfo in
2588 if conf.leftscroll
2589 then (
2590 GlMat.push ();
2591 GlMat.translate ~x:(float conf.scrollbw) ();
2593 let x0 = 0.0 and x1 = float (state.winw - conf.scrollbw - 1) in
2594 let rec loop row =
2595 if (row - m_first) > fstate.maxrows
2596 then ()
2597 else (
2598 if row >= 0 && row < itemcount
2599 then (
2600 let (s, level) = source#getitem row in
2601 let y = (row - m_first) * nfs in
2602 let x = 5.0 +. (float (level + m_pan)) *. ww in
2603 if helpmode
2604 then GlDraw.color
2605 (let c = if row land 1 = 0 then 1.0 else 0.92 in (c,c,c));
2607 if row = m_active
2608 then (
2609 Gl.disable `texture_2d;
2610 let alpha = if source#hasaction row then 0.9 else 0.3 in
2611 GlDraw.color (1., 1., 1.) ~alpha;
2612 linerect (x0 +. 1.) (float (y + 1)) (x1) (float (y + fs + 3));
2613 Gl.enable `texture_2d;
2615 let c =
2616 if zebra && row land 1 = 1
2617 then 0.8
2618 else 1.0
2620 GlDraw.color (c,c,c);
2621 let drawtabularstring s =
2622 let drawstr x s =
2623 let x' = truncate (x0 +. x) in
2624 let pos = nindex s '\000' in
2625 if pos = -1
2626 then drawstring1 fs x' (y+nfs) s
2627 else
2628 let s1 = String.sub s 0 pos
2629 and s2 = String.sub s (pos+1) (String.length s - pos - 1) in
2630 let rec e s =
2631 if emptystr s
2632 then s
2633 else
2634 let s' = withoutlastutf8 s in
2635 let s = s' ^ "@Uellipsis" in
2636 let w = measurestr fs s in
2637 if float x' +. w +. ww < float (hw + x')
2638 then s
2639 else e s'
2641 let s1 =
2642 if float x' +. ww +. measurestr fs s1 > float (hw + x')
2643 then e s1
2644 else s1
2646 ignore (drawstring1 fs x' (y+nfs) s1);
2647 drawstring1 fs (hw + x') (y+nfs) s2
2649 if trusted
2650 then
2651 let x = if helpmode && row > 0 then x +. ww else x in
2652 let tabpos = nindex s '\t' in
2653 if tabpos > 0
2654 then
2655 let len = String.length s - tabpos - 1 in
2656 let s1 = String.sub s 0 tabpos
2657 and s2 = String.sub s (tabpos + 1) len in
2658 let nx = drawstr x s1 in
2659 let sw = nx -. x in
2660 let x = x +. (max tabw sw) in
2661 drawstr x s2
2662 else
2663 let len = String.length s - 2 in
2664 if len > 0 && s.[0] = '\xc2' && s.[1] = '\xb7'
2665 then
2666 let s = String.sub s 2 len in
2667 let x = if not helpmode then x +. ww else x in
2668 GlDraw.color (1.2, 1.2, 1.2);
2669 let vinc = drawstring1 (fs+fs/4)
2670 (truncate (x -. ww)) (y+nfs) s in
2671 GlDraw.color (1., 1., 1.);
2672 vinc +. (float fs *. 0.8)
2673 else
2674 drawstr x s
2675 else
2676 drawstr x s
2678 ignore (drawtabularstring s);
2679 loop (row+1)
2683 loop m_first;
2684 GlDraw.color (1.0, 1.0, 1.0) ~alpha:0.5;
2685 let xadj = 5.0 in
2686 let rec loop row =
2687 if (row - m_first) > fstate.maxrows
2688 then ()
2689 else (
2690 if row >= 0 && row < itemcount
2691 then (
2692 let (s, level) = source#getitem row in
2693 let pos0 = nindex s '\000' in
2694 let y = (row - m_first) * nfs in
2695 let x = float (level + m_pan) *. ww in
2696 let (first, last) = minfo.(row) in
2697 let prefix =
2698 if pos0 > 0 && first > pos0
2699 then String.sub s (pos0+1) (first-pos0-1)
2700 else String.sub s 0 first
2702 let suffix = String.sub s first (last - first) in
2703 let w1 = measurestr fstate.fontsize prefix in
2704 let w2 = measurestr fstate.fontsize suffix in
2705 let x = x +. if conf.leftscroll then xadj else 5.0 in
2706 let x = if pos0 > 0 && first > pos0 then x +. float hw else x in
2707 let x0 = x +. w1
2708 and y0 = float (y+2) in
2709 let x1 = x0 +. w2
2710 and y1 = float (y+fs+3) in
2711 filledrect x0 y0 x1 y1;
2712 loop (row+1)
2716 Gl.disable `texture_2d;
2717 if Array.length minfo > 0 then loop m_first;
2718 Gl.disable `blend;
2719 if conf.leftscroll
2720 then GlMat.pop ();
2722 method updownlevel incr =
2723 let len = source#getitemcount in
2724 let curlevel =
2725 if m_active >= 0 && m_active < len
2726 then snd (source#getitem m_active)
2727 else -1
2729 let rec flow i =
2730 if i = len then i-1 else if i = -1 then 0 else
2731 let _, l = source#getitem i in
2732 if l != curlevel then i else flow (i+incr)
2734 let active = flow m_active in
2735 let first = calcfirst m_first active in
2736 G.postRedisplay "outline updownlevel";
2737 {< m_active = active; m_first = first >}
2739 method private key1 key mask =
2740 let set1 active first qsearch =
2741 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2743 let search active pattern incr =
2744 let active = if active = -1 then m_first else active in
2745 let dosearch re =
2746 let rec loop n =
2747 if n >= 0 && n < source#getitemcount
2748 then (
2749 let s, _ = source#getitem n in
2750 match Str.search_forward re s 0 with
2751 | (exception Not_found) -> loop (n + incr)
2752 | _ -> Some n
2754 else None
2756 loop active
2758 let qpat = Str.quote pattern in
2759 match Str.regexp_case_fold qpat with
2760 | s -> dosearch s
2761 | exception exn ->
2762 adderrfmt "listview key1" "regexp_case_fold for `%S' failed: %S\n"
2763 qpat @@ Printexc.to_string exn;
2764 None
2766 let itemcount = source#getitemcount in
2767 let find start incr =
2768 let rec find i =
2769 if i = -1 || i = itemcount
2770 then -1
2771 else (
2772 if source#hasaction i
2773 then i
2774 else find (i + incr)
2777 find start
2779 let set active first =
2780 let first = bound first 0 (itemcount - fstate.maxrows) in
2781 state.text <- E.s;
2782 coe {< m_active = active; m_first = first; m_qsearch = E.s >}
2784 let navigate incr =
2785 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2786 let active, first =
2787 let incr1 = if incr > 0 then 1 else -1 in
2788 if isvisible m_first m_active
2789 then
2790 let next =
2791 let next = m_active + incr in
2792 let next =
2793 if next < 0 || next >= itemcount
2794 then -1
2795 else find next incr1
2797 if abs (m_active - next) > fstate.maxrows
2798 then -1
2799 else next
2801 if next = -1
2802 then
2803 let first = m_first + incr in
2804 let first = bound first 0 (itemcount - fstate.maxrows) in
2805 let next =
2806 let next = m_active + incr in
2807 let next = bound next 0 (itemcount - 1) in
2808 find next ~-incr1
2810 let active =
2811 if next = -1
2812 then m_active
2813 else (
2814 if isvisible first next
2815 then next
2816 else m_active
2819 active, first
2820 else
2821 let first = min next m_first in
2822 let first =
2823 if abs (next - first) > fstate.maxrows
2824 then first + incr
2825 else first
2827 next, first
2828 else
2829 let first = m_first + incr in
2830 let first = bound first 0 (itemcount - 1) in
2831 let active =
2832 let next = m_active + incr in
2833 let next = bound next 0 (itemcount - 1) in
2834 let next = find next incr1 in
2835 let active =
2836 if next = -1 || abs (m_active - first) > fstate.maxrows
2837 then (
2838 let active = if m_active = -1 then next else m_active in
2839 active
2841 else next
2843 if isvisible first active
2844 then active
2845 else -1
2847 active, first
2849 G.postRedisplay "listview navigate";
2850 set active first;
2852 match key with
2853 | (@r|@s) when Wsi.withctrl mask ->
2854 let incr = if key = @r then -1 else 1 in
2855 let active, first =
2856 match search (m_active + incr) m_qsearch incr with
2857 | None ->
2858 state.text <- m_qsearch ^ " [not found]";
2859 m_active, m_first
2860 | Some active ->
2861 state.text <- m_qsearch;
2862 active, firstof m_first active
2864 G.postRedisplay "listview ctrl-r/s";
2865 set1 active first m_qsearch;
2867 | @insert when Wsi.withctrl mask ->
2868 if m_active >= 0 && m_active < source#getitemcount
2869 then (
2870 let s, _ = source#getitem m_active in
2871 selstring s;
2873 coe self
2875 | @backspace ->
2876 if emptystr m_qsearch
2877 then coe self
2878 else (
2879 let qsearch = withoutlastutf8 m_qsearch in
2880 if emptystr qsearch
2881 then (
2882 state.text <- E.s;
2883 G.postRedisplay "listview empty qsearch";
2884 set1 m_active m_first E.s;
2886 else
2887 let active, first =
2888 match search m_active qsearch ~-1 with
2889 | None ->
2890 state.text <- qsearch ^ " [not found]";
2891 m_active, m_first
2892 | Some active ->
2893 state.text <- qsearch;
2894 active, firstof m_first active
2896 G.postRedisplay "listview backspace qsearch";
2897 set1 active first qsearch
2900 | key when (key != 0 && not (Wsi.isspecialkey key)) ->
2901 let pattern = m_qsearch ^ toutf8 key in
2902 let active, first =
2903 match search m_active pattern 1 with
2904 | None ->
2905 state.text <- pattern ^ " [not found]";
2906 m_active, m_first
2907 | Some active ->
2908 state.text <- pattern;
2909 active, firstof m_first active
2911 G.postRedisplay "listview qsearch add";
2912 set1 active first pattern;
2914 | @escape ->
2915 state.text <- E.s;
2916 if emptystr m_qsearch
2917 then (
2918 G.postRedisplay "list view escape";
2919 let mx, my = state.mpos in
2920 updateunder mx my;
2921 begin
2922 match
2923 source#exit ~uioh:(coe self)
2924 ~cancel:true ~active:m_active ~first:m_first ~pan:m_pan
2925 with
2926 | None -> m_prev_uioh
2927 | Some uioh -> uioh
2930 else (
2931 G.postRedisplay "list view kill qsearch";
2932 coe {< m_qsearch = E.s >}
2935 | @enter | @kpenter ->
2936 state.text <- E.s;
2937 let self = {< m_qsearch = E.s >} in
2938 let opt =
2939 G.postRedisplay "listview enter";
2940 if m_active >= 0 && m_active < source#getitemcount
2941 then (
2942 source#exit ~uioh:(coe self) ~cancel:false
2943 ~active:m_active ~first:m_first ~pan:m_pan;
2945 else (
2946 source#exit ~uioh:(coe self) ~cancel:true
2947 ~active:m_active ~first:m_first ~pan:m_pan;
2950 begin match opt with
2951 | None -> m_prev_uioh
2952 | Some uioh -> uioh
2955 | @delete | @kpdelete ->
2956 coe self
2958 | @up | @kpup -> navigate ~-1
2959 | @down | @kpdown -> navigate 1
2960 | @prior | @kpprior -> navigate ~-(fstate.maxrows)
2961 | @next | @kpnext -> navigate fstate.maxrows
2963 | @right | @kpright ->
2964 state.text <- E.s;
2965 G.postRedisplay "listview right";
2966 coe {< m_pan = m_pan - 1 >}
2968 | @left | @kpleft ->
2969 state.text <- E.s;
2970 G.postRedisplay "listview left";
2971 coe {< m_pan = m_pan + 1 >}
2973 | @home | @kphome ->
2974 let active = find 0 1 in
2975 G.postRedisplay "listview home";
2976 set active 0;
2978 | @jend | @kpend ->
2979 let first = max 0 (itemcount - fstate.maxrows) in
2980 let active = find (itemcount - 1) ~-1 in
2981 G.postRedisplay "listview end";
2982 set active first;
2984 | key when (key = 0 || Wsi.isspecialkey key) ->
2985 coe self
2987 | _ ->
2988 dolog "listview unknown key %#x" key; coe self
2990 method key key mask =
2991 match state.mode with
2992 | Textentry te -> textentrykeyboard key mask te; coe self
2993 | Birdseye _
2994 | View
2995 | LinkNav _ -> self#key1 key mask
2997 method button button down x y _ =
2998 let opt =
2999 match button with
3000 | 1 when vscrollhit x ->
3001 G.postRedisplay "listview scroll";
3002 if down
3003 then
3004 let _, position, sh = self#scrollph in
3005 if y > truncate position && y < truncate (position +. sh)
3006 then (
3007 state.mstate <- Mscrolly;
3008 Some (coe self)
3010 else
3011 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3012 let first = truncate (s *. float source#getitemcount) in
3013 let first = min source#getitemcount first in
3014 Some (coe {< m_first = first; m_active = first >})
3015 else (
3016 state.mstate <- Mnone;
3017 Some (coe self);
3019 | 1 when down ->
3020 begin match self#elemunder y with
3021 | Some n ->
3022 G.postRedisplay "listview click";
3023 source#exit ~uioh:(coe {< m_active = n >})
3024 ~cancel:false ~active:n ~first:m_first ~pan:m_pan
3025 | _ ->
3026 Some (coe self)
3028 | n when (n == 4 || n == 5) && not down ->
3029 let len = source#getitemcount in
3030 let first =
3031 if n = 5 && m_first + fstate.maxrows >= len
3032 then
3033 m_first
3034 else
3035 let first = m_first + (if n == 4 then -1 else 1) in
3036 bound first 0 (len - 1)
3038 G.postRedisplay "listview wheel";
3039 Some (coe {< m_first = first >})
3040 | n when (n = 6 || n = 7) && not down ->
3041 let inc = if n = 7 then -1 else 1 in
3042 G.postRedisplay "listview hwheel";
3043 Some (coe {< m_pan = m_pan + inc >})
3044 | _ ->
3045 Some (coe self)
3047 match opt with
3048 | None -> m_prev_uioh
3049 | Some uioh -> uioh
3051 method multiclick _ x y = self#button 1 true x y
3053 method motion _ y =
3054 match state.mstate with
3055 | Mscrolly ->
3056 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3057 let first = truncate (s *. float source#getitemcount) in
3058 let first = min source#getitemcount first in
3059 G.postRedisplay "listview motion";
3060 coe {< m_first = first; m_active = first >}
3061 | Msel _
3062 | Mpan _
3063 | Mscrollx
3064 | Mzoom _
3065 | Mzoomrect _
3066 | Mnone -> coe self
3068 method pmotion x y =
3069 if x < state.winw - conf.scrollbw
3070 then
3071 let n =
3072 match self#elemunder y with
3073 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3074 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3076 let o =
3077 if n != m_active
3078 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3079 else self
3081 coe o
3082 else (
3083 Wsi.setcursor Wsi.CURSOR_INHERIT;
3084 coe self
3087 method infochanged _ = ()
3089 method scrollpw = (0, 0.0, 0.0)
3090 method scrollph =
3091 let nfs = fstate.fontsize + 1 in
3092 let y = m_first * nfs in
3093 let itemcount = source#getitemcount in
3094 let maxi = max 0 (itemcount - fstate.maxrows) in
3095 let maxy = maxi * nfs in
3096 let p, h = scrollph y maxy in
3097 conf.scrollbw, p, h
3099 method modehash = modehash
3100 method eformsgs = false
3101 method alwaysscrolly = true
3102 end;;
3104 class outlinelistview ~zebra ~source =
3105 let settext autonarrow s =
3106 if autonarrow
3107 then
3108 let ss = source#statestr in
3109 state.text <-
3110 if emptystr ss
3111 then "[" ^ s ^ "]"
3112 else "{" ^ ss ^ "} [" ^ s ^ "]"
3113 else state.text <- s
3115 object (self)
3116 inherit listview
3117 ~zebra
3118 ~helpmode:false
3119 ~source:(source :> lvsource)
3120 ~trusted:false
3121 ~modehash:(findkeyhash conf "outline")
3122 as super
3124 val m_autonarrow = false
3126 method! key key mask =
3127 let maxrows =
3128 if emptystr state.text
3129 then fstate.maxrows
3130 else fstate.maxrows - 2
3132 let calcfirst first active =
3133 if active > first
3134 then
3135 let rows = active - first in
3136 if rows > maxrows then active - maxrows else first
3137 else active
3139 let navigate incr =
3140 let active = m_active + incr in
3141 let active = bound active 0 (source#getitemcount - 1) in
3142 let first = calcfirst m_first active in
3143 G.postRedisplay "outline navigate";
3144 coe {< m_active = active; m_first = first >}
3146 let navscroll first =
3147 let active =
3148 let dist = m_active - first in
3149 if dist < 0
3150 then first
3151 else (
3152 if dist < maxrows
3153 then m_active
3154 else first + maxrows
3157 G.postRedisplay "outline navscroll";
3158 coe {< m_first = first; m_active = active >}
3160 let ctrl = Wsi.withctrl mask in
3161 match key with
3162 | @a when ctrl ->
3163 let text =
3164 if m_autonarrow
3165 then (source#denarrow; E.s)
3166 else (
3167 let pattern = source#renarrow in
3168 if nonemptystr m_qsearch
3169 then (source#narrow m_qsearch; m_qsearch)
3170 else pattern
3173 settext (not m_autonarrow) text;
3174 G.postRedisplay "toggle auto narrowing";
3175 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
3177 | @slash when emptystr m_qsearch && not m_autonarrow ->
3178 settext true E.s;
3179 G.postRedisplay "toggle auto narrowing";
3180 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
3182 | @n when ctrl ->
3183 source#narrow m_qsearch;
3184 if not m_autonarrow
3185 then source#add_narrow_pattern m_qsearch;
3186 G.postRedisplay "outline ctrl-n";
3187 coe {< m_first = 0; m_active = 0 >}
3189 | @S when ctrl ->
3190 let active = source#calcactive (getanchor ()) in
3191 let first = firstof m_first active in
3192 G.postRedisplay "outline ctrl-s";
3193 coe {< m_first = first; m_active = active >}
3195 | @u when ctrl ->
3196 G.postRedisplay "outline ctrl-u";
3197 if m_autonarrow && nonemptystr m_qsearch
3198 then (
3199 ignore (source#renarrow);
3200 settext m_autonarrow E.s;
3201 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3203 else (
3204 source#del_narrow_pattern;
3205 let pattern = source#renarrow in
3206 let text =
3207 if emptystr pattern then E.s else "Narrowed to " ^ pattern
3209 settext m_autonarrow text;
3210 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3213 | @l when ctrl ->
3214 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3215 G.postRedisplay "outline ctrl-l";
3216 coe {< m_first = first >}
3218 | @tab when m_autonarrow ->
3219 if nonemptystr m_qsearch
3220 then (
3221 G.postRedisplay "outline list view tab";
3222 source#add_narrow_pattern m_qsearch;
3223 settext true E.s;
3224 coe {< m_qsearch = E.s >}
3226 else coe self
3228 | @escape when m_autonarrow ->
3229 if nonemptystr m_qsearch
3230 then source#add_narrow_pattern m_qsearch;
3231 super#key key mask
3233 | @enter | @kpenter when m_autonarrow ->
3234 if nonemptystr m_qsearch
3235 then source#add_narrow_pattern m_qsearch;
3236 super#key key mask
3238 | key when m_autonarrow && (not (Wsi.isspecialkey key)) ->
3239 let pattern = m_qsearch ^ toutf8 key in
3240 G.postRedisplay "outlinelistview autonarrow add";
3241 source#narrow pattern;
3242 settext true pattern;
3243 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3245 | key when m_autonarrow && key = @backspace ->
3246 if emptystr m_qsearch
3247 then coe self
3248 else
3249 let pattern = withoutlastutf8 m_qsearch in
3250 G.postRedisplay "outlinelistview autonarrow backspace";
3251 ignore (source#renarrow);
3252 source#narrow pattern;
3253 settext true pattern;
3254 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3256 | @up | @kpup when ctrl ->
3257 navscroll (max 0 (m_first - 1))
3259 | @down | @kpdown when ctrl ->
3260 navscroll (min (source#getitemcount - 1) (m_first + 1))
3262 | @up | @kpup -> navigate ~-1
3263 | @down | @kpdown -> navigate 1
3264 | @prior | @kpprior -> navigate ~-(fstate.maxrows)
3265 | @next | @kpnext -> navigate fstate.maxrows
3267 | @right | @kpright ->
3268 let o =
3269 if ctrl
3270 then (
3271 G.postRedisplay "outline ctrl right";
3272 {< m_pan = m_pan + 1 >}
3274 else self#updownlevel 1
3276 coe o
3278 | @left | @kpleft ->
3279 let o =
3280 if ctrl
3281 then (
3282 G.postRedisplay "outline ctrl left";
3283 {< m_pan = m_pan - 1 >}
3285 else self#updownlevel ~-1
3287 coe o
3289 | @home | @kphome ->
3290 G.postRedisplay "outline home";
3291 coe {< m_first = 0; m_active = 0 >}
3293 | @jend | @kpend ->
3294 let active = source#getitemcount - 1 in
3295 let first = max 0 (active - fstate.maxrows) in
3296 G.postRedisplay "outline end";
3297 coe {< m_active = active; m_first = first >}
3299 | _ -> super#key key mask
3300 end;;
3302 let genhistoutlines () =
3303 Config.gethist ()
3304 |> List.sort (fun (_, c1, _, _, _, _) (_, c2, _, _, _, _) ->
3305 compare c2.lastvisit c1.lastvisit)
3306 |> List.map
3307 (fun ((path, c, _, _, _, origin) as hist) ->
3308 let path = if nonemptystr origin then origin else path in
3309 let base = mbtoutf8 @@ Filename.basename path in
3310 (base ^ "\000" ^ c.title, 1, Ohistory hist)
3312 |> Array.of_list
3315 let gotohist (path, c, bookmarks, x, anchor, origin) =
3316 Config.save leavebirdseye;
3317 state.anchor <- anchor;
3318 state.bookmarks <- bookmarks;
3319 state.origin <- origin;
3320 state.x <- x;
3321 setconf conf c;
3322 let x0, y0, x1, y1 = conf.trimfuzz in
3323 wcmd "trimset %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1;
3324 reshape ~firsttime:true state.winw state.winh;
3325 opendoc path origin;
3326 setzoom c.zoom;
3329 let makecheckers () =
3330 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3331 following to say:
3332 converted by Issac Trotts. July 25, 2002 *)
3333 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3334 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3335 let id = GlTex.gen_texture () in
3336 GlTex.bind_texture ~target:`texture_2d id;
3337 GlPix.store (`unpack_alignment 1);
3338 GlTex.image2d image;
3339 List.iter (GlTex.parameter ~target:`texture_2d)
3340 [ `mag_filter `nearest; `min_filter `nearest ];
3344 let setcheckers enabled =
3345 match state.checkerstexid with
3346 | None ->
3347 if enabled then state.checkerstexid <- Some (makecheckers ())
3349 | Some checkerstexid ->
3350 if not enabled
3351 then (
3352 GlTex.delete_texture checkerstexid;
3353 state.checkerstexid <- None;
3357 let describe_location () =
3358 let fn = page_of_y state.y in
3359 let ln = page_of_y (state.y + state.winh - 1) in
3360 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
3361 let percent =
3362 if maxy <= 0
3363 then 100.
3364 else (100. *. (float state.y /. float maxy))
3366 if fn = ln
3367 then
3368 Printf.sprintf "page %d of %d [%.2f%%]"
3369 (fn+1) state.pagecount percent
3370 else
3371 Printf.sprintf
3372 "pages %d-%d of %d [%.2f%%]"
3373 (fn+1) (ln+1) state.pagecount percent
3376 let setpresentationmode v =
3377 let n = page_of_y state.y in
3378 state.anchor <- (n, 0.0, 1.0);
3379 conf.presentation <- v;
3380 if conf.fitmodel = FitPage
3381 then reqlayout conf.angle conf.fitmodel;
3382 represent ();
3385 let setbgcol (r, g, b) =
3386 let col =
3387 let r = r *. 255.0 |> truncate
3388 and g = g *. 255.0 |> truncate
3389 and b = b *. 255.0 |> truncate in
3390 r lsl 16 |> (lor) (g lsl 8) |> (lor) b
3392 Wsi.setwinbgcol col;
3395 let enterinfomode =
3396 let btos b = if b then "@Uradical" else E.s in
3397 let showextended = ref false in
3398 let leave mode _ = state.mode <- mode in
3399 let src =
3400 (object
3401 val mutable m_l = []
3402 val mutable m_a = E.a
3403 val mutable m_prev_uioh = nouioh
3404 val mutable m_prev_mode = View
3406 inherit lvsourcebase
3408 method reset prev_mode prev_uioh =
3409 m_a <- Array.of_list (List.rev m_l);
3410 m_l <- [];
3411 m_prev_mode <- prev_mode;
3412 m_prev_uioh <- prev_uioh;
3414 method int name get set =
3415 m_l <-
3416 (name, `int get, 1,
3417 Action (
3418 fun u ->
3419 let ondone s =
3420 try set (int_of_string s)
3421 with exn ->
3422 state.text <- Printf.sprintf "bad integer `%s': %s"
3423 s @@ exntos exn
3425 state.text <- E.s;
3426 let te = name ^ ": ", E.s, None, intentry, ondone, true in
3427 state.mode <- Textentry (te, leave m_prev_mode);
3429 )) :: m_l
3431 method int_with_suffix name get set =
3432 m_l <-
3433 (name, `intws get, 1,
3434 Action (
3435 fun u ->
3436 let ondone s =
3437 try set (int_of_string_with_suffix s)
3438 with exn ->
3439 state.text <- Printf.sprintf "bad integer `%s': %s"
3440 s @@ exntos exn
3442 state.text <- E.s;
3443 let te =
3444 name ^ ": ", E.s, None, intentry_with_suffix, ondone, true
3446 state.mode <- Textentry (te, leave m_prev_mode);
3448 )) :: m_l
3450 method bool ?(offset=1) ?(btos=btos) name get set =
3451 m_l <-
3452 (name, `bool (btos, get), offset, Action (
3453 fun u ->
3454 let v = get () in
3455 set (not v);
3457 )) :: m_l
3459 method color name get set =
3460 m_l <-
3461 (name, `color get, 1,
3462 Action (
3463 fun u ->
3464 let invalid = (nan, nan, nan) in
3465 let ondone s =
3466 let c =
3467 try color_of_string s
3468 with exn ->
3469 state.text <- Printf.sprintf "bad color `%s': %s"
3470 s @@ exntos exn;
3471 invalid
3473 if c <> invalid
3474 then set c;
3476 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3477 state.text <- color_to_string (get ());
3478 state.mode <- Textentry (te, leave m_prev_mode);
3480 )) :: m_l
3482 method string name get set =
3483 m_l <-
3484 (name, `string get, 1,
3485 Action (
3486 fun u ->
3487 let ondone s = set s in
3488 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3489 state.mode <- Textentry (te, leave m_prev_mode);
3491 )) :: m_l
3493 method colorspace name get set =
3494 m_l <-
3495 (name, `string get, 1,
3496 Action (
3497 fun _ ->
3498 let source =
3499 (object
3500 inherit lvsourcebase
3502 initializer
3503 m_active <- CSTE.to_int conf.colorspace;
3504 m_first <- 0;
3506 method getitemcount =
3507 Array.length CSTE.names
3508 method getitem n =
3509 (CSTE.names.(n), 0)
3510 method exit ~uioh ~cancel ~active ~first ~pan =
3511 ignore (uioh, first, pan);
3512 if not cancel then set active;
3513 None
3514 method hasaction _ = true
3515 end)
3517 state.text <- E.s;
3518 let modehash = findkeyhash conf "info" in
3519 coe (new listview ~zebra:false ~helpmode:false
3520 ~source ~trusted:true ~modehash)
3521 )) :: m_l
3523 method paxmark name get set =
3524 m_l <-
3525 (name, `string get, 1,
3526 Action (
3527 fun _ ->
3528 let source =
3529 (object
3530 inherit lvsourcebase
3532 initializer
3533 m_active <- MTE.to_int conf.paxmark;
3534 m_first <- 0;
3536 method getitemcount = Array.length MTE.names
3537 method getitem n = (MTE.names.(n), 0)
3538 method exit ~uioh ~cancel ~active ~first ~pan =
3539 ignore (uioh, first, pan);
3540 if not cancel then set active;
3541 None
3542 method hasaction _ = true
3543 end)
3545 state.text <- E.s;
3546 let modehash = findkeyhash conf "info" in
3547 coe (new listview ~zebra:false ~helpmode:false
3548 ~source ~trusted:true ~modehash)
3549 )) :: m_l
3551 method fitmodel name get set =
3552 m_l <-
3553 (name, `string get, 1,
3554 Action (
3555 fun _ ->
3556 let source =
3557 (object
3558 inherit lvsourcebase
3560 initializer
3561 m_active <- FMTE.to_int conf.fitmodel;
3562 m_first <- 0;
3564 method getitemcount = Array.length FMTE.names
3565 method getitem n = (FMTE.names.(n), 0)
3566 method exit ~uioh ~cancel ~active ~first ~pan =
3567 ignore (uioh, first, pan);
3568 if not cancel then set active;
3569 None
3570 method hasaction _ = true
3571 end)
3573 state.text <- E.s;
3574 let modehash = findkeyhash conf "info" in
3575 coe (new listview ~zebra:false ~helpmode:false
3576 ~source ~trusted:true ~modehash)
3577 )) :: m_l
3579 method caption s offset =
3580 m_l <- (s, `empty, offset, Noaction) :: m_l
3582 method caption2 s f offset =
3583 m_l <- (s, `string f, offset, Noaction) :: m_l
3585 method getitemcount = Array.length m_a
3587 method getitem n =
3588 let tostr = function
3589 | `int f -> string_of_int (f ())
3590 | `intws f -> string_with_suffix_of_int (f ())
3591 | `string f -> f ()
3592 | `color f -> color_to_string (f ())
3593 | `bool (btos, f) -> btos (f ())
3594 | `empty -> E.s
3596 let name, t, offset, _ = m_a.(n) in
3597 ((let s = tostr t in
3598 if nonemptystr s
3599 then Printf.sprintf "%s\t%s" name s
3600 else name),
3601 offset)
3603 method exit ~uioh ~cancel ~active ~first ~pan =
3604 let uiohopt =
3605 if not cancel
3606 then (
3607 let uioh =
3608 match m_a.(active) with
3609 | _, _, _, Action f -> f uioh
3610 | _, _, _, Noaction -> uioh
3612 Some uioh
3614 else None
3616 m_active <- active;
3617 m_first <- first;
3618 m_pan <- pan;
3619 uiohopt
3621 method hasaction n =
3622 match m_a.(n) with
3623 | _, _, _, Action _ -> true
3624 | _, _, _, Noaction -> false
3626 initializer m_active <- 1
3627 end)
3629 let rec fillsrc prevmode prevuioh =
3630 let sep () = src#caption E.s 0 in
3631 let colorp name get set =
3632 src#string name
3633 (fun () -> color_to_string (get ()))
3634 (fun v ->
3636 let c = color_of_string v in
3637 set c
3638 with exn ->
3639 state.text <-
3640 Printf.sprintf "bad color `%s': %s" v @@ exntos exn
3643 let oldmode = state.mode in
3644 let birdseye = isbirdseye state.mode in
3646 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3648 src#bool "presentation mode"
3649 (fun () -> conf.presentation)
3650 (fun v -> setpresentationmode v);
3652 src#bool "ignore case in searches"
3653 (fun () -> conf.icase)
3654 (fun v -> conf.icase <- v);
3656 src#bool "preload"
3657 (fun () -> conf.preload)
3658 (fun v -> conf.preload <- v);
3660 src#bool "highlight links"
3661 (fun () -> conf.hlinks)
3662 (fun v -> conf.hlinks <- v);
3664 src#bool "under info"
3665 (fun () -> conf.underinfo)
3666 (fun v -> conf.underinfo <- v);
3668 src#bool "persistent bookmarks"
3669 (fun () -> conf.savebmarks)
3670 (fun v -> conf.savebmarks <- v);
3672 src#fitmodel "fit model"
3673 (fun () -> FMTE.to_string conf.fitmodel)
3674 (fun v -> reqlayout conf.angle (FMTE.of_int v));
3676 src#bool "trim margins"
3677 (fun () -> conf.trimmargins)
3678 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3680 src#bool "persistent location"
3681 (fun () -> conf.jumpback)
3682 (fun v -> conf.jumpback <- v);
3684 sep ();
3685 src#int "inter-page space"
3686 (fun () -> conf.interpagespace)
3687 (fun n ->
3688 conf.interpagespace <- n;
3689 docolumns conf.columns;
3690 let pageno, py =
3691 match state.layout with
3692 | [] -> 0, 0
3693 | l :: _ ->
3694 l.pageno, l.pagey
3696 state.maxy <- calcheight ();
3697 let y = getpagey pageno in
3698 gotoxy state.x (y + py)
3701 src#int "page bias"
3702 (fun () -> conf.pagebias)
3703 (fun v -> conf.pagebias <- v);
3705 src#int "scroll step"
3706 (fun () -> conf.scrollstep)
3707 (fun n -> conf.scrollstep <- n);
3709 src#int "horizontal scroll step"
3710 (fun () -> conf.hscrollstep)
3711 (fun v -> conf.hscrollstep <- v);
3713 src#int "auto scroll step"
3714 (fun () ->
3715 match state.autoscroll with
3716 | Some step -> step
3717 | _ -> conf.autoscrollstep)
3718 (fun n ->
3719 let n = boundastep state.winh n in
3720 if state.autoscroll <> None
3721 then state.autoscroll <- Some n;
3722 conf.autoscrollstep <- n);
3724 src#int "zoom"
3725 (fun () -> truncate (conf.zoom *. 100.))
3726 (fun v -> pivotzoom ((float v) /. 100.));
3728 src#int "rotation"
3729 (fun () -> conf.angle)
3730 (fun v -> reqlayout v conf.fitmodel);
3732 src#int "scroll bar width"
3733 (fun () -> conf.scrollbw)
3734 (fun v ->
3735 conf.scrollbw <- v;
3736 reshape state.winw state.winh;
3739 src#int "scroll handle height"
3740 (fun () -> conf.scrollh)
3741 (fun v -> conf.scrollh <- v;);
3743 src#int "thumbnail width"
3744 (fun () -> conf.thumbw)
3745 (fun v ->
3746 conf.thumbw <- min 4096 v;
3747 match oldmode with
3748 | Birdseye beye ->
3749 leavebirdseye beye false;
3750 enterbirdseye ()
3751 | Textentry _
3752 | View
3753 | LinkNav _ -> ()
3756 let mode = state.mode in
3757 src#string "columns"
3758 (fun () ->
3759 match conf.columns with
3760 | Csingle _ -> "1"
3761 | Cmulti (multi, _) -> multicolumns_to_string multi
3762 | Csplit (count, _) -> "-" ^ string_of_int count
3764 (fun v ->
3765 let n, a, b = multicolumns_of_string v in
3766 setcolumns mode n a b);
3768 sep ();
3769 src#caption "Pixmap cache" 0;
3770 src#int_with_suffix "size (advisory)"
3771 (fun () -> conf.memlimit)
3772 (fun v -> conf.memlimit <- v);
3774 src#caption2 "used"
3775 (fun () ->
3776 Printf.sprintf "%s bytes, %d tiles"
3777 (string_with_suffix_of_int state.memused)
3778 (Hashtbl.length state.tilemap)) 1;
3780 sep ();
3781 src#caption "Layout" 0;
3782 src#caption2 "Dimension"
3783 (fun () ->
3784 Printf.sprintf "%dx%d (virtual %dx%d)"
3785 state.winw state.winh
3786 state.w state.maxy)
3788 if conf.debug
3789 then
3790 src#caption2 "Position" (fun () ->
3791 Printf.sprintf "%dx%d" state.x state.y
3793 else
3794 src#caption2 "Position" (fun () -> describe_location ()) 1
3797 sep ();
3798 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3799 "Save these parameters as global defaults at exit"
3800 (fun () -> conf.bedefault)
3801 (fun v -> conf.bedefault <- v)
3804 sep ();
3805 let btos b = if b then "@Ulguillemet" else "@Urguillemet" in
3806 src#bool ~offset:0 ~btos "Extended parameters"
3807 (fun () -> !showextended)
3808 (fun v -> showextended := v; fillsrc prevmode prevuioh);
3809 if !showextended
3810 then (
3811 src#bool "checkers"
3812 (fun () -> conf.checkers)
3813 (fun v -> conf.checkers <- v; setcheckers v);
3814 src#bool "update cursor"
3815 (fun () -> conf.updatecurs)
3816 (fun v -> conf.updatecurs <- v);
3817 src#bool "scroll-bar on the left"
3818 (fun () -> conf.leftscroll)
3819 (fun v -> conf.leftscroll <- v);
3820 src#bool "verbose"
3821 (fun () -> conf.verbose)
3822 (fun v -> conf.verbose <- v);
3823 src#bool "invert colors"
3824 (fun () -> conf.invert)
3825 (fun v -> conf.invert <- v);
3826 src#bool "max fit"
3827 (fun () -> conf.maxhfit)
3828 (fun v -> conf.maxhfit <- v);
3829 src#bool "pax mode"
3830 (fun () -> conf.pax != None)
3831 (fun v ->
3832 if v
3833 then conf.pax <- Some (ref (now (), 0, 0))
3834 else conf.pax <- None);
3835 src#string "uri launcher"
3836 (fun () -> conf.urilauncher)
3837 (fun v -> conf.urilauncher <- v);
3838 src#string "path launcher"
3839 (fun () -> conf.pathlauncher)
3840 (fun v -> conf.pathlauncher <- v);
3841 src#string "tile size"
3842 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3843 (fun v ->
3845 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
3846 conf.tilew <- max 64 w;
3847 conf.tileh <- max 64 h;
3848 flushtiles ();
3849 with exn ->
3850 state.text <- Printf.sprintf "bad tile size `%s': %s"
3851 v @@ exntos exn
3853 src#int "texture count"
3854 (fun () -> conf.texcount)
3855 (fun v ->
3856 if realloctexts v
3857 then conf.texcount <- v
3858 else impmsg "failed to set texture count please retry later"
3860 src#int "slice height"
3861 (fun () -> conf.sliceheight)
3862 (fun v ->
3863 conf.sliceheight <- v;
3864 wcmd "sliceh %d" conf.sliceheight;
3866 src#int "anti-aliasing level"
3867 (fun () -> conf.aalevel)
3868 (fun v ->
3869 conf.aalevel <- bound v 0 8;
3870 state.anchor <- getanchor ();
3871 opendoc state.path state.password;
3873 src#string "page scroll scaling factor"
3874 (fun () -> string_of_float conf.pgscale)
3875 (fun v ->
3877 let s = float_of_string v in
3878 conf.pgscale <- s
3879 with exn ->
3880 state.text <- Printf.sprintf
3881 "bad page scroll scaling factor `%s': %s" v
3882 @@ exntos exn
3885 src#int "ui font size"
3886 (fun () -> fstate.fontsize)
3887 (fun v -> setfontsize (bound v 5 100));
3888 src#int "hint font size"
3889 (fun () -> conf.hfsize)
3890 (fun v -> conf.hfsize <- bound v 5 100);
3891 colorp "background color"
3892 (fun () -> conf.bgcolor)
3893 (fun v -> conf.bgcolor <- v; setbgcol v);
3894 src#bool "crop hack"
3895 (fun () -> conf.crophack)
3896 (fun v -> conf.crophack <- v);
3897 src#string "trim fuzz"
3898 (fun () -> irect_to_string conf.trimfuzz)
3899 (fun v ->
3901 conf.trimfuzz <- irect_of_string v;
3902 if conf.trimmargins
3903 then settrim true conf.trimfuzz;
3904 with exn ->
3905 state.text <- Printf.sprintf "bad irect `%s': %s" v
3906 @@ exntos exn
3908 src#string "throttle"
3909 (fun () ->
3910 match conf.maxwait with
3911 | None -> "show place holder if page is not ready"
3912 | Some time ->
3913 if time = infinity
3914 then "wait for page to fully render"
3915 else
3916 "wait " ^ string_of_float time
3917 ^ " seconds before showing placeholder"
3919 (fun v ->
3921 let f = float_of_string v in
3922 if f <= 0.0
3923 then conf.maxwait <- None
3924 else conf.maxwait <- Some f
3925 with exn ->
3926 state.text <- Printf.sprintf "bad time `%s': %s" v
3927 @@ exntos exn
3929 src#string "ghyll scroll"
3930 (fun () ->
3931 match conf.ghyllscroll with
3932 | None -> E.s
3933 | Some nab -> ghyllscroll_to_string nab
3935 (fun v ->
3936 try conf.ghyllscroll <- ghyllscroll_of_string v
3937 with
3938 | Failure msg ->
3939 state.text <- Printf.sprintf "bad ghyll `%s': %s" v msg
3940 | exn ->
3941 state.text <- Printf.sprintf "bad ghyll `%s': %s" v
3942 @@ exntos exn
3944 src#string "selection command"
3945 (fun () -> conf.selcmd)
3946 (fun v -> conf.selcmd <- v);
3947 src#string "synctex command"
3948 (fun () -> conf.stcmd)
3949 (fun v -> conf.stcmd <- v);
3950 src#string "pax command"
3951 (fun () -> conf.paxcmd)
3952 (fun v -> conf.paxcmd <- v);
3953 src#string "ask password command"
3954 (fun () -> conf.passcmd)
3955 (fun v -> conf.passcmd <- v);
3956 src#string "save path command"
3957 (fun () -> conf.savecmd)
3958 (fun v -> conf.savecmd <- v);
3959 src#colorspace "color space"
3960 (fun () -> CSTE.to_string conf.colorspace)
3961 (fun v ->
3962 conf.colorspace <- CSTE.of_int v;
3963 wcmd "cs %d" v;
3964 load state.layout;
3966 src#paxmark "pax mark method"
3967 (fun () -> MTE.to_string conf.paxmark)
3968 (fun v -> conf.paxmark <- MTE.of_int v);
3969 if bousable () && !opengl_has_pbo
3970 then
3971 src#bool "use PBO"
3972 (fun () -> conf.usepbo)
3973 (fun v -> conf.usepbo <- v);
3974 src#bool "mouse wheel scrolls pages"
3975 (fun () -> conf.wheelbypage)
3976 (fun v -> conf.wheelbypage <- v);
3977 src#bool "open remote links in a new instance"
3978 (fun () -> conf.riani)
3979 (fun v -> conf.riani <- v);
3980 src#bool "edit annotations inline"
3981 (fun () -> conf.annotinline)
3982 (fun v -> conf.annotinline <- v);
3983 src#bool "coarse positioning in presentation mode"
3984 (fun () -> conf.coarseprespos)
3985 (fun v -> conf.coarseprespos <- v);
3986 src#bool "use document css"
3987 (fun () -> conf.usedoccss)
3988 (fun v -> conf.usedoccss <- v)
3991 sep ();
3992 src#caption "Document" 0;
3993 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
3994 src#caption2 "Pages"
3995 (fun () -> string_of_int state.pagecount) 1;
3996 src#caption2 "Dimensions"
3997 (fun () -> string_of_int (List.length state.pdims)) 1;
3998 if nonemptystr conf.css
3999 then src#caption2 "CSS" (fun () -> conf.css) 1;
4000 if conf.trimmargins
4001 then (
4002 sep ();
4003 src#caption "Trimmed margins" 0;
4004 src#caption2 "Dimensions"
4005 (fun () -> string_of_int (List.length state.pdims)) 1;
4008 sep ();
4009 src#caption "OpenGL" 0;
4010 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4011 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4013 sep ();
4014 src#caption "Location" 0;
4015 if nonemptystr state.origin
4016 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
4017 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
4019 src#reset prevmode prevuioh;
4021 fun () ->
4022 state.text <- E.s;
4023 resetmstate ();
4024 let prevmode = state.mode
4025 and prevuioh = state.uioh in
4026 fillsrc prevmode prevuioh;
4027 let source = (src :> lvsource) in
4028 let modehash = findkeyhash conf "info" in
4029 state.uioh <-
4030 coe (object (self)
4031 inherit listview ~zebra:false ~helpmode:false
4032 ~source ~trusted:true ~modehash as super
4033 val mutable m_prevmemused = 0
4034 method! infochanged = function
4035 | Memused ->
4036 if m_prevmemused != state.memused
4037 then (
4038 m_prevmemused <- state.memused;
4039 G.postRedisplay "memusedchanged";
4041 | Pdim -> G.postRedisplay "pdimchanged"
4042 | Docinfo -> fillsrc prevmode prevuioh
4044 method! key key mask =
4045 if not (Wsi.withctrl mask)
4046 then
4047 match key with
4048 | @left | @kpleft -> coe (self#updownlevel ~-1)
4049 | @right | @kpright -> coe (self#updownlevel 1)
4050 | _ -> super#key key mask
4051 else super#key key mask
4052 end);
4053 G.postRedisplay "info";
4056 let enterhelpmode =
4057 let source =
4058 (object
4059 inherit lvsourcebase
4060 method getitemcount = Array.length state.help
4061 method getitem n =
4062 let s, l, _ = state.help.(n) in
4063 (s, l)
4065 method exit ~uioh ~cancel ~active ~first ~pan =
4066 let optuioh =
4067 if not cancel
4068 then (
4069 match state.help.(active) with
4070 | _, _, Action f -> Some (f uioh)
4071 | _, _, Noaction -> Some uioh
4073 else None
4075 m_active <- active;
4076 m_first <- first;
4077 m_pan <- pan;
4078 optuioh
4080 method hasaction n =
4081 match state.help.(n) with
4082 | _, _, Action _ -> true
4083 | _, _, Noaction -> false
4085 initializer
4086 m_active <- -1
4087 end)
4088 in fun () ->
4089 let modehash = findkeyhash conf "help" in
4090 resetmstate ();
4091 state.uioh <- coe (new listview
4092 ~zebra:false ~helpmode:true
4093 ~source ~trusted:true ~modehash);
4094 G.postRedisplay "help";
4097 let entermsgsmode =
4098 let msgsource =
4099 (object
4100 inherit lvsourcebase
4101 val mutable m_items = E.a
4103 method getitemcount = 1 + Array.length m_items
4105 method getitem n =
4106 if n = 0
4107 then "[Clear]", 0
4108 else m_items.(n-1), 0
4110 method exit ~uioh ~cancel ~active ~first ~pan =
4111 ignore uioh;
4112 if not cancel
4113 then (
4114 if active = 0
4115 then Buffer.clear state.errmsgs;
4117 m_active <- active;
4118 m_first <- first;
4119 m_pan <- pan;
4120 None
4122 method hasaction n =
4123 n = 0
4125 method reset =
4126 state.newerrmsgs <- false;
4127 let l = Str.split newlinere (Buffer.contents state.errmsgs) in
4128 m_items <- Array.of_list l
4130 initializer
4131 m_active <- 0
4132 end)
4133 in fun () ->
4134 state.text <- E.s;
4135 resetmstate ();
4136 msgsource#reset;
4137 let source = (msgsource :> lvsource) in
4138 let modehash = findkeyhash conf "listview" in
4139 state.uioh <-
4140 coe (object
4141 inherit listview ~zebra:false ~helpmode:false
4142 ~source ~trusted:false ~modehash as super
4143 method! display =
4144 if state.newerrmsgs
4145 then msgsource#reset;
4146 super#display
4147 end);
4148 G.postRedisplay "msgs";
4151 let getusertext s =
4152 let editor = getenvwithdef "EDITOR" E.s in
4153 if emptystr editor
4154 then E.s
4155 else
4156 let tmppath = Filename.temp_file "llpp" "note" in
4157 if nonemptystr s
4158 then (
4159 let oc = open_out tmppath in
4160 output_string oc s;
4161 close_out oc;
4163 let execstr = editor ^ " " ^ tmppath in
4164 let s =
4165 match spawn execstr [] with
4166 | (exception exn) ->
4167 impmsg "spawn(%S) failed: %s" execstr @@ exntos exn;
4169 | pid ->
4170 match Unix.waitpid [] pid with
4171 | (exception exn) ->
4172 impmsg "waitpid(%d) failed: %s" pid @@ exntos exn;
4174 | (_pid, status) ->
4175 match status with
4176 | Unix.WEXITED 0 -> filecontents tmppath
4177 | Unix.WEXITED n ->
4178 impmsg "editor process(%s) exited abnormally: %d" execstr n;
4180 | Unix.WSIGNALED n ->
4181 impmsg "editor process(%s) was killed by signal %d" execstr n;
4183 | Unix.WSTOPPED n ->
4184 impmsg "editor(%s) process was stopped by signal %d" execstr n;
4187 match Unix.unlink tmppath with
4188 | (exception exn) ->
4189 impmsg "failed to ulink %S: %s" tmppath @@ exntos exn;
4191 | () -> s
4194 let enterannotmode opaque slinkindex =
4195 let msgsource =
4196 (object
4197 inherit lvsourcebase
4198 val mutable m_text = E.s
4199 val mutable m_items = E.a
4201 method getitemcount = Array.length m_items
4203 method getitem n =
4204 let label, _func = m_items.(n) in
4205 label, 0
4207 method exit ~uioh ~cancel ~active ~first ~pan =
4208 ignore (uioh, first, pan);
4209 if not cancel
4210 then (
4211 let _label, func = m_items.(active) in
4212 func ()
4214 None
4216 method hasaction n = nonemptystr @@ fst m_items.(n)
4218 method reset s =
4219 let rec split accu b i =
4220 let p = b+i in
4221 if p = String.length s
4222 then (String.sub s b (p-b), unit) :: accu
4223 else
4224 if (i > 70 && s.[p] = ' ') || s.[p] = '\r' || s.[p] = '\n'
4225 then
4226 let ss = if i = 0 then E.s else String.sub s b i in
4227 split ((ss, unit)::accu) (p+1) 0
4228 else
4229 split accu b (i+1)
4231 let cleanup () =
4232 wcmd "freepage %s" (~> opaque);
4233 let keys =
4234 Hashtbl.fold (fun key opaque' accu ->
4235 if opaque' = opaque'
4236 then key :: accu else accu) state.pagemap []
4238 List.iter (Hashtbl.remove state.pagemap) keys;
4239 flushtiles ();
4240 gotoxy state.x state.y
4242 let dele () =
4243 delannot opaque slinkindex;
4244 cleanup ();
4246 let edit inline () =
4247 let update s =
4248 if emptystr s
4249 then dele ()
4250 else (
4251 modannot opaque slinkindex s;
4252 cleanup ();
4255 if inline
4256 then
4257 let mode = state.mode in
4258 state.mode <-
4259 Textentry (
4260 ("annotation: ", m_text, None, textentry, update, true),
4261 fun _ -> state.mode <- mode);
4262 state.text <- E.s;
4263 enttext ();
4264 else
4265 let s = getusertext m_text in
4266 update s
4268 m_text <- s;
4269 m_items <-
4270 ( "[Copy]", fun () -> selstring m_text)
4271 :: ("[Delete]", dele)
4272 :: ("[Edit]", edit conf.annotinline)
4273 :: (E.s, unit)
4274 :: split [] 0 0 |> List.rev |> Array.of_list
4276 initializer
4277 m_active <- 0
4278 end)
4280 state.text <- E.s;
4281 let s = getannotcontents opaque slinkindex in
4282 resetmstate ();
4283 msgsource#reset s;
4284 let source = (msgsource :> lvsource) in
4285 let modehash = findkeyhash conf "listview" in
4286 state.uioh <- coe (object
4287 inherit listview ~zebra:false ~helpmode:false
4288 ~source ~trusted:false ~modehash
4289 end);
4290 G.postRedisplay "enterannotmode";
4293 let gotoremote spec =
4294 let filename, dest = splitatchar spec '#' in
4295 let getpath filename =
4296 let path =
4297 if nonemptystr filename
4298 then
4299 if Filename.is_relative filename
4300 then
4301 let dir = Filename.dirname state.path in
4302 let dir =
4303 if Filename.is_implicit dir
4304 then Filename.concat (Sys.getcwd ()) dir
4305 else dir
4307 Filename.concat dir filename
4308 else filename
4309 else E.s
4311 if Sys.file_exists path
4312 then path
4313 else E.s
4315 let path = getpath filename in
4316 let dospawn lcmd =
4317 if conf.riani
4318 then
4319 let cmd = Lazy.force_val lcmd in
4320 match spawn cmd with
4321 | _pid -> ()
4322 | (exception exn) ->
4323 dolog "failed to execute `%s': %s" cmd @@ exntos exn
4324 else
4325 let anchor = getanchor () in
4326 let ranchor = state.path, state.password, anchor, state.origin in
4327 state.origin <- E.s;
4328 state.ranchors <- ranchor :: state.ranchors;
4329 opendoc path E.s;
4331 if substratis spec 0 "page="
4332 then
4333 match Scanf.sscanf spec "page=%d" (fun n -> n) with
4334 | pageno ->
4335 state.anchor <- (pageno, 0.0, 0.0);
4336 dospawn @@ lazy (Printf.sprintf "%s -page %d %S" !selfexec pageno path);
4337 | exception exn ->
4338 adderrfmt "error parsing remote destination" "page: %s" @@ exntos exn
4339 else (
4340 state.nameddest <- dest;
4341 dospawn @@ lazy (!selfexec ^ " " ^ path ^ " -dest " ^ dest)
4345 let gotounder = function
4346 | Ulinkuri s when isexternallink s ->
4347 if substratis s 0 "file://"
4348 then gotoremote @@ String.sub s 7 (String.length s - 7)
4349 else gotouri s
4350 | Ulinkuri s ->
4351 let pageno, x, y = uritolocation s in
4352 addnav ();
4353 gotopagexy !wtmode pageno x y
4354 | Utext _ | Unone -> ()
4355 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
4358 let gotooutline (_, _, kind) =
4359 match kind with
4360 | Onone -> ()
4361 | Oanchor anchor ->
4362 let (pageno, y, _) = anchor in
4363 let y = getanchory
4364 (if conf.presentation then (pageno, y, 1.0) else anchor)
4366 addnav ();
4367 gotoghyll y
4368 | Ouri uri -> gotounder (Ulinkuri uri)
4369 | Olaunch _cmd -> failwith "gotounder (Ulaunch cmd)"
4370 | Oremote _remote -> failwith "gotounder (Uremote remote)"
4371 | Ohistory hist -> gotohist hist
4372 | Oremotedest _remotedest -> failwith "gotounder (Uremotedest remotedest)"
4375 class outlinesoucebase fetchoutlines = object (self)
4376 inherit lvsourcebase
4377 val mutable m_items = E.a
4378 val mutable m_minfo = E.a
4379 val mutable m_orig_items = E.a
4380 val mutable m_orig_minfo = E.a
4381 val mutable m_narrow_patterns = []
4382 val mutable m_gen = -1
4384 method getitemcount = Array.length m_items
4386 method getitem n =
4387 let s, n, _ = m_items.(n) in
4388 (s, n+0)
4390 method exit ~(uioh:uioh) ~cancel ~active ~(first:int) ~pan :
4391 uioh option =
4392 ignore (uioh, first);
4393 let items, minfo =
4394 if m_narrow_patterns = []
4395 then m_orig_items, m_orig_minfo
4396 else m_items, m_minfo
4398 m_pan <- pan;
4399 if not cancel
4400 then (
4401 m_items <- items;
4402 m_minfo <- minfo;
4403 gotooutline m_items.(active);
4405 else (
4406 m_items <- items;
4407 m_minfo <- minfo;
4409 None
4411 method hasaction (_:int) = true
4413 method greetmsg =
4414 if Array.length m_items != Array.length m_orig_items
4415 then
4416 let s =
4417 match m_narrow_patterns with
4418 | one :: [] -> one
4419 | many -> String.concat "@Uellipsis" (List.rev many)
4421 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
4422 else E.s
4424 method statestr =
4425 match m_narrow_patterns with
4426 | [] -> E.s
4427 | one :: [] -> one
4428 | head :: _ -> "@Uellipsis" ^ head
4430 method narrow pattern =
4431 match Str.regexp_case_fold pattern with
4432 | (exception _) -> ()
4433 | re ->
4434 let rec loop accu minfo n =
4435 if n = -1
4436 then (
4437 m_items <- Array.of_list accu;
4438 m_minfo <- Array.of_list minfo;
4440 else
4441 let (s, _, _) as o = m_items.(n) in
4442 let accu, minfo =
4443 match Str.search_forward re s 0 with
4444 | (exception Not_found) -> accu, minfo
4445 | first -> o :: accu, (first, Str.match_end ()) :: minfo
4447 loop accu minfo (n-1)
4449 loop [] [] (Array.length m_items - 1)
4451 method! getminfo = m_minfo
4453 method denarrow =
4454 m_orig_items <- fetchoutlines ();
4455 m_minfo <- m_orig_minfo;
4456 m_items <- m_orig_items
4458 method add_narrow_pattern pattern =
4459 m_narrow_patterns <- pattern :: m_narrow_patterns
4461 method del_narrow_pattern =
4462 match m_narrow_patterns with
4463 | _ :: rest -> m_narrow_patterns <- rest
4464 | [] -> ()
4466 method renarrow =
4467 self#denarrow;
4468 match m_narrow_patterns with
4469 | pattern :: [] -> self#narrow pattern; pattern
4470 | list ->
4471 List.fold_left (fun accu pattern ->
4472 self#narrow pattern;
4473 pattern ^ "@Uellipsis" ^ accu) E.s list
4475 method calcactive (_:anchor) = 0
4477 method reset anchor items =
4478 if state.gen != m_gen
4479 then (
4480 m_orig_items <- items;
4481 m_items <- items;
4482 m_narrow_patterns <- [];
4483 m_minfo <- E.a;
4484 m_orig_minfo <- E.a;
4485 m_gen <- state.gen;
4487 else (
4488 if items != m_orig_items
4489 then (
4490 m_orig_items <- items;
4491 if m_narrow_patterns == []
4492 then m_items <- items;
4495 let active = self#calcactive anchor in
4496 m_active <- active;
4497 m_first <- firstof m_first active
4501 let outlinesource fetchoutlines =
4502 (object
4503 inherit outlinesoucebase fetchoutlines
4504 method! calcactive anchor =
4505 let rely = getanchory anchor in
4506 let rec loop n best bestd =
4507 if n = Array.length m_items
4508 then best
4509 else
4510 let _, _, kind = m_items.(n) in
4511 match kind with
4512 | Oanchor anchor ->
4513 let orely = getanchory anchor in
4514 let d = abs (orely - rely) in
4515 if d < bestd
4516 then loop (n+1) n d
4517 else loop (n+1) best bestd
4518 | Onone | Oremote _ | Olaunch _
4519 | Oremotedest _ | Ouri _ | Ohistory _ ->
4520 loop (n+1) best bestd
4522 loop 0 ~-1 max_int
4523 end)
4526 let enteroutlinemode, enterbookmarkmode, enterhistmode =
4527 let mkselector sourcetype =
4528 let fetchoutlines () =
4529 match sourcetype with
4530 | `bookmarks -> Array.of_list state.bookmarks
4531 | `outlines -> state.outlines
4532 | `history -> genhistoutlines ()
4534 let source =
4535 if sourcetype = `history
4536 then new outlinesoucebase fetchoutlines
4537 else outlinesource fetchoutlines
4539 fun errmsg ->
4540 let outlines = fetchoutlines () in
4541 if Array.length outlines = 0
4542 then (
4543 showtext ' ' errmsg;
4545 else (
4546 resetmstate ();
4547 Wsi.setcursor Wsi.CURSOR_INHERIT;
4548 let anchor = getanchor () in
4549 source#reset anchor outlines;
4550 state.text <- source#greetmsg;
4551 state.uioh <-
4552 coe (new outlinelistview ~zebra:(sourcetype=`history) ~source);
4553 G.postRedisplay "enter selector";
4556 let mkenter sourcetype errmsg =
4557 let enter = mkselector sourcetype in
4558 fun () -> enter errmsg
4560 mkenter `outlines "document has no outline"
4561 , mkenter `bookmarks "document has no bookmarks (yet)"
4562 , mkenter `history "history is empty"
4565 let quickbookmark ?title () =
4566 match state.layout with
4567 | [] -> ()
4568 | l :: _ ->
4569 let title =
4570 match title with
4571 | None ->
4572 let tm = Unix.localtime (now ()) in
4573 Printf.sprintf
4574 "Quick (page %d) (bookmarked at %02d/%02d/%d %02d:%02d)"
4575 (l.pageno+1)
4576 tm.Unix.tm_mday
4577 (tm.Unix.tm_mon+1)
4578 (tm.Unix.tm_year + 1900)
4579 tm.Unix.tm_hour
4580 tm.Unix.tm_min
4581 | Some title -> title
4583 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4586 let setautoscrollspeed step goingdown =
4587 let incr = max 1 ((abs step) / 2) in
4588 let incr = if goingdown then incr else -incr in
4589 let astep = boundastep state.winh (step + incr) in
4590 state.autoscroll <- Some astep;
4593 let canpan () =
4594 match conf.columns with
4595 | Csplit _ -> true
4596 | Csingle _ | Cmulti _ -> state.x != 0 || conf.zoom > 1.0
4599 let panbound x = bound x (-state.w) state.winw;;
4601 let existsinrow pageno (columns, coverA, coverB) p =
4602 let last = ((pageno - coverA) mod columns) + columns in
4603 let rec any = function
4604 | [] -> false
4605 | l :: rest ->
4606 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4607 then p l
4608 else (
4609 if not (p l)
4610 then (if l.pageno = last then false else any rest)
4611 else true
4614 any state.layout
4617 let nextpage () =
4618 match state.layout with
4619 | [] ->
4620 let pageno = page_of_y state.y in
4621 gotoghyll (getpagey (pageno+1))
4622 | l :: rest ->
4623 match conf.columns with
4624 | Csingle _ ->
4625 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4626 then
4627 let y = clamp (pgscale state.winh) in
4628 gotoghyll y
4629 else
4630 let pageno = min (l.pageno+1) (state.pagecount-1) in
4631 gotoghyll (getpagey pageno)
4632 | Cmulti ((c, _, _) as cl, _) ->
4633 if conf.presentation
4634 && (existsinrow l.pageno cl
4635 (fun l -> 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+c) (state.pagecount-1) in
4641 gotoghyll (getpagey pageno)
4642 | Csplit (n, _) ->
4643 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4644 then
4645 let pagey, pageh = getpageyh l.pageno in
4646 let pagey = pagey + pageh * l.pagecol in
4647 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4648 gotoghyll (pagey + pageh + ips)
4651 let prevpage () =
4652 match state.layout with
4653 | [] ->
4654 let pageno = page_of_y state.y in
4655 gotoghyll (getpagey (pageno-1))
4656 | l :: _ ->
4657 match conf.columns with
4658 | Csingle _ ->
4659 if conf.presentation && l.pagey != 0
4660 then
4661 gotoghyll (clamp (pgscale ~-(state.winh)))
4662 else
4663 let pageno = max 0 (l.pageno-1) in
4664 gotoghyll (getpagey pageno)
4665 | Cmulti ((c, _, coverB) as cl, _) ->
4666 if conf.presentation &&
4667 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4668 then
4669 gotoghyll (clamp (pgscale ~-(state.winh)))
4670 else
4671 let decr =
4672 if l.pageno = state.pagecount - coverB
4673 then 1
4674 else c
4676 let pageno = max 0 (l.pageno-decr) in
4677 gotoghyll (getpagey pageno)
4678 | Csplit (n, _) ->
4679 let y =
4680 if l.pagecol = 0
4681 then
4682 if l.pageno = 0
4683 then l.pagey
4684 else
4685 let pageno = max 0 (l.pageno-1) in
4686 let pagey, pageh = getpageyh pageno in
4687 pagey + (n-1)*pageh
4688 else
4689 let pagey, pageh = getpageyh l.pageno in
4690 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4692 gotoghyll y
4695 let save () =
4696 if emptystr conf.savecmd
4697 then error "don't know where to save modified document"
4698 else
4699 let savecmd = Str.global_replace percentsre state.path conf.savecmd in
4700 let path =
4701 getcmdoutput
4702 (fun s -> error "failed to obtain path to the saved copy: %s" s)
4703 savecmd
4705 if nonemptystr path
4706 then
4707 let tmp = path ^ ".tmp" in
4708 savedoc tmp;
4709 Unix.rename tmp path;
4712 let viewkeyboard key mask =
4713 let enttext te =
4714 let mode = state.mode in
4715 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4716 state.text <- E.s;
4717 enttext ();
4718 G.postRedisplay "view:enttext"
4720 let ctrl = Wsi.withctrl mask in
4721 let key = Wsi.keypadtodigitkey key in
4722 match key with
4723 | @Q -> exit 0
4725 | @W ->
4726 if hasunsavedchanges ()
4727 then save ()
4729 | @insert ->
4730 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4731 then (
4732 state.mode <- (
4733 match state.lnava with
4734 | None -> LinkNav (Ltgendir 0)
4735 | Some pn -> LinkNav (Ltexact pn)
4737 gotoxy state.x state.y;
4739 else impmsg "keyboard link navigation does not work under rotation"
4741 | @escape | @q ->
4742 begin match state.mstate with
4743 | Mzoomrect _ ->
4744 resetmstate ();
4745 G.postRedisplay "kill rect";
4746 | Msel _
4747 | Mpan _
4748 | Mscrolly | Mscrollx
4749 | Mzoom _
4750 | Mnone ->
4751 begin match state.mode with
4752 | LinkNav _ ->
4753 state.mode <- View;
4754 G.postRedisplay "esc leave linknav"
4755 | Birdseye _
4756 | Textentry _
4757 | View ->
4758 match state.ranchors with
4759 | [] -> raise Quit
4760 | (path, password, anchor, origin) :: rest ->
4761 state.ranchors <- rest;
4762 state.anchor <- anchor;
4763 state.origin <- origin;
4764 state.nameddest <- E.s;
4765 opendoc path password
4766 end;
4767 end;
4769 | @backspace ->
4770 gotoghyll (getnav ~-1)
4772 | @o ->
4773 enteroutlinemode ()
4775 | @H ->
4776 enterhistmode ()
4778 | @u ->
4779 state.rects <- [];
4780 state.text <- E.s;
4781 Hashtbl.iter (fun _ opaque ->
4782 clearmark opaque;
4783 Hashtbl.clear state.prects) state.pagemap;
4784 G.postRedisplay "dehighlight";
4786 | @slash | @question ->
4787 let ondone isforw s =
4788 cbput state.hists.pat s;
4789 state.searchpattern <- s;
4790 search s isforw
4792 let s = String.make 1 (Char.chr key) in
4793 enttext (s, E.s, Some (onhist state.hists.pat),
4794 textentry, ondone (key = @slash), true)
4796 | @plus | @kpplus | @equals when ctrl ->
4797 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4798 pivotzoom (conf.zoom +. incr)
4800 | @plus | @kpplus ->
4801 let ondone s =
4802 let n =
4803 try int_of_string s with exn ->
4804 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn;
4805 max_int
4807 if n != max_int
4808 then (
4809 conf.pagebias <- n;
4810 state.text <- "page bias is now " ^ string_of_int n;
4813 enttext ("page bias: ", E.s, None, intentry, ondone, true)
4815 | @minus | @kpminus when ctrl ->
4816 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4817 pivotzoom (max 0.01 (conf.zoom -. decr))
4819 | @minus | @kpminus ->
4820 let ondone msg = state.text <- msg in
4821 enttext (
4822 "option [acfhilpstvxACFPRSZTISM]: ", E.s, None,
4823 optentry state.mode, ondone, true
4826 | @0 when ctrl ->
4827 if conf.zoom = 1.0
4828 then gotoxy 0 state.y
4829 else setzoom 1.0
4831 | (@1 | @2) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
4832 let cols =
4833 match conf.columns with
4834 | Csingle _ | Cmulti _ -> 1
4835 | Csplit (n, _) -> n
4837 let h = state.winh -
4838 conf.interpagespace lsl (if conf.presentation then 1 else 0)
4840 let zoom = zoomforh state.winw h 0 cols in
4841 if zoom > 0.0 && (key = @2 || zoom < 1.0)
4842 then setzoom zoom
4844 | @3 when ctrl ->
4845 let fm =
4846 match conf.fitmodel with
4847 | FitWidth -> FitProportional
4848 | FitProportional -> FitPage
4849 | FitPage -> FitWidth
4851 state.text <- "fit model: " ^ FMTE.to_string fm;
4852 reqlayout conf.angle fm
4854 | @4 when ctrl -> (* ctrl-4 *)
4855 let zoom = getmaxw () /. float state.winw in
4856 if zoom > 0.0 then setzoom zoom
4858 | @F9 ->
4859 togglebirdseye ()
4861 | @9 when ctrl ->
4862 togglebirdseye ()
4864 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4865 when not ctrl -> (* 0..9 *)
4866 let ondone s =
4867 let n =
4868 try int_of_string s with exn ->
4869 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn;
4872 if n >= 0
4873 then (
4874 addnav ();
4875 cbput state.hists.pag (string_of_int n);
4876 gotopage1 (n + conf.pagebias - 1) 0;
4879 let pageentry text key =
4880 match Char.unsafe_chr key with
4881 | 'g' -> TEdone text
4882 | _ -> intentry text key
4884 let text = String.make 1 (Char.chr key) in
4885 enttext (":", text, Some (onhist state.hists.pag),
4886 pageentry, ondone, true)
4888 | @b ->
4889 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
4890 G.postRedisplay "toggle scrollbar";
4892 | @B ->
4893 state.bzoom <- not state.bzoom;
4894 state.rects <- [];
4895 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
4897 | @l ->
4898 conf.hlinks <- not conf.hlinks;
4899 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4900 G.postRedisplay "toggle highlightlinks";
4902 | @F ->
4903 if conf.angle mod 360 = 0
4904 then (
4905 state.glinks <- true;
4906 let mode = state.mode in
4907 state.mode <-
4908 Textentry (
4909 (":", E.s, None, linknentry, linknact gotounder, false),
4910 (fun _ ->
4911 state.glinks <- false;
4912 state.mode <- mode)
4914 state.text <- E.s;
4915 G.postRedisplay "view:linkent(F)"
4917 else impmsg "hint mode does not work under rotation"
4919 | @y ->
4920 state.glinks <- true;
4921 let mode = state.mode in
4922 state.mode <-
4923 Textentry (
4925 ":", E.s, None, linknentry, linknact (fun under ->
4926 selstring (undertext under);
4927 ), false
4929 fun _ ->
4930 state.glinks <- false;
4931 state.mode <- mode
4933 state.text <- E.s;
4934 G.postRedisplay "view:linkent"
4936 | @a ->
4937 begin match state.autoscroll with
4938 | Some step ->
4939 conf.autoscrollstep <- step;
4940 state.autoscroll <- None
4941 | None ->
4942 if conf.autoscrollstep = 0
4943 then state.autoscroll <- Some 1
4944 else state.autoscroll <- Some conf.autoscrollstep
4947 | @p when ctrl ->
4948 launchpath () (* XXX where do error messages go? *)
4950 | @P ->
4951 setpresentationmode (not conf.presentation);
4952 showtext ' ' ("presentation mode " ^
4953 if conf.presentation then "on" else "off");
4955 | @f ->
4956 if List.mem Wsi.Fullscreen state.winstate
4957 then Wsi.reshape conf.cwinw conf.cwinh
4958 else Wsi.fullscreen ()
4960 | @p | @N ->
4961 search state.searchpattern false
4963 | @n | @F3 ->
4964 search state.searchpattern true
4966 | @t ->
4967 begin match state.layout with
4968 | [] -> ()
4969 | l :: _ ->
4970 gotoghyll (getpagey l.pageno)
4973 | @space ->
4974 nextpage ()
4976 | @delete | @kpdelete -> (* delete *)
4977 prevpage ()
4979 | @equals ->
4980 showtext ' ' (describe_location ());
4982 | @w ->
4983 begin match state.layout with
4984 | [] -> ()
4985 | l :: _ ->
4986 Wsi.reshape l.pagew l.pageh;
4987 G.postRedisplay "w"
4990 | @apos ->
4991 enterbookmarkmode ()
4993 | @h | @F1 ->
4994 enterhelpmode ()
4996 | @i ->
4997 enterinfomode ()
4999 | @e when Buffer.length state.errmsgs > 0 ->
5000 entermsgsmode ()
5002 | @m ->
5003 let ondone s =
5004 match state.layout with
5005 | l :: _ ->
5006 if nonemptystr s
5007 then
5008 state.bookmarks <-
5009 (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
5010 | _ -> ()
5012 enttext ("bookmark: ", E.s, None, textentry, ondone, true)
5014 | @tilde ->
5015 quickbookmark ();
5016 showtext ' ' "Quick bookmark added";
5018 | @z ->
5019 begin match state.layout with
5020 | l :: _ ->
5021 let rect = getpdimrect l.pagedimno in
5022 let w, h =
5023 if conf.crophack
5024 then
5025 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5026 truncate (1.2 *. (rect.(3) -. rect.(0))))
5027 else
5028 (truncate (rect.(1) -. rect.(0)),
5029 truncate (rect.(3) -. rect.(0)))
5031 let w = truncate ((float w)*.conf.zoom)
5032 and h = truncate ((float h)*.conf.zoom) in
5033 if w != 0 && h != 0
5034 then (
5035 state.anchor <- getanchor ();
5036 Wsi.reshape w (h + conf.interpagespace)
5038 G.postRedisplay "z";
5040 | [] -> ()
5043 | @x -> state.roam ()
5045 | @Lt | @Gt ->
5046 reqlayout (conf.angle +
5047 (if key = @Gt then 30 else -30)) conf.fitmodel
5049 | @Lb | @Rb ->
5050 conf.colorscale <-
5051 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5053 G.postRedisplay "brightness";
5055 | @c when state.mode = View ->
5056 if Wsi.withalt mask
5057 then (
5058 if conf.zoom > 1.0
5059 then
5060 let m = (state.winw - state.w) / 2 in
5061 gotoxy_and_clear_text m state.y
5063 else
5064 let (c, a, b), z =
5065 match state.prevcolumns with
5066 | None -> (1, 0, 0), 1.0
5067 | Some (columns, z) ->
5068 let cab =
5069 match columns with
5070 | Csplit (c, _) -> -c, 0, 0
5071 | Cmulti ((c, a, b), _) -> c, a, b
5072 | Csingle _ -> 1, 0, 0
5074 cab, z
5076 setcolumns View c a b;
5077 setzoom z
5079 | @down | @up when ctrl && Wsi.withshift mask ->
5080 let zoom, x = state.prevzoom in
5081 setzoom zoom;
5082 state.x <- x;
5084 | @k | @up | @kpup ->
5085 begin match state.autoscroll with
5086 | None ->
5087 begin match state.mode with
5088 | Birdseye beye -> upbirdseye 1 beye
5089 | Textentry _
5090 | View
5091 | LinkNav _ ->
5092 if ctrl
5093 then gotoxy_and_clear_text state.x (clamp ~-(state.winh/2))
5094 else (
5095 if not (Wsi.withshift mask) && conf.presentation
5096 then prevpage ()
5097 else gotoghyll1 true (clamp (-conf.scrollstep))
5100 | Some n ->
5101 setautoscrollspeed n false
5104 | @j | @down | @kpdown ->
5105 begin match state.autoscroll with
5106 | None ->
5107 begin match state.mode with
5108 | Birdseye beye -> downbirdseye 1 beye
5109 | Textentry _
5110 | View
5111 | LinkNav _ ->
5112 if ctrl
5113 then gotoxy_and_clear_text state.x (clamp (state.winh/2))
5114 else (
5115 if not (Wsi.withshift mask) && conf.presentation
5116 then nextpage ()
5117 else gotoghyll1 true (clamp (conf.scrollstep))
5120 | Some n ->
5121 setautoscrollspeed n true
5124 | @left | @right | @kpleft | @kpright when not (Wsi.withalt mask) ->
5125 if canpan ()
5126 then
5127 let dx =
5128 if ctrl
5129 then state.winw / 2
5130 else conf.hscrollstep
5132 let dx = if key = @left || key = @kpleft then dx else -dx in
5133 gotoxy_and_clear_text (panbound (state.x + dx)) state.y
5134 else (
5135 state.text <- E.s;
5136 G.postRedisplay "left/right"
5139 | @prior | @kpprior ->
5140 let y =
5141 if ctrl
5142 then
5143 match state.layout with
5144 | [] -> state.y
5145 | l :: _ -> state.y - l.pagey
5146 else
5147 clamp (pgscale (-state.winh))
5149 gotoghyll y
5151 | @next | @kpnext ->
5152 let y =
5153 if ctrl
5154 then
5155 match List.rev state.layout with
5156 | [] -> state.y
5157 | l :: _ -> getpagey l.pageno
5158 else
5159 clamp (pgscale state.winh)
5161 gotoghyll y
5163 | @g | @home | @kphome ->
5164 addnav ();
5165 gotoghyll 0
5166 | @G | @jend | @kpend ->
5167 addnav ();
5168 gotoghyll (clamp state.maxy)
5170 | @right | @kpright when Wsi.withalt mask ->
5171 gotoghyll (getnav 1)
5172 | @left | @kpleft when Wsi.withalt mask ->
5173 gotoghyll (getnav ~-1)
5175 | @r ->
5176 reload ()
5178 | @v when conf.debug ->
5179 state.rects <- [];
5180 List.iter (fun l ->
5181 match getopaque l.pageno with
5182 | None -> ()
5183 | Some opaque ->
5184 let x0, y0, x1, y1 = pagebbox opaque in
5185 let rect = (float x0, float y0,
5186 float x1, float y0,
5187 float x1, float y1,
5188 float x0, float y1) in
5189 debugrect rect;
5190 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
5191 state.rects <- (l.pageno, color, rect) :: state.rects;
5192 ) state.layout;
5193 G.postRedisplay "v";
5195 | @pipe ->
5196 let mode = state.mode in
5197 let cmd = ref E.s in
5198 let onleave = function
5199 | Cancel -> state.mode <- mode
5200 | Confirm ->
5201 List.iter (fun l ->
5202 match getopaque l.pageno with
5203 | Some opaque -> pipesel opaque !cmd
5204 | None -> ()) state.layout;
5205 state.mode <- mode
5207 let ondone s =
5208 cbput state.hists.sel s;
5209 cmd := s
5211 let te =
5212 "| ", !cmd, Some (onhist state.hists.sel), textentry, ondone, true
5214 G.postRedisplay "|";
5215 state.mode <- Textentry (te, onleave);
5217 | _ ->
5218 vlog "huh? %s" (Wsi.keyname key)
5221 let linknavkeyboard key mask linknav =
5222 let getpage pageno =
5223 let rec loop = function
5224 | [] -> None
5225 | l :: _ when l.pageno = pageno -> Some l
5226 | _ :: rest -> loop rest
5227 in loop state.layout
5229 let doexact (pageno, n) =
5230 match getopaque pageno, getpage pageno with
5231 | Some opaque, Some l ->
5232 if key = @enter || key = @kpenter
5233 then
5234 let under = getlink opaque n in
5235 G.postRedisplay "link gotounder";
5236 gotounder under;
5237 state.mode <- View;
5238 else
5239 let opt, dir =
5240 match key with
5241 | @home ->
5242 Some (findlink opaque LDfirst), -1
5244 | @jend ->
5245 Some (findlink opaque LDlast), 1
5247 | @left ->
5248 Some (findlink opaque (LDleft n)), -1
5250 | @right ->
5251 Some (findlink opaque (LDright n)), 1
5253 | @up ->
5254 Some (findlink opaque (LDup n)), -1
5256 | @down ->
5257 Some (findlink opaque (LDdown n)), 1
5259 | _ -> None, 0
5261 let pwl l dir =
5262 begin match findpwl l.pageno dir with
5263 | Pwlnotfound -> ()
5264 | Pwl pageno ->
5265 let notfound dir =
5266 state.mode <- LinkNav (Ltgendir dir);
5267 let y, h = getpageyh pageno in
5268 let y =
5269 if dir < 0
5270 then y + h - state.winh
5271 else y
5273 gotoxy state.x y
5275 begin match getopaque pageno, getpage pageno with
5276 | Some opaque, Some _ ->
5277 let link =
5278 let ld = if dir > 0 then LDfirst else LDlast in
5279 findlink opaque ld
5281 begin match link with
5282 | Lfound m ->
5283 showlinktype (getlink opaque m);
5284 state.mode <- LinkNav (Ltexact (pageno, m));
5285 G.postRedisplay "linknav jpage";
5286 | Lnotfound -> notfound dir
5287 end;
5288 | _ -> notfound dir
5289 end;
5290 end;
5292 begin match opt with
5293 | Some Lnotfound -> pwl l dir;
5294 | Some (Lfound m) ->
5295 if m = n
5296 then pwl l dir
5297 else (
5298 let _, y0, _, y1 = getlinkrect opaque m in
5299 if y0 < l.pagey
5300 then gotopage1 l.pageno y0
5301 else (
5302 let d = fstate.fontsize + 1 in
5303 if y1 - l.pagey > l.pagevh - d
5304 then gotopage1 l.pageno (y1 - state.winh + d)
5305 else G.postRedisplay "linknav";
5307 showlinktype (getlink opaque m);
5308 state.mode <- LinkNav (Ltexact (l.pageno, m));
5311 | None -> viewkeyboard key mask
5312 end;
5313 | _ -> viewkeyboard key mask
5315 if key = @insert
5316 then (
5317 begin match linknav with
5318 | Ltexact pa -> state.lnava <- Some pa
5319 | Ltgendir _ | Ltnotready _ -> ()
5320 end;
5321 state.mode <- View;
5322 G.postRedisplay "leave linknav"
5324 else
5325 match linknav with
5326 | Ltgendir _ | Ltnotready _ -> viewkeyboard key mask
5327 | Ltexact exact -> doexact exact
5330 let keyboard key mask =
5331 if (key = @g && Wsi.withctrl mask) && not (istextentry state.mode)
5332 then wcmd "interrupt"
5333 else state.uioh <- state.uioh#key key mask
5336 let birdseyekeyboard key mask
5337 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5338 let incr =
5339 match conf.columns with
5340 | Csingle _ -> 1
5341 | Cmulti ((c, _, _), _) -> c
5342 | Csplit _ -> failwith "bird's eye split mode"
5344 let pgh layout = List.fold_left
5345 (fun m l -> max l.pageh m) state.winh layout in
5346 match key with
5347 | @l when Wsi.withctrl mask ->
5348 let y, h = getpageyh pageno in
5349 let top = (state.winh - h) / 2 in
5350 gotoxy state.x (max 0 (y - top))
5351 | @enter | @kpenter -> leavebirdseye beye false
5352 | @escape -> leavebirdseye beye true
5353 | @up -> upbirdseye incr beye
5354 | @down -> downbirdseye incr beye
5355 | @left -> upbirdseye 1 beye
5356 | @right -> downbirdseye 1 beye
5358 | @prior ->
5359 begin match state.layout with
5360 | l :: _ ->
5361 if l.pagey != 0
5362 then (
5363 state.mode <- Birdseye (
5364 oconf, leftx, l.pageno, hooverpageno, anchor
5366 gotopage1 l.pageno 0;
5368 else (
5369 let layout = layout state.x (state.y-state.winh)
5370 state.winw
5371 (pgh state.layout) in
5372 match layout with
5373 | [] -> gotoxy state.x (clamp (-state.winh))
5374 | l :: _ ->
5375 state.mode <- Birdseye (
5376 oconf, leftx, l.pageno, hooverpageno, anchor
5378 gotopage1 l.pageno 0
5381 | [] -> gotoxy state.x (clamp (-state.winh))
5382 end;
5384 | @next ->
5385 begin match List.rev state.layout with
5386 | l :: _ ->
5387 let layout = layout state.x
5388 (state.y + (pgh state.layout))
5389 state.winw state.winh in
5390 begin match layout with
5391 | [] ->
5392 let incr = l.pageh - l.pagevh in
5393 if incr = 0
5394 then (
5395 state.mode <-
5396 Birdseye (
5397 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5399 G.postRedisplay "birdseye pagedown";
5401 else gotoxy state.x (clamp (incr + conf.interpagespace*2));
5403 | l :: _ ->
5404 state.mode <-
5405 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5406 gotopage1 l.pageno 0;
5409 | [] -> gotoxy state.x (clamp state.winh)
5410 end;
5412 | @home ->
5413 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5414 gotopage1 0 0
5416 | @jend ->
5417 let pageno = state.pagecount - 1 in
5418 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5419 if not (pagevisible state.layout pageno)
5420 then
5421 let h =
5422 match List.rev state.pdims with
5423 | [] -> state.winh
5424 | (_, _, h, _) :: _ -> h
5426 gotoxy
5427 state.x
5428 (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5429 else G.postRedisplay "birdseye end";
5431 | _ -> viewkeyboard key mask
5434 let drawpage l =
5435 let color =
5436 match state.mode with
5437 | Textentry _ -> scalecolor 0.4
5438 | LinkNav _
5439 | View -> scalecolor 1.0
5440 | Birdseye (_, _, pageno, hooverpageno, _) ->
5441 if l.pageno = hooverpageno
5442 then scalecolor 0.9
5443 else (
5444 if l.pageno = pageno
5445 then (
5446 let c = scalecolor 1.0 in
5447 GlDraw.color c;
5448 GlDraw.line_width 3.0;
5449 let dispx = l.pagedispx in
5450 linerect
5451 (float (dispx-1)) (float (l.pagedispy-1))
5452 (float (dispx+l.pagevw+1))
5453 (float (l.pagedispy+l.pagevh+1))
5455 GlDraw.line_width 1.0;
5458 else scalecolor 0.8
5461 drawtiles l color;
5464 let postdrawpage l linkindexbase =
5465 match getopaque l.pageno with
5466 | Some opaque ->
5467 if tileready l l.pagex l.pagey
5468 then
5469 let x = l.pagedispx - l.pagex
5470 and y = l.pagedispy - l.pagey in
5471 let hlmask =
5472 match conf.columns with
5473 | Csingle _ | Cmulti _ ->
5474 (if conf.hlinks then 1 else 0)
5475 + (if state.glinks
5476 && not (isbirdseye state.mode) then 2 else 0)
5477 | Csplit _ -> 0
5479 let s =
5480 match state.mode with
5481 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5482 | Textentry _
5483 | Birdseye _
5484 | View
5485 | LinkNav _ -> E.s
5487 Hashtbl.find_all state.prects l.pageno |>
5488 List.iter (fun vals -> drawprect opaque x y vals);
5489 let n = postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize) in
5490 if n < 0
5491 then (state.redisplay <- true; 0)
5492 else n
5493 else 0
5494 | _ -> 0
5497 let scrollindicator () =
5498 let sbw, ph, sh = state.uioh#scrollph in
5499 let sbh, pw, sw = state.uioh#scrollpw in
5501 let x0,x1,hx0 =
5502 if conf.leftscroll
5503 then (0, sbw, sbw)
5504 else ((state.winw - sbw), state.winw, 0)
5507 Gl.enable `blend;
5508 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5509 GlDraw.color (0.64, 0.64, 0.64) ~alpha:0.7;
5510 filledrect (float x0) 0. (float x1) (float state.winh);
5511 filledrect
5512 (float hx0) (float (state.winh - sbh))
5513 (float (hx0 + state.winw)) (float state.winh)
5515 GlDraw.color (0.0, 0.0, 0.0) ~alpha:0.7;
5517 filledrect (float x0) ph (float x1) (ph +. sh);
5518 let pw = pw +. float hx0 in
5519 filledrect pw (float (state.winh - sbh)) (pw +. sw) (float state.winh);
5520 Gl.disable `blend;
5523 let showsel () =
5524 match state.mstate with
5525 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5528 | Msel ((x0, y0), (x1, y1)) ->
5529 let identify opaque l px py = Some (opaque, l.pageno, px, py) in
5530 let o0,n0,px0,py0 = onppundermouse identify x0 y0 (~< E.s, -1, 0, 0) in
5531 let _o1,n1,px1,py1 = onppundermouse identify x1 y1 (~< E.s, -1, 0, 0) in
5532 if n0 != -1 && n0 = n1 then seltext o0 (px0, py0, px1, py1);
5535 let showrects =
5536 function [] -> ()
5537 | rects ->
5538 Gl.enable `blend;
5539 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5540 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5541 List.iter
5542 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5543 List.iter (fun l ->
5544 if l.pageno = pageno
5545 then (
5546 let dx = float (l.pagedispx - l.pagex) in
5547 let dy = float (l.pagedispy - l.pagey) in
5548 let r, g, b, alpha = c in
5549 GlDraw.color (r, g, b) ~alpha;
5550 filledrect2 (x0+.dx) (y0+.dy)
5551 (x1+.dx) (y1+.dy)
5552 (x3+.dx) (y3+.dy)
5553 (x2+.dx) (y2+.dy);
5555 ) state.layout
5556 ) rects
5558 Gl.disable `blend;
5561 let display () =
5562 begin match conf.columns, state.layout with
5563 | Csingle _, _ :: _ ->
5564 GlDraw.color (scalecolor2 conf.bgcolor);
5565 let y =
5566 List.fold_left (fun y l ->
5567 let x0 = 0 in
5568 let y0 = y in
5569 let x1 = l.pagedispx in
5570 let y1 = (l.pagedispy + l.pagevh) in
5571 filledrect (float x0) (float y0) (float x1) (float y1);
5572 let x0 = x1 + l.pagevw in
5573 let x1 = state.winw in
5574 filledrect1 (float x0) (float y0) (float x1) (float y1);
5575 if y != l.pagedispy
5576 then (
5577 let x0 = 0
5578 and x1 = state.winw in
5579 let y0 = y
5580 and y1 = l.pagedispy in
5581 filledrect1 (float x0) (float y0) (float x1) (float y1);
5583 l.pagedispy + l.pagevh) 0 state.layout
5585 let x0 = 0
5586 and x1 = state.winw in
5587 let y0 = y
5588 and y1 = state.winh in
5589 filledrect1 (float x0) (float y0) (float x1) (float y1)
5590 | (Cmulti _ | Csplit _), _ | Csingle _, [] ->
5591 GlClear.color (scalecolor2 conf.bgcolor);
5592 GlClear.clear [`color];
5593 end;
5594 List.iter drawpage state.layout;
5595 let rects =
5596 match state.mode with
5597 | LinkNav (Ltexact (pageno, linkno)) ->
5598 begin match getopaque pageno with
5599 | Some opaque ->
5600 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5601 let color = (0.0, 0.0, 0.5, 0.5) in
5602 (pageno, color, (
5603 float x0, float y0,
5604 float x1, float y0,
5605 float x1, float y1,
5606 float x0, float y1)
5607 ) :: state.rects
5608 | None -> state.rects
5610 | LinkNav (Ltgendir _) | LinkNav (Ltnotready _)
5611 | Birdseye _
5612 | Textentry _
5613 | View -> state.rects
5615 showrects rects;
5616 let rec postloop linkindexbase = function
5617 | l :: rest ->
5618 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5619 postloop linkindexbase rest
5620 | [] -> ()
5622 showsel ();
5623 postloop 0 state.layout;
5624 state.uioh#display;
5625 begin match state.mstate with
5626 | Mzoomrect ((x0, y0), (x1, y1)) ->
5627 Gl.enable `blend;
5628 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5629 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5630 filledrect (float x0) (float y0) (float x1) (float y1);
5631 Gl.disable `blend;
5632 | Msel _
5633 | Mpan _
5634 | Mscrolly | Mscrollx
5635 | Mzoom _
5636 | Mnone -> ()
5637 end;
5638 enttext ();
5639 scrollindicator ();
5640 Wsi.swapb ();
5643 let zoomrect x y x1 y1 =
5644 let x0 = min x x1
5645 and x1 = max x x1
5646 and y0 = min y y1 in
5647 let zoom = (float state.w) /. float (x1 - x0) in
5648 let margin =
5649 let simple () =
5650 if state.w < state.winw
5651 then (state.winw - state.w) / 2
5652 else 0
5654 match conf.fitmodel with
5655 | FitWidth | FitProportional -> simple ()
5656 | FitPage ->
5657 match conf.columns with
5658 | Csplit _ ->
5659 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
5660 | Cmulti _ | Csingle _ -> simple ()
5662 gotoxy ((state.x + margin) - x0) (state.y + y0);
5663 state.anchor <- getanchor ();
5664 setzoom zoom;
5665 resetmstate ();
5668 let annot inline x y =
5669 match unproject x y with
5670 | Some (opaque, n, ux, uy) ->
5671 let add text =
5672 addannot opaque ux uy text;
5673 wcmd "freepage %s" (~> opaque);
5674 Hashtbl.remove state.pagemap (n, state.gen);
5675 flushtiles ();
5676 gotoxy state.x state.y
5678 if inline
5679 then
5680 let ondone s = add s in
5681 let mode = state.mode in
5682 state.mode <- Textentry (
5683 ("annotation: ", E.s, None, textentry, ondone, true),
5684 fun _ -> state.mode <- mode);
5685 state.text <- E.s;
5686 enttext ();
5687 G.postRedisplay "annot"
5688 else
5689 add @@ getusertext E.s
5690 | _ -> ()
5693 let zoomblock x y =
5694 let g opaque l px py =
5695 match rectofblock opaque px py with
5696 | Some a ->
5697 let x0 = a.(0) -. 20. in
5698 let x1 = a.(1) +. 20. in
5699 let y0 = a.(2) -. 20. in
5700 let zoom = (float state.w) /. (x1 -. x0) in
5701 let pagey = getpagey l.pageno in
5702 let margin = (state.w - l.pagew)/2 in
5703 let nx = -truncate x0 - margin in
5704 gotoxy_and_clear_text nx (pagey + truncate y0);
5705 state.anchor <- getanchor ();
5706 setzoom zoom;
5707 None
5708 | None -> None
5710 match conf.columns with
5711 | Csplit _ ->
5712 impmsg "block zooming does not work properly in split columns mode"
5713 | Cmulti _ | Csingle _ -> onppundermouse g x y ()
5716 let scrollx x =
5717 let winw = state.winw - 1 in
5718 let s = float x /. float winw in
5719 let destx = truncate (float (state.w + winw) *. s) in
5720 gotoxy_and_clear_text (winw - destx) state.y;
5721 state.mstate <- Mscrollx;
5724 let scrolly y =
5725 let s = float y /. float state.winh in
5726 let desty = truncate (float (state.maxy -
5727 (if conf.maxhfit then state.winh else 0))
5728 *. s) in
5729 gotoxy_and_clear_text state.x desty;
5730 state.mstate <- Mscrolly;
5733 let viewmulticlick clicks x y mask =
5734 let g opaque l px py =
5735 let mark =
5736 match clicks with
5737 | 2 -> Mark_word
5738 | 3 -> Mark_line
5739 | 4 -> Mark_block
5740 | _ -> Mark_page
5742 if markunder opaque px py mark
5743 then (
5744 Some (fun () ->
5745 let dopipe cmd =
5746 match getopaque l.pageno with
5747 | None -> ()
5748 | Some opaque -> pipesel opaque cmd
5750 state.roam <- (fun () -> dopipe conf.paxcmd);
5751 if not (Wsi.withctrl mask) then dopipe conf.selcmd;
5754 else None
5756 G.postRedisplay "viewmulticlick";
5757 onppundermouse g x y (fun () -> impmsg "nothing to select") ();
5760 let canselect () =
5761 match conf.columns with
5762 | Csplit _ -> false
5763 | Csingle _ | Cmulti _ -> conf.angle mod 360 = 0
5766 let viewmouse button down x y mask =
5767 match button with
5768 | n when (n == 4 || n == 5) && not down ->
5769 if Wsi.withctrl mask
5770 then (
5771 match state.mstate with
5772 | Mzoom (oldn, i, (ftx, fty)) ->
5773 let recenter =
5774 if false
5775 then abs (ftx - x) > 5 || abs (fty - y) > 5
5776 else false
5778 if oldn = n
5779 then (
5780 if i = 2
5781 then
5782 let incr =
5783 match n with
5784 | 5 ->
5785 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5786 | _ ->
5787 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5789 let zoom = conf.zoom -. incr in
5790 if recenter
5791 then pivotzoom ~x ~y zoom
5792 else pivotzoom zoom;
5793 state.mstate <- Mzoom (n, 0, (x, y));
5794 else
5795 state.mstate <- Mzoom (n, i+1, (ftx, fty));
5797 else state.mstate <- Mzoom (n, 0, (ftx, fty))
5799 | Msel _
5800 | Mpan _
5801 | Mscrolly | Mscrollx
5802 | Mzoomrect _
5803 | Mnone -> state.mstate <- Mzoom (n, 0, (0, 0))
5805 else (
5806 match state.autoscroll with
5807 | Some step -> setautoscrollspeed step (n=4)
5808 | None ->
5809 if conf.wheelbypage || conf.presentation
5810 then (
5811 if n = 4
5812 then prevpage ()
5813 else nextpage ()
5815 else
5816 let incr =
5817 if n = 4
5818 then -conf.scrollstep
5819 else conf.scrollstep
5821 let incr = incr * 2 in
5822 let y = clamp incr in
5823 gotoxy_and_clear_text state.x y
5826 | n when (n = 6 || n = 7) && not down && canpan () ->
5827 let x =
5828 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep) in
5829 gotoxy_and_clear_text x state.y
5831 | 1 when Wsi.withshift mask ->
5832 state.mstate <- Mnone;
5833 if not down
5834 then (
5835 match unproject x y with
5836 | None -> ()
5837 | Some (_, pageno, ux, uy) ->
5838 let cmd = Printf.sprintf
5839 "%s %s %d %d %d"
5840 conf.stcmd state.path pageno ux uy
5842 match spawn cmd [] with
5843 | (exception exn) ->
5844 impmsg "execution of synctex command(%S) failed: %S"
5845 conf.stcmd @@ exntos exn
5846 | _pid -> ()
5849 | 1 when Wsi.withctrl mask ->
5850 if down
5851 then (
5852 Wsi.setcursor Wsi.CURSOR_FLEUR;
5853 state.mstate <- Mpan (x, y)
5855 else
5856 state.mstate <- Mnone
5858 | 3 ->
5859 if down
5860 then (
5861 if Wsi.withshift mask
5862 then (
5863 annot conf.annotinline x y;
5864 G.postRedisplay "addannot"
5866 else
5867 let p = (x, y) in
5868 Wsi.setcursor Wsi.CURSOR_CYCLE;
5869 state.mstate <- Mzoomrect (p, p)
5871 else (
5872 match state.mstate with
5873 | Mzoomrect ((x0, y0), _) ->
5874 if abs (x-x0) > 10 && abs (y - y0) > 10
5875 then zoomrect x0 y0 x y
5876 else (
5877 resetmstate ();
5878 G.postRedisplay "kill accidental zoom rect";
5880 | Msel _
5881 | Mpan _
5882 | Mscrolly | Mscrollx
5883 | Mzoom _
5884 | Mnone ->
5885 resetmstate ()
5888 | 1 when vscrollhit x ->
5889 if down
5890 then
5891 let _, position, sh = state.uioh#scrollph in
5892 if y > truncate position && y < truncate (position +. sh)
5893 then state.mstate <- Mscrolly
5894 else scrolly y
5895 else
5896 state.mstate <- Mnone
5898 | 1 when y > state.winh - hscrollh () ->
5899 if down
5900 then
5901 let _, position, sw = state.uioh#scrollpw in
5902 if x > truncate position && x < truncate (position +. sw)
5903 then state.mstate <- Mscrollx
5904 else scrollx x
5905 else
5906 state.mstate <- Mnone
5908 | 1 when state.bzoom -> if not down then zoomblock x y
5910 | 1 ->
5911 let dest = if down then getunder x y else Unone in
5912 begin match dest with
5913 | Ulinkuri _ ->
5914 gotounder dest
5916 | Unone when down ->
5917 Wsi.setcursor Wsi.CURSOR_FLEUR;
5918 state.mstate <- Mpan (x, y);
5920 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
5922 | Unone | Utext _ ->
5923 if down
5924 then (
5925 if canselect ()
5926 then (
5927 state.mstate <- Msel ((x, y), (x, y));
5928 G.postRedisplay "mouse select";
5931 else (
5932 match state.mstate with
5933 | Mnone -> ()
5935 | Mzoom _ | Mscrollx | Mscrolly ->
5936 state.mstate <- Mnone
5938 | Mzoomrect ((x0, y0), _) ->
5939 zoomrect x0 y0 x y
5941 | Mpan _ ->
5942 Wsi.setcursor Wsi.CURSOR_INHERIT;
5943 state.mstate <- Mnone
5945 | Msel ((x0, y0), (x1, y1)) ->
5946 let rec loop = function
5947 | [] -> ()
5948 | l :: rest ->
5949 let inside =
5950 let a0 = l.pagedispy in
5951 let a1 = a0 + l.pagevh in
5952 let b0 = l.pagedispx in
5953 let b1 = b0 + l.pagevw in
5954 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5955 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5957 if inside
5958 then
5959 match getopaque l.pageno with
5960 | Some opaque ->
5961 let dosel cmd () =
5962 match Unix.pipe () with
5963 | (exception exn) ->
5964 impmsg "cannot create sel pipe: %s" @@
5965 exntos exn;
5966 | (r, w) ->
5967 let clo what fd =
5968 Ne.clo fd (fun msg ->
5969 dolog "%s close failed: %s" what msg)
5971 let pid =
5972 try spawn cmd [r, 0; w, -1]
5973 with exn ->
5974 dolog "cannot execute %S: %s"
5975 cmd @@ exntos exn;
5978 if pid > 0
5979 then (
5980 copysel w opaque;
5981 G.postRedisplay "copysel";
5983 else clo "Msel pipe/w" w;
5984 clo "Msel pipe/r" r;
5986 dosel conf.selcmd ();
5987 state.roam <- dosel conf.paxcmd;
5988 | None -> ()
5989 else loop rest
5991 loop state.layout;
5992 resetmstate ();
5996 | _ -> ()
5999 let birdseyemouse button down x y mask
6000 (conf, leftx, _, hooverpageno, anchor) =
6001 match button with
6002 | 1 when down ->
6003 let rec loop = function
6004 | [] -> ()
6005 | l :: rest ->
6006 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6007 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6008 then (
6009 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
6011 else loop rest
6013 loop state.layout
6014 | 3 -> ()
6015 | _ -> viewmouse button down x y mask
6018 let uioh = object
6019 method display = ()
6021 method key key mask =
6022 begin match state.mode with
6023 | Textentry textentry -> textentrykeyboard key mask textentry
6024 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6025 | View -> viewkeyboard key mask
6026 | LinkNav linknav -> linknavkeyboard key mask linknav
6027 end;
6028 state.uioh
6030 method button button bstate x y mask =
6031 begin match state.mode with
6032 | LinkNav _
6033 | View -> viewmouse button bstate x y mask
6034 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6035 | Textentry _ -> ()
6036 end;
6037 state.uioh
6039 method multiclick clicks x y mask =
6040 begin match state.mode with
6041 | LinkNav _
6042 | View -> viewmulticlick clicks x y mask
6043 | Birdseye _
6044 | Textentry _ -> ()
6045 end;
6046 state.uioh
6048 method motion x y =
6049 begin match state.mode with
6050 | Textentry _ -> ()
6051 | View | Birdseye _ | LinkNav _ ->
6052 match state.mstate with
6053 | Mzoom _ | Mnone -> ()
6055 | Mpan (x0, y0) ->
6056 let dx = x - x0
6057 and dy = y0 - y in
6058 state.mstate <- Mpan (x, y);
6059 let x = if canpan () then panbound (state.x + dx) else state.x in
6060 let y = clamp dy in
6061 gotoxy_and_clear_text x y
6063 | Msel (a, _) ->
6064 state.mstate <- Msel (a, (x, y));
6065 G.postRedisplay "motion select";
6067 | Mscrolly ->
6068 let y = min state.winh (max 0 y) in
6069 scrolly y
6071 | Mscrollx ->
6072 let x = min state.winw (max 0 x) in
6073 scrollx x
6075 | Mzoomrect (p0, _) ->
6076 state.mstate <- Mzoomrect (p0, (x, y));
6077 G.postRedisplay "motion zoomrect";
6078 end;
6079 state.uioh
6081 method pmotion x y =
6082 begin match state.mode with
6083 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6084 let rec loop = function
6085 | [] ->
6086 if hooverpageno != -1
6087 then (
6088 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6089 G.postRedisplay "pmotion birdseye no hoover";
6091 | l :: rest ->
6092 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6093 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6094 then (
6095 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6096 G.postRedisplay "pmotion birdseye hoover";
6098 else loop rest
6100 loop state.layout
6102 | Textentry _ -> ()
6104 | LinkNav _
6105 | View ->
6106 match state.mstate with
6107 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ -> ()
6108 | Mnone ->
6109 updateunder x y;
6110 if canselect ()
6111 then
6112 match conf.pax with
6113 | None -> ()
6114 | Some r ->
6115 let past, _, _ = !r in
6116 let now = now () in
6117 let delta = now -. past in
6118 if delta > 0.01
6119 then paxunder x y
6120 else r := (now, x, y)
6121 end;
6122 state.uioh
6124 method infochanged _ = ()
6126 method scrollph =
6127 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6128 let p, h =
6129 if maxy = 0
6130 then 0.0, float state.winh
6131 else scrollph state.y maxy
6133 vscrollw (), p, h
6135 method scrollpw =
6136 let fwinw = float (state.winw - vscrollw ()) in
6137 let sw =
6138 let sw = fwinw /. float state.w in
6139 let sw = fwinw *. sw in
6140 max sw (float conf.scrollh)
6142 let position =
6143 let maxx = state.w + state.winw in
6144 let x = state.winw - state.x in
6145 let percent = float x /. float maxx in
6146 (fwinw -. sw) *. percent
6148 hscrollh (), position, sw
6150 method modehash =
6151 let modename =
6152 match state.mode with
6153 | LinkNav _ -> "links"
6154 | Textentry _ -> "textentry"
6155 | Birdseye _ -> "birdseye"
6156 | View -> "view"
6158 findkeyhash conf modename
6160 method eformsgs = true
6161 method alwaysscrolly = false
6162 end;;
6164 let addrect pageno r g b a x0 y0 x1 y1 =
6165 Hashtbl.add state.prects pageno [|r; g; b; a; x0; y0; x1; y1|];
6168 let ract cmds =
6169 let cl = splitatchar cmds ' ' in
6170 let scan s fmt f =
6171 try Scanf.sscanf s fmt f
6172 with exn ->
6173 adderrfmt "remote exec"
6174 "error processing '%S': %s\n" cmds @@ exntos exn
6176 let rectx s pageno (r, g, b, a) x0 y0 x1 y1 =
6177 vlog "%s page %d color (%f %f %f %f) x0,y0,x1,y1 = %f %f %f %f"
6178 s pageno r g b a x0 y0 x1 y1;
6179 onpagerect
6180 pageno
6181 (fun w h ->
6182 let _,w1,h1,_ = getpagedim pageno in
6183 let sw = float w1 /. float w
6184 and sh = float h1 /. float h in
6185 let x0s = x0 *. sw
6186 and x1s = x1 *. sw
6187 and y0s = y0 *. sh
6188 and y1s = y1 *. sh in
6189 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
6190 let color = (r, g, b, a) in
6191 if conf.verbose then debugrect rect;
6192 state.rects <- (pageno, color, rect) :: state.rects;
6193 G.postRedisplay s;
6196 match cl with
6197 | "reload", "" -> reload ()
6198 | "goto", args ->
6199 scan args "%u %f %f"
6200 (fun pageno x y ->
6201 let cmd, _ = state.geomcmds in
6202 if emptystr cmd
6203 then gotopagexy !wtmode pageno x y
6204 else
6205 let f prevf () =
6206 gotopagexy !wtmode pageno x y;
6207 prevf ()
6209 state.reprf <- f state.reprf
6211 | "goto1", args -> scan args "%u %f" gotopage
6212 | "gotor", args ->
6213 scan args "%S %u"
6214 (fun _filename _pageno ->
6215 failwith "gotounder (Uremote (filename, pageno))")
6216 | "gotord", args ->
6217 scan args "%S %S"
6218 (fun _filename _dest ->
6219 failwith "gotounder (Uremotedest (filename, dest))")
6220 | "rect", args ->
6221 scan args "%u %u %f %f %f %f"
6222 (fun pageno c x0 y0 x1 y1 ->
6223 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
6224 rectx "rect" pageno color x0 y0 x1 y1;
6226 | "prect", args ->
6227 scan args "%u %f %f %f %f %f %f %f %f"
6228 (fun pageno r g b alpha x0 y0 x1 y1 ->
6229 addrect pageno r g b alpha x0 y0 x1 y1;
6230 G.postRedisplay "prect"
6232 | "pgoto", args ->
6233 scan args "%u %f %f"
6234 (fun pageno x y ->
6235 let optopaque =
6236 match getopaque pageno with
6237 | Some opaque -> opaque
6238 | None -> ~< E.s
6240 pgoto optopaque pageno x y;
6241 let rec fixx = function
6242 | [] -> ()
6243 | l :: rest ->
6244 if l.pageno = pageno
6245 then gotoxy (state.x - l.pagedispx) state.y
6246 else fixx rest
6248 let layout =
6249 let mult =
6250 match conf.columns with
6251 | Csingle _ | Csplit _ -> 1
6252 | Cmulti ((n, _, _), _) -> n
6254 layout 0 state.y (state.winw * mult) state.winh
6256 fixx layout
6258 | "activatewin", "" -> Wsi.activatewin ()
6259 | "quit", "" -> raise Quit
6260 | "keys", keys ->
6261 begin try
6262 let l = Config.keys_of_string keys in
6263 List.iter (fun (k, m) -> keyboard k m) l
6264 with exn ->
6265 adderrfmt "error processing keys" "`%S': %s\n" cmds @@ exntos exn
6267 | "clearrects", "" ->
6268 Hashtbl.clear state.prects;
6269 G.postRedisplay "clearrects"
6270 | _ ->
6271 adderrfmt "remote command"
6272 "error processing remote command: %S\n" cmds;
6275 let remote =
6276 let scratch = Bytes.create 80 in
6277 let buf = Buffer.create 80 in
6278 fun fd ->
6279 match tempfailureretry (Unix.read fd scratch 0) 80 with
6280 | (exception Unix.Unix_error (Unix.EAGAIN, _, _)) -> None
6281 | 0 ->
6282 Unix.close fd;
6283 if Buffer.length buf > 0
6284 then (
6285 let s = Buffer.contents buf in
6286 Buffer.clear buf;
6287 ract s;
6289 None
6290 | n ->
6291 let rec eat ppos =
6292 let nlpos =
6293 match Bytes.index_from scratch ppos '\n' with
6294 | pos -> if pos >= n then -1 else pos
6295 | (exception Not_found) -> -1
6297 if nlpos >= 0
6298 then (
6299 Buffer.add_subbytes buf scratch ppos (nlpos-ppos);
6300 let s = Buffer.contents buf in
6301 Buffer.clear buf;
6302 ract s;
6303 eat (nlpos+1);
6305 else (
6306 Buffer.add_subbytes buf scratch ppos (n-ppos);
6307 Some fd
6309 in eat 0
6312 let remoteopen path =
6313 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
6314 with exn ->
6315 adderrfmt "remoteopen" "error opening %S: %s" path @@ exntos exn;
6316 None
6319 let () =
6320 let gcconfig = ref E.s in
6321 let trimcachepath = ref E.s in
6322 let rcmdpath = ref E.s in
6323 let pageno = ref None in
6324 let rootwid = ref 0 in
6325 let openlast = ref false in
6326 let nofc = ref false in
6327 let doreap = ref false in
6328 let csspath = ref None in
6329 selfexec := Sys.executable_name;
6330 Arg.parse
6331 (Arg.align
6332 [("-p", Arg.String (fun s -> state.password <- s),
6333 "<password> Set password");
6335 ("-f", Arg.String
6336 (fun s ->
6337 Config.fontpath := s;
6338 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
6340 "<path> Set path to the user interface font");
6342 ("-c", Arg.String
6343 (fun s ->
6344 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
6345 Config.confpath := s),
6346 "<path> Set path to the configuration file");
6348 ("-last", Arg.Set openlast, " Open last document");
6350 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
6351 "<page-number> Jump to page");
6353 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6354 "<path> Set path to the trim cache file");
6356 ("-dest", Arg.String (fun s -> state.nameddest <- s),
6357 "<named-destination> Set named destination");
6359 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
6360 ("-cxack", Arg.Set cxack, " Cut corners");
6362 ("-remote", Arg.String (fun s -> rcmdpath := s),
6363 "<path> Set path to the remote commands source");
6365 ("-origin", Arg.String (fun s -> state.origin <- s),
6366 "<original-path> Set original path");
6368 ("-gc", Arg.Set_string gcconfig,
6369 "<script-path> Collect garbage with the help of a script");
6371 ("-nofc", Arg.Set nofc, " Do not use fontconfig");
6373 ("-v", Arg.Unit (fun () ->
6374 Printf.printf
6375 "%s\nconfiguration path: %s\n"
6376 (version ())
6377 Config.defconfpath
6379 exit 0), " Print version and exit");
6381 ("-css", Arg.String (fun s -> csspath := Some s),
6382 "<css-path> Style sheet to use for EPUB/HTML");
6384 ("-embed", Arg.Set_int rootwid,
6385 "<window-id> Embed into window")
6388 (fun s -> state.path <- s)
6389 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:");
6391 if !wtmode
6392 then selfexec := !selfexec ^ " -wtmode";
6394 let histmode = emptystr state.path && not !openlast in
6396 if not (Config.load !openlast)
6397 then dolog "failed to load configuration";
6399 begin match !pageno with
6400 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
6401 | None -> ()
6402 end;
6404 if nonemptystr !gcconfig
6405 then (
6406 let (c, s) =
6407 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6408 | (exception exn) -> error "socketpair for gc failed: %s" @@ exntos exn
6409 | fds -> fds
6411 match spawn !gcconfig [(c, 0); (c, 1); (s, -1)] with
6412 | (exception exn) -> error "failed to execute gc script: %s" @@ exntos exn
6413 | _pid ->
6414 Ne.clo c @@ (fun s -> error "failed to close gc fd %s" s);
6415 Config.gc s;
6416 exit 0
6419 let mu =
6420 object (self)
6421 val mutable m_clicks = 0
6422 val mutable m_click_x = 0
6423 val mutable m_click_y = 0
6424 val mutable m_lastclicktime = infinity
6426 method private cleanup =
6427 state.roam <- noroam;
6428 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap
6429 method expose = G.postRedisplay "expose"
6430 method visible v =
6431 let name =
6432 match v with
6433 | Wsi.Unobscured -> "unobscured"
6434 | Wsi.PartiallyObscured -> "partiallyobscured"
6435 | Wsi.FullyObscured -> "fullyobscured"
6437 vlog "visibility change %s" name
6438 method display = display ()
6439 method map mapped = vlog "mapped %b" mapped
6440 method reshape w h =
6441 self#cleanup;
6442 reshape w h
6443 method mouse b d x y m =
6444 if d && canselect ()
6445 then (
6446 (* http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx *)
6447 m_click_x <- x;
6448 m_click_y <- y;
6449 if b = 1
6450 then (
6451 let t = now () in
6452 if abs x - m_click_x > 10
6453 || abs y - m_click_y > 10
6454 || abs_float (t -. m_lastclicktime) > 0.3
6455 then m_clicks <- 0;
6456 m_clicks <- m_clicks + 1;
6457 m_lastclicktime <- t;
6458 if m_clicks = 1
6459 then (
6460 self#cleanup;
6461 G.postRedisplay "cleanup";
6462 state.uioh <- state.uioh#button b d x y m;
6464 else state.uioh <- state.uioh#multiclick m_clicks x y m
6466 else (
6467 self#cleanup;
6468 m_clicks <- 0;
6469 m_lastclicktime <- infinity;
6470 state.uioh <- state.uioh#button b d x y m
6473 else (
6474 state.uioh <- state.uioh#button b d x y m
6476 method motion x y =
6477 state.mpos <- (x, y);
6478 state.uioh <- state.uioh#motion x y
6479 method pmotion x y =
6480 state.mpos <- (x, y);
6481 state.uioh <- state.uioh#pmotion x y
6482 method key k m =
6483 let mascm = m land (
6484 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6485 ) in
6486 let keyboard k m =
6487 let x = state.x and y = state.y in
6488 keyboard k m;
6489 if x != state.x || y != state.y then self#cleanup
6491 match state.keystate with
6492 | KSnone ->
6493 let km = k, mascm in
6494 begin
6495 match
6496 let modehash = state.uioh#modehash in
6497 try Hashtbl.find modehash km
6498 with Not_found ->
6499 try Hashtbl.find (findkeyhash conf "global") km
6500 with Not_found -> KMinsrt (k, m)
6501 with
6502 | KMinsrt (k, m) -> keyboard k m
6503 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6504 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6506 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6507 List.iter (fun (k, m) -> keyboard k m) insrt;
6508 state.keystate <- KSnone
6509 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6510 state.keystate <- KSinto (keys, insrt)
6511 | KSinto _ -> state.keystate <- KSnone
6513 method enter x y =
6514 state.mpos <- (x, y);
6515 state.uioh <- state.uioh#pmotion x y
6516 method leave = state.mpos <- (-1, -1)
6517 method winstate wsl = state.winstate <- wsl
6518 method quit = raise Quit
6521 let wsfd, winw, winh = Wsi.init mu !rootwid conf.cwinw conf.cwinh platform in
6523 setbgcol conf.bgcolor;
6524 state.wsfd <- wsfd;
6526 if not @@ List.exists GlMisc.check_extension
6527 [ "GL_ARB_texture_rectangle"
6528 ; "GL_EXT_texture_recangle"
6529 ; "GL_NV_texture_rectangle" ]
6530 then (dolog "OpenGL does not suppport rectangular textures"; exit 1);
6532 if substratis (GlMisc.get_string `renderer) 0 "Mesa DRI Intel("
6533 then (
6534 defconf.sliceheight <- 1024;
6535 defconf.texcount <- 32;
6536 defconf.usepbo <- true;
6539 let cs, ss =
6540 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6541 | (exception exn) ->
6542 dolog "socketpair failed: %s" @@ exntos exn;
6543 exit 1
6544 | (r, w) ->
6545 cloexec r;
6546 cloexec w;
6547 r, w
6550 setcheckers conf.checkers;
6552 opengl_has_pbo := GlMisc.check_extension "GL_ARB_pixel_buffer_object";
6554 begin match !csspath with
6555 | None -> ()
6556 | Some "" -> conf.css <- E.s
6557 | Some path ->
6558 let css = filecontents path in
6559 let l = String.length css in
6560 conf.css <-
6561 if substratis css (l-2) "\r\n"
6562 then String.sub css 0 (l-2)
6563 else (if css.[l-1] = '\n'
6564 then String.sub css 0 (l-1)
6565 else css);
6566 end;
6567 init cs (
6568 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
6569 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6570 !Config.fontpath, !trimcachepath, !opengl_has_pbo, not !nofc
6572 List.iter GlArray.enable [`texture_coord; `vertex];
6573 state.ss <- ss;
6574 reshape ~firsttime:true winw winh;
6575 state.uioh <- uioh;
6576 if histmode
6577 then (
6578 Wsi.settitle "llpp (history)";
6579 enterhistmode ();
6581 else (
6582 state.text <- "Opening " ^ (mbtoutf8 state.path);
6583 opendoc state.path state.password;
6585 display ();
6586 Wsi.mapwin ();
6587 Wsi.setcursor Wsi.CURSOR_INHERIT;
6588 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
6590 let rec reap () =
6591 match Unix.waitpid [Unix.WNOHANG] ~-1 with
6592 | (exception (Unix.Unix_error (Unix.ECHILD, _, _))) -> ()
6593 | (exception exn) -> dolog "Unix.waitpid: %s" @@ exntos exn
6594 | 0, _ -> ()
6595 | _pid, _status -> reap ()
6597 Sys.set_signal Sys.sigchld (Sys.Signal_handle (fun _ -> doreap := true));
6599 let optrfd =
6600 ref (
6601 if nonemptystr !rcmdpath
6602 then remoteopen !rcmdpath
6603 else None
6607 let rec loop deadline =
6608 if !doreap
6609 then (
6610 doreap := false;
6611 reap ()
6613 let r = [state.ss; state.wsfd] in
6614 let r =
6615 match !optrfd with
6616 | None -> r
6617 | Some fd -> fd :: r
6619 if state.redisplay
6620 then (
6621 state.redisplay <- false;
6622 display ();
6624 let timeout =
6625 let now = now () in
6626 if deadline > now
6627 then (
6628 if deadline = infinity
6629 then ~-.1.0
6630 else max 0.0 (deadline -. now)
6632 else 0.0
6634 let r, _, _ =
6635 try Unix.select r [] [] timeout
6636 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6638 begin match r with
6639 | [] ->
6640 state.ghyll None;
6641 let newdeadline =
6642 if state.ghyll == noghyll
6643 then
6644 match state.autoscroll with
6645 | Some step when step != 0 ->
6646 let y = state.y + step in
6647 let fy = if conf.maxhfit then state.winh else 0 in
6648 let y =
6649 if y < 0
6650 then state.maxy - fy
6651 else if y >= state.maxy - fy then 0 else y
6653 if state.mode = View
6654 then gotoxy_and_clear_text state.x y
6655 else gotoxy state.x y;
6656 deadline +. 0.01
6657 | _ -> infinity
6658 else deadline +. 0.01
6660 loop newdeadline
6662 | l ->
6663 let rec checkfds = function
6664 | [] -> ()
6665 | fd :: rest when fd = state.ss ->
6666 let cmd = rcmd state.ss in
6667 act cmd;
6668 checkfds rest
6670 | fd :: rest when fd = state.wsfd ->
6671 Wsi.readresp fd;
6672 checkfds rest
6674 | fd :: rest when Some fd = !optrfd ->
6675 begin match remote fd with
6676 | None -> optrfd := remoteopen !rcmdpath;
6677 | opt -> optrfd := opt
6678 end;
6679 checkfds rest
6681 | _ :: rest ->
6682 dolog "select returned unknown descriptor";
6683 checkfds rest
6685 checkfds l;
6686 let newdeadline =
6687 let deadline1 =
6688 if deadline = infinity
6689 then now () +. 0.01
6690 else deadline
6692 match state.autoscroll with
6693 | Some step when step != 0 -> deadline1
6694 | _ -> if state.ghyll == noghyll then infinity else deadline1
6696 loop newdeadline
6697 end;
6699 match loop infinity with
6700 | exception Quit ->
6701 Config.save leavebirdseye;
6702 if hasunsavedchanges ()
6703 then save ()
6704 | _ -> error "umpossible - infinity reached"