Remove throttling
[llpp.git] / main.ml
blob3912623c12c65c974a39900c4f30b91b5d771b15
1 open Utils;;
2 open Config;;
3 open Glutils;;
5 external init : Unix.file_descr -> initparams -> unit = "ml_init";;
6 external seltext : opaque -> (int * int * int * int) -> unit = "ml_seltext";;
7 external hassel : opaque -> bool = "ml_hassel";;
8 external getpdimrect : int -> float array = "ml_getpdimrect";;
9 external whatsunder : opaque -> int -> int -> under = "ml_whatsunder";;
10 external markunder : opaque -> int -> int -> mark -> bool = "ml_markunder";;
11 external clearmark : opaque -> unit = "ml_clearmark";;
12 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
13 external getmaxw : unit -> float = "ml_getmaxw";;
14 external postprocess : opaque -> int -> int -> int -> (int * string * int)
15 -> int = "ml_postprocess";;
16 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
17 external setaalevel : int -> unit = "ml_setaalevel";;
18 external realloctexts : int -> bool = "ml_realloctexts";;
19 external findlink : opaque -> linkdir -> link = "ml_findlink";;
20 external getlink : opaque -> int -> under = "ml_getlink";;
21 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
22 external getlinkcount : opaque -> int = "ml_getlinkcount";;
23 external findpwl : int -> int -> pagewithlinks = "ml_find_page_with_links";;
24 external getpbo : width -> height -> colorspace -> opaque = "ml_getpbo";;
25 external freepbo : opaque -> unit = "ml_freepbo";;
26 external unmappbo : opaque -> unit = "ml_unmappbo";;
27 external bousable : unit -> bool = "ml_bo_usable";;
28 external unproject : opaque -> int -> int
29 -> (int * int) option = "ml_unproject";;
30 external project : opaque -> int -> int -> float -> float
31 -> (float * float) = "ml_project";;
32 external drawtile : tileparams -> opaque
33 -> unit = "ml_drawtile";;
34 external rectofblock : opaque -> int -> int
35 -> float array option = "ml_rectofblock";;
36 external begintiles : unit -> unit = "ml_begintiles";;
37 external endtiles : unit -> unit = "ml_endtiles";;
38 external addannot : opaque -> int -> int -> string -> unit = "ml_addannot";;
39 external modannot : opaque -> slinkindex -> string -> unit = "ml_modannot";;
40 external delannot : opaque -> slinkindex -> unit = "ml_delannot";;
41 external hasunsavedchanges : unit -> bool = "ml_hasunsavedchanges";;
42 external savedoc : string -> unit = "ml_savedoc";;
43 external getannotcontents : opaque -> slinkindex
44 -> string = "ml_getannotcontents";;
45 external drawprect : opaque -> int -> int -> float array
46 -> unit = "ml_drawprect";;
47 external wcmd : Unix.file_descr -> bytes -> int -> unit = "ml_wcmd";;
48 external rcmd : Unix.file_descr -> string = "ml_rcmd";;
49 external uritolocation : string
50 -> (pageno * float * float) = "ml_uritolocation";;
51 external isexternallink : string -> bool = "ml_isexternallink";;
53 (* copysel _will_ close the supplied descriptor *)
54 external copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
56 let selfexec = ref E.s;;
57 let ignoredoctitlte = ref false;;
58 let opengl_has_pbo = ref false;;
59 let layouth = ref ~-1;;
61 let _debugl l =
62 dolog {|l %d dim=%d {
63 WxH %dx%d
64 vWxH %dx%d
65 pagex,y %d,%d
66 dispx,y %d,%d
67 column %d
68 }|}
69 l.pageno l.pagedimno
70 l.pagew l.pageh
71 l.pagevw l.pagevh
72 l.pagex l.pagey
73 l.pagedispx l.pagedispy
74 l.pagecol
77 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
78 dolog {|rect {
79 x0,y0=(% f, % f)
80 x1,y1=(% f, % f)
81 x2,y2=(% f, % f)
82 x3,y3=(% f, % f)
83 }|} x0 y0 x1 y1 x2 y2 x3 y3;
86 let isbirdseye = function
87 | Birdseye _ -> true
88 | Textentry _ | View | LinkNav _ -> false
91 let istextentry = function
92 | Textentry _ -> true
93 | Birdseye _ | View | LinkNav _ -> false
96 let pgscale h = truncate (float h *. conf.pgscale);;
98 let hscrollh () =
99 if state.uioh#alwaysscrolly || ((conf.scrollb land scrollbhv != 0)
100 && (state.w > state.winw))
101 then conf.scrollbw
102 else 0
105 let vscrollw () =
106 if state.uioh#alwaysscrolly || ((conf.scrollb land scrollbvv != 0)
107 && (state.maxy > state.winh))
108 then conf.scrollbw
109 else 0
112 let vscrollhit x =
113 if conf.leftscroll
114 then x < vscrollw ()
115 else x > state.winw - vscrollw ()
118 let setfontsize n =
119 fstate.fontsize <- n;
120 fstate.wwidth <- measurestr fstate.fontsize "w";
121 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
124 let vlog fmt =
125 if conf.verbose
126 then dolog fmt
127 else Printf.kprintf ignore fmt
130 let launchpath () =
131 if emptystr conf.pathlauncher
132 then dolog "%s" state.path
133 else (
134 let command = Str.global_replace percentsre state.path conf.pathlauncher in
135 match spawn command [] with
136 | _pid -> ()
137 | exception exn ->
138 dolog "failed to execute `%s': %s" command @@ exntos exn
142 module G =
143 struct
144 let postRedisplay who =
145 vlog "redisplay for [%S]" who;
146 state.redisplay <- true;
151 let getopaque pageno =
152 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
153 with Not_found -> None
156 let pagetranslatepoint l x y =
157 let dy = y - l.pagedispy in
158 let y = dy + l.pagey in
159 let dx = x - l.pagedispx in
160 let x = dx + l.pagex in
161 (x, y);
164 let onppundermouse g x y d =
165 let rec f = function
166 | l :: rest ->
167 begin match getopaque l.pageno with
168 | Some opaque ->
169 let x0 = l.pagedispx in
170 let x1 = x0 + l.pagevw in
171 let y0 = l.pagedispy in
172 let y1 = y0 + l.pagevh in
173 if y >= y0 && y <= y1 && x >= x0 && x <= x1
174 then
175 let px, py = pagetranslatepoint l x y in
176 match g opaque l px py with
177 | Some res -> res
178 | None -> f rest
179 else f rest
180 | _ ->
181 f rest
183 | [] -> d
185 f state.layout
188 let getunder x y =
189 let g opaque l px py =
190 if state.bzoom
191 then (
192 match rectofblock opaque px py with
193 | Some [|x0;x1;y0;y1|] ->
194 let rect = (x0, y0, x1, y0, x1, y1, x0, y1) in
195 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
196 state.rects <- [l.pageno, color, rect];
197 G.postRedisplay "getunder";
198 | _ -> ()
200 let under = whatsunder opaque px py in
201 if under = Unone then None else Some under
203 onppundermouse g x y Unone
206 let unproject x y =
207 let g opaque l x y =
208 match unproject opaque x y with
209 | Some (x, y) -> Some (Some (opaque, l.pageno, x, y))
210 | None -> None
212 onppundermouse g x y None;
215 let showtext c s =
216 state.text <- Printf.sprintf "%c%s" c s;
217 G.postRedisplay "showtext";
220 let impmsg fmt =
221 Format.ksprintf (fun s -> showtext '!' s) fmt;
224 let pipesel opaque cmd =
225 if hassel opaque
226 then pipef ~closew:false "pipesel"
227 (fun w ->
228 copysel w opaque;
229 G.postRedisplay "pipesel"
230 ) cmd
233 let paxunder x y =
234 let g opaque l px py =
235 if markunder opaque px py conf.paxmark
236 then (
237 Some (fun () ->
238 match getopaque l.pageno with
239 | None -> ()
240 | Some opaque -> pipesel opaque conf.paxcmd
243 else None
245 G.postRedisplay "paxunder";
246 if conf.paxmark = Mark_page
247 then
248 List.iter (fun l ->
249 match getopaque l.pageno with
250 | None -> ()
251 | Some opaque -> clearmark opaque) state.layout;
252 state.roam <- onppundermouse g x y (fun () -> impmsg "whoopsie daisy");
255 let undertext = function
256 | Unone -> "none"
257 | Ulinkuri s -> s
258 | Utext s -> "font: " ^ s
259 | Uannotation (opaque, slinkindex) ->
260 "annotation: " ^ getannotcontents opaque slinkindex
263 let updateunder x y =
264 match getunder x y with
265 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
266 | Ulinkuri uri ->
267 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
268 Wsi.setcursor Wsi.CURSOR_INFO
269 | Utext s ->
270 if conf.underinfo then showtext 'f' ("ont: " ^ s);
271 Wsi.setcursor Wsi.CURSOR_TEXT
272 | Uannotation _ ->
273 if conf.underinfo then showtext 'a' "nnotation";
274 Wsi.setcursor Wsi.CURSOR_INFO
277 let showlinktype under =
278 if conf.underinfo && under != Unone
279 then showtext ' ' @@ undertext under
282 let [@warning "-4"] intentry_with_suffix text key =
283 let text =
284 match key with
285 | Keys.Ascii ('0'..'9' as c) -> addchar text c
286 | Keys.Ascii ('k' | 'm' | 'g' | 'K' | 'M' | 'G' as c) ->
287 addchar text @@ asciilower c
288 | _ ->
289 state.text <- Printf.sprintf "invalid key";
290 text
292 TEcont text
295 let wcmd fmt =
296 let b = Buffer.create 16 in
297 Printf.kbprintf
298 (fun b ->
299 let b = Buffer.to_bytes b in
300 wcmd state.ss b @@ Bytes.length b
301 ) b fmt
304 let nogeomcmds cmds =
305 match cmds with
306 | s, [] -> emptystr s
307 | _ -> false
310 let layoutN ((columns, coverA, coverB), b) x y sw sh =
311 let rec fold accu n =
312 if n = Array.length b
313 then accu
314 else
315 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
316 if (vy - y) > sh &&
317 (n = coverA - 1
318 || n = state.pagecount - coverB
319 || (n - coverA) mod columns = columns - 1)
320 then accu
321 else
322 let accu =
323 if vy + h > y
324 then
325 let pagey = max 0 (y - vy) in
326 let pagedispy = if pagey > 0 then 0 else vy - y in
327 let pagedispx, pagex =
328 let pdx =
329 if n = coverA - 1 || n = state.pagecount - coverB
330 then x + (sw - w) / 2
331 else dx + xoff + x
333 if pdx < 0
334 then 0, -pdx
335 else pdx, 0
337 let pagevw =
338 let vw = sw - pagedispx in
339 let pw = w - pagex in
340 min vw pw
342 let pagevh = min (h - pagey) (sh - pagedispy) in
343 if pagevw > 0 && pagevh > 0
344 then
345 let e =
346 { pageno = n
347 ; pagedimno = pdimno
348 ; pagew = w
349 ; pageh = h
350 ; pagex = pagex
351 ; pagey = pagey
352 ; pagevw = pagevw
353 ; pagevh = pagevh
354 ; pagedispx = pagedispx
355 ; pagedispy = pagedispy
356 ; pagecol = 0
359 e :: accu
360 else
361 accu
362 else
363 accu
365 fold accu (n+1)
367 if Array.length b = 0
368 then []
369 else List.rev (fold [] (page_of_y y))
372 let layoutS (columns, b) x y sw sh =
373 let rec fold accu n =
374 if n = Array.length b
375 then accu
376 else
377 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
378 if (vy - y) > sh
379 then accu
380 else
381 let accu =
382 if vy + pageh > y
383 then
384 let x = xoff + x in
385 let pagey = max 0 (y - vy) in
386 let pagedispy = if pagey > 0 then 0 else vy - y in
387 let pagedispx, pagex =
388 if px = 0
389 then (
390 if x < 0
391 then 0, -x
392 else x, 0
394 else (
395 let px = px - x in
396 if px < 0
397 then -px, 0
398 else 0, px
401 let pagecolw = pagew/columns in
402 let pagedispx =
403 if pagecolw < sw
404 then pagedispx + ((sw - pagecolw) / 2)
405 else pagedispx
407 let pagevw =
408 let vw = sw - pagedispx in
409 let pw = pagew - pagex in
410 min vw pw
412 let pagevw = min pagevw pagecolw in
413 let pagevh = min (pageh - pagey) (sh - pagedispy) in
414 if pagevw > 0 && pagevh > 0
415 then
416 let e =
417 { pageno = n/columns
418 ; pagedimno = pdimno
419 ; pagew = pagew
420 ; pageh = pageh
421 ; pagex = pagex
422 ; pagey = pagey
423 ; pagevw = pagevw
424 ; pagevh = pagevh
425 ; pagedispx = pagedispx
426 ; pagedispy = pagedispy
427 ; pagecol = n mod columns
430 e :: accu
431 else
432 accu
433 else
434 accu
436 fold accu (n+1)
438 List.rev (fold [] 0)
441 let layout x y sw sh =
442 if nogeomcmds state.geomcmds
443 then
444 match conf.columns with
445 | Csingle b -> layoutN ((1, 0, 0), b) x y sw sh
446 | Cmulti c -> layoutN c x y sw sh
447 | Csplit s -> layoutS s x y sw sh
448 else []
451 let maxy () = state.maxy - if conf.maxhfit then state.winh else 0;;
453 let clamp incr = bound (state.y + incr) 0 @@ maxy ();;
455 let itertiles l f =
456 let tilex = l.pagex mod conf.tilew in
457 let tiley = l.pagey mod conf.tileh in
459 let col = l.pagex / conf.tilew in
460 let row = l.pagey / conf.tileh in
462 let rec rowloop row y0 dispy h =
463 if h = 0
464 then ()
465 else (
466 let dh = conf.tileh - y0 in
467 let dh = min h dh in
468 let rec colloop col x0 dispx w =
469 if w = 0
470 then ()
471 else (
472 let dw = conf.tilew - x0 in
473 let dw = min w dw in
474 f col row dispx dispy x0 y0 dw dh;
475 colloop (col+1) 0 (dispx+dw) (w-dw)
478 colloop col tilex l.pagedispx l.pagevw;
479 rowloop (row+1) 0 (dispy+dh) (h-dh)
482 if l.pagevw > 0 && l.pagevh > 0
483 then rowloop row tiley l.pagedispy l.pagevh;
486 let gettileopaque l col row =
487 let key =
488 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
490 try Some (Hashtbl.find state.tilemap key)
491 with Not_found -> None
494 let puttileopaque l col row gen colorspace angle opaque size elapsed =
495 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
496 Hashtbl.add state.tilemap key (opaque, size, elapsed)
499 let drawtiles l color =
500 GlDraw.color color;
501 begintiles ();
502 let f col row x y tilex tiley w h =
503 match gettileopaque l col row with
504 | Some (opaque, _, t) ->
505 let params = x, y, w, h, tilex, tiley in
506 if conf.invert
507 then GlTex.env (`mode `blend);
508 drawtile params opaque;
509 if conf.invert
510 then GlTex.env (`mode `modulate);
511 if conf.debug
512 then (
513 endtiles ();
514 let s = Printf.sprintf
515 "%d[%d,%d] %f sec"
516 l.pageno col row t
518 let w = measurestr fstate.fontsize s in
519 GlDraw.color (0.0, 0.0, 0.0);
520 filledrect (float (x-2))
521 (float (y-2))
522 (float (x+2) +. w)
523 (float (y + fstate.fontsize + 2));
524 GlDraw.color color;
525 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
526 begintiles ();
529 | None ->
530 endtiles ();
531 let w =
532 let lw = state.winw - x in
533 min lw w
534 and h =
535 let lh = state.winh - y in
536 min lh h
538 if conf.invert
539 then GlTex.env (`mode `blend);
540 begin match state.checkerstexid with
541 | Some id ->
542 Gl.enable `texture_2d;
543 GlTex.bind_texture ~target:`texture_2d id;
544 let x0 = float x
545 and y0 = float y
546 and x1 = float (x+w)
547 and y1 = float (y+h) in
549 let tw = float w /. 16.0
550 and th = float h /. 16.0 in
551 let tx0 = float tilex /. 16.0
552 and ty0 = float tiley /. 16.0 in
553 let tx1 = tx0 +. tw
554 and ty1 = ty0 +. th in
555 Raw.sets_float Glutils.vraw ~pos:0
556 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
557 Raw.sets_float Glutils.traw ~pos:0
558 [| tx0; ty0; tx0; ty1; tx1; ty0; tx1; ty1 |];
559 GlArray.vertex `two Glutils.vraw;
560 GlArray.tex_coord `two Glutils.traw;
561 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
562 Gl.disable `texture_2d;
564 | None ->
565 GlDraw.color (1.0, 1.0, 1.0);
566 filledrect (float x) (float y) (float (x+w)) (float (y+h));
567 end;
568 if conf.invert
569 then GlTex.env (`mode `modulate);
570 if w > 128 && h > fstate.fontsize + 10
571 then (
572 let c = if conf.invert then 1.0 else 0.0 in
573 GlDraw.color (c, c, c);
574 let c, r =
575 if conf.verbose
576 then (col*conf.tilew, row*conf.tileh)
577 else col, row
579 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
581 GlDraw.color color;
582 begintiles ();
584 itertiles l f;
585 endtiles ();
588 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
590 let tilevisible1 l x y =
591 let ax0 = l.pagex
592 and ax1 = l.pagex + l.pagevw
593 and ay0 = l.pagey
594 and ay1 = l.pagey + l.pagevh in
596 let bx0 = x
597 and by0 = y in
598 let bx1 = min (bx0 + conf.tilew) l.pagew
599 and by1 = min (by0 + conf.tileh) l.pageh in
601 let rx0 = max ax0 bx0
602 and ry0 = max ay0 by0
603 and rx1 = min ax1 bx1
604 and ry1 = min ay1 by1 in
606 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
607 nonemptyintersection
610 let tilevisible layout n x y =
611 let rec findpageinlayout m = function
612 | l :: rest when l.pageno = n ->
613 tilevisible1 l x y || (
614 match conf.columns with
615 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
616 | Csplit _ | Csingle _ | Cmulti _ -> false
618 | _ :: rest -> findpageinlayout 0 rest
619 | [] -> false
621 findpageinlayout 0 layout;
624 let tileready l x y =
625 tilevisible1 l x y &&
626 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
629 let tilepage n p layout =
630 let rec loop = function
631 | l :: rest ->
632 if l.pageno = n
633 then
634 let f col row _ _ _ _ _ _ =
635 if state.currently = Idle
636 then
637 match gettileopaque l col row with
638 | Some _ -> ()
639 | None ->
640 let x = col*conf.tilew
641 and y = row*conf.tileh in
642 let w =
643 let w = l.pagew - x in
644 min w conf.tilew
646 let h =
647 let h = l.pageh - y in
648 min h conf.tileh
650 let pbo =
651 if conf.usepbo
652 then getpbo w h conf.colorspace
653 else ~< "0"
655 wcmd "tile %s %d %d %d %d %s"
656 (~> p) x y w h (~> pbo);
657 state.currently <-
658 Tiling (
659 l, p, conf.colorspace, conf.angle,
660 state.gen, col, row, conf.tilew, conf.tileh
663 itertiles l f;
664 else
665 loop rest
667 | [] -> ()
669 if nogeomcmds state.geomcmds
670 then loop layout;
673 let preloadlayout x y sw sh =
674 let y = if y < sh then 0 else y - sh in
675 let x = min 0 (x + sw) in
676 let h = sh*3 in
677 let w = sw*3 in
678 layout x y w h;
681 let load pages =
682 let rec loop pages =
683 if state.currently != Idle
684 then ()
685 else
686 match pages with
687 | l :: rest ->
688 begin match getopaque l.pageno with
689 | None ->
690 wcmd "page %d %d" l.pageno l.pagedimno;
691 state.currently <- Loading (l, state.gen);
692 | Some opaque ->
693 tilepage l.pageno opaque pages;
694 loop rest
695 end;
696 | _ -> ()
698 if nogeomcmds state.geomcmds
699 then loop pages
702 let preload pages =
703 load pages;
704 if conf.preload && state.currently = Idle
705 then load (preloadlayout state.x state.y state.winw state.winh);
708 let layoutready layout =
709 let rec fold all ls =
710 all && match ls with
711 | l :: rest ->
712 let seen = ref false in
713 let allvisible = ref true in
714 let foo col row _ _ _ _ _ _ =
715 seen := true;
716 allvisible := !allvisible &&
717 begin match gettileopaque l col row with
718 | Some _ -> true
719 | None -> false
722 itertiles l foo;
723 fold (!seen && !allvisible) rest
724 | [] -> true
726 let alltilesvisible = fold true layout in
727 alltilesvisible;
730 let gotoxy x y =
731 let y = bound y 0 state.maxy in
732 let y, layout =
733 let layout = layout x y state.winw state.winh in
734 G.postRedisplay "gotoxy ready";
735 y, layout
737 state.x <- x;
738 state.y <- y;
739 state.layout <- layout;
740 begin match state.mode with
741 | LinkNav ln ->
742 begin match ln with
743 | Ltexact (pageno, linkno) ->
744 let rec loop = function
745 | [] ->
746 state.lnava <- Some (pageno, linkno);
747 state.mode <- LinkNav (Ltgendir 0)
748 | l :: _ when l.pageno = pageno ->
749 begin match getopaque pageno with
750 | None -> state.mode <- LinkNav (Ltnotready (pageno, 0))
751 | Some opaque ->
752 let x0, y0, x1, y1 = getlinkrect opaque linkno in
753 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
754 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
755 then state.mode <- LinkNav (Ltgendir 0)
757 | _ :: rest -> loop rest
759 loop layout
760 | Ltnotready _ | Ltgendir _ -> ()
762 | Birdseye _ | Textentry _ | View -> ()
763 end;
764 begin match state.mode with
765 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
766 if not (pagevisible layout pageno)
767 then (
768 match state.layout with
769 | [] -> ()
770 | l :: _ ->
771 state.mode <- Birdseye (
772 conf, leftx, l.pageno, hooverpageno, anchor
775 | LinkNav lt ->
776 begin match lt with
777 | Ltnotready (_, dir)
778 | Ltgendir dir ->
779 let linknav =
780 let rec loop = function
781 | [] -> lt
782 | l :: rest ->
783 match getopaque l.pageno with
784 | None -> Ltnotready (l.pageno, dir)
785 | Some opaque ->
786 let link =
787 let ld =
788 if dir = 0
789 then LDfirstvisible (l.pagex, l.pagey, dir)
790 else (
791 if dir > 0 then LDfirst else LDlast
794 findlink opaque ld
796 match link with
797 | Lnotfound -> loop rest
798 | Lfound n ->
799 showlinktype (getlink opaque n);
800 Ltexact (l.pageno, n)
802 loop state.layout
804 state.mode <- LinkNav linknav
805 | Ltexact _ -> ()
807 | Textentry _ | View -> ()
808 end;
809 preload layout;
810 if conf.updatecurs
811 then (
812 let mx, my = state.mpos in
813 updateunder mx my;
817 let conttiling pageno opaque =
818 tilepage pageno opaque
819 (if conf.preload
820 then preloadlayout state.x state.y state.winw state.winh
821 else state.layout)
824 let gotoxy x y =
825 if not conf.verbose then state.text <- E.s;
826 gotoxy x y;
829 let getanchory (n, top, dtop) =
830 let y, h = getpageyh n in
831 if conf.presentation
832 then
833 let ips = calcips h in
834 y + truncate (top*.float h -. dtop*.float ips) + ips;
835 else
836 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
839 let gotoanchor anchor =
840 gotoxy state.x (getanchory anchor);
843 let addnav () =
844 getanchor () |> cbput state.hists.nav;
847 let addnavnorc () =
848 getanchor () |> cbput_dont_update_rc state.hists.nav;
851 let getnav dir =
852 let anchor = cbgetc state.hists.nav dir in
853 getanchory anchor;
856 let gotopage n top =
857 let y, h = getpageyh n in
858 let y = y + (truncate (top *. float h)) in
859 gotoxy state.x y
862 let gotopage1 n top =
863 let y = getpagey n in
864 let y = y + top in
865 gotoxy state.x y
868 let invalidate s f =
869 state.redisplay <- false;
870 state.layout <- [];
871 state.pdims <- [];
872 state.rects <- [];
873 state.rects1 <- [];
874 match state.geomcmds with
875 | ps, [] when emptystr ps ->
876 f ();
877 state.geomcmds <- s, [];
879 | ps, [] ->
880 state.geomcmds <- ps, [s, f];
882 | ps, (s', _) :: rest when s' = s ->
883 state.geomcmds <- ps, ((s, f) :: rest);
885 | ps, cmds ->
886 state.geomcmds <- ps, ((s, f) :: cmds);
889 let flushpages () =
890 Hashtbl.iter (fun _ opaque ->
891 wcmd "freepage %s" (~> opaque);
892 ) state.pagemap;
893 Hashtbl.clear state.pagemap;
896 let flushtiles () =
897 if not (Queue.is_empty state.tilelru)
898 then (
899 Queue.iter (fun (k, p, s) ->
900 wcmd "freetile %s" (~> p);
901 state.memused <- state.memused - s;
902 Hashtbl.remove state.tilemap k;
903 ) state.tilelru;
904 state.uioh#infochanged Memused;
905 Queue.clear state.tilelru;
907 load state.layout;
910 let stateh h =
911 let h = truncate (float h*.conf.zoom) in
912 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
913 h - d
916 let fillhelp () =
917 state.help <-
918 let sl = keystostrlist conf in
919 let rec loop accu =
920 function | [] -> accu
921 | s :: rest -> loop ((s, 0, Noaction) :: accu) rest
922 in makehelp () @ (("", 0, Noaction) :: loop [] sl) |> Array.of_list
925 let opendoc path password =
926 state.path <- path;
927 state.password <- password;
928 state.gen <- state.gen + 1;
929 state.docinfo <- [];
930 state.outlines <- [||];
932 flushpages ();
933 setaalevel conf.aalevel;
934 let titlepath =
935 if emptystr state.origin
936 then path
937 else state.origin
939 Wsi.settitle ("llpp " ^ mbtoutf8 (Filename.basename titlepath));
940 wcmd "open %d %d %s\000%s\000%s\000"
941 (btod conf.usedoccss) !layouth
942 path password conf.css;
943 invalidate "reqlayout"
944 (fun () ->
945 wcmd "reqlayout %d %d %d %s\000"
946 conf.angle (FMTE.to_int conf.fitmodel)
947 (stateh state.winh) state.nameddest
949 fillhelp ();
952 let reload () =
953 state.anchor <- getanchor ();
954 opendoc state.path state.password;
957 let scalecolor c =
958 let c = c *. conf.colorscale in
959 (c, c, c);
962 let scalecolor2 (r, g, b) =
963 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
966 let docolumns columns =
967 match columns with
968 | Csingle _ ->
969 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
970 let rec loop pageno pdimno pdim y ph pdims =
971 if pageno = state.pagecount
972 then ()
973 else
974 let pdimno, ((_, w, h, xoff) as pdim), pdims =
975 match pdims with
976 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
977 pdimno+1, pdim, rest
978 | _ ->
979 pdimno, pdim, pdims
981 let x = max 0 (((state.winw - w) / 2) - xoff) in
982 let y =
983 y + (if conf.presentation
984 then (if pageno = 0 then calcips h else calcips ph + calcips h)
985 else (if pageno = 0 then 0 else conf.interpagespace)
988 a.(pageno) <- (pdimno, x, y, pdim);
989 loop (pageno+1) pdimno pdim (y + h) h pdims
991 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
992 conf.columns <- Csingle a;
994 | Cmulti ((columns, coverA, coverB), _) ->
995 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
996 let rec loop pageno pdimno pdim x y rowh pdims =
997 let rec fixrow m =
998 if m = pageno then () else
999 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1000 if h < rowh
1001 then (
1002 let y = y + (rowh - h) / 2 in
1003 a.(m) <- (pdimno, x, y, pdim);
1005 fixrow (m+1)
1007 if pageno = state.pagecount
1008 then fixrow (((pageno - 1) / columns) * columns)
1009 else
1010 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1011 match pdims with
1012 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1013 pdimno+1, pdim, rest
1014 | _ ->
1015 pdimno, pdim, pdims
1017 let x, y, rowh' =
1018 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1019 then (
1020 let x = (state.winw - w) / 2 in
1021 let ips =
1022 if conf.presentation then calcips h else conf.interpagespace in
1023 x, y + ips + rowh, h
1025 else (
1026 if (pageno - coverA) mod columns = 0
1027 then (
1028 let x = max 0 (state.winw - state.w) / 2 in
1029 let y =
1030 if conf.presentation
1031 then
1032 let ips = calcips h in
1033 y + (if pageno = 0 then 0 else calcips rowh + ips)
1034 else
1035 y + (if pageno = 0 then 0 else conf.interpagespace)
1037 x, y + rowh, h
1039 else x, y, max rowh h
1042 let y =
1043 if pageno > 1 && (pageno - coverA) mod columns = 0
1044 then (
1045 let y =
1046 if pageno = columns && conf.presentation
1047 then (
1048 let ips = calcips rowh in
1049 for i = 0 to pred columns
1051 let (pdimno, x, y, pdim) = a.(i) in
1052 a.(i) <- (pdimno, x, y+ips, pdim)
1053 done;
1054 y+ips;
1056 else y
1058 fixrow (pageno - columns);
1061 else y
1063 a.(pageno) <- (pdimno, x, y, pdim);
1064 let x = x + w + xoff*2 + conf.interpagespace in
1065 loop (pageno+1) pdimno pdim x y rowh' pdims
1067 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1068 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1070 | Csplit (c, _) ->
1071 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1072 let rec loop pageno pdimno pdim y pdims =
1073 if pageno = state.pagecount
1074 then ()
1075 else
1076 let pdimno, ((_, w, h, _) as pdim), pdims =
1077 match pdims with
1078 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1079 pdimno+1, pdim, rest
1080 | _ ->
1081 pdimno, pdim, pdims
1083 let cw = w / c in
1084 let rec loop1 n x y =
1085 if n = c then y else (
1086 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1087 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1090 let y = loop1 0 0 y in
1091 loop (pageno+1) pdimno pdim y pdims
1093 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1094 conf.columns <- Csplit (c, a);
1097 let represent () =
1098 docolumns conf.columns;
1099 state.maxy <- calcheight ();
1100 if state.reprf == noreprf
1101 then (
1102 match state.mode with
1103 | Birdseye (_, _, pageno, _, _) ->
1104 let y, h = getpageyh pageno in
1105 let top = (state.winh - h) / 2 in
1106 gotoxy state.x (max 0 (y - top))
1107 | Textentry _ | View | LinkNav _ ->
1108 let y = getanchory state.anchor in
1109 let y = min y (state.maxy - state.winh) in
1110 gotoxy state.x y;
1112 else (
1113 state.reprf ();
1114 state.reprf <- noreprf;
1118 let reshape ?(firsttime=false) w h =
1119 GlDraw.viewport ~x:0 ~y:0 ~w ~h;
1120 if not firsttime && nogeomcmds state.geomcmds
1121 then state.anchor <- getanchor ();
1123 state.winw <- w;
1124 let w = truncate (float w *. conf.zoom) in
1125 let w = max w 2 in
1126 state.winh <- h;
1127 setfontsize fstate.fontsize;
1128 GlMat.mode `modelview;
1129 GlMat.load_identity ();
1131 GlMat.mode `projection;
1132 GlMat.load_identity ();
1133 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1134 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1135 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
1137 let relx =
1138 if conf.zoom <= 1.0
1139 then 0.0
1140 else float state.x /. float state.w
1142 invalidate "geometry"
1143 (fun () ->
1144 state.w <- w;
1145 if not firsttime
1146 then state.x <- truncate (relx *. float w);
1147 let w =
1148 match conf.columns with
1149 | Csingle _ -> w
1150 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1151 | Csplit (c, _) -> w * c
1153 wcmd "geometry %d %d %d"
1154 w (stateh h) (FMTE.to_int conf.fitmodel)
1158 let enttext () =
1159 let len = String.length state.text in
1160 let x0 = if conf.leftscroll then vscrollw () else 0 in
1161 let drawstring s =
1162 let hscrollh =
1163 match state.mode with
1164 | Textentry _ | View | LinkNav _ ->
1165 let h, _, _ = state.uioh#scrollpw in
1167 | Birdseye _ -> 0
1169 let rect x w =
1170 filledrect
1171 x (float (state.winh - (fstate.fontsize + 4) - hscrollh))
1172 (x+.w) (float (state.winh - hscrollh))
1175 let w = float (state.winw - 1 - vscrollw ()) in
1176 if state.progress >= 0.0 && state.progress < 1.0
1177 then (
1178 GlDraw.color (0.3, 0.3, 0.3);
1179 let w1 = w *. state.progress in
1180 rect (float x0) w1;
1181 GlDraw.color (0.0, 0.0, 0.0);
1182 rect (float x0+.w1) (float x0+.w-.w1)
1184 else (
1185 GlDraw.color (0.0, 0.0, 0.0);
1186 rect (float x0) w;
1189 GlDraw.color (1.0, 1.0, 1.0);
1190 drawstring
1191 fstate.fontsize
1192 (if conf.leftscroll then x0 + 2 else x0 + if len > 0 then 8 else 2)
1193 (state.winh - hscrollh - 5) s;
1195 let s =
1196 match state.mode with
1197 | Textentry ((prefix, text, _, _, _, _), _) ->
1198 let s =
1199 if len > 0
1200 then Printf.sprintf "%s%s_ [%s]" prefix text state.text
1201 else Printf.sprintf "%s%s_" prefix text
1205 | Birdseye _ | View | LinkNav _ -> state.text
1207 let s =
1208 if state.newerrmsgs
1209 then (
1210 if not (istextentry state.mode) && state.uioh#eformsgs
1211 then
1212 let s1 = "(press 'e' to review error messasges)" in
1213 if nonemptystr s then s ^ " " ^ s1 else s1
1214 else s
1216 else s
1218 if nonemptystr s
1219 then drawstring s
1222 let gctiles () =
1223 let len = Queue.length state.tilelru in
1224 let layout = lazy (if conf.preload
1225 then preloadlayout state.x state.y state.winw state.winh
1226 else state.layout) in
1227 let rec loop qpos =
1228 if state.memused > conf.memlimit
1229 then (
1230 if qpos < len
1231 then
1232 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1233 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1234 let (_, pw, ph, _) = getpagedim n in
1235 if gen = state.gen
1236 && colorspace = conf.colorspace
1237 && angle = conf.angle
1238 && pagew = pw
1239 && pageh = ph
1240 && (
1241 let x = col*conf.tilew
1242 and y = row*conf.tileh in
1243 tilevisible (Lazy.force_val layout) n x y
1245 then Queue.push lruitem state.tilelru
1246 else (
1247 freepbo p;
1248 wcmd "freetile %s" (~> p);
1249 state.memused <- state.memused - s;
1250 state.uioh#infochanged Memused;
1251 Hashtbl.remove state.tilemap k;
1253 loop (qpos+1)
1256 loop 0
1259 let onpagerect pageno f =
1260 let b =
1261 match conf.columns with
1262 | Cmulti (_, b) -> b
1263 | Csingle b -> b
1264 | Csplit (_, b) -> b
1266 if pageno >= 0 && pageno < Array.length b
1267 then
1268 let (_, _, _, (_, w, h, _)) = b.(pageno) in
1269 f w h
1272 let gotopagexy1 pageno x y =
1273 let _,w1,h1,leftx = getpagedim pageno in
1274 let top = y /. (float h1) in
1275 let left = x /. (float w1) in
1276 let py, w, h = getpageywh pageno in
1277 let wh = state.winh in
1278 let x = left *. (float w) in
1279 let x = leftx + state.x + truncate x in
1280 let sx =
1281 if x < 0 || x >= state.winw
1282 then state.x - x
1283 else state.x
1285 let pdy = truncate (top *. float h) in
1286 let y' = py + pdy in
1287 let dy = y' - state.y in
1288 let sy =
1289 if x != state.x || not (dy > 0 && dy < wh)
1290 then (
1291 if conf.presentation
1292 then
1293 if abs (py - y') > wh
1294 then y'
1295 else py
1296 else y';
1298 else state.y
1300 if state.x != sx || state.y != sy
1301 then gotoxy sx sy
1302 else gotoxy state.x state.y;
1305 let gotopagexy pageno x y =
1306 match state.mode with
1307 | Birdseye _ -> gotopage pageno 0.0
1308 | Textentry _ | View | LinkNav _ -> gotopagexy1 pageno x y
1311 let getpassword () =
1312 let passcmd = getenvwithdef "LLPP_ASKPASS" conf.passcmd in
1313 if emptystr passcmd
1314 then E.s
1315 else getcmdoutput
1316 (fun s ->
1317 impmsg "error getting password: %s" s;
1318 dolog "%s" s) passcmd;
1321 let pgoto opaque pageno x y =
1322 let pdimno = getpdimno pageno in
1323 let x, y = project opaque pageno pdimno x y in
1324 gotopagexy pageno x y;
1327 let act cmds =
1328 (* dolog "%S" cmds; *)
1329 let spl = splitatchar cmds ' ' in
1330 let scan s fmt f =
1331 try Scanf.sscanf s fmt f
1332 with exn ->
1333 dolog "error processing '%S': %s" cmds @@ exntos exn;
1334 exit 1
1336 let addoutline outline =
1337 match state.currently with
1338 | Outlining outlines -> state.currently <- Outlining (outline :: outlines)
1339 | Idle -> state.currently <- Outlining [outline]
1340 | Loading _ | Tiling _ ->
1341 dolog "invalid outlining state";
1342 logcurrently state.currently
1344 match spl with
1345 | "clear", "" ->
1346 state.pdims <- [];
1347 state.uioh#infochanged Pdim;
1349 | "clearrects", "" ->
1350 state.rects <- state.rects1;
1351 G.postRedisplay "clearrects";
1353 | "continue", args ->
1354 let n = scan args "%u" (fun n -> n) in
1355 state.pagecount <- n;
1356 begin match state.currently with
1357 | Outlining l ->
1358 state.currently <- Idle;
1359 state.outlines <- Array.of_list (List.rev l)
1360 | Idle | Loading _ | Tiling _ -> ()
1361 end;
1363 let cur, cmds = state.geomcmds in
1364 if emptystr cur
1365 then failwith "umpossible";
1367 begin match List.rev cmds with
1368 | [] ->
1369 state.geomcmds <- E.s, [];
1370 represent ();
1371 | (s, f) :: rest ->
1372 f ();
1373 state.geomcmds <- s, List.rev rest;
1374 end;
1375 G.postRedisplay "continue";
1377 | "msg", args ->
1378 showtext ' ' args
1380 | "vmsg", args ->
1381 if conf.verbose
1382 then showtext ' ' args
1384 | "emsg", args ->
1385 Buffer.add_string state.errmsgs args;
1386 state.newerrmsgs <- true;
1387 G.postRedisplay "error message"
1389 | "progress", args ->
1390 let progress, text =
1391 scan args "%f %n"
1392 (fun f pos ->
1393 f, String.sub args pos (String.length args - pos))
1395 state.text <- text;
1396 state.progress <- progress;
1397 G.postRedisplay "progress"
1399 | "firstmatch", args ->
1400 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1401 scan args "%u %d %f %f %f %f %f %f %f %f"
1402 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1403 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1405 let y = (getpagey pageno) + truncate y0 in
1406 let x =
1407 if (state.x < - truncate x0) || (state.x > state.winw - truncate x1)
1408 then state.winw/2 - truncate (x0 /. 2. +. x1 /. 2.)
1409 else state.x
1411 addnav ();
1412 gotoxy x y;
1413 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1414 state.rects1 <- [pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)]
1416 | "match", args ->
1417 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1418 scan args "%u %d %f %f %f %f %f %f %f %f"
1419 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1420 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1422 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1423 state.rects1 <-
1424 (pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1426 | "page", args ->
1427 let pageopaques, t = scan args "%s %f" (fun p t -> p, t) in
1428 let pageopaque = ~< pageopaques in
1429 begin match state.currently with
1430 | Loading (l, gen) ->
1431 vlog "page %d took %f sec" l.pageno t;
1432 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1433 let preloadedpages =
1434 if conf.preload
1435 then preloadlayout state.x state.y state.winw state.winh
1436 else state.layout
1438 let evict () =
1439 let set = List.fold_left (fun s l -> IntSet.add l.pageno s)
1440 IntSet.empty preloadedpages
1442 let evictedpages =
1443 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1444 if not (IntSet.mem pageno set)
1445 then (
1446 wcmd "freepage %s" (~> opaque);
1447 key :: accu
1449 else accu
1450 ) state.pagemap []
1452 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1454 evict ();
1455 state.currently <- Idle;
1456 if gen = state.gen
1457 then (
1458 tilepage l.pageno pageopaque state.layout;
1459 load state.layout;
1460 load preloadedpages;
1461 let visible = pagevisible state.layout l.pageno in
1462 if visible
1463 then (
1464 match state.mode with
1465 | LinkNav (Ltnotready (pageno, dir)) ->
1466 if pageno = l.pageno
1467 then (
1468 let link =
1469 let ld =
1470 if dir = 0
1471 then LDfirstvisible (l.pagex, l.pagey, dir)
1472 else (
1473 if dir > 0 then LDfirst else LDlast
1476 findlink pageopaque ld
1478 match link with
1479 | Lnotfound -> ()
1480 | Lfound n ->
1481 showlinktype (getlink pageopaque n);
1482 state.mode <- LinkNav (Ltexact (l.pageno, n))
1484 | LinkNav (Ltgendir _)
1485 | LinkNav (Ltexact _)
1486 | View
1487 | Birdseye _
1488 | Textentry _ -> ()
1491 if visible && layoutready state.layout
1492 then (
1493 G.postRedisplay "page";
1497 | Idle | Tiling _ | Outlining _ ->
1498 dolog "Inconsistent loading state";
1499 logcurrently state.currently;
1500 exit 1
1503 | "tile" , args ->
1504 let (x, y, opaques, size, t) =
1505 scan args "%u %u %s %u %f"
1506 (fun x y p size t -> (x, y, p, size, t))
1508 let opaque = ~< opaques in
1509 begin match state.currently with
1510 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1511 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1513 unmappbo opaque;
1514 if tilew != conf.tilew || tileh != conf.tileh
1515 then (
1516 wcmd "freetile %s" (~> opaque);
1517 state.currently <- Idle;
1518 load state.layout;
1520 else (
1521 puttileopaque l col row gen cs angle opaque size t;
1522 state.memused <- state.memused + size;
1523 state.uioh#infochanged Memused;
1524 gctiles ();
1525 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1526 opaque, size) state.tilelru;
1528 state.currently <- Idle;
1529 if gen = state.gen
1530 && conf.colorspace = cs
1531 && conf.angle = angle
1532 && tilevisible state.layout l.pageno x y
1533 then conttiling l.pageno pageopaque;
1535 preload state.layout;
1536 if gen = state.gen
1537 && conf.colorspace = cs
1538 && conf.angle = angle
1539 && tilevisible state.layout l.pageno x y
1540 && layoutready state.layout
1541 then G.postRedisplay "tile nothrottle";
1544 | Idle | Loading _ | Outlining _ ->
1545 dolog "Inconsistent tiling state";
1546 logcurrently state.currently;
1547 exit 1
1550 | "pdim", args ->
1551 let (n, w, h, _) as pdim =
1552 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1554 let pdim =
1555 match conf.fitmodel with
1556 | FitWidth -> pdim
1557 | FitPage | FitProportional ->
1558 match conf.columns with
1559 | Csplit _ -> (n, w, h, 0)
1560 | Csingle _ | Cmulti _ -> pdim
1562 state.pdims <- pdim :: state.pdims;
1563 state.uioh#infochanged Pdim
1565 | "o", args ->
1566 let (l, n, t, h, pos) =
1567 scan args "%u %u %d %u %n"
1568 (fun l n t h pos -> l, n, t, h, pos)
1570 let s = String.sub args pos (String.length args - pos) in
1571 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
1573 | "ou", args ->
1574 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
1575 let s = String.sub args pos len in
1576 let pos2 = pos + len + 1 in
1577 let uri = String.sub args pos2 (String.length args - pos2) in
1578 addoutline (s, l, Ouri uri)
1580 | "on", args ->
1581 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
1582 let s = String.sub args pos (String.length args - pos) in
1583 addoutline (s, l, Onone)
1585 | "a", args ->
1586 let (n, l, t) =
1587 scan args "%u %d %d" (fun n l t -> n, l, t)
1589 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
1591 | "info", args ->
1592 let c, v = splitatchar args '\t' in
1593 let s =
1594 if nonemptystr v
1595 then
1596 if c = "Title"
1597 then (
1598 conf.title <- v;
1599 if not !ignoredoctitlte
1600 then Wsi.settitle v;
1601 args
1603 else
1604 if let len = String.length c in
1605 len > 6 && ((String.sub c (len-4) 4) = "date")
1606 then (
1607 if String.length v >= 7 && v.[0] = 'D' && v.[1] = ':'
1608 then
1609 let b = Buffer.create 10 in
1610 Printf.bprintf b "%s\t" c;
1611 let sub p l c =
1613 Buffer.add_substring b v p l;
1614 Buffer.add_char b c;
1615 with exn -> Buffer.add_string b @@ exntos exn
1617 sub 2 4 '/';
1618 sub 6 2 '/';
1619 sub 8 2 ' ';
1620 sub 10 2 ':';
1621 sub 12 2 ':';
1622 sub 14 2 ' ';
1623 Buffer.add_char b '[';
1624 Buffer.add_string b v;
1625 Buffer.add_char b ']';
1626 Buffer.contents b
1627 else args
1629 else args
1630 else args
1632 state.docinfo <- (1, s) :: state.docinfo
1634 | "infoend", "" ->
1635 state.docinfo <- List.rev state.docinfo;
1636 state.uioh#infochanged Docinfo
1638 | "pass", args ->
1639 if args = "fail"
1640 then Wsi.settitle "Wrong password";
1641 let password = getpassword () in
1642 if emptystr password
1643 then error "document is password protected"
1644 else opendoc state.path password
1646 | _ ->
1647 error "unknown cmd `%S'" cmds
1650 let onhist cb =
1651 let rc = cb.rc in
1652 let action = function
1653 | HCprev -> cbget cb ~-1
1654 | HCnext -> cbget cb 1
1655 | HCfirst -> cbget cb ~-(cb.rc)
1656 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1657 and cancel () = cb.rc <- rc
1658 in (action, cancel)
1661 let search pattern forward =
1662 match conf.columns with
1663 | Csplit _ -> impmsg "searching does not work properly in split columns mode"
1664 | Csingle _ | Cmulti _ ->
1665 if nonemptystr pattern
1666 then
1667 let pn, py =
1668 match state.layout with
1669 | [] -> 0, 0
1670 | l :: _ ->
1671 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1673 wcmd "search %d %d %d %d,%s\000"
1674 (btod conf.icase) pn py (btod forward) pattern;
1677 let [@warning "-4"] intentry text key =
1678 let text =
1679 if emptystr text && key = Keys.Ascii '-'
1680 then addchar text '-'
1681 else
1682 match key with
1683 | Keys.Ascii ('0'..'9' as c) -> addchar text c
1684 | _ ->
1685 state.text <- "invalid key";
1686 text
1688 TEcont text
1691 let linknact f s =
1692 if nonemptystr s
1693 then (
1694 let n =
1695 let l = String.length s in
1696 let rec loop pos n =
1697 if pos = l
1698 then n
1699 else
1700 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
1701 loop (pos+1) (n*26 + m)
1702 in loop 0 0
1704 let rec loop n = function
1705 | [] -> ()
1706 | l :: rest ->
1707 match getopaque l.pageno with
1708 | None -> loop n rest
1709 | Some opaque ->
1710 let m = getlinkcount opaque in
1711 if n < m
1712 then (
1713 let under = getlink opaque n in
1714 f under
1716 else loop (n-m) rest
1718 loop n state.layout;
1722 let [@warning "-4"] linknentry text = function
1723 | Keys.Ascii c ->
1724 let text = addchar text c in
1725 linknact (fun under -> state.text <- undertext under) text;
1726 TEcont text
1727 | _ ->
1728 state.text <- Printf.sprintf "invalid key";
1729 TEcont text
1732 let [@warning "-4"] textentry text = function
1733 | Keys.Ascii c -> TEcont (addchar text c)
1734 | Keys.Code c -> TEcont (text ^ toutf8 c)
1735 | _ -> TEcont text
1738 let reqlayout angle fitmodel =
1739 if nogeomcmds state.geomcmds
1740 then state.anchor <- getanchor ();
1741 conf.angle <- angle mod 360;
1742 if conf.angle != 0
1743 then (
1744 match state.mode with
1745 | LinkNav _ -> state.mode <- View
1746 | Birdseye _ | Textentry _ | View -> ()
1748 conf.fitmodel <- fitmodel;
1749 invalidate
1750 "reqlayout"
1751 (fun () ->
1752 wcmd "reqlayout %d %d %d"
1753 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
1757 let settrim trimmargins trimfuzz =
1758 if nogeomcmds state.geomcmds
1759 then state.anchor <- getanchor ();
1760 conf.trimmargins <- trimmargins;
1761 conf.trimfuzz <- trimfuzz;
1762 let x0, y0, x1, y1 = trimfuzz in
1763 invalidate
1764 "settrim" (fun () ->
1765 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
1766 flushpages ();
1769 let setzoom zoom =
1770 let zoom = max 0.0001 zoom in
1771 if zoom <> conf.zoom
1772 then (
1773 state.prevzoom <- (conf.zoom, state.x);
1774 conf.zoom <- zoom;
1775 reshape state.winw state.winh;
1776 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
1780 let pivotzoom ?(vw=min state.w state.winw)
1781 ?(vh=min (state.maxy-state.y) state.winh)
1782 ?(x=vw/2) ?(y=vh/2) zoom =
1783 let w = float state.w /. zoom in
1784 let hw = w /. 2.0 in
1785 let ratio = float vh /. float vw in
1786 let hh = hw *. ratio in
1787 let x0 = float x -. hw
1788 and y0 = float y -. hh in
1789 gotoxy (state.x - truncate x0) (state.y + truncate y0);
1790 setzoom zoom;
1793 let pivotzoom ?vw ?vh ?x ?y zoom =
1794 if nogeomcmds state.geomcmds
1795 then
1796 if zoom > 1.0
1797 then pivotzoom ?vw ?vh ?x ?y zoom
1798 else setzoom zoom
1801 let setcolumns mode columns coverA coverB =
1802 state.prevcolumns <- Some (conf.columns, conf.zoom);
1803 if columns < 0
1804 then (
1805 if isbirdseye mode
1806 then impmsg "split mode doesn't work in bird's eye"
1807 else (
1808 conf.columns <- Csplit (-columns, E.a);
1809 state.x <- 0;
1810 conf.zoom <- 1.0;
1813 else (
1814 if columns < 2
1815 then (
1816 conf.columns <- Csingle E.a;
1817 state.x <- 0;
1818 setzoom 1.0;
1820 else (
1821 conf.columns <- Cmulti ((columns, coverA, coverB), E.a);
1822 conf.zoom <- 1.0;
1825 reshape state.winw state.winh;
1828 let resetmstate () =
1829 state.mstate <- Mnone;
1830 Wsi.setcursor Wsi.CURSOR_INHERIT;
1833 let enterbirdseye () =
1834 let zoom = float conf.thumbw /. float state.winw in
1835 let birdseyepageno =
1836 let cy = state.winh / 2 in
1837 let fold = function
1838 | [] -> 0
1839 | l :: rest ->
1840 let rec fold best = function
1841 | [] -> best.pageno
1842 | l :: rest ->
1843 let d = cy - (l.pagedispy + l.pagevh/2)
1844 and dbest = cy - (best.pagedispy + best.pagevh/2) in
1845 if abs d < abs dbest
1846 then fold l rest
1847 else best.pageno
1848 in fold l rest
1850 fold state.layout
1852 state.mode <-
1853 Birdseye (
1854 { conf with zoom = conf.zoom },
1855 state.x, birdseyepageno, -1, getanchor ()
1857 resetmstate ();
1858 conf.zoom <- zoom;
1859 conf.presentation <- false;
1860 conf.interpagespace <- 10;
1861 conf.hlinks <- false;
1862 conf.fitmodel <- FitPage;
1863 state.x <- 0;
1864 conf.columns <- (
1865 match conf.beyecolumns with
1866 | Some c ->
1867 conf.zoom <- 1.0;
1868 Cmulti ((c, 0, 0), E.a)
1869 | None -> Csingle E.a
1871 if conf.verbose
1872 then
1873 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1874 (100.0*.zoom)
1875 else
1876 state.text <- E.s
1878 reshape state.winw state.winh;
1881 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
1882 state.mode <- View;
1883 conf.zoom <- c.zoom;
1884 conf.presentation <- c.presentation;
1885 conf.interpagespace <- c.interpagespace;
1886 conf.hlinks <- c.hlinks;
1887 conf.fitmodel <- c.fitmodel;
1888 conf.beyecolumns <- (
1889 match conf.columns with
1890 | Cmulti ((c, _, _), _) -> Some c
1891 | Csingle _ -> None
1892 | Csplit _ -> failwith "leaving bird's eye split mode"
1894 conf.columns <- (
1895 match c.columns with
1896 | Cmulti (c, _) -> Cmulti (c, E.a)
1897 | Csingle _ -> Csingle E.a
1898 | Csplit (c, _) -> Csplit (c, E.a)
1900 if conf.verbose
1901 then
1902 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1903 (100.0*.conf.zoom)
1905 reshape state.winw state.winh;
1906 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
1907 state.x <- leftx;
1910 let togglebirdseye () =
1911 match state.mode with
1912 | Birdseye vals -> leavebirdseye vals true
1913 | View -> enterbirdseye ()
1914 | Textentry _ | LinkNav _ -> ()
1917 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
1918 let pageno = max 0 (pageno - incr) in
1919 let rec loop = function
1920 | [] -> gotopage1 pageno 0
1921 | l :: _ when l.pageno = pageno ->
1922 if l.pagedispy >= 0 && l.pagey = 0
1923 then G.postRedisplay "upbirdseye"
1924 else gotopage1 pageno 0
1925 | _ :: rest -> loop rest
1927 loop state.layout;
1928 state.text <- E.s;
1929 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
1932 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
1933 let pageno = min (state.pagecount - 1) (pageno + incr) in
1934 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
1935 let rec loop = function
1936 | [] ->
1937 let y, h = getpageyh pageno in
1938 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
1939 gotoxy state.x (clamp dy)
1940 | l :: _ when l.pageno = pageno ->
1941 if l.pagevh != l.pageh
1942 then gotoxy state.x (clamp (l.pageh - l.pagevh + conf.interpagespace))
1943 else G.postRedisplay "downbirdseye"
1944 | _ :: rest -> loop rest
1946 loop state.layout;
1947 state.text <- E.s;
1950 let [@warning "-4"] optentry mode _ key =
1951 let btos b = if b then "on" else "off" in
1952 match key with
1953 | Keys.Ascii 's' ->
1954 let ondone s =
1955 try conf.scrollstep <- int_of_string s with exn ->
1956 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
1958 TEswitch ("scroll step: ", E.s, None, intentry, ondone, true)
1960 | Keys.Ascii 'A' ->
1961 let ondone s =
1963 conf.autoscrollstep <- boundastep state.winh (int_of_string s);
1964 if state.autoscroll <> None
1965 then state.autoscroll <- Some conf.autoscrollstep
1966 with exn ->
1967 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
1969 TEswitch ("auto scroll step: ", E.s, None, intentry, ondone, true)
1971 | Keys.Ascii 'C' ->
1972 let ondone s =
1974 let n, a, b = multicolumns_of_string s in
1975 setcolumns mode n a b;
1976 with exn ->
1977 state.text <- Printf.sprintf "bad columns `%s': %s" s @@ exntos exn
1979 TEswitch ("columns: ", E.s, None, textentry, ondone, true)
1981 | Keys.Ascii 'Z' ->
1982 let ondone s =
1984 let zoom = float (int_of_string s) /. 100.0 in
1985 pivotzoom zoom
1986 with exn ->
1987 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
1989 TEswitch ("zoom: ", E.s, None, intentry, ondone, true)
1991 | Keys.Ascii 't' ->
1992 let ondone s =
1994 conf.thumbw <- bound (int_of_string s) 2 4096;
1995 state.text <-
1996 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
1997 begin match mode with
1998 | Birdseye beye ->
1999 leavebirdseye beye false;
2000 enterbirdseye ();
2001 | Textentry _ | View | LinkNav _ -> ();
2003 with exn ->
2004 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2006 TEswitch ("thumbnail width: ", E.s, None, intentry, ondone, true)
2008 | Keys.Ascii 'R' ->
2009 let ondone s =
2010 match int_of_string s with
2011 | angle -> reqlayout angle conf.fitmodel
2012 | exception exn ->
2013 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2015 TEswitch ("rotation: ", E.s, None, intentry, ondone, true)
2017 | Keys.Ascii 'i' ->
2018 conf.icase <- not conf.icase;
2019 TEdone ("case insensitive search " ^ (btos conf.icase))
2021 | Keys.Ascii 'p' ->
2022 conf.preload <- not conf.preload;
2023 gotoxy state.x state.y;
2024 TEdone ("preload " ^ (btos conf.preload))
2026 | Keys.Ascii 'v' ->
2027 conf.verbose <- not conf.verbose;
2028 TEdone ("verbose " ^ (btos conf.verbose))
2030 | Keys.Ascii 'd' ->
2031 conf.debug <- not conf.debug;
2032 TEdone ("debug " ^ (btos conf.debug))
2034 | Keys.Ascii 'h' ->
2035 conf.maxhfit <- not conf.maxhfit;
2036 state.maxy <- calcheight ();
2037 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2039 | Keys.Ascii 'c' ->
2040 conf.crophack <- not conf.crophack;
2041 TEdone ("crophack " ^ btos conf.crophack)
2043 | Keys.Ascii 'f' ->
2044 conf.underinfo <- not conf.underinfo;
2045 TEdone ("underinfo " ^ btos conf.underinfo)
2047 | Keys.Ascii 'P' ->
2048 conf.savebmarks <- not conf.savebmarks;
2049 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2051 | Keys.Ascii 'S' ->
2052 let ondone s =
2054 let pageno, py =
2055 match state.layout with
2056 | [] -> 0, 0
2057 | l :: _ ->
2058 l.pageno, l.pagey
2060 conf.interpagespace <- int_of_string s;
2061 docolumns conf.columns;
2062 state.maxy <- calcheight ();
2063 let y = getpagey pageno in
2064 gotoxy state.x (y + py)
2065 with exn ->
2066 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2068 TEswitch ("vertical margin: ", E.s, None, intentry, ondone, true)
2070 | Keys.Ascii 'l' ->
2071 let fm =
2072 match conf.fitmodel with
2073 | FitProportional -> FitWidth
2074 | FitWidth | FitPage -> FitProportional
2076 reqlayout conf.angle fm;
2077 TEdone ("proportional display " ^ btos (fm == FitProportional))
2079 | Keys.Ascii 'T' ->
2080 settrim (not conf.trimmargins) conf.trimfuzz;
2081 TEdone ("trim margins " ^ btos conf.trimmargins)
2083 | Keys.Ascii 'I' ->
2084 conf.invert <- not conf.invert;
2085 TEdone ("invert colors " ^ btos conf.invert)
2087 | Keys.Ascii 'x' ->
2088 let ondone s =
2089 cbput state.hists.sel s;
2090 conf.selcmd <- s;
2092 TEswitch ("selection command: ", E.s, Some (onhist state.hists.sel),
2093 textentry, ondone, true)
2095 | Keys.Ascii 'M' ->
2096 if conf.pax == None
2097 then conf.pax <- Some 0.0
2098 else conf.pax <- None;
2099 TEdone ("PAX " ^ btos (conf.pax != None))
2101 | (Keys.Ascii c) ->
2102 state.text <- Printf.sprintf "bad option %d `%c'"
2103 (Char.code c) c;
2104 TEstop
2106 | _ ->
2107 TEcont state.text
2110 class type lvsource =
2111 object
2112 method getitemcount : int
2113 method getitem : int -> (string * int)
2114 method hasaction : int -> bool
2115 method exit : uioh:uioh ->
2116 cancel:bool ->
2117 active:int ->
2118 first:int ->
2119 pan:int ->
2120 uioh option
2121 method getactive : int
2122 method getfirst : int
2123 method getpan : int
2124 method getminfo : (int * int) array
2125 end;;
2127 class virtual lvsourcebase = object
2128 val mutable m_active = 0
2129 val mutable m_first = 0
2130 val mutable m_pan = 0
2131 method getactive = m_active
2132 method getfirst = m_first
2133 method getpan = m_pan
2134 method getminfo : (int * int) array = E.a
2135 end;;
2137 let [@warning "-4"]
2138 textentrykeyboard
2139 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2140 state.text <- E.s;
2141 let enttext te =
2142 state.mode <- Textentry (te, onleave);
2143 enttext ();
2144 G.postRedisplay "textentrykeyboard enttext";
2146 let histaction cmd =
2147 match opthist with
2148 | None -> ()
2149 | Some (action, _) ->
2150 state.mode <-
2151 Textentry (
2152 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
2154 G.postRedisplay "textentry histaction"
2156 let open Keys in
2157 let kt = Wsi.kc2kt key in
2158 match kt with
2159 | Backspace ->
2160 if emptystr text && cancelonempty
2161 then (
2162 onleave Cancel;
2163 G.postRedisplay "textentrykeyboard after cancel";
2165 else
2166 let s = withoutlastutf8 text in
2167 enttext (c, s, opthist, onkey, ondone, cancelonempty)
2169 | Enter ->
2170 ondone text;
2171 onleave Confirm;
2172 G.postRedisplay "textentrykeyboard after confirm"
2174 | Up -> histaction HCprev
2175 | Down -> histaction HCnext
2176 | Home -> histaction HCfirst
2177 | End -> histaction HClast
2179 | Escape ->
2180 if emptystr text
2181 then (
2182 begin match opthist with
2183 | None -> ()
2184 | Some (_, onhistcancel) -> onhistcancel ()
2185 end;
2186 onleave Cancel;
2187 state.text <- E.s;
2188 G.postRedisplay "textentrykeyboard after cancel2"
2190 else (
2191 enttext (c, E.s, opthist, onkey, ondone, cancelonempty)
2194 | Delete -> ()
2196 | Code _ | Ascii _ ->
2197 begin match onkey text kt with
2198 | TEdone text ->
2199 ondone text;
2200 onleave Confirm;
2201 G.postRedisplay "textentrykeyboard after confirm2";
2203 | TEcont text ->
2204 enttext (c, text, opthist, onkey, ondone, cancelonempty);
2206 | TEstop ->
2207 onleave Cancel;
2208 G.postRedisplay "textentrykeyboard after cancel3"
2210 | TEswitch te ->
2211 state.mode <- Textentry (te, onleave);
2212 G.postRedisplay "textentrykeyboard switch";
2214 | _ -> vlog "unhandled key"
2217 let firstof first active =
2218 if first > active || abs (first - active) > fstate.maxrows - 1
2219 then max 0 (active - (fstate.maxrows/2))
2220 else first
2223 let calcfirst first active =
2224 if active > first
2225 then
2226 let rows = active - first in
2227 if rows > fstate.maxrows then active - fstate.maxrows else first
2228 else active
2231 let scrollph y maxy =
2232 let sh = float (maxy + state.winh) /. float state.winh in
2233 let sh = float state.winh /. sh in
2234 let sh = max sh (float conf.scrollh) in
2236 let percent = float y /. float maxy in
2237 let position = (float state.winh -. sh) *. percent in
2239 let position =
2240 if position +. sh > float state.winh
2241 then float state.winh -. sh
2242 else position
2244 position, sh;
2247 let adderrmsg src msg =
2248 Buffer.add_string state.errmsgs msg;
2249 state.newerrmsgs <- true;
2250 G.postRedisplay src
2253 let adderrfmt src fmt =
2254 Format.ksprintf (fun s -> adderrmsg src s) fmt;
2257 let coe s = (s :> uioh);;
2259 class listview ~zebra ~helpmode ~(source:lvsource) ~trusted ~modehash =
2260 object (self)
2261 val m_pan = source#getpan
2262 val m_first = source#getfirst
2263 val m_active = source#getactive
2264 val m_qsearch = E.s
2265 val m_prev_uioh = state.uioh
2267 method private elemunder y =
2268 if y < 0
2269 then None
2270 else
2271 let n = y / (fstate.fontsize+1) in
2272 if m_first + n < source#getitemcount
2273 then (
2274 if source#hasaction (m_first + n)
2275 then Some (m_first + n)
2276 else None
2278 else None
2280 method display =
2281 Gl.enable `blend;
2282 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
2283 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2284 filledrect 0. 0. (float state.winw) (float state.winh);
2285 GlDraw.color (1., 1., 1.);
2286 Gl.enable `texture_2d;
2287 let fs = fstate.fontsize in
2288 let nfs = fs + 1 in
2289 let hw = state.winw/3 in
2290 let ww = fstate.wwidth in
2291 let tabw = 17.0*.ww in
2292 let itemcount = source#getitemcount in
2293 let minfo = source#getminfo in
2294 if conf.leftscroll
2295 then (
2296 GlMat.push ();
2297 GlMat.translate ~x:(float conf.scrollbw) ();
2299 let x0 = 0.0 and x1 = float (state.winw - conf.scrollbw - 1) in
2300 let rec loop row =
2301 if (row - m_first) > fstate.maxrows
2302 then ()
2303 else (
2304 if row >= 0 && row < itemcount
2305 then (
2306 let (s, level) = source#getitem row in
2307 let y = (row - m_first) * nfs in
2308 let x = 5.0 +. (float (level + m_pan)) *. ww in
2309 if helpmode
2310 then GlDraw.color
2311 (let c = if row land 1 = 0 then 1.0 else 0.92 in (c,c,c));
2313 if row = m_active
2314 then (
2315 Gl.disable `texture_2d;
2316 let alpha = if source#hasaction row then 0.9 else 0.3 in
2317 GlDraw.color (1., 1., 1.) ~alpha;
2318 linerect (x0 +. 1.) (float (y + 1)) (x1) (float (y + fs + 3));
2319 Gl.enable `texture_2d;
2321 let c =
2322 if zebra && row land 1 = 1
2323 then 0.8
2324 else 1.0
2326 GlDraw.color (c,c,c);
2327 let drawtabularstring s =
2328 let drawstr x s =
2329 let x' = truncate (x0 +. x) in
2330 let s1, s2 = splitatchar s '\000' in
2331 if emptystr s2
2332 then drawstring1 fs x' (y+nfs) s
2333 else
2334 let rec e s =
2335 if emptystr s
2336 then s
2337 else
2338 let s' = withoutlastutf8 s in
2339 let s = s' ^ Unisyms.ellipsis in
2340 let w = measurestr fs s in
2341 if float x' +. w +. ww < float (hw + x')
2342 then s
2343 else e s'
2345 let s1 =
2346 if float x' +. ww +. measurestr fs s1 > float (hw + x')
2347 then e s1
2348 else s1
2350 ignore (drawstring1 fs x' (y+nfs) s1);
2351 drawstring1 fs (hw + x') (y+nfs) s2
2353 if trusted
2354 then
2355 let x = if helpmode && row > 0 then x +. ww else x in
2356 let s1, s2 = splitatchar s '\t' in
2357 if nonemptystr s2
2358 then
2359 let nx = drawstr x s1 in
2360 let sw = nx -. x in
2361 let x = x +. (max tabw sw) in
2362 drawstr x s2
2363 else
2364 let len = String.length s - 2 in
2365 if len > 0 && s.[0] = '\xc2' && s.[1] = '\xb7'
2366 then
2367 let s = String.sub s 2 len in
2368 let x = if not helpmode then x +. ww else x in
2369 GlDraw.color (1.2, 1.2, 1.2);
2370 let vinc = drawstring1 (fs+fs/4)
2371 (truncate (x -. ww)) (y+nfs) s in
2372 GlDraw.color (1., 1., 1.);
2373 vinc +. (float fs *. 0.8)
2374 else
2375 drawstr x s
2376 else
2377 drawstr x s
2379 ignore (drawtabularstring s);
2380 loop (row+1)
2384 loop m_first;
2385 GlDraw.color (1.0, 1.0, 1.0) ~alpha:0.5;
2386 let xadj = 5.0 in
2387 let rec loop row =
2388 if (row - m_first) <= fstate.maxrows
2389 then
2390 if row >= 0 && row < itemcount
2391 then (
2392 let (s, level) = source#getitem row in
2393 let pos0 = nindex s '\000' in
2394 let y = (row - m_first) * nfs in
2395 let x = float (level + m_pan) *. ww in
2396 let (first, last) = minfo.(row) in
2397 let prefix =
2398 if pos0 > 0 && first > pos0
2399 then String.sub s (pos0+1) (first-pos0-1)
2400 else String.sub s 0 first
2402 let suffix = String.sub s first (last - first) in
2403 let w1 = measurestr fstate.fontsize prefix in
2404 let w2 = measurestr fstate.fontsize suffix in
2405 let x = x +. if conf.leftscroll then xadj else 5.0 in
2406 let x = if pos0 > 0 && first > pos0 then x +. float hw else x in
2407 let x0 = x +. w1
2408 and y0 = float (y+2) in
2409 let x1 = x0 +. w2
2410 and y1 = float (y+fs+3) in
2411 filledrect x0 y0 x1 y1;
2412 loop (row+1)
2415 Gl.disable `texture_2d;
2416 if Array.length minfo > 0 then loop m_first;
2417 Gl.disable `blend;
2418 if conf.leftscroll
2419 then GlMat.pop ()
2421 method updownlevel incr =
2422 let len = source#getitemcount in
2423 let curlevel =
2424 if m_active >= 0 && m_active < len
2425 then snd (source#getitem m_active)
2426 else -1
2428 let rec flow i =
2429 if i = len then i-1 else if i = -1 then 0 else
2430 let _, l = source#getitem i in
2431 if l != curlevel then i else flow (i+incr)
2433 let active = flow m_active in
2434 let first = calcfirst m_first active in
2435 G.postRedisplay "outline updownlevel";
2436 {< m_active = active; m_first = first >}
2438 method private key1 key mask =
2439 let set1 active first qsearch =
2440 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2442 let search active pattern incr =
2443 let active = if active = -1 then m_first else active in
2444 let dosearch re =
2445 let rec loop n =
2446 if n >= 0 && n < source#getitemcount
2447 then (
2448 let s, _ = source#getitem n in
2449 match Str.search_forward re s 0 with
2450 | exception Not_found -> loop (n + incr)
2451 | _ -> Some n
2453 else None
2455 loop active
2457 let qpat = Str.quote pattern in
2458 match Str.regexp_case_fold qpat with
2459 | s -> dosearch s
2460 | exception exn ->
2461 adderrfmt "listview key1" "regexp_case_fold for `%S' failed: %S\n"
2462 qpat @@ Printexc.to_string exn;
2463 None
2465 let itemcount = source#getitemcount in
2466 let find start incr =
2467 let rec find i =
2468 if i = -1 || i = itemcount
2469 then -1
2470 else (
2471 if source#hasaction i
2472 then i
2473 else find (i + incr)
2476 find start
2478 let set active first =
2479 let first = bound first 0 (itemcount - fstate.maxrows) in
2480 state.text <- E.s;
2481 coe {< m_active = active; m_first = first; m_qsearch = E.s >}
2483 let navigate incr =
2484 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2485 let active, first =
2486 let incr1 = if incr > 0 then 1 else -1 in
2487 if isvisible m_first m_active
2488 then
2489 let next =
2490 let next = m_active + incr in
2491 let next =
2492 if next < 0 || next >= itemcount
2493 then -1
2494 else find next incr1
2496 if abs (m_active - next) > fstate.maxrows
2497 then -1
2498 else next
2500 if next = -1
2501 then
2502 let first = m_first + incr in
2503 let first = bound first 0 (itemcount - fstate.maxrows) in
2504 let next =
2505 let next = m_active + incr in
2506 let next = bound next 0 (itemcount - 1) in
2507 find next ~-incr1
2509 let active =
2510 if next = -1
2511 then m_active
2512 else (
2513 if isvisible first next
2514 then next
2515 else m_active
2518 active, first
2519 else
2520 let first = min next m_first in
2521 let first =
2522 if abs (next - first) > fstate.maxrows
2523 then first + incr
2524 else first
2526 next, first
2527 else
2528 let first = m_first + incr in
2529 let first = bound first 0 (itemcount - 1) in
2530 let active =
2531 let next = m_active + incr in
2532 let next = bound next 0 (itemcount - 1) in
2533 let next = find next incr1 in
2534 let active =
2535 if next = -1 || abs (m_active - first) > fstate.maxrows
2536 then (
2537 let active = if m_active = -1 then next else m_active in
2538 active
2540 else next
2542 if isvisible first active
2543 then active
2544 else -1
2546 active, first
2548 G.postRedisplay "listview navigate";
2549 set active first;
2551 let open Keys in
2552 let kt = Wsi.kc2kt key in
2553 match [@warning "-4"] kt with
2554 | Ascii (('r'|'s') as c) when Wsi.withctrl mask ->
2555 let incr = if c = 'r' then -1 else 1 in
2556 let active, first =
2557 match search (m_active + incr) m_qsearch incr with
2558 | None ->
2559 state.text <- m_qsearch ^ " [not found]";
2560 m_active, m_first
2561 | Some active ->
2562 state.text <- m_qsearch;
2563 active, firstof m_first active
2565 G.postRedisplay "listview ctrl-r/s";
2566 set1 active first m_qsearch;
2568 | Insert when Wsi.withctrl mask ->
2569 if m_active >= 0 && m_active < source#getitemcount
2570 then (
2571 let s, _ = source#getitem m_active in
2572 selstring conf.selcmd s;
2574 coe self
2576 | Backspace ->
2577 if emptystr m_qsearch
2578 then coe self
2579 else (
2580 let qsearch = withoutlastutf8 m_qsearch in
2581 if emptystr qsearch
2582 then (
2583 state.text <- E.s;
2584 G.postRedisplay "listview empty qsearch";
2585 set1 m_active m_first E.s;
2587 else
2588 let active, first =
2589 match search m_active qsearch ~-1 with
2590 | None ->
2591 state.text <- qsearch ^ " [not found]";
2592 m_active, m_first
2593 | Some active ->
2594 state.text <- qsearch;
2595 active, firstof m_first active
2597 G.postRedisplay "listview backspace qsearch";
2598 set1 active first qsearch
2601 | Ascii _ | Code _ ->
2602 let utf8 =
2603 match [@warning "-8"] kt with
2604 | Ascii c -> String.make 1 c
2605 | Code code -> toutf8 code
2607 let pattern = m_qsearch ^ utf8 in
2608 let active, first =
2609 match search m_active pattern 1 with
2610 | None ->
2611 state.text <- pattern ^ " [not found]";
2612 m_active, m_first
2613 | Some active ->
2614 state.text <- pattern;
2615 active, firstof m_first active
2617 G.postRedisplay "listview qsearch add";
2618 set1 active first pattern;
2620 | Escape ->
2621 state.text <- E.s;
2622 if emptystr m_qsearch
2623 then (
2624 G.postRedisplay "list view escape";
2625 let mx, my = state.mpos in
2626 updateunder mx my;
2627 match source#exit ~uioh:(coe self) ~cancel:true ~active:m_active
2628 ~first:m_first ~pan:m_pan with
2629 | None -> m_prev_uioh
2630 | Some uioh -> uioh
2632 else (
2633 G.postRedisplay "list view kill qsearch";
2634 coe {< m_qsearch = E.s >}
2637 | Enter ->
2638 state.text <- E.s;
2639 let self = {< m_qsearch = E.s >} in
2640 let opt =
2641 G.postRedisplay "listview enter";
2642 let cancel = not (m_active >= 0 && m_active < source#getitemcount) in
2643 source#exit ~uioh:(coe self) ~cancel
2644 ~active:m_active ~first:m_first ~pan:m_pan;
2646 begin match opt with
2647 | None -> m_prev_uioh
2648 | Some uioh -> uioh
2651 | Delete ->
2652 coe self
2654 | Up -> navigate ~-1
2655 | Down -> navigate 1
2656 | Prior -> navigate ~-(fstate.maxrows)
2657 | Next -> navigate fstate.maxrows
2659 | Right ->
2660 state.text <- E.s;
2661 G.postRedisplay "listview right";
2662 coe {< m_pan = m_pan - 1 >}
2664 | Left ->
2665 state.text <- E.s;
2666 G.postRedisplay "listview left";
2667 coe {< m_pan = m_pan + 1 >}
2669 | Home ->
2670 let active = find 0 1 in
2671 G.postRedisplay "listview home";
2672 set active 0;
2674 | End ->
2675 let first = max 0 (itemcount - fstate.maxrows) in
2676 let active = find (itemcount - 1) ~-1 in
2677 G.postRedisplay "listview end";
2678 set active first;
2680 | _ -> coe self
2682 method key key mask =
2683 match state.mode with
2684 | Textentry te ->
2685 textentrykeyboard key mask te;
2686 coe self
2687 | Birdseye _ | View | LinkNav _ -> self#key1 key mask
2689 method button button down x y _ =
2690 let opt =
2691 match button with
2692 | 1 when vscrollhit x ->
2693 G.postRedisplay "listview scroll";
2694 if down
2695 then
2696 let _, position, sh = self#scrollph in
2697 if y > truncate position && y < truncate (position +. sh)
2698 then (
2699 state.mstate <- Mscrolly;
2700 Some (coe self)
2702 else
2703 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
2704 let first = truncate (s *. float source#getitemcount) in
2705 let first = min source#getitemcount first in
2706 Some (coe {< m_first = first; m_active = first >})
2707 else (
2708 state.mstate <- Mnone;
2709 Some (coe self);
2711 | 1 when down ->
2712 begin match self#elemunder y with
2713 | Some n ->
2714 G.postRedisplay "listview click";
2715 source#exit ~uioh:(coe {< m_active = n >})
2716 ~cancel:false ~active:n ~first:m_first ~pan:m_pan
2717 | _ ->
2718 Some (coe self)
2720 | n when (n == 4 || n == 5) && not down ->
2721 let len = source#getitemcount in
2722 let first =
2723 if n = 5 && m_first + fstate.maxrows >= len
2724 then
2725 m_first
2726 else
2727 let first = m_first + (if n == 4 then -1 else 1) in
2728 bound first 0 (len - 1)
2730 G.postRedisplay "listview wheel";
2731 Some (coe {< m_first = first >})
2732 | n when (n = 6 || n = 7) && not down ->
2733 let inc = if n = 7 then -1 else 1 in
2734 G.postRedisplay "listview hwheel";
2735 Some (coe {< m_pan = m_pan + inc >})
2736 | _ ->
2737 Some (coe self)
2739 match opt with
2740 | None -> m_prev_uioh
2741 | Some uioh -> uioh
2743 method multiclick _ x y = self#button 1 true x y
2745 method motion _ y =
2746 match state.mstate with
2747 | Mscrolly ->
2748 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
2749 let first = truncate (s *. float source#getitemcount) in
2750 let first = min source#getitemcount first in
2751 G.postRedisplay "listview motion";
2752 coe {< m_first = first; m_active = first >}
2753 | Msel _
2754 | Mpan _
2755 | Mscrollx
2756 | Mzoom _
2757 | Mzoomrect _
2758 | Mnone -> coe self
2760 method pmotion x y =
2761 if x < state.winw - conf.scrollbw
2762 then
2763 let n =
2764 match self#elemunder y with
2765 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
2766 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
2768 let o =
2769 if n != m_active
2770 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
2771 else self
2773 coe o
2774 else (
2775 Wsi.setcursor Wsi.CURSOR_INHERIT;
2776 coe self
2779 method infochanged _ = ()
2781 method scrollpw = (0, 0.0, 0.0)
2782 method scrollph =
2783 let nfs = fstate.fontsize + 1 in
2784 let y = m_first * nfs in
2785 let itemcount = source#getitemcount in
2786 let maxi = max 0 (itemcount - fstate.maxrows) in
2787 let maxy = maxi * nfs in
2788 let p, h = scrollph y maxy in
2789 conf.scrollbw, p, h
2791 method modehash = modehash
2792 method eformsgs = false
2793 method alwaysscrolly = true
2794 method scroll _ dy =
2795 let self =
2796 if dy != 0 then begin
2797 let len = source#getitemcount in
2798 let first =
2799 if dy > 0 && m_first + fstate.maxrows >= len
2800 then
2801 m_first
2802 else
2803 let first = m_first + dy / 10 in
2804 bound first 0 (len - 1)
2806 G.postRedisplay "listview wheel";
2807 {< m_first = first >}
2808 end else
2809 self
2811 coe self
2813 method zoom _ _ _ = ()
2814 end;;
2816 class outlinelistview ~zebra ~source =
2817 let settext autonarrow s =
2818 if autonarrow
2819 then
2820 let ss = source#statestr in
2821 state.text <-
2822 if emptystr ss
2823 then "[" ^ s ^ "]"
2824 else "{" ^ ss ^ "} [" ^ s ^ "]"
2825 else state.text <- s
2827 object (self)
2828 inherit listview
2829 ~zebra
2830 ~helpmode:false
2831 ~source:(source :> lvsource)
2832 ~trusted:false
2833 ~modehash:(findkeyhash conf "outline")
2834 as super
2836 val m_autonarrow = false
2838 method! key key mask =
2839 let maxrows =
2840 if emptystr state.text
2841 then fstate.maxrows
2842 else fstate.maxrows - 2
2844 let calcfirst first active =
2845 if active > first
2846 then
2847 let rows = active - first in
2848 if rows > maxrows then active - maxrows else first
2849 else active
2851 let navigate incr =
2852 let active = m_active + incr in
2853 let active = bound active 0 (source#getitemcount - 1) in
2854 let first = calcfirst m_first active in
2855 G.postRedisplay "outline navigate";
2856 coe {< m_active = active; m_first = first >}
2858 let navscroll first =
2859 let active =
2860 let dist = m_active - first in
2861 if dist < 0
2862 then first
2863 else (
2864 if dist < maxrows
2865 then m_active
2866 else first + maxrows
2869 G.postRedisplay "outline navscroll";
2870 coe {< m_first = first; m_active = active >}
2872 let ctrl = Wsi.withctrl mask in
2873 let open Keys in
2874 match Wsi.kc2kt key with
2875 | Ascii 'a' when ctrl ->
2876 let text =
2877 if m_autonarrow
2878 then (source#denarrow; E.s)
2879 else (
2880 let pattern = source#renarrow in
2881 if nonemptystr m_qsearch
2882 then (source#narrow m_qsearch; m_qsearch)
2883 else pattern
2886 settext (not m_autonarrow) text;
2887 G.postRedisplay "toggle auto narrowing";
2888 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
2890 | Ascii '/' when emptystr m_qsearch && not m_autonarrow ->
2891 settext true E.s;
2892 G.postRedisplay "toggle auto narrowing";
2893 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
2895 | Ascii 'n' when ctrl ->
2896 source#narrow m_qsearch;
2897 if not m_autonarrow
2898 then source#add_narrow_pattern m_qsearch;
2899 G.postRedisplay "outline ctrl-n";
2900 coe {< m_first = 0; m_active = 0 >}
2902 | Ascii 'S' when ctrl ->
2903 let active = source#calcactive (getanchor ()) in
2904 let first = firstof m_first active in
2905 G.postRedisplay "outline ctrl-s";
2906 coe {< m_first = first; m_active = active >}
2908 | Ascii 'u' when ctrl ->
2909 G.postRedisplay "outline ctrl-u";
2910 if m_autonarrow && nonemptystr m_qsearch
2911 then (
2912 ignore (source#renarrow);
2913 settext m_autonarrow E.s;
2914 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
2916 else (
2917 source#del_narrow_pattern;
2918 let pattern = source#renarrow in
2919 let text =
2920 if emptystr pattern then E.s else "Narrowed to " ^ pattern
2922 settext m_autonarrow text;
2923 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
2926 | Ascii 'l' when ctrl ->
2927 let first = max 0 (m_active - (fstate.maxrows / 2)) in
2928 G.postRedisplay "outline ctrl-l";
2929 coe {< m_first = first >}
2931 | Ascii '\t' when m_autonarrow ->
2932 if nonemptystr m_qsearch
2933 then (
2934 G.postRedisplay "outline list view tab";
2935 source#add_narrow_pattern m_qsearch;
2936 settext true E.s;
2937 coe {< m_qsearch = E.s >}
2939 else coe self
2941 | Escape when m_autonarrow ->
2942 if nonemptystr m_qsearch
2943 then source#add_narrow_pattern m_qsearch;
2944 super#key key mask
2946 | Enter when m_autonarrow ->
2947 if nonemptystr m_qsearch
2948 then source#add_narrow_pattern m_qsearch;
2949 super#key key mask
2951 | (Ascii _ | Code _) when m_autonarrow ->
2952 let pattern = m_qsearch ^ toutf8 key in
2953 G.postRedisplay "outlinelistview autonarrow add";
2954 source#narrow pattern;
2955 settext true pattern;
2956 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
2958 | Backspace when m_autonarrow ->
2959 if emptystr m_qsearch
2960 then coe self
2961 else
2962 let pattern = withoutlastutf8 m_qsearch in
2963 G.postRedisplay "outlinelistview autonarrow backspace";
2964 ignore (source#renarrow);
2965 source#narrow pattern;
2966 settext true pattern;
2967 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
2969 | Up when ctrl ->
2970 navscroll (max 0 (m_first - 1))
2972 | Down when ctrl ->
2973 navscroll (min (source#getitemcount - 1) (m_first + 1))
2975 | Up -> navigate ~-1
2976 | Down -> navigate 1
2977 | Prior -> navigate ~-(fstate.maxrows)
2978 | Next -> navigate fstate.maxrows
2980 | Right ->
2981 let o =
2982 if ctrl
2983 then (
2984 G.postRedisplay "outline ctrl right";
2985 {< m_pan = m_pan + 1 >}
2987 else self#updownlevel 1
2989 coe o
2991 | Left ->
2992 let o =
2993 if ctrl
2994 then (
2995 G.postRedisplay "outline ctrl left";
2996 {< m_pan = m_pan - 1 >}
2998 else self#updownlevel ~-1
3000 coe o
3002 | Home ->
3003 G.postRedisplay "outline home";
3004 coe {< m_first = 0; m_active = 0 >}
3006 | End ->
3007 let active = source#getitemcount - 1 in
3008 let first = max 0 (active - fstate.maxrows) in
3009 G.postRedisplay "outline end";
3010 coe {< m_active = active; m_first = first >}
3012 | Delete|Escape|Insert|Enter|Ascii _|Code _|Ctrl _|Backspace|Fn _ ->
3013 super#key key mask
3014 end;;
3016 let genhistoutlines () =
3017 Config.gethist ()
3018 |> List.sort (fun (_, c1, _, _, _, _) (_, c2, _, _, _, _) ->
3019 compare c2.lastvisit c1.lastvisit)
3020 |> List.map (fun ((path, c, _, _, _, origin) as hist) ->
3021 let path = if nonemptystr origin then origin else path in
3022 let base = mbtoutf8 @@ Filename.basename path in
3023 (base ^ "\000" ^ c.title, 1, Ohistory hist)
3027 let gotohist (path, c, bookmarks, x, anchor, origin) =
3028 Config.save leavebirdseye;
3029 state.anchor <- anchor;
3030 state.bookmarks <- bookmarks;
3031 state.origin <- origin;
3032 state.x <- x;
3033 setconf conf c;
3034 let x0, y0, x1, y1 = conf.trimfuzz in
3035 wcmd "trimset %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1;
3036 reshape ~firsttime:true state.winw state.winh;
3037 opendoc path origin;
3038 setzoom c.zoom;
3041 let setcheckers enabled =
3042 match state.checkerstexid with
3043 | None ->
3044 if enabled then state.checkerstexid <- Some (makecheckers ())
3046 | Some checkerstexid ->
3047 if not enabled
3048 then (
3049 GlTex.delete_texture checkerstexid;
3050 state.checkerstexid <- None;
3054 let describe_layout layout =
3055 let d =
3056 match layout with
3057 | [] -> "Page 0"
3058 | l :: [] -> Printf.sprintf "Page %d" (l.pageno+1)
3059 | l :: rest ->
3060 let rangestr a b =
3061 if a.pageno = b.pageno then Printf.sprintf "%d" (a.pageno+1)
3062 else
3063 let sep = if a.pageno+1 = b.pageno then ", " else Unisyms.ellipsis in
3064 Printf.sprintf "%d%s%d" (a.pageno+1) sep (b.pageno+1)
3066 let rec fold s la lb = function
3067 | [] -> Printf.sprintf "%s %s" s (rangestr la lb)
3068 | l :: rest when l.pageno = succ lb.pageno -> fold s la l rest
3069 | l :: rest -> fold (s ^ " " ^ rangestr la lb ^ ",") l l rest
3071 fold "Pages" l l rest
3073 let percent =
3074 let maxy = maxy () in
3075 if maxy <= 0
3076 then 100.
3077 else 100. *. (float state.y /. float maxy)
3079 Printf.sprintf "%s of %d [%.2f%%]" d state.pagecount percent
3082 let setpresentationmode v =
3083 let n = page_of_y state.y in
3084 state.anchor <- (n, 0.0, 1.0);
3085 conf.presentation <- v;
3086 if conf.fitmodel = FitPage
3087 then reqlayout conf.angle conf.fitmodel;
3088 represent ();
3091 let enterinfomode =
3092 let btos b = if b then Unisyms.radical else E.s in
3093 let showextended = ref false in
3094 let showcolors = ref false in
3095 let leave mode _ = state.mode <- mode in
3096 let src =
3097 (object
3098 val mutable m_l = []
3099 val mutable m_a = E.a
3100 val mutable m_prev_uioh = nouioh
3101 val mutable m_prev_mode = View
3103 inherit lvsourcebase
3105 method reset prev_mode prev_uioh =
3106 m_a <- Array.of_list (List.rev m_l);
3107 m_l <- [];
3108 m_prev_mode <- prev_mode;
3109 m_prev_uioh <- prev_uioh;
3111 method int name get set =
3112 m_l <-
3113 (name, `int get, 1,
3114 Action (
3115 fun u ->
3116 let ondone s =
3117 try set (int_of_string s)
3118 with exn ->
3119 state.text <- Printf.sprintf "bad integer `%s': %s"
3120 s @@ exntos exn
3122 state.text <- E.s;
3123 let te = name ^ ": ", E.s, None, intentry, ondone, true in
3124 state.mode <- Textentry (te, leave m_prev_mode);
3126 )) :: m_l
3128 method int_with_suffix name get set =
3129 m_l <-
3130 (name, `intws get, 1,
3131 Action (
3132 fun u ->
3133 let ondone s =
3134 try set (int_of_string_with_suffix s)
3135 with exn ->
3136 state.text <- Printf.sprintf "bad integer `%s': %s"
3137 s @@ exntos exn
3139 state.text <- E.s;
3140 let te =
3141 name ^ ": ", E.s, None, intentry_with_suffix, ondone, true
3143 state.mode <- Textentry (te, leave m_prev_mode);
3145 )) :: m_l
3147 method bool ?(offset=1) ?(btos=btos) name get set =
3148 m_l <-
3149 (name, `bool (btos, get), offset, Action (
3150 fun u ->
3151 let v = get () in
3152 set (not v);
3154 )) :: m_l
3156 method color name get set =
3157 m_l <-
3158 (name, `color get, 1,
3159 Action (
3160 fun u ->
3161 let invalid = (nan, nan, nan) in
3162 let ondone s =
3163 let c =
3164 try color_of_string s
3165 with exn ->
3166 state.text <- Printf.sprintf "bad color `%s': %s"
3167 s @@ exntos exn;
3168 invalid
3170 if c <> invalid
3171 then set c;
3173 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3174 state.text <- color_to_string (get ());
3175 state.mode <- Textentry (te, leave m_prev_mode);
3177 )) :: m_l
3179 method string name get set =
3180 m_l <-
3181 (name, `string get, 1,
3182 Action (
3183 fun u ->
3184 let ondone s = set s in
3185 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3186 state.mode <- Textentry (te, leave m_prev_mode);
3188 )) :: m_l
3190 method colorspace name get set =
3191 m_l <-
3192 (name, `string get, 1,
3193 Action (
3194 fun _ ->
3195 let source =
3196 (object
3197 inherit lvsourcebase
3199 initializer
3200 m_active <- CSTE.to_int conf.colorspace;
3201 m_first <- 0;
3203 method getitemcount =
3204 Array.length CSTE.names
3205 method getitem n =
3206 (CSTE.names.(n), 0)
3207 method exit ~uioh ~cancel ~active ~first ~pan =
3208 ignore (uioh, first, pan);
3209 if not cancel then set active;
3210 None
3211 method hasaction _ = true
3212 end)
3214 state.text <- E.s;
3215 let modehash = findkeyhash conf "info" in
3216 coe (new listview ~zebra:false ~helpmode:false
3217 ~source ~trusted:true ~modehash)
3218 )) :: m_l
3220 method paxmark name get set =
3221 m_l <-
3222 (name, `string get, 1,
3223 Action (
3224 fun _ ->
3225 let source =
3226 (object
3227 inherit lvsourcebase
3229 initializer
3230 m_active <- MTE.to_int conf.paxmark;
3231 m_first <- 0;
3233 method getitemcount = Array.length MTE.names
3234 method getitem n = (MTE.names.(n), 0)
3235 method exit ~uioh ~cancel ~active ~first ~pan =
3236 ignore (uioh, first, pan);
3237 if not cancel then set active;
3238 None
3239 method hasaction _ = true
3240 end)
3242 state.text <- E.s;
3243 let modehash = findkeyhash conf "info" in
3244 coe (new listview ~zebra:false ~helpmode:false
3245 ~source ~trusted:true ~modehash)
3246 )) :: m_l
3248 method fitmodel name get set =
3249 m_l <-
3250 (name, `string get, 1,
3251 Action (
3252 fun _ ->
3253 let source =
3254 (object
3255 inherit lvsourcebase
3257 initializer
3258 m_active <- FMTE.to_int conf.fitmodel;
3259 m_first <- 0;
3261 method getitemcount = Array.length FMTE.names
3262 method getitem n = (FMTE.names.(n), 0)
3263 method exit ~uioh ~cancel ~active ~first ~pan =
3264 ignore (uioh, first, pan);
3265 if not cancel then set active;
3266 None
3267 method hasaction _ = true
3268 end)
3270 state.text <- E.s;
3271 let modehash = findkeyhash conf "info" in
3272 coe (new listview ~zebra:false ~helpmode:false
3273 ~source ~trusted:true ~modehash)
3274 )) :: m_l
3276 method caption s offset =
3277 m_l <- (s, `empty, offset, Noaction) :: m_l
3279 method caption2 s f offset =
3280 m_l <- (s, `string f, offset, Noaction) :: m_l
3282 method getitemcount = Array.length m_a
3284 method getitem n =
3285 let tostr = function
3286 | `int f -> string_of_int (f ())
3287 | `intws f -> string_with_suffix_of_int (f ())
3288 | `string f -> f ()
3289 | `color f -> color_to_string (f ())
3290 | `bool (btos, f) -> btos (f ())
3291 | `empty -> E.s
3293 let name, t, offset, _ = m_a.(n) in
3294 ((let s = tostr t in
3295 if nonemptystr s
3296 then Printf.sprintf "%s\t%s" name s
3297 else name),
3298 offset)
3300 method exit ~uioh ~cancel ~active ~first ~pan =
3301 let uiohopt =
3302 if not cancel
3303 then (
3304 let uioh =
3305 match m_a.(active) with
3306 | _, _, _, Action f -> f uioh
3307 | _, _, _, Noaction -> uioh
3309 Some uioh
3311 else None
3313 m_active <- active;
3314 m_first <- first;
3315 m_pan <- pan;
3316 uiohopt
3318 method hasaction n =
3319 match m_a.(n) with
3320 | _, _, _, Action _ -> true
3321 | _, _, _, Noaction -> false
3323 initializer m_active <- 1
3324 end)
3326 let rec fillsrc prevmode prevuioh =
3327 let sep () = src#caption E.s 0 in
3328 let colorp name get set =
3329 src#string name
3330 (fun () -> color_to_string (get ()))
3331 (fun v ->
3333 let c = color_of_string v in
3334 set c
3335 with exn ->
3336 state.text <-
3337 Printf.sprintf "bad color `%s': %s" v @@ exntos exn
3340 let rgba name get set =
3341 src#string name
3342 (fun () -> rgba_to_string (get ()))
3343 (fun v ->
3345 let c = rgba_of_string v in
3346 set c
3347 with exn ->
3348 state.text <-
3349 Printf.sprintf "bad color `%s': %s" v @@ exntos exn
3352 let oldmode = state.mode in
3353 let birdseye = isbirdseye state.mode in
3355 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3357 src#bool "presentation mode"
3358 (fun () -> conf.presentation)
3359 (fun v -> setpresentationmode v);
3361 src#bool "ignore case in searches"
3362 (fun () -> conf.icase)
3363 (fun v -> conf.icase <- v);
3365 src#bool "preload"
3366 (fun () -> conf.preload)
3367 (fun v -> conf.preload <- v);
3369 src#bool "highlight links"
3370 (fun () -> conf.hlinks)
3371 (fun v -> conf.hlinks <- v);
3373 src#bool "under info"
3374 (fun () -> conf.underinfo)
3375 (fun v -> conf.underinfo <- v);
3377 src#bool "persistent bookmarks"
3378 (fun () -> conf.savebmarks)
3379 (fun v -> conf.savebmarks <- v);
3381 src#fitmodel "fit model"
3382 (fun () -> FMTE.to_string conf.fitmodel)
3383 (fun v -> reqlayout conf.angle (FMTE.of_int v));
3385 src#bool "trim margins"
3386 (fun () -> conf.trimmargins)
3387 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3389 src#bool "persistent location"
3390 (fun () -> conf.jumpback)
3391 (fun v -> conf.jumpback <- v);
3393 sep ();
3394 src#int "inter-page space"
3395 (fun () -> conf.interpagespace)
3396 (fun n ->
3397 conf.interpagespace <- n;
3398 docolumns conf.columns;
3399 let pageno, py =
3400 match state.layout with
3401 | [] -> 0, 0
3402 | l :: _ ->
3403 l.pageno, l.pagey
3405 state.maxy <- calcheight ();
3406 let y = getpagey pageno in
3407 gotoxy state.x (y + py)
3410 src#int "page bias"
3411 (fun () -> conf.pagebias)
3412 (fun v -> conf.pagebias <- v);
3414 src#int "scroll step"
3415 (fun () -> conf.scrollstep)
3416 (fun n -> conf.scrollstep <- n);
3418 src#int "horizontal scroll step"
3419 (fun () -> conf.hscrollstep)
3420 (fun v -> conf.hscrollstep <- v);
3422 src#int "auto scroll step"
3423 (fun () ->
3424 match state.autoscroll with
3425 | Some step -> step
3426 | _ -> conf.autoscrollstep)
3427 (fun n ->
3428 let n = boundastep state.winh n in
3429 if state.autoscroll <> None
3430 then state.autoscroll <- Some n;
3431 conf.autoscrollstep <- n);
3433 src#int "zoom"
3434 (fun () -> truncate (conf.zoom *. 100.))
3435 (fun v -> pivotzoom ((float v) /. 100.));
3437 src#int "rotation"
3438 (fun () -> conf.angle)
3439 (fun v -> reqlayout v conf.fitmodel);
3441 src#int "scroll bar width"
3442 (fun () -> conf.scrollbw)
3443 (fun v ->
3444 conf.scrollbw <- v;
3445 reshape state.winw state.winh;
3448 src#int "scroll handle height"
3449 (fun () -> conf.scrollh)
3450 (fun v -> conf.scrollh <- v;);
3452 src#int "thumbnail width"
3453 (fun () -> conf.thumbw)
3454 (fun v ->
3455 conf.thumbw <- min 4096 v;
3456 match oldmode with
3457 | Birdseye beye ->
3458 leavebirdseye beye false;
3459 enterbirdseye ()
3460 | Textentry _
3461 | View
3462 | LinkNav _ -> ()
3465 let mode = state.mode in
3466 src#string "columns"
3467 (fun () ->
3468 match conf.columns with
3469 | Csingle _ -> "1"
3470 | Cmulti (multi, _) -> multicolumns_to_string multi
3471 | Csplit (count, _) -> "-" ^ string_of_int count
3473 (fun v ->
3474 let n, a, b = multicolumns_of_string v in
3475 setcolumns mode n a b);
3477 sep ();
3478 src#caption "Pixmap cache" 0;
3479 src#int_with_suffix "size (advisory)"
3480 (fun () -> conf.memlimit)
3481 (fun v -> conf.memlimit <- v);
3483 src#caption2 "used"
3484 (fun () ->
3485 Printf.sprintf "%s bytes, %d tiles"
3486 (string_with_suffix_of_int state.memused)
3487 (Hashtbl.length state.tilemap)) 1;
3489 sep ();
3490 src#caption "Layout" 0;
3491 src#caption2 "Dimension"
3492 (fun () ->
3493 Printf.sprintf "%dx%d (virtual %dx%d)"
3494 state.winw state.winh
3495 state.w state.maxy)
3497 if conf.debug
3498 then
3499 src#caption2 "Position" (fun () ->
3500 Printf.sprintf "%dx%d" state.x state.y
3502 else
3503 src#caption2 "Position" (fun () -> describe_layout state.layout) 1;
3505 sep ();
3506 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3507 "Save these parameters as global defaults at exit"
3508 (fun () -> conf.bedefault)
3509 (fun v -> conf.bedefault <- v);
3511 sep ();
3512 let btos b = if b then Unisyms.lguillemet else Unisyms.rguillemet in
3513 src#bool ~offset:0 ~btos "Extended parameters"
3514 (fun () -> !showextended)
3515 (fun v -> showextended := v; fillsrc prevmode prevuioh);
3516 if !showextended
3517 then (
3518 src#bool "checkers"
3519 (fun () -> conf.checkers)
3520 (fun v -> conf.checkers <- v; setcheckers v);
3521 src#bool "update cursor"
3522 (fun () -> conf.updatecurs)
3523 (fun v -> conf.updatecurs <- v);
3524 src#bool "scroll-bar on the left"
3525 (fun () -> conf.leftscroll)
3526 (fun v -> conf.leftscroll <- v);
3527 src#bool "verbose"
3528 (fun () -> conf.verbose)
3529 (fun v -> conf.verbose <- v);
3530 src#bool "invert colors"
3531 (fun () -> conf.invert)
3532 (fun v -> conf.invert <- v);
3533 src#bool "max fit"
3534 (fun () -> conf.maxhfit)
3535 (fun v -> conf.maxhfit <- v);
3536 src#bool "pax mode"
3537 (fun () -> conf.pax != None)
3538 (fun v ->
3539 if v
3540 then conf.pax <- Some (now ())
3541 else conf.pax <- None);
3542 src#string "uri launcher"
3543 (fun () -> conf.urilauncher)
3544 (fun v -> conf.urilauncher <- v);
3545 src#string "path launcher"
3546 (fun () -> conf.pathlauncher)
3547 (fun v -> conf.pathlauncher <- v);
3548 src#string "tile size"
3549 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3550 (fun v ->
3552 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
3553 conf.tilew <- max 64 w;
3554 conf.tileh <- max 64 h;
3555 flushtiles ();
3556 with exn ->
3557 state.text <- Printf.sprintf "bad tile size `%s': %s"
3558 v @@ exntos exn
3560 src#int "texture count"
3561 (fun () -> conf.texcount)
3562 (fun v ->
3563 if realloctexts v
3564 then conf.texcount <- v
3565 else impmsg "failed to set texture count please retry later"
3567 src#int "slice height"
3568 (fun () -> conf.sliceheight)
3569 (fun v ->
3570 conf.sliceheight <- v;
3571 wcmd "sliceh %d" conf.sliceheight;
3573 src#int "anti-aliasing level"
3574 (fun () -> conf.aalevel)
3575 (fun v ->
3576 conf.aalevel <- bound v 0 8;
3577 state.anchor <- getanchor ();
3578 opendoc state.path state.password;
3580 src#string "page scroll scaling factor"
3581 (fun () -> string_of_float conf.pgscale)
3582 (fun v ->
3584 let s = float_of_string v in
3585 conf.pgscale <- s
3586 with exn ->
3587 state.text <- Printf.sprintf
3588 "bad page scroll scaling factor `%s': %s" v
3589 @@ exntos exn
3592 src#int "ui font size"
3593 (fun () -> fstate.fontsize)
3594 (fun v -> setfontsize (bound v 5 100));
3595 src#int "hint font size"
3596 (fun () -> conf.hfsize)
3597 (fun v -> conf.hfsize <- bound v 5 100);
3598 src#bool "crop hack"
3599 (fun () -> conf.crophack)
3600 (fun v -> conf.crophack <- v);
3601 src#string "trim fuzz"
3602 (fun () -> irect_to_string conf.trimfuzz)
3603 (fun v ->
3605 conf.trimfuzz <- irect_of_string v;
3606 if conf.trimmargins
3607 then settrim true conf.trimfuzz;
3608 with exn ->
3609 state.text <- Printf.sprintf "bad irect `%s': %s" v
3610 @@ exntos exn
3612 src#string "selection command"
3613 (fun () -> conf.selcmd)
3614 (fun v -> conf.selcmd <- v);
3615 src#string "synctex command"
3616 (fun () -> conf.stcmd)
3617 (fun v -> conf.stcmd <- v);
3618 src#string "pax command"
3619 (fun () -> conf.paxcmd)
3620 (fun v -> conf.paxcmd <- v);
3621 src#string "ask password command"
3622 (fun () -> conf.passcmd)
3623 (fun v -> conf.passcmd <- v);
3624 src#string "save path command"
3625 (fun () -> conf.savecmd)
3626 (fun v -> conf.savecmd <- v);
3627 src#colorspace "color space"
3628 (fun () -> CSTE.to_string conf.colorspace)
3629 (fun v ->
3630 conf.colorspace <- CSTE.of_int v;
3631 wcmd "cs %d" v;
3632 load state.layout;
3634 src#paxmark "pax mark method"
3635 (fun () -> MTE.to_string conf.paxmark)
3636 (fun v -> conf.paxmark <- MTE.of_int v);
3637 if bousable () && !opengl_has_pbo
3638 then
3639 src#bool "use PBO"
3640 (fun () -> conf.usepbo)
3641 (fun v -> conf.usepbo <- v);
3642 src#bool "mouse wheel scrolls pages"
3643 (fun () -> conf.wheelbypage)
3644 (fun v -> conf.wheelbypage <- v);
3645 src#bool "open remote links in a new instance"
3646 (fun () -> conf.riani)
3647 (fun v -> conf.riani <- v);
3648 src#bool "edit annotations inline"
3649 (fun () -> conf.annotinline)
3650 (fun v -> conf.annotinline <- v);
3651 src#bool "coarse positioning in presentation mode"
3652 (fun () -> conf.coarseprespos)
3653 (fun v -> conf.coarseprespos <- v);
3654 src#bool "use document CSS"
3655 (fun () -> conf.usedoccss)
3656 (fun v ->
3657 conf.usedoccss <- v;
3658 state.anchor <- getanchor ();
3659 opendoc state.path state.password;
3661 src#bool ~btos "colors"
3662 (fun () -> !showcolors)
3663 (fun v -> showcolors := v; fillsrc prevmode prevuioh);
3664 if !showcolors
3665 then (
3666 colorp " background"
3667 (fun () -> conf.bgcolor)
3668 (fun v -> conf.bgcolor <- v);
3669 rgba " scrollbar"
3670 (fun () -> conf.sbarcolor)
3671 (fun v -> conf.sbarcolor <- v);
3672 rgba " scrollbar handle"
3673 (fun () -> conf.sbarhndlcolor)
3674 (fun v -> conf.sbarhndlcolor <- v);
3678 sep ();
3679 src#caption "Document" 0;
3680 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
3681 src#caption2 "Pages"
3682 (fun () -> string_of_int state.pagecount) 1;
3683 src#caption2 "Dimensions"
3684 (fun () -> string_of_int (List.length state.pdims)) 1;
3685 if nonemptystr conf.css
3686 then src#caption2 "CSS" (fun () -> conf.css) 1;
3687 if conf.trimmargins
3688 then (
3689 sep ();
3690 src#caption "Trimmed margins" 0;
3691 src#caption2 "Dimensions"
3692 (fun () -> string_of_int (List.length state.pdims)) 1;
3695 sep ();
3696 src#caption "OpenGL" 0;
3697 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
3698 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
3700 sep ();
3701 src#caption "Location" 0;
3702 if nonemptystr state.origin
3703 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
3704 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
3706 src#reset prevmode prevuioh;
3708 fun () ->
3709 state.text <- E.s;
3710 resetmstate ();
3711 let prevmode = state.mode
3712 and prevuioh = state.uioh in
3713 fillsrc prevmode prevuioh;
3714 let source = (src :> lvsource) in
3715 let modehash = findkeyhash conf "info" in
3716 state.uioh <-
3717 coe (object (self)
3718 inherit listview ~zebra:false ~helpmode:false
3719 ~source ~trusted:true ~modehash as super
3720 val mutable m_prevmemused = 0
3721 method! infochanged = function
3722 | Memused ->
3723 if m_prevmemused != state.memused
3724 then (
3725 m_prevmemused <- state.memused;
3726 G.postRedisplay "memusedchanged";
3728 | Pdim -> G.postRedisplay "pdimchanged"
3729 | Docinfo -> fillsrc prevmode prevuioh
3731 method! key key mask =
3732 if not (Wsi.withctrl mask)
3733 then
3734 match [@warning "-4"] Wsi.kc2kt key with
3735 | Keys.Left -> coe (self#updownlevel ~-1)
3736 | Keys.Right -> coe (self#updownlevel 1)
3737 | _ -> super#key key mask
3738 else super#key key mask
3739 end);
3740 G.postRedisplay "info";
3743 let enterhelpmode =
3744 let source =
3745 (object
3746 inherit lvsourcebase
3747 method getitemcount = Array.length state.help
3748 method getitem n =
3749 let s, l, _ = state.help.(n) in
3750 (s, l)
3752 method exit ~uioh ~cancel ~active ~first ~pan =
3753 let optuioh =
3754 if not cancel
3755 then (
3756 match state.help.(active) with
3757 | _, _, Action f -> Some (f uioh)
3758 | _, _, Noaction -> Some uioh
3760 else None
3762 m_active <- active;
3763 m_first <- first;
3764 m_pan <- pan;
3765 optuioh
3767 method hasaction n =
3768 match state.help.(n) with
3769 | _, _, Action _ -> true
3770 | _, _, Noaction -> false
3772 initializer
3773 m_active <- -1
3774 end)
3775 in fun () ->
3776 let modehash = findkeyhash conf "help" in
3777 resetmstate ();
3778 state.uioh <- coe (new listview
3779 ~zebra:false ~helpmode:true
3780 ~source ~trusted:true ~modehash);
3781 G.postRedisplay "help";
3784 let entermsgsmode =
3785 let msgsource =
3786 (object
3787 inherit lvsourcebase
3788 val mutable m_items = E.a
3790 method getitemcount = 1 + Array.length m_items
3792 method getitem n =
3793 if n = 0
3794 then "[Clear]", 0
3795 else m_items.(n-1), 0
3797 method exit ~uioh ~cancel ~active ~first ~pan =
3798 ignore uioh;
3799 if not cancel
3800 then (
3801 if active = 0
3802 then Buffer.clear state.errmsgs;
3804 m_active <- active;
3805 m_first <- first;
3806 m_pan <- pan;
3807 None
3809 method hasaction n =
3810 n = 0
3812 method reset =
3813 state.newerrmsgs <- false;
3814 let l = Str.split newlinere (Buffer.contents state.errmsgs) in
3815 m_items <- Array.of_list l
3817 initializer
3818 m_active <- 0
3819 end)
3820 in fun () ->
3821 state.text <- E.s;
3822 resetmstate ();
3823 msgsource#reset;
3824 let source = (msgsource :> lvsource) in
3825 let modehash = findkeyhash conf "listview" in
3826 state.uioh <-
3827 coe (object
3828 inherit listview ~zebra:false ~helpmode:false
3829 ~source ~trusted:false ~modehash as super
3830 method! display =
3831 if state.newerrmsgs
3832 then msgsource#reset;
3833 super#display
3834 end);
3835 G.postRedisplay "msgs";
3838 let getusertext s =
3839 let editor = getenvwithdef "EDITOR" E.s in
3840 if emptystr editor
3841 then E.s
3842 else
3843 let tmppath = Filename.temp_file "llpp" "note" in
3844 if nonemptystr s
3845 then (
3846 let oc = open_out tmppath in
3847 output_string oc s;
3848 close_out oc;
3850 let execstr = editor ^ " " ^ tmppath in
3851 let s =
3852 match spawn execstr [] with
3853 | exception exn ->
3854 impmsg "spawn(%S) failed: %s" execstr @@ exntos exn;
3856 | pid ->
3857 match Unix.waitpid [] pid with
3858 | exception exn ->
3859 impmsg "waitpid(%d) failed: %s" pid @@ exntos exn;
3861 | (_pid, status) ->
3862 match status with
3863 | Unix.WEXITED 0 -> filecontents tmppath
3864 | Unix.WEXITED n ->
3865 impmsg "editor process(%s) exited abnormally: %d" execstr n;
3867 | Unix.WSIGNALED n ->
3868 impmsg "editor process(%s) was killed by signal %d" execstr n;
3870 | Unix.WSTOPPED n ->
3871 impmsg "editor(%s) process was stopped by signal %d" execstr n;
3874 match Unix.unlink tmppath with
3875 | exception exn ->
3876 impmsg "failed to ulink %S: %s" tmppath @@ exntos exn;
3878 | () -> s
3881 let enterannotmode opaque slinkindex =
3882 let msgsource =
3883 (object
3884 inherit lvsourcebase
3885 val mutable m_text = E.s
3886 val mutable m_items = E.a
3888 method getitemcount = Array.length m_items
3890 method getitem n =
3891 let label, _func = m_items.(n) in
3892 label, 0
3894 method exit ~uioh ~cancel ~active ~first ~pan =
3895 ignore (uioh, first, pan);
3896 if not cancel
3897 then (
3898 let _label, func = m_items.(active) in
3899 func ()
3901 None
3903 method hasaction n = nonemptystr @@ fst m_items.(n)
3905 method reset s =
3906 let rec split accu b i =
3907 let p = b+i in
3908 if p = String.length s
3909 then (String.sub s b (p-b), unit) :: accu
3910 else
3911 if (i > 70 && s.[p] = ' ') || s.[p] = '\r' || s.[p] = '\n'
3912 then
3913 let ss = if i = 0 then E.s else String.sub s b i in
3914 split ((ss, unit)::accu) (p+1) 0
3915 else
3916 split accu b (i+1)
3918 let cleanup () =
3919 wcmd "freepage %s" (~> opaque);
3920 let keys =
3921 Hashtbl.fold (fun key opaque' accu ->
3922 if opaque' = opaque'
3923 then key :: accu else accu) state.pagemap []
3925 List.iter (Hashtbl.remove state.pagemap) keys;
3926 flushtiles ();
3927 gotoxy state.x state.y
3929 let dele () =
3930 delannot opaque slinkindex;
3931 cleanup ();
3933 let edit inline () =
3934 let update s =
3935 if emptystr s
3936 then dele ()
3937 else (
3938 modannot opaque slinkindex s;
3939 cleanup ();
3942 if inline
3943 then
3944 let mode = state.mode in
3945 state.mode <-
3946 Textentry (
3947 ("annotation: ", m_text, None, textentry, update, true),
3948 fun _ -> state.mode <- mode);
3949 state.text <- E.s;
3950 enttext ();
3951 else
3952 let s = getusertext m_text in
3953 update s
3955 m_text <- s;
3956 m_items <-
3957 ( "[Copy]", fun () -> selstring conf.selcmd m_text)
3958 :: ("[Delete]", dele)
3959 :: ("[Edit]", edit conf.annotinline)
3960 :: (E.s, unit)
3961 :: split [] 0 0 |> List.rev |> Array.of_list
3963 initializer
3964 m_active <- 0
3965 end)
3967 state.text <- E.s;
3968 let s = getannotcontents opaque slinkindex in
3969 resetmstate ();
3970 msgsource#reset s;
3971 let source = (msgsource :> lvsource) in
3972 let modehash = findkeyhash conf "listview" in
3973 state.uioh <- coe (object
3974 inherit listview ~zebra:false ~helpmode:false
3975 ~source ~trusted:false ~modehash
3976 end);
3977 G.postRedisplay "enterannotmode";
3980 let gotoremote spec =
3981 let filename, dest = splitatchar spec '#' in
3982 let getpath filename =
3983 let path =
3984 if nonemptystr filename
3985 then
3986 if Filename.is_relative filename
3987 then
3988 let dir = Filename.dirname state.path in
3989 let dir =
3990 if Filename.is_implicit dir
3991 then Filename.concat (Sys.getcwd ()) dir
3992 else dir
3994 Filename.concat dir filename
3995 else filename
3996 else E.s
3998 if Sys.file_exists path
3999 then path
4000 else E.s
4002 let path = getpath filename in
4003 let dospawn lcmd =
4004 if conf.riani
4005 then
4006 let cmd = Lazy.force_val lcmd in
4007 match spawn cmd with
4008 | _pid -> ()
4009 | exception exn -> dolog "failed to execute `%s': %s" cmd @@ exntos exn
4010 else
4011 let anchor = getanchor () in
4012 let ranchor = state.path, state.password, anchor, state.origin in
4013 state.origin <- E.s;
4014 state.ranchors <- ranchor :: state.ranchors;
4015 opendoc path E.s;
4017 if substratis spec 0 "page="
4018 then
4019 match Scanf.sscanf spec "page=%d" (fun n -> n) with
4020 | pageno ->
4021 state.anchor <- (pageno, 0.0, 0.0);
4022 dospawn @@ lazy (Printf.sprintf "%s -page %d %S" !selfexec pageno path);
4023 | exception exn ->
4024 adderrfmt "error parsing remote destination" "page: %s" @@ exntos exn
4025 else (
4026 state.nameddest <- dest;
4027 dospawn @@ lazy (!selfexec ^ " " ^ path ^ " -dest " ^ dest)
4031 let gotounder = function
4032 | Ulinkuri s when isexternallink s ->
4033 if substratis s 0 "file://"
4034 then gotoremote @@ String.sub s 7 (String.length s - 7)
4035 else gotouri s
4036 | Ulinkuri s ->
4037 let pageno, x, y = uritolocation s in
4038 addnav ();
4039 gotopagexy pageno x y
4040 | Utext _ | Unone -> ()
4041 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
4044 let gotooutline (_, _, kind) =
4045 match kind with
4046 | Onone -> ()
4047 | Oanchor anchor ->
4048 let (pageno, y, _) = anchor in
4049 let y = getanchory
4050 (if conf.presentation then (pageno, y, 1.0) else anchor)
4052 addnav ();
4053 gotoxy state.x y
4054 | Ouri uri -> gotounder (Ulinkuri uri)
4055 | Olaunch _cmd -> failwith "gotounder (Ulaunch cmd)"
4056 | Oremote _remote -> failwith "gotounder (Uremote remote)"
4057 | Ohistory hist -> gotohist hist
4058 | Oremotedest _remotedest -> failwith "gotounder (Uremotedest remotedest)"
4061 class outlinesoucebase fetchoutlines = object (self)
4062 inherit lvsourcebase
4063 val mutable m_items = E.a
4064 val mutable m_minfo = E.a
4065 val mutable m_orig_items = E.a
4066 val mutable m_orig_minfo = E.a
4067 val mutable m_narrow_patterns = []
4068 val mutable m_gen = -1
4070 method getitemcount = Array.length m_items
4072 method getitem n =
4073 let s, n, _ = m_items.(n) in
4074 (s, n+0)
4076 method exit ~(uioh:uioh) ~cancel ~active ~(first:int) ~pan : uioh option =
4077 ignore (uioh, first);
4078 let items, minfo =
4079 if m_narrow_patterns = []
4080 then m_orig_items, m_orig_minfo
4081 else m_items, m_minfo
4083 m_pan <- pan;
4084 if not cancel
4085 then (
4086 m_items <- items;
4087 m_minfo <- minfo;
4088 gotooutline m_items.(active);
4090 else (
4091 m_items <- items;
4092 m_minfo <- minfo;
4094 None
4096 method hasaction (_:int) = true
4098 method greetmsg =
4099 if Array.length m_items != Array.length m_orig_items
4100 then
4101 let s =
4102 match m_narrow_patterns with
4103 | one :: [] -> one
4104 | many -> String.concat Unisyms.ellipsis (List.rev many)
4106 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
4107 else E.s
4109 method statestr =
4110 match m_narrow_patterns with
4111 | [] -> E.s
4112 | one :: [] -> one
4113 | head :: _ -> Unisyms.ellipsis ^ head
4115 method narrow pattern =
4116 match Str.regexp_case_fold pattern with
4117 | exception _ -> ()
4118 | re ->
4119 let rec loop accu minfo n =
4120 if n = -1
4121 then (
4122 m_items <- Array.of_list accu;
4123 m_minfo <- Array.of_list minfo;
4125 else
4126 let (s, _, _) as o = m_items.(n) in
4127 let accu, minfo =
4128 match Str.search_forward re s 0 with
4129 | exception Not_found -> accu, minfo
4130 | first -> o :: accu, (first, Str.match_end ()) :: minfo
4132 loop accu minfo (n-1)
4134 loop [] [] (Array.length m_items - 1)
4136 method! getminfo = m_minfo
4138 method denarrow =
4139 m_orig_items <- fetchoutlines ();
4140 m_minfo <- m_orig_minfo;
4141 m_items <- m_orig_items
4143 method add_narrow_pattern pattern =
4144 m_narrow_patterns <- pattern :: m_narrow_patterns
4146 method del_narrow_pattern =
4147 match m_narrow_patterns with
4148 | _ :: rest -> m_narrow_patterns <- rest
4149 | [] -> ()
4151 method renarrow =
4152 self#denarrow;
4153 match m_narrow_patterns with
4154 | pattern :: [] -> self#narrow pattern; pattern
4155 | list ->
4156 List.fold_left (fun accu pattern ->
4157 self#narrow pattern;
4158 pattern ^ Unisyms.ellipsis ^ accu) E.s list
4160 method calcactive (_:anchor) = 0
4162 method reset anchor items =
4163 if state.gen != m_gen
4164 then (
4165 m_orig_items <- items;
4166 m_items <- items;
4167 m_narrow_patterns <- [];
4168 m_minfo <- E.a;
4169 m_orig_minfo <- E.a;
4170 m_gen <- state.gen;
4172 else (
4173 if items != m_orig_items
4174 then (
4175 m_orig_items <- items;
4176 if m_narrow_patterns == []
4177 then m_items <- items;
4180 let active = self#calcactive anchor in
4181 m_active <- active;
4182 m_first <- firstof m_first active
4186 let outlinesource fetchoutlines =
4187 (object
4188 inherit outlinesoucebase fetchoutlines
4189 method! calcactive anchor =
4190 let rely = getanchory anchor in
4191 let rec loop n best bestd =
4192 if n = Array.length m_items
4193 then best
4194 else
4195 let _, _, kind = m_items.(n) in
4196 match kind with
4197 | Oanchor anchor ->
4198 let orely = getanchory anchor in
4199 let d = abs (orely - rely) in
4200 if d < bestd
4201 then loop (n+1) n d
4202 else loop (n+1) best bestd
4203 | Onone | Oremote _ | Olaunch _
4204 | Oremotedest _ | Ouri _ | Ohistory _ ->
4205 loop (n+1) best bestd
4207 loop 0 ~-1 max_int
4208 end)
4211 let enteroutlinemode, enterbookmarkmode, enterhistmode =
4212 let mkselector sourcetype =
4213 let fetchoutlines () =
4214 match sourcetype with
4215 | `bookmarks -> Array.of_list state.bookmarks
4216 | `outlines -> state.outlines
4217 | `history -> genhistoutlines () |> Array.of_list
4219 let source =
4220 if sourcetype = `history
4221 then new outlinesoucebase fetchoutlines
4222 else outlinesource fetchoutlines
4224 (fun errmsg ->
4225 let outlines = fetchoutlines () in
4226 if Array.length outlines = 0
4227 then showtext ' ' errmsg
4228 else (
4229 resetmstate ();
4230 Wsi.setcursor Wsi.CURSOR_INHERIT;
4231 let anchor = getanchor () in
4232 source#reset anchor outlines;
4233 state.text <- source#greetmsg;
4234 state.uioh <-
4235 coe (new outlinelistview ~zebra:(sourcetype=`history) ~source);
4236 G.postRedisplay "enter selector";
4240 let mkenter sourcetype errmsg =
4241 let enter = mkselector sourcetype in
4242 fun () -> enter errmsg
4244 ( mkenter `outlines "document has no outline"
4245 , mkenter `bookmarks "document has no bookmarks (yet)"
4246 , mkenter `history "history is empty" )
4249 let quickbookmark ?title () =
4250 match state.layout with
4251 | [] -> ()
4252 | l :: _ ->
4253 let title =
4254 match title with
4255 | None ->
4256 Unix.(
4257 let tm = localtime (now ()) in
4258 Printf.sprintf
4259 "Quick (page %d) (bookmarked on %02d/%02d/%d at %02d:%02d)"
4260 (l.pageno+1)
4261 tm.tm_mday (tm.tm_mon+1) (tm.tm_year+1900) tm.tm_hour tm.tm_min
4263 | Some title -> title
4265 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4268 let setautoscrollspeed step goingdown =
4269 let incr = max 1 ((abs step) / 2) in
4270 let incr = if goingdown then incr else -incr in
4271 let astep = boundastep state.winh (step + incr) in
4272 state.autoscroll <- Some astep;
4275 let canpan () =
4276 match conf.columns with
4277 | Csplit _ -> true
4278 | Csingle _ | Cmulti _ -> state.x != 0 || conf.zoom > 1.0
4281 let panbound x = bound x (-state.w) state.winw;;
4283 let existsinrow pageno (columns, coverA, coverB) p =
4284 let last = ((pageno - coverA) mod columns) + columns in
4285 let rec any = function
4286 | [] -> false
4287 | l :: rest ->
4288 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4289 then p l
4290 else (
4291 if not (p l)
4292 then (if l.pageno = last then false else any rest)
4293 else true
4296 any state.layout
4299 let nextpage () =
4300 match state.layout with
4301 | [] ->
4302 let pageno = page_of_y state.y in
4303 gotoxy state.x (getpagey (pageno+1))
4304 | l :: rest ->
4305 match conf.columns with
4306 | Csingle _ ->
4307 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4308 then
4309 let y = clamp (pgscale state.winh) in
4310 gotoxy state.x y
4311 else
4312 let pageno = min (l.pageno+1) (state.pagecount-1) in
4313 gotoxy state.x (getpagey pageno)
4314 | Cmulti ((c, _, _) as cl, _) ->
4315 if conf.presentation
4316 && (existsinrow l.pageno cl
4317 (fun l -> l.pageh > l.pagey + l.pagevh))
4318 then
4319 let y = clamp (pgscale state.winh) in
4320 gotoxy state.x y
4321 else
4322 let pageno = min (l.pageno+c) (state.pagecount-1) in
4323 gotoxy state.x (getpagey pageno)
4324 | Csplit (n, _) ->
4325 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4326 then
4327 let pagey, pageh = getpageyh l.pageno in
4328 let pagey = pagey + pageh * l.pagecol in
4329 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4330 gotoxy state.x (pagey + pageh + ips)
4333 let prevpage () =
4334 match state.layout with
4335 | [] ->
4336 let pageno = page_of_y state.y in
4337 gotoxy state.x (getpagey (pageno-1))
4338 | l :: _ ->
4339 match conf.columns with
4340 | Csingle _ ->
4341 if conf.presentation && l.pagey != 0
4342 then
4343 gotoxy state.x (clamp (pgscale ~-(state.winh)))
4344 else
4345 let pageno = max 0 (l.pageno-1) in
4346 gotoxy state.x (getpagey pageno)
4347 | Cmulti ((c, _, coverB) as cl, _) ->
4348 if conf.presentation &&
4349 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4350 then
4351 gotoxy state.x (clamp (pgscale ~-(state.winh)))
4352 else
4353 let decr =
4354 if l.pageno = state.pagecount - coverB
4355 then 1
4356 else c
4358 let pageno = max 0 (l.pageno-decr) in
4359 gotoxy state.x (getpagey pageno)
4360 | Csplit (n, _) ->
4361 let y =
4362 if l.pagecol = 0
4363 then
4364 if l.pageno = 0
4365 then l.pagey
4366 else
4367 let pageno = max 0 (l.pageno-1) in
4368 let pagey, pageh = getpageyh pageno in
4369 pagey + (n-1)*pageh
4370 else
4371 let pagey, pageh = getpageyh l.pageno in
4372 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4374 gotoxy state.x y
4377 let save () =
4378 if emptystr conf.savecmd
4379 then adderrmsg "savepath-command is empty"
4380 "don't know where to save modified document"
4381 else
4382 let savecmd = Str.global_replace percentsre state.path conf.savecmd in
4383 let path =
4384 getcmdoutput
4385 (fun exn ->
4386 adderrfmt savecmd "failed to produce path to the saved copy: %s" exn)
4387 savecmd
4389 if nonemptystr path
4390 then
4391 let tmp = path ^ ".tmp" in
4392 savedoc tmp;
4393 Unix.rename tmp path;
4396 let viewkeyboard key mask =
4397 let enttext te =
4398 let mode = state.mode in
4399 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4400 state.text <- E.s;
4401 enttext ();
4402 G.postRedisplay "view:enttext"
4404 let ctrl = Wsi.withctrl mask in
4405 let open Keys in
4406 match Wsi.kc2kt key with
4407 | Ascii 'S' -> state.slideshow <- state.slideshow lxor 1
4409 | Ascii 'Q' -> exit 0
4411 | Ascii 'W' ->
4412 if hasunsavedchanges ()
4413 then save ()
4415 | Insert ->
4416 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4417 then (
4418 state.mode <- (
4419 match state.lnava with
4420 | None -> LinkNav (Ltgendir 0)
4421 | Some pn -> LinkNav (Ltexact pn)
4423 gotoxy state.x state.y;
4425 else impmsg "keyboard link navigation does not work under rotation"
4427 | Escape | Ascii 'q' ->
4428 begin match state.mstate with
4429 | Mzoomrect _ ->
4430 resetmstate ();
4431 G.postRedisplay "kill rect";
4432 | Msel _
4433 | Mpan _
4434 | Mscrolly | Mscrollx
4435 | Mzoom _
4436 | Mnone ->
4437 begin match state.mode with
4438 | LinkNav ln ->
4439 begin match ln with
4440 | Ltexact pl -> state.lnava <- Some pl
4441 | Ltgendir _ | Ltnotready _ -> state.lnava <- None
4442 end;
4443 state.mode <- View;
4444 G.postRedisplay "esc leave linknav"
4445 | Birdseye _ | Textentry _ | View ->
4446 match state.ranchors with
4447 | [] -> raise Quit
4448 | (path, password, anchor, origin) :: rest ->
4449 state.ranchors <- rest;
4450 state.anchor <- anchor;
4451 state.origin <- origin;
4452 state.nameddest <- E.s;
4453 opendoc path password
4454 end;
4455 end;
4457 | Backspace ->
4458 addnavnorc ();
4459 gotoxy state.x (getnav ~-1)
4461 | Ascii 'o' ->
4462 enteroutlinemode ()
4464 | Ascii 'H' ->
4465 enterhistmode ()
4467 | Ascii 'u' ->
4468 state.rects <- [];
4469 state.text <- E.s;
4470 Hashtbl.iter (fun _ opaque ->
4471 clearmark opaque;
4472 Hashtbl.clear state.prects) state.pagemap;
4473 G.postRedisplay "dehighlight";
4475 | Ascii (('/' | '?') as c) ->
4476 let ondone isforw s =
4477 cbput state.hists.pat s;
4478 state.searchpattern <- s;
4479 search s isforw
4481 let s = String.make 1 c in
4482 enttext (s, E.s, Some (onhist state.hists.pat),
4483 textentry, ondone (c = '/'), true)
4485 | Ascii '+' | Ascii '=' when ctrl ->
4486 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4487 pivotzoom (conf.zoom +. incr)
4489 | Ascii '+' ->
4490 let ondone s =
4491 let n =
4492 try int_of_string s with exn ->
4493 state.text <-
4494 Printf.sprintf "bad integer `%s': %s" s @@ exntos exn;
4495 max_int
4497 if n != max_int
4498 then (
4499 conf.pagebias <- n;
4500 state.text <- "page bias is now " ^ string_of_int n;
4503 enttext ("page bias: ", E.s, None, intentry, ondone, true)
4505 | Ascii '-' when ctrl ->
4506 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4507 pivotzoom (max 0.01 (conf.zoom -. decr))
4509 | Ascii '-' ->
4510 let ondone msg = state.text <- msg in
4511 enttext (
4512 "option [acfhilpstvxACFPRSZTISM]: ", E.s, None,
4513 optentry state.mode, ondone, true
4516 | Ascii '0' when ctrl ->
4517 if conf.zoom = 1.0
4518 then gotoxy 0 state.y
4519 else setzoom 1.0
4521 | Ascii ('1'|'2' as c) when ctrl && conf.fitmodel != FitPage ->
4522 let cols =
4523 match conf.columns with
4524 | Csingle _ | Cmulti _ -> 1
4525 | Csplit (n, _) -> n
4527 let h = state.winh -
4528 conf.interpagespace lsl (if conf.presentation then 1 else 0)
4530 let zoom = zoomforh state.winw h 0 cols in
4531 if zoom > 0.0 && (c = '2' || zoom < 1.0)
4532 then setzoom zoom
4534 | Ascii '3' when ctrl ->
4535 let fm =
4536 match conf.fitmodel with
4537 | FitWidth -> FitProportional
4538 | FitProportional -> FitPage
4539 | FitPage -> FitWidth
4541 state.text <- "fit model: " ^ FMTE.to_string fm;
4542 reqlayout conf.angle fm
4544 | Ascii '4' when ctrl ->
4545 let zoom = getmaxw () /. float state.winw in
4546 if zoom > 0.0 then setzoom zoom
4548 | Fn 9 ->
4549 togglebirdseye ()
4551 | Ascii '9' when ctrl ->
4552 togglebirdseye ()
4554 | Ascii ('0'..'9' as c) when not ctrl ->
4555 let ondone s =
4556 let n =
4557 try int_of_string s with exn ->
4558 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn;
4561 if n >= 0
4562 then (
4563 addnav ();
4564 cbput state.hists.pag (string_of_int n);
4565 gotopage1 (n + conf.pagebias - 1) 0;
4568 let [@warning "-4"] pageentry text = function
4569 | Keys.Ascii 'g' -> TEdone text
4570 | key -> intentry text key
4572 let text = String.make 1 c in
4573 enttext (":", text, Some (onhist state.hists.pag),
4574 pageentry, ondone, true)
4576 | Ascii 'b' ->
4577 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
4578 G.postRedisplay "toggle scrollbar";
4580 | Ascii 'B' ->
4581 state.bzoom <- not state.bzoom;
4582 state.rects <- [];
4583 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
4585 | Ascii 'l' ->
4586 conf.hlinks <- not conf.hlinks;
4587 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4588 G.postRedisplay "toggle highlightlinks";
4590 | Ascii 'F' ->
4591 if conf.angle mod 360 = 0
4592 then (
4593 state.glinks <- true;
4594 let mode = state.mode in
4595 state.mode <-
4596 Textentry (
4597 (":", E.s, None, linknentry, linknact gotounder, false),
4598 (fun _ ->
4599 state.glinks <- false;
4600 state.mode <- mode)
4602 state.text <- E.s;
4603 G.postRedisplay "view:linkent(F)"
4605 else impmsg "hint mode does not work under rotation"
4607 | Ascii 'y' ->
4608 state.glinks <- true;
4609 let mode = state.mode in
4610 state.mode <-
4611 Textentry (
4612 (":", E.s, None, linknentry,
4613 linknact (fun under ->
4614 selstring conf.selcmd (undertext under)), false),
4615 (fun _ ->
4616 state.glinks <- false;
4617 state.mode <- mode)
4619 state.text <- E.s;
4620 G.postRedisplay "view:linkent"
4622 | Ascii 'a' ->
4623 begin match state.autoscroll with
4624 | Some step ->
4625 conf.autoscrollstep <- step;
4626 state.autoscroll <- None
4627 | None ->
4628 state.autoscroll <- Some conf.autoscrollstep;
4629 state.slideshow <- state.slideshow land lnot 2
4632 | Ascii 'p' when ctrl ->
4633 launchpath () (* XXX where do error messages go? *)
4635 | Ascii 'P' ->
4636 setpresentationmode (not conf.presentation);
4637 showtext ' ' ("presentation mode " ^
4638 if conf.presentation then "on" else "off");
4640 | Ascii 'f' ->
4641 if List.mem Wsi.Fullscreen state.winstate
4642 then Wsi.reshape conf.cwinw conf.cwinh
4643 else Wsi.fullscreen ()
4645 | Ascii ('p'|'N') ->
4646 search state.searchpattern false
4648 | Ascii 'n' | Fn 3 ->
4649 search state.searchpattern true
4651 | Ascii 't' ->
4652 begin match state.layout with
4653 | [] -> ()
4654 | l :: _ ->
4655 gotoxy state.x (getpagey l.pageno)
4658 | Ascii ' ' ->
4659 nextpage ()
4661 | Delete ->
4662 prevpage ()
4664 | Ascii '=' ->
4665 showtext ' ' (describe_layout state.layout);
4667 | Ascii 'w' ->
4668 begin match state.layout with
4669 | [] -> ()
4670 | l :: _ ->
4671 Wsi.reshape l.pagew l.pageh;
4672 G.postRedisplay "w"
4675 | Ascii '\'' ->
4676 enterbookmarkmode ()
4678 | Ascii 'h' | Fn 1 ->
4679 enterhelpmode ()
4681 | Ascii 'i' ->
4682 enterinfomode ()
4684 | Ascii 'e' when Buffer.length state.errmsgs > 0 ->
4685 entermsgsmode ()
4687 | Ascii 'm' ->
4688 let ondone s =
4689 match state.layout with
4690 | l :: _ ->
4691 if nonemptystr s
4692 then
4693 state.bookmarks <-
4694 (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4695 | _ -> ()
4697 enttext ("bookmark: ", E.s, None, textentry, ondone, true)
4699 | Ascii '~' ->
4700 quickbookmark ();
4701 showtext ' ' "Quick bookmark added";
4703 | Ascii 'z' ->
4704 begin match state.layout with
4705 | l :: _ ->
4706 let rect = getpdimrect l.pagedimno in
4707 let w, h =
4708 if conf.crophack
4709 then
4710 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4711 truncate (1.2 *. (rect.(3) -. rect.(0))))
4712 else
4713 (truncate (rect.(1) -. rect.(0)),
4714 truncate (rect.(3) -. rect.(0)))
4716 let w = truncate ((float w)*.conf.zoom)
4717 and h = truncate ((float h)*.conf.zoom) in
4718 if w != 0 && h != 0
4719 then (
4720 state.anchor <- getanchor ();
4721 Wsi.reshape w (h + conf.interpagespace)
4723 G.postRedisplay "z";
4725 | [] -> ()
4728 | Ascii 'x' -> state.roam ()
4730 | Ascii ('<'|'>' as c) ->
4731 reqlayout
4732 (conf.angle + (if c = '>' then 30 else -30)) conf.fitmodel
4734 | Ascii ('['|']' as c) ->
4735 conf.colorscale <-
4736 bound (conf.colorscale +. (if c = ']' then 0.1 else -0.1)) 0.0 1.0;
4737 G.postRedisplay "brightness";
4739 | Ascii 'c' when state.mode = View ->
4740 if Wsi.withalt mask
4741 then (
4742 if conf.zoom > 1.0
4743 then
4744 let m = (state.winw - state.w) / 2 in
4745 gotoxy m state.y
4747 else
4748 let (c, a, b), z =
4749 match state.prevcolumns with
4750 | None -> (1, 0, 0), 1.0
4751 | Some (columns, z) ->
4752 let cab =
4753 match columns with
4754 | Csplit (c, _) -> -c, 0, 0
4755 | Cmulti ((c, a, b), _) -> c, a, b
4756 | Csingle _ -> 1, 0, 0
4758 cab, z
4760 setcolumns View c a b;
4761 setzoom z
4763 | Down | Up when ctrl && Wsi.withshift mask ->
4764 let zoom, x = state.prevzoom in
4765 setzoom zoom;
4766 state.x <- x;
4768 | Ascii 'k' | Up ->
4769 begin match state.autoscroll with
4770 | None ->
4771 begin match state.mode with
4772 | Birdseye beye -> upbirdseye 1 beye
4773 | Textentry _ | View | LinkNav _ ->
4774 if ctrl
4775 then gotoxy state.x (clamp ~-(state.winh/2))
4776 else (
4777 if not (Wsi.withshift mask) && conf.presentation
4778 then prevpage ()
4779 else gotoxy state.x (clamp (-conf.scrollstep))
4782 | Some n ->
4783 setautoscrollspeed n false
4786 | Ascii 'j' | Down ->
4787 begin match state.autoscroll with
4788 | None ->
4789 begin match state.mode with
4790 | Birdseye beye -> downbirdseye 1 beye
4791 | Textentry _ | View | LinkNav _ ->
4792 if ctrl
4793 then gotoxy state.x (clamp (state.winh/2))
4794 else (
4795 if not (Wsi.withshift mask) && conf.presentation
4796 then nextpage ()
4797 else gotoxy state.x (clamp (conf.scrollstep))
4800 | Some n ->
4801 setautoscrollspeed n true
4804 | Left | Right when not (Wsi.withalt mask) ->
4805 if canpan ()
4806 then
4807 let dx =
4808 if ctrl
4809 then state.winw / 2
4810 else conf.hscrollstep
4812 let dx =
4813 let pv = Wsi.kc2kt key in
4814 if pv = Keys.Left then dx else -dx
4816 gotoxy (panbound (state.x + dx)) state.y
4817 else (
4818 state.text <- E.s;
4819 G.postRedisplay "left/right"
4822 | Prior ->
4823 let y =
4824 if ctrl
4825 then
4826 match state.layout with
4827 | [] -> state.y
4828 | l :: _ -> state.y - l.pagey
4829 else
4830 clamp (pgscale (-state.winh))
4832 gotoxy state.x y
4834 | Next ->
4835 let y =
4836 if ctrl
4837 then
4838 match List.rev state.layout with
4839 | [] -> state.y
4840 | l :: _ -> getpagey l.pageno
4841 else
4842 clamp (pgscale state.winh)
4844 gotoxy state.x y
4846 | Ascii 'g' | Home ->
4847 addnav ();
4848 gotoxy 0 0
4849 | Ascii 'G' | End ->
4850 addnav ();
4851 gotoxy 0 (clamp state.maxy)
4853 | Right when Wsi.withalt mask ->
4854 addnavnorc ();
4855 gotoxy state.x (getnav 1)
4856 | Left when Wsi.withalt mask ->
4857 addnavnorc ();
4858 gotoxy state.x (getnav ~-1)
4860 | Ascii 'r' ->
4861 reload ()
4863 | Ascii 'v' when conf.debug ->
4864 state.rects <- [];
4865 List.iter (fun l ->
4866 match getopaque l.pageno with
4867 | None -> ()
4868 | Some opaque ->
4869 let x0, y0, x1, y1 = pagebbox opaque in
4870 let rect = (float x0, float y0,
4871 float x1, float y0,
4872 float x1, float y1,
4873 float x0, float y1) in
4874 debugrect rect;
4875 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
4876 state.rects <- (l.pageno, color, rect) :: state.rects;
4877 ) state.layout;
4878 G.postRedisplay "v";
4880 | Ascii '|' ->
4881 let mode = state.mode in
4882 let cmd = ref E.s in
4883 let onleave = function
4884 | Cancel -> state.mode <- mode
4885 | Confirm ->
4886 List.iter (fun l ->
4887 match getopaque l.pageno with
4888 | Some opaque -> pipesel opaque !cmd
4889 | None -> ()) state.layout;
4890 state.mode <- mode
4892 let ondone s =
4893 cbput state.hists.sel s;
4894 cmd := s
4896 let te =
4897 "| ", !cmd, Some (onhist state.hists.sel), textentry, ondone, true
4899 G.postRedisplay "|";
4900 state.mode <- Textentry (te, onleave);
4902 | (Ascii _|Fn _|Enter|Left|Right|Code _|Ctrl _) ->
4903 vlog "huh? %s" (Wsi.keyname key)
4906 let linknavkeyboard key mask linknav =
4907 let pv = Wsi.kc2kt key in
4908 let getpage pageno =
4909 let rec loop = function
4910 | [] -> None
4911 | l :: _ when l.pageno = pageno -> Some l
4912 | _ :: rest -> loop rest
4913 in loop state.layout
4915 let doexact (pageno, n) =
4916 match getopaque pageno, getpage pageno with
4917 | Some opaque, Some l ->
4918 if pv = Keys.Enter
4919 then
4920 let under = getlink opaque n in
4921 G.postRedisplay "link gotounder";
4922 gotounder under;
4923 state.mode <- View;
4924 else
4925 let opt, dir =
4926 let open Keys in
4927 match pv with
4928 | Home -> Some (findlink opaque LDfirst), -1
4929 | End -> Some (findlink opaque LDlast), 1
4930 | Left -> Some (findlink opaque (LDleft n)), -1
4931 | Right -> Some (findlink opaque (LDright n)), 1
4932 | Up -> Some (findlink opaque (LDup n)), -1
4933 | Down -> Some (findlink opaque (LDdown n)), 1
4935 | Delete|Escape|Insert|Enter|Next|Prior|Ascii _
4936 | Code _|Fn _|Ctrl _|Backspace -> None, 0
4938 let pwl l dir =
4939 begin match findpwl l.pageno dir with
4940 | Pwlnotfound -> ()
4941 | Pwl pageno ->
4942 let notfound dir =
4943 state.mode <- LinkNav (Ltgendir dir);
4944 let y, h = getpageyh pageno in
4945 let y =
4946 if dir < 0
4947 then y + h - state.winh
4948 else y
4950 gotoxy state.x y
4952 begin match getopaque pageno, getpage pageno with
4953 | Some opaque, Some _ ->
4954 let link =
4955 let ld = if dir > 0 then LDfirst else LDlast in
4956 findlink opaque ld
4958 begin match link with
4959 | Lfound m ->
4960 showlinktype (getlink opaque m);
4961 state.mode <- LinkNav (Ltexact (pageno, m));
4962 G.postRedisplay "linknav jpage";
4963 | Lnotfound -> notfound dir
4964 end;
4965 | _ -> notfound dir
4966 end;
4967 end;
4969 begin match opt with
4970 | Some Lnotfound -> pwl l dir;
4971 | Some (Lfound m) ->
4972 if m = n
4973 then pwl l dir
4974 else (
4975 let _, y0, _, y1 = getlinkrect opaque m in
4976 if y0 < l.pagey
4977 then gotopage1 l.pageno y0
4978 else (
4979 let d = fstate.fontsize + 1 in
4980 if y1 - l.pagey > l.pagevh - d
4981 then gotopage1 l.pageno (y1 - state.winh + d)
4982 else G.postRedisplay "linknav";
4984 showlinktype (getlink opaque m);
4985 state.mode <- LinkNav (Ltexact (l.pageno, m));
4988 | None -> viewkeyboard key mask
4989 end;
4990 | _ -> viewkeyboard key mask
4992 if pv = Keys.Insert
4993 then (
4994 begin match linknav with
4995 | Ltexact pa -> state.lnava <- Some pa
4996 | Ltgendir _ | Ltnotready _ -> ()
4997 end;
4998 state.mode <- View;
4999 G.postRedisplay "leave linknav"
5001 else
5002 match linknav with
5003 | Ltgendir _ | Ltnotready _ -> viewkeyboard key mask
5004 | Ltexact exact -> doexact exact
5007 let keyboard key mask =
5008 if (key = Char.code 'g' && Wsi.withctrl mask) && not (istextentry state.mode)
5009 then wcmd "interrupt"
5010 else state.uioh <- state.uioh#key key mask
5013 let birdseyekeyboard key mask
5014 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5015 let incr =
5016 match conf.columns with
5017 | Csingle _ -> 1
5018 | Cmulti ((c, _, _), _) -> c
5019 | Csplit _ -> failwith "bird's eye split mode"
5021 let pgh layout = List.fold_left
5022 (fun m l -> max l.pageh m) state.winh layout in
5023 let open Keys in
5024 match Wsi.kc2kt key with
5025 | Ascii 'l' when Wsi.withctrl mask ->
5026 let y, h = getpageyh pageno in
5027 let top = (state.winh - h) / 2 in
5028 gotoxy state.x (max 0 (y - top))
5029 | Enter -> leavebirdseye beye false
5030 | Escape -> leavebirdseye beye true
5031 | Up -> upbirdseye incr beye
5032 | Down -> downbirdseye incr beye
5033 | Left -> upbirdseye 1 beye
5034 | Right -> downbirdseye 1 beye
5036 | Prior ->
5037 begin match state.layout with
5038 | l :: _ ->
5039 if l.pagey != 0
5040 then (
5041 state.mode <- Birdseye (
5042 oconf, leftx, l.pageno, hooverpageno, anchor
5044 gotopage1 l.pageno 0;
5046 else (
5047 let layout = layout state.x (state.y-state.winh)
5048 state.winw
5049 (pgh state.layout) in
5050 match layout with
5051 | [] -> gotoxy state.x (clamp (-state.winh))
5052 | l :: _ ->
5053 state.mode <- Birdseye (
5054 oconf, leftx, l.pageno, hooverpageno, anchor
5056 gotopage1 l.pageno 0
5059 | [] -> gotoxy state.x (clamp (-state.winh))
5060 end;
5062 | Next ->
5063 begin match List.rev state.layout with
5064 | l :: _ ->
5065 let layout = layout state.x
5066 (state.y + (pgh state.layout))
5067 state.winw state.winh in
5068 begin match layout with
5069 | [] ->
5070 let incr = l.pageh - l.pagevh in
5071 if incr = 0
5072 then (
5073 state.mode <-
5074 Birdseye (
5075 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5077 G.postRedisplay "birdseye pagedown";
5079 else gotoxy state.x (clamp (incr + conf.interpagespace*2));
5081 | l :: _ ->
5082 state.mode <-
5083 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5084 gotopage1 l.pageno 0;
5087 | [] -> gotoxy state.x (clamp state.winh)
5088 end;
5090 | Home ->
5091 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5092 gotopage1 0 0
5094 | End ->
5095 let pageno = state.pagecount - 1 in
5096 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5097 if not (pagevisible state.layout pageno)
5098 then
5099 let h =
5100 match List.rev state.pdims with
5101 | [] -> state.winh
5102 | (_, _, h, _) :: _ -> h
5104 gotoxy
5105 state.x
5106 (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5107 else G.postRedisplay "birdseye end";
5109 | Delete|Insert|Ascii _|Code _|Ctrl _|Fn _|Backspace -> viewkeyboard key mask
5112 let drawpage l =
5113 let color =
5114 match state.mode with
5115 | Textentry _ -> scalecolor 0.4
5116 | LinkNav _ | View -> scalecolor 1.0
5117 | Birdseye (_, _, pageno, hooverpageno, _) ->
5118 if l.pageno = hooverpageno
5119 then scalecolor 0.9
5120 else (
5121 if l.pageno = pageno
5122 then (
5123 let c = scalecolor 1.0 in
5124 GlDraw.color c;
5125 GlDraw.line_width 3.0;
5126 let dispx = l.pagedispx in
5127 linerect
5128 (float (dispx-1)) (float (l.pagedispy-1))
5129 (float (dispx+l.pagevw+1))
5130 (float (l.pagedispy+l.pagevh+1))
5132 GlDraw.line_width 1.0;
5135 else scalecolor 0.8
5138 drawtiles l color;
5141 let postdrawpage l linkindexbase =
5142 match getopaque l.pageno with
5143 | Some opaque ->
5144 if tileready l l.pagex l.pagey
5145 then
5146 let x = l.pagedispx - l.pagex
5147 and y = l.pagedispy - l.pagey in
5148 let hlmask =
5149 match conf.columns with
5150 | Csingle _ | Cmulti _ ->
5151 (if conf.hlinks then 1 else 0)
5152 + (if state.glinks
5153 && not (isbirdseye state.mode) then 2 else 0)
5154 | Csplit _ -> 0
5156 let s =
5157 match state.mode with
5158 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5159 | Textentry _
5160 | Birdseye _
5161 | View
5162 | LinkNav _ -> E.s
5164 Hashtbl.find_all state.prects l.pageno |>
5165 List.iter (fun vals -> drawprect opaque x y vals);
5166 let n = postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize) in
5167 if n < 0
5168 then (state.redisplay <- true; 0)
5169 else n
5170 else 0
5171 | _ -> 0
5174 let scrollindicator () =
5175 let sbw, ph, sh = state.uioh#scrollph in
5176 let sbh, pw, sw = state.uioh#scrollpw in
5178 let x0,x1,hx0 =
5179 if conf.leftscroll
5180 then (0, sbw, sbw)
5181 else ((state.winw - sbw), state.winw, 0)
5184 Gl.enable `blend;
5185 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5186 let (r, g, b, alpha) = conf.sbarcolor in
5187 GlDraw.color (r, g, b) ~alpha;
5188 filledrect (float x0) 0. (float x1) (float state.winh);
5189 filledrect
5190 (float hx0) (float (state.winh - sbh))
5191 (float (hx0 + state.winw)) (float state.winh);
5192 let (r, g, b, alpha) = conf.sbarhndlcolor in
5193 GlDraw.color (r, g, b) ~alpha;
5195 filledrect (float x0) ph (float x1) (ph +. sh);
5196 let pw = pw +. float hx0 in
5197 filledrect pw (float (state.winh - sbh)) (pw +. sw) (float state.winh);
5198 Gl.disable `blend;
5201 let showsel () =
5202 match state.mstate with
5203 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5206 | Msel ((x0, y0), (x1, y1)) ->
5207 let identify opaque l px py = Some (opaque, l.pageno, px, py) in
5208 let o0,n0,px0,py0 = onppundermouse identify x0 y0 (~< E.s, -1, 0, 0) in
5209 let _o1,n1,px1,py1 = onppundermouse identify x1 y1 (~< E.s, -1, 0, 0) in
5210 if n0 != -1 && n0 = n1 then seltext o0 (px0, py0, px1, py1);
5213 let showrects =
5214 function [] -> ()
5215 | rects ->
5216 Gl.enable `blend;
5217 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5218 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5219 List.iter
5220 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5221 List.iter (fun l ->
5222 if l.pageno = pageno
5223 then (
5224 let dx = float (l.pagedispx - l.pagex) in
5225 let dy = float (l.pagedispy - l.pagey) in
5226 let r, g, b, alpha = c in
5227 GlDraw.color (r, g, b) ~alpha;
5228 filledrect2 (x0+.dx) (y0+.dy)
5229 (x1+.dx) (y1+.dy)
5230 (x3+.dx) (y3+.dy)
5231 (x2+.dx) (y2+.dy);
5233 ) state.layout
5234 ) rects;
5235 Gl.disable `blend;
5238 let display () =
5239 GlDraw.color (scalecolor2 conf.bgcolor);
5240 GlClear.color (scalecolor2 conf.bgcolor);
5241 GlClear.clear [`color];
5242 List.iter drawpage state.layout;
5243 let rects =
5244 match state.mode with
5245 | LinkNav (Ltexact (pageno, linkno)) ->
5246 begin match getopaque pageno with
5247 | Some opaque ->
5248 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5249 let color = (0.0, 0.0, 0.5, 0.5) in
5250 (pageno, color,
5251 (float x0, float y0,
5252 float x1, float y0,
5253 float x1, float y1,
5254 float x0, float y1)
5255 ) :: state.rects
5256 | None -> state.rects
5258 | LinkNav (Ltgendir _) | LinkNav (Ltnotready _)
5259 | Birdseye _
5260 | Textentry _
5261 | View -> state.rects
5263 showrects rects;
5264 let rec postloop linkindexbase = function
5265 | l :: rest ->
5266 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5267 postloop linkindexbase rest
5268 | [] -> ()
5270 showsel ();
5271 postloop 0 state.layout;
5272 state.uioh#display;
5273 begin match state.mstate with
5274 | Mzoomrect ((x0, y0), (x1, y1)) ->
5275 Gl.enable `blend;
5276 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5277 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5278 filledrect (float x0) (float y0) (float x1) (float y1);
5279 Gl.disable `blend;
5280 | Msel _
5281 | Mpan _
5282 | Mscrolly | Mscrollx
5283 | Mzoom _
5284 | Mnone -> ()
5285 end;
5286 enttext ();
5287 scrollindicator ();
5288 Wsi.swapb ();
5291 let zoomrect x y x1 y1 =
5292 let x0 = min x x1
5293 and x1 = max x x1
5294 and y0 = min y y1 in
5295 let zoom = (float state.w) /. float (x1 - x0) in
5296 let margin =
5297 let simple () =
5298 if state.w < state.winw
5299 then (state.winw - state.w) / 2
5300 else 0
5302 match conf.fitmodel with
5303 | FitWidth | FitProportional -> simple ()
5304 | FitPage ->
5305 match conf.columns with
5306 | Csplit _ ->
5307 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
5308 | Cmulti _ | Csingle _ -> simple ()
5310 gotoxy ((state.x + margin) - x0) (state.y + y0);
5311 state.anchor <- getanchor ();
5312 setzoom zoom;
5313 resetmstate ();
5316 let annot inline x y =
5317 match unproject x y with
5318 | Some (opaque, n, ux, uy) ->
5319 let add text =
5320 addannot opaque ux uy text;
5321 wcmd "freepage %s" (~> opaque);
5322 Hashtbl.remove state.pagemap (n, state.gen);
5323 flushtiles ();
5324 gotoxy state.x state.y
5326 if inline
5327 then
5328 let ondone s = add s in
5329 let mode = state.mode in
5330 state.mode <- Textentry (
5331 ("annotation: ", E.s, None, textentry, ondone, true),
5332 fun _ -> state.mode <- mode);
5333 state.text <- E.s;
5334 enttext ();
5335 G.postRedisplay "annot"
5336 else
5337 add @@ getusertext E.s
5338 | _ -> ()
5341 let zoomblock x y =
5342 let g opaque l px py =
5343 match rectofblock opaque px py with
5344 | Some a ->
5345 let x0 = a.(0) -. 20. in
5346 let x1 = a.(1) +. 20. in
5347 let y0 = a.(2) -. 20. in
5348 let zoom = (float state.w) /. (x1 -. x0) in
5349 let pagey = getpagey l.pageno in
5350 let margin = (state.w - l.pagew)/2 in
5351 let nx = -truncate x0 - margin in
5352 gotoxy nx (pagey + truncate y0);
5353 state.anchor <- getanchor ();
5354 setzoom zoom;
5355 None
5356 | None -> None
5358 match conf.columns with
5359 | Csplit _ ->
5360 impmsg "block zooming does not work properly in split columns mode"
5361 | Cmulti _ | Csingle _ -> onppundermouse g x y ()
5364 let scrollx x =
5365 let winw = state.winw - 1 in
5366 let s = float x /. float winw in
5367 let destx = truncate (float (state.w + winw) *. s) in
5368 gotoxy (winw - destx) state.y;
5369 state.mstate <- Mscrollx;
5372 let scrolly y =
5373 let s = float y /. float state.winh in
5374 let desty = truncate (s *. float (maxy ())) in
5375 gotoxy state.x desty;
5376 state.mstate <- Mscrolly;
5379 let viewmulticlick clicks x y mask =
5380 let g opaque l px py =
5381 let mark =
5382 match clicks with
5383 | 2 -> Mark_word
5384 | 3 -> Mark_line
5385 | 4 -> Mark_block
5386 | _ -> Mark_page
5388 if markunder opaque px py mark
5389 then (
5390 Some (fun () ->
5391 let dopipe cmd =
5392 match getopaque l.pageno with
5393 | None -> ()
5394 | Some opaque -> pipesel opaque cmd
5396 state.roam <- (fun () -> dopipe conf.paxcmd);
5397 if not (Wsi.withctrl mask) then dopipe conf.selcmd;
5400 else None
5402 G.postRedisplay "viewmulticlick";
5403 onppundermouse g x y (fun () -> impmsg "nothing to select") ();
5406 let canselect () =
5407 match conf.columns with
5408 | Csplit _ -> false
5409 | Csingle _ | Cmulti _ -> conf.angle mod 360 = 0
5412 let viewmouse button down x y mask =
5413 match button with
5414 | n when (n == 4 || n == 5) && not down ->
5415 if Wsi.withctrl mask
5416 then (
5417 let incr =
5418 if n = 5
5419 then if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5420 else if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5422 let fx, fy =
5423 match state.mstate with
5424 | Mzoom (oldn, _, pos) when n = oldn -> pos
5425 | Mzoomrect _ | Mnone | Mpan _
5426 | Msel _ | Mscrollx | Mscrolly | Mzoom _ -> (x, y)
5428 let zoom = conf.zoom -. incr in
5429 state.mstate <- Mzoom (n, 0, (x, y));
5430 if false && abs (fx - x) > 5 || abs (fy - y) > 5
5431 then pivotzoom ~x ~y zoom
5432 else pivotzoom zoom
5434 else (
5435 match state.autoscroll with
5436 | Some step -> setautoscrollspeed step (n=4)
5437 | None ->
5438 if conf.wheelbypage || conf.presentation
5439 then (
5440 if n = 4
5441 then prevpage ()
5442 else nextpage ()
5444 else
5445 let incr =
5446 if n = 4
5447 then -conf.scrollstep
5448 else conf.scrollstep
5450 let incr = incr * 2 in
5451 let y = clamp incr in
5452 gotoxy state.x y
5455 | n when (n = 6 || n = 7) && not down && canpan () ->
5456 let x =
5457 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep) in
5458 gotoxy x state.y
5460 | 1 when Wsi.withshift mask ->
5461 state.mstate <- Mnone;
5462 if not down
5463 then (
5464 match unproject x y with
5465 | None -> ()
5466 | Some (_, pageno, ux, uy) ->
5467 let cmd = Printf.sprintf
5468 "%s %s %d %d %d"
5469 conf.stcmd state.path pageno ux uy
5471 match spawn cmd [] with
5472 | exception exn ->
5473 impmsg "execution of synctex command(%S) failed: %S"
5474 conf.stcmd @@ exntos exn
5475 | _pid -> ()
5478 | 1 when Wsi.withctrl mask ->
5479 if down
5480 then (
5481 Wsi.setcursor Wsi.CURSOR_FLEUR;
5482 state.mstate <- Mpan (x, y)
5484 else
5485 state.mstate <- Mnone
5487 | 3 ->
5488 if down
5489 then (
5490 if Wsi.withshift mask
5491 then (
5492 annot conf.annotinline x y;
5493 G.postRedisplay "addannot"
5495 else
5496 let p = (x, y) in
5497 Wsi.setcursor Wsi.CURSOR_CYCLE;
5498 state.mstate <- Mzoomrect (p, p)
5500 else (
5501 match state.mstate with
5502 | Mzoomrect ((x0, y0), _) ->
5503 if abs (x-x0) > 10 && abs (y - y0) > 10
5504 then zoomrect x0 y0 x y
5505 else (
5506 resetmstate ();
5507 G.postRedisplay "kill accidental zoom rect";
5509 | Msel _
5510 | Mpan _
5511 | Mscrolly | Mscrollx
5512 | Mzoom _
5513 | Mnone -> resetmstate ()
5516 | 1 when vscrollhit x ->
5517 if down
5518 then
5519 let _, position, sh = state.uioh#scrollph in
5520 if y > truncate position && y < truncate (position +. sh)
5521 then state.mstate <- Mscrolly
5522 else scrolly y
5523 else
5524 state.mstate <- Mnone
5526 | 1 when y > state.winh - hscrollh () ->
5527 if down
5528 then
5529 let _, position, sw = state.uioh#scrollpw in
5530 if x > truncate position && x < truncate (position +. sw)
5531 then state.mstate <- Mscrollx
5532 else scrollx x
5533 else
5534 state.mstate <- Mnone
5536 | 1 when state.bzoom -> if not down then zoomblock x y
5538 | 1 ->
5539 let dest = if down then getunder x y else Unone in
5540 begin match dest with
5541 | Ulinkuri _ ->
5542 gotounder dest
5544 | Unone when down ->
5545 Wsi.setcursor Wsi.CURSOR_FLEUR;
5546 state.mstate <- Mpan (x, y);
5548 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
5550 | Unone | Utext _ ->
5551 if down
5552 then (
5553 if canselect ()
5554 then (
5555 state.mstate <- Msel ((x, y), (x, y));
5556 G.postRedisplay "mouse select";
5559 else (
5560 match state.mstate with
5561 | Mnone -> ()
5563 | Mzoom _ | Mscrollx | Mscrolly ->
5564 state.mstate <- Mnone
5566 | Mzoomrect ((x0, y0), _) ->
5567 zoomrect x0 y0 x y
5569 | Mpan _ ->
5570 Wsi.setcursor Wsi.CURSOR_INHERIT;
5571 state.mstate <- Mnone
5573 | Msel ((x0, y0), (x1, y1)) ->
5574 let rec loop = function
5575 | [] -> ()
5576 | l :: rest ->
5577 let inside =
5578 let a0 = l.pagedispy in
5579 let a1 = a0 + l.pagevh in
5580 let b0 = l.pagedispx in
5581 let b1 = b0 + l.pagevw in
5582 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5583 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5585 if inside
5586 then
5587 match getopaque l.pageno with
5588 | Some opaque ->
5589 let dosel cmd () =
5590 pipef ~closew:false "Msel"
5591 (fun w ->
5592 copysel w opaque;
5593 G.postRedisplay "Msel") cmd
5595 dosel conf.selcmd ();
5596 state.roam <- dosel conf.paxcmd;
5597 | None -> ()
5598 else loop rest
5600 loop state.layout;
5601 resetmstate ();
5605 | _ -> ()
5608 let birdseyemouse button down x y mask
5609 (conf, leftx, _, hooverpageno, anchor) =
5610 match button with
5611 | 1 when down ->
5612 let rec loop = function
5613 | [] -> ()
5614 | l :: rest ->
5615 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5616 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5617 then (
5618 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5620 else loop rest
5622 loop state.layout
5623 | 3 -> ()
5624 | _ -> viewmouse button down x y mask
5627 let uioh = object
5628 method display = ()
5630 method key key mask =
5631 begin match state.mode with
5632 | Textentry textentry -> textentrykeyboard key mask textentry
5633 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5634 | View -> viewkeyboard key mask
5635 | LinkNav linknav -> linknavkeyboard key mask linknav
5636 end;
5637 state.uioh
5639 method button button bstate x y mask =
5640 begin match state.mode with
5641 | LinkNav _ | View -> viewmouse button bstate x y mask
5642 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5643 | Textentry _ -> ()
5644 end;
5645 state.uioh
5647 method multiclick clicks x y mask =
5648 begin match state.mode with
5649 | LinkNav _ | View -> viewmulticlick clicks x y mask
5650 | Birdseye _ | Textentry _ -> ()
5651 end;
5652 state.uioh
5654 method motion x y =
5655 begin match state.mode with
5656 | Textentry _ -> ()
5657 | View | Birdseye _ | LinkNav _ ->
5658 match state.mstate with
5659 | Mzoom _ | Mnone -> ()
5661 | Mpan (x0, y0) ->
5662 let dx = x - x0
5663 and dy = y0 - y in
5664 state.mstate <- Mpan (x, y);
5665 let x = if canpan () then panbound (state.x + dx) else state.x in
5666 let y = clamp dy in
5667 gotoxy x y
5669 | Msel (a, _) ->
5670 state.mstate <- Msel (a, (x, y));
5671 G.postRedisplay "motion select";
5673 | Mscrolly ->
5674 let y = min state.winh (max 0 y) in
5675 scrolly y
5677 | Mscrollx ->
5678 let x = min state.winw (max 0 x) in
5679 scrollx x
5681 | Mzoomrect (p0, _) ->
5682 state.mstate <- Mzoomrect (p0, (x, y));
5683 G.postRedisplay "motion zoomrect";
5684 end;
5685 state.uioh
5687 method pmotion x y =
5688 begin match state.mode with
5689 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5690 let rec loop = function
5691 | [] ->
5692 if hooverpageno != -1
5693 then (
5694 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5695 G.postRedisplay "pmotion birdseye no hoover";
5697 | l :: rest ->
5698 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5699 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5700 then (
5701 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5702 G.postRedisplay "pmotion birdseye hoover";
5704 else loop rest
5706 loop state.layout
5708 | Textentry _ -> ()
5710 | LinkNav _ | View ->
5711 match state.mstate with
5712 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ -> ()
5713 | Mnone ->
5714 updateunder x y;
5715 if canselect ()
5716 then
5717 match conf.pax with
5718 | None -> ()
5719 | Some past ->
5720 let now = now () in
5721 let delta = now -. past in
5722 if delta > 0.01
5723 then paxunder x y
5724 else conf.pax <- Some now
5725 end;
5726 state.uioh
5728 method infochanged _ = ()
5730 method scrollph =
5731 let maxy = maxy () in
5732 let p, h =
5733 if maxy = 0
5734 then 0.0, float state.winh
5735 else scrollph state.y maxy
5737 vscrollw (), p, h
5739 method scrollpw =
5740 let fwinw = float (state.winw - vscrollw ()) in
5741 let sw =
5742 let sw = fwinw /. float state.w in
5743 let sw = fwinw *. sw in
5744 max sw (float conf.scrollh)
5746 let position =
5747 let maxx = state.w + state.winw in
5748 let x = state.winw - state.x in
5749 let percent = float x /. float maxx in
5750 (fwinw -. sw) *. percent
5752 hscrollh (), position, sw
5754 method modehash =
5755 let modename =
5756 match state.mode with
5757 | LinkNav _ -> "links"
5758 | Textentry _ -> "textentry"
5759 | Birdseye _ -> "birdseye"
5760 | View -> "view"
5762 findkeyhash conf modename
5764 method eformsgs = true
5765 method alwaysscrolly = false
5766 method scroll dx dy =
5767 let x = if canpan () then panbound (state.x + dx) else state.x in
5768 gotoxy x (clamp (2 * dy));
5769 state.uioh
5770 method zoom z x y =
5771 pivotzoom ~x ~y (conf.zoom *. exp z);
5772 end;;
5774 let addrect pageno r g b a x0 y0 x1 y1 =
5775 Hashtbl.add state.prects pageno [|r; g; b; a; x0; y0; x1; y1|];
5778 let ract cmds =
5779 let cl = splitatchar cmds ' ' in
5780 let scan s fmt f =
5781 try Scanf.sscanf s fmt f
5782 with exn -> adderrfmt "remote exec" "error processing '%S': %s\n"
5783 cmds @@ exntos exn
5785 let rectx s pageno (r, g, b, a) x0 y0 x1 y1 =
5786 vlog "%s page %d color (%f %f %f %f) x0,y0,x1,y1 = %f %f %f %f"
5787 s pageno r g b a x0 y0 x1 y1;
5788 onpagerect
5789 pageno
5790 (fun w h ->
5791 let _,w1,h1,_ = getpagedim pageno in
5792 let sw = float w1 /. float w
5793 and sh = float h1 /. float h in
5794 let x0s = x0 *. sw
5795 and x1s = x1 *. sw
5796 and y0s = y0 *. sh
5797 and y1s = y1 *. sh in
5798 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
5799 let color = (r, g, b, a) in
5800 if conf.verbose then debugrect rect;
5801 state.rects <- (pageno, color, rect) :: state.rects;
5802 G.postRedisplay s;
5805 match cl with
5806 | "reload", "" -> reload ()
5807 | "goto", args ->
5808 scan args "%u %f %f"
5809 (fun pageno x y ->
5810 let cmd, _ = state.geomcmds in
5811 if emptystr cmd
5812 then gotopagexy pageno x y
5813 else
5814 let f prevf () =
5815 gotopagexy pageno x y;
5816 prevf ()
5818 state.reprf <- f state.reprf
5820 | "goto1", args -> scan args "%u %f" gotopage
5821 | "gotor", args -> scan args "%S" gotoremote
5822 | "rect", args ->
5823 scan args "%u %u %f %f %f %f"
5824 (fun pageno c x0 y0 x1 y1 ->
5825 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
5826 rectx "rect" pageno color x0 y0 x1 y1;
5828 | "prect", args ->
5829 scan args "%u %f %f %f %f %f %f %f %f"
5830 (fun pageno r g b alpha x0 y0 x1 y1 ->
5831 addrect pageno r g b alpha x0 y0 x1 y1;
5832 G.postRedisplay "prect"
5834 | "pgoto", args ->
5835 scan args "%u %f %f"
5836 (fun pageno x y ->
5837 let optopaque =
5838 match getopaque pageno with
5839 | Some opaque -> opaque
5840 | None -> ~< E.s
5842 pgoto optopaque pageno x y;
5843 let rec fixx = function
5844 | [] -> ()
5845 | l :: rest ->
5846 if l.pageno = pageno
5847 then gotoxy (state.x - l.pagedispx) state.y
5848 else fixx rest
5850 let layout =
5851 let mult =
5852 match conf.columns with
5853 | Csingle _ | Csplit _ -> 1
5854 | Cmulti ((n, _, _), _) -> n
5856 layout 0 state.y (state.winw * mult) state.winh
5858 fixx layout
5860 | "activatewin", "" -> Wsi.activatewin ()
5861 | "quit", "" -> raise Quit
5862 | "keys", keys ->
5863 begin try
5864 let l = Config.keys_of_string keys in
5865 List.iter (fun (k, m) -> keyboard k m) l
5866 with exn -> adderrfmt "error processing keys" "`%S': %s\n"
5867 cmds @@ exntos exn
5869 | "clearrects", "" ->
5870 Hashtbl.clear state.prects;
5871 G.postRedisplay "clearrects"
5872 | _ ->
5873 adderrfmt "remote command"
5874 "error processing remote command: %S\n" cmds;
5877 let remote =
5878 let scratch = Bytes.create 80 in
5879 let buf = Buffer.create 80 in
5880 fun fd ->
5881 match tempfailureretry (Unix.read fd scratch 0) 80 with
5882 | exception Unix.Unix_error (Unix.EAGAIN, _, _) -> None
5883 | 0 ->
5884 Unix.close fd;
5885 if Buffer.length buf > 0
5886 then (
5887 let s = Buffer.contents buf in
5888 Buffer.clear buf;
5889 ract s;
5891 None
5892 | n ->
5893 let rec eat ppos =
5894 let nlpos =
5895 match Bytes.index_from scratch ppos '\n' with
5896 | pos -> if pos >= n then -1 else pos
5897 | exception Not_found -> -1
5899 if nlpos >= 0
5900 then (
5901 Buffer.add_subbytes buf scratch ppos (nlpos-ppos);
5902 let s = Buffer.contents buf in
5903 Buffer.clear buf;
5904 ract s;
5905 eat (nlpos+1);
5907 else (
5908 Buffer.add_subbytes buf scratch ppos (n-ppos);
5909 Some fd
5911 in eat 0
5914 let remoteopen path =
5915 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
5916 with exn ->
5917 adderrfmt "remoteopen" "error opening %S: %s" path @@ exntos exn;
5918 None
5921 let () =
5922 let gcconfig = ref false in
5923 let trimcachepath = ref E.s in
5924 let rcmdpath = ref E.s in
5925 let pageno = ref None in
5926 let openlast = ref false in
5927 let doreap = ref false in
5928 let csspath = ref None in
5929 selfexec := Sys.executable_name;
5930 Arg.parse
5931 (Arg.align
5932 [("-p", Arg.String (fun s -> state.password <- s),
5933 "<password> Set password");
5935 ("-f", Arg.String
5936 (fun s ->
5937 Config.fontpath := s;
5938 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
5940 "<path> Set path to the user interface font");
5942 ("-c", Arg.String
5943 (fun s ->
5944 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
5945 Config.confpath := s),
5946 "<path> Set path to the configuration file");
5948 ("-last", Arg.Set openlast, " Open last document");
5950 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
5951 "<page-number> Jump to page");
5953 ("-tcf", Arg.String (fun s -> trimcachepath := s),
5954 "<path> Set path to the trim cache file");
5956 ("-dest", Arg.String (fun s -> state.nameddest <- s),
5957 "<named-destination> Set named destination");
5959 ("-remote", Arg.String (fun s -> rcmdpath := s),
5960 "<path> Set path to the source of remote commands");
5962 ("-gc", Arg.Set gcconfig, " Collect config garbage");
5964 ("-v", Arg.Unit (fun () ->
5965 Printf.printf
5966 "%s\nconfiguration path: %s\n"
5967 (version ())
5968 Config.defconfpath;
5969 exit 0), " Print version and exit");
5971 ("-css", Arg.String (fun s -> csspath := Some s),
5972 "<path> Set path to the style sheet to use with EPUB/HTML");
5974 ("-origin", Arg.String (fun s -> state.origin <- s),
5975 "<origin> <undocumented>");
5977 ("-no-title", Arg.Set ignoredoctitlte, " ignore document title");
5978 ("-layout-height", Arg.Set_int layouth,
5979 "<height> layout height html/epub/etc (-1, 0, N)");
5982 (fun s -> state.path <- s)
5983 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:");
5985 let histmode = emptystr state.path && not !openlast in
5987 if not (Config.load !openlast)
5988 then dolog "failed to load configuration";
5990 begin match !pageno with
5991 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
5992 | None -> ()
5993 end;
5995 fillhelp ();
5996 if !gcconfig
5997 then (
5998 Config.gc ();
5999 exit 0
6002 let mu =
6003 object (self)
6004 val mutable m_clicks = 0
6005 val mutable m_click_x = 0
6006 val mutable m_click_y = 0
6007 val mutable m_lastclicktime = infinity
6009 method private cleanup =
6010 state.roam <- noroam;
6011 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap
6012 method expose = G.postRedisplay "expose"
6013 method visible v =
6014 let name =
6015 match v with
6016 | Wsi.Unobscured -> "unobscured"
6017 | Wsi.PartiallyObscured -> "partiallyobscured"
6018 | Wsi.FullyObscured -> "fullyobscured"
6020 vlog "visibility change %s" name
6021 method display = display ()
6022 method map mapped = vlog "mapped %b" mapped
6023 method reshape w h =
6024 self#cleanup;
6025 reshape w h
6026 method mouse b d x y m =
6027 if d && canselect ()
6028 then (
6030 * http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx
6032 m_click_x <- x;
6033 m_click_y <- y;
6034 if b = 1
6035 then (
6036 let t = now () in
6037 if abs x - m_click_x > 10
6038 || abs y - m_click_y > 10
6039 || abs_float (t -. m_lastclicktime) > 0.3
6040 then m_clicks <- 0;
6041 m_clicks <- m_clicks + 1;
6042 m_lastclicktime <- t;
6043 if m_clicks = 1
6044 then (
6045 self#cleanup;
6046 G.postRedisplay "cleanup";
6047 state.uioh <- state.uioh#button b d x y m;
6049 else state.uioh <- state.uioh#multiclick m_clicks x y m
6051 else (
6052 self#cleanup;
6053 m_clicks <- 0;
6054 m_lastclicktime <- infinity;
6055 state.uioh <- state.uioh#button b d x y m
6058 else (
6059 state.uioh <- state.uioh#button b d x y m
6061 method motion x y =
6062 state.mpos <- (x, y);
6063 state.uioh <- state.uioh#motion x y
6064 method pmotion x y =
6065 state.mpos <- (x, y);
6066 state.uioh <- state.uioh#pmotion x y
6067 method key k m =
6068 vlog "k=%#x m=%#x" k m;
6069 let mascm = m land (
6070 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6071 ) in
6072 let keyboard k m =
6073 let x = state.x and y = state.y in
6074 keyboard k m;
6075 if x != state.x || y != state.y then self#cleanup
6077 match state.keystate with
6078 | KSnone ->
6079 let km = k, mascm in
6080 begin
6081 match
6082 let modehash = state.uioh#modehash in
6083 try Hashtbl.find modehash km
6084 with Not_found ->
6085 try Hashtbl.find (findkeyhash conf "global") km
6086 with Not_found -> KMinsrt (k, m)
6087 with
6088 | KMinsrt (k, m) -> keyboard k m
6089 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6090 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6092 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6093 List.iter (fun (k, m) -> keyboard k m) insrt;
6094 state.keystate <- KSnone
6095 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6096 state.keystate <- KSinto (keys, insrt)
6097 | KSinto _ -> state.keystate <- KSnone
6099 method enter x y =
6100 state.mpos <- (x, y);
6101 state.uioh <- state.uioh#pmotion x y
6102 method leave = state.mpos <- (-1, -1)
6103 method winstate wsl = state.winstate <- wsl
6104 method quit : 'a. 'a = raise Quit
6105 method scroll dx dy = state.uioh <- state.uioh#scroll dx dy
6106 method zoom z x y = state.uioh#zoom z x y
6107 method opendoc path =
6108 state.mode <- View;
6109 state.uioh <- uioh;
6110 G.postRedisplay "opendoc";
6111 opendoc path state.password
6114 let wsfd, winw, winh = Wsi.init mu conf.cwinw conf.cwinh platform in
6115 state.wsfd <- wsfd;
6117 if not @@ List.exists GlMisc.check_extension
6118 [ "GL_ARB_texture_rectangle"
6119 ; "GL_EXT_texture_recangle"
6120 ; "GL_NV_texture_rectangle" ]
6121 then (dolog "OpenGL does not suppport rectangular textures"; exit 1);
6123 if substratis (GlMisc.get_string `renderer) 0 "Mesa DRI Intel("
6124 then (
6125 defconf.sliceheight <- 1024;
6126 defconf.texcount <- 32;
6127 defconf.usepbo <- true;
6130 let cs, ss =
6131 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6132 | exception exn ->
6133 dolog "socketpair failed: %s" @@ exntos exn;
6134 exit 1
6135 | (r, w) ->
6136 cloexec r;
6137 cloexec w;
6138 r, w
6141 setcheckers conf.checkers;
6143 opengl_has_pbo := GlMisc.check_extension "GL_ARB_pixel_buffer_object";
6145 begin match !csspath with
6146 | None -> ()
6147 | Some "" -> conf.css <- E.s
6148 | Some path ->
6149 let css = filecontents path in
6150 let l = String.length css in
6151 conf.css <-
6152 if substratis css (l-2) "\r\n"
6153 then String.sub css 0 (l-2)
6154 else (if css.[l-1] = '\n'
6155 then String.sub css 0 (l-1)
6156 else css);
6157 end;
6158 init cs (
6159 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
6160 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6161 !Config.fontpath, !trimcachepath, !opengl_has_pbo
6163 List.iter GlArray.enable [`texture_coord; `vertex];
6164 state.ss <- ss;
6165 reshape ~firsttime:true winw winh;
6166 state.uioh <- uioh;
6167 if histmode
6168 then (
6169 Wsi.settitle "llpp (history)";
6170 enterhistmode ();
6172 else (
6173 state.text <- "Opening " ^ (mbtoutf8 state.path);
6174 opendoc state.path state.password;
6176 display ();
6177 Wsi.mapwin ();
6178 Wsi.setcursor Wsi.CURSOR_INHERIT;
6179 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
6181 let rec reap () =
6182 match Unix.waitpid [Unix.WNOHANG] ~-1 with
6183 | exception (Unix.Unix_error (Unix.ECHILD, _, _)) -> ()
6184 | exception exn -> dolog "Unix.waitpid: %s" @@ exntos exn
6185 | 0, _ -> ()
6186 | _pid, _status -> reap ()
6188 Sys.set_signal Sys.sigchld (Sys.Signal_handle (fun _ -> doreap := true));
6190 let optrfd =
6191 ref (
6192 if nonemptystr !rcmdpath
6193 then remoteopen !rcmdpath
6194 else None
6198 let rec loop deadline =
6199 if !doreap
6200 then (
6201 doreap := false;
6202 reap ()
6204 let r = [state.ss; state.wsfd] in
6205 let r =
6206 match !optrfd with
6207 | None -> r
6208 | Some fd -> fd :: r
6210 if state.redisplay
6211 then (
6212 state.redisplay <- false;
6213 display ();
6215 let timeout =
6216 let now = now () in
6217 if deadline > now
6218 then (
6219 if deadline = infinity
6220 then ~-.1.0
6221 else max 0.0 (deadline -. now)
6223 else 0.0
6225 let r, _, _ =
6226 try Unix.select r [] [] timeout
6227 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6229 begin match r with
6230 | [] ->
6231 let newdeadline =
6232 match state.autoscroll with
6233 | Some step when step != 0 ->
6234 if state.slideshow land 1 = 1
6235 then (
6236 if state.slideshow land 2 = 0
6237 then state.slideshow <- state.slideshow lor 2
6238 else if step < 0 then prevpage () else nextpage ();
6239 deadline +. (float (abs step))
6241 else
6242 let y = state.y + step in
6243 let fy = if conf.maxhfit then state.winh else 0 in
6244 let y =
6245 if y < 0
6246 then state.maxy - fy
6247 else if y >= state.maxy - fy then 0 else y
6249 gotoxy state.x y;
6250 deadline +. 0.01
6251 | _ -> infinity
6253 loop newdeadline
6255 | l ->
6256 let rec checkfds = function
6257 | [] -> ()
6258 | fd :: rest when fd = state.ss ->
6259 let cmd = rcmd state.ss in
6260 act cmd;
6261 checkfds rest
6263 | fd :: rest when fd = state.wsfd ->
6264 Wsi.readresp fd;
6265 checkfds rest
6267 | fd :: rest when Some fd = !optrfd ->
6268 begin match remote fd with
6269 | None -> optrfd := remoteopen !rcmdpath;
6270 | opt -> optrfd := opt
6271 end;
6272 checkfds rest
6274 | _ :: rest ->
6275 dolog "select returned unknown descriptor";
6276 checkfds rest
6278 checkfds l;
6279 let newdeadline =
6280 let deadline1 =
6281 if deadline = infinity
6282 then now () +. 0.01
6283 else deadline
6285 match state.autoscroll with
6286 | Some step when step != 0 -> deadline1
6287 | _ -> infinity
6289 loop newdeadline
6290 end;
6292 match loop infinity with
6293 | exception Quit ->
6294 Config.save leavebirdseye;
6295 if hasunsavedchanges ()
6296 then save ()
6297 | _ -> error "umpossible - infinity reached"