Respect coarse positioning setting
[llpp.git] / main.ml
blob7c10fcb8dcafa295f437f2e79ea70582b773ffa8
1 open Utils;;
2 open Config;;
4 exception Quit;;
6 external init : Unix.file_descr -> params -> unit = "ml_init";;
7 external seltext : opaque -> (int * int * int * int) -> unit = "ml_seltext";;
8 external hassel : opaque -> bool = "ml_hassel";;
9 external copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
10 external getpdimrect : int -> float array = "ml_getpdimrect";;
11 external whatsunder : opaque -> int -> int -> under = "ml_whatsunder";;
12 external markunder : opaque -> int -> int -> mark -> bool = "ml_markunder";;
13 external clearmark : opaque -> unit = "ml_clearmark";;
14 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
15 external getmaxw : unit -> float = "ml_getmaxw";;
16 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
17 external measurestr : int -> string -> float = "ml_measure_string";;
18 external postprocess :
19 opaque -> int -> int -> int -> (int * string * int) -> int
20 = "ml_postprocess";;
21 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
22 external setaalevel : int -> unit = "ml_setaalevel";;
23 external realloctexts : int -> bool = "ml_realloctexts";;
24 external findlink : opaque -> linkdir -> link = "ml_findlink";;
25 external getlink : opaque -> int -> under = "ml_getlink";;
26 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
27 external getlinkcount : opaque -> int = "ml_getlinkcount";;
28 external findpwl : int -> int -> pagewithlinks = "ml_find_page_with_links";;
29 external getpbo : width -> height -> colorspace -> opaque = "ml_getpbo";;
30 external freepbo : opaque -> unit = "ml_freepbo";;
31 external unmappbo : opaque -> unit = "ml_unmappbo";;
32 external bousable : unit -> bool = "ml_bo_usable";;
33 external unproject : opaque -> int -> int -> (int * int) option
34 = "ml_unproject";;
35 external project : opaque -> int -> int -> float -> float -> (float * float)
36 = "ml_project";;
37 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
38 external rectofblock : opaque -> int -> int -> float array option
39 = "ml_rectofblock";;
40 external begintiles : unit -> unit = "ml_begintiles";;
41 external endtiles : unit -> unit = "ml_endtiles";;
42 external addannot : opaque -> int -> int -> string -> unit = "ml_addannot";;
43 external modannot : opaque -> slinkindex -> string -> unit = "ml_modannot";;
44 external delannot : opaque -> slinkindex -> unit = "ml_delannot";;
45 external hasunsavedchanges : unit -> bool = "ml_hasunsavedchanges";;
46 external savedoc : string -> unit = "ml_savedoc";;
47 external getannotcontents : opaque -> slinkindex -> string
48 = "ml_getannotcontents";;
49 external drawprect : opaque -> int -> int -> float array -> unit =
50 "ml_drawprect";;
51 external wcmd : Unix.file_descr -> bytes -> int -> unit = "ml_wcmd";;
52 external rcmd : Unix.file_descr -> string = "ml_rcmd";;
53 external transformpagepoint : int -> int -> int -> float array
54 = "ml_transform_page_point";;
55 let selfexec = ref E.s;;
56 let opengl_has_pbo = ref false;;
58 let drawstring size x y s =
59 Gl.enable `blend;
60 Gl.enable `texture_2d;
61 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
62 ignore (drawstr size x y s);
63 Gl.disable `blend;
64 Gl.disable `texture_2d;
67 let drawstring1 size x y s =
68 drawstr size x y s;
71 let drawstring2 size x y fmt =
72 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
75 let _debugl l =
76 dolog "l %d dim=%d {" l.pageno l.pagedimno;
77 dolog " WxH %dx%d" l.pagew l.pageh;
78 dolog " vWxH %dx%d" l.pagevw l.pagevh;
79 dolog " pagex,y %d,%d" l.pagex l.pagey;
80 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
81 dolog " column %d" l.pagecol;
82 dolog "}";
85 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
86 dolog "rect {";
87 dolog " x0,y0=(% f, % f)" x0 y0;
88 dolog " x1,y1=(% f, % f)" x1 y1;
89 dolog " x2,y2=(% f, % f)" x2 y2;
90 dolog " x3,y3=(% f, % f)" x3 y3;
91 dolog "}";
94 let isbirdseye = function
95 | Birdseye _ -> true
96 | Textentry _
97 | View
98 | LinkNav _ -> false
101 let istextentry = function
102 | Textentry _ -> true
103 | Birdseye _
104 | View
105 | LinkNav _ -> false
108 let wtmode = ref false;;
109 let cxack = ref false;;
111 let pgscale h = truncate (float h *. conf.pgscale);;
113 let hscrollh () =
114 if state.uioh#alwaysscrolly || ((conf.scrollb land scrollbhv != 0)
115 && (state.w > state.winw))
116 then conf.scrollbw
117 else 0
120 let vscrollw () =
121 if state.uioh#alwaysscrolly || ((conf.scrollb land scrollbvv != 0)
122 && (state.maxy > state.winh))
123 then conf.scrollbw
124 else 0
127 let vscrollhit x =
128 if conf.leftscroll
129 then x < vscrollw ()
130 else x > state.winw - vscrollw ()
133 let setfontsize n =
134 fstate.fontsize <- n;
135 fstate.wwidth <- measurestr fstate.fontsize "w";
136 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
139 let vlog fmt =
140 if conf.verbose
141 then dolog fmt
142 else Printf.kprintf ignore fmt
145 let launchpath () =
146 if emptystr conf.pathlauncher
147 then dolog "%s" state.path
148 else (
149 let command = Str.global_replace percentsre state.path conf.pathlauncher in
150 match spawn command [] with
151 | _pid -> ()
152 | (exception exn) ->
153 dolog "failed to execute `%s': %s" command @@ exntos exn
157 module G =
158 struct
159 let postRedisplay who =
160 vlog "redisplay for [%S]" who;
161 state.redisplay <- true;
163 end;;
165 let getopaque pageno =
166 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
167 with Not_found -> None
170 let pagetranslatepoint l x y =
171 let dy = y - l.pagedispy in
172 let y = dy + l.pagey in
173 let dx = x - l.pagedispx in
174 let x = dx + l.pagex in
175 (x, y);
178 let onppundermouse g x y d =
179 let rec f = function
180 | l :: rest ->
181 begin match getopaque l.pageno with
182 | Some opaque ->
183 let x0 = l.pagedispx in
184 let x1 = x0 + l.pagevw in
185 let y0 = l.pagedispy in
186 let y1 = y0 + l.pagevh in
187 if y >= y0 && y <= y1 && x >= x0 && x <= x1
188 then
189 let px, py = pagetranslatepoint l x y in
190 match g opaque l px py with
191 | Some res -> res
192 | None -> f rest
193 else f rest
194 | _ ->
195 f rest
197 | [] -> d
199 f state.layout
202 let getunder x y =
203 let g opaque l px py =
204 if state.bzoom
205 then (
206 match rectofblock opaque px py with
207 | Some [|x0;x1;y0;y1|] ->
208 let rect = (x0, y0, x1, y0, x1, y1, x0, y1) in
209 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
210 state.rects <- [l.pageno, color, rect];
211 G.postRedisplay "getunder";
212 | _otherwise -> ()
214 let under = whatsunder opaque px py in
215 if under = Unone then None else Some under
217 onppundermouse g x y Unone
220 let unproject x y =
221 let g opaque l x y =
222 match unproject opaque x y with
223 | Some (x, y) -> Some (Some (opaque, l.pageno, x, y))
224 | None -> None
226 onppundermouse g x y None;
229 let showtext c s =
230 state.text <- Printf.sprintf "%c%s" c s;
231 G.postRedisplay "showtext";
234 let impmsg fmt =
235 Format.ksprintf (fun s -> showtext '!' s) fmt;
238 let pipesel opaque cmd =
239 if hassel opaque
240 then
241 match Unix.pipe () with
242 | (exception exn) -> dolog "pipesel cannot create pipe: %S" @@ exntos exn;
243 | (r, w) ->
244 let doclose what fd =
245 Ne.clo fd (fun msg -> dolog "%s close failed: %s" what msg)
247 let pid =
248 try spawn cmd [r, 0; w, -1]
249 with exn ->
250 dolog "cannot execute %S: %s" cmd @@ exntos exn;
253 if pid > 0
254 then (
255 copysel w opaque;
256 G.postRedisplay "pipesel";
258 else doclose "pipesel pipe/w" w;
259 doclose "pipesel pipe/r" r;
262 let paxunder x y =
263 let g opaque l px py =
264 if markunder opaque px py conf.paxmark
265 then (
266 Some (fun () ->
267 match getopaque l.pageno with
268 | None -> ()
269 | Some opaque -> pipesel opaque conf.paxcmd
272 else None
274 G.postRedisplay "paxunder";
275 if conf.paxmark = Mark_page
276 then
277 List.iter (fun l ->
278 match getopaque l.pageno with
279 | None -> ()
280 | Some opaque -> clearmark opaque) state.layout;
281 state.roam <- onppundermouse g x y (fun () -> impmsg "whoopsie daisy");
284 let selstring s =
285 match Unix.pipe () with
286 | (exception exn) -> impmsg "pipe failed: %s" @@ exntos exn
287 | (r, w) ->
288 let clo cap fd =
289 Ne.clo fd (fun msg -> impmsg "failed to close %s: %s" cap msg)
291 let pid =
292 try spawn conf.selcmd [r, 0; w, -1]
293 with exn ->
294 impmsg "failed to execute %s: %s" conf.selcmd @@ exntos exn;
297 if pid > 0
298 then (
300 let l = String.length s in
301 let bytes = Bytes.unsafe_of_string s in
302 let n = tempfailureretry (Unix.write w bytes 0) l in
303 if n != l
304 then impmsg "failed to write %d characters to sel pipe, wrote %d"
306 with exn ->
307 impmsg "failed to write to sel pipe: %s" @@ exntos exn
309 else dolog "%s" s;
310 clo "selstring pipe/r" r;
311 clo "selstring pipe/w" w;
314 let undertext ?(nopath=false) = function
315 | Unone -> "none"
316 | Ulinkuri s -> s
317 | Ulinkgoto (pageno, _) ->
318 if nopath
319 then "page " ^ string_of_int (pageno+1)
320 else Printf.sprintf "%s: page %d" state.path (pageno+1)
321 | Utext s -> "font: " ^ s
322 | Uunexpected s -> "unexpected: " ^ s
323 | Ulaunch s -> "launch: " ^ s
324 | Unamed s -> "named: " ^ s
325 | Uremote (filename, pageno) ->
326 Printf.sprintf "%s: page %d" filename (pageno+1)
327 | Uremotedest (filename, destname) ->
328 Printf.sprintf "%s: destination %S" filename destname
329 | Uannotation (opaque, slinkindex) ->
330 "annotation: " ^ getannotcontents opaque slinkindex
333 let updateunder x y =
334 match getunder x y with
335 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
336 | Ulinkuri uri ->
337 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
338 Wsi.setcursor Wsi.CURSOR_INFO
339 | Ulinkgoto (pageno, _) ->
340 if conf.underinfo
341 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
342 Wsi.setcursor Wsi.CURSOR_INFO
343 | Utext s ->
344 if conf.underinfo then showtext 'f' ("ont: " ^ s);
345 Wsi.setcursor Wsi.CURSOR_TEXT
346 | Uunexpected s ->
347 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
348 Wsi.setcursor Wsi.CURSOR_INHERIT
349 | Ulaunch s ->
350 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
351 Wsi.setcursor Wsi.CURSOR_INHERIT
352 | Unamed s ->
353 if conf.underinfo then showtext 'n' ("amed: " ^ s);
354 Wsi.setcursor Wsi.CURSOR_INHERIT
355 | Uremote (filename, pageno) ->
356 if conf.underinfo then showtext 'r'
357 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
358 Wsi.setcursor Wsi.CURSOR_INFO
359 | Uremotedest (filename, destname) ->
360 if conf.underinfo then showtext 'r'
361 (Printf.sprintf "emote destination: %s (%S)" filename destname);
362 Wsi.setcursor Wsi.CURSOR_INFO
363 | Uannotation _ ->
364 if conf.underinfo then showtext 'a' "nnotation";
365 Wsi.setcursor Wsi.CURSOR_INFO
368 let showlinktype under =
369 if conf.underinfo && under != Unone
370 then showtext ' ' @@ undertext under
373 let intentry_with_suffix text key =
374 let text =
375 if key >= 32 && key < 127
376 then
377 let c = Char.chr key in
378 match c with
379 | '0' .. '9' ->
380 addchar text c
382 | 'k' | 'm' | 'g' | 'K' | 'M' | 'G' ->
383 addchar text @@ asciilower c
384 | _ ->
385 state.text <- Printf.sprintf "invalid key (%d, `%c')" key c;
386 text
387 else (
388 state.text <- Printf.sprintf "invalid key %d" key;
389 text
392 TEcont text
395 let wcmd fmt =
396 let b = Buffer.create 16 in
397 Buffer.add_string b "llll";
398 Printf.kbprintf
399 (fun b ->
400 let b = Buffer.to_bytes b in
401 wcmd state.ss b @@ Bytes.length b
402 ) b fmt
405 let nogeomcmds cmds =
406 match cmds with
407 | s, [] -> emptystr s
408 | _ -> false
411 let layoutN ((columns, coverA, coverB), b) x y sw sh =
412 let rec fold accu n =
413 if n = Array.length b
414 then accu
415 else
416 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
417 if (vy - y) > sh &&
418 (n = coverA - 1
419 || n = state.pagecount - coverB
420 || (n - coverA) mod columns = columns - 1)
421 then accu
422 else
423 let accu =
424 if vy + h > y
425 then
426 let pagey = max 0 (y - vy) in
427 let pagedispy = if pagey > 0 then 0 else vy - y in
428 let pagedispx, pagex =
429 let pdx =
430 if n = coverA - 1 || n = state.pagecount - coverB
431 then x + (sw - w) / 2
432 else dx + xoff + x
434 if pdx < 0
435 then 0, -pdx
436 else pdx, 0
438 let pagevw =
439 let vw = sw - pagedispx in
440 let pw = w - pagex in
441 min vw pw
443 let pagevh = min (h - pagey) (sh - pagedispy) in
444 if pagevw > 0 && pagevh > 0
445 then
446 let e =
447 { pageno = n
448 ; pagedimno = pdimno
449 ; pagew = w
450 ; pageh = h
451 ; pagex = pagex
452 ; pagey = pagey
453 ; pagevw = pagevw
454 ; pagevh = pagevh
455 ; pagedispx = pagedispx
456 ; pagedispy = pagedispy
457 ; pagecol = 0
460 e :: accu
461 else
462 accu
463 else
464 accu
466 fold accu (n+1)
468 if Array.length b = 0
469 then []
470 else List.rev (fold [] (page_of_y y))
473 let layoutS (columns, b) x y sw sh =
474 let rec fold accu n =
475 if n = Array.length b
476 then accu
477 else
478 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
479 if (vy - y) > sh
480 then accu
481 else
482 let accu =
483 if vy + pageh > y
484 then
485 let x = xoff + x in
486 let pagey = max 0 (y - vy) in
487 let pagedispy = if pagey > 0 then 0 else vy - y in
488 let pagedispx, pagex =
489 if px = 0
490 then (
491 if x < 0
492 then 0, -x
493 else x, 0
495 else (
496 let px = px - x in
497 if px < 0
498 then -px, 0
499 else 0, px
502 let pagecolw = pagew/columns in
503 let pagedispx =
504 if pagecolw < sw
505 then pagedispx + ((sw - pagecolw) / 2)
506 else pagedispx
508 let pagevw =
509 let vw = sw - pagedispx in
510 let pw = pagew - pagex in
511 min vw pw
513 let pagevw = min pagevw pagecolw in
514 let pagevh = min (pageh - pagey) (sh - pagedispy) in
515 if pagevw > 0 && pagevh > 0
516 then
517 let e =
518 { pageno = n/columns
519 ; pagedimno = pdimno
520 ; pagew = pagew
521 ; pageh = pageh
522 ; pagex = pagex
523 ; pagey = pagey
524 ; pagevw = pagevw
525 ; pagevh = pagevh
526 ; pagedispx = pagedispx
527 ; pagedispy = pagedispy
528 ; pagecol = n mod columns
531 e :: accu
532 else
533 accu
534 else
535 accu
537 fold accu (n+1)
539 List.rev (fold [] 0)
542 let layout x y sw sh =
543 if nogeomcmds state.geomcmds
544 then
545 match conf.columns with
546 | Csingle b -> layoutN ((1, 0, 0), b) x y sw sh
547 | Cmulti c -> layoutN c x y sw sh
548 | Csplit s -> layoutS s x y sw sh
549 else []
552 let clamp incr =
553 let y = state.y + incr in
554 let y = max 0 y in
555 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
559 let itertiles l f =
560 let tilex = l.pagex mod conf.tilew in
561 let tiley = l.pagey mod conf.tileh in
563 let col = l.pagex / conf.tilew in
564 let row = l.pagey / conf.tileh in
566 let rec rowloop row y0 dispy h =
567 if h = 0
568 then ()
569 else (
570 let dh = conf.tileh - y0 in
571 let dh = min h dh in
572 let rec colloop col x0 dispx w =
573 if w = 0
574 then ()
575 else (
576 let dw = conf.tilew - x0 in
577 let dw = min w dw in
578 f col row dispx dispy x0 y0 dw dh;
579 colloop (col+1) 0 (dispx+dw) (w-dw)
582 colloop col tilex l.pagedispx l.pagevw;
583 rowloop (row+1) 0 (dispy+dh) (h-dh)
586 if l.pagevw > 0 && l.pagevh > 0
587 then rowloop row tiley l.pagedispy l.pagevh;
590 let gettileopaque l col row =
591 let key =
592 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
594 try Some (Hashtbl.find state.tilemap key)
595 with Not_found -> None
598 let puttileopaque l col row gen colorspace angle opaque size elapsed =
599 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
600 Hashtbl.add state.tilemap key (opaque, size, elapsed)
603 let filledrect2 x0 y0 x1 y1 x2 y2 x3 y3 =
604 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x1; y1; x2; y2; x3; y3 |];
605 GlArray.vertex `two state.vraw;
606 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
609 let filledrect1 x0 y0 x1 y1 = filledrect2 x0 y0 x0 y1 x1 y0 x1 y1;;
611 let filledrect x0 y0 x1 y1 =
612 GlArray.disable `texture_coord;
613 filledrect1 x0 y0 x1 y1;
614 GlArray.enable `texture_coord;
617 let linerect x0 y0 x1 y1 =
618 GlArray.disable `texture_coord;
619 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y1; x1; y0 |];
620 GlArray.vertex `two state.vraw;
621 GlArray.draw_arrays `line_loop ~first:0 ~count:4;
622 GlArray.enable `texture_coord;
625 let drawtiles l color =
626 GlDraw.color color;
627 begintiles ();
628 let f col row x y tilex tiley w h =
629 match gettileopaque l col row with
630 | Some (opaque, _, t) ->
631 let params = x, y, w, h, tilex, tiley in
632 if conf.invert
633 then GlTex.env (`mode `blend);
634 drawtile params opaque;
635 if conf.invert
636 then GlTex.env (`mode `modulate);
637 if conf.debug
638 then (
639 endtiles ();
640 let s = Printf.sprintf
641 "%d[%d,%d] %f sec"
642 l.pageno col row t
644 let w = measurestr fstate.fontsize s in
645 GlDraw.color (0.0, 0.0, 0.0);
646 filledrect (float (x-2))
647 (float (y-2))
648 (float (x+2) +. w)
649 (float (y + fstate.fontsize + 2));
650 GlDraw.color color;
651 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
652 begintiles ();
655 | None ->
656 endtiles ();
657 let w =
658 let lw = state.winw - x in
659 min lw w
660 and h =
661 let lh = state.winh - y in
662 min lh h
664 if conf.invert
665 then GlTex.env (`mode `blend);
666 begin match state.checkerstexid with
667 | Some id ->
668 Gl.enable `texture_2d;
669 GlTex.bind_texture ~target:`texture_2d id;
670 let x0 = float x
671 and y0 = float y
672 and x1 = float (x+w)
673 and y1 = float (y+h) in
675 let tw = float w /. 16.0
676 and th = float h /. 16.0 in
677 let tx0 = float tilex /. 16.0
678 and ty0 = float tiley /. 16.0 in
679 let tx1 = tx0 +. tw
680 and ty1 = ty0 +. th in
681 Raw.sets_float state.vraw ~pos:0
682 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
683 Raw.sets_float state.traw ~pos:0
684 [| tx0; ty0; tx0; ty1; tx1; ty0; tx1; ty1 |];
685 GlArray.vertex `two state.vraw;
686 GlArray.tex_coord `two state.traw;
687 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
688 Gl.disable `texture_2d;
690 | None ->
691 GlDraw.color (1.0, 1.0, 1.0);
692 filledrect (float x) (float y) (float (x+w)) (float (y+h));
693 end;
694 if conf.invert
695 then GlTex.env (`mode `modulate);
696 if w > 128 && h > fstate.fontsize + 10
697 then (
698 let c = if conf.invert then 1.0 else 0.0 in
699 GlDraw.color (c, c, c);
700 let c, r =
701 if conf.verbose
702 then (col*conf.tilew, row*conf.tileh)
703 else col, row
705 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
707 GlDraw.color color;
708 begintiles ();
710 itertiles l f;
711 endtiles ();
714 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
716 let tilevisible1 l x y =
717 let ax0 = l.pagex
718 and ax1 = l.pagex + l.pagevw
719 and ay0 = l.pagey
720 and ay1 = l.pagey + l.pagevh in
722 let bx0 = x
723 and by0 = y in
724 let bx1 = min (bx0 + conf.tilew) l.pagew
725 and by1 = min (by0 + conf.tileh) l.pageh in
727 let rx0 = max ax0 bx0
728 and ry0 = max ay0 by0
729 and rx1 = min ax1 bx1
730 and ry1 = min ay1 by1 in
732 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
733 nonemptyintersection
736 let tilevisible layout n x y =
737 let rec findpageinlayout m = function
738 | l :: rest when l.pageno = n ->
739 tilevisible1 l x y || (
740 match conf.columns with
741 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
742 | Csplit _
743 | Csingle _
744 | Cmulti _ -> false
746 | _ :: rest -> findpageinlayout 0 rest
747 | [] -> false
749 findpageinlayout 0 layout;
752 let tileready l x y =
753 tilevisible1 l x y &&
754 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
757 let tilepage n p layout =
758 let rec loop = function
759 | l :: rest ->
760 if l.pageno = n
761 then
762 let f col row _ _ _ _ _ _ =
763 if state.currently = Idle
764 then
765 match gettileopaque l col row with
766 | Some _ -> ()
767 | None ->
768 let x = col*conf.tilew
769 and y = row*conf.tileh in
770 let w =
771 let w = l.pagew - x in
772 min w conf.tilew
774 let h =
775 let h = l.pageh - y in
776 min h conf.tileh
778 let pbo =
779 if conf.usepbo
780 then getpbo w h conf.colorspace
781 else ~< "0"
783 wcmd "tile %s %d %d %d %d %s"
784 (~> p) x y w h (~> pbo);
785 state.currently <-
786 Tiling (
787 l, p, conf.colorspace, conf.angle,
788 state.gen, col, row, conf.tilew, conf.tileh
791 itertiles l f;
792 else
793 loop rest
795 | [] -> ()
797 if nogeomcmds state.geomcmds
798 then loop layout;
801 let preloadlayout x y sw sh =
802 let y = if y < sh then 0 else y - sh in
803 let x = min 0 (x + sw) in
804 let h = sh*3 in
805 let w = sw*3 in
806 layout x y w h;
809 let load pages =
810 let rec loop pages =
811 if state.currently != Idle
812 then ()
813 else
814 match pages with
815 | l :: rest ->
816 begin match getopaque l.pageno with
817 | None ->
818 wcmd "page %d %d" l.pageno l.pagedimno;
819 state.currently <- Loading (l, state.gen);
820 | Some opaque ->
821 tilepage l.pageno opaque pages;
822 loop rest
823 end;
824 | _ -> ()
826 if nogeomcmds state.geomcmds
827 then loop pages
830 let preload pages =
831 load pages;
832 if conf.preload && state.currently = Idle
833 then load (preloadlayout state.x state.y state.winw state.winh);
836 let layoutready layout =
837 let rec fold all ls =
838 all && match ls with
839 | l :: rest ->
840 let seen = ref false in
841 let allvisible = ref true in
842 let foo col row _ _ _ _ _ _ =
843 seen := true;
844 allvisible := !allvisible &&
845 begin match gettileopaque l col row with
846 | Some _ -> true
847 | None -> false
850 itertiles l foo;
851 fold (!seen && !allvisible) rest
852 | [] -> true
854 let alltilesvisible = fold true layout in
855 alltilesvisible;
858 let gotoxy x y =
859 let y = bound y 0 state.maxy in
860 let y, layout, proceed =
861 match conf.maxwait with
862 | Some time when state.ghyll == noghyll ->
863 begin match state.throttle with
864 | None ->
865 let layout = layout x y state.winw state.winh in
866 let ready = layoutready layout in
867 if not ready
868 then (
869 load layout;
870 state.throttle <- Some (layout, y, now ());
872 else G.postRedisplay "gotoxy showall (None)";
873 y, layout, ready
874 | Some (_, _, started) ->
875 let dt = now () -. started in
876 if dt > time
877 then (
878 state.throttle <- None;
879 let layout = layout x y state.winw state.winh in
880 load layout;
881 G.postRedisplay "maxwait";
882 y, layout, true
884 else -1, [], false
887 | _ ->
888 let layout = layout x y state.winw state.winh in
889 if not !wtmode || layoutready layout
890 then G.postRedisplay "gotoxy ready";
891 y, layout, true
893 if proceed
894 then (
895 state.x <- x;
896 state.y <- y;
897 state.layout <- layout;
898 begin match state.mode with
899 | LinkNav ln ->
900 begin match ln with
901 | Ltexact (pageno, linkno) ->
902 let rec loop = function
903 | [] ->
904 state.mode <- LinkNav (Ltgendir 0)
905 | l :: _ when l.pageno = pageno ->
906 begin match getopaque pageno with
907 | None -> state.mode <- LinkNav (Ltnotready (pageno, 0))
908 | Some opaque ->
909 let x0, y0, x1, y1 = getlinkrect opaque linkno in
910 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
911 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
912 then state.mode <- LinkNav (Ltgendir 0)
914 | _ :: rest -> loop rest
916 loop layout
917 | Ltnotready _ | Ltgendir _ -> ()
919 | Birdseye _
920 | Textentry _
921 | View -> ()
922 end;
923 begin match state.mode with
924 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
925 if not (pagevisible layout pageno)
926 then (
927 match state.layout with
928 | [] -> ()
929 | l :: _ ->
930 state.mode <- Birdseye (
931 conf, leftx, l.pageno, hooverpageno, anchor
934 | LinkNav lt ->
935 begin match lt with
936 | Ltnotready (_, dir)
937 | Ltgendir dir ->
938 let linknav =
939 let rec loop = function
940 | [] -> lt
941 | l :: rest ->
942 match getopaque l.pageno with
943 | None -> Ltnotready (l.pageno, dir)
944 | Some opaque ->
945 let link =
946 let ld =
947 if dir = 0
948 then LDfirstvisible (l.pagex, l.pagey, dir)
949 else (
950 if dir > 0 then LDfirst else LDlast
953 findlink opaque ld
955 match link with
956 | Lnotfound -> loop rest
957 | Lfound n ->
958 showlinktype (getlink opaque n);
959 Ltexact (l.pageno, n)
961 loop state.layout
963 state.mode <- LinkNav linknav
964 | Ltexact _ -> ()
966 | Textentry _
967 | View -> ()
968 end;
969 preload layout;
971 state.ghyll <- noghyll;
972 if conf.updatecurs
973 then (
974 let mx, my = state.mpos in
975 updateunder mx my;
979 let conttiling pageno opaque =
980 tilepage pageno opaque
981 (if conf.preload
982 then preloadlayout state.x state.y state.winw state.winh
983 else state.layout)
986 let gotoxy_and_clear_text x y =
987 if not conf.verbose then state.text <- E.s;
988 gotoxy x y;
991 let getanchory (n, top, dtop) =
992 let y, h = getpageyh n in
993 if conf.presentation
994 then
995 let ips = calcips h in
996 y + truncate (top*.float h -. dtop*.float ips) + ips;
997 else
998 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1001 let gotoanchor anchor =
1002 gotoxy state.x (getanchory anchor);
1005 let addnav () =
1006 cbput state.hists.nav (getanchor ());
1009 let getnav dir =
1010 let anchor = cbgetc state.hists.nav dir in
1011 getanchory anchor;
1014 let gotoghyll1 single y =
1015 let scroll f n a b =
1016 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1017 let snake f a b =
1018 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1019 if f < a
1020 then s (float f /. float a)
1021 else (
1022 if f > b
1023 then 1.0 -. s ((float (f-b) /. float (n-b)))
1024 else 1.0
1027 snake f a b
1028 and summa n a b =
1029 let ins = float a *. 0.5
1030 and outs = float (n-b) *. 0.5 in
1031 let ones = b - a in
1032 ins +. outs +. float ones
1034 let rec set nab y sy =
1035 let (_N, _A, _B), y =
1036 if single
1037 then
1038 let scl = if y > sy then 2 else -2 in
1039 let _N, _, _ = nab in
1040 (_N,0,_N), y+conf.scrollstep*scl
1041 else nab,y in
1042 let sum = summa _N _A _B in
1043 let dy = float (y - sy) in
1044 state.ghyll <- (
1045 let rec gf n y1 o =
1046 if n >= _N
1047 then state.ghyll <- noghyll
1048 else
1049 let go n =
1050 let s = scroll n _N _A _B in
1051 let y1 = y1 +. ((s *. dy) /. sum) in
1052 gotoxy_and_clear_text state.x (truncate y1);
1053 state.ghyll <- gf (n+1) y1;
1055 match o with
1056 | None -> go n
1057 | Some y' when single -> set nab y' state.y
1058 | Some y' -> set (_N/2, 1, 1) y' state.y
1060 gf 0 (float state.y)
1063 match conf.ghyllscroll with
1064 | Some nab when not conf.presentation ->
1065 if state.ghyll == noghyll
1066 then set nab y state.y
1067 else state.ghyll (Some y)
1068 | _ ->
1069 gotoxy_and_clear_text state.x y
1072 let gotoghyll = gotoghyll1 false;;
1074 let gotopage n top =
1075 let y, h = getpageyh n in
1076 let y = y + (truncate (top *. float h)) in
1077 gotoghyll y
1080 let gotopage1 n top =
1081 let y = getpagey n in
1082 let y = y + top in
1083 gotoghyll y
1086 let invalidate s f =
1087 state.redisplay <- false;
1088 state.layout <- [];
1089 state.pdims <- [];
1090 state.rects <- [];
1091 state.rects1 <- [];
1092 match state.geomcmds with
1093 | ps, [] when emptystr ps ->
1094 f ();
1095 state.geomcmds <- s, [];
1097 | ps, [] ->
1098 state.geomcmds <- ps, [s, f];
1100 | ps, (s', _) :: rest when s' = s ->
1101 state.geomcmds <- ps, ((s, f) :: rest);
1103 | ps, cmds ->
1104 state.geomcmds <- ps, ((s, f) :: cmds);
1107 let flushpages () =
1108 Hashtbl.iter (fun _ opaque ->
1109 wcmd "freepage %s" (~> opaque);
1110 ) state.pagemap;
1111 Hashtbl.clear state.pagemap;
1114 let flushtiles () =
1115 if not (Queue.is_empty state.tilelru)
1116 then (
1117 Queue.iter (fun (k, p, s) ->
1118 wcmd "freetile %s" (~> p);
1119 state.memused <- state.memused - s;
1120 Hashtbl.remove state.tilemap k;
1121 ) state.tilelru;
1122 state.uioh#infochanged Memused;
1123 Queue.clear state.tilelru;
1125 load state.layout;
1128 let stateh h =
1129 let h = truncate (float h*.conf.zoom) in
1130 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
1131 h - d
1134 let opendoc path password =
1135 state.path <- path;
1136 state.password <- password;
1137 state.gen <- state.gen + 1;
1138 state.docinfo <- [];
1139 state.outlines <- [||];
1141 flushpages ();
1142 setaalevel conf.aalevel;
1143 let titlepath =
1144 if emptystr state.origin
1145 then path
1146 else state.origin
1148 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
1149 wcmd "open %d %d %s\000%s\000" (btod !wtmode) (btod !cxack) path password;
1150 invalidate "reqlayout"
1151 (fun () ->
1152 wcmd "reqlayout %d %d %d %s\000"
1153 conf.angle (FMTE.to_int conf.fitmodel)
1154 (stateh state.winh) state.nameddest
1158 let reload () =
1159 state.anchor <- getanchor ();
1160 opendoc state.path state.password;
1163 let scalecolor c =
1164 let c = c *. conf.colorscale in
1165 (c, c, c);
1168 let scalecolor2 (r, g, b) =
1169 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1172 let docolumns columns =
1173 match columns with
1174 | Csingle _ ->
1175 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1176 let rec loop pageno pdimno pdim y ph pdims =
1177 if pageno = state.pagecount
1178 then ()
1179 else
1180 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1181 match pdims with
1182 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1183 pdimno+1, pdim, rest
1184 | _ ->
1185 pdimno, pdim, pdims
1187 let x = max 0 (((state.winw - w) / 2) - xoff) in
1188 let y = y +
1189 (if conf.presentation
1190 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1191 else (if pageno = 0 then 0 else conf.interpagespace)
1194 a.(pageno) <- (pdimno, x, y, pdim);
1195 loop (pageno+1) pdimno pdim (y + h) h pdims
1197 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1198 conf.columns <- Csingle a;
1200 | Cmulti ((columns, coverA, coverB), _) ->
1201 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1202 let rec loop pageno pdimno pdim x y rowh pdims =
1203 let rec fixrow m = if m = pageno then () else
1204 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1205 if h < rowh
1206 then (
1207 let y = y + (rowh - h) / 2 in
1208 a.(m) <- (pdimno, x, y, pdim);
1210 fixrow (m+1)
1212 if pageno = state.pagecount
1213 then fixrow (((pageno - 1) / columns) * columns)
1214 else
1215 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1216 match pdims with
1217 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1218 pdimno+1, pdim, rest
1219 | _ ->
1220 pdimno, pdim, pdims
1222 let x, y, rowh' =
1223 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1224 then (
1225 let x = (state.winw - w) / 2 in
1226 let ips =
1227 if conf.presentation then calcips h else conf.interpagespace in
1228 x, y + ips + rowh, h
1230 else (
1231 if (pageno - coverA) mod columns = 0
1232 then (
1233 let x = max 0 (state.winw - state.w) / 2 in
1234 let y =
1235 if conf.presentation
1236 then
1237 let ips = calcips h in
1238 y + (if pageno = 0 then 0 else calcips rowh + ips)
1239 else
1240 y + (if pageno = 0 then 0 else conf.interpagespace)
1242 x, y + rowh, h
1244 else x, y, max rowh h
1247 let y =
1248 if pageno > 1 && (pageno - coverA) mod columns = 0
1249 then (
1250 let y =
1251 if pageno = columns && conf.presentation
1252 then (
1253 let ips = calcips rowh in
1254 for i = 0 to pred columns
1256 let (pdimno, x, y, pdim) = a.(i) in
1257 a.(i) <- (pdimno, x, y+ips, pdim)
1258 done;
1259 y+ips;
1261 else y
1263 fixrow (pageno - columns);
1266 else y
1268 a.(pageno) <- (pdimno, x, y, pdim);
1269 let x = x + w + xoff*2 + conf.interpagespace in
1270 loop (pageno+1) pdimno pdim x y rowh' pdims
1272 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1273 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1275 | Csplit (c, _) ->
1276 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1277 let rec loop pageno pdimno pdim y pdims =
1278 if pageno = state.pagecount
1279 then ()
1280 else
1281 let pdimno, ((_, w, h, _) as pdim), pdims =
1282 match pdims with
1283 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1284 pdimno+1, pdim, rest
1285 | _ ->
1286 pdimno, pdim, pdims
1288 let cw = w / c in
1289 let rec loop1 n x y =
1290 if n = c then y else (
1291 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1292 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1295 let y = loop1 0 0 y in
1296 loop (pageno+1) pdimno pdim y pdims
1298 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1299 conf.columns <- Csplit (c, a);
1302 let represent () =
1303 docolumns conf.columns;
1304 state.maxy <- calcheight ();
1305 if state.reprf == noreprf
1306 then (
1307 match state.mode with
1308 | Birdseye (_, _, pageno, _, _) ->
1309 let y, h = getpageyh pageno in
1310 let top = (state.winh - h) / 2 in
1311 gotoxy state.x (max 0 (y - top))
1312 | Textentry _
1313 | View
1314 | LinkNav _ ->
1315 let y = getanchory state.anchor in
1316 let y = min y (state.maxy - state.winh) in
1317 gotoxy state.x y;
1319 else (
1320 state.reprf ();
1321 state.reprf <- noreprf;
1325 let reshape ?(firsttime=false) w h =
1326 GlDraw.viewport ~x:0 ~y:0 ~w ~h;
1327 if not firsttime && nogeomcmds state.geomcmds
1328 then state.anchor <- getanchor ();
1330 state.winw <- w;
1331 let w = truncate (float w *. conf.zoom) in
1332 let w = max w 2 in
1333 state.winh <- h;
1334 setfontsize fstate.fontsize;
1335 GlMat.mode `modelview;
1336 GlMat.load_identity ();
1338 GlMat.mode `projection;
1339 GlMat.load_identity ();
1340 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1341 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1342 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
1344 let relx =
1345 if conf.zoom <= 1.0
1346 then 0.0
1347 else float state.x /. float state.w
1349 invalidate "geometry"
1350 (fun () ->
1351 state.w <- w;
1352 if not firsttime
1353 then state.x <- truncate (relx *. float w);
1354 let w =
1355 match conf.columns with
1356 | Csingle _ -> w
1357 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1358 | Csplit (c, _) -> w * c
1360 wcmd "geometry %d %d %d"
1361 w (stateh h) (FMTE.to_int conf.fitmodel)
1365 let enttext () =
1366 let len = String.length state.text in
1367 let x0 = if conf.leftscroll then vscrollw () else 0 in
1368 let drawstring s =
1369 let hscrollh =
1370 match state.mode with
1371 | Textentry _ | View | LinkNav _ ->
1372 let h, _, _ = state.uioh#scrollpw in
1374 | Birdseye _ -> 0
1376 let rect x w =
1377 filledrect x (float (state.winh - (fstate.fontsize + 4) - hscrollh))
1378 (x+.w) (float (state.winh - hscrollh))
1381 let w = float (state.winw - 1 - vscrollw ()) in
1382 if state.progress >= 0.0 && state.progress < 1.0
1383 then (
1384 GlDraw.color (0.3, 0.3, 0.3);
1385 let w1 = w *. state.progress in
1386 rect (float x0) w1;
1387 GlDraw.color (0.0, 0.0, 0.0);
1388 rect (float x0+.w1) (float x0+.w-.w1)
1390 else (
1391 GlDraw.color (0.0, 0.0, 0.0);
1392 rect (float x0) w;
1395 GlDraw.color (1.0, 1.0, 1.0);
1396 drawstring fstate.fontsize
1397 (if conf.leftscroll then x0 + 2 else x0 + if len > 0 then 8 else 2)
1398 (state.winh - hscrollh - 5) s;
1400 let s =
1401 match state.mode with
1402 | Textentry ((prefix, text, _, _, _, _), _) ->
1403 let s =
1404 if len > 0
1405 then
1406 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1407 else
1408 Printf.sprintf "%s%s_" prefix text
1412 | Birdseye _
1413 | View
1414 | LinkNav _ -> state.text
1416 let s =
1417 if state.newerrmsgs
1418 then (
1419 if not (istextentry state.mode) && state.uioh#eformsgs
1420 then
1421 let s1 = "(press 'e' to review error messasges)" in
1422 if nonemptystr s then s ^ " " ^ s1 else s1
1423 else s
1425 else s
1427 if nonemptystr s
1428 then drawstring s
1431 let gctiles () =
1432 let len = Queue.length state.tilelru in
1433 let layout = lazy (
1434 match state.throttle with
1435 | None ->
1436 if conf.preload
1437 then preloadlayout state.x state.y state.winw state.winh
1438 else state.layout
1439 | Some (layout, _, _) ->
1440 layout
1441 ) in
1442 let rec loop qpos =
1443 if state.memused <= conf.memlimit
1444 then ()
1445 else (
1446 if qpos < len
1447 then
1448 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1449 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1450 let (_, pw, ph, _) = getpagedim n in
1452 gen = state.gen
1453 && colorspace = conf.colorspace
1454 && angle = conf.angle
1455 && pagew = pw
1456 && pageh = ph
1457 && (
1458 let x = col*conf.tilew
1459 and y = row*conf.tileh in
1460 tilevisible (Lazy.force_val layout) n x y
1462 then Queue.push lruitem state.tilelru
1463 else (
1464 freepbo p;
1465 wcmd "freetile %s" (~> p);
1466 state.memused <- state.memused - s;
1467 state.uioh#infochanged Memused;
1468 Hashtbl.remove state.tilemap k;
1470 loop (qpos+1)
1473 loop 0
1476 let onpagerect pageno f =
1477 let b =
1478 match conf.columns with
1479 | Cmulti (_, b) -> b
1480 | Csingle b -> b
1481 | Csplit (_, b) -> b
1483 if pageno >= 0 && pageno < Array.length b
1484 then
1485 let (_, _, _, (_, w, h, _)) = b.(pageno) in
1486 f w h
1489 let gotopagexy1 wtmode pageno x y =
1490 let _,w1,h1,leftx = getpagedim pageno in
1491 let top = y /. (float h1) in
1492 let left = x /. (float w1) in
1493 let py, w, h = getpageywh pageno in
1494 let wh = state.winh in
1495 let x = left *. (float w) in
1496 let x = leftx + state.x + truncate x in
1497 let sx =
1498 if x < 0 || x >= state.winw
1499 then state.x - x
1500 else state.x
1502 let pdy = truncate (top *. float h) in
1503 let y' = py + pdy in
1504 let dy = y' - state.y in
1505 let sy =
1506 if x != state.x || not (dy > 0 && dy < wh)
1507 then (
1508 if conf.presentation
1509 then
1510 if abs (py - y') > wh
1511 then y'
1512 else py
1513 else y';
1515 else state.y
1517 if state.x != sx || state.y != sy
1518 then (
1519 let x, y =
1520 if wtmode
1521 then (
1522 let ww = state.winw in
1523 let qx = sx / ww
1524 and qy = pdy / wh in
1525 let x = qx * ww
1526 and y = py + qy * wh in
1527 let x = if -x + ww > w1 then -(w1-ww) else x
1528 and y' = if y + wh > state.maxy then state.maxy - wh else y in
1529 let y =
1530 if conf.presentation
1531 then
1532 if abs (py - y') > wh
1533 then y'
1534 else py
1535 else y';
1537 (x, y)
1539 else (sx, sy)
1541 gotoxy_and_clear_text x y;
1543 else gotoxy_and_clear_text state.x state.y;
1546 let gotopagexy wtmode pageno x y =
1547 match state.mode with
1548 | Birdseye _ -> gotopage pageno 0.0
1549 | Textentry _
1550 | View
1551 | LinkNav _ -> gotopagexy1 wtmode pageno x y
1554 let getpassword () =
1555 let passcmd = getenvwithdef "LLPP_ASKPASS" conf.passcmd in
1556 if emptystr passcmd
1557 then E.s
1558 else getcmdoutput
1559 (fun s ->
1560 impmsg "error getting password: %s" s;
1561 dolog "%s" s) passcmd;
1564 let pgoto opaque pageno x y =
1565 let pdimno = getpdimno pageno in
1566 let x, y = project opaque pageno pdimno x y in
1567 gotopagexy false pageno x y;
1570 let act cmds =
1571 (* dolog "%S" cmds; *)
1572 let spl = splitatspace cmds in
1573 let scan s fmt f =
1574 try Scanf.sscanf s fmt f
1575 with exn ->
1576 dolog "error processing '%S': %s" cmds @@ exntos exn;
1577 exit 1
1579 let addoutline outline =
1580 match state.currently with
1581 | Outlining outlines ->
1582 state.currently <- Outlining (outline :: outlines)
1583 | Idle -> state.currently <- Outlining [outline]
1584 | Loading _
1585 | Tiling _ ->
1586 dolog "invalid outlining state";
1587 logcurrently state.currently
1589 match spl with
1590 | "clear", "" ->
1591 state.uioh#infochanged Pdim;
1592 state.pdims <- [];
1594 | "clearrects", "" ->
1595 state.rects <- state.rects1;
1596 G.postRedisplay "clearrects";
1598 | "continue", args ->
1599 let n = scan args "%u" (fun n -> n) in
1600 state.pagecount <- n;
1601 begin match state.currently with
1602 | Outlining l ->
1603 state.currently <- Idle;
1604 state.outlines <- Array.of_list (List.rev l)
1605 | Idle
1606 | Loading _
1607 | Tiling _ -> ()
1608 end;
1610 let cur, cmds = state.geomcmds in
1611 if emptystr cur
1612 then failwith "umpossible";
1614 begin match List.rev cmds with
1615 | [] ->
1616 state.geomcmds <- E.s, [];
1617 state.throttle <- None;
1618 represent ();
1619 | (s, f) :: rest ->
1620 f ();
1621 state.geomcmds <- s, List.rev rest;
1622 end;
1623 if conf.maxwait = None && not !wtmode
1624 then G.postRedisplay "continue";
1626 | "msg", args ->
1627 showtext ' ' args
1629 | "vmsg", args ->
1630 if conf.verbose
1631 then showtext ' ' args
1633 | "emsg", args ->
1634 Buffer.add_string state.errmsgs args;
1635 state.newerrmsgs <- true;
1636 G.postRedisplay "error message"
1638 | "progress", args ->
1639 let progress, text =
1640 scan args "%f %n"
1641 (fun f pos ->
1642 f, String.sub args pos (String.length args - pos))
1644 state.text <- text;
1645 state.progress <- progress;
1646 G.postRedisplay "progress"
1648 | "firstmatch", args ->
1649 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1650 scan args "%u %d %f %f %f %f %f %f %f %f"
1651 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1652 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1654 let y = (getpagey pageno) + truncate y0 in
1655 let x =
1656 if conf.zoom > 1.0
1657 then state.winw/2
1658 else state.x
1660 addnav ();
1661 gotoxy x y;
1662 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1663 state.rects1 <- [pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)]
1665 | "match", args ->
1666 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1667 scan args "%u %d %f %f %f %f %f %f %f %f"
1668 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1669 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1671 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1672 state.rects1 <-
1673 (pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1675 | "page", args ->
1676 let pageopaques, t = scan args "%s %f" (fun p t -> p, t) in
1677 let pageopaque = ~< pageopaques in
1678 begin match state.currently with
1679 | Loading (l, gen) ->
1680 vlog "page %d took %f sec" l.pageno t;
1681 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1682 begin match state.throttle with
1683 | None ->
1684 let preloadedpages =
1685 if conf.preload
1686 then preloadlayout state.x state.y state.winw state.winh
1687 else state.layout
1689 let evict () =
1690 let set =
1691 List.fold_left (fun s l -> IntSet.add l.pageno s)
1692 IntSet.empty preloadedpages
1694 let evictedpages =
1695 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1696 if not (IntSet.mem pageno set)
1697 then (
1698 wcmd "freepage %s" (~> opaque);
1699 key :: accu
1701 else accu
1702 ) state.pagemap []
1704 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1706 evict ();
1707 state.currently <- Idle;
1708 if gen = state.gen
1709 then (
1710 tilepage l.pageno pageopaque state.layout;
1711 load state.layout;
1712 load preloadedpages;
1713 let visible = pagevisible state.layout l.pageno in
1714 if visible
1715 then (
1716 match state.mode with
1717 | LinkNav (Ltnotready (pageno, dir)) ->
1718 if pageno = l.pageno
1719 then (
1720 let link =
1721 let ld =
1722 if dir = 0
1723 then LDfirstvisible (l.pagex, l.pagey, dir)
1724 else (
1725 if dir > 0 then LDfirst else LDlast
1728 findlink pageopaque ld
1730 match link with
1731 | Lnotfound -> ()
1732 | Lfound n ->
1733 showlinktype (getlink pageopaque n);
1734 state.mode <- LinkNav (Ltexact (l.pageno, n))
1736 | LinkNav (Ltgendir _)
1737 | LinkNav (Ltexact _)
1738 | View
1739 | Birdseye _
1740 | Textentry _ -> ()
1743 if visible && layoutready state.layout
1744 then (
1745 G.postRedisplay "page";
1749 | Some (layout, _, _) ->
1750 state.currently <- Idle;
1751 tilepage l.pageno pageopaque layout;
1752 load state.layout
1753 end;
1755 | Idle
1756 | Tiling _
1757 | Outlining _ ->
1758 dolog "Inconsistent loading state";
1759 logcurrently state.currently;
1760 exit 1
1763 | "tile" , args ->
1764 let (x, y, opaques, size, t) =
1765 scan args "%u %u %s %u %f"
1766 (fun x y p size t -> (x, y, p, size, t))
1768 let opaque = ~< opaques in
1769 begin match state.currently with
1770 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1771 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1773 unmappbo opaque;
1774 if tilew != conf.tilew || tileh != conf.tileh
1775 then (
1776 wcmd "freetile %s" (~> opaque);
1777 state.currently <- Idle;
1778 load state.layout;
1780 else (
1781 puttileopaque l col row gen cs angle opaque size t;
1782 state.memused <- state.memused + size;
1783 state.uioh#infochanged Memused;
1784 gctiles ();
1785 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1786 opaque, size) state.tilelru;
1788 let layout =
1789 match state.throttle with
1790 | None -> state.layout
1791 | Some (layout, _, _) -> layout
1794 state.currently <- Idle;
1795 if gen = state.gen
1796 && conf.colorspace = cs
1797 && conf.angle = angle
1798 && tilevisible layout l.pageno x y
1799 then conttiling l.pageno pageopaque;
1801 begin match state.throttle with
1802 | None ->
1803 preload state.layout;
1804 if gen = state.gen
1805 && conf.colorspace = cs
1806 && conf.angle = angle
1807 && tilevisible state.layout l.pageno x y
1808 && (not !wtmode || layoutready state.layout)
1809 then G.postRedisplay "tile nothrottle";
1811 | Some (layout, y, _) ->
1812 let ready = layoutready layout in
1813 if ready
1814 then (
1815 state.y <- y;
1816 state.layout <- layout;
1817 state.throttle <- None;
1818 G.postRedisplay "throttle";
1820 else load layout;
1821 end;
1824 | Idle
1825 | Loading _
1826 | Outlining _ ->
1827 dolog "Inconsistent tiling state";
1828 logcurrently state.currently;
1829 exit 1
1832 | "pdim", args ->
1833 let (n, w, h, _) as pdim =
1834 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1836 let pdim =
1837 match conf.fitmodel with
1838 | FitWidth -> pdim
1839 | FitPage | FitProportional ->
1840 match conf.columns with
1841 | Csplit _ -> (n, w, h, 0)
1842 | Csingle _ | Cmulti _ -> pdim
1844 state.uioh#infochanged Pdim;
1845 state.pdims <- pdim :: state.pdims
1847 | "o", args ->
1848 let (l, n, t, h, pos) =
1849 scan args "%u %u %d %u %n"
1850 (fun l n t h pos -> l, n, t, h, pos)
1852 let s = String.sub args pos (String.length args - pos) in
1853 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
1855 | "ou", args ->
1856 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
1857 let s = String.sub args pos len in
1858 let pos2 = pos + len + 1 in
1859 let uri = String.sub args pos2 (String.length args - pos2) in
1860 addoutline (s, l, Ouri uri)
1862 | "on", args ->
1863 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
1864 let s = String.sub args pos (String.length args - pos) in
1865 addoutline (s, l, Onone)
1867 | "a", args ->
1868 let (n, l, t) =
1869 scan args "%u %d %d" (fun n l t -> n, l, t)
1871 state.reprf <- (fun () -> gotopagexy !wtmode n (float l) (float t))
1873 | "info", args ->
1874 let pos = nindex args '\t' in
1875 if pos >= 0 && String.sub args 0 pos = "Title"
1876 then (
1877 let s = String.sub args (pos+1) @@ String.length args - pos - 1 in
1878 conf.title <- s;
1879 Wsi.settitle s;
1881 state.docinfo <- (1, args) :: state.docinfo
1883 | "infoend", "" ->
1884 state.uioh#infochanged Docinfo;
1885 state.docinfo <- List.rev state.docinfo
1887 | "pass", args ->
1888 if args = "fail"
1889 then Wsi.settitle "Wrong password";
1890 let password = getpassword () in
1891 if emptystr password
1892 then error "document is password protected"
1893 else opendoc state.path password
1895 | _ ->
1896 error "unknown cmd `%S'" cmds
1899 let onhist cb =
1900 let rc = cb.rc in
1901 let action = function
1902 | HCprev -> cbget cb ~-1
1903 | HCnext -> cbget cb 1
1904 | HCfirst -> cbget cb ~-(cb.rc)
1905 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1906 and cancel () = cb.rc <- rc
1907 in (action, cancel)
1910 let search pattern forward =
1911 match conf.columns with
1912 | Csplit _ -> impmsg "searching does not work properly in split columns mode"
1913 | Csingle _
1914 | Cmulti _ ->
1915 if nonemptystr pattern
1916 then
1917 let pn, py =
1918 match state.layout with
1919 | [] -> 0, 0
1920 | l :: _ ->
1921 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1923 wcmd "search %d %d %d %d,%s\000"
1924 (btod conf.icase) pn py (btod forward) pattern;
1927 let intentry text key =
1928 let text =
1929 if key >= 32 && key < 127
1930 then
1931 let c = Char.chr key in
1932 match c with
1933 | '0' .. '9' -> addchar text c
1934 | _ ->
1935 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1936 text
1937 else (
1938 state.text <- Printf.sprintf "invalid key (%d)" key;
1939 text
1942 TEcont text
1945 let linknact f s =
1946 if nonemptystr s
1947 then (
1948 let n =
1949 let l = String.length s in
1950 let rec loop pos n = if pos = l then n else
1951 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
1952 loop (pos+1) (n*26 + m)
1953 in loop 0 0
1955 let rec loop n = function
1956 | [] -> ()
1957 | l :: rest ->
1958 match getopaque l.pageno with
1959 | None -> loop n rest
1960 | Some opaque ->
1961 let m = getlinkcount opaque in
1962 if n < m
1963 then (
1964 let under = getlink opaque n in
1965 f under
1967 else loop (n-m) rest
1969 loop n state.layout;
1973 let linknentry text key =
1974 if key >= 32 && key < 127
1975 then
1976 let text = addchar text (Char.chr key) in
1977 linknact (fun under -> state.text <- undertext ~nopath:true under) text;
1978 TEcont text
1979 else (
1980 state.text <- Printf.sprintf "invalid key %d" key;
1981 TEcont text
1985 let textentry text key =
1986 if Wsi.isspecialkey key
1987 then TEcont text
1988 else TEcont (text ^ toutf8 key)
1991 let reqlayout angle fitmodel =
1992 match state.throttle with
1993 | None ->
1994 if nogeomcmds state.geomcmds
1995 then state.anchor <- getanchor ();
1996 conf.angle <- angle mod 360;
1997 if conf.angle != 0
1998 then (
1999 match state.mode with
2000 | LinkNav _ -> state.mode <- View
2001 | Birdseye _
2002 | Textentry _
2003 | View -> ()
2005 conf.fitmodel <- fitmodel;
2006 invalidate "reqlayout"
2007 (fun () ->
2008 wcmd "reqlayout %d %d %d"
2009 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
2011 | _ -> ()
2014 let settrim trimmargins trimfuzz =
2015 if nogeomcmds state.geomcmds
2016 then state.anchor <- getanchor ();
2017 conf.trimmargins <- trimmargins;
2018 conf.trimfuzz <- trimfuzz;
2019 let x0, y0, x1, y1 = trimfuzz in
2020 invalidate "settrim"
2021 (fun () ->
2022 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2023 flushpages ();
2026 let setzoom zoom =
2027 match state.throttle with
2028 | None ->
2029 let zoom = max 0.0001 zoom in
2030 if zoom <> conf.zoom
2031 then (
2032 state.prevzoom <- (conf.zoom, state.x);
2033 conf.zoom <- zoom;
2034 reshape state.winw state.winh;
2035 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2038 | Some (layout, y, started) ->
2039 let time =
2040 match conf.maxwait with
2041 | None -> 0.0
2042 | Some t -> t
2044 let dt = now () -. started in
2045 if dt > time
2046 then (
2047 state.y <- y;
2048 load layout;
2052 let pivotzoom ?(vw=min state.w state.winw)
2053 ?(vh=min (state.maxy-state.y) state.winh)
2054 ?(x=vw/2) ?(y=vh/2) zoom =
2055 let w = float state.w /. zoom in
2056 let hw = w /. 2.0 in
2057 let ratio = float vh /. float vw in
2058 let hh = hw *. ratio in
2059 let x0 = if zoom < 1.0 then 0.0 else float x -. hw in
2060 let y0 = float y -. hh in
2061 gotoxy (state.x - truncate x0) (state.y + truncate y0);
2062 setzoom zoom;
2065 let pivotzoom ?vw ?vh ?x ?y zoom =
2066 if nogeomcmds state.geomcmds then pivotzoom ?vw ?vh ?x ?y zoom
2069 let setcolumns mode columns coverA coverB =
2070 state.prevcolumns <- Some (conf.columns, conf.zoom);
2071 if columns < 0
2072 then (
2073 if isbirdseye mode
2074 then impmsg "split mode doesn't work in bird's eye"
2075 else (
2076 conf.columns <- Csplit (-columns, E.a);
2077 state.x <- 0;
2078 conf.zoom <- 1.0;
2081 else (
2082 if columns < 2
2083 then (
2084 conf.columns <- Csingle E.a;
2085 state.x <- 0;
2086 setzoom 1.0;
2088 else (
2089 conf.columns <- Cmulti ((columns, coverA, coverB), E.a);
2090 conf.zoom <- 1.0;
2093 reshape state.winw state.winh;
2096 let resetmstate () =
2097 state.mstate <- Mnone;
2098 Wsi.setcursor Wsi.CURSOR_INHERIT;
2101 let enterbirdseye () =
2102 let zoom = float conf.thumbw /. float state.winw in
2103 let birdseyepageno =
2104 let cy = state.winh / 2 in
2105 let fold = function
2106 | [] -> 0
2107 | l :: rest ->
2108 let rec fold best = function
2109 | [] -> best.pageno
2110 | l :: rest ->
2111 let d = cy - (l.pagedispy + l.pagevh/2)
2112 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2113 if abs d < abs dbest
2114 then fold l rest
2115 else best.pageno
2116 in fold l rest
2118 fold state.layout
2120 state.mode <- Birdseye (
2121 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2123 resetmstate ();
2124 conf.zoom <- zoom;
2125 conf.presentation <- false;
2126 conf.interpagespace <- 10;
2127 conf.hlinks <- false;
2128 conf.fitmodel <- FitPage;
2129 state.x <- 0;
2130 conf.maxwait <- None;
2131 conf.columns <- (
2132 match conf.beyecolumns with
2133 | Some c ->
2134 conf.zoom <- 1.0;
2135 Cmulti ((c, 0, 0), E.a)
2136 | None -> Csingle E.a
2138 if conf.verbose
2139 then
2140 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2141 (100.0*.zoom)
2142 else
2143 state.text <- E.s
2145 reshape state.winw state.winh;
2148 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2149 state.mode <- View;
2150 conf.zoom <- c.zoom;
2151 conf.presentation <- c.presentation;
2152 conf.interpagespace <- c.interpagespace;
2153 conf.maxwait <- c.maxwait;
2154 conf.hlinks <- c.hlinks;
2155 conf.fitmodel <- c.fitmodel;
2156 conf.beyecolumns <- (
2157 match conf.columns with
2158 | Cmulti ((c, _, _), _) -> Some c
2159 | Csingle _ -> None
2160 | Csplit _ -> failwith "leaving bird's eye split mode"
2162 conf.columns <- (
2163 match c.columns with
2164 | Cmulti (c, _) -> Cmulti (c, E.a)
2165 | Csingle _ -> Csingle E.a
2166 | Csplit (c, _) -> Csplit (c, E.a)
2168 if conf.verbose
2169 then
2170 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2171 (100.0*.conf.zoom)
2173 reshape state.winw state.winh;
2174 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2175 state.x <- leftx;
2178 let togglebirdseye () =
2179 match state.mode with
2180 | Birdseye vals -> leavebirdseye vals true
2181 | View -> enterbirdseye ()
2182 | Textentry _
2183 | LinkNav _ -> ()
2186 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2187 let pageno = max 0 (pageno - incr) in
2188 let rec loop = function
2189 | [] -> gotopage1 pageno 0
2190 | l :: _ when l.pageno = pageno ->
2191 if l.pagedispy >= 0 && l.pagey = 0
2192 then G.postRedisplay "upbirdseye"
2193 else gotopage1 pageno 0
2194 | _ :: rest -> loop rest
2196 loop state.layout;
2197 state.text <- E.s;
2198 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2201 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2202 let pageno = min (state.pagecount - 1) (pageno + incr) in
2203 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2204 let rec loop = function
2205 | [] ->
2206 let y, h = getpageyh pageno in
2207 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2208 gotoxy state.x (clamp dy)
2209 | l :: _ when l.pageno = pageno ->
2210 if l.pagevh != l.pageh
2211 then gotoxy state.x (clamp (l.pageh - l.pagevh + conf.interpagespace))
2212 else G.postRedisplay "downbirdseye"
2213 | _ :: rest -> loop rest
2215 loop state.layout;
2216 state.text <- E.s;
2219 let optentry mode _ key =
2220 let btos b = if b then "on" else "off" in
2221 if key >= 32 && key < 127
2222 then
2223 let c = Char.chr key in
2224 match c with
2225 | 's' ->
2226 let ondone s =
2227 try conf.scrollstep <- int_of_string s with exn ->
2228 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2230 TEswitch ("scroll step: ", E.s, None, intentry, ondone, true)
2232 | 'A' ->
2233 let ondone s =
2235 conf.autoscrollstep <- boundastep state.winh (int_of_string s);
2236 if state.autoscroll <> None
2237 then state.autoscroll <- Some conf.autoscrollstep
2238 with exn ->
2239 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2241 TEswitch ("auto scroll step: ", E.s, None, intentry, ondone, true)
2243 | 'C' ->
2244 let ondone s =
2246 let n, a, b = multicolumns_of_string s in
2247 setcolumns mode n a b;
2248 with exn ->
2249 state.text <- Printf.sprintf "bad columns `%s': %s" s @@ exntos exn
2251 TEswitch ("columns: ", E.s, None, textentry, ondone, true)
2253 | 'Z' ->
2254 let ondone s =
2256 let zoom = float (int_of_string s) /. 100.0 in
2257 pivotzoom zoom
2258 with exn ->
2259 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2261 TEswitch ("zoom: ", E.s, None, intentry, ondone, true)
2263 | 't' ->
2264 let ondone s =
2266 conf.thumbw <- bound (int_of_string s) 2 4096;
2267 state.text <-
2268 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2269 begin match mode with
2270 | Birdseye beye ->
2271 leavebirdseye beye false;
2272 enterbirdseye ();
2273 | Textentry _
2274 | View
2275 | LinkNav _ -> ();
2277 with exn ->
2278 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2280 TEswitch ("thumbnail width: ", E.s, None, intentry, ondone, true)
2282 | 'R' ->
2283 let ondone s =
2284 match int_of_string s with
2285 | angle -> reqlayout angle conf.fitmodel
2286 | exception exn ->
2287 state.text <-
2288 Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2290 TEswitch ("rotation: ", E.s, None, intentry, ondone, true)
2292 | 'i' ->
2293 conf.icase <- not conf.icase;
2294 TEdone ("case insensitive search " ^ (btos conf.icase))
2296 | 'p' ->
2297 conf.preload <- not conf.preload;
2298 gotoxy state.x state.y;
2299 TEdone ("preload " ^ (btos conf.preload))
2301 | 'v' ->
2302 conf.verbose <- not conf.verbose;
2303 TEdone ("verbose " ^ (btos conf.verbose))
2305 | 'd' ->
2306 conf.debug <- not conf.debug;
2307 TEdone ("debug " ^ (btos conf.debug))
2309 | 'h' ->
2310 conf.maxhfit <- not conf.maxhfit;
2311 state.maxy <- calcheight ();
2312 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2314 | 'c' ->
2315 conf.crophack <- not conf.crophack;
2316 TEdone ("crophack " ^ btos conf.crophack)
2318 | 'a' ->
2319 let s =
2320 match conf.maxwait with
2321 | None ->
2322 conf.maxwait <- Some infinity;
2323 "always wait for page to complete"
2324 | Some _ ->
2325 conf.maxwait <- None;
2326 "show placeholder if page is not ready"
2328 TEdone s
2330 | 'f' ->
2331 conf.underinfo <- not conf.underinfo;
2332 TEdone ("underinfo " ^ btos conf.underinfo)
2334 | 'P' ->
2335 conf.savebmarks <- not conf.savebmarks;
2336 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2338 | 'S' ->
2339 let ondone s =
2341 let pageno, py =
2342 match state.layout with
2343 | [] -> 0, 0
2344 | l :: _ ->
2345 l.pageno, l.pagey
2347 conf.interpagespace <- int_of_string s;
2348 docolumns conf.columns;
2349 state.maxy <- calcheight ();
2350 let y = getpagey pageno in
2351 gotoxy state.x (y + py)
2352 with exn ->
2353 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2355 TEswitch ("vertical margin: ", E.s, None, intentry, ondone, true)
2357 | 'l' ->
2358 let fm =
2359 match conf.fitmodel with
2360 | FitProportional -> FitWidth
2361 | FitWidth | FitPage -> FitProportional
2363 reqlayout conf.angle fm;
2364 TEdone ("proportional display " ^ btos (fm == FitProportional))
2366 | 'T' ->
2367 settrim (not conf.trimmargins) conf.trimfuzz;
2368 TEdone ("trim margins " ^ btos conf.trimmargins)
2370 | 'I' ->
2371 conf.invert <- not conf.invert;
2372 TEdone ("invert colors " ^ btos conf.invert)
2374 | 'x' ->
2375 let ondone s =
2376 cbput state.hists.sel s;
2377 conf.selcmd <- s;
2379 TEswitch ("selection command: ", E.s, Some (onhist state.hists.sel),
2380 textentry, ondone, true)
2382 | 'M' ->
2383 if conf.pax == None
2384 then conf.pax <- Some (ref (0.0, 0, 0))
2385 else conf.pax <- None;
2386 TEdone ("PAX " ^ btos (conf.pax != None))
2388 | _ ->
2389 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2390 TEstop
2391 else
2392 TEcont state.text
2395 class type lvsource = object
2396 method getitemcount : int
2397 method getitem : int -> (string * int)
2398 method hasaction : int -> bool
2399 method exit :
2400 uioh:uioh ->
2401 cancel:bool ->
2402 active:int ->
2403 first:int ->
2404 pan:int ->
2405 uioh option
2406 method getactive : int
2407 method getfirst : int
2408 method getpan : int
2409 method getminfo : (int * int) array
2410 end;;
2412 class virtual lvsourcebase = object
2413 val mutable m_active = 0
2414 val mutable m_first = 0
2415 val mutable m_pan = 0
2416 method getactive = m_active
2417 method getfirst = m_first
2418 method getpan = m_pan
2419 method getminfo : (int * int) array = E.a
2420 end;;
2422 let textentrykeyboard
2423 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2424 state.text <- E.s;
2425 let key = Wsi.keypadtodigitkey key in
2426 let enttext te =
2427 state.mode <- Textentry (te, onleave);
2428 enttext ();
2429 G.postRedisplay "textentrykeyboard enttext";
2431 let histaction cmd =
2432 match opthist with
2433 | None -> ()
2434 | Some (action, _) ->
2435 state.mode <- Textentry (
2436 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
2438 G.postRedisplay "textentry histaction"
2440 match key with
2441 | @backspace ->
2442 if emptystr text && cancelonempty
2443 then (
2444 onleave Cancel;
2445 G.postRedisplay "textentrykeyboard after cancel";
2447 else
2448 let s = withoutlastutf8 text in
2449 enttext (c, s, opthist, onkey, ondone, cancelonempty)
2451 | @enter | @kpenter ->
2452 ondone text;
2453 onleave Confirm;
2454 G.postRedisplay "textentrykeyboard after confirm"
2456 | @up | @kpup -> histaction HCprev
2457 | @down | @kpdown -> histaction HCnext
2458 | @home | @kphome -> histaction HCfirst
2459 | @jend | @kpend -> histaction HClast
2461 | @escape ->
2462 if emptystr text
2463 then (
2464 begin match opthist with
2465 | None -> ()
2466 | Some (_, onhistcancel) -> onhistcancel ()
2467 end;
2468 onleave Cancel;
2469 state.text <- E.s;
2470 G.postRedisplay "textentrykeyboard after cancel2"
2472 else (
2473 enttext (c, E.s, opthist, onkey, ondone, cancelonempty)
2476 | @delete | @kpdelete -> ()
2478 | _ when key != 0 && not (Wsi.isspecialkey key) ->
2479 begin match onkey text key with
2480 | TEdone text ->
2481 ondone text;
2482 onleave Confirm;
2483 G.postRedisplay "textentrykeyboard after confirm2";
2485 | TEcont text ->
2486 enttext (c, text, opthist, onkey, ondone, cancelonempty);
2488 | TEstop ->
2489 onleave Cancel;
2490 G.postRedisplay "textentrykeyboard after cancel3"
2492 | TEswitch te ->
2493 state.mode <- Textentry (te, onleave);
2494 G.postRedisplay "textentrykeyboard switch";
2495 end;
2497 | _ ->
2498 vlog "unhandled key %s" (Wsi.keyname key)
2501 let firstof first active =
2502 if first > active || abs (first - active) > fstate.maxrows - 1
2503 then max 0 (active - (fstate.maxrows/2))
2504 else first
2507 let calcfirst first active =
2508 if active > first
2509 then
2510 let rows = active - first in
2511 if rows > fstate.maxrows then active - fstate.maxrows else first
2512 else active
2515 let scrollph y maxy =
2516 let sh = float (maxy + state.winh) /. float state.winh in
2517 let sh = float state.winh /. sh in
2518 let sh = max sh (float conf.scrollh) in
2520 let percent = float y /. float maxy in
2521 let position = (float state.winh -. sh) *. percent in
2523 let position =
2524 if position +. sh > float state.winh
2525 then float state.winh -. sh
2526 else position
2528 position, sh;
2531 let adderrmsg src msg =
2532 Buffer.add_string state.errmsgs msg;
2533 state.newerrmsgs <- true;
2534 G.postRedisplay src
2537 let adderrfmt src fmt =
2538 Format.ksprintf (fun s -> adderrmsg src s) fmt;
2541 let coe s = (s :> uioh);;
2543 class listview ~zebra ~helpmode ~(source:lvsource) ~trusted ~modehash =
2544 object (self)
2545 val m_pan = source#getpan
2546 val m_first = source#getfirst
2547 val m_active = source#getactive
2548 val m_qsearch = E.s
2549 val m_prev_uioh = state.uioh
2551 method private elemunder y =
2552 if y < 0
2553 then None
2554 else
2555 let n = y / (fstate.fontsize+1) in
2556 if m_first + n < source#getitemcount
2557 then (
2558 if source#hasaction (m_first + n)
2559 then Some (m_first + n)
2560 else None
2562 else None
2564 method display =
2565 Gl.enable `blend;
2566 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
2567 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2568 filledrect 0. 0. (float state.winw) (float state.winh);
2569 GlDraw.color (1., 1., 1.);
2570 Gl.enable `texture_2d;
2571 let fs = fstate.fontsize in
2572 let nfs = fs + 1 in
2573 let hw = state.winw/3 in
2574 let ww = fstate.wwidth in
2575 let tabw = 17.0*.ww in
2576 let itemcount = source#getitemcount in
2577 let minfo = source#getminfo in
2578 if conf.leftscroll
2579 then (
2580 GlMat.push ();
2581 GlMat.translate ~x:(float conf.scrollbw) ();
2583 let x0 = 0.0 and x1 = float (state.winw - conf.scrollbw - 1) in
2584 let rec loop row =
2585 if (row - m_first) > fstate.maxrows
2586 then ()
2587 else (
2588 if row >= 0 && row < itemcount
2589 then (
2590 let (s, level) = source#getitem row in
2591 let y = (row - m_first) * nfs in
2592 let x = 5.0 +. (float (level + m_pan)) *. ww in
2593 if helpmode
2594 then GlDraw.color
2595 (let c = if row land 1 = 0 then 1.0 else 0.92 in (c,c,c));
2597 if row = m_active
2598 then (
2599 Gl.disable `texture_2d;
2600 let alpha = if source#hasaction row then 0.9 else 0.3 in
2601 GlDraw.color (1., 1., 1.) ~alpha;
2602 linerect (x0 +. 1.) (float (y + 1)) (x1) (float (y + fs + 3));
2603 Gl.enable `texture_2d;
2605 let c =
2606 if zebra && row land 1 = 1
2607 then 0.8
2608 else 1.0
2610 GlDraw.color (c,c,c);
2611 let drawtabularstring s =
2612 let drawstr x s =
2613 let x' = truncate (x0 +. x) in
2614 let pos = nindex s '\000' in
2615 if pos = -1
2616 then drawstring1 fs x' (y+nfs) s
2617 else
2618 let s1 = String.sub s 0 pos
2619 and s2 = String.sub s (pos+1) (String.length s - pos - 1) in
2620 let rec e s =
2621 if emptystr s
2622 then s
2623 else
2624 let s' = withoutlastutf8 s in
2625 let s = s' ^ "@Uellipsis" in
2626 let w = measurestr fs s in
2627 if float x' +. w +. ww < float (hw + x')
2628 then s
2629 else e s'
2631 let s1 =
2632 if float x' +. ww +. measurestr fs s1 > float (hw + x')
2633 then e s1
2634 else s1
2636 ignore (drawstring1 fs x' (y+nfs) s1);
2637 drawstring1 fs (hw + x') (y+nfs) s2
2639 if trusted
2640 then
2641 let x = if helpmode && row > 0 then x +. ww else x in
2642 let tabpos = nindex s '\t' in
2643 if tabpos > 0
2644 then
2645 let len = String.length s - tabpos - 1 in
2646 let s1 = String.sub s 0 tabpos
2647 and s2 = String.sub s (tabpos + 1) len in
2648 let nx = drawstr x s1 in
2649 let sw = nx -. x in
2650 let x = x +. (max tabw sw) in
2651 drawstr x s2
2652 else
2653 let len = String.length s - 2 in
2654 if len > 0 && s.[0] = '\xc2' && s.[1] = '\xb7'
2655 then
2656 let s = String.sub s 2 len in
2657 let x = if not helpmode then x +. ww else x in
2658 GlDraw.color (1.2, 1.2, 1.2);
2659 let vinc = drawstring1 (fs+fs/4)
2660 (truncate (x -. ww)) (y+nfs) s in
2661 GlDraw.color (1., 1., 1.);
2662 vinc +. (float fs *. 0.8)
2663 else
2664 drawstr x s
2665 else
2666 drawstr x s
2668 ignore (drawtabularstring s);
2669 loop (row+1)
2673 loop m_first;
2674 GlDraw.color (1.0, 1.0, 1.0) ~alpha:0.5;
2675 let xadj = 5.0 in
2676 let rec loop row =
2677 if (row - m_first) > fstate.maxrows
2678 then ()
2679 else (
2680 if row >= 0 && row < itemcount
2681 then (
2682 let (s, level) = source#getitem row in
2683 let pos0 = nindex s '\000' in
2684 let y = (row - m_first) * nfs in
2685 let x = float (level + m_pan) *. ww in
2686 let (first, last) = minfo.(row) in
2687 let prefix =
2688 if pos0 > 0 && first > pos0
2689 then String.sub s (pos0+1) (first-pos0-1)
2690 else String.sub s 0 first
2692 let suffix = String.sub s first (last - first) in
2693 let w1 = measurestr fstate.fontsize prefix in
2694 let w2 = measurestr fstate.fontsize suffix in
2695 let x = x +. if conf.leftscroll then xadj else 5.0 in
2696 let x = if pos0 > 0 && first > pos0 then x +. float hw else x in
2697 let x0 = x +. w1
2698 and y0 = float (y+2) in
2699 let x1 = x0 +. w2
2700 and y1 = float (y+fs+3) in
2701 filledrect x0 y0 x1 y1;
2702 loop (row+1)
2706 Gl.disable `texture_2d;
2707 if Array.length minfo > 0 then loop m_first;
2708 Gl.disable `blend;
2709 if conf.leftscroll
2710 then GlMat.pop ();
2712 method updownlevel incr =
2713 let len = source#getitemcount in
2714 let curlevel =
2715 if m_active >= 0 && m_active < len
2716 then snd (source#getitem m_active)
2717 else -1
2719 let rec flow i =
2720 if i = len then i-1 else if i = -1 then 0 else
2721 let _, l = source#getitem i in
2722 if l != curlevel then i else flow (i+incr)
2724 let active = flow m_active in
2725 let first = calcfirst m_first active in
2726 G.postRedisplay "outline updownlevel";
2727 {< m_active = active; m_first = first >}
2729 method private key1 key mask =
2730 let set1 active first qsearch =
2731 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2733 let search active pattern incr =
2734 let active = if active = -1 then m_first else active in
2735 let dosearch re =
2736 let rec loop n =
2737 if n >= 0 && n < source#getitemcount
2738 then (
2739 let s, _ = source#getitem n in
2740 match Str.search_forward re s 0 with
2741 | (exception Not_found) -> loop (n + incr)
2742 | _ -> Some n
2744 else None
2746 loop active
2748 let qpat = Str.quote pattern in
2749 match Str.regexp_case_fold qpat with
2750 | s -> dosearch s
2751 | exception exn ->
2752 adderrfmt "listview key1" "regexp_case_fold for `%S' failed: %S\n"
2753 qpat @@ Printexc.to_string exn;
2754 None
2756 let itemcount = source#getitemcount in
2757 let find start incr =
2758 let rec find i =
2759 if i = -1 || i = itemcount
2760 then -1
2761 else (
2762 if source#hasaction i
2763 then i
2764 else find (i + incr)
2767 find start
2769 let set active first =
2770 let first = bound first 0 (itemcount - fstate.maxrows) in
2771 state.text <- E.s;
2772 coe {< m_active = active; m_first = first; m_qsearch = E.s >}
2774 let navigate incr =
2775 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2776 let active, first =
2777 let incr1 = if incr > 0 then 1 else -1 in
2778 if isvisible m_first m_active
2779 then
2780 let next =
2781 let next = m_active + incr in
2782 let next =
2783 if next < 0 || next >= itemcount
2784 then -1
2785 else find next incr1
2787 if abs (m_active - next) > fstate.maxrows
2788 then -1
2789 else next
2791 if next = -1
2792 then
2793 let first = m_first + incr in
2794 let first = bound first 0 (itemcount - fstate.maxrows) in
2795 let next =
2796 let next = m_active + incr in
2797 let next = bound next 0 (itemcount - 1) in
2798 find next ~-incr1
2800 let active =
2801 if next = -1
2802 then m_active
2803 else (
2804 if isvisible first next
2805 then next
2806 else m_active
2809 active, first
2810 else
2811 let first = min next m_first in
2812 let first =
2813 if abs (next - first) > fstate.maxrows
2814 then first + incr
2815 else first
2817 next, first
2818 else
2819 let first = m_first + incr in
2820 let first = bound first 0 (itemcount - 1) in
2821 let active =
2822 let next = m_active + incr in
2823 let next = bound next 0 (itemcount - 1) in
2824 let next = find next incr1 in
2825 let active =
2826 if next = -1 || abs (m_active - first) > fstate.maxrows
2827 then (
2828 let active = if m_active = -1 then next else m_active in
2829 active
2831 else next
2833 if isvisible first active
2834 then active
2835 else -1
2837 active, first
2839 G.postRedisplay "listview navigate";
2840 set active first;
2842 match key with
2843 | (@r|@s) when Wsi.withctrl mask ->
2844 let incr = if key = @r then -1 else 1 in
2845 let active, first =
2846 match search (m_active + incr) m_qsearch incr with
2847 | None ->
2848 state.text <- m_qsearch ^ " [not found]";
2849 m_active, m_first
2850 | Some active ->
2851 state.text <- m_qsearch;
2852 active, firstof m_first active
2854 G.postRedisplay "listview ctrl-r/s";
2855 set1 active first m_qsearch;
2857 | @insert when Wsi.withctrl mask ->
2858 if m_active >= 0 && m_active < source#getitemcount
2859 then (
2860 let s, _ = source#getitem m_active in
2861 selstring s;
2863 coe self
2865 | @backspace ->
2866 if emptystr m_qsearch
2867 then coe self
2868 else (
2869 let qsearch = withoutlastutf8 m_qsearch in
2870 if emptystr qsearch
2871 then (
2872 state.text <- E.s;
2873 G.postRedisplay "listview empty qsearch";
2874 set1 m_active m_first E.s;
2876 else
2877 let active, first =
2878 match search m_active qsearch ~-1 with
2879 | None ->
2880 state.text <- qsearch ^ " [not found]";
2881 m_active, m_first
2882 | Some active ->
2883 state.text <- qsearch;
2884 active, firstof m_first active
2886 G.postRedisplay "listview backspace qsearch";
2887 set1 active first qsearch
2890 | key when (key != 0 && not (Wsi.isspecialkey key)) ->
2891 let pattern = m_qsearch ^ toutf8 key in
2892 let active, first =
2893 match search m_active pattern 1 with
2894 | None ->
2895 state.text <- pattern ^ " [not found]";
2896 m_active, m_first
2897 | Some active ->
2898 state.text <- pattern;
2899 active, firstof m_first active
2901 G.postRedisplay "listview qsearch add";
2902 set1 active first pattern;
2904 | @escape ->
2905 state.text <- E.s;
2906 if emptystr m_qsearch
2907 then (
2908 G.postRedisplay "list view escape";
2909 let mx, my = state.mpos in
2910 updateunder mx my;
2911 begin
2912 match
2913 source#exit ~uioh:(coe self)
2914 ~cancel:true ~active:m_active ~first:m_first ~pan:m_pan
2915 with
2916 | None -> m_prev_uioh
2917 | Some uioh -> uioh
2920 else (
2921 G.postRedisplay "list view kill qsearch";
2922 coe {< m_qsearch = E.s >}
2925 | @enter | @kpenter ->
2926 state.text <- E.s;
2927 let self = {< m_qsearch = E.s >} in
2928 let opt =
2929 G.postRedisplay "listview enter";
2930 if m_active >= 0 && m_active < source#getitemcount
2931 then (
2932 source#exit ~uioh:(coe self) ~cancel:false
2933 ~active:m_active ~first:m_first ~pan:m_pan;
2935 else (
2936 source#exit ~uioh:(coe self) ~cancel:true
2937 ~active:m_active ~first:m_first ~pan:m_pan;
2940 begin match opt with
2941 | None -> m_prev_uioh
2942 | Some uioh -> uioh
2945 | @delete | @kpdelete ->
2946 coe self
2948 | @up | @kpup -> navigate ~-1
2949 | @down | @kpdown -> navigate 1
2950 | @prior | @kpprior -> navigate ~-(fstate.maxrows)
2951 | @next | @kpnext -> navigate fstate.maxrows
2953 | @right | @kpright ->
2954 state.text <- E.s;
2955 G.postRedisplay "listview right";
2956 coe {< m_pan = m_pan - 1 >}
2958 | @left | @kpleft ->
2959 state.text <- E.s;
2960 G.postRedisplay "listview left";
2961 coe {< m_pan = m_pan + 1 >}
2963 | @home | @kphome ->
2964 let active = find 0 1 in
2965 G.postRedisplay "listview home";
2966 set active 0;
2968 | @jend | @kpend ->
2969 let first = max 0 (itemcount - fstate.maxrows) in
2970 let active = find (itemcount - 1) ~-1 in
2971 G.postRedisplay "listview end";
2972 set active first;
2974 | key when (key = 0 || Wsi.isspecialkey key) ->
2975 coe self
2977 | _ ->
2978 dolog "listview unknown key %#x" key; coe self
2980 method key key mask =
2981 match state.mode with
2982 | Textentry te -> textentrykeyboard key mask te; coe self
2983 | Birdseye _
2984 | View
2985 | LinkNav _ -> self#key1 key mask
2987 method button button down x y _ =
2988 let opt =
2989 match button with
2990 | 1 when vscrollhit x ->
2991 G.postRedisplay "listview scroll";
2992 if down
2993 then
2994 let _, position, sh = self#scrollph in
2995 if y > truncate position && y < truncate (position +. sh)
2996 then (
2997 state.mstate <- Mscrolly;
2998 Some (coe self)
3000 else
3001 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3002 let first = truncate (s *. float source#getitemcount) in
3003 let first = min source#getitemcount first in
3004 Some (coe {< m_first = first; m_active = first >})
3005 else (
3006 state.mstate <- Mnone;
3007 Some (coe self);
3009 | 1 when down ->
3010 begin match self#elemunder y with
3011 | Some n ->
3012 G.postRedisplay "listview click";
3013 source#exit ~uioh:(coe {< m_active = n >})
3014 ~cancel:false ~active:n ~first:m_first ~pan:m_pan
3015 | _ ->
3016 Some (coe self)
3018 | n when (n == 4 || n == 5) && not down ->
3019 let len = source#getitemcount in
3020 let first =
3021 if n = 5 && m_first + fstate.maxrows >= len
3022 then
3023 m_first
3024 else
3025 let first = m_first + (if n == 4 then -1 else 1) in
3026 bound first 0 (len - 1)
3028 G.postRedisplay "listview wheel";
3029 Some (coe {< m_first = first >})
3030 | n when (n = 6 || n = 7) && not down ->
3031 let inc = if n = 7 then -1 else 1 in
3032 G.postRedisplay "listview hwheel";
3033 Some (coe {< m_pan = m_pan + inc >})
3034 | _ ->
3035 Some (coe self)
3037 match opt with
3038 | None -> m_prev_uioh
3039 | Some uioh -> uioh
3041 method multiclick _ x y = self#button 1 true x y
3043 method motion _ y =
3044 match state.mstate with
3045 | Mscrolly ->
3046 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3047 let first = truncate (s *. float source#getitemcount) in
3048 let first = min source#getitemcount first in
3049 G.postRedisplay "listview motion";
3050 coe {< m_first = first; m_active = first >}
3051 | Msel _
3052 | Mpan _
3053 | Mscrollx
3054 | Mzoom _
3055 | Mzoomrect _
3056 | Mnone -> coe self
3058 method pmotion x y =
3059 if x < state.winw - conf.scrollbw
3060 then
3061 let n =
3062 match self#elemunder y with
3063 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3064 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3066 let o =
3067 if n != m_active
3068 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3069 else self
3071 coe o
3072 else (
3073 Wsi.setcursor Wsi.CURSOR_INHERIT;
3074 coe self
3077 method infochanged _ = ()
3079 method scrollpw = (0, 0.0, 0.0)
3080 method scrollph =
3081 let nfs = fstate.fontsize + 1 in
3082 let y = m_first * nfs in
3083 let itemcount = source#getitemcount in
3084 let maxi = max 0 (itemcount - fstate.maxrows) in
3085 let maxy = maxi * nfs in
3086 let p, h = scrollph y maxy in
3087 conf.scrollbw, p, h
3089 method modehash = modehash
3090 method eformsgs = false
3091 method alwaysscrolly = true
3092 end;;
3094 class outlinelistview ~zebra ~source =
3095 let settext autonarrow s =
3096 if autonarrow
3097 then
3098 let ss = source#statestr in
3099 state.text <-
3100 if emptystr ss
3101 then "[" ^ s ^ "]"
3102 else "{" ^ ss ^ "} [" ^ s ^ "]"
3103 else state.text <- s
3105 object (self)
3106 inherit listview
3107 ~zebra
3108 ~helpmode:false
3109 ~source:(source :> lvsource)
3110 ~trusted:false
3111 ~modehash:(findkeyhash conf "outline")
3112 as super
3114 val m_autonarrow = false
3116 method! key key mask =
3117 let maxrows =
3118 if emptystr state.text
3119 then fstate.maxrows
3120 else fstate.maxrows - 2
3122 let calcfirst first active =
3123 if active > first
3124 then
3125 let rows = active - first in
3126 if rows > maxrows then active - maxrows else first
3127 else active
3129 let navigate incr =
3130 let active = m_active + incr in
3131 let active = bound active 0 (source#getitemcount - 1) in
3132 let first = calcfirst m_first active in
3133 G.postRedisplay "outline navigate";
3134 coe {< m_active = active; m_first = first >}
3136 let navscroll first =
3137 let active =
3138 let dist = m_active - first in
3139 if dist < 0
3140 then first
3141 else (
3142 if dist < maxrows
3143 then m_active
3144 else first + maxrows
3147 G.postRedisplay "outline navscroll";
3148 coe {< m_first = first; m_active = active >}
3150 let ctrl = Wsi.withctrl mask in
3151 match key with
3152 | @a when ctrl ->
3153 let text =
3154 if m_autonarrow
3155 then (source#denarrow; E.s)
3156 else (
3157 let pattern = source#renarrow in
3158 if nonemptystr m_qsearch
3159 then (source#narrow m_qsearch; m_qsearch)
3160 else pattern
3163 settext (not m_autonarrow) text;
3164 G.postRedisplay "toggle auto narrowing";
3165 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
3167 | @slash when emptystr m_qsearch && not m_autonarrow ->
3168 settext true E.s;
3169 G.postRedisplay "toggle auto narrowing";
3170 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
3172 | @n when ctrl ->
3173 source#narrow m_qsearch;
3174 if not m_autonarrow
3175 then source#add_narrow_pattern m_qsearch;
3176 G.postRedisplay "outline ctrl-n";
3177 coe {< m_first = 0; m_active = 0 >}
3179 | @S when ctrl ->
3180 let active = source#calcactive (getanchor ()) in
3181 let first = firstof m_first active in
3182 G.postRedisplay "outline ctrl-s";
3183 coe {< m_first = first; m_active = active >}
3185 | @u when ctrl ->
3186 G.postRedisplay "outline ctrl-u";
3187 if m_autonarrow && nonemptystr m_qsearch
3188 then (
3189 ignore (source#renarrow);
3190 settext m_autonarrow E.s;
3191 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3193 else (
3194 source#del_narrow_pattern;
3195 let pattern = source#renarrow in
3196 let text =
3197 if emptystr pattern then E.s else "Narrowed to " ^ pattern
3199 settext m_autonarrow text;
3200 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3203 | @l when ctrl ->
3204 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3205 G.postRedisplay "outline ctrl-l";
3206 coe {< m_first = first >}
3208 | @tab when m_autonarrow ->
3209 if nonemptystr m_qsearch
3210 then (
3211 G.postRedisplay "outline list view tab";
3212 source#add_narrow_pattern m_qsearch;
3213 settext true E.s;
3214 coe {< m_qsearch = E.s >}
3216 else coe self
3218 | @escape when m_autonarrow ->
3219 if nonemptystr m_qsearch
3220 then source#add_narrow_pattern m_qsearch;
3221 super#key key mask
3223 | @enter | @kpenter when m_autonarrow ->
3224 if nonemptystr m_qsearch
3225 then source#add_narrow_pattern m_qsearch;
3226 super#key key mask
3228 | key when m_autonarrow && (not (Wsi.isspecialkey key)) ->
3229 let pattern = m_qsearch ^ toutf8 key in
3230 G.postRedisplay "outlinelistview autonarrow add";
3231 source#narrow pattern;
3232 settext true pattern;
3233 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3235 | key when m_autonarrow && key = @backspace ->
3236 if emptystr m_qsearch
3237 then coe self
3238 else
3239 let pattern = withoutlastutf8 m_qsearch in
3240 G.postRedisplay "outlinelistview autonarrow backspace";
3241 ignore (source#renarrow);
3242 source#narrow pattern;
3243 settext true pattern;
3244 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3246 | @up | @kpup when ctrl ->
3247 navscroll (max 0 (m_first - 1))
3249 | @down | @kpdown when ctrl ->
3250 navscroll (min (source#getitemcount - 1) (m_first + 1))
3252 | @up | @kpup -> navigate ~-1
3253 | @down | @kpdown -> navigate 1
3254 | @prior | @kpprior -> navigate ~-(fstate.maxrows)
3255 | @next | @kpnext -> navigate fstate.maxrows
3257 | @right | @kpright ->
3258 let o =
3259 if ctrl
3260 then (
3261 G.postRedisplay "outline ctrl right";
3262 {< m_pan = m_pan + 1 >}
3264 else self#updownlevel 1
3266 coe o
3268 | @left | @kpleft ->
3269 let o =
3270 if ctrl
3271 then (
3272 G.postRedisplay "outline ctrl left";
3273 {< m_pan = m_pan - 1 >}
3275 else self#updownlevel ~-1
3277 coe o
3279 | @home | @kphome ->
3280 G.postRedisplay "outline home";
3281 coe {< m_first = 0; m_active = 0 >}
3283 | @jend | @kpend ->
3284 let active = source#getitemcount - 1 in
3285 let first = max 0 (active - fstate.maxrows) in
3286 G.postRedisplay "outline end";
3287 coe {< m_active = active; m_first = first >}
3289 | _ -> super#key key mask
3290 end;;
3292 let genhistoutlines () =
3293 Config.gethist ()
3294 |> List.sort (fun (_, c1, _, _, _, _) (_, c2, _, _, _, _) ->
3295 compare c2.lastvisit c1.lastvisit)
3296 |> List.map
3297 (fun ((path, c, _, _, _, origin) as hist) ->
3298 let path = if nonemptystr origin then origin else path in
3299 let base = mbtoutf8 @@ Filename.basename path in
3300 (base ^ "\000" ^ c.title, 1, Ohistory hist)
3302 |> Array.of_list
3305 let gotohist (path, c, bookmarks, x, anchor, origin) =
3306 Config.save leavebirdseye;
3307 state.anchor <- anchor;
3308 state.bookmarks <- bookmarks;
3309 state.origin <- origin;
3310 state.x <- x;
3311 setconf conf c;
3312 let x0, y0, x1, y1 = conf.trimfuzz in
3313 wcmd "trimset %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1;
3314 reshape ~firsttime:true state.winw state.winh;
3315 opendoc path origin;
3316 setzoom c.zoom;
3319 let makecheckers () =
3320 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3321 following to say:
3322 converted by Issac Trotts. July 25, 2002 *)
3323 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3324 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3325 let id = GlTex.gen_texture () in
3326 GlTex.bind_texture ~target:`texture_2d id;
3327 GlPix.store (`unpack_alignment 1);
3328 GlTex.image2d image;
3329 List.iter (GlTex.parameter ~target:`texture_2d)
3330 [ `mag_filter `nearest; `min_filter `nearest ];
3334 let setcheckers enabled =
3335 match state.checkerstexid with
3336 | None ->
3337 if enabled then state.checkerstexid <- Some (makecheckers ())
3339 | Some checkerstexid ->
3340 if not enabled
3341 then (
3342 GlTex.delete_texture checkerstexid;
3343 state.checkerstexid <- None;
3347 let describe_location () =
3348 let fn = page_of_y state.y in
3349 let ln = page_of_y (state.y + state.winh - 1) in
3350 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
3351 let percent =
3352 if maxy <= 0
3353 then 100.
3354 else (100. *. (float state.y /. float maxy))
3356 if fn = ln
3357 then
3358 Printf.sprintf "page %d of %d [%.2f%%]"
3359 (fn+1) state.pagecount percent
3360 else
3361 Printf.sprintf
3362 "pages %d-%d of %d [%.2f%%]"
3363 (fn+1) (ln+1) state.pagecount percent
3366 let setpresentationmode v =
3367 let n = page_of_y state.y in
3368 state.anchor <- (n, 0.0, 1.0);
3369 conf.presentation <- v;
3370 if conf.fitmodel = FitPage
3371 then reqlayout conf.angle conf.fitmodel;
3372 represent ();
3375 let setbgcol (r, g, b) =
3376 let col =
3377 let r = r *. 255.0 |> truncate
3378 and g = g *. 255.0 |> truncate
3379 and b = b *. 255.0 |> truncate in
3380 r lsl 16 |> (lor) (g lsl 8) |> (lor) b
3382 Wsi.setwinbgcol col;
3385 let enterinfomode =
3386 let btos b = if b then "@Uradical" else E.s in
3387 let showextended = ref false in
3388 let leave mode _ = state.mode <- mode in
3389 let src =
3390 (object
3391 val mutable m_l = []
3392 val mutable m_a = E.a
3393 val mutable m_prev_uioh = nouioh
3394 val mutable m_prev_mode = View
3396 inherit lvsourcebase
3398 method reset prev_mode prev_uioh =
3399 m_a <- Array.of_list (List.rev m_l);
3400 m_l <- [];
3401 m_prev_mode <- prev_mode;
3402 m_prev_uioh <- prev_uioh;
3404 method int name get set =
3405 m_l <-
3406 (name, `int get, 1, Action (
3407 fun u ->
3408 let ondone s =
3409 try set (int_of_string s)
3410 with exn ->
3411 state.text <- Printf.sprintf "bad integer `%s': %s"
3412 s @@ exntos exn
3414 state.text <- E.s;
3415 let te = name ^ ": ", E.s, None, intentry, ondone, true in
3416 state.mode <- Textentry (te, leave m_prev_mode);
3418 )) :: m_l
3420 method int_with_suffix name get set =
3421 m_l <-
3422 (name, `intws get, 1, Action (
3423 fun u ->
3424 let ondone s =
3425 try set (int_of_string_with_suffix s)
3426 with exn ->
3427 state.text <- Printf.sprintf "bad integer `%s': %s"
3428 s @@ exntos exn
3430 state.text <- E.s;
3431 let te =
3432 name ^ ": ", E.s, None, intentry_with_suffix, ondone, true
3434 state.mode <- Textentry (te, leave m_prev_mode);
3436 )) :: m_l
3438 method bool ?(offset=1) ?(btos=btos) name get set =
3439 m_l <-
3440 (name, `bool (btos, get), offset, Action (
3441 fun u ->
3442 let v = get () in
3443 set (not v);
3445 )) :: m_l
3447 method color name get set =
3448 m_l <-
3449 (name, `color get, 1, Action (
3450 fun u ->
3451 let invalid = (nan, nan, nan) in
3452 let ondone s =
3453 let c =
3454 try color_of_string s
3455 with exn ->
3456 state.text <- Printf.sprintf "bad color `%s': %s"
3457 s @@ exntos exn;
3458 invalid
3460 if c <> invalid
3461 then set c;
3463 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3464 state.text <- color_to_string (get ());
3465 state.mode <- Textentry (te, leave m_prev_mode);
3467 )) :: m_l
3469 method string name get set =
3470 m_l <-
3471 (name, `string get, 1, Action (
3472 fun u ->
3473 let ondone s = set s in
3474 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3475 state.mode <- Textentry (te, leave m_prev_mode);
3477 )) :: m_l
3479 method colorspace name get set =
3480 m_l <-
3481 (name, `string get, 1, Action (
3482 fun _ ->
3483 let source =
3484 (object
3485 inherit lvsourcebase
3487 initializer
3488 m_active <- CSTE.to_int conf.colorspace;
3489 m_first <- 0;
3491 method getitemcount =
3492 Array.length CSTE.names
3493 method getitem n =
3494 (CSTE.names.(n), 0)
3495 method exit ~uioh ~cancel ~active ~first ~pan =
3496 ignore (uioh, first, pan);
3497 if not cancel then set active;
3498 None
3499 method hasaction _ = true
3500 end)
3502 state.text <- E.s;
3503 let modehash = findkeyhash conf "info" in
3504 coe (new listview ~zebra:false ~helpmode:false
3505 ~source ~trusted:true ~modehash)
3506 )) :: m_l
3508 method paxmark name get set =
3509 m_l <-
3510 (name, `string get, 1, Action (
3511 fun _ ->
3512 let source =
3513 (object
3514 inherit lvsourcebase
3516 initializer
3517 m_active <- MTE.to_int conf.paxmark;
3518 m_first <- 0;
3520 method getitemcount = Array.length MTE.names
3521 method getitem n = (MTE.names.(n), 0)
3522 method exit ~uioh ~cancel ~active ~first ~pan =
3523 ignore (uioh, first, pan);
3524 if not cancel then set active;
3525 None
3526 method hasaction _ = true
3527 end)
3529 state.text <- E.s;
3530 let modehash = findkeyhash conf "info" in
3531 coe (new listview ~zebra:false ~helpmode:false
3532 ~source ~trusted:true ~modehash)
3533 )) :: m_l
3535 method fitmodel name get set =
3536 m_l <-
3537 (name, `string get, 1, Action (
3538 fun _ ->
3539 let source =
3540 (object
3541 inherit lvsourcebase
3543 initializer
3544 m_active <- FMTE.to_int conf.fitmodel;
3545 m_first <- 0;
3547 method getitemcount = Array.length FMTE.names
3548 method getitem n = (FMTE.names.(n), 0)
3549 method exit ~uioh ~cancel ~active ~first ~pan =
3550 ignore (uioh, first, pan);
3551 if not cancel then set active;
3552 None
3553 method hasaction _ = true
3554 end)
3556 state.text <- E.s;
3557 let modehash = findkeyhash conf "info" in
3558 coe (new listview ~zebra:false ~helpmode:false
3559 ~source ~trusted:true ~modehash)
3560 )) :: m_l
3562 method caption s offset =
3563 m_l <- (s, `empty, offset, Noaction) :: m_l
3565 method caption2 s f offset =
3566 m_l <- (s, `string f, offset, Noaction) :: m_l
3568 method getitemcount = Array.length m_a
3570 method getitem n =
3571 let tostr = function
3572 | `int f -> string_of_int (f ())
3573 | `intws f -> string_with_suffix_of_int (f ())
3574 | `string f -> f ()
3575 | `color f -> color_to_string (f ())
3576 | `bool (btos, f) -> btos (f ())
3577 | `empty -> E.s
3579 let name, t, offset, _ = m_a.(n) in
3580 ((let s = tostr t in
3581 if nonemptystr s
3582 then Printf.sprintf "%s\t%s" name s
3583 else name),
3584 offset)
3586 method exit ~uioh ~cancel ~active ~first ~pan =
3587 let uiohopt =
3588 if not cancel
3589 then (
3590 let uioh =
3591 match m_a.(active) with
3592 | _, _, _, Action f -> f uioh
3593 | _, _, _, Noaction -> uioh
3595 Some uioh
3597 else None
3599 m_active <- active;
3600 m_first <- first;
3601 m_pan <- pan;
3602 uiohopt
3604 method hasaction n =
3605 match m_a.(n) with
3606 | _, _, _, Action _ -> true
3607 | _, _, _, Noaction -> false
3609 initializer m_active <- 1
3610 end)
3612 let rec fillsrc prevmode prevuioh =
3613 let sep () = src#caption E.s 0 in
3614 let colorp name get set =
3615 src#string name
3616 (fun () -> color_to_string (get ()))
3617 (fun v ->
3619 let c = color_of_string v in
3620 set c
3621 with exn ->
3622 state.text <- Printf.sprintf "bad color `%s': %s" v @@ exntos exn
3625 let oldmode = state.mode in
3626 let birdseye = isbirdseye state.mode in
3628 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3630 src#bool "presentation mode"
3631 (fun () -> conf.presentation)
3632 (fun v -> setpresentationmode v);
3634 src#bool "ignore case in searches"
3635 (fun () -> conf.icase)
3636 (fun v -> conf.icase <- v);
3638 src#bool "preload"
3639 (fun () -> conf.preload)
3640 (fun v -> conf.preload <- v);
3642 src#bool "highlight links"
3643 (fun () -> conf.hlinks)
3644 (fun v -> conf.hlinks <- v);
3646 src#bool "under info"
3647 (fun () -> conf.underinfo)
3648 (fun v -> conf.underinfo <- v);
3650 src#bool "persistent bookmarks"
3651 (fun () -> conf.savebmarks)
3652 (fun v -> conf.savebmarks <- v);
3654 src#fitmodel "fit model"
3655 (fun () -> FMTE.to_string conf.fitmodel)
3656 (fun v -> reqlayout conf.angle (FMTE.of_int v));
3658 src#bool "trim margins"
3659 (fun () -> conf.trimmargins)
3660 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3662 src#bool "persistent location"
3663 (fun () -> conf.jumpback)
3664 (fun v -> conf.jumpback <- v);
3666 sep ();
3667 src#int "inter-page space"
3668 (fun () -> conf.interpagespace)
3669 (fun n ->
3670 conf.interpagespace <- n;
3671 docolumns conf.columns;
3672 let pageno, py =
3673 match state.layout with
3674 | [] -> 0, 0
3675 | l :: _ ->
3676 l.pageno, l.pagey
3678 state.maxy <- calcheight ();
3679 let y = getpagey pageno in
3680 gotoxy state.x (y + py)
3683 src#int "page bias"
3684 (fun () -> conf.pagebias)
3685 (fun v -> conf.pagebias <- v);
3687 src#int "scroll step"
3688 (fun () -> conf.scrollstep)
3689 (fun n -> conf.scrollstep <- n);
3691 src#int "horizontal scroll step"
3692 (fun () -> conf.hscrollstep)
3693 (fun v -> conf.hscrollstep <- v);
3695 src#int "auto scroll step"
3696 (fun () ->
3697 match state.autoscroll with
3698 | Some step -> step
3699 | _ -> conf.autoscrollstep)
3700 (fun n ->
3701 let n = boundastep state.winh n in
3702 if state.autoscroll <> None
3703 then state.autoscroll <- Some n;
3704 conf.autoscrollstep <- n);
3706 src#int "zoom"
3707 (fun () -> truncate (conf.zoom *. 100.))
3708 (fun v -> pivotzoom ((float v) /. 100.));
3710 src#int "rotation"
3711 (fun () -> conf.angle)
3712 (fun v -> reqlayout v conf.fitmodel);
3714 src#int "scroll bar width"
3715 (fun () -> conf.scrollbw)
3716 (fun v ->
3717 conf.scrollbw <- v;
3718 reshape state.winw state.winh;
3721 src#int "scroll handle height"
3722 (fun () -> conf.scrollh)
3723 (fun v -> conf.scrollh <- v;);
3725 src#int "thumbnail width"
3726 (fun () -> conf.thumbw)
3727 (fun v ->
3728 conf.thumbw <- min 4096 v;
3729 match oldmode with
3730 | Birdseye beye ->
3731 leavebirdseye beye false;
3732 enterbirdseye ()
3733 | Textentry _
3734 | View
3735 | LinkNav _ -> ()
3738 let mode = state.mode in
3739 src#string "columns"
3740 (fun () ->
3741 match conf.columns with
3742 | Csingle _ -> "1"
3743 | Cmulti (multi, _) -> multicolumns_to_string multi
3744 | Csplit (count, _) -> "-" ^ string_of_int count
3746 (fun v ->
3747 let n, a, b = multicolumns_of_string v in
3748 setcolumns mode n a b);
3750 sep ();
3751 src#caption "Pixmap cache" 0;
3752 src#int_with_suffix "size (advisory)"
3753 (fun () -> conf.memlimit)
3754 (fun v -> conf.memlimit <- v);
3756 src#caption2 "used"
3757 (fun () -> Printf.sprintf "%s bytes, %d tiles"
3758 (string_with_suffix_of_int state.memused)
3759 (Hashtbl.length state.tilemap)) 1;
3761 sep ();
3762 src#caption "Layout" 0;
3763 src#caption2 "Dimension"
3764 (fun () ->
3765 Printf.sprintf "%dx%d (virtual %dx%d)"
3766 state.winw state.winh
3767 state.w state.maxy)
3769 if conf.debug
3770 then
3771 src#caption2 "Position" (fun () ->
3772 Printf.sprintf "%dx%d" state.x state.y
3774 else
3775 src#caption2 "Position" (fun () -> describe_location ()) 1
3778 sep ();
3779 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3780 "Save these parameters as global defaults at exit"
3781 (fun () -> conf.bedefault)
3782 (fun v -> conf.bedefault <- v)
3785 sep ();
3786 let btos b = if b then "@Ulguillemet" else "@Urguillemet" in
3787 src#bool ~offset:0 ~btos "Extended parameters"
3788 (fun () -> !showextended)
3789 (fun v -> showextended := v; fillsrc prevmode prevuioh);
3790 if !showextended
3791 then (
3792 src#bool "checkers"
3793 (fun () -> conf.checkers)
3794 (fun v -> conf.checkers <- v; setcheckers v);
3795 src#bool "update cursor"
3796 (fun () -> conf.updatecurs)
3797 (fun v -> conf.updatecurs <- v);
3798 src#bool "scroll-bar on the left"
3799 (fun () -> conf.leftscroll)
3800 (fun v -> conf.leftscroll <- v);
3801 src#bool "verbose"
3802 (fun () -> conf.verbose)
3803 (fun v -> conf.verbose <- v);
3804 src#bool "invert colors"
3805 (fun () -> conf.invert)
3806 (fun v -> conf.invert <- v);
3807 src#bool "max fit"
3808 (fun () -> conf.maxhfit)
3809 (fun v -> conf.maxhfit <- v);
3810 src#bool "pax mode"
3811 (fun () -> conf.pax != None)
3812 (fun v ->
3813 if v
3814 then conf.pax <- Some (ref (now (), 0, 0))
3815 else conf.pax <- None);
3816 src#string "uri launcher"
3817 (fun () -> conf.urilauncher)
3818 (fun v -> conf.urilauncher <- v);
3819 src#string "path launcher"
3820 (fun () -> conf.pathlauncher)
3821 (fun v -> conf.pathlauncher <- v);
3822 src#string "tile size"
3823 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3824 (fun v ->
3826 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
3827 conf.tilew <- max 64 w;
3828 conf.tileh <- max 64 h;
3829 flushtiles ();
3830 with exn ->
3831 state.text <- Printf.sprintf "bad tile size `%s': %s"
3832 v @@ exntos exn
3834 src#int "texture count"
3835 (fun () -> conf.texcount)
3836 (fun v ->
3837 if realloctexts v
3838 then conf.texcount <- v
3839 else impmsg "failed to set texture count please retry later"
3841 src#int "slice height"
3842 (fun () -> conf.sliceheight)
3843 (fun v ->
3844 conf.sliceheight <- v;
3845 wcmd "sliceh %d" conf.sliceheight;
3847 src#int "anti-aliasing level"
3848 (fun () -> conf.aalevel)
3849 (fun v ->
3850 conf.aalevel <- bound v 0 8;
3851 state.anchor <- getanchor ();
3852 opendoc state.path state.password;
3854 src#string "page scroll scaling factor"
3855 (fun () -> string_of_float conf.pgscale)
3856 (fun v ->
3858 let s = float_of_string v in
3859 conf.pgscale <- s
3860 with exn ->
3861 state.text <- Printf.sprintf
3862 "bad page scroll scaling factor `%s': %s" v @@ exntos exn
3865 src#int "ui font size"
3866 (fun () -> fstate.fontsize)
3867 (fun v -> setfontsize (bound v 5 100));
3868 src#int "hint font size"
3869 (fun () -> conf.hfsize)
3870 (fun v -> conf.hfsize <- bound v 5 100);
3871 colorp "background color"
3872 (fun () -> conf.bgcolor)
3873 (fun v -> conf.bgcolor <- v; setbgcol v);
3874 src#bool "crop hack"
3875 (fun () -> conf.crophack)
3876 (fun v -> conf.crophack <- v);
3877 src#string "trim fuzz"
3878 (fun () -> irect_to_string conf.trimfuzz)
3879 (fun v ->
3881 conf.trimfuzz <- irect_of_string v;
3882 if conf.trimmargins
3883 then settrim true conf.trimfuzz;
3884 with exn ->
3885 state.text <- Printf.sprintf "bad irect `%s': %s" v @@ exntos exn
3887 src#string "throttle"
3888 (fun () ->
3889 match conf.maxwait with
3890 | None -> "show place holder if page is not ready"
3891 | Some time ->
3892 if time = infinity
3893 then "wait for page to fully render"
3894 else
3895 "wait " ^ string_of_float time
3896 ^ " seconds before showing placeholder"
3898 (fun v ->
3900 let f = float_of_string v in
3901 if f <= 0.0
3902 then conf.maxwait <- None
3903 else conf.maxwait <- Some f
3904 with exn ->
3905 state.text <- Printf.sprintf "bad time `%s': %s" v @@ exntos exn
3907 src#string "ghyll scroll"
3908 (fun () ->
3909 match conf.ghyllscroll with
3910 | None -> E.s
3911 | Some nab -> ghyllscroll_to_string nab
3913 (fun v ->
3914 try conf.ghyllscroll <- ghyllscroll_of_string v
3915 with
3916 | Failure msg ->
3917 state.text <- Printf.sprintf "bad ghyll `%s': %s" v msg
3918 | exn ->
3919 state.text <- Printf.sprintf "bad ghyll `%s': %s" v @@ exntos exn
3921 src#string "selection command"
3922 (fun () -> conf.selcmd)
3923 (fun v -> conf.selcmd <- v);
3924 src#string "synctex command"
3925 (fun () -> conf.stcmd)
3926 (fun v -> conf.stcmd <- v);
3927 src#string "pax command"
3928 (fun () -> conf.paxcmd)
3929 (fun v -> conf.paxcmd <- v);
3930 src#string "ask password command"
3931 (fun () -> conf.passcmd)
3932 (fun v -> conf.passcmd <- v);
3933 src#string "save path command"
3934 (fun () -> conf.savecmd)
3935 (fun v -> conf.savecmd <- v);
3936 src#colorspace "color space"
3937 (fun () -> CSTE.to_string conf.colorspace)
3938 (fun v ->
3939 conf.colorspace <- CSTE.of_int v;
3940 wcmd "cs %d" v;
3941 load state.layout;
3943 src#paxmark "pax mark method"
3944 (fun () -> MTE.to_string conf.paxmark)
3945 (fun v -> conf.paxmark <- MTE.of_int v);
3946 if bousable () && !opengl_has_pbo
3947 then
3948 src#bool "use PBO"
3949 (fun () -> conf.usepbo)
3950 (fun v -> conf.usepbo <- v);
3951 src#bool "mouse wheel scrolls pages"
3952 (fun () -> conf.wheelbypage)
3953 (fun v -> conf.wheelbypage <- v);
3954 src#bool "open remote links in a new instance"
3955 (fun () -> conf.riani)
3956 (fun v -> conf.riani <- v);
3957 src#bool "edit annotations inline"
3958 (fun () -> conf.annotinline)
3959 (fun v -> conf.annotinline <- v);
3960 src#bool "coarse positioning in presentation mode"
3961 (fun () -> conf.coarseprespos)
3962 (fun v -> conf.coarseprespos <- v);
3965 sep ();
3966 src#caption "Document" 0;
3967 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
3968 src#caption2 "Pages"
3969 (fun () -> string_of_int state.pagecount) 1;
3970 src#caption2 "Dimensions"
3971 (fun () -> string_of_int (List.length state.pdims)) 1;
3972 if conf.trimmargins
3973 then (
3974 sep ();
3975 src#caption "Trimmed margins" 0;
3976 src#caption2 "Dimensions"
3977 (fun () -> string_of_int (List.length state.pdims)) 1;
3980 sep ();
3981 src#caption "OpenGL" 0;
3982 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
3983 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
3985 sep ();
3986 src#caption "Location" 0;
3987 if nonemptystr state.origin
3988 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
3989 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
3991 src#reset prevmode prevuioh;
3993 fun () ->
3994 state.text <- E.s;
3995 resetmstate ();
3996 let prevmode = state.mode
3997 and prevuioh = state.uioh in
3998 fillsrc prevmode prevuioh;
3999 let source = (src :> lvsource) in
4000 let modehash = findkeyhash conf "info" in
4001 state.uioh <- coe (object (self)
4002 inherit listview ~zebra:false ~helpmode:false
4003 ~source ~trusted:true ~modehash as super
4004 val mutable m_prevmemused = 0
4005 method! infochanged = function
4006 | Memused ->
4007 if m_prevmemused != state.memused
4008 then (
4009 m_prevmemused <- state.memused;
4010 G.postRedisplay "memusedchanged";
4012 | Pdim -> G.postRedisplay "pdimchanged"
4013 | Docinfo -> fillsrc prevmode prevuioh
4015 method! key key mask =
4016 if not (Wsi.withctrl mask)
4017 then
4018 match key with
4019 | @left | @kpleft -> coe (self#updownlevel ~-1)
4020 | @right | @kpright -> coe (self#updownlevel 1)
4021 | _ -> super#key key mask
4022 else super#key key mask
4023 end);
4024 G.postRedisplay "info";
4027 let enterhelpmode =
4028 let source =
4029 (object
4030 inherit lvsourcebase
4031 method getitemcount = Array.length state.help
4032 method getitem n =
4033 let s, l, _ = state.help.(n) in
4034 (s, l)
4036 method exit ~uioh ~cancel ~active ~first ~pan =
4037 let optuioh =
4038 if not cancel
4039 then (
4040 match state.help.(active) with
4041 | _, _, Action f -> Some (f uioh)
4042 | _, _, Noaction -> Some uioh
4044 else None
4046 m_active <- active;
4047 m_first <- first;
4048 m_pan <- pan;
4049 optuioh
4051 method hasaction n =
4052 match state.help.(n) with
4053 | _, _, Action _ -> true
4054 | _, _, Noaction -> false
4056 initializer
4057 m_active <- -1
4058 end)
4059 in fun () ->
4060 let modehash = findkeyhash conf "help" in
4061 resetmstate ();
4062 state.uioh <- coe (new listview
4063 ~zebra:false ~helpmode:true
4064 ~source ~trusted:true ~modehash);
4065 G.postRedisplay "help";
4068 let entermsgsmode =
4069 let msgsource =
4070 (object
4071 inherit lvsourcebase
4072 val mutable m_items = E.a
4074 method getitemcount = 1 + Array.length m_items
4076 method getitem n =
4077 if n = 0
4078 then "[Clear]", 0
4079 else m_items.(n-1), 0
4081 method exit ~uioh ~cancel ~active ~first ~pan =
4082 ignore uioh;
4083 if not cancel
4084 then (
4085 if active = 0
4086 then Buffer.clear state.errmsgs;
4088 m_active <- active;
4089 m_first <- first;
4090 m_pan <- pan;
4091 None
4093 method hasaction n =
4094 n = 0
4096 method reset =
4097 state.newerrmsgs <- false;
4098 let l = Str.split newlinere (Buffer.contents state.errmsgs) in
4099 m_items <- Array.of_list l
4101 initializer
4102 m_active <- 0
4103 end)
4104 in fun () ->
4105 state.text <- E.s;
4106 resetmstate ();
4107 msgsource#reset;
4108 let source = (msgsource :> lvsource) in
4109 let modehash = findkeyhash conf "listview" in
4110 state.uioh <- coe (object
4111 inherit listview ~zebra:false ~helpmode:false
4112 ~source ~trusted:false ~modehash as super
4113 method! display =
4114 if state.newerrmsgs
4115 then msgsource#reset;
4116 super#display
4117 end);
4118 G.postRedisplay "msgs";
4121 let getusertext s =
4122 let editor = getenvwithdef "EDITOR" E.s in
4123 if emptystr editor
4124 then E.s
4125 else
4126 let tmppath = Filename.temp_file "llpp" "note" in
4127 if nonemptystr s
4128 then (
4129 let oc = open_out tmppath in
4130 output_string oc s;
4131 close_out oc;
4133 let execstr = editor ^ " " ^ tmppath in
4134 let s =
4135 match spawn execstr [] with
4136 | (exception exn) ->
4137 impmsg "spawn(%S) failed: %s" execstr @@ exntos exn;
4139 | pid ->
4140 match Unix.waitpid [] pid with
4141 | (exception exn) ->
4142 impmsg "waitpid(%d) failed: %s" pid @@ exntos exn;
4144 | (_pid, status) ->
4145 match status with
4146 | Unix.WEXITED 0 -> filecontents tmppath
4147 | Unix.WEXITED n ->
4148 impmsg "editor process(%s) exited abnormally: %d" execstr n;
4150 | Unix.WSIGNALED n ->
4151 impmsg "editor process(%s) was killed by signal %d" execstr n;
4153 | Unix.WSTOPPED n ->
4154 impmsg "editor(%s) process was stopped by signal %d" execstr n;
4157 match Unix.unlink tmppath with
4158 | (exception exn) ->
4159 impmsg "failed to ulink %S: %s" tmppath @@ exntos exn;
4161 | () -> s
4164 let enterannotmode opaque slinkindex =
4165 let msgsource =
4166 (object
4167 inherit lvsourcebase
4168 val mutable m_text = E.s
4169 val mutable m_items = E.a
4171 method getitemcount = Array.length m_items
4173 method getitem n =
4174 let label, _func = m_items.(n) in
4175 label, 0
4177 method exit ~uioh ~cancel ~active ~first ~pan =
4178 ignore (uioh, first, pan);
4179 if not cancel
4180 then (
4181 let _label, func = m_items.(active) in
4182 func ()
4184 None
4186 method hasaction n = nonemptystr @@ fst m_items.(n)
4188 method reset s =
4189 let rec split accu b i =
4190 let p = b+i in
4191 if p = String.length s
4192 then (String.sub s b (p-b), unit) :: accu
4193 else
4194 if (i > 70 && s.[p] = ' ') || s.[p] = '\r' || s.[p] = '\n'
4195 then
4196 let ss = if i = 0 then E.s else String.sub s b i in
4197 split ((ss, unit)::accu) (p+1) 0
4198 else
4199 split accu b (i+1)
4201 let cleanup () =
4202 wcmd "freepage %s" (~> opaque);
4203 let keys =
4204 Hashtbl.fold (fun key opaque' accu ->
4205 if opaque' = opaque'
4206 then key :: accu else accu) state.pagemap []
4208 List.iter (Hashtbl.remove state.pagemap) keys;
4209 flushtiles ();
4210 gotoxy state.x state.y
4212 let dele () =
4213 delannot opaque slinkindex;
4214 cleanup ();
4216 let edit inline () =
4217 let update s =
4218 if emptystr s
4219 then dele ()
4220 else (
4221 modannot opaque slinkindex s;
4222 cleanup ();
4225 if inline
4226 then
4227 let mode = state.mode in
4228 state.mode <-
4229 Textentry (
4230 ("annotation: ", m_text, None, textentry, update, true),
4231 fun _ -> state.mode <- mode);
4232 state.text <- E.s;
4233 enttext ();
4234 else
4235 let s = getusertext m_text in
4236 update s
4238 m_text <- s;
4239 m_items <-
4240 ( "[Copy]", fun () -> selstring m_text)
4241 :: ("[Delete]", dele)
4242 :: ("[Edit]", edit conf.annotinline)
4243 :: (E.s, unit)
4244 :: split [] 0 0 |> List.rev |> Array.of_list
4246 initializer
4247 m_active <- 0
4248 end)
4250 state.text <- E.s;
4251 let s = getannotcontents opaque slinkindex in
4252 resetmstate ();
4253 msgsource#reset s;
4254 let source = (msgsource :> lvsource) in
4255 let modehash = findkeyhash conf "listview" in
4256 state.uioh <- coe (object
4257 inherit listview ~zebra:false ~helpmode:false
4258 ~source ~trusted:false ~modehash
4259 end);
4260 G.postRedisplay "enterannotmode";
4263 let gotounder under =
4264 let getpath filename =
4265 let path =
4266 if nonemptystr filename
4267 then
4268 if Filename.is_relative filename
4269 then
4270 let dir = Filename.dirname state.path in
4271 let dir =
4272 if Filename.is_implicit dir
4273 then Filename.concat (Sys.getcwd ()) dir
4274 else dir
4276 Filename.concat dir filename
4277 else filename
4278 else E.s
4280 if Sys.file_exists path
4281 then path
4282 else E.s
4284 match under with
4285 | Ulinkgoto (pageno, top) ->
4286 if pageno >= 0
4287 then (
4288 addnav ();
4289 let top =
4290 if conf.presentation && conf.coarseprespos
4291 then 0
4292 else top
4294 gotopage1 pageno top;
4297 | Ulinkuri s ->
4298 let re = Str.regexp {|\(([a-z]\)+://|} in
4299 if Str.string_match re s 0
4300 then
4301 match Str.matched_group 1 s with
4302 | "file" -> dolog "remote!"
4303 | _ -> gotouri s
4304 else (
4306 Scanf.sscanf s "#%d,%d,%d" (fun n x y ->
4307 if n > 0
4308 then (
4309 addnav ();
4310 let _, h = getpageyh n in
4311 let p = transformpagepoint (n-1) x y in
4312 let y =
4313 if conf.coarseprespos
4314 then 0
4315 else h - truncate p.(1)
4317 gotopage1 (n-1) y
4319 else gotouri s
4321 with _ -> gotouri s
4324 | Uremote (filename, pageno) ->
4325 let path = getpath filename in
4326 if nonemptystr path
4327 then (
4328 if conf.riani
4329 then
4330 let command = Printf.sprintf "%s -page %d %S" !selfexec pageno path in
4331 match spawn command [] with
4332 | _pid -> ()
4333 | (exception exn) ->
4334 dolog "failed to execute `%s': %s" command @@ exntos exn
4335 else
4336 let anchor = getanchor () in
4337 let ranchor = state.path, state.password, anchor, state.origin in
4338 state.origin <- E.s;
4339 state.anchor <- (pageno, 0.0, 0.0);
4340 state.ranchors <- ranchor :: state.ranchors;
4341 opendoc path E.s;
4343 else impmsg "cannot find %s" filename
4345 | Uremotedest (filename, destname) ->
4346 let path = getpath filename in
4347 if nonemptystr path
4348 then (
4349 if conf.riani
4350 then
4351 let command = !selfexec ^ " " ^ path ^ " -dest " ^ destname in
4352 match spawn command [] with
4353 | (exception exn) ->
4354 dolog "failed to execute `%s': %s" command @@ exntos exn
4355 | _pid -> ()
4356 else
4357 let anchor = getanchor () in
4358 let ranchor = state.path, state.password, anchor, state.origin in
4359 state.origin <- E.s;
4360 state.nameddest <- destname;
4361 state.ranchors <- ranchor :: state.ranchors;
4362 opendoc path E.s;
4364 else impmsg "cannot find %s" filename
4366 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4367 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
4370 let gotooutline (_, _, kind) =
4371 match kind with
4372 | Onone -> ()
4373 | Oanchor anchor ->
4374 let (pageno, y, _) = anchor in
4375 let y = getanchory
4376 (if conf.presentation then (pageno, y, 1.0) else anchor)
4378 addnav ();
4379 gotoghyll y
4380 | Ouri uri -> gotounder (Ulinkuri uri)
4381 | Olaunch cmd -> gotounder (Ulaunch cmd)
4382 | Oremote remote -> gotounder (Uremote remote)
4383 | Ohistory hist -> gotohist hist
4384 | Oremotedest remotedest -> gotounder (Uremotedest remotedest)
4387 class outlinesoucebase fetchoutlines = object (self)
4388 inherit lvsourcebase
4389 val mutable m_items = E.a
4390 val mutable m_minfo = E.a
4391 val mutable m_orig_items = E.a
4392 val mutable m_orig_minfo = E.a
4393 val mutable m_narrow_patterns = []
4394 val mutable m_gen = -1
4396 method getitemcount = Array.length m_items
4398 method getitem n =
4399 let s, n, _ = m_items.(n) in
4400 (s, n+0)
4402 method exit ~(uioh:uioh) ~cancel ~active ~(first:int) ~pan :
4403 uioh option =
4404 ignore (uioh, first);
4405 let items, minfo =
4406 if m_narrow_patterns = []
4407 then m_orig_items, m_orig_minfo
4408 else m_items, m_minfo
4410 m_pan <- pan;
4411 if not cancel
4412 then (
4413 m_items <- items;
4414 m_minfo <- minfo;
4415 gotooutline m_items.(active);
4417 else (
4418 m_items <- items;
4419 m_minfo <- minfo;
4421 None
4423 method hasaction (_:int) = true
4425 method greetmsg =
4426 if Array.length m_items != Array.length m_orig_items
4427 then
4428 let s =
4429 match m_narrow_patterns with
4430 | one :: [] -> one
4431 | many -> String.concat "@Uellipsis" (List.rev many)
4433 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
4434 else E.s
4436 method statestr =
4437 match m_narrow_patterns with
4438 | [] -> E.s
4439 | one :: [] -> one
4440 | head :: _ -> "@Uellipsis" ^ head
4442 method narrow pattern =
4443 match Str.regexp_case_fold pattern with
4444 | (exception _) -> ()
4445 | re ->
4446 let rec loop accu minfo n =
4447 if n = -1
4448 then (
4449 m_items <- Array.of_list accu;
4450 m_minfo <- Array.of_list minfo;
4452 else
4453 let (s, _, _) as o = m_items.(n) in
4454 let accu, minfo =
4455 match Str.search_forward re s 0 with
4456 | (exception Not_found) -> accu, minfo
4457 | first -> o :: accu, (first, Str.match_end ()) :: minfo
4459 loop accu minfo (n-1)
4461 loop [] [] (Array.length m_items - 1)
4463 method! getminfo = m_minfo
4465 method denarrow =
4466 m_orig_items <- fetchoutlines ();
4467 m_minfo <- m_orig_minfo;
4468 m_items <- m_orig_items
4470 method add_narrow_pattern pattern =
4471 m_narrow_patterns <- pattern :: m_narrow_patterns
4473 method del_narrow_pattern =
4474 match m_narrow_patterns with
4475 | _ :: rest -> m_narrow_patterns <- rest
4476 | [] -> ()
4478 method renarrow =
4479 self#denarrow;
4480 match m_narrow_patterns with
4481 | pattern :: [] -> self#narrow pattern; pattern
4482 | list ->
4483 List.fold_left (fun accu pattern ->
4484 self#narrow pattern;
4485 pattern ^ "@Uellipsis" ^ accu) E.s list
4487 method calcactive (_:anchor) = 0
4489 method reset anchor items =
4490 if state.gen != m_gen
4491 then (
4492 m_orig_items <- items;
4493 m_items <- items;
4494 m_narrow_patterns <- [];
4495 m_minfo <- E.a;
4496 m_orig_minfo <- E.a;
4497 m_gen <- state.gen;
4499 else (
4500 if items != m_orig_items
4501 then (
4502 m_orig_items <- items;
4503 if m_narrow_patterns == []
4504 then m_items <- items;
4507 let active = self#calcactive anchor in
4508 m_active <- active;
4509 m_first <- firstof m_first active
4513 let outlinesource fetchoutlines =
4514 (object
4515 inherit outlinesoucebase fetchoutlines
4516 method! calcactive anchor =
4517 let rely = getanchory anchor in
4518 let rec loop n best bestd =
4519 if n = Array.length m_items
4520 then best
4521 else
4522 let _, _, kind = m_items.(n) in
4523 match kind with
4524 | Oanchor anchor ->
4525 let orely = getanchory anchor in
4526 let d = abs (orely - rely) in
4527 if d < bestd
4528 then loop (n+1) n d
4529 else loop (n+1) best bestd
4530 | Onone | Oremote _ | Olaunch _
4531 | Oremotedest _ | Ouri _ | Ohistory _ ->
4532 loop (n+1) best bestd
4534 loop 0 ~-1 max_int
4535 end)
4538 let enteroutlinemode, enterbookmarkmode, enterhistmode =
4539 let mkselector sourcetype =
4540 let fetchoutlines () =
4541 match sourcetype with
4542 | `bookmarks -> Array.of_list state.bookmarks
4543 | `outlines -> state.outlines
4544 | `history -> genhistoutlines ()
4546 let source =
4547 if sourcetype = `history
4548 then new outlinesoucebase fetchoutlines
4549 else outlinesource fetchoutlines
4551 fun errmsg ->
4552 let outlines = fetchoutlines () in
4553 if Array.length outlines = 0
4554 then (
4555 showtext ' ' errmsg;
4557 else (
4558 resetmstate ();
4559 Wsi.setcursor Wsi.CURSOR_INHERIT;
4560 let anchor = getanchor () in
4561 source#reset anchor outlines;
4562 state.text <- source#greetmsg;
4563 state.uioh <-
4564 coe (new outlinelistview ~zebra:(sourcetype=`history) ~source);
4565 G.postRedisplay "enter selector";
4568 let mkenter sourcetype errmsg =
4569 let enter = mkselector sourcetype in
4570 fun () -> enter errmsg
4572 mkenter `outlines "document has no outline"
4573 , mkenter `bookmarks "document has no bookmarks (yet)"
4574 , mkenter `history "history is empty"
4577 let quickbookmark ?title () =
4578 match state.layout with
4579 | [] -> ()
4580 | l :: _ ->
4581 let title =
4582 match title with
4583 | None ->
4584 let tm = Unix.localtime (now ()) in
4585 Printf.sprintf
4586 "Quick (page %d) (bookmarked at %02d/%02d/%d %02d:%02d)"
4587 (l.pageno+1)
4588 tm.Unix.tm_mday
4589 (tm.Unix.tm_mon+1)
4590 (tm.Unix.tm_year + 1900)
4591 tm.Unix.tm_hour
4592 tm.Unix.tm_min
4593 | Some title -> title
4595 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4598 let setautoscrollspeed step goingdown =
4599 let incr = max 1 ((abs step) / 2) in
4600 let incr = if goingdown then incr else -incr in
4601 let astep = boundastep state.winh (step + incr) in
4602 state.autoscroll <- Some astep;
4605 let canpan () =
4606 match conf.columns with
4607 | Csplit _ -> true
4608 | Csingle _ | Cmulti _ -> state.x != 0 || conf.zoom > 1.0
4611 let panbound x = bound x (-state.w) state.winw;;
4613 let existsinrow pageno (columns, coverA, coverB) p =
4614 let last = ((pageno - coverA) mod columns) + columns in
4615 let rec any = function
4616 | [] -> false
4617 | l :: rest ->
4618 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4619 then p l
4620 else (
4621 if not (p l)
4622 then (if l.pageno = last then false else any rest)
4623 else true
4626 any state.layout
4629 let nextpage () =
4630 match state.layout with
4631 | [] ->
4632 let pageno = page_of_y state.y in
4633 gotoghyll (getpagey (pageno+1))
4634 | l :: rest ->
4635 match conf.columns with
4636 | Csingle _ ->
4637 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4638 then
4639 let y = clamp (pgscale state.winh) in
4640 gotoghyll y
4641 else
4642 let pageno = min (l.pageno+1) (state.pagecount-1) in
4643 gotoghyll (getpagey pageno)
4644 | Cmulti ((c, _, _) as cl, _) ->
4645 if conf.presentation
4646 && (existsinrow l.pageno cl
4647 (fun l -> l.pageh > l.pagey + l.pagevh))
4648 then
4649 let y = clamp (pgscale state.winh) in
4650 gotoghyll y
4651 else
4652 let pageno = min (l.pageno+c) (state.pagecount-1) in
4653 gotoghyll (getpagey pageno)
4654 | Csplit (n, _) ->
4655 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4656 then
4657 let pagey, pageh = getpageyh l.pageno in
4658 let pagey = pagey + pageh * l.pagecol in
4659 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4660 gotoghyll (pagey + pageh + ips)
4663 let prevpage () =
4664 match state.layout with
4665 | [] ->
4666 let pageno = page_of_y state.y in
4667 gotoghyll (getpagey (pageno-1))
4668 | l :: _ ->
4669 match conf.columns with
4670 | Csingle _ ->
4671 if conf.presentation && l.pagey != 0
4672 then
4673 gotoghyll (clamp (pgscale ~-(state.winh)))
4674 else
4675 let pageno = max 0 (l.pageno-1) in
4676 gotoghyll (getpagey pageno)
4677 | Cmulti ((c, _, coverB) as cl, _) ->
4678 if conf.presentation &&
4679 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4680 then
4681 gotoghyll (clamp (pgscale ~-(state.winh)))
4682 else
4683 let decr =
4684 if l.pageno = state.pagecount - coverB
4685 then 1
4686 else c
4688 let pageno = max 0 (l.pageno-decr) in
4689 gotoghyll (getpagey pageno)
4690 | Csplit (n, _) ->
4691 let y =
4692 if l.pagecol = 0
4693 then
4694 if l.pageno = 0
4695 then l.pagey
4696 else
4697 let pageno = max 0 (l.pageno-1) in
4698 let pagey, pageh = getpageyh pageno in
4699 pagey + (n-1)*pageh
4700 else
4701 let pagey, pageh = getpageyh l.pageno in
4702 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4704 gotoghyll y
4707 let save () =
4708 if emptystr conf.savecmd
4709 then error "don't know where to save modified document"
4710 else
4711 let savecmd = Str.global_replace percentsre state.path conf.savecmd in
4712 let path =
4713 getcmdoutput
4714 (fun s -> error "failed to obtain path to the saved copy: %s" s)
4715 savecmd
4717 if nonemptystr path
4718 then
4719 let tmp = path ^ ".tmp" in
4720 savedoc tmp;
4721 Unix.rename tmp path;
4724 let viewkeyboard key mask =
4725 let enttext te =
4726 let mode = state.mode in
4727 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4728 state.text <- E.s;
4729 enttext ();
4730 G.postRedisplay "view:enttext"
4732 let ctrl = Wsi.withctrl mask in
4733 let key = Wsi.keypadtodigitkey key in
4734 match key with
4735 | @Q -> exit 0
4737 | @W ->
4738 if hasunsavedchanges ()
4739 then save ()
4741 | @insert ->
4742 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4743 then (
4744 state.mode <- LinkNav (Ltgendir 0);
4745 gotoxy state.x state.y;
4747 else impmsg "keyboard link navigation does not work under rotation"
4749 | @escape | @q ->
4750 begin match state.mstate with
4751 | Mzoomrect _ ->
4752 resetmstate ();
4753 G.postRedisplay "kill rect";
4754 | Msel _
4755 | Mpan _
4756 | Mscrolly | Mscrollx
4757 | Mzoom _
4758 | Mnone ->
4759 begin match state.mode with
4760 | LinkNav _ ->
4761 state.mode <- View;
4762 G.postRedisplay "esc leave linknav"
4763 | Birdseye _
4764 | Textentry _
4765 | View ->
4766 match state.ranchors with
4767 | [] -> raise Quit
4768 | (path, password, anchor, origin) :: rest ->
4769 state.ranchors <- rest;
4770 state.anchor <- anchor;
4771 state.origin <- origin;
4772 state.nameddest <- E.s;
4773 opendoc path password
4774 end;
4775 end;
4777 | @backspace ->
4778 gotoghyll (getnav ~-1)
4780 | @o ->
4781 enteroutlinemode ()
4783 | @H ->
4784 enterhistmode ()
4786 | @u ->
4787 state.rects <- [];
4788 state.text <- E.s;
4789 Hashtbl.iter (fun _ opaque ->
4790 clearmark opaque;
4791 Hashtbl.clear state.prects) state.pagemap;
4792 G.postRedisplay "dehighlight";
4794 | @slash | @question ->
4795 let ondone isforw s =
4796 cbput state.hists.pat s;
4797 state.searchpattern <- s;
4798 search s isforw
4800 let s = String.make 1 (Char.chr key) in
4801 enttext (s, E.s, Some (onhist state.hists.pat),
4802 textentry, ondone (key = @slash), true)
4804 | @plus | @kpplus | @equals when ctrl ->
4805 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4806 pivotzoom (conf.zoom +. incr)
4808 | @plus | @kpplus ->
4809 let ondone s =
4810 let n =
4811 try int_of_string s with exn ->
4812 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn;
4813 max_int
4815 if n != max_int
4816 then (
4817 conf.pagebias <- n;
4818 state.text <- "page bias is now " ^ string_of_int n;
4821 enttext ("page bias: ", E.s, None, intentry, ondone, true)
4823 | @minus | @kpminus when ctrl ->
4824 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4825 pivotzoom (max 0.01 (conf.zoom -. decr))
4827 | @minus | @kpminus ->
4828 let ondone msg = state.text <- msg in
4829 enttext (
4830 "option [acfhilpstvxACFPRSZTISM]: ", E.s, None,
4831 optentry state.mode, ondone, true
4834 | @0 when ctrl ->
4835 if conf.zoom = 1.0
4836 then gotoxy 0 state.y
4837 else setzoom 1.0
4839 | (@1 | @2) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
4840 let cols =
4841 match conf.columns with
4842 | Csingle _ | Cmulti _ -> 1
4843 | Csplit (n, _) -> n
4845 let h = state.winh -
4846 conf.interpagespace lsl (if conf.presentation then 1 else 0)
4848 let zoom = zoomforh state.winw h 0 cols in
4849 if zoom > 0.0 && (key = @2 || zoom < 1.0)
4850 then setzoom zoom
4852 | @3 when ctrl ->
4853 let fm =
4854 match conf.fitmodel with
4855 | FitWidth -> FitProportional
4856 | FitProportional -> FitPage
4857 | FitPage -> FitWidth
4859 state.text <- "fit model: " ^ FMTE.to_string fm;
4860 reqlayout conf.angle fm
4862 | @4 when ctrl -> (* ctrl-4 *)
4863 let zoom = getmaxw () /. float state.winw in
4864 if zoom > 0.0 then setzoom zoom
4866 | @F9 ->
4867 togglebirdseye ()
4869 | @9 when ctrl ->
4870 togglebirdseye ()
4872 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4873 when not ctrl -> (* 0..9 *)
4874 let ondone s =
4875 let n =
4876 try int_of_string s with exn ->
4877 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn;
4880 if n >= 0
4881 then (
4882 addnav ();
4883 cbput state.hists.pag (string_of_int n);
4884 gotopage1 (n + conf.pagebias - 1) 0;
4887 let pageentry text key =
4888 match Char.unsafe_chr key with
4889 | 'g' -> TEdone text
4890 | _ -> intentry text key
4892 let text = String.make 1 (Char.chr key) in
4893 enttext (":", text, Some (onhist state.hists.pag),
4894 pageentry, ondone, true)
4896 | @b ->
4897 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
4898 G.postRedisplay "toggle scrollbar";
4900 | @B ->
4901 state.bzoom <- not state.bzoom;
4902 state.rects <- [];
4903 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
4905 | @l ->
4906 conf.hlinks <- not conf.hlinks;
4907 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4908 G.postRedisplay "toggle highlightlinks";
4910 | @F ->
4911 if conf.angle mod 360 = 0
4912 then (
4913 state.glinks <- true;
4914 let mode = state.mode in
4915 state.mode <-
4916 Textentry (
4917 (":", E.s, None, linknentry, linknact gotounder, false),
4918 (fun _ ->
4919 state.glinks <- false;
4920 state.mode <- mode)
4922 state.text <- E.s;
4923 G.postRedisplay "view:linkent(F)"
4925 else impmsg "hint mode does not work under rotation"
4927 | @y ->
4928 state.glinks <- true;
4929 let mode = state.mode in
4930 state.mode <- Textentry (
4932 ":", E.s, None, linknentry, linknact (fun under ->
4933 selstring (undertext under);
4934 ), false
4936 fun _ ->
4937 state.glinks <- false;
4938 state.mode <- mode
4940 state.text <- E.s;
4941 G.postRedisplay "view:linkent"
4943 | @a ->
4944 begin match state.autoscroll with
4945 | Some step ->
4946 conf.autoscrollstep <- step;
4947 state.autoscroll <- None
4948 | None ->
4949 if conf.autoscrollstep = 0
4950 then state.autoscroll <- Some 1
4951 else state.autoscroll <- Some conf.autoscrollstep
4954 | @p when ctrl ->
4955 launchpath () (* XXX where do error messages go? *)
4957 | @P ->
4958 setpresentationmode (not conf.presentation);
4959 showtext ' ' ("presentation mode " ^
4960 if conf.presentation then "on" else "off");
4962 | @f ->
4963 if List.mem Wsi.Fullscreen state.winstate
4964 then Wsi.reshape conf.cwinw conf.cwinh
4965 else Wsi.fullscreen ()
4967 | @p | @N ->
4968 search state.searchpattern false
4970 | @n | @F3 ->
4971 search state.searchpattern true
4973 | @t ->
4974 begin match state.layout with
4975 | [] -> ()
4976 | l :: _ ->
4977 gotoghyll (getpagey l.pageno)
4980 | @space ->
4981 nextpage ()
4983 | @delete | @kpdelete -> (* delete *)
4984 prevpage ()
4986 | @equals ->
4987 showtext ' ' (describe_location ());
4989 | @w ->
4990 begin match state.layout with
4991 | [] -> ()
4992 | l :: _ ->
4993 Wsi.reshape l.pagew l.pageh;
4994 G.postRedisplay "w"
4997 | @apos ->
4998 enterbookmarkmode ()
5000 | @h | @F1 ->
5001 enterhelpmode ()
5003 | @i ->
5004 enterinfomode ()
5006 | @e when Buffer.length state.errmsgs > 0 ->
5007 entermsgsmode ()
5009 | @m ->
5010 let ondone s =
5011 match state.layout with
5012 | l :: _ ->
5013 if nonemptystr s
5014 then
5015 state.bookmarks <-
5016 (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
5017 | _ -> ()
5019 enttext ("bookmark: ", E.s, None, textentry, ondone, true)
5021 | @tilde ->
5022 quickbookmark ();
5023 showtext ' ' "Quick bookmark added";
5025 | @z ->
5026 begin match state.layout with
5027 | l :: _ ->
5028 let rect = getpdimrect l.pagedimno in
5029 let w, h =
5030 if conf.crophack
5031 then
5032 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5033 truncate (1.2 *. (rect.(3) -. rect.(0))))
5034 else
5035 (truncate (rect.(1) -. rect.(0)),
5036 truncate (rect.(3) -. rect.(0)))
5038 let w = truncate ((float w)*.conf.zoom)
5039 and h = truncate ((float h)*.conf.zoom) in
5040 if w != 0 && h != 0
5041 then (
5042 state.anchor <- getanchor ();
5043 Wsi.reshape w (h + conf.interpagespace)
5045 G.postRedisplay "z";
5047 | [] -> ()
5050 | @x -> state.roam ()
5052 | @Lt | @Gt ->
5053 reqlayout (conf.angle +
5054 (if key = @Gt then 30 else -30)) conf.fitmodel
5056 | @Lb | @Rb ->
5057 conf.colorscale <-
5058 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5060 G.postRedisplay "brightness";
5062 | @c when state.mode = View ->
5063 if Wsi.withalt mask
5064 then (
5065 if conf.zoom > 1.0
5066 then
5067 let m = (state.winw - state.w) / 2 in
5068 gotoxy_and_clear_text m state.y
5070 else
5071 let (c, a, b), z =
5072 match state.prevcolumns with
5073 | None -> (1, 0, 0), 1.0
5074 | Some (columns, z) ->
5075 let cab =
5076 match columns with
5077 | Csplit (c, _) -> -c, 0, 0
5078 | Cmulti ((c, a, b), _) -> c, a, b
5079 | Csingle _ -> 1, 0, 0
5081 cab, z
5083 setcolumns View c a b;
5084 setzoom z
5086 | @down | @up when ctrl && Wsi.withshift mask ->
5087 let zoom, x = state.prevzoom in
5088 setzoom zoom;
5089 state.x <- x;
5091 | @k | @up | @kpup ->
5092 begin match state.autoscroll with
5093 | None ->
5094 begin match state.mode with
5095 | Birdseye beye -> upbirdseye 1 beye
5096 | Textentry _
5097 | View
5098 | LinkNav _ ->
5099 if ctrl
5100 then gotoxy_and_clear_text state.x (clamp ~-(state.winh/2))
5101 else (
5102 if not (Wsi.withshift mask) && conf.presentation
5103 then prevpage ()
5104 else gotoghyll1 true (clamp (-conf.scrollstep))
5107 | Some n ->
5108 setautoscrollspeed n false
5111 | @j | @down | @kpdown ->
5112 begin match state.autoscroll with
5113 | None ->
5114 begin match state.mode with
5115 | Birdseye beye -> downbirdseye 1 beye
5116 | Textentry _
5117 | View
5118 | LinkNav _ ->
5119 if ctrl
5120 then gotoxy_and_clear_text state.x (clamp (state.winh/2))
5121 else (
5122 if not (Wsi.withshift mask) && conf.presentation
5123 then nextpage ()
5124 else gotoghyll1 true (clamp (conf.scrollstep))
5127 | Some n ->
5128 setautoscrollspeed n true
5131 | @left | @right | @kpleft | @kpright when not (Wsi.withalt mask) ->
5132 if canpan ()
5133 then
5134 let dx =
5135 if ctrl
5136 then state.winw / 2
5137 else conf.hscrollstep
5139 let dx = if key = @left || key = @kpleft then dx else -dx in
5140 gotoxy_and_clear_text (panbound (state.x + dx)) state.y
5141 else (
5142 state.text <- E.s;
5143 G.postRedisplay "left/right"
5146 | @prior | @kpprior ->
5147 let y =
5148 if ctrl
5149 then
5150 match state.layout with
5151 | [] -> state.y
5152 | l :: _ -> state.y - l.pagey
5153 else
5154 clamp (pgscale (-state.winh))
5156 gotoghyll y
5158 | @next | @kpnext ->
5159 let y =
5160 if ctrl
5161 then
5162 match List.rev state.layout with
5163 | [] -> state.y
5164 | l :: _ -> getpagey l.pageno
5165 else
5166 clamp (pgscale state.winh)
5168 gotoghyll y
5170 | @g | @home | @kphome ->
5171 addnav ();
5172 gotoghyll 0
5173 | @G | @jend | @kpend ->
5174 addnav ();
5175 gotoghyll (clamp state.maxy)
5177 | @right | @kpright when Wsi.withalt mask ->
5178 gotoghyll (getnav 1)
5179 | @left | @kpleft when Wsi.withalt mask ->
5180 gotoghyll (getnav ~-1)
5182 | @r ->
5183 reload ()
5185 | @v when conf.debug ->
5186 state.rects <- [];
5187 List.iter (fun l ->
5188 match getopaque l.pageno with
5189 | None -> ()
5190 | Some opaque ->
5191 let x0, y0, x1, y1 = pagebbox opaque in
5192 let rect = (float x0, float y0,
5193 float x1, float y0,
5194 float x1, float y1,
5195 float x0, float y1) in
5196 debugrect rect;
5197 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
5198 state.rects <- (l.pageno, color, rect) :: state.rects;
5199 ) state.layout;
5200 G.postRedisplay "v";
5202 | @pipe ->
5203 let mode = state.mode in
5204 let cmd = ref E.s in
5205 let onleave = function
5206 | Cancel -> state.mode <- mode
5207 | Confirm ->
5208 List.iter (fun l ->
5209 match getopaque l.pageno with
5210 | Some opaque -> pipesel opaque !cmd
5211 | None -> ()) state.layout;
5212 state.mode <- mode
5214 let ondone s =
5215 cbput state.hists.sel s;
5216 cmd := s
5218 let te =
5219 "| ", !cmd, Some (onhist state.hists.sel), textentry, ondone, true
5221 G.postRedisplay "|";
5222 state.mode <- Textentry (te, onleave);
5224 | _ ->
5225 vlog "huh? %s" (Wsi.keyname key)
5228 let linknavkeyboard key mask linknav =
5229 let getpage pageno =
5230 let rec loop = function
5231 | [] -> None
5232 | l :: _ when l.pageno = pageno -> Some l
5233 | _ :: rest -> loop rest
5234 in loop state.layout
5236 let doexact (pageno, n) =
5237 match getopaque pageno, getpage pageno with
5238 | Some opaque, Some l ->
5239 if key = @enter || key = @kpenter
5240 then
5241 let under = getlink opaque n in
5242 G.postRedisplay "link gotounder";
5243 gotounder under;
5244 state.mode <- View;
5245 else
5246 let opt, dir =
5247 match key with
5248 | @home ->
5249 Some (findlink opaque LDfirst), -1
5251 | @jend ->
5252 Some (findlink opaque LDlast), 1
5254 | @left ->
5255 Some (findlink opaque (LDleft n)), -1
5257 | @right ->
5258 Some (findlink opaque (LDright n)), 1
5260 | @up ->
5261 Some (findlink opaque (LDup n)), -1
5263 | @down ->
5264 Some (findlink opaque (LDdown n)), 1
5266 | _ -> None, 0
5268 let pwl l dir =
5269 begin match findpwl l.pageno dir with
5270 | Pwlnotfound -> ()
5271 | Pwl pageno ->
5272 let notfound dir =
5273 state.mode <- LinkNav (Ltgendir dir);
5274 let y, h = getpageyh pageno in
5275 let y =
5276 if dir < 0
5277 then y + h - state.winh
5278 else y
5280 gotoxy state.x y
5282 begin match getopaque pageno, getpage pageno with
5283 | Some opaque, Some _ ->
5284 let link =
5285 let ld = if dir > 0 then LDfirst else LDlast in
5286 findlink opaque ld
5288 begin match link with
5289 | Lfound m ->
5290 showlinktype (getlink opaque m);
5291 state.mode <- LinkNav (Ltexact (pageno, m));
5292 G.postRedisplay "linknav jpage";
5293 | Lnotfound -> notfound dir
5294 end;
5295 | _ -> notfound dir
5296 end;
5297 end;
5299 begin match opt with
5300 | Some Lnotfound -> pwl l dir;
5301 | Some (Lfound m) ->
5302 if m = n
5303 then pwl l dir
5304 else (
5305 let _, y0, _, y1 = getlinkrect opaque m in
5306 if y0 < l.pagey
5307 then gotopage1 l.pageno y0
5308 else (
5309 let d = fstate.fontsize + 1 in
5310 if y1 - l.pagey > l.pagevh - d
5311 then gotopage1 l.pageno (y1 - state.winh + d)
5312 else G.postRedisplay "linknav";
5314 showlinktype (getlink opaque m);
5315 state.mode <- LinkNav (Ltexact (l.pageno, m));
5318 | None -> viewkeyboard key mask
5319 end;
5320 | _ -> viewkeyboard key mask
5322 if key = @insert
5323 then (
5324 state.mode <- View;
5325 G.postRedisplay "leave linknav"
5327 else
5328 match linknav with
5329 | Ltgendir _ | Ltnotready _ -> viewkeyboard key mask
5330 | Ltexact exact -> doexact exact
5333 let keyboard key mask =
5334 if (key = @g && Wsi.withctrl mask) && not (istextentry state.mode)
5335 then wcmd "interrupt"
5336 else state.uioh <- state.uioh#key key mask
5339 let birdseyekeyboard key mask
5340 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5341 let incr =
5342 match conf.columns with
5343 | Csingle _ -> 1
5344 | Cmulti ((c, _, _), _) -> c
5345 | Csplit _ -> failwith "bird's eye split mode"
5347 let pgh layout = List.fold_left
5348 (fun m l -> max l.pageh m) state.winh layout in
5349 match key with
5350 | @l when Wsi.withctrl mask ->
5351 let y, h = getpageyh pageno in
5352 let top = (state.winh - h) / 2 in
5353 gotoxy state.x (max 0 (y - top))
5354 | @enter | @kpenter -> leavebirdseye beye false
5355 | @escape -> leavebirdseye beye true
5356 | @up -> upbirdseye incr beye
5357 | @down -> downbirdseye incr beye
5358 | @left -> upbirdseye 1 beye
5359 | @right -> downbirdseye 1 beye
5361 | @prior ->
5362 begin match state.layout with
5363 | l :: _ ->
5364 if l.pagey != 0
5365 then (
5366 state.mode <- Birdseye (
5367 oconf, leftx, l.pageno, hooverpageno, anchor
5369 gotopage1 l.pageno 0;
5371 else (
5372 let layout = layout state.x (state.y-state.winh)
5373 state.winw
5374 (pgh state.layout) in
5375 match layout with
5376 | [] -> gotoxy state.x (clamp (-state.winh))
5377 | l :: _ ->
5378 state.mode <- Birdseye (
5379 oconf, leftx, l.pageno, hooverpageno, anchor
5381 gotopage1 l.pageno 0
5384 | [] -> gotoxy state.x (clamp (-state.winh))
5385 end;
5387 | @next ->
5388 begin match List.rev state.layout with
5389 | l :: _ ->
5390 let layout = layout state.x
5391 (state.y + (pgh state.layout))
5392 state.winw state.winh in
5393 begin match layout with
5394 | [] ->
5395 let incr = l.pageh - l.pagevh in
5396 if incr = 0
5397 then (
5398 state.mode <-
5399 Birdseye (
5400 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5402 G.postRedisplay "birdseye pagedown";
5404 else gotoxy state.x (clamp (incr + conf.interpagespace*2));
5406 | l :: _ ->
5407 state.mode <-
5408 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5409 gotopage1 l.pageno 0;
5412 | [] -> gotoxy state.x (clamp state.winh)
5413 end;
5415 | @home ->
5416 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5417 gotopage1 0 0
5419 | @jend ->
5420 let pageno = state.pagecount - 1 in
5421 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5422 if not (pagevisible state.layout pageno)
5423 then
5424 let h =
5425 match List.rev state.pdims with
5426 | [] -> state.winh
5427 | (_, _, h, _) :: _ -> h
5429 gotoxy
5430 state.x
5431 (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5432 else G.postRedisplay "birdseye end";
5434 | _ -> viewkeyboard key mask
5437 let drawpage l =
5438 let color =
5439 match state.mode with
5440 | Textentry _ -> scalecolor 0.4
5441 | LinkNav _
5442 | View -> scalecolor 1.0
5443 | Birdseye (_, _, pageno, hooverpageno, _) ->
5444 if l.pageno = hooverpageno
5445 then scalecolor 0.9
5446 else (
5447 if l.pageno = pageno
5448 then (
5449 let c = scalecolor 1.0 in
5450 GlDraw.color c;
5451 GlDraw.line_width 3.0;
5452 let dispx = l.pagedispx in
5453 linerect
5454 (float (dispx-1)) (float (l.pagedispy-1))
5455 (float (dispx+l.pagevw+1))
5456 (float (l.pagedispy+l.pagevh+1))
5458 GlDraw.line_width 1.0;
5461 else scalecolor 0.8
5464 drawtiles l color;
5467 let postdrawpage l linkindexbase =
5468 match getopaque l.pageno with
5469 | Some opaque ->
5470 if tileready l l.pagex l.pagey
5471 then
5472 let x = l.pagedispx - l.pagex
5473 and y = l.pagedispy - l.pagey in
5474 let hlmask =
5475 match conf.columns with
5476 | Csingle _ | Cmulti _ ->
5477 (if conf.hlinks then 1 else 0)
5478 + (if state.glinks
5479 && not (isbirdseye state.mode) then 2 else 0)
5480 | Csplit _ -> 0
5482 let s =
5483 match state.mode with
5484 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5485 | Textentry _
5486 | Birdseye _
5487 | View
5488 | LinkNav _ -> E.s
5490 Hashtbl.find_all state.prects l.pageno |>
5491 List.iter (fun vals -> drawprect opaque x y vals);
5492 let n = postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize) in
5493 if n < 0
5494 then (state.redisplay <- true; 0)
5495 else n
5496 else 0
5497 | _ -> 0
5500 let scrollindicator () =
5501 let sbw, ph, sh = state.uioh#scrollph in
5502 let sbh, pw, sw = state.uioh#scrollpw in
5504 let x0,x1,hx0 =
5505 if conf.leftscroll
5506 then (0, sbw, sbw)
5507 else ((state.winw - sbw), state.winw, 0)
5510 Gl.enable `blend;
5511 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5512 GlDraw.color (0.64, 0.64, 0.64) ~alpha:0.7;
5513 filledrect (float x0) 0. (float x1) (float state.winh);
5514 filledrect
5515 (float hx0) (float (state.winh - sbh))
5516 (float (hx0 + state.winw)) (float state.winh)
5518 GlDraw.color (0.0, 0.0, 0.0) ~alpha:0.7;
5520 filledrect (float x0) ph (float x1) (ph +. sh);
5521 let pw = pw +. float hx0 in
5522 filledrect pw (float (state.winh - sbh)) (pw +. sw) (float state.winh);
5523 Gl.disable `blend;
5526 let showsel () =
5527 match state.mstate with
5528 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5531 | Msel ((x0, y0), (x1, y1)) ->
5532 let identify opaque l px py = Some (opaque, l.pageno, px, py) in
5533 let o0,n0,px0,py0 = onppundermouse identify x0 y0 (~< E.s, -1, 0, 0) in
5534 let _o1,n1,px1,py1 = onppundermouse identify x1 y1 (~< E.s, -1, 0, 0) in
5535 if n0 != -1 && n0 = n1 then seltext o0 (px0, py0, px1, py1);
5538 let showrects = function [] -> () | rects ->
5539 Gl.enable `blend;
5540 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5541 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5542 List.iter
5543 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5544 List.iter (fun l ->
5545 if l.pageno = pageno
5546 then (
5547 let dx = float (l.pagedispx - l.pagex) in
5548 let dy = float (l.pagedispy - l.pagey) in
5549 let r, g, b, alpha = c in
5550 GlDraw.color (r, g, b) ~alpha;
5551 filledrect2 (x0+.dx) (y0+.dy)
5552 (x1+.dx) (y1+.dy)
5553 (x3+.dx) (y3+.dy)
5554 (x2+.dx) (y2+.dy);
5556 ) state.layout
5557 ) rects
5559 Gl.disable `blend;
5562 let display () =
5563 begin match conf.columns, state.layout with
5564 | Csingle _, _ :: _ ->
5565 GlDraw.color (scalecolor2 conf.bgcolor);
5566 let y =
5567 List.fold_left (fun y l ->
5568 let x0 = 0 in
5569 let y0 = y in
5570 let x1 = l.pagedispx in
5571 let y1 = (l.pagedispy + l.pagevh) in
5572 filledrect (float x0) (float y0) (float x1) (float y1);
5573 let x0 = x1 + l.pagevw in
5574 let x1 = state.winw in
5575 filledrect1 (float x0) (float y0) (float x1) (float y1);
5576 if y != l.pagedispy
5577 then (
5578 let x0 = 0
5579 and x1 = state.winw in
5580 let y0 = y
5581 and y1 = l.pagedispy in
5582 filledrect1 (float x0) (float y0) (float x1) (float y1);
5584 l.pagedispy + l.pagevh) 0 state.layout
5586 let x0 = 0
5587 and x1 = state.winw in
5588 let y0 = y
5589 and y1 = state.winh in
5590 filledrect1 (float x0) (float y0) (float x1) (float y1)
5591 | (Cmulti _ | Csplit _), _ | Csingle _, [] ->
5592 GlClear.color (scalecolor2 conf.bgcolor);
5593 GlClear.clear [`color];
5594 end;
5595 List.iter drawpage state.layout;
5596 let rects =
5597 match state.mode with
5598 | LinkNav (Ltexact (pageno, linkno)) ->
5599 begin match getopaque pageno with
5600 | Some opaque ->
5601 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5602 let color = (0.0, 0.0, 0.5, 0.5) in
5603 (pageno, color, (
5604 float x0, float y0,
5605 float x1, float y0,
5606 float x1, float y1,
5607 float x0, float y1)
5608 ) :: state.rects
5609 | None -> state.rects
5611 | LinkNav (Ltgendir _) | LinkNav (Ltnotready _)
5612 | Birdseye _
5613 | Textentry _
5614 | View -> state.rects
5616 showrects rects;
5617 let rec postloop linkindexbase = function
5618 | l :: rest ->
5619 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5620 postloop linkindexbase rest
5621 | [] -> ()
5623 showsel ();
5624 postloop 0 state.layout;
5625 state.uioh#display;
5626 begin match state.mstate with
5627 | Mzoomrect ((x0, y0), (x1, y1)) ->
5628 Gl.enable `blend;
5629 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5630 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5631 filledrect (float x0) (float y0) (float x1) (float y1);
5632 Gl.disable `blend;
5633 | Msel _
5634 | Mpan _
5635 | Mscrolly | Mscrollx
5636 | Mzoom _
5637 | Mnone -> ()
5638 end;
5639 enttext ();
5640 scrollindicator ();
5641 Wsi.swapb ();
5644 let zoomrect x y x1 y1 =
5645 let x0 = min x x1
5646 and x1 = max x x1
5647 and y0 = min y y1 in
5648 let zoom = (float state.w) /. float (x1 - x0) in
5649 let margin =
5650 let simple () =
5651 if state.w < state.winw
5652 then (state.winw - state.w) / 2
5653 else 0
5655 match conf.fitmodel with
5656 | FitWidth | FitProportional -> simple ()
5657 | FitPage ->
5658 match conf.columns with
5659 | Csplit _ ->
5660 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
5661 | Cmulti _ | Csingle _ -> simple ()
5663 gotoxy ((state.x + margin) - x0) (state.y + y0);
5664 state.anchor <- getanchor ();
5665 setzoom zoom;
5666 resetmstate ();
5669 let annot inline x y =
5670 match unproject x y with
5671 | Some (opaque, n, ux, uy) ->
5672 let add text =
5673 addannot opaque ux uy text;
5674 wcmd "freepage %s" (~> opaque);
5675 Hashtbl.remove state.pagemap (n, state.gen);
5676 flushtiles ();
5677 gotoxy state.x state.y
5679 if inline
5680 then
5681 let ondone s = add s in
5682 let mode = state.mode in
5683 state.mode <- Textentry (
5684 ("annotation: ", E.s, None, textentry, ondone, true),
5685 fun _ -> state.mode <- mode);
5686 state.text <- E.s;
5687 enttext ();
5688 G.postRedisplay "annot"
5689 else
5690 add @@ getusertext E.s
5691 | _ -> ()
5694 let zoomblock x y =
5695 let g opaque l px py =
5696 match rectofblock opaque px py with
5697 | Some a ->
5698 let x0 = a.(0) -. 20. in
5699 let x1 = a.(1) +. 20. in
5700 let y0 = a.(2) -. 20. in
5701 let zoom = (float state.w) /. (x1 -. x0) in
5702 let pagey = getpagey l.pageno in
5703 let margin = (state.w - l.pagew)/2 in
5704 let nx = -truncate x0 - margin in
5705 gotoxy_and_clear_text nx (pagey + truncate y0);
5706 state.anchor <- getanchor ();
5707 setzoom zoom;
5708 None
5709 | None -> None
5711 match conf.columns with
5712 | Csplit _ ->
5713 impmsg "block zooming does not work properly in split columns mode"
5714 | Cmulti _ | Csingle _ -> onppundermouse g x y ()
5717 let scrollx x =
5718 let winw = state.winw - 1 in
5719 let s = float x /. float winw in
5720 let destx = truncate (float (state.w + winw) *. s) in
5721 gotoxy_and_clear_text (winw - destx) state.y;
5722 state.mstate <- Mscrollx;
5725 let scrolly y =
5726 let s = float y /. float state.winh in
5727 let desty = truncate (float (state.maxy -
5728 (if conf.maxhfit then state.winh else 0))
5729 *. s) in
5730 gotoxy_and_clear_text state.x desty;
5731 state.mstate <- Mscrolly;
5734 let viewmulticlick clicks x y mask =
5735 let g opaque l px py =
5736 let mark =
5737 match clicks with
5738 | 2 -> Mark_word
5739 | 3 -> Mark_line
5740 | 4 -> Mark_block
5741 | _ -> Mark_page
5743 if markunder opaque px py mark
5744 then (
5745 Some (fun () ->
5746 let dopipe cmd =
5747 match getopaque l.pageno with
5748 | None -> ()
5749 | Some opaque -> pipesel opaque cmd
5751 state.roam <- (fun () -> dopipe conf.paxcmd);
5752 if not (Wsi.withctrl mask) then dopipe conf.selcmd;
5755 else None
5757 G.postRedisplay "viewmulticlick";
5758 onppundermouse g x y (fun () -> impmsg "nothing to select") ();
5761 let canselect () =
5762 match conf.columns with
5763 | Csplit _ -> false
5764 | Csingle _ | Cmulti _ -> conf.angle mod 360 = 0
5767 let viewmouse button down x y mask =
5768 match button with
5769 | n when (n == 4 || n == 5) && not down ->
5770 if Wsi.withctrl mask
5771 then (
5772 match state.mstate with
5773 | Mzoom (oldn, i, (ftx, fty)) ->
5774 let recenter =
5775 if false
5776 then abs (ftx - x) > 5 || abs (fty - y) > 5
5777 else false
5779 if oldn = n
5780 then (
5781 if i = 2
5782 then
5783 let incr =
5784 match n with
5785 | 5 ->
5786 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5787 | _ ->
5788 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5790 let zoom = conf.zoom -. incr in
5791 if recenter
5792 then pivotzoom ~x ~y zoom
5793 else pivotzoom zoom;
5794 state.mstate <- Mzoom (n, 0, (x, y));
5795 else
5796 state.mstate <- Mzoom (n, i+1, (ftx, fty));
5798 else state.mstate <- Mzoom (n, 0, (ftx, fty))
5800 | Msel _
5801 | Mpan _
5802 | Mscrolly | Mscrollx
5803 | Mzoomrect _
5804 | Mnone -> state.mstate <- Mzoom (n, 0, (0, 0))
5806 else (
5807 match state.autoscroll with
5808 | Some step -> setautoscrollspeed step (n=4)
5809 | None ->
5810 if conf.wheelbypage || conf.presentation
5811 then (
5812 if n = 4
5813 then prevpage ()
5814 else nextpage ()
5816 else
5817 let incr =
5818 if n = 4
5819 then -conf.scrollstep
5820 else conf.scrollstep
5822 let incr = incr * 2 in
5823 let y = clamp incr in
5824 gotoxy_and_clear_text state.x y
5827 | n when (n = 6 || n = 7) && not down && canpan () ->
5828 let x =
5829 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep) in
5830 gotoxy_and_clear_text x state.y
5832 | 1 when Wsi.withshift mask ->
5833 state.mstate <- Mnone;
5834 if not down
5835 then (
5836 match unproject x y with
5837 | None -> ()
5838 | Some (_, pageno, ux, uy) ->
5839 let cmd = Printf.sprintf
5840 "%s %s %d %d %d"
5841 conf.stcmd state.path pageno ux uy
5843 match spawn cmd [] with
5844 | (exception exn) ->
5845 impmsg "execution of synctex command(%S) failed: %S"
5846 conf.stcmd @@ exntos exn
5847 | _pid -> ()
5850 | 1 when Wsi.withctrl mask ->
5851 if down
5852 then (
5853 Wsi.setcursor Wsi.CURSOR_FLEUR;
5854 state.mstate <- Mpan (x, y)
5856 else
5857 state.mstate <- Mnone
5859 | 3 ->
5860 if down
5861 then (
5862 if Wsi.withshift mask
5863 then (
5864 annot conf.annotinline x y;
5865 G.postRedisplay "addannot"
5867 else
5868 let p = (x, y) in
5869 Wsi.setcursor Wsi.CURSOR_CYCLE;
5870 state.mstate <- Mzoomrect (p, p)
5872 else (
5873 match state.mstate with
5874 | Mzoomrect ((x0, y0), _) ->
5875 if abs (x-x0) > 10 && abs (y - y0) > 10
5876 then zoomrect x0 y0 x y
5877 else (
5878 resetmstate ();
5879 G.postRedisplay "kill accidental zoom rect";
5881 | Msel _
5882 | Mpan _
5883 | Mscrolly | Mscrollx
5884 | Mzoom _
5885 | Mnone ->
5886 resetmstate ()
5889 | 1 when vscrollhit x ->
5890 if down
5891 then
5892 let _, position, sh = state.uioh#scrollph in
5893 if y > truncate position && y < truncate (position +. sh)
5894 then state.mstate <- Mscrolly
5895 else scrolly y
5896 else
5897 state.mstate <- Mnone
5899 | 1 when y > state.winh - hscrollh () ->
5900 if down
5901 then
5902 let _, position, sw = state.uioh#scrollpw in
5903 if x > truncate position && x < truncate (position +. sw)
5904 then state.mstate <- Mscrollx
5905 else scrollx x
5906 else
5907 state.mstate <- Mnone
5909 | 1 when state.bzoom -> if not down then zoomblock x y
5911 | 1 ->
5912 let dest = if down then getunder x y else Unone in
5913 begin match dest with
5914 | Ulinkgoto _
5915 | Ulinkuri _
5916 | Uremote _ | Uremotedest _
5917 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5918 gotounder dest
5920 | Unone when down ->
5921 Wsi.setcursor Wsi.CURSOR_FLEUR;
5922 state.mstate <- Mpan (x, y);
5924 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
5926 | Unone | Utext _ ->
5927 if down
5928 then (
5929 if canselect ()
5930 then (
5931 state.mstate <- Msel ((x, y), (x, y));
5932 G.postRedisplay "mouse select";
5935 else (
5936 match state.mstate with
5937 | Mnone -> ()
5939 | Mzoom _ | Mscrollx | Mscrolly ->
5940 state.mstate <- Mnone
5942 | Mzoomrect ((x0, y0), _) ->
5943 zoomrect x0 y0 x y
5945 | Mpan _ ->
5946 Wsi.setcursor Wsi.CURSOR_INHERIT;
5947 state.mstate <- Mnone
5949 | Msel ((x0, y0), (x1, y1)) ->
5950 let rec loop = function
5951 | [] -> ()
5952 | l :: rest ->
5953 let inside =
5954 let a0 = l.pagedispy in
5955 let a1 = a0 + l.pagevh in
5956 let b0 = l.pagedispx in
5957 let b1 = b0 + l.pagevw in
5958 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5959 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5961 if inside
5962 then
5963 match getopaque l.pageno with
5964 | Some opaque ->
5965 let dosel cmd () =
5966 match Unix.pipe () with
5967 | (exception exn) ->
5968 impmsg "cannot create sel pipe: %s" @@
5969 exntos exn;
5970 | (r, w) ->
5971 let clo what fd =
5972 Ne.clo fd (fun msg ->
5973 dolog "%s close failed: %s" what msg)
5975 let pid =
5976 try spawn cmd [r, 0; w, -1]
5977 with exn ->
5978 dolog "cannot execute %S: %s"
5979 cmd @@ exntos exn;
5982 if pid > 0
5983 then (
5984 copysel w opaque;
5985 G.postRedisplay "copysel";
5987 else clo "Msel pipe/w" w;
5988 clo "Msel pipe/r" r;
5990 dosel conf.selcmd ();
5991 state.roam <- dosel conf.paxcmd;
5992 | None -> ()
5993 else loop rest
5995 loop state.layout;
5996 resetmstate ();
6000 | _ -> ()
6003 let birdseyemouse button down x y mask
6004 (conf, leftx, _, hooverpageno, anchor) =
6005 match button with
6006 | 1 when down ->
6007 let rec loop = function
6008 | [] -> ()
6009 | l :: rest ->
6010 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6011 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6012 then (
6013 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
6015 else loop rest
6017 loop state.layout
6018 | 3 -> ()
6019 | _ -> viewmouse button down x y mask
6022 let uioh = object
6023 method display = ()
6025 method key key mask =
6026 begin match state.mode with
6027 | Textentry textentry -> textentrykeyboard key mask textentry
6028 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6029 | View -> viewkeyboard key mask
6030 | LinkNav linknav -> linknavkeyboard key mask linknav
6031 end;
6032 state.uioh
6034 method button button bstate x y mask =
6035 begin match state.mode with
6036 | LinkNav _
6037 | View -> viewmouse button bstate x y mask
6038 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6039 | Textentry _ -> ()
6040 end;
6041 state.uioh
6043 method multiclick clicks x y mask =
6044 begin match state.mode with
6045 | LinkNav _
6046 | View -> viewmulticlick clicks x y mask
6047 | Birdseye _
6048 | Textentry _ -> ()
6049 end;
6050 state.uioh
6052 method motion x y =
6053 begin match state.mode with
6054 | Textentry _ -> ()
6055 | View | Birdseye _ | LinkNav _ ->
6056 match state.mstate with
6057 | Mzoom _ | Mnone -> ()
6059 | Mpan (x0, y0) ->
6060 let dx = x - x0
6061 and dy = y0 - y in
6062 state.mstate <- Mpan (x, y);
6063 let x = if canpan () then panbound (state.x + dx) else state.x in
6064 let y = clamp dy in
6065 gotoxy_and_clear_text x y
6067 | Msel (a, _) ->
6068 state.mstate <- Msel (a, (x, y));
6069 G.postRedisplay "motion select";
6071 | Mscrolly ->
6072 let y = min state.winh (max 0 y) in
6073 scrolly y
6075 | Mscrollx ->
6076 let x = min state.winw (max 0 x) in
6077 scrollx x
6079 | Mzoomrect (p0, _) ->
6080 state.mstate <- Mzoomrect (p0, (x, y));
6081 G.postRedisplay "motion zoomrect";
6082 end;
6083 state.uioh
6085 method pmotion x y =
6086 begin match state.mode with
6087 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6088 let rec loop = function
6089 | [] ->
6090 if hooverpageno != -1
6091 then (
6092 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6093 G.postRedisplay "pmotion birdseye no hoover";
6095 | l :: rest ->
6096 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6097 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6098 then (
6099 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6100 G.postRedisplay "pmotion birdseye hoover";
6102 else loop rest
6104 loop state.layout
6106 | Textentry _ -> ()
6108 | LinkNav _
6109 | View ->
6110 match state.mstate with
6111 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ -> ()
6112 | Mnone ->
6113 updateunder x y;
6114 if canselect ()
6115 then
6116 match conf.pax with
6117 | None -> ()
6118 | Some r ->
6119 let past, _, _ = !r in
6120 let now = now () in
6121 let delta = now -. past in
6122 if delta > 0.01
6123 then paxunder x y
6124 else r := (now, x, y)
6125 end;
6126 state.uioh
6128 method infochanged _ = ()
6130 method scrollph =
6131 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6132 let p, h =
6133 if maxy = 0
6134 then 0.0, float state.winh
6135 else scrollph state.y maxy
6137 vscrollw (), p, h
6139 method scrollpw =
6140 let fwinw = float (state.winw - vscrollw ()) in
6141 let sw =
6142 let sw = fwinw /. float state.w in
6143 let sw = fwinw *. sw in
6144 max sw (float conf.scrollh)
6146 let position =
6147 let maxx = state.w + state.winw in
6148 let x = state.winw - state.x in
6149 let percent = float x /. float maxx in
6150 (fwinw -. sw) *. percent
6152 hscrollh (), position, sw
6154 method modehash =
6155 let modename =
6156 match state.mode with
6157 | LinkNav _ -> "links"
6158 | Textentry _ -> "textentry"
6159 | Birdseye _ -> "birdseye"
6160 | View -> "view"
6162 findkeyhash conf modename
6164 method eformsgs = true
6165 method alwaysscrolly = false
6166 end;;
6168 let addrect pageno r g b a x0 y0 x1 y1 =
6169 Hashtbl.add state.prects pageno [|r; g; b; a; x0; y0; x1; y1|];
6172 let ract cmds =
6173 let cl = splitatspace cmds in
6174 let scan s fmt f =
6175 try Scanf.sscanf s fmt f
6176 with exn ->
6177 adderrfmt "remote exec"
6178 "error processing '%S': %s\n" cmds @@ exntos exn
6180 let rectx s pageno (r, g, b, a) x0 y0 x1 y1 =
6181 vlog "%s page %d color (%f %f %f %f) x0,y0,x1,y1 = %f %f %f %f"
6182 s pageno r g b a x0 y0 x1 y1;
6183 onpagerect
6184 pageno
6185 (fun w h ->
6186 let _,w1,h1,_ = getpagedim pageno in
6187 let sw = float w1 /. float w
6188 and sh = float h1 /. float h in
6189 let x0s = x0 *. sw
6190 and x1s = x1 *. sw
6191 and y0s = y0 *. sh
6192 and y1s = y1 *. sh in
6193 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
6194 let color = (r, g, b, a) in
6195 if conf.verbose then debugrect rect;
6196 state.rects <- (pageno, color, rect) :: state.rects;
6197 G.postRedisplay s;
6200 match cl with
6201 | "reload", "" -> reload ()
6202 | "goto", args ->
6203 scan args "%u %f %f"
6204 (fun pageno x y ->
6205 let cmd, _ = state.geomcmds in
6206 if emptystr cmd
6207 then gotopagexy !wtmode pageno x y
6208 else
6209 let f prevf () =
6210 gotopagexy !wtmode pageno x y;
6211 prevf ()
6213 state.reprf <- f state.reprf
6215 | "goto1", args -> scan args "%u %f" gotopage
6216 | "gotor", args ->
6217 scan args "%S %u"
6218 (fun filename pageno -> gotounder (Uremote (filename, pageno)))
6219 | "gotord", args ->
6220 scan args "%S %S"
6221 (fun filename dest -> gotounder (Uremotedest (filename, dest)))
6222 | "rect", args ->
6223 scan args "%u %u %f %f %f %f"
6224 (fun pageno c x0 y0 x1 y1 ->
6225 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
6226 rectx "rect" pageno color x0 y0 x1 y1;
6228 | "prect", args ->
6229 scan args "%u %f %f %f %f %f %f %f %f"
6230 (fun pageno r g b alpha x0 y0 x1 y1 ->
6231 addrect pageno r g b alpha x0 y0 x1 y1;
6232 G.postRedisplay "prect"
6234 | "pgoto", args ->
6235 scan args "%u %f %f"
6236 (fun pageno x y ->
6237 let optopaque =
6238 match getopaque pageno with
6239 | Some opaque -> opaque
6240 | None -> ~< E.s
6242 pgoto optopaque pageno x y;
6243 let rec fixx = function
6244 | [] -> ()
6245 | l :: rest ->
6246 if l.pageno = pageno
6247 then gotoxy (state.x - l.pagedispx) state.y
6248 else fixx rest
6250 let layout =
6251 let mult =
6252 match conf.columns with
6253 | Csingle _ | Csplit _ -> 1
6254 | Cmulti ((n, _, _), _) -> n
6256 layout 0 state.y (state.winw * mult) state.winh
6258 fixx layout
6260 | "activatewin", "" -> Wsi.activatewin ()
6261 | "quit", "" -> raise Quit
6262 | "keys", keys ->
6263 begin try
6264 let l = Config.keys_of_string keys in
6265 List.iter (fun (k, m) -> keyboard k m) l
6266 with exn ->
6267 adderrfmt "error processing keys" "`%S': %s\n" cmds @@ exntos exn
6269 | "clearrects", "" ->
6270 Hashtbl.clear state.prects;
6271 G.postRedisplay "clearrects"
6272 | _ ->
6273 adderrfmt "remote command"
6274 "error processing remote command: %S\n" cmds;
6277 let remote =
6278 let scratch = Bytes.create 80 in
6279 let buf = Buffer.create 80 in
6280 fun fd ->
6281 match tempfailureretry (Unix.read fd scratch 0) 80 with
6282 | (exception Unix.Unix_error (Unix.EAGAIN, _, _)) -> None
6283 | 0 ->
6284 Unix.close fd;
6285 if Buffer.length buf > 0
6286 then (
6287 let s = Buffer.contents buf in
6288 Buffer.clear buf;
6289 ract s;
6291 None
6292 | n ->
6293 let rec eat ppos =
6294 let nlpos =
6295 match Bytes.index_from scratch ppos '\n' with
6296 | pos -> if pos >= n then -1 else pos
6297 | (exception Not_found) -> -1
6299 if nlpos >= 0
6300 then (
6301 Buffer.add_subbytes buf scratch ppos (nlpos-ppos);
6302 let s = Buffer.contents buf in
6303 Buffer.clear buf;
6304 ract s;
6305 eat (nlpos+1);
6307 else (
6308 Buffer.add_subbytes buf scratch ppos (n-ppos);
6309 Some fd
6311 in eat 0
6314 let remoteopen path =
6315 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
6316 with exn ->
6317 adderrfmt "remoteopen" "error opening %S: %s" path @@ exntos exn;
6318 None
6321 let () =
6322 let gcconfig = ref E.s in
6323 let trimcachepath = ref E.s in
6324 let rcmdpath = ref E.s in
6325 let pageno = ref None in
6326 let rootwid = ref 0 in
6327 let openlast = ref false in
6328 let nofc = ref false in
6329 let doreap = ref false in
6330 selfexec := Sys.executable_name;
6331 Arg.parse
6332 (Arg.align
6333 [("-p", Arg.String (fun s -> state.password <- s),
6334 "<password> Set password");
6336 ("-f", Arg.String
6337 (fun s ->
6338 Config.fontpath := s;
6339 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
6341 "<path> Set path to the user interface font");
6343 ("-c", Arg.String
6344 (fun s ->
6345 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
6346 Config.confpath := s),
6347 "<path> Set path to the configuration file");
6349 ("-last", Arg.Set openlast, " Open last document");
6351 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
6352 "<page-number> Jump to page");
6354 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6355 "<path> Set path to the trim cache file");
6357 ("-dest", Arg.String (fun s -> state.nameddest <- s),
6358 "<named-destination> Set named destination");
6360 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
6361 ("-cxack", Arg.Set cxack, " Cut corners");
6363 ("-remote", Arg.String (fun s -> rcmdpath := s),
6364 "<path> Set path to the remote commands source");
6366 ("-origin", Arg.String (fun s -> state.origin <- s),
6367 "<original-path> Set original path");
6369 ("-gc", Arg.Set_string gcconfig,
6370 "<script-path> Collect garbage with the help of a script");
6372 ("-nofc", Arg.Set nofc, " Do not use fontconfig");
6374 ("-v", Arg.Unit (fun () ->
6375 Printf.printf
6376 "%s\nconfiguration path: %s\n"
6377 (version ())
6378 Config.defconfpath
6380 exit 0), " Print version and exit");
6382 ("-embed", Arg.Set_int rootwid,
6383 "<window-id> Embed into window")
6386 (fun s -> state.path <- s)
6387 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6389 if !wtmode
6390 then selfexec := !selfexec ^ " -wtmode";
6392 let histmode = emptystr state.path && not !openlast in
6394 if not (Config.load !openlast)
6395 then dolog "failed to load configuration";
6397 begin match !pageno with
6398 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
6399 | None -> ()
6400 end;
6402 if nonemptystr !gcconfig
6403 then (
6404 let (c, s) =
6405 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6406 | (exception exn) -> error "socketpair for gc failed: %s" @@ exntos exn
6407 | fds -> fds
6409 match spawn !gcconfig [(c, 0); (c, 1); (s, -1)] with
6410 | (exception exn) -> error "failed to execute gc script: %s" @@ exntos exn
6411 | _pid ->
6412 Ne.clo c @@ (fun s -> error "failed to close gc fd %s" s);
6413 Config.gc s;
6414 exit 0
6417 let wsfd, winw, winh = Wsi.init (object (self)
6418 val mutable m_clicks = 0
6419 val mutable m_click_x = 0
6420 val mutable m_click_y = 0
6421 val mutable m_lastclicktime = infinity
6423 method private cleanup =
6424 state.roam <- noroam;
6425 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap
6426 method expose = G.postRedisplay "expose"
6427 method visible v =
6428 let name =
6429 match v with
6430 | Wsi.Unobscured -> "unobscured"
6431 | Wsi.PartiallyObscured -> "partiallyobscured"
6432 | Wsi.FullyObscured -> "fullyobscured"
6434 vlog "visibility change %s" name
6435 method display = display ()
6436 method map mapped = vlog "mapped %b" mapped
6437 method reshape w h =
6438 self#cleanup;
6439 reshape w h
6440 method mouse b d x y m =
6441 if d && canselect ()
6442 then (
6443 (* http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx *)
6444 m_click_x <- x;
6445 m_click_y <- y;
6446 if b = 1
6447 then (
6448 let t = now () in
6449 if abs x - m_click_x > 10
6450 || abs y - m_click_y > 10
6451 || abs_float (t -. m_lastclicktime) > 0.3
6452 then m_clicks <- 0;
6453 m_clicks <- m_clicks + 1;
6454 m_lastclicktime <- t;
6455 if m_clicks = 1
6456 then (
6457 self#cleanup;
6458 G.postRedisplay "cleanup";
6459 state.uioh <- state.uioh#button b d x y m;
6461 else state.uioh <- state.uioh#multiclick m_clicks x y m
6463 else (
6464 self#cleanup;
6465 m_clicks <- 0;
6466 m_lastclicktime <- infinity;
6467 state.uioh <- state.uioh#button b d x y m
6470 else (
6471 state.uioh <- state.uioh#button b d x y m
6473 method motion x y =
6474 state.mpos <- (x, y);
6475 state.uioh <- state.uioh#motion x y
6476 method pmotion x y =
6477 state.mpos <- (x, y);
6478 state.uioh <- state.uioh#pmotion x y
6479 method key k m =
6480 let mascm = m land (
6481 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6482 ) in
6483 let keyboard k m =
6484 let x = state.x and y = state.y in
6485 keyboard k m;
6486 if x != state.x || y != state.y then self#cleanup
6488 match state.keystate with
6489 | KSnone ->
6490 let km = k, mascm in
6491 begin
6492 match
6493 let modehash = state.uioh#modehash in
6494 try Hashtbl.find modehash km
6495 with Not_found ->
6496 try Hashtbl.find (findkeyhash conf "global") km
6497 with Not_found -> KMinsrt (k, m)
6498 with
6499 | KMinsrt (k, m) -> keyboard k m
6500 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6501 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6503 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6504 List.iter (fun (k, m) -> keyboard k m) insrt;
6505 state.keystate <- KSnone
6506 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6507 state.keystate <- KSinto (keys, insrt)
6508 | KSinto _ -> state.keystate <- KSnone
6510 method enter x y =
6511 state.mpos <- (x, y);
6512 state.uioh <- state.uioh#pmotion x y
6513 method leave = state.mpos <- (-1, -1)
6514 method winstate wsl = state.winstate <- wsl
6515 method quit = raise Quit
6516 end) !rootwid conf.cwinw conf.cwinh platform in
6518 setbgcol conf.bgcolor;
6519 state.wsfd <- wsfd;
6521 if not (
6522 List.exists GlMisc.check_extension
6523 [ "GL_ARB_texture_rectangle"
6524 ; "GL_EXT_texture_recangle"
6525 ; "GL_NV_texture_rectangle" ]
6527 then (dolog "OpenGL does not suppport rectangular textures"; exit 1);
6529 if (
6530 let r = GlMisc.get_string `renderer in
6531 let p = "Mesa DRI Intel(" in
6532 let l = String.length p in
6533 String.length r > l && String.sub r 0 l = p
6535 then (
6536 defconf.sliceheight <- 1024;
6537 defconf.texcount <- 32;
6538 defconf.usepbo <- true;
6541 let cs, ss =
6542 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6543 | (exception exn) ->
6544 dolog "socketpair failed: %s" @@ exntos exn;
6545 exit 1
6546 | (r, w) ->
6547 cloexec r;
6548 cloexec w;
6549 r, w
6552 setcheckers conf.checkers;
6554 opengl_has_pbo := GlMisc.check_extension "GL_ARB_pixel_buffer_object";
6556 init cs (
6557 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
6558 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6559 !Config.fontpath, !trimcachepath,
6560 !opengl_has_pbo,
6561 not !nofc
6563 List.iter GlArray.enable [`texture_coord; `vertex];
6564 state.ss <- ss;
6565 reshape ~firsttime:true winw winh;
6566 state.uioh <- uioh;
6567 if histmode
6568 then (
6569 Wsi.settitle "llpp (history)";
6570 enterhistmode ();
6572 else (
6573 state.text <- "Opening " ^ (mbtoutf8 state.path);
6574 opendoc state.path state.password;
6576 display ();
6577 Wsi.mapwin ();
6578 Wsi.setcursor Wsi.CURSOR_INHERIT;
6579 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
6581 let rec reap () =
6582 match Unix.waitpid [Unix.WNOHANG] ~-1 with
6583 | (exception (Unix.Unix_error (Unix.ECHILD, _, _))) -> ()
6584 | (exception exn) -> dolog "Unix.waitpid: %s" @@ exntos exn
6585 | 0, _ -> ()
6586 | _pid, _status -> reap ()
6588 Sys.set_signal Sys.sigchld (Sys.Signal_handle (fun _ -> doreap := true));
6590 let optrfd =
6591 ref (
6592 if nonemptystr !rcmdpath
6593 then remoteopen !rcmdpath
6594 else None
6598 let rec loop deadline =
6599 if !doreap
6600 then (
6601 doreap := false;
6602 reap ()
6604 let r = [state.ss; state.wsfd] in
6605 let r =
6606 match !optrfd with
6607 | None -> r
6608 | Some fd -> fd :: r
6610 if state.redisplay
6611 then (
6612 state.redisplay <- false;
6613 display ();
6615 let timeout =
6616 let now = now () in
6617 if deadline > now
6618 then (
6619 if deadline = infinity
6620 then ~-.1.0
6621 else max 0.0 (deadline -. now)
6623 else 0.0
6625 let r, _, _ =
6626 try Unix.select r [] [] timeout
6627 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6629 begin match r with
6630 | [] ->
6631 state.ghyll None;
6632 let newdeadline =
6633 if state.ghyll == noghyll
6634 then
6635 match state.autoscroll with
6636 | Some step when step != 0 ->
6637 let y = state.y + step in
6638 let fy = if conf.maxhfit then state.winh else 0 in
6639 let y =
6640 if y < 0
6641 then state.maxy - fy
6642 else if y >= state.maxy - fy then 0 else y
6644 if state.mode = View
6645 then gotoxy_and_clear_text state.x y
6646 else gotoxy state.x y;
6647 deadline +. 0.01
6648 | _ -> infinity
6649 else deadline +. 0.01
6651 loop newdeadline
6653 | l ->
6654 let rec checkfds = function
6655 | [] -> ()
6656 | fd :: rest when fd = state.ss ->
6657 let cmd = rcmd state.ss in
6658 act cmd;
6659 checkfds rest
6661 | fd :: rest when fd = state.wsfd ->
6662 Wsi.readresp fd;
6663 checkfds rest
6665 | fd :: rest when Some fd = !optrfd ->
6666 begin match remote fd with
6667 | None -> optrfd := remoteopen !rcmdpath;
6668 | opt -> optrfd := opt
6669 end;
6670 checkfds rest
6672 | _ :: rest ->
6673 dolog "select returned unknown descriptor";
6674 checkfds rest
6676 checkfds l;
6677 let newdeadline =
6678 let deadline1 =
6679 if deadline = infinity
6680 then now () +. 0.01
6681 else deadline
6683 match state.autoscroll with
6684 | Some step when step != 0 -> deadline1
6685 | _ -> if state.ghyll == noghyll then infinity else deadline1
6687 loop newdeadline
6688 end;
6691 loop infinity;
6692 with Quit ->
6693 Config.save leavebirdseye;
6694 if hasunsavedchanges ()
6695 then save ();