Show link contents in an echo area (when keyboard navigating)
[llpp.git] / main.ml
blob312b16b6b2f96b1db301d4dbb205dc90232f680e
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 if pid > 0
293 then (
294 copysel w opaque;
295 G.postRedisplay "pipesel";
297 else doclose "pipesel pipe/w" w;
298 doclose "pipesel pipe/r" r;
301 let paxunder x y =
302 let g opaque l px py =
303 if markunder opaque px py conf.paxmark
304 then (
305 Some (fun () ->
306 match getopaque l.pageno with
307 | None -> ()
308 | Some opaque -> pipesel opaque conf.paxcmd
311 else None
313 G.postRedisplay "paxunder";
314 if conf.paxmark = Mark_page
315 then
316 List.iter (fun l ->
317 match getopaque l.pageno with
318 | None -> ()
319 | Some opaque -> clearmark opaque) state.layout;
320 state.roam <-
321 onppundermouse g x y (fun () -> showtext '!' "Whoopsie daisy");
324 let selstring s =
325 match Unix.pipe () with
326 | exception exn ->
327 showtext '!' (Printf.sprintf "pipe failed: %s" (exntos exn))
328 | (r, w) ->
329 let clo cap fd =
330 Ne.clo fd (fun msg ->
331 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
334 let pid =
335 try popen conf.selcmd [r, 0; w, -1]
336 with exn ->
337 showtext '!'
338 (Printf.sprintf "failed to execute %s: %s"
339 conf.selcmd (exntos exn));
342 if pid > 0
343 then (
345 let l = String.length s in
346 let bytes = Bytes.unsafe_of_string s in
347 let n = tempfailureretry (Unix.write w bytes 0) l in
348 if n != l
349 then
350 showtext '!'
351 (Printf.sprintf
352 "failed to write %d characters to sel pipe, wrote %d"
355 with exn ->
356 showtext '!'
357 (Printf.sprintf "failed to write to sel pipe: %s"
358 (exntos exn)
361 else dolog "%s" s;
362 clo "selstring pipe/r" r;
363 clo "selstring pipe/w" w;
366 let undertext ?(nopath=false) = function
367 | Unone -> "none"
368 | Ulinkuri s -> s
369 | Ulinkgoto (pageno, _) ->
370 if nopath
371 then "page " ^ string_of_int (pageno+1)
372 else Printf.sprintf "%s: page %d" state.path (pageno+1)
373 | Utext s -> "font: " ^ s
374 | Uunexpected s -> "unexpected: " ^ s
375 | Ulaunch s -> "launch: " ^ s
376 | Unamed s -> "named: " ^ s
377 | Uremote (filename, pageno) ->
378 Printf.sprintf "%s: page %d" filename (pageno+1)
379 | Uremotedest (filename, destname) ->
380 Printf.sprintf "%s: destination %S" filename destname
381 | Uannotation (opaque, slinkindex) ->
382 "annotation: " ^ getannotcontents opaque slinkindex
385 let updateunder x y =
386 match getunder x y with
387 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
388 | Ulinkuri uri ->
389 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
390 Wsi.setcursor Wsi.CURSOR_INFO
391 | Ulinkgoto (pageno, _) ->
392 if conf.underinfo
393 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
394 Wsi.setcursor Wsi.CURSOR_INFO
395 | Utext s ->
396 if conf.underinfo then showtext 'f' ("ont: " ^ s);
397 Wsi.setcursor Wsi.CURSOR_TEXT
398 | Uunexpected s ->
399 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
400 Wsi.setcursor Wsi.CURSOR_INHERIT
401 | Ulaunch s ->
402 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
403 Wsi.setcursor Wsi.CURSOR_INHERIT
404 | Unamed s ->
405 if conf.underinfo then showtext 'n' ("amed: " ^ s);
406 Wsi.setcursor Wsi.CURSOR_INHERIT
407 | Uremote (filename, pageno) ->
408 if conf.underinfo then showtext 'r'
409 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
410 Wsi.setcursor Wsi.CURSOR_INFO
411 | Uremotedest (filename, destname) ->
412 if conf.underinfo then showtext 'r'
413 (Printf.sprintf "emote destination: %s (%S)" filename destname);
414 Wsi.setcursor Wsi.CURSOR_INFO
415 | Uannotation _ ->
416 if conf.underinfo then showtext 'a' "nnotation";
417 Wsi.setcursor Wsi.CURSOR_INFO
420 let showlinktype under =
421 if conf.underinfo && under != Unone
422 then showtext ' ' @@ undertext under
425 let intentry_with_suffix text key =
426 let c =
427 if key >= 32 && key < 127
428 then Char.chr key
429 else '\000'
431 match Char.lowercase c with
432 | '0' .. '9' ->
433 let text = addchar text c in
434 TEcont text
436 | 'k' | 'm' | 'g' ->
437 let text = addchar text c in
438 TEcont text
440 | _ ->
441 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
442 TEcont text
445 let readcmd fd =
446 let s = Bytes.create 4 in
447 let n = tempfailureretry (Unix.read fd s 0) 4 in
448 if n != 4 then error "incomplete read(len) = %d" n;
449 let len = (Char.code (Bytes.get s 0) lsl 24)
450 lor (Char.code (Bytes.get s 1) lsl 16)
451 lor (Char.code (Bytes.get s 2) lsl 8)
452 lor (Char.code (Bytes.get s 3))
454 let s = Bytes.create len in
455 let n = tempfailureretry (Unix.read fd s 0) len in
456 if n != len then error "incomplete read(data) %d vs %d" n len;
457 Bytes.to_string s
460 let wcmd fmt =
461 let b = Buffer.create 16 in
462 Buffer.add_string b "llll";
463 Printf.kbprintf
464 (fun b ->
465 let s = Buffer.to_bytes b in
466 let n = Bytes.length s in
467 let len = n - 4 in
468 (* dolog "wcmd %S" (String.sub s 4 len); *)
469 Bytes.set s 0 (Char.chr ((len lsr 24) land 0xff));
470 Bytes.set s 1 (Char.chr ((len lsr 16) land 0xff));
471 Bytes.set s 2 (Char.chr ((len lsr 8) land 0xff));
472 Bytes.set s 3 (Char.chr (len land 0xff));
473 let n' = tempfailureretry (Unix.write state.ss s 0) n in
474 if n' != n then error "write failed %d vs %d" n' n;
475 ) b fmt;
478 let nogeomcmds cmds =
479 match cmds with
480 | s, [] -> emptystr s
481 | _ -> false
484 let layoutN ((columns, coverA, coverB), b) y sh =
485 let sh = sh - (hscrollh ()) in
486 let wadj = wadjsb () in
487 let rec fold accu n =
488 if n = Array.length b
489 then accu
490 else
491 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
492 if (vy - y) > sh &&
493 (n = coverA - 1
494 || n = state.pagecount - coverB
495 || (n - coverA) mod columns = columns - 1)
496 then accu
497 else
498 let accu =
499 if vy + h > y
500 then
501 let pagey = max 0 (y - vy) in
502 let pagedispy = if pagey > 0 then 0 else vy - y in
503 let pagedispx, pagex =
504 let pdx =
505 if n = coverA - 1 || n = state.pagecount - coverB
506 then state.x + (wadj + state.winw - w) / 2
507 else dx + xoff + state.x
509 if pdx < 0
510 then 0, -pdx
511 else pdx, 0
513 let pagevw =
514 let vw = wadj + state.winw - pagedispx in
515 let pw = w - pagex in
516 min vw pw
518 let pagevh = min (h - pagey) (sh - pagedispy) in
519 if pagevw > 0 && pagevh > 0
520 then
521 let e =
522 { pageno = n
523 ; pagedimno = pdimno
524 ; pagew = w
525 ; pageh = h
526 ; pagex = pagex
527 ; pagey = pagey
528 ; pagevw = pagevw
529 ; pagevh = pagevh
530 ; pagedispx = pagedispx
531 ; pagedispy = pagedispy
532 ; pagecol = 0
535 e :: accu
536 else
537 accu
538 else
539 accu
541 fold accu (n+1)
543 if Array.length b = 0
544 then []
545 else List.rev (fold [] (page_of_y y))
548 let layoutS (columns, b) y sh =
549 let sh = sh - hscrollh () in
550 let wadj = wadjsb () in
551 let rec fold accu n =
552 if n = Array.length b
553 then accu
554 else
555 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
556 if (vy - y) > sh
557 then accu
558 else
559 let accu =
560 if vy + pageh > y
561 then
562 let x = xoff + state.x in
563 let pagey = max 0 (y - vy) in
564 let pagedispy = if pagey > 0 then 0 else vy - y in
565 let pagedispx, pagex =
566 if px = 0
567 then (
568 if x < 0
569 then 0, -x
570 else x, 0
572 else (
573 let px = px - x in
574 if px < 0
575 then -px, 0
576 else 0, px
579 let pagecolw = pagew/columns in
580 let pagedispx =
581 if pagecolw < state.winw
582 then pagedispx + ((wadj + state.winw - pagecolw) / 2)
583 else pagedispx
585 let pagevw =
586 let vw = wadj + state.winw - pagedispx in
587 let pw = pagew - pagex in
588 min vw pw
590 let pagevw = min pagevw pagecolw in
591 let pagevh = min (pageh - pagey) (sh - pagedispy) in
592 if pagevw > 0 && pagevh > 0
593 then
594 let e =
595 { pageno = n/columns
596 ; pagedimno = pdimno
597 ; pagew = pagew
598 ; pageh = pageh
599 ; pagex = pagex
600 ; pagey = pagey
601 ; pagevw = pagevw
602 ; pagevh = pagevh
603 ; pagedispx = pagedispx
604 ; pagedispy = pagedispy
605 ; pagecol = n mod columns
608 e :: accu
609 else
610 accu
611 else
612 accu
614 fold accu (n+1)
616 List.rev (fold [] 0)
619 let layout y sh =
620 if nogeomcmds state.geomcmds
621 then
622 match conf.columns with
623 | Csingle b -> layoutN ((1, 0, 0), b) y sh
624 | Cmulti c -> layoutN c y sh
625 | Csplit s -> layoutS s y sh
626 else []
629 let clamp incr =
630 let y = state.y + incr in
631 let y = max 0 y in
632 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
636 let itertiles l f =
637 let tilex = l.pagex mod conf.tilew in
638 let tiley = l.pagey mod conf.tileh in
640 let col = l.pagex / conf.tilew in
641 let row = l.pagey / conf.tileh in
643 let xadj = xadjsb () in
644 let rec rowloop row y0 dispy h =
645 if h = 0
646 then ()
647 else (
648 let dh = conf.tileh - y0 in
649 let dh = min h dh in
650 let rec colloop col x0 dispx w =
651 if w = 0
652 then ()
653 else (
654 let dw = conf.tilew - x0 in
655 let dw = min w dw in
656 let dispx' = xadj + dispx in
657 f col row dispx' dispy x0 y0 dw dh;
658 colloop (col+1) 0 (dispx+dw) (w-dw)
661 colloop col tilex l.pagedispx l.pagevw;
662 rowloop (row+1) 0 (dispy+dh) (h-dh)
665 if l.pagevw > 0 && l.pagevh > 0
666 then rowloop row tiley l.pagedispy l.pagevh;
669 let gettileopaque l col row =
670 let key =
671 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
673 try Some (Hashtbl.find state.tilemap key)
674 with Not_found -> None
677 let puttileopaque l col row gen colorspace angle opaque size elapsed =
678 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
679 Hashtbl.add state.tilemap key (opaque, size, elapsed)
682 let filledrect x0 y0 x1 y1 =
683 GlArray.disable `texture_coord;
684 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
685 GlArray.vertex `two state.vraw;
686 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
687 GlArray.enable `texture_coord;
690 let linerect x0 y0 x1 y1 =
691 GlArray.disable `texture_coord;
692 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y1; x1; y0 |];
693 GlArray.vertex `two state.vraw;
694 GlArray.draw_arrays `line_loop ~first:0 ~count:4;
695 GlArray.enable `texture_coord;
698 let drawtiles l color =
699 GlDraw.color color;
700 let wadj = wadjsb () in
701 begintiles ();
702 let f col row x y tilex tiley w h =
703 match gettileopaque l col row with
704 | Some (opaque, _, t) ->
705 let params = x, y, w, h, tilex, tiley in
706 if conf.invert
707 then GlTex.env (`mode `blend);
708 drawtile params opaque;
709 if conf.invert
710 then GlTex.env (`mode `modulate);
711 if conf.debug
712 then (
713 endtiles ();
714 let s = Printf.sprintf
715 "%d[%d,%d] %f sec"
716 l.pageno col row t
718 let w = measurestr fstate.fontsize s in
719 GlDraw.color (0.0, 0.0, 0.0);
720 filledrect (float (x-2))
721 (float (y-2))
722 (float (x+2) +. w)
723 (float (y + fstate.fontsize + 2));
724 GlDraw.color (1.0, 1.0, 1.0);
725 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
726 begintiles ();
729 | None ->
730 endtiles ();
731 let w =
732 if conf.leftscroll
733 then w
734 else
735 let lw = wadj + state.winw - x in
736 min lw w
737 and h =
738 let lh = state.winh - y in
739 min lh h
741 if conf.invert
742 then GlTex.env (`mode `blend);
743 begin match state.checkerstexid with
744 | Some id ->
745 Gl.enable `texture_2d;
746 GlTex.bind_texture ~target:`texture_2d id;
747 let x0 = float x
748 and y0 = float y
749 and x1 = float (x+w)
750 and y1 = float (y+h) in
752 let tw = float w /. 16.0
753 and th = float h /. 16.0 in
754 let tx0 = float tilex /. 16.0
755 and ty0 = float tiley /. 16.0 in
756 let tx1 = tx0 +. tw
757 and ty1 = ty0 +. th in
758 Raw.sets_float state.vraw ~pos:0
759 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
760 Raw.sets_float state.traw ~pos:0
761 [| tx0; ty0; tx0; ty1; tx1; ty0; tx1; ty1 |];
762 GlArray.vertex `two state.vraw;
763 GlArray.tex_coord `two state.traw;
764 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
765 Gl.disable `texture_2d;
767 | None ->
768 GlDraw.color (1.0, 1.0, 1.0);
769 filledrect (float x) (float y) (float (x+w)) (float (y+h));
770 end;
771 if conf.invert
772 then GlTex.env (`mode `modulate);
773 if w > 128 && h > fstate.fontsize + 10
774 then (
775 let c = if conf.invert then 1.0 else 0.0 in
776 GlDraw.color (c, c, c);
777 let c, r =
778 if conf.verbose
779 then (col*conf.tilew, row*conf.tileh)
780 else col, row
782 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
784 GlDraw.color color;
785 begintiles ();
787 itertiles l f;
788 endtiles ();
791 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
793 let tilevisible1 l x y =
794 let ax0 = l.pagex
795 and ax1 = l.pagex + l.pagevw
796 and ay0 = l.pagey
797 and ay1 = l.pagey + l.pagevh in
799 let bx0 = x
800 and by0 = y in
801 let bx1 = min (bx0 + conf.tilew) l.pagew
802 and by1 = min (by0 + conf.tileh) l.pageh in
804 let rx0 = max ax0 bx0
805 and ry0 = max ay0 by0
806 and rx1 = min ax1 bx1
807 and ry1 = min ay1 by1 in
809 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
810 nonemptyintersection
813 let tilevisible layout n x y =
814 let rec findpageinlayout m = function
815 | l :: rest when l.pageno = n ->
816 tilevisible1 l x y || (
817 match conf.columns with
818 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
819 | Csplit _
820 | Csingle _
821 | Cmulti _ -> false
823 | _ :: rest -> findpageinlayout 0 rest
824 | [] -> false
826 findpageinlayout 0 layout;
829 let tileready l x y =
830 tilevisible1 l x y &&
831 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
834 let tilepage n p layout =
835 let rec loop = function
836 | l :: rest ->
837 if l.pageno = n
838 then
839 let f col row _ _ _ _ _ _ =
840 if state.currently = Idle
841 then
842 match gettileopaque l col row with
843 | Some _ -> ()
844 | None ->
845 let x = col*conf.tilew
846 and y = row*conf.tileh in
847 let w =
848 let w = l.pagew - x in
849 min w conf.tilew
851 let h =
852 let h = l.pageh - y in
853 min h conf.tileh
855 let pbo =
856 if conf.usepbo
857 then getpbo w h conf.colorspace
858 else ~< "0"
860 wcmd "tile %s %d %d %d %d %s"
861 (~> p) x y w h (~> pbo);
862 state.currently <-
863 Tiling (
864 l, p, conf.colorspace, conf.angle,
865 state.gen, col, row, conf.tilew, conf.tileh
868 itertiles l f;
869 else
870 loop rest
872 | [] -> ()
874 if nogeomcmds state.geomcmds
875 then loop layout;
878 let preloadlayout y =
879 let y = if y < state.winh then 0 else y - state.winh in
880 let h = state.winh*3 in
881 layout y h;
884 let load pages =
885 let rec loop pages =
886 if state.currently != Idle
887 then ()
888 else
889 match pages with
890 | l :: rest ->
891 begin match getopaque l.pageno with
892 | None ->
893 wcmd "page %d %d" l.pageno l.pagedimno;
894 state.currently <- Loading (l, state.gen);
895 | Some opaque ->
896 tilepage l.pageno opaque pages;
897 loop rest
898 end;
899 | _ -> ()
901 if nogeomcmds state.geomcmds
902 then loop pages
905 let preload pages =
906 load pages;
907 if conf.preload && state.currently = Idle
908 then load (preloadlayout state.y);
911 let layoutready layout =
912 let rec fold all ls =
913 all && match ls with
914 | l :: rest ->
915 let seen = ref false in
916 let allvisible = ref true in
917 let foo col row _ _ _ _ _ _ =
918 seen := true;
919 allvisible := !allvisible &&
920 begin match gettileopaque l col row with
921 | Some _ -> true
922 | None -> false
925 itertiles l foo;
926 fold (!seen && !allvisible) rest
927 | [] -> true
929 let alltilesvisible = fold true layout in
930 alltilesvisible;
933 let gotoy y =
934 let y = bound y 0 state.maxy in
935 let y, layout, proceed =
936 match conf.maxwait with
937 | Some time when state.ghyll == noghyll ->
938 begin match state.throttle with
939 | None ->
940 let layout = layout y state.winh in
941 let ready = layoutready layout in
942 if not ready
943 then (
944 load layout;
945 state.throttle <- Some (layout, y, now ());
947 else G.postRedisplay "gotoy showall (None)";
948 y, layout, ready
949 | Some (_, _, started) ->
950 let dt = now () -. started in
951 if dt > time
952 then (
953 state.throttle <- None;
954 let layout = layout y state.winh in
955 load layout;
956 G.postRedisplay "maxwait";
957 y, layout, true
959 else -1, [], false
962 | _ ->
963 let layout = layout y state.winh in
964 if not !wtmode || layoutready layout
965 then G.postRedisplay "gotoy ready";
966 y, layout, true
968 if proceed
969 then (
970 state.y <- y;
971 state.layout <- layout;
972 begin match state.mode with
973 | LinkNav ln ->
974 begin match ln with
975 | Ltexact (pageno, linkno) ->
976 let rec loop = function
977 | [] ->
978 state.mode <- LinkNav (Ltgendir 0)
979 | l :: _ when l.pageno = pageno ->
980 begin match getopaque pageno with
981 | None -> state.mode <- LinkNav (Ltnotready (pageno, 0))
982 | Some opaque ->
983 let x0, y0, x1, y1 = getlinkrect opaque linkno in
984 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
985 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
986 then state.mode <- LinkNav (Ltgendir 0)
988 | _ :: rest -> loop rest
990 loop layout
991 | Ltnotready _ | Ltgendir _ -> ()
993 | Birdseye _
994 | Textentry _
995 | View -> ()
996 end;
997 begin match state.mode with
998 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
999 if not (pagevisible layout pageno)
1000 then (
1001 match state.layout with
1002 | [] -> ()
1003 | l :: _ ->
1004 state.mode <- Birdseye (
1005 conf, leftx, l.pageno, hooverpageno, anchor
1008 | LinkNav lt ->
1009 begin match lt with
1010 | Ltnotready (_, dir)
1011 | Ltgendir dir ->
1012 let linknav =
1013 let rec loop = function
1014 | [] -> lt
1015 | l :: rest ->
1016 match getopaque l.pageno with
1017 | None -> Ltnotready (l.pageno, dir)
1018 | Some opaque ->
1019 let link =
1020 let ld =
1021 if dir = 0
1022 then LDfirstvisible (l.pagex, l.pagey, dir)
1023 else (
1024 if dir > 0 then LDfirst else LDlast
1027 findlink opaque ld
1029 match link with
1030 | Lnotfound -> loop rest
1031 | Lfound n ->
1032 showlinktype (getlink opaque n);
1033 Ltexact (l.pageno, n)
1035 loop state.layout
1037 state.mode <- LinkNav linknav
1038 | Ltexact _ -> ()
1040 | Textentry _
1041 | View -> ()
1042 end;
1043 preload layout;
1045 state.ghyll <- noghyll;
1046 if conf.updatecurs
1047 then (
1048 let mx, my = state.mpos in
1049 updateunder mx my;
1053 let conttiling pageno opaque =
1054 tilepage pageno opaque
1055 (if conf.preload then preloadlayout state.y else state.layout)
1058 let gotoy_and_clear_text y =
1059 if not conf.verbose then state.text <- E.s;
1060 gotoy y;
1063 let getanchory (n, top, dtop) =
1064 let y, h = getpageyh n in
1065 if conf.presentation
1066 then
1067 let ips = calcips h in
1068 y + truncate (top*.float h -. dtop*.float ips) + ips;
1069 else
1070 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1073 let gotoanchor anchor =
1074 gotoy (getanchory anchor);
1077 let addnav () =
1078 cbput state.hists.nav (getanchor ());
1081 let getnav dir =
1082 let anchor = cbgetc state.hists.nav dir in
1083 getanchory anchor;
1086 let gotoghyll1 single y =
1087 let scroll f n a b =
1088 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1089 let snake f a b =
1090 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1091 if f < a
1092 then s (float f /. float a)
1093 else (
1094 if f > b
1095 then 1.0 -. s ((float (f-b) /. float (n-b)))
1096 else 1.0
1099 snake f a b
1100 and summa n a b =
1101 let ins = float a *. 0.5
1102 and outs = float (n-b) *. 0.5 in
1103 let ones = b - a in
1104 ins +. outs +. float ones
1106 let rec set nab y sy =
1107 let (_N, _A, _B), y =
1108 if single
1109 then
1110 let scl = if y > sy then 2 else -2 in
1111 let _N, _, _ = nab in
1112 (_N,0,_N), y+conf.scrollstep*scl
1113 else nab,y in
1114 let sum = summa _N _A _B in
1115 let dy = float (y - sy) in
1116 state.ghyll <- (
1117 let rec gf n y1 o =
1118 if n >= _N
1119 then state.ghyll <- noghyll
1120 else
1121 let go n =
1122 let s = scroll n _N _A _B in
1123 let y1 = y1 +. ((s *. dy) /. sum) in
1124 gotoy_and_clear_text (truncate y1);
1125 state.ghyll <- gf (n+1) y1;
1127 match o with
1128 | None -> go n
1129 | Some y' when single -> set nab y' state.y
1130 | Some y' -> set (_N/2, 1, 1) y' state.y
1132 gf 0 (float state.y)
1135 match conf.ghyllscroll with
1136 | Some nab when not conf.presentation ->
1137 if state.ghyll == noghyll
1138 then set nab y state.y
1139 else state.ghyll (Some y)
1140 | _ ->
1141 gotoy_and_clear_text y
1144 let gotoghyll = gotoghyll1 false;;
1146 let gotopage n top =
1147 let y, h = getpageyh n in
1148 let y = y + (truncate (top *. float h)) in
1149 gotoghyll y
1152 let gotopage1 n top =
1153 let y = getpagey n in
1154 let y = y + top in
1155 gotoghyll y
1158 let invalidate s f =
1159 state.layout <- [];
1160 state.pdims <- [];
1161 state.rects <- [];
1162 state.rects1 <- [];
1163 match state.geomcmds with
1164 | ps, [] when emptystr ps ->
1165 f ();
1166 state.geomcmds <- s, [];
1168 | ps, [] ->
1169 state.geomcmds <- ps, [s, f];
1171 | ps, (s', _) :: rest when s' = s ->
1172 state.geomcmds <- ps, ((s, f) :: rest);
1174 | ps, cmds ->
1175 state.geomcmds <- ps, ((s, f) :: cmds);
1178 let flushpages () =
1179 Hashtbl.iter (fun _ opaque ->
1180 wcmd "freepage %s" (~> opaque);
1181 ) state.pagemap;
1182 Hashtbl.clear state.pagemap;
1185 let flushtiles () =
1186 if not (Queue.is_empty state.tilelru)
1187 then (
1188 Queue.iter (fun (k, p, s) ->
1189 wcmd "freetile %s" (~> p);
1190 state.memused <- state.memused - s;
1191 Hashtbl.remove state.tilemap k;
1192 ) state.tilelru;
1193 state.uioh#infochanged Memused;
1194 Queue.clear state.tilelru;
1196 load state.layout;
1199 let stateh h =
1200 let h = truncate (float h*.conf.zoom) in
1201 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
1202 h - d
1205 let opendoc path password =
1206 state.path <- path;
1207 state.password <- password;
1208 state.gen <- state.gen + 1;
1209 state.docinfo <- [];
1210 state.outlines <- [||];
1212 flushpages ();
1213 setaalevel conf.aalevel;
1214 let titlepath =
1215 if emptystr state.origin
1216 then path
1217 else state.origin
1219 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
1220 wcmd "open %d %d %s\000%s\000" (btod !wtmode) (btod !cxack) path password;
1221 invalidate "reqlayout"
1222 (fun () ->
1223 wcmd "reqlayout %d %d %d %s\000"
1224 conf.angle (FMTE.to_int conf.fitmodel)
1225 (stateh state.winh) state.nameddest
1229 let reload () =
1230 state.anchor <- getanchor ();
1231 opendoc state.path state.password;
1234 let scalecolor c =
1235 let c = c *. conf.colorscale in
1236 (c, c, c);
1239 let scalecolor2 (r, g, b) =
1240 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1243 let docolumns columns =
1244 let wadj = wadjsb () in
1245 match columns with
1246 | Csingle _ ->
1247 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1248 let wadj = wadjsb () in
1249 let rec loop pageno pdimno pdim y ph pdims =
1250 if pageno = state.pagecount
1251 then ()
1252 else
1253 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1254 match pdims with
1255 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1256 pdimno+1, pdim, rest
1257 | _ ->
1258 pdimno, pdim, pdims
1260 let x = max 0 (((wadj + state.winw - w) / 2) - xoff) in
1261 let y = y +
1262 (if conf.presentation
1263 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1264 else (if pageno = 0 then 0 else conf.interpagespace)
1267 a.(pageno) <- (pdimno, x, y, pdim);
1268 loop (pageno+1) pdimno pdim (y + h) h pdims
1270 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1271 conf.columns <- Csingle a;
1273 | Cmulti ((columns, coverA, coverB), _) ->
1274 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1275 let rec loop pageno pdimno pdim x y rowh pdims =
1276 let rec fixrow m = if m = pageno then () else
1277 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1278 if h < rowh
1279 then (
1280 let y = y + (rowh - h) / 2 in
1281 a.(m) <- (pdimno, x, y, pdim);
1283 fixrow (m+1)
1285 if pageno = state.pagecount
1286 then fixrow (((pageno - 1) / columns) * columns)
1287 else
1288 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1289 match pdims with
1290 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1291 pdimno+1, pdim, rest
1292 | _ ->
1293 pdimno, pdim, pdims
1295 let x, y, rowh' =
1296 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1297 then (
1298 let x = (wadj + state.winw - w) / 2 in
1299 let ips =
1300 if conf.presentation then calcips h else conf.interpagespace in
1301 x, y + ips + rowh, h
1303 else (
1304 if (pageno - coverA) mod columns = 0
1305 then (
1306 let x = max 0 (wadj + state.winw - state.w) / 2 in
1307 let y =
1308 if conf.presentation
1309 then
1310 let ips = calcips h in
1311 y + (if pageno = 0 then 0 else calcips rowh + ips)
1312 else
1313 y + (if pageno = 0 then 0 else conf.interpagespace)
1315 x, y + rowh, h
1317 else x, y, max rowh h
1320 let y =
1321 if pageno > 1 && (pageno - coverA) mod columns = 0
1322 then (
1323 let y =
1324 if pageno = columns && conf.presentation
1325 then (
1326 let ips = calcips rowh in
1327 for i = 0 to pred columns
1329 let (pdimno, x, y, pdim) = a.(i) in
1330 a.(i) <- (pdimno, x, y+ips, pdim)
1331 done;
1332 y+ips;
1334 else y
1336 fixrow (pageno - columns);
1339 else y
1341 a.(pageno) <- (pdimno, x, y, pdim);
1342 let x = x + w + xoff*2 + conf.interpagespace in
1343 loop (pageno+1) pdimno pdim x y rowh' pdims
1345 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1346 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1348 | Csplit (c, _) ->
1349 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1350 let rec loop pageno pdimno pdim y pdims =
1351 if pageno = state.pagecount
1352 then ()
1353 else
1354 let pdimno, ((_, w, h, _) as pdim), pdims =
1355 match pdims with
1356 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1357 pdimno+1, pdim, rest
1358 | _ ->
1359 pdimno, pdim, pdims
1361 let cw = w / c in
1362 let rec loop1 n x y =
1363 if n = c then y else (
1364 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1365 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1368 let y = loop1 0 0 y in
1369 loop (pageno+1) pdimno pdim y pdims
1371 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1372 conf.columns <- Csplit (c, a);
1375 let represent () =
1376 docolumns conf.columns;
1377 state.maxy <- calcheight ();
1378 if state.reprf == noreprf
1379 then (
1380 match state.mode with
1381 | Birdseye (_, _, pageno, _, _) ->
1382 let y, h = getpageyh pageno in
1383 let top = (state.winh - h) / 2 in
1384 gotoy (max 0 (y - top))
1385 | Textentry _
1386 | View
1387 | LinkNav _ -> gotoanchor state.anchor
1389 else (
1390 state.reprf ();
1391 state.reprf <- noreprf;
1395 let reshape ?(firsttime=false) w h =
1396 GlDraw.viewport ~x:0 ~y:0 ~w:w ~h:h;
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 getcmdoutput
1631 (fun s ->
1632 showtext '!' @@ "error getting password: " ^ s;
1633 dolog "%s" s) passcmd;
1636 let act cmds =
1637 (* dolog "%S" cmds; *)
1638 let cl = splitatspace cmds in
1639 let scan s fmt f =
1640 try Scanf.sscanf s fmt f
1641 with exn ->
1642 dolog "error processing '%S': %s" cmds (exntos exn);
1643 exit 1
1645 let addoutline outline =
1646 match state.currently with
1647 | Outlining outlines ->
1648 state.currently <- Outlining (outline :: outlines)
1649 | Idle -> state.currently <- Outlining [outline]
1650 | Loading _
1651 | Tiling _ ->
1652 dolog "invalid outlining state";
1653 logcurrently state.currently
1655 match cl with
1656 | "clear" :: [] ->
1657 state.uioh#infochanged Pdim;
1658 state.pdims <- [];
1660 | "clearrects" :: [] ->
1661 state.rects <- state.rects1;
1662 G.postRedisplay "clearrects";
1664 | "continue" :: args :: [] ->
1665 let n = scan args "%u" (fun n -> n) in
1666 state.pagecount <- n;
1667 begin match state.currently with
1668 | Outlining l ->
1669 state.currently <- Idle;
1670 state.outlines <- Array.of_list (List.rev l)
1671 | Idle
1672 | Loading _
1673 | Tiling _ -> ()
1674 end;
1676 let cur, cmds = state.geomcmds in
1677 if emptystr cur
1678 then failwith "umpossible";
1680 begin match List.rev cmds with
1681 | [] ->
1682 state.geomcmds <- E.s, [];
1683 state.throttle <- None;
1684 represent ();
1685 | (s, f) :: rest ->
1686 f ();
1687 state.geomcmds <- s, List.rev rest;
1688 end;
1689 if conf.maxwait = None && not !wtmode
1690 then G.postRedisplay "continue";
1692 | "msg" :: args :: [] ->
1693 showtext ' ' args
1695 | "vmsg" :: args :: [] ->
1696 if conf.verbose
1697 then showtext ' ' args
1699 | "emsg" :: args :: [] ->
1700 Buffer.add_string state.errmsgs args;
1701 state.newerrmsgs <- true;
1702 G.postRedisplay "error message"
1704 | "progress" :: args :: [] ->
1705 let progress, text =
1706 scan args "%f %n"
1707 (fun f pos ->
1708 f, String.sub args pos (String.length args - pos))
1710 state.text <- text;
1711 state.progress <- progress;
1712 G.postRedisplay "progress"
1714 | "firstmatch" :: args :: [] ->
1715 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1716 scan args "%u %d %f %f %f %f %f %f %f %f"
1717 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1718 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1720 let xoff = float (xadjsb ()) in
1721 let x0 = x0 +. xoff
1722 and x1 = x1 +. xoff
1723 and x2 = x2 +. xoff
1724 and x3 = x3 +. xoff in
1725 let y = (getpagey pageno) + truncate y0 in
1726 addnav ();
1727 gotoy y;
1728 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
1730 | "match" :: args :: [] ->
1731 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1732 scan args "%u %d %f %f %f %f %f %f %f %f"
1733 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1734 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1736 let xoff = float (xadjsb ()) in
1737 let x0 = x0 +. xoff
1738 and x1 = x1 +. xoff
1739 and x2 = x2 +. xoff
1740 and x3 = x3 +. xoff in
1741 state.rects1 <-
1742 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1744 | "page" :: args :: [] ->
1745 let pageopaques, t = scan args "%s %f" (fun p t -> p, t) in
1746 let pageopaque = ~< pageopaques in
1747 begin match state.currently with
1748 | Loading (l, gen) ->
1749 vlog "page %d took %f sec" l.pageno t;
1750 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1751 begin match state.throttle with
1752 | None ->
1753 let preloadedpages =
1754 if conf.preload
1755 then preloadlayout state.y
1756 else state.layout
1758 let evict () =
1759 let set =
1760 List.fold_left (fun s l -> IntSet.add l.pageno s)
1761 IntSet.empty preloadedpages
1763 let evictedpages =
1764 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1765 if not (IntSet.mem pageno set)
1766 then (
1767 wcmd "freepage %s" (~> opaque);
1768 key :: accu
1770 else accu
1771 ) state.pagemap []
1773 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1775 evict ();
1776 state.currently <- Idle;
1777 if gen = state.gen
1778 then (
1779 tilepage l.pageno pageopaque state.layout;
1780 load state.layout;
1781 load preloadedpages;
1782 let visible = pagevisible state.layout l.pageno in
1783 if visible
1784 then (
1785 match state.mode with
1786 | LinkNav (Ltnotready (pageno, dir)) ->
1787 if pageno = l.pageno
1788 then (
1789 let link =
1790 let ld =
1791 if dir = 0
1792 then LDfirstvisible (l.pagex, l.pagey, dir)
1793 else (
1794 if dir > 0 then LDfirst else LDlast
1797 findlink pageopaque ld
1799 match link with
1800 | Lnotfound -> ()
1801 | Lfound n ->
1802 showlinktype (getlink pageopaque n);
1803 state.mode <- LinkNav (Ltexact (l.pageno, n))
1805 | LinkNav (Ltgendir _)
1806 | LinkNav (Ltexact _)
1807 | View
1808 | Birdseye _
1809 | Textentry _ -> ()
1812 if visible && layoutready state.layout
1813 then (
1814 G.postRedisplay "page";
1818 | Some (layout, _, _) ->
1819 state.currently <- Idle;
1820 tilepage l.pageno pageopaque layout;
1821 load state.layout
1822 end;
1824 | Idle
1825 | Tiling _
1826 | Outlining _ ->
1827 dolog "Inconsistent loading state";
1828 logcurrently state.currently;
1829 exit 1
1832 | "tile" :: args :: [] ->
1833 let (x, y, opaques, size, t) =
1834 scan args "%u %u %s %u %f"
1835 (fun x y p size t -> (x, y, p, size, t))
1837 let opaque = ~< opaques in
1838 begin match state.currently with
1839 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1840 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1842 unmappbo opaque;
1843 if tilew != conf.tilew || tileh != conf.tileh
1844 then (
1845 wcmd "freetile %s" (~> opaque);
1846 state.currently <- Idle;
1847 load state.layout;
1849 else (
1850 puttileopaque l col row gen cs angle opaque size t;
1851 state.memused <- state.memused + size;
1852 state.uioh#infochanged Memused;
1853 gctiles ();
1854 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1855 opaque, size) state.tilelru;
1857 let layout =
1858 match state.throttle with
1859 | None -> state.layout
1860 | Some (layout, _, _) -> layout
1863 state.currently <- Idle;
1864 if gen = state.gen
1865 && conf.colorspace = cs
1866 && conf.angle = angle
1867 && tilevisible layout l.pageno x y
1868 then conttiling l.pageno pageopaque;
1870 begin match state.throttle with
1871 | None ->
1872 preload state.layout;
1873 if gen = state.gen
1874 && conf.colorspace = cs
1875 && conf.angle = angle
1876 && tilevisible state.layout l.pageno x y
1877 && (not !wtmode || layoutready state.layout)
1878 then G.postRedisplay "tile nothrottle";
1880 | Some (layout, y, _) ->
1881 let ready = layoutready layout in
1882 if ready
1883 then (
1884 state.y <- y;
1885 state.layout <- layout;
1886 state.throttle <- None;
1887 G.postRedisplay "throttle";
1889 else load layout;
1890 end;
1893 | Idle
1894 | Loading _
1895 | Outlining _ ->
1896 dolog "Inconsistent tiling state";
1897 logcurrently state.currently;
1898 exit 1
1901 | "pdim" :: args :: [] ->
1902 let (n, w, h, _) as pdim =
1903 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1905 let pdim =
1906 match conf.fitmodel with
1907 | FitWidth -> pdim
1908 | FitPage | FitProportional ->
1909 match conf.columns with
1910 | Csplit _ -> (n, w, h, 0)
1911 | Csingle _ | Cmulti _ -> pdim
1913 state.uioh#infochanged Pdim;
1914 state.pdims <- pdim :: state.pdims
1916 | "o" :: args :: [] ->
1917 let (l, n, t, h, pos) =
1918 scan args "%u %u %d %u %n"
1919 (fun l n t h pos -> l, n, t, h, pos)
1921 let s = String.sub args pos (String.length args - pos) in
1922 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
1924 | "ou" :: args :: [] ->
1925 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
1926 let s = String.sub args pos len in
1927 let pos2 = pos + len + 1 in
1928 let uri = String.sub args pos2 (String.length args - pos2) in
1929 addoutline (s, l, Ouri uri)
1931 | "on" :: args :: [] ->
1932 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
1933 let s = String.sub args pos (String.length args - pos) in
1934 addoutline (s, l, Onone)
1936 | "a" :: args :: [] ->
1937 let (n, l, t) =
1938 scan args "%u %d %d" (fun n l t -> n, l, t)
1940 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
1942 | "info" :: args :: [] ->
1943 let pos = nindex args '\t' in
1944 if pos >= 0 && String.sub args 0 pos = "Title"
1945 then (
1946 let s = String.sub args (pos+1) @@ String.length args - pos - 1 in
1947 conf.title <- s;
1948 Wsi.settitle s;
1950 state.docinfo <- (1, args) :: state.docinfo
1952 | "infoend" :: [] ->
1953 state.uioh#infochanged Docinfo;
1954 state.docinfo <- List.rev state.docinfo
1956 | "pass" :: l ->
1957 if l = "fail" :: []
1958 then Wsi.settitle "Wrong password";
1959 let password = getpassword () in
1960 if password == E.s
1961 then error "document is password protected"
1962 else opendoc state.path password
1964 | _ ->
1965 error "unknown cmd `%S'" cmds
1968 let onhist cb =
1969 let rc = cb.rc in
1970 let action = function
1971 | HCprev -> cbget cb ~-1
1972 | HCnext -> cbget cb 1
1973 | HCfirst -> cbget cb ~-(cb.rc)
1974 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1975 and cancel () = cb.rc <- rc
1976 in (action, cancel)
1979 let search pattern forward =
1980 match conf.columns with
1981 | Csplit _ ->
1982 showtext '!' "searching does not work properly in split columns mode"
1983 | Csingle _
1984 | Cmulti _ ->
1985 if nonemptystr pattern
1986 then
1987 let pn, py =
1988 match state.layout with
1989 | [] -> 0, 0
1990 | l :: _ ->
1991 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1993 wcmd "search %d %d %d %d,%s\000"
1994 (btod conf.icase) pn py (btod forward) pattern;
1997 let intentry text key =
1998 let c =
1999 if key >= 32 && key < 127
2000 then Char.chr key
2001 else '\000'
2003 match c with
2004 | '0' .. '9' ->
2005 let text = addchar text c in
2006 TEcont text
2008 | _ ->
2009 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2010 TEcont text
2013 let linknact f s =
2014 if nonemptystr s
2015 then (
2016 let n =
2017 let l = String.length s in
2018 let rec loop pos n = if pos = l then n else
2019 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2020 loop (pos+1) (n*26 + m)
2021 in loop 0 0
2023 let rec loop n = function
2024 | [] -> ()
2025 | l :: rest ->
2026 match getopaque l.pageno with
2027 | None -> loop n rest
2028 | Some opaque ->
2029 let m = getlinkcount opaque in
2030 if n < m
2031 then (
2032 let under = getlink opaque n in
2033 f under
2035 else loop (n-m) rest
2037 loop n state.layout;
2041 let linknentry text key =
2042 let c =
2043 if key >= 32 && key < 127
2044 then Char.chr key
2045 else '\000'
2047 match c with
2048 | 'a' .. 'z' ->
2049 let text = addchar text c in
2050 linknact (fun under -> state.text <- undertext ~nopath:true under) text;
2051 TEcont text
2053 | _ ->
2054 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2055 TEcont text
2058 let textentry text key =
2059 if key land 0xff00 = 0xff00
2060 then TEcont text
2061 else TEcont (text ^ toutf8 key)
2064 let reqlayout angle fitmodel =
2065 match state.throttle with
2066 | None ->
2067 if nogeomcmds state.geomcmds
2068 then state.anchor <- getanchor ();
2069 conf.angle <- angle mod 360;
2070 if conf.angle != 0
2071 then (
2072 match state.mode with
2073 | LinkNav _ -> state.mode <- View
2074 | Birdseye _
2075 | Textentry _
2076 | View -> ()
2078 conf.fitmodel <- fitmodel;
2079 invalidate "reqlayout"
2080 (fun () ->
2081 wcmd "reqlayout %d %d %d"
2082 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
2084 | _ -> ()
2087 let settrim trimmargins trimfuzz =
2088 if nogeomcmds state.geomcmds
2089 then state.anchor <- getanchor ();
2090 conf.trimmargins <- trimmargins;
2091 conf.trimfuzz <- trimfuzz;
2092 let x0, y0, x1, y1 = trimfuzz in
2093 invalidate "settrim"
2094 (fun () ->
2095 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2096 flushpages ();
2099 let setzoom zoom =
2100 match state.throttle with
2101 | None ->
2102 let zoom = max 0.0001 zoom in
2103 if zoom <> conf.zoom
2104 then (
2105 state.prevzoom <- (conf.zoom, state.x);
2106 conf.zoom <- zoom;
2107 reshape state.winw state.winh;
2108 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2111 | Some (layout, y, started) ->
2112 let time =
2113 match conf.maxwait with
2114 | None -> 0.0
2115 | Some t -> t
2117 let dt = now () -. started in
2118 if dt > time
2119 then (
2120 state.y <- y;
2121 load layout;
2125 let setcolumns mode columns coverA coverB =
2126 state.prevcolumns <- Some (conf.columns, conf.zoom);
2127 if columns < 0
2128 then (
2129 if isbirdseye mode
2130 then showtext '!' "split mode doesn't work in bird's eye"
2131 else (
2132 conf.columns <- Csplit (-columns, E.a);
2133 state.x <- 0;
2134 conf.zoom <- 1.0;
2137 else (
2138 if columns < 2
2139 then (
2140 conf.columns <- Csingle E.a;
2141 state.x <- 0;
2142 setzoom 1.0;
2144 else (
2145 conf.columns <- Cmulti ((columns, coverA, coverB), E.a);
2146 conf.zoom <- 1.0;
2149 reshape state.winw state.winh;
2152 let resetmstate () =
2153 state.mstate <- Mnone;
2154 Wsi.setcursor Wsi.CURSOR_INHERIT;
2157 let enterbirdseye () =
2158 let zoom = float conf.thumbw /. float state.winw in
2159 let birdseyepageno =
2160 let cy = state.winh / 2 in
2161 let fold = function
2162 | [] -> 0
2163 | l :: rest ->
2164 let rec fold best = function
2165 | [] -> best.pageno
2166 | l :: rest ->
2167 let d = cy - (l.pagedispy + l.pagevh/2)
2168 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2169 if abs d < abs dbest
2170 then fold l rest
2171 else best.pageno
2172 in fold l rest
2174 fold state.layout
2176 state.mode <- Birdseye (
2177 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2179 resetmstate ();
2180 conf.zoom <- zoom;
2181 conf.presentation <- false;
2182 conf.interpagespace <- 10;
2183 conf.hlinks <- false;
2184 conf.fitmodel <- FitPage;
2185 state.x <- 0;
2186 conf.maxwait <- None;
2187 conf.columns <- (
2188 match conf.beyecolumns with
2189 | Some c ->
2190 conf.zoom <- 1.0;
2191 Cmulti ((c, 0, 0), E.a)
2192 | None -> Csingle E.a
2194 if conf.verbose
2195 then
2196 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2197 (100.0*.zoom)
2198 else
2199 state.text <- E.s
2201 reshape state.winw state.winh;
2204 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2205 state.mode <- View;
2206 conf.zoom <- c.zoom;
2207 conf.presentation <- c.presentation;
2208 conf.interpagespace <- c.interpagespace;
2209 conf.maxwait <- c.maxwait;
2210 conf.hlinks <- c.hlinks;
2211 conf.fitmodel <- c.fitmodel;
2212 conf.beyecolumns <- (
2213 match conf.columns with
2214 | Cmulti ((c, _, _), _) -> Some c
2215 | Csingle _ -> None
2216 | Csplit _ -> failwith "leaving bird's eye split mode"
2218 conf.columns <- (
2219 match c.columns with
2220 | Cmulti (c, _) -> Cmulti (c, E.a)
2221 | Csingle _ -> Csingle E.a
2222 | Csplit (c, _) -> Csplit (c, E.a)
2224 if conf.verbose
2225 then
2226 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2227 (100.0*.conf.zoom)
2229 reshape state.winw state.winh;
2230 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2231 state.x <- leftx;
2234 let togglebirdseye () =
2235 match state.mode with
2236 | Birdseye vals -> leavebirdseye vals true
2237 | View -> enterbirdseye ()
2238 | Textentry _
2239 | LinkNav _ -> ()
2242 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2243 let pageno = max 0 (pageno - incr) in
2244 let rec loop = function
2245 | [] -> gotopage1 pageno 0
2246 | l :: _ when l.pageno = pageno ->
2247 if l.pagedispy >= 0 && l.pagey = 0
2248 then G.postRedisplay "upbirdseye"
2249 else gotopage1 pageno 0
2250 | _ :: rest -> loop rest
2252 loop state.layout;
2253 state.text <- E.s;
2254 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2257 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2258 let pageno = min (state.pagecount - 1) (pageno + incr) in
2259 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2260 let rec loop = function
2261 | [] ->
2262 let y, h = getpageyh pageno in
2263 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2264 gotoy (clamp dy)
2265 | l :: _ when l.pageno = pageno ->
2266 if l.pagevh != l.pageh
2267 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2268 else G.postRedisplay "downbirdseye"
2269 | _ :: rest -> loop rest
2271 loop state.layout;
2272 state.text <- E.s;
2275 let optentry mode _ key =
2276 let btos b = if b then "on" else "off" in
2277 if key >= 32 && key < 127
2278 then
2279 let c = Char.chr key in
2280 match c with
2281 | 's' ->
2282 let ondone s =
2283 try conf.scrollstep <- int_of_string s with exc ->
2284 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2286 TEswitch ("scroll step: ", E.s, None, intentry, ondone, true)
2288 | 'A' ->
2289 let ondone s =
2291 conf.autoscrollstep <- boundastep state.winh (int_of_string s);
2292 if state.autoscroll <> None
2293 then state.autoscroll <- Some conf.autoscrollstep
2294 with exc ->
2295 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2297 TEswitch ("auto scroll step: ", E.s, None, intentry, ondone, true)
2299 | 'C' ->
2300 let ondone s =
2302 let n, a, b = multicolumns_of_string s in
2303 setcolumns mode n a b;
2304 with exc ->
2305 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
2307 TEswitch ("columns: ", E.s, None, textentry, ondone, true)
2309 | 'Z' ->
2310 let ondone s =
2312 let zoom = float (int_of_string s) /. 100.0 in
2313 setzoom zoom
2314 with exc ->
2315 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2317 TEswitch ("zoom: ", E.s, None, intentry, ondone, true)
2319 | 't' ->
2320 let ondone s =
2322 conf.thumbw <- bound (int_of_string s) 2 4096;
2323 state.text <-
2324 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2325 begin match mode with
2326 | Birdseye beye ->
2327 leavebirdseye beye false;
2328 enterbirdseye ();
2329 | Textentry _
2330 | View
2331 | LinkNav _ -> ();
2333 with exc ->
2334 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2336 TEswitch ("thumbnail width: ", E.s, None, intentry, ondone, true)
2338 | 'R' ->
2339 let ondone s =
2340 match try
2341 Some (int_of_string s)
2342 with exc ->
2343 state.text <- Printf.sprintf "bad integer `%s': %s"
2344 s (exntos exc);
2345 None
2346 with
2347 | Some angle -> reqlayout angle conf.fitmodel
2348 | None -> ()
2350 TEswitch ("rotation: ", E.s, None, intentry, ondone, true)
2352 | 'i' ->
2353 conf.icase <- not conf.icase;
2354 TEdone ("case insensitive search " ^ (btos conf.icase))
2356 | 'p' ->
2357 conf.preload <- not conf.preload;
2358 gotoy state.y;
2359 TEdone ("preload " ^ (btos conf.preload))
2361 | 'v' ->
2362 conf.verbose <- not conf.verbose;
2363 TEdone ("verbose " ^ (btos conf.verbose))
2365 | 'd' ->
2366 conf.debug <- not conf.debug;
2367 TEdone ("debug " ^ (btos conf.debug))
2369 | 'h' ->
2370 conf.maxhfit <- not conf.maxhfit;
2371 state.maxy <- calcheight ();
2372 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2374 | 'c' ->
2375 conf.crophack <- not conf.crophack;
2376 TEdone ("crophack " ^ btos conf.crophack)
2378 | 'a' ->
2379 let s =
2380 match conf.maxwait with
2381 | None ->
2382 conf.maxwait <- Some infinity;
2383 "always wait for page to complete"
2384 | Some _ ->
2385 conf.maxwait <- None;
2386 "show placeholder if page is not ready"
2388 TEdone s
2390 | 'f' ->
2391 conf.underinfo <- not conf.underinfo;
2392 TEdone ("underinfo " ^ btos conf.underinfo)
2394 | 'P' ->
2395 conf.savebmarks <- not conf.savebmarks;
2396 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2398 | 'S' ->
2399 let ondone s =
2401 let pageno, py =
2402 match state.layout with
2403 | [] -> 0, 0
2404 | l :: _ ->
2405 l.pageno, l.pagey
2407 conf.interpagespace <- int_of_string s;
2408 docolumns conf.columns;
2409 state.maxy <- calcheight ();
2410 let y = getpagey pageno in
2411 gotoy (y + py)
2412 with exc ->
2413 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2415 TEswitch ("vertical margin: ", E.s, None, intentry, ondone, true)
2417 | 'l' ->
2418 let fm =
2419 match conf.fitmodel with
2420 | FitProportional -> FitWidth
2421 | FitWidth | FitPage -> FitProportional
2423 reqlayout conf.angle fm;
2424 TEdone ("proportional display " ^ btos (fm == FitProportional))
2426 | 'T' ->
2427 settrim (not conf.trimmargins) conf.trimfuzz;
2428 TEdone ("trim margins " ^ btos conf.trimmargins)
2430 | 'I' ->
2431 conf.invert <- not conf.invert;
2432 TEdone ("invert colors " ^ btos conf.invert)
2434 | 'x' ->
2435 let ondone s =
2436 cbput state.hists.sel s;
2437 conf.selcmd <- s;
2439 TEswitch ("selection command: ", E.s, Some (onhist state.hists.sel),
2440 textentry, ondone, true)
2442 | 'M' ->
2443 if conf.pax == None
2444 then conf.pax <- Some (ref (0.0, 0, 0))
2445 else conf.pax <- None;
2446 TEdone ("PAX " ^ btos (conf.pax != None))
2448 | _ ->
2449 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2450 TEstop
2451 else
2452 TEcont state.text
2455 class type lvsource = object
2456 method getitemcount : int
2457 method getitem : int -> (string * int)
2458 method hasaction : int -> bool
2459 method exit :
2460 uioh:uioh ->
2461 cancel:bool ->
2462 active:int ->
2463 first:int ->
2464 pan:int ->
2465 uioh option
2466 method getactive : int
2467 method getfirst : int
2468 method getpan : int
2469 method getminfo : (int * int) array
2470 end;;
2472 class virtual lvsourcebase = object
2473 val mutable m_active = 0
2474 val mutable m_first = 0
2475 val mutable m_pan = 0
2476 method getactive = m_active
2477 method getfirst = m_first
2478 method getpan = m_pan
2479 method getminfo : (int * int) array = E.a
2480 end;;
2482 let textentrykeyboard
2483 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2484 let key =
2485 if key >= 0xffb0 && key <= 0xffb9
2486 then key - 0xffb0 + 48 else key
2488 let enttext te =
2489 state.mode <- Textentry (te, onleave);
2490 enttext ();
2491 G.postRedisplay "textentrykeyboard enttext";
2493 let histaction cmd =
2494 match opthist with
2495 | None -> ()
2496 | Some (action, _) ->
2497 state.mode <- Textentry (
2498 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
2500 G.postRedisplay "textentry histaction"
2502 match key with
2503 | @backspace ->
2504 if emptystr text && cancelonempty
2505 then (
2506 onleave Cancel;
2507 G.postRedisplay "textentrykeyboard after cancel";
2509 else
2510 let s = withoutlastutf8 text in
2511 enttext (c, s, opthist, onkey, ondone, cancelonempty)
2513 | @enter | @kpenter ->
2514 ondone text;
2515 onleave Confirm;
2516 G.postRedisplay "textentrykeyboard after confirm"
2518 | @up | @kpup -> histaction HCprev
2519 | @down | @kpdown -> histaction HCnext
2520 | @home | @kphome -> histaction HCfirst
2521 | @jend | @kpend -> histaction HClast
2523 | @escape ->
2524 if emptystr text
2525 then (
2526 begin match opthist with
2527 | None -> ()
2528 | Some (_, onhistcancel) -> onhistcancel ()
2529 end;
2530 onleave Cancel;
2531 state.text <- E.s;
2532 G.postRedisplay "textentrykeyboard after cancel2"
2534 else (
2535 enttext (c, E.s, opthist, onkey, ondone, cancelonempty)
2538 | @delete | @kpdelete -> ()
2540 | _ when key != 0
2541 && key land 0xff00 != 0xff00 (* keyboard *)
2542 && key land 0xfe00 != 0xfe00 (* xkb *)
2543 && key land 0xfd00 != 0xfd00 (* 3270 *)
2545 begin match onkey text key with
2546 | TEdone text ->
2547 ondone text;
2548 onleave Confirm;
2549 G.postRedisplay "textentrykeyboard after confirm2";
2551 | TEcont text ->
2552 enttext (c, text, opthist, onkey, ondone, cancelonempty);
2554 | TEstop ->
2555 onleave Cancel;
2556 G.postRedisplay "textentrykeyboard after cancel3"
2558 | TEswitch te ->
2559 state.mode <- Textentry (te, onleave);
2560 G.postRedisplay "textentrykeyboard switch";
2561 end;
2563 | _ ->
2564 vlog "unhandled key %s" (Wsi.keyname key)
2567 let firstof first active =
2568 if first > active || abs (first - active) > fstate.maxrows - 1
2569 then max 0 (active - (fstate.maxrows/2))
2570 else first
2573 let calcfirst first active =
2574 if active > first
2575 then
2576 let rows = active - first in
2577 if rows > fstate.maxrows then active - fstate.maxrows else first
2578 else active
2581 let scrollph y maxy =
2582 let sh = float (maxy + state.winh) /. float state.winh in
2583 let sh = float state.winh /. sh in
2584 let sh = max sh (float conf.scrollh) in
2586 let percent = float y /. float maxy in
2587 let position = (float state.winh -. sh) *. percent in
2589 let position =
2590 if position +. sh > float state.winh
2591 then float state.winh -. sh
2592 else position
2594 position, sh;
2597 let coe s = (s :> uioh);;
2599 class listview ~zebra ~helpmode ~(source:lvsource) ~trusted ~modehash =
2600 object (self)
2601 val m_pan = source#getpan
2602 val m_first = source#getfirst
2603 val m_active = source#getactive
2604 val m_qsearch = E.s
2605 val m_prev_uioh = state.uioh
2607 method private elemunder y =
2608 if y < 0
2609 then None
2610 else
2611 let n = y / (fstate.fontsize+1) in
2612 if m_first + n < source#getitemcount
2613 then (
2614 if source#hasaction (m_first + n)
2615 then Some (m_first + n)
2616 else None
2618 else None
2620 method display =
2621 Gl.enable `blend;
2622 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
2623 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2624 filledrect 0. 0. (float state.winw) (float state.winh);
2625 GlDraw.color (1., 1., 1.);
2626 Gl.enable `texture_2d;
2627 let fs = fstate.fontsize in
2628 let nfs = fs + 1 in
2629 let hw = (wadjsb () + xadjsb () + state.winw)/3 in
2630 let ww = fstate.wwidth in
2631 let tabw = 17.0*.ww in
2632 let itemcount = source#getitemcount in
2633 let minfo = source#getminfo in
2634 let x0, x1 =
2635 if conf.leftscroll
2636 then float (xadjsb ()), float (state.winw - 1)
2637 else 0.0, float (state.winw - conf.scrollbw - 1)
2639 let xadj = xadjsb () in
2640 let rec loop row =
2641 if (row - m_first) > fstate.maxrows
2642 then ()
2643 else (
2644 if row >= 0 && row < itemcount
2645 then (
2646 let (s, level) = source#getitem row in
2647 let y = (row - m_first) * nfs in
2648 let x =
2649 (if conf.leftscroll then float xadj else 5.0)
2650 +. (float (level + m_pan)) *. ww in
2651 if helpmode
2652 then GlDraw.color
2653 (let c = if row land 1 = 0 then 1.0 else 0.92 in (c,c,c));
2655 if row = m_active
2656 then (
2657 Gl.disable `texture_2d;
2658 let alpha = if source#hasaction row then 0.9 else 0.3 in
2659 GlDraw.color (1., 1., 1.) ~alpha;
2660 linerect (x0 +. 1.) (float (y + 1)) (x1) (float (y + fs + 3));
2661 Gl.enable `texture_2d;
2663 let c =
2664 if zebra && row land 1 = 1
2665 then 0.8
2666 else 1.0
2668 GlDraw.color (c,c,c);
2669 let drawtabularstring s =
2670 let drawstr x s =
2671 let x' = truncate (x0 +. x) in
2672 let pos = nindex s '\000' in
2673 if pos = -1
2674 then drawstring1 fs x' (y+nfs) s
2675 else
2676 let s1 = String.sub s 0 pos
2677 and s2 = String.sub s (pos+1) (String.length s - pos - 1) in
2678 let rec e s =
2679 if emptystr s
2680 then s
2681 else
2682 let s' = withoutlastutf8 s in
2683 let s = s' ^ "@Uellipsis" in
2684 let w = measurestr fs s in
2685 if float x' +. w +. ww < float (hw + x')
2686 then s
2687 else e s'
2689 let s1 =
2690 if float x' +. ww +. measurestr fs s1 > float (hw + x')
2691 then e s1
2692 else s1
2694 ignore (drawstring1 fs x' (y+nfs) s1);
2695 drawstring1 fs (hw + x') (y+nfs) s2
2697 if trusted
2698 then
2699 let x = if helpmode && row > 0 then x +. ww else x in
2700 let tabpos = nindex s '\t' in
2701 if tabpos > 0
2702 then
2703 let len = String.length s - tabpos - 1 in
2704 let s1 = String.sub s 0 tabpos
2705 and s2 = String.sub s (tabpos + 1) len in
2706 let nx = drawstr x s1 in
2707 let sw = nx -. x in
2708 let x = x +. (max tabw sw) in
2709 drawstr x s2
2710 else
2711 let len = String.length s - 2 in
2712 if len > 0 && s.[0] = '\xc2' && s.[1] = '\xb7'
2713 then
2714 let s = String.sub s 2 len in
2715 let x = if not helpmode then x +. ww else x in
2716 GlDraw.color (1.2, 1.2, 1.2);
2717 let vinc = drawstring1 (fs+fs/4)
2718 (truncate (x -. ww)) (y+nfs) s in
2719 GlDraw.color (1., 1., 1.);
2720 vinc +. (float fs *. 0.8)
2721 else
2722 drawstr x s
2723 else
2724 drawstr x s
2726 ignore (drawtabularstring s);
2727 loop (row+1)
2731 loop m_first;
2732 GlDraw.color (1.0, 1.0, 1.0) ~alpha:0.5;
2733 let xadj = float (xadjsb () + 5) in
2734 let rec loop row =
2735 if (row - m_first) > fstate.maxrows
2736 then ()
2737 else (
2738 if row >= 0 && row < itemcount
2739 then (
2740 let (s, level) = source#getitem row in
2741 let pos0 = nindex s '\000' in
2742 let y = (row - m_first) * nfs in
2743 let x = float (level + m_pan) *. ww in
2744 let (first, last) = minfo.(row) in
2745 let prefix =
2746 if pos0 > 0 && first > pos0
2747 then String.sub s (pos0+1) (first-pos0-1)
2748 else String.sub s 0 first
2750 let suffix = String.sub s first (last - first) in
2751 let w1 = measurestr fstate.fontsize prefix in
2752 let w2 = measurestr fstate.fontsize suffix in
2753 let x = x +. if conf.leftscroll then xadj else 5.0 in
2754 let x = if pos0 > 0 && first > pos0 then x +. float hw else x in
2755 let x0 = x +. w1
2756 and y0 = float (y+2) in
2757 let x1 = x0 +. w2
2758 and y1 = float (y+fs+3) in
2759 filledrect x0 y0 x1 y1;
2760 loop (row+1)
2764 Gl.disable `texture_2d;
2765 if Array.length minfo > 0 then loop m_first;
2766 Gl.disable `blend;
2768 method updownlevel incr =
2769 let len = source#getitemcount in
2770 let curlevel =
2771 if m_active >= 0 && m_active < len
2772 then snd (source#getitem m_active)
2773 else -1
2775 let rec flow i =
2776 if i = len then i-1 else if i = -1 then 0 else
2777 let _, l = source#getitem i in
2778 if l != curlevel then i else flow (i+incr)
2780 let active = flow m_active in
2781 let first = calcfirst m_first active in
2782 G.postRedisplay "outline updownlevel";
2783 {< m_active = active; m_first = first >}
2785 method private key1 key mask =
2786 let set1 active first qsearch =
2787 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2789 let search active pattern incr =
2790 let active = if active = -1 then m_first else active in
2791 let dosearch re =
2792 let rec loop n =
2793 if n >= 0 && n < source#getitemcount
2794 then (
2795 let s, _ = source#getitem n in
2797 (try ignore (Str.search_forward re s 0); true
2798 with Not_found -> false)
2799 then Some n
2800 else loop (n + incr)
2802 else None
2804 loop active
2807 let re = Str.regexp_case_fold pattern in
2808 dosearch re
2809 with Failure s ->
2810 state.text <- s;
2811 None
2813 let itemcount = source#getitemcount in
2814 let find start incr =
2815 let rec find i =
2816 if i = -1 || i = itemcount
2817 then -1
2818 else (
2819 if source#hasaction i
2820 then i
2821 else find (i + incr)
2824 find start
2826 let set active first =
2827 let first = bound first 0 (itemcount - fstate.maxrows) in
2828 state.text <- E.s;
2829 coe {< m_active = active; m_first = first; m_qsearch = E.s >}
2831 let navigate incr =
2832 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2833 let active, first =
2834 let incr1 = if incr > 0 then 1 else -1 in
2835 if isvisible m_first m_active
2836 then
2837 let next =
2838 let next = m_active + incr in
2839 let next =
2840 if next < 0 || next >= itemcount
2841 then -1
2842 else find next incr1
2844 if abs (m_active - next) > fstate.maxrows
2845 then -1
2846 else next
2848 if next = -1
2849 then
2850 let first = m_first + incr in
2851 let first = bound first 0 (itemcount - fstate.maxrows) in
2852 let next =
2853 let next = m_active + incr in
2854 let next = bound next 0 (itemcount - 1) in
2855 find next ~-incr1
2857 let active =
2858 if next = -1
2859 then m_active
2860 else (
2861 if isvisible first next
2862 then next
2863 else m_active
2866 active, first
2867 else
2868 let first = min next m_first in
2869 let first =
2870 if abs (next - first) > fstate.maxrows
2871 then first + incr
2872 else first
2874 next, first
2875 else
2876 let first = m_first + incr in
2877 let first = bound first 0 (itemcount - 1) in
2878 let active =
2879 let next = m_active + incr in
2880 let next = bound next 0 (itemcount - 1) in
2881 let next = find next incr1 in
2882 let active =
2883 if next = -1 || abs (m_active - first) > fstate.maxrows
2884 then (
2885 let active = if m_active = -1 then next else m_active in
2886 active
2888 else next
2890 if isvisible first active
2891 then active
2892 else -1
2894 active, first
2896 G.postRedisplay "listview navigate";
2897 set active first;
2899 match key with
2900 | (@r|@s) when Wsi.withctrl mask ->
2901 let incr = if key = @r then -1 else 1 in
2902 let active, first =
2903 match search (m_active + incr) m_qsearch incr with
2904 | None ->
2905 state.text <- m_qsearch ^ " [not found]";
2906 m_active, m_first
2907 | Some active ->
2908 state.text <- m_qsearch;
2909 active, firstof m_first active
2911 G.postRedisplay "listview ctrl-r/s";
2912 set1 active first m_qsearch;
2914 | @insert when Wsi.withctrl mask ->
2915 if m_active >= 0 && m_active < source#getitemcount
2916 then (
2917 let s, _ = source#getitem m_active in
2918 selstring s;
2920 coe self
2922 | @backspace ->
2923 if emptystr m_qsearch
2924 then coe self
2925 else (
2926 let qsearch = withoutlastutf8 m_qsearch in
2927 if emptystr qsearch
2928 then (
2929 state.text <- E.s;
2930 G.postRedisplay "listview empty qsearch";
2931 set1 m_active m_first E.s;
2933 else
2934 let active, first =
2935 match search m_active qsearch ~-1 with
2936 | None ->
2937 state.text <- qsearch ^ " [not found]";
2938 m_active, m_first
2939 | Some active ->
2940 state.text <- qsearch;
2941 active, firstof m_first active
2943 G.postRedisplay "listview backspace qsearch";
2944 set1 active first qsearch
2947 | key when (key != 0 && key land 0xff00 != 0xff00) ->
2948 let pattern = m_qsearch ^ toutf8 key in
2949 let active, first =
2950 match search m_active pattern 1 with
2951 | None ->
2952 state.text <- pattern ^ " [not found]";
2953 m_active, m_first
2954 | Some active ->
2955 state.text <- pattern;
2956 active, firstof m_first active
2958 G.postRedisplay "listview qsearch add";
2959 set1 active first pattern;
2961 | @escape ->
2962 state.text <- E.s;
2963 if emptystr m_qsearch
2964 then (
2965 G.postRedisplay "list view escape";
2966 let mx, my = state.mpos in
2967 updateunder mx my;
2968 begin
2969 match
2970 source#exit ~uioh:(coe self)
2971 ~cancel:true ~active:m_active ~first:m_first ~pan:m_pan
2972 with
2973 | None -> m_prev_uioh
2974 | Some uioh -> uioh
2977 else (
2978 G.postRedisplay "list view kill qsearch";
2979 coe {< m_qsearch = E.s >}
2982 | @enter | @kpenter ->
2983 state.text <- E.s;
2984 let self = {< m_qsearch = E.s >} in
2985 let opt =
2986 G.postRedisplay "listview enter";
2987 if m_active >= 0 && m_active < source#getitemcount
2988 then (
2989 source#exit ~uioh:(coe self) ~cancel:false
2990 ~active:m_active ~first:m_first ~pan:m_pan;
2992 else (
2993 source#exit ~uioh:(coe self) ~cancel:true
2994 ~active:m_active ~first:m_first ~pan:m_pan;
2997 begin match opt with
2998 | None -> m_prev_uioh
2999 | Some uioh -> uioh
3002 | @delete | @kpdelete ->
3003 coe self
3005 | @up | @kpup -> navigate ~-1
3006 | @down | @kpdown -> navigate 1
3007 | @prior | @kpprior -> navigate ~-(fstate.maxrows)
3008 | @next | @kpnext -> navigate fstate.maxrows
3010 | @right | @kpright ->
3011 state.text <- E.s;
3012 G.postRedisplay "listview right";
3013 coe {< m_pan = m_pan - 1 >}
3015 | @left | @kpleft ->
3016 state.text <- E.s;
3017 G.postRedisplay "listview left";
3018 coe {< m_pan = m_pan + 1 >}
3020 | @home | @kphome ->
3021 let active = find 0 1 in
3022 G.postRedisplay "listview home";
3023 set active 0;
3025 | @jend | @kpend ->
3026 let first = max 0 (itemcount - fstate.maxrows) in
3027 let active = find (itemcount - 1) ~-1 in
3028 G.postRedisplay "listview end";
3029 set active first;
3031 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3032 coe self
3034 | _ ->
3035 dolog "listview unknown key %#x" key; coe self
3037 method key key mask =
3038 match state.mode with
3039 | Textentry te -> textentrykeyboard key mask te; coe self
3040 | Birdseye _
3041 | View
3042 | LinkNav _ -> self#key1 key mask
3044 method button button down x y _ =
3045 let opt =
3046 match button with
3047 | 1 when x > state.winw - conf.scrollbw ->
3048 G.postRedisplay "listview scroll";
3049 if down
3050 then
3051 let _, position, sh = self#scrollph in
3052 if y > truncate position && y < truncate (position +. sh)
3053 then (
3054 state.mstate <- Mscrolly;
3055 Some (coe self)
3057 else
3058 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3059 let first = truncate (s *. float source#getitemcount) in
3060 let first = min source#getitemcount first in
3061 Some (coe {< m_first = first; m_active = first >})
3062 else (
3063 state.mstate <- Mnone;
3064 Some (coe self);
3066 | 1 when down ->
3067 begin match self#elemunder y with
3068 | Some n ->
3069 G.postRedisplay "listview click";
3070 source#exit ~uioh:(coe {< m_active = n >})
3071 ~cancel:false ~active:n ~first:m_first ~pan:m_pan
3072 | _ ->
3073 Some (coe self)
3075 | n when (n == 4 || n == 5) && not down ->
3076 let len = source#getitemcount in
3077 let first =
3078 if n = 5 && m_first + fstate.maxrows >= len
3079 then
3080 m_first
3081 else
3082 let first = m_first + (if n == 4 then -1 else 1) in
3083 bound first 0 (len - 1)
3085 G.postRedisplay "listview wheel";
3086 Some (coe {< m_first = first >})
3087 | n when (n = 6 || n = 7) && not down ->
3088 let inc = if n = 7 then -1 else 1 in
3089 G.postRedisplay "listview hwheel";
3090 Some (coe {< m_pan = m_pan + inc >})
3091 | _ ->
3092 Some (coe self)
3094 match opt with
3095 | None -> m_prev_uioh
3096 | Some uioh -> uioh
3098 method multiclick _ x y = self#button 1 true x y
3100 method motion _ y =
3101 match state.mstate with
3102 | Mscrolly ->
3103 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3104 let first = truncate (s *. float source#getitemcount) in
3105 let first = min source#getitemcount first in
3106 G.postRedisplay "listview motion";
3107 coe {< m_first = first; m_active = first >}
3108 | Msel _
3109 | Mpan _
3110 | Mscrollx
3111 | Mzoom _
3112 | Mzoomrect _
3113 | Mnone -> coe self
3115 method pmotion x y =
3116 if x < state.winw - conf.scrollbw
3117 then
3118 let n =
3119 match self#elemunder y with
3120 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3121 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3123 let o =
3124 if n != m_active
3125 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3126 else self
3128 coe o
3129 else (
3130 Wsi.setcursor Wsi.CURSOR_INHERIT;
3131 coe self
3134 method infochanged _ = ()
3136 method scrollpw = (0, 0.0, 0.0)
3137 method scrollph =
3138 let nfs = fstate.fontsize + 1 in
3139 let y = m_first * nfs in
3140 let itemcount = source#getitemcount in
3141 let maxi = max 0 (itemcount - fstate.maxrows) in
3142 let maxy = maxi * nfs in
3143 let p, h = scrollph y maxy in
3144 conf.scrollbw, p, h
3146 method modehash = modehash
3147 method eformsgs = false
3148 method alwaysscrolly = true
3149 end;;
3151 class outlinelistview ~zebra ~source =
3152 let settext autonarrow s =
3153 if autonarrow
3154 then
3155 let ss = source#statestr in
3156 state.text <-
3157 if emptystr ss
3158 then "[" ^ s ^ "]"
3159 else "{" ^ ss ^ "} [" ^ s ^ "]"
3160 else state.text <- s
3162 object (self)
3163 inherit listview
3164 ~zebra
3165 ~helpmode:false
3166 ~source:(source :> lvsource)
3167 ~trusted:false
3168 ~modehash:(findkeyhash conf "outline")
3169 as super
3171 val m_autonarrow = false
3173 method! key key mask =
3174 let maxrows =
3175 if emptystr state.text
3176 then fstate.maxrows
3177 else fstate.maxrows - 2
3179 let calcfirst first active =
3180 if active > first
3181 then
3182 let rows = active - first in
3183 if rows > maxrows then active - maxrows else first
3184 else active
3186 let navigate incr =
3187 let active = m_active + incr in
3188 let active = bound active 0 (source#getitemcount - 1) in
3189 let first = calcfirst m_first active in
3190 G.postRedisplay "outline navigate";
3191 coe {< m_active = active; m_first = first >}
3193 let navscroll first =
3194 let active =
3195 let dist = m_active - first in
3196 if dist < 0
3197 then first
3198 else (
3199 if dist < maxrows
3200 then m_active
3201 else first + maxrows
3204 G.postRedisplay "outline navscroll";
3205 coe {< m_first = first; m_active = active >}
3207 let ctrl = Wsi.withctrl mask in
3208 match key with
3209 | @a when ctrl ->
3210 let text =
3211 if m_autonarrow
3212 then (source#denarrow; E.s)
3213 else (
3214 let pattern = source#renarrow in
3215 if nonemptystr m_qsearch
3216 then (source#narrow m_qsearch; m_qsearch)
3217 else pattern
3220 settext (not m_autonarrow) text;
3221 G.postRedisplay "toggle auto narrowing";
3222 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
3224 | @slash when emptystr m_qsearch && not m_autonarrow ->
3225 settext true E.s;
3226 G.postRedisplay "toggle auto narrowing";
3227 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
3229 | @n when ctrl ->
3230 source#narrow m_qsearch;
3231 if not m_autonarrow
3232 then source#add_narrow_pattern m_qsearch;
3233 G.postRedisplay "outline ctrl-n";
3234 coe {< m_first = 0; m_active = 0 >}
3236 | @S when ctrl ->
3237 let active = source#calcactive (getanchor ()) in
3238 let first = firstof m_first active in
3239 G.postRedisplay "outline ctrl-s";
3240 coe {< m_first = first; m_active = active >}
3242 | @u when ctrl ->
3243 G.postRedisplay "outline ctrl-u";
3244 if m_autonarrow && nonemptystr m_qsearch
3245 then (
3246 ignore (source#renarrow);
3247 settext m_autonarrow E.s;
3248 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3250 else (
3251 source#del_narrow_pattern;
3252 let pattern = source#renarrow in
3253 let text =
3254 if emptystr pattern then E.s else "Narrowed to " ^ pattern
3256 settext m_autonarrow text;
3257 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3260 | @l when ctrl ->
3261 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3262 G.postRedisplay "outline ctrl-l";
3263 coe {< m_first = first >}
3265 | @tab when m_autonarrow ->
3266 if nonemptystr m_qsearch
3267 then (
3268 G.postRedisplay "outline list view tab";
3269 source#add_narrow_pattern m_qsearch;
3270 settext true E.s;
3271 coe {< m_qsearch = E.s >}
3273 else coe self
3275 | @escape when m_autonarrow ->
3276 if nonemptystr m_qsearch
3277 then source#add_narrow_pattern m_qsearch;
3278 super#key key mask
3280 | @enter | @kpenter when m_autonarrow ->
3281 if nonemptystr m_qsearch
3282 then source#add_narrow_pattern m_qsearch;
3283 super#key key mask
3285 | key when m_autonarrow && (key != 0 && key land 0xff00 != 0xff00) ->
3286 let pattern = m_qsearch ^ toutf8 key in
3287 G.postRedisplay "outlinelistview autonarrow add";
3288 source#narrow pattern;
3289 settext true pattern;
3290 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3292 | key when m_autonarrow && key = @backspace ->
3293 if emptystr m_qsearch
3294 then coe self
3295 else
3296 let pattern = withoutlastutf8 m_qsearch in
3297 G.postRedisplay "outlinelistview autonarrow backspace";
3298 ignore (source#renarrow);
3299 source#narrow pattern;
3300 settext true pattern;
3301 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3303 | @delete | @kpdelete ->
3304 source#remove m_active;
3305 G.postRedisplay "outline delete";
3306 let active = max 0 (m_active-1) in
3307 coe {< m_first = firstof m_first active;
3308 m_active = active >}
3310 | @up | @kpup when ctrl ->
3311 navscroll (max 0 (m_first - 1))
3313 | @down | @kpdown when ctrl ->
3314 navscroll (min (source#getitemcount - 1) (m_first + 1))
3316 | @up | @kpup -> navigate ~-1
3317 | @down | @kpdown -> navigate 1
3318 | @prior | @kpprior -> navigate ~-(fstate.maxrows)
3319 | @next | @kpnext -> navigate fstate.maxrows
3321 | @right | @kpright ->
3322 let o =
3323 if ctrl
3324 then (
3325 G.postRedisplay "outline ctrl right";
3326 {< m_pan = m_pan + 1 >}
3328 else self#updownlevel 1
3330 coe o
3332 | @left | @kpleft ->
3333 let o =
3334 if ctrl
3335 then (
3336 G.postRedisplay "outline ctrl left";
3337 {< m_pan = m_pan - 1 >}
3339 else self#updownlevel ~-1
3341 coe o
3343 | @home | @kphome ->
3344 G.postRedisplay "outline home";
3345 coe {< m_first = 0; m_active = 0 >}
3347 | @jend | @kpend ->
3348 let active = source#getitemcount - 1 in
3349 let first = max 0 (active - fstate.maxrows) in
3350 G.postRedisplay "outline end";
3351 coe {< m_active = active; m_first = first >}
3353 | _ -> super#key key mask
3356 let genhistoutlines =
3357 let order ty (p1, c1, _, _, _, _) (p2, c2, _, _, _, _) =
3358 match ty with
3359 | `lastvisit -> compare c1.lastvisit c2.lastvisit
3360 | `path -> compare p2 p1
3361 | `file -> compare (Filename.basename p2) (Filename.basename p1)
3362 | `title ->
3363 let e1 = emptystr c1.title
3364 and e2 = emptystr c2.title in
3365 (**) if e1 && e2
3366 then compare (Filename.basename p2) (Filename.basename p1)
3367 else if e1 then -1
3368 else if e2 then 1
3369 else compare c1.title c2.title
3371 let showfullpath = ref false in
3372 let showorigin = ref true in
3373 fun orderty ->
3374 let setorty s t =
3375 let s = if orderty = t then "[@Uradical] " ^ s else "[ ] " ^ s in
3376 s, 0, Oaction (fun () -> Config.historder := t; reeenterhist := true)
3378 let list = ref [] in
3379 if Config.gethist list
3380 then
3381 let ol =
3382 List.fold_left
3383 (fun accu (path, c, b, x, a, o) ->
3384 let hist = (path, (c, b, x, a, o)) in
3385 let s =
3386 let s = if nonemptystr o && !showorigin then o else path in
3387 if !showfullpath then s else Filename.basename s
3389 let base = mbtoutf8 s in
3390 (base ^ "\000" ^ c.title, 1, Ohistory hist) :: accu
3392 [ setorty "Sort by time of last visit" `lastvisit;
3393 setorty "Sort by file name" `file;
3394 setorty "Sort by path" `path;
3395 setorty "Sort by title" `title;
3396 (if !showfullpath then "@Uradical "
3397 else " ") ^ "Show full path", 0, Oaction (fun () ->
3398 showfullpath := not !showfullpath; reeenterhist := true);
3399 (if !showorigin then "@Uradical "
3400 else " ") ^ "Show origin", 0, Oaction (fun () ->
3401 showorigin := not !showorigin; reeenterhist := true)
3402 ] (List.sort (order orderty) !list)
3404 Array.of_list ol
3405 else E.a;
3408 let gotohist (path, (c, bookmarks, x, anchor, origin)) =
3409 Config.save leavebirdseye;
3410 state.anchor <- anchor;
3411 state.bookmarks <- bookmarks;
3412 state.origin <- origin;
3413 state.x <- x;
3414 setconf conf c;
3415 let x0, y0, x1, y1 = conf.trimfuzz in
3416 wcmd "trimset %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1;
3417 reshape ~firsttime:true state.winw state.winh;
3418 opendoc path origin;
3419 setzoom c.zoom;
3422 let makecheckers () =
3423 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3424 following to say:
3425 converted by Issac Trotts. July 25, 2002 *)
3426 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3427 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3428 let id = GlTex.gen_texture () in
3429 GlTex.bind_texture ~target:`texture_2d id;
3430 GlPix.store (`unpack_alignment 1);
3431 GlTex.image2d image;
3432 List.iter (GlTex.parameter ~target:`texture_2d)
3433 [ `mag_filter `nearest; `min_filter `nearest ];
3437 let setcheckers enabled =
3438 match state.checkerstexid with
3439 | None ->
3440 if enabled then state.checkerstexid <- Some (makecheckers ())
3442 | Some checkerstexid ->
3443 if not enabled
3444 then (
3445 GlTex.delete_texture checkerstexid;
3446 state.checkerstexid <- None;
3450 let describe_location () =
3451 let fn = page_of_y state.y in
3452 let ln = page_of_y (state.y + state.winh - hscrollh () - 1) in
3453 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
3454 let percent =
3455 if maxy <= 0
3456 then 100.
3457 else (100. *. (float state.y /. float maxy))
3459 if fn = ln
3460 then
3461 Printf.sprintf "page %d of %d [%.2f%%]"
3462 (fn+1) state.pagecount percent
3463 else
3464 Printf.sprintf
3465 "pages %d-%d of %d [%.2f%%]"
3466 (fn+1) (ln+1) state.pagecount percent
3469 let setpresentationmode v =
3470 let n = page_of_y state.y in
3471 state.anchor <- (n, 0.0, 1.0);
3472 conf.presentation <- v;
3473 if conf.fitmodel = FitPage
3474 then reqlayout conf.angle conf.fitmodel;
3475 represent ();
3478 let enterinfomode =
3479 let btos b = if b then "@Uradical" else E.s in
3480 let showextended = ref false in
3481 let leave mode _ = state.mode <- mode in
3482 let src =
3483 (object
3484 val mutable m_first_time = true
3485 val mutable m_l = []
3486 val mutable m_a = E.a
3487 val mutable m_prev_uioh = nouioh
3488 val mutable m_prev_mode = View
3490 inherit lvsourcebase
3492 method reset prev_mode prev_uioh =
3493 m_a <- Array.of_list (List.rev m_l);
3494 m_l <- [];
3495 m_prev_mode <- prev_mode;
3496 m_prev_uioh <- prev_uioh;
3497 if m_first_time
3498 then (
3499 let rec loop n =
3500 if n >= Array.length m_a
3501 then ()
3502 else
3503 match m_a.(n) with
3504 | _, _, _, Action _ -> m_active <- n
3505 | _, _, _, Noaction -> loop (n+1)
3507 loop 0;
3508 m_first_time <- false;
3511 method int name get set =
3512 m_l <-
3513 (name, `int get, 1, Action (
3514 fun u ->
3515 let ondone s =
3516 try set (int_of_string s)
3517 with exn ->
3518 state.text <- Printf.sprintf "bad integer `%s': %s"
3519 s (exntos exn)
3521 state.text <- E.s;
3522 let te = name ^ ": ", E.s, None, intentry, ondone, true in
3523 state.mode <- Textentry (te, leave m_prev_mode);
3525 )) :: m_l
3527 method int_with_suffix name get set =
3528 m_l <-
3529 (name, `intws get, 1, Action (
3530 fun u ->
3531 let ondone s =
3532 try set (int_of_string_with_suffix s)
3533 with exn ->
3534 state.text <- Printf.sprintf "bad integer `%s': %s"
3535 s (exntos exn)
3537 state.text <- E.s;
3538 let te =
3539 name ^ ": ", E.s, None, intentry_with_suffix, ondone, true
3541 state.mode <- Textentry (te, leave m_prev_mode);
3543 )) :: m_l
3545 method bool ?(offset=1) ?(btos=btos) name get set =
3546 m_l <-
3547 (name, `bool (btos, get), offset, Action (
3548 fun u ->
3549 let v = get () in
3550 set (not v);
3552 )) :: m_l
3554 method color name get set =
3555 m_l <-
3556 (name, `color get, 1, Action (
3557 fun u ->
3558 let invalid = (nan, nan, nan) in
3559 let ondone s =
3560 let c =
3561 try color_of_string s
3562 with exn ->
3563 state.text <- Printf.sprintf "bad color `%s': %s"
3564 s (exntos exn);
3565 invalid
3567 if c <> invalid
3568 then set c;
3570 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3571 state.text <- color_to_string (get ());
3572 state.mode <- Textentry (te, leave m_prev_mode);
3574 )) :: m_l
3576 method string name get set =
3577 m_l <-
3578 (name, `string get, 1, Action (
3579 fun u ->
3580 let ondone s = set s in
3581 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3582 state.mode <- Textentry (te, leave m_prev_mode);
3584 )) :: m_l
3586 method colorspace name get set =
3587 m_l <-
3588 (name, `string get, 1, Action (
3589 fun _ ->
3590 let source =
3591 (object
3592 inherit lvsourcebase
3594 initializer
3595 m_active <- CSTE.to_int conf.colorspace;
3596 m_first <- 0;
3598 method getitemcount =
3599 Array.length CSTE.names
3600 method getitem n =
3601 (CSTE.names.(n), 0)
3602 method exit ~uioh ~cancel ~active ~first ~pan =
3603 ignore (uioh, first, pan);
3604 if not cancel then set active;
3605 None
3606 method hasaction _ = true
3607 end)
3609 state.text <- E.s;
3610 let modehash = findkeyhash conf "info" in
3611 coe (new listview ~zebra:false ~helpmode:false
3612 ~source ~trusted:true ~modehash)
3613 )) :: m_l
3615 method paxmark name get set =
3616 m_l <-
3617 (name, `string get, 1, Action (
3618 fun _ ->
3619 let source =
3620 (object
3621 inherit lvsourcebase
3623 initializer
3624 m_active <- MTE.to_int conf.paxmark;
3625 m_first <- 0;
3627 method getitemcount = Array.length MTE.names
3628 method getitem n = (MTE.names.(n), 0)
3629 method exit ~uioh ~cancel ~active ~first ~pan =
3630 ignore (uioh, first, pan);
3631 if not cancel then set active;
3632 None
3633 method hasaction _ = true
3634 end)
3636 state.text <- E.s;
3637 let modehash = findkeyhash conf "info" in
3638 coe (new listview ~zebra:false ~helpmode:false
3639 ~source ~trusted:true ~modehash)
3640 )) :: m_l
3642 method fitmodel name get set =
3643 m_l <-
3644 (name, `string get, 1, Action (
3645 fun _ ->
3646 let source =
3647 (object
3648 inherit lvsourcebase
3650 initializer
3651 m_active <- FMTE.to_int conf.fitmodel;
3652 m_first <- 0;
3654 method getitemcount = Array.length FMTE.names
3655 method getitem n = (FMTE.names.(n), 0)
3656 method exit ~uioh ~cancel ~active ~first ~pan =
3657 ignore (uioh, first, pan);
3658 if not cancel then set active;
3659 None
3660 method hasaction _ = true
3661 end)
3663 state.text <- E.s;
3664 let modehash = findkeyhash conf "info" in
3665 coe (new listview ~zebra:false ~helpmode:false
3666 ~source ~trusted:true ~modehash)
3667 )) :: m_l
3669 method caption s offset =
3670 m_l <- (s, `empty, offset, Noaction) :: m_l
3672 method caption2 s f offset =
3673 m_l <- (s, `string f, offset, Noaction) :: m_l
3675 method getitemcount = Array.length m_a
3677 method getitem n =
3678 let tostr = function
3679 | `int f -> string_of_int (f ())
3680 | `intws f -> string_with_suffix_of_int (f ())
3681 | `string f -> f ()
3682 | `color f -> color_to_string (f ())
3683 | `bool (btos, f) -> btos (f ())
3684 | `empty -> E.s
3686 let name, t, offset, _ = m_a.(n) in
3687 ((let s = tostr t in
3688 if nonemptystr s
3689 then Printf.sprintf "%s\t%s" name s
3690 else name),
3691 offset)
3693 method exit ~uioh ~cancel ~active ~first ~pan =
3694 let uiohopt =
3695 if not cancel
3696 then (
3697 let uioh =
3698 match m_a.(active) with
3699 | _, _, _, Action f -> f uioh
3700 | _, _, _, Noaction -> uioh
3702 Some uioh
3704 else None
3706 m_active <- active;
3707 m_first <- first;
3708 m_pan <- pan;
3709 uiohopt
3711 method hasaction n =
3712 match m_a.(n) with
3713 | _, _, _, Action _ -> true
3714 | _, _, _, Noaction -> false
3715 end)
3717 let rec fillsrc prevmode prevuioh =
3718 let sep () = src#caption E.s 0 in
3719 let colorp name get set =
3720 src#string name
3721 (fun () -> color_to_string (get ()))
3722 (fun v ->
3724 let c = color_of_string v in
3725 set c
3726 with exn ->
3727 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
3730 let oldmode = state.mode in
3731 let birdseye = isbirdseye state.mode in
3733 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3735 src#bool "presentation mode"
3736 (fun () -> conf.presentation)
3737 (fun v -> setpresentationmode v);
3739 src#bool "ignore case in searches"
3740 (fun () -> conf.icase)
3741 (fun v -> conf.icase <- v);
3743 src#bool "preload"
3744 (fun () -> conf.preload)
3745 (fun v -> conf.preload <- v);
3747 src#bool "highlight links"
3748 (fun () -> conf.hlinks)
3749 (fun v -> conf.hlinks <- v);
3751 src#bool "under info"
3752 (fun () -> conf.underinfo)
3753 (fun v -> conf.underinfo <- v);
3755 src#bool "persistent bookmarks"
3756 (fun () -> conf.savebmarks)
3757 (fun v -> conf.savebmarks <- v);
3759 src#fitmodel "fit model"
3760 (fun () -> FMTE.to_string conf.fitmodel)
3761 (fun v -> reqlayout conf.angle (FMTE.of_int v));
3763 src#bool "trim margins"
3764 (fun () -> conf.trimmargins)
3765 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3767 src#bool "persistent location"
3768 (fun () -> conf.jumpback)
3769 (fun v -> conf.jumpback <- v);
3771 sep ();
3772 src#int "inter-page space"
3773 (fun () -> conf.interpagespace)
3774 (fun n ->
3775 conf.interpagespace <- n;
3776 docolumns conf.columns;
3777 let pageno, py =
3778 match state.layout with
3779 | [] -> 0, 0
3780 | l :: _ ->
3781 l.pageno, l.pagey
3783 state.maxy <- calcheight ();
3784 let y = getpagey pageno in
3785 gotoy (y + py)
3788 src#int "page bias"
3789 (fun () -> conf.pagebias)
3790 (fun v -> conf.pagebias <- v);
3792 src#int "scroll step"
3793 (fun () -> conf.scrollstep)
3794 (fun n -> conf.scrollstep <- n);
3796 src#int "horizontal scroll step"
3797 (fun () -> conf.hscrollstep)
3798 (fun v -> conf.hscrollstep <- v);
3800 src#int "auto scroll step"
3801 (fun () ->
3802 match state.autoscroll with
3803 | Some step -> step
3804 | _ -> conf.autoscrollstep)
3805 (fun n ->
3806 let n = boundastep state.winh n in
3807 if state.autoscroll <> None
3808 then state.autoscroll <- Some n;
3809 conf.autoscrollstep <- n);
3811 src#int "zoom"
3812 (fun () -> truncate (conf.zoom *. 100.))
3813 (fun v -> setzoom ((float v) /. 100.));
3815 src#int "rotation"
3816 (fun () -> conf.angle)
3817 (fun v -> reqlayout v conf.fitmodel);
3819 src#int "scroll bar width"
3820 (fun () -> conf.scrollbw)
3821 (fun v ->
3822 conf.scrollbw <- v;
3823 reshape state.winw state.winh;
3826 src#int "scroll handle height"
3827 (fun () -> conf.scrollh)
3828 (fun v -> conf.scrollh <- v;);
3830 src#int "thumbnail width"
3831 (fun () -> conf.thumbw)
3832 (fun v ->
3833 conf.thumbw <- min 4096 v;
3834 match oldmode with
3835 | Birdseye beye ->
3836 leavebirdseye beye false;
3837 enterbirdseye ()
3838 | Textentry _
3839 | View
3840 | LinkNav _ -> ()
3843 let mode = state.mode in
3844 src#string "columns"
3845 (fun () ->
3846 match conf.columns with
3847 | Csingle _ -> "1"
3848 | Cmulti (multi, _) -> multicolumns_to_string multi
3849 | Csplit (count, _) -> "-" ^ string_of_int count
3851 (fun v ->
3852 let n, a, b = multicolumns_of_string v in
3853 setcolumns mode n a b);
3855 sep ();
3856 src#caption "Pixmap cache" 0;
3857 src#int_with_suffix "size (advisory)"
3858 (fun () -> conf.memlimit)
3859 (fun v -> conf.memlimit <- v);
3861 src#caption2 "used"
3862 (fun () -> Printf.sprintf "%s bytes, %d tiles"
3863 (string_with_suffix_of_int state.memused)
3864 (Hashtbl.length state.tilemap)) 1;
3866 sep ();
3867 src#caption "Layout" 0;
3868 src#caption2 "Dimension"
3869 (fun () ->
3870 Printf.sprintf "%dx%d (virtual %dx%d)"
3871 state.winw state.winh
3872 state.w state.maxy)
3874 if conf.debug
3875 then
3876 src#caption2 "Position" (fun () ->
3877 Printf.sprintf "%dx%d" state.x state.y
3879 else
3880 src#caption2 "Position" (fun () -> describe_location ()) 1
3883 sep ();
3884 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3885 "Save these parameters as global defaults at exit"
3886 (fun () -> conf.bedefault)
3887 (fun v -> conf.bedefault <- v)
3890 sep ();
3891 let btos b = if b then "@Ulguillemet" else "@Urguillemet" in
3892 src#bool ~offset:0 ~btos "Extended parameters"
3893 (fun () -> !showextended)
3894 (fun v -> showextended := v; fillsrc prevmode prevuioh);
3895 if !showextended
3896 then (
3897 src#bool "checkers"
3898 (fun () -> conf.checkers)
3899 (fun v -> conf.checkers <- v; setcheckers v);
3900 src#bool "update cursor"
3901 (fun () -> conf.updatecurs)
3902 (fun v -> conf.updatecurs <- v);
3903 src#bool "scroll-bar on the left"
3904 (fun () -> conf.leftscroll)
3905 (fun v -> conf.leftscroll <- v);
3906 src#bool "verbose"
3907 (fun () -> conf.verbose)
3908 (fun v -> conf.verbose <- v);
3909 src#bool "invert colors"
3910 (fun () -> conf.invert)
3911 (fun v -> conf.invert <- v);
3912 src#bool "max fit"
3913 (fun () -> conf.maxhfit)
3914 (fun v -> conf.maxhfit <- v);
3915 src#bool "redirect stderr"
3916 (fun () -> conf.redirectstderr)
3917 (fun v -> conf.redirectstderr <- v; redirectstderr ());
3918 src#bool "pax mode"
3919 (fun () -> conf.pax != None)
3920 (fun v ->
3921 if v
3922 then conf.pax <- Some (ref (now (), 0, 0))
3923 else conf.pax <- None);
3924 src#string "uri launcher"
3925 (fun () -> conf.urilauncher)
3926 (fun v -> conf.urilauncher <- v);
3927 src#string "path launcher"
3928 (fun () -> conf.pathlauncher)
3929 (fun v -> conf.pathlauncher <- v);
3930 src#string "tile size"
3931 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3932 (fun v ->
3934 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
3935 conf.tilew <- max 64 w;
3936 conf.tileh <- max 64 h;
3937 flushtiles ();
3938 with exn ->
3939 state.text <- Printf.sprintf "bad tile size `%s': %s"
3940 v (exntos exn)
3942 src#int "texture count"
3943 (fun () -> conf.texcount)
3944 (fun v ->
3945 if realloctexts v
3946 then conf.texcount <- v
3947 else showtext '!' " Failed to set texture count please retry later"
3949 src#int "slice height"
3950 (fun () -> conf.sliceheight)
3951 (fun v ->
3952 conf.sliceheight <- v;
3953 wcmd "sliceh %d" conf.sliceheight;
3955 src#int "anti-aliasing level"
3956 (fun () -> conf.aalevel)
3957 (fun v ->
3958 conf.aalevel <- bound v 0 8;
3959 state.anchor <- getanchor ();
3960 opendoc state.path state.password;
3962 src#string "page scroll scaling factor"
3963 (fun () -> string_of_float conf.pgscale)
3964 (fun v ->
3966 let s = float_of_string v in
3967 conf.pgscale <- s
3968 with exn ->
3969 state.text <- Printf.sprintf
3970 "bad page scroll scaling factor `%s': %s" v (exntos exn)
3973 src#int "ui font size"
3974 (fun () -> fstate.fontsize)
3975 (fun v -> setfontsize (bound v 5 100));
3976 src#int "hint font size"
3977 (fun () -> conf.hfsize)
3978 (fun v -> conf.hfsize <- bound v 5 100);
3979 colorp "background color"
3980 (fun () -> conf.bgcolor)
3981 (fun v -> conf.bgcolor <- v);
3982 src#bool "crop hack"
3983 (fun () -> conf.crophack)
3984 (fun v -> conf.crophack <- v);
3985 src#string "trim fuzz"
3986 (fun () -> irect_to_string conf.trimfuzz)
3987 (fun v ->
3989 conf.trimfuzz <- irect_of_string v;
3990 if conf.trimmargins
3991 then settrim true conf.trimfuzz;
3992 with exn ->
3993 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
3995 src#string "throttle"
3996 (fun () ->
3997 match conf.maxwait with
3998 | None -> "show place holder if page is not ready"
3999 | Some time ->
4000 if time = infinity
4001 then "wait for page to fully render"
4002 else
4003 "wait " ^ string_of_float time
4004 ^ " seconds before showing placeholder"
4006 (fun v ->
4008 let f = float_of_string v in
4009 if f <= 0.0
4010 then conf.maxwait <- None
4011 else conf.maxwait <- Some f
4012 with exn ->
4013 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
4015 src#string "ghyll scroll"
4016 (fun () ->
4017 match conf.ghyllscroll with
4018 | None -> E.s
4019 | Some nab -> ghyllscroll_to_string nab
4021 (fun v ->
4022 try conf.ghyllscroll <- ghyllscroll_of_string v
4023 with exn ->
4024 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
4026 src#string "selection command"
4027 (fun () -> conf.selcmd)
4028 (fun v -> conf.selcmd <- v);
4029 src#string "synctex command"
4030 (fun () -> conf.stcmd)
4031 (fun v -> conf.stcmd <- v);
4032 src#string "pax command"
4033 (fun () -> conf.paxcmd)
4034 (fun v -> conf.paxcmd <- v);
4035 src#string "ask password command"
4036 (fun () -> conf.passcmd)
4037 (fun v -> conf.passcmd <- v);
4038 src#string "save path command"
4039 (fun () -> conf.savecmd)
4040 (fun v -> conf.savecmd <- v);
4041 src#colorspace "color space"
4042 (fun () -> CSTE.to_string conf.colorspace)
4043 (fun v ->
4044 conf.colorspace <- CSTE.of_int v;
4045 wcmd "cs %d" v;
4046 load state.layout;
4048 src#paxmark "pax mark method"
4049 (fun () -> MTE.to_string conf.paxmark)
4050 (fun v -> conf.paxmark <- MTE.of_int v);
4051 if pbousable ()
4052 then
4053 src#bool "use PBO"
4054 (fun () -> conf.usepbo)
4055 (fun v -> conf.usepbo <- v);
4056 src#bool "mouse wheel scrolls pages"
4057 (fun () -> conf.wheelbypage)
4058 (fun v -> conf.wheelbypage <- v);
4059 src#bool "open remote links in a new instance"
4060 (fun () -> conf.riani)
4061 (fun v -> conf.riani <- v);
4062 src#bool "edit annotations inline"
4063 (fun () -> conf.annotinline)
4064 (fun v -> conf.annotinline <- 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 popen execstr [] with
4238 | (exception exn) ->
4239 showtext '!' @@
4240 Printf.sprintf "popen(%S) failed: %s" execstr (exntos exn);
4242 | pid ->
4243 match Unix.waitpid [] pid
4244 with
4245 | (exception exn) ->
4246 showtext '!' @@
4247 Printf.sprintf "waitpid(%d) failed: %s" pid (exntos exn);
4249 | (_pid, status) ->
4250 match status with
4251 | Unix.WEXITED 0 -> filelines tmppath
4252 | Unix.WEXITED n ->
4253 showtext '!' @@
4254 Printf.sprintf "editor process(%s) exited abnormally: %d"
4255 execstr n;
4257 | Unix.WSIGNALED n ->
4258 showtext '!' @@
4259 Printf.sprintf "editor process(%s) was killed by signal %d"
4260 execstr n;
4262 | Unix.WSTOPPED n ->
4263 showtext '!' @@
4264 Printf.sprintf "editor(%s) process was stopped by signal %d"
4265 execstr n;
4268 match Unix.unlink tmppath with
4269 | (exception exn) ->
4270 showtext '!' @@ Printf.sprintf "failed to ulink %S: %s"
4271 tmppath (exntos exn);
4273 | () -> s
4276 let enterannotmode opaque slinkindex =
4277 let msgsource =
4278 (object
4279 inherit lvsourcebase
4280 val mutable m_text = E.s
4281 val mutable m_items = E.a
4283 method getitemcount = Array.length m_items
4285 method getitem n =
4286 let label, _func = m_items.(n) in
4287 label, 0
4289 method exit ~uioh ~cancel ~active ~first ~pan =
4290 ignore (uioh, first, pan);
4291 if not cancel
4292 then (
4293 let _label, func = m_items.(active) in
4294 func ()
4296 None
4298 method hasaction n = not @@ emptystr @@ fst m_items.(n)
4300 method reset s =
4301 let rec split accu b i =
4302 let p = b+i in
4303 if p = String.length s
4304 then (String.sub s b (p-b), unit) :: accu
4305 else
4306 if (i > 70 && s.[p] = ' ') || s.[p] = '\r' || s.[p] = '\n'
4307 then
4308 let ss = if i = 0 then E.s else String.sub s b i in
4309 split ((ss, unit)::accu) (p+1) 0
4310 else
4311 split accu b (i+1)
4313 let cleanup () =
4314 wcmd "freepage %s" (~> opaque);
4315 let keys =
4316 Hashtbl.fold (fun key opaque' accu ->
4317 if opaque' = opaque'
4318 then key :: accu else accu) state.pagemap []
4320 List.iter (Hashtbl.remove state.pagemap) keys;
4321 flushtiles ();
4322 gotoy state.y
4324 let dele () =
4325 delannot opaque slinkindex;
4326 cleanup ();
4328 let edit inline () =
4329 let update s =
4330 if emptystr s
4331 then dele ()
4332 else (
4333 modannot opaque slinkindex s;
4334 cleanup ();
4337 if inline
4338 then
4339 let mode = state.mode in
4340 state.mode <-
4341 Textentry (
4342 ("annotation: ", m_text, None, textentry, update, true),
4343 fun _ -> state.mode <- mode);
4344 state.text <- E.s;
4345 enttext ();
4346 else
4347 let s = getusertext m_text in
4348 update s
4350 m_text <- s;
4351 m_items <-
4352 ( "[Copy]", fun () -> selstring m_text)
4353 :: ("[Delete]", dele)
4354 :: ("[Edit]", edit conf.annotinline)
4355 :: (E.s, unit)
4356 :: split [] 0 0 |> List.rev |> Array.of_list
4358 initializer
4359 m_active <- 0
4360 end)
4362 state.text <- E.s;
4363 let s = getannotcontents opaque slinkindex in
4364 resetmstate ();
4365 msgsource#reset s;
4366 let source = (msgsource :> lvsource) in
4367 let modehash = findkeyhash conf "listview" in
4368 state.uioh <- coe (object
4369 inherit listview ~zebra:false ~helpmode:false
4370 ~source ~trusted:false ~modehash
4371 end);
4372 G.postRedisplay "enterannotmode";
4375 let gotounder under =
4376 let getpath filename =
4377 let path =
4378 if nonemptystr filename
4379 then
4380 if Filename.is_relative filename
4381 then
4382 let dir = Filename.dirname state.path in
4383 let dir =
4384 if Filename.is_implicit dir
4385 then Filename.concat (Sys.getcwd ()) dir
4386 else dir
4388 Filename.concat dir filename
4389 else filename
4390 else E.s
4392 if Sys.file_exists path
4393 then path
4394 else E.s
4396 match under with
4397 | Ulinkgoto (pageno, top) ->
4398 if pageno >= 0
4399 then (
4400 addnav ();
4401 gotopage1 pageno top;
4404 | Ulinkuri s ->
4405 addpid @@ gotouri s
4407 | Uremote (filename, pageno) ->
4408 let path = getpath filename in
4409 if nonemptystr path
4410 then (
4411 if conf.riani
4412 then
4413 let command = Printf.sprintf "%s -page %d %S" !selfexec pageno path in
4414 try addpid @@ popen command []
4415 with exn ->
4416 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
4417 flush stderr;
4418 else
4419 let anchor = getanchor () in
4420 let ranchor = state.path, state.password, anchor, state.origin in
4421 state.origin <- E.s;
4422 state.anchor <- (pageno, 0.0, 0.0);
4423 state.ranchors <- ranchor :: state.ranchors;
4424 opendoc path E.s;
4426 else showtext '!' ("Could not find " ^ filename)
4428 | Uremotedest (filename, destname) ->
4429 let path = getpath filename in
4430 if nonemptystr path
4431 then (
4432 if conf.riani
4433 then
4434 let command = !selfexec ^ " " ^ path ^ " -dest " ^ destname in
4435 try addpid @@ popen command []
4436 with exn ->
4437 Printf.eprintf
4438 "failed to execute `%s': %s\n" command (exntos exn);
4439 flush stderr;
4440 else
4441 let anchor = getanchor () in
4442 let ranchor = state.path, state.password, anchor, state.origin in
4443 state.origin <- E.s;
4444 state.nameddest <- destname;
4445 state.ranchors <- ranchor :: state.ranchors;
4446 opendoc path E.s;
4448 else showtext '!' ("Could not find " ^ filename)
4450 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4451 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
4454 let gotooutline (_, _, kind) =
4455 match kind with
4456 | Onone -> ()
4457 | Oanchor anchor ->
4458 let (pageno, y, _) = anchor in
4459 let y = getanchory
4460 (if conf.presentation then (pageno, y, 1.0) else anchor)
4462 addnav ();
4463 gotoghyll y
4464 | Ouri uri -> gotounder (Ulinkuri uri)
4465 | Olaunch cmd -> gotounder (Ulaunch cmd)
4466 | Oremote remote -> gotounder (Uremote remote)
4467 | Ohistory hist -> gotohist hist
4468 | Oremotedest remotedest -> gotounder (Uremotedest remotedest)
4469 | Oaction f -> f ()
4472 let outlinesource sourcetype =
4473 (object (self)
4474 inherit lvsourcebase
4475 val mutable m_items = E.a
4476 val mutable m_minfo = E.a
4477 val mutable m_orig_items = E.a
4478 val mutable m_orig_minfo = E.a
4479 val mutable m_narrow_patterns = []
4480 val mutable m_hadremovals = false
4481 val mutable m_gen = -1
4483 method getitemcount =
4484 Array.length m_items + (if m_hadremovals then 1 else 0)
4486 method getitem n =
4487 if n == Array.length m_items && m_hadremovals
4488 then
4489 ("[Confirm removal]", 0)
4490 else
4491 let s, n, _ = m_items.(n) in
4492 (s, n)
4494 method exit ~uioh ~cancel ~active ~first ~pan =
4495 ignore (uioh, first);
4496 let confrimremoval = m_hadremovals && active = Array.length m_items in
4497 let items, minfo =
4498 if m_narrow_patterns = []
4499 then m_orig_items, m_orig_minfo
4500 else m_items, m_minfo
4502 if not cancel
4503 then (
4504 if not confrimremoval
4505 then (
4506 gotooutline m_items.(active);
4507 m_items <- items;
4508 m_minfo <- minfo;
4510 else (
4511 state.bookmarks <- Array.to_list m_items;
4512 m_orig_items <- m_items;
4513 m_orig_minfo <- m_minfo;
4516 else (
4517 m_items <- items;
4518 m_minfo <- minfo;
4520 m_pan <- pan;
4521 None
4523 method hasaction _ = true
4525 method greetmsg =
4526 if Array.length m_items != Array.length m_orig_items
4527 then
4528 let s =
4529 match m_narrow_patterns with
4530 | one :: [] -> one
4531 | many -> String.concat "@Uellipsis" (List.rev many)
4533 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
4534 else E.s
4536 method statestr =
4537 match m_narrow_patterns with
4538 | [] -> E.s
4539 | one :: [] -> one
4540 | head :: _ -> "@Uellipsis" ^ head
4542 method narrow pattern =
4543 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
4544 match reopt with
4545 | None -> ()
4546 | Some re ->
4547 let rec loop accu minfo n =
4548 if n = -1
4549 then (
4550 m_items <- Array.of_list accu;
4551 m_minfo <- Array.of_list minfo;
4553 else
4554 let (s, _, t) as o = m_items.(n) in
4555 let accu, minfo =
4556 match t with
4557 | Oaction _ -> o :: accu, (0, 0) :: minfo
4558 | Onone | Oanchor _ | Ouri _ | Olaunch _
4559 | Oremote _ | Oremotedest _ | Ohistory _ ->
4560 let first =
4561 try Str.search_forward re s 0
4562 with Not_found -> -1
4564 if first >= 0
4565 then o :: accu, (first, Str.match_end ()) :: minfo
4566 else accu, minfo
4568 loop accu minfo (n-1)
4570 loop [] [] (Array.length m_items - 1)
4572 method! getminfo = m_minfo
4574 method denarrow =
4575 m_orig_items <- (
4576 match sourcetype with
4577 | `bookmarks -> Array.of_list state.bookmarks
4578 | `outlines -> state.outlines
4579 | `history -> genhistoutlines !Config.historder
4581 m_minfo <- m_orig_minfo;
4582 m_items <- m_orig_items
4584 method remove m =
4585 if sourcetype = `bookmarks
4586 then
4587 if m >= 0 && m < Array.length m_items
4588 then (
4589 m_hadremovals <- true;
4590 m_items <- Array.init (Array.length m_items - 1) (fun n ->
4591 let n = if n >= m then n+1 else n in
4592 m_items.(n)
4596 method add_narrow_pattern pattern =
4597 m_narrow_patterns <- pattern :: m_narrow_patterns
4599 method del_narrow_pattern =
4600 match m_narrow_patterns with
4601 | _ :: rest -> m_narrow_patterns <- rest
4602 | [] -> ()
4604 method renarrow =
4605 self#denarrow;
4606 match m_narrow_patterns with
4607 | pattern :: [] -> self#narrow pattern; pattern
4608 | list ->
4609 List.fold_left (fun accu pattern ->
4610 self#narrow pattern;
4611 pattern ^ "@Uellipsis" ^ accu) E.s list
4613 method calcactive anchor =
4614 let rely = getanchory anchor in
4615 let rec loop n best bestd =
4616 if n = Array.length m_items
4617 then best
4618 else
4619 let _, _, kind = m_items.(n) in
4620 match kind with
4621 | Oanchor anchor ->
4622 let orely = getanchory anchor in
4623 let d = abs (orely - rely) in
4624 if d < bestd
4625 then loop (n+1) n d
4626 else loop (n+1) best bestd
4627 | Onone | Oremote _ | Olaunch _
4628 | Oremotedest _ | Ouri _ | Ohistory _ | Oaction _ ->
4629 loop (n+1) best bestd
4631 loop 0 ~-1 max_int
4633 method reset anchor items =
4634 m_hadremovals <- false;
4635 if state.gen != m_gen
4636 then (
4637 m_orig_items <- items;
4638 m_items <- items;
4639 m_narrow_patterns <- [];
4640 m_minfo <- E.a;
4641 m_orig_minfo <- E.a;
4642 m_gen <- state.gen;
4644 else (
4645 if items != m_orig_items
4646 then (
4647 m_orig_items <- items;
4648 if m_narrow_patterns == []
4649 then m_items <- items;
4652 let active = self#calcactive anchor in
4653 m_active <- active;
4654 m_first <- firstof m_first active
4655 end)
4658 let enterselector sourcetype =
4659 resetmstate ();
4660 let source = outlinesource sourcetype in
4661 fun errmsg ->
4662 let outlines =
4663 match sourcetype with
4664 | `bookmarks -> Array.of_list state.bookmarks
4665 | `outlines -> state.outlines
4666 | `history -> genhistoutlines !Config.historder
4668 if Array.length outlines = 0
4669 then (
4670 showtext ' ' errmsg;
4672 else (
4673 state.text <- source#greetmsg;
4674 Wsi.setcursor Wsi.CURSOR_INHERIT;
4675 let anchor = getanchor () in
4676 source#reset anchor outlines;
4677 state.uioh <-
4678 coe (new outlinelistview ~zebra:(sourcetype=`history) ~source);
4679 G.postRedisplay "enter selector";
4683 let enteroutlinemode =
4684 let f = enterselector `outlines in
4685 fun () -> f "Document has no outline";
4688 let enterbookmarkmode =
4689 let f = enterselector `bookmarks in
4690 fun () -> f "Document has no bookmarks (yet)";
4693 let enterhistmode () = enterselector `history "No history (yet)";;
4695 let quickbookmark ?title () =
4696 match state.layout with
4697 | [] -> ()
4698 | l :: _ ->
4699 let title =
4700 match title with
4701 | None ->
4702 let tm = Unix.localtime (now ()) in
4703 Printf.sprintf "Quick (page %d) (bookmarked at %02d/%02d/%d %02d:%02d)"
4704 (l.pageno+1)
4705 tm.Unix.tm_mday
4706 (tm.Unix.tm_mon+1)
4707 (tm.Unix.tm_year + 1900)
4708 tm.Unix.tm_hour
4709 tm.Unix.tm_min
4710 | Some title -> title
4712 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4715 let setautoscrollspeed step goingdown =
4716 let incr = max 1 ((abs step) / 2) in
4717 let incr = if goingdown then incr else -incr in
4718 let astep = boundastep state.winh (step + incr) in
4719 state.autoscroll <- Some astep;
4722 let canpan () =
4723 match conf.columns with
4724 | Csplit _ -> true
4725 | Csingle _ | Cmulti _ -> state.x != 0 || conf.zoom > 1.0
4728 let panbound x = bound x (-state.w) (wadjsb () + state.winw);;
4730 let existsinrow pageno (columns, coverA, coverB) p =
4731 let last = ((pageno - coverA) mod columns) + columns in
4732 let rec any = function
4733 | [] -> false
4734 | l :: rest ->
4735 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4736 then p l
4737 else (
4738 if not (p l)
4739 then (if l.pageno = last then false else any rest)
4740 else true
4743 any state.layout
4746 let nextpage () =
4747 match state.layout with
4748 | [] ->
4749 let pageno = page_of_y state.y in
4750 gotoghyll (getpagey (pageno+1))
4751 | l :: rest ->
4752 match conf.columns with
4753 | Csingle _ ->
4754 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4755 then
4756 let y = clamp (pgscale state.winh) in
4757 gotoghyll y
4758 else
4759 let pageno = min (l.pageno+1) (state.pagecount-1) in
4760 gotoghyll (getpagey pageno)
4761 | Cmulti ((c, _, _) as cl, _) ->
4762 if conf.presentation
4763 && (existsinrow l.pageno cl
4764 (fun l -> l.pageh > l.pagey + l.pagevh))
4765 then
4766 let y = clamp (pgscale state.winh) in
4767 gotoghyll y
4768 else
4769 let pageno = min (l.pageno+c) (state.pagecount-1) in
4770 gotoghyll (getpagey pageno)
4771 | Csplit (n, _) ->
4772 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4773 then
4774 let pagey, pageh = getpageyh l.pageno in
4775 let pagey = pagey + pageh * l.pagecol in
4776 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4777 gotoghyll (pagey + pageh + ips)
4780 let prevpage () =
4781 match state.layout with
4782 | [] ->
4783 let pageno = page_of_y state.y in
4784 gotoghyll (getpagey (pageno-1))
4785 | l :: _ ->
4786 match conf.columns with
4787 | Csingle _ ->
4788 if conf.presentation && l.pagey != 0
4789 then
4790 gotoghyll (clamp (pgscale ~-(state.winh)))
4791 else
4792 let pageno = max 0 (l.pageno-1) in
4793 gotoghyll (getpagey pageno)
4794 | Cmulti ((c, _, coverB) as cl, _) ->
4795 if conf.presentation &&
4796 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4797 then
4798 gotoghyll (clamp (pgscale ~-(state.winh)))
4799 else
4800 let decr =
4801 if l.pageno = state.pagecount - coverB
4802 then 1
4803 else c
4805 let pageno = max 0 (l.pageno-decr) in
4806 gotoghyll (getpagey pageno)
4807 | Csplit (n, _) ->
4808 let y =
4809 if l.pagecol = 0
4810 then
4811 if l.pageno = 0
4812 then l.pagey
4813 else
4814 let pageno = max 0 (l.pageno-1) in
4815 let pagey, pageh = getpageyh pageno in
4816 pagey + (n-1)*pageh
4817 else
4818 let pagey, pageh = getpageyh l.pageno in
4819 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4821 gotoghyll y
4824 let save () =
4825 if emptystr conf.savecmd
4826 then error "don't know where to save modified document"
4827 else
4828 let savecmd = Str.global_replace percentsre state.path conf.savecmd in
4829 let path =
4830 getcmdoutput
4831 (fun s -> error "failed to obtain path to the saved copy: %s" s)
4832 savecmd
4834 if not (emptystr path)
4835 then
4836 let tmp = path ^ ".tmp" in
4837 savedoc tmp;
4838 Unix.rename tmp path;
4841 let viewkeyboard key mask =
4842 let enttext te =
4843 let mode = state.mode in
4844 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4845 state.text <- E.s;
4846 enttext ();
4847 G.postRedisplay "view:enttext"
4849 let ctrl = Wsi.withctrl mask in
4850 let key =
4851 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
4853 match key with
4854 | @Q -> exit 0
4856 | @W ->
4857 if hasunsavedchanges ()
4858 then save ()
4860 | @insert ->
4861 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4862 then (
4863 state.mode <- LinkNav (Ltgendir 0);
4864 gotoy state.y;
4866 else showtext '!' "Keyboard link navigation does not work under rotation"
4868 | @escape | @q ->
4869 begin match state.mstate with
4870 | Mzoomrect _ ->
4871 resetmstate ();
4872 G.postRedisplay "kill rect";
4873 | Msel _
4874 | Mpan _
4875 | Mscrolly | Mscrollx
4876 | Mzoom _
4877 | Mnone ->
4878 begin match state.mode with
4879 | LinkNav _ ->
4880 state.mode <- View;
4881 G.postRedisplay "esc leave linknav"
4882 | Birdseye _
4883 | Textentry _
4884 | View ->
4885 match state.ranchors with
4886 | [] -> raise Quit
4887 | (path, password, anchor, origin) :: rest ->
4888 state.ranchors <- rest;
4889 state.anchor <- anchor;
4890 state.origin <- origin;
4891 state.nameddest <- E.s;
4892 opendoc path password
4893 end;
4894 end;
4896 | @backspace ->
4897 gotoghyll (getnav ~-1)
4899 | @o ->
4900 enteroutlinemode ()
4902 | @H ->
4903 enterhistmode ()
4905 | @u ->
4906 state.rects <- [];
4907 state.text <- E.s;
4908 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap;
4909 G.postRedisplay "dehighlight";
4911 | @slash | @question ->
4912 let ondone isforw s =
4913 cbput state.hists.pat s;
4914 state.searchpattern <- s;
4915 search s isforw
4917 let s = String.make 1 (Char.chr key) in
4918 enttext (s, E.s, Some (onhist state.hists.pat),
4919 textentry, ondone (key = @slash), true)
4921 | @plus | @kpplus | @equals when ctrl ->
4922 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4923 setzoom (conf.zoom +. incr)
4925 | @plus | @kpplus ->
4926 let ondone s =
4927 let n =
4928 try int_of_string s with exc ->
4929 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
4930 max_int
4932 if n != max_int
4933 then (
4934 conf.pagebias <- n;
4935 state.text <- "page bias is now " ^ string_of_int n;
4938 enttext ("page bias: ", E.s, None, intentry, ondone, true)
4940 | @minus | @kpminus when ctrl ->
4941 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4942 setzoom (max 0.01 (conf.zoom -. decr))
4944 | @minus | @kpminus ->
4945 let ondone msg = state.text <- msg in
4946 enttext (
4947 "option [acfhilpstvxACFPRSZTISM]: ", E.s, None,
4948 optentry state.mode, ondone, true
4951 | @0 when ctrl ->
4952 if conf.zoom = 1.0
4953 then (
4954 state.x <- 0;
4955 gotoy state.y
4957 else setzoom 1.0
4959 | (@1 | @2) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
4960 let cols =
4961 match conf.columns with
4962 | Csingle _ | Cmulti _ -> 1
4963 | Csplit (n, _) -> n
4965 let h = state.winh -
4966 conf.interpagespace lsl (if conf.presentation then 1 else 0)
4968 let zoom = zoomforh state.winw h (vscrollw ()) cols in
4969 if zoom > 0.0 && (key = 50 || zoom < 1.0)
4970 then setzoom zoom
4972 | @3 when ctrl ->
4973 let fm =
4974 match conf.fitmodel with
4975 | FitWidth -> FitProportional
4976 | FitProportional -> FitPage
4977 | FitPage -> FitWidth
4979 state.text <- "fit model: " ^ FMTE.to_string fm;
4980 reqlayout conf.angle fm
4982 | @F9 ->
4983 togglebirdseye ()
4985 | @9 when ctrl ->
4986 togglebirdseye ()
4988 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4989 when not ctrl -> (* 0..9 *)
4990 let ondone s =
4991 let n =
4992 try int_of_string s with exc ->
4993 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
4996 if n >= 0
4997 then (
4998 addnav ();
4999 cbput state.hists.pag (string_of_int n);
5000 gotopage1 (n + conf.pagebias - 1) 0;
5003 let pageentry text key =
5004 match Char.unsafe_chr key with
5005 | 'g' -> TEdone text
5006 | _ -> intentry text key
5008 let text = String.make 1 (Char.chr key) in
5009 enttext (":", text, Some (onhist state.hists.pag),
5010 pageentry, ondone, true)
5012 | @b ->
5013 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
5014 reshape state.winw state.winh;
5016 | @B ->
5017 state.bzoom <- not state.bzoom;
5018 state.rects <- [];
5019 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
5021 | @l ->
5022 conf.hlinks <- not conf.hlinks;
5023 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5024 G.postRedisplay "toggle highlightlinks";
5026 | @F ->
5027 state.glinks <- true;
5028 let mode = state.mode in
5029 state.mode <- Textentry (
5030 (":", E.s, None, linknentry, linknact gotounder, false),
5031 (fun _ ->
5032 state.glinks <- false;
5033 state.mode <- mode)
5035 state.text <- E.s;
5036 G.postRedisplay "view:linkent(F)"
5038 | @y ->
5039 state.glinks <- true;
5040 let mode = state.mode in
5041 state.mode <- Textentry (
5043 ":", E.s, None, linknentry, linknact (fun under ->
5044 selstring (undertext under);
5045 ), false
5047 fun _ ->
5048 state.glinks <- false;
5049 state.mode <- mode
5051 state.text <- E.s;
5052 G.postRedisplay "view:linkent"
5054 | @a ->
5055 begin match state.autoscroll with
5056 | Some step ->
5057 conf.autoscrollstep <- step;
5058 state.autoscroll <- None
5059 | None ->
5060 if conf.autoscrollstep = 0
5061 then state.autoscroll <- Some 1
5062 else state.autoscroll <- Some conf.autoscrollstep
5065 | @p when ctrl ->
5066 launchpath ()
5068 | @P ->
5069 setpresentationmode (not conf.presentation);
5070 showtext ' ' ("presentation mode " ^
5071 if conf.presentation then "on" else "off");
5073 | @f ->
5074 if List.mem Wsi.Fullscreen state.winstate
5075 then Wsi.reshape conf.cwinw conf.cwinh
5076 else Wsi.fullscreen ()
5078 | @p | @N ->
5079 search state.searchpattern false
5081 | @n | @F3 ->
5082 search state.searchpattern true
5084 | @t ->
5085 begin match state.layout with
5086 | [] -> ()
5087 | l :: _ ->
5088 gotoghyll (getpagey l.pageno)
5091 | @space ->
5092 nextpage ()
5094 | @delete | @kpdelete -> (* delete *)
5095 prevpage ()
5097 | @equals ->
5098 showtext ' ' (describe_location ());
5100 | @w ->
5101 begin match state.layout with
5102 | [] -> ()
5103 | l :: _ ->
5104 Wsi.reshape (l.pagew + vscrollw ()) l.pageh;
5105 G.postRedisplay "w"
5108 | @apos ->
5109 enterbookmarkmode ()
5111 | @h | @F1 ->
5112 enterhelpmode ()
5114 | @i ->
5115 enterinfomode ()
5117 | @e when Buffer.length state.errmsgs > 0 ->
5118 entermsgsmode ()
5120 | @m ->
5121 let ondone s =
5122 match state.layout with
5123 | l :: _ ->
5124 if nonemptystr s
5125 then
5126 state.bookmarks <-
5127 (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
5128 | _ -> ()
5130 enttext ("bookmark: ", E.s, None, textentry, ondone, true)
5132 | @tilde ->
5133 quickbookmark ();
5134 showtext ' ' "Quick bookmark added";
5136 | @z ->
5137 begin match state.layout with
5138 | l :: _ ->
5139 let rect = getpdimrect l.pagedimno in
5140 let w, h =
5141 if conf.crophack
5142 then
5143 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5144 truncate (1.2 *. (rect.(3) -. rect.(0))))
5145 else
5146 (truncate (rect.(1) -. rect.(0)),
5147 truncate (rect.(3) -. rect.(0)))
5149 let w = truncate ((float w)*.conf.zoom)
5150 and h = truncate ((float h)*.conf.zoom) in
5151 if w != 0 && h != 0
5152 then (
5153 state.anchor <- getanchor ();
5154 Wsi.reshape (w + vscrollw ()) (h + conf.interpagespace)
5156 G.postRedisplay "z";
5158 | [] -> ()
5161 | @x -> state.roam ()
5163 | @Lt | @Gt ->
5164 reqlayout (conf.angle +
5165 (if key = @Gt then 30 else -30)) conf.fitmodel
5167 | @Lb | @Rb ->
5168 conf.colorscale <-
5169 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5171 G.postRedisplay "brightness";
5173 | @c when state.mode = View ->
5174 if Wsi.withalt mask
5175 then (
5176 if conf.zoom > 1.0
5177 then
5178 let m = (wadjsb () + state.winw - state.w) / 2 in
5179 state.x <- m;
5180 gotoy_and_clear_text state.y
5182 else
5183 let (c, a, b), z =
5184 match state.prevcolumns with
5185 | None -> (1, 0, 0), 1.0
5186 | Some (columns, z) ->
5187 let cab =
5188 match columns with
5189 | Csplit (c, _) -> -c, 0, 0
5190 | Cmulti ((c, a, b), _) -> c, a, b
5191 | Csingle _ -> 1, 0, 0
5193 cab, z
5195 setcolumns View c a b;
5196 setzoom z
5198 | @down | @up when ctrl && Wsi.withshift mask ->
5199 let zoom, x = state.prevzoom in
5200 setzoom zoom;
5201 state.x <- x;
5203 | @k | @up | @kpup ->
5204 begin match state.autoscroll with
5205 | None ->
5206 begin match state.mode with
5207 | Birdseye beye -> upbirdseye 1 beye
5208 | Textentry _
5209 | View
5210 | LinkNav _ ->
5211 if ctrl
5212 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5213 else (
5214 if not (Wsi.withshift mask) && conf.presentation
5215 then prevpage ()
5216 else gotoghyll1 true (clamp (-conf.scrollstep))
5219 | Some n ->
5220 setautoscrollspeed n false
5223 | @j | @down | @kpdown ->
5224 begin match state.autoscroll with
5225 | None ->
5226 begin match state.mode with
5227 | Birdseye beye -> downbirdseye 1 beye
5228 | Textentry _
5229 | View
5230 | LinkNav _ ->
5231 if ctrl
5232 then gotoy_and_clear_text (clamp (state.winh/2))
5233 else (
5234 if not (Wsi.withshift mask) && conf.presentation
5235 then nextpage ()
5236 else gotoghyll1 true (clamp (conf.scrollstep))
5239 | Some n ->
5240 setautoscrollspeed n true
5243 | @left | @right | @kpleft | @kpright when not (Wsi.withalt mask) ->
5244 if canpan ()
5245 then
5246 let dx =
5247 if ctrl
5248 then state.winw / 2
5249 else conf.hscrollstep
5251 let dx = if key = @left || key = @kpleft then dx else -dx in
5252 state.x <- panbound (state.x + dx);
5253 gotoy_and_clear_text state.y
5254 else (
5255 state.text <- E.s;
5256 G.postRedisplay "left/right"
5259 | @prior | @kpprior ->
5260 let y =
5261 if ctrl
5262 then
5263 match state.layout with
5264 | [] -> state.y
5265 | l :: _ -> state.y - l.pagey
5266 else
5267 clamp (pgscale (-state.winh))
5269 gotoghyll y
5271 | @next | @kpnext ->
5272 let y =
5273 if ctrl
5274 then
5275 match List.rev state.layout with
5276 | [] -> state.y
5277 | l :: _ -> getpagey l.pageno
5278 else
5279 clamp (pgscale state.winh)
5281 gotoghyll y
5283 | @g | @home | @kphome ->
5284 addnav ();
5285 gotoghyll 0
5286 | @G | @jend | @kpend ->
5287 addnav ();
5288 gotoghyll (clamp state.maxy)
5290 | @right | @kpright when Wsi.withalt mask ->
5291 gotoghyll (getnav 1)
5292 | @left | @kpleft when Wsi.withalt mask ->
5293 gotoghyll (getnav ~-1)
5295 | @r ->
5296 reload ()
5298 | @v when conf.debug ->
5299 state.rects <- [];
5300 List.iter (fun l ->
5301 match getopaque l.pageno with
5302 | None -> ()
5303 | Some opaque ->
5304 let x0, y0, x1, y1 = pagebbox opaque in
5305 let a,b = float x0, float y0 in
5306 let c,d = float x1, float y0 in
5307 let e,f = float x1, float y1 in
5308 let h,j = float x0, float y1 in
5309 let rect = (a,b,c,d,e,f,h,j) in
5310 debugrect rect;
5311 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5312 ) state.layout;
5313 G.postRedisplay "v";
5315 | @pipe ->
5316 let mode = state.mode in
5317 let cmd = ref E.s in
5318 let onleave = function
5319 | Cancel -> state.mode <- mode
5320 | Confirm ->
5321 List.iter (fun l ->
5322 match getopaque l.pageno with
5323 | Some opaque -> pipesel opaque !cmd
5324 | None -> ()) state.layout;
5325 state.mode <- mode
5327 let ondone s =
5328 cbput state.hists.sel s;
5329 cmd := s
5331 let te =
5332 "| ", !cmd, Some (onhist state.hists.sel), textentry, ondone, true
5334 G.postRedisplay "|";
5335 state.mode <- Textentry (te, onleave);
5337 | _ ->
5338 vlog "huh? %s" (Wsi.keyname key)
5341 let linknavkeyboard key mask linknav =
5342 let getpage pageno =
5343 let rec loop = function
5344 | [] -> None
5345 | l :: _ when l.pageno = pageno -> Some l
5346 | _ :: rest -> loop rest
5347 in loop state.layout
5349 let doexact (pageno, n) =
5350 match getopaque pageno, getpage pageno with
5351 | Some opaque, Some l ->
5352 if key = @enter || key = @kpenter
5353 then
5354 let under = getlink opaque n in
5355 G.postRedisplay "link gotounder";
5356 gotounder under;
5357 state.mode <- View;
5358 else
5359 let opt, dir =
5360 match key with
5361 | @home ->
5362 Some (findlink opaque LDfirst), -1
5364 | @jend ->
5365 Some (findlink opaque LDlast), 1
5367 | @left ->
5368 Some (findlink opaque (LDleft n)), -1
5370 | @right ->
5371 Some (findlink opaque (LDright n)), 1
5373 | @up ->
5374 Some (findlink opaque (LDup n)), -1
5376 | @down ->
5377 Some (findlink opaque (LDdown n)), 1
5379 | _ -> None, 0
5381 let pwl l dir =
5382 begin match findpwl l.pageno dir with
5383 | Pwlnotfound -> ()
5384 | Pwl pageno ->
5385 let notfound dir =
5386 state.mode <- LinkNav (Ltgendir dir);
5387 let y, h = getpageyh pageno in
5388 let y =
5389 if dir < 0
5390 then y + h - state.winh
5391 else y
5393 gotoy y
5395 begin match getopaque pageno, getpage pageno with
5396 | Some opaque, Some _ ->
5397 let link =
5398 let ld = if dir > 0 then LDfirst else LDlast in
5399 findlink opaque ld
5401 begin match link with
5402 | Lfound m ->
5403 showlinktype (getlink opaque m);
5404 state.mode <- LinkNav (Ltexact (pageno, m));
5405 G.postRedisplay "linknav jpage";
5406 | Lnotfound -> notfound dir
5407 end;
5408 | _ -> notfound dir
5409 end;
5410 end;
5412 begin match opt with
5413 | Some Lnotfound -> pwl l dir;
5414 | Some (Lfound m) ->
5415 if m = n
5416 then pwl l dir
5417 else (
5418 let _, y0, _, y1 = getlinkrect opaque m in
5419 if y0 < l.pagey
5420 then gotopage1 l.pageno y0
5421 else (
5422 let d = fstate.fontsize + 1 in
5423 if y1 - l.pagey > l.pagevh - d
5424 then gotopage1 l.pageno (y1 - state.winh - hscrollh () + d)
5425 else G.postRedisplay "linknav";
5427 showlinktype (getlink opaque m);
5428 state.mode <- LinkNav (Ltexact (l.pageno, m));
5431 | None -> viewkeyboard key mask
5432 end;
5433 | _ -> viewkeyboard key mask
5435 if key = @insert
5436 then (
5437 state.mode <- View;
5438 G.postRedisplay "leave linknav"
5440 else
5441 match linknav with
5442 | Ltgendir _ | Ltnotready _ -> viewkeyboard key mask
5443 | Ltexact exact -> doexact exact
5446 let keyboard key mask =
5447 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5448 then wcmd "interrupt"
5449 else state.uioh <- state.uioh#key key mask
5452 let birdseyekeyboard key mask
5453 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5454 let incr =
5455 match conf.columns with
5456 | Csingle _ -> 1
5457 | Cmulti ((c, _, _), _) -> c
5458 | Csplit _ -> failwith "bird's eye split mode"
5460 let pgh layout = List.fold_left
5461 (fun m l -> max l.pageh m) state.winh layout in
5462 match key with
5463 | @l when Wsi.withctrl mask ->
5464 let y, h = getpageyh pageno in
5465 let top = (state.winh - h) / 2 in
5466 gotoy (max 0 (y - top))
5467 | @enter | @kpenter -> leavebirdseye beye false
5468 | @escape -> leavebirdseye beye true
5469 | @up -> upbirdseye incr beye
5470 | @down -> downbirdseye incr beye
5471 | @left -> upbirdseye 1 beye
5472 | @right -> downbirdseye 1 beye
5474 | @prior ->
5475 begin match state.layout with
5476 | l :: _ ->
5477 if l.pagey != 0
5478 then (
5479 state.mode <- Birdseye (
5480 oconf, leftx, l.pageno, hooverpageno, anchor
5482 gotopage1 l.pageno 0;
5484 else (
5485 let layout = layout (state.y-state.winh) (pgh state.layout) in
5486 match layout with
5487 | [] -> gotoy (clamp (-state.winh))
5488 | l :: _ ->
5489 state.mode <- Birdseye (
5490 oconf, leftx, l.pageno, hooverpageno, anchor
5492 gotopage1 l.pageno 0
5495 | [] -> gotoy (clamp (-state.winh))
5496 end;
5498 | @next ->
5499 begin match List.rev state.layout with
5500 | l :: _ ->
5501 let layout = layout (state.y + (pgh state.layout)) state.winh in
5502 begin match layout with
5503 | [] ->
5504 let incr = l.pageh - l.pagevh in
5505 if incr = 0
5506 then (
5507 state.mode <-
5508 Birdseye (
5509 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5511 G.postRedisplay "birdseye pagedown";
5513 else gotoy (clamp (incr + conf.interpagespace*2));
5515 | l :: _ ->
5516 state.mode <-
5517 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5518 gotopage1 l.pageno 0;
5521 | [] -> gotoy (clamp state.winh)
5522 end;
5524 | @home ->
5525 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5526 gotopage1 0 0
5528 | @jend ->
5529 let pageno = state.pagecount - 1 in
5530 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5531 if not (pagevisible state.layout pageno)
5532 then
5533 let h =
5534 match List.rev state.pdims with
5535 | [] -> state.winh
5536 | (_, _, h, _) :: _ -> h
5538 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5539 else G.postRedisplay "birdseye end";
5541 | _ -> viewkeyboard key mask
5544 let drawpage l =
5545 let color =
5546 match state.mode with
5547 | Textentry _ -> scalecolor 0.4
5548 | LinkNav _
5549 | View -> scalecolor 1.0
5550 | Birdseye (_, _, pageno, hooverpageno, _) ->
5551 if l.pageno = hooverpageno
5552 then scalecolor 0.9
5553 else (
5554 if l.pageno = pageno
5555 then (
5556 let c = scalecolor 1.0 in
5557 GlDraw.color c;
5558 GlDraw.line_width 3.0;
5559 let dispx = xadjsb () + l.pagedispx in
5560 linerect
5561 (float (dispx-1)) (float (l.pagedispy-1))
5562 (float (dispx+l.pagevw+1))
5563 (float (l.pagedispy+l.pagevh+1))
5565 GlDraw.line_width 1.0;
5568 else scalecolor 0.8
5571 drawtiles l color;
5574 let postdrawpage l linkindexbase =
5575 match getopaque l.pageno with
5576 | Some opaque ->
5577 if tileready l l.pagex l.pagey
5578 then
5579 let x = l.pagedispx - l.pagex + xadjsb ()
5580 and y = l.pagedispy - l.pagey in
5581 let hlmask =
5582 match conf.columns with
5583 | Csingle _ | Cmulti _ ->
5584 (if conf.hlinks then 1 else 0)
5585 + (if state.glinks
5586 && not (isbirdseye state.mode) then 2 else 0)
5587 | Csplit _ -> 0
5589 let s =
5590 match state.mode with
5591 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5592 | Textentry _
5593 | Birdseye _
5594 | View
5595 | LinkNav _ -> E.s
5597 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5598 else 0
5599 | _ -> 0
5602 let scrollindicator () =
5603 let sbw, ph, sh = state.uioh#scrollph in
5604 let sbh, pw, sw = state.uioh#scrollpw in
5606 let x0,x1,hx0 =
5607 if conf.leftscroll
5608 then (0, sbw, sbw)
5609 else ((state.winw - sbw), state.winw, 0)
5612 GlDraw.color (0.64, 0.64, 0.64);
5613 filledrect (float x0) 0. (float x1) (float state.winh);
5614 filledrect
5615 (float hx0) (float (state.winh - sbh))
5616 (float (hx0 + wadjsb () + state.winw)) (float state.winh)
5618 GlDraw.color (0.0, 0.0, 0.0);
5620 filledrect (float x0) ph (float x1) (ph +. sh);
5621 let pw = pw +. float hx0 in
5622 filledrect pw (float (state.winh - sbh)) (pw +. sw) (float state.winh);
5625 let showsel () =
5626 match state.mstate with
5627 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5630 | Msel ((x0, y0), (x1, y1)) ->
5631 let identify opaque l px py = Some (opaque, l.pageno, px, py) in
5632 let o0,n0,px0,py0 = onppundermouse identify x0 y0 (~< E.s, -1, 0, 0) in
5633 let _o1,n1,px1,py1 = onppundermouse identify x1 y1 (~< E.s, -1, 0, 0) in
5634 if n0 != -1 && n0 = n1 then seltext o0 (px0, py0, px1, py1);
5637 let showrects = function [] -> () | rects ->
5638 Gl.enable `blend;
5639 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5640 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5641 List.iter
5642 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5643 List.iter (fun l ->
5644 if l.pageno = pageno
5645 then (
5646 let dx = float (l.pagedispx - l.pagex) in
5647 let dy = float (l.pagedispy - l.pagey) in
5648 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5649 Raw.sets_float state.vraw ~pos:0
5650 [| x0+.dx; y0+.dy;
5651 x1+.dx; y1+.dy;
5652 x3+.dx; y3+.dy;
5653 x2+.dx; y2+.dy |];
5654 GlArray.vertex `two state.vraw;
5655 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
5657 ) state.layout
5658 ) rects
5660 Gl.disable `blend;
5663 let display () =
5664 GlClear.color (scalecolor2 conf.bgcolor);
5665 GlClear.clear [`color];
5666 List.iter drawpage state.layout;
5667 let rects =
5668 match state.mode with
5669 | LinkNav (Ltexact (pageno, linkno)) ->
5670 begin match getopaque pageno with
5671 | Some opaque ->
5672 let dx = xadjsb () in
5673 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5674 let x0 = x0 + dx and x1 = x1 + dx in
5675 (pageno, 5, (
5676 float x0, float y0,
5677 float x1, float y0,
5678 float x1, float y1,
5679 float x0, float y1)
5680 ) :: state.rects
5681 | None -> state.rects
5683 | LinkNav (Ltgendir _) | LinkNav (Ltnotready _)
5684 | Birdseye _
5685 | Textentry _
5686 | View -> state.rects
5688 showrects rects;
5689 let rec postloop linkindexbase = function
5690 | l :: rest ->
5691 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5692 postloop linkindexbase rest
5693 | [] -> ()
5695 showsel ();
5696 postloop 0 state.layout;
5697 state.uioh#display;
5698 begin match state.mstate with
5699 | Mzoomrect ((x0, y0), (x1, y1)) ->
5700 Gl.enable `blend;
5701 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5702 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5703 filledrect (float x0) (float y0) (float x1) (float y1);
5704 Gl.disable `blend;
5705 | Msel _
5706 | Mpan _
5707 | Mscrolly | Mscrollx
5708 | Mzoom _
5709 | Mnone -> ()
5710 end;
5711 enttext ();
5712 scrollindicator ();
5713 Wsi.swapb ();
5716 let zoomrect x y x1 y1 =
5717 let x0 = min x x1
5718 and x1 = max x x1
5719 and y0 = min y y1 in
5720 gotoy (state.y + y0);
5721 state.anchor <- getanchor ();
5722 let zoom = (float state.w) /. float (x1 - x0) in
5723 let margin =
5724 let simple () =
5725 let adjw = wadjsb () + state.winw in
5726 if state.w < adjw
5727 then (adjw - state.w) / 2
5728 else 0
5730 match conf.fitmodel with
5731 | FitWidth | FitProportional -> simple ()
5732 | FitPage ->
5733 match conf.columns with
5734 | Csplit _ ->
5735 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
5736 | Cmulti _ | Csingle _ -> simple ()
5738 state.x <- (state.x + margin) - x0;
5739 setzoom zoom;
5740 resetmstate ();
5743 let annot inline x y =
5744 match unproject x y with
5745 | Some (opaque, n, ux, uy) ->
5746 let add text =
5747 addannot opaque ux uy text;
5748 wcmd "freepage %s" (~> opaque);
5749 Hashtbl.remove state.pagemap (n, state.gen);
5750 flushtiles ();
5751 gotoy state.y
5753 if inline
5754 then
5755 let ondone s = add s in
5756 let mode = state.mode in
5757 state.mode <- Textentry (
5758 ("annotation: ", E.s, None, textentry, ondone, true),
5759 fun _ -> state.mode <- mode);
5760 state.text <- E.s;
5761 enttext ();
5762 G.postRedisplay "annot"
5763 else
5764 let text =
5765 let s = getusertext E.s in
5766 let l = Str.split newlinere s in
5767 String.concat " " l
5769 add text
5770 | _ -> ()
5773 let zoomblock x y =
5774 let g opaque l px py =
5775 match rectofblock opaque px py with
5776 | Some a ->
5777 let x0 = a.(0) -. 20. in
5778 let x1 = a.(1) +. 20. in
5779 let y0 = a.(2) -. 20. in
5780 let zoom = (float state.w) /. (x1 -. x0) in
5781 let pagey = getpagey l.pageno in
5782 gotoy_and_clear_text (pagey + truncate y0);
5783 state.anchor <- getanchor ();
5784 let margin = (state.w - l.pagew)/2 in
5785 state.x <- -truncate x0 - margin;
5786 setzoom zoom;
5787 None
5788 | None -> None
5790 match conf.columns with
5791 | Csplit _ ->
5792 showtext '!' "block zooming does not work properly in split columns mode"
5793 | Cmulti _ | Csingle _ -> onppundermouse g x y ()
5796 let scrollx x =
5797 let winw = wadjsb () + state.winw - 1 in
5798 let s = float x /. float winw in
5799 let destx = truncate (float (state.w + winw) *. s) in
5800 state.x <- winw - destx;
5801 gotoy_and_clear_text state.y;
5802 state.mstate <- Mscrollx;
5805 let scrolly y =
5806 let s = float y /. float state.winh in
5807 let desty = truncate (float (state.maxy - state.winh) *. s) in
5808 gotoy_and_clear_text desty;
5809 state.mstate <- Mscrolly;
5812 let viewmulticlick clicks x y mask =
5813 let g opaque l px py =
5814 let mark =
5815 match clicks with
5816 | 2 -> Mark_word
5817 | 3 -> Mark_line
5818 | 4 -> Mark_block
5819 | _ -> Mark_page
5821 if markunder opaque px py mark
5822 then (
5823 Some (fun () ->
5824 let dopipe cmd =
5825 match getopaque l.pageno with
5826 | None -> ()
5827 | Some opaque -> pipesel opaque cmd
5829 state.roam <- (fun () -> dopipe conf.paxcmd);
5830 if not (Wsi.withctrl mask) then dopipe conf.selcmd;
5833 else None
5835 G.postRedisplay "viewmulticlick";
5836 onppundermouse g x y (fun () -> showtext '!' "Nothing to select") ();
5839 let canselect () =
5840 match conf.columns with
5841 | Csplit _ -> false
5842 | Csingle _ | Cmulti _ -> conf.angle mod 360 = 0
5845 let viewmouse button down x y mask =
5846 match button with
5847 | n when (n == 4 || n == 5) && not down ->
5848 if Wsi.withctrl mask
5849 then (
5850 match state.mstate with
5851 | Mzoom (oldn, i) ->
5852 if oldn = n
5853 then (
5854 if i = 2
5855 then
5856 let incr =
5857 match n with
5858 | 5 ->
5859 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5860 | _ ->
5861 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5863 let zoom = conf.zoom -. incr in
5864 setzoom zoom;
5865 state.mstate <- Mzoom (n, 0);
5866 else
5867 state.mstate <- Mzoom (n, i+1);
5869 else state.mstate <- Mzoom (n, 0)
5871 | Msel _
5872 | Mpan _
5873 | Mscrolly | Mscrollx
5874 | Mzoomrect _
5875 | Mnone -> state.mstate <- Mzoom (n, 0)
5877 else (
5878 match state.autoscroll with
5879 | Some step -> setautoscrollspeed step (n=4)
5880 | None ->
5881 if conf.wheelbypage || conf.presentation
5882 then (
5883 if n = 4
5884 then prevpage ()
5885 else nextpage ()
5887 else
5888 let incr =
5889 if n = 4
5890 then -conf.scrollstep
5891 else conf.scrollstep
5893 let incr = incr * 2 in
5894 let y = clamp incr in
5895 gotoy_and_clear_text y
5898 | n when (n = 6 || n = 7) && not down && canpan () ->
5899 state.x <-
5900 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
5901 gotoy_and_clear_text state.y
5903 | 1 when Wsi.withshift mask ->
5904 state.mstate <- Mnone;
5905 if not down
5906 then (
5907 match unproject x y with
5908 | Some (_, pageno, ux, uy) ->
5909 let cmd = Printf.sprintf
5910 "%s %s %d %d %d"
5911 conf.stcmd state.path pageno ux uy
5913 addpid @@ popen cmd []
5914 | None -> ()
5917 | 1 when Wsi.withctrl mask ->
5918 if down
5919 then (
5920 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5921 state.mstate <- Mpan (x, y)
5923 else
5924 state.mstate <- Mnone
5926 | 3 ->
5927 if down
5928 then (
5929 if Wsi.withshift mask
5930 then (
5931 annot conf.annotinline x y;
5932 G.postRedisplay "addannot"
5934 else
5935 let p = (x, y) in
5936 Wsi.setcursor Wsi.CURSOR_CYCLE;
5937 state.mstate <- Mzoomrect (p, p)
5939 else (
5940 match state.mstate with
5941 | Mzoomrect ((x0, y0), _) ->
5942 if abs (x-x0) > 10 && abs (y - y0) > 10
5943 then zoomrect x0 y0 x y
5944 else (
5945 resetmstate ();
5946 G.postRedisplay "kill accidental zoom rect";
5948 | Msel _
5949 | Mpan _
5950 | Mscrolly | Mscrollx
5951 | Mzoom _
5952 | Mnone ->
5953 resetmstate ()
5956 | 1 when x > state.winw - vscrollw () ->
5957 if down
5958 then
5959 let _, position, sh = state.uioh#scrollph in
5960 if y > truncate position && y < truncate (position +. sh)
5961 then state.mstate <- Mscrolly
5962 else scrolly y
5963 else
5964 state.mstate <- Mnone
5966 | 1 when y > state.winh - hscrollh () ->
5967 if down
5968 then
5969 let _, position, sw = state.uioh#scrollpw in
5970 if x > truncate position && x < truncate (position +. sw)
5971 then state.mstate <- Mscrollx
5972 else scrollx x
5973 else
5974 state.mstate <- Mnone
5976 | 1 when state.bzoom -> if not down then zoomblock x y
5978 | 1 ->
5979 let dest = if down then getunder x y else Unone in
5980 begin match dest with
5981 | Ulinkgoto _
5982 | Ulinkuri _
5983 | Uremote _ | Uremotedest _
5984 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5985 gotounder dest
5987 | Unone when down ->
5988 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5989 state.mstate <- Mpan (x, y);
5991 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
5993 | Unone | Utext _ ->
5994 if down
5995 then (
5996 if canselect ()
5997 then (
5998 state.mstate <- Msel ((x, y), (x, y));
5999 G.postRedisplay "mouse select";
6002 else (
6003 match state.mstate with
6004 | Mnone -> ()
6006 | Mzoom _ | Mscrollx | Mscrolly ->
6007 state.mstate <- Mnone
6009 | Mzoomrect ((x0, y0), _) ->
6010 zoomrect x0 y0 x y
6012 | Mpan _ ->
6013 Wsi.setcursor Wsi.CURSOR_INHERIT;
6014 state.mstate <- Mnone
6016 | Msel ((x0, y0), (x1, y1)) ->
6017 let rec loop = function
6018 | [] -> ()
6019 | l :: rest ->
6020 let inside =
6021 let a0 = l.pagedispy in
6022 let a1 = a0 + l.pagevh in
6023 let b0 = l.pagedispx in
6024 let b1 = b0 + l.pagevw in
6025 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
6026 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
6028 if inside
6029 then
6030 match getopaque l.pageno with
6031 | Some opaque ->
6032 let dosel cmd () =
6033 match Unix.pipe () with
6034 | exception exn ->
6035 showtext '!'
6036 (Printf.sprintf
6037 "can not create sel pipe: %s"
6038 (exntos exn));
6039 | (r, w) ->
6040 let clo what fd =
6041 Ne.clo fd (fun msg ->
6042 dolog "%s close failed: %s" what msg)
6044 let pid =
6045 try popen cmd [r, 0; w, -1]
6046 with exn ->
6047 dolog "can not execute %S: %s"
6048 cmd (exntos exn);
6051 if pid > 0
6052 then (
6053 copysel w opaque;
6054 G.postRedisplay "copysel";
6056 else clo "Msel pipe/w" w;
6057 clo "Msel pipe/r" r;
6059 dosel conf.selcmd ();
6060 state.roam <- dosel conf.paxcmd;
6061 | None -> ()
6062 else loop rest
6064 loop state.layout;
6065 resetmstate ();
6069 | _ -> ()
6072 let birdseyemouse button down x y mask
6073 (conf, leftx, _, hooverpageno, anchor) =
6074 match button with
6075 | 1 when down ->
6076 let rec loop = function
6077 | [] -> ()
6078 | l :: rest ->
6079 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6080 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6081 then (
6082 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
6084 else loop rest
6086 loop state.layout
6087 | 3 -> ()
6088 | _ -> viewmouse button down x y mask
6091 let uioh = object
6092 method display = ()
6094 method key key mask =
6095 begin match state.mode with
6096 | Textentry textentry -> textentrykeyboard key mask textentry
6097 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6098 | View -> viewkeyboard key mask
6099 | LinkNav linknav -> linknavkeyboard key mask linknav
6100 end;
6101 state.uioh
6103 method button button bstate x y mask =
6104 begin match state.mode with
6105 | LinkNav _
6106 | View -> viewmouse button bstate x y mask
6107 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6108 | Textentry _ -> ()
6109 end;
6110 state.uioh
6112 method multiclick clicks x y mask =
6113 begin match state.mode with
6114 | LinkNav _
6115 | View -> viewmulticlick clicks x y mask
6116 | Birdseye _
6117 | Textentry _ -> ()
6118 end;
6119 state.uioh
6121 method motion x y =
6122 begin match state.mode with
6123 | Textentry _ -> ()
6124 | View | Birdseye _ | LinkNav _ ->
6125 match state.mstate with
6126 | Mzoom _ | Mnone -> ()
6128 | Mpan (x0, y0) ->
6129 let dx = x - x0
6130 and dy = y0 - y in
6131 state.mstate <- Mpan (x, y);
6132 if canpan ()
6133 then state.x <- panbound (state.x + dx);
6134 let y = clamp dy in
6135 gotoy_and_clear_text y
6137 | Msel (a, _) ->
6138 state.mstate <- Msel (a, (x, y));
6139 G.postRedisplay "motion select";
6141 | Mscrolly ->
6142 let y = min state.winh (max 0 y) in
6143 scrolly y
6145 | Mscrollx ->
6146 let x = min state.winw (max 0 x) in
6147 scrollx x
6149 | Mzoomrect (p0, _) ->
6150 state.mstate <- Mzoomrect (p0, (x, y));
6151 G.postRedisplay "motion zoomrect";
6152 end;
6153 state.uioh
6155 method pmotion x y =
6156 begin match state.mode with
6157 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6158 let rec loop = function
6159 | [] ->
6160 if hooverpageno != -1
6161 then (
6162 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6163 G.postRedisplay "pmotion birdseye no hoover";
6165 | l :: rest ->
6166 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6167 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6168 then (
6169 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6170 G.postRedisplay "pmotion birdseye hoover";
6172 else loop rest
6174 loop state.layout
6176 | Textentry _ -> ()
6178 | LinkNav _
6179 | View ->
6180 match state.mstate with
6181 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ -> ()
6182 | Mnone ->
6183 updateunder x y;
6184 if canselect ()
6185 then
6186 match conf.pax with
6187 | None -> ()
6188 | Some r ->
6189 let past, _, _ = !r in
6190 let now = now () in
6191 let delta = now -. past in
6192 if delta > 0.01
6193 then paxunder x y
6194 else r := (now, x, y)
6195 end;
6196 state.uioh
6198 method infochanged _ = ()
6200 method scrollph =
6201 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6202 let p, h =
6203 if maxy = 0
6204 then 0.0, float state.winh
6205 else scrollph state.y maxy
6207 vscrollw (), p, h
6209 method scrollpw =
6210 let winw = wadjsb () + state.winw in
6211 let fwinw = float winw in
6212 let sw =
6213 let sw = fwinw /. float state.w in
6214 let sw = fwinw *. sw in
6215 max sw (float conf.scrollh)
6217 let position =
6218 let maxx = state.w + winw in
6219 let x = winw - state.x in
6220 let percent = float x /. float maxx in
6221 (fwinw -. sw) *. percent
6223 hscrollh (), position, sw
6225 method modehash =
6226 let modename =
6227 match state.mode with
6228 | LinkNav _ -> "links"
6229 | Textentry _ -> "textentry"
6230 | Birdseye _ -> "birdseye"
6231 | View -> "view"
6233 findkeyhash conf modename
6235 method eformsgs = true
6236 method alwaysscrolly = false
6237 end;;
6239 let adderrmsg src msg =
6240 Buffer.add_string state.errmsgs msg;
6241 state.newerrmsgs <- true;
6242 G.postRedisplay src
6245 let adderrfmt src fmt =
6246 Format.ksprintf (fun s -> adderrmsg src s) fmt;
6249 let ract cmds =
6250 let cl = splitatspace cmds in
6251 let scan s fmt f =
6252 try Scanf.sscanf s fmt f
6253 with exn ->
6254 adderrfmt "remote exec"
6255 "error processing '%S': %s\n" cmds (exntos exn)
6257 match cl with
6258 | "reload" :: [] -> reload ()
6259 | "goto" :: args :: [] ->
6260 scan args "%u %f %f"
6261 (fun pageno x y ->
6262 let cmd, _ = state.geomcmds in
6263 if emptystr cmd
6264 then gotopagexy pageno x y
6265 else
6266 let f prevf () =
6267 gotopagexy pageno x y;
6268 prevf ()
6270 state.reprf <- f state.reprf
6272 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
6273 | "gotor" :: args :: [] ->
6274 scan args "%S %u"
6275 (fun filename pageno -> gotounder (Uremote (filename, pageno)))
6276 | "gotord" :: args :: [] ->
6277 scan args "%S %S"
6278 (fun filename dest -> gotounder (Uremotedest (filename, dest)))
6279 | "rect" :: args :: [] ->
6280 scan args "%u %u %f %f %f %f"
6281 (fun pageno color x0 y0 x1 y1 ->
6282 onpagerect pageno (fun w h ->
6283 let _,w1,h1,_ = getpagedim pageno in
6284 let sw = float w1 /. float w
6285 and sh = float h1 /. float h in
6286 let x0s = x0 *. sw
6287 and x1s = x1 *. sw
6288 and y0s = y0 *. sh
6289 and y1s = y1 *. sh in
6290 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
6291 debugrect rect;
6292 state.rects <- (pageno, color, rect) :: state.rects;
6293 G.postRedisplay "rect";
6296 | "activatewin" :: [] -> Wsi.activatewin ()
6297 | "quit" :: [] -> raise Quit
6298 | _ ->
6299 adderrfmt "remote command"
6300 "error processing remote command: %S\n" cmds;
6303 let remote =
6304 let scratch = Bytes.create 80 in
6305 let buf = Buffer.create 80 in
6306 fun fd ->
6307 let rec tempfr () =
6308 try Some (Unix.read fd scratch 0 80)
6309 with
6310 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
6311 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
6312 | exn -> raise exn
6314 match tempfr () with
6315 | None -> Some fd
6316 | Some n ->
6317 if n = 0
6318 then (
6319 Unix.close fd;
6320 if Buffer.length buf > 0
6321 then (
6322 let s = Buffer.contents buf in
6323 Buffer.clear buf;
6324 ract s;
6326 None
6328 else
6329 let rec eat ppos =
6330 let nlpos =
6332 let pos = Bytes.index_from scratch ppos '\n' in
6333 if pos >= n then -1 else pos
6334 with Not_found -> -1
6336 if nlpos >= 0
6337 then (
6338 Buffer.add_subbytes buf scratch ppos (nlpos-ppos);
6339 let s = Buffer.contents buf in
6340 Buffer.clear buf;
6341 ract s;
6342 eat (nlpos+1);
6344 else (
6345 Buffer.add_subbytes buf scratch ppos (n-ppos);
6346 Some fd
6348 in eat 0
6351 let remoteopen path =
6352 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
6353 with exn ->
6354 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
6355 None
6358 let () =
6359 let gcconfig = ref E.s in
6360 let trimcachepath = ref E.s in
6361 let rcmdpath = ref E.s in
6362 let pageno = ref None in
6363 let rootwid = ref 0 in
6364 let openlast = ref false in
6365 let nofc = ref false in
6366 let doreap = ref false in
6367 selfexec := Sys.executable_name;
6368 Arg.parse
6369 (Arg.align
6370 [("-p", Arg.String (fun s -> state.password <- s),
6371 "<password> Set password");
6373 ("-f", Arg.String
6374 (fun s ->
6375 Config.fontpath := s;
6376 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
6378 "<path> Set path to the user interface font");
6380 ("-c", Arg.String
6381 (fun s ->
6382 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
6383 Config.confpath := s),
6384 "<path> Set path to the configuration file");
6386 ("-last", Arg.Set openlast, " Open last document");
6388 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
6389 "<page-number> Jump to page");
6391 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6392 "<path> Set path to the trim cache file");
6394 ("-dest", Arg.String (fun s -> state.nameddest <- s),
6395 "<named-destination> Set named destination");
6397 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
6398 ("-cxack", Arg.Set cxack, " Cut corners");
6400 ("-remote", Arg.String (fun s -> rcmdpath := s),
6401 "<path> Set path to the remote commands source");
6403 ("-origin", Arg.String (fun s -> state.origin <- s),
6404 "<original-path> Set original path");
6406 ("-gc", Arg.Set_string gcconfig,
6407 "<script-path> Collect garbage with the help of a script");
6409 ("-nofc", Arg.Set nofc, " Do not use fontconfig");
6411 ("-v", Arg.Unit (fun () ->
6412 Printf.printf
6413 "%s\nconfiguration path: %s\n"
6414 (version ())
6415 Config.defconfpath
6417 exit 0), " Print version and exit");
6419 ("-embed", Arg.Set_int rootwid,
6420 "<window-id> Embed into window")
6423 (fun s -> state.path <- s)
6424 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6426 if !wtmode
6427 then selfexec := !selfexec ^ " -wtmode";
6429 let histmode = emptystr state.path && not !openlast in
6431 if not (Config.load !openlast)
6432 then prerr_endline "failed to load configuration";
6433 begin match !pageno with
6434 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
6435 | None -> ()
6436 end;
6438 if not (emptystr !gcconfig)
6439 then (
6440 let c, s =
6441 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6442 | exception exn ->
6443 error "gc socketpair failed: %s" (exntos exn)
6444 | rw -> rw
6446 match addpid @@ popen !gcconfig [(c, 0); (c, 1)] with
6447 | exception exn ->
6448 error "failed to popen gc script: %s" (exntos exn);
6449 | _ ->
6450 Config.gc s s;
6451 exit 0
6454 let wsfd, winw, winh = Wsi.init (object (self)
6455 val mutable m_clicks = 0
6456 val mutable m_click_x = 0
6457 val mutable m_click_y = 0
6458 val mutable m_lastclicktime = infinity
6460 method private cleanup =
6461 state.roam <- noroam;
6462 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap
6463 method expose = G.postRedisplay"expose"
6464 method visible v =
6465 let name =
6466 match v with
6467 | Wsi.Unobscured -> "unobscured"
6468 | Wsi.PartiallyObscured -> "partiallyobscured"
6469 | Wsi.FullyObscured -> "fullyobscured"
6471 vlog "visibility change %s" name
6472 method display = display ()
6473 method map mapped = vlog "mappped %b" mapped
6474 method reshape w h =
6475 self#cleanup;
6476 reshape w h
6477 method mouse b d x y m =
6478 if d && canselect ()
6479 then (
6480 (* http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx *)
6481 m_click_x <- x;
6482 m_click_y <- y;
6483 if b = 1
6484 then (
6485 let t = now () in
6486 if abs x - m_click_x > 10
6487 || abs y - m_click_y > 10
6488 || abs_float (t -. m_lastclicktime) > 0.3
6489 then m_clicks <- 0;
6490 m_clicks <- m_clicks + 1;
6491 m_lastclicktime <- t;
6492 if m_clicks = 1
6493 then (
6494 self#cleanup;
6495 G.postRedisplay "cleanup";
6496 state.uioh <- state.uioh#button b d x y m;
6498 else state.uioh <- state.uioh#multiclick m_clicks x y m
6500 else (
6501 self#cleanup;
6502 m_clicks <- 0;
6503 m_lastclicktime <- infinity;
6504 state.uioh <- state.uioh#button b d x y m
6507 else (
6508 state.uioh <- state.uioh#button b d x y m
6510 method motion x y =
6511 state.mpos <- (x, y);
6512 state.uioh <- state.uioh#motion x y
6513 method pmotion x y =
6514 state.mpos <- (x, y);
6515 state.uioh <- state.uioh#pmotion x y
6516 method key k m =
6517 let mascm = m land (
6518 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6519 ) in
6520 let keyboard k m =
6521 let x = state.x and y = state.y in
6522 keyboard k m;
6523 if x != state.x || y != state.y then self#cleanup
6525 match state.keystate with
6526 | KSnone ->
6527 let km = k, mascm in
6528 begin
6529 match
6530 let modehash = state.uioh#modehash in
6531 try Hashtbl.find modehash km
6532 with Not_found ->
6533 try Hashtbl.find (findkeyhash conf "global") km
6534 with Not_found -> KMinsrt (k, m)
6535 with
6536 | KMinsrt (k, m) -> keyboard k m
6537 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6538 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6540 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6541 List.iter (fun (k, m) -> keyboard k m) insrt;
6542 state.keystate <- KSnone
6543 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6544 state.keystate <- KSinto (keys, insrt)
6545 | KSinto _ -> state.keystate <- KSnone
6547 method enter x y =
6548 state.mpos <- (x, y);
6549 state.uioh <- state.uioh#pmotion x y
6550 method leave = state.mpos <- (-1, -1)
6551 method winstate wsl = state.winstate <- wsl
6552 method quit = raise Quit
6553 end) !rootwid conf.cwinw conf.cwinh platform in
6555 state.wsfd <- wsfd;
6557 if not (
6558 List.exists GlMisc.check_extension
6559 [ "GL_ARB_texture_rectangle"
6560 ; "GL_EXT_texture_recangle"
6561 ; "GL_NV_texture_rectangle" ]
6563 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6565 if (
6566 let r = GlMisc.get_string `renderer in
6567 let p = "Mesa DRI Intel(" in
6568 let l = String.length p in
6569 String.length r > l && String.sub r 0 l = p
6571 then (
6572 defconf.sliceheight <- 1024;
6573 defconf.texcount <- 32;
6574 defconf.usepbo <- true;
6577 let cs, ss =
6578 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6579 | exception exn ->
6580 Printf.eprintf "socketpair failed: %s" (exntos exn);
6581 exit 1
6582 | (r, w) ->
6583 cloexec r;
6584 cloexec w;
6585 r, w
6588 setcheckers conf.checkers;
6589 redirectstderr ();
6590 if conf.redirectstderr
6591 then
6592 at_exit (fun () ->
6593 let s = Bytes.cat
6594 (Buffer.to_bytes state.errmsgs)
6595 (match state.errfd with
6596 | Some fd ->
6597 let s = Bytes.create (80*24) in
6598 let n =
6600 let r, _, _ = Unix.select [fd] [] [] 0.0 in
6601 if List.mem fd r
6602 then Unix.read fd s 0 (Bytes.length s)
6603 else 0
6604 with _ -> 0
6606 if n = 0
6607 then E.b
6608 else Bytes.sub s 0 n
6609 | None -> E.b
6612 try ignore (Unix.write state.stderr s 0 (Bytes.length s))
6613 with exn -> print_endline (exntos exn)
6617 init cs (
6618 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
6619 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6620 !Config.fontpath, !trimcachepath,
6621 GlMisc.check_extension "GL_ARB_pixel_buffer_object",
6622 not !nofc
6624 List.iter GlArray.enable [`texture_coord; `vertex];
6625 state.ss <- ss;
6626 reshape ~firsttime:true winw winh;
6627 state.uioh <- uioh;
6628 if histmode
6629 then (
6630 Wsi.settitle "llpp (history)";
6631 enterhistmode ();
6633 else (
6634 state.text <- "Opening " ^ (mbtoutf8 state.path);
6635 opendoc state.path state.password;
6637 display ();
6638 Wsi.mapwin ();
6639 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
6641 let rec reap () =
6642 match Unix.waitpid [Unix.WNOHANG] ~-1 with
6643 | (exception (Unix.Unix_error (Unix.ECHILD, _, _))) -> ()
6644 | (exception exn) -> dolog "Unix.waitpid: %s" @@ exntos exn
6645 | 0, _ -> ()
6646 | _pid, _status -> reap ()
6648 Sys.set_signal Sys.sigchld (Sys.Signal_handle (fun _ -> doreap := true));
6650 let optrfd =
6651 ref (
6652 if nonemptystr !rcmdpath
6653 then remoteopen !rcmdpath
6654 else None
6658 let rec loop deadline =
6659 if !doreap
6660 then (
6661 doreap := false;
6662 reap ()
6664 let r =
6665 match state.errfd with
6666 | None -> [state.ss; state.wsfd]
6667 | Some fd -> [state.ss; state.wsfd; fd]
6669 let r =
6670 match !optrfd with
6671 | None -> r
6672 | Some fd -> fd :: r
6674 if state.redisplay
6675 then (
6676 state.redisplay <- false;
6677 display ();
6679 let timeout =
6680 let now = now () in
6681 if deadline > now
6682 then (
6683 if deadline = infinity
6684 then ~-.1.0
6685 else max 0.0 (deadline -. now)
6687 else 0.0
6689 let r, _, _ =
6690 try Unix.select r [] [] timeout
6691 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6693 begin match r with
6694 | [] ->
6695 state.ghyll None;
6696 let newdeadline =
6697 if state.ghyll == noghyll
6698 then
6699 match state.autoscroll with
6700 | Some step when step != 0 ->
6701 let y = state.y + step in
6702 let y =
6703 if y < 0
6704 then state.maxy
6705 else if y >= state.maxy then 0 else y
6707 gotoy y;
6708 if state.mode = View
6709 then state.text <- E.s;
6710 deadline +. 0.01
6711 | _ -> infinity
6712 else deadline +. 0.01
6714 loop newdeadline
6716 | l ->
6717 let rec checkfds = function
6718 | [] -> ()
6719 | fd :: rest when fd = state.ss ->
6720 let cmd = readcmd state.ss in
6721 act cmd;
6722 checkfds rest
6724 | fd :: rest when fd = state.wsfd ->
6725 Wsi.readresp fd;
6726 checkfds rest
6728 | fd :: rest when Some fd = !optrfd ->
6729 begin match remote fd with
6730 | None -> optrfd := remoteopen !rcmdpath;
6731 | opt -> optrfd := opt
6732 end;
6733 checkfds rest
6735 | fd :: rest ->
6736 let s = Bytes.create 80 in
6737 let n = tempfailureretry (Unix.read fd s 0) 80 in
6738 if conf.redirectstderr
6739 then (
6740 Buffer.add_substring state.errmsgs (Bytes.to_string s) 0 n;
6741 state.newerrmsgs <- true;
6742 state.redisplay <- true;
6744 else (
6745 prerr_string (String.sub (Bytes.to_string s) 0 n);
6746 flush stderr;
6748 checkfds rest
6750 checkfds l;
6751 if !reeenterhist then (
6752 enterhistmode ();
6753 reeenterhist := false;
6755 let newdeadline =
6756 let deadline1 =
6757 if deadline = infinity
6758 then now () +. 0.01
6759 else deadline
6761 match state.autoscroll with
6762 | Some step when step != 0 -> deadline1
6763 | _ -> if state.ghyll == noghyll then infinity else deadline1
6765 loop newdeadline
6766 end;
6769 loop infinity;
6770 with Quit ->
6771 Config.save leavebirdseye;
6772 if hasunsavedchanges ()
6773 then save ();