Make addition of annotation behave the same way modification does
[llpp.git] / main.ml
blob586ef40f2c7a151eac77ebed14b51f6f2a6368c8
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 drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
16 external measurestr : int -> string -> float = "ml_measure_string";;
17 external postprocess :
18 opaque -> int -> int -> int -> (int * string * int) -> int
19 = "ml_postprocess";;
20 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
21 external setaalevel : int -> unit = "ml_setaalevel";;
22 external realloctexts : int -> bool = "ml_realloctexts";;
23 external findlink : opaque -> linkdir -> link = "ml_findlink";;
24 external getlink : opaque -> int -> under = "ml_getlink";;
25 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
26 external getlinkcount : opaque -> int = "ml_getlinkcount";;
27 external findpwl : int -> int -> pagewithlinks = "ml_find_page_with_links";;
28 external getpbo : width -> height -> colorspace -> opaque = "ml_getpbo";;
29 external freepbo : opaque -> unit = "ml_freepbo";;
30 external unmappbo : opaque -> unit = "ml_unmappbo";;
31 external pbousable : unit -> bool = "ml_pbo_usable";;
32 external unproject : opaque -> int -> int -> (int * int) option
33 = "ml_unproject";;
34 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
35 external rectofblock : opaque -> int -> int -> float array option
36 = "ml_rectofblock";;
37 external begintiles : unit -> unit = "ml_begintiles";;
38 external endtiles : unit -> unit = "ml_endtiles";;
39 external addannot : opaque -> int -> int -> string -> unit = "ml_addannot";;
40 external modannot : opaque -> slinkindex -> string -> unit = "ml_modannot";;
41 external delannot : opaque -> slinkindex -> unit = "ml_delannot";;
42 external hasunsavedchanges : unit -> bool = "ml_hasunsavedchanges";;
43 external savedoc : string -> unit = "ml_savedoc";;
44 external getannotcontents : opaque -> slinkindex -> string
45 = "ml_getannotcontents";;
47 let selfexec = ref E.s;;
49 let drawstring size x y s =
50 Gl.enable `blend;
51 Gl.enable `texture_2d;
52 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
53 ignore (drawstr size x y s);
54 Gl.disable `blend;
55 Gl.disable `texture_2d;
58 let drawstring1 size x y s =
59 drawstr size x y s;
62 let drawstring2 size x y fmt =
63 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
66 let debugl l =
67 dolog "l %d dim=%d {" l.pageno l.pagedimno;
68 dolog " WxH %dx%d" l.pagew l.pageh;
69 dolog " vWxH %dx%d" l.pagevw l.pagevh;
70 dolog " pagex,y %d,%d" l.pagex l.pagey;
71 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
72 dolog " column %d" l.pagecol;
73 dolog "}";
76 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
77 dolog "rect {";
78 dolog " x0,y0=(% f, % f)" x0 y0;
79 dolog " x1,y1=(% f, % f)" x1 y1;
80 dolog " x2,y2=(% f, % f)" x2 y2;
81 dolog " x3,y3=(% f, % f)" x3 y3;
82 dolog "}";
85 let isbirdseye = function
86 | Birdseye _ -> true
87 | Textentry _
88 | View
89 | LinkNav _ -> false
92 let istextentry = function
93 | Textentry _ -> true
94 | Birdseye _
95 | View
96 | LinkNav _ -> false
99 let wtmode = ref false;;
100 let cxack = ref false;;
102 let pgscale h = truncate (float h *. conf.pgscale);;
104 let hscrollh () =
105 if not state.uioh#alwaysscrolly && (conf.scrollb land scrollbhv = 0)
106 || (state.x = 0 && state.w <= state.winw - conf.scrollbw)
107 then 0
108 else conf.scrollbw
111 let vscrollw () =
112 if not state.uioh#alwaysscrolly && (conf.scrollb land scrollbvv = 0)
113 then 0
114 else conf.scrollbw
117 let wadjsb () = -vscrollw ();;
118 let xadjsb () = if conf.leftscroll then vscrollw () else 0;;
120 let setfontsize n =
121 fstate.fontsize <- n;
122 fstate.wwidth <- measurestr fstate.fontsize "w";
123 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
126 let vlog fmt =
127 if conf.verbose
128 then
129 Printf.kprintf prerr_endline fmt
130 else
131 Printf.kprintf ignore fmt
134 let launchpath () =
135 if emptystr conf.pathlauncher
136 then print_endline state.path
137 else (
138 let command = Str.global_replace percentsre state.path conf.pathlauncher in
139 try addpid @@ popen command []
140 with exn ->
141 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
142 flush stderr;
146 let redirectstderr () =
147 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
148 if conf.redirectstderr
149 then
150 match Unix.pipe () with
151 | exception exn ->
152 dolog "failed to create stderr redirection pipes: %s" (exntos exn)
154 | (r, w) ->
155 begin match Unix.dup Unix.stderr with
156 | exception exn ->
157 dolog "failed to dup stderr: %s" (exntos exn);
158 Ne.clo r (clofail "pipe/r");
159 Ne.clo w (clofail "pipe/w");
161 | dupstderr ->
162 begin match Unix.dup2 w Unix.stderr with
163 | exception exn ->
164 dolog "failed to dup2 to stderr: %s" (exntos exn);
165 Ne.clo dupstderr (clofail "stderr duplicate");
166 Ne.clo r (clofail "redir pipe/r");
167 Ne.clo w (clofail "redir pipe/w");
169 | () ->
170 state.stderr <- dupstderr;
171 state.errfd <- Some r;
172 end;
174 else (
175 state.newerrmsgs <- false;
176 begin match state.errfd with
177 | Some fd ->
178 begin match Unix.dup2 state.stderr Unix.stderr with
179 | exception exn ->
180 dolog "failed to dup2 original stderr: %s" (exntos exn)
181 | () ->
182 Ne.clo fd (clofail "dup of stderr");
183 state.errfd <- None;
184 end;
185 | None -> ()
186 end;
187 prerr_string (Buffer.contents state.errmsgs);
188 flush stderr;
189 Buffer.clear state.errmsgs;
193 module G =
194 struct
195 let postRedisplay who =
196 if conf.verbose
197 then prerr_endline ("redisplay for " ^ who);
198 state.redisplay <- true;
200 end;;
202 let getopaque pageno =
203 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
204 with Not_found -> None
207 let putopaque pageno opaque =
208 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
211 let pagetranslatepoint l x y =
212 let dy = y - l.pagedispy in
213 let y = dy + l.pagey in
214 let dx = x - l.pagedispx in
215 let x = dx + l.pagex in
216 (x, y);
219 let onppundermouse g x y d =
220 let rec f = function
221 | l :: rest ->
222 begin match getopaque l.pageno with
223 | Some opaque ->
224 let x0 = l.pagedispx in
225 let x1 = x0 + l.pagevw in
226 let y0 = l.pagedispy in
227 let y1 = y0 + l.pagevh in
228 if y >= y0 && y <= y1 && x >= x0 && x <= x1
229 then
230 let px, py = pagetranslatepoint l x y in
231 match g opaque l px py with
232 | Some res -> res
233 | None -> f rest
234 else f rest
235 | _ ->
236 f rest
238 | [] -> d
240 f state.layout
243 let getunder x y =
244 let g opaque l px py =
245 if state.bzoom
246 then (
247 match rectofblock opaque px py with
248 | Some a ->
249 let rect = (a.(0),a.(2),a.(1),a.(2),a.(1),a.(3),a.(0),a.(3)) in
250 state.rects <- [l.pageno, l.pageno mod 3, rect];
251 G.postRedisplay "getunder";
252 | None -> ()
254 let under = whatsunder opaque px py in
255 if under = Unone then None else Some under
257 onppundermouse g x y Unone
260 let unproject x y =
261 let g opaque l x y =
262 match unproject opaque x y with
263 | Some (x, y) -> Some (Some (opaque, l.pageno, x, y))
264 | None -> None
266 onppundermouse g x y None;
269 let showtext c s =
270 state.text <- Printf.sprintf "%c%s" c s;
271 G.postRedisplay "showtext";
274 let pipesel opaque cmd =
275 if hassel opaque
276 then
277 match Unix.pipe () with
278 | exception exn ->
279 showtext '!'
280 (Printf.sprintf "pipesel can not create pipe: %s" (exntos exn));
281 | (r, w) ->
282 let doclose what fd =
283 Ne.clo fd (fun msg -> dolog "%s close failed: %s" what msg)
285 let pid =
286 try popen cmd [r, 0; w, -1]
287 with exn ->
288 dolog "can not execute %S: %s" cmd (exntos exn);
291 if pid > 0
292 then (
293 copysel w opaque;
294 G.postRedisplay "pipesel";
296 else doclose "pipesel pipe/w" w;
297 doclose "pipesel pipe/r" r;
300 let paxunder x y =
301 let g opaque l px py =
302 if markunder opaque px py conf.paxmark
303 then (
304 Some (fun () ->
305 match getopaque l.pageno with
306 | None -> ()
307 | Some opaque -> pipesel opaque conf.paxcmd
310 else None
312 G.postRedisplay "paxunder";
313 if conf.paxmark = Mark_page
314 then
315 List.iter (fun l ->
316 match getopaque l.pageno with
317 | None -> ()
318 | Some opaque -> clearmark opaque) state.layout;
319 state.roam <-
320 onppundermouse g x y (fun () -> showtext '!' "Whoopsie daisy");
323 let selstring s =
324 match Unix.pipe () with
325 | exception exn ->
326 showtext '!' (Printf.sprintf "pipe failed: %s" (exntos exn))
327 | (r, w) ->
328 let clo cap fd =
329 Ne.clo fd (fun msg ->
330 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
333 let pid =
334 try popen conf.selcmd [r, 0; w, -1]
335 with exn ->
336 showtext '!'
337 (Printf.sprintf "failed to execute %s: %s"
338 conf.selcmd (exntos exn));
341 if pid > 0
342 then (
344 let l = String.length s in
345 let bytes = Bytes.unsafe_of_string s in
346 let n = tempfailureretry (Unix.write w bytes 0) l in
347 if n != l
348 then
349 showtext '!'
350 (Printf.sprintf
351 "failed to write %d characters to sel pipe, wrote %d"
354 with exn ->
355 showtext '!'
356 (Printf.sprintf "failed to write to sel pipe: %s"
357 (exntos exn)
360 else dolog "%s" s;
361 clo "selstring pipe/r" r;
362 clo "selstring pipe/w" w;
365 let undertext ?(nopath=false) = function
366 | Unone -> "none"
367 | Ulinkuri s -> s
368 | Ulinkgoto (pageno, _) ->
369 if nopath
370 then "page " ^ string_of_int (pageno+1)
371 else Printf.sprintf "%s: page %d" state.path (pageno+1)
372 | Utext s -> "font: " ^ s
373 | Uunexpected s -> "unexpected: " ^ s
374 | Ulaunch s -> "launch: " ^ s
375 | Unamed s -> "named: " ^ s
376 | Uremote (filename, pageno) ->
377 Printf.sprintf "%s: page %d" filename (pageno+1)
378 | Uremotedest (filename, destname) ->
379 Printf.sprintf "%s: destination %S" filename destname
380 | Uannotation (opaque, slinkindex) ->
381 "annotation: " ^ getannotcontents opaque slinkindex
384 let updateunder x y =
385 match getunder x y with
386 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
387 | Ulinkuri uri ->
388 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
389 Wsi.setcursor Wsi.CURSOR_INFO
390 | Ulinkgoto (pageno, _) ->
391 if conf.underinfo
392 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
393 Wsi.setcursor Wsi.CURSOR_INFO
394 | Utext s ->
395 if conf.underinfo then showtext 'f' ("ont: " ^ s);
396 Wsi.setcursor Wsi.CURSOR_TEXT
397 | Uunexpected s ->
398 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
399 Wsi.setcursor Wsi.CURSOR_INHERIT
400 | Ulaunch s ->
401 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
402 Wsi.setcursor Wsi.CURSOR_INHERIT
403 | Unamed s ->
404 if conf.underinfo then showtext 'n' ("amed: " ^ s);
405 Wsi.setcursor Wsi.CURSOR_INHERIT
406 | Uremote (filename, pageno) ->
407 if conf.underinfo then showtext 'r'
408 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
409 Wsi.setcursor Wsi.CURSOR_INFO
410 | Uremotedest (filename, destname) ->
411 if conf.underinfo then showtext 'r'
412 (Printf.sprintf "emote destination: %s (%S)" filename destname);
413 Wsi.setcursor Wsi.CURSOR_INFO
414 | Uannotation _ ->
415 if conf.underinfo then showtext 'a' "nnotation";
416 Wsi.setcursor Wsi.CURSOR_INFO
419 let showlinktype under =
420 if conf.underinfo && under != Unone
421 then showtext ' ' @@ undertext under
424 let intentry_with_suffix text key =
425 let c =
426 if key >= 32 && key < 127
427 then Char.chr key
428 else '\000'
430 match Char.lowercase c with
431 | '0' .. '9' ->
432 let text = addchar text c in
433 TEcont text
435 | 'k' | 'm' | 'g' ->
436 let text = addchar text c in
437 TEcont text
439 | _ ->
440 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
441 TEcont text
444 let readcmd fd =
445 let s = Bytes.create 4 in
446 let n = tempfailureretry (Unix.read fd s 0) 4 in
447 if n != 4 then error "incomplete read(len) = %d" n;
448 let len = (Char.code (Bytes.get s 0) lsl 24)
449 lor (Char.code (Bytes.get s 1) lsl 16)
450 lor (Char.code (Bytes.get s 2) lsl 8)
451 lor (Char.code (Bytes.get s 3))
453 let s = Bytes.create len in
454 let n = tempfailureretry (Unix.read fd s 0) len in
455 if n != len then error "incomplete read(data) %d vs %d" n len;
456 Bytes.to_string s
459 let wcmd fmt =
460 let b = Buffer.create 16 in
461 Buffer.add_string b "llll";
462 Printf.kbprintf
463 (fun b ->
464 let s = Buffer.to_bytes b in
465 let n = Bytes.length s in
466 let len = n - 4 in
467 (* dolog "wcmd %S" (String.sub s 4 len); *)
468 Bytes.set s 0 (Char.chr ((len lsr 24) land 0xff));
469 Bytes.set s 1 (Char.chr ((len lsr 16) land 0xff));
470 Bytes.set s 2 (Char.chr ((len lsr 8) land 0xff));
471 Bytes.set s 3 (Char.chr (len land 0xff));
472 let n' = tempfailureretry (Unix.write state.ss s 0) n in
473 if n' != n then error "write failed %d vs %d" n' n;
474 ) b fmt;
477 let nogeomcmds cmds =
478 match cmds with
479 | s, [] -> emptystr s
480 | _ -> false
483 let layoutN ((columns, coverA, coverB), b) y sh =
484 let sh = sh - (hscrollh ()) in
485 let wadj = wadjsb () in
486 let rec fold accu n =
487 if n = Array.length b
488 then accu
489 else
490 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
491 if (vy - y) > sh &&
492 (n = coverA - 1
493 || n = state.pagecount - coverB
494 || (n - coverA) mod columns = columns - 1)
495 then accu
496 else
497 let accu =
498 if vy + h > y
499 then
500 let pagey = max 0 (y - vy) in
501 let pagedispy = if pagey > 0 then 0 else vy - y in
502 let pagedispx, pagex =
503 let pdx =
504 if n = coverA - 1 || n = state.pagecount - coverB
505 then state.x + (wadj + state.winw - w) / 2
506 else dx + xoff + state.x
508 if pdx < 0
509 then 0, -pdx
510 else pdx, 0
512 let pagevw =
513 let vw = wadj + state.winw - pagedispx in
514 let pw = w - pagex in
515 min vw pw
517 let pagevh = min (h - pagey) (sh - pagedispy) in
518 if pagevw > 0 && pagevh > 0
519 then
520 let e =
521 { pageno = n
522 ; pagedimno = pdimno
523 ; pagew = w
524 ; pageh = h
525 ; pagex = pagex
526 ; pagey = pagey
527 ; pagevw = pagevw
528 ; pagevh = pagevh
529 ; pagedispx = pagedispx
530 ; pagedispy = pagedispy
531 ; pagecol = 0
534 e :: accu
535 else
536 accu
537 else
538 accu
540 fold accu (n+1)
542 if Array.length b = 0
543 then []
544 else List.rev (fold [] (page_of_y y))
547 let layoutS (columns, b) y sh =
548 let sh = sh - hscrollh () in
549 let wadj = wadjsb () in
550 let rec fold accu n =
551 if n = Array.length b
552 then accu
553 else
554 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
555 if (vy - y) > sh
556 then accu
557 else
558 let accu =
559 if vy + pageh > y
560 then
561 let x = xoff + state.x in
562 let pagey = max 0 (y - vy) in
563 let pagedispy = if pagey > 0 then 0 else vy - y in
564 let pagedispx, pagex =
565 if px = 0
566 then (
567 if x < 0
568 then 0, -x
569 else x, 0
571 else (
572 let px = px - x in
573 if px < 0
574 then -px, 0
575 else 0, px
578 let pagecolw = pagew/columns in
579 let pagedispx =
580 if pagecolw < state.winw
581 then pagedispx + ((wadj + state.winw - pagecolw) / 2)
582 else pagedispx
584 let pagevw =
585 let vw = wadj + state.winw - pagedispx in
586 let pw = pagew - pagex in
587 min vw pw
589 let pagevw = min pagevw pagecolw in
590 let pagevh = min (pageh - pagey) (sh - pagedispy) in
591 if pagevw > 0 && pagevh > 0
592 then
593 let e =
594 { pageno = n/columns
595 ; pagedimno = pdimno
596 ; pagew = pagew
597 ; pageh = pageh
598 ; pagex = pagex
599 ; pagey = pagey
600 ; pagevw = pagevw
601 ; pagevh = pagevh
602 ; pagedispx = pagedispx
603 ; pagedispy = pagedispy
604 ; pagecol = n mod columns
607 e :: accu
608 else
609 accu
610 else
611 accu
613 fold accu (n+1)
615 List.rev (fold [] 0)
618 let layout y sh =
619 if nogeomcmds state.geomcmds
620 then
621 match conf.columns with
622 | Csingle b -> layoutN ((1, 0, 0), b) y sh
623 | Cmulti c -> layoutN c y sh
624 | Csplit s -> layoutS s y sh
625 else []
628 let clamp incr =
629 let y = state.y + incr in
630 let y = max 0 y in
631 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
635 let itertiles l f =
636 let tilex = l.pagex mod conf.tilew in
637 let tiley = l.pagey mod conf.tileh in
639 let col = l.pagex / conf.tilew in
640 let row = l.pagey / conf.tileh in
642 let xadj = xadjsb () in
643 let rec rowloop row y0 dispy h =
644 if h = 0
645 then ()
646 else (
647 let dh = conf.tileh - y0 in
648 let dh = min h dh in
649 let rec colloop col x0 dispx w =
650 if w = 0
651 then ()
652 else (
653 let dw = conf.tilew - x0 in
654 let dw = min w dw in
655 let dispx' = xadj + dispx in
656 f col row dispx' dispy x0 y0 dw dh;
657 colloop (col+1) 0 (dispx+dw) (w-dw)
660 colloop col tilex l.pagedispx l.pagevw;
661 rowloop (row+1) 0 (dispy+dh) (h-dh)
664 if l.pagevw > 0 && l.pagevh > 0
665 then rowloop row tiley l.pagedispy l.pagevh;
668 let gettileopaque l col row =
669 let key =
670 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
672 try Some (Hashtbl.find state.tilemap key)
673 with Not_found -> None
676 let puttileopaque l col row gen colorspace angle opaque size elapsed =
677 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
678 Hashtbl.add state.tilemap key (opaque, size, elapsed)
681 let filledrect x0 y0 x1 y1 =
682 GlArray.disable `texture_coord;
683 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
684 GlArray.vertex `two state.vraw;
685 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
686 GlArray.enable `texture_coord;
689 let linerect x0 y0 x1 y1 =
690 GlArray.disable `texture_coord;
691 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y1; x1; y0 |];
692 GlArray.vertex `two state.vraw;
693 GlArray.draw_arrays `line_loop ~first:0 ~count:4;
694 GlArray.enable `texture_coord;
697 let drawtiles l color =
698 GlDraw.color color;
699 let wadj = wadjsb () in
700 begintiles ();
701 let f col row x y tilex tiley w h =
702 match gettileopaque l col row with
703 | Some (opaque, _, t) ->
704 let params = x, y, w, h, tilex, tiley in
705 if conf.invert
706 then GlTex.env (`mode `blend);
707 drawtile params opaque;
708 if conf.invert
709 then GlTex.env (`mode `modulate);
710 if conf.debug
711 then (
712 endtiles ();
713 let s = Printf.sprintf
714 "%d[%d,%d] %f sec"
715 l.pageno col row t
717 let w = measurestr fstate.fontsize s in
718 GlDraw.color (0.0, 0.0, 0.0);
719 filledrect (float (x-2))
720 (float (y-2))
721 (float (x+2) +. w)
722 (float (y + fstate.fontsize + 2));
723 GlDraw.color (1.0, 1.0, 1.0);
724 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
725 begintiles ();
728 | None ->
729 endtiles ();
730 let w =
731 if conf.leftscroll
732 then w
733 else
734 let lw = wadj + state.winw - x in
735 min lw w
736 and h =
737 let lh = state.winh - y in
738 min lh h
740 if conf.invert
741 then GlTex.env (`mode `blend);
742 begin match state.checkerstexid with
743 | Some id ->
744 Gl.enable `texture_2d;
745 GlTex.bind_texture ~target:`texture_2d id;
746 let x0 = float x
747 and y0 = float y
748 and x1 = float (x+w)
749 and y1 = float (y+h) in
751 let tw = float w /. 16.0
752 and th = float h /. 16.0 in
753 let tx0 = float tilex /. 16.0
754 and ty0 = float tiley /. 16.0 in
755 let tx1 = tx0 +. tw
756 and ty1 = ty0 +. th in
757 Raw.sets_float state.vraw ~pos:0
758 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
759 Raw.sets_float state.traw ~pos:0
760 [| tx0; ty0; tx0; ty1; tx1; ty0; tx1; ty1 |];
761 GlArray.vertex `two state.vraw;
762 GlArray.tex_coord `two state.traw;
763 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
764 Gl.disable `texture_2d;
766 | None ->
767 GlDraw.color (1.0, 1.0, 1.0);
768 filledrect (float x) (float y) (float (x+w)) (float (y+h));
769 end;
770 if conf.invert
771 then GlTex.env (`mode `modulate);
772 if w > 128 && h > fstate.fontsize + 10
773 then (
774 let c = if conf.invert then 1.0 else 0.0 in
775 GlDraw.color (c, c, c);
776 let c, r =
777 if conf.verbose
778 then (col*conf.tilew, row*conf.tileh)
779 else col, row
781 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
783 GlDraw.color color;
784 begintiles ();
786 itertiles l f;
787 endtiles ();
790 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
792 let tilevisible1 l x y =
793 let ax0 = l.pagex
794 and ax1 = l.pagex + l.pagevw
795 and ay0 = l.pagey
796 and ay1 = l.pagey + l.pagevh in
798 let bx0 = x
799 and by0 = y in
800 let bx1 = min (bx0 + conf.tilew) l.pagew
801 and by1 = min (by0 + conf.tileh) l.pageh in
803 let rx0 = max ax0 bx0
804 and ry0 = max ay0 by0
805 and rx1 = min ax1 bx1
806 and ry1 = min ay1 by1 in
808 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
809 nonemptyintersection
812 let tilevisible layout n x y =
813 let rec findpageinlayout m = function
814 | l :: rest when l.pageno = n ->
815 tilevisible1 l x y || (
816 match conf.columns with
817 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
818 | Csplit _
819 | Csingle _
820 | Cmulti _ -> false
822 | _ :: rest -> findpageinlayout 0 rest
823 | [] -> false
825 findpageinlayout 0 layout;
828 let tileready l x y =
829 tilevisible1 l x y &&
830 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
833 let tilepage n p layout =
834 let rec loop = function
835 | l :: rest ->
836 if l.pageno = n
837 then
838 let f col row _ _ _ _ _ _ =
839 if state.currently = Idle
840 then
841 match gettileopaque l col row with
842 | Some _ -> ()
843 | None ->
844 let x = col*conf.tilew
845 and y = row*conf.tileh in
846 let w =
847 let w = l.pagew - x in
848 min w conf.tilew
850 let h =
851 let h = l.pageh - y in
852 min h conf.tileh
854 let pbo =
855 if conf.usepbo
856 then getpbo w h conf.colorspace
857 else ~< "0"
859 wcmd "tile %s %d %d %d %d %s"
860 (~> p) x y w h (~> pbo);
861 state.currently <-
862 Tiling (
863 l, p, conf.colorspace, conf.angle,
864 state.gen, col, row, conf.tilew, conf.tileh
867 itertiles l f;
868 else
869 loop rest
871 | [] -> ()
873 if nogeomcmds state.geomcmds
874 then loop layout;
877 let preloadlayout y =
878 let y = if y < state.winh then 0 else y - state.winh in
879 let h = state.winh*3 in
880 layout y h;
883 let load pages =
884 let rec loop pages =
885 if state.currently != Idle
886 then ()
887 else
888 match pages with
889 | l :: rest ->
890 begin match getopaque l.pageno with
891 | None ->
892 wcmd "page %d %d" l.pageno l.pagedimno;
893 state.currently <- Loading (l, state.gen);
894 | Some opaque ->
895 tilepage l.pageno opaque pages;
896 loop rest
897 end;
898 | _ -> ()
900 if nogeomcmds state.geomcmds
901 then loop pages
904 let preload pages =
905 load pages;
906 if conf.preload && state.currently = Idle
907 then load (preloadlayout state.y);
910 let layoutready layout =
911 let rec fold all ls =
912 all && match ls with
913 | l :: rest ->
914 let seen = ref false in
915 let allvisible = ref true in
916 let foo col row _ _ _ _ _ _ =
917 seen := true;
918 allvisible := !allvisible &&
919 begin match gettileopaque l col row with
920 | Some _ -> true
921 | None -> false
924 itertiles l foo;
925 fold (!seen && !allvisible) rest
926 | [] -> true
928 let alltilesvisible = fold true layout in
929 alltilesvisible;
932 let gotoy y =
933 let y = bound y 0 state.maxy in
934 let y, layout, proceed =
935 match conf.maxwait with
936 | Some time when state.ghyll == noghyll ->
937 begin match state.throttle with
938 | None ->
939 let layout = layout y state.winh in
940 let ready = layoutready layout in
941 if not ready
942 then (
943 load layout;
944 state.throttle <- Some (layout, y, now ());
946 else G.postRedisplay "gotoy showall (None)";
947 y, layout, ready
948 | Some (_, _, started) ->
949 let dt = now () -. started in
950 if dt > time
951 then (
952 state.throttle <- None;
953 let layout = layout y state.winh in
954 load layout;
955 G.postRedisplay "maxwait";
956 y, layout, true
958 else -1, [], false
961 | _ ->
962 let layout = layout y state.winh in
963 if not !wtmode || layoutready layout
964 then G.postRedisplay "gotoy ready";
965 y, layout, true
967 if proceed
968 then (
969 state.y <- y;
970 state.layout <- layout;
971 begin match state.mode with
972 | LinkNav ln ->
973 begin match ln with
974 | Ltexact (pageno, linkno) ->
975 let rec loop = function
976 | [] ->
977 state.mode <- LinkNav (Ltgendir 0)
978 | l :: _ when l.pageno = pageno ->
979 begin match getopaque pageno with
980 | None -> state.mode <- LinkNav (Ltnotready (pageno, 0))
981 | Some opaque ->
982 let x0, y0, x1, y1 = getlinkrect opaque linkno in
983 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
984 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
985 then state.mode <- LinkNav (Ltgendir 0)
987 | _ :: rest -> loop rest
989 loop layout
990 | Ltnotready _ | Ltgendir _ -> ()
992 | Birdseye _
993 | Textentry _
994 | View -> ()
995 end;
996 begin match state.mode with
997 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
998 if not (pagevisible layout pageno)
999 then (
1000 match state.layout with
1001 | [] -> ()
1002 | l :: _ ->
1003 state.mode <- Birdseye (
1004 conf, leftx, l.pageno, hooverpageno, anchor
1007 | LinkNav lt ->
1008 begin match lt with
1009 | Ltnotready (_, dir)
1010 | Ltgendir dir ->
1011 let linknav =
1012 let rec loop = function
1013 | [] -> lt
1014 | l :: rest ->
1015 match getopaque l.pageno with
1016 | None -> Ltnotready (l.pageno, dir)
1017 | Some opaque ->
1018 let link =
1019 let ld =
1020 if dir = 0
1021 then LDfirstvisible (l.pagex, l.pagey, dir)
1022 else (
1023 if dir > 0 then LDfirst else LDlast
1026 findlink opaque ld
1028 match link with
1029 | Lnotfound -> loop rest
1030 | Lfound n ->
1031 showlinktype (getlink opaque n);
1032 Ltexact (l.pageno, n)
1034 loop state.layout
1036 state.mode <- LinkNav linknav
1037 | Ltexact _ -> ()
1039 | Textentry _
1040 | View -> ()
1041 end;
1042 preload layout;
1044 state.ghyll <- noghyll;
1045 if conf.updatecurs
1046 then (
1047 let mx, my = state.mpos in
1048 updateunder mx my;
1052 let conttiling pageno opaque =
1053 tilepage pageno opaque
1054 (if conf.preload then preloadlayout state.y else state.layout)
1057 let gotoy_and_clear_text y =
1058 if not conf.verbose then state.text <- E.s;
1059 gotoy y;
1062 let getanchory (n, top, dtop) =
1063 let y, h = getpageyh n in
1064 if conf.presentation
1065 then
1066 let ips = calcips h in
1067 y + truncate (top*.float h -. dtop*.float ips) + ips;
1068 else
1069 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1072 let gotoanchor anchor =
1073 gotoy (getanchory anchor);
1076 let addnav () =
1077 cbput state.hists.nav (getanchor ());
1080 let getnav dir =
1081 let anchor = cbgetc state.hists.nav dir in
1082 getanchory anchor;
1085 let gotoghyll1 single y =
1086 let scroll f n a b =
1087 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1088 let snake f a b =
1089 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1090 if f < a
1091 then s (float f /. float a)
1092 else (
1093 if f > b
1094 then 1.0 -. s ((float (f-b) /. float (n-b)))
1095 else 1.0
1098 snake f a b
1099 and summa n a b =
1100 let ins = float a *. 0.5
1101 and outs = float (n-b) *. 0.5 in
1102 let ones = b - a in
1103 ins +. outs +. float ones
1105 let rec set nab y sy =
1106 let (_N, _A, _B), y =
1107 if single
1108 then
1109 let scl = if y > sy then 2 else -2 in
1110 let _N, _, _ = nab in
1111 (_N,0,_N), y+conf.scrollstep*scl
1112 else nab,y in
1113 let sum = summa _N _A _B in
1114 let dy = float (y - sy) in
1115 state.ghyll <- (
1116 let rec gf n y1 o =
1117 if n >= _N
1118 then state.ghyll <- noghyll
1119 else
1120 let go n =
1121 let s = scroll n _N _A _B in
1122 let y1 = y1 +. ((s *. dy) /. sum) in
1123 gotoy_and_clear_text (truncate y1);
1124 state.ghyll <- gf (n+1) y1;
1126 match o with
1127 | None -> go n
1128 | Some y' when single -> set nab y' state.y
1129 | Some y' -> set (_N/2, 1, 1) y' state.y
1131 gf 0 (float state.y)
1134 match conf.ghyllscroll with
1135 | Some nab when not conf.presentation ->
1136 if state.ghyll == noghyll
1137 then set nab y state.y
1138 else state.ghyll (Some y)
1139 | _ ->
1140 gotoy_and_clear_text y
1143 let gotoghyll = gotoghyll1 false;;
1145 let gotopage n top =
1146 let y, h = getpageyh n in
1147 let y = y + (truncate (top *. float h)) in
1148 gotoghyll y
1151 let gotopage1 n top =
1152 let y = getpagey n in
1153 let y = y + top in
1154 gotoghyll y
1157 let invalidate s f =
1158 state.layout <- [];
1159 state.pdims <- [];
1160 state.rects <- [];
1161 state.rects1 <- [];
1162 match state.geomcmds with
1163 | ps, [] when emptystr ps ->
1164 f ();
1165 state.geomcmds <- s, [];
1167 | ps, [] ->
1168 state.geomcmds <- ps, [s, f];
1170 | ps, (s', _) :: rest when s' = s ->
1171 state.geomcmds <- ps, ((s, f) :: rest);
1173 | ps, cmds ->
1174 state.geomcmds <- ps, ((s, f) :: cmds);
1177 let flushpages () =
1178 Hashtbl.iter (fun _ opaque ->
1179 wcmd "freepage %s" (~> opaque);
1180 ) state.pagemap;
1181 Hashtbl.clear state.pagemap;
1184 let flushtiles () =
1185 if not (Queue.is_empty state.tilelru)
1186 then (
1187 Queue.iter (fun (k, p, s) ->
1188 wcmd "freetile %s" (~> p);
1189 state.memused <- state.memused - s;
1190 Hashtbl.remove state.tilemap k;
1191 ) state.tilelru;
1192 state.uioh#infochanged Memused;
1193 Queue.clear state.tilelru;
1195 load state.layout;
1198 let stateh h =
1199 let h = truncate (float h*.conf.zoom) in
1200 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
1201 h - d
1204 let opendoc path password =
1205 state.path <- path;
1206 state.password <- password;
1207 state.gen <- state.gen + 1;
1208 state.docinfo <- [];
1209 state.outlines <- [||];
1211 flushpages ();
1212 setaalevel conf.aalevel;
1213 let titlepath =
1214 if emptystr state.origin
1215 then path
1216 else state.origin
1218 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
1219 wcmd "open %d %d %s\000%s\000" (btod !wtmode) (btod !cxack) path password;
1220 invalidate "reqlayout"
1221 (fun () ->
1222 wcmd "reqlayout %d %d %d %s\000"
1223 conf.angle (FMTE.to_int conf.fitmodel)
1224 (stateh state.winh) state.nameddest
1228 let reload () =
1229 state.anchor <- getanchor ();
1230 opendoc state.path state.password;
1233 let scalecolor c =
1234 let c = c *. conf.colorscale in
1235 (c, c, c);
1238 let scalecolor2 (r, g, b) =
1239 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1242 let docolumns columns =
1243 let wadj = wadjsb () in
1244 match columns with
1245 | Csingle _ ->
1246 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1247 let wadj = wadjsb () in
1248 let rec loop pageno pdimno pdim y ph pdims =
1249 if pageno = state.pagecount
1250 then ()
1251 else
1252 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1253 match pdims with
1254 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1255 pdimno+1, pdim, rest
1256 | _ ->
1257 pdimno, pdim, pdims
1259 let x = max 0 (((wadj + state.winw - w) / 2) - xoff) in
1260 let y = y +
1261 (if conf.presentation
1262 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1263 else (if pageno = 0 then 0 else conf.interpagespace)
1266 a.(pageno) <- (pdimno, x, y, pdim);
1267 loop (pageno+1) pdimno pdim (y + h) h pdims
1269 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1270 conf.columns <- Csingle a;
1272 | Cmulti ((columns, coverA, coverB), _) ->
1273 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1274 let rec loop pageno pdimno pdim x y rowh pdims =
1275 let rec fixrow m = if m = pageno then () else
1276 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1277 if h < rowh
1278 then (
1279 let y = y + (rowh - h) / 2 in
1280 a.(m) <- (pdimno, x, y, pdim);
1282 fixrow (m+1)
1284 if pageno = state.pagecount
1285 then fixrow (((pageno - 1) / columns) * columns)
1286 else
1287 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1288 match pdims with
1289 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1290 pdimno+1, pdim, rest
1291 | _ ->
1292 pdimno, pdim, pdims
1294 let x, y, rowh' =
1295 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1296 then (
1297 let x = (wadj + state.winw - w) / 2 in
1298 let ips =
1299 if conf.presentation then calcips h else conf.interpagespace in
1300 x, y + ips + rowh, h
1302 else (
1303 if (pageno - coverA) mod columns = 0
1304 then (
1305 let x = max 0 (wadj + state.winw - state.w) / 2 in
1306 let y =
1307 if conf.presentation
1308 then
1309 let ips = calcips h in
1310 y + (if pageno = 0 then 0 else calcips rowh + ips)
1311 else
1312 y + (if pageno = 0 then 0 else conf.interpagespace)
1314 x, y + rowh, h
1316 else x, y, max rowh h
1319 let y =
1320 if pageno > 1 && (pageno - coverA) mod columns = 0
1321 then (
1322 let y =
1323 if pageno = columns && conf.presentation
1324 then (
1325 let ips = calcips rowh in
1326 for i = 0 to pred columns
1328 let (pdimno, x, y, pdim) = a.(i) in
1329 a.(i) <- (pdimno, x, y+ips, pdim)
1330 done;
1331 y+ips;
1333 else y
1335 fixrow (pageno - columns);
1338 else y
1340 a.(pageno) <- (pdimno, x, y, pdim);
1341 let x = x + w + xoff*2 + conf.interpagespace in
1342 loop (pageno+1) pdimno pdim x y rowh' pdims
1344 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1345 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1347 | Csplit (c, _) ->
1348 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1349 let rec loop pageno pdimno pdim y pdims =
1350 if pageno = state.pagecount
1351 then ()
1352 else
1353 let pdimno, ((_, w, h, _) as pdim), pdims =
1354 match pdims with
1355 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1356 pdimno+1, pdim, rest
1357 | _ ->
1358 pdimno, pdim, pdims
1360 let cw = w / c in
1361 let rec loop1 n x y =
1362 if n = c then y else (
1363 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1364 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1367 let y = loop1 0 0 y in
1368 loop (pageno+1) pdimno pdim y pdims
1370 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1371 conf.columns <- Csplit (c, a);
1374 let represent () =
1375 docolumns conf.columns;
1376 state.maxy <- calcheight ();
1377 if state.reprf == noreprf
1378 then (
1379 match state.mode with
1380 | Birdseye (_, _, pageno, _, _) ->
1381 let y, h = getpageyh pageno in
1382 let top = (state.winh - h) / 2 in
1383 gotoy (max 0 (y - top))
1384 | Textentry _
1385 | View
1386 | LinkNav _ -> gotoanchor state.anchor
1388 else (
1389 state.reprf ();
1390 state.reprf <- noreprf;
1394 let reshape ?(firsttime=false) w h =
1395 GlDraw.viewport ~x:0 ~y:0 ~w:w ~h:h;
1396 if not firsttime && nogeomcmds state.geomcmds
1397 then state.anchor <- getanchor ();
1399 state.winw <- w;
1400 let w = wadjsb () + (truncate (float w *. conf.zoom)) in
1401 let w = max w 2 in
1402 state.winh <- h;
1403 setfontsize fstate.fontsize;
1404 GlMat.mode `modelview;
1405 GlMat.load_identity ();
1407 GlMat.mode `projection;
1408 GlMat.load_identity ();
1409 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1410 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1411 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
1413 let relx =
1414 if conf.zoom <= 1.0
1415 then 0.0
1416 else float state.x /. float state.w
1418 invalidate "geometry"
1419 (fun () ->
1420 state.w <- w;
1421 if not firsttime
1422 then state.x <- truncate (relx *. float w);
1423 let w =
1424 match conf.columns with
1425 | Csingle _ -> w
1426 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1427 | Csplit (c, _) -> w * c
1429 wcmd "geometry %d %d %d"
1430 w (stateh h) (FMTE.to_int conf.fitmodel)
1434 let enttext () =
1435 let len = String.length state.text in
1436 let x0 = xadjsb () in
1437 let drawstring s =
1438 let hscrollh =
1439 match state.mode with
1440 | Textentry _ | View | LinkNav _ ->
1441 let h, _, _ = state.uioh#scrollpw in
1443 | Birdseye _ -> 0
1445 let rect x w =
1446 filledrect x (float (state.winh - (fstate.fontsize + 4) - hscrollh))
1447 (x+.w) (float (state.winh - hscrollh))
1450 let w = float (wadjsb () + state.winw - 1) in
1451 if state.progress >= 0.0 && state.progress < 1.0
1452 then (
1453 GlDraw.color (0.3, 0.3, 0.3);
1454 let w1 = w *. state.progress in
1455 rect (float x0) w1;
1456 GlDraw.color (0.0, 0.0, 0.0);
1457 rect (float x0+.w1) (float x0+.w-.w1)
1459 else (
1460 GlDraw.color (0.0, 0.0, 0.0);
1461 rect (float x0) w;
1464 GlDraw.color (1.0, 1.0, 1.0);
1465 drawstring fstate.fontsize
1466 (if conf.leftscroll then x0 + 2 else x0 + if len > 0 then 8 else 2)
1467 (state.winh - hscrollh - 5) s;
1469 let s =
1470 match state.mode with
1471 | Textentry ((prefix, text, _, _, _, _), _) ->
1472 let s =
1473 if len > 0
1474 then
1475 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1476 else
1477 Printf.sprintf "%s%s_" prefix text
1481 | Birdseye _
1482 | View
1483 | LinkNav _ -> state.text
1485 let s =
1486 if state.newerrmsgs
1487 then (
1488 if not (istextentry state.mode) && state.uioh#eformsgs
1489 then
1490 let s1 = "(press 'e' to review error messasges)" in
1491 if nonemptystr s then s ^ " " ^ s1 else s1
1492 else s
1494 else s
1496 if nonemptystr s
1497 then drawstring s
1500 let gctiles () =
1501 let len = Queue.length state.tilelru in
1502 let layout = lazy (
1503 match state.throttle with
1504 | None ->
1505 if conf.preload
1506 then preloadlayout state.y
1507 else state.layout
1508 | Some (layout, _, _) ->
1509 layout
1510 ) in
1511 let rec loop qpos =
1512 if state.memused <= conf.memlimit
1513 then ()
1514 else (
1515 if qpos < len
1516 then
1517 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1518 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1519 let (_, pw, ph, _) = getpagedim n in
1521 gen = state.gen
1522 && colorspace = conf.colorspace
1523 && angle = conf.angle
1524 && pagew = pw
1525 && pageh = ph
1526 && (
1527 let x = col*conf.tilew
1528 and y = row*conf.tileh in
1529 tilevisible (Lazy.force_val layout) n x y
1531 then Queue.push lruitem state.tilelru
1532 else (
1533 freepbo p;
1534 wcmd "freetile %s" (~> p);
1535 state.memused <- state.memused - s;
1536 state.uioh#infochanged Memused;
1537 Hashtbl.remove state.tilemap k;
1539 loop (qpos+1)
1542 loop 0
1545 let onpagerect pageno f =
1546 let b =
1547 match conf.columns with
1548 | Cmulti (_, b) -> b
1549 | Csingle b -> b
1550 | Csplit (_, b) -> b
1552 if pageno >= 0 && pageno < Array.length b
1553 then
1554 let (_, _, _, (_, w, h, _)) = b.(pageno) in
1555 f w h
1558 let gotopagexy1 pageno x y =
1559 let _,w1,h1,leftx = getpagedim pageno in
1560 let top = y /. (float h1) in
1561 let left = x /. (float w1) in
1562 let py, w, h = getpageywh pageno in
1563 let wh = state.winh - hscrollh () in
1564 let x = left *. (float w) in
1565 let x = leftx + state.x + truncate x in
1566 let wadj = wadjsb () in
1567 let sx =
1568 if x < 0 || x >= wadj + state.winw
1569 then state.x - x
1570 else state.x
1572 let pdy = truncate (top *. float h) in
1573 let y' = py + pdy in
1574 let dy = y' - state.y in
1575 let sy =
1576 if x != state.x || not (dy > 0 && dy < wh)
1577 then (
1578 if conf.presentation
1579 then
1580 if abs (py - y') > wh
1581 then y'
1582 else py
1583 else y';
1585 else state.y
1587 if state.x != sx || state.y != sy
1588 then (
1589 let x, y =
1590 if !wtmode
1591 then (
1592 let ww = wadj + state.winw in
1593 let qx = sx / ww
1594 and qy = pdy / wh in
1595 let x = qx * ww
1596 and y = py + qy * wh in
1597 let x = if -x + ww > w1 then -(w1-ww) else x
1598 and y' = if y + wh > state.maxy then state.maxy - wh else y in
1599 let y =
1600 if conf.presentation
1601 then
1602 if abs (py - y') > wh
1603 then y'
1604 else py
1605 else y';
1607 (x, y)
1609 else (sx, sy)
1611 state.x <- x;
1612 gotoy_and_clear_text y;
1614 else gotoy_and_clear_text state.y;
1617 let gotopagexy pageno x y =
1618 match state.mode with
1619 | Birdseye _ -> gotopage pageno 0.0
1620 | Textentry _
1621 | View
1622 | LinkNav _ -> gotopagexy1 pageno x y
1625 let getpassword () =
1626 let passcmd = getenvwithdef "LLPP_ASKPASS" conf.passcmd in
1627 if emptystr passcmd
1628 then E.s
1629 else getcmdoutput
1630 (fun s ->
1631 showtext '!' @@ "error getting password: " ^ s;
1632 dolog "%s" s) passcmd;
1635 let act cmds =
1636 (* dolog "%S" cmds; *)
1637 let cl = splitatspace cmds in
1638 let scan s fmt f =
1639 try Scanf.sscanf s fmt f
1640 with exn ->
1641 dolog "error processing '%S': %s" cmds (exntos exn);
1642 exit 1
1644 let addoutline outline =
1645 match state.currently with
1646 | Outlining outlines ->
1647 state.currently <- Outlining (outline :: outlines)
1648 | Idle -> state.currently <- Outlining [outline]
1649 | Loading _
1650 | Tiling _ ->
1651 dolog "invalid outlining state";
1652 logcurrently state.currently
1654 match cl with
1655 | "clear" :: [] ->
1656 state.uioh#infochanged Pdim;
1657 state.pdims <- [];
1659 | "clearrects" :: [] ->
1660 state.rects <- state.rects1;
1661 G.postRedisplay "clearrects";
1663 | "continue" :: args :: [] ->
1664 let n = scan args "%u" (fun n -> n) in
1665 state.pagecount <- n;
1666 begin match state.currently with
1667 | Outlining l ->
1668 state.currently <- Idle;
1669 state.outlines <- Array.of_list (List.rev l)
1670 | Idle
1671 | Loading _
1672 | Tiling _ -> ()
1673 end;
1675 let cur, cmds = state.geomcmds in
1676 if emptystr cur
1677 then failwith "umpossible";
1679 begin match List.rev cmds with
1680 | [] ->
1681 state.geomcmds <- E.s, [];
1682 state.throttle <- None;
1683 represent ();
1684 | (s, f) :: rest ->
1685 f ();
1686 state.geomcmds <- s, List.rev rest;
1687 end;
1688 if conf.maxwait = None && not !wtmode
1689 then G.postRedisplay "continue";
1691 | "msg" :: args :: [] ->
1692 showtext ' ' args
1694 | "vmsg" :: args :: [] ->
1695 if conf.verbose
1696 then showtext ' ' args
1698 | "emsg" :: args :: [] ->
1699 Buffer.add_string state.errmsgs args;
1700 state.newerrmsgs <- true;
1701 G.postRedisplay "error message"
1703 | "progress" :: args :: [] ->
1704 let progress, text =
1705 scan args "%f %n"
1706 (fun f pos ->
1707 f, String.sub args pos (String.length args - pos))
1709 state.text <- text;
1710 state.progress <- progress;
1711 G.postRedisplay "progress"
1713 | "firstmatch" :: args :: [] ->
1714 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1715 scan args "%u %d %f %f %f %f %f %f %f %f"
1716 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1717 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1719 let xoff = float (xadjsb ()) in
1720 let x0 = x0 +. xoff
1721 and x1 = x1 +. xoff
1722 and x2 = x2 +. xoff
1723 and x3 = x3 +. xoff in
1724 let y = (getpagey pageno) + truncate y0 in
1725 addnav ();
1726 gotoy y;
1727 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
1729 | "match" :: args :: [] ->
1730 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1731 scan args "%u %d %f %f %f %f %f %f %f %f"
1732 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1733 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1735 let xoff = float (xadjsb ()) in
1736 let x0 = x0 +. xoff
1737 and x1 = x1 +. xoff
1738 and x2 = x2 +. xoff
1739 and x3 = x3 +. xoff in
1740 state.rects1 <-
1741 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1743 | "page" :: args :: [] ->
1744 let pageopaques, t = scan args "%s %f" (fun p t -> p, t) in
1745 let pageopaque = ~< pageopaques in
1746 begin match state.currently with
1747 | Loading (l, gen) ->
1748 vlog "page %d took %f sec" l.pageno t;
1749 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1750 begin match state.throttle with
1751 | None ->
1752 let preloadedpages =
1753 if conf.preload
1754 then preloadlayout state.y
1755 else state.layout
1757 let evict () =
1758 let set =
1759 List.fold_left (fun s l -> IntSet.add l.pageno s)
1760 IntSet.empty preloadedpages
1762 let evictedpages =
1763 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1764 if not (IntSet.mem pageno set)
1765 then (
1766 wcmd "freepage %s" (~> opaque);
1767 key :: accu
1769 else accu
1770 ) state.pagemap []
1772 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1774 evict ();
1775 state.currently <- Idle;
1776 if gen = state.gen
1777 then (
1778 tilepage l.pageno pageopaque state.layout;
1779 load state.layout;
1780 load preloadedpages;
1781 let visible = pagevisible state.layout l.pageno in
1782 if visible
1783 then (
1784 match state.mode with
1785 | LinkNav (Ltnotready (pageno, dir)) ->
1786 if pageno = l.pageno
1787 then (
1788 let link =
1789 let ld =
1790 if dir = 0
1791 then LDfirstvisible (l.pagex, l.pagey, dir)
1792 else (
1793 if dir > 0 then LDfirst else LDlast
1796 findlink pageopaque ld
1798 match link with
1799 | Lnotfound -> ()
1800 | Lfound n ->
1801 showlinktype (getlink pageopaque n);
1802 state.mode <- LinkNav (Ltexact (l.pageno, n))
1804 | LinkNav (Ltgendir _)
1805 | LinkNav (Ltexact _)
1806 | View
1807 | Birdseye _
1808 | Textentry _ -> ()
1811 if visible && layoutready state.layout
1812 then (
1813 G.postRedisplay "page";
1817 | Some (layout, _, _) ->
1818 state.currently <- Idle;
1819 tilepage l.pageno pageopaque layout;
1820 load state.layout
1821 end;
1823 | Idle
1824 | Tiling _
1825 | Outlining _ ->
1826 dolog "Inconsistent loading state";
1827 logcurrently state.currently;
1828 exit 1
1831 | "tile" :: args :: [] ->
1832 let (x, y, opaques, size, t) =
1833 scan args "%u %u %s %u %f"
1834 (fun x y p size t -> (x, y, p, size, t))
1836 let opaque = ~< opaques in
1837 begin match state.currently with
1838 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1839 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1841 unmappbo opaque;
1842 if tilew != conf.tilew || tileh != conf.tileh
1843 then (
1844 wcmd "freetile %s" (~> opaque);
1845 state.currently <- Idle;
1846 load state.layout;
1848 else (
1849 puttileopaque l col row gen cs angle opaque size t;
1850 state.memused <- state.memused + size;
1851 state.uioh#infochanged Memused;
1852 gctiles ();
1853 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1854 opaque, size) state.tilelru;
1856 let layout =
1857 match state.throttle with
1858 | None -> state.layout
1859 | Some (layout, _, _) -> layout
1862 state.currently <- Idle;
1863 if gen = state.gen
1864 && conf.colorspace = cs
1865 && conf.angle = angle
1866 && tilevisible layout l.pageno x y
1867 then conttiling l.pageno pageopaque;
1869 begin match state.throttle with
1870 | None ->
1871 preload state.layout;
1872 if gen = state.gen
1873 && conf.colorspace = cs
1874 && conf.angle = angle
1875 && tilevisible state.layout l.pageno x y
1876 && (not !wtmode || layoutready state.layout)
1877 then G.postRedisplay "tile nothrottle";
1879 | Some (layout, y, _) ->
1880 let ready = layoutready layout in
1881 if ready
1882 then (
1883 state.y <- y;
1884 state.layout <- layout;
1885 state.throttle <- None;
1886 G.postRedisplay "throttle";
1888 else load layout;
1889 end;
1892 | Idle
1893 | Loading _
1894 | Outlining _ ->
1895 dolog "Inconsistent tiling state";
1896 logcurrently state.currently;
1897 exit 1
1900 | "pdim" :: args :: [] ->
1901 let (n, w, h, _) as pdim =
1902 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1904 let pdim =
1905 match conf.fitmodel with
1906 | FitWidth -> pdim
1907 | FitPage | FitProportional ->
1908 match conf.columns with
1909 | Csplit _ -> (n, w, h, 0)
1910 | Csingle _ | Cmulti _ -> pdim
1912 state.uioh#infochanged Pdim;
1913 state.pdims <- pdim :: state.pdims
1915 | "o" :: args :: [] ->
1916 let (l, n, t, h, pos) =
1917 scan args "%u %u %d %u %n"
1918 (fun l n t h pos -> l, n, t, h, pos)
1920 let s = String.sub args pos (String.length args - pos) in
1921 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
1923 | "ou" :: args :: [] ->
1924 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
1925 let s = String.sub args pos len in
1926 let pos2 = pos + len + 1 in
1927 let uri = String.sub args pos2 (String.length args - pos2) in
1928 addoutline (s, l, Ouri uri)
1930 | "on" :: args :: [] ->
1931 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
1932 let s = String.sub args pos (String.length args - pos) in
1933 addoutline (s, l, Onone)
1935 | "a" :: args :: [] ->
1936 let (n, l, t) =
1937 scan args "%u %d %d" (fun n l t -> n, l, t)
1939 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
1941 | "info" :: args :: [] ->
1942 let pos = nindex args '\t' in
1943 if pos >= 0 && String.sub args 0 pos = "Title"
1944 then (
1945 let s = String.sub args (pos+1) @@ String.length args - pos - 1 in
1946 conf.title <- s;
1947 Wsi.settitle s;
1949 state.docinfo <- (1, args) :: state.docinfo
1951 | "infoend" :: [] ->
1952 state.uioh#infochanged Docinfo;
1953 state.docinfo <- List.rev state.docinfo
1955 | "pass" :: l ->
1956 if l = "fail" :: []
1957 then Wsi.settitle "Wrong password";
1958 let password = getpassword () in
1959 if password == E.s
1960 then error "document is password protected"
1961 else opendoc state.path password
1963 | _ ->
1964 error "unknown cmd `%S'" cmds
1967 let onhist cb =
1968 let rc = cb.rc in
1969 let action = function
1970 | HCprev -> cbget cb ~-1
1971 | HCnext -> cbget cb 1
1972 | HCfirst -> cbget cb ~-(cb.rc)
1973 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1974 and cancel () = cb.rc <- rc
1975 in (action, cancel)
1978 let search pattern forward =
1979 match conf.columns with
1980 | Csplit _ ->
1981 showtext '!' "searching does not work properly in split columns mode"
1982 | Csingle _
1983 | Cmulti _ ->
1984 if nonemptystr pattern
1985 then
1986 let pn, py =
1987 match state.layout with
1988 | [] -> 0, 0
1989 | l :: _ ->
1990 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1992 wcmd "search %d %d %d %d,%s\000"
1993 (btod conf.icase) pn py (btod forward) pattern;
1996 let intentry text key =
1997 let c =
1998 if key >= 32 && key < 127
1999 then Char.chr key
2000 else '\000'
2002 match c with
2003 | '0' .. '9' ->
2004 let text = addchar text c in
2005 TEcont text
2007 | _ ->
2008 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2009 TEcont text
2012 let linknact f s =
2013 if nonemptystr s
2014 then (
2015 let n =
2016 let l = String.length s in
2017 let rec loop pos n = if pos = l then n else
2018 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2019 loop (pos+1) (n*26 + m)
2020 in loop 0 0
2022 let rec loop n = function
2023 | [] -> ()
2024 | l :: rest ->
2025 match getopaque l.pageno with
2026 | None -> loop n rest
2027 | Some opaque ->
2028 let m = getlinkcount opaque in
2029 if n < m
2030 then (
2031 let under = getlink opaque n in
2032 f under
2034 else loop (n-m) rest
2036 loop n state.layout;
2040 let linknentry text key =
2041 let c =
2042 if key >= 32 && key < 127
2043 then Char.chr key
2044 else '\000'
2046 match c with
2047 | 'a' .. 'z' ->
2048 let text = addchar text c in
2049 linknact (fun under -> state.text <- undertext ~nopath:true under) text;
2050 TEcont text
2052 | _ ->
2053 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2054 TEcont text
2057 let textentry text key =
2058 if key land 0xff00 = 0xff00
2059 then TEcont text
2060 else TEcont (text ^ toutf8 key)
2063 let reqlayout angle fitmodel =
2064 match state.throttle with
2065 | None ->
2066 if nogeomcmds state.geomcmds
2067 then state.anchor <- getanchor ();
2068 conf.angle <- angle mod 360;
2069 if conf.angle != 0
2070 then (
2071 match state.mode with
2072 | LinkNav _ -> state.mode <- View
2073 | Birdseye _
2074 | Textentry _
2075 | View -> ()
2077 conf.fitmodel <- fitmodel;
2078 invalidate "reqlayout"
2079 (fun () ->
2080 wcmd "reqlayout %d %d %d"
2081 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
2083 | _ -> ()
2086 let settrim trimmargins trimfuzz =
2087 if nogeomcmds state.geomcmds
2088 then state.anchor <- getanchor ();
2089 conf.trimmargins <- trimmargins;
2090 conf.trimfuzz <- trimfuzz;
2091 let x0, y0, x1, y1 = trimfuzz in
2092 invalidate "settrim"
2093 (fun () ->
2094 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2095 flushpages ();
2098 let setzoom zoom =
2099 match state.throttle with
2100 | None ->
2101 let zoom = max 0.0001 zoom in
2102 if zoom <> conf.zoom
2103 then (
2104 state.prevzoom <- (conf.zoom, state.x);
2105 conf.zoom <- zoom;
2106 reshape state.winw state.winh;
2107 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2110 | Some (layout, y, started) ->
2111 let time =
2112 match conf.maxwait with
2113 | None -> 0.0
2114 | Some t -> t
2116 let dt = now () -. started in
2117 if dt > time
2118 then (
2119 state.y <- y;
2120 load layout;
2124 let setcolumns mode columns coverA coverB =
2125 state.prevcolumns <- Some (conf.columns, conf.zoom);
2126 if columns < 0
2127 then (
2128 if isbirdseye mode
2129 then showtext '!' "split mode doesn't work in bird's eye"
2130 else (
2131 conf.columns <- Csplit (-columns, E.a);
2132 state.x <- 0;
2133 conf.zoom <- 1.0;
2136 else (
2137 if columns < 2
2138 then (
2139 conf.columns <- Csingle E.a;
2140 state.x <- 0;
2141 setzoom 1.0;
2143 else (
2144 conf.columns <- Cmulti ((columns, coverA, coverB), E.a);
2145 conf.zoom <- 1.0;
2148 reshape state.winw state.winh;
2151 let resetmstate () =
2152 state.mstate <- Mnone;
2153 Wsi.setcursor Wsi.CURSOR_INHERIT;
2156 let enterbirdseye () =
2157 let zoom = float conf.thumbw /. float state.winw in
2158 let birdseyepageno =
2159 let cy = state.winh / 2 in
2160 let fold = function
2161 | [] -> 0
2162 | l :: rest ->
2163 let rec fold best = function
2164 | [] -> best.pageno
2165 | l :: rest ->
2166 let d = cy - (l.pagedispy + l.pagevh/2)
2167 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2168 if abs d < abs dbest
2169 then fold l rest
2170 else best.pageno
2171 in fold l rest
2173 fold state.layout
2175 state.mode <- Birdseye (
2176 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2178 resetmstate ();
2179 conf.zoom <- zoom;
2180 conf.presentation <- false;
2181 conf.interpagespace <- 10;
2182 conf.hlinks <- false;
2183 conf.fitmodel <- FitPage;
2184 state.x <- 0;
2185 conf.maxwait <- None;
2186 conf.columns <- (
2187 match conf.beyecolumns with
2188 | Some c ->
2189 conf.zoom <- 1.0;
2190 Cmulti ((c, 0, 0), E.a)
2191 | None -> Csingle E.a
2193 if conf.verbose
2194 then
2195 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2196 (100.0*.zoom)
2197 else
2198 state.text <- E.s
2200 reshape state.winw state.winh;
2203 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2204 state.mode <- View;
2205 conf.zoom <- c.zoom;
2206 conf.presentation <- c.presentation;
2207 conf.interpagespace <- c.interpagespace;
2208 conf.maxwait <- c.maxwait;
2209 conf.hlinks <- c.hlinks;
2210 conf.fitmodel <- c.fitmodel;
2211 conf.beyecolumns <- (
2212 match conf.columns with
2213 | Cmulti ((c, _, _), _) -> Some c
2214 | Csingle _ -> None
2215 | Csplit _ -> failwith "leaving bird's eye split mode"
2217 conf.columns <- (
2218 match c.columns with
2219 | Cmulti (c, _) -> Cmulti (c, E.a)
2220 | Csingle _ -> Csingle E.a
2221 | Csplit (c, _) -> Csplit (c, E.a)
2223 if conf.verbose
2224 then
2225 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2226 (100.0*.conf.zoom)
2228 reshape state.winw state.winh;
2229 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2230 state.x <- leftx;
2233 let togglebirdseye () =
2234 match state.mode with
2235 | Birdseye vals -> leavebirdseye vals true
2236 | View -> enterbirdseye ()
2237 | Textentry _
2238 | LinkNav _ -> ()
2241 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2242 let pageno = max 0 (pageno - incr) in
2243 let rec loop = function
2244 | [] -> gotopage1 pageno 0
2245 | l :: _ when l.pageno = pageno ->
2246 if l.pagedispy >= 0 && l.pagey = 0
2247 then G.postRedisplay "upbirdseye"
2248 else gotopage1 pageno 0
2249 | _ :: rest -> loop rest
2251 loop state.layout;
2252 state.text <- E.s;
2253 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2256 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2257 let pageno = min (state.pagecount - 1) (pageno + incr) in
2258 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2259 let rec loop = function
2260 | [] ->
2261 let y, h = getpageyh pageno in
2262 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2263 gotoy (clamp dy)
2264 | l :: _ when l.pageno = pageno ->
2265 if l.pagevh != l.pageh
2266 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2267 else G.postRedisplay "downbirdseye"
2268 | _ :: rest -> loop rest
2270 loop state.layout;
2271 state.text <- E.s;
2274 let optentry mode _ key =
2275 let btos b = if b then "on" else "off" in
2276 if key >= 32 && key < 127
2277 then
2278 let c = Char.chr key in
2279 match c with
2280 | 's' ->
2281 let ondone s =
2282 try conf.scrollstep <- int_of_string s with exc ->
2283 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2285 TEswitch ("scroll step: ", E.s, None, intentry, ondone, true)
2287 | 'A' ->
2288 let ondone s =
2290 conf.autoscrollstep <- boundastep state.winh (int_of_string s);
2291 if state.autoscroll <> None
2292 then state.autoscroll <- Some conf.autoscrollstep
2293 with exc ->
2294 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2296 TEswitch ("auto scroll step: ", E.s, None, intentry, ondone, true)
2298 | 'C' ->
2299 let ondone s =
2301 let n, a, b = multicolumns_of_string s in
2302 setcolumns mode n a b;
2303 with exc ->
2304 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
2306 TEswitch ("columns: ", E.s, None, textentry, ondone, true)
2308 | 'Z' ->
2309 let ondone s =
2311 let zoom = float (int_of_string s) /. 100.0 in
2312 setzoom zoom
2313 with exc ->
2314 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2316 TEswitch ("zoom: ", E.s, None, intentry, ondone, true)
2318 | 't' ->
2319 let ondone s =
2321 conf.thumbw <- bound (int_of_string s) 2 4096;
2322 state.text <-
2323 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2324 begin match mode with
2325 | Birdseye beye ->
2326 leavebirdseye beye false;
2327 enterbirdseye ();
2328 | Textentry _
2329 | View
2330 | LinkNav _ -> ();
2332 with exc ->
2333 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2335 TEswitch ("thumbnail width: ", E.s, None, intentry, ondone, true)
2337 | 'R' ->
2338 let ondone s =
2339 match try
2340 Some (int_of_string s)
2341 with exc ->
2342 state.text <- Printf.sprintf "bad integer `%s': %s"
2343 s (exntos exc);
2344 None
2345 with
2346 | Some angle -> reqlayout angle conf.fitmodel
2347 | None -> ()
2349 TEswitch ("rotation: ", E.s, None, intentry, ondone, true)
2351 | 'i' ->
2352 conf.icase <- not conf.icase;
2353 TEdone ("case insensitive search " ^ (btos conf.icase))
2355 | 'p' ->
2356 conf.preload <- not conf.preload;
2357 gotoy state.y;
2358 TEdone ("preload " ^ (btos conf.preload))
2360 | 'v' ->
2361 conf.verbose <- not conf.verbose;
2362 TEdone ("verbose " ^ (btos conf.verbose))
2364 | 'd' ->
2365 conf.debug <- not conf.debug;
2366 TEdone ("debug " ^ (btos conf.debug))
2368 | 'h' ->
2369 conf.maxhfit <- not conf.maxhfit;
2370 state.maxy <- calcheight ();
2371 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2373 | 'c' ->
2374 conf.crophack <- not conf.crophack;
2375 TEdone ("crophack " ^ btos conf.crophack)
2377 | 'a' ->
2378 let s =
2379 match conf.maxwait with
2380 | None ->
2381 conf.maxwait <- Some infinity;
2382 "always wait for page to complete"
2383 | Some _ ->
2384 conf.maxwait <- None;
2385 "show placeholder if page is not ready"
2387 TEdone s
2389 | 'f' ->
2390 conf.underinfo <- not conf.underinfo;
2391 TEdone ("underinfo " ^ btos conf.underinfo)
2393 | 'P' ->
2394 conf.savebmarks <- not conf.savebmarks;
2395 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2397 | 'S' ->
2398 let ondone s =
2400 let pageno, py =
2401 match state.layout with
2402 | [] -> 0, 0
2403 | l :: _ ->
2404 l.pageno, l.pagey
2406 conf.interpagespace <- int_of_string s;
2407 docolumns conf.columns;
2408 state.maxy <- calcheight ();
2409 let y = getpagey pageno in
2410 gotoy (y + py)
2411 with exc ->
2412 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2414 TEswitch ("vertical margin: ", E.s, None, intentry, ondone, true)
2416 | 'l' ->
2417 let fm =
2418 match conf.fitmodel with
2419 | FitProportional -> FitWidth
2420 | FitWidth | FitPage -> FitProportional
2422 reqlayout conf.angle fm;
2423 TEdone ("proportional display " ^ btos (fm == FitProportional))
2425 | 'T' ->
2426 settrim (not conf.trimmargins) conf.trimfuzz;
2427 TEdone ("trim margins " ^ btos conf.trimmargins)
2429 | 'I' ->
2430 conf.invert <- not conf.invert;
2431 TEdone ("invert colors " ^ btos conf.invert)
2433 | 'x' ->
2434 let ondone s =
2435 cbput state.hists.sel s;
2436 conf.selcmd <- s;
2438 TEswitch ("selection command: ", E.s, Some (onhist state.hists.sel),
2439 textentry, ondone, true)
2441 | 'M' ->
2442 if conf.pax == None
2443 then conf.pax <- Some (ref (0.0, 0, 0))
2444 else conf.pax <- None;
2445 TEdone ("PAX " ^ btos (conf.pax != None))
2447 | _ ->
2448 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2449 TEstop
2450 else
2451 TEcont state.text
2454 class type lvsource = object
2455 method getitemcount : int
2456 method getitem : int -> (string * int)
2457 method hasaction : int -> bool
2458 method exit :
2459 uioh:uioh ->
2460 cancel:bool ->
2461 active:int ->
2462 first:int ->
2463 pan:int ->
2464 uioh option
2465 method getactive : int
2466 method getfirst : int
2467 method getpan : int
2468 method getminfo : (int * int) array
2469 end;;
2471 class virtual lvsourcebase = object
2472 val mutable m_active = 0
2473 val mutable m_first = 0
2474 val mutable m_pan = 0
2475 method getactive = m_active
2476 method getfirst = m_first
2477 method getpan = m_pan
2478 method getminfo : (int * int) array = E.a
2479 end;;
2481 let textentrykeyboard
2482 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2483 state.text <- E.s;
2484 let key =
2485 if key >= 0xffb0 && key <= 0xffb9
2486 then key - 0xffb0 + 48 else key
2488 let enttext te =
2489 state.mode <- Textentry (te, onleave);
2490 enttext ();
2491 G.postRedisplay "textentrykeyboard enttext";
2493 let histaction cmd =
2494 match opthist with
2495 | None -> ()
2496 | Some (action, _) ->
2497 state.mode <- Textentry (
2498 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
2500 G.postRedisplay "textentry histaction"
2502 match key with
2503 | @backspace ->
2504 if emptystr text && cancelonempty
2505 then (
2506 onleave Cancel;
2507 G.postRedisplay "textentrykeyboard after cancel";
2509 else
2510 let s = withoutlastutf8 text in
2511 enttext (c, s, opthist, onkey, ondone, cancelonempty)
2513 | @enter | @kpenter ->
2514 ondone text;
2515 onleave Confirm;
2516 G.postRedisplay "textentrykeyboard after confirm"
2518 | @up | @kpup -> histaction HCprev
2519 | @down | @kpdown -> histaction HCnext
2520 | @home | @kphome -> histaction HCfirst
2521 | @jend | @kpend -> histaction HClast
2523 | @escape ->
2524 if emptystr text
2525 then (
2526 begin match opthist with
2527 | None -> ()
2528 | Some (_, onhistcancel) -> onhistcancel ()
2529 end;
2530 onleave Cancel;
2531 state.text <- E.s;
2532 G.postRedisplay "textentrykeyboard after cancel2"
2534 else (
2535 enttext (c, E.s, opthist, onkey, ondone, cancelonempty)
2538 | @delete | @kpdelete -> ()
2540 | _ when key != 0
2541 && key land 0xff00 != 0xff00 (* keyboard *)
2542 && key land 0xfe00 != 0xfe00 (* xkb *)
2543 && key land 0xfd00 != 0xfd00 (* 3270 *)
2545 begin match onkey text key with
2546 | TEdone text ->
2547 ondone text;
2548 onleave Confirm;
2549 G.postRedisplay "textentrykeyboard after confirm2";
2551 | TEcont text ->
2552 enttext (c, text, opthist, onkey, ondone, cancelonempty);
2554 | TEstop ->
2555 onleave Cancel;
2556 G.postRedisplay "textentrykeyboard after cancel3"
2558 | TEswitch te ->
2559 state.mode <- Textentry (te, onleave);
2560 G.postRedisplay "textentrykeyboard switch";
2561 end;
2563 | _ ->
2564 vlog "unhandled key %s" (Wsi.keyname key)
2567 let firstof first active =
2568 if first > active || abs (first - active) > fstate.maxrows - 1
2569 then max 0 (active - (fstate.maxrows/2))
2570 else first
2573 let calcfirst first active =
2574 if active > first
2575 then
2576 let rows = active - first in
2577 if rows > fstate.maxrows then active - fstate.maxrows else first
2578 else active
2581 let scrollph y maxy =
2582 let sh = float (maxy + state.winh) /. float state.winh in
2583 let sh = float state.winh /. sh in
2584 let sh = max sh (float conf.scrollh) in
2586 let percent = float y /. float maxy in
2587 let position = (float state.winh -. sh) *. percent in
2589 let position =
2590 if position +. sh > float state.winh
2591 then float state.winh -. sh
2592 else position
2594 position, sh;
2597 let coe s = (s :> uioh);;
2599 class listview ~zebra ~helpmode ~(source:lvsource) ~trusted ~modehash =
2600 object (self)
2601 val m_pan = source#getpan
2602 val m_first = source#getfirst
2603 val m_active = source#getactive
2604 val m_qsearch = E.s
2605 val m_prev_uioh = state.uioh
2607 method private elemunder y =
2608 if y < 0
2609 then None
2610 else
2611 let n = y / (fstate.fontsize+1) in
2612 if m_first + n < source#getitemcount
2613 then (
2614 if source#hasaction (m_first + n)
2615 then Some (m_first + n)
2616 else None
2618 else None
2620 method display =
2621 Gl.enable `blend;
2622 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
2623 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2624 filledrect 0. 0. (float state.winw) (float state.winh);
2625 GlDraw.color (1., 1., 1.);
2626 Gl.enable `texture_2d;
2627 let fs = fstate.fontsize in
2628 let nfs = fs + 1 in
2629 let hw = (wadjsb () + xadjsb () + state.winw)/3 in
2630 let ww = fstate.wwidth in
2631 let tabw = 17.0*.ww in
2632 let itemcount = source#getitemcount in
2633 let minfo = source#getminfo in
2634 let x0, x1 =
2635 if conf.leftscroll
2636 then float (xadjsb ()), float (state.winw - 1)
2637 else 0.0, float (state.winw - conf.scrollbw - 1)
2639 let xadj = xadjsb () in
2640 let rec loop row =
2641 if (row - m_first) > fstate.maxrows
2642 then ()
2643 else (
2644 if row >= 0 && row < itemcount
2645 then (
2646 let (s, level) = source#getitem row in
2647 let y = (row - m_first) * nfs in
2648 let x =
2649 (if conf.leftscroll then float xadj else 5.0)
2650 +. (float (level + m_pan)) *. ww in
2651 if helpmode
2652 then GlDraw.color
2653 (let c = if row land 1 = 0 then 1.0 else 0.92 in (c,c,c));
2655 if row = m_active
2656 then (
2657 Gl.disable `texture_2d;
2658 let alpha = if source#hasaction row then 0.9 else 0.3 in
2659 GlDraw.color (1., 1., 1.) ~alpha;
2660 linerect (x0 +. 1.) (float (y + 1)) (x1) (float (y + fs + 3));
2661 Gl.enable `texture_2d;
2663 let c =
2664 if zebra && row land 1 = 1
2665 then 0.8
2666 else 1.0
2668 GlDraw.color (c,c,c);
2669 let drawtabularstring s =
2670 let drawstr x s =
2671 let x' = truncate (x0 +. x) in
2672 let pos = nindex s '\000' in
2673 if pos = -1
2674 then drawstring1 fs x' (y+nfs) s
2675 else
2676 let s1 = String.sub s 0 pos
2677 and s2 = String.sub s (pos+1) (String.length s - pos - 1) in
2678 let rec e s =
2679 if emptystr s
2680 then s
2681 else
2682 let s' = withoutlastutf8 s in
2683 let s = s' ^ "@Uellipsis" in
2684 let w = measurestr fs s in
2685 if float x' +. w +. ww < float (hw + x')
2686 then s
2687 else e s'
2689 let s1 =
2690 if float x' +. ww +. measurestr fs s1 > float (hw + x')
2691 then e s1
2692 else s1
2694 ignore (drawstring1 fs x' (y+nfs) s1);
2695 drawstring1 fs (hw + x') (y+nfs) s2
2697 if trusted
2698 then
2699 let x = if helpmode && row > 0 then x +. ww else x in
2700 let tabpos = nindex s '\t' in
2701 if tabpos > 0
2702 then
2703 let len = String.length s - tabpos - 1 in
2704 let s1 = String.sub s 0 tabpos
2705 and s2 = String.sub s (tabpos + 1) len in
2706 let nx = drawstr x s1 in
2707 let sw = nx -. x in
2708 let x = x +. (max tabw sw) in
2709 drawstr x s2
2710 else
2711 let len = String.length s - 2 in
2712 if len > 0 && s.[0] = '\xc2' && s.[1] = '\xb7'
2713 then
2714 let s = String.sub s 2 len in
2715 let x = if not helpmode then x +. ww else x in
2716 GlDraw.color (1.2, 1.2, 1.2);
2717 let vinc = drawstring1 (fs+fs/4)
2718 (truncate (x -. ww)) (y+nfs) s in
2719 GlDraw.color (1., 1., 1.);
2720 vinc +. (float fs *. 0.8)
2721 else
2722 drawstr x s
2723 else
2724 drawstr x s
2726 ignore (drawtabularstring s);
2727 loop (row+1)
2731 loop m_first;
2732 GlDraw.color (1.0, 1.0, 1.0) ~alpha:0.5;
2733 let xadj = float (xadjsb () + 5) in
2734 let rec loop row =
2735 if (row - m_first) > fstate.maxrows
2736 then ()
2737 else (
2738 if row >= 0 && row < itemcount
2739 then (
2740 let (s, level) = source#getitem row in
2741 let pos0 = nindex s '\000' in
2742 let y = (row - m_first) * nfs in
2743 let x = float (level + m_pan) *. ww in
2744 let (first, last) = minfo.(row) in
2745 let prefix =
2746 if pos0 > 0 && first > pos0
2747 then String.sub s (pos0+1) (first-pos0-1)
2748 else String.sub s 0 first
2750 let suffix = String.sub s first (last - first) in
2751 let w1 = measurestr fstate.fontsize prefix in
2752 let w2 = measurestr fstate.fontsize suffix in
2753 let x = x +. if conf.leftscroll then xadj else 5.0 in
2754 let x = if pos0 > 0 && first > pos0 then x +. float hw else x in
2755 let x0 = x +. w1
2756 and y0 = float (y+2) in
2757 let x1 = x0 +. w2
2758 and y1 = float (y+fs+3) in
2759 filledrect x0 y0 x1 y1;
2760 loop (row+1)
2764 Gl.disable `texture_2d;
2765 if Array.length minfo > 0 then loop m_first;
2766 Gl.disable `blend;
2768 method updownlevel incr =
2769 let len = source#getitemcount in
2770 let curlevel =
2771 if m_active >= 0 && m_active < len
2772 then snd (source#getitem m_active)
2773 else -1
2775 let rec flow i =
2776 if i = len then i-1 else if i = -1 then 0 else
2777 let _, l = source#getitem i in
2778 if l != curlevel then i else flow (i+incr)
2780 let active = flow m_active in
2781 let first = calcfirst m_first active in
2782 G.postRedisplay "outline updownlevel";
2783 {< m_active = active; m_first = first >}
2785 method private key1 key mask =
2786 let set1 active first qsearch =
2787 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2789 let search active pattern incr =
2790 let active = if active = -1 then m_first else active in
2791 let dosearch re =
2792 let rec loop n =
2793 if n >= 0 && n < source#getitemcount
2794 then (
2795 let s, _ = source#getitem n in
2797 (try ignore (Str.search_forward re s 0); true
2798 with Not_found -> false)
2799 then Some n
2800 else loop (n + incr)
2802 else None
2804 loop active
2807 let re = Str.regexp_case_fold pattern in
2808 dosearch re
2809 with Failure s ->
2810 state.text <- s;
2811 None
2813 let itemcount = source#getitemcount in
2814 let find start incr =
2815 let rec find i =
2816 if i = -1 || i = itemcount
2817 then -1
2818 else (
2819 if source#hasaction i
2820 then i
2821 else find (i + incr)
2824 find start
2826 let set active first =
2827 let first = bound first 0 (itemcount - fstate.maxrows) in
2828 state.text <- E.s;
2829 coe {< m_active = active; m_first = first; m_qsearch = E.s >}
2831 let navigate incr =
2832 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2833 let active, first =
2834 let incr1 = if incr > 0 then 1 else -1 in
2835 if isvisible m_first m_active
2836 then
2837 let next =
2838 let next = m_active + incr in
2839 let next =
2840 if next < 0 || next >= itemcount
2841 then -1
2842 else find next incr1
2844 if abs (m_active - next) > fstate.maxrows
2845 then -1
2846 else next
2848 if next = -1
2849 then
2850 let first = m_first + incr in
2851 let first = bound first 0 (itemcount - fstate.maxrows) in
2852 let next =
2853 let next = m_active + incr in
2854 let next = bound next 0 (itemcount - 1) in
2855 find next ~-incr1
2857 let active =
2858 if next = -1
2859 then m_active
2860 else (
2861 if isvisible first next
2862 then next
2863 else m_active
2866 active, first
2867 else
2868 let first = min next m_first in
2869 let first =
2870 if abs (next - first) > fstate.maxrows
2871 then first + incr
2872 else first
2874 next, first
2875 else
2876 let first = m_first + incr in
2877 let first = bound first 0 (itemcount - 1) in
2878 let active =
2879 let next = m_active + incr in
2880 let next = bound next 0 (itemcount - 1) in
2881 let next = find next incr1 in
2882 let active =
2883 if next = -1 || abs (m_active - first) > fstate.maxrows
2884 then (
2885 let active = if m_active = -1 then next else m_active in
2886 active
2888 else next
2890 if isvisible first active
2891 then active
2892 else -1
2894 active, first
2896 G.postRedisplay "listview navigate";
2897 set active first;
2899 match key with
2900 | (@r|@s) when Wsi.withctrl mask ->
2901 let incr = if key = @r then -1 else 1 in
2902 let active, first =
2903 match search (m_active + incr) m_qsearch incr with
2904 | None ->
2905 state.text <- m_qsearch ^ " [not found]";
2906 m_active, m_first
2907 | Some active ->
2908 state.text <- m_qsearch;
2909 active, firstof m_first active
2911 G.postRedisplay "listview ctrl-r/s";
2912 set1 active first m_qsearch;
2914 | @insert when Wsi.withctrl mask ->
2915 if m_active >= 0 && m_active < source#getitemcount
2916 then (
2917 let s, _ = source#getitem m_active in
2918 selstring s;
2920 coe self
2922 | @backspace ->
2923 if emptystr m_qsearch
2924 then coe self
2925 else (
2926 let qsearch = withoutlastutf8 m_qsearch in
2927 if emptystr qsearch
2928 then (
2929 state.text <- E.s;
2930 G.postRedisplay "listview empty qsearch";
2931 set1 m_active m_first E.s;
2933 else
2934 let active, first =
2935 match search m_active qsearch ~-1 with
2936 | None ->
2937 state.text <- qsearch ^ " [not found]";
2938 m_active, m_first
2939 | Some active ->
2940 state.text <- qsearch;
2941 active, firstof m_first active
2943 G.postRedisplay "listview backspace qsearch";
2944 set1 active first qsearch
2947 | key when (key != 0 && key land 0xff00 != 0xff00) ->
2948 let pattern = m_qsearch ^ toutf8 key in
2949 let active, first =
2950 match search m_active pattern 1 with
2951 | None ->
2952 state.text <- pattern ^ " [not found]";
2953 m_active, m_first
2954 | Some active ->
2955 state.text <- pattern;
2956 active, firstof m_first active
2958 G.postRedisplay "listview qsearch add";
2959 set1 active first pattern;
2961 | @escape ->
2962 state.text <- E.s;
2963 if emptystr m_qsearch
2964 then (
2965 G.postRedisplay "list view escape";
2966 let mx, my = state.mpos in
2967 updateunder mx my;
2968 begin
2969 match
2970 source#exit ~uioh:(coe self)
2971 ~cancel:true ~active:m_active ~first:m_first ~pan:m_pan
2972 with
2973 | None -> m_prev_uioh
2974 | Some uioh -> uioh
2977 else (
2978 G.postRedisplay "list view kill qsearch";
2979 coe {< m_qsearch = E.s >}
2982 | @enter | @kpenter ->
2983 state.text <- E.s;
2984 let self = {< m_qsearch = E.s >} in
2985 let opt =
2986 G.postRedisplay "listview enter";
2987 if m_active >= 0 && m_active < source#getitemcount
2988 then (
2989 source#exit ~uioh:(coe self) ~cancel:false
2990 ~active:m_active ~first:m_first ~pan:m_pan;
2992 else (
2993 source#exit ~uioh:(coe self) ~cancel:true
2994 ~active:m_active ~first:m_first ~pan:m_pan;
2997 begin match opt with
2998 | None -> m_prev_uioh
2999 | Some uioh -> uioh
3002 | @delete | @kpdelete ->
3003 coe self
3005 | @up | @kpup -> navigate ~-1
3006 | @down | @kpdown -> navigate 1
3007 | @prior | @kpprior -> navigate ~-(fstate.maxrows)
3008 | @next | @kpnext -> navigate fstate.maxrows
3010 | @right | @kpright ->
3011 state.text <- E.s;
3012 G.postRedisplay "listview right";
3013 coe {< m_pan = m_pan - 1 >}
3015 | @left | @kpleft ->
3016 state.text <- E.s;
3017 G.postRedisplay "listview left";
3018 coe {< m_pan = m_pan + 1 >}
3020 | @home | @kphome ->
3021 let active = find 0 1 in
3022 G.postRedisplay "listview home";
3023 set active 0;
3025 | @jend | @kpend ->
3026 let first = max 0 (itemcount - fstate.maxrows) in
3027 let active = find (itemcount - 1) ~-1 in
3028 G.postRedisplay "listview end";
3029 set active first;
3031 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3032 coe self
3034 | _ ->
3035 dolog "listview unknown key %#x" key; coe self
3037 method key key mask =
3038 match state.mode with
3039 | Textentry te -> textentrykeyboard key mask te; coe self
3040 | Birdseye _
3041 | View
3042 | LinkNav _ -> self#key1 key mask
3044 method button button down x y _ =
3045 let opt =
3046 match button with
3047 | 1 when x > state.winw - conf.scrollbw ->
3048 G.postRedisplay "listview scroll";
3049 if down
3050 then
3051 let _, position, sh = self#scrollph in
3052 if y > truncate position && y < truncate (position +. sh)
3053 then (
3054 state.mstate <- Mscrolly;
3055 Some (coe self)
3057 else
3058 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3059 let first = truncate (s *. float source#getitemcount) in
3060 let first = min source#getitemcount first in
3061 Some (coe {< m_first = first; m_active = first >})
3062 else (
3063 state.mstate <- Mnone;
3064 Some (coe self);
3066 | 1 when down ->
3067 begin match self#elemunder y with
3068 | Some n ->
3069 G.postRedisplay "listview click";
3070 source#exit ~uioh:(coe {< m_active = n >})
3071 ~cancel:false ~active:n ~first:m_first ~pan:m_pan
3072 | _ ->
3073 Some (coe self)
3075 | n when (n == 4 || n == 5) && not down ->
3076 let len = source#getitemcount in
3077 let first =
3078 if n = 5 && m_first + fstate.maxrows >= len
3079 then
3080 m_first
3081 else
3082 let first = m_first + (if n == 4 then -1 else 1) in
3083 bound first 0 (len - 1)
3085 G.postRedisplay "listview wheel";
3086 Some (coe {< m_first = first >})
3087 | n when (n = 6 || n = 7) && not down ->
3088 let inc = if n = 7 then -1 else 1 in
3089 G.postRedisplay "listview hwheel";
3090 Some (coe {< m_pan = m_pan + inc >})
3091 | _ ->
3092 Some (coe self)
3094 match opt with
3095 | None -> m_prev_uioh
3096 | Some uioh -> uioh
3098 method multiclick _ x y = self#button 1 true x y
3100 method motion _ y =
3101 match state.mstate with
3102 | Mscrolly ->
3103 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3104 let first = truncate (s *. float source#getitemcount) in
3105 let first = min source#getitemcount first in
3106 G.postRedisplay "listview motion";
3107 coe {< m_first = first; m_active = first >}
3108 | Msel _
3109 | Mpan _
3110 | Mscrollx
3111 | Mzoom _
3112 | Mzoomrect _
3113 | Mnone -> coe self
3115 method pmotion x y =
3116 if x < state.winw - conf.scrollbw
3117 then
3118 let n =
3119 match self#elemunder y with
3120 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3121 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3123 let o =
3124 if n != m_active
3125 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3126 else self
3128 coe o
3129 else (
3130 Wsi.setcursor Wsi.CURSOR_INHERIT;
3131 coe self
3134 method infochanged _ = ()
3136 method scrollpw = (0, 0.0, 0.0)
3137 method scrollph =
3138 let nfs = fstate.fontsize + 1 in
3139 let y = m_first * nfs in
3140 let itemcount = source#getitemcount in
3141 let maxi = max 0 (itemcount - fstate.maxrows) in
3142 let maxy = maxi * nfs in
3143 let p, h = scrollph y maxy in
3144 conf.scrollbw, p, h
3146 method modehash = modehash
3147 method eformsgs = false
3148 method alwaysscrolly = true
3149 end;;
3151 class outlinelistview ~zebra ~source =
3152 let settext autonarrow s =
3153 if autonarrow
3154 then
3155 let ss = source#statestr in
3156 state.text <-
3157 if emptystr ss
3158 then "[" ^ s ^ "]"
3159 else "{" ^ ss ^ "} [" ^ s ^ "]"
3160 else state.text <- s
3162 object (self)
3163 inherit listview
3164 ~zebra
3165 ~helpmode:false
3166 ~source:(source :> lvsource)
3167 ~trusted:false
3168 ~modehash:(findkeyhash conf "outline")
3169 as super
3171 val m_autonarrow = false
3173 method! key key mask =
3174 let maxrows =
3175 if emptystr state.text
3176 then fstate.maxrows
3177 else fstate.maxrows - 2
3179 let calcfirst first active =
3180 if active > first
3181 then
3182 let rows = active - first in
3183 if rows > maxrows then active - maxrows else first
3184 else active
3186 let navigate incr =
3187 let active = m_active + incr in
3188 let active = bound active 0 (source#getitemcount - 1) in
3189 let first = calcfirst m_first active in
3190 G.postRedisplay "outline navigate";
3191 coe {< m_active = active; m_first = first >}
3193 let navscroll first =
3194 let active =
3195 let dist = m_active - first in
3196 if dist < 0
3197 then first
3198 else (
3199 if dist < maxrows
3200 then m_active
3201 else first + maxrows
3204 G.postRedisplay "outline navscroll";
3205 coe {< m_first = first; m_active = active >}
3207 let ctrl = Wsi.withctrl mask in
3208 match key with
3209 | @a when ctrl ->
3210 let text =
3211 if m_autonarrow
3212 then (source#denarrow; E.s)
3213 else (
3214 let pattern = source#renarrow in
3215 if nonemptystr m_qsearch
3216 then (source#narrow m_qsearch; m_qsearch)
3217 else pattern
3220 settext (not m_autonarrow) text;
3221 G.postRedisplay "toggle auto narrowing";
3222 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
3224 | @slash when emptystr m_qsearch && not m_autonarrow ->
3225 settext true E.s;
3226 G.postRedisplay "toggle auto narrowing";
3227 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
3229 | @n when ctrl ->
3230 source#narrow m_qsearch;
3231 if not m_autonarrow
3232 then source#add_narrow_pattern m_qsearch;
3233 G.postRedisplay "outline ctrl-n";
3234 coe {< m_first = 0; m_active = 0 >}
3236 | @S when ctrl ->
3237 let active = source#calcactive (getanchor ()) in
3238 let first = firstof m_first active in
3239 G.postRedisplay "outline ctrl-s";
3240 coe {< m_first = first; m_active = active >}
3242 | @u when ctrl ->
3243 G.postRedisplay "outline ctrl-u";
3244 if m_autonarrow && nonemptystr m_qsearch
3245 then (
3246 ignore (source#renarrow);
3247 settext m_autonarrow E.s;
3248 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3250 else (
3251 source#del_narrow_pattern;
3252 let pattern = source#renarrow in
3253 let text =
3254 if emptystr pattern then E.s else "Narrowed to " ^ pattern
3256 settext m_autonarrow text;
3257 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3260 | @l when ctrl ->
3261 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3262 G.postRedisplay "outline ctrl-l";
3263 coe {< m_first = first >}
3265 | @tab when m_autonarrow ->
3266 if nonemptystr m_qsearch
3267 then (
3268 G.postRedisplay "outline list view tab";
3269 source#add_narrow_pattern m_qsearch;
3270 settext true E.s;
3271 coe {< m_qsearch = E.s >}
3273 else coe self
3275 | @escape when m_autonarrow ->
3276 if nonemptystr m_qsearch
3277 then source#add_narrow_pattern m_qsearch;
3278 super#key key mask
3280 | @enter | @kpenter when m_autonarrow ->
3281 if nonemptystr m_qsearch
3282 then source#add_narrow_pattern m_qsearch;
3283 super#key key mask
3285 | key when m_autonarrow && (key != 0 && key land 0xff00 != 0xff00) ->
3286 let pattern = m_qsearch ^ toutf8 key in
3287 G.postRedisplay "outlinelistview autonarrow add";
3288 source#narrow pattern;
3289 settext true pattern;
3290 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3292 | key when m_autonarrow && key = @backspace ->
3293 if emptystr m_qsearch
3294 then coe self
3295 else
3296 let pattern = withoutlastutf8 m_qsearch in
3297 G.postRedisplay "outlinelistview autonarrow backspace";
3298 ignore (source#renarrow);
3299 source#narrow pattern;
3300 settext true pattern;
3301 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3303 | @delete | @kpdelete ->
3304 source#remove m_active;
3305 G.postRedisplay "outline delete";
3306 let active = max 0 (m_active-1) in
3307 coe {< m_first = firstof m_first active;
3308 m_active = active >}
3310 | @up | @kpup when ctrl ->
3311 navscroll (max 0 (m_first - 1))
3313 | @down | @kpdown when ctrl ->
3314 navscroll (min (source#getitemcount - 1) (m_first + 1))
3316 | @up | @kpup -> navigate ~-1
3317 | @down | @kpdown -> navigate 1
3318 | @prior | @kpprior -> navigate ~-(fstate.maxrows)
3319 | @next | @kpnext -> navigate fstate.maxrows
3321 | @right | @kpright ->
3322 let o =
3323 if ctrl
3324 then (
3325 G.postRedisplay "outline ctrl right";
3326 {< m_pan = m_pan + 1 >}
3328 else self#updownlevel 1
3330 coe o
3332 | @left | @kpleft ->
3333 let o =
3334 if ctrl
3335 then (
3336 G.postRedisplay "outline ctrl left";
3337 {< m_pan = m_pan - 1 >}
3339 else self#updownlevel ~-1
3341 coe o
3343 | @home | @kphome ->
3344 G.postRedisplay "outline home";
3345 coe {< m_first = 0; m_active = 0 >}
3347 | @jend | @kpend ->
3348 let active = source#getitemcount - 1 in
3349 let first = max 0 (active - fstate.maxrows) in
3350 G.postRedisplay "outline end";
3351 coe {< m_active = active; m_first = first >}
3353 | _ -> super#key key mask
3356 let genhistoutlines =
3357 let order ty (p1, c1, _, _, _, _) (p2, c2, _, _, _, _) =
3358 match ty with
3359 | `lastvisit -> compare c1.lastvisit c2.lastvisit
3360 | `path -> compare p2 p1
3361 | `file -> compare (Filename.basename p2) (Filename.basename p1)
3362 | `title ->
3363 let e1 = emptystr c1.title
3364 and e2 = emptystr c2.title in
3365 (**) if e1 && e2
3366 then compare (Filename.basename p2) (Filename.basename p1)
3367 else if e1 then -1
3368 else if e2 then 1
3369 else compare c1.title c2.title
3371 let showfullpath = ref false in
3372 let showorigin = ref true in
3373 let orderty : historder ref = ref `lastvisit in
3374 let rec f () =
3375 let setorty s t =
3376 let s = if !orderty = t then "[@Uradical] " ^ s else "[ ] " ^ s in
3377 s, 0, Oreaction (fun () -> orderty := t; f ())
3379 match Config.gethist () with
3380 | [] -> E.a
3381 | list ->
3382 let ol =
3383 List.fold_left
3384 (fun accu (path, c, b, x, a, o) ->
3385 let hist = (path, (c, b, x, a, o)) in
3386 let s =
3387 let s = if nonemptystr o && !showorigin then o else path in
3388 if !showfullpath then s else Filename.basename s
3390 let base = mbtoutf8 s in
3391 (base ^ "\000" ^ c.title, 1, Ohistory hist) :: accu
3393 [ setorty "Sort by time of last visit" `lastvisit;
3394 setorty "Sort by file name" `file;
3395 setorty "Sort by path" `path;
3396 setorty "Sort by title" `title;
3397 (if !showfullpath then "@Uradical "
3398 else " ") ^ "Show full path", 0, Oreaction (fun () ->
3399 showfullpath := not !showfullpath;
3400 f ());
3401 (if !showorigin then "@Uradical "
3402 else " ") ^ "Show origin", 0, Oreaction (fun () ->
3403 showorigin := not !showorigin;
3404 f ())
3405 ] (List.sort (order !orderty) list)
3407 Array.of_list ol
3408 in f
3411 let gotohist (path, (c, bookmarks, x, anchor, origin)) =
3412 Config.save leavebirdseye;
3413 state.anchor <- anchor;
3414 state.bookmarks <- bookmarks;
3415 state.origin <- origin;
3416 state.x <- x;
3417 setconf conf c;
3418 let x0, y0, x1, y1 = conf.trimfuzz in
3419 wcmd "trimset %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1;
3420 reshape ~firsttime:true state.winw state.winh;
3421 opendoc path origin;
3422 setzoom c.zoom;
3425 let makecheckers () =
3426 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3427 following to say:
3428 converted by Issac Trotts. July 25, 2002 *)
3429 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3430 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3431 let id = GlTex.gen_texture () in
3432 GlTex.bind_texture ~target:`texture_2d id;
3433 GlPix.store (`unpack_alignment 1);
3434 GlTex.image2d image;
3435 List.iter (GlTex.parameter ~target:`texture_2d)
3436 [ `mag_filter `nearest; `min_filter `nearest ];
3440 let setcheckers enabled =
3441 match state.checkerstexid with
3442 | None ->
3443 if enabled then state.checkerstexid <- Some (makecheckers ())
3445 | Some checkerstexid ->
3446 if not enabled
3447 then (
3448 GlTex.delete_texture checkerstexid;
3449 state.checkerstexid <- None;
3453 let describe_location () =
3454 let fn = page_of_y state.y in
3455 let ln = page_of_y (state.y + state.winh - hscrollh () - 1) in
3456 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
3457 let percent =
3458 if maxy <= 0
3459 then 100.
3460 else (100. *. (float state.y /. float maxy))
3462 if fn = ln
3463 then
3464 Printf.sprintf "page %d of %d [%.2f%%]"
3465 (fn+1) state.pagecount percent
3466 else
3467 Printf.sprintf
3468 "pages %d-%d of %d [%.2f%%]"
3469 (fn+1) (ln+1) state.pagecount percent
3472 let setpresentationmode v =
3473 let n = page_of_y state.y in
3474 state.anchor <- (n, 0.0, 1.0);
3475 conf.presentation <- v;
3476 if conf.fitmodel = FitPage
3477 then reqlayout conf.angle conf.fitmodel;
3478 represent ();
3481 let enterinfomode =
3482 let btos b = if b then "@Uradical" else E.s in
3483 let showextended = ref false in
3484 let leave mode _ = state.mode <- mode in
3485 let src =
3486 (object
3487 val mutable m_first_time = true
3488 val mutable m_l = []
3489 val mutable m_a = E.a
3490 val mutable m_prev_uioh = nouioh
3491 val mutable m_prev_mode = View
3493 inherit lvsourcebase
3495 method reset prev_mode prev_uioh =
3496 m_a <- Array.of_list (List.rev m_l);
3497 m_l <- [];
3498 m_prev_mode <- prev_mode;
3499 m_prev_uioh <- prev_uioh;
3500 if m_first_time
3501 then (
3502 let rec loop n =
3503 if n >= Array.length m_a
3504 then ()
3505 else
3506 match m_a.(n) with
3507 | _, _, _, Action _ -> m_active <- n
3508 | _, _, _, Noaction -> loop (n+1)
3510 loop 0;
3511 m_first_time <- false;
3514 method int name get set =
3515 m_l <-
3516 (name, `int get, 1, Action (
3517 fun u ->
3518 let ondone s =
3519 try set (int_of_string s)
3520 with exn ->
3521 state.text <- Printf.sprintf "bad integer `%s': %s"
3522 s (exntos exn)
3524 state.text <- E.s;
3525 let te = name ^ ": ", E.s, None, intentry, ondone, true in
3526 state.mode <- Textentry (te, leave m_prev_mode);
3528 )) :: m_l
3530 method int_with_suffix name get set =
3531 m_l <-
3532 (name, `intws get, 1, Action (
3533 fun u ->
3534 let ondone s =
3535 try set (int_of_string_with_suffix s)
3536 with exn ->
3537 state.text <- Printf.sprintf "bad integer `%s': %s"
3538 s (exntos exn)
3540 state.text <- E.s;
3541 let te =
3542 name ^ ": ", E.s, None, intentry_with_suffix, ondone, true
3544 state.mode <- Textentry (te, leave m_prev_mode);
3546 )) :: m_l
3548 method bool ?(offset=1) ?(btos=btos) name get set =
3549 m_l <-
3550 (name, `bool (btos, get), offset, Action (
3551 fun u ->
3552 let v = get () in
3553 set (not v);
3555 )) :: m_l
3557 method color name get set =
3558 m_l <-
3559 (name, `color get, 1, Action (
3560 fun u ->
3561 let invalid = (nan, nan, nan) in
3562 let ondone s =
3563 let c =
3564 try color_of_string s
3565 with exn ->
3566 state.text <- Printf.sprintf "bad color `%s': %s"
3567 s (exntos exn);
3568 invalid
3570 if c <> invalid
3571 then set c;
3573 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3574 state.text <- color_to_string (get ());
3575 state.mode <- Textentry (te, leave m_prev_mode);
3577 )) :: m_l
3579 method string name get set =
3580 m_l <-
3581 (name, `string get, 1, Action (
3582 fun u ->
3583 let ondone s = set s in
3584 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3585 state.mode <- Textentry (te, leave m_prev_mode);
3587 )) :: m_l
3589 method colorspace name get set =
3590 m_l <-
3591 (name, `string get, 1, Action (
3592 fun _ ->
3593 let source =
3594 (object
3595 inherit lvsourcebase
3597 initializer
3598 m_active <- CSTE.to_int conf.colorspace;
3599 m_first <- 0;
3601 method getitemcount =
3602 Array.length CSTE.names
3603 method getitem n =
3604 (CSTE.names.(n), 0)
3605 method exit ~uioh ~cancel ~active ~first ~pan =
3606 ignore (uioh, first, pan);
3607 if not cancel then set active;
3608 None
3609 method hasaction _ = true
3610 end)
3612 state.text <- E.s;
3613 let modehash = findkeyhash conf "info" in
3614 coe (new listview ~zebra:false ~helpmode:false
3615 ~source ~trusted:true ~modehash)
3616 )) :: m_l
3618 method paxmark name get set =
3619 m_l <-
3620 (name, `string get, 1, Action (
3621 fun _ ->
3622 let source =
3623 (object
3624 inherit lvsourcebase
3626 initializer
3627 m_active <- MTE.to_int conf.paxmark;
3628 m_first <- 0;
3630 method getitemcount = Array.length MTE.names
3631 method getitem n = (MTE.names.(n), 0)
3632 method exit ~uioh ~cancel ~active ~first ~pan =
3633 ignore (uioh, first, pan);
3634 if not cancel then set active;
3635 None
3636 method hasaction _ = true
3637 end)
3639 state.text <- E.s;
3640 let modehash = findkeyhash conf "info" in
3641 coe (new listview ~zebra:false ~helpmode:false
3642 ~source ~trusted:true ~modehash)
3643 )) :: m_l
3645 method fitmodel name get set =
3646 m_l <-
3647 (name, `string get, 1, Action (
3648 fun _ ->
3649 let source =
3650 (object
3651 inherit lvsourcebase
3653 initializer
3654 m_active <- FMTE.to_int conf.fitmodel;
3655 m_first <- 0;
3657 method getitemcount = Array.length FMTE.names
3658 method getitem n = (FMTE.names.(n), 0)
3659 method exit ~uioh ~cancel ~active ~first ~pan =
3660 ignore (uioh, first, pan);
3661 if not cancel then set active;
3662 None
3663 method hasaction _ = true
3664 end)
3666 state.text <- E.s;
3667 let modehash = findkeyhash conf "info" in
3668 coe (new listview ~zebra:false ~helpmode:false
3669 ~source ~trusted:true ~modehash)
3670 )) :: m_l
3672 method caption s offset =
3673 m_l <- (s, `empty, offset, Noaction) :: m_l
3675 method caption2 s f offset =
3676 m_l <- (s, `string f, offset, Noaction) :: m_l
3678 method getitemcount = Array.length m_a
3680 method getitem n =
3681 let tostr = function
3682 | `int f -> string_of_int (f ())
3683 | `intws f -> string_with_suffix_of_int (f ())
3684 | `string f -> f ()
3685 | `color f -> color_to_string (f ())
3686 | `bool (btos, f) -> btos (f ())
3687 | `empty -> E.s
3689 let name, t, offset, _ = m_a.(n) in
3690 ((let s = tostr t in
3691 if nonemptystr s
3692 then Printf.sprintf "%s\t%s" name s
3693 else name),
3694 offset)
3696 method exit ~uioh ~cancel ~active ~first ~pan =
3697 let uiohopt =
3698 if not cancel
3699 then (
3700 let uioh =
3701 match m_a.(active) with
3702 | _, _, _, Action f -> f uioh
3703 | _, _, _, Noaction -> uioh
3705 Some uioh
3707 else None
3709 m_active <- active;
3710 m_first <- first;
3711 m_pan <- pan;
3712 uiohopt
3714 method hasaction n =
3715 match m_a.(n) with
3716 | _, _, _, Action _ -> true
3717 | _, _, _, Noaction -> false
3718 end)
3720 let rec fillsrc prevmode prevuioh =
3721 let sep () = src#caption E.s 0 in
3722 let colorp name get set =
3723 src#string name
3724 (fun () -> color_to_string (get ()))
3725 (fun v ->
3727 let c = color_of_string v in
3728 set c
3729 with exn ->
3730 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
3733 let oldmode = state.mode in
3734 let birdseye = isbirdseye state.mode in
3736 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3738 src#bool "presentation mode"
3739 (fun () -> conf.presentation)
3740 (fun v -> setpresentationmode v);
3742 src#bool "ignore case in searches"
3743 (fun () -> conf.icase)
3744 (fun v -> conf.icase <- v);
3746 src#bool "preload"
3747 (fun () -> conf.preload)
3748 (fun v -> conf.preload <- v);
3750 src#bool "highlight links"
3751 (fun () -> conf.hlinks)
3752 (fun v -> conf.hlinks <- v);
3754 src#bool "under info"
3755 (fun () -> conf.underinfo)
3756 (fun v -> conf.underinfo <- v);
3758 src#bool "persistent bookmarks"
3759 (fun () -> conf.savebmarks)
3760 (fun v -> conf.savebmarks <- v);
3762 src#fitmodel "fit model"
3763 (fun () -> FMTE.to_string conf.fitmodel)
3764 (fun v -> reqlayout conf.angle (FMTE.of_int v));
3766 src#bool "trim margins"
3767 (fun () -> conf.trimmargins)
3768 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3770 src#bool "persistent location"
3771 (fun () -> conf.jumpback)
3772 (fun v -> conf.jumpback <- v);
3774 sep ();
3775 src#int "inter-page space"
3776 (fun () -> conf.interpagespace)
3777 (fun n ->
3778 conf.interpagespace <- n;
3779 docolumns conf.columns;
3780 let pageno, py =
3781 match state.layout with
3782 | [] -> 0, 0
3783 | l :: _ ->
3784 l.pageno, l.pagey
3786 state.maxy <- calcheight ();
3787 let y = getpagey pageno in
3788 gotoy (y + py)
3791 src#int "page bias"
3792 (fun () -> conf.pagebias)
3793 (fun v -> conf.pagebias <- v);
3795 src#int "scroll step"
3796 (fun () -> conf.scrollstep)
3797 (fun n -> conf.scrollstep <- n);
3799 src#int "horizontal scroll step"
3800 (fun () -> conf.hscrollstep)
3801 (fun v -> conf.hscrollstep <- v);
3803 src#int "auto scroll step"
3804 (fun () ->
3805 match state.autoscroll with
3806 | Some step -> step
3807 | _ -> conf.autoscrollstep)
3808 (fun n ->
3809 let n = boundastep state.winh n in
3810 if state.autoscroll <> None
3811 then state.autoscroll <- Some n;
3812 conf.autoscrollstep <- n);
3814 src#int "zoom"
3815 (fun () -> truncate (conf.zoom *. 100.))
3816 (fun v -> setzoom ((float v) /. 100.));
3818 src#int "rotation"
3819 (fun () -> conf.angle)
3820 (fun v -> reqlayout v conf.fitmodel);
3822 src#int "scroll bar width"
3823 (fun () -> conf.scrollbw)
3824 (fun v ->
3825 conf.scrollbw <- v;
3826 reshape state.winw state.winh;
3829 src#int "scroll handle height"
3830 (fun () -> conf.scrollh)
3831 (fun v -> conf.scrollh <- v;);
3833 src#int "thumbnail width"
3834 (fun () -> conf.thumbw)
3835 (fun v ->
3836 conf.thumbw <- min 4096 v;
3837 match oldmode with
3838 | Birdseye beye ->
3839 leavebirdseye beye false;
3840 enterbirdseye ()
3841 | Textentry _
3842 | View
3843 | LinkNav _ -> ()
3846 let mode = state.mode in
3847 src#string "columns"
3848 (fun () ->
3849 match conf.columns with
3850 | Csingle _ -> "1"
3851 | Cmulti (multi, _) -> multicolumns_to_string multi
3852 | Csplit (count, _) -> "-" ^ string_of_int count
3854 (fun v ->
3855 let n, a, b = multicolumns_of_string v in
3856 setcolumns mode n a b);
3858 sep ();
3859 src#caption "Pixmap cache" 0;
3860 src#int_with_suffix "size (advisory)"
3861 (fun () -> conf.memlimit)
3862 (fun v -> conf.memlimit <- v);
3864 src#caption2 "used"
3865 (fun () -> Printf.sprintf "%s bytes, %d tiles"
3866 (string_with_suffix_of_int state.memused)
3867 (Hashtbl.length state.tilemap)) 1;
3869 sep ();
3870 src#caption "Layout" 0;
3871 src#caption2 "Dimension"
3872 (fun () ->
3873 Printf.sprintf "%dx%d (virtual %dx%d)"
3874 state.winw state.winh
3875 state.w state.maxy)
3877 if conf.debug
3878 then
3879 src#caption2 "Position" (fun () ->
3880 Printf.sprintf "%dx%d" state.x state.y
3882 else
3883 src#caption2 "Position" (fun () -> describe_location ()) 1
3886 sep ();
3887 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3888 "Save these parameters as global defaults at exit"
3889 (fun () -> conf.bedefault)
3890 (fun v -> conf.bedefault <- v)
3893 sep ();
3894 let btos b = if b then "@Ulguillemet" else "@Urguillemet" in
3895 src#bool ~offset:0 ~btos "Extended parameters"
3896 (fun () -> !showextended)
3897 (fun v -> showextended := v; fillsrc prevmode prevuioh);
3898 if !showextended
3899 then (
3900 src#bool "checkers"
3901 (fun () -> conf.checkers)
3902 (fun v -> conf.checkers <- v; setcheckers v);
3903 src#bool "update cursor"
3904 (fun () -> conf.updatecurs)
3905 (fun v -> conf.updatecurs <- v);
3906 src#bool "scroll-bar on the left"
3907 (fun () -> conf.leftscroll)
3908 (fun v -> conf.leftscroll <- v);
3909 src#bool "verbose"
3910 (fun () -> conf.verbose)
3911 (fun v -> conf.verbose <- v);
3912 src#bool "invert colors"
3913 (fun () -> conf.invert)
3914 (fun v -> conf.invert <- v);
3915 src#bool "max fit"
3916 (fun () -> conf.maxhfit)
3917 (fun v -> conf.maxhfit <- v);
3918 src#bool "redirect stderr"
3919 (fun () -> conf.redirectstderr)
3920 (fun v -> conf.redirectstderr <- v; redirectstderr ());
3921 src#bool "pax mode"
3922 (fun () -> conf.pax != None)
3923 (fun v ->
3924 if v
3925 then conf.pax <- Some (ref (now (), 0, 0))
3926 else conf.pax <- None);
3927 src#string "uri launcher"
3928 (fun () -> conf.urilauncher)
3929 (fun v -> conf.urilauncher <- v);
3930 src#string "path launcher"
3931 (fun () -> conf.pathlauncher)
3932 (fun v -> conf.pathlauncher <- v);
3933 src#string "tile size"
3934 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3935 (fun v ->
3937 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
3938 conf.tilew <- max 64 w;
3939 conf.tileh <- max 64 h;
3940 flushtiles ();
3941 with exn ->
3942 state.text <- Printf.sprintf "bad tile size `%s': %s"
3943 v (exntos exn)
3945 src#int "texture count"
3946 (fun () -> conf.texcount)
3947 (fun v ->
3948 if realloctexts v
3949 then conf.texcount <- v
3950 else showtext '!' " Failed to set texture count please retry later"
3952 src#int "slice height"
3953 (fun () -> conf.sliceheight)
3954 (fun v ->
3955 conf.sliceheight <- v;
3956 wcmd "sliceh %d" conf.sliceheight;
3958 src#int "anti-aliasing level"
3959 (fun () -> conf.aalevel)
3960 (fun v ->
3961 conf.aalevel <- bound v 0 8;
3962 state.anchor <- getanchor ();
3963 opendoc state.path state.password;
3965 src#string "page scroll scaling factor"
3966 (fun () -> string_of_float conf.pgscale)
3967 (fun v ->
3969 let s = float_of_string v in
3970 conf.pgscale <- s
3971 with exn ->
3972 state.text <- Printf.sprintf
3973 "bad page scroll scaling factor `%s': %s" v (exntos exn)
3976 src#int "ui font size"
3977 (fun () -> fstate.fontsize)
3978 (fun v -> setfontsize (bound v 5 100));
3979 src#int "hint font size"
3980 (fun () -> conf.hfsize)
3981 (fun v -> conf.hfsize <- bound v 5 100);
3982 colorp "background color"
3983 (fun () -> conf.bgcolor)
3984 (fun v -> conf.bgcolor <- v);
3985 src#bool "crop hack"
3986 (fun () -> conf.crophack)
3987 (fun v -> conf.crophack <- v);
3988 src#string "trim fuzz"
3989 (fun () -> irect_to_string conf.trimfuzz)
3990 (fun v ->
3992 conf.trimfuzz <- irect_of_string v;
3993 if conf.trimmargins
3994 then settrim true conf.trimfuzz;
3995 with exn ->
3996 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
3998 src#string "throttle"
3999 (fun () ->
4000 match conf.maxwait with
4001 | None -> "show place holder if page is not ready"
4002 | Some time ->
4003 if time = infinity
4004 then "wait for page to fully render"
4005 else
4006 "wait " ^ string_of_float time
4007 ^ " seconds before showing placeholder"
4009 (fun v ->
4011 let f = float_of_string v in
4012 if f <= 0.0
4013 then conf.maxwait <- None
4014 else conf.maxwait <- Some f
4015 with exn ->
4016 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
4018 src#string "ghyll scroll"
4019 (fun () ->
4020 match conf.ghyllscroll with
4021 | None -> E.s
4022 | Some nab -> ghyllscroll_to_string nab
4024 (fun v ->
4025 try conf.ghyllscroll <- ghyllscroll_of_string v
4026 with exn ->
4027 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
4029 src#string "selection command"
4030 (fun () -> conf.selcmd)
4031 (fun v -> conf.selcmd <- v);
4032 src#string "synctex command"
4033 (fun () -> conf.stcmd)
4034 (fun v -> conf.stcmd <- v);
4035 src#string "pax command"
4036 (fun () -> conf.paxcmd)
4037 (fun v -> conf.paxcmd <- v);
4038 src#string "ask password command"
4039 (fun () -> conf.passcmd)
4040 (fun v -> conf.passcmd <- v);
4041 src#string "save path command"
4042 (fun () -> conf.savecmd)
4043 (fun v -> conf.savecmd <- v);
4044 src#colorspace "color space"
4045 (fun () -> CSTE.to_string conf.colorspace)
4046 (fun v ->
4047 conf.colorspace <- CSTE.of_int v;
4048 wcmd "cs %d" v;
4049 load state.layout;
4051 src#paxmark "pax mark method"
4052 (fun () -> MTE.to_string conf.paxmark)
4053 (fun v -> conf.paxmark <- MTE.of_int v);
4054 if pbousable ()
4055 then
4056 src#bool "use PBO"
4057 (fun () -> conf.usepbo)
4058 (fun v -> conf.usepbo <- v);
4059 src#bool "mouse wheel scrolls pages"
4060 (fun () -> conf.wheelbypage)
4061 (fun v -> conf.wheelbypage <- v);
4062 src#bool "open remote links in a new instance"
4063 (fun () -> conf.riani)
4064 (fun v -> conf.riani <- v);
4065 src#bool "edit annotations inline"
4066 (fun () -> conf.annotinline)
4067 (fun v -> conf.annotinline <- v);
4070 sep ();
4071 src#caption "Document" 0;
4072 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4073 src#caption2 "Pages"
4074 (fun () -> string_of_int state.pagecount) 1;
4075 src#caption2 "Dimensions"
4076 (fun () -> string_of_int (List.length state.pdims)) 1;
4077 if conf.trimmargins
4078 then (
4079 sep ();
4080 src#caption "Trimmed margins" 0;
4081 src#caption2 "Dimensions"
4082 (fun () -> string_of_int (List.length state.pdims)) 1;
4085 sep ();
4086 src#caption "OpenGL" 0;
4087 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4088 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4090 sep ();
4091 src#caption "Location" 0;
4092 if nonemptystr state.origin
4093 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
4094 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
4096 src#reset prevmode prevuioh;
4098 fun () ->
4099 state.text <- E.s;
4100 resetmstate ();
4101 let prevmode = state.mode
4102 and prevuioh = state.uioh in
4103 fillsrc prevmode prevuioh;
4104 let source = (src :> lvsource) in
4105 let modehash = findkeyhash conf "info" in
4106 state.uioh <- coe (object (self)
4107 inherit listview ~zebra:false ~helpmode:false
4108 ~source ~trusted:true ~modehash as super
4109 val mutable m_prevmemused = 0
4110 method! infochanged = function
4111 | Memused ->
4112 if m_prevmemused != state.memused
4113 then (
4114 m_prevmemused <- state.memused;
4115 G.postRedisplay "memusedchanged";
4117 | Pdim -> G.postRedisplay "pdimchanged"
4118 | Docinfo -> fillsrc prevmode prevuioh
4120 method! key key mask =
4121 if not (Wsi.withctrl mask)
4122 then
4123 match key with
4124 | @left | @kpleft -> coe (self#updownlevel ~-1)
4125 | @right | @kpright -> coe (self#updownlevel 1)
4126 | _ -> super#key key mask
4127 else super#key key mask
4128 end);
4129 G.postRedisplay "info";
4132 let enterhelpmode =
4133 let source =
4134 (object
4135 inherit lvsourcebase
4136 method getitemcount = Array.length state.help
4137 method getitem n =
4138 let s, l, _ = state.help.(n) in
4139 (s, l)
4141 method exit ~uioh ~cancel ~active ~first ~pan =
4142 let optuioh =
4143 if not cancel
4144 then (
4145 match state.help.(active) with
4146 | _, _, Action f -> Some (f uioh)
4147 | _, _, Noaction -> Some uioh
4149 else None
4151 m_active <- active;
4152 m_first <- first;
4153 m_pan <- pan;
4154 optuioh
4156 method hasaction n =
4157 match state.help.(n) with
4158 | _, _, Action _ -> true
4159 | _, _, Noaction -> false
4161 initializer
4162 m_active <- -1
4163 end)
4164 in fun () ->
4165 let modehash = findkeyhash conf "help" in
4166 resetmstate ();
4167 state.uioh <- coe (new listview
4168 ~zebra:false ~helpmode:true
4169 ~source ~trusted:true ~modehash);
4170 G.postRedisplay "help";
4173 let entermsgsmode =
4174 let msgsource =
4175 (object
4176 inherit lvsourcebase
4177 val mutable m_items = E.a
4179 method getitemcount = 1 + Array.length m_items
4181 method getitem n =
4182 if n = 0
4183 then "[Clear]", 0
4184 else m_items.(n-1), 0
4186 method exit ~uioh ~cancel ~active ~first ~pan =
4187 ignore uioh;
4188 if not cancel
4189 then (
4190 if active = 0
4191 then Buffer.clear state.errmsgs;
4193 m_active <- active;
4194 m_first <- first;
4195 m_pan <- pan;
4196 None
4198 method hasaction n =
4199 n = 0
4201 method reset =
4202 state.newerrmsgs <- false;
4203 let l = Str.split newlinere (Buffer.contents state.errmsgs) in
4204 m_items <- Array.of_list l
4206 initializer
4207 m_active <- 0
4208 end)
4209 in fun () ->
4210 state.text <- E.s;
4211 resetmstate ();
4212 msgsource#reset;
4213 let source = (msgsource :> lvsource) in
4214 let modehash = findkeyhash conf "listview" in
4215 state.uioh <- coe (object
4216 inherit listview ~zebra:false ~helpmode:false
4217 ~source ~trusted:false ~modehash as super
4218 method! display =
4219 if state.newerrmsgs
4220 then msgsource#reset;
4221 super#display
4222 end);
4223 G.postRedisplay "msgs";
4226 let getusertext s =
4227 let editor = getenvwithdef "EDITOR" E.s in
4228 if emptystr editor
4229 then E.s
4230 else
4231 let tmppath = Filename.temp_file "llpp" "note" in
4232 if nonemptystr s
4233 then (
4234 let oc = open_out tmppath in
4235 output_string oc s;
4236 close_out oc;
4238 let execstr = editor ^ " " ^ tmppath in
4239 let s =
4240 match popen execstr [] with
4241 | (exception exn) ->
4242 showtext '!' @@
4243 Printf.sprintf "popen(%S) failed: %s" execstr (exntos exn);
4245 | pid ->
4246 match Unix.waitpid [] pid
4247 with
4248 | (exception exn) ->
4249 showtext '!' @@
4250 Printf.sprintf "waitpid(%d) failed: %s" pid (exntos exn);
4252 | (_pid, status) ->
4253 match status with
4254 | Unix.WEXITED 0 -> filelines tmppath
4255 | Unix.WEXITED n ->
4256 showtext '!' @@
4257 Printf.sprintf "editor process(%s) exited abnormally: %d"
4258 execstr n;
4260 | Unix.WSIGNALED n ->
4261 showtext '!' @@
4262 Printf.sprintf "editor process(%s) was killed by signal %d"
4263 execstr n;
4265 | Unix.WSTOPPED n ->
4266 showtext '!' @@
4267 Printf.sprintf "editor(%s) process was stopped by signal %d"
4268 execstr n;
4271 match Unix.unlink tmppath with
4272 | (exception exn) ->
4273 showtext '!' @@ Printf.sprintf "failed to ulink %S: %s"
4274 tmppath (exntos exn);
4276 | () -> s
4279 let enterannotmode opaque slinkindex =
4280 let msgsource =
4281 (object
4282 inherit lvsourcebase
4283 val mutable m_text = E.s
4284 val mutable m_items = E.a
4286 method getitemcount = Array.length m_items
4288 method getitem n =
4289 let label, _func = m_items.(n) in
4290 label, 0
4292 method exit ~uioh ~cancel ~active ~first ~pan =
4293 ignore (uioh, first, pan);
4294 if not cancel
4295 then (
4296 let _label, func = m_items.(active) in
4297 func ()
4299 None
4301 method hasaction n = nonemptystr @@ fst m_items.(n)
4303 method reset s =
4304 let rec split accu b i =
4305 let p = b+i in
4306 if p = String.length s
4307 then (String.sub s b (p-b), unit) :: accu
4308 else
4309 if (i > 70 && s.[p] = ' ') || s.[p] = '\r' || s.[p] = '\n'
4310 then
4311 let ss = if i = 0 then E.s else String.sub s b i in
4312 split ((ss, unit)::accu) (p+1) 0
4313 else
4314 split accu b (i+1)
4316 let cleanup () =
4317 wcmd "freepage %s" (~> opaque);
4318 let keys =
4319 Hashtbl.fold (fun key opaque' accu ->
4320 if opaque' = opaque'
4321 then key :: accu else accu) state.pagemap []
4323 List.iter (Hashtbl.remove state.pagemap) keys;
4324 flushtiles ();
4325 gotoy state.y
4327 let dele () =
4328 delannot opaque slinkindex;
4329 cleanup ();
4331 let edit inline () =
4332 let update s =
4333 if emptystr s
4334 then dele ()
4335 else (
4336 modannot opaque slinkindex s;
4337 cleanup ();
4340 if inline
4341 then
4342 let mode = state.mode in
4343 state.mode <-
4344 Textentry (
4345 ("annotation: ", m_text, None, textentry, update, true),
4346 fun _ -> state.mode <- mode);
4347 state.text <- E.s;
4348 enttext ();
4349 else
4350 let s = getusertext m_text in
4351 update s
4353 m_text <- s;
4354 m_items <-
4355 ( "[Copy]", fun () -> selstring m_text)
4356 :: ("[Delete]", dele)
4357 :: ("[Edit]", edit conf.annotinline)
4358 :: (E.s, unit)
4359 :: split [] 0 0 |> List.rev |> Array.of_list
4361 initializer
4362 m_active <- 0
4363 end)
4365 state.text <- E.s;
4366 let s = getannotcontents opaque slinkindex in
4367 resetmstate ();
4368 msgsource#reset s;
4369 let source = (msgsource :> lvsource) in
4370 let modehash = findkeyhash conf "listview" in
4371 state.uioh <- coe (object
4372 inherit listview ~zebra:false ~helpmode:false
4373 ~source ~trusted:false ~modehash
4374 end);
4375 G.postRedisplay "enterannotmode";
4378 let gotounder under =
4379 let getpath filename =
4380 let path =
4381 if nonemptystr filename
4382 then
4383 if Filename.is_relative filename
4384 then
4385 let dir = Filename.dirname state.path in
4386 let dir =
4387 if Filename.is_implicit dir
4388 then Filename.concat (Sys.getcwd ()) dir
4389 else dir
4391 Filename.concat dir filename
4392 else filename
4393 else E.s
4395 if Sys.file_exists path
4396 then path
4397 else E.s
4399 match under with
4400 | Ulinkgoto (pageno, top) ->
4401 if pageno >= 0
4402 then (
4403 addnav ();
4404 gotopage1 pageno top;
4407 | Ulinkuri s ->
4408 addpid @@ gotouri s
4410 | Uremote (filename, pageno) ->
4411 let path = getpath filename in
4412 if nonemptystr path
4413 then (
4414 if conf.riani
4415 then
4416 let command = Printf.sprintf "%s -page %d %S" !selfexec pageno path in
4417 try addpid @@ popen command []
4418 with exn ->
4419 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
4420 flush stderr;
4421 else
4422 let anchor = getanchor () in
4423 let ranchor = state.path, state.password, anchor, state.origin in
4424 state.origin <- E.s;
4425 state.anchor <- (pageno, 0.0, 0.0);
4426 state.ranchors <- ranchor :: state.ranchors;
4427 opendoc path E.s;
4429 else showtext '!' ("Could not find " ^ filename)
4431 | Uremotedest (filename, destname) ->
4432 let path = getpath filename in
4433 if nonemptystr path
4434 then (
4435 if conf.riani
4436 then
4437 let command = !selfexec ^ " " ^ path ^ " -dest " ^ destname in
4438 try addpid @@ popen command []
4439 with exn ->
4440 Printf.eprintf
4441 "failed to execute `%s': %s\n" command (exntos exn);
4442 flush stderr;
4443 else
4444 let anchor = getanchor () in
4445 let ranchor = state.path, state.password, anchor, state.origin in
4446 state.origin <- E.s;
4447 state.nameddest <- destname;
4448 state.ranchors <- ranchor :: state.ranchors;
4449 opendoc path E.s;
4451 else showtext '!' ("Could not find " ^ filename)
4453 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4454 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
4457 let gotooutline (_, _, kind) =
4458 match kind with
4459 | Onone -> None
4460 | Oanchor anchor ->
4461 let (pageno, y, _) = anchor in
4462 let y = getanchory
4463 (if conf.presentation then (pageno, y, 1.0) else anchor)
4465 addnav ();
4466 gotoghyll y;
4467 None
4468 | Ouri uri -> gotounder (Ulinkuri uri); None
4469 | Olaunch cmd -> gotounder (Ulaunch cmd); None
4470 | Oremote remote -> gotounder (Uremote remote); None
4471 | Ohistory hist -> gotohist hist; None
4472 | Oremotedest remotedest -> gotounder (Uremotedest remotedest); None
4473 | Oaction f -> f (); None
4474 | Oreaction f -> Some (f ())
4477 let outlinesource sourcetype =
4478 (object (self)
4479 inherit lvsourcebase
4480 val mutable m_items = E.a
4481 val mutable m_minfo = E.a
4482 val mutable m_orig_items = E.a
4483 val mutable m_orig_minfo = E.a
4484 val mutable m_narrow_patterns = []
4485 val mutable m_hadremovals = false
4486 val mutable m_gen = -1
4488 method getitemcount =
4489 Array.length m_items + (if m_hadremovals then 1 else 0)
4491 method getitem n =
4492 if n == Array.length m_items && m_hadremovals
4493 then
4494 ("[Confirm removal]", 0)
4495 else
4496 let s, n, _ = m_items.(n) in
4497 (s, n)
4499 method exit ~uioh ~cancel ~active ~first ~pan =
4500 ignore (uioh, first);
4501 let confrimremoval = m_hadremovals && active = Array.length m_items in
4502 let items, minfo =
4503 if m_narrow_patterns = []
4504 then m_orig_items, m_orig_minfo
4505 else m_items, m_minfo
4507 m_pan <- pan;
4508 if not cancel
4509 then (
4510 if not confrimremoval
4511 then (
4512 m_items <- items;
4513 m_minfo <- minfo;
4514 match gotooutline m_items.(active) with
4515 | None -> None
4516 | Some outlines ->
4517 self#reset emptyanchor outlines;
4518 Some uioh
4520 else (
4521 state.bookmarks <- Array.to_list m_items;
4522 m_orig_items <- m_items;
4523 m_orig_minfo <- m_minfo;
4524 None
4527 else (
4528 m_items <- items;
4529 m_minfo <- minfo;
4530 None
4533 method hasaction _ = true
4535 method greetmsg =
4536 if Array.length m_items != Array.length m_orig_items
4537 then
4538 let s =
4539 match m_narrow_patterns with
4540 | one :: [] -> one
4541 | many -> String.concat "@Uellipsis" (List.rev many)
4543 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
4544 else E.s
4546 method statestr =
4547 match m_narrow_patterns with
4548 | [] -> E.s
4549 | one :: [] -> one
4550 | head :: _ -> "@Uellipsis" ^ head
4552 method narrow pattern =
4553 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
4554 match reopt with
4555 | None -> ()
4556 | Some re ->
4557 let rec loop accu minfo n =
4558 if n = -1
4559 then (
4560 m_items <- Array.of_list accu;
4561 m_minfo <- Array.of_list minfo;
4563 else
4564 let (s, _, t) as o = m_items.(n) in
4565 let accu, minfo =
4566 match t with
4567 | Oaction _ | Oreaction _ -> o :: accu, (0, 0) :: minfo
4568 | Onone | Oanchor _ | Ouri _ | Olaunch _
4569 | Oremote _ | Oremotedest _ | Ohistory _ ->
4570 let first =
4571 try Str.search_forward re s 0
4572 with Not_found -> -1
4574 if first >= 0
4575 then o :: accu, (first, Str.match_end ()) :: minfo
4576 else accu, minfo
4578 loop accu minfo (n-1)
4580 loop [] [] (Array.length m_items - 1)
4582 method! getminfo = m_minfo
4584 method denarrow =
4585 m_orig_items <- (
4586 match sourcetype with
4587 | `bookmarks -> Array.of_list state.bookmarks
4588 | `outlines -> state.outlines
4589 | `history -> genhistoutlines ()
4591 m_minfo <- m_orig_minfo;
4592 m_items <- m_orig_items
4594 method remove m =
4595 if sourcetype = `bookmarks
4596 then
4597 if m >= 0 && m < Array.length m_items
4598 then (
4599 m_hadremovals <- true;
4600 m_items <- Array.init (Array.length m_items - 1) (fun n ->
4601 let n = if n >= m then n+1 else n in
4602 m_items.(n)
4606 method add_narrow_pattern pattern =
4607 m_narrow_patterns <- pattern :: m_narrow_patterns
4609 method del_narrow_pattern =
4610 match m_narrow_patterns with
4611 | _ :: rest -> m_narrow_patterns <- rest
4612 | [] -> ()
4614 method renarrow =
4615 self#denarrow;
4616 match m_narrow_patterns with
4617 | pattern :: [] -> self#narrow pattern; pattern
4618 | list ->
4619 List.fold_left (fun accu pattern ->
4620 self#narrow pattern;
4621 pattern ^ "@Uellipsis" ^ accu) E.s list
4623 method calcactive anchor =
4624 let rely = getanchory anchor in
4625 let rec loop n best bestd =
4626 if n = Array.length m_items
4627 then best
4628 else
4629 let _, _, kind = m_items.(n) in
4630 match kind with
4631 | Oanchor anchor ->
4632 let orely = getanchory anchor in
4633 let d = abs (orely - rely) in
4634 if d < bestd
4635 then loop (n+1) n d
4636 else loop (n+1) best bestd
4637 | Onone | Oremote _ | Olaunch _
4638 | Oremotedest _ | Ouri _ | Ohistory _ | Oaction _ | Oreaction _ ->
4639 loop (n+1) best bestd
4641 loop 0 ~-1 max_int
4643 method reset anchor items =
4644 m_hadremovals <- false;
4645 if state.gen != m_gen
4646 then (
4647 m_orig_items <- items;
4648 m_items <- items;
4649 m_narrow_patterns <- [];
4650 m_minfo <- E.a;
4651 m_orig_minfo <- E.a;
4652 m_gen <- state.gen;
4654 else (
4655 if items != m_orig_items
4656 then (
4657 m_orig_items <- items;
4658 if m_narrow_patterns == []
4659 then m_items <- items;
4662 let active = self#calcactive anchor in
4663 m_active <- active;
4664 m_first <- firstof m_first active
4665 end)
4668 let enteroutlinemode, enterbookmarkmode, enterhistmode =
4669 let mkselector sourcetype =
4670 let source = outlinesource sourcetype in
4671 fun errmsg ->
4672 let outlines =
4673 match sourcetype with
4674 | `bookmarks -> Array.of_list state.bookmarks
4675 | `outlines -> state.outlines
4676 | `history -> genhistoutlines ()
4678 if Array.length outlines = 0
4679 then (
4680 showtext ' ' errmsg;
4682 else (
4683 resetmstate ();
4684 state.text <- source#greetmsg;
4685 Wsi.setcursor Wsi.CURSOR_INHERIT;
4686 let anchor = getanchor () in
4687 source#reset anchor outlines;
4688 state.uioh <-
4689 coe (new outlinelistview ~zebra:(sourcetype=`history) ~source);
4690 G.postRedisplay "enter selector";
4693 let mkenter sourcetype errmsg =
4694 let enter = mkselector sourcetype in
4695 fun () -> enter errmsg
4697 (**)mkenter `outlines "Document has no outline"
4698 , mkenter `bookmarks "Document has no bookmarks (yet)"
4699 , mkenter `history "History is empty"
4702 let quickbookmark ?title () =
4703 match state.layout with
4704 | [] -> ()
4705 | l :: _ ->
4706 let title =
4707 match title with
4708 | None ->
4709 let tm = Unix.localtime (now ()) in
4710 Printf.sprintf
4711 "Quick (page %d) (bookmarked at %02d/%02d/%d %02d:%02d)"
4712 (l.pageno+1)
4713 tm.Unix.tm_mday
4714 (tm.Unix.tm_mon+1)
4715 (tm.Unix.tm_year + 1900)
4716 tm.Unix.tm_hour
4717 tm.Unix.tm_min
4718 | Some title -> title
4720 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4723 let setautoscrollspeed step goingdown =
4724 let incr = max 1 ((abs step) / 2) in
4725 let incr = if goingdown then incr else -incr in
4726 let astep = boundastep state.winh (step + incr) in
4727 state.autoscroll <- Some astep;
4730 let canpan () =
4731 match conf.columns with
4732 | Csplit _ -> true
4733 | Csingle _ | Cmulti _ -> state.x != 0 || conf.zoom > 1.0
4736 let panbound x = bound x (-state.w) (wadjsb () + state.winw);;
4738 let existsinrow pageno (columns, coverA, coverB) p =
4739 let last = ((pageno - coverA) mod columns) + columns in
4740 let rec any = function
4741 | [] -> false
4742 | l :: rest ->
4743 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4744 then p l
4745 else (
4746 if not (p l)
4747 then (if l.pageno = last then false else any rest)
4748 else true
4751 any state.layout
4754 let nextpage () =
4755 match state.layout with
4756 | [] ->
4757 let pageno = page_of_y state.y in
4758 gotoghyll (getpagey (pageno+1))
4759 | l :: rest ->
4760 match conf.columns with
4761 | Csingle _ ->
4762 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4763 then
4764 let y = clamp (pgscale state.winh) in
4765 gotoghyll y
4766 else
4767 let pageno = min (l.pageno+1) (state.pagecount-1) in
4768 gotoghyll (getpagey pageno)
4769 | Cmulti ((c, _, _) as cl, _) ->
4770 if conf.presentation
4771 && (existsinrow l.pageno cl
4772 (fun l -> l.pageh > l.pagey + l.pagevh))
4773 then
4774 let y = clamp (pgscale state.winh) in
4775 gotoghyll y
4776 else
4777 let pageno = min (l.pageno+c) (state.pagecount-1) in
4778 gotoghyll (getpagey pageno)
4779 | Csplit (n, _) ->
4780 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4781 then
4782 let pagey, pageh = getpageyh l.pageno in
4783 let pagey = pagey + pageh * l.pagecol in
4784 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4785 gotoghyll (pagey + pageh + ips)
4788 let prevpage () =
4789 match state.layout with
4790 | [] ->
4791 let pageno = page_of_y state.y in
4792 gotoghyll (getpagey (pageno-1))
4793 | l :: _ ->
4794 match conf.columns with
4795 | Csingle _ ->
4796 if conf.presentation && l.pagey != 0
4797 then
4798 gotoghyll (clamp (pgscale ~-(state.winh)))
4799 else
4800 let pageno = max 0 (l.pageno-1) in
4801 gotoghyll (getpagey pageno)
4802 | Cmulti ((c, _, coverB) as cl, _) ->
4803 if conf.presentation &&
4804 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4805 then
4806 gotoghyll (clamp (pgscale ~-(state.winh)))
4807 else
4808 let decr =
4809 if l.pageno = state.pagecount - coverB
4810 then 1
4811 else c
4813 let pageno = max 0 (l.pageno-decr) in
4814 gotoghyll (getpagey pageno)
4815 | Csplit (n, _) ->
4816 let y =
4817 if l.pagecol = 0
4818 then
4819 if l.pageno = 0
4820 then l.pagey
4821 else
4822 let pageno = max 0 (l.pageno-1) in
4823 let pagey, pageh = getpageyh pageno in
4824 pagey + (n-1)*pageh
4825 else
4826 let pagey, pageh = getpageyh l.pageno in
4827 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4829 gotoghyll y
4832 let save () =
4833 if emptystr conf.savecmd
4834 then error "don't know where to save modified document"
4835 else
4836 let savecmd = Str.global_replace percentsre state.path conf.savecmd in
4837 let path =
4838 getcmdoutput
4839 (fun s -> error "failed to obtain path to the saved copy: %s" s)
4840 savecmd
4842 if nonemptystr path
4843 then
4844 let tmp = path ^ ".tmp" in
4845 savedoc tmp;
4846 Unix.rename tmp path;
4849 let viewkeyboard key mask =
4850 let enttext te =
4851 let mode = state.mode in
4852 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4853 state.text <- E.s;
4854 enttext ();
4855 G.postRedisplay "view:enttext"
4857 let ctrl = Wsi.withctrl mask in
4858 let key =
4859 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
4861 match key with
4862 | @Q -> exit 0
4864 | @W ->
4865 if hasunsavedchanges ()
4866 then save ()
4868 | @insert ->
4869 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4870 then (
4871 state.mode <- LinkNav (Ltgendir 0);
4872 gotoy state.y;
4874 else showtext '!' "Keyboard link navigation does not work under rotation"
4876 | @escape | @q ->
4877 begin match state.mstate with
4878 | Mzoomrect _ ->
4879 resetmstate ();
4880 G.postRedisplay "kill rect";
4881 | Msel _
4882 | Mpan _
4883 | Mscrolly | Mscrollx
4884 | Mzoom _
4885 | Mnone ->
4886 begin match state.mode with
4887 | LinkNav _ ->
4888 state.mode <- View;
4889 G.postRedisplay "esc leave linknav"
4890 | Birdseye _
4891 | Textentry _
4892 | View ->
4893 match state.ranchors with
4894 | [] -> raise Quit
4895 | (path, password, anchor, origin) :: rest ->
4896 state.ranchors <- rest;
4897 state.anchor <- anchor;
4898 state.origin <- origin;
4899 state.nameddest <- E.s;
4900 opendoc path password
4901 end;
4902 end;
4904 | @backspace ->
4905 gotoghyll (getnav ~-1)
4907 | @o ->
4908 enteroutlinemode ()
4910 | @H ->
4911 enterhistmode ()
4913 | @u ->
4914 state.rects <- [];
4915 state.text <- E.s;
4916 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap;
4917 G.postRedisplay "dehighlight";
4919 | @slash | @question ->
4920 let ondone isforw s =
4921 cbput state.hists.pat s;
4922 state.searchpattern <- s;
4923 search s isforw
4925 let s = String.make 1 (Char.chr key) in
4926 enttext (s, E.s, Some (onhist state.hists.pat),
4927 textentry, ondone (key = @slash), true)
4929 | @plus | @kpplus | @equals when ctrl ->
4930 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4931 setzoom (conf.zoom +. incr)
4933 | @plus | @kpplus ->
4934 let ondone s =
4935 let n =
4936 try int_of_string s with exc ->
4937 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
4938 max_int
4940 if n != max_int
4941 then (
4942 conf.pagebias <- n;
4943 state.text <- "page bias is now " ^ string_of_int n;
4946 enttext ("page bias: ", E.s, None, intentry, ondone, true)
4948 | @minus | @kpminus when ctrl ->
4949 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4950 setzoom (max 0.01 (conf.zoom -. decr))
4952 | @minus | @kpminus ->
4953 let ondone msg = state.text <- msg in
4954 enttext (
4955 "option [acfhilpstvxACFPRSZTISM]: ", E.s, None,
4956 optentry state.mode, ondone, true
4959 | @0 when ctrl ->
4960 if conf.zoom = 1.0
4961 then (
4962 state.x <- 0;
4963 gotoy state.y
4965 else setzoom 1.0
4967 | (@1 | @2) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
4968 let cols =
4969 match conf.columns with
4970 | Csingle _ | Cmulti _ -> 1
4971 | Csplit (n, _) -> n
4973 let h = state.winh -
4974 conf.interpagespace lsl (if conf.presentation then 1 else 0)
4976 let zoom = zoomforh state.winw h (vscrollw ()) cols in
4977 if zoom > 0.0 && (key = 50 || zoom < 1.0)
4978 then setzoom zoom
4980 | @3 when ctrl ->
4981 let fm =
4982 match conf.fitmodel with
4983 | FitWidth -> FitProportional
4984 | FitProportional -> FitPage
4985 | FitPage -> FitWidth
4987 state.text <- "fit model: " ^ FMTE.to_string fm;
4988 reqlayout conf.angle fm
4990 | @F9 ->
4991 togglebirdseye ()
4993 | @9 when ctrl ->
4994 togglebirdseye ()
4996 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4997 when not ctrl -> (* 0..9 *)
4998 let ondone s =
4999 let n =
5000 try int_of_string s with exc ->
5001 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5004 if n >= 0
5005 then (
5006 addnav ();
5007 cbput state.hists.pag (string_of_int n);
5008 gotopage1 (n + conf.pagebias - 1) 0;
5011 let pageentry text key =
5012 match Char.unsafe_chr key with
5013 | 'g' -> TEdone text
5014 | _ -> intentry text key
5016 let text = String.make 1 (Char.chr key) in
5017 enttext (":", text, Some (onhist state.hists.pag),
5018 pageentry, ondone, true)
5020 | @b ->
5021 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
5022 reshape state.winw state.winh;
5024 | @B ->
5025 state.bzoom <- not state.bzoom;
5026 state.rects <- [];
5027 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
5029 | @l ->
5030 conf.hlinks <- not conf.hlinks;
5031 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5032 G.postRedisplay "toggle highlightlinks";
5034 | @F ->
5035 state.glinks <- true;
5036 let mode = state.mode in
5037 state.mode <- Textentry (
5038 (":", E.s, None, linknentry, linknact gotounder, false),
5039 (fun _ ->
5040 state.glinks <- false;
5041 state.mode <- mode)
5043 state.text <- E.s;
5044 G.postRedisplay "view:linkent(F)"
5046 | @y ->
5047 state.glinks <- true;
5048 let mode = state.mode in
5049 state.mode <- Textentry (
5051 ":", E.s, None, linknentry, linknact (fun under ->
5052 selstring (undertext under);
5053 ), false
5055 fun _ ->
5056 state.glinks <- false;
5057 state.mode <- mode
5059 state.text <- E.s;
5060 G.postRedisplay "view:linkent"
5062 | @a ->
5063 begin match state.autoscroll with
5064 | Some step ->
5065 conf.autoscrollstep <- step;
5066 state.autoscroll <- None
5067 | None ->
5068 if conf.autoscrollstep = 0
5069 then state.autoscroll <- Some 1
5070 else state.autoscroll <- Some conf.autoscrollstep
5073 | @p when ctrl ->
5074 launchpath ()
5076 | @P ->
5077 setpresentationmode (not conf.presentation);
5078 showtext ' ' ("presentation mode " ^
5079 if conf.presentation then "on" else "off");
5081 | @f ->
5082 if List.mem Wsi.Fullscreen state.winstate
5083 then Wsi.reshape conf.cwinw conf.cwinh
5084 else Wsi.fullscreen ()
5086 | @p | @N ->
5087 search state.searchpattern false
5089 | @n | @F3 ->
5090 search state.searchpattern true
5092 | @t ->
5093 begin match state.layout with
5094 | [] -> ()
5095 | l :: _ ->
5096 gotoghyll (getpagey l.pageno)
5099 | @space ->
5100 nextpage ()
5102 | @delete | @kpdelete -> (* delete *)
5103 prevpage ()
5105 | @equals ->
5106 showtext ' ' (describe_location ());
5108 | @w ->
5109 begin match state.layout with
5110 | [] -> ()
5111 | l :: _ ->
5112 Wsi.reshape (l.pagew + vscrollw ()) l.pageh;
5113 G.postRedisplay "w"
5116 | @apos ->
5117 enterbookmarkmode ()
5119 | @h | @F1 ->
5120 enterhelpmode ()
5122 | @i ->
5123 enterinfomode ()
5125 | @e when Buffer.length state.errmsgs > 0 ->
5126 entermsgsmode ()
5128 | @m ->
5129 let ondone s =
5130 match state.layout with
5131 | l :: _ ->
5132 if nonemptystr s
5133 then
5134 state.bookmarks <-
5135 (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
5136 | _ -> ()
5138 enttext ("bookmark: ", E.s, None, textentry, ondone, true)
5140 | @tilde ->
5141 quickbookmark ();
5142 showtext ' ' "Quick bookmark added";
5144 | @z ->
5145 begin match state.layout with
5146 | l :: _ ->
5147 let rect = getpdimrect l.pagedimno in
5148 let w, h =
5149 if conf.crophack
5150 then
5151 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5152 truncate (1.2 *. (rect.(3) -. rect.(0))))
5153 else
5154 (truncate (rect.(1) -. rect.(0)),
5155 truncate (rect.(3) -. rect.(0)))
5157 let w = truncate ((float w)*.conf.zoom)
5158 and h = truncate ((float h)*.conf.zoom) in
5159 if w != 0 && h != 0
5160 then (
5161 state.anchor <- getanchor ();
5162 Wsi.reshape (w + vscrollw ()) (h + conf.interpagespace)
5164 G.postRedisplay "z";
5166 | [] -> ()
5169 | @x -> state.roam ()
5171 | @Lt | @Gt ->
5172 reqlayout (conf.angle +
5173 (if key = @Gt then 30 else -30)) conf.fitmodel
5175 | @Lb | @Rb ->
5176 conf.colorscale <-
5177 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5179 G.postRedisplay "brightness";
5181 | @c when state.mode = View ->
5182 if Wsi.withalt mask
5183 then (
5184 if conf.zoom > 1.0
5185 then
5186 let m = (wadjsb () + state.winw - state.w) / 2 in
5187 state.x <- m;
5188 gotoy_and_clear_text state.y
5190 else
5191 let (c, a, b), z =
5192 match state.prevcolumns with
5193 | None -> (1, 0, 0), 1.0
5194 | Some (columns, z) ->
5195 let cab =
5196 match columns with
5197 | Csplit (c, _) -> -c, 0, 0
5198 | Cmulti ((c, a, b), _) -> c, a, b
5199 | Csingle _ -> 1, 0, 0
5201 cab, z
5203 setcolumns View c a b;
5204 setzoom z
5206 | @down | @up when ctrl && Wsi.withshift mask ->
5207 let zoom, x = state.prevzoom in
5208 setzoom zoom;
5209 state.x <- x;
5211 | @k | @up | @kpup ->
5212 begin match state.autoscroll with
5213 | None ->
5214 begin match state.mode with
5215 | Birdseye beye -> upbirdseye 1 beye
5216 | Textentry _
5217 | View
5218 | LinkNav _ ->
5219 if ctrl
5220 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5221 else (
5222 if not (Wsi.withshift mask) && conf.presentation
5223 then prevpage ()
5224 else gotoghyll1 true (clamp (-conf.scrollstep))
5227 | Some n ->
5228 setautoscrollspeed n false
5231 | @j | @down | @kpdown ->
5232 begin match state.autoscroll with
5233 | None ->
5234 begin match state.mode with
5235 | Birdseye beye -> downbirdseye 1 beye
5236 | Textentry _
5237 | View
5238 | LinkNav _ ->
5239 if ctrl
5240 then gotoy_and_clear_text (clamp (state.winh/2))
5241 else (
5242 if not (Wsi.withshift mask) && conf.presentation
5243 then nextpage ()
5244 else gotoghyll1 true (clamp (conf.scrollstep))
5247 | Some n ->
5248 setautoscrollspeed n true
5251 | @left | @right | @kpleft | @kpright when not (Wsi.withalt mask) ->
5252 if canpan ()
5253 then
5254 let dx =
5255 if ctrl
5256 then state.winw / 2
5257 else conf.hscrollstep
5259 let dx = if key = @left || key = @kpleft then dx else -dx in
5260 state.x <- panbound (state.x + dx);
5261 gotoy_and_clear_text state.y
5262 else (
5263 state.text <- E.s;
5264 G.postRedisplay "left/right"
5267 | @prior | @kpprior ->
5268 let y =
5269 if ctrl
5270 then
5271 match state.layout with
5272 | [] -> state.y
5273 | l :: _ -> state.y - l.pagey
5274 else
5275 clamp (pgscale (-state.winh))
5277 gotoghyll y
5279 | @next | @kpnext ->
5280 let y =
5281 if ctrl
5282 then
5283 match List.rev state.layout with
5284 | [] -> state.y
5285 | l :: _ -> getpagey l.pageno
5286 else
5287 clamp (pgscale state.winh)
5289 gotoghyll y
5291 | @g | @home | @kphome ->
5292 addnav ();
5293 gotoghyll 0
5294 | @G | @jend | @kpend ->
5295 addnav ();
5296 gotoghyll (clamp state.maxy)
5298 | @right | @kpright when Wsi.withalt mask ->
5299 gotoghyll (getnav 1)
5300 | @left | @kpleft when Wsi.withalt mask ->
5301 gotoghyll (getnav ~-1)
5303 | @r ->
5304 reload ()
5306 | @v when conf.debug ->
5307 state.rects <- [];
5308 List.iter (fun l ->
5309 match getopaque l.pageno with
5310 | None -> ()
5311 | Some opaque ->
5312 let x0, y0, x1, y1 = pagebbox opaque in
5313 let a,b = float x0, float y0 in
5314 let c,d = float x1, float y0 in
5315 let e,f = float x1, float y1 in
5316 let h,j = float x0, float y1 in
5317 let rect = (a,b,c,d,e,f,h,j) in
5318 debugrect rect;
5319 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5320 ) state.layout;
5321 G.postRedisplay "v";
5323 | @pipe ->
5324 let mode = state.mode in
5325 let cmd = ref E.s in
5326 let onleave = function
5327 | Cancel -> state.mode <- mode
5328 | Confirm ->
5329 List.iter (fun l ->
5330 match getopaque l.pageno with
5331 | Some opaque -> pipesel opaque !cmd
5332 | None -> ()) state.layout;
5333 state.mode <- mode
5335 let ondone s =
5336 cbput state.hists.sel s;
5337 cmd := s
5339 let te =
5340 "| ", !cmd, Some (onhist state.hists.sel), textentry, ondone, true
5342 G.postRedisplay "|";
5343 state.mode <- Textentry (te, onleave);
5345 | _ ->
5346 vlog "huh? %s" (Wsi.keyname key)
5349 let linknavkeyboard key mask linknav =
5350 let getpage pageno =
5351 let rec loop = function
5352 | [] -> None
5353 | l :: _ when l.pageno = pageno -> Some l
5354 | _ :: rest -> loop rest
5355 in loop state.layout
5357 let doexact (pageno, n) =
5358 match getopaque pageno, getpage pageno with
5359 | Some opaque, Some l ->
5360 if key = @enter || key = @kpenter
5361 then
5362 let under = getlink opaque n in
5363 G.postRedisplay "link gotounder";
5364 gotounder under;
5365 state.mode <- View;
5366 else
5367 let opt, dir =
5368 match key with
5369 | @home ->
5370 Some (findlink opaque LDfirst), -1
5372 | @jend ->
5373 Some (findlink opaque LDlast), 1
5375 | @left ->
5376 Some (findlink opaque (LDleft n)), -1
5378 | @right ->
5379 Some (findlink opaque (LDright n)), 1
5381 | @up ->
5382 Some (findlink opaque (LDup n)), -1
5384 | @down ->
5385 Some (findlink opaque (LDdown n)), 1
5387 | _ -> None, 0
5389 let pwl l dir =
5390 begin match findpwl l.pageno dir with
5391 | Pwlnotfound -> ()
5392 | Pwl pageno ->
5393 let notfound dir =
5394 state.mode <- LinkNav (Ltgendir dir);
5395 let y, h = getpageyh pageno in
5396 let y =
5397 if dir < 0
5398 then y + h - state.winh
5399 else y
5401 gotoy y
5403 begin match getopaque pageno, getpage pageno with
5404 | Some opaque, Some _ ->
5405 let link =
5406 let ld = if dir > 0 then LDfirst else LDlast in
5407 findlink opaque ld
5409 begin match link with
5410 | Lfound m ->
5411 showlinktype (getlink opaque m);
5412 state.mode <- LinkNav (Ltexact (pageno, m));
5413 G.postRedisplay "linknav jpage";
5414 | Lnotfound -> notfound dir
5415 end;
5416 | _ -> notfound dir
5417 end;
5418 end;
5420 begin match opt with
5421 | Some Lnotfound -> pwl l dir;
5422 | Some (Lfound m) ->
5423 if m = n
5424 then pwl l dir
5425 else (
5426 let _, y0, _, y1 = getlinkrect opaque m in
5427 if y0 < l.pagey
5428 then gotopage1 l.pageno y0
5429 else (
5430 let d = fstate.fontsize + 1 in
5431 if y1 - l.pagey > l.pagevh - d
5432 then gotopage1 l.pageno (y1 - state.winh - hscrollh () + d)
5433 else G.postRedisplay "linknav";
5435 showlinktype (getlink opaque m);
5436 state.mode <- LinkNav (Ltexact (l.pageno, m));
5439 | None -> viewkeyboard key mask
5440 end;
5441 | _ -> viewkeyboard key mask
5443 if key = @insert
5444 then (
5445 state.mode <- View;
5446 G.postRedisplay "leave linknav"
5448 else
5449 match linknav with
5450 | Ltgendir _ | Ltnotready _ -> viewkeyboard key mask
5451 | Ltexact exact -> doexact exact
5454 let keyboard key mask =
5455 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5456 then wcmd "interrupt"
5457 else state.uioh <- state.uioh#key key mask
5460 let birdseyekeyboard key mask
5461 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5462 let incr =
5463 match conf.columns with
5464 | Csingle _ -> 1
5465 | Cmulti ((c, _, _), _) -> c
5466 | Csplit _ -> failwith "bird's eye split mode"
5468 let pgh layout = List.fold_left
5469 (fun m l -> max l.pageh m) state.winh layout in
5470 match key with
5471 | @l when Wsi.withctrl mask ->
5472 let y, h = getpageyh pageno in
5473 let top = (state.winh - h) / 2 in
5474 gotoy (max 0 (y - top))
5475 | @enter | @kpenter -> leavebirdseye beye false
5476 | @escape -> leavebirdseye beye true
5477 | @up -> upbirdseye incr beye
5478 | @down -> downbirdseye incr beye
5479 | @left -> upbirdseye 1 beye
5480 | @right -> downbirdseye 1 beye
5482 | @prior ->
5483 begin match state.layout with
5484 | l :: _ ->
5485 if l.pagey != 0
5486 then (
5487 state.mode <- Birdseye (
5488 oconf, leftx, l.pageno, hooverpageno, anchor
5490 gotopage1 l.pageno 0;
5492 else (
5493 let layout = layout (state.y-state.winh) (pgh state.layout) in
5494 match layout with
5495 | [] -> gotoy (clamp (-state.winh))
5496 | l :: _ ->
5497 state.mode <- Birdseye (
5498 oconf, leftx, l.pageno, hooverpageno, anchor
5500 gotopage1 l.pageno 0
5503 | [] -> gotoy (clamp (-state.winh))
5504 end;
5506 | @next ->
5507 begin match List.rev state.layout with
5508 | l :: _ ->
5509 let layout = layout (state.y + (pgh state.layout)) state.winh in
5510 begin match layout with
5511 | [] ->
5512 let incr = l.pageh - l.pagevh in
5513 if incr = 0
5514 then (
5515 state.mode <-
5516 Birdseye (
5517 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5519 G.postRedisplay "birdseye pagedown";
5521 else gotoy (clamp (incr + conf.interpagespace*2));
5523 | l :: _ ->
5524 state.mode <-
5525 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5526 gotopage1 l.pageno 0;
5529 | [] -> gotoy (clamp state.winh)
5530 end;
5532 | @home ->
5533 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5534 gotopage1 0 0
5536 | @jend ->
5537 let pageno = state.pagecount - 1 in
5538 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5539 if not (pagevisible state.layout pageno)
5540 then
5541 let h =
5542 match List.rev state.pdims with
5543 | [] -> state.winh
5544 | (_, _, h, _) :: _ -> h
5546 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5547 else G.postRedisplay "birdseye end";
5549 | _ -> viewkeyboard key mask
5552 let drawpage l =
5553 let color =
5554 match state.mode with
5555 | Textentry _ -> scalecolor 0.4
5556 | LinkNav _
5557 | View -> scalecolor 1.0
5558 | Birdseye (_, _, pageno, hooverpageno, _) ->
5559 if l.pageno = hooverpageno
5560 then scalecolor 0.9
5561 else (
5562 if l.pageno = pageno
5563 then (
5564 let c = scalecolor 1.0 in
5565 GlDraw.color c;
5566 GlDraw.line_width 3.0;
5567 let dispx = xadjsb () + l.pagedispx in
5568 linerect
5569 (float (dispx-1)) (float (l.pagedispy-1))
5570 (float (dispx+l.pagevw+1))
5571 (float (l.pagedispy+l.pagevh+1))
5573 GlDraw.line_width 1.0;
5576 else scalecolor 0.8
5579 drawtiles l color;
5582 let postdrawpage l linkindexbase =
5583 match getopaque l.pageno with
5584 | Some opaque ->
5585 if tileready l l.pagex l.pagey
5586 then
5587 let x = l.pagedispx - l.pagex + xadjsb ()
5588 and y = l.pagedispy - l.pagey in
5589 let hlmask =
5590 match conf.columns with
5591 | Csingle _ | Cmulti _ ->
5592 (if conf.hlinks then 1 else 0)
5593 + (if state.glinks
5594 && not (isbirdseye state.mode) then 2 else 0)
5595 | Csplit _ -> 0
5597 let s =
5598 match state.mode with
5599 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5600 | Textentry _
5601 | Birdseye _
5602 | View
5603 | LinkNav _ -> E.s
5605 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5606 else 0
5607 | _ -> 0
5610 let scrollindicator () =
5611 let sbw, ph, sh = state.uioh#scrollph in
5612 let sbh, pw, sw = state.uioh#scrollpw in
5614 let x0,x1,hx0 =
5615 if conf.leftscroll
5616 then (0, sbw, sbw)
5617 else ((state.winw - sbw), state.winw, 0)
5620 GlDraw.color (0.64, 0.64, 0.64);
5621 filledrect (float x0) 0. (float x1) (float state.winh);
5622 filledrect
5623 (float hx0) (float (state.winh - sbh))
5624 (float (hx0 + wadjsb () + state.winw)) (float state.winh)
5626 GlDraw.color (0.0, 0.0, 0.0);
5628 filledrect (float x0) ph (float x1) (ph +. sh);
5629 let pw = pw +. float hx0 in
5630 filledrect pw (float (state.winh - sbh)) (pw +. sw) (float state.winh);
5633 let showsel () =
5634 match state.mstate with
5635 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5638 | Msel ((x0, y0), (x1, y1)) ->
5639 let identify opaque l px py = Some (opaque, l.pageno, px, py) in
5640 let o0,n0,px0,py0 = onppundermouse identify x0 y0 (~< E.s, -1, 0, 0) in
5641 let _o1,n1,px1,py1 = onppundermouse identify x1 y1 (~< E.s, -1, 0, 0) in
5642 if n0 != -1 && n0 = n1 then seltext o0 (px0, py0, px1, py1);
5645 let showrects = function [] -> () | rects ->
5646 Gl.enable `blend;
5647 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5648 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5649 List.iter
5650 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5651 List.iter (fun l ->
5652 if l.pageno = pageno
5653 then (
5654 let dx = float (l.pagedispx - l.pagex) in
5655 let dy = float (l.pagedispy - l.pagey) in
5656 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5657 Raw.sets_float state.vraw ~pos:0
5658 [| x0+.dx; y0+.dy;
5659 x1+.dx; y1+.dy;
5660 x3+.dx; y3+.dy;
5661 x2+.dx; y2+.dy |];
5662 GlArray.vertex `two state.vraw;
5663 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
5665 ) state.layout
5666 ) rects
5668 Gl.disable `blend;
5671 let display () =
5672 GlClear.color (scalecolor2 conf.bgcolor);
5673 GlClear.clear [`color];
5674 List.iter drawpage state.layout;
5675 let rects =
5676 match state.mode with
5677 | LinkNav (Ltexact (pageno, linkno)) ->
5678 begin match getopaque pageno with
5679 | Some opaque ->
5680 let dx = xadjsb () in
5681 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5682 let x0 = x0 + dx and x1 = x1 + dx in
5683 (pageno, 5, (
5684 float x0, float y0,
5685 float x1, float y0,
5686 float x1, float y1,
5687 float x0, float y1)
5688 ) :: state.rects
5689 | None -> state.rects
5691 | LinkNav (Ltgendir _) | LinkNav (Ltnotready _)
5692 | Birdseye _
5693 | Textentry _
5694 | View -> state.rects
5696 showrects rects;
5697 let rec postloop linkindexbase = function
5698 | l :: rest ->
5699 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5700 postloop linkindexbase rest
5701 | [] -> ()
5703 showsel ();
5704 postloop 0 state.layout;
5705 state.uioh#display;
5706 begin match state.mstate with
5707 | Mzoomrect ((x0, y0), (x1, y1)) ->
5708 Gl.enable `blend;
5709 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5710 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5711 filledrect (float x0) (float y0) (float x1) (float y1);
5712 Gl.disable `blend;
5713 | Msel _
5714 | Mpan _
5715 | Mscrolly | Mscrollx
5716 | Mzoom _
5717 | Mnone -> ()
5718 end;
5719 enttext ();
5720 scrollindicator ();
5721 Wsi.swapb ();
5724 let zoomrect x y x1 y1 =
5725 let x0 = min x x1
5726 and x1 = max x x1
5727 and y0 = min y y1 in
5728 gotoy (state.y + y0);
5729 state.anchor <- getanchor ();
5730 let zoom = (float state.w) /. float (x1 - x0) in
5731 let margin =
5732 let simple () =
5733 let adjw = wadjsb () + state.winw in
5734 if state.w < adjw
5735 then (adjw - state.w) / 2
5736 else 0
5738 match conf.fitmodel with
5739 | FitWidth | FitProportional -> simple ()
5740 | FitPage ->
5741 match conf.columns with
5742 | Csplit _ ->
5743 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
5744 | Cmulti _ | Csingle _ -> simple ()
5746 state.x <- (state.x + margin) - x0;
5747 setzoom zoom;
5748 resetmstate ();
5751 let annot inline x y =
5752 match unproject x y with
5753 | Some (opaque, n, ux, uy) ->
5754 let add text =
5755 addannot opaque ux uy text;
5756 wcmd "freepage %s" (~> opaque);
5757 Hashtbl.remove state.pagemap (n, state.gen);
5758 flushtiles ();
5759 gotoy state.y
5761 if inline
5762 then
5763 let ondone s = add s in
5764 let mode = state.mode in
5765 state.mode <- Textentry (
5766 ("annotation: ", E.s, None, textentry, ondone, true),
5767 fun _ -> state.mode <- mode);
5768 state.text <- E.s;
5769 enttext ();
5770 G.postRedisplay "annot"
5771 else
5772 add @@ getusertext E.s
5773 | _ -> ()
5776 let zoomblock x y =
5777 let g opaque l px py =
5778 match rectofblock opaque px py with
5779 | Some a ->
5780 let x0 = a.(0) -. 20. in
5781 let x1 = a.(1) +. 20. in
5782 let y0 = a.(2) -. 20. in
5783 let zoom = (float state.w) /. (x1 -. x0) in
5784 let pagey = getpagey l.pageno in
5785 gotoy_and_clear_text (pagey + truncate y0);
5786 state.anchor <- getanchor ();
5787 let margin = (state.w - l.pagew)/2 in
5788 state.x <- -truncate x0 - margin;
5789 setzoom zoom;
5790 None
5791 | None -> None
5793 match conf.columns with
5794 | Csplit _ ->
5795 showtext '!' "block zooming does not work properly in split columns mode"
5796 | Cmulti _ | Csingle _ -> onppundermouse g x y ()
5799 let scrollx x =
5800 let winw = wadjsb () + state.winw - 1 in
5801 let s = float x /. float winw in
5802 let destx = truncate (float (state.w + winw) *. s) in
5803 state.x <- winw - destx;
5804 gotoy_and_clear_text state.y;
5805 state.mstate <- Mscrollx;
5808 let scrolly y =
5809 let s = float y /. float state.winh in
5810 let desty = truncate (float (state.maxy - state.winh) *. s) in
5811 gotoy_and_clear_text desty;
5812 state.mstate <- Mscrolly;
5815 let viewmulticlick clicks x y mask =
5816 let g opaque l px py =
5817 let mark =
5818 match clicks with
5819 | 2 -> Mark_word
5820 | 3 -> Mark_line
5821 | 4 -> Mark_block
5822 | _ -> Mark_page
5824 if markunder opaque px py mark
5825 then (
5826 Some (fun () ->
5827 let dopipe cmd =
5828 match getopaque l.pageno with
5829 | None -> ()
5830 | Some opaque -> pipesel opaque cmd
5832 state.roam <- (fun () -> dopipe conf.paxcmd);
5833 if not (Wsi.withctrl mask) then dopipe conf.selcmd;
5836 else None
5838 G.postRedisplay "viewmulticlick";
5839 onppundermouse g x y (fun () -> showtext '!' "Nothing to select") ();
5842 let canselect () =
5843 match conf.columns with
5844 | Csplit _ -> false
5845 | Csingle _ | Cmulti _ -> conf.angle mod 360 = 0
5848 let viewmouse button down x y mask =
5849 match button with
5850 | n when (n == 4 || n == 5) && not down ->
5851 if Wsi.withctrl mask
5852 then (
5853 match state.mstate with
5854 | Mzoom (oldn, i) ->
5855 if oldn = n
5856 then (
5857 if i = 2
5858 then
5859 let incr =
5860 match n with
5861 | 5 ->
5862 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5863 | _ ->
5864 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5866 let zoom = conf.zoom -. incr in
5867 setzoom zoom;
5868 state.mstate <- Mzoom (n, 0);
5869 else
5870 state.mstate <- Mzoom (n, i+1);
5872 else state.mstate <- Mzoom (n, 0)
5874 | Msel _
5875 | Mpan _
5876 | Mscrolly | Mscrollx
5877 | Mzoomrect _
5878 | Mnone -> state.mstate <- Mzoom (n, 0)
5880 else (
5881 match state.autoscroll with
5882 | Some step -> setautoscrollspeed step (n=4)
5883 | None ->
5884 if conf.wheelbypage || conf.presentation
5885 then (
5886 if n = 4
5887 then prevpage ()
5888 else nextpage ()
5890 else
5891 let incr =
5892 if n = 4
5893 then -conf.scrollstep
5894 else conf.scrollstep
5896 let incr = incr * 2 in
5897 let y = clamp incr in
5898 gotoy_and_clear_text y
5901 | n when (n = 6 || n = 7) && not down && canpan () ->
5902 state.x <-
5903 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
5904 gotoy_and_clear_text state.y
5906 | 1 when Wsi.withshift mask ->
5907 state.mstate <- Mnone;
5908 if not down
5909 then (
5910 match unproject x y with
5911 | Some (_, pageno, ux, uy) ->
5912 let cmd = Printf.sprintf
5913 "%s %s %d %d %d"
5914 conf.stcmd state.path pageno ux uy
5916 addpid @@ popen cmd []
5917 | None -> ()
5920 | 1 when Wsi.withctrl mask ->
5921 if down
5922 then (
5923 Wsi.setcursor Wsi.CURSOR_FLEUR;
5924 state.mstate <- Mpan (x, y)
5926 else
5927 state.mstate <- Mnone
5929 | 3 ->
5930 if down
5931 then (
5932 if Wsi.withshift mask
5933 then (
5934 annot conf.annotinline x y;
5935 G.postRedisplay "addannot"
5937 else
5938 let p = (x, y) in
5939 Wsi.setcursor Wsi.CURSOR_CYCLE;
5940 state.mstate <- Mzoomrect (p, p)
5942 else (
5943 match state.mstate with
5944 | Mzoomrect ((x0, y0), _) ->
5945 if abs (x-x0) > 10 && abs (y - y0) > 10
5946 then zoomrect x0 y0 x y
5947 else (
5948 resetmstate ();
5949 G.postRedisplay "kill accidental zoom rect";
5951 | Msel _
5952 | Mpan _
5953 | Mscrolly | Mscrollx
5954 | Mzoom _
5955 | Mnone ->
5956 resetmstate ()
5959 | 1 when x > state.winw - vscrollw () ->
5960 if down
5961 then
5962 let _, position, sh = state.uioh#scrollph in
5963 if y > truncate position && y < truncate (position +. sh)
5964 then state.mstate <- Mscrolly
5965 else scrolly y
5966 else
5967 state.mstate <- Mnone
5969 | 1 when y > state.winh - hscrollh () ->
5970 if down
5971 then
5972 let _, position, sw = state.uioh#scrollpw in
5973 if x > truncate position && x < truncate (position +. sw)
5974 then state.mstate <- Mscrollx
5975 else scrollx x
5976 else
5977 state.mstate <- Mnone
5979 | 1 when state.bzoom -> if not down then zoomblock x y
5981 | 1 ->
5982 let dest = if down then getunder x y else Unone in
5983 begin match dest with
5984 | Ulinkgoto _
5985 | Ulinkuri _
5986 | Uremote _ | Uremotedest _
5987 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5988 gotounder dest
5990 | Unone when down ->
5991 Wsi.setcursor Wsi.CURSOR_FLEUR;
5992 state.mstate <- Mpan (x, y);
5994 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
5996 | Unone | Utext _ ->
5997 if down
5998 then (
5999 if canselect ()
6000 then (
6001 state.mstate <- Msel ((x, y), (x, y));
6002 G.postRedisplay "mouse select";
6005 else (
6006 match state.mstate with
6007 | Mnone -> ()
6009 | Mzoom _ | Mscrollx | Mscrolly ->
6010 state.mstate <- Mnone
6012 | Mzoomrect ((x0, y0), _) ->
6013 zoomrect x0 y0 x y
6015 | Mpan _ ->
6016 Wsi.setcursor Wsi.CURSOR_INHERIT;
6017 state.mstate <- Mnone
6019 | Msel ((x0, y0), (x1, y1)) ->
6020 let rec loop = function
6021 | [] -> ()
6022 | l :: rest ->
6023 let inside =
6024 let a0 = l.pagedispy in
6025 let a1 = a0 + l.pagevh in
6026 let b0 = l.pagedispx in
6027 let b1 = b0 + l.pagevw in
6028 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
6029 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
6031 if inside
6032 then
6033 match getopaque l.pageno with
6034 | Some opaque ->
6035 let dosel cmd () =
6036 match Unix.pipe () with
6037 | exception exn ->
6038 showtext '!'
6039 (Printf.sprintf
6040 "can not create sel pipe: %s"
6041 (exntos exn));
6042 | (r, w) ->
6043 let clo what fd =
6044 Ne.clo fd (fun msg ->
6045 dolog "%s close failed: %s" what msg)
6047 let pid =
6048 try popen cmd [r, 0; w, -1]
6049 with exn ->
6050 dolog "can not execute %S: %s"
6051 cmd (exntos exn);
6054 if pid > 0
6055 then (
6056 copysel w opaque;
6057 G.postRedisplay "copysel";
6059 else clo "Msel pipe/w" w;
6060 clo "Msel pipe/r" r;
6062 dosel conf.selcmd ();
6063 state.roam <- dosel conf.paxcmd;
6064 | None -> ()
6065 else loop rest
6067 loop state.layout;
6068 resetmstate ();
6072 | _ -> ()
6075 let birdseyemouse button down x y mask
6076 (conf, leftx, _, hooverpageno, anchor) =
6077 match button with
6078 | 1 when down ->
6079 let rec loop = function
6080 | [] -> ()
6081 | l :: rest ->
6082 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6083 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6084 then (
6085 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
6087 else loop rest
6089 loop state.layout
6090 | 3 -> ()
6091 | _ -> viewmouse button down x y mask
6094 let uioh = object
6095 method display = ()
6097 method key key mask =
6098 begin match state.mode with
6099 | Textentry textentry -> textentrykeyboard key mask textentry
6100 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6101 | View -> viewkeyboard key mask
6102 | LinkNav linknav -> linknavkeyboard key mask linknav
6103 end;
6104 state.uioh
6106 method button button bstate x y mask =
6107 begin match state.mode with
6108 | LinkNav _
6109 | View -> viewmouse button bstate x y mask
6110 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6111 | Textentry _ -> ()
6112 end;
6113 state.uioh
6115 method multiclick clicks x y mask =
6116 begin match state.mode with
6117 | LinkNav _
6118 | View -> viewmulticlick clicks x y mask
6119 | Birdseye _
6120 | Textentry _ -> ()
6121 end;
6122 state.uioh
6124 method motion x y =
6125 begin match state.mode with
6126 | Textentry _ -> ()
6127 | View | Birdseye _ | LinkNav _ ->
6128 match state.mstate with
6129 | Mzoom _ | Mnone -> ()
6131 | Mpan (x0, y0) ->
6132 let dx = x - x0
6133 and dy = y0 - y in
6134 state.mstate <- Mpan (x, y);
6135 if canpan ()
6136 then state.x <- panbound (state.x + dx);
6137 let y = clamp dy in
6138 gotoy_and_clear_text y
6140 | Msel (a, _) ->
6141 state.mstate <- Msel (a, (x, y));
6142 G.postRedisplay "motion select";
6144 | Mscrolly ->
6145 let y = min state.winh (max 0 y) in
6146 scrolly y
6148 | Mscrollx ->
6149 let x = min state.winw (max 0 x) in
6150 scrollx x
6152 | Mzoomrect (p0, _) ->
6153 state.mstate <- Mzoomrect (p0, (x, y));
6154 G.postRedisplay "motion zoomrect";
6155 end;
6156 state.uioh
6158 method pmotion x y =
6159 begin match state.mode with
6160 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6161 let rec loop = function
6162 | [] ->
6163 if hooverpageno != -1
6164 then (
6165 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6166 G.postRedisplay "pmotion birdseye no hoover";
6168 | l :: rest ->
6169 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6170 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6171 then (
6172 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6173 G.postRedisplay "pmotion birdseye hoover";
6175 else loop rest
6177 loop state.layout
6179 | Textentry _ -> ()
6181 | LinkNav _
6182 | View ->
6183 match state.mstate with
6184 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ -> ()
6185 | Mnone ->
6186 updateunder x y;
6187 if canselect ()
6188 then
6189 match conf.pax with
6190 | None -> ()
6191 | Some r ->
6192 let past, _, _ = !r in
6193 let now = now () in
6194 let delta = now -. past in
6195 if delta > 0.01
6196 then paxunder x y
6197 else r := (now, x, y)
6198 end;
6199 state.uioh
6201 method infochanged _ = ()
6203 method scrollph =
6204 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6205 let p, h =
6206 if maxy = 0
6207 then 0.0, float state.winh
6208 else scrollph state.y maxy
6210 vscrollw (), p, h
6212 method scrollpw =
6213 let winw = wadjsb () + state.winw in
6214 let fwinw = float winw in
6215 let sw =
6216 let sw = fwinw /. float state.w in
6217 let sw = fwinw *. sw in
6218 max sw (float conf.scrollh)
6220 let position =
6221 let maxx = state.w + winw in
6222 let x = winw - state.x in
6223 let percent = float x /. float maxx in
6224 (fwinw -. sw) *. percent
6226 hscrollh (), position, sw
6228 method modehash =
6229 let modename =
6230 match state.mode with
6231 | LinkNav _ -> "links"
6232 | Textentry _ -> "textentry"
6233 | Birdseye _ -> "birdseye"
6234 | View -> "view"
6236 findkeyhash conf modename
6238 method eformsgs = true
6239 method alwaysscrolly = false
6240 end;;
6242 let adderrmsg src msg =
6243 Buffer.add_string state.errmsgs msg;
6244 state.newerrmsgs <- true;
6245 G.postRedisplay src
6248 let adderrfmt src fmt =
6249 Format.ksprintf (fun s -> adderrmsg src s) fmt;
6252 let ract cmds =
6253 let cl = splitatspace cmds in
6254 let scan s fmt f =
6255 try Scanf.sscanf s fmt f
6256 with exn ->
6257 adderrfmt "remote exec"
6258 "error processing '%S': %s\n" cmds (exntos exn)
6260 match cl with
6261 | "reload" :: [] -> reload ()
6262 | "goto" :: args :: [] ->
6263 scan args "%u %f %f"
6264 (fun pageno x y ->
6265 let cmd, _ = state.geomcmds in
6266 if emptystr cmd
6267 then gotopagexy pageno x y
6268 else
6269 let f prevf () =
6270 gotopagexy pageno x y;
6271 prevf ()
6273 state.reprf <- f state.reprf
6275 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
6276 | "gotor" :: args :: [] ->
6277 scan args "%S %u"
6278 (fun filename pageno -> gotounder (Uremote (filename, pageno)))
6279 | "gotord" :: args :: [] ->
6280 scan args "%S %S"
6281 (fun filename dest -> gotounder (Uremotedest (filename, dest)))
6282 | "rect" :: args :: [] ->
6283 scan args "%u %u %f %f %f %f"
6284 (fun pageno color x0 y0 x1 y1 ->
6285 onpagerect pageno (fun w h ->
6286 let _,w1,h1,_ = getpagedim pageno in
6287 let sw = float w1 /. float w
6288 and sh = float h1 /. float h in
6289 let x0s = x0 *. sw
6290 and x1s = x1 *. sw
6291 and y0s = y0 *. sh
6292 and y1s = y1 *. sh in
6293 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
6294 debugrect rect;
6295 state.rects <- (pageno, color, rect) :: state.rects;
6296 G.postRedisplay "rect";
6299 | "activatewin" :: [] -> Wsi.activatewin ()
6300 | "quit" :: [] -> raise Quit
6301 | _ ->
6302 adderrfmt "remote command"
6303 "error processing remote command: %S\n" cmds;
6306 let remote =
6307 let scratch = Bytes.create 80 in
6308 let buf = Buffer.create 80 in
6309 fun fd ->
6310 let rec tempfr () =
6311 try Some (Unix.read fd scratch 0 80)
6312 with
6313 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
6314 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
6315 | exn -> raise exn
6317 match tempfr () with
6318 | None -> Some fd
6319 | Some n ->
6320 if n = 0
6321 then (
6322 Unix.close fd;
6323 if Buffer.length buf > 0
6324 then (
6325 let s = Buffer.contents buf in
6326 Buffer.clear buf;
6327 ract s;
6329 None
6331 else
6332 let rec eat ppos =
6333 let nlpos =
6335 let pos = Bytes.index_from scratch ppos '\n' in
6336 if pos >= n then -1 else pos
6337 with Not_found -> -1
6339 if nlpos >= 0
6340 then (
6341 Buffer.add_subbytes buf scratch ppos (nlpos-ppos);
6342 let s = Buffer.contents buf in
6343 Buffer.clear buf;
6344 ract s;
6345 eat (nlpos+1);
6347 else (
6348 Buffer.add_subbytes buf scratch ppos (n-ppos);
6349 Some fd
6351 in eat 0
6354 let remoteopen path =
6355 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
6356 with exn ->
6357 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
6358 None
6361 let () =
6362 let gcconfig = ref E.s in
6363 let trimcachepath = ref E.s in
6364 let rcmdpath = ref E.s in
6365 let pageno = ref None in
6366 let rootwid = ref 0 in
6367 let openlast = ref false in
6368 let nofc = ref false in
6369 let doreap = ref false in
6370 selfexec := Sys.executable_name;
6371 Arg.parse
6372 (Arg.align
6373 [("-p", Arg.String (fun s -> state.password <- s),
6374 "<password> Set password");
6376 ("-f", Arg.String
6377 (fun s ->
6378 Config.fontpath := s;
6379 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
6381 "<path> Set path to the user interface font");
6383 ("-c", Arg.String
6384 (fun s ->
6385 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
6386 Config.confpath := s),
6387 "<path> Set path to the configuration file");
6389 ("-last", Arg.Set openlast, " Open last document");
6391 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
6392 "<page-number> Jump to page");
6394 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6395 "<path> Set path to the trim cache file");
6397 ("-dest", Arg.String (fun s -> state.nameddest <- s),
6398 "<named-destination> Set named destination");
6400 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
6401 ("-cxack", Arg.Set cxack, " Cut corners");
6403 ("-remote", Arg.String (fun s -> rcmdpath := s),
6404 "<path> Set path to the remote commands source");
6406 ("-origin", Arg.String (fun s -> state.origin <- s),
6407 "<original-path> Set original path");
6409 ("-gc", Arg.Set_string gcconfig,
6410 "<script-path> Collect garbage with the help of a script");
6412 ("-nofc", Arg.Set nofc, " Do not use fontconfig");
6414 ("-v", Arg.Unit (fun () ->
6415 Printf.printf
6416 "%s\nconfiguration path: %s\n"
6417 (version ())
6418 Config.defconfpath
6420 exit 0), " Print version and exit");
6422 ("-embed", Arg.Set_int rootwid,
6423 "<window-id> Embed into window")
6426 (fun s -> state.path <- s)
6427 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6429 if !wtmode
6430 then selfexec := !selfexec ^ " -wtmode";
6432 let histmode = emptystr state.path && not !openlast in
6434 if not (Config.load !openlast)
6435 then prerr_endline "failed to load configuration";
6436 begin match !pageno with
6437 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
6438 | None -> ()
6439 end;
6441 if nonemptystr !gcconfig
6442 then (
6443 let c, s =
6444 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6445 | exception exn ->
6446 error "gc socketpair failed: %s" (exntos exn)
6447 | rw -> rw
6449 match addpid @@ popen !gcconfig [(c, 0); (c, 1)] with
6450 | exception exn ->
6451 error "failed to popen gc script: %s" (exntos exn);
6452 | _ ->
6453 Config.gc s s;
6454 exit 0
6457 let wsfd, winw, winh = Wsi.init (object (self)
6458 val mutable m_clicks = 0
6459 val mutable m_click_x = 0
6460 val mutable m_click_y = 0
6461 val mutable m_lastclicktime = infinity
6463 method private cleanup =
6464 state.roam <- noroam;
6465 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap
6466 method expose = G.postRedisplay"expose"
6467 method visible v =
6468 let name =
6469 match v with
6470 | Wsi.Unobscured -> "unobscured"
6471 | Wsi.PartiallyObscured -> "partiallyobscured"
6472 | Wsi.FullyObscured -> "fullyobscured"
6474 vlog "visibility change %s" name
6475 method display = display ()
6476 method map mapped = vlog "mappped %b" mapped
6477 method reshape w h =
6478 self#cleanup;
6479 reshape w h
6480 method mouse b d x y m =
6481 if d && canselect ()
6482 then (
6483 (* http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx *)
6484 m_click_x <- x;
6485 m_click_y <- y;
6486 if b = 1
6487 then (
6488 let t = now () in
6489 if abs x - m_click_x > 10
6490 || abs y - m_click_y > 10
6491 || abs_float (t -. m_lastclicktime) > 0.3
6492 then m_clicks <- 0;
6493 m_clicks <- m_clicks + 1;
6494 m_lastclicktime <- t;
6495 if m_clicks = 1
6496 then (
6497 self#cleanup;
6498 G.postRedisplay "cleanup";
6499 state.uioh <- state.uioh#button b d x y m;
6501 else state.uioh <- state.uioh#multiclick m_clicks x y m
6503 else (
6504 self#cleanup;
6505 m_clicks <- 0;
6506 m_lastclicktime <- infinity;
6507 state.uioh <- state.uioh#button b d x y m
6510 else (
6511 state.uioh <- state.uioh#button b d x y m
6513 method motion x y =
6514 state.mpos <- (x, y);
6515 state.uioh <- state.uioh#motion x y
6516 method pmotion x y =
6517 state.mpos <- (x, y);
6518 state.uioh <- state.uioh#pmotion x y
6519 method key k m =
6520 let mascm = m land (
6521 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6522 ) in
6523 let keyboard k m =
6524 let x = state.x and y = state.y in
6525 keyboard k m;
6526 if x != state.x || y != state.y then self#cleanup
6528 match state.keystate with
6529 | KSnone ->
6530 let km = k, mascm in
6531 begin
6532 match
6533 let modehash = state.uioh#modehash in
6534 try Hashtbl.find modehash km
6535 with Not_found ->
6536 try Hashtbl.find (findkeyhash conf "global") km
6537 with Not_found -> KMinsrt (k, m)
6538 with
6539 | KMinsrt (k, m) -> keyboard k m
6540 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6541 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6543 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6544 List.iter (fun (k, m) -> keyboard k m) insrt;
6545 state.keystate <- KSnone
6546 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6547 state.keystate <- KSinto (keys, insrt)
6548 | KSinto _ -> state.keystate <- KSnone
6550 method enter x y =
6551 state.mpos <- (x, y);
6552 state.uioh <- state.uioh#pmotion x y
6553 method leave = state.mpos <- (-1, -1)
6554 method winstate wsl = state.winstate <- wsl
6555 method quit = raise Quit
6556 end) !rootwid conf.cwinw conf.cwinh platform in
6558 state.wsfd <- wsfd;
6560 if not (
6561 List.exists GlMisc.check_extension
6562 [ "GL_ARB_texture_rectangle"
6563 ; "GL_EXT_texture_recangle"
6564 ; "GL_NV_texture_rectangle" ]
6566 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6568 if (
6569 let r = GlMisc.get_string `renderer in
6570 let p = "Mesa DRI Intel(" in
6571 let l = String.length p in
6572 String.length r > l && String.sub r 0 l = p
6574 then (
6575 defconf.sliceheight <- 1024;
6576 defconf.texcount <- 32;
6577 defconf.usepbo <- true;
6580 let cs, ss =
6581 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6582 | exception exn ->
6583 Printf.eprintf "socketpair failed: %s" (exntos exn);
6584 exit 1
6585 | (r, w) ->
6586 cloexec r;
6587 cloexec w;
6588 r, w
6591 setcheckers conf.checkers;
6592 redirectstderr ();
6593 if conf.redirectstderr
6594 then
6595 at_exit (fun () ->
6596 let s = Bytes.cat
6597 (Buffer.to_bytes state.errmsgs)
6598 (match state.errfd with
6599 | Some fd ->
6600 let s = Bytes.create (80*24) in
6601 let n =
6603 let r, _, _ = Unix.select [fd] [] [] 0.0 in
6604 if List.mem fd r
6605 then Unix.read fd s 0 (Bytes.length s)
6606 else 0
6607 with _ -> 0
6609 if n = 0
6610 then E.b
6611 else Bytes.sub s 0 n
6612 | None -> E.b
6615 try ignore (Unix.write state.stderr s 0 (Bytes.length s))
6616 with exn -> print_endline (exntos exn)
6620 init cs (
6621 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
6622 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6623 !Config.fontpath, !trimcachepath,
6624 GlMisc.check_extension "GL_ARB_pixel_buffer_object",
6625 not !nofc
6627 List.iter GlArray.enable [`texture_coord; `vertex];
6628 state.ss <- ss;
6629 reshape ~firsttime:true winw winh;
6630 state.uioh <- uioh;
6631 if histmode
6632 then (
6633 Wsi.settitle "llpp (history)";
6634 enterhistmode ();
6636 else (
6637 state.text <- "Opening " ^ (mbtoutf8 state.path);
6638 opendoc state.path state.password;
6640 display ();
6641 Wsi.mapwin ();
6642 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
6644 let rec reap () =
6645 match Unix.waitpid [Unix.WNOHANG] ~-1 with
6646 | (exception (Unix.Unix_error (Unix.ECHILD, _, _))) -> ()
6647 | (exception exn) -> dolog "Unix.waitpid: %s" @@ exntos exn
6648 | 0, _ -> ()
6649 | _pid, _status -> reap ()
6651 Sys.set_signal Sys.sigchld (Sys.Signal_handle (fun _ -> doreap := true));
6653 let optrfd =
6654 ref (
6655 if nonemptystr !rcmdpath
6656 then remoteopen !rcmdpath
6657 else None
6661 let rec loop deadline =
6662 if !doreap
6663 then (
6664 doreap := false;
6665 reap ()
6667 let r =
6668 match state.errfd with
6669 | None -> [state.ss; state.wsfd]
6670 | Some fd -> [state.ss; state.wsfd; fd]
6672 let r =
6673 match !optrfd with
6674 | None -> r
6675 | Some fd -> fd :: r
6677 if state.redisplay
6678 then (
6679 state.redisplay <- false;
6680 display ();
6682 let timeout =
6683 let now = now () in
6684 if deadline > now
6685 then (
6686 if deadline = infinity
6687 then ~-.1.0
6688 else max 0.0 (deadline -. now)
6690 else 0.0
6692 let r, _, _ =
6693 try Unix.select r [] [] timeout
6694 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6696 begin match r with
6697 | [] ->
6698 state.ghyll None;
6699 let newdeadline =
6700 if state.ghyll == noghyll
6701 then
6702 match state.autoscroll with
6703 | Some step when step != 0 ->
6704 let y = state.y + step in
6705 let y =
6706 if y < 0
6707 then state.maxy
6708 else if y >= state.maxy then 0 else y
6710 gotoy y;
6711 if state.mode = View
6712 then state.text <- E.s;
6713 deadline +. 0.01
6714 | _ -> infinity
6715 else deadline +. 0.01
6717 loop newdeadline
6719 | l ->
6720 let rec checkfds = function
6721 | [] -> ()
6722 | fd :: rest when fd = state.ss ->
6723 let cmd = readcmd state.ss in
6724 act cmd;
6725 checkfds rest
6727 | fd :: rest when fd = state.wsfd ->
6728 Wsi.readresp fd;
6729 checkfds rest
6731 | fd :: rest when Some fd = !optrfd ->
6732 begin match remote fd with
6733 | None -> optrfd := remoteopen !rcmdpath;
6734 | opt -> optrfd := opt
6735 end;
6736 checkfds rest
6738 | fd :: rest ->
6739 let s = Bytes.create 80 in
6740 let n = tempfailureretry (Unix.read fd s 0) 80 in
6741 if conf.redirectstderr
6742 then (
6743 Buffer.add_substring state.errmsgs (Bytes.to_string s) 0 n;
6744 state.newerrmsgs <- true;
6745 state.redisplay <- true;
6747 else (
6748 prerr_string (String.sub (Bytes.to_string s) 0 n);
6749 flush stderr;
6751 checkfds rest
6753 checkfds l;
6754 let newdeadline =
6755 let deadline1 =
6756 if deadline = infinity
6757 then now () +. 0.01
6758 else deadline
6760 match state.autoscroll with
6761 | Some step when step != 0 -> deadline1
6762 | _ -> if state.ghyll == noghyll then infinity else deadline1
6764 loop newdeadline
6765 end;
6768 loop infinity;
6769 with Quit ->
6770 Config.save leavebirdseye;
6771 if hasunsavedchanges ()
6772 then save ();