Hodgepodge of...
[llpp.git] / main.ml
blob86b2c44271aa17fcbbcf1fa5781a2ef5e9ccd4e4
1 open Utils;;
2 open Config;;
4 exception Quit;;
6 external init : Unix.file_descr -> initparams -> unit = "ml_init";;
7 external seltext : opaque -> (int * int * int * int) -> unit = "ml_seltext";;
8 external hassel : opaque -> bool = "ml_hassel";;
9 external copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
10 external getpdimrect : int -> float array = "ml_getpdimrect";;
11 external whatsunder : opaque -> int -> int -> under = "ml_whatsunder";;
12 external markunder : opaque -> int -> int -> mark -> bool = "ml_markunder";;
13 external clearmark : opaque -> unit = "ml_clearmark";;
14 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
15 external getmaxw : unit -> float = "ml_getmaxw";;
16 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
17 external measurestr : int -> string -> float = "ml_measure_string";;
18 external postprocess :
19 opaque -> int -> int -> int -> (int * string * int) -> int
20 = "ml_postprocess";;
21 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
22 external setaalevel : int -> unit = "ml_setaalevel";;
23 external realloctexts : int -> bool = "ml_realloctexts";;
24 external findlink : opaque -> linkdir -> link = "ml_findlink";;
25 external getlink : opaque -> int -> under = "ml_getlink";;
26 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
27 external getlinkcount : opaque -> int = "ml_getlinkcount";;
28 external findpwl : int -> int -> pagewithlinks = "ml_find_page_with_links";;
29 external getpbo : width -> height -> colorspace -> opaque = "ml_getpbo";;
30 external freepbo : opaque -> unit = "ml_freepbo";;
31 external unmappbo : opaque -> unit = "ml_unmappbo";;
32 external bousable : unit -> bool = "ml_bo_usable";;
33 external unproject : opaque -> int -> int -> (int * int) option
34 = "ml_unproject";;
35 external project : opaque -> int -> int -> float -> float -> (float * float)
36 = "ml_project";;
37 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
38 external rectofblock : opaque -> int -> int -> float array option
39 = "ml_rectofblock";;
40 external begintiles : unit -> unit = "ml_begintiles";;
41 external endtiles : unit -> unit = "ml_endtiles";;
42 external addannot : opaque -> int -> int -> string -> unit = "ml_addannot";;
43 external modannot : opaque -> slinkindex -> string -> unit = "ml_modannot";;
44 external delannot : opaque -> slinkindex -> unit = "ml_delannot";;
45 external hasunsavedchanges : unit -> bool = "ml_hasunsavedchanges";;
46 external savedoc : string -> unit = "ml_savedoc";;
47 external getannotcontents : opaque -> slinkindex -> string
48 = "ml_getannotcontents";;
49 external drawprect : opaque -> int -> int -> float array -> unit
50 = "ml_drawprect";;
51 external wcmd : Unix.file_descr -> bytes -> int -> unit = "ml_wcmd";;
52 external rcmd : Unix.file_descr -> string = "ml_rcmd";;
53 external uritolocation : string -> (pageno * float * float)
54 = "ml_uritolocation";;
55 external isexternallink : string -> bool = "ml_isexternallink";;
57 let selfexec = ref E.s;;
58 let opengl_has_pbo = ref false;;
60 let drawstring size x y s =
61 Gl.enable `blend;
62 Gl.enable `texture_2d;
63 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
64 ignore (drawstr size x y s);
65 Gl.disable `blend;
66 Gl.disable `texture_2d;
69 let drawstring1 size x y s =
70 drawstr size x y s;
73 let drawstring2 size x y fmt =
74 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
77 module UniSyms = struct
78 let ellipsis = "\xe2\x80\xa6";;
79 let radical = "\xe2\x88\x9a";;
80 let lguillemet = "\xc2\xab";;
81 let rguillemet = "\xc2\xbb";;
82 end;;
84 let _debugl l =
85 dolog {|l %d dim=%d {
86 WxH %dx%d
87 vWxH %dx%d
88 pagex,y %d,%d
89 dispx,y %d,%d
90 column %d
91 }|}
92 l.pageno l.pagedimno
93 l.pagew l.pageh
94 l.pagevw l.pagevh
95 l.pagex l.pagey
96 l.pagedispx l.pagedispy
97 l.pagecol
100 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
101 dolog {|rect {
102 x0,y0=(% f, % f)
103 x1,y1=(% f, % f)
104 x2,y2=(% f, % f)
105 x3,y3=(% f, % f)
106 }|} x0 y0 x1 y1 x2 y2 x3 y3;
109 let isbirdseye = function
110 | Birdseye _ -> true
111 | Textentry _ | View | LinkNav _ -> false
114 let istextentry = function
115 | Textentry _ -> true
116 | Birdseye _ | View | LinkNav _ -> false
119 let wtmode = ref false;;
120 let cxack = ref false;;
122 let pgscale h = truncate (float h *. conf.pgscale);;
124 let hscrollh () =
125 if state.uioh#alwaysscrolly || ((conf.scrollb land scrollbhv != 0)
126 && (state.w > state.winw))
127 then conf.scrollbw
128 else 0
131 let vscrollw () =
132 if state.uioh#alwaysscrolly || ((conf.scrollb land scrollbvv != 0)
133 && (state.maxy > state.winh))
134 then conf.scrollbw
135 else 0
138 let vscrollhit x =
139 if conf.leftscroll
140 then x < vscrollw ()
141 else x > state.winw - vscrollw ()
144 let setfontsize n =
145 fstate.fontsize <- n;
146 fstate.wwidth <- measurestr fstate.fontsize "w";
147 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
150 let vlog fmt =
151 if conf.verbose
152 then dolog fmt
153 else Printf.kprintf ignore fmt
156 let launchpath () =
157 if emptystr conf.pathlauncher
158 then dolog "%s" state.path
159 else (
160 let command = Str.global_replace percentsre state.path conf.pathlauncher in
161 match spawn command [] with
162 | _pid -> ()
163 | exception exn ->
164 dolog "failed to execute `%s': %s" command @@ exntos exn
168 module G =
169 struct
170 let postRedisplay who =
171 vlog "redisplay for [%S]" who;
172 state.redisplay <- true;
174 end;;
176 let getopaque pageno =
177 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
178 with Not_found -> None
181 let pagetranslatepoint l x y =
182 let dy = y - l.pagedispy in
183 let y = dy + l.pagey in
184 let dx = x - l.pagedispx in
185 let x = dx + l.pagex in
186 (x, y);
189 let onppundermouse g x y d =
190 let rec f = function
191 | l :: rest ->
192 begin match getopaque l.pageno with
193 | Some opaque ->
194 let x0 = l.pagedispx in
195 let x1 = x0 + l.pagevw in
196 let y0 = l.pagedispy in
197 let y1 = y0 + l.pagevh in
198 if y >= y0 && y <= y1 && x >= x0 && x <= x1
199 then
200 let px, py = pagetranslatepoint l x y in
201 match g opaque l px py with
202 | Some res -> res
203 | None -> f rest
204 else f rest
205 | _ ->
206 f rest
208 | [] -> d
210 f state.layout
213 let getunder x y =
214 let g opaque l px py =
215 if state.bzoom
216 then (
217 match rectofblock opaque px py with
218 | Some [|x0;x1;y0;y1|] ->
219 let rect = (x0, y0, x1, y0, x1, y1, x0, y1) in
220 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
221 state.rects <- [l.pageno, color, rect];
222 G.postRedisplay "getunder";
223 | _otherwise -> ()
225 let under = whatsunder opaque px py in
226 if under = Unone then None else Some under
228 onppundermouse g x y Unone
231 let unproject x y =
232 let g opaque l x y =
233 match unproject opaque x y with
234 | Some (x, y) -> Some (Some (opaque, l.pageno, x, y))
235 | None -> None
237 onppundermouse g x y None;
240 let showtext c s =
241 state.text <- Printf.sprintf "%c%s" c s;
242 G.postRedisplay "showtext";
245 let impmsg fmt =
246 Format.ksprintf (fun s -> showtext '!' s) fmt;
249 let pipesel opaque cmd =
250 if hassel opaque
251 then
252 match Unix.pipe () with
253 | exception exn -> dolog "pipesel cannot create pipe: %S" @@ exntos exn;
254 | (r, w) ->
255 let doclose what fd =
256 Ne.clo fd (fun msg -> dolog "%s close failed: %s" what msg)
258 begin match spawn cmd [r, 0; w, -1] with
259 | exception exn ->
260 doclose "pipesel pipe/w" w;
261 dolog "cannot execute %S: %s" cmd @@ exntos exn
262 | _pid ->
263 copysel w opaque;
264 G.postRedisplay "pipesel";
265 end;
266 doclose "pipesel pipe/r" r;
269 let paxunder x y =
270 let g opaque l px py =
271 if markunder opaque px py conf.paxmark
272 then (
273 Some (fun () ->
274 match getopaque l.pageno with
275 | None -> ()
276 | Some opaque -> pipesel opaque conf.paxcmd
279 else None
281 G.postRedisplay "paxunder";
282 if conf.paxmark = Mark_page
283 then
284 List.iter (fun l ->
285 match getopaque l.pageno with
286 | None -> ()
287 | Some opaque -> clearmark opaque) state.layout;
288 state.roam <- onppundermouse g x y (fun () -> impmsg "whoopsie daisy");
291 let selstring s =
292 match Unix.pipe () with
293 | exception exn -> impmsg "pipe failed: %s" @@ exntos exn
294 | (r, w) ->
295 let clo cap fd =
296 Ne.clo fd (fun msg -> impmsg "failed to close %s: %s" cap msg)
298 begin match spawn conf.selcmd [r, 0; w, -1] with
299 | exception exn ->
300 clo "selstring pipe/w" w;
301 impmsg "failed to execute %s: %s" conf.selcmd @@ exntos exn
302 | _pid ->
304 let l = String.length s in
305 let bytes = Bytes.unsafe_of_string s in
306 let n = tempfailureretry (Unix.write w bytes 0) l in
307 if n != l
308 then impmsg "failed to write %d characters to sel pipe, wrote %d" l n
309 with exn -> impmsg "failed to write to sel pipe: %s" @@ exntos exn
310 end;
311 clo "selstring pipe/r" r;
314 let undertext = function
315 | Unone -> "none"
316 | Ulinkuri s -> s
317 | Utext s -> "font: " ^ s
318 | Uannotation (opaque, slinkindex) ->
319 "annotation: " ^ getannotcontents opaque slinkindex
322 let updateunder x y =
323 match getunder x y with
324 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
325 | Ulinkuri uri ->
326 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
327 Wsi.setcursor Wsi.CURSOR_INFO
328 | Utext s ->
329 if conf.underinfo then showtext 'f' ("ont: " ^ s);
330 Wsi.setcursor Wsi.CURSOR_TEXT
331 | Uannotation _ ->
332 if conf.underinfo then showtext 'a' "nnotation";
333 Wsi.setcursor Wsi.CURSOR_INFO
336 let showlinktype under =
337 if conf.underinfo && under != Unone
338 then showtext ' ' @@ undertext under
341 let [@warning "-4"] intentry_with_suffix text key =
342 let text =
343 match key with
344 | Keys.Ascii ('0'..'9' as c) -> addchar text c
345 | Keys.Ascii ('k' | 'm' | 'g' | 'K' | 'M' | 'G' as c) ->
346 addchar text @@ asciilower c
347 | _ ->
348 state.text <- Printf.sprintf "invalid key";
349 text
351 TEcont text
354 let wcmd fmt =
355 let b = Buffer.create 16 in
356 Printf.kbprintf
357 (fun b ->
358 let b = Buffer.to_bytes b in
359 wcmd state.ss b @@ Bytes.length b
360 ) b fmt
363 let nogeomcmds cmds =
364 match cmds with
365 | s, [] -> emptystr s
366 | _ -> false
369 let layoutN ((columns, coverA, coverB), b) x y sw sh =
370 let rec fold accu n =
371 if n = Array.length b
372 then accu
373 else
374 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
375 if (vy - y) > sh &&
376 (n = coverA - 1
377 || n = state.pagecount - coverB
378 || (n - coverA) mod columns = columns - 1)
379 then accu
380 else
381 let accu =
382 if vy + h > y
383 then
384 let pagey = max 0 (y - vy) in
385 let pagedispy = if pagey > 0 then 0 else vy - y in
386 let pagedispx, pagex =
387 let pdx =
388 if n = coverA - 1 || n = state.pagecount - coverB
389 then x + (sw - w) / 2
390 else dx + xoff + x
392 if pdx < 0
393 then 0, -pdx
394 else pdx, 0
396 let pagevw =
397 let vw = sw - pagedispx in
398 let pw = w - pagex in
399 min vw pw
401 let pagevh = min (h - pagey) (sh - pagedispy) in
402 if pagevw > 0 && pagevh > 0
403 then
404 let e =
405 { pageno = n
406 ; pagedimno = pdimno
407 ; pagew = w
408 ; pageh = h
409 ; pagex = pagex
410 ; pagey = pagey
411 ; pagevw = pagevw
412 ; pagevh = pagevh
413 ; pagedispx = pagedispx
414 ; pagedispy = pagedispy
415 ; pagecol = 0
418 e :: accu
419 else
420 accu
421 else
422 accu
424 fold accu (n+1)
426 if Array.length b = 0
427 then []
428 else List.rev (fold [] (page_of_y y))
431 let layoutS (columns, b) x y sw sh =
432 let rec fold accu n =
433 if n = Array.length b
434 then accu
435 else
436 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
437 if (vy - y) > sh
438 then accu
439 else
440 let accu =
441 if vy + pageh > y
442 then
443 let x = xoff + x in
444 let pagey = max 0 (y - vy) in
445 let pagedispy = if pagey > 0 then 0 else vy - y in
446 let pagedispx, pagex =
447 if px = 0
448 then (
449 if x < 0
450 then 0, -x
451 else x, 0
453 else (
454 let px = px - x in
455 if px < 0
456 then -px, 0
457 else 0, px
460 let pagecolw = pagew/columns in
461 let pagedispx =
462 if pagecolw < sw
463 then pagedispx + ((sw - pagecolw) / 2)
464 else pagedispx
466 let pagevw =
467 let vw = sw - pagedispx in
468 let pw = pagew - pagex in
469 min vw pw
471 let pagevw = min pagevw pagecolw in
472 let pagevh = min (pageh - pagey) (sh - pagedispy) in
473 if pagevw > 0 && pagevh > 0
474 then
475 let e =
476 { pageno = n/columns
477 ; pagedimno = pdimno
478 ; pagew = pagew
479 ; pageh = pageh
480 ; pagex = pagex
481 ; pagey = pagey
482 ; pagevw = pagevw
483 ; pagevh = pagevh
484 ; pagedispx = pagedispx
485 ; pagedispy = pagedispy
486 ; pagecol = n mod columns
489 e :: accu
490 else
491 accu
492 else
493 accu
495 fold accu (n+1)
497 List.rev (fold [] 0)
500 let layout x y sw sh =
501 if nogeomcmds state.geomcmds
502 then
503 match conf.columns with
504 | Csingle b -> layoutN ((1, 0, 0), b) x y sw sh
505 | Cmulti c -> layoutN c x y sw sh
506 | Csplit s -> layoutS s x y sw sh
507 else []
510 let clamp incr =
511 let y = state.y + incr in
512 let y = max 0 y in
513 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
517 let itertiles l f =
518 let tilex = l.pagex mod conf.tilew in
519 let tiley = l.pagey mod conf.tileh in
521 let col = l.pagex / conf.tilew in
522 let row = l.pagey / conf.tileh in
524 let rec rowloop row y0 dispy h =
525 if h = 0
526 then ()
527 else (
528 let dh = conf.tileh - y0 in
529 let dh = min h dh in
530 let rec colloop col x0 dispx w =
531 if w = 0
532 then ()
533 else (
534 let dw = conf.tilew - x0 in
535 let dw = min w dw in
536 f col row dispx dispy x0 y0 dw dh;
537 colloop (col+1) 0 (dispx+dw) (w-dw)
540 colloop col tilex l.pagedispx l.pagevw;
541 rowloop (row+1) 0 (dispy+dh) (h-dh)
544 if l.pagevw > 0 && l.pagevh > 0
545 then rowloop row tiley l.pagedispy l.pagevh;
548 let gettileopaque l col row =
549 let key =
550 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
552 try Some (Hashtbl.find state.tilemap key)
553 with Not_found -> None
556 let puttileopaque l col row gen colorspace angle opaque size elapsed =
557 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
558 Hashtbl.add state.tilemap key (opaque, size, elapsed)
561 let filledrect2 x0 y0 x1 y1 x2 y2 x3 y3 =
562 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x1; y1; x2; y2; x3; y3 |];
563 GlArray.vertex `two state.vraw;
564 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
567 let filledrect1 x0 y0 x1 y1 = filledrect2 x0 y0 x0 y1 x1 y0 x1 y1;;
569 let filledrect x0 y0 x1 y1 =
570 GlArray.disable `texture_coord;
571 filledrect1 x0 y0 x1 y1;
572 GlArray.enable `texture_coord;
575 let linerect x0 y0 x1 y1 =
576 GlArray.disable `texture_coord;
577 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y1; x1; y0 |];
578 GlArray.vertex `two state.vraw;
579 GlArray.draw_arrays `line_loop ~first:0 ~count:4;
580 GlArray.enable `texture_coord;
583 let drawtiles l color =
584 GlDraw.color color;
585 begintiles ();
586 let f col row x y tilex tiley w h =
587 match gettileopaque l col row with
588 | Some (opaque, _, t) ->
589 let params = x, y, w, h, tilex, tiley in
590 if conf.invert
591 then GlTex.env (`mode `blend);
592 drawtile params opaque;
593 if conf.invert
594 then GlTex.env (`mode `modulate);
595 if conf.debug
596 then (
597 endtiles ();
598 let s = Printf.sprintf
599 "%d[%d,%d] %f sec"
600 l.pageno col row t
602 let w = measurestr fstate.fontsize s in
603 GlDraw.color (0.0, 0.0, 0.0);
604 filledrect (float (x-2))
605 (float (y-2))
606 (float (x+2) +. w)
607 (float (y + fstate.fontsize + 2));
608 GlDraw.color color;
609 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
610 begintiles ();
613 | None ->
614 endtiles ();
615 let w =
616 let lw = state.winw - x in
617 min lw w
618 and h =
619 let lh = state.winh - y in
620 min lh h
622 if conf.invert
623 then GlTex.env (`mode `blend);
624 begin match state.checkerstexid with
625 | Some id ->
626 Gl.enable `texture_2d;
627 GlTex.bind_texture ~target:`texture_2d id;
628 let x0 = float x
629 and y0 = float y
630 and x1 = float (x+w)
631 and y1 = float (y+h) in
633 let tw = float w /. 16.0
634 and th = float h /. 16.0 in
635 let tx0 = float tilex /. 16.0
636 and ty0 = float tiley /. 16.0 in
637 let tx1 = tx0 +. tw
638 and ty1 = ty0 +. th in
639 Raw.sets_float state.vraw ~pos:0
640 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
641 Raw.sets_float state.traw ~pos:0
642 [| tx0; ty0; tx0; ty1; tx1; ty0; tx1; ty1 |];
643 GlArray.vertex `two state.vraw;
644 GlArray.tex_coord `two state.traw;
645 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
646 Gl.disable `texture_2d;
648 | None ->
649 GlDraw.color (1.0, 1.0, 1.0);
650 filledrect (float x) (float y) (float (x+w)) (float (y+h));
651 end;
652 if conf.invert
653 then GlTex.env (`mode `modulate);
654 if w > 128 && h > fstate.fontsize + 10
655 then (
656 let c = if conf.invert then 1.0 else 0.0 in
657 GlDraw.color (c, c, c);
658 let c, r =
659 if conf.verbose
660 then (col*conf.tilew, row*conf.tileh)
661 else col, row
663 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
665 GlDraw.color color;
666 begintiles ();
668 itertiles l f;
669 endtiles ();
672 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
674 let tilevisible1 l x y =
675 let ax0 = l.pagex
676 and ax1 = l.pagex + l.pagevw
677 and ay0 = l.pagey
678 and ay1 = l.pagey + l.pagevh in
680 let bx0 = x
681 and by0 = y in
682 let bx1 = min (bx0 + conf.tilew) l.pagew
683 and by1 = min (by0 + conf.tileh) l.pageh in
685 let rx0 = max ax0 bx0
686 and ry0 = max ay0 by0
687 and rx1 = min ax1 bx1
688 and ry1 = min ay1 by1 in
690 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
691 nonemptyintersection
694 let tilevisible layout n x y =
695 let rec findpageinlayout m = function
696 | l :: rest when l.pageno = n ->
697 tilevisible1 l x y || (
698 match conf.columns with
699 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
700 | Csplit _ | Csingle _ | Cmulti _ -> false
702 | _ :: rest -> findpageinlayout 0 rest
703 | [] -> false
705 findpageinlayout 0 layout;
708 let tileready l x y =
709 tilevisible1 l x y &&
710 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
713 let tilepage n p layout =
714 let rec loop = function
715 | l :: rest ->
716 if l.pageno = n
717 then
718 let f col row _ _ _ _ _ _ =
719 if state.currently = Idle
720 then
721 match gettileopaque l col row with
722 | Some _ -> ()
723 | None ->
724 let x = col*conf.tilew
725 and y = row*conf.tileh in
726 let w =
727 let w = l.pagew - x in
728 min w conf.tilew
730 let h =
731 let h = l.pageh - y in
732 min h conf.tileh
734 let pbo =
735 if conf.usepbo
736 then getpbo w h conf.colorspace
737 else ~< "0"
739 wcmd "tile %s %d %d %d %d %s"
740 (~> p) x y w h (~> pbo);
741 state.currently <-
742 Tiling (
743 l, p, conf.colorspace, conf.angle,
744 state.gen, col, row, conf.tilew, conf.tileh
747 itertiles l f;
748 else
749 loop rest
751 | [] -> ()
753 if nogeomcmds state.geomcmds
754 then loop layout;
757 let preloadlayout x y sw sh =
758 let y = if y < sh then 0 else y - sh in
759 let x = min 0 (x + sw) in
760 let h = sh*3 in
761 let w = sw*3 in
762 layout x y w h;
765 let load pages =
766 let rec loop pages =
767 if state.currently != Idle
768 then ()
769 else
770 match pages with
771 | l :: rest ->
772 begin match getopaque l.pageno with
773 | None ->
774 wcmd "page %d %d" l.pageno l.pagedimno;
775 state.currently <- Loading (l, state.gen);
776 | Some opaque ->
777 tilepage l.pageno opaque pages;
778 loop rest
779 end;
780 | _ -> ()
782 if nogeomcmds state.geomcmds
783 then loop pages
786 let preload pages =
787 load pages;
788 if conf.preload && state.currently = Idle
789 then load (preloadlayout state.x state.y state.winw state.winh);
792 let layoutready layout =
793 let rec fold all ls =
794 all && match ls with
795 | l :: rest ->
796 let seen = ref false in
797 let allvisible = ref true in
798 let foo col row _ _ _ _ _ _ =
799 seen := true;
800 allvisible := !allvisible &&
801 begin match gettileopaque l col row with
802 | Some _ -> true
803 | None -> false
806 itertiles l foo;
807 fold (!seen && !allvisible) rest
808 | [] -> true
810 let alltilesvisible = fold true layout in
811 alltilesvisible;
814 let gotoxy x y =
815 let y = bound y 0 state.maxy in
816 let y, layout, proceed =
817 match conf.maxwait with
818 | Some time when state.ghyll == noghyll ->
819 begin match state.throttle with
820 | None ->
821 let layout = layout x y state.winw state.winh in
822 let ready = layoutready layout in
823 if not ready
824 then (
825 load layout;
826 state.throttle <- Some (layout, y, now ());
828 else G.postRedisplay "gotoxy showall (None)";
829 y, layout, ready
830 | Some (_, _, started) ->
831 let dt = now () -. started in
832 if dt > time
833 then (
834 state.throttle <- None;
835 let layout = layout x y state.winw state.winh in
836 load layout;
837 G.postRedisplay "maxwait";
838 y, layout, true
840 else -1, [], false
843 | _ ->
844 let layout = layout x y state.winw state.winh in
845 if not !wtmode || layoutready layout
846 then G.postRedisplay "gotoxy ready";
847 y, layout, true
849 if proceed
850 then (
851 state.x <- x;
852 state.y <- y;
853 state.layout <- layout;
854 begin match state.mode with
855 | LinkNav ln ->
856 begin match ln with
857 | Ltexact (pageno, linkno) ->
858 let rec loop = function
859 | [] ->
860 state.lnava <- Some (pageno, linkno);
861 state.mode <- LinkNav (Ltgendir 0)
862 | l :: _ when l.pageno = pageno ->
863 begin match getopaque pageno with
864 | None -> state.mode <- LinkNav (Ltnotready (pageno, 0))
865 | Some opaque ->
866 let x0, y0, x1, y1 = getlinkrect opaque linkno in
867 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
868 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
869 then state.mode <- LinkNav (Ltgendir 0)
871 | _ :: rest -> loop rest
873 loop layout
874 | Ltnotready _ | Ltgendir _ -> ()
876 | Birdseye _ | Textentry _ | View -> ()
877 end;
878 begin match state.mode with
879 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
880 if not (pagevisible layout pageno)
881 then (
882 match state.layout with
883 | [] -> ()
884 | l :: _ ->
885 state.mode <- Birdseye (
886 conf, leftx, l.pageno, hooverpageno, anchor
889 | LinkNav lt ->
890 begin match lt with
891 | Ltnotready (_, dir)
892 | Ltgendir dir ->
893 let linknav =
894 let rec loop = function
895 | [] -> lt
896 | l :: rest ->
897 match getopaque l.pageno with
898 | None -> Ltnotready (l.pageno, dir)
899 | Some opaque ->
900 let link =
901 let ld =
902 if dir = 0
903 then LDfirstvisible (l.pagex, l.pagey, dir)
904 else (
905 if dir > 0 then LDfirst else LDlast
908 findlink opaque ld
910 match link with
911 | Lnotfound -> loop rest
912 | Lfound n ->
913 showlinktype (getlink opaque n);
914 Ltexact (l.pageno, n)
916 loop state.layout
918 state.mode <- LinkNav linknav
919 | Ltexact _ -> ()
921 | Textentry _ | View -> ()
922 end;
923 preload layout;
925 state.ghyll <- noghyll;
926 if conf.updatecurs
927 then (
928 let mx, my = state.mpos in
929 updateunder mx my;
933 let conttiling pageno opaque =
934 tilepage pageno opaque
935 (if conf.preload
936 then preloadlayout state.x state.y state.winw state.winh
937 else state.layout)
940 let gotoxy_and_clear_text x y =
941 if not conf.verbose then state.text <- E.s;
942 gotoxy x y;
945 let getanchory (n, top, dtop) =
946 let y, h = getpageyh n in
947 if conf.presentation
948 then
949 let ips = calcips h in
950 y + truncate (top*.float h -. dtop*.float ips) + ips;
951 else
952 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
955 let gotoanchor anchor =
956 gotoxy state.x (getanchory anchor);
959 let addnav () =
960 cbput state.hists.nav (getanchor ());
963 let getnav dir =
964 let anchor = cbgetc state.hists.nav dir in
965 getanchory anchor;
968 let gotoghyll1 single y =
969 let scroll f n a b =
970 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
971 let snake f a b =
972 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
973 if f < a
974 then s (float f /. float a)
975 else (
976 if f > b
977 then 1.0 -. s ((float (f-b) /. float (n-b)))
978 else 1.0
981 snake f a b
982 and summa n a b =
983 let ins = float a *. 0.5
984 and outs = float (n-b) *. 0.5 in
985 let ones = b - a in
986 ins +. outs +. float ones
988 let rec set nab y sy =
989 let (_N, _A, _B), y =
990 if single
991 then
992 let scl = if y > sy then 2 else -2 in
993 let _N, _, _ = nab in
994 (_N,0,_N), y+conf.scrollstep*scl
995 else nab,y in
996 let sum = summa _N _A _B in
997 let dy = float (y - sy) in
998 state.ghyll <- (
999 let rec gf n y1 o =
1000 if n >= _N
1001 then state.ghyll <- noghyll
1002 else
1003 let go n =
1004 let s = scroll n _N _A _B in
1005 let y1 = y1 +. ((s *. dy) /. sum) in
1006 gotoxy_and_clear_text state.x (truncate y1);
1007 state.ghyll <- gf (n+1) y1;
1009 match o with
1010 | None -> go n
1011 | Some y' when single -> set nab y' state.y
1012 | Some y' -> set (_N/2, 1, 1) y' state.y
1014 gf 0 (float state.y)
1017 match conf.ghyllscroll with
1018 | Some nab when not conf.presentation ->
1019 if state.ghyll == noghyll
1020 then set nab y state.y
1021 else state.ghyll (Some y)
1022 | _ ->
1023 gotoxy_and_clear_text state.x y
1026 let gotoghyll = gotoghyll1 false;;
1028 let gotopage n top =
1029 let y, h = getpageyh n in
1030 let y = y + (truncate (top *. float h)) in
1031 gotoghyll y
1034 let gotopage1 n top =
1035 let y = getpagey n in
1036 let y = y + top in
1037 gotoghyll y
1040 let invalidate s f =
1041 state.redisplay <- false;
1042 state.layout <- [];
1043 state.pdims <- [];
1044 state.rects <- [];
1045 state.rects1 <- [];
1046 match state.geomcmds with
1047 | ps, [] when emptystr ps ->
1048 f ();
1049 state.geomcmds <- s, [];
1051 | ps, [] ->
1052 state.geomcmds <- ps, [s, f];
1054 | ps, (s', _) :: rest when s' = s ->
1055 state.geomcmds <- ps, ((s, f) :: rest);
1057 | ps, cmds ->
1058 state.geomcmds <- ps, ((s, f) :: cmds);
1061 let flushpages () =
1062 Hashtbl.iter (fun _ opaque ->
1063 wcmd "freepage %s" (~> opaque);
1064 ) state.pagemap;
1065 Hashtbl.clear state.pagemap;
1068 let flushtiles () =
1069 if not (Queue.is_empty state.tilelru)
1070 then (
1071 Queue.iter (fun (k, p, s) ->
1072 wcmd "freetile %s" (~> p);
1073 state.memused <- state.memused - s;
1074 Hashtbl.remove state.tilemap k;
1075 ) state.tilelru;
1076 state.uioh#infochanged Memused;
1077 Queue.clear state.tilelru;
1079 load state.layout;
1082 let stateh h =
1083 let h = truncate (float h*.conf.zoom) in
1084 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
1085 h - d
1088 let opendoc path password =
1089 state.path <- path;
1090 state.password <- password;
1091 state.gen <- state.gen + 1;
1092 state.docinfo <- [];
1093 state.outlines <- [||];
1095 flushpages ();
1096 setaalevel conf.aalevel;
1097 let titlepath =
1098 if emptystr state.origin
1099 then path
1100 else state.origin
1102 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
1103 wcmd "open %d %d %d %s\000%s\000%s\000"
1104 (btod !wtmode) (btod !cxack) (btod conf.usedoccss)
1105 path password conf.css;
1106 invalidate "reqlayout"
1107 (fun () ->
1108 wcmd "reqlayout %d %d %d %s\000"
1109 conf.angle (FMTE.to_int conf.fitmodel)
1110 (stateh state.winh) state.nameddest
1112 state.help <-
1113 let sl = keystostrlist conf in
1114 let rec loop accu =
1115 function | [] -> accu
1116 | s :: rest -> loop ((s, 0, Noaction) :: accu) rest
1117 in makehelp () @ (("", 0, Noaction) :: loop [] sl) |> Array.of_list
1120 let reload () =
1121 state.anchor <- getanchor ();
1122 opendoc state.path state.password;
1125 let scalecolor c =
1126 let c = c *. conf.colorscale in
1127 (c, c, c);
1130 let scalecolor2 (r, g, b) =
1131 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1134 let docolumns columns =
1135 match columns with
1136 | Csingle _ ->
1137 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1138 let rec loop pageno pdimno pdim y ph pdims =
1139 if pageno = state.pagecount
1140 then ()
1141 else
1142 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1143 match pdims with
1144 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1145 pdimno+1, pdim, rest
1146 | _ ->
1147 pdimno, pdim, pdims
1149 let x = max 0 (((state.winw - w) / 2) - xoff) in
1150 let y =
1151 y + (if conf.presentation
1152 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1153 else (if pageno = 0 then 0 else conf.interpagespace)
1156 a.(pageno) <- (pdimno, x, y, pdim);
1157 loop (pageno+1) pdimno pdim (y + h) h pdims
1159 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1160 conf.columns <- Csingle a;
1162 | Cmulti ((columns, coverA, coverB), _) ->
1163 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1164 let rec loop pageno pdimno pdim x y rowh pdims =
1165 let rec fixrow m =
1166 if m = pageno then () else
1167 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1168 if h < rowh
1169 then (
1170 let y = y + (rowh - h) / 2 in
1171 a.(m) <- (pdimno, x, y, pdim);
1173 fixrow (m+1)
1175 if pageno = state.pagecount
1176 then fixrow (((pageno - 1) / columns) * columns)
1177 else
1178 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1179 match pdims with
1180 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1181 pdimno+1, pdim, rest
1182 | _ ->
1183 pdimno, pdim, pdims
1185 let x, y, rowh' =
1186 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1187 then (
1188 let x = (state.winw - w) / 2 in
1189 let ips =
1190 if conf.presentation then calcips h else conf.interpagespace in
1191 x, y + ips + rowh, h
1193 else (
1194 if (pageno - coverA) mod columns = 0
1195 then (
1196 let x = max 0 (state.winw - state.w) / 2 in
1197 let y =
1198 if conf.presentation
1199 then
1200 let ips = calcips h in
1201 y + (if pageno = 0 then 0 else calcips rowh + ips)
1202 else
1203 y + (if pageno = 0 then 0 else conf.interpagespace)
1205 x, y + rowh, h
1207 else x, y, max rowh h
1210 let y =
1211 if pageno > 1 && (pageno - coverA) mod columns = 0
1212 then (
1213 let y =
1214 if pageno = columns && conf.presentation
1215 then (
1216 let ips = calcips rowh in
1217 for i = 0 to pred columns
1219 let (pdimno, x, y, pdim) = a.(i) in
1220 a.(i) <- (pdimno, x, y+ips, pdim)
1221 done;
1222 y+ips;
1224 else y
1226 fixrow (pageno - columns);
1229 else y
1231 a.(pageno) <- (pdimno, x, y, pdim);
1232 let x = x + w + xoff*2 + conf.interpagespace in
1233 loop (pageno+1) pdimno pdim x y rowh' pdims
1235 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1236 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1238 | Csplit (c, _) ->
1239 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1240 let rec loop pageno pdimno pdim y pdims =
1241 if pageno = state.pagecount
1242 then ()
1243 else
1244 let pdimno, ((_, w, h, _) as pdim), pdims =
1245 match pdims with
1246 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1247 pdimno+1, pdim, rest
1248 | _ ->
1249 pdimno, pdim, pdims
1251 let cw = w / c in
1252 let rec loop1 n x y =
1253 if n = c then y else (
1254 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1255 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1258 let y = loop1 0 0 y in
1259 loop (pageno+1) pdimno pdim y pdims
1261 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1262 conf.columns <- Csplit (c, a);
1265 let represent () =
1266 docolumns conf.columns;
1267 state.maxy <- calcheight ();
1268 if state.reprf == noreprf
1269 then (
1270 match state.mode with
1271 | Birdseye (_, _, pageno, _, _) ->
1272 let y, h = getpageyh pageno in
1273 let top = (state.winh - h) / 2 in
1274 gotoxy state.x (max 0 (y - top))
1275 | Textentry _ | View | LinkNav _ ->
1276 let y = getanchory state.anchor in
1277 let y = min y (state.maxy - state.winh) in
1278 gotoxy state.x y;
1280 else (
1281 state.reprf ();
1282 state.reprf <- noreprf;
1286 let reshape ?(firsttime=false) w h =
1287 GlDraw.viewport ~x:0 ~y:0 ~w ~h;
1288 if not firsttime && nogeomcmds state.geomcmds
1289 then state.anchor <- getanchor ();
1291 state.winw <- w;
1292 let w = truncate (float w *. conf.zoom) in
1293 let w = max w 2 in
1294 state.winh <- h;
1295 setfontsize fstate.fontsize;
1296 GlMat.mode `modelview;
1297 GlMat.load_identity ();
1299 GlMat.mode `projection;
1300 GlMat.load_identity ();
1301 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1302 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1303 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
1305 let relx =
1306 if conf.zoom <= 1.0
1307 then 0.0
1308 else float state.x /. float state.w
1310 invalidate "geometry"
1311 (fun () ->
1312 state.w <- w;
1313 if not firsttime
1314 then state.x <- truncate (relx *. float w);
1315 let w =
1316 match conf.columns with
1317 | Csingle _ -> w
1318 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1319 | Csplit (c, _) -> w * c
1321 wcmd "geometry %d %d %d"
1322 w (stateh h) (FMTE.to_int conf.fitmodel)
1326 let enttext () =
1327 let len = String.length state.text in
1328 let x0 = if conf.leftscroll then vscrollw () else 0 in
1329 let drawstring s =
1330 let hscrollh =
1331 match state.mode with
1332 | Textentry _ | View | LinkNav _ ->
1333 let h, _, _ = state.uioh#scrollpw in
1335 | Birdseye _ -> 0
1337 let rect x w =
1338 filledrect
1339 x (float (state.winh - (fstate.fontsize + 4) - hscrollh))
1340 (x+.w) (float (state.winh - hscrollh))
1343 let w = float (state.winw - 1 - vscrollw ()) in
1344 if state.progress >= 0.0 && state.progress < 1.0
1345 then (
1346 GlDraw.color (0.3, 0.3, 0.3);
1347 let w1 = w *. state.progress in
1348 rect (float x0) w1;
1349 GlDraw.color (0.0, 0.0, 0.0);
1350 rect (float x0+.w1) (float x0+.w-.w1)
1352 else (
1353 GlDraw.color (0.0, 0.0, 0.0);
1354 rect (float x0) w;
1357 GlDraw.color (1.0, 1.0, 1.0);
1358 drawstring
1359 fstate.fontsize
1360 (if conf.leftscroll then x0 + 2 else x0 + if len > 0 then 8 else 2)
1361 (state.winh - hscrollh - 5) s;
1363 let s =
1364 match state.mode with
1365 | Textentry ((prefix, text, _, _, _, _), _) ->
1366 let s =
1367 if len > 0
1368 then Printf.sprintf "%s%s_ [%s]" prefix text state.text
1369 else Printf.sprintf "%s%s_" prefix text
1373 | Birdseye _ | View | LinkNav _ -> state.text
1375 let s =
1376 if state.newerrmsgs
1377 then (
1378 if not (istextentry state.mode) && state.uioh#eformsgs
1379 then
1380 let s1 = "(press 'e' to review error messasges)" in
1381 if nonemptystr s then s ^ " " ^ s1 else s1
1382 else s
1384 else s
1386 if nonemptystr s
1387 then drawstring s
1390 let gctiles () =
1391 let len = Queue.length state.tilelru in
1392 let layout = lazy (
1393 match state.throttle with
1394 | None ->
1395 if conf.preload
1396 then preloadlayout state.x state.y state.winw state.winh
1397 else state.layout
1398 | Some (layout, _, _) ->
1399 layout
1400 ) in
1401 let rec loop qpos =
1402 if state.memused > conf.memlimit
1403 then (
1404 if qpos < len
1405 then
1406 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1407 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1408 let (_, pw, ph, _) = getpagedim n in
1409 if gen = state.gen
1410 && colorspace = conf.colorspace
1411 && angle = conf.angle
1412 && pagew = pw
1413 && pageh = ph
1414 && (
1415 let x = col*conf.tilew
1416 and y = row*conf.tileh in
1417 tilevisible (Lazy.force_val layout) n x y
1419 then Queue.push lruitem state.tilelru
1420 else (
1421 freepbo p;
1422 wcmd "freetile %s" (~> p);
1423 state.memused <- state.memused - s;
1424 state.uioh#infochanged Memused;
1425 Hashtbl.remove state.tilemap k;
1427 loop (qpos+1)
1430 loop 0
1433 let onpagerect pageno f =
1434 let b =
1435 match conf.columns with
1436 | Cmulti (_, b) -> b
1437 | Csingle b -> b
1438 | Csplit (_, b) -> b
1440 if pageno >= 0 && pageno < Array.length b
1441 then
1442 let (_, _, _, (_, w, h, _)) = b.(pageno) in
1443 f w h
1446 let gotopagexy1 wtmode pageno x y =
1447 let _,w1,h1,leftx = getpagedim pageno in
1448 let top = y /. (float h1) in
1449 let left = x /. (float w1) in
1450 let py, w, h = getpageywh pageno in
1451 let wh = state.winh in
1452 let x = left *. (float w) in
1453 let x = leftx + state.x + truncate x in
1454 let sx =
1455 if x < 0 || x >= state.winw
1456 then state.x - x
1457 else state.x
1459 let pdy = truncate (top *. float h) in
1460 let y' = py + pdy in
1461 let dy = y' - state.y in
1462 let sy =
1463 if x != state.x || not (dy > 0 && dy < wh)
1464 then (
1465 if conf.presentation
1466 then
1467 if abs (py - y') > wh
1468 then y'
1469 else py
1470 else y';
1472 else state.y
1474 if state.x != sx || state.y != sy
1475 then (
1476 let x, y =
1477 if wtmode
1478 then (
1479 let ww = state.winw in
1480 let qx = sx / ww
1481 and qy = pdy / wh in
1482 let x = qx * ww
1483 and y = py + qy * wh in
1484 let x = if -x + ww > w1 then -(w1-ww) else x
1485 and y' = if y + wh > state.maxy then state.maxy - wh else y in
1486 let y =
1487 if conf.presentation
1488 then
1489 if abs (py - y') > wh
1490 then y'
1491 else py
1492 else y';
1494 (x, y)
1496 else (sx, sy)
1498 gotoxy_and_clear_text x y;
1500 else gotoxy_and_clear_text state.x state.y;
1503 let gotopagexy wtmode pageno x y =
1504 match state.mode with
1505 | Birdseye _ -> gotopage pageno 0.0
1506 | Textentry _ | View | LinkNav _ -> gotopagexy1 wtmode pageno x y
1509 let getpassword () =
1510 let passcmd = getenvwithdef "LLPP_ASKPASS" conf.passcmd in
1511 if emptystr passcmd
1512 then E.s
1513 else getcmdoutput
1514 (fun s ->
1515 impmsg "error getting password: %s" s;
1516 dolog "%s" s) passcmd;
1519 let pgoto opaque pageno x y =
1520 let pdimno = getpdimno pageno in
1521 let x, y = project opaque pageno pdimno x y in
1522 gotopagexy false pageno x y;
1525 let act cmds =
1526 (* dolog "%S" cmds; *)
1527 let spl = splitatchar cmds ' ' in
1528 let scan s fmt f =
1529 try Scanf.sscanf s fmt f
1530 with exn ->
1531 dolog "error processing '%S': %s" cmds @@ exntos exn;
1532 exit 1
1534 let addoutline outline =
1535 match state.currently with
1536 | Outlining outlines -> state.currently <- Outlining (outline :: outlines)
1537 | Idle -> state.currently <- Outlining [outline]
1538 | Loading _ | Tiling _ ->
1539 dolog "invalid outlining state";
1540 logcurrently state.currently
1542 match spl with
1543 | "clear", "" ->
1544 state.pdims <- [];
1545 state.uioh#infochanged Pdim;
1547 | "clearrects", "" ->
1548 state.rects <- state.rects1;
1549 G.postRedisplay "clearrects";
1551 | "continue", args ->
1552 let n = scan args "%u" (fun n -> n) in
1553 state.pagecount <- n;
1554 begin match state.currently with
1555 | Outlining l ->
1556 state.currently <- Idle;
1557 state.outlines <- Array.of_list (List.rev l)
1558 | Idle | Loading _ | Tiling _ -> ()
1559 end;
1561 let cur, cmds = state.geomcmds in
1562 if emptystr cur
1563 then failwith "umpossible";
1565 begin match List.rev cmds with
1566 | [] ->
1567 state.geomcmds <- E.s, [];
1568 state.throttle <- None;
1569 represent ();
1570 | (s, f) :: rest ->
1571 f ();
1572 state.geomcmds <- s, List.rev rest;
1573 end;
1574 if conf.maxwait = None && not !wtmode
1575 then G.postRedisplay "continue";
1577 | "msg", args ->
1578 showtext ' ' args
1580 | "vmsg", args ->
1581 if conf.verbose
1582 then showtext ' ' args
1584 | "emsg", args ->
1585 Buffer.add_string state.errmsgs args;
1586 state.newerrmsgs <- true;
1587 G.postRedisplay "error message"
1589 | "progress", args ->
1590 let progress, text =
1591 scan args "%f %n"
1592 (fun f pos ->
1593 f, String.sub args pos (String.length args - pos))
1595 state.text <- text;
1596 state.progress <- progress;
1597 G.postRedisplay "progress"
1599 | "firstmatch", args ->
1600 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1601 scan args "%u %d %f %f %f %f %f %f %f %f"
1602 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1603 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1605 let y = (getpagey pageno) + truncate y0 in
1606 let x =
1607 if conf.zoom > 1.0
1608 then state.winw/2
1609 else state.x
1611 addnav ();
1612 gotoxy x y;
1613 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1614 state.rects1 <- [pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)]
1616 | "match", args ->
1617 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1618 scan args "%u %d %f %f %f %f %f %f %f %f"
1619 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1620 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1622 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1623 state.rects1 <-
1624 (pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1626 | "page", args ->
1627 let pageopaques, t = scan args "%s %f" (fun p t -> p, t) in
1628 let pageopaque = ~< pageopaques in
1629 begin match state.currently with
1630 | Loading (l, gen) ->
1631 vlog "page %d took %f sec" l.pageno t;
1632 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1633 begin match state.throttle with
1634 | None ->
1635 let preloadedpages =
1636 if conf.preload
1637 then preloadlayout state.x state.y state.winw state.winh
1638 else state.layout
1640 let evict () =
1641 let set =
1642 List.fold_left (fun s l -> IntSet.add l.pageno s)
1643 IntSet.empty preloadedpages
1645 let evictedpages =
1646 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1647 if not (IntSet.mem pageno set)
1648 then (
1649 wcmd "freepage %s" (~> opaque);
1650 key :: accu
1652 else accu
1653 ) state.pagemap []
1655 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1657 evict ();
1658 state.currently <- Idle;
1659 if gen = state.gen
1660 then (
1661 tilepage l.pageno pageopaque state.layout;
1662 load state.layout;
1663 load preloadedpages;
1664 let visible = pagevisible state.layout l.pageno in
1665 if visible
1666 then (
1667 match state.mode with
1668 | LinkNav (Ltnotready (pageno, dir)) ->
1669 if pageno = l.pageno
1670 then (
1671 let link =
1672 let ld =
1673 if dir = 0
1674 then LDfirstvisible (l.pagex, l.pagey, dir)
1675 else (
1676 if dir > 0 then LDfirst else LDlast
1679 findlink pageopaque ld
1681 match link with
1682 | Lnotfound -> ()
1683 | Lfound n ->
1684 showlinktype (getlink pageopaque n);
1685 state.mode <- LinkNav (Ltexact (l.pageno, n))
1687 | LinkNav (Ltgendir _)
1688 | LinkNav (Ltexact _)
1689 | View
1690 | Birdseye _
1691 | Textentry _ -> ()
1694 if visible && layoutready state.layout
1695 then (
1696 G.postRedisplay "page";
1700 | Some (layout, _, _) ->
1701 state.currently <- Idle;
1702 tilepage l.pageno pageopaque layout;
1703 load state.layout
1704 end;
1706 | Idle | Tiling _ | Outlining _ ->
1707 dolog "Inconsistent loading state";
1708 logcurrently state.currently;
1709 exit 1
1712 | "tile" , args ->
1713 let (x, y, opaques, size, t) =
1714 scan args "%u %u %s %u %f"
1715 (fun x y p size t -> (x, y, p, size, t))
1717 let opaque = ~< opaques in
1718 begin match state.currently with
1719 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1720 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1722 unmappbo opaque;
1723 if tilew != conf.tilew || tileh != conf.tileh
1724 then (
1725 wcmd "freetile %s" (~> opaque);
1726 state.currently <- Idle;
1727 load state.layout;
1729 else (
1730 puttileopaque l col row gen cs angle opaque size t;
1731 state.memused <- state.memused + size;
1732 state.uioh#infochanged Memused;
1733 gctiles ();
1734 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1735 opaque, size) state.tilelru;
1737 let layout =
1738 match state.throttle with
1739 | None -> state.layout
1740 | Some (layout, _, _) -> layout
1743 state.currently <- Idle;
1744 if gen = state.gen
1745 && conf.colorspace = cs
1746 && conf.angle = angle
1747 && tilevisible layout l.pageno x y
1748 then conttiling l.pageno pageopaque;
1750 begin match state.throttle with
1751 | None ->
1752 preload state.layout;
1753 if gen = state.gen
1754 && conf.colorspace = cs
1755 && conf.angle = angle
1756 && tilevisible state.layout l.pageno x y
1757 && (not !wtmode || layoutready state.layout)
1758 then G.postRedisplay "tile nothrottle";
1760 | Some (layout, y, _) ->
1761 let ready = layoutready layout in
1762 if ready
1763 then (
1764 state.y <- y;
1765 state.layout <- layout;
1766 state.throttle <- None;
1767 G.postRedisplay "throttle";
1769 else load layout;
1770 end;
1773 | Idle | Loading _ | Outlining _ ->
1774 dolog "Inconsistent tiling state";
1775 logcurrently state.currently;
1776 exit 1
1779 | "pdim", args ->
1780 let (n, w, h, _) as pdim =
1781 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1783 let pdim =
1784 match conf.fitmodel with
1785 | FitWidth -> pdim
1786 | FitPage | FitProportional ->
1787 match conf.columns with
1788 | Csplit _ -> (n, w, h, 0)
1789 | Csingle _ | Cmulti _ -> pdim
1791 state.pdims <- pdim :: state.pdims;
1792 state.uioh#infochanged Pdim
1794 | "o", args ->
1795 let (l, n, t, h, pos) =
1796 scan args "%u %u %d %u %n"
1797 (fun l n t h pos -> l, n, t, h, pos)
1799 let s = String.sub args pos (String.length args - pos) in
1800 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
1802 | "ou", args ->
1803 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
1804 let s = String.sub args pos len in
1805 let pos2 = pos + len + 1 in
1806 let uri = String.sub args pos2 (String.length args - pos2) in
1807 addoutline (s, l, Ouri uri)
1809 | "on", args ->
1810 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
1811 let s = String.sub args pos (String.length args - pos) in
1812 addoutline (s, l, Onone)
1814 | "a", args ->
1815 let (n, l, t) =
1816 scan args "%u %d %d" (fun n l t -> n, l, t)
1818 state.reprf <- (fun () -> gotopagexy !wtmode n (float l) (float t))
1820 | "info", args ->
1821 let c, v = splitatchar args '\t' in
1822 let s =
1823 if nonemptystr v
1824 then
1825 if c = "Title"
1826 then (
1827 conf.title <- v;
1828 Wsi.settitle v;
1829 args
1831 else
1832 if let len = String.length c in
1833 len > 6 && ((String.sub c (len-4) 4) = "date")
1834 then (
1835 if String.length v >= 7 && v.[0] = 'D' && v.[1] = ':'
1836 then
1837 let b = Buffer.create 10 in
1838 Printf.bprintf b "%s\t" c;
1839 let sub p l c =
1841 Buffer.add_substring b v p l;
1842 Buffer.add_char b c;
1843 with exn -> Buffer.add_string b @@ exntos exn
1845 sub 2 4 '/';
1846 sub 6 2 '/';
1847 sub 8 2 ' ';
1848 sub 10 2 ':';
1849 sub 12 2 ':';
1850 sub 14 2 ' ';
1851 Buffer.add_char b '[';
1852 Buffer.add_string b v;
1853 Buffer.add_char b ']';
1854 Buffer.contents b
1855 else args
1857 else args
1858 else args
1860 state.docinfo <- (1, s) :: state.docinfo
1862 | "infoend", "" ->
1863 state.docinfo <- List.rev state.docinfo;
1864 state.uioh#infochanged Docinfo
1866 | "pass", args ->
1867 if args = "fail"
1868 then Wsi.settitle "Wrong password";
1869 let password = getpassword () in
1870 if emptystr password
1871 then error "document is password protected"
1872 else opendoc state.path password
1874 | _ ->
1875 error "unknown cmd `%S'" cmds
1878 let onhist cb =
1879 let rc = cb.rc in
1880 let action = function
1881 | HCprev -> cbget cb ~-1
1882 | HCnext -> cbget cb 1
1883 | HCfirst -> cbget cb ~-(cb.rc)
1884 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1885 and cancel () = cb.rc <- rc
1886 in (action, cancel)
1889 let search pattern forward =
1890 match conf.columns with
1891 | Csplit _ -> impmsg "searching does not work properly in split columns mode"
1892 | Csingle _ | Cmulti _ ->
1893 if nonemptystr pattern
1894 then
1895 let pn, py =
1896 match state.layout with
1897 | [] -> 0, 0
1898 | l :: _ ->
1899 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1901 wcmd "search %d %d %d %d,%s\000"
1902 (btod conf.icase) pn py (btod forward) pattern;
1905 let [@warning "-4"] intentry text key =
1906 let text =
1907 match key with
1908 | Keys.Ascii ('0'..'9' as c) -> addchar text c
1909 | _ ->
1910 state.text <- "invalid key";
1911 text
1913 TEcont text
1916 let linknact f s =
1917 if nonemptystr s
1918 then (
1919 let n =
1920 let l = String.length s in
1921 let rec loop pos n =
1922 if pos = l
1923 then n
1924 else
1925 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
1926 loop (pos+1) (n*26 + m)
1927 in loop 0 0
1929 let rec loop n = function
1930 | [] -> ()
1931 | l :: rest ->
1932 match getopaque l.pageno with
1933 | None -> loop n rest
1934 | Some opaque ->
1935 let m = getlinkcount opaque in
1936 if n < m
1937 then (
1938 let under = getlink opaque n in
1939 f under
1941 else loop (n-m) rest
1943 loop n state.layout;
1947 let [@warning "-4"] linknentry text = function
1948 | Keys.Ascii c ->
1949 let text = addchar text c in
1950 linknact (fun under -> state.text <- undertext under) text;
1951 TEcont text
1952 | _ ->
1953 state.text <- Printf.sprintf "invalid key";
1954 TEcont text
1957 let [@warning "-4"] textentry text = function
1958 | Keys.Ascii c -> TEcont (addchar text c)
1959 | Keys.Code c -> TEcont (text ^ toutf8 c)
1960 | _ -> TEcont text
1963 let reqlayout angle fitmodel =
1964 match state.throttle with
1965 | None ->
1966 if nogeomcmds state.geomcmds
1967 then state.anchor <- getanchor ();
1968 conf.angle <- angle mod 360;
1969 if conf.angle != 0
1970 then (
1971 match state.mode with
1972 | LinkNav _ -> state.mode <- View
1973 | Birdseye _ | Textentry _ | View -> ()
1975 conf.fitmodel <- fitmodel;
1976 invalidate
1977 "reqlayout"
1978 (fun () ->
1979 wcmd "reqlayout %d %d %d"
1980 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
1982 | _ -> ()
1985 let settrim trimmargins trimfuzz =
1986 if nogeomcmds state.geomcmds
1987 then state.anchor <- getanchor ();
1988 conf.trimmargins <- trimmargins;
1989 conf.trimfuzz <- trimfuzz;
1990 let x0, y0, x1, y1 = trimfuzz in
1991 invalidate
1992 "settrim" (fun () ->
1993 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
1994 flushpages ();
1997 let setzoom zoom =
1998 match state.throttle with
1999 | None ->
2000 let zoom = max 0.0001 zoom in
2001 if zoom <> conf.zoom
2002 then (
2003 state.prevzoom <- (conf.zoom, state.x);
2004 conf.zoom <- zoom;
2005 reshape state.winw state.winh;
2006 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2009 | Some (layout, y, started) ->
2010 let time =
2011 match conf.maxwait with
2012 | None -> 0.0
2013 | Some t -> t
2015 let dt = now () -. started in
2016 if dt > time
2017 then (
2018 state.y <- y;
2019 load layout;
2023 let pivotzoom ?(vw=min state.w state.winw)
2024 ?(vh=min (state.maxy-state.y) state.winh)
2025 ?(x=vw/2) ?(y=vh/2) zoom =
2026 let w = float state.w /. zoom in
2027 let hw = w /. 2.0 in
2028 let ratio = float vh /. float vw in
2029 let hh = hw *. ratio in
2030 let x0 = if zoom < 1.0 then 0.0 else float x -. hw in
2031 let y0 = float y -. hh in
2032 gotoxy (state.x - truncate x0) (state.y + truncate y0);
2033 setzoom zoom;
2036 let pivotzoom ?vw ?vh ?x ?y zoom =
2037 if nogeomcmds state.geomcmds then pivotzoom ?vw ?vh ?x ?y zoom
2040 let setcolumns mode columns coverA coverB =
2041 state.prevcolumns <- Some (conf.columns, conf.zoom);
2042 if columns < 0
2043 then (
2044 if isbirdseye mode
2045 then impmsg "split mode doesn't work in bird's eye"
2046 else (
2047 conf.columns <- Csplit (-columns, E.a);
2048 state.x <- 0;
2049 conf.zoom <- 1.0;
2052 else (
2053 if columns < 2
2054 then (
2055 conf.columns <- Csingle E.a;
2056 state.x <- 0;
2057 setzoom 1.0;
2059 else (
2060 conf.columns <- Cmulti ((columns, coverA, coverB), E.a);
2061 conf.zoom <- 1.0;
2064 reshape state.winw state.winh;
2067 let resetmstate () =
2068 state.mstate <- Mnone;
2069 Wsi.setcursor Wsi.CURSOR_INHERIT;
2072 let enterbirdseye () =
2073 let zoom = float conf.thumbw /. float state.winw in
2074 let birdseyepageno =
2075 let cy = state.winh / 2 in
2076 let fold = function
2077 | [] -> 0
2078 | l :: rest ->
2079 let rec fold best = function
2080 | [] -> best.pageno
2081 | l :: rest ->
2082 let d = cy - (l.pagedispy + l.pagevh/2)
2083 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2084 if abs d < abs dbest
2085 then fold l rest
2086 else best.pageno
2087 in fold l rest
2089 fold state.layout
2091 state.mode <-
2092 Birdseye (
2093 { conf with zoom = conf.zoom },
2094 state.x, birdseyepageno, -1, getanchor ()
2096 resetmstate ();
2097 conf.zoom <- zoom;
2098 conf.presentation <- false;
2099 conf.interpagespace <- 10;
2100 conf.hlinks <- false;
2101 conf.fitmodel <- FitPage;
2102 state.x <- 0;
2103 conf.maxwait <- None;
2104 conf.columns <- (
2105 match conf.beyecolumns with
2106 | Some c ->
2107 conf.zoom <- 1.0;
2108 Cmulti ((c, 0, 0), E.a)
2109 | None -> Csingle E.a
2111 if conf.verbose
2112 then
2113 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2114 (100.0*.zoom)
2115 else
2116 state.text <- E.s
2118 reshape state.winw state.winh;
2121 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2122 state.mode <- View;
2123 conf.zoom <- c.zoom;
2124 conf.presentation <- c.presentation;
2125 conf.interpagespace <- c.interpagespace;
2126 conf.maxwait <- c.maxwait;
2127 conf.hlinks <- c.hlinks;
2128 conf.fitmodel <- c.fitmodel;
2129 conf.beyecolumns <- (
2130 match conf.columns with
2131 | Cmulti ((c, _, _), _) -> Some c
2132 | Csingle _ -> None
2133 | Csplit _ -> failwith "leaving bird's eye split mode"
2135 conf.columns <- (
2136 match c.columns with
2137 | Cmulti (c, _) -> Cmulti (c, E.a)
2138 | Csingle _ -> Csingle E.a
2139 | Csplit (c, _) -> Csplit (c, E.a)
2141 if conf.verbose
2142 then
2143 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2144 (100.0*.conf.zoom)
2146 reshape state.winw state.winh;
2147 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2148 state.x <- leftx;
2151 let togglebirdseye () =
2152 match state.mode with
2153 | Birdseye vals -> leavebirdseye vals true
2154 | View -> enterbirdseye ()
2155 | Textentry _ | LinkNav _ -> ()
2158 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2159 let pageno = max 0 (pageno - incr) in
2160 let rec loop = function
2161 | [] -> gotopage1 pageno 0
2162 | l :: _ when l.pageno = pageno ->
2163 if l.pagedispy >= 0 && l.pagey = 0
2164 then G.postRedisplay "upbirdseye"
2165 else gotopage1 pageno 0
2166 | _ :: rest -> loop rest
2168 loop state.layout;
2169 state.text <- E.s;
2170 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2173 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2174 let pageno = min (state.pagecount - 1) (pageno + incr) in
2175 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2176 let rec loop = function
2177 | [] ->
2178 let y, h = getpageyh pageno in
2179 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2180 gotoxy state.x (clamp dy)
2181 | l :: _ when l.pageno = pageno ->
2182 if l.pagevh != l.pageh
2183 then gotoxy state.x (clamp (l.pageh - l.pagevh + conf.interpagespace))
2184 else G.postRedisplay "downbirdseye"
2185 | _ :: rest -> loop rest
2187 loop state.layout;
2188 state.text <- E.s;
2191 let [@warning "-4"] optentry mode _ key =
2192 let btos b = if b then "on" else "off" in
2193 match key with
2194 | Keys.Ascii 's' ->
2195 let ondone s =
2196 try conf.scrollstep <- int_of_string s with exn ->
2197 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2199 TEswitch ("scroll step: ", E.s, None, intentry, ondone, true)
2201 | Keys.Ascii 'A' ->
2202 let ondone s =
2204 conf.autoscrollstep <- boundastep state.winh (int_of_string s);
2205 if state.autoscroll <> None
2206 then state.autoscroll <- Some conf.autoscrollstep
2207 with exn ->
2208 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2210 TEswitch ("auto scroll step: ", E.s, None, intentry, ondone, true)
2212 | Keys.Ascii 'C' ->
2213 let ondone s =
2215 let n, a, b = multicolumns_of_string s in
2216 setcolumns mode n a b;
2217 with exn ->
2218 state.text <- Printf.sprintf "bad columns `%s': %s" s @@ exntos exn
2220 TEswitch ("columns: ", E.s, None, textentry, ondone, true)
2222 | Keys.Ascii 'Z' ->
2223 let ondone s =
2225 let zoom = float (int_of_string s) /. 100.0 in
2226 pivotzoom zoom
2227 with exn ->
2228 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2230 TEswitch ("zoom: ", E.s, None, intentry, ondone, true)
2232 | Keys.Ascii 't' ->
2233 let ondone s =
2235 conf.thumbw <- bound (int_of_string s) 2 4096;
2236 state.text <-
2237 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2238 begin match mode with
2239 | Birdseye beye ->
2240 leavebirdseye beye false;
2241 enterbirdseye ();
2242 | Textentry _ | View | LinkNav _ -> ();
2244 with exn ->
2245 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2247 TEswitch ("thumbnail width: ", E.s, None, intentry, ondone, true)
2249 | Keys.Ascii 'R' ->
2250 let ondone s =
2251 match int_of_string s with
2252 | angle -> reqlayout angle conf.fitmodel
2253 | exception exn ->
2254 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2256 TEswitch ("rotation: ", E.s, None, intentry, ondone, true)
2258 | Keys.Ascii 'i' ->
2259 conf.icase <- not conf.icase;
2260 TEdone ("case insensitive search " ^ (btos conf.icase))
2262 | Keys.Ascii 'p' ->
2263 conf.preload <- not conf.preload;
2264 gotoxy state.x state.y;
2265 TEdone ("preload " ^ (btos conf.preload))
2267 | Keys.Ascii 'v' ->
2268 conf.verbose <- not conf.verbose;
2269 TEdone ("verbose " ^ (btos conf.verbose))
2271 | Keys.Ascii 'd' ->
2272 conf.debug <- not conf.debug;
2273 TEdone ("debug " ^ (btos conf.debug))
2275 | Keys.Ascii 'h' ->
2276 conf.maxhfit <- not conf.maxhfit;
2277 state.maxy <- calcheight ();
2278 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2280 | Keys.Ascii 'c' ->
2281 conf.crophack <- not conf.crophack;
2282 TEdone ("crophack " ^ btos conf.crophack)
2284 | Keys.Ascii 'a' ->
2285 let s =
2286 match conf.maxwait with
2287 | None ->
2288 conf.maxwait <- Some infinity;
2289 "always wait for page to complete"
2290 | Some _ ->
2291 conf.maxwait <- None;
2292 "show placeholder if page is not ready"
2294 TEdone s
2296 | Keys.Ascii 'f' ->
2297 conf.underinfo <- not conf.underinfo;
2298 TEdone ("underinfo " ^ btos conf.underinfo)
2300 | Keys.Ascii 'P' ->
2301 conf.savebmarks <- not conf.savebmarks;
2302 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2304 | Keys.Ascii 'S' ->
2305 let ondone s =
2307 let pageno, py =
2308 match state.layout with
2309 | [] -> 0, 0
2310 | l :: _ ->
2311 l.pageno, l.pagey
2313 conf.interpagespace <- int_of_string s;
2314 docolumns conf.columns;
2315 state.maxy <- calcheight ();
2316 let y = getpagey pageno in
2317 gotoxy state.x (y + py)
2318 with exn ->
2319 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2321 TEswitch ("vertical margin: ", E.s, None, intentry, ondone, true)
2323 | Keys.Ascii 'l' ->
2324 let fm =
2325 match conf.fitmodel with
2326 | FitProportional -> FitWidth
2327 | FitWidth | FitPage -> FitProportional
2329 reqlayout conf.angle fm;
2330 TEdone ("proportional display " ^ btos (fm == FitProportional))
2332 | Keys.Ascii 'T' ->
2333 settrim (not conf.trimmargins) conf.trimfuzz;
2334 TEdone ("trim margins " ^ btos conf.trimmargins)
2336 | Keys.Ascii 'I' ->
2337 conf.invert <- not conf.invert;
2338 TEdone ("invert colors " ^ btos conf.invert)
2340 | Keys.Ascii 'x' ->
2341 let ondone s =
2342 cbput state.hists.sel s;
2343 conf.selcmd <- s;
2345 TEswitch ("selection command: ", E.s, Some (onhist state.hists.sel),
2346 textentry, ondone, true)
2348 | Keys.Ascii 'M' ->
2349 if conf.pax == None
2350 then conf.pax <- Some (ref (0.0, 0, 0))
2351 else conf.pax <- None;
2352 TEdone ("PAX " ^ btos (conf.pax != None))
2354 | (Keys.Ascii c) ->
2355 state.text <- Printf.sprintf "bad option %d `%c'"
2356 (Char.code c) c;
2357 TEstop
2359 | _ ->
2360 TEcont state.text
2363 class type lvsource =
2364 object
2365 method getitemcount : int
2366 method getitem : int -> (string * int)
2367 method hasaction : int -> bool
2368 method exit : uioh:uioh ->
2369 cancel:bool ->
2370 active:int ->
2371 first:int ->
2372 pan:int ->
2373 uioh option
2374 method getactive : int
2375 method getfirst : int
2376 method getpan : int
2377 method getminfo : (int * int) array
2378 end;;
2380 class virtual lvsourcebase = object
2381 val mutable m_active = 0
2382 val mutable m_first = 0
2383 val mutable m_pan = 0
2384 method getactive = m_active
2385 method getfirst = m_first
2386 method getpan = m_pan
2387 method getminfo : (int * int) array = E.a
2388 end;;
2390 let [@warning "-4"] textentrykeyboard
2391 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2392 state.text <- E.s;
2393 let enttext te =
2394 state.mode <- Textentry (te, onleave);
2395 enttext ();
2396 G.postRedisplay "textentrykeyboard enttext";
2398 let histaction cmd =
2399 match opthist with
2400 | None -> ()
2401 | Some (action, _) ->
2402 state.mode <-
2403 Textentry (
2404 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
2406 G.postRedisplay "textentry histaction"
2408 let open Keys in let kt = Wsi.kc2kt key in
2409 match kt with
2410 | Backspace ->
2411 if emptystr text && cancelonempty
2412 then (
2413 onleave Cancel;
2414 G.postRedisplay "textentrykeyboard after cancel";
2416 else
2417 let s = withoutlastutf8 text in
2418 enttext (c, s, opthist, onkey, ondone, cancelonempty)
2420 | Enter | KPenter ->
2421 ondone text;
2422 onleave Confirm;
2423 G.postRedisplay "textentrykeyboard after confirm"
2425 | Up | KPup -> histaction HCprev
2426 | Down | KPdown -> histaction HCnext
2427 | Home | KPhome -> histaction HCfirst
2428 | End | KPend -> histaction HClast
2430 | Escape ->
2431 if emptystr text
2432 then (
2433 begin match opthist with
2434 | None -> ()
2435 | Some (_, onhistcancel) -> onhistcancel ()
2436 end;
2437 onleave Cancel;
2438 state.text <- E.s;
2439 G.postRedisplay "textentrykeyboard after cancel2"
2441 else (
2442 enttext (c, E.s, opthist, onkey, ondone, cancelonempty)
2445 | Delete | KPdelete -> ()
2447 | Code _ | Ascii _ ->
2448 begin match onkey text kt with
2449 | TEdone text ->
2450 ondone text;
2451 onleave Confirm;
2452 G.postRedisplay "textentrykeyboard after confirm2";
2454 | TEcont text ->
2455 enttext (c, text, opthist, onkey, ondone, cancelonempty);
2457 | TEstop ->
2458 onleave Cancel;
2459 G.postRedisplay "textentrykeyboard after cancel3"
2461 | TEswitch te ->
2462 state.mode <- Textentry (te, onleave);
2463 G.postRedisplay "textentrykeyboard switch";
2465 | _ -> vlog "unhandled key"
2468 let firstof first active =
2469 if first > active || abs (first - active) > fstate.maxrows - 1
2470 then max 0 (active - (fstate.maxrows/2))
2471 else first
2474 let calcfirst first active =
2475 if active > first
2476 then
2477 let rows = active - first in
2478 if rows > fstate.maxrows then active - fstate.maxrows else first
2479 else active
2482 let scrollph y maxy =
2483 let sh = float (maxy + state.winh) /. float state.winh in
2484 let sh = float state.winh /. sh in
2485 let sh = max sh (float conf.scrollh) in
2487 let percent = float y /. float maxy in
2488 let position = (float state.winh -. sh) *. percent in
2490 let position =
2491 if position +. sh > float state.winh
2492 then float state.winh -. sh
2493 else position
2495 position, sh;
2498 let adderrmsg src msg =
2499 Buffer.add_string state.errmsgs msg;
2500 state.newerrmsgs <- true;
2501 G.postRedisplay src
2504 let adderrfmt src fmt =
2505 Format.ksprintf (fun s -> adderrmsg src s) fmt;
2508 let coe s = (s :> uioh);;
2510 class listview ~zebra ~helpmode ~(source:lvsource) ~trusted ~modehash =
2511 object (self)
2512 val m_pan = source#getpan
2513 val m_first = source#getfirst
2514 val m_active = source#getactive
2515 val m_qsearch = E.s
2516 val m_prev_uioh = state.uioh
2518 method private elemunder y =
2519 if y < 0
2520 then None
2521 else
2522 let n = y / (fstate.fontsize+1) in
2523 if m_first + n < source#getitemcount
2524 then (
2525 if source#hasaction (m_first + n)
2526 then Some (m_first + n)
2527 else None
2529 else None
2531 method display =
2532 Gl.enable `blend;
2533 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
2534 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2535 filledrect 0. 0. (float state.winw) (float state.winh);
2536 GlDraw.color (1., 1., 1.);
2537 Gl.enable `texture_2d;
2538 let fs = fstate.fontsize in
2539 let nfs = fs + 1 in
2540 let hw = state.winw/3 in
2541 let ww = fstate.wwidth in
2542 let tabw = 17.0*.ww in
2543 let itemcount = source#getitemcount in
2544 let minfo = source#getminfo in
2545 if conf.leftscroll
2546 then (
2547 GlMat.push ();
2548 GlMat.translate ~x:(float conf.scrollbw) ();
2550 let x0 = 0.0 and x1 = float (state.winw - conf.scrollbw - 1) in
2551 let rec loop row =
2552 if (row - m_first) > fstate.maxrows
2553 then ()
2554 else (
2555 if row >= 0 && row < itemcount
2556 then (
2557 let (s, level) = source#getitem row in
2558 let y = (row - m_first) * nfs in
2559 let x = 5.0 +. (float (level + m_pan)) *. ww in
2560 if helpmode
2561 then GlDraw.color
2562 (let c = if row land 1 = 0 then 1.0 else 0.92 in (c,c,c));
2564 if row = m_active
2565 then (
2566 Gl.disable `texture_2d;
2567 let alpha = if source#hasaction row then 0.9 else 0.3 in
2568 GlDraw.color (1., 1., 1.) ~alpha;
2569 linerect (x0 +. 1.) (float (y + 1)) (x1) (float (y + fs + 3));
2570 Gl.enable `texture_2d;
2572 let c =
2573 if zebra && row land 1 = 1
2574 then 0.8
2575 else 1.0
2577 GlDraw.color (c,c,c);
2578 let drawtabularstring s =
2579 let drawstr x s =
2580 let x' = truncate (x0 +. x) in
2581 let s1, s2 = splitatchar s '\000' in
2582 if emptystr s2
2583 then drawstring1 fs x' (y+nfs) s
2584 else
2585 let rec e s =
2586 if emptystr s
2587 then s
2588 else
2589 let s' = withoutlastutf8 s in
2590 let s = s' ^ UniSyms.ellipsis in
2591 let w = measurestr fs s in
2592 if float x' +. w +. ww < float (hw + x')
2593 then s
2594 else e s'
2596 let s1 =
2597 if float x' +. ww +. measurestr fs s1 > float (hw + x')
2598 then e s1
2599 else s1
2601 ignore (drawstring1 fs x' (y+nfs) s1);
2602 drawstring1 fs (hw + x') (y+nfs) s2
2604 if trusted
2605 then
2606 let x = if helpmode && row > 0 then x +. ww else x in
2607 let s1, s2 = splitatchar s '\t' in
2608 if nonemptystr s2
2609 then
2610 let nx = drawstr x s1 in
2611 let sw = nx -. x in
2612 let x = x +. (max tabw sw) in
2613 drawstr x s2
2614 else
2615 let len = String.length s - 2 in
2616 if len > 0 && s.[0] = '\xc2' && s.[1] = '\xb7'
2617 then
2618 let s = String.sub s 2 len in
2619 let x = if not helpmode then x +. ww else x in
2620 GlDraw.color (1.2, 1.2, 1.2);
2621 let vinc = drawstring1 (fs+fs/4)
2622 (truncate (x -. ww)) (y+nfs) s in
2623 GlDraw.color (1., 1., 1.);
2624 vinc +. (float fs *. 0.8)
2625 else
2626 drawstr x s
2627 else
2628 drawstr x s
2630 ignore (drawtabularstring s);
2631 loop (row+1)
2635 loop m_first;
2636 GlDraw.color (1.0, 1.0, 1.0) ~alpha:0.5;
2637 let xadj = 5.0 in
2638 let rec loop row =
2639 if (row - m_first) <= fstate.maxrows
2640 then
2641 if row >= 0 && row < itemcount
2642 then (
2643 let (s, level) = source#getitem row in
2644 let pos0 = nindex s '\000' in
2645 let y = (row - m_first) * nfs in
2646 let x = float (level + m_pan) *. ww in
2647 let (first, last) = minfo.(row) in
2648 let prefix =
2649 if pos0 > 0 && first > pos0
2650 then String.sub s (pos0+1) (first-pos0-1)
2651 else String.sub s 0 first
2653 let suffix = String.sub s first (last - first) in
2654 let w1 = measurestr fstate.fontsize prefix in
2655 let w2 = measurestr fstate.fontsize suffix in
2656 let x = x +. if conf.leftscroll then xadj else 5.0 in
2657 let x = if pos0 > 0 && first > pos0 then x +. float hw else x in
2658 let x0 = x +. w1
2659 and y0 = float (y+2) in
2660 let x1 = x0 +. w2
2661 and y1 = float (y+fs+3) in
2662 filledrect x0 y0 x1 y1;
2663 loop (row+1)
2666 Gl.disable `texture_2d;
2667 if Array.length minfo > 0 then loop m_first;
2668 Gl.disable `blend;
2669 if conf.leftscroll
2670 then GlMat.pop ()
2672 method updownlevel incr =
2673 let len = source#getitemcount in
2674 let curlevel =
2675 if m_active >= 0 && m_active < len
2676 then snd (source#getitem m_active)
2677 else -1
2679 let rec flow i =
2680 if i = len then i-1 else if i = -1 then 0 else
2681 let _, l = source#getitem i in
2682 if l != curlevel then i else flow (i+incr)
2684 let active = flow m_active in
2685 let first = calcfirst m_first active in
2686 G.postRedisplay "outline updownlevel";
2687 {< m_active = active; m_first = first >}
2689 method private key1 key mask =
2690 let set1 active first qsearch =
2691 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2693 let search active pattern incr =
2694 let active = if active = -1 then m_first else active in
2695 let dosearch re =
2696 let rec loop n =
2697 if n >= 0 && n < source#getitemcount
2698 then (
2699 let s, _ = source#getitem n in
2700 match Str.search_forward re s 0 with
2701 | exception Not_found -> loop (n + incr)
2702 | _ -> Some n
2704 else None
2706 loop active
2708 let qpat = Str.quote pattern in
2709 match Str.regexp_case_fold qpat with
2710 | s -> dosearch s
2711 | exception exn ->
2712 adderrfmt "listview key1" "regexp_case_fold for `%S' failed: %S\n"
2713 qpat @@ Printexc.to_string exn;
2714 None
2716 let itemcount = source#getitemcount in
2717 let find start incr =
2718 let rec find i =
2719 if i = -1 || i = itemcount
2720 then -1
2721 else (
2722 if source#hasaction i
2723 then i
2724 else find (i + incr)
2727 find start
2729 let set active first =
2730 let first = bound first 0 (itemcount - fstate.maxrows) in
2731 state.text <- E.s;
2732 coe {< m_active = active; m_first = first; m_qsearch = E.s >}
2734 let navigate incr =
2735 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2736 let active, first =
2737 let incr1 = if incr > 0 then 1 else -1 in
2738 if isvisible m_first m_active
2739 then
2740 let next =
2741 let next = m_active + incr in
2742 let next =
2743 if next < 0 || next >= itemcount
2744 then -1
2745 else find next incr1
2747 if abs (m_active - next) > fstate.maxrows
2748 then -1
2749 else next
2751 if next = -1
2752 then
2753 let first = m_first + incr in
2754 let first = bound first 0 (itemcount - fstate.maxrows) in
2755 let next =
2756 let next = m_active + incr in
2757 let next = bound next 0 (itemcount - 1) in
2758 find next ~-incr1
2760 let active =
2761 if next = -1
2762 then m_active
2763 else (
2764 if isvisible first next
2765 then next
2766 else m_active
2769 active, first
2770 else
2771 let first = min next m_first in
2772 let first =
2773 if abs (next - first) > fstate.maxrows
2774 then first + incr
2775 else first
2777 next, first
2778 else
2779 let first = m_first + incr in
2780 let first = bound first 0 (itemcount - 1) in
2781 let active =
2782 let next = m_active + incr in
2783 let next = bound next 0 (itemcount - 1) in
2784 let next = find next incr1 in
2785 let active =
2786 if next = -1 || abs (m_active - first) > fstate.maxrows
2787 then (
2788 let active = if m_active = -1 then next else m_active in
2789 active
2791 else next
2793 if isvisible first active
2794 then active
2795 else -1
2797 active, first
2799 G.postRedisplay "listview navigate";
2800 set active first;
2802 let open Keys in
2803 let kt = Wsi.kc2kt key in
2804 match [@warning "-4"] kt with
2805 | Ascii (('r'|'s') as c) when Wsi.withctrl mask ->
2806 let incr = if c = 'r' then -1 else 1 in
2807 let active, first =
2808 match search (m_active + incr) m_qsearch incr with
2809 | None ->
2810 state.text <- m_qsearch ^ " [not found]";
2811 m_active, m_first
2812 | Some active ->
2813 state.text <- m_qsearch;
2814 active, firstof m_first active
2816 G.postRedisplay "listview ctrl-r/s";
2817 set1 active first m_qsearch;
2819 | Insert when Wsi.withctrl mask ->
2820 if m_active >= 0 && m_active < source#getitemcount
2821 then (
2822 let s, _ = source#getitem m_active in
2823 selstring s;
2825 coe self
2827 | Backspace ->
2828 if emptystr m_qsearch
2829 then coe self
2830 else (
2831 let qsearch = withoutlastutf8 m_qsearch in
2832 if emptystr qsearch
2833 then (
2834 state.text <- E.s;
2835 G.postRedisplay "listview empty qsearch";
2836 set1 m_active m_first E.s;
2838 else
2839 let active, first =
2840 match search m_active qsearch ~-1 with
2841 | None ->
2842 state.text <- qsearch ^ " [not found]";
2843 m_active, m_first
2844 | Some active ->
2845 state.text <- qsearch;
2846 active, firstof m_first active
2848 G.postRedisplay "listview backspace qsearch";
2849 set1 active first qsearch
2852 | Ascii _ | Code _ ->
2853 let utf8 =
2854 match [@warning "-4"] kt with
2855 | Ascii c -> String.make 1 c
2856 | Code code -> toutf8 code
2857 | _ -> assert false
2859 let pattern = m_qsearch ^ utf8 in
2860 let active, first =
2861 match search m_active pattern 1 with
2862 | None ->
2863 state.text <- pattern ^ " [not found]";
2864 m_active, m_first
2865 | Some active ->
2866 state.text <- pattern;
2867 active, firstof m_first active
2869 G.postRedisplay "listview qsearch add";
2870 set1 active first pattern;
2872 | Escape ->
2873 state.text <- E.s;
2874 if emptystr m_qsearch
2875 then (
2876 G.postRedisplay "list view escape";
2877 let mx, my = state.mpos in
2878 updateunder mx my;
2879 match source#exit ~uioh:(coe self) ~cancel:true ~active:m_active
2880 ~first:m_first ~pan:m_pan with
2881 | None -> m_prev_uioh
2882 | Some uioh -> uioh
2884 else (
2885 G.postRedisplay "list view kill qsearch";
2886 coe {< m_qsearch = E.s >}
2889 | Enter | KPenter ->
2890 state.text <- E.s;
2891 let self = {< m_qsearch = E.s >} in
2892 let opt =
2893 G.postRedisplay "listview enter";
2894 let cancel = not (m_active >= 0 && m_active < source#getitemcount) in
2895 source#exit ~uioh:(coe self) ~cancel
2896 ~active:m_active ~first:m_first ~pan:m_pan;
2898 begin match opt with
2899 | None -> m_prev_uioh
2900 | Some uioh -> uioh
2903 | Delete | KPdelete ->
2904 coe self
2906 | Up | KPup -> navigate ~-1
2907 | Down | KPdown -> navigate 1
2908 | Prior | KPprior -> navigate ~-(fstate.maxrows)
2909 | Next | KPnext -> navigate fstate.maxrows
2911 | Right | KPright ->
2912 state.text <- E.s;
2913 G.postRedisplay "listview right";
2914 coe {< m_pan = m_pan - 1 >}
2916 | Left | KPleft ->
2917 state.text <- E.s;
2918 G.postRedisplay "listview left";
2919 coe {< m_pan = m_pan + 1 >}
2921 | Home | KPhome ->
2922 let active = find 0 1 in
2923 G.postRedisplay "listview home";
2924 set active 0;
2926 | End | KPend ->
2927 let first = max 0 (itemcount - fstate.maxrows) in
2928 let active = find (itemcount - 1) ~-1 in
2929 G.postRedisplay "listview end";
2930 set active first;
2932 | _ -> coe self
2934 method key key mask =
2935 match state.mode with
2936 | Textentry te ->
2937 textentrykeyboard key mask te;
2938 coe self
2939 | Birdseye _ | View | LinkNav _ -> self#key1 key mask
2941 method button button down x y _ =
2942 let opt =
2943 match button with
2944 | 1 when vscrollhit x ->
2945 G.postRedisplay "listview scroll";
2946 if down
2947 then
2948 let _, position, sh = self#scrollph in
2949 if y > truncate position && y < truncate (position +. sh)
2950 then (
2951 state.mstate <- Mscrolly;
2952 Some (coe self)
2954 else
2955 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
2956 let first = truncate (s *. float source#getitemcount) in
2957 let first = min source#getitemcount first in
2958 Some (coe {< m_first = first; m_active = first >})
2959 else (
2960 state.mstate <- Mnone;
2961 Some (coe self);
2963 | 1 when down ->
2964 begin match self#elemunder y with
2965 | Some n ->
2966 G.postRedisplay "listview click";
2967 source#exit ~uioh:(coe {< m_active = n >})
2968 ~cancel:false ~active:n ~first:m_first ~pan:m_pan
2969 | _ ->
2970 Some (coe self)
2972 | n when (n == 4 || n == 5) && not down ->
2973 let len = source#getitemcount in
2974 let first =
2975 if n = 5 && m_first + fstate.maxrows >= len
2976 then
2977 m_first
2978 else
2979 let first = m_first + (if n == 4 then -1 else 1) in
2980 bound first 0 (len - 1)
2982 G.postRedisplay "listview wheel";
2983 Some (coe {< m_first = first >})
2984 | n when (n = 6 || n = 7) && not down ->
2985 let inc = if n = 7 then -1 else 1 in
2986 G.postRedisplay "listview hwheel";
2987 Some (coe {< m_pan = m_pan + inc >})
2988 | _ ->
2989 Some (coe self)
2991 match opt with
2992 | None -> m_prev_uioh
2993 | Some uioh -> uioh
2995 method multiclick _ x y = self#button 1 true x y
2997 method motion _ y =
2998 match state.mstate with
2999 | Mscrolly ->
3000 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3001 let first = truncate (s *. float source#getitemcount) in
3002 let first = min source#getitemcount first in
3003 G.postRedisplay "listview motion";
3004 coe {< m_first = first; m_active = first >}
3005 | Msel _
3006 | Mpan _
3007 | Mscrollx
3008 | Mzoom _
3009 | Mzoomrect _
3010 | Mnone -> coe self
3012 method pmotion x y =
3013 if x < state.winw - conf.scrollbw
3014 then
3015 let n =
3016 match self#elemunder y with
3017 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3018 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3020 let o =
3021 if n != m_active
3022 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3023 else self
3025 coe o
3026 else (
3027 Wsi.setcursor Wsi.CURSOR_INHERIT;
3028 coe self
3031 method infochanged _ = ()
3033 method scrollpw = (0, 0.0, 0.0)
3034 method scrollph =
3035 let nfs = fstate.fontsize + 1 in
3036 let y = m_first * nfs in
3037 let itemcount = source#getitemcount in
3038 let maxi = max 0 (itemcount - fstate.maxrows) in
3039 let maxy = maxi * nfs in
3040 let p, h = scrollph y maxy in
3041 conf.scrollbw, p, h
3043 method modehash = modehash
3044 method eformsgs = false
3045 method alwaysscrolly = true
3046 end;;
3048 class outlinelistview ~zebra ~source =
3049 let settext autonarrow s =
3050 if autonarrow
3051 then
3052 let ss = source#statestr in
3053 state.text <-
3054 if emptystr ss
3055 then "[" ^ s ^ "]"
3056 else "{" ^ ss ^ "} [" ^ s ^ "]"
3057 else state.text <- s
3059 object (self)
3060 inherit listview
3061 ~zebra
3062 ~helpmode:false
3063 ~source:(source :> lvsource)
3064 ~trusted:false
3065 ~modehash:(findkeyhash conf "outline")
3066 as super
3068 val m_autonarrow = false
3070 method! key key mask =
3071 let maxrows =
3072 if emptystr state.text
3073 then fstate.maxrows
3074 else fstate.maxrows - 2
3076 let calcfirst first active =
3077 if active > first
3078 then
3079 let rows = active - first in
3080 if rows > maxrows then active - maxrows else first
3081 else active
3083 let navigate incr =
3084 let active = m_active + incr in
3085 let active = bound active 0 (source#getitemcount - 1) in
3086 let first = calcfirst m_first active in
3087 G.postRedisplay "outline navigate";
3088 coe {< m_active = active; m_first = first >}
3090 let navscroll first =
3091 let active =
3092 let dist = m_active - first in
3093 if dist < 0
3094 then first
3095 else (
3096 if dist < maxrows
3097 then m_active
3098 else first + maxrows
3101 G.postRedisplay "outline navscroll";
3102 coe {< m_first = first; m_active = active >}
3104 let ctrl = Wsi.withctrl mask in
3105 let open Keys in
3106 match Wsi.kc2kt key with
3107 | Ascii 'a' when ctrl ->
3108 let text =
3109 if m_autonarrow
3110 then (source#denarrow; E.s)
3111 else (
3112 let pattern = source#renarrow in
3113 if nonemptystr m_qsearch
3114 then (source#narrow m_qsearch; m_qsearch)
3115 else pattern
3118 settext (not m_autonarrow) text;
3119 G.postRedisplay "toggle auto narrowing";
3120 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
3122 | Ascii '/' when emptystr m_qsearch && not m_autonarrow ->
3123 settext true E.s;
3124 G.postRedisplay "toggle auto narrowing";
3125 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
3127 | Ascii 'n' when ctrl ->
3128 source#narrow m_qsearch;
3129 if not m_autonarrow
3130 then source#add_narrow_pattern m_qsearch;
3131 G.postRedisplay "outline ctrl-n";
3132 coe {< m_first = 0; m_active = 0 >}
3134 | Ascii 'S' when ctrl ->
3135 let active = source#calcactive (getanchor ()) in
3136 let first = firstof m_first active in
3137 G.postRedisplay "outline ctrl-s";
3138 coe {< m_first = first; m_active = active >}
3140 | Ascii 'u' when ctrl ->
3141 G.postRedisplay "outline ctrl-u";
3142 if m_autonarrow && nonemptystr m_qsearch
3143 then (
3144 ignore (source#renarrow);
3145 settext m_autonarrow E.s;
3146 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3148 else (
3149 source#del_narrow_pattern;
3150 let pattern = source#renarrow in
3151 let text =
3152 if emptystr pattern then E.s else "Narrowed to " ^ pattern
3154 settext m_autonarrow text;
3155 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3158 | Ascii 'l' when ctrl ->
3159 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3160 G.postRedisplay "outline ctrl-l";
3161 coe {< m_first = first >}
3163 | Ascii '\t' when m_autonarrow ->
3164 if nonemptystr m_qsearch
3165 then (
3166 G.postRedisplay "outline list view tab";
3167 source#add_narrow_pattern m_qsearch;
3168 settext true E.s;
3169 coe {< m_qsearch = E.s >}
3171 else coe self
3173 | Escape when m_autonarrow ->
3174 if nonemptystr m_qsearch
3175 then source#add_narrow_pattern m_qsearch;
3176 super#key key mask
3178 | Enter | KPenter when m_autonarrow ->
3179 if nonemptystr m_qsearch
3180 then source#add_narrow_pattern m_qsearch;
3181 super#key key mask
3183 | _ when m_autonarrow ->
3184 let pattern = m_qsearch ^ toutf8 key in
3185 G.postRedisplay "outlinelistview autonarrow add";
3186 source#narrow pattern;
3187 settext true pattern;
3188 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3190 | key when m_autonarrow && key = Backspace ->
3191 if emptystr m_qsearch
3192 then coe self
3193 else
3194 let pattern = withoutlastutf8 m_qsearch in
3195 G.postRedisplay "outlinelistview autonarrow backspace";
3196 ignore (source#renarrow);
3197 source#narrow pattern;
3198 settext true pattern;
3199 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3201 | Up | KPup when ctrl ->
3202 navscroll (max 0 (m_first - 1))
3204 | Down | KPdown when ctrl ->
3205 navscroll (min (source#getitemcount - 1) (m_first + 1))
3207 | Up | KPup -> navigate ~-1
3208 | Down | KPdown -> navigate 1
3209 | Prior | KPprior -> navigate ~-(fstate.maxrows)
3210 | Next | KPnext -> navigate fstate.maxrows
3212 | Right | KPright ->
3213 let o =
3214 if ctrl
3215 then (
3216 G.postRedisplay "outline ctrl right";
3217 {< m_pan = m_pan + 1 >}
3219 else self#updownlevel 1
3221 coe o
3223 | Left | KPleft ->
3224 let o =
3225 if ctrl
3226 then (
3227 G.postRedisplay "outline ctrl left";
3228 {< m_pan = m_pan - 1 >}
3230 else self#updownlevel ~-1
3232 coe o
3234 | Home | KPhome ->
3235 G.postRedisplay "outline home";
3236 coe {< m_first = 0; m_active = 0 >}
3238 | End | KPend ->
3239 let active = source#getitemcount - 1 in
3240 let first = max 0 (active - fstate.maxrows) in
3241 G.postRedisplay "outline end";
3242 coe {< m_active = active; m_first = first >}
3244 | Delete|Escape|Insert|Enter|KPdelete|KPenter|KPminus
3245 | KPplus|Ascii _|Code _|Backspace|Fn _ -> super#key key mask
3246 end;;
3248 let genhistoutlines () =
3249 Config.gethist ()
3250 |> List.sort (fun (_, c1, _, _, _, _) (_, c2, _, _, _, _) ->
3251 compare c2.lastvisit c1.lastvisit)
3252 |> List.map
3253 (fun ((path, c, _, _, _, origin) as hist) ->
3254 let path = if nonemptystr origin then origin else path in
3255 let base = mbtoutf8 @@ Filename.basename path in
3256 (base ^ "\000" ^ c.title, 1, Ohistory hist)
3258 |> Array.of_list
3261 let gotohist (path, c, bookmarks, x, anchor, origin) =
3262 Config.save leavebirdseye;
3263 state.anchor <- anchor;
3264 state.bookmarks <- bookmarks;
3265 state.origin <- origin;
3266 state.x <- x;
3267 setconf conf c;
3268 let x0, y0, x1, y1 = conf.trimfuzz in
3269 wcmd "trimset %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1;
3270 reshape ~firsttime:true state.winw state.winh;
3271 opendoc path origin;
3272 setzoom c.zoom;
3275 let makecheckers () =
3276 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3277 following to say:
3278 converted by Issac Trotts. July 25, 2002 *)
3279 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3280 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3281 let id = GlTex.gen_texture () in
3282 GlTex.bind_texture ~target:`texture_2d id;
3283 GlPix.store (`unpack_alignment 1);
3284 GlTex.image2d image;
3285 List.iter (GlTex.parameter ~target:`texture_2d)
3286 [ `mag_filter `nearest; `min_filter `nearest ];
3290 let setcheckers enabled =
3291 match state.checkerstexid with
3292 | None ->
3293 if enabled then state.checkerstexid <- Some (makecheckers ())
3295 | Some checkerstexid ->
3296 if not enabled
3297 then (
3298 GlTex.delete_texture checkerstexid;
3299 state.checkerstexid <- None;
3303 let describe_location () =
3304 let fn = page_of_y state.y in
3305 let ln = page_of_y (state.y + state.winh - 1) in
3306 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
3307 let percent =
3308 if maxy <= 0
3309 then 100.
3310 else (100. *. (float state.y /. float maxy))
3312 if fn = ln
3313 then
3314 Printf.sprintf "page %d of %d [%.2f%%]"
3315 (fn+1) state.pagecount percent
3316 else
3317 Printf.sprintf
3318 "pages %d-%d of %d [%.2f%%]"
3319 (fn+1) (ln+1) state.pagecount percent
3322 let setpresentationmode v =
3323 let n = page_of_y state.y in
3324 state.anchor <- (n, 0.0, 1.0);
3325 conf.presentation <- v;
3326 if conf.fitmodel = FitPage
3327 then reqlayout conf.angle conf.fitmodel;
3328 represent ();
3331 let setbgcol (r, g, b) =
3332 let col =
3333 let r = r *. 255.0 |> truncate
3334 and g = g *. 255.0 |> truncate
3335 and b = b *. 255.0 |> truncate in
3336 r lsl 16 |> (lor) (g lsl 8) |> (lor) b
3338 Wsi.setwinbgcol col;
3341 let enterinfomode =
3342 let btos b = if b then UniSyms.radical else E.s in
3343 let showextended = ref false in
3344 let leave mode _ = state.mode <- mode in
3345 let src =
3346 (object
3347 val mutable m_l = []
3348 val mutable m_a = E.a
3349 val mutable m_prev_uioh = nouioh
3350 val mutable m_prev_mode = View
3352 inherit lvsourcebase
3354 method reset prev_mode prev_uioh =
3355 m_a <- Array.of_list (List.rev m_l);
3356 m_l <- [];
3357 m_prev_mode <- prev_mode;
3358 m_prev_uioh <- prev_uioh;
3360 method int name get set =
3361 m_l <-
3362 (name, `int get, 1,
3363 Action (
3364 fun u ->
3365 let ondone s =
3366 try set (int_of_string s)
3367 with exn ->
3368 state.text <- Printf.sprintf "bad integer `%s': %s"
3369 s @@ exntos exn
3371 state.text <- E.s;
3372 let te = name ^ ": ", E.s, None, intentry, ondone, true in
3373 state.mode <- Textentry (te, leave m_prev_mode);
3375 )) :: m_l
3377 method int_with_suffix name get set =
3378 m_l <-
3379 (name, `intws get, 1,
3380 Action (
3381 fun u ->
3382 let ondone s =
3383 try set (int_of_string_with_suffix s)
3384 with exn ->
3385 state.text <- Printf.sprintf "bad integer `%s': %s"
3386 s @@ exntos exn
3388 state.text <- E.s;
3389 let te =
3390 name ^ ": ", E.s, None, intentry_with_suffix, ondone, true
3392 state.mode <- Textentry (te, leave m_prev_mode);
3394 )) :: m_l
3396 method bool ?(offset=1) ?(btos=btos) name get set =
3397 m_l <-
3398 (name, `bool (btos, get), offset, Action (
3399 fun u ->
3400 let v = get () in
3401 set (not v);
3403 )) :: m_l
3405 method color name get set =
3406 m_l <-
3407 (name, `color get, 1,
3408 Action (
3409 fun u ->
3410 let invalid = (nan, nan, nan) in
3411 let ondone s =
3412 let c =
3413 try color_of_string s
3414 with exn ->
3415 state.text <- Printf.sprintf "bad color `%s': %s"
3416 s @@ exntos exn;
3417 invalid
3419 if c <> invalid
3420 then set c;
3422 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3423 state.text <- color_to_string (get ());
3424 state.mode <- Textentry (te, leave m_prev_mode);
3426 )) :: m_l
3428 method string name get set =
3429 m_l <-
3430 (name, `string get, 1,
3431 Action (
3432 fun u ->
3433 let ondone s = set s in
3434 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3435 state.mode <- Textentry (te, leave m_prev_mode);
3437 )) :: m_l
3439 method colorspace name get set =
3440 m_l <-
3441 (name, `string get, 1,
3442 Action (
3443 fun _ ->
3444 let source =
3445 (object
3446 inherit lvsourcebase
3448 initializer
3449 m_active <- CSTE.to_int conf.colorspace;
3450 m_first <- 0;
3452 method getitemcount =
3453 Array.length CSTE.names
3454 method getitem n =
3455 (CSTE.names.(n), 0)
3456 method exit ~uioh ~cancel ~active ~first ~pan =
3457 ignore (uioh, first, pan);
3458 if not cancel then set active;
3459 None
3460 method hasaction _ = true
3461 end)
3463 state.text <- E.s;
3464 let modehash = findkeyhash conf "info" in
3465 coe (new listview ~zebra:false ~helpmode:false
3466 ~source ~trusted:true ~modehash)
3467 )) :: m_l
3469 method paxmark name get set =
3470 m_l <-
3471 (name, `string get, 1,
3472 Action (
3473 fun _ ->
3474 let source =
3475 (object
3476 inherit lvsourcebase
3478 initializer
3479 m_active <- MTE.to_int conf.paxmark;
3480 m_first <- 0;
3482 method getitemcount = Array.length MTE.names
3483 method getitem n = (MTE.names.(n), 0)
3484 method exit ~uioh ~cancel ~active ~first ~pan =
3485 ignore (uioh, first, pan);
3486 if not cancel then set active;
3487 None
3488 method hasaction _ = true
3489 end)
3491 state.text <- E.s;
3492 let modehash = findkeyhash conf "info" in
3493 coe (new listview ~zebra:false ~helpmode:false
3494 ~source ~trusted:true ~modehash)
3495 )) :: m_l
3497 method fitmodel name get set =
3498 m_l <-
3499 (name, `string get, 1,
3500 Action (
3501 fun _ ->
3502 let source =
3503 (object
3504 inherit lvsourcebase
3506 initializer
3507 m_active <- FMTE.to_int conf.fitmodel;
3508 m_first <- 0;
3510 method getitemcount = Array.length FMTE.names
3511 method getitem n = (FMTE.names.(n), 0)
3512 method exit ~uioh ~cancel ~active ~first ~pan =
3513 ignore (uioh, first, pan);
3514 if not cancel then set active;
3515 None
3516 method hasaction _ = true
3517 end)
3519 state.text <- E.s;
3520 let modehash = findkeyhash conf "info" in
3521 coe (new listview ~zebra:false ~helpmode:false
3522 ~source ~trusted:true ~modehash)
3523 )) :: m_l
3525 method caption s offset =
3526 m_l <- (s, `empty, offset, Noaction) :: m_l
3528 method caption2 s f offset =
3529 m_l <- (s, `string f, offset, Noaction) :: m_l
3531 method getitemcount = Array.length m_a
3533 method getitem n =
3534 let tostr = function
3535 | `int f -> string_of_int (f ())
3536 | `intws f -> string_with_suffix_of_int (f ())
3537 | `string f -> f ()
3538 | `color f -> color_to_string (f ())
3539 | `bool (btos, f) -> btos (f ())
3540 | `empty -> E.s
3542 let name, t, offset, _ = m_a.(n) in
3543 ((let s = tostr t in
3544 if nonemptystr s
3545 then Printf.sprintf "%s\t%s" name s
3546 else name),
3547 offset)
3549 method exit ~uioh ~cancel ~active ~first ~pan =
3550 let uiohopt =
3551 if not cancel
3552 then (
3553 let uioh =
3554 match m_a.(active) with
3555 | _, _, _, Action f -> f uioh
3556 | _, _, _, Noaction -> uioh
3558 Some uioh
3560 else None
3562 m_active <- active;
3563 m_first <- first;
3564 m_pan <- pan;
3565 uiohopt
3567 method hasaction n =
3568 match m_a.(n) with
3569 | _, _, _, Action _ -> true
3570 | _, _, _, Noaction -> false
3572 initializer m_active <- 1
3573 end)
3575 let rec fillsrc prevmode prevuioh =
3576 let sep () = src#caption E.s 0 in
3577 let colorp name get set =
3578 src#string name
3579 (fun () -> color_to_string (get ()))
3580 (fun v ->
3582 let c = color_of_string v in
3583 set c
3584 with exn ->
3585 state.text <-
3586 Printf.sprintf "bad color `%s': %s" v @@ exntos exn
3589 let oldmode = state.mode in
3590 let birdseye = isbirdseye state.mode in
3592 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3594 src#bool "presentation mode"
3595 (fun () -> conf.presentation)
3596 (fun v -> setpresentationmode v);
3598 src#bool "ignore case in searches"
3599 (fun () -> conf.icase)
3600 (fun v -> conf.icase <- v);
3602 src#bool "preload"
3603 (fun () -> conf.preload)
3604 (fun v -> conf.preload <- v);
3606 src#bool "highlight links"
3607 (fun () -> conf.hlinks)
3608 (fun v -> conf.hlinks <- v);
3610 src#bool "under info"
3611 (fun () -> conf.underinfo)
3612 (fun v -> conf.underinfo <- v);
3614 src#bool "persistent bookmarks"
3615 (fun () -> conf.savebmarks)
3616 (fun v -> conf.savebmarks <- v);
3618 src#fitmodel "fit model"
3619 (fun () -> FMTE.to_string conf.fitmodel)
3620 (fun v -> reqlayout conf.angle (FMTE.of_int v));
3622 src#bool "trim margins"
3623 (fun () -> conf.trimmargins)
3624 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3626 src#bool "persistent location"
3627 (fun () -> conf.jumpback)
3628 (fun v -> conf.jumpback <- v);
3630 sep ();
3631 src#int "inter-page space"
3632 (fun () -> conf.interpagespace)
3633 (fun n ->
3634 conf.interpagespace <- n;
3635 docolumns conf.columns;
3636 let pageno, py =
3637 match state.layout with
3638 | [] -> 0, 0
3639 | l :: _ ->
3640 l.pageno, l.pagey
3642 state.maxy <- calcheight ();
3643 let y = getpagey pageno in
3644 gotoxy state.x (y + py)
3647 src#int "page bias"
3648 (fun () -> conf.pagebias)
3649 (fun v -> conf.pagebias <- v);
3651 src#int "scroll step"
3652 (fun () -> conf.scrollstep)
3653 (fun n -> conf.scrollstep <- n);
3655 src#int "horizontal scroll step"
3656 (fun () -> conf.hscrollstep)
3657 (fun v -> conf.hscrollstep <- v);
3659 src#int "auto scroll step"
3660 (fun () ->
3661 match state.autoscroll with
3662 | Some step -> step
3663 | _ -> conf.autoscrollstep)
3664 (fun n ->
3665 let n = boundastep state.winh n in
3666 if state.autoscroll <> None
3667 then state.autoscroll <- Some n;
3668 conf.autoscrollstep <- n);
3670 src#int "zoom"
3671 (fun () -> truncate (conf.zoom *. 100.))
3672 (fun v -> pivotzoom ((float v) /. 100.));
3674 src#int "rotation"
3675 (fun () -> conf.angle)
3676 (fun v -> reqlayout v conf.fitmodel);
3678 src#int "scroll bar width"
3679 (fun () -> conf.scrollbw)
3680 (fun v ->
3681 conf.scrollbw <- v;
3682 reshape state.winw state.winh;
3685 src#int "scroll handle height"
3686 (fun () -> conf.scrollh)
3687 (fun v -> conf.scrollh <- v;);
3689 src#int "thumbnail width"
3690 (fun () -> conf.thumbw)
3691 (fun v ->
3692 conf.thumbw <- min 4096 v;
3693 match oldmode with
3694 | Birdseye beye ->
3695 leavebirdseye beye false;
3696 enterbirdseye ()
3697 | Textentry _
3698 | View
3699 | LinkNav _ -> ()
3702 let mode = state.mode in
3703 src#string "columns"
3704 (fun () ->
3705 match conf.columns with
3706 | Csingle _ -> "1"
3707 | Cmulti (multi, _) -> multicolumns_to_string multi
3708 | Csplit (count, _) -> "-" ^ string_of_int count
3710 (fun v ->
3711 let n, a, b = multicolumns_of_string v in
3712 setcolumns mode n a b);
3714 sep ();
3715 src#caption "Pixmap cache" 0;
3716 src#int_with_suffix "size (advisory)"
3717 (fun () -> conf.memlimit)
3718 (fun v -> conf.memlimit <- v);
3720 src#caption2 "used"
3721 (fun () ->
3722 Printf.sprintf "%s bytes, %d tiles"
3723 (string_with_suffix_of_int state.memused)
3724 (Hashtbl.length state.tilemap)) 1;
3726 sep ();
3727 src#caption "Layout" 0;
3728 src#caption2 "Dimension"
3729 (fun () ->
3730 Printf.sprintf "%dx%d (virtual %dx%d)"
3731 state.winw state.winh
3732 state.w state.maxy)
3734 if conf.debug
3735 then
3736 src#caption2 "Position" (fun () ->
3737 Printf.sprintf "%dx%d" state.x state.y
3739 else
3740 src#caption2 "Position" (fun () -> describe_location ()) 1
3743 sep ();
3744 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3745 "Save these parameters as global defaults at exit"
3746 (fun () -> conf.bedefault)
3747 (fun v -> conf.bedefault <- v)
3750 sep ();
3751 let btos b = if b then UniSyms.lguillemet else UniSyms.rguillemet in
3752 src#bool ~offset:0 ~btos "Extended parameters"
3753 (fun () -> !showextended)
3754 (fun v -> showextended := v; fillsrc prevmode prevuioh);
3755 if !showextended
3756 then (
3757 src#bool "checkers"
3758 (fun () -> conf.checkers)
3759 (fun v -> conf.checkers <- v; setcheckers v);
3760 src#bool "update cursor"
3761 (fun () -> conf.updatecurs)
3762 (fun v -> conf.updatecurs <- v);
3763 src#bool "scroll-bar on the left"
3764 (fun () -> conf.leftscroll)
3765 (fun v -> conf.leftscroll <- v);
3766 src#bool "verbose"
3767 (fun () -> conf.verbose)
3768 (fun v -> conf.verbose <- v);
3769 src#bool "invert colors"
3770 (fun () -> conf.invert)
3771 (fun v -> conf.invert <- v);
3772 src#bool "max fit"
3773 (fun () -> conf.maxhfit)
3774 (fun v -> conf.maxhfit <- v);
3775 src#bool "pax mode"
3776 (fun () -> conf.pax != None)
3777 (fun v ->
3778 if v
3779 then conf.pax <- Some (ref (now (), 0, 0))
3780 else conf.pax <- None);
3781 src#string "uri launcher"
3782 (fun () -> conf.urilauncher)
3783 (fun v -> conf.urilauncher <- v);
3784 src#string "path launcher"
3785 (fun () -> conf.pathlauncher)
3786 (fun v -> conf.pathlauncher <- v);
3787 src#string "tile size"
3788 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3789 (fun v ->
3791 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
3792 conf.tilew <- max 64 w;
3793 conf.tileh <- max 64 h;
3794 flushtiles ();
3795 with exn ->
3796 state.text <- Printf.sprintf "bad tile size `%s': %s"
3797 v @@ exntos exn
3799 src#int "texture count"
3800 (fun () -> conf.texcount)
3801 (fun v ->
3802 if realloctexts v
3803 then conf.texcount <- v
3804 else impmsg "failed to set texture count please retry later"
3806 src#int "slice height"
3807 (fun () -> conf.sliceheight)
3808 (fun v ->
3809 conf.sliceheight <- v;
3810 wcmd "sliceh %d" conf.sliceheight;
3812 src#int "anti-aliasing level"
3813 (fun () -> conf.aalevel)
3814 (fun v ->
3815 conf.aalevel <- bound v 0 8;
3816 state.anchor <- getanchor ();
3817 opendoc state.path state.password;
3819 src#string "page scroll scaling factor"
3820 (fun () -> string_of_float conf.pgscale)
3821 (fun v ->
3823 let s = float_of_string v in
3824 conf.pgscale <- s
3825 with exn ->
3826 state.text <- Printf.sprintf
3827 "bad page scroll scaling factor `%s': %s" v
3828 @@ exntos exn
3831 src#int "ui font size"
3832 (fun () -> fstate.fontsize)
3833 (fun v -> setfontsize (bound v 5 100));
3834 src#int "hint font size"
3835 (fun () -> conf.hfsize)
3836 (fun v -> conf.hfsize <- bound v 5 100);
3837 colorp "background color"
3838 (fun () -> conf.bgcolor)
3839 (fun v -> conf.bgcolor <- v; setbgcol v);
3840 src#bool "crop hack"
3841 (fun () -> conf.crophack)
3842 (fun v -> conf.crophack <- v);
3843 src#string "trim fuzz"
3844 (fun () -> irect_to_string conf.trimfuzz)
3845 (fun v ->
3847 conf.trimfuzz <- irect_of_string v;
3848 if conf.trimmargins
3849 then settrim true conf.trimfuzz;
3850 with exn ->
3851 state.text <- Printf.sprintf "bad irect `%s': %s" v
3852 @@ exntos exn
3854 src#string "throttle"
3855 (fun () ->
3856 match conf.maxwait with
3857 | None -> "show place holder if page is not ready"
3858 | Some time ->
3859 if time = infinity
3860 then "wait for page to fully render"
3861 else
3862 "wait " ^ string_of_float time
3863 ^ " seconds before showing placeholder"
3865 (fun v ->
3867 let f = float_of_string v in
3868 if f <= 0.0
3869 then conf.maxwait <- None
3870 else conf.maxwait <- Some f
3871 with exn ->
3872 state.text <- Printf.sprintf "bad time `%s': %s" v
3873 @@ exntos exn
3875 src#string "ghyll scroll"
3876 (fun () ->
3877 match conf.ghyllscroll with
3878 | None -> E.s
3879 | Some nab -> ghyllscroll_to_string nab
3881 (fun v ->
3882 try conf.ghyllscroll <- ghyllscroll_of_string v
3883 with
3884 | Failure msg ->
3885 state.text <- Printf.sprintf "bad ghyll `%s': %s" v msg
3886 | exn ->
3887 state.text <- Printf.sprintf "bad ghyll `%s': %s" v
3888 @@ exntos exn
3890 src#string "selection command"
3891 (fun () -> conf.selcmd)
3892 (fun v -> conf.selcmd <- v);
3893 src#string "synctex command"
3894 (fun () -> conf.stcmd)
3895 (fun v -> conf.stcmd <- v);
3896 src#string "pax command"
3897 (fun () -> conf.paxcmd)
3898 (fun v -> conf.paxcmd <- v);
3899 src#string "ask password command"
3900 (fun () -> conf.passcmd)
3901 (fun v -> conf.passcmd <- v);
3902 src#string "save path command"
3903 (fun () -> conf.savecmd)
3904 (fun v -> conf.savecmd <- v);
3905 src#colorspace "color space"
3906 (fun () -> CSTE.to_string conf.colorspace)
3907 (fun v ->
3908 conf.colorspace <- CSTE.of_int v;
3909 wcmd "cs %d" v;
3910 load state.layout;
3912 src#paxmark "pax mark method"
3913 (fun () -> MTE.to_string conf.paxmark)
3914 (fun v -> conf.paxmark <- MTE.of_int v);
3915 if bousable () && !opengl_has_pbo
3916 then
3917 src#bool "use PBO"
3918 (fun () -> conf.usepbo)
3919 (fun v -> conf.usepbo <- v);
3920 src#bool "mouse wheel scrolls pages"
3921 (fun () -> conf.wheelbypage)
3922 (fun v -> conf.wheelbypage <- v);
3923 src#bool "open remote links in a new instance"
3924 (fun () -> conf.riani)
3925 (fun v -> conf.riani <- v);
3926 src#bool "edit annotations inline"
3927 (fun () -> conf.annotinline)
3928 (fun v -> conf.annotinline <- v);
3929 src#bool "coarse positioning in presentation mode"
3930 (fun () -> conf.coarseprespos)
3931 (fun v -> conf.coarseprespos <- v);
3932 src#bool "use document CSS"
3933 (fun () -> conf.usedoccss)
3934 (fun v ->
3935 conf.usedoccss <- v;
3936 state.anchor <- getanchor ();
3937 opendoc state.path state.password;
3941 sep ();
3942 src#caption "Document" 0;
3943 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
3944 src#caption2 "Pages"
3945 (fun () -> string_of_int state.pagecount) 1;
3946 src#caption2 "Dimensions"
3947 (fun () -> string_of_int (List.length state.pdims)) 1;
3948 if nonemptystr conf.css
3949 then src#caption2 "CSS" (fun () -> conf.css) 1;
3950 if conf.trimmargins
3951 then (
3952 sep ();
3953 src#caption "Trimmed margins" 0;
3954 src#caption2 "Dimensions"
3955 (fun () -> string_of_int (List.length state.pdims)) 1;
3958 sep ();
3959 src#caption "OpenGL" 0;
3960 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
3961 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
3963 sep ();
3964 src#caption "Location" 0;
3965 if nonemptystr state.origin
3966 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
3967 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
3969 src#reset prevmode prevuioh;
3971 fun () ->
3972 state.text <- E.s;
3973 resetmstate ();
3974 let prevmode = state.mode
3975 and prevuioh = state.uioh in
3976 fillsrc prevmode prevuioh;
3977 let source = (src :> lvsource) in
3978 let modehash = findkeyhash conf "info" in
3979 state.uioh <-
3980 coe (object (self)
3981 inherit listview ~zebra:false ~helpmode:false
3982 ~source ~trusted:true ~modehash as super
3983 val mutable m_prevmemused = 0
3984 method! infochanged = function
3985 | Memused ->
3986 if m_prevmemused != state.memused
3987 then (
3988 m_prevmemused <- state.memused;
3989 G.postRedisplay "memusedchanged";
3991 | Pdim -> G.postRedisplay "pdimchanged"
3992 | Docinfo -> fillsrc prevmode prevuioh
3994 method! key key mask =
3995 if not (Wsi.withctrl mask)
3996 then
3997 let open Keys in
3998 match [@warning "-4"] Wsi.kc2kt key with
3999 | Left | KPleft -> coe (self#updownlevel ~-1)
4000 | Right | KPright -> coe (self#updownlevel 1)
4001 | _ -> super#key key mask
4002 else super#key key mask
4003 end);
4004 G.postRedisplay "info";
4007 let enterhelpmode =
4008 let source =
4009 (object
4010 inherit lvsourcebase
4011 method getitemcount = Array.length state.help
4012 method getitem n =
4013 let s, l, _ = state.help.(n) in
4014 (s, l)
4016 method exit ~uioh ~cancel ~active ~first ~pan =
4017 let optuioh =
4018 if not cancel
4019 then (
4020 match state.help.(active) with
4021 | _, _, Action f -> Some (f uioh)
4022 | _, _, Noaction -> Some uioh
4024 else None
4026 m_active <- active;
4027 m_first <- first;
4028 m_pan <- pan;
4029 optuioh
4031 method hasaction n =
4032 match state.help.(n) with
4033 | _, _, Action _ -> true
4034 | _, _, Noaction -> false
4036 initializer
4037 m_active <- -1
4038 end)
4039 in fun () ->
4040 let modehash = findkeyhash conf "help" in
4041 resetmstate ();
4042 state.uioh <- coe (new listview
4043 ~zebra:false ~helpmode:true
4044 ~source ~trusted:true ~modehash);
4045 G.postRedisplay "help";
4048 let entermsgsmode =
4049 let msgsource =
4050 (object
4051 inherit lvsourcebase
4052 val mutable m_items = E.a
4054 method getitemcount = 1 + Array.length m_items
4056 method getitem n =
4057 if n = 0
4058 then "[Clear]", 0
4059 else m_items.(n-1), 0
4061 method exit ~uioh ~cancel ~active ~first ~pan =
4062 ignore uioh;
4063 if not cancel
4064 then (
4065 if active = 0
4066 then Buffer.clear state.errmsgs;
4068 m_active <- active;
4069 m_first <- first;
4070 m_pan <- pan;
4071 None
4073 method hasaction n =
4074 n = 0
4076 method reset =
4077 state.newerrmsgs <- false;
4078 let l = Str.split newlinere (Buffer.contents state.errmsgs) in
4079 m_items <- Array.of_list l
4081 initializer
4082 m_active <- 0
4083 end)
4084 in fun () ->
4085 state.text <- E.s;
4086 resetmstate ();
4087 msgsource#reset;
4088 let source = (msgsource :> lvsource) in
4089 let modehash = findkeyhash conf "listview" in
4090 state.uioh <-
4091 coe (object
4092 inherit listview ~zebra:false ~helpmode:false
4093 ~source ~trusted:false ~modehash as super
4094 method! display =
4095 if state.newerrmsgs
4096 then msgsource#reset;
4097 super#display
4098 end);
4099 G.postRedisplay "msgs";
4102 let getusertext s =
4103 let editor = getenvwithdef "EDITOR" E.s in
4104 if emptystr editor
4105 then E.s
4106 else
4107 let tmppath = Filename.temp_file "llpp" "note" in
4108 if nonemptystr s
4109 then (
4110 let oc = open_out tmppath in
4111 output_string oc s;
4112 close_out oc;
4114 let execstr = editor ^ " " ^ tmppath in
4115 let s =
4116 match spawn execstr [] with
4117 | exception exn ->
4118 impmsg "spawn(%S) failed: %s" execstr @@ exntos exn;
4120 | pid ->
4121 match Unix.waitpid [] pid with
4122 | exception exn ->
4123 impmsg "waitpid(%d) failed: %s" pid @@ exntos exn;
4125 | (_pid, status) ->
4126 match status with
4127 | Unix.WEXITED 0 -> filecontents tmppath
4128 | Unix.WEXITED n ->
4129 impmsg "editor process(%s) exited abnormally: %d" execstr n;
4131 | Unix.WSIGNALED n ->
4132 impmsg "editor process(%s) was killed by signal %d" execstr n;
4134 | Unix.WSTOPPED n ->
4135 impmsg "editor(%s) process was stopped by signal %d" execstr n;
4138 match Unix.unlink tmppath with
4139 | exception exn ->
4140 impmsg "failed to ulink %S: %s" tmppath @@ exntos exn;
4142 | () -> s
4145 let enterannotmode opaque slinkindex =
4146 let msgsource =
4147 (object
4148 inherit lvsourcebase
4149 val mutable m_text = E.s
4150 val mutable m_items = E.a
4152 method getitemcount = Array.length m_items
4154 method getitem n =
4155 let label, _func = m_items.(n) in
4156 label, 0
4158 method exit ~uioh ~cancel ~active ~first ~pan =
4159 ignore (uioh, first, pan);
4160 if not cancel
4161 then (
4162 let _label, func = m_items.(active) in
4163 func ()
4165 None
4167 method hasaction n = nonemptystr @@ fst m_items.(n)
4169 method reset s =
4170 let rec split accu b i =
4171 let p = b+i in
4172 if p = String.length s
4173 then (String.sub s b (p-b), unit) :: accu
4174 else
4175 if (i > 70 && s.[p] = ' ') || s.[p] = '\r' || s.[p] = '\n'
4176 then
4177 let ss = if i = 0 then E.s else String.sub s b i in
4178 split ((ss, unit)::accu) (p+1) 0
4179 else
4180 split accu b (i+1)
4182 let cleanup () =
4183 wcmd "freepage %s" (~> opaque);
4184 let keys =
4185 Hashtbl.fold (fun key opaque' accu ->
4186 if opaque' = opaque'
4187 then key :: accu else accu) state.pagemap []
4189 List.iter (Hashtbl.remove state.pagemap) keys;
4190 flushtiles ();
4191 gotoxy state.x state.y
4193 let dele () =
4194 delannot opaque slinkindex;
4195 cleanup ();
4197 let edit inline () =
4198 let update s =
4199 if emptystr s
4200 then dele ()
4201 else (
4202 modannot opaque slinkindex s;
4203 cleanup ();
4206 if inline
4207 then
4208 let mode = state.mode in
4209 state.mode <-
4210 Textentry (
4211 ("annotation: ", m_text, None, textentry, update, true),
4212 fun _ -> state.mode <- mode);
4213 state.text <- E.s;
4214 enttext ();
4215 else
4216 let s = getusertext m_text in
4217 update s
4219 m_text <- s;
4220 m_items <-
4221 ( "[Copy]", fun () -> selstring m_text)
4222 :: ("[Delete]", dele)
4223 :: ("[Edit]", edit conf.annotinline)
4224 :: (E.s, unit)
4225 :: split [] 0 0 |> List.rev |> Array.of_list
4227 initializer
4228 m_active <- 0
4229 end)
4231 state.text <- E.s;
4232 let s = getannotcontents opaque slinkindex in
4233 resetmstate ();
4234 msgsource#reset s;
4235 let source = (msgsource :> lvsource) in
4236 let modehash = findkeyhash conf "listview" in
4237 state.uioh <- coe (object
4238 inherit listview ~zebra:false ~helpmode:false
4239 ~source ~trusted:false ~modehash
4240 end);
4241 G.postRedisplay "enterannotmode";
4244 let gotoremote spec =
4245 let filename, dest = splitatchar spec '#' in
4246 let getpath filename =
4247 let path =
4248 if nonemptystr filename
4249 then
4250 if Filename.is_relative filename
4251 then
4252 let dir = Filename.dirname state.path in
4253 let dir =
4254 if Filename.is_implicit dir
4255 then Filename.concat (Sys.getcwd ()) dir
4256 else dir
4258 Filename.concat dir filename
4259 else filename
4260 else E.s
4262 if Sys.file_exists path
4263 then path
4264 else E.s
4266 let path = getpath filename in
4267 let dospawn lcmd =
4268 if conf.riani
4269 then
4270 let cmd = Lazy.force_val lcmd in
4271 match spawn cmd with
4272 | _pid -> ()
4273 | exception exn -> dolog "failed to execute `%s': %s" cmd @@ exntos exn
4274 else
4275 let anchor = getanchor () in
4276 let ranchor = state.path, state.password, anchor, state.origin in
4277 state.origin <- E.s;
4278 state.ranchors <- ranchor :: state.ranchors;
4279 opendoc path E.s;
4281 if substratis spec 0 "page="
4282 then
4283 match Scanf.sscanf spec "page=%d" (fun n -> n) with
4284 | pageno ->
4285 state.anchor <- (pageno, 0.0, 0.0);
4286 dospawn @@ lazy (Printf.sprintf "%s -page %d %S" !selfexec pageno path);
4287 | exception exn ->
4288 adderrfmt "error parsing remote destination" "page: %s" @@ exntos exn
4289 else (
4290 state.nameddest <- dest;
4291 dospawn @@ lazy (!selfexec ^ " " ^ path ^ " -dest " ^ dest)
4295 let gotounder = function
4296 | Ulinkuri s when isexternallink s ->
4297 if substratis s 0 "file://"
4298 then gotoremote @@ String.sub s 7 (String.length s - 7)
4299 else gotouri s
4300 | Ulinkuri s ->
4301 let pageno, x, y = uritolocation s in
4302 addnav ();
4303 gotopagexy !wtmode pageno x y
4304 | Utext _ | Unone -> ()
4305 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
4308 let gotooutline (_, _, kind) =
4309 match kind with
4310 | Onone -> ()
4311 | Oanchor anchor ->
4312 let (pageno, y, _) = anchor in
4313 let y = getanchory
4314 (if conf.presentation then (pageno, y, 1.0) else anchor)
4316 addnav ();
4317 gotoghyll y
4318 | Ouri uri -> gotounder (Ulinkuri uri)
4319 | Olaunch _cmd -> failwith "gotounder (Ulaunch cmd)"
4320 | Oremote _remote -> failwith "gotounder (Uremote remote)"
4321 | Ohistory hist -> gotohist hist
4322 | Oremotedest _remotedest -> failwith "gotounder (Uremotedest remotedest)"
4325 class outlinesoucebase fetchoutlines = object (self)
4326 inherit lvsourcebase
4327 val mutable m_items = E.a
4328 val mutable m_minfo = E.a
4329 val mutable m_orig_items = E.a
4330 val mutable m_orig_minfo = E.a
4331 val mutable m_narrow_patterns = []
4332 val mutable m_gen = -1
4334 method getitemcount = Array.length m_items
4336 method getitem n =
4337 let s, n, _ = m_items.(n) in
4338 (s, n+0)
4340 method exit ~(uioh:uioh) ~cancel ~active ~(first:int) ~pan : uioh option =
4341 ignore (uioh, first);
4342 let items, minfo =
4343 if m_narrow_patterns = []
4344 then m_orig_items, m_orig_minfo
4345 else m_items, m_minfo
4347 m_pan <- pan;
4348 if not cancel
4349 then (
4350 m_items <- items;
4351 m_minfo <- minfo;
4352 gotooutline m_items.(active);
4354 else (
4355 m_items <- items;
4356 m_minfo <- minfo;
4358 None
4360 method hasaction (_:int) = true
4362 method greetmsg =
4363 if Array.length m_items != Array.length m_orig_items
4364 then
4365 let s =
4366 match m_narrow_patterns with
4367 | one :: [] -> one
4368 | many -> String.concat UniSyms.ellipsis (List.rev many)
4370 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
4371 else E.s
4373 method statestr =
4374 match m_narrow_patterns with
4375 | [] -> E.s
4376 | one :: [] -> one
4377 | head :: _ -> UniSyms.ellipsis ^ head
4379 method narrow pattern =
4380 match Str.regexp_case_fold pattern with
4381 | exception _ -> ()
4382 | re ->
4383 let rec loop accu minfo n =
4384 if n = -1
4385 then (
4386 m_items <- Array.of_list accu;
4387 m_minfo <- Array.of_list minfo;
4389 else
4390 let (s, _, _) as o = m_items.(n) in
4391 let accu, minfo =
4392 match Str.search_forward re s 0 with
4393 | exception Not_found -> accu, minfo
4394 | first -> o :: accu, (first, Str.match_end ()) :: minfo
4396 loop accu minfo (n-1)
4398 loop [] [] (Array.length m_items - 1)
4400 method! getminfo = m_minfo
4402 method denarrow =
4403 m_orig_items <- fetchoutlines ();
4404 m_minfo <- m_orig_minfo;
4405 m_items <- m_orig_items
4407 method add_narrow_pattern pattern =
4408 m_narrow_patterns <- pattern :: m_narrow_patterns
4410 method del_narrow_pattern =
4411 match m_narrow_patterns with
4412 | _ :: rest -> m_narrow_patterns <- rest
4413 | [] -> ()
4415 method renarrow =
4416 self#denarrow;
4417 match m_narrow_patterns with
4418 | pattern :: [] -> self#narrow pattern; pattern
4419 | list ->
4420 List.fold_left (fun accu pattern ->
4421 self#narrow pattern;
4422 pattern ^ UniSyms.ellipsis ^ accu) E.s list
4424 method calcactive (_:anchor) = 0
4426 method reset anchor items =
4427 if state.gen != m_gen
4428 then (
4429 m_orig_items <- items;
4430 m_items <- items;
4431 m_narrow_patterns <- [];
4432 m_minfo <- E.a;
4433 m_orig_minfo <- E.a;
4434 m_gen <- state.gen;
4436 else (
4437 if items != m_orig_items
4438 then (
4439 m_orig_items <- items;
4440 if m_narrow_patterns == []
4441 then m_items <- items;
4444 let active = self#calcactive anchor in
4445 m_active <- active;
4446 m_first <- firstof m_first active
4450 let outlinesource fetchoutlines =
4451 (object
4452 inherit outlinesoucebase fetchoutlines
4453 method! calcactive anchor =
4454 let rely = getanchory anchor in
4455 let rec loop n best bestd =
4456 if n = Array.length m_items
4457 then best
4458 else
4459 let _, _, kind = m_items.(n) in
4460 match kind with
4461 | Oanchor anchor ->
4462 let orely = getanchory anchor in
4463 let d = abs (orely - rely) in
4464 if d < bestd
4465 then loop (n+1) n d
4466 else loop (n+1) best bestd
4467 | Onone | Oremote _ | Olaunch _
4468 | Oremotedest _ | Ouri _ | Ohistory _ ->
4469 loop (n+1) best bestd
4471 loop 0 ~-1 max_int
4472 end)
4475 let enteroutlinemode, enterbookmarkmode, enterhistmode =
4476 let mkselector sourcetype =
4477 let fetchoutlines () =
4478 match sourcetype with
4479 | `bookmarks -> Array.of_list state.bookmarks
4480 | `outlines -> state.outlines
4481 | `history -> genhistoutlines ()
4483 let source =
4484 if sourcetype = `history
4485 then new outlinesoucebase fetchoutlines
4486 else outlinesource fetchoutlines
4488 fun errmsg ->
4489 let outlines = fetchoutlines () in
4490 if Array.length outlines = 0
4491 then (
4492 showtext ' ' errmsg;
4494 else (
4495 resetmstate ();
4496 Wsi.setcursor Wsi.CURSOR_INHERIT;
4497 let anchor = getanchor () in
4498 source#reset anchor outlines;
4499 state.text <- source#greetmsg;
4500 state.uioh <-
4501 coe (new outlinelistview ~zebra:(sourcetype=`history) ~source);
4502 G.postRedisplay "enter selector";
4505 let mkenter sourcetype errmsg =
4506 let enter = mkselector sourcetype in
4507 fun () -> enter errmsg
4509 mkenter `outlines "document has no outline"
4510 , mkenter `bookmarks "document has no bookmarks (yet)"
4511 , mkenter `history "history is empty"
4514 let quickbookmark ?title () =
4515 match state.layout with
4516 | [] -> ()
4517 | l :: _ ->
4518 let title =
4519 match title with
4520 | None ->
4521 let tm = Unix.localtime (now ()) in
4522 Printf.sprintf
4523 "Quick (page %d) (bookmarked at %02d/%02d/%d %02d:%02d)"
4524 (l.pageno+1)
4525 tm.Unix.tm_mday
4526 (tm.Unix.tm_mon+1)
4527 (tm.Unix.tm_year + 1900)
4528 tm.Unix.tm_hour
4529 tm.Unix.tm_min
4530 | Some title -> title
4532 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4535 let setautoscrollspeed step goingdown =
4536 let incr = max 1 ((abs step) / 2) in
4537 let incr = if goingdown then incr else -incr in
4538 let astep = boundastep state.winh (step + incr) in
4539 state.autoscroll <- Some astep;
4542 let canpan () =
4543 match conf.columns with
4544 | Csplit _ -> true
4545 | Csingle _ | Cmulti _ -> state.x != 0 || conf.zoom > 1.0
4548 let panbound x = bound x (-state.w) state.winw;;
4550 let existsinrow pageno (columns, coverA, coverB) p =
4551 let last = ((pageno - coverA) mod columns) + columns in
4552 let rec any = function
4553 | [] -> false
4554 | l :: rest ->
4555 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4556 then p l
4557 else (
4558 if not (p l)
4559 then (if l.pageno = last then false else any rest)
4560 else true
4563 any state.layout
4566 let nextpage () =
4567 match state.layout with
4568 | [] ->
4569 let pageno = page_of_y state.y in
4570 gotoghyll (getpagey (pageno+1))
4571 | l :: rest ->
4572 match conf.columns with
4573 | Csingle _ ->
4574 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4575 then
4576 let y = clamp (pgscale state.winh) in
4577 gotoghyll y
4578 else
4579 let pageno = min (l.pageno+1) (state.pagecount-1) in
4580 gotoghyll (getpagey pageno)
4581 | Cmulti ((c, _, _) as cl, _) ->
4582 if conf.presentation
4583 && (existsinrow l.pageno cl
4584 (fun l -> l.pageh > l.pagey + l.pagevh))
4585 then
4586 let y = clamp (pgscale state.winh) in
4587 gotoghyll y
4588 else
4589 let pageno = min (l.pageno+c) (state.pagecount-1) in
4590 gotoghyll (getpagey pageno)
4591 | Csplit (n, _) ->
4592 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4593 then
4594 let pagey, pageh = getpageyh l.pageno in
4595 let pagey = pagey + pageh * l.pagecol in
4596 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4597 gotoghyll (pagey + pageh + ips)
4600 let prevpage () =
4601 match state.layout with
4602 | [] ->
4603 let pageno = page_of_y state.y in
4604 gotoghyll (getpagey (pageno-1))
4605 | l :: _ ->
4606 match conf.columns with
4607 | Csingle _ ->
4608 if conf.presentation && l.pagey != 0
4609 then
4610 gotoghyll (clamp (pgscale ~-(state.winh)))
4611 else
4612 let pageno = max 0 (l.pageno-1) in
4613 gotoghyll (getpagey pageno)
4614 | Cmulti ((c, _, coverB) as cl, _) ->
4615 if conf.presentation &&
4616 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4617 then
4618 gotoghyll (clamp (pgscale ~-(state.winh)))
4619 else
4620 let decr =
4621 if l.pageno = state.pagecount - coverB
4622 then 1
4623 else c
4625 let pageno = max 0 (l.pageno-decr) in
4626 gotoghyll (getpagey pageno)
4627 | Csplit (n, _) ->
4628 let y =
4629 if l.pagecol = 0
4630 then
4631 if l.pageno = 0
4632 then l.pagey
4633 else
4634 let pageno = max 0 (l.pageno-1) in
4635 let pagey, pageh = getpageyh pageno in
4636 pagey + (n-1)*pageh
4637 else
4638 let pagey, pageh = getpageyh l.pageno in
4639 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4641 gotoghyll y
4644 let save () =
4645 if emptystr conf.savecmd
4646 then error "don't know where to save modified document"
4647 else
4648 let savecmd = Str.global_replace percentsre state.path conf.savecmd in
4649 let path =
4650 getcmdoutput
4651 (fun s -> error "failed to obtain path to the saved copy: %s" s)
4652 savecmd
4654 if nonemptystr path
4655 then
4656 let tmp = path ^ ".tmp" in
4657 savedoc tmp;
4658 Unix.rename tmp path;
4661 let viewkeyboard key mask =
4662 let enttext te =
4663 let mode = state.mode in
4664 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4665 state.text <- E.s;
4666 enttext ();
4667 G.postRedisplay "view:enttext"
4669 let ctrl = Wsi.withctrl mask in
4670 let open Keys in
4671 match Wsi.kc2kt key with
4672 | Ascii 'Q' -> exit 0
4674 | Ascii 'W' ->
4675 if hasunsavedchanges ()
4676 then save ()
4678 | Insert ->
4679 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4680 then (
4681 state.mode <- (
4682 match state.lnava with
4683 | None -> LinkNav (Ltgendir 0)
4684 | Some pn -> LinkNav (Ltexact pn)
4686 gotoxy state.x state.y;
4688 else impmsg "keyboard link navigation does not work under rotation"
4690 | Escape | Ascii 'q' ->
4691 begin match state.mstate with
4692 | Mzoomrect _ ->
4693 resetmstate ();
4694 G.postRedisplay "kill rect";
4695 | Msel _
4696 | Mpan _
4697 | Mscrolly | Mscrollx
4698 | Mzoom _
4699 | Mnone ->
4700 begin match state.mode with
4701 | LinkNav ln ->
4702 begin match ln with
4703 | Ltexact pl -> state.lnava <- Some pl
4704 | Ltgendir _ | Ltnotready _ -> state.lnava <- None
4705 end;
4706 state.mode <- View;
4707 G.postRedisplay "esc leave linknav"
4708 | Birdseye _ | Textentry _ | View ->
4709 match state.ranchors with
4710 | [] -> raise Quit
4711 | (path, password, anchor, origin) :: rest ->
4712 state.ranchors <- rest;
4713 state.anchor <- anchor;
4714 state.origin <- origin;
4715 state.nameddest <- E.s;
4716 opendoc path password
4717 end;
4718 end;
4720 | Backspace ->
4721 gotoghyll (getnav ~-1)
4723 | Ascii 'o' ->
4724 enteroutlinemode ()
4726 | Ascii 'H' ->
4727 enterhistmode ()
4729 | Ascii 'u' ->
4730 state.rects <- [];
4731 state.text <- E.s;
4732 Hashtbl.iter (fun _ opaque ->
4733 clearmark opaque;
4734 Hashtbl.clear state.prects) state.pagemap;
4735 G.postRedisplay "dehighlight";
4737 | (Ascii '/' | Ascii '?') as pv ->
4738 let ondone isforw s =
4739 cbput state.hists.pat s;
4740 state.searchpattern <- s;
4741 search s isforw
4743 let s = String.make 1 (Char.chr key) in
4744 enttext (s, E.s, Some (onhist state.hists.pat),
4745 textentry, ondone (pv = Ascii '/'), true)
4747 | Ascii '+' | KPplus | Ascii '=' when ctrl ->
4748 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4749 pivotzoom (conf.zoom +. incr)
4751 | Ascii '+' | KPplus ->
4752 let ondone s =
4753 let n =
4754 try int_of_string s with exn ->
4755 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn;
4756 max_int
4758 if n != max_int
4759 then (
4760 conf.pagebias <- n;
4761 state.text <- "page bias is now " ^ string_of_int n;
4764 enttext ("page bias: ", E.s, None, intentry, ondone, true)
4766 | Ascii '-' | KPminus when ctrl ->
4767 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4768 pivotzoom (max 0.01 (conf.zoom -. decr))
4770 | Ascii '-' | KPminus ->
4771 let ondone msg = state.text <- msg in
4772 enttext (
4773 "option [acfhilpstvxACFPRSZTISM]: ", E.s, None,
4774 optentry state.mode, ondone, true
4777 | Ascii '0' when ctrl ->
4778 if conf.zoom = 1.0
4779 then gotoxy 0 state.y
4780 else setzoom 1.0
4782 | Ascii ('1'|'2' as c)
4783 when ctrl && conf.fitmodel != FitPage ->
4784 let cols =
4785 match conf.columns with
4786 | Csingle _ | Cmulti _ -> 1
4787 | Csplit (n, _) -> n
4789 let h = state.winh -
4790 conf.interpagespace lsl (if conf.presentation then 1 else 0)
4792 let zoom = zoomforh state.winw h 0 cols in
4793 if zoom > 0.0 && (c = '2' || zoom < 1.0)
4794 then setzoom zoom
4796 | Ascii '3' when ctrl ->
4797 let fm =
4798 match conf.fitmodel with
4799 | FitWidth -> FitProportional
4800 | FitProportional -> FitPage
4801 | FitPage -> FitWidth
4803 state.text <- "fit model: " ^ FMTE.to_string fm;
4804 reqlayout conf.angle fm
4806 | Ascii '4' when ctrl ->
4807 let zoom = getmaxw () /. float state.winw in
4808 if zoom > 0.0 then setzoom zoom
4810 | Fn 9 ->
4811 togglebirdseye ()
4813 | Ascii '9' when ctrl ->
4814 togglebirdseye ()
4816 | Ascii ('0'..'9')
4817 when not ctrl ->
4818 let ondone s =
4819 let n =
4820 try int_of_string s with exn ->
4821 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn;
4824 if n >= 0
4825 then (
4826 addnav ();
4827 cbput state.hists.pag (string_of_int n);
4828 gotopage1 (n + conf.pagebias - 1) 0;
4831 let [@warning "-4"] pageentry text = function
4832 | Keys.Ascii 'g' -> TEdone text
4833 | key -> intentry text key
4835 let text = String.make 1 (Char.chr key) in
4836 enttext (":", text, Some (onhist state.hists.pag),
4837 pageentry, ondone, true)
4839 | Ascii 'b' ->
4840 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
4841 G.postRedisplay "toggle scrollbar";
4843 | Ascii 'B' ->
4844 state.bzoom <- not state.bzoom;
4845 state.rects <- [];
4846 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
4848 | Ascii 'l' ->
4849 conf.hlinks <- not conf.hlinks;
4850 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4851 G.postRedisplay "toggle highlightlinks";
4853 | Ascii 'F' ->
4854 if conf.angle mod 360 = 0
4855 then (
4856 state.glinks <- true;
4857 let mode = state.mode in
4858 state.mode <-
4859 Textentry (
4860 (":", E.s, None, linknentry, linknact gotounder, false),
4861 (fun _ ->
4862 state.glinks <- false;
4863 state.mode <- mode)
4865 state.text <- E.s;
4866 G.postRedisplay "view:linkent(F)"
4868 else impmsg "hint mode does not work under rotation"
4870 | Ascii 'y' ->
4871 state.glinks <- true;
4872 let mode = state.mode in
4873 state.mode <-
4874 Textentry (
4876 ":", E.s, None, linknentry, linknact (fun under ->
4877 selstring (undertext under);
4878 ), false
4880 fun _ ->
4881 state.glinks <- false;
4882 state.mode <- mode
4884 state.text <- E.s;
4885 G.postRedisplay "view:linkent"
4887 | Ascii 'a' ->
4888 begin match state.autoscroll with
4889 | Some step ->
4890 conf.autoscrollstep <- step;
4891 state.autoscroll <- None
4892 | None ->
4893 if conf.autoscrollstep = 0
4894 then state.autoscroll <- Some 1
4895 else state.autoscroll <- Some conf.autoscrollstep
4898 | Ascii 'p' when ctrl ->
4899 launchpath () (* XXX where do error messages go? *)
4901 | Ascii 'P' ->
4902 setpresentationmode (not conf.presentation);
4903 showtext ' ' ("presentation mode " ^
4904 if conf.presentation then "on" else "off");
4906 | Ascii 'f' ->
4907 if List.mem Wsi.Fullscreen state.winstate
4908 then Wsi.reshape conf.cwinw conf.cwinh
4909 else Wsi.fullscreen ()
4911 | Ascii ('p'|'N') ->
4912 search state.searchpattern false
4914 | Ascii 'n' | Fn 3 ->
4915 search state.searchpattern true
4917 | Ascii 't' ->
4918 begin match state.layout with
4919 | [] -> ()
4920 | l :: _ ->
4921 gotoghyll (getpagey l.pageno)
4924 | Ascii ' ' ->
4925 nextpage ()
4927 | Delete | KPdelete ->
4928 prevpage ()
4930 | Ascii '=' ->
4931 showtext ' ' (describe_location ());
4933 | Ascii 'w' ->
4934 begin match state.layout with
4935 | [] -> ()
4936 | l :: _ ->
4937 Wsi.reshape l.pagew l.pageh;
4938 G.postRedisplay "w"
4941 | Ascii '\'' ->
4942 enterbookmarkmode ()
4944 | Ascii 'h' | Fn 1 ->
4945 enterhelpmode ()
4947 | Ascii 'i' ->
4948 enterinfomode ()
4950 | Ascii 'e' when Buffer.length state.errmsgs > 0 ->
4951 entermsgsmode ()
4953 | Ascii 'm' ->
4954 let ondone s =
4955 match state.layout with
4956 | l :: _ ->
4957 if nonemptystr s
4958 then
4959 state.bookmarks <-
4960 (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4961 | _ -> ()
4963 enttext ("bookmark: ", E.s, None, textentry, ondone, true)
4965 | Ascii '~' ->
4966 quickbookmark ();
4967 showtext ' ' "Quick bookmark added";
4969 | Ascii 'z' ->
4970 begin match state.layout with
4971 | l :: _ ->
4972 let rect = getpdimrect l.pagedimno in
4973 let w, h =
4974 if conf.crophack
4975 then
4976 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4977 truncate (1.2 *. (rect.(3) -. rect.(0))))
4978 else
4979 (truncate (rect.(1) -. rect.(0)),
4980 truncate (rect.(3) -. rect.(0)))
4982 let w = truncate ((float w)*.conf.zoom)
4983 and h = truncate ((float h)*.conf.zoom) in
4984 if w != 0 && h != 0
4985 then (
4986 state.anchor <- getanchor ();
4987 Wsi.reshape w (h + conf.interpagespace)
4989 G.postRedisplay "z";
4991 | [] -> ()
4994 | Ascii 'x' -> state.roam ()
4996 | Ascii ('<'|'>' as c) ->
4997 reqlayout
4998 (conf.angle + (if c = '>' then 30 else -30)) conf.fitmodel
5000 | Ascii ('['|']' as c) ->
5001 conf.colorscale <-
5002 bound (conf.colorscale +. (if c = '>' then 0.1 else -0.1)) 0.0 1.0;
5003 G.postRedisplay "brightness";
5005 | Ascii 'c' when state.mode = View ->
5006 if Wsi.withalt mask
5007 then (
5008 if conf.zoom > 1.0
5009 then
5010 let m = (state.winw - state.w) / 2 in
5011 gotoxy_and_clear_text m state.y
5013 else
5014 let (c, a, b), z =
5015 match state.prevcolumns with
5016 | None -> (1, 0, 0), 1.0
5017 | Some (columns, z) ->
5018 let cab =
5019 match columns with
5020 | Csplit (c, _) -> -c, 0, 0
5021 | Cmulti ((c, a, b), _) -> c, a, b
5022 | Csingle _ -> 1, 0, 0
5024 cab, z
5026 setcolumns View c a b;
5027 setzoom z
5029 | Down | Up when ctrl && Wsi.withshift mask ->
5030 let zoom, x = state.prevzoom in
5031 setzoom zoom;
5032 state.x <- x;
5034 | Ascii 'k' | Up | KPup ->
5035 begin match state.autoscroll with
5036 | None ->
5037 begin match state.mode with
5038 | Birdseye beye -> upbirdseye 1 beye
5039 | Textentry _ | View | LinkNav _ ->
5040 if ctrl
5041 then gotoxy_and_clear_text state.x (clamp ~-(state.winh/2))
5042 else (
5043 if not (Wsi.withshift mask) && conf.presentation
5044 then prevpage ()
5045 else gotoghyll1 true (clamp (-conf.scrollstep))
5048 | Some n ->
5049 setautoscrollspeed n false
5052 | Ascii 'j' | Down | KPdown ->
5053 begin match state.autoscroll with
5054 | None ->
5055 begin match state.mode with
5056 | Birdseye beye -> downbirdseye 1 beye
5057 | Textentry _ | View | LinkNav _ ->
5058 if ctrl
5059 then gotoxy_and_clear_text state.x (clamp (state.winh/2))
5060 else (
5061 if not (Wsi.withshift mask) && conf.presentation
5062 then nextpage ()
5063 else gotoghyll1 true (clamp (conf.scrollstep))
5066 | Some n ->
5067 setautoscrollspeed n true
5070 | Left | Right | KPleft | KPright when not (Wsi.withalt mask) ->
5071 if canpan ()
5072 then
5073 let dx =
5074 if ctrl
5075 then state.winw / 2
5076 else conf.hscrollstep
5078 let dx =
5079 let pv = Wsi.kc2kt key in
5080 if pv = Keys.Left || pv = Keys.KPleft then dx else -dx
5082 gotoxy_and_clear_text (panbound (state.x + dx)) state.y
5083 else (
5084 state.text <- E.s;
5085 G.postRedisplay "left/right"
5088 | Prior | KPprior ->
5089 let y =
5090 if ctrl
5091 then
5092 match state.layout with
5093 | [] -> state.y
5094 | l :: _ -> state.y - l.pagey
5095 else
5096 clamp (pgscale (-state.winh))
5098 gotoghyll y
5100 | Next | KPnext ->
5101 let y =
5102 if ctrl
5103 then
5104 match List.rev state.layout with
5105 | [] -> state.y
5106 | l :: _ -> getpagey l.pageno
5107 else
5108 clamp (pgscale state.winh)
5110 gotoghyll y
5112 | Ascii 'g' | Home | KPhome ->
5113 addnav ();
5114 gotoghyll 0
5115 | Ascii 'G' | End | KPend ->
5116 addnav ();
5117 gotoghyll (clamp state.maxy)
5119 | Right | KPright when Wsi.withalt mask ->
5120 gotoghyll (getnav 1)
5121 | Left | KPleft when Wsi.withalt mask ->
5122 gotoghyll (getnav ~-1)
5124 | Ascii 'r' ->
5125 reload ()
5127 | Ascii 'v' when conf.debug ->
5128 state.rects <- [];
5129 List.iter (fun l ->
5130 match getopaque l.pageno with
5131 | None -> ()
5132 | Some opaque ->
5133 let x0, y0, x1, y1 = pagebbox opaque in
5134 let rect = (float x0, float y0,
5135 float x1, float y0,
5136 float x1, float y1,
5137 float x0, float y1) in
5138 debugrect rect;
5139 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
5140 state.rects <- (l.pageno, color, rect) :: state.rects;
5141 ) state.layout;
5142 G.postRedisplay "v";
5144 | Ascii '|' ->
5145 let mode = state.mode in
5146 let cmd = ref E.s in
5147 let onleave = function
5148 | Cancel -> state.mode <- mode
5149 | Confirm ->
5150 List.iter (fun l ->
5151 match getopaque l.pageno with
5152 | Some opaque -> pipesel opaque !cmd
5153 | None -> ()) state.layout;
5154 state.mode <- mode
5156 let ondone s =
5157 cbput state.hists.sel s;
5158 cmd := s
5160 let te =
5161 "| ", !cmd, Some (onhist state.hists.sel), textentry, ondone, true
5163 G.postRedisplay "|";
5164 state.mode <- Textentry (te, onleave);
5166 | (Ascii _|Fn _|Enter|KPenter|KPleft|KPright|Left|Right|Code _) ->
5167 vlog "huh? %s" (Wsi.keyname key)
5170 let linknavkeyboard key mask linknav =
5171 let pv = Wsi.kc2kt key in
5172 let getpage pageno =
5173 let rec loop = function
5174 | [] -> None
5175 | l :: _ when l.pageno = pageno -> Some l
5176 | _ :: rest -> loop rest
5177 in loop state.layout
5179 let doexact (pageno, n) =
5180 match getopaque pageno, getpage pageno with
5181 | Some opaque, Some l ->
5182 if pv = Keys.Enter || pv = Keys.KPenter
5183 then
5184 let under = getlink opaque n in
5185 G.postRedisplay "link gotounder";
5186 gotounder under;
5187 state.mode <- View;
5188 else
5189 let opt, dir =
5190 let open Keys in
5191 match pv with
5192 | Home ->
5193 Some (findlink opaque LDfirst), -1
5195 | End ->
5196 Some (findlink opaque LDlast), 1
5198 | Left ->
5199 Some (findlink opaque (LDleft n)), -1
5201 | Right ->
5202 Some (findlink opaque (LDright n)), 1
5204 | Up ->
5205 Some (findlink opaque (LDup n)), -1
5207 | Down ->
5208 Some (findlink opaque (LDdown n)), 1
5210 | Delete|Escape|Insert|Enter|KPdelete|KPdown|KPend|KPenter
5211 | KPhome|KPleft|KPminus|KPnext|KPplus|KPprior|KPright|KPup
5212 | Next|Prior|Ascii _|Code _|Fn _|Backspace -> None, 0
5214 let pwl l dir =
5215 begin match findpwl l.pageno dir with
5216 | Pwlnotfound -> ()
5217 | Pwl pageno ->
5218 let notfound dir =
5219 state.mode <- LinkNav (Ltgendir dir);
5220 let y, h = getpageyh pageno in
5221 let y =
5222 if dir < 0
5223 then y + h - state.winh
5224 else y
5226 gotoxy state.x y
5228 begin match getopaque pageno, getpage pageno with
5229 | Some opaque, Some _ ->
5230 let link =
5231 let ld = if dir > 0 then LDfirst else LDlast in
5232 findlink opaque ld
5234 begin match link with
5235 | Lfound m ->
5236 showlinktype (getlink opaque m);
5237 state.mode <- LinkNav (Ltexact (pageno, m));
5238 G.postRedisplay "linknav jpage";
5239 | Lnotfound -> notfound dir
5240 end;
5241 | _ -> notfound dir
5242 end;
5243 end;
5245 begin match opt with
5246 | Some Lnotfound -> pwl l dir;
5247 | Some (Lfound m) ->
5248 if m = n
5249 then pwl l dir
5250 else (
5251 let _, y0, _, y1 = getlinkrect opaque m in
5252 if y0 < l.pagey
5253 then gotopage1 l.pageno y0
5254 else (
5255 let d = fstate.fontsize + 1 in
5256 if y1 - l.pagey > l.pagevh - d
5257 then gotopage1 l.pageno (y1 - state.winh + d)
5258 else G.postRedisplay "linknav";
5260 showlinktype (getlink opaque m);
5261 state.mode <- LinkNav (Ltexact (l.pageno, m));
5264 | None -> viewkeyboard key mask
5265 end;
5266 | _ -> viewkeyboard key mask
5268 if pv = Keys.Insert
5269 then (
5270 begin match linknav with
5271 | Ltexact pa -> state.lnava <- Some pa
5272 | Ltgendir _ | Ltnotready _ -> ()
5273 end;
5274 state.mode <- View;
5275 G.postRedisplay "leave linknav"
5277 else
5278 match linknav with
5279 | Ltgendir _ | Ltnotready _ -> viewkeyboard key mask
5280 | Ltexact exact -> doexact exact
5283 let keyboard key mask =
5284 if (key = Char.code 'g' && Wsi.withctrl mask) && not (istextentry state.mode)
5285 then wcmd "interrupt"
5286 else state.uioh <- state.uioh#key key mask
5289 let birdseyekeyboard key mask
5290 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5291 let incr =
5292 match conf.columns with
5293 | Csingle _ -> 1
5294 | Cmulti ((c, _, _), _) -> c
5295 | Csplit _ -> failwith "bird's eye split mode"
5297 let pgh layout = List.fold_left
5298 (fun m l -> max l.pageh m) state.winh layout in
5299 let open Keys in
5300 match Wsi.kc2kt key with
5301 | Ascii 'l' when Wsi.withctrl mask ->
5302 let y, h = getpageyh pageno in
5303 let top = (state.winh - h) / 2 in
5304 gotoxy state.x (max 0 (y - top))
5305 | Enter | KPenter -> leavebirdseye beye false
5306 | Escape -> leavebirdseye beye true
5307 | Up -> upbirdseye incr beye
5308 | Down -> downbirdseye incr beye
5309 | Left -> upbirdseye 1 beye
5310 | Right -> downbirdseye 1 beye
5312 | Prior ->
5313 begin match state.layout with
5314 | l :: _ ->
5315 if l.pagey != 0
5316 then (
5317 state.mode <- Birdseye (
5318 oconf, leftx, l.pageno, hooverpageno, anchor
5320 gotopage1 l.pageno 0;
5322 else (
5323 let layout = layout state.x (state.y-state.winh)
5324 state.winw
5325 (pgh state.layout) in
5326 match layout with
5327 | [] -> gotoxy state.x (clamp (-state.winh))
5328 | l :: _ ->
5329 state.mode <- Birdseye (
5330 oconf, leftx, l.pageno, hooverpageno, anchor
5332 gotopage1 l.pageno 0
5335 | [] -> gotoxy state.x (clamp (-state.winh))
5336 end;
5338 | Next ->
5339 begin match List.rev state.layout with
5340 | l :: _ ->
5341 let layout = layout state.x
5342 (state.y + (pgh state.layout))
5343 state.winw state.winh in
5344 begin match layout with
5345 | [] ->
5346 let incr = l.pageh - l.pagevh in
5347 if incr = 0
5348 then (
5349 state.mode <-
5350 Birdseye (
5351 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5353 G.postRedisplay "birdseye pagedown";
5355 else gotoxy state.x (clamp (incr + conf.interpagespace*2));
5357 | l :: _ ->
5358 state.mode <-
5359 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5360 gotopage1 l.pageno 0;
5363 | [] -> gotoxy state.x (clamp state.winh)
5364 end;
5366 | Home ->
5367 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5368 gotopage1 0 0
5370 | End ->
5371 let pageno = state.pagecount - 1 in
5372 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5373 if not (pagevisible state.layout pageno)
5374 then
5375 let h =
5376 match List.rev state.pdims with
5377 | [] -> state.winh
5378 | (_, _, h, _) :: _ -> h
5380 gotoxy
5381 state.x
5382 (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5383 else G.postRedisplay "birdseye end";
5385 | Delete|Insert|KPdelete|KPdown|KPend|KPhome|KPleft
5386 | KPminus|KPnext|KPplus|KPprior|KPright|KPup|Ascii _
5387 | Code _|Fn _|Backspace -> viewkeyboard key mask
5390 let drawpage l =
5391 let color =
5392 match state.mode with
5393 | Textentry _ -> scalecolor 0.4
5394 | LinkNav _ | View -> scalecolor 1.0
5395 | Birdseye (_, _, pageno, hooverpageno, _) ->
5396 if l.pageno = hooverpageno
5397 then scalecolor 0.9
5398 else (
5399 if l.pageno = pageno
5400 then (
5401 let c = scalecolor 1.0 in
5402 GlDraw.color c;
5403 GlDraw.line_width 3.0;
5404 let dispx = l.pagedispx in
5405 linerect
5406 (float (dispx-1)) (float (l.pagedispy-1))
5407 (float (dispx+l.pagevw+1))
5408 (float (l.pagedispy+l.pagevh+1))
5410 GlDraw.line_width 1.0;
5413 else scalecolor 0.8
5416 drawtiles l color;
5419 let postdrawpage l linkindexbase =
5420 match getopaque l.pageno with
5421 | Some opaque ->
5422 if tileready l l.pagex l.pagey
5423 then
5424 let x = l.pagedispx - l.pagex
5425 and y = l.pagedispy - l.pagey in
5426 let hlmask =
5427 match conf.columns with
5428 | Csingle _ | Cmulti _ ->
5429 (if conf.hlinks then 1 else 0)
5430 + (if state.glinks
5431 && not (isbirdseye state.mode) then 2 else 0)
5432 | Csplit _ -> 0
5434 let s =
5435 match state.mode with
5436 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5437 | Textentry _
5438 | Birdseye _
5439 | View
5440 | LinkNav _ -> E.s
5442 Hashtbl.find_all state.prects l.pageno |>
5443 List.iter (fun vals -> drawprect opaque x y vals);
5444 let n = postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize) in
5445 if n < 0
5446 then (state.redisplay <- true; 0)
5447 else n
5448 else 0
5449 | _ -> 0
5452 let scrollindicator () =
5453 let sbw, ph, sh = state.uioh#scrollph in
5454 let sbh, pw, sw = state.uioh#scrollpw in
5456 let x0,x1,hx0 =
5457 if conf.leftscroll
5458 then (0, sbw, sbw)
5459 else ((state.winw - sbw), state.winw, 0)
5462 Gl.enable `blend;
5463 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5464 GlDraw.color (0.64, 0.64, 0.64) ~alpha:0.7;
5465 filledrect (float x0) 0. (float x1) (float state.winh);
5466 filledrect
5467 (float hx0) (float (state.winh - sbh))
5468 (float (hx0 + state.winw)) (float state.winh)
5470 GlDraw.color (0.0, 0.0, 0.0) ~alpha:0.7;
5472 filledrect (float x0) ph (float x1) (ph +. sh);
5473 let pw = pw +. float hx0 in
5474 filledrect pw (float (state.winh - sbh)) (pw +. sw) (float state.winh);
5475 Gl.disable `blend;
5478 let showsel () =
5479 match state.mstate with
5480 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5483 | Msel ((x0, y0), (x1, y1)) ->
5484 let identify opaque l px py = Some (opaque, l.pageno, px, py) in
5485 let o0,n0,px0,py0 = onppundermouse identify x0 y0 (~< E.s, -1, 0, 0) in
5486 let _o1,n1,px1,py1 = onppundermouse identify x1 y1 (~< E.s, -1, 0, 0) in
5487 if n0 != -1 && n0 = n1 then seltext o0 (px0, py0, px1, py1);
5490 let showrects =
5491 function [] -> ()
5492 | rects ->
5493 Gl.enable `blend;
5494 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5495 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5496 List.iter
5497 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5498 List.iter (fun l ->
5499 if l.pageno = pageno
5500 then (
5501 let dx = float (l.pagedispx - l.pagex) in
5502 let dy = float (l.pagedispy - l.pagey) in
5503 let r, g, b, alpha = c in
5504 GlDraw.color (r, g, b) ~alpha;
5505 filledrect2 (x0+.dx) (y0+.dy)
5506 (x1+.dx) (y1+.dy)
5507 (x3+.dx) (y3+.dy)
5508 (x2+.dx) (y2+.dy);
5510 ) state.layout
5511 ) rects
5513 Gl.disable `blend;
5516 let display () =
5517 begin match conf.columns, state.layout with
5518 | Csingle _, _ :: _ ->
5519 GlDraw.color (scalecolor2 conf.bgcolor);
5520 let y =
5521 List.fold_left (fun y l ->
5522 let x0 = 0 in
5523 let y0 = y in
5524 let x1 = l.pagedispx in
5525 let y1 = (l.pagedispy + l.pagevh) in
5526 filledrect (float x0) (float y0) (float x1) (float y1);
5527 let x0 = x1 + l.pagevw in
5528 let x1 = state.winw in
5529 filledrect1 (float x0) (float y0) (float x1) (float y1);
5530 if y != l.pagedispy
5531 then (
5532 let x0 = 0
5533 and x1 = state.winw in
5534 let y0 = y
5535 and y1 = l.pagedispy in
5536 filledrect1 (float x0) (float y0) (float x1) (float y1);
5538 l.pagedispy + l.pagevh) 0 state.layout
5540 let x0 = 0
5541 and x1 = state.winw in
5542 let y0 = y
5543 and y1 = state.winh in
5544 filledrect1 (float x0) (float y0) (float x1) (float y1)
5545 | (Cmulti _ | Csplit _), _ | Csingle _, [] ->
5546 GlClear.color (scalecolor2 conf.bgcolor);
5547 GlClear.clear [`color];
5548 end;
5549 List.iter drawpage state.layout;
5550 let rects =
5551 match state.mode with
5552 | LinkNav (Ltexact (pageno, linkno)) ->
5553 begin match getopaque pageno with
5554 | Some opaque ->
5555 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5556 let color = (0.0, 0.0, 0.5, 0.5) in
5557 (pageno, color, (
5558 float x0, float y0,
5559 float x1, float y0,
5560 float x1, float y1,
5561 float x0, float y1)
5562 ) :: state.rects
5563 | None -> state.rects
5565 | LinkNav (Ltgendir _) | LinkNav (Ltnotready _)
5566 | Birdseye _
5567 | Textentry _
5568 | View -> state.rects
5570 showrects rects;
5571 let rec postloop linkindexbase = function
5572 | l :: rest ->
5573 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5574 postloop linkindexbase rest
5575 | [] -> ()
5577 showsel ();
5578 postloop 0 state.layout;
5579 state.uioh#display;
5580 begin match state.mstate with
5581 | Mzoomrect ((x0, y0), (x1, y1)) ->
5582 Gl.enable `blend;
5583 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5584 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5585 filledrect (float x0) (float y0) (float x1) (float y1);
5586 Gl.disable `blend;
5587 | Msel _
5588 | Mpan _
5589 | Mscrolly | Mscrollx
5590 | Mzoom _
5591 | Mnone -> ()
5592 end;
5593 enttext ();
5594 scrollindicator ();
5595 Wsi.swapb ();
5598 let zoomrect x y x1 y1 =
5599 let x0 = min x x1
5600 and x1 = max x x1
5601 and y0 = min y y1 in
5602 let zoom = (float state.w) /. float (x1 - x0) in
5603 let margin =
5604 let simple () =
5605 if state.w < state.winw
5606 then (state.winw - state.w) / 2
5607 else 0
5609 match conf.fitmodel with
5610 | FitWidth | FitProportional -> simple ()
5611 | FitPage ->
5612 match conf.columns with
5613 | Csplit _ ->
5614 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
5615 | Cmulti _ | Csingle _ -> simple ()
5617 gotoxy ((state.x + margin) - x0) (state.y + y0);
5618 state.anchor <- getanchor ();
5619 setzoom zoom;
5620 resetmstate ();
5623 let annot inline x y =
5624 match unproject x y with
5625 | Some (opaque, n, ux, uy) ->
5626 let add text =
5627 addannot opaque ux uy text;
5628 wcmd "freepage %s" (~> opaque);
5629 Hashtbl.remove state.pagemap (n, state.gen);
5630 flushtiles ();
5631 gotoxy state.x state.y
5633 if inline
5634 then
5635 let ondone s = add s in
5636 let mode = state.mode in
5637 state.mode <- Textentry (
5638 ("annotation: ", E.s, None, textentry, ondone, true),
5639 fun _ -> state.mode <- mode);
5640 state.text <- E.s;
5641 enttext ();
5642 G.postRedisplay "annot"
5643 else
5644 add @@ getusertext E.s
5645 | _ -> ()
5648 let zoomblock x y =
5649 let g opaque l px py =
5650 match rectofblock opaque px py with
5651 | Some a ->
5652 let x0 = a.(0) -. 20. in
5653 let x1 = a.(1) +. 20. in
5654 let y0 = a.(2) -. 20. in
5655 let zoom = (float state.w) /. (x1 -. x0) in
5656 let pagey = getpagey l.pageno in
5657 let margin = (state.w - l.pagew)/2 in
5658 let nx = -truncate x0 - margin in
5659 gotoxy_and_clear_text nx (pagey + truncate y0);
5660 state.anchor <- getanchor ();
5661 setzoom zoom;
5662 None
5663 | None -> None
5665 match conf.columns with
5666 | Csplit _ ->
5667 impmsg "block zooming does not work properly in split columns mode"
5668 | Cmulti _ | Csingle _ -> onppundermouse g x y ()
5671 let scrollx x =
5672 let winw = state.winw - 1 in
5673 let s = float x /. float winw in
5674 let destx = truncate (float (state.w + winw) *. s) in
5675 gotoxy_and_clear_text (winw - destx) state.y;
5676 state.mstate <- Mscrollx;
5679 let scrolly y =
5680 let s = float y /. float state.winh in
5681 let desty = truncate (float (state.maxy -
5682 (if conf.maxhfit then state.winh else 0))
5683 *. s) in
5684 gotoxy_and_clear_text state.x desty;
5685 state.mstate <- Mscrolly;
5688 let viewmulticlick clicks x y mask =
5689 let g opaque l px py =
5690 let mark =
5691 match clicks with
5692 | 2 -> Mark_word
5693 | 3 -> Mark_line
5694 | 4 -> Mark_block
5695 | _ -> Mark_page
5697 if markunder opaque px py mark
5698 then (
5699 Some (fun () ->
5700 let dopipe cmd =
5701 match getopaque l.pageno with
5702 | None -> ()
5703 | Some opaque -> pipesel opaque cmd
5705 state.roam <- (fun () -> dopipe conf.paxcmd);
5706 if not (Wsi.withctrl mask) then dopipe conf.selcmd;
5709 else None
5711 G.postRedisplay "viewmulticlick";
5712 onppundermouse g x y (fun () -> impmsg "nothing to select") ();
5715 let canselect () =
5716 match conf.columns with
5717 | Csplit _ -> false
5718 | Csingle _ | Cmulti _ -> conf.angle mod 360 = 0
5721 let viewmouse button down x y mask =
5722 match button with
5723 | n when (n == 4 || n == 5) && not down ->
5724 if Wsi.withctrl mask
5725 then (
5726 match state.mstate with
5727 | Mzoom (oldn, i, (ftx, fty)) ->
5728 let recenter =
5729 if false
5730 then abs (ftx - x) > 5 || abs (fty - y) > 5
5731 else false
5733 if oldn = n
5734 then (
5735 if i = 2
5736 then
5737 let incr =
5738 match n with
5739 | 5 ->
5740 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5741 | _ ->
5742 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5744 let zoom = conf.zoom -. incr in
5745 if recenter
5746 then pivotzoom ~x ~y zoom
5747 else pivotzoom zoom;
5748 state.mstate <- Mzoom (n, 0, (x, y));
5749 else
5750 state.mstate <- Mzoom (n, i+1, (ftx, fty));
5752 else state.mstate <- Mzoom (n, 0, (ftx, fty))
5754 | Msel _
5755 | Mpan _
5756 | Mscrolly | Mscrollx
5757 | Mzoomrect _
5758 | Mnone -> state.mstate <- Mzoom (n, 0, (0, 0))
5760 else (
5761 match state.autoscroll with
5762 | Some step -> setautoscrollspeed step (n=4)
5763 | None ->
5764 if conf.wheelbypage || conf.presentation
5765 then (
5766 if n = 4
5767 then prevpage ()
5768 else nextpage ()
5770 else
5771 let incr =
5772 if n = 4
5773 then -conf.scrollstep
5774 else conf.scrollstep
5776 let incr = incr * 2 in
5777 let y = clamp incr in
5778 gotoxy_and_clear_text state.x y
5781 | n when (n = 6 || n = 7) && not down && canpan () ->
5782 let x =
5783 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep) in
5784 gotoxy_and_clear_text x state.y
5786 | 1 when Wsi.withshift mask ->
5787 state.mstate <- Mnone;
5788 if not down
5789 then (
5790 match unproject x y with
5791 | None -> ()
5792 | Some (_, pageno, ux, uy) ->
5793 let cmd = Printf.sprintf
5794 "%s %s %d %d %d"
5795 conf.stcmd state.path pageno ux uy
5797 match spawn cmd [] with
5798 | exception exn ->
5799 impmsg "execution of synctex command(%S) failed: %S"
5800 conf.stcmd @@ exntos exn
5801 | _pid -> ()
5804 | 1 when Wsi.withctrl mask ->
5805 if down
5806 then (
5807 Wsi.setcursor Wsi.CURSOR_FLEUR;
5808 state.mstate <- Mpan (x, y)
5810 else
5811 state.mstate <- Mnone
5813 | 3 ->
5814 if down
5815 then (
5816 if Wsi.withshift mask
5817 then (
5818 annot conf.annotinline x y;
5819 G.postRedisplay "addannot"
5821 else
5822 let p = (x, y) in
5823 Wsi.setcursor Wsi.CURSOR_CYCLE;
5824 state.mstate <- Mzoomrect (p, p)
5826 else (
5827 match state.mstate with
5828 | Mzoomrect ((x0, y0), _) ->
5829 if abs (x-x0) > 10 && abs (y - y0) > 10
5830 then zoomrect x0 y0 x y
5831 else (
5832 resetmstate ();
5833 G.postRedisplay "kill accidental zoom rect";
5835 | Msel _
5836 | Mpan _
5837 | Mscrolly | Mscrollx
5838 | Mzoom _
5839 | Mnone -> resetmstate ()
5842 | 1 when vscrollhit x ->
5843 if down
5844 then
5845 let _, position, sh = state.uioh#scrollph in
5846 if y > truncate position && y < truncate (position +. sh)
5847 then state.mstate <- Mscrolly
5848 else scrolly y
5849 else
5850 state.mstate <- Mnone
5852 | 1 when y > state.winh - hscrollh () ->
5853 if down
5854 then
5855 let _, position, sw = state.uioh#scrollpw in
5856 if x > truncate position && x < truncate (position +. sw)
5857 then state.mstate <- Mscrollx
5858 else scrollx x
5859 else
5860 state.mstate <- Mnone
5862 | 1 when state.bzoom -> if not down then zoomblock x y
5864 | 1 ->
5865 let dest = if down then getunder x y else Unone in
5866 begin match dest with
5867 | Ulinkuri _ ->
5868 gotounder dest
5870 | Unone when down ->
5871 Wsi.setcursor Wsi.CURSOR_FLEUR;
5872 state.mstate <- Mpan (x, y);
5874 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
5876 | Unone | Utext _ ->
5877 if down
5878 then (
5879 if canselect ()
5880 then (
5881 state.mstate <- Msel ((x, y), (x, y));
5882 G.postRedisplay "mouse select";
5885 else (
5886 match state.mstate with
5887 | Mnone -> ()
5889 | Mzoom _ | Mscrollx | Mscrolly ->
5890 state.mstate <- Mnone
5892 | Mzoomrect ((x0, y0), _) ->
5893 zoomrect x0 y0 x y
5895 | Mpan _ ->
5896 Wsi.setcursor Wsi.CURSOR_INHERIT;
5897 state.mstate <- Mnone
5899 | Msel ((x0, y0), (x1, y1)) ->
5900 let rec loop = function
5901 | [] -> ()
5902 | l :: rest ->
5903 let inside =
5904 let a0 = l.pagedispy in
5905 let a1 = a0 + l.pagevh in
5906 let b0 = l.pagedispx in
5907 let b1 = b0 + l.pagevw in
5908 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5909 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5911 if inside
5912 then
5913 match getopaque l.pageno with
5914 | Some opaque ->
5915 let dosel cmd () =
5916 match Unix.pipe () with
5917 | exception exn ->
5918 impmsg "cannot create sel pipe: %s" @@
5919 exntos exn;
5920 | (r, w) ->
5921 let clo what fd =
5922 Ne.clo fd (fun msg ->
5923 dolog "%s close failed: %s" what msg)
5925 begin match spawn cmd [r, 0; w, -1] with
5926 | exception exn ->
5927 clo "Msel pipe/w" w;
5928 dolog "cannot execute %S: %s" cmd @@ exntos exn
5929 | _pid ->
5930 copysel w opaque;
5931 G.postRedisplay "copysel";
5932 end;
5933 clo "Msel pipe/r" r;
5935 dosel conf.selcmd ();
5936 state.roam <- dosel conf.paxcmd;
5937 | None -> ()
5938 else loop rest
5940 loop state.layout;
5941 resetmstate ();
5945 | _ -> ()
5948 let birdseyemouse button down x y mask
5949 (conf, leftx, _, hooverpageno, anchor) =
5950 match button with
5951 | 1 when down ->
5952 let rec loop = function
5953 | [] -> ()
5954 | l :: rest ->
5955 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5956 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5957 then (
5958 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5960 else loop rest
5962 loop state.layout
5963 | 3 -> ()
5964 | _ -> viewmouse button down x y mask
5967 let uioh = object
5968 method display = ()
5970 method key key mask =
5971 begin match state.mode with
5972 | Textentry textentry -> textentrykeyboard key mask textentry
5973 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5974 | View -> viewkeyboard key mask
5975 | LinkNav linknav -> linknavkeyboard key mask linknav
5976 end;
5977 state.uioh
5979 method button button bstate x y mask =
5980 begin match state.mode with
5981 | LinkNav _ | View -> viewmouse button bstate x y mask
5982 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5983 | Textentry _ -> ()
5984 end;
5985 state.uioh
5987 method multiclick clicks x y mask =
5988 begin match state.mode with
5989 | LinkNav _ | View -> viewmulticlick clicks x y mask
5990 | Birdseye _ | Textentry _ -> ()
5991 end;
5992 state.uioh
5994 method motion x y =
5995 begin match state.mode with
5996 | Textentry _ -> ()
5997 | View | Birdseye _ | LinkNav _ ->
5998 match state.mstate with
5999 | Mzoom _ | Mnone -> ()
6001 | Mpan (x0, y0) ->
6002 let dx = x - x0
6003 and dy = y0 - y in
6004 state.mstate <- Mpan (x, y);
6005 let x = if canpan () then panbound (state.x + dx) else state.x in
6006 let y = clamp dy in
6007 gotoxy_and_clear_text x y
6009 | Msel (a, _) ->
6010 state.mstate <- Msel (a, (x, y));
6011 G.postRedisplay "motion select";
6013 | Mscrolly ->
6014 let y = min state.winh (max 0 y) in
6015 scrolly y
6017 | Mscrollx ->
6018 let x = min state.winw (max 0 x) in
6019 scrollx x
6021 | Mzoomrect (p0, _) ->
6022 state.mstate <- Mzoomrect (p0, (x, y));
6023 G.postRedisplay "motion zoomrect";
6024 end;
6025 state.uioh
6027 method pmotion x y =
6028 begin match state.mode with
6029 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6030 let rec loop = function
6031 | [] ->
6032 if hooverpageno != -1
6033 then (
6034 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6035 G.postRedisplay "pmotion birdseye no hoover";
6037 | l :: rest ->
6038 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6039 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6040 then (
6041 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6042 G.postRedisplay "pmotion birdseye hoover";
6044 else loop rest
6046 loop state.layout
6048 | Textentry _ -> ()
6050 | LinkNav _ | View ->
6051 match state.mstate with
6052 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ -> ()
6053 | Mnone ->
6054 updateunder x y;
6055 if canselect ()
6056 then
6057 match conf.pax with
6058 | None -> ()
6059 | Some r ->
6060 let past, _, _ = !r in
6061 let now = now () in
6062 let delta = now -. past in
6063 if delta > 0.01
6064 then paxunder x y
6065 else r := (now, x, y)
6066 end;
6067 state.uioh
6069 method infochanged _ = ()
6071 method scrollph =
6072 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6073 let p, h =
6074 if maxy = 0
6075 then 0.0, float state.winh
6076 else scrollph state.y maxy
6078 vscrollw (), p, h
6080 method scrollpw =
6081 let fwinw = float (state.winw - vscrollw ()) in
6082 let sw =
6083 let sw = fwinw /. float state.w in
6084 let sw = fwinw *. sw in
6085 max sw (float conf.scrollh)
6087 let position =
6088 let maxx = state.w + state.winw in
6089 let x = state.winw - state.x in
6090 let percent = float x /. float maxx in
6091 (fwinw -. sw) *. percent
6093 hscrollh (), position, sw
6095 method modehash =
6096 let modename =
6097 match state.mode with
6098 | LinkNav _ -> "links"
6099 | Textentry _ -> "textentry"
6100 | Birdseye _ -> "birdseye"
6101 | View -> "view"
6103 findkeyhash conf modename
6105 method eformsgs = true
6106 method alwaysscrolly = false
6107 end;;
6109 let addrect pageno r g b a x0 y0 x1 y1 =
6110 Hashtbl.add state.prects pageno [|r; g; b; a; x0; y0; x1; y1|];
6113 let ract cmds =
6114 let cl = splitatchar cmds ' ' in
6115 let scan s fmt f =
6116 try Scanf.sscanf s fmt f
6117 with exn -> adderrfmt "remote exec" "error processing '%S': %s\n"
6118 cmds @@ exntos exn
6120 let rectx s pageno (r, g, b, a) x0 y0 x1 y1 =
6121 vlog "%s page %d color (%f %f %f %f) x0,y0,x1,y1 = %f %f %f %f"
6122 s pageno r g b a x0 y0 x1 y1;
6123 onpagerect
6124 pageno
6125 (fun w h ->
6126 let _,w1,h1,_ = getpagedim pageno in
6127 let sw = float w1 /. float w
6128 and sh = float h1 /. float h in
6129 let x0s = x0 *. sw
6130 and x1s = x1 *. sw
6131 and y0s = y0 *. sh
6132 and y1s = y1 *. sh in
6133 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
6134 let color = (r, g, b, a) in
6135 if conf.verbose then debugrect rect;
6136 state.rects <- (pageno, color, rect) :: state.rects;
6137 G.postRedisplay s;
6140 match cl with
6141 | "reload", "" -> reload ()
6142 | "goto", args ->
6143 scan args "%u %f %f"
6144 (fun pageno x y ->
6145 let cmd, _ = state.geomcmds in
6146 if emptystr cmd
6147 then gotopagexy !wtmode pageno x y
6148 else
6149 let f prevf () =
6150 gotopagexy !wtmode pageno x y;
6151 prevf ()
6153 state.reprf <- f state.reprf
6155 | "goto1", args -> scan args "%u %f" gotopage
6156 | "gotor", args ->
6157 scan args "%S %u"
6158 (fun _filename _pageno ->
6159 failwith "gotounder (Uremote (filename, pageno))")
6160 | "gotord", args ->
6161 scan args "%S %S"
6162 (fun _filename _dest ->
6163 failwith "gotounder (Uremotedest (filename, dest))")
6164 | "rect", args ->
6165 scan args "%u %u %f %f %f %f"
6166 (fun pageno c x0 y0 x1 y1 ->
6167 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
6168 rectx "rect" pageno color x0 y0 x1 y1;
6170 | "prect", args ->
6171 scan args "%u %f %f %f %f %f %f %f %f"
6172 (fun pageno r g b alpha x0 y0 x1 y1 ->
6173 addrect pageno r g b alpha x0 y0 x1 y1;
6174 G.postRedisplay "prect"
6176 | "pgoto", args ->
6177 scan args "%u %f %f"
6178 (fun pageno x y ->
6179 let optopaque =
6180 match getopaque pageno with
6181 | Some opaque -> opaque
6182 | None -> ~< E.s
6184 pgoto optopaque pageno x y;
6185 let rec fixx = function
6186 | [] -> ()
6187 | l :: rest ->
6188 if l.pageno = pageno
6189 then gotoxy (state.x - l.pagedispx) state.y
6190 else fixx rest
6192 let layout =
6193 let mult =
6194 match conf.columns with
6195 | Csingle _ | Csplit _ -> 1
6196 | Cmulti ((n, _, _), _) -> n
6198 layout 0 state.y (state.winw * mult) state.winh
6200 fixx layout
6202 | "activatewin", "" -> Wsi.activatewin ()
6203 | "quit", "" -> raise Quit
6204 | "keys", keys ->
6205 begin try
6206 let l = Config.keys_of_string keys in
6207 List.iter (fun (k, m) -> keyboard k m) l
6208 with exn -> adderrfmt "error processing keys" "`%S': %s\n"
6209 cmds @@ exntos exn
6211 | "clearrects", "" ->
6212 Hashtbl.clear state.prects;
6213 G.postRedisplay "clearrects"
6214 | _ ->
6215 adderrfmt "remote command"
6216 "error processing remote command: %S\n" cmds;
6219 let remote =
6220 let scratch = Bytes.create 80 in
6221 let buf = Buffer.create 80 in
6222 fun fd ->
6223 match tempfailureretry (Unix.read fd scratch 0) 80 with
6224 | exception Unix.Unix_error (Unix.EAGAIN, _, _) -> None
6225 | 0 ->
6226 Unix.close fd;
6227 if Buffer.length buf > 0
6228 then (
6229 let s = Buffer.contents buf in
6230 Buffer.clear buf;
6231 ract s;
6233 None
6234 | n ->
6235 let rec eat ppos =
6236 let nlpos =
6237 match Bytes.index_from scratch ppos '\n' with
6238 | pos -> if pos >= n then -1 else pos
6239 | exception Not_found -> -1
6241 if nlpos >= 0
6242 then (
6243 Buffer.add_subbytes buf scratch ppos (nlpos-ppos);
6244 let s = Buffer.contents buf in
6245 Buffer.clear buf;
6246 ract s;
6247 eat (nlpos+1);
6249 else (
6250 Buffer.add_subbytes buf scratch ppos (n-ppos);
6251 Some fd
6253 in eat 0
6256 let remoteopen path =
6257 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
6258 with exn ->
6259 adderrfmt "remoteopen" "error opening %S: %s" path @@ exntos exn;
6260 None
6263 let () =
6264 let gcconfig = ref E.s in
6265 let trimcachepath = ref E.s in
6266 let rcmdpath = ref E.s in
6267 let pageno = ref None in
6268 let rootwid = ref 0 in
6269 let openlast = ref false in
6270 let nofc = ref false in
6271 let doreap = ref false in
6272 let csspath = ref None in
6273 selfexec := Sys.executable_name;
6274 Arg.parse
6275 (Arg.align
6276 [("-p", Arg.String (fun s -> state.password <- s),
6277 "<password> Set password");
6279 ("-f", Arg.String
6280 (fun s ->
6281 Config.fontpath := s;
6282 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
6284 "<path> Set path to the user interface font");
6286 ("-c", Arg.String
6287 (fun s ->
6288 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
6289 Config.confpath := s),
6290 "<path> Set path to the configuration file");
6292 ("-last", Arg.Set openlast, " Open last document");
6294 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
6295 "<page-number> Jump to page");
6297 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6298 "<path> Set path to the trim cache file");
6300 ("-dest", Arg.String (fun s -> state.nameddest <- s),
6301 "<named-destination> Set named destination");
6303 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
6304 ("-cxack", Arg.Set cxack, " Cut corners");
6306 ("-remote", Arg.String (fun s -> rcmdpath := s),
6307 "<path> Set path to the source of remote commands");
6309 ("-gc", Arg.Set_string gcconfig,
6310 "<path> Collect garbage with the help of a script");
6312 ("-nofc", Arg.Set nofc, " Do not use fontconfig");
6314 ("-v", Arg.Unit (fun () ->
6315 Printf.printf
6316 "%s\nconfiguration path: %s\n"
6317 (version ())
6318 Config.defconfpath;
6319 exit 0), " Print version and exit");
6321 ("-css", Arg.String (fun s -> csspath := Some s),
6322 "<path> Set path to the style sheet to use with EPUB/HTML");
6324 ("-embed", Arg.Set_int rootwid, "<window-id> Embed into window");
6326 ("-origin", Arg.String (fun s -> state.origin <- s),
6327 "<origin> <undocumented>");
6330 (fun s -> state.path <- s)
6331 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:");
6333 if !wtmode
6334 then selfexec := !selfexec ^ " -wtmode";
6336 let histmode = emptystr state.path && not !openlast in
6338 if not (Config.load !openlast)
6339 then dolog "failed to load configuration";
6341 begin match !pageno with
6342 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
6343 | None -> ()
6344 end;
6346 if nonemptystr !gcconfig
6347 then (
6348 let (c, s) =
6349 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6350 | exception exn -> error "socketpair for gc failed: %s" @@ exntos exn
6351 | fds -> fds
6353 match spawn !gcconfig [(c, 0); (c, 1); (s, -1)] with
6354 | exception exn -> error "failed to execute gc script: %s" @@ exntos exn
6355 | _pid ->
6356 Ne.clo c @@ (fun s -> error "failed to close gc fd %s" s);
6357 Config.gc s;
6358 exit 0
6361 let mu =
6362 object (self)
6363 val mutable m_clicks = 0
6364 val mutable m_click_x = 0
6365 val mutable m_click_y = 0
6366 val mutable m_lastclicktime = infinity
6368 method private cleanup =
6369 state.roam <- noroam;
6370 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap
6371 method expose = G.postRedisplay "expose"
6372 method visible v =
6373 let name =
6374 match v with
6375 | Wsi.Unobscured -> "unobscured"
6376 | Wsi.PartiallyObscured -> "partiallyobscured"
6377 | Wsi.FullyObscured -> "fullyobscured"
6379 vlog "visibility change %s" name
6380 method display = display ()
6381 method map mapped = vlog "mapped %b" mapped
6382 method reshape w h =
6383 self#cleanup;
6384 reshape w h
6385 method mouse b d x y m =
6386 if d && canselect ()
6387 then (
6389 * http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx
6391 m_click_x <- x;
6392 m_click_y <- y;
6393 if b = 1
6394 then (
6395 let t = now () in
6396 if abs x - m_click_x > 10
6397 || abs y - m_click_y > 10
6398 || abs_float (t -. m_lastclicktime) > 0.3
6399 then m_clicks <- 0;
6400 m_clicks <- m_clicks + 1;
6401 m_lastclicktime <- t;
6402 if m_clicks = 1
6403 then (
6404 self#cleanup;
6405 G.postRedisplay "cleanup";
6406 state.uioh <- state.uioh#button b d x y m;
6408 else state.uioh <- state.uioh#multiclick m_clicks x y m
6410 else (
6411 self#cleanup;
6412 m_clicks <- 0;
6413 m_lastclicktime <- infinity;
6414 state.uioh <- state.uioh#button b d x y m
6417 else (
6418 state.uioh <- state.uioh#button b d x y m
6420 method motion x y =
6421 state.mpos <- (x, y);
6422 state.uioh <- state.uioh#motion x y
6423 method pmotion x y =
6424 state.mpos <- (x, y);
6425 state.uioh <- state.uioh#pmotion x y
6426 method key k m =
6427 let mascm = m land (
6428 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6429 ) in
6430 let keyboard k m =
6431 let x = state.x and y = state.y in
6432 keyboard k m;
6433 if x != state.x || y != state.y then self#cleanup
6435 match state.keystate with
6436 | KSnone ->
6437 let km = k, mascm in
6438 begin
6439 match
6440 let modehash = state.uioh#modehash in
6441 try Hashtbl.find modehash km
6442 with Not_found ->
6443 try Hashtbl.find (findkeyhash conf "global") km
6444 with Not_found -> KMinsrt (k, m)
6445 with
6446 | KMinsrt (k, m) -> keyboard k m
6447 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6448 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6450 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6451 List.iter (fun (k, m) -> keyboard k m) insrt;
6452 state.keystate <- KSnone
6453 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6454 state.keystate <- KSinto (keys, insrt)
6455 | KSinto _ -> state.keystate <- KSnone
6457 method enter x y =
6458 state.mpos <- (x, y);
6459 state.uioh <- state.uioh#pmotion x y
6460 method leave = state.mpos <- (-1, -1)
6461 method winstate wsl = state.winstate <- wsl
6462 method quit = raise Quit
6465 let wsfd, winw, winh = Wsi.init mu !rootwid conf.cwinw conf.cwinh platform in
6467 setbgcol conf.bgcolor;
6468 state.wsfd <- wsfd;
6470 if not @@ List.exists GlMisc.check_extension
6471 [ "GL_ARB_texture_rectangle"
6472 ; "GL_EXT_texture_recangle"
6473 ; "GL_NV_texture_rectangle" ]
6474 then (dolog "OpenGL does not suppport rectangular textures"; exit 1);
6476 if substratis (GlMisc.get_string `renderer) 0 "Mesa DRI Intel("
6477 then (
6478 defconf.sliceheight <- 1024;
6479 defconf.texcount <- 32;
6480 defconf.usepbo <- true;
6483 let cs, ss =
6484 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6485 | exception exn ->
6486 dolog "socketpair failed: %s" @@ exntos exn;
6487 exit 1
6488 | (r, w) ->
6489 cloexec r;
6490 cloexec w;
6491 r, w
6494 setcheckers conf.checkers;
6496 opengl_has_pbo := GlMisc.check_extension "GL_ARB_pixel_buffer_object";
6498 begin match !csspath with
6499 | None -> ()
6500 | Some "" -> conf.css <- E.s
6501 | Some path ->
6502 let css = filecontents path in
6503 let l = String.length css in
6504 conf.css <-
6505 if substratis css (l-2) "\r\n"
6506 then String.sub css 0 (l-2)
6507 else (if css.[l-1] = '\n'
6508 then String.sub css 0 (l-1)
6509 else css);
6510 end;
6511 init cs (
6512 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
6513 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6514 !Config.fontpath, !trimcachepath, !opengl_has_pbo, not !nofc
6516 List.iter GlArray.enable [`texture_coord; `vertex];
6517 state.ss <- ss;
6518 reshape ~firsttime:true winw winh;
6519 state.uioh <- uioh;
6520 if histmode
6521 then (
6522 Wsi.settitle "llpp (history)";
6523 enterhistmode ();
6525 else (
6526 state.text <- "Opening " ^ (mbtoutf8 state.path);
6527 opendoc state.path state.password;
6529 display ();
6530 Wsi.mapwin ();
6531 Wsi.setcursor Wsi.CURSOR_INHERIT;
6532 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
6534 let rec reap () =
6535 match Unix.waitpid [Unix.WNOHANG] ~-1 with
6536 | exception (Unix.Unix_error (Unix.ECHILD, _, _)) -> ()
6537 | exception exn -> dolog "Unix.waitpid: %s" @@ exntos exn
6538 | 0, _ -> ()
6539 | _pid, _status -> reap ()
6541 Sys.set_signal Sys.sigchld (Sys.Signal_handle (fun _ -> doreap := true));
6543 let optrfd =
6544 ref (
6545 if nonemptystr !rcmdpath
6546 then remoteopen !rcmdpath
6547 else None
6551 let rec loop deadline =
6552 if !doreap
6553 then (
6554 doreap := false;
6555 reap ()
6557 let r = [state.ss; state.wsfd] in
6558 let r =
6559 match !optrfd with
6560 | None -> r
6561 | Some fd -> fd :: r
6563 if state.redisplay
6564 then (
6565 state.redisplay <- false;
6566 display ();
6568 let timeout =
6569 let now = now () in
6570 if deadline > now
6571 then (
6572 if deadline = infinity
6573 then ~-.1.0
6574 else max 0.0 (deadline -. now)
6576 else 0.0
6578 let r, _, _ =
6579 try Unix.select r [] [] timeout
6580 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6582 begin match r with
6583 | [] ->
6584 state.ghyll None;
6585 let newdeadline =
6586 if state.ghyll == noghyll
6587 then
6588 match state.autoscroll with
6589 | Some step when step != 0 ->
6590 let y = state.y + step in
6591 let fy = if conf.maxhfit then state.winh else 0 in
6592 let y =
6593 if y < 0
6594 then state.maxy - fy
6595 else if y >= state.maxy - fy then 0 else y
6597 if state.mode = View
6598 then gotoxy_and_clear_text state.x y
6599 else gotoxy state.x y;
6600 deadline +. 0.01
6601 | _ -> infinity
6602 else deadline +. 0.01
6604 loop newdeadline
6606 | l ->
6607 let rec checkfds = function
6608 | [] -> ()
6609 | fd :: rest when fd = state.ss ->
6610 let cmd = rcmd state.ss in
6611 act cmd;
6612 checkfds rest
6614 | fd :: rest when fd = state.wsfd ->
6615 Wsi.readresp fd;
6616 checkfds rest
6618 | fd :: rest when Some fd = !optrfd ->
6619 begin match remote fd with
6620 | None -> optrfd := remoteopen !rcmdpath;
6621 | opt -> optrfd := opt
6622 end;
6623 checkfds rest
6625 | _ :: rest ->
6626 dolog "select returned unknown descriptor";
6627 checkfds rest
6629 checkfds l;
6630 let newdeadline =
6631 let deadline1 =
6632 if deadline = infinity
6633 then now () +. 0.01
6634 else deadline
6636 match state.autoscroll with
6637 | Some step when step != 0 -> deadline1
6638 | _ -> if state.ghyll == noghyll then infinity else deadline1
6640 loop newdeadline
6641 end;
6643 match loop infinity with
6644 | exception Quit ->
6645 Config.save leavebirdseye;
6646 if hasunsavedchanges ()
6647 then save ()
6648 | _ -> error "umpossible - infinity reached"