Don't warn about absent _implicit_ .Xauthority
[llpp.git] / main.ml
blob4fbd9a8f28a0dea8e35533e33559aaddb9ca4744
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 let titlepath =
782 if emptystr state.origin
783 then path
784 else state.origin
786 Wsi.settitle ("llpp " ^ mbtoutf8 (Filename.basename titlepath));
787 wcmd "open %d %d %s\000%s\000%s\000"
788 (btod conf.usedoccss) !layouth
789 path password conf.css;
790 invalidate "reqlayout"
791 (fun () ->
792 wcmd "reqlayout %d %d %d %s\000"
793 conf.angle (FMTE.to_int conf.fitmodel)
794 (stateh state.winh) state.nameddest
796 fillhelp ();
799 let reload () =
800 state.anchor <- getanchor ();
801 state.reload <- Some (state.x, state.y, now ());
802 opendoc state.path state.password;
805 let scalecolor c = let c = c *. conf.colorscale in (c, c, c);;
806 let scalecolor2 (r, g, b) =
807 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
810 let docolumns columns =
811 match columns with
812 | Csingle _ ->
813 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
814 let rec loop pageno pdimno pdim y ph pdims =
815 if pageno != state.pagecount
816 then
817 let pdimno, ((_, w, h, xoff) as pdim), pdims =
818 match pdims with
819 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
820 pdimno+1, pdim, rest
821 | _ ->
822 pdimno, pdim, pdims
824 let x = max 0 (((state.winw - w) / 2) - xoff) in
825 let y =
826 y + (if conf.presentation
827 then (if pageno = 0 then calcips h else calcips ph + calcips h)
828 else (if pageno = 0 then 0 else conf.interpagespace))
830 a.(pageno) <- (pdimno, x, y, pdim);
831 loop (pageno+1) pdimno pdim (y + h) h pdims
833 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
834 conf.columns <- Csingle a;
836 | Cmulti ((columns, coverA, coverB), _) ->
837 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
838 let rec loop pageno pdimno pdim x y rowh pdims =
839 let rec fixrow m =
840 if m = pageno then () else
841 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
842 if h < rowh
843 then (
844 let y = y + (rowh - h) / 2 in
845 a.(m) <- (pdimno, x, y, pdim);
847 fixrow (m+1)
849 if pageno = state.pagecount
850 then fixrow (((pageno - 1) / columns) * columns)
851 else
852 let pdimno, ((_, w, h, xoff) as pdim), pdims =
853 match pdims with
854 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
855 pdimno+1, pdim, rest
856 | _ -> pdimno, pdim, pdims
858 let x, y, rowh' =
859 if pageno = coverA - 1 || pageno = state.pagecount - coverB
860 then (
861 let x = (state.winw - w) / 2 in
862 let ips =
863 if conf.presentation then calcips h else conf.interpagespace in
864 x, y + ips + rowh, h
866 else (
867 if (pageno - coverA) mod columns = 0
868 then (
869 let x = max 0 (state.winw - state.w) / 2 in
870 let y =
871 if conf.presentation
872 then
873 let ips = calcips h in
874 y + (if pageno = 0 then 0 else calcips rowh + ips)
875 else
876 y + (if pageno = 0 then 0 else conf.interpagespace)
878 x, y + rowh, h
880 else x, y, max rowh h
883 let y =
884 if pageno > 1 && (pageno - coverA) mod columns = 0
885 then (
886 let y =
887 if pageno = columns && conf.presentation
888 then (
889 let ips = calcips rowh in
890 for i = 0 to pred columns
892 let (pdimno, x, y, pdim) = a.(i) in
893 a.(i) <- (pdimno, x, y+ips, pdim)
894 done;
895 y+ips;
897 else y
899 fixrow (pageno - columns);
902 else y
904 a.(pageno) <- (pdimno, x, y, pdim);
905 let x = x + w + xoff*2 + conf.interpagespace in
906 loop (pageno+1) pdimno pdim x y rowh' pdims
908 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
909 conf.columns <- Cmulti ((columns, coverA, coverB), a);
911 | Csplit (c, _) ->
912 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
913 let rec loop pageno pdimno pdim y pdims =
914 if pageno != state.pagecount
915 then
916 let pdimno, ((_, w, h, _) as pdim), pdims =
917 match pdims with
918 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
919 pdimno+1, pdim, rest
920 | _ -> pdimno, pdim, pdims
922 let cw = w / c in
923 let rec loop1 n x y =
924 if n = c then y else (
925 a.(pageno*c + n) <- (pdimno, x, y, pdim);
926 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
929 let y = loop1 0 0 y in
930 loop (pageno+1) pdimno pdim y pdims
932 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
933 conf.columns <- Csplit (c, a);
936 let represent () =
937 docolumns conf.columns;
938 state.maxy <- calcheight ();
939 if state.reprf == noreprf
940 then (
941 match state.mode with
942 | Birdseye (_, _, pageno, _, _) ->
943 let y, h = getpageyh pageno in
944 let top = (state.winh - h) / 2 in
945 gotoxy state.x (max 0 (y - top))
946 | Textentry _ | View | LinkNav _ ->
947 let y = getanchory state.anchor in
948 let y = min y (state.maxy - state.winh) in
949 gotoxy state.x y;
951 else (
952 state.reprf ();
953 state.reprf <- noreprf;
957 let reshape ?(firsttime=false) w h =
958 GlDraw.viewport ~x:0 ~y:0 ~w ~h;
959 if not firsttime && nogeomcmds state.geomcmds
960 then state.anchor <- getanchor ();
962 state.winw <- w;
963 let w = truncate (float w *. conf.zoom) in
964 let w = max w 2 in
965 state.winh <- h;
966 setfontsize fstate.fontsize;
967 GlMat.mode `modelview;
968 GlMat.load_identity ();
970 GlMat.mode `projection;
971 GlMat.load_identity ();
972 GlMat.rotate ~x:1.0 ~angle:180.0 ();
973 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
974 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
976 let relx =
977 if conf.zoom <= 1.0
978 then 0.0
979 else float state.x /. float state.w
981 invalidate "geometry"
982 (fun () ->
983 state.w <- w;
984 if not firsttime
985 then state.x <- truncate (relx *. float w);
986 let w =
987 match conf.columns with
988 | Csingle _ -> w
989 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
990 | Csplit (c, _) -> w * c
992 wcmd "geometry %d %d %d" w (stateh h) (FMTE.to_int conf.fitmodel)
996 let gctiles () =
997 let len = Queue.length state.tilelru in
998 let layout = lazy (if conf.preload
999 then preloadlayout state.x state.y state.winw state.winh
1000 else state.layout) in
1001 let rec loop qpos =
1002 if state.memused > conf.memlimit
1003 then (
1004 if qpos < len
1005 then
1006 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1007 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1008 let (_, pw, ph, _) = getpagedim n in
1009 if gen = state.gen
1010 && colorspace = conf.colorspace
1011 && angle = conf.angle
1012 && pagew = pw
1013 && pageh = ph
1014 && (
1015 let x = col*conf.tilew and y = row*conf.tileh in
1016 tilevisible (Lazy.force_val layout) n x y
1018 then Queue.push lruitem state.tilelru
1019 else (
1020 freepbo p;
1021 wcmd "freetile %s" (~> p);
1022 state.memused <- state.memused - s;
1023 state.uioh#infochanged Memused;
1024 Hashtbl.remove state.tilemap k;
1026 loop (qpos+1)
1029 loop 0
1032 let onpagerect pageno f =
1033 let b =
1034 match conf.columns with
1035 | Cmulti (_, b) -> b
1036 | Csingle b -> b
1037 | Csplit (_, b) -> b
1039 if pageno >= 0 && pageno < Array.length b
1040 then
1041 let (_, _, _, (_, w, h, _)) = b.(pageno) in
1042 f w h
1045 let gotopagexy1 pageno x y =
1046 let _,w1,h1,leftx = getpagedim pageno in
1047 let top = y /. (float h1) in
1048 let left = x /. (float w1) in
1049 let py, w, h = getpageywh pageno in
1050 let wh = state.winh in
1051 let x = left *. (float w) in
1052 let x = leftx + state.x + truncate x in
1053 let sx =
1054 if x < 0 || x >= state.winw
1055 then state.x - x
1056 else state.x
1058 let pdy = truncate (top *. float h) in
1059 let y' = py + pdy in
1060 let dy = y' - state.y in
1061 let sy =
1062 if x != state.x || not (dy > 0 && dy < wh)
1063 then (
1064 if conf.presentation
1065 then
1066 if abs (py - y') > wh
1067 then y'
1068 else py
1069 else y';
1071 else state.y
1073 if state.x != sx || state.y != sy
1074 then gotoxy sx sy
1075 else gotoxy state.x state.y;
1078 let gotopagexy pageno x y =
1079 match state.mode with
1080 | Birdseye _ -> gotopage pageno 0.0
1081 | Textentry _ | View | LinkNav _ -> gotopagexy1 pageno x y
1084 let getpassword () =
1085 let passcmd = getenvdef "LLPP_ASKPASS" conf.passcmd in
1086 if emptystr passcmd
1087 then E.s
1088 else getcmdoutput (fun s ->
1089 impmsg "error getting password: %s" s;
1090 dolog "%s" s) passcmd;
1093 let pgoto opaque pageno x y =
1094 let pdimno = getpdimno pageno in
1095 let x, y = project opaque pageno pdimno x y in
1096 gotopagexy pageno x y;
1099 let act cmds =
1100 (* dolog "%S" cmds; *)
1101 let spl = splitatchar cmds ' ' in
1102 let scan s fmt f =
1103 try Scanf.sscanf s fmt f
1104 with exn ->
1105 dolog "error processing '%S': %s" cmds @@ exntos exn;
1106 exit 1
1108 let addoutline outline =
1109 match state.currently with
1110 | Outlining outlines -> state.currently <- Outlining (outline :: outlines)
1111 | Idle -> state.currently <- Outlining [outline]
1112 | Loading _ | Tiling _ ->
1113 dolog "invalid outlining state";
1114 logcurrently state.currently
1116 match spl with
1117 | "clear", "" ->
1118 state.pdims <- [];
1119 state.uioh#infochanged Pdim;
1121 | "clearrects", "" ->
1122 state.rects <- state.rects1;
1123 postRedisplay "clearrects";
1125 | "continue", args ->
1126 let n = scan args "%u" (fun n -> n) in
1127 state.pagecount <- n;
1128 begin match state.currently with
1129 | Outlining l ->
1130 state.currently <- Idle;
1131 state.outlines <- Array.of_list (List.rev l)
1132 | Idle | Loading _ | Tiling _ -> ()
1133 end;
1135 let cur, cmds = state.geomcmds in
1136 if emptystr cur then error "empty geomcmd";
1138 begin match List.rev cmds with
1139 | [] ->
1140 state.geomcmds <- E.s, [];
1141 represent ();
1142 | (s, f) :: rest ->
1143 f ();
1144 state.geomcmds <- s, List.rev rest;
1145 end;
1146 postRedisplay "continue";
1148 | "msg", args ->
1149 showtext ' ' args
1151 | "vmsg", args ->
1152 if conf.verbose then showtext ' ' args
1154 | "emsg", args ->
1155 Buffer.add_string state.errmsgs args;
1156 state.newerrmsgs <- true;
1157 postRedisplay "error message"
1159 | "progress", args ->
1160 let progress, text =
1161 scan args "%f %n"
1162 (fun f pos -> f, String.sub args pos (String.length args - pos))
1164 state.text <- text;
1165 state.progress <- progress;
1166 postRedisplay "progress"
1168 | "firstmatch", args ->
1169 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1170 scan args "%u %d %f %f %f %f %f %f %f %f"
1171 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1172 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1174 let y = (getpagey pageno) + truncate y0 in
1175 let x =
1176 if (state.x < - truncate x0) || (state.x > state.winw - truncate x1)
1177 then state.winw/2 - truncate (x0 /. 2. +. x1 /. 2.)
1178 else state.x
1180 addnav ();
1181 gotoxy x y;
1182 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1183 state.rects1 <- [pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)]
1185 | "match", args ->
1186 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1187 scan args "%u %d %f %f %f %f %f %f %f %f"
1188 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1189 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1191 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1192 state.rects1 <-
1193 (pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1195 | "page", args ->
1196 let pageopaques, t = scan args "%s %f" (fun p t -> p, t) in
1197 let pageopaque = ~< pageopaques in
1198 begin match state.currently with
1199 | Loading (l, gen) ->
1200 vlog "page %d took %f sec" l.pageno t;
1201 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1202 let preloadedpages =
1203 if conf.preload
1204 then preloadlayout state.x state.y state.winw state.winh
1205 else state.layout
1207 let evict () =
1208 let set = List.fold_left (fun s l -> IntSet.add l.pageno s)
1209 IntSet.empty preloadedpages
1211 let evictedpages =
1212 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1213 if not (IntSet.mem pageno set)
1214 then (
1215 wcmd "freepage %s" (~> opaque);
1216 key :: accu
1218 else accu
1219 ) state.pagemap []
1221 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1223 evict ();
1224 state.currently <- Idle;
1225 if gen = state.gen
1226 then (
1227 tilepage l.pageno pageopaque state.layout;
1228 load state.layout;
1229 load preloadedpages;
1230 let visible = pagevisible state.layout l.pageno in
1231 if visible
1232 then (
1233 match state.mode with
1234 | LinkNav (Ltnotready (pageno, dir)) ->
1235 if pageno = l.pageno
1236 then (
1237 let link =
1238 let ld =
1239 if dir = 0
1240 then LDfirstvisible (l.pagex, l.pagey, dir)
1241 else if dir > 0 then LDfirst else LDlast
1243 findlink pageopaque ld
1245 match link with
1246 | Lnotfound -> ()
1247 | Lfound n ->
1248 showlinktype (getlink pageopaque n);
1249 state.mode <- LinkNav (Ltexact (l.pageno, n))
1251 | LinkNav (Ltgendir _)
1252 | LinkNav (Ltexact _)
1253 | View
1254 | Birdseye _
1255 | Textentry _ -> ()
1258 if visible && layoutready state.layout
1259 then postRedisplay "page";
1262 | Idle | Tiling _ | Outlining _ ->
1263 dolog "Inconsistent loading state";
1264 logcurrently state.currently;
1265 exit 1
1268 | "tile" , args ->
1269 let (x, y, opaques, size, t) =
1270 scan args "%u %u %s %u %f" (fun x y p size t -> (x, y, p, size, t))
1272 let opaque = ~< opaques in
1273 begin match state.currently with
1274 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1275 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1277 unmappbo opaque;
1278 if tilew != conf.tilew || tileh != conf.tileh
1279 then (
1280 wcmd "freetile %s" (~> opaque);
1281 state.currently <- Idle;
1282 load state.layout;
1284 else (
1285 puttileopaque l col row gen cs angle opaque size t;
1286 state.memused <- state.memused + size;
1287 state.uioh#infochanged Memused;
1288 gctiles ();
1289 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1290 opaque, size) state.tilelru;
1292 state.currently <- Idle;
1293 if gen = state.gen
1294 && conf.colorspace = cs
1295 && conf.angle = angle
1296 && tilevisible state.layout l.pageno x y
1297 then conttiling l.pageno pageopaque;
1299 preload state.layout;
1300 if gen = state.gen
1301 && conf.colorspace = cs
1302 && conf.angle = angle
1303 && tilevisible state.layout l.pageno x y
1304 && layoutready state.layout
1305 then postRedisplay "tile nothrottle";
1308 | Idle | Loading _ | Outlining _ ->
1309 dolog "Inconsistent tiling state";
1310 logcurrently state.currently;
1311 exit 1
1314 | "pdim", args ->
1315 let (n, w, h, _) as pdim =
1316 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1318 let pdim =
1319 match conf.fitmodel with
1320 | FitWidth -> pdim
1321 | FitPage | FitProportional ->
1322 match conf.columns with
1323 | Csplit _ -> (n, w, h, 0)
1324 | Csingle _ | Cmulti _ -> pdim
1326 state.pdims <- pdim :: state.pdims;
1327 state.uioh#infochanged Pdim
1329 | "o", args ->
1330 let (l, n, t, h, pos) =
1331 scan args "%u %u %d %u %n" (fun l n t h pos -> l, n, t, h, pos)
1333 let s = String.sub args pos (String.length args - pos) in
1334 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
1336 | "ou", args ->
1337 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
1338 let s = String.sub args pos len in
1339 let pos2 = pos + len + 1 in
1340 let uri = String.sub args pos2 (String.length args - pos2) in
1341 addoutline (s, l, Ouri uri)
1343 | "on", args ->
1344 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
1345 let s = String.sub args pos (String.length args - pos) in
1346 addoutline (s, l, Onone)
1348 | "a", args ->
1349 let (n, l, t) = scan args "%u %d %d" (fun n l t -> n, l, t) in
1350 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
1352 | "info", args ->
1353 let c, v = splitatchar args '\t' in
1354 let s =
1355 if nonemptystr v
1356 then
1357 if c = "Title"
1358 then (
1359 conf.title <- v;
1360 if not !ignoredoctitlte then Wsi.settitle v;
1361 args
1363 else
1364 if let len = String.length c in
1365 len > 6 && ((String.sub c (len-4) 4) = "date")
1366 then (
1367 if String.length v >= 7 && v.[0] = 'D' && v.[1] = ':'
1368 then
1369 let b = Buffer.create 10 in
1370 Printf.bprintf b "%s\t" c;
1371 let sub p l c =
1373 Buffer.add_substring b v p l;
1374 Buffer.add_char b c;
1375 with exn -> Buffer.add_string b @@ exntos exn
1377 sub 2 4 '/';
1378 sub 6 2 '/';
1379 sub 8 2 ' ';
1380 sub 10 2 ':';
1381 sub 12 2 ':';
1382 sub 14 2 ' ';
1383 Buffer.add_char b '[';
1384 Buffer.add_string b v;
1385 Buffer.add_char b ']';
1386 Buffer.contents b
1387 else args
1389 else args
1390 else args
1392 state.docinfo <- (1, s) :: state.docinfo
1394 | "infoend", "" ->
1395 state.docinfo <- List.rev state.docinfo;
1396 state.uioh#infochanged Docinfo
1398 | "pass", args ->
1399 if args = "fail"
1400 then Wsi.settitle "Wrong password";
1401 let password = getpassword () in
1402 if emptystr password
1403 then error "document is password protected"
1404 else opendoc state.path password
1406 | _ -> error "unknown cmd `%S'" cmds
1409 let onhist cb =
1410 let rc = cb.rc in
1411 let action = function
1412 | HCprev -> cbget cb ~-1
1413 | HCnext -> cbget cb 1
1414 | HCfirst -> cbget cb ~-(cb.rc)
1415 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1416 and cancel () = cb.rc <- rc
1417 in (action, cancel)
1420 let search pattern forward =
1421 match conf.columns with
1422 | Csplit _ -> impmsg "searching does not work properly in split columns mode"
1423 | Csingle _ | Cmulti _ ->
1424 if nonemptystr pattern
1425 then
1426 let pn, py =
1427 match state.layout with
1428 | [] -> 0, 0
1429 | l :: _ -> l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1431 wcmd "search %d %d %d %d,%s\000"
1432 (btod conf.icase) pn py (btod forward) pattern;
1435 let intentry text key =
1436 let text =
1437 if emptystr text && key = Keys.Ascii '-'
1438 then addchar text '-'
1439 else
1440 match [@warning "-4"] key with
1441 | Keys.Ascii ('0'..'9' as c) -> addchar text c
1442 | _ ->
1443 state.text <- "invalid key";
1444 text
1446 TEcont text
1449 let linknact f s =
1450 if nonemptystr s
1451 then
1452 let n =
1453 let l = String.length s in
1454 let rec loop pos n =
1455 if pos = l
1456 then n
1457 else
1458 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
1459 loop (pos+1) (n*26 + m)
1460 in loop 0 0
1462 let rec loop n = function
1463 | [] -> ()
1464 | l :: rest ->
1465 match getopaque l.pageno with
1466 | None -> loop n rest
1467 | Some opaque ->
1468 let m = getlinkcount opaque in
1469 if n < m
1470 then
1471 let under = getlink opaque n in
1472 f under
1473 else loop (n-m) rest
1475 loop n state.layout;
1478 let linknentry text key = match [@warning "-4"] key with
1479 | Keys.Ascii ('a' .. 'z' as c) ->
1480 let text = addchar text c in
1481 linknact (fun under -> state.text <- undertext under) text;
1482 TEcont text
1483 | _ ->
1484 state.text <- Printf.sprintf "invalid key %s" @@ Keys.to_string key;
1485 TEcont text
1488 let textentry text key = match [@warning "-4"] key with
1489 | Keys.Ascii c -> TEcont (addchar text c)
1490 | Keys.Code c -> TEcont (text ^ toutf8 c)
1491 | _ -> TEcont text
1494 let reqlayout angle fitmodel =
1495 if nogeomcmds state.geomcmds
1496 then state.anchor <- getanchor ();
1497 conf.angle <- angle mod 360;
1498 if conf.angle != 0
1499 then (
1500 match state.mode with
1501 | LinkNav _ -> state.mode <- View
1502 | Birdseye _ | Textentry _ | View -> ()
1504 conf.fitmodel <- fitmodel;
1505 invalidate "reqlayout"
1506 (fun () -> wcmd "reqlayout %d %d %d"
1507 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh));
1510 let settrim trimmargins trimfuzz =
1511 if nogeomcmds state.geomcmds
1512 then state.anchor <- getanchor ();
1513 conf.trimmargins <- trimmargins;
1514 conf.trimfuzz <- trimfuzz;
1515 let x0, y0, x1, y1 = trimfuzz in
1516 invalidate "settrim"
1517 (fun () -> wcmd "settrim %d %d %d %d %d"
1518 (btod conf.trimmargins) x0 y0 x1 y1);
1519 flushpages ();
1522 let setzoom zoom =
1523 let zoom = max 0.0001 zoom in
1524 if zoom <> conf.zoom
1525 then (
1526 state.prevzoom <- (conf.zoom, state.x);
1527 conf.zoom <- zoom;
1528 reshape state.winw state.winh;
1529 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
1533 let pivotzoom ?(vw=min state.w state.winw)
1534 ?(vh=min (state.maxy-state.y) state.winh)
1535 ?(x=vw/2) ?(y=vh/2) zoom =
1536 let w = float state.w /. zoom in
1537 let hw = w /. 2.0 in
1538 let ratio = float vh /. float vw in
1539 let hh = hw *. ratio in
1540 let x0 = float x -. hw
1541 and y0 = float y -. hh in
1542 gotoxy (state.x - truncate x0) (state.y + truncate y0);
1543 setzoom zoom;
1546 let pivotzoom ?vw ?vh ?x ?y zoom =
1547 if nogeomcmds state.geomcmds
1548 then
1549 if zoom > 1.0
1550 then pivotzoom ?vw ?vh ?x ?y zoom
1551 else setzoom zoom
1554 let setcolumns mode columns coverA coverB =
1555 state.prevcolumns <- Some (conf.columns, conf.zoom);
1556 if columns < 0
1557 then (
1558 if isbirdseye mode
1559 then impmsg "split mode doesn't work in bird's eye"
1560 else (
1561 conf.columns <- Csplit (-columns, E.a);
1562 state.x <- 0;
1563 conf.zoom <- 1.0;
1566 else (
1567 if columns < 2
1568 then (
1569 conf.columns <- Csingle E.a;
1570 state.x <- 0;
1571 setzoom 1.0;
1573 else (
1574 conf.columns <- Cmulti ((columns, coverA, coverB), E.a);
1575 conf.zoom <- 1.0;
1578 reshape state.winw state.winh;
1581 let resetmstate () =
1582 state.mstate <- Mnone;
1583 Wsi.setcursor Wsi.CURSOR_INHERIT;
1586 let enterbirdseye () =
1587 let zoom = float conf.thumbw /. float state.winw in
1588 let birdseyepageno =
1589 let cy = state.winh / 2 in
1590 let fold = function
1591 | [] -> 0
1592 | l :: rest ->
1593 let rec fold best = function
1594 | [] -> best.pageno
1595 | l :: rest ->
1596 let d = cy - (l.pagedispy + l.pagevh/2)
1597 and dbest = cy - (best.pagedispy + best.pagevh/2) in
1598 if abs d < abs dbest
1599 then fold l rest
1600 else best.pageno
1601 in fold l rest
1603 fold state.layout
1605 state.mode <-
1606 Birdseye (
1607 { conf with zoom = conf.zoom },
1608 state.x, birdseyepageno, -1, getanchor ()
1610 resetmstate ();
1611 conf.zoom <- zoom;
1612 conf.presentation <- false;
1613 conf.interpagespace <- 10;
1614 conf.hlinks <- false;
1615 conf.fitmodel <- FitPage;
1616 state.x <- 0;
1617 conf.columns <- (
1618 match conf.beyecolumns with
1619 | Some c ->
1620 conf.zoom <- 1.0;
1621 Cmulti ((c, 0, 0), E.a)
1622 | None -> Csingle E.a
1624 if conf.verbose
1625 then state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1626 (100.0*.zoom)
1627 else state.text <- E.s;
1628 reshape state.winw state.winh;
1631 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
1632 state.mode <- View;
1633 conf.zoom <- c.zoom;
1634 conf.presentation <- c.presentation;
1635 conf.interpagespace <- c.interpagespace;
1636 conf.hlinks <- c.hlinks;
1637 conf.fitmodel <- c.fitmodel;
1638 conf.beyecolumns <- (
1639 match conf.columns with
1640 | Cmulti ((c, _, _), _) -> Some c
1641 | Csingle _ -> None
1642 | Csplit _ -> error "leaving bird's eye split mode"
1644 conf.columns <- (
1645 match c.columns with
1646 | Cmulti (c, _) -> Cmulti (c, E.a)
1647 | Csingle _ -> Csingle E.a
1648 | Csplit (c, _) -> Csplit (c, E.a)
1650 if conf.verbose
1651 then state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1652 (100.0*.conf.zoom);
1653 reshape state.winw state.winh;
1654 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
1655 state.x <- leftx;
1658 let togglebirdseye () =
1659 match state.mode with
1660 | Birdseye vals -> leavebirdseye vals true
1661 | View -> enterbirdseye ()
1662 | Textentry _ | LinkNav _ -> ()
1665 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
1666 let pageno = max 0 (pageno - incr) in
1667 let rec loop = function
1668 | [] -> gotopage1 pageno 0
1669 | l :: _ when l.pageno = pageno ->
1670 if l.pagedispy >= 0 && l.pagey = 0
1671 then postRedisplay "upbirdseye"
1672 else gotopage1 pageno 0
1673 | _ :: rest -> loop rest
1675 loop state.layout;
1676 state.text <- E.s;
1677 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
1680 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
1681 let pageno = min (state.pagecount - 1) (pageno + incr) in
1682 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
1683 let rec loop = function
1684 | [] ->
1685 let y, h = getpageyh pageno in
1686 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
1687 gotoxy state.x (clamp dy)
1688 | l :: _ when l.pageno = pageno ->
1689 if l.pagevh != l.pageh
1690 then gotoxy state.x (clamp (l.pageh - l.pagevh + conf.interpagespace))
1691 else postRedisplay "downbirdseye"
1692 | _ :: rest -> loop rest
1694 loop state.layout;
1695 state.text <- E.s;
1698 let optentry mode _ key =
1699 let btos b = if b then "on" else "off" in
1700 match [@warning "-4"] key with
1701 | Keys.Ascii 'C' ->
1702 let ondone s =
1704 let n, a, b = multicolumns_of_string s in
1705 setcolumns mode n a b;
1706 with exn ->
1707 state.text <- Printf.sprintf "bad columns `%s': %s" s @@ exntos exn
1709 TEswitch ("columns: ", E.s, None, textentry, ondone, true)
1711 | Keys.Ascii 'Z' ->
1712 let ondone s =
1714 let zoom = float (int_of_string s) /. 100.0 in
1715 pivotzoom zoom
1716 with exn ->
1717 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
1719 TEswitch ("zoom: ", E.s, None, intentry, ondone, true)
1721 | Keys.Ascii 'i' ->
1722 conf.icase <- not conf.icase;
1723 TEdone ("case insensitive search " ^ (btos conf.icase))
1725 | Keys.Ascii 'v' ->
1726 conf.verbose <- not conf.verbose;
1727 TEdone ("verbose " ^ (btos conf.verbose))
1729 | Keys.Ascii 'd' ->
1730 conf.debug <- not conf.debug;
1731 TEdone ("debug " ^ (btos conf.debug))
1733 | Keys.Ascii 'f' ->
1734 conf.underinfo <- not conf.underinfo;
1735 TEdone ("underinfo " ^ btos conf.underinfo)
1737 | Keys.Ascii 'T' ->
1738 settrim (not conf.trimmargins) conf.trimfuzz;
1739 TEdone ("trim margins " ^ btos conf.trimmargins)
1741 | Keys.Ascii 'I' ->
1742 conf.invert <- not conf.invert;
1743 TEdone ("invert colors " ^ btos conf.invert)
1745 | Keys.Ascii 'x' ->
1746 let ondone s =
1747 cbput state.hists.sel s;
1748 conf.selcmd <- s;
1750 TEswitch ("selection command: ", E.s, Some (onhist state.hists.sel),
1751 textentry, ondone, true)
1753 | Keys.Ascii 'M' ->
1754 if conf.pax == None
1755 then conf.pax <- Some 0.0
1756 else conf.pax <- None;
1757 TEdone ("PAX " ^ btos (conf.pax != None))
1759 | (Keys.Ascii c) ->
1760 state.text <- Printf.sprintf "bad option %d `%c'" (Char.code c) c;
1761 TEstop
1763 | _ -> TEcont state.text
1766 let adderrmsg src msg =
1767 Buffer.add_string state.errmsgs msg;
1768 state.newerrmsgs <- true;
1769 postRedisplay src
1772 let adderrfmt src fmt = Format.ksprintf (fun s -> adderrmsg src s) fmt;;
1774 class outlinelistview ~zebra ~source =
1775 let settext autonarrow s =
1776 if autonarrow
1777 then
1778 let ss = source#statestr in
1779 state.text <- if emptystr ss
1780 then "[" ^ s ^ "]"
1781 else "{" ^ ss ^ "} [" ^ s ^ "]"
1782 else state.text <- s
1784 object (self)
1785 inherit listview
1786 ~zebra
1787 ~helpmode:false
1788 ~source:(source :> lvsource)
1789 ~trusted:false
1790 ~modehash:(findkeyhash conf "outline")
1791 as super
1793 val m_autonarrow = false
1795 method! key key mask =
1796 let maxrows =
1797 if emptystr state.text
1798 then fstate.maxrows
1799 else fstate.maxrows - 2
1801 let calcfirst first active =
1802 if active > first
1803 then
1804 let rows = active - first in
1805 if rows > maxrows then active - maxrows else first
1806 else active
1808 let navigate incr =
1809 let active = m_active + incr in
1810 let active = bound active 0 (source#getitemcount - 1) in
1811 let first = calcfirst m_first active in
1812 postRedisplay "outline navigate";
1813 coe {< m_active = active; m_first = first >}
1815 let navscroll first =
1816 let active =
1817 let dist = m_active - first in
1818 if dist < 0
1819 then first
1820 else (
1821 if dist < maxrows
1822 then m_active
1823 else first + maxrows
1826 postRedisplay "outline navscroll";
1827 coe {< m_first = first; m_active = active >}
1829 let ctrl = Wsi.withctrl mask in
1830 let open Keys in
1831 match Wsi.kc2kt key with
1832 | Ascii 'a' when ctrl ->
1833 let text =
1834 if m_autonarrow
1835 then (
1836 source#denarrow;
1839 else (
1840 let pattern = source#renarrow in
1841 if nonemptystr m_qsearch
1842 then (source#narrow m_qsearch; m_qsearch)
1843 else pattern
1846 settext (not m_autonarrow) text;
1847 postRedisplay "toggle auto narrowing";
1848 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
1850 | Ascii '/' when emptystr m_qsearch && not m_autonarrow ->
1851 settext true E.s;
1852 postRedisplay "toggle auto narrowing";
1853 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
1855 | Ascii 'n' when ctrl ->
1856 source#narrow m_qsearch;
1857 if not m_autonarrow
1858 then source#add_narrow_pattern m_qsearch;
1859 postRedisplay "outline ctrl-n";
1860 coe {< m_first = 0; m_active = 0 >}
1862 | Ascii 'S' when ctrl ->
1863 let active = source#calcactive (getanchor ()) in
1864 let first = firstof m_first active in
1865 postRedisplay "outline ctrl-s";
1866 coe {< m_first = first; m_active = active >}
1868 | Ascii 'u' when ctrl ->
1869 postRedisplay "outline ctrl-u";
1870 if m_autonarrow && nonemptystr m_qsearch
1871 then (
1872 ignore (source#renarrow);
1873 settext m_autonarrow E.s;
1874 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
1876 else (
1877 source#del_narrow_pattern;
1878 let pattern = source#renarrow in
1879 let text =
1880 if emptystr pattern then E.s else "Narrowed to " ^ pattern
1882 settext m_autonarrow text;
1883 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
1886 | Ascii 'l' when ctrl ->
1887 let first = max 0 (m_active - (fstate.maxrows / 2)) in
1888 postRedisplay "outline ctrl-l";
1889 coe {< m_first = first >}
1891 | Ascii '\t' when m_autonarrow ->
1892 if nonemptystr m_qsearch
1893 then (
1894 postRedisplay "outline list view tab";
1895 source#add_narrow_pattern m_qsearch;
1896 settext true E.s;
1897 coe {< m_qsearch = E.s >}
1899 else coe self
1901 | Escape when m_autonarrow ->
1902 if nonemptystr m_qsearch
1903 then source#add_narrow_pattern m_qsearch;
1904 super#key key mask
1906 | Enter when m_autonarrow ->
1907 if nonemptystr m_qsearch
1908 then source#add_narrow_pattern m_qsearch;
1909 super#key key mask
1911 | (Ascii _ | Code _) when m_autonarrow ->
1912 let pattern = m_qsearch ^ toutf8 key in
1913 postRedisplay "outlinelistview autonarrow add";
1914 source#narrow pattern;
1915 settext true pattern;
1916 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
1918 | Backspace when m_autonarrow ->
1919 if emptystr m_qsearch
1920 then coe self
1921 else
1922 let pattern = withoutlastutf8 m_qsearch in
1923 postRedisplay "outlinelistview autonarrow backspace";
1924 ignore (source#renarrow);
1925 source#narrow pattern;
1926 settext true pattern;
1927 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
1929 | Up when ctrl -> navscroll (max 0 (m_first - 1))
1931 | Down when ctrl ->
1932 navscroll (min (source#getitemcount - 1) (m_first + 1))
1934 | Up -> navigate ~-1
1935 | Down -> navigate 1
1936 | Prior -> navigate ~-(fstate.maxrows)
1937 | Next -> navigate fstate.maxrows
1939 | Right ->
1940 let o =
1941 if ctrl
1942 then (
1943 postRedisplay "outline ctrl right";
1944 {< m_pan = m_pan + 1 >}
1946 else self#updownlevel 1
1948 coe o
1950 | Left ->
1951 let o =
1952 if ctrl
1953 then (
1954 postRedisplay "outline ctrl left";
1955 {< m_pan = m_pan - 1 >}
1957 else self#updownlevel ~-1
1959 coe o
1961 | Home ->
1962 postRedisplay "outline home";
1963 coe {< m_first = 0; m_active = 0 >}
1965 | End ->
1966 let active = source#getitemcount - 1 in
1967 let first = max 0 (active - fstate.maxrows) in
1968 postRedisplay "outline end";
1969 coe {< m_active = active; m_first = first >}
1971 | Delete|Escape|Insert|Enter|Ascii _|Code _|Ctrl _|Backspace|Fn _ ->
1972 super#key key mask
1973 end;;
1975 let genhistoutlines () =
1976 Config.gethist ()
1977 |> List.sort (fun (_, c1, _, _, _, _) (_, c2, _, _, _, _) ->
1978 compare c2.lastvisit c1.lastvisit)
1979 |> List.map (fun ((path, c, _, _, _, origin) as hist) ->
1980 let path = if nonemptystr origin then origin else path in
1981 let base = mbtoutf8 @@ Filename.basename path in
1982 (base ^ "\000" ^ c.title, 1, Ohistory hist)
1986 let gotohist (path, c, bookmarks, x, anchor, origin) =
1987 Config.save leavebirdseye;
1988 state.anchor <- anchor;
1989 state.bookmarks <- bookmarks;
1990 state.origin <- origin;
1991 state.x <- x;
1992 setconf conf c;
1993 let x0, y0, x1, y1 = conf.trimfuzz in
1994 wcmd "trimset %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1;
1995 reshape ~firsttime:true state.winw state.winh;
1996 opendoc path origin;
1997 setzoom c.zoom;
2000 let setcheckers enabled =
2001 match !checkerstexid with
2002 | None -> if enabled then checkerstexid := Some (makecheckers ())
2003 | Some id ->
2004 if not enabled
2005 then (
2006 GlTex.delete_texture id;
2007 checkerstexid := None;
2011 let describe_layout layout =
2012 let d =
2013 match layout with
2014 | [] -> "Page 0"
2015 | l :: [] -> Printf.sprintf "Page %d" (l.pageno+1)
2016 | l :: rest ->
2017 let rangestr a b =
2018 if a.pageno = b.pageno then Printf.sprintf "%d" (a.pageno+1)
2019 else Printf.sprintf "%d%s%d" (a.pageno+1)
2020 (if a.pageno+1 = b.pageno then ", " else Utf8syms.ellipsis)
2021 (b.pageno+1)
2023 let rec fold s la lb = function
2024 | [] -> Printf.sprintf "%s %s" s (rangestr la lb)
2025 | l :: rest when l.pageno = succ lb.pageno -> fold s la l rest
2026 | l :: rest -> fold (s ^ " " ^ rangestr la lb ^ ",") l l rest
2028 fold "Pages" l l rest
2030 let percent =
2031 let maxy = maxy () in
2032 if maxy <= 0
2033 then 100.
2034 else 100. *. (float state.y /. float maxy)
2036 Printf.sprintf "%s of %d [%.2f%%]" d state.pagecount percent
2039 let setpresentationmode v =
2040 let n = page_of_y state.y in
2041 state.anchor <- (n, 0.0, 1.0);
2042 conf.presentation <- v;
2043 if conf.fitmodel = FitPage
2044 then reqlayout conf.angle conf.fitmodel;
2045 represent ();
2048 let enterinfomode =
2049 let btos b = if b then Utf8syms.radical else E.s in
2050 let showextended = ref false in
2051 let showcolors = ref false in
2052 let leave mode _ = state.mode <- mode in
2053 let src =
2054 (object
2055 val mutable m_l = []
2056 val mutable m_a = E.a
2057 val mutable m_prev_uioh = nouioh
2058 val mutable m_prev_mode = View
2060 inherit lvsourcebase
2062 method reset prev_mode prev_uioh =
2063 m_a <- Array.of_list (List.rev m_l);
2064 m_l <- [];
2065 m_prev_mode <- prev_mode;
2066 m_prev_uioh <- prev_uioh;
2068 method int name get set =
2069 m_l <-
2070 (name, `int get, 1,
2071 Action (
2072 fun u ->
2073 let ondone s =
2074 try set (int_of_string s)
2075 with exn ->
2076 state.text <- Printf.sprintf "bad integer `%s': %s"
2077 s @@ exntos exn
2079 state.text <- E.s;
2080 let te = name ^ ": ", E.s, None, intentry, ondone, true in
2081 state.mode <- Textentry (te, leave m_prev_mode);
2083 )) :: m_l
2085 method int_with_suffix name get set =
2086 m_l <-
2087 (name, `intws get, 1,
2088 Action (
2089 fun u ->
2090 let ondone s =
2091 try set (int_of_string_with_suffix s)
2092 with exn ->
2093 state.text <- Printf.sprintf "bad integer `%s': %s"
2094 s @@ exntos exn
2096 state.text <- E.s;
2097 let te =
2098 name ^ ": ", E.s, None, intentry_with_suffix, ondone, true
2100 state.mode <- Textentry (te, leave m_prev_mode);
2102 )) :: m_l
2104 method bool ?(offset=1) ?(btos=btos) name get set =
2105 m_l <-
2106 (name, `bool (btos, get), offset, Action (
2107 fun u ->
2108 let v = get () in
2109 set (not v);
2111 )) :: m_l
2113 method color name get set =
2114 m_l <-
2115 (name, `color get, 1,
2116 Action (
2117 fun u ->
2118 let invalid = (nan, nan, nan) in
2119 let ondone s =
2120 let c =
2121 try color_of_string s
2122 with exn ->
2123 state.text <- Printf.sprintf "bad color `%s': %s"
2124 s @@ exntos exn;
2125 invalid
2127 if c <> invalid
2128 then set c;
2130 let te = name ^ ": ", E.s, None, textentry, ondone, true in
2131 state.text <- color_to_string (get ());
2132 state.mode <- Textentry (te, leave m_prev_mode);
2134 )) :: m_l
2136 method string name get set =
2137 m_l <-
2138 (name, `string get, 1,
2139 Action (
2140 fun u ->
2141 let ondone s = set s in
2142 let te = name ^ ": ", E.s, None, textentry, ondone, true in
2143 state.mode <- Textentry (te, leave m_prev_mode);
2145 )) :: m_l
2147 method colorspace name get set =
2148 m_l <-
2149 (name, `string get, 1,
2150 Action (
2151 fun _ ->
2152 let source =
2153 (object
2154 inherit lvsourcebase
2156 initializer
2157 m_active <- CSTE.to_int conf.colorspace;
2158 m_first <- 0;
2160 method getitemcount =
2161 Array.length CSTE.names
2162 method getitem n =
2163 (CSTE.names.(n), 0)
2164 method exit ~uioh ~cancel ~active ~first ~pan =
2165 ignore (uioh, first, pan);
2166 if not cancel then set active;
2167 None
2168 method hasaction _ = true
2169 end)
2171 state.text <- E.s;
2172 let modehash = findkeyhash conf "info" in
2173 coe (new listview ~zebra:false ~helpmode:false
2174 ~source ~trusted:true ~modehash)
2175 )) :: m_l
2177 method paxmark name get set =
2178 m_l <-
2179 (name, `string get, 1,
2180 Action (
2181 fun _ ->
2182 let source =
2183 (object
2184 inherit lvsourcebase
2186 initializer
2187 m_active <- MTE.to_int conf.paxmark;
2188 m_first <- 0;
2190 method getitemcount = Array.length MTE.names
2191 method getitem n = (MTE.names.(n), 0)
2192 method exit ~uioh ~cancel ~active ~first ~pan =
2193 ignore (uioh, first, pan);
2194 if not cancel then set active;
2195 None
2196 method hasaction _ = true
2197 end)
2199 state.text <- E.s;
2200 let modehash = findkeyhash conf "info" in
2201 coe (new listview ~zebra:false ~helpmode:false
2202 ~source ~trusted:true ~modehash)
2203 )) :: m_l
2205 method fitmodel name get set =
2206 m_l <-
2207 (name, `string get, 1,
2208 Action (
2209 fun _ ->
2210 let source =
2211 (object
2212 inherit lvsourcebase
2214 initializer
2215 m_active <- FMTE.to_int conf.fitmodel;
2216 m_first <- 0;
2218 method getitemcount = Array.length FMTE.names
2219 method getitem n = (FMTE.names.(n), 0)
2220 method exit ~uioh ~cancel ~active ~first ~pan =
2221 ignore (uioh, first, pan);
2222 if not cancel then set active;
2223 None
2224 method hasaction _ = true
2225 end)
2227 state.text <- E.s;
2228 let modehash = findkeyhash conf "info" in
2229 coe (new listview ~zebra:false ~helpmode:false
2230 ~source ~trusted:true ~modehash)
2231 )) :: m_l
2233 method caption s offset =
2234 m_l <- (s, `empty, offset, Noaction) :: m_l
2236 method caption2 s f offset =
2237 m_l <- (s, `string f, offset, Noaction) :: m_l
2239 method getitemcount = Array.length m_a
2241 method getitem n =
2242 let tostr = function
2243 | `int f -> string_of_int (f ())
2244 | `intws f -> string_with_suffix_of_int (f ())
2245 | `string f -> f ()
2246 | `color f -> color_to_string (f ())
2247 | `bool (btos, f) -> btos (f ())
2248 | `empty -> E.s
2250 let name, t, offset, _ = m_a.(n) in
2251 ((let s = tostr t in
2252 if nonemptystr s
2253 then Printf.sprintf "%s\t%s" name s
2254 else name),
2255 offset)
2257 method exit ~uioh ~cancel ~active ~first ~pan =
2258 let uiohopt =
2259 if not cancel
2260 then (
2261 let uioh =
2262 match m_a.(active) with
2263 | _, _, _, Action f -> f uioh
2264 | _, _, _, Noaction -> uioh
2266 Some uioh
2268 else None
2270 m_active <- active;
2271 m_first <- first;
2272 m_pan <- pan;
2273 uiohopt
2275 method hasaction n =
2276 match m_a.(n) with
2277 | _, _, _, Action _ -> true
2278 | _, _, _, Noaction -> false
2280 initializer m_active <- 1
2281 end)
2283 let rec fillsrc prevmode prevuioh =
2284 let sep () = src#caption E.s 0 in
2285 let colorp name get set =
2286 src#string name
2287 (fun () -> color_to_string (get ()))
2288 (fun v ->
2289 try set @@ color_of_string v
2290 with exn ->
2291 state.text <-
2292 Printf.sprintf "bad color `%s': %s" v @@ exntos exn
2295 let rgba name get set =
2296 src#string name
2297 (fun () -> get () |> rgba_to_string)
2298 (fun v ->
2299 try set @@ rgba_of_string v
2300 with exn ->
2301 state.text <-
2302 Printf.sprintf "bad color `%s': %s" v @@ exntos exn
2305 let oldmode = state.mode in
2306 let birdseye = isbirdseye state.mode in
2308 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
2310 src#bool "presentation mode"
2311 (fun () -> conf.presentation)
2312 (fun v -> setpresentationmode v);
2314 src#bool "ignore case in searches"
2315 (fun () -> conf.icase)
2316 (fun v -> conf.icase <- v);
2318 src#bool "preload"
2319 (fun () -> conf.preload)
2320 (fun v -> conf.preload <- v);
2322 src#bool "highlight links"
2323 (fun () -> conf.hlinks)
2324 (fun v -> conf.hlinks <- v);
2326 src#bool "under info"
2327 (fun () -> conf.underinfo)
2328 (fun v -> conf.underinfo <- v);
2330 src#fitmodel "fit model"
2331 (fun () -> FMTE.to_string conf.fitmodel)
2332 (fun v -> reqlayout conf.angle (FMTE.of_int v));
2334 src#bool "trim margins"
2335 (fun () -> conf.trimmargins)
2336 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
2338 sep ();
2339 src#int "inter-page space"
2340 (fun () -> conf.interpagespace)
2341 (fun n ->
2342 conf.interpagespace <- n;
2343 docolumns conf.columns;
2344 let pageno, py =
2345 match state.layout with
2346 | [] -> 0, 0
2347 | l :: _ -> l.pageno, l.pagey
2349 state.maxy <- calcheight ();
2350 let y = getpagey pageno in
2351 gotoxy state.x (y + py)
2354 src#int "page bias"
2355 (fun () -> conf.pagebias)
2356 (fun v -> conf.pagebias <- v);
2358 src#int "scroll step"
2359 (fun () -> conf.scrollstep)
2360 (fun n -> conf.scrollstep <- n);
2362 src#int "horizontal scroll step"
2363 (fun () -> conf.hscrollstep)
2364 (fun v -> conf.hscrollstep <- v);
2366 src#int "auto scroll step"
2367 (fun () ->
2368 match state.autoscroll with
2369 | Some step -> step
2370 | _ -> conf.autoscrollstep)
2371 (fun n ->
2372 let n = boundastep state.winh n in
2373 if state.autoscroll <> None
2374 then state.autoscroll <- Some n;
2375 conf.autoscrollstep <- n);
2377 src#int "zoom"
2378 (fun () -> truncate (conf.zoom *. 100.))
2379 (fun v -> pivotzoom ((float v) /. 100.));
2381 src#int "rotation"
2382 (fun () -> conf.angle)
2383 (fun v -> reqlayout v conf.fitmodel);
2385 src#int "scroll bar width"
2386 (fun () -> conf.scrollbw)
2387 (fun v ->
2388 conf.scrollbw <- v;
2389 reshape state.winw state.winh;
2392 src#int "scroll handle height"
2393 (fun () -> conf.scrollh)
2394 (fun v -> conf.scrollh <- v;);
2396 src#int "thumbnail width"
2397 (fun () -> conf.thumbw)
2398 (fun v ->
2399 conf.thumbw <- min 4096 v;
2400 match oldmode with
2401 | Birdseye beye ->
2402 leavebirdseye beye false;
2403 enterbirdseye ()
2404 | Textentry _
2405 | View
2406 | LinkNav _ -> ()
2409 let mode = state.mode in
2410 src#string "columns"
2411 (fun () ->
2412 match conf.columns with
2413 | Csingle _ -> "1"
2414 | Cmulti (multi, _) -> multicolumns_to_string multi
2415 | Csplit (count, _) -> "-" ^ string_of_int count
2417 (fun v ->
2418 let n, a, b = multicolumns_of_string v in
2419 setcolumns mode n a b);
2421 sep ();
2422 src#caption "Pixmap cache" 0;
2423 src#int_with_suffix "size (advisory)"
2424 (fun () -> conf.memlimit)
2425 (fun v -> conf.memlimit <- v);
2427 src#caption2 "used"
2428 (fun () ->
2429 Printf.sprintf "%s bytes, %d tiles"
2430 (string_with_suffix_of_int state.memused)
2431 (Hashtbl.length state.tilemap)) 1;
2433 sep ();
2434 src#caption "Layout" 0;
2435 src#caption2 "Dimension"
2436 (fun () -> Printf.sprintf "%dx%d (virtual %dx%d)"
2437 state.winw state.winh
2438 state.w state.maxy)
2440 if conf.debug
2441 then src#caption2 "Position" (fun () ->
2442 Printf.sprintf "%dx%d" state.x state.y
2444 else src#caption2 "Position" (fun () -> describe_layout state.layout) 1;
2446 sep ();
2447 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
2448 "Save these parameters as global defaults at exit"
2449 (fun () -> conf.bedefault)
2450 (fun v -> conf.bedefault <- v);
2452 sep ();
2453 let btos b = Utf8syms.(if b then lguillemet else rguillemet) in
2454 src#bool ~offset:0 ~btos "Extended parameters"
2455 (fun () -> !showextended)
2456 (fun v -> showextended := v; fillsrc prevmode prevuioh);
2457 if !showextended
2458 then (
2459 src#bool "checkers"
2460 (fun () -> conf.checkers)
2461 (fun v -> conf.checkers <- v; setcheckers v);
2462 src#bool "update cursor"
2463 (fun () -> conf.updatecurs)
2464 (fun v -> conf.updatecurs <- v);
2465 src#bool "scroll-bar on the left"
2466 (fun () -> conf.leftscroll)
2467 (fun v -> conf.leftscroll <- v);
2468 src#bool "verbose"
2469 (fun () -> conf.verbose)
2470 (fun v -> conf.verbose <- v);
2471 src#bool "invert colors"
2472 (fun () -> conf.invert)
2473 (fun v -> conf.invert <- v);
2474 src#bool "max fit"
2475 (fun () -> conf.maxhfit)
2476 (fun v -> conf.maxhfit <- v);
2477 src#bool "pax mode"
2478 (fun () -> conf.pax != None)
2479 (fun v ->
2480 if v
2481 then conf.pax <- Some (now ())
2482 else conf.pax <- None);
2483 src#string "uri launcher"
2484 (fun () -> conf.urilauncher)
2485 (fun v -> conf.urilauncher <- v);
2486 src#string "path launcher"
2487 (fun () -> conf.pathlauncher)
2488 (fun v -> conf.pathlauncher <- v);
2489 src#string "tile size"
2490 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
2491 (fun v ->
2493 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
2494 conf.tilew <- max 64 w;
2495 conf.tileh <- max 64 h;
2496 flushtiles ();
2497 with exn ->
2498 state.text <- Printf.sprintf "bad tile size `%s': %s"
2499 v @@ exntos exn
2501 src#int "texture count"
2502 (fun () -> conf.texcount)
2503 (fun v ->
2504 if realloctexts v
2505 then conf.texcount <- v
2506 else impmsg "failed to set texture count please retry later"
2508 src#int "slice height"
2509 (fun () -> conf.sliceheight)
2510 (fun v ->
2511 conf.sliceheight <- v;
2512 wcmd "sliceh %d" conf.sliceheight;
2514 src#int "anti-aliasing level"
2515 (fun () -> conf.aalevel)
2516 (fun v ->
2517 conf.aalevel <- bound v 0 8;
2518 state.anchor <- getanchor ();
2519 opendoc state.path state.password;
2521 src#string "page scroll scaling factor"
2522 (fun () -> string_of_float conf.pgscale)
2523 (fun v ->
2524 try conf.pgscale <- float_of_string v
2525 with exn ->
2526 state.text <-
2527 Printf.sprintf "bad page scroll scaling factor `%s': %s" v
2528 @@ exntos exn
2530 src#int "ui font size"
2531 (fun () -> fstate.fontsize)
2532 (fun v -> setfontsize (bound v 5 100));
2533 src#int "hint font size"
2534 (fun () -> conf.hfsize)
2535 (fun v -> conf.hfsize <- bound v 5 100);
2536 src#string "trim fuzz"
2537 (fun () -> irect_to_string conf.trimfuzz)
2538 (fun v ->
2540 conf.trimfuzz <- irect_of_string v;
2541 if conf.trimmargins
2542 then settrim true conf.trimfuzz;
2543 with exn ->
2544 state.text <- Printf.sprintf "bad irect `%s': %s" v
2545 @@ exntos exn
2547 src#string "selection command"
2548 (fun () -> conf.selcmd)
2549 (fun v -> conf.selcmd <- v);
2550 src#string "synctex command"
2551 (fun () -> conf.stcmd)
2552 (fun v -> conf.stcmd <- v);
2553 src#string "pax command"
2554 (fun () -> conf.paxcmd)
2555 (fun v -> conf.paxcmd <- v);
2556 src#string "ask password command"
2557 (fun () -> conf.passcmd)
2558 (fun v -> conf.passcmd <- v);
2559 src#string "save path command"
2560 (fun () -> conf.savecmd)
2561 (fun v -> conf.savecmd <- v);
2562 src#colorspace "color space"
2563 (fun () -> CSTE.to_string conf.colorspace)
2564 (fun v ->
2565 conf.colorspace <- CSTE.of_int v;
2566 wcmd "cs %d" v;
2567 load state.layout;
2569 src#paxmark "pax mark method"
2570 (fun () -> MTE.to_string conf.paxmark)
2571 (fun v -> conf.paxmark <- MTE.of_int v);
2572 if bousable ()
2573 then
2574 src#bool "use PBO"
2575 (fun () -> conf.usepbo)
2576 (fun v -> conf.usepbo <- v);
2577 src#bool "mouse wheel scrolls pages"
2578 (fun () -> conf.wheelbypage)
2579 (fun v -> conf.wheelbypage <- v);
2580 src#bool "open remote links in a new instance"
2581 (fun () -> conf.riani)
2582 (fun v -> conf.riani <- v);
2583 src#bool "edit annotations inline"
2584 (fun () -> conf.annotinline)
2585 (fun v -> conf.annotinline <- v);
2586 src#bool "coarse positioning in presentation mode"
2587 (fun () -> conf.coarseprespos)
2588 (fun v -> conf.coarseprespos <- v);
2589 src#bool "use document CSS"
2590 (fun () -> conf.usedoccss)
2591 (fun v ->
2592 conf.usedoccss <- v;
2593 state.anchor <- getanchor ();
2594 opendoc state.path state.password;
2596 src#bool ~btos "colors"
2597 (fun () -> !showcolors)
2598 (fun v -> showcolors := v; fillsrc prevmode prevuioh);
2599 if !showcolors
2600 then (
2601 colorp " background"
2602 (fun () -> conf.bgcolor)
2603 (fun v -> conf.bgcolor <- v);
2604 rgba " scrollbar"
2605 (fun () -> conf.sbarcolor)
2606 (fun v -> conf.sbarcolor <- v);
2607 rgba " scrollbar handle"
2608 (fun () -> conf.sbarhndlcolor)
2609 (fun v -> conf.sbarhndlcolor <- v);
2610 rgba " texture color"
2611 (fun () -> conf.texturecolor)
2612 (fun v ->
2613 GlTex.env (`color v);
2614 conf.texturecolor <- v;
2619 sep ();
2620 src#caption "Document" 0;
2621 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
2622 src#caption2 "Pages" (fun () -> string_of_int state.pagecount) 1;
2623 src#caption2 "Dimensions"
2624 (fun () -> string_of_int (List.length state.pdims)) 1;
2625 if nonemptystr conf.css
2626 then src#caption2 "CSS" (fun () -> conf.css) 1;
2627 if conf.trimmargins
2628 then (
2629 sep ();
2630 src#caption "Trimmed margins" 0;
2631 src#caption2 "Dimensions"
2632 (fun () -> string_of_int (List.length state.pdims)) 1;
2635 sep ();
2636 src#caption "OpenGL" 0;
2637 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
2638 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
2640 sep ();
2641 src#caption "Location" 0;
2642 if nonemptystr state.origin
2643 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
2644 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
2646 src#reset prevmode prevuioh;
2648 fun () -> (
2649 state.text <- E.s;
2650 resetmstate ();
2651 let prevmode = state.mode
2652 and prevuioh = state.uioh in
2653 fillsrc prevmode prevuioh;
2654 let source = (src :> lvsource) in
2655 let modehash = findkeyhash conf "info" in
2656 state.uioh <-
2657 coe (object (self)
2658 inherit listview ~zebra:false ~helpmode:false
2659 ~source ~trusted:true ~modehash as super
2660 val mutable m_prevmemused = 0
2661 method! infochanged = function
2662 | Memused ->
2663 if m_prevmemused != state.memused
2664 then (
2665 m_prevmemused <- state.memused;
2666 postRedisplay "memusedchanged";
2668 | Pdim -> postRedisplay "pdimchanged"
2669 | Docinfo -> fillsrc prevmode prevuioh
2671 method! key key mask =
2672 if not (Wsi.withctrl mask)
2673 then
2674 match [@warning "-4"] Wsi.kc2kt key with
2675 | Keys.Left -> coe (self#updownlevel ~-1)
2676 | Keys.Right -> coe (self#updownlevel 1)
2677 | _ -> super#key key mask
2678 else super#key key mask
2679 end);
2680 postRedisplay "info";
2684 let enterhelpmode =
2685 let source =
2686 (object
2687 inherit lvsourcebase
2688 method getitemcount = Array.length state.help
2689 method getitem n =
2690 let s, l, _ = state.help.(n) in
2691 (s, l)
2693 method exit ~uioh ~cancel ~active ~first ~pan =
2694 let optuioh =
2695 if not cancel
2696 then (
2697 match state.help.(active) with
2698 | _, _, Action f -> Some (f uioh)
2699 | _, _, Noaction -> Some uioh
2701 else None
2703 m_active <- active;
2704 m_first <- first;
2705 m_pan <- pan;
2706 optuioh
2708 method hasaction n =
2709 match state.help.(n) with
2710 | _, _, Action _ -> true
2711 | _, _, Noaction -> false
2713 initializer
2714 m_active <- -1
2715 end)
2717 fun () ->
2718 let modehash = findkeyhash conf "help" in
2719 resetmstate ();
2720 state.uioh <- coe (new listview
2721 ~zebra:false ~helpmode:true
2722 ~source ~trusted:true ~modehash);
2723 postRedisplay "help";
2726 let entermsgsmode =
2727 let msgsource =
2728 (object
2729 inherit lvsourcebase
2730 val mutable m_items = E.a
2732 method getitemcount = 1 + Array.length m_items
2734 method getitem n =
2735 if n = 0
2736 then "[Clear]", 0
2737 else m_items.(n-1), 0
2739 method exit ~uioh ~cancel ~active ~first ~pan =
2740 ignore uioh;
2741 if not cancel
2742 then (
2743 if active = 0
2744 then Buffer.clear state.errmsgs;
2746 m_active <- active;
2747 m_first <- first;
2748 m_pan <- pan;
2749 None
2751 method hasaction n =
2752 n = 0
2754 method reset =
2755 state.newerrmsgs <- false;
2756 let l = Str.split Utils.Re.crlf (Buffer.contents state.errmsgs) in
2757 m_items <- Array.of_list l
2759 initializer
2760 m_active <- 0
2761 end)
2762 in fun () ->
2763 state.text <- E.s;
2764 resetmstate ();
2765 msgsource#reset;
2766 let source = (msgsource :> lvsource) in
2767 let modehash = findkeyhash conf "listview" in
2768 state.uioh <-
2769 coe (object
2770 inherit listview ~zebra:false ~helpmode:false
2771 ~source ~trusted:false ~modehash as super
2772 method! display =
2773 if state.newerrmsgs
2774 then msgsource#reset;
2775 super#display
2776 end);
2777 postRedisplay "msgs";
2780 let getusertext s =
2781 let editor = getenvdef "EDITOR" E.s in
2782 if emptystr editor
2783 then E.s
2784 else
2785 let tmppath = Filename.temp_file "llpp" "note" in
2786 if nonemptystr s
2787 then (
2788 let oc = open_out tmppath in
2789 output_string oc s;
2790 close_out oc;
2792 let execstr = editor ^ " " ^ tmppath in
2793 let s =
2794 match spawn execstr [] with
2795 | exception exn ->
2796 impmsg "spawn(%S) failed: %s" execstr @@ exntos exn;
2798 | pid ->
2799 match Unix.waitpid [] pid with
2800 | exception exn ->
2801 impmsg "waitpid(%d) failed: %s" pid @@ exntos exn;
2803 | (_pid, status) ->
2804 match status with
2805 | Unix.WEXITED 0 -> filecontents tmppath
2806 | Unix.WEXITED n ->
2807 impmsg "editor process(%s) exited abnormally: %d" execstr n;
2809 | Unix.WSIGNALED n ->
2810 impmsg "editor process(%s) was killed by signal %d" execstr n;
2812 | Unix.WSTOPPED n ->
2813 impmsg "editor(%s) process was stopped by signal %d" execstr n;
2816 match Unix.unlink tmppath with
2817 | exception exn ->
2818 impmsg "failed to ulink %S: %s" tmppath @@ exntos exn;
2820 | () -> s
2823 let enterannotmode opaque slinkindex =
2824 let msgsource =
2825 (object
2826 inherit lvsourcebase
2827 val mutable m_text = E.s
2828 val mutable m_items = E.a
2830 method getitemcount = Array.length m_items
2832 method getitem n =
2833 let label, _func = m_items.(n) in
2834 label, 0
2836 method exit ~uioh ~cancel ~active ~first ~pan =
2837 ignore (uioh, first, pan);
2838 if not cancel
2839 then (
2840 let _label, func = m_items.(active) in
2841 func ()
2843 None
2845 method hasaction n = nonemptystr @@ fst m_items.(n)
2847 method reset s =
2848 let rec split accu b i =
2849 let p = b+i in
2850 if p = String.length s
2851 then (String.sub s b (p-b), fun () -> ()) :: accu
2852 else
2853 if (i > 70 && s.[p] = ' ') || s.[p] = '\r' || s.[p] = '\n'
2854 then
2855 let ss = if i = 0 then E.s else String.sub s b i in
2856 split ((ss, fun () -> ())::accu) (p+1) 0
2857 else split accu b (i+1)
2859 let cleanup () =
2860 wcmd "freepage %s" (~> opaque);
2861 let keys =
2862 Hashtbl.fold (fun key opaque' accu ->
2863 if opaque' = opaque'
2864 then key :: accu else accu) state.pagemap []
2866 List.iter (Hashtbl.remove state.pagemap) keys;
2867 flushtiles ();
2868 gotoxy state.x state.y
2870 let dele () =
2871 delannot opaque slinkindex;
2872 cleanup ();
2874 let edit inline () =
2875 let update s =
2876 if emptystr s
2877 then dele ()
2878 else (
2879 modannot opaque slinkindex s;
2880 cleanup ();
2883 if inline
2884 then
2885 let mode = state.mode in
2886 state.mode <-
2887 Textentry (
2888 ("annotation: ", m_text, None, textentry, update, true),
2889 fun _ -> state.mode <- mode
2891 state.text <- E.s;
2892 enttext ();
2893 else
2894 let s = getusertext m_text in
2895 update s
2897 m_text <- s;
2898 m_items <-
2899 ( "[Copy]", fun () -> selstring conf.selcmd m_text)
2900 :: ("[Delete]", dele)
2901 :: ("[Edit]", edit conf.annotinline)
2902 :: (E.s, fun () -> ())
2903 :: split [] 0 0 |> List.rev |> Array.of_list
2905 initializer
2906 m_active <- 0
2907 end)
2909 state.text <- E.s;
2910 let s = getannotcontents opaque slinkindex in
2911 resetmstate ();
2912 msgsource#reset s;
2913 let source = (msgsource :> lvsource) in
2914 let modehash = findkeyhash conf "listview" in
2915 state.uioh <- coe (object
2916 inherit listview ~zebra:false ~helpmode:false
2917 ~source ~trusted:false ~modehash
2918 end);
2919 postRedisplay "enterannotmode";
2922 let gotoremote spec =
2923 let filename, dest = splitatchar spec '#' in
2924 let getpath filename =
2925 let path =
2926 if nonemptystr filename
2927 then
2928 if Filename.is_relative filename
2929 then
2930 let dir = Filename.dirname state.path in
2931 let dir =
2932 if Filename.is_implicit dir
2933 then Filename.concat (Sys.getcwd ()) dir
2934 else dir
2936 Filename.concat dir filename
2937 else filename
2938 else E.s
2940 if Sys.file_exists path
2941 then path
2942 else E.s
2944 let path = getpath filename in
2945 let dospawn lcmd =
2946 if conf.riani
2947 then
2948 let cmd = Lazy.force_val lcmd in
2949 match spawn cmd with
2950 | _pid -> ()
2951 | exception exn -> dolog "failed to execute `%s': %s" cmd @@ exntos exn
2952 else
2953 let anchor = getanchor () in
2954 let ranchor = state.path, state.password, anchor, state.origin in
2955 state.origin <- E.s;
2956 state.ranchors <- ranchor :: state.ranchors;
2957 opendoc path E.s;
2959 if substratis spec 0 "page="
2960 then
2961 match Scanf.sscanf spec "page=%d" (fun n -> n) with
2962 | pageno ->
2963 state.anchor <- (pageno, 0.0, 0.0);
2964 dospawn @@ lazy (Printf.sprintf "%s -page %d %S" !selfexec pageno path);
2965 | exception exn ->
2966 adderrfmt "error parsing remote destination" "page: %s" @@ exntos exn
2967 else (
2968 state.nameddest <- dest;
2969 dospawn @@ lazy (!selfexec ^ " " ^ path ^ " -dest " ^ dest)
2973 let gotounder = function
2974 | Ulinkuri s when isexternallink s ->
2975 if substratis s 0 "file://"
2976 then gotoremote @@ String.sub s 7 (String.length s - 7)
2977 else Help.gotouri conf.urilauncher s
2978 | Ulinkuri s ->
2979 let pageno, x, y = uritolocation s in
2980 addnav ();
2981 gotopagexy pageno x y
2982 | Utext _ | Unone -> ()
2983 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
2986 let gotooutline (_, _, kind) =
2987 match kind with
2988 | Onone -> ()
2989 | Oanchor ((pageno, y, _) as anchor) ->
2990 addnav ();
2991 gotoxy state.x @@
2992 getanchory (if conf.presentation then (pageno, y, 1.0) else anchor)
2993 | Ouri uri -> gotounder (Ulinkuri uri)
2994 | Olaunch cmd -> error "gotounder (Ulaunch %S)" cmd
2995 | Oremote (remote, pageno) ->
2996 error "gotounder (Uremote (%S,%d) )" remote pageno
2997 | Ohistory hist -> gotohist hist
2998 | Oremotedest (path, dest) ->
2999 error "gotounder (Uremotedest (%S, %S))" path dest
3002 class outlinesoucebase fetchoutlines = object (self)
3003 inherit lvsourcebase
3004 val mutable m_items = E.a
3005 val mutable m_minfo = E.a
3006 val mutable m_orig_items = E.a
3007 val mutable m_orig_minfo = E.a
3008 val mutable m_narrow_patterns = []
3009 val mutable m_gen = -1
3011 method getitemcount = Array.length m_items
3013 method getitem n =
3014 let s, n, _ = m_items.(n) in
3015 (s, n+0)
3017 method exit ~(uioh:uioh) ~cancel ~active ~(first:int) ~pan : uioh option =
3018 ignore (uioh, first);
3019 let items, minfo =
3020 if m_narrow_patterns = []
3021 then m_orig_items, m_orig_minfo
3022 else m_items, m_minfo
3024 m_pan <- pan;
3025 if not cancel
3026 then (
3027 m_items <- items;
3028 m_minfo <- minfo;
3029 gotooutline m_items.(active);
3031 else (
3032 m_items <- items;
3033 m_minfo <- minfo;
3035 None
3037 method hasaction (_:int) = true
3039 method greetmsg =
3040 if Array.length m_items != Array.length m_orig_items
3041 then
3042 let s =
3043 match m_narrow_patterns with
3044 | one :: [] -> one
3045 | many -> String.concat Utf8syms.ellipsis (List.rev many)
3047 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
3048 else E.s
3050 method statestr =
3051 match m_narrow_patterns with
3052 | [] -> E.s
3053 | one :: [] -> one
3054 | head :: _ -> Utf8syms.ellipsis ^ head
3056 method narrow pattern =
3057 match Str.regexp_case_fold pattern with
3058 | exception _ -> ()
3059 | re ->
3060 let rec loop accu minfo n =
3061 if n = -1
3062 then (
3063 m_items <- Array.of_list accu;
3064 m_minfo <- Array.of_list minfo;
3066 else
3067 let (s, _, _) as o = m_items.(n) in
3068 let accu, minfo =
3069 match Str.search_forward re s 0 with
3070 | exception Not_found -> accu, minfo
3071 | first -> o :: accu, (first, Str.match_end ()) :: minfo
3073 loop accu minfo (n-1)
3075 loop [] [] (Array.length m_items - 1)
3077 method! getminfo = m_minfo
3079 method denarrow =
3080 m_orig_items <- fetchoutlines ();
3081 m_minfo <- m_orig_minfo;
3082 m_items <- m_orig_items
3084 method add_narrow_pattern pattern =
3085 m_narrow_patterns <- pattern :: m_narrow_patterns
3087 method del_narrow_pattern =
3088 match m_narrow_patterns with
3089 | _ :: rest -> m_narrow_patterns <- rest
3090 | [] -> ()
3092 method renarrow =
3093 self#denarrow;
3094 match m_narrow_patterns with
3095 | pattern :: [] -> self#narrow pattern; pattern
3096 | list ->
3097 List.fold_left (fun accu pattern ->
3098 self#narrow pattern;
3099 pattern ^ Utf8syms.ellipsis ^ accu) E.s list
3101 method calcactive (_:anchor) = 0
3103 method reset anchor items =
3104 if state.gen != m_gen
3105 then (
3106 m_orig_items <- items;
3107 m_items <- items;
3108 m_narrow_patterns <- [];
3109 m_minfo <- E.a;
3110 m_orig_minfo <- E.a;
3111 m_gen <- state.gen;
3113 else (
3114 if items != m_orig_items
3115 then (
3116 m_orig_items <- items;
3117 if m_narrow_patterns == []
3118 then m_items <- items;
3121 let active = self#calcactive anchor in
3122 m_active <- active;
3123 m_first <- firstof m_first active
3127 let outlinesource fetchoutlines =
3128 (object
3129 inherit outlinesoucebase fetchoutlines
3130 method! calcactive anchor =
3131 let rely = getanchory anchor in
3132 let rec loop n best bestd =
3133 if n = Array.length m_items
3134 then best
3135 else
3136 let _, _, kind = m_items.(n) in
3137 match kind with
3138 | Oanchor anchor ->
3139 let orely = getanchory anchor in
3140 let d = abs (orely - rely) in
3141 if d < bestd
3142 then loop (n+1) n d
3143 else loop (n+1) best bestd
3144 | Onone | Oremote _ | Olaunch _
3145 | Oremotedest _ | Ouri _ | Ohistory _ ->
3146 loop (n+1) best bestd
3148 loop 0 ~-1 max_int
3149 end)
3152 let enteroutlinemode, enterbookmarkmode, enterhistmode =
3153 let fetchoutlines sourcetype () =
3154 match sourcetype with
3155 | `bookmarks -> Array.of_list state.bookmarks
3156 | `outlines -> state.outlines
3157 | `history -> genhistoutlines () |> Array.of_list
3159 let so = outlinesource (fetchoutlines `outlines) in
3160 let sb = outlinesource (fetchoutlines `bookmarks) in
3161 let sh = outlinesource (fetchoutlines `history) in
3162 let mkselector sourcetype source =
3163 (fun errmsg ->
3164 let outlines = fetchoutlines sourcetype () in
3165 if Array.length outlines = 0
3166 then showtext ' ' errmsg
3167 else (
3168 resetmstate ();
3169 Wsi.setcursor Wsi.CURSOR_INHERIT;
3170 let anchor = getanchor () in
3171 source#reset anchor outlines;
3172 state.text <- source#greetmsg;
3173 state.uioh <-
3174 coe (new outlinelistview ~zebra:(sourcetype=`history) ~source);
3175 postRedisplay "enter selector";
3179 let mkenter sourcetype errmsg s = fun () -> mkselector sourcetype s errmsg in
3180 ( mkenter `outlines "document has no outline" so
3181 , mkenter `bookmarks "document has no bookmarks (yet)" sb
3182 , mkenter `history "history is empty" sh )
3185 let quickbookmark ?title () =
3186 match state.layout with
3187 | [] -> ()
3188 | l :: _ ->
3189 let title =
3190 match title with
3191 | None ->
3192 Unix.(
3193 let tm = localtime (now ()) in
3194 Printf.sprintf
3195 "Quick (page %d) (bookmarked on %02d/%02d/%d at %02d:%02d)"
3196 (l.pageno+1)
3197 tm.tm_mday (tm.tm_mon+1) (tm.tm_year+1900) tm.tm_hour tm.tm_min
3199 | Some title -> title
3201 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
3204 let setautoscrollspeed step goingdown =
3205 let incr = max 1 ((abs step) / 2) in
3206 let incr = if goingdown then incr else -incr in
3207 let astep = boundastep state.winh (step + incr) in
3208 state.autoscroll <- Some astep;
3211 let canpan () =
3212 match conf.columns with
3213 | Csplit _ -> true
3214 | Csingle _ | Cmulti _ -> state.x != 0 || conf.zoom > 1.0
3217 let panbound x = bound x (-state.w) state.winw;;
3219 let existsinrow pageno (columns, coverA, coverB) p =
3220 let last = ((pageno - coverA) mod columns) + columns in
3221 let rec any = function
3222 | [] -> false
3223 | l :: rest ->
3224 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
3225 then p l
3226 else (
3227 if not (p l)
3228 then (if l.pageno = last then false else any rest)
3229 else true
3232 any state.layout
3235 let nextpage () =
3236 match state.layout with
3237 | [] ->
3238 let pageno = page_of_y state.y in
3239 gotoxy state.x (getpagey (pageno+1))
3240 | l :: rest ->
3241 match conf.columns with
3242 | Csingle _ ->
3243 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
3244 then
3245 let y = clamp (pgscale state.winh) in
3246 gotoxy state.x y
3247 else
3248 let pageno = min (l.pageno+1) (state.pagecount-1) in
3249 gotoxy state.x (getpagey pageno)
3250 | Cmulti ((c, _, _) as cl, _) ->
3251 if conf.presentation
3252 && (existsinrow l.pageno cl
3253 (fun l -> l.pageh > l.pagey + l.pagevh))
3254 then
3255 let y = clamp (pgscale state.winh) in
3256 gotoxy state.x y
3257 else
3258 let pageno = min (l.pageno+c) (state.pagecount-1) in
3259 gotoxy state.x (getpagey pageno)
3260 | Csplit (n, _) ->
3261 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
3262 then
3263 let pagey, pageh = getpageyh l.pageno in
3264 let pagey = pagey + pageh * l.pagecol in
3265 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
3266 gotoxy state.x (pagey + pageh + ips)
3269 let prevpage () =
3270 match state.layout with
3271 | [] ->
3272 let pageno = page_of_y state.y in
3273 gotoxy state.x (getpagey (pageno-1))
3274 | l :: _ ->
3275 match conf.columns with
3276 | Csingle _ ->
3277 if conf.presentation && l.pagey != 0
3278 then gotoxy state.x (clamp (pgscale ~-(state.winh)))
3279 else
3280 let pageno = max 0 (l.pageno-1) in
3281 gotoxy state.x (getpagey pageno)
3282 | Cmulti ((c, _, coverB) as cl, _) ->
3283 if conf.presentation &&
3284 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
3285 then gotoxy state.x (clamp (pgscale ~-(state.winh)))
3286 else
3287 let decr =
3288 if l.pageno = state.pagecount - coverB
3289 then 1
3290 else c
3292 let pageno = max 0 (l.pageno-decr) in
3293 gotoxy state.x (getpagey pageno)
3294 | Csplit (n, _) ->
3295 let y =
3296 if l.pagecol = 0
3297 then
3298 if l.pageno = 0
3299 then l.pagey
3300 else
3301 let pageno = max 0 (l.pageno-1) in
3302 let pagey, pageh = getpageyh pageno in
3303 pagey + (n-1)*pageh
3304 else
3305 let pagey, pageh = getpageyh l.pageno in
3306 pagey + pageh * (l.pagecol-1) - conf.interpagespace
3308 gotoxy state.x y
3311 let save () =
3312 if emptystr conf.savecmd
3313 then adderrmsg "savepath-command is empty"
3314 "don't know where to save modified document"
3315 else
3316 let savecmd = Str.global_replace Utils.Re.percent state.path conf.savecmd in
3317 let path =
3318 getcmdoutput
3319 (fun exn ->
3320 adderrfmt savecmd "failed to produce path to the saved copy: %s" exn)
3321 savecmd
3323 if nonemptystr path
3324 then
3325 let tmp = path ^ ".tmp" in
3326 savedoc tmp;
3327 Unix.rename tmp path;
3330 let viewkeyboard key mask =
3331 let enttext te =
3332 let mode = state.mode in
3333 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
3334 state.text <- E.s;
3335 enttext ();
3336 postRedisplay "view:enttext"
3338 let ctrl = Wsi.withctrl mask in
3339 let open Keys in
3340 match Wsi.kc2kt key with
3341 | Ascii 'S' -> state.slideshow <- state.slideshow lxor 1
3343 | Ascii 'Q' -> exit 0
3345 | Ascii 'z' ->
3346 let yloc f =
3347 match List.rev state.rects with
3348 | [] -> ()
3349 | (pageno, _, (_, y0, _, y1, _, y2, _, y3)) :: _ ->
3350 f pageno (y0, y1, y2, y3)
3351 and yminmax (y0, y1, y2, y3) =
3352 let ym = min y0 y1 |> min y2 |> min y3 |> truncate in
3353 let yM = max y0 y1 |> max y2 |> max y3 |> truncate in
3354 ym, yM
3356 let ondone msg = state.text <- msg
3357 and zmod _ _ k =
3358 match [@warning "-4"] k with
3359 | Keys.Ascii 'z' ->
3360 let f pageno ys =
3361 let ym, yM = yminmax ys in
3362 let hh = (yM - ym)/2 in
3363 gotopage1 pageno (ym + hh - state.winh/2)
3365 yloc f;
3366 TEdone "center"
3367 | Keys.Ascii 't' ->
3368 let f pageno ys =
3369 let ym, _ = yminmax ys in
3370 gotopage1 pageno ym
3372 yloc f;
3373 TEdone "top"
3374 | Keys.Ascii 'b' ->
3375 let f pageno ys =
3376 let _, yM = yminmax ys in
3377 gotopage1 pageno (yM - state.winh)
3379 yloc f;
3380 TEdone "bottom"
3381 | _ -> TEstop
3383 enttext (": ", E.s, None, zmod state.mode, ondone, true)
3385 | Ascii 'W' ->
3386 if hasunsavedchanges ()
3387 then save ()
3389 | Insert ->
3390 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
3391 then (
3392 state.mode <- (
3393 match state.lnava with
3394 | None -> LinkNav (Ltgendir 0)
3395 | Some pn -> LinkNav (Ltexact pn)
3397 gotoxy state.x state.y;
3399 else impmsg "keyboard link navigation does not work under rotation"
3401 | Escape | Ascii 'q' ->
3402 begin match state.mstate with
3403 | Mzoomrect _ ->
3404 resetmstate ();
3405 postRedisplay "kill rect";
3406 | Msel _
3407 | Mpan _
3408 | Mscrolly | Mscrollx
3409 | Mzoom _
3410 | Mnone ->
3411 begin match state.mode with
3412 | LinkNav ln ->
3413 begin match ln with
3414 | Ltexact pl -> state.lnava <- Some pl
3415 | Ltgendir _ | Ltnotready _ -> state.lnava <- None
3416 end;
3417 state.mode <- View;
3418 postRedisplay "esc leave linknav"
3419 | Birdseye _ | Textentry _ | View ->
3420 match state.ranchors with
3421 | [] -> raise Quit
3422 | (path, password, anchor, origin) :: rest ->
3423 state.ranchors <- rest;
3424 state.anchor <- anchor;
3425 state.origin <- origin;
3426 state.nameddest <- E.s;
3427 opendoc path password
3428 end;
3429 end;
3431 | Backspace ->
3432 addnavnorc ();
3433 gotoxy state.x (getnav ~-1)
3435 | Ascii 'o' -> enteroutlinemode ()
3436 | Ascii 'H' -> enterhistmode ()
3438 | Ascii 'u' ->
3439 state.rects <- [];
3440 state.text <- E.s;
3441 Hashtbl.iter (fun _ opaque ->
3442 clearmark opaque;
3443 Hashtbl.clear state.prects) state.pagemap;
3444 postRedisplay "dehighlight";
3446 | Ascii (('/' | '?') as c) ->
3447 let ondone isforw s =
3448 cbput state.hists.pat s;
3449 state.searchpattern <- s;
3450 search s isforw
3452 let s = String.make 1 c in
3453 enttext (s, E.s, Some (onhist state.hists.pat),
3454 textentry, ondone (c = '/'), true)
3456 | Ascii '+' | Ascii '=' when ctrl ->
3457 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
3458 pivotzoom (conf.zoom +. incr)
3460 | Ascii '+' ->
3461 let ondone s =
3462 let n =
3463 try int_of_string s with exn ->
3464 state.text <-
3465 Printf.sprintf "bad integer `%s': %s" s @@ exntos exn;
3466 max_int
3468 if n != max_int
3469 then (
3470 conf.pagebias <- n;
3471 state.text <- "page bias is now " ^ string_of_int n;
3474 enttext ("page bias: ", E.s, None, intentry, ondone, true)
3476 | Ascii '-' when ctrl ->
3477 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
3478 pivotzoom (max 0.01 (conf.zoom -. decr))
3480 | Ascii '-' ->
3481 let ondone msg = state.text <- msg in
3482 enttext ("option: ", E.s, None,
3483 optentry state.mode, ondone, true)
3485 | Ascii '0' when ctrl ->
3486 if conf.zoom = 1.0
3487 then gotoxy 0 state.y
3488 else setzoom 1.0
3490 | Ascii ('1'|'2' as c) when ctrl && conf.fitmodel != FitPage ->
3491 let cols =
3492 match conf.columns with
3493 | Csingle _ | Cmulti _ -> 1
3494 | Csplit (n, _) -> n
3496 let h = state.winh -
3497 conf.interpagespace lsl (if conf.presentation then 1 else 0)
3499 let zoom = zoomforh state.winw h 0 cols in
3500 if zoom > 0.0 && (c = '2' || zoom < 1.0)
3501 then setzoom zoom
3503 | Ascii '3' when ctrl ->
3504 let fm =
3505 match conf.fitmodel with
3506 | FitWidth -> FitProportional
3507 | FitProportional -> FitPage
3508 | FitPage -> FitWidth
3510 state.text <- "fit model: " ^ FMTE.to_string fm;
3511 reqlayout conf.angle fm
3513 | Ascii '4' when ctrl ->
3514 let zoom = getmaxw () /. float state.winw in
3515 if zoom > 0.0 then setzoom zoom
3517 | Fn 9 | Ascii '9' when ctrl -> togglebirdseye ()
3519 | Ascii ('0'..'9' as c) when not ctrl ->
3520 let ondone s =
3521 let n =
3522 try int_of_string s with exn ->
3523 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn;
3526 if n >= 0
3527 then (
3528 addnav ();
3529 cbput state.hists.pag (string_of_int n);
3530 gotopage1 (n + conf.pagebias - 1) 0;
3533 let pageentry text = function [@warning "-4"]
3534 | Keys.Ascii 'g' -> TEdone text
3535 | key -> intentry text key
3537 let text = String.make 1 c in
3538 enttext (":", text, Some (onhist state.hists.pag),
3539 pageentry, ondone, true)
3541 | Ascii 'b' ->
3542 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
3543 postRedisplay "toggle scrollbar";
3545 | Ascii 'B' ->
3546 state.bzoom <- not state.bzoom;
3547 state.rects <- [];
3548 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
3550 | Ascii 'l' ->
3551 conf.hlinks <- not conf.hlinks;
3552 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
3553 postRedisplay "toggle highlightlinks";
3555 | Ascii 'F' ->
3556 if conf.angle mod 360 = 0
3557 then (
3558 state.glinks <- true;
3559 let mode = state.mode in
3560 state.mode <-
3561 Textentry (
3562 ("goto: ", E.s, None, linknentry, linknact gotounder, false),
3563 (fun _ ->
3564 state.glinks <- false;
3565 state.mode <- mode)
3567 state.text <- E.s;
3568 postRedisplay "view:linkent(F)"
3570 else impmsg "hint mode does not work under rotation"
3572 | Ascii 'y' ->
3573 state.glinks <- true;
3574 let mode = state.mode in
3575 state.mode <-
3576 Textentry (
3577 ("copy: ", E.s, None, linknentry,
3578 linknact (fun under ->
3579 selstring conf.selcmd (undertext under)), false),
3580 (fun _ ->
3581 state.glinks <- false;
3582 state.mode <- mode)
3584 state.text <- E.s;
3585 postRedisplay "view:linkent"
3587 | Ascii 'a' ->
3588 begin match state.autoscroll with
3589 | Some step ->
3590 conf.autoscrollstep <- step;
3591 state.autoscroll <- None
3592 | None ->
3593 state.autoscroll <- Some conf.autoscrollstep;
3594 state.slideshow <- state.slideshow land lnot 2
3597 | Ascii 'p' when ctrl ->
3598 launchpath () (* XXX where do error messages go? *)
3600 | Ascii 'P' ->
3601 setpresentationmode (not conf.presentation);
3602 showtext ' ' ("presentation mode " ^
3603 if conf.presentation then "on" else "off");
3605 | Ascii 'f' ->
3606 if List.mem Wsi.Fullscreen state.winstate
3607 then Wsi.reshape conf.cwinw conf.cwinh
3608 else Wsi.fullscreen ()
3610 | Ascii ('p'|'N') -> search state.searchpattern false
3611 | Ascii 'n' | Fn 3 -> search state.searchpattern true
3613 | Ascii 't' ->
3614 begin match state.layout with
3615 | [] -> ()
3616 | l :: _ -> gotoxy state.x (getpagey l.pageno)
3619 | Ascii ' ' -> nextpage ()
3620 | Delete -> prevpage ()
3621 | Ascii '=' -> showtext ' ' (describe_layout state.layout);
3623 | Ascii 'w' ->
3624 begin match state.layout with
3625 | [] -> ()
3626 | l :: _ ->
3627 Wsi.reshape l.pagew l.pageh;
3628 postRedisplay "w"
3631 | Ascii '\'' -> enterbookmarkmode ()
3632 | Ascii 'h' | Fn 1 -> enterhelpmode ()
3633 | Ascii 'i' -> enterinfomode ()
3634 | Ascii 'e' when Buffer.length state.errmsgs > 0 -> entermsgsmode ()
3636 | Ascii 'm' ->
3637 let ondone s =
3638 match state.layout with
3639 | l :: _ when nonemptystr s ->
3640 state.bookmarks <- (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
3641 | _ -> ()
3643 enttext ("bookmark: ", E.s, None, textentry, ondone, true)
3645 | Ascii '~' ->
3646 quickbookmark ();
3647 showtext ' ' "Quick bookmark added";
3649 | Ascii 'x' -> state.roam ()
3651 | Ascii ('<'|'>' as c) ->
3652 reqlayout (conf.angle + (if c = '>' then 30 else -30)) conf.fitmodel
3654 | Ascii ('['|']' as c) ->
3655 conf.colorscale <-
3656 bound (conf.colorscale +. (if c = ']' then 0.1 else -0.1)) 0.0 1.0;
3657 postRedisplay "brightness";
3659 | Ascii 'c' when state.mode = View ->
3660 if Wsi.withalt mask
3661 then (
3662 if conf.zoom > 1.0
3663 then
3664 let m = (state.winw - state.w) / 2 in
3665 gotoxy m state.y
3667 else
3668 let (c, a, b), z =
3669 match state.prevcolumns with
3670 | None -> (1, 0, 0), 1.0
3671 | Some (columns, z) ->
3672 let cab =
3673 match columns with
3674 | Csplit (c, _) -> -c, 0, 0
3675 | Cmulti ((c, a, b), _) -> c, a, b
3676 | Csingle _ -> 1, 0, 0
3678 cab, z
3680 setcolumns View c a b;
3681 setzoom z
3683 | Down | Up when ctrl && Wsi.withshift mask ->
3684 let zoom, x = state.prevzoom in
3685 setzoom zoom;
3686 state.x <- x;
3688 | Up ->
3689 begin match state.autoscroll with
3690 | None ->
3691 begin match state.mode with
3692 | Birdseye beye -> upbirdseye 1 beye
3693 | Textentry _ | View | LinkNav _ ->
3694 if ctrl
3695 then gotoxy state.x (clamp ~-(state.winh/2))
3696 else (
3697 if not (Wsi.withshift mask) && conf.presentation
3698 then prevpage ()
3699 else gotoxy state.x (clamp (-conf.scrollstep))
3702 | Some n -> setautoscrollspeed n false
3705 | Down ->
3706 begin match state.autoscroll with
3707 | None ->
3708 begin match state.mode with
3709 | Birdseye beye -> downbirdseye 1 beye
3710 | Textentry _ | View | LinkNav _ ->
3711 if ctrl
3712 then gotoxy state.x (clamp (state.winh/2))
3713 else (
3714 if not (Wsi.withshift mask) && conf.presentation
3715 then nextpage ()
3716 else gotoxy state.x (clamp (conf.scrollstep))
3719 | Some n -> setautoscrollspeed n true
3722 | Left | Right when not (Wsi.withalt mask) ->
3723 if canpan ()
3724 then
3725 let dx =
3726 if ctrl
3727 then state.winw / 2
3728 else conf.hscrollstep
3730 let dx =
3731 let pv = Wsi.kc2kt key in
3732 if pv = Keys.Left then dx else -dx
3734 gotoxy (panbound (state.x + dx)) state.y
3735 else (
3736 state.text <- E.s;
3737 postRedisplay "left/right"
3740 | Prior ->
3741 let y =
3742 if ctrl
3743 then
3744 match state.layout with
3745 | [] -> state.y
3746 | l :: _ -> state.y - l.pagey
3747 else clamp (pgscale (-state.winh))
3749 gotoxy state.x y
3751 | Next ->
3752 let y =
3753 if ctrl
3754 then
3755 match List.rev state.layout with
3756 | [] -> state.y
3757 | l :: _ -> getpagey l.pageno
3758 else clamp (pgscale state.winh)
3760 gotoxy state.x y
3762 | Ascii 'g' | Home ->
3763 addnav ();
3764 gotoxy 0 0
3765 | Ascii 'G' | End ->
3766 addnav ();
3767 gotoxy 0 (clamp state.maxy)
3769 | Right when Wsi.withalt mask ->
3770 addnavnorc ();
3771 gotoxy state.x (getnav 1)
3772 | Left when Wsi.withalt mask ->
3773 addnavnorc ();
3774 gotoxy state.x (getnav ~-1)
3776 | Ascii 'r' ->
3777 reload ()
3779 | Ascii 'v' when conf.debug ->
3780 state.rects <- [];
3781 List.iter (fun l ->
3782 match getopaque l.pageno with
3783 | None -> ()
3784 | Some opaque ->
3785 let x0, y0, x1, y1 = pagebbox opaque in
3786 let rect = (float x0, float y0,
3787 float x1, float y0,
3788 float x1, float y1,
3789 float x0, float y1) in
3790 debugrect rect;
3791 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
3792 state.rects <- (l.pageno, color, rect) :: state.rects;
3793 ) state.layout;
3794 postRedisplay "v";
3796 | Ascii '|' ->
3797 let mode = state.mode in
3798 let cmd = ref E.s in
3799 let onleave = function
3800 | Cancel -> state.mode <- mode
3801 | Confirm ->
3802 List.iter (fun l ->
3803 match getopaque l.pageno with
3804 | Some opaque -> pipesel opaque !cmd
3805 | None -> ()) state.layout;
3806 state.mode <- mode
3808 let ondone s =
3809 cbput state.hists.sel s;
3810 cmd := s
3812 let te =
3813 "| ", !cmd, Some (onhist state.hists.sel), textentry, ondone, true
3815 postRedisplay "|";
3816 state.mode <- Textentry (te, onleave);
3818 | (Ascii _|Fn _|Enter|Left|Right|Code _|Ctrl _) ->
3819 vlog "huh? %s" (Wsi.keyname key)
3822 let linknavkeyboard key mask linknav =
3823 let pv = Wsi.kc2kt key in
3824 let getpage pageno =
3825 let rec loop = function
3826 | [] -> None
3827 | l :: _ when l.pageno = pageno -> Some l
3828 | _ :: rest -> loop rest
3829 in loop state.layout
3831 let doexact (pageno, n) =
3832 match getopaque pageno, getpage pageno with
3833 | Some opaque, Some l ->
3834 if pv = Keys.Enter
3835 then
3836 let under = getlink opaque n in
3837 postRedisplay "link gotounder";
3838 gotounder under;
3839 state.mode <- View;
3840 else
3841 let opt, dir =
3842 let open Keys in
3843 match pv with
3844 | Home -> Some (findlink opaque LDfirst), -1
3845 | End -> Some (findlink opaque LDlast), 1
3846 | Left -> Some (findlink opaque (LDleft n)), -1
3847 | Right -> Some (findlink opaque (LDright n)), 1
3848 | Up -> Some (findlink opaque (LDup n)), -1
3849 | Down -> Some (findlink opaque (LDdown n)), 1
3850 | Delete|Escape|Insert|Enter|Next|Prior|Ascii _
3851 | Code _|Fn _|Ctrl _|Backspace -> None, 0
3853 let pwl l dir =
3854 begin match findpwl l.pageno dir with
3855 | Pwlnotfound -> ()
3856 | Pwl pageno ->
3857 let notfound dir =
3858 state.mode <- LinkNav (Ltgendir dir);
3859 let y, h = getpageyh pageno in
3860 let y =
3861 if dir < 0
3862 then y + h - state.winh
3863 else y
3865 gotoxy state.x y
3867 begin match getopaque pageno, getpage pageno with
3868 | Some opaque, Some _ ->
3869 let link =
3870 let ld = if dir > 0 then LDfirst else LDlast in
3871 findlink opaque ld
3873 begin match link with
3874 | Lfound m ->
3875 showlinktype (getlink opaque m);
3876 state.mode <- LinkNav (Ltexact (pageno, m));
3877 postRedisplay "linknav jpage";
3878 | Lnotfound -> notfound dir
3879 end;
3880 | _ -> notfound dir
3881 end;
3882 end;
3884 begin match opt with
3885 | Some Lnotfound -> pwl l dir;
3886 | Some (Lfound m) ->
3887 if m = n
3888 then pwl l dir
3889 else (
3890 let _, y0, _, y1 = getlinkrect opaque m in
3891 if y0 < l.pagey
3892 then gotopage1 l.pageno y0
3893 else (
3894 let d = fstate.fontsize + 1 in
3895 if y1 - l.pagey > l.pagevh - d
3896 then gotopage1 l.pageno (y1 - state.winh + d)
3897 else postRedisplay "linknav";
3899 showlinktype (getlink opaque m);
3900 state.mode <- LinkNav (Ltexact (l.pageno, m));
3903 | None -> viewkeyboard key mask
3904 end;
3905 | _ -> viewkeyboard key mask
3907 if pv = Keys.Insert
3908 then (
3909 begin match linknav with
3910 | Ltexact pa -> state.lnava <- Some pa
3911 | Ltgendir _ | Ltnotready _ -> ()
3912 end;
3913 state.mode <- View;
3914 postRedisplay "leave linknav"
3916 else
3917 match linknav with
3918 | Ltgendir _ | Ltnotready _ -> viewkeyboard key mask
3919 | Ltexact exact -> doexact exact
3922 let keyboard key mask =
3923 if (key = Char.code 'g' && Wsi.withctrl mask) && not (istextentry state.mode)
3924 then wcmd "interrupt"
3925 else state.uioh <- state.uioh#key key mask
3928 let birdseyekeyboard key mask
3929 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
3930 let incr =
3931 match conf.columns with
3932 | Csingle _ -> 1
3933 | Cmulti ((c, _, _), _) -> c
3934 | Csplit _ -> error "bird's eye split mode"
3936 let pgh layout = List.fold_left
3937 (fun m l -> max l.pageh m) state.winh layout in
3938 let open Keys in
3939 match Wsi.kc2kt key with
3940 | Ascii 'l' when Wsi.withctrl mask ->
3941 let y, h = getpageyh pageno in
3942 let top = (state.winh - h) / 2 in
3943 gotoxy state.x (max 0 (y - top))
3944 | Enter -> leavebirdseye beye false
3945 | Escape -> leavebirdseye beye true
3946 | Up -> upbirdseye incr beye
3947 | Down -> downbirdseye incr beye
3948 | Left -> upbirdseye 1 beye
3949 | Right -> downbirdseye 1 beye
3951 | Prior ->
3952 begin match state.layout with
3953 | l :: _ ->
3954 if l.pagey != 0
3955 then (
3956 state.mode <- Birdseye (
3957 oconf, leftx, l.pageno, hooverpageno, anchor
3959 gotopage1 l.pageno 0;
3961 else (
3962 let layout = layout state.x (state.y-state.winh)
3963 state.winw
3964 (pgh state.layout) in
3965 match layout with
3966 | [] -> gotoxy state.x (clamp (-state.winh))
3967 | l :: _ ->
3968 state.mode <- Birdseye (
3969 oconf, leftx, l.pageno, hooverpageno, anchor
3971 gotopage1 l.pageno 0
3974 | [] -> gotoxy state.x (clamp (-state.winh))
3975 end;
3977 | Next ->
3978 begin match List.rev state.layout with
3979 | l :: _ ->
3980 let layout = layout state.x
3981 (state.y + (pgh state.layout))
3982 state.winw state.winh in
3983 begin match layout with
3984 | [] ->
3985 let incr = l.pageh - l.pagevh in
3986 if incr = 0
3987 then (
3988 state.mode <-
3989 Birdseye (
3990 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
3992 postRedisplay "birdseye pagedown";
3994 else gotoxy state.x (clamp (incr + conf.interpagespace*2));
3996 | l :: _ ->
3997 state.mode <-
3998 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
3999 gotopage1 l.pageno 0;
4002 | [] -> gotoxy state.x (clamp state.winh)
4003 end;
4005 | Home ->
4006 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
4007 gotopage1 0 0
4009 | End ->
4010 let pageno = state.pagecount - 1 in
4011 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
4012 if not (pagevisible state.layout pageno)
4013 then
4014 let h =
4015 match List.rev state.pdims with
4016 | [] -> state.winh
4017 | (_, _, h, _) :: _ -> h
4019 gotoxy
4020 state.x
4021 (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
4022 else postRedisplay "birdseye end";
4024 | Delete|Insert|Ascii _|Code _|Ctrl _|Fn _|Backspace -> viewkeyboard key mask
4027 let drawpage l =
4028 let color =
4029 match state.mode with
4030 | Textentry _ -> scalecolor 0.4
4031 | LinkNav _ | View -> scalecolor 1.0
4032 | Birdseye (_, _, pageno, hooverpageno, _) ->
4033 if l.pageno = hooverpageno
4034 then scalecolor 0.9
4035 else (
4036 if l.pageno = pageno
4037 then (
4038 let c = scalecolor 1.0 in
4039 GlDraw.color c;
4040 GlDraw.line_width 3.0;
4041 let dispx = l.pagedispx in
4042 linerect
4043 (float (dispx-1)) (float (l.pagedispy-1))
4044 (float (dispx+l.pagevw+1))
4045 (float (l.pagedispy+l.pagevh+1));
4046 GlDraw.line_width 1.0;
4049 else scalecolor 0.8
4052 drawtiles l color;
4055 let postdrawpage l linkindexbase =
4056 match getopaque l.pageno with
4057 | Some opaque ->
4058 if tileready l l.pagex l.pagey
4059 then
4060 let x = l.pagedispx - l.pagex
4061 and y = l.pagedispy - l.pagey in
4062 let hlmask =
4063 match conf.columns with
4064 | Csingle _ | Cmulti _ ->
4065 (if conf.hlinks then 1 else 0)
4066 + (if state.glinks
4067 && not (isbirdseye state.mode) then 2 else 0)
4068 | Csplit _ -> 0
4070 let s =
4071 match state.mode with
4072 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
4073 | Textentry _
4074 | Birdseye _
4075 | View
4076 | LinkNav _ -> E.s
4078 Hashtbl.find_all state.prects l.pageno |>
4079 List.iter (fun vals -> drawprect opaque x y vals);
4080 let n = postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize) in
4081 if n < 0
4082 then (Glutils.redisplay := true; 0)
4083 else n
4084 else 0
4085 | _ -> 0
4088 let scrollindicator () =
4089 let sbw, ph, sh = state.uioh#scrollph in
4090 let sbh, pw, sw = state.uioh#scrollpw in
4092 let x0,x1,hx0 =
4093 if conf.leftscroll
4094 then (0, sbw, sbw)
4095 else ((state.winw - sbw), state.winw, 0)
4098 Gl.enable `blend;
4099 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
4100 let (r, g, b, alpha) = conf.sbarcolor in
4101 GlDraw.color (r, g, b) ~alpha;
4102 filledrect (float x0) 0. (float x1) (float state.winh);
4103 filledrect
4104 (float hx0) (float (state.winh - sbh))
4105 (float (hx0 + state.winw)) (float state.winh);
4106 let (r, g, b, alpha) = conf.sbarhndlcolor in
4107 GlDraw.color (r, g, b) ~alpha;
4109 filledrect (float x0) ph (float x1) (ph +. sh);
4110 let pw = pw +. float hx0 in
4111 filledrect pw (float (state.winh - sbh)) (pw +. sw) (float state.winh);
4112 Gl.disable `blend;
4115 let showsel () =
4116 match state.mstate with
4117 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ -> ()
4118 | Msel ((x0, y0), (x1, y1)) ->
4119 let identify opaque l px py = Some (opaque, l.pageno, px, py) in
4120 let o0,n0,px0,py0 = onppundermouse identify x0 y0 (~< E.s, -1, 0, 0) in
4121 let _o1,n1,px1,py1 = onppundermouse identify x1 y1 (~< E.s, -1, 0, 0) in
4122 if n0 != -1 && n0 = n1 then seltext o0 (px0, py0, px1, py1);
4125 let showrects = function
4126 | [] -> ()
4127 | rects ->
4128 Gl.enable `blend;
4129 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
4130 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
4131 List.iter
4132 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
4133 List.iter (fun l ->
4134 if l.pageno = pageno
4135 then
4136 let dx = float (l.pagedispx - l.pagex) in
4137 let dy = float (l.pagedispy - l.pagey) in
4138 let r, g, b, alpha = c in
4139 GlDraw.color (r, g, b) ~alpha;
4140 filledrect2
4141 (x0+.dx) (y0+.dy)
4142 (x1+.dx) (y1+.dy)
4143 (x3+.dx) (y3+.dy)
4144 (x2+.dx) (y2+.dy);
4145 ) state.layout
4146 ) rects;
4147 Gl.disable `blend;
4150 let display () =
4151 GlDraw.color (scalecolor2 conf.bgcolor);
4152 GlClear.color (scalecolor2 conf.bgcolor);
4153 GlClear.clear [`color];
4154 List.iter drawpage state.layout;
4155 let rects =
4156 match state.mode with
4157 | LinkNav (Ltexact (pageno, linkno)) ->
4158 begin match getopaque pageno with
4159 | Some opaque ->
4160 let x0, y0, x1, y1 = getlinkrect opaque linkno in
4161 let color = (0.0, 0.0, 0.5, 0.5) in
4162 (pageno, color,
4163 (float x0, float y0,
4164 float x1, float y0,
4165 float x1, float y1,
4166 float x0, float y1)
4167 ) :: state.rects
4168 | None -> state.rects
4170 | LinkNav (Ltgendir _) | LinkNav (Ltnotready _)
4171 | Birdseye _
4172 | Textentry _
4173 | View -> state.rects
4175 showrects rects;
4176 let rec postloop linkindexbase = function
4177 | l :: rest ->
4178 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
4179 postloop linkindexbase rest
4180 | [] -> ()
4182 showsel ();
4183 postloop 0 state.layout;
4184 state.uioh#display;
4185 begin match state.mstate with
4186 | Mzoomrect ((x0, y0), (x1, y1)) ->
4187 Gl.enable `blend;
4188 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
4189 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
4190 filledrect (float x0) (float y0) (float x1) (float y1);
4191 Gl.disable `blend;
4192 | Msel _
4193 | Mpan _
4194 | Mscrolly | Mscrollx
4195 | Mzoom _
4196 | Mnone -> ()
4197 end;
4198 enttext ();
4199 scrollindicator ();
4200 Wsi.swapb ();
4203 let display () =
4204 match state.reload with
4205 | Some (x, y, t) ->
4206 if x != state.x || y != state.y || abs_float @@ now () -. t > 0.5
4207 || (state.layout != [] && layoutready state.layout)
4208 then (
4209 state.reload <- None;
4210 display ()
4212 | None -> display ()
4215 let zoomrect x y x1 y1 =
4216 let x0 = min x x1
4217 and x1 = max x x1
4218 and y0 = min y y1 in
4219 let zoom = (float state.w) /. float (x1 - x0) in
4220 let margin =
4221 let simple () =
4222 if state.w < state.winw
4223 then (state.winw - state.w) / 2
4224 else 0
4226 match conf.fitmodel with
4227 | FitWidth | FitProportional -> simple ()
4228 | FitPage ->
4229 match conf.columns with
4230 | Csplit _ ->
4231 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
4232 | Cmulti _ | Csingle _ -> simple ()
4234 gotoxy ((state.x + margin) - x0) (state.y + y0);
4235 state.anchor <- getanchor ();
4236 setzoom zoom;
4237 resetmstate ();
4240 let annot inline x y =
4241 match unproject x y with
4242 | Some (opaque, n, ux, uy) ->
4243 let add text =
4244 addannot opaque ux uy text;
4245 wcmd "freepage %s" (~> opaque);
4246 Hashtbl.remove state.pagemap (n, state.gen);
4247 flushtiles ();
4248 gotoxy state.x state.y
4250 if inline
4251 then
4252 let ondone s = add s in
4253 let mode = state.mode in
4254 state.mode <- Textentry (
4255 ("annotation: ", E.s, None, textentry, ondone, true),
4256 fun _ -> state.mode <- mode);
4257 state.text <- E.s;
4258 enttext ();
4259 postRedisplay "annot"
4260 else add @@ getusertext E.s
4261 | _ -> ()
4264 let zoomblock x y =
4265 let g opaque l px py =
4266 match rectofblock opaque px py with
4267 | Some a ->
4268 let x0 = a.(0) -. 20. in
4269 let x1 = a.(1) +. 20. in
4270 let y0 = a.(2) -. 20. in
4271 let zoom = (float state.w) /. (x1 -. x0) in
4272 let pagey = getpagey l.pageno in
4273 let margin = (state.w - l.pagew)/2 in
4274 let nx = -truncate x0 - margin in
4275 gotoxy nx (pagey + truncate y0);
4276 state.anchor <- getanchor ();
4277 setzoom zoom;
4278 None
4279 | None -> None
4281 match conf.columns with
4282 | Csplit _ ->
4283 impmsg "block zooming does not work properly in split columns mode"
4284 | Cmulti _ | Csingle _ -> onppundermouse g x y ()
4287 let scrollx x =
4288 let winw = state.winw - 1 in
4289 let s = float x /. float winw in
4290 let destx = truncate (float (state.w + winw) *. s) in
4291 gotoxy (winw - destx) state.y;
4292 state.mstate <- Mscrollx;
4295 let scrolly y =
4296 let s = float y /. float state.winh in
4297 let desty = truncate (s *. float (maxy ())) in
4298 gotoxy state.x desty;
4299 state.mstate <- Mscrolly;
4302 let viewmulticlick clicks x y mask =
4303 let g opaque l px py =
4304 let mark =
4305 match clicks with
4306 | 2 -> Mark_word
4307 | 3 -> Mark_line
4308 | 4 -> Mark_block
4309 | _ -> Mark_page
4311 if markunder opaque px py mark
4312 then (
4313 Some (fun () ->
4314 let dopipe cmd =
4315 match getopaque l.pageno with
4316 | None -> ()
4317 | Some opaque -> pipesel opaque cmd
4319 state.roam <- (fun () -> dopipe conf.paxcmd);
4320 if not (Wsi.withctrl mask) then dopipe conf.selcmd;
4323 else None
4325 postRedisplay "viewmulticlick";
4326 onppundermouse g x y (fun () -> impmsg "nothing to select") ();
4329 let canselect () =
4330 match conf.columns with
4331 | Csplit _ -> false
4332 | Csingle _ | Cmulti _ -> conf.angle mod 360 = 0
4335 let viewmouse button down x y mask =
4336 match button with
4337 | n when (n == 4 || n == 5) && not down ->
4338 if Wsi.withctrl mask
4339 then (
4340 let incr =
4341 if n = 5
4342 then if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
4343 else if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
4345 let fx, fy =
4346 match state.mstate with
4347 | Mzoom (oldn, _, pos) when n = oldn -> pos
4348 | Mzoomrect _ | Mnone | Mpan _
4349 | Msel _ | Mscrollx | Mscrolly | Mzoom _ -> (x, y)
4351 let zoom = conf.zoom -. incr in
4352 state.mstate <- Mzoom (n, 0, (x, y));
4353 if false && abs (fx - x) > 5 || abs (fy - y) > 5
4354 then pivotzoom ~x ~y zoom
4355 else pivotzoom zoom
4357 else (
4358 match state.autoscroll with
4359 | Some step -> setautoscrollspeed step (n=4)
4360 | None ->
4361 if conf.wheelbypage || conf.presentation
4362 then (
4363 if n = 4
4364 then prevpage ()
4365 else nextpage ()
4367 else
4368 let incr = if n = 4 then -conf.scrollstep else conf.scrollstep in
4369 let incr = incr * 2 in
4370 let y = clamp incr in
4371 gotoxy state.x y
4374 | n when (n = 6 || n = 7) && not down && canpan () ->
4375 let x =
4376 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep) in
4377 gotoxy x state.y
4379 | 1 when Wsi.withshift mask ->
4380 state.mstate <- Mnone;
4381 if not down
4382 then (
4383 match unproject x y with
4384 | None -> ()
4385 | Some (_, pageno, ux, uy) ->
4386 let cmd = Printf.sprintf
4387 "%s %s %d %d %d"
4388 conf.stcmd state.path pageno ux uy
4390 match spawn cmd [] with
4391 | exception exn ->
4392 impmsg "execution of synctex command(%S) failed: %S"
4393 conf.stcmd @@ exntos exn
4394 | _pid -> ()
4397 | 1 when Wsi.withctrl mask ->
4398 if down
4399 then (
4400 Wsi.setcursor Wsi.CURSOR_FLEUR;
4401 state.mstate <- Mpan (x, y)
4403 else state.mstate <- Mnone
4405 | 3 ->
4406 if down
4407 then (
4408 if Wsi.withshift mask
4409 then (
4410 annot conf.annotinline x y;
4411 postRedisplay "addannot"
4413 else
4414 let p = (x, y) in
4415 Wsi.setcursor Wsi.CURSOR_CYCLE;
4416 state.mstate <- Mzoomrect (p, p)
4418 else (
4419 match state.mstate with
4420 | Mzoomrect ((x0, y0), _) ->
4421 if abs (x-x0) > 10 && abs (y - y0) > 10
4422 then zoomrect x0 y0 x y
4423 else (
4424 resetmstate ();
4425 postRedisplay "kill accidental zoom rect";
4427 | Msel _
4428 | Mpan _
4429 | Mscrolly | Mscrollx
4430 | Mzoom _
4431 | Mnone -> resetmstate ()
4434 | 1 when vscrollhit x ->
4435 if down
4436 then
4437 let _, position, sh = state.uioh#scrollph in
4438 if y > truncate position && y < truncate (position +. sh)
4439 then state.mstate <- Mscrolly
4440 else scrolly y
4441 else state.mstate <- Mnone
4443 | 1 when y > state.winh - hscrollh () ->
4444 if down
4445 then
4446 let _, position, sw = state.uioh#scrollpw in
4447 if x > truncate position && x < truncate (position +. sw)
4448 then state.mstate <- Mscrollx
4449 else scrollx x
4450 else state.mstate <- Mnone
4452 | 1 when state.bzoom -> if not down then zoomblock x y
4454 | 1 ->
4455 let dest = if down then getunder x y else Unone in
4456 begin match dest with
4457 | Ulinkuri _ -> gotounder dest
4458 | Unone when down ->
4459 Wsi.setcursor Wsi.CURSOR_FLEUR;
4460 state.mstate <- Mpan (x, y);
4461 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
4462 | Unone | Utext _ ->
4463 if down
4464 then (
4465 if canselect ()
4466 then (
4467 state.mstate <- Msel ((x, y), (x, y));
4468 postRedisplay "mouse select";
4471 else (
4472 match state.mstate with
4473 | Mnone -> ()
4474 | Mzoom _ | Mscrollx | Mscrolly -> state.mstate <- Mnone
4475 | Mzoomrect ((x0, y0), _) -> zoomrect x0 y0 x y
4476 | Mpan _ ->
4477 Wsi.setcursor Wsi.CURSOR_INHERIT;
4478 state.mstate <- Mnone
4479 | Msel ((x0, y0), (x1, y1)) ->
4480 let rec loop = function
4481 | [] -> ()
4482 | l :: rest ->
4483 let inside =
4484 let a0 = l.pagedispy in
4485 let a1 = a0 + l.pagevh in
4486 let b0 = l.pagedispx in
4487 let b1 = b0 + l.pagevw in
4488 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
4489 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
4491 if inside
4492 then
4493 match getopaque l.pageno with
4494 | Some opaque ->
4495 let dosel cmd () =
4496 pipef ~closew:false "Msel"
4497 (fun w ->
4498 copysel w opaque;
4499 postRedisplay "Msel") cmd
4501 dosel conf.selcmd ();
4502 state.roam <- dosel conf.paxcmd;
4503 | None -> ()
4504 else loop rest
4506 loop state.layout;
4507 resetmstate ();
4510 | _ -> ()
4513 let birdseyemouse button down x y mask
4514 (conf, leftx, _, hooverpageno, anchor) =
4515 match button with
4516 | 1 when down ->
4517 let rec loop = function
4518 | [] -> ()
4519 | l :: rest ->
4520 if y > l.pagedispy && y < l.pagedispy + l.pagevh
4521 && x > l.pagedispx && x < l.pagedispx + l.pagevw
4522 then
4523 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false
4524 else loop rest
4526 loop state.layout
4527 | 3 -> ()
4528 | _ -> viewmouse button down x y mask
4531 let uioh = object
4532 method display = ()
4534 method key key mask =
4535 begin match state.mode with
4536 | Textentry textentry -> textentrykeyboard key mask textentry
4537 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
4538 | View -> viewkeyboard key mask
4539 | LinkNav linknav -> linknavkeyboard key mask linknav
4540 end;
4541 state.uioh
4543 method button button bstate x y mask =
4544 begin match state.mode with
4545 | LinkNav _ | View -> viewmouse button bstate x y mask
4546 | Birdseye beye -> birdseyemouse button bstate x y mask beye
4547 | Textentry _ -> ()
4548 end;
4549 state.uioh
4551 method multiclick clicks x y mask =
4552 begin match state.mode with
4553 | LinkNav _ | View -> viewmulticlick clicks x y mask
4554 | Birdseye _ | Textentry _ -> ()
4555 end;
4556 state.uioh
4558 method motion x y =
4559 begin match state.mode with
4560 | Textentry _ -> ()
4561 | View | Birdseye _ | LinkNav _ ->
4562 match state.mstate with
4563 | Mzoom _ | Mnone -> ()
4564 | Mpan (x0, y0) ->
4565 let dx = x - x0
4566 and dy = y0 - y in
4567 state.mstate <- Mpan (x, y);
4568 let x = if canpan () then panbound (state.x + dx) else state.x in
4569 let y = clamp dy in
4570 gotoxy x y
4572 | Msel (a, _) ->
4573 state.mstate <- Msel (a, (x, y));
4574 postRedisplay "motion select";
4576 | Mscrolly ->
4577 let y = min state.winh (max 0 y) in
4578 scrolly y
4580 | Mscrollx ->
4581 let x = min state.winw (max 0 x) in
4582 scrollx x
4584 | Mzoomrect (p0, _) ->
4585 state.mstate <- Mzoomrect (p0, (x, y));
4586 postRedisplay "motion zoomrect";
4587 end;
4588 state.uioh
4590 method pmotion x y =
4591 begin match state.mode with
4592 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
4593 let rec loop = function
4594 | [] ->
4595 if hooverpageno != -1
4596 then (
4597 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
4598 postRedisplay "pmotion birdseye no hoover";
4600 | l :: rest ->
4601 if y > l.pagedispy && y < l.pagedispy + l.pagevh
4602 && x > l.pagedispx && x < l.pagedispx + l.pagevw
4603 then (
4604 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
4605 postRedisplay "pmotion birdseye hoover";
4607 else loop rest
4609 loop state.layout
4611 | Textentry _ -> ()
4613 | LinkNav _ | View ->
4614 match state.mstate with
4615 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ -> ()
4616 | Mnone ->
4617 updateunder x y;
4618 if canselect ()
4619 then
4620 match conf.pax with
4621 | None -> ()
4622 | Some past ->
4623 let now = now () in
4624 let delta = now -. past in
4625 if delta > 0.01
4626 then paxunder x y
4627 else conf.pax <- Some now
4628 end;
4629 state.uioh
4631 method infochanged _ = ()
4633 method scrollph =
4634 let maxy = maxy () in
4635 let p, h =
4636 if maxy = 0
4637 then 0.0, float state.winh
4638 else scrollph state.y maxy
4640 vscrollw (), p, h
4642 method scrollpw =
4643 let fwinw = float (state.winw - vscrollw ()) in
4644 let sw =
4645 let sw = fwinw /. float state.w in
4646 let sw = fwinw *. sw in
4647 max sw (float conf.scrollh)
4649 let position =
4650 let maxx = state.w + state.winw in
4651 let x = state.winw - state.x in
4652 let percent = float x /. float maxx in
4653 (fwinw -. sw) *. percent
4655 hscrollh (), position, sw
4657 method modehash =
4658 let modename =
4659 match state.mode with
4660 | LinkNav _ -> "links"
4661 | Textentry _ -> "textentry"
4662 | Birdseye _ -> "birdseye"
4663 | View -> "view"
4665 findkeyhash conf modename
4667 method eformsgs = true
4668 method alwaysscrolly = false
4669 method scroll dx dy =
4670 let x = if canpan () then panbound (state.x + dx) else state.x in
4671 gotoxy x (clamp (2 * dy));
4672 state.uioh
4673 method zoom z x y =
4674 pivotzoom ~x ~y (conf.zoom *. exp z);
4675 end;;
4677 let addrect pageno r g b a x0 y0 x1 y1 =
4678 Hashtbl.add state.prects pageno [|r; g; b; a; x0; y0; x1; y1|];
4681 let ract cmds =
4682 let cl = splitatchar cmds ' ' in
4683 let scan s fmt f =
4684 try Scanf.sscanf s fmt f
4685 with exn -> adderrfmt "remote exec" "error processing '%S': %s\n"
4686 cmds @@ exntos exn
4688 let rectx s pageno (r, g, b, a) x0 y0 x1 y1 =
4689 vlog "%s page %d color (%f %f %f %f) x0,y0,x1,y1 = %f %f %f %f"
4690 s pageno r g b a x0 y0 x1 y1;
4691 onpagerect
4692 pageno
4693 (fun w h ->
4694 let _,w1,h1,_ = getpagedim pageno in
4695 let sw = float w1 /. float w
4696 and sh = float h1 /. float h in
4697 let x0s = x0 *. sw
4698 and x1s = x1 *. sw
4699 and y0s = y0 *. sh
4700 and y1s = y1 *. sh in
4701 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
4702 let color = (r, g, b, a) in
4703 if conf.verbose then debugrect rect;
4704 state.rects <- (pageno, color, rect) :: state.rects;
4705 postRedisplay s;
4708 match cl with
4709 | "reload", "" -> reload ()
4710 | "goto", args ->
4711 scan args "%u %f %f"
4712 (fun pageno x y ->
4713 let cmd, _ = state.geomcmds in
4714 if emptystr cmd
4715 then gotopagexy pageno x y
4716 else
4717 let f prevf () =
4718 gotopagexy pageno x y;
4719 prevf ()
4721 state.reprf <- f state.reprf
4723 | "goto1", args -> scan args "%u %f" gotopage
4724 | "gotor", args -> scan args "%S" gotoremote
4725 | "rect", args ->
4726 scan args "%u %u %f %f %f %f"
4727 (fun pageno c x0 y0 x1 y1 ->
4728 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
4729 rectx "rect" pageno color x0 y0 x1 y1;
4731 | "prect", args ->
4732 scan args "%u %f %f %f %f %f %f %f %f"
4733 (fun pageno r g b alpha x0 y0 x1 y1 ->
4734 addrect pageno r g b alpha x0 y0 x1 y1;
4735 postRedisplay "prect"
4737 | "pgoto", args ->
4738 scan args "%u %f %f"
4739 (fun pageno x y ->
4740 let optopaque =
4741 match getopaque pageno with
4742 | Some opaque -> opaque
4743 | None -> ~< E.s
4745 pgoto optopaque pageno x y;
4746 let rec fixx = function
4747 | [] -> ()
4748 | l :: rest ->
4749 if l.pageno = pageno
4750 then gotoxy (state.x - l.pagedispx) state.y
4751 else fixx rest
4753 let layout =
4754 let mult =
4755 match conf.columns with
4756 | Csingle _ | Csplit _ -> 1
4757 | Cmulti ((n, _, _), _) -> n
4759 layout 0 state.y (state.winw * mult) state.winh
4761 fixx layout
4763 | "activatewin", "" -> Wsi.activatewin ()
4764 | "quit", "" -> raise Quit
4765 | "keys", keys ->
4766 begin try
4767 let l = Config.keys_of_string keys in
4768 List.iter (fun (k, m) -> keyboard k m) l
4769 with exn -> adderrfmt "error processing keys" "`%S': %s\n"
4770 cmds @@ exntos exn
4772 | "clearrects", "" ->
4773 Hashtbl.clear state.prects;
4774 postRedisplay "clearrects"
4775 | _ ->
4776 adderrfmt "remote command"
4777 "error processing remote command: %S\n" cmds;
4780 let remote =
4781 let scratch = Bytes.create 80 in
4782 let buf = Buffer.create 80 in
4783 fun fd ->
4784 match tempfailureretry (Unix.read fd scratch 0) 80 with
4785 | exception Unix.Unix_error (Unix.EAGAIN, _, _) -> None
4786 | 0 ->
4787 Unix.close fd;
4788 if Buffer.length buf > 0
4789 then (
4790 let s = Buffer.contents buf in
4791 Buffer.clear buf;
4792 ract s;
4794 None
4795 | n ->
4796 let rec eat ppos =
4797 let nlpos =
4798 match Bytes.index_from scratch ppos '\n' with
4799 | pos -> if pos >= n then -1 else pos
4800 | exception Not_found -> -1
4802 if nlpos >= 0
4803 then (
4804 Buffer.add_subbytes buf scratch ppos (nlpos-ppos);
4805 let s = Buffer.contents buf in
4806 Buffer.clear buf;
4807 ract s;
4808 eat (nlpos+1);
4810 else (
4811 Buffer.add_subbytes buf scratch ppos (n-ppos);
4812 Some fd
4814 in eat 0
4817 let remoteopen path =
4818 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
4819 with exn ->
4820 adderrfmt "remoteopen" "error opening %S: %s" path @@ exntos exn;
4821 None
4824 let () =
4825 Utils.vlogf := (fun s -> if conf.verbose then prerr_endline s else ignore s);
4826 let gcconfig = ref false in
4827 let trimcachepath = ref E.s in
4828 let rcmdpath = ref E.s in
4829 let pageno = ref None in
4830 let openlast = ref false in
4831 let doreap = ref false in
4832 let csspath = ref None in
4833 selfexec := Sys.executable_name;
4834 Arg.parse
4835 (Arg.align
4836 [("-p", Arg.String (fun s -> state.password <- s),
4837 "<password> Set password");
4839 ("-f", Arg.String
4840 (fun s ->
4841 Config.fontpath := s;
4842 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
4844 "<path> Set path to the user interface font");
4846 ("-c", Arg.String
4847 (fun s ->
4848 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
4849 Config.confpath := s),
4850 "<path> Set path to the configuration file");
4852 ("-last", Arg.Set openlast, " Open last document");
4854 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
4855 "<page-number> Jump to page");
4857 ("-tcf", Arg.String (fun s -> trimcachepath := s),
4858 "<path> Set path to the trim cache file");
4860 ("-dest", Arg.String (fun s -> state.nameddest <- s),
4861 "<named-destination> Set named destination");
4863 ("-remote", Arg.String (fun s -> rcmdpath := s),
4864 "<path> Set path to the source of remote commands");
4866 ("-gc", Arg.Set gcconfig, " Collect config garbage");
4868 ("-v", Arg.Unit (fun () ->
4869 Printf.printf
4870 "%s\nconfiguration file: %s\n"
4871 (Help.version ())
4872 Config.defconfpath;
4873 exit 0), " Print version and exit");
4875 ("-css", Arg.String (fun s -> csspath := Some s),
4876 "<path> Set path to the style sheet to use with EPUB/HTML");
4878 ("-origin", Arg.String (fun s -> state.origin <- s),
4879 "<origin> <undocumented>");
4881 ("-no-title", Arg.Set ignoredoctitlte, " ignore document title");
4882 ("-layout-height", Arg.Set_int layouth,
4883 "<height> layout height html/epub/etc (-1, 0, N)");
4886 (fun s -> state.path <- s)
4887 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:");
4889 let histmode = emptystr state.path && not !openlast in
4891 if not (Config.load !openlast)
4892 then dolog "failed to load configuration";
4894 begin match !pageno with
4895 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
4896 | None -> ()
4897 end;
4899 fillhelp ();
4900 if !gcconfig
4901 then (
4902 Config.gc ();
4903 exit 0
4906 let mu =
4907 object (self)
4908 val mutable m_clicks = 0
4909 val mutable m_click_x = 0
4910 val mutable m_click_y = 0
4911 val mutable m_lastclicktime = infinity
4913 method private cleanup =
4914 state.roam <- noroam;
4915 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap
4916 method expose = postRedisplay "expose"
4917 method visible v =
4918 let name =
4919 match v with
4920 | Wsi.Unobscured -> "unobscured"
4921 | Wsi.PartiallyObscured -> "partiallyobscured"
4922 | Wsi.FullyObscured -> "fullyobscured"
4924 vlog "visibility change %s" name
4925 method display = display ()
4926 method map mapped = vlog "mapped %b" mapped
4927 method reshape w h =
4928 self#cleanup;
4929 reshape w h
4930 method mouse b d x y m =
4931 if d && canselect ()
4932 then (
4934 * http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx
4936 m_click_x <- x;
4937 m_click_y <- y;
4938 if b = 1
4939 then (
4940 let t = now () in
4941 if abs x - m_click_x > 10
4942 || abs y - m_click_y > 10
4943 || abs_float (t -. m_lastclicktime) > 0.3
4944 then m_clicks <- 0;
4945 m_clicks <- m_clicks + 1;
4946 m_lastclicktime <- t;
4947 if m_clicks = 1
4948 then (
4949 self#cleanup;
4950 postRedisplay "cleanup";
4951 state.uioh <- state.uioh#button b d x y m;
4953 else state.uioh <- state.uioh#multiclick m_clicks x y m
4955 else (
4956 self#cleanup;
4957 m_clicks <- 0;
4958 m_lastclicktime <- infinity;
4959 state.uioh <- state.uioh#button b d x y m
4962 else state.uioh <- state.uioh#button b d x y m
4963 method motion x y =
4964 state.mpos <- (x, y);
4965 state.uioh <- state.uioh#motion x y
4966 method pmotion x y =
4967 state.mpos <- (x, y);
4968 state.uioh <- state.uioh#pmotion x y
4969 method key k m =
4970 vlog "k=%#x m=%#x" k m;
4971 let mascm = m land (
4972 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
4973 ) in
4974 let keyboard k m =
4975 let x = state.x and y = state.y in
4976 keyboard k m;
4977 if x != state.x || y != state.y then self#cleanup
4979 match state.keystate with
4980 | KSnone ->
4981 let km = k, mascm in
4982 begin
4983 match
4984 let modehash = state.uioh#modehash in
4985 try Hashtbl.find modehash km
4986 with Not_found ->
4987 try Hashtbl.find (findkeyhash conf "global") km
4988 with Not_found -> KMinsrt (k, m)
4989 with
4990 | KMinsrt (k, m) -> keyboard k m
4991 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
4992 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
4994 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
4995 List.iter (fun (k, m) -> keyboard k m) insrt;
4996 state.keystate <- KSnone
4997 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
4998 state.keystate <- KSinto (keys, insrt)
4999 | KSinto _ -> state.keystate <- KSnone
5001 method enter x y =
5002 state.mpos <- (x, y);
5003 state.uioh <- state.uioh#pmotion x y
5004 method leave = state.mpos <- (-1, -1)
5005 method winstate wsl = state.winstate <- wsl
5006 method quit : 'a. 'a = raise Quit
5007 method scroll dx dy = state.uioh <- state.uioh#scroll dx dy
5008 method zoom z x y = state.uioh#zoom z x y
5009 method opendoc path =
5010 state.mode <- View;
5011 state.uioh <- uioh;
5012 postRedisplay "opendoc";
5013 opendoc path state.password
5016 let wsfd, winw, winh = Wsi.init mu conf.cwinw conf.cwinh platform in
5017 state.wsfd <- wsfd;
5019 if not @@ List.exists GlMisc.check_extension
5020 [ "GL_ARB_texture_rectangle"
5021 ; "GL_EXT_texture_recangle"
5022 ; "GL_NV_texture_rectangle" ]
5023 then (dolog "OpenGL does not suppport rectangular textures"; exit 1);
5025 let cs, ss =
5026 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
5027 | exception exn ->
5028 dolog "socketpair failed: %s" @@ exntos exn;
5029 exit 1
5030 | (r, w) ->
5031 cloexec r;
5032 cloexec w;
5033 r, w
5036 setcheckers conf.checkers;
5037 begin match !csspath with
5038 | None -> ()
5039 | Some "" -> conf.css <- E.s
5040 | Some path ->
5041 let css = filecontents path in
5042 let l = String.length css in
5043 conf.css <-
5044 if substratis css (l-2) "\r\n"
5045 then String.sub css 0 (l-2)
5046 else (if css.[l-1] = '\n' then String.sub css 0 (l-1) else css)
5047 end;
5048 init cs (
5049 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
5050 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
5051 !Config.fontpath, !trimcachepath
5053 List.iter GlArray.enable [`texture_coord; `vertex];
5054 GlTex.env (`color conf.texturecolor);
5055 state.ss <- ss;
5056 reshape ~firsttime:true winw winh;
5057 state.uioh <- uioh;
5058 if histmode
5059 then (
5060 Wsi.settitle "llpp (history)";
5061 enterhistmode ();
5063 else (
5064 state.text <- "Opening " ^ (mbtoutf8 state.path);
5065 opendoc state.path state.password;
5067 display ();
5068 Wsi.mapwin ();
5069 Wsi.setcursor Wsi.CURSOR_INHERIT;
5070 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
5072 let rec reap () =
5073 match Unix.waitpid [Unix.WNOHANG] ~-1 with
5074 | exception (Unix.Unix_error (Unix.ECHILD, _, _)) -> ()
5075 | exception exn -> dolog "Unix.waitpid: %s" @@ exntos exn
5076 | 0, _ -> ()
5077 | _pid, _status -> reap ()
5079 Sys.set_signal Sys.sigchld (Sys.Signal_handle (fun _ -> doreap := true));
5081 let optrfd =
5082 ref (if nonemptystr !rcmdpath then remoteopen !rcmdpath else None)
5085 let rec loop deadline =
5086 if !doreap
5087 then (
5088 doreap := false;
5089 reap ()
5091 let r = [state.ss; state.wsfd] in
5092 let r =
5093 match !optrfd with
5094 | None -> r
5095 | Some fd -> fd :: r
5097 if !redisplay
5098 then (
5099 Glutils.redisplay := false;
5100 display ();
5102 let timeout =
5103 let now = now () in
5104 if deadline > now
5105 then (
5106 if deadline = infinity
5107 then ~-.1.0
5108 else max 0.0 (deadline -. now)
5110 else 0.0
5112 let r, _, _ =
5113 try Unix.select r [] [] timeout
5114 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
5116 begin match r with
5117 | [] ->
5118 let newdeadline =
5119 match state.autoscroll with
5120 | Some step when step != 0 ->
5121 if state.slideshow land 1 = 1
5122 then (
5123 if state.slideshow land 2 = 0
5124 then state.slideshow <- state.slideshow lor 2
5125 else if step < 0 then prevpage () else nextpage ();
5126 deadline +. (float (abs step))
5128 else
5129 let y = state.y + step in
5130 let fy = if conf.maxhfit then state.winh else 0 in
5131 let y =
5132 if y < 0
5133 then state.maxy - fy
5134 else if y >= state.maxy - fy then 0 else y
5136 gotoxy state.x y;
5137 deadline +. 0.01
5138 | _ -> infinity
5140 loop newdeadline
5142 | l ->
5143 let rec checkfds = function
5144 | [] -> ()
5145 | fd :: rest when fd = state.ss ->
5146 let cmd = rcmd state.ss in
5147 act cmd;
5148 checkfds rest
5150 | fd :: rest when fd = state.wsfd ->
5151 Wsi.readresp fd;
5152 checkfds rest
5154 | fd :: rest when Some fd = !optrfd ->
5155 begin match remote fd with
5156 | None -> optrfd := remoteopen !rcmdpath;
5157 | opt -> optrfd := opt
5158 end;
5159 checkfds rest
5161 | _ :: rest ->
5162 dolog "select returned unknown descriptor";
5163 checkfds rest
5165 checkfds l;
5166 let newdeadline =
5167 let deadline1 =
5168 if deadline = infinity
5169 then now () +. 0.01
5170 else deadline
5172 match state.autoscroll with
5173 | Some step when step != 0 -> deadline1
5174 | _ -> infinity
5176 loop newdeadline
5177 end;
5179 match loop infinity with
5180 | exception Quit ->
5181 Config.save leavebirdseye;
5182 if hasunsavedchanges ()
5183 then save ()
5184 | _ -> error "umpossible - infinity reached"