Make wget dl behave like the curl one
[llpp.git] / main.ml
blobba856d0f68c0d7c1705901630386c628275dad36
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 getpdimrect : int -> float array = "ml_getpdimrect";;
10 external whatsunder : opaque -> int -> int -> under = "ml_whatsunder";;
11 external markunder : opaque -> int -> int -> mark -> bool = "ml_markunder";;
12 external clearmark : opaque -> unit = "ml_clearmark";;
13 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
14 external getmaxw : unit -> float = "ml_getmaxw";;
15 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
16 external measurestr : int -> string -> float = "ml_measure_string";;
17 external postprocess : opaque -> int -> int -> int -> (int * string * int)
18 -> int = "ml_postprocess";;
19 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
20 external setaalevel : int -> unit = "ml_setaalevel";;
21 external realloctexts : int -> bool = "ml_realloctexts";;
22 external findlink : opaque -> linkdir -> link = "ml_findlink";;
23 external getlink : opaque -> int -> under = "ml_getlink";;
24 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
25 external getlinkcount : opaque -> int = "ml_getlinkcount";;
26 external findpwl : int -> int -> pagewithlinks = "ml_find_page_with_links";;
27 external getpbo : width -> height -> colorspace -> opaque = "ml_getpbo";;
28 external freepbo : opaque -> unit = "ml_freepbo";;
29 external unmappbo : opaque -> unit = "ml_unmappbo";;
30 external bousable : unit -> bool = "ml_bo_usable";;
31 external unproject : opaque -> int -> int
32 -> (int * int) option = "ml_unproject";;
33 external project : opaque -> int -> int -> float -> float
34 -> (float * float) = "ml_project";;
35 external drawtile : tileparams -> opaque
36 -> unit = "ml_drawtile";;
37 external rectofblock : opaque -> int -> int
38 -> float array option = "ml_rectofblock";;
39 external begintiles : unit -> unit = "ml_begintiles";;
40 external endtiles : unit -> unit = "ml_endtiles";;
41 external addannot : opaque -> int -> int -> string -> unit = "ml_addannot";;
42 external modannot : opaque -> slinkindex -> string -> unit = "ml_modannot";;
43 external delannot : opaque -> slinkindex -> unit = "ml_delannot";;
44 external hasunsavedchanges : unit -> bool = "ml_hasunsavedchanges";;
45 external savedoc : string -> unit = "ml_savedoc";;
46 external getannotcontents : opaque -> slinkindex
47 -> string = "ml_getannotcontents";;
48 external drawprect : opaque -> int -> int -> float array
49 -> unit = "ml_drawprect";;
50 external wcmd : Unix.file_descr -> bytes -> int -> unit = "ml_wcmd";;
51 external rcmd : Unix.file_descr -> string = "ml_rcmd";;
52 external uritolocation : string
53 -> (pageno * float * float) = "ml_uritolocation";;
54 external isexternallink : string -> bool = "ml_isexternallink";;
56 (* copysel _will_ close the supplied descriptor *)
57 external copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
59 let selfexec = ref E.s;;
60 let ignoredoctitlte = ref false;;
61 let opengl_has_pbo = ref false;;
63 let drawstring size x y s =
64 Gl.enable `blend;
65 Gl.enable `texture_2d;
66 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
67 ignore (drawstr size x y s);
68 Gl.disable `blend;
69 Gl.disable `texture_2d;
72 let drawstring1 size x y s =
73 drawstr size x y s;
76 let drawstring2 size x y fmt =
77 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
80 module UniSyms = struct
81 let ellipsis = "\xe2\x80\xa6";;
82 let radical = "\xe2\x88\x9a";;
83 let lguillemet = "\xc2\xab";;
84 let rguillemet = "\xc2\xbb";;
85 end;;
87 let _debugl l =
88 dolog {|l %d dim=%d {
89 WxH %dx%d
90 vWxH %dx%d
91 pagex,y %d,%d
92 dispx,y %d,%d
93 column %d
94 }|}
95 l.pageno l.pagedimno
96 l.pagew l.pageh
97 l.pagevw l.pagevh
98 l.pagex l.pagey
99 l.pagedispx l.pagedispy
100 l.pagecol
103 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
104 dolog {|rect {
105 x0,y0=(% f, % f)
106 x1,y1=(% f, % f)
107 x2,y2=(% f, % f)
108 x3,y3=(% f, % f)
109 }|} x0 y0 x1 y1 x2 y2 x3 y3;
112 let isbirdseye = function
113 | Birdseye _ -> true
114 | Textentry _ | View | LinkNav _ -> false
117 let istextentry = function
118 | Textentry _ -> true
119 | Birdseye _ | View | LinkNav _ -> false
122 let wtmode = ref false;;
123 let cxack = ref false;;
125 let pgscale h = truncate (float h *. conf.pgscale);;
127 let hscrollh () =
128 if state.uioh#alwaysscrolly || ((conf.scrollb land scrollbhv != 0)
129 && (state.w > state.winw))
130 then conf.scrollbw
131 else 0
134 let vscrollw () =
135 if state.uioh#alwaysscrolly || ((conf.scrollb land scrollbvv != 0)
136 && (state.maxy > state.winh))
137 then conf.scrollbw
138 else 0
141 let vscrollhit x =
142 if conf.leftscroll
143 then x < vscrollw ()
144 else x > state.winw - vscrollw ()
147 let setfontsize n =
148 fstate.fontsize <- n;
149 fstate.wwidth <- measurestr fstate.fontsize "w";
150 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
153 let vlog fmt =
154 if conf.verbose
155 then dolog fmt
156 else Printf.kprintf ignore fmt
159 let launchpath () =
160 if emptystr conf.pathlauncher
161 then dolog "%s" state.path
162 else (
163 let command = Str.global_replace percentsre state.path conf.pathlauncher in
164 match spawn command [] with
165 | _pid -> ()
166 | exception exn ->
167 dolog "failed to execute `%s': %s" command @@ exntos exn
171 module G =
172 struct
173 let postRedisplay who =
174 vlog "redisplay for [%S]" who;
175 state.redisplay <- true;
177 end;;
179 let getopaque pageno =
180 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
181 with Not_found -> None
184 let pagetranslatepoint l x y =
185 let dy = y - l.pagedispy in
186 let y = dy + l.pagey in
187 let dx = x - l.pagedispx in
188 let x = dx + l.pagex in
189 (x, y);
192 let onppundermouse g x y d =
193 let rec f = function
194 | l :: rest ->
195 begin match getopaque l.pageno with
196 | Some opaque ->
197 let x0 = l.pagedispx in
198 let x1 = x0 + l.pagevw in
199 let y0 = l.pagedispy in
200 let y1 = y0 + l.pagevh in
201 if y >= y0 && y <= y1 && x >= x0 && x <= x1
202 then
203 let px, py = pagetranslatepoint l x y in
204 match g opaque l px py with
205 | Some res -> res
206 | None -> f rest
207 else f rest
208 | _ ->
209 f rest
211 | [] -> d
213 f state.layout
216 let getunder x y =
217 let g opaque l px py =
218 if state.bzoom
219 then (
220 match rectofblock opaque px py with
221 | Some [|x0;x1;y0;y1|] ->
222 let rect = (x0, y0, x1, y0, x1, y1, x0, y1) in
223 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
224 state.rects <- [l.pageno, color, rect];
225 G.postRedisplay "getunder";
226 | _ -> ()
228 let under = whatsunder opaque px py in
229 if under = Unone then None else Some under
231 onppundermouse g x y Unone
234 let unproject x y =
235 let g opaque l x y =
236 match unproject opaque x y with
237 | Some (x, y) -> Some (Some (opaque, l.pageno, x, y))
238 | None -> None
240 onppundermouse g x y None;
243 let showtext c s =
244 state.text <- Printf.sprintf "%c%s" c s;
245 G.postRedisplay "showtext";
248 let impmsg fmt =
249 Format.ksprintf (fun s -> showtext '!' s) fmt;
252 let pipef ?(closew=true) cap f cmd =
253 match Unix.pipe () with
254 | exception exn -> dolog "%s cannot create pipe: %S" cap @@ exntos exn
255 | (r, w) ->
256 begin match spawn cmd [r, 0; w, -1] with
257 | exception exn -> dolog "%s: cannot execute %S: %s" cap cmd @@ exntos exn
258 | _pid -> f w
259 end;
260 Ne.clo r (dolog "%s failed to close r: %s" cap);
261 if closew then Ne.clo w (dolog "%s failed to close w: %s" cap);
264 let pipesel opaque cmd =
265 if hassel opaque
266 then pipef ~closew:false "pipesel"
267 (fun w ->
268 copysel w opaque;
269 G.postRedisplay "pipesel"
270 ) cmd
273 let paxunder x y =
274 let g opaque l px py =
275 if markunder opaque px py conf.paxmark
276 then (
277 Some (fun () ->
278 match getopaque l.pageno with
279 | None -> ()
280 | Some opaque -> pipesel opaque conf.paxcmd
283 else None
285 G.postRedisplay "paxunder";
286 if conf.paxmark = Mark_page
287 then
288 List.iter (fun l ->
289 match getopaque l.pageno with
290 | None -> ()
291 | Some opaque -> clearmark opaque) state.layout;
292 state.roam <- onppundermouse g x y (fun () -> impmsg "whoopsie daisy");
295 let selstring s =
296 pipef
297 "selstring" (fun w ->
299 let l = String.length s in
300 let bytes = Bytes.unsafe_of_string s in
301 let n = tempfailureretry (Unix.write w bytes 0) l in
302 if n != l
303 then impmsg "failed to write %d characters to sel pipe, wrote %d" l n;
304 with exn -> impmsg "failed to write to sel pipe: %s" @@ exntos exn
305 ) conf.selcmd
308 let undertext = function
309 | Unone -> "none"
310 | Ulinkuri s -> s
311 | Utext s -> "font: " ^ s
312 | Uannotation (opaque, slinkindex) ->
313 "annotation: " ^ getannotcontents opaque slinkindex
316 let updateunder x y =
317 match getunder x y with
318 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
319 | Ulinkuri uri ->
320 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
321 Wsi.setcursor Wsi.CURSOR_INFO
322 | Utext s ->
323 if conf.underinfo then showtext 'f' ("ont: " ^ s);
324 Wsi.setcursor Wsi.CURSOR_TEXT
325 | Uannotation _ ->
326 if conf.underinfo then showtext 'a' "nnotation";
327 Wsi.setcursor Wsi.CURSOR_INFO
330 let showlinktype under =
331 if conf.underinfo && under != Unone
332 then showtext ' ' @@ undertext under
335 let [@warning "-4"] intentry_with_suffix text key =
336 let text =
337 match key with
338 | Keys.Ascii ('0'..'9' as c) -> addchar text c
339 | Keys.Ascii ('k' | 'm' | 'g' | 'K' | 'M' | 'G' as c) ->
340 addchar text @@ asciilower c
341 | _ ->
342 state.text <- Printf.sprintf "invalid key";
343 text
345 TEcont text
348 let wcmd fmt =
349 let b = Buffer.create 16 in
350 Printf.kbprintf
351 (fun b ->
352 let b = Buffer.to_bytes b in
353 wcmd state.ss b @@ Bytes.length b
354 ) b fmt
357 let nogeomcmds cmds =
358 match cmds with
359 | s, [] -> emptystr s
360 | _ -> false
363 let layoutN ((columns, coverA, coverB), b) x y sw sh =
364 let rec fold accu n =
365 if n = Array.length b
366 then accu
367 else
368 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
369 if (vy - y) > sh &&
370 (n = coverA - 1
371 || n = state.pagecount - coverB
372 || (n - coverA) mod columns = columns - 1)
373 then accu
374 else
375 let accu =
376 if vy + h > y
377 then
378 let pagey = max 0 (y - vy) in
379 let pagedispy = if pagey > 0 then 0 else vy - y in
380 let pagedispx, pagex =
381 let pdx =
382 if n = coverA - 1 || n = state.pagecount - coverB
383 then x + (sw - w) / 2
384 else dx + xoff + x
386 if pdx < 0
387 then 0, -pdx
388 else pdx, 0
390 let pagevw =
391 let vw = sw - pagedispx in
392 let pw = w - pagex in
393 min vw pw
395 let pagevh = min (h - pagey) (sh - pagedispy) in
396 if pagevw > 0 && pagevh > 0
397 then
398 let e =
399 { pageno = n
400 ; pagedimno = pdimno
401 ; pagew = w
402 ; pageh = h
403 ; pagex = pagex
404 ; pagey = pagey
405 ; pagevw = pagevw
406 ; pagevh = pagevh
407 ; pagedispx = pagedispx
408 ; pagedispy = pagedispy
409 ; pagecol = 0
412 e :: accu
413 else
414 accu
415 else
416 accu
418 fold accu (n+1)
420 if Array.length b = 0
421 then []
422 else List.rev (fold [] (page_of_y y))
425 let layoutS (columns, b) x y sw sh =
426 let rec fold accu n =
427 if n = Array.length b
428 then accu
429 else
430 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
431 if (vy - y) > sh
432 then accu
433 else
434 let accu =
435 if vy + pageh > y
436 then
437 let x = xoff + x in
438 let pagey = max 0 (y - vy) in
439 let pagedispy = if pagey > 0 then 0 else vy - y in
440 let pagedispx, pagex =
441 if px = 0
442 then (
443 if x < 0
444 then 0, -x
445 else x, 0
447 else (
448 let px = px - x in
449 if px < 0
450 then -px, 0
451 else 0, px
454 let pagecolw = pagew/columns in
455 let pagedispx =
456 if pagecolw < sw
457 then pagedispx + ((sw - pagecolw) / 2)
458 else pagedispx
460 let pagevw =
461 let vw = sw - pagedispx in
462 let pw = pagew - pagex in
463 min vw pw
465 let pagevw = min pagevw pagecolw in
466 let pagevh = min (pageh - pagey) (sh - pagedispy) in
467 if pagevw > 0 && pagevh > 0
468 then
469 let e =
470 { pageno = n/columns
471 ; pagedimno = pdimno
472 ; pagew = pagew
473 ; pageh = pageh
474 ; pagex = pagex
475 ; pagey = pagey
476 ; pagevw = pagevw
477 ; pagevh = pagevh
478 ; pagedispx = pagedispx
479 ; pagedispy = pagedispy
480 ; pagecol = n mod columns
483 e :: accu
484 else
485 accu
486 else
487 accu
489 fold accu (n+1)
491 List.rev (fold [] 0)
494 let layout x y sw sh =
495 if nogeomcmds state.geomcmds
496 then
497 match conf.columns with
498 | Csingle b -> layoutN ((1, 0, 0), b) x y sw sh
499 | Cmulti c -> layoutN c x y sw sh
500 | Csplit s -> layoutS s x y sw sh
501 else []
504 let maxy () = state.maxy - if conf.maxhfit then state.winh else 0;;
506 let clamp incr = bound (state.y + incr) 0 @@ maxy ();;
508 let itertiles l f =
509 let tilex = l.pagex mod conf.tilew in
510 let tiley = l.pagey mod conf.tileh in
512 let col = l.pagex / conf.tilew in
513 let row = l.pagey / conf.tileh in
515 let rec rowloop row y0 dispy h =
516 if h = 0
517 then ()
518 else (
519 let dh = conf.tileh - y0 in
520 let dh = min h dh in
521 let rec colloop col x0 dispx w =
522 if w = 0
523 then ()
524 else (
525 let dw = conf.tilew - x0 in
526 let dw = min w dw in
527 f col row dispx dispy x0 y0 dw dh;
528 colloop (col+1) 0 (dispx+dw) (w-dw)
531 colloop col tilex l.pagedispx l.pagevw;
532 rowloop (row+1) 0 (dispy+dh) (h-dh)
535 if l.pagevw > 0 && l.pagevh > 0
536 then rowloop row tiley l.pagedispy l.pagevh;
539 let gettileopaque l col row =
540 let key =
541 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
543 try Some (Hashtbl.find state.tilemap key)
544 with Not_found -> None
547 let puttileopaque l col row gen colorspace angle opaque size elapsed =
548 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
549 Hashtbl.add state.tilemap key (opaque, size, elapsed)
552 let filledrect2 x0 y0 x1 y1 x2 y2 x3 y3 =
553 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x1; y1; x2; y2; x3; y3 |];
554 GlArray.vertex `two state.vraw;
555 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
558 let filledrect1 x0 y0 x1 y1 = filledrect2 x0 y0 x0 y1 x1 y0 x1 y1;;
560 let filledrect x0 y0 x1 y1 =
561 GlArray.disable `texture_coord;
562 filledrect1 x0 y0 x1 y1;
563 GlArray.enable `texture_coord;
566 let linerect x0 y0 x1 y1 =
567 GlArray.disable `texture_coord;
568 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y1; x1; y0 |];
569 GlArray.vertex `two state.vraw;
570 GlArray.draw_arrays `line_loop ~first:0 ~count:4;
571 GlArray.enable `texture_coord;
574 let drawtiles l color =
575 GlDraw.color color;
576 begintiles ();
577 let f col row x y tilex tiley w h =
578 match gettileopaque l col row with
579 | Some (opaque, _, t) ->
580 let params = x, y, w, h, tilex, tiley in
581 if conf.invert
582 then GlTex.env (`mode `blend);
583 drawtile params opaque;
584 if conf.invert
585 then GlTex.env (`mode `modulate);
586 if conf.debug
587 then (
588 endtiles ();
589 let s = Printf.sprintf
590 "%d[%d,%d] %f sec"
591 l.pageno col row t
593 let w = measurestr fstate.fontsize s in
594 GlDraw.color (0.0, 0.0, 0.0);
595 filledrect (float (x-2))
596 (float (y-2))
597 (float (x+2) +. w)
598 (float (y + fstate.fontsize + 2));
599 GlDraw.color color;
600 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
601 begintiles ();
604 | None ->
605 endtiles ();
606 let w =
607 let lw = state.winw - x in
608 min lw w
609 and h =
610 let lh = state.winh - y in
611 min lh h
613 if conf.invert
614 then GlTex.env (`mode `blend);
615 begin match state.checkerstexid with
616 | Some id ->
617 Gl.enable `texture_2d;
618 GlTex.bind_texture ~target:`texture_2d id;
619 let x0 = float x
620 and y0 = float y
621 and x1 = float (x+w)
622 and y1 = float (y+h) in
624 let tw = float w /. 16.0
625 and th = float h /. 16.0 in
626 let tx0 = float tilex /. 16.0
627 and ty0 = float tiley /. 16.0 in
628 let tx1 = tx0 +. tw
629 and ty1 = ty0 +. th in
630 Raw.sets_float state.vraw ~pos:0
631 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
632 Raw.sets_float state.traw ~pos:0
633 [| tx0; ty0; tx0; ty1; tx1; ty0; tx1; ty1 |];
634 GlArray.vertex `two state.vraw;
635 GlArray.tex_coord `two state.traw;
636 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
637 Gl.disable `texture_2d;
639 | None ->
640 GlDraw.color (1.0, 1.0, 1.0);
641 filledrect (float x) (float y) (float (x+w)) (float (y+h));
642 end;
643 if conf.invert
644 then GlTex.env (`mode `modulate);
645 if w > 128 && h > fstate.fontsize + 10
646 then (
647 let c = if conf.invert then 1.0 else 0.0 in
648 GlDraw.color (c, c, c);
649 let c, r =
650 if conf.verbose
651 then (col*conf.tilew, row*conf.tileh)
652 else col, row
654 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
656 GlDraw.color color;
657 begintiles ();
659 itertiles l f;
660 endtiles ();
663 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
665 let tilevisible1 l x y =
666 let ax0 = l.pagex
667 and ax1 = l.pagex + l.pagevw
668 and ay0 = l.pagey
669 and ay1 = l.pagey + l.pagevh in
671 let bx0 = x
672 and by0 = y in
673 let bx1 = min (bx0 + conf.tilew) l.pagew
674 and by1 = min (by0 + conf.tileh) l.pageh in
676 let rx0 = max ax0 bx0
677 and ry0 = max ay0 by0
678 and rx1 = min ax1 bx1
679 and ry1 = min ay1 by1 in
681 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
682 nonemptyintersection
685 let tilevisible layout n x y =
686 let rec findpageinlayout m = function
687 | l :: rest when l.pageno = n ->
688 tilevisible1 l x y || (
689 match conf.columns with
690 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
691 | Csplit _ | Csingle _ | Cmulti _ -> false
693 | _ :: rest -> findpageinlayout 0 rest
694 | [] -> false
696 findpageinlayout 0 layout;
699 let tileready l x y =
700 tilevisible1 l x y &&
701 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
704 let tilepage n p layout =
705 let rec loop = function
706 | l :: rest ->
707 if l.pageno = n
708 then
709 let f col row _ _ _ _ _ _ =
710 if state.currently = Idle
711 then
712 match gettileopaque l col row with
713 | Some _ -> ()
714 | None ->
715 let x = col*conf.tilew
716 and y = row*conf.tileh in
717 let w =
718 let w = l.pagew - x in
719 min w conf.tilew
721 let h =
722 let h = l.pageh - y in
723 min h conf.tileh
725 let pbo =
726 if conf.usepbo
727 then getpbo w h conf.colorspace
728 else ~< "0"
730 wcmd "tile %s %d %d %d %d %s"
731 (~> p) x y w h (~> pbo);
732 state.currently <-
733 Tiling (
734 l, p, conf.colorspace, conf.angle,
735 state.gen, col, row, conf.tilew, conf.tileh
738 itertiles l f;
739 else
740 loop rest
742 | [] -> ()
744 if nogeomcmds state.geomcmds
745 then loop layout;
748 let preloadlayout x y sw sh =
749 let y = if y < sh then 0 else y - sh in
750 let x = min 0 (x + sw) in
751 let h = sh*3 in
752 let w = sw*3 in
753 layout x y w h;
756 let load pages =
757 let rec loop pages =
758 if state.currently != Idle
759 then ()
760 else
761 match pages with
762 | l :: rest ->
763 begin match getopaque l.pageno with
764 | None ->
765 wcmd "page %d %d" l.pageno l.pagedimno;
766 state.currently <- Loading (l, state.gen);
767 | Some opaque ->
768 tilepage l.pageno opaque pages;
769 loop rest
770 end;
771 | _ -> ()
773 if nogeomcmds state.geomcmds
774 then loop pages
777 let preload pages =
778 load pages;
779 if conf.preload && state.currently = Idle
780 then load (preloadlayout state.x state.y state.winw state.winh);
783 let layoutready layout =
784 let rec fold all ls =
785 all && match ls with
786 | l :: rest ->
787 let seen = ref false in
788 let allvisible = ref true in
789 let foo col row _ _ _ _ _ _ =
790 seen := true;
791 allvisible := !allvisible &&
792 begin match gettileopaque l col row with
793 | Some _ -> true
794 | None -> false
797 itertiles l foo;
798 fold (!seen && !allvisible) rest
799 | [] -> true
801 let alltilesvisible = fold true layout in
802 alltilesvisible;
805 let gotoxy x y =
806 let y = bound y 0 state.maxy in
807 let y, layout, proceed =
808 match conf.maxwait with
809 | Some time when state.ghyll == noghyll ->
810 begin match state.throttle with
811 | None ->
812 let layout = layout x y state.winw state.winh in
813 let ready = layoutready layout in
814 if not ready
815 then (
816 load layout;
817 state.throttle <- Some (layout, y, now ());
819 else G.postRedisplay "gotoxy showall (None)";
820 y, layout, ready
821 | Some (_, _, started) ->
822 let dt = now () -. started in
823 if dt > time
824 then (
825 state.throttle <- None;
826 let layout = layout x y state.winw state.winh in
827 load layout;
828 G.postRedisplay "maxwait";
829 y, layout, true
831 else -1, [], false
834 | _ ->
835 let layout = layout x y state.winw state.winh in
836 if not !wtmode || layoutready layout
837 then G.postRedisplay "gotoxy ready";
838 y, layout, true
840 if proceed
841 then (
842 state.x <- x;
843 state.y <- y;
844 state.layout <- layout;
845 begin match state.mode with
846 | LinkNav ln ->
847 begin match ln with
848 | Ltexact (pageno, linkno) ->
849 let rec loop = function
850 | [] ->
851 state.lnava <- Some (pageno, linkno);
852 state.mode <- LinkNav (Ltgendir 0)
853 | l :: _ when l.pageno = pageno ->
854 begin match getopaque pageno with
855 | None -> state.mode <- LinkNav (Ltnotready (pageno, 0))
856 | Some opaque ->
857 let x0, y0, x1, y1 = getlinkrect opaque linkno in
858 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
859 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
860 then state.mode <- LinkNav (Ltgendir 0)
862 | _ :: rest -> loop rest
864 loop layout
865 | Ltnotready _ | Ltgendir _ -> ()
867 | Birdseye _ | Textentry _ | View -> ()
868 end;
869 begin match state.mode with
870 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
871 if not (pagevisible layout pageno)
872 then (
873 match state.layout with
874 | [] -> ()
875 | l :: _ ->
876 state.mode <- Birdseye (
877 conf, leftx, l.pageno, hooverpageno, anchor
880 | LinkNav lt ->
881 begin match lt with
882 | Ltnotready (_, dir)
883 | Ltgendir dir ->
884 let linknav =
885 let rec loop = function
886 | [] -> lt
887 | l :: rest ->
888 match getopaque l.pageno with
889 | None -> Ltnotready (l.pageno, dir)
890 | Some opaque ->
891 let link =
892 let ld =
893 if dir = 0
894 then LDfirstvisible (l.pagex, l.pagey, dir)
895 else (
896 if dir > 0 then LDfirst else LDlast
899 findlink opaque ld
901 match link with
902 | Lnotfound -> loop rest
903 | Lfound n ->
904 showlinktype (getlink opaque n);
905 Ltexact (l.pageno, n)
907 loop state.layout
909 state.mode <- LinkNav linknav
910 | Ltexact _ -> ()
912 | Textentry _ | View -> ()
913 end;
914 preload layout;
916 state.ghyll <- noghyll;
917 if conf.updatecurs
918 then (
919 let mx, my = state.mpos in
920 updateunder mx my;
924 let conttiling pageno opaque =
925 tilepage pageno opaque
926 (if conf.preload
927 then preloadlayout state.x state.y state.winw state.winh
928 else state.layout)
931 let gotoxy_and_clear_text x y =
932 if not conf.verbose then state.text <- E.s;
933 gotoxy x y;
936 let getanchory (n, top, dtop) =
937 let y, h = getpageyh n in
938 if conf.presentation
939 then
940 let ips = calcips h in
941 y + truncate (top*.float h -. dtop*.float ips) + ips;
942 else
943 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
946 let gotoanchor anchor =
947 gotoxy state.x (getanchory anchor);
950 let addnav () =
951 getanchor () |> cbput state.hists.nav;
954 let addnavnorc () =
955 getanchor () |> cbput_dont_update_rc state.hists.nav;
958 let getnav dir =
959 let anchor = cbgetc state.hists.nav dir in
960 getanchory anchor;
963 let gotoghyll1 single y =
964 let scroll f n a b =
965 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
966 let snake f a b =
967 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
968 if f < a
969 then s (float f /. float a)
970 else (
971 if f > b
972 then 1.0 -. s ((float (f-b) /. float (n-b)))
973 else 1.0
976 snake f a b
977 and summa n a b =
978 let ins = float a *. 0.5
979 and outs = float (n-b) *. 0.5 in
980 let ones = b - a in
981 ins +. outs +. float ones
983 let rec set nab y sy =
984 let (_N, _A, _B), y =
985 if single
986 then
987 let scl = if y > sy then 2 else -2 in
988 let _N, _, _ = nab in
989 (_N,0,_N), y+conf.scrollstep*scl
990 else nab,y in
991 let sum = summa _N _A _B in
992 let dy = float (y - sy) in
993 state.ghyll <- (
994 let rec gf n y1 o =
995 if n >= _N
996 then state.ghyll <- noghyll
997 else
998 let go n =
999 let s = scroll n _N _A _B in
1000 let y1 = y1 +. ((s *. dy) /. sum) in
1001 gotoxy_and_clear_text state.x (truncate y1);
1002 state.ghyll <- gf (n+1) y1;
1004 match o with
1005 | None -> go n
1006 | Some y' when single -> set nab y' state.y
1007 | Some y' -> set (_N/2, 1, 1) y' state.y
1009 gf 0 (float state.y)
1012 match conf.ghyllscroll with
1013 | Some nab when not conf.presentation ->
1014 if state.ghyll == noghyll
1015 then set nab y state.y
1016 else state.ghyll (Some y)
1017 | _ ->
1018 gotoxy_and_clear_text state.x y
1021 let gotoghyll = gotoghyll1 false;;
1023 let gotopage n top =
1024 let y, h = getpageyh n in
1025 let y = y + (truncate (top *. float h)) in
1026 gotoghyll y
1029 let gotopage1 n top =
1030 let y = getpagey n in
1031 let y = y + top in
1032 gotoghyll y
1035 let invalidate s f =
1036 state.redisplay <- false;
1037 state.layout <- [];
1038 state.pdims <- [];
1039 state.rects <- [];
1040 state.rects1 <- [];
1041 match state.geomcmds with
1042 | ps, [] when emptystr ps ->
1043 f ();
1044 state.geomcmds <- s, [];
1046 | ps, [] ->
1047 state.geomcmds <- ps, [s, f];
1049 | ps, (s', _) :: rest when s' = s ->
1050 state.geomcmds <- ps, ((s, f) :: rest);
1052 | ps, cmds ->
1053 state.geomcmds <- ps, ((s, f) :: cmds);
1056 let flushpages () =
1057 Hashtbl.iter (fun _ opaque ->
1058 wcmd "freepage %s" (~> opaque);
1059 ) state.pagemap;
1060 Hashtbl.clear state.pagemap;
1063 let flushtiles () =
1064 if not (Queue.is_empty state.tilelru)
1065 then (
1066 Queue.iter (fun (k, p, s) ->
1067 wcmd "freetile %s" (~> p);
1068 state.memused <- state.memused - s;
1069 Hashtbl.remove state.tilemap k;
1070 ) state.tilelru;
1071 state.uioh#infochanged Memused;
1072 Queue.clear state.tilelru;
1074 load state.layout;
1077 let stateh h =
1078 let h = truncate (float h*.conf.zoom) in
1079 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
1080 h - d
1083 let fillhelp () =
1084 state.help <-
1085 let sl = keystostrlist conf in
1086 let rec loop accu =
1087 function | [] -> accu
1088 | s :: rest -> loop ((s, 0, Noaction) :: accu) rest
1089 in makehelp () @ (("", 0, Noaction) :: loop [] sl) |> Array.of_list
1092 let opendoc path password =
1093 state.path <- path;
1094 state.password <- password;
1095 state.gen <- state.gen + 1;
1096 state.docinfo <- [];
1097 state.outlines <- [||];
1099 flushpages ();
1100 setaalevel conf.aalevel;
1101 let titlepath =
1102 if emptystr state.origin
1103 then path
1104 else state.origin
1106 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
1107 wcmd "open %d %d %d %s\000%s\000%s\000"
1108 (btod !wtmode) (btod !cxack) (btod conf.usedoccss)
1109 path password conf.css;
1110 invalidate "reqlayout"
1111 (fun () ->
1112 wcmd "reqlayout %d %d %d %s\000"
1113 conf.angle (FMTE.to_int conf.fitmodel)
1114 (stateh state.winh) state.nameddest
1116 fillhelp ();
1119 let reload () =
1120 state.anchor <- getanchor ();
1121 opendoc state.path state.password;
1124 let scalecolor c =
1125 let c = c *. conf.colorscale in
1126 (c, c, c);
1129 let scalecolor2 (r, g, b) =
1130 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1133 let docolumns columns =
1134 match columns with
1135 | Csingle _ ->
1136 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1137 let rec loop pageno pdimno pdim y ph pdims =
1138 if pageno = state.pagecount
1139 then ()
1140 else
1141 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1142 match pdims with
1143 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1144 pdimno+1, pdim, rest
1145 | _ ->
1146 pdimno, pdim, pdims
1148 let x = max 0 (((state.winw - w) / 2) - xoff) in
1149 let y =
1150 y + (if conf.presentation
1151 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1152 else (if pageno = 0 then 0 else conf.interpagespace)
1155 a.(pageno) <- (pdimno, x, y, pdim);
1156 loop (pageno+1) pdimno pdim (y + h) h pdims
1158 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1159 conf.columns <- Csingle a;
1161 | Cmulti ((columns, coverA, coverB), _) ->
1162 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1163 let rec loop pageno pdimno pdim x y rowh pdims =
1164 let rec fixrow m =
1165 if m = pageno then () else
1166 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1167 if h < rowh
1168 then (
1169 let y = y + (rowh - h) / 2 in
1170 a.(m) <- (pdimno, x, y, pdim);
1172 fixrow (m+1)
1174 if pageno = state.pagecount
1175 then fixrow (((pageno - 1) / columns) * columns)
1176 else
1177 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1178 match pdims with
1179 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1180 pdimno+1, pdim, rest
1181 | _ ->
1182 pdimno, pdim, pdims
1184 let x, y, rowh' =
1185 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1186 then (
1187 let x = (state.winw - w) / 2 in
1188 let ips =
1189 if conf.presentation then calcips h else conf.interpagespace in
1190 x, y + ips + rowh, h
1192 else (
1193 if (pageno - coverA) mod columns = 0
1194 then (
1195 let x = max 0 (state.winw - state.w) / 2 in
1196 let y =
1197 if conf.presentation
1198 then
1199 let ips = calcips h in
1200 y + (if pageno = 0 then 0 else calcips rowh + ips)
1201 else
1202 y + (if pageno = 0 then 0 else conf.interpagespace)
1204 x, y + rowh, h
1206 else x, y, max rowh h
1209 let y =
1210 if pageno > 1 && (pageno - coverA) mod columns = 0
1211 then (
1212 let y =
1213 if pageno = columns && conf.presentation
1214 then (
1215 let ips = calcips rowh in
1216 for i = 0 to pred columns
1218 let (pdimno, x, y, pdim) = a.(i) in
1219 a.(i) <- (pdimno, x, y+ips, pdim)
1220 done;
1221 y+ips;
1223 else y
1225 fixrow (pageno - columns);
1228 else y
1230 a.(pageno) <- (pdimno, x, y, pdim);
1231 let x = x + w + xoff*2 + conf.interpagespace in
1232 loop (pageno+1) pdimno pdim x y rowh' pdims
1234 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1235 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1237 | Csplit (c, _) ->
1238 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1239 let rec loop pageno pdimno pdim y pdims =
1240 if pageno = state.pagecount
1241 then ()
1242 else
1243 let pdimno, ((_, w, h, _) as pdim), pdims =
1244 match pdims with
1245 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1246 pdimno+1, pdim, rest
1247 | _ ->
1248 pdimno, pdim, pdims
1250 let cw = w / c in
1251 let rec loop1 n x y =
1252 if n = c then y else (
1253 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1254 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1257 let y = loop1 0 0 y in
1258 loop (pageno+1) pdimno pdim y pdims
1260 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1261 conf.columns <- Csplit (c, a);
1264 let represent () =
1265 docolumns conf.columns;
1266 state.maxy <- calcheight ();
1267 if state.reprf == noreprf
1268 then (
1269 match state.mode with
1270 | Birdseye (_, _, pageno, _, _) ->
1271 let y, h = getpageyh pageno in
1272 let top = (state.winh - h) / 2 in
1273 gotoxy state.x (max 0 (y - top))
1274 | Textentry _ | View | LinkNav _ ->
1275 let y = getanchory state.anchor in
1276 let y = min y (state.maxy - state.winh) in
1277 gotoxy state.x y;
1279 else (
1280 state.reprf ();
1281 state.reprf <- noreprf;
1285 let reshape ?(firsttime=false) w h =
1286 GlDraw.viewport ~x:0 ~y:0 ~w ~h;
1287 if not firsttime && nogeomcmds state.geomcmds
1288 then state.anchor <- getanchor ();
1290 state.winw <- w;
1291 let w = truncate (float w *. conf.zoom) in
1292 let w = max w 2 in
1293 state.winh <- h;
1294 setfontsize fstate.fontsize;
1295 GlMat.mode `modelview;
1296 GlMat.load_identity ();
1298 GlMat.mode `projection;
1299 GlMat.load_identity ();
1300 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1301 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1302 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
1304 let relx =
1305 if conf.zoom <= 1.0
1306 then 0.0
1307 else float state.x /. float state.w
1309 invalidate "geometry"
1310 (fun () ->
1311 state.w <- w;
1312 if not firsttime
1313 then state.x <- truncate (relx *. float w);
1314 let w =
1315 match conf.columns with
1316 | Csingle _ -> w
1317 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1318 | Csplit (c, _) -> w * c
1320 wcmd "geometry %d %d %d"
1321 w (stateh h) (FMTE.to_int conf.fitmodel)
1325 let enttext () =
1326 let len = String.length state.text in
1327 let x0 = if conf.leftscroll then vscrollw () else 0 in
1328 let drawstring s =
1329 let hscrollh =
1330 match state.mode with
1331 | Textentry _ | View | LinkNav _ ->
1332 let h, _, _ = state.uioh#scrollpw in
1334 | Birdseye _ -> 0
1336 let rect x w =
1337 filledrect
1338 x (float (state.winh - (fstate.fontsize + 4) - hscrollh))
1339 (x+.w) (float (state.winh - hscrollh))
1342 let w = float (state.winw - 1 - vscrollw ()) in
1343 if state.progress >= 0.0 && state.progress < 1.0
1344 then (
1345 GlDraw.color (0.3, 0.3, 0.3);
1346 let w1 = w *. state.progress in
1347 rect (float x0) w1;
1348 GlDraw.color (0.0, 0.0, 0.0);
1349 rect (float x0+.w1) (float x0+.w-.w1)
1351 else (
1352 GlDraw.color (0.0, 0.0, 0.0);
1353 rect (float x0) w;
1356 GlDraw.color (1.0, 1.0, 1.0);
1357 drawstring
1358 fstate.fontsize
1359 (if conf.leftscroll then x0 + 2 else x0 + if len > 0 then 8 else 2)
1360 (state.winh - hscrollh - 5) s;
1362 let s =
1363 match state.mode with
1364 | Textentry ((prefix, text, _, _, _, _), _) ->
1365 let s =
1366 if len > 0
1367 then Printf.sprintf "%s%s_ [%s]" prefix text state.text
1368 else Printf.sprintf "%s%s_" prefix text
1372 | Birdseye _ | View | LinkNav _ -> state.text
1374 let s =
1375 if state.newerrmsgs
1376 then (
1377 if not (istextentry state.mode) && state.uioh#eformsgs
1378 then
1379 let s1 = "(press 'e' to review error messasges)" in
1380 if nonemptystr s then s ^ " " ^ s1 else s1
1381 else s
1383 else s
1385 if nonemptystr s
1386 then drawstring s
1389 let gctiles () =
1390 let len = Queue.length state.tilelru in
1391 let layout = lazy (
1392 match state.throttle with
1393 | None ->
1394 if conf.preload
1395 then preloadlayout state.x state.y state.winw state.winh
1396 else state.layout
1397 | Some (layout, _, _) ->
1398 layout
1399 ) in
1400 let rec loop qpos =
1401 if state.memused > conf.memlimit
1402 then (
1403 if qpos < len
1404 then
1405 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1406 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1407 let (_, pw, ph, _) = getpagedim n in
1408 if gen = state.gen
1409 && colorspace = conf.colorspace
1410 && angle = conf.angle
1411 && pagew = pw
1412 && pageh = ph
1413 && (
1414 let x = col*conf.tilew
1415 and y = row*conf.tileh in
1416 tilevisible (Lazy.force_val layout) n x y
1418 then Queue.push lruitem state.tilelru
1419 else (
1420 freepbo p;
1421 wcmd "freetile %s" (~> p);
1422 state.memused <- state.memused - s;
1423 state.uioh#infochanged Memused;
1424 Hashtbl.remove state.tilemap k;
1426 loop (qpos+1)
1429 loop 0
1432 let onpagerect pageno f =
1433 let b =
1434 match conf.columns with
1435 | Cmulti (_, b) -> b
1436 | Csingle b -> b
1437 | Csplit (_, b) -> b
1439 if pageno >= 0 && pageno < Array.length b
1440 then
1441 let (_, _, _, (_, w, h, _)) = b.(pageno) in
1442 f w h
1445 let gotopagexy1 wtmode pageno x y =
1446 let _,w1,h1,leftx = getpagedim pageno in
1447 let top = y /. (float h1) in
1448 let left = x /. (float w1) in
1449 let py, w, h = getpageywh pageno in
1450 let wh = state.winh in
1451 let x = left *. (float w) in
1452 let x = leftx + state.x + truncate x in
1453 let sx =
1454 if x < 0 || x >= state.winw
1455 then state.x - x
1456 else state.x
1458 let pdy = truncate (top *. float h) in
1459 let y' = py + pdy in
1460 let dy = y' - state.y in
1461 let sy =
1462 if x != state.x || not (dy > 0 && dy < wh)
1463 then (
1464 if conf.presentation
1465 then
1466 if abs (py - y') > wh
1467 then y'
1468 else py
1469 else y';
1471 else state.y
1473 if state.x != sx || state.y != sy
1474 then (
1475 let x, y =
1476 if wtmode
1477 then (
1478 let ww = state.winw in
1479 let qx = sx / ww
1480 and qy = pdy / wh in
1481 let x = qx * ww
1482 and y = py + qy * wh in
1483 let x = if -x + ww > w1 then -(w1-ww) else x
1484 and y' = if y + wh > state.maxy then state.maxy - wh else y in
1485 let y =
1486 if conf.presentation
1487 then
1488 if abs (py - y') > wh
1489 then y'
1490 else py
1491 else y';
1493 (x, y)
1495 else (sx, sy)
1497 gotoxy_and_clear_text x y;
1499 else gotoxy_and_clear_text state.x state.y;
1502 let gotopagexy wtmode pageno x y =
1503 match state.mode with
1504 | Birdseye _ -> gotopage pageno 0.0
1505 | Textentry _ | View | LinkNav _ -> gotopagexy1 wtmode pageno x y
1508 let getpassword () =
1509 let passcmd = getenvwithdef "LLPP_ASKPASS" conf.passcmd in
1510 if emptystr passcmd
1511 then E.s
1512 else getcmdoutput
1513 (fun s ->
1514 impmsg "error getting password: %s" s;
1515 dolog "%s" s) passcmd;
1518 let pgoto opaque pageno x y =
1519 let pdimno = getpdimno pageno in
1520 let x, y = project opaque pageno pdimno x y in
1521 gotopagexy false pageno x y;
1524 let act cmds =
1525 (* dolog "%S" cmds; *)
1526 let spl = splitatchar cmds ' ' in
1527 let scan s fmt f =
1528 try Scanf.sscanf s fmt f
1529 with exn ->
1530 dolog "error processing '%S': %s" cmds @@ exntos exn;
1531 exit 1
1533 let addoutline outline =
1534 match state.currently with
1535 | Outlining outlines -> state.currently <- Outlining (outline :: outlines)
1536 | Idle -> state.currently <- Outlining [outline]
1537 | Loading _ | Tiling _ ->
1538 dolog "invalid outlining state";
1539 logcurrently state.currently
1541 match spl with
1542 | "clear", "" ->
1543 state.pdims <- [];
1544 state.uioh#infochanged Pdim;
1546 | "clearrects", "" ->
1547 state.rects <- state.rects1;
1548 G.postRedisplay "clearrects";
1550 | "continue", args ->
1551 let n = scan args "%u" (fun n -> n) in
1552 state.pagecount <- n;
1553 begin match state.currently with
1554 | Outlining l ->
1555 state.currently <- Idle;
1556 state.outlines <- Array.of_list (List.rev l)
1557 | Idle | Loading _ | Tiling _ -> ()
1558 end;
1560 let cur, cmds = state.geomcmds in
1561 if emptystr cur
1562 then failwith "umpossible";
1564 begin match List.rev cmds with
1565 | [] ->
1566 state.geomcmds <- E.s, [];
1567 state.throttle <- None;
1568 represent ();
1569 | (s, f) :: rest ->
1570 f ();
1571 state.geomcmds <- s, List.rev rest;
1572 end;
1573 if conf.maxwait = None && not !wtmode
1574 then G.postRedisplay "continue";
1576 | "msg", args ->
1577 showtext ' ' args
1579 | "vmsg", args ->
1580 if conf.verbose
1581 then showtext ' ' args
1583 | "emsg", args ->
1584 Buffer.add_string state.errmsgs args;
1585 state.newerrmsgs <- true;
1586 G.postRedisplay "error message"
1588 | "progress", args ->
1589 let progress, text =
1590 scan args "%f %n"
1591 (fun f pos ->
1592 f, String.sub args pos (String.length args - pos))
1594 state.text <- text;
1595 state.progress <- progress;
1596 G.postRedisplay "progress"
1598 | "firstmatch", args ->
1599 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1600 scan args "%u %d %f %f %f %f %f %f %f %f"
1601 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1602 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1604 let y = (getpagey pageno) + truncate y0 in
1605 let x =
1606 if conf.zoom > 1.0
1607 then state.winw/2
1608 else state.x
1610 addnav ();
1611 gotoxy x y;
1612 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1613 state.rects1 <- [pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)]
1615 | "match", args ->
1616 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1617 scan args "%u %d %f %f %f %f %f %f %f %f"
1618 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1619 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1621 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1622 state.rects1 <-
1623 (pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1625 | "page", args ->
1626 let pageopaques, t = scan args "%s %f" (fun p t -> p, t) in
1627 let pageopaque = ~< pageopaques in
1628 begin match state.currently with
1629 | Loading (l, gen) ->
1630 vlog "page %d took %f sec" l.pageno t;
1631 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1632 begin match state.throttle with
1633 | None ->
1634 let preloadedpages =
1635 if conf.preload
1636 then preloadlayout state.x state.y state.winw state.winh
1637 else state.layout
1639 let evict () =
1640 let set =
1641 List.fold_left (fun s l -> IntSet.add l.pageno s)
1642 IntSet.empty preloadedpages
1644 let evictedpages =
1645 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1646 if not (IntSet.mem pageno set)
1647 then (
1648 wcmd "freepage %s" (~> opaque);
1649 key :: accu
1651 else accu
1652 ) state.pagemap []
1654 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1656 evict ();
1657 state.currently <- Idle;
1658 if gen = state.gen
1659 then (
1660 tilepage l.pageno pageopaque state.layout;
1661 load state.layout;
1662 load preloadedpages;
1663 let visible = pagevisible state.layout l.pageno in
1664 if visible
1665 then (
1666 match state.mode with
1667 | LinkNav (Ltnotready (pageno, dir)) ->
1668 if pageno = l.pageno
1669 then (
1670 let link =
1671 let ld =
1672 if dir = 0
1673 then LDfirstvisible (l.pagex, l.pagey, dir)
1674 else (
1675 if dir > 0 then LDfirst else LDlast
1678 findlink pageopaque ld
1680 match link with
1681 | Lnotfound -> ()
1682 | Lfound n ->
1683 showlinktype (getlink pageopaque n);
1684 state.mode <- LinkNav (Ltexact (l.pageno, n))
1686 | LinkNav (Ltgendir _)
1687 | LinkNav (Ltexact _)
1688 | View
1689 | Birdseye _
1690 | Textentry _ -> ()
1693 if visible && layoutready state.layout
1694 then (
1695 G.postRedisplay "page";
1699 | Some (layout, _, _) ->
1700 state.currently <- Idle;
1701 tilepage l.pageno pageopaque layout;
1702 load state.layout
1703 end;
1705 | Idle | Tiling _ | Outlining _ ->
1706 dolog "Inconsistent loading state";
1707 logcurrently state.currently;
1708 exit 1
1711 | "tile" , args ->
1712 let (x, y, opaques, size, t) =
1713 scan args "%u %u %s %u %f"
1714 (fun x y p size t -> (x, y, p, size, t))
1716 let opaque = ~< opaques in
1717 begin match state.currently with
1718 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1719 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1721 unmappbo opaque;
1722 if tilew != conf.tilew || tileh != conf.tileh
1723 then (
1724 wcmd "freetile %s" (~> opaque);
1725 state.currently <- Idle;
1726 load state.layout;
1728 else (
1729 puttileopaque l col row gen cs angle opaque size t;
1730 state.memused <- state.memused + size;
1731 state.uioh#infochanged Memused;
1732 gctiles ();
1733 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1734 opaque, size) state.tilelru;
1736 let layout =
1737 match state.throttle with
1738 | None -> state.layout
1739 | Some (layout, _, _) -> layout
1742 state.currently <- Idle;
1743 if gen = state.gen
1744 && conf.colorspace = cs
1745 && conf.angle = angle
1746 && tilevisible layout l.pageno x y
1747 then conttiling l.pageno pageopaque;
1749 begin match state.throttle with
1750 | None ->
1751 preload state.layout;
1752 if gen = state.gen
1753 && conf.colorspace = cs
1754 && conf.angle = angle
1755 && tilevisible state.layout l.pageno x y
1756 && (not !wtmode || layoutready state.layout)
1757 then G.postRedisplay "tile nothrottle";
1759 | Some (layout, y, _) ->
1760 let ready = layoutready layout in
1761 if ready
1762 then (
1763 state.y <- y;
1764 state.layout <- layout;
1765 state.throttle <- None;
1766 G.postRedisplay "throttle";
1768 else load layout;
1769 end;
1772 | Idle | Loading _ | Outlining _ ->
1773 dolog "Inconsistent tiling state";
1774 logcurrently state.currently;
1775 exit 1
1778 | "pdim", args ->
1779 let (n, w, h, _) as pdim =
1780 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1782 let pdim =
1783 match conf.fitmodel with
1784 | FitWidth -> pdim
1785 | FitPage | FitProportional ->
1786 match conf.columns with
1787 | Csplit _ -> (n, w, h, 0)
1788 | Csingle _ | Cmulti _ -> pdim
1790 state.pdims <- pdim :: state.pdims;
1791 state.uioh#infochanged Pdim
1793 | "o", args ->
1794 let (l, n, t, h, pos) =
1795 scan args "%u %u %d %u %n"
1796 (fun l n t h pos -> l, n, t, h, pos)
1798 let s = String.sub args pos (String.length args - pos) in
1799 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
1801 | "ou", args ->
1802 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
1803 let s = String.sub args pos len in
1804 let pos2 = pos + len + 1 in
1805 let uri = String.sub args pos2 (String.length args - pos2) in
1806 addoutline (s, l, Ouri uri)
1808 | "on", args ->
1809 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
1810 let s = String.sub args pos (String.length args - pos) in
1811 addoutline (s, l, Onone)
1813 | "a", args ->
1814 let (n, l, t) =
1815 scan args "%u %d %d" (fun n l t -> n, l, t)
1817 state.reprf <- (fun () -> gotopagexy !wtmode n (float l) (float t))
1819 | "info", args ->
1820 let c, v = splitatchar args '\t' in
1821 let s =
1822 if nonemptystr v
1823 then
1824 if c = "Title"
1825 then (
1826 conf.title <- v;
1827 if not !ignoredoctitlte
1828 then 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 if emptystr text && key = Keys.Ascii '-'
1908 then addchar text '-'
1909 else
1910 match key with
1911 | Keys.Ascii ('0'..'9' as c) -> addchar text c
1912 | _ ->
1913 state.text <- "invalid key";
1914 text
1916 TEcont text
1919 let linknact f s =
1920 if nonemptystr s
1921 then (
1922 let n =
1923 let l = String.length s in
1924 let rec loop pos n =
1925 if pos = l
1926 then n
1927 else
1928 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
1929 loop (pos+1) (n*26 + m)
1930 in loop 0 0
1932 let rec loop n = function
1933 | [] -> ()
1934 | l :: rest ->
1935 match getopaque l.pageno with
1936 | None -> loop n rest
1937 | Some opaque ->
1938 let m = getlinkcount opaque in
1939 if n < m
1940 then (
1941 let under = getlink opaque n in
1942 f under
1944 else loop (n-m) rest
1946 loop n state.layout;
1950 let [@warning "-4"] linknentry text = function
1951 | Keys.Ascii c ->
1952 let text = addchar text c in
1953 linknact (fun under -> state.text <- undertext under) text;
1954 TEcont text
1955 | _ ->
1956 state.text <- Printf.sprintf "invalid key";
1957 TEcont text
1960 let [@warning "-4"] textentry text = function
1961 | Keys.Ascii c -> TEcont (addchar text c)
1962 | Keys.Code c -> TEcont (text ^ toutf8 c)
1963 | _ -> TEcont text
1966 let reqlayout angle fitmodel =
1967 match state.throttle with
1968 | None ->
1969 if nogeomcmds state.geomcmds
1970 then state.anchor <- getanchor ();
1971 conf.angle <- angle mod 360;
1972 if conf.angle != 0
1973 then (
1974 match state.mode with
1975 | LinkNav _ -> state.mode <- View
1976 | Birdseye _ | Textentry _ | View -> ()
1978 conf.fitmodel <- fitmodel;
1979 invalidate
1980 "reqlayout"
1981 (fun () ->
1982 wcmd "reqlayout %d %d %d"
1983 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
1985 | _ -> ()
1988 let settrim trimmargins trimfuzz =
1989 if nogeomcmds state.geomcmds
1990 then state.anchor <- getanchor ();
1991 conf.trimmargins <- trimmargins;
1992 conf.trimfuzz <- trimfuzz;
1993 let x0, y0, x1, y1 = trimfuzz in
1994 invalidate
1995 "settrim" (fun () ->
1996 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
1997 flushpages ();
2000 let setzoom zoom =
2001 match state.throttle with
2002 | None ->
2003 let zoom = max 0.0001 zoom in
2004 if zoom <> conf.zoom
2005 then (
2006 state.prevzoom <- (conf.zoom, state.x);
2007 conf.zoom <- zoom;
2008 reshape state.winw state.winh;
2009 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2012 | Some (layout, y, started) ->
2013 let time =
2014 match conf.maxwait with
2015 | None -> 0.0
2016 | Some t -> t
2018 let dt = now () -. started in
2019 if dt > time
2020 then (
2021 state.y <- y;
2022 load layout;
2026 let pivotzoom ?(vw=min state.w state.winw)
2027 ?(vh=min (state.maxy-state.y) state.winh)
2028 ?(x=vw/2) ?(y=vh/2) zoom =
2029 let w = float state.w /. zoom in
2030 let hw = w /. 2.0 in
2031 let ratio = float vh /. float vw in
2032 let hh = hw *. ratio in
2033 let x0 = float x -. hw
2034 and y0 = float y -. hh in
2035 gotoxy (state.x - truncate x0) (state.y + truncate y0);
2036 setzoom zoom;
2039 let pivotzoom ?vw ?vh ?x ?y zoom =
2040 if nogeomcmds state.geomcmds
2041 then
2042 if zoom > 1.0
2043 then pivotzoom ?vw ?vh ?x ?y zoom
2044 else setzoom zoom
2047 let setcolumns mode columns coverA coverB =
2048 state.prevcolumns <- Some (conf.columns, conf.zoom);
2049 if columns < 0
2050 then (
2051 if isbirdseye mode
2052 then impmsg "split mode doesn't work in bird's eye"
2053 else (
2054 conf.columns <- Csplit (-columns, E.a);
2055 state.x <- 0;
2056 conf.zoom <- 1.0;
2059 else (
2060 if columns < 2
2061 then (
2062 conf.columns <- Csingle E.a;
2063 state.x <- 0;
2064 setzoom 1.0;
2066 else (
2067 conf.columns <- Cmulti ((columns, coverA, coverB), E.a);
2068 conf.zoom <- 1.0;
2071 reshape state.winw state.winh;
2074 let resetmstate () =
2075 state.mstate <- Mnone;
2076 Wsi.setcursor Wsi.CURSOR_INHERIT;
2079 let enterbirdseye () =
2080 let zoom = float conf.thumbw /. float state.winw in
2081 let birdseyepageno =
2082 let cy = state.winh / 2 in
2083 let fold = function
2084 | [] -> 0
2085 | l :: rest ->
2086 let rec fold best = function
2087 | [] -> best.pageno
2088 | l :: rest ->
2089 let d = cy - (l.pagedispy + l.pagevh/2)
2090 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2091 if abs d < abs dbest
2092 then fold l rest
2093 else best.pageno
2094 in fold l rest
2096 fold state.layout
2098 state.mode <-
2099 Birdseye (
2100 { conf with zoom = conf.zoom },
2101 state.x, birdseyepageno, -1, getanchor ()
2103 resetmstate ();
2104 conf.zoom <- zoom;
2105 conf.presentation <- false;
2106 conf.interpagespace <- 10;
2107 conf.hlinks <- false;
2108 conf.fitmodel <- FitPage;
2109 state.x <- 0;
2110 conf.maxwait <- None;
2111 conf.columns <- (
2112 match conf.beyecolumns with
2113 | Some c ->
2114 conf.zoom <- 1.0;
2115 Cmulti ((c, 0, 0), E.a)
2116 | None -> Csingle E.a
2118 if conf.verbose
2119 then
2120 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2121 (100.0*.zoom)
2122 else
2123 state.text <- E.s
2125 reshape state.winw state.winh;
2128 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2129 state.mode <- View;
2130 conf.zoom <- c.zoom;
2131 conf.presentation <- c.presentation;
2132 conf.interpagespace <- c.interpagespace;
2133 conf.maxwait <- c.maxwait;
2134 conf.hlinks <- c.hlinks;
2135 conf.fitmodel <- c.fitmodel;
2136 conf.beyecolumns <- (
2137 match conf.columns with
2138 | Cmulti ((c, _, _), _) -> Some c
2139 | Csingle _ -> None
2140 | Csplit _ -> failwith "leaving bird's eye split mode"
2142 conf.columns <- (
2143 match c.columns with
2144 | Cmulti (c, _) -> Cmulti (c, E.a)
2145 | Csingle _ -> Csingle E.a
2146 | Csplit (c, _) -> Csplit (c, E.a)
2148 if conf.verbose
2149 then
2150 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2151 (100.0*.conf.zoom)
2153 reshape state.winw state.winh;
2154 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2155 state.x <- leftx;
2158 let togglebirdseye () =
2159 match state.mode with
2160 | Birdseye vals -> leavebirdseye vals true
2161 | View -> enterbirdseye ()
2162 | Textentry _ | LinkNav _ -> ()
2165 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2166 let pageno = max 0 (pageno - incr) in
2167 let rec loop = function
2168 | [] -> gotopage1 pageno 0
2169 | l :: _ when l.pageno = pageno ->
2170 if l.pagedispy >= 0 && l.pagey = 0
2171 then G.postRedisplay "upbirdseye"
2172 else gotopage1 pageno 0
2173 | _ :: rest -> loop rest
2175 loop state.layout;
2176 state.text <- E.s;
2177 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2180 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2181 let pageno = min (state.pagecount - 1) (pageno + incr) in
2182 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2183 let rec loop = function
2184 | [] ->
2185 let y, h = getpageyh pageno in
2186 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2187 gotoxy state.x (clamp dy)
2188 | l :: _ when l.pageno = pageno ->
2189 if l.pagevh != l.pageh
2190 then gotoxy state.x (clamp (l.pageh - l.pagevh + conf.interpagespace))
2191 else G.postRedisplay "downbirdseye"
2192 | _ :: rest -> loop rest
2194 loop state.layout;
2195 state.text <- E.s;
2198 let [@warning "-4"] optentry mode _ key =
2199 let btos b = if b then "on" else "off" in
2200 match key with
2201 | Keys.Ascii 's' ->
2202 let ondone s =
2203 try conf.scrollstep <- int_of_string s with exn ->
2204 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2206 TEswitch ("scroll step: ", E.s, None, intentry, ondone, true)
2208 | Keys.Ascii 'A' ->
2209 let ondone s =
2211 conf.autoscrollstep <- boundastep state.winh (int_of_string s);
2212 if state.autoscroll <> None
2213 then state.autoscroll <- Some conf.autoscrollstep
2214 with exn ->
2215 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2217 TEswitch ("auto scroll step: ", E.s, None, intentry, ondone, true)
2219 | Keys.Ascii 'C' ->
2220 let ondone s =
2222 let n, a, b = multicolumns_of_string s in
2223 setcolumns mode n a b;
2224 with exn ->
2225 state.text <- Printf.sprintf "bad columns `%s': %s" s @@ exntos exn
2227 TEswitch ("columns: ", E.s, None, textentry, ondone, true)
2229 | Keys.Ascii 'Z' ->
2230 let ondone s =
2232 let zoom = float (int_of_string s) /. 100.0 in
2233 pivotzoom zoom
2234 with exn ->
2235 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2237 TEswitch ("zoom: ", E.s, None, intentry, ondone, true)
2239 | Keys.Ascii 't' ->
2240 let ondone s =
2242 conf.thumbw <- bound (int_of_string s) 2 4096;
2243 state.text <-
2244 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2245 begin match mode with
2246 | Birdseye beye ->
2247 leavebirdseye beye false;
2248 enterbirdseye ();
2249 | Textentry _ | View | LinkNav _ -> ();
2251 with exn ->
2252 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2254 TEswitch ("thumbnail width: ", E.s, None, intentry, ondone, true)
2256 | Keys.Ascii 'R' ->
2257 let ondone s =
2258 match int_of_string s with
2259 | angle -> reqlayout angle conf.fitmodel
2260 | exception exn ->
2261 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2263 TEswitch ("rotation: ", E.s, None, intentry, ondone, true)
2265 | Keys.Ascii 'i' ->
2266 conf.icase <- not conf.icase;
2267 TEdone ("case insensitive search " ^ (btos conf.icase))
2269 | Keys.Ascii 'p' ->
2270 conf.preload <- not conf.preload;
2271 gotoxy state.x state.y;
2272 TEdone ("preload " ^ (btos conf.preload))
2274 | Keys.Ascii 'v' ->
2275 conf.verbose <- not conf.verbose;
2276 TEdone ("verbose " ^ (btos conf.verbose))
2278 | Keys.Ascii 'd' ->
2279 conf.debug <- not conf.debug;
2280 TEdone ("debug " ^ (btos conf.debug))
2282 | Keys.Ascii 'h' ->
2283 conf.maxhfit <- not conf.maxhfit;
2284 state.maxy <- calcheight ();
2285 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2287 | Keys.Ascii 'c' ->
2288 conf.crophack <- not conf.crophack;
2289 TEdone ("crophack " ^ btos conf.crophack)
2291 | Keys.Ascii 'a' ->
2292 let s =
2293 match conf.maxwait with
2294 | None ->
2295 conf.maxwait <- Some infinity;
2296 "always wait for page to complete"
2297 | Some _ ->
2298 conf.maxwait <- None;
2299 "show placeholder if page is not ready"
2301 TEdone s
2303 | Keys.Ascii 'f' ->
2304 conf.underinfo <- not conf.underinfo;
2305 TEdone ("underinfo " ^ btos conf.underinfo)
2307 | Keys.Ascii 'P' ->
2308 conf.savebmarks <- not conf.savebmarks;
2309 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2311 | Keys.Ascii 'S' ->
2312 let ondone s =
2314 let pageno, py =
2315 match state.layout with
2316 | [] -> 0, 0
2317 | l :: _ ->
2318 l.pageno, l.pagey
2320 conf.interpagespace <- int_of_string s;
2321 docolumns conf.columns;
2322 state.maxy <- calcheight ();
2323 let y = getpagey pageno in
2324 gotoxy state.x (y + py)
2325 with exn ->
2326 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2328 TEswitch ("vertical margin: ", E.s, None, intentry, ondone, true)
2330 | Keys.Ascii 'l' ->
2331 let fm =
2332 match conf.fitmodel with
2333 | FitProportional -> FitWidth
2334 | FitWidth | FitPage -> FitProportional
2336 reqlayout conf.angle fm;
2337 TEdone ("proportional display " ^ btos (fm == FitProportional))
2339 | Keys.Ascii 'T' ->
2340 settrim (not conf.trimmargins) conf.trimfuzz;
2341 TEdone ("trim margins " ^ btos conf.trimmargins)
2343 | Keys.Ascii 'I' ->
2344 conf.invert <- not conf.invert;
2345 TEdone ("invert colors " ^ btos conf.invert)
2347 | Keys.Ascii 'x' ->
2348 let ondone s =
2349 cbput state.hists.sel s;
2350 conf.selcmd <- s;
2352 TEswitch ("selection command: ", E.s, Some (onhist state.hists.sel),
2353 textentry, ondone, true)
2355 | Keys.Ascii 'M' ->
2356 if conf.pax == None
2357 then conf.pax <- Some 0.0
2358 else conf.pax <- None;
2359 TEdone ("PAX " ^ btos (conf.pax != None))
2361 | (Keys.Ascii c) ->
2362 state.text <- Printf.sprintf "bad option %d `%c'"
2363 (Char.code c) c;
2364 TEstop
2366 | _ ->
2367 TEcont state.text
2370 class type lvsource =
2371 object
2372 method getitemcount : int
2373 method getitem : int -> (string * int)
2374 method hasaction : int -> bool
2375 method exit : uioh:uioh ->
2376 cancel:bool ->
2377 active:int ->
2378 first:int ->
2379 pan:int ->
2380 uioh option
2381 method getactive : int
2382 method getfirst : int
2383 method getpan : int
2384 method getminfo : (int * int) array
2385 end;;
2387 class virtual lvsourcebase = object
2388 val mutable m_active = 0
2389 val mutable m_first = 0
2390 val mutable m_pan = 0
2391 method getactive = m_active
2392 method getfirst = m_first
2393 method getpan = m_pan
2394 method getminfo : (int * int) array = E.a
2395 end;;
2397 let [@warning "-4"]
2398 textentrykeyboard
2399 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2400 state.text <- E.s;
2401 let enttext te =
2402 state.mode <- Textentry (te, onleave);
2403 enttext ();
2404 G.postRedisplay "textentrykeyboard enttext";
2406 let histaction cmd =
2407 match opthist with
2408 | None -> ()
2409 | Some (action, _) ->
2410 state.mode <-
2411 Textentry (
2412 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
2414 G.postRedisplay "textentry histaction"
2416 let open Keys in
2417 let kt = Wsi.kc2kt key in
2418 match kt with
2419 | Backspace ->
2420 if emptystr text && cancelonempty
2421 then (
2422 onleave Cancel;
2423 G.postRedisplay "textentrykeyboard after cancel";
2425 else
2426 let s = withoutlastutf8 text in
2427 enttext (c, s, opthist, onkey, ondone, cancelonempty)
2429 | Enter ->
2430 ondone text;
2431 onleave Confirm;
2432 G.postRedisplay "textentrykeyboard after confirm"
2434 | Up -> histaction HCprev
2435 | Down -> histaction HCnext
2436 | Home -> histaction HCfirst
2437 | End -> histaction HClast
2439 | Escape ->
2440 if emptystr text
2441 then (
2442 begin match opthist with
2443 | None -> ()
2444 | Some (_, onhistcancel) -> onhistcancel ()
2445 end;
2446 onleave Cancel;
2447 state.text <- E.s;
2448 G.postRedisplay "textentrykeyboard after cancel2"
2450 else (
2451 enttext (c, E.s, opthist, onkey, ondone, cancelonempty)
2454 | Delete -> ()
2456 | Code _ | Ascii _ ->
2457 begin match onkey text kt with
2458 | TEdone text ->
2459 ondone text;
2460 onleave Confirm;
2461 G.postRedisplay "textentrykeyboard after confirm2";
2463 | TEcont text ->
2464 enttext (c, text, opthist, onkey, ondone, cancelonempty);
2466 | TEstop ->
2467 onleave Cancel;
2468 G.postRedisplay "textentrykeyboard after cancel3"
2470 | TEswitch te ->
2471 state.mode <- Textentry (te, onleave);
2472 G.postRedisplay "textentrykeyboard switch";
2474 | _ -> vlog "unhandled key"
2477 let firstof first active =
2478 if first > active || abs (first - active) > fstate.maxrows - 1
2479 then max 0 (active - (fstate.maxrows/2))
2480 else first
2483 let calcfirst first active =
2484 if active > first
2485 then
2486 let rows = active - first in
2487 if rows > fstate.maxrows then active - fstate.maxrows else first
2488 else active
2491 let scrollph y maxy =
2492 let sh = float (maxy + state.winh) /. float state.winh in
2493 let sh = float state.winh /. sh in
2494 let sh = max sh (float conf.scrollh) in
2496 let percent = float y /. float maxy in
2497 let position = (float state.winh -. sh) *. percent in
2499 let position =
2500 if position +. sh > float state.winh
2501 then float state.winh -. sh
2502 else position
2504 position, sh;
2507 let adderrmsg src msg =
2508 Buffer.add_string state.errmsgs msg;
2509 state.newerrmsgs <- true;
2510 G.postRedisplay src
2513 let adderrfmt src fmt =
2514 Format.ksprintf (fun s -> adderrmsg src s) fmt;
2517 let coe s = (s :> uioh);;
2519 class listview ~zebra ~helpmode ~(source:lvsource) ~trusted ~modehash =
2520 object (self)
2521 val m_pan = source#getpan
2522 val m_first = source#getfirst
2523 val m_active = source#getactive
2524 val m_qsearch = E.s
2525 val m_prev_uioh = state.uioh
2527 method private elemunder y =
2528 if y < 0
2529 then None
2530 else
2531 let n = y / (fstate.fontsize+1) in
2532 if m_first + n < source#getitemcount
2533 then (
2534 if source#hasaction (m_first + n)
2535 then Some (m_first + n)
2536 else None
2538 else None
2540 method display =
2541 Gl.enable `blend;
2542 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
2543 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2544 filledrect 0. 0. (float state.winw) (float state.winh);
2545 GlDraw.color (1., 1., 1.);
2546 Gl.enable `texture_2d;
2547 let fs = fstate.fontsize in
2548 let nfs = fs + 1 in
2549 let hw = state.winw/3 in
2550 let ww = fstate.wwidth in
2551 let tabw = 17.0*.ww in
2552 let itemcount = source#getitemcount in
2553 let minfo = source#getminfo in
2554 if conf.leftscroll
2555 then (
2556 GlMat.push ();
2557 GlMat.translate ~x:(float conf.scrollbw) ();
2559 let x0 = 0.0 and x1 = float (state.winw - conf.scrollbw - 1) in
2560 let rec loop row =
2561 if (row - m_first) > fstate.maxrows
2562 then ()
2563 else (
2564 if row >= 0 && row < itemcount
2565 then (
2566 let (s, level) = source#getitem row in
2567 let y = (row - m_first) * nfs in
2568 let x = 5.0 +. (float (level + m_pan)) *. ww in
2569 if helpmode
2570 then GlDraw.color
2571 (let c = if row land 1 = 0 then 1.0 else 0.92 in (c,c,c));
2573 if row = m_active
2574 then (
2575 Gl.disable `texture_2d;
2576 let alpha = if source#hasaction row then 0.9 else 0.3 in
2577 GlDraw.color (1., 1., 1.) ~alpha;
2578 linerect (x0 +. 1.) (float (y + 1)) (x1) (float (y + fs + 3));
2579 Gl.enable `texture_2d;
2581 let c =
2582 if zebra && row land 1 = 1
2583 then 0.8
2584 else 1.0
2586 GlDraw.color (c,c,c);
2587 let drawtabularstring s =
2588 let drawstr x s =
2589 let x' = truncate (x0 +. x) in
2590 let s1, s2 = splitatchar s '\000' in
2591 if emptystr s2
2592 then drawstring1 fs x' (y+nfs) s
2593 else
2594 let rec e s =
2595 if emptystr s
2596 then s
2597 else
2598 let s' = withoutlastutf8 s in
2599 let s = s' ^ UniSyms.ellipsis in
2600 let w = measurestr fs s in
2601 if float x' +. w +. ww < float (hw + x')
2602 then s
2603 else e s'
2605 let s1 =
2606 if float x' +. ww +. measurestr fs s1 > float (hw + x')
2607 then e s1
2608 else s1
2610 ignore (drawstring1 fs x' (y+nfs) s1);
2611 drawstring1 fs (hw + x') (y+nfs) s2
2613 if trusted
2614 then
2615 let x = if helpmode && row > 0 then x +. ww else x in
2616 let s1, s2 = splitatchar s '\t' in
2617 if nonemptystr s2
2618 then
2619 let nx = drawstr x s1 in
2620 let sw = nx -. x in
2621 let x = x +. (max tabw sw) in
2622 drawstr x s2
2623 else
2624 let len = String.length s - 2 in
2625 if len > 0 && s.[0] = '\xc2' && s.[1] = '\xb7'
2626 then
2627 let s = String.sub s 2 len in
2628 let x = if not helpmode then x +. ww else x in
2629 GlDraw.color (1.2, 1.2, 1.2);
2630 let vinc = drawstring1 (fs+fs/4)
2631 (truncate (x -. ww)) (y+nfs) s in
2632 GlDraw.color (1., 1., 1.);
2633 vinc +. (float fs *. 0.8)
2634 else
2635 drawstr x s
2636 else
2637 drawstr x s
2639 ignore (drawtabularstring s);
2640 loop (row+1)
2644 loop m_first;
2645 GlDraw.color (1.0, 1.0, 1.0) ~alpha:0.5;
2646 let xadj = 5.0 in
2647 let rec loop row =
2648 if (row - m_first) <= fstate.maxrows
2649 then
2650 if row >= 0 && row < itemcount
2651 then (
2652 let (s, level) = source#getitem row in
2653 let pos0 = nindex s '\000' in
2654 let y = (row - m_first) * nfs in
2655 let x = float (level + m_pan) *. ww in
2656 let (first, last) = minfo.(row) in
2657 let prefix =
2658 if pos0 > 0 && first > pos0
2659 then String.sub s (pos0+1) (first-pos0-1)
2660 else String.sub s 0 first
2662 let suffix = String.sub s first (last - first) in
2663 let w1 = measurestr fstate.fontsize prefix in
2664 let w2 = measurestr fstate.fontsize suffix in
2665 let x = x +. if conf.leftscroll then xadj else 5.0 in
2666 let x = if pos0 > 0 && first > pos0 then x +. float hw else x in
2667 let x0 = x +. w1
2668 and y0 = float (y+2) in
2669 let x1 = x0 +. w2
2670 and y1 = float (y+fs+3) in
2671 filledrect x0 y0 x1 y1;
2672 loop (row+1)
2675 Gl.disable `texture_2d;
2676 if Array.length minfo > 0 then loop m_first;
2677 Gl.disable `blend;
2678 if conf.leftscroll
2679 then GlMat.pop ()
2681 method updownlevel incr =
2682 let len = source#getitemcount in
2683 let curlevel =
2684 if m_active >= 0 && m_active < len
2685 then snd (source#getitem m_active)
2686 else -1
2688 let rec flow i =
2689 if i = len then i-1 else if i = -1 then 0 else
2690 let _, l = source#getitem i in
2691 if l != curlevel then i else flow (i+incr)
2693 let active = flow m_active in
2694 let first = calcfirst m_first active in
2695 G.postRedisplay "outline updownlevel";
2696 {< m_active = active; m_first = first >}
2698 method private key1 key mask =
2699 let set1 active first qsearch =
2700 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2702 let search active pattern incr =
2703 let active = if active = -1 then m_first else active in
2704 let dosearch re =
2705 let rec loop n =
2706 if n >= 0 && n < source#getitemcount
2707 then (
2708 let s, _ = source#getitem n in
2709 match Str.search_forward re s 0 with
2710 | exception Not_found -> loop (n + incr)
2711 | _ -> Some n
2713 else None
2715 loop active
2717 let qpat = Str.quote pattern in
2718 match Str.regexp_case_fold qpat with
2719 | s -> dosearch s
2720 | exception exn ->
2721 adderrfmt "listview key1" "regexp_case_fold for `%S' failed: %S\n"
2722 qpat @@ Printexc.to_string exn;
2723 None
2725 let itemcount = source#getitemcount in
2726 let find start incr =
2727 let rec find i =
2728 if i = -1 || i = itemcount
2729 then -1
2730 else (
2731 if source#hasaction i
2732 then i
2733 else find (i + incr)
2736 find start
2738 let set active first =
2739 let first = bound first 0 (itemcount - fstate.maxrows) in
2740 state.text <- E.s;
2741 coe {< m_active = active; m_first = first; m_qsearch = E.s >}
2743 let navigate incr =
2744 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2745 let active, first =
2746 let incr1 = if incr > 0 then 1 else -1 in
2747 if isvisible m_first m_active
2748 then
2749 let next =
2750 let next = m_active + incr in
2751 let next =
2752 if next < 0 || next >= itemcount
2753 then -1
2754 else find next incr1
2756 if abs (m_active - next) > fstate.maxrows
2757 then -1
2758 else next
2760 if next = -1
2761 then
2762 let first = m_first + incr in
2763 let first = bound first 0 (itemcount - fstate.maxrows) in
2764 let next =
2765 let next = m_active + incr in
2766 let next = bound next 0 (itemcount - 1) in
2767 find next ~-incr1
2769 let active =
2770 if next = -1
2771 then m_active
2772 else (
2773 if isvisible first next
2774 then next
2775 else m_active
2778 active, first
2779 else
2780 let first = min next m_first in
2781 let first =
2782 if abs (next - first) > fstate.maxrows
2783 then first + incr
2784 else first
2786 next, first
2787 else
2788 let first = m_first + incr in
2789 let first = bound first 0 (itemcount - 1) in
2790 let active =
2791 let next = m_active + incr in
2792 let next = bound next 0 (itemcount - 1) in
2793 let next = find next incr1 in
2794 let active =
2795 if next = -1 || abs (m_active - first) > fstate.maxrows
2796 then (
2797 let active = if m_active = -1 then next else m_active in
2798 active
2800 else next
2802 if isvisible first active
2803 then active
2804 else -1
2806 active, first
2808 G.postRedisplay "listview navigate";
2809 set active first;
2811 let open Keys in
2812 let kt = Wsi.kc2kt key in
2813 match [@warning "-4"] kt with
2814 | Ascii (('r'|'s') as c) when Wsi.withctrl mask ->
2815 let incr = if c = 'r' then -1 else 1 in
2816 let active, first =
2817 match search (m_active + incr) m_qsearch incr with
2818 | None ->
2819 state.text <- m_qsearch ^ " [not found]";
2820 m_active, m_first
2821 | Some active ->
2822 state.text <- m_qsearch;
2823 active, firstof m_first active
2825 G.postRedisplay "listview ctrl-r/s";
2826 set1 active first m_qsearch;
2828 | Insert when Wsi.withctrl mask ->
2829 if m_active >= 0 && m_active < source#getitemcount
2830 then (
2831 let s, _ = source#getitem m_active in
2832 selstring s;
2834 coe self
2836 | Backspace ->
2837 if emptystr m_qsearch
2838 then coe self
2839 else (
2840 let qsearch = withoutlastutf8 m_qsearch in
2841 if emptystr qsearch
2842 then (
2843 state.text <- E.s;
2844 G.postRedisplay "listview empty qsearch";
2845 set1 m_active m_first E.s;
2847 else
2848 let active, first =
2849 match search m_active qsearch ~-1 with
2850 | None ->
2851 state.text <- qsearch ^ " [not found]";
2852 m_active, m_first
2853 | Some active ->
2854 state.text <- qsearch;
2855 active, firstof m_first active
2857 G.postRedisplay "listview backspace qsearch";
2858 set1 active first qsearch
2861 | Ascii _ | Code _ ->
2862 let utf8 =
2863 match [@warning "-8"] kt with
2864 | Ascii c -> String.make 1 c
2865 | Code code -> toutf8 code
2867 let pattern = m_qsearch ^ utf8 in
2868 let active, first =
2869 match search m_active pattern 1 with
2870 | None ->
2871 state.text <- pattern ^ " [not found]";
2872 m_active, m_first
2873 | Some active ->
2874 state.text <- pattern;
2875 active, firstof m_first active
2877 G.postRedisplay "listview qsearch add";
2878 set1 active first pattern;
2880 | Escape ->
2881 state.text <- E.s;
2882 if emptystr m_qsearch
2883 then (
2884 G.postRedisplay "list view escape";
2885 let mx, my = state.mpos in
2886 updateunder mx my;
2887 match source#exit ~uioh:(coe self) ~cancel:true ~active:m_active
2888 ~first:m_first ~pan:m_pan with
2889 | None -> m_prev_uioh
2890 | Some uioh -> uioh
2892 else (
2893 G.postRedisplay "list view kill qsearch";
2894 coe {< m_qsearch = E.s >}
2897 | Enter ->
2898 state.text <- E.s;
2899 let self = {< m_qsearch = E.s >} in
2900 let opt =
2901 G.postRedisplay "listview enter";
2902 let cancel = not (m_active >= 0 && m_active < source#getitemcount) in
2903 source#exit ~uioh:(coe self) ~cancel
2904 ~active:m_active ~first:m_first ~pan:m_pan;
2906 begin match opt with
2907 | None -> m_prev_uioh
2908 | Some uioh -> uioh
2911 | Delete ->
2912 coe self
2914 | Up -> navigate ~-1
2915 | Down -> navigate 1
2916 | Prior -> navigate ~-(fstate.maxrows)
2917 | Next -> navigate fstate.maxrows
2919 | Right ->
2920 state.text <- E.s;
2921 G.postRedisplay "listview right";
2922 coe {< m_pan = m_pan - 1 >}
2924 | Left ->
2925 state.text <- E.s;
2926 G.postRedisplay "listview left";
2927 coe {< m_pan = m_pan + 1 >}
2929 | Home ->
2930 let active = find 0 1 in
2931 G.postRedisplay "listview home";
2932 set active 0;
2934 | End ->
2935 let first = max 0 (itemcount - fstate.maxrows) in
2936 let active = find (itemcount - 1) ~-1 in
2937 G.postRedisplay "listview end";
2938 set active first;
2940 | _ -> coe self
2942 method key key mask =
2943 match state.mode with
2944 | Textentry te ->
2945 textentrykeyboard key mask te;
2946 coe self
2947 | Birdseye _ | View | LinkNav _ -> self#key1 key mask
2949 method button button down x y _ =
2950 let opt =
2951 match button with
2952 | 1 when vscrollhit x ->
2953 G.postRedisplay "listview scroll";
2954 if down
2955 then
2956 let _, position, sh = self#scrollph in
2957 if y > truncate position && y < truncate (position +. sh)
2958 then (
2959 state.mstate <- Mscrolly;
2960 Some (coe self)
2962 else
2963 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
2964 let first = truncate (s *. float source#getitemcount) in
2965 let first = min source#getitemcount first in
2966 Some (coe {< m_first = first; m_active = first >})
2967 else (
2968 state.mstate <- Mnone;
2969 Some (coe self);
2971 | 1 when down ->
2972 begin match self#elemunder y with
2973 | Some n ->
2974 G.postRedisplay "listview click";
2975 source#exit ~uioh:(coe {< m_active = n >})
2976 ~cancel:false ~active:n ~first:m_first ~pan:m_pan
2977 | _ ->
2978 Some (coe self)
2980 | n when (n == 4 || n == 5) && not down ->
2981 let len = source#getitemcount in
2982 let first =
2983 if n = 5 && m_first + fstate.maxrows >= len
2984 then
2985 m_first
2986 else
2987 let first = m_first + (if n == 4 then -1 else 1) in
2988 bound first 0 (len - 1)
2990 G.postRedisplay "listview wheel";
2991 Some (coe {< m_first = first >})
2992 | n when (n = 6 || n = 7) && not down ->
2993 let inc = if n = 7 then -1 else 1 in
2994 G.postRedisplay "listview hwheel";
2995 Some (coe {< m_pan = m_pan + inc >})
2996 | _ ->
2997 Some (coe self)
2999 match opt with
3000 | None -> m_prev_uioh
3001 | Some uioh -> uioh
3003 method multiclick _ x y = self#button 1 true x y
3005 method motion _ y =
3006 match state.mstate with
3007 | Mscrolly ->
3008 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3009 let first = truncate (s *. float source#getitemcount) in
3010 let first = min source#getitemcount first in
3011 G.postRedisplay "listview motion";
3012 coe {< m_first = first; m_active = first >}
3013 | Msel _
3014 | Mpan _
3015 | Mscrollx
3016 | Mzoom _
3017 | Mzoomrect _
3018 | Mnone -> coe self
3020 method pmotion x y =
3021 if x < state.winw - conf.scrollbw
3022 then
3023 let n =
3024 match self#elemunder y with
3025 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3026 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3028 let o =
3029 if n != m_active
3030 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3031 else self
3033 coe o
3034 else (
3035 Wsi.setcursor Wsi.CURSOR_INHERIT;
3036 coe self
3039 method infochanged _ = ()
3041 method scrollpw = (0, 0.0, 0.0)
3042 method scrollph =
3043 let nfs = fstate.fontsize + 1 in
3044 let y = m_first * nfs in
3045 let itemcount = source#getitemcount in
3046 let maxi = max 0 (itemcount - fstate.maxrows) in
3047 let maxy = maxi * nfs in
3048 let p, h = scrollph y maxy in
3049 conf.scrollbw, p, h
3051 method modehash = modehash
3052 method eformsgs = false
3053 method alwaysscrolly = true
3054 method scroll _ dy =
3055 let self =
3056 if dy != 0 then begin
3057 let len = source#getitemcount in
3058 let first =
3059 if dy > 0 && m_first + fstate.maxrows >= len
3060 then
3061 m_first
3062 else
3063 let first = m_first + dy / 10 in
3064 bound first 0 (len - 1)
3066 G.postRedisplay "listview wheel";
3067 {< m_first = first >}
3068 end else
3069 self
3071 coe self
3073 method zoom _ _ _ = ()
3074 end;;
3076 class outlinelistview ~zebra ~source =
3077 let settext autonarrow s =
3078 if autonarrow
3079 then
3080 let ss = source#statestr in
3081 state.text <-
3082 if emptystr ss
3083 then "[" ^ s ^ "]"
3084 else "{" ^ ss ^ "} [" ^ s ^ "]"
3085 else state.text <- s
3087 object (self)
3088 inherit listview
3089 ~zebra
3090 ~helpmode:false
3091 ~source:(source :> lvsource)
3092 ~trusted:false
3093 ~modehash:(findkeyhash conf "outline")
3094 as super
3096 val m_autonarrow = false
3098 method! key key mask =
3099 let maxrows =
3100 if emptystr state.text
3101 then fstate.maxrows
3102 else fstate.maxrows - 2
3104 let calcfirst first active =
3105 if active > first
3106 then
3107 let rows = active - first in
3108 if rows > maxrows then active - maxrows else first
3109 else active
3111 let navigate incr =
3112 let active = m_active + incr in
3113 let active = bound active 0 (source#getitemcount - 1) in
3114 let first = calcfirst m_first active in
3115 G.postRedisplay "outline navigate";
3116 coe {< m_active = active; m_first = first >}
3118 let navscroll first =
3119 let active =
3120 let dist = m_active - first in
3121 if dist < 0
3122 then first
3123 else (
3124 if dist < maxrows
3125 then m_active
3126 else first + maxrows
3129 G.postRedisplay "outline navscroll";
3130 coe {< m_first = first; m_active = active >}
3132 let ctrl = Wsi.withctrl mask in
3133 let open Keys in
3134 match Wsi.kc2kt key with
3135 | Ascii 'a' when ctrl ->
3136 let text =
3137 if m_autonarrow
3138 then (source#denarrow; E.s)
3139 else (
3140 let pattern = source#renarrow in
3141 if nonemptystr m_qsearch
3142 then (source#narrow m_qsearch; m_qsearch)
3143 else pattern
3146 settext (not m_autonarrow) text;
3147 G.postRedisplay "toggle auto narrowing";
3148 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
3150 | Ascii '/' when emptystr m_qsearch && not m_autonarrow ->
3151 settext true E.s;
3152 G.postRedisplay "toggle auto narrowing";
3153 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
3155 | Ascii 'n' when ctrl ->
3156 source#narrow m_qsearch;
3157 if not m_autonarrow
3158 then source#add_narrow_pattern m_qsearch;
3159 G.postRedisplay "outline ctrl-n";
3160 coe {< m_first = 0; m_active = 0 >}
3162 | Ascii 'S' when ctrl ->
3163 let active = source#calcactive (getanchor ()) in
3164 let first = firstof m_first active in
3165 G.postRedisplay "outline ctrl-s";
3166 coe {< m_first = first; m_active = active >}
3168 | Ascii 'u' when ctrl ->
3169 G.postRedisplay "outline ctrl-u";
3170 if m_autonarrow && nonemptystr m_qsearch
3171 then (
3172 ignore (source#renarrow);
3173 settext m_autonarrow E.s;
3174 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3176 else (
3177 source#del_narrow_pattern;
3178 let pattern = source#renarrow in
3179 let text =
3180 if emptystr pattern then E.s else "Narrowed to " ^ pattern
3182 settext m_autonarrow text;
3183 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3186 | Ascii 'l' when ctrl ->
3187 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3188 G.postRedisplay "outline ctrl-l";
3189 coe {< m_first = first >}
3191 | Ascii '\t' when m_autonarrow ->
3192 if nonemptystr m_qsearch
3193 then (
3194 G.postRedisplay "outline list view tab";
3195 source#add_narrow_pattern m_qsearch;
3196 settext true E.s;
3197 coe {< m_qsearch = E.s >}
3199 else coe self
3201 | Escape when m_autonarrow ->
3202 if nonemptystr m_qsearch
3203 then source#add_narrow_pattern m_qsearch;
3204 super#key key mask
3206 | Enter when m_autonarrow ->
3207 if nonemptystr m_qsearch
3208 then source#add_narrow_pattern m_qsearch;
3209 super#key key mask
3211 | (Ascii _ | Code _) when m_autonarrow ->
3212 let pattern = m_qsearch ^ toutf8 key in
3213 G.postRedisplay "outlinelistview autonarrow add";
3214 source#narrow pattern;
3215 settext true pattern;
3216 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3218 | Backspace when m_autonarrow ->
3219 if emptystr m_qsearch
3220 then coe self
3221 else
3222 let pattern = withoutlastutf8 m_qsearch in
3223 G.postRedisplay "outlinelistview autonarrow backspace";
3224 ignore (source#renarrow);
3225 source#narrow pattern;
3226 settext true pattern;
3227 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3229 | Up when ctrl ->
3230 navscroll (max 0 (m_first - 1))
3232 | Down when ctrl ->
3233 navscroll (min (source#getitemcount - 1) (m_first + 1))
3235 | Up -> navigate ~-1
3236 | Down -> navigate 1
3237 | Prior -> navigate ~-(fstate.maxrows)
3238 | Next -> navigate fstate.maxrows
3240 | Right ->
3241 let o =
3242 if ctrl
3243 then (
3244 G.postRedisplay "outline ctrl right";
3245 {< m_pan = m_pan + 1 >}
3247 else self#updownlevel 1
3249 coe o
3251 | Left ->
3252 let o =
3253 if ctrl
3254 then (
3255 G.postRedisplay "outline ctrl left";
3256 {< m_pan = m_pan - 1 >}
3258 else self#updownlevel ~-1
3260 coe o
3262 | Home ->
3263 G.postRedisplay "outline home";
3264 coe {< m_first = 0; m_active = 0 >}
3266 | End ->
3267 let active = source#getitemcount - 1 in
3268 let first = max 0 (active - fstate.maxrows) in
3269 G.postRedisplay "outline end";
3270 coe {< m_active = active; m_first = first >}
3272 | Delete|Escape|Insert|Enter|Ascii _|Code _|Ctrl _|Backspace|Fn _ ->
3273 super#key key mask
3274 end;;
3276 let genhistoutlines () =
3277 Config.gethist ()
3278 |> List.sort (fun (_, c1, _, _, _, _) (_, c2, _, _, _, _) ->
3279 compare c2.lastvisit c1.lastvisit)
3280 |> List.map (fun ((path, c, _, _, _, origin) as hist) ->
3281 let path = if nonemptystr origin then origin else path in
3282 let base = mbtoutf8 @@ Filename.basename path in
3283 (base ^ "\000" ^ c.title, 1, Ohistory hist)
3287 let gotohist (path, c, bookmarks, x, anchor, origin) =
3288 Config.save leavebirdseye;
3289 state.anchor <- anchor;
3290 state.bookmarks <- bookmarks;
3291 state.origin <- origin;
3292 state.x <- x;
3293 setconf conf c;
3294 let x0, y0, x1, y1 = conf.trimfuzz in
3295 wcmd "trimset %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1;
3296 reshape ~firsttime:true state.winw state.winh;
3297 opendoc path origin;
3298 setzoom c.zoom;
3301 let makecheckers () =
3302 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3303 following to say:
3304 converted by Issac Trotts. July 25, 2002 *)
3305 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3306 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3307 let id = GlTex.gen_texture () in
3308 GlTex.bind_texture ~target:`texture_2d id;
3309 GlPix.store (`unpack_alignment 1);
3310 GlTex.image2d image;
3311 List.iter (GlTex.parameter ~target:`texture_2d)
3312 [ `mag_filter `nearest; `min_filter `nearest ];
3316 let setcheckers enabled =
3317 match state.checkerstexid with
3318 | None ->
3319 if enabled then state.checkerstexid <- Some (makecheckers ())
3321 | Some checkerstexid ->
3322 if not enabled
3323 then (
3324 GlTex.delete_texture checkerstexid;
3325 state.checkerstexid <- None;
3329 let describe_layout layout =
3330 let d =
3331 match layout with
3332 | [] -> "Page 0"
3333 | l :: [] -> Printf.sprintf "Page %d" (l.pageno+1)
3334 | l :: rest ->
3335 let rangestr a b =
3336 if a.pageno = b.pageno then Printf.sprintf "%d" (a.pageno+1)
3337 else
3338 let sep = if a.pageno+1 = b.pageno then ", " else UniSyms.ellipsis in
3339 Printf.sprintf "%d%s%d" (a.pageno+1) sep (b.pageno+1)
3341 let rec fold s la lb = function
3342 | [] -> Printf.sprintf "%s %s" s (rangestr la lb)
3343 | l :: rest when l.pageno = succ lb.pageno -> fold s la l rest
3344 | l :: rest -> fold (s ^ " " ^ rangestr la lb ^ ",") l l rest
3346 fold "Pages" l l rest
3348 let percent =
3349 let maxy = maxy () in
3350 if maxy <= 0
3351 then 100.
3352 else 100. *. (float state.y /. float maxy)
3354 Printf.sprintf "%s of %d [%.2f%%]" d state.pagecount percent
3357 let setpresentationmode v =
3358 let n = page_of_y state.y in
3359 state.anchor <- (n, 0.0, 1.0);
3360 conf.presentation <- v;
3361 if conf.fitmodel = FitPage
3362 then reqlayout conf.angle conf.fitmodel;
3363 represent ();
3366 let enterinfomode =
3367 let btos b = if b then UniSyms.radical else E.s in
3368 let showextended = ref false in
3369 let showcolors = ref false in
3370 let leave mode _ = state.mode <- mode in
3371 let src =
3372 (object
3373 val mutable m_l = []
3374 val mutable m_a = E.a
3375 val mutable m_prev_uioh = nouioh
3376 val mutable m_prev_mode = View
3378 inherit lvsourcebase
3380 method reset prev_mode prev_uioh =
3381 m_a <- Array.of_list (List.rev m_l);
3382 m_l <- [];
3383 m_prev_mode <- prev_mode;
3384 m_prev_uioh <- prev_uioh;
3386 method int name get set =
3387 m_l <-
3388 (name, `int get, 1,
3389 Action (
3390 fun u ->
3391 let ondone s =
3392 try set (int_of_string s)
3393 with exn ->
3394 state.text <- Printf.sprintf "bad integer `%s': %s"
3395 s @@ exntos exn
3397 state.text <- E.s;
3398 let te = name ^ ": ", E.s, None, intentry, ondone, true in
3399 state.mode <- Textentry (te, leave m_prev_mode);
3401 )) :: m_l
3403 method int_with_suffix name get set =
3404 m_l <-
3405 (name, `intws get, 1,
3406 Action (
3407 fun u ->
3408 let ondone s =
3409 try set (int_of_string_with_suffix s)
3410 with exn ->
3411 state.text <- Printf.sprintf "bad integer `%s': %s"
3412 s @@ exntos exn
3414 state.text <- E.s;
3415 let te =
3416 name ^ ": ", E.s, None, intentry_with_suffix, ondone, true
3418 state.mode <- Textentry (te, leave m_prev_mode);
3420 )) :: m_l
3422 method bool ?(offset=1) ?(btos=btos) name get set =
3423 m_l <-
3424 (name, `bool (btos, get), offset, Action (
3425 fun u ->
3426 let v = get () in
3427 set (not v);
3429 )) :: m_l
3431 method color name get set =
3432 m_l <-
3433 (name, `color get, 1,
3434 Action (
3435 fun u ->
3436 let invalid = (nan, nan, nan) in
3437 let ondone s =
3438 let c =
3439 try color_of_string s
3440 with exn ->
3441 state.text <- Printf.sprintf "bad color `%s': %s"
3442 s @@ exntos exn;
3443 invalid
3445 if c <> invalid
3446 then set c;
3448 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3449 state.text <- color_to_string (get ());
3450 state.mode <- Textentry (te, leave m_prev_mode);
3452 )) :: m_l
3454 method string name get set =
3455 m_l <-
3456 (name, `string get, 1,
3457 Action (
3458 fun u ->
3459 let ondone s = set s in
3460 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3461 state.mode <- Textentry (te, leave m_prev_mode);
3463 )) :: m_l
3465 method colorspace name get set =
3466 m_l <-
3467 (name, `string get, 1,
3468 Action (
3469 fun _ ->
3470 let source =
3471 (object
3472 inherit lvsourcebase
3474 initializer
3475 m_active <- CSTE.to_int conf.colorspace;
3476 m_first <- 0;
3478 method getitemcount =
3479 Array.length CSTE.names
3480 method getitem n =
3481 (CSTE.names.(n), 0)
3482 method exit ~uioh ~cancel ~active ~first ~pan =
3483 ignore (uioh, first, pan);
3484 if not cancel then set active;
3485 None
3486 method hasaction _ = true
3487 end)
3489 state.text <- E.s;
3490 let modehash = findkeyhash conf "info" in
3491 coe (new listview ~zebra:false ~helpmode:false
3492 ~source ~trusted:true ~modehash)
3493 )) :: m_l
3495 method paxmark name get set =
3496 m_l <-
3497 (name, `string get, 1,
3498 Action (
3499 fun _ ->
3500 let source =
3501 (object
3502 inherit lvsourcebase
3504 initializer
3505 m_active <- MTE.to_int conf.paxmark;
3506 m_first <- 0;
3508 method getitemcount = Array.length MTE.names
3509 method getitem n = (MTE.names.(n), 0)
3510 method exit ~uioh ~cancel ~active ~first ~pan =
3511 ignore (uioh, first, pan);
3512 if not cancel then set active;
3513 None
3514 method hasaction _ = true
3515 end)
3517 state.text <- E.s;
3518 let modehash = findkeyhash conf "info" in
3519 coe (new listview ~zebra:false ~helpmode:false
3520 ~source ~trusted:true ~modehash)
3521 )) :: m_l
3523 method fitmodel name get set =
3524 m_l <-
3525 (name, `string get, 1,
3526 Action (
3527 fun _ ->
3528 let source =
3529 (object
3530 inherit lvsourcebase
3532 initializer
3533 m_active <- FMTE.to_int conf.fitmodel;
3534 m_first <- 0;
3536 method getitemcount = Array.length FMTE.names
3537 method getitem n = (FMTE.names.(n), 0)
3538 method exit ~uioh ~cancel ~active ~first ~pan =
3539 ignore (uioh, first, pan);
3540 if not cancel then set active;
3541 None
3542 method hasaction _ = true
3543 end)
3545 state.text <- E.s;
3546 let modehash = findkeyhash conf "info" in
3547 coe (new listview ~zebra:false ~helpmode:false
3548 ~source ~trusted:true ~modehash)
3549 )) :: m_l
3551 method caption s offset =
3552 m_l <- (s, `empty, offset, Noaction) :: m_l
3554 method caption2 s f offset =
3555 m_l <- (s, `string f, offset, Noaction) :: m_l
3557 method getitemcount = Array.length m_a
3559 method getitem n =
3560 let tostr = function
3561 | `int f -> string_of_int (f ())
3562 | `intws f -> string_with_suffix_of_int (f ())
3563 | `string f -> f ()
3564 | `color f -> color_to_string (f ())
3565 | `bool (btos, f) -> btos (f ())
3566 | `empty -> E.s
3568 let name, t, offset, _ = m_a.(n) in
3569 ((let s = tostr t in
3570 if nonemptystr s
3571 then Printf.sprintf "%s\t%s" name s
3572 else name),
3573 offset)
3575 method exit ~uioh ~cancel ~active ~first ~pan =
3576 let uiohopt =
3577 if not cancel
3578 then (
3579 let uioh =
3580 match m_a.(active) with
3581 | _, _, _, Action f -> f uioh
3582 | _, _, _, Noaction -> uioh
3584 Some uioh
3586 else None
3588 m_active <- active;
3589 m_first <- first;
3590 m_pan <- pan;
3591 uiohopt
3593 method hasaction n =
3594 match m_a.(n) with
3595 | _, _, _, Action _ -> true
3596 | _, _, _, Noaction -> false
3598 initializer m_active <- 1
3599 end)
3601 let rec fillsrc prevmode prevuioh =
3602 let sep () = src#caption E.s 0 in
3603 let colorp name get set =
3604 src#string name
3605 (fun () -> color_to_string (get ()))
3606 (fun v ->
3608 let c = color_of_string v in
3609 set c
3610 with exn ->
3611 state.text <-
3612 Printf.sprintf "bad color `%s': %s" v @@ exntos exn
3615 let rgba name get set =
3616 src#string name
3617 (fun () -> rgba_to_string (get ()))
3618 (fun v ->
3620 let c = rgba_of_string v in
3621 set c
3622 with exn ->
3623 state.text <-
3624 Printf.sprintf "bad color `%s': %s" v @@ exntos exn
3627 let oldmode = state.mode in
3628 let birdseye = isbirdseye state.mode in
3630 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3632 src#bool "presentation mode"
3633 (fun () -> conf.presentation)
3634 (fun v -> setpresentationmode v);
3636 src#bool "ignore case in searches"
3637 (fun () -> conf.icase)
3638 (fun v -> conf.icase <- v);
3640 src#bool "preload"
3641 (fun () -> conf.preload)
3642 (fun v -> conf.preload <- v);
3644 src#bool "highlight links"
3645 (fun () -> conf.hlinks)
3646 (fun v -> conf.hlinks <- v);
3648 src#bool "under info"
3649 (fun () -> conf.underinfo)
3650 (fun v -> conf.underinfo <- v);
3652 src#bool "persistent bookmarks"
3653 (fun () -> conf.savebmarks)
3654 (fun v -> conf.savebmarks <- v);
3656 src#fitmodel "fit model"
3657 (fun () -> FMTE.to_string conf.fitmodel)
3658 (fun v -> reqlayout conf.angle (FMTE.of_int v));
3660 src#bool "trim margins"
3661 (fun () -> conf.trimmargins)
3662 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3664 src#bool "persistent location"
3665 (fun () -> conf.jumpback)
3666 (fun v -> conf.jumpback <- v);
3668 sep ();
3669 src#int "inter-page space"
3670 (fun () -> conf.interpagespace)
3671 (fun n ->
3672 conf.interpagespace <- n;
3673 docolumns conf.columns;
3674 let pageno, py =
3675 match state.layout with
3676 | [] -> 0, 0
3677 | l :: _ ->
3678 l.pageno, l.pagey
3680 state.maxy <- calcheight ();
3681 let y = getpagey pageno in
3682 gotoxy state.x (y + py)
3685 src#int "page bias"
3686 (fun () -> conf.pagebias)
3687 (fun v -> conf.pagebias <- v);
3689 src#int "scroll step"
3690 (fun () -> conf.scrollstep)
3691 (fun n -> conf.scrollstep <- n);
3693 src#int "horizontal scroll step"
3694 (fun () -> conf.hscrollstep)
3695 (fun v -> conf.hscrollstep <- v);
3697 src#int "auto scroll step"
3698 (fun () ->
3699 match state.autoscroll with
3700 | Some step -> step
3701 | _ -> conf.autoscrollstep)
3702 (fun n ->
3703 let n = boundastep state.winh n in
3704 if state.autoscroll <> None
3705 then state.autoscroll <- Some n;
3706 conf.autoscrollstep <- n);
3708 src#int "zoom"
3709 (fun () -> truncate (conf.zoom *. 100.))
3710 (fun v -> pivotzoom ((float v) /. 100.));
3712 src#int "rotation"
3713 (fun () -> conf.angle)
3714 (fun v -> reqlayout v conf.fitmodel);
3716 src#int "scroll bar width"
3717 (fun () -> conf.scrollbw)
3718 (fun v ->
3719 conf.scrollbw <- v;
3720 reshape state.winw state.winh;
3723 src#int "scroll handle height"
3724 (fun () -> conf.scrollh)
3725 (fun v -> conf.scrollh <- v;);
3727 src#int "thumbnail width"
3728 (fun () -> conf.thumbw)
3729 (fun v ->
3730 conf.thumbw <- min 4096 v;
3731 match oldmode with
3732 | Birdseye beye ->
3733 leavebirdseye beye false;
3734 enterbirdseye ()
3735 | Textentry _
3736 | View
3737 | LinkNav _ -> ()
3740 let mode = state.mode in
3741 src#string "columns"
3742 (fun () ->
3743 match conf.columns with
3744 | Csingle _ -> "1"
3745 | Cmulti (multi, _) -> multicolumns_to_string multi
3746 | Csplit (count, _) -> "-" ^ string_of_int count
3748 (fun v ->
3749 let n, a, b = multicolumns_of_string v in
3750 setcolumns mode n a b);
3752 sep ();
3753 src#caption "Pixmap cache" 0;
3754 src#int_with_suffix "size (advisory)"
3755 (fun () -> conf.memlimit)
3756 (fun v -> conf.memlimit <- v);
3758 src#caption2 "used"
3759 (fun () ->
3760 Printf.sprintf "%s bytes, %d tiles"
3761 (string_with_suffix_of_int state.memused)
3762 (Hashtbl.length state.tilemap)) 1;
3764 sep ();
3765 src#caption "Layout" 0;
3766 src#caption2 "Dimension"
3767 (fun () ->
3768 Printf.sprintf "%dx%d (virtual %dx%d)"
3769 state.winw state.winh
3770 state.w state.maxy)
3772 if conf.debug
3773 then
3774 src#caption2 "Position" (fun () ->
3775 Printf.sprintf "%dx%d" state.x state.y
3777 else
3778 src#caption2 "Position" (fun () -> describe_layout state.layout) 1;
3780 sep ();
3781 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3782 "Save these parameters as global defaults at exit"
3783 (fun () -> conf.bedefault)
3784 (fun v -> conf.bedefault <- v);
3786 sep ();
3787 let btos b = if b then UniSyms.lguillemet else UniSyms.rguillemet in
3788 src#bool ~offset:0 ~btos "Extended parameters"
3789 (fun () -> !showextended)
3790 (fun v -> showextended := v; fillsrc prevmode prevuioh);
3791 if !showextended
3792 then (
3793 src#bool "checkers"
3794 (fun () -> conf.checkers)
3795 (fun v -> conf.checkers <- v; setcheckers v);
3796 src#bool "update cursor"
3797 (fun () -> conf.updatecurs)
3798 (fun v -> conf.updatecurs <- v);
3799 src#bool "scroll-bar on the left"
3800 (fun () -> conf.leftscroll)
3801 (fun v -> conf.leftscroll <- v);
3802 src#bool "verbose"
3803 (fun () -> conf.verbose)
3804 (fun v -> conf.verbose <- v);
3805 src#bool "invert colors"
3806 (fun () -> conf.invert)
3807 (fun v -> conf.invert <- v);
3808 src#bool "max fit"
3809 (fun () -> conf.maxhfit)
3810 (fun v -> conf.maxhfit <- v);
3811 src#bool "pax mode"
3812 (fun () -> conf.pax != None)
3813 (fun v ->
3814 if v
3815 then conf.pax <- Some (now ())
3816 else conf.pax <- None);
3817 src#string "uri launcher"
3818 (fun () -> conf.urilauncher)
3819 (fun v -> conf.urilauncher <- v);
3820 src#string "path launcher"
3821 (fun () -> conf.pathlauncher)
3822 (fun v -> conf.pathlauncher <- v);
3823 src#string "tile size"
3824 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3825 (fun v ->
3827 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
3828 conf.tilew <- max 64 w;
3829 conf.tileh <- max 64 h;
3830 flushtiles ();
3831 with exn ->
3832 state.text <- Printf.sprintf "bad tile size `%s': %s"
3833 v @@ exntos exn
3835 src#int "texture count"
3836 (fun () -> conf.texcount)
3837 (fun v ->
3838 if realloctexts v
3839 then conf.texcount <- v
3840 else impmsg "failed to set texture count please retry later"
3842 src#int "slice height"
3843 (fun () -> conf.sliceheight)
3844 (fun v ->
3845 conf.sliceheight <- v;
3846 wcmd "sliceh %d" conf.sliceheight;
3848 src#int "anti-aliasing level"
3849 (fun () -> conf.aalevel)
3850 (fun v ->
3851 conf.aalevel <- bound v 0 8;
3852 state.anchor <- getanchor ();
3853 opendoc state.path state.password;
3855 src#string "page scroll scaling factor"
3856 (fun () -> string_of_float conf.pgscale)
3857 (fun v ->
3859 let s = float_of_string v in
3860 conf.pgscale <- s
3861 with exn ->
3862 state.text <- Printf.sprintf
3863 "bad page scroll scaling factor `%s': %s" v
3864 @@ exntos exn
3867 src#int "ui font size"
3868 (fun () -> fstate.fontsize)
3869 (fun v -> setfontsize (bound v 5 100));
3870 src#int "hint font size"
3871 (fun () -> conf.hfsize)
3872 (fun v -> conf.hfsize <- bound v 5 100);
3873 src#bool "crop hack"
3874 (fun () -> conf.crophack)
3875 (fun v -> conf.crophack <- v);
3876 src#string "trim fuzz"
3877 (fun () -> irect_to_string conf.trimfuzz)
3878 (fun v ->
3880 conf.trimfuzz <- irect_of_string v;
3881 if conf.trimmargins
3882 then settrim true conf.trimfuzz;
3883 with exn ->
3884 state.text <- Printf.sprintf "bad irect `%s': %s" v
3885 @@ exntos exn
3887 src#string "throttle"
3888 (fun () ->
3889 match conf.maxwait with
3890 | None -> "show place holder if page is not ready"
3891 | Some time ->
3892 if time = infinity
3893 then "wait for page to fully render"
3894 else
3895 "wait " ^ string_of_float time
3896 ^ " seconds before showing placeholder"
3898 (fun v ->
3900 let f = float_of_string v in
3901 if f <= 0.0
3902 then conf.maxwait <- None
3903 else conf.maxwait <- Some f
3904 with exn ->
3905 state.text <- Printf.sprintf "bad time `%s': %s" v
3906 @@ exntos exn
3908 src#string "ghyll scroll"
3909 (fun () ->
3910 match conf.ghyllscroll with
3911 | None -> E.s
3912 | Some nab -> ghyllscroll_to_string nab
3914 (fun v ->
3915 try conf.ghyllscroll <- ghyllscroll_of_string v
3916 with
3917 | Failure msg ->
3918 state.text <- Printf.sprintf "bad ghyll `%s': %s" v msg
3919 | exn ->
3920 state.text <- Printf.sprintf "bad ghyll `%s': %s" v
3921 @@ exntos exn
3923 src#string "selection command"
3924 (fun () -> conf.selcmd)
3925 (fun v -> conf.selcmd <- v);
3926 src#string "synctex command"
3927 (fun () -> conf.stcmd)
3928 (fun v -> conf.stcmd <- v);
3929 src#string "pax command"
3930 (fun () -> conf.paxcmd)
3931 (fun v -> conf.paxcmd <- v);
3932 src#string "ask password command"
3933 (fun () -> conf.passcmd)
3934 (fun v -> conf.passcmd <- v);
3935 src#string "save path command"
3936 (fun () -> conf.savecmd)
3937 (fun v -> conf.savecmd <- v);
3938 src#colorspace "color space"
3939 (fun () -> CSTE.to_string conf.colorspace)
3940 (fun v ->
3941 conf.colorspace <- CSTE.of_int v;
3942 wcmd "cs %d" v;
3943 load state.layout;
3945 src#paxmark "pax mark method"
3946 (fun () -> MTE.to_string conf.paxmark)
3947 (fun v -> conf.paxmark <- MTE.of_int v);
3948 if bousable () && !opengl_has_pbo
3949 then
3950 src#bool "use PBO"
3951 (fun () -> conf.usepbo)
3952 (fun v -> conf.usepbo <- v);
3953 src#bool "mouse wheel scrolls pages"
3954 (fun () -> conf.wheelbypage)
3955 (fun v -> conf.wheelbypage <- v);
3956 src#bool "open remote links in a new instance"
3957 (fun () -> conf.riani)
3958 (fun v -> conf.riani <- v);
3959 src#bool "edit annotations inline"
3960 (fun () -> conf.annotinline)
3961 (fun v -> conf.annotinline <- v);
3962 src#bool "coarse positioning in presentation mode"
3963 (fun () -> conf.coarseprespos)
3964 (fun v -> conf.coarseprespos <- v);
3965 src#bool "use document CSS"
3966 (fun () -> conf.usedoccss)
3967 (fun v ->
3968 conf.usedoccss <- v;
3969 state.anchor <- getanchor ();
3970 opendoc state.path state.password;
3972 src#bool ~btos "colors"
3973 (fun () -> !showcolors)
3974 (fun v -> showcolors := v; fillsrc prevmode prevuioh);
3975 if !showcolors
3976 then (
3977 colorp " background"
3978 (fun () -> conf.bgcolor)
3979 (fun v -> conf.bgcolor <- v);
3980 rgba " scrollbar"
3981 (fun () -> conf.sbarcolor)
3982 (fun v -> conf.sbarcolor <- v);
3983 rgba " scrollbar handle"
3984 (fun () -> conf.sbarhndlcolor)
3985 (fun v -> conf.sbarhndlcolor <- v);
3989 sep ();
3990 src#caption "Document" 0;
3991 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
3992 src#caption2 "Pages"
3993 (fun () -> string_of_int state.pagecount) 1;
3994 src#caption2 "Dimensions"
3995 (fun () -> string_of_int (List.length state.pdims)) 1;
3996 if nonemptystr conf.css
3997 then src#caption2 "CSS" (fun () -> conf.css) 1;
3998 if conf.trimmargins
3999 then (
4000 sep ();
4001 src#caption "Trimmed margins" 0;
4002 src#caption2 "Dimensions"
4003 (fun () -> string_of_int (List.length state.pdims)) 1;
4006 sep ();
4007 src#caption "OpenGL" 0;
4008 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4009 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4011 sep ();
4012 src#caption "Location" 0;
4013 if nonemptystr state.origin
4014 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
4015 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
4017 src#reset prevmode prevuioh;
4019 fun () ->
4020 state.text <- E.s;
4021 resetmstate ();
4022 let prevmode = state.mode
4023 and prevuioh = state.uioh in
4024 fillsrc prevmode prevuioh;
4025 let source = (src :> lvsource) in
4026 let modehash = findkeyhash conf "info" in
4027 state.uioh <-
4028 coe (object (self)
4029 inherit listview ~zebra:false ~helpmode:false
4030 ~source ~trusted:true ~modehash as super
4031 val mutable m_prevmemused = 0
4032 method! infochanged = function
4033 | Memused ->
4034 if m_prevmemused != state.memused
4035 then (
4036 m_prevmemused <- state.memused;
4037 G.postRedisplay "memusedchanged";
4039 | Pdim -> G.postRedisplay "pdimchanged"
4040 | Docinfo -> fillsrc prevmode prevuioh
4042 method! key key mask =
4043 if not (Wsi.withctrl mask)
4044 then
4045 match [@warning "-4"] Wsi.kc2kt key with
4046 | Keys.Left -> coe (self#updownlevel ~-1)
4047 | Keys.Right -> coe (self#updownlevel 1)
4048 | _ -> super#key key mask
4049 else super#key key mask
4050 end);
4051 G.postRedisplay "info";
4054 let enterhelpmode =
4055 let source =
4056 (object
4057 inherit lvsourcebase
4058 method getitemcount = Array.length state.help
4059 method getitem n =
4060 let s, l, _ = state.help.(n) in
4061 (s, l)
4063 method exit ~uioh ~cancel ~active ~first ~pan =
4064 let optuioh =
4065 if not cancel
4066 then (
4067 match state.help.(active) with
4068 | _, _, Action f -> Some (f uioh)
4069 | _, _, Noaction -> Some uioh
4071 else None
4073 m_active <- active;
4074 m_first <- first;
4075 m_pan <- pan;
4076 optuioh
4078 method hasaction n =
4079 match state.help.(n) with
4080 | _, _, Action _ -> true
4081 | _, _, Noaction -> false
4083 initializer
4084 m_active <- -1
4085 end)
4086 in fun () ->
4087 let modehash = findkeyhash conf "help" in
4088 resetmstate ();
4089 state.uioh <- coe (new listview
4090 ~zebra:false ~helpmode:true
4091 ~source ~trusted:true ~modehash);
4092 G.postRedisplay "help";
4095 let entermsgsmode =
4096 let msgsource =
4097 (object
4098 inherit lvsourcebase
4099 val mutable m_items = E.a
4101 method getitemcount = 1 + Array.length m_items
4103 method getitem n =
4104 if n = 0
4105 then "[Clear]", 0
4106 else m_items.(n-1), 0
4108 method exit ~uioh ~cancel ~active ~first ~pan =
4109 ignore uioh;
4110 if not cancel
4111 then (
4112 if active = 0
4113 then Buffer.clear state.errmsgs;
4115 m_active <- active;
4116 m_first <- first;
4117 m_pan <- pan;
4118 None
4120 method hasaction n =
4121 n = 0
4123 method reset =
4124 state.newerrmsgs <- false;
4125 let l = Str.split newlinere (Buffer.contents state.errmsgs) in
4126 m_items <- Array.of_list l
4128 initializer
4129 m_active <- 0
4130 end)
4131 in fun () ->
4132 state.text <- E.s;
4133 resetmstate ();
4134 msgsource#reset;
4135 let source = (msgsource :> lvsource) in
4136 let modehash = findkeyhash conf "listview" in
4137 state.uioh <-
4138 coe (object
4139 inherit listview ~zebra:false ~helpmode:false
4140 ~source ~trusted:false ~modehash as super
4141 method! display =
4142 if state.newerrmsgs
4143 then msgsource#reset;
4144 super#display
4145 end);
4146 G.postRedisplay "msgs";
4149 let getusertext s =
4150 let editor = getenvwithdef "EDITOR" E.s in
4151 if emptystr editor
4152 then E.s
4153 else
4154 let tmppath = Filename.temp_file "llpp" "note" in
4155 if nonemptystr s
4156 then (
4157 let oc = open_out tmppath in
4158 output_string oc s;
4159 close_out oc;
4161 let execstr = editor ^ " " ^ tmppath in
4162 let s =
4163 match spawn execstr [] with
4164 | exception exn ->
4165 impmsg "spawn(%S) failed: %s" execstr @@ exntos exn;
4167 | pid ->
4168 match Unix.waitpid [] pid with
4169 | exception exn ->
4170 impmsg "waitpid(%d) failed: %s" pid @@ exntos exn;
4172 | (_pid, status) ->
4173 match status with
4174 | Unix.WEXITED 0 -> filecontents tmppath
4175 | Unix.WEXITED n ->
4176 impmsg "editor process(%s) exited abnormally: %d" execstr n;
4178 | Unix.WSIGNALED n ->
4179 impmsg "editor process(%s) was killed by signal %d" execstr n;
4181 | Unix.WSTOPPED n ->
4182 impmsg "editor(%s) process was stopped by signal %d" execstr n;
4185 match Unix.unlink tmppath with
4186 | exception exn ->
4187 impmsg "failed to ulink %S: %s" tmppath @@ exntos exn;
4189 | () -> s
4192 let enterannotmode opaque slinkindex =
4193 let msgsource =
4194 (object
4195 inherit lvsourcebase
4196 val mutable m_text = E.s
4197 val mutable m_items = E.a
4199 method getitemcount = Array.length m_items
4201 method getitem n =
4202 let label, _func = m_items.(n) in
4203 label, 0
4205 method exit ~uioh ~cancel ~active ~first ~pan =
4206 ignore (uioh, first, pan);
4207 if not cancel
4208 then (
4209 let _label, func = m_items.(active) in
4210 func ()
4212 None
4214 method hasaction n = nonemptystr @@ fst m_items.(n)
4216 method reset s =
4217 let rec split accu b i =
4218 let p = b+i in
4219 if p = String.length s
4220 then (String.sub s b (p-b), unit) :: accu
4221 else
4222 if (i > 70 && s.[p] = ' ') || s.[p] = '\r' || s.[p] = '\n'
4223 then
4224 let ss = if i = 0 then E.s else String.sub s b i in
4225 split ((ss, unit)::accu) (p+1) 0
4226 else
4227 split accu b (i+1)
4229 let cleanup () =
4230 wcmd "freepage %s" (~> opaque);
4231 let keys =
4232 Hashtbl.fold (fun key opaque' accu ->
4233 if opaque' = opaque'
4234 then key :: accu else accu) state.pagemap []
4236 List.iter (Hashtbl.remove state.pagemap) keys;
4237 flushtiles ();
4238 gotoxy state.x state.y
4240 let dele () =
4241 delannot opaque slinkindex;
4242 cleanup ();
4244 let edit inline () =
4245 let update s =
4246 if emptystr s
4247 then dele ()
4248 else (
4249 modannot opaque slinkindex s;
4250 cleanup ();
4253 if inline
4254 then
4255 let mode = state.mode in
4256 state.mode <-
4257 Textentry (
4258 ("annotation: ", m_text, None, textentry, update, true),
4259 fun _ -> state.mode <- mode);
4260 state.text <- E.s;
4261 enttext ();
4262 else
4263 let s = getusertext m_text in
4264 update s
4266 m_text <- s;
4267 m_items <-
4268 ( "[Copy]", fun () -> selstring m_text)
4269 :: ("[Delete]", dele)
4270 :: ("[Edit]", edit conf.annotinline)
4271 :: (E.s, unit)
4272 :: split [] 0 0 |> List.rev |> Array.of_list
4274 initializer
4275 m_active <- 0
4276 end)
4278 state.text <- E.s;
4279 let s = getannotcontents opaque slinkindex in
4280 resetmstate ();
4281 msgsource#reset s;
4282 let source = (msgsource :> lvsource) in
4283 let modehash = findkeyhash conf "listview" in
4284 state.uioh <- coe (object
4285 inherit listview ~zebra:false ~helpmode:false
4286 ~source ~trusted:false ~modehash
4287 end);
4288 G.postRedisplay "enterannotmode";
4291 let gotoremote spec =
4292 let filename, dest = splitatchar spec '#' in
4293 let getpath filename =
4294 let path =
4295 if nonemptystr filename
4296 then
4297 if Filename.is_relative filename
4298 then
4299 let dir = Filename.dirname state.path in
4300 let dir =
4301 if Filename.is_implicit dir
4302 then Filename.concat (Sys.getcwd ()) dir
4303 else dir
4305 Filename.concat dir filename
4306 else filename
4307 else E.s
4309 if Sys.file_exists path
4310 then path
4311 else E.s
4313 let path = getpath filename in
4314 let dospawn lcmd =
4315 if conf.riani
4316 then
4317 let cmd = Lazy.force_val lcmd in
4318 match spawn cmd with
4319 | _pid -> ()
4320 | exception exn -> dolog "failed to execute `%s': %s" cmd @@ exntos exn
4321 else
4322 let anchor = getanchor () in
4323 let ranchor = state.path, state.password, anchor, state.origin in
4324 state.origin <- E.s;
4325 state.ranchors <- ranchor :: state.ranchors;
4326 opendoc path E.s;
4328 if substratis spec 0 "page="
4329 then
4330 match Scanf.sscanf spec "page=%d" (fun n -> n) with
4331 | pageno ->
4332 state.anchor <- (pageno, 0.0, 0.0);
4333 dospawn @@ lazy (Printf.sprintf "%s -page %d %S" !selfexec pageno path);
4334 | exception exn ->
4335 adderrfmt "error parsing remote destination" "page: %s" @@ exntos exn
4336 else (
4337 state.nameddest <- dest;
4338 dospawn @@ lazy (!selfexec ^ " " ^ path ^ " -dest " ^ dest)
4342 let gotounder = function
4343 | Ulinkuri s when isexternallink s ->
4344 if substratis s 0 "file://"
4345 then gotoremote @@ String.sub s 7 (String.length s - 7)
4346 else gotouri s
4347 | Ulinkuri s ->
4348 let pageno, x, y = uritolocation s in
4349 addnav ();
4350 gotopagexy !wtmode pageno x y
4351 | Utext _ | Unone -> ()
4352 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
4355 let gotooutline (_, _, kind) =
4356 match kind with
4357 | Onone -> ()
4358 | Oanchor anchor ->
4359 let (pageno, y, _) = anchor in
4360 let y = getanchory
4361 (if conf.presentation then (pageno, y, 1.0) else anchor)
4363 addnav ();
4364 gotoghyll y
4365 | Ouri uri -> gotounder (Ulinkuri uri)
4366 | Olaunch _cmd -> failwith "gotounder (Ulaunch cmd)"
4367 | Oremote _remote -> failwith "gotounder (Uremote remote)"
4368 | Ohistory hist -> gotohist hist
4369 | Oremotedest _remotedest -> failwith "gotounder (Uremotedest remotedest)"
4372 class outlinesoucebase fetchoutlines = object (self)
4373 inherit lvsourcebase
4374 val mutable m_items = E.a
4375 val mutable m_minfo = E.a
4376 val mutable m_orig_items = E.a
4377 val mutable m_orig_minfo = E.a
4378 val mutable m_narrow_patterns = []
4379 val mutable m_gen = -1
4381 method getitemcount = Array.length m_items
4383 method getitem n =
4384 let s, n, _ = m_items.(n) in
4385 (s, n+0)
4387 method exit ~(uioh:uioh) ~cancel ~active ~(first:int) ~pan : uioh option =
4388 ignore (uioh, first);
4389 let items, minfo =
4390 if m_narrow_patterns = []
4391 then m_orig_items, m_orig_minfo
4392 else m_items, m_minfo
4394 m_pan <- pan;
4395 if not cancel
4396 then (
4397 m_items <- items;
4398 m_minfo <- minfo;
4399 gotooutline m_items.(active);
4401 else (
4402 m_items <- items;
4403 m_minfo <- minfo;
4405 None
4407 method hasaction (_:int) = true
4409 method greetmsg =
4410 if Array.length m_items != Array.length m_orig_items
4411 then
4412 let s =
4413 match m_narrow_patterns with
4414 | one :: [] -> one
4415 | many -> String.concat UniSyms.ellipsis (List.rev many)
4417 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
4418 else E.s
4420 method statestr =
4421 match m_narrow_patterns with
4422 | [] -> E.s
4423 | one :: [] -> one
4424 | head :: _ -> UniSyms.ellipsis ^ head
4426 method narrow pattern =
4427 match Str.regexp_case_fold pattern with
4428 | exception _ -> ()
4429 | re ->
4430 let rec loop accu minfo n =
4431 if n = -1
4432 then (
4433 m_items <- Array.of_list accu;
4434 m_minfo <- Array.of_list minfo;
4436 else
4437 let (s, _, _) as o = m_items.(n) in
4438 let accu, minfo =
4439 match Str.search_forward re s 0 with
4440 | exception Not_found -> accu, minfo
4441 | first -> o :: accu, (first, Str.match_end ()) :: minfo
4443 loop accu minfo (n-1)
4445 loop [] [] (Array.length m_items - 1)
4447 method! getminfo = m_minfo
4449 method denarrow =
4450 m_orig_items <- fetchoutlines ();
4451 m_minfo <- m_orig_minfo;
4452 m_items <- m_orig_items
4454 method add_narrow_pattern pattern =
4455 m_narrow_patterns <- pattern :: m_narrow_patterns
4457 method del_narrow_pattern =
4458 match m_narrow_patterns with
4459 | _ :: rest -> m_narrow_patterns <- rest
4460 | [] -> ()
4462 method renarrow =
4463 self#denarrow;
4464 match m_narrow_patterns with
4465 | pattern :: [] -> self#narrow pattern; pattern
4466 | list ->
4467 List.fold_left (fun accu pattern ->
4468 self#narrow pattern;
4469 pattern ^ UniSyms.ellipsis ^ accu) E.s list
4471 method calcactive (_:anchor) = 0
4473 method reset anchor items =
4474 if state.gen != m_gen
4475 then (
4476 m_orig_items <- items;
4477 m_items <- items;
4478 m_narrow_patterns <- [];
4479 m_minfo <- E.a;
4480 m_orig_minfo <- E.a;
4481 m_gen <- state.gen;
4483 else (
4484 if items != m_orig_items
4485 then (
4486 m_orig_items <- items;
4487 if m_narrow_patterns == []
4488 then m_items <- items;
4491 let active = self#calcactive anchor in
4492 m_active <- active;
4493 m_first <- firstof m_first active
4497 let outlinesource fetchoutlines =
4498 (object
4499 inherit outlinesoucebase fetchoutlines
4500 method! calcactive anchor =
4501 let rely = getanchory anchor in
4502 let rec loop n best bestd =
4503 if n = Array.length m_items
4504 then best
4505 else
4506 let _, _, kind = m_items.(n) in
4507 match kind with
4508 | Oanchor anchor ->
4509 let orely = getanchory anchor in
4510 let d = abs (orely - rely) in
4511 if d < bestd
4512 then loop (n+1) n d
4513 else loop (n+1) best bestd
4514 | Onone | Oremote _ | Olaunch _
4515 | Oremotedest _ | Ouri _ | Ohistory _ ->
4516 loop (n+1) best bestd
4518 loop 0 ~-1 max_int
4519 end)
4522 let enteroutlinemode, enterbookmarkmode, enterhistmode =
4523 let mkselector sourcetype =
4524 let fetchoutlines () =
4525 match sourcetype with
4526 | `bookmarks -> Array.of_list state.bookmarks
4527 | `outlines -> state.outlines
4528 | `history -> genhistoutlines () |> Array.of_list
4530 let source =
4531 if sourcetype = `history
4532 then new outlinesoucebase fetchoutlines
4533 else outlinesource fetchoutlines
4535 (fun errmsg ->
4536 let outlines = fetchoutlines () in
4537 if Array.length outlines = 0
4538 then showtext ' ' errmsg
4539 else (
4540 resetmstate ();
4541 Wsi.setcursor Wsi.CURSOR_INHERIT;
4542 let anchor = getanchor () in
4543 source#reset anchor outlines;
4544 state.text <- source#greetmsg;
4545 state.uioh <-
4546 coe (new outlinelistview ~zebra:(sourcetype=`history) ~source);
4547 G.postRedisplay "enter selector";
4551 let mkenter sourcetype errmsg =
4552 let enter = mkselector sourcetype in
4553 fun () -> enter errmsg
4555 ( mkenter `outlines "document has no outline"
4556 , mkenter `bookmarks "document has no bookmarks (yet)"
4557 , mkenter `history "history is empty" )
4560 let quickbookmark ?title () =
4561 match state.layout with
4562 | [] -> ()
4563 | l :: _ ->
4564 let title =
4565 match title with
4566 | None ->
4567 Unix.(
4568 let tm = localtime (now ()) in
4569 Printf.sprintf
4570 "Quick (page %d) (bookmarked on %02d/%02d/%d at %02d:%02d)"
4571 (l.pageno+1)
4572 tm.tm_mday (tm.tm_mon+1) (tm.tm_year+1900) tm.tm_hour tm.tm_min
4574 | Some title -> title
4576 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4579 let setautoscrollspeed step goingdown =
4580 let incr = max 1 ((abs step) / 2) in
4581 let incr = if goingdown then incr else -incr in
4582 let astep = boundastep state.winh (step + incr) in
4583 state.autoscroll <- Some astep;
4586 let canpan () =
4587 match conf.columns with
4588 | Csplit _ -> true
4589 | Csingle _ | Cmulti _ -> state.x != 0 || conf.zoom > 1.0
4592 let panbound x = bound x (-state.w) state.winw;;
4594 let existsinrow pageno (columns, coverA, coverB) p =
4595 let last = ((pageno - coverA) mod columns) + columns in
4596 let rec any = function
4597 | [] -> false
4598 | l :: rest ->
4599 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4600 then p l
4601 else (
4602 if not (p l)
4603 then (if l.pageno = last then false else any rest)
4604 else true
4607 any state.layout
4610 let nextpage () =
4611 match state.layout with
4612 | [] ->
4613 let pageno = page_of_y state.y in
4614 gotoghyll (getpagey (pageno+1))
4615 | l :: rest ->
4616 match conf.columns with
4617 | Csingle _ ->
4618 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4619 then
4620 let y = clamp (pgscale state.winh) in
4621 gotoghyll y
4622 else
4623 let pageno = min (l.pageno+1) (state.pagecount-1) in
4624 gotoghyll (getpagey pageno)
4625 | Cmulti ((c, _, _) as cl, _) ->
4626 if conf.presentation
4627 && (existsinrow l.pageno cl
4628 (fun l -> l.pageh > l.pagey + l.pagevh))
4629 then
4630 let y = clamp (pgscale state.winh) in
4631 gotoghyll y
4632 else
4633 let pageno = min (l.pageno+c) (state.pagecount-1) in
4634 gotoghyll (getpagey pageno)
4635 | Csplit (n, _) ->
4636 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4637 then
4638 let pagey, pageh = getpageyh l.pageno in
4639 let pagey = pagey + pageh * l.pagecol in
4640 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4641 gotoghyll (pagey + pageh + ips)
4644 let prevpage () =
4645 match state.layout with
4646 | [] ->
4647 let pageno = page_of_y state.y in
4648 gotoghyll (getpagey (pageno-1))
4649 | l :: _ ->
4650 match conf.columns with
4651 | Csingle _ ->
4652 if conf.presentation && l.pagey != 0
4653 then
4654 gotoghyll (clamp (pgscale ~-(state.winh)))
4655 else
4656 let pageno = max 0 (l.pageno-1) in
4657 gotoghyll (getpagey pageno)
4658 | Cmulti ((c, _, coverB) as cl, _) ->
4659 if conf.presentation &&
4660 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4661 then
4662 gotoghyll (clamp (pgscale ~-(state.winh)))
4663 else
4664 let decr =
4665 if l.pageno = state.pagecount - coverB
4666 then 1
4667 else c
4669 let pageno = max 0 (l.pageno-decr) in
4670 gotoghyll (getpagey pageno)
4671 | Csplit (n, _) ->
4672 let y =
4673 if l.pagecol = 0
4674 then
4675 if l.pageno = 0
4676 then l.pagey
4677 else
4678 let pageno = max 0 (l.pageno-1) in
4679 let pagey, pageh = getpageyh pageno in
4680 pagey + (n-1)*pageh
4681 else
4682 let pagey, pageh = getpageyh l.pageno in
4683 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4685 gotoghyll y
4688 let save () =
4689 if emptystr conf.savecmd
4690 then adderrmsg "savepath-command is empty"
4691 "don't know where to save modified document"
4692 else
4693 let savecmd = Str.global_replace percentsre state.path conf.savecmd in
4694 let path =
4695 getcmdoutput
4696 (fun exn ->
4697 adderrfmt savecmd "failed to produce path to the saved copy: %s" exn)
4698 savecmd
4700 if nonemptystr path
4701 then
4702 let tmp = path ^ ".tmp" in
4703 savedoc tmp;
4704 Unix.rename tmp path;
4707 let viewkeyboard key mask =
4708 let enttext te =
4709 let mode = state.mode in
4710 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4711 state.text <- E.s;
4712 enttext ();
4713 G.postRedisplay "view:enttext"
4715 let ctrl = Wsi.withctrl mask in
4716 let open Keys in
4717 match Wsi.kc2kt key with
4718 | Ascii 'S' -> state.slideshow <- state.slideshow lxor 1
4720 | Ascii 'Q' -> exit 0
4722 | Ascii 'W' ->
4723 if hasunsavedchanges ()
4724 then save ()
4726 | Insert ->
4727 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4728 then (
4729 state.mode <- (
4730 match state.lnava with
4731 | None -> LinkNav (Ltgendir 0)
4732 | Some pn -> LinkNav (Ltexact pn)
4734 gotoxy state.x state.y;
4736 else impmsg "keyboard link navigation does not work under rotation"
4738 | Escape | Ascii 'q' ->
4739 begin match state.mstate with
4740 | Mzoomrect _ ->
4741 resetmstate ();
4742 G.postRedisplay "kill rect";
4743 | Msel _
4744 | Mpan _
4745 | Mscrolly | Mscrollx
4746 | Mzoom _
4747 | Mnone ->
4748 begin match state.mode with
4749 | LinkNav ln ->
4750 begin match ln with
4751 | Ltexact pl -> state.lnava <- Some pl
4752 | Ltgendir _ | Ltnotready _ -> state.lnava <- None
4753 end;
4754 state.mode <- View;
4755 G.postRedisplay "esc leave linknav"
4756 | Birdseye _ | Textentry _ | View ->
4757 match state.ranchors with
4758 | [] -> raise Quit
4759 | (path, password, anchor, origin) :: rest ->
4760 state.ranchors <- rest;
4761 state.anchor <- anchor;
4762 state.origin <- origin;
4763 state.nameddest <- E.s;
4764 opendoc path password
4765 end;
4766 end;
4768 | Backspace ->
4769 addnavnorc ();
4770 gotoghyll (getnav ~-1)
4772 | Ascii 'o' ->
4773 enteroutlinemode ()
4775 | Ascii 'H' ->
4776 enterhistmode ()
4778 | Ascii 'u' ->
4779 state.rects <- [];
4780 state.text <- E.s;
4781 Hashtbl.iter (fun _ opaque ->
4782 clearmark opaque;
4783 Hashtbl.clear state.prects) state.pagemap;
4784 G.postRedisplay "dehighlight";
4786 | Ascii (('/' | '?') as c) ->
4787 let ondone isforw s =
4788 cbput state.hists.pat s;
4789 state.searchpattern <- s;
4790 search s isforw
4792 let s = String.make 1 c in
4793 enttext (s, E.s, Some (onhist state.hists.pat),
4794 textentry, ondone (c = '/'), true)
4796 | Ascii '+' | Ascii '=' when ctrl ->
4797 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4798 pivotzoom (conf.zoom +. incr)
4800 | Ascii '+' ->
4801 let ondone s =
4802 let n =
4803 try int_of_string s with exn ->
4804 state.text <-
4805 Printf.sprintf "bad integer `%s': %s" s @@ exntos exn;
4806 max_int
4808 if n != max_int
4809 then (
4810 conf.pagebias <- n;
4811 state.text <- "page bias is now " ^ string_of_int n;
4814 enttext ("page bias: ", E.s, None, intentry, ondone, true)
4816 | Ascii '-' when ctrl ->
4817 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4818 pivotzoom (max 0.01 (conf.zoom -. decr))
4820 | Ascii '-' ->
4821 let ondone msg = state.text <- msg in
4822 enttext (
4823 "option [acfhilpstvxACFPRSZTISM]: ", E.s, None,
4824 optentry state.mode, ondone, true
4827 | Ascii '0' when ctrl ->
4828 if conf.zoom = 1.0
4829 then gotoxy 0 state.y
4830 else setzoom 1.0
4832 | Ascii ('1'|'2' as c) when ctrl && conf.fitmodel != FitPage ->
4833 let cols =
4834 match conf.columns with
4835 | Csingle _ | Cmulti _ -> 1
4836 | Csplit (n, _) -> n
4838 let h = state.winh -
4839 conf.interpagespace lsl (if conf.presentation then 1 else 0)
4841 let zoom = zoomforh state.winw h 0 cols in
4842 if zoom > 0.0 && (c = '2' || zoom < 1.0)
4843 then setzoom zoom
4845 | Ascii '3' when ctrl ->
4846 let fm =
4847 match conf.fitmodel with
4848 | FitWidth -> FitProportional
4849 | FitProportional -> FitPage
4850 | FitPage -> FitWidth
4852 state.text <- "fit model: " ^ FMTE.to_string fm;
4853 reqlayout conf.angle fm
4855 | Ascii '4' when ctrl ->
4856 let zoom = getmaxw () /. float state.winw in
4857 if zoom > 0.0 then setzoom zoom
4859 | Fn 9 ->
4860 togglebirdseye ()
4862 | Ascii '9' when ctrl ->
4863 togglebirdseye ()
4865 | Ascii ('0'..'9' as c) when not ctrl ->
4866 let ondone s =
4867 let n =
4868 try int_of_string s with exn ->
4869 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn;
4872 if n >= 0
4873 then (
4874 addnav ();
4875 cbput state.hists.pag (string_of_int n);
4876 gotopage1 (n + conf.pagebias - 1) 0;
4879 let [@warning "-4"] pageentry text = function
4880 | Keys.Ascii 'g' -> TEdone text
4881 | key -> intentry text key
4883 let text = String.make 1 c in
4884 enttext (":", text, Some (onhist state.hists.pag),
4885 pageentry, ondone, true)
4887 | Ascii 'b' ->
4888 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
4889 G.postRedisplay "toggle scrollbar";
4891 | Ascii 'B' ->
4892 state.bzoom <- not state.bzoom;
4893 state.rects <- [];
4894 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
4896 | Ascii 'l' ->
4897 conf.hlinks <- not conf.hlinks;
4898 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4899 G.postRedisplay "toggle highlightlinks";
4901 | Ascii 'F' ->
4902 if conf.angle mod 360 = 0
4903 then (
4904 state.glinks <- true;
4905 let mode = state.mode in
4906 state.mode <-
4907 Textentry (
4908 (":", E.s, None, linknentry, linknact gotounder, false),
4909 (fun _ ->
4910 state.glinks <- false;
4911 state.mode <- mode)
4913 state.text <- E.s;
4914 G.postRedisplay "view:linkent(F)"
4916 else impmsg "hint mode does not work under rotation"
4918 | Ascii 'y' ->
4919 state.glinks <- true;
4920 let mode = state.mode in
4921 state.mode <-
4922 Textentry (
4923 (":", E.s, None, linknentry,
4924 linknact (fun under -> selstring (undertext under)), false),
4925 (fun _ ->
4926 state.glinks <- false;
4927 state.mode <- mode)
4929 state.text <- E.s;
4930 G.postRedisplay "view:linkent"
4932 | Ascii 'a' ->
4933 begin match state.autoscroll with
4934 | Some step ->
4935 conf.autoscrollstep <- step;
4936 state.autoscroll <- None
4937 | None ->
4938 state.autoscroll <- Some conf.autoscrollstep;
4939 state.slideshow <- state.slideshow land lnot 2
4942 | Ascii 'p' when ctrl ->
4943 launchpath () (* XXX where do error messages go? *)
4945 | Ascii 'P' ->
4946 setpresentationmode (not conf.presentation);
4947 showtext ' ' ("presentation mode " ^
4948 if conf.presentation then "on" else "off");
4950 | Ascii 'f' ->
4951 if List.mem Wsi.Fullscreen state.winstate
4952 then Wsi.reshape conf.cwinw conf.cwinh
4953 else Wsi.fullscreen ()
4955 | Ascii ('p'|'N') ->
4956 search state.searchpattern false
4958 | Ascii 'n' | Fn 3 ->
4959 search state.searchpattern true
4961 | Ascii 't' ->
4962 begin match state.layout with
4963 | [] -> ()
4964 | l :: _ ->
4965 gotoghyll (getpagey l.pageno)
4968 | Ascii ' ' ->
4969 nextpage ()
4971 | Delete ->
4972 prevpage ()
4974 | Ascii '=' ->
4975 showtext ' ' (describe_layout state.layout);
4977 | Ascii 'w' ->
4978 begin match state.layout with
4979 | [] -> ()
4980 | l :: _ ->
4981 Wsi.reshape l.pagew l.pageh;
4982 G.postRedisplay "w"
4985 | Ascii '\'' ->
4986 enterbookmarkmode ()
4988 | Ascii 'h' | Fn 1 ->
4989 enterhelpmode ()
4991 | Ascii 'i' ->
4992 enterinfomode ()
4994 | Ascii 'e' when Buffer.length state.errmsgs > 0 ->
4995 entermsgsmode ()
4997 | Ascii 'm' ->
4998 let ondone s =
4999 match state.layout with
5000 | l :: _ ->
5001 if nonemptystr s
5002 then
5003 state.bookmarks <-
5004 (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
5005 | _ -> ()
5007 enttext ("bookmark: ", E.s, None, textentry, ondone, true)
5009 | Ascii '~' ->
5010 quickbookmark ();
5011 showtext ' ' "Quick bookmark added";
5013 | Ascii 'z' ->
5014 begin match state.layout with
5015 | l :: _ ->
5016 let rect = getpdimrect l.pagedimno in
5017 let w, h =
5018 if conf.crophack
5019 then
5020 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5021 truncate (1.2 *. (rect.(3) -. rect.(0))))
5022 else
5023 (truncate (rect.(1) -. rect.(0)),
5024 truncate (rect.(3) -. rect.(0)))
5026 let w = truncate ((float w)*.conf.zoom)
5027 and h = truncate ((float h)*.conf.zoom) in
5028 if w != 0 && h != 0
5029 then (
5030 state.anchor <- getanchor ();
5031 Wsi.reshape w (h + conf.interpagespace)
5033 G.postRedisplay "z";
5035 | [] -> ()
5038 | Ascii 'x' -> state.roam ()
5040 | Ascii ('<'|'>' as c) ->
5041 reqlayout
5042 (conf.angle + (if c = '>' then 30 else -30)) conf.fitmodel
5044 | Ascii ('['|']' as c) ->
5045 conf.colorscale <-
5046 bound (conf.colorscale +. (if c = ']' then 0.1 else -0.1)) 0.0 1.0;
5047 G.postRedisplay "brightness";
5049 | Ascii 'c' when state.mode = View ->
5050 if Wsi.withalt mask
5051 then (
5052 if conf.zoom > 1.0
5053 then
5054 let m = (state.winw - state.w) / 2 in
5055 gotoxy_and_clear_text m state.y
5057 else
5058 let (c, a, b), z =
5059 match state.prevcolumns with
5060 | None -> (1, 0, 0), 1.0
5061 | Some (columns, z) ->
5062 let cab =
5063 match columns with
5064 | Csplit (c, _) -> -c, 0, 0
5065 | Cmulti ((c, a, b), _) -> c, a, b
5066 | Csingle _ -> 1, 0, 0
5068 cab, z
5070 setcolumns View c a b;
5071 setzoom z
5073 | Down | Up when ctrl && Wsi.withshift mask ->
5074 let zoom, x = state.prevzoom in
5075 setzoom zoom;
5076 state.x <- x;
5078 | Ascii 'k' | Up ->
5079 begin match state.autoscroll with
5080 | None ->
5081 begin match state.mode with
5082 | Birdseye beye -> upbirdseye 1 beye
5083 | Textentry _ | View | LinkNav _ ->
5084 if ctrl
5085 then gotoxy_and_clear_text state.x (clamp ~-(state.winh/2))
5086 else (
5087 if not (Wsi.withshift mask) && conf.presentation
5088 then prevpage ()
5089 else gotoghyll1 true (clamp (-conf.scrollstep))
5092 | Some n ->
5093 setautoscrollspeed n false
5096 | Ascii 'j' | Down ->
5097 begin match state.autoscroll with
5098 | None ->
5099 begin match state.mode with
5100 | Birdseye beye -> downbirdseye 1 beye
5101 | Textentry _ | View | LinkNav _ ->
5102 if ctrl
5103 then gotoxy_and_clear_text state.x (clamp (state.winh/2))
5104 else (
5105 if not (Wsi.withshift mask) && conf.presentation
5106 then nextpage ()
5107 else gotoghyll1 true (clamp (conf.scrollstep))
5110 | Some n ->
5111 setautoscrollspeed n true
5114 | Left | Right when not (Wsi.withalt mask) ->
5115 if canpan ()
5116 then
5117 let dx =
5118 if ctrl
5119 then state.winw / 2
5120 else conf.hscrollstep
5122 let dx =
5123 let pv = Wsi.kc2kt key in
5124 if pv = Keys.Left then dx else -dx
5126 gotoxy_and_clear_text (panbound (state.x + dx)) state.y
5127 else (
5128 state.text <- E.s;
5129 G.postRedisplay "left/right"
5132 | Prior ->
5133 let y =
5134 if ctrl
5135 then
5136 match state.layout with
5137 | [] -> state.y
5138 | l :: _ -> state.y - l.pagey
5139 else
5140 clamp (pgscale (-state.winh))
5142 gotoghyll y
5144 | Next ->
5145 let y =
5146 if ctrl
5147 then
5148 match List.rev state.layout with
5149 | [] -> state.y
5150 | l :: _ -> getpagey l.pageno
5151 else
5152 clamp (pgscale state.winh)
5154 gotoghyll y
5156 | Ascii 'g' | Home ->
5157 addnav ();
5158 gotoghyll 0
5159 | Ascii 'G' | End ->
5160 addnav ();
5161 gotoghyll (clamp state.maxy)
5163 | Right when Wsi.withalt mask ->
5164 addnavnorc ();
5165 gotoghyll (getnav 1)
5166 | Left when Wsi.withalt mask ->
5167 addnavnorc ();
5168 gotoghyll (getnav ~-1)
5170 | Ascii 'r' ->
5171 reload ()
5173 | Ascii 'v' when conf.debug ->
5174 state.rects <- [];
5175 List.iter (fun l ->
5176 match getopaque l.pageno with
5177 | None -> ()
5178 | Some opaque ->
5179 let x0, y0, x1, y1 = pagebbox opaque in
5180 let rect = (float x0, float y0,
5181 float x1, float y0,
5182 float x1, float y1,
5183 float x0, float y1) in
5184 debugrect rect;
5185 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
5186 state.rects <- (l.pageno, color, rect) :: state.rects;
5187 ) state.layout;
5188 G.postRedisplay "v";
5190 | Ascii '|' ->
5191 let mode = state.mode in
5192 let cmd = ref E.s in
5193 let onleave = function
5194 | Cancel -> state.mode <- mode
5195 | Confirm ->
5196 List.iter (fun l ->
5197 match getopaque l.pageno with
5198 | Some opaque -> pipesel opaque !cmd
5199 | None -> ()) state.layout;
5200 state.mode <- mode
5202 let ondone s =
5203 cbput state.hists.sel s;
5204 cmd := s
5206 let te =
5207 "| ", !cmd, Some (onhist state.hists.sel), textentry, ondone, true
5209 G.postRedisplay "|";
5210 state.mode <- Textentry (te, onleave);
5212 | (Ascii _|Fn _|Enter|Left|Right|Code _|Ctrl _) ->
5213 vlog "huh? %s" (Wsi.keyname key)
5216 let linknavkeyboard key mask linknav =
5217 let pv = Wsi.kc2kt key in
5218 let getpage pageno =
5219 let rec loop = function
5220 | [] -> None
5221 | l :: _ when l.pageno = pageno -> Some l
5222 | _ :: rest -> loop rest
5223 in loop state.layout
5225 let doexact (pageno, n) =
5226 match getopaque pageno, getpage pageno with
5227 | Some opaque, Some l ->
5228 if pv = Keys.Enter
5229 then
5230 let under = getlink opaque n in
5231 G.postRedisplay "link gotounder";
5232 gotounder under;
5233 state.mode <- View;
5234 else
5235 let opt, dir =
5236 let open Keys in
5237 match pv with
5238 | Home -> Some (findlink opaque LDfirst), -1
5239 | End -> Some (findlink opaque LDlast), 1
5240 | Left -> Some (findlink opaque (LDleft n)), -1
5241 | Right -> Some (findlink opaque (LDright n)), 1
5242 | Up -> Some (findlink opaque (LDup n)), -1
5243 | Down -> Some (findlink opaque (LDdown n)), 1
5245 | Delete|Escape|Insert|Enter|Next|Prior|Ascii _
5246 | Code _|Fn _|Ctrl _|Backspace -> None, 0
5248 let pwl l dir =
5249 begin match findpwl l.pageno dir with
5250 | Pwlnotfound -> ()
5251 | Pwl pageno ->
5252 let notfound dir =
5253 state.mode <- LinkNav (Ltgendir dir);
5254 let y, h = getpageyh pageno in
5255 let y =
5256 if dir < 0
5257 then y + h - state.winh
5258 else y
5260 gotoxy state.x y
5262 begin match getopaque pageno, getpage pageno with
5263 | Some opaque, Some _ ->
5264 let link =
5265 let ld = if dir > 0 then LDfirst else LDlast in
5266 findlink opaque ld
5268 begin match link with
5269 | Lfound m ->
5270 showlinktype (getlink opaque m);
5271 state.mode <- LinkNav (Ltexact (pageno, m));
5272 G.postRedisplay "linknav jpage";
5273 | Lnotfound -> notfound dir
5274 end;
5275 | _ -> notfound dir
5276 end;
5277 end;
5279 begin match opt with
5280 | Some Lnotfound -> pwl l dir;
5281 | Some (Lfound m) ->
5282 if m = n
5283 then pwl l dir
5284 else (
5285 let _, y0, _, y1 = getlinkrect opaque m in
5286 if y0 < l.pagey
5287 then gotopage1 l.pageno y0
5288 else (
5289 let d = fstate.fontsize + 1 in
5290 if y1 - l.pagey > l.pagevh - d
5291 then gotopage1 l.pageno (y1 - state.winh + d)
5292 else G.postRedisplay "linknav";
5294 showlinktype (getlink opaque m);
5295 state.mode <- LinkNav (Ltexact (l.pageno, m));
5298 | None -> viewkeyboard key mask
5299 end;
5300 | _ -> viewkeyboard key mask
5302 if pv = Keys.Insert
5303 then (
5304 begin match linknav with
5305 | Ltexact pa -> state.lnava <- Some pa
5306 | Ltgendir _ | Ltnotready _ -> ()
5307 end;
5308 state.mode <- View;
5309 G.postRedisplay "leave linknav"
5311 else
5312 match linknav with
5313 | Ltgendir _ | Ltnotready _ -> viewkeyboard key mask
5314 | Ltexact exact -> doexact exact
5317 let keyboard key mask =
5318 if (key = Char.code 'g' && Wsi.withctrl mask) && not (istextentry state.mode)
5319 then wcmd "interrupt"
5320 else state.uioh <- state.uioh#key key mask
5323 let birdseyekeyboard key mask
5324 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5325 let incr =
5326 match conf.columns with
5327 | Csingle _ -> 1
5328 | Cmulti ((c, _, _), _) -> c
5329 | Csplit _ -> failwith "bird's eye split mode"
5331 let pgh layout = List.fold_left
5332 (fun m l -> max l.pageh m) state.winh layout in
5333 let open Keys in
5334 match Wsi.kc2kt key with
5335 | Ascii 'l' when Wsi.withctrl mask ->
5336 let y, h = getpageyh pageno in
5337 let top = (state.winh - h) / 2 in
5338 gotoxy state.x (max 0 (y - top))
5339 | Enter -> leavebirdseye beye false
5340 | Escape -> leavebirdseye beye true
5341 | Up -> upbirdseye incr beye
5342 | Down -> downbirdseye incr beye
5343 | Left -> upbirdseye 1 beye
5344 | Right -> downbirdseye 1 beye
5346 | Prior ->
5347 begin match state.layout with
5348 | l :: _ ->
5349 if l.pagey != 0
5350 then (
5351 state.mode <- Birdseye (
5352 oconf, leftx, l.pageno, hooverpageno, anchor
5354 gotopage1 l.pageno 0;
5356 else (
5357 let layout = layout state.x (state.y-state.winh)
5358 state.winw
5359 (pgh state.layout) in
5360 match layout with
5361 | [] -> gotoxy state.x (clamp (-state.winh))
5362 | l :: _ ->
5363 state.mode <- Birdseye (
5364 oconf, leftx, l.pageno, hooverpageno, anchor
5366 gotopage1 l.pageno 0
5369 | [] -> gotoxy state.x (clamp (-state.winh))
5370 end;
5372 | Next ->
5373 begin match List.rev state.layout with
5374 | l :: _ ->
5375 let layout = layout state.x
5376 (state.y + (pgh state.layout))
5377 state.winw state.winh in
5378 begin match layout with
5379 | [] ->
5380 let incr = l.pageh - l.pagevh in
5381 if incr = 0
5382 then (
5383 state.mode <-
5384 Birdseye (
5385 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5387 G.postRedisplay "birdseye pagedown";
5389 else gotoxy state.x (clamp (incr + conf.interpagespace*2));
5391 | l :: _ ->
5392 state.mode <-
5393 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5394 gotopage1 l.pageno 0;
5397 | [] -> gotoxy state.x (clamp state.winh)
5398 end;
5400 | Home ->
5401 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5402 gotopage1 0 0
5404 | End ->
5405 let pageno = state.pagecount - 1 in
5406 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5407 if not (pagevisible state.layout pageno)
5408 then
5409 let h =
5410 match List.rev state.pdims with
5411 | [] -> state.winh
5412 | (_, _, h, _) :: _ -> h
5414 gotoxy
5415 state.x
5416 (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5417 else G.postRedisplay "birdseye end";
5419 | Delete|Insert|Ascii _|Code _|Ctrl _|Fn _|Backspace -> viewkeyboard key mask
5422 let drawpage l =
5423 let color =
5424 match state.mode with
5425 | Textentry _ -> scalecolor 0.4
5426 | LinkNav _ | View -> scalecolor 1.0
5427 | Birdseye (_, _, pageno, hooverpageno, _) ->
5428 if l.pageno = hooverpageno
5429 then scalecolor 0.9
5430 else (
5431 if l.pageno = pageno
5432 then (
5433 let c = scalecolor 1.0 in
5434 GlDraw.color c;
5435 GlDraw.line_width 3.0;
5436 let dispx = l.pagedispx in
5437 linerect
5438 (float (dispx-1)) (float (l.pagedispy-1))
5439 (float (dispx+l.pagevw+1))
5440 (float (l.pagedispy+l.pagevh+1))
5442 GlDraw.line_width 1.0;
5445 else scalecolor 0.8
5448 drawtiles l color;
5451 let postdrawpage l linkindexbase =
5452 match getopaque l.pageno with
5453 | Some opaque ->
5454 if tileready l l.pagex l.pagey
5455 then
5456 let x = l.pagedispx - l.pagex
5457 and y = l.pagedispy - l.pagey in
5458 let hlmask =
5459 match conf.columns with
5460 | Csingle _ | Cmulti _ ->
5461 (if conf.hlinks then 1 else 0)
5462 + (if state.glinks
5463 && not (isbirdseye state.mode) then 2 else 0)
5464 | Csplit _ -> 0
5466 let s =
5467 match state.mode with
5468 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5469 | Textentry _
5470 | Birdseye _
5471 | View
5472 | LinkNav _ -> E.s
5474 Hashtbl.find_all state.prects l.pageno |>
5475 List.iter (fun vals -> drawprect opaque x y vals);
5476 let n = postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize) in
5477 if n < 0
5478 then (state.redisplay <- true; 0)
5479 else n
5480 else 0
5481 | _ -> 0
5484 let scrollindicator () =
5485 let sbw, ph, sh = state.uioh#scrollph in
5486 let sbh, pw, sw = state.uioh#scrollpw in
5488 let x0,x1,hx0 =
5489 if conf.leftscroll
5490 then (0, sbw, sbw)
5491 else ((state.winw - sbw), state.winw, 0)
5494 Gl.enable `blend;
5495 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5496 let (r, g, b, alpha) = conf.sbarcolor in
5497 GlDraw.color (r, g, b) ~alpha;
5498 filledrect (float x0) 0. (float x1) (float state.winh);
5499 filledrect
5500 (float hx0) (float (state.winh - sbh))
5501 (float (hx0 + state.winw)) (float state.winh);
5502 let (r, g, b, alpha) = conf.sbarhndlcolor in
5503 GlDraw.color (r, g, b) ~alpha;
5505 filledrect (float x0) ph (float x1) (ph +. sh);
5506 let pw = pw +. float hx0 in
5507 filledrect pw (float (state.winh - sbh)) (pw +. sw) (float state.winh);
5508 Gl.disable `blend;
5511 let showsel () =
5512 match state.mstate with
5513 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5516 | Msel ((x0, y0), (x1, y1)) ->
5517 let identify opaque l px py = Some (opaque, l.pageno, px, py) in
5518 let o0,n0,px0,py0 = onppundermouse identify x0 y0 (~< E.s, -1, 0, 0) in
5519 let _o1,n1,px1,py1 = onppundermouse identify x1 y1 (~< E.s, -1, 0, 0) in
5520 if n0 != -1 && n0 = n1 then seltext o0 (px0, py0, px1, py1);
5523 let showrects =
5524 function [] -> ()
5525 | rects ->
5526 Gl.enable `blend;
5527 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5528 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5529 List.iter
5530 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5531 List.iter (fun l ->
5532 if l.pageno = pageno
5533 then (
5534 let dx = float (l.pagedispx - l.pagex) in
5535 let dy = float (l.pagedispy - l.pagey) in
5536 let r, g, b, alpha = c in
5537 GlDraw.color (r, g, b) ~alpha;
5538 filledrect2 (x0+.dx) (y0+.dy)
5539 (x1+.dx) (y1+.dy)
5540 (x3+.dx) (y3+.dy)
5541 (x2+.dx) (y2+.dy);
5543 ) state.layout
5544 ) rects;
5545 Gl.disable `blend;
5548 let display () =
5549 GlDraw.color (scalecolor2 conf.bgcolor);
5550 GlClear.color (scalecolor2 conf.bgcolor);
5551 GlClear.clear [`color];
5552 List.iter drawpage state.layout;
5553 let rects =
5554 match state.mode with
5555 | LinkNav (Ltexact (pageno, linkno)) ->
5556 begin match getopaque pageno with
5557 | Some opaque ->
5558 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5559 let color = (0.0, 0.0, 0.5, 0.5) in
5560 (pageno, color,
5561 (float x0, float y0,
5562 float x1, float y0,
5563 float x1, float y1,
5564 float x0, float y1)
5565 ) :: state.rects
5566 | None -> state.rects
5568 | LinkNav (Ltgendir _) | LinkNav (Ltnotready _)
5569 | Birdseye _
5570 | Textentry _
5571 | View -> state.rects
5573 showrects rects;
5574 let rec postloop linkindexbase = function
5575 | l :: rest ->
5576 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5577 postloop linkindexbase rest
5578 | [] -> ()
5580 showsel ();
5581 postloop 0 state.layout;
5582 state.uioh#display;
5583 begin match state.mstate with
5584 | Mzoomrect ((x0, y0), (x1, y1)) ->
5585 Gl.enable `blend;
5586 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5587 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5588 filledrect (float x0) (float y0) (float x1) (float y1);
5589 Gl.disable `blend;
5590 | Msel _
5591 | Mpan _
5592 | Mscrolly | Mscrollx
5593 | Mzoom _
5594 | Mnone -> ()
5595 end;
5596 enttext ();
5597 scrollindicator ();
5598 Wsi.swapb ();
5601 let zoomrect x y x1 y1 =
5602 let x0 = min x x1
5603 and x1 = max x x1
5604 and y0 = min y y1 in
5605 let zoom = (float state.w) /. float (x1 - x0) in
5606 let margin =
5607 let simple () =
5608 if state.w < state.winw
5609 then (state.winw - state.w) / 2
5610 else 0
5612 match conf.fitmodel with
5613 | FitWidth | FitProportional -> simple ()
5614 | FitPage ->
5615 match conf.columns with
5616 | Csplit _ ->
5617 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
5618 | Cmulti _ | Csingle _ -> simple ()
5620 gotoxy ((state.x + margin) - x0) (state.y + y0);
5621 state.anchor <- getanchor ();
5622 setzoom zoom;
5623 resetmstate ();
5626 let annot inline x y =
5627 match unproject x y with
5628 | Some (opaque, n, ux, uy) ->
5629 let add text =
5630 addannot opaque ux uy text;
5631 wcmd "freepage %s" (~> opaque);
5632 Hashtbl.remove state.pagemap (n, state.gen);
5633 flushtiles ();
5634 gotoxy state.x state.y
5636 if inline
5637 then
5638 let ondone s = add s in
5639 let mode = state.mode in
5640 state.mode <- Textentry (
5641 ("annotation: ", E.s, None, textentry, ondone, true),
5642 fun _ -> state.mode <- mode);
5643 state.text <- E.s;
5644 enttext ();
5645 G.postRedisplay "annot"
5646 else
5647 add @@ getusertext E.s
5648 | _ -> ()
5651 let zoomblock x y =
5652 let g opaque l px py =
5653 match rectofblock opaque px py with
5654 | Some a ->
5655 let x0 = a.(0) -. 20. in
5656 let x1 = a.(1) +. 20. in
5657 let y0 = a.(2) -. 20. in
5658 let zoom = (float state.w) /. (x1 -. x0) in
5659 let pagey = getpagey l.pageno in
5660 let margin = (state.w - l.pagew)/2 in
5661 let nx = -truncate x0 - margin in
5662 gotoxy_and_clear_text nx (pagey + truncate y0);
5663 state.anchor <- getanchor ();
5664 setzoom zoom;
5665 None
5666 | None -> None
5668 match conf.columns with
5669 | Csplit _ ->
5670 impmsg "block zooming does not work properly in split columns mode"
5671 | Cmulti _ | Csingle _ -> onppundermouse g x y ()
5674 let scrollx x =
5675 let winw = state.winw - 1 in
5676 let s = float x /. float winw in
5677 let destx = truncate (float (state.w + winw) *. s) in
5678 gotoxy_and_clear_text (winw - destx) state.y;
5679 state.mstate <- Mscrollx;
5682 let scrolly y =
5683 let s = float y /. float state.winh in
5684 let desty = truncate (s *. float (maxy ())) in
5685 gotoxy_and_clear_text state.x desty;
5686 state.mstate <- Mscrolly;
5689 let viewmulticlick clicks x y mask =
5690 let g opaque l px py =
5691 let mark =
5692 match clicks with
5693 | 2 -> Mark_word
5694 | 3 -> Mark_line
5695 | 4 -> Mark_block
5696 | _ -> Mark_page
5698 if markunder opaque px py mark
5699 then (
5700 Some (fun () ->
5701 let dopipe cmd =
5702 match getopaque l.pageno with
5703 | None -> ()
5704 | Some opaque -> pipesel opaque cmd
5706 state.roam <- (fun () -> dopipe conf.paxcmd);
5707 if not (Wsi.withctrl mask) then dopipe conf.selcmd;
5710 else None
5712 G.postRedisplay "viewmulticlick";
5713 onppundermouse g x y (fun () -> impmsg "nothing to select") ();
5716 let canselect () =
5717 match conf.columns with
5718 | Csplit _ -> false
5719 | Csingle _ | Cmulti _ -> conf.angle mod 360 = 0
5722 let viewmouse button down x y mask =
5723 match button with
5724 | n when (n == 4 || n == 5) && not down ->
5725 if Wsi.withctrl mask
5726 then (
5727 let incr =
5728 if n = 5
5729 then if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5730 else if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5732 let fx, fy =
5733 match state.mstate with
5734 | Mzoom (oldn, _, pos) when n = oldn -> pos
5735 | Mzoomrect _ | Mnone | Mpan _
5736 | Msel _ | Mscrollx | Mscrolly | Mzoom _ -> (x, y)
5738 let zoom = conf.zoom -. incr in
5739 state.mstate <- Mzoom (n, 0, (x, y));
5740 if false && abs (fx - x) > 5 || abs (fy - y) > 5
5741 then pivotzoom ~x ~y zoom
5742 else pivotzoom zoom
5744 else (
5745 match state.autoscroll with
5746 | Some step -> setautoscrollspeed step (n=4)
5747 | None ->
5748 if conf.wheelbypage || conf.presentation
5749 then (
5750 if n = 4
5751 then prevpage ()
5752 else nextpage ()
5754 else
5755 let incr =
5756 if n = 4
5757 then -conf.scrollstep
5758 else conf.scrollstep
5760 let incr = incr * 2 in
5761 let y = clamp incr in
5762 gotoxy_and_clear_text state.x y
5765 | n when (n = 6 || n = 7) && not down && canpan () ->
5766 let x =
5767 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep) in
5768 gotoxy_and_clear_text x state.y
5770 | 1 when Wsi.withshift mask ->
5771 state.mstate <- Mnone;
5772 if not down
5773 then (
5774 match unproject x y with
5775 | None -> ()
5776 | Some (_, pageno, ux, uy) ->
5777 let cmd = Printf.sprintf
5778 "%s %s %d %d %d"
5779 conf.stcmd state.path pageno ux uy
5781 match spawn cmd [] with
5782 | exception exn ->
5783 impmsg "execution of synctex command(%S) failed: %S"
5784 conf.stcmd @@ exntos exn
5785 | _pid -> ()
5788 | 1 when Wsi.withctrl mask ->
5789 if down
5790 then (
5791 Wsi.setcursor Wsi.CURSOR_FLEUR;
5792 state.mstate <- Mpan (x, y)
5794 else
5795 state.mstate <- Mnone
5797 | 3 ->
5798 if down
5799 then (
5800 if Wsi.withshift mask
5801 then (
5802 annot conf.annotinline x y;
5803 G.postRedisplay "addannot"
5805 else
5806 let p = (x, y) in
5807 Wsi.setcursor Wsi.CURSOR_CYCLE;
5808 state.mstate <- Mzoomrect (p, p)
5810 else (
5811 match state.mstate with
5812 | Mzoomrect ((x0, y0), _) ->
5813 if abs (x-x0) > 10 && abs (y - y0) > 10
5814 then zoomrect x0 y0 x y
5815 else (
5816 resetmstate ();
5817 G.postRedisplay "kill accidental zoom rect";
5819 | Msel _
5820 | Mpan _
5821 | Mscrolly | Mscrollx
5822 | Mzoom _
5823 | Mnone -> resetmstate ()
5826 | 1 when vscrollhit x ->
5827 if down
5828 then
5829 let _, position, sh = state.uioh#scrollph in
5830 if y > truncate position && y < truncate (position +. sh)
5831 then state.mstate <- Mscrolly
5832 else scrolly y
5833 else
5834 state.mstate <- Mnone
5836 | 1 when y > state.winh - hscrollh () ->
5837 if down
5838 then
5839 let _, position, sw = state.uioh#scrollpw in
5840 if x > truncate position && x < truncate (position +. sw)
5841 then state.mstate <- Mscrollx
5842 else scrollx x
5843 else
5844 state.mstate <- Mnone
5846 | 1 when state.bzoom -> if not down then zoomblock x y
5848 | 1 ->
5849 let dest = if down then getunder x y else Unone in
5850 begin match dest with
5851 | Ulinkuri _ ->
5852 gotounder dest
5854 | Unone when down ->
5855 Wsi.setcursor Wsi.CURSOR_FLEUR;
5856 state.mstate <- Mpan (x, y);
5858 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
5860 | Unone | Utext _ ->
5861 if down
5862 then (
5863 if canselect ()
5864 then (
5865 state.mstate <- Msel ((x, y), (x, y));
5866 G.postRedisplay "mouse select";
5869 else (
5870 match state.mstate with
5871 | Mnone -> ()
5873 | Mzoom _ | Mscrollx | Mscrolly ->
5874 state.mstate <- Mnone
5876 | Mzoomrect ((x0, y0), _) ->
5877 zoomrect x0 y0 x y
5879 | Mpan _ ->
5880 Wsi.setcursor Wsi.CURSOR_INHERIT;
5881 state.mstate <- Mnone
5883 | Msel ((x0, y0), (x1, y1)) ->
5884 let rec loop = function
5885 | [] -> ()
5886 | l :: rest ->
5887 let inside =
5888 let a0 = l.pagedispy in
5889 let a1 = a0 + l.pagevh in
5890 let b0 = l.pagedispx in
5891 let b1 = b0 + l.pagevw in
5892 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5893 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5895 if inside
5896 then
5897 match getopaque l.pageno with
5898 | Some opaque ->
5899 let dosel cmd () =
5900 pipef ~closew:false "Msel"
5901 (fun w ->
5902 copysel w opaque;
5903 G.postRedisplay "Msel") cmd
5905 dosel conf.selcmd ();
5906 state.roam <- dosel conf.paxcmd;
5907 | None -> ()
5908 else loop rest
5910 loop state.layout;
5911 resetmstate ();
5915 | _ -> ()
5918 let birdseyemouse button down x y mask
5919 (conf, leftx, _, hooverpageno, anchor) =
5920 match button with
5921 | 1 when down ->
5922 let rec loop = function
5923 | [] -> ()
5924 | l :: rest ->
5925 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5926 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5927 then (
5928 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5930 else loop rest
5932 loop state.layout
5933 | 3 -> ()
5934 | _ -> viewmouse button down x y mask
5937 let uioh = object
5938 method display = ()
5940 method key key mask =
5941 begin match state.mode with
5942 | Textentry textentry -> textentrykeyboard key mask textentry
5943 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5944 | View -> viewkeyboard key mask
5945 | LinkNav linknav -> linknavkeyboard key mask linknav
5946 end;
5947 state.uioh
5949 method button button bstate x y mask =
5950 begin match state.mode with
5951 | LinkNav _ | View -> viewmouse button bstate x y mask
5952 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5953 | Textentry _ -> ()
5954 end;
5955 state.uioh
5957 method multiclick clicks x y mask =
5958 begin match state.mode with
5959 | LinkNav _ | View -> viewmulticlick clicks x y mask
5960 | Birdseye _ | Textentry _ -> ()
5961 end;
5962 state.uioh
5964 method motion x y =
5965 begin match state.mode with
5966 | Textentry _ -> ()
5967 | View | Birdseye _ | LinkNav _ ->
5968 match state.mstate with
5969 | Mzoom _ | Mnone -> ()
5971 | Mpan (x0, y0) ->
5972 let dx = x - x0
5973 and dy = y0 - y in
5974 state.mstate <- Mpan (x, y);
5975 let x = if canpan () then panbound (state.x + dx) else state.x in
5976 let y = clamp dy in
5977 gotoxy_and_clear_text x y
5979 | Msel (a, _) ->
5980 state.mstate <- Msel (a, (x, y));
5981 G.postRedisplay "motion select";
5983 | Mscrolly ->
5984 let y = min state.winh (max 0 y) in
5985 scrolly y
5987 | Mscrollx ->
5988 let x = min state.winw (max 0 x) in
5989 scrollx x
5991 | Mzoomrect (p0, _) ->
5992 state.mstate <- Mzoomrect (p0, (x, y));
5993 G.postRedisplay "motion zoomrect";
5994 end;
5995 state.uioh
5997 method pmotion x y =
5998 begin match state.mode with
5999 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6000 let rec loop = function
6001 | [] ->
6002 if hooverpageno != -1
6003 then (
6004 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6005 G.postRedisplay "pmotion birdseye no hoover";
6007 | l :: rest ->
6008 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6009 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6010 then (
6011 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6012 G.postRedisplay "pmotion birdseye hoover";
6014 else loop rest
6016 loop state.layout
6018 | Textentry _ -> ()
6020 | LinkNav _ | View ->
6021 match state.mstate with
6022 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ -> ()
6023 | Mnone ->
6024 updateunder x y;
6025 if canselect ()
6026 then
6027 match conf.pax with
6028 | None -> ()
6029 | Some past ->
6030 let now = now () in
6031 let delta = now -. past in
6032 if delta > 0.01
6033 then paxunder x y
6034 else conf.pax <- Some now
6035 end;
6036 state.uioh
6038 method infochanged _ = ()
6040 method scrollph =
6041 let maxy = maxy () in
6042 let p, h =
6043 if maxy = 0
6044 then 0.0, float state.winh
6045 else scrollph state.y maxy
6047 vscrollw (), p, h
6049 method scrollpw =
6050 let fwinw = float (state.winw - vscrollw ()) in
6051 let sw =
6052 let sw = fwinw /. float state.w in
6053 let sw = fwinw *. sw in
6054 max sw (float conf.scrollh)
6056 let position =
6057 let maxx = state.w + state.winw in
6058 let x = state.winw - state.x in
6059 let percent = float x /. float maxx in
6060 (fwinw -. sw) *. percent
6062 hscrollh (), position, sw
6064 method modehash =
6065 let modename =
6066 match state.mode with
6067 | LinkNav _ -> "links"
6068 | Textentry _ -> "textentry"
6069 | Birdseye _ -> "birdseye"
6070 | View -> "view"
6072 findkeyhash conf modename
6074 method eformsgs = true
6075 method alwaysscrolly = false
6076 method scroll dx dy =
6077 let x = if canpan () then panbound (state.x + dx) else state.x in
6078 gotoxy_and_clear_text x (clamp (2 * dy));
6079 state.uioh
6080 method zoom z x y =
6081 pivotzoom ~x ~y (conf.zoom *. exp z);
6082 end;;
6084 let addrect pageno r g b a x0 y0 x1 y1 =
6085 Hashtbl.add state.prects pageno [|r; g; b; a; x0; y0; x1; y1|];
6088 let ract cmds =
6089 let cl = splitatchar cmds ' ' in
6090 let scan s fmt f =
6091 try Scanf.sscanf s fmt f
6092 with exn -> adderrfmt "remote exec" "error processing '%S': %s\n"
6093 cmds @@ exntos exn
6095 let rectx s pageno (r, g, b, a) x0 y0 x1 y1 =
6096 vlog "%s page %d color (%f %f %f %f) x0,y0,x1,y1 = %f %f %f %f"
6097 s pageno r g b a x0 y0 x1 y1;
6098 onpagerect
6099 pageno
6100 (fun w h ->
6101 let _,w1,h1,_ = getpagedim pageno in
6102 let sw = float w1 /. float w
6103 and sh = float h1 /. float h in
6104 let x0s = x0 *. sw
6105 and x1s = x1 *. sw
6106 and y0s = y0 *. sh
6107 and y1s = y1 *. sh in
6108 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
6109 let color = (r, g, b, a) in
6110 if conf.verbose then debugrect rect;
6111 state.rects <- (pageno, color, rect) :: state.rects;
6112 G.postRedisplay s;
6115 match cl with
6116 | "reload", "" -> reload ()
6117 | "goto", args ->
6118 scan args "%u %f %f"
6119 (fun pageno x y ->
6120 let cmd, _ = state.geomcmds in
6121 if emptystr cmd
6122 then gotopagexy !wtmode pageno x y
6123 else
6124 let f prevf () =
6125 gotopagexy !wtmode pageno x y;
6126 prevf ()
6128 state.reprf <- f state.reprf
6130 | "goto1", args -> scan args "%u %f" gotopage
6131 | "gotor", args -> scan args "%S" gotoremote
6132 | "rect", args ->
6133 scan args "%u %u %f %f %f %f"
6134 (fun pageno c x0 y0 x1 y1 ->
6135 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
6136 rectx "rect" pageno color x0 y0 x1 y1;
6138 | "prect", args ->
6139 scan args "%u %f %f %f %f %f %f %f %f"
6140 (fun pageno r g b alpha x0 y0 x1 y1 ->
6141 addrect pageno r g b alpha x0 y0 x1 y1;
6142 G.postRedisplay "prect"
6144 | "pgoto", args ->
6145 scan args "%u %f %f"
6146 (fun pageno x y ->
6147 let optopaque =
6148 match getopaque pageno with
6149 | Some opaque -> opaque
6150 | None -> ~< E.s
6152 pgoto optopaque pageno x y;
6153 let rec fixx = function
6154 | [] -> ()
6155 | l :: rest ->
6156 if l.pageno = pageno
6157 then gotoxy (state.x - l.pagedispx) state.y
6158 else fixx rest
6160 let layout =
6161 let mult =
6162 match conf.columns with
6163 | Csingle _ | Csplit _ -> 1
6164 | Cmulti ((n, _, _), _) -> n
6166 layout 0 state.y (state.winw * mult) state.winh
6168 fixx layout
6170 | "activatewin", "" -> Wsi.activatewin ()
6171 | "quit", "" -> raise Quit
6172 | "keys", keys ->
6173 begin try
6174 let l = Config.keys_of_string keys in
6175 List.iter (fun (k, m) -> keyboard k m) l
6176 with exn -> adderrfmt "error processing keys" "`%S': %s\n"
6177 cmds @@ exntos exn
6179 | "clearrects", "" ->
6180 Hashtbl.clear state.prects;
6181 G.postRedisplay "clearrects"
6182 | _ ->
6183 adderrfmt "remote command"
6184 "error processing remote command: %S\n" cmds;
6187 let remote =
6188 let scratch = Bytes.create 80 in
6189 let buf = Buffer.create 80 in
6190 fun fd ->
6191 match tempfailureretry (Unix.read fd scratch 0) 80 with
6192 | exception Unix.Unix_error (Unix.EAGAIN, _, _) -> None
6193 | 0 ->
6194 Unix.close fd;
6195 if Buffer.length buf > 0
6196 then (
6197 let s = Buffer.contents buf in
6198 Buffer.clear buf;
6199 ract s;
6201 None
6202 | n ->
6203 let rec eat ppos =
6204 let nlpos =
6205 match Bytes.index_from scratch ppos '\n' with
6206 | pos -> if pos >= n then -1 else pos
6207 | exception Not_found -> -1
6209 if nlpos >= 0
6210 then (
6211 Buffer.add_subbytes buf scratch ppos (nlpos-ppos);
6212 let s = Buffer.contents buf in
6213 Buffer.clear buf;
6214 ract s;
6215 eat (nlpos+1);
6217 else (
6218 Buffer.add_subbytes buf scratch ppos (n-ppos);
6219 Some fd
6221 in eat 0
6224 let remoteopen path =
6225 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
6226 with exn ->
6227 adderrfmt "remoteopen" "error opening %S: %s" path @@ exntos exn;
6228 None
6231 let () =
6232 let gcconfig = ref false in
6233 let trimcachepath = ref E.s in
6234 let rcmdpath = ref E.s in
6235 let pageno = ref None in
6236 let rootwid = ref 0 in
6237 let openlast = ref false in
6238 let doreap = ref false in
6239 let csspath = ref None in
6240 selfexec := Sys.executable_name;
6241 Arg.parse
6242 (Arg.align
6243 [("-p", Arg.String (fun s -> state.password <- s),
6244 "<password> Set password");
6246 ("-f", Arg.String
6247 (fun s ->
6248 Config.fontpath := s;
6249 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
6251 "<path> Set path to the user interface font");
6253 ("-c", Arg.String
6254 (fun s ->
6255 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
6256 Config.confpath := s),
6257 "<path> Set path to the configuration file");
6259 ("-last", Arg.Set openlast, " Open last document");
6261 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
6262 "<page-number> Jump to page");
6264 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6265 "<path> Set path to the trim cache file");
6267 ("-dest", Arg.String (fun s -> state.nameddest <- s),
6268 "<named-destination> Set named destination");
6270 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
6271 ("-cxack", Arg.Set cxack, " Cut corners");
6273 ("-remote", Arg.String (fun s -> rcmdpath := s),
6274 "<path> Set path to the source of remote commands");
6276 ("-gc", Arg.Set gcconfig, " Collect config garbage");
6278 ("-v", Arg.Unit (fun () ->
6279 Printf.printf
6280 "%s\nconfiguration path: %s\n"
6281 (version ())
6282 Config.defconfpath;
6283 exit 0), " Print version and exit");
6285 ("-css", Arg.String (fun s -> csspath := Some s),
6286 "<path> Set path to the style sheet to use with EPUB/HTML");
6288 ("-embed", Arg.Set_int rootwid, "<window-id> Embed into window");
6290 ("-origin", Arg.String (fun s -> state.origin <- s),
6291 "<origin> <undocumented>");
6293 ("-no-title", Arg.Set ignoredoctitlte, " ignore document title")
6296 (fun s -> state.path <- s)
6297 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:");
6299 if !wtmode
6300 then selfexec := !selfexec ^ " -wtmode";
6302 let histmode = emptystr state.path && not !openlast in
6304 if not (Config.load !openlast)
6305 then dolog "failed to load configuration";
6307 begin match !pageno with
6308 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
6309 | None -> ()
6310 end;
6312 fillhelp ();
6313 if !gcconfig
6314 then (
6315 Config.gc ();
6316 exit 0
6319 let mu =
6320 object (self)
6321 val mutable m_clicks = 0
6322 val mutable m_click_x = 0
6323 val mutable m_click_y = 0
6324 val mutable m_lastclicktime = infinity
6326 method private cleanup =
6327 state.roam <- noroam;
6328 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap
6329 method expose = G.postRedisplay "expose"
6330 method visible v =
6331 let name =
6332 match v with
6333 | Wsi.Unobscured -> "unobscured"
6334 | Wsi.PartiallyObscured -> "partiallyobscured"
6335 | Wsi.FullyObscured -> "fullyobscured"
6337 vlog "visibility change %s" name
6338 method display = display ()
6339 method map mapped = vlog "mapped %b" mapped
6340 method reshape w h =
6341 self#cleanup;
6342 reshape w h
6343 method mouse b d x y m =
6344 if d && canselect ()
6345 then (
6347 * http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx
6349 m_click_x <- x;
6350 m_click_y <- y;
6351 if b = 1
6352 then (
6353 let t = now () in
6354 if abs x - m_click_x > 10
6355 || abs y - m_click_y > 10
6356 || abs_float (t -. m_lastclicktime) > 0.3
6357 then m_clicks <- 0;
6358 m_clicks <- m_clicks + 1;
6359 m_lastclicktime <- t;
6360 if m_clicks = 1
6361 then (
6362 self#cleanup;
6363 G.postRedisplay "cleanup";
6364 state.uioh <- state.uioh#button b d x y m;
6366 else state.uioh <- state.uioh#multiclick m_clicks x y m
6368 else (
6369 self#cleanup;
6370 m_clicks <- 0;
6371 m_lastclicktime <- infinity;
6372 state.uioh <- state.uioh#button b d x y m
6375 else (
6376 state.uioh <- state.uioh#button b d x y m
6378 method motion x y =
6379 state.mpos <- (x, y);
6380 state.uioh <- state.uioh#motion x y
6381 method pmotion x y =
6382 state.mpos <- (x, y);
6383 state.uioh <- state.uioh#pmotion x y
6384 method key k m =
6385 vlog "k=%#x m=%#x" k m;
6386 let mascm = m land (
6387 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6388 ) in
6389 let keyboard k m =
6390 let x = state.x and y = state.y in
6391 keyboard k m;
6392 if x != state.x || y != state.y then self#cleanup
6394 match state.keystate with
6395 | KSnone ->
6396 let km = k, mascm in
6397 begin
6398 match
6399 let modehash = state.uioh#modehash in
6400 try Hashtbl.find modehash km
6401 with Not_found ->
6402 try Hashtbl.find (findkeyhash conf "global") km
6403 with Not_found -> KMinsrt (k, m)
6404 with
6405 | KMinsrt (k, m) -> keyboard k m
6406 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6407 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6409 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6410 List.iter (fun (k, m) -> keyboard k m) insrt;
6411 state.keystate <- KSnone
6412 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6413 state.keystate <- KSinto (keys, insrt)
6414 | KSinto _ -> state.keystate <- KSnone
6416 method enter x y =
6417 state.mpos <- (x, y);
6418 state.uioh <- state.uioh#pmotion x y
6419 method leave = state.mpos <- (-1, -1)
6420 method winstate wsl = state.winstate <- wsl
6421 method quit : 'a. 'a = raise Quit
6422 method scroll dx dy = state.uioh <- state.uioh#scroll dx dy
6423 method zoom z x y = state.uioh#zoom z x y
6424 method opendoc path =
6425 state.mode <- View;
6426 state.uioh <- uioh;
6427 G.postRedisplay "opendoc";
6428 opendoc path state.password
6431 let wsfd, winw, winh = Wsi.init mu !rootwid conf.cwinw conf.cwinh platform in
6432 state.wsfd <- wsfd;
6434 if not @@ List.exists GlMisc.check_extension
6435 [ "GL_ARB_texture_rectangle"
6436 ; "GL_EXT_texture_recangle"
6437 ; "GL_NV_texture_rectangle" ]
6438 then (dolog "OpenGL does not suppport rectangular textures"; exit 1);
6440 if substratis (GlMisc.get_string `renderer) 0 "Mesa DRI Intel("
6441 then (
6442 defconf.sliceheight <- 1024;
6443 defconf.texcount <- 32;
6444 defconf.usepbo <- true;
6447 let cs, ss =
6448 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6449 | exception exn ->
6450 dolog "socketpair failed: %s" @@ exntos exn;
6451 exit 1
6452 | (r, w) ->
6453 cloexec r;
6454 cloexec w;
6455 r, w
6458 setcheckers conf.checkers;
6460 opengl_has_pbo := GlMisc.check_extension "GL_ARB_pixel_buffer_object";
6462 begin match !csspath with
6463 | None -> ()
6464 | Some "" -> conf.css <- E.s
6465 | Some path ->
6466 let css = filecontents path in
6467 let l = String.length css in
6468 conf.css <-
6469 if substratis css (l-2) "\r\n"
6470 then String.sub css 0 (l-2)
6471 else (if css.[l-1] = '\n'
6472 then String.sub css 0 (l-1)
6473 else css);
6474 end;
6475 init cs (
6476 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
6477 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6478 !Config.fontpath, !trimcachepath, !opengl_has_pbo
6480 List.iter GlArray.enable [`texture_coord; `vertex];
6481 state.ss <- ss;
6482 reshape ~firsttime:true winw winh;
6483 state.uioh <- uioh;
6484 if histmode
6485 then (
6486 Wsi.settitle "llpp (history)";
6487 enterhistmode ();
6489 else (
6490 state.text <- "Opening " ^ (mbtoutf8 state.path);
6491 opendoc state.path state.password;
6493 display ();
6494 Wsi.mapwin ();
6495 Wsi.setcursor Wsi.CURSOR_INHERIT;
6496 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
6498 let rec reap () =
6499 match Unix.waitpid [Unix.WNOHANG] ~-1 with
6500 | exception (Unix.Unix_error (Unix.ECHILD, _, _)) -> ()
6501 | exception exn -> dolog "Unix.waitpid: %s" @@ exntos exn
6502 | 0, _ -> ()
6503 | _pid, _status -> reap ()
6505 Sys.set_signal Sys.sigchld (Sys.Signal_handle (fun _ -> doreap := true));
6507 let optrfd =
6508 ref (
6509 if nonemptystr !rcmdpath
6510 then remoteopen !rcmdpath
6511 else None
6515 let rec loop deadline =
6516 if !doreap
6517 then (
6518 doreap := false;
6519 reap ()
6521 let r = [state.ss; state.wsfd] in
6522 let r =
6523 match !optrfd with
6524 | None -> r
6525 | Some fd -> fd :: r
6527 if state.redisplay
6528 then (
6529 state.redisplay <- false;
6530 display ();
6532 let timeout =
6533 let now = now () in
6534 if deadline > now
6535 then (
6536 if deadline = infinity
6537 then ~-.1.0
6538 else max 0.0 (deadline -. now)
6540 else 0.0
6542 let r, _, _ =
6543 try Unix.select r [] [] timeout
6544 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6546 begin match r with
6547 | [] ->
6548 state.ghyll None;
6549 let newdeadline =
6550 if state.ghyll == noghyll
6551 then
6552 match state.autoscroll with
6553 | Some step when step != 0 ->
6554 if state.slideshow land 1 = 1
6555 then (
6556 if state.slideshow land 2 = 0
6557 then state.slideshow <- state.slideshow lor 2
6558 else if step < 0 then prevpage () else nextpage ();
6559 deadline +. (float (abs step))
6561 else
6562 let y = state.y + step in
6563 let fy = if conf.maxhfit then state.winh else 0 in
6564 let y =
6565 if y < 0
6566 then state.maxy - fy
6567 else if y >= state.maxy - fy then 0 else y
6569 if state.mode = View
6570 then gotoxy_and_clear_text state.x y
6571 else gotoxy state.x y;
6572 deadline +. 0.01
6573 | _ -> infinity
6574 else deadline +. 0.01
6576 loop newdeadline
6578 | l ->
6579 let rec checkfds = function
6580 | [] -> ()
6581 | fd :: rest when fd = state.ss ->
6582 let cmd = rcmd state.ss in
6583 act cmd;
6584 checkfds rest
6586 | fd :: rest when fd = state.wsfd ->
6587 Wsi.readresp fd;
6588 checkfds rest
6590 | fd :: rest when Some fd = !optrfd ->
6591 begin match remote fd with
6592 | None -> optrfd := remoteopen !rcmdpath;
6593 | opt -> optrfd := opt
6594 end;
6595 checkfds rest
6597 | _ :: rest ->
6598 dolog "select returned unknown descriptor";
6599 checkfds rest
6601 checkfds l;
6602 let newdeadline =
6603 let deadline1 =
6604 if deadline = infinity
6605 then now () +. 0.01
6606 else deadline
6608 match state.autoscroll with
6609 | Some step when step != 0 -> deadline1
6610 | _ -> if state.ghyll == noghyll then infinity else deadline1
6612 loop newdeadline
6613 end;
6615 match loop infinity with
6616 | exception Quit ->
6617 Config.save leavebirdseye;
6618 if hasunsavedchanges ()
6619 then save ()
6620 | _ -> error "umpossible - infinity reached"