Don't lie
[llpp.git] / main.ml
blob1e9d80b25776e5e7d925bffd712cfdb47c45db64
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 reeenterhist = ref false;;
48 let selfexec = ref E.s;;
50 let drawstring size x y s =
51 Gl.enable `blend;
52 Gl.enable `texture_2d;
53 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
54 ignore (drawstr size x y s);
55 Gl.disable `blend;
56 Gl.disable `texture_2d;
59 let drawstring1 size x y s =
60 drawstr size x y s;
63 let drawstring2 size x y fmt =
64 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
67 let debugl l =
68 dolog "l %d dim=%d {" l.pageno l.pagedimno;
69 dolog " WxH %dx%d" l.pagew l.pageh;
70 dolog " vWxH %dx%d" l.pagevw l.pagevh;
71 dolog " pagex,y %d,%d" l.pagex l.pagey;
72 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
73 dolog " column %d" l.pagecol;
74 dolog "}";
77 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
78 dolog "rect {";
79 dolog " x0,y0=(% f, % f)" x0 y0;
80 dolog " x1,y1=(% f, % f)" x1 y1;
81 dolog " x2,y2=(% f, % f)" x2 y2;
82 dolog " x3,y3=(% f, % f)" x3 y3;
83 dolog "}";
86 let isbirdseye = function
87 | Birdseye _ -> true
88 | Textentry _
89 | View
90 | LinkNav _ -> false
93 let istextentry = function
94 | Textentry _ -> true
95 | Birdseye _
96 | View
97 | LinkNav _ -> false
100 let wtmode = ref false;;
101 let cxack = ref false;;
103 let pgscale h = truncate (float h *. conf.pgscale);;
105 let hscrollh () =
106 if not state.uioh#alwaysscrolly && (conf.scrollb land scrollbhv = 0)
107 || (state.x = 0 && state.w <= state.winw - conf.scrollbw)
108 then 0
109 else conf.scrollbw
112 let vscrollw () =
113 if not state.uioh#alwaysscrolly && (conf.scrollb land scrollbvv = 0)
114 then 0
115 else conf.scrollbw
118 let wadjsb () = -vscrollw ();;
119 let xadjsb () = if conf.leftscroll then vscrollw () else 0;;
121 let setfontsize n =
122 fstate.fontsize <- n;
123 fstate.wwidth <- measurestr fstate.fontsize "w";
124 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
127 let vlog fmt =
128 if conf.verbose
129 then
130 Printf.kprintf prerr_endline fmt
131 else
132 Printf.kprintf ignore fmt
135 let launchpath () =
136 if emptystr conf.pathlauncher
137 then print_endline state.path
138 else (
139 let command = Str.global_replace percentsre state.path conf.pathlauncher in
140 try addpid @@ popen command []
141 with exn ->
142 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
143 flush stderr;
147 let redirectstderr () =
148 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
149 if conf.redirectstderr
150 then
151 match Unix.pipe () with
152 | exception exn ->
153 dolog "failed to create stderr redirection pipes: %s" (exntos exn)
155 | (r, w) ->
156 begin match Unix.dup Unix.stderr with
157 | exception exn ->
158 dolog "failed to dup stderr: %s" (exntos exn);
159 Ne.clo r (clofail "pipe/r");
160 Ne.clo w (clofail "pipe/w");
162 | dupstderr ->
163 begin match Unix.dup2 w Unix.stderr with
164 | exception exn ->
165 dolog "failed to dup2 to stderr: %s" (exntos exn);
166 Ne.clo dupstderr (clofail "stderr duplicate");
167 Ne.clo r (clofail "redir pipe/r");
168 Ne.clo w (clofail "redir pipe/w");
170 | () ->
171 state.stderr <- dupstderr;
172 state.errfd <- Some r;
173 end;
175 else (
176 state.newerrmsgs <- false;
177 begin match state.errfd with
178 | Some fd ->
179 begin match Unix.dup2 state.stderr Unix.stderr with
180 | exception exn ->
181 dolog "failed to dup2 original stderr: %s" (exntos exn)
182 | () ->
183 Ne.clo fd (clofail "dup of stderr");
184 state.errfd <- None;
185 end;
186 | None -> ()
187 end;
188 prerr_string (Buffer.contents state.errmsgs);
189 flush stderr;
190 Buffer.clear state.errmsgs;
194 module G =
195 struct
196 let postRedisplay who =
197 if conf.verbose
198 then prerr_endline ("redisplay for " ^ who);
199 state.redisplay <- true;
201 end;;
203 let getopaque pageno =
204 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
205 with Not_found -> None
208 let putopaque pageno opaque =
209 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
212 let pagetranslatepoint l x y =
213 let dy = y - l.pagedispy in
214 let y = dy + l.pagey in
215 let dx = x - l.pagedispx in
216 let x = dx + l.pagex in
217 (x, y);
220 let onppundermouse g x y d =
221 let rec f = function
222 | l :: rest ->
223 begin match getopaque l.pageno with
224 | Some opaque ->
225 let x0 = l.pagedispx in
226 let x1 = x0 + l.pagevw in
227 let y0 = l.pagedispy in
228 let y1 = y0 + l.pagevh in
229 if y >= y0 && y <= y1 && x >= x0 && x <= x1
230 then
231 let px, py = pagetranslatepoint l x y in
232 match g opaque l px py with
233 | Some res -> res
234 | None -> f rest
235 else f rest
236 | _ ->
237 f rest
239 | [] -> d
241 f state.layout
244 let getunder x y =
245 let g opaque l px py =
246 if state.bzoom
247 then (
248 match rectofblock opaque px py with
249 | Some a ->
250 let rect = (a.(0),a.(2),a.(1),a.(2),a.(1),a.(3),a.(0),a.(3)) in
251 state.rects <- [l.pageno, l.pageno mod 3, rect];
252 G.postRedisplay "getunder";
253 | None -> ()
255 let under = whatsunder opaque px py in
256 if under = Unone then None else Some under
258 onppundermouse g x y Unone
261 let unproject x y =
262 let g opaque l x y =
263 match unproject opaque x y with
264 | Some (x, y) -> Some (Some (opaque, l.pageno, x, y))
265 | None -> None
267 onppundermouse g x y None;
270 let showtext c s =
271 state.text <- Printf.sprintf "%c%s" c s;
272 G.postRedisplay "showtext";
275 let pipesel opaque cmd =
276 if hassel opaque
277 then
278 match Unix.pipe () with
279 | exception exn ->
280 showtext '!'
281 (Printf.sprintf "pipesel can not create pipe: %s" (exntos exn));
282 | (r, w) ->
283 let doclose what fd =
284 Ne.clo fd (fun msg -> dolog "%s close failed: %s" what msg)
286 let pid =
287 try popen cmd [r, 0; w, -1]
288 with exn ->
289 dolog "can not execute %S: %s" cmd (exntos exn);
292 addpid pid;
293 if pid > 0
294 then (
295 copysel w opaque;
296 G.postRedisplay "pipesel";
298 else doclose "pipesel pipe/w" w;
299 doclose "pipesel pipe/r" r;
302 let paxunder x y =
303 let g opaque l px py =
304 if markunder opaque px py conf.paxmark
305 then (
306 Some (fun () ->
307 match getopaque l.pageno with
308 | None -> ()
309 | Some opaque -> pipesel opaque conf.paxcmd
312 else None
314 G.postRedisplay "paxunder";
315 if conf.paxmark = Mark_page
316 then
317 List.iter (fun l ->
318 match getopaque l.pageno with
319 | None -> ()
320 | Some opaque -> clearmark opaque) state.layout;
321 state.roam <-
322 onppundermouse g x y (fun () -> showtext '!' "Whoopsie daisy");
325 let selstring s =
326 match Unix.pipe () with
327 | exception exn ->
328 showtext '!' (Printf.sprintf "pipe failed: %s" (exntos exn))
329 | (r, w) ->
330 let clo cap fd =
331 Ne.clo fd (fun msg ->
332 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
335 let pid =
336 try popen conf.selcmd [r, 0; w, -1]
337 with exn ->
338 showtext '!'
339 (Printf.sprintf "failed to execute %s: %s"
340 conf.selcmd (exntos exn));
343 addpid pid;
344 if pid > 0
345 then (
347 let l = String.length s in
348 let bytes = Bytes.unsafe_of_string s in
349 let n = tempfailureretry (Unix.write w bytes 0) l in
350 if n != l
351 then
352 showtext '!'
353 (Printf.sprintf
354 "failed to write %d characters to sel pipe, wrote %d"
357 with exn ->
358 showtext '!'
359 (Printf.sprintf "failed to write to sel pipe: %s"
360 (exntos exn)
363 else dolog "%s" s;
364 clo "selstring pipe/r" r;
365 clo "selstring pipe/w" w;
368 let undertext = function
369 | Unone -> "none"
370 | Ulinkuri s -> s
371 | Ulinkgoto (pageno, _) -> 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 w h =
1395 GlDraw.viewport ~x:0 ~y:0 ~w:w ~h:h;
1396 let firsttime = state.geomcmds == firstgeomcmds in
1397 if not firsttime && nogeomcmds state.geomcmds
1398 then state.anchor <- getanchor ();
1400 state.winw <- w;
1401 let w = wadjsb () + (truncate (float w *. conf.zoom)) in
1402 let w = max w 2 in
1403 state.winh <- h;
1404 setfontsize fstate.fontsize;
1405 GlMat.mode `modelview;
1406 GlMat.load_identity ();
1408 GlMat.mode `projection;
1409 GlMat.load_identity ();
1410 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1411 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1412 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
1414 let relx =
1415 if conf.zoom <= 1.0
1416 then 0.0
1417 else float state.x /. float state.w
1419 invalidate "geometry"
1420 (fun () ->
1421 state.w <- w;
1422 if not firsttime
1423 then state.x <- truncate (relx *. float w);
1424 let w =
1425 match conf.columns with
1426 | Csingle _ -> w
1427 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1428 | Csplit (c, _) -> w * c
1430 wcmd "geometry %d %d %d"
1431 w (stateh h) (FMTE.to_int conf.fitmodel)
1435 let enttext () =
1436 let len = String.length state.text in
1437 let x0 = xadjsb () in
1438 let drawstring s =
1439 let hscrollh =
1440 match state.mode with
1441 | Textentry _ | View | LinkNav _ ->
1442 let h, _, _ = state.uioh#scrollpw in
1444 | Birdseye _ -> 0
1446 let rect x w =
1447 filledrect x (float (state.winh - (fstate.fontsize + 4) - hscrollh))
1448 (x+.w) (float (state.winh - hscrollh))
1451 let w = float (wadjsb () + state.winw - 1) in
1452 if state.progress >= 0.0 && state.progress < 1.0
1453 then (
1454 GlDraw.color (0.3, 0.3, 0.3);
1455 let w1 = w *. state.progress in
1456 rect (float x0) w1;
1457 GlDraw.color (0.0, 0.0, 0.0);
1458 rect (float x0+.w1) (float x0+.w-.w1)
1460 else (
1461 GlDraw.color (0.0, 0.0, 0.0);
1462 rect (float x0) w;
1465 GlDraw.color (1.0, 1.0, 1.0);
1466 drawstring fstate.fontsize
1467 (if conf.leftscroll then x0 + 2 else x0 + if len > 0 then 8 else 2)
1468 (state.winh - hscrollh - 5) s;
1470 let s =
1471 match state.mode with
1472 | Textentry ((prefix, text, _, _, _, _), _) ->
1473 let s =
1474 if len > 0
1475 then
1476 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1477 else
1478 Printf.sprintf "%s%s_" prefix text
1482 | Birdseye _
1483 | View
1484 | LinkNav _ -> state.text
1486 let s =
1487 if state.newerrmsgs
1488 then (
1489 if not (istextentry state.mode) && state.uioh#eformsgs
1490 then
1491 let s1 = "(press 'e' to review error messasges)" in
1492 if nonemptystr s then s ^ " " ^ s1 else s1
1493 else s
1495 else s
1497 if nonemptystr s
1498 then drawstring s
1501 let gctiles () =
1502 let len = Queue.length state.tilelru in
1503 let layout = lazy (
1504 match state.throttle with
1505 | None ->
1506 if conf.preload
1507 then preloadlayout state.y
1508 else state.layout
1509 | Some (layout, _, _) ->
1510 layout
1511 ) in
1512 let rec loop qpos =
1513 if state.memused <= conf.memlimit
1514 then ()
1515 else (
1516 if qpos < len
1517 then
1518 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1519 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1520 let (_, pw, ph, _) = getpagedim n in
1522 gen = state.gen
1523 && colorspace = conf.colorspace
1524 && angle = conf.angle
1525 && pagew = pw
1526 && pageh = ph
1527 && (
1528 let x = col*conf.tilew
1529 and y = row*conf.tileh in
1530 tilevisible (Lazy.force_val layout) n x y
1532 then Queue.push lruitem state.tilelru
1533 else (
1534 freepbo p;
1535 wcmd "freetile %s" (~> p);
1536 state.memused <- state.memused - s;
1537 state.uioh#infochanged Memused;
1538 Hashtbl.remove state.tilemap k;
1540 loop (qpos+1)
1543 loop 0
1546 let onpagerect pageno f =
1547 let b =
1548 match conf.columns with
1549 | Cmulti (_, b) -> b
1550 | Csingle b -> b
1551 | Csplit (_, b) -> b
1553 if pageno >= 0 && pageno < Array.length b
1554 then
1555 let (_, _, _, (w, h, _, _)) = b.(pageno) in
1556 f w h
1559 let gotopagexy1 pageno x y =
1560 let _,w1,h1,leftx = getpagedim pageno in
1561 let top = y /. (float h1) in
1562 let left = x /. (float w1) in
1563 let py, w, h = getpageywh pageno in
1564 let wh = state.winh - hscrollh () in
1565 let x = left *. (float w) in
1566 let x = leftx + state.x + truncate x in
1567 let wadj = wadjsb () in
1568 let sx =
1569 if x < 0 || x >= wadj + state.winw
1570 then state.x - x
1571 else state.x
1573 let pdy = truncate (top *. float h) in
1574 let y' = py + pdy in
1575 let dy = y' - state.y in
1576 let sy =
1577 if x != state.x || not (dy > 0 && dy < wh)
1578 then (
1579 if conf.presentation
1580 then
1581 if abs (py - y') > wh
1582 then y'
1583 else py
1584 else y';
1586 else state.y
1588 if state.x != sx || state.y != sy
1589 then (
1590 let x, y =
1591 if !wtmode
1592 then (
1593 let ww = wadj + state.winw in
1594 let qx = sx / ww
1595 and qy = pdy / wh in
1596 let x = qx * ww
1597 and y = py + qy * wh in
1598 let x = if -x + ww > w1 then -(w1-ww) else x
1599 and y' = if y + wh > state.maxy then state.maxy - wh else y in
1600 let y =
1601 if conf.presentation
1602 then
1603 if abs (py - y') > wh
1604 then y'
1605 else py
1606 else y';
1608 (x, y)
1610 else (sx, sy)
1612 state.x <- x;
1613 gotoy_and_clear_text y;
1615 else gotoy_and_clear_text state.y;
1618 let gotopagexy pageno x y =
1619 match state.mode with
1620 | Birdseye _ -> gotopage pageno 0.0
1621 | Textentry _
1622 | View
1623 | LinkNav _ -> gotopagexy1 pageno x y
1626 let getpassword () =
1627 let passcmd = getenvwithdef "LLPP_ASKPASS" conf.passcmd in
1628 if emptystr passcmd
1629 then E.s
1630 else
1631 match Unix.open_process_in passcmd with
1632 | (exception exn) ->
1633 showtext '!'
1634 (Printf.sprintf
1635 "getpassword: open_process_in failed: %s" (exntos exn));
1637 | ic ->
1638 let s = try input_line ic with End_of_file -> E.s in
1639 let s =
1640 match Unix.close_process_in ic with
1641 | (exception exn) ->
1642 showtext '!'
1643 (Printf.sprintf "getpassword: close_process_in failed: %s"
1644 (exntos exn));
1646 | _ -> s
1651 let act cmds =
1652 (* dolog "%S" cmds; *)
1653 let cl = splitatspace cmds in
1654 let scan s fmt f =
1655 try Scanf.sscanf s fmt f
1656 with exn ->
1657 dolog "error processing '%S': %s" cmds (exntos exn);
1658 exit 1
1660 let addoutline outline =
1661 match state.currently with
1662 | Outlining outlines ->
1663 state.currently <- Outlining (outline :: outlines)
1664 | Idle -> state.currently <- Outlining [outline]
1665 | Loading _
1666 | Tiling _ ->
1667 dolog "invalid outlining state";
1668 logcurrently state.currently
1670 match cl with
1671 | "clear" :: [] ->
1672 state.uioh#infochanged Pdim;
1673 state.pdims <- [];
1675 | "clearrects" :: [] ->
1676 state.rects <- state.rects1;
1677 G.postRedisplay "clearrects";
1679 | "continue" :: args :: [] ->
1680 let n = scan args "%u" (fun n -> n) in
1681 state.pagecount <- n;
1682 begin match state.currently with
1683 | Outlining l ->
1684 state.currently <- Idle;
1685 state.outlines <- Array.of_list (List.rev l)
1686 | Idle
1687 | Loading _
1688 | Tiling _ -> ()
1689 end;
1691 let cur, cmds = state.geomcmds in
1692 if emptystr cur
1693 then failwith "umpossible";
1695 begin match List.rev cmds with
1696 | [] ->
1697 state.geomcmds <- E.s, [];
1698 state.throttle <- None;
1699 represent ();
1700 | (s, f) :: rest ->
1701 f ();
1702 state.geomcmds <- s, List.rev rest;
1703 end;
1704 if conf.maxwait = None && not !wtmode
1705 then G.postRedisplay "continue";
1707 | "msg" :: args :: [] ->
1708 showtext ' ' args
1710 | "vmsg" :: args :: [] ->
1711 if conf.verbose
1712 then showtext ' ' args
1714 | "emsg" :: args :: [] ->
1715 Buffer.add_string state.errmsgs args;
1716 state.newerrmsgs <- true;
1717 G.postRedisplay "error message"
1719 | "progress" :: args :: [] ->
1720 let progress, text =
1721 scan args "%f %n"
1722 (fun f pos ->
1723 f, String.sub args pos (String.length args - pos))
1725 state.text <- text;
1726 state.progress <- progress;
1727 G.postRedisplay "progress"
1729 | "firstmatch" :: 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 let y = (getpagey pageno) + truncate y0 in
1741 addnav ();
1742 gotoy y;
1743 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
1745 | "match" :: args :: [] ->
1746 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1747 scan args "%u %d %f %f %f %f %f %f %f %f"
1748 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1749 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1751 let xoff = float (xadjsb ()) in
1752 let x0 = x0 +. xoff
1753 and x1 = x1 +. xoff
1754 and x2 = x2 +. xoff
1755 and x3 = x3 +. xoff in
1756 state.rects1 <-
1757 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1759 | "page" :: args :: [] ->
1760 let pageopaques, t = scan args "%s %f" (fun p t -> p, t) in
1761 let pageopaque = ~< pageopaques in
1762 begin match state.currently with
1763 | Loading (l, gen) ->
1764 vlog "page %d took %f sec" l.pageno t;
1765 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1766 begin match state.throttle with
1767 | None ->
1768 let preloadedpages =
1769 if conf.preload
1770 then preloadlayout state.y
1771 else state.layout
1773 let evict () =
1774 let set =
1775 List.fold_left (fun s l -> IntSet.add l.pageno s)
1776 IntSet.empty preloadedpages
1778 let evictedpages =
1779 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1780 if not (IntSet.mem pageno set)
1781 then (
1782 wcmd "freepage %s" (~> opaque);
1783 key :: accu
1785 else accu
1786 ) state.pagemap []
1788 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1790 evict ();
1791 state.currently <- Idle;
1792 if gen = state.gen
1793 then (
1794 tilepage l.pageno pageopaque state.layout;
1795 load state.layout;
1796 load preloadedpages;
1797 let visible = pagevisible state.layout l.pageno in
1798 if visible
1799 then (
1800 match state.mode with
1801 | LinkNav (Ltnotready (pageno, dir)) ->
1802 if pageno = l.pageno
1803 then (
1804 let link =
1805 let ld =
1806 if dir = 0
1807 then LDfirstvisible (l.pagex, l.pagey, dir)
1808 else (
1809 if dir > 0 then LDfirst else LDlast
1812 findlink pageopaque ld
1814 match link with
1815 | Lnotfound -> ()
1816 | Lfound n ->
1817 showlinktype (getlink pageopaque n);
1818 state.mode <- LinkNav (Ltexact (l.pageno, n))
1820 | LinkNav (Ltgendir _)
1821 | LinkNav (Ltexact _)
1822 | View
1823 | Birdseye _
1824 | Textentry _ -> ()
1827 if visible && layoutready state.layout
1828 then (
1829 G.postRedisplay "page";
1833 | Some (layout, _, _) ->
1834 state.currently <- Idle;
1835 tilepage l.pageno pageopaque layout;
1836 load state.layout
1837 end;
1839 | Idle
1840 | Tiling _
1841 | Outlining _ ->
1842 dolog "Inconsistent loading state";
1843 logcurrently state.currently;
1844 exit 1
1847 | "tile" :: args :: [] ->
1848 let (x, y, opaques, size, t) =
1849 scan args "%u %u %s %u %f"
1850 (fun x y p size t -> (x, y, p, size, t))
1852 let opaque = ~< opaques in
1853 begin match state.currently with
1854 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1855 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1857 unmappbo opaque;
1858 if tilew != conf.tilew || tileh != conf.tileh
1859 then (
1860 wcmd "freetile %s" (~> opaque);
1861 state.currently <- Idle;
1862 load state.layout;
1864 else (
1865 puttileopaque l col row gen cs angle opaque size t;
1866 state.memused <- state.memused + size;
1867 state.uioh#infochanged Memused;
1868 gctiles ();
1869 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1870 opaque, size) state.tilelru;
1872 let layout =
1873 match state.throttle with
1874 | None -> state.layout
1875 | Some (layout, _, _) -> layout
1878 state.currently <- Idle;
1879 if gen = state.gen
1880 && conf.colorspace = cs
1881 && conf.angle = angle
1882 && tilevisible layout l.pageno x y
1883 then conttiling l.pageno pageopaque;
1885 begin match state.throttle with
1886 | None ->
1887 preload state.layout;
1888 if gen = state.gen
1889 && conf.colorspace = cs
1890 && conf.angle = angle
1891 && tilevisible state.layout l.pageno x y
1892 && (not !wtmode || layoutready state.layout)
1893 then G.postRedisplay "tile nothrottle";
1895 | Some (layout, y, _) ->
1896 let ready = layoutready layout in
1897 if ready
1898 then (
1899 state.y <- y;
1900 state.layout <- layout;
1901 state.throttle <- None;
1902 G.postRedisplay "throttle";
1904 else load layout;
1905 end;
1908 | Idle
1909 | Loading _
1910 | Outlining _ ->
1911 dolog "Inconsistent tiling state";
1912 logcurrently state.currently;
1913 exit 1
1916 | "pdim" :: args :: [] ->
1917 let (n, w, h, _) as pdim =
1918 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1920 let pdim =
1921 match conf.fitmodel with
1922 | FitWidth -> pdim
1923 | FitPage | FitProportional ->
1924 match conf.columns with
1925 | Csplit _ -> (n, w, h, 0)
1926 | Csingle _ | Cmulti _ -> pdim
1928 state.uioh#infochanged Pdim;
1929 state.pdims <- pdim :: state.pdims
1931 | "o" :: args :: [] ->
1932 let (l, n, t, h, pos) =
1933 scan args "%u %u %d %u %n"
1934 (fun l n t h pos -> l, n, t, h, pos)
1936 let s = String.sub args pos (String.length args - pos) in
1937 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
1939 | "ou" :: args :: [] ->
1940 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
1941 let s = String.sub args pos len in
1942 let pos2 = pos + len + 1 in
1943 let uri = String.sub args pos2 (String.length args - pos2) in
1944 addoutline (s, l, Ouri uri)
1946 | "on" :: args :: [] ->
1947 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
1948 let s = String.sub args pos (String.length args - pos) in
1949 addoutline (s, l, Onone)
1951 | "a" :: args :: [] ->
1952 let (n, l, t) =
1953 scan args "%u %d %d" (fun n l t -> n, l, t)
1955 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
1957 | "info" :: args :: [] ->
1958 let pos = nindex args '\t' in
1959 if pos >= 0 && String.sub args 0 pos = "Title"
1960 then
1961 let s = String.sub args (pos+1) @@ String.length args - pos - 1 in
1962 conf.title <- s;
1963 Wsi.settitle s;
1964 state.docinfo <- (1, args) :: state.docinfo
1966 | "infoend" :: [] ->
1967 state.uioh#infochanged Docinfo;
1968 state.docinfo <- List.rev state.docinfo
1970 | "pass" :: l ->
1971 if l = "fail" :: []
1972 then Wsi.settitle "Wrong password";
1973 let password = getpassword () in
1974 if password == E.s
1975 then error "document is password protected"
1976 else opendoc state.path password
1978 | _ ->
1979 error "unknown cmd `%S'" cmds
1982 let onhist cb =
1983 let rc = cb.rc in
1984 let action = function
1985 | HCprev -> cbget cb ~-1
1986 | HCnext -> cbget cb 1
1987 | HCfirst -> cbget cb ~-(cb.rc)
1988 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1989 and cancel () = cb.rc <- rc
1990 in (action, cancel)
1993 let search pattern forward =
1994 match conf.columns with
1995 | Csplit _ ->
1996 showtext '!' "searching does not work properly in split columns mode"
1997 | Csingle _
1998 | Cmulti _ ->
1999 if nonemptystr pattern
2000 then
2001 let pn, py =
2002 match state.layout with
2003 | [] -> 0, 0
2004 | l :: _ ->
2005 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2007 wcmd "search %d %d %d %d,%s\000"
2008 (btod conf.icase) pn py (btod forward) pattern;
2011 let intentry text key =
2012 let c =
2013 if key >= 32 && key < 127
2014 then Char.chr key
2015 else '\000'
2017 match c with
2018 | '0' .. '9' ->
2019 let text = addchar text c in
2020 TEcont text
2022 | _ ->
2023 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2024 TEcont text
2027 let linknentry text key =
2028 let c =
2029 if key >= 32 && key < 127
2030 then Char.chr key
2031 else '\000'
2033 match c with
2034 | 'a' .. 'z' ->
2035 let text = addchar text c in
2036 TEcont text
2038 | _ ->
2039 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2040 TEcont text
2043 let linkndone f s =
2044 if nonemptystr s
2045 then (
2046 let n =
2047 let l = String.length s in
2048 let rec loop pos n = if pos = l then n else
2049 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2050 loop (pos+1) (n*26 + m)
2051 in loop 0 0
2053 let rec loop n = function
2054 | [] -> ()
2055 | l :: rest ->
2056 match getopaque l.pageno with
2057 | None -> loop n rest
2058 | Some opaque ->
2059 let m = getlinkcount opaque in
2060 if n < m
2061 then (
2062 let under = getlink opaque n in
2063 f under
2065 else loop (n-m) rest
2067 loop n state.layout;
2071 let textentry text key =
2072 if key land 0xff00 = 0xff00
2073 then TEcont text
2074 else TEcont (text ^ toutf8 key)
2077 let reqlayout angle fitmodel =
2078 match state.throttle with
2079 | None ->
2080 if nogeomcmds state.geomcmds
2081 then state.anchor <- getanchor ();
2082 conf.angle <- angle mod 360;
2083 if conf.angle != 0
2084 then (
2085 match state.mode with
2086 | LinkNav _ -> state.mode <- View
2087 | Birdseye _
2088 | Textentry _
2089 | View -> ()
2091 conf.fitmodel <- fitmodel;
2092 invalidate "reqlayout"
2093 (fun () ->
2094 wcmd "reqlayout %d %d %d"
2095 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
2097 | _ -> ()
2100 let settrim trimmargins trimfuzz =
2101 if nogeomcmds state.geomcmds
2102 then state.anchor <- getanchor ();
2103 conf.trimmargins <- trimmargins;
2104 conf.trimfuzz <- trimfuzz;
2105 let x0, y0, x1, y1 = trimfuzz in
2106 invalidate "settrim"
2107 (fun () ->
2108 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2109 flushpages ();
2112 let setzoom zoom =
2113 match state.throttle with
2114 | None ->
2115 let zoom = max 0.0001 zoom in
2116 if zoom <> conf.zoom
2117 then (
2118 state.prevzoom <- (conf.zoom, state.x);
2119 conf.zoom <- zoom;
2120 reshape state.winw state.winh;
2121 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2124 | Some (layout, y, started) ->
2125 let time =
2126 match conf.maxwait with
2127 | None -> 0.0
2128 | Some t -> t
2130 let dt = now () -. started in
2131 if dt > time
2132 then (
2133 state.y <- y;
2134 load layout;
2138 let setcolumns mode columns coverA coverB =
2139 state.prevcolumns <- Some (conf.columns, conf.zoom);
2140 if columns < 0
2141 then (
2142 if isbirdseye mode
2143 then showtext '!' "split mode doesn't work in bird's eye"
2144 else (
2145 conf.columns <- Csplit (-columns, E.a);
2146 state.x <- 0;
2147 conf.zoom <- 1.0;
2150 else (
2151 if columns < 2
2152 then (
2153 conf.columns <- Csingle E.a;
2154 state.x <- 0;
2155 setzoom 1.0;
2157 else (
2158 conf.columns <- Cmulti ((columns, coverA, coverB), E.a);
2159 conf.zoom <- 1.0;
2162 reshape state.winw state.winh;
2165 let resetmstate () =
2166 state.mstate <- Mnone;
2167 Wsi.setcursor Wsi.CURSOR_INHERIT;
2170 let enterbirdseye () =
2171 let zoom = float conf.thumbw /. float state.winw in
2172 let birdseyepageno =
2173 let cy = state.winh / 2 in
2174 let fold = function
2175 | [] -> 0
2176 | l :: rest ->
2177 let rec fold best = function
2178 | [] -> best.pageno
2179 | l :: rest ->
2180 let d = cy - (l.pagedispy + l.pagevh/2)
2181 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2182 if abs d < abs dbest
2183 then fold l rest
2184 else best.pageno
2185 in fold l rest
2187 fold state.layout
2189 state.mode <- Birdseye (
2190 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2192 resetmstate ();
2193 conf.zoom <- zoom;
2194 conf.presentation <- false;
2195 conf.interpagespace <- 10;
2196 conf.hlinks <- false;
2197 conf.fitmodel <- FitPage;
2198 state.x <- 0;
2199 conf.maxwait <- None;
2200 conf.columns <- (
2201 match conf.beyecolumns with
2202 | Some c ->
2203 conf.zoom <- 1.0;
2204 Cmulti ((c, 0, 0), E.a)
2205 | None -> Csingle E.a
2207 if conf.verbose
2208 then
2209 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2210 (100.0*.zoom)
2211 else
2212 state.text <- E.s
2214 reshape state.winw state.winh;
2217 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2218 state.mode <- View;
2219 conf.zoom <- c.zoom;
2220 conf.presentation <- c.presentation;
2221 conf.interpagespace <- c.interpagespace;
2222 conf.maxwait <- c.maxwait;
2223 conf.hlinks <- c.hlinks;
2224 conf.fitmodel <- c.fitmodel;
2225 conf.beyecolumns <- (
2226 match conf.columns with
2227 | Cmulti ((c, _, _), _) -> Some c
2228 | Csingle _ -> None
2229 | Csplit _ -> failwith "leaving bird's eye split mode"
2231 conf.columns <- (
2232 match c.columns with
2233 | Cmulti (c, _) -> Cmulti (c, E.a)
2234 | Csingle _ -> Csingle E.a
2235 | Csplit (c, _) -> Csplit (c, E.a)
2237 if conf.verbose
2238 then
2239 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2240 (100.0*.conf.zoom)
2242 reshape state.winw state.winh;
2243 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2244 state.x <- leftx;
2247 let togglebirdseye () =
2248 match state.mode with
2249 | Birdseye vals -> leavebirdseye vals true
2250 | View -> enterbirdseye ()
2251 | Textentry _
2252 | LinkNav _ -> ()
2255 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2256 let pageno = max 0 (pageno - incr) in
2257 let rec loop = function
2258 | [] -> gotopage1 pageno 0
2259 | l :: _ when l.pageno = pageno ->
2260 if l.pagedispy >= 0 && l.pagey = 0
2261 then G.postRedisplay "upbirdseye"
2262 else gotopage1 pageno 0
2263 | _ :: rest -> loop rest
2265 loop state.layout;
2266 state.text <- E.s;
2267 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2270 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2271 let pageno = min (state.pagecount - 1) (pageno + incr) in
2272 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2273 let rec loop = function
2274 | [] ->
2275 let y, h = getpageyh pageno in
2276 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2277 gotoy (clamp dy)
2278 | l :: _ when l.pageno = pageno ->
2279 if l.pagevh != l.pageh
2280 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2281 else G.postRedisplay "downbirdseye"
2282 | _ :: rest -> loop rest
2284 loop state.layout;
2285 state.text <- E.s;
2288 let optentry mode _ key =
2289 let btos b = if b then "on" else "off" in
2290 if key >= 32 && key < 127
2291 then
2292 let c = Char.chr key in
2293 match c with
2294 | 's' ->
2295 let ondone s =
2296 try conf.scrollstep <- int_of_string s with exc ->
2297 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2299 TEswitch ("scroll step: ", E.s, None, intentry, ondone, true)
2301 | 'A' ->
2302 let ondone s =
2304 conf.autoscrollstep <- boundastep state.winh (int_of_string s);
2305 if state.autoscroll <> None
2306 then state.autoscroll <- Some conf.autoscrollstep
2307 with exc ->
2308 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2310 TEswitch ("auto scroll step: ", E.s, None, intentry, ondone, true)
2312 | 'C' ->
2313 let ondone s =
2315 let n, a, b = multicolumns_of_string s in
2316 setcolumns mode n a b;
2317 with exc ->
2318 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
2320 TEswitch ("columns: ", E.s, None, textentry, ondone, true)
2322 | 'Z' ->
2323 let ondone s =
2325 let zoom = float (int_of_string s) /. 100.0 in
2326 setzoom zoom
2327 with exc ->
2328 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2330 TEswitch ("zoom: ", E.s, None, intentry, ondone, true)
2332 | 't' ->
2333 let ondone s =
2335 conf.thumbw <- bound (int_of_string s) 2 4096;
2336 state.text <-
2337 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2338 begin match mode with
2339 | Birdseye beye ->
2340 leavebirdseye beye false;
2341 enterbirdseye ();
2342 | Textentry _
2343 | View
2344 | LinkNav _ -> ();
2346 with exc ->
2347 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2349 TEswitch ("thumbnail width: ", E.s, None, intentry, ondone, true)
2351 | 'R' ->
2352 let ondone s =
2353 match try
2354 Some (int_of_string s)
2355 with exc ->
2356 state.text <- Printf.sprintf "bad integer `%s': %s"
2357 s (exntos exc);
2358 None
2359 with
2360 | Some angle -> reqlayout angle conf.fitmodel
2361 | None -> ()
2363 TEswitch ("rotation: ", E.s, None, intentry, ondone, true)
2365 | 'i' ->
2366 conf.icase <- not conf.icase;
2367 TEdone ("case insensitive search " ^ (btos conf.icase))
2369 | 'p' ->
2370 conf.preload <- not conf.preload;
2371 gotoy state.y;
2372 TEdone ("preload " ^ (btos conf.preload))
2374 | 'v' ->
2375 conf.verbose <- not conf.verbose;
2376 TEdone ("verbose " ^ (btos conf.verbose))
2378 | 'd' ->
2379 conf.debug <- not conf.debug;
2380 TEdone ("debug " ^ (btos conf.debug))
2382 | 'h' ->
2383 conf.maxhfit <- not conf.maxhfit;
2384 state.maxy <- calcheight ();
2385 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2387 | 'c' ->
2388 conf.crophack <- not conf.crophack;
2389 TEdone ("crophack " ^ btos conf.crophack)
2391 | 'a' ->
2392 let s =
2393 match conf.maxwait with
2394 | None ->
2395 conf.maxwait <- Some infinity;
2396 "always wait for page to complete"
2397 | Some _ ->
2398 conf.maxwait <- None;
2399 "show placeholder if page is not ready"
2401 TEdone s
2403 | 'f' ->
2404 conf.underinfo <- not conf.underinfo;
2405 TEdone ("underinfo " ^ btos conf.underinfo)
2407 | 'P' ->
2408 conf.savebmarks <- not conf.savebmarks;
2409 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2411 | 'S' ->
2412 let ondone s =
2414 let pageno, py =
2415 match state.layout with
2416 | [] -> 0, 0
2417 | l :: _ ->
2418 l.pageno, l.pagey
2420 conf.interpagespace <- int_of_string s;
2421 docolumns conf.columns;
2422 state.maxy <- calcheight ();
2423 let y = getpagey pageno in
2424 gotoy (y + py)
2425 with exc ->
2426 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2428 TEswitch ("vertical margin: ", E.s, None, intentry, ondone, true)
2430 | 'l' ->
2431 let fm =
2432 match conf.fitmodel with
2433 | FitProportional -> FitWidth
2434 | FitWidth | FitPage -> FitProportional
2436 reqlayout conf.angle fm;
2437 TEdone ("proportional display " ^ btos (fm == FitProportional))
2439 | 'T' ->
2440 settrim (not conf.trimmargins) conf.trimfuzz;
2441 TEdone ("trim margins " ^ btos conf.trimmargins)
2443 | 'I' ->
2444 conf.invert <- not conf.invert;
2445 TEdone ("invert colors " ^ btos conf.invert)
2447 | 'x' ->
2448 let ondone s =
2449 cbput state.hists.sel s;
2450 conf.selcmd <- s;
2452 TEswitch ("selection command: ", E.s, Some (onhist state.hists.sel),
2453 textentry, ondone, true)
2455 | 'M' ->
2456 if conf.pax == None
2457 then conf.pax <- Some (ref (0.0, 0, 0))
2458 else conf.pax <- None;
2459 TEdone ("PAX " ^ btos (conf.pax != None))
2461 | _ ->
2462 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2463 TEstop
2464 else
2465 TEcont state.text
2468 class type lvsource = object
2469 method getitemcount : int
2470 method getitem : int -> (string * int)
2471 method hasaction : int -> bool
2472 method exit :
2473 uioh:uioh ->
2474 cancel:bool ->
2475 active:int ->
2476 first:int ->
2477 pan:int ->
2478 uioh option
2479 method getactive : int
2480 method getfirst : int
2481 method getpan : int
2482 method getminfo : (int * int) array
2483 end;;
2485 class virtual lvsourcebase = object
2486 val mutable m_active = 0
2487 val mutable m_first = 0
2488 val mutable m_pan = 0
2489 method getactive = m_active
2490 method getfirst = m_first
2491 method getpan = m_pan
2492 method getminfo : (int * int) array = E.a
2493 end;;
2495 let textentrykeyboard
2496 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2497 let key =
2498 if key >= 0xffb0 && key <= 0xffb9
2499 then key - 0xffb0 + 48 else key
2501 let enttext te =
2502 state.mode <- Textentry (te, onleave);
2503 state.text <- E.s;
2504 enttext ();
2505 G.postRedisplay "textentrykeyboard enttext";
2507 let histaction cmd =
2508 match opthist with
2509 | None -> ()
2510 | Some (action, _) ->
2511 state.mode <- Textentry (
2512 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
2514 G.postRedisplay "textentry histaction"
2516 match key with
2517 | @backspace ->
2518 if emptystr text && cancelonempty
2519 then (
2520 onleave Cancel;
2521 G.postRedisplay "textentrykeyboard after cancel";
2523 else
2524 let s = withoutlastutf8 text in
2525 enttext (c, s, opthist, onkey, ondone, cancelonempty)
2527 | @enter | @kpenter ->
2528 ondone text;
2529 onleave Confirm;
2530 G.postRedisplay "textentrykeyboard after confirm"
2532 | @up | @kpup -> histaction HCprev
2533 | @down | @kpdown -> histaction HCnext
2534 | @home | @kphome -> histaction HCfirst
2535 | @jend | @kpend -> histaction HClast
2537 | @escape ->
2538 if emptystr text
2539 then (
2540 begin match opthist with
2541 | None -> ()
2542 | Some (_, onhistcancel) -> onhistcancel ()
2543 end;
2544 onleave Cancel;
2545 state.text <- E.s;
2546 G.postRedisplay "textentrykeyboard after cancel2"
2548 else (
2549 enttext (c, E.s, opthist, onkey, ondone, cancelonempty)
2552 | @delete | @kpdelete -> ()
2554 | _ when key != 0
2555 && key land 0xff00 != 0xff00 (* keyboard *)
2556 && key land 0xfe00 != 0xfe00 (* xkb *)
2557 && key land 0xfd00 != 0xfd00 (* 3270 *)
2559 begin match onkey text key with
2560 | TEdone text ->
2561 ondone text;
2562 onleave Confirm;
2563 G.postRedisplay "textentrykeyboard after confirm2";
2565 | TEcont text ->
2566 enttext (c, text, opthist, onkey, ondone, cancelonempty);
2568 | TEstop ->
2569 onleave Cancel;
2570 G.postRedisplay "textentrykeyboard after cancel3"
2572 | TEswitch te ->
2573 state.mode <- Textentry (te, onleave);
2574 G.postRedisplay "textentrykeyboard switch";
2575 end;
2577 | _ ->
2578 vlog "unhandled key %s" (Wsi.keyname key)
2581 let firstof first active =
2582 if first > active || abs (first - active) > fstate.maxrows - 1
2583 then max 0 (active - (fstate.maxrows/2))
2584 else first
2587 let calcfirst first active =
2588 if active > first
2589 then
2590 let rows = active - first in
2591 if rows > fstate.maxrows then active - fstate.maxrows else first
2592 else active
2595 let scrollph y maxy =
2596 let sh = float (maxy + state.winh) /. float state.winh in
2597 let sh = float state.winh /. sh in
2598 let sh = max sh (float conf.scrollh) in
2600 let percent = float y /. float maxy in
2601 let position = (float state.winh -. sh) *. percent in
2603 let position =
2604 if position +. sh > float state.winh
2605 then float state.winh -. sh
2606 else position
2608 position, sh;
2611 let coe s = (s :> uioh);;
2613 class listview ~zebra ~helpmode ~(source:lvsource) ~trusted ~modehash =
2614 object (self)
2615 val m_pan = source#getpan
2616 val m_first = source#getfirst
2617 val m_active = source#getactive
2618 val m_qsearch = E.s
2619 val m_prev_uioh = state.uioh
2621 method private elemunder y =
2622 if y < 0
2623 then None
2624 else
2625 let n = y / (fstate.fontsize+1) in
2626 if m_first + n < source#getitemcount
2627 then (
2628 if source#hasaction (m_first + n)
2629 then Some (m_first + n)
2630 else None
2632 else None
2634 method display =
2635 Gl.enable `blend;
2636 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
2637 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2638 filledrect 0. 0. (float state.winw) (float state.winh);
2639 GlDraw.color (1., 1., 1.);
2640 Gl.enable `texture_2d;
2641 let fs = fstate.fontsize in
2642 let nfs = fs + 1 in
2643 let hw = (wadjsb () + xadjsb () + state.winw)/3 in
2644 let ww = fstate.wwidth in
2645 let tabw = 17.0*.ww in
2646 let itemcount = source#getitemcount in
2647 let minfo = source#getminfo in
2648 let x0, x1 =
2649 if conf.leftscroll
2650 then float (xadjsb ()), float (state.winw - 1)
2651 else 0.0, float (state.winw - conf.scrollbw - 1)
2653 let xadj = xadjsb () in
2654 let rec loop row =
2655 if (row - m_first) > fstate.maxrows
2656 then ()
2657 else (
2658 if row >= 0 && row < itemcount
2659 then (
2660 let (s, level) = source#getitem row in
2661 let y = (row - m_first) * nfs in
2662 let x =
2663 (if conf.leftscroll then float xadj else 5.0)
2664 +. (float (level + m_pan)) *. ww in
2665 if helpmode
2666 then GlDraw.color
2667 (let c = if row land 1 = 0 then 1.0 else 0.92 in (c,c,c));
2669 if row = m_active
2670 then (
2671 Gl.disable `texture_2d;
2672 let alpha = if source#hasaction row then 0.9 else 0.3 in
2673 GlDraw.color (1., 1., 1.) ~alpha;
2674 linerect (x0 +. 1.) (float (y + 1)) (x1) (float (y + fs + 3));
2675 Gl.enable `texture_2d;
2677 let c =
2678 if zebra && row land 1 = 1
2679 then 0.8
2680 else 1.0
2682 GlDraw.color (c,c,c);
2683 let drawtabularstring s =
2684 let drawstr x s =
2685 let x' = truncate (x0 +. x) in
2686 let pos = nindex s '\000' in
2687 if pos = -1
2688 then drawstring1 fs x' (y+nfs) s
2689 else
2690 let s1 = String.sub s 0 pos
2691 and s2 = String.sub s (pos+1) (String.length s - pos - 1) in
2692 let rec e s =
2693 if emptystr s
2694 then s
2695 else
2696 let s' = withoutlastutf8 s in
2697 let s = s' ^ "@Uellipsis" in
2698 let w = measurestr fs s in
2699 if float x' +. w +. ww < float (hw + x')
2700 then s
2701 else e s'
2703 let s1 =
2704 if float x' +. ww +. measurestr fs s1 > float (hw + x')
2705 then e s1
2706 else s1
2708 ignore (drawstring1 fs x' (y+nfs) s1);
2709 drawstring1 fs (hw + x') (y+nfs) s2
2711 if trusted
2712 then
2713 let x = if helpmode && row > 0 then x +. ww else x in
2714 let tabpos = nindex s '\t' in
2715 if tabpos > 0
2716 then
2717 let len = String.length s - tabpos - 1 in
2718 let s1 = String.sub s 0 tabpos
2719 and s2 = String.sub s (tabpos + 1) len in
2720 let nx = drawstr x s1 in
2721 let sw = nx -. x in
2722 let x = x +. (max tabw sw) in
2723 drawstr x s2
2724 else
2725 let len = String.length s - 2 in
2726 if len > 0 && s.[0] = '\xc2' && s.[1] = '\xb7'
2727 then
2728 let s = String.sub s 2 len in
2729 let x = if not helpmode then x +. ww else x in
2730 GlDraw.color (1.2, 1.2, 1.2);
2731 let vinc = drawstring1 (fs+fs/4)
2732 (truncate (x -. ww)) (y+nfs) s in
2733 GlDraw.color (1., 1., 1.);
2734 vinc +. (float fs *. 0.8)
2735 else
2736 drawstr x s
2737 else
2738 drawstr x s
2740 ignore (drawtabularstring s);
2741 loop (row+1)
2745 loop m_first;
2746 GlDraw.color (1.0, 1.0, 1.0) ~alpha:0.5;
2747 let xadj = float (xadjsb () + 5) in
2748 let rec loop row =
2749 if (row - m_first) > fstate.maxrows
2750 then ()
2751 else (
2752 if row >= 0 && row < itemcount
2753 then (
2754 let (s, level) = source#getitem row in
2755 let pos0 = nindex s '\000' in
2756 let y = (row - m_first) * nfs in
2757 let x = float (level + m_pan) *. ww in
2758 let (first, last) = minfo.(row) in
2759 let prefix =
2760 if pos0 > 0 && first > pos0
2761 then String.sub s (pos0+1) (first-pos0-1)
2762 else String.sub s 0 first
2764 let suffix = String.sub s first (last - first) in
2765 let w1 = measurestr fstate.fontsize prefix in
2766 let w2 = measurestr fstate.fontsize suffix in
2767 let x = x +. if conf.leftscroll then xadj else 5.0 in
2768 let x = if pos0 > 0 && first > pos0 then x +. float hw else x in
2769 let x0 = x +. w1
2770 and y0 = float (y+2) in
2771 let x1 = x0 +. w2
2772 and y1 = float (y+fs+3) in
2773 filledrect x0 y0 x1 y1;
2774 loop (row+1)
2778 Gl.disable `texture_2d;
2779 if Array.length minfo > 0 then loop m_first;
2780 Gl.disable `blend;
2782 method updownlevel incr =
2783 let len = source#getitemcount in
2784 let curlevel =
2785 if m_active >= 0 && m_active < len
2786 then snd (source#getitem m_active)
2787 else -1
2789 let rec flow i =
2790 if i = len then i-1 else if i = -1 then 0 else
2791 let _, l = source#getitem i in
2792 if l != curlevel then i else flow (i+incr)
2794 let active = flow m_active in
2795 let first = calcfirst m_first active in
2796 G.postRedisplay "outline updownlevel";
2797 {< m_active = active; m_first = first >}
2799 method private key1 key mask =
2800 let set1 active first qsearch =
2801 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2803 let search active pattern incr =
2804 let active = if active = -1 then m_first else active in
2805 let dosearch re =
2806 let rec loop n =
2807 if n >= 0 && n < source#getitemcount
2808 then (
2809 let s, _ = source#getitem n in
2811 (try ignore (Str.search_forward re s 0); true
2812 with Not_found -> false)
2813 then Some n
2814 else loop (n + incr)
2816 else None
2818 loop active
2821 let re = Str.regexp_case_fold pattern in
2822 dosearch re
2823 with Failure s ->
2824 state.text <- s;
2825 None
2827 let itemcount = source#getitemcount in
2828 let find start incr =
2829 let rec find i =
2830 if i = -1 || i = itemcount
2831 then -1
2832 else (
2833 if source#hasaction i
2834 then i
2835 else find (i + incr)
2838 find start
2840 let set active first =
2841 let first = bound first 0 (itemcount - fstate.maxrows) in
2842 state.text <- E.s;
2843 coe {< m_active = active; m_first = first; m_qsearch = E.s >}
2845 let navigate incr =
2846 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2847 let active, first =
2848 let incr1 = if incr > 0 then 1 else -1 in
2849 if isvisible m_first m_active
2850 then
2851 let next =
2852 let next = m_active + incr in
2853 let next =
2854 if next < 0 || next >= itemcount
2855 then -1
2856 else find next incr1
2858 if abs (m_active - next) > fstate.maxrows
2859 then -1
2860 else next
2862 if next = -1
2863 then
2864 let first = m_first + incr in
2865 let first = bound first 0 (itemcount - fstate.maxrows) in
2866 let next =
2867 let next = m_active + incr in
2868 let next = bound next 0 (itemcount - 1) in
2869 find next ~-incr1
2871 let active =
2872 if next = -1
2873 then m_active
2874 else (
2875 if isvisible first next
2876 then next
2877 else m_active
2880 active, first
2881 else
2882 let first = min next m_first in
2883 let first =
2884 if abs (next - first) > fstate.maxrows
2885 then first + incr
2886 else first
2888 next, first
2889 else
2890 let first = m_first + incr in
2891 let first = bound first 0 (itemcount - 1) in
2892 let active =
2893 let next = m_active + incr in
2894 let next = bound next 0 (itemcount - 1) in
2895 let next = find next incr1 in
2896 let active =
2897 if next = -1 || abs (m_active - first) > fstate.maxrows
2898 then (
2899 let active = if m_active = -1 then next else m_active in
2900 active
2902 else next
2904 if isvisible first active
2905 then active
2906 else -1
2908 active, first
2910 G.postRedisplay "listview navigate";
2911 set active first;
2913 match key with
2914 | (@r|@s) when Wsi.withctrl mask ->
2915 let incr = if key = @r then -1 else 1 in
2916 let active, first =
2917 match search (m_active + incr) m_qsearch incr with
2918 | None ->
2919 state.text <- m_qsearch ^ " [not found]";
2920 m_active, m_first
2921 | Some active ->
2922 state.text <- m_qsearch;
2923 active, firstof m_first active
2925 G.postRedisplay "listview ctrl-r/s";
2926 set1 active first m_qsearch;
2928 | @insert when Wsi.withctrl mask ->
2929 if m_active >= 0 && m_active < source#getitemcount
2930 then (
2931 let s, _ = source#getitem m_active in
2932 selstring s;
2934 coe self
2936 | @backspace ->
2937 if emptystr m_qsearch
2938 then coe self
2939 else (
2940 let qsearch = withoutlastutf8 m_qsearch in
2941 if emptystr qsearch
2942 then (
2943 state.text <- E.s;
2944 G.postRedisplay "listview empty qsearch";
2945 set1 m_active m_first E.s;
2947 else
2948 let active, first =
2949 match search m_active qsearch ~-1 with
2950 | None ->
2951 state.text <- qsearch ^ " [not found]";
2952 m_active, m_first
2953 | Some active ->
2954 state.text <- qsearch;
2955 active, firstof m_first active
2957 G.postRedisplay "listview backspace qsearch";
2958 set1 active first qsearch
2961 | key when (key != 0 && key land 0xff00 != 0xff00) ->
2962 let pattern = m_qsearch ^ toutf8 key in
2963 let active, first =
2964 match search m_active pattern 1 with
2965 | None ->
2966 state.text <- pattern ^ " [not found]";
2967 m_active, m_first
2968 | Some active ->
2969 state.text <- pattern;
2970 active, firstof m_first active
2972 G.postRedisplay "listview qsearch add";
2973 set1 active first pattern;
2975 | @escape ->
2976 state.text <- E.s;
2977 if emptystr m_qsearch
2978 then (
2979 G.postRedisplay "list view escape";
2980 begin
2981 match
2982 source#exit ~uioh:(coe self)
2983 ~cancel:true ~active:m_active ~first:m_first ~pan:m_pan
2984 with
2985 | None -> m_prev_uioh
2986 | Some uioh -> uioh
2989 else (
2990 G.postRedisplay "list view kill qsearch";
2991 coe {< m_qsearch = E.s >}
2994 | @enter | @kpenter ->
2995 state.text <- E.s;
2996 let self = {< m_qsearch = E.s >} in
2997 let opt =
2998 G.postRedisplay "listview enter";
2999 if m_active >= 0 && m_active < source#getitemcount
3000 then (
3001 source#exit ~uioh:(coe self) ~cancel:false
3002 ~active:m_active ~first:m_first ~pan:m_pan;
3004 else (
3005 source#exit ~uioh:(coe self) ~cancel:true
3006 ~active:m_active ~first:m_first ~pan:m_pan;
3009 begin match opt with
3010 | None -> m_prev_uioh
3011 | Some uioh -> uioh
3014 | @delete | @kpdelete ->
3015 coe self
3017 | @up | @kpup -> navigate ~-1
3018 | @down | @kpdown -> navigate 1
3019 | @prior | @kpprior -> navigate ~-(fstate.maxrows)
3020 | @next | @kpnext -> navigate fstate.maxrows
3022 | @right | @kpright ->
3023 state.text <- E.s;
3024 G.postRedisplay "listview right";
3025 coe {< m_pan = m_pan - 1 >}
3027 | @left | @kpleft ->
3028 state.text <- E.s;
3029 G.postRedisplay "listview left";
3030 coe {< m_pan = m_pan + 1 >}
3032 | @home | @kphome ->
3033 let active = find 0 1 in
3034 G.postRedisplay "listview home";
3035 set active 0;
3037 | @jend | @kpend ->
3038 let first = max 0 (itemcount - fstate.maxrows) in
3039 let active = find (itemcount - 1) ~-1 in
3040 G.postRedisplay "listview end";
3041 set active first;
3043 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3044 coe self
3046 | _ ->
3047 dolog "listview unknown key %#x" key; coe self
3049 method key key mask =
3050 match state.mode with
3051 | Textentry te -> textentrykeyboard key mask te; coe self
3052 | Birdseye _
3053 | View
3054 | LinkNav _ -> self#key1 key mask
3056 method button button down x y _ =
3057 let opt =
3058 match button with
3059 | 1 when x > state.winw - conf.scrollbw ->
3060 G.postRedisplay "listview scroll";
3061 if down
3062 then
3063 let _, position, sh = self#scrollph in
3064 if y > truncate position && y < truncate (position +. sh)
3065 then (
3066 state.mstate <- Mscrolly;
3067 Some (coe self)
3069 else
3070 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3071 let first = truncate (s *. float source#getitemcount) in
3072 let first = min source#getitemcount first in
3073 Some (coe {< m_first = first; m_active = first >})
3074 else (
3075 state.mstate <- Mnone;
3076 Some (coe self);
3078 | 1 when down ->
3079 begin match self#elemunder y with
3080 | Some n ->
3081 G.postRedisplay "listview click";
3082 source#exit ~uioh:(coe {< m_active = n >})
3083 ~cancel:false ~active:n ~first:m_first ~pan:m_pan
3084 | _ ->
3085 Some (coe self)
3087 | n when (n == 4 || n == 5) && not down ->
3088 let len = source#getitemcount in
3089 let first =
3090 if n = 5 && m_first + fstate.maxrows >= len
3091 then
3092 m_first
3093 else
3094 let first = m_first + (if n == 4 then -1 else 1) in
3095 bound first 0 (len - 1)
3097 G.postRedisplay "listview wheel";
3098 Some (coe {< m_first = first >})
3099 | n when (n = 6 || n = 7) && not down ->
3100 let inc = if n = 7 then -1 else 1 in
3101 G.postRedisplay "listview hwheel";
3102 Some (coe {< m_pan = m_pan + inc >})
3103 | _ ->
3104 Some (coe self)
3106 match opt with
3107 | None -> m_prev_uioh
3108 | Some uioh -> uioh
3110 method multiclick _ x y = self#button 1 true x y
3112 method motion _ y =
3113 match state.mstate with
3114 | Mscrolly ->
3115 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3116 let first = truncate (s *. float source#getitemcount) in
3117 let first = min source#getitemcount first in
3118 G.postRedisplay "listview motion";
3119 coe {< m_first = first; m_active = first >}
3120 | Msel _
3121 | Mpan _
3122 | Mscrollx
3123 | Mzoom _
3124 | Mzoomrect _
3125 | Mnone -> coe self
3127 method pmotion x y =
3128 if x < state.winw - conf.scrollbw
3129 then
3130 let n =
3131 match self#elemunder y with
3132 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3133 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3135 let o =
3136 if n != m_active
3137 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3138 else self
3140 coe o
3141 else (
3142 Wsi.setcursor Wsi.CURSOR_INHERIT;
3143 coe self
3146 method infochanged _ = ()
3148 method scrollpw = (0, 0.0, 0.0)
3149 method scrollph =
3150 let nfs = fstate.fontsize + 1 in
3151 let y = m_first * nfs in
3152 let itemcount = source#getitemcount in
3153 let maxi = max 0 (itemcount - fstate.maxrows) in
3154 let maxy = maxi * nfs in
3155 let p, h = scrollph y maxy in
3156 conf.scrollbw, p, h
3158 method modehash = modehash
3159 method eformsgs = false
3160 method alwaysscrolly = true
3161 end;;
3163 class outlinelistview ~zebra ~source =
3164 let settext autonarrow s =
3165 if autonarrow
3166 then
3167 let ss = source#statestr in
3168 state.text <-
3169 if emptystr ss
3170 then "[" ^ s ^ "]"
3171 else "{" ^ ss ^ "} [" ^ s ^ "]"
3172 else state.text <- s
3174 object (self)
3175 inherit listview
3176 ~zebra
3177 ~helpmode:false
3178 ~source:(source :> lvsource)
3179 ~trusted:false
3180 ~modehash:(findkeyhash conf "outline")
3181 as super
3183 val m_autonarrow = false
3185 method! key key mask =
3186 let maxrows =
3187 if emptystr state.text
3188 then fstate.maxrows
3189 else fstate.maxrows - 2
3191 let calcfirst first active =
3192 if active > first
3193 then
3194 let rows = active - first in
3195 if rows > maxrows then active - maxrows else first
3196 else active
3198 let navigate incr =
3199 let active = m_active + incr in
3200 let active = bound active 0 (source#getitemcount - 1) in
3201 let first = calcfirst m_first active in
3202 G.postRedisplay "outline navigate";
3203 coe {< m_active = active; m_first = first >}
3205 let navscroll first =
3206 let active =
3207 let dist = m_active - first in
3208 if dist < 0
3209 then first
3210 else (
3211 if dist < maxrows
3212 then m_active
3213 else first + maxrows
3216 G.postRedisplay "outline navscroll";
3217 coe {< m_first = first; m_active = active >}
3219 let ctrl = Wsi.withctrl mask in
3220 match key with
3221 | @a when ctrl ->
3222 let text =
3223 if m_autonarrow
3224 then (source#denarrow; E.s)
3225 else (
3226 let pattern = source#renarrow in
3227 if nonemptystr m_qsearch
3228 then (source#narrow m_qsearch; m_qsearch)
3229 else pattern
3232 settext (not m_autonarrow) text;
3233 G.postRedisplay "toggle auto narrowing";
3234 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
3236 | @slash when emptystr m_qsearch && not m_autonarrow ->
3237 settext true E.s;
3238 G.postRedisplay "toggle auto narrowing";
3239 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
3241 | @n when ctrl ->
3242 source#narrow m_qsearch;
3243 if not m_autonarrow
3244 then source#add_narrow_pattern m_qsearch;
3245 G.postRedisplay "outline ctrl-n";
3246 coe {< m_first = 0; m_active = 0 >}
3248 | @S when ctrl ->
3249 let active = source#calcactive (getanchor ()) in
3250 let first = firstof m_first active in
3251 G.postRedisplay "outline ctrl-s";
3252 coe {< m_first = first; m_active = active >}
3254 | @u when ctrl ->
3255 G.postRedisplay "outline ctrl-u";
3256 if m_autonarrow && nonemptystr m_qsearch
3257 then (
3258 ignore (source#renarrow);
3259 settext m_autonarrow E.s;
3260 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3262 else (
3263 source#del_narrow_pattern;
3264 let pattern = source#renarrow in
3265 let text =
3266 if emptystr pattern then E.s else "Narrowed to " ^ pattern
3268 settext m_autonarrow text;
3269 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3272 | @l when ctrl ->
3273 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3274 G.postRedisplay "outline ctrl-l";
3275 coe {< m_first = first >}
3277 | @tab when m_autonarrow ->
3278 if nonemptystr m_qsearch
3279 then (
3280 G.postRedisplay "outline list view tab";
3281 source#add_narrow_pattern m_qsearch;
3282 settext true E.s;
3283 coe {< m_qsearch = E.s >}
3285 else coe self
3287 | @escape when m_autonarrow ->
3288 if nonemptystr m_qsearch
3289 then source#add_narrow_pattern m_qsearch;
3290 super#key key mask
3292 | @enter | @kpenter when m_autonarrow ->
3293 if nonemptystr m_qsearch
3294 then source#add_narrow_pattern m_qsearch;
3295 super#key key mask
3297 | key when m_autonarrow && (key != 0 && key land 0xff00 != 0xff00) ->
3298 let pattern = m_qsearch ^ toutf8 key in
3299 G.postRedisplay "outlinelistview autonarrow add";
3300 source#narrow pattern;
3301 settext true pattern;
3302 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3304 | key when m_autonarrow && key = @backspace ->
3305 if emptystr m_qsearch
3306 then coe self
3307 else
3308 let pattern = withoutlastutf8 m_qsearch in
3309 G.postRedisplay "outlinelistview autonarrow backspace";
3310 ignore (source#renarrow);
3311 source#narrow pattern;
3312 settext true pattern;
3313 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3315 | @delete | @kpdelete ->
3316 source#remove m_active;
3317 G.postRedisplay "outline delete";
3318 let active = max 0 (m_active-1) in
3319 coe {< m_first = firstof m_first active;
3320 m_active = active >}
3322 | @up | @kpup when ctrl ->
3323 navscroll (max 0 (m_first - 1))
3325 | @down | @kpdown when ctrl ->
3326 navscroll (min (source#getitemcount - 1) (m_first + 1))
3328 | @up | @kpup -> navigate ~-1
3329 | @down | @kpdown -> navigate 1
3330 | @prior | @kpprior -> navigate ~-(fstate.maxrows)
3331 | @next | @kpnext -> navigate fstate.maxrows
3333 | @right | @kpright ->
3334 let o =
3335 if ctrl
3336 then (
3337 G.postRedisplay "outline ctrl right";
3338 {< m_pan = m_pan + 1 >}
3340 else self#updownlevel 1
3342 coe o
3344 | @left | @kpleft ->
3345 let o =
3346 if ctrl
3347 then (
3348 G.postRedisplay "outline ctrl left";
3349 {< m_pan = m_pan - 1 >}
3351 else self#updownlevel ~-1
3353 coe o
3355 | @home | @kphome ->
3356 G.postRedisplay "outline home";
3357 coe {< m_first = 0; m_active = 0 >}
3359 | @jend | @kpend ->
3360 let active = source#getitemcount - 1 in
3361 let first = max 0 (active - fstate.maxrows) in
3362 G.postRedisplay "outline end";
3363 coe {< m_active = active; m_first = first >}
3365 | _ -> super#key key mask
3368 let genhistoutlines =
3369 let order ty (p1, c1, _, _, _) (p2, c2, _, _, _) =
3370 match ty with
3371 | `lastvisit -> compare c1.lastvisit c2.lastvisit
3372 | `path -> compare p2 p1
3373 | `file -> compare (Filename.basename p2) (Filename.basename p1)
3374 | `title ->
3375 let e1 = emptystr c1.title
3376 and e2 = emptystr c2.title in
3377 (**) if e1 && e2
3378 then compare (Filename.basename p2) (Filename.basename p1)
3379 else if e1 then -1
3380 else if e2 then 1
3381 else compare c1.title c2.title
3383 let showfullpath = ref false in
3384 fun orderty ->
3385 let setorty s t =
3386 let s = if orderty = t then "[@Uradical] " ^ s else "[ ] " ^ s in
3387 s, 0, Oaction (fun () -> Config.historder := t; reeenterhist := true)
3389 let list = ref [] in
3390 if Config.gethist list
3391 then
3392 let ol =
3393 List.fold_left
3394 (fun accu (path, c, b, x, a) ->
3395 let hist = (path, (c, b, x, a)) in
3396 let s = if !showfullpath then path else Filename.basename path in
3397 let base = mbtoutf8 s in
3398 (base ^ "\000" ^ c.title, 1, Ohistory hist) :: accu
3400 [ setorty "Sort by time of last visit" `lastvisit;
3401 setorty "Sort by file name" `file;
3402 setorty "Sort by path" `path;
3403 setorty "Sort by title" `title;
3404 (if !showfullpath then "@Uradical "
3405 else " ") ^ "Show full path", 0, Oaction (fun () ->
3406 showfullpath := not !showfullpath; reeenterhist := true)
3407 ] (List.sort (order orderty) !list)
3409 Array.of_list ol
3410 else E.a;
3413 let gotohist (path, (c, bookmarks, x, anchor)) =
3414 Config.save leavebirdseye;
3415 state.anchor <- anchor;
3416 state.x <- x;
3417 state.bookmarks <- bookmarks;
3418 state.origin <- E.s;
3419 setconf conf c;
3420 let x0, y0, x1, y1 = conf.trimfuzz in
3421 wcmd "trimset %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1;
3422 opendoc path E.s;
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);
4067 sep ();
4068 src#caption "Document" 0;
4069 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4070 src#caption2 "Pages"
4071 (fun () -> string_of_int state.pagecount) 1;
4072 src#caption2 "Dimensions"
4073 (fun () -> string_of_int (List.length state.pdims)) 1;
4074 if conf.trimmargins
4075 then (
4076 sep ();
4077 src#caption "Trimmed margins" 0;
4078 src#caption2 "Dimensions"
4079 (fun () -> string_of_int (List.length state.pdims)) 1;
4082 sep ();
4083 src#caption "OpenGL" 0;
4084 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4085 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4087 sep ();
4088 src#caption "Location" 0;
4089 if nonemptystr state.origin
4090 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
4091 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
4093 src#reset prevmode prevuioh;
4095 fun () ->
4096 state.text <- E.s;
4097 resetmstate ();
4098 let prevmode = state.mode
4099 and prevuioh = state.uioh in
4100 fillsrc prevmode prevuioh;
4101 let source = (src :> lvsource) in
4102 let modehash = findkeyhash conf "info" in
4103 state.uioh <- coe (object (self)
4104 inherit listview ~zebra:false ~helpmode:false
4105 ~source ~trusted:true ~modehash as super
4106 val mutable m_prevmemused = 0
4107 method! infochanged = function
4108 | Memused ->
4109 if m_prevmemused != state.memused
4110 then (
4111 m_prevmemused <- state.memused;
4112 G.postRedisplay "memusedchanged";
4114 | Pdim -> G.postRedisplay "pdimchanged"
4115 | Docinfo -> fillsrc prevmode prevuioh
4117 method! key key mask =
4118 if not (Wsi.withctrl mask)
4119 then
4120 match key with
4121 | @left | @kpleft -> coe (self#updownlevel ~-1)
4122 | @right | @kpright -> coe (self#updownlevel 1)
4123 | _ -> super#key key mask
4124 else super#key key mask
4125 end);
4126 G.postRedisplay "info";
4129 let enterhelpmode =
4130 let source =
4131 (object
4132 inherit lvsourcebase
4133 method getitemcount = Array.length state.help
4134 method getitem n =
4135 let s, l, _ = state.help.(n) in
4136 (s, l)
4138 method exit ~uioh ~cancel ~active ~first ~pan =
4139 let optuioh =
4140 if not cancel
4141 then (
4142 match state.help.(active) with
4143 | _, _, Action f -> Some (f uioh)
4144 | _, _, Noaction -> Some uioh
4146 else None
4148 m_active <- active;
4149 m_first <- first;
4150 m_pan <- pan;
4151 optuioh
4153 method hasaction n =
4154 match state.help.(n) with
4155 | _, _, Action _ -> true
4156 | _, _, Noaction -> false
4158 initializer
4159 m_active <- -1
4160 end)
4161 in fun () ->
4162 let modehash = findkeyhash conf "help" in
4163 resetmstate ();
4164 state.uioh <- coe (new listview
4165 ~zebra:false ~helpmode:true
4166 ~source ~trusted:true ~modehash);
4167 G.postRedisplay "help";
4170 let entermsgsmode =
4171 let msgsource =
4172 (object
4173 inherit lvsourcebase
4174 val mutable m_items = E.a
4176 method getitemcount = 1 + Array.length m_items
4178 method getitem n =
4179 if n = 0
4180 then "[Clear]", 0
4181 else m_items.(n-1), 0
4183 method exit ~uioh ~cancel ~active ~first ~pan =
4184 ignore uioh;
4185 if not cancel
4186 then (
4187 if active = 0
4188 then Buffer.clear state.errmsgs;
4190 m_active <- active;
4191 m_first <- first;
4192 m_pan <- pan;
4193 None
4195 method hasaction n =
4196 n = 0
4198 method reset =
4199 state.newerrmsgs <- false;
4200 let l = Str.split newlinere (Buffer.contents state.errmsgs) in
4201 m_items <- Array.of_list l
4203 initializer
4204 m_active <- 0
4205 end)
4206 in fun () ->
4207 state.text <- E.s;
4208 resetmstate ();
4209 msgsource#reset;
4210 let source = (msgsource :> lvsource) in
4211 let modehash = findkeyhash conf "listview" in
4212 state.uioh <- coe (object
4213 inherit listview ~zebra:false ~helpmode:false
4214 ~source ~trusted:false ~modehash as super
4215 method! display =
4216 if state.newerrmsgs
4217 then msgsource#reset;
4218 super#display
4219 end);
4220 G.postRedisplay "msgs";
4223 let getusertext s =
4224 let editor = getenvwithdef "EDITOR" E.s in
4225 if emptystr editor
4226 then E.s
4227 else
4228 let tmppath = Filename.temp_file "llpp" "note" in
4229 if not (emptystr s)
4230 then (
4231 let oc = open_out tmppath in
4232 output_string oc s;
4233 close_out oc;
4235 let execstr = editor ^ " " ^ tmppath in
4236 let s =
4237 match Unix.system execstr with
4238 | (exception exn) ->
4239 showtext '!' @@
4240 Printf.sprintf "Unix.system(%S) failed: %s" execstr (exntos exn);
4242 | Unix.WEXITED 0 -> filelines tmppath
4243 | Unix.WEXITED n ->
4244 showtext '!' @@
4245 Printf.sprintf "editor process(%s) exited abnormally: %d"
4246 execstr n;
4248 | Unix.WSIGNALED n ->
4249 showtext '!' @@
4250 Printf.sprintf "editor process(%s) was killed by signal %d"
4251 execstr n;
4253 | Unix.WSTOPPED n ->
4254 showtext '!' @@
4255 Printf.sprintf "editor(%s) process was stopped by signal %d"
4256 execstr n;
4259 match Unix.unlink tmppath with
4260 | (exception exn) ->
4261 showtext '!' @@
4262 Printf.sprintf "failed to ulink %S: %s"
4263 tmppath (exntos exn);
4265 | () -> s
4268 let enterannotmode opaque slinkindex =
4269 let msgsource =
4270 (object
4271 inherit lvsourcebase
4272 val mutable m_text = E.s
4273 val mutable m_items = E.a
4275 method getitemcount = Array.length m_items
4277 method getitem n =
4278 let label, _func = m_items.(n) in
4279 label, 0
4281 method exit ~uioh ~cancel ~active ~first ~pan =
4282 ignore (uioh, first, pan);
4283 if not cancel
4284 then (
4285 let _label, func = m_items.(active) in
4286 func ()
4288 None
4290 method hasaction n = not @@ emptystr @@ fst m_items.(n)
4292 method reset s =
4293 let rec split accu b i =
4294 let p = b+i in
4295 if p = String.length s
4296 then (String.sub s b (p-b), unit) :: accu
4297 else
4298 if (i > 70 && s.[p] = ' ') || s.[p] = '\r' || s.[p] = '\n'
4299 then
4300 let ss = if i = 0 then E.s else String.sub s b i in
4301 split ((ss, unit)::accu) (p+1) 0
4302 else
4303 split accu b (i+1)
4305 let cleanup () =
4306 wcmd "freepage %s" (~> opaque);
4307 let keys =
4308 Hashtbl.fold (fun key opaque' accu ->
4309 if opaque' = opaque'
4310 then key :: accu else accu) state.pagemap []
4312 List.iter (Hashtbl.remove state.pagemap) keys;
4313 flushtiles ();
4314 gotoy state.y
4316 let dele () =
4317 delannot opaque slinkindex;
4318 cleanup ();
4320 let edit inline () =
4321 let update s =
4322 if emptystr s
4323 then dele ()
4324 else (
4325 modannot opaque slinkindex s;
4326 cleanup ();
4329 if inline
4330 then
4331 let mode = state.mode in
4332 state.mode <-
4333 Textentry (
4334 ("annotation: ", m_text, None, textentry, update, true),
4335 fun _ -> state.mode <- mode);
4336 state.text <- E.s;
4337 enttext ();
4338 else
4339 let s = getusertext m_text in
4340 update s
4342 m_text <- s;
4343 m_items <-
4344 ( "[Copy]", fun () -> selstring m_text)
4345 :: ("[Delete]", dele)
4346 :: ("[Edit]", edit true)
4347 :: (E.s, unit)
4348 :: split [] 0 0 |> List.rev |> Array.of_list
4350 initializer
4351 m_active <- 0
4352 end)
4354 state.text <- E.s;
4355 let s = getannotcontents opaque slinkindex in
4356 resetmstate ();
4357 msgsource#reset s;
4358 let source = (msgsource :> lvsource) in
4359 let modehash = findkeyhash conf "listview" in
4360 state.uioh <- coe (object
4361 inherit listview ~zebra:false ~helpmode:false
4362 ~source ~trusted:false ~modehash
4363 end);
4364 G.postRedisplay "enterannotmode";
4367 let gotounder under =
4368 let getpath filename =
4369 let path =
4370 if nonemptystr filename
4371 then
4372 if Filename.is_relative filename
4373 then
4374 let dir = Filename.dirname state.path in
4375 let dir =
4376 if Filename.is_implicit dir
4377 then Filename.concat (Sys.getcwd ()) dir
4378 else dir
4380 Filename.concat dir filename
4381 else filename
4382 else E.s
4384 if Sys.file_exists path
4385 then path
4386 else E.s
4388 match under with
4389 | Ulinkgoto (pageno, top) ->
4390 if pageno >= 0
4391 then (
4392 addnav ();
4393 gotopage1 pageno top;
4396 | Ulinkuri s ->
4397 addpid @@ gotouri s
4399 | Uremote (filename, pageno) ->
4400 let path = getpath filename in
4401 if nonemptystr path
4402 then (
4403 if conf.riani
4404 then
4405 let command = Printf.sprintf "%s -page %d %S" !selfexec pageno path in
4406 try addpid @@ popen command []
4407 with exn ->
4408 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
4409 flush stderr;
4410 else
4411 let anchor = getanchor () in
4412 let ranchor = state.path, state.password, anchor, state.origin in
4413 state.origin <- E.s;
4414 state.anchor <- (pageno, 0.0, 0.0);
4415 state.ranchors <- ranchor :: state.ranchors;
4416 opendoc path E.s;
4418 else showtext '!' ("Could not find " ^ filename)
4420 | Uremotedest (filename, destname) ->
4421 let path = getpath filename in
4422 if nonemptystr path
4423 then (
4424 if conf.riani
4425 then
4426 let command = !selfexec ^ " " ^ path ^ " -dest " ^ destname in
4427 try addpid @@ popen command []
4428 with exn ->
4429 Printf.eprintf
4430 "failed to execute `%s': %s\n" command (exntos exn);
4431 flush stderr;
4432 else
4433 let anchor = getanchor () in
4434 let ranchor = state.path, state.password, anchor, state.origin in
4435 state.origin <- E.s;
4436 state.nameddest <- destname;
4437 state.ranchors <- ranchor :: state.ranchors;
4438 opendoc path E.s;
4440 else showtext '!' ("Could not find " ^ filename)
4442 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4443 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
4446 let gotooutline (_, _, kind) =
4447 match kind with
4448 | Onone -> ()
4449 | Oanchor anchor ->
4450 let (pageno, y, _) = anchor in
4451 let y = getanchory
4452 (if conf.presentation then (pageno, y, 1.0) else anchor)
4454 addnav ();
4455 gotoghyll y
4456 | Ouri uri -> gotounder (Ulinkuri uri)
4457 | Olaunch cmd -> gotounder (Ulaunch cmd)
4458 | Oremote remote -> gotounder (Uremote remote)
4459 | Ohistory hist -> gotohist hist
4460 | Oremotedest remotedest -> gotounder (Uremotedest remotedest)
4461 | Oaction f -> f ()
4464 let outlinesource sourcetype =
4465 (object (self)
4466 inherit lvsourcebase
4467 val mutable m_items = E.a
4468 val mutable m_minfo = E.a
4469 val mutable m_orig_items = E.a
4470 val mutable m_orig_minfo = E.a
4471 val mutable m_narrow_patterns = []
4472 val mutable m_hadremovals = false
4473 val mutable m_gen = -1
4475 method getitemcount =
4476 Array.length m_items + (if m_hadremovals then 1 else 0)
4478 method getitem n =
4479 if n == Array.length m_items && m_hadremovals
4480 then
4481 ("[Confirm removal]", 0)
4482 else
4483 let s, n, _ = m_items.(n) in
4484 (s, n)
4486 method exit ~uioh ~cancel ~active ~first ~pan =
4487 ignore (uioh, first);
4488 let confrimremoval = m_hadremovals && active = Array.length m_items in
4489 let items, minfo =
4490 if m_narrow_patterns = []
4491 then m_orig_items, m_orig_minfo
4492 else m_items, m_minfo
4494 if not cancel
4495 then (
4496 if not confrimremoval
4497 then (
4498 gotooutline m_items.(active);
4499 m_items <- items;
4500 m_minfo <- minfo;
4502 else (
4503 state.bookmarks <- Array.to_list m_items;
4504 m_orig_items <- m_items;
4505 m_orig_minfo <- m_minfo;
4508 else (
4509 m_items <- items;
4510 m_minfo <- minfo;
4512 m_pan <- pan;
4513 None
4515 method hasaction _ = true
4517 method greetmsg =
4518 if Array.length m_items != Array.length m_orig_items
4519 then
4520 let s =
4521 match m_narrow_patterns with
4522 | one :: [] -> one
4523 | many -> String.concat "@Uellipsis" (List.rev many)
4525 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
4526 else E.s
4528 method statestr =
4529 match m_narrow_patterns with
4530 | [] -> E.s
4531 | one :: [] -> one
4532 | head :: _ -> "@Uellipsis" ^ head
4534 method narrow pattern =
4535 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
4536 match reopt with
4537 | None -> ()
4538 | Some re ->
4539 let rec loop accu minfo n =
4540 if n = -1
4541 then (
4542 m_items <- Array.of_list accu;
4543 m_minfo <- Array.of_list minfo;
4545 else
4546 let (s, _, t) as o = m_items.(n) in
4547 let accu, minfo =
4548 match t with
4549 | Oaction _ -> o :: accu, (0, 0) :: minfo
4550 | Onone | Oanchor _ | Ouri _ | Olaunch _
4551 | Oremote _ | Oremotedest _ | Ohistory _ ->
4552 let first =
4553 try Str.search_forward re s 0
4554 with Not_found -> -1
4556 if first >= 0
4557 then o :: accu, (first, Str.match_end ()) :: minfo
4558 else accu, minfo
4560 loop accu minfo (n-1)
4562 loop [] [] (Array.length m_items - 1)
4564 method! getminfo = m_minfo
4566 method denarrow =
4567 m_orig_items <- (
4568 match sourcetype with
4569 | `bookmarks -> Array.of_list state.bookmarks
4570 | `outlines -> state.outlines
4571 | `history -> genhistoutlines !Config.historder
4573 m_minfo <- m_orig_minfo;
4574 m_items <- m_orig_items
4576 method remove m =
4577 if sourcetype = `bookmarks
4578 then
4579 if m >= 0 && m < Array.length m_items
4580 then (
4581 m_hadremovals <- true;
4582 m_items <- Array.init (Array.length m_items - 1) (fun n ->
4583 let n = if n >= m then n+1 else n in
4584 m_items.(n)
4588 method add_narrow_pattern pattern =
4589 m_narrow_patterns <- pattern :: m_narrow_patterns
4591 method del_narrow_pattern =
4592 match m_narrow_patterns with
4593 | _ :: rest -> m_narrow_patterns <- rest
4594 | [] -> ()
4596 method renarrow =
4597 self#denarrow;
4598 match m_narrow_patterns with
4599 | pattern :: [] -> self#narrow pattern; pattern
4600 | list ->
4601 List.fold_left (fun accu pattern ->
4602 self#narrow pattern;
4603 pattern ^ "@Uellipsis" ^ accu) E.s list
4605 method calcactive anchor =
4606 let rely = getanchory anchor in
4607 let rec loop n best bestd =
4608 if n = Array.length m_items
4609 then best
4610 else
4611 let _, _, kind = m_items.(n) in
4612 match kind with
4613 | Oanchor anchor ->
4614 let orely = getanchory anchor in
4615 let d = abs (orely - rely) in
4616 if d < bestd
4617 then loop (n+1) n d
4618 else loop (n+1) best bestd
4619 | Onone | Oremote _ | Olaunch _
4620 | Oremotedest _ | Ouri _ | Ohistory _ | Oaction _ ->
4621 loop (n+1) best bestd
4623 loop 0 ~-1 max_int
4625 method reset anchor items =
4626 m_hadremovals <- false;
4627 if state.gen != m_gen
4628 then (
4629 m_orig_items <- items;
4630 m_items <- items;
4631 m_narrow_patterns <- [];
4632 m_minfo <- E.a;
4633 m_orig_minfo <- E.a;
4634 m_gen <- state.gen;
4636 else (
4637 if items != m_orig_items
4638 then (
4639 m_orig_items <- items;
4640 if m_narrow_patterns == []
4641 then m_items <- items;
4644 let active = self#calcactive anchor in
4645 m_active <- active;
4646 m_first <- firstof m_first active
4647 end)
4650 let enterselector sourcetype =
4651 resetmstate ();
4652 let source = outlinesource sourcetype in
4653 fun errmsg ->
4654 let outlines =
4655 match sourcetype with
4656 | `bookmarks -> Array.of_list state.bookmarks
4657 | `outlines -> state.outlines
4658 | `history -> genhistoutlines !Config.historder
4660 if Array.length outlines = 0
4661 then (
4662 showtext ' ' errmsg;
4664 else (
4665 state.text <- source#greetmsg;
4666 Wsi.setcursor Wsi.CURSOR_INHERIT;
4667 let anchor = getanchor () in
4668 source#reset anchor outlines;
4669 state.uioh <-
4670 coe (new outlinelistview ~zebra:(sourcetype=`history) ~source);
4671 G.postRedisplay "enter selector";
4675 let enteroutlinemode =
4676 let f = enterselector `outlines in
4677 fun () -> f "Document has no outline";
4680 let enterbookmarkmode =
4681 let f = enterselector `bookmarks in
4682 fun () -> f "Document has no bookmarks (yet)";
4685 let enterhistmode () = enterselector `history "No history (yet)";;
4687 let quickbookmark ?title () =
4688 match state.layout with
4689 | [] -> ()
4690 | l :: _ ->
4691 let title =
4692 match title with
4693 | None ->
4694 let tm = Unix.localtime (now ()) in
4695 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4696 (l.pageno+1)
4697 tm.Unix.tm_mday
4698 tm.Unix.tm_mon
4699 (tm.Unix.tm_year + 1900)
4700 tm.Unix.tm_hour
4701 tm.Unix.tm_min
4702 | Some title -> title
4704 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4707 let setautoscrollspeed step goingdown =
4708 let incr = max 1 ((abs step) / 2) in
4709 let incr = if goingdown then incr else -incr in
4710 let astep = boundastep state.winh (step + incr) in
4711 state.autoscroll <- Some astep;
4714 let canpan () =
4715 match conf.columns with
4716 | Csplit _ -> true
4717 | Csingle _ | Cmulti _ -> state.x != 0 || conf.zoom > 1.0
4720 let panbound x = bound x (-state.w) (wadjsb () + state.winw);;
4722 let existsinrow pageno (columns, coverA, coverB) p =
4723 let last = ((pageno - coverA) mod columns) + columns in
4724 let rec any = function
4725 | [] -> false
4726 | l :: rest ->
4727 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4728 then p l
4729 else (
4730 if not (p l)
4731 then (if l.pageno = last then false else any rest)
4732 else true
4735 any state.layout
4738 let nextpage () =
4739 match state.layout with
4740 | [] ->
4741 let pageno = page_of_y state.y in
4742 gotoghyll (getpagey (pageno+1))
4743 | l :: rest ->
4744 match conf.columns with
4745 | Csingle _ ->
4746 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4747 then
4748 let y = clamp (pgscale state.winh) in
4749 gotoghyll y
4750 else
4751 let pageno = min (l.pageno+1) (state.pagecount-1) in
4752 gotoghyll (getpagey pageno)
4753 | Cmulti ((c, _, _) as cl, _) ->
4754 if conf.presentation
4755 && (existsinrow l.pageno cl
4756 (fun l -> l.pageh > l.pagey + l.pagevh))
4757 then
4758 let y = clamp (pgscale state.winh) in
4759 gotoghyll y
4760 else
4761 let pageno = min (l.pageno+c) (state.pagecount-1) in
4762 gotoghyll (getpagey pageno)
4763 | Csplit (n, _) ->
4764 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4765 then
4766 let pagey, pageh = getpageyh l.pageno in
4767 let pagey = pagey + pageh * l.pagecol in
4768 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4769 gotoghyll (pagey + pageh + ips)
4772 let prevpage () =
4773 match state.layout with
4774 | [] ->
4775 let pageno = page_of_y state.y in
4776 gotoghyll (getpagey (pageno-1))
4777 | l :: _ ->
4778 match conf.columns with
4779 | Csingle _ ->
4780 if conf.presentation && l.pagey != 0
4781 then
4782 gotoghyll (clamp (pgscale ~-(state.winh)))
4783 else
4784 let pageno = max 0 (l.pageno-1) in
4785 gotoghyll (getpagey pageno)
4786 | Cmulti ((c, _, coverB) as cl, _) ->
4787 if conf.presentation &&
4788 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4789 then
4790 gotoghyll (clamp (pgscale ~-(state.winh)))
4791 else
4792 let decr =
4793 if l.pageno = state.pagecount - coverB
4794 then 1
4795 else c
4797 let pageno = max 0 (l.pageno-decr) in
4798 gotoghyll (getpagey pageno)
4799 | Csplit (n, _) ->
4800 let y =
4801 if l.pagecol = 0
4802 then
4803 if l.pageno = 0
4804 then l.pagey
4805 else
4806 let pageno = max 0 (l.pageno-1) in
4807 let pagey, pageh = getpageyh pageno in
4808 pagey + (n-1)*pageh
4809 else
4810 let pagey, pageh = getpageyh l.pageno in
4811 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4813 gotoghyll y
4816 let save () =
4817 if emptystr conf.savecmd
4818 then error "don't know where to save modified document"
4819 else
4820 let command = Str.global_replace percentsre state.path conf.savecmd in
4821 match Unix.open_process_in command with
4822 | (exception exn) ->
4823 showtext '!'
4824 (Printf.sprintf "savecmd open_process_in failed: %s"
4825 (exntos exn));
4826 | ic ->
4827 let path = try input_line ic with End_of_file -> E.s in
4828 let path =
4829 match Unix.close_process_in ic with
4830 | (exception exn) ->
4831 error "error obtaining save path: %s" (exntos exn)
4832 | _ -> path
4834 let tmp = path ^ ".tmp" in
4835 savedoc tmp;
4836 Unix.rename tmp path;
4839 let viewkeyboard key mask =
4840 let enttext te =
4841 let mode = state.mode in
4842 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4843 state.text <- E.s;
4844 enttext ();
4845 G.postRedisplay "view:enttext"
4847 let ctrl = Wsi.withctrl mask in
4848 let key =
4849 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
4851 match key with
4852 | @Q -> exit 0
4854 | @W ->
4855 if hasunsavedchanges ()
4856 then save ()
4858 | @insert ->
4859 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4860 then (
4861 state.mode <- LinkNav (Ltgendir 0);
4862 gotoy state.y;
4864 else showtext '!' "Keyboard link navigation does not work under rotation"
4866 | @escape | @q ->
4867 begin match state.mstate with
4868 | Mzoomrect _ ->
4869 resetmstate ();
4870 G.postRedisplay "kill rect";
4871 | Msel _
4872 | Mpan _
4873 | Mscrolly | Mscrollx
4874 | Mzoom _
4875 | Mnone ->
4876 begin match state.mode with
4877 | LinkNav _ ->
4878 state.mode <- View;
4879 G.postRedisplay "esc leave linknav"
4880 | Birdseye _
4881 | Textentry _
4882 | View ->
4883 match state.ranchors with
4884 | [] -> raise Quit
4885 | (path, password, anchor, origin) :: rest ->
4886 state.ranchors <- rest;
4887 state.anchor <- anchor;
4888 state.origin <- origin;
4889 state.nameddest <- E.s;
4890 opendoc path password
4891 end;
4892 end;
4894 | @backspace ->
4895 gotoghyll (getnav ~-1)
4897 | @o ->
4898 enteroutlinemode ()
4900 | @H ->
4901 enterhistmode ()
4903 | @u ->
4904 state.rects <- [];
4905 state.text <- E.s;
4906 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap;
4907 G.postRedisplay "dehighlight";
4909 | @slash | @question ->
4910 let ondone isforw s =
4911 cbput state.hists.pat s;
4912 state.searchpattern <- s;
4913 search s isforw
4915 let s = String.make 1 (Char.chr key) in
4916 enttext (s, E.s, Some (onhist state.hists.pat),
4917 textentry, ondone (key = @slash), true)
4919 | @plus | @kpplus | @equals when ctrl ->
4920 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4921 setzoom (conf.zoom +. incr)
4923 | @plus | @kpplus ->
4924 let ondone s =
4925 let n =
4926 try int_of_string s with exc ->
4927 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
4928 max_int
4930 if n != max_int
4931 then (
4932 conf.pagebias <- n;
4933 state.text <- "page bias is now " ^ string_of_int n;
4936 enttext ("page bias: ", E.s, None, intentry, ondone, true)
4938 | @minus | @kpminus when ctrl ->
4939 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4940 setzoom (max 0.01 (conf.zoom -. decr))
4942 | @minus | @kpminus ->
4943 let ondone msg = state.text <- msg in
4944 enttext (
4945 "option [acfhilpstvxACFPRSZTISM]: ", E.s, None,
4946 optentry state.mode, ondone, true
4949 | @0 when ctrl ->
4950 if conf.zoom = 1.0
4951 then (
4952 state.x <- 0;
4953 gotoy state.y
4955 else setzoom 1.0
4957 | (@1 | @2) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
4958 let cols =
4959 match conf.columns with
4960 | Csingle _ | Cmulti _ -> 1
4961 | Csplit (n, _) -> n
4963 let h = state.winh -
4964 conf.interpagespace lsl (if conf.presentation then 1 else 0)
4966 let zoom = zoomforh state.winw h (vscrollw ()) cols in
4967 if zoom > 0.0 && (key = 50 || zoom < 1.0)
4968 then setzoom zoom
4970 | @3 when ctrl ->
4971 let fm =
4972 match conf.fitmodel with
4973 | FitWidth -> FitProportional
4974 | FitProportional -> FitPage
4975 | FitPage -> FitWidth
4977 state.text <- "fit model: " ^ FMTE.to_string fm;
4978 reqlayout conf.angle fm
4980 | @F9 ->
4981 togglebirdseye ()
4983 | @9 when ctrl ->
4984 togglebirdseye ()
4986 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4987 when not ctrl -> (* 0..9 *)
4988 let ondone s =
4989 let n =
4990 try int_of_string s with exc ->
4991 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
4994 if n >= 0
4995 then (
4996 addnav ();
4997 cbput state.hists.pag (string_of_int n);
4998 gotopage1 (n + conf.pagebias - 1) 0;
5001 let pageentry text key =
5002 match Char.unsafe_chr key with
5003 | 'g' -> TEdone text
5004 | _ -> intentry text key
5006 let text = String.make 1 (Char.chr key) in
5007 enttext (":", text, Some (onhist state.hists.pag),
5008 pageentry, ondone, true)
5010 | @b ->
5011 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
5012 reshape state.winw state.winh;
5014 | @B ->
5015 state.bzoom <- not state.bzoom;
5016 state.rects <- [];
5017 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
5019 | @l ->
5020 conf.hlinks <- not conf.hlinks;
5021 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5022 G.postRedisplay "toggle highlightlinks";
5024 | @F ->
5025 state.glinks <- true;
5026 let mode = state.mode in
5027 state.mode <- Textentry (
5028 (":", E.s, None, linknentry, linkndone gotounder, false),
5029 (fun _ ->
5030 state.glinks <- false;
5031 state.mode <- mode)
5033 state.text <- E.s;
5034 G.postRedisplay "view:linkent(F)"
5036 | @y ->
5037 state.glinks <- true;
5038 let mode = state.mode in
5039 state.mode <- Textentry (
5041 ":", E.s, None, linknentry, linkndone (fun under ->
5042 selstring (undertext under);
5043 ), false
5045 fun _ ->
5046 state.glinks <- false;
5047 state.mode <- mode
5049 state.text <- E.s;
5050 G.postRedisplay "view:linkent"
5052 | @a ->
5053 begin match state.autoscroll with
5054 | Some step ->
5055 conf.autoscrollstep <- step;
5056 state.autoscroll <- None
5057 | None ->
5058 if conf.autoscrollstep = 0
5059 then state.autoscroll <- Some 1
5060 else state.autoscroll <- Some conf.autoscrollstep
5063 | @p when ctrl ->
5064 launchpath ()
5066 | @P ->
5067 setpresentationmode (not conf.presentation);
5068 showtext ' ' ("presentation mode " ^
5069 if conf.presentation then "on" else "off");
5071 | @f ->
5072 if List.mem Wsi.Fullscreen state.winstate
5073 then Wsi.reshape conf.cwinw conf.cwinh
5074 else Wsi.fullscreen ()
5076 | @p | @N ->
5077 search state.searchpattern false
5079 | @n | @F3 ->
5080 search state.searchpattern true
5082 | @t ->
5083 begin match state.layout with
5084 | [] -> ()
5085 | l :: _ ->
5086 gotoghyll (getpagey l.pageno)
5089 | @space ->
5090 nextpage ()
5092 | @delete | @kpdelete -> (* delete *)
5093 prevpage ()
5095 | @equals ->
5096 showtext ' ' (describe_location ());
5098 | @w ->
5099 begin match state.layout with
5100 | [] -> ()
5101 | l :: _ ->
5102 Wsi.reshape (l.pagew + vscrollw ()) l.pageh;
5103 G.postRedisplay "w"
5106 | @apos ->
5107 enterbookmarkmode ()
5109 | @h | @F1 ->
5110 enterhelpmode ()
5112 | @i ->
5113 enterinfomode ()
5115 | @e when Buffer.length state.errmsgs > 0 ->
5116 entermsgsmode ()
5118 | @m ->
5119 let ondone s =
5120 match state.layout with
5121 | l :: _ ->
5122 if nonemptystr s
5123 then
5124 state.bookmarks <-
5125 (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
5126 | _ -> ()
5128 enttext ("bookmark: ", E.s, None, textentry, ondone, true)
5130 | @tilde ->
5131 quickbookmark ();
5132 showtext ' ' "Quick bookmark added";
5134 | @z ->
5135 begin match state.layout with
5136 | l :: _ ->
5137 let rect = getpdimrect l.pagedimno in
5138 let w, h =
5139 if conf.crophack
5140 then
5141 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5142 truncate (1.2 *. (rect.(3) -. rect.(0))))
5143 else
5144 (truncate (rect.(1) -. rect.(0)),
5145 truncate (rect.(3) -. rect.(0)))
5147 let w = truncate ((float w)*.conf.zoom)
5148 and h = truncate ((float h)*.conf.zoom) in
5149 if w != 0 && h != 0
5150 then (
5151 state.anchor <- getanchor ();
5152 Wsi.reshape (w + vscrollw ()) (h + conf.interpagespace)
5154 G.postRedisplay "z";
5156 | [] -> ()
5159 | @x -> state.roam ()
5161 | @Lt | @Gt ->
5162 reqlayout (conf.angle +
5163 (if key = @Gt then 30 else -30)) conf.fitmodel
5165 | @Lb | @Rb ->
5166 conf.colorscale <-
5167 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5169 G.postRedisplay "brightness";
5171 | @c when state.mode = View ->
5172 if Wsi.withalt mask
5173 then (
5174 if conf.zoom > 1.0
5175 then
5176 let m = (wadjsb () + state.winw - state.w) / 2 in
5177 state.x <- m;
5178 gotoy_and_clear_text state.y
5180 else
5181 let (c, a, b), z =
5182 match state.prevcolumns with
5183 | None -> (1, 0, 0), 1.0
5184 | Some (columns, z) ->
5185 let cab =
5186 match columns with
5187 | Csplit (c, _) -> -c, 0, 0
5188 | Cmulti ((c, a, b), _) -> c, a, b
5189 | Csingle _ -> 1, 0, 0
5191 cab, z
5193 setcolumns View c a b;
5194 setzoom z
5196 | @down | @up when ctrl && Wsi.withshift mask ->
5197 let zoom, x = state.prevzoom in
5198 setzoom zoom;
5199 state.x <- x;
5201 | @k | @up | @kpup ->
5202 begin match state.autoscroll with
5203 | None ->
5204 begin match state.mode with
5205 | Birdseye beye -> upbirdseye 1 beye
5206 | Textentry _
5207 | View
5208 | LinkNav _ ->
5209 if ctrl
5210 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5211 else (
5212 if not (Wsi.withshift mask) && conf.presentation
5213 then prevpage ()
5214 else gotoghyll1 true (clamp (-conf.scrollstep))
5217 | Some n ->
5218 setautoscrollspeed n false
5221 | @j | @down | @kpdown ->
5222 begin match state.autoscroll with
5223 | None ->
5224 begin match state.mode with
5225 | Birdseye beye -> downbirdseye 1 beye
5226 | Textentry _
5227 | View
5228 | LinkNav _ ->
5229 if ctrl
5230 then gotoy_and_clear_text (clamp (state.winh/2))
5231 else (
5232 if not (Wsi.withshift mask) && conf.presentation
5233 then nextpage ()
5234 else gotoghyll1 true (clamp (conf.scrollstep))
5237 | Some n ->
5238 setautoscrollspeed n true
5241 | @left | @right | @kpleft | @kpright when not (Wsi.withalt mask) ->
5242 if canpan ()
5243 then
5244 let dx =
5245 if ctrl
5246 then state.winw / 2
5247 else conf.hscrollstep
5249 let dx = if key = @left || key = @kpleft then dx else -dx in
5250 state.x <- panbound (state.x + dx);
5251 gotoy_and_clear_text state.y
5252 else (
5253 state.text <- E.s;
5254 G.postRedisplay "left/right"
5257 | @prior | @kpprior ->
5258 let y =
5259 if ctrl
5260 then
5261 match state.layout with
5262 | [] -> state.y
5263 | l :: _ -> state.y - l.pagey
5264 else
5265 clamp (pgscale (-state.winh))
5267 gotoghyll y
5269 | @next | @kpnext ->
5270 let y =
5271 if ctrl
5272 then
5273 match List.rev state.layout with
5274 | [] -> state.y
5275 | l :: _ -> getpagey l.pageno
5276 else
5277 clamp (pgscale state.winh)
5279 gotoghyll y
5281 | @g | @home | @kphome ->
5282 addnav ();
5283 gotoghyll 0
5284 | @G | @jend | @kpend ->
5285 addnav ();
5286 gotoghyll (clamp state.maxy)
5288 | @right | @kpright when Wsi.withalt mask ->
5289 gotoghyll (getnav 1)
5290 | @left | @kpleft when Wsi.withalt mask ->
5291 gotoghyll (getnav ~-1)
5293 | @r ->
5294 reload ()
5296 | @v when conf.debug ->
5297 state.rects <- [];
5298 List.iter (fun l ->
5299 match getopaque l.pageno with
5300 | None -> ()
5301 | Some opaque ->
5302 let x0, y0, x1, y1 = pagebbox opaque in
5303 let a,b = float x0, float y0 in
5304 let c,d = float x1, float y0 in
5305 let e,f = float x1, float y1 in
5306 let h,j = float x0, float y1 in
5307 let rect = (a,b,c,d,e,f,h,j) in
5308 debugrect rect;
5309 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5310 ) state.layout;
5311 G.postRedisplay "v";
5313 | @pipe ->
5314 let mode = state.mode in
5315 let cmd = ref E.s in
5316 let onleave = function
5317 | Cancel -> state.mode <- mode
5318 | Confirm ->
5319 List.iter (fun l ->
5320 match getopaque l.pageno with
5321 | Some opaque -> pipesel opaque !cmd
5322 | None -> ()) state.layout;
5323 state.mode <- mode
5325 let ondone s =
5326 cbput state.hists.sel s;
5327 cmd := s
5329 let te =
5330 "| ", !cmd, Some (onhist state.hists.sel), textentry, ondone, true
5332 G.postRedisplay "|";
5333 state.mode <- Textentry (te, onleave);
5335 | _ ->
5336 vlog "huh? %s" (Wsi.keyname key)
5339 let linknavkeyboard key mask linknav =
5340 let getpage pageno =
5341 let rec loop = function
5342 | [] -> None
5343 | l :: _ when l.pageno = pageno -> Some l
5344 | _ :: rest -> loop rest
5345 in loop state.layout
5347 let doexact (pageno, n) =
5348 match getopaque pageno, getpage pageno with
5349 | Some opaque, Some l ->
5350 if key = @enter || key = @kpenter
5351 then
5352 let under = getlink opaque n in
5353 G.postRedisplay "link gotounder";
5354 gotounder under;
5355 state.mode <- View;
5356 else
5357 let opt, dir =
5358 match key with
5359 | @home ->
5360 Some (findlink opaque LDfirst), -1
5362 | @jend ->
5363 Some (findlink opaque LDlast), 1
5365 | @left ->
5366 Some (findlink opaque (LDleft n)), -1
5368 | @right ->
5369 Some (findlink opaque (LDright n)), 1
5371 | @up ->
5372 Some (findlink opaque (LDup n)), -1
5374 | @down ->
5375 Some (findlink opaque (LDdown n)), 1
5377 | _ -> None, 0
5379 let pwl l dir =
5380 begin match findpwl l.pageno dir with
5381 | Pwlnotfound -> ()
5382 | Pwl pageno ->
5383 let notfound dir =
5384 state.mode <- LinkNav (Ltgendir dir);
5385 let y, h = getpageyh pageno in
5386 let y =
5387 if dir < 0
5388 then y + h - state.winh
5389 else y
5391 gotoy y
5393 begin match getopaque pageno, getpage pageno with
5394 | Some opaque, Some _ ->
5395 let link =
5396 let ld = if dir > 0 then LDfirst else LDlast in
5397 findlink opaque ld
5399 begin match link with
5400 | Lfound m ->
5401 showlinktype (getlink opaque m);
5402 state.mode <- LinkNav (Ltexact (pageno, m));
5403 G.postRedisplay "linknav jpage";
5404 | Lnotfound -> notfound dir
5405 end;
5406 | _ -> notfound dir
5407 end;
5408 end;
5410 begin match opt with
5411 | Some Lnotfound -> pwl l dir;
5412 | Some (Lfound m) ->
5413 if m = n
5414 then pwl l dir
5415 else (
5416 let _, y0, _, y1 = getlinkrect opaque m in
5417 if y0 < l.pagey
5418 then gotopage1 l.pageno y0
5419 else (
5420 let d = fstate.fontsize + 1 in
5421 if y1 - l.pagey > l.pagevh - d
5422 then gotopage1 l.pageno (y1 - state.winh - hscrollh () + d)
5423 else G.postRedisplay "linknav";
5425 showlinktype (getlink opaque m);
5426 state.mode <- LinkNav (Ltexact (l.pageno, m));
5429 | None -> viewkeyboard key mask
5430 end;
5431 | _ -> viewkeyboard key mask
5433 if key = @insert
5434 then (
5435 state.mode <- View;
5436 G.postRedisplay "leave linknav"
5438 else
5439 match linknav with
5440 | Ltgendir _ | Ltnotready _ -> viewkeyboard key mask
5441 | Ltexact exact -> doexact exact
5444 let keyboard key mask =
5445 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5446 then wcmd "interrupt"
5447 else state.uioh <- state.uioh#key key mask
5450 let birdseyekeyboard key mask
5451 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5452 let incr =
5453 match conf.columns with
5454 | Csingle _ -> 1
5455 | Cmulti ((c, _, _), _) -> c
5456 | Csplit _ -> failwith "bird's eye split mode"
5458 let pgh layout = List.fold_left
5459 (fun m l -> max l.pageh m) state.winh layout in
5460 match key with
5461 | @l when Wsi.withctrl mask ->
5462 let y, h = getpageyh pageno in
5463 let top = (state.winh - h) / 2 in
5464 gotoy (max 0 (y - top))
5465 | @enter | @kpenter -> leavebirdseye beye false
5466 | @escape -> leavebirdseye beye true
5467 | @up -> upbirdseye incr beye
5468 | @down -> downbirdseye incr beye
5469 | @left -> upbirdseye 1 beye
5470 | @right -> downbirdseye 1 beye
5472 | @prior ->
5473 begin match state.layout with
5474 | l :: _ ->
5475 if l.pagey != 0
5476 then (
5477 state.mode <- Birdseye (
5478 oconf, leftx, l.pageno, hooverpageno, anchor
5480 gotopage1 l.pageno 0;
5482 else (
5483 let layout = layout (state.y-state.winh) (pgh state.layout) in
5484 match layout with
5485 | [] -> gotoy (clamp (-state.winh))
5486 | l :: _ ->
5487 state.mode <- Birdseye (
5488 oconf, leftx, l.pageno, hooverpageno, anchor
5490 gotopage1 l.pageno 0
5493 | [] -> gotoy (clamp (-state.winh))
5494 end;
5496 | @next ->
5497 begin match List.rev state.layout with
5498 | l :: _ ->
5499 let layout = layout (state.y + (pgh state.layout)) state.winh in
5500 begin match layout with
5501 | [] ->
5502 let incr = l.pageh - l.pagevh in
5503 if incr = 0
5504 then (
5505 state.mode <-
5506 Birdseye (
5507 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5509 G.postRedisplay "birdseye pagedown";
5511 else gotoy (clamp (incr + conf.interpagespace*2));
5513 | l :: _ ->
5514 state.mode <-
5515 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5516 gotopage1 l.pageno 0;
5519 | [] -> gotoy (clamp state.winh)
5520 end;
5522 | @home ->
5523 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5524 gotopage1 0 0
5526 | @jend ->
5527 let pageno = state.pagecount - 1 in
5528 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5529 if not (pagevisible state.layout pageno)
5530 then
5531 let h =
5532 match List.rev state.pdims with
5533 | [] -> state.winh
5534 | (_, _, h, _) :: _ -> h
5536 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5537 else G.postRedisplay "birdseye end";
5539 | _ -> viewkeyboard key mask
5542 let drawpage l =
5543 let color =
5544 match state.mode with
5545 | Textentry _ -> scalecolor 0.4
5546 | LinkNav _
5547 | View -> scalecolor 1.0
5548 | Birdseye (_, _, pageno, hooverpageno, _) ->
5549 if l.pageno = hooverpageno
5550 then scalecolor 0.9
5551 else (
5552 if l.pageno = pageno
5553 then (
5554 let c = scalecolor 1.0 in
5555 GlDraw.color c;
5556 GlDraw.line_width 3.0;
5557 let dispx = xadjsb () + l.pagedispx in
5558 linerect
5559 (float (dispx-1)) (float (l.pagedispy-1))
5560 (float (dispx+l.pagevw+1))
5561 (float (l.pagedispy+l.pagevh+1))
5563 GlDraw.line_width 1.0;
5566 else scalecolor 0.8
5569 drawtiles l color;
5572 let postdrawpage l linkindexbase =
5573 match getopaque l.pageno with
5574 | Some opaque ->
5575 if tileready l l.pagex l.pagey
5576 then
5577 let x = l.pagedispx - l.pagex + xadjsb ()
5578 and y = l.pagedispy - l.pagey in
5579 let hlmask =
5580 match conf.columns with
5581 | Csingle _ | Cmulti _ ->
5582 (if conf.hlinks then 1 else 0)
5583 + (if state.glinks
5584 && not (isbirdseye state.mode) then 2 else 0)
5585 | Csplit _ -> 0
5587 let s =
5588 match state.mode with
5589 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5590 | Textentry _
5591 | Birdseye _
5592 | View
5593 | LinkNav _ -> E.s
5595 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5596 else 0
5597 | _ -> 0
5600 let scrollindicator () =
5601 let sbw, ph, sh = state.uioh#scrollph in
5602 let sbh, pw, sw = state.uioh#scrollpw in
5604 let x0,x1,hx0 =
5605 if conf.leftscroll
5606 then (0, sbw, sbw)
5607 else ((state.winw - sbw), state.winw, 0)
5610 GlDraw.color (0.64, 0.64, 0.64);
5611 filledrect (float x0) 0. (float x1) (float state.winh);
5612 filledrect
5613 (float hx0) (float (state.winh - sbh))
5614 (float (hx0 + wadjsb () + state.winw)) (float state.winh)
5616 GlDraw.color (0.0, 0.0, 0.0);
5618 filledrect (float x0) ph (float x1) (ph +. sh);
5619 let pw = pw +. float hx0 in
5620 filledrect pw (float (state.winh - sbh)) (pw +. sw) (float state.winh);
5623 let showsel () =
5624 match state.mstate with
5625 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5628 | Msel ((x0, y0), (x1, y1)) ->
5629 let identify opaque l px py = Some (opaque, l.pageno, px, py) in
5630 let o0,n0,px0,py0 = onppundermouse identify x0 y0 (~< E.s, -1, 0, 0) in
5631 let _o1,n1,px1,py1 = onppundermouse identify x1 y1 (~< E.s, -1, 0, 0) in
5632 if n0 != -1 && n0 = n1 then seltext o0 (px0, py0, px1, py1);
5635 let showrects = function [] -> () | rects ->
5636 Gl.enable `blend;
5637 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5638 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5639 List.iter
5640 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5641 List.iter (fun l ->
5642 if l.pageno = pageno
5643 then (
5644 let dx = float (l.pagedispx - l.pagex) in
5645 let dy = float (l.pagedispy - l.pagey) in
5646 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5647 Raw.sets_float state.vraw ~pos:0
5648 [| x0+.dx; y0+.dy;
5649 x1+.dx; y1+.dy;
5650 x3+.dx; y3+.dy;
5651 x2+.dx; y2+.dy |];
5652 GlArray.vertex `two state.vraw;
5653 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
5655 ) state.layout
5656 ) rects
5658 Gl.disable `blend;
5661 let display () =
5662 GlClear.color (scalecolor2 conf.bgcolor);
5663 GlClear.clear [`color];
5664 List.iter drawpage state.layout;
5665 let rects =
5666 match state.mode with
5667 | LinkNav (Ltexact (pageno, linkno)) ->
5668 begin match getopaque pageno with
5669 | Some opaque ->
5670 let dx = xadjsb () in
5671 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5672 let x0 = x0 + dx and x1 = x1 + dx in
5673 (pageno, 5, (
5674 float x0, float y0,
5675 float x1, float y0,
5676 float x1, float y1,
5677 float x0, float y1)
5678 ) :: state.rects
5679 | None -> state.rects
5681 | LinkNav (Ltgendir _) | LinkNav (Ltnotready _)
5682 | Birdseye _
5683 | Textentry _
5684 | View -> state.rects
5686 showrects rects;
5687 let rec postloop linkindexbase = function
5688 | l :: rest ->
5689 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5690 postloop linkindexbase rest
5691 | [] -> ()
5693 showsel ();
5694 postloop 0 state.layout;
5695 state.uioh#display;
5696 begin match state.mstate with
5697 | Mzoomrect ((x0, y0), (x1, y1)) ->
5698 Gl.enable `blend;
5699 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5700 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5701 filledrect (float x0) (float y0) (float x1) (float y1);
5702 Gl.disable `blend;
5703 | Msel _
5704 | Mpan _
5705 | Mscrolly | Mscrollx
5706 | Mzoom _
5707 | Mnone -> ()
5708 end;
5709 enttext ();
5710 scrollindicator ();
5711 Wsi.swapb ();
5714 let zoomrect x y x1 y1 =
5715 let x0 = min x x1
5716 and x1 = max x x1
5717 and y0 = min y y1 in
5718 gotoy (state.y + y0);
5719 state.anchor <- getanchor ();
5720 let zoom = (float state.w) /. float (x1 - x0) in
5721 let margin =
5722 let simple () =
5723 let adjw = wadjsb () + state.winw in
5724 if state.w < adjw
5725 then (adjw - state.w) / 2
5726 else 0
5728 match conf.fitmodel with
5729 | FitWidth | FitProportional -> simple ()
5730 | FitPage ->
5731 match conf.columns with
5732 | Csplit _ ->
5733 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
5734 | Cmulti _ | Csingle _ -> simple ()
5736 state.x <- (state.x + margin) - x0;
5737 setzoom zoom;
5738 resetmstate ();
5741 let annot inline x y =
5742 match unproject x y with
5743 | Some (opaque, n, ux, uy) ->
5744 let add text =
5745 addannot opaque ux uy text;
5746 wcmd "freepage %s" (~> opaque);
5747 Hashtbl.remove state.pagemap (n, state.gen);
5748 flushtiles ();
5749 gotoy state.y
5751 if inline
5752 then
5753 let ondone s = add s in
5754 let mode = state.mode in
5755 state.mode <- Textentry (
5756 ("annotation: ", E.s, None, textentry, ondone, true),
5757 fun _ -> state.mode <- mode);
5758 state.text <- E.s;
5759 enttext ();
5760 G.postRedisplay "annot"
5761 else
5762 let text =
5763 let s = getusertext E.s in
5764 let l = Str.split newlinere s in
5765 String.concat " " l
5767 add text
5768 | _ -> ()
5771 let zoomblock x y =
5772 let g opaque l px py =
5773 match rectofblock opaque px py with
5774 | Some a ->
5775 let x0 = a.(0) -. 20. in
5776 let x1 = a.(1) +. 20. in
5777 let y0 = a.(2) -. 20. in
5778 let zoom = (float state.w) /. (x1 -. x0) in
5779 let pagey = getpagey l.pageno in
5780 gotoy_and_clear_text (pagey + truncate y0);
5781 state.anchor <- getanchor ();
5782 let margin = (state.w - l.pagew)/2 in
5783 state.x <- -truncate x0 - margin;
5784 setzoom zoom;
5785 None
5786 | None -> None
5788 match conf.columns with
5789 | Csplit _ ->
5790 showtext '!' "block zooming does not work properly in split columns mode"
5791 | Cmulti _ | Csingle _ -> onppundermouse g x y ()
5794 let scrollx x =
5795 let winw = wadjsb () + state.winw - 1 in
5796 let s = float x /. float winw in
5797 let destx = truncate (float (state.w + winw) *. s) in
5798 state.x <- winw - destx;
5799 gotoy_and_clear_text state.y;
5800 state.mstate <- Mscrollx;
5803 let scrolly y =
5804 let s = float y /. float state.winh in
5805 let desty = truncate (float (state.maxy - state.winh) *. s) in
5806 gotoy_and_clear_text desty;
5807 state.mstate <- Mscrolly;
5810 let viewmulticlick clicks x y mask =
5811 let g opaque l px py =
5812 let mark =
5813 match clicks with
5814 | 2 -> Mark_word
5815 | 3 -> Mark_line
5816 | 4 -> Mark_block
5817 | _ -> Mark_page
5819 if markunder opaque px py mark
5820 then (
5821 Some (fun () ->
5822 let dopipe cmd =
5823 match getopaque l.pageno with
5824 | None -> ()
5825 | Some opaque -> pipesel opaque cmd
5827 state.roam <- (fun () -> dopipe conf.paxcmd);
5828 if not (Wsi.withctrl mask) then dopipe conf.selcmd;
5831 else None
5833 G.postRedisplay "viewmulticlick";
5834 onppundermouse g x y (fun () -> showtext '!' "Nothing to select") ();
5837 let canselect () =
5838 match conf.columns with
5839 | Csplit _ -> false
5840 | Csingle _ | Cmulti _ -> conf.angle mod 360 = 0
5843 let viewmouse button down x y mask =
5844 match button with
5845 | n when (n == 4 || n == 5) && not down ->
5846 if Wsi.withctrl mask
5847 then (
5848 match state.mstate with
5849 | Mzoom (oldn, i) ->
5850 if oldn = n
5851 then (
5852 if i = 2
5853 then
5854 let incr =
5855 match n with
5856 | 5 ->
5857 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5858 | _ ->
5859 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5861 let zoom = conf.zoom -. incr in
5862 setzoom zoom;
5863 state.mstate <- Mzoom (n, 0);
5864 else
5865 state.mstate <- Mzoom (n, i+1);
5867 else state.mstate <- Mzoom (n, 0)
5869 | Msel _
5870 | Mpan _
5871 | Mscrolly | Mscrollx
5872 | Mzoomrect _
5873 | Mnone -> state.mstate <- Mzoom (n, 0)
5875 else (
5876 match state.autoscroll with
5877 | Some step -> setautoscrollspeed step (n=4)
5878 | None ->
5879 if conf.wheelbypage || conf.presentation
5880 then (
5881 if n = 4
5882 then prevpage ()
5883 else nextpage ()
5885 else
5886 let incr =
5887 if n = 4
5888 then -conf.scrollstep
5889 else conf.scrollstep
5891 let incr = incr * 2 in
5892 let y = clamp incr in
5893 gotoy_and_clear_text y
5896 | n when (n = 6 || n = 7) && not down && canpan () ->
5897 state.x <-
5898 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
5899 gotoy_and_clear_text state.y
5901 | 1 when Wsi.withshift mask ->
5902 state.mstate <- Mnone;
5903 if not down
5904 then (
5905 match unproject x y with
5906 | Some (_, pageno, ux, uy) ->
5907 let cmd = Printf.sprintf
5908 "%s %s %d %d %d"
5909 conf.stcmd state.path pageno ux uy
5911 addpid @@ popen cmd []
5912 | None -> ()
5915 | 1 when Wsi.withctrl mask ->
5916 if down
5917 then (
5918 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5919 state.mstate <- Mpan (x, y)
5921 else
5922 state.mstate <- Mnone
5924 | 3 ->
5925 if down
5926 then (
5927 if Wsi.withshift mask
5928 then (
5929 annot (not (Wsi.withctrl mask)) x y;
5930 G.postRedisplay "addannot"
5932 else
5933 let p = (x, y) in
5934 Wsi.setcursor Wsi.CURSOR_CYCLE;
5935 state.mstate <- Mzoomrect (p, p)
5937 else (
5938 match state.mstate with
5939 | Mzoomrect ((x0, y0), _) ->
5940 if abs (x-x0) > 10 && abs (y - y0) > 10
5941 then zoomrect x0 y0 x y
5942 else (
5943 resetmstate ();
5944 G.postRedisplay "kill accidental zoom rect";
5946 | Msel _
5947 | Mpan _
5948 | Mscrolly | Mscrollx
5949 | Mzoom _
5950 | Mnone ->
5951 resetmstate ()
5954 | 1 when x > state.winw - vscrollw () ->
5955 if down
5956 then
5957 let _, position, sh = state.uioh#scrollph in
5958 if y > truncate position && y < truncate (position +. sh)
5959 then state.mstate <- Mscrolly
5960 else scrolly y
5961 else
5962 state.mstate <- Mnone
5964 | 1 when y > state.winh - hscrollh () ->
5965 if down
5966 then
5967 let _, position, sw = state.uioh#scrollpw in
5968 if x > truncate position && x < truncate (position +. sw)
5969 then state.mstate <- Mscrollx
5970 else scrollx x
5971 else
5972 state.mstate <- Mnone
5974 | 1 when state.bzoom -> if not down then zoomblock x y
5976 | 1 ->
5977 let dest = if down then getunder x y else Unone in
5978 begin match dest with
5979 | Ulinkgoto _
5980 | Ulinkuri _
5981 | Uremote _ | Uremotedest _
5982 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5983 gotounder dest
5985 | Unone when down ->
5986 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5987 state.mstate <- Mpan (x, y);
5989 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
5991 | Unone | Utext _ ->
5992 if down
5993 then (
5994 if canselect ()
5995 then (
5996 state.mstate <- Msel ((x, y), (x, y));
5997 G.postRedisplay "mouse select";
6000 else (
6001 match state.mstate with
6002 | Mnone -> ()
6004 | Mzoom _ | Mscrollx | Mscrolly ->
6005 state.mstate <- Mnone
6007 | Mzoomrect ((x0, y0), _) ->
6008 zoomrect x0 y0 x y
6010 | Mpan _ ->
6011 Wsi.setcursor Wsi.CURSOR_INHERIT;
6012 state.mstate <- Mnone
6014 | Msel ((x0, y0), (x1, y1)) ->
6015 let rec loop = function
6016 | [] -> ()
6017 | l :: rest ->
6018 let inside =
6019 let a0 = l.pagedispy in
6020 let a1 = a0 + l.pagevh in
6021 let b0 = l.pagedispx in
6022 let b1 = b0 + l.pagevw in
6023 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
6024 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
6026 if inside
6027 then
6028 match getopaque l.pageno with
6029 | Some opaque ->
6030 let dosel cmd () =
6031 match Unix.pipe () with
6032 | exception exn ->
6033 showtext '!'
6034 (Printf.sprintf
6035 "can not create sel pipe: %s"
6036 (exntos exn));
6037 | (r, w) ->
6038 let clo what fd =
6039 Ne.clo fd (fun msg ->
6040 dolog "%s close failed: %s" what msg)
6042 let pid =
6043 try popen cmd [r, 0; w, -1]
6044 with exn ->
6045 dolog "can not execute %S: %s"
6046 cmd (exntos exn);
6049 addpid pid;
6050 if pid > 0
6051 then (
6052 copysel w opaque;
6053 G.postRedisplay "copysel";
6055 else clo "Msel pipe/w" w;
6056 clo "Msel pipe/r" r;
6058 dosel conf.selcmd ();
6059 state.roam <- dosel conf.paxcmd;
6060 | None -> ()
6061 else loop rest
6063 loop state.layout;
6064 resetmstate ();
6068 | _ -> ()
6071 let birdseyemouse button down x y mask
6072 (conf, leftx, _, hooverpageno, anchor) =
6073 match button with
6074 | 1 when down ->
6075 let rec loop = function
6076 | [] -> ()
6077 | l :: rest ->
6078 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6079 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6080 then (
6081 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
6083 else loop rest
6085 loop state.layout
6086 | 3 -> ()
6087 | _ -> viewmouse button down x y mask
6090 let uioh = object
6091 method display = ()
6093 method key key mask =
6094 begin match state.mode with
6095 | Textentry textentry -> textentrykeyboard key mask textentry
6096 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6097 | View -> viewkeyboard key mask
6098 | LinkNav linknav -> linknavkeyboard key mask linknav
6099 end;
6100 state.uioh
6102 method button button bstate x y mask =
6103 begin match state.mode with
6104 | LinkNav _
6105 | View -> viewmouse button bstate x y mask
6106 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6107 | Textentry _ -> ()
6108 end;
6109 state.uioh
6111 method multiclick clicks x y mask =
6112 begin match state.mode with
6113 | LinkNav _
6114 | View -> viewmulticlick clicks x y mask
6115 | Birdseye _
6116 | Textentry _ -> ()
6117 end;
6118 state.uioh
6120 method motion x y =
6121 begin match state.mode with
6122 | Textentry _ -> ()
6123 | View | Birdseye _ | LinkNav _ ->
6124 match state.mstate with
6125 | Mzoom _ | Mnone -> ()
6127 | Mpan (x0, y0) ->
6128 let dx = x - x0
6129 and dy = y0 - y in
6130 state.mstate <- Mpan (x, y);
6131 if canpan ()
6132 then state.x <- panbound (state.x + dx);
6133 let y = clamp dy in
6134 gotoy_and_clear_text y
6136 | Msel (a, _) ->
6137 state.mstate <- Msel (a, (x, y));
6138 G.postRedisplay "motion select";
6140 | Mscrolly ->
6141 let y = min state.winh (max 0 y) in
6142 scrolly y
6144 | Mscrollx ->
6145 let x = min state.winw (max 0 x) in
6146 scrollx x
6148 | Mzoomrect (p0, _) ->
6149 state.mstate <- Mzoomrect (p0, (x, y));
6150 G.postRedisplay "motion zoomrect";
6151 end;
6152 state.uioh
6154 method pmotion x y =
6155 begin match state.mode with
6156 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6157 let rec loop = function
6158 | [] ->
6159 if hooverpageno != -1
6160 then (
6161 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6162 G.postRedisplay "pmotion birdseye no hoover";
6164 | l :: rest ->
6165 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6166 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6167 then (
6168 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6169 G.postRedisplay "pmotion birdseye hoover";
6171 else loop rest
6173 loop state.layout
6175 | Textentry _ -> ()
6177 | LinkNav _
6178 | View ->
6179 match state.mstate with
6180 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ -> ()
6181 | Mnone ->
6182 updateunder x y;
6183 if canselect ()
6184 then
6185 match conf.pax with
6186 | None -> ()
6187 | Some r ->
6188 let past, _, _ = !r in
6189 let now = now () in
6190 let delta = now -. past in
6191 if delta > 0.01
6192 then paxunder x y
6193 else r := (now, x, y)
6194 end;
6195 state.uioh
6197 method infochanged _ = ()
6199 method scrollph =
6200 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6201 let p, h =
6202 if maxy = 0
6203 then 0.0, float state.winh
6204 else scrollph state.y maxy
6206 vscrollw (), p, h
6208 method scrollpw =
6209 let winw = wadjsb () + state.winw in
6210 let fwinw = float winw in
6211 let sw =
6212 let sw = fwinw /. float state.w in
6213 let sw = fwinw *. sw in
6214 max sw (float conf.scrollh)
6216 let position =
6217 let maxx = state.w + winw in
6218 let x = winw - state.x in
6219 let percent = float x /. float maxx in
6220 (fwinw -. sw) *. percent
6222 hscrollh (), position, sw
6224 method modehash =
6225 let modename =
6226 match state.mode with
6227 | LinkNav _ -> "links"
6228 | Textentry _ -> "textentry"
6229 | Birdseye _ -> "birdseye"
6230 | View -> "view"
6232 findkeyhash conf modename
6234 method eformsgs = true
6235 method alwaysscrolly = false
6236 end;;
6238 let adderrmsg src msg =
6239 Buffer.add_string state.errmsgs msg;
6240 state.newerrmsgs <- true;
6241 G.postRedisplay src
6244 let adderrfmt src fmt =
6245 Format.ksprintf (fun s -> adderrmsg src s) fmt;
6248 let ract cmds =
6249 let cl = splitatspace cmds in
6250 let scan s fmt f =
6251 try Scanf.sscanf s fmt f
6252 with exn ->
6253 adderrfmt "remote exec"
6254 "error processing '%S': %s\n" cmds (exntos exn)
6256 match cl with
6257 | "reload" :: [] -> reload ()
6258 | "goto" :: args :: [] ->
6259 scan args "%u %f %f"
6260 (fun pageno x y ->
6261 let cmd, _ = state.geomcmds in
6262 if emptystr cmd
6263 then gotopagexy pageno x y
6264 else
6265 let f prevf () =
6266 gotopagexy pageno x y;
6267 prevf ()
6269 state.reprf <- f state.reprf
6271 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
6272 | "gotor" :: args :: [] ->
6273 scan args "%S %u"
6274 (fun filename pageno -> gotounder (Uremote (filename, pageno)))
6275 | "gotord" :: args :: [] ->
6276 scan args "%S %S"
6277 (fun filename dest -> gotounder (Uremotedest (filename, dest)))
6278 | "rect" :: args :: [] ->
6279 scan args "%u %u %f %f %f %f"
6280 (fun pageno color x0 y0 x1 y1 ->
6281 onpagerect pageno (fun w h ->
6282 let _,w1,h1,_ = getpagedim pageno in
6283 let sw = float w1 /. float w
6284 and sh = float h1 /. float h in
6285 let x0s = x0 *. sw
6286 and x1s = x1 *. sw
6287 and y0s = y0 *. sh
6288 and y1s = y1 *. sh in
6289 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
6290 debugrect rect;
6291 state.rects <- (pageno, color, rect) :: state.rects;
6292 G.postRedisplay "rect";
6295 | "activatewin" :: [] -> Wsi.activatewin ()
6296 | "quit" :: [] -> raise Quit
6297 | _ ->
6298 adderrfmt "remote command"
6299 "error processing remote command: %S\n" cmds;
6302 let remote =
6303 let scratch = Bytes.create 80 in
6304 let buf = Buffer.create 80 in
6305 fun fd ->
6306 let rec tempfr () =
6307 try Some (Unix.read fd scratch 0 80)
6308 with
6309 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
6310 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
6311 | exn -> raise exn
6313 match tempfr () with
6314 | None -> Some fd
6315 | Some n ->
6316 if n = 0
6317 then (
6318 Unix.close fd;
6319 if Buffer.length buf > 0
6320 then (
6321 let s = Buffer.contents buf in
6322 Buffer.clear buf;
6323 ract s;
6325 None
6327 else
6328 let rec eat ppos =
6329 let nlpos =
6331 let pos = Bytes.index_from scratch ppos '\n' in
6332 if pos >= n then -1 else pos
6333 with Not_found -> -1
6335 if nlpos >= 0
6336 then (
6337 Buffer.add_subbytes buf scratch ppos (nlpos-ppos);
6338 let s = Buffer.contents buf in
6339 Buffer.clear buf;
6340 ract s;
6341 eat (nlpos+1);
6343 else (
6344 Buffer.add_subbytes buf scratch ppos (n-ppos);
6345 Some fd
6347 in eat 0
6350 let remoteopen path =
6351 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
6352 with exn ->
6353 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
6354 None
6357 let () =
6358 let gcconfig = ref E.s in
6359 let trimcachepath = ref E.s in
6360 let rcmdpath = ref E.s in
6361 let pageno = ref None in
6362 let rootwid = ref 0 in
6363 let openlast = ref false in
6364 let nofc = ref false in
6365 selfexec := Sys.executable_name;
6366 Arg.parse
6367 (Arg.align
6368 [("-p", Arg.String (fun s -> state.password <- s),
6369 "<password> Set password");
6371 ("-f", Arg.String
6372 (fun s ->
6373 Config.fontpath := s;
6374 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
6376 "<path> Set path to the user interface font");
6378 ("-c", Arg.String
6379 (fun s ->
6380 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
6381 Config.confpath := s),
6382 "<path> Set path to the configuration file");
6384 ("-last", Arg.Set openlast, " Open last document");
6386 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
6387 "<page-number> Jump to page");
6389 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6390 "<path> Set path to the trim cache file");
6392 ("-dest", Arg.String (fun s -> state.nameddest <- s),
6393 "<named-destination> Set named destination");
6395 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
6396 ("-cxack", Arg.Set cxack, " Cut corners");
6398 ("-remote", Arg.String (fun s -> rcmdpath := s),
6399 "<path> Set path to the remote commands source");
6401 ("-origin", Arg.String (fun s -> state.origin <- s),
6402 "<original-path> Set original path");
6404 ("-gc", Arg.Set_string gcconfig,
6405 "<script-path> Collect garbage with the help of a script");
6407 ("-nofc", Arg.Set nofc, " Do not use fontconfig");
6409 ("-v", Arg.Unit (fun () ->
6410 Printf.printf
6411 "%s\nconfiguration path: %s\n"
6412 (version ())
6413 Config.defconfpath
6415 exit 0), " Print version and exit");
6417 ("-embed", Arg.Set_int rootwid,
6418 "<window-id> Embed into window")
6421 (fun s -> state.path <- s)
6422 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6424 if !wtmode
6425 then selfexec := !selfexec ^ " -wtmode";
6427 let histmode = emptystr state.path && not !openlast in
6429 if not (Config.load !openlast)
6430 then prerr_endline "failed to load configuration";
6431 begin match !pageno with
6432 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
6433 | None -> ()
6434 end;
6436 if not (emptystr !gcconfig)
6437 then (
6438 let c, s =
6439 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6440 | exception exn ->
6441 error "gc socketpair failed: %s" (exntos exn)
6442 | rw -> rw
6444 match addpid @@ popen !gcconfig [(c, 0); (c, 1)] with
6445 | exception exn ->
6446 error "failed to popen gc script: %s" (exntos exn);
6447 | _ ->
6448 Config.gc s s;
6449 exit 0
6452 let wsfd, winw, winh = Wsi.init (object (self)
6453 val mutable m_clicks = 0
6454 val mutable m_click_x = 0
6455 val mutable m_click_y = 0
6456 val mutable m_lastclicktime = infinity
6458 method private cleanup =
6459 state.roam <- noroam;
6460 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap
6461 method expose = G.postRedisplay"expose"
6462 method visible v =
6463 let name =
6464 match v with
6465 | Wsi.Unobscured -> "unobscured"
6466 | Wsi.PartiallyObscured -> "partiallyobscured"
6467 | Wsi.FullyObscured -> "fullyobscured"
6469 vlog "visibility change %s" name
6470 method display = display ()
6471 method map mapped = vlog "mappped %b" mapped
6472 method reshape w h =
6473 self#cleanup;
6474 reshape w h
6475 method mouse b d x y m =
6476 if d && canselect ()
6477 then (
6478 (* http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx *)
6479 m_click_x <- x;
6480 m_click_y <- y;
6481 if b = 1
6482 then (
6483 let t = now () in
6484 if abs x - m_click_x > 10
6485 || abs y - m_click_y > 10
6486 || abs_float (t -. m_lastclicktime) > 0.3
6487 then m_clicks <- 0;
6488 m_clicks <- m_clicks + 1;
6489 m_lastclicktime <- t;
6490 if m_clicks = 1
6491 then (
6492 self#cleanup;
6493 G.postRedisplay "cleanup";
6494 state.uioh <- state.uioh#button b d x y m;
6496 else state.uioh <- state.uioh#multiclick m_clicks x y m
6498 else (
6499 self#cleanup;
6500 m_clicks <- 0;
6501 m_lastclicktime <- infinity;
6502 state.uioh <- state.uioh#button b d x y m
6505 else (
6506 state.uioh <- state.uioh#button b d x y m
6508 method motion x y =
6509 state.mpos <- (x, y);
6510 state.uioh <- state.uioh#motion x y
6511 method pmotion x y =
6512 state.mpos <- (x, y);
6513 state.uioh <- state.uioh#pmotion x y
6514 method key k m =
6515 let mascm = m land (
6516 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6517 ) in
6518 let keyboard k m =
6519 let x = state.x and y = state.y in
6520 keyboard k m;
6521 if x != state.x || y != state.y then self#cleanup
6523 match state.keystate with
6524 | KSnone ->
6525 let km = k, mascm in
6526 begin
6527 match
6528 let modehash = state.uioh#modehash in
6529 try Hashtbl.find modehash km
6530 with Not_found ->
6531 try Hashtbl.find (findkeyhash conf "global") km
6532 with Not_found -> KMinsrt (k, m)
6533 with
6534 | KMinsrt (k, m) -> keyboard k m
6535 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6536 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6538 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6539 List.iter (fun (k, m) -> keyboard k m) insrt;
6540 state.keystate <- KSnone
6541 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6542 state.keystate <- KSinto (keys, insrt)
6543 | KSinto _ -> state.keystate <- KSnone
6545 method enter x y =
6546 state.mpos <- (x, y);
6547 state.uioh <- state.uioh#pmotion x y
6548 method leave = state.mpos <- (-1, -1)
6549 method winstate wsl = state.winstate <- wsl
6550 method quit = raise Quit
6551 end) !rootwid conf.cwinw conf.cwinh platform in
6553 state.wsfd <- wsfd;
6555 if not (
6556 List.exists GlMisc.check_extension
6557 [ "GL_ARB_texture_rectangle"
6558 ; "GL_EXT_texture_recangle"
6559 ; "GL_NV_texture_rectangle" ]
6561 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6563 if (
6564 let r = GlMisc.get_string `renderer in
6565 let p = "Mesa DRI Intel(" in
6566 let l = String.length p in
6567 String.length r > l && String.sub r 0 l = p
6569 then (
6570 defconf.sliceheight <- 1024;
6571 defconf.texcount <- 32;
6572 defconf.usepbo <- true;
6575 let cs, ss =
6576 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6577 | exception exn ->
6578 Printf.eprintf "socketpair failed: %s" (exntos exn);
6579 exit 1
6580 | (r, w) ->
6581 cloexec r;
6582 cloexec w;
6583 r, w
6586 setcheckers conf.checkers;
6587 redirectstderr ();
6588 if conf.redirectstderr
6589 then
6590 at_exit (fun () ->
6591 let s = Bytes.cat
6592 (Buffer.to_bytes state.errmsgs)
6593 (match state.errfd with
6594 | Some fd ->
6595 let s = Bytes.create (80*24) in
6596 let n =
6598 let r, _, _ = Unix.select [fd] [] [] 0.0 in
6599 if List.mem fd r
6600 then Unix.read fd s 0 (Bytes.length s)
6601 else 0
6602 with _ -> 0
6604 if n = 0
6605 then E.b
6606 else Bytes.sub s 0 n
6607 | None -> E.b
6610 try ignore (Unix.write state.stderr s 0 (Bytes.length s))
6611 with exn -> print_endline (exntos exn)
6615 init cs (
6616 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
6617 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6618 !Config.fontpath, !trimcachepath,
6619 GlMisc.check_extension "GL_ARB_pixel_buffer_object",
6620 not !nofc
6622 List.iter GlArray.enable [`texture_coord; `vertex];
6623 state.ss <- ss;
6624 reshape winw winh;
6625 if histmode
6626 then (
6627 state.uioh <- uioh;
6628 Wsi.settitle "llpp (history)";
6629 enterhistmode ();
6631 else (
6632 state.text <- "Opening " ^ (mbtoutf8 state.path);
6633 opendoc state.path state.password;
6634 state.uioh <- uioh;
6636 display ();
6637 Wsi.mapwin ();
6638 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
6639 let optrfd =
6640 ref (
6641 if nonemptystr !rcmdpath
6642 then remoteopen !rcmdpath
6643 else None
6647 let rec loop deadline =
6648 let rec reap () =
6649 if not (ispidsetempty ())
6650 then
6651 match Unix.waitpid [Unix.WNOHANG] 0 with
6652 | (exception exn) -> dolog "Unix.waitpid: %s" @@ exntos exn
6653 | 0, _ -> ()
6654 | pid, status ->
6655 begin match status with
6656 | Unix.WEXITED _ | Unix.WSIGNALED _ -> delpid pid
6657 | Unix.WSTOPPED _ -> ()
6658 end;
6659 reap ()
6661 reap ();
6662 let r =
6663 match state.errfd with
6664 | None -> [state.ss; state.wsfd]
6665 | Some fd -> [state.ss; state.wsfd; fd]
6667 let r =
6668 match !optrfd with
6669 | None -> r
6670 | Some fd -> fd :: r
6672 if state.redisplay
6673 then (
6674 state.redisplay <- false;
6675 display ();
6677 let timeout =
6678 let now = now () in
6679 if deadline > now
6680 then (
6681 if deadline = infinity
6682 then ~-.1.0
6683 else max 0.0 (deadline -. now)
6685 else 0.0
6687 let r, _, _ =
6688 try Unix.select r [] [] timeout
6689 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6691 begin match r with
6692 | [] ->
6693 state.ghyll None;
6694 let newdeadline =
6695 if state.ghyll == noghyll
6696 then
6697 match state.autoscroll with
6698 | Some step when step != 0 ->
6699 let y = state.y + step in
6700 let y =
6701 if y < 0
6702 then state.maxy
6703 else if y >= state.maxy then 0 else y
6705 gotoy y;
6706 if state.mode = View
6707 then state.text <- E.s;
6708 deadline +. 0.01
6709 | _ -> infinity
6710 else deadline +. 0.01
6712 loop newdeadline
6714 | l ->
6715 let rec checkfds = function
6716 | [] -> ()
6717 | fd :: rest when fd = state.ss ->
6718 let cmd = readcmd state.ss in
6719 act cmd;
6720 checkfds rest
6722 | fd :: rest when fd = state.wsfd ->
6723 Wsi.readresp fd;
6724 checkfds rest
6726 | fd :: rest when Some fd = !optrfd ->
6727 begin match remote fd with
6728 | None -> optrfd := remoteopen !rcmdpath;
6729 | opt -> optrfd := opt
6730 end;
6731 checkfds rest
6733 | fd :: rest ->
6734 let s = Bytes.create 80 in
6735 let n = tempfailureretry (Unix.read fd s 0) 80 in
6736 if conf.redirectstderr
6737 then (
6738 Buffer.add_substring state.errmsgs (Bytes.to_string s) 0 n;
6739 state.newerrmsgs <- true;
6740 state.redisplay <- true;
6742 else (
6743 prerr_string (String.sub (Bytes.to_string s) 0 n);
6744 flush stderr;
6746 checkfds rest
6748 checkfds l;
6749 if !reeenterhist then (
6750 enterhistmode ();
6751 reeenterhist := false;
6753 let newdeadline =
6754 let deadline1 =
6755 if deadline = infinity
6756 then now () +. 0.01
6757 else deadline
6759 match state.autoscroll with
6760 | Some step when step != 0 -> deadline1
6761 | _ -> if state.ghyll == noghyll then infinity else deadline1
6763 loop newdeadline
6764 end;
6767 loop infinity;
6768 with Quit ->
6769 Config.save leavebirdseye;
6770 if hasunsavedchanges ()
6771 then save ();