Beautify CreationDate
[llpp.git] / main.ml
blob3dd36156b3ebd40f1c1c09bd44950b4d5d12a198
1 open Utils;;
2 open Config;;
4 exception Quit;;
6 external init : Unix.file_descr -> params -> unit = "ml_init";;
7 external seltext : opaque -> (int * int * int * int) -> unit = "ml_seltext";;
8 external hassel : opaque -> bool = "ml_hassel";;
9 external copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
10 external getpdimrect : int -> float array = "ml_getpdimrect";;
11 external whatsunder : opaque -> int -> int -> under = "ml_whatsunder";;
12 external markunder : opaque -> int -> int -> mark -> bool = "ml_markunder";;
13 external clearmark : opaque -> unit = "ml_clearmark";;
14 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
15 external getmaxw : unit -> float = "ml_getmaxw";;
16 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
17 external measurestr : int -> string -> float = "ml_measure_string";;
18 external postprocess :
19 opaque -> int -> int -> int -> (int * string * int) -> int
20 = "ml_postprocess";;
21 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
22 external setaalevel : int -> unit = "ml_setaalevel";;
23 external realloctexts : int -> bool = "ml_realloctexts";;
24 external findlink : opaque -> linkdir -> link = "ml_findlink";;
25 external getlink : opaque -> int -> under = "ml_getlink";;
26 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
27 external getlinkcount : opaque -> int = "ml_getlinkcount";;
28 external findpwl : int -> int -> pagewithlinks = "ml_find_page_with_links";;
29 external getpbo : width -> height -> colorspace -> opaque = "ml_getpbo";;
30 external freepbo : opaque -> unit = "ml_freepbo";;
31 external unmappbo : opaque -> unit = "ml_unmappbo";;
32 external bousable : unit -> bool = "ml_bo_usable";;
33 external unproject : opaque -> int -> int -> (int * int) option
34 = "ml_unproject";;
35 external project : opaque -> int -> int -> float -> float -> (float * float)
36 = "ml_project";;
37 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
38 external rectofblock : opaque -> int -> int -> float array option
39 = "ml_rectofblock";;
40 external begintiles : unit -> unit = "ml_begintiles";;
41 external endtiles : unit -> unit = "ml_endtiles";;
42 external addannot : opaque -> int -> int -> string -> unit = "ml_addannot";;
43 external modannot : opaque -> slinkindex -> string -> unit = "ml_modannot";;
44 external delannot : opaque -> slinkindex -> unit = "ml_delannot";;
45 external hasunsavedchanges : unit -> bool = "ml_hasunsavedchanges";;
46 external savedoc : string -> unit = "ml_savedoc";;
47 external getannotcontents : opaque -> slinkindex -> string
48 = "ml_getannotcontents";;
49 external drawprect : opaque -> int -> int -> float array -> unit =
50 "ml_drawprect";;
51 external wcmd : Unix.file_descr -> bytes -> int -> unit = "ml_wcmd";;
52 external rcmd : Unix.file_descr -> string = "ml_rcmd";;
53 external uritolocation : string -> (pageno * float * float)
54 = "ml_uritolocation";;
55 external isexternallink : string -> bool = "ml_isexternallink";;
57 let selfexec = ref E.s;;
58 let opengl_has_pbo = ref false;;
60 let drawstring size x y s =
61 Gl.enable `blend;
62 Gl.enable `texture_2d;
63 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
64 ignore (drawstr size x y s);
65 Gl.disable `blend;
66 Gl.disable `texture_2d;
69 let drawstring1 size x y s =
70 drawstr size x y s;
73 let drawstring2 size x y fmt =
74 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
77 let _debugl l =
78 dolog "l %d dim=%d {" l.pageno l.pagedimno;
79 dolog " WxH %dx%d" l.pagew l.pageh;
80 dolog " vWxH %dx%d" l.pagevw l.pagevh;
81 dolog " pagex,y %d,%d" l.pagex l.pagey;
82 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
83 dolog " column %d" l.pagecol;
84 dolog "}";
87 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
88 dolog "rect {";
89 dolog " x0,y0=(% f, % f)" x0 y0;
90 dolog " x1,y1=(% f, % f)" x1 y1;
91 dolog " x2,y2=(% f, % f)" x2 y2;
92 dolog " x3,y3=(% f, % f)" x3 y3;
93 dolog "}";
96 let isbirdseye = function
97 | Birdseye _ -> true
98 | Textentry _
99 | View
100 | LinkNav _ -> false
103 let istextentry = function
104 | Textentry _ -> true
105 | Birdseye _
106 | View
107 | LinkNav _ -> false
110 let wtmode = ref false;;
111 let cxack = ref false;;
113 let pgscale h = truncate (float h *. conf.pgscale);;
115 let hscrollh () =
116 if state.uioh#alwaysscrolly || ((conf.scrollb land scrollbhv != 0)
117 && (state.w > state.winw))
118 then conf.scrollbw
119 else 0
122 let vscrollw () =
123 if state.uioh#alwaysscrolly || ((conf.scrollb land scrollbvv != 0)
124 && (state.maxy > state.winh))
125 then conf.scrollbw
126 else 0
129 let vscrollhit x =
130 if conf.leftscroll
131 then x < vscrollw ()
132 else x > state.winw - vscrollw ()
135 let setfontsize n =
136 fstate.fontsize <- n;
137 fstate.wwidth <- measurestr fstate.fontsize "w";
138 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
141 let vlog fmt =
142 if conf.verbose
143 then dolog fmt
144 else Printf.kprintf ignore fmt
147 let launchpath () =
148 if emptystr conf.pathlauncher
149 then dolog "%s" state.path
150 else (
151 let command = Str.global_replace percentsre state.path conf.pathlauncher in
152 match spawn command [] with
153 | _pid -> ()
154 | (exception exn) ->
155 dolog "failed to execute `%s': %s" command @@ exntos exn
159 module G =
160 struct
161 let postRedisplay who =
162 vlog "redisplay for [%S]" who;
163 state.redisplay <- true;
165 end;;
167 let getopaque pageno =
168 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
169 with Not_found -> None
172 let pagetranslatepoint l x y =
173 let dy = y - l.pagedispy in
174 let y = dy + l.pagey in
175 let dx = x - l.pagedispx in
176 let x = dx + l.pagex in
177 (x, y);
180 let onppundermouse g x y d =
181 let rec f = function
182 | l :: rest ->
183 begin match getopaque l.pageno with
184 | Some opaque ->
185 let x0 = l.pagedispx in
186 let x1 = x0 + l.pagevw in
187 let y0 = l.pagedispy in
188 let y1 = y0 + l.pagevh in
189 if y >= y0 && y <= y1 && x >= x0 && x <= x1
190 then
191 let px, py = pagetranslatepoint l x y in
192 match g opaque l px py with
193 | Some res -> res
194 | None -> f rest
195 else f rest
196 | _ ->
197 f rest
199 | [] -> d
201 f state.layout
204 let getunder x y =
205 let g opaque l px py =
206 if state.bzoom
207 then (
208 match rectofblock opaque px py with
209 | Some [|x0;x1;y0;y1|] ->
210 let rect = (x0, y0, x1, y0, x1, y1, x0, y1) in
211 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
212 state.rects <- [l.pageno, color, rect];
213 G.postRedisplay "getunder";
214 | _otherwise -> ()
216 let under = whatsunder opaque px py in
217 if under = Unone then None else Some under
219 onppundermouse g x y Unone
222 let unproject x y =
223 let g opaque l x y =
224 match unproject opaque x y with
225 | Some (x, y) -> Some (Some (opaque, l.pageno, x, y))
226 | None -> None
228 onppundermouse g x y None;
231 let showtext c s =
232 state.text <- Printf.sprintf "%c%s" c s;
233 G.postRedisplay "showtext";
236 let impmsg fmt =
237 Format.ksprintf (fun s -> showtext '!' s) fmt;
240 let pipesel opaque cmd =
241 if hassel opaque
242 then
243 match Unix.pipe () with
244 | (exception exn) -> dolog "pipesel cannot create pipe: %S" @@ exntos exn;
245 | (r, w) ->
246 let doclose what fd =
247 Ne.clo fd (fun msg -> dolog "%s close failed: %s" what msg)
249 let pid =
250 try spawn cmd [r, 0; w, -1]
251 with exn ->
252 dolog "cannot execute %S: %s" cmd @@ exntos exn;
255 if pid > 0
256 then (
257 copysel w opaque;
258 G.postRedisplay "pipesel";
260 else doclose "pipesel pipe/w" w;
261 doclose "pipesel pipe/r" r;
264 let paxunder x y =
265 let g opaque l px py =
266 if markunder opaque px py conf.paxmark
267 then (
268 Some (fun () ->
269 match getopaque l.pageno with
270 | None -> ()
271 | Some opaque -> pipesel opaque conf.paxcmd
274 else None
276 G.postRedisplay "paxunder";
277 if conf.paxmark = Mark_page
278 then
279 List.iter (fun l ->
280 match getopaque l.pageno with
281 | None -> ()
282 | Some opaque -> clearmark opaque) state.layout;
283 state.roam <- onppundermouse g x y (fun () -> impmsg "whoopsie daisy");
286 let selstring s =
287 match Unix.pipe () with
288 | (exception exn) -> impmsg "pipe failed: %s" @@ exntos exn
289 | (r, w) ->
290 let clo cap fd =
291 Ne.clo fd (fun msg -> impmsg "failed to close %s: %s" cap msg)
293 let pid =
294 try spawn conf.selcmd [r, 0; w, -1]
295 with exn ->
296 impmsg "failed to execute %s: %s" conf.selcmd @@ exntos exn;
299 if pid > 0
300 then (
302 let l = String.length s in
303 let bytes = Bytes.unsafe_of_string s in
304 let n = tempfailureretry (Unix.write w bytes 0) l in
305 if n != l
306 then impmsg "failed to write %d characters to sel pipe, wrote %d"
308 with exn ->
309 impmsg "failed to write to sel pipe: %s" @@ exntos exn
311 else dolog "%s" s;
312 clo "selstring pipe/r" r;
313 clo "selstring pipe/w" w;
316 let undertext = function
317 | Unone -> "none"
318 | Ulinkuri s -> s
319 | Utext s -> "font: " ^ s
320 | Uannotation (opaque, slinkindex) ->
321 "annotation: " ^ getannotcontents opaque slinkindex
324 let updateunder x y =
325 match getunder x y with
326 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
327 | Ulinkuri uri ->
328 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
329 Wsi.setcursor Wsi.CURSOR_INFO
330 | Utext s ->
331 if conf.underinfo then showtext 'f' ("ont: " ^ s);
332 Wsi.setcursor Wsi.CURSOR_TEXT
333 | Uannotation _ ->
334 if conf.underinfo then showtext 'a' "nnotation";
335 Wsi.setcursor Wsi.CURSOR_INFO
338 let showlinktype under =
339 if conf.underinfo && under != Unone
340 then showtext ' ' @@ undertext under
343 let intentry_with_suffix text key =
344 let text =
345 if key >= 32 && key < 127
346 then
347 let c = Char.chr key in
348 match c with
349 | '0' .. '9' ->
350 addchar text c
352 | 'k' | 'm' | 'g' | 'K' | 'M' | 'G' ->
353 addchar text @@ asciilower c
354 | _ ->
355 state.text <- Printf.sprintf "invalid key (%d, `%c')" key c;
356 text
357 else (
358 state.text <- Printf.sprintf "invalid key %d" key;
359 text
362 TEcont text
365 let wcmd fmt =
366 let b = Buffer.create 16 in
367 Buffer.add_string b "llll";
368 Printf.kbprintf
369 (fun b ->
370 let b = Buffer.to_bytes b in
371 wcmd state.ss b @@ Bytes.length b
372 ) b fmt
375 let nogeomcmds cmds =
376 match cmds with
377 | s, [] -> emptystr s
378 | _ -> false
381 let layoutN ((columns, coverA, coverB), b) x y sw sh =
382 let rec fold accu n =
383 if n = Array.length b
384 then accu
385 else
386 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
387 if (vy - y) > sh &&
388 (n = coverA - 1
389 || n = state.pagecount - coverB
390 || (n - coverA) mod columns = columns - 1)
391 then accu
392 else
393 let accu =
394 if vy + h > y
395 then
396 let pagey = max 0 (y - vy) in
397 let pagedispy = if pagey > 0 then 0 else vy - y in
398 let pagedispx, pagex =
399 let pdx =
400 if n = coverA - 1 || n = state.pagecount - coverB
401 then x + (sw - w) / 2
402 else dx + xoff + x
404 if pdx < 0
405 then 0, -pdx
406 else pdx, 0
408 let pagevw =
409 let vw = sw - pagedispx in
410 let pw = w - pagex in
411 min vw pw
413 let pagevh = min (h - pagey) (sh - pagedispy) in
414 if pagevw > 0 && pagevh > 0
415 then
416 let e =
417 { pageno = n
418 ; pagedimno = pdimno
419 ; pagew = w
420 ; pageh = h
421 ; pagex = pagex
422 ; pagey = pagey
423 ; pagevw = pagevw
424 ; pagevh = pagevh
425 ; pagedispx = pagedispx
426 ; pagedispy = pagedispy
427 ; pagecol = 0
430 e :: accu
431 else
432 accu
433 else
434 accu
436 fold accu (n+1)
438 if Array.length b = 0
439 then []
440 else List.rev (fold [] (page_of_y y))
443 let layoutS (columns, b) x y sw sh =
444 let rec fold accu n =
445 if n = Array.length b
446 then accu
447 else
448 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
449 if (vy - y) > sh
450 then accu
451 else
452 let accu =
453 if vy + pageh > y
454 then
455 let x = xoff + x in
456 let pagey = max 0 (y - vy) in
457 let pagedispy = if pagey > 0 then 0 else vy - y in
458 let pagedispx, pagex =
459 if px = 0
460 then (
461 if x < 0
462 then 0, -x
463 else x, 0
465 else (
466 let px = px - x in
467 if px < 0
468 then -px, 0
469 else 0, px
472 let pagecolw = pagew/columns in
473 let pagedispx =
474 if pagecolw < sw
475 then pagedispx + ((sw - pagecolw) / 2)
476 else pagedispx
478 let pagevw =
479 let vw = sw - pagedispx in
480 let pw = pagew - pagex in
481 min vw pw
483 let pagevw = min pagevw pagecolw in
484 let pagevh = min (pageh - pagey) (sh - pagedispy) in
485 if pagevw > 0 && pagevh > 0
486 then
487 let e =
488 { pageno = n/columns
489 ; pagedimno = pdimno
490 ; pagew = pagew
491 ; pageh = pageh
492 ; pagex = pagex
493 ; pagey = pagey
494 ; pagevw = pagevw
495 ; pagevh = pagevh
496 ; pagedispx = pagedispx
497 ; pagedispy = pagedispy
498 ; pagecol = n mod columns
501 e :: accu
502 else
503 accu
504 else
505 accu
507 fold accu (n+1)
509 List.rev (fold [] 0)
512 let layout x y sw sh =
513 if nogeomcmds state.geomcmds
514 then
515 match conf.columns with
516 | Csingle b -> layoutN ((1, 0, 0), b) x y sw sh
517 | Cmulti c -> layoutN c x y sw sh
518 | Csplit s -> layoutS s x y sw sh
519 else []
522 let clamp incr =
523 let y = state.y + incr in
524 let y = max 0 y in
525 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
529 let itertiles l f =
530 let tilex = l.pagex mod conf.tilew in
531 let tiley = l.pagey mod conf.tileh in
533 let col = l.pagex / conf.tilew in
534 let row = l.pagey / conf.tileh in
536 let rec rowloop row y0 dispy h =
537 if h = 0
538 then ()
539 else (
540 let dh = conf.tileh - y0 in
541 let dh = min h dh in
542 let rec colloop col x0 dispx w =
543 if w = 0
544 then ()
545 else (
546 let dw = conf.tilew - x0 in
547 let dw = min w dw in
548 f col row dispx dispy x0 y0 dw dh;
549 colloop (col+1) 0 (dispx+dw) (w-dw)
552 colloop col tilex l.pagedispx l.pagevw;
553 rowloop (row+1) 0 (dispy+dh) (h-dh)
556 if l.pagevw > 0 && l.pagevh > 0
557 then rowloop row tiley l.pagedispy l.pagevh;
560 let gettileopaque l col row =
561 let key =
562 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
564 try Some (Hashtbl.find state.tilemap key)
565 with Not_found -> None
568 let puttileopaque l col row gen colorspace angle opaque size elapsed =
569 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
570 Hashtbl.add state.tilemap key (opaque, size, elapsed)
573 let filledrect2 x0 y0 x1 y1 x2 y2 x3 y3 =
574 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x1; y1; x2; y2; x3; y3 |];
575 GlArray.vertex `two state.vraw;
576 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
579 let filledrect1 x0 y0 x1 y1 = filledrect2 x0 y0 x0 y1 x1 y0 x1 y1;;
581 let filledrect x0 y0 x1 y1 =
582 GlArray.disable `texture_coord;
583 filledrect1 x0 y0 x1 y1;
584 GlArray.enable `texture_coord;
587 let linerect x0 y0 x1 y1 =
588 GlArray.disable `texture_coord;
589 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y1; x1; y0 |];
590 GlArray.vertex `two state.vraw;
591 GlArray.draw_arrays `line_loop ~first:0 ~count:4;
592 GlArray.enable `texture_coord;
595 let drawtiles l color =
596 GlDraw.color color;
597 begintiles ();
598 let f col row x y tilex tiley w h =
599 match gettileopaque l col row with
600 | Some (opaque, _, t) ->
601 let params = x, y, w, h, tilex, tiley in
602 if conf.invert
603 then GlTex.env (`mode `blend);
604 drawtile params opaque;
605 if conf.invert
606 then GlTex.env (`mode `modulate);
607 if conf.debug
608 then (
609 endtiles ();
610 let s = Printf.sprintf
611 "%d[%d,%d] %f sec"
612 l.pageno col row t
614 let w = measurestr fstate.fontsize s in
615 GlDraw.color (0.0, 0.0, 0.0);
616 filledrect (float (x-2))
617 (float (y-2))
618 (float (x+2) +. w)
619 (float (y + fstate.fontsize + 2));
620 GlDraw.color color;
621 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
622 begintiles ();
625 | None ->
626 endtiles ();
627 let w =
628 let lw = state.winw - x in
629 min lw w
630 and h =
631 let lh = state.winh - y in
632 min lh h
634 if conf.invert
635 then GlTex.env (`mode `blend);
636 begin match state.checkerstexid with
637 | Some id ->
638 Gl.enable `texture_2d;
639 GlTex.bind_texture ~target:`texture_2d id;
640 let x0 = float x
641 and y0 = float y
642 and x1 = float (x+w)
643 and y1 = float (y+h) in
645 let tw = float w /. 16.0
646 and th = float h /. 16.0 in
647 let tx0 = float tilex /. 16.0
648 and ty0 = float tiley /. 16.0 in
649 let tx1 = tx0 +. tw
650 and ty1 = ty0 +. th in
651 Raw.sets_float state.vraw ~pos:0
652 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
653 Raw.sets_float state.traw ~pos:0
654 [| tx0; ty0; tx0; ty1; tx1; ty0; tx1; ty1 |];
655 GlArray.vertex `two state.vraw;
656 GlArray.tex_coord `two state.traw;
657 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
658 Gl.disable `texture_2d;
660 | None ->
661 GlDraw.color (1.0, 1.0, 1.0);
662 filledrect (float x) (float y) (float (x+w)) (float (y+h));
663 end;
664 if conf.invert
665 then GlTex.env (`mode `modulate);
666 if w > 128 && h > fstate.fontsize + 10
667 then (
668 let c = if conf.invert then 1.0 else 0.0 in
669 GlDraw.color (c, c, c);
670 let c, r =
671 if conf.verbose
672 then (col*conf.tilew, row*conf.tileh)
673 else col, row
675 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
677 GlDraw.color color;
678 begintiles ();
680 itertiles l f;
681 endtiles ();
684 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
686 let tilevisible1 l x y =
687 let ax0 = l.pagex
688 and ax1 = l.pagex + l.pagevw
689 and ay0 = l.pagey
690 and ay1 = l.pagey + l.pagevh in
692 let bx0 = x
693 and by0 = y in
694 let bx1 = min (bx0 + conf.tilew) l.pagew
695 and by1 = min (by0 + conf.tileh) l.pageh in
697 let rx0 = max ax0 bx0
698 and ry0 = max ay0 by0
699 and rx1 = min ax1 bx1
700 and ry1 = min ay1 by1 in
702 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
703 nonemptyintersection
706 let tilevisible layout n x y =
707 let rec findpageinlayout m = function
708 | l :: rest when l.pageno = n ->
709 tilevisible1 l x y || (
710 match conf.columns with
711 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
712 | Csplit _
713 | Csingle _
714 | Cmulti _ -> false
716 | _ :: rest -> findpageinlayout 0 rest
717 | [] -> false
719 findpageinlayout 0 layout;
722 let tileready l x y =
723 tilevisible1 l x y &&
724 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
727 let tilepage n p layout =
728 let rec loop = function
729 | l :: rest ->
730 if l.pageno = n
731 then
732 let f col row _ _ _ _ _ _ =
733 if state.currently = Idle
734 then
735 match gettileopaque l col row with
736 | Some _ -> ()
737 | None ->
738 let x = col*conf.tilew
739 and y = row*conf.tileh in
740 let w =
741 let w = l.pagew - x in
742 min w conf.tilew
744 let h =
745 let h = l.pageh - y in
746 min h conf.tileh
748 let pbo =
749 if conf.usepbo
750 then getpbo w h conf.colorspace
751 else ~< "0"
753 wcmd "tile %s %d %d %d %d %s"
754 (~> p) x y w h (~> pbo);
755 state.currently <-
756 Tiling (
757 l, p, conf.colorspace, conf.angle,
758 state.gen, col, row, conf.tilew, conf.tileh
761 itertiles l f;
762 else
763 loop rest
765 | [] -> ()
767 if nogeomcmds state.geomcmds
768 then loop layout;
771 let preloadlayout x y sw sh =
772 let y = if y < sh then 0 else y - sh in
773 let x = min 0 (x + sw) in
774 let h = sh*3 in
775 let w = sw*3 in
776 layout x y w h;
779 let load pages =
780 let rec loop pages =
781 if state.currently != Idle
782 then ()
783 else
784 match pages with
785 | l :: rest ->
786 begin match getopaque l.pageno with
787 | None ->
788 wcmd "page %d %d" l.pageno l.pagedimno;
789 state.currently <- Loading (l, state.gen);
790 | Some opaque ->
791 tilepage l.pageno opaque pages;
792 loop rest
793 end;
794 | _ -> ()
796 if nogeomcmds state.geomcmds
797 then loop pages
800 let preload pages =
801 load pages;
802 if conf.preload && state.currently = Idle
803 then load (preloadlayout state.x state.y state.winw state.winh);
806 let layoutready layout =
807 let rec fold all ls =
808 all && match ls with
809 | l :: rest ->
810 let seen = ref false in
811 let allvisible = ref true in
812 let foo col row _ _ _ _ _ _ =
813 seen := true;
814 allvisible := !allvisible &&
815 begin match gettileopaque l col row with
816 | Some _ -> true
817 | None -> false
820 itertiles l foo;
821 fold (!seen && !allvisible) rest
822 | [] -> true
824 let alltilesvisible = fold true layout in
825 alltilesvisible;
828 let gotoxy x y =
829 let y = bound y 0 state.maxy in
830 let y, layout, proceed =
831 match conf.maxwait with
832 | Some time when state.ghyll == noghyll ->
833 begin match state.throttle with
834 | None ->
835 let layout = layout x y state.winw state.winh in
836 let ready = layoutready layout in
837 if not ready
838 then (
839 load layout;
840 state.throttle <- Some (layout, y, now ());
842 else G.postRedisplay "gotoxy showall (None)";
843 y, layout, ready
844 | Some (_, _, started) ->
845 let dt = now () -. started in
846 if dt > time
847 then (
848 state.throttle <- None;
849 let layout = layout x y state.winw state.winh in
850 load layout;
851 G.postRedisplay "maxwait";
852 y, layout, true
854 else -1, [], false
857 | _ ->
858 let layout = layout x y state.winw state.winh in
859 if not !wtmode || layoutready layout
860 then G.postRedisplay "gotoxy ready";
861 y, layout, true
863 if proceed
864 then (
865 state.x <- x;
866 state.y <- y;
867 state.layout <- layout;
868 begin match state.mode with
869 | LinkNav ln ->
870 begin match ln with
871 | Ltexact (pageno, linkno) ->
872 let rec loop = function
873 | [] ->
874 state.mode <- LinkNav (Ltgendir 0)
875 | l :: _ when l.pageno = pageno ->
876 begin match getopaque pageno with
877 | None -> state.mode <- LinkNav (Ltnotready (pageno, 0))
878 | Some opaque ->
879 let x0, y0, x1, y1 = getlinkrect opaque linkno in
880 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
881 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
882 then state.mode <- LinkNav (Ltgendir 0)
884 | _ :: rest -> loop rest
886 loop layout
887 | Ltnotready _ | Ltgendir _ -> ()
889 | Birdseye _
890 | Textentry _
891 | View -> ()
892 end;
893 begin match state.mode with
894 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
895 if not (pagevisible layout pageno)
896 then (
897 match state.layout with
898 | [] -> ()
899 | l :: _ ->
900 state.mode <- Birdseye (
901 conf, leftx, l.pageno, hooverpageno, anchor
904 | LinkNav lt ->
905 begin match lt with
906 | Ltnotready (_, dir)
907 | Ltgendir dir ->
908 let linknav =
909 let rec loop = function
910 | [] -> lt
911 | l :: rest ->
912 match getopaque l.pageno with
913 | None -> Ltnotready (l.pageno, dir)
914 | Some opaque ->
915 let link =
916 let ld =
917 if dir = 0
918 then LDfirstvisible (l.pagex, l.pagey, dir)
919 else (
920 if dir > 0 then LDfirst else LDlast
923 findlink opaque ld
925 match link with
926 | Lnotfound -> loop rest
927 | Lfound n ->
928 showlinktype (getlink opaque n);
929 Ltexact (l.pageno, n)
931 loop state.layout
933 state.mode <- LinkNav linknav
934 | Ltexact _ -> ()
936 | Textentry _
937 | View -> ()
938 end;
939 preload layout;
941 state.ghyll <- noghyll;
942 if conf.updatecurs
943 then (
944 let mx, my = state.mpos in
945 updateunder mx my;
949 let conttiling pageno opaque =
950 tilepage pageno opaque
951 (if conf.preload
952 then preloadlayout state.x state.y state.winw state.winh
953 else state.layout)
956 let gotoxy_and_clear_text x y =
957 if not conf.verbose then state.text <- E.s;
958 gotoxy x y;
961 let getanchory (n, top, dtop) =
962 let y, h = getpageyh n in
963 if conf.presentation
964 then
965 let ips = calcips h in
966 y + truncate (top*.float h -. dtop*.float ips) + ips;
967 else
968 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
971 let gotoanchor anchor =
972 gotoxy state.x (getanchory anchor);
975 let addnav () =
976 cbput state.hists.nav (getanchor ());
979 let getnav dir =
980 let anchor = cbgetc state.hists.nav dir in
981 getanchory anchor;
984 let gotoghyll1 single y =
985 let scroll f n a b =
986 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
987 let snake f a b =
988 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
989 if f < a
990 then s (float f /. float a)
991 else (
992 if f > b
993 then 1.0 -. s ((float (f-b) /. float (n-b)))
994 else 1.0
997 snake f a b
998 and summa n a b =
999 let ins = float a *. 0.5
1000 and outs = float (n-b) *. 0.5 in
1001 let ones = b - a in
1002 ins +. outs +. float ones
1004 let rec set nab y sy =
1005 let (_N, _A, _B), y =
1006 if single
1007 then
1008 let scl = if y > sy then 2 else -2 in
1009 let _N, _, _ = nab in
1010 (_N,0,_N), y+conf.scrollstep*scl
1011 else nab,y in
1012 let sum = summa _N _A _B in
1013 let dy = float (y - sy) in
1014 state.ghyll <- (
1015 let rec gf n y1 o =
1016 if n >= _N
1017 then state.ghyll <- noghyll
1018 else
1019 let go n =
1020 let s = scroll n _N _A _B in
1021 let y1 = y1 +. ((s *. dy) /. sum) in
1022 gotoxy_and_clear_text state.x (truncate y1);
1023 state.ghyll <- gf (n+1) y1;
1025 match o with
1026 | None -> go n
1027 | Some y' when single -> set nab y' state.y
1028 | Some y' -> set (_N/2, 1, 1) y' state.y
1030 gf 0 (float state.y)
1033 match conf.ghyllscroll with
1034 | Some nab when not conf.presentation ->
1035 if state.ghyll == noghyll
1036 then set nab y state.y
1037 else state.ghyll (Some y)
1038 | _ ->
1039 gotoxy_and_clear_text state.x y
1042 let gotoghyll = gotoghyll1 false;;
1044 let gotopage n top =
1045 let y, h = getpageyh n in
1046 let y = y + (truncate (top *. float h)) in
1047 gotoghyll y
1050 let gotopage1 n top =
1051 let y = getpagey n in
1052 let y = y + top in
1053 gotoghyll y
1056 let invalidate s f =
1057 state.redisplay <- false;
1058 state.layout <- [];
1059 state.pdims <- [];
1060 state.rects <- [];
1061 state.rects1 <- [];
1062 match state.geomcmds with
1063 | ps, [] when emptystr ps ->
1064 f ();
1065 state.geomcmds <- s, [];
1067 | ps, [] ->
1068 state.geomcmds <- ps, [s, f];
1070 | ps, (s', _) :: rest when s' = s ->
1071 state.geomcmds <- ps, ((s, f) :: rest);
1073 | ps, cmds ->
1074 state.geomcmds <- ps, ((s, f) :: cmds);
1077 let flushpages () =
1078 Hashtbl.iter (fun _ opaque ->
1079 wcmd "freepage %s" (~> opaque);
1080 ) state.pagemap;
1081 Hashtbl.clear state.pagemap;
1084 let flushtiles () =
1085 if not (Queue.is_empty state.tilelru)
1086 then (
1087 Queue.iter (fun (k, p, s) ->
1088 wcmd "freetile %s" (~> p);
1089 state.memused <- state.memused - s;
1090 Hashtbl.remove state.tilemap k;
1091 ) state.tilelru;
1092 state.uioh#infochanged Memused;
1093 Queue.clear state.tilelru;
1095 load state.layout;
1098 let stateh h =
1099 let h = truncate (float h*.conf.zoom) in
1100 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
1101 h - d
1104 let opendoc path password =
1105 state.path <- path;
1106 state.password <- password;
1107 state.gen <- state.gen + 1;
1108 state.docinfo <- [];
1109 state.outlines <- [||];
1111 flushpages ();
1112 setaalevel conf.aalevel;
1113 let titlepath =
1114 if emptystr state.origin
1115 then path
1116 else state.origin
1118 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
1119 wcmd "open %d %d %s\000%s\000" (btod !wtmode) (btod !cxack) path password;
1120 invalidate "reqlayout"
1121 (fun () ->
1122 wcmd "reqlayout %d %d %d %s\000"
1123 conf.angle (FMTE.to_int conf.fitmodel)
1124 (stateh state.winh) state.nameddest
1128 let reload () =
1129 state.anchor <- getanchor ();
1130 opendoc state.path state.password;
1133 let scalecolor c =
1134 let c = c *. conf.colorscale in
1135 (c, c, c);
1138 let scalecolor2 (r, g, b) =
1139 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1142 let docolumns columns =
1143 match columns with
1144 | Csingle _ ->
1145 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1146 let rec loop pageno pdimno pdim y ph pdims =
1147 if pageno = state.pagecount
1148 then ()
1149 else
1150 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1151 match pdims with
1152 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1153 pdimno+1, pdim, rest
1154 | _ ->
1155 pdimno, pdim, pdims
1157 let x = max 0 (((state.winw - w) / 2) - xoff) in
1158 let y = y +
1159 (if conf.presentation
1160 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1161 else (if pageno = 0 then 0 else conf.interpagespace)
1164 a.(pageno) <- (pdimno, x, y, pdim);
1165 loop (pageno+1) pdimno pdim (y + h) h pdims
1167 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1168 conf.columns <- Csingle a;
1170 | Cmulti ((columns, coverA, coverB), _) ->
1171 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1172 let rec loop pageno pdimno pdim x y rowh pdims =
1173 let rec fixrow m = if m = pageno then () else
1174 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1175 if h < rowh
1176 then (
1177 let y = y + (rowh - h) / 2 in
1178 a.(m) <- (pdimno, x, y, pdim);
1180 fixrow (m+1)
1182 if pageno = state.pagecount
1183 then fixrow (((pageno - 1) / columns) * columns)
1184 else
1185 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1186 match pdims with
1187 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1188 pdimno+1, pdim, rest
1189 | _ ->
1190 pdimno, pdim, pdims
1192 let x, y, rowh' =
1193 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1194 then (
1195 let x = (state.winw - w) / 2 in
1196 let ips =
1197 if conf.presentation then calcips h else conf.interpagespace in
1198 x, y + ips + rowh, h
1200 else (
1201 if (pageno - coverA) mod columns = 0
1202 then (
1203 let x = max 0 (state.winw - state.w) / 2 in
1204 let y =
1205 if conf.presentation
1206 then
1207 let ips = calcips h in
1208 y + (if pageno = 0 then 0 else calcips rowh + ips)
1209 else
1210 y + (if pageno = 0 then 0 else conf.interpagespace)
1212 x, y + rowh, h
1214 else x, y, max rowh h
1217 let y =
1218 if pageno > 1 && (pageno - coverA) mod columns = 0
1219 then (
1220 let y =
1221 if pageno = columns && conf.presentation
1222 then (
1223 let ips = calcips rowh in
1224 for i = 0 to pred columns
1226 let (pdimno, x, y, pdim) = a.(i) in
1227 a.(i) <- (pdimno, x, y+ips, pdim)
1228 done;
1229 y+ips;
1231 else y
1233 fixrow (pageno - columns);
1236 else y
1238 a.(pageno) <- (pdimno, x, y, pdim);
1239 let x = x + w + xoff*2 + conf.interpagespace in
1240 loop (pageno+1) pdimno pdim x y rowh' pdims
1242 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1243 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1245 | Csplit (c, _) ->
1246 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1247 let rec loop pageno pdimno pdim y pdims =
1248 if pageno = state.pagecount
1249 then ()
1250 else
1251 let pdimno, ((_, w, h, _) as pdim), pdims =
1252 match pdims with
1253 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1254 pdimno+1, pdim, rest
1255 | _ ->
1256 pdimno, pdim, pdims
1258 let cw = w / c in
1259 let rec loop1 n x y =
1260 if n = c then y else (
1261 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1262 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1265 let y = loop1 0 0 y in
1266 loop (pageno+1) pdimno pdim y pdims
1268 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1269 conf.columns <- Csplit (c, a);
1272 let represent () =
1273 docolumns conf.columns;
1274 state.maxy <- calcheight ();
1275 if state.reprf == noreprf
1276 then (
1277 match state.mode with
1278 | Birdseye (_, _, pageno, _, _) ->
1279 let y, h = getpageyh pageno in
1280 let top = (state.winh - h) / 2 in
1281 gotoxy state.x (max 0 (y - top))
1282 | Textentry _
1283 | View
1284 | LinkNav _ ->
1285 let y = getanchory state.anchor in
1286 let y = min y (state.maxy - state.winh) in
1287 gotoxy state.x y;
1289 else (
1290 state.reprf ();
1291 state.reprf <- noreprf;
1295 let reshape ?(firsttime=false) w h =
1296 GlDraw.viewport ~x:0 ~y:0 ~w ~h;
1297 if not firsttime && nogeomcmds state.geomcmds
1298 then state.anchor <- getanchor ();
1300 state.winw <- w;
1301 let w = truncate (float w *. conf.zoom) in
1302 let w = max w 2 in
1303 state.winh <- h;
1304 setfontsize fstate.fontsize;
1305 GlMat.mode `modelview;
1306 GlMat.load_identity ();
1308 GlMat.mode `projection;
1309 GlMat.load_identity ();
1310 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1311 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1312 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
1314 let relx =
1315 if conf.zoom <= 1.0
1316 then 0.0
1317 else float state.x /. float state.w
1319 invalidate "geometry"
1320 (fun () ->
1321 state.w <- w;
1322 if not firsttime
1323 then state.x <- truncate (relx *. float w);
1324 let w =
1325 match conf.columns with
1326 | Csingle _ -> w
1327 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1328 | Csplit (c, _) -> w * c
1330 wcmd "geometry %d %d %d"
1331 w (stateh h) (FMTE.to_int conf.fitmodel)
1335 let enttext () =
1336 let len = String.length state.text in
1337 let x0 = if conf.leftscroll then vscrollw () else 0 in
1338 let drawstring s =
1339 let hscrollh =
1340 match state.mode with
1341 | Textentry _ | View | LinkNav _ ->
1342 let h, _, _ = state.uioh#scrollpw in
1344 | Birdseye _ -> 0
1346 let rect x w =
1347 filledrect x (float (state.winh - (fstate.fontsize + 4) - hscrollh))
1348 (x+.w) (float (state.winh - hscrollh))
1351 let w = float (state.winw - 1 - vscrollw ()) in
1352 if state.progress >= 0.0 && state.progress < 1.0
1353 then (
1354 GlDraw.color (0.3, 0.3, 0.3);
1355 let w1 = w *. state.progress in
1356 rect (float x0) w1;
1357 GlDraw.color (0.0, 0.0, 0.0);
1358 rect (float x0+.w1) (float x0+.w-.w1)
1360 else (
1361 GlDraw.color (0.0, 0.0, 0.0);
1362 rect (float x0) w;
1365 GlDraw.color (1.0, 1.0, 1.0);
1366 drawstring fstate.fontsize
1367 (if conf.leftscroll then x0 + 2 else x0 + if len > 0 then 8 else 2)
1368 (state.winh - hscrollh - 5) s;
1370 let s =
1371 match state.mode with
1372 | Textentry ((prefix, text, _, _, _, _), _) ->
1373 let s =
1374 if len > 0
1375 then
1376 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1377 else
1378 Printf.sprintf "%s%s_" prefix text
1382 | Birdseye _
1383 | View
1384 | LinkNav _ -> state.text
1386 let s =
1387 if state.newerrmsgs
1388 then (
1389 if not (istextentry state.mode) && state.uioh#eformsgs
1390 then
1391 let s1 = "(press 'e' to review error messasges)" in
1392 if nonemptystr s then s ^ " " ^ s1 else s1
1393 else s
1395 else s
1397 if nonemptystr s
1398 then drawstring s
1401 let gctiles () =
1402 let len = Queue.length state.tilelru in
1403 let layout = lazy (
1404 match state.throttle with
1405 | None ->
1406 if conf.preload
1407 then preloadlayout state.x state.y state.winw state.winh
1408 else state.layout
1409 | Some (layout, _, _) ->
1410 layout
1411 ) in
1412 let rec loop qpos =
1413 if state.memused <= conf.memlimit
1414 then ()
1415 else (
1416 if qpos < len
1417 then
1418 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1419 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1420 let (_, pw, ph, _) = getpagedim n in
1422 gen = state.gen
1423 && colorspace = conf.colorspace
1424 && angle = conf.angle
1425 && pagew = pw
1426 && pageh = ph
1427 && (
1428 let x = col*conf.tilew
1429 and y = row*conf.tileh in
1430 tilevisible (Lazy.force_val layout) n x y
1432 then Queue.push lruitem state.tilelru
1433 else (
1434 freepbo p;
1435 wcmd "freetile %s" (~> p);
1436 state.memused <- state.memused - s;
1437 state.uioh#infochanged Memused;
1438 Hashtbl.remove state.tilemap k;
1440 loop (qpos+1)
1443 loop 0
1446 let onpagerect pageno f =
1447 let b =
1448 match conf.columns with
1449 | Cmulti (_, b) -> b
1450 | Csingle b -> b
1451 | Csplit (_, b) -> b
1453 if pageno >= 0 && pageno < Array.length b
1454 then
1455 let (_, _, _, (_, w, h, _)) = b.(pageno) in
1456 f w h
1459 let gotopagexy1 wtmode pageno x y =
1460 let _,w1,h1,leftx = getpagedim pageno in
1461 let top = y /. (float h1) in
1462 let left = x /. (float w1) in
1463 let py, w, h = getpageywh pageno in
1464 let wh = state.winh in
1465 let x = left *. (float w) in
1466 let x = leftx + state.x + truncate x in
1467 let sx =
1468 if x < 0 || x >= state.winw
1469 then state.x - x
1470 else state.x
1472 let pdy = truncate (top *. float h) in
1473 let y' = py + pdy in
1474 let dy = y' - state.y in
1475 let sy =
1476 if x != state.x || not (dy > 0 && dy < wh)
1477 then (
1478 if conf.presentation
1479 then
1480 if abs (py - y') > wh
1481 then y'
1482 else py
1483 else y';
1485 else state.y
1487 if state.x != sx || state.y != sy
1488 then (
1489 let x, y =
1490 if wtmode
1491 then (
1492 let ww = state.winw in
1493 let qx = sx / ww
1494 and qy = pdy / wh in
1495 let x = qx * ww
1496 and y = py + qy * wh in
1497 let x = if -x + ww > w1 then -(w1-ww) else x
1498 and y' = if y + wh > state.maxy then state.maxy - wh else y in
1499 let y =
1500 if conf.presentation
1501 then
1502 if abs (py - y') > wh
1503 then y'
1504 else py
1505 else y';
1507 (x, y)
1509 else (sx, sy)
1511 gotoxy_and_clear_text x y;
1513 else gotoxy_and_clear_text state.x state.y;
1516 let gotopagexy wtmode pageno x y =
1517 match state.mode with
1518 | Birdseye _ -> gotopage pageno 0.0
1519 | Textentry _
1520 | View
1521 | LinkNav _ -> gotopagexy1 wtmode pageno x y
1524 let getpassword () =
1525 let passcmd = getenvwithdef "LLPP_ASKPASS" conf.passcmd in
1526 if emptystr passcmd
1527 then E.s
1528 else getcmdoutput
1529 (fun s ->
1530 impmsg "error getting password: %s" s;
1531 dolog "%s" s) passcmd;
1534 let pgoto opaque pageno x y =
1535 let pdimno = getpdimno pageno in
1536 let x, y = project opaque pageno pdimno x y in
1537 gotopagexy false pageno x y;
1540 let act cmds =
1541 (* dolog "%S" cmds; *)
1542 let spl = splitatchar cmds ' ' in
1543 let scan s fmt f =
1544 try Scanf.sscanf s fmt f
1545 with exn ->
1546 dolog "error processing '%S': %s" cmds @@ exntos exn;
1547 exit 1
1549 let addoutline outline =
1550 match state.currently with
1551 | Outlining outlines ->
1552 state.currently <- Outlining (outline :: outlines)
1553 | Idle -> state.currently <- Outlining [outline]
1554 | Loading _
1555 | Tiling _ ->
1556 dolog "invalid outlining state";
1557 logcurrently state.currently
1559 match spl with
1560 | "clear", "" ->
1561 state.uioh#infochanged Pdim;
1562 state.pdims <- [];
1564 | "clearrects", "" ->
1565 state.rects <- state.rects1;
1566 G.postRedisplay "clearrects";
1568 | "continue", args ->
1569 let n = scan args "%u" (fun n -> n) in
1570 state.pagecount <- n;
1571 begin match state.currently with
1572 | Outlining l ->
1573 state.currently <- Idle;
1574 state.outlines <- Array.of_list (List.rev l)
1575 | Idle
1576 | Loading _
1577 | Tiling _ -> ()
1578 end;
1580 let cur, cmds = state.geomcmds in
1581 if emptystr cur
1582 then failwith "umpossible";
1584 begin match List.rev cmds with
1585 | [] ->
1586 state.geomcmds <- E.s, [];
1587 state.throttle <- None;
1588 represent ();
1589 | (s, f) :: rest ->
1590 f ();
1591 state.geomcmds <- s, List.rev rest;
1592 end;
1593 if conf.maxwait = None && not !wtmode
1594 then G.postRedisplay "continue";
1596 | "msg", args ->
1597 showtext ' ' args
1599 | "vmsg", args ->
1600 if conf.verbose
1601 then showtext ' ' args
1603 | "emsg", args ->
1604 Buffer.add_string state.errmsgs args;
1605 state.newerrmsgs <- true;
1606 G.postRedisplay "error message"
1608 | "progress", args ->
1609 let progress, text =
1610 scan args "%f %n"
1611 (fun f pos ->
1612 f, String.sub args pos (String.length args - pos))
1614 state.text <- text;
1615 state.progress <- progress;
1616 G.postRedisplay "progress"
1618 | "firstmatch", args ->
1619 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1620 scan args "%u %d %f %f %f %f %f %f %f %f"
1621 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1622 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1624 let y = (getpagey pageno) + truncate y0 in
1625 let x =
1626 if conf.zoom > 1.0
1627 then state.winw/2
1628 else state.x
1630 addnav ();
1631 gotoxy x y;
1632 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1633 state.rects1 <- [pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)]
1635 | "match", args ->
1636 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1637 scan args "%u %d %f %f %f %f %f %f %f %f"
1638 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1639 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1641 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1642 state.rects1 <-
1643 (pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1645 | "page", args ->
1646 let pageopaques, t = scan args "%s %f" (fun p t -> p, t) in
1647 let pageopaque = ~< pageopaques in
1648 begin match state.currently with
1649 | Loading (l, gen) ->
1650 vlog "page %d took %f sec" l.pageno t;
1651 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1652 begin match state.throttle with
1653 | None ->
1654 let preloadedpages =
1655 if conf.preload
1656 then preloadlayout state.x state.y state.winw state.winh
1657 else state.layout
1659 let evict () =
1660 let set =
1661 List.fold_left (fun s l -> IntSet.add l.pageno s)
1662 IntSet.empty preloadedpages
1664 let evictedpages =
1665 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1666 if not (IntSet.mem pageno set)
1667 then (
1668 wcmd "freepage %s" (~> opaque);
1669 key :: accu
1671 else accu
1672 ) state.pagemap []
1674 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1676 evict ();
1677 state.currently <- Idle;
1678 if gen = state.gen
1679 then (
1680 tilepage l.pageno pageopaque state.layout;
1681 load state.layout;
1682 load preloadedpages;
1683 let visible = pagevisible state.layout l.pageno in
1684 if visible
1685 then (
1686 match state.mode with
1687 | LinkNav (Ltnotready (pageno, dir)) ->
1688 if pageno = l.pageno
1689 then (
1690 let link =
1691 let ld =
1692 if dir = 0
1693 then LDfirstvisible (l.pagex, l.pagey, dir)
1694 else (
1695 if dir > 0 then LDfirst else LDlast
1698 findlink pageopaque ld
1700 match link with
1701 | Lnotfound -> ()
1702 | Lfound n ->
1703 showlinktype (getlink pageopaque n);
1704 state.mode <- LinkNav (Ltexact (l.pageno, n))
1706 | LinkNav (Ltgendir _)
1707 | LinkNav (Ltexact _)
1708 | View
1709 | Birdseye _
1710 | Textentry _ -> ()
1713 if visible && layoutready state.layout
1714 then (
1715 G.postRedisplay "page";
1719 | Some (layout, _, _) ->
1720 state.currently <- Idle;
1721 tilepage l.pageno pageopaque layout;
1722 load state.layout
1723 end;
1725 | Idle
1726 | Tiling _
1727 | Outlining _ ->
1728 dolog "Inconsistent loading state";
1729 logcurrently state.currently;
1730 exit 1
1733 | "tile" , args ->
1734 let (x, y, opaques, size, t) =
1735 scan args "%u %u %s %u %f"
1736 (fun x y p size t -> (x, y, p, size, t))
1738 let opaque = ~< opaques in
1739 begin match state.currently with
1740 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1741 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1743 unmappbo opaque;
1744 if tilew != conf.tilew || tileh != conf.tileh
1745 then (
1746 wcmd "freetile %s" (~> opaque);
1747 state.currently <- Idle;
1748 load state.layout;
1750 else (
1751 puttileopaque l col row gen cs angle opaque size t;
1752 state.memused <- state.memused + size;
1753 state.uioh#infochanged Memused;
1754 gctiles ();
1755 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1756 opaque, size) state.tilelru;
1758 let layout =
1759 match state.throttle with
1760 | None -> state.layout
1761 | Some (layout, _, _) -> layout
1764 state.currently <- Idle;
1765 if gen = state.gen
1766 && conf.colorspace = cs
1767 && conf.angle = angle
1768 && tilevisible layout l.pageno x y
1769 then conttiling l.pageno pageopaque;
1771 begin match state.throttle with
1772 | None ->
1773 preload state.layout;
1774 if gen = state.gen
1775 && conf.colorspace = cs
1776 && conf.angle = angle
1777 && tilevisible state.layout l.pageno x y
1778 && (not !wtmode || layoutready state.layout)
1779 then G.postRedisplay "tile nothrottle";
1781 | Some (layout, y, _) ->
1782 let ready = layoutready layout in
1783 if ready
1784 then (
1785 state.y <- y;
1786 state.layout <- layout;
1787 state.throttle <- None;
1788 G.postRedisplay "throttle";
1790 else load layout;
1791 end;
1794 | Idle
1795 | Loading _
1796 | Outlining _ ->
1797 dolog "Inconsistent tiling state";
1798 logcurrently state.currently;
1799 exit 1
1802 | "pdim", args ->
1803 let (n, w, h, _) as pdim =
1804 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1806 let pdim =
1807 match conf.fitmodel with
1808 | FitWidth -> pdim
1809 | FitPage | FitProportional ->
1810 match conf.columns with
1811 | Csplit _ -> (n, w, h, 0)
1812 | Csingle _ | Cmulti _ -> pdim
1814 state.uioh#infochanged Pdim;
1815 state.pdims <- pdim :: state.pdims
1817 | "o", args ->
1818 let (l, n, t, h, pos) =
1819 scan args "%u %u %d %u %n"
1820 (fun l n t h pos -> l, n, t, h, pos)
1822 let s = String.sub args pos (String.length args - pos) in
1823 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
1825 | "ou", args ->
1826 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
1827 let s = String.sub args pos len in
1828 let pos2 = pos + len + 1 in
1829 let uri = String.sub args pos2 (String.length args - pos2) in
1830 addoutline (s, l, Ouri uri)
1832 | "on", args ->
1833 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
1834 let s = String.sub args pos (String.length args - pos) in
1835 addoutline (s, l, Onone)
1837 | "a", args ->
1838 let (n, l, t) =
1839 scan args "%u %d %d" (fun n l t -> n, l, t)
1841 state.reprf <- (fun () -> gotopagexy !wtmode n (float l) (float t))
1843 | "info", args ->
1844 let pos = nindex args '\t' in
1845 let s =
1846 if pos >= 0
1847 then
1848 if substratis args 0 "Title"
1849 then
1850 let s = String.sub args (pos+1) @@ String.length args - pos - 1 in
1851 conf.title <- s;
1852 Wsi.settitle s;
1853 args
1854 else
1855 if substratis args 0 "CreationDate"
1856 then (
1857 if String.length args >= pos + 7
1858 && args.[pos+1] = 'D' && args.[pos+2] = ':'
1859 then
1860 let b = Buffer.create 18 in
1861 Buffer.add_string b "CreationDate\t";
1862 let sub p l c =
1864 Buffer.add_substring b args (pos+p+1) l;
1865 Buffer.add_char b c;
1866 with exn -> Buffer.add_string b @@ Printexc.to_string exn
1868 sub 2 4 '/';
1869 sub 6 2 '/';
1870 sub 8 2 ' ';
1871 sub 10 2 ':';
1872 sub 12 2 ':';
1873 sub 14 2 ' ';
1874 Buffer.add_char b '[';
1875 Buffer.add_substring b args (pos+1)
1876 (String.length args - pos - 1);
1877 Buffer.add_char b ']';
1878 Buffer.contents b
1879 else args
1881 else args
1882 else args
1884 state.docinfo <- (1, s) :: state.docinfo
1886 | "infoend", "" ->
1887 state.uioh#infochanged Docinfo;
1888 state.docinfo <- List.rev state.docinfo
1890 | "pass", args ->
1891 if args = "fail"
1892 then Wsi.settitle "Wrong password";
1893 let password = getpassword () in
1894 if emptystr password
1895 then error "document is password protected"
1896 else opendoc state.path password
1898 | _ ->
1899 error "unknown cmd `%S'" cmds
1902 let onhist cb =
1903 let rc = cb.rc in
1904 let action = function
1905 | HCprev -> cbget cb ~-1
1906 | HCnext -> cbget cb 1
1907 | HCfirst -> cbget cb ~-(cb.rc)
1908 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1909 and cancel () = cb.rc <- rc
1910 in (action, cancel)
1913 let search pattern forward =
1914 match conf.columns with
1915 | Csplit _ -> impmsg "searching does not work properly in split columns mode"
1916 | Csingle _
1917 | Cmulti _ ->
1918 if nonemptystr pattern
1919 then
1920 let pn, py =
1921 match state.layout with
1922 | [] -> 0, 0
1923 | l :: _ ->
1924 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1926 wcmd "search %d %d %d %d,%s\000"
1927 (btod conf.icase) pn py (btod forward) pattern;
1930 let intentry text key =
1931 let text =
1932 if key >= 32 && key < 127
1933 then
1934 let c = Char.chr key in
1935 match c with
1936 | '0' .. '9' -> addchar text c
1937 | _ ->
1938 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1939 text
1940 else (
1941 state.text <- Printf.sprintf "invalid key (%d)" key;
1942 text
1945 TEcont text
1948 let linknact f s =
1949 if nonemptystr s
1950 then (
1951 let n =
1952 let l = String.length s in
1953 let rec loop pos n = if pos = l then n else
1954 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
1955 loop (pos+1) (n*26 + m)
1956 in loop 0 0
1958 let rec loop n = function
1959 | [] -> ()
1960 | l :: rest ->
1961 match getopaque l.pageno with
1962 | None -> loop n rest
1963 | Some opaque ->
1964 let m = getlinkcount opaque in
1965 if n < m
1966 then (
1967 let under = getlink opaque n in
1968 f under
1970 else loop (n-m) rest
1972 loop n state.layout;
1976 let linknentry text key =
1977 if key >= 32 && key < 127
1978 then
1979 let text = addchar text (Char.chr key) in
1980 linknact (fun under -> state.text <- undertext under) text;
1981 TEcont text
1982 else (
1983 state.text <- Printf.sprintf "invalid key %d" key;
1984 TEcont text
1988 let textentry text key =
1989 if Wsi.isspecialkey key
1990 then TEcont text
1991 else TEcont (text ^ toutf8 key)
1994 let reqlayout angle fitmodel =
1995 match state.throttle with
1996 | None ->
1997 if nogeomcmds state.geomcmds
1998 then state.anchor <- getanchor ();
1999 conf.angle <- angle mod 360;
2000 if conf.angle != 0
2001 then (
2002 match state.mode with
2003 | LinkNav _ -> state.mode <- View
2004 | Birdseye _
2005 | Textentry _
2006 | View -> ()
2008 conf.fitmodel <- fitmodel;
2009 invalidate "reqlayout"
2010 (fun () ->
2011 wcmd "reqlayout %d %d %d"
2012 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
2014 | _ -> ()
2017 let settrim trimmargins trimfuzz =
2018 if nogeomcmds state.geomcmds
2019 then state.anchor <- getanchor ();
2020 conf.trimmargins <- trimmargins;
2021 conf.trimfuzz <- trimfuzz;
2022 let x0, y0, x1, y1 = trimfuzz in
2023 invalidate "settrim"
2024 (fun () ->
2025 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2026 flushpages ();
2029 let setzoom zoom =
2030 match state.throttle with
2031 | None ->
2032 let zoom = max 0.0001 zoom in
2033 if zoom <> conf.zoom
2034 then (
2035 state.prevzoom <- (conf.zoom, state.x);
2036 conf.zoom <- zoom;
2037 reshape state.winw state.winh;
2038 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2041 | Some (layout, y, started) ->
2042 let time =
2043 match conf.maxwait with
2044 | None -> 0.0
2045 | Some t -> t
2047 let dt = now () -. started in
2048 if dt > time
2049 then (
2050 state.y <- y;
2051 load layout;
2055 let pivotzoom ?(vw=min state.w state.winw)
2056 ?(vh=min (state.maxy-state.y) state.winh)
2057 ?(x=vw/2) ?(y=vh/2) zoom =
2058 let w = float state.w /. zoom in
2059 let hw = w /. 2.0 in
2060 let ratio = float vh /. float vw in
2061 let hh = hw *. ratio in
2062 let x0 = if zoom < 1.0 then 0.0 else float x -. hw in
2063 let y0 = float y -. hh in
2064 gotoxy (state.x - truncate x0) (state.y + truncate y0);
2065 setzoom zoom;
2068 let pivotzoom ?vw ?vh ?x ?y zoom =
2069 if nogeomcmds state.geomcmds then pivotzoom ?vw ?vh ?x ?y zoom
2072 let setcolumns mode columns coverA coverB =
2073 state.prevcolumns <- Some (conf.columns, conf.zoom);
2074 if columns < 0
2075 then (
2076 if isbirdseye mode
2077 then impmsg "split mode doesn't work in bird's eye"
2078 else (
2079 conf.columns <- Csplit (-columns, E.a);
2080 state.x <- 0;
2081 conf.zoom <- 1.0;
2084 else (
2085 if columns < 2
2086 then (
2087 conf.columns <- Csingle E.a;
2088 state.x <- 0;
2089 setzoom 1.0;
2091 else (
2092 conf.columns <- Cmulti ((columns, coverA, coverB), E.a);
2093 conf.zoom <- 1.0;
2096 reshape state.winw state.winh;
2099 let resetmstate () =
2100 state.mstate <- Mnone;
2101 Wsi.setcursor Wsi.CURSOR_INHERIT;
2104 let enterbirdseye () =
2105 let zoom = float conf.thumbw /. float state.winw in
2106 let birdseyepageno =
2107 let cy = state.winh / 2 in
2108 let fold = function
2109 | [] -> 0
2110 | l :: rest ->
2111 let rec fold best = function
2112 | [] -> best.pageno
2113 | l :: rest ->
2114 let d = cy - (l.pagedispy + l.pagevh/2)
2115 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2116 if abs d < abs dbest
2117 then fold l rest
2118 else best.pageno
2119 in fold l rest
2121 fold state.layout
2123 state.mode <- Birdseye (
2124 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2126 resetmstate ();
2127 conf.zoom <- zoom;
2128 conf.presentation <- false;
2129 conf.interpagespace <- 10;
2130 conf.hlinks <- false;
2131 conf.fitmodel <- FitPage;
2132 state.x <- 0;
2133 conf.maxwait <- None;
2134 conf.columns <- (
2135 match conf.beyecolumns with
2136 | Some c ->
2137 conf.zoom <- 1.0;
2138 Cmulti ((c, 0, 0), E.a)
2139 | None -> Csingle E.a
2141 if conf.verbose
2142 then
2143 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2144 (100.0*.zoom)
2145 else
2146 state.text <- E.s
2148 reshape state.winw state.winh;
2151 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2152 state.mode <- View;
2153 conf.zoom <- c.zoom;
2154 conf.presentation <- c.presentation;
2155 conf.interpagespace <- c.interpagespace;
2156 conf.maxwait <- c.maxwait;
2157 conf.hlinks <- c.hlinks;
2158 conf.fitmodel <- c.fitmodel;
2159 conf.beyecolumns <- (
2160 match conf.columns with
2161 | Cmulti ((c, _, _), _) -> Some c
2162 | Csingle _ -> None
2163 | Csplit _ -> failwith "leaving bird's eye split mode"
2165 conf.columns <- (
2166 match c.columns with
2167 | Cmulti (c, _) -> Cmulti (c, E.a)
2168 | Csingle _ -> Csingle E.a
2169 | Csplit (c, _) -> Csplit (c, E.a)
2171 if conf.verbose
2172 then
2173 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2174 (100.0*.conf.zoom)
2176 reshape state.winw state.winh;
2177 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2178 state.x <- leftx;
2181 let togglebirdseye () =
2182 match state.mode with
2183 | Birdseye vals -> leavebirdseye vals true
2184 | View -> enterbirdseye ()
2185 | Textentry _
2186 | LinkNav _ -> ()
2189 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2190 let pageno = max 0 (pageno - incr) in
2191 let rec loop = function
2192 | [] -> gotopage1 pageno 0
2193 | l :: _ when l.pageno = pageno ->
2194 if l.pagedispy >= 0 && l.pagey = 0
2195 then G.postRedisplay "upbirdseye"
2196 else gotopage1 pageno 0
2197 | _ :: rest -> loop rest
2199 loop state.layout;
2200 state.text <- E.s;
2201 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2204 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2205 let pageno = min (state.pagecount - 1) (pageno + incr) in
2206 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2207 let rec loop = function
2208 | [] ->
2209 let y, h = getpageyh pageno in
2210 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2211 gotoxy state.x (clamp dy)
2212 | l :: _ when l.pageno = pageno ->
2213 if l.pagevh != l.pageh
2214 then gotoxy state.x (clamp (l.pageh - l.pagevh + conf.interpagespace))
2215 else G.postRedisplay "downbirdseye"
2216 | _ :: rest -> loop rest
2218 loop state.layout;
2219 state.text <- E.s;
2222 let optentry mode _ key =
2223 let btos b = if b then "on" else "off" in
2224 if key >= 32 && key < 127
2225 then
2226 let c = Char.chr key in
2227 match c with
2228 | 's' ->
2229 let ondone s =
2230 try conf.scrollstep <- int_of_string s with exn ->
2231 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2233 TEswitch ("scroll step: ", E.s, None, intentry, ondone, true)
2235 | 'A' ->
2236 let ondone s =
2238 conf.autoscrollstep <- boundastep state.winh (int_of_string s);
2239 if state.autoscroll <> None
2240 then state.autoscroll <- Some conf.autoscrollstep
2241 with exn ->
2242 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2244 TEswitch ("auto scroll step: ", E.s, None, intentry, ondone, true)
2246 | 'C' ->
2247 let ondone s =
2249 let n, a, b = multicolumns_of_string s in
2250 setcolumns mode n a b;
2251 with exn ->
2252 state.text <- Printf.sprintf "bad columns `%s': %s" s @@ exntos exn
2254 TEswitch ("columns: ", E.s, None, textentry, ondone, true)
2256 | 'Z' ->
2257 let ondone s =
2259 let zoom = float (int_of_string s) /. 100.0 in
2260 pivotzoom zoom
2261 with exn ->
2262 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2264 TEswitch ("zoom: ", E.s, None, intentry, ondone, true)
2266 | 't' ->
2267 let ondone s =
2269 conf.thumbw <- bound (int_of_string s) 2 4096;
2270 state.text <-
2271 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2272 begin match mode with
2273 | Birdseye beye ->
2274 leavebirdseye beye false;
2275 enterbirdseye ();
2276 | Textentry _
2277 | View
2278 | LinkNav _ -> ();
2280 with exn ->
2281 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2283 TEswitch ("thumbnail width: ", E.s, None, intentry, ondone, true)
2285 | 'R' ->
2286 let ondone s =
2287 match int_of_string s with
2288 | angle -> reqlayout angle conf.fitmodel
2289 | exception exn ->
2290 state.text <-
2291 Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2293 TEswitch ("rotation: ", E.s, None, intentry, ondone, true)
2295 | 'i' ->
2296 conf.icase <- not conf.icase;
2297 TEdone ("case insensitive search " ^ (btos conf.icase))
2299 | 'p' ->
2300 conf.preload <- not conf.preload;
2301 gotoxy state.x state.y;
2302 TEdone ("preload " ^ (btos conf.preload))
2304 | 'v' ->
2305 conf.verbose <- not conf.verbose;
2306 TEdone ("verbose " ^ (btos conf.verbose))
2308 | 'd' ->
2309 conf.debug <- not conf.debug;
2310 TEdone ("debug " ^ (btos conf.debug))
2312 | 'h' ->
2313 conf.maxhfit <- not conf.maxhfit;
2314 state.maxy <- calcheight ();
2315 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2317 | 'c' ->
2318 conf.crophack <- not conf.crophack;
2319 TEdone ("crophack " ^ btos conf.crophack)
2321 | 'a' ->
2322 let s =
2323 match conf.maxwait with
2324 | None ->
2325 conf.maxwait <- Some infinity;
2326 "always wait for page to complete"
2327 | Some _ ->
2328 conf.maxwait <- None;
2329 "show placeholder if page is not ready"
2331 TEdone s
2333 | 'f' ->
2334 conf.underinfo <- not conf.underinfo;
2335 TEdone ("underinfo " ^ btos conf.underinfo)
2337 | 'P' ->
2338 conf.savebmarks <- not conf.savebmarks;
2339 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2341 | 'S' ->
2342 let ondone s =
2344 let pageno, py =
2345 match state.layout with
2346 | [] -> 0, 0
2347 | l :: _ ->
2348 l.pageno, l.pagey
2350 conf.interpagespace <- int_of_string s;
2351 docolumns conf.columns;
2352 state.maxy <- calcheight ();
2353 let y = getpagey pageno in
2354 gotoxy state.x (y + py)
2355 with exn ->
2356 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2358 TEswitch ("vertical margin: ", E.s, None, intentry, ondone, true)
2360 | 'l' ->
2361 let fm =
2362 match conf.fitmodel with
2363 | FitProportional -> FitWidth
2364 | FitWidth | FitPage -> FitProportional
2366 reqlayout conf.angle fm;
2367 TEdone ("proportional display " ^ btos (fm == FitProportional))
2369 | 'T' ->
2370 settrim (not conf.trimmargins) conf.trimfuzz;
2371 TEdone ("trim margins " ^ btos conf.trimmargins)
2373 | 'I' ->
2374 conf.invert <- not conf.invert;
2375 TEdone ("invert colors " ^ btos conf.invert)
2377 | 'x' ->
2378 let ondone s =
2379 cbput state.hists.sel s;
2380 conf.selcmd <- s;
2382 TEswitch ("selection command: ", E.s, Some (onhist state.hists.sel),
2383 textentry, ondone, true)
2385 | 'M' ->
2386 if conf.pax == None
2387 then conf.pax <- Some (ref (0.0, 0, 0))
2388 else conf.pax <- None;
2389 TEdone ("PAX " ^ btos (conf.pax != None))
2391 | _ ->
2392 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2393 TEstop
2394 else
2395 TEcont state.text
2398 class type lvsource = object
2399 method getitemcount : int
2400 method getitem : int -> (string * int)
2401 method hasaction : int -> bool
2402 method exit :
2403 uioh:uioh ->
2404 cancel:bool ->
2405 active:int ->
2406 first:int ->
2407 pan:int ->
2408 uioh option
2409 method getactive : int
2410 method getfirst : int
2411 method getpan : int
2412 method getminfo : (int * int) array
2413 end;;
2415 class virtual lvsourcebase = object
2416 val mutable m_active = 0
2417 val mutable m_first = 0
2418 val mutable m_pan = 0
2419 method getactive = m_active
2420 method getfirst = m_first
2421 method getpan = m_pan
2422 method getminfo : (int * int) array = E.a
2423 end;;
2425 let textentrykeyboard
2426 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2427 state.text <- E.s;
2428 let key = Wsi.keypadtodigitkey key in
2429 let enttext te =
2430 state.mode <- Textentry (te, onleave);
2431 enttext ();
2432 G.postRedisplay "textentrykeyboard enttext";
2434 let histaction cmd =
2435 match opthist with
2436 | None -> ()
2437 | Some (action, _) ->
2438 state.mode <- Textentry (
2439 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
2441 G.postRedisplay "textentry histaction"
2443 match key with
2444 | @backspace ->
2445 if emptystr text && cancelonempty
2446 then (
2447 onleave Cancel;
2448 G.postRedisplay "textentrykeyboard after cancel";
2450 else
2451 let s = withoutlastutf8 text in
2452 enttext (c, s, opthist, onkey, ondone, cancelonempty)
2454 | @enter | @kpenter ->
2455 ondone text;
2456 onleave Confirm;
2457 G.postRedisplay "textentrykeyboard after confirm"
2459 | @up | @kpup -> histaction HCprev
2460 | @down | @kpdown -> histaction HCnext
2461 | @home | @kphome -> histaction HCfirst
2462 | @jend | @kpend -> histaction HClast
2464 | @escape ->
2465 if emptystr text
2466 then (
2467 begin match opthist with
2468 | None -> ()
2469 | Some (_, onhistcancel) -> onhistcancel ()
2470 end;
2471 onleave Cancel;
2472 state.text <- E.s;
2473 G.postRedisplay "textentrykeyboard after cancel2"
2475 else (
2476 enttext (c, E.s, opthist, onkey, ondone, cancelonempty)
2479 | @delete | @kpdelete -> ()
2481 | _ when key != 0 && not (Wsi.isspecialkey key) ->
2482 begin match onkey text key with
2483 | TEdone text ->
2484 ondone text;
2485 onleave Confirm;
2486 G.postRedisplay "textentrykeyboard after confirm2";
2488 | TEcont text ->
2489 enttext (c, text, opthist, onkey, ondone, cancelonempty);
2491 | TEstop ->
2492 onleave Cancel;
2493 G.postRedisplay "textentrykeyboard after cancel3"
2495 | TEswitch te ->
2496 state.mode <- Textentry (te, onleave);
2497 G.postRedisplay "textentrykeyboard switch";
2498 end;
2500 | _ ->
2501 vlog "unhandled key %s" (Wsi.keyname key)
2504 let firstof first active =
2505 if first > active || abs (first - active) > fstate.maxrows - 1
2506 then max 0 (active - (fstate.maxrows/2))
2507 else first
2510 let calcfirst first active =
2511 if active > first
2512 then
2513 let rows = active - first in
2514 if rows > fstate.maxrows then active - fstate.maxrows else first
2515 else active
2518 let scrollph y maxy =
2519 let sh = float (maxy + state.winh) /. float state.winh in
2520 let sh = float state.winh /. sh in
2521 let sh = max sh (float conf.scrollh) in
2523 let percent = float y /. float maxy in
2524 let position = (float state.winh -. sh) *. percent in
2526 let position =
2527 if position +. sh > float state.winh
2528 then float state.winh -. sh
2529 else position
2531 position, sh;
2534 let adderrmsg src msg =
2535 Buffer.add_string state.errmsgs msg;
2536 state.newerrmsgs <- true;
2537 G.postRedisplay src
2540 let adderrfmt src fmt =
2541 Format.ksprintf (fun s -> adderrmsg src s) fmt;
2544 let coe s = (s :> uioh);;
2546 class listview ~zebra ~helpmode ~(source:lvsource) ~trusted ~modehash =
2547 object (self)
2548 val m_pan = source#getpan
2549 val m_first = source#getfirst
2550 val m_active = source#getactive
2551 val m_qsearch = E.s
2552 val m_prev_uioh = state.uioh
2554 method private elemunder y =
2555 if y < 0
2556 then None
2557 else
2558 let n = y / (fstate.fontsize+1) in
2559 if m_first + n < source#getitemcount
2560 then (
2561 if source#hasaction (m_first + n)
2562 then Some (m_first + n)
2563 else None
2565 else None
2567 method display =
2568 Gl.enable `blend;
2569 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
2570 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2571 filledrect 0. 0. (float state.winw) (float state.winh);
2572 GlDraw.color (1., 1., 1.);
2573 Gl.enable `texture_2d;
2574 let fs = fstate.fontsize in
2575 let nfs = fs + 1 in
2576 let hw = state.winw/3 in
2577 let ww = fstate.wwidth in
2578 let tabw = 17.0*.ww in
2579 let itemcount = source#getitemcount in
2580 let minfo = source#getminfo in
2581 if conf.leftscroll
2582 then (
2583 GlMat.push ();
2584 GlMat.translate ~x:(float conf.scrollbw) ();
2586 let x0 = 0.0 and x1 = float (state.winw - conf.scrollbw - 1) in
2587 let rec loop row =
2588 if (row - m_first) > fstate.maxrows
2589 then ()
2590 else (
2591 if row >= 0 && row < itemcount
2592 then (
2593 let (s, level) = source#getitem row in
2594 let y = (row - m_first) * nfs in
2595 let x = 5.0 +. (float (level + m_pan)) *. ww in
2596 if helpmode
2597 then GlDraw.color
2598 (let c = if row land 1 = 0 then 1.0 else 0.92 in (c,c,c));
2600 if row = m_active
2601 then (
2602 Gl.disable `texture_2d;
2603 let alpha = if source#hasaction row then 0.9 else 0.3 in
2604 GlDraw.color (1., 1., 1.) ~alpha;
2605 linerect (x0 +. 1.) (float (y + 1)) (x1) (float (y + fs + 3));
2606 Gl.enable `texture_2d;
2608 let c =
2609 if zebra && row land 1 = 1
2610 then 0.8
2611 else 1.0
2613 GlDraw.color (c,c,c);
2614 let drawtabularstring s =
2615 let drawstr x s =
2616 let x' = truncate (x0 +. x) in
2617 let pos = nindex s '\000' in
2618 if pos = -1
2619 then drawstring1 fs x' (y+nfs) s
2620 else
2621 let s1 = String.sub s 0 pos
2622 and s2 = String.sub s (pos+1) (String.length s - pos - 1) in
2623 let rec e s =
2624 if emptystr s
2625 then s
2626 else
2627 let s' = withoutlastutf8 s in
2628 let s = s' ^ "@Uellipsis" in
2629 let w = measurestr fs s in
2630 if float x' +. w +. ww < float (hw + x')
2631 then s
2632 else e s'
2634 let s1 =
2635 if float x' +. ww +. measurestr fs s1 > float (hw + x')
2636 then e s1
2637 else s1
2639 ignore (drawstring1 fs x' (y+nfs) s1);
2640 drawstring1 fs (hw + x') (y+nfs) s2
2642 if trusted
2643 then
2644 let x = if helpmode && row > 0 then x +. ww else x in
2645 let tabpos = nindex s '\t' in
2646 if tabpos > 0
2647 then
2648 let len = String.length s - tabpos - 1 in
2649 let s1 = String.sub s 0 tabpos
2650 and s2 = String.sub s (tabpos + 1) len in
2651 let nx = drawstr x s1 in
2652 let sw = nx -. x in
2653 let x = x +. (max tabw sw) in
2654 drawstr x s2
2655 else
2656 let len = String.length s - 2 in
2657 if len > 0 && s.[0] = '\xc2' && s.[1] = '\xb7'
2658 then
2659 let s = String.sub s 2 len in
2660 let x = if not helpmode then x +. ww else x in
2661 GlDraw.color (1.2, 1.2, 1.2);
2662 let vinc = drawstring1 (fs+fs/4)
2663 (truncate (x -. ww)) (y+nfs) s in
2664 GlDraw.color (1., 1., 1.);
2665 vinc +. (float fs *. 0.8)
2666 else
2667 drawstr x s
2668 else
2669 drawstr x s
2671 ignore (drawtabularstring s);
2672 loop (row+1)
2676 loop m_first;
2677 GlDraw.color (1.0, 1.0, 1.0) ~alpha:0.5;
2678 let xadj = 5.0 in
2679 let rec loop row =
2680 if (row - m_first) > fstate.maxrows
2681 then ()
2682 else (
2683 if row >= 0 && row < itemcount
2684 then (
2685 let (s, level) = source#getitem row in
2686 let pos0 = nindex s '\000' in
2687 let y = (row - m_first) * nfs in
2688 let x = float (level + m_pan) *. ww in
2689 let (first, last) = minfo.(row) in
2690 let prefix =
2691 if pos0 > 0 && first > pos0
2692 then String.sub s (pos0+1) (first-pos0-1)
2693 else String.sub s 0 first
2695 let suffix = String.sub s first (last - first) in
2696 let w1 = measurestr fstate.fontsize prefix in
2697 let w2 = measurestr fstate.fontsize suffix in
2698 let x = x +. if conf.leftscroll then xadj else 5.0 in
2699 let x = if pos0 > 0 && first > pos0 then x +. float hw else x in
2700 let x0 = x +. w1
2701 and y0 = float (y+2) in
2702 let x1 = x0 +. w2
2703 and y1 = float (y+fs+3) in
2704 filledrect x0 y0 x1 y1;
2705 loop (row+1)
2709 Gl.disable `texture_2d;
2710 if Array.length minfo > 0 then loop m_first;
2711 Gl.disable `blend;
2712 if conf.leftscroll
2713 then GlMat.pop ();
2715 method updownlevel incr =
2716 let len = source#getitemcount in
2717 let curlevel =
2718 if m_active >= 0 && m_active < len
2719 then snd (source#getitem m_active)
2720 else -1
2722 let rec flow i =
2723 if i = len then i-1 else if i = -1 then 0 else
2724 let _, l = source#getitem i in
2725 if l != curlevel then i else flow (i+incr)
2727 let active = flow m_active in
2728 let first = calcfirst m_first active in
2729 G.postRedisplay "outline updownlevel";
2730 {< m_active = active; m_first = first >}
2732 method private key1 key mask =
2733 let set1 active first qsearch =
2734 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2736 let search active pattern incr =
2737 let active = if active = -1 then m_first else active in
2738 let dosearch re =
2739 let rec loop n =
2740 if n >= 0 && n < source#getitemcount
2741 then (
2742 let s, _ = source#getitem n in
2743 match Str.search_forward re s 0 with
2744 | (exception Not_found) -> loop (n + incr)
2745 | _ -> Some n
2747 else None
2749 loop active
2751 let qpat = Str.quote pattern in
2752 match Str.regexp_case_fold qpat with
2753 | s -> dosearch s
2754 | exception exn ->
2755 adderrfmt "listview key1" "regexp_case_fold for `%S' failed: %S\n"
2756 qpat @@ Printexc.to_string exn;
2757 None
2759 let itemcount = source#getitemcount in
2760 let find start incr =
2761 let rec find i =
2762 if i = -1 || i = itemcount
2763 then -1
2764 else (
2765 if source#hasaction i
2766 then i
2767 else find (i + incr)
2770 find start
2772 let set active first =
2773 let first = bound first 0 (itemcount - fstate.maxrows) in
2774 state.text <- E.s;
2775 coe {< m_active = active; m_first = first; m_qsearch = E.s >}
2777 let navigate incr =
2778 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2779 let active, first =
2780 let incr1 = if incr > 0 then 1 else -1 in
2781 if isvisible m_first m_active
2782 then
2783 let next =
2784 let next = m_active + incr in
2785 let next =
2786 if next < 0 || next >= itemcount
2787 then -1
2788 else find next incr1
2790 if abs (m_active - next) > fstate.maxrows
2791 then -1
2792 else next
2794 if next = -1
2795 then
2796 let first = m_first + incr in
2797 let first = bound first 0 (itemcount - fstate.maxrows) in
2798 let next =
2799 let next = m_active + incr in
2800 let next = bound next 0 (itemcount - 1) in
2801 find next ~-incr1
2803 let active =
2804 if next = -1
2805 then m_active
2806 else (
2807 if isvisible first next
2808 then next
2809 else m_active
2812 active, first
2813 else
2814 let first = min next m_first in
2815 let first =
2816 if abs (next - first) > fstate.maxrows
2817 then first + incr
2818 else first
2820 next, first
2821 else
2822 let first = m_first + incr in
2823 let first = bound first 0 (itemcount - 1) in
2824 let active =
2825 let next = m_active + incr in
2826 let next = bound next 0 (itemcount - 1) in
2827 let next = find next incr1 in
2828 let active =
2829 if next = -1 || abs (m_active - first) > fstate.maxrows
2830 then (
2831 let active = if m_active = -1 then next else m_active in
2832 active
2834 else next
2836 if isvisible first active
2837 then active
2838 else -1
2840 active, first
2842 G.postRedisplay "listview navigate";
2843 set active first;
2845 match key with
2846 | (@r|@s) when Wsi.withctrl mask ->
2847 let incr = if key = @r then -1 else 1 in
2848 let active, first =
2849 match search (m_active + incr) m_qsearch incr with
2850 | None ->
2851 state.text <- m_qsearch ^ " [not found]";
2852 m_active, m_first
2853 | Some active ->
2854 state.text <- m_qsearch;
2855 active, firstof m_first active
2857 G.postRedisplay "listview ctrl-r/s";
2858 set1 active first m_qsearch;
2860 | @insert when Wsi.withctrl mask ->
2861 if m_active >= 0 && m_active < source#getitemcount
2862 then (
2863 let s, _ = source#getitem m_active in
2864 selstring s;
2866 coe self
2868 | @backspace ->
2869 if emptystr m_qsearch
2870 then coe self
2871 else (
2872 let qsearch = withoutlastutf8 m_qsearch in
2873 if emptystr qsearch
2874 then (
2875 state.text <- E.s;
2876 G.postRedisplay "listview empty qsearch";
2877 set1 m_active m_first E.s;
2879 else
2880 let active, first =
2881 match search m_active qsearch ~-1 with
2882 | None ->
2883 state.text <- qsearch ^ " [not found]";
2884 m_active, m_first
2885 | Some active ->
2886 state.text <- qsearch;
2887 active, firstof m_first active
2889 G.postRedisplay "listview backspace qsearch";
2890 set1 active first qsearch
2893 | key when (key != 0 && not (Wsi.isspecialkey key)) ->
2894 let pattern = m_qsearch ^ toutf8 key in
2895 let active, first =
2896 match search m_active pattern 1 with
2897 | None ->
2898 state.text <- pattern ^ " [not found]";
2899 m_active, m_first
2900 | Some active ->
2901 state.text <- pattern;
2902 active, firstof m_first active
2904 G.postRedisplay "listview qsearch add";
2905 set1 active first pattern;
2907 | @escape ->
2908 state.text <- E.s;
2909 if emptystr m_qsearch
2910 then (
2911 G.postRedisplay "list view escape";
2912 let mx, my = state.mpos in
2913 updateunder mx my;
2914 begin
2915 match
2916 source#exit ~uioh:(coe self)
2917 ~cancel:true ~active:m_active ~first:m_first ~pan:m_pan
2918 with
2919 | None -> m_prev_uioh
2920 | Some uioh -> uioh
2923 else (
2924 G.postRedisplay "list view kill qsearch";
2925 coe {< m_qsearch = E.s >}
2928 | @enter | @kpenter ->
2929 state.text <- E.s;
2930 let self = {< m_qsearch = E.s >} in
2931 let opt =
2932 G.postRedisplay "listview enter";
2933 if m_active >= 0 && m_active < source#getitemcount
2934 then (
2935 source#exit ~uioh:(coe self) ~cancel:false
2936 ~active:m_active ~first:m_first ~pan:m_pan;
2938 else (
2939 source#exit ~uioh:(coe self) ~cancel:true
2940 ~active:m_active ~first:m_first ~pan:m_pan;
2943 begin match opt with
2944 | None -> m_prev_uioh
2945 | Some uioh -> uioh
2948 | @delete | @kpdelete ->
2949 coe self
2951 | @up | @kpup -> navigate ~-1
2952 | @down | @kpdown -> navigate 1
2953 | @prior | @kpprior -> navigate ~-(fstate.maxrows)
2954 | @next | @kpnext -> navigate fstate.maxrows
2956 | @right | @kpright ->
2957 state.text <- E.s;
2958 G.postRedisplay "listview right";
2959 coe {< m_pan = m_pan - 1 >}
2961 | @left | @kpleft ->
2962 state.text <- E.s;
2963 G.postRedisplay "listview left";
2964 coe {< m_pan = m_pan + 1 >}
2966 | @home | @kphome ->
2967 let active = find 0 1 in
2968 G.postRedisplay "listview home";
2969 set active 0;
2971 | @jend | @kpend ->
2972 let first = max 0 (itemcount - fstate.maxrows) in
2973 let active = find (itemcount - 1) ~-1 in
2974 G.postRedisplay "listview end";
2975 set active first;
2977 | key when (key = 0 || Wsi.isspecialkey key) ->
2978 coe self
2980 | _ ->
2981 dolog "listview unknown key %#x" key; coe self
2983 method key key mask =
2984 match state.mode with
2985 | Textentry te -> textentrykeyboard key mask te; coe self
2986 | Birdseye _
2987 | View
2988 | LinkNav _ -> self#key1 key mask
2990 method button button down x y _ =
2991 let opt =
2992 match button with
2993 | 1 when vscrollhit x ->
2994 G.postRedisplay "listview scroll";
2995 if down
2996 then
2997 let _, position, sh = self#scrollph in
2998 if y > truncate position && y < truncate (position +. sh)
2999 then (
3000 state.mstate <- Mscrolly;
3001 Some (coe self)
3003 else
3004 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3005 let first = truncate (s *. float source#getitemcount) in
3006 let first = min source#getitemcount first in
3007 Some (coe {< m_first = first; m_active = first >})
3008 else (
3009 state.mstate <- Mnone;
3010 Some (coe self);
3012 | 1 when down ->
3013 begin match self#elemunder y with
3014 | Some n ->
3015 G.postRedisplay "listview click";
3016 source#exit ~uioh:(coe {< m_active = n >})
3017 ~cancel:false ~active:n ~first:m_first ~pan:m_pan
3018 | _ ->
3019 Some (coe self)
3021 | n when (n == 4 || n == 5) && not down ->
3022 let len = source#getitemcount in
3023 let first =
3024 if n = 5 && m_first + fstate.maxrows >= len
3025 then
3026 m_first
3027 else
3028 let first = m_first + (if n == 4 then -1 else 1) in
3029 bound first 0 (len - 1)
3031 G.postRedisplay "listview wheel";
3032 Some (coe {< m_first = first >})
3033 | n when (n = 6 || n = 7) && not down ->
3034 let inc = if n = 7 then -1 else 1 in
3035 G.postRedisplay "listview hwheel";
3036 Some (coe {< m_pan = m_pan + inc >})
3037 | _ ->
3038 Some (coe self)
3040 match opt with
3041 | None -> m_prev_uioh
3042 | Some uioh -> uioh
3044 method multiclick _ x y = self#button 1 true x y
3046 method motion _ y =
3047 match state.mstate with
3048 | Mscrolly ->
3049 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3050 let first = truncate (s *. float source#getitemcount) in
3051 let first = min source#getitemcount first in
3052 G.postRedisplay "listview motion";
3053 coe {< m_first = first; m_active = first >}
3054 | Msel _
3055 | Mpan _
3056 | Mscrollx
3057 | Mzoom _
3058 | Mzoomrect _
3059 | Mnone -> coe self
3061 method pmotion x y =
3062 if x < state.winw - conf.scrollbw
3063 then
3064 let n =
3065 match self#elemunder y with
3066 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3067 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3069 let o =
3070 if n != m_active
3071 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3072 else self
3074 coe o
3075 else (
3076 Wsi.setcursor Wsi.CURSOR_INHERIT;
3077 coe self
3080 method infochanged _ = ()
3082 method scrollpw = (0, 0.0, 0.0)
3083 method scrollph =
3084 let nfs = fstate.fontsize + 1 in
3085 let y = m_first * nfs in
3086 let itemcount = source#getitemcount in
3087 let maxi = max 0 (itemcount - fstate.maxrows) in
3088 let maxy = maxi * nfs in
3089 let p, h = scrollph y maxy in
3090 conf.scrollbw, p, h
3092 method modehash = modehash
3093 method eformsgs = false
3094 method alwaysscrolly = true
3095 end;;
3097 class outlinelistview ~zebra ~source =
3098 let settext autonarrow s =
3099 if autonarrow
3100 then
3101 let ss = source#statestr in
3102 state.text <-
3103 if emptystr ss
3104 then "[" ^ s ^ "]"
3105 else "{" ^ ss ^ "} [" ^ s ^ "]"
3106 else state.text <- s
3108 object (self)
3109 inherit listview
3110 ~zebra
3111 ~helpmode:false
3112 ~source:(source :> lvsource)
3113 ~trusted:false
3114 ~modehash:(findkeyhash conf "outline")
3115 as super
3117 val m_autonarrow = false
3119 method! key key mask =
3120 let maxrows =
3121 if emptystr state.text
3122 then fstate.maxrows
3123 else fstate.maxrows - 2
3125 let calcfirst first active =
3126 if active > first
3127 then
3128 let rows = active - first in
3129 if rows > maxrows then active - maxrows else first
3130 else active
3132 let navigate incr =
3133 let active = m_active + incr in
3134 let active = bound active 0 (source#getitemcount - 1) in
3135 let first = calcfirst m_first active in
3136 G.postRedisplay "outline navigate";
3137 coe {< m_active = active; m_first = first >}
3139 let navscroll first =
3140 let active =
3141 let dist = m_active - first in
3142 if dist < 0
3143 then first
3144 else (
3145 if dist < maxrows
3146 then m_active
3147 else first + maxrows
3150 G.postRedisplay "outline navscroll";
3151 coe {< m_first = first; m_active = active >}
3153 let ctrl = Wsi.withctrl mask in
3154 match key with
3155 | @a when ctrl ->
3156 let text =
3157 if m_autonarrow
3158 then (source#denarrow; E.s)
3159 else (
3160 let pattern = source#renarrow in
3161 if nonemptystr m_qsearch
3162 then (source#narrow m_qsearch; m_qsearch)
3163 else pattern
3166 settext (not m_autonarrow) text;
3167 G.postRedisplay "toggle auto narrowing";
3168 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
3170 | @slash when emptystr m_qsearch && not m_autonarrow ->
3171 settext true E.s;
3172 G.postRedisplay "toggle auto narrowing";
3173 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
3175 | @n when ctrl ->
3176 source#narrow m_qsearch;
3177 if not m_autonarrow
3178 then source#add_narrow_pattern m_qsearch;
3179 G.postRedisplay "outline ctrl-n";
3180 coe {< m_first = 0; m_active = 0 >}
3182 | @S when ctrl ->
3183 let active = source#calcactive (getanchor ()) in
3184 let first = firstof m_first active in
3185 G.postRedisplay "outline ctrl-s";
3186 coe {< m_first = first; m_active = active >}
3188 | @u when ctrl ->
3189 G.postRedisplay "outline ctrl-u";
3190 if m_autonarrow && nonemptystr m_qsearch
3191 then (
3192 ignore (source#renarrow);
3193 settext m_autonarrow E.s;
3194 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3196 else (
3197 source#del_narrow_pattern;
3198 let pattern = source#renarrow in
3199 let text =
3200 if emptystr pattern then E.s else "Narrowed to " ^ pattern
3202 settext m_autonarrow text;
3203 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3206 | @l when ctrl ->
3207 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3208 G.postRedisplay "outline ctrl-l";
3209 coe {< m_first = first >}
3211 | @tab when m_autonarrow ->
3212 if nonemptystr m_qsearch
3213 then (
3214 G.postRedisplay "outline list view tab";
3215 source#add_narrow_pattern m_qsearch;
3216 settext true E.s;
3217 coe {< m_qsearch = E.s >}
3219 else coe self
3221 | @escape when m_autonarrow ->
3222 if nonemptystr m_qsearch
3223 then source#add_narrow_pattern m_qsearch;
3224 super#key key mask
3226 | @enter | @kpenter when m_autonarrow ->
3227 if nonemptystr m_qsearch
3228 then source#add_narrow_pattern m_qsearch;
3229 super#key key mask
3231 | key when m_autonarrow && (not (Wsi.isspecialkey key)) ->
3232 let pattern = m_qsearch ^ toutf8 key in
3233 G.postRedisplay "outlinelistview autonarrow add";
3234 source#narrow pattern;
3235 settext true pattern;
3236 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3238 | key when m_autonarrow && key = @backspace ->
3239 if emptystr m_qsearch
3240 then coe self
3241 else
3242 let pattern = withoutlastutf8 m_qsearch in
3243 G.postRedisplay "outlinelistview autonarrow backspace";
3244 ignore (source#renarrow);
3245 source#narrow pattern;
3246 settext true pattern;
3247 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3249 | @up | @kpup when ctrl ->
3250 navscroll (max 0 (m_first - 1))
3252 | @down | @kpdown when ctrl ->
3253 navscroll (min (source#getitemcount - 1) (m_first + 1))
3255 | @up | @kpup -> navigate ~-1
3256 | @down | @kpdown -> navigate 1
3257 | @prior | @kpprior -> navigate ~-(fstate.maxrows)
3258 | @next | @kpnext -> navigate fstate.maxrows
3260 | @right | @kpright ->
3261 let o =
3262 if ctrl
3263 then (
3264 G.postRedisplay "outline ctrl right";
3265 {< m_pan = m_pan + 1 >}
3267 else self#updownlevel 1
3269 coe o
3271 | @left | @kpleft ->
3272 let o =
3273 if ctrl
3274 then (
3275 G.postRedisplay "outline ctrl left";
3276 {< m_pan = m_pan - 1 >}
3278 else self#updownlevel ~-1
3280 coe o
3282 | @home | @kphome ->
3283 G.postRedisplay "outline home";
3284 coe {< m_first = 0; m_active = 0 >}
3286 | @jend | @kpend ->
3287 let active = source#getitemcount - 1 in
3288 let first = max 0 (active - fstate.maxrows) in
3289 G.postRedisplay "outline end";
3290 coe {< m_active = active; m_first = first >}
3292 | _ -> super#key key mask
3293 end;;
3295 let genhistoutlines () =
3296 Config.gethist ()
3297 |> List.sort (fun (_, c1, _, _, _, _) (_, c2, _, _, _, _) ->
3298 compare c2.lastvisit c1.lastvisit)
3299 |> List.map
3300 (fun ((path, c, _, _, _, origin) as hist) ->
3301 let path = if nonemptystr origin then origin else path in
3302 let base = mbtoutf8 @@ Filename.basename path in
3303 (base ^ "\000" ^ c.title, 1, Ohistory hist)
3305 |> Array.of_list
3308 let gotohist (path, c, bookmarks, x, anchor, origin) =
3309 Config.save leavebirdseye;
3310 state.anchor <- anchor;
3311 state.bookmarks <- bookmarks;
3312 state.origin <- origin;
3313 state.x <- x;
3314 setconf conf c;
3315 let x0, y0, x1, y1 = conf.trimfuzz in
3316 wcmd "trimset %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1;
3317 reshape ~firsttime:true state.winw state.winh;
3318 opendoc path origin;
3319 setzoom c.zoom;
3322 let makecheckers () =
3323 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3324 following to say:
3325 converted by Issac Trotts. July 25, 2002 *)
3326 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3327 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3328 let id = GlTex.gen_texture () in
3329 GlTex.bind_texture ~target:`texture_2d id;
3330 GlPix.store (`unpack_alignment 1);
3331 GlTex.image2d image;
3332 List.iter (GlTex.parameter ~target:`texture_2d)
3333 [ `mag_filter `nearest; `min_filter `nearest ];
3337 let setcheckers enabled =
3338 match state.checkerstexid with
3339 | None ->
3340 if enabled then state.checkerstexid <- Some (makecheckers ())
3342 | Some checkerstexid ->
3343 if not enabled
3344 then (
3345 GlTex.delete_texture checkerstexid;
3346 state.checkerstexid <- None;
3350 let describe_location () =
3351 let fn = page_of_y state.y in
3352 let ln = page_of_y (state.y + state.winh - 1) in
3353 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
3354 let percent =
3355 if maxy <= 0
3356 then 100.
3357 else (100. *. (float state.y /. float maxy))
3359 if fn = ln
3360 then
3361 Printf.sprintf "page %d of %d [%.2f%%]"
3362 (fn+1) state.pagecount percent
3363 else
3364 Printf.sprintf
3365 "pages %d-%d of %d [%.2f%%]"
3366 (fn+1) (ln+1) state.pagecount percent
3369 let setpresentationmode v =
3370 let n = page_of_y state.y in
3371 state.anchor <- (n, 0.0, 1.0);
3372 conf.presentation <- v;
3373 if conf.fitmodel = FitPage
3374 then reqlayout conf.angle conf.fitmodel;
3375 represent ();
3378 let setbgcol (r, g, b) =
3379 let col =
3380 let r = r *. 255.0 |> truncate
3381 and g = g *. 255.0 |> truncate
3382 and b = b *. 255.0 |> truncate in
3383 r lsl 16 |> (lor) (g lsl 8) |> (lor) b
3385 Wsi.setwinbgcol col;
3388 let enterinfomode =
3389 let btos b = if b then "@Uradical" else E.s in
3390 let showextended = ref false in
3391 let leave mode _ = state.mode <- mode in
3392 let src =
3393 (object
3394 val mutable m_l = []
3395 val mutable m_a = E.a
3396 val mutable m_prev_uioh = nouioh
3397 val mutable m_prev_mode = View
3399 inherit lvsourcebase
3401 method reset prev_mode prev_uioh =
3402 m_a <- Array.of_list (List.rev m_l);
3403 m_l <- [];
3404 m_prev_mode <- prev_mode;
3405 m_prev_uioh <- prev_uioh;
3407 method int name get set =
3408 m_l <-
3409 (name, `int get, 1, Action (
3410 fun u ->
3411 let ondone s =
3412 try set (int_of_string s)
3413 with exn ->
3414 state.text <- Printf.sprintf "bad integer `%s': %s"
3415 s @@ exntos exn
3417 state.text <- E.s;
3418 let te = name ^ ": ", E.s, None, intentry, ondone, true in
3419 state.mode <- Textentry (te, leave m_prev_mode);
3421 )) :: m_l
3423 method int_with_suffix name get set =
3424 m_l <-
3425 (name, `intws get, 1, Action (
3426 fun u ->
3427 let ondone s =
3428 try set (int_of_string_with_suffix s)
3429 with exn ->
3430 state.text <- Printf.sprintf "bad integer `%s': %s"
3431 s @@ exntos exn
3433 state.text <- E.s;
3434 let te =
3435 name ^ ": ", E.s, None, intentry_with_suffix, ondone, true
3437 state.mode <- Textentry (te, leave m_prev_mode);
3439 )) :: m_l
3441 method bool ?(offset=1) ?(btos=btos) name get set =
3442 m_l <-
3443 (name, `bool (btos, get), offset, Action (
3444 fun u ->
3445 let v = get () in
3446 set (not v);
3448 )) :: m_l
3450 method color name get set =
3451 m_l <-
3452 (name, `color get, 1, Action (
3453 fun u ->
3454 let invalid = (nan, nan, nan) in
3455 let ondone s =
3456 let c =
3457 try color_of_string s
3458 with exn ->
3459 state.text <- Printf.sprintf "bad color `%s': %s"
3460 s @@ exntos exn;
3461 invalid
3463 if c <> invalid
3464 then set c;
3466 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3467 state.text <- color_to_string (get ());
3468 state.mode <- Textentry (te, leave m_prev_mode);
3470 )) :: m_l
3472 method string name get set =
3473 m_l <-
3474 (name, `string get, 1, Action (
3475 fun u ->
3476 let ondone s = set s in
3477 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3478 state.mode <- Textentry (te, leave m_prev_mode);
3480 )) :: m_l
3482 method colorspace name get set =
3483 m_l <-
3484 (name, `string get, 1, Action (
3485 fun _ ->
3486 let source =
3487 (object
3488 inherit lvsourcebase
3490 initializer
3491 m_active <- CSTE.to_int conf.colorspace;
3492 m_first <- 0;
3494 method getitemcount =
3495 Array.length CSTE.names
3496 method getitem n =
3497 (CSTE.names.(n), 0)
3498 method exit ~uioh ~cancel ~active ~first ~pan =
3499 ignore (uioh, first, pan);
3500 if not cancel then set active;
3501 None
3502 method hasaction _ = true
3503 end)
3505 state.text <- E.s;
3506 let modehash = findkeyhash conf "info" in
3507 coe (new listview ~zebra:false ~helpmode:false
3508 ~source ~trusted:true ~modehash)
3509 )) :: m_l
3511 method paxmark name get set =
3512 m_l <-
3513 (name, `string get, 1, Action (
3514 fun _ ->
3515 let source =
3516 (object
3517 inherit lvsourcebase
3519 initializer
3520 m_active <- MTE.to_int conf.paxmark;
3521 m_first <- 0;
3523 method getitemcount = Array.length MTE.names
3524 method getitem n = (MTE.names.(n), 0)
3525 method exit ~uioh ~cancel ~active ~first ~pan =
3526 ignore (uioh, first, pan);
3527 if not cancel then set active;
3528 None
3529 method hasaction _ = true
3530 end)
3532 state.text <- E.s;
3533 let modehash = findkeyhash conf "info" in
3534 coe (new listview ~zebra:false ~helpmode:false
3535 ~source ~trusted:true ~modehash)
3536 )) :: m_l
3538 method fitmodel name get set =
3539 m_l <-
3540 (name, `string get, 1, Action (
3541 fun _ ->
3542 let source =
3543 (object
3544 inherit lvsourcebase
3546 initializer
3547 m_active <- FMTE.to_int conf.fitmodel;
3548 m_first <- 0;
3550 method getitemcount = Array.length FMTE.names
3551 method getitem n = (FMTE.names.(n), 0)
3552 method exit ~uioh ~cancel ~active ~first ~pan =
3553 ignore (uioh, first, pan);
3554 if not cancel then set active;
3555 None
3556 method hasaction _ = true
3557 end)
3559 state.text <- E.s;
3560 let modehash = findkeyhash conf "info" in
3561 coe (new listview ~zebra:false ~helpmode:false
3562 ~source ~trusted:true ~modehash)
3563 )) :: m_l
3565 method caption s offset =
3566 m_l <- (s, `empty, offset, Noaction) :: m_l
3568 method caption2 s f offset =
3569 m_l <- (s, `string f, offset, Noaction) :: m_l
3571 method getitemcount = Array.length m_a
3573 method getitem n =
3574 let tostr = function
3575 | `int f -> string_of_int (f ())
3576 | `intws f -> string_with_suffix_of_int (f ())
3577 | `string f -> f ()
3578 | `color f -> color_to_string (f ())
3579 | `bool (btos, f) -> btos (f ())
3580 | `empty -> E.s
3582 let name, t, offset, _ = m_a.(n) in
3583 ((let s = tostr t in
3584 if nonemptystr s
3585 then Printf.sprintf "%s\t%s" name s
3586 else name),
3587 offset)
3589 method exit ~uioh ~cancel ~active ~first ~pan =
3590 let uiohopt =
3591 if not cancel
3592 then (
3593 let uioh =
3594 match m_a.(active) with
3595 | _, _, _, Action f -> f uioh
3596 | _, _, _, Noaction -> uioh
3598 Some uioh
3600 else None
3602 m_active <- active;
3603 m_first <- first;
3604 m_pan <- pan;
3605 uiohopt
3607 method hasaction n =
3608 match m_a.(n) with
3609 | _, _, _, Action _ -> true
3610 | _, _, _, Noaction -> false
3612 initializer m_active <- 1
3613 end)
3615 let rec fillsrc prevmode prevuioh =
3616 let sep () = src#caption E.s 0 in
3617 let colorp name get set =
3618 src#string name
3619 (fun () -> color_to_string (get ()))
3620 (fun v ->
3622 let c = color_of_string v in
3623 set c
3624 with exn ->
3625 state.text <- Printf.sprintf "bad color `%s': %s" v @@ exntos exn
3628 let oldmode = state.mode in
3629 let birdseye = isbirdseye state.mode in
3631 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3633 src#bool "presentation mode"
3634 (fun () -> conf.presentation)
3635 (fun v -> setpresentationmode v);
3637 src#bool "ignore case in searches"
3638 (fun () -> conf.icase)
3639 (fun v -> conf.icase <- v);
3641 src#bool "preload"
3642 (fun () -> conf.preload)
3643 (fun v -> conf.preload <- v);
3645 src#bool "highlight links"
3646 (fun () -> conf.hlinks)
3647 (fun v -> conf.hlinks <- v);
3649 src#bool "under info"
3650 (fun () -> conf.underinfo)
3651 (fun v -> conf.underinfo <- v);
3653 src#bool "persistent bookmarks"
3654 (fun () -> conf.savebmarks)
3655 (fun v -> conf.savebmarks <- v);
3657 src#fitmodel "fit model"
3658 (fun () -> FMTE.to_string conf.fitmodel)
3659 (fun v -> reqlayout conf.angle (FMTE.of_int v));
3661 src#bool "trim margins"
3662 (fun () -> conf.trimmargins)
3663 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3665 src#bool "persistent location"
3666 (fun () -> conf.jumpback)
3667 (fun v -> conf.jumpback <- v);
3669 sep ();
3670 src#int "inter-page space"
3671 (fun () -> conf.interpagespace)
3672 (fun n ->
3673 conf.interpagespace <- n;
3674 docolumns conf.columns;
3675 let pageno, py =
3676 match state.layout with
3677 | [] -> 0, 0
3678 | l :: _ ->
3679 l.pageno, l.pagey
3681 state.maxy <- calcheight ();
3682 let y = getpagey pageno in
3683 gotoxy state.x (y + py)
3686 src#int "page bias"
3687 (fun () -> conf.pagebias)
3688 (fun v -> conf.pagebias <- v);
3690 src#int "scroll step"
3691 (fun () -> conf.scrollstep)
3692 (fun n -> conf.scrollstep <- n);
3694 src#int "horizontal scroll step"
3695 (fun () -> conf.hscrollstep)
3696 (fun v -> conf.hscrollstep <- v);
3698 src#int "auto scroll step"
3699 (fun () ->
3700 match state.autoscroll with
3701 | Some step -> step
3702 | _ -> conf.autoscrollstep)
3703 (fun n ->
3704 let n = boundastep state.winh n in
3705 if state.autoscroll <> None
3706 then state.autoscroll <- Some n;
3707 conf.autoscrollstep <- n);
3709 src#int "zoom"
3710 (fun () -> truncate (conf.zoom *. 100.))
3711 (fun v -> pivotzoom ((float v) /. 100.));
3713 src#int "rotation"
3714 (fun () -> conf.angle)
3715 (fun v -> reqlayout v conf.fitmodel);
3717 src#int "scroll bar width"
3718 (fun () -> conf.scrollbw)
3719 (fun v ->
3720 conf.scrollbw <- v;
3721 reshape state.winw state.winh;
3724 src#int "scroll handle height"
3725 (fun () -> conf.scrollh)
3726 (fun v -> conf.scrollh <- v;);
3728 src#int "thumbnail width"
3729 (fun () -> conf.thumbw)
3730 (fun v ->
3731 conf.thumbw <- min 4096 v;
3732 match oldmode with
3733 | Birdseye beye ->
3734 leavebirdseye beye false;
3735 enterbirdseye ()
3736 | Textentry _
3737 | View
3738 | LinkNav _ -> ()
3741 let mode = state.mode in
3742 src#string "columns"
3743 (fun () ->
3744 match conf.columns with
3745 | Csingle _ -> "1"
3746 | Cmulti (multi, _) -> multicolumns_to_string multi
3747 | Csplit (count, _) -> "-" ^ string_of_int count
3749 (fun v ->
3750 let n, a, b = multicolumns_of_string v in
3751 setcolumns mode n a b);
3753 sep ();
3754 src#caption "Pixmap cache" 0;
3755 src#int_with_suffix "size (advisory)"
3756 (fun () -> conf.memlimit)
3757 (fun v -> conf.memlimit <- v);
3759 src#caption2 "used"
3760 (fun () -> Printf.sprintf "%s bytes, %d tiles"
3761 (string_with_suffix_of_int state.memused)
3762 (Hashtbl.length state.tilemap)) 1;
3764 sep ();
3765 src#caption "Layout" 0;
3766 src#caption2 "Dimension"
3767 (fun () ->
3768 Printf.sprintf "%dx%d (virtual %dx%d)"
3769 state.winw state.winh
3770 state.w state.maxy)
3772 if conf.debug
3773 then
3774 src#caption2 "Position" (fun () ->
3775 Printf.sprintf "%dx%d" state.x state.y
3777 else
3778 src#caption2 "Position" (fun () -> describe_location ()) 1
3781 sep ();
3782 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3783 "Save these parameters as global defaults at exit"
3784 (fun () -> conf.bedefault)
3785 (fun v -> conf.bedefault <- v)
3788 sep ();
3789 let btos b = if b then "@Ulguillemet" else "@Urguillemet" in
3790 src#bool ~offset:0 ~btos "Extended parameters"
3791 (fun () -> !showextended)
3792 (fun v -> showextended := v; fillsrc prevmode prevuioh);
3793 if !showextended
3794 then (
3795 src#bool "checkers"
3796 (fun () -> conf.checkers)
3797 (fun v -> conf.checkers <- v; setcheckers v);
3798 src#bool "update cursor"
3799 (fun () -> conf.updatecurs)
3800 (fun v -> conf.updatecurs <- v);
3801 src#bool "scroll-bar on the left"
3802 (fun () -> conf.leftscroll)
3803 (fun v -> conf.leftscroll <- v);
3804 src#bool "verbose"
3805 (fun () -> conf.verbose)
3806 (fun v -> conf.verbose <- v);
3807 src#bool "invert colors"
3808 (fun () -> conf.invert)
3809 (fun v -> conf.invert <- v);
3810 src#bool "max fit"
3811 (fun () -> conf.maxhfit)
3812 (fun v -> conf.maxhfit <- v);
3813 src#bool "pax mode"
3814 (fun () -> conf.pax != None)
3815 (fun v ->
3816 if v
3817 then conf.pax <- Some (ref (now (), 0, 0))
3818 else conf.pax <- None);
3819 src#string "uri launcher"
3820 (fun () -> conf.urilauncher)
3821 (fun v -> conf.urilauncher <- v);
3822 src#string "path launcher"
3823 (fun () -> conf.pathlauncher)
3824 (fun v -> conf.pathlauncher <- v);
3825 src#string "tile size"
3826 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3827 (fun v ->
3829 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
3830 conf.tilew <- max 64 w;
3831 conf.tileh <- max 64 h;
3832 flushtiles ();
3833 with exn ->
3834 state.text <- Printf.sprintf "bad tile size `%s': %s"
3835 v @@ exntos exn
3837 src#int "texture count"
3838 (fun () -> conf.texcount)
3839 (fun v ->
3840 if realloctexts v
3841 then conf.texcount <- v
3842 else impmsg "failed to set texture count please retry later"
3844 src#int "slice height"
3845 (fun () -> conf.sliceheight)
3846 (fun v ->
3847 conf.sliceheight <- v;
3848 wcmd "sliceh %d" conf.sliceheight;
3850 src#int "anti-aliasing level"
3851 (fun () -> conf.aalevel)
3852 (fun v ->
3853 conf.aalevel <- bound v 0 8;
3854 state.anchor <- getanchor ();
3855 opendoc state.path state.password;
3857 src#string "page scroll scaling factor"
3858 (fun () -> string_of_float conf.pgscale)
3859 (fun v ->
3861 let s = float_of_string v in
3862 conf.pgscale <- s
3863 with exn ->
3864 state.text <- Printf.sprintf
3865 "bad page scroll scaling factor `%s': %s" v @@ exntos exn
3868 src#int "ui font size"
3869 (fun () -> fstate.fontsize)
3870 (fun v -> setfontsize (bound v 5 100));
3871 src#int "hint font size"
3872 (fun () -> conf.hfsize)
3873 (fun v -> conf.hfsize <- bound v 5 100);
3874 colorp "background color"
3875 (fun () -> conf.bgcolor)
3876 (fun v -> conf.bgcolor <- v; setbgcol v);
3877 src#bool "crop hack"
3878 (fun () -> conf.crophack)
3879 (fun v -> conf.crophack <- v);
3880 src#string "trim fuzz"
3881 (fun () -> irect_to_string conf.trimfuzz)
3882 (fun v ->
3884 conf.trimfuzz <- irect_of_string v;
3885 if conf.trimmargins
3886 then settrim true conf.trimfuzz;
3887 with exn ->
3888 state.text <- Printf.sprintf "bad irect `%s': %s" v @@ exntos exn
3890 src#string "throttle"
3891 (fun () ->
3892 match conf.maxwait with
3893 | None -> "show place holder if page is not ready"
3894 | Some time ->
3895 if time = infinity
3896 then "wait for page to fully render"
3897 else
3898 "wait " ^ string_of_float time
3899 ^ " seconds before showing placeholder"
3901 (fun v ->
3903 let f = float_of_string v in
3904 if f <= 0.0
3905 then conf.maxwait <- None
3906 else conf.maxwait <- Some f
3907 with exn ->
3908 state.text <- Printf.sprintf "bad time `%s': %s" v @@ exntos exn
3910 src#string "ghyll scroll"
3911 (fun () ->
3912 match conf.ghyllscroll with
3913 | None -> E.s
3914 | Some nab -> ghyllscroll_to_string nab
3916 (fun v ->
3917 try conf.ghyllscroll <- ghyllscroll_of_string v
3918 with
3919 | Failure msg ->
3920 state.text <- Printf.sprintf "bad ghyll `%s': %s" v msg
3921 | exn ->
3922 state.text <- Printf.sprintf "bad ghyll `%s': %s" v @@ exntos exn
3924 src#string "selection command"
3925 (fun () -> conf.selcmd)
3926 (fun v -> conf.selcmd <- v);
3927 src#string "synctex command"
3928 (fun () -> conf.stcmd)
3929 (fun v -> conf.stcmd <- v);
3930 src#string "pax command"
3931 (fun () -> conf.paxcmd)
3932 (fun v -> conf.paxcmd <- v);
3933 src#string "ask password command"
3934 (fun () -> conf.passcmd)
3935 (fun v -> conf.passcmd <- v);
3936 src#string "save path command"
3937 (fun () -> conf.savecmd)
3938 (fun v -> conf.savecmd <- v);
3939 src#colorspace "color space"
3940 (fun () -> CSTE.to_string conf.colorspace)
3941 (fun v ->
3942 conf.colorspace <- CSTE.of_int v;
3943 wcmd "cs %d" v;
3944 load state.layout;
3946 src#paxmark "pax mark method"
3947 (fun () -> MTE.to_string conf.paxmark)
3948 (fun v -> conf.paxmark <- MTE.of_int v);
3949 if bousable () && !opengl_has_pbo
3950 then
3951 src#bool "use PBO"
3952 (fun () -> conf.usepbo)
3953 (fun v -> conf.usepbo <- v);
3954 src#bool "mouse wheel scrolls pages"
3955 (fun () -> conf.wheelbypage)
3956 (fun v -> conf.wheelbypage <- v);
3957 src#bool "open remote links in a new instance"
3958 (fun () -> conf.riani)
3959 (fun v -> conf.riani <- v);
3960 src#bool "edit annotations inline"
3961 (fun () -> conf.annotinline)
3962 (fun v -> conf.annotinline <- v);
3963 src#bool "coarse positioning in presentation mode"
3964 (fun () -> conf.coarseprespos)
3965 (fun v -> conf.coarseprespos <- v);
3968 sep ();
3969 src#caption "Document" 0;
3970 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
3971 src#caption2 "Pages"
3972 (fun () -> string_of_int state.pagecount) 1;
3973 src#caption2 "Dimensions"
3974 (fun () -> string_of_int (List.length state.pdims)) 1;
3975 if conf.trimmargins
3976 then (
3977 sep ();
3978 src#caption "Trimmed margins" 0;
3979 src#caption2 "Dimensions"
3980 (fun () -> string_of_int (List.length state.pdims)) 1;
3983 sep ();
3984 src#caption "OpenGL" 0;
3985 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
3986 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
3988 sep ();
3989 src#caption "Location" 0;
3990 if nonemptystr state.origin
3991 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
3992 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
3994 src#reset prevmode prevuioh;
3996 fun () ->
3997 state.text <- E.s;
3998 resetmstate ();
3999 let prevmode = state.mode
4000 and prevuioh = state.uioh in
4001 fillsrc prevmode prevuioh;
4002 let source = (src :> lvsource) in
4003 let modehash = findkeyhash conf "info" in
4004 state.uioh <- coe (object (self)
4005 inherit listview ~zebra:false ~helpmode:false
4006 ~source ~trusted:true ~modehash as super
4007 val mutable m_prevmemused = 0
4008 method! infochanged = function
4009 | Memused ->
4010 if m_prevmemused != state.memused
4011 then (
4012 m_prevmemused <- state.memused;
4013 G.postRedisplay "memusedchanged";
4015 | Pdim -> G.postRedisplay "pdimchanged"
4016 | Docinfo -> fillsrc prevmode prevuioh
4018 method! key key mask =
4019 if not (Wsi.withctrl mask)
4020 then
4021 match key with
4022 | @left | @kpleft -> coe (self#updownlevel ~-1)
4023 | @right | @kpright -> coe (self#updownlevel 1)
4024 | _ -> super#key key mask
4025 else super#key key mask
4026 end);
4027 G.postRedisplay "info";
4030 let enterhelpmode =
4031 let source =
4032 (object
4033 inherit lvsourcebase
4034 method getitemcount = Array.length state.help
4035 method getitem n =
4036 let s, l, _ = state.help.(n) in
4037 (s, l)
4039 method exit ~uioh ~cancel ~active ~first ~pan =
4040 let optuioh =
4041 if not cancel
4042 then (
4043 match state.help.(active) with
4044 | _, _, Action f -> Some (f uioh)
4045 | _, _, Noaction -> Some uioh
4047 else None
4049 m_active <- active;
4050 m_first <- first;
4051 m_pan <- pan;
4052 optuioh
4054 method hasaction n =
4055 match state.help.(n) with
4056 | _, _, Action _ -> true
4057 | _, _, Noaction -> false
4059 initializer
4060 m_active <- -1
4061 end)
4062 in fun () ->
4063 let modehash = findkeyhash conf "help" in
4064 resetmstate ();
4065 state.uioh <- coe (new listview
4066 ~zebra:false ~helpmode:true
4067 ~source ~trusted:true ~modehash);
4068 G.postRedisplay "help";
4071 let entermsgsmode =
4072 let msgsource =
4073 (object
4074 inherit lvsourcebase
4075 val mutable m_items = E.a
4077 method getitemcount = 1 + Array.length m_items
4079 method getitem n =
4080 if n = 0
4081 then "[Clear]", 0
4082 else m_items.(n-1), 0
4084 method exit ~uioh ~cancel ~active ~first ~pan =
4085 ignore uioh;
4086 if not cancel
4087 then (
4088 if active = 0
4089 then Buffer.clear state.errmsgs;
4091 m_active <- active;
4092 m_first <- first;
4093 m_pan <- pan;
4094 None
4096 method hasaction n =
4097 n = 0
4099 method reset =
4100 state.newerrmsgs <- false;
4101 let l = Str.split newlinere (Buffer.contents state.errmsgs) in
4102 m_items <- Array.of_list l
4104 initializer
4105 m_active <- 0
4106 end)
4107 in fun () ->
4108 state.text <- E.s;
4109 resetmstate ();
4110 msgsource#reset;
4111 let source = (msgsource :> lvsource) in
4112 let modehash = findkeyhash conf "listview" in
4113 state.uioh <- coe (object
4114 inherit listview ~zebra:false ~helpmode:false
4115 ~source ~trusted:false ~modehash as super
4116 method! display =
4117 if state.newerrmsgs
4118 then msgsource#reset;
4119 super#display
4120 end);
4121 G.postRedisplay "msgs";
4124 let getusertext s =
4125 let editor = getenvwithdef "EDITOR" E.s in
4126 if emptystr editor
4127 then E.s
4128 else
4129 let tmppath = Filename.temp_file "llpp" "note" in
4130 if nonemptystr s
4131 then (
4132 let oc = open_out tmppath in
4133 output_string oc s;
4134 close_out oc;
4136 let execstr = editor ^ " " ^ tmppath in
4137 let s =
4138 match spawn execstr [] with
4139 | (exception exn) ->
4140 impmsg "spawn(%S) failed: %s" execstr @@ exntos exn;
4142 | pid ->
4143 match Unix.waitpid [] pid with
4144 | (exception exn) ->
4145 impmsg "waitpid(%d) failed: %s" pid @@ exntos exn;
4147 | (_pid, status) ->
4148 match status with
4149 | Unix.WEXITED 0 -> filecontents tmppath
4150 | Unix.WEXITED n ->
4151 impmsg "editor process(%s) exited abnormally: %d" execstr n;
4153 | Unix.WSIGNALED n ->
4154 impmsg "editor process(%s) was killed by signal %d" execstr n;
4156 | Unix.WSTOPPED n ->
4157 impmsg "editor(%s) process was stopped by signal %d" execstr n;
4160 match Unix.unlink tmppath with
4161 | (exception exn) ->
4162 impmsg "failed to ulink %S: %s" tmppath @@ exntos exn;
4164 | () -> s
4167 let enterannotmode opaque slinkindex =
4168 let msgsource =
4169 (object
4170 inherit lvsourcebase
4171 val mutable m_text = E.s
4172 val mutable m_items = E.a
4174 method getitemcount = Array.length m_items
4176 method getitem n =
4177 let label, _func = m_items.(n) in
4178 label, 0
4180 method exit ~uioh ~cancel ~active ~first ~pan =
4181 ignore (uioh, first, pan);
4182 if not cancel
4183 then (
4184 let _label, func = m_items.(active) in
4185 func ()
4187 None
4189 method hasaction n = nonemptystr @@ fst m_items.(n)
4191 method reset s =
4192 let rec split accu b i =
4193 let p = b+i in
4194 if p = String.length s
4195 then (String.sub s b (p-b), unit) :: accu
4196 else
4197 if (i > 70 && s.[p] = ' ') || s.[p] = '\r' || s.[p] = '\n'
4198 then
4199 let ss = if i = 0 then E.s else String.sub s b i in
4200 split ((ss, unit)::accu) (p+1) 0
4201 else
4202 split accu b (i+1)
4204 let cleanup () =
4205 wcmd "freepage %s" (~> opaque);
4206 let keys =
4207 Hashtbl.fold (fun key opaque' accu ->
4208 if opaque' = opaque'
4209 then key :: accu else accu) state.pagemap []
4211 List.iter (Hashtbl.remove state.pagemap) keys;
4212 flushtiles ();
4213 gotoxy state.x state.y
4215 let dele () =
4216 delannot opaque slinkindex;
4217 cleanup ();
4219 let edit inline () =
4220 let update s =
4221 if emptystr s
4222 then dele ()
4223 else (
4224 modannot opaque slinkindex s;
4225 cleanup ();
4228 if inline
4229 then
4230 let mode = state.mode in
4231 state.mode <-
4232 Textentry (
4233 ("annotation: ", m_text, None, textentry, update, true),
4234 fun _ -> state.mode <- mode);
4235 state.text <- E.s;
4236 enttext ();
4237 else
4238 let s = getusertext m_text in
4239 update s
4241 m_text <- s;
4242 m_items <-
4243 ( "[Copy]", fun () -> selstring m_text)
4244 :: ("[Delete]", dele)
4245 :: ("[Edit]", edit conf.annotinline)
4246 :: (E.s, unit)
4247 :: split [] 0 0 |> List.rev |> Array.of_list
4249 initializer
4250 m_active <- 0
4251 end)
4253 state.text <- E.s;
4254 let s = getannotcontents opaque slinkindex in
4255 resetmstate ();
4256 msgsource#reset s;
4257 let source = (msgsource :> lvsource) in
4258 let modehash = findkeyhash conf "listview" in
4259 state.uioh <- coe (object
4260 inherit listview ~zebra:false ~helpmode:false
4261 ~source ~trusted:false ~modehash
4262 end);
4263 G.postRedisplay "enterannotmode";
4266 let gotoremote spec =
4267 let filename, dest = splitatchar spec '#' in
4268 let getpath filename =
4269 let path =
4270 if nonemptystr filename
4271 then
4272 if Filename.is_relative filename
4273 then
4274 let dir = Filename.dirname state.path in
4275 let dir =
4276 if Filename.is_implicit dir
4277 then Filename.concat (Sys.getcwd ()) dir
4278 else dir
4280 Filename.concat dir filename
4281 else filename
4282 else E.s
4284 if Sys.file_exists path
4285 then path
4286 else E.s
4288 let path = getpath filename in
4289 let dospawn lcmd =
4290 if conf.riani
4291 then
4292 let cmd = Lazy.force_val lcmd in
4293 match spawn cmd with
4294 | _pid -> ()
4295 | (exception exn) ->
4296 dolog "failed to execute `%s': %s" cmd @@ exntos exn
4297 else
4298 let anchor = getanchor () in
4299 let ranchor = state.path, state.password, anchor, state.origin in
4300 state.origin <- E.s;
4301 state.ranchors <- ranchor :: state.ranchors;
4302 opendoc path E.s;
4304 if substratis spec 0 "page="
4305 then
4306 match Scanf.sscanf spec "page=%d" (fun n -> n) with
4307 | pageno ->
4308 state.anchor <- (pageno, 0.0, 0.0);
4309 dospawn @@ lazy (Printf.sprintf "%s -page %d %S" !selfexec pageno path);
4310 | exception exn ->
4311 adderrfmt "error parsing remote destination" "page: %s" @@ exntos exn
4312 else (
4313 state.nameddest <- dest;
4314 dospawn @@ lazy (!selfexec ^ " " ^ path ^ " -dest " ^ dest)
4318 let gotounder = function
4319 | Ulinkuri s when isexternallink s ->
4320 if substratis s 0 "file://"
4321 then gotoremote @@ String.sub s 7 (String.length s - 7)
4322 else gotouri s
4323 | Ulinkuri s ->
4324 let pageno, x, y = uritolocation s in
4325 addnav ();
4326 gotopagexy !wtmode pageno x y
4327 | Utext _ | Unone -> ()
4328 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
4331 let gotooutline (_, _, kind) =
4332 match kind with
4333 | Onone -> ()
4334 | Oanchor anchor ->
4335 let (pageno, y, _) = anchor in
4336 let y = getanchory
4337 (if conf.presentation then (pageno, y, 1.0) else anchor)
4339 addnav ();
4340 gotoghyll y
4341 | Ouri uri -> gotounder (Ulinkuri uri)
4342 | Olaunch _cmd -> failwith "gotounder (Ulaunch cmd)"
4343 | Oremote _remote -> failwith "gotounder (Uremote remote)"
4344 | Ohistory hist -> gotohist hist
4345 | Oremotedest _remotedest -> failwith "gotounder (Uremotedest remotedest)"
4348 class outlinesoucebase fetchoutlines = object (self)
4349 inherit lvsourcebase
4350 val mutable m_items = E.a
4351 val mutable m_minfo = E.a
4352 val mutable m_orig_items = E.a
4353 val mutable m_orig_minfo = E.a
4354 val mutable m_narrow_patterns = []
4355 val mutable m_gen = -1
4357 method getitemcount = Array.length m_items
4359 method getitem n =
4360 let s, n, _ = m_items.(n) in
4361 (s, n+0)
4363 method exit ~(uioh:uioh) ~cancel ~active ~(first:int) ~pan :
4364 uioh option =
4365 ignore (uioh, first);
4366 let items, minfo =
4367 if m_narrow_patterns = []
4368 then m_orig_items, m_orig_minfo
4369 else m_items, m_minfo
4371 m_pan <- pan;
4372 if not cancel
4373 then (
4374 m_items <- items;
4375 m_minfo <- minfo;
4376 gotooutline m_items.(active);
4378 else (
4379 m_items <- items;
4380 m_minfo <- minfo;
4382 None
4384 method hasaction (_:int) = true
4386 method greetmsg =
4387 if Array.length m_items != Array.length m_orig_items
4388 then
4389 let s =
4390 match m_narrow_patterns with
4391 | one :: [] -> one
4392 | many -> String.concat "@Uellipsis" (List.rev many)
4394 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
4395 else E.s
4397 method statestr =
4398 match m_narrow_patterns with
4399 | [] -> E.s
4400 | one :: [] -> one
4401 | head :: _ -> "@Uellipsis" ^ head
4403 method narrow pattern =
4404 match Str.regexp_case_fold pattern with
4405 | (exception _) -> ()
4406 | re ->
4407 let rec loop accu minfo n =
4408 if n = -1
4409 then (
4410 m_items <- Array.of_list accu;
4411 m_minfo <- Array.of_list minfo;
4413 else
4414 let (s, _, _) as o = m_items.(n) in
4415 let accu, minfo =
4416 match Str.search_forward re s 0 with
4417 | (exception Not_found) -> accu, minfo
4418 | first -> o :: accu, (first, Str.match_end ()) :: minfo
4420 loop accu minfo (n-1)
4422 loop [] [] (Array.length m_items - 1)
4424 method! getminfo = m_minfo
4426 method denarrow =
4427 m_orig_items <- fetchoutlines ();
4428 m_minfo <- m_orig_minfo;
4429 m_items <- m_orig_items
4431 method add_narrow_pattern pattern =
4432 m_narrow_patterns <- pattern :: m_narrow_patterns
4434 method del_narrow_pattern =
4435 match m_narrow_patterns with
4436 | _ :: rest -> m_narrow_patterns <- rest
4437 | [] -> ()
4439 method renarrow =
4440 self#denarrow;
4441 match m_narrow_patterns with
4442 | pattern :: [] -> self#narrow pattern; pattern
4443 | list ->
4444 List.fold_left (fun accu pattern ->
4445 self#narrow pattern;
4446 pattern ^ "@Uellipsis" ^ accu) E.s list
4448 method calcactive (_:anchor) = 0
4450 method reset anchor items =
4451 if state.gen != m_gen
4452 then (
4453 m_orig_items <- items;
4454 m_items <- items;
4455 m_narrow_patterns <- [];
4456 m_minfo <- E.a;
4457 m_orig_minfo <- E.a;
4458 m_gen <- state.gen;
4460 else (
4461 if items != m_orig_items
4462 then (
4463 m_orig_items <- items;
4464 if m_narrow_patterns == []
4465 then m_items <- items;
4468 let active = self#calcactive anchor in
4469 m_active <- active;
4470 m_first <- firstof m_first active
4474 let outlinesource fetchoutlines =
4475 (object
4476 inherit outlinesoucebase fetchoutlines
4477 method! calcactive anchor =
4478 let rely = getanchory anchor in
4479 let rec loop n best bestd =
4480 if n = Array.length m_items
4481 then best
4482 else
4483 let _, _, kind = m_items.(n) in
4484 match kind with
4485 | Oanchor anchor ->
4486 let orely = getanchory anchor in
4487 let d = abs (orely - rely) in
4488 if d < bestd
4489 then loop (n+1) n d
4490 else loop (n+1) best bestd
4491 | Onone | Oremote _ | Olaunch _
4492 | Oremotedest _ | Ouri _ | Ohistory _ ->
4493 loop (n+1) best bestd
4495 loop 0 ~-1 max_int
4496 end)
4499 let enteroutlinemode, enterbookmarkmode, enterhistmode =
4500 let mkselector sourcetype =
4501 let fetchoutlines () =
4502 match sourcetype with
4503 | `bookmarks -> Array.of_list state.bookmarks
4504 | `outlines -> state.outlines
4505 | `history -> genhistoutlines ()
4507 let source =
4508 if sourcetype = `history
4509 then new outlinesoucebase fetchoutlines
4510 else outlinesource fetchoutlines
4512 fun errmsg ->
4513 let outlines = fetchoutlines () in
4514 if Array.length outlines = 0
4515 then (
4516 showtext ' ' errmsg;
4518 else (
4519 resetmstate ();
4520 Wsi.setcursor Wsi.CURSOR_INHERIT;
4521 let anchor = getanchor () in
4522 source#reset anchor outlines;
4523 state.text <- source#greetmsg;
4524 state.uioh <-
4525 coe (new outlinelistview ~zebra:(sourcetype=`history) ~source);
4526 G.postRedisplay "enter selector";
4529 let mkenter sourcetype errmsg =
4530 let enter = mkselector sourcetype in
4531 fun () -> enter errmsg
4533 mkenter `outlines "document has no outline"
4534 , mkenter `bookmarks "document has no bookmarks (yet)"
4535 , mkenter `history "history is empty"
4538 let quickbookmark ?title () =
4539 match state.layout with
4540 | [] -> ()
4541 | l :: _ ->
4542 let title =
4543 match title with
4544 | None ->
4545 let tm = Unix.localtime (now ()) in
4546 Printf.sprintf
4547 "Quick (page %d) (bookmarked at %02d/%02d/%d %02d:%02d)"
4548 (l.pageno+1)
4549 tm.Unix.tm_mday
4550 (tm.Unix.tm_mon+1)
4551 (tm.Unix.tm_year + 1900)
4552 tm.Unix.tm_hour
4553 tm.Unix.tm_min
4554 | Some title -> title
4556 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4559 let setautoscrollspeed step goingdown =
4560 let incr = max 1 ((abs step) / 2) in
4561 let incr = if goingdown then incr else -incr in
4562 let astep = boundastep state.winh (step + incr) in
4563 state.autoscroll <- Some astep;
4566 let canpan () =
4567 match conf.columns with
4568 | Csplit _ -> true
4569 | Csingle _ | Cmulti _ -> state.x != 0 || conf.zoom > 1.0
4572 let panbound x = bound x (-state.w) state.winw;;
4574 let existsinrow pageno (columns, coverA, coverB) p =
4575 let last = ((pageno - coverA) mod columns) + columns in
4576 let rec any = function
4577 | [] -> false
4578 | l :: rest ->
4579 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4580 then p l
4581 else (
4582 if not (p l)
4583 then (if l.pageno = last then false else any rest)
4584 else true
4587 any state.layout
4590 let nextpage () =
4591 match state.layout with
4592 | [] ->
4593 let pageno = page_of_y state.y in
4594 gotoghyll (getpagey (pageno+1))
4595 | l :: rest ->
4596 match conf.columns with
4597 | Csingle _ ->
4598 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4599 then
4600 let y = clamp (pgscale state.winh) in
4601 gotoghyll y
4602 else
4603 let pageno = min (l.pageno+1) (state.pagecount-1) in
4604 gotoghyll (getpagey pageno)
4605 | Cmulti ((c, _, _) as cl, _) ->
4606 if conf.presentation
4607 && (existsinrow l.pageno cl
4608 (fun l -> l.pageh > l.pagey + l.pagevh))
4609 then
4610 let y = clamp (pgscale state.winh) in
4611 gotoghyll y
4612 else
4613 let pageno = min (l.pageno+c) (state.pagecount-1) in
4614 gotoghyll (getpagey pageno)
4615 | Csplit (n, _) ->
4616 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4617 then
4618 let pagey, pageh = getpageyh l.pageno in
4619 let pagey = pagey + pageh * l.pagecol in
4620 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4621 gotoghyll (pagey + pageh + ips)
4624 let prevpage () =
4625 match state.layout with
4626 | [] ->
4627 let pageno = page_of_y state.y in
4628 gotoghyll (getpagey (pageno-1))
4629 | l :: _ ->
4630 match conf.columns with
4631 | Csingle _ ->
4632 if conf.presentation && l.pagey != 0
4633 then
4634 gotoghyll (clamp (pgscale ~-(state.winh)))
4635 else
4636 let pageno = max 0 (l.pageno-1) in
4637 gotoghyll (getpagey pageno)
4638 | Cmulti ((c, _, coverB) as cl, _) ->
4639 if conf.presentation &&
4640 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4641 then
4642 gotoghyll (clamp (pgscale ~-(state.winh)))
4643 else
4644 let decr =
4645 if l.pageno = state.pagecount - coverB
4646 then 1
4647 else c
4649 let pageno = max 0 (l.pageno-decr) in
4650 gotoghyll (getpagey pageno)
4651 | Csplit (n, _) ->
4652 let y =
4653 if l.pagecol = 0
4654 then
4655 if l.pageno = 0
4656 then l.pagey
4657 else
4658 let pageno = max 0 (l.pageno-1) in
4659 let pagey, pageh = getpageyh pageno in
4660 pagey + (n-1)*pageh
4661 else
4662 let pagey, pageh = getpageyh l.pageno in
4663 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4665 gotoghyll y
4668 let save () =
4669 if emptystr conf.savecmd
4670 then error "don't know where to save modified document"
4671 else
4672 let savecmd = Str.global_replace percentsre state.path conf.savecmd in
4673 let path =
4674 getcmdoutput
4675 (fun s -> error "failed to obtain path to the saved copy: %s" s)
4676 savecmd
4678 if nonemptystr path
4679 then
4680 let tmp = path ^ ".tmp" in
4681 savedoc tmp;
4682 Unix.rename tmp path;
4685 let viewkeyboard key mask =
4686 let enttext te =
4687 let mode = state.mode in
4688 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4689 state.text <- E.s;
4690 enttext ();
4691 G.postRedisplay "view:enttext"
4693 let ctrl = Wsi.withctrl mask in
4694 let key = Wsi.keypadtodigitkey key in
4695 match key with
4696 | @Q -> exit 0
4698 | @W ->
4699 if hasunsavedchanges ()
4700 then save ()
4702 | @insert ->
4703 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4704 then (
4705 state.mode <- LinkNav (Ltgendir 0);
4706 gotoxy state.x state.y;
4708 else impmsg "keyboard link navigation does not work under rotation"
4710 | @escape | @q ->
4711 begin match state.mstate with
4712 | Mzoomrect _ ->
4713 resetmstate ();
4714 G.postRedisplay "kill rect";
4715 | Msel _
4716 | Mpan _
4717 | Mscrolly | Mscrollx
4718 | Mzoom _
4719 | Mnone ->
4720 begin match state.mode with
4721 | LinkNav _ ->
4722 state.mode <- View;
4723 G.postRedisplay "esc leave linknav"
4724 | Birdseye _
4725 | Textentry _
4726 | View ->
4727 match state.ranchors with
4728 | [] -> raise Quit
4729 | (path, password, anchor, origin) :: rest ->
4730 state.ranchors <- rest;
4731 state.anchor <- anchor;
4732 state.origin <- origin;
4733 state.nameddest <- E.s;
4734 opendoc path password
4735 end;
4736 end;
4738 | @backspace ->
4739 gotoghyll (getnav ~-1)
4741 | @o ->
4742 enteroutlinemode ()
4744 | @H ->
4745 enterhistmode ()
4747 | @u ->
4748 state.rects <- [];
4749 state.text <- E.s;
4750 Hashtbl.iter (fun _ opaque ->
4751 clearmark opaque;
4752 Hashtbl.clear state.prects) state.pagemap;
4753 G.postRedisplay "dehighlight";
4755 | @slash | @question ->
4756 let ondone isforw s =
4757 cbput state.hists.pat s;
4758 state.searchpattern <- s;
4759 search s isforw
4761 let s = String.make 1 (Char.chr key) in
4762 enttext (s, E.s, Some (onhist state.hists.pat),
4763 textentry, ondone (key = @slash), true)
4765 | @plus | @kpplus | @equals when ctrl ->
4766 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4767 pivotzoom (conf.zoom +. incr)
4769 | @plus | @kpplus ->
4770 let ondone s =
4771 let n =
4772 try int_of_string s with exn ->
4773 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn;
4774 max_int
4776 if n != max_int
4777 then (
4778 conf.pagebias <- n;
4779 state.text <- "page bias is now " ^ string_of_int n;
4782 enttext ("page bias: ", E.s, None, intentry, ondone, true)
4784 | @minus | @kpminus when ctrl ->
4785 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4786 pivotzoom (max 0.01 (conf.zoom -. decr))
4788 | @minus | @kpminus ->
4789 let ondone msg = state.text <- msg in
4790 enttext (
4791 "option [acfhilpstvxACFPRSZTISM]: ", E.s, None,
4792 optentry state.mode, ondone, true
4795 | @0 when ctrl ->
4796 if conf.zoom = 1.0
4797 then gotoxy 0 state.y
4798 else setzoom 1.0
4800 | (@1 | @2) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
4801 let cols =
4802 match conf.columns with
4803 | Csingle _ | Cmulti _ -> 1
4804 | Csplit (n, _) -> n
4806 let h = state.winh -
4807 conf.interpagespace lsl (if conf.presentation then 1 else 0)
4809 let zoom = zoomforh state.winw h 0 cols in
4810 if zoom > 0.0 && (key = @2 || zoom < 1.0)
4811 then setzoom zoom
4813 | @3 when ctrl ->
4814 let fm =
4815 match conf.fitmodel with
4816 | FitWidth -> FitProportional
4817 | FitProportional -> FitPage
4818 | FitPage -> FitWidth
4820 state.text <- "fit model: " ^ FMTE.to_string fm;
4821 reqlayout conf.angle fm
4823 | @4 when ctrl -> (* ctrl-4 *)
4824 let zoom = getmaxw () /. float state.winw in
4825 if zoom > 0.0 then setzoom zoom
4827 | @F9 ->
4828 togglebirdseye ()
4830 | @9 when ctrl ->
4831 togglebirdseye ()
4833 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4834 when not ctrl -> (* 0..9 *)
4835 let ondone s =
4836 let n =
4837 try int_of_string s with exn ->
4838 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn;
4841 if n >= 0
4842 then (
4843 addnav ();
4844 cbput state.hists.pag (string_of_int n);
4845 gotopage1 (n + conf.pagebias - 1) 0;
4848 let pageentry text key =
4849 match Char.unsafe_chr key with
4850 | 'g' -> TEdone text
4851 | _ -> intentry text key
4853 let text = String.make 1 (Char.chr key) in
4854 enttext (":", text, Some (onhist state.hists.pag),
4855 pageentry, ondone, true)
4857 | @b ->
4858 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
4859 G.postRedisplay "toggle scrollbar";
4861 | @B ->
4862 state.bzoom <- not state.bzoom;
4863 state.rects <- [];
4864 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
4866 | @l ->
4867 conf.hlinks <- not conf.hlinks;
4868 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4869 G.postRedisplay "toggle highlightlinks";
4871 | @F ->
4872 if conf.angle mod 360 = 0
4873 then (
4874 state.glinks <- true;
4875 let mode = state.mode in
4876 state.mode <-
4877 Textentry (
4878 (":", E.s, None, linknentry, linknact gotounder, false),
4879 (fun _ ->
4880 state.glinks <- false;
4881 state.mode <- mode)
4883 state.text <- E.s;
4884 G.postRedisplay "view:linkent(F)"
4886 else impmsg "hint mode does not work under rotation"
4888 | @y ->
4889 state.glinks <- true;
4890 let mode = state.mode in
4891 state.mode <- Textentry (
4893 ":", E.s, None, linknentry, linknact (fun under ->
4894 selstring (undertext under);
4895 ), false
4897 fun _ ->
4898 state.glinks <- false;
4899 state.mode <- mode
4901 state.text <- E.s;
4902 G.postRedisplay "view:linkent"
4904 | @a ->
4905 begin match state.autoscroll with
4906 | Some step ->
4907 conf.autoscrollstep <- step;
4908 state.autoscroll <- None
4909 | None ->
4910 if conf.autoscrollstep = 0
4911 then state.autoscroll <- Some 1
4912 else state.autoscroll <- Some conf.autoscrollstep
4915 | @p when ctrl ->
4916 launchpath () (* XXX where do error messages go? *)
4918 | @P ->
4919 setpresentationmode (not conf.presentation);
4920 showtext ' ' ("presentation mode " ^
4921 if conf.presentation then "on" else "off");
4923 | @f ->
4924 if List.mem Wsi.Fullscreen state.winstate
4925 then Wsi.reshape conf.cwinw conf.cwinh
4926 else Wsi.fullscreen ()
4928 | @p | @N ->
4929 search state.searchpattern false
4931 | @n | @F3 ->
4932 search state.searchpattern true
4934 | @t ->
4935 begin match state.layout with
4936 | [] -> ()
4937 | l :: _ ->
4938 gotoghyll (getpagey l.pageno)
4941 | @space ->
4942 nextpage ()
4944 | @delete | @kpdelete -> (* delete *)
4945 prevpage ()
4947 | @equals ->
4948 showtext ' ' (describe_location ());
4950 | @w ->
4951 begin match state.layout with
4952 | [] -> ()
4953 | l :: _ ->
4954 Wsi.reshape l.pagew l.pageh;
4955 G.postRedisplay "w"
4958 | @apos ->
4959 enterbookmarkmode ()
4961 | @h | @F1 ->
4962 enterhelpmode ()
4964 | @i ->
4965 enterinfomode ()
4967 | @e when Buffer.length state.errmsgs > 0 ->
4968 entermsgsmode ()
4970 | @m ->
4971 let ondone s =
4972 match state.layout with
4973 | l :: _ ->
4974 if nonemptystr s
4975 then
4976 state.bookmarks <-
4977 (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4978 | _ -> ()
4980 enttext ("bookmark: ", E.s, None, textentry, ondone, true)
4982 | @tilde ->
4983 quickbookmark ();
4984 showtext ' ' "Quick bookmark added";
4986 | @z ->
4987 begin match state.layout with
4988 | l :: _ ->
4989 let rect = getpdimrect l.pagedimno in
4990 let w, h =
4991 if conf.crophack
4992 then
4993 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4994 truncate (1.2 *. (rect.(3) -. rect.(0))))
4995 else
4996 (truncate (rect.(1) -. rect.(0)),
4997 truncate (rect.(3) -. rect.(0)))
4999 let w = truncate ((float w)*.conf.zoom)
5000 and h = truncate ((float h)*.conf.zoom) in
5001 if w != 0 && h != 0
5002 then (
5003 state.anchor <- getanchor ();
5004 Wsi.reshape w (h + conf.interpagespace)
5006 G.postRedisplay "z";
5008 | [] -> ()
5011 | @x -> state.roam ()
5013 | @Lt | @Gt ->
5014 reqlayout (conf.angle +
5015 (if key = @Gt then 30 else -30)) conf.fitmodel
5017 | @Lb | @Rb ->
5018 conf.colorscale <-
5019 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5021 G.postRedisplay "brightness";
5023 | @c when state.mode = View ->
5024 if Wsi.withalt mask
5025 then (
5026 if conf.zoom > 1.0
5027 then
5028 let m = (state.winw - state.w) / 2 in
5029 gotoxy_and_clear_text m state.y
5031 else
5032 let (c, a, b), z =
5033 match state.prevcolumns with
5034 | None -> (1, 0, 0), 1.0
5035 | Some (columns, z) ->
5036 let cab =
5037 match columns with
5038 | Csplit (c, _) -> -c, 0, 0
5039 | Cmulti ((c, a, b), _) -> c, a, b
5040 | Csingle _ -> 1, 0, 0
5042 cab, z
5044 setcolumns View c a b;
5045 setzoom z
5047 | @down | @up when ctrl && Wsi.withshift mask ->
5048 let zoom, x = state.prevzoom in
5049 setzoom zoom;
5050 state.x <- x;
5052 | @k | @up | @kpup ->
5053 begin match state.autoscroll with
5054 | None ->
5055 begin match state.mode with
5056 | Birdseye beye -> upbirdseye 1 beye
5057 | Textentry _
5058 | View
5059 | LinkNav _ ->
5060 if ctrl
5061 then gotoxy_and_clear_text state.x (clamp ~-(state.winh/2))
5062 else (
5063 if not (Wsi.withshift mask) && conf.presentation
5064 then prevpage ()
5065 else gotoghyll1 true (clamp (-conf.scrollstep))
5068 | Some n ->
5069 setautoscrollspeed n false
5072 | @j | @down | @kpdown ->
5073 begin match state.autoscroll with
5074 | None ->
5075 begin match state.mode with
5076 | Birdseye beye -> downbirdseye 1 beye
5077 | Textentry _
5078 | View
5079 | LinkNav _ ->
5080 if ctrl
5081 then gotoxy_and_clear_text state.x (clamp (state.winh/2))
5082 else (
5083 if not (Wsi.withshift mask) && conf.presentation
5084 then nextpage ()
5085 else gotoghyll1 true (clamp (conf.scrollstep))
5088 | Some n ->
5089 setautoscrollspeed n true
5092 | @left | @right | @kpleft | @kpright when not (Wsi.withalt mask) ->
5093 if canpan ()
5094 then
5095 let dx =
5096 if ctrl
5097 then state.winw / 2
5098 else conf.hscrollstep
5100 let dx = if key = @left || key = @kpleft then dx else -dx in
5101 gotoxy_and_clear_text (panbound (state.x + dx)) state.y
5102 else (
5103 state.text <- E.s;
5104 G.postRedisplay "left/right"
5107 | @prior | @kpprior ->
5108 let y =
5109 if ctrl
5110 then
5111 match state.layout with
5112 | [] -> state.y
5113 | l :: _ -> state.y - l.pagey
5114 else
5115 clamp (pgscale (-state.winh))
5117 gotoghyll y
5119 | @next | @kpnext ->
5120 let y =
5121 if ctrl
5122 then
5123 match List.rev state.layout with
5124 | [] -> state.y
5125 | l :: _ -> getpagey l.pageno
5126 else
5127 clamp (pgscale state.winh)
5129 gotoghyll y
5131 | @g | @home | @kphome ->
5132 addnav ();
5133 gotoghyll 0
5134 | @G | @jend | @kpend ->
5135 addnav ();
5136 gotoghyll (clamp state.maxy)
5138 | @right | @kpright when Wsi.withalt mask ->
5139 gotoghyll (getnav 1)
5140 | @left | @kpleft when Wsi.withalt mask ->
5141 gotoghyll (getnav ~-1)
5143 | @r ->
5144 reload ()
5146 | @v when conf.debug ->
5147 state.rects <- [];
5148 List.iter (fun l ->
5149 match getopaque l.pageno with
5150 | None -> ()
5151 | Some opaque ->
5152 let x0, y0, x1, y1 = pagebbox opaque in
5153 let rect = (float x0, float y0,
5154 float x1, float y0,
5155 float x1, float y1,
5156 float x0, float y1) in
5157 debugrect rect;
5158 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
5159 state.rects <- (l.pageno, color, rect) :: state.rects;
5160 ) state.layout;
5161 G.postRedisplay "v";
5163 | @pipe ->
5164 let mode = state.mode in
5165 let cmd = ref E.s in
5166 let onleave = function
5167 | Cancel -> state.mode <- mode
5168 | Confirm ->
5169 List.iter (fun l ->
5170 match getopaque l.pageno with
5171 | Some opaque -> pipesel opaque !cmd
5172 | None -> ()) state.layout;
5173 state.mode <- mode
5175 let ondone s =
5176 cbput state.hists.sel s;
5177 cmd := s
5179 let te =
5180 "| ", !cmd, Some (onhist state.hists.sel), textentry, ondone, true
5182 G.postRedisplay "|";
5183 state.mode <- Textentry (te, onleave);
5185 | _ ->
5186 vlog "huh? %s" (Wsi.keyname key)
5189 let linknavkeyboard key mask linknav =
5190 let getpage pageno =
5191 let rec loop = function
5192 | [] -> None
5193 | l :: _ when l.pageno = pageno -> Some l
5194 | _ :: rest -> loop rest
5195 in loop state.layout
5197 let doexact (pageno, n) =
5198 match getopaque pageno, getpage pageno with
5199 | Some opaque, Some l ->
5200 if key = @enter || key = @kpenter
5201 then
5202 let under = getlink opaque n in
5203 G.postRedisplay "link gotounder";
5204 gotounder under;
5205 state.mode <- View;
5206 else
5207 let opt, dir =
5208 match key with
5209 | @home ->
5210 Some (findlink opaque LDfirst), -1
5212 | @jend ->
5213 Some (findlink opaque LDlast), 1
5215 | @left ->
5216 Some (findlink opaque (LDleft n)), -1
5218 | @right ->
5219 Some (findlink opaque (LDright n)), 1
5221 | @up ->
5222 Some (findlink opaque (LDup n)), -1
5224 | @down ->
5225 Some (findlink opaque (LDdown n)), 1
5227 | _ -> None, 0
5229 let pwl l dir =
5230 begin match findpwl l.pageno dir with
5231 | Pwlnotfound -> ()
5232 | Pwl pageno ->
5233 let notfound dir =
5234 state.mode <- LinkNav (Ltgendir dir);
5235 let y, h = getpageyh pageno in
5236 let y =
5237 if dir < 0
5238 then y + h - state.winh
5239 else y
5241 gotoxy state.x y
5243 begin match getopaque pageno, getpage pageno with
5244 | Some opaque, Some _ ->
5245 let link =
5246 let ld = if dir > 0 then LDfirst else LDlast in
5247 findlink opaque ld
5249 begin match link with
5250 | Lfound m ->
5251 showlinktype (getlink opaque m);
5252 state.mode <- LinkNav (Ltexact (pageno, m));
5253 G.postRedisplay "linknav jpage";
5254 | Lnotfound -> notfound dir
5255 end;
5256 | _ -> notfound dir
5257 end;
5258 end;
5260 begin match opt with
5261 | Some Lnotfound -> pwl l dir;
5262 | Some (Lfound m) ->
5263 if m = n
5264 then pwl l dir
5265 else (
5266 let _, y0, _, y1 = getlinkrect opaque m in
5267 if y0 < l.pagey
5268 then gotopage1 l.pageno y0
5269 else (
5270 let d = fstate.fontsize + 1 in
5271 if y1 - l.pagey > l.pagevh - d
5272 then gotopage1 l.pageno (y1 - state.winh + d)
5273 else G.postRedisplay "linknav";
5275 showlinktype (getlink opaque m);
5276 state.mode <- LinkNav (Ltexact (l.pageno, m));
5279 | None -> viewkeyboard key mask
5280 end;
5281 | _ -> viewkeyboard key mask
5283 if key = @insert
5284 then (
5285 state.mode <- View;
5286 G.postRedisplay "leave linknav"
5288 else
5289 match linknav with
5290 | Ltgendir _ | Ltnotready _ -> viewkeyboard key mask
5291 | Ltexact exact -> doexact exact
5294 let keyboard key mask =
5295 if (key = @g && Wsi.withctrl mask) && not (istextentry state.mode)
5296 then wcmd "interrupt"
5297 else state.uioh <- state.uioh#key key mask
5300 let birdseyekeyboard key mask
5301 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5302 let incr =
5303 match conf.columns with
5304 | Csingle _ -> 1
5305 | Cmulti ((c, _, _), _) -> c
5306 | Csplit _ -> failwith "bird's eye split mode"
5308 let pgh layout = List.fold_left
5309 (fun m l -> max l.pageh m) state.winh layout in
5310 match key with
5311 | @l when Wsi.withctrl mask ->
5312 let y, h = getpageyh pageno in
5313 let top = (state.winh - h) / 2 in
5314 gotoxy state.x (max 0 (y - top))
5315 | @enter | @kpenter -> leavebirdseye beye false
5316 | @escape -> leavebirdseye beye true
5317 | @up -> upbirdseye incr beye
5318 | @down -> downbirdseye incr beye
5319 | @left -> upbirdseye 1 beye
5320 | @right -> downbirdseye 1 beye
5322 | @prior ->
5323 begin match state.layout with
5324 | l :: _ ->
5325 if l.pagey != 0
5326 then (
5327 state.mode <- Birdseye (
5328 oconf, leftx, l.pageno, hooverpageno, anchor
5330 gotopage1 l.pageno 0;
5332 else (
5333 let layout = layout state.x (state.y-state.winh)
5334 state.winw
5335 (pgh state.layout) in
5336 match layout with
5337 | [] -> gotoxy state.x (clamp (-state.winh))
5338 | l :: _ ->
5339 state.mode <- Birdseye (
5340 oconf, leftx, l.pageno, hooverpageno, anchor
5342 gotopage1 l.pageno 0
5345 | [] -> gotoxy state.x (clamp (-state.winh))
5346 end;
5348 | @next ->
5349 begin match List.rev state.layout with
5350 | l :: _ ->
5351 let layout = layout state.x
5352 (state.y + (pgh state.layout))
5353 state.winw state.winh in
5354 begin match layout with
5355 | [] ->
5356 let incr = l.pageh - l.pagevh in
5357 if incr = 0
5358 then (
5359 state.mode <-
5360 Birdseye (
5361 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5363 G.postRedisplay "birdseye pagedown";
5365 else gotoxy state.x (clamp (incr + conf.interpagespace*2));
5367 | l :: _ ->
5368 state.mode <-
5369 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5370 gotopage1 l.pageno 0;
5373 | [] -> gotoxy state.x (clamp state.winh)
5374 end;
5376 | @home ->
5377 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5378 gotopage1 0 0
5380 | @jend ->
5381 let pageno = state.pagecount - 1 in
5382 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5383 if not (pagevisible state.layout pageno)
5384 then
5385 let h =
5386 match List.rev state.pdims with
5387 | [] -> state.winh
5388 | (_, _, h, _) :: _ -> h
5390 gotoxy
5391 state.x
5392 (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5393 else G.postRedisplay "birdseye end";
5395 | _ -> viewkeyboard key mask
5398 let drawpage l =
5399 let color =
5400 match state.mode with
5401 | Textentry _ -> scalecolor 0.4
5402 | LinkNav _
5403 | View -> scalecolor 1.0
5404 | Birdseye (_, _, pageno, hooverpageno, _) ->
5405 if l.pageno = hooverpageno
5406 then scalecolor 0.9
5407 else (
5408 if l.pageno = pageno
5409 then (
5410 let c = scalecolor 1.0 in
5411 GlDraw.color c;
5412 GlDraw.line_width 3.0;
5413 let dispx = l.pagedispx in
5414 linerect
5415 (float (dispx-1)) (float (l.pagedispy-1))
5416 (float (dispx+l.pagevw+1))
5417 (float (l.pagedispy+l.pagevh+1))
5419 GlDraw.line_width 1.0;
5422 else scalecolor 0.8
5425 drawtiles l color;
5428 let postdrawpage l linkindexbase =
5429 match getopaque l.pageno with
5430 | Some opaque ->
5431 if tileready l l.pagex l.pagey
5432 then
5433 let x = l.pagedispx - l.pagex
5434 and y = l.pagedispy - l.pagey in
5435 let hlmask =
5436 match conf.columns with
5437 | Csingle _ | Cmulti _ ->
5438 (if conf.hlinks then 1 else 0)
5439 + (if state.glinks
5440 && not (isbirdseye state.mode) then 2 else 0)
5441 | Csplit _ -> 0
5443 let s =
5444 match state.mode with
5445 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5446 | Textentry _
5447 | Birdseye _
5448 | View
5449 | LinkNav _ -> E.s
5451 Hashtbl.find_all state.prects l.pageno |>
5452 List.iter (fun vals -> drawprect opaque x y vals);
5453 let n = postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize) in
5454 if n < 0
5455 then (state.redisplay <- true; 0)
5456 else n
5457 else 0
5458 | _ -> 0
5461 let scrollindicator () =
5462 let sbw, ph, sh = state.uioh#scrollph in
5463 let sbh, pw, sw = state.uioh#scrollpw in
5465 let x0,x1,hx0 =
5466 if conf.leftscroll
5467 then (0, sbw, sbw)
5468 else ((state.winw - sbw), state.winw, 0)
5471 Gl.enable `blend;
5472 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5473 GlDraw.color (0.64, 0.64, 0.64) ~alpha:0.7;
5474 filledrect (float x0) 0. (float x1) (float state.winh);
5475 filledrect
5476 (float hx0) (float (state.winh - sbh))
5477 (float (hx0 + state.winw)) (float state.winh)
5479 GlDraw.color (0.0, 0.0, 0.0) ~alpha:0.7;
5481 filledrect (float x0) ph (float x1) (ph +. sh);
5482 let pw = pw +. float hx0 in
5483 filledrect pw (float (state.winh - sbh)) (pw +. sw) (float state.winh);
5484 Gl.disable `blend;
5487 let showsel () =
5488 match state.mstate with
5489 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5492 | Msel ((x0, y0), (x1, y1)) ->
5493 let identify opaque l px py = Some (opaque, l.pageno, px, py) in
5494 let o0,n0,px0,py0 = onppundermouse identify x0 y0 (~< E.s, -1, 0, 0) in
5495 let _o1,n1,px1,py1 = onppundermouse identify x1 y1 (~< E.s, -1, 0, 0) in
5496 if n0 != -1 && n0 = n1 then seltext o0 (px0, py0, px1, py1);
5499 let showrects = function [] -> () | rects ->
5500 Gl.enable `blend;
5501 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5502 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5503 List.iter
5504 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5505 List.iter (fun l ->
5506 if l.pageno = pageno
5507 then (
5508 let dx = float (l.pagedispx - l.pagex) in
5509 let dy = float (l.pagedispy - l.pagey) in
5510 let r, g, b, alpha = c in
5511 GlDraw.color (r, g, b) ~alpha;
5512 filledrect2 (x0+.dx) (y0+.dy)
5513 (x1+.dx) (y1+.dy)
5514 (x3+.dx) (y3+.dy)
5515 (x2+.dx) (y2+.dy);
5517 ) state.layout
5518 ) rects
5520 Gl.disable `blend;
5523 let display () =
5524 begin match conf.columns, state.layout with
5525 | Csingle _, _ :: _ ->
5526 GlDraw.color (scalecolor2 conf.bgcolor);
5527 let y =
5528 List.fold_left (fun y l ->
5529 let x0 = 0 in
5530 let y0 = y in
5531 let x1 = l.pagedispx in
5532 let y1 = (l.pagedispy + l.pagevh) in
5533 filledrect (float x0) (float y0) (float x1) (float y1);
5534 let x0 = x1 + l.pagevw in
5535 let x1 = state.winw in
5536 filledrect1 (float x0) (float y0) (float x1) (float y1);
5537 if y != l.pagedispy
5538 then (
5539 let x0 = 0
5540 and x1 = state.winw in
5541 let y0 = y
5542 and y1 = l.pagedispy in
5543 filledrect1 (float x0) (float y0) (float x1) (float y1);
5545 l.pagedispy + l.pagevh) 0 state.layout
5547 let x0 = 0
5548 and x1 = state.winw in
5549 let y0 = y
5550 and y1 = state.winh in
5551 filledrect1 (float x0) (float y0) (float x1) (float y1)
5552 | (Cmulti _ | Csplit _), _ | Csingle _, [] ->
5553 GlClear.color (scalecolor2 conf.bgcolor);
5554 GlClear.clear [`color];
5555 end;
5556 List.iter drawpage state.layout;
5557 let rects =
5558 match state.mode with
5559 | LinkNav (Ltexact (pageno, linkno)) ->
5560 begin match getopaque pageno with
5561 | Some opaque ->
5562 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5563 let color = (0.0, 0.0, 0.5, 0.5) in
5564 (pageno, color, (
5565 float x0, float y0,
5566 float x1, float y0,
5567 float x1, float y1,
5568 float x0, float y1)
5569 ) :: state.rects
5570 | None -> state.rects
5572 | LinkNav (Ltgendir _) | LinkNav (Ltnotready _)
5573 | Birdseye _
5574 | Textentry _
5575 | View -> state.rects
5577 showrects rects;
5578 let rec postloop linkindexbase = function
5579 | l :: rest ->
5580 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5581 postloop linkindexbase rest
5582 | [] -> ()
5584 showsel ();
5585 postloop 0 state.layout;
5586 state.uioh#display;
5587 begin match state.mstate with
5588 | Mzoomrect ((x0, y0), (x1, y1)) ->
5589 Gl.enable `blend;
5590 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5591 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5592 filledrect (float x0) (float y0) (float x1) (float y1);
5593 Gl.disable `blend;
5594 | Msel _
5595 | Mpan _
5596 | Mscrolly | Mscrollx
5597 | Mzoom _
5598 | Mnone -> ()
5599 end;
5600 enttext ();
5601 scrollindicator ();
5602 Wsi.swapb ();
5605 let zoomrect x y x1 y1 =
5606 let x0 = min x x1
5607 and x1 = max x x1
5608 and y0 = min y y1 in
5609 let zoom = (float state.w) /. float (x1 - x0) in
5610 let margin =
5611 let simple () =
5612 if state.w < state.winw
5613 then (state.winw - state.w) / 2
5614 else 0
5616 match conf.fitmodel with
5617 | FitWidth | FitProportional -> simple ()
5618 | FitPage ->
5619 match conf.columns with
5620 | Csplit _ ->
5621 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
5622 | Cmulti _ | Csingle _ -> simple ()
5624 gotoxy ((state.x + margin) - x0) (state.y + y0);
5625 state.anchor <- getanchor ();
5626 setzoom zoom;
5627 resetmstate ();
5630 let annot inline x y =
5631 match unproject x y with
5632 | Some (opaque, n, ux, uy) ->
5633 let add text =
5634 addannot opaque ux uy text;
5635 wcmd "freepage %s" (~> opaque);
5636 Hashtbl.remove state.pagemap (n, state.gen);
5637 flushtiles ();
5638 gotoxy state.x state.y
5640 if inline
5641 then
5642 let ondone s = add s in
5643 let mode = state.mode in
5644 state.mode <- Textentry (
5645 ("annotation: ", E.s, None, textentry, ondone, true),
5646 fun _ -> state.mode <- mode);
5647 state.text <- E.s;
5648 enttext ();
5649 G.postRedisplay "annot"
5650 else
5651 add @@ getusertext E.s
5652 | _ -> ()
5655 let zoomblock x y =
5656 let g opaque l px py =
5657 match rectofblock opaque px py with
5658 | Some a ->
5659 let x0 = a.(0) -. 20. in
5660 let x1 = a.(1) +. 20. in
5661 let y0 = a.(2) -. 20. in
5662 let zoom = (float state.w) /. (x1 -. x0) in
5663 let pagey = getpagey l.pageno in
5664 let margin = (state.w - l.pagew)/2 in
5665 let nx = -truncate x0 - margin in
5666 gotoxy_and_clear_text nx (pagey + truncate y0);
5667 state.anchor <- getanchor ();
5668 setzoom zoom;
5669 None
5670 | None -> None
5672 match conf.columns with
5673 | Csplit _ ->
5674 impmsg "block zooming does not work properly in split columns mode"
5675 | Cmulti _ | Csingle _ -> onppundermouse g x y ()
5678 let scrollx x =
5679 let winw = state.winw - 1 in
5680 let s = float x /. float winw in
5681 let destx = truncate (float (state.w + winw) *. s) in
5682 gotoxy_and_clear_text (winw - destx) state.y;
5683 state.mstate <- Mscrollx;
5686 let scrolly y =
5687 let s = float y /. float state.winh in
5688 let desty = truncate (float (state.maxy -
5689 (if conf.maxhfit then state.winh else 0))
5690 *. s) in
5691 gotoxy_and_clear_text state.x desty;
5692 state.mstate <- Mscrolly;
5695 let viewmulticlick clicks x y mask =
5696 let g opaque l px py =
5697 let mark =
5698 match clicks with
5699 | 2 -> Mark_word
5700 | 3 -> Mark_line
5701 | 4 -> Mark_block
5702 | _ -> Mark_page
5704 if markunder opaque px py mark
5705 then (
5706 Some (fun () ->
5707 let dopipe cmd =
5708 match getopaque l.pageno with
5709 | None -> ()
5710 | Some opaque -> pipesel opaque cmd
5712 state.roam <- (fun () -> dopipe conf.paxcmd);
5713 if not (Wsi.withctrl mask) then dopipe conf.selcmd;
5716 else None
5718 G.postRedisplay "viewmulticlick";
5719 onppundermouse g x y (fun () -> impmsg "nothing to select") ();
5722 let canselect () =
5723 match conf.columns with
5724 | Csplit _ -> false
5725 | Csingle _ | Cmulti _ -> conf.angle mod 360 = 0
5728 let viewmouse button down x y mask =
5729 match button with
5730 | n when (n == 4 || n == 5) && not down ->
5731 if Wsi.withctrl mask
5732 then (
5733 match state.mstate with
5734 | Mzoom (oldn, i, (ftx, fty)) ->
5735 let recenter =
5736 if false
5737 then abs (ftx - x) > 5 || abs (fty - y) > 5
5738 else false
5740 if oldn = n
5741 then (
5742 if i = 2
5743 then
5744 let incr =
5745 match n with
5746 | 5 ->
5747 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5748 | _ ->
5749 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5751 let zoom = conf.zoom -. incr in
5752 if recenter
5753 then pivotzoom ~x ~y zoom
5754 else pivotzoom zoom;
5755 state.mstate <- Mzoom (n, 0, (x, y));
5756 else
5757 state.mstate <- Mzoom (n, i+1, (ftx, fty));
5759 else state.mstate <- Mzoom (n, 0, (ftx, fty))
5761 | Msel _
5762 | Mpan _
5763 | Mscrolly | Mscrollx
5764 | Mzoomrect _
5765 | Mnone -> state.mstate <- Mzoom (n, 0, (0, 0))
5767 else (
5768 match state.autoscroll with
5769 | Some step -> setautoscrollspeed step (n=4)
5770 | None ->
5771 if conf.wheelbypage || conf.presentation
5772 then (
5773 if n = 4
5774 then prevpage ()
5775 else nextpage ()
5777 else
5778 let incr =
5779 if n = 4
5780 then -conf.scrollstep
5781 else conf.scrollstep
5783 let incr = incr * 2 in
5784 let y = clamp incr in
5785 gotoxy_and_clear_text state.x y
5788 | n when (n = 6 || n = 7) && not down && canpan () ->
5789 let x =
5790 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep) in
5791 gotoxy_and_clear_text x state.y
5793 | 1 when Wsi.withshift mask ->
5794 state.mstate <- Mnone;
5795 if not down
5796 then (
5797 match unproject x y with
5798 | None -> ()
5799 | Some (_, pageno, ux, uy) ->
5800 let cmd = Printf.sprintf
5801 "%s %s %d %d %d"
5802 conf.stcmd state.path pageno ux uy
5804 match spawn cmd [] with
5805 | (exception exn) ->
5806 impmsg "execution of synctex command(%S) failed: %S"
5807 conf.stcmd @@ exntos exn
5808 | _pid -> ()
5811 | 1 when Wsi.withctrl mask ->
5812 if down
5813 then (
5814 Wsi.setcursor Wsi.CURSOR_FLEUR;
5815 state.mstate <- Mpan (x, y)
5817 else
5818 state.mstate <- Mnone
5820 | 3 ->
5821 if down
5822 then (
5823 if Wsi.withshift mask
5824 then (
5825 annot conf.annotinline x y;
5826 G.postRedisplay "addannot"
5828 else
5829 let p = (x, y) in
5830 Wsi.setcursor Wsi.CURSOR_CYCLE;
5831 state.mstate <- Mzoomrect (p, p)
5833 else (
5834 match state.mstate with
5835 | Mzoomrect ((x0, y0), _) ->
5836 if abs (x-x0) > 10 && abs (y - y0) > 10
5837 then zoomrect x0 y0 x y
5838 else (
5839 resetmstate ();
5840 G.postRedisplay "kill accidental zoom rect";
5842 | Msel _
5843 | Mpan _
5844 | Mscrolly | Mscrollx
5845 | Mzoom _
5846 | Mnone ->
5847 resetmstate ()
5850 | 1 when vscrollhit x ->
5851 if down
5852 then
5853 let _, position, sh = state.uioh#scrollph in
5854 if y > truncate position && y < truncate (position +. sh)
5855 then state.mstate <- Mscrolly
5856 else scrolly y
5857 else
5858 state.mstate <- Mnone
5860 | 1 when y > state.winh - hscrollh () ->
5861 if down
5862 then
5863 let _, position, sw = state.uioh#scrollpw in
5864 if x > truncate position && x < truncate (position +. sw)
5865 then state.mstate <- Mscrollx
5866 else scrollx x
5867 else
5868 state.mstate <- Mnone
5870 | 1 when state.bzoom -> if not down then zoomblock x y
5872 | 1 ->
5873 let dest = if down then getunder x y else Unone in
5874 begin match dest with
5875 | Ulinkuri _ ->
5876 gotounder dest
5878 | Unone when down ->
5879 Wsi.setcursor Wsi.CURSOR_FLEUR;
5880 state.mstate <- Mpan (x, y);
5882 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
5884 | Unone | Utext _ ->
5885 if down
5886 then (
5887 if canselect ()
5888 then (
5889 state.mstate <- Msel ((x, y), (x, y));
5890 G.postRedisplay "mouse select";
5893 else (
5894 match state.mstate with
5895 | Mnone -> ()
5897 | Mzoom _ | Mscrollx | Mscrolly ->
5898 state.mstate <- Mnone
5900 | Mzoomrect ((x0, y0), _) ->
5901 zoomrect x0 y0 x y
5903 | Mpan _ ->
5904 Wsi.setcursor Wsi.CURSOR_INHERIT;
5905 state.mstate <- Mnone
5907 | Msel ((x0, y0), (x1, y1)) ->
5908 let rec loop = function
5909 | [] -> ()
5910 | l :: rest ->
5911 let inside =
5912 let a0 = l.pagedispy in
5913 let a1 = a0 + l.pagevh in
5914 let b0 = l.pagedispx in
5915 let b1 = b0 + l.pagevw in
5916 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5917 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5919 if inside
5920 then
5921 match getopaque l.pageno with
5922 | Some opaque ->
5923 let dosel cmd () =
5924 match Unix.pipe () with
5925 | (exception exn) ->
5926 impmsg "cannot create sel pipe: %s" @@
5927 exntos exn;
5928 | (r, w) ->
5929 let clo what fd =
5930 Ne.clo fd (fun msg ->
5931 dolog "%s close failed: %s" what msg)
5933 let pid =
5934 try spawn cmd [r, 0; w, -1]
5935 with exn ->
5936 dolog "cannot execute %S: %s"
5937 cmd @@ exntos exn;
5940 if pid > 0
5941 then (
5942 copysel w opaque;
5943 G.postRedisplay "copysel";
5945 else clo "Msel pipe/w" w;
5946 clo "Msel pipe/r" r;
5948 dosel conf.selcmd ();
5949 state.roam <- dosel conf.paxcmd;
5950 | None -> ()
5951 else loop rest
5953 loop state.layout;
5954 resetmstate ();
5958 | _ -> ()
5961 let birdseyemouse button down x y mask
5962 (conf, leftx, _, hooverpageno, anchor) =
5963 match button with
5964 | 1 when down ->
5965 let rec loop = function
5966 | [] -> ()
5967 | l :: rest ->
5968 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5969 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5970 then (
5971 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5973 else loop rest
5975 loop state.layout
5976 | 3 -> ()
5977 | _ -> viewmouse button down x y mask
5980 let uioh = object
5981 method display = ()
5983 method key key mask =
5984 begin match state.mode with
5985 | Textentry textentry -> textentrykeyboard key mask textentry
5986 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5987 | View -> viewkeyboard key mask
5988 | LinkNav linknav -> linknavkeyboard key mask linknav
5989 end;
5990 state.uioh
5992 method button button bstate x y mask =
5993 begin match state.mode with
5994 | LinkNav _
5995 | View -> viewmouse button bstate x y mask
5996 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5997 | Textentry _ -> ()
5998 end;
5999 state.uioh
6001 method multiclick clicks x y mask =
6002 begin match state.mode with
6003 | LinkNav _
6004 | View -> viewmulticlick clicks x y mask
6005 | Birdseye _
6006 | Textentry _ -> ()
6007 end;
6008 state.uioh
6010 method motion x y =
6011 begin match state.mode with
6012 | Textentry _ -> ()
6013 | View | Birdseye _ | LinkNav _ ->
6014 match state.mstate with
6015 | Mzoom _ | Mnone -> ()
6017 | Mpan (x0, y0) ->
6018 let dx = x - x0
6019 and dy = y0 - y in
6020 state.mstate <- Mpan (x, y);
6021 let x = if canpan () then panbound (state.x + dx) else state.x in
6022 let y = clamp dy in
6023 gotoxy_and_clear_text x y
6025 | Msel (a, _) ->
6026 state.mstate <- Msel (a, (x, y));
6027 G.postRedisplay "motion select";
6029 | Mscrolly ->
6030 let y = min state.winh (max 0 y) in
6031 scrolly y
6033 | Mscrollx ->
6034 let x = min state.winw (max 0 x) in
6035 scrollx x
6037 | Mzoomrect (p0, _) ->
6038 state.mstate <- Mzoomrect (p0, (x, y));
6039 G.postRedisplay "motion zoomrect";
6040 end;
6041 state.uioh
6043 method pmotion x y =
6044 begin match state.mode with
6045 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6046 let rec loop = function
6047 | [] ->
6048 if hooverpageno != -1
6049 then (
6050 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6051 G.postRedisplay "pmotion birdseye no hoover";
6053 | l :: rest ->
6054 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6055 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6056 then (
6057 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6058 G.postRedisplay "pmotion birdseye hoover";
6060 else loop rest
6062 loop state.layout
6064 | Textentry _ -> ()
6066 | LinkNav _
6067 | View ->
6068 match state.mstate with
6069 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ -> ()
6070 | Mnone ->
6071 updateunder x y;
6072 if canselect ()
6073 then
6074 match conf.pax with
6075 | None -> ()
6076 | Some r ->
6077 let past, _, _ = !r in
6078 let now = now () in
6079 let delta = now -. past in
6080 if delta > 0.01
6081 then paxunder x y
6082 else r := (now, x, y)
6083 end;
6084 state.uioh
6086 method infochanged _ = ()
6088 method scrollph =
6089 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6090 let p, h =
6091 if maxy = 0
6092 then 0.0, float state.winh
6093 else scrollph state.y maxy
6095 vscrollw (), p, h
6097 method scrollpw =
6098 let fwinw = float (state.winw - vscrollw ()) in
6099 let sw =
6100 let sw = fwinw /. float state.w in
6101 let sw = fwinw *. sw in
6102 max sw (float conf.scrollh)
6104 let position =
6105 let maxx = state.w + state.winw in
6106 let x = state.winw - state.x in
6107 let percent = float x /. float maxx in
6108 (fwinw -. sw) *. percent
6110 hscrollh (), position, sw
6112 method modehash =
6113 let modename =
6114 match state.mode with
6115 | LinkNav _ -> "links"
6116 | Textentry _ -> "textentry"
6117 | Birdseye _ -> "birdseye"
6118 | View -> "view"
6120 findkeyhash conf modename
6122 method eformsgs = true
6123 method alwaysscrolly = false
6124 end;;
6126 let addrect pageno r g b a x0 y0 x1 y1 =
6127 Hashtbl.add state.prects pageno [|r; g; b; a; x0; y0; x1; y1|];
6130 let ract cmds =
6131 let cl = splitatchar cmds ' ' in
6132 let scan s fmt f =
6133 try Scanf.sscanf s fmt f
6134 with exn ->
6135 adderrfmt "remote exec"
6136 "error processing '%S': %s\n" cmds @@ exntos exn
6138 let rectx s pageno (r, g, b, a) x0 y0 x1 y1 =
6139 vlog "%s page %d color (%f %f %f %f) x0,y0,x1,y1 = %f %f %f %f"
6140 s pageno r g b a x0 y0 x1 y1;
6141 onpagerect
6142 pageno
6143 (fun w h ->
6144 let _,w1,h1,_ = getpagedim pageno in
6145 let sw = float w1 /. float w
6146 and sh = float h1 /. float h in
6147 let x0s = x0 *. sw
6148 and x1s = x1 *. sw
6149 and y0s = y0 *. sh
6150 and y1s = y1 *. sh in
6151 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
6152 let color = (r, g, b, a) in
6153 if conf.verbose then debugrect rect;
6154 state.rects <- (pageno, color, rect) :: state.rects;
6155 G.postRedisplay s;
6158 match cl with
6159 | "reload", "" -> reload ()
6160 | "goto", args ->
6161 scan args "%u %f %f"
6162 (fun pageno x y ->
6163 let cmd, _ = state.geomcmds in
6164 if emptystr cmd
6165 then gotopagexy !wtmode pageno x y
6166 else
6167 let f prevf () =
6168 gotopagexy !wtmode pageno x y;
6169 prevf ()
6171 state.reprf <- f state.reprf
6173 | "goto1", args -> scan args "%u %f" gotopage
6174 | "gotor", args ->
6175 scan args "%S %u"
6176 (fun _filename _pageno ->
6177 failwith "gotounder (Uremote (filename, pageno))")
6178 | "gotord", args ->
6179 scan args "%S %S"
6180 (fun _filename _dest ->
6181 failwith "gotounder (Uremotedest (filename, dest))")
6182 | "rect", args ->
6183 scan args "%u %u %f %f %f %f"
6184 (fun pageno c x0 y0 x1 y1 ->
6185 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
6186 rectx "rect" pageno color x0 y0 x1 y1;
6188 | "prect", args ->
6189 scan args "%u %f %f %f %f %f %f %f %f"
6190 (fun pageno r g b alpha x0 y0 x1 y1 ->
6191 addrect pageno r g b alpha x0 y0 x1 y1;
6192 G.postRedisplay "prect"
6194 | "pgoto", args ->
6195 scan args "%u %f %f"
6196 (fun pageno x y ->
6197 let optopaque =
6198 match getopaque pageno with
6199 | Some opaque -> opaque
6200 | None -> ~< E.s
6202 pgoto optopaque pageno x y;
6203 let rec fixx = function
6204 | [] -> ()
6205 | l :: rest ->
6206 if l.pageno = pageno
6207 then gotoxy (state.x - l.pagedispx) state.y
6208 else fixx rest
6210 let layout =
6211 let mult =
6212 match conf.columns with
6213 | Csingle _ | Csplit _ -> 1
6214 | Cmulti ((n, _, _), _) -> n
6216 layout 0 state.y (state.winw * mult) state.winh
6218 fixx layout
6220 | "activatewin", "" -> Wsi.activatewin ()
6221 | "quit", "" -> raise Quit
6222 | "keys", keys ->
6223 begin try
6224 let l = Config.keys_of_string keys in
6225 List.iter (fun (k, m) -> keyboard k m) l
6226 with exn ->
6227 adderrfmt "error processing keys" "`%S': %s\n" cmds @@ exntos exn
6229 | "clearrects", "" ->
6230 Hashtbl.clear state.prects;
6231 G.postRedisplay "clearrects"
6232 | _ ->
6233 adderrfmt "remote command"
6234 "error processing remote command: %S\n" cmds;
6237 let remote =
6238 let scratch = Bytes.create 80 in
6239 let buf = Buffer.create 80 in
6240 fun fd ->
6241 match tempfailureretry (Unix.read fd scratch 0) 80 with
6242 | (exception Unix.Unix_error (Unix.EAGAIN, _, _)) -> None
6243 | 0 ->
6244 Unix.close fd;
6245 if Buffer.length buf > 0
6246 then (
6247 let s = Buffer.contents buf in
6248 Buffer.clear buf;
6249 ract s;
6251 None
6252 | n ->
6253 let rec eat ppos =
6254 let nlpos =
6255 match Bytes.index_from scratch ppos '\n' with
6256 | pos -> if pos >= n then -1 else pos
6257 | (exception Not_found) -> -1
6259 if nlpos >= 0
6260 then (
6261 Buffer.add_subbytes buf scratch ppos (nlpos-ppos);
6262 let s = Buffer.contents buf in
6263 Buffer.clear buf;
6264 ract s;
6265 eat (nlpos+1);
6267 else (
6268 Buffer.add_subbytes buf scratch ppos (n-ppos);
6269 Some fd
6271 in eat 0
6274 let remoteopen path =
6275 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
6276 with exn ->
6277 adderrfmt "remoteopen" "error opening %S: %s" path @@ exntos exn;
6278 None
6281 let () =
6282 let gcconfig = ref E.s in
6283 let trimcachepath = ref E.s in
6284 let rcmdpath = ref E.s in
6285 let pageno = ref None in
6286 let rootwid = ref 0 in
6287 let openlast = ref false in
6288 let nofc = ref false in
6289 let doreap = ref false in
6290 selfexec := Sys.executable_name;
6291 Arg.parse
6292 (Arg.align
6293 [("-p", Arg.String (fun s -> state.password <- s),
6294 "<password> Set password");
6296 ("-f", Arg.String
6297 (fun s ->
6298 Config.fontpath := s;
6299 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
6301 "<path> Set path to the user interface font");
6303 ("-c", Arg.String
6304 (fun s ->
6305 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
6306 Config.confpath := s),
6307 "<path> Set path to the configuration file");
6309 ("-last", Arg.Set openlast, " Open last document");
6311 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
6312 "<page-number> Jump to page");
6314 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6315 "<path> Set path to the trim cache file");
6317 ("-dest", Arg.String (fun s -> state.nameddest <- s),
6318 "<named-destination> Set named destination");
6320 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
6321 ("-cxack", Arg.Set cxack, " Cut corners");
6323 ("-remote", Arg.String (fun s -> rcmdpath := s),
6324 "<path> Set path to the remote commands source");
6326 ("-origin", Arg.String (fun s -> state.origin <- s),
6327 "<original-path> Set original path");
6329 ("-gc", Arg.Set_string gcconfig,
6330 "<script-path> Collect garbage with the help of a script");
6332 ("-nofc", Arg.Set nofc, " Do not use fontconfig");
6334 ("-v", Arg.Unit (fun () ->
6335 Printf.printf
6336 "%s\nconfiguration path: %s\n"
6337 (version ())
6338 Config.defconfpath
6340 exit 0), " Print version and exit");
6342 ("-embed", Arg.Set_int rootwid,
6343 "<window-id> Embed into window")
6346 (fun s -> state.path <- s)
6347 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6349 if !wtmode
6350 then selfexec := !selfexec ^ " -wtmode";
6352 let histmode = emptystr state.path && not !openlast in
6354 if not (Config.load !openlast)
6355 then dolog "failed to load configuration";
6357 begin match !pageno with
6358 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
6359 | None -> ()
6360 end;
6362 if nonemptystr !gcconfig
6363 then (
6364 let (c, s) =
6365 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6366 | (exception exn) -> error "socketpair for gc failed: %s" @@ exntos exn
6367 | fds -> fds
6369 match spawn !gcconfig [(c, 0); (c, 1); (s, -1)] with
6370 | (exception exn) -> error "failed to execute gc script: %s" @@ exntos exn
6371 | _pid ->
6372 Ne.clo c @@ (fun s -> error "failed to close gc fd %s" s);
6373 Config.gc s;
6374 exit 0
6377 let wsfd, winw, winh = Wsi.init (object (self)
6378 val mutable m_clicks = 0
6379 val mutable m_click_x = 0
6380 val mutable m_click_y = 0
6381 val mutable m_lastclicktime = infinity
6383 method private cleanup =
6384 state.roam <- noroam;
6385 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap
6386 method expose = G.postRedisplay "expose"
6387 method visible v =
6388 let name =
6389 match v with
6390 | Wsi.Unobscured -> "unobscured"
6391 | Wsi.PartiallyObscured -> "partiallyobscured"
6392 | Wsi.FullyObscured -> "fullyobscured"
6394 vlog "visibility change %s" name
6395 method display = display ()
6396 method map mapped = vlog "mapped %b" mapped
6397 method reshape w h =
6398 self#cleanup;
6399 reshape w h
6400 method mouse b d x y m =
6401 if d && canselect ()
6402 then (
6403 (* http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx *)
6404 m_click_x <- x;
6405 m_click_y <- y;
6406 if b = 1
6407 then (
6408 let t = now () in
6409 if abs x - m_click_x > 10
6410 || abs y - m_click_y > 10
6411 || abs_float (t -. m_lastclicktime) > 0.3
6412 then m_clicks <- 0;
6413 m_clicks <- m_clicks + 1;
6414 m_lastclicktime <- t;
6415 if m_clicks = 1
6416 then (
6417 self#cleanup;
6418 G.postRedisplay "cleanup";
6419 state.uioh <- state.uioh#button b d x y m;
6421 else state.uioh <- state.uioh#multiclick m_clicks x y m
6423 else (
6424 self#cleanup;
6425 m_clicks <- 0;
6426 m_lastclicktime <- infinity;
6427 state.uioh <- state.uioh#button b d x y m
6430 else (
6431 state.uioh <- state.uioh#button b d x y m
6433 method motion x y =
6434 state.mpos <- (x, y);
6435 state.uioh <- state.uioh#motion x y
6436 method pmotion x y =
6437 state.mpos <- (x, y);
6438 state.uioh <- state.uioh#pmotion x y
6439 method key k m =
6440 let mascm = m land (
6441 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6442 ) in
6443 let keyboard k m =
6444 let x = state.x and y = state.y in
6445 keyboard k m;
6446 if x != state.x || y != state.y then self#cleanup
6448 match state.keystate with
6449 | KSnone ->
6450 let km = k, mascm in
6451 begin
6452 match
6453 let modehash = state.uioh#modehash in
6454 try Hashtbl.find modehash km
6455 with Not_found ->
6456 try Hashtbl.find (findkeyhash conf "global") km
6457 with Not_found -> KMinsrt (k, m)
6458 with
6459 | KMinsrt (k, m) -> keyboard k m
6460 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6461 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6463 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6464 List.iter (fun (k, m) -> keyboard k m) insrt;
6465 state.keystate <- KSnone
6466 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6467 state.keystate <- KSinto (keys, insrt)
6468 | KSinto _ -> state.keystate <- KSnone
6470 method enter x y =
6471 state.mpos <- (x, y);
6472 state.uioh <- state.uioh#pmotion x y
6473 method leave = state.mpos <- (-1, -1)
6474 method winstate wsl = state.winstate <- wsl
6475 method quit = raise Quit
6476 end) !rootwid conf.cwinw conf.cwinh platform in
6478 setbgcol conf.bgcolor;
6479 state.wsfd <- wsfd;
6481 if not (
6482 List.exists GlMisc.check_extension
6483 [ "GL_ARB_texture_rectangle"
6484 ; "GL_EXT_texture_recangle"
6485 ; "GL_NV_texture_rectangle" ]
6487 then (dolog "OpenGL does not suppport rectangular textures"; exit 1);
6489 if (
6490 let r = GlMisc.get_string `renderer in
6491 let p = "Mesa DRI Intel(" in
6492 let l = String.length p in
6493 String.length r > l && String.sub r 0 l = p
6495 then (
6496 defconf.sliceheight <- 1024;
6497 defconf.texcount <- 32;
6498 defconf.usepbo <- true;
6501 let cs, ss =
6502 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6503 | (exception exn) ->
6504 dolog "socketpair failed: %s" @@ exntos exn;
6505 exit 1
6506 | (r, w) ->
6507 cloexec r;
6508 cloexec w;
6509 r, w
6512 setcheckers conf.checkers;
6514 opengl_has_pbo := GlMisc.check_extension "GL_ARB_pixel_buffer_object";
6516 init cs (
6517 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
6518 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6519 !Config.fontpath, !trimcachepath,
6520 !opengl_has_pbo,
6521 not !nofc
6523 List.iter GlArray.enable [`texture_coord; `vertex];
6524 state.ss <- ss;
6525 reshape ~firsttime:true winw winh;
6526 state.uioh <- uioh;
6527 if histmode
6528 then (
6529 Wsi.settitle "llpp (history)";
6530 enterhistmode ();
6532 else (
6533 state.text <- "Opening " ^ (mbtoutf8 state.path);
6534 opendoc state.path state.password;
6536 display ();
6537 Wsi.mapwin ();
6538 Wsi.setcursor Wsi.CURSOR_INHERIT;
6539 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
6541 let rec reap () =
6542 match Unix.waitpid [Unix.WNOHANG] ~-1 with
6543 | (exception (Unix.Unix_error (Unix.ECHILD, _, _))) -> ()
6544 | (exception exn) -> dolog "Unix.waitpid: %s" @@ exntos exn
6545 | 0, _ -> ()
6546 | _pid, _status -> reap ()
6548 Sys.set_signal Sys.sigchld (Sys.Signal_handle (fun _ -> doreap := true));
6550 let optrfd =
6551 ref (
6552 if nonemptystr !rcmdpath
6553 then remoteopen !rcmdpath
6554 else None
6558 let rec loop deadline =
6559 if !doreap
6560 then (
6561 doreap := false;
6562 reap ()
6564 let r = [state.ss; state.wsfd] in
6565 let r =
6566 match !optrfd with
6567 | None -> r
6568 | Some fd -> fd :: r
6570 if state.redisplay
6571 then (
6572 state.redisplay <- false;
6573 display ();
6575 let timeout =
6576 let now = now () in
6577 if deadline > now
6578 then (
6579 if deadline = infinity
6580 then ~-.1.0
6581 else max 0.0 (deadline -. now)
6583 else 0.0
6585 let r, _, _ =
6586 try Unix.select r [] [] timeout
6587 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6589 begin match r with
6590 | [] ->
6591 state.ghyll None;
6592 let newdeadline =
6593 if state.ghyll == noghyll
6594 then
6595 match state.autoscroll with
6596 | Some step when step != 0 ->
6597 let y = state.y + step in
6598 let fy = if conf.maxhfit then state.winh else 0 in
6599 let y =
6600 if y < 0
6601 then state.maxy - fy
6602 else if y >= state.maxy - fy then 0 else y
6604 if state.mode = View
6605 then gotoxy_and_clear_text state.x y
6606 else gotoxy state.x y;
6607 deadline +. 0.01
6608 | _ -> infinity
6609 else deadline +. 0.01
6611 loop newdeadline
6613 | l ->
6614 let rec checkfds = function
6615 | [] -> ()
6616 | fd :: rest when fd = state.ss ->
6617 let cmd = rcmd state.ss in
6618 act cmd;
6619 checkfds rest
6621 | fd :: rest when fd = state.wsfd ->
6622 Wsi.readresp fd;
6623 checkfds rest
6625 | fd :: rest when Some fd = !optrfd ->
6626 begin match remote fd with
6627 | None -> optrfd := remoteopen !rcmdpath;
6628 | opt -> optrfd := opt
6629 end;
6630 checkfds rest
6632 | _ :: rest ->
6633 dolog "select returned unknown descriptor";
6634 checkfds rest
6636 checkfds l;
6637 let newdeadline =
6638 let deadline1 =
6639 if deadline = infinity
6640 then now () +. 0.01
6641 else deadline
6643 match state.autoscroll with
6644 | Some step when step != 0 -> deadline1
6645 | _ -> if state.ghyll == noghyll then infinity else deadline1
6647 loop newdeadline
6648 end;
6651 loop infinity;
6652 with Quit ->
6653 Config.save leavebirdseye;
6654 if hasunsavedchanges ()
6655 then save ();