Fix parallel build issue
[llpp.git] / main.ml
blobb968786a5da12462d9fe318c2364b0c3b9607c4a
1 open Utils;;
2 open Config;;
4 exception Quit;;
6 external init : Unix.file_descr -> initparams -> unit = "ml_init";;
7 external seltext : opaque -> (int * int * int * int) -> unit = "ml_seltext";;
8 external hassel : opaque -> bool = "ml_hassel";;
9 external copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
10 external getpdimrect : int -> float array = "ml_getpdimrect";;
11 external whatsunder : opaque -> int -> int -> under = "ml_whatsunder";;
12 external markunder : opaque -> int -> int -> mark -> bool = "ml_markunder";;
13 external clearmark : opaque -> unit = "ml_clearmark";;
14 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
15 external getmaxw : unit -> float = "ml_getmaxw";;
16 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
17 external measurestr : int -> string -> float = "ml_measure_string";;
18 external postprocess :
19 opaque -> int -> int -> int -> (int * string * int) -> int
20 = "ml_postprocess";;
21 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
22 external setaalevel : int -> unit = "ml_setaalevel";;
23 external realloctexts : int -> bool = "ml_realloctexts";;
24 external findlink : opaque -> linkdir -> link = "ml_findlink";;
25 external getlink : opaque -> int -> under = "ml_getlink";;
26 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
27 external getlinkcount : opaque -> int = "ml_getlinkcount";;
28 external findpwl : int -> int -> pagewithlinks = "ml_find_page_with_links";;
29 external getpbo : width -> height -> colorspace -> opaque = "ml_getpbo";;
30 external freepbo : opaque -> unit = "ml_freepbo";;
31 external unmappbo : opaque -> unit = "ml_unmappbo";;
32 external bousable : unit -> bool = "ml_bo_usable";;
33 external unproject : opaque -> int -> int -> (int * int) option
34 = "ml_unproject";;
35 external project : opaque -> int -> int -> float -> float -> (float * float)
36 = "ml_project";;
37 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
38 external rectofblock : opaque -> int -> int -> float array option
39 = "ml_rectofblock";;
40 external begintiles : unit -> unit = "ml_begintiles";;
41 external endtiles : unit -> unit = "ml_endtiles";;
42 external addannot : opaque -> int -> int -> string -> unit = "ml_addannot";;
43 external modannot : opaque -> slinkindex -> string -> unit = "ml_modannot";;
44 external delannot : opaque -> slinkindex -> unit = "ml_delannot";;
45 external hasunsavedchanges : unit -> bool = "ml_hasunsavedchanges";;
46 external savedoc : string -> unit = "ml_savedoc";;
47 external getannotcontents : opaque -> slinkindex -> string
48 = "ml_getannotcontents";;
49 external drawprect : opaque -> int -> int -> float array -> unit
50 = "ml_drawprect";;
51 external wcmd : Unix.file_descr -> bytes -> int -> unit = "ml_wcmd";;
52 external rcmd : Unix.file_descr -> string = "ml_rcmd";;
53 external uritolocation : string -> (pageno * float * float)
54 = "ml_uritolocation";;
55 external isexternallink : string -> bool = "ml_isexternallink";;
57 let selfexec = ref E.s;;
58 let opengl_has_pbo = ref false;;
60 let drawstring size x y s =
61 Gl.enable `blend;
62 Gl.enable `texture_2d;
63 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
64 ignore (drawstr size x y s);
65 Gl.disable `blend;
66 Gl.disable `texture_2d;
69 let drawstring1 size x y s =
70 drawstr size x y s;
73 let drawstring2 size x y fmt =
74 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
77 module UniSyms = struct
78 let ellipsis = "\xe2\x80\xa6";;
79 let radical = "\xe2\x88\x9a";;
80 let lguillemet = "\xc2\xab";;
81 let rguillemet = "\xc2\xbb";;
82 end;;
84 let _debugl l =
85 dolog {|l %d dim=%d {
86 WxH %dx%d
87 vWxH %dx%d
88 pagex,y %d,%d
89 dispx,y %d,%d
90 column %d
91 }|}
92 l.pageno l.pagedimno
93 l.pagew l.pageh
94 l.pagevw l.pagevh
95 l.pagex l.pagey
96 l.pagedispx l.pagedispy
97 l.pagecol
100 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
101 dolog {|rect {
102 x0,y0=(% f, % f)
103 x1,y1=(% f, % f)
104 x2,y2=(% f, % f)
105 x3,y3=(% f, % f)
106 }|} x0 y0 x1 y1 x2 y2 x3 y3;
109 let isbirdseye = function
110 | Birdseye _ -> true
111 | Textentry _ | View | LinkNav _ -> false
114 let istextentry = function
115 | Textentry _ -> true
116 | Birdseye _ | View | LinkNav _ -> false
119 let wtmode = ref false;;
120 let cxack = ref false;;
122 let pgscale h = truncate (float h *. conf.pgscale);;
124 let hscrollh () =
125 if state.uioh#alwaysscrolly || ((conf.scrollb land scrollbhv != 0)
126 && (state.w > state.winw))
127 then conf.scrollbw
128 else 0
131 let vscrollw () =
132 if state.uioh#alwaysscrolly || ((conf.scrollb land scrollbvv != 0)
133 && (state.maxy > state.winh))
134 then conf.scrollbw
135 else 0
138 let vscrollhit x =
139 if conf.leftscroll
140 then x < vscrollw ()
141 else x > state.winw - vscrollw ()
144 let setfontsize n =
145 fstate.fontsize <- n;
146 fstate.wwidth <- measurestr fstate.fontsize "w";
147 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
150 let vlog fmt =
151 if conf.verbose
152 then dolog fmt
153 else Printf.kprintf ignore fmt
156 let launchpath () =
157 if emptystr conf.pathlauncher
158 then dolog "%s" state.path
159 else (
160 let command = Str.global_replace percentsre state.path conf.pathlauncher in
161 match spawn command [] with
162 | _pid -> ()
163 | (exception exn) ->
164 dolog "failed to execute `%s': %s" command @@ exntos exn
168 module G =
169 struct
170 let postRedisplay who =
171 vlog "redisplay for [%S]" who;
172 state.redisplay <- true;
174 end;;
176 let getopaque pageno =
177 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
178 with Not_found -> None
181 let pagetranslatepoint l x y =
182 let dy = y - l.pagedispy in
183 let y = dy + l.pagey in
184 let dx = x - l.pagedispx in
185 let x = dx + l.pagex in
186 (x, y);
189 let onppundermouse g x y d =
190 let rec f = function
191 | l :: rest ->
192 begin match getopaque l.pageno with
193 | Some opaque ->
194 let x0 = l.pagedispx in
195 let x1 = x0 + l.pagevw in
196 let y0 = l.pagedispy in
197 let y1 = y0 + l.pagevh in
198 if y >= y0 && y <= y1 && x >= x0 && x <= x1
199 then
200 let px, py = pagetranslatepoint l x y in
201 match g opaque l px py with
202 | Some res -> res
203 | None -> f rest
204 else f rest
205 | _ ->
206 f rest
208 | [] -> d
210 f state.layout
213 let getunder x y =
214 let g opaque l px py =
215 if state.bzoom
216 then (
217 match rectofblock opaque px py with
218 | Some [|x0;x1;y0;y1|] ->
219 let rect = (x0, y0, x1, y0, x1, y1, x0, y1) in
220 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
221 state.rects <- [l.pageno, color, rect];
222 G.postRedisplay "getunder";
223 | _otherwise -> ()
225 let under = whatsunder opaque px py in
226 if under = Unone then None else Some under
228 onppundermouse g x y Unone
231 let unproject x y =
232 let g opaque l x y =
233 match unproject opaque x y with
234 | Some (x, y) -> Some (Some (opaque, l.pageno, x, y))
235 | None -> None
237 onppundermouse g x y None;
240 let showtext c s =
241 state.text <- Printf.sprintf "%c%s" c s;
242 G.postRedisplay "showtext";
245 let impmsg fmt =
246 Format.ksprintf (fun s -> showtext '!' s) fmt;
249 let pipesel opaque cmd =
250 if hassel opaque
251 then
252 match Unix.pipe () with
253 | (exception exn) -> dolog "pipesel cannot create pipe: %S" @@ exntos exn;
254 | (r, w) ->
255 let doclose what fd =
256 Ne.clo fd (fun msg -> dolog "%s close failed: %s" what msg)
258 let pid =
259 try spawn cmd [r, 0; w, -1]
260 with exn ->
261 dolog "cannot execute %S: %s" cmd @@ exntos exn;
264 if pid > 0
265 then (
266 copysel w opaque;
267 G.postRedisplay "pipesel";
269 else doclose "pipesel pipe/w" w;
270 doclose "pipesel pipe/r" r;
273 let paxunder x y =
274 let g opaque l px py =
275 if markunder opaque px py conf.paxmark
276 then (
277 Some (fun () ->
278 match getopaque l.pageno with
279 | None -> ()
280 | Some opaque -> pipesel opaque conf.paxcmd
283 else None
285 G.postRedisplay "paxunder";
286 if conf.paxmark = Mark_page
287 then
288 List.iter (fun l ->
289 match getopaque l.pageno with
290 | None -> ()
291 | Some opaque -> clearmark opaque) state.layout;
292 state.roam <- onppundermouse g x y (fun () -> impmsg "whoopsie daisy");
295 let selstring s =
296 match Unix.pipe () with
297 | (exception exn) -> impmsg "pipe failed: %s" @@ exntos exn
298 | (r, w) ->
299 let clo cap fd =
300 Ne.clo fd (fun msg -> impmsg "failed to close %s: %s" cap msg)
302 let pid =
303 try spawn conf.selcmd [r, 0; w, -1]
304 with exn ->
305 impmsg "failed to execute %s: %s" conf.selcmd @@ exntos exn;
308 if pid > 0
309 then (
311 let l = String.length s in
312 let bytes = Bytes.unsafe_of_string s in
313 let n = tempfailureretry (Unix.write w bytes 0) l in
314 if n != l
315 then impmsg "failed to write %d characters to sel pipe, wrote %d"
317 with exn ->
318 impmsg "failed to write to sel pipe: %s" @@ exntos exn
320 else dolog "%s" s;
321 clo "selstring pipe/r" r;
322 clo "selstring pipe/w" w;
325 let undertext = function
326 | Unone -> "none"
327 | Ulinkuri s -> s
328 | Utext s -> "font: " ^ s
329 | Uannotation (opaque, slinkindex) ->
330 "annotation: " ^ getannotcontents opaque slinkindex
333 let updateunder x y =
334 match getunder x y with
335 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
336 | Ulinkuri uri ->
337 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
338 Wsi.setcursor Wsi.CURSOR_INFO
339 | Utext s ->
340 if conf.underinfo then showtext 'f' ("ont: " ^ s);
341 Wsi.setcursor Wsi.CURSOR_TEXT
342 | Uannotation _ ->
343 if conf.underinfo then showtext 'a' "nnotation";
344 Wsi.setcursor Wsi.CURSOR_INFO
347 let showlinktype under =
348 if conf.underinfo && under != Unone
349 then showtext ' ' @@ undertext under
352 let intentry_with_suffix text key =
353 let text =
354 if key >= 32 && key < 127
355 then
356 let c = Char.chr key in
357 match c with
358 | '0' .. '9' ->
359 addchar text c
361 | 'k' | 'm' | 'g' | 'K' | 'M' | 'G' ->
362 addchar text @@ asciilower c
363 | _ ->
364 state.text <- Printf.sprintf "invalid key (%d, `%c')" key c;
365 text
366 else (
367 state.text <- Printf.sprintf "invalid key %d" key;
368 text
371 TEcont text
374 let wcmd fmt =
375 let b = Buffer.create 16 in
376 Printf.kbprintf
377 (fun b ->
378 let b = Buffer.to_bytes b in
379 wcmd state.ss b @@ Bytes.length b
380 ) b fmt
383 let nogeomcmds cmds =
384 match cmds with
385 | s, [] -> emptystr s
386 | _ -> false
389 let layoutN ((columns, coverA, coverB), b) x y sw sh =
390 let rec fold accu n =
391 if n = Array.length b
392 then accu
393 else
394 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
395 if (vy - y) > sh &&
396 (n = coverA - 1
397 || n = state.pagecount - coverB
398 || (n - coverA) mod columns = columns - 1)
399 then accu
400 else
401 let accu =
402 if vy + h > y
403 then
404 let pagey = max 0 (y - vy) in
405 let pagedispy = if pagey > 0 then 0 else vy - y in
406 let pagedispx, pagex =
407 let pdx =
408 if n = coverA - 1 || n = state.pagecount - coverB
409 then x + (sw - w) / 2
410 else dx + xoff + x
412 if pdx < 0
413 then 0, -pdx
414 else pdx, 0
416 let pagevw =
417 let vw = sw - pagedispx in
418 let pw = w - pagex in
419 min vw pw
421 let pagevh = min (h - pagey) (sh - pagedispy) in
422 if pagevw > 0 && pagevh > 0
423 then
424 let e =
425 { pageno = n
426 ; pagedimno = pdimno
427 ; pagew = w
428 ; pageh = h
429 ; pagex = pagex
430 ; pagey = pagey
431 ; pagevw = pagevw
432 ; pagevh = pagevh
433 ; pagedispx = pagedispx
434 ; pagedispy = pagedispy
435 ; pagecol = 0
438 e :: accu
439 else
440 accu
441 else
442 accu
444 fold accu (n+1)
446 if Array.length b = 0
447 then []
448 else List.rev (fold [] (page_of_y y))
451 let layoutS (columns, b) x y sw sh =
452 let rec fold accu n =
453 if n = Array.length b
454 then accu
455 else
456 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
457 if (vy - y) > sh
458 then accu
459 else
460 let accu =
461 if vy + pageh > y
462 then
463 let x = xoff + x in
464 let pagey = max 0 (y - vy) in
465 let pagedispy = if pagey > 0 then 0 else vy - y in
466 let pagedispx, pagex =
467 if px = 0
468 then (
469 if x < 0
470 then 0, -x
471 else x, 0
473 else (
474 let px = px - x in
475 if px < 0
476 then -px, 0
477 else 0, px
480 let pagecolw = pagew/columns in
481 let pagedispx =
482 if pagecolw < sw
483 then pagedispx + ((sw - pagecolw) / 2)
484 else pagedispx
486 let pagevw =
487 let vw = sw - pagedispx in
488 let pw = pagew - pagex in
489 min vw pw
491 let pagevw = min pagevw pagecolw in
492 let pagevh = min (pageh - pagey) (sh - pagedispy) in
493 if pagevw > 0 && pagevh > 0
494 then
495 let e =
496 { pageno = n/columns
497 ; pagedimno = pdimno
498 ; pagew = pagew
499 ; pageh = pageh
500 ; pagex = pagex
501 ; pagey = pagey
502 ; pagevw = pagevw
503 ; pagevh = pagevh
504 ; pagedispx = pagedispx
505 ; pagedispy = pagedispy
506 ; pagecol = n mod columns
509 e :: accu
510 else
511 accu
512 else
513 accu
515 fold accu (n+1)
517 List.rev (fold [] 0)
520 let layout x y sw sh =
521 if nogeomcmds state.geomcmds
522 then
523 match conf.columns with
524 | Csingle b -> layoutN ((1, 0, 0), b) x y sw sh
525 | Cmulti c -> layoutN c x y sw sh
526 | Csplit s -> layoutS s x y sw sh
527 else []
530 let clamp incr =
531 let y = state.y + incr in
532 let y = max 0 y in
533 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
537 let itertiles l f =
538 let tilex = l.pagex mod conf.tilew in
539 let tiley = l.pagey mod conf.tileh in
541 let col = l.pagex / conf.tilew in
542 let row = l.pagey / conf.tileh in
544 let rec rowloop row y0 dispy h =
545 if h = 0
546 then ()
547 else (
548 let dh = conf.tileh - y0 in
549 let dh = min h dh in
550 let rec colloop col x0 dispx w =
551 if w = 0
552 then ()
553 else (
554 let dw = conf.tilew - x0 in
555 let dw = min w dw in
556 f col row dispx dispy x0 y0 dw dh;
557 colloop (col+1) 0 (dispx+dw) (w-dw)
560 colloop col tilex l.pagedispx l.pagevw;
561 rowloop (row+1) 0 (dispy+dh) (h-dh)
564 if l.pagevw > 0 && l.pagevh > 0
565 then rowloop row tiley l.pagedispy l.pagevh;
568 let gettileopaque l col row =
569 let key =
570 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
572 try Some (Hashtbl.find state.tilemap key)
573 with Not_found -> None
576 let puttileopaque l col row gen colorspace angle opaque size elapsed =
577 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
578 Hashtbl.add state.tilemap key (opaque, size, elapsed)
581 let filledrect2 x0 y0 x1 y1 x2 y2 x3 y3 =
582 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x1; y1; x2; y2; x3; y3 |];
583 GlArray.vertex `two state.vraw;
584 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
587 let filledrect1 x0 y0 x1 y1 = filledrect2 x0 y0 x0 y1 x1 y0 x1 y1;;
589 let filledrect x0 y0 x1 y1 =
590 GlArray.disable `texture_coord;
591 filledrect1 x0 y0 x1 y1;
592 GlArray.enable `texture_coord;
595 let linerect x0 y0 x1 y1 =
596 GlArray.disable `texture_coord;
597 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y1; x1; y0 |];
598 GlArray.vertex `two state.vraw;
599 GlArray.draw_arrays `line_loop ~first:0 ~count:4;
600 GlArray.enable `texture_coord;
603 let drawtiles l color =
604 GlDraw.color color;
605 begintiles ();
606 let f col row x y tilex tiley w h =
607 match gettileopaque l col row with
608 | Some (opaque, _, t) ->
609 let params = x, y, w, h, tilex, tiley in
610 if conf.invert
611 then GlTex.env (`mode `blend);
612 drawtile params opaque;
613 if conf.invert
614 then GlTex.env (`mode `modulate);
615 if conf.debug
616 then (
617 endtiles ();
618 let s = Printf.sprintf
619 "%d[%d,%d] %f sec"
620 l.pageno col row t
622 let w = measurestr fstate.fontsize s in
623 GlDraw.color (0.0, 0.0, 0.0);
624 filledrect (float (x-2))
625 (float (y-2))
626 (float (x+2) +. w)
627 (float (y + fstate.fontsize + 2));
628 GlDraw.color color;
629 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
630 begintiles ();
633 | None ->
634 endtiles ();
635 let w =
636 let lw = state.winw - x in
637 min lw w
638 and h =
639 let lh = state.winh - y in
640 min lh h
642 if conf.invert
643 then GlTex.env (`mode `blend);
644 begin match state.checkerstexid with
645 | Some id ->
646 Gl.enable `texture_2d;
647 GlTex.bind_texture ~target:`texture_2d id;
648 let x0 = float x
649 and y0 = float y
650 and x1 = float (x+w)
651 and y1 = float (y+h) in
653 let tw = float w /. 16.0
654 and th = float h /. 16.0 in
655 let tx0 = float tilex /. 16.0
656 and ty0 = float tiley /. 16.0 in
657 let tx1 = tx0 +. tw
658 and ty1 = ty0 +. th in
659 Raw.sets_float state.vraw ~pos:0
660 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
661 Raw.sets_float state.traw ~pos:0
662 [| tx0; ty0; tx0; ty1; tx1; ty0; tx1; ty1 |];
663 GlArray.vertex `two state.vraw;
664 GlArray.tex_coord `two state.traw;
665 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
666 Gl.disable `texture_2d;
668 | None ->
669 GlDraw.color (1.0, 1.0, 1.0);
670 filledrect (float x) (float y) (float (x+w)) (float (y+h));
671 end;
672 if conf.invert
673 then GlTex.env (`mode `modulate);
674 if w > 128 && h > fstate.fontsize + 10
675 then (
676 let c = if conf.invert then 1.0 else 0.0 in
677 GlDraw.color (c, c, c);
678 let c, r =
679 if conf.verbose
680 then (col*conf.tilew, row*conf.tileh)
681 else col, row
683 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
685 GlDraw.color color;
686 begintiles ();
688 itertiles l f;
689 endtiles ();
692 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
694 let tilevisible1 l x y =
695 let ax0 = l.pagex
696 and ax1 = l.pagex + l.pagevw
697 and ay0 = l.pagey
698 and ay1 = l.pagey + l.pagevh in
700 let bx0 = x
701 and by0 = y in
702 let bx1 = min (bx0 + conf.tilew) l.pagew
703 and by1 = min (by0 + conf.tileh) l.pageh in
705 let rx0 = max ax0 bx0
706 and ry0 = max ay0 by0
707 and rx1 = min ax1 bx1
708 and ry1 = min ay1 by1 in
710 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
711 nonemptyintersection
714 let tilevisible layout n x y =
715 let rec findpageinlayout m = function
716 | l :: rest when l.pageno = n ->
717 tilevisible1 l x y || (
718 match conf.columns with
719 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
720 | Csplit _
721 | Csingle _
722 | Cmulti _ -> false
724 | _ :: rest -> findpageinlayout 0 rest
725 | [] -> false
727 findpageinlayout 0 layout;
730 let tileready l x y =
731 tilevisible1 l x y &&
732 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
735 let tilepage n p layout =
736 let rec loop = function
737 | l :: rest ->
738 if l.pageno = n
739 then
740 let f col row _ _ _ _ _ _ =
741 if state.currently = Idle
742 then
743 match gettileopaque l col row with
744 | Some _ -> ()
745 | None ->
746 let x = col*conf.tilew
747 and y = row*conf.tileh in
748 let w =
749 let w = l.pagew - x in
750 min w conf.tilew
752 let h =
753 let h = l.pageh - y in
754 min h conf.tileh
756 let pbo =
757 if conf.usepbo
758 then getpbo w h conf.colorspace
759 else ~< "0"
761 wcmd "tile %s %d %d %d %d %s"
762 (~> p) x y w h (~> pbo);
763 state.currently <-
764 Tiling (
765 l, p, conf.colorspace, conf.angle,
766 state.gen, col, row, conf.tilew, conf.tileh
769 itertiles l f;
770 else
771 loop rest
773 | [] -> ()
775 if nogeomcmds state.geomcmds
776 then loop layout;
779 let preloadlayout x y sw sh =
780 let y = if y < sh then 0 else y - sh in
781 let x = min 0 (x + sw) in
782 let h = sh*3 in
783 let w = sw*3 in
784 layout x y w h;
787 let load pages =
788 let rec loop pages =
789 if state.currently != Idle
790 then ()
791 else
792 match pages with
793 | l :: rest ->
794 begin match getopaque l.pageno with
795 | None ->
796 wcmd "page %d %d" l.pageno l.pagedimno;
797 state.currently <- Loading (l, state.gen);
798 | Some opaque ->
799 tilepage l.pageno opaque pages;
800 loop rest
801 end;
802 | _ -> ()
804 if nogeomcmds state.geomcmds
805 then loop pages
808 let preload pages =
809 load pages;
810 if conf.preload && state.currently = Idle
811 then load (preloadlayout state.x state.y state.winw state.winh);
814 let layoutready layout =
815 let rec fold all ls =
816 all && match ls with
817 | l :: rest ->
818 let seen = ref false in
819 let allvisible = ref true in
820 let foo col row _ _ _ _ _ _ =
821 seen := true;
822 allvisible := !allvisible &&
823 begin match gettileopaque l col row with
824 | Some _ -> true
825 | None -> false
828 itertiles l foo;
829 fold (!seen && !allvisible) rest
830 | [] -> true
832 let alltilesvisible = fold true layout in
833 alltilesvisible;
836 let gotoxy x y =
837 let y = bound y 0 state.maxy in
838 let y, layout, proceed =
839 match conf.maxwait with
840 | Some time when state.ghyll == noghyll ->
841 begin match state.throttle with
842 | None ->
843 let layout = layout x y state.winw state.winh in
844 let ready = layoutready layout in
845 if not ready
846 then (
847 load layout;
848 state.throttle <- Some (layout, y, now ());
850 else G.postRedisplay "gotoxy showall (None)";
851 y, layout, ready
852 | Some (_, _, started) ->
853 let dt = now () -. started in
854 if dt > time
855 then (
856 state.throttle <- None;
857 let layout = layout x y state.winw state.winh in
858 load layout;
859 G.postRedisplay "maxwait";
860 y, layout, true
862 else -1, [], false
865 | _ ->
866 let layout = layout x y state.winw state.winh in
867 if not !wtmode || layoutready layout
868 then G.postRedisplay "gotoxy ready";
869 y, layout, true
871 if proceed
872 then (
873 state.x <- x;
874 state.y <- y;
875 state.layout <- layout;
876 begin match state.mode with
877 | LinkNav ln ->
878 begin match ln with
879 | Ltexact (pageno, linkno) ->
880 let rec loop = function
881 | [] ->
882 state.lnava <- Some (pageno, linkno);
883 state.mode <- LinkNav (Ltgendir 0)
884 | l :: _ when l.pageno = pageno ->
885 begin match getopaque pageno with
886 | None -> state.mode <- LinkNav (Ltnotready (pageno, 0))
887 | Some opaque ->
888 let x0, y0, x1, y1 = getlinkrect opaque linkno in
889 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
890 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
891 then state.mode <- LinkNav (Ltgendir 0)
893 | _ :: rest -> loop rest
895 loop layout
896 | Ltnotready _ | Ltgendir _ -> ()
898 | Birdseye _ | Textentry _ | View -> ()
899 end;
900 begin match state.mode with
901 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
902 if not (pagevisible layout pageno)
903 then (
904 match state.layout with
905 | [] -> ()
906 | l :: _ ->
907 state.mode <- Birdseye (
908 conf, leftx, l.pageno, hooverpageno, anchor
911 | LinkNav lt ->
912 begin match lt with
913 | Ltnotready (_, dir)
914 | Ltgendir dir ->
915 let linknav =
916 let rec loop = function
917 | [] -> lt
918 | l :: rest ->
919 match getopaque l.pageno with
920 | None -> Ltnotready (l.pageno, dir)
921 | Some opaque ->
922 let link =
923 let ld =
924 if dir = 0
925 then LDfirstvisible (l.pagex, l.pagey, dir)
926 else (
927 if dir > 0 then LDfirst else LDlast
930 findlink opaque ld
932 match link with
933 | Lnotfound -> loop rest
934 | Lfound n ->
935 showlinktype (getlink opaque n);
936 Ltexact (l.pageno, n)
938 loop state.layout
940 state.mode <- LinkNav linknav
941 | Ltexact _ -> ()
943 | Textentry _ | View -> ()
944 end;
945 preload layout;
947 state.ghyll <- noghyll;
948 if conf.updatecurs
949 then (
950 let mx, my = state.mpos in
951 updateunder mx my;
955 let conttiling pageno opaque =
956 tilepage pageno opaque
957 (if conf.preload
958 then preloadlayout state.x state.y state.winw state.winh
959 else state.layout)
962 let gotoxy_and_clear_text x y =
963 if not conf.verbose then state.text <- E.s;
964 gotoxy x y;
967 let getanchory (n, top, dtop) =
968 let y, h = getpageyh n in
969 if conf.presentation
970 then
971 let ips = calcips h in
972 y + truncate (top*.float h -. dtop*.float ips) + ips;
973 else
974 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
977 let gotoanchor anchor =
978 gotoxy state.x (getanchory anchor);
981 let addnav () =
982 cbput state.hists.nav (getanchor ());
985 let getnav dir =
986 let anchor = cbgetc state.hists.nav dir in
987 getanchory anchor;
990 let gotoghyll1 single y =
991 let scroll f n a b =
992 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
993 let snake f a b =
994 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
995 if f < a
996 then s (float f /. float a)
997 else (
998 if f > b
999 then 1.0 -. s ((float (f-b) /. float (n-b)))
1000 else 1.0
1003 snake f a b
1004 and summa n a b =
1005 let ins = float a *. 0.5
1006 and outs = float (n-b) *. 0.5 in
1007 let ones = b - a in
1008 ins +. outs +. float ones
1010 let rec set nab y sy =
1011 let (_N, _A, _B), y =
1012 if single
1013 then
1014 let scl = if y > sy then 2 else -2 in
1015 let _N, _, _ = nab in
1016 (_N,0,_N), y+conf.scrollstep*scl
1017 else nab,y in
1018 let sum = summa _N _A _B in
1019 let dy = float (y - sy) in
1020 state.ghyll <- (
1021 let rec gf n y1 o =
1022 if n >= _N
1023 then state.ghyll <- noghyll
1024 else
1025 let go n =
1026 let s = scroll n _N _A _B in
1027 let y1 = y1 +. ((s *. dy) /. sum) in
1028 gotoxy_and_clear_text state.x (truncate y1);
1029 state.ghyll <- gf (n+1) y1;
1031 match o with
1032 | None -> go n
1033 | Some y' when single -> set nab y' state.y
1034 | Some y' -> set (_N/2, 1, 1) y' state.y
1036 gf 0 (float state.y)
1039 match conf.ghyllscroll with
1040 | Some nab when not conf.presentation ->
1041 if state.ghyll == noghyll
1042 then set nab y state.y
1043 else state.ghyll (Some y)
1044 | _ ->
1045 gotoxy_and_clear_text state.x y
1048 let gotoghyll = gotoghyll1 false;;
1050 let gotopage n top =
1051 let y, h = getpageyh n in
1052 let y = y + (truncate (top *. float h)) in
1053 gotoghyll y
1056 let gotopage1 n top =
1057 let y = getpagey n in
1058 let y = y + top in
1059 gotoghyll y
1062 let invalidate s f =
1063 state.redisplay <- false;
1064 state.layout <- [];
1065 state.pdims <- [];
1066 state.rects <- [];
1067 state.rects1 <- [];
1068 match state.geomcmds with
1069 | ps, [] when emptystr ps ->
1070 f ();
1071 state.geomcmds <- s, [];
1073 | ps, [] ->
1074 state.geomcmds <- ps, [s, f];
1076 | ps, (s', _) :: rest when s' = s ->
1077 state.geomcmds <- ps, ((s, f) :: rest);
1079 | ps, cmds ->
1080 state.geomcmds <- ps, ((s, f) :: cmds);
1083 let flushpages () =
1084 Hashtbl.iter (fun _ opaque ->
1085 wcmd "freepage %s" (~> opaque);
1086 ) state.pagemap;
1087 Hashtbl.clear state.pagemap;
1090 let flushtiles () =
1091 if not (Queue.is_empty state.tilelru)
1092 then (
1093 Queue.iter (fun (k, p, s) ->
1094 wcmd "freetile %s" (~> p);
1095 state.memused <- state.memused - s;
1096 Hashtbl.remove state.tilemap k;
1097 ) state.tilelru;
1098 state.uioh#infochanged Memused;
1099 Queue.clear state.tilelru;
1101 load state.layout;
1104 let stateh h =
1105 let h = truncate (float h*.conf.zoom) in
1106 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
1107 h - d
1110 let opendoc path password =
1111 state.path <- path;
1112 state.password <- password;
1113 state.gen <- state.gen + 1;
1114 state.docinfo <- [];
1115 state.outlines <- [||];
1117 flushpages ();
1118 setaalevel conf.aalevel;
1119 let titlepath =
1120 if emptystr state.origin
1121 then path
1122 else state.origin
1124 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
1125 wcmd "open %d %d %d %s\000%s\000%s\000"
1126 (btod !wtmode) (btod !cxack) (btod conf.usedoccss)
1127 path password conf.css;
1128 invalidate "reqlayout"
1129 (fun () ->
1130 wcmd "reqlayout %d %d %d %s\000"
1131 conf.angle (FMTE.to_int conf.fitmodel)
1132 (stateh state.winh) state.nameddest
1134 state.help <-
1135 let sl = keystostrlist conf in
1136 let rec loop accu =
1137 function | [] -> accu
1138 | s :: rest -> loop ((s, 0, Noaction) :: accu) rest
1139 in makehelp () @ (("", 0, Noaction) :: loop [] sl) |> Array.of_list
1142 let reload () =
1143 state.anchor <- getanchor ();
1144 opendoc state.path state.password;
1147 let scalecolor c =
1148 let c = c *. conf.colorscale in
1149 (c, c, c);
1152 let scalecolor2 (r, g, b) =
1153 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1156 let docolumns columns =
1157 match columns with
1158 | Csingle _ ->
1159 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1160 let rec loop pageno pdimno pdim y ph pdims =
1161 if pageno = state.pagecount
1162 then ()
1163 else
1164 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1165 match pdims with
1166 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1167 pdimno+1, pdim, rest
1168 | _ ->
1169 pdimno, pdim, pdims
1171 let x = max 0 (((state.winw - w) / 2) - xoff) in
1172 let y =
1173 y + (if conf.presentation
1174 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1175 else (if pageno = 0 then 0 else conf.interpagespace)
1178 a.(pageno) <- (pdimno, x, y, pdim);
1179 loop (pageno+1) pdimno pdim (y + h) h pdims
1181 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1182 conf.columns <- Csingle a;
1184 | Cmulti ((columns, coverA, coverB), _) ->
1185 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1186 let rec loop pageno pdimno pdim x y rowh pdims =
1187 let rec fixrow m =
1188 if m = pageno then () else
1189 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1190 if h < rowh
1191 then (
1192 let y = y + (rowh - h) / 2 in
1193 a.(m) <- (pdimno, x, y, pdim);
1195 fixrow (m+1)
1197 if pageno = state.pagecount
1198 then fixrow (((pageno - 1) / columns) * columns)
1199 else
1200 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1201 match pdims with
1202 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1203 pdimno+1, pdim, rest
1204 | _ ->
1205 pdimno, pdim, pdims
1207 let x, y, rowh' =
1208 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1209 then (
1210 let x = (state.winw - w) / 2 in
1211 let ips =
1212 if conf.presentation then calcips h else conf.interpagespace in
1213 x, y + ips + rowh, h
1215 else (
1216 if (pageno - coverA) mod columns = 0
1217 then (
1218 let x = max 0 (state.winw - state.w) / 2 in
1219 let y =
1220 if conf.presentation
1221 then
1222 let ips = calcips h in
1223 y + (if pageno = 0 then 0 else calcips rowh + ips)
1224 else
1225 y + (if pageno = 0 then 0 else conf.interpagespace)
1227 x, y + rowh, h
1229 else x, y, max rowh h
1232 let y =
1233 if pageno > 1 && (pageno - coverA) mod columns = 0
1234 then (
1235 let y =
1236 if pageno = columns && conf.presentation
1237 then (
1238 let ips = calcips rowh in
1239 for i = 0 to pred columns
1241 let (pdimno, x, y, pdim) = a.(i) in
1242 a.(i) <- (pdimno, x, y+ips, pdim)
1243 done;
1244 y+ips;
1246 else y
1248 fixrow (pageno - columns);
1251 else y
1253 a.(pageno) <- (pdimno, x, y, pdim);
1254 let x = x + w + xoff*2 + conf.interpagespace in
1255 loop (pageno+1) pdimno pdim x y rowh' pdims
1257 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1258 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1260 | Csplit (c, _) ->
1261 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1262 let rec loop pageno pdimno pdim y pdims =
1263 if pageno = state.pagecount
1264 then ()
1265 else
1266 let pdimno, ((_, w, h, _) as pdim), pdims =
1267 match pdims with
1268 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1269 pdimno+1, pdim, rest
1270 | _ ->
1271 pdimno, pdim, pdims
1273 let cw = w / c in
1274 let rec loop1 n x y =
1275 if n = c then y else (
1276 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1277 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1280 let y = loop1 0 0 y in
1281 loop (pageno+1) pdimno pdim y pdims
1283 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1284 conf.columns <- Csplit (c, a);
1287 let represent () =
1288 docolumns conf.columns;
1289 state.maxy <- calcheight ();
1290 if state.reprf == noreprf
1291 then (
1292 match state.mode with
1293 | Birdseye (_, _, pageno, _, _) ->
1294 let y, h = getpageyh pageno in
1295 let top = (state.winh - h) / 2 in
1296 gotoxy state.x (max 0 (y - top))
1297 | Textentry _ | View | LinkNav _ ->
1298 let y = getanchory state.anchor in
1299 let y = min y (state.maxy - state.winh) in
1300 gotoxy state.x y;
1302 else (
1303 state.reprf ();
1304 state.reprf <- noreprf;
1308 let reshape ?(firsttime=false) w h =
1309 GlDraw.viewport ~x:0 ~y:0 ~w ~h;
1310 if not firsttime && nogeomcmds state.geomcmds
1311 then state.anchor <- getanchor ();
1313 state.winw <- w;
1314 let w = truncate (float w *. conf.zoom) in
1315 let w = max w 2 in
1316 state.winh <- h;
1317 setfontsize fstate.fontsize;
1318 GlMat.mode `modelview;
1319 GlMat.load_identity ();
1321 GlMat.mode `projection;
1322 GlMat.load_identity ();
1323 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1324 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1325 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
1327 let relx =
1328 if conf.zoom <= 1.0
1329 then 0.0
1330 else float state.x /. float state.w
1332 invalidate "geometry"
1333 (fun () ->
1334 state.w <- w;
1335 if not firsttime
1336 then state.x <- truncate (relx *. float w);
1337 let w =
1338 match conf.columns with
1339 | Csingle _ -> w
1340 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1341 | Csplit (c, _) -> w * c
1343 wcmd "geometry %d %d %d"
1344 w (stateh h) (FMTE.to_int conf.fitmodel)
1348 let enttext () =
1349 let len = String.length state.text in
1350 let x0 = if conf.leftscroll then vscrollw () else 0 in
1351 let drawstring s =
1352 let hscrollh =
1353 match state.mode with
1354 | Textentry _ | View | LinkNav _ ->
1355 let h, _, _ = state.uioh#scrollpw in
1357 | Birdseye _ -> 0
1359 let rect x w =
1360 filledrect
1361 x (float (state.winh - (fstate.fontsize + 4) - hscrollh))
1362 (x+.w) (float (state.winh - hscrollh))
1365 let w = float (state.winw - 1 - vscrollw ()) in
1366 if state.progress >= 0.0 && state.progress < 1.0
1367 then (
1368 GlDraw.color (0.3, 0.3, 0.3);
1369 let w1 = w *. state.progress in
1370 rect (float x0) w1;
1371 GlDraw.color (0.0, 0.0, 0.0);
1372 rect (float x0+.w1) (float x0+.w-.w1)
1374 else (
1375 GlDraw.color (0.0, 0.0, 0.0);
1376 rect (float x0) w;
1379 GlDraw.color (1.0, 1.0, 1.0);
1380 drawstring
1381 fstate.fontsize
1382 (if conf.leftscroll then x0 + 2 else x0 + if len > 0 then 8 else 2)
1383 (state.winh - hscrollh - 5) s;
1385 let s =
1386 match state.mode with
1387 | Textentry ((prefix, text, _, _, _, _), _) ->
1388 let s =
1389 if len > 0
1390 then Printf.sprintf "%s%s_ [%s]" prefix text state.text
1391 else Printf.sprintf "%s%s_" prefix text
1395 | Birdseye _ | View | LinkNav _ -> state.text
1397 let s =
1398 if state.newerrmsgs
1399 then (
1400 if not (istextentry state.mode) && state.uioh#eformsgs
1401 then
1402 let s1 = "(press 'e' to review error messasges)" in
1403 if nonemptystr s then s ^ " " ^ s1 else s1
1404 else s
1406 else s
1408 if nonemptystr s
1409 then drawstring s
1412 let gctiles () =
1413 let len = Queue.length state.tilelru in
1414 let layout = lazy (
1415 match state.throttle with
1416 | None ->
1417 if conf.preload
1418 then preloadlayout state.x state.y state.winw state.winh
1419 else state.layout
1420 | Some (layout, _, _) ->
1421 layout
1422 ) in
1423 let rec loop qpos =
1424 if state.memused > conf.memlimit
1425 then (
1426 if qpos < len
1427 then
1428 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1429 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1430 let (_, pw, ph, _) = getpagedim n in
1431 if gen = state.gen
1432 && colorspace = conf.colorspace
1433 && angle = conf.angle
1434 && pagew = pw
1435 && pageh = ph
1436 && (
1437 let x = col*conf.tilew
1438 and y = row*conf.tileh in
1439 tilevisible (Lazy.force_val layout) n x y
1441 then Queue.push lruitem state.tilelru
1442 else (
1443 freepbo p;
1444 wcmd "freetile %s" (~> p);
1445 state.memused <- state.memused - s;
1446 state.uioh#infochanged Memused;
1447 Hashtbl.remove state.tilemap k;
1449 loop (qpos+1)
1452 loop 0
1455 let onpagerect pageno f =
1456 let b =
1457 match conf.columns with
1458 | Cmulti (_, b) -> b
1459 | Csingle b -> b
1460 | Csplit (_, b) -> b
1462 if pageno >= 0 && pageno < Array.length b
1463 then
1464 let (_, _, _, (_, w, h, _)) = b.(pageno) in
1465 f w h
1468 let gotopagexy1 wtmode pageno x y =
1469 let _,w1,h1,leftx = getpagedim pageno in
1470 let top = y /. (float h1) in
1471 let left = x /. (float w1) in
1472 let py, w, h = getpageywh pageno in
1473 let wh = state.winh in
1474 let x = left *. (float w) in
1475 let x = leftx + state.x + truncate x in
1476 let sx =
1477 if x < 0 || x >= state.winw
1478 then state.x - x
1479 else state.x
1481 let pdy = truncate (top *. float h) in
1482 let y' = py + pdy in
1483 let dy = y' - state.y in
1484 let sy =
1485 if x != state.x || not (dy > 0 && dy < wh)
1486 then (
1487 if conf.presentation
1488 then
1489 if abs (py - y') > wh
1490 then y'
1491 else py
1492 else y';
1494 else state.y
1496 if state.x != sx || state.y != sy
1497 then (
1498 let x, y =
1499 if wtmode
1500 then (
1501 let ww = state.winw in
1502 let qx = sx / ww
1503 and qy = pdy / wh in
1504 let x = qx * ww
1505 and y = py + qy * wh in
1506 let x = if -x + ww > w1 then -(w1-ww) else x
1507 and y' = if y + wh > state.maxy then state.maxy - wh else y in
1508 let y =
1509 if conf.presentation
1510 then
1511 if abs (py - y') > wh
1512 then y'
1513 else py
1514 else y';
1516 (x, y)
1518 else (sx, sy)
1520 gotoxy_and_clear_text x y;
1522 else gotoxy_and_clear_text state.x state.y;
1525 let gotopagexy wtmode pageno x y =
1526 match state.mode with
1527 | Birdseye _ -> gotopage pageno 0.0
1528 | Textentry _ | View | LinkNav _ -> gotopagexy1 wtmode pageno x y
1531 let getpassword () =
1532 let passcmd = getenvwithdef "LLPP_ASKPASS" conf.passcmd in
1533 if emptystr passcmd
1534 then E.s
1535 else getcmdoutput
1536 (fun s ->
1537 impmsg "error getting password: %s" s;
1538 dolog "%s" s) passcmd;
1541 let pgoto opaque pageno x y =
1542 let pdimno = getpdimno pageno in
1543 let x, y = project opaque pageno pdimno x y in
1544 gotopagexy false pageno x y;
1547 let act cmds =
1548 (* dolog "%S" cmds; *)
1549 let spl = splitatchar cmds ' ' in
1550 let scan s fmt f =
1551 try Scanf.sscanf s fmt f
1552 with exn ->
1553 dolog "error processing '%S': %s" cmds @@ exntos exn;
1554 exit 1
1556 let addoutline outline =
1557 match state.currently with
1558 | Outlining outlines ->
1559 state.currently <- Outlining (outline :: outlines)
1560 | Idle -> state.currently <- Outlining [outline]
1561 | Loading _
1562 | Tiling _ ->
1563 dolog "invalid outlining state";
1564 logcurrently state.currently
1566 match spl with
1567 | "clear", "" ->
1568 state.pdims <- [];
1569 state.uioh#infochanged Pdim;
1571 | "clearrects", "" ->
1572 state.rects <- state.rects1;
1573 G.postRedisplay "clearrects";
1575 | "continue", args ->
1576 let n = scan args "%u" (fun n -> n) in
1577 state.pagecount <- n;
1578 begin match state.currently with
1579 | Outlining l ->
1580 state.currently <- Idle;
1581 state.outlines <- Array.of_list (List.rev l)
1582 | Idle
1583 | Loading _
1584 | Tiling _ -> ()
1585 end;
1587 let cur, cmds = state.geomcmds in
1588 if emptystr cur
1589 then failwith "umpossible";
1591 begin match List.rev cmds with
1592 | [] ->
1593 state.geomcmds <- E.s, [];
1594 state.throttle <- None;
1595 represent ();
1596 | (s, f) :: rest ->
1597 f ();
1598 state.geomcmds <- s, List.rev rest;
1599 end;
1600 if conf.maxwait = None && not !wtmode
1601 then G.postRedisplay "continue";
1603 | "msg", args ->
1604 showtext ' ' args
1606 | "vmsg", args ->
1607 if conf.verbose
1608 then showtext ' ' args
1610 | "emsg", args ->
1611 Buffer.add_string state.errmsgs args;
1612 state.newerrmsgs <- true;
1613 G.postRedisplay "error message"
1615 | "progress", args ->
1616 let progress, text =
1617 scan args "%f %n"
1618 (fun f pos ->
1619 f, String.sub args pos (String.length args - pos))
1621 state.text <- text;
1622 state.progress <- progress;
1623 G.postRedisplay "progress"
1625 | "firstmatch", args ->
1626 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1627 scan args "%u %d %f %f %f %f %f %f %f %f"
1628 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1629 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1631 let y = (getpagey pageno) + truncate y0 in
1632 let x =
1633 if conf.zoom > 1.0
1634 then state.winw/2
1635 else state.x
1637 addnav ();
1638 gotoxy x y;
1639 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1640 state.rects1 <- [pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)]
1642 | "match", args ->
1643 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1644 scan args "%u %d %f %f %f %f %f %f %f %f"
1645 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1646 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1648 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1649 state.rects1 <-
1650 (pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1652 | "page", args ->
1653 let pageopaques, t = scan args "%s %f" (fun p t -> p, t) in
1654 let pageopaque = ~< pageopaques in
1655 begin match state.currently with
1656 | Loading (l, gen) ->
1657 vlog "page %d took %f sec" l.pageno t;
1658 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1659 begin match state.throttle with
1660 | None ->
1661 let preloadedpages =
1662 if conf.preload
1663 then preloadlayout state.x state.y state.winw state.winh
1664 else state.layout
1666 let evict () =
1667 let set =
1668 List.fold_left (fun s l -> IntSet.add l.pageno s)
1669 IntSet.empty preloadedpages
1671 let evictedpages =
1672 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1673 if not (IntSet.mem pageno set)
1674 then (
1675 wcmd "freepage %s" (~> opaque);
1676 key :: accu
1678 else accu
1679 ) state.pagemap []
1681 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1683 evict ();
1684 state.currently <- Idle;
1685 if gen = state.gen
1686 then (
1687 tilepage l.pageno pageopaque state.layout;
1688 load state.layout;
1689 load preloadedpages;
1690 let visible = pagevisible state.layout l.pageno in
1691 if visible
1692 then (
1693 match state.mode with
1694 | LinkNav (Ltnotready (pageno, dir)) ->
1695 if pageno = l.pageno
1696 then (
1697 let link =
1698 let ld =
1699 if dir = 0
1700 then LDfirstvisible (l.pagex, l.pagey, dir)
1701 else (
1702 if dir > 0 then LDfirst else LDlast
1705 findlink pageopaque ld
1707 match link with
1708 | Lnotfound -> ()
1709 | Lfound n ->
1710 showlinktype (getlink pageopaque n);
1711 state.mode <- LinkNav (Ltexact (l.pageno, n))
1713 | LinkNav (Ltgendir _)
1714 | LinkNav (Ltexact _)
1715 | View
1716 | Birdseye _
1717 | Textentry _ -> ()
1720 if visible && layoutready state.layout
1721 then (
1722 G.postRedisplay "page";
1726 | Some (layout, _, _) ->
1727 state.currently <- Idle;
1728 tilepage l.pageno pageopaque layout;
1729 load state.layout
1730 end;
1732 | Idle
1733 | Tiling _
1734 | Outlining _ ->
1735 dolog "Inconsistent loading state";
1736 logcurrently state.currently;
1737 exit 1
1740 | "tile" , args ->
1741 let (x, y, opaques, size, t) =
1742 scan args "%u %u %s %u %f"
1743 (fun x y p size t -> (x, y, p, size, t))
1745 let opaque = ~< opaques in
1746 begin match state.currently with
1747 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1748 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1750 unmappbo opaque;
1751 if tilew != conf.tilew || tileh != conf.tileh
1752 then (
1753 wcmd "freetile %s" (~> opaque);
1754 state.currently <- Idle;
1755 load state.layout;
1757 else (
1758 puttileopaque l col row gen cs angle opaque size t;
1759 state.memused <- state.memused + size;
1760 state.uioh#infochanged Memused;
1761 gctiles ();
1762 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1763 opaque, size) state.tilelru;
1765 let layout =
1766 match state.throttle with
1767 | None -> state.layout
1768 | Some (layout, _, _) -> layout
1771 state.currently <- Idle;
1772 if gen = state.gen
1773 && conf.colorspace = cs
1774 && conf.angle = angle
1775 && tilevisible layout l.pageno x y
1776 then conttiling l.pageno pageopaque;
1778 begin match state.throttle with
1779 | None ->
1780 preload state.layout;
1781 if gen = state.gen
1782 && conf.colorspace = cs
1783 && conf.angle = angle
1784 && tilevisible state.layout l.pageno x y
1785 && (not !wtmode || layoutready state.layout)
1786 then G.postRedisplay "tile nothrottle";
1788 | Some (layout, y, _) ->
1789 let ready = layoutready layout in
1790 if ready
1791 then (
1792 state.y <- y;
1793 state.layout <- layout;
1794 state.throttle <- None;
1795 G.postRedisplay "throttle";
1797 else load layout;
1798 end;
1801 | Idle
1802 | Loading _
1803 | Outlining _ ->
1804 dolog "Inconsistent tiling state";
1805 logcurrently state.currently;
1806 exit 1
1809 | "pdim", args ->
1810 let (n, w, h, _) as pdim =
1811 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1813 let pdim =
1814 match conf.fitmodel with
1815 | FitWidth -> pdim
1816 | FitPage | FitProportional ->
1817 match conf.columns with
1818 | Csplit _ -> (n, w, h, 0)
1819 | Csingle _ | Cmulti _ -> pdim
1821 state.pdims <- pdim :: state.pdims;
1822 state.uioh#infochanged Pdim
1824 | "o", args ->
1825 let (l, n, t, h, pos) =
1826 scan args "%u %u %d %u %n"
1827 (fun l n t h pos -> l, n, t, h, pos)
1829 let s = String.sub args pos (String.length args - pos) in
1830 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
1832 | "ou", args ->
1833 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
1834 let s = String.sub args pos len in
1835 let pos2 = pos + len + 1 in
1836 let uri = String.sub args pos2 (String.length args - pos2) in
1837 addoutline (s, l, Ouri uri)
1839 | "on", args ->
1840 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
1841 let s = String.sub args pos (String.length args - pos) in
1842 addoutline (s, l, Onone)
1844 | "a", args ->
1845 let (n, l, t) =
1846 scan args "%u %d %d" (fun n l t -> n, l, t)
1848 state.reprf <- (fun () -> gotopagexy !wtmode n (float l) (float t))
1850 | "info", args ->
1851 let c, v = splitatchar args '\t' in
1852 let s =
1853 if nonemptystr v
1854 then
1855 if c = "Title"
1856 then (
1857 conf.title <- v;
1858 Wsi.settitle v;
1859 args
1861 else
1862 if let len = String.length c in
1863 len > 6 && ((String.sub c (len-4) 4) = "date")
1864 then (
1865 if String.length v >= 7 && v.[0] = 'D' && v.[1] = ':'
1866 then
1867 let b = Buffer.create 10 in
1868 Printf.bprintf b "%s\t" c;
1869 let sub p l c =
1871 Buffer.add_substring b v p l;
1872 Buffer.add_char b c;
1873 with exn -> Buffer.add_string b @@ exntos exn
1875 sub 2 4 '/';
1876 sub 6 2 '/';
1877 sub 8 2 ' ';
1878 sub 10 2 ':';
1879 sub 12 2 ':';
1880 sub 14 2 ' ';
1881 Buffer.add_char b '[';
1882 Buffer.add_string b v;
1883 Buffer.add_char b ']';
1884 Buffer.contents b
1885 else args
1887 else args
1888 else args
1890 state.docinfo <- (1, s) :: state.docinfo
1892 | "infoend", "" ->
1893 state.docinfo <- List.rev state.docinfo;
1894 state.uioh#infochanged Docinfo
1896 | "pass", args ->
1897 if args = "fail"
1898 then Wsi.settitle "Wrong password";
1899 let password = getpassword () in
1900 if emptystr password
1901 then error "document is password protected"
1902 else opendoc state.path password
1904 | _ ->
1905 error "unknown cmd `%S'" cmds
1908 let onhist cb =
1909 let rc = cb.rc in
1910 let action = function
1911 | HCprev -> cbget cb ~-1
1912 | HCnext -> cbget cb 1
1913 | HCfirst -> cbget cb ~-(cb.rc)
1914 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1915 and cancel () = cb.rc <- rc
1916 in (action, cancel)
1919 let search pattern forward =
1920 match conf.columns with
1921 | Csplit _ -> impmsg "searching does not work properly in split columns mode"
1922 | Csingle _ | Cmulti _ ->
1923 if nonemptystr pattern
1924 then
1925 let pn, py =
1926 match state.layout with
1927 | [] -> 0, 0
1928 | l :: _ ->
1929 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1931 wcmd "search %d %d %d %d,%s\000"
1932 (btod conf.icase) pn py (btod forward) pattern;
1935 let intentry text key =
1936 let text =
1937 if key >= 32 && key < 127
1938 then
1939 let c = Char.chr key in
1940 match c with
1941 | '0' .. '9' -> addchar text c
1942 | _ ->
1943 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1944 text
1945 else (
1946 state.text <- Printf.sprintf "invalid key (%d)" key;
1947 text
1950 TEcont text
1953 let linknact f s =
1954 if nonemptystr s
1955 then (
1956 let n =
1957 let l = String.length s in
1958 let rec loop pos n =
1959 if pos = l
1960 then n
1961 else
1962 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
1963 loop (pos+1) (n*26 + m)
1964 in loop 0 0
1966 let rec loop n = function
1967 | [] -> ()
1968 | l :: rest ->
1969 match getopaque l.pageno with
1970 | None -> loop n rest
1971 | Some opaque ->
1972 let m = getlinkcount opaque in
1973 if n < m
1974 then (
1975 let under = getlink opaque n in
1976 f under
1978 else loop (n-m) rest
1980 loop n state.layout;
1984 let linknentry text key =
1985 if key >= 32 && key < 127
1986 then
1987 let text = addchar text (Char.chr key) in
1988 linknact (fun under -> state.text <- undertext under) text;
1989 TEcont text
1990 else (
1991 state.text <- Printf.sprintf "invalid key %d" key;
1992 TEcont text
1996 let textentry text key =
1997 if Wsi.isspecialkey key
1998 then TEcont text
1999 else TEcont (text ^ toutf8 key)
2002 let reqlayout angle fitmodel =
2003 match state.throttle with
2004 | None ->
2005 if nogeomcmds state.geomcmds
2006 then state.anchor <- getanchor ();
2007 conf.angle <- angle mod 360;
2008 if conf.angle != 0
2009 then (
2010 match state.mode with
2011 | LinkNav _ -> state.mode <- View
2012 | Birdseye _ | Textentry _ | View -> ()
2014 conf.fitmodel <- fitmodel;
2015 invalidate
2016 "reqlayout"
2017 (fun () ->
2018 wcmd "reqlayout %d %d %d"
2019 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
2021 | _ -> ()
2024 let settrim trimmargins trimfuzz =
2025 if nogeomcmds state.geomcmds
2026 then state.anchor <- getanchor ();
2027 conf.trimmargins <- trimmargins;
2028 conf.trimfuzz <- trimfuzz;
2029 let x0, y0, x1, y1 = trimfuzz in
2030 invalidate
2031 "settrim" (fun () ->
2032 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2033 flushpages ();
2036 let setzoom zoom =
2037 match state.throttle with
2038 | None ->
2039 let zoom = max 0.0001 zoom in
2040 if zoom <> conf.zoom
2041 then (
2042 state.prevzoom <- (conf.zoom, state.x);
2043 conf.zoom <- zoom;
2044 reshape state.winw state.winh;
2045 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2048 | Some (layout, y, started) ->
2049 let time =
2050 match conf.maxwait with
2051 | None -> 0.0
2052 | Some t -> t
2054 let dt = now () -. started in
2055 if dt > time
2056 then (
2057 state.y <- y;
2058 load layout;
2062 let pivotzoom ?(vw=min state.w state.winw)
2063 ?(vh=min (state.maxy-state.y) state.winh)
2064 ?(x=vw/2) ?(y=vh/2) zoom =
2065 let w = float state.w /. zoom in
2066 let hw = w /. 2.0 in
2067 let ratio = float vh /. float vw in
2068 let hh = hw *. ratio in
2069 let x0 = if zoom < 1.0 then 0.0 else float x -. hw in
2070 let y0 = float y -. hh in
2071 gotoxy (state.x - truncate x0) (state.y + truncate y0);
2072 setzoom zoom;
2075 let pivotzoom ?vw ?vh ?x ?y zoom =
2076 if nogeomcmds state.geomcmds then pivotzoom ?vw ?vh ?x ?y zoom
2079 let setcolumns mode columns coverA coverB =
2080 state.prevcolumns <- Some (conf.columns, conf.zoom);
2081 if columns < 0
2082 then (
2083 if isbirdseye mode
2084 then impmsg "split mode doesn't work in bird's eye"
2085 else (
2086 conf.columns <- Csplit (-columns, E.a);
2087 state.x <- 0;
2088 conf.zoom <- 1.0;
2091 else (
2092 if columns < 2
2093 then (
2094 conf.columns <- Csingle E.a;
2095 state.x <- 0;
2096 setzoom 1.0;
2098 else (
2099 conf.columns <- Cmulti ((columns, coverA, coverB), E.a);
2100 conf.zoom <- 1.0;
2103 reshape state.winw state.winh;
2106 let resetmstate () =
2107 state.mstate <- Mnone;
2108 Wsi.setcursor Wsi.CURSOR_INHERIT;
2111 let enterbirdseye () =
2112 let zoom = float conf.thumbw /. float state.winw in
2113 let birdseyepageno =
2114 let cy = state.winh / 2 in
2115 let fold = function
2116 | [] -> 0
2117 | l :: rest ->
2118 let rec fold best = function
2119 | [] -> best.pageno
2120 | l :: rest ->
2121 let d = cy - (l.pagedispy + l.pagevh/2)
2122 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2123 if abs d < abs dbest
2124 then fold l rest
2125 else best.pageno
2126 in fold l rest
2128 fold state.layout
2130 state.mode <-
2131 Birdseye (
2132 { conf with zoom = conf.zoom },
2133 state.x, birdseyepageno, -1, getanchor ()
2135 resetmstate ();
2136 conf.zoom <- zoom;
2137 conf.presentation <- false;
2138 conf.interpagespace <- 10;
2139 conf.hlinks <- false;
2140 conf.fitmodel <- FitPage;
2141 state.x <- 0;
2142 conf.maxwait <- None;
2143 conf.columns <- (
2144 match conf.beyecolumns with
2145 | Some c ->
2146 conf.zoom <- 1.0;
2147 Cmulti ((c, 0, 0), E.a)
2148 | None -> Csingle E.a
2150 if conf.verbose
2151 then
2152 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2153 (100.0*.zoom)
2154 else
2155 state.text <- E.s
2157 reshape state.winw state.winh;
2160 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2161 state.mode <- View;
2162 conf.zoom <- c.zoom;
2163 conf.presentation <- c.presentation;
2164 conf.interpagespace <- c.interpagespace;
2165 conf.maxwait <- c.maxwait;
2166 conf.hlinks <- c.hlinks;
2167 conf.fitmodel <- c.fitmodel;
2168 conf.beyecolumns <- (
2169 match conf.columns with
2170 | Cmulti ((c, _, _), _) -> Some c
2171 | Csingle _ -> None
2172 | Csplit _ -> failwith "leaving bird's eye split mode"
2174 conf.columns <- (
2175 match c.columns with
2176 | Cmulti (c, _) -> Cmulti (c, E.a)
2177 | Csingle _ -> Csingle E.a
2178 | Csplit (c, _) -> Csplit (c, E.a)
2180 if conf.verbose
2181 then
2182 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2183 (100.0*.conf.zoom)
2185 reshape state.winw state.winh;
2186 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2187 state.x <- leftx;
2190 let togglebirdseye () =
2191 match state.mode with
2192 | Birdseye vals -> leavebirdseye vals true
2193 | View -> enterbirdseye ()
2194 | Textentry _ | LinkNav _ -> ()
2197 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2198 let pageno = max 0 (pageno - incr) in
2199 let rec loop = function
2200 | [] -> gotopage1 pageno 0
2201 | l :: _ when l.pageno = pageno ->
2202 if l.pagedispy >= 0 && l.pagey = 0
2203 then G.postRedisplay "upbirdseye"
2204 else gotopage1 pageno 0
2205 | _ :: rest -> loop rest
2207 loop state.layout;
2208 state.text <- E.s;
2209 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2212 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2213 let pageno = min (state.pagecount - 1) (pageno + incr) in
2214 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2215 let rec loop = function
2216 | [] ->
2217 let y, h = getpageyh pageno in
2218 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2219 gotoxy state.x (clamp dy)
2220 | l :: _ when l.pageno = pageno ->
2221 if l.pagevh != l.pageh
2222 then gotoxy state.x (clamp (l.pageh - l.pagevh + conf.interpagespace))
2223 else G.postRedisplay "downbirdseye"
2224 | _ :: rest -> loop rest
2226 loop state.layout;
2227 state.text <- E.s;
2230 let optentry mode _ key =
2231 let btos b = if b then "on" else "off" in
2232 if key >= 32 && key < 127
2233 then
2234 let c = Char.chr key in
2235 match c with
2236 | 's' ->
2237 let ondone s =
2238 try conf.scrollstep <- int_of_string s with exn ->
2239 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2241 TEswitch ("scroll step: ", E.s, None, intentry, ondone, true)
2243 | 'A' ->
2244 let ondone s =
2246 conf.autoscrollstep <- boundastep state.winh (int_of_string s);
2247 if state.autoscroll <> None
2248 then state.autoscroll <- Some conf.autoscrollstep
2249 with exn ->
2250 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2252 TEswitch ("auto scroll step: ", E.s, None, intentry, ondone, true)
2254 | 'C' ->
2255 let ondone s =
2257 let n, a, b = multicolumns_of_string s in
2258 setcolumns mode n a b;
2259 with exn ->
2260 state.text <- Printf.sprintf "bad columns `%s': %s" s @@ exntos exn
2262 TEswitch ("columns: ", E.s, None, textentry, ondone, true)
2264 | 'Z' ->
2265 let ondone s =
2267 let zoom = float (int_of_string s) /. 100.0 in
2268 pivotzoom zoom
2269 with exn ->
2270 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2272 TEswitch ("zoom: ", E.s, None, intentry, ondone, true)
2274 | 't' ->
2275 let ondone s =
2277 conf.thumbw <- bound (int_of_string s) 2 4096;
2278 state.text <-
2279 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2280 begin match mode with
2281 | Birdseye beye ->
2282 leavebirdseye beye false;
2283 enterbirdseye ();
2284 | Textentry _ | View | LinkNav _ -> ();
2286 with exn ->
2287 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2289 TEswitch ("thumbnail width: ", E.s, None, intentry, ondone, true)
2291 | 'R' ->
2292 let ondone s =
2293 match int_of_string s with
2294 | angle -> reqlayout angle conf.fitmodel
2295 | exception exn ->
2296 state.text <-
2297 Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2299 TEswitch ("rotation: ", E.s, None, intentry, ondone, true)
2301 | 'i' ->
2302 conf.icase <- not conf.icase;
2303 TEdone ("case insensitive search " ^ (btos conf.icase))
2305 | 'p' ->
2306 conf.preload <- not conf.preload;
2307 gotoxy state.x state.y;
2308 TEdone ("preload " ^ (btos conf.preload))
2310 | 'v' ->
2311 conf.verbose <- not conf.verbose;
2312 TEdone ("verbose " ^ (btos conf.verbose))
2314 | 'd' ->
2315 conf.debug <- not conf.debug;
2316 TEdone ("debug " ^ (btos conf.debug))
2318 | 'h' ->
2319 conf.maxhfit <- not conf.maxhfit;
2320 state.maxy <- calcheight ();
2321 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2323 | 'c' ->
2324 conf.crophack <- not conf.crophack;
2325 TEdone ("crophack " ^ btos conf.crophack)
2327 | 'a' ->
2328 let s =
2329 match conf.maxwait with
2330 | None ->
2331 conf.maxwait <- Some infinity;
2332 "always wait for page to complete"
2333 | Some _ ->
2334 conf.maxwait <- None;
2335 "show placeholder if page is not ready"
2337 TEdone s
2339 | 'f' ->
2340 conf.underinfo <- not conf.underinfo;
2341 TEdone ("underinfo " ^ btos conf.underinfo)
2343 | 'P' ->
2344 conf.savebmarks <- not conf.savebmarks;
2345 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2347 | 'S' ->
2348 let ondone s =
2350 let pageno, py =
2351 match state.layout with
2352 | [] -> 0, 0
2353 | l :: _ ->
2354 l.pageno, l.pagey
2356 conf.interpagespace <- int_of_string s;
2357 docolumns conf.columns;
2358 state.maxy <- calcheight ();
2359 let y = getpagey pageno in
2360 gotoxy state.x (y + py)
2361 with exn ->
2362 state.text <-
2363 Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2365 TEswitch ("vertical margin: ", E.s, None, intentry, ondone, true)
2367 | 'l' ->
2368 let fm =
2369 match conf.fitmodel with
2370 | FitProportional -> FitWidth
2371 | FitWidth | FitPage -> FitProportional
2373 reqlayout conf.angle fm;
2374 TEdone ("proportional display " ^ btos (fm == FitProportional))
2376 | 'T' ->
2377 settrim (not conf.trimmargins) conf.trimfuzz;
2378 TEdone ("trim margins " ^ btos conf.trimmargins)
2380 | 'I' ->
2381 conf.invert <- not conf.invert;
2382 TEdone ("invert colors " ^ btos conf.invert)
2384 | 'x' ->
2385 let ondone s =
2386 cbput state.hists.sel s;
2387 conf.selcmd <- s;
2389 TEswitch ("selection command: ", E.s, Some (onhist state.hists.sel),
2390 textentry, ondone, true)
2392 | 'M' ->
2393 if conf.pax == None
2394 then conf.pax <- Some (ref (0.0, 0, 0))
2395 else conf.pax <- None;
2396 TEdone ("PAX " ^ btos (conf.pax != None))
2398 | _ ->
2399 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2400 TEstop
2401 else
2402 TEcont state.text
2405 class type lvsource =
2406 object
2407 method getitemcount : int
2408 method getitem : int -> (string * int)
2409 method hasaction : int -> bool
2410 method exit : uioh:uioh ->
2411 cancel:bool ->
2412 active:int ->
2413 first:int ->
2414 pan:int ->
2415 uioh option
2416 method getactive : int
2417 method getfirst : int
2418 method getpan : int
2419 method getminfo : (int * int) array
2420 end;;
2422 class virtual lvsourcebase = object
2423 val mutable m_active = 0
2424 val mutable m_first = 0
2425 val mutable m_pan = 0
2426 method getactive = m_active
2427 method getfirst = m_first
2428 method getpan = m_pan
2429 method getminfo : (int * int) array = E.a
2430 end;;
2432 let textentrykeyboard
2433 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2434 state.text <- E.s;
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 let open Keys in
2451 match Wsi.kc2pv key with
2452 | Backspace ->
2453 if emptystr text && cancelonempty
2454 then (
2455 onleave Cancel;
2456 G.postRedisplay "textentrykeyboard after cancel";
2458 else
2459 let s = withoutlastutf8 text in
2460 enttext (c, s, opthist, onkey, ondone, cancelonempty)
2462 | Enter | KPenter ->
2463 ondone text;
2464 onleave Confirm;
2465 G.postRedisplay "textentrykeyboard after confirm"
2467 | Up | KPup -> histaction HCprev
2468 | Down | KPdown -> histaction HCnext
2469 | Home | KPhome -> histaction HCfirst
2470 | End | KPend -> histaction HClast
2472 | Escape ->
2473 if emptystr text
2474 then (
2475 begin match opthist with
2476 | None -> ()
2477 | Some (_, onhistcancel) -> onhistcancel ()
2478 end;
2479 onleave Cancel;
2480 state.text <- E.s;
2481 G.postRedisplay "textentrykeyboard after cancel2"
2483 else (
2484 enttext (c, E.s, opthist, onkey, ondone, cancelonempty)
2487 | Delete | KPdelete -> ()
2489 | Code _ | Ascii _->
2490 begin match onkey text key with
2491 | TEdone text ->
2492 ondone text;
2493 onleave Confirm;
2494 G.postRedisplay "textentrykeyboard after confirm2";
2496 | TEcont text ->
2497 enttext (c, text, opthist, onkey, ondone, cancelonempty);
2499 | TEstop ->
2500 onleave Cancel;
2501 G.postRedisplay "textentrykeyboard after cancel3"
2503 | TEswitch te ->
2504 state.mode <- Textentry (te, onleave);
2505 G.postRedisplay "textentrykeyboard switch";
2507 | (Insert|KPleft|KPminus|KPnext
2508 |KPplus|KPprior|KPright|Left|Right|Next|Prior|Fn _) ->
2509 vlog "unhandled key %s" (Wsi.keyname key)
2512 let firstof first active =
2513 if first > active || abs (first - active) > fstate.maxrows - 1
2514 then max 0 (active - (fstate.maxrows/2))
2515 else first
2518 let calcfirst first active =
2519 if active > first
2520 then
2521 let rows = active - first in
2522 if rows > fstate.maxrows then active - fstate.maxrows else first
2523 else active
2526 let scrollph y maxy =
2527 let sh = float (maxy + state.winh) /. float state.winh in
2528 let sh = float state.winh /. sh in
2529 let sh = max sh (float conf.scrollh) in
2531 let percent = float y /. float maxy in
2532 let position = (float state.winh -. sh) *. percent in
2534 let position =
2535 if position +. sh > float state.winh
2536 then float state.winh -. sh
2537 else position
2539 position, sh;
2542 let adderrmsg src msg =
2543 Buffer.add_string state.errmsgs msg;
2544 state.newerrmsgs <- true;
2545 G.postRedisplay src
2548 let adderrfmt src fmt =
2549 Format.ksprintf (fun s -> adderrmsg src s) fmt;
2552 let coe s = (s :> uioh);;
2554 class listview ~zebra ~helpmode ~(source:lvsource) ~trusted ~modehash =
2555 object (self)
2556 val m_pan = source#getpan
2557 val m_first = source#getfirst
2558 val m_active = source#getactive
2559 val m_qsearch = E.s
2560 val m_prev_uioh = state.uioh
2562 method private elemunder y =
2563 if y < 0
2564 then None
2565 else
2566 let n = y / (fstate.fontsize+1) in
2567 if m_first + n < source#getitemcount
2568 then (
2569 if source#hasaction (m_first + n)
2570 then Some (m_first + n)
2571 else None
2573 else None
2575 method display =
2576 Gl.enable `blend;
2577 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
2578 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2579 filledrect 0. 0. (float state.winw) (float state.winh);
2580 GlDraw.color (1., 1., 1.);
2581 Gl.enable `texture_2d;
2582 let fs = fstate.fontsize in
2583 let nfs = fs + 1 in
2584 let hw = state.winw/3 in
2585 let ww = fstate.wwidth in
2586 let tabw = 17.0*.ww in
2587 let itemcount = source#getitemcount in
2588 let minfo = source#getminfo in
2589 if conf.leftscroll
2590 then (
2591 GlMat.push ();
2592 GlMat.translate ~x:(float conf.scrollbw) ();
2594 let x0 = 0.0 and x1 = float (state.winw - conf.scrollbw - 1) in
2595 let rec loop row =
2596 if (row - m_first) > fstate.maxrows
2597 then ()
2598 else (
2599 if row >= 0 && row < itemcount
2600 then (
2601 let (s, level) = source#getitem row in
2602 let y = (row - m_first) * nfs in
2603 let x = 5.0 +. (float (level + m_pan)) *. ww in
2604 if helpmode
2605 then GlDraw.color
2606 (let c = if row land 1 = 0 then 1.0 else 0.92 in (c,c,c));
2608 if row = m_active
2609 then (
2610 Gl.disable `texture_2d;
2611 let alpha = if source#hasaction row then 0.9 else 0.3 in
2612 GlDraw.color (1., 1., 1.) ~alpha;
2613 linerect (x0 +. 1.) (float (y + 1)) (x1) (float (y + fs + 3));
2614 Gl.enable `texture_2d;
2616 let c =
2617 if zebra && row land 1 = 1
2618 then 0.8
2619 else 1.0
2621 GlDraw.color (c,c,c);
2622 let drawtabularstring s =
2623 let drawstr x s =
2624 let x' = truncate (x0 +. x) in
2625 let s1, s2 = splitatchar s '\000' in
2626 if emptystr s2
2627 then drawstring1 fs x' (y+nfs) s
2628 else
2629 let rec e s =
2630 if emptystr s
2631 then s
2632 else
2633 let s' = withoutlastutf8 s in
2634 let s = s' ^ UniSyms.ellipsis in
2635 let w = measurestr fs s in
2636 if float x' +. w +. ww < float (hw + x')
2637 then s
2638 else e s'
2640 let s1 =
2641 if float x' +. ww +. measurestr fs s1 > float (hw + x')
2642 then e s1
2643 else s1
2645 ignore (drawstring1 fs x' (y+nfs) s1);
2646 drawstring1 fs (hw + x') (y+nfs) s2
2648 if trusted
2649 then
2650 let x = if helpmode && row > 0 then x +. ww else x in
2651 let s1, s2 = splitatchar s '\t' in
2652 if nonemptystr s2
2653 then
2654 let nx = drawstr x s1 in
2655 let sw = nx -. x in
2656 let x = x +. (max tabw sw) in
2657 drawstr x s2
2658 else
2659 let len = String.length s - 2 in
2660 if len > 0 && s.[0] = '\xc2' && s.[1] = '\xb7'
2661 then
2662 let s = String.sub s 2 len in
2663 let x = if not helpmode then x +. ww else x in
2664 GlDraw.color (1.2, 1.2, 1.2);
2665 let vinc = drawstring1 (fs+fs/4)
2666 (truncate (x -. ww)) (y+nfs) s in
2667 GlDraw.color (1., 1., 1.);
2668 vinc +. (float fs *. 0.8)
2669 else
2670 drawstr x s
2671 else
2672 drawstr x s
2674 ignore (drawtabularstring s);
2675 loop (row+1)
2679 loop m_first;
2680 GlDraw.color (1.0, 1.0, 1.0) ~alpha:0.5;
2681 let xadj = 5.0 in
2682 let rec loop row =
2683 if (row - m_first) <= fstate.maxrows
2684 then
2685 if row >= 0 && row < itemcount
2686 then (
2687 let (s, level) = source#getitem row in
2688 let pos0 = nindex s '\000' in
2689 let y = (row - m_first) * nfs in
2690 let x = float (level + m_pan) *. ww in
2691 let (first, last) = minfo.(row) in
2692 let prefix =
2693 if pos0 > 0 && first > pos0
2694 then String.sub s (pos0+1) (first-pos0-1)
2695 else String.sub s 0 first
2697 let suffix = String.sub s first (last - first) in
2698 let w1 = measurestr fstate.fontsize prefix in
2699 let w2 = measurestr fstate.fontsize suffix in
2700 let x = x +. if conf.leftscroll then xadj else 5.0 in
2701 let x = if pos0 > 0 && first > pos0 then x +. float hw else x in
2702 let x0 = x +. w1
2703 and y0 = float (y+2) in
2704 let x1 = x0 +. w2
2705 and y1 = float (y+fs+3) in
2706 filledrect x0 y0 x1 y1;
2707 loop (row+1)
2710 Gl.disable `texture_2d;
2711 if Array.length minfo > 0 then loop m_first;
2712 Gl.disable `blend;
2713 if conf.leftscroll
2714 then GlMat.pop ()
2716 method updownlevel incr =
2717 let len = source#getitemcount in
2718 let curlevel =
2719 if m_active >= 0 && m_active < len
2720 then snd (source#getitem m_active)
2721 else -1
2723 let rec flow i =
2724 if i = len then i-1 else if i = -1 then 0 else
2725 let _, l = source#getitem i in
2726 if l != curlevel then i else flow (i+incr)
2728 let active = flow m_active in
2729 let first = calcfirst m_first active in
2730 G.postRedisplay "outline updownlevel";
2731 {< m_active = active; m_first = first >}
2733 method private key1 key mask =
2734 let set1 active first qsearch =
2735 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2737 let search active pattern incr =
2738 let active = if active = -1 then m_first else active in
2739 let dosearch re =
2740 let rec loop n =
2741 if n >= 0 && n < source#getitemcount
2742 then (
2743 let s, _ = source#getitem n in
2744 match Str.search_forward re s 0 with
2745 | (exception Not_found) -> loop (n + incr)
2746 | _ -> Some n
2748 else None
2750 loop active
2752 let qpat = Str.quote pattern in
2753 match Str.regexp_case_fold qpat with
2754 | s -> dosearch s
2755 | exception exn ->
2756 adderrfmt "listview key1" "regexp_case_fold for `%S' failed: %S\n"
2757 qpat @@ Printexc.to_string exn;
2758 None
2760 let itemcount = source#getitemcount in
2761 let find start incr =
2762 let rec find i =
2763 if i = -1 || i = itemcount
2764 then -1
2765 else (
2766 if source#hasaction i
2767 then i
2768 else find (i + incr)
2771 find start
2773 let set active first =
2774 let first = bound first 0 (itemcount - fstate.maxrows) in
2775 state.text <- E.s;
2776 coe {< m_active = active; m_first = first; m_qsearch = E.s >}
2778 let navigate incr =
2779 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2780 let active, first =
2781 let incr1 = if incr > 0 then 1 else -1 in
2782 if isvisible m_first m_active
2783 then
2784 let next =
2785 let next = m_active + incr in
2786 let next =
2787 if next < 0 || next >= itemcount
2788 then -1
2789 else find next incr1
2791 if abs (m_active - next) > fstate.maxrows
2792 then -1
2793 else next
2795 if next = -1
2796 then
2797 let first = m_first + incr in
2798 let first = bound first 0 (itemcount - fstate.maxrows) in
2799 let next =
2800 let next = m_active + incr in
2801 let next = bound next 0 (itemcount - 1) in
2802 find next ~-incr1
2804 let active =
2805 if next = -1
2806 then m_active
2807 else (
2808 if isvisible first next
2809 then next
2810 else m_active
2813 active, first
2814 else
2815 let first = min next m_first in
2816 let first =
2817 if abs (next - first) > fstate.maxrows
2818 then first + incr
2819 else first
2821 next, first
2822 else
2823 let first = m_first + incr in
2824 let first = bound first 0 (itemcount - 1) in
2825 let active =
2826 let next = m_active + incr in
2827 let next = bound next 0 (itemcount - 1) in
2828 let next = find next incr1 in
2829 let active =
2830 if next = -1 || abs (m_active - first) > fstate.maxrows
2831 then (
2832 let active = if m_active = -1 then next else m_active in
2833 active
2835 else next
2837 if isvisible first active
2838 then active
2839 else -1
2841 active, first
2843 G.postRedisplay "listview navigate";
2844 set active first;
2846 let open Keys in
2847 match Wsi.kc2pv key with
2848 | Ascii (('r'|'s') as c) when Wsi.withctrl mask ->
2849 let incr = if c = 'r' then -1 else 1 in
2850 let active, first =
2851 match search (m_active + incr) m_qsearch incr with
2852 | None ->
2853 state.text <- m_qsearch ^ " [not found]";
2854 m_active, m_first
2855 | Some active ->
2856 state.text <- m_qsearch;
2857 active, firstof m_first active
2859 G.postRedisplay "listview ctrl-r/s";
2860 set1 active first m_qsearch;
2862 | Insert when Wsi.withctrl mask ->
2863 if m_active >= 0 && m_active < source#getitemcount
2864 then (
2865 let s, _ = source#getitem m_active in
2866 selstring s;
2868 coe self
2870 | Backspace ->
2871 if emptystr m_qsearch
2872 then coe self
2873 else (
2874 let qsearch = withoutlastutf8 m_qsearch in
2875 if emptystr qsearch
2876 then (
2877 state.text <- E.s;
2878 G.postRedisplay "listview empty qsearch";
2879 set1 m_active m_first E.s;
2881 else
2882 let active, first =
2883 match search m_active qsearch ~-1 with
2884 | None ->
2885 state.text <- qsearch ^ " [not found]";
2886 m_active, m_first
2887 | Some active ->
2888 state.text <- qsearch;
2889 active, firstof m_first active
2891 G.postRedisplay "listview backspace qsearch";
2892 set1 active first qsearch
2895 | _ when not (Wsi.isspecialkey key) ->
2896 let pattern = m_qsearch ^ toutf8 key in
2897 let active, first =
2898 match search m_active pattern 1 with
2899 | None ->
2900 state.text <- pattern ^ " [not found]";
2901 m_active, m_first
2902 | Some active ->
2903 state.text <- pattern;
2904 active, firstof m_first active
2906 G.postRedisplay "listview qsearch add";
2907 set1 active first pattern;
2909 | Escape ->
2910 state.text <- E.s;
2911 if emptystr m_qsearch
2912 then (
2913 G.postRedisplay "list view escape";
2914 let mx, my = state.mpos in
2915 updateunder mx my;
2916 begin
2917 match
2918 source#exit ~uioh:(coe self)
2919 ~cancel:true ~active:m_active ~first:m_first ~pan:m_pan
2920 with
2921 | None -> m_prev_uioh
2922 | Some uioh -> uioh
2925 else (
2926 G.postRedisplay "list view kill qsearch";
2927 coe {< m_qsearch = E.s >}
2930 | Enter | KPenter ->
2931 state.text <- E.s;
2932 let self = {< m_qsearch = E.s >} in
2933 let opt =
2934 G.postRedisplay "listview enter";
2935 if m_active >= 0 && m_active < source#getitemcount
2936 then (
2937 source#exit ~uioh:(coe self) ~cancel:false
2938 ~active:m_active ~first:m_first ~pan:m_pan;
2940 else (
2941 source#exit ~uioh:(coe self) ~cancel:true
2942 ~active:m_active ~first:m_first ~pan:m_pan;
2945 begin match opt with
2946 | None -> m_prev_uioh
2947 | Some uioh -> uioh
2950 | Delete | KPdelete ->
2951 coe self
2953 | Up | KPup -> navigate ~-1
2954 | Down | KPdown -> navigate 1
2955 | Prior | KPprior -> navigate ~-(fstate.maxrows)
2956 | Next | KPnext -> navigate fstate.maxrows
2958 | Right | KPright ->
2959 state.text <- E.s;
2960 G.postRedisplay "listview right";
2961 coe {< m_pan = m_pan - 1 >}
2963 | Left | KPleft ->
2964 state.text <- E.s;
2965 G.postRedisplay "listview left";
2966 coe {< m_pan = m_pan + 1 >}
2968 | Home | KPhome ->
2969 let active = find 0 1 in
2970 G.postRedisplay "listview home";
2971 set active 0;
2973 | End | KPend ->
2974 let first = max 0 (itemcount - fstate.maxrows) in
2975 let active = find (itemcount - 1) ~-1 in
2976 G.postRedisplay "listview end";
2977 set active first;
2979 | _ when (key = 0 || Wsi.isspecialkey key) ->
2980 coe self
2982 | (Insert|KPminus|KPplus|Ascii _|Code _|Fn _) ->
2983 dolog "listview unknown key %#x" key; coe self
2985 method key key mask =
2986 match state.mode with
2987 | Textentry te ->
2988 textentrykeyboard key mask te;
2989 coe self
2990 | Birdseye _ | View | LinkNav _ -> self#key1 key mask
2992 method button button down x y _ =
2993 let opt =
2994 match button with
2995 | 1 when vscrollhit x ->
2996 G.postRedisplay "listview scroll";
2997 if down
2998 then
2999 let _, position, sh = self#scrollph in
3000 if y > truncate position && y < truncate (position +. sh)
3001 then (
3002 state.mstate <- Mscrolly;
3003 Some (coe self)
3005 else
3006 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3007 let first = truncate (s *. float source#getitemcount) in
3008 let first = min source#getitemcount first in
3009 Some (coe {< m_first = first; m_active = first >})
3010 else (
3011 state.mstate <- Mnone;
3012 Some (coe self);
3014 | 1 when down ->
3015 begin match self#elemunder y with
3016 | Some n ->
3017 G.postRedisplay "listview click";
3018 source#exit ~uioh:(coe {< m_active = n >})
3019 ~cancel:false ~active:n ~first:m_first ~pan:m_pan
3020 | _ ->
3021 Some (coe self)
3023 | n when (n == 4 || n == 5) && not down ->
3024 let len = source#getitemcount in
3025 let first =
3026 if n = 5 && m_first + fstate.maxrows >= len
3027 then
3028 m_first
3029 else
3030 let first = m_first + (if n == 4 then -1 else 1) in
3031 bound first 0 (len - 1)
3033 G.postRedisplay "listview wheel";
3034 Some (coe {< m_first = first >})
3035 | n when (n = 6 || n = 7) && not down ->
3036 let inc = if n = 7 then -1 else 1 in
3037 G.postRedisplay "listview hwheel";
3038 Some (coe {< m_pan = m_pan + inc >})
3039 | _ ->
3040 Some (coe self)
3042 match opt with
3043 | None -> m_prev_uioh
3044 | Some uioh -> uioh
3046 method multiclick _ x y = self#button 1 true x y
3048 method motion _ y =
3049 match state.mstate with
3050 | Mscrolly ->
3051 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3052 let first = truncate (s *. float source#getitemcount) in
3053 let first = min source#getitemcount first in
3054 G.postRedisplay "listview motion";
3055 coe {< m_first = first; m_active = first >}
3056 | Msel _
3057 | Mpan _
3058 | Mscrollx
3059 | Mzoom _
3060 | Mzoomrect _
3061 | Mnone -> coe self
3063 method pmotion x y =
3064 if x < state.winw - conf.scrollbw
3065 then
3066 let n =
3067 match self#elemunder y with
3068 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3069 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3071 let o =
3072 if n != m_active
3073 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3074 else self
3076 coe o
3077 else (
3078 Wsi.setcursor Wsi.CURSOR_INHERIT;
3079 coe self
3082 method infochanged _ = ()
3084 method scrollpw = (0, 0.0, 0.0)
3085 method scrollph =
3086 let nfs = fstate.fontsize + 1 in
3087 let y = m_first * nfs in
3088 let itemcount = source#getitemcount in
3089 let maxi = max 0 (itemcount - fstate.maxrows) in
3090 let maxy = maxi * nfs in
3091 let p, h = scrollph y maxy in
3092 conf.scrollbw, p, h
3094 method modehash = modehash
3095 method eformsgs = false
3096 method alwaysscrolly = true
3097 end;;
3099 class outlinelistview ~zebra ~source =
3100 let settext autonarrow s =
3101 if autonarrow
3102 then
3103 let ss = source#statestr in
3104 state.text <-
3105 if emptystr ss
3106 then "[" ^ s ^ "]"
3107 else "{" ^ ss ^ "} [" ^ s ^ "]"
3108 else state.text <- s
3110 object (self)
3111 inherit listview
3112 ~zebra
3113 ~helpmode:false
3114 ~source:(source :> lvsource)
3115 ~trusted:false
3116 ~modehash:(findkeyhash conf "outline")
3117 as super
3119 val m_autonarrow = false
3121 method! key key mask =
3122 let maxrows =
3123 if emptystr state.text
3124 then fstate.maxrows
3125 else fstate.maxrows - 2
3127 let calcfirst first active =
3128 if active > first
3129 then
3130 let rows = active - first in
3131 if rows > maxrows then active - maxrows else first
3132 else active
3134 let navigate incr =
3135 let active = m_active + incr in
3136 let active = bound active 0 (source#getitemcount - 1) in
3137 let first = calcfirst m_first active in
3138 G.postRedisplay "outline navigate";
3139 coe {< m_active = active; m_first = first >}
3141 let navscroll first =
3142 let active =
3143 let dist = m_active - first in
3144 if dist < 0
3145 then first
3146 else (
3147 if dist < maxrows
3148 then m_active
3149 else first + maxrows
3152 G.postRedisplay "outline navscroll";
3153 coe {< m_first = first; m_active = active >}
3155 let ctrl = Wsi.withctrl mask in
3156 let open Keys in
3157 match Wsi.kc2pv key with
3158 | Ascii 'a' when ctrl ->
3159 let text =
3160 if m_autonarrow
3161 then (source#denarrow; E.s)
3162 else (
3163 let pattern = source#renarrow in
3164 if nonemptystr m_qsearch
3165 then (source#narrow m_qsearch; m_qsearch)
3166 else pattern
3169 settext (not m_autonarrow) text;
3170 G.postRedisplay "toggle auto narrowing";
3171 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
3173 | Ascii '/' when emptystr m_qsearch && not m_autonarrow ->
3174 settext true E.s;
3175 G.postRedisplay "toggle auto narrowing";
3176 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
3178 | Ascii 'n' when ctrl ->
3179 source#narrow m_qsearch;
3180 if not m_autonarrow
3181 then source#add_narrow_pattern m_qsearch;
3182 G.postRedisplay "outline ctrl-n";
3183 coe {< m_first = 0; m_active = 0 >}
3185 | Ascii 'S' when ctrl ->
3186 let active = source#calcactive (getanchor ()) in
3187 let first = firstof m_first active in
3188 G.postRedisplay "outline ctrl-s";
3189 coe {< m_first = first; m_active = active >}
3191 | Ascii 'u' when ctrl ->
3192 G.postRedisplay "outline ctrl-u";
3193 if m_autonarrow && nonemptystr m_qsearch
3194 then (
3195 ignore (source#renarrow);
3196 settext m_autonarrow E.s;
3197 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3199 else (
3200 source#del_narrow_pattern;
3201 let pattern = source#renarrow in
3202 let text =
3203 if emptystr pattern then E.s else "Narrowed to " ^ pattern
3205 settext m_autonarrow text;
3206 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3209 | Ascii 'l' when ctrl ->
3210 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3211 G.postRedisplay "outline ctrl-l";
3212 coe {< m_first = first >}
3214 | Ascii '\t' when m_autonarrow ->
3215 if nonemptystr m_qsearch
3216 then (
3217 G.postRedisplay "outline list view tab";
3218 source#add_narrow_pattern m_qsearch;
3219 settext true E.s;
3220 coe {< m_qsearch = E.s >}
3222 else coe self
3224 | Escape when m_autonarrow ->
3225 if nonemptystr m_qsearch
3226 then source#add_narrow_pattern m_qsearch;
3227 super#key key mask
3229 | Enter | KPenter when m_autonarrow ->
3230 if nonemptystr m_qsearch
3231 then source#add_narrow_pattern m_qsearch;
3232 super#key key mask
3234 | _ when m_autonarrow && (not (Wsi.isspecialkey key)) ->
3235 let pattern = m_qsearch ^ toutf8 key in
3236 G.postRedisplay "outlinelistview autonarrow add";
3237 source#narrow pattern;
3238 settext true pattern;
3239 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3241 | key when m_autonarrow && key = Backspace ->
3242 if emptystr m_qsearch
3243 then coe self
3244 else
3245 let pattern = withoutlastutf8 m_qsearch in
3246 G.postRedisplay "outlinelistview autonarrow backspace";
3247 ignore (source#renarrow);
3248 source#narrow pattern;
3249 settext true pattern;
3250 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3252 | Up | KPup when ctrl ->
3253 navscroll (max 0 (m_first - 1))
3255 | Down | KPdown when ctrl ->
3256 navscroll (min (source#getitemcount - 1) (m_first + 1))
3258 | Up | KPup -> navigate ~-1
3259 | Down | KPdown -> navigate 1
3260 | Prior | KPprior -> navigate ~-(fstate.maxrows)
3261 | Next | KPnext -> navigate fstate.maxrows
3263 | Right | KPright ->
3264 let o =
3265 if ctrl
3266 then (
3267 G.postRedisplay "outline ctrl right";
3268 {< m_pan = m_pan + 1 >}
3270 else self#updownlevel 1
3272 coe o
3274 | Left | KPleft ->
3275 let o =
3276 if ctrl
3277 then (
3278 G.postRedisplay "outline ctrl left";
3279 {< m_pan = m_pan - 1 >}
3281 else self#updownlevel ~-1
3283 coe o
3285 | Home | KPhome ->
3286 G.postRedisplay "outline home";
3287 coe {< m_first = 0; m_active = 0 >}
3289 | End | KPend ->
3290 let active = source#getitemcount - 1 in
3291 let first = max 0 (active - fstate.maxrows) in
3292 G.postRedisplay "outline end";
3293 coe {< m_active = active; m_first = first >}
3295 | (Delete|Escape|Insert|Enter|KPdelete|KPenter|KPminus
3296 |KPplus|Ascii _|Code _|Backspace|Fn _) -> super#key key mask
3297 end;;
3299 let genhistoutlines () =
3300 Config.gethist ()
3301 |> List.sort (fun (_, c1, _, _, _, _) (_, c2, _, _, _, _) ->
3302 compare c2.lastvisit c1.lastvisit)
3303 |> List.map
3304 (fun ((path, c, _, _, _, origin) as hist) ->
3305 let path = if nonemptystr origin then origin else path in
3306 let base = mbtoutf8 @@ Filename.basename path in
3307 (base ^ "\000" ^ c.title, 1, Ohistory hist)
3309 |> Array.of_list
3312 let gotohist (path, c, bookmarks, x, anchor, origin) =
3313 Config.save leavebirdseye;
3314 state.anchor <- anchor;
3315 state.bookmarks <- bookmarks;
3316 state.origin <- origin;
3317 state.x <- x;
3318 setconf conf c;
3319 let x0, y0, x1, y1 = conf.trimfuzz in
3320 wcmd "trimset %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1;
3321 reshape ~firsttime:true state.winw state.winh;
3322 opendoc path origin;
3323 setzoom c.zoom;
3326 let makecheckers () =
3327 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3328 following to say:
3329 converted by Issac Trotts. July 25, 2002 *)
3330 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3331 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3332 let id = GlTex.gen_texture () in
3333 GlTex.bind_texture ~target:`texture_2d id;
3334 GlPix.store (`unpack_alignment 1);
3335 GlTex.image2d image;
3336 List.iter (GlTex.parameter ~target:`texture_2d)
3337 [ `mag_filter `nearest; `min_filter `nearest ];
3341 let setcheckers enabled =
3342 match state.checkerstexid with
3343 | None ->
3344 if enabled then state.checkerstexid <- Some (makecheckers ())
3346 | Some checkerstexid ->
3347 if not enabled
3348 then (
3349 GlTex.delete_texture checkerstexid;
3350 state.checkerstexid <- None;
3354 let describe_location () =
3355 let fn = page_of_y state.y in
3356 let ln = page_of_y (state.y + state.winh - 1) in
3357 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
3358 let percent =
3359 if maxy <= 0
3360 then 100.
3361 else (100. *. (float state.y /. float maxy))
3363 if fn = ln
3364 then
3365 Printf.sprintf "page %d of %d [%.2f%%]"
3366 (fn+1) state.pagecount percent
3367 else
3368 Printf.sprintf
3369 "pages %d-%d of %d [%.2f%%]"
3370 (fn+1) (ln+1) state.pagecount percent
3373 let setpresentationmode v =
3374 let n = page_of_y state.y in
3375 state.anchor <- (n, 0.0, 1.0);
3376 conf.presentation <- v;
3377 if conf.fitmodel = FitPage
3378 then reqlayout conf.angle conf.fitmodel;
3379 represent ();
3382 let setbgcol (r, g, b) =
3383 let col =
3384 let r = r *. 255.0 |> truncate
3385 and g = g *. 255.0 |> truncate
3386 and b = b *. 255.0 |> truncate in
3387 r lsl 16 |> (lor) (g lsl 8) |> (lor) b
3389 Wsi.setwinbgcol col;
3392 let enterinfomode =
3393 let btos b = if b then UniSyms.radical else E.s in
3394 let showextended = ref false in
3395 let leave mode _ = state.mode <- mode in
3396 let src =
3397 (object
3398 val mutable m_l = []
3399 val mutable m_a = E.a
3400 val mutable m_prev_uioh = nouioh
3401 val mutable m_prev_mode = View
3403 inherit lvsourcebase
3405 method reset prev_mode prev_uioh =
3406 m_a <- Array.of_list (List.rev m_l);
3407 m_l <- [];
3408 m_prev_mode <- prev_mode;
3409 m_prev_uioh <- prev_uioh;
3411 method int name get set =
3412 m_l <-
3413 (name, `int get, 1,
3414 Action (
3415 fun u ->
3416 let ondone s =
3417 try set (int_of_string s)
3418 with exn ->
3419 state.text <- Printf.sprintf "bad integer `%s': %s"
3420 s @@ exntos exn
3422 state.text <- E.s;
3423 let te = name ^ ": ", E.s, None, intentry, ondone, true in
3424 state.mode <- Textentry (te, leave m_prev_mode);
3426 )) :: m_l
3428 method int_with_suffix name get set =
3429 m_l <-
3430 (name, `intws get, 1,
3431 Action (
3432 fun u ->
3433 let ondone s =
3434 try set (int_of_string_with_suffix s)
3435 with exn ->
3436 state.text <- Printf.sprintf "bad integer `%s': %s"
3437 s @@ exntos exn
3439 state.text <- E.s;
3440 let te =
3441 name ^ ": ", E.s, None, intentry_with_suffix, ondone, true
3443 state.mode <- Textentry (te, leave m_prev_mode);
3445 )) :: m_l
3447 method bool ?(offset=1) ?(btos=btos) name get set =
3448 m_l <-
3449 (name, `bool (btos, get), offset, Action (
3450 fun u ->
3451 let v = get () in
3452 set (not v);
3454 )) :: m_l
3456 method color name get set =
3457 m_l <-
3458 (name, `color get, 1,
3459 Action (
3460 fun u ->
3461 let invalid = (nan, nan, nan) in
3462 let ondone s =
3463 let c =
3464 try color_of_string s
3465 with exn ->
3466 state.text <- Printf.sprintf "bad color `%s': %s"
3467 s @@ exntos exn;
3468 invalid
3470 if c <> invalid
3471 then set c;
3473 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3474 state.text <- color_to_string (get ());
3475 state.mode <- Textentry (te, leave m_prev_mode);
3477 )) :: m_l
3479 method string name get set =
3480 m_l <-
3481 (name, `string get, 1,
3482 Action (
3483 fun u ->
3484 let ondone s = set s in
3485 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3486 state.mode <- Textentry (te, leave m_prev_mode);
3488 )) :: m_l
3490 method colorspace name get set =
3491 m_l <-
3492 (name, `string get, 1,
3493 Action (
3494 fun _ ->
3495 let source =
3496 (object
3497 inherit lvsourcebase
3499 initializer
3500 m_active <- CSTE.to_int conf.colorspace;
3501 m_first <- 0;
3503 method getitemcount =
3504 Array.length CSTE.names
3505 method getitem n =
3506 (CSTE.names.(n), 0)
3507 method exit ~uioh ~cancel ~active ~first ~pan =
3508 ignore (uioh, first, pan);
3509 if not cancel then set active;
3510 None
3511 method hasaction _ = true
3512 end)
3514 state.text <- E.s;
3515 let modehash = findkeyhash conf "info" in
3516 coe (new listview ~zebra:false ~helpmode:false
3517 ~source ~trusted:true ~modehash)
3518 )) :: m_l
3520 method paxmark name get set =
3521 m_l <-
3522 (name, `string get, 1,
3523 Action (
3524 fun _ ->
3525 let source =
3526 (object
3527 inherit lvsourcebase
3529 initializer
3530 m_active <- MTE.to_int conf.paxmark;
3531 m_first <- 0;
3533 method getitemcount = Array.length MTE.names
3534 method getitem n = (MTE.names.(n), 0)
3535 method exit ~uioh ~cancel ~active ~first ~pan =
3536 ignore (uioh, first, pan);
3537 if not cancel then set active;
3538 None
3539 method hasaction _ = true
3540 end)
3542 state.text <- E.s;
3543 let modehash = findkeyhash conf "info" in
3544 coe (new listview ~zebra:false ~helpmode:false
3545 ~source ~trusted:true ~modehash)
3546 )) :: m_l
3548 method fitmodel name get set =
3549 m_l <-
3550 (name, `string get, 1,
3551 Action (
3552 fun _ ->
3553 let source =
3554 (object
3555 inherit lvsourcebase
3557 initializer
3558 m_active <- FMTE.to_int conf.fitmodel;
3559 m_first <- 0;
3561 method getitemcount = Array.length FMTE.names
3562 method getitem n = (FMTE.names.(n), 0)
3563 method exit ~uioh ~cancel ~active ~first ~pan =
3564 ignore (uioh, first, pan);
3565 if not cancel then set active;
3566 None
3567 method hasaction _ = true
3568 end)
3570 state.text <- E.s;
3571 let modehash = findkeyhash conf "info" in
3572 coe (new listview ~zebra:false ~helpmode:false
3573 ~source ~trusted:true ~modehash)
3574 )) :: m_l
3576 method caption s offset =
3577 m_l <- (s, `empty, offset, Noaction) :: m_l
3579 method caption2 s f offset =
3580 m_l <- (s, `string f, offset, Noaction) :: m_l
3582 method getitemcount = Array.length m_a
3584 method getitem n =
3585 let tostr = function
3586 | `int f -> string_of_int (f ())
3587 | `intws f -> string_with_suffix_of_int (f ())
3588 | `string f -> f ()
3589 | `color f -> color_to_string (f ())
3590 | `bool (btos, f) -> btos (f ())
3591 | `empty -> E.s
3593 let name, t, offset, _ = m_a.(n) in
3594 ((let s = tostr t in
3595 if nonemptystr s
3596 then Printf.sprintf "%s\t%s" name s
3597 else name),
3598 offset)
3600 method exit ~uioh ~cancel ~active ~first ~pan =
3601 let uiohopt =
3602 if not cancel
3603 then (
3604 let uioh =
3605 match m_a.(active) with
3606 | _, _, _, Action f -> f uioh
3607 | _, _, _, Noaction -> uioh
3609 Some uioh
3611 else None
3613 m_active <- active;
3614 m_first <- first;
3615 m_pan <- pan;
3616 uiohopt
3618 method hasaction n =
3619 match m_a.(n) with
3620 | _, _, _, Action _ -> true
3621 | _, _, _, Noaction -> false
3623 initializer m_active <- 1
3624 end)
3626 let rec fillsrc prevmode prevuioh =
3627 let sep () = src#caption E.s 0 in
3628 let colorp name get set =
3629 src#string name
3630 (fun () -> color_to_string (get ()))
3631 (fun v ->
3633 let c = color_of_string v in
3634 set c
3635 with exn ->
3636 state.text <-
3637 Printf.sprintf "bad color `%s': %s" v @@ exntos exn
3640 let oldmode = state.mode in
3641 let birdseye = isbirdseye state.mode in
3643 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3645 src#bool "presentation mode"
3646 (fun () -> conf.presentation)
3647 (fun v -> setpresentationmode v);
3649 src#bool "ignore case in searches"
3650 (fun () -> conf.icase)
3651 (fun v -> conf.icase <- v);
3653 src#bool "preload"
3654 (fun () -> conf.preload)
3655 (fun v -> conf.preload <- v);
3657 src#bool "highlight links"
3658 (fun () -> conf.hlinks)
3659 (fun v -> conf.hlinks <- v);
3661 src#bool "under info"
3662 (fun () -> conf.underinfo)
3663 (fun v -> conf.underinfo <- v);
3665 src#bool "persistent bookmarks"
3666 (fun () -> conf.savebmarks)
3667 (fun v -> conf.savebmarks <- v);
3669 src#fitmodel "fit model"
3670 (fun () -> FMTE.to_string conf.fitmodel)
3671 (fun v -> reqlayout conf.angle (FMTE.of_int v));
3673 src#bool "trim margins"
3674 (fun () -> conf.trimmargins)
3675 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3677 src#bool "persistent location"
3678 (fun () -> conf.jumpback)
3679 (fun v -> conf.jumpback <- v);
3681 sep ();
3682 src#int "inter-page space"
3683 (fun () -> conf.interpagespace)
3684 (fun n ->
3685 conf.interpagespace <- n;
3686 docolumns conf.columns;
3687 let pageno, py =
3688 match state.layout with
3689 | [] -> 0, 0
3690 | l :: _ ->
3691 l.pageno, l.pagey
3693 state.maxy <- calcheight ();
3694 let y = getpagey pageno in
3695 gotoxy state.x (y + py)
3698 src#int "page bias"
3699 (fun () -> conf.pagebias)
3700 (fun v -> conf.pagebias <- v);
3702 src#int "scroll step"
3703 (fun () -> conf.scrollstep)
3704 (fun n -> conf.scrollstep <- n);
3706 src#int "horizontal scroll step"
3707 (fun () -> conf.hscrollstep)
3708 (fun v -> conf.hscrollstep <- v);
3710 src#int "auto scroll step"
3711 (fun () ->
3712 match state.autoscroll with
3713 | Some step -> step
3714 | _ -> conf.autoscrollstep)
3715 (fun n ->
3716 let n = boundastep state.winh n in
3717 if state.autoscroll <> None
3718 then state.autoscroll <- Some n;
3719 conf.autoscrollstep <- n);
3721 src#int "zoom"
3722 (fun () -> truncate (conf.zoom *. 100.))
3723 (fun v -> pivotzoom ((float v) /. 100.));
3725 src#int "rotation"
3726 (fun () -> conf.angle)
3727 (fun v -> reqlayout v conf.fitmodel);
3729 src#int "scroll bar width"
3730 (fun () -> conf.scrollbw)
3731 (fun v ->
3732 conf.scrollbw <- v;
3733 reshape state.winw state.winh;
3736 src#int "scroll handle height"
3737 (fun () -> conf.scrollh)
3738 (fun v -> conf.scrollh <- v;);
3740 src#int "thumbnail width"
3741 (fun () -> conf.thumbw)
3742 (fun v ->
3743 conf.thumbw <- min 4096 v;
3744 match oldmode with
3745 | Birdseye beye ->
3746 leavebirdseye beye false;
3747 enterbirdseye ()
3748 | Textentry _
3749 | View
3750 | LinkNav _ -> ()
3753 let mode = state.mode in
3754 src#string "columns"
3755 (fun () ->
3756 match conf.columns with
3757 | Csingle _ -> "1"
3758 | Cmulti (multi, _) -> multicolumns_to_string multi
3759 | Csplit (count, _) -> "-" ^ string_of_int count
3761 (fun v ->
3762 let n, a, b = multicolumns_of_string v in
3763 setcolumns mode n a b);
3765 sep ();
3766 src#caption "Pixmap cache" 0;
3767 src#int_with_suffix "size (advisory)"
3768 (fun () -> conf.memlimit)
3769 (fun v -> conf.memlimit <- v);
3771 src#caption2 "used"
3772 (fun () ->
3773 Printf.sprintf "%s bytes, %d tiles"
3774 (string_with_suffix_of_int state.memused)
3775 (Hashtbl.length state.tilemap)) 1;
3777 sep ();
3778 src#caption "Layout" 0;
3779 src#caption2 "Dimension"
3780 (fun () ->
3781 Printf.sprintf "%dx%d (virtual %dx%d)"
3782 state.winw state.winh
3783 state.w state.maxy)
3785 if conf.debug
3786 then
3787 src#caption2 "Position" (fun () ->
3788 Printf.sprintf "%dx%d" state.x state.y
3790 else
3791 src#caption2 "Position" (fun () -> describe_location ()) 1
3794 sep ();
3795 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3796 "Save these parameters as global defaults at exit"
3797 (fun () -> conf.bedefault)
3798 (fun v -> conf.bedefault <- v)
3801 sep ();
3802 let btos b = if b then UniSyms.lguillemet else UniSyms.rguillemet in
3803 src#bool ~offset:0 ~btos "Extended parameters"
3804 (fun () -> !showextended)
3805 (fun v -> showextended := v; fillsrc prevmode prevuioh);
3806 if !showextended
3807 then (
3808 src#bool "checkers"
3809 (fun () -> conf.checkers)
3810 (fun v -> conf.checkers <- v; setcheckers v);
3811 src#bool "update cursor"
3812 (fun () -> conf.updatecurs)
3813 (fun v -> conf.updatecurs <- v);
3814 src#bool "scroll-bar on the left"
3815 (fun () -> conf.leftscroll)
3816 (fun v -> conf.leftscroll <- v);
3817 src#bool "verbose"
3818 (fun () -> conf.verbose)
3819 (fun v -> conf.verbose <- v);
3820 src#bool "invert colors"
3821 (fun () -> conf.invert)
3822 (fun v -> conf.invert <- v);
3823 src#bool "max fit"
3824 (fun () -> conf.maxhfit)
3825 (fun v -> conf.maxhfit <- v);
3826 src#bool "pax mode"
3827 (fun () -> conf.pax != None)
3828 (fun v ->
3829 if v
3830 then conf.pax <- Some (ref (now (), 0, 0))
3831 else conf.pax <- None);
3832 src#string "uri launcher"
3833 (fun () -> conf.urilauncher)
3834 (fun v -> conf.urilauncher <- v);
3835 src#string "path launcher"
3836 (fun () -> conf.pathlauncher)
3837 (fun v -> conf.pathlauncher <- v);
3838 src#string "tile size"
3839 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3840 (fun v ->
3842 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
3843 conf.tilew <- max 64 w;
3844 conf.tileh <- max 64 h;
3845 flushtiles ();
3846 with exn ->
3847 state.text <- Printf.sprintf "bad tile size `%s': %s"
3848 v @@ exntos exn
3850 src#int "texture count"
3851 (fun () -> conf.texcount)
3852 (fun v ->
3853 if realloctexts v
3854 then conf.texcount <- v
3855 else impmsg "failed to set texture count please retry later"
3857 src#int "slice height"
3858 (fun () -> conf.sliceheight)
3859 (fun v ->
3860 conf.sliceheight <- v;
3861 wcmd "sliceh %d" conf.sliceheight;
3863 src#int "anti-aliasing level"
3864 (fun () -> conf.aalevel)
3865 (fun v ->
3866 conf.aalevel <- bound v 0 8;
3867 state.anchor <- getanchor ();
3868 opendoc state.path state.password;
3870 src#string "page scroll scaling factor"
3871 (fun () -> string_of_float conf.pgscale)
3872 (fun v ->
3874 let s = float_of_string v in
3875 conf.pgscale <- s
3876 with exn ->
3877 state.text <- Printf.sprintf
3878 "bad page scroll scaling factor `%s': %s" v
3879 @@ exntos exn
3882 src#int "ui font size"
3883 (fun () -> fstate.fontsize)
3884 (fun v -> setfontsize (bound v 5 100));
3885 src#int "hint font size"
3886 (fun () -> conf.hfsize)
3887 (fun v -> conf.hfsize <- bound v 5 100);
3888 colorp "background color"
3889 (fun () -> conf.bgcolor)
3890 (fun v -> conf.bgcolor <- v; setbgcol v);
3891 src#bool "crop hack"
3892 (fun () -> conf.crophack)
3893 (fun v -> conf.crophack <- v);
3894 src#string "trim fuzz"
3895 (fun () -> irect_to_string conf.trimfuzz)
3896 (fun v ->
3898 conf.trimfuzz <- irect_of_string v;
3899 if conf.trimmargins
3900 then settrim true conf.trimfuzz;
3901 with exn ->
3902 state.text <- Printf.sprintf "bad irect `%s': %s" v
3903 @@ exntos exn
3905 src#string "throttle"
3906 (fun () ->
3907 match conf.maxwait with
3908 | None -> "show place holder if page is not ready"
3909 | Some time ->
3910 if time = infinity
3911 then "wait for page to fully render"
3912 else
3913 "wait " ^ string_of_float time
3914 ^ " seconds before showing placeholder"
3916 (fun v ->
3918 let f = float_of_string v in
3919 if f <= 0.0
3920 then conf.maxwait <- None
3921 else conf.maxwait <- Some f
3922 with exn ->
3923 state.text <- Printf.sprintf "bad time `%s': %s" v
3924 @@ exntos exn
3926 src#string "ghyll scroll"
3927 (fun () ->
3928 match conf.ghyllscroll with
3929 | None -> E.s
3930 | Some nab -> ghyllscroll_to_string nab
3932 (fun v ->
3933 try conf.ghyllscroll <- ghyllscroll_of_string v
3934 with
3935 | Failure msg ->
3936 state.text <- Printf.sprintf "bad ghyll `%s': %s" v msg
3937 | exn ->
3938 state.text <- Printf.sprintf "bad ghyll `%s': %s" v
3939 @@ exntos exn
3941 src#string "selection command"
3942 (fun () -> conf.selcmd)
3943 (fun v -> conf.selcmd <- v);
3944 src#string "synctex command"
3945 (fun () -> conf.stcmd)
3946 (fun v -> conf.stcmd <- v);
3947 src#string "pax command"
3948 (fun () -> conf.paxcmd)
3949 (fun v -> conf.paxcmd <- v);
3950 src#string "ask password command"
3951 (fun () -> conf.passcmd)
3952 (fun v -> conf.passcmd <- v);
3953 src#string "save path command"
3954 (fun () -> conf.savecmd)
3955 (fun v -> conf.savecmd <- v);
3956 src#colorspace "color space"
3957 (fun () -> CSTE.to_string conf.colorspace)
3958 (fun v ->
3959 conf.colorspace <- CSTE.of_int v;
3960 wcmd "cs %d" v;
3961 load state.layout;
3963 src#paxmark "pax mark method"
3964 (fun () -> MTE.to_string conf.paxmark)
3965 (fun v -> conf.paxmark <- MTE.of_int v);
3966 if bousable () && !opengl_has_pbo
3967 then
3968 src#bool "use PBO"
3969 (fun () -> conf.usepbo)
3970 (fun v -> conf.usepbo <- v);
3971 src#bool "mouse wheel scrolls pages"
3972 (fun () -> conf.wheelbypage)
3973 (fun v -> conf.wheelbypage <- v);
3974 src#bool "open remote links in a new instance"
3975 (fun () -> conf.riani)
3976 (fun v -> conf.riani <- v);
3977 src#bool "edit annotations inline"
3978 (fun () -> conf.annotinline)
3979 (fun v -> conf.annotinline <- v);
3980 src#bool "coarse positioning in presentation mode"
3981 (fun () -> conf.coarseprespos)
3982 (fun v -> conf.coarseprespos <- v);
3983 src#bool "use document CSS"
3984 (fun () -> conf.usedoccss)
3985 (fun v ->
3986 conf.usedoccss <- v;
3987 state.anchor <- getanchor ();
3988 opendoc state.path state.password;
3992 sep ();
3993 src#caption "Document" 0;
3994 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
3995 src#caption2 "Pages"
3996 (fun () -> string_of_int state.pagecount) 1;
3997 src#caption2 "Dimensions"
3998 (fun () -> string_of_int (List.length state.pdims)) 1;
3999 if nonemptystr conf.css
4000 then src#caption2 "CSS" (fun () -> conf.css) 1;
4001 if conf.trimmargins
4002 then (
4003 sep ();
4004 src#caption "Trimmed margins" 0;
4005 src#caption2 "Dimensions"
4006 (fun () -> string_of_int (List.length state.pdims)) 1;
4009 sep ();
4010 src#caption "OpenGL" 0;
4011 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4012 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4014 sep ();
4015 src#caption "Location" 0;
4016 if nonemptystr state.origin
4017 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
4018 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
4020 src#reset prevmode prevuioh;
4022 fun () ->
4023 state.text <- E.s;
4024 resetmstate ();
4025 let prevmode = state.mode
4026 and prevuioh = state.uioh in
4027 fillsrc prevmode prevuioh;
4028 let source = (src :> lvsource) in
4029 let modehash = findkeyhash conf "info" in
4030 state.uioh <-
4031 coe (object (self)
4032 inherit listview ~zebra:false ~helpmode:false
4033 ~source ~trusted:true ~modehash as super
4034 val mutable m_prevmemused = 0
4035 method! infochanged = function
4036 | Memused ->
4037 if m_prevmemused != state.memused
4038 then (
4039 m_prevmemused <- state.memused;
4040 G.postRedisplay "memusedchanged";
4042 | Pdim -> G.postRedisplay "pdimchanged"
4043 | Docinfo -> fillsrc prevmode prevuioh
4045 method! key key mask =
4046 if not (Wsi.withctrl mask)
4047 then
4048 let open Keys in
4049 begin [@warning "-4"]
4050 match Wsi.kc2pv key with
4051 | Left | KPleft -> coe (self#updownlevel ~-1)
4052 | Right | KPright -> coe (self#updownlevel 1)
4053 | _ -> super#key key mask
4055 else super#key key mask
4056 end);
4057 G.postRedisplay "info";
4060 let enterhelpmode =
4061 let source =
4062 (object
4063 inherit lvsourcebase
4064 method getitemcount = Array.length state.help
4065 method getitem n =
4066 let s, l, _ = state.help.(n) in
4067 (s, l)
4069 method exit ~uioh ~cancel ~active ~first ~pan =
4070 let optuioh =
4071 if not cancel
4072 then (
4073 match state.help.(active) with
4074 | _, _, Action f -> Some (f uioh)
4075 | _, _, Noaction -> Some uioh
4077 else None
4079 m_active <- active;
4080 m_first <- first;
4081 m_pan <- pan;
4082 optuioh
4084 method hasaction n =
4085 match state.help.(n) with
4086 | _, _, Action _ -> true
4087 | _, _, Noaction -> false
4089 initializer
4090 m_active <- -1
4091 end)
4092 in fun () ->
4093 let modehash = findkeyhash conf "help" in
4094 resetmstate ();
4095 state.uioh <- coe (new listview
4096 ~zebra:false ~helpmode:true
4097 ~source ~trusted:true ~modehash);
4098 G.postRedisplay "help";
4101 let entermsgsmode =
4102 let msgsource =
4103 (object
4104 inherit lvsourcebase
4105 val mutable m_items = E.a
4107 method getitemcount = 1 + Array.length m_items
4109 method getitem n =
4110 if n = 0
4111 then "[Clear]", 0
4112 else m_items.(n-1), 0
4114 method exit ~uioh ~cancel ~active ~first ~pan =
4115 ignore uioh;
4116 if not cancel
4117 then (
4118 if active = 0
4119 then Buffer.clear state.errmsgs;
4121 m_active <- active;
4122 m_first <- first;
4123 m_pan <- pan;
4124 None
4126 method hasaction n =
4127 n = 0
4129 method reset =
4130 state.newerrmsgs <- false;
4131 let l = Str.split newlinere (Buffer.contents state.errmsgs) in
4132 m_items <- Array.of_list l
4134 initializer
4135 m_active <- 0
4136 end)
4137 in fun () ->
4138 state.text <- E.s;
4139 resetmstate ();
4140 msgsource#reset;
4141 let source = (msgsource :> lvsource) in
4142 let modehash = findkeyhash conf "listview" in
4143 state.uioh <-
4144 coe (object
4145 inherit listview ~zebra:false ~helpmode:false
4146 ~source ~trusted:false ~modehash as super
4147 method! display =
4148 if state.newerrmsgs
4149 then msgsource#reset;
4150 super#display
4151 end);
4152 G.postRedisplay "msgs";
4155 let getusertext s =
4156 let editor = getenvwithdef "EDITOR" E.s in
4157 if emptystr editor
4158 then E.s
4159 else
4160 let tmppath = Filename.temp_file "llpp" "note" in
4161 if nonemptystr s
4162 then (
4163 let oc = open_out tmppath in
4164 output_string oc s;
4165 close_out oc;
4167 let execstr = editor ^ " " ^ tmppath in
4168 let s =
4169 match spawn execstr [] with
4170 | (exception exn) ->
4171 impmsg "spawn(%S) failed: %s" execstr @@ exntos exn;
4173 | pid ->
4174 match Unix.waitpid [] pid with
4175 | (exception exn) ->
4176 impmsg "waitpid(%d) failed: %s" pid @@ exntos exn;
4178 | (_pid, status) ->
4179 match status with
4180 | Unix.WEXITED 0 -> filecontents tmppath
4181 | Unix.WEXITED n ->
4182 impmsg "editor process(%s) exited abnormally: %d" execstr n;
4184 | Unix.WSIGNALED n ->
4185 impmsg "editor process(%s) was killed by signal %d" execstr n;
4187 | Unix.WSTOPPED n ->
4188 impmsg "editor(%s) process was stopped by signal %d" execstr n;
4191 match Unix.unlink tmppath with
4192 | (exception exn) ->
4193 impmsg "failed to ulink %S: %s" tmppath @@ exntos exn;
4195 | () -> s
4198 let enterannotmode opaque slinkindex =
4199 let msgsource =
4200 (object
4201 inherit lvsourcebase
4202 val mutable m_text = E.s
4203 val mutable m_items = E.a
4205 method getitemcount = Array.length m_items
4207 method getitem n =
4208 let label, _func = m_items.(n) in
4209 label, 0
4211 method exit ~uioh ~cancel ~active ~first ~pan =
4212 ignore (uioh, first, pan);
4213 if not cancel
4214 then (
4215 let _label, func = m_items.(active) in
4216 func ()
4218 None
4220 method hasaction n = nonemptystr @@ fst m_items.(n)
4222 method reset s =
4223 let rec split accu b i =
4224 let p = b+i in
4225 if p = String.length s
4226 then (String.sub s b (p-b), unit) :: accu
4227 else
4228 if (i > 70 && s.[p] = ' ') || s.[p] = '\r' || s.[p] = '\n'
4229 then
4230 let ss = if i = 0 then E.s else String.sub s b i in
4231 split ((ss, unit)::accu) (p+1) 0
4232 else
4233 split accu b (i+1)
4235 let cleanup () =
4236 wcmd "freepage %s" (~> opaque);
4237 let keys =
4238 Hashtbl.fold (fun key opaque' accu ->
4239 if opaque' = opaque'
4240 then key :: accu else accu) state.pagemap []
4242 List.iter (Hashtbl.remove state.pagemap) keys;
4243 flushtiles ();
4244 gotoxy state.x state.y
4246 let dele () =
4247 delannot opaque slinkindex;
4248 cleanup ();
4250 let edit inline () =
4251 let update s =
4252 if emptystr s
4253 then dele ()
4254 else (
4255 modannot opaque slinkindex s;
4256 cleanup ();
4259 if inline
4260 then
4261 let mode = state.mode in
4262 state.mode <-
4263 Textentry (
4264 ("annotation: ", m_text, None, textentry, update, true),
4265 fun _ -> state.mode <- mode);
4266 state.text <- E.s;
4267 enttext ();
4268 else
4269 let s = getusertext m_text in
4270 update s
4272 m_text <- s;
4273 m_items <-
4274 ( "[Copy]", fun () -> selstring m_text)
4275 :: ("[Delete]", dele)
4276 :: ("[Edit]", edit conf.annotinline)
4277 :: (E.s, unit)
4278 :: split [] 0 0 |> List.rev |> Array.of_list
4280 initializer
4281 m_active <- 0
4282 end)
4284 state.text <- E.s;
4285 let s = getannotcontents opaque slinkindex in
4286 resetmstate ();
4287 msgsource#reset s;
4288 let source = (msgsource :> lvsource) in
4289 let modehash = findkeyhash conf "listview" in
4290 state.uioh <- coe (object
4291 inherit listview ~zebra:false ~helpmode:false
4292 ~source ~trusted:false ~modehash
4293 end);
4294 G.postRedisplay "enterannotmode";
4297 let gotoremote spec =
4298 let filename, dest = splitatchar spec '#' in
4299 let getpath filename =
4300 let path =
4301 if nonemptystr filename
4302 then
4303 if Filename.is_relative filename
4304 then
4305 let dir = Filename.dirname state.path in
4306 let dir =
4307 if Filename.is_implicit dir
4308 then Filename.concat (Sys.getcwd ()) dir
4309 else dir
4311 Filename.concat dir filename
4312 else filename
4313 else E.s
4315 if Sys.file_exists path
4316 then path
4317 else E.s
4319 let path = getpath filename in
4320 let dospawn lcmd =
4321 if conf.riani
4322 then
4323 let cmd = Lazy.force_val lcmd in
4324 match spawn cmd with
4325 | _pid -> ()
4326 | (exception exn) ->
4327 dolog "failed to execute `%s': %s" cmd @@ exntos exn
4328 else
4329 let anchor = getanchor () in
4330 let ranchor = state.path, state.password, anchor, state.origin in
4331 state.origin <- E.s;
4332 state.ranchors <- ranchor :: state.ranchors;
4333 opendoc path E.s;
4335 if substratis spec 0 "page="
4336 then
4337 match Scanf.sscanf spec "page=%d" (fun n -> n) with
4338 | pageno ->
4339 state.anchor <- (pageno, 0.0, 0.0);
4340 dospawn @@ lazy (Printf.sprintf "%s -page %d %S" !selfexec pageno path);
4341 | exception exn ->
4342 adderrfmt "error parsing remote destination" "page: %s" @@ exntos exn
4343 else (
4344 state.nameddest <- dest;
4345 dospawn @@ lazy (!selfexec ^ " " ^ path ^ " -dest " ^ dest)
4349 let gotounder = function
4350 | Ulinkuri s when isexternallink s ->
4351 if substratis s 0 "file://"
4352 then gotoremote @@ String.sub s 7 (String.length s - 7)
4353 else gotouri s
4354 | Ulinkuri s ->
4355 let pageno, x, y = uritolocation s in
4356 addnav ();
4357 gotopagexy !wtmode pageno x y
4358 | Utext _ | Unone -> ()
4359 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
4362 let gotooutline (_, _, kind) =
4363 match kind with
4364 | Onone -> ()
4365 | Oanchor anchor ->
4366 let (pageno, y, _) = anchor in
4367 let y = getanchory
4368 (if conf.presentation then (pageno, y, 1.0) else anchor)
4370 addnav ();
4371 gotoghyll y
4372 | Ouri uri -> gotounder (Ulinkuri uri)
4373 | Olaunch _cmd -> failwith "gotounder (Ulaunch cmd)"
4374 | Oremote _remote -> failwith "gotounder (Uremote remote)"
4375 | Ohistory hist -> gotohist hist
4376 | Oremotedest _remotedest -> failwith "gotounder (Uremotedest remotedest)"
4379 class outlinesoucebase fetchoutlines = object (self)
4380 inherit lvsourcebase
4381 val mutable m_items = E.a
4382 val mutable m_minfo = E.a
4383 val mutable m_orig_items = E.a
4384 val mutable m_orig_minfo = E.a
4385 val mutable m_narrow_patterns = []
4386 val mutable m_gen = -1
4388 method getitemcount = Array.length m_items
4390 method getitem n =
4391 let s, n, _ = m_items.(n) in
4392 (s, n+0)
4394 method exit ~(uioh:uioh) ~cancel ~active ~(first:int) ~pan : uioh option =
4395 ignore (uioh, first);
4396 let items, minfo =
4397 if m_narrow_patterns = []
4398 then m_orig_items, m_orig_minfo
4399 else m_items, m_minfo
4401 m_pan <- pan;
4402 if not cancel
4403 then (
4404 m_items <- items;
4405 m_minfo <- minfo;
4406 gotooutline m_items.(active);
4408 else (
4409 m_items <- items;
4410 m_minfo <- minfo;
4412 None
4414 method hasaction (_:int) = true
4416 method greetmsg =
4417 if Array.length m_items != Array.length m_orig_items
4418 then
4419 let s =
4420 match m_narrow_patterns with
4421 | one :: [] -> one
4422 | many -> String.concat UniSyms.ellipsis (List.rev many)
4424 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
4425 else E.s
4427 method statestr =
4428 match m_narrow_patterns with
4429 | [] -> E.s
4430 | one :: [] -> one
4431 | head :: _ -> UniSyms.ellipsis ^ head
4433 method narrow pattern =
4434 match Str.regexp_case_fold pattern with
4435 | (exception _) -> ()
4436 | re ->
4437 let rec loop accu minfo n =
4438 if n = -1
4439 then (
4440 m_items <- Array.of_list accu;
4441 m_minfo <- Array.of_list minfo;
4443 else
4444 let (s, _, _) as o = m_items.(n) in
4445 let accu, minfo =
4446 match Str.search_forward re s 0 with
4447 | (exception Not_found) -> accu, minfo
4448 | first -> o :: accu, (first, Str.match_end ()) :: minfo
4450 loop accu minfo (n-1)
4452 loop [] [] (Array.length m_items - 1)
4454 method! getminfo = m_minfo
4456 method denarrow =
4457 m_orig_items <- fetchoutlines ();
4458 m_minfo <- m_orig_minfo;
4459 m_items <- m_orig_items
4461 method add_narrow_pattern pattern =
4462 m_narrow_patterns <- pattern :: m_narrow_patterns
4464 method del_narrow_pattern =
4465 match m_narrow_patterns with
4466 | _ :: rest -> m_narrow_patterns <- rest
4467 | [] -> ()
4469 method renarrow =
4470 self#denarrow;
4471 match m_narrow_patterns with
4472 | pattern :: [] -> self#narrow pattern; pattern
4473 | list ->
4474 List.fold_left (fun accu pattern ->
4475 self#narrow pattern;
4476 pattern ^ UniSyms.ellipsis ^ accu) E.s list
4478 method calcactive (_:anchor) = 0
4480 method reset anchor items =
4481 if state.gen != m_gen
4482 then (
4483 m_orig_items <- items;
4484 m_items <- items;
4485 m_narrow_patterns <- [];
4486 m_minfo <- E.a;
4487 m_orig_minfo <- E.a;
4488 m_gen <- state.gen;
4490 else (
4491 if items != m_orig_items
4492 then (
4493 m_orig_items <- items;
4494 if m_narrow_patterns == []
4495 then m_items <- items;
4498 let active = self#calcactive anchor in
4499 m_active <- active;
4500 m_first <- firstof m_first active
4504 let outlinesource fetchoutlines =
4505 (object
4506 inherit outlinesoucebase fetchoutlines
4507 method! calcactive anchor =
4508 let rely = getanchory anchor in
4509 let rec loop n best bestd =
4510 if n = Array.length m_items
4511 then best
4512 else
4513 let _, _, kind = m_items.(n) in
4514 match kind with
4515 | Oanchor anchor ->
4516 let orely = getanchory anchor in
4517 let d = abs (orely - rely) in
4518 if d < bestd
4519 then loop (n+1) n d
4520 else loop (n+1) best bestd
4521 | Onone | Oremote _ | Olaunch _
4522 | Oremotedest _ | Ouri _ | Ohistory _ ->
4523 loop (n+1) best bestd
4525 loop 0 ~-1 max_int
4526 end)
4529 let enteroutlinemode, enterbookmarkmode, enterhistmode =
4530 let mkselector sourcetype =
4531 let fetchoutlines () =
4532 match sourcetype with
4533 | `bookmarks -> Array.of_list state.bookmarks
4534 | `outlines -> state.outlines
4535 | `history -> genhistoutlines ()
4537 let source =
4538 if sourcetype = `history
4539 then new outlinesoucebase fetchoutlines
4540 else outlinesource fetchoutlines
4542 fun errmsg ->
4543 let outlines = fetchoutlines () in
4544 if Array.length outlines = 0
4545 then (
4546 showtext ' ' errmsg;
4548 else (
4549 resetmstate ();
4550 Wsi.setcursor Wsi.CURSOR_INHERIT;
4551 let anchor = getanchor () in
4552 source#reset anchor outlines;
4553 state.text <- source#greetmsg;
4554 state.uioh <-
4555 coe (new outlinelistview ~zebra:(sourcetype=`history) ~source);
4556 G.postRedisplay "enter selector";
4559 let mkenter sourcetype errmsg =
4560 let enter = mkselector sourcetype in
4561 fun () -> enter errmsg
4563 mkenter `outlines "document has no outline"
4564 , mkenter `bookmarks "document has no bookmarks (yet)"
4565 , mkenter `history "history is empty"
4568 let quickbookmark ?title () =
4569 match state.layout with
4570 | [] -> ()
4571 | l :: _ ->
4572 let title =
4573 match title with
4574 | None ->
4575 let tm = Unix.localtime (now ()) in
4576 Printf.sprintf
4577 "Quick (page %d) (bookmarked at %02d/%02d/%d %02d:%02d)"
4578 (l.pageno+1)
4579 tm.Unix.tm_mday
4580 (tm.Unix.tm_mon+1)
4581 (tm.Unix.tm_year + 1900)
4582 tm.Unix.tm_hour
4583 tm.Unix.tm_min
4584 | Some title -> title
4586 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4589 let setautoscrollspeed step goingdown =
4590 let incr = max 1 ((abs step) / 2) in
4591 let incr = if goingdown then incr else -incr in
4592 let astep = boundastep state.winh (step + incr) in
4593 state.autoscroll <- Some astep;
4596 let canpan () =
4597 match conf.columns with
4598 | Csplit _ -> true
4599 | Csingle _ | Cmulti _ -> state.x != 0 || conf.zoom > 1.0
4602 let panbound x = bound x (-state.w) state.winw;;
4604 let existsinrow pageno (columns, coverA, coverB) p =
4605 let last = ((pageno - coverA) mod columns) + columns in
4606 let rec any = function
4607 | [] -> false
4608 | l :: rest ->
4609 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4610 then p l
4611 else (
4612 if not (p l)
4613 then (if l.pageno = last then false else any rest)
4614 else true
4617 any state.layout
4620 let nextpage () =
4621 match state.layout with
4622 | [] ->
4623 let pageno = page_of_y state.y in
4624 gotoghyll (getpagey (pageno+1))
4625 | l :: rest ->
4626 match conf.columns with
4627 | Csingle _ ->
4628 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4629 then
4630 let y = clamp (pgscale state.winh) in
4631 gotoghyll y
4632 else
4633 let pageno = min (l.pageno+1) (state.pagecount-1) in
4634 gotoghyll (getpagey pageno)
4635 | Cmulti ((c, _, _) as cl, _) ->
4636 if conf.presentation
4637 && (existsinrow l.pageno cl
4638 (fun l -> l.pageh > l.pagey + l.pagevh))
4639 then
4640 let y = clamp (pgscale state.winh) in
4641 gotoghyll y
4642 else
4643 let pageno = min (l.pageno+c) (state.pagecount-1) in
4644 gotoghyll (getpagey pageno)
4645 | Csplit (n, _) ->
4646 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4647 then
4648 let pagey, pageh = getpageyh l.pageno in
4649 let pagey = pagey + pageh * l.pagecol in
4650 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4651 gotoghyll (pagey + pageh + ips)
4654 let prevpage () =
4655 match state.layout with
4656 | [] ->
4657 let pageno = page_of_y state.y in
4658 gotoghyll (getpagey (pageno-1))
4659 | l :: _ ->
4660 match conf.columns with
4661 | Csingle _ ->
4662 if conf.presentation && l.pagey != 0
4663 then
4664 gotoghyll (clamp (pgscale ~-(state.winh)))
4665 else
4666 let pageno = max 0 (l.pageno-1) in
4667 gotoghyll (getpagey pageno)
4668 | Cmulti ((c, _, coverB) as cl, _) ->
4669 if conf.presentation &&
4670 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4671 then
4672 gotoghyll (clamp (pgscale ~-(state.winh)))
4673 else
4674 let decr =
4675 if l.pageno = state.pagecount - coverB
4676 then 1
4677 else c
4679 let pageno = max 0 (l.pageno-decr) in
4680 gotoghyll (getpagey pageno)
4681 | Csplit (n, _) ->
4682 let y =
4683 if l.pagecol = 0
4684 then
4685 if l.pageno = 0
4686 then l.pagey
4687 else
4688 let pageno = max 0 (l.pageno-1) in
4689 let pagey, pageh = getpageyh pageno in
4690 pagey + (n-1)*pageh
4691 else
4692 let pagey, pageh = getpageyh l.pageno in
4693 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4695 gotoghyll y
4698 let save () =
4699 if emptystr conf.savecmd
4700 then error "don't know where to save modified document"
4701 else
4702 let savecmd = Str.global_replace percentsre state.path conf.savecmd in
4703 let path =
4704 getcmdoutput
4705 (fun s -> error "failed to obtain path to the saved copy: %s" s)
4706 savecmd
4708 if nonemptystr path
4709 then
4710 let tmp = path ^ ".tmp" in
4711 savedoc tmp;
4712 Unix.rename tmp path;
4715 let viewkeyboard key mask =
4716 let enttext te =
4717 let mode = state.mode in
4718 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4719 state.text <- E.s;
4720 enttext ();
4721 G.postRedisplay "view:enttext"
4723 let ctrl = Wsi.withctrl mask in
4724 let key = Wsi.keypadtodigitkey key in
4725 let open Keys in
4726 match Wsi.kc2pv key with
4727 | Ascii 'Q' -> exit 0
4729 | Ascii 'W' ->
4730 if hasunsavedchanges ()
4731 then save ()
4733 | Insert ->
4734 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4735 then (
4736 state.mode <- (
4737 match state.lnava with
4738 | None -> LinkNav (Ltgendir 0)
4739 | Some pn -> LinkNav (Ltexact pn)
4741 gotoxy state.x state.y;
4743 else impmsg "keyboard link navigation does not work under rotation"
4745 | Escape | Ascii 'q' ->
4746 begin match state.mstate with
4747 | Mzoomrect _ ->
4748 resetmstate ();
4749 G.postRedisplay "kill rect";
4750 | Msel _
4751 | Mpan _
4752 | Mscrolly | Mscrollx
4753 | Mzoom _
4754 | Mnone ->
4755 begin match state.mode with
4756 | LinkNav ln ->
4757 begin match ln with
4758 | Ltexact pl -> state.lnava <- Some pl
4759 | Ltgendir _ | Ltnotready _ -> state.lnava <- None
4760 end;
4761 state.mode <- View;
4762 G.postRedisplay "esc leave linknav"
4763 | Birdseye _
4764 | Textentry _
4765 | View ->
4766 match state.ranchors with
4767 | [] -> raise Quit
4768 | (path, password, anchor, origin) :: rest ->
4769 state.ranchors <- rest;
4770 state.anchor <- anchor;
4771 state.origin <- origin;
4772 state.nameddest <- E.s;
4773 opendoc path password
4774 end;
4775 end;
4777 | Backspace ->
4778 gotoghyll (getnav ~-1)
4780 | Ascii 'o' ->
4781 enteroutlinemode ()
4783 | Ascii 'H' ->
4784 enterhistmode ()
4786 | Ascii 'u' ->
4787 state.rects <- [];
4788 state.text <- E.s;
4789 Hashtbl.iter (fun _ opaque ->
4790 clearmark opaque;
4791 Hashtbl.clear state.prects) state.pagemap;
4792 G.postRedisplay "dehighlight";
4794 | (Ascii '/' | Ascii '?') as pv ->
4795 let ondone isforw s =
4796 cbput state.hists.pat s;
4797 state.searchpattern <- s;
4798 search s isforw
4800 let s = String.make 1 (Char.chr key) in
4801 enttext (s, E.s, Some (onhist state.hists.pat),
4802 textentry, ondone (pv = Ascii '/'), true)
4804 | Ascii '+' | KPplus | Ascii '=' when ctrl ->
4805 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4806 pivotzoom (conf.zoom +. incr)
4808 | Ascii '+' | KPplus ->
4809 let ondone s =
4810 let n =
4811 try int_of_string s with exn ->
4812 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn;
4813 max_int
4815 if n != max_int
4816 then (
4817 conf.pagebias <- n;
4818 state.text <- "page bias is now " ^ string_of_int n;
4821 enttext ("page bias: ", E.s, None, intentry, ondone, true)
4823 | Ascii '-' | KPminus when ctrl ->
4824 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4825 pivotzoom (max 0.01 (conf.zoom -. decr))
4827 | Ascii '-' | KPminus ->
4828 let ondone msg = state.text <- msg in
4829 enttext (
4830 "option [acfhilpstvxACFPRSZTISM]: ", E.s, None,
4831 optentry state.mode, ondone, true
4834 | Ascii '0' when ctrl ->
4835 if conf.zoom = 1.0
4836 then gotoxy 0 state.y
4837 else setzoom 1.0
4839 | Ascii ('1'|'2' as c)
4840 when ctrl && conf.fitmodel != FitPage ->
4841 let cols =
4842 match conf.columns with
4843 | Csingle _ | Cmulti _ -> 1
4844 | Csplit (n, _) -> n
4846 let h = state.winh -
4847 conf.interpagespace lsl (if conf.presentation then 1 else 0)
4849 let zoom = zoomforh state.winw h 0 cols in
4850 if zoom > 0.0 && (c = '2' || zoom < 1.0)
4851 then setzoom zoom
4853 | Ascii '3' when ctrl ->
4854 let fm =
4855 match conf.fitmodel with
4856 | FitWidth -> FitProportional
4857 | FitProportional -> FitPage
4858 | FitPage -> FitWidth
4860 state.text <- "fit model: " ^ FMTE.to_string fm;
4861 reqlayout conf.angle fm
4863 | Ascii '4' when ctrl ->
4864 let zoom = getmaxw () /. float state.winw in
4865 if zoom > 0.0 then setzoom zoom
4867 | Fn 9 ->
4868 togglebirdseye ()
4870 | Ascii '9' when ctrl ->
4871 togglebirdseye ()
4873 | Ascii ('0'..'9')
4874 when not ctrl ->
4875 let ondone s =
4876 let n =
4877 try int_of_string s with exn ->
4878 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn;
4881 if n >= 0
4882 then (
4883 addnav ();
4884 cbput state.hists.pag (string_of_int n);
4885 gotopage1 (n + conf.pagebias - 1) 0;
4888 let pageentry text key =
4889 match Char.unsafe_chr key with
4890 | 'g' -> TEdone text
4891 | _ -> intentry text key
4893 let text = String.make 1 (Char.chr key) in
4894 enttext (":", text, Some (onhist state.hists.pag),
4895 pageentry, ondone, true)
4897 | Ascii 'b' ->
4898 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
4899 G.postRedisplay "toggle scrollbar";
4901 | Ascii 'B' ->
4902 state.bzoom <- not state.bzoom;
4903 state.rects <- [];
4904 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
4906 | Ascii 'l' ->
4907 conf.hlinks <- not conf.hlinks;
4908 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4909 G.postRedisplay "toggle highlightlinks";
4911 | Ascii 'F' ->
4912 if conf.angle mod 360 = 0
4913 then (
4914 state.glinks <- true;
4915 let mode = state.mode in
4916 state.mode <-
4917 Textentry (
4918 (":", E.s, None, linknentry, linknact gotounder, false),
4919 (fun _ ->
4920 state.glinks <- false;
4921 state.mode <- mode)
4923 state.text <- E.s;
4924 G.postRedisplay "view:linkent(F)"
4926 else impmsg "hint mode does not work under rotation"
4928 | Ascii 'y' ->
4929 state.glinks <- true;
4930 let mode = state.mode in
4931 state.mode <-
4932 Textentry (
4934 ":", E.s, None, linknentry, linknact (fun under ->
4935 selstring (undertext under);
4936 ), false
4938 fun _ ->
4939 state.glinks <- false;
4940 state.mode <- mode
4942 state.text <- E.s;
4943 G.postRedisplay "view:linkent"
4945 | Ascii 'a' ->
4946 begin match state.autoscroll with
4947 | Some step ->
4948 conf.autoscrollstep <- step;
4949 state.autoscroll <- None
4950 | None ->
4951 if conf.autoscrollstep = 0
4952 then state.autoscroll <- Some 1
4953 else state.autoscroll <- Some conf.autoscrollstep
4956 | Ascii 'p' when ctrl ->
4957 launchpath () (* XXX where do error messages go? *)
4959 | Ascii 'P' ->
4960 setpresentationmode (not conf.presentation);
4961 showtext ' ' ("presentation mode " ^
4962 if conf.presentation then "on" else "off");
4964 | Ascii 'f' ->
4965 if List.mem Wsi.Fullscreen state.winstate
4966 then Wsi.reshape conf.cwinw conf.cwinh
4967 else Wsi.fullscreen ()
4969 | Ascii ('p'|'N') ->
4970 search state.searchpattern false
4972 | Ascii 'n' | Fn 3 ->
4973 search state.searchpattern true
4975 | Ascii 't' ->
4976 begin match state.layout with
4977 | [] -> ()
4978 | l :: _ ->
4979 gotoghyll (getpagey l.pageno)
4982 | Ascii ' ' ->
4983 nextpage ()
4985 | Delete | KPdelete ->
4986 prevpage ()
4988 | Ascii '=' ->
4989 showtext ' ' (describe_location ());
4991 | Ascii 'w' ->
4992 begin match state.layout with
4993 | [] -> ()
4994 | l :: _ ->
4995 Wsi.reshape l.pagew l.pageh;
4996 G.postRedisplay "w"
4999 | Ascii '\'' ->
5000 enterbookmarkmode ()
5002 | Ascii 'h' | Fn 1 ->
5003 enterhelpmode ()
5005 | Ascii 'i' ->
5006 enterinfomode ()
5008 | Ascii 'e' when Buffer.length state.errmsgs > 0 ->
5009 entermsgsmode ()
5011 | Ascii 'm' ->
5012 let ondone s =
5013 match state.layout with
5014 | l :: _ ->
5015 if nonemptystr s
5016 then
5017 state.bookmarks <-
5018 (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
5019 | _ -> ()
5021 enttext ("bookmark: ", E.s, None, textentry, ondone, true)
5023 | Ascii '~' ->
5024 quickbookmark ();
5025 showtext ' ' "Quick bookmark added";
5027 | Ascii 'z' ->
5028 begin match state.layout with
5029 | l :: _ ->
5030 let rect = getpdimrect l.pagedimno in
5031 let w, h =
5032 if conf.crophack
5033 then
5034 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5035 truncate (1.2 *. (rect.(3) -. rect.(0))))
5036 else
5037 (truncate (rect.(1) -. rect.(0)),
5038 truncate (rect.(3) -. rect.(0)))
5040 let w = truncate ((float w)*.conf.zoom)
5041 and h = truncate ((float h)*.conf.zoom) in
5042 if w != 0 && h != 0
5043 then (
5044 state.anchor <- getanchor ();
5045 Wsi.reshape w (h + conf.interpagespace)
5047 G.postRedisplay "z";
5049 | [] -> ()
5052 | Ascii 'x' -> state.roam ()
5054 | Ascii ('<'|'>' as c) ->
5055 reqlayout
5056 (conf.angle + (if c = '>' then 30 else -30)) conf.fitmodel
5058 | Ascii ('['|']' as c) ->
5059 conf.colorscale <-
5060 bound (conf.colorscale +. (if c = '>' then 0.1 else -0.1)) 0.0 1.0;
5061 G.postRedisplay "brightness";
5063 | Ascii 'c' when state.mode = View ->
5064 if Wsi.withalt mask
5065 then (
5066 if conf.zoom > 1.0
5067 then
5068 let m = (state.winw - state.w) / 2 in
5069 gotoxy_and_clear_text m state.y
5071 else
5072 let (c, a, b), z =
5073 match state.prevcolumns with
5074 | None -> (1, 0, 0), 1.0
5075 | Some (columns, z) ->
5076 let cab =
5077 match columns with
5078 | Csplit (c, _) -> -c, 0, 0
5079 | Cmulti ((c, a, b), _) -> c, a, b
5080 | Csingle _ -> 1, 0, 0
5082 cab, z
5084 setcolumns View c a b;
5085 setzoom z
5087 | Down | Up when ctrl && Wsi.withshift mask ->
5088 let zoom, x = state.prevzoom in
5089 setzoom zoom;
5090 state.x <- x;
5092 | Ascii 'k' | Up | KPup ->
5093 begin match state.autoscroll with
5094 | None ->
5095 begin match state.mode with
5096 | Birdseye beye -> upbirdseye 1 beye
5097 | Textentry _
5098 | View
5099 | LinkNav _ ->
5100 if ctrl
5101 then gotoxy_and_clear_text state.x (clamp ~-(state.winh/2))
5102 else (
5103 if not (Wsi.withshift mask) && conf.presentation
5104 then prevpage ()
5105 else gotoghyll1 true (clamp (-conf.scrollstep))
5108 | Some n ->
5109 setautoscrollspeed n false
5112 | Ascii 'j' | Down | KPdown ->
5113 begin match state.autoscroll with
5114 | None ->
5115 begin match state.mode with
5116 | Birdseye beye -> downbirdseye 1 beye
5117 | Textentry _
5118 | View
5119 | LinkNav _ ->
5120 if ctrl
5121 then gotoxy_and_clear_text state.x (clamp (state.winh/2))
5122 else (
5123 if not (Wsi.withshift mask) && conf.presentation
5124 then nextpage ()
5125 else gotoghyll1 true (clamp (conf.scrollstep))
5128 | Some n ->
5129 setautoscrollspeed n true
5132 | Left | Right | KPleft | KPright when not (Wsi.withalt mask) ->
5133 if canpan ()
5134 then
5135 let dx =
5136 if ctrl
5137 then state.winw / 2
5138 else conf.hscrollstep
5140 let dx =
5141 let pv = Wsi.kc2pv key in
5142 if pv = Keys.Left || pv = Keys.KPleft then dx else -dx
5144 gotoxy_and_clear_text (panbound (state.x + dx)) state.y
5145 else (
5146 state.text <- E.s;
5147 G.postRedisplay "left/right"
5150 | Prior | KPprior ->
5151 let y =
5152 if ctrl
5153 then
5154 match state.layout with
5155 | [] -> state.y
5156 | l :: _ -> state.y - l.pagey
5157 else
5158 clamp (pgscale (-state.winh))
5160 gotoghyll y
5162 | Next | KPnext ->
5163 let y =
5164 if ctrl
5165 then
5166 match List.rev state.layout with
5167 | [] -> state.y
5168 | l :: _ -> getpagey l.pageno
5169 else
5170 clamp (pgscale state.winh)
5172 gotoghyll y
5174 | Ascii 'g' | Home | KPhome ->
5175 addnav ();
5176 gotoghyll 0
5177 | Ascii 'G' | End | KPend ->
5178 addnav ();
5179 gotoghyll (clamp state.maxy)
5181 | Right | KPright when Wsi.withalt mask ->
5182 gotoghyll (getnav 1)
5183 | Left | KPleft when Wsi.withalt mask ->
5184 gotoghyll (getnav ~-1)
5186 | Ascii 'r' ->
5187 reload ()
5189 | Ascii 'v' when conf.debug ->
5190 state.rects <- [];
5191 List.iter (fun l ->
5192 match getopaque l.pageno with
5193 | None -> ()
5194 | Some opaque ->
5195 let x0, y0, x1, y1 = pagebbox opaque in
5196 let rect = (float x0, float y0,
5197 float x1, float y0,
5198 float x1, float y1,
5199 float x0, float y1) in
5200 debugrect rect;
5201 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
5202 state.rects <- (l.pageno, color, rect) :: state.rects;
5203 ) state.layout;
5204 G.postRedisplay "v";
5206 | Ascii '|' ->
5207 let mode = state.mode in
5208 let cmd = ref E.s in
5209 let onleave = function
5210 | Cancel -> state.mode <- mode
5211 | Confirm ->
5212 List.iter (fun l ->
5213 match getopaque l.pageno with
5214 | Some opaque -> pipesel opaque !cmd
5215 | None -> ()) state.layout;
5216 state.mode <- mode
5218 let ondone s =
5219 cbput state.hists.sel s;
5220 cmd := s
5222 let te =
5223 "| ", !cmd, Some (onhist state.hists.sel), textentry, ondone, true
5225 G.postRedisplay "|";
5226 state.mode <- Textentry (te, onleave);
5228 | (Ascii _|Fn _|Enter|KPenter|KPleft|KPright|Left|Right|Code _) ->
5229 vlog "huh? %s" (Wsi.keyname key)
5232 let linknavkeyboard key mask linknav =
5233 let pv = Wsi.kc2pv key in
5234 let getpage pageno =
5235 let rec loop = function
5236 | [] -> None
5237 | l :: _ when l.pageno = pageno -> Some l
5238 | _ :: rest -> loop rest
5239 in loop state.layout
5241 let doexact (pageno, n) =
5242 match getopaque pageno, getpage pageno with
5243 | Some opaque, Some l ->
5244 if pv = Keys.Enter || pv = Keys.KPenter
5245 then
5246 let under = getlink opaque n in
5247 G.postRedisplay "link gotounder";
5248 gotounder under;
5249 state.mode <- View;
5250 else
5251 let opt, dir =
5252 let open Keys in
5253 match pv with
5254 | Home ->
5255 Some (findlink opaque LDfirst), -1
5257 | End ->
5258 Some (findlink opaque LDlast), 1
5260 | Left ->
5261 Some (findlink opaque (LDleft n)), -1
5263 | Right ->
5264 Some (findlink opaque (LDright n)), 1
5266 | Up ->
5267 Some (findlink opaque (LDup n)), -1
5269 | Down ->
5270 Some (findlink opaque (LDdown n)), 1
5272 | (Delete|Escape|Insert|Enter|KPdelete|KPdown|KPend|KPenter
5273 |KPhome|KPleft|KPminus|KPnext|KPplus|KPprior|KPright|KPup
5274 |Next|Prior|Ascii _|Code _|Fn _|Backspace) -> None, 0
5276 let pwl l dir =
5277 begin match findpwl l.pageno dir with
5278 | Pwlnotfound -> ()
5279 | Pwl pageno ->
5280 let notfound dir =
5281 state.mode <- LinkNav (Ltgendir dir);
5282 let y, h = getpageyh pageno in
5283 let y =
5284 if dir < 0
5285 then y + h - state.winh
5286 else y
5288 gotoxy state.x y
5290 begin match getopaque pageno, getpage pageno with
5291 | Some opaque, Some _ ->
5292 let link =
5293 let ld = if dir > 0 then LDfirst else LDlast in
5294 findlink opaque ld
5296 begin match link with
5297 | Lfound m ->
5298 showlinktype (getlink opaque m);
5299 state.mode <- LinkNav (Ltexact (pageno, m));
5300 G.postRedisplay "linknav jpage";
5301 | Lnotfound -> notfound dir
5302 end;
5303 | _ -> notfound dir
5304 end;
5305 end;
5307 begin match opt with
5308 | Some Lnotfound -> pwl l dir;
5309 | Some (Lfound m) ->
5310 if m = n
5311 then pwl l dir
5312 else (
5313 let _, y0, _, y1 = getlinkrect opaque m in
5314 if y0 < l.pagey
5315 then gotopage1 l.pageno y0
5316 else (
5317 let d = fstate.fontsize + 1 in
5318 if y1 - l.pagey > l.pagevh - d
5319 then gotopage1 l.pageno (y1 - state.winh + d)
5320 else G.postRedisplay "linknav";
5322 showlinktype (getlink opaque m);
5323 state.mode <- LinkNav (Ltexact (l.pageno, m));
5326 | None -> viewkeyboard key mask
5327 end;
5328 | _ -> viewkeyboard key mask
5330 if pv = Keys.Insert
5331 then (
5332 begin match linknav with
5333 | Ltexact pa -> state.lnava <- Some pa
5334 | Ltgendir _ | Ltnotready _ -> ()
5335 end;
5336 state.mode <- View;
5337 G.postRedisplay "leave linknav"
5339 else
5340 match linknav with
5341 | Ltgendir _ | Ltnotready _ -> viewkeyboard key mask
5342 | Ltexact exact -> doexact exact
5345 let keyboard key mask =
5346 if (key = Char.code 'g' && Wsi.withctrl mask) && not (istextentry state.mode)
5347 then wcmd "interrupt"
5348 else state.uioh <- state.uioh#key key mask
5351 let birdseyekeyboard key mask
5352 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5353 let incr =
5354 match conf.columns with
5355 | Csingle _ -> 1
5356 | Cmulti ((c, _, _), _) -> c
5357 | Csplit _ -> failwith "bird's eye split mode"
5359 let pgh layout = List.fold_left
5360 (fun m l -> max l.pageh m) state.winh layout in
5361 let open Keys in
5362 match Wsi.kc2pv key with
5363 | Ascii 'l' when Wsi.withctrl mask ->
5364 let y, h = getpageyh pageno in
5365 let top = (state.winh - h) / 2 in
5366 gotoxy state.x (max 0 (y - top))
5367 | Enter | KPenter -> leavebirdseye beye false
5368 | Escape -> leavebirdseye beye true
5369 | Up -> upbirdseye incr beye
5370 | Down -> downbirdseye incr beye
5371 | Left -> upbirdseye 1 beye
5372 | Right -> downbirdseye 1 beye
5374 | Prior ->
5375 begin match state.layout with
5376 | l :: _ ->
5377 if l.pagey != 0
5378 then (
5379 state.mode <- Birdseye (
5380 oconf, leftx, l.pageno, hooverpageno, anchor
5382 gotopage1 l.pageno 0;
5384 else (
5385 let layout = layout state.x (state.y-state.winh)
5386 state.winw
5387 (pgh state.layout) in
5388 match layout with
5389 | [] -> gotoxy state.x (clamp (-state.winh))
5390 | l :: _ ->
5391 state.mode <- Birdseye (
5392 oconf, leftx, l.pageno, hooverpageno, anchor
5394 gotopage1 l.pageno 0
5397 | [] -> gotoxy state.x (clamp (-state.winh))
5398 end;
5400 | Next ->
5401 begin match List.rev state.layout with
5402 | l :: _ ->
5403 let layout = layout state.x
5404 (state.y + (pgh state.layout))
5405 state.winw state.winh in
5406 begin match layout with
5407 | [] ->
5408 let incr = l.pageh - l.pagevh in
5409 if incr = 0
5410 then (
5411 state.mode <-
5412 Birdseye (
5413 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5415 G.postRedisplay "birdseye pagedown";
5417 else gotoxy state.x (clamp (incr + conf.interpagespace*2));
5419 | l :: _ ->
5420 state.mode <-
5421 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5422 gotopage1 l.pageno 0;
5425 | [] -> gotoxy state.x (clamp state.winh)
5426 end;
5428 | Home ->
5429 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5430 gotopage1 0 0
5432 | End ->
5433 let pageno = state.pagecount - 1 in
5434 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5435 if not (pagevisible state.layout pageno)
5436 then
5437 let h =
5438 match List.rev state.pdims with
5439 | [] -> state.winh
5440 | (_, _, h, _) :: _ -> h
5442 gotoxy
5443 state.x
5444 (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5445 else G.postRedisplay "birdseye end";
5447 | (Delete|Insert|KPdelete|KPdown|KPend|KPhome|KPleft
5448 |KPminus|KPnext|KPplus|KPprior|KPright|KPup|Ascii _
5449 |Code _|Fn _|Backspace) -> viewkeyboard key mask
5452 let drawpage l =
5453 let color =
5454 match state.mode with
5455 | Textentry _ -> scalecolor 0.4
5456 | LinkNav _
5457 | View -> scalecolor 1.0
5458 | Birdseye (_, _, pageno, hooverpageno, _) ->
5459 if l.pageno = hooverpageno
5460 then scalecolor 0.9
5461 else (
5462 if l.pageno = pageno
5463 then (
5464 let c = scalecolor 1.0 in
5465 GlDraw.color c;
5466 GlDraw.line_width 3.0;
5467 let dispx = l.pagedispx in
5468 linerect
5469 (float (dispx-1)) (float (l.pagedispy-1))
5470 (float (dispx+l.pagevw+1))
5471 (float (l.pagedispy+l.pagevh+1))
5473 GlDraw.line_width 1.0;
5476 else scalecolor 0.8
5479 drawtiles l color;
5482 let postdrawpage l linkindexbase =
5483 match getopaque l.pageno with
5484 | Some opaque ->
5485 if tileready l l.pagex l.pagey
5486 then
5487 let x = l.pagedispx - l.pagex
5488 and y = l.pagedispy - l.pagey in
5489 let hlmask =
5490 match conf.columns with
5491 | Csingle _ | Cmulti _ ->
5492 (if conf.hlinks then 1 else 0)
5493 + (if state.glinks
5494 && not (isbirdseye state.mode) then 2 else 0)
5495 | Csplit _ -> 0
5497 let s =
5498 match state.mode with
5499 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5500 | Textentry _
5501 | Birdseye _
5502 | View
5503 | LinkNav _ -> E.s
5505 Hashtbl.find_all state.prects l.pageno |>
5506 List.iter (fun vals -> drawprect opaque x y vals);
5507 let n = postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize) in
5508 if n < 0
5509 then (state.redisplay <- true; 0)
5510 else n
5511 else 0
5512 | _ -> 0
5515 let scrollindicator () =
5516 let sbw, ph, sh = state.uioh#scrollph in
5517 let sbh, pw, sw = state.uioh#scrollpw in
5519 let x0,x1,hx0 =
5520 if conf.leftscroll
5521 then (0, sbw, sbw)
5522 else ((state.winw - sbw), state.winw, 0)
5525 Gl.enable `blend;
5526 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5527 GlDraw.color (0.64, 0.64, 0.64) ~alpha:0.7;
5528 filledrect (float x0) 0. (float x1) (float state.winh);
5529 filledrect
5530 (float hx0) (float (state.winh - sbh))
5531 (float (hx0 + state.winw)) (float state.winh)
5533 GlDraw.color (0.0, 0.0, 0.0) ~alpha:0.7;
5535 filledrect (float x0) ph (float x1) (ph +. sh);
5536 let pw = pw +. float hx0 in
5537 filledrect pw (float (state.winh - sbh)) (pw +. sw) (float state.winh);
5538 Gl.disable `blend;
5541 let showsel () =
5542 match state.mstate with
5543 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5546 | Msel ((x0, y0), (x1, y1)) ->
5547 let identify opaque l px py = Some (opaque, l.pageno, px, py) in
5548 let o0,n0,px0,py0 = onppundermouse identify x0 y0 (~< E.s, -1, 0, 0) in
5549 let _o1,n1,px1,py1 = onppundermouse identify x1 y1 (~< E.s, -1, 0, 0) in
5550 if n0 != -1 && n0 = n1 then seltext o0 (px0, py0, px1, py1);
5553 let showrects =
5554 function [] -> ()
5555 | rects ->
5556 Gl.enable `blend;
5557 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5558 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5559 List.iter
5560 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5561 List.iter (fun l ->
5562 if l.pageno = pageno
5563 then (
5564 let dx = float (l.pagedispx - l.pagex) in
5565 let dy = float (l.pagedispy - l.pagey) in
5566 let r, g, b, alpha = c in
5567 GlDraw.color (r, g, b) ~alpha;
5568 filledrect2 (x0+.dx) (y0+.dy)
5569 (x1+.dx) (y1+.dy)
5570 (x3+.dx) (y3+.dy)
5571 (x2+.dx) (y2+.dy);
5573 ) state.layout
5574 ) rects
5576 Gl.disable `blend;
5579 let display () =
5580 begin match conf.columns, state.layout with
5581 | Csingle _, _ :: _ ->
5582 GlDraw.color (scalecolor2 conf.bgcolor);
5583 let y =
5584 List.fold_left (fun y l ->
5585 let x0 = 0 in
5586 let y0 = y in
5587 let x1 = l.pagedispx in
5588 let y1 = (l.pagedispy + l.pagevh) in
5589 filledrect (float x0) (float y0) (float x1) (float y1);
5590 let x0 = x1 + l.pagevw in
5591 let x1 = state.winw in
5592 filledrect1 (float x0) (float y0) (float x1) (float y1);
5593 if y != l.pagedispy
5594 then (
5595 let x0 = 0
5596 and x1 = state.winw in
5597 let y0 = y
5598 and y1 = l.pagedispy in
5599 filledrect1 (float x0) (float y0) (float x1) (float y1);
5601 l.pagedispy + l.pagevh) 0 state.layout
5603 let x0 = 0
5604 and x1 = state.winw in
5605 let y0 = y
5606 and y1 = state.winh in
5607 filledrect1 (float x0) (float y0) (float x1) (float y1)
5608 | (Cmulti _ | Csplit _), _ | Csingle _, [] ->
5609 GlClear.color (scalecolor2 conf.bgcolor);
5610 GlClear.clear [`color];
5611 end;
5612 List.iter drawpage state.layout;
5613 let rects =
5614 match state.mode with
5615 | LinkNav (Ltexact (pageno, linkno)) ->
5616 begin match getopaque pageno with
5617 | Some opaque ->
5618 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5619 let color = (0.0, 0.0, 0.5, 0.5) in
5620 (pageno, color, (
5621 float x0, float y0,
5622 float x1, float y0,
5623 float x1, float y1,
5624 float x0, float y1)
5625 ) :: state.rects
5626 | None -> state.rects
5628 | LinkNav (Ltgendir _) | LinkNav (Ltnotready _)
5629 | Birdseye _
5630 | Textentry _
5631 | View -> state.rects
5633 showrects rects;
5634 let rec postloop linkindexbase = function
5635 | l :: rest ->
5636 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5637 postloop linkindexbase rest
5638 | [] -> ()
5640 showsel ();
5641 postloop 0 state.layout;
5642 state.uioh#display;
5643 begin match state.mstate with
5644 | Mzoomrect ((x0, y0), (x1, y1)) ->
5645 Gl.enable `blend;
5646 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5647 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5648 filledrect (float x0) (float y0) (float x1) (float y1);
5649 Gl.disable `blend;
5650 | Msel _
5651 | Mpan _
5652 | Mscrolly | Mscrollx
5653 | Mzoom _
5654 | Mnone -> ()
5655 end;
5656 enttext ();
5657 scrollindicator ();
5658 Wsi.swapb ();
5661 let zoomrect x y x1 y1 =
5662 let x0 = min x x1
5663 and x1 = max x x1
5664 and y0 = min y y1 in
5665 let zoom = (float state.w) /. float (x1 - x0) in
5666 let margin =
5667 let simple () =
5668 if state.w < state.winw
5669 then (state.winw - state.w) / 2
5670 else 0
5672 match conf.fitmodel with
5673 | FitWidth | FitProportional -> simple ()
5674 | FitPage ->
5675 match conf.columns with
5676 | Csplit _ ->
5677 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
5678 | Cmulti _ | Csingle _ -> simple ()
5680 gotoxy ((state.x + margin) - x0) (state.y + y0);
5681 state.anchor <- getanchor ();
5682 setzoom zoom;
5683 resetmstate ();
5686 let annot inline x y =
5687 match unproject x y with
5688 | Some (opaque, n, ux, uy) ->
5689 let add text =
5690 addannot opaque ux uy text;
5691 wcmd "freepage %s" (~> opaque);
5692 Hashtbl.remove state.pagemap (n, state.gen);
5693 flushtiles ();
5694 gotoxy state.x state.y
5696 if inline
5697 then
5698 let ondone s = add s in
5699 let mode = state.mode in
5700 state.mode <- Textentry (
5701 ("annotation: ", E.s, None, textentry, ondone, true),
5702 fun _ -> state.mode <- mode);
5703 state.text <- E.s;
5704 enttext ();
5705 G.postRedisplay "annot"
5706 else
5707 add @@ getusertext E.s
5708 | _ -> ()
5711 let zoomblock x y =
5712 let g opaque l px py =
5713 match rectofblock opaque px py with
5714 | Some a ->
5715 let x0 = a.(0) -. 20. in
5716 let x1 = a.(1) +. 20. in
5717 let y0 = a.(2) -. 20. in
5718 let zoom = (float state.w) /. (x1 -. x0) in
5719 let pagey = getpagey l.pageno in
5720 let margin = (state.w - l.pagew)/2 in
5721 let nx = -truncate x0 - margin in
5722 gotoxy_and_clear_text nx (pagey + truncate y0);
5723 state.anchor <- getanchor ();
5724 setzoom zoom;
5725 None
5726 | None -> None
5728 match conf.columns with
5729 | Csplit _ ->
5730 impmsg "block zooming does not work properly in split columns mode"
5731 | Cmulti _ | Csingle _ -> onppundermouse g x y ()
5734 let scrollx x =
5735 let winw = state.winw - 1 in
5736 let s = float x /. float winw in
5737 let destx = truncate (float (state.w + winw) *. s) in
5738 gotoxy_and_clear_text (winw - destx) state.y;
5739 state.mstate <- Mscrollx;
5742 let scrolly y =
5743 let s = float y /. float state.winh in
5744 let desty = truncate (float (state.maxy -
5745 (if conf.maxhfit then state.winh else 0))
5746 *. s) in
5747 gotoxy_and_clear_text state.x desty;
5748 state.mstate <- Mscrolly;
5751 let viewmulticlick clicks x y mask =
5752 let g opaque l px py =
5753 let mark =
5754 match clicks with
5755 | 2 -> Mark_word
5756 | 3 -> Mark_line
5757 | 4 -> Mark_block
5758 | _ -> Mark_page
5760 if markunder opaque px py mark
5761 then (
5762 Some (fun () ->
5763 let dopipe cmd =
5764 match getopaque l.pageno with
5765 | None -> ()
5766 | Some opaque -> pipesel opaque cmd
5768 state.roam <- (fun () -> dopipe conf.paxcmd);
5769 if not (Wsi.withctrl mask) then dopipe conf.selcmd;
5772 else None
5774 G.postRedisplay "viewmulticlick";
5775 onppundermouse g x y (fun () -> impmsg "nothing to select") ();
5778 let canselect () =
5779 match conf.columns with
5780 | Csplit _ -> false
5781 | Csingle _ | Cmulti _ -> conf.angle mod 360 = 0
5784 let viewmouse button down x y mask =
5785 match button with
5786 | n when (n == 4 || n == 5) && not down ->
5787 if Wsi.withctrl mask
5788 then (
5789 match state.mstate with
5790 | Mzoom (oldn, i, (ftx, fty)) ->
5791 let recenter =
5792 if false
5793 then abs (ftx - x) > 5 || abs (fty - y) > 5
5794 else false
5796 if oldn = n
5797 then (
5798 if i = 2
5799 then
5800 let incr =
5801 match n with
5802 | 5 ->
5803 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5804 | _ ->
5805 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5807 let zoom = conf.zoom -. incr in
5808 if recenter
5809 then pivotzoom ~x ~y zoom
5810 else pivotzoom zoom;
5811 state.mstate <- Mzoom (n, 0, (x, y));
5812 else
5813 state.mstate <- Mzoom (n, i+1, (ftx, fty));
5815 else state.mstate <- Mzoom (n, 0, (ftx, fty))
5817 | Msel _
5818 | Mpan _
5819 | Mscrolly | Mscrollx
5820 | Mzoomrect _
5821 | Mnone -> state.mstate <- Mzoom (n, 0, (0, 0))
5823 else (
5824 match state.autoscroll with
5825 | Some step -> setautoscrollspeed step (n=4)
5826 | None ->
5827 if conf.wheelbypage || conf.presentation
5828 then (
5829 if n = 4
5830 then prevpage ()
5831 else nextpage ()
5833 else
5834 let incr =
5835 if n = 4
5836 then -conf.scrollstep
5837 else conf.scrollstep
5839 let incr = incr * 2 in
5840 let y = clamp incr in
5841 gotoxy_and_clear_text state.x y
5844 | n when (n = 6 || n = 7) && not down && canpan () ->
5845 let x =
5846 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep) in
5847 gotoxy_and_clear_text x state.y
5849 | 1 when Wsi.withshift mask ->
5850 state.mstate <- Mnone;
5851 if not down
5852 then (
5853 match unproject x y with
5854 | None -> ()
5855 | Some (_, pageno, ux, uy) ->
5856 let cmd = Printf.sprintf
5857 "%s %s %d %d %d"
5858 conf.stcmd state.path pageno ux uy
5860 match spawn cmd [] with
5861 | (exception exn) ->
5862 impmsg "execution of synctex command(%S) failed: %S"
5863 conf.stcmd @@ exntos exn
5864 | _pid -> ()
5867 | 1 when Wsi.withctrl mask ->
5868 if down
5869 then (
5870 Wsi.setcursor Wsi.CURSOR_FLEUR;
5871 state.mstate <- Mpan (x, y)
5873 else
5874 state.mstate <- Mnone
5876 | 3 ->
5877 if down
5878 then (
5879 if Wsi.withshift mask
5880 then (
5881 annot conf.annotinline x y;
5882 G.postRedisplay "addannot"
5884 else
5885 let p = (x, y) in
5886 Wsi.setcursor Wsi.CURSOR_CYCLE;
5887 state.mstate <- Mzoomrect (p, p)
5889 else (
5890 match state.mstate with
5891 | Mzoomrect ((x0, y0), _) ->
5892 if abs (x-x0) > 10 && abs (y - y0) > 10
5893 then zoomrect x0 y0 x y
5894 else (
5895 resetmstate ();
5896 G.postRedisplay "kill accidental zoom rect";
5898 | Msel _
5899 | Mpan _
5900 | Mscrolly | Mscrollx
5901 | Mzoom _
5902 | Mnone ->
5903 resetmstate ()
5906 | 1 when vscrollhit x ->
5907 if down
5908 then
5909 let _, position, sh = state.uioh#scrollph in
5910 if y > truncate position && y < truncate (position +. sh)
5911 then state.mstate <- Mscrolly
5912 else scrolly y
5913 else
5914 state.mstate <- Mnone
5916 | 1 when y > state.winh - hscrollh () ->
5917 if down
5918 then
5919 let _, position, sw = state.uioh#scrollpw in
5920 if x > truncate position && x < truncate (position +. sw)
5921 then state.mstate <- Mscrollx
5922 else scrollx x
5923 else
5924 state.mstate <- Mnone
5926 | 1 when state.bzoom -> if not down then zoomblock x y
5928 | 1 ->
5929 let dest = if down then getunder x y else Unone in
5930 begin match dest with
5931 | Ulinkuri _ ->
5932 gotounder dest
5934 | Unone when down ->
5935 Wsi.setcursor Wsi.CURSOR_FLEUR;
5936 state.mstate <- Mpan (x, y);
5938 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
5940 | Unone | Utext _ ->
5941 if down
5942 then (
5943 if canselect ()
5944 then (
5945 state.mstate <- Msel ((x, y), (x, y));
5946 G.postRedisplay "mouse select";
5949 else (
5950 match state.mstate with
5951 | Mnone -> ()
5953 | Mzoom _ | Mscrollx | Mscrolly ->
5954 state.mstate <- Mnone
5956 | Mzoomrect ((x0, y0), _) ->
5957 zoomrect x0 y0 x y
5959 | Mpan _ ->
5960 Wsi.setcursor Wsi.CURSOR_INHERIT;
5961 state.mstate <- Mnone
5963 | Msel ((x0, y0), (x1, y1)) ->
5964 let rec loop = function
5965 | [] -> ()
5966 | l :: rest ->
5967 let inside =
5968 let a0 = l.pagedispy in
5969 let a1 = a0 + l.pagevh in
5970 let b0 = l.pagedispx in
5971 let b1 = b0 + l.pagevw in
5972 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5973 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5975 if inside
5976 then
5977 match getopaque l.pageno with
5978 | Some opaque ->
5979 let dosel cmd () =
5980 match Unix.pipe () with
5981 | (exception exn) ->
5982 impmsg "cannot create sel pipe: %s" @@
5983 exntos exn;
5984 | (r, w) ->
5985 let clo what fd =
5986 Ne.clo fd (fun msg ->
5987 dolog "%s close failed: %s" what msg)
5989 let pid =
5990 try spawn cmd [r, 0; w, -1]
5991 with exn ->
5992 dolog "cannot execute %S: %s"
5993 cmd @@ exntos exn;
5996 if pid > 0
5997 then (
5998 copysel w opaque;
5999 G.postRedisplay "copysel";
6001 else clo "Msel pipe/w" w;
6002 clo "Msel pipe/r" r;
6004 dosel conf.selcmd ();
6005 state.roam <- dosel conf.paxcmd;
6006 | None -> ()
6007 else loop rest
6009 loop state.layout;
6010 resetmstate ();
6014 | _ -> ()
6017 let birdseyemouse button down x y mask
6018 (conf, leftx, _, hooverpageno, anchor) =
6019 match button with
6020 | 1 when down ->
6021 let rec loop = function
6022 | [] -> ()
6023 | l :: rest ->
6024 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6025 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6026 then (
6027 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
6029 else loop rest
6031 loop state.layout
6032 | 3 -> ()
6033 | _ -> viewmouse button down x y mask
6036 let uioh = object
6037 method display = ()
6039 method key key mask =
6040 begin match state.mode with
6041 | Textentry textentry -> textentrykeyboard key mask textentry
6042 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6043 | View -> viewkeyboard key mask
6044 | LinkNav linknav -> linknavkeyboard key mask linknav
6045 end;
6046 state.uioh
6048 method button button bstate x y mask =
6049 begin match state.mode with
6050 | LinkNav _
6051 | View -> viewmouse button bstate x y mask
6052 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6053 | Textentry _ -> ()
6054 end;
6055 state.uioh
6057 method multiclick clicks x y mask =
6058 begin match state.mode with
6059 | LinkNav _
6060 | View -> viewmulticlick clicks x y mask
6061 | Birdseye _
6062 | Textentry _ -> ()
6063 end;
6064 state.uioh
6066 method motion x y =
6067 begin match state.mode with
6068 | Textentry _ -> ()
6069 | View | Birdseye _ | LinkNav _ ->
6070 match state.mstate with
6071 | Mzoom _ | Mnone -> ()
6073 | Mpan (x0, y0) ->
6074 let dx = x - x0
6075 and dy = y0 - y in
6076 state.mstate <- Mpan (x, y);
6077 let x = if canpan () then panbound (state.x + dx) else state.x in
6078 let y = clamp dy in
6079 gotoxy_and_clear_text x y
6081 | Msel (a, _) ->
6082 state.mstate <- Msel (a, (x, y));
6083 G.postRedisplay "motion select";
6085 | Mscrolly ->
6086 let y = min state.winh (max 0 y) in
6087 scrolly y
6089 | Mscrollx ->
6090 let x = min state.winw (max 0 x) in
6091 scrollx x
6093 | Mzoomrect (p0, _) ->
6094 state.mstate <- Mzoomrect (p0, (x, y));
6095 G.postRedisplay "motion zoomrect";
6096 end;
6097 state.uioh
6099 method pmotion x y =
6100 begin match state.mode with
6101 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6102 let rec loop = function
6103 | [] ->
6104 if hooverpageno != -1
6105 then (
6106 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6107 G.postRedisplay "pmotion birdseye no hoover";
6109 | l :: rest ->
6110 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6111 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6112 then (
6113 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6114 G.postRedisplay "pmotion birdseye hoover";
6116 else loop rest
6118 loop state.layout
6120 | Textentry _ -> ()
6122 | LinkNav _
6123 | View ->
6124 match state.mstate with
6125 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ -> ()
6126 | Mnone ->
6127 updateunder x y;
6128 if canselect ()
6129 then
6130 match conf.pax with
6131 | None -> ()
6132 | Some r ->
6133 let past, _, _ = !r in
6134 let now = now () in
6135 let delta = now -. past in
6136 if delta > 0.01
6137 then paxunder x y
6138 else r := (now, x, y)
6139 end;
6140 state.uioh
6142 method infochanged _ = ()
6144 method scrollph =
6145 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6146 let p, h =
6147 if maxy = 0
6148 then 0.0, float state.winh
6149 else scrollph state.y maxy
6151 vscrollw (), p, h
6153 method scrollpw =
6154 let fwinw = float (state.winw - vscrollw ()) in
6155 let sw =
6156 let sw = fwinw /. float state.w in
6157 let sw = fwinw *. sw in
6158 max sw (float conf.scrollh)
6160 let position =
6161 let maxx = state.w + state.winw in
6162 let x = state.winw - state.x in
6163 let percent = float x /. float maxx in
6164 (fwinw -. sw) *. percent
6166 hscrollh (), position, sw
6168 method modehash =
6169 let modename =
6170 match state.mode with
6171 | LinkNav _ -> "links"
6172 | Textentry _ -> "textentry"
6173 | Birdseye _ -> "birdseye"
6174 | View -> "view"
6176 findkeyhash conf modename
6178 method eformsgs = true
6179 method alwaysscrolly = false
6180 end;;
6182 let addrect pageno r g b a x0 y0 x1 y1 =
6183 Hashtbl.add state.prects pageno [|r; g; b; a; x0; y0; x1; y1|];
6186 let ract cmds =
6187 let cl = splitatchar cmds ' ' in
6188 let scan s fmt f =
6189 try Scanf.sscanf s fmt f
6190 with exn ->
6191 adderrfmt "remote exec"
6192 "error processing '%S': %s\n" cmds @@ exntos exn
6194 let rectx s pageno (r, g, b, a) x0 y0 x1 y1 =
6195 vlog "%s page %d color (%f %f %f %f) x0,y0,x1,y1 = %f %f %f %f"
6196 s pageno r g b a x0 y0 x1 y1;
6197 onpagerect
6198 pageno
6199 (fun w h ->
6200 let _,w1,h1,_ = getpagedim pageno in
6201 let sw = float w1 /. float w
6202 and sh = float h1 /. float h in
6203 let x0s = x0 *. sw
6204 and x1s = x1 *. sw
6205 and y0s = y0 *. sh
6206 and y1s = y1 *. sh in
6207 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
6208 let color = (r, g, b, a) in
6209 if conf.verbose then debugrect rect;
6210 state.rects <- (pageno, color, rect) :: state.rects;
6211 G.postRedisplay s;
6214 match cl with
6215 | "reload", "" -> reload ()
6216 | "goto", args ->
6217 scan args "%u %f %f"
6218 (fun pageno x y ->
6219 let cmd, _ = state.geomcmds in
6220 if emptystr cmd
6221 then gotopagexy !wtmode pageno x y
6222 else
6223 let f prevf () =
6224 gotopagexy !wtmode pageno x y;
6225 prevf ()
6227 state.reprf <- f state.reprf
6229 | "goto1", args -> scan args "%u %f" gotopage
6230 | "gotor", args ->
6231 scan args "%S %u"
6232 (fun _filename _pageno ->
6233 failwith "gotounder (Uremote (filename, pageno))")
6234 | "gotord", args ->
6235 scan args "%S %S"
6236 (fun _filename _dest ->
6237 failwith "gotounder (Uremotedest (filename, dest))")
6238 | "rect", args ->
6239 scan args "%u %u %f %f %f %f"
6240 (fun pageno c x0 y0 x1 y1 ->
6241 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
6242 rectx "rect" pageno color x0 y0 x1 y1;
6244 | "prect", args ->
6245 scan args "%u %f %f %f %f %f %f %f %f"
6246 (fun pageno r g b alpha x0 y0 x1 y1 ->
6247 addrect pageno r g b alpha x0 y0 x1 y1;
6248 G.postRedisplay "prect"
6250 | "pgoto", args ->
6251 scan args "%u %f %f"
6252 (fun pageno x y ->
6253 let optopaque =
6254 match getopaque pageno with
6255 | Some opaque -> opaque
6256 | None -> ~< E.s
6258 pgoto optopaque pageno x y;
6259 let rec fixx = function
6260 | [] -> ()
6261 | l :: rest ->
6262 if l.pageno = pageno
6263 then gotoxy (state.x - l.pagedispx) state.y
6264 else fixx rest
6266 let layout =
6267 let mult =
6268 match conf.columns with
6269 | Csingle _ | Csplit _ -> 1
6270 | Cmulti ((n, _, _), _) -> n
6272 layout 0 state.y (state.winw * mult) state.winh
6274 fixx layout
6276 | "activatewin", "" -> Wsi.activatewin ()
6277 | "quit", "" -> raise Quit
6278 | "keys", keys ->
6279 begin try
6280 let l = Config.keys_of_string keys in
6281 List.iter (fun (k, m) -> keyboard k m) l
6282 with exn ->
6283 adderrfmt "error processing keys" "`%S': %s\n" cmds @@ exntos exn
6285 | "clearrects", "" ->
6286 Hashtbl.clear state.prects;
6287 G.postRedisplay "clearrects"
6288 | _ ->
6289 adderrfmt "remote command"
6290 "error processing remote command: %S\n" cmds;
6293 let remote =
6294 let scratch = Bytes.create 80 in
6295 let buf = Buffer.create 80 in
6296 fun fd ->
6297 match tempfailureretry (Unix.read fd scratch 0) 80 with
6298 | (exception Unix.Unix_error (Unix.EAGAIN, _, _)) -> None
6299 | 0 ->
6300 Unix.close fd;
6301 if Buffer.length buf > 0
6302 then (
6303 let s = Buffer.contents buf in
6304 Buffer.clear buf;
6305 ract s;
6307 None
6308 | n ->
6309 let rec eat ppos =
6310 let nlpos =
6311 match Bytes.index_from scratch ppos '\n' with
6312 | pos -> if pos >= n then -1 else pos
6313 | (exception Not_found) -> -1
6315 if nlpos >= 0
6316 then (
6317 Buffer.add_subbytes buf scratch ppos (nlpos-ppos);
6318 let s = Buffer.contents buf in
6319 Buffer.clear buf;
6320 ract s;
6321 eat (nlpos+1);
6323 else (
6324 Buffer.add_subbytes buf scratch ppos (n-ppos);
6325 Some fd
6327 in eat 0
6330 let remoteopen path =
6331 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
6332 with exn ->
6333 adderrfmt "remoteopen" "error opening %S: %s" path @@ exntos exn;
6334 None
6337 let () =
6338 let gcconfig = ref E.s in
6339 let trimcachepath = ref E.s in
6340 let rcmdpath = ref E.s in
6341 let pageno = ref None in
6342 let rootwid = ref 0 in
6343 let openlast = ref false in
6344 let nofc = ref false in
6345 let doreap = ref false in
6346 let csspath = ref None in
6347 selfexec := Sys.executable_name;
6348 Arg.parse
6349 (Arg.align
6350 [("-p", Arg.String (fun s -> state.password <- s),
6351 "<password> Set password");
6353 ("-f", Arg.String
6354 (fun s ->
6355 Config.fontpath := s;
6356 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
6358 "<path> Set path to the user interface font");
6360 ("-c", Arg.String
6361 (fun s ->
6362 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
6363 Config.confpath := s),
6364 "<path> Set path to the configuration file");
6366 ("-last", Arg.Set openlast, " Open last document");
6368 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
6369 "<page-number> Jump to page");
6371 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6372 "<path> Set path to the trim cache file");
6374 ("-dest", Arg.String (fun s -> state.nameddest <- s),
6375 "<named-destination> Set named destination");
6377 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
6378 ("-cxack", Arg.Set cxack, " Cut corners");
6380 ("-remote", Arg.String (fun s -> rcmdpath := s),
6381 "<path> Set path to the source of remote commands");
6383 ("-gc", Arg.Set_string gcconfig,
6384 "<path> Collect garbage with the help of a script");
6386 ("-nofc", Arg.Set nofc, " Do not use fontconfig");
6388 ("-v", Arg.Unit (fun () ->
6389 Printf.printf
6390 "%s\nconfiguration path: %s\n"
6391 (version ())
6392 Config.defconfpath;
6393 exit 0), " Print version and exit");
6395 ("-css", Arg.String (fun s -> csspath := Some s),
6396 "<path> Set path to the style sheet to use with EPUB/HTML");
6398 ("-embed", Arg.Set_int rootwid, "<window-id> Embed into window");
6400 ("-origin", Arg.String (fun s -> state.origin <- s),
6401 "<origin> <undocumented>");
6404 (fun s -> state.path <- s)
6405 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:");
6407 if !wtmode
6408 then selfexec := !selfexec ^ " -wtmode";
6410 let histmode = emptystr state.path && not !openlast in
6412 if not (Config.load !openlast)
6413 then dolog "failed to load configuration";
6415 begin match !pageno with
6416 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
6417 | None -> ()
6418 end;
6420 if nonemptystr !gcconfig
6421 then (
6422 let (c, s) =
6423 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6424 | (exception exn) -> error "socketpair for gc failed: %s" @@ exntos exn
6425 | fds -> fds
6427 match spawn !gcconfig [(c, 0); (c, 1); (s, -1)] with
6428 | (exception exn) -> error "failed to execute gc script: %s" @@ exntos exn
6429 | _pid ->
6430 Ne.clo c @@ (fun s -> error "failed to close gc fd %s" s);
6431 Config.gc s;
6432 exit 0
6435 let mu =
6436 object (self)
6437 val mutable m_clicks = 0
6438 val mutable m_click_x = 0
6439 val mutable m_click_y = 0
6440 val mutable m_lastclicktime = infinity
6442 method private cleanup =
6443 state.roam <- noroam;
6444 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap
6445 method expose = G.postRedisplay "expose"
6446 method visible v =
6447 let name =
6448 match v with
6449 | Wsi.Unobscured -> "unobscured"
6450 | Wsi.PartiallyObscured -> "partiallyobscured"
6451 | Wsi.FullyObscured -> "fullyobscured"
6453 vlog "visibility change %s" name
6454 method display = display ()
6455 method map mapped = vlog "mapped %b" mapped
6456 method reshape w h =
6457 self#cleanup;
6458 reshape w h
6459 method mouse b d x y m =
6460 if d && canselect ()
6461 then (
6463 * http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx
6465 m_click_x <- x;
6466 m_click_y <- y;
6467 if b = 1
6468 then (
6469 let t = now () in
6470 if abs x - m_click_x > 10
6471 || abs y - m_click_y > 10
6472 || abs_float (t -. m_lastclicktime) > 0.3
6473 then m_clicks <- 0;
6474 m_clicks <- m_clicks + 1;
6475 m_lastclicktime <- t;
6476 if m_clicks = 1
6477 then (
6478 self#cleanup;
6479 G.postRedisplay "cleanup";
6480 state.uioh <- state.uioh#button b d x y m;
6482 else state.uioh <- state.uioh#multiclick m_clicks x y m
6484 else (
6485 self#cleanup;
6486 m_clicks <- 0;
6487 m_lastclicktime <- infinity;
6488 state.uioh <- state.uioh#button b d x y m
6491 else (
6492 state.uioh <- state.uioh#button b d x y m
6494 method motion x y =
6495 state.mpos <- (x, y);
6496 state.uioh <- state.uioh#motion x y
6497 method pmotion x y =
6498 state.mpos <- (x, y);
6499 state.uioh <- state.uioh#pmotion x y
6500 method key k m =
6501 let mascm = m land (
6502 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6503 ) in
6504 let keyboard k m =
6505 let x = state.x and y = state.y in
6506 keyboard k m;
6507 if x != state.x || y != state.y then self#cleanup
6509 match state.keystate with
6510 | KSnone ->
6511 let km = k, mascm in
6512 begin
6513 match
6514 let modehash = state.uioh#modehash in
6515 try Hashtbl.find modehash km
6516 with Not_found ->
6517 try Hashtbl.find (findkeyhash conf "global") km
6518 with Not_found -> KMinsrt (k, m)
6519 with
6520 | KMinsrt (k, m) -> keyboard k m
6521 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6522 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6524 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6525 List.iter (fun (k, m) -> keyboard k m) insrt;
6526 state.keystate <- KSnone
6527 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6528 state.keystate <- KSinto (keys, insrt)
6529 | KSinto _ -> state.keystate <- KSnone
6531 method enter x y =
6532 state.mpos <- (x, y);
6533 state.uioh <- state.uioh#pmotion x y
6534 method leave = state.mpos <- (-1, -1)
6535 method winstate wsl = state.winstate <- wsl
6536 method quit = raise Quit
6539 let wsfd, winw, winh = Wsi.init mu !rootwid conf.cwinw conf.cwinh platform in
6541 setbgcol conf.bgcolor;
6542 state.wsfd <- wsfd;
6544 if not @@ List.exists GlMisc.check_extension
6545 [ "GL_ARB_texture_rectangle"
6546 ; "GL_EXT_texture_recangle"
6547 ; "GL_NV_texture_rectangle" ]
6548 then (dolog "OpenGL does not suppport rectangular textures"; exit 1);
6550 if substratis (GlMisc.get_string `renderer) 0 "Mesa DRI Intel("
6551 then (
6552 defconf.sliceheight <- 1024;
6553 defconf.texcount <- 32;
6554 defconf.usepbo <- true;
6557 let cs, ss =
6558 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6559 | (exception exn) ->
6560 dolog "socketpair failed: %s" @@ exntos exn;
6561 exit 1
6562 | (r, w) ->
6563 cloexec r;
6564 cloexec w;
6565 r, w
6568 setcheckers conf.checkers;
6570 opengl_has_pbo := GlMisc.check_extension "GL_ARB_pixel_buffer_object";
6572 begin match !csspath with
6573 | None -> ()
6574 | Some "" -> conf.css <- E.s
6575 | Some path ->
6576 let css = filecontents path in
6577 let l = String.length css in
6578 conf.css <-
6579 if substratis css (l-2) "\r\n"
6580 then String.sub css 0 (l-2)
6581 else (if css.[l-1] = '\n'
6582 then String.sub css 0 (l-1)
6583 else css);
6584 end;
6585 init cs (
6586 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
6587 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6588 !Config.fontpath, !trimcachepath, !opengl_has_pbo, not !nofc
6590 List.iter GlArray.enable [`texture_coord; `vertex];
6591 state.ss <- ss;
6592 reshape ~firsttime:true winw winh;
6593 state.uioh <- uioh;
6594 if histmode
6595 then (
6596 Wsi.settitle "llpp (history)";
6597 enterhistmode ();
6599 else (
6600 state.text <- "Opening " ^ (mbtoutf8 state.path);
6601 opendoc state.path state.password;
6603 display ();
6604 Wsi.mapwin ();
6605 Wsi.setcursor Wsi.CURSOR_INHERIT;
6606 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
6608 let rec reap () =
6609 match Unix.waitpid [Unix.WNOHANG] ~-1 with
6610 | (exception (Unix.Unix_error (Unix.ECHILD, _, _))) -> ()
6611 | (exception exn) -> dolog "Unix.waitpid: %s" @@ exntos exn
6612 | 0, _ -> ()
6613 | _pid, _status -> reap ()
6615 Sys.set_signal Sys.sigchld (Sys.Signal_handle (fun _ -> doreap := true));
6617 let optrfd =
6618 ref (
6619 if nonemptystr !rcmdpath
6620 then remoteopen !rcmdpath
6621 else None
6625 let rec loop deadline =
6626 if !doreap
6627 then (
6628 doreap := false;
6629 reap ()
6631 let r = [state.ss; state.wsfd] in
6632 let r =
6633 match !optrfd with
6634 | None -> r
6635 | Some fd -> fd :: r
6637 if state.redisplay
6638 then (
6639 state.redisplay <- false;
6640 display ();
6642 let timeout =
6643 let now = now () in
6644 if deadline > now
6645 then (
6646 if deadline = infinity
6647 then ~-.1.0
6648 else max 0.0 (deadline -. now)
6650 else 0.0
6652 let r, _, _ =
6653 try Unix.select r [] [] timeout
6654 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6656 begin match r with
6657 | [] ->
6658 state.ghyll None;
6659 let newdeadline =
6660 if state.ghyll == noghyll
6661 then
6662 match state.autoscroll with
6663 | Some step when step != 0 ->
6664 let y = state.y + step in
6665 let fy = if conf.maxhfit then state.winh else 0 in
6666 let y =
6667 if y < 0
6668 then state.maxy - fy
6669 else if y >= state.maxy - fy then 0 else y
6671 if state.mode = View
6672 then gotoxy_and_clear_text state.x y
6673 else gotoxy state.x y;
6674 deadline +. 0.01
6675 | _ -> infinity
6676 else deadline +. 0.01
6678 loop newdeadline
6680 | l ->
6681 let rec checkfds = function
6682 | [] -> ()
6683 | fd :: rest when fd = state.ss ->
6684 let cmd = rcmd state.ss in
6685 act cmd;
6686 checkfds rest
6688 | fd :: rest when fd = state.wsfd ->
6689 Wsi.readresp fd;
6690 checkfds rest
6692 | fd :: rest when Some fd = !optrfd ->
6693 begin match remote fd with
6694 | None -> optrfd := remoteopen !rcmdpath;
6695 | opt -> optrfd := opt
6696 end;
6697 checkfds rest
6699 | _ :: rest ->
6700 dolog "select returned unknown descriptor";
6701 checkfds rest
6703 checkfds l;
6704 let newdeadline =
6705 let deadline1 =
6706 if deadline = infinity
6707 then now () +. 0.01
6708 else deadline
6710 match state.autoscroll with
6711 | Some step when step != 0 -> deadline1
6712 | _ -> if state.ghyll == noghyll then infinity else deadline1
6714 loop newdeadline
6715 end;
6717 match loop infinity with
6718 | exception Quit ->
6719 Config.save leavebirdseye;
6720 if hasunsavedchanges ()
6721 then save ()
6722 | _ -> error "umpossible - infinity reached"