Sync with upstream
[llpp.git] / main.ml
blobd6896a85b1f56b9e7d32f34f83f16ddb501f44f0
1 open Utils;;
2 open Config;;
3 open Glutils;;
4 open Listview;;
5 open Ffi;;
7 let selfexec = ref E.s;;
8 let ignoredoctitlte = ref false;;
9 let layouth = ref ~-1;;
10 let checkerstexid = ref None;;
12 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
13 dolog {|rect {
14 x0,y0=(% f, % f)
15 x1,y1=(% f, % f)
16 x2,y2=(% f, % f)
17 x3,y3=(% f, % f)
18 }|} x0 y0 x1 y1 x2 y2 x3 y3;
21 let pgscale h = truncate (float h *. conf.pgscale);;
23 let hscrollh () =
24 if ((conf.scrollb land scrollbhv != 0) && (state.w > state.winw))
25 || state.uioh#alwaysscrolly
26 then conf.scrollbw
27 else 0
30 let setfontsize n =
31 fstate.fontsize <- n;
32 fstate.wwidth <- measurestr fstate.fontsize "w";
33 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
36 let launchpath () =
37 if emptystr conf.pathlauncher
38 then dolog "%s" state.path
39 else (
40 let command =
41 Str.global_replace Utils.Re.percent state.path conf.pathlauncher in
42 match spawn command [] with
43 | _pid -> ()
44 | exception exn -> dolog "failed to execute `%s': %s" command @@ exntos exn
48 let getopaque pageno =
49 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
50 with Not_found -> None
53 let pagetranslatepoint l x y =
54 let dy = y - l.pagedispy in
55 let y = dy + l.pagey in
56 let dx = x - l.pagedispx in
57 let x = dx + l.pagex in
58 (x, y);
61 let onppundermouse g x y d =
62 let rec f = function
63 | l :: rest ->
64 begin match getopaque l.pageno with
65 | Some opaque ->
66 let x0 = l.pagedispx in
67 let x1 = x0 + l.pagevw in
68 let y0 = l.pagedispy in
69 let y1 = y0 + l.pagevh in
70 if y >= y0 && y <= y1 && x >= x0 && x <= x1
71 then
72 let px, py = pagetranslatepoint l x y in
73 match g opaque l px py with
74 | Some res -> res
75 | None -> f rest
76 else f rest
77 | _ -> f rest
78 end
79 | [] -> d
81 f state.layout
84 let getunder x y =
85 let g opaque l px py =
86 if state.bzoom
87 then (
88 match rectofblock opaque px py with
89 | Some [|x0;x1;y0;y1|] ->
90 let rect = (x0, y0, x1, y0, x1, y1, x0, y1) in
91 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
92 state.rects <- [l.pageno, color, rect];
93 postRedisplay "getunder";
94 | _ -> ()
96 let under = whatsunder opaque px py in
97 if under = Unone then None else Some under
99 onppundermouse g x y Unone
102 let unproject x y =
103 let g opaque l x y =
104 match unproject opaque x y with
105 | Some (x, y) -> Some (Some (opaque, l.pageno, x, y))
106 | None -> None
108 onppundermouse g x y None;
111 let showtext c s =
112 state.text <- Printf.sprintf "%c%s" c s;
113 postRedisplay "showtext";
116 let impmsg fmt = Format.ksprintf (fun s -> showtext '!' s) fmt;;
118 let pipesel opaque cmd =
119 if hassel opaque
120 then pipef ~closew:false "pipesel"
121 (fun w ->
122 copysel w opaque;
123 postRedisplay "pipesel"
124 ) cmd
127 let paxunder x y =
128 let g opaque l px py =
129 if markunder opaque px py conf.paxmark
130 then
131 Some (fun () ->
132 match getopaque l.pageno with
133 | None -> ()
134 | Some opaque -> pipesel opaque conf.paxcmd
136 else None
138 postRedisplay "paxunder";
139 if conf.paxmark = Mark_page
140 then
141 List.iter (fun l ->
142 match getopaque l.pageno with
143 | None -> ()
144 | Some opaque -> clearmark opaque) state.layout;
145 state.roam <- onppundermouse g x y (fun () -> impmsg "whoopsie daisy");
148 let undertext = function
149 | Unone -> "none"
150 | Ulinkuri s -> s
151 | Utext s -> "font: " ^ s
152 | Uannotation (opaque, slinkindex) ->
153 "annotation: " ^ getannotcontents opaque slinkindex
156 let updateunder x y =
157 match getunder x y with
158 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
159 | Ulinkuri uri ->
160 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
161 Wsi.setcursor Wsi.CURSOR_INFO
162 | Utext s ->
163 if conf.underinfo then showtext 'f' ("ont: " ^ s);
164 Wsi.setcursor Wsi.CURSOR_TEXT
165 | Uannotation _ ->
166 if conf.underinfo then showtext 'a' "nnotation";
167 Wsi.setcursor Wsi.CURSOR_INFO
170 let showlinktype under =
171 if conf.underinfo && under != Unone
172 then showtext ' ' @@ undertext under
175 let intentry_with_suffix text key =
176 let text =
177 match [@warning "-4"] key with
178 | Keys.Ascii ('0'..'9' as c) -> addchar text c
179 | Keys.Ascii ('k' | 'm' | 'g' | 'K' | 'M' | 'G' as c) ->
180 addchar text @@ asciilower c
181 | _ ->
182 state.text <- "invalid key";
183 text
185 TEcont text
188 let wcmd fmt =
189 let b = Buffer.create 16 in
190 Printf.kbprintf
191 (fun b ->
192 let b = Buffer.to_bytes b in
193 wcmd state.ss b @@ Bytes.length b
194 ) b fmt
197 let nogeomcmds = function
198 | s, [] -> emptystr s
199 | _ -> false
202 let layoutN ((columns, coverA, coverB), b) x y sw sh =
203 let rec fold accu n =
204 if n = Array.length b
205 then accu
206 else
207 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
208 if (vy - y) > sh &&
209 (n = coverA - 1
210 || n = state.pagecount - coverB
211 || (n - coverA) mod columns = columns - 1)
212 then accu
213 else
214 let accu =
215 if vy + h > y
216 then
217 let pagey = max 0 (y - vy) in
218 let pagedispy = if pagey > 0 then 0 else vy - y in
219 let pagedispx, pagex =
220 let pdx =
221 if n = coverA - 1 || n = state.pagecount - coverB
222 then x + (sw - w) / 2
223 else dx + xoff + x
225 if pdx < 0
226 then 0, -pdx
227 else pdx, 0
229 let pagevw =
230 let vw = sw - pagedispx in
231 let pw = w - pagex in
232 min vw pw
234 let pagevh = min (h - pagey) (sh - pagedispy) in
235 if pagevw > 0 && pagevh > 0
236 then
237 { pageno = n
238 ; pagedimno = pdimno
239 ; pagew = w
240 ; pageh = h
241 ; pagex = pagex
242 ; pagey = pagey
243 ; pagevw = pagevw
244 ; pagevh = pagevh
245 ; pagedispx = pagedispx
246 ; pagedispy = pagedispy
247 ; pagecol = 0
248 } :: accu
249 else accu
250 else accu
252 fold accu (n+1)
254 if Array.length b = 0
255 then []
256 else List.rev (fold [] (page_of_y y))
259 let layoutS (columns, b) x y sw sh =
260 let rec fold accu n =
261 if n = Array.length b
262 then accu
263 else
264 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
265 if (vy - y) > sh
266 then accu
267 else
268 let accu =
269 if vy + pageh > y
270 then
271 let x = xoff + x in
272 let pagey = max 0 (y - vy) in
273 let pagedispy = if pagey > 0 then 0 else vy - y in
274 let pagedispx, pagex =
275 if px = 0
276 then (
277 if x < 0
278 then 0, -x
279 else x, 0
281 else (
282 let px = px - x in
283 if px < 0
284 then -px, 0
285 else 0, px
288 let pagecolw = pagew/columns in
289 let pagedispx =
290 if pagecolw < sw
291 then pagedispx + ((sw - pagecolw) / 2)
292 else pagedispx
294 let pagevw =
295 let vw = sw - pagedispx in
296 let pw = pagew - pagex in
297 min vw pw
299 let pagevw = min pagevw pagecolw in
300 let pagevh = min (pageh - pagey) (sh - pagedispy) in
301 if pagevw > 0 && pagevh > 0
302 then
303 { pageno = n/columns
304 ; pagedimno = pdimno
305 ; pagew = pagew
306 ; pageh = pageh
307 ; pagex = pagex
308 ; pagey = pagey
309 ; pagevw = pagevw
310 ; pagevh = pagevh
311 ; pagedispx = pagedispx
312 ; pagedispy = pagedispy
313 ; pagecol = n mod columns
314 } :: accu
315 else accu
316 else accu
318 fold accu (n+1)
320 List.rev (fold [] 0)
323 let layout x y sw sh =
324 if nogeomcmds state.geomcmds
325 then
326 match conf.columns with
327 | Csingle b -> layoutN ((1, 0, 0), b) x y sw sh
328 | Cmulti c -> layoutN c x y sw sh
329 | Csplit s -> layoutS s x y sw sh
330 else []
333 let maxy () = state.maxy - if conf.maxhfit then state.winh else 0;;
334 let clamp incr = bound (state.y + incr) 0 @@ maxy ();;
336 let itertiles l f =
337 let tilex = l.pagex mod conf.tilew in
338 let tiley = l.pagey mod conf.tileh in
340 let col = l.pagex / conf.tilew in
341 let row = l.pagey / conf.tileh in
343 let rec rowloop row y0 dispy h =
344 if h != 0
345 then
346 let dh = conf.tileh - y0 in
347 let dh = min h dh in
348 let rec colloop col x0 dispx w =
349 if w != 0
350 then
351 let dw = conf.tilew - x0 in
352 let dw = min w dw in
353 f col row dispx dispy x0 y0 dw dh;
354 colloop (col+1) 0 (dispx+dw) (w-dw)
356 colloop col tilex l.pagedispx l.pagevw;
357 rowloop (row+1) 0 (dispy+dh) (h-dh)
359 if l.pagevw > 0 && l.pagevh > 0
360 then rowloop row tiley l.pagedispy l.pagevh;
363 let gettileopaque l col row =
364 let key = l.pageno, state.gen, conf.colorspace,
365 conf.angle, l.pagew, l.pageh, col, row in
366 try Some (Hashtbl.find state.tilemap key)
367 with Not_found -> None
370 let puttileopaque l col row gen colorspace angle opaque size elapsed =
371 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
372 Hashtbl.add state.tilemap key (opaque, size, elapsed)
375 let drawtiles l color =
376 GlDraw.color color;
377 begintiles ();
378 let f col row x y tilex tiley w h =
379 match gettileopaque l col row with
380 | Some (opaque, _, t) ->
381 let params = x, y, w, h, tilex, tiley in
382 if conf.invert
383 then GlTex.env (`mode `blend);
384 drawtile params opaque;
385 if conf.invert
386 then GlTex.env (`mode `modulate);
387 if conf.debug
388 then (
389 endtiles ();
390 let s = Printf.sprintf "%d[%d,%d] %f sec" l.pageno col row t in
391 let w = measurestr fstate.fontsize s in
392 GlDraw.color (0.0, 0.0, 0.0);
393 filledrect
394 (float (x-2))
395 (float (y-2))
396 (float (x+2) +. w)
397 (float (y + fstate.fontsize + 2));
398 GlDraw.color color;
399 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
400 begintiles ();
403 | None ->
404 endtiles ();
405 let w = let lw = state.winw - x in min lw w
406 and h = let lh = state.winh - y in min lh h
408 if conf.invert
409 then GlTex.env (`mode `blend);
410 begin match !checkerstexid with
411 | Some id ->
412 Gl.enable `texture_2d;
413 GlTex.bind_texture ~target:`texture_2d id;
414 let x0 = float x
415 and y0 = float y
416 and x1 = float (x+w)
417 and y1 = float (y+h) in
419 let tw = float w /. 16.0
420 and th = float h /. 16.0 in
421 let tx0 = float tilex /. 16.0
422 and ty0 = float tiley /. 16.0 in
423 let tx1 = tx0 +. tw
424 and ty1 = ty0 +. th in
425 Raw.sets_float Glutils.vraw ~pos:0
426 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
427 Raw.sets_float Glutils.traw ~pos:0
428 [| tx0; ty0; tx0; ty1; tx1; ty0; tx1; ty1 |];
429 GlArray.vertex `two Glutils.vraw;
430 GlArray.tex_coord `two Glutils.traw;
431 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
432 Gl.disable `texture_2d;
434 | None ->
435 GlDraw.color (1.0, 1.0, 1.0);
436 filledrect (float x) (float y) (float (x+w)) (float (y+h));
437 end;
438 if conf.invert
439 then GlTex.env (`mode `modulate);
440 if w > 128 && h > fstate.fontsize + 10
441 then (
442 let c = if conf.invert then 1.0 else 0.0 in
443 GlDraw.color (c, c, c);
444 let c, r =
445 if conf.verbose
446 then (col*conf.tilew, row*conf.tileh)
447 else col, row
449 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
451 GlDraw.color color;
452 begintiles ();
454 itertiles l f;
455 endtiles ();
458 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
460 let tilevisible1 l x y =
461 let ax0 = l.pagex
462 and ax1 = l.pagex + l.pagevw
463 and ay0 = l.pagey
464 and ay1 = l.pagey + l.pagevh in
466 let bx0 = x
467 and by0 = y in
468 let bx1 = min (bx0 + conf.tilew) l.pagew
469 and by1 = min (by0 + conf.tileh) l.pageh in
471 let rx0 = max ax0 bx0
472 and ry0 = max ay0 by0
473 and rx1 = min ax1 bx1
474 and ry1 = min ay1 by1 in
476 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
477 nonemptyintersection
480 let tilevisible layout n x y =
481 let rec findpageinlayout m = function
482 | l :: rest when l.pageno = n ->
483 tilevisible1 l x y || (
484 match conf.columns with
485 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
486 | Csplit _ | Csingle _ | Cmulti _ -> false
488 | _ :: rest -> findpageinlayout 0 rest
489 | [] -> false
491 findpageinlayout 0 layout;
494 let tileready l x y =
495 tilevisible1 l x y &&
496 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
499 let tilepage n p layout =
500 let rec loop = function
501 | l :: rest ->
502 if l.pageno = n
503 then
504 let f col row _ _ _ _ _ _ =
505 if state.currently = Idle
506 then
507 match gettileopaque l col row with
508 | Some _ -> ()
509 | None ->
510 let x = col*conf.tilew
511 and y = row*conf.tileh in
512 let w =
513 let w = l.pagew - x in
514 min w conf.tilew
516 let h =
517 let h = l.pageh - y in
518 min h conf.tileh
520 let pbo =
521 if conf.usepbo
522 then getpbo w h conf.colorspace
523 else ~< "0"
525 wcmd "tile %s %d %d %d %d %s" (~> p) x y w h (~> pbo);
526 state.currently <-
527 Tiling (
528 l, p, conf.colorspace, conf.angle,
529 state.gen, col, row, conf.tilew, conf.tileh
532 itertiles l f;
533 else
534 loop rest
536 | [] -> ()
538 if nogeomcmds state.geomcmds
539 then loop layout;
542 let preloadlayout x y sw sh =
543 let y = if y < sh then 0 else y - sh in
544 let x = min 0 (x + sw) in
545 let h = sh*3 in
546 let w = sw*3 in
547 layout x y w h;
550 let load pages =
551 let rec loop pages =
552 if state.currently = Idle
553 then
554 match pages with
555 | l :: rest ->
556 begin match getopaque l.pageno with
557 | None ->
558 wcmd "page %d %d" l.pageno l.pagedimno;
559 state.currently <- Loading (l, state.gen);
560 | Some opaque ->
561 tilepage l.pageno opaque pages;
562 loop rest
563 end;
564 | _ -> ()
566 if nogeomcmds state.geomcmds
567 then loop pages
570 let preload pages =
571 load pages;
572 if conf.preload && state.currently = Idle
573 then load (preloadlayout state.x state.y state.winw state.winh);
576 let layoutready layout =
577 let rec fold all ls =
578 all && match ls with
579 | l :: rest ->
580 let seen = ref false in
581 let allvisible = ref true in
582 let foo col row _ _ _ _ _ _ =
583 seen := true;
584 allvisible := !allvisible &&
585 begin match gettileopaque l col row with
586 | Some _ -> true
587 | None -> false
590 itertiles l foo;
591 fold (!seen && !allvisible) rest
592 | [] -> true
594 let alltilesvisible = fold true layout in
595 alltilesvisible;
598 let gotoxy x y =
599 let y = bound y 0 state.maxy in
600 let y, layout =
601 let layout = layout x y state.winw state.winh in
602 postRedisplay "gotoxy ready";
603 y, layout
605 state.x <- x;
606 state.y <- y;
607 state.layout <- layout;
608 begin match state.mode with
609 | LinkNav ln ->
610 begin match ln with
611 | Ltexact (pageno, linkno) ->
612 let rec loop = function
613 | [] ->
614 state.lnava <- Some (pageno, linkno);
615 state.mode <- LinkNav (Ltgendir 0)
616 | l :: _ when l.pageno = pageno ->
617 begin match getopaque pageno with
618 | None -> state.mode <- LinkNav (Ltnotready (pageno, 0))
619 | Some opaque ->
620 let x0, y0, x1, y1 = getlinkrect opaque linkno in
621 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
622 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
623 then state.mode <- LinkNav (Ltgendir 0)
625 | _ :: rest -> loop rest
627 loop layout
628 | Ltnotready _ | Ltgendir _ -> ()
630 | Birdseye _ | Textentry _ | View -> ()
631 end;
632 begin match state.mode with
633 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
634 if not (pagevisible layout pageno)
635 then (
636 match state.layout with
637 | [] -> ()
638 | l :: _ ->
639 state.mode <- Birdseye (conf, leftx, l.pageno, hooverpageno, anchor)
641 | LinkNav lt ->
642 begin match lt with
643 | Ltnotready (_, dir)
644 | Ltgendir dir ->
645 let linknav =
646 let rec loop = function
647 | [] -> lt
648 | l :: rest ->
649 match getopaque l.pageno with
650 | None -> Ltnotready (l.pageno, dir)
651 | Some opaque ->
652 let link =
653 let ld =
654 if dir = 0
655 then LDfirstvisible (l.pagex, l.pagey, dir)
656 else if dir > 0 then LDfirst else LDlast
658 findlink opaque ld
660 match link with
661 | Lnotfound -> loop rest
662 | Lfound n ->
663 showlinktype (getlink opaque n);
664 Ltexact (l.pageno, n)
666 loop state.layout
668 state.mode <- LinkNav linknav
669 | Ltexact _ -> ()
671 | Textentry _ | View -> ()
672 end;
673 preload layout;
674 if conf.updatecurs
675 then (
676 let mx, my = state.mpos in
677 updateunder mx my;
681 let conttiling pageno opaque =
682 tilepage pageno opaque
683 (if conf.preload
684 then preloadlayout state.x state.y state.winw state.winh
685 else state.layout)
688 let gotoxy x y =
689 if not conf.verbose then state.text <- E.s;
690 gotoxy x y;
693 let getanchory (n, top, dtop) =
694 let y, h = getpageyh n in
695 if conf.presentation
696 then
697 let ips = calcips h in
698 y + truncate (top*.float h -. dtop*.float ips) + ips;
699 else y + truncate (top*.float h -. dtop*.float conf.interpagespace)
702 let addnav () = getanchor () |> cbput state.hists.nav;;
703 let addnavnorc () = getanchor () |> cbput_dont_update_rc state.hists.nav;;
705 let getnav dir =
706 let anchor = cbgetc state.hists.nav dir in
707 getanchory anchor;
710 let gotopage n top =
711 let y, h = getpageyh n in
712 let y = y + (truncate (top *. float h)) in
713 gotoxy state.x y
716 let gotopage1 n top =
717 let y = getpagey n in
718 let y = y + top in
719 gotoxy state.x y
722 let invalidate s f =
723 Glutils.redisplay := false;
724 state.layout <- [];
725 state.pdims <- [];
726 state.rects <- [];
727 state.rects1 <- [];
728 match state.geomcmds with
729 | ps, [] when emptystr ps ->
730 f ();
731 state.geomcmds <- s, [];
732 | ps, [] -> state.geomcmds <- ps, [s, f];
733 | ps, (s', _) :: rest when s' = s -> state.geomcmds <- ps, ((s, f) :: rest);
734 | ps, cmds -> state.geomcmds <- ps, ((s, f) :: cmds);
737 let flushpages () =
738 Hashtbl.iter (fun _ opaque -> wcmd "freepage %s" (~> opaque)) state.pagemap;
739 Hashtbl.clear state.pagemap;
742 let flushtiles () =
743 if not (Queue.is_empty state.tilelru)
744 then (
745 Queue.iter (fun (k, p, s) ->
746 wcmd "freetile %s" (~> p);
747 state.memused <- state.memused - s;
748 Hashtbl.remove state.tilemap k;
749 ) state.tilelru;
750 state.uioh#infochanged Memused;
751 Queue.clear state.tilelru;
753 load state.layout;
756 let stateh h =
757 let h = truncate (float h*.conf.zoom) in
758 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
759 h - d
762 let fillhelp () =
763 state.help <-
764 let sl = keystostrlist conf in
765 let rec loop accu =
766 function | [] -> accu
767 | s :: rest -> loop ((s, 0, Noaction) :: accu) rest
768 in Help.makehelp conf.urilauncher
769 @ (("", 0, Noaction) :: loop [] sl) |> Array.of_list
772 let opendoc path password =
773 state.path <- path;
774 state.password <- password;
775 state.gen <- state.gen + 1;
776 state.docinfo <- [];
777 state.outlines <- [||];
779 flushpages ();
780 setaalevel conf.aalevel;
781 setpapercolor conf.papercolor;
782 let titlepath =
783 if emptystr state.origin
784 then path
785 else state.origin
787 Wsi.settitle ("llpp " ^ mbtoutf8 (Filename.basename titlepath));
788 wcmd "open %d %d %s\000%s\000%s\000"
789 (btod conf.usedoccss) !layouth
790 path password conf.css;
791 invalidate "reqlayout"
792 (fun () ->
793 wcmd "reqlayout %d %d %d %s\000"
794 conf.angle (FMTE.to_int conf.fitmodel)
795 (stateh state.winh) state.nameddest
797 fillhelp ();
800 let reload () =
801 state.anchor <- getanchor ();
802 state.reload <- Some (state.x, state.y, now ());
803 opendoc state.path state.password;
806 let scalecolor c = let c = c *. conf.colorscale in (c, c, c);;
807 let scalecolor2 (r, g, b) =
808 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
811 let docolumns columns =
812 match columns with
813 | Csingle _ ->
814 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
815 let rec loop pageno pdimno pdim y ph pdims =
816 if pageno != state.pagecount
817 then
818 let pdimno, ((_, w, h, xoff) as pdim), pdims =
819 match pdims with
820 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
821 pdimno+1, pdim, rest
822 | _ ->
823 pdimno, pdim, pdims
825 let x = max 0 (((state.winw - w) / 2) - xoff) in
826 let y =
827 y + (if conf.presentation
828 then (if pageno = 0 then calcips h else calcips ph + calcips h)
829 else (if pageno = 0 then 0 else conf.interpagespace))
831 a.(pageno) <- (pdimno, x, y, pdim);
832 loop (pageno+1) pdimno pdim (y + h) h pdims
834 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
835 conf.columns <- Csingle a;
837 | Cmulti ((columns, coverA, coverB), _) ->
838 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
839 let rec loop pageno pdimno pdim x y rowh pdims =
840 let rec fixrow m =
841 if m = pageno then () else
842 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
843 if h < rowh
844 then (
845 let y = y + (rowh - h) / 2 in
846 a.(m) <- (pdimno, x, y, pdim);
848 fixrow (m+1)
850 if pageno = state.pagecount
851 then fixrow (((pageno - 1) / columns) * columns)
852 else
853 let pdimno, ((_, w, h, xoff) as pdim), pdims =
854 match pdims with
855 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
856 pdimno+1, pdim, rest
857 | _ -> pdimno, pdim, pdims
859 let x, y, rowh' =
860 if pageno = coverA - 1 || pageno = state.pagecount - coverB
861 then (
862 let x = (state.winw - w) / 2 in
863 let ips =
864 if conf.presentation then calcips h else conf.interpagespace in
865 x, y + ips + rowh, h
867 else (
868 if (pageno - coverA) mod columns = 0
869 then (
870 let x = max 0 (state.winw - state.w) / 2 in
871 let y =
872 if conf.presentation
873 then
874 let ips = calcips h in
875 y + (if pageno = 0 then 0 else calcips rowh + ips)
876 else
877 y + (if pageno = 0 then 0 else conf.interpagespace)
879 x, y + rowh, h
881 else x, y, max rowh h
884 let y =
885 if pageno > 1 && (pageno - coverA) mod columns = 0
886 then (
887 let y =
888 if pageno = columns && conf.presentation
889 then (
890 let ips = calcips rowh in
891 for i = 0 to pred columns
893 let (pdimno, x, y, pdim) = a.(i) in
894 a.(i) <- (pdimno, x, y+ips, pdim)
895 done;
896 y+ips;
898 else y
900 fixrow (pageno - columns);
903 else y
905 a.(pageno) <- (pdimno, x, y, pdim);
906 let x = x + w + xoff*2 + conf.interpagespace in
907 loop (pageno+1) pdimno pdim x y rowh' pdims
909 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
910 conf.columns <- Cmulti ((columns, coverA, coverB), a);
912 | Csplit (c, _) ->
913 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
914 let rec loop pageno pdimno pdim y pdims =
915 if pageno != state.pagecount
916 then
917 let pdimno, ((_, w, h, _) as pdim), pdims =
918 match pdims with
919 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
920 pdimno+1, pdim, rest
921 | _ -> pdimno, pdim, pdims
923 let cw = w / c in
924 let rec loop1 n x y =
925 if n = c then y else (
926 a.(pageno*c + n) <- (pdimno, x, y, pdim);
927 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
930 let y = loop1 0 0 y in
931 loop (pageno+1) pdimno pdim y pdims
933 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
934 conf.columns <- Csplit (c, a);
937 let represent () =
938 docolumns conf.columns;
939 state.maxy <- calcheight ();
940 if state.reprf == noreprf
941 then (
942 match state.mode with
943 | Birdseye (_, _, pageno, _, _) ->
944 let y, h = getpageyh pageno in
945 let top = (state.winh - h) / 2 in
946 gotoxy state.x (max 0 (y - top))
947 | Textentry _ | View | LinkNav _ ->
948 let y = getanchory state.anchor in
949 let y = min y (state.maxy - state.winh) in
950 gotoxy state.x y;
952 else (
953 state.reprf ();
954 state.reprf <- noreprf;
958 let reshape ?(firsttime=false) w h =
959 GlDraw.viewport ~x:0 ~y:0 ~w ~h;
960 if not firsttime && nogeomcmds state.geomcmds
961 then state.anchor <- getanchor ();
963 state.winw <- w;
964 let w = truncate (float w *. conf.zoom) in
965 let w = max w 2 in
966 state.winh <- h;
967 setfontsize fstate.fontsize;
968 GlMat.mode `modelview;
969 GlMat.load_identity ();
971 GlMat.mode `projection;
972 GlMat.load_identity ();
973 GlMat.rotate ~x:1.0 ~angle:180.0 ();
974 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
975 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
977 let relx =
978 if conf.zoom <= 1.0
979 then 0.0
980 else float state.x /. float state.w
982 invalidate "geometry"
983 (fun () ->
984 state.w <- w;
985 if not firsttime
986 then state.x <- truncate (relx *. float w);
987 let w =
988 match conf.columns with
989 | Csingle _ -> w
990 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
991 | Csplit (c, _) -> w * c
993 wcmd "geometry %d %d %d" w (stateh h) (FMTE.to_int conf.fitmodel)
997 let gctiles () =
998 let len = Queue.length state.tilelru in
999 let layout = lazy (if conf.preload
1000 then preloadlayout state.x state.y state.winw state.winh
1001 else state.layout) in
1002 let rec loop qpos =
1003 if state.memused > conf.memlimit
1004 then (
1005 if qpos < len
1006 then
1007 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1008 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1009 let (_, pw, ph, _) = getpagedim n in
1010 if gen = state.gen
1011 && colorspace = conf.colorspace
1012 && angle = conf.angle
1013 && pagew = pw
1014 && pageh = ph
1015 && (
1016 let x = col*conf.tilew and y = row*conf.tileh in
1017 tilevisible (Lazy.force_val layout) n x y
1019 then Queue.push lruitem state.tilelru
1020 else (
1021 freepbo p;
1022 wcmd "freetile %s" (~> p);
1023 state.memused <- state.memused - s;
1024 state.uioh#infochanged Memused;
1025 Hashtbl.remove state.tilemap k;
1027 loop (qpos+1)
1030 loop 0
1033 let onpagerect pageno f =
1034 let b =
1035 match conf.columns with
1036 | Cmulti (_, b) -> b
1037 | Csingle b -> b
1038 | Csplit (_, b) -> b
1040 if pageno >= 0 && pageno < Array.length b
1041 then
1042 let (_, _, _, (_, w, h, _)) = b.(pageno) in
1043 f w h
1046 let gotopagexy1 pageno x y =
1047 let _,w1,h1,leftx = getpagedim pageno in
1048 let top = y /. (float h1) in
1049 let left = x /. (float w1) in
1050 let py, w, h = getpageywh pageno in
1051 let wh = state.winh in
1052 let x = left *. (float w) in
1053 let x = leftx + state.x + truncate x in
1054 let sx =
1055 if x < 0 || x >= state.winw
1056 then state.x - x
1057 else state.x
1059 let pdy = truncate (top *. float h) in
1060 let y' = py + pdy in
1061 let dy = y' - state.y in
1062 let sy =
1063 if x != state.x || not (dy > 0 && dy < wh)
1064 then (
1065 if conf.presentation
1066 then
1067 if abs (py - y') > wh
1068 then y'
1069 else py
1070 else y';
1072 else state.y
1074 if state.x != sx || state.y != sy
1075 then gotoxy sx sy
1076 else gotoxy state.x state.y;
1079 let gotopagexy pageno x y =
1080 match state.mode with
1081 | Birdseye _ -> gotopage pageno 0.0
1082 | Textentry _ | View | LinkNav _ -> gotopagexy1 pageno x y
1085 let getpassword () =
1086 let passcmd = getenvdef "LLPP_ASKPASS" conf.passcmd in
1087 if emptystr passcmd
1088 then E.s
1089 else getcmdoutput (fun s ->
1090 impmsg "error getting password: %s" s;
1091 dolog "%s" s) passcmd;
1094 let pgoto opaque pageno x y =
1095 let pdimno = getpdimno pageno in
1096 let x, y = project opaque pageno pdimno x y in
1097 gotopagexy pageno x y;
1100 let act cmds =
1101 (* dolog "%S" cmds; *)
1102 let spl = splitatchar cmds ' ' in
1103 let scan s fmt f =
1104 try Scanf.sscanf s fmt f
1105 with exn ->
1106 dolog "error processing '%S': %s" cmds @@ exntos exn;
1107 exit 1
1109 let addoutline outline =
1110 match state.currently with
1111 | Outlining outlines -> state.currently <- Outlining (outline :: outlines)
1112 | Idle -> state.currently <- Outlining [outline]
1113 | Loading _ | Tiling _ ->
1114 dolog "invalid outlining state";
1115 logcurrently state.currently
1117 match spl with
1118 | "clear", "" ->
1119 state.pdims <- [];
1120 state.uioh#infochanged Pdim;
1122 | "clearrects", "" ->
1123 state.rects <- state.rects1;
1124 postRedisplay "clearrects";
1126 | "continue", args ->
1127 let n = scan args "%u" (fun n -> n) in
1128 state.pagecount <- n;
1129 begin match state.currently with
1130 | Outlining l ->
1131 state.currently <- Idle;
1132 state.outlines <- Array.of_list (List.rev l)
1133 | Idle | Loading _ | Tiling _ -> ()
1134 end;
1136 let cur, cmds = state.geomcmds in
1137 if emptystr cur then error "empty geomcmd";
1139 begin match List.rev cmds with
1140 | [] ->
1141 state.geomcmds <- E.s, [];
1142 represent ();
1143 | (s, f) :: rest ->
1144 f ();
1145 state.geomcmds <- s, List.rev rest;
1146 end;
1147 postRedisplay "continue";
1149 | "msg", args ->
1150 showtext ' ' args
1152 | "vmsg", args ->
1153 if conf.verbose then showtext ' ' args
1155 | "emsg", args ->
1156 Buffer.add_string state.errmsgs args;
1157 state.newerrmsgs <- true;
1158 postRedisplay "error message"
1160 | "progress", args ->
1161 let progress, text =
1162 scan args "%f %n"
1163 (fun f pos -> f, String.sub args pos (String.length args - pos))
1165 state.text <- text;
1166 state.progress <- progress;
1167 postRedisplay "progress"
1169 | "firstmatch", args ->
1170 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1171 scan args "%u %d %f %f %f %f %f %f %f %f"
1172 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1173 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1175 let y = (getpagey pageno) + truncate y0 in
1176 let x =
1177 if (state.x < - truncate x0) || (state.x > state.winw - truncate x1)
1178 then state.winw/2 - truncate (x0 /. 2. +. x1 /. 2.)
1179 else state.x
1181 addnav ();
1182 gotoxy x y;
1183 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1184 state.rects1 <- [pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)]
1186 | "match", args ->
1187 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1188 scan args "%u %d %f %f %f %f %f %f %f %f"
1189 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1190 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1192 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1193 state.rects1 <-
1194 (pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1196 | "page", args ->
1197 let pageopaques, t = scan args "%s %f" (fun p t -> p, t) in
1198 let pageopaque = ~< pageopaques in
1199 begin match state.currently with
1200 | Loading (l, gen) ->
1201 vlog "page %d took %f sec" l.pageno t;
1202 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1203 let preloadedpages =
1204 if conf.preload
1205 then preloadlayout state.x state.y state.winw state.winh
1206 else state.layout
1208 let evict () =
1209 let set = List.fold_left (fun s l -> IntSet.add l.pageno s)
1210 IntSet.empty preloadedpages
1212 let evictedpages =
1213 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1214 if not (IntSet.mem pageno set)
1215 then (
1216 wcmd "freepage %s" (~> opaque);
1217 key :: accu
1219 else accu
1220 ) state.pagemap []
1222 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1224 evict ();
1225 state.currently <- Idle;
1226 if gen = state.gen
1227 then (
1228 tilepage l.pageno pageopaque state.layout;
1229 load state.layout;
1230 load preloadedpages;
1231 let visible = pagevisible state.layout l.pageno in
1232 if visible
1233 then (
1234 match state.mode with
1235 | LinkNav (Ltnotready (pageno, dir)) ->
1236 if pageno = l.pageno
1237 then (
1238 let link =
1239 let ld =
1240 if dir = 0
1241 then LDfirstvisible (l.pagex, l.pagey, dir)
1242 else if dir > 0 then LDfirst else LDlast
1244 findlink pageopaque ld
1246 match link with
1247 | Lnotfound -> ()
1248 | Lfound n ->
1249 showlinktype (getlink pageopaque n);
1250 state.mode <- LinkNav (Ltexact (l.pageno, n))
1252 | LinkNav (Ltgendir _)
1253 | LinkNav (Ltexact _)
1254 | View
1255 | Birdseye _
1256 | Textentry _ -> ()
1259 if visible && layoutready state.layout
1260 then postRedisplay "page";
1263 | Idle | Tiling _ | Outlining _ ->
1264 dolog "Inconsistent loading state";
1265 logcurrently state.currently;
1266 exit 1
1269 | "tile" , args ->
1270 let (x, y, opaques, size, t) =
1271 scan args "%u %u %s %u %f" (fun x y p size t -> (x, y, p, size, t))
1273 let opaque = ~< opaques in
1274 begin match state.currently with
1275 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1276 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1278 unmappbo opaque;
1279 if tilew != conf.tilew || tileh != conf.tileh
1280 then (
1281 wcmd "freetile %s" (~> opaque);
1282 state.currently <- Idle;
1283 load state.layout;
1285 else (
1286 puttileopaque l col row gen cs angle opaque size t;
1287 state.memused <- state.memused + size;
1288 state.uioh#infochanged Memused;
1289 gctiles ();
1290 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1291 opaque, size) state.tilelru;
1293 state.currently <- Idle;
1294 if gen = state.gen
1295 && conf.colorspace = cs
1296 && conf.angle = angle
1297 && tilevisible state.layout l.pageno x y
1298 then conttiling l.pageno pageopaque;
1300 preload state.layout;
1301 if gen = state.gen
1302 && conf.colorspace = cs
1303 && conf.angle = angle
1304 && tilevisible state.layout l.pageno x y
1305 && layoutready state.layout
1306 then postRedisplay "tile nothrottle";
1309 | Idle | Loading _ | Outlining _ ->
1310 dolog "Inconsistent tiling state";
1311 logcurrently state.currently;
1312 exit 1
1315 | "pdim", args ->
1316 let (n, w, h, _) as pdim =
1317 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1319 let pdim =
1320 match conf.fitmodel with
1321 | FitWidth -> pdim
1322 | FitPage | FitProportional ->
1323 match conf.columns with
1324 | Csplit _ -> (n, w, h, 0)
1325 | Csingle _ | Cmulti _ -> pdim
1327 state.pdims <- pdim :: state.pdims;
1328 state.uioh#infochanged Pdim
1330 | "o", args ->
1331 let (l, n, t, h, pos) =
1332 scan args "%u %u %d %u %n" (fun l n t h pos -> l, n, t, h, pos)
1334 let s = String.sub args pos (String.length args - pos) in
1335 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
1337 | "ou", args ->
1338 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
1339 let s = String.sub args pos len in
1340 let pos2 = pos + len + 1 in
1341 let uri = String.sub args pos2 (String.length args - pos2) in
1342 addoutline (s, l, Ouri uri)
1344 | "on", args ->
1345 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
1346 let s = String.sub args pos (String.length args - pos) in
1347 addoutline (s, l, Onone)
1349 | "a", args ->
1350 let (n, l, t) = scan args "%u %d %d" (fun n l t -> n, l, t) in
1351 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
1353 | "info", args ->
1354 let c, v = splitatchar args '\t' in
1355 let s =
1356 if nonemptystr v
1357 then
1358 if c = "Title"
1359 then (
1360 conf.title <- v;
1361 if not !ignoredoctitlte then Wsi.settitle v;
1362 args
1364 else
1365 if let len = String.length c in
1366 len > 6 && ((String.sub c (len-4) 4) = "date")
1367 then (
1368 if String.length v >= 7 && v.[0] = 'D' && v.[1] = ':'
1369 then
1370 let b = Buffer.create 10 in
1371 Printf.bprintf b "%s\t" c;
1372 let sub p l c =
1374 Buffer.add_substring b v p l;
1375 Buffer.add_char b c;
1376 with exn -> Buffer.add_string b @@ exntos exn
1378 sub 2 4 '/';
1379 sub 6 2 '/';
1380 sub 8 2 ' ';
1381 sub 10 2 ':';
1382 sub 12 2 ':';
1383 sub 14 2 ' ';
1384 Printf.bprintf b "[%s]" v;
1385 Buffer.contents b
1386 else args
1388 else args
1389 else args
1391 state.docinfo <- (1, s) :: state.docinfo
1393 | "infoend", "" ->
1394 state.docinfo <- List.rev state.docinfo;
1395 state.uioh#infochanged Docinfo
1397 | "pass", args ->
1398 if args = "fail"
1399 then Wsi.settitle "Wrong password";
1400 let password = getpassword () in
1401 if emptystr password
1402 then error "document is password protected"
1403 else opendoc state.path password
1405 | _ -> error "unknown cmd `%S'" cmds
1408 let onhist cb =
1409 let rc = cb.rc in
1410 let action = function
1411 | HCprev -> cbget cb ~-1
1412 | HCnext -> cbget cb 1
1413 | HCfirst -> cbget cb ~-(cb.rc)
1414 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1415 and cancel () = cb.rc <- rc
1416 in (action, cancel)
1419 let search pattern forward =
1420 match conf.columns with
1421 | Csplit _ -> impmsg "searching does not work properly in split columns mode"
1422 | Csingle _ | Cmulti _ ->
1423 if nonemptystr pattern
1424 then
1425 let pn, py =
1426 match state.layout with
1427 | [] -> 0, 0
1428 | l :: _ -> l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1430 wcmd "search %d %d %d %d,%s\000"
1431 (btod conf.icase) pn py (btod forward) pattern;
1434 let intentry text key =
1435 let text =
1436 if emptystr text && key = Keys.Ascii '-'
1437 then addchar text '-'
1438 else
1439 match [@warning "-4"] key with
1440 | Keys.Ascii ('0'..'9' as c) -> addchar text c
1441 | _ ->
1442 state.text <- "invalid key";
1443 text
1445 TEcont text
1448 let linknact f s =
1449 if nonemptystr s
1450 then
1451 let n =
1452 let l = String.length s in
1453 let rec loop pos n =
1454 if pos = l
1455 then n
1456 else
1457 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
1458 loop (pos+1) (n*26 + m)
1459 in loop 0 0
1461 let rec loop n = function
1462 | [] -> ()
1463 | l :: rest ->
1464 match getopaque l.pageno with
1465 | None -> loop n rest
1466 | Some opaque ->
1467 let m = getlinkcount opaque in
1468 if n < m
1469 then
1470 let under = getlink opaque n in
1471 f under
1472 else loop (n-m) rest
1474 loop n state.layout;
1477 let linknentry text key = match [@warning "-4"] key with
1478 | Keys.Ascii ('a' .. 'z' as c) ->
1479 let text = addchar text c in
1480 linknact (fun under -> state.text <- undertext under) text;
1481 TEcont text
1482 | _ ->
1483 state.text <- Printf.sprintf "invalid key %s" @@ Keys.to_string key;
1484 TEcont text
1487 let textentry text key = match [@warning "-4"] key with
1488 | Keys.Ascii c -> TEcont (addchar text c)
1489 | Keys.Code c -> TEcont (text ^ toutf8 c)
1490 | _ -> TEcont text
1493 let reqlayout angle fitmodel =
1494 if nogeomcmds state.geomcmds
1495 then state.anchor <- getanchor ();
1496 conf.angle <- angle mod 360;
1497 if conf.angle != 0
1498 then (
1499 match state.mode with
1500 | LinkNav _ -> state.mode <- View
1501 | Birdseye _ | Textentry _ | View -> ()
1503 conf.fitmodel <- fitmodel;
1504 invalidate "reqlayout"
1505 (fun () -> wcmd "reqlayout %d %d %d"
1506 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh));
1509 let settrim trimmargins trimfuzz =
1510 if nogeomcmds state.geomcmds
1511 then state.anchor <- getanchor ();
1512 conf.trimmargins <- trimmargins;
1513 conf.trimfuzz <- trimfuzz;
1514 let x0, y0, x1, y1 = trimfuzz in
1515 invalidate "settrim"
1516 (fun () -> wcmd "settrim %d %d %d %d %d"
1517 (btod conf.trimmargins) x0 y0 x1 y1);
1518 flushpages ();
1521 let setzoom zoom =
1522 let zoom = max 0.0001 zoom in
1523 if zoom <> conf.zoom
1524 then (
1525 state.prevzoom <- (conf.zoom, state.x);
1526 conf.zoom <- zoom;
1527 reshape state.winw state.winh;
1528 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
1532 let pivotzoom ?(vw=min state.w state.winw)
1533 ?(vh=min (state.maxy-state.y) state.winh)
1534 ?(x=vw/2) ?(y=vh/2) zoom =
1535 let w = float state.w /. zoom in
1536 let hw = w /. 2.0 in
1537 let ratio = float vh /. float vw in
1538 let hh = hw *. ratio in
1539 let x0 = float x -. hw
1540 and y0 = float y -. hh in
1541 gotoxy (state.x - truncate x0) (state.y + truncate y0);
1542 setzoom zoom;
1545 let pivotzoom ?vw ?vh ?x ?y zoom =
1546 if nogeomcmds state.geomcmds
1547 then
1548 if zoom > 1.0
1549 then pivotzoom ?vw ?vh ?x ?y zoom
1550 else setzoom zoom
1553 let setcolumns mode columns coverA coverB =
1554 state.prevcolumns <- Some (conf.columns, conf.zoom);
1555 if columns < 0
1556 then (
1557 if isbirdseye mode
1558 then impmsg "split mode doesn't work in bird's eye"
1559 else (
1560 conf.columns <- Csplit (-columns, E.a);
1561 state.x <- 0;
1562 conf.zoom <- 1.0;
1565 else (
1566 if columns < 2
1567 then (
1568 conf.columns <- Csingle E.a;
1569 state.x <- 0;
1570 setzoom 1.0;
1572 else (
1573 conf.columns <- Cmulti ((columns, coverA, coverB), E.a);
1574 conf.zoom <- 1.0;
1577 reshape state.winw state.winh;
1580 let resetmstate () =
1581 state.mstate <- Mnone;
1582 Wsi.setcursor Wsi.CURSOR_INHERIT;
1585 let enterbirdseye () =
1586 let zoom = float conf.thumbw /. float state.winw in
1587 let birdseyepageno =
1588 let cy = state.winh / 2 in
1589 let fold = function
1590 | [] -> 0
1591 | l :: rest ->
1592 let rec fold best = function
1593 | [] -> best.pageno
1594 | l :: rest ->
1595 let d = cy - (l.pagedispy + l.pagevh/2)
1596 and dbest = cy - (best.pagedispy + best.pagevh/2) in
1597 if abs d < abs dbest
1598 then fold l rest
1599 else best.pageno
1600 in fold l rest
1602 fold state.layout
1604 state.mode <-
1605 Birdseye (
1606 { conf with zoom = conf.zoom },
1607 state.x, birdseyepageno, -1, getanchor ()
1609 resetmstate ();
1610 conf.zoom <- zoom;
1611 conf.presentation <- false;
1612 conf.interpagespace <- 10;
1613 conf.hlinks <- false;
1614 conf.fitmodel <- FitPage;
1615 state.x <- 0;
1616 conf.columns <- (
1617 match conf.beyecolumns with
1618 | Some c ->
1619 conf.zoom <- 1.0;
1620 Cmulti ((c, 0, 0), E.a)
1621 | None -> Csingle E.a
1623 if conf.verbose
1624 then state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1625 (100.0*.zoom)
1626 else state.text <- E.s;
1627 reshape state.winw state.winh;
1630 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
1631 state.mode <- View;
1632 conf.zoom <- c.zoom;
1633 conf.presentation <- c.presentation;
1634 conf.interpagespace <- c.interpagespace;
1635 conf.hlinks <- c.hlinks;
1636 conf.fitmodel <- c.fitmodel;
1637 conf.beyecolumns <- (
1638 match conf.columns with
1639 | Cmulti ((c, _, _), _) -> Some c
1640 | Csingle _ -> None
1641 | Csplit _ -> error "leaving bird's eye split mode"
1643 conf.columns <- (
1644 match c.columns with
1645 | Cmulti (c, _) -> Cmulti (c, E.a)
1646 | Csingle _ -> Csingle E.a
1647 | Csplit (c, _) -> Csplit (c, E.a)
1649 if conf.verbose
1650 then state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1651 (100.0*.conf.zoom);
1652 reshape state.winw state.winh;
1653 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
1654 state.x <- leftx;
1657 let togglebirdseye () =
1658 match state.mode with
1659 | Birdseye vals -> leavebirdseye vals true
1660 | View -> enterbirdseye ()
1661 | Textentry _ | LinkNav _ -> ()
1664 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
1665 let pageno = max 0 (pageno - incr) in
1666 let rec loop = function
1667 | [] -> gotopage1 pageno 0
1668 | l :: _ when l.pageno = pageno ->
1669 if l.pagedispy >= 0 && l.pagey = 0
1670 then postRedisplay "upbirdseye"
1671 else gotopage1 pageno 0
1672 | _ :: rest -> loop rest
1674 loop state.layout;
1675 state.text <- E.s;
1676 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
1679 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
1680 let pageno = min (state.pagecount - 1) (pageno + incr) in
1681 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
1682 let rec loop = function
1683 | [] ->
1684 let y, h = getpageyh pageno in
1685 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
1686 gotoxy state.x (clamp dy)
1687 | l :: _ when l.pageno = pageno ->
1688 if l.pagevh != l.pageh
1689 then gotoxy state.x (clamp (l.pageh - l.pagevh + conf.interpagespace))
1690 else postRedisplay "downbirdseye"
1691 | _ :: rest -> loop rest
1693 loop state.layout;
1694 state.text <- E.s;
1697 let optentry mode _ key =
1698 let btos b = if b then "on" else "off" in
1699 match [@warning "-4"] key with
1700 | Keys.Ascii 'C' ->
1701 let ondone s =
1703 let n, a, b = multicolumns_of_string s in
1704 setcolumns mode n a b;
1705 with exn ->
1706 state.text <- Printf.sprintf "bad columns `%s': %s" s @@ exntos exn
1708 TEswitch ("columns: ", E.s, None, textentry, ondone, true)
1710 | Keys.Ascii 'Z' ->
1711 let ondone s =
1713 let zoom = float (int_of_string s) /. 100.0 in
1714 pivotzoom zoom
1715 with exn ->
1716 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
1718 TEswitch ("zoom: ", E.s, None, intentry, ondone, true)
1720 | Keys.Ascii 'i' ->
1721 conf.icase <- not conf.icase;
1722 TEdone ("case insensitive search " ^ (btos conf.icase))
1724 | Keys.Ascii 'v' ->
1725 conf.verbose <- not conf.verbose;
1726 TEdone ("verbose " ^ (btos conf.verbose))
1728 | Keys.Ascii 'd' ->
1729 conf.debug <- not conf.debug;
1730 TEdone ("debug " ^ (btos conf.debug))
1732 | Keys.Ascii 'f' ->
1733 conf.underinfo <- not conf.underinfo;
1734 TEdone ("underinfo " ^ btos conf.underinfo)
1736 | Keys.Ascii 'T' ->
1737 settrim (not conf.trimmargins) conf.trimfuzz;
1738 TEdone ("trim margins " ^ btos conf.trimmargins)
1740 | Keys.Ascii 'I' ->
1741 conf.invert <- not conf.invert;
1742 TEdone ("invert colors " ^ btos conf.invert)
1744 | Keys.Ascii 'x' ->
1745 let ondone s =
1746 cbput state.hists.sel s;
1747 conf.selcmd <- s;
1749 TEswitch ("selection command: ", E.s, Some (onhist state.hists.sel),
1750 textentry, ondone, true)
1752 | Keys.Ascii 'M' ->
1753 if conf.pax == None
1754 then conf.pax <- Some 0.0
1755 else conf.pax <- None;
1756 TEdone ("PAX " ^ btos (conf.pax != None))
1758 | (Keys.Ascii c) ->
1759 state.text <- Printf.sprintf "bad option %d `%c'" (Char.code c) c;
1760 TEstop
1762 | _ -> TEcont state.text
1765 let adderrmsg src msg =
1766 Buffer.add_string state.errmsgs msg;
1767 state.newerrmsgs <- true;
1768 postRedisplay src
1771 let adderrfmt src fmt = Format.ksprintf (fun s -> adderrmsg src s) fmt;;
1773 class outlinelistview ~zebra ~source =
1774 let settext autonarrow s =
1775 if autonarrow
1776 then
1777 let ss = source#statestr in
1778 state.text <- if emptystr ss
1779 then "[" ^ s ^ "]"
1780 else "{" ^ ss ^ "} [" ^ s ^ "]"
1781 else state.text <- s
1783 object (self)
1784 inherit listview
1785 ~zebra
1786 ~helpmode:false
1787 ~source:(source :> lvsource)
1788 ~trusted:false
1789 ~modehash:(findkeyhash conf "outline")
1790 as super
1792 val m_autonarrow = false
1794 method! key key mask =
1795 let maxrows =
1796 if emptystr state.text
1797 then fstate.maxrows
1798 else fstate.maxrows - 2
1800 let calcfirst first active =
1801 if active > first
1802 then
1803 let rows = active - first in
1804 if rows > maxrows then active - maxrows else first
1805 else active
1807 let navigate incr =
1808 let active = m_active + incr in
1809 let active = bound active 0 (source#getitemcount - 1) in
1810 let first = calcfirst m_first active in
1811 postRedisplay "outline navigate";
1812 coe {< m_active = active; m_first = first >}
1814 let navscroll first =
1815 let active =
1816 let dist = m_active - first in
1817 if dist < 0
1818 then first
1819 else (
1820 if dist < maxrows
1821 then m_active
1822 else first + maxrows
1825 postRedisplay "outline navscroll";
1826 coe {< m_first = first; m_active = active >}
1828 let ctrl = Wsi.withctrl mask in
1829 let open Keys in
1830 match Wsi.kc2kt key with
1831 | Ascii 'a' when ctrl ->
1832 let text =
1833 if m_autonarrow
1834 then (
1835 source#denarrow;
1838 else (
1839 let pattern = source#renarrow in
1840 if nonemptystr m_qsearch
1841 then (source#narrow m_qsearch; m_qsearch)
1842 else pattern
1845 settext (not m_autonarrow) text;
1846 postRedisplay "toggle auto narrowing";
1847 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
1849 | Ascii '/' when emptystr m_qsearch && not m_autonarrow ->
1850 settext true E.s;
1851 postRedisplay "toggle auto narrowing";
1852 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
1854 | Ascii 'n' when ctrl ->
1855 source#narrow m_qsearch;
1856 if not m_autonarrow
1857 then source#add_narrow_pattern m_qsearch;
1858 postRedisplay "outline ctrl-n";
1859 coe {< m_first = 0; m_active = 0 >}
1861 | Ascii 'S' when ctrl ->
1862 let active = source#calcactive (getanchor ()) in
1863 let first = firstof m_first active in
1864 postRedisplay "outline ctrl-s";
1865 coe {< m_first = first; m_active = active >}
1867 | Ascii 'u' when ctrl ->
1868 postRedisplay "outline ctrl-u";
1869 if m_autonarrow && nonemptystr m_qsearch
1870 then (
1871 ignore (source#renarrow);
1872 settext m_autonarrow E.s;
1873 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
1875 else (
1876 source#del_narrow_pattern;
1877 let pattern = source#renarrow in
1878 let text =
1879 if emptystr pattern then E.s else "Narrowed to " ^ pattern
1881 settext m_autonarrow text;
1882 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
1885 | Ascii 'l' when ctrl ->
1886 let first = max 0 (m_active - (fstate.maxrows / 2)) in
1887 postRedisplay "outline ctrl-l";
1888 coe {< m_first = first >}
1890 | Ascii '\t' when m_autonarrow ->
1891 if nonemptystr m_qsearch
1892 then (
1893 postRedisplay "outline list view tab";
1894 source#add_narrow_pattern m_qsearch;
1895 settext true E.s;
1896 coe {< m_qsearch = E.s >}
1898 else coe self
1900 | Escape when m_autonarrow ->
1901 if nonemptystr m_qsearch
1902 then source#add_narrow_pattern m_qsearch;
1903 super#key key mask
1905 | Enter when m_autonarrow ->
1906 if nonemptystr m_qsearch
1907 then source#add_narrow_pattern m_qsearch;
1908 super#key key mask
1910 | (Ascii _ | Code _) when m_autonarrow ->
1911 let pattern = m_qsearch ^ toutf8 key in
1912 postRedisplay "outlinelistview autonarrow add";
1913 source#narrow pattern;
1914 settext true pattern;
1915 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
1917 | Backspace when m_autonarrow ->
1918 if emptystr m_qsearch
1919 then coe self
1920 else
1921 let pattern = withoutlastutf8 m_qsearch in
1922 postRedisplay "outlinelistview autonarrow backspace";
1923 ignore (source#renarrow);
1924 source#narrow pattern;
1925 settext true pattern;
1926 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
1928 | Up when ctrl -> navscroll (max 0 (m_first - 1))
1930 | Down when ctrl ->
1931 navscroll (min (source#getitemcount - 1) (m_first + 1))
1933 | Up -> navigate ~-1
1934 | Down -> navigate 1
1935 | Prior -> navigate ~-(fstate.maxrows)
1936 | Next -> navigate fstate.maxrows
1938 | Right ->
1939 let o =
1940 if ctrl
1941 then (
1942 postRedisplay "outline ctrl right";
1943 {< m_pan = m_pan + 1 >}
1945 else self#updownlevel 1
1947 coe o
1949 | Left ->
1950 let o =
1951 if ctrl
1952 then (
1953 postRedisplay "outline ctrl left";
1954 {< m_pan = m_pan - 1 >}
1956 else self#updownlevel ~-1
1958 coe o
1960 | Home ->
1961 postRedisplay "outline home";
1962 coe {< m_first = 0; m_active = 0 >}
1964 | End ->
1965 let active = source#getitemcount - 1 in
1966 let first = max 0 (active - fstate.maxrows) in
1967 postRedisplay "outline end";
1968 coe {< m_active = active; m_first = first >}
1970 | Delete|Escape|Insert|Enter|Ascii _|Code _|Ctrl _|Backspace|Fn _ ->
1971 super#key key mask
1972 end;;
1974 let genhistoutlines () =
1975 Config.gethist ()
1976 |> List.sort (fun (_, c1, _, _, _, _) (_, c2, _, _, _, _) ->
1977 compare c2.lastvisit c1.lastvisit)
1978 |> List.map (fun ((path, c, _, _, _, origin) as hist) ->
1979 let path = if nonemptystr origin then origin else path in
1980 let base = mbtoutf8 @@ Filename.basename path in
1981 (base ^ "\000" ^ c.title, 1, Ohistory hist)
1985 let gotohist (path, c, bookmarks, x, anchor, origin) =
1986 Config.save leavebirdseye;
1987 state.anchor <- anchor;
1988 state.bookmarks <- bookmarks;
1989 state.origin <- origin;
1990 state.x <- x;
1991 setconf conf c;
1992 let x0, y0, x1, y1 = conf.trimfuzz in
1993 wcmd "trimset %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1;
1994 reshape ~firsttime:true state.winw state.winh;
1995 opendoc path origin;
1996 setzoom c.zoom;
1999 let setcheckers enabled =
2000 match !checkerstexid with
2001 | None -> if enabled then checkerstexid := Some (makecheckers ())
2002 | Some id ->
2003 if not enabled
2004 then (
2005 GlTex.delete_texture id;
2006 checkerstexid := None;
2010 let describe_layout layout =
2011 let d =
2012 match layout with
2013 | [] -> "Page 0"
2014 | l :: [] -> Printf.sprintf "Page %d" (l.pageno+1)
2015 | l :: rest ->
2016 let rangestr a b =
2017 if a.pageno = b.pageno then Printf.sprintf "%d" (a.pageno+1)
2018 else Printf.sprintf "%d%s%d" (a.pageno+1)
2019 (if a.pageno+1 = b.pageno then ", " else Utf8syms.ellipsis)
2020 (b.pageno+1)
2022 let rec fold s la lb = function
2023 | [] -> Printf.sprintf "%s %s" s (rangestr la lb)
2024 | l :: rest when l.pageno = succ lb.pageno -> fold s la l rest
2025 | l :: rest -> fold (s ^ " " ^ rangestr la lb ^ ",") l l rest
2027 fold "Pages" l l rest
2029 let percent =
2030 let maxy = maxy () in
2031 if maxy <= 0
2032 then 100.
2033 else 100. *. (float state.y /. float maxy)
2035 Printf.sprintf "%s of %d [%.2f%%]" d state.pagecount percent
2038 let setpresentationmode v =
2039 let n = page_of_y state.y in
2040 state.anchor <- (n, 0.0, 1.0);
2041 conf.presentation <- v;
2042 if conf.fitmodel = FitPage
2043 then reqlayout conf.angle conf.fitmodel;
2044 represent ();
2047 let enterinfomode =
2048 let btos b = if b then Utf8syms.radical else E.s in
2049 let showextended = ref false in
2050 let showcolors = ref false in
2051 let leave mode _ = state.mode <- mode in
2052 let src =
2053 (object
2054 val mutable m_l = []
2055 val mutable m_a = E.a
2056 val mutable m_prev_uioh = nouioh
2057 val mutable m_prev_mode = View
2059 inherit lvsourcebase
2061 method reset prev_mode prev_uioh =
2062 m_a <- Array.of_list (List.rev m_l);
2063 m_l <- [];
2064 m_prev_mode <- prev_mode;
2065 m_prev_uioh <- prev_uioh;
2067 method int name get set =
2068 m_l <-
2069 (name, `int get, 1,
2070 Action (
2071 fun u ->
2072 let ondone s =
2073 try set (int_of_string s)
2074 with exn ->
2075 state.text <- Printf.sprintf "bad integer `%s': %s"
2076 s @@ exntos exn
2078 state.text <- E.s;
2079 let te = name ^ ": ", E.s, None, intentry, ondone, true in
2080 state.mode <- Textentry (te, leave m_prev_mode);
2082 )) :: m_l
2084 method int_with_suffix name get set =
2085 m_l <-
2086 (name, `intws get, 1,
2087 Action (
2088 fun u ->
2089 let ondone s =
2090 try set (int_of_string_with_suffix s)
2091 with exn ->
2092 state.text <- Printf.sprintf "bad integer `%s': %s"
2093 s @@ exntos exn
2095 state.text <- E.s;
2096 let te =
2097 name ^ ": ", E.s, None, intentry_with_suffix, ondone, true
2099 state.mode <- Textentry (te, leave m_prev_mode);
2101 )) :: m_l
2103 method bool ?(offset=1) ?(btos=btos) name get set =
2104 m_l <-
2105 (name, `bool (btos, get), offset, Action (
2106 fun u ->
2107 let v = get () in
2108 set (not v);
2110 )) :: m_l
2112 method color name get set =
2113 m_l <-
2114 (name, `color get, 1,
2115 Action (
2116 fun u ->
2117 let invalid = (nan, nan, nan) in
2118 let ondone s =
2119 let c =
2120 try color_of_string s
2121 with exn ->
2122 state.text <- Printf.sprintf "bad color `%s': %s"
2123 s @@ exntos exn;
2124 invalid
2126 if c <> invalid
2127 then set c;
2129 let te = name ^ ": ", E.s, None, textentry, ondone, true in
2130 state.text <- color_to_string (get ());
2131 state.mode <- Textentry (te, leave m_prev_mode);
2133 )) :: m_l
2135 method string name get set =
2136 m_l <-
2137 (name, `string get, 1,
2138 Action (
2139 fun u ->
2140 let ondone s = set s in
2141 let te = name ^ ": ", E.s, None, textentry, ondone, true in
2142 state.mode <- Textentry (te, leave m_prev_mode);
2144 )) :: m_l
2146 method colorspace name get set =
2147 m_l <-
2148 (name, `string get, 1,
2149 Action (
2150 fun _ ->
2151 let source =
2152 (object
2153 inherit lvsourcebase
2155 initializer
2156 m_active <- CSTE.to_int conf.colorspace;
2157 m_first <- 0;
2159 method getitemcount =
2160 Array.length CSTE.names
2161 method getitem n =
2162 (CSTE.names.(n), 0)
2163 method exit ~uioh ~cancel ~active ~first ~pan =
2164 ignore (uioh, first, pan);
2165 if not cancel then set active;
2166 None
2167 method hasaction _ = true
2168 end)
2170 state.text <- E.s;
2171 let modehash = findkeyhash conf "info" in
2172 coe (new listview ~zebra:false ~helpmode:false
2173 ~source ~trusted:true ~modehash)
2174 )) :: m_l
2176 method paxmark name get set =
2177 m_l <-
2178 (name, `string get, 1,
2179 Action (
2180 fun _ ->
2181 let source =
2182 (object
2183 inherit lvsourcebase
2185 initializer
2186 m_active <- MTE.to_int conf.paxmark;
2187 m_first <- 0;
2189 method getitemcount = Array.length MTE.names
2190 method getitem n = (MTE.names.(n), 0)
2191 method exit ~uioh ~cancel ~active ~first ~pan =
2192 ignore (uioh, first, pan);
2193 if not cancel then set active;
2194 None
2195 method hasaction _ = true
2196 end)
2198 state.text <- E.s;
2199 let modehash = findkeyhash conf "info" in
2200 coe (new listview ~zebra:false ~helpmode:false
2201 ~source ~trusted:true ~modehash)
2202 )) :: m_l
2204 method fitmodel name get set =
2205 m_l <-
2206 (name, `string get, 1,
2207 Action (
2208 fun _ ->
2209 let source =
2210 (object
2211 inherit lvsourcebase
2213 initializer
2214 m_active <- FMTE.to_int conf.fitmodel;
2215 m_first <- 0;
2217 method getitemcount = Array.length FMTE.names
2218 method getitem n = (FMTE.names.(n), 0)
2219 method exit ~uioh ~cancel ~active ~first ~pan =
2220 ignore (uioh, first, pan);
2221 if not cancel then set active;
2222 None
2223 method hasaction _ = true
2224 end)
2226 state.text <- E.s;
2227 let modehash = findkeyhash conf "info" in
2228 coe (new listview ~zebra:false ~helpmode:false
2229 ~source ~trusted:true ~modehash)
2230 )) :: m_l
2232 method caption s offset =
2233 m_l <- (s, `empty, offset, Noaction) :: m_l
2235 method caption2 s f offset =
2236 m_l <- (s, `string f, offset, Noaction) :: m_l
2238 method getitemcount = Array.length m_a
2240 method getitem n =
2241 let tostr = function
2242 | `int f -> string_of_int (f ())
2243 | `intws f -> string_with_suffix_of_int (f ())
2244 | `string f -> f ()
2245 | `color f -> color_to_string (f ())
2246 | `bool (btos, f) -> btos (f ())
2247 | `empty -> E.s
2249 let name, t, offset, _ = m_a.(n) in
2250 ((let s = tostr t in
2251 if nonemptystr s
2252 then Printf.sprintf "%s\t%s" name s
2253 else name),
2254 offset)
2256 method exit ~uioh ~cancel ~active ~first ~pan =
2257 let uiohopt =
2258 if not cancel
2259 then (
2260 let uioh =
2261 match m_a.(active) with
2262 | _, _, _, Action f -> f uioh
2263 | _, _, _, Noaction -> uioh
2265 Some uioh
2267 else None
2269 m_active <- active;
2270 m_first <- first;
2271 m_pan <- pan;
2272 uiohopt
2274 method hasaction n =
2275 match m_a.(n) with
2276 | _, _, _, Action _ -> true
2277 | _, _, _, Noaction -> false
2279 initializer m_active <- 1
2280 end)
2282 let rec fillsrc prevmode prevuioh =
2283 let sep () = src#caption E.s 0 in
2284 let colorp name get set =
2285 src#string name
2286 (fun () -> color_to_string (get ()))
2287 (fun v ->
2288 try set @@ color_of_string v
2289 with exn ->
2290 state.text <-
2291 Printf.sprintf "bad color `%s': %s" v @@ exntos exn
2294 let rgba name get set =
2295 src#string name
2296 (fun () -> get () |> rgba_to_string)
2297 (fun v ->
2298 try set @@ rgba_of_string v
2299 with exn ->
2300 state.text <-
2301 Printf.sprintf "bad color `%s': %s" v @@ exntos exn
2304 let oldmode = state.mode in
2305 let birdseye = isbirdseye state.mode in
2307 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
2309 src#bool "presentation mode"
2310 (fun () -> conf.presentation)
2311 (fun v -> setpresentationmode v);
2313 src#bool "ignore case in searches"
2314 (fun () -> conf.icase)
2315 (fun v -> conf.icase <- v);
2317 src#bool "preload"
2318 (fun () -> conf.preload)
2319 (fun v -> conf.preload <- v);
2321 src#bool "highlight links"
2322 (fun () -> conf.hlinks)
2323 (fun v -> conf.hlinks <- v);
2325 src#bool "under info"
2326 (fun () -> conf.underinfo)
2327 (fun v -> conf.underinfo <- v);
2329 src#fitmodel "fit model"
2330 (fun () -> FMTE.to_string conf.fitmodel)
2331 (fun v -> reqlayout conf.angle (FMTE.of_int v));
2333 src#bool "trim margins"
2334 (fun () -> conf.trimmargins)
2335 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
2337 sep ();
2338 src#int "inter-page space"
2339 (fun () -> conf.interpagespace)
2340 (fun n ->
2341 conf.interpagespace <- n;
2342 docolumns conf.columns;
2343 let pageno, py =
2344 match state.layout with
2345 | [] -> 0, 0
2346 | l :: _ -> l.pageno, l.pagey
2348 state.maxy <- calcheight ();
2349 let y = getpagey pageno in
2350 gotoxy state.x (y + py)
2353 src#int "page bias"
2354 (fun () -> conf.pagebias)
2355 (fun v -> conf.pagebias <- v);
2357 src#int "scroll step"
2358 (fun () -> conf.scrollstep)
2359 (fun n -> conf.scrollstep <- n);
2361 src#int "horizontal scroll step"
2362 (fun () -> conf.hscrollstep)
2363 (fun v -> conf.hscrollstep <- v);
2365 src#int "auto scroll step"
2366 (fun () ->
2367 match state.autoscroll with
2368 | Some step -> step
2369 | _ -> conf.autoscrollstep)
2370 (fun n ->
2371 let n = boundastep state.winh n in
2372 if state.autoscroll <> None
2373 then state.autoscroll <- Some n;
2374 conf.autoscrollstep <- n);
2376 src#int "zoom"
2377 (fun () -> truncate (conf.zoom *. 100.))
2378 (fun v -> pivotzoom ((float v) /. 100.));
2380 src#int "rotation"
2381 (fun () -> conf.angle)
2382 (fun v -> reqlayout v conf.fitmodel);
2384 src#int "scroll bar width"
2385 (fun () -> conf.scrollbw)
2386 (fun v ->
2387 conf.scrollbw <- v;
2388 reshape state.winw state.winh;
2391 src#int "scroll handle height"
2392 (fun () -> conf.scrollh)
2393 (fun v -> conf.scrollh <- v;);
2395 src#int "thumbnail width"
2396 (fun () -> conf.thumbw)
2397 (fun v ->
2398 conf.thumbw <- min 4096 v;
2399 match oldmode with
2400 | Birdseye beye ->
2401 leavebirdseye beye false;
2402 enterbirdseye ()
2403 | Textentry _
2404 | View
2405 | LinkNav _ -> ()
2408 let mode = state.mode in
2409 src#string "columns"
2410 (fun () ->
2411 match conf.columns with
2412 | Csingle _ -> "1"
2413 | Cmulti (multi, _) -> multicolumns_to_string multi
2414 | Csplit (count, _) -> "-" ^ string_of_int count
2416 (fun v ->
2417 let n, a, b = multicolumns_of_string v in
2418 setcolumns mode n a b);
2420 sep ();
2421 src#caption "Pixmap cache" 0;
2422 src#int_with_suffix "size (advisory)"
2423 (fun () -> conf.memlimit)
2424 (fun v -> conf.memlimit <- v);
2426 src#caption2 "used"
2427 (fun () ->
2428 Printf.sprintf "%s bytes, %d tiles"
2429 (string_with_suffix_of_int state.memused)
2430 (Hashtbl.length state.tilemap)) 1;
2432 sep ();
2433 src#caption "Layout" 0;
2434 src#caption2 "Dimension"
2435 (fun () -> Printf.sprintf "%dx%d (virtual %dx%d)"
2436 state.winw state.winh
2437 state.w state.maxy)
2439 if conf.debug
2440 then src#caption2 "Position" (fun () ->
2441 Printf.sprintf "%dx%d" state.x state.y
2443 else src#caption2 "Position" (fun () -> describe_layout state.layout) 1;
2445 sep ();
2446 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
2447 "Save these parameters as global defaults at exit"
2448 (fun () -> conf.bedefault)
2449 (fun v -> conf.bedefault <- v);
2451 sep ();
2452 let btos b = Utf8syms.(if b then lguillemet else rguillemet) in
2453 src#bool ~offset:0 ~btos "Extended parameters"
2454 (fun () -> !showextended)
2455 (fun v -> showextended := v; fillsrc prevmode prevuioh);
2456 if !showextended
2457 then (
2458 src#bool "checkers"
2459 (fun () -> conf.checkers)
2460 (fun v -> conf.checkers <- v; setcheckers v);
2461 src#bool "update cursor"
2462 (fun () -> conf.updatecurs)
2463 (fun v -> conf.updatecurs <- v);
2464 src#bool "scroll-bar on the left"
2465 (fun () -> conf.leftscroll)
2466 (fun v -> conf.leftscroll <- v);
2467 src#bool "verbose"
2468 (fun () -> conf.verbose)
2469 (fun v -> conf.verbose <- v);
2470 src#bool "invert colors"
2471 (fun () -> conf.invert)
2472 (fun v -> conf.invert <- v);
2473 src#bool "max fit"
2474 (fun () -> conf.maxhfit)
2475 (fun v -> conf.maxhfit <- v);
2476 src#bool "pax mode"
2477 (fun () -> conf.pax != None)
2478 (fun v ->
2479 if v
2480 then conf.pax <- Some (now ())
2481 else conf.pax <- None);
2482 src#string "uri launcher"
2483 (fun () -> conf.urilauncher)
2484 (fun v -> conf.urilauncher <- v);
2485 src#string "path launcher"
2486 (fun () -> conf.pathlauncher)
2487 (fun v -> conf.pathlauncher <- v);
2488 src#string "tile size"
2489 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
2490 (fun v ->
2492 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
2493 conf.tilew <- max 64 w;
2494 conf.tileh <- max 64 h;
2495 flushtiles ();
2496 with exn ->
2497 state.text <- Printf.sprintf "bad tile size `%s': %s"
2498 v @@ exntos exn
2500 src#int "texture count"
2501 (fun () -> conf.texcount)
2502 (fun v ->
2503 if realloctexts v
2504 then conf.texcount <- v
2505 else impmsg "failed to set texture count please retry later"
2507 src#int "slice height"
2508 (fun () -> conf.sliceheight)
2509 (fun v ->
2510 conf.sliceheight <- v;
2511 wcmd "sliceh %d" conf.sliceheight;
2513 src#int "anti-aliasing level"
2514 (fun () -> conf.aalevel)
2515 (fun v ->
2516 conf.aalevel <- bound v 0 8;
2517 state.anchor <- getanchor ();
2518 opendoc state.path state.password;
2520 src#string "page scroll scaling factor"
2521 (fun () -> string_of_float conf.pgscale)
2522 (fun v ->
2523 try conf.pgscale <- float_of_string v
2524 with exn ->
2525 state.text <-
2526 Printf.sprintf "bad page scroll scaling factor `%s': %s" v
2527 @@ exntos exn
2529 src#int "ui font size"
2530 (fun () -> fstate.fontsize)
2531 (fun v -> setfontsize (bound v 5 100));
2532 src#int "hint font size"
2533 (fun () -> conf.hfsize)
2534 (fun v -> conf.hfsize <- bound v 5 100);
2535 src#string "trim fuzz"
2536 (fun () -> irect_to_string conf.trimfuzz)
2537 (fun v ->
2539 conf.trimfuzz <- irect_of_string v;
2540 if conf.trimmargins
2541 then settrim true conf.trimfuzz;
2542 with exn ->
2543 state.text <- Printf.sprintf "bad irect `%s': %s" v
2544 @@ exntos exn
2546 src#string "selection command"
2547 (fun () -> conf.selcmd)
2548 (fun v -> conf.selcmd <- v);
2549 src#string "synctex command"
2550 (fun () -> conf.stcmd)
2551 (fun v -> conf.stcmd <- v);
2552 src#string "pax command"
2553 (fun () -> conf.paxcmd)
2554 (fun v -> conf.paxcmd <- v);
2555 src#string "ask password command"
2556 (fun () -> conf.passcmd)
2557 (fun v -> conf.passcmd <- v);
2558 src#string "save path command"
2559 (fun () -> conf.savecmd)
2560 (fun v -> conf.savecmd <- v);
2561 src#colorspace "color space"
2562 (fun () -> CSTE.to_string conf.colorspace)
2563 (fun v ->
2564 conf.colorspace <- CSTE.of_int v;
2565 wcmd "cs %d" v;
2566 load state.layout;
2568 src#paxmark "pax mark method"
2569 (fun () -> MTE.to_string conf.paxmark)
2570 (fun v -> conf.paxmark <- MTE.of_int v);
2571 if bousable ()
2572 then
2573 src#bool "use PBO"
2574 (fun () -> conf.usepbo)
2575 (fun v -> conf.usepbo <- v);
2576 src#bool "mouse wheel scrolls pages"
2577 (fun () -> conf.wheelbypage)
2578 (fun v -> conf.wheelbypage <- v);
2579 src#bool "open remote links in a new instance"
2580 (fun () -> conf.riani)
2581 (fun v -> conf.riani <- v);
2582 src#bool "edit annotations inline"
2583 (fun () -> conf.annotinline)
2584 (fun v -> conf.annotinline <- v);
2585 src#bool "coarse positioning in presentation mode"
2586 (fun () -> conf.coarseprespos)
2587 (fun v -> conf.coarseprespos <- v);
2588 src#bool "use document CSS"
2589 (fun () -> conf.usedoccss)
2590 (fun v ->
2591 conf.usedoccss <- v;
2592 state.anchor <- getanchor ();
2593 opendoc state.path state.password;
2595 src#bool ~btos "colors"
2596 (fun () -> !showcolors)
2597 (fun v -> showcolors := v; fillsrc prevmode prevuioh);
2598 if !showcolors
2599 then (
2600 colorp " background"
2601 (fun () -> conf.bgcolor)
2602 (fun v -> conf.bgcolor <- v);
2604 rgba " paper color"
2605 (fun () -> conf.papercolor)
2606 (fun v ->
2607 conf.papercolor <- v;
2608 setpapercolor conf.papercolor;
2609 flushtiles ();
2611 rgba " scrollbar"
2612 (fun () -> conf.sbarcolor)
2613 (fun v -> conf.sbarcolor <- v);
2614 rgba " scrollbar handle"
2615 (fun () -> conf.sbarhndlcolor)
2616 (fun v -> conf.sbarhndlcolor <- v);
2617 rgba " texture color"
2618 (fun () -> conf.texturecolor)
2619 (fun v ->
2620 GlTex.env (`color v);
2621 conf.texturecolor <- v;
2626 sep ();
2627 src#caption "Document" 0;
2628 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
2629 src#caption2 "Pages" (fun () -> string_of_int state.pagecount) 1;
2630 src#caption2 "Dimensions"
2631 (fun () -> string_of_int (List.length state.pdims)) 1;
2632 if nonemptystr conf.css
2633 then src#caption2 "CSS" (fun () -> conf.css) 1;
2634 if conf.trimmargins
2635 then (
2636 sep ();
2637 src#caption "Trimmed margins" 0;
2638 src#caption2 "Dimensions"
2639 (fun () -> string_of_int (List.length state.pdims)) 1;
2642 sep ();
2643 src#caption "OpenGL" 0;
2644 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
2645 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
2647 sep ();
2648 src#caption "Location" 0;
2649 if nonemptystr state.origin
2650 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
2651 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
2653 src#reset prevmode prevuioh;
2655 fun () -> (
2656 state.text <- E.s;
2657 resetmstate ();
2658 let prevmode = state.mode
2659 and prevuioh = state.uioh in
2660 fillsrc prevmode prevuioh;
2661 let source = (src :> lvsource) in
2662 let modehash = findkeyhash conf "info" in
2663 state.uioh <-
2664 coe (object (self)
2665 inherit listview ~zebra:false ~helpmode:false
2666 ~source ~trusted:true ~modehash as super
2667 val mutable m_prevmemused = 0
2668 method! infochanged = function
2669 | Memused ->
2670 if m_prevmemused != state.memused
2671 then (
2672 m_prevmemused <- state.memused;
2673 postRedisplay "memusedchanged";
2675 | Pdim -> postRedisplay "pdimchanged"
2676 | Docinfo -> fillsrc prevmode prevuioh
2678 method! key key mask =
2679 if not (Wsi.withctrl mask)
2680 then
2681 match [@warning "-4"] Wsi.kc2kt key with
2682 | Keys.Left -> coe (self#updownlevel ~-1)
2683 | Keys.Right -> coe (self#updownlevel 1)
2684 | _ -> super#key key mask
2685 else super#key key mask
2686 end);
2687 postRedisplay "info";
2691 let enterhelpmode =
2692 let source =
2693 (object
2694 inherit lvsourcebase
2695 method getitemcount = Array.length state.help
2696 method getitem n =
2697 let s, l, _ = state.help.(n) in
2698 (s, l)
2700 method exit ~uioh ~cancel ~active ~first ~pan =
2701 let optuioh =
2702 if not cancel
2703 then (
2704 match state.help.(active) with
2705 | _, _, Action f -> Some (f uioh)
2706 | _, _, Noaction -> Some uioh
2708 else None
2710 m_active <- active;
2711 m_first <- first;
2712 m_pan <- pan;
2713 optuioh
2715 method hasaction n =
2716 match state.help.(n) with
2717 | _, _, Action _ -> true
2718 | _, _, Noaction -> false
2720 initializer
2721 m_active <- -1
2722 end)
2724 fun () ->
2725 let modehash = findkeyhash conf "help" in
2726 resetmstate ();
2727 state.uioh <- coe (new listview
2728 ~zebra:false ~helpmode:true
2729 ~source ~trusted:true ~modehash);
2730 postRedisplay "help";
2733 let entermsgsmode =
2734 let msgsource =
2735 (object
2736 inherit lvsourcebase
2737 val mutable m_items = E.a
2739 method getitemcount = 1 + Array.length m_items
2741 method getitem n =
2742 if n = 0
2743 then "[Clear]", 0
2744 else m_items.(n-1), 0
2746 method exit ~uioh ~cancel ~active ~first ~pan =
2747 ignore uioh;
2748 if not cancel
2749 then (
2750 if active = 0
2751 then Buffer.clear state.errmsgs;
2753 m_active <- active;
2754 m_first <- first;
2755 m_pan <- pan;
2756 None
2758 method hasaction n =
2759 n = 0
2761 method reset =
2762 state.newerrmsgs <- false;
2763 let l = Str.split Utils.Re.crlf (Buffer.contents state.errmsgs) in
2764 m_items <- Array.of_list l
2766 initializer
2767 m_active <- 0
2768 end)
2769 in fun () ->
2770 state.text <- E.s;
2771 resetmstate ();
2772 msgsource#reset;
2773 let source = (msgsource :> lvsource) in
2774 let modehash = findkeyhash conf "listview" in
2775 state.uioh <-
2776 coe (object
2777 inherit listview ~zebra:false ~helpmode:false
2778 ~source ~trusted:false ~modehash as super
2779 method! display =
2780 if state.newerrmsgs
2781 then msgsource#reset;
2782 super#display
2783 end);
2784 postRedisplay "msgs";
2787 let getusertext s =
2788 let editor = getenvdef "EDITOR" E.s in
2789 if emptystr editor
2790 then E.s
2791 else
2792 let tmppath = Filename.temp_file "llpp" "note" in
2793 if nonemptystr s
2794 then (
2795 let oc = open_out tmppath in
2796 output_string oc s;
2797 close_out oc;
2799 let execstr = editor ^ " " ^ tmppath in
2800 let s =
2801 match spawn execstr [] with
2802 | exception exn ->
2803 impmsg "spawn(%S) failed: %s" execstr @@ exntos exn;
2805 | pid ->
2806 match Unix.waitpid [] pid with
2807 | exception exn ->
2808 impmsg "waitpid(%d) failed: %s" pid @@ exntos exn;
2810 | (_pid, status) ->
2811 match status with
2812 | Unix.WEXITED 0 -> filecontents tmppath
2813 | Unix.WEXITED n ->
2814 impmsg "editor process(%s) exited abnormally: %d" execstr n;
2816 | Unix.WSIGNALED n ->
2817 impmsg "editor process(%s) was killed by signal %d" execstr n;
2819 | Unix.WSTOPPED n ->
2820 impmsg "editor(%s) process was stopped by signal %d" execstr n;
2823 match Unix.unlink tmppath with
2824 | exception exn ->
2825 impmsg "failed to ulink %S: %s" tmppath @@ exntos exn;
2827 | () -> s
2830 let enterannotmode opaque slinkindex =
2831 let msgsource =
2832 (object
2833 inherit lvsourcebase
2834 val mutable m_text = E.s
2835 val mutable m_items = E.a
2837 method getitemcount = Array.length m_items
2839 method getitem n =
2840 let label, _func = m_items.(n) in
2841 label, 0
2843 method exit ~uioh ~cancel ~active ~first ~pan =
2844 ignore (uioh, first, pan);
2845 if not cancel
2846 then (
2847 let _label, func = m_items.(active) in
2848 func ()
2850 None
2852 method hasaction n = nonemptystr @@ fst m_items.(n)
2854 method reset s =
2855 let rec split accu b i =
2856 let p = b+i in
2857 if p = String.length s
2858 then (String.sub s b (p-b), fun () -> ()) :: accu
2859 else
2860 if (i > 70 && s.[p] = ' ') || s.[p] = '\r' || s.[p] = '\n'
2861 then
2862 let ss = if i = 0 then E.s else String.sub s b i in
2863 split ((ss, fun () -> ())::accu) (p+1) 0
2864 else split accu b (i+1)
2866 let cleanup () =
2867 wcmd "freepage %s" (~> opaque);
2868 let keys =
2869 Hashtbl.fold (fun key opaque' accu ->
2870 if opaque' = opaque'
2871 then key :: accu else accu) state.pagemap []
2873 List.iter (Hashtbl.remove state.pagemap) keys;
2874 flushtiles ();
2875 gotoxy state.x state.y
2877 let dele () =
2878 delannot opaque slinkindex;
2879 cleanup ();
2881 let edit inline () =
2882 let update s =
2883 if emptystr s
2884 then dele ()
2885 else (
2886 modannot opaque slinkindex s;
2887 cleanup ();
2890 if inline
2891 then
2892 let mode = state.mode in
2893 state.mode <-
2894 Textentry (
2895 ("annotation: ", m_text, None, textentry, update, true),
2896 fun _ -> state.mode <- mode
2898 state.text <- E.s;
2899 enttext ();
2900 else
2901 let s = getusertext m_text in
2902 update s
2904 m_text <- s;
2905 m_items <-
2906 ( "[Copy]", fun () -> selstring conf.selcmd m_text)
2907 :: ("[Delete]", dele)
2908 :: ("[Edit]", edit conf.annotinline)
2909 :: (E.s, fun () -> ())
2910 :: split [] 0 0 |> List.rev |> Array.of_list
2912 initializer
2913 m_active <- 0
2914 end)
2916 state.text <- E.s;
2917 let s = getannotcontents opaque slinkindex in
2918 resetmstate ();
2919 msgsource#reset s;
2920 let source = (msgsource :> lvsource) in
2921 let modehash = findkeyhash conf "listview" in
2922 state.uioh <- coe (object
2923 inherit listview ~zebra:false ~helpmode:false
2924 ~source ~trusted:false ~modehash
2925 end);
2926 postRedisplay "enterannotmode";
2929 let gotoremote spec =
2930 let filename, dest = splitatchar spec '#' in
2931 let getpath filename =
2932 let path =
2933 if nonemptystr filename
2934 then
2935 if Filename.is_relative filename
2936 then
2937 let dir = Filename.dirname state.path in
2938 let dir =
2939 if Filename.is_implicit dir
2940 then Filename.concat (Sys.getcwd ()) dir
2941 else dir
2943 Filename.concat dir filename
2944 else filename
2945 else E.s
2947 if Sys.file_exists path
2948 then path
2949 else E.s
2951 let path = getpath filename in
2952 let dospawn lcmd =
2953 if conf.riani
2954 then
2955 let cmd = Lazy.force_val lcmd in
2956 match spawn cmd with
2957 | _pid -> ()
2958 | exception exn -> dolog "failed to execute `%s': %s" cmd @@ exntos exn
2959 else
2960 let anchor = getanchor () in
2961 let ranchor = state.path, state.password, anchor, state.origin in
2962 state.origin <- E.s;
2963 state.ranchors <- ranchor :: state.ranchors;
2964 opendoc path E.s;
2966 if substratis spec 0 "page="
2967 then
2968 match Scanf.sscanf spec "page=%d" (fun n -> n) with
2969 | pageno ->
2970 state.anchor <- (pageno, 0.0, 0.0);
2971 dospawn @@ lazy (Printf.sprintf "%s -page %d %S" !selfexec pageno path);
2972 | exception exn ->
2973 adderrfmt "error parsing remote destination" "page: %s" @@ exntos exn
2974 else (
2975 state.nameddest <- dest;
2976 dospawn @@ lazy (!selfexec ^ " " ^ path ^ " -dest " ^ dest)
2980 let gotounder = function
2981 | Ulinkuri s when isexternallink s ->
2982 if substratis s 0 "file://"
2983 then gotoremote @@ String.sub s 7 (String.length s - 7)
2984 else Help.gotouri conf.urilauncher s
2985 | Ulinkuri s ->
2986 let pageno, x, y = uritolocation s in
2987 addnav ();
2988 gotopagexy pageno x y
2989 | Utext _ | Unone -> ()
2990 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
2993 let gotooutline (_, _, kind) =
2994 match kind with
2995 | Onone -> ()
2996 | Oanchor ((pageno, y, _) as anchor) ->
2997 addnav ();
2998 gotoxy state.x @@
2999 getanchory (if conf.presentation then (pageno, y, 1.0) else anchor)
3000 | Ouri uri -> gotounder (Ulinkuri uri)
3001 | Olaunch cmd -> error "gotounder (Ulaunch %S)" cmd
3002 | Oremote (remote, pageno) ->
3003 error "gotounder (Uremote (%S,%d) )" remote pageno
3004 | Ohistory hist -> gotohist hist
3005 | Oremotedest (path, dest) ->
3006 error "gotounder (Uremotedest (%S, %S))" path dest
3009 class outlinesoucebase fetchoutlines = object (self)
3010 inherit lvsourcebase
3011 val mutable m_items = E.a
3012 val mutable m_minfo = E.a
3013 val mutable m_orig_items = E.a
3014 val mutable m_orig_minfo = E.a
3015 val mutable m_narrow_patterns = []
3016 val mutable m_gen = -1
3018 method getitemcount = Array.length m_items
3020 method getitem n =
3021 let s, n, _ = m_items.(n) in
3022 (s, n+0)
3024 method exit ~(uioh:uioh) ~cancel ~active ~(first:int) ~pan : uioh option =
3025 ignore (uioh, first);
3026 let items, minfo =
3027 if m_narrow_patterns = []
3028 then m_orig_items, m_orig_minfo
3029 else m_items, m_minfo
3031 m_pan <- pan;
3032 if not cancel
3033 then (
3034 m_items <- items;
3035 m_minfo <- minfo;
3036 gotooutline m_items.(active);
3038 else (
3039 m_items <- items;
3040 m_minfo <- minfo;
3042 None
3044 method hasaction (_:int) = true
3046 method greetmsg =
3047 if Array.length m_items != Array.length m_orig_items
3048 then
3049 let s =
3050 match m_narrow_patterns with
3051 | one :: [] -> one
3052 | many -> String.concat Utf8syms.ellipsis (List.rev many)
3054 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
3055 else E.s
3057 method statestr =
3058 match m_narrow_patterns with
3059 | [] -> E.s
3060 | one :: [] -> one
3061 | head :: _ -> Utf8syms.ellipsis ^ head
3063 method narrow pattern =
3064 match Str.regexp_case_fold pattern with
3065 | exception _ -> ()
3066 | re ->
3067 let rec loop accu minfo n =
3068 if n = -1
3069 then (
3070 m_items <- Array.of_list accu;
3071 m_minfo <- Array.of_list minfo;
3073 else
3074 let (s, _, _) as o = m_items.(n) in
3075 let accu, minfo =
3076 match Str.search_forward re s 0 with
3077 | exception Not_found -> accu, minfo
3078 | first -> o :: accu, (first, Str.match_end ()) :: minfo
3080 loop accu minfo (n-1)
3082 loop [] [] (Array.length m_items - 1)
3084 method! getminfo = m_minfo
3086 method denarrow =
3087 m_orig_items <- fetchoutlines ();
3088 m_minfo <- m_orig_minfo;
3089 m_items <- m_orig_items
3091 method add_narrow_pattern pattern =
3092 m_narrow_patterns <- pattern :: m_narrow_patterns
3094 method del_narrow_pattern =
3095 match m_narrow_patterns with
3096 | _ :: rest -> m_narrow_patterns <- rest
3097 | [] -> ()
3099 method renarrow =
3100 self#denarrow;
3101 match m_narrow_patterns with
3102 | pattern :: [] -> self#narrow pattern; pattern
3103 | list ->
3104 List.fold_left (fun accu pattern ->
3105 self#narrow pattern;
3106 pattern ^ Utf8syms.ellipsis ^ accu) E.s list
3108 method calcactive (_:anchor) = 0
3110 method reset anchor items =
3111 if state.gen != m_gen
3112 then (
3113 m_orig_items <- items;
3114 m_items <- items;
3115 m_narrow_patterns <- [];
3116 m_minfo <- E.a;
3117 m_orig_minfo <- E.a;
3118 m_gen <- state.gen;
3120 else (
3121 if items != m_orig_items
3122 then (
3123 m_orig_items <- items;
3124 if m_narrow_patterns == []
3125 then m_items <- items;
3128 let active = self#calcactive anchor in
3129 m_active <- active;
3130 m_first <- firstof m_first active
3134 let outlinesource fetchoutlines =
3135 (object
3136 inherit outlinesoucebase fetchoutlines
3137 method! calcactive anchor =
3138 let rely = getanchory anchor in
3139 let rec loop n best bestd =
3140 if n = Array.length m_items
3141 then best
3142 else
3143 let _, _, kind = m_items.(n) in
3144 match kind with
3145 | Oanchor anchor ->
3146 let orely = getanchory anchor in
3147 let d = abs (orely - rely) in
3148 if d < bestd
3149 then loop (n+1) n d
3150 else loop (n+1) best bestd
3151 | Onone | Oremote _ | Olaunch _
3152 | Oremotedest _ | Ouri _ | Ohistory _ ->
3153 loop (n+1) best bestd
3155 loop 0 ~-1 max_int
3156 end)
3159 let enteroutlinemode, enterbookmarkmode, enterhistmode =
3160 let fetchoutlines sourcetype () =
3161 match sourcetype with
3162 | `bookmarks -> Array.of_list state.bookmarks
3163 | `outlines -> state.outlines
3164 | `history -> genhistoutlines () |> Array.of_list
3166 let so = outlinesource (fetchoutlines `outlines) in
3167 let sb = outlinesource (fetchoutlines `bookmarks) in
3168 let sh = outlinesource (fetchoutlines `history) in
3169 let mkselector sourcetype source =
3170 (fun errmsg ->
3171 let outlines = fetchoutlines sourcetype () in
3172 if Array.length outlines = 0
3173 then showtext ' ' errmsg
3174 else (
3175 resetmstate ();
3176 Wsi.setcursor Wsi.CURSOR_INHERIT;
3177 let anchor = getanchor () in
3178 source#reset anchor outlines;
3179 state.text <- source#greetmsg;
3180 state.uioh <-
3181 coe (new outlinelistview ~zebra:(sourcetype=`history) ~source);
3182 postRedisplay "enter selector";
3186 let mkenter sourcetype errmsg s = fun () -> mkselector sourcetype s errmsg in
3187 ( mkenter `outlines "document has no outline" so
3188 , mkenter `bookmarks "document has no bookmarks (yet)" sb
3189 , mkenter `history "history is empty" sh )
3192 let quickbookmark ?title () =
3193 match state.layout with
3194 | [] -> ()
3195 | l :: _ ->
3196 let title =
3197 match title with
3198 | None ->
3199 Unix.(
3200 let tm = localtime (now ()) in
3201 Printf.sprintf
3202 "Quick (page %d) (bookmarked on %02d/%02d/%d at %02d:%02d)"
3203 (l.pageno+1)
3204 tm.tm_mday (tm.tm_mon+1) (tm.tm_year+1900) tm.tm_hour tm.tm_min
3206 | Some title -> title
3208 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
3211 let setautoscrollspeed step goingdown =
3212 let incr = max 1 ((abs step) / 2) in
3213 let incr = if goingdown then incr else -incr in
3214 let astep = boundastep state.winh (step + incr) in
3215 state.autoscroll <- Some astep;
3218 let canpan () =
3219 match conf.columns with
3220 | Csplit _ -> true
3221 | Csingle _ | Cmulti _ -> state.x != 0 || conf.zoom > 1.0
3224 let panbound x = bound x (-state.w) state.winw;;
3226 let existsinrow pageno (columns, coverA, coverB) p =
3227 let last = ((pageno - coverA) mod columns) + columns in
3228 let rec any = function
3229 | [] -> false
3230 | l :: rest ->
3231 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
3232 then p l
3233 else (
3234 if not (p l)
3235 then (if l.pageno = last then false else any rest)
3236 else true
3239 any state.layout
3242 let nextpage () =
3243 match state.layout with
3244 | [] ->
3245 let pageno = page_of_y state.y in
3246 gotoxy state.x (getpagey (pageno+1))
3247 | l :: rest ->
3248 match conf.columns with
3249 | Csingle _ ->
3250 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
3251 then
3252 let y = clamp (pgscale state.winh) in
3253 gotoxy state.x y
3254 else
3255 let pageno = min (l.pageno+1) (state.pagecount-1) in
3256 gotoxy state.x (getpagey pageno)
3257 | Cmulti ((c, _, _) as cl, _) ->
3258 if conf.presentation
3259 && (existsinrow l.pageno cl
3260 (fun l -> l.pageh > l.pagey + l.pagevh))
3261 then
3262 let y = clamp (pgscale state.winh) in
3263 gotoxy state.x y
3264 else
3265 let pageno = min (l.pageno+c) (state.pagecount-1) in
3266 gotoxy state.x (getpagey pageno)
3267 | Csplit (n, _) ->
3268 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
3269 then
3270 let pagey, pageh = getpageyh l.pageno in
3271 let pagey = pagey + pageh * l.pagecol in
3272 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
3273 gotoxy state.x (pagey + pageh + ips)
3276 let prevpage () =
3277 match state.layout with
3278 | [] ->
3279 let pageno = page_of_y state.y in
3280 gotoxy state.x (getpagey (pageno-1))
3281 | l :: _ ->
3282 match conf.columns with
3283 | Csingle _ ->
3284 if conf.presentation && l.pagey != 0
3285 then gotoxy state.x (clamp (pgscale ~-(state.winh)))
3286 else
3287 let pageno = max 0 (l.pageno-1) in
3288 gotoxy state.x (getpagey pageno)
3289 | Cmulti ((c, _, coverB) as cl, _) ->
3290 if conf.presentation &&
3291 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
3292 then gotoxy state.x (clamp (pgscale ~-(state.winh)))
3293 else
3294 let decr =
3295 if l.pageno = state.pagecount - coverB
3296 then 1
3297 else c
3299 let pageno = max 0 (l.pageno-decr) in
3300 gotoxy state.x (getpagey pageno)
3301 | Csplit (n, _) ->
3302 let y =
3303 if l.pagecol = 0
3304 then
3305 if l.pageno = 0
3306 then l.pagey
3307 else
3308 let pageno = max 0 (l.pageno-1) in
3309 let pagey, pageh = getpageyh pageno in
3310 pagey + (n-1)*pageh
3311 else
3312 let pagey, pageh = getpageyh l.pageno in
3313 pagey + pageh * (l.pagecol-1) - conf.interpagespace
3315 gotoxy state.x y
3318 let save () =
3319 if emptystr conf.savecmd
3320 then adderrmsg "savepath-command is empty"
3321 "don't know where to save modified document"
3322 else
3323 let savecmd = Str.global_replace Utils.Re.percent state.path conf.savecmd in
3324 let path =
3325 getcmdoutput
3326 (fun exn ->
3327 adderrfmt savecmd "failed to produce path to the saved copy: %s" exn)
3328 savecmd
3330 if nonemptystr path
3331 then
3332 let tmp = path ^ ".tmp" in
3333 savedoc tmp;
3334 Unix.rename tmp path;
3337 let viewkeyboard key mask =
3338 let enttext te =
3339 let mode = state.mode in
3340 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
3341 state.text <- E.s;
3342 enttext ();
3343 postRedisplay "view:enttext"
3345 let ctrl = Wsi.withctrl mask in
3346 let open Keys in
3347 match Wsi.kc2kt key with
3348 | Ascii 'S' -> state.slideshow <- state.slideshow lxor 1
3350 | Ascii 'Q' -> exit 0
3352 | Ascii 'z' ->
3353 let yloc f =
3354 match List.rev state.rects with
3355 | [] -> ()
3356 | (pageno, _, (_, y0, _, y1, _, y2, _, y3)) :: _ ->
3357 f pageno (y0, y1, y2, y3)
3358 and yminmax (y0, y1, y2, y3) =
3359 let ym = min y0 y1 |> min y2 |> min y3 |> truncate in
3360 let yM = max y0 y1 |> max y2 |> max y3 |> truncate in
3361 ym, yM
3363 let ondone msg = state.text <- msg
3364 and zmod _ _ k =
3365 match [@warning "-4"] k with
3366 | Keys.Ascii 'z' ->
3367 let f pageno ys =
3368 let ym, yM = yminmax ys in
3369 let hh = (yM - ym)/2 in
3370 gotopage1 pageno (ym + hh - state.winh/2)
3372 yloc f;
3373 TEdone "center"
3374 | Keys.Ascii 't' ->
3375 let f pageno ys =
3376 let ym, _ = yminmax ys in
3377 gotopage1 pageno ym
3379 yloc f;
3380 TEdone "top"
3381 | Keys.Ascii 'b' ->
3382 let f pageno ys =
3383 let _, yM = yminmax ys in
3384 gotopage1 pageno (yM - state.winh)
3386 yloc f;
3387 TEdone "bottom"
3388 | _ -> TEstop
3390 enttext (": ", E.s, None, zmod state.mode, ondone, true)
3392 | Ascii 'W' ->
3393 if hasunsavedchanges ()
3394 then save ()
3396 | Insert ->
3397 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
3398 then (
3399 state.mode <- (
3400 match state.lnava with
3401 | None -> LinkNav (Ltgendir 0)
3402 | Some pn -> LinkNav (Ltexact pn)
3404 gotoxy state.x state.y;
3406 else impmsg "keyboard link navigation does not work under rotation"
3408 | Escape | Ascii 'q' ->
3409 begin match state.mstate with
3410 | Mzoomrect _ ->
3411 resetmstate ();
3412 postRedisplay "kill rect";
3413 | Msel _
3414 | Mpan _
3415 | Mscrolly | Mscrollx
3416 | Mzoom _
3417 | Mnone ->
3418 begin match state.mode with
3419 | LinkNav ln ->
3420 begin match ln with
3421 | Ltexact pl -> state.lnava <- Some pl
3422 | Ltgendir _ | Ltnotready _ -> state.lnava <- None
3423 end;
3424 state.mode <- View;
3425 postRedisplay "esc leave linknav"
3426 | Birdseye _ | Textentry _ | View ->
3427 match state.ranchors with
3428 | [] -> raise Quit
3429 | (path, password, anchor, origin) :: rest ->
3430 state.ranchors <- rest;
3431 state.anchor <- anchor;
3432 state.origin <- origin;
3433 state.nameddest <- E.s;
3434 opendoc path password
3435 end;
3436 end;
3438 | Backspace ->
3439 addnavnorc ();
3440 gotoxy state.x (getnav ~-1)
3442 | Ascii 'o' -> enteroutlinemode ()
3443 | Ascii 'H' -> enterhistmode ()
3445 | Ascii 'u' ->
3446 state.rects <- [];
3447 state.text <- E.s;
3448 Hashtbl.iter (fun _ opaque ->
3449 clearmark opaque;
3450 Hashtbl.clear state.prects) state.pagemap;
3451 postRedisplay "dehighlight";
3453 | Ascii (('/' | '?') as c) ->
3454 let ondone isforw s =
3455 cbput state.hists.pat s;
3456 state.searchpattern <- s;
3457 search s isforw
3459 let s = String.make 1 c in
3460 enttext (s, E.s, Some (onhist state.hists.pat),
3461 textentry, ondone (c = '/'), true)
3463 | Ascii '+' | Ascii '=' when ctrl ->
3464 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
3465 pivotzoom (conf.zoom +. incr)
3467 | Ascii '+' ->
3468 let ondone s =
3469 let n =
3470 try int_of_string s with exn ->
3471 state.text <-
3472 Printf.sprintf "bad integer `%s': %s" s @@ exntos exn;
3473 max_int
3475 if n != max_int
3476 then (
3477 conf.pagebias <- n;
3478 state.text <- "page bias is now " ^ string_of_int n;
3481 enttext ("page bias: ", E.s, None, intentry, ondone, true)
3483 | Ascii '-' when ctrl ->
3484 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
3485 pivotzoom (max 0.01 (conf.zoom -. decr))
3487 | Ascii '-' ->
3488 let ondone msg = state.text <- msg in
3489 enttext ("option: ", E.s, None,
3490 optentry state.mode, ondone, true)
3492 | Ascii '0' when ctrl ->
3493 if conf.zoom = 1.0
3494 then gotoxy 0 state.y
3495 else setzoom 1.0
3497 | Ascii ('1'|'2' as c) when ctrl && conf.fitmodel != FitPage ->
3498 let cols =
3499 match conf.columns with
3500 | Csingle _ | Cmulti _ -> 1
3501 | Csplit (n, _) -> n
3503 let h = state.winh -
3504 conf.interpagespace lsl (if conf.presentation then 1 else 0)
3506 let zoom = zoomforh state.winw h 0 cols in
3507 if zoom > 0.0 && (c = '2' || zoom < 1.0)
3508 then setzoom zoom
3510 | Ascii '3' when ctrl ->
3511 let fm =
3512 match conf.fitmodel with
3513 | FitWidth -> FitProportional
3514 | FitProportional -> FitPage
3515 | FitPage -> FitWidth
3517 state.text <- "fit model: " ^ FMTE.to_string fm;
3518 reqlayout conf.angle fm
3520 | Ascii '4' when ctrl ->
3521 let zoom = getmaxw () /. float state.winw in
3522 if zoom > 0.0 then setzoom zoom
3524 | Fn 9 | Ascii '9' when ctrl -> togglebirdseye ()
3526 | Ascii ('0'..'9' as c) when not ctrl ->
3527 let ondone s =
3528 let n =
3529 try int_of_string s with exn ->
3530 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn;
3533 if n >= 0
3534 then (
3535 addnav ();
3536 cbput state.hists.pag (string_of_int n);
3537 gotopage1 (n + conf.pagebias - 1) 0;
3540 let pageentry text = function [@warning "-4"]
3541 | Keys.Ascii 'g' -> TEdone text
3542 | key -> intentry text key
3544 let text = String.make 1 c in
3545 enttext (":", text, Some (onhist state.hists.pag),
3546 pageentry, ondone, true)
3548 | Ascii 'b' ->
3549 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
3550 postRedisplay "toggle scrollbar";
3552 | Ascii 'B' ->
3553 state.bzoom <- not state.bzoom;
3554 state.rects <- [];
3555 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
3557 | Ascii 'l' ->
3558 conf.hlinks <- not conf.hlinks;
3559 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
3560 postRedisplay "toggle highlightlinks";
3562 | Ascii 'F' ->
3563 if conf.angle mod 360 = 0
3564 then (
3565 state.glinks <- true;
3566 let mode = state.mode in
3567 state.mode <-
3568 Textentry (
3569 ("goto: ", E.s, None, linknentry, linknact gotounder, false),
3570 (fun _ ->
3571 state.glinks <- false;
3572 state.mode <- mode)
3574 state.text <- E.s;
3575 postRedisplay "view:linkent(F)"
3577 else impmsg "hint mode does not work under rotation"
3579 | Ascii 'y' ->
3580 state.glinks <- true;
3581 let mode = state.mode in
3582 state.mode <-
3583 Textentry (
3584 ("copy: ", E.s, None, linknentry,
3585 linknact (fun under ->
3586 selstring conf.selcmd (undertext under)), false),
3587 (fun _ ->
3588 state.glinks <- false;
3589 state.mode <- mode)
3591 state.text <- E.s;
3592 postRedisplay "view:linkent"
3594 | Ascii 'a' ->
3595 begin match state.autoscroll with
3596 | Some step ->
3597 conf.autoscrollstep <- step;
3598 state.autoscroll <- None
3599 | None ->
3600 state.autoscroll <- Some conf.autoscrollstep;
3601 state.slideshow <- state.slideshow land lnot 2
3604 | Ascii 'p' when ctrl ->
3605 launchpath () (* XXX where do error messages go? *)
3607 | Ascii 'P' ->
3608 setpresentationmode (not conf.presentation);
3609 showtext ' ' ("presentation mode " ^
3610 if conf.presentation then "on" else "off");
3612 | Ascii 'f' ->
3613 if List.mem Wsi.Fullscreen state.winstate
3614 then Wsi.reshape conf.cwinw conf.cwinh
3615 else Wsi.fullscreen ()
3617 | Ascii ('p'|'N') -> search state.searchpattern false
3618 | Ascii 'n' | Fn 3 -> search state.searchpattern true
3620 | Ascii 't' ->
3621 begin match state.layout with
3622 | [] -> ()
3623 | l :: _ -> gotoxy state.x (getpagey l.pageno)
3626 | Ascii ' ' -> nextpage ()
3627 | Delete -> prevpage ()
3628 | Ascii '=' -> showtext ' ' (describe_layout state.layout);
3630 | Ascii 'w' ->
3631 begin match state.layout with
3632 | [] -> ()
3633 | l :: _ ->
3634 Wsi.reshape l.pagew l.pageh;
3635 postRedisplay "w"
3638 | Ascii '\'' -> enterbookmarkmode ()
3639 | Ascii 'h' | Fn 1 -> enterhelpmode ()
3640 | Ascii 'i' -> enterinfomode ()
3641 | Ascii 'e' when Buffer.length state.errmsgs > 0 -> entermsgsmode ()
3643 | Ascii 'm' ->
3644 let ondone s =
3645 match state.layout with
3646 | l :: _ when nonemptystr s ->
3647 state.bookmarks <- (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
3648 | _ -> ()
3650 enttext ("bookmark: ", E.s, None, textentry, ondone, true)
3652 | Ascii '~' ->
3653 quickbookmark ();
3654 showtext ' ' "Quick bookmark added";
3656 | Ascii 'x' -> state.roam ()
3658 | Ascii ('<'|'>' as c) ->
3659 reqlayout (conf.angle + (if c = '>' then 30 else -30)) conf.fitmodel
3661 | Ascii ('['|']' as c) ->
3662 conf.colorscale <-
3663 bound (conf.colorscale +. (if c = ']' then 0.1 else -0.1)) 0.0 1.0;
3664 postRedisplay "brightness";
3666 | Ascii 'c' when state.mode = View ->
3667 if Wsi.withalt mask
3668 then (
3669 if conf.zoom > 1.0
3670 then
3671 let m = (state.winw - state.w) / 2 in
3672 gotoxy m state.y
3674 else
3675 let (c, a, b), z =
3676 match state.prevcolumns with
3677 | None -> (1, 0, 0), 1.0
3678 | Some (columns, z) ->
3679 let cab =
3680 match columns with
3681 | Csplit (c, _) -> -c, 0, 0
3682 | Cmulti ((c, a, b), _) -> c, a, b
3683 | Csingle _ -> 1, 0, 0
3685 cab, z
3687 setcolumns View c a b;
3688 setzoom z
3690 | Down | Up when ctrl && Wsi.withshift mask ->
3691 let zoom, x = state.prevzoom in
3692 setzoom zoom;
3693 state.x <- x;
3695 | Up ->
3696 begin match state.autoscroll with
3697 | None ->
3698 begin match state.mode with
3699 | Birdseye beye -> upbirdseye 1 beye
3700 | Textentry _ | View | LinkNav _ ->
3701 if ctrl
3702 then gotoxy state.x (clamp ~-(state.winh/2))
3703 else (
3704 if not (Wsi.withshift mask) && conf.presentation
3705 then prevpage ()
3706 else gotoxy state.x (clamp (-conf.scrollstep))
3709 | Some n -> setautoscrollspeed n false
3712 | Down ->
3713 begin match state.autoscroll with
3714 | None ->
3715 begin match state.mode with
3716 | Birdseye beye -> downbirdseye 1 beye
3717 | Textentry _ | View | LinkNav _ ->
3718 if ctrl
3719 then gotoxy state.x (clamp (state.winh/2))
3720 else (
3721 if not (Wsi.withshift mask) && conf.presentation
3722 then nextpage ()
3723 else gotoxy state.x (clamp (conf.scrollstep))
3726 | Some n -> setautoscrollspeed n true
3729 | Left | Right when not (Wsi.withalt mask) ->
3730 if canpan ()
3731 then
3732 let dx =
3733 if ctrl
3734 then state.winw / 2
3735 else conf.hscrollstep
3737 let dx =
3738 let pv = Wsi.kc2kt key in
3739 if pv = Keys.Left then dx else -dx
3741 gotoxy (panbound (state.x + dx)) state.y
3742 else (
3743 state.text <- E.s;
3744 postRedisplay "left/right"
3747 | Prior ->
3748 let y =
3749 if ctrl
3750 then
3751 match state.layout with
3752 | [] -> state.y
3753 | l :: _ -> state.y - l.pagey
3754 else clamp (pgscale (-state.winh))
3756 gotoxy state.x y
3758 | Next ->
3759 let y =
3760 if ctrl
3761 then
3762 match List.rev state.layout with
3763 | [] -> state.y
3764 | l :: _ -> getpagey l.pageno
3765 else clamp (pgscale state.winh)
3767 gotoxy state.x y
3769 | Ascii 'g' | Home ->
3770 addnav ();
3771 gotoxy 0 0
3772 | Ascii 'G' | End ->
3773 addnav ();
3774 gotoxy 0 (clamp state.maxy)
3776 | Right when Wsi.withalt mask ->
3777 addnavnorc ();
3778 gotoxy state.x (getnav 1)
3779 | Left when Wsi.withalt mask ->
3780 addnavnorc ();
3781 gotoxy state.x (getnav ~-1)
3783 | Ascii 'r' ->
3784 reload ()
3786 | Ascii 'v' when conf.debug ->
3787 state.rects <- [];
3788 List.iter (fun l ->
3789 match getopaque l.pageno with
3790 | None -> ()
3791 | Some opaque ->
3792 let x0, y0, x1, y1 = pagebbox opaque in
3793 let rect = (float x0, float y0,
3794 float x1, float y0,
3795 float x1, float y1,
3796 float x0, float y1) in
3797 debugrect rect;
3798 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
3799 state.rects <- (l.pageno, color, rect) :: state.rects;
3800 ) state.layout;
3801 postRedisplay "v";
3803 | Ascii '|' ->
3804 let mode = state.mode in
3805 let cmd = ref E.s in
3806 let onleave = function
3807 | Cancel -> state.mode <- mode
3808 | Confirm ->
3809 List.iter (fun l ->
3810 match getopaque l.pageno with
3811 | Some opaque -> pipesel opaque !cmd
3812 | None -> ()) state.layout;
3813 state.mode <- mode
3815 let ondone s =
3816 cbput state.hists.sel s;
3817 cmd := s
3819 let te =
3820 "| ", !cmd, Some (onhist state.hists.sel), textentry, ondone, true
3822 postRedisplay "|";
3823 state.mode <- Textentry (te, onleave);
3825 | (Ascii _|Fn _|Enter|Left|Right|Code _|Ctrl _) ->
3826 vlog "huh? %s" (Wsi.keyname key)
3829 let linknavkeyboard key mask linknav =
3830 let pv = Wsi.kc2kt key in
3831 let getpage pageno =
3832 let rec loop = function
3833 | [] -> None
3834 | l :: _ when l.pageno = pageno -> Some l
3835 | _ :: rest -> loop rest
3836 in loop state.layout
3838 let doexact (pageno, n) =
3839 match getopaque pageno, getpage pageno with
3840 | Some opaque, Some l ->
3841 if pv = Keys.Enter
3842 then
3843 let under = getlink opaque n in
3844 postRedisplay "link gotounder";
3845 gotounder under;
3846 state.mode <- View;
3847 else
3848 let opt, dir =
3849 let open Keys in
3850 match pv with
3851 | Home -> Some (findlink opaque LDfirst), -1
3852 | End -> Some (findlink opaque LDlast), 1
3853 | Left -> Some (findlink opaque (LDleft n)), -1
3854 | Right -> Some (findlink opaque (LDright n)), 1
3855 | Up -> Some (findlink opaque (LDup n)), -1
3856 | Down -> Some (findlink opaque (LDdown n)), 1
3857 | Delete|Escape|Insert|Enter|Next|Prior|Ascii _
3858 | Code _|Fn _|Ctrl _|Backspace -> None, 0
3860 let pwl l dir =
3861 begin match findpwl l.pageno dir with
3862 | Pwlnotfound -> ()
3863 | Pwl pageno ->
3864 let notfound dir =
3865 state.mode <- LinkNav (Ltgendir dir);
3866 let y, h = getpageyh pageno in
3867 let y =
3868 if dir < 0
3869 then y + h - state.winh
3870 else y
3872 gotoxy state.x y
3874 begin match getopaque pageno, getpage pageno with
3875 | Some opaque, Some _ ->
3876 let link =
3877 let ld = if dir > 0 then LDfirst else LDlast in
3878 findlink opaque ld
3880 begin match link with
3881 | Lfound m ->
3882 showlinktype (getlink opaque m);
3883 state.mode <- LinkNav (Ltexact (pageno, m));
3884 postRedisplay "linknav jpage";
3885 | Lnotfound -> notfound dir
3886 end;
3887 | _ -> notfound dir
3888 end;
3889 end;
3891 begin match opt with
3892 | Some Lnotfound -> pwl l dir;
3893 | Some (Lfound m) ->
3894 if m = n
3895 then pwl l dir
3896 else (
3897 let _, y0, _, y1 = getlinkrect opaque m in
3898 if y0 < l.pagey
3899 then gotopage1 l.pageno y0
3900 else (
3901 let d = fstate.fontsize + 1 in
3902 if y1 - l.pagey > l.pagevh - d
3903 then gotopage1 l.pageno (y1 - state.winh + d)
3904 else postRedisplay "linknav";
3906 showlinktype (getlink opaque m);
3907 state.mode <- LinkNav (Ltexact (l.pageno, m));
3910 | None -> viewkeyboard key mask
3911 end;
3912 | _ -> viewkeyboard key mask
3914 if pv = Keys.Insert
3915 then (
3916 begin match linknav with
3917 | Ltexact pa -> state.lnava <- Some pa
3918 | Ltgendir _ | Ltnotready _ -> ()
3919 end;
3920 state.mode <- View;
3921 postRedisplay "leave linknav"
3923 else
3924 match linknav with
3925 | Ltgendir _ | Ltnotready _ -> viewkeyboard key mask
3926 | Ltexact exact -> doexact exact
3929 let keyboard key mask =
3930 if (key = Char.code 'g' && Wsi.withctrl mask) && not (istextentry state.mode)
3931 then wcmd "interrupt"
3932 else state.uioh <- state.uioh#key key mask
3935 let birdseyekeyboard key mask
3936 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
3937 let incr =
3938 match conf.columns with
3939 | Csingle _ -> 1
3940 | Cmulti ((c, _, _), _) -> c
3941 | Csplit _ -> error "bird's eye split mode"
3943 let pgh layout = List.fold_left
3944 (fun m l -> max l.pageh m) state.winh layout in
3945 let open Keys in
3946 match Wsi.kc2kt key with
3947 | Ascii 'l' when Wsi.withctrl mask ->
3948 let y, h = getpageyh pageno in
3949 let top = (state.winh - h) / 2 in
3950 gotoxy state.x (max 0 (y - top))
3951 | Enter -> leavebirdseye beye false
3952 | Escape -> leavebirdseye beye true
3953 | Up -> upbirdseye incr beye
3954 | Down -> downbirdseye incr beye
3955 | Left -> upbirdseye 1 beye
3956 | Right -> downbirdseye 1 beye
3958 | Prior ->
3959 begin match state.layout with
3960 | l :: _ ->
3961 if l.pagey != 0
3962 then (
3963 state.mode <- Birdseye (
3964 oconf, leftx, l.pageno, hooverpageno, anchor
3966 gotopage1 l.pageno 0;
3968 else (
3969 let layout = layout state.x (state.y-state.winh)
3970 state.winw
3971 (pgh state.layout) in
3972 match layout with
3973 | [] -> gotoxy state.x (clamp (-state.winh))
3974 | l :: _ ->
3975 state.mode <- Birdseye (
3976 oconf, leftx, l.pageno, hooverpageno, anchor
3978 gotopage1 l.pageno 0
3981 | [] -> gotoxy state.x (clamp (-state.winh))
3982 end;
3984 | Next ->
3985 begin match List.rev state.layout with
3986 | l :: _ ->
3987 let layout = layout state.x
3988 (state.y + (pgh state.layout))
3989 state.winw state.winh in
3990 begin match layout with
3991 | [] ->
3992 let incr = l.pageh - l.pagevh in
3993 if incr = 0
3994 then (
3995 state.mode <-
3996 Birdseye (
3997 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
3999 postRedisplay "birdseye pagedown";
4001 else gotoxy state.x (clamp (incr + conf.interpagespace*2));
4003 | l :: _ ->
4004 state.mode <-
4005 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
4006 gotopage1 l.pageno 0;
4009 | [] -> gotoxy state.x (clamp state.winh)
4010 end;
4012 | Home ->
4013 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
4014 gotopage1 0 0
4016 | End ->
4017 let pageno = state.pagecount - 1 in
4018 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
4019 if not (pagevisible state.layout pageno)
4020 then
4021 let h =
4022 match List.rev state.pdims with
4023 | [] -> state.winh
4024 | (_, _, h, _) :: _ -> h
4026 gotoxy
4027 state.x
4028 (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
4029 else postRedisplay "birdseye end";
4031 | Delete|Insert|Ascii _|Code _|Ctrl _|Fn _|Backspace -> viewkeyboard key mask
4034 let drawpage l =
4035 let color =
4036 match state.mode with
4037 | Textentry _ -> scalecolor 0.4
4038 | LinkNav _ | View -> scalecolor 1.0
4039 | Birdseye (_, _, pageno, hooverpageno, _) ->
4040 if l.pageno = hooverpageno
4041 then scalecolor 0.9
4042 else (
4043 if l.pageno = pageno
4044 then (
4045 let c = scalecolor 1.0 in
4046 GlDraw.color c;
4047 GlDraw.line_width 3.0;
4048 let dispx = l.pagedispx in
4049 linerect
4050 (float (dispx-1)) (float (l.pagedispy-1))
4051 (float (dispx+l.pagevw+1))
4052 (float (l.pagedispy+l.pagevh+1));
4053 GlDraw.line_width 1.0;
4056 else scalecolor 0.8
4059 drawtiles l color;
4062 let postdrawpage l linkindexbase =
4063 match getopaque l.pageno with
4064 | Some opaque ->
4065 if tileready l l.pagex l.pagey
4066 then
4067 let x = l.pagedispx - l.pagex
4068 and y = l.pagedispy - l.pagey in
4069 let hlmask =
4070 match conf.columns with
4071 | Csingle _ | Cmulti _ ->
4072 (if conf.hlinks then 1 else 0)
4073 + (if state.glinks
4074 && not (isbirdseye state.mode) then 2 else 0)
4075 | Csplit _ -> 0
4077 let s =
4078 match state.mode with
4079 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
4080 | Textentry _
4081 | Birdseye _
4082 | View
4083 | LinkNav _ -> E.s
4085 Hashtbl.find_all state.prects l.pageno |>
4086 List.iter (fun vals -> drawprect opaque x y vals);
4087 let n = postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize) in
4088 if n < 0
4089 then (Glutils.redisplay := true; 0)
4090 else n
4091 else 0
4092 | _ -> 0
4095 let scrollindicator () =
4096 let sbw, ph, sh = state.uioh#scrollph in
4097 let sbh, pw, sw = state.uioh#scrollpw in
4099 let x0,x1,hx0 =
4100 if conf.leftscroll
4101 then (0, sbw, sbw)
4102 else ((state.winw - sbw), state.winw, 0)
4105 Gl.enable `blend;
4106 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
4107 let (r, g, b, alpha) = conf.sbarcolor in
4108 GlDraw.color (r, g, b) ~alpha;
4109 filledrect (float x0) 0. (float x1) (float state.winh);
4110 filledrect
4111 (float hx0) (float (state.winh - sbh))
4112 (float (hx0 + state.winw)) (float state.winh);
4113 let (r, g, b, alpha) = conf.sbarhndlcolor in
4114 GlDraw.color (r, g, b) ~alpha;
4116 filledrect (float x0) ph (float x1) (ph +. sh);
4117 let pw = pw +. float hx0 in
4118 filledrect pw (float (state.winh - sbh)) (pw +. sw) (float state.winh);
4119 Gl.disable `blend;
4122 let showsel () =
4123 match state.mstate with
4124 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ -> ()
4125 | Msel ((x0, y0), (x1, y1)) ->
4126 let identify opaque l px py = Some (opaque, l.pageno, px, py) in
4127 let o0,n0,px0,py0 = onppundermouse identify x0 y0 (~< E.s, -1, 0, 0) in
4128 let _o1,n1,px1,py1 = onppundermouse identify x1 y1 (~< E.s, -1, 0, 0) in
4129 if n0 != -1 && n0 = n1 then seltext o0 (px0, py0, px1, py1);
4132 let showrects = function
4133 | [] -> ()
4134 | rects ->
4135 Gl.enable `blend;
4136 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
4137 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
4138 List.iter
4139 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
4140 List.iter (fun l ->
4141 if l.pageno = pageno
4142 then
4143 let dx = float (l.pagedispx - l.pagex) in
4144 let dy = float (l.pagedispy - l.pagey) in
4145 let r, g, b, alpha = c in
4146 GlDraw.color (r, g, b) ~alpha;
4147 filledrect2
4148 (x0+.dx) (y0+.dy)
4149 (x1+.dx) (y1+.dy)
4150 (x3+.dx) (y3+.dy)
4151 (x2+.dx) (y2+.dy);
4152 ) state.layout
4153 ) rects;
4154 Gl.disable `blend;
4157 let display () =
4158 GlDraw.color (scalecolor2 conf.bgcolor);
4159 GlClear.color (scalecolor2 conf.bgcolor);
4160 GlClear.clear [`color];
4161 List.iter drawpage state.layout;
4162 let rects =
4163 match state.mode with
4164 | LinkNav (Ltexact (pageno, linkno)) ->
4165 begin match getopaque pageno with
4166 | Some opaque ->
4167 let x0, y0, x1, y1 = getlinkrect opaque linkno in
4168 let color = (0.0, 0.0, 0.5, 0.5) in
4169 (pageno, color,
4170 (float x0, float y0,
4171 float x1, float y0,
4172 float x1, float y1,
4173 float x0, float y1)
4174 ) :: state.rects
4175 | None -> state.rects
4177 | LinkNav (Ltgendir _) | LinkNav (Ltnotready _)
4178 | Birdseye _
4179 | Textentry _
4180 | View -> state.rects
4182 showrects rects;
4183 let rec postloop linkindexbase = function
4184 | l :: rest ->
4185 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
4186 postloop linkindexbase rest
4187 | [] -> ()
4189 showsel ();
4190 postloop 0 state.layout;
4191 state.uioh#display;
4192 begin match state.mstate with
4193 | Mzoomrect ((x0, y0), (x1, y1)) ->
4194 Gl.enable `blend;
4195 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
4196 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
4197 filledrect (float x0) (float y0) (float x1) (float y1);
4198 Gl.disable `blend;
4199 | Msel _
4200 | Mpan _
4201 | Mscrolly | Mscrollx
4202 | Mzoom _
4203 | Mnone -> ()
4204 end;
4205 enttext ();
4206 scrollindicator ();
4207 Wsi.swapb ();
4210 let display () =
4211 match state.reload with
4212 | Some (x, y, t) ->
4213 if x != state.x || y != state.y || abs_float @@ now () -. t > 0.5
4214 || (state.layout != [] && layoutready state.layout)
4215 then (
4216 state.reload <- None;
4217 display ()
4219 | None -> display ()
4222 let zoomrect x y x1 y1 =
4223 let x0 = min x x1
4224 and x1 = max x x1
4225 and y0 = min y y1 in
4226 let zoom = (float state.w) /. float (x1 - x0) in
4227 let margin =
4228 let simple () =
4229 if state.w < state.winw
4230 then (state.winw - state.w) / 2
4231 else 0
4233 match conf.fitmodel with
4234 | FitWidth | FitProportional -> simple ()
4235 | FitPage ->
4236 match conf.columns with
4237 | Csplit _ ->
4238 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
4239 | Cmulti _ | Csingle _ -> simple ()
4241 gotoxy ((state.x + margin) - x0) (state.y + y0);
4242 state.anchor <- getanchor ();
4243 setzoom zoom;
4244 resetmstate ();
4247 let annot inline x y =
4248 match unproject x y with
4249 | Some (opaque, n, ux, uy) ->
4250 let add text =
4251 addannot opaque ux uy text;
4252 wcmd "freepage %s" (~> opaque);
4253 Hashtbl.remove state.pagemap (n, state.gen);
4254 flushtiles ();
4255 gotoxy state.x state.y
4257 if inline
4258 then
4259 let ondone s = add s in
4260 let mode = state.mode in
4261 state.mode <- Textentry (
4262 ("annotation: ", E.s, None, textentry, ondone, true),
4263 fun _ -> state.mode <- mode);
4264 state.text <- E.s;
4265 enttext ();
4266 postRedisplay "annot"
4267 else add @@ getusertext E.s
4268 | _ -> ()
4271 let zoomblock x y =
4272 let g opaque l px py =
4273 match rectofblock opaque px py with
4274 | Some a ->
4275 let x0 = a.(0) -. 20. in
4276 let x1 = a.(1) +. 20. in
4277 let y0 = a.(2) -. 20. in
4278 let zoom = (float state.w) /. (x1 -. x0) in
4279 let pagey = getpagey l.pageno in
4280 let margin = (state.w - l.pagew)/2 in
4281 let nx = -truncate x0 - margin in
4282 gotoxy nx (pagey + truncate y0);
4283 state.anchor <- getanchor ();
4284 setzoom zoom;
4285 None
4286 | None -> None
4288 match conf.columns with
4289 | Csplit _ ->
4290 impmsg "block zooming does not work properly in split columns mode"
4291 | Cmulti _ | Csingle _ -> onppundermouse g x y ()
4294 let scrollx x =
4295 let winw = state.winw - 1 in
4296 let s = float x /. float winw in
4297 let destx = truncate (float (state.w + winw) *. s) in
4298 gotoxy (winw - destx) state.y;
4299 state.mstate <- Mscrollx;
4302 let scrolly y =
4303 let s = float y /. float state.winh in
4304 let desty = truncate (s *. float (maxy ())) in
4305 gotoxy state.x desty;
4306 state.mstate <- Mscrolly;
4309 let viewmulticlick clicks x y mask =
4310 let g opaque l px py =
4311 let mark =
4312 match clicks with
4313 | 2 -> Mark_word
4314 | 3 -> Mark_line
4315 | 4 -> Mark_block
4316 | _ -> Mark_page
4318 if markunder opaque px py mark
4319 then (
4320 Some (fun () ->
4321 let dopipe cmd =
4322 match getopaque l.pageno with
4323 | None -> ()
4324 | Some opaque -> pipesel opaque cmd
4326 state.roam <- (fun () -> dopipe conf.paxcmd);
4327 if not (Wsi.withctrl mask) then dopipe conf.selcmd;
4330 else None
4332 postRedisplay "viewmulticlick";
4333 onppundermouse g x y (fun () -> impmsg "nothing to select") ();
4336 let canselect () =
4337 match conf.columns with
4338 | Csplit _ -> false
4339 | Csingle _ | Cmulti _ -> conf.angle mod 360 = 0
4342 let viewmouse button down x y mask =
4343 match button with
4344 | n when (n == 4 || n == 5) && not down ->
4345 if Wsi.withctrl mask
4346 then (
4347 let incr =
4348 if n = 5
4349 then if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
4350 else if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
4352 let fx, fy =
4353 match state.mstate with
4354 | Mzoom (oldn, _, pos) when n = oldn -> pos
4355 | Mzoomrect _ | Mnone | Mpan _
4356 | Msel _ | Mscrollx | Mscrolly | Mzoom _ -> (x, y)
4358 let zoom = conf.zoom -. incr in
4359 state.mstate <- Mzoom (n, 0, (x, y));
4360 if false && abs (fx - x) > 5 || abs (fy - y) > 5
4361 then pivotzoom ~x ~y zoom
4362 else pivotzoom zoom
4364 else (
4365 match state.autoscroll with
4366 | Some step -> setautoscrollspeed step (n=4)
4367 | None ->
4368 if conf.wheelbypage || conf.presentation
4369 then (
4370 if n = 4
4371 then prevpage ()
4372 else nextpage ()
4374 else
4375 let incr = if n = 4 then -conf.scrollstep else conf.scrollstep in
4376 let incr = incr * 2 in
4377 let y = clamp incr in
4378 gotoxy state.x y
4381 | n when (n = 6 || n = 7) && not down && canpan () ->
4382 let x =
4383 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep) in
4384 gotoxy x state.y
4386 | 1 when Wsi.withshift mask ->
4387 state.mstate <- Mnone;
4388 if not down
4389 then (
4390 match unproject x y with
4391 | None -> ()
4392 | Some (_, pageno, ux, uy) ->
4393 let cmd = Printf.sprintf
4394 "%s %s %d %d %d"
4395 conf.stcmd state.path pageno ux uy
4397 match spawn cmd [] with
4398 | exception exn ->
4399 impmsg "execution of synctex command(%S) failed: %S"
4400 conf.stcmd @@ exntos exn
4401 | _pid -> ()
4404 | 1 when Wsi.withctrl mask ->
4405 if down
4406 then (
4407 Wsi.setcursor Wsi.CURSOR_FLEUR;
4408 state.mstate <- Mpan (x, y)
4410 else state.mstate <- Mnone
4412 | 3 ->
4413 if down
4414 then (
4415 if Wsi.withshift mask
4416 then (
4417 annot conf.annotinline x y;
4418 postRedisplay "addannot"
4420 else
4421 let p = (x, y) in
4422 Wsi.setcursor Wsi.CURSOR_CYCLE;
4423 state.mstate <- Mzoomrect (p, p)
4425 else (
4426 match state.mstate with
4427 | Mzoomrect ((x0, y0), _) ->
4428 if abs (x-x0) > 10 && abs (y - y0) > 10
4429 then zoomrect x0 y0 x y
4430 else (
4431 resetmstate ();
4432 postRedisplay "kill accidental zoom rect";
4434 | Msel _
4435 | Mpan _
4436 | Mscrolly | Mscrollx
4437 | Mzoom _
4438 | Mnone -> resetmstate ()
4441 | 1 when vscrollhit x ->
4442 if down
4443 then
4444 let _, position, sh = state.uioh#scrollph in
4445 if y > truncate position && y < truncate (position +. sh)
4446 then state.mstate <- Mscrolly
4447 else scrolly y
4448 else state.mstate <- Mnone
4450 | 1 when y > state.winh - hscrollh () ->
4451 if down
4452 then
4453 let _, position, sw = state.uioh#scrollpw in
4454 if x > truncate position && x < truncate (position +. sw)
4455 then state.mstate <- Mscrollx
4456 else scrollx x
4457 else state.mstate <- Mnone
4459 | 1 when state.bzoom -> if not down then zoomblock x y
4461 | 1 ->
4462 let dest = if down then getunder x y else Unone in
4463 begin match dest with
4464 | Ulinkuri _ -> gotounder dest
4465 | Unone when down ->
4466 Wsi.setcursor Wsi.CURSOR_FLEUR;
4467 state.mstate <- Mpan (x, y);
4468 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
4469 | Unone | Utext _ ->
4470 if down
4471 then (
4472 if canselect ()
4473 then (
4474 state.mstate <- Msel ((x, y), (x, y));
4475 postRedisplay "mouse select";
4478 else (
4479 match state.mstate with
4480 | Mnone -> ()
4481 | Mzoom _ | Mscrollx | Mscrolly -> state.mstate <- Mnone
4482 | Mzoomrect ((x0, y0), _) -> zoomrect x0 y0 x y
4483 | Mpan _ ->
4484 Wsi.setcursor Wsi.CURSOR_INHERIT;
4485 state.mstate <- Mnone
4486 | Msel ((x0, y0), (x1, y1)) ->
4487 let rec loop = function
4488 | [] -> ()
4489 | l :: rest ->
4490 let inside =
4491 let a0 = l.pagedispy in
4492 let a1 = a0 + l.pagevh in
4493 let b0 = l.pagedispx in
4494 let b1 = b0 + l.pagevw in
4495 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
4496 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
4498 if inside
4499 then
4500 match getopaque l.pageno with
4501 | Some opaque ->
4502 let dosel cmd () =
4503 pipef ~closew:false "Msel"
4504 (fun w ->
4505 copysel w opaque;
4506 postRedisplay "Msel") cmd
4508 dosel conf.selcmd ();
4509 state.roam <- dosel conf.paxcmd;
4510 | None -> ()
4511 else loop rest
4513 loop state.layout;
4514 resetmstate ();
4517 | _ -> ()
4520 let birdseyemouse button down x y mask
4521 (conf, leftx, _, hooverpageno, anchor) =
4522 match button with
4523 | 1 when down ->
4524 let rec loop = function
4525 | [] -> ()
4526 | l :: rest ->
4527 if y > l.pagedispy && y < l.pagedispy + l.pagevh
4528 && x > l.pagedispx && x < l.pagedispx + l.pagevw
4529 then
4530 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false
4531 else loop rest
4533 loop state.layout
4534 | 3 -> ()
4535 | _ -> viewmouse button down x y mask
4538 let uioh = object
4539 method display = ()
4541 method key key mask =
4542 begin match state.mode with
4543 | Textentry textentry -> textentrykeyboard key mask textentry
4544 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
4545 | View -> viewkeyboard key mask
4546 | LinkNav linknav -> linknavkeyboard key mask linknav
4547 end;
4548 state.uioh
4550 method button button bstate x y mask =
4551 begin match state.mode with
4552 | LinkNav _ | View -> viewmouse button bstate x y mask
4553 | Birdseye beye -> birdseyemouse button bstate x y mask beye
4554 | Textentry _ -> ()
4555 end;
4556 state.uioh
4558 method multiclick clicks x y mask =
4559 begin match state.mode with
4560 | LinkNav _ | View -> viewmulticlick clicks x y mask
4561 | Birdseye _ | Textentry _ -> ()
4562 end;
4563 state.uioh
4565 method motion x y =
4566 begin match state.mode with
4567 | Textentry _ -> ()
4568 | View | Birdseye _ | LinkNav _ ->
4569 match state.mstate with
4570 | Mzoom _ | Mnone -> ()
4571 | Mpan (x0, y0) ->
4572 let dx = x - x0
4573 and dy = y0 - y in
4574 state.mstate <- Mpan (x, y);
4575 let x = if canpan () then panbound (state.x + dx) else state.x in
4576 let y = clamp dy in
4577 gotoxy x y
4579 | Msel (a, _) ->
4580 state.mstate <- Msel (a, (x, y));
4581 postRedisplay "motion select";
4583 | Mscrolly ->
4584 let y = min state.winh (max 0 y) in
4585 scrolly y
4587 | Mscrollx ->
4588 let x = min state.winw (max 0 x) in
4589 scrollx x
4591 | Mzoomrect (p0, _) ->
4592 state.mstate <- Mzoomrect (p0, (x, y));
4593 postRedisplay "motion zoomrect";
4594 end;
4595 state.uioh
4597 method pmotion x y =
4598 begin match state.mode with
4599 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
4600 let rec loop = function
4601 | [] ->
4602 if hooverpageno != -1
4603 then (
4604 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
4605 postRedisplay "pmotion birdseye no hoover";
4607 | l :: rest ->
4608 if y > l.pagedispy && y < l.pagedispy + l.pagevh
4609 && x > l.pagedispx && x < l.pagedispx + l.pagevw
4610 then (
4611 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
4612 postRedisplay "pmotion birdseye hoover";
4614 else loop rest
4616 loop state.layout
4618 | Textentry _ -> ()
4620 | LinkNav _ | View ->
4621 match state.mstate with
4622 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ -> ()
4623 | Mnone ->
4624 updateunder x y;
4625 if canselect ()
4626 then
4627 match conf.pax with
4628 | None -> ()
4629 | Some past ->
4630 let now = now () in
4631 let delta = now -. past in
4632 if delta > 0.01
4633 then paxunder x y
4634 else conf.pax <- Some now
4635 end;
4636 state.uioh
4638 method infochanged _ = ()
4640 method scrollph =
4641 let maxy = maxy () in
4642 let p, h =
4643 if maxy = 0
4644 then 0.0, float state.winh
4645 else scrollph state.y maxy
4647 vscrollw (), p, h
4649 method scrollpw =
4650 let fwinw = float (state.winw - vscrollw ()) in
4651 let sw =
4652 let sw = fwinw /. float state.w in
4653 let sw = fwinw *. sw in
4654 max sw (float conf.scrollh)
4656 let position =
4657 let maxx = state.w + state.winw in
4658 let x = state.winw - state.x in
4659 let percent = float x /. float maxx in
4660 (fwinw -. sw) *. percent
4662 hscrollh (), position, sw
4664 method modehash =
4665 let modename =
4666 match state.mode with
4667 | LinkNav _ -> "links"
4668 | Textentry _ -> "textentry"
4669 | Birdseye _ -> "birdseye"
4670 | View -> "view"
4672 findkeyhash conf modename
4674 method eformsgs = true
4675 method alwaysscrolly = false
4676 method scroll dx dy =
4677 let x = if canpan () then panbound (state.x + dx) else state.x in
4678 gotoxy x (clamp (2 * dy));
4679 state.uioh
4680 method zoom z x y =
4681 pivotzoom ~x ~y (conf.zoom *. exp z);
4682 end;;
4684 let addrect pageno r g b a x0 y0 x1 y1 =
4685 Hashtbl.add state.prects pageno [|r; g; b; a; x0; y0; x1; y1|];
4688 let ract cmds =
4689 let cl = splitatchar cmds ' ' in
4690 let scan s fmt f =
4691 try Scanf.sscanf s fmt f
4692 with exn -> adderrfmt "remote exec" "error processing '%S': %s\n"
4693 cmds @@ exntos exn
4695 let rectx s pageno (r, g, b, a) x0 y0 x1 y1 =
4696 vlog "%s page %d color (%f %f %f %f) x0,y0,x1,y1 = %f %f %f %f"
4697 s pageno r g b a x0 y0 x1 y1;
4698 onpagerect
4699 pageno
4700 (fun w h ->
4701 let _,w1,h1,_ = getpagedim pageno in
4702 let sw = float w1 /. float w
4703 and sh = float h1 /. float h in
4704 let x0s = x0 *. sw
4705 and x1s = x1 *. sw
4706 and y0s = y0 *. sh
4707 and y1s = y1 *. sh in
4708 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
4709 let color = (r, g, b, a) in
4710 if conf.verbose then debugrect rect;
4711 state.rects <- (pageno, color, rect) :: state.rects;
4712 postRedisplay s;
4715 match cl with
4716 | "reload", "" -> reload ()
4717 | "goto", args ->
4718 scan args "%u %f %f"
4719 (fun pageno x y ->
4720 let cmd, _ = state.geomcmds in
4721 if emptystr cmd
4722 then gotopagexy pageno x y
4723 else
4724 let f prevf () =
4725 gotopagexy pageno x y;
4726 prevf ()
4728 state.reprf <- f state.reprf
4730 | "goto1", args -> scan args "%u %f" gotopage
4731 | "gotor", args -> scan args "%S" gotoremote
4732 | "rect", args ->
4733 scan args "%u %u %f %f %f %f"
4734 (fun pageno c x0 y0 x1 y1 ->
4735 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
4736 rectx "rect" pageno color x0 y0 x1 y1;
4738 | "prect", args ->
4739 scan args "%u %f %f %f %f %f %f %f %f"
4740 (fun pageno r g b alpha x0 y0 x1 y1 ->
4741 addrect pageno r g b alpha x0 y0 x1 y1;
4742 postRedisplay "prect"
4744 | "pgoto", args ->
4745 scan args "%u %f %f"
4746 (fun pageno x y ->
4747 let optopaque =
4748 match getopaque pageno with
4749 | Some opaque -> opaque
4750 | None -> ~< E.s
4752 pgoto optopaque pageno x y;
4753 let rec fixx = function
4754 | [] -> ()
4755 | l :: rest ->
4756 if l.pageno = pageno
4757 then gotoxy (state.x - l.pagedispx) state.y
4758 else fixx rest
4760 let layout =
4761 let mult =
4762 match conf.columns with
4763 | Csingle _ | Csplit _ -> 1
4764 | Cmulti ((n, _, _), _) -> n
4766 layout 0 state.y (state.winw * mult) state.winh
4768 fixx layout
4770 | "activatewin", "" -> Wsi.activatewin ()
4771 | "quit", "" -> raise Quit
4772 | "keys", keys ->
4773 begin try
4774 let l = Config.keys_of_string keys in
4775 List.iter (fun (k, m) -> keyboard k m) l
4776 with exn -> adderrfmt "error processing keys" "`%S': %s\n"
4777 cmds @@ exntos exn
4779 | "clearrects", "" ->
4780 Hashtbl.clear state.prects;
4781 postRedisplay "clearrects"
4782 | _ ->
4783 adderrfmt "remote command"
4784 "error processing remote command: %S\n" cmds;
4787 let remote =
4788 let scratch = Bytes.create 80 in
4789 let buf = Buffer.create 80 in
4790 fun fd ->
4791 match tempfailureretry (Unix.read fd scratch 0) 80 with
4792 | exception Unix.Unix_error (Unix.EAGAIN, _, _) -> None
4793 | 0 ->
4794 Unix.close fd;
4795 if Buffer.length buf > 0
4796 then (
4797 let s = Buffer.contents buf in
4798 Buffer.clear buf;
4799 ract s;
4801 None
4802 | n ->
4803 let rec eat ppos =
4804 let nlpos =
4805 match Bytes.index_from scratch ppos '\n' with
4806 | pos -> if pos >= n then -1 else pos
4807 | exception Not_found -> -1
4809 if nlpos >= 0
4810 then (
4811 Buffer.add_subbytes buf scratch ppos (nlpos-ppos);
4812 let s = Buffer.contents buf in
4813 Buffer.clear buf;
4814 ract s;
4815 eat (nlpos+1);
4817 else (
4818 Buffer.add_subbytes buf scratch ppos (n-ppos);
4819 Some fd
4821 in eat 0
4824 let remoteopen path =
4825 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
4826 with exn ->
4827 adderrfmt "remoteopen" "error opening %S: %s" path @@ exntos exn;
4828 None
4831 let () =
4832 Utils.vlogf := (fun s -> if conf.verbose then prerr_endline s else ignore s);
4833 let gcconfig = ref false in
4834 let trimcachepath = ref E.s in
4835 let rcmdpath = ref E.s in
4836 let pageno = ref None in
4837 let openlast = ref false in
4838 let doreap = ref false in
4839 let csspath = ref None in
4840 selfexec := Sys.executable_name;
4841 Arg.parse
4842 (Arg.align
4843 [("-p", Arg.String (fun s -> state.password <- s),
4844 "<password> Set password");
4846 ("-f", Arg.String
4847 (fun s ->
4848 Config.fontpath := s;
4849 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
4851 "<path> Set path to the user interface font");
4853 ("-c", Arg.String
4854 (fun s ->
4855 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
4856 Config.confpath := s),
4857 "<path> Set path to the configuration file");
4859 ("-last", Arg.Set openlast, " Open last document");
4861 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
4862 "<page-number> Jump to page");
4864 ("-tcf", Arg.String (fun s -> trimcachepath := s),
4865 "<path> Set path to the trim cache file");
4867 ("-dest", Arg.String (fun s -> state.nameddest <- s),
4868 "<named-destination> Set named destination");
4870 ("-remote", Arg.String (fun s -> rcmdpath := s),
4871 "<path> Set path to the source of remote commands");
4873 ("-gc", Arg.Set gcconfig, " Collect config garbage");
4875 ("-v", Arg.Unit (fun () ->
4876 Printf.printf
4877 "%s\nconfiguration file: %s\n"
4878 (Help.version ())
4879 Config.defconfpath;
4880 exit 0), " Print version and exit");
4882 ("-css", Arg.String (fun s -> csspath := Some s),
4883 "<path> Set path to the style sheet to use with EPUB/HTML");
4885 ("-origin", Arg.String (fun s -> state.origin <- s),
4886 "<origin> <undocumented>");
4888 ("-no-title", Arg.Set ignoredoctitlte, " ignore document title");
4889 ("-layout-height", Arg.Set_int layouth,
4890 "<height> layout height html/epub/etc (-1, 0, N)");
4893 (fun s -> state.path <- s)
4894 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:");
4896 let histmode = emptystr state.path && not !openlast in
4898 if not (Config.load !openlast)
4899 then dolog "failed to load configuration";
4901 begin match !pageno with
4902 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
4903 | None -> ()
4904 end;
4906 fillhelp ();
4907 if !gcconfig
4908 then (
4909 Config.gc ();
4910 exit 0
4913 let mu =
4914 object (self)
4915 val mutable m_clicks = 0
4916 val mutable m_click_x = 0
4917 val mutable m_click_y = 0
4918 val mutable m_lastclicktime = infinity
4920 method private cleanup =
4921 state.roam <- noroam;
4922 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap
4923 method expose = postRedisplay "expose"
4924 method visible v =
4925 let name =
4926 match v with
4927 | Wsi.Unobscured -> "unobscured"
4928 | Wsi.PartiallyObscured -> "partiallyobscured"
4929 | Wsi.FullyObscured -> "fullyobscured"
4931 vlog "visibility change %s" name
4932 method display = display ()
4933 method map mapped = vlog "mapped %b" mapped
4934 method reshape w h =
4935 self#cleanup;
4936 reshape w h
4937 method mouse b d x y m =
4938 if d && canselect ()
4939 then (
4941 * http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx
4943 m_click_x <- x;
4944 m_click_y <- y;
4945 if b = 1
4946 then (
4947 let t = now () in
4948 if abs x - m_click_x > 10
4949 || abs y - m_click_y > 10
4950 || abs_float (t -. m_lastclicktime) > 0.3
4951 then m_clicks <- 0;
4952 m_clicks <- m_clicks + 1;
4953 m_lastclicktime <- t;
4954 if m_clicks = 1
4955 then (
4956 self#cleanup;
4957 postRedisplay "cleanup";
4958 state.uioh <- state.uioh#button b d x y m;
4960 else state.uioh <- state.uioh#multiclick m_clicks x y m
4962 else (
4963 self#cleanup;
4964 m_clicks <- 0;
4965 m_lastclicktime <- infinity;
4966 state.uioh <- state.uioh#button b d x y m
4969 else state.uioh <- state.uioh#button b d x y m
4970 method motion x y =
4971 state.mpos <- (x, y);
4972 state.uioh <- state.uioh#motion x y
4973 method pmotion x y =
4974 state.mpos <- (x, y);
4975 state.uioh <- state.uioh#pmotion x y
4976 method key k m =
4977 vlog "k=%#x m=%#x" k m;
4978 let mascm = m land (
4979 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
4980 ) in
4981 let keyboard k m =
4982 let x = state.x and y = state.y in
4983 keyboard k m;
4984 if x != state.x || y != state.y then self#cleanup
4986 match state.keystate with
4987 | KSnone ->
4988 let km = k, mascm in
4989 begin
4990 match
4991 let modehash = state.uioh#modehash in
4992 try Hashtbl.find modehash km
4993 with Not_found ->
4994 try Hashtbl.find (findkeyhash conf "global") km
4995 with Not_found -> KMinsrt (k, m)
4996 with
4997 | KMinsrt (k, m) -> keyboard k m
4998 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
4999 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
5001 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
5002 List.iter (fun (k, m) -> keyboard k m) insrt;
5003 state.keystate <- KSnone
5004 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
5005 state.keystate <- KSinto (keys, insrt)
5006 | KSinto _ -> state.keystate <- KSnone
5008 method enter x y =
5009 state.mpos <- (x, y);
5010 state.uioh <- state.uioh#pmotion x y
5011 method leave = state.mpos <- (-1, -1)
5012 method winstate wsl = state.winstate <- wsl
5013 method quit : 'a. 'a = raise Quit
5014 method scroll dx dy = state.uioh <- state.uioh#scroll dx dy
5015 method zoom z x y = state.uioh#zoom z x y
5016 method opendoc path =
5017 state.mode <- View;
5018 state.uioh <- uioh;
5019 postRedisplay "opendoc";
5020 opendoc path state.password
5023 let wsfd, winw, winh = Wsi.init mu conf.cwinw conf.cwinh platform in
5024 state.wsfd <- wsfd;
5026 if not @@ List.exists GlMisc.check_extension
5027 [ "GL_ARB_texture_rectangle"
5028 ; "GL_EXT_texture_recangle"
5029 ; "GL_NV_texture_rectangle" ]
5030 then (dolog "OpenGL does not suppport rectangular textures"; exit 1);
5032 let cs, ss =
5033 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
5034 | exception exn ->
5035 dolog "socketpair failed: %s" @@ exntos exn;
5036 exit 1
5037 | (r, w) ->
5038 cloexec r;
5039 cloexec w;
5040 r, w
5043 setcheckers conf.checkers;
5044 begin match !csspath with
5045 | None -> ()
5046 | Some "" -> conf.css <- E.s
5047 | Some path ->
5048 let css = filecontents path in
5049 let l = String.length css in
5050 conf.css <-
5051 if substratis css (l-2) "\r\n"
5052 then String.sub css 0 (l-2)
5053 else (if css.[l-1] = '\n' then String.sub css 0 (l-1) else css)
5054 end;
5055 init cs (
5056 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
5057 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
5058 !Config.fontpath, !trimcachepath
5060 List.iter GlArray.enable [`texture_coord; `vertex];
5061 GlTex.env (`color conf.texturecolor);
5062 state.ss <- ss;
5063 reshape ~firsttime:true winw winh;
5064 state.uioh <- uioh;
5065 if histmode
5066 then (
5067 Wsi.settitle "llpp (history)";
5068 enterhistmode ();
5070 else (
5071 state.text <- "Opening " ^ (mbtoutf8 state.path);
5072 opendoc state.path state.password;
5074 display ();
5075 Wsi.mapwin ();
5076 Wsi.setcursor Wsi.CURSOR_INHERIT;
5077 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
5079 let rec reap () =
5080 match Unix.waitpid [Unix.WNOHANG] ~-1 with
5081 | exception (Unix.Unix_error (Unix.ECHILD, _, _)) -> ()
5082 | exception exn -> dolog "Unix.waitpid: %s" @@ exntos exn
5083 | 0, _ -> ()
5084 | _pid, _status -> reap ()
5086 Sys.set_signal Sys.sigchld (Sys.Signal_handle (fun _ -> doreap := true));
5088 let optrfd =
5089 ref (if nonemptystr !rcmdpath then remoteopen !rcmdpath else None)
5092 let rec loop deadline =
5093 if !doreap
5094 then (
5095 doreap := false;
5096 reap ()
5098 let r = [state.ss; state.wsfd] in
5099 let r =
5100 match !optrfd with
5101 | None -> r
5102 | Some fd -> fd :: r
5104 if !redisplay
5105 then (
5106 Glutils.redisplay := false;
5107 display ();
5109 let timeout =
5110 let now = now () in
5111 if deadline > now
5112 then (
5113 if deadline = infinity
5114 then ~-.1.0
5115 else max 0.0 (deadline -. now)
5117 else 0.0
5119 let r, _, _ =
5120 try Unix.select r [] [] timeout
5121 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
5123 begin match r with
5124 | [] ->
5125 let newdeadline =
5126 match state.autoscroll with
5127 | Some step when step != 0 ->
5128 if state.slideshow land 1 = 1
5129 then (
5130 if state.slideshow land 2 = 0
5131 then state.slideshow <- state.slideshow lor 2
5132 else if step < 0 then prevpage () else nextpage ();
5133 deadline +. (float (abs step))
5135 else
5136 let y = state.y + step in
5137 let fy = if conf.maxhfit then state.winh else 0 in
5138 let y =
5139 if y < 0
5140 then state.maxy - fy
5141 else if y >= state.maxy - fy then 0 else y
5143 gotoxy state.x y;
5144 deadline +. 0.01
5145 | _ -> infinity
5147 loop newdeadline
5149 | l ->
5150 let rec checkfds = function
5151 | [] -> ()
5152 | fd :: rest when fd = state.ss ->
5153 let cmd = rcmd state.ss in
5154 act cmd;
5155 checkfds rest
5157 | fd :: rest when fd = state.wsfd ->
5158 Wsi.readresp fd;
5159 checkfds rest
5161 | fd :: rest when Some fd = !optrfd ->
5162 begin match remote fd with
5163 | None -> optrfd := remoteopen !rcmdpath;
5164 | opt -> optrfd := opt
5165 end;
5166 checkfds rest
5168 | _ :: rest ->
5169 dolog "select returned unknown descriptor";
5170 checkfds rest
5172 checkfds l;
5173 let newdeadline =
5174 let deadline1 =
5175 if deadline = infinity
5176 then now () +. 0.01
5177 else deadline
5179 match state.autoscroll with
5180 | Some step when step != 0 -> deadline1
5181 | _ -> infinity
5183 loop newdeadline
5184 end;
5186 match loop infinity with
5187 | exception Quit ->
5188 Config.save leavebirdseye;
5189 if hasunsavedchanges ()
5190 then save ()
5191 | _ -> error "umpossible - infinity reached"