Cosmetics
[llpp.git] / main.ml
blob73bf8e8ac7adc2e31b30cca6b7686406ad24c9ee
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 vlog fmt = if conf.verbose then dolog fmt else Printf.kprintf ignore fmt;;
38 let launchpath () =
39 if emptystr conf.pathlauncher
40 then dolog "%s" state.path
41 else (
42 let command =
43 Str.global_replace Utils.Re.percent state.path conf.pathlauncher in
44 match spawn command [] with
45 | _pid -> ()
46 | exception exn -> dolog "failed to execute `%s': %s" command @@ exntos exn
50 let getopaque pageno =
51 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
52 with Not_found -> None
55 let pagetranslatepoint l x y =
56 let dy = y - l.pagedispy in
57 let y = dy + l.pagey in
58 let dx = x - l.pagedispx in
59 let x = dx + l.pagex in
60 (x, y);
63 let onppundermouse g x y d =
64 let rec f = function
65 | l :: rest ->
66 begin match getopaque l.pageno with
67 | Some opaque ->
68 let x0 = l.pagedispx in
69 let x1 = x0 + l.pagevw in
70 let y0 = l.pagedispy in
71 let y1 = y0 + l.pagevh in
72 if y >= y0 && y <= y1 && x >= x0 && x <= x1
73 then
74 let px, py = pagetranslatepoint l x y in
75 match g opaque l px py with
76 | Some res -> res
77 | None -> f rest
78 else f rest
79 | _ -> f rest
80 end
81 | [] -> d
83 f state.layout
86 let getunder x y =
87 let g opaque l px py =
88 if state.bzoom
89 then (
90 match rectofblock opaque px py with
91 | Some [|x0;x1;y0;y1|] ->
92 let rect = (x0, y0, x1, y0, x1, y1, x0, y1) in
93 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
94 state.rects <- [l.pageno, color, rect];
95 postRedisplay "getunder";
96 | _ -> ()
98 let under = whatsunder opaque px py in
99 if under = Unone then None else Some under
101 onppundermouse g x y Unone
104 let unproject x y =
105 let g opaque l x y =
106 match unproject opaque x y with
107 | Some (x, y) -> Some (Some (opaque, l.pageno, x, y))
108 | None -> None
110 onppundermouse g x y None;
113 let showtext c s =
114 state.text <- Printf.sprintf "%c%s" c s;
115 postRedisplay "showtext";
118 let impmsg fmt = Format.ksprintf (fun s -> showtext '!' s) fmt;;
120 let pipesel opaque cmd =
121 if hassel opaque
122 then pipef ~closew:false "pipesel"
123 (fun w ->
124 copysel w opaque;
125 postRedisplay "pipesel"
126 ) cmd
129 let paxunder x y =
130 let g opaque l px py =
131 if markunder opaque px py conf.paxmark
132 then
133 Some (fun () ->
134 match getopaque l.pageno with
135 | None -> ()
136 | Some opaque -> pipesel opaque conf.paxcmd
138 else None
140 postRedisplay "paxunder";
141 if conf.paxmark = Mark_page
142 then
143 List.iter (fun l ->
144 match getopaque l.pageno with
145 | None -> ()
146 | Some opaque -> clearmark opaque) state.layout;
147 state.roam <- onppundermouse g x y (fun () -> impmsg "whoopsie daisy");
150 let undertext = function
151 | Unone -> "none"
152 | Ulinkuri s -> s
153 | Utext s -> "font: " ^ s
154 | Uannotation (opaque, slinkindex) ->
155 "annotation: " ^ getannotcontents opaque slinkindex
158 let updateunder x y =
159 match getunder x y with
160 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
161 | Ulinkuri uri ->
162 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
163 Wsi.setcursor Wsi.CURSOR_INFO
164 | Utext s ->
165 if conf.underinfo then showtext 'f' ("ont: " ^ s);
166 Wsi.setcursor Wsi.CURSOR_TEXT
167 | Uannotation _ ->
168 if conf.underinfo then showtext 'a' "nnotation";
169 Wsi.setcursor Wsi.CURSOR_INFO
172 let showlinktype under =
173 if conf.underinfo && under != Unone
174 then showtext ' ' @@ undertext under
177 let intentry_with_suffix text key =
178 let text =
179 match [@warning "-4"] key with
180 | Keys.Ascii ('0'..'9' as c) -> addchar text c
181 | Keys.Ascii ('k' | 'm' | 'g' | 'K' | 'M' | 'G' as c) ->
182 addchar text @@ asciilower c
183 | _ ->
184 state.text <- "invalid key";
185 text
187 TEcont text
190 let wcmd fmt =
191 let b = Buffer.create 16 in
192 Printf.kbprintf
193 (fun b ->
194 let b = Buffer.to_bytes b in
195 wcmd state.ss b @@ Bytes.length b
196 ) b fmt
199 let nogeomcmds = function
200 | s, [] -> emptystr s
201 | _ -> false
204 let layoutN ((columns, coverA, coverB), b) x y sw sh =
205 let rec fold accu n =
206 if n = Array.length b
207 then accu
208 else
209 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
210 if (vy - y) > sh &&
211 (n = coverA - 1
212 || n = state.pagecount - coverB
213 || (n - coverA) mod columns = columns - 1)
214 then accu
215 else
216 let accu =
217 if vy + h > y
218 then
219 let pagey = max 0 (y - vy) in
220 let pagedispy = if pagey > 0 then 0 else vy - y in
221 let pagedispx, pagex =
222 let pdx =
223 if n = coverA - 1 || n = state.pagecount - coverB
224 then x + (sw - w) / 2
225 else dx + xoff + x
227 if pdx < 0
228 then 0, -pdx
229 else pdx, 0
231 let pagevw =
232 let vw = sw - pagedispx in
233 let pw = w - pagex in
234 min vw pw
236 let pagevh = min (h - pagey) (sh - pagedispy) in
237 if pagevw > 0 && pagevh > 0
238 then
239 { pageno = n
240 ; pagedimno = pdimno
241 ; pagew = w
242 ; pageh = h
243 ; pagex = pagex
244 ; pagey = pagey
245 ; pagevw = pagevw
246 ; pagevh = pagevh
247 ; pagedispx = pagedispx
248 ; pagedispy = pagedispy
249 ; pagecol = 0
250 } :: accu
251 else accu
252 else accu
254 fold accu (n+1)
256 if Array.length b = 0
257 then []
258 else List.rev (fold [] (page_of_y y))
261 let layoutS (columns, b) x y sw sh =
262 let rec fold accu n =
263 if n = Array.length b
264 then accu
265 else
266 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
267 if (vy - y) > sh
268 then accu
269 else
270 let accu =
271 if vy + pageh > y
272 then
273 let x = xoff + x in
274 let pagey = max 0 (y - vy) in
275 let pagedispy = if pagey > 0 then 0 else vy - y in
276 let pagedispx, pagex =
277 if px = 0
278 then (
279 if x < 0
280 then 0, -x
281 else x, 0
283 else (
284 let px = px - x in
285 if px < 0
286 then -px, 0
287 else 0, px
290 let pagecolw = pagew/columns in
291 let pagedispx =
292 if pagecolw < sw
293 then pagedispx + ((sw - pagecolw) / 2)
294 else pagedispx
296 let pagevw =
297 let vw = sw - pagedispx in
298 let pw = pagew - pagex in
299 min vw pw
301 let pagevw = min pagevw pagecolw in
302 let pagevh = min (pageh - pagey) (sh - pagedispy) in
303 if pagevw > 0 && pagevh > 0
304 then
305 { pageno = n/columns
306 ; pagedimno = pdimno
307 ; pagew = pagew
308 ; pageh = pageh
309 ; pagex = pagex
310 ; pagey = pagey
311 ; pagevw = pagevw
312 ; pagevh = pagevh
313 ; pagedispx = pagedispx
314 ; pagedispy = pagedispy
315 ; pagecol = n mod columns
316 } :: accu
317 else accu
318 else accu
320 fold accu (n+1)
322 List.rev (fold [] 0)
325 let layout x y sw sh =
326 if nogeomcmds state.geomcmds
327 then
328 match conf.columns with
329 | Csingle b -> layoutN ((1, 0, 0), b) x y sw sh
330 | Cmulti c -> layoutN c x y sw sh
331 | Csplit s -> layoutS s x y sw sh
332 else []
335 let maxy () = state.maxy - if conf.maxhfit then state.winh else 0;;
336 let clamp incr = bound (state.y + incr) 0 @@ maxy ();;
338 let itertiles l f =
339 let tilex = l.pagex mod conf.tilew in
340 let tiley = l.pagey mod conf.tileh in
342 let col = l.pagex / conf.tilew in
343 let row = l.pagey / conf.tileh in
345 let rec rowloop row y0 dispy h =
346 if h != 0
347 then
348 let dh = conf.tileh - y0 in
349 let dh = min h dh in
350 let rec colloop col x0 dispx w =
351 if w != 0
352 then
353 let dw = conf.tilew - x0 in
354 let dw = min w dw in
355 f col row dispx dispy x0 y0 dw dh;
356 colloop (col+1) 0 (dispx+dw) (w-dw)
358 colloop col tilex l.pagedispx l.pagevw;
359 rowloop (row+1) 0 (dispy+dh) (h-dh)
361 if l.pagevw > 0 && l.pagevh > 0
362 then rowloop row tiley l.pagedispy l.pagevh;
365 let gettileopaque l col row =
366 let key = l.pageno, state.gen, conf.colorspace,
367 conf.angle, l.pagew, l.pageh, col, row in
368 try Some (Hashtbl.find state.tilemap key)
369 with Not_found -> None
372 let puttileopaque l col row gen colorspace angle opaque size elapsed =
373 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
374 Hashtbl.add state.tilemap key (opaque, size, elapsed)
377 let drawtiles l color =
378 GlDraw.color color;
379 begintiles ();
380 let f col row x y tilex tiley w h =
381 match gettileopaque l col row with
382 | Some (opaque, _, t) ->
383 let params = x, y, w, h, tilex, tiley in
384 if conf.invert
385 then GlTex.env (`mode `blend);
386 drawtile params opaque;
387 if conf.invert
388 then GlTex.env (`mode `modulate);
389 if conf.debug
390 then (
391 endtiles ();
392 let s = Printf.sprintf "%d[%d,%d] %f sec" l.pageno col row t in
393 let w = measurestr fstate.fontsize s in
394 GlDraw.color (0.0, 0.0, 0.0);
395 filledrect
396 (float (x-2))
397 (float (y-2))
398 (float (x+2) +. w)
399 (float (y + fstate.fontsize + 2));
400 GlDraw.color color;
401 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
402 begintiles ();
405 | None ->
406 endtiles ();
407 let w = let lw = state.winw - x in min lw w
408 and h = let lh = state.winh - y in min lh h
410 if conf.invert
411 then GlTex.env (`mode `blend);
412 begin match !checkerstexid with
413 | Some id ->
414 Gl.enable `texture_2d;
415 GlTex.bind_texture ~target:`texture_2d id;
416 let x0 = float x
417 and y0 = float y
418 and x1 = float (x+w)
419 and y1 = float (y+h) in
421 let tw = float w /. 16.0
422 and th = float h /. 16.0 in
423 let tx0 = float tilex /. 16.0
424 and ty0 = float tiley /. 16.0 in
425 let tx1 = tx0 +. tw
426 and ty1 = ty0 +. th in
427 Raw.sets_float Glutils.vraw ~pos:0
428 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
429 Raw.sets_float Glutils.traw ~pos:0
430 [| tx0; ty0; tx0; ty1; tx1; ty0; tx1; ty1 |];
431 GlArray.vertex `two Glutils.vraw;
432 GlArray.tex_coord `two Glutils.traw;
433 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
434 Gl.disable `texture_2d;
436 | None ->
437 GlDraw.color (1.0, 1.0, 1.0);
438 filledrect (float x) (float y) (float (x+w)) (float (y+h));
439 end;
440 if conf.invert
441 then GlTex.env (`mode `modulate);
442 if w > 128 && h > fstate.fontsize + 10
443 then (
444 let c = if conf.invert then 1.0 else 0.0 in
445 GlDraw.color (c, c, c);
446 let c, r =
447 if conf.verbose
448 then (col*conf.tilew, row*conf.tileh)
449 else col, row
451 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
453 GlDraw.color color;
454 begintiles ();
456 itertiles l f;
457 endtiles ();
460 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
462 let tilevisible1 l x y =
463 let ax0 = l.pagex
464 and ax1 = l.pagex + l.pagevw
465 and ay0 = l.pagey
466 and ay1 = l.pagey + l.pagevh in
468 let bx0 = x
469 and by0 = y in
470 let bx1 = min (bx0 + conf.tilew) l.pagew
471 and by1 = min (by0 + conf.tileh) l.pageh in
473 let rx0 = max ax0 bx0
474 and ry0 = max ay0 by0
475 and rx1 = min ax1 bx1
476 and ry1 = min ay1 by1 in
478 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
479 nonemptyintersection
482 let tilevisible layout n x y =
483 let rec findpageinlayout m = function
484 | l :: rest when l.pageno = n ->
485 tilevisible1 l x y || (
486 match conf.columns with
487 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
488 | Csplit _ | Csingle _ | Cmulti _ -> false
490 | _ :: rest -> findpageinlayout 0 rest
491 | [] -> false
493 findpageinlayout 0 layout;
496 let tileready l x y =
497 tilevisible1 l x y &&
498 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
501 let tilepage n p layout =
502 let rec loop = function
503 | l :: rest ->
504 if l.pageno = n
505 then
506 let f col row _ _ _ _ _ _ =
507 if state.currently = Idle
508 then
509 match gettileopaque l col row with
510 | Some _ -> ()
511 | None ->
512 let x = col*conf.tilew
513 and y = row*conf.tileh in
514 let w =
515 let w = l.pagew - x in
516 min w conf.tilew
518 let h =
519 let h = l.pageh - y in
520 min h conf.tileh
522 let pbo =
523 if conf.usepbo
524 then getpbo w h conf.colorspace
525 else ~< "0"
527 wcmd "tile %s %d %d %d %d %s" (~> p) x y w h (~> pbo);
528 state.currently <-
529 Tiling (
530 l, p, conf.colorspace, conf.angle,
531 state.gen, col, row, conf.tilew, conf.tileh
534 itertiles l f;
535 else
536 loop rest
538 | [] -> ()
540 if nogeomcmds state.geomcmds
541 then loop layout;
544 let preloadlayout x y sw sh =
545 let y = if y < sh then 0 else y - sh in
546 let x = min 0 (x + sw) in
547 let h = sh*3 in
548 let w = sw*3 in
549 layout x y w h;
552 let load pages =
553 let rec loop pages =
554 if state.currently = Idle
555 then
556 match pages with
557 | l :: rest ->
558 begin match getopaque l.pageno with
559 | None ->
560 wcmd "page %d %d" l.pageno l.pagedimno;
561 state.currently <- Loading (l, state.gen);
562 | Some opaque ->
563 tilepage l.pageno opaque pages;
564 loop rest
565 end;
566 | _ -> ()
568 if nogeomcmds state.geomcmds
569 then loop pages
572 let preload pages =
573 load pages;
574 if conf.preload && state.currently = Idle
575 then load (preloadlayout state.x state.y state.winw state.winh);
578 let layoutready layout =
579 let rec fold all ls =
580 all && match ls with
581 | l :: rest ->
582 let seen = ref false in
583 let allvisible = ref true in
584 let foo col row _ _ _ _ _ _ =
585 seen := true;
586 allvisible := !allvisible &&
587 begin match gettileopaque l col row with
588 | Some _ -> true
589 | None -> false
592 itertiles l foo;
593 fold (!seen && !allvisible) rest
594 | [] -> true
596 let alltilesvisible = fold true layout in
597 alltilesvisible;
600 let gotoxy x y =
601 let y = bound y 0 state.maxy in
602 let y, layout =
603 let layout = layout x y state.winw state.winh in
604 postRedisplay "gotoxy ready";
605 y, layout
607 state.x <- x;
608 state.y <- y;
609 state.layout <- layout;
610 begin match state.mode with
611 | LinkNav ln ->
612 begin match ln with
613 | Ltexact (pageno, linkno) ->
614 let rec loop = function
615 | [] ->
616 state.lnava <- Some (pageno, linkno);
617 state.mode <- LinkNav (Ltgendir 0)
618 | l :: _ when l.pageno = pageno ->
619 begin match getopaque pageno with
620 | None -> state.mode <- LinkNav (Ltnotready (pageno, 0))
621 | Some opaque ->
622 let x0, y0, x1, y1 = getlinkrect opaque linkno in
623 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
624 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
625 then state.mode <- LinkNav (Ltgendir 0)
627 | _ :: rest -> loop rest
629 loop layout
630 | Ltnotready _ | Ltgendir _ -> ()
632 | Birdseye _ | Textentry _ | View -> ()
633 end;
634 begin match state.mode with
635 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
636 if not (pagevisible layout pageno)
637 then (
638 match state.layout with
639 | [] -> ()
640 | l :: _ ->
641 state.mode <- Birdseye (conf, leftx, l.pageno, hooverpageno, anchor)
643 | LinkNav lt ->
644 begin match lt with
645 | Ltnotready (_, dir)
646 | Ltgendir dir ->
647 let linknav =
648 let rec loop = function
649 | [] -> lt
650 | l :: rest ->
651 match getopaque l.pageno with
652 | None -> Ltnotready (l.pageno, dir)
653 | Some opaque ->
654 let link =
655 let ld =
656 if dir = 0
657 then LDfirstvisible (l.pagex, l.pagey, dir)
658 else if dir > 0 then LDfirst else LDlast
660 findlink opaque ld
662 match link with
663 | Lnotfound -> loop rest
664 | Lfound n ->
665 showlinktype (getlink opaque n);
666 Ltexact (l.pageno, n)
668 loop state.layout
670 state.mode <- LinkNav linknav
671 | Ltexact _ -> ()
673 | Textentry _ | View -> ()
674 end;
675 preload layout;
676 if conf.updatecurs
677 then (
678 let mx, my = state.mpos in
679 updateunder mx my;
683 let conttiling pageno opaque =
684 tilepage pageno opaque
685 (if conf.preload
686 then preloadlayout state.x state.y state.winw state.winh
687 else state.layout)
690 let gotoxy x y =
691 if not conf.verbose then state.text <- E.s;
692 gotoxy x y;
695 let getanchory (n, top, dtop) =
696 let y, h = getpageyh n in
697 if conf.presentation
698 then
699 let ips = calcips h in
700 y + truncate (top*.float h -. dtop*.float ips) + ips;
701 else y + truncate (top*.float h -. dtop*.float conf.interpagespace)
704 let gotoanchor anchor = gotoxy state.x (getanchory anchor);;
705 let addnav () = getanchor () |> cbput state.hists.nav;;
706 let addnavnorc () = getanchor () |> cbput_dont_update_rc state.hists.nav;;
708 let getnav dir =
709 let anchor = cbgetc state.hists.nav dir in
710 getanchory anchor;
713 let gotopage n top =
714 let y, h = getpageyh n in
715 let y = y + (truncate (top *. float h)) in
716 gotoxy state.x y
719 let gotopage1 n top =
720 let y = getpagey n in
721 let y = y + top in
722 gotoxy state.x y
725 let invalidate s f =
726 Glutils.redisplay := false;
727 state.layout <- [];
728 state.pdims <- [];
729 state.rects <- [];
730 state.rects1 <- [];
731 match state.geomcmds with
732 | ps, [] when emptystr ps ->
733 f ();
734 state.geomcmds <- s, [];
735 | ps, [] -> state.geomcmds <- ps, [s, f];
736 | ps, (s', _) :: rest when s' = s -> state.geomcmds <- ps, ((s, f) :: rest);
737 | ps, cmds -> state.geomcmds <- ps, ((s, f) :: cmds);
740 let flushpages () =
741 Hashtbl.iter (fun _ opaque -> wcmd "freepage %s" (~> opaque)) state.pagemap;
742 Hashtbl.clear state.pagemap;
745 let flushtiles () =
746 if not (Queue.is_empty state.tilelru)
747 then (
748 Queue.iter (fun (k, p, s) ->
749 wcmd "freetile %s" (~> p);
750 state.memused <- state.memused - s;
751 Hashtbl.remove state.tilemap k;
752 ) state.tilelru;
753 state.uioh#infochanged Memused;
754 Queue.clear state.tilelru;
756 load state.layout;
759 let stateh h =
760 let h = truncate (float h*.conf.zoom) in
761 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
762 h - d
765 let fillhelp () =
766 state.help <-
767 let sl = keystostrlist conf in
768 let rec loop accu =
769 function | [] -> accu
770 | s :: rest -> loop ((s, 0, Noaction) :: accu) rest
771 in Help.makehelp conf.urilauncher
772 @ (("", 0, Noaction) :: loop [] sl) |> Array.of_list
775 let opendoc path password =
776 state.path <- path;
777 state.password <- password;
778 state.gen <- state.gen + 1;
779 state.docinfo <- [];
780 state.outlines <- [||];
782 flushpages ();
783 setaalevel conf.aalevel;
784 let titlepath =
785 if emptystr state.origin
786 then path
787 else state.origin
789 Wsi.settitle ("llpp " ^ mbtoutf8 (Filename.basename titlepath));
790 wcmd "open %d %d %s\000%s\000%s\000"
791 (btod conf.usedoccss) !layouth
792 path password conf.css;
793 invalidate "reqlayout"
794 (fun () ->
795 wcmd "reqlayout %d %d %d %s\000"
796 conf.angle (FMTE.to_int conf.fitmodel)
797 (stateh state.winh) state.nameddest
799 fillhelp ();
802 let reload () =
803 state.anchor <- getanchor ();
804 opendoc state.path state.password;
807 let scalecolor c = let c = c *. conf.colorscale in (c, c, c);;
808 let scalecolor2 (r, g, b) =
809 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
812 let docolumns columns =
813 match columns with
814 | Csingle _ ->
815 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
816 let rec loop pageno pdimno pdim y ph pdims =
817 if pageno != state.pagecount
818 then
819 let pdimno, ((_, w, h, xoff) as pdim), pdims =
820 match pdims with
821 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
822 pdimno+1, pdim, rest
823 | _ ->
824 pdimno, pdim, pdims
826 let x = max 0 (((state.winw - w) / 2) - xoff) in
827 let y =
828 y + (if conf.presentation
829 then (if pageno = 0 then calcips h else calcips ph + calcips h)
830 else (if pageno = 0 then 0 else conf.interpagespace))
832 a.(pageno) <- (pdimno, x, y, pdim);
833 loop (pageno+1) pdimno pdim (y + h) h pdims
835 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
836 conf.columns <- Csingle a;
838 | Cmulti ((columns, coverA, coverB), _) ->
839 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
840 let rec loop pageno pdimno pdim x y rowh pdims =
841 let rec fixrow m =
842 if m = pageno then () else
843 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
844 if h < rowh
845 then (
846 let y = y + (rowh - h) / 2 in
847 a.(m) <- (pdimno, x, y, pdim);
849 fixrow (m+1)
851 if pageno = state.pagecount
852 then fixrow (((pageno - 1) / columns) * columns)
853 else
854 let pdimno, ((_, w, h, xoff) as pdim), pdims =
855 match pdims with
856 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
857 pdimno+1, pdim, rest
858 | _ -> pdimno, pdim, pdims
860 let x, y, rowh' =
861 if pageno = coverA - 1 || pageno = state.pagecount - coverB
862 then (
863 let x = (state.winw - w) / 2 in
864 let ips =
865 if conf.presentation then calcips h else conf.interpagespace in
866 x, y + ips + rowh, h
868 else (
869 if (pageno - coverA) mod columns = 0
870 then (
871 let x = max 0 (state.winw - state.w) / 2 in
872 let y =
873 if conf.presentation
874 then
875 let ips = calcips h in
876 y + (if pageno = 0 then 0 else calcips rowh + ips)
877 else
878 y + (if pageno = 0 then 0 else conf.interpagespace)
880 x, y + rowh, h
882 else x, y, max rowh h
885 let y =
886 if pageno > 1 && (pageno - coverA) mod columns = 0
887 then (
888 let y =
889 if pageno = columns && conf.presentation
890 then (
891 let ips = calcips rowh in
892 for i = 0 to pred columns
894 let (pdimno, x, y, pdim) = a.(i) in
895 a.(i) <- (pdimno, x, y+ips, pdim)
896 done;
897 y+ips;
899 else y
901 fixrow (pageno - columns);
904 else y
906 a.(pageno) <- (pdimno, x, y, pdim);
907 let x = x + w + xoff*2 + conf.interpagespace in
908 loop (pageno+1) pdimno pdim x y rowh' pdims
910 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
911 conf.columns <- Cmulti ((columns, coverA, coverB), a);
913 | Csplit (c, _) ->
914 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
915 let rec loop pageno pdimno pdim y pdims =
916 if pageno != state.pagecount
917 then
918 let pdimno, ((_, w, h, _) as pdim), pdims =
919 match pdims with
920 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
921 pdimno+1, pdim, rest
922 | _ -> pdimno, pdim, pdims
924 let cw = w / c in
925 let rec loop1 n x y =
926 if n = c then y else (
927 a.(pageno*c + n) <- (pdimno, x, y, pdim);
928 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
931 let y = loop1 0 0 y in
932 loop (pageno+1) pdimno pdim y pdims
934 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
935 conf.columns <- Csplit (c, a);
938 let represent () =
939 docolumns conf.columns;
940 state.maxy <- calcheight ();
941 if state.reprf == noreprf
942 then (
943 match state.mode with
944 | Birdseye (_, _, pageno, _, _) ->
945 let y, h = getpageyh pageno in
946 let top = (state.winh - h) / 2 in
947 gotoxy state.x (max 0 (y - top))
948 | Textentry _ | View | LinkNav _ ->
949 let y = getanchory state.anchor in
950 let y = min y (state.maxy - state.winh) in
951 gotoxy state.x y;
953 else (
954 state.reprf ();
955 state.reprf <- noreprf;
959 let reshape ?(firsttime=false) w h =
960 GlDraw.viewport ~x:0 ~y:0 ~w ~h;
961 if not firsttime && nogeomcmds state.geomcmds
962 then state.anchor <- getanchor ();
964 state.winw <- w;
965 let w = truncate (float w *. conf.zoom) in
966 let w = max w 2 in
967 state.winh <- h;
968 setfontsize fstate.fontsize;
969 GlMat.mode `modelview;
970 GlMat.load_identity ();
972 GlMat.mode `projection;
973 GlMat.load_identity ();
974 GlMat.rotate ~x:1.0 ~angle:180.0 ();
975 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
976 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
978 let relx =
979 if conf.zoom <= 1.0
980 then 0.0
981 else float state.x /. float state.w
983 invalidate "geometry"
984 (fun () ->
985 state.w <- w;
986 if not firsttime
987 then state.x <- truncate (relx *. float w);
988 let w =
989 match conf.columns with
990 | Csingle _ -> w
991 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
992 | Csplit (c, _) -> w * c
994 wcmd "geometry %d %d %d" w (stateh h) (FMTE.to_int conf.fitmodel)
998 let gctiles () =
999 let len = Queue.length state.tilelru in
1000 let layout = lazy (if conf.preload
1001 then preloadlayout state.x state.y state.winw state.winh
1002 else state.layout) in
1003 let rec loop qpos =
1004 if state.memused > conf.memlimit
1005 then (
1006 if qpos < len
1007 then
1008 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1009 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1010 let (_, pw, ph, _) = getpagedim n in
1011 if gen = state.gen
1012 && colorspace = conf.colorspace
1013 && angle = conf.angle
1014 && pagew = pw
1015 && pageh = ph
1016 && (
1017 let x = col*conf.tilew and y = row*conf.tileh in
1018 tilevisible (Lazy.force_val layout) n x y
1020 then Queue.push lruitem state.tilelru
1021 else (
1022 freepbo p;
1023 wcmd "freetile %s" (~> p);
1024 state.memused <- state.memused - s;
1025 state.uioh#infochanged Memused;
1026 Hashtbl.remove state.tilemap k;
1028 loop (qpos+1)
1031 loop 0
1034 let onpagerect pageno f =
1035 let b =
1036 match conf.columns with
1037 | Cmulti (_, b) -> b
1038 | Csingle b -> b
1039 | Csplit (_, b) -> b
1041 if pageno >= 0 && pageno < Array.length b
1042 then
1043 let (_, _, _, (_, w, h, _)) = b.(pageno) in
1044 f w h
1047 let gotopagexy1 pageno x y =
1048 let _,w1,h1,leftx = getpagedim pageno in
1049 let top = y /. (float h1) in
1050 let left = x /. (float w1) in
1051 let py, w, h = getpageywh pageno in
1052 let wh = state.winh in
1053 let x = left *. (float w) in
1054 let x = leftx + state.x + truncate x in
1055 let sx =
1056 if x < 0 || x >= state.winw
1057 then state.x - x
1058 else state.x
1060 let pdy = truncate (top *. float h) in
1061 let y' = py + pdy in
1062 let dy = y' - state.y in
1063 let sy =
1064 if x != state.x || not (dy > 0 && dy < wh)
1065 then (
1066 if conf.presentation
1067 then
1068 if abs (py - y') > wh
1069 then y'
1070 else py
1071 else y';
1073 else state.y
1075 if state.x != sx || state.y != sy
1076 then gotoxy sx sy
1077 else gotoxy state.x state.y;
1080 let gotopagexy pageno x y =
1081 match state.mode with
1082 | Birdseye _ -> gotopage pageno 0.0
1083 | Textentry _ | View | LinkNav _ -> gotopagexy1 pageno x y
1086 let getpassword () =
1087 let passcmd = getenvdef "LLPP_ASKPASS" conf.passcmd in
1088 if emptystr passcmd
1089 then E.s
1090 else getcmdoutput (fun s ->
1091 impmsg "error getting password: %s" s;
1092 dolog "%s" s) passcmd;
1095 let pgoto opaque pageno x y =
1096 let pdimno = getpdimno pageno in
1097 let x, y = project opaque pageno pdimno x y in
1098 gotopagexy pageno x y;
1101 let act cmds =
1102 (* dolog "%S" cmds; *)
1103 let spl = splitatchar cmds ' ' in
1104 let scan s fmt f =
1105 try Scanf.sscanf s fmt f
1106 with exn ->
1107 dolog "error processing '%S': %s" cmds @@ exntos exn;
1108 exit 1
1110 let addoutline outline =
1111 match state.currently with
1112 | Outlining outlines -> state.currently <- Outlining (outline :: outlines)
1113 | Idle -> state.currently <- Outlining [outline]
1114 | Loading _ | Tiling _ ->
1115 dolog "invalid outlining state";
1116 logcurrently state.currently
1118 match spl with
1119 | "clear", "" ->
1120 state.pdims <- [];
1121 state.uioh#infochanged Pdim;
1123 | "clearrects", "" ->
1124 state.rects <- state.rects1;
1125 postRedisplay "clearrects";
1127 | "continue", args ->
1128 let n = scan args "%u" (fun n -> n) in
1129 state.pagecount <- n;
1130 begin match state.currently with
1131 | Outlining l ->
1132 state.currently <- Idle;
1133 state.outlines <- Array.of_list (List.rev l)
1134 | Idle | Loading _ | Tiling _ -> ()
1135 end;
1137 let cur, cmds = state.geomcmds in
1138 if emptystr cur then error "empty geomcmd";
1140 begin match List.rev cmds with
1141 | [] ->
1142 state.geomcmds <- E.s, [];
1143 represent ();
1144 | (s, f) :: rest ->
1145 f ();
1146 state.geomcmds <- s, List.rev rest;
1147 end;
1148 postRedisplay "continue";
1150 | "msg", args ->
1151 showtext ' ' args
1153 | "vmsg", args ->
1154 if conf.verbose then showtext ' ' args
1156 | "emsg", args ->
1157 Buffer.add_string state.errmsgs args;
1158 state.newerrmsgs <- true;
1159 postRedisplay "error message"
1161 | "progress", args ->
1162 let progress, text =
1163 scan args "%f %n"
1164 (fun f pos -> f, String.sub args pos (String.length args - pos))
1166 state.text <- text;
1167 state.progress <- progress;
1168 postRedisplay "progress"
1170 | "firstmatch", args ->
1171 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1172 scan args "%u %d %f %f %f %f %f %f %f %f"
1173 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1174 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1176 let y = (getpagey pageno) + truncate y0 in
1177 let x =
1178 if (state.x < - truncate x0) || (state.x > state.winw - truncate x1)
1179 then state.winw/2 - truncate (x0 /. 2. +. x1 /. 2.)
1180 else state.x
1182 addnav ();
1183 gotoxy x y;
1184 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1185 state.rects1 <- [pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)]
1187 | "match", args ->
1188 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1189 scan args "%u %d %f %f %f %f %f %f %f %f"
1190 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1191 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1193 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1194 state.rects1 <-
1195 (pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1197 | "page", args ->
1198 let pageopaques, t = scan args "%s %f" (fun p t -> p, t) in
1199 let pageopaque = ~< pageopaques in
1200 begin match state.currently with
1201 | Loading (l, gen) ->
1202 vlog "page %d took %f sec" l.pageno t;
1203 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1204 let preloadedpages =
1205 if conf.preload
1206 then preloadlayout state.x state.y state.winw state.winh
1207 else state.layout
1209 let evict () =
1210 let set = List.fold_left (fun s l -> IntSet.add l.pageno s)
1211 IntSet.empty preloadedpages
1213 let evictedpages =
1214 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1215 if not (IntSet.mem pageno set)
1216 then (
1217 wcmd "freepage %s" (~> opaque);
1218 key :: accu
1220 else accu
1221 ) state.pagemap []
1223 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1225 evict ();
1226 state.currently <- Idle;
1227 if gen = state.gen
1228 then (
1229 tilepage l.pageno pageopaque state.layout;
1230 load state.layout;
1231 load preloadedpages;
1232 let visible = pagevisible state.layout l.pageno in
1233 if visible
1234 then (
1235 match state.mode with
1236 | LinkNav (Ltnotready (pageno, dir)) ->
1237 if pageno = l.pageno
1238 then (
1239 let link =
1240 let ld =
1241 if dir = 0
1242 then LDfirstvisible (l.pagex, l.pagey, dir)
1243 else if dir > 0 then LDfirst else LDlast
1245 findlink pageopaque ld
1247 match link with
1248 | Lnotfound -> ()
1249 | Lfound n ->
1250 showlinktype (getlink pageopaque n);
1251 state.mode <- LinkNav (Ltexact (l.pageno, n))
1253 | LinkNav (Ltgendir _)
1254 | LinkNav (Ltexact _)
1255 | View
1256 | Birdseye _
1257 | Textentry _ -> ()
1260 if visible && layoutready state.layout
1261 then postRedisplay "page";
1264 | Idle | Tiling _ | Outlining _ ->
1265 dolog "Inconsistent loading state";
1266 logcurrently state.currently;
1267 exit 1
1270 | "tile" , args ->
1271 let (x, y, opaques, size, t) =
1272 scan args "%u %u %s %u %f" (fun x y p size t -> (x, y, p, size, t))
1274 let opaque = ~< opaques in
1275 begin match state.currently with
1276 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1277 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1279 unmappbo opaque;
1280 if tilew != conf.tilew || tileh != conf.tileh
1281 then (
1282 wcmd "freetile %s" (~> opaque);
1283 state.currently <- Idle;
1284 load state.layout;
1286 else (
1287 puttileopaque l col row gen cs angle opaque size t;
1288 state.memused <- state.memused + size;
1289 state.uioh#infochanged Memused;
1290 gctiles ();
1291 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1292 opaque, size) state.tilelru;
1294 state.currently <- Idle;
1295 if gen = state.gen
1296 && conf.colorspace = cs
1297 && conf.angle = angle
1298 && tilevisible state.layout l.pageno x y
1299 then conttiling l.pageno pageopaque;
1301 preload state.layout;
1302 if gen = state.gen
1303 && conf.colorspace = cs
1304 && conf.angle = angle
1305 && tilevisible state.layout l.pageno x y
1306 && layoutready state.layout
1307 then postRedisplay "tile nothrottle";
1310 | Idle | Loading _ | Outlining _ ->
1311 dolog "Inconsistent tiling state";
1312 logcurrently state.currently;
1313 exit 1
1316 | "pdim", args ->
1317 let (n, w, h, _) as pdim =
1318 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1320 let pdim =
1321 match conf.fitmodel with
1322 | FitWidth -> pdim
1323 | FitPage | FitProportional ->
1324 match conf.columns with
1325 | Csplit _ -> (n, w, h, 0)
1326 | Csingle _ | Cmulti _ -> pdim
1328 state.pdims <- pdim :: state.pdims;
1329 state.uioh#infochanged Pdim
1331 | "o", args ->
1332 let (l, n, t, h, pos) =
1333 scan args "%u %u %d %u %n" (fun l n t h pos -> l, n, t, h, pos)
1335 let s = String.sub args pos (String.length args - pos) in
1336 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
1338 | "ou", args ->
1339 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
1340 let s = String.sub args pos len in
1341 let pos2 = pos + len + 1 in
1342 let uri = String.sub args pos2 (String.length args - pos2) in
1343 addoutline (s, l, Ouri uri)
1345 | "on", args ->
1346 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
1347 let s = String.sub args pos (String.length args - pos) in
1348 addoutline (s, l, Onone)
1350 | "a", args ->
1351 let (n, l, t) = scan args "%u %d %d" (fun n l t -> n, l, t) in
1352 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
1354 | "info", args ->
1355 let c, v = splitatchar args '\t' in
1356 let s =
1357 if nonemptystr v
1358 then
1359 if c = "Title"
1360 then (
1361 conf.title <- v;
1362 if not !ignoredoctitlte then Wsi.settitle v;
1363 args
1365 else
1366 if let len = String.length c in
1367 len > 6 && ((String.sub c (len-4) 4) = "date")
1368 then (
1369 if String.length v >= 7 && v.[0] = 'D' && v.[1] = ':'
1370 then
1371 let b = Buffer.create 10 in
1372 Printf.bprintf b "%s\t" c;
1373 let sub p l c =
1375 Buffer.add_substring b v p l;
1376 Buffer.add_char b c;
1377 with exn -> Buffer.add_string b @@ exntos exn
1379 sub 2 4 '/';
1380 sub 6 2 '/';
1381 sub 8 2 ' ';
1382 sub 10 2 ':';
1383 sub 12 2 ':';
1384 sub 14 2 ' ';
1385 Buffer.add_char b '[';
1386 Buffer.add_string b v;
1387 Buffer.add_char b ']';
1388 Buffer.contents b
1389 else args
1391 else args
1392 else args
1394 state.docinfo <- (1, s) :: state.docinfo
1396 | "infoend", "" ->
1397 state.docinfo <- List.rev state.docinfo;
1398 state.uioh#infochanged Docinfo
1400 | "pass", args ->
1401 if args = "fail"
1402 then Wsi.settitle "Wrong password";
1403 let password = getpassword () in
1404 if emptystr password
1405 then error "document is password protected"
1406 else opendoc state.path password
1408 | _ -> error "unknown cmd `%S'" cmds
1411 let onhist cb =
1412 let rc = cb.rc in
1413 let action = function
1414 | HCprev -> cbget cb ~-1
1415 | HCnext -> cbget cb 1
1416 | HCfirst -> cbget cb ~-(cb.rc)
1417 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1418 and cancel () = cb.rc <- rc
1419 in (action, cancel)
1422 let search pattern forward =
1423 match conf.columns with
1424 | Csplit _ -> impmsg "searching does not work properly in split columns mode"
1425 | Csingle _ | Cmulti _ ->
1426 if nonemptystr pattern
1427 then
1428 let pn, py =
1429 match state.layout with
1430 | [] -> 0, 0
1431 | l :: _ -> l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1433 wcmd "search %d %d %d %d,%s\000"
1434 (btod conf.icase) pn py (btod forward) pattern;
1437 let intentry text key =
1438 let text =
1439 if emptystr text && key = Keys.Ascii '-'
1440 then addchar text '-'
1441 else
1442 match [@warning "-4"] key with
1443 | Keys.Ascii ('0'..'9' as c) -> addchar text c
1444 | _ ->
1445 state.text <- "invalid key";
1446 text
1448 TEcont text
1451 let linknact f s =
1452 if nonemptystr s
1453 then
1454 let n =
1455 let l = String.length s in
1456 let rec loop pos n =
1457 if pos = l
1458 then n
1459 else
1460 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
1461 loop (pos+1) (n*26 + m)
1462 in loop 0 0
1464 let rec loop n = function
1465 | [] -> ()
1466 | l :: rest ->
1467 match getopaque l.pageno with
1468 | None -> loop n rest
1469 | Some opaque ->
1470 let m = getlinkcount opaque in
1471 if n < m
1472 then
1473 let under = getlink opaque n in
1474 f under
1475 else loop (n-m) rest
1477 loop n state.layout;
1480 let linknentry text key = match [@warning "-4"] key with
1481 | Keys.Ascii c ->
1482 let text = addchar text c in
1483 linknact (fun under -> state.text <- undertext under) text;
1484 TEcont text
1485 | _ ->
1486 state.text <- Printf.sprintf "invalid key";
1487 TEcont text
1490 let textentry text key = match [@warning "-4"] key with
1491 | Keys.Ascii c -> TEcont (addchar text c)
1492 | Keys.Code c -> TEcont (text ^ toutf8 c)
1493 | _ -> TEcont text
1496 let reqlayout angle fitmodel =
1497 if nogeomcmds state.geomcmds
1498 then state.anchor <- getanchor ();
1499 conf.angle <- angle mod 360;
1500 if conf.angle != 0
1501 then (
1502 match state.mode with
1503 | LinkNav _ -> state.mode <- View
1504 | Birdseye _ | Textentry _ | View -> ()
1506 conf.fitmodel <- fitmodel;
1507 invalidate "reqlayout"
1508 (fun () -> wcmd "reqlayout %d %d %d"
1509 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh));
1512 let settrim trimmargins trimfuzz =
1513 if nogeomcmds state.geomcmds
1514 then state.anchor <- getanchor ();
1515 conf.trimmargins <- trimmargins;
1516 conf.trimfuzz <- trimfuzz;
1517 let x0, y0, x1, y1 = trimfuzz in
1518 invalidate "settrim"
1519 (fun () -> wcmd "settrim %d %d %d %d %d"
1520 (btod conf.trimmargins) x0 y0 x1 y1);
1521 flushpages ();
1524 let setzoom zoom =
1525 let zoom = max 0.0001 zoom in
1526 if zoom <> conf.zoom
1527 then (
1528 state.prevzoom <- (conf.zoom, state.x);
1529 conf.zoom <- zoom;
1530 reshape state.winw state.winh;
1531 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
1535 let pivotzoom ?(vw=min state.w state.winw)
1536 ?(vh=min (state.maxy-state.y) state.winh)
1537 ?(x=vw/2) ?(y=vh/2) zoom =
1538 let w = float state.w /. zoom in
1539 let hw = w /. 2.0 in
1540 let ratio = float vh /. float vw in
1541 let hh = hw *. ratio in
1542 let x0 = float x -. hw
1543 and y0 = float y -. hh in
1544 gotoxy (state.x - truncate x0) (state.y + truncate y0);
1545 setzoom zoom;
1548 let pivotzoom ?vw ?vh ?x ?y zoom =
1549 if nogeomcmds state.geomcmds
1550 then
1551 if zoom > 1.0
1552 then pivotzoom ?vw ?vh ?x ?y zoom
1553 else setzoom zoom
1556 let setcolumns mode columns coverA coverB =
1557 state.prevcolumns <- Some (conf.columns, conf.zoom);
1558 if columns < 0
1559 then (
1560 if isbirdseye mode
1561 then impmsg "split mode doesn't work in bird's eye"
1562 else (
1563 conf.columns <- Csplit (-columns, E.a);
1564 state.x <- 0;
1565 conf.zoom <- 1.0;
1568 else (
1569 if columns < 2
1570 then (
1571 conf.columns <- Csingle E.a;
1572 state.x <- 0;
1573 setzoom 1.0;
1575 else (
1576 conf.columns <- Cmulti ((columns, coverA, coverB), E.a);
1577 conf.zoom <- 1.0;
1580 reshape state.winw state.winh;
1583 let resetmstate () =
1584 state.mstate <- Mnone;
1585 Wsi.setcursor Wsi.CURSOR_INHERIT;
1588 let enterbirdseye () =
1589 let zoom = float conf.thumbw /. float state.winw in
1590 let birdseyepageno =
1591 let cy = state.winh / 2 in
1592 let fold = function
1593 | [] -> 0
1594 | l :: rest ->
1595 let rec fold best = function
1596 | [] -> best.pageno
1597 | l :: rest ->
1598 let d = cy - (l.pagedispy + l.pagevh/2)
1599 and dbest = cy - (best.pagedispy + best.pagevh/2) in
1600 if abs d < abs dbest
1601 then fold l rest
1602 else best.pageno
1603 in fold l rest
1605 fold state.layout
1607 state.mode <-
1608 Birdseye (
1609 { conf with zoom = conf.zoom },
1610 state.x, birdseyepageno, -1, getanchor ()
1612 resetmstate ();
1613 conf.zoom <- zoom;
1614 conf.presentation <- false;
1615 conf.interpagespace <- 10;
1616 conf.hlinks <- false;
1617 conf.fitmodel <- FitPage;
1618 state.x <- 0;
1619 conf.columns <- (
1620 match conf.beyecolumns with
1621 | Some c ->
1622 conf.zoom <- 1.0;
1623 Cmulti ((c, 0, 0), E.a)
1624 | None -> Csingle E.a
1626 if conf.verbose
1627 then state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
1628 (100.0*.zoom)
1629 else state.text <- E.s;
1630 reshape state.winw state.winh;
1633 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
1634 state.mode <- View;
1635 conf.zoom <- c.zoom;
1636 conf.presentation <- c.presentation;
1637 conf.interpagespace <- c.interpagespace;
1638 conf.hlinks <- c.hlinks;
1639 conf.fitmodel <- c.fitmodel;
1640 conf.beyecolumns <- (
1641 match conf.columns with
1642 | Cmulti ((c, _, _), _) -> Some c
1643 | Csingle _ -> None
1644 | Csplit _ -> error "leaving bird's eye split mode"
1646 conf.columns <- (
1647 match c.columns with
1648 | Cmulti (c, _) -> Cmulti (c, E.a)
1649 | Csingle _ -> Csingle E.a
1650 | Csplit (c, _) -> Csplit (c, E.a)
1652 if conf.verbose
1653 then state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
1654 (100.0*.conf.zoom);
1655 reshape state.winw state.winh;
1656 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
1657 state.x <- leftx;
1660 let togglebirdseye () =
1661 match state.mode with
1662 | Birdseye vals -> leavebirdseye vals true
1663 | View -> enterbirdseye ()
1664 | Textentry _ | LinkNav _ -> ()
1667 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
1668 let pageno = max 0 (pageno - incr) in
1669 let rec loop = function
1670 | [] -> gotopage1 pageno 0
1671 | l :: _ when l.pageno = pageno ->
1672 if l.pagedispy >= 0 && l.pagey = 0
1673 then postRedisplay "upbirdseye"
1674 else gotopage1 pageno 0
1675 | _ :: rest -> loop rest
1677 loop state.layout;
1678 state.text <- E.s;
1679 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
1682 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
1683 let pageno = min (state.pagecount - 1) (pageno + incr) in
1684 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
1685 let rec loop = function
1686 | [] ->
1687 let y, h = getpageyh pageno in
1688 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
1689 gotoxy state.x (clamp dy)
1690 | l :: _ when l.pageno = pageno ->
1691 if l.pagevh != l.pageh
1692 then gotoxy state.x (clamp (l.pageh - l.pagevh + conf.interpagespace))
1693 else postRedisplay "downbirdseye"
1694 | _ :: rest -> loop rest
1696 loop state.layout;
1697 state.text <- E.s;
1700 let optentry mode _ key =
1701 let btos b = if b then "on" else "off" in
1702 match [@warning "-4"] key with
1703 | Keys.Ascii 'C' ->
1704 let ondone s =
1706 let n, a, b = multicolumns_of_string s in
1707 setcolumns mode n a b;
1708 with exn ->
1709 state.text <- Printf.sprintf "bad columns `%s': %s" s @@ exntos exn
1711 TEswitch ("columns: ", E.s, None, textentry, ondone, true)
1713 | Keys.Ascii 'Z' ->
1714 let ondone s =
1716 let zoom = float (int_of_string s) /. 100.0 in
1717 pivotzoom zoom
1718 with exn ->
1719 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
1721 TEswitch ("zoom: ", E.s, None, intentry, ondone, true)
1723 | Keys.Ascii 'i' ->
1724 conf.icase <- not conf.icase;
1725 TEdone ("case insensitive search " ^ (btos conf.icase))
1727 | Keys.Ascii 'v' ->
1728 conf.verbose <- not conf.verbose;
1729 TEdone ("verbose " ^ (btos conf.verbose))
1731 | Keys.Ascii 'd' ->
1732 conf.debug <- not conf.debug;
1733 TEdone ("debug " ^ (btos conf.debug))
1735 | Keys.Ascii 'f' ->
1736 conf.underinfo <- not conf.underinfo;
1737 TEdone ("underinfo " ^ btos conf.underinfo)
1739 | Keys.Ascii 'T' ->
1740 settrim (not conf.trimmargins) conf.trimfuzz;
1741 TEdone ("trim margins " ^ btos conf.trimmargins)
1743 | Keys.Ascii 'I' ->
1744 conf.invert <- not conf.invert;
1745 TEdone ("invert colors " ^ btos conf.invert)
1747 | Keys.Ascii 'x' ->
1748 let ondone s =
1749 cbput state.hists.sel s;
1750 conf.selcmd <- s;
1752 TEswitch ("selection command: ", E.s, Some (onhist state.hists.sel),
1753 textentry, ondone, true)
1755 | Keys.Ascii 'M' ->
1756 if conf.pax == None
1757 then conf.pax <- Some 0.0
1758 else conf.pax <- None;
1759 TEdone ("PAX " ^ btos (conf.pax != None))
1761 | (Keys.Ascii c) ->
1762 state.text <- Printf.sprintf "bad option %d `%c'" (Char.code c) c;
1763 TEstop
1765 | _ -> TEcont state.text
1768 let adderrmsg src msg =
1769 Buffer.add_string state.errmsgs msg;
1770 state.newerrmsgs <- true;
1771 postRedisplay src
1774 let adderrfmt src fmt = Format.ksprintf (fun s -> adderrmsg src s) fmt;;
1776 class outlinelistview ~zebra ~source =
1777 let settext autonarrow s =
1778 if autonarrow
1779 then
1780 let ss = source#statestr in
1781 state.text <- if emptystr ss
1782 then "[" ^ s ^ "]"
1783 else "{" ^ ss ^ "} [" ^ s ^ "]"
1784 else state.text <- s
1786 object (self)
1787 inherit listview
1788 ~zebra
1789 ~helpmode:false
1790 ~source:(source :> lvsource)
1791 ~trusted:false
1792 ~modehash:(findkeyhash conf "outline")
1793 as super
1795 val m_autonarrow = false
1797 method! key key mask =
1798 let maxrows =
1799 if emptystr state.text
1800 then fstate.maxrows
1801 else fstate.maxrows - 2
1803 let calcfirst first active =
1804 if active > first
1805 then
1806 let rows = active - first in
1807 if rows > maxrows then active - maxrows else first
1808 else active
1810 let navigate incr =
1811 let active = m_active + incr in
1812 let active = bound active 0 (source#getitemcount - 1) in
1813 let first = calcfirst m_first active in
1814 postRedisplay "outline navigate";
1815 coe {< m_active = active; m_first = first >}
1817 let navscroll first =
1818 let active =
1819 let dist = m_active - first in
1820 if dist < 0
1821 then first
1822 else (
1823 if dist < maxrows
1824 then m_active
1825 else first + maxrows
1828 postRedisplay "outline navscroll";
1829 coe {< m_first = first; m_active = active >}
1831 let ctrl = Wsi.withctrl mask in
1832 let open Keys in
1833 match Wsi.kc2kt key with
1834 | Ascii 'a' when ctrl ->
1835 let text =
1836 if m_autonarrow
1837 then (
1838 source#denarrow;
1841 else (
1842 let pattern = source#renarrow in
1843 if nonemptystr m_qsearch
1844 then (source#narrow m_qsearch; m_qsearch)
1845 else pattern
1848 settext (not m_autonarrow) text;
1849 postRedisplay "toggle auto narrowing";
1850 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
1852 | Ascii '/' when emptystr m_qsearch && not m_autonarrow ->
1853 settext true E.s;
1854 postRedisplay "toggle auto narrowing";
1855 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
1857 | Ascii 'n' when ctrl ->
1858 source#narrow m_qsearch;
1859 if not m_autonarrow
1860 then source#add_narrow_pattern m_qsearch;
1861 postRedisplay "outline ctrl-n";
1862 coe {< m_first = 0; m_active = 0 >}
1864 | Ascii 'S' when ctrl ->
1865 let active = source#calcactive (getanchor ()) in
1866 let first = firstof m_first active in
1867 postRedisplay "outline ctrl-s";
1868 coe {< m_first = first; m_active = active >}
1870 | Ascii 'u' when ctrl ->
1871 postRedisplay "outline ctrl-u";
1872 if m_autonarrow && nonemptystr m_qsearch
1873 then (
1874 ignore (source#renarrow);
1875 settext m_autonarrow E.s;
1876 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
1878 else (
1879 source#del_narrow_pattern;
1880 let pattern = source#renarrow in
1881 let text =
1882 if emptystr pattern then E.s else "Narrowed to " ^ pattern
1884 settext m_autonarrow text;
1885 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
1888 | Ascii 'l' when ctrl ->
1889 let first = max 0 (m_active - (fstate.maxrows / 2)) in
1890 postRedisplay "outline ctrl-l";
1891 coe {< m_first = first >}
1893 | Ascii '\t' when m_autonarrow ->
1894 if nonemptystr m_qsearch
1895 then (
1896 postRedisplay "outline list view tab";
1897 source#add_narrow_pattern m_qsearch;
1898 settext true E.s;
1899 coe {< m_qsearch = E.s >}
1901 else coe self
1903 | Escape when m_autonarrow ->
1904 if nonemptystr m_qsearch
1905 then source#add_narrow_pattern m_qsearch;
1906 super#key key mask
1908 | Enter when m_autonarrow ->
1909 if nonemptystr m_qsearch
1910 then source#add_narrow_pattern m_qsearch;
1911 super#key key mask
1913 | (Ascii _ | Code _) when m_autonarrow ->
1914 let pattern = m_qsearch ^ toutf8 key in
1915 postRedisplay "outlinelistview autonarrow add";
1916 source#narrow pattern;
1917 settext true pattern;
1918 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
1920 | Backspace when m_autonarrow ->
1921 if emptystr m_qsearch
1922 then coe self
1923 else
1924 let pattern = withoutlastutf8 m_qsearch in
1925 postRedisplay "outlinelistview autonarrow backspace";
1926 ignore (source#renarrow);
1927 source#narrow pattern;
1928 settext true pattern;
1929 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
1931 | Up when ctrl -> navscroll (max 0 (m_first - 1))
1933 | Down when ctrl ->
1934 navscroll (min (source#getitemcount - 1) (m_first + 1))
1936 | Up -> navigate ~-1
1937 | Down -> navigate 1
1938 | Prior -> navigate ~-(fstate.maxrows)
1939 | Next -> navigate fstate.maxrows
1941 | Right ->
1942 let o =
1943 if ctrl
1944 then (
1945 postRedisplay "outline ctrl right";
1946 {< m_pan = m_pan + 1 >}
1948 else self#updownlevel 1
1950 coe o
1952 | Left ->
1953 let o =
1954 if ctrl
1955 then (
1956 postRedisplay "outline ctrl left";
1957 {< m_pan = m_pan - 1 >}
1959 else self#updownlevel ~-1
1961 coe o
1963 | Home ->
1964 postRedisplay "outline home";
1965 coe {< m_first = 0; m_active = 0 >}
1967 | End ->
1968 let active = source#getitemcount - 1 in
1969 let first = max 0 (active - fstate.maxrows) in
1970 postRedisplay "outline end";
1971 coe {< m_active = active; m_first = first >}
1973 | Delete|Escape|Insert|Enter|Ascii _|Code _|Ctrl _|Backspace|Fn _ ->
1974 super#key key mask
1975 end;;
1977 let genhistoutlines () =
1978 Config.gethist ()
1979 |> List.sort (fun (_, c1, _, _, _, _) (_, c2, _, _, _, _) ->
1980 compare c2.lastvisit c1.lastvisit)
1981 |> List.map (fun ((path, c, _, _, _, origin) as hist) ->
1982 let path = if nonemptystr origin then origin else path in
1983 let base = mbtoutf8 @@ Filename.basename path in
1984 (base ^ "\000" ^ c.title, 1, Ohistory hist)
1988 let gotohist (path, c, bookmarks, x, anchor, origin) =
1989 Config.save leavebirdseye;
1990 state.anchor <- anchor;
1991 state.bookmarks <- bookmarks;
1992 state.origin <- origin;
1993 state.x <- x;
1994 setconf conf c;
1995 let x0, y0, x1, y1 = conf.trimfuzz in
1996 wcmd "trimset %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1;
1997 reshape ~firsttime:true state.winw state.winh;
1998 opendoc path origin;
1999 setzoom c.zoom;
2002 let setcheckers enabled =
2003 match !checkerstexid with
2004 | None -> if enabled then checkerstexid := Some (makecheckers ())
2005 | Some id ->
2006 if not enabled
2007 then (
2008 GlTex.delete_texture id;
2009 checkerstexid := None;
2013 let describe_layout layout =
2014 let d =
2015 match layout with
2016 | [] -> "Page 0"
2017 | l :: [] -> Printf.sprintf "Page %d" (l.pageno+1)
2018 | l :: rest ->
2019 let rangestr a b =
2020 if a.pageno = b.pageno then Printf.sprintf "%d" (a.pageno+1)
2021 else Printf.sprintf "%d%s%d" (a.pageno+1)
2022 (if a.pageno+1 = b.pageno then ", " else Utf8syms.ellipsis)
2023 (b.pageno+1)
2025 let rec fold s la lb = function
2026 | [] -> Printf.sprintf "%s %s" s (rangestr la lb)
2027 | l :: rest when l.pageno = succ lb.pageno -> fold s la l rest
2028 | l :: rest -> fold (s ^ " " ^ rangestr la lb ^ ",") l l rest
2030 fold "Pages" l l rest
2032 let percent =
2033 let maxy = maxy () in
2034 if maxy <= 0
2035 then 100.
2036 else 100. *. (float state.y /. float maxy)
2038 Printf.sprintf "%s of %d [%.2f%%]" d state.pagecount percent
2041 let setpresentationmode v =
2042 let n = page_of_y state.y in
2043 state.anchor <- (n, 0.0, 1.0);
2044 conf.presentation <- v;
2045 if conf.fitmodel = FitPage
2046 then reqlayout conf.angle conf.fitmodel;
2047 represent ();
2050 let enterinfomode =
2051 let btos b = if b then Utf8syms.radical else E.s in
2052 let showextended = ref false in
2053 let showcolors = ref false in
2054 let leave mode _ = state.mode <- mode in
2055 let src =
2056 (object
2057 val mutable m_l = []
2058 val mutable m_a = E.a
2059 val mutable m_prev_uioh = nouioh
2060 val mutable m_prev_mode = View
2062 inherit lvsourcebase
2064 method reset prev_mode prev_uioh =
2065 m_a <- Array.of_list (List.rev m_l);
2066 m_l <- [];
2067 m_prev_mode <- prev_mode;
2068 m_prev_uioh <- prev_uioh;
2070 method int name get set =
2071 m_l <-
2072 (name, `int get, 1,
2073 Action (
2074 fun u ->
2075 let ondone s =
2076 try set (int_of_string s)
2077 with exn ->
2078 state.text <- Printf.sprintf "bad integer `%s': %s"
2079 s @@ exntos exn
2081 state.text <- E.s;
2082 let te = name ^ ": ", E.s, None, intentry, ondone, true in
2083 state.mode <- Textentry (te, leave m_prev_mode);
2085 )) :: m_l
2087 method int_with_suffix name get set =
2088 m_l <-
2089 (name, `intws get, 1,
2090 Action (
2091 fun u ->
2092 let ondone s =
2093 try set (int_of_string_with_suffix s)
2094 with exn ->
2095 state.text <- Printf.sprintf "bad integer `%s': %s"
2096 s @@ exntos exn
2098 state.text <- E.s;
2099 let te =
2100 name ^ ": ", E.s, None, intentry_with_suffix, ondone, true
2102 state.mode <- Textentry (te, leave m_prev_mode);
2104 )) :: m_l
2106 method bool ?(offset=1) ?(btos=btos) name get set =
2107 m_l <-
2108 (name, `bool (btos, get), offset, Action (
2109 fun u ->
2110 let v = get () in
2111 set (not v);
2113 )) :: m_l
2115 method color name get set =
2116 m_l <-
2117 (name, `color get, 1,
2118 Action (
2119 fun u ->
2120 let invalid = (nan, nan, nan) in
2121 let ondone s =
2122 let c =
2123 try color_of_string s
2124 with exn ->
2125 state.text <- Printf.sprintf "bad color `%s': %s"
2126 s @@ exntos exn;
2127 invalid
2129 if c <> invalid
2130 then set c;
2132 let te = name ^ ": ", E.s, None, textentry, ondone, true in
2133 state.text <- color_to_string (get ());
2134 state.mode <- Textentry (te, leave m_prev_mode);
2136 )) :: m_l
2138 method string name get set =
2139 m_l <-
2140 (name, `string get, 1,
2141 Action (
2142 fun u ->
2143 let ondone s = set s in
2144 let te = name ^ ": ", E.s, None, textentry, ondone, true in
2145 state.mode <- Textentry (te, leave m_prev_mode);
2147 )) :: m_l
2149 method colorspace name get set =
2150 m_l <-
2151 (name, `string get, 1,
2152 Action (
2153 fun _ ->
2154 let source =
2155 (object
2156 inherit lvsourcebase
2158 initializer
2159 m_active <- CSTE.to_int conf.colorspace;
2160 m_first <- 0;
2162 method getitemcount =
2163 Array.length CSTE.names
2164 method getitem n =
2165 (CSTE.names.(n), 0)
2166 method exit ~uioh ~cancel ~active ~first ~pan =
2167 ignore (uioh, first, pan);
2168 if not cancel then set active;
2169 None
2170 method hasaction _ = true
2171 end)
2173 state.text <- E.s;
2174 let modehash = findkeyhash conf "info" in
2175 coe (new listview ~zebra:false ~helpmode:false
2176 ~source ~trusted:true ~modehash)
2177 )) :: m_l
2179 method paxmark name get set =
2180 m_l <-
2181 (name, `string get, 1,
2182 Action (
2183 fun _ ->
2184 let source =
2185 (object
2186 inherit lvsourcebase
2188 initializer
2189 m_active <- MTE.to_int conf.paxmark;
2190 m_first <- 0;
2192 method getitemcount = Array.length MTE.names
2193 method getitem n = (MTE.names.(n), 0)
2194 method exit ~uioh ~cancel ~active ~first ~pan =
2195 ignore (uioh, first, pan);
2196 if not cancel then set active;
2197 None
2198 method hasaction _ = true
2199 end)
2201 state.text <- E.s;
2202 let modehash = findkeyhash conf "info" in
2203 coe (new listview ~zebra:false ~helpmode:false
2204 ~source ~trusted:true ~modehash)
2205 )) :: m_l
2207 method fitmodel name get set =
2208 m_l <-
2209 (name, `string get, 1,
2210 Action (
2211 fun _ ->
2212 let source =
2213 (object
2214 inherit lvsourcebase
2216 initializer
2217 m_active <- FMTE.to_int conf.fitmodel;
2218 m_first <- 0;
2220 method getitemcount = Array.length FMTE.names
2221 method getitem n = (FMTE.names.(n), 0)
2222 method exit ~uioh ~cancel ~active ~first ~pan =
2223 ignore (uioh, first, pan);
2224 if not cancel then set active;
2225 None
2226 method hasaction _ = true
2227 end)
2229 state.text <- E.s;
2230 let modehash = findkeyhash conf "info" in
2231 coe (new listview ~zebra:false ~helpmode:false
2232 ~source ~trusted:true ~modehash)
2233 )) :: m_l
2235 method caption s offset =
2236 m_l <- (s, `empty, offset, Noaction) :: m_l
2238 method caption2 s f offset =
2239 m_l <- (s, `string f, offset, Noaction) :: m_l
2241 method getitemcount = Array.length m_a
2243 method getitem n =
2244 let tostr = function
2245 | `int f -> string_of_int (f ())
2246 | `intws f -> string_with_suffix_of_int (f ())
2247 | `string f -> f ()
2248 | `color f -> color_to_string (f ())
2249 | `bool (btos, f) -> btos (f ())
2250 | `empty -> E.s
2252 let name, t, offset, _ = m_a.(n) in
2253 ((let s = tostr t in
2254 if nonemptystr s
2255 then Printf.sprintf "%s\t%s" name s
2256 else name),
2257 offset)
2259 method exit ~uioh ~cancel ~active ~first ~pan =
2260 let uiohopt =
2261 if not cancel
2262 then (
2263 let uioh =
2264 match m_a.(active) with
2265 | _, _, _, Action f -> f uioh
2266 | _, _, _, Noaction -> uioh
2268 Some uioh
2270 else None
2272 m_active <- active;
2273 m_first <- first;
2274 m_pan <- pan;
2275 uiohopt
2277 method hasaction n =
2278 match m_a.(n) with
2279 | _, _, _, Action _ -> true
2280 | _, _, _, Noaction -> false
2282 initializer m_active <- 1
2283 end)
2285 let rec fillsrc prevmode prevuioh =
2286 let sep () = src#caption E.s 0 in
2287 let colorp name get set =
2288 src#string name
2289 (fun () -> color_to_string (get ()))
2290 (fun v ->
2291 try set @@ color_of_string v
2292 with exn ->
2293 state.text <-
2294 Printf.sprintf "bad color `%s': %s" v @@ exntos exn
2297 let rgba name get set =
2298 src#string name
2299 (fun () -> get () |> rgba_to_string)
2300 (fun v ->
2301 try set @@ rgba_of_string v
2302 with exn ->
2303 state.text <-
2304 Printf.sprintf "bad color `%s': %s" v @@ exntos exn
2307 let oldmode = state.mode in
2308 let birdseye = isbirdseye state.mode in
2310 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
2312 src#bool "presentation mode"
2313 (fun () -> conf.presentation)
2314 (fun v -> setpresentationmode v);
2316 src#bool "ignore case in searches"
2317 (fun () -> conf.icase)
2318 (fun v -> conf.icase <- v);
2320 src#bool "preload"
2321 (fun () -> conf.preload)
2322 (fun v -> conf.preload <- v);
2324 src#bool "highlight links"
2325 (fun () -> conf.hlinks)
2326 (fun v -> conf.hlinks <- v);
2328 src#bool "under info"
2329 (fun () -> conf.underinfo)
2330 (fun v -> conf.underinfo <- v);
2332 src#fitmodel "fit model"
2333 (fun () -> FMTE.to_string conf.fitmodel)
2334 (fun v -> reqlayout conf.angle (FMTE.of_int v));
2336 src#bool "trim margins"
2337 (fun () -> conf.trimmargins)
2338 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
2340 sep ();
2341 src#int "inter-page space"
2342 (fun () -> conf.interpagespace)
2343 (fun n ->
2344 conf.interpagespace <- n;
2345 docolumns conf.columns;
2346 let pageno, py =
2347 match state.layout with
2348 | [] -> 0, 0
2349 | l :: _ -> l.pageno, l.pagey
2351 state.maxy <- calcheight ();
2352 let y = getpagey pageno in
2353 gotoxy state.x (y + py)
2356 src#int "page bias"
2357 (fun () -> conf.pagebias)
2358 (fun v -> conf.pagebias <- v);
2360 src#int "scroll step"
2361 (fun () -> conf.scrollstep)
2362 (fun n -> conf.scrollstep <- n);
2364 src#int "horizontal scroll step"
2365 (fun () -> conf.hscrollstep)
2366 (fun v -> conf.hscrollstep <- v);
2368 src#int "auto scroll step"
2369 (fun () ->
2370 match state.autoscroll with
2371 | Some step -> step
2372 | _ -> conf.autoscrollstep)
2373 (fun n ->
2374 let n = boundastep state.winh n in
2375 if state.autoscroll <> None
2376 then state.autoscroll <- Some n;
2377 conf.autoscrollstep <- n);
2379 src#int "zoom"
2380 (fun () -> truncate (conf.zoom *. 100.))
2381 (fun v -> pivotzoom ((float v) /. 100.));
2383 src#int "rotation"
2384 (fun () -> conf.angle)
2385 (fun v -> reqlayout v conf.fitmodel);
2387 src#int "scroll bar width"
2388 (fun () -> conf.scrollbw)
2389 (fun v ->
2390 conf.scrollbw <- v;
2391 reshape state.winw state.winh;
2394 src#int "scroll handle height"
2395 (fun () -> conf.scrollh)
2396 (fun v -> conf.scrollh <- v;);
2398 src#int "thumbnail width"
2399 (fun () -> conf.thumbw)
2400 (fun v ->
2401 conf.thumbw <- min 4096 v;
2402 match oldmode with
2403 | Birdseye beye ->
2404 leavebirdseye beye false;
2405 enterbirdseye ()
2406 | Textentry _
2407 | View
2408 | LinkNav _ -> ()
2411 let mode = state.mode in
2412 src#string "columns"
2413 (fun () ->
2414 match conf.columns with
2415 | Csingle _ -> "1"
2416 | Cmulti (multi, _) -> multicolumns_to_string multi
2417 | Csplit (count, _) -> "-" ^ string_of_int count
2419 (fun v ->
2420 let n, a, b = multicolumns_of_string v in
2421 setcolumns mode n a b);
2423 sep ();
2424 src#caption "Pixmap cache" 0;
2425 src#int_with_suffix "size (advisory)"
2426 (fun () -> conf.memlimit)
2427 (fun v -> conf.memlimit <- v);
2429 src#caption2 "used"
2430 (fun () ->
2431 Printf.sprintf "%s bytes, %d tiles"
2432 (string_with_suffix_of_int state.memused)
2433 (Hashtbl.length state.tilemap)) 1;
2435 sep ();
2436 src#caption "Layout" 0;
2437 src#caption2 "Dimension"
2438 (fun () -> Printf.sprintf "%dx%d (virtual %dx%d)"
2439 state.winw state.winh
2440 state.w state.maxy)
2442 if conf.debug
2443 then src#caption2 "Position" (fun () ->
2444 Printf.sprintf "%dx%d" state.x state.y
2446 else src#caption2 "Position" (fun () -> describe_layout state.layout) 1;
2448 sep ();
2449 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
2450 "Save these parameters as global defaults at exit"
2451 (fun () -> conf.bedefault)
2452 (fun v -> conf.bedefault <- v);
2454 sep ();
2455 let btos b = Utf8syms.(if b then lguillemet else rguillemet) in
2456 src#bool ~offset:0 ~btos "Extended parameters"
2457 (fun () -> !showextended)
2458 (fun v -> showextended := v; fillsrc prevmode prevuioh);
2459 if !showextended
2460 then (
2461 src#bool "checkers"
2462 (fun () -> conf.checkers)
2463 (fun v -> conf.checkers <- v; setcheckers v);
2464 src#bool "update cursor"
2465 (fun () -> conf.updatecurs)
2466 (fun v -> conf.updatecurs <- v);
2467 src#bool "scroll-bar on the left"
2468 (fun () -> conf.leftscroll)
2469 (fun v -> conf.leftscroll <- v);
2470 src#bool "verbose"
2471 (fun () -> conf.verbose)
2472 (fun v -> conf.verbose <- v);
2473 src#bool "invert colors"
2474 (fun () -> conf.invert)
2475 (fun v -> conf.invert <- v);
2476 src#bool "max fit"
2477 (fun () -> conf.maxhfit)
2478 (fun v -> conf.maxhfit <- v);
2479 src#bool "pax mode"
2480 (fun () -> conf.pax != None)
2481 (fun v ->
2482 if v
2483 then conf.pax <- Some (now ())
2484 else conf.pax <- None);
2485 src#string "uri launcher"
2486 (fun () -> conf.urilauncher)
2487 (fun v -> conf.urilauncher <- v);
2488 src#string "path launcher"
2489 (fun () -> conf.pathlauncher)
2490 (fun v -> conf.pathlauncher <- v);
2491 src#string "tile size"
2492 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
2493 (fun v ->
2495 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
2496 conf.tilew <- max 64 w;
2497 conf.tileh <- max 64 h;
2498 flushtiles ();
2499 with exn ->
2500 state.text <- Printf.sprintf "bad tile size `%s': %s"
2501 v @@ exntos exn
2503 src#int "texture count"
2504 (fun () -> conf.texcount)
2505 (fun v ->
2506 if realloctexts v
2507 then conf.texcount <- v
2508 else impmsg "failed to set texture count please retry later"
2510 src#int "slice height"
2511 (fun () -> conf.sliceheight)
2512 (fun v ->
2513 conf.sliceheight <- v;
2514 wcmd "sliceh %d" conf.sliceheight;
2516 src#int "anti-aliasing level"
2517 (fun () -> conf.aalevel)
2518 (fun v ->
2519 conf.aalevel <- bound v 0 8;
2520 state.anchor <- getanchor ();
2521 opendoc state.path state.password;
2523 src#string "page scroll scaling factor"
2524 (fun () -> string_of_float conf.pgscale)
2525 (fun v ->
2526 try conf.pgscale <- float_of_string v
2527 with exn ->
2528 state.text <-
2529 Printf.sprintf "bad page scroll scaling factor `%s': %s" v
2530 @@ exntos exn
2532 src#int "ui font size"
2533 (fun () -> fstate.fontsize)
2534 (fun v -> setfontsize (bound v 5 100));
2535 src#int "hint font size"
2536 (fun () -> conf.hfsize)
2537 (fun v -> conf.hfsize <- bound v 5 100);
2538 src#string "trim fuzz"
2539 (fun () -> irect_to_string conf.trimfuzz)
2540 (fun v ->
2542 conf.trimfuzz <- irect_of_string v;
2543 if conf.trimmargins
2544 then settrim true conf.trimfuzz;
2545 with exn ->
2546 state.text <- Printf.sprintf "bad irect `%s': %s" v
2547 @@ exntos exn
2549 src#string "selection command"
2550 (fun () -> conf.selcmd)
2551 (fun v -> conf.selcmd <- v);
2552 src#string "synctex command"
2553 (fun () -> conf.stcmd)
2554 (fun v -> conf.stcmd <- v);
2555 src#string "pax command"
2556 (fun () -> conf.paxcmd)
2557 (fun v -> conf.paxcmd <- v);
2558 src#string "ask password command"
2559 (fun () -> conf.passcmd)
2560 (fun v -> conf.passcmd <- v);
2561 src#string "save path command"
2562 (fun () -> conf.savecmd)
2563 (fun v -> conf.savecmd <- v);
2564 src#colorspace "color space"
2565 (fun () -> CSTE.to_string conf.colorspace)
2566 (fun v ->
2567 conf.colorspace <- CSTE.of_int v;
2568 wcmd "cs %d" v;
2569 load state.layout;
2571 src#paxmark "pax mark method"
2572 (fun () -> MTE.to_string conf.paxmark)
2573 (fun v -> conf.paxmark <- MTE.of_int v);
2574 if bousable ()
2575 then
2576 src#bool "use PBO"
2577 (fun () -> conf.usepbo)
2578 (fun v -> conf.usepbo <- v);
2579 src#bool "mouse wheel scrolls pages"
2580 (fun () -> conf.wheelbypage)
2581 (fun v -> conf.wheelbypage <- v);
2582 src#bool "open remote links in a new instance"
2583 (fun () -> conf.riani)
2584 (fun v -> conf.riani <- v);
2585 src#bool "edit annotations inline"
2586 (fun () -> conf.annotinline)
2587 (fun v -> conf.annotinline <- v);
2588 src#bool "coarse positioning in presentation mode"
2589 (fun () -> conf.coarseprespos)
2590 (fun v -> conf.coarseprespos <- v);
2591 src#bool "use document CSS"
2592 (fun () -> conf.usedoccss)
2593 (fun v ->
2594 conf.usedoccss <- v;
2595 state.anchor <- getanchor ();
2596 opendoc state.path state.password;
2598 src#bool ~btos "colors"
2599 (fun () -> !showcolors)
2600 (fun v -> showcolors := v; fillsrc prevmode prevuioh);
2601 if !showcolors
2602 then (
2603 colorp " background"
2604 (fun () -> conf.bgcolor)
2605 (fun v -> conf.bgcolor <- v);
2606 rgba " scrollbar"
2607 (fun () -> conf.sbarcolor)
2608 (fun v -> conf.sbarcolor <- v);
2609 rgba " scrollbar handle"
2610 (fun () -> conf.sbarhndlcolor)
2611 (fun v -> conf.sbarhndlcolor <- v);
2615 sep ();
2616 src#caption "Document" 0;
2617 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
2618 src#caption2 "Pages" (fun () -> string_of_int state.pagecount) 1;
2619 src#caption2 "Dimensions"
2620 (fun () -> string_of_int (List.length state.pdims)) 1;
2621 if nonemptystr conf.css
2622 then src#caption2 "CSS" (fun () -> conf.css) 1;
2623 if conf.trimmargins
2624 then (
2625 sep ();
2626 src#caption "Trimmed margins" 0;
2627 src#caption2 "Dimensions"
2628 (fun () -> string_of_int (List.length state.pdims)) 1;
2631 sep ();
2632 src#caption "OpenGL" 0;
2633 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
2634 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
2636 sep ();
2637 src#caption "Location" 0;
2638 if nonemptystr state.origin
2639 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
2640 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
2642 src#reset prevmode prevuioh;
2644 fun () -> (
2645 state.text <- E.s;
2646 resetmstate ();
2647 let prevmode = state.mode
2648 and prevuioh = state.uioh in
2649 fillsrc prevmode prevuioh;
2650 let source = (src :> lvsource) in
2651 let modehash = findkeyhash conf "info" in
2652 state.uioh <-
2653 coe (object (self)
2654 inherit listview ~zebra:false ~helpmode:false
2655 ~source ~trusted:true ~modehash as super
2656 val mutable m_prevmemused = 0
2657 method! infochanged = function
2658 | Memused ->
2659 if m_prevmemused != state.memused
2660 then (
2661 m_prevmemused <- state.memused;
2662 postRedisplay "memusedchanged";
2664 | Pdim -> postRedisplay "pdimchanged"
2665 | Docinfo -> fillsrc prevmode prevuioh
2667 method! key key mask =
2668 if not (Wsi.withctrl mask)
2669 then
2670 match [@warning "-4"] Wsi.kc2kt key with
2671 | Keys.Left -> coe (self#updownlevel ~-1)
2672 | Keys.Right -> coe (self#updownlevel 1)
2673 | _ -> super#key key mask
2674 else super#key key mask
2675 end);
2676 postRedisplay "info";
2680 let enterhelpmode =
2681 let source =
2682 (object
2683 inherit lvsourcebase
2684 method getitemcount = Array.length state.help
2685 method getitem n =
2686 let s, l, _ = state.help.(n) in
2687 (s, l)
2689 method exit ~uioh ~cancel ~active ~first ~pan =
2690 let optuioh =
2691 if not cancel
2692 then (
2693 match state.help.(active) with
2694 | _, _, Action f -> Some (f uioh)
2695 | _, _, Noaction -> Some uioh
2697 else None
2699 m_active <- active;
2700 m_first <- first;
2701 m_pan <- pan;
2702 optuioh
2704 method hasaction n =
2705 match state.help.(n) with
2706 | _, _, Action _ -> true
2707 | _, _, Noaction -> false
2709 initializer
2710 m_active <- -1
2711 end)
2713 fun () ->
2714 let modehash = findkeyhash conf "help" in
2715 resetmstate ();
2716 state.uioh <- coe (new listview
2717 ~zebra:false ~helpmode:true
2718 ~source ~trusted:true ~modehash);
2719 postRedisplay "help";
2722 let entermsgsmode =
2723 let msgsource =
2724 (object
2725 inherit lvsourcebase
2726 val mutable m_items = E.a
2728 method getitemcount = 1 + Array.length m_items
2730 method getitem n =
2731 if n = 0
2732 then "[Clear]", 0
2733 else m_items.(n-1), 0
2735 method exit ~uioh ~cancel ~active ~first ~pan =
2736 ignore uioh;
2737 if not cancel
2738 then (
2739 if active = 0
2740 then Buffer.clear state.errmsgs;
2742 m_active <- active;
2743 m_first <- first;
2744 m_pan <- pan;
2745 None
2747 method hasaction n =
2748 n = 0
2750 method reset =
2751 state.newerrmsgs <- false;
2752 let l = Str.split Utils.Re.crlf (Buffer.contents state.errmsgs) in
2753 m_items <- Array.of_list l
2755 initializer
2756 m_active <- 0
2757 end)
2758 in fun () ->
2759 state.text <- E.s;
2760 resetmstate ();
2761 msgsource#reset;
2762 let source = (msgsource :> lvsource) in
2763 let modehash = findkeyhash conf "listview" in
2764 state.uioh <-
2765 coe (object
2766 inherit listview ~zebra:false ~helpmode:false
2767 ~source ~trusted:false ~modehash as super
2768 method! display =
2769 if state.newerrmsgs
2770 then msgsource#reset;
2771 super#display
2772 end);
2773 postRedisplay "msgs";
2776 let getusertext s =
2777 let editor = getenvdef "EDITOR" E.s in
2778 if emptystr editor
2779 then E.s
2780 else
2781 let tmppath = Filename.temp_file "llpp" "note" in
2782 if nonemptystr s
2783 then (
2784 let oc = open_out tmppath in
2785 output_string oc s;
2786 close_out oc;
2788 let execstr = editor ^ " " ^ tmppath in
2789 let s =
2790 match spawn execstr [] with
2791 | exception exn ->
2792 impmsg "spawn(%S) failed: %s" execstr @@ exntos exn;
2794 | pid ->
2795 match Unix.waitpid [] pid with
2796 | exception exn ->
2797 impmsg "waitpid(%d) failed: %s" pid @@ exntos exn;
2799 | (_pid, status) ->
2800 match status with
2801 | Unix.WEXITED 0 -> filecontents tmppath
2802 | Unix.WEXITED n ->
2803 impmsg "editor process(%s) exited abnormally: %d" execstr n;
2805 | Unix.WSIGNALED n ->
2806 impmsg "editor process(%s) was killed by signal %d" execstr n;
2808 | Unix.WSTOPPED n ->
2809 impmsg "editor(%s) process was stopped by signal %d" execstr n;
2812 match Unix.unlink tmppath with
2813 | exception exn ->
2814 impmsg "failed to ulink %S: %s" tmppath @@ exntos exn;
2816 | () -> s
2819 let enterannotmode opaque slinkindex =
2820 let msgsource =
2821 (object
2822 inherit lvsourcebase
2823 val mutable m_text = E.s
2824 val mutable m_items = E.a
2826 method getitemcount = Array.length m_items
2828 method getitem n =
2829 let label, _func = m_items.(n) in
2830 label, 0
2832 method exit ~uioh ~cancel ~active ~first ~pan =
2833 ignore (uioh, first, pan);
2834 if not cancel
2835 then (
2836 let _label, func = m_items.(active) in
2837 func ()
2839 None
2841 method hasaction n = nonemptystr @@ fst m_items.(n)
2843 method reset s =
2844 let rec split accu b i =
2845 let p = b+i in
2846 if p = String.length s
2847 then (String.sub s b (p-b), fun () -> ()) :: accu
2848 else
2849 if (i > 70 && s.[p] = ' ') || s.[p] = '\r' || s.[p] = '\n'
2850 then
2851 let ss = if i = 0 then E.s else String.sub s b i in
2852 split ((ss, fun () -> ())::accu) (p+1) 0
2853 else split accu b (i+1)
2855 let cleanup () =
2856 wcmd "freepage %s" (~> opaque);
2857 let keys =
2858 Hashtbl.fold (fun key opaque' accu ->
2859 if opaque' = opaque'
2860 then key :: accu else accu) state.pagemap []
2862 List.iter (Hashtbl.remove state.pagemap) keys;
2863 flushtiles ();
2864 gotoxy state.x state.y
2866 let dele () =
2867 delannot opaque slinkindex;
2868 cleanup ();
2870 let edit inline () =
2871 let update s =
2872 if emptystr s
2873 then dele ()
2874 else (
2875 modannot opaque slinkindex s;
2876 cleanup ();
2879 if inline
2880 then
2881 let mode = state.mode in
2882 state.mode <-
2883 Textentry (
2884 ("annotation: ", m_text, None, textentry, update, true),
2885 fun _ -> state.mode <- mode
2887 state.text <- E.s;
2888 enttext ();
2889 else
2890 let s = getusertext m_text in
2891 update s
2893 m_text <- s;
2894 m_items <-
2895 ( "[Copy]", fun () -> selstring conf.selcmd m_text)
2896 :: ("[Delete]", dele)
2897 :: ("[Edit]", edit conf.annotinline)
2898 :: (E.s, fun () -> ())
2899 :: split [] 0 0 |> List.rev |> Array.of_list
2901 initializer
2902 m_active <- 0
2903 end)
2905 state.text <- E.s;
2906 let s = getannotcontents opaque slinkindex in
2907 resetmstate ();
2908 msgsource#reset s;
2909 let source = (msgsource :> lvsource) in
2910 let modehash = findkeyhash conf "listview" in
2911 state.uioh <- coe (object
2912 inherit listview ~zebra:false ~helpmode:false
2913 ~source ~trusted:false ~modehash
2914 end);
2915 postRedisplay "enterannotmode";
2918 let gotoremote spec =
2919 let filename, dest = splitatchar spec '#' in
2920 let getpath filename =
2921 let path =
2922 if nonemptystr filename
2923 then
2924 if Filename.is_relative filename
2925 then
2926 let dir = Filename.dirname state.path in
2927 let dir =
2928 if Filename.is_implicit dir
2929 then Filename.concat (Sys.getcwd ()) dir
2930 else dir
2932 Filename.concat dir filename
2933 else filename
2934 else E.s
2936 if Sys.file_exists path
2937 then path
2938 else E.s
2940 let path = getpath filename in
2941 let dospawn lcmd =
2942 if conf.riani
2943 then
2944 let cmd = Lazy.force_val lcmd in
2945 match spawn cmd with
2946 | _pid -> ()
2947 | exception exn -> dolog "failed to execute `%s': %s" cmd @@ exntos exn
2948 else
2949 let anchor = getanchor () in
2950 let ranchor = state.path, state.password, anchor, state.origin in
2951 state.origin <- E.s;
2952 state.ranchors <- ranchor :: state.ranchors;
2953 opendoc path E.s;
2955 if substratis spec 0 "page="
2956 then
2957 match Scanf.sscanf spec "page=%d" (fun n -> n) with
2958 | pageno ->
2959 state.anchor <- (pageno, 0.0, 0.0);
2960 dospawn @@ lazy (Printf.sprintf "%s -page %d %S" !selfexec pageno path);
2961 | exception exn ->
2962 adderrfmt "error parsing remote destination" "page: %s" @@ exntos exn
2963 else (
2964 state.nameddest <- dest;
2965 dospawn @@ lazy (!selfexec ^ " " ^ path ^ " -dest " ^ dest)
2969 let gotounder = function
2970 | Ulinkuri s when isexternallink s ->
2971 if substratis s 0 "file://"
2972 then gotoremote @@ String.sub s 7 (String.length s - 7)
2973 else Help.gotouri conf.urilauncher s
2974 | Ulinkuri s ->
2975 let pageno, x, y = uritolocation s in
2976 addnav ();
2977 gotopagexy pageno x y
2978 | Utext _ | Unone -> ()
2979 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
2982 let gotooutline (_, _, kind) =
2983 match kind with
2984 | Onone -> ()
2985 | Oanchor ((pageno, y, _) as anchor) ->
2986 addnav ();
2987 gotoxy state.x @@
2988 getanchory (if conf.presentation then (pageno, y, 1.0) else anchor)
2989 | Ouri uri -> gotounder (Ulinkuri uri)
2990 | Olaunch cmd -> error "gotounder (Ulaunch %S)" cmd
2991 | Oremote (remote, pageno) ->
2992 error "gotounder (Uremote (%S,%d) )" remote pageno
2993 | Ohistory hist -> gotohist hist
2994 | Oremotedest (path, dest) ->
2995 error "gotounder (Uremotedest (%S, %S))" path dest
2998 class outlinesoucebase fetchoutlines = object (self)
2999 inherit lvsourcebase
3000 val mutable m_items = E.a
3001 val mutable m_minfo = E.a
3002 val mutable m_orig_items = E.a
3003 val mutable m_orig_minfo = E.a
3004 val mutable m_narrow_patterns = []
3005 val mutable m_gen = -1
3007 method getitemcount = Array.length m_items
3009 method getitem n =
3010 let s, n, _ = m_items.(n) in
3011 (s, n+0)
3013 method exit ~(uioh:uioh) ~cancel ~active ~(first:int) ~pan : uioh option =
3014 ignore (uioh, first);
3015 let items, minfo =
3016 if m_narrow_patterns = []
3017 then m_orig_items, m_orig_minfo
3018 else m_items, m_minfo
3020 m_pan <- pan;
3021 if not cancel
3022 then (
3023 m_items <- items;
3024 m_minfo <- minfo;
3025 gotooutline m_items.(active);
3027 else (
3028 m_items <- items;
3029 m_minfo <- minfo;
3031 None
3033 method hasaction (_:int) = true
3035 method greetmsg =
3036 if Array.length m_items != Array.length m_orig_items
3037 then
3038 let s =
3039 match m_narrow_patterns with
3040 | one :: [] -> one
3041 | many -> String.concat Utf8syms.ellipsis (List.rev many)
3043 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
3044 else E.s
3046 method statestr =
3047 match m_narrow_patterns with
3048 | [] -> E.s
3049 | one :: [] -> one
3050 | head :: _ -> Utf8syms.ellipsis ^ head
3052 method narrow pattern =
3053 match Str.regexp_case_fold pattern with
3054 | exception _ -> ()
3055 | re ->
3056 let rec loop accu minfo n =
3057 if n = -1
3058 then (
3059 m_items <- Array.of_list accu;
3060 m_minfo <- Array.of_list minfo;
3062 else
3063 let (s, _, _) as o = m_items.(n) in
3064 let accu, minfo =
3065 match Str.search_forward re s 0 with
3066 | exception Not_found -> accu, minfo
3067 | first -> o :: accu, (first, Str.match_end ()) :: minfo
3069 loop accu minfo (n-1)
3071 loop [] [] (Array.length m_items - 1)
3073 method! getminfo = m_minfo
3075 method denarrow =
3076 m_orig_items <- fetchoutlines ();
3077 m_minfo <- m_orig_minfo;
3078 m_items <- m_orig_items
3080 method add_narrow_pattern pattern =
3081 m_narrow_patterns <- pattern :: m_narrow_patterns
3083 method del_narrow_pattern =
3084 match m_narrow_patterns with
3085 | _ :: rest -> m_narrow_patterns <- rest
3086 | [] -> ()
3088 method renarrow =
3089 self#denarrow;
3090 match m_narrow_patterns with
3091 | pattern :: [] -> self#narrow pattern; pattern
3092 | list ->
3093 List.fold_left (fun accu pattern ->
3094 self#narrow pattern;
3095 pattern ^ Utf8syms.ellipsis ^ accu) E.s list
3097 method calcactive (_:anchor) = 0
3099 method reset anchor items =
3100 if state.gen != m_gen
3101 then (
3102 m_orig_items <- items;
3103 m_items <- items;
3104 m_narrow_patterns <- [];
3105 m_minfo <- E.a;
3106 m_orig_minfo <- E.a;
3107 m_gen <- state.gen;
3109 else (
3110 if items != m_orig_items
3111 then (
3112 m_orig_items <- items;
3113 if m_narrow_patterns == []
3114 then m_items <- items;
3117 let active = self#calcactive anchor in
3118 m_active <- active;
3119 m_first <- firstof m_first active
3123 let outlinesource fetchoutlines =
3124 (object
3125 inherit outlinesoucebase fetchoutlines
3126 method! calcactive anchor =
3127 let rely = getanchory anchor in
3128 let rec loop n best bestd =
3129 if n = Array.length m_items
3130 then best
3131 else
3132 let _, _, kind = m_items.(n) in
3133 match kind with
3134 | Oanchor anchor ->
3135 let orely = getanchory anchor in
3136 let d = abs (orely - rely) in
3137 if d < bestd
3138 then loop (n+1) n d
3139 else loop (n+1) best bestd
3140 | Onone | Oremote _ | Olaunch _
3141 | Oremotedest _ | Ouri _ | Ohistory _ ->
3142 loop (n+1) best bestd
3144 loop 0 ~-1 max_int
3145 end)
3148 let enteroutlinemode, enterbookmarkmode, enterhistmode =
3149 let mkselector sourcetype =
3150 let fetchoutlines () =
3151 match sourcetype with
3152 | `bookmarks -> Array.of_list state.bookmarks
3153 | `outlines -> state.outlines
3154 | `history -> genhistoutlines () |> Array.of_list
3156 let source =
3157 if sourcetype = `history
3158 then new outlinesoucebase fetchoutlines
3159 else outlinesource fetchoutlines
3161 (fun errmsg ->
3162 let outlines = fetchoutlines () in
3163 if Array.length outlines = 0
3164 then showtext ' ' errmsg
3165 else (
3166 resetmstate ();
3167 Wsi.setcursor Wsi.CURSOR_INHERIT;
3168 let anchor = getanchor () in
3169 source#reset anchor outlines;
3170 state.text <- source#greetmsg;
3171 state.uioh <-
3172 coe (new outlinelistview ~zebra:(sourcetype=`history) ~source);
3173 postRedisplay "enter selector";
3177 let mkenter sourcetype errmsg = fun () -> mkselector sourcetype errmsg in
3178 ( mkenter `outlines "document has no outline"
3179 , mkenter `bookmarks "document has no bookmarks (yet)"
3180 , mkenter `history "history is empty" )
3183 let quickbookmark ?title () =
3184 match state.layout with
3185 | [] -> ()
3186 | l :: _ ->
3187 let title =
3188 match title with
3189 | None ->
3190 Unix.(
3191 let tm = localtime (now ()) in
3192 Printf.sprintf
3193 "Quick (page %d) (bookmarked on %02d/%02d/%d at %02d:%02d)"
3194 (l.pageno+1)
3195 tm.tm_mday (tm.tm_mon+1) (tm.tm_year+1900) tm.tm_hour tm.tm_min
3197 | Some title -> title
3199 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
3202 let setautoscrollspeed step goingdown =
3203 let incr = max 1 ((abs step) / 2) in
3204 let incr = if goingdown then incr else -incr in
3205 let astep = boundastep state.winh (step + incr) in
3206 state.autoscroll <- Some astep;
3209 let canpan () =
3210 match conf.columns with
3211 | Csplit _ -> true
3212 | Csingle _ | Cmulti _ -> state.x != 0 || conf.zoom > 1.0
3215 let panbound x = bound x (-state.w) state.winw;;
3217 let existsinrow pageno (columns, coverA, coverB) p =
3218 let last = ((pageno - coverA) mod columns) + columns in
3219 let rec any = function
3220 | [] -> false
3221 | l :: rest ->
3222 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
3223 then p l
3224 else (
3225 if not (p l)
3226 then (if l.pageno = last then false else any rest)
3227 else true
3230 any state.layout
3233 let nextpage () =
3234 match state.layout with
3235 | [] ->
3236 let pageno = page_of_y state.y in
3237 gotoxy state.x (getpagey (pageno+1))
3238 | l :: rest ->
3239 match conf.columns with
3240 | Csingle _ ->
3241 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
3242 then
3243 let y = clamp (pgscale state.winh) in
3244 gotoxy state.x y
3245 else
3246 let pageno = min (l.pageno+1) (state.pagecount-1) in
3247 gotoxy state.x (getpagey pageno)
3248 | Cmulti ((c, _, _) as cl, _) ->
3249 if conf.presentation
3250 && (existsinrow l.pageno cl
3251 (fun l -> l.pageh > l.pagey + l.pagevh))
3252 then
3253 let y = clamp (pgscale state.winh) in
3254 gotoxy state.x y
3255 else
3256 let pageno = min (l.pageno+c) (state.pagecount-1) in
3257 gotoxy state.x (getpagey pageno)
3258 | Csplit (n, _) ->
3259 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
3260 then
3261 let pagey, pageh = getpageyh l.pageno in
3262 let pagey = pagey + pageh * l.pagecol in
3263 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
3264 gotoxy state.x (pagey + pageh + ips)
3267 let prevpage () =
3268 match state.layout with
3269 | [] ->
3270 let pageno = page_of_y state.y in
3271 gotoxy state.x (getpagey (pageno-1))
3272 | l :: _ ->
3273 match conf.columns with
3274 | Csingle _ ->
3275 if conf.presentation && l.pagey != 0
3276 then gotoxy state.x (clamp (pgscale ~-(state.winh)))
3277 else
3278 let pageno = max 0 (l.pageno-1) in
3279 gotoxy state.x (getpagey pageno)
3280 | Cmulti ((c, _, coverB) as cl, _) ->
3281 if conf.presentation &&
3282 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
3283 then gotoxy state.x (clamp (pgscale ~-(state.winh)))
3284 else
3285 let decr =
3286 if l.pageno = state.pagecount - coverB
3287 then 1
3288 else c
3290 let pageno = max 0 (l.pageno-decr) in
3291 gotoxy state.x (getpagey pageno)
3292 | Csplit (n, _) ->
3293 let y =
3294 if l.pagecol = 0
3295 then
3296 if l.pageno = 0
3297 then l.pagey
3298 else
3299 let pageno = max 0 (l.pageno-1) in
3300 let pagey, pageh = getpageyh pageno in
3301 pagey + (n-1)*pageh
3302 else
3303 let pagey, pageh = getpageyh l.pageno in
3304 pagey + pageh * (l.pagecol-1) - conf.interpagespace
3306 gotoxy state.x y
3309 let save () =
3310 if emptystr conf.savecmd
3311 then adderrmsg "savepath-command is empty"
3312 "don't know where to save modified document"
3313 else
3314 let savecmd = Str.global_replace Utils.Re.percent state.path conf.savecmd in
3315 let path =
3316 getcmdoutput
3317 (fun exn ->
3318 adderrfmt savecmd "failed to produce path to the saved copy: %s" exn)
3319 savecmd
3321 if nonemptystr path
3322 then
3323 let tmp = path ^ ".tmp" in
3324 savedoc tmp;
3325 Unix.rename tmp path;
3328 let viewkeyboard key mask =
3329 let enttext te =
3330 let mode = state.mode in
3331 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
3332 state.text <- E.s;
3333 enttext ();
3334 postRedisplay "view:enttext"
3336 let ctrl = Wsi.withctrl mask in
3337 let open Keys in
3338 match Wsi.kc2kt key with
3339 | Ascii 'S' -> state.slideshow <- state.slideshow lxor 1
3341 | Ascii 'Q' -> exit 0
3343 | Ascii 'W' ->
3344 if hasunsavedchanges ()
3345 then save ()
3347 | Insert ->
3348 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
3349 then (
3350 state.mode <- (
3351 match state.lnava with
3352 | None -> LinkNav (Ltgendir 0)
3353 | Some pn -> LinkNav (Ltexact pn)
3355 gotoxy state.x state.y;
3357 else impmsg "keyboard link navigation does not work under rotation"
3359 | Escape | Ascii 'q' ->
3360 begin match state.mstate with
3361 | Mzoomrect _ ->
3362 resetmstate ();
3363 postRedisplay "kill rect";
3364 | Msel _
3365 | Mpan _
3366 | Mscrolly | Mscrollx
3367 | Mzoom _
3368 | Mnone ->
3369 begin match state.mode with
3370 | LinkNav ln ->
3371 begin match ln with
3372 | Ltexact pl -> state.lnava <- Some pl
3373 | Ltgendir _ | Ltnotready _ -> state.lnava <- None
3374 end;
3375 state.mode <- View;
3376 postRedisplay "esc leave linknav"
3377 | Birdseye _ | Textentry _ | View ->
3378 match state.ranchors with
3379 | [] -> raise Quit
3380 | (path, password, anchor, origin) :: rest ->
3381 state.ranchors <- rest;
3382 state.anchor <- anchor;
3383 state.origin <- origin;
3384 state.nameddest <- E.s;
3385 opendoc path password
3386 end;
3387 end;
3389 | Backspace ->
3390 addnavnorc ();
3391 gotoxy state.x (getnav ~-1)
3393 | Ascii 'o' -> enteroutlinemode ()
3394 | Ascii 'H' -> enterhistmode ()
3396 | Ascii 'u' ->
3397 state.rects <- [];
3398 state.text <- E.s;
3399 Hashtbl.iter (fun _ opaque ->
3400 clearmark opaque;
3401 Hashtbl.clear state.prects) state.pagemap;
3402 postRedisplay "dehighlight";
3404 | Ascii (('/' | '?') as c) ->
3405 let ondone isforw s =
3406 cbput state.hists.pat s;
3407 state.searchpattern <- s;
3408 search s isforw
3410 let s = String.make 1 c in
3411 enttext (s, E.s, Some (onhist state.hists.pat),
3412 textentry, ondone (c = '/'), true)
3414 | Ascii '+' | Ascii '=' when ctrl ->
3415 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
3416 pivotzoom (conf.zoom +. incr)
3418 | Ascii '+' ->
3419 let ondone s =
3420 let n =
3421 try int_of_string s with exn ->
3422 state.text <-
3423 Printf.sprintf "bad integer `%s': %s" s @@ exntos exn;
3424 max_int
3426 if n != max_int
3427 then (
3428 conf.pagebias <- n;
3429 state.text <- "page bias is now " ^ string_of_int n;
3432 enttext ("page bias: ", E.s, None, intentry, ondone, true)
3434 | Ascii '-' when ctrl ->
3435 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
3436 pivotzoom (max 0.01 (conf.zoom -. decr))
3438 | Ascii '-' ->
3439 let ondone msg = state.text <- msg in
3440 enttext ("option: ", E.s, None,
3441 optentry state.mode, ondone, true)
3443 | Ascii '0' when ctrl ->
3444 if conf.zoom = 1.0
3445 then gotoxy 0 state.y
3446 else setzoom 1.0
3448 | Ascii ('1'|'2' as c) when ctrl && conf.fitmodel != FitPage ->
3449 let cols =
3450 match conf.columns with
3451 | Csingle _ | Cmulti _ -> 1
3452 | Csplit (n, _) -> n
3454 let h = state.winh -
3455 conf.interpagespace lsl (if conf.presentation then 1 else 0)
3457 let zoom = zoomforh state.winw h 0 cols in
3458 if zoom > 0.0 && (c = '2' || zoom < 1.0)
3459 then setzoom zoom
3461 | Ascii '3' when ctrl ->
3462 let fm =
3463 match conf.fitmodel with
3464 | FitWidth -> FitProportional
3465 | FitProportional -> FitPage
3466 | FitPage -> FitWidth
3468 state.text <- "fit model: " ^ FMTE.to_string fm;
3469 reqlayout conf.angle fm
3471 | Ascii '4' when ctrl ->
3472 let zoom = getmaxw () /. float state.winw in
3473 if zoom > 0.0 then setzoom zoom
3475 | Fn 9 | Ascii '9' when ctrl -> togglebirdseye ()
3477 | Ascii ('0'..'9' as c) when not ctrl ->
3478 let ondone s =
3479 let n =
3480 try int_of_string s with exn ->
3481 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn;
3484 if n >= 0
3485 then (
3486 addnav ();
3487 cbput state.hists.pag (string_of_int n);
3488 gotopage1 (n + conf.pagebias - 1) 0;
3491 let pageentry text = function [@warning "-4"]
3492 | Keys.Ascii 'g' -> TEdone text
3493 | key -> intentry text key
3495 let text = String.make 1 c in
3496 enttext (":", text, Some (onhist state.hists.pag),
3497 pageentry, ondone, true)
3499 | Ascii 'b' ->
3500 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
3501 postRedisplay "toggle scrollbar";
3503 | Ascii 'B' ->
3504 state.bzoom <- not state.bzoom;
3505 state.rects <- [];
3506 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
3508 | Ascii 'l' ->
3509 conf.hlinks <- not conf.hlinks;
3510 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
3511 postRedisplay "toggle highlightlinks";
3513 | Ascii 'F' ->
3514 if conf.angle mod 360 = 0
3515 then (
3516 state.glinks <- true;
3517 let mode = state.mode in
3518 state.mode <-
3519 Textentry (
3520 (":", E.s, None, linknentry, linknact gotounder, false),
3521 (fun _ ->
3522 state.glinks <- false;
3523 state.mode <- mode)
3525 state.text <- E.s;
3526 postRedisplay "view:linkent(F)"
3528 else impmsg "hint mode does not work under rotation"
3530 | Ascii 'y' ->
3531 state.glinks <- true;
3532 let mode = state.mode in
3533 state.mode <-
3534 Textentry (
3535 (":", E.s, None, linknentry,
3536 linknact (fun under ->
3537 selstring conf.selcmd (undertext under)), false),
3538 (fun _ ->
3539 state.glinks <- false;
3540 state.mode <- mode)
3542 state.text <- E.s;
3543 postRedisplay "view:linkent"
3545 | Ascii 'a' ->
3546 begin match state.autoscroll with
3547 | Some step ->
3548 conf.autoscrollstep <- step;
3549 state.autoscroll <- None
3550 | None ->
3551 state.autoscroll <- Some conf.autoscrollstep;
3552 state.slideshow <- state.slideshow land lnot 2
3555 | Ascii 'p' when ctrl ->
3556 launchpath () (* XXX where do error messages go? *)
3558 | Ascii 'P' ->
3559 setpresentationmode (not conf.presentation);
3560 showtext ' ' ("presentation mode " ^
3561 if conf.presentation then "on" else "off");
3563 | Ascii 'f' ->
3564 if List.mem Wsi.Fullscreen state.winstate
3565 then Wsi.reshape conf.cwinw conf.cwinh
3566 else Wsi.fullscreen ()
3568 | Ascii ('p'|'N') -> search state.searchpattern false
3569 | Ascii 'n' | Fn 3 -> search state.searchpattern true
3571 | Ascii 't' ->
3572 begin match state.layout with
3573 | [] -> ()
3574 | l :: _ -> gotoxy state.x (getpagey l.pageno)
3577 | Ascii ' ' -> nextpage ()
3578 | Delete -> prevpage ()
3579 | Ascii '=' -> showtext ' ' (describe_layout state.layout);
3581 | Ascii 'w' ->
3582 begin match state.layout with
3583 | [] -> ()
3584 | l :: _ ->
3585 Wsi.reshape l.pagew l.pageh;
3586 postRedisplay "w"
3589 | Ascii '\'' -> enterbookmarkmode ()
3590 | Ascii 'h' | Fn 1 -> enterhelpmode ()
3591 | Ascii 'i' -> enterinfomode ()
3592 | Ascii 'e' when Buffer.length state.errmsgs > 0 -> entermsgsmode ()
3594 | Ascii 'm' ->
3595 let ondone s =
3596 match state.layout with
3597 | l :: _ when nonemptystr s ->
3598 state.bookmarks <- (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
3599 | _ -> ()
3601 enttext ("bookmark: ", E.s, None, textentry, ondone, true)
3603 | Ascii '~' ->
3604 quickbookmark ();
3605 showtext ' ' "Quick bookmark added";
3607 | Ascii 'x' -> state.roam ()
3609 | Ascii ('<'|'>' as c) ->
3610 reqlayout (conf.angle + (if c = '>' then 30 else -30)) conf.fitmodel
3612 | Ascii ('['|']' as c) ->
3613 conf.colorscale <-
3614 bound (conf.colorscale +. (if c = ']' then 0.1 else -0.1)) 0.0 1.0;
3615 postRedisplay "brightness";
3617 | Ascii 'c' when state.mode = View ->
3618 if Wsi.withalt mask
3619 then (
3620 if conf.zoom > 1.0
3621 then
3622 let m = (state.winw - state.w) / 2 in
3623 gotoxy m state.y
3625 else
3626 let (c, a, b), z =
3627 match state.prevcolumns with
3628 | None -> (1, 0, 0), 1.0
3629 | Some (columns, z) ->
3630 let cab =
3631 match columns with
3632 | Csplit (c, _) -> -c, 0, 0
3633 | Cmulti ((c, a, b), _) -> c, a, b
3634 | Csingle _ -> 1, 0, 0
3636 cab, z
3638 setcolumns View c a b;
3639 setzoom z
3641 | Down | Up when ctrl && Wsi.withshift mask ->
3642 let zoom, x = state.prevzoom in
3643 setzoom zoom;
3644 state.x <- x;
3646 | Ascii 'k' | Up ->
3647 begin match state.autoscroll with
3648 | None ->
3649 begin match state.mode with
3650 | Birdseye beye -> upbirdseye 1 beye
3651 | Textentry _ | View | LinkNav _ ->
3652 if ctrl
3653 then gotoxy state.x (clamp ~-(state.winh/2))
3654 else (
3655 if not (Wsi.withshift mask) && conf.presentation
3656 then prevpage ()
3657 else gotoxy state.x (clamp (-conf.scrollstep))
3660 | Some n -> setautoscrollspeed n false
3663 | Ascii 'j' | Down ->
3664 begin match state.autoscroll with
3665 | None ->
3666 begin match state.mode with
3667 | Birdseye beye -> downbirdseye 1 beye
3668 | Textentry _ | View | LinkNav _ ->
3669 if ctrl
3670 then gotoxy state.x (clamp (state.winh/2))
3671 else (
3672 if not (Wsi.withshift mask) && conf.presentation
3673 then nextpage ()
3674 else gotoxy state.x (clamp (conf.scrollstep))
3677 | Some n -> setautoscrollspeed n true
3680 | Left | Right when not (Wsi.withalt mask) ->
3681 if canpan ()
3682 then
3683 let dx =
3684 if ctrl
3685 then state.winw / 2
3686 else conf.hscrollstep
3688 let dx =
3689 let pv = Wsi.kc2kt key in
3690 if pv = Keys.Left then dx else -dx
3692 gotoxy (panbound (state.x + dx)) state.y
3693 else (
3694 state.text <- E.s;
3695 postRedisplay "left/right"
3698 | Prior ->
3699 let y =
3700 if ctrl
3701 then
3702 match state.layout with
3703 | [] -> state.y
3704 | l :: _ -> state.y - l.pagey
3705 else clamp (pgscale (-state.winh))
3707 gotoxy state.x y
3709 | Next ->
3710 let y =
3711 if ctrl
3712 then
3713 match List.rev state.layout with
3714 | [] -> state.y
3715 | l :: _ -> getpagey l.pageno
3716 else clamp (pgscale state.winh)
3718 gotoxy state.x y
3720 | Ascii 'g' | Home ->
3721 addnav ();
3722 gotoxy 0 0
3723 | Ascii 'G' | End ->
3724 addnav ();
3725 gotoxy 0 (clamp state.maxy)
3727 | Right when Wsi.withalt mask ->
3728 addnavnorc ();
3729 gotoxy state.x (getnav 1)
3730 | Left when Wsi.withalt mask ->
3731 addnavnorc ();
3732 gotoxy state.x (getnav ~-1)
3734 | Ascii 'r' ->
3735 reload ()
3737 | Ascii 'v' when conf.debug ->
3738 state.rects <- [];
3739 List.iter (fun l ->
3740 match getopaque l.pageno with
3741 | None -> ()
3742 | Some opaque ->
3743 let x0, y0, x1, y1 = pagebbox opaque in
3744 let rect = (float x0, float y0,
3745 float x1, float y0,
3746 float x1, float y1,
3747 float x0, float y1) in
3748 debugrect rect;
3749 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
3750 state.rects <- (l.pageno, color, rect) :: state.rects;
3751 ) state.layout;
3752 postRedisplay "v";
3754 | Ascii '|' ->
3755 let mode = state.mode in
3756 let cmd = ref E.s in
3757 let onleave = function
3758 | Cancel -> state.mode <- mode
3759 | Confirm ->
3760 List.iter (fun l ->
3761 match getopaque l.pageno with
3762 | Some opaque -> pipesel opaque !cmd
3763 | None -> ()) state.layout;
3764 state.mode <- mode
3766 let ondone s =
3767 cbput state.hists.sel s;
3768 cmd := s
3770 let te =
3771 "| ", !cmd, Some (onhist state.hists.sel), textentry, ondone, true
3773 postRedisplay "|";
3774 state.mode <- Textentry (te, onleave);
3776 | (Ascii _|Fn _|Enter|Left|Right|Code _|Ctrl _) ->
3777 vlog "huh? %s" (Wsi.keyname key)
3780 let linknavkeyboard key mask linknav =
3781 let pv = Wsi.kc2kt key in
3782 let getpage pageno =
3783 let rec loop = function
3784 | [] -> None
3785 | l :: _ when l.pageno = pageno -> Some l
3786 | _ :: rest -> loop rest
3787 in loop state.layout
3789 let doexact (pageno, n) =
3790 match getopaque pageno, getpage pageno with
3791 | Some opaque, Some l ->
3792 if pv = Keys.Enter
3793 then
3794 let under = getlink opaque n in
3795 postRedisplay "link gotounder";
3796 gotounder under;
3797 state.mode <- View;
3798 else
3799 let opt, dir =
3800 let open Keys in
3801 match pv with
3802 | Home -> Some (findlink opaque LDfirst), -1
3803 | End -> Some (findlink opaque LDlast), 1
3804 | Left -> Some (findlink opaque (LDleft n)), -1
3805 | Right -> Some (findlink opaque (LDright n)), 1
3806 | Up -> Some (findlink opaque (LDup n)), -1
3807 | Down -> Some (findlink opaque (LDdown n)), 1
3808 | Delete|Escape|Insert|Enter|Next|Prior|Ascii _
3809 | Code _|Fn _|Ctrl _|Backspace -> None, 0
3811 let pwl l dir =
3812 begin match findpwl l.pageno dir with
3813 | Pwlnotfound -> ()
3814 | Pwl pageno ->
3815 let notfound dir =
3816 state.mode <- LinkNav (Ltgendir dir);
3817 let y, h = getpageyh pageno in
3818 let y =
3819 if dir < 0
3820 then y + h - state.winh
3821 else y
3823 gotoxy state.x y
3825 begin match getopaque pageno, getpage pageno with
3826 | Some opaque, Some _ ->
3827 let link =
3828 let ld = if dir > 0 then LDfirst else LDlast in
3829 findlink opaque ld
3831 begin match link with
3832 | Lfound m ->
3833 showlinktype (getlink opaque m);
3834 state.mode <- LinkNav (Ltexact (pageno, m));
3835 postRedisplay "linknav jpage";
3836 | Lnotfound -> notfound dir
3837 end;
3838 | _ -> notfound dir
3839 end;
3840 end;
3842 begin match opt with
3843 | Some Lnotfound -> pwl l dir;
3844 | Some (Lfound m) ->
3845 if m = n
3846 then pwl l dir
3847 else (
3848 let _, y0, _, y1 = getlinkrect opaque m in
3849 if y0 < l.pagey
3850 then gotopage1 l.pageno y0
3851 else (
3852 let d = fstate.fontsize + 1 in
3853 if y1 - l.pagey > l.pagevh - d
3854 then gotopage1 l.pageno (y1 - state.winh + d)
3855 else postRedisplay "linknav";
3857 showlinktype (getlink opaque m);
3858 state.mode <- LinkNav (Ltexact (l.pageno, m));
3861 | None -> viewkeyboard key mask
3862 end;
3863 | _ -> viewkeyboard key mask
3865 if pv = Keys.Insert
3866 then (
3867 begin match linknav with
3868 | Ltexact pa -> state.lnava <- Some pa
3869 | Ltgendir _ | Ltnotready _ -> ()
3870 end;
3871 state.mode <- View;
3872 postRedisplay "leave linknav"
3874 else
3875 match linknav with
3876 | Ltgendir _ | Ltnotready _ -> viewkeyboard key mask
3877 | Ltexact exact -> doexact exact
3880 let keyboard key mask =
3881 if (key = Char.code 'g' && Wsi.withctrl mask) && not (istextentry state.mode)
3882 then wcmd "interrupt"
3883 else state.uioh <- state.uioh#key key mask
3886 let birdseyekeyboard key mask
3887 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
3888 let incr =
3889 match conf.columns with
3890 | Csingle _ -> 1
3891 | Cmulti ((c, _, _), _) -> c
3892 | Csplit _ -> error "bird's eye split mode"
3894 let pgh layout = List.fold_left
3895 (fun m l -> max l.pageh m) state.winh layout in
3896 let open Keys in
3897 match Wsi.kc2kt key with
3898 | Ascii 'l' when Wsi.withctrl mask ->
3899 let y, h = getpageyh pageno in
3900 let top = (state.winh - h) / 2 in
3901 gotoxy state.x (max 0 (y - top))
3902 | Enter -> leavebirdseye beye false
3903 | Escape -> leavebirdseye beye true
3904 | Up -> upbirdseye incr beye
3905 | Down -> downbirdseye incr beye
3906 | Left -> upbirdseye 1 beye
3907 | Right -> downbirdseye 1 beye
3909 | Prior ->
3910 begin match state.layout with
3911 | l :: _ ->
3912 if l.pagey != 0
3913 then (
3914 state.mode <- Birdseye (
3915 oconf, leftx, l.pageno, hooverpageno, anchor
3917 gotopage1 l.pageno 0;
3919 else (
3920 let layout = layout state.x (state.y-state.winh)
3921 state.winw
3922 (pgh state.layout) in
3923 match layout with
3924 | [] -> gotoxy state.x (clamp (-state.winh))
3925 | l :: _ ->
3926 state.mode <- Birdseye (
3927 oconf, leftx, l.pageno, hooverpageno, anchor
3929 gotopage1 l.pageno 0
3932 | [] -> gotoxy state.x (clamp (-state.winh))
3933 end;
3935 | Next ->
3936 begin match List.rev state.layout with
3937 | l :: _ ->
3938 let layout = layout state.x
3939 (state.y + (pgh state.layout))
3940 state.winw state.winh in
3941 begin match layout with
3942 | [] ->
3943 let incr = l.pageh - l.pagevh in
3944 if incr = 0
3945 then (
3946 state.mode <-
3947 Birdseye (
3948 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
3950 postRedisplay "birdseye pagedown";
3952 else gotoxy state.x (clamp (incr + conf.interpagespace*2));
3954 | l :: _ ->
3955 state.mode <-
3956 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
3957 gotopage1 l.pageno 0;
3960 | [] -> gotoxy state.x (clamp state.winh)
3961 end;
3963 | Home ->
3964 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
3965 gotopage1 0 0
3967 | End ->
3968 let pageno = state.pagecount - 1 in
3969 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
3970 if not (pagevisible state.layout pageno)
3971 then
3972 let h =
3973 match List.rev state.pdims with
3974 | [] -> state.winh
3975 | (_, _, h, _) :: _ -> h
3977 gotoxy
3978 state.x
3979 (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
3980 else postRedisplay "birdseye end";
3982 | Delete|Insert|Ascii _|Code _|Ctrl _|Fn _|Backspace -> viewkeyboard key mask
3985 let drawpage l =
3986 let color =
3987 match state.mode with
3988 | Textentry _ -> scalecolor 0.4
3989 | LinkNav _ | View -> scalecolor 1.0
3990 | Birdseye (_, _, pageno, hooverpageno, _) ->
3991 if l.pageno = hooverpageno
3992 then scalecolor 0.9
3993 else (
3994 if l.pageno = pageno
3995 then (
3996 let c = scalecolor 1.0 in
3997 GlDraw.color c;
3998 GlDraw.line_width 3.0;
3999 let dispx = l.pagedispx in
4000 linerect
4001 (float (dispx-1)) (float (l.pagedispy-1))
4002 (float (dispx+l.pagevw+1))
4003 (float (l.pagedispy+l.pagevh+1));
4004 GlDraw.line_width 1.0;
4007 else scalecolor 0.8
4010 drawtiles l color;
4013 let postdrawpage l linkindexbase =
4014 match getopaque l.pageno with
4015 | Some opaque ->
4016 if tileready l l.pagex l.pagey
4017 then
4018 let x = l.pagedispx - l.pagex
4019 and y = l.pagedispy - l.pagey in
4020 let hlmask =
4021 match conf.columns with
4022 | Csingle _ | Cmulti _ ->
4023 (if conf.hlinks then 1 else 0)
4024 + (if state.glinks
4025 && not (isbirdseye state.mode) then 2 else 0)
4026 | Csplit _ -> 0
4028 let s =
4029 match state.mode with
4030 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
4031 | Textentry _
4032 | Birdseye _
4033 | View
4034 | LinkNav _ -> E.s
4036 Hashtbl.find_all state.prects l.pageno |>
4037 List.iter (fun vals -> drawprect opaque x y vals);
4038 let n = postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize) in
4039 if n < 0
4040 then (Glutils.redisplay := true; 0)
4041 else n
4042 else 0
4043 | _ -> 0
4046 let scrollindicator () =
4047 let sbw, ph, sh = state.uioh#scrollph in
4048 let sbh, pw, sw = state.uioh#scrollpw in
4050 let x0,x1,hx0 =
4051 if conf.leftscroll
4052 then (0, sbw, sbw)
4053 else ((state.winw - sbw), state.winw, 0)
4056 Gl.enable `blend;
4057 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
4058 let (r, g, b, alpha) = conf.sbarcolor in
4059 GlDraw.color (r, g, b) ~alpha;
4060 filledrect (float x0) 0. (float x1) (float state.winh);
4061 filledrect
4062 (float hx0) (float (state.winh - sbh))
4063 (float (hx0 + state.winw)) (float state.winh);
4064 let (r, g, b, alpha) = conf.sbarhndlcolor in
4065 GlDraw.color (r, g, b) ~alpha;
4067 filledrect (float x0) ph (float x1) (ph +. sh);
4068 let pw = pw +. float hx0 in
4069 filledrect pw (float (state.winh - sbh)) (pw +. sw) (float state.winh);
4070 Gl.disable `blend;
4073 let showsel () =
4074 match state.mstate with
4075 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ -> ()
4076 | Msel ((x0, y0), (x1, y1)) ->
4077 let identify opaque l px py = Some (opaque, l.pageno, px, py) in
4078 let o0,n0,px0,py0 = onppundermouse identify x0 y0 (~< E.s, -1, 0, 0) in
4079 let _o1,n1,px1,py1 = onppundermouse identify x1 y1 (~< E.s, -1, 0, 0) in
4080 if n0 != -1 && n0 = n1 then seltext o0 (px0, py0, px1, py1);
4083 let showrects = function
4084 | [] -> ()
4085 | rects ->
4086 Gl.enable `blend;
4087 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
4088 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
4089 List.iter
4090 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
4091 List.iter (fun l ->
4092 if l.pageno = pageno
4093 then
4094 let dx = float (l.pagedispx - l.pagex) in
4095 let dy = float (l.pagedispy - l.pagey) in
4096 let r, g, b, alpha = c in
4097 GlDraw.color (r, g, b) ~alpha;
4098 filledrect2
4099 (x0+.dx) (y0+.dy)
4100 (x1+.dx) (y1+.dy)
4101 (x3+.dx) (y3+.dy)
4102 (x2+.dx) (y2+.dy);
4103 ) state.layout
4104 ) rects;
4105 Gl.disable `blend;
4108 let display () =
4109 GlDraw.color (scalecolor2 conf.bgcolor);
4110 GlClear.color (scalecolor2 conf.bgcolor);
4111 GlClear.clear [`color];
4112 List.iter drawpage state.layout;
4113 let rects =
4114 match state.mode with
4115 | LinkNav (Ltexact (pageno, linkno)) ->
4116 begin match getopaque pageno with
4117 | Some opaque ->
4118 let x0, y0, x1, y1 = getlinkrect opaque linkno in
4119 let color = (0.0, 0.0, 0.5, 0.5) in
4120 (pageno, color,
4121 (float x0, float y0,
4122 float x1, float y0,
4123 float x1, float y1,
4124 float x0, float y1)
4125 ) :: state.rects
4126 | None -> state.rects
4128 | LinkNav (Ltgendir _) | LinkNav (Ltnotready _)
4129 | Birdseye _
4130 | Textentry _
4131 | View -> state.rects
4133 showrects rects;
4134 let rec postloop linkindexbase = function
4135 | l :: rest ->
4136 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
4137 postloop linkindexbase rest
4138 | [] -> ()
4140 showsel ();
4141 postloop 0 state.layout;
4142 state.uioh#display;
4143 begin match state.mstate with
4144 | Mzoomrect ((x0, y0), (x1, y1)) ->
4145 Gl.enable `blend;
4146 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
4147 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
4148 filledrect (float x0) (float y0) (float x1) (float y1);
4149 Gl.disable `blend;
4150 | Msel _
4151 | Mpan _
4152 | Mscrolly | Mscrollx
4153 | Mzoom _
4154 | Mnone -> ()
4155 end;
4156 enttext ();
4157 scrollindicator ();
4158 Wsi.swapb ();
4161 let zoomrect x y x1 y1 =
4162 let x0 = min x x1
4163 and x1 = max x x1
4164 and y0 = min y y1 in
4165 let zoom = (float state.w) /. float (x1 - x0) in
4166 let margin =
4167 let simple () =
4168 if state.w < state.winw
4169 then (state.winw - state.w) / 2
4170 else 0
4172 match conf.fitmodel with
4173 | FitWidth | FitProportional -> simple ()
4174 | FitPage ->
4175 match conf.columns with
4176 | Csplit _ ->
4177 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
4178 | Cmulti _ | Csingle _ -> simple ()
4180 gotoxy ((state.x + margin) - x0) (state.y + y0);
4181 state.anchor <- getanchor ();
4182 setzoom zoom;
4183 resetmstate ();
4186 let annot inline x y =
4187 match unproject x y with
4188 | Some (opaque, n, ux, uy) ->
4189 let add text =
4190 addannot opaque ux uy text;
4191 wcmd "freepage %s" (~> opaque);
4192 Hashtbl.remove state.pagemap (n, state.gen);
4193 flushtiles ();
4194 gotoxy state.x state.y
4196 if inline
4197 then
4198 let ondone s = add s in
4199 let mode = state.mode in
4200 state.mode <- Textentry (
4201 ("annotation: ", E.s, None, textentry, ondone, true),
4202 fun _ -> state.mode <- mode);
4203 state.text <- E.s;
4204 enttext ();
4205 postRedisplay "annot"
4206 else add @@ getusertext E.s
4207 | _ -> ()
4210 let zoomblock x y =
4211 let g opaque l px py =
4212 match rectofblock opaque px py with
4213 | Some a ->
4214 let x0 = a.(0) -. 20. in
4215 let x1 = a.(1) +. 20. in
4216 let y0 = a.(2) -. 20. in
4217 let zoom = (float state.w) /. (x1 -. x0) in
4218 let pagey = getpagey l.pageno in
4219 let margin = (state.w - l.pagew)/2 in
4220 let nx = -truncate x0 - margin in
4221 gotoxy nx (pagey + truncate y0);
4222 state.anchor <- getanchor ();
4223 setzoom zoom;
4224 None
4225 | None -> None
4227 match conf.columns with
4228 | Csplit _ ->
4229 impmsg "block zooming does not work properly in split columns mode"
4230 | Cmulti _ | Csingle _ -> onppundermouse g x y ()
4233 let scrollx x =
4234 let winw = state.winw - 1 in
4235 let s = float x /. float winw in
4236 let destx = truncate (float (state.w + winw) *. s) in
4237 gotoxy (winw - destx) state.y;
4238 state.mstate <- Mscrollx;
4241 let scrolly y =
4242 let s = float y /. float state.winh in
4243 let desty = truncate (s *. float (maxy ())) in
4244 gotoxy state.x desty;
4245 state.mstate <- Mscrolly;
4248 let viewmulticlick clicks x y mask =
4249 let g opaque l px py =
4250 let mark =
4251 match clicks with
4252 | 2 -> Mark_word
4253 | 3 -> Mark_line
4254 | 4 -> Mark_block
4255 | _ -> Mark_page
4257 if markunder opaque px py mark
4258 then (
4259 Some (fun () ->
4260 let dopipe cmd =
4261 match getopaque l.pageno with
4262 | None -> ()
4263 | Some opaque -> pipesel opaque cmd
4265 state.roam <- (fun () -> dopipe conf.paxcmd);
4266 if not (Wsi.withctrl mask) then dopipe conf.selcmd;
4269 else None
4271 postRedisplay "viewmulticlick";
4272 onppundermouse g x y (fun () -> impmsg "nothing to select") ();
4275 let canselect () =
4276 match conf.columns with
4277 | Csplit _ -> false
4278 | Csingle _ | Cmulti _ -> conf.angle mod 360 = 0
4281 let viewmouse button down x y mask =
4282 match button with
4283 | n when (n == 4 || n == 5) && not down ->
4284 if Wsi.withctrl mask
4285 then (
4286 let incr =
4287 if n = 5
4288 then if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
4289 else if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
4291 let fx, fy =
4292 match state.mstate with
4293 | Mzoom (oldn, _, pos) when n = oldn -> pos
4294 | Mzoomrect _ | Mnone | Mpan _
4295 | Msel _ | Mscrollx | Mscrolly | Mzoom _ -> (x, y)
4297 let zoom = conf.zoom -. incr in
4298 state.mstate <- Mzoom (n, 0, (x, y));
4299 if false && abs (fx - x) > 5 || abs (fy - y) > 5
4300 then pivotzoom ~x ~y zoom
4301 else pivotzoom zoom
4303 else (
4304 match state.autoscroll with
4305 | Some step -> setautoscrollspeed step (n=4)
4306 | None ->
4307 if conf.wheelbypage || conf.presentation
4308 then (
4309 if n = 4
4310 then prevpage ()
4311 else nextpage ()
4313 else
4314 let incr = if n = 4 then -conf.scrollstep else conf.scrollstep in
4315 let incr = incr * 2 in
4316 let y = clamp incr in
4317 gotoxy state.x y
4320 | n when (n = 6 || n = 7) && not down && canpan () ->
4321 let x =
4322 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep) in
4323 gotoxy x state.y
4325 | 1 when Wsi.withshift mask ->
4326 state.mstate <- Mnone;
4327 if not down
4328 then (
4329 match unproject x y with
4330 | None -> ()
4331 | Some (_, pageno, ux, uy) ->
4332 let cmd = Printf.sprintf
4333 "%s %s %d %d %d"
4334 conf.stcmd state.path pageno ux uy
4336 match spawn cmd [] with
4337 | exception exn ->
4338 impmsg "execution of synctex command(%S) failed: %S"
4339 conf.stcmd @@ exntos exn
4340 | _pid -> ()
4343 | 1 when Wsi.withctrl mask ->
4344 if down
4345 then (
4346 Wsi.setcursor Wsi.CURSOR_FLEUR;
4347 state.mstate <- Mpan (x, y)
4349 else state.mstate <- Mnone
4351 | 3 ->
4352 if down
4353 then (
4354 if Wsi.withshift mask
4355 then (
4356 annot conf.annotinline x y;
4357 postRedisplay "addannot"
4359 else
4360 let p = (x, y) in
4361 Wsi.setcursor Wsi.CURSOR_CYCLE;
4362 state.mstate <- Mzoomrect (p, p)
4364 else (
4365 match state.mstate with
4366 | Mzoomrect ((x0, y0), _) ->
4367 if abs (x-x0) > 10 && abs (y - y0) > 10
4368 then zoomrect x0 y0 x y
4369 else (
4370 resetmstate ();
4371 postRedisplay "kill accidental zoom rect";
4373 | Msel _
4374 | Mpan _
4375 | Mscrolly | Mscrollx
4376 | Mzoom _
4377 | Mnone -> resetmstate ()
4380 | 1 when vscrollhit x ->
4381 if down
4382 then
4383 let _, position, sh = state.uioh#scrollph in
4384 if y > truncate position && y < truncate (position +. sh)
4385 then state.mstate <- Mscrolly
4386 else scrolly y
4387 else state.mstate <- Mnone
4389 | 1 when y > state.winh - hscrollh () ->
4390 if down
4391 then
4392 let _, position, sw = state.uioh#scrollpw in
4393 if x > truncate position && x < truncate (position +. sw)
4394 then state.mstate <- Mscrollx
4395 else scrollx x
4396 else state.mstate <- Mnone
4398 | 1 when state.bzoom -> if not down then zoomblock x y
4400 | 1 ->
4401 let dest = if down then getunder x y else Unone in
4402 begin match dest with
4403 | Ulinkuri _ -> gotounder dest
4404 | Unone when down ->
4405 Wsi.setcursor Wsi.CURSOR_FLEUR;
4406 state.mstate <- Mpan (x, y);
4407 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
4408 | Unone | Utext _ ->
4409 if down
4410 then (
4411 if canselect ()
4412 then (
4413 state.mstate <- Msel ((x, y), (x, y));
4414 postRedisplay "mouse select";
4417 else (
4418 match state.mstate with
4419 | Mnone -> ()
4420 | Mzoom _ | Mscrollx | Mscrolly -> state.mstate <- Mnone
4421 | Mzoomrect ((x0, y0), _) -> zoomrect x0 y0 x y
4422 | Mpan _ ->
4423 Wsi.setcursor Wsi.CURSOR_INHERIT;
4424 state.mstate <- Mnone
4425 | Msel ((x0, y0), (x1, y1)) ->
4426 let rec loop = function
4427 | [] -> ()
4428 | l :: rest ->
4429 let inside =
4430 let a0 = l.pagedispy in
4431 let a1 = a0 + l.pagevh in
4432 let b0 = l.pagedispx in
4433 let b1 = b0 + l.pagevw in
4434 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
4435 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
4437 if inside
4438 then
4439 match getopaque l.pageno with
4440 | Some opaque ->
4441 let dosel cmd () =
4442 pipef ~closew:false "Msel"
4443 (fun w ->
4444 copysel w opaque;
4445 postRedisplay "Msel") cmd
4447 dosel conf.selcmd ();
4448 state.roam <- dosel conf.paxcmd;
4449 | None -> ()
4450 else loop rest
4452 loop state.layout;
4453 resetmstate ();
4456 | _ -> ()
4459 let birdseyemouse button down x y mask
4460 (conf, leftx, _, hooverpageno, anchor) =
4461 match button with
4462 | 1 when down ->
4463 let rec loop = function
4464 | [] -> ()
4465 | l :: rest ->
4466 if y > l.pagedispy && y < l.pagedispy + l.pagevh
4467 && x > l.pagedispx && x < l.pagedispx + l.pagevw
4468 then
4469 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false
4470 else loop rest
4472 loop state.layout
4473 | 3 -> ()
4474 | _ -> viewmouse button down x y mask
4477 let uioh = object
4478 method display = ()
4480 method key key mask =
4481 begin match state.mode with
4482 | Textentry textentry -> textentrykeyboard key mask textentry
4483 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
4484 | View -> viewkeyboard key mask
4485 | LinkNav linknav -> linknavkeyboard key mask linknav
4486 end;
4487 state.uioh
4489 method button button bstate x y mask =
4490 begin match state.mode with
4491 | LinkNav _ | View -> viewmouse button bstate x y mask
4492 | Birdseye beye -> birdseyemouse button bstate x y mask beye
4493 | Textentry _ -> ()
4494 end;
4495 state.uioh
4497 method multiclick clicks x y mask =
4498 begin match state.mode with
4499 | LinkNav _ | View -> viewmulticlick clicks x y mask
4500 | Birdseye _ | Textentry _ -> ()
4501 end;
4502 state.uioh
4504 method motion x y =
4505 begin match state.mode with
4506 | Textentry _ -> ()
4507 | View | Birdseye _ | LinkNav _ ->
4508 match state.mstate with
4509 | Mzoom _ | Mnone -> ()
4510 | Mpan (x0, y0) ->
4511 let dx = x - x0
4512 and dy = y0 - y in
4513 state.mstate <- Mpan (x, y);
4514 let x = if canpan () then panbound (state.x + dx) else state.x in
4515 let y = clamp dy in
4516 gotoxy x y
4518 | Msel (a, _) ->
4519 state.mstate <- Msel (a, (x, y));
4520 postRedisplay "motion select";
4522 | Mscrolly ->
4523 let y = min state.winh (max 0 y) in
4524 scrolly y
4526 | Mscrollx ->
4527 let x = min state.winw (max 0 x) in
4528 scrollx x
4530 | Mzoomrect (p0, _) ->
4531 state.mstate <- Mzoomrect (p0, (x, y));
4532 postRedisplay "motion zoomrect";
4533 end;
4534 state.uioh
4536 method pmotion x y =
4537 begin match state.mode with
4538 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
4539 let rec loop = function
4540 | [] ->
4541 if hooverpageno != -1
4542 then (
4543 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
4544 postRedisplay "pmotion birdseye no hoover";
4546 | l :: rest ->
4547 if y > l.pagedispy && y < l.pagedispy + l.pagevh
4548 && x > l.pagedispx && x < l.pagedispx + l.pagevw
4549 then (
4550 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
4551 postRedisplay "pmotion birdseye hoover";
4553 else loop rest
4555 loop state.layout
4557 | Textentry _ -> ()
4559 | LinkNav _ | View ->
4560 match state.mstate with
4561 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ -> ()
4562 | Mnone ->
4563 updateunder x y;
4564 if canselect ()
4565 then
4566 match conf.pax with
4567 | None -> ()
4568 | Some past ->
4569 let now = now () in
4570 let delta = now -. past in
4571 if delta > 0.01
4572 then paxunder x y
4573 else conf.pax <- Some now
4574 end;
4575 state.uioh
4577 method infochanged _ = ()
4579 method scrollph =
4580 let maxy = maxy () in
4581 let p, h =
4582 if maxy = 0
4583 then 0.0, float state.winh
4584 else scrollph state.y maxy
4586 vscrollw (), p, h
4588 method scrollpw =
4589 let fwinw = float (state.winw - vscrollw ()) in
4590 let sw =
4591 let sw = fwinw /. float state.w in
4592 let sw = fwinw *. sw in
4593 max sw (float conf.scrollh)
4595 let position =
4596 let maxx = state.w + state.winw in
4597 let x = state.winw - state.x in
4598 let percent = float x /. float maxx in
4599 (fwinw -. sw) *. percent
4601 hscrollh (), position, sw
4603 method modehash =
4604 let modename =
4605 match state.mode with
4606 | LinkNav _ -> "links"
4607 | Textentry _ -> "textentry"
4608 | Birdseye _ -> "birdseye"
4609 | View -> "view"
4611 findkeyhash conf modename
4613 method eformsgs = true
4614 method alwaysscrolly = false
4615 method scroll dx dy =
4616 let x = if canpan () then panbound (state.x + dx) else state.x in
4617 gotoxy x (clamp (2 * dy));
4618 state.uioh
4619 method zoom z x y =
4620 pivotzoom ~x ~y (conf.zoom *. exp z);
4621 end;;
4623 let addrect pageno r g b a x0 y0 x1 y1 =
4624 Hashtbl.add state.prects pageno [|r; g; b; a; x0; y0; x1; y1|];
4627 let ract cmds =
4628 let cl = splitatchar cmds ' ' in
4629 let scan s fmt f =
4630 try Scanf.sscanf s fmt f
4631 with exn -> adderrfmt "remote exec" "error processing '%S': %s\n"
4632 cmds @@ exntos exn
4634 let rectx s pageno (r, g, b, a) x0 y0 x1 y1 =
4635 vlog "%s page %d color (%f %f %f %f) x0,y0,x1,y1 = %f %f %f %f"
4636 s pageno r g b a x0 y0 x1 y1;
4637 onpagerect
4638 pageno
4639 (fun w h ->
4640 let _,w1,h1,_ = getpagedim pageno in
4641 let sw = float w1 /. float w
4642 and sh = float h1 /. float h in
4643 let x0s = x0 *. sw
4644 and x1s = x1 *. sw
4645 and y0s = y0 *. sh
4646 and y1s = y1 *. sh in
4647 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
4648 let color = (r, g, b, a) in
4649 if conf.verbose then debugrect rect;
4650 state.rects <- (pageno, color, rect) :: state.rects;
4651 postRedisplay s;
4654 match cl with
4655 | "reload", "" -> reload ()
4656 | "goto", args ->
4657 scan args "%u %f %f"
4658 (fun pageno x y ->
4659 let cmd, _ = state.geomcmds in
4660 if emptystr cmd
4661 then gotopagexy pageno x y
4662 else
4663 let f prevf () =
4664 gotopagexy pageno x y;
4665 prevf ()
4667 state.reprf <- f state.reprf
4669 | "goto1", args -> scan args "%u %f" gotopage
4670 | "gotor", args -> scan args "%S" gotoremote
4671 | "rect", args ->
4672 scan args "%u %u %f %f %f %f"
4673 (fun pageno c x0 y0 x1 y1 ->
4674 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
4675 rectx "rect" pageno color x0 y0 x1 y1;
4677 | "prect", args ->
4678 scan args "%u %f %f %f %f %f %f %f %f"
4679 (fun pageno r g b alpha x0 y0 x1 y1 ->
4680 addrect pageno r g b alpha x0 y0 x1 y1;
4681 postRedisplay "prect"
4683 | "pgoto", args ->
4684 scan args "%u %f %f"
4685 (fun pageno x y ->
4686 let optopaque =
4687 match getopaque pageno with
4688 | Some opaque -> opaque
4689 | None -> ~< E.s
4691 pgoto optopaque pageno x y;
4692 let rec fixx = function
4693 | [] -> ()
4694 | l :: rest ->
4695 if l.pageno = pageno
4696 then gotoxy (state.x - l.pagedispx) state.y
4697 else fixx rest
4699 let layout =
4700 let mult =
4701 match conf.columns with
4702 | Csingle _ | Csplit _ -> 1
4703 | Cmulti ((n, _, _), _) -> n
4705 layout 0 state.y (state.winw * mult) state.winh
4707 fixx layout
4709 | "activatewin", "" -> Wsi.activatewin ()
4710 | "quit", "" -> raise Quit
4711 | "keys", keys ->
4712 begin try
4713 let l = Config.keys_of_string keys in
4714 List.iter (fun (k, m) -> keyboard k m) l
4715 with exn -> adderrfmt "error processing keys" "`%S': %s\n"
4716 cmds @@ exntos exn
4718 | "clearrects", "" ->
4719 Hashtbl.clear state.prects;
4720 postRedisplay "clearrects"
4721 | _ ->
4722 adderrfmt "remote command"
4723 "error processing remote command: %S\n" cmds;
4726 let remote =
4727 let scratch = Bytes.create 80 in
4728 let buf = Buffer.create 80 in
4729 fun fd ->
4730 match tempfailureretry (Unix.read fd scratch 0) 80 with
4731 | exception Unix.Unix_error (Unix.EAGAIN, _, _) -> None
4732 | 0 ->
4733 Unix.close fd;
4734 if Buffer.length buf > 0
4735 then (
4736 let s = Buffer.contents buf in
4737 Buffer.clear buf;
4738 ract s;
4740 None
4741 | n ->
4742 let rec eat ppos =
4743 let nlpos =
4744 match Bytes.index_from scratch ppos '\n' with
4745 | pos -> if pos >= n then -1 else pos
4746 | exception Not_found -> -1
4748 if nlpos >= 0
4749 then (
4750 Buffer.add_subbytes buf scratch ppos (nlpos-ppos);
4751 let s = Buffer.contents buf in
4752 Buffer.clear buf;
4753 ract s;
4754 eat (nlpos+1);
4756 else (
4757 Buffer.add_subbytes buf scratch ppos (n-ppos);
4758 Some fd
4760 in eat 0
4763 let remoteopen path =
4764 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
4765 with exn ->
4766 adderrfmt "remoteopen" "error opening %S: %s" path @@ exntos exn;
4767 None
4770 let () =
4771 let gcconfig = ref false in
4772 let trimcachepath = ref E.s in
4773 let rcmdpath = ref E.s in
4774 let pageno = ref None in
4775 let openlast = ref false in
4776 let doreap = ref false in
4777 let csspath = ref None in
4778 selfexec := Sys.executable_name;
4779 Arg.parse
4780 (Arg.align
4781 [("-p", Arg.String (fun s -> state.password <- s),
4782 "<password> Set password");
4784 ("-f", Arg.String
4785 (fun s ->
4786 Config.fontpath := s;
4787 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
4789 "<path> Set path to the user interface font");
4791 ("-c", Arg.String
4792 (fun s ->
4793 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
4794 Config.confpath := s),
4795 "<path> Set path to the configuration file");
4797 ("-last", Arg.Set openlast, " Open last document");
4799 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
4800 "<page-number> Jump to page");
4802 ("-tcf", Arg.String (fun s -> trimcachepath := s),
4803 "<path> Set path to the trim cache file");
4805 ("-dest", Arg.String (fun s -> state.nameddest <- s),
4806 "<named-destination> Set named destination");
4808 ("-remote", Arg.String (fun s -> rcmdpath := s),
4809 "<path> Set path to the source of remote commands");
4811 ("-gc", Arg.Set gcconfig, " Collect config garbage");
4813 ("-v", Arg.Unit (fun () ->
4814 Printf.printf
4815 "%s\nconfiguration file: %s\n"
4816 (Help.version ())
4817 Config.defconfpath;
4818 exit 0), " Print version and exit");
4820 ("-css", Arg.String (fun s -> csspath := Some s),
4821 "<path> Set path to the style sheet to use with EPUB/HTML");
4823 ("-origin", Arg.String (fun s -> state.origin <- s),
4824 "<origin> <undocumented>");
4826 ("-no-title", Arg.Set ignoredoctitlte, " ignore document title");
4827 ("-layout-height", Arg.Set_int layouth,
4828 "<height> layout height html/epub/etc (-1, 0, N)");
4831 (fun s -> state.path <- s)
4832 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:");
4834 let histmode = emptystr state.path && not !openlast in
4836 if not (Config.load !openlast)
4837 then dolog "failed to load configuration";
4839 begin match !pageno with
4840 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
4841 | None -> ()
4842 end;
4844 fillhelp ();
4845 if !gcconfig
4846 then (
4847 Config.gc ();
4848 exit 0
4851 let mu =
4852 object (self)
4853 val mutable m_clicks = 0
4854 val mutable m_click_x = 0
4855 val mutable m_click_y = 0
4856 val mutable m_lastclicktime = infinity
4858 method private cleanup =
4859 state.roam <- noroam;
4860 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap
4861 method expose = postRedisplay "expose"
4862 method visible v =
4863 let name =
4864 match v with
4865 | Wsi.Unobscured -> "unobscured"
4866 | Wsi.PartiallyObscured -> "partiallyobscured"
4867 | Wsi.FullyObscured -> "fullyobscured"
4869 vlog "visibility change %s" name
4870 method display = display ()
4871 method map mapped = vlog "mapped %b" mapped
4872 method reshape w h =
4873 self#cleanup;
4874 reshape w h
4875 method mouse b d x y m =
4876 if d && canselect ()
4877 then (
4879 * http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx
4881 m_click_x <- x;
4882 m_click_y <- y;
4883 if b = 1
4884 then (
4885 let t = now () in
4886 if abs x - m_click_x > 10
4887 || abs y - m_click_y > 10
4888 || abs_float (t -. m_lastclicktime) > 0.3
4889 then m_clicks <- 0;
4890 m_clicks <- m_clicks + 1;
4891 m_lastclicktime <- t;
4892 if m_clicks = 1
4893 then (
4894 self#cleanup;
4895 postRedisplay "cleanup";
4896 state.uioh <- state.uioh#button b d x y m;
4898 else state.uioh <- state.uioh#multiclick m_clicks x y m
4900 else (
4901 self#cleanup;
4902 m_clicks <- 0;
4903 m_lastclicktime <- infinity;
4904 state.uioh <- state.uioh#button b d x y m
4907 else state.uioh <- state.uioh#button b d x y m
4908 method motion x y =
4909 state.mpos <- (x, y);
4910 state.uioh <- state.uioh#motion x y
4911 method pmotion x y =
4912 state.mpos <- (x, y);
4913 state.uioh <- state.uioh#pmotion x y
4914 method key k m =
4915 vlog "k=%#x m=%#x" k m;
4916 let mascm = m land (
4917 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
4918 ) in
4919 let keyboard k m =
4920 let x = state.x and y = state.y in
4921 keyboard k m;
4922 if x != state.x || y != state.y then self#cleanup
4924 match state.keystate with
4925 | KSnone ->
4926 let km = k, mascm in
4927 begin
4928 match
4929 let modehash = state.uioh#modehash in
4930 try Hashtbl.find modehash km
4931 with Not_found ->
4932 try Hashtbl.find (findkeyhash conf "global") km
4933 with Not_found -> KMinsrt (k, m)
4934 with
4935 | KMinsrt (k, m) -> keyboard k m
4936 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
4937 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
4939 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
4940 List.iter (fun (k, m) -> keyboard k m) insrt;
4941 state.keystate <- KSnone
4942 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
4943 state.keystate <- KSinto (keys, insrt)
4944 | KSinto _ -> state.keystate <- KSnone
4946 method enter x y =
4947 state.mpos <- (x, y);
4948 state.uioh <- state.uioh#pmotion x y
4949 method leave = state.mpos <- (-1, -1)
4950 method winstate wsl = state.winstate <- wsl
4951 method quit : 'a. 'a = raise Quit
4952 method scroll dx dy = state.uioh <- state.uioh#scroll dx dy
4953 method zoom z x y = state.uioh#zoom z x y
4954 method opendoc path =
4955 state.mode <- View;
4956 state.uioh <- uioh;
4957 postRedisplay "opendoc";
4958 opendoc path state.password
4961 let wsfd, winw, winh = Wsi.init mu conf.cwinw conf.cwinh platform in
4962 state.wsfd <- wsfd;
4964 if not @@ List.exists GlMisc.check_extension
4965 [ "GL_ARB_texture_rectangle"
4966 ; "GL_EXT_texture_recangle"
4967 ; "GL_NV_texture_rectangle" ]
4968 then (dolog "OpenGL does not suppport rectangular textures"; exit 1);
4970 let cs, ss =
4971 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
4972 | exception exn ->
4973 dolog "socketpair failed: %s" @@ exntos exn;
4974 exit 1
4975 | (r, w) ->
4976 cloexec r;
4977 cloexec w;
4978 r, w
4981 setcheckers conf.checkers;
4982 begin match !csspath with
4983 | None -> ()
4984 | Some "" -> conf.css <- E.s
4985 | Some path ->
4986 let css = filecontents path in
4987 let l = String.length css in
4988 conf.css <-
4989 if substratis css (l-2) "\r\n"
4990 then String.sub css 0 (l-2)
4991 else (if css.[l-1] = '\n' then String.sub css 0 (l-1) else css)
4992 end;
4993 init cs (
4994 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
4995 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
4996 !Config.fontpath, !trimcachepath
4998 List.iter GlArray.enable [`texture_coord; `vertex];
4999 state.ss <- ss;
5000 reshape ~firsttime:true winw winh;
5001 state.uioh <- uioh;
5002 if histmode
5003 then (
5004 Wsi.settitle "llpp (history)";
5005 enterhistmode ();
5007 else (
5008 state.text <- "Opening " ^ (mbtoutf8 state.path);
5009 opendoc state.path state.password;
5011 display ();
5012 Wsi.mapwin ();
5013 Wsi.setcursor Wsi.CURSOR_INHERIT;
5014 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
5016 let rec reap () =
5017 match Unix.waitpid [Unix.WNOHANG] ~-1 with
5018 | exception (Unix.Unix_error (Unix.ECHILD, _, _)) -> ()
5019 | exception exn -> dolog "Unix.waitpid: %s" @@ exntos exn
5020 | 0, _ -> ()
5021 | _pid, _status -> reap ()
5023 Sys.set_signal Sys.sigchld (Sys.Signal_handle (fun _ -> doreap := true));
5025 let optrfd =
5026 ref (if nonemptystr !rcmdpath then remoteopen !rcmdpath else None)
5029 let rec loop deadline =
5030 if !doreap
5031 then (
5032 doreap := false;
5033 reap ()
5035 let r = [state.ss; state.wsfd] in
5036 let r =
5037 match !optrfd with
5038 | None -> r
5039 | Some fd -> fd :: r
5041 if !redisplay
5042 then (
5043 Glutils.redisplay := false;
5044 display ();
5046 let timeout =
5047 let now = now () in
5048 if deadline > now
5049 then (
5050 if deadline = infinity
5051 then ~-.1.0
5052 else max 0.0 (deadline -. now)
5054 else 0.0
5056 let r, _, _ =
5057 try Unix.select r [] [] timeout
5058 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
5060 begin match r with
5061 | [] ->
5062 let newdeadline =
5063 match state.autoscroll with
5064 | Some step when step != 0 ->
5065 if state.slideshow land 1 = 1
5066 then (
5067 if state.slideshow land 2 = 0
5068 then state.slideshow <- state.slideshow lor 2
5069 else if step < 0 then prevpage () else nextpage ();
5070 deadline +. (float (abs step))
5072 else
5073 let y = state.y + step in
5074 let fy = if conf.maxhfit then state.winh else 0 in
5075 let y =
5076 if y < 0
5077 then state.maxy - fy
5078 else if y >= state.maxy - fy then 0 else y
5080 gotoxy state.x y;
5081 deadline +. 0.01
5082 | _ -> infinity
5084 loop newdeadline
5086 | l ->
5087 let rec checkfds = function
5088 | [] -> ()
5089 | fd :: rest when fd = state.ss ->
5090 let cmd = rcmd state.ss in
5091 act cmd;
5092 checkfds rest
5094 | fd :: rest when fd = state.wsfd ->
5095 Wsi.readresp fd;
5096 checkfds rest
5098 | fd :: rest when Some fd = !optrfd ->
5099 begin match remote fd with
5100 | None -> optrfd := remoteopen !rcmdpath;
5101 | opt -> optrfd := opt
5102 end;
5103 checkfds rest
5105 | _ :: rest ->
5106 dolog "select returned unknown descriptor";
5107 checkfds rest
5109 checkfds l;
5110 let newdeadline =
5111 let deadline1 =
5112 if deadline = infinity
5113 then now () +. 0.01
5114 else deadline
5116 match state.autoscroll with
5117 | Some step when step != 0 -> deadline1
5118 | _ -> infinity
5120 loop newdeadline
5121 end;
5123 match loop infinity with
5124 | exception Quit ->
5125 Config.save leavebirdseye;
5126 if hasunsavedchanges ()
5127 then save ()
5128 | _ -> error "umpossible - infinity reached"