Sync with upstream (c22548d0539d)
[llpp.git] / main.ml
blobb92562e3d029cd38b83cc63b78e5c98a09041c73
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 = function
367 | Unone -> "none"
368 | Ulinkuri s -> s
369 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
370 | Utext s -> "font: " ^ s
371 | Uunexpected s -> "unexpected: " ^ s
372 | Ulaunch s -> "launch: " ^ s
373 | Unamed s -> "named: " ^ s
374 | Uremote (filename, pageno) ->
375 Printf.sprintf "%s: page %d" filename (pageno+1)
376 | Uremotedest (filename, destname) ->
377 Printf.sprintf "%s: destination %S" filename destname
378 | Uannotation (opaque, slinkindex) ->
379 "annotation: " ^ getannotcontents opaque slinkindex
382 let updateunder x y =
383 match getunder x y with
384 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
385 | Ulinkuri uri ->
386 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
387 Wsi.setcursor Wsi.CURSOR_INFO
388 | Ulinkgoto (pageno, _) ->
389 if conf.underinfo
390 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
391 Wsi.setcursor Wsi.CURSOR_INFO
392 | Utext s ->
393 if conf.underinfo then showtext 'f' ("ont: " ^ s);
394 Wsi.setcursor Wsi.CURSOR_TEXT
395 | Uunexpected s ->
396 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
397 Wsi.setcursor Wsi.CURSOR_INHERIT
398 | Ulaunch s ->
399 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
400 Wsi.setcursor Wsi.CURSOR_INHERIT
401 | Unamed s ->
402 if conf.underinfo then showtext 'n' ("amed: " ^ s);
403 Wsi.setcursor Wsi.CURSOR_INHERIT
404 | Uremote (filename, pageno) ->
405 if conf.underinfo then showtext 'r'
406 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
407 Wsi.setcursor Wsi.CURSOR_INFO
408 | Uremotedest (filename, destname) ->
409 if conf.underinfo then showtext 'r'
410 (Printf.sprintf "emote destination: %s (%S)" filename destname);
411 Wsi.setcursor Wsi.CURSOR_INFO
412 | Uannotation _ ->
413 if conf.underinfo then showtext 'a' "nnotation";
414 Wsi.setcursor Wsi.CURSOR_INFO
417 let showlinktype under =
418 if conf.underinfo && under != Unone
419 then showtext ' ' @@ undertext under
422 let intentry_with_suffix text key =
423 let c =
424 if key >= 32 && key < 127
425 then Char.chr key
426 else '\000'
428 match Char.lowercase c with
429 | '0' .. '9' ->
430 let text = addchar text c in
431 TEcont text
433 | 'k' | 'm' | 'g' ->
434 let text = addchar text c in
435 TEcont text
437 | _ ->
438 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
439 TEcont text
442 let readcmd fd =
443 let s = Bytes.create 4 in
444 let n = tempfailureretry (Unix.read fd s 0) 4 in
445 if n != 4 then error "incomplete read(len) = %d" n;
446 let len = (Char.code (Bytes.get s 0) lsl 24)
447 lor (Char.code (Bytes.get s 1) lsl 16)
448 lor (Char.code (Bytes.get s 2) lsl 8)
449 lor (Char.code (Bytes.get s 3))
451 let s = Bytes.create len in
452 let n = tempfailureretry (Unix.read fd s 0) len in
453 if n != len then error "incomplete read(data) %d vs %d" n len;
454 Bytes.to_string s
457 let wcmd fmt =
458 let b = Buffer.create 16 in
459 Buffer.add_string b "llll";
460 Printf.kbprintf
461 (fun b ->
462 let s = Buffer.to_bytes b in
463 let n = Bytes.length s in
464 let len = n - 4 in
465 (* dolog "wcmd %S" (String.sub s 4 len); *)
466 Bytes.set s 0 (Char.chr ((len lsr 24) land 0xff));
467 Bytes.set s 1 (Char.chr ((len lsr 16) land 0xff));
468 Bytes.set s 2 (Char.chr ((len lsr 8) land 0xff));
469 Bytes.set s 3 (Char.chr (len land 0xff));
470 let n' = tempfailureretry (Unix.write state.ss s 0) n in
471 if n' != n then error "write failed %d vs %d" n' n;
472 ) b fmt;
475 let nogeomcmds cmds =
476 match cmds with
477 | s, [] -> emptystr s
478 | _ -> false
481 let layoutN ((columns, coverA, coverB), b) y sh =
482 let sh = sh - (hscrollh ()) in
483 let wadj = wadjsb () in
484 let rec fold accu n =
485 if n = Array.length b
486 then accu
487 else
488 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
489 if (vy - y) > sh &&
490 (n = coverA - 1
491 || n = state.pagecount - coverB
492 || (n - coverA) mod columns = columns - 1)
493 then accu
494 else
495 let accu =
496 if vy + h > y
497 then
498 let pagey = max 0 (y - vy) in
499 let pagedispy = if pagey > 0 then 0 else vy - y in
500 let pagedispx, pagex =
501 let pdx =
502 if n = coverA - 1 || n = state.pagecount - coverB
503 then state.x + (wadj + state.winw - w) / 2
504 else dx + xoff + state.x
506 if pdx < 0
507 then 0, -pdx
508 else pdx, 0
510 let pagevw =
511 let vw = wadj + state.winw - pagedispx in
512 let pw = w - pagex in
513 min vw pw
515 let pagevh = min (h - pagey) (sh - pagedispy) in
516 if pagevw > 0 && pagevh > 0
517 then
518 let e =
519 { pageno = n
520 ; pagedimno = pdimno
521 ; pagew = w
522 ; pageh = h
523 ; pagex = pagex
524 ; pagey = pagey
525 ; pagevw = pagevw
526 ; pagevh = pagevh
527 ; pagedispx = pagedispx
528 ; pagedispy = pagedispy
529 ; pagecol = 0
532 e :: accu
533 else
534 accu
535 else
536 accu
538 fold accu (n+1)
540 if Array.length b = 0
541 then []
542 else List.rev (fold [] (page_of_y y))
545 let layoutS (columns, b) y sh =
546 let sh = sh - hscrollh () in
547 let wadj = wadjsb () in
548 let rec fold accu n =
549 if n = Array.length b
550 then accu
551 else
552 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
553 if (vy - y) > sh
554 then accu
555 else
556 let accu =
557 if vy + pageh > y
558 then
559 let x = xoff + state.x in
560 let pagey = max 0 (y - vy) in
561 let pagedispy = if pagey > 0 then 0 else vy - y in
562 let pagedispx, pagex =
563 if px = 0
564 then (
565 if x < 0
566 then 0, -x
567 else x, 0
569 else (
570 let px = px - x in
571 if px < 0
572 then -px, 0
573 else 0, px
576 let pagecolw = pagew/columns in
577 let pagedispx =
578 if pagecolw < state.winw
579 then pagedispx + ((wadj + state.winw - pagecolw) / 2)
580 else pagedispx
582 let pagevw =
583 let vw = wadj + state.winw - pagedispx in
584 let pw = pagew - pagex in
585 min vw pw
587 let pagevw = min pagevw pagecolw in
588 let pagevh = min (pageh - pagey) (sh - pagedispy) in
589 if pagevw > 0 && pagevh > 0
590 then
591 let e =
592 { pageno = n/columns
593 ; pagedimno = pdimno
594 ; pagew = pagew
595 ; pageh = pageh
596 ; pagex = pagex
597 ; pagey = pagey
598 ; pagevw = pagevw
599 ; pagevh = pagevh
600 ; pagedispx = pagedispx
601 ; pagedispy = pagedispy
602 ; pagecol = n mod columns
605 e :: accu
606 else
607 accu
608 else
609 accu
611 fold accu (n+1)
613 List.rev (fold [] 0)
616 let layout y sh =
617 if nogeomcmds state.geomcmds
618 then
619 match conf.columns with
620 | Csingle b -> layoutN ((1, 0, 0), b) y sh
621 | Cmulti c -> layoutN c y sh
622 | Csplit s -> layoutS s y sh
623 else []
626 let clamp incr =
627 let y = state.y + incr in
628 let y = max 0 y in
629 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
633 let itertiles l f =
634 let tilex = l.pagex mod conf.tilew in
635 let tiley = l.pagey mod conf.tileh in
637 let col = l.pagex / conf.tilew in
638 let row = l.pagey / conf.tileh in
640 let xadj = xadjsb () in
641 let rec rowloop row y0 dispy h =
642 if h = 0
643 then ()
644 else (
645 let dh = conf.tileh - y0 in
646 let dh = min h dh in
647 let rec colloop col x0 dispx w =
648 if w = 0
649 then ()
650 else (
651 let dw = conf.tilew - x0 in
652 let dw = min w dw in
653 let dispx' = xadj + dispx in
654 f col row dispx' dispy x0 y0 dw dh;
655 colloop (col+1) 0 (dispx+dw) (w-dw)
658 colloop col tilex l.pagedispx l.pagevw;
659 rowloop (row+1) 0 (dispy+dh) (h-dh)
662 if l.pagevw > 0 && l.pagevh > 0
663 then rowloop row tiley l.pagedispy l.pagevh;
666 let gettileopaque l col row =
667 let key =
668 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
670 try Some (Hashtbl.find state.tilemap key)
671 with Not_found -> None
674 let puttileopaque l col row gen colorspace angle opaque size elapsed =
675 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
676 Hashtbl.add state.tilemap key (opaque, size, elapsed)
679 let filledrect x0 y0 x1 y1 =
680 GlArray.disable `texture_coord;
681 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
682 GlArray.vertex `two state.vraw;
683 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
684 GlArray.enable `texture_coord;
687 let linerect x0 y0 x1 y1 =
688 GlArray.disable `texture_coord;
689 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y1; x1; y0 |];
690 GlArray.vertex `two state.vraw;
691 GlArray.draw_arrays `line_loop ~first:0 ~count:4;
692 GlArray.enable `texture_coord;
695 let drawtiles l color =
696 GlDraw.color color;
697 let wadj = wadjsb () in
698 begintiles ();
699 let f col row x y tilex tiley w h =
700 match gettileopaque l col row with
701 | Some (opaque, _, t) ->
702 let params = x, y, w, h, tilex, tiley in
703 if conf.invert
704 then GlTex.env (`mode `blend);
705 drawtile params opaque;
706 if conf.invert
707 then GlTex.env (`mode `modulate);
708 if conf.debug
709 then (
710 endtiles ();
711 let s = Printf.sprintf
712 "%d[%d,%d] %f sec"
713 l.pageno col row t
715 let w = measurestr fstate.fontsize s in
716 GlDraw.color (0.0, 0.0, 0.0);
717 filledrect (float (x-2))
718 (float (y-2))
719 (float (x+2) +. w)
720 (float (y + fstate.fontsize + 2));
721 GlDraw.color (1.0, 1.0, 1.0);
722 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
723 begintiles ();
726 | None ->
727 endtiles ();
728 let w =
729 if conf.leftscroll
730 then w
731 else
732 let lw = wadj + state.winw - x in
733 min lw w
734 and h =
735 let lh = state.winh - y in
736 min lh h
738 if conf.invert
739 then GlTex.env (`mode `blend);
740 begin match state.checkerstexid with
741 | Some id ->
742 Gl.enable `texture_2d;
743 GlTex.bind_texture ~target:`texture_2d id;
744 let x0 = float x
745 and y0 = float y
746 and x1 = float (x+w)
747 and y1 = float (y+h) in
749 let tw = float w /. 16.0
750 and th = float h /. 16.0 in
751 let tx0 = float tilex /. 16.0
752 and ty0 = float tiley /. 16.0 in
753 let tx1 = tx0 +. tw
754 and ty1 = ty0 +. th in
755 Raw.sets_float state.vraw ~pos:0
756 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
757 Raw.sets_float state.traw ~pos:0
758 [| tx0; ty0; tx0; ty1; tx1; ty0; tx1; ty1 |];
759 GlArray.vertex `two state.vraw;
760 GlArray.tex_coord `two state.traw;
761 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
762 Gl.disable `texture_2d;
764 | None ->
765 GlDraw.color (1.0, 1.0, 1.0);
766 filledrect (float x) (float y) (float (x+w)) (float (y+h));
767 end;
768 if conf.invert
769 then GlTex.env (`mode `modulate);
770 if w > 128 && h > fstate.fontsize + 10
771 then (
772 let c = if conf.invert then 1.0 else 0.0 in
773 GlDraw.color (c, c, c);
774 let c, r =
775 if conf.verbose
776 then (col*conf.tilew, row*conf.tileh)
777 else col, row
779 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
781 GlDraw.color color;
782 begintiles ();
784 itertiles l f;
785 endtiles ();
788 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
790 let tilevisible1 l x y =
791 let ax0 = l.pagex
792 and ax1 = l.pagex + l.pagevw
793 and ay0 = l.pagey
794 and ay1 = l.pagey + l.pagevh in
796 let bx0 = x
797 and by0 = y in
798 let bx1 = min (bx0 + conf.tilew) l.pagew
799 and by1 = min (by0 + conf.tileh) l.pageh in
801 let rx0 = max ax0 bx0
802 and ry0 = max ay0 by0
803 and rx1 = min ax1 bx1
804 and ry1 = min ay1 by1 in
806 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
807 nonemptyintersection
810 let tilevisible layout n x y =
811 let rec findpageinlayout m = function
812 | l :: rest when l.pageno = n ->
813 tilevisible1 l x y || (
814 match conf.columns with
815 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
816 | Csplit _
817 | Csingle _
818 | Cmulti _ -> false
820 | _ :: rest -> findpageinlayout 0 rest
821 | [] -> false
823 findpageinlayout 0 layout;
826 let tileready l x y =
827 tilevisible1 l x y &&
828 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
831 let tilepage n p layout =
832 let rec loop = function
833 | l :: rest ->
834 if l.pageno = n
835 then
836 let f col row _ _ _ _ _ _ =
837 if state.currently = Idle
838 then
839 match gettileopaque l col row with
840 | Some _ -> ()
841 | None ->
842 let x = col*conf.tilew
843 and y = row*conf.tileh in
844 let w =
845 let w = l.pagew - x in
846 min w conf.tilew
848 let h =
849 let h = l.pageh - y in
850 min h conf.tileh
852 let pbo =
853 if conf.usepbo
854 then getpbo w h conf.colorspace
855 else ~< "0"
857 wcmd "tile %s %d %d %d %d %s"
858 (~> p) x y w h (~> pbo);
859 state.currently <-
860 Tiling (
861 l, p, conf.colorspace, conf.angle,
862 state.gen, col, row, conf.tilew, conf.tileh
865 itertiles l f;
866 else
867 loop rest
869 | [] -> ()
871 if nogeomcmds state.geomcmds
872 then loop layout;
875 let preloadlayout y =
876 let y = if y < state.winh then 0 else y - state.winh in
877 let h = state.winh*3 in
878 layout y h;
881 let load pages =
882 let rec loop pages =
883 if state.currently != Idle
884 then ()
885 else
886 match pages with
887 | l :: rest ->
888 begin match getopaque l.pageno with
889 | None ->
890 wcmd "page %d %d" l.pageno l.pagedimno;
891 state.currently <- Loading (l, state.gen);
892 | Some opaque ->
893 tilepage l.pageno opaque pages;
894 loop rest
895 end;
896 | _ -> ()
898 if nogeomcmds state.geomcmds
899 then loop pages
902 let preload pages =
903 load pages;
904 if conf.preload && state.currently = Idle
905 then load (preloadlayout state.y);
908 let layoutready layout =
909 let rec fold all ls =
910 all && match ls with
911 | l :: rest ->
912 let seen = ref false in
913 let allvisible = ref true in
914 let foo col row _ _ _ _ _ _ =
915 seen := true;
916 allvisible := !allvisible &&
917 begin match gettileopaque l col row with
918 | Some _ -> true
919 | None -> false
922 itertiles l foo;
923 fold (!seen && !allvisible) rest
924 | [] -> true
926 let alltilesvisible = fold true layout in
927 alltilesvisible;
930 let gotoy y =
931 let y = bound y 0 state.maxy in
932 let y, layout, proceed =
933 match conf.maxwait with
934 | Some time when state.ghyll == noghyll ->
935 begin match state.throttle with
936 | None ->
937 let layout = layout y state.winh in
938 let ready = layoutready layout in
939 if not ready
940 then (
941 load layout;
942 state.throttle <- Some (layout, y, now ());
944 else G.postRedisplay "gotoy showall (None)";
945 y, layout, ready
946 | Some (_, _, started) ->
947 let dt = now () -. started in
948 if dt > time
949 then (
950 state.throttle <- None;
951 let layout = layout y state.winh in
952 load layout;
953 G.postRedisplay "maxwait";
954 y, layout, true
956 else -1, [], false
959 | _ ->
960 let layout = layout y state.winh in
961 if not !wtmode || layoutready layout
962 then G.postRedisplay "gotoy ready";
963 y, layout, true
965 if proceed
966 then (
967 state.y <- y;
968 state.layout <- layout;
969 begin match state.mode with
970 | LinkNav ln ->
971 begin match ln with
972 | Ltexact (pageno, linkno) ->
973 let rec loop = function
974 | [] ->
975 state.mode <- LinkNav (Ltgendir 0)
976 | l :: _ when l.pageno = pageno ->
977 begin match getopaque pageno with
978 | None -> state.mode <- LinkNav (Ltnotready (pageno, 0))
979 | Some opaque ->
980 let x0, y0, x1, y1 = getlinkrect opaque linkno in
981 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
982 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
983 then state.mode <- LinkNav (Ltgendir 0)
985 | _ :: rest -> loop rest
987 loop layout
988 | Ltnotready _ | Ltgendir _ -> ()
990 | Birdseye _
991 | Textentry _
992 | View -> ()
993 end;
994 begin match state.mode with
995 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
996 if not (pagevisible layout pageno)
997 then (
998 match state.layout with
999 | [] -> ()
1000 | l :: _ ->
1001 state.mode <- Birdseye (
1002 conf, leftx, l.pageno, hooverpageno, anchor
1005 | LinkNav lt ->
1006 begin match lt with
1007 | Ltnotready (_, dir)
1008 | Ltgendir dir ->
1009 let linknav =
1010 let rec loop = function
1011 | [] -> lt
1012 | l :: rest ->
1013 match getopaque l.pageno with
1014 | None -> Ltnotready (l.pageno, dir)
1015 | Some opaque ->
1016 let link =
1017 let ld =
1018 if dir = 0
1019 then LDfirstvisible (l.pagex, l.pagey, dir)
1020 else (
1021 if dir > 0 then LDfirst else LDlast
1024 findlink opaque ld
1026 match link with
1027 | Lnotfound -> loop rest
1028 | Lfound n ->
1029 showlinktype (getlink opaque n);
1030 Ltexact (l.pageno, n)
1032 loop state.layout
1034 state.mode <- LinkNav linknav
1035 | Ltexact _ -> ()
1037 | Textentry _
1038 | View -> ()
1039 end;
1040 preload layout;
1042 state.ghyll <- noghyll;
1043 if conf.updatecurs
1044 then (
1045 let mx, my = state.mpos in
1046 updateunder mx my;
1050 let conttiling pageno opaque =
1051 tilepage pageno opaque
1052 (if conf.preload then preloadlayout state.y else state.layout)
1055 let gotoy_and_clear_text y =
1056 if not conf.verbose then state.text <- E.s;
1057 gotoy y;
1060 let getanchory (n, top, dtop) =
1061 let y, h = getpageyh n in
1062 if conf.presentation
1063 then
1064 let ips = calcips h in
1065 y + truncate (top*.float h -. dtop*.float ips) + ips;
1066 else
1067 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1070 let gotoanchor anchor =
1071 gotoy (getanchory anchor);
1074 let addnav () =
1075 cbput state.hists.nav (getanchor ());
1078 let getnav dir =
1079 let anchor = cbgetc state.hists.nav dir in
1080 getanchory anchor;
1083 let gotoghyll1 single y =
1084 let scroll f n a b =
1085 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1086 let snake f a b =
1087 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1088 if f < a
1089 then s (float f /. float a)
1090 else (
1091 if f > b
1092 then 1.0 -. s ((float (f-b) /. float (n-b)))
1093 else 1.0
1096 snake f a b
1097 and summa n a b =
1098 let ins = float a *. 0.5
1099 and outs = float (n-b) *. 0.5 in
1100 let ones = b - a in
1101 ins +. outs +. float ones
1103 let rec set nab y sy =
1104 let (_N, _A, _B), y =
1105 if single
1106 then
1107 let scl = if y > sy then 2 else -2 in
1108 let _N, _, _ = nab in
1109 (_N,0,_N), y+conf.scrollstep*scl
1110 else nab,y in
1111 let sum = summa _N _A _B in
1112 let dy = float (y - sy) in
1113 state.ghyll <- (
1114 let rec gf n y1 o =
1115 if n >= _N
1116 then state.ghyll <- noghyll
1117 else
1118 let go n =
1119 let s = scroll n _N _A _B in
1120 let y1 = y1 +. ((s *. dy) /. sum) in
1121 gotoy_and_clear_text (truncate y1);
1122 state.ghyll <- gf (n+1) y1;
1124 match o with
1125 | None -> go n
1126 | Some y' when single -> set nab y' state.y
1127 | Some y' -> set (_N/2, 1, 1) y' state.y
1129 gf 0 (float state.y)
1132 match conf.ghyllscroll with
1133 | Some nab when not conf.presentation ->
1134 if state.ghyll == noghyll
1135 then set nab y state.y
1136 else state.ghyll (Some y)
1137 | _ ->
1138 gotoy_and_clear_text y
1141 let gotoghyll = gotoghyll1 false;;
1143 let gotopage n top =
1144 let y, h = getpageyh n in
1145 let y = y + (truncate (top *. float h)) in
1146 gotoghyll y
1149 let gotopage1 n top =
1150 let y = getpagey n in
1151 let y = y + top in
1152 gotoghyll y
1155 let invalidate s f =
1156 state.layout <- [];
1157 state.pdims <- [];
1158 state.rects <- [];
1159 state.rects1 <- [];
1160 match state.geomcmds with
1161 | ps, [] when emptystr ps ->
1162 f ();
1163 state.geomcmds <- s, [];
1165 | ps, [] ->
1166 state.geomcmds <- ps, [s, f];
1168 | ps, (s', _) :: rest when s' = s ->
1169 state.geomcmds <- ps, ((s, f) :: rest);
1171 | ps, cmds ->
1172 state.geomcmds <- ps, ((s, f) :: cmds);
1175 let flushpages () =
1176 Hashtbl.iter (fun _ opaque ->
1177 wcmd "freepage %s" (~> opaque);
1178 ) state.pagemap;
1179 Hashtbl.clear state.pagemap;
1182 let flushtiles () =
1183 if not (Queue.is_empty state.tilelru)
1184 then (
1185 Queue.iter (fun (k, p, s) ->
1186 wcmd "freetile %s" (~> p);
1187 state.memused <- state.memused - s;
1188 Hashtbl.remove state.tilemap k;
1189 ) state.tilelru;
1190 state.uioh#infochanged Memused;
1191 Queue.clear state.tilelru;
1193 load state.layout;
1196 let stateh h =
1197 let h = truncate (float h*.conf.zoom) in
1198 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
1199 h - d
1202 let opendoc path password =
1203 state.path <- path;
1204 state.password <- password;
1205 state.gen <- state.gen + 1;
1206 state.docinfo <- [];
1207 state.outlines <- [||];
1209 flushpages ();
1210 setaalevel conf.aalevel;
1211 let titlepath =
1212 if emptystr state.origin
1213 then path
1214 else state.origin
1216 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
1217 wcmd "open %d %d %s\000%s\000" (btod !wtmode) (btod !cxack) path password;
1218 invalidate "reqlayout"
1219 (fun () ->
1220 wcmd "reqlayout %d %d %d %s\000"
1221 conf.angle (FMTE.to_int conf.fitmodel)
1222 (stateh state.winh) state.nameddest
1226 let reload () =
1227 state.anchor <- getanchor ();
1228 opendoc state.path state.password;
1231 let scalecolor c =
1232 let c = c *. conf.colorscale in
1233 (c, c, c);
1236 let scalecolor2 (r, g, b) =
1237 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1240 let docolumns columns =
1241 let wadj = wadjsb () in
1242 match columns with
1243 | Csingle _ ->
1244 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1245 let wadj = wadjsb () in
1246 let rec loop pageno pdimno pdim y ph pdims =
1247 if pageno = state.pagecount
1248 then ()
1249 else
1250 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1251 match pdims with
1252 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1253 pdimno+1, pdim, rest
1254 | _ ->
1255 pdimno, pdim, pdims
1257 let x = max 0 (((wadj + state.winw - w) / 2) - xoff) in
1258 let y = y +
1259 (if conf.presentation
1260 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1261 else (if pageno = 0 then 0 else conf.interpagespace)
1264 a.(pageno) <- (pdimno, x, y, pdim);
1265 loop (pageno+1) pdimno pdim (y + h) h pdims
1267 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1268 conf.columns <- Csingle a;
1270 | Cmulti ((columns, coverA, coverB), _) ->
1271 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1272 let rec loop pageno pdimno pdim x y rowh pdims =
1273 let rec fixrow m = if m = pageno then () else
1274 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1275 if h < rowh
1276 then (
1277 let y = y + (rowh - h) / 2 in
1278 a.(m) <- (pdimno, x, y, pdim);
1280 fixrow (m+1)
1282 if pageno = state.pagecount
1283 then fixrow (((pageno - 1) / columns) * columns)
1284 else
1285 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1286 match pdims with
1287 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1288 pdimno+1, pdim, rest
1289 | _ ->
1290 pdimno, pdim, pdims
1292 let x, y, rowh' =
1293 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1294 then (
1295 let x = (wadj + state.winw - w) / 2 in
1296 let ips =
1297 if conf.presentation then calcips h else conf.interpagespace in
1298 x, y + ips + rowh, h
1300 else (
1301 if (pageno - coverA) mod columns = 0
1302 then (
1303 let x = max 0 (wadj + state.winw - state.w) / 2 in
1304 let y =
1305 if conf.presentation
1306 then
1307 let ips = calcips h in
1308 y + (if pageno = 0 then 0 else calcips rowh + ips)
1309 else
1310 y + (if pageno = 0 then 0 else conf.interpagespace)
1312 x, y + rowh, h
1314 else x, y, max rowh h
1317 let y =
1318 if pageno > 1 && (pageno - coverA) mod columns = 0
1319 then (
1320 let y =
1321 if pageno = columns && conf.presentation
1322 then (
1323 let ips = calcips rowh in
1324 for i = 0 to pred columns
1326 let (pdimno, x, y, pdim) = a.(i) in
1327 a.(i) <- (pdimno, x, y+ips, pdim)
1328 done;
1329 y+ips;
1331 else y
1333 fixrow (pageno - columns);
1336 else y
1338 a.(pageno) <- (pdimno, x, y, pdim);
1339 let x = x + w + xoff*2 + conf.interpagespace in
1340 loop (pageno+1) pdimno pdim x y rowh' pdims
1342 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1343 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1345 | Csplit (c, _) ->
1346 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1347 let rec loop pageno pdimno pdim y pdims =
1348 if pageno = state.pagecount
1349 then ()
1350 else
1351 let pdimno, ((_, w, h, _) as pdim), pdims =
1352 match pdims with
1353 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1354 pdimno+1, pdim, rest
1355 | _ ->
1356 pdimno, pdim, pdims
1358 let cw = w / c in
1359 let rec loop1 n x y =
1360 if n = c then y else (
1361 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1362 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1365 let y = loop1 0 0 y in
1366 loop (pageno+1) pdimno pdim y pdims
1368 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1369 conf.columns <- Csplit (c, a);
1372 let represent () =
1373 docolumns conf.columns;
1374 state.maxy <- calcheight ();
1375 if state.reprf == noreprf
1376 then (
1377 match state.mode with
1378 | Birdseye (_, _, pageno, _, _) ->
1379 let y, h = getpageyh pageno in
1380 let top = (state.winh - h) / 2 in
1381 gotoy (max 0 (y - top))
1382 | Textentry _
1383 | View
1384 | LinkNav _ -> gotoanchor state.anchor
1386 else (
1387 state.reprf ();
1388 state.reprf <- noreprf;
1392 let reshape ?(firsttime=false) w h =
1393 GlDraw.viewport ~x:0 ~y:0 ~w:w ~h:h;
1394 if not firsttime && nogeomcmds state.geomcmds
1395 then state.anchor <- getanchor ();
1397 state.winw <- w;
1398 let w = wadjsb () + (truncate (float w *. conf.zoom)) in
1399 let w = max w 2 in
1400 state.winh <- h;
1401 setfontsize fstate.fontsize;
1402 GlMat.mode `modelview;
1403 GlMat.load_identity ();
1405 GlMat.mode `projection;
1406 GlMat.load_identity ();
1407 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1408 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1409 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
1411 let relx =
1412 if conf.zoom <= 1.0
1413 then 0.0
1414 else float state.x /. float state.w
1416 invalidate "geometry"
1417 (fun () ->
1418 state.w <- w;
1419 if not firsttime
1420 then state.x <- truncate (relx *. float w);
1421 let w =
1422 match conf.columns with
1423 | Csingle _ -> w
1424 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1425 | Csplit (c, _) -> w * c
1427 wcmd "geometry %d %d %d"
1428 w (stateh h) (FMTE.to_int conf.fitmodel)
1432 let enttext () =
1433 let len = String.length state.text in
1434 let x0 = xadjsb () in
1435 let drawstring s =
1436 let hscrollh =
1437 match state.mode with
1438 | Textentry _ | View | LinkNav _ ->
1439 let h, _, _ = state.uioh#scrollpw in
1441 | Birdseye _ -> 0
1443 let rect x w =
1444 filledrect x (float (state.winh - (fstate.fontsize + 4) - hscrollh))
1445 (x+.w) (float (state.winh - hscrollh))
1448 let w = float (wadjsb () + state.winw - 1) in
1449 if state.progress >= 0.0 && state.progress < 1.0
1450 then (
1451 GlDraw.color (0.3, 0.3, 0.3);
1452 let w1 = w *. state.progress in
1453 rect (float x0) w1;
1454 GlDraw.color (0.0, 0.0, 0.0);
1455 rect (float x0+.w1) (float x0+.w-.w1)
1457 else (
1458 GlDraw.color (0.0, 0.0, 0.0);
1459 rect (float x0) w;
1462 GlDraw.color (1.0, 1.0, 1.0);
1463 drawstring fstate.fontsize
1464 (if conf.leftscroll then x0 + 2 else x0 + if len > 0 then 8 else 2)
1465 (state.winh - hscrollh - 5) s;
1467 let s =
1468 match state.mode with
1469 | Textentry ((prefix, text, _, _, _, _), _) ->
1470 let s =
1471 if len > 0
1472 then
1473 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1474 else
1475 Printf.sprintf "%s%s_" prefix text
1479 | Birdseye _
1480 | View
1481 | LinkNav _ -> state.text
1483 let s =
1484 if state.newerrmsgs
1485 then (
1486 if not (istextentry state.mode) && state.uioh#eformsgs
1487 then
1488 let s1 = "(press 'e' to review error messasges)" in
1489 if nonemptystr s then s ^ " " ^ s1 else s1
1490 else s
1492 else s
1494 if nonemptystr s
1495 then drawstring s
1498 let gctiles () =
1499 let len = Queue.length state.tilelru in
1500 let layout = lazy (
1501 match state.throttle with
1502 | None ->
1503 if conf.preload
1504 then preloadlayout state.y
1505 else state.layout
1506 | Some (layout, _, _) ->
1507 layout
1508 ) in
1509 let rec loop qpos =
1510 if state.memused <= conf.memlimit
1511 then ()
1512 else (
1513 if qpos < len
1514 then
1515 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1516 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1517 let (_, pw, ph, _) = getpagedim n in
1519 gen = state.gen
1520 && colorspace = conf.colorspace
1521 && angle = conf.angle
1522 && pagew = pw
1523 && pageh = ph
1524 && (
1525 let x = col*conf.tilew
1526 and y = row*conf.tileh in
1527 tilevisible (Lazy.force_val layout) n x y
1529 then Queue.push lruitem state.tilelru
1530 else (
1531 freepbo p;
1532 wcmd "freetile %s" (~> p);
1533 state.memused <- state.memused - s;
1534 state.uioh#infochanged Memused;
1535 Hashtbl.remove state.tilemap k;
1537 loop (qpos+1)
1540 loop 0
1543 let onpagerect pageno f =
1544 let b =
1545 match conf.columns with
1546 | Cmulti (_, b) -> b
1547 | Csingle b -> b
1548 | Csplit (_, b) -> b
1550 if pageno >= 0 && pageno < Array.length b
1551 then
1552 let (_, _, _, (_, w, h, _)) = b.(pageno) in
1553 f w h
1556 let gotopagexy1 pageno x y =
1557 let _,w1,h1,leftx = getpagedim pageno in
1558 let top = y /. (float h1) in
1559 let left = x /. (float w1) in
1560 let py, w, h = getpageywh pageno in
1561 let wh = state.winh - hscrollh () in
1562 let x = left *. (float w) in
1563 let x = leftx + state.x + truncate x in
1564 let wadj = wadjsb () in
1565 let sx =
1566 if x < 0 || x >= wadj + state.winw
1567 then state.x - x
1568 else state.x
1570 let pdy = truncate (top *. float h) in
1571 let y' = py + pdy in
1572 let dy = y' - state.y in
1573 let sy =
1574 if x != state.x || not (dy > 0 && dy < wh)
1575 then (
1576 if conf.presentation
1577 then
1578 if abs (py - y') > wh
1579 then y'
1580 else py
1581 else y';
1583 else state.y
1585 if state.x != sx || state.y != sy
1586 then (
1587 let x, y =
1588 if !wtmode
1589 then (
1590 let ww = wadj + state.winw in
1591 let qx = sx / ww
1592 and qy = pdy / wh in
1593 let x = qx * ww
1594 and y = py + qy * wh in
1595 let x = if -x + ww > w1 then -(w1-ww) else x
1596 and y' = if y + wh > state.maxy then state.maxy - wh else y in
1597 let y =
1598 if conf.presentation
1599 then
1600 if abs (py - y') > wh
1601 then y'
1602 else py
1603 else y';
1605 (x, y)
1607 else (sx, sy)
1609 state.x <- x;
1610 gotoy_and_clear_text y;
1612 else gotoy_and_clear_text state.y;
1615 let gotopagexy pageno x y =
1616 match state.mode with
1617 | Birdseye _ -> gotopage pageno 0.0
1618 | Textentry _
1619 | View
1620 | LinkNav _ -> gotopagexy1 pageno x y
1623 let getpassword () =
1624 let passcmd = getenvwithdef "LLPP_ASKPASS" conf.passcmd in
1625 if emptystr passcmd
1626 then E.s
1627 else getcmdoutput
1628 (fun s ->
1629 showtext '!' @@ "error getting password: " ^ s;
1630 dolog "%s" s) passcmd;
1633 let act cmds =
1634 (* dolog "%S" cmds; *)
1635 let cl = splitatspace cmds in
1636 let scan s fmt f =
1637 try Scanf.sscanf s fmt f
1638 with exn ->
1639 dolog "error processing '%S': %s" cmds (exntos exn);
1640 exit 1
1642 let addoutline outline =
1643 match state.currently with
1644 | Outlining outlines ->
1645 state.currently <- Outlining (outline :: outlines)
1646 | Idle -> state.currently <- Outlining [outline]
1647 | Loading _
1648 | Tiling _ ->
1649 dolog "invalid outlining state";
1650 logcurrently state.currently
1652 match cl with
1653 | "clear" :: [] ->
1654 state.uioh#infochanged Pdim;
1655 state.pdims <- [];
1657 | "clearrects" :: [] ->
1658 state.rects <- state.rects1;
1659 G.postRedisplay "clearrects";
1661 | "continue" :: args :: [] ->
1662 let n = scan args "%u" (fun n -> n) in
1663 state.pagecount <- n;
1664 begin match state.currently with
1665 | Outlining l ->
1666 state.currently <- Idle;
1667 state.outlines <- Array.of_list (List.rev l)
1668 | Idle
1669 | Loading _
1670 | Tiling _ -> ()
1671 end;
1673 let cur, cmds = state.geomcmds in
1674 if emptystr cur
1675 then failwith "umpossible";
1677 begin match List.rev cmds with
1678 | [] ->
1679 state.geomcmds <- E.s, [];
1680 state.throttle <- None;
1681 represent ();
1682 | (s, f) :: rest ->
1683 f ();
1684 state.geomcmds <- s, List.rev rest;
1685 end;
1686 if conf.maxwait = None && not !wtmode
1687 then G.postRedisplay "continue";
1689 | "msg" :: args :: [] ->
1690 showtext ' ' args
1692 | "vmsg" :: args :: [] ->
1693 if conf.verbose
1694 then showtext ' ' args
1696 | "emsg" :: args :: [] ->
1697 Buffer.add_string state.errmsgs args;
1698 state.newerrmsgs <- true;
1699 G.postRedisplay "error message"
1701 | "progress" :: args :: [] ->
1702 let progress, text =
1703 scan args "%f %n"
1704 (fun f pos ->
1705 f, String.sub args pos (String.length args - pos))
1707 state.text <- text;
1708 state.progress <- progress;
1709 G.postRedisplay "progress"
1711 | "firstmatch" :: args :: [] ->
1712 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1713 scan args "%u %d %f %f %f %f %f %f %f %f"
1714 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1715 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1717 let xoff = float (xadjsb ()) in
1718 let x0 = x0 +. xoff
1719 and x1 = x1 +. xoff
1720 and x2 = x2 +. xoff
1721 and x3 = x3 +. xoff in
1722 let y = (getpagey pageno) + truncate y0 in
1723 addnav ();
1724 gotoy y;
1725 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
1727 | "match" :: args :: [] ->
1728 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1729 scan args "%u %d %f %f %f %f %f %f %f %f"
1730 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1731 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1733 let xoff = float (xadjsb ()) in
1734 let x0 = x0 +. xoff
1735 and x1 = x1 +. xoff
1736 and x2 = x2 +. xoff
1737 and x3 = x3 +. xoff in
1738 state.rects1 <-
1739 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1741 | "page" :: args :: [] ->
1742 let pageopaques, t = scan args "%s %f" (fun p t -> p, t) in
1743 let pageopaque = ~< pageopaques in
1744 begin match state.currently with
1745 | Loading (l, gen) ->
1746 vlog "page %d took %f sec" l.pageno t;
1747 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1748 begin match state.throttle with
1749 | None ->
1750 let preloadedpages =
1751 if conf.preload
1752 then preloadlayout state.y
1753 else state.layout
1755 let evict () =
1756 let set =
1757 List.fold_left (fun s l -> IntSet.add l.pageno s)
1758 IntSet.empty preloadedpages
1760 let evictedpages =
1761 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1762 if not (IntSet.mem pageno set)
1763 then (
1764 wcmd "freepage %s" (~> opaque);
1765 key :: accu
1767 else accu
1768 ) state.pagemap []
1770 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1772 evict ();
1773 state.currently <- Idle;
1774 if gen = state.gen
1775 then (
1776 tilepage l.pageno pageopaque state.layout;
1777 load state.layout;
1778 load preloadedpages;
1779 let visible = pagevisible state.layout l.pageno in
1780 if visible
1781 then (
1782 match state.mode with
1783 | LinkNav (Ltnotready (pageno, dir)) ->
1784 if pageno = l.pageno
1785 then (
1786 let link =
1787 let ld =
1788 if dir = 0
1789 then LDfirstvisible (l.pagex, l.pagey, dir)
1790 else (
1791 if dir > 0 then LDfirst else LDlast
1794 findlink pageopaque ld
1796 match link with
1797 | Lnotfound -> ()
1798 | Lfound n ->
1799 showlinktype (getlink pageopaque n);
1800 state.mode <- LinkNav (Ltexact (l.pageno, n))
1802 | LinkNav (Ltgendir _)
1803 | LinkNav (Ltexact _)
1804 | View
1805 | Birdseye _
1806 | Textentry _ -> ()
1809 if visible && layoutready state.layout
1810 then (
1811 G.postRedisplay "page";
1815 | Some (layout, _, _) ->
1816 state.currently <- Idle;
1817 tilepage l.pageno pageopaque layout;
1818 load state.layout
1819 end;
1821 | Idle
1822 | Tiling _
1823 | Outlining _ ->
1824 dolog "Inconsistent loading state";
1825 logcurrently state.currently;
1826 exit 1
1829 | "tile" :: args :: [] ->
1830 let (x, y, opaques, size, t) =
1831 scan args "%u %u %s %u %f"
1832 (fun x y p size t -> (x, y, p, size, t))
1834 let opaque = ~< opaques in
1835 begin match state.currently with
1836 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1837 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1839 unmappbo opaque;
1840 if tilew != conf.tilew || tileh != conf.tileh
1841 then (
1842 wcmd "freetile %s" (~> opaque);
1843 state.currently <- Idle;
1844 load state.layout;
1846 else (
1847 puttileopaque l col row gen cs angle opaque size t;
1848 state.memused <- state.memused + size;
1849 state.uioh#infochanged Memused;
1850 gctiles ();
1851 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1852 opaque, size) state.tilelru;
1854 let layout =
1855 match state.throttle with
1856 | None -> state.layout
1857 | Some (layout, _, _) -> layout
1860 state.currently <- Idle;
1861 if gen = state.gen
1862 && conf.colorspace = cs
1863 && conf.angle = angle
1864 && tilevisible layout l.pageno x y
1865 then conttiling l.pageno pageopaque;
1867 begin match state.throttle with
1868 | None ->
1869 preload state.layout;
1870 if gen = state.gen
1871 && conf.colorspace = cs
1872 && conf.angle = angle
1873 && tilevisible state.layout l.pageno x y
1874 && (not !wtmode || layoutready state.layout)
1875 then G.postRedisplay "tile nothrottle";
1877 | Some (layout, y, _) ->
1878 let ready = layoutready layout in
1879 if ready
1880 then (
1881 state.y <- y;
1882 state.layout <- layout;
1883 state.throttle <- None;
1884 G.postRedisplay "throttle";
1886 else load layout;
1887 end;
1890 | Idle
1891 | Loading _
1892 | Outlining _ ->
1893 dolog "Inconsistent tiling state";
1894 logcurrently state.currently;
1895 exit 1
1898 | "pdim" :: args :: [] ->
1899 let (n, w, h, _) as pdim =
1900 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1902 let pdim =
1903 match conf.fitmodel with
1904 | FitWidth -> pdim
1905 | FitPage | FitProportional ->
1906 match conf.columns with
1907 | Csplit _ -> (n, w, h, 0)
1908 | Csingle _ | Cmulti _ -> pdim
1910 state.uioh#infochanged Pdim;
1911 state.pdims <- pdim :: state.pdims
1913 | "o" :: args :: [] ->
1914 let (l, n, t, h, pos) =
1915 scan args "%u %u %d %u %n"
1916 (fun l n t h pos -> l, n, t, h, pos)
1918 let s = String.sub args pos (String.length args - pos) in
1919 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
1921 | "ou" :: args :: [] ->
1922 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
1923 let s = String.sub args pos len in
1924 let pos2 = pos + len + 1 in
1925 let uri = String.sub args pos2 (String.length args - pos2) in
1926 addoutline (s, l, Ouri uri)
1928 | "on" :: args :: [] ->
1929 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
1930 let s = String.sub args pos (String.length args - pos) in
1931 addoutline (s, l, Onone)
1933 | "a" :: args :: [] ->
1934 let (n, l, t) =
1935 scan args "%u %d %d" (fun n l t -> n, l, t)
1937 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
1939 | "info" :: args :: [] ->
1940 let pos = nindex args '\t' in
1941 if pos >= 0 && String.sub args 0 pos = "Title"
1942 then (
1943 let s = String.sub args (pos+1) @@ String.length args - pos - 1 in
1944 conf.title <- s;
1945 Wsi.settitle s;
1947 state.docinfo <- (1, args) :: state.docinfo
1949 | "infoend" :: [] ->
1950 state.uioh#infochanged Docinfo;
1951 state.docinfo <- List.rev state.docinfo
1953 | "pass" :: l ->
1954 if l = "fail" :: []
1955 then Wsi.settitle "Wrong password";
1956 let password = getpassword () in
1957 if password == E.s
1958 then error "document is password protected"
1959 else opendoc state.path password
1961 | _ ->
1962 error "unknown cmd `%S'" cmds
1965 let onhist cb =
1966 let rc = cb.rc in
1967 let action = function
1968 | HCprev -> cbget cb ~-1
1969 | HCnext -> cbget cb 1
1970 | HCfirst -> cbget cb ~-(cb.rc)
1971 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1972 and cancel () = cb.rc <- rc
1973 in (action, cancel)
1976 let search pattern forward =
1977 match conf.columns with
1978 | Csplit _ ->
1979 showtext '!' "searching does not work properly in split columns mode"
1980 | Csingle _
1981 | Cmulti _ ->
1982 if nonemptystr pattern
1983 then
1984 let pn, py =
1985 match state.layout with
1986 | [] -> 0, 0
1987 | l :: _ ->
1988 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1990 wcmd "search %d %d %d %d,%s\000"
1991 (btod conf.icase) pn py (btod forward) pattern;
1994 let intentry text key =
1995 let c =
1996 if key >= 32 && key < 127
1997 then Char.chr key
1998 else '\000'
2000 match c with
2001 | '0' .. '9' ->
2002 let text = addchar text c in
2003 TEcont text
2005 | _ ->
2006 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2007 TEcont text
2010 let linknentry text key =
2011 let c =
2012 if key >= 32 && key < 127
2013 then Char.chr key
2014 else '\000'
2016 match c with
2017 | 'a' .. 'z' ->
2018 let text = addchar text c in
2019 TEcont text
2021 | _ ->
2022 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2023 TEcont text
2026 let linkndone f s =
2027 if nonemptystr s
2028 then (
2029 let n =
2030 let l = String.length s in
2031 let rec loop pos n = if pos = l then n else
2032 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2033 loop (pos+1) (n*26 + m)
2034 in loop 0 0
2036 let rec loop n = function
2037 | [] -> ()
2038 | l :: rest ->
2039 match getopaque l.pageno with
2040 | None -> loop n rest
2041 | Some opaque ->
2042 let m = getlinkcount opaque in
2043 if n < m
2044 then (
2045 let under = getlink opaque n in
2046 f under
2048 else loop (n-m) rest
2050 loop n state.layout;
2054 let textentry text key =
2055 if key land 0xff00 = 0xff00
2056 then TEcont text
2057 else TEcont (text ^ toutf8 key)
2060 let reqlayout angle fitmodel =
2061 match state.throttle with
2062 | None ->
2063 if nogeomcmds state.geomcmds
2064 then state.anchor <- getanchor ();
2065 conf.angle <- angle mod 360;
2066 if conf.angle != 0
2067 then (
2068 match state.mode with
2069 | LinkNav _ -> state.mode <- View
2070 | Birdseye _
2071 | Textentry _
2072 | View -> ()
2074 conf.fitmodel <- fitmodel;
2075 invalidate "reqlayout"
2076 (fun () ->
2077 wcmd "reqlayout %d %d %d"
2078 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
2080 | _ -> ()
2083 let settrim trimmargins trimfuzz =
2084 if nogeomcmds state.geomcmds
2085 then state.anchor <- getanchor ();
2086 conf.trimmargins <- trimmargins;
2087 conf.trimfuzz <- trimfuzz;
2088 let x0, y0, x1, y1 = trimfuzz in
2089 invalidate "settrim"
2090 (fun () ->
2091 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2092 flushpages ();
2095 let setzoom zoom =
2096 match state.throttle with
2097 | None ->
2098 let zoom = max 0.0001 zoom in
2099 if zoom <> conf.zoom
2100 then (
2101 state.prevzoom <- (conf.zoom, state.x);
2102 conf.zoom <- zoom;
2103 reshape state.winw state.winh;
2104 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2107 | Some (layout, y, started) ->
2108 let time =
2109 match conf.maxwait with
2110 | None -> 0.0
2111 | Some t -> t
2113 let dt = now () -. started in
2114 if dt > time
2115 then (
2116 state.y <- y;
2117 load layout;
2121 let setcolumns mode columns coverA coverB =
2122 state.prevcolumns <- Some (conf.columns, conf.zoom);
2123 if columns < 0
2124 then (
2125 if isbirdseye mode
2126 then showtext '!' "split mode doesn't work in bird's eye"
2127 else (
2128 conf.columns <- Csplit (-columns, E.a);
2129 state.x <- 0;
2130 conf.zoom <- 1.0;
2133 else (
2134 if columns < 2
2135 then (
2136 conf.columns <- Csingle E.a;
2137 state.x <- 0;
2138 setzoom 1.0;
2140 else (
2141 conf.columns <- Cmulti ((columns, coverA, coverB), E.a);
2142 conf.zoom <- 1.0;
2145 reshape state.winw state.winh;
2148 let resetmstate () =
2149 state.mstate <- Mnone;
2150 Wsi.setcursor Wsi.CURSOR_INHERIT;
2153 let enterbirdseye () =
2154 let zoom = float conf.thumbw /. float state.winw in
2155 let birdseyepageno =
2156 let cy = state.winh / 2 in
2157 let fold = function
2158 | [] -> 0
2159 | l :: rest ->
2160 let rec fold best = function
2161 | [] -> best.pageno
2162 | l :: rest ->
2163 let d = cy - (l.pagedispy + l.pagevh/2)
2164 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2165 if abs d < abs dbest
2166 then fold l rest
2167 else best.pageno
2168 in fold l rest
2170 fold state.layout
2172 state.mode <- Birdseye (
2173 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2175 resetmstate ();
2176 conf.zoom <- zoom;
2177 conf.presentation <- false;
2178 conf.interpagespace <- 10;
2179 conf.hlinks <- false;
2180 conf.fitmodel <- FitPage;
2181 state.x <- 0;
2182 conf.maxwait <- None;
2183 conf.columns <- (
2184 match conf.beyecolumns with
2185 | Some c ->
2186 conf.zoom <- 1.0;
2187 Cmulti ((c, 0, 0), E.a)
2188 | None -> Csingle E.a
2190 if conf.verbose
2191 then
2192 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2193 (100.0*.zoom)
2194 else
2195 state.text <- E.s
2197 reshape state.winw state.winh;
2200 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2201 state.mode <- View;
2202 conf.zoom <- c.zoom;
2203 conf.presentation <- c.presentation;
2204 conf.interpagespace <- c.interpagespace;
2205 conf.maxwait <- c.maxwait;
2206 conf.hlinks <- c.hlinks;
2207 conf.fitmodel <- c.fitmodel;
2208 conf.beyecolumns <- (
2209 match conf.columns with
2210 | Cmulti ((c, _, _), _) -> Some c
2211 | Csingle _ -> None
2212 | Csplit _ -> failwith "leaving bird's eye split mode"
2214 conf.columns <- (
2215 match c.columns with
2216 | Cmulti (c, _) -> Cmulti (c, E.a)
2217 | Csingle _ -> Csingle E.a
2218 | Csplit (c, _) -> Csplit (c, E.a)
2220 if conf.verbose
2221 then
2222 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2223 (100.0*.conf.zoom)
2225 reshape state.winw state.winh;
2226 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2227 state.x <- leftx;
2230 let togglebirdseye () =
2231 match state.mode with
2232 | Birdseye vals -> leavebirdseye vals true
2233 | View -> enterbirdseye ()
2234 | Textentry _
2235 | LinkNav _ -> ()
2238 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2239 let pageno = max 0 (pageno - incr) in
2240 let rec loop = function
2241 | [] -> gotopage1 pageno 0
2242 | l :: _ when l.pageno = pageno ->
2243 if l.pagedispy >= 0 && l.pagey = 0
2244 then G.postRedisplay "upbirdseye"
2245 else gotopage1 pageno 0
2246 | _ :: rest -> loop rest
2248 loop state.layout;
2249 state.text <- E.s;
2250 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2253 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2254 let pageno = min (state.pagecount - 1) (pageno + incr) in
2255 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2256 let rec loop = function
2257 | [] ->
2258 let y, h = getpageyh pageno in
2259 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2260 gotoy (clamp dy)
2261 | l :: _ when l.pageno = pageno ->
2262 if l.pagevh != l.pageh
2263 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2264 else G.postRedisplay "downbirdseye"
2265 | _ :: rest -> loop rest
2267 loop state.layout;
2268 state.text <- E.s;
2271 let optentry mode _ key =
2272 let btos b = if b then "on" else "off" in
2273 if key >= 32 && key < 127
2274 then
2275 let c = Char.chr key in
2276 match c with
2277 | 's' ->
2278 let ondone s =
2279 try conf.scrollstep <- int_of_string s with exc ->
2280 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2282 TEswitch ("scroll step: ", E.s, None, intentry, ondone, true)
2284 | 'A' ->
2285 let ondone s =
2287 conf.autoscrollstep <- boundastep state.winh (int_of_string s);
2288 if state.autoscroll <> None
2289 then state.autoscroll <- Some conf.autoscrollstep
2290 with exc ->
2291 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2293 TEswitch ("auto scroll step: ", E.s, None, intentry, ondone, true)
2295 | 'C' ->
2296 let ondone s =
2298 let n, a, b = multicolumns_of_string s in
2299 setcolumns mode n a b;
2300 with exc ->
2301 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
2303 TEswitch ("columns: ", E.s, None, textentry, ondone, true)
2305 | 'Z' ->
2306 let ondone s =
2308 let zoom = float (int_of_string s) /. 100.0 in
2309 setzoom zoom
2310 with exc ->
2311 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2313 TEswitch ("zoom: ", E.s, None, intentry, ondone, true)
2315 | 't' ->
2316 let ondone s =
2318 conf.thumbw <- bound (int_of_string s) 2 4096;
2319 state.text <-
2320 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2321 begin match mode with
2322 | Birdseye beye ->
2323 leavebirdseye beye false;
2324 enterbirdseye ();
2325 | Textentry _
2326 | View
2327 | LinkNav _ -> ();
2329 with exc ->
2330 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2332 TEswitch ("thumbnail width: ", E.s, None, intentry, ondone, true)
2334 | 'R' ->
2335 let ondone s =
2336 match try
2337 Some (int_of_string s)
2338 with exc ->
2339 state.text <- Printf.sprintf "bad integer `%s': %s"
2340 s (exntos exc);
2341 None
2342 with
2343 | Some angle -> reqlayout angle conf.fitmodel
2344 | None -> ()
2346 TEswitch ("rotation: ", E.s, None, intentry, ondone, true)
2348 | 'i' ->
2349 conf.icase <- not conf.icase;
2350 TEdone ("case insensitive search " ^ (btos conf.icase))
2352 | 'p' ->
2353 conf.preload <- not conf.preload;
2354 gotoy state.y;
2355 TEdone ("preload " ^ (btos conf.preload))
2357 | 'v' ->
2358 conf.verbose <- not conf.verbose;
2359 TEdone ("verbose " ^ (btos conf.verbose))
2361 | 'd' ->
2362 conf.debug <- not conf.debug;
2363 TEdone ("debug " ^ (btos conf.debug))
2365 | 'h' ->
2366 conf.maxhfit <- not conf.maxhfit;
2367 state.maxy <- calcheight ();
2368 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2370 | 'c' ->
2371 conf.crophack <- not conf.crophack;
2372 TEdone ("crophack " ^ btos conf.crophack)
2374 | 'a' ->
2375 let s =
2376 match conf.maxwait with
2377 | None ->
2378 conf.maxwait <- Some infinity;
2379 "always wait for page to complete"
2380 | Some _ ->
2381 conf.maxwait <- None;
2382 "show placeholder if page is not ready"
2384 TEdone s
2386 | 'f' ->
2387 conf.underinfo <- not conf.underinfo;
2388 TEdone ("underinfo " ^ btos conf.underinfo)
2390 | 'P' ->
2391 conf.savebmarks <- not conf.savebmarks;
2392 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2394 | 'S' ->
2395 let ondone s =
2397 let pageno, py =
2398 match state.layout with
2399 | [] -> 0, 0
2400 | l :: _ ->
2401 l.pageno, l.pagey
2403 conf.interpagespace <- int_of_string s;
2404 docolumns conf.columns;
2405 state.maxy <- calcheight ();
2406 let y = getpagey pageno in
2407 gotoy (y + py)
2408 with exc ->
2409 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2411 TEswitch ("vertical margin: ", E.s, None, intentry, ondone, true)
2413 | 'l' ->
2414 let fm =
2415 match conf.fitmodel with
2416 | FitProportional -> FitWidth
2417 | FitWidth | FitPage -> FitProportional
2419 reqlayout conf.angle fm;
2420 TEdone ("proportional display " ^ btos (fm == FitProportional))
2422 | 'T' ->
2423 settrim (not conf.trimmargins) conf.trimfuzz;
2424 TEdone ("trim margins " ^ btos conf.trimmargins)
2426 | 'I' ->
2427 conf.invert <- not conf.invert;
2428 TEdone ("invert colors " ^ btos conf.invert)
2430 | 'x' ->
2431 let ondone s =
2432 cbput state.hists.sel s;
2433 conf.selcmd <- s;
2435 TEswitch ("selection command: ", E.s, Some (onhist state.hists.sel),
2436 textentry, ondone, true)
2438 | 'M' ->
2439 if conf.pax == None
2440 then conf.pax <- Some (ref (0.0, 0, 0))
2441 else conf.pax <- None;
2442 TEdone ("PAX " ^ btos (conf.pax != None))
2444 | _ ->
2445 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2446 TEstop
2447 else
2448 TEcont state.text
2451 class type lvsource = object
2452 method getitemcount : int
2453 method getitem : int -> (string * int)
2454 method hasaction : int -> bool
2455 method exit :
2456 uioh:uioh ->
2457 cancel:bool ->
2458 active:int ->
2459 first:int ->
2460 pan:int ->
2461 uioh option
2462 method getactive : int
2463 method getfirst : int
2464 method getpan : int
2465 method getminfo : (int * int) array
2466 end;;
2468 class virtual lvsourcebase = object
2469 val mutable m_active = 0
2470 val mutable m_first = 0
2471 val mutable m_pan = 0
2472 method getactive = m_active
2473 method getfirst = m_first
2474 method getpan = m_pan
2475 method getminfo : (int * int) array = E.a
2476 end;;
2478 let textentrykeyboard
2479 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2480 let key =
2481 if key >= 0xffb0 && key <= 0xffb9
2482 then key - 0xffb0 + 48 else key
2484 let enttext te =
2485 state.mode <- Textentry (te, onleave);
2486 state.text <- E.s;
2487 enttext ();
2488 G.postRedisplay "textentrykeyboard enttext";
2490 let histaction cmd =
2491 match opthist with
2492 | None -> ()
2493 | Some (action, _) ->
2494 state.mode <- Textentry (
2495 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
2497 G.postRedisplay "textentry histaction"
2499 match key with
2500 | @backspace ->
2501 if emptystr text && cancelonempty
2502 then (
2503 onleave Cancel;
2504 G.postRedisplay "textentrykeyboard after cancel";
2506 else
2507 let s = withoutlastutf8 text in
2508 enttext (c, s, opthist, onkey, ondone, cancelonempty)
2510 | @enter | @kpenter ->
2511 ondone text;
2512 onleave Confirm;
2513 G.postRedisplay "textentrykeyboard after confirm"
2515 | @up | @kpup -> histaction HCprev
2516 | @down | @kpdown -> histaction HCnext
2517 | @home | @kphome -> histaction HCfirst
2518 | @jend | @kpend -> histaction HClast
2520 | @escape ->
2521 if emptystr text
2522 then (
2523 begin match opthist with
2524 | None -> ()
2525 | Some (_, onhistcancel) -> onhistcancel ()
2526 end;
2527 onleave Cancel;
2528 state.text <- E.s;
2529 G.postRedisplay "textentrykeyboard after cancel2"
2531 else (
2532 enttext (c, E.s, opthist, onkey, ondone, cancelonempty)
2535 | @delete | @kpdelete -> ()
2537 | _ when key != 0
2538 && key land 0xff00 != 0xff00 (* keyboard *)
2539 && key land 0xfe00 != 0xfe00 (* xkb *)
2540 && key land 0xfd00 != 0xfd00 (* 3270 *)
2542 begin match onkey text key with
2543 | TEdone text ->
2544 ondone text;
2545 onleave Confirm;
2546 G.postRedisplay "textentrykeyboard after confirm2";
2548 | TEcont text ->
2549 enttext (c, text, opthist, onkey, ondone, cancelonempty);
2551 | TEstop ->
2552 onleave Cancel;
2553 G.postRedisplay "textentrykeyboard after cancel3"
2555 | TEswitch te ->
2556 state.mode <- Textentry (te, onleave);
2557 G.postRedisplay "textentrykeyboard switch";
2558 end;
2560 | _ ->
2561 vlog "unhandled key %s" (Wsi.keyname key)
2564 let firstof first active =
2565 if first > active || abs (first - active) > fstate.maxrows - 1
2566 then max 0 (active - (fstate.maxrows/2))
2567 else first
2570 let calcfirst first active =
2571 if active > first
2572 then
2573 let rows = active - first in
2574 if rows > fstate.maxrows then active - fstate.maxrows else first
2575 else active
2578 let scrollph y maxy =
2579 let sh = float (maxy + state.winh) /. float state.winh in
2580 let sh = float state.winh /. sh in
2581 let sh = max sh (float conf.scrollh) in
2583 let percent = float y /. float maxy in
2584 let position = (float state.winh -. sh) *. percent in
2586 let position =
2587 if position +. sh > float state.winh
2588 then float state.winh -. sh
2589 else position
2591 position, sh;
2594 let coe s = (s :> uioh);;
2596 class listview ~zebra ~helpmode ~(source:lvsource) ~trusted ~modehash =
2597 object (self)
2598 val m_pan = source#getpan
2599 val m_first = source#getfirst
2600 val m_active = source#getactive
2601 val m_qsearch = E.s
2602 val m_prev_uioh = state.uioh
2604 method private elemunder y =
2605 if y < 0
2606 then None
2607 else
2608 let n = y / (fstate.fontsize+1) in
2609 if m_first + n < source#getitemcount
2610 then (
2611 if source#hasaction (m_first + n)
2612 then Some (m_first + n)
2613 else None
2615 else None
2617 method display =
2618 Gl.enable `blend;
2619 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
2620 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2621 filledrect 0. 0. (float state.winw) (float state.winh);
2622 GlDraw.color (1., 1., 1.);
2623 Gl.enable `texture_2d;
2624 let fs = fstate.fontsize in
2625 let nfs = fs + 1 in
2626 let hw = (wadjsb () + xadjsb () + state.winw)/3 in
2627 let ww = fstate.wwidth in
2628 let tabw = 17.0*.ww in
2629 let itemcount = source#getitemcount in
2630 let minfo = source#getminfo in
2631 let x0, x1 =
2632 if conf.leftscroll
2633 then float (xadjsb ()), float (state.winw - 1)
2634 else 0.0, float (state.winw - conf.scrollbw - 1)
2636 let xadj = xadjsb () in
2637 let rec loop row =
2638 if (row - m_first) > fstate.maxrows
2639 then ()
2640 else (
2641 if row >= 0 && row < itemcount
2642 then (
2643 let (s, level) = source#getitem row in
2644 let y = (row - m_first) * nfs in
2645 let x =
2646 (if conf.leftscroll then float xadj else 5.0)
2647 +. (float (level + m_pan)) *. ww in
2648 if helpmode
2649 then GlDraw.color
2650 (let c = if row land 1 = 0 then 1.0 else 0.92 in (c,c,c));
2652 if row = m_active
2653 then (
2654 Gl.disable `texture_2d;
2655 let alpha = if source#hasaction row then 0.9 else 0.3 in
2656 GlDraw.color (1., 1., 1.) ~alpha;
2657 linerect (x0 +. 1.) (float (y + 1)) (x1) (float (y + fs + 3));
2658 Gl.enable `texture_2d;
2660 let c =
2661 if zebra && row land 1 = 1
2662 then 0.8
2663 else 1.0
2665 GlDraw.color (c,c,c);
2666 let drawtabularstring s =
2667 let drawstr x s =
2668 let x' = truncate (x0 +. x) in
2669 let pos = nindex s '\000' in
2670 if pos = -1
2671 then drawstring1 fs x' (y+nfs) s
2672 else
2673 let s1 = String.sub s 0 pos
2674 and s2 = String.sub s (pos+1) (String.length s - pos - 1) in
2675 let rec e s =
2676 if emptystr s
2677 then s
2678 else
2679 let s' = withoutlastutf8 s in
2680 let s = s' ^ "@Uellipsis" in
2681 let w = measurestr fs s in
2682 if float x' +. w +. ww < float (hw + x')
2683 then s
2684 else e s'
2686 let s1 =
2687 if float x' +. ww +. measurestr fs s1 > float (hw + x')
2688 then e s1
2689 else s1
2691 ignore (drawstring1 fs x' (y+nfs) s1);
2692 drawstring1 fs (hw + x') (y+nfs) s2
2694 if trusted
2695 then
2696 let x = if helpmode && row > 0 then x +. ww else x in
2697 let tabpos = nindex s '\t' in
2698 if tabpos > 0
2699 then
2700 let len = String.length s - tabpos - 1 in
2701 let s1 = String.sub s 0 tabpos
2702 and s2 = String.sub s (tabpos + 1) len in
2703 let nx = drawstr x s1 in
2704 let sw = nx -. x in
2705 let x = x +. (max tabw sw) in
2706 drawstr x s2
2707 else
2708 let len = String.length s - 2 in
2709 if len > 0 && s.[0] = '\xc2' && s.[1] = '\xb7'
2710 then
2711 let s = String.sub s 2 len in
2712 let x = if not helpmode then x +. ww else x in
2713 GlDraw.color (1.2, 1.2, 1.2);
2714 let vinc = drawstring1 (fs+fs/4)
2715 (truncate (x -. ww)) (y+nfs) s in
2716 GlDraw.color (1., 1., 1.);
2717 vinc +. (float fs *. 0.8)
2718 else
2719 drawstr x s
2720 else
2721 drawstr x s
2723 ignore (drawtabularstring s);
2724 loop (row+1)
2728 loop m_first;
2729 GlDraw.color (1.0, 1.0, 1.0) ~alpha:0.5;
2730 let xadj = float (xadjsb () + 5) in
2731 let rec loop row =
2732 if (row - m_first) > fstate.maxrows
2733 then ()
2734 else (
2735 if row >= 0 && row < itemcount
2736 then (
2737 let (s, level) = source#getitem row in
2738 let pos0 = nindex s '\000' in
2739 let y = (row - m_first) * nfs in
2740 let x = float (level + m_pan) *. ww in
2741 let (first, last) = minfo.(row) in
2742 let prefix =
2743 if pos0 > 0 && first > pos0
2744 then String.sub s (pos0+1) (first-pos0-1)
2745 else String.sub s 0 first
2747 let suffix = String.sub s first (last - first) in
2748 let w1 = measurestr fstate.fontsize prefix in
2749 let w2 = measurestr fstate.fontsize suffix in
2750 let x = x +. if conf.leftscroll then xadj else 5.0 in
2751 let x = if pos0 > 0 && first > pos0 then x +. float hw else x in
2752 let x0 = x +. w1
2753 and y0 = float (y+2) in
2754 let x1 = x0 +. w2
2755 and y1 = float (y+fs+3) in
2756 filledrect x0 y0 x1 y1;
2757 loop (row+1)
2761 Gl.disable `texture_2d;
2762 if Array.length minfo > 0 then loop m_first;
2763 Gl.disable `blend;
2765 method updownlevel incr =
2766 let len = source#getitemcount in
2767 let curlevel =
2768 if m_active >= 0 && m_active < len
2769 then snd (source#getitem m_active)
2770 else -1
2772 let rec flow i =
2773 if i = len then i-1 else if i = -1 then 0 else
2774 let _, l = source#getitem i in
2775 if l != curlevel then i else flow (i+incr)
2777 let active = flow m_active in
2778 let first = calcfirst m_first active in
2779 G.postRedisplay "outline updownlevel";
2780 {< m_active = active; m_first = first >}
2782 method private key1 key mask =
2783 let set1 active first qsearch =
2784 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2786 let search active pattern incr =
2787 let active = if active = -1 then m_first else active in
2788 let dosearch re =
2789 let rec loop n =
2790 if n >= 0 && n < source#getitemcount
2791 then (
2792 let s, _ = source#getitem n in
2794 (try ignore (Str.search_forward re s 0); true
2795 with Not_found -> false)
2796 then Some n
2797 else loop (n + incr)
2799 else None
2801 loop active
2804 let re = Str.regexp_case_fold pattern in
2805 dosearch re
2806 with Failure s ->
2807 state.text <- s;
2808 None
2810 let itemcount = source#getitemcount in
2811 let find start incr =
2812 let rec find i =
2813 if i = -1 || i = itemcount
2814 then -1
2815 else (
2816 if source#hasaction i
2817 then i
2818 else find (i + incr)
2821 find start
2823 let set active first =
2824 let first = bound first 0 (itemcount - fstate.maxrows) in
2825 state.text <- E.s;
2826 coe {< m_active = active; m_first = first; m_qsearch = E.s >}
2828 let navigate incr =
2829 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2830 let active, first =
2831 let incr1 = if incr > 0 then 1 else -1 in
2832 if isvisible m_first m_active
2833 then
2834 let next =
2835 let next = m_active + incr in
2836 let next =
2837 if next < 0 || next >= itemcount
2838 then -1
2839 else find next incr1
2841 if abs (m_active - next) > fstate.maxrows
2842 then -1
2843 else next
2845 if next = -1
2846 then
2847 let first = m_first + incr in
2848 let first = bound first 0 (itemcount - fstate.maxrows) in
2849 let next =
2850 let next = m_active + incr in
2851 let next = bound next 0 (itemcount - 1) in
2852 find next ~-incr1
2854 let active =
2855 if next = -1
2856 then m_active
2857 else (
2858 if isvisible first next
2859 then next
2860 else m_active
2863 active, first
2864 else
2865 let first = min next m_first in
2866 let first =
2867 if abs (next - first) > fstate.maxrows
2868 then first + incr
2869 else first
2871 next, first
2872 else
2873 let first = m_first + incr in
2874 let first = bound first 0 (itemcount - 1) in
2875 let active =
2876 let next = m_active + incr in
2877 let next = bound next 0 (itemcount - 1) in
2878 let next = find next incr1 in
2879 let active =
2880 if next = -1 || abs (m_active - first) > fstate.maxrows
2881 then (
2882 let active = if m_active = -1 then next else m_active in
2883 active
2885 else next
2887 if isvisible first active
2888 then active
2889 else -1
2891 active, first
2893 G.postRedisplay "listview navigate";
2894 set active first;
2896 match key with
2897 | (@r|@s) when Wsi.withctrl mask ->
2898 let incr = if key = @r then -1 else 1 in
2899 let active, first =
2900 match search (m_active + incr) m_qsearch incr with
2901 | None ->
2902 state.text <- m_qsearch ^ " [not found]";
2903 m_active, m_first
2904 | Some active ->
2905 state.text <- m_qsearch;
2906 active, firstof m_first active
2908 G.postRedisplay "listview ctrl-r/s";
2909 set1 active first m_qsearch;
2911 | @insert when Wsi.withctrl mask ->
2912 if m_active >= 0 && m_active < source#getitemcount
2913 then (
2914 let s, _ = source#getitem m_active in
2915 selstring s;
2917 coe self
2919 | @backspace ->
2920 if emptystr m_qsearch
2921 then coe self
2922 else (
2923 let qsearch = withoutlastutf8 m_qsearch in
2924 if emptystr qsearch
2925 then (
2926 state.text <- E.s;
2927 G.postRedisplay "listview empty qsearch";
2928 set1 m_active m_first E.s;
2930 else
2931 let active, first =
2932 match search m_active qsearch ~-1 with
2933 | None ->
2934 state.text <- qsearch ^ " [not found]";
2935 m_active, m_first
2936 | Some active ->
2937 state.text <- qsearch;
2938 active, firstof m_first active
2940 G.postRedisplay "listview backspace qsearch";
2941 set1 active first qsearch
2944 | key when (key != 0 && key land 0xff00 != 0xff00) ->
2945 let pattern = m_qsearch ^ toutf8 key in
2946 let active, first =
2947 match search m_active pattern 1 with
2948 | None ->
2949 state.text <- pattern ^ " [not found]";
2950 m_active, m_first
2951 | Some active ->
2952 state.text <- pattern;
2953 active, firstof m_first active
2955 G.postRedisplay "listview qsearch add";
2956 set1 active first pattern;
2958 | @escape ->
2959 state.text <- E.s;
2960 if emptystr m_qsearch
2961 then (
2962 G.postRedisplay "list view escape";
2963 begin
2964 match
2965 source#exit ~uioh:(coe self)
2966 ~cancel:true ~active:m_active ~first:m_first ~pan:m_pan
2967 with
2968 | None -> m_prev_uioh
2969 | Some uioh -> uioh
2972 else (
2973 G.postRedisplay "list view kill qsearch";
2974 coe {< m_qsearch = E.s >}
2977 | @enter | @kpenter ->
2978 state.text <- E.s;
2979 let self = {< m_qsearch = E.s >} in
2980 let opt =
2981 G.postRedisplay "listview enter";
2982 if m_active >= 0 && m_active < source#getitemcount
2983 then (
2984 source#exit ~uioh:(coe self) ~cancel:false
2985 ~active:m_active ~first:m_first ~pan:m_pan;
2987 else (
2988 source#exit ~uioh:(coe self) ~cancel:true
2989 ~active:m_active ~first:m_first ~pan:m_pan;
2992 begin match opt with
2993 | None -> m_prev_uioh
2994 | Some uioh -> uioh
2997 | @delete | @kpdelete ->
2998 coe self
3000 | @up | @kpup -> navigate ~-1
3001 | @down | @kpdown -> navigate 1
3002 | @prior | @kpprior -> navigate ~-(fstate.maxrows)
3003 | @next | @kpnext -> navigate fstate.maxrows
3005 | @right | @kpright ->
3006 state.text <- E.s;
3007 G.postRedisplay "listview right";
3008 coe {< m_pan = m_pan - 1 >}
3010 | @left | @kpleft ->
3011 state.text <- E.s;
3012 G.postRedisplay "listview left";
3013 coe {< m_pan = m_pan + 1 >}
3015 | @home | @kphome ->
3016 let active = find 0 1 in
3017 G.postRedisplay "listview home";
3018 set active 0;
3020 | @jend | @kpend ->
3021 let first = max 0 (itemcount - fstate.maxrows) in
3022 let active = find (itemcount - 1) ~-1 in
3023 G.postRedisplay "listview end";
3024 set active first;
3026 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3027 coe self
3029 | _ ->
3030 dolog "listview unknown key %#x" key; coe self
3032 method key key mask =
3033 match state.mode with
3034 | Textentry te -> textentrykeyboard key mask te; coe self
3035 | Birdseye _
3036 | View
3037 | LinkNav _ -> self#key1 key mask
3039 method button button down x y _ =
3040 let opt =
3041 match button with
3042 | 1 when x > state.winw - conf.scrollbw ->
3043 G.postRedisplay "listview scroll";
3044 if down
3045 then
3046 let _, position, sh = self#scrollph in
3047 if y > truncate position && y < truncate (position +. sh)
3048 then (
3049 state.mstate <- Mscrolly;
3050 Some (coe self)
3052 else
3053 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3054 let first = truncate (s *. float source#getitemcount) in
3055 let first = min source#getitemcount first in
3056 Some (coe {< m_first = first; m_active = first >})
3057 else (
3058 state.mstate <- Mnone;
3059 Some (coe self);
3061 | 1 when down ->
3062 begin match self#elemunder y with
3063 | Some n ->
3064 G.postRedisplay "listview click";
3065 source#exit ~uioh:(coe {< m_active = n >})
3066 ~cancel:false ~active:n ~first:m_first ~pan:m_pan
3067 | _ ->
3068 Some (coe self)
3070 | n when (n == 4 || n == 5) && not down ->
3071 let len = source#getitemcount in
3072 let first =
3073 if n = 5 && m_first + fstate.maxrows >= len
3074 then
3075 m_first
3076 else
3077 let first = m_first + (if n == 4 then -1 else 1) in
3078 bound first 0 (len - 1)
3080 G.postRedisplay "listview wheel";
3081 Some (coe {< m_first = first >})
3082 | n when (n = 6 || n = 7) && not down ->
3083 let inc = if n = 7 then -1 else 1 in
3084 G.postRedisplay "listview hwheel";
3085 Some (coe {< m_pan = m_pan + inc >})
3086 | _ ->
3087 Some (coe self)
3089 match opt with
3090 | None -> m_prev_uioh
3091 | Some uioh -> uioh
3093 method multiclick _ x y = self#button 1 true x y
3095 method motion _ y =
3096 match state.mstate with
3097 | Mscrolly ->
3098 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3099 let first = truncate (s *. float source#getitemcount) in
3100 let first = min source#getitemcount first in
3101 G.postRedisplay "listview motion";
3102 coe {< m_first = first; m_active = first >}
3103 | Msel _
3104 | Mpan _
3105 | Mscrollx
3106 | Mzoom _
3107 | Mzoomrect _
3108 | Mnone -> coe self
3110 method pmotion x y =
3111 if x < state.winw - conf.scrollbw
3112 then
3113 let n =
3114 match self#elemunder y with
3115 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3116 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3118 let o =
3119 if n != m_active
3120 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3121 else self
3123 coe o
3124 else (
3125 Wsi.setcursor Wsi.CURSOR_INHERIT;
3126 coe self
3129 method infochanged _ = ()
3131 method scrollpw = (0, 0.0, 0.0)
3132 method scrollph =
3133 let nfs = fstate.fontsize + 1 in
3134 let y = m_first * nfs in
3135 let itemcount = source#getitemcount in
3136 let maxi = max 0 (itemcount - fstate.maxrows) in
3137 let maxy = maxi * nfs in
3138 let p, h = scrollph y maxy in
3139 conf.scrollbw, p, h
3141 method modehash = modehash
3142 method eformsgs = false
3143 method alwaysscrolly = true
3144 end;;
3146 class outlinelistview ~zebra ~source =
3147 let settext autonarrow s =
3148 if autonarrow
3149 then
3150 let ss = source#statestr in
3151 state.text <-
3152 if emptystr ss
3153 then "[" ^ s ^ "]"
3154 else "{" ^ ss ^ "} [" ^ s ^ "]"
3155 else state.text <- s
3157 object (self)
3158 inherit listview
3159 ~zebra
3160 ~helpmode:false
3161 ~source:(source :> lvsource)
3162 ~trusted:false
3163 ~modehash:(findkeyhash conf "outline")
3164 as super
3166 val m_autonarrow = false
3168 method! key key mask =
3169 let maxrows =
3170 if emptystr state.text
3171 then fstate.maxrows
3172 else fstate.maxrows - 2
3174 let calcfirst first active =
3175 if active > first
3176 then
3177 let rows = active - first in
3178 if rows > maxrows then active - maxrows else first
3179 else active
3181 let navigate incr =
3182 let active = m_active + incr in
3183 let active = bound active 0 (source#getitemcount - 1) in
3184 let first = calcfirst m_first active in
3185 G.postRedisplay "outline navigate";
3186 coe {< m_active = active; m_first = first >}
3188 let navscroll first =
3189 let active =
3190 let dist = m_active - first in
3191 if dist < 0
3192 then first
3193 else (
3194 if dist < maxrows
3195 then m_active
3196 else first + maxrows
3199 G.postRedisplay "outline navscroll";
3200 coe {< m_first = first; m_active = active >}
3202 let ctrl = Wsi.withctrl mask in
3203 match key with
3204 | @a when ctrl ->
3205 let text =
3206 if m_autonarrow
3207 then (source#denarrow; E.s)
3208 else (
3209 let pattern = source#renarrow in
3210 if nonemptystr m_qsearch
3211 then (source#narrow m_qsearch; m_qsearch)
3212 else pattern
3215 settext (not m_autonarrow) text;
3216 G.postRedisplay "toggle auto narrowing";
3217 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
3219 | @slash when emptystr m_qsearch && not m_autonarrow ->
3220 settext true E.s;
3221 G.postRedisplay "toggle auto narrowing";
3222 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
3224 | @n when ctrl ->
3225 source#narrow m_qsearch;
3226 if not m_autonarrow
3227 then source#add_narrow_pattern m_qsearch;
3228 G.postRedisplay "outline ctrl-n";
3229 coe {< m_first = 0; m_active = 0 >}
3231 | @S when ctrl ->
3232 let active = source#calcactive (getanchor ()) in
3233 let first = firstof m_first active in
3234 G.postRedisplay "outline ctrl-s";
3235 coe {< m_first = first; m_active = active >}
3237 | @u when ctrl ->
3238 G.postRedisplay "outline ctrl-u";
3239 if m_autonarrow && nonemptystr m_qsearch
3240 then (
3241 ignore (source#renarrow);
3242 settext m_autonarrow E.s;
3243 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3245 else (
3246 source#del_narrow_pattern;
3247 let pattern = source#renarrow in
3248 let text =
3249 if emptystr pattern then E.s else "Narrowed to " ^ pattern
3251 settext m_autonarrow text;
3252 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3255 | @l when ctrl ->
3256 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3257 G.postRedisplay "outline ctrl-l";
3258 coe {< m_first = first >}
3260 | @tab when m_autonarrow ->
3261 if nonemptystr m_qsearch
3262 then (
3263 G.postRedisplay "outline list view tab";
3264 source#add_narrow_pattern m_qsearch;
3265 settext true E.s;
3266 coe {< m_qsearch = E.s >}
3268 else coe self
3270 | @escape when m_autonarrow ->
3271 if nonemptystr m_qsearch
3272 then source#add_narrow_pattern m_qsearch;
3273 super#key key mask
3275 | @enter | @kpenter when m_autonarrow ->
3276 if nonemptystr m_qsearch
3277 then source#add_narrow_pattern m_qsearch;
3278 super#key key mask
3280 | key when m_autonarrow && (key != 0 && key land 0xff00 != 0xff00) ->
3281 let pattern = m_qsearch ^ toutf8 key in
3282 G.postRedisplay "outlinelistview autonarrow add";
3283 source#narrow pattern;
3284 settext true pattern;
3285 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3287 | key when m_autonarrow && key = @backspace ->
3288 if emptystr m_qsearch
3289 then coe self
3290 else
3291 let pattern = withoutlastutf8 m_qsearch in
3292 G.postRedisplay "outlinelistview autonarrow backspace";
3293 ignore (source#renarrow);
3294 source#narrow pattern;
3295 settext true pattern;
3296 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3298 | @delete | @kpdelete ->
3299 source#remove m_active;
3300 G.postRedisplay "outline delete";
3301 let active = max 0 (m_active-1) in
3302 coe {< m_first = firstof m_first active;
3303 m_active = active >}
3305 | @up | @kpup when ctrl ->
3306 navscroll (max 0 (m_first - 1))
3308 | @down | @kpdown when ctrl ->
3309 navscroll (min (source#getitemcount - 1) (m_first + 1))
3311 | @up | @kpup -> navigate ~-1
3312 | @down | @kpdown -> navigate 1
3313 | @prior | @kpprior -> navigate ~-(fstate.maxrows)
3314 | @next | @kpnext -> navigate fstate.maxrows
3316 | @right | @kpright ->
3317 let o =
3318 if ctrl
3319 then (
3320 G.postRedisplay "outline ctrl right";
3321 {< m_pan = m_pan + 1 >}
3323 else self#updownlevel 1
3325 coe o
3327 | @left | @kpleft ->
3328 let o =
3329 if ctrl
3330 then (
3331 G.postRedisplay "outline ctrl left";
3332 {< m_pan = m_pan - 1 >}
3334 else self#updownlevel ~-1
3336 coe o
3338 | @home | @kphome ->
3339 G.postRedisplay "outline home";
3340 coe {< m_first = 0; m_active = 0 >}
3342 | @jend | @kpend ->
3343 let active = source#getitemcount - 1 in
3344 let first = max 0 (active - fstate.maxrows) in
3345 G.postRedisplay "outline end";
3346 coe {< m_active = active; m_first = first >}
3348 | _ -> super#key key mask
3351 let genhistoutlines =
3352 let order ty (p1, c1, _, _, _) (p2, c2, _, _, _) =
3353 match ty with
3354 | `lastvisit -> compare c1.lastvisit c2.lastvisit
3355 | `path -> compare p2 p1
3356 | `file -> compare (Filename.basename p2) (Filename.basename p1)
3357 | `title ->
3358 let e1 = emptystr c1.title
3359 and e2 = emptystr c2.title in
3360 (**) if e1 && e2
3361 then compare (Filename.basename p2) (Filename.basename p1)
3362 else if e1 then -1
3363 else if e2 then 1
3364 else compare c1.title c2.title
3366 let showfullpath = ref false in
3367 fun orderty ->
3368 let setorty s t =
3369 let s = if orderty = t then "[@Uradical] " ^ s else "[ ] " ^ s in
3370 s, 0, Oaction (fun () -> Config.historder := t; reeenterhist := true)
3372 let list = ref [] in
3373 if Config.gethist list
3374 then
3375 let ol =
3376 List.fold_left
3377 (fun accu (path, c, b, x, a) ->
3378 let hist = (path, (c, b, x, a)) in
3379 let s = if !showfullpath then path else Filename.basename path in
3380 let base = mbtoutf8 s in
3381 (base ^ "\000" ^ c.title, 1, Ohistory hist) :: accu
3383 [ setorty "Sort by time of last visit" `lastvisit;
3384 setorty "Sort by file name" `file;
3385 setorty "Sort by path" `path;
3386 setorty "Sort by title" `title;
3387 (if !showfullpath then "@Uradical "
3388 else " ") ^ "Show full path", 0, Oaction (fun () ->
3389 showfullpath := not !showfullpath; reeenterhist := true)
3390 ] (List.sort (order orderty) !list)
3392 Array.of_list ol
3393 else E.a;
3396 let gotohist (path, (c, bookmarks, x, anchor)) =
3397 Config.save leavebirdseye;
3398 state.anchor <- anchor;
3399 state.bookmarks <- bookmarks;
3400 state.origin <- E.s;
3401 state.x <- x;
3402 setconf conf c;
3403 let x0, y0, x1, y1 = conf.trimfuzz in
3404 wcmd "trimset %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1;
3405 reshape ~firsttime:true state.winw state.winh;
3406 opendoc path E.s;
3407 setzoom c.zoom;
3410 let makecheckers () =
3411 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3412 following to say:
3413 converted by Issac Trotts. July 25, 2002 *)
3414 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3415 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3416 let id = GlTex.gen_texture () in
3417 GlTex.bind_texture ~target:`texture_2d id;
3418 GlPix.store (`unpack_alignment 1);
3419 GlTex.image2d image;
3420 List.iter (GlTex.parameter ~target:`texture_2d)
3421 [ `mag_filter `nearest; `min_filter `nearest ];
3425 let setcheckers enabled =
3426 match state.checkerstexid with
3427 | None ->
3428 if enabled then state.checkerstexid <- Some (makecheckers ())
3430 | Some checkerstexid ->
3431 if not enabled
3432 then (
3433 GlTex.delete_texture checkerstexid;
3434 state.checkerstexid <- None;
3438 let describe_location () =
3439 let fn = page_of_y state.y in
3440 let ln = page_of_y (state.y + state.winh - hscrollh () - 1) in
3441 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
3442 let percent =
3443 if maxy <= 0
3444 then 100.
3445 else (100. *. (float state.y /. float maxy))
3447 if fn = ln
3448 then
3449 Printf.sprintf "page %d of %d [%.2f%%]"
3450 (fn+1) state.pagecount percent
3451 else
3452 Printf.sprintf
3453 "pages %d-%d of %d [%.2f%%]"
3454 (fn+1) (ln+1) state.pagecount percent
3457 let setpresentationmode v =
3458 let n = page_of_y state.y in
3459 state.anchor <- (n, 0.0, 1.0);
3460 conf.presentation <- v;
3461 if conf.fitmodel = FitPage
3462 then reqlayout conf.angle conf.fitmodel;
3463 represent ();
3466 let enterinfomode =
3467 let btos b = if b then "@Uradical" else E.s in
3468 let showextended = ref false in
3469 let leave mode _ = state.mode <- mode in
3470 let src =
3471 (object
3472 val mutable m_first_time = true
3473 val mutable m_l = []
3474 val mutable m_a = E.a
3475 val mutable m_prev_uioh = nouioh
3476 val mutable m_prev_mode = View
3478 inherit lvsourcebase
3480 method reset prev_mode prev_uioh =
3481 m_a <- Array.of_list (List.rev m_l);
3482 m_l <- [];
3483 m_prev_mode <- prev_mode;
3484 m_prev_uioh <- prev_uioh;
3485 if m_first_time
3486 then (
3487 let rec loop n =
3488 if n >= Array.length m_a
3489 then ()
3490 else
3491 match m_a.(n) with
3492 | _, _, _, Action _ -> m_active <- n
3493 | _, _, _, Noaction -> loop (n+1)
3495 loop 0;
3496 m_first_time <- false;
3499 method int name get set =
3500 m_l <-
3501 (name, `int get, 1, Action (
3502 fun u ->
3503 let ondone s =
3504 try set (int_of_string s)
3505 with exn ->
3506 state.text <- Printf.sprintf "bad integer `%s': %s"
3507 s (exntos exn)
3509 state.text <- E.s;
3510 let te = name ^ ": ", E.s, None, intentry, ondone, true in
3511 state.mode <- Textentry (te, leave m_prev_mode);
3513 )) :: m_l
3515 method int_with_suffix name get set =
3516 m_l <-
3517 (name, `intws get, 1, Action (
3518 fun u ->
3519 let ondone s =
3520 try set (int_of_string_with_suffix s)
3521 with exn ->
3522 state.text <- Printf.sprintf "bad integer `%s': %s"
3523 s (exntos exn)
3525 state.text <- E.s;
3526 let te =
3527 name ^ ": ", E.s, None, intentry_with_suffix, ondone, true
3529 state.mode <- Textentry (te, leave m_prev_mode);
3531 )) :: m_l
3533 method bool ?(offset=1) ?(btos=btos) name get set =
3534 m_l <-
3535 (name, `bool (btos, get), offset, Action (
3536 fun u ->
3537 let v = get () in
3538 set (not v);
3540 )) :: m_l
3542 method color name get set =
3543 m_l <-
3544 (name, `color get, 1, Action (
3545 fun u ->
3546 let invalid = (nan, nan, nan) in
3547 let ondone s =
3548 let c =
3549 try color_of_string s
3550 with exn ->
3551 state.text <- Printf.sprintf "bad color `%s': %s"
3552 s (exntos exn);
3553 invalid
3555 if c <> invalid
3556 then set c;
3558 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3559 state.text <- color_to_string (get ());
3560 state.mode <- Textentry (te, leave m_prev_mode);
3562 )) :: m_l
3564 method string name get set =
3565 m_l <-
3566 (name, `string get, 1, Action (
3567 fun u ->
3568 let ondone s = set s in
3569 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3570 state.mode <- Textentry (te, leave m_prev_mode);
3572 )) :: m_l
3574 method colorspace name get set =
3575 m_l <-
3576 (name, `string get, 1, Action (
3577 fun _ ->
3578 let source =
3579 (object
3580 inherit lvsourcebase
3582 initializer
3583 m_active <- CSTE.to_int conf.colorspace;
3584 m_first <- 0;
3586 method getitemcount =
3587 Array.length CSTE.names
3588 method getitem n =
3589 (CSTE.names.(n), 0)
3590 method exit ~uioh ~cancel ~active ~first ~pan =
3591 ignore (uioh, first, pan);
3592 if not cancel then set active;
3593 None
3594 method hasaction _ = true
3595 end)
3597 state.text <- E.s;
3598 let modehash = findkeyhash conf "info" in
3599 coe (new listview ~zebra:false ~helpmode:false
3600 ~source ~trusted:true ~modehash)
3601 )) :: m_l
3603 method paxmark name get set =
3604 m_l <-
3605 (name, `string get, 1, Action (
3606 fun _ ->
3607 let source =
3608 (object
3609 inherit lvsourcebase
3611 initializer
3612 m_active <- MTE.to_int conf.paxmark;
3613 m_first <- 0;
3615 method getitemcount = Array.length MTE.names
3616 method getitem n = (MTE.names.(n), 0)
3617 method exit ~uioh ~cancel ~active ~first ~pan =
3618 ignore (uioh, first, pan);
3619 if not cancel then set active;
3620 None
3621 method hasaction _ = true
3622 end)
3624 state.text <- E.s;
3625 let modehash = findkeyhash conf "info" in
3626 coe (new listview ~zebra:false ~helpmode:false
3627 ~source ~trusted:true ~modehash)
3628 )) :: m_l
3630 method fitmodel name get set =
3631 m_l <-
3632 (name, `string get, 1, Action (
3633 fun _ ->
3634 let source =
3635 (object
3636 inherit lvsourcebase
3638 initializer
3639 m_active <- FMTE.to_int conf.fitmodel;
3640 m_first <- 0;
3642 method getitemcount = Array.length FMTE.names
3643 method getitem n = (FMTE.names.(n), 0)
3644 method exit ~uioh ~cancel ~active ~first ~pan =
3645 ignore (uioh, first, pan);
3646 if not cancel then set active;
3647 None
3648 method hasaction _ = true
3649 end)
3651 state.text <- E.s;
3652 let modehash = findkeyhash conf "info" in
3653 coe (new listview ~zebra:false ~helpmode:false
3654 ~source ~trusted:true ~modehash)
3655 )) :: m_l
3657 method caption s offset =
3658 m_l <- (s, `empty, offset, Noaction) :: m_l
3660 method caption2 s f offset =
3661 m_l <- (s, `string f, offset, Noaction) :: m_l
3663 method getitemcount = Array.length m_a
3665 method getitem n =
3666 let tostr = function
3667 | `int f -> string_of_int (f ())
3668 | `intws f -> string_with_suffix_of_int (f ())
3669 | `string f -> f ()
3670 | `color f -> color_to_string (f ())
3671 | `bool (btos, f) -> btos (f ())
3672 | `empty -> E.s
3674 let name, t, offset, _ = m_a.(n) in
3675 ((let s = tostr t in
3676 if nonemptystr s
3677 then Printf.sprintf "%s\t%s" name s
3678 else name),
3679 offset)
3681 method exit ~uioh ~cancel ~active ~first ~pan =
3682 let uiohopt =
3683 if not cancel
3684 then (
3685 let uioh =
3686 match m_a.(active) with
3687 | _, _, _, Action f -> f uioh
3688 | _, _, _, Noaction -> uioh
3690 Some uioh
3692 else None
3694 m_active <- active;
3695 m_first <- first;
3696 m_pan <- pan;
3697 uiohopt
3699 method hasaction n =
3700 match m_a.(n) with
3701 | _, _, _, Action _ -> true
3702 | _, _, _, Noaction -> false
3703 end)
3705 let rec fillsrc prevmode prevuioh =
3706 let sep () = src#caption E.s 0 in
3707 let colorp name get set =
3708 src#string name
3709 (fun () -> color_to_string (get ()))
3710 (fun v ->
3712 let c = color_of_string v in
3713 set c
3714 with exn ->
3715 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
3718 let oldmode = state.mode in
3719 let birdseye = isbirdseye state.mode in
3721 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3723 src#bool "presentation mode"
3724 (fun () -> conf.presentation)
3725 (fun v -> setpresentationmode v);
3727 src#bool "ignore case in searches"
3728 (fun () -> conf.icase)
3729 (fun v -> conf.icase <- v);
3731 src#bool "preload"
3732 (fun () -> conf.preload)
3733 (fun v -> conf.preload <- v);
3735 src#bool "highlight links"
3736 (fun () -> conf.hlinks)
3737 (fun v -> conf.hlinks <- v);
3739 src#bool "under info"
3740 (fun () -> conf.underinfo)
3741 (fun v -> conf.underinfo <- v);
3743 src#bool "persistent bookmarks"
3744 (fun () -> conf.savebmarks)
3745 (fun v -> conf.savebmarks <- v);
3747 src#fitmodel "fit model"
3748 (fun () -> FMTE.to_string conf.fitmodel)
3749 (fun v -> reqlayout conf.angle (FMTE.of_int v));
3751 src#bool "trim margins"
3752 (fun () -> conf.trimmargins)
3753 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3755 src#bool "persistent location"
3756 (fun () -> conf.jumpback)
3757 (fun v -> conf.jumpback <- v);
3759 sep ();
3760 src#int "inter-page space"
3761 (fun () -> conf.interpagespace)
3762 (fun n ->
3763 conf.interpagespace <- n;
3764 docolumns conf.columns;
3765 let pageno, py =
3766 match state.layout with
3767 | [] -> 0, 0
3768 | l :: _ ->
3769 l.pageno, l.pagey
3771 state.maxy <- calcheight ();
3772 let y = getpagey pageno in
3773 gotoy (y + py)
3776 src#int "page bias"
3777 (fun () -> conf.pagebias)
3778 (fun v -> conf.pagebias <- v);
3780 src#int "scroll step"
3781 (fun () -> conf.scrollstep)
3782 (fun n -> conf.scrollstep <- n);
3784 src#int "horizontal scroll step"
3785 (fun () -> conf.hscrollstep)
3786 (fun v -> conf.hscrollstep <- v);
3788 src#int "auto scroll step"
3789 (fun () ->
3790 match state.autoscroll with
3791 | Some step -> step
3792 | _ -> conf.autoscrollstep)
3793 (fun n ->
3794 let n = boundastep state.winh n in
3795 if state.autoscroll <> None
3796 then state.autoscroll <- Some n;
3797 conf.autoscrollstep <- n);
3799 src#int "zoom"
3800 (fun () -> truncate (conf.zoom *. 100.))
3801 (fun v -> setzoom ((float v) /. 100.));
3803 src#int "rotation"
3804 (fun () -> conf.angle)
3805 (fun v -> reqlayout v conf.fitmodel);
3807 src#int "scroll bar width"
3808 (fun () -> conf.scrollbw)
3809 (fun v ->
3810 conf.scrollbw <- v;
3811 reshape state.winw state.winh;
3814 src#int "scroll handle height"
3815 (fun () -> conf.scrollh)
3816 (fun v -> conf.scrollh <- v;);
3818 src#int "thumbnail width"
3819 (fun () -> conf.thumbw)
3820 (fun v ->
3821 conf.thumbw <- min 4096 v;
3822 match oldmode with
3823 | Birdseye beye ->
3824 leavebirdseye beye false;
3825 enterbirdseye ()
3826 | Textentry _
3827 | View
3828 | LinkNav _ -> ()
3831 let mode = state.mode in
3832 src#string "columns"
3833 (fun () ->
3834 match conf.columns with
3835 | Csingle _ -> "1"
3836 | Cmulti (multi, _) -> multicolumns_to_string multi
3837 | Csplit (count, _) -> "-" ^ string_of_int count
3839 (fun v ->
3840 let n, a, b = multicolumns_of_string v in
3841 setcolumns mode n a b);
3843 sep ();
3844 src#caption "Pixmap cache" 0;
3845 src#int_with_suffix "size (advisory)"
3846 (fun () -> conf.memlimit)
3847 (fun v -> conf.memlimit <- v);
3849 src#caption2 "used"
3850 (fun () -> Printf.sprintf "%s bytes, %d tiles"
3851 (string_with_suffix_of_int state.memused)
3852 (Hashtbl.length state.tilemap)) 1;
3854 sep ();
3855 src#caption "Layout" 0;
3856 src#caption2 "Dimension"
3857 (fun () ->
3858 Printf.sprintf "%dx%d (virtual %dx%d)"
3859 state.winw state.winh
3860 state.w state.maxy)
3862 if conf.debug
3863 then
3864 src#caption2 "Position" (fun () ->
3865 Printf.sprintf "%dx%d" state.x state.y
3867 else
3868 src#caption2 "Position" (fun () -> describe_location ()) 1
3871 sep ();
3872 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3873 "Save these parameters as global defaults at exit"
3874 (fun () -> conf.bedefault)
3875 (fun v -> conf.bedefault <- v)
3878 sep ();
3879 let btos b = if b then "@Ulguillemet" else "@Urguillemet" in
3880 src#bool ~offset:0 ~btos "Extended parameters"
3881 (fun () -> !showextended)
3882 (fun v -> showextended := v; fillsrc prevmode prevuioh);
3883 if !showextended
3884 then (
3885 src#bool "checkers"
3886 (fun () -> conf.checkers)
3887 (fun v -> conf.checkers <- v; setcheckers v);
3888 src#bool "update cursor"
3889 (fun () -> conf.updatecurs)
3890 (fun v -> conf.updatecurs <- v);
3891 src#bool "scroll-bar on the left"
3892 (fun () -> conf.leftscroll)
3893 (fun v -> conf.leftscroll <- v);
3894 src#bool "verbose"
3895 (fun () -> conf.verbose)
3896 (fun v -> conf.verbose <- v);
3897 src#bool "invert colors"
3898 (fun () -> conf.invert)
3899 (fun v -> conf.invert <- v);
3900 src#bool "max fit"
3901 (fun () -> conf.maxhfit)
3902 (fun v -> conf.maxhfit <- v);
3903 src#bool "redirect stderr"
3904 (fun () -> conf.redirectstderr)
3905 (fun v -> conf.redirectstderr <- v; redirectstderr ());
3906 src#bool "pax mode"
3907 (fun () -> conf.pax != None)
3908 (fun v ->
3909 if v
3910 then conf.pax <- Some (ref (now (), 0, 0))
3911 else conf.pax <- None);
3912 src#string "uri launcher"
3913 (fun () -> conf.urilauncher)
3914 (fun v -> conf.urilauncher <- v);
3915 src#string "path launcher"
3916 (fun () -> conf.pathlauncher)
3917 (fun v -> conf.pathlauncher <- v);
3918 src#string "tile size"
3919 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3920 (fun v ->
3922 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
3923 conf.tilew <- max 64 w;
3924 conf.tileh <- max 64 h;
3925 flushtiles ();
3926 with exn ->
3927 state.text <- Printf.sprintf "bad tile size `%s': %s"
3928 v (exntos exn)
3930 src#int "texture count"
3931 (fun () -> conf.texcount)
3932 (fun v ->
3933 if realloctexts v
3934 then conf.texcount <- v
3935 else showtext '!' " Failed to set texture count please retry later"
3937 src#int "slice height"
3938 (fun () -> conf.sliceheight)
3939 (fun v ->
3940 conf.sliceheight <- v;
3941 wcmd "sliceh %d" conf.sliceheight;
3943 src#int "anti-aliasing level"
3944 (fun () -> conf.aalevel)
3945 (fun v ->
3946 conf.aalevel <- bound v 0 8;
3947 state.anchor <- getanchor ();
3948 opendoc state.path state.password;
3950 src#string "page scroll scaling factor"
3951 (fun () -> string_of_float conf.pgscale)
3952 (fun v ->
3954 let s = float_of_string v in
3955 conf.pgscale <- s
3956 with exn ->
3957 state.text <- Printf.sprintf
3958 "bad page scroll scaling factor `%s': %s" v (exntos exn)
3961 src#int "ui font size"
3962 (fun () -> fstate.fontsize)
3963 (fun v -> setfontsize (bound v 5 100));
3964 src#int "hint font size"
3965 (fun () -> conf.hfsize)
3966 (fun v -> conf.hfsize <- bound v 5 100);
3967 colorp "background color"
3968 (fun () -> conf.bgcolor)
3969 (fun v -> conf.bgcolor <- v);
3970 src#bool "crop hack"
3971 (fun () -> conf.crophack)
3972 (fun v -> conf.crophack <- v);
3973 src#string "trim fuzz"
3974 (fun () -> irect_to_string conf.trimfuzz)
3975 (fun v ->
3977 conf.trimfuzz <- irect_of_string v;
3978 if conf.trimmargins
3979 then settrim true conf.trimfuzz;
3980 with exn ->
3981 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
3983 src#string "throttle"
3984 (fun () ->
3985 match conf.maxwait with
3986 | None -> "show place holder if page is not ready"
3987 | Some time ->
3988 if time = infinity
3989 then "wait for page to fully render"
3990 else
3991 "wait " ^ string_of_float time
3992 ^ " seconds before showing placeholder"
3994 (fun v ->
3996 let f = float_of_string v in
3997 if f <= 0.0
3998 then conf.maxwait <- None
3999 else conf.maxwait <- Some f
4000 with exn ->
4001 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
4003 src#string "ghyll scroll"
4004 (fun () ->
4005 match conf.ghyllscroll with
4006 | None -> E.s
4007 | Some nab -> ghyllscroll_to_string nab
4009 (fun v ->
4010 try conf.ghyllscroll <- ghyllscroll_of_string v
4011 with exn ->
4012 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
4014 src#string "selection command"
4015 (fun () -> conf.selcmd)
4016 (fun v -> conf.selcmd <- v);
4017 src#string "synctex command"
4018 (fun () -> conf.stcmd)
4019 (fun v -> conf.stcmd <- v);
4020 src#string "pax command"
4021 (fun () -> conf.paxcmd)
4022 (fun v -> conf.paxcmd <- v);
4023 src#string "ask password command"
4024 (fun () -> conf.passcmd)
4025 (fun v -> conf.passcmd <- v);
4026 src#string "save path command"
4027 (fun () -> conf.savecmd)
4028 (fun v -> conf.savecmd <- v);
4029 src#colorspace "color space"
4030 (fun () -> CSTE.to_string conf.colorspace)
4031 (fun v ->
4032 conf.colorspace <- CSTE.of_int v;
4033 wcmd "cs %d" v;
4034 load state.layout;
4036 src#paxmark "pax mark method"
4037 (fun () -> MTE.to_string conf.paxmark)
4038 (fun v -> conf.paxmark <- MTE.of_int v);
4039 if pbousable ()
4040 then
4041 src#bool "use PBO"
4042 (fun () -> conf.usepbo)
4043 (fun v -> conf.usepbo <- v);
4044 src#bool "mouse wheel scrolls pages"
4045 (fun () -> conf.wheelbypage)
4046 (fun v -> conf.wheelbypage <- v);
4047 src#bool "open remote links in a new instance"
4048 (fun () -> conf.riani)
4049 (fun v -> conf.riani <- v);
4050 src#bool "edit annotations inline"
4051 (fun () -> conf.annotinline)
4052 (fun v -> conf.annotinline <- v);
4055 sep ();
4056 src#caption "Document" 0;
4057 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4058 src#caption2 "Pages"
4059 (fun () -> string_of_int state.pagecount) 1;
4060 src#caption2 "Dimensions"
4061 (fun () -> string_of_int (List.length state.pdims)) 1;
4062 if conf.trimmargins
4063 then (
4064 sep ();
4065 src#caption "Trimmed margins" 0;
4066 src#caption2 "Dimensions"
4067 (fun () -> string_of_int (List.length state.pdims)) 1;
4070 sep ();
4071 src#caption "OpenGL" 0;
4072 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4073 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4075 sep ();
4076 src#caption "Location" 0;
4077 if nonemptystr state.origin
4078 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
4079 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
4081 src#reset prevmode prevuioh;
4083 fun () ->
4084 state.text <- E.s;
4085 resetmstate ();
4086 let prevmode = state.mode
4087 and prevuioh = state.uioh in
4088 fillsrc prevmode prevuioh;
4089 let source = (src :> lvsource) in
4090 let modehash = findkeyhash conf "info" in
4091 state.uioh <- coe (object (self)
4092 inherit listview ~zebra:false ~helpmode:false
4093 ~source ~trusted:true ~modehash as super
4094 val mutable m_prevmemused = 0
4095 method! infochanged = function
4096 | Memused ->
4097 if m_prevmemused != state.memused
4098 then (
4099 m_prevmemused <- state.memused;
4100 G.postRedisplay "memusedchanged";
4102 | Pdim -> G.postRedisplay "pdimchanged"
4103 | Docinfo -> fillsrc prevmode prevuioh
4105 method! key key mask =
4106 if not (Wsi.withctrl mask)
4107 then
4108 match key with
4109 | @left | @kpleft -> coe (self#updownlevel ~-1)
4110 | @right | @kpright -> coe (self#updownlevel 1)
4111 | _ -> super#key key mask
4112 else super#key key mask
4113 end);
4114 G.postRedisplay "info";
4117 let enterhelpmode =
4118 let source =
4119 (object
4120 inherit lvsourcebase
4121 method getitemcount = Array.length state.help
4122 method getitem n =
4123 let s, l, _ = state.help.(n) in
4124 (s, l)
4126 method exit ~uioh ~cancel ~active ~first ~pan =
4127 let optuioh =
4128 if not cancel
4129 then (
4130 match state.help.(active) with
4131 | _, _, Action f -> Some (f uioh)
4132 | _, _, Noaction -> Some uioh
4134 else None
4136 m_active <- active;
4137 m_first <- first;
4138 m_pan <- pan;
4139 optuioh
4141 method hasaction n =
4142 match state.help.(n) with
4143 | _, _, Action _ -> true
4144 | _, _, Noaction -> false
4146 initializer
4147 m_active <- -1
4148 end)
4149 in fun () ->
4150 let modehash = findkeyhash conf "help" in
4151 resetmstate ();
4152 state.uioh <- coe (new listview
4153 ~zebra:false ~helpmode:true
4154 ~source ~trusted:true ~modehash);
4155 G.postRedisplay "help";
4158 let entermsgsmode =
4159 let msgsource =
4160 (object
4161 inherit lvsourcebase
4162 val mutable m_items = E.a
4164 method getitemcount = 1 + Array.length m_items
4166 method getitem n =
4167 if n = 0
4168 then "[Clear]", 0
4169 else m_items.(n-1), 0
4171 method exit ~uioh ~cancel ~active ~first ~pan =
4172 ignore uioh;
4173 if not cancel
4174 then (
4175 if active = 0
4176 then Buffer.clear state.errmsgs;
4178 m_active <- active;
4179 m_first <- first;
4180 m_pan <- pan;
4181 None
4183 method hasaction n =
4184 n = 0
4186 method reset =
4187 state.newerrmsgs <- false;
4188 let l = Str.split newlinere (Buffer.contents state.errmsgs) in
4189 m_items <- Array.of_list l
4191 initializer
4192 m_active <- 0
4193 end)
4194 in fun () ->
4195 state.text <- E.s;
4196 resetmstate ();
4197 msgsource#reset;
4198 let source = (msgsource :> lvsource) in
4199 let modehash = findkeyhash conf "listview" in
4200 state.uioh <- coe (object
4201 inherit listview ~zebra:false ~helpmode:false
4202 ~source ~trusted:false ~modehash as super
4203 method! display =
4204 if state.newerrmsgs
4205 then msgsource#reset;
4206 super#display
4207 end);
4208 G.postRedisplay "msgs";
4211 let getusertext s =
4212 let editor = getenvwithdef "EDITOR" E.s in
4213 if emptystr editor
4214 then E.s
4215 else
4216 let tmppath = Filename.temp_file "llpp" "note" in
4217 if not (emptystr s)
4218 then (
4219 let oc = open_out tmppath in
4220 output_string oc s;
4221 close_out oc;
4223 let execstr = editor ^ " " ^ tmppath in
4224 let s =
4225 match popen execstr [] with
4226 | (exception exn) ->
4227 showtext '!' @@
4228 Printf.sprintf "popen(%S) failed: %s" execstr (exntos exn);
4230 | pid ->
4231 match Unix.waitpid [] pid
4232 with
4233 | (exception exn) ->
4234 showtext '!' @@
4235 Printf.sprintf "waitpid(%d) failed: %s" pid (exntos exn);
4237 | (_pid, status) ->
4238 match status with
4239 | Unix.WEXITED 0 -> filelines tmppath
4240 | Unix.WEXITED n ->
4241 showtext '!' @@
4242 Printf.sprintf "editor process(%s) exited abnormally: %d"
4243 execstr n;
4245 | Unix.WSIGNALED n ->
4246 showtext '!' @@
4247 Printf.sprintf "editor process(%s) was killed by signal %d"
4248 execstr n;
4250 | Unix.WSTOPPED n ->
4251 showtext '!' @@
4252 Printf.sprintf "editor(%s) process was stopped by signal %d"
4253 execstr n;
4256 match Unix.unlink tmppath with
4257 | (exception exn) ->
4258 showtext '!' @@ Printf.sprintf "failed to ulink %S: %s"
4259 tmppath (exntos exn);
4261 | () -> s
4264 let enterannotmode opaque slinkindex =
4265 let msgsource =
4266 (object
4267 inherit lvsourcebase
4268 val mutable m_text = E.s
4269 val mutable m_items = E.a
4271 method getitemcount = Array.length m_items
4273 method getitem n =
4274 let label, _func = m_items.(n) in
4275 label, 0
4277 method exit ~uioh ~cancel ~active ~first ~pan =
4278 ignore (uioh, first, pan);
4279 if not cancel
4280 then (
4281 let _label, func = m_items.(active) in
4282 func ()
4284 None
4286 method hasaction n = not @@ emptystr @@ fst m_items.(n)
4288 method reset s =
4289 let rec split accu b i =
4290 let p = b+i in
4291 if p = String.length s
4292 then (String.sub s b (p-b), unit) :: accu
4293 else
4294 if (i > 70 && s.[p] = ' ') || s.[p] = '\r' || s.[p] = '\n'
4295 then
4296 let ss = if i = 0 then E.s else String.sub s b i in
4297 split ((ss, unit)::accu) (p+1) 0
4298 else
4299 split accu b (i+1)
4301 let cleanup () =
4302 wcmd "freepage %s" (~> opaque);
4303 let keys =
4304 Hashtbl.fold (fun key opaque' accu ->
4305 if opaque' = opaque'
4306 then key :: accu else accu) state.pagemap []
4308 List.iter (Hashtbl.remove state.pagemap) keys;
4309 flushtiles ();
4310 gotoy state.y
4312 let dele () =
4313 delannot opaque slinkindex;
4314 cleanup ();
4316 let edit inline () =
4317 let update s =
4318 if emptystr s
4319 then dele ()
4320 else (
4321 modannot opaque slinkindex s;
4322 cleanup ();
4325 if inline
4326 then
4327 let mode = state.mode in
4328 state.mode <-
4329 Textentry (
4330 ("annotation: ", m_text, None, textentry, update, true),
4331 fun _ -> state.mode <- mode);
4332 state.text <- E.s;
4333 enttext ();
4334 else
4335 let s = getusertext m_text in
4336 update s
4338 m_text <- s;
4339 m_items <-
4340 ( "[Copy]", fun () -> selstring m_text)
4341 :: ("[Delete]", dele)
4342 :: ("[Edit]", edit conf.annotinline)
4343 :: (E.s, unit)
4344 :: split [] 0 0 |> List.rev |> Array.of_list
4346 initializer
4347 m_active <- 0
4348 end)
4350 state.text <- E.s;
4351 let s = getannotcontents opaque slinkindex in
4352 resetmstate ();
4353 msgsource#reset s;
4354 let source = (msgsource :> lvsource) in
4355 let modehash = findkeyhash conf "listview" in
4356 state.uioh <- coe (object
4357 inherit listview ~zebra:false ~helpmode:false
4358 ~source ~trusted:false ~modehash
4359 end);
4360 G.postRedisplay "enterannotmode";
4363 let gotounder under =
4364 let getpath filename =
4365 let path =
4366 if nonemptystr filename
4367 then
4368 if Filename.is_relative filename
4369 then
4370 let dir = Filename.dirname state.path in
4371 let dir =
4372 if Filename.is_implicit dir
4373 then Filename.concat (Sys.getcwd ()) dir
4374 else dir
4376 Filename.concat dir filename
4377 else filename
4378 else E.s
4380 if Sys.file_exists path
4381 then path
4382 else E.s
4384 match under with
4385 | Ulinkgoto (pageno, top) ->
4386 if pageno >= 0
4387 then (
4388 addnav ();
4389 gotopage1 pageno top;
4392 | Ulinkuri s ->
4393 addpid @@ gotouri s
4395 | Uremote (filename, pageno) ->
4396 let path = getpath filename in
4397 if nonemptystr path
4398 then (
4399 if conf.riani
4400 then
4401 let command = Printf.sprintf "%s -page %d %S" !selfexec pageno path in
4402 try addpid @@ popen command []
4403 with exn ->
4404 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
4405 flush stderr;
4406 else
4407 let anchor = getanchor () in
4408 let ranchor = state.path, state.password, anchor, state.origin in
4409 state.origin <- E.s;
4410 state.anchor <- (pageno, 0.0, 0.0);
4411 state.ranchors <- ranchor :: state.ranchors;
4412 opendoc path E.s;
4414 else showtext '!' ("Could not find " ^ filename)
4416 | Uremotedest (filename, destname) ->
4417 let path = getpath filename in
4418 if nonemptystr path
4419 then (
4420 if conf.riani
4421 then
4422 let command = !selfexec ^ " " ^ path ^ " -dest " ^ destname in
4423 try addpid @@ popen command []
4424 with exn ->
4425 Printf.eprintf
4426 "failed to execute `%s': %s\n" command (exntos exn);
4427 flush stderr;
4428 else
4429 let anchor = getanchor () in
4430 let ranchor = state.path, state.password, anchor, state.origin in
4431 state.origin <- E.s;
4432 state.nameddest <- destname;
4433 state.ranchors <- ranchor :: state.ranchors;
4434 opendoc path E.s;
4436 else showtext '!' ("Could not find " ^ filename)
4438 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4439 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
4442 let gotooutline (_, _, kind) =
4443 match kind with
4444 | Onone -> ()
4445 | Oanchor anchor ->
4446 let (pageno, y, _) = anchor in
4447 let y = getanchory
4448 (if conf.presentation then (pageno, y, 1.0) else anchor)
4450 addnav ();
4451 gotoghyll y
4452 | Ouri uri -> gotounder (Ulinkuri uri)
4453 | Olaunch cmd -> gotounder (Ulaunch cmd)
4454 | Oremote remote -> gotounder (Uremote remote)
4455 | Ohistory hist -> gotohist hist
4456 | Oremotedest remotedest -> gotounder (Uremotedest remotedest)
4457 | Oaction f -> f ()
4460 let outlinesource sourcetype =
4461 (object (self)
4462 inherit lvsourcebase
4463 val mutable m_items = E.a
4464 val mutable m_minfo = E.a
4465 val mutable m_orig_items = E.a
4466 val mutable m_orig_minfo = E.a
4467 val mutable m_narrow_patterns = []
4468 val mutable m_hadremovals = false
4469 val mutable m_gen = -1
4471 method getitemcount =
4472 Array.length m_items + (if m_hadremovals then 1 else 0)
4474 method getitem n =
4475 if n == Array.length m_items && m_hadremovals
4476 then
4477 ("[Confirm removal]", 0)
4478 else
4479 let s, n, _ = m_items.(n) in
4480 (s, n)
4482 method exit ~uioh ~cancel ~active ~first ~pan =
4483 ignore (uioh, first);
4484 let confrimremoval = m_hadremovals && active = Array.length m_items in
4485 let items, minfo =
4486 if m_narrow_patterns = []
4487 then m_orig_items, m_orig_minfo
4488 else m_items, m_minfo
4490 if not cancel
4491 then (
4492 if not confrimremoval
4493 then (
4494 gotooutline m_items.(active);
4495 m_items <- items;
4496 m_minfo <- minfo;
4498 else (
4499 state.bookmarks <- Array.to_list m_items;
4500 m_orig_items <- m_items;
4501 m_orig_minfo <- m_minfo;
4504 else (
4505 m_items <- items;
4506 m_minfo <- minfo;
4508 m_pan <- pan;
4509 None
4511 method hasaction _ = true
4513 method greetmsg =
4514 if Array.length m_items != Array.length m_orig_items
4515 then
4516 let s =
4517 match m_narrow_patterns with
4518 | one :: [] -> one
4519 | many -> String.concat "@Uellipsis" (List.rev many)
4521 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
4522 else E.s
4524 method statestr =
4525 match m_narrow_patterns with
4526 | [] -> E.s
4527 | one :: [] -> one
4528 | head :: _ -> "@Uellipsis" ^ head
4530 method narrow pattern =
4531 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
4532 match reopt with
4533 | None -> ()
4534 | Some re ->
4535 let rec loop accu minfo n =
4536 if n = -1
4537 then (
4538 m_items <- Array.of_list accu;
4539 m_minfo <- Array.of_list minfo;
4541 else
4542 let (s, _, t) as o = m_items.(n) in
4543 let accu, minfo =
4544 match t with
4545 | Oaction _ -> o :: accu, (0, 0) :: minfo
4546 | Onone | Oanchor _ | Ouri _ | Olaunch _
4547 | Oremote _ | Oremotedest _ | Ohistory _ ->
4548 let first =
4549 try Str.search_forward re s 0
4550 with Not_found -> -1
4552 if first >= 0
4553 then o :: accu, (first, Str.match_end ()) :: minfo
4554 else accu, minfo
4556 loop accu minfo (n-1)
4558 loop [] [] (Array.length m_items - 1)
4560 method! getminfo = m_minfo
4562 method denarrow =
4563 m_orig_items <- (
4564 match sourcetype with
4565 | `bookmarks -> Array.of_list state.bookmarks
4566 | `outlines -> state.outlines
4567 | `history -> genhistoutlines !Config.historder
4569 m_minfo <- m_orig_minfo;
4570 m_items <- m_orig_items
4572 method remove m =
4573 if sourcetype = `bookmarks
4574 then
4575 if m >= 0 && m < Array.length m_items
4576 then (
4577 m_hadremovals <- true;
4578 m_items <- Array.init (Array.length m_items - 1) (fun n ->
4579 let n = if n >= m then n+1 else n in
4580 m_items.(n)
4584 method add_narrow_pattern pattern =
4585 m_narrow_patterns <- pattern :: m_narrow_patterns
4587 method del_narrow_pattern =
4588 match m_narrow_patterns with
4589 | _ :: rest -> m_narrow_patterns <- rest
4590 | [] -> ()
4592 method renarrow =
4593 self#denarrow;
4594 match m_narrow_patterns with
4595 | pattern :: [] -> self#narrow pattern; pattern
4596 | list ->
4597 List.fold_left (fun accu pattern ->
4598 self#narrow pattern;
4599 pattern ^ "@Uellipsis" ^ accu) E.s list
4601 method calcactive anchor =
4602 let rely = getanchory anchor in
4603 let rec loop n best bestd =
4604 if n = Array.length m_items
4605 then best
4606 else
4607 let _, _, kind = m_items.(n) in
4608 match kind with
4609 | Oanchor anchor ->
4610 let orely = getanchory anchor in
4611 let d = abs (orely - rely) in
4612 if d < bestd
4613 then loop (n+1) n d
4614 else loop (n+1) best bestd
4615 | Onone | Oremote _ | Olaunch _
4616 | Oremotedest _ | Ouri _ | Ohistory _ | Oaction _ ->
4617 loop (n+1) best bestd
4619 loop 0 ~-1 max_int
4621 method reset anchor items =
4622 m_hadremovals <- false;
4623 if state.gen != m_gen
4624 then (
4625 m_orig_items <- items;
4626 m_items <- items;
4627 m_narrow_patterns <- [];
4628 m_minfo <- E.a;
4629 m_orig_minfo <- E.a;
4630 m_gen <- state.gen;
4632 else (
4633 if items != m_orig_items
4634 then (
4635 m_orig_items <- items;
4636 if m_narrow_patterns == []
4637 then m_items <- items;
4640 let active = self#calcactive anchor in
4641 m_active <- active;
4642 m_first <- firstof m_first active
4643 end)
4646 let enterselector sourcetype =
4647 resetmstate ();
4648 let source = outlinesource sourcetype in
4649 fun errmsg ->
4650 let outlines =
4651 match sourcetype with
4652 | `bookmarks -> Array.of_list state.bookmarks
4653 | `outlines -> state.outlines
4654 | `history -> genhistoutlines !Config.historder
4656 if Array.length outlines = 0
4657 then (
4658 showtext ' ' errmsg;
4660 else (
4661 state.text <- source#greetmsg;
4662 Wsi.setcursor Wsi.CURSOR_INHERIT;
4663 let anchor = getanchor () in
4664 source#reset anchor outlines;
4665 state.uioh <-
4666 coe (new outlinelistview ~zebra:(sourcetype=`history) ~source);
4667 G.postRedisplay "enter selector";
4671 let enteroutlinemode =
4672 let f = enterselector `outlines in
4673 fun () -> f "Document has no outline";
4676 let enterbookmarkmode =
4677 let f = enterselector `bookmarks in
4678 fun () -> f "Document has no bookmarks (yet)";
4681 let enterhistmode () = enterselector `history "No history (yet)";;
4683 let quickbookmark ?title () =
4684 match state.layout with
4685 | [] -> ()
4686 | l :: _ ->
4687 let title =
4688 match title with
4689 | None ->
4690 let tm = Unix.localtime (now ()) in
4691 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4692 (l.pageno+1)
4693 tm.Unix.tm_mday
4694 tm.Unix.tm_mon
4695 (tm.Unix.tm_year + 1900)
4696 tm.Unix.tm_hour
4697 tm.Unix.tm_min
4698 | Some title -> title
4700 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4703 let setautoscrollspeed step goingdown =
4704 let incr = max 1 ((abs step) / 2) in
4705 let incr = if goingdown then incr else -incr in
4706 let astep = boundastep state.winh (step + incr) in
4707 state.autoscroll <- Some astep;
4710 let canpan () =
4711 match conf.columns with
4712 | Csplit _ -> true
4713 | Csingle _ | Cmulti _ -> state.x != 0 || conf.zoom > 1.0
4716 let panbound x = bound x (-state.w) (wadjsb () + state.winw);;
4718 let existsinrow pageno (columns, coverA, coverB) p =
4719 let last = ((pageno - coverA) mod columns) + columns in
4720 let rec any = function
4721 | [] -> false
4722 | l :: rest ->
4723 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4724 then p l
4725 else (
4726 if not (p l)
4727 then (if l.pageno = last then false else any rest)
4728 else true
4731 any state.layout
4734 let nextpage () =
4735 match state.layout with
4736 | [] ->
4737 let pageno = page_of_y state.y in
4738 gotoghyll (getpagey (pageno+1))
4739 | l :: rest ->
4740 match conf.columns with
4741 | Csingle _ ->
4742 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4743 then
4744 let y = clamp (pgscale state.winh) in
4745 gotoghyll y
4746 else
4747 let pageno = min (l.pageno+1) (state.pagecount-1) in
4748 gotoghyll (getpagey pageno)
4749 | Cmulti ((c, _, _) as cl, _) ->
4750 if conf.presentation
4751 && (existsinrow l.pageno cl
4752 (fun l -> l.pageh > l.pagey + l.pagevh))
4753 then
4754 let y = clamp (pgscale state.winh) in
4755 gotoghyll y
4756 else
4757 let pageno = min (l.pageno+c) (state.pagecount-1) in
4758 gotoghyll (getpagey pageno)
4759 | Csplit (n, _) ->
4760 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4761 then
4762 let pagey, pageh = getpageyh l.pageno in
4763 let pagey = pagey + pageh * l.pagecol in
4764 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4765 gotoghyll (pagey + pageh + ips)
4768 let prevpage () =
4769 match state.layout with
4770 | [] ->
4771 let pageno = page_of_y state.y in
4772 gotoghyll (getpagey (pageno-1))
4773 | l :: _ ->
4774 match conf.columns with
4775 | Csingle _ ->
4776 if conf.presentation && l.pagey != 0
4777 then
4778 gotoghyll (clamp (pgscale ~-(state.winh)))
4779 else
4780 let pageno = max 0 (l.pageno-1) in
4781 gotoghyll (getpagey pageno)
4782 | Cmulti ((c, _, coverB) as cl, _) ->
4783 if conf.presentation &&
4784 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4785 then
4786 gotoghyll (clamp (pgscale ~-(state.winh)))
4787 else
4788 let decr =
4789 if l.pageno = state.pagecount - coverB
4790 then 1
4791 else c
4793 let pageno = max 0 (l.pageno-decr) in
4794 gotoghyll (getpagey pageno)
4795 | Csplit (n, _) ->
4796 let y =
4797 if l.pagecol = 0
4798 then
4799 if l.pageno = 0
4800 then l.pagey
4801 else
4802 let pageno = max 0 (l.pageno-1) in
4803 let pagey, pageh = getpageyh pageno in
4804 pagey + (n-1)*pageh
4805 else
4806 let pagey, pageh = getpageyh l.pageno in
4807 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4809 gotoghyll y
4812 let save () =
4813 if emptystr conf.savecmd
4814 then error "don't know where to save modified document"
4815 else
4816 let savecmd = Str.global_replace percentsre state.path conf.savecmd in
4817 let path =
4818 getcmdoutput
4819 (fun s -> error "failed to obtain path to the saved copy: %s" s)
4820 savecmd
4822 if not (emptystr path)
4823 then
4824 let tmp = path ^ ".tmp" in
4825 savedoc tmp;
4826 Unix.rename tmp path;
4829 let viewkeyboard key mask =
4830 let enttext te =
4831 let mode = state.mode in
4832 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4833 state.text <- E.s;
4834 enttext ();
4835 G.postRedisplay "view:enttext"
4837 let ctrl = Wsi.withctrl mask in
4838 let key =
4839 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
4841 match key with
4842 | @Q -> exit 0
4844 | @W ->
4845 if hasunsavedchanges ()
4846 then save ()
4848 | @insert ->
4849 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4850 then (
4851 state.mode <- LinkNav (Ltgendir 0);
4852 gotoy state.y;
4854 else showtext '!' "Keyboard link navigation does not work under rotation"
4856 | @escape | @q ->
4857 begin match state.mstate with
4858 | Mzoomrect _ ->
4859 resetmstate ();
4860 G.postRedisplay "kill rect";
4861 | Msel _
4862 | Mpan _
4863 | Mscrolly | Mscrollx
4864 | Mzoom _
4865 | Mnone ->
4866 begin match state.mode with
4867 | LinkNav _ ->
4868 state.mode <- View;
4869 G.postRedisplay "esc leave linknav"
4870 | Birdseye _
4871 | Textentry _
4872 | View ->
4873 match state.ranchors with
4874 | [] -> raise Quit
4875 | (path, password, anchor, origin) :: rest ->
4876 state.ranchors <- rest;
4877 state.anchor <- anchor;
4878 state.origin <- origin;
4879 state.nameddest <- E.s;
4880 opendoc path password
4881 end;
4882 end;
4884 | @backspace ->
4885 gotoghyll (getnav ~-1)
4887 | @o ->
4888 enteroutlinemode ()
4890 | @H ->
4891 enterhistmode ()
4893 | @u ->
4894 state.rects <- [];
4895 state.text <- E.s;
4896 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap;
4897 G.postRedisplay "dehighlight";
4899 | @slash | @question ->
4900 let ondone isforw s =
4901 cbput state.hists.pat s;
4902 state.searchpattern <- s;
4903 search s isforw
4905 let s = String.make 1 (Char.chr key) in
4906 enttext (s, E.s, Some (onhist state.hists.pat),
4907 textentry, ondone (key = @slash), true)
4909 | @plus | @kpplus | @equals when ctrl ->
4910 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4911 setzoom (conf.zoom +. incr)
4913 | @plus | @kpplus ->
4914 let ondone s =
4915 let n =
4916 try int_of_string s with exc ->
4917 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
4918 max_int
4920 if n != max_int
4921 then (
4922 conf.pagebias <- n;
4923 state.text <- "page bias is now " ^ string_of_int n;
4926 enttext ("page bias: ", E.s, None, intentry, ondone, true)
4928 | @minus | @kpminus when ctrl ->
4929 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4930 setzoom (max 0.01 (conf.zoom -. decr))
4932 | @minus | @kpminus ->
4933 let ondone msg = state.text <- msg in
4934 enttext (
4935 "option [acfhilpstvxACFPRSZTISM]: ", E.s, None,
4936 optentry state.mode, ondone, true
4939 | @0 when ctrl ->
4940 if conf.zoom = 1.0
4941 then (
4942 state.x <- 0;
4943 gotoy state.y
4945 else setzoom 1.0
4947 | (@1 | @2) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
4948 let cols =
4949 match conf.columns with
4950 | Csingle _ | Cmulti _ -> 1
4951 | Csplit (n, _) -> n
4953 let h = state.winh -
4954 conf.interpagespace lsl (if conf.presentation then 1 else 0)
4956 let zoom = zoomforh state.winw h (vscrollw ()) cols in
4957 if zoom > 0.0 && (key = 50 || zoom < 1.0)
4958 then setzoom zoom
4960 | @3 when ctrl ->
4961 let fm =
4962 match conf.fitmodel with
4963 | FitWidth -> FitProportional
4964 | FitProportional -> FitPage
4965 | FitPage -> FitWidth
4967 state.text <- "fit model: " ^ FMTE.to_string fm;
4968 reqlayout conf.angle fm
4970 | @F9 ->
4971 togglebirdseye ()
4973 | @9 when ctrl ->
4974 togglebirdseye ()
4976 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4977 when not ctrl -> (* 0..9 *)
4978 let ondone s =
4979 let n =
4980 try int_of_string s with exc ->
4981 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
4984 if n >= 0
4985 then (
4986 addnav ();
4987 cbput state.hists.pag (string_of_int n);
4988 gotopage1 (n + conf.pagebias - 1) 0;
4991 let pageentry text key =
4992 match Char.unsafe_chr key with
4993 | 'g' -> TEdone text
4994 | _ -> intentry text key
4996 let text = String.make 1 (Char.chr key) in
4997 enttext (":", text, Some (onhist state.hists.pag),
4998 pageentry, ondone, true)
5000 | @b ->
5001 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
5002 reshape state.winw state.winh;
5004 | @B ->
5005 state.bzoom <- not state.bzoom;
5006 state.rects <- [];
5007 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
5009 | @l ->
5010 conf.hlinks <- not conf.hlinks;
5011 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5012 G.postRedisplay "toggle highlightlinks";
5014 | @F ->
5015 state.glinks <- true;
5016 let mode = state.mode in
5017 state.mode <- Textentry (
5018 (":", E.s, None, linknentry, linkndone gotounder, false),
5019 (fun _ ->
5020 state.glinks <- false;
5021 state.mode <- mode)
5023 state.text <- E.s;
5024 G.postRedisplay "view:linkent(F)"
5026 | @y ->
5027 state.glinks <- true;
5028 let mode = state.mode in
5029 state.mode <- Textentry (
5031 ":", E.s, None, linknentry, linkndone (fun under ->
5032 selstring (undertext under);
5033 ), false
5035 fun _ ->
5036 state.glinks <- false;
5037 state.mode <- mode
5039 state.text <- E.s;
5040 G.postRedisplay "view:linkent"
5042 | @a ->
5043 begin match state.autoscroll with
5044 | Some step ->
5045 conf.autoscrollstep <- step;
5046 state.autoscroll <- None
5047 | None ->
5048 if conf.autoscrollstep = 0
5049 then state.autoscroll <- Some 1
5050 else state.autoscroll <- Some conf.autoscrollstep
5053 | @p when ctrl ->
5054 launchpath ()
5056 | @P ->
5057 setpresentationmode (not conf.presentation);
5058 showtext ' ' ("presentation mode " ^
5059 if conf.presentation then "on" else "off");
5061 | @f ->
5062 if List.mem Wsi.Fullscreen state.winstate
5063 then Wsi.reshape conf.cwinw conf.cwinh
5064 else Wsi.fullscreen ()
5066 | @p | @N ->
5067 search state.searchpattern false
5069 | @n | @F3 ->
5070 search state.searchpattern true
5072 | @t ->
5073 begin match state.layout with
5074 | [] -> ()
5075 | l :: _ ->
5076 gotoghyll (getpagey l.pageno)
5079 | @space ->
5080 nextpage ()
5082 | @delete | @kpdelete -> (* delete *)
5083 prevpage ()
5085 | @equals ->
5086 showtext ' ' (describe_location ());
5088 | @w ->
5089 begin match state.layout with
5090 | [] -> ()
5091 | l :: _ ->
5092 Wsi.reshape (l.pagew + vscrollw ()) l.pageh;
5093 G.postRedisplay "w"
5096 | @apos ->
5097 enterbookmarkmode ()
5099 | @h | @F1 ->
5100 enterhelpmode ()
5102 | @i ->
5103 enterinfomode ()
5105 | @e when Buffer.length state.errmsgs > 0 ->
5106 entermsgsmode ()
5108 | @m ->
5109 let ondone s =
5110 match state.layout with
5111 | l :: _ ->
5112 if nonemptystr s
5113 then
5114 state.bookmarks <-
5115 (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
5116 | _ -> ()
5118 enttext ("bookmark: ", E.s, None, textentry, ondone, true)
5120 | @tilde ->
5121 quickbookmark ();
5122 showtext ' ' "Quick bookmark added";
5124 | @z ->
5125 begin match state.layout with
5126 | l :: _ ->
5127 let rect = getpdimrect l.pagedimno in
5128 let w, h =
5129 if conf.crophack
5130 then
5131 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5132 truncate (1.2 *. (rect.(3) -. rect.(0))))
5133 else
5134 (truncate (rect.(1) -. rect.(0)),
5135 truncate (rect.(3) -. rect.(0)))
5137 let w = truncate ((float w)*.conf.zoom)
5138 and h = truncate ((float h)*.conf.zoom) in
5139 if w != 0 && h != 0
5140 then (
5141 state.anchor <- getanchor ();
5142 Wsi.reshape (w + vscrollw ()) (h + conf.interpagespace)
5144 G.postRedisplay "z";
5146 | [] -> ()
5149 | @x -> state.roam ()
5151 | @Lt | @Gt ->
5152 reqlayout (conf.angle +
5153 (if key = @Gt then 30 else -30)) conf.fitmodel
5155 | @Lb | @Rb ->
5156 conf.colorscale <-
5157 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5159 G.postRedisplay "brightness";
5161 | @c when state.mode = View ->
5162 if Wsi.withalt mask
5163 then (
5164 if conf.zoom > 1.0
5165 then
5166 let m = (wadjsb () + state.winw - state.w) / 2 in
5167 state.x <- m;
5168 gotoy_and_clear_text state.y
5170 else
5171 let (c, a, b), z =
5172 match state.prevcolumns with
5173 | None -> (1, 0, 0), 1.0
5174 | Some (columns, z) ->
5175 let cab =
5176 match columns with
5177 | Csplit (c, _) -> -c, 0, 0
5178 | Cmulti ((c, a, b), _) -> c, a, b
5179 | Csingle _ -> 1, 0, 0
5181 cab, z
5183 setcolumns View c a b;
5184 setzoom z
5186 | @down | @up when ctrl && Wsi.withshift mask ->
5187 let zoom, x = state.prevzoom in
5188 setzoom zoom;
5189 state.x <- x;
5191 | @k | @up | @kpup ->
5192 begin match state.autoscroll with
5193 | None ->
5194 begin match state.mode with
5195 | Birdseye beye -> upbirdseye 1 beye
5196 | Textentry _
5197 | View
5198 | LinkNav _ ->
5199 if ctrl
5200 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5201 else (
5202 if not (Wsi.withshift mask) && conf.presentation
5203 then prevpage ()
5204 else gotoghyll1 true (clamp (-conf.scrollstep))
5207 | Some n ->
5208 setautoscrollspeed n false
5211 | @j | @down | @kpdown ->
5212 begin match state.autoscroll with
5213 | None ->
5214 begin match state.mode with
5215 | Birdseye beye -> downbirdseye 1 beye
5216 | Textentry _
5217 | View
5218 | LinkNav _ ->
5219 if ctrl
5220 then gotoy_and_clear_text (clamp (state.winh/2))
5221 else (
5222 if not (Wsi.withshift mask) && conf.presentation
5223 then nextpage ()
5224 else gotoghyll1 true (clamp (conf.scrollstep))
5227 | Some n ->
5228 setautoscrollspeed n true
5231 | @left | @right | @kpleft | @kpright when not (Wsi.withalt mask) ->
5232 if canpan ()
5233 then
5234 let dx =
5235 if ctrl
5236 then state.winw / 2
5237 else conf.hscrollstep
5239 let dx = if key = @left || key = @kpleft then dx else -dx in
5240 state.x <- panbound (state.x + dx);
5241 gotoy_and_clear_text state.y
5242 else (
5243 state.text <- E.s;
5244 G.postRedisplay "left/right"
5247 | @prior | @kpprior ->
5248 let y =
5249 if ctrl
5250 then
5251 match state.layout with
5252 | [] -> state.y
5253 | l :: _ -> state.y - l.pagey
5254 else
5255 clamp (pgscale (-state.winh))
5257 gotoghyll y
5259 | @next | @kpnext ->
5260 let y =
5261 if ctrl
5262 then
5263 match List.rev state.layout with
5264 | [] -> state.y
5265 | l :: _ -> getpagey l.pageno
5266 else
5267 clamp (pgscale state.winh)
5269 gotoghyll y
5271 | @g | @home | @kphome ->
5272 addnav ();
5273 gotoghyll 0
5274 | @G | @jend | @kpend ->
5275 addnav ();
5276 gotoghyll (clamp state.maxy)
5278 | @right | @kpright when Wsi.withalt mask ->
5279 gotoghyll (getnav 1)
5280 | @left | @kpleft when Wsi.withalt mask ->
5281 gotoghyll (getnav ~-1)
5283 | @r ->
5284 reload ()
5286 | @v when conf.debug ->
5287 state.rects <- [];
5288 List.iter (fun l ->
5289 match getopaque l.pageno with
5290 | None -> ()
5291 | Some opaque ->
5292 let x0, y0, x1, y1 = pagebbox opaque in
5293 let a,b = float x0, float y0 in
5294 let c,d = float x1, float y0 in
5295 let e,f = float x1, float y1 in
5296 let h,j = float x0, float y1 in
5297 let rect = (a,b,c,d,e,f,h,j) in
5298 debugrect rect;
5299 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5300 ) state.layout;
5301 G.postRedisplay "v";
5303 | @pipe ->
5304 let mode = state.mode in
5305 let cmd = ref E.s in
5306 let onleave = function
5307 | Cancel -> state.mode <- mode
5308 | Confirm ->
5309 List.iter (fun l ->
5310 match getopaque l.pageno with
5311 | Some opaque -> pipesel opaque !cmd
5312 | None -> ()) state.layout;
5313 state.mode <- mode
5315 let ondone s =
5316 cbput state.hists.sel s;
5317 cmd := s
5319 let te =
5320 "| ", !cmd, Some (onhist state.hists.sel), textentry, ondone, true
5322 G.postRedisplay "|";
5323 state.mode <- Textentry (te, onleave);
5325 | _ ->
5326 vlog "huh? %s" (Wsi.keyname key)
5329 let linknavkeyboard key mask linknav =
5330 let getpage pageno =
5331 let rec loop = function
5332 | [] -> None
5333 | l :: _ when l.pageno = pageno -> Some l
5334 | _ :: rest -> loop rest
5335 in loop state.layout
5337 let doexact (pageno, n) =
5338 match getopaque pageno, getpage pageno with
5339 | Some opaque, Some l ->
5340 if key = @enter || key = @kpenter
5341 then
5342 let under = getlink opaque n in
5343 G.postRedisplay "link gotounder";
5344 gotounder under;
5345 state.mode <- View;
5346 else
5347 let opt, dir =
5348 match key with
5349 | @home ->
5350 Some (findlink opaque LDfirst), -1
5352 | @jend ->
5353 Some (findlink opaque LDlast), 1
5355 | @left ->
5356 Some (findlink opaque (LDleft n)), -1
5358 | @right ->
5359 Some (findlink opaque (LDright n)), 1
5361 | @up ->
5362 Some (findlink opaque (LDup n)), -1
5364 | @down ->
5365 Some (findlink opaque (LDdown n)), 1
5367 | _ -> None, 0
5369 let pwl l dir =
5370 begin match findpwl l.pageno dir with
5371 | Pwlnotfound -> ()
5372 | Pwl pageno ->
5373 let notfound dir =
5374 state.mode <- LinkNav (Ltgendir dir);
5375 let y, h = getpageyh pageno in
5376 let y =
5377 if dir < 0
5378 then y + h - state.winh
5379 else y
5381 gotoy y
5383 begin match getopaque pageno, getpage pageno with
5384 | Some opaque, Some _ ->
5385 let link =
5386 let ld = if dir > 0 then LDfirst else LDlast in
5387 findlink opaque ld
5389 begin match link with
5390 | Lfound m ->
5391 showlinktype (getlink opaque m);
5392 state.mode <- LinkNav (Ltexact (pageno, m));
5393 G.postRedisplay "linknav jpage";
5394 | Lnotfound -> notfound dir
5395 end;
5396 | _ -> notfound dir
5397 end;
5398 end;
5400 begin match opt with
5401 | Some Lnotfound -> pwl l dir;
5402 | Some (Lfound m) ->
5403 if m = n
5404 then pwl l dir
5405 else (
5406 let _, y0, _, y1 = getlinkrect opaque m in
5407 if y0 < l.pagey
5408 then gotopage1 l.pageno y0
5409 else (
5410 let d = fstate.fontsize + 1 in
5411 if y1 - l.pagey > l.pagevh - d
5412 then gotopage1 l.pageno (y1 - state.winh - hscrollh () + d)
5413 else G.postRedisplay "linknav";
5415 showlinktype (getlink opaque m);
5416 state.mode <- LinkNav (Ltexact (l.pageno, m));
5419 | None -> viewkeyboard key mask
5420 end;
5421 | _ -> viewkeyboard key mask
5423 if key = @insert
5424 then (
5425 state.mode <- View;
5426 G.postRedisplay "leave linknav"
5428 else
5429 match linknav with
5430 | Ltgendir _ | Ltnotready _ -> viewkeyboard key mask
5431 | Ltexact exact -> doexact exact
5434 let keyboard key mask =
5435 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5436 then wcmd "interrupt"
5437 else state.uioh <- state.uioh#key key mask
5440 let birdseyekeyboard key mask
5441 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5442 let incr =
5443 match conf.columns with
5444 | Csingle _ -> 1
5445 | Cmulti ((c, _, _), _) -> c
5446 | Csplit _ -> failwith "bird's eye split mode"
5448 let pgh layout = List.fold_left
5449 (fun m l -> max l.pageh m) state.winh layout in
5450 match key with
5451 | @l when Wsi.withctrl mask ->
5452 let y, h = getpageyh pageno in
5453 let top = (state.winh - h) / 2 in
5454 gotoy (max 0 (y - top))
5455 | @enter | @kpenter -> leavebirdseye beye false
5456 | @escape -> leavebirdseye beye true
5457 | @up -> upbirdseye incr beye
5458 | @down -> downbirdseye incr beye
5459 | @left -> upbirdseye 1 beye
5460 | @right -> downbirdseye 1 beye
5462 | @prior ->
5463 begin match state.layout with
5464 | l :: _ ->
5465 if l.pagey != 0
5466 then (
5467 state.mode <- Birdseye (
5468 oconf, leftx, l.pageno, hooverpageno, anchor
5470 gotopage1 l.pageno 0;
5472 else (
5473 let layout = layout (state.y-state.winh) (pgh state.layout) in
5474 match layout with
5475 | [] -> gotoy (clamp (-state.winh))
5476 | l :: _ ->
5477 state.mode <- Birdseye (
5478 oconf, leftx, l.pageno, hooverpageno, anchor
5480 gotopage1 l.pageno 0
5483 | [] -> gotoy (clamp (-state.winh))
5484 end;
5486 | @next ->
5487 begin match List.rev state.layout with
5488 | l :: _ ->
5489 let layout = layout (state.y + (pgh state.layout)) state.winh in
5490 begin match layout with
5491 | [] ->
5492 let incr = l.pageh - l.pagevh in
5493 if incr = 0
5494 then (
5495 state.mode <-
5496 Birdseye (
5497 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5499 G.postRedisplay "birdseye pagedown";
5501 else gotoy (clamp (incr + conf.interpagespace*2));
5503 | l :: _ ->
5504 state.mode <-
5505 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5506 gotopage1 l.pageno 0;
5509 | [] -> gotoy (clamp state.winh)
5510 end;
5512 | @home ->
5513 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5514 gotopage1 0 0
5516 | @jend ->
5517 let pageno = state.pagecount - 1 in
5518 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5519 if not (pagevisible state.layout pageno)
5520 then
5521 let h =
5522 match List.rev state.pdims with
5523 | [] -> state.winh
5524 | (_, _, h, _) :: _ -> h
5526 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5527 else G.postRedisplay "birdseye end";
5529 | _ -> viewkeyboard key mask
5532 let drawpage l =
5533 let color =
5534 match state.mode with
5535 | Textentry _ -> scalecolor 0.4
5536 | LinkNav _
5537 | View -> scalecolor 1.0
5538 | Birdseye (_, _, pageno, hooverpageno, _) ->
5539 if l.pageno = hooverpageno
5540 then scalecolor 0.9
5541 else (
5542 if l.pageno = pageno
5543 then (
5544 let c = scalecolor 1.0 in
5545 GlDraw.color c;
5546 GlDraw.line_width 3.0;
5547 let dispx = xadjsb () + l.pagedispx in
5548 linerect
5549 (float (dispx-1)) (float (l.pagedispy-1))
5550 (float (dispx+l.pagevw+1))
5551 (float (l.pagedispy+l.pagevh+1))
5553 GlDraw.line_width 1.0;
5556 else scalecolor 0.8
5559 drawtiles l color;
5562 let postdrawpage l linkindexbase =
5563 match getopaque l.pageno with
5564 | Some opaque ->
5565 if tileready l l.pagex l.pagey
5566 then
5567 let x = l.pagedispx - l.pagex + xadjsb ()
5568 and y = l.pagedispy - l.pagey in
5569 let hlmask =
5570 match conf.columns with
5571 | Csingle _ | Cmulti _ ->
5572 (if conf.hlinks then 1 else 0)
5573 + (if state.glinks
5574 && not (isbirdseye state.mode) then 2 else 0)
5575 | Csplit _ -> 0
5577 let s =
5578 match state.mode with
5579 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5580 | Textentry _
5581 | Birdseye _
5582 | View
5583 | LinkNav _ -> E.s
5585 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5586 else 0
5587 | _ -> 0
5590 let scrollindicator () =
5591 let sbw, ph, sh = state.uioh#scrollph in
5592 let sbh, pw, sw = state.uioh#scrollpw in
5594 let x0,x1,hx0 =
5595 if conf.leftscroll
5596 then (0, sbw, sbw)
5597 else ((state.winw - sbw), state.winw, 0)
5600 GlDraw.color (0.64, 0.64, 0.64);
5601 filledrect (float x0) 0. (float x1) (float state.winh);
5602 filledrect
5603 (float hx0) (float (state.winh - sbh))
5604 (float (hx0 + wadjsb () + state.winw)) (float state.winh)
5606 GlDraw.color (0.0, 0.0, 0.0);
5608 filledrect (float x0) ph (float x1) (ph +. sh);
5609 let pw = pw +. float hx0 in
5610 filledrect pw (float (state.winh - sbh)) (pw +. sw) (float state.winh);
5613 let showsel () =
5614 match state.mstate with
5615 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5618 | Msel ((x0, y0), (x1, y1)) ->
5619 let identify opaque l px py = Some (opaque, l.pageno, px, py) in
5620 let o0,n0,px0,py0 = onppundermouse identify x0 y0 (~< E.s, -1, 0, 0) in
5621 let _o1,n1,px1,py1 = onppundermouse identify x1 y1 (~< E.s, -1, 0, 0) in
5622 if n0 != -1 && n0 = n1 then seltext o0 (px0, py0, px1, py1);
5625 let showrects = function [] -> () | rects ->
5626 Gl.enable `blend;
5627 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5628 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5629 List.iter
5630 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5631 List.iter (fun l ->
5632 if l.pageno = pageno
5633 then (
5634 let dx = float (l.pagedispx - l.pagex) in
5635 let dy = float (l.pagedispy - l.pagey) in
5636 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5637 Raw.sets_float state.vraw ~pos:0
5638 [| x0+.dx; y0+.dy;
5639 x1+.dx; y1+.dy;
5640 x3+.dx; y3+.dy;
5641 x2+.dx; y2+.dy |];
5642 GlArray.vertex `two state.vraw;
5643 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
5645 ) state.layout
5646 ) rects
5648 Gl.disable `blend;
5651 let display () =
5652 GlClear.color (scalecolor2 conf.bgcolor);
5653 GlClear.clear [`color];
5654 List.iter drawpage state.layout;
5655 let rects =
5656 match state.mode with
5657 | LinkNav (Ltexact (pageno, linkno)) ->
5658 begin match getopaque pageno with
5659 | Some opaque ->
5660 let dx = xadjsb () in
5661 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5662 let x0 = x0 + dx and x1 = x1 + dx in
5663 (pageno, 5, (
5664 float x0, float y0,
5665 float x1, float y0,
5666 float x1, float y1,
5667 float x0, float y1)
5668 ) :: state.rects
5669 | None -> state.rects
5671 | LinkNav (Ltgendir _) | LinkNav (Ltnotready _)
5672 | Birdseye _
5673 | Textentry _
5674 | View -> state.rects
5676 showrects rects;
5677 let rec postloop linkindexbase = function
5678 | l :: rest ->
5679 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5680 postloop linkindexbase rest
5681 | [] -> ()
5683 showsel ();
5684 postloop 0 state.layout;
5685 state.uioh#display;
5686 begin match state.mstate with
5687 | Mzoomrect ((x0, y0), (x1, y1)) ->
5688 Gl.enable `blend;
5689 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5690 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5691 filledrect (float x0) (float y0) (float x1) (float y1);
5692 Gl.disable `blend;
5693 | Msel _
5694 | Mpan _
5695 | Mscrolly | Mscrollx
5696 | Mzoom _
5697 | Mnone -> ()
5698 end;
5699 enttext ();
5700 scrollindicator ();
5701 Wsi.swapb ();
5704 let zoomrect x y x1 y1 =
5705 let x0 = min x x1
5706 and x1 = max x x1
5707 and y0 = min y y1 in
5708 gotoy (state.y + y0);
5709 state.anchor <- getanchor ();
5710 let zoom = (float state.w) /. float (x1 - x0) in
5711 let margin =
5712 let simple () =
5713 let adjw = wadjsb () + state.winw in
5714 if state.w < adjw
5715 then (adjw - state.w) / 2
5716 else 0
5718 match conf.fitmodel with
5719 | FitWidth | FitProportional -> simple ()
5720 | FitPage ->
5721 match conf.columns with
5722 | Csplit _ ->
5723 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
5724 | Cmulti _ | Csingle _ -> simple ()
5726 state.x <- (state.x + margin) - x0;
5727 setzoom zoom;
5728 resetmstate ();
5731 let annot inline x y =
5732 match unproject x y with
5733 | Some (opaque, n, ux, uy) ->
5734 let add text =
5735 addannot opaque ux uy text;
5736 wcmd "freepage %s" (~> opaque);
5737 Hashtbl.remove state.pagemap (n, state.gen);
5738 flushtiles ();
5739 gotoy state.y
5741 if inline
5742 then
5743 let ondone s = add s in
5744 let mode = state.mode in
5745 state.mode <- Textentry (
5746 ("annotation: ", E.s, None, textentry, ondone, true),
5747 fun _ -> state.mode <- mode);
5748 state.text <- E.s;
5749 enttext ();
5750 G.postRedisplay "annot"
5751 else
5752 let text =
5753 let s = getusertext E.s in
5754 let l = Str.split newlinere s in
5755 String.concat " " l
5757 add text
5758 | _ -> ()
5761 let zoomblock x y =
5762 let g opaque l px py =
5763 match rectofblock opaque px py with
5764 | Some a ->
5765 let x0 = a.(0) -. 20. in
5766 let x1 = a.(1) +. 20. in
5767 let y0 = a.(2) -. 20. in
5768 let zoom = (float state.w) /. (x1 -. x0) in
5769 let pagey = getpagey l.pageno in
5770 gotoy_and_clear_text (pagey + truncate y0);
5771 state.anchor <- getanchor ();
5772 let margin = (state.w - l.pagew)/2 in
5773 state.x <- -truncate x0 - margin;
5774 setzoom zoom;
5775 None
5776 | None -> None
5778 match conf.columns with
5779 | Csplit _ ->
5780 showtext '!' "block zooming does not work properly in split columns mode"
5781 | Cmulti _ | Csingle _ -> onppundermouse g x y ()
5784 let scrollx x =
5785 let winw = wadjsb () + state.winw - 1 in
5786 let s = float x /. float winw in
5787 let destx = truncate (float (state.w + winw) *. s) in
5788 state.x <- winw - destx;
5789 gotoy_and_clear_text state.y;
5790 state.mstate <- Mscrollx;
5793 let scrolly y =
5794 let s = float y /. float state.winh in
5795 let desty = truncate (float (state.maxy - state.winh) *. s) in
5796 gotoy_and_clear_text desty;
5797 state.mstate <- Mscrolly;
5800 let viewmulticlick clicks x y mask =
5801 let g opaque l px py =
5802 let mark =
5803 match clicks with
5804 | 2 -> Mark_word
5805 | 3 -> Mark_line
5806 | 4 -> Mark_block
5807 | _ -> Mark_page
5809 if markunder opaque px py mark
5810 then (
5811 Some (fun () ->
5812 let dopipe cmd =
5813 match getopaque l.pageno with
5814 | None -> ()
5815 | Some opaque -> pipesel opaque cmd
5817 state.roam <- (fun () -> dopipe conf.paxcmd);
5818 if not (Wsi.withctrl mask) then dopipe conf.selcmd;
5821 else None
5823 G.postRedisplay "viewmulticlick";
5824 onppundermouse g x y (fun () -> showtext '!' "Nothing to select") ();
5827 let canselect () =
5828 match conf.columns with
5829 | Csplit _ -> false
5830 | Csingle _ | Cmulti _ -> conf.angle mod 360 = 0
5833 let viewmouse button down x y mask =
5834 match button with
5835 | n when (n == 4 || n == 5) && not down ->
5836 if Wsi.withctrl mask
5837 then (
5838 match state.mstate with
5839 | Mzoom (oldn, i) ->
5840 if oldn = n
5841 then (
5842 if i = 2
5843 then
5844 let incr =
5845 match n with
5846 | 5 ->
5847 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5848 | _ ->
5849 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5851 let zoom = conf.zoom -. incr in
5852 setzoom zoom;
5853 state.mstate <- Mzoom (n, 0);
5854 else
5855 state.mstate <- Mzoom (n, i+1);
5857 else state.mstate <- Mzoom (n, 0)
5859 | Msel _
5860 | Mpan _
5861 | Mscrolly | Mscrollx
5862 | Mzoomrect _
5863 | Mnone -> state.mstate <- Mzoom (n, 0)
5865 else (
5866 match state.autoscroll with
5867 | Some step -> setautoscrollspeed step (n=4)
5868 | None ->
5869 if conf.wheelbypage || conf.presentation
5870 then (
5871 if n = 4
5872 then prevpage ()
5873 else nextpage ()
5875 else
5876 let incr =
5877 if n = 4
5878 then -conf.scrollstep
5879 else conf.scrollstep
5881 let incr = incr * 2 in
5882 let y = clamp incr in
5883 gotoy_and_clear_text y
5886 | n when (n = 6 || n = 7) && not down && canpan () ->
5887 state.x <-
5888 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
5889 gotoy_and_clear_text state.y
5891 | 1 when Wsi.withshift mask ->
5892 state.mstate <- Mnone;
5893 if not down
5894 then (
5895 match unproject x y with
5896 | Some (_, pageno, ux, uy) ->
5897 let cmd = Printf.sprintf
5898 "%s %s %d %d %d"
5899 conf.stcmd state.path pageno ux uy
5901 addpid @@ popen cmd []
5902 | None -> ()
5905 | 1 when Wsi.withctrl mask ->
5906 if down
5907 then (
5908 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5909 state.mstate <- Mpan (x, y)
5911 else
5912 state.mstate <- Mnone
5914 | 3 ->
5915 if down
5916 then (
5917 if Wsi.withshift mask
5918 then (
5919 annot conf.annotinline x y;
5920 G.postRedisplay "addannot"
5922 else
5923 let p = (x, y) in
5924 Wsi.setcursor Wsi.CURSOR_CYCLE;
5925 state.mstate <- Mzoomrect (p, p)
5927 else (
5928 match state.mstate with
5929 | Mzoomrect ((x0, y0), _) ->
5930 if abs (x-x0) > 10 && abs (y - y0) > 10
5931 then zoomrect x0 y0 x y
5932 else (
5933 resetmstate ();
5934 G.postRedisplay "kill accidental zoom rect";
5936 | Msel _
5937 | Mpan _
5938 | Mscrolly | Mscrollx
5939 | Mzoom _
5940 | Mnone ->
5941 resetmstate ()
5944 | 1 when x > state.winw - vscrollw () ->
5945 if down
5946 then
5947 let _, position, sh = state.uioh#scrollph in
5948 if y > truncate position && y < truncate (position +. sh)
5949 then state.mstate <- Mscrolly
5950 else scrolly y
5951 else
5952 state.mstate <- Mnone
5954 | 1 when y > state.winh - hscrollh () ->
5955 if down
5956 then
5957 let _, position, sw = state.uioh#scrollpw in
5958 if x > truncate position && x < truncate (position +. sw)
5959 then state.mstate <- Mscrollx
5960 else scrollx x
5961 else
5962 state.mstate <- Mnone
5964 | 1 when state.bzoom -> if not down then zoomblock x y
5966 | 1 ->
5967 let dest = if down then getunder x y else Unone in
5968 begin match dest with
5969 | Ulinkgoto _
5970 | Ulinkuri _
5971 | Uremote _ | Uremotedest _
5972 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5973 gotounder dest
5975 | Unone when down ->
5976 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5977 state.mstate <- Mpan (x, y);
5979 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
5981 | Unone | Utext _ ->
5982 if down
5983 then (
5984 if canselect ()
5985 then (
5986 state.mstate <- Msel ((x, y), (x, y));
5987 G.postRedisplay "mouse select";
5990 else (
5991 match state.mstate with
5992 | Mnone -> ()
5994 | Mzoom _ | Mscrollx | Mscrolly ->
5995 state.mstate <- Mnone
5997 | Mzoomrect ((x0, y0), _) ->
5998 zoomrect x0 y0 x y
6000 | Mpan _ ->
6001 Wsi.setcursor Wsi.CURSOR_INHERIT;
6002 state.mstate <- Mnone
6004 | Msel ((x0, y0), (x1, y1)) ->
6005 let rec loop = function
6006 | [] -> ()
6007 | l :: rest ->
6008 let inside =
6009 let a0 = l.pagedispy in
6010 let a1 = a0 + l.pagevh in
6011 let b0 = l.pagedispx in
6012 let b1 = b0 + l.pagevw in
6013 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
6014 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
6016 if inside
6017 then
6018 match getopaque l.pageno with
6019 | Some opaque ->
6020 let dosel cmd () =
6021 match Unix.pipe () with
6022 | exception exn ->
6023 showtext '!'
6024 (Printf.sprintf
6025 "can not create sel pipe: %s"
6026 (exntos exn));
6027 | (r, w) ->
6028 let clo what fd =
6029 Ne.clo fd (fun msg ->
6030 dolog "%s close failed: %s" what msg)
6032 let pid =
6033 try popen cmd [r, 0; w, -1]
6034 with exn ->
6035 dolog "can not execute %S: %s"
6036 cmd (exntos exn);
6039 if pid > 0
6040 then (
6041 copysel w opaque;
6042 G.postRedisplay "copysel";
6044 else clo "Msel pipe/w" w;
6045 clo "Msel pipe/r" r;
6047 dosel conf.selcmd ();
6048 state.roam <- dosel conf.paxcmd;
6049 | None -> ()
6050 else loop rest
6052 loop state.layout;
6053 resetmstate ();
6057 | _ -> ()
6060 let birdseyemouse button down x y mask
6061 (conf, leftx, _, hooverpageno, anchor) =
6062 match button with
6063 | 1 when down ->
6064 let rec loop = function
6065 | [] -> ()
6066 | l :: rest ->
6067 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6068 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6069 then (
6070 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
6072 else loop rest
6074 loop state.layout
6075 | 3 -> ()
6076 | _ -> viewmouse button down x y mask
6079 let uioh = object
6080 method display = ()
6082 method key key mask =
6083 begin match state.mode with
6084 | Textentry textentry -> textentrykeyboard key mask textentry
6085 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6086 | View -> viewkeyboard key mask
6087 | LinkNav linknav -> linknavkeyboard key mask linknav
6088 end;
6089 state.uioh
6091 method button button bstate x y mask =
6092 begin match state.mode with
6093 | LinkNav _
6094 | View -> viewmouse button bstate x y mask
6095 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6096 | Textentry _ -> ()
6097 end;
6098 state.uioh
6100 method multiclick clicks x y mask =
6101 begin match state.mode with
6102 | LinkNav _
6103 | View -> viewmulticlick clicks x y mask
6104 | Birdseye _
6105 | Textentry _ -> ()
6106 end;
6107 state.uioh
6109 method motion x y =
6110 begin match state.mode with
6111 | Textentry _ -> ()
6112 | View | Birdseye _ | LinkNav _ ->
6113 match state.mstate with
6114 | Mzoom _ | Mnone -> ()
6116 | Mpan (x0, y0) ->
6117 let dx = x - x0
6118 and dy = y0 - y in
6119 state.mstate <- Mpan (x, y);
6120 if canpan ()
6121 then state.x <- panbound (state.x + dx);
6122 let y = clamp dy in
6123 gotoy_and_clear_text y
6125 | Msel (a, _) ->
6126 state.mstate <- Msel (a, (x, y));
6127 G.postRedisplay "motion select";
6129 | Mscrolly ->
6130 let y = min state.winh (max 0 y) in
6131 scrolly y
6133 | Mscrollx ->
6134 let x = min state.winw (max 0 x) in
6135 scrollx x
6137 | Mzoomrect (p0, _) ->
6138 state.mstate <- Mzoomrect (p0, (x, y));
6139 G.postRedisplay "motion zoomrect";
6140 end;
6141 state.uioh
6143 method pmotion x y =
6144 begin match state.mode with
6145 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6146 let rec loop = function
6147 | [] ->
6148 if hooverpageno != -1
6149 then (
6150 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6151 G.postRedisplay "pmotion birdseye no hoover";
6153 | l :: rest ->
6154 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6155 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6156 then (
6157 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6158 G.postRedisplay "pmotion birdseye hoover";
6160 else loop rest
6162 loop state.layout
6164 | Textentry _ -> ()
6166 | LinkNav _
6167 | View ->
6168 match state.mstate with
6169 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ -> ()
6170 | Mnone ->
6171 updateunder x y;
6172 if canselect ()
6173 then
6174 match conf.pax with
6175 | None -> ()
6176 | Some r ->
6177 let past, _, _ = !r in
6178 let now = now () in
6179 let delta = now -. past in
6180 if delta > 0.01
6181 then paxunder x y
6182 else r := (now, x, y)
6183 end;
6184 state.uioh
6186 method infochanged _ = ()
6188 method scrollph =
6189 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6190 let p, h =
6191 if maxy = 0
6192 then 0.0, float state.winh
6193 else scrollph state.y maxy
6195 vscrollw (), p, h
6197 method scrollpw =
6198 let winw = wadjsb () + state.winw in
6199 let fwinw = float winw in
6200 let sw =
6201 let sw = fwinw /. float state.w in
6202 let sw = fwinw *. sw in
6203 max sw (float conf.scrollh)
6205 let position =
6206 let maxx = state.w + winw in
6207 let x = winw - state.x in
6208 let percent = float x /. float maxx in
6209 (fwinw -. sw) *. percent
6211 hscrollh (), position, sw
6213 method modehash =
6214 let modename =
6215 match state.mode with
6216 | LinkNav _ -> "links"
6217 | Textentry _ -> "textentry"
6218 | Birdseye _ -> "birdseye"
6219 | View -> "view"
6221 findkeyhash conf modename
6223 method eformsgs = true
6224 method alwaysscrolly = false
6225 end;;
6227 let adderrmsg src msg =
6228 Buffer.add_string state.errmsgs msg;
6229 state.newerrmsgs <- true;
6230 G.postRedisplay src
6233 let adderrfmt src fmt =
6234 Format.ksprintf (fun s -> adderrmsg src s) fmt;
6237 let ract cmds =
6238 let cl = splitatspace cmds in
6239 let scan s fmt f =
6240 try Scanf.sscanf s fmt f
6241 with exn ->
6242 adderrfmt "remote exec"
6243 "error processing '%S': %s\n" cmds (exntos exn)
6245 match cl with
6246 | "reload" :: [] -> reload ()
6247 | "goto" :: args :: [] ->
6248 scan args "%u %f %f"
6249 (fun pageno x y ->
6250 let cmd, _ = state.geomcmds in
6251 if emptystr cmd
6252 then gotopagexy pageno x y
6253 else
6254 let f prevf () =
6255 gotopagexy pageno x y;
6256 prevf ()
6258 state.reprf <- f state.reprf
6260 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
6261 | "gotor" :: args :: [] ->
6262 scan args "%S %u"
6263 (fun filename pageno -> gotounder (Uremote (filename, pageno)))
6264 | "gotord" :: args :: [] ->
6265 scan args "%S %S"
6266 (fun filename dest -> gotounder (Uremotedest (filename, dest)))
6267 | "rect" :: args :: [] ->
6268 scan args "%u %u %f %f %f %f"
6269 (fun pageno color x0 y0 x1 y1 ->
6270 onpagerect pageno (fun w h ->
6271 let _,w1,h1,_ = getpagedim pageno in
6272 let sw = float w1 /. float w
6273 and sh = float h1 /. float h in
6274 let x0s = x0 *. sw
6275 and x1s = x1 *. sw
6276 and y0s = y0 *. sh
6277 and y1s = y1 *. sh in
6278 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
6279 debugrect rect;
6280 state.rects <- (pageno, color, rect) :: state.rects;
6281 G.postRedisplay "rect";
6284 | "activatewin" :: [] -> Wsi.activatewin ()
6285 | "quit" :: [] -> raise Quit
6286 | _ ->
6287 adderrfmt "remote command"
6288 "error processing remote command: %S\n" cmds;
6291 let remote =
6292 let scratch = Bytes.create 80 in
6293 let buf = Buffer.create 80 in
6294 fun fd ->
6295 let rec tempfr () =
6296 try Some (Unix.read fd scratch 0 80)
6297 with
6298 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
6299 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
6300 | exn -> raise exn
6302 match tempfr () with
6303 | None -> Some fd
6304 | Some n ->
6305 if n = 0
6306 then (
6307 Unix.close fd;
6308 if Buffer.length buf > 0
6309 then (
6310 let s = Buffer.contents buf in
6311 Buffer.clear buf;
6312 ract s;
6314 None
6316 else
6317 let rec eat ppos =
6318 let nlpos =
6320 let pos = Bytes.index_from scratch ppos '\n' in
6321 if pos >= n then -1 else pos
6322 with Not_found -> -1
6324 if nlpos >= 0
6325 then (
6326 Buffer.add_subbytes buf scratch ppos (nlpos-ppos);
6327 let s = Buffer.contents buf in
6328 Buffer.clear buf;
6329 ract s;
6330 eat (nlpos+1);
6332 else (
6333 Buffer.add_subbytes buf scratch ppos (n-ppos);
6334 Some fd
6336 in eat 0
6339 let remoteopen path =
6340 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
6341 with exn ->
6342 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
6343 None
6346 let () =
6347 let gcconfig = ref E.s in
6348 let trimcachepath = ref E.s in
6349 let rcmdpath = ref E.s in
6350 let pageno = ref None in
6351 let rootwid = ref 0 in
6352 let openlast = ref false in
6353 let nofc = ref false in
6354 let doreap = ref false in
6355 selfexec := Sys.executable_name;
6356 Arg.parse
6357 (Arg.align
6358 [("-p", Arg.String (fun s -> state.password <- s),
6359 "<password> Set password");
6361 ("-f", Arg.String
6362 (fun s ->
6363 Config.fontpath := s;
6364 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
6366 "<path> Set path to the user interface font");
6368 ("-c", Arg.String
6369 (fun s ->
6370 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
6371 Config.confpath := s),
6372 "<path> Set path to the configuration file");
6374 ("-last", Arg.Set openlast, " Open last document");
6376 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
6377 "<page-number> Jump to page");
6379 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6380 "<path> Set path to the trim cache file");
6382 ("-dest", Arg.String (fun s -> state.nameddest <- s),
6383 "<named-destination> Set named destination");
6385 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
6386 ("-cxack", Arg.Set cxack, " Cut corners");
6388 ("-remote", Arg.String (fun s -> rcmdpath := s),
6389 "<path> Set path to the remote commands source");
6391 ("-origin", Arg.String (fun s -> state.origin <- s),
6392 "<original-path> Set original path");
6394 ("-gc", Arg.Set_string gcconfig,
6395 "<script-path> Collect garbage with the help of a script");
6397 ("-nofc", Arg.Set nofc, " Do not use fontconfig");
6399 ("-v", Arg.Unit (fun () ->
6400 Printf.printf
6401 "%s\nconfiguration path: %s\n"
6402 (version ())
6403 Config.defconfpath
6405 exit 0), " Print version and exit");
6407 ("-embed", Arg.Set_int rootwid,
6408 "<window-id> Embed into window")
6411 (fun s -> state.path <- s)
6412 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6414 if !wtmode
6415 then selfexec := !selfexec ^ " -wtmode";
6417 let histmode = emptystr state.path && not !openlast in
6419 if not (Config.load !openlast)
6420 then prerr_endline "failed to load configuration";
6421 begin match !pageno with
6422 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
6423 | None -> ()
6424 end;
6426 if not (emptystr !gcconfig)
6427 then (
6428 let c, s =
6429 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6430 | exception exn ->
6431 error "gc socketpair failed: %s" (exntos exn)
6432 | rw -> rw
6434 match addpid @@ popen !gcconfig [(c, 0); (c, 1)] with
6435 | exception exn ->
6436 error "failed to popen gc script: %s" (exntos exn);
6437 | _ ->
6438 Config.gc s s;
6439 exit 0
6442 let wsfd, winw, winh = Wsi.init (object (self)
6443 val mutable m_clicks = 0
6444 val mutable m_click_x = 0
6445 val mutable m_click_y = 0
6446 val mutable m_lastclicktime = infinity
6448 method private cleanup =
6449 state.roam <- noroam;
6450 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap
6451 method expose = G.postRedisplay"expose"
6452 method visible v =
6453 let name =
6454 match v with
6455 | Wsi.Unobscured -> "unobscured"
6456 | Wsi.PartiallyObscured -> "partiallyobscured"
6457 | Wsi.FullyObscured -> "fullyobscured"
6459 vlog "visibility change %s" name
6460 method display = display ()
6461 method map mapped = vlog "mappped %b" mapped
6462 method reshape w h =
6463 self#cleanup;
6464 reshape w h
6465 method mouse b d x y m =
6466 if d && canselect ()
6467 then (
6468 (* http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx *)
6469 m_click_x <- x;
6470 m_click_y <- y;
6471 if b = 1
6472 then (
6473 let t = now () in
6474 if abs x - m_click_x > 10
6475 || abs y - m_click_y > 10
6476 || abs_float (t -. m_lastclicktime) > 0.3
6477 then m_clicks <- 0;
6478 m_clicks <- m_clicks + 1;
6479 m_lastclicktime <- t;
6480 if m_clicks = 1
6481 then (
6482 self#cleanup;
6483 G.postRedisplay "cleanup";
6484 state.uioh <- state.uioh#button b d x y m;
6486 else state.uioh <- state.uioh#multiclick m_clicks x y m
6488 else (
6489 self#cleanup;
6490 m_clicks <- 0;
6491 m_lastclicktime <- infinity;
6492 state.uioh <- state.uioh#button b d x y m
6495 else (
6496 state.uioh <- state.uioh#button b d x y m
6498 method motion x y =
6499 state.mpos <- (x, y);
6500 state.uioh <- state.uioh#motion x y
6501 method pmotion x y =
6502 state.mpos <- (x, y);
6503 state.uioh <- state.uioh#pmotion x y
6504 method key k m =
6505 let mascm = m land (
6506 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6507 ) in
6508 let keyboard k m =
6509 let x = state.x and y = state.y in
6510 keyboard k m;
6511 if x != state.x || y != state.y then self#cleanup
6513 match state.keystate with
6514 | KSnone ->
6515 let km = k, mascm in
6516 begin
6517 match
6518 let modehash = state.uioh#modehash in
6519 try Hashtbl.find modehash km
6520 with Not_found ->
6521 try Hashtbl.find (findkeyhash conf "global") km
6522 with Not_found -> KMinsrt (k, m)
6523 with
6524 | KMinsrt (k, m) -> keyboard k m
6525 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6526 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6528 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6529 List.iter (fun (k, m) -> keyboard k m) insrt;
6530 state.keystate <- KSnone
6531 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6532 state.keystate <- KSinto (keys, insrt)
6533 | KSinto _ -> state.keystate <- KSnone
6535 method enter x y =
6536 state.mpos <- (x, y);
6537 state.uioh <- state.uioh#pmotion x y
6538 method leave = state.mpos <- (-1, -1)
6539 method winstate wsl = state.winstate <- wsl
6540 method quit = raise Quit
6541 end) !rootwid conf.cwinw conf.cwinh platform in
6543 state.wsfd <- wsfd;
6545 if not (
6546 List.exists GlMisc.check_extension
6547 [ "GL_ARB_texture_rectangle"
6548 ; "GL_EXT_texture_recangle"
6549 ; "GL_NV_texture_rectangle" ]
6551 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6553 if (
6554 let r = GlMisc.get_string `renderer in
6555 let p = "Mesa DRI Intel(" in
6556 let l = String.length p in
6557 String.length r > l && String.sub r 0 l = p
6559 then (
6560 defconf.sliceheight <- 1024;
6561 defconf.texcount <- 32;
6562 defconf.usepbo <- true;
6565 let cs, ss =
6566 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6567 | exception exn ->
6568 Printf.eprintf "socketpair failed: %s" (exntos exn);
6569 exit 1
6570 | (r, w) ->
6571 cloexec r;
6572 cloexec w;
6573 r, w
6576 setcheckers conf.checkers;
6577 redirectstderr ();
6578 if conf.redirectstderr
6579 then
6580 at_exit (fun () ->
6581 let s = Bytes.cat
6582 (Buffer.to_bytes state.errmsgs)
6583 (match state.errfd with
6584 | Some fd ->
6585 let s = Bytes.create (80*24) in
6586 let n =
6588 let r, _, _ = Unix.select [fd] [] [] 0.0 in
6589 if List.mem fd r
6590 then Unix.read fd s 0 (Bytes.length s)
6591 else 0
6592 with _ -> 0
6594 if n = 0
6595 then E.b
6596 else Bytes.sub s 0 n
6597 | None -> E.b
6600 try ignore (Unix.write state.stderr s 0 (Bytes.length s))
6601 with exn -> print_endline (exntos exn)
6605 init cs (
6606 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
6607 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6608 !Config.fontpath, !trimcachepath,
6609 GlMisc.check_extension "GL_ARB_pixel_buffer_object",
6610 not !nofc
6612 List.iter GlArray.enable [`texture_coord; `vertex];
6613 state.ss <- ss;
6614 reshape ~firsttime:true winw winh;
6615 state.uioh <- uioh;
6616 if histmode
6617 then (
6618 Wsi.settitle "llpp (history)";
6619 enterhistmode ();
6621 else (
6622 state.text <- "Opening " ^ (mbtoutf8 state.path);
6623 opendoc state.path state.password;
6625 display ();
6626 Wsi.mapwin ();
6627 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
6629 let rec reap () =
6630 match Unix.waitpid [Unix.WNOHANG] ~-1 with
6631 | (exception (Unix.Unix_error (Unix.ECHILD, _, _))) -> ()
6632 | (exception exn) -> dolog "Unix.waitpid: %s" @@ exntos exn
6633 | 0, _ -> ()
6634 | _pid, _status -> reap ()
6636 Sys.set_signal Sys.sigchld (Sys.Signal_handle (fun _ -> doreap := true));
6638 let optrfd =
6639 ref (
6640 if nonemptystr !rcmdpath
6641 then remoteopen !rcmdpath
6642 else None
6646 let rec loop deadline =
6647 if !doreap
6648 then (
6649 doreap := false;
6650 reap ()
6652 let r =
6653 match state.errfd with
6654 | None -> [state.ss; state.wsfd]
6655 | Some fd -> [state.ss; state.wsfd; fd]
6657 let r =
6658 match !optrfd with
6659 | None -> r
6660 | Some fd -> fd :: r
6662 if state.redisplay
6663 then (
6664 state.redisplay <- false;
6665 display ();
6667 let timeout =
6668 let now = now () in
6669 if deadline > now
6670 then (
6671 if deadline = infinity
6672 then ~-.1.0
6673 else max 0.0 (deadline -. now)
6675 else 0.0
6677 let r, _, _ =
6678 try Unix.select r [] [] timeout
6679 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6681 begin match r with
6682 | [] ->
6683 state.ghyll None;
6684 let newdeadline =
6685 if state.ghyll == noghyll
6686 then
6687 match state.autoscroll with
6688 | Some step when step != 0 ->
6689 let y = state.y + step in
6690 let y =
6691 if y < 0
6692 then state.maxy
6693 else if y >= state.maxy then 0 else y
6695 gotoy y;
6696 if state.mode = View
6697 then state.text <- E.s;
6698 deadline +. 0.01
6699 | _ -> infinity
6700 else deadline +. 0.01
6702 loop newdeadline
6704 | l ->
6705 let rec checkfds = function
6706 | [] -> ()
6707 | fd :: rest when fd = state.ss ->
6708 let cmd = readcmd state.ss in
6709 act cmd;
6710 checkfds rest
6712 | fd :: rest when fd = state.wsfd ->
6713 Wsi.readresp fd;
6714 checkfds rest
6716 | fd :: rest when Some fd = !optrfd ->
6717 begin match remote fd with
6718 | None -> optrfd := remoteopen !rcmdpath;
6719 | opt -> optrfd := opt
6720 end;
6721 checkfds rest
6723 | fd :: rest ->
6724 let s = Bytes.create 80 in
6725 let n = tempfailureretry (Unix.read fd s 0) 80 in
6726 if conf.redirectstderr
6727 then (
6728 Buffer.add_substring state.errmsgs (Bytes.to_string s) 0 n;
6729 state.newerrmsgs <- true;
6730 state.redisplay <- true;
6732 else (
6733 prerr_string (String.sub (Bytes.to_string s) 0 n);
6734 flush stderr;
6736 checkfds rest
6738 checkfds l;
6739 if !reeenterhist then (
6740 enterhistmode ();
6741 reeenterhist := false;
6743 let newdeadline =
6744 let deadline1 =
6745 if deadline = infinity
6746 then now () +. 0.01
6747 else deadline
6749 match state.autoscroll with
6750 | Some step when step != 0 -> deadline1
6751 | _ -> if state.ghyll == noghyll then infinity else deadline1
6753 loop newdeadline
6754 end;
6757 loop infinity;
6758 with Quit ->
6759 Config.save leavebirdseye;
6760 if hasunsavedchanges ()
6761 then save ();