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