Use local versions of libraries
[llpp.git] / main.ml
blob49356ce990461dc70caeee06822831e4f5f72017
1 open Utils;;
2 open Config;;
4 exception Quit;;
6 external init : Unix.file_descr -> params -> unit = "ml_init";;
7 external seltext : opaque -> (int * int * int * int) -> unit = "ml_seltext";;
8 external hassel : opaque -> bool = "ml_hassel";;
9 external copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
10 external getpdimrect : int -> float array = "ml_getpdimrect";;
11 external whatsunder : opaque -> int -> int -> under = "ml_whatsunder";;
12 external markunder : opaque -> int -> int -> mark -> bool = "ml_markunder";;
13 external clearmark : opaque -> unit = "ml_clearmark";;
14 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
15 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
16 external measurestr : int -> string -> float = "ml_measure_string";;
17 external postprocess :
18 opaque -> int -> int -> int -> (int * string * int) -> int
19 = "ml_postprocess";;
20 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
21 external setaalevel : int -> unit = "ml_setaalevel";;
22 external realloctexts : int -> bool = "ml_realloctexts";;
23 external findlink : opaque -> linkdir -> link = "ml_findlink";;
24 external getlink : opaque -> int -> under = "ml_getlink";;
25 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
26 external getlinkcount : opaque -> int = "ml_getlinkcount";;
27 external findpwl : int -> int -> pagewithlinks = "ml_find_page_with_links";;
28 external getpbo : width -> height -> colorspace -> opaque = "ml_getpbo";;
29 external freepbo : opaque -> unit = "ml_freepbo";;
30 external unmappbo : opaque -> unit = "ml_unmappbo";;
31 external pbousable : unit -> bool = "ml_pbo_usable";;
32 external unproject : opaque -> int -> int -> (int * int) option
33 = "ml_unproject";;
34 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
35 external rectofblock : opaque -> int -> int -> float array option
36 = "ml_rectofblock";;
37 external begintiles : unit -> unit = "ml_begintiles";;
38 external endtiles : unit -> unit = "ml_endtiles";;
39 external addannot : opaque -> int -> int -> string -> unit = "ml_addannot";;
40 external modannot : opaque -> slinkindex -> string -> unit = "ml_modannot";;
41 external delannot : opaque -> slinkindex -> unit = "ml_delannot";;
42 external hasunsavedchanges : unit -> bool = "ml_hasunsavedchanges";;
43 external savedoc : string -> unit = "ml_savedoc";;
44 external getannotcontents : opaque -> slinkindex -> string
45 = "ml_getannotcontents";;
47 let selfexec = ref E.s;;
49 let drawstring size x y s =
50 Gl.enable `blend;
51 Gl.enable `texture_2d;
52 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
53 ignore (drawstr size x y s);
54 Gl.disable `blend;
55 Gl.disable `texture_2d;
58 let drawstring1 size x y s =
59 drawstr size x y s;
62 let drawstring2 size x y fmt =
63 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
66 let debugl l =
67 dolog "l %d dim=%d {" l.pageno l.pagedimno;
68 dolog " WxH %dx%d" l.pagew l.pageh;
69 dolog " vWxH %dx%d" l.pagevw l.pagevh;
70 dolog " pagex,y %d,%d" l.pagex l.pagey;
71 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
72 dolog " column %d" l.pagecol;
73 dolog "}";
76 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
77 dolog "rect {";
78 dolog " x0,y0=(% f, % f)" x0 y0;
79 dolog " x1,y1=(% f, % f)" x1 y1;
80 dolog " x2,y2=(% f, % f)" x2 y2;
81 dolog " x3,y3=(% f, % f)" x3 y3;
82 dolog "}";
85 let isbirdseye = function
86 | Birdseye _ -> true
87 | Textentry _
88 | View
89 | LinkNav _ -> false
92 let istextentry = function
93 | Textentry _ -> true
94 | Birdseye _
95 | View
96 | LinkNav _ -> false
99 let wtmode = ref false;;
100 let cxack = ref false;;
102 let pgscale h = truncate (float h *. conf.pgscale);;
104 let hscrollh () =
105 if not state.uioh#alwaysscrolly && (conf.scrollb land scrollbhv = 0)
106 || (state.x = 0 && state.w <= state.winw - conf.scrollbw)
107 then 0
108 else conf.scrollbw
111 let vscrollw () =
112 if not state.uioh#alwaysscrolly && (conf.scrollb land scrollbvv = 0)
113 then 0
114 else conf.scrollbw
117 let wadjsb () = -vscrollw ();;
118 let xadjsb () = if conf.leftscroll then vscrollw () else 0;;
120 let setfontsize n =
121 fstate.fontsize <- n;
122 fstate.wwidth <- measurestr fstate.fontsize "w";
123 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
126 let vlog fmt =
127 if conf.verbose
128 then
129 Printf.kprintf prerr_endline fmt
130 else
131 Printf.kprintf ignore fmt
134 let launchpath () =
135 if emptystr conf.pathlauncher
136 then print_endline state.path
137 else (
138 let command = Str.global_replace percentsre state.path conf.pathlauncher in
139 try addpid @@ popen command []
140 with exn -> dolog "failed to execute `%s': %s" command (exntos exn)
144 let redirectstderr () =
145 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
146 if conf.redirectstderr
147 then
148 match Unix.pipe () with
149 | exception exn ->
150 dolog "failed to create stderr redirection pipes: %s" (exntos exn)
152 | (r, w) ->
153 begin match Unix.dup Unix.stderr with
154 | exception exn ->
155 dolog "failed to dup stderr: %s" (exntos exn);
156 Ne.clo r (clofail "pipe/r");
157 Ne.clo w (clofail "pipe/w");
159 | dupstderr ->
160 begin match Unix.dup2 w Unix.stderr with
161 | exception exn ->
162 dolog "failed to dup2 to stderr: %s" (exntos exn);
163 Ne.clo dupstderr (clofail "stderr duplicate");
164 Ne.clo r (clofail "redir pipe/r");
165 Ne.clo w (clofail "redir pipe/w");
167 | () ->
168 state.stderr <- dupstderr;
169 state.errfd <- Some r;
170 end;
172 else (
173 state.newerrmsgs <- false;
174 begin match state.errfd with
175 | Some fd ->
176 begin match Unix.dup2 state.stderr Unix.stderr with
177 | exception exn ->
178 dolog "failed to dup2 original stderr: %s" (exntos exn)
179 | () ->
180 Ne.clo fd (clofail "dup of stderr");
181 state.errfd <- None;
182 end;
183 | None -> ()
184 end;
185 prerr_string (Buffer.contents state.errmsgs);
186 flush stderr;
187 Buffer.clear state.errmsgs;
191 module G =
192 struct
193 let postRedisplay who =
194 if conf.verbose
195 then prerr_endline ("redisplay for " ^ who);
196 state.redisplay <- true;
198 end;;
200 let getopaque pageno =
201 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
202 with Not_found -> None
205 let putopaque pageno opaque =
206 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
209 let pagetranslatepoint l x y =
210 let dy = y - l.pagedispy in
211 let y = dy + l.pagey in
212 let dx = x - l.pagedispx in
213 let x = dx + l.pagex in
214 (x, y);
217 let onppundermouse g x y d =
218 let rec f = function
219 | l :: rest ->
220 begin match getopaque l.pageno with
221 | Some opaque ->
222 let x0 = l.pagedispx in
223 let x1 = x0 + l.pagevw in
224 let y0 = l.pagedispy in
225 let y1 = y0 + l.pagevh in
226 if y >= y0 && y <= y1 && x >= x0 && x <= x1
227 then
228 let px, py = pagetranslatepoint l x y in
229 match g opaque l px py with
230 | Some res -> res
231 | None -> f rest
232 else f rest
233 | _ ->
234 f rest
236 | [] -> d
238 f state.layout
241 let getunder x y =
242 let g opaque l px py =
243 if state.bzoom
244 then (
245 match rectofblock opaque px py with
246 | Some a ->
247 let rect = (a.(0),a.(2),a.(1),a.(2),a.(1),a.(3),a.(0),a.(3)) in
248 state.rects <- [l.pageno, l.pageno mod 3, rect];
249 G.postRedisplay "getunder";
250 | None -> ()
252 let under = whatsunder opaque px py in
253 if under = Unone then None else Some under
255 onppundermouse g x y Unone
258 let unproject x y =
259 let g opaque l x y =
260 match unproject opaque x y with
261 | Some (x, y) -> Some (Some (opaque, l.pageno, x, y))
262 | None -> None
264 onppundermouse g x y None;
267 let showtext c s =
268 state.text <- Printf.sprintf "%c%s" c s;
269 G.postRedisplay "showtext";
272 let pipesel opaque cmd =
273 if hassel opaque
274 then
275 match Unix.pipe () with
276 | exception exn ->
277 showtext '!'
278 (Printf.sprintf "pipesel can not create pipe: %s" (exntos exn));
279 | (r, w) ->
280 let doclose what fd =
281 Ne.clo fd (fun msg -> dolog "%s close failed: %s" what msg)
283 let pid =
284 try popen cmd [r, 0; w, -1]
285 with exn ->
286 dolog "can not execute %S: %s" cmd (exntos exn);
289 if pid > 0
290 then (
291 copysel w opaque;
292 G.postRedisplay "pipesel";
294 else doclose "pipesel pipe/w" w;
295 doclose "pipesel pipe/r" r;
298 let paxunder x y =
299 let g opaque l px py =
300 if markunder opaque px py conf.paxmark
301 then (
302 Some (fun () ->
303 match getopaque l.pageno with
304 | None -> ()
305 | Some opaque -> pipesel opaque conf.paxcmd
308 else None
310 G.postRedisplay "paxunder";
311 if conf.paxmark = Mark_page
312 then
313 List.iter (fun l ->
314 match getopaque l.pageno with
315 | None -> ()
316 | Some opaque -> clearmark opaque) state.layout;
317 state.roam <-
318 onppundermouse g x y (fun () -> showtext '!' "Whoopsie daisy");
321 let selstring s =
322 match Unix.pipe () with
323 | exception exn ->
324 showtext '!' (Printf.sprintf "pipe failed: %s" (exntos exn))
325 | (r, w) ->
326 let clo cap fd =
327 Ne.clo fd (fun msg ->
328 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
331 let pid =
332 try popen conf.selcmd [r, 0; w, -1]
333 with exn ->
334 showtext '!'
335 (Printf.sprintf "failed to execute %s: %s"
336 conf.selcmd (exntos exn));
339 if pid > 0
340 then (
342 let l = String.length s in
343 let bytes = Bytes.unsafe_of_string s in
344 let n = tempfailureretry (Unix.write w bytes 0) l in
345 if n != l
346 then
347 showtext '!'
348 (Printf.sprintf
349 "failed to write %d characters to sel pipe, wrote %d"
352 with exn ->
353 showtext '!'
354 (Printf.sprintf "failed to write to sel pipe: %s"
355 (exntos exn)
358 else dolog "%s" s;
359 clo "selstring pipe/r" r;
360 clo "selstring pipe/w" w;
363 let undertext ?(nopath=false) = function
364 | Unone -> "none"
365 | Ulinkuri s -> s
366 | Ulinkgoto (pageno, _) ->
367 if nopath
368 then "page " ^ string_of_int (pageno+1)
369 else 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 emptystr password
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 linknact f s =
2011 if nonemptystr s
2012 then (
2013 let n =
2014 let l = String.length s in
2015 let rec loop pos n = if pos = l then n else
2016 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2017 loop (pos+1) (n*26 + m)
2018 in loop 0 0
2020 let rec loop n = function
2021 | [] -> ()
2022 | l :: rest ->
2023 match getopaque l.pageno with
2024 | None -> loop n rest
2025 | Some opaque ->
2026 let m = getlinkcount opaque in
2027 if n < m
2028 then (
2029 let under = getlink opaque n in
2030 f under
2032 else loop (n-m) rest
2034 loop n state.layout;
2038 let linknentry text key =
2039 let c =
2040 if key >= 32 && key < 127
2041 then Char.chr key
2042 else '\000'
2044 match c with
2045 | 'a' .. 'z' ->
2046 let text = addchar text c in
2047 linknact (fun under -> state.text <- undertext ~nopath:true under) text;
2048 TEcont text
2050 | _ ->
2051 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2052 TEcont text
2055 let textentry text key =
2056 if key land 0xff00 = 0xff00
2057 then TEcont text
2058 else TEcont (text ^ toutf8 key)
2061 let reqlayout angle fitmodel =
2062 match state.throttle with
2063 | None ->
2064 if nogeomcmds state.geomcmds
2065 then state.anchor <- getanchor ();
2066 conf.angle <- angle mod 360;
2067 if conf.angle != 0
2068 then (
2069 match state.mode with
2070 | LinkNav _ -> state.mode <- View
2071 | Birdseye _
2072 | Textentry _
2073 | View -> ()
2075 conf.fitmodel <- fitmodel;
2076 invalidate "reqlayout"
2077 (fun () ->
2078 wcmd "reqlayout %d %d %d"
2079 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
2081 | _ -> ()
2084 let settrim trimmargins trimfuzz =
2085 if nogeomcmds state.geomcmds
2086 then state.anchor <- getanchor ();
2087 conf.trimmargins <- trimmargins;
2088 conf.trimfuzz <- trimfuzz;
2089 let x0, y0, x1, y1 = trimfuzz in
2090 invalidate "settrim"
2091 (fun () ->
2092 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2093 flushpages ();
2096 let setzoom zoom =
2097 match state.throttle with
2098 | None ->
2099 let zoom = max 0.0001 zoom in
2100 if zoom <> conf.zoom
2101 then (
2102 state.prevzoom <- (conf.zoom, state.x);
2103 conf.zoom <- zoom;
2104 reshape state.winw state.winh;
2105 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2108 | Some (layout, y, started) ->
2109 let time =
2110 match conf.maxwait with
2111 | None -> 0.0
2112 | Some t -> t
2114 let dt = now () -. started in
2115 if dt > time
2116 then (
2117 state.y <- y;
2118 load layout;
2122 let setcolumns mode columns coverA coverB =
2123 state.prevcolumns <- Some (conf.columns, conf.zoom);
2124 if columns < 0
2125 then (
2126 if isbirdseye mode
2127 then showtext '!' "split mode doesn't work in bird's eye"
2128 else (
2129 conf.columns <- Csplit (-columns, E.a);
2130 state.x <- 0;
2131 conf.zoom <- 1.0;
2134 else (
2135 if columns < 2
2136 then (
2137 conf.columns <- Csingle E.a;
2138 state.x <- 0;
2139 setzoom 1.0;
2141 else (
2142 conf.columns <- Cmulti ((columns, coverA, coverB), E.a);
2143 conf.zoom <- 1.0;
2146 reshape state.winw state.winh;
2149 let resetmstate () =
2150 state.mstate <- Mnone;
2151 Wsi.setcursor Wsi.CURSOR_INHERIT;
2154 let enterbirdseye () =
2155 let zoom = float conf.thumbw /. float state.winw in
2156 let birdseyepageno =
2157 let cy = state.winh / 2 in
2158 let fold = function
2159 | [] -> 0
2160 | l :: rest ->
2161 let rec fold best = function
2162 | [] -> best.pageno
2163 | l :: rest ->
2164 let d = cy - (l.pagedispy + l.pagevh/2)
2165 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2166 if abs d < abs dbest
2167 then fold l rest
2168 else best.pageno
2169 in fold l rest
2171 fold state.layout
2173 state.mode <- Birdseye (
2174 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2176 resetmstate ();
2177 conf.zoom <- zoom;
2178 conf.presentation <- false;
2179 conf.interpagespace <- 10;
2180 conf.hlinks <- false;
2181 conf.fitmodel <- FitPage;
2182 state.x <- 0;
2183 conf.maxwait <- None;
2184 conf.columns <- (
2185 match conf.beyecolumns with
2186 | Some c ->
2187 conf.zoom <- 1.0;
2188 Cmulti ((c, 0, 0), E.a)
2189 | None -> Csingle E.a
2191 if conf.verbose
2192 then
2193 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2194 (100.0*.zoom)
2195 else
2196 state.text <- E.s
2198 reshape state.winw state.winh;
2201 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2202 state.mode <- View;
2203 conf.zoom <- c.zoom;
2204 conf.presentation <- c.presentation;
2205 conf.interpagespace <- c.interpagespace;
2206 conf.maxwait <- c.maxwait;
2207 conf.hlinks <- c.hlinks;
2208 conf.fitmodel <- c.fitmodel;
2209 conf.beyecolumns <- (
2210 match conf.columns with
2211 | Cmulti ((c, _, _), _) -> Some c
2212 | Csingle _ -> None
2213 | Csplit _ -> failwith "leaving bird's eye split mode"
2215 conf.columns <- (
2216 match c.columns with
2217 | Cmulti (c, _) -> Cmulti (c, E.a)
2218 | Csingle _ -> Csingle E.a
2219 | Csplit (c, _) -> Csplit (c, E.a)
2221 if conf.verbose
2222 then
2223 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2224 (100.0*.conf.zoom)
2226 reshape state.winw state.winh;
2227 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2228 state.x <- leftx;
2231 let togglebirdseye () =
2232 match state.mode with
2233 | Birdseye vals -> leavebirdseye vals true
2234 | View -> enterbirdseye ()
2235 | Textentry _
2236 | LinkNav _ -> ()
2239 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2240 let pageno = max 0 (pageno - incr) in
2241 let rec loop = function
2242 | [] -> gotopage1 pageno 0
2243 | l :: _ when l.pageno = pageno ->
2244 if l.pagedispy >= 0 && l.pagey = 0
2245 then G.postRedisplay "upbirdseye"
2246 else gotopage1 pageno 0
2247 | _ :: rest -> loop rest
2249 loop state.layout;
2250 state.text <- E.s;
2251 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2254 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2255 let pageno = min (state.pagecount - 1) (pageno + incr) in
2256 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2257 let rec loop = function
2258 | [] ->
2259 let y, h = getpageyh pageno in
2260 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2261 gotoy (clamp dy)
2262 | l :: _ when l.pageno = pageno ->
2263 if l.pagevh != l.pageh
2264 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2265 else G.postRedisplay "downbirdseye"
2266 | _ :: rest -> loop rest
2268 loop state.layout;
2269 state.text <- E.s;
2272 let optentry mode _ key =
2273 let btos b = if b then "on" else "off" in
2274 if key >= 32 && key < 127
2275 then
2276 let c = Char.chr key in
2277 match c with
2278 | 's' ->
2279 let ondone s =
2280 try conf.scrollstep <- int_of_string s with exc ->
2281 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2283 TEswitch ("scroll step: ", E.s, None, intentry, ondone, true)
2285 | 'A' ->
2286 let ondone s =
2288 conf.autoscrollstep <- boundastep state.winh (int_of_string s);
2289 if state.autoscroll <> None
2290 then state.autoscroll <- Some conf.autoscrollstep
2291 with exc ->
2292 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2294 TEswitch ("auto scroll step: ", E.s, None, intentry, ondone, true)
2296 | 'C' ->
2297 let ondone s =
2299 let n, a, b = multicolumns_of_string s in
2300 setcolumns mode n a b;
2301 with exc ->
2302 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
2304 TEswitch ("columns: ", E.s, None, textentry, ondone, true)
2306 | 'Z' ->
2307 let ondone s =
2309 let zoom = float (int_of_string s) /. 100.0 in
2310 setzoom zoom
2311 with exc ->
2312 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2314 TEswitch ("zoom: ", E.s, None, intentry, ondone, true)
2316 | 't' ->
2317 let ondone s =
2319 conf.thumbw <- bound (int_of_string s) 2 4096;
2320 state.text <-
2321 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2322 begin match mode with
2323 | Birdseye beye ->
2324 leavebirdseye beye false;
2325 enterbirdseye ();
2326 | Textentry _
2327 | View
2328 | LinkNav _ -> ();
2330 with exc ->
2331 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2333 TEswitch ("thumbnail width: ", E.s, None, intentry, ondone, true)
2335 | 'R' ->
2336 let ondone s =
2337 match try
2338 Some (int_of_string s)
2339 with exc ->
2340 state.text <- Printf.sprintf "bad integer `%s': %s"
2341 s (exntos exc);
2342 None
2343 with
2344 | Some angle -> reqlayout angle conf.fitmodel
2345 | None -> ()
2347 TEswitch ("rotation: ", E.s, None, intentry, ondone, true)
2349 | 'i' ->
2350 conf.icase <- not conf.icase;
2351 TEdone ("case insensitive search " ^ (btos conf.icase))
2353 | 'p' ->
2354 conf.preload <- not conf.preload;
2355 gotoy state.y;
2356 TEdone ("preload " ^ (btos conf.preload))
2358 | 'v' ->
2359 conf.verbose <- not conf.verbose;
2360 TEdone ("verbose " ^ (btos conf.verbose))
2362 | 'd' ->
2363 conf.debug <- not conf.debug;
2364 TEdone ("debug " ^ (btos conf.debug))
2366 | 'h' ->
2367 conf.maxhfit <- not conf.maxhfit;
2368 state.maxy <- calcheight ();
2369 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2371 | 'c' ->
2372 conf.crophack <- not conf.crophack;
2373 TEdone ("crophack " ^ btos conf.crophack)
2375 | 'a' ->
2376 let s =
2377 match conf.maxwait with
2378 | None ->
2379 conf.maxwait <- Some infinity;
2380 "always wait for page to complete"
2381 | Some _ ->
2382 conf.maxwait <- None;
2383 "show placeholder if page is not ready"
2385 TEdone s
2387 | 'f' ->
2388 conf.underinfo <- not conf.underinfo;
2389 TEdone ("underinfo " ^ btos conf.underinfo)
2391 | 'P' ->
2392 conf.savebmarks <- not conf.savebmarks;
2393 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2395 | 'S' ->
2396 let ondone s =
2398 let pageno, py =
2399 match state.layout with
2400 | [] -> 0, 0
2401 | l :: _ ->
2402 l.pageno, l.pagey
2404 conf.interpagespace <- int_of_string s;
2405 docolumns conf.columns;
2406 state.maxy <- calcheight ();
2407 let y = getpagey pageno in
2408 gotoy (y + py)
2409 with exc ->
2410 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2412 TEswitch ("vertical margin: ", E.s, None, intentry, ondone, true)
2414 | 'l' ->
2415 let fm =
2416 match conf.fitmodel with
2417 | FitProportional -> FitWidth
2418 | FitWidth | FitPage -> FitProportional
2420 reqlayout conf.angle fm;
2421 TEdone ("proportional display " ^ btos (fm == FitProportional))
2423 | 'T' ->
2424 settrim (not conf.trimmargins) conf.trimfuzz;
2425 TEdone ("trim margins " ^ btos conf.trimmargins)
2427 | 'I' ->
2428 conf.invert <- not conf.invert;
2429 TEdone ("invert colors " ^ btos conf.invert)
2431 | 'x' ->
2432 let ondone s =
2433 cbput state.hists.sel s;
2434 conf.selcmd <- s;
2436 TEswitch ("selection command: ", E.s, Some (onhist state.hists.sel),
2437 textentry, ondone, true)
2439 | 'M' ->
2440 if conf.pax == None
2441 then conf.pax <- Some (ref (0.0, 0, 0))
2442 else conf.pax <- None;
2443 TEdone ("PAX " ^ btos (conf.pax != None))
2445 | _ ->
2446 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2447 TEstop
2448 else
2449 TEcont state.text
2452 class type lvsource = object
2453 method getitemcount : int
2454 method getitem : int -> (string * int)
2455 method hasaction : int -> bool
2456 method exit :
2457 uioh:uioh ->
2458 cancel:bool ->
2459 active:int ->
2460 first:int ->
2461 pan:int ->
2462 uioh option
2463 method getactive : int
2464 method getfirst : int
2465 method getpan : int
2466 method getminfo : (int * int) array
2467 end;;
2469 class virtual lvsourcebase = object
2470 val mutable m_active = 0
2471 val mutable m_first = 0
2472 val mutable m_pan = 0
2473 method getactive = m_active
2474 method getfirst = m_first
2475 method getpan = m_pan
2476 method getminfo : (int * int) array = E.a
2477 end;;
2479 let textentrykeyboard
2480 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2481 state.text <- E.s;
2482 let key =
2483 if key >= 0xffb0 && key <= 0xffb9
2484 then key - 0xffb0 + 48 else key
2486 let enttext te =
2487 state.mode <- Textentry (te, onleave);
2488 enttext ();
2489 G.postRedisplay "textentrykeyboard enttext";
2491 let histaction cmd =
2492 match opthist with
2493 | None -> ()
2494 | Some (action, _) ->
2495 state.mode <- Textentry (
2496 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
2498 G.postRedisplay "textentry histaction"
2500 match key with
2501 | @backspace ->
2502 if emptystr text && cancelonempty
2503 then (
2504 onleave Cancel;
2505 G.postRedisplay "textentrykeyboard after cancel";
2507 else
2508 let s = withoutlastutf8 text in
2509 enttext (c, s, opthist, onkey, ondone, cancelonempty)
2511 | @enter | @kpenter ->
2512 ondone text;
2513 onleave Confirm;
2514 G.postRedisplay "textentrykeyboard after confirm"
2516 | @up | @kpup -> histaction HCprev
2517 | @down | @kpdown -> histaction HCnext
2518 | @home | @kphome -> histaction HCfirst
2519 | @jend | @kpend -> histaction HClast
2521 | @escape ->
2522 if emptystr text
2523 then (
2524 begin match opthist with
2525 | None -> ()
2526 | Some (_, onhistcancel) -> onhistcancel ()
2527 end;
2528 onleave Cancel;
2529 state.text <- E.s;
2530 G.postRedisplay "textentrykeyboard after cancel2"
2532 else (
2533 enttext (c, E.s, opthist, onkey, ondone, cancelonempty)
2536 | @delete | @kpdelete -> ()
2538 | _ when key != 0
2539 && key land 0xff00 != 0xff00 (* keyboard *)
2540 && key land 0xfe00 != 0xfe00 (* xkb *)
2541 && key land 0xfd00 != 0xfd00 (* 3270 *)
2543 begin match onkey text key with
2544 | TEdone text ->
2545 ondone text;
2546 onleave Confirm;
2547 G.postRedisplay "textentrykeyboard after confirm2";
2549 | TEcont text ->
2550 enttext (c, text, opthist, onkey, ondone, cancelonempty);
2552 | TEstop ->
2553 onleave Cancel;
2554 G.postRedisplay "textentrykeyboard after cancel3"
2556 | TEswitch te ->
2557 state.mode <- Textentry (te, onleave);
2558 G.postRedisplay "textentrykeyboard switch";
2559 end;
2561 | _ ->
2562 vlog "unhandled key %s" (Wsi.keyname key)
2565 let firstof first active =
2566 if first > active || abs (first - active) > fstate.maxrows - 1
2567 then max 0 (active - (fstate.maxrows/2))
2568 else first
2571 let calcfirst first active =
2572 if active > first
2573 then
2574 let rows = active - first in
2575 if rows > fstate.maxrows then active - fstate.maxrows else first
2576 else active
2579 let scrollph y maxy =
2580 let sh = float (maxy + state.winh) /. float state.winh in
2581 let sh = float state.winh /. sh in
2582 let sh = max sh (float conf.scrollh) in
2584 let percent = float y /. float maxy in
2585 let position = (float state.winh -. sh) *. percent in
2587 let position =
2588 if position +. sh > float state.winh
2589 then float state.winh -. sh
2590 else position
2592 position, sh;
2595 let coe s = (s :> uioh);;
2597 class listview ~zebra ~helpmode ~(source:lvsource) ~trusted ~modehash =
2598 object (self)
2599 val m_pan = source#getpan
2600 val m_first = source#getfirst
2601 val m_active = source#getactive
2602 val m_qsearch = E.s
2603 val m_prev_uioh = state.uioh
2605 method private elemunder y =
2606 if y < 0
2607 then None
2608 else
2609 let n = y / (fstate.fontsize+1) in
2610 if m_first + n < source#getitemcount
2611 then (
2612 if source#hasaction (m_first + n)
2613 then Some (m_first + n)
2614 else None
2616 else None
2618 method display =
2619 Gl.enable `blend;
2620 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
2621 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2622 filledrect 0. 0. (float state.winw) (float state.winh);
2623 GlDraw.color (1., 1., 1.);
2624 Gl.enable `texture_2d;
2625 let fs = fstate.fontsize in
2626 let nfs = fs + 1 in
2627 let hw = (wadjsb () + xadjsb () + state.winw)/3 in
2628 let ww = fstate.wwidth in
2629 let tabw = 17.0*.ww in
2630 let itemcount = source#getitemcount in
2631 let minfo = source#getminfo in
2632 let x0, x1 =
2633 if conf.leftscroll
2634 then float (xadjsb ()), float (state.winw - 1)
2635 else 0.0, float (state.winw - conf.scrollbw - 1)
2637 let xadj = xadjsb () in
2638 let rec loop row =
2639 if (row - m_first) > fstate.maxrows
2640 then ()
2641 else (
2642 if row >= 0 && row < itemcount
2643 then (
2644 let (s, level) = source#getitem row in
2645 let y = (row - m_first) * nfs in
2646 let x =
2647 (if conf.leftscroll then float xadj else 5.0)
2648 +. (float (level + m_pan)) *. ww in
2649 if helpmode
2650 then GlDraw.color
2651 (let c = if row land 1 = 0 then 1.0 else 0.92 in (c,c,c));
2653 if row = m_active
2654 then (
2655 Gl.disable `texture_2d;
2656 let alpha = if source#hasaction row then 0.9 else 0.3 in
2657 GlDraw.color (1., 1., 1.) ~alpha;
2658 linerect (x0 +. 1.) (float (y + 1)) (x1) (float (y + fs + 3));
2659 Gl.enable `texture_2d;
2661 let c =
2662 if zebra && row land 1 = 1
2663 then 0.8
2664 else 1.0
2666 GlDraw.color (c,c,c);
2667 let drawtabularstring s =
2668 let drawstr x s =
2669 let x' = truncate (x0 +. x) in
2670 let pos = nindex s '\000' in
2671 if pos = -1
2672 then drawstring1 fs x' (y+nfs) s
2673 else
2674 let s1 = String.sub s 0 pos
2675 and s2 = String.sub s (pos+1) (String.length s - pos - 1) in
2676 let rec e s =
2677 if emptystr s
2678 then s
2679 else
2680 let s' = withoutlastutf8 s in
2681 let s = s' ^ "@Uellipsis" in
2682 let w = measurestr fs s in
2683 if float x' +. w +. ww < float (hw + x')
2684 then s
2685 else e s'
2687 let s1 =
2688 if float x' +. ww +. measurestr fs s1 > float (hw + x')
2689 then e s1
2690 else s1
2692 ignore (drawstring1 fs x' (y+nfs) s1);
2693 drawstring1 fs (hw + x') (y+nfs) s2
2695 if trusted
2696 then
2697 let x = if helpmode && row > 0 then x +. ww else x in
2698 let tabpos = nindex s '\t' in
2699 if tabpos > 0
2700 then
2701 let len = String.length s - tabpos - 1 in
2702 let s1 = String.sub s 0 tabpos
2703 and s2 = String.sub s (tabpos + 1) len in
2704 let nx = drawstr x s1 in
2705 let sw = nx -. x in
2706 let x = x +. (max tabw sw) in
2707 drawstr x s2
2708 else
2709 let len = String.length s - 2 in
2710 if len > 0 && s.[0] = '\xc2' && s.[1] = '\xb7'
2711 then
2712 let s = String.sub s 2 len in
2713 let x = if not helpmode then x +. ww else x in
2714 GlDraw.color (1.2, 1.2, 1.2);
2715 let vinc = drawstring1 (fs+fs/4)
2716 (truncate (x -. ww)) (y+nfs) s in
2717 GlDraw.color (1., 1., 1.);
2718 vinc +. (float fs *. 0.8)
2719 else
2720 drawstr x s
2721 else
2722 drawstr x s
2724 ignore (drawtabularstring s);
2725 loop (row+1)
2729 loop m_first;
2730 GlDraw.color (1.0, 1.0, 1.0) ~alpha:0.5;
2731 let xadj = float (xadjsb () + 5) in
2732 let rec loop row =
2733 if (row - m_first) > fstate.maxrows
2734 then ()
2735 else (
2736 if row >= 0 && row < itemcount
2737 then (
2738 let (s, level) = source#getitem row in
2739 let pos0 = nindex s '\000' in
2740 let y = (row - m_first) * nfs in
2741 let x = float (level + m_pan) *. ww in
2742 let (first, last) = minfo.(row) in
2743 let prefix =
2744 if pos0 > 0 && first > pos0
2745 then String.sub s (pos0+1) (first-pos0-1)
2746 else String.sub s 0 first
2748 let suffix = String.sub s first (last - first) in
2749 let w1 = measurestr fstate.fontsize prefix in
2750 let w2 = measurestr fstate.fontsize suffix in
2751 let x = x +. if conf.leftscroll then xadj else 5.0 in
2752 let x = if pos0 > 0 && first > pos0 then x +. float hw else x in
2753 let x0 = x +. w1
2754 and y0 = float (y+2) in
2755 let x1 = x0 +. w2
2756 and y1 = float (y+fs+3) in
2757 filledrect x0 y0 x1 y1;
2758 loop (row+1)
2762 Gl.disable `texture_2d;
2763 if Array.length minfo > 0 then loop m_first;
2764 Gl.disable `blend;
2766 method updownlevel incr =
2767 let len = source#getitemcount in
2768 let curlevel =
2769 if m_active >= 0 && m_active < len
2770 then snd (source#getitem m_active)
2771 else -1
2773 let rec flow i =
2774 if i = len then i-1 else if i = -1 then 0 else
2775 let _, l = source#getitem i in
2776 if l != curlevel then i else flow (i+incr)
2778 let active = flow m_active in
2779 let first = calcfirst m_first active in
2780 G.postRedisplay "outline updownlevel";
2781 {< m_active = active; m_first = first >}
2783 method private key1 key mask =
2784 let set1 active first qsearch =
2785 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2787 let search active pattern incr =
2788 let active = if active = -1 then m_first else active in
2789 let dosearch re =
2790 let rec loop n =
2791 if n >= 0 && n < source#getitemcount
2792 then (
2793 let s, _ = source#getitem n in
2795 (try ignore (Str.search_forward re s 0); true
2796 with Not_found -> false)
2797 then Some n
2798 else loop (n + incr)
2800 else None
2802 loop active
2805 let re = Str.regexp_case_fold pattern in
2806 dosearch re
2807 with Failure s ->
2808 state.text <- s;
2809 None
2811 let itemcount = source#getitemcount in
2812 let find start incr =
2813 let rec find i =
2814 if i = -1 || i = itemcount
2815 then -1
2816 else (
2817 if source#hasaction i
2818 then i
2819 else find (i + incr)
2822 find start
2824 let set active first =
2825 let first = bound first 0 (itemcount - fstate.maxrows) in
2826 state.text <- E.s;
2827 coe {< m_active = active; m_first = first; m_qsearch = E.s >}
2829 let navigate incr =
2830 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2831 let active, first =
2832 let incr1 = if incr > 0 then 1 else -1 in
2833 if isvisible m_first m_active
2834 then
2835 let next =
2836 let next = m_active + incr in
2837 let next =
2838 if next < 0 || next >= itemcount
2839 then -1
2840 else find next incr1
2842 if abs (m_active - next) > fstate.maxrows
2843 then -1
2844 else next
2846 if next = -1
2847 then
2848 let first = m_first + incr in
2849 let first = bound first 0 (itemcount - fstate.maxrows) in
2850 let next =
2851 let next = m_active + incr in
2852 let next = bound next 0 (itemcount - 1) in
2853 find next ~-incr1
2855 let active =
2856 if next = -1
2857 then m_active
2858 else (
2859 if isvisible first next
2860 then next
2861 else m_active
2864 active, first
2865 else
2866 let first = min next m_first in
2867 let first =
2868 if abs (next - first) > fstate.maxrows
2869 then first + incr
2870 else first
2872 next, first
2873 else
2874 let first = m_first + incr in
2875 let first = bound first 0 (itemcount - 1) in
2876 let active =
2877 let next = m_active + incr in
2878 let next = bound next 0 (itemcount - 1) in
2879 let next = find next incr1 in
2880 let active =
2881 if next = -1 || abs (m_active - first) > fstate.maxrows
2882 then (
2883 let active = if m_active = -1 then next else m_active in
2884 active
2886 else next
2888 if isvisible first active
2889 then active
2890 else -1
2892 active, first
2894 G.postRedisplay "listview navigate";
2895 set active first;
2897 match key with
2898 | (@r|@s) when Wsi.withctrl mask ->
2899 let incr = if key = @r then -1 else 1 in
2900 let active, first =
2901 match search (m_active + incr) m_qsearch incr with
2902 | None ->
2903 state.text <- m_qsearch ^ " [not found]";
2904 m_active, m_first
2905 | Some active ->
2906 state.text <- m_qsearch;
2907 active, firstof m_first active
2909 G.postRedisplay "listview ctrl-r/s";
2910 set1 active first m_qsearch;
2912 | @insert when Wsi.withctrl mask ->
2913 if m_active >= 0 && m_active < source#getitemcount
2914 then (
2915 let s, _ = source#getitem m_active in
2916 selstring s;
2918 coe self
2920 | @backspace ->
2921 if emptystr m_qsearch
2922 then coe self
2923 else (
2924 let qsearch = withoutlastutf8 m_qsearch in
2925 if emptystr qsearch
2926 then (
2927 state.text <- E.s;
2928 G.postRedisplay "listview empty qsearch";
2929 set1 m_active m_first E.s;
2931 else
2932 let active, first =
2933 match search m_active qsearch ~-1 with
2934 | None ->
2935 state.text <- qsearch ^ " [not found]";
2936 m_active, m_first
2937 | Some active ->
2938 state.text <- qsearch;
2939 active, firstof m_first active
2941 G.postRedisplay "listview backspace qsearch";
2942 set1 active first qsearch
2945 | key when (key != 0 && key land 0xff00 != 0xff00) ->
2946 let pattern = m_qsearch ^ toutf8 key in
2947 let active, first =
2948 match search m_active pattern 1 with
2949 | None ->
2950 state.text <- pattern ^ " [not found]";
2951 m_active, m_first
2952 | Some active ->
2953 state.text <- pattern;
2954 active, firstof m_first active
2956 G.postRedisplay "listview qsearch add";
2957 set1 active first pattern;
2959 | @escape ->
2960 state.text <- E.s;
2961 if emptystr m_qsearch
2962 then (
2963 G.postRedisplay "list view escape";
2964 let mx, my = state.mpos in
2965 updateunder mx my;
2966 begin
2967 match
2968 source#exit ~uioh:(coe self)
2969 ~cancel:true ~active:m_active ~first:m_first ~pan:m_pan
2970 with
2971 | None -> m_prev_uioh
2972 | Some uioh -> uioh
2975 else (
2976 G.postRedisplay "list view kill qsearch";
2977 coe {< m_qsearch = E.s >}
2980 | @enter | @kpenter ->
2981 state.text <- E.s;
2982 let self = {< m_qsearch = E.s >} in
2983 let opt =
2984 G.postRedisplay "listview enter";
2985 if m_active >= 0 && m_active < source#getitemcount
2986 then (
2987 source#exit ~uioh:(coe self) ~cancel:false
2988 ~active:m_active ~first:m_first ~pan:m_pan;
2990 else (
2991 source#exit ~uioh:(coe self) ~cancel:true
2992 ~active:m_active ~first:m_first ~pan:m_pan;
2995 begin match opt with
2996 | None -> m_prev_uioh
2997 | Some uioh -> uioh
3000 | @delete | @kpdelete ->
3001 coe self
3003 | @up | @kpup -> navigate ~-1
3004 | @down | @kpdown -> navigate 1
3005 | @prior | @kpprior -> navigate ~-(fstate.maxrows)
3006 | @next | @kpnext -> navigate fstate.maxrows
3008 | @right | @kpright ->
3009 state.text <- E.s;
3010 G.postRedisplay "listview right";
3011 coe {< m_pan = m_pan - 1 >}
3013 | @left | @kpleft ->
3014 state.text <- E.s;
3015 G.postRedisplay "listview left";
3016 coe {< m_pan = m_pan + 1 >}
3018 | @home | @kphome ->
3019 let active = find 0 1 in
3020 G.postRedisplay "listview home";
3021 set active 0;
3023 | @jend | @kpend ->
3024 let first = max 0 (itemcount - fstate.maxrows) in
3025 let active = find (itemcount - 1) ~-1 in
3026 G.postRedisplay "listview end";
3027 set active first;
3029 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3030 coe self
3032 | _ ->
3033 dolog "listview unknown key %#x" key; coe self
3035 method key key mask =
3036 match state.mode with
3037 | Textentry te -> textentrykeyboard key mask te; coe self
3038 | Birdseye _
3039 | View
3040 | LinkNav _ -> self#key1 key mask
3042 method button button down x y _ =
3043 let opt =
3044 match button with
3045 | 1 when x > state.winw - conf.scrollbw ->
3046 G.postRedisplay "listview scroll";
3047 if down
3048 then
3049 let _, position, sh = self#scrollph in
3050 if y > truncate position && y < truncate (position +. sh)
3051 then (
3052 state.mstate <- Mscrolly;
3053 Some (coe self)
3055 else
3056 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3057 let first = truncate (s *. float source#getitemcount) in
3058 let first = min source#getitemcount first in
3059 Some (coe {< m_first = first; m_active = first >})
3060 else (
3061 state.mstate <- Mnone;
3062 Some (coe self);
3064 | 1 when down ->
3065 begin match self#elemunder y with
3066 | Some n ->
3067 G.postRedisplay "listview click";
3068 source#exit ~uioh:(coe {< m_active = n >})
3069 ~cancel:false ~active:n ~first:m_first ~pan:m_pan
3070 | _ ->
3071 Some (coe self)
3073 | n when (n == 4 || n == 5) && not down ->
3074 let len = source#getitemcount in
3075 let first =
3076 if n = 5 && m_first + fstate.maxrows >= len
3077 then
3078 m_first
3079 else
3080 let first = m_first + (if n == 4 then -1 else 1) in
3081 bound first 0 (len - 1)
3083 G.postRedisplay "listview wheel";
3084 Some (coe {< m_first = first >})
3085 | n when (n = 6 || n = 7) && not down ->
3086 let inc = if n = 7 then -1 else 1 in
3087 G.postRedisplay "listview hwheel";
3088 Some (coe {< m_pan = m_pan + inc >})
3089 | _ ->
3090 Some (coe self)
3092 match opt with
3093 | None -> m_prev_uioh
3094 | Some uioh -> uioh
3096 method multiclick _ x y = self#button 1 true x y
3098 method motion _ y =
3099 match state.mstate with
3100 | Mscrolly ->
3101 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3102 let first = truncate (s *. float source#getitemcount) in
3103 let first = min source#getitemcount first in
3104 G.postRedisplay "listview motion";
3105 coe {< m_first = first; m_active = first >}
3106 | Msel _
3107 | Mpan _
3108 | Mscrollx
3109 | Mzoom _
3110 | Mzoomrect _
3111 | Mnone -> coe self
3113 method pmotion x y =
3114 if x < state.winw - conf.scrollbw
3115 then
3116 let n =
3117 match self#elemunder y with
3118 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3119 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3121 let o =
3122 if n != m_active
3123 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3124 else self
3126 coe o
3127 else (
3128 Wsi.setcursor Wsi.CURSOR_INHERIT;
3129 coe self
3132 method infochanged _ = ()
3134 method scrollpw = (0, 0.0, 0.0)
3135 method scrollph =
3136 let nfs = fstate.fontsize + 1 in
3137 let y = m_first * nfs in
3138 let itemcount = source#getitemcount in
3139 let maxi = max 0 (itemcount - fstate.maxrows) in
3140 let maxy = maxi * nfs in
3141 let p, h = scrollph y maxy in
3142 conf.scrollbw, p, h
3144 method modehash = modehash
3145 method eformsgs = false
3146 method alwaysscrolly = true
3147 end;;
3149 class outlinelistview ~zebra ~source =
3150 let settext autonarrow s =
3151 if autonarrow
3152 then
3153 let ss = source#statestr in
3154 state.text <-
3155 if emptystr ss
3156 then "[" ^ s ^ "]"
3157 else "{" ^ ss ^ "} [" ^ s ^ "]"
3158 else state.text <- s
3160 object (self)
3161 inherit listview
3162 ~zebra
3163 ~helpmode:false
3164 ~source:(source :> lvsource)
3165 ~trusted:false
3166 ~modehash:(findkeyhash conf "outline")
3167 as super
3169 val m_autonarrow = false
3171 method! key key mask =
3172 let maxrows =
3173 if emptystr state.text
3174 then fstate.maxrows
3175 else fstate.maxrows - 2
3177 let calcfirst first active =
3178 if active > first
3179 then
3180 let rows = active - first in
3181 if rows > maxrows then active - maxrows else first
3182 else active
3184 let navigate incr =
3185 let active = m_active + incr in
3186 let active = bound active 0 (source#getitemcount - 1) in
3187 let first = calcfirst m_first active in
3188 G.postRedisplay "outline navigate";
3189 coe {< m_active = active; m_first = first >}
3191 let navscroll first =
3192 let active =
3193 let dist = m_active - first in
3194 if dist < 0
3195 then first
3196 else (
3197 if dist < maxrows
3198 then m_active
3199 else first + maxrows
3202 G.postRedisplay "outline navscroll";
3203 coe {< m_first = first; m_active = active >}
3205 let ctrl = Wsi.withctrl mask in
3206 match key with
3207 | @a when ctrl ->
3208 let text =
3209 if m_autonarrow
3210 then (source#denarrow; E.s)
3211 else (
3212 let pattern = source#renarrow in
3213 if nonemptystr m_qsearch
3214 then (source#narrow m_qsearch; m_qsearch)
3215 else pattern
3218 settext (not m_autonarrow) text;
3219 G.postRedisplay "toggle auto narrowing";
3220 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
3222 | @slash when emptystr m_qsearch && not m_autonarrow ->
3223 settext true E.s;
3224 G.postRedisplay "toggle auto narrowing";
3225 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
3227 | @n when ctrl ->
3228 source#narrow m_qsearch;
3229 if not m_autonarrow
3230 then source#add_narrow_pattern m_qsearch;
3231 G.postRedisplay "outline ctrl-n";
3232 coe {< m_first = 0; m_active = 0 >}
3234 | @S when ctrl ->
3235 let active = source#calcactive (getanchor ()) in
3236 let first = firstof m_first active in
3237 G.postRedisplay "outline ctrl-s";
3238 coe {< m_first = first; m_active = active >}
3240 | @u when ctrl ->
3241 G.postRedisplay "outline ctrl-u";
3242 if m_autonarrow && nonemptystr m_qsearch
3243 then (
3244 ignore (source#renarrow);
3245 settext m_autonarrow E.s;
3246 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3248 else (
3249 source#del_narrow_pattern;
3250 let pattern = source#renarrow in
3251 let text =
3252 if emptystr pattern then E.s else "Narrowed to " ^ pattern
3254 settext m_autonarrow text;
3255 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3258 | @l when ctrl ->
3259 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3260 G.postRedisplay "outline ctrl-l";
3261 coe {< m_first = first >}
3263 | @tab when m_autonarrow ->
3264 if nonemptystr m_qsearch
3265 then (
3266 G.postRedisplay "outline list view tab";
3267 source#add_narrow_pattern m_qsearch;
3268 settext true E.s;
3269 coe {< m_qsearch = E.s >}
3271 else coe self
3273 | @escape when m_autonarrow ->
3274 if nonemptystr m_qsearch
3275 then source#add_narrow_pattern m_qsearch;
3276 super#key key mask
3278 | @enter | @kpenter when m_autonarrow ->
3279 if nonemptystr m_qsearch
3280 then source#add_narrow_pattern m_qsearch;
3281 super#key key mask
3283 | key when m_autonarrow && (key != 0 && key land 0xff00 != 0xff00) ->
3284 let pattern = m_qsearch ^ toutf8 key in
3285 G.postRedisplay "outlinelistview autonarrow add";
3286 source#narrow pattern;
3287 settext true pattern;
3288 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3290 | key when m_autonarrow && key = @backspace ->
3291 if emptystr m_qsearch
3292 then coe self
3293 else
3294 let pattern = withoutlastutf8 m_qsearch in
3295 G.postRedisplay "outlinelistview autonarrow backspace";
3296 ignore (source#renarrow);
3297 source#narrow pattern;
3298 settext true pattern;
3299 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3301 | @delete | @kpdelete ->
3302 source#remove m_active;
3303 G.postRedisplay "outline delete";
3304 let active = max 0 (m_active-1) in
3305 coe {< m_first = firstof m_first active;
3306 m_active = active >}
3308 | @up | @kpup when ctrl ->
3309 navscroll (max 0 (m_first - 1))
3311 | @down | @kpdown when ctrl ->
3312 navscroll (min (source#getitemcount - 1) (m_first + 1))
3314 | @up | @kpup -> navigate ~-1
3315 | @down | @kpdown -> navigate 1
3316 | @prior | @kpprior -> navigate ~-(fstate.maxrows)
3317 | @next | @kpnext -> navigate fstate.maxrows
3319 | @right | @kpright ->
3320 let o =
3321 if ctrl
3322 then (
3323 G.postRedisplay "outline ctrl right";
3324 {< m_pan = m_pan + 1 >}
3326 else self#updownlevel 1
3328 coe o
3330 | @left | @kpleft ->
3331 let o =
3332 if ctrl
3333 then (
3334 G.postRedisplay "outline ctrl left";
3335 {< m_pan = m_pan - 1 >}
3337 else self#updownlevel ~-1
3339 coe o
3341 | @home | @kphome ->
3342 G.postRedisplay "outline home";
3343 coe {< m_first = 0; m_active = 0 >}
3345 | @jend | @kpend ->
3346 let active = source#getitemcount - 1 in
3347 let first = max 0 (active - fstate.maxrows) in
3348 G.postRedisplay "outline end";
3349 coe {< m_active = active; m_first = first >}
3351 | _ -> super#key key mask
3354 let genhistoutlines =
3355 let order ty (p1, c1, _, _, _, _) (p2, c2, _, _, _, _) =
3356 match ty with
3357 | `lastvisit -> compare c1.lastvisit c2.lastvisit
3358 | `path -> compare p2 p1
3359 | `file -> compare (Filename.basename p2) (Filename.basename p1)
3360 | `title ->
3361 let e1 = emptystr c1.title
3362 and e2 = emptystr c2.title in
3363 (**) if e1 && e2
3364 then compare (Filename.basename p2) (Filename.basename p1)
3365 else if e1 then -1
3366 else if e2 then 1
3367 else compare c1.title c2.title
3369 let showfullpath = ref false in
3370 let showorigin = ref true in
3371 let orderty : historder ref = ref `lastvisit in
3372 let rec f () =
3373 let setorty s t =
3374 let s = if !orderty = t then "[@Uradical] " ^ s else "[ ] " ^ s in
3375 s, 0, Oreaction (fun () -> orderty := t; f ())
3377 match Config.gethist () with
3378 | [] -> E.a
3379 | list ->
3380 let ol =
3381 List.fold_left
3382 (fun accu (path, c, b, x, a, o) ->
3383 let hist = (path, (c, b, x, a, o)) in
3384 let s =
3385 let s = if nonemptystr o && !showorigin then o else path in
3386 if !showfullpath then s else Filename.basename s
3388 let base = mbtoutf8 s in
3389 (base ^ "\000" ^ c.title, 1, Ohistory hist) :: accu
3391 [ setorty "Sort by time of last visit" `lastvisit;
3392 setorty "Sort by file name" `file;
3393 setorty "Sort by path" `path;
3394 setorty "Sort by title" `title;
3395 (if !showfullpath then "@Uradical "
3396 else " ") ^ "Show full path", 0, Oreaction (fun () ->
3397 showfullpath := not !showfullpath;
3398 f ());
3399 (if !showorigin then "@Uradical "
3400 else " ") ^ "Show origin", 0, Oreaction (fun () ->
3401 showorigin := not !showorigin;
3402 f ())
3403 ] (List.sort (order !orderty) list)
3405 Array.of_list ol
3406 in f
3409 let gotohist (path, (c, bookmarks, x, anchor, origin)) =
3410 Config.save leavebirdseye;
3411 state.anchor <- anchor;
3412 state.bookmarks <- bookmarks;
3413 state.origin <- origin;
3414 state.x <- x;
3415 setconf conf c;
3416 let x0, y0, x1, y1 = conf.trimfuzz in
3417 wcmd "trimset %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1;
3418 reshape ~firsttime:true state.winw state.winh;
3419 opendoc path origin;
3420 setzoom c.zoom;
3423 let makecheckers () =
3424 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3425 following to say:
3426 converted by Issac Trotts. July 25, 2002 *)
3427 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3428 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3429 let id = GlTex.gen_texture () in
3430 GlTex.bind_texture ~target:`texture_2d id;
3431 GlPix.store (`unpack_alignment 1);
3432 GlTex.image2d image;
3433 List.iter (GlTex.parameter ~target:`texture_2d)
3434 [ `mag_filter `nearest; `min_filter `nearest ];
3438 let setcheckers enabled =
3439 match state.checkerstexid with
3440 | None ->
3441 if enabled then state.checkerstexid <- Some (makecheckers ())
3443 | Some checkerstexid ->
3444 if not enabled
3445 then (
3446 GlTex.delete_texture checkerstexid;
3447 state.checkerstexid <- None;
3451 let describe_location () =
3452 let fn = page_of_y state.y in
3453 let ln = page_of_y (state.y + state.winh - hscrollh () - 1) in
3454 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
3455 let percent =
3456 if maxy <= 0
3457 then 100.
3458 else (100. *. (float state.y /. float maxy))
3460 if fn = ln
3461 then
3462 Printf.sprintf "page %d of %d [%.2f%%]"
3463 (fn+1) state.pagecount percent
3464 else
3465 Printf.sprintf
3466 "pages %d-%d of %d [%.2f%%]"
3467 (fn+1) (ln+1) state.pagecount percent
3470 let setpresentationmode v =
3471 let n = page_of_y state.y in
3472 state.anchor <- (n, 0.0, 1.0);
3473 conf.presentation <- v;
3474 if conf.fitmodel = FitPage
3475 then reqlayout conf.angle conf.fitmodel;
3476 represent ();
3479 let enterinfomode =
3480 let btos b = if b then "@Uradical" else E.s in
3481 let showextended = ref false in
3482 let leave mode _ = state.mode <- mode in
3483 let src =
3484 (object
3485 val mutable m_first_time = true
3486 val mutable m_l = []
3487 val mutable m_a = E.a
3488 val mutable m_prev_uioh = nouioh
3489 val mutable m_prev_mode = View
3491 inherit lvsourcebase
3493 method reset prev_mode prev_uioh =
3494 m_a <- Array.of_list (List.rev m_l);
3495 m_l <- [];
3496 m_prev_mode <- prev_mode;
3497 m_prev_uioh <- prev_uioh;
3498 if m_first_time
3499 then (
3500 let rec loop n =
3501 if n >= Array.length m_a
3502 then ()
3503 else
3504 match m_a.(n) with
3505 | _, _, _, Action _ -> m_active <- n
3506 | _, _, _, Noaction -> loop (n+1)
3508 loop 0;
3509 m_first_time <- false;
3512 method int name get set =
3513 m_l <-
3514 (name, `int get, 1, Action (
3515 fun u ->
3516 let ondone s =
3517 try set (int_of_string s)
3518 with exn ->
3519 state.text <- Printf.sprintf "bad integer `%s': %s"
3520 s (exntos exn)
3522 state.text <- E.s;
3523 let te = name ^ ": ", E.s, None, intentry, ondone, true in
3524 state.mode <- Textentry (te, leave m_prev_mode);
3526 )) :: m_l
3528 method int_with_suffix name get set =
3529 m_l <-
3530 (name, `intws get, 1, Action (
3531 fun u ->
3532 let ondone s =
3533 try set (int_of_string_with_suffix s)
3534 with exn ->
3535 state.text <- Printf.sprintf "bad integer `%s': %s"
3536 s (exntos exn)
3538 state.text <- E.s;
3539 let te =
3540 name ^ ": ", E.s, None, intentry_with_suffix, ondone, true
3542 state.mode <- Textentry (te, leave m_prev_mode);
3544 )) :: m_l
3546 method bool ?(offset=1) ?(btos=btos) name get set =
3547 m_l <-
3548 (name, `bool (btos, get), offset, Action (
3549 fun u ->
3550 let v = get () in
3551 set (not v);
3553 )) :: m_l
3555 method color name get set =
3556 m_l <-
3557 (name, `color get, 1, Action (
3558 fun u ->
3559 let invalid = (nan, nan, nan) in
3560 let ondone s =
3561 let c =
3562 try color_of_string s
3563 with exn ->
3564 state.text <- Printf.sprintf "bad color `%s': %s"
3565 s (exntos exn);
3566 invalid
3568 if c <> invalid
3569 then set c;
3571 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3572 state.text <- color_to_string (get ());
3573 state.mode <- Textentry (te, leave m_prev_mode);
3575 )) :: m_l
3577 method string name get set =
3578 m_l <-
3579 (name, `string get, 1, Action (
3580 fun u ->
3581 let ondone s = set s in
3582 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3583 state.mode <- Textentry (te, leave m_prev_mode);
3585 )) :: m_l
3587 method colorspace name get set =
3588 m_l <-
3589 (name, `string get, 1, Action (
3590 fun _ ->
3591 let source =
3592 (object
3593 inherit lvsourcebase
3595 initializer
3596 m_active <- CSTE.to_int conf.colorspace;
3597 m_first <- 0;
3599 method getitemcount =
3600 Array.length CSTE.names
3601 method getitem n =
3602 (CSTE.names.(n), 0)
3603 method exit ~uioh ~cancel ~active ~first ~pan =
3604 ignore (uioh, first, pan);
3605 if not cancel then set active;
3606 None
3607 method hasaction _ = true
3608 end)
3610 state.text <- E.s;
3611 let modehash = findkeyhash conf "info" in
3612 coe (new listview ~zebra:false ~helpmode:false
3613 ~source ~trusted:true ~modehash)
3614 )) :: m_l
3616 method paxmark name get set =
3617 m_l <-
3618 (name, `string get, 1, Action (
3619 fun _ ->
3620 let source =
3621 (object
3622 inherit lvsourcebase
3624 initializer
3625 m_active <- MTE.to_int conf.paxmark;
3626 m_first <- 0;
3628 method getitemcount = Array.length MTE.names
3629 method getitem n = (MTE.names.(n), 0)
3630 method exit ~uioh ~cancel ~active ~first ~pan =
3631 ignore (uioh, first, pan);
3632 if not cancel then set active;
3633 None
3634 method hasaction _ = true
3635 end)
3637 state.text <- E.s;
3638 let modehash = findkeyhash conf "info" in
3639 coe (new listview ~zebra:false ~helpmode:false
3640 ~source ~trusted:true ~modehash)
3641 )) :: m_l
3643 method fitmodel name get set =
3644 m_l <-
3645 (name, `string get, 1, Action (
3646 fun _ ->
3647 let source =
3648 (object
3649 inherit lvsourcebase
3651 initializer
3652 m_active <- FMTE.to_int conf.fitmodel;
3653 m_first <- 0;
3655 method getitemcount = Array.length FMTE.names
3656 method getitem n = (FMTE.names.(n), 0)
3657 method exit ~uioh ~cancel ~active ~first ~pan =
3658 ignore (uioh, first, pan);
3659 if not cancel then set active;
3660 None
3661 method hasaction _ = true
3662 end)
3664 state.text <- E.s;
3665 let modehash = findkeyhash conf "info" in
3666 coe (new listview ~zebra:false ~helpmode:false
3667 ~source ~trusted:true ~modehash)
3668 )) :: m_l
3670 method caption s offset =
3671 m_l <- (s, `empty, offset, Noaction) :: m_l
3673 method caption2 s f offset =
3674 m_l <- (s, `string f, offset, Noaction) :: m_l
3676 method getitemcount = Array.length m_a
3678 method getitem n =
3679 let tostr = function
3680 | `int f -> string_of_int (f ())
3681 | `intws f -> string_with_suffix_of_int (f ())
3682 | `string f -> f ()
3683 | `color f -> color_to_string (f ())
3684 | `bool (btos, f) -> btos (f ())
3685 | `empty -> E.s
3687 let name, t, offset, _ = m_a.(n) in
3688 ((let s = tostr t in
3689 if nonemptystr s
3690 then Printf.sprintf "%s\t%s" name s
3691 else name),
3692 offset)
3694 method exit ~uioh ~cancel ~active ~first ~pan =
3695 let uiohopt =
3696 if not cancel
3697 then (
3698 let uioh =
3699 match m_a.(active) with
3700 | _, _, _, Action f -> f uioh
3701 | _, _, _, Noaction -> uioh
3703 Some uioh
3705 else None
3707 m_active <- active;
3708 m_first <- first;
3709 m_pan <- pan;
3710 uiohopt
3712 method hasaction n =
3713 match m_a.(n) with
3714 | _, _, _, Action _ -> true
3715 | _, _, _, Noaction -> false
3716 end)
3718 let rec fillsrc prevmode prevuioh =
3719 let sep () = src#caption E.s 0 in
3720 let colorp name get set =
3721 src#string name
3722 (fun () -> color_to_string (get ()))
3723 (fun v ->
3725 let c = color_of_string v in
3726 set c
3727 with exn ->
3728 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
3731 let oldmode = state.mode in
3732 let birdseye = isbirdseye state.mode in
3734 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3736 src#bool "presentation mode"
3737 (fun () -> conf.presentation)
3738 (fun v -> setpresentationmode v);
3740 src#bool "ignore case in searches"
3741 (fun () -> conf.icase)
3742 (fun v -> conf.icase <- v);
3744 src#bool "preload"
3745 (fun () -> conf.preload)
3746 (fun v -> conf.preload <- v);
3748 src#bool "highlight links"
3749 (fun () -> conf.hlinks)
3750 (fun v -> conf.hlinks <- v);
3752 src#bool "under info"
3753 (fun () -> conf.underinfo)
3754 (fun v -> conf.underinfo <- v);
3756 src#bool "persistent bookmarks"
3757 (fun () -> conf.savebmarks)
3758 (fun v -> conf.savebmarks <- v);
3760 src#fitmodel "fit model"
3761 (fun () -> FMTE.to_string conf.fitmodel)
3762 (fun v -> reqlayout conf.angle (FMTE.of_int v));
3764 src#bool "trim margins"
3765 (fun () -> conf.trimmargins)
3766 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3768 src#bool "persistent location"
3769 (fun () -> conf.jumpback)
3770 (fun v -> conf.jumpback <- v);
3772 sep ();
3773 src#int "inter-page space"
3774 (fun () -> conf.interpagespace)
3775 (fun n ->
3776 conf.interpagespace <- n;
3777 docolumns conf.columns;
3778 let pageno, py =
3779 match state.layout with
3780 | [] -> 0, 0
3781 | l :: _ ->
3782 l.pageno, l.pagey
3784 state.maxy <- calcheight ();
3785 let y = getpagey pageno in
3786 gotoy (y + py)
3789 src#int "page bias"
3790 (fun () -> conf.pagebias)
3791 (fun v -> conf.pagebias <- v);
3793 src#int "scroll step"
3794 (fun () -> conf.scrollstep)
3795 (fun n -> conf.scrollstep <- n);
3797 src#int "horizontal scroll step"
3798 (fun () -> conf.hscrollstep)
3799 (fun v -> conf.hscrollstep <- v);
3801 src#int "auto scroll step"
3802 (fun () ->
3803 match state.autoscroll with
3804 | Some step -> step
3805 | _ -> conf.autoscrollstep)
3806 (fun n ->
3807 let n = boundastep state.winh n in
3808 if state.autoscroll <> None
3809 then state.autoscroll <- Some n;
3810 conf.autoscrollstep <- n);
3812 src#int "zoom"
3813 (fun () -> truncate (conf.zoom *. 100.))
3814 (fun v -> setzoom ((float v) /. 100.));
3816 src#int "rotation"
3817 (fun () -> conf.angle)
3818 (fun v -> reqlayout v conf.fitmodel);
3820 src#int "scroll bar width"
3821 (fun () -> conf.scrollbw)
3822 (fun v ->
3823 conf.scrollbw <- v;
3824 reshape state.winw state.winh;
3827 src#int "scroll handle height"
3828 (fun () -> conf.scrollh)
3829 (fun v -> conf.scrollh <- v;);
3831 src#int "thumbnail width"
3832 (fun () -> conf.thumbw)
3833 (fun v ->
3834 conf.thumbw <- min 4096 v;
3835 match oldmode with
3836 | Birdseye beye ->
3837 leavebirdseye beye false;
3838 enterbirdseye ()
3839 | Textentry _
3840 | View
3841 | LinkNav _ -> ()
3844 let mode = state.mode in
3845 src#string "columns"
3846 (fun () ->
3847 match conf.columns with
3848 | Csingle _ -> "1"
3849 | Cmulti (multi, _) -> multicolumns_to_string multi
3850 | Csplit (count, _) -> "-" ^ string_of_int count
3852 (fun v ->
3853 let n, a, b = multicolumns_of_string v in
3854 setcolumns mode n a b);
3856 sep ();
3857 src#caption "Pixmap cache" 0;
3858 src#int_with_suffix "size (advisory)"
3859 (fun () -> conf.memlimit)
3860 (fun v -> conf.memlimit <- v);
3862 src#caption2 "used"
3863 (fun () -> Printf.sprintf "%s bytes, %d tiles"
3864 (string_with_suffix_of_int state.memused)
3865 (Hashtbl.length state.tilemap)) 1;
3867 sep ();
3868 src#caption "Layout" 0;
3869 src#caption2 "Dimension"
3870 (fun () ->
3871 Printf.sprintf "%dx%d (virtual %dx%d)"
3872 state.winw state.winh
3873 state.w state.maxy)
3875 if conf.debug
3876 then
3877 src#caption2 "Position" (fun () ->
3878 Printf.sprintf "%dx%d" state.x state.y
3880 else
3881 src#caption2 "Position" (fun () -> describe_location ()) 1
3884 sep ();
3885 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3886 "Save these parameters as global defaults at exit"
3887 (fun () -> conf.bedefault)
3888 (fun v -> conf.bedefault <- v)
3891 sep ();
3892 let btos b = if b then "@Ulguillemet" else "@Urguillemet" in
3893 src#bool ~offset:0 ~btos "Extended parameters"
3894 (fun () -> !showextended)
3895 (fun v -> showextended := v; fillsrc prevmode prevuioh);
3896 if !showextended
3897 then (
3898 src#bool "checkers"
3899 (fun () -> conf.checkers)
3900 (fun v -> conf.checkers <- v; setcheckers v);
3901 src#bool "update cursor"
3902 (fun () -> conf.updatecurs)
3903 (fun v -> conf.updatecurs <- v);
3904 src#bool "scroll-bar on the left"
3905 (fun () -> conf.leftscroll)
3906 (fun v -> conf.leftscroll <- v);
3907 src#bool "verbose"
3908 (fun () -> conf.verbose)
3909 (fun v -> conf.verbose <- v);
3910 src#bool "invert colors"
3911 (fun () -> conf.invert)
3912 (fun v -> conf.invert <- v);
3913 src#bool "max fit"
3914 (fun () -> conf.maxhfit)
3915 (fun v -> conf.maxhfit <- v);
3916 src#bool "redirect stderr"
3917 (fun () -> conf.redirectstderr)
3918 (fun v -> conf.redirectstderr <- v; redirectstderr ());
3919 src#bool "pax mode"
3920 (fun () -> conf.pax != None)
3921 (fun v ->
3922 if v
3923 then conf.pax <- Some (ref (now (), 0, 0))
3924 else conf.pax <- None);
3925 src#string "uri launcher"
3926 (fun () -> conf.urilauncher)
3927 (fun v -> conf.urilauncher <- v);
3928 src#string "path launcher"
3929 (fun () -> conf.pathlauncher)
3930 (fun v -> conf.pathlauncher <- v);
3931 src#string "tile size"
3932 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3933 (fun v ->
3935 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
3936 conf.tilew <- max 64 w;
3937 conf.tileh <- max 64 h;
3938 flushtiles ();
3939 with exn ->
3940 state.text <- Printf.sprintf "bad tile size `%s': %s"
3941 v (exntos exn)
3943 src#int "texture count"
3944 (fun () -> conf.texcount)
3945 (fun v ->
3946 if realloctexts v
3947 then conf.texcount <- v
3948 else showtext '!' " Failed to set texture count please retry later"
3950 src#int "slice height"
3951 (fun () -> conf.sliceheight)
3952 (fun v ->
3953 conf.sliceheight <- v;
3954 wcmd "sliceh %d" conf.sliceheight;
3956 src#int "anti-aliasing level"
3957 (fun () -> conf.aalevel)
3958 (fun v ->
3959 conf.aalevel <- bound v 0 8;
3960 state.anchor <- getanchor ();
3961 opendoc state.path state.password;
3963 src#string "page scroll scaling factor"
3964 (fun () -> string_of_float conf.pgscale)
3965 (fun v ->
3967 let s = float_of_string v in
3968 conf.pgscale <- s
3969 with exn ->
3970 state.text <- Printf.sprintf
3971 "bad page scroll scaling factor `%s': %s" v (exntos exn)
3974 src#int "ui font size"
3975 (fun () -> fstate.fontsize)
3976 (fun v -> setfontsize (bound v 5 100));
3977 src#int "hint font size"
3978 (fun () -> conf.hfsize)
3979 (fun v -> conf.hfsize <- bound v 5 100);
3980 colorp "background color"
3981 (fun () -> conf.bgcolor)
3982 (fun v -> conf.bgcolor <- v);
3983 src#bool "crop hack"
3984 (fun () -> conf.crophack)
3985 (fun v -> conf.crophack <- v);
3986 src#string "trim fuzz"
3987 (fun () -> irect_to_string conf.trimfuzz)
3988 (fun v ->
3990 conf.trimfuzz <- irect_of_string v;
3991 if conf.trimmargins
3992 then settrim true conf.trimfuzz;
3993 with exn ->
3994 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
3996 src#string "throttle"
3997 (fun () ->
3998 match conf.maxwait with
3999 | None -> "show place holder if page is not ready"
4000 | Some time ->
4001 if time = infinity
4002 then "wait for page to fully render"
4003 else
4004 "wait " ^ string_of_float time
4005 ^ " seconds before showing placeholder"
4007 (fun v ->
4009 let f = float_of_string v in
4010 if f <= 0.0
4011 then conf.maxwait <- None
4012 else conf.maxwait <- Some f
4013 with exn ->
4014 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
4016 src#string "ghyll scroll"
4017 (fun () ->
4018 match conf.ghyllscroll with
4019 | None -> E.s
4020 | Some nab -> ghyllscroll_to_string nab
4022 (fun v ->
4023 try conf.ghyllscroll <- ghyllscroll_of_string v
4024 with exn ->
4025 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
4027 src#string "selection command"
4028 (fun () -> conf.selcmd)
4029 (fun v -> conf.selcmd <- v);
4030 src#string "synctex command"
4031 (fun () -> conf.stcmd)
4032 (fun v -> conf.stcmd <- v);
4033 src#string "pax command"
4034 (fun () -> conf.paxcmd)
4035 (fun v -> conf.paxcmd <- v);
4036 src#string "ask password command"
4037 (fun () -> conf.passcmd)
4038 (fun v -> conf.passcmd <- v);
4039 src#string "save path command"
4040 (fun () -> conf.savecmd)
4041 (fun v -> conf.savecmd <- v);
4042 src#colorspace "color space"
4043 (fun () -> CSTE.to_string conf.colorspace)
4044 (fun v ->
4045 conf.colorspace <- CSTE.of_int v;
4046 wcmd "cs %d" v;
4047 load state.layout;
4049 src#paxmark "pax mark method"
4050 (fun () -> MTE.to_string conf.paxmark)
4051 (fun v -> conf.paxmark <- MTE.of_int v);
4052 if pbousable ()
4053 then
4054 src#bool "use PBO"
4055 (fun () -> conf.usepbo)
4056 (fun v -> conf.usepbo <- v);
4057 src#bool "mouse wheel scrolls pages"
4058 (fun () -> conf.wheelbypage)
4059 (fun v -> conf.wheelbypage <- v);
4060 src#bool "open remote links in a new instance"
4061 (fun () -> conf.riani)
4062 (fun v -> conf.riani <- v);
4063 src#bool "edit annotations inline"
4064 (fun () -> conf.annotinline)
4065 (fun v -> conf.annotinline <- v);
4068 sep ();
4069 src#caption "Document" 0;
4070 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4071 src#caption2 "Pages"
4072 (fun () -> string_of_int state.pagecount) 1;
4073 src#caption2 "Dimensions"
4074 (fun () -> string_of_int (List.length state.pdims)) 1;
4075 if conf.trimmargins
4076 then (
4077 sep ();
4078 src#caption "Trimmed margins" 0;
4079 src#caption2 "Dimensions"
4080 (fun () -> string_of_int (List.length state.pdims)) 1;
4083 sep ();
4084 src#caption "OpenGL" 0;
4085 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4086 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4088 sep ();
4089 src#caption "Location" 0;
4090 if nonemptystr state.origin
4091 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
4092 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
4094 src#reset prevmode prevuioh;
4096 fun () ->
4097 state.text <- E.s;
4098 resetmstate ();
4099 let prevmode = state.mode
4100 and prevuioh = state.uioh in
4101 fillsrc prevmode prevuioh;
4102 let source = (src :> lvsource) in
4103 let modehash = findkeyhash conf "info" in
4104 state.uioh <- coe (object (self)
4105 inherit listview ~zebra:false ~helpmode:false
4106 ~source ~trusted:true ~modehash as super
4107 val mutable m_prevmemused = 0
4108 method! infochanged = function
4109 | Memused ->
4110 if m_prevmemused != state.memused
4111 then (
4112 m_prevmemused <- state.memused;
4113 G.postRedisplay "memusedchanged";
4115 | Pdim -> G.postRedisplay "pdimchanged"
4116 | Docinfo -> fillsrc prevmode prevuioh
4118 method! key key mask =
4119 if not (Wsi.withctrl mask)
4120 then
4121 match key with
4122 | @left | @kpleft -> coe (self#updownlevel ~-1)
4123 | @right | @kpright -> coe (self#updownlevel 1)
4124 | _ -> super#key key mask
4125 else super#key key mask
4126 end);
4127 G.postRedisplay "info";
4130 let enterhelpmode =
4131 let source =
4132 (object
4133 inherit lvsourcebase
4134 method getitemcount = Array.length state.help
4135 method getitem n =
4136 let s, l, _ = state.help.(n) in
4137 (s, l)
4139 method exit ~uioh ~cancel ~active ~first ~pan =
4140 let optuioh =
4141 if not cancel
4142 then (
4143 match state.help.(active) with
4144 | _, _, Action f -> Some (f uioh)
4145 | _, _, Noaction -> Some uioh
4147 else None
4149 m_active <- active;
4150 m_first <- first;
4151 m_pan <- pan;
4152 optuioh
4154 method hasaction n =
4155 match state.help.(n) with
4156 | _, _, Action _ -> true
4157 | _, _, Noaction -> false
4159 initializer
4160 m_active <- -1
4161 end)
4162 in fun () ->
4163 let modehash = findkeyhash conf "help" in
4164 resetmstate ();
4165 state.uioh <- coe (new listview
4166 ~zebra:false ~helpmode:true
4167 ~source ~trusted:true ~modehash);
4168 G.postRedisplay "help";
4171 let entermsgsmode =
4172 let msgsource =
4173 (object
4174 inherit lvsourcebase
4175 val mutable m_items = E.a
4177 method getitemcount = 1 + Array.length m_items
4179 method getitem n =
4180 if n = 0
4181 then "[Clear]", 0
4182 else m_items.(n-1), 0
4184 method exit ~uioh ~cancel ~active ~first ~pan =
4185 ignore uioh;
4186 if not cancel
4187 then (
4188 if active = 0
4189 then Buffer.clear state.errmsgs;
4191 m_active <- active;
4192 m_first <- first;
4193 m_pan <- pan;
4194 None
4196 method hasaction n =
4197 n = 0
4199 method reset =
4200 state.newerrmsgs <- false;
4201 let l = Str.split newlinere (Buffer.contents state.errmsgs) in
4202 m_items <- Array.of_list l
4204 initializer
4205 m_active <- 0
4206 end)
4207 in fun () ->
4208 state.text <- E.s;
4209 resetmstate ();
4210 msgsource#reset;
4211 let source = (msgsource :> lvsource) in
4212 let modehash = findkeyhash conf "listview" in
4213 state.uioh <- coe (object
4214 inherit listview ~zebra:false ~helpmode:false
4215 ~source ~trusted:false ~modehash as super
4216 method! display =
4217 if state.newerrmsgs
4218 then msgsource#reset;
4219 super#display
4220 end);
4221 G.postRedisplay "msgs";
4224 let getusertext s =
4225 let editor = getenvwithdef "EDITOR" E.s in
4226 if emptystr editor
4227 then E.s
4228 else
4229 let tmppath = Filename.temp_file "llpp" "note" in
4230 if nonemptystr s
4231 then (
4232 let oc = open_out tmppath in
4233 output_string oc s;
4234 close_out oc;
4236 let execstr = editor ^ " " ^ tmppath in
4237 let s =
4238 match popen execstr [] with
4239 | (exception exn) ->
4240 showtext '!' @@
4241 Printf.sprintf "popen(%S) failed: %s" execstr (exntos exn);
4243 | pid ->
4244 match Unix.waitpid [] pid with
4245 | (exception exn) ->
4246 showtext '!' @@
4247 Printf.sprintf "waitpid(%d) failed: %s" pid (exntos exn);
4249 | (_pid, status) ->
4250 match status with
4251 | Unix.WEXITED 0 -> filecontents tmppath
4252 | Unix.WEXITED n ->
4253 showtext '!' @@
4254 Printf.sprintf "editor process(%s) exited abnormally: %d"
4255 execstr n;
4257 | Unix.WSIGNALED n ->
4258 showtext '!' @@
4259 Printf.sprintf "editor process(%s) was killed by signal %d"
4260 execstr n;
4262 | Unix.WSTOPPED n ->
4263 showtext '!' @@
4264 Printf.sprintf "editor(%s) process was stopped by signal %d"
4265 execstr n;
4268 match Unix.unlink tmppath with
4269 | (exception exn) ->
4270 showtext '!' @@ Printf.sprintf "failed to ulink %S: %s"
4271 tmppath (exntos exn);
4273 | () -> s
4276 let enterannotmode opaque slinkindex =
4277 let msgsource =
4278 (object
4279 inherit lvsourcebase
4280 val mutable m_text = E.s
4281 val mutable m_items = E.a
4283 method getitemcount = Array.length m_items
4285 method getitem n =
4286 let label, _func = m_items.(n) in
4287 label, 0
4289 method exit ~uioh ~cancel ~active ~first ~pan =
4290 ignore (uioh, first, pan);
4291 if not cancel
4292 then (
4293 let _label, func = m_items.(active) in
4294 func ()
4296 None
4298 method hasaction n = nonemptystr @@ fst m_items.(n)
4300 method reset s =
4301 let rec split accu b i =
4302 let p = b+i in
4303 if p = String.length s
4304 then (String.sub s b (p-b), unit) :: accu
4305 else
4306 if (i > 70 && s.[p] = ' ') || s.[p] = '\r' || s.[p] = '\n'
4307 then
4308 let ss = if i = 0 then E.s else String.sub s b i in
4309 split ((ss, unit)::accu) (p+1) 0
4310 else
4311 split accu b (i+1)
4313 let cleanup () =
4314 wcmd "freepage %s" (~> opaque);
4315 let keys =
4316 Hashtbl.fold (fun key opaque' accu ->
4317 if opaque' = opaque'
4318 then key :: accu else accu) state.pagemap []
4320 List.iter (Hashtbl.remove state.pagemap) keys;
4321 flushtiles ();
4322 gotoy state.y
4324 let dele () =
4325 delannot opaque slinkindex;
4326 cleanup ();
4328 let edit inline () =
4329 let update s =
4330 if emptystr s
4331 then dele ()
4332 else (
4333 modannot opaque slinkindex s;
4334 cleanup ();
4337 if inline
4338 then
4339 let mode = state.mode in
4340 state.mode <-
4341 Textentry (
4342 ("annotation: ", m_text, None, textentry, update, true),
4343 fun _ -> state.mode <- mode);
4344 state.text <- E.s;
4345 enttext ();
4346 else
4347 let s = getusertext m_text in
4348 update s
4350 m_text <- s;
4351 m_items <-
4352 ( "[Copy]", fun () -> selstring m_text)
4353 :: ("[Delete]", dele)
4354 :: ("[Edit]", edit conf.annotinline)
4355 :: (E.s, unit)
4356 :: split [] 0 0 |> List.rev |> Array.of_list
4358 initializer
4359 m_active <- 0
4360 end)
4362 state.text <- E.s;
4363 let s = getannotcontents opaque slinkindex in
4364 resetmstate ();
4365 msgsource#reset s;
4366 let source = (msgsource :> lvsource) in
4367 let modehash = findkeyhash conf "listview" in
4368 state.uioh <- coe (object
4369 inherit listview ~zebra:false ~helpmode:false
4370 ~source ~trusted:false ~modehash
4371 end);
4372 G.postRedisplay "enterannotmode";
4375 let gotounder under =
4376 let getpath filename =
4377 let path =
4378 if nonemptystr filename
4379 then
4380 if Filename.is_relative filename
4381 then
4382 let dir = Filename.dirname state.path in
4383 let dir =
4384 if Filename.is_implicit dir
4385 then Filename.concat (Sys.getcwd ()) dir
4386 else dir
4388 Filename.concat dir filename
4389 else filename
4390 else E.s
4392 if Sys.file_exists path
4393 then path
4394 else E.s
4396 match under with
4397 | Ulinkgoto (pageno, top) ->
4398 if pageno >= 0
4399 then (
4400 addnav ();
4401 gotopage1 pageno top;
4404 | Ulinkuri s ->
4405 addpid @@ gotouri s
4407 | Uremote (filename, pageno) ->
4408 let path = getpath filename in
4409 if nonemptystr path
4410 then (
4411 if conf.riani
4412 then
4413 let command = Printf.sprintf "%s -page %d %S" !selfexec pageno path in
4414 try addpid @@ popen command []
4415 with exn -> dolog "failed to execute `%s': %s" command (exntos exn)
4416 else
4417 let anchor = getanchor () in
4418 let ranchor = state.path, state.password, anchor, state.origin in
4419 state.origin <- E.s;
4420 state.anchor <- (pageno, 0.0, 0.0);
4421 state.ranchors <- ranchor :: state.ranchors;
4422 opendoc path E.s;
4424 else showtext '!' ("Could not find " ^ filename)
4426 | Uremotedest (filename, destname) ->
4427 let path = getpath filename in
4428 if nonemptystr path
4429 then (
4430 if conf.riani
4431 then
4432 let command = !selfexec ^ " " ^ path ^ " -dest " ^ destname in
4433 try addpid @@ popen command []
4434 with exn -> dolog "failed to execute `%s': %s" command (exntos exn)
4435 else
4436 let anchor = getanchor () in
4437 let ranchor = state.path, state.password, anchor, state.origin in
4438 state.origin <- E.s;
4439 state.nameddest <- destname;
4440 state.ranchors <- ranchor :: state.ranchors;
4441 opendoc path E.s;
4443 else showtext '!' ("Could not find " ^ filename)
4445 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4446 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
4449 let gotooutline (_, _, kind) =
4450 match kind with
4451 | Onone -> None
4452 | Oanchor anchor ->
4453 let (pageno, y, _) = anchor in
4454 let y = getanchory
4455 (if conf.presentation then (pageno, y, 1.0) else anchor)
4457 addnav ();
4458 gotoghyll y;
4459 None
4460 | Ouri uri -> gotounder (Ulinkuri uri); None
4461 | Olaunch cmd -> gotounder (Ulaunch cmd); None
4462 | Oremote remote -> gotounder (Uremote remote); None
4463 | Ohistory hist -> gotohist hist; None
4464 | Oremotedest remotedest -> gotounder (Uremotedest remotedest); None
4465 | Oaction f -> f (); None
4466 | Oreaction f -> Some (f ())
4469 let outlinesource sourcetype =
4470 (object (self)
4471 inherit lvsourcebase
4472 val mutable m_items = E.a
4473 val mutable m_minfo = E.a
4474 val mutable m_orig_items = E.a
4475 val mutable m_orig_minfo = E.a
4476 val mutable m_narrow_patterns = []
4477 val mutable m_hadremovals = false
4478 val mutable m_gen = -1
4480 method getitemcount =
4481 Array.length m_items + (if m_hadremovals then 1 else 0)
4483 method getitem n =
4484 if n == Array.length m_items && m_hadremovals
4485 then
4486 ("[Confirm removal]", 0)
4487 else
4488 let s, n, _ = m_items.(n) in
4489 (s, n)
4491 method exit ~uioh ~cancel ~active ~first ~pan =
4492 ignore (uioh, first);
4493 let confrimremoval = m_hadremovals && active = Array.length m_items in
4494 let items, minfo =
4495 if m_narrow_patterns = []
4496 then m_orig_items, m_orig_minfo
4497 else m_items, m_minfo
4499 m_pan <- pan;
4500 if not cancel
4501 then (
4502 if not confrimremoval
4503 then (
4504 m_items <- items;
4505 m_minfo <- minfo;
4506 match gotooutline m_items.(active) with
4507 | None -> None
4508 | Some outlines ->
4509 self#reset emptyanchor outlines;
4510 Some uioh
4512 else (
4513 state.bookmarks <- Array.to_list m_items;
4514 m_orig_items <- m_items;
4515 m_orig_minfo <- m_minfo;
4516 None
4519 else (
4520 m_items <- items;
4521 m_minfo <- minfo;
4522 None
4525 method hasaction _ = true
4527 method greetmsg =
4528 if Array.length m_items != Array.length m_orig_items
4529 then
4530 let s =
4531 match m_narrow_patterns with
4532 | one :: [] -> one
4533 | many -> String.concat "@Uellipsis" (List.rev many)
4535 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
4536 else E.s
4538 method statestr =
4539 match m_narrow_patterns with
4540 | [] -> E.s
4541 | one :: [] -> one
4542 | head :: _ -> "@Uellipsis" ^ head
4544 method narrow pattern =
4545 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
4546 match reopt with
4547 | None -> ()
4548 | Some re ->
4549 let rec loop accu minfo n =
4550 if n = -1
4551 then (
4552 m_items <- Array.of_list accu;
4553 m_minfo <- Array.of_list minfo;
4555 else
4556 let (s, _, t) as o = m_items.(n) in
4557 let accu, minfo =
4558 match t with
4559 | Oaction _ | Oreaction _ -> o :: accu, (0, 0) :: minfo
4560 | Onone | Oanchor _ | Ouri _ | Olaunch _
4561 | Oremote _ | Oremotedest _ | Ohistory _ ->
4562 let first =
4563 try Str.search_forward re s 0
4564 with Not_found -> -1
4566 if first >= 0
4567 then o :: accu, (first, Str.match_end ()) :: minfo
4568 else accu, minfo
4570 loop accu minfo (n-1)
4572 loop [] [] (Array.length m_items - 1)
4574 method! getminfo = m_minfo
4576 method denarrow =
4577 m_orig_items <- (
4578 match sourcetype with
4579 | `bookmarks -> Array.of_list state.bookmarks
4580 | `outlines -> state.outlines
4581 | `history -> genhistoutlines ()
4583 m_minfo <- m_orig_minfo;
4584 m_items <- m_orig_items
4586 method remove m =
4587 if sourcetype = `bookmarks
4588 then
4589 if m >= 0 && m < Array.length m_items
4590 then (
4591 m_hadremovals <- true;
4592 m_items <- Array.init (Array.length m_items - 1) (fun n ->
4593 let n = if n >= m then n+1 else n in
4594 m_items.(n)
4598 method add_narrow_pattern pattern =
4599 m_narrow_patterns <- pattern :: m_narrow_patterns
4601 method del_narrow_pattern =
4602 match m_narrow_patterns with
4603 | _ :: rest -> m_narrow_patterns <- rest
4604 | [] -> ()
4606 method renarrow =
4607 self#denarrow;
4608 match m_narrow_patterns with
4609 | pattern :: [] -> self#narrow pattern; pattern
4610 | list ->
4611 List.fold_left (fun accu pattern ->
4612 self#narrow pattern;
4613 pattern ^ "@Uellipsis" ^ accu) E.s list
4615 method calcactive anchor =
4616 let rely = getanchory anchor in
4617 let rec loop n best bestd =
4618 if n = Array.length m_items
4619 then best
4620 else
4621 let _, _, kind = m_items.(n) in
4622 match kind with
4623 | Oanchor anchor ->
4624 let orely = getanchory anchor in
4625 let d = abs (orely - rely) in
4626 if d < bestd
4627 then loop (n+1) n d
4628 else loop (n+1) best bestd
4629 | Onone | Oremote _ | Olaunch _
4630 | Oremotedest _ | Ouri _ | Ohistory _ | Oaction _ | Oreaction _ ->
4631 loop (n+1) best bestd
4633 loop 0 ~-1 max_int
4635 method reset anchor items =
4636 m_hadremovals <- false;
4637 if state.gen != m_gen
4638 then (
4639 m_orig_items <- items;
4640 m_items <- items;
4641 m_narrow_patterns <- [];
4642 m_minfo <- E.a;
4643 m_orig_minfo <- E.a;
4644 m_gen <- state.gen;
4646 else (
4647 if items != m_orig_items
4648 then (
4649 m_orig_items <- items;
4650 if m_narrow_patterns == []
4651 then m_items <- items;
4654 let active = self#calcactive anchor in
4655 m_active <- active;
4656 m_first <- firstof m_first active
4657 end)
4660 let enteroutlinemode, enterbookmarkmode, enterhistmode =
4661 let mkselector sourcetype =
4662 let source = outlinesource sourcetype in
4663 fun errmsg ->
4664 let outlines =
4665 match sourcetype with
4666 | `bookmarks -> Array.of_list state.bookmarks
4667 | `outlines -> state.outlines
4668 | `history -> genhistoutlines ()
4670 if Array.length outlines = 0
4671 then (
4672 showtext ' ' errmsg;
4674 else (
4675 resetmstate ();
4676 state.text <- source#greetmsg;
4677 Wsi.setcursor Wsi.CURSOR_INHERIT;
4678 let anchor = getanchor () in
4679 source#reset anchor outlines;
4680 state.uioh <-
4681 coe (new outlinelistview ~zebra:(sourcetype=`history) ~source);
4682 G.postRedisplay "enter selector";
4685 let mkenter sourcetype errmsg =
4686 let enter = mkselector sourcetype in
4687 fun () -> enter errmsg
4689 (**)mkenter `outlines "Document has no outline"
4690 , mkenter `bookmarks "Document has no bookmarks (yet)"
4691 , mkenter `history "History is empty"
4694 let quickbookmark ?title () =
4695 match state.layout with
4696 | [] -> ()
4697 | l :: _ ->
4698 let title =
4699 match title with
4700 | None ->
4701 let tm = Unix.localtime (now ()) in
4702 Printf.sprintf
4703 "Quick (page %d) (bookmarked at %02d/%02d/%d %02d:%02d)"
4704 (l.pageno+1)
4705 tm.Unix.tm_mday
4706 (tm.Unix.tm_mon+1)
4707 (tm.Unix.tm_year + 1900)
4708 tm.Unix.tm_hour
4709 tm.Unix.tm_min
4710 | Some title -> title
4712 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4715 let setautoscrollspeed step goingdown =
4716 let incr = max 1 ((abs step) / 2) in
4717 let incr = if goingdown then incr else -incr in
4718 let astep = boundastep state.winh (step + incr) in
4719 state.autoscroll <- Some astep;
4722 let canpan () =
4723 match conf.columns with
4724 | Csplit _ -> true
4725 | Csingle _ | Cmulti _ -> state.x != 0 || conf.zoom > 1.0
4728 let panbound x = bound x (-state.w) (wadjsb () + state.winw);;
4730 let existsinrow pageno (columns, coverA, coverB) p =
4731 let last = ((pageno - coverA) mod columns) + columns in
4732 let rec any = function
4733 | [] -> false
4734 | l :: rest ->
4735 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4736 then p l
4737 else (
4738 if not (p l)
4739 then (if l.pageno = last then false else any rest)
4740 else true
4743 any state.layout
4746 let nextpage () =
4747 match state.layout with
4748 | [] ->
4749 let pageno = page_of_y state.y in
4750 gotoghyll (getpagey (pageno+1))
4751 | l :: rest ->
4752 match conf.columns with
4753 | Csingle _ ->
4754 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4755 then
4756 let y = clamp (pgscale state.winh) in
4757 gotoghyll y
4758 else
4759 let pageno = min (l.pageno+1) (state.pagecount-1) in
4760 gotoghyll (getpagey pageno)
4761 | Cmulti ((c, _, _) as cl, _) ->
4762 if conf.presentation
4763 && (existsinrow l.pageno cl
4764 (fun l -> l.pageh > l.pagey + l.pagevh))
4765 then
4766 let y = clamp (pgscale state.winh) in
4767 gotoghyll y
4768 else
4769 let pageno = min (l.pageno+c) (state.pagecount-1) in
4770 gotoghyll (getpagey pageno)
4771 | Csplit (n, _) ->
4772 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4773 then
4774 let pagey, pageh = getpageyh l.pageno in
4775 let pagey = pagey + pageh * l.pagecol in
4776 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4777 gotoghyll (pagey + pageh + ips)
4780 let prevpage () =
4781 match state.layout with
4782 | [] ->
4783 let pageno = page_of_y state.y in
4784 gotoghyll (getpagey (pageno-1))
4785 | l :: _ ->
4786 match conf.columns with
4787 | Csingle _ ->
4788 if conf.presentation && l.pagey != 0
4789 then
4790 gotoghyll (clamp (pgscale ~-(state.winh)))
4791 else
4792 let pageno = max 0 (l.pageno-1) in
4793 gotoghyll (getpagey pageno)
4794 | Cmulti ((c, _, coverB) as cl, _) ->
4795 if conf.presentation &&
4796 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4797 then
4798 gotoghyll (clamp (pgscale ~-(state.winh)))
4799 else
4800 let decr =
4801 if l.pageno = state.pagecount - coverB
4802 then 1
4803 else c
4805 let pageno = max 0 (l.pageno-decr) in
4806 gotoghyll (getpagey pageno)
4807 | Csplit (n, _) ->
4808 let y =
4809 if l.pagecol = 0
4810 then
4811 if l.pageno = 0
4812 then l.pagey
4813 else
4814 let pageno = max 0 (l.pageno-1) in
4815 let pagey, pageh = getpageyh pageno in
4816 pagey + (n-1)*pageh
4817 else
4818 let pagey, pageh = getpageyh l.pageno in
4819 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4821 gotoghyll y
4824 let save () =
4825 if emptystr conf.savecmd
4826 then error "don't know where to save modified document"
4827 else
4828 let savecmd = Str.global_replace percentsre state.path conf.savecmd in
4829 let path =
4830 getcmdoutput
4831 (fun s -> error "failed to obtain path to the saved copy: %s" s)
4832 savecmd
4834 if nonemptystr path
4835 then
4836 let tmp = path ^ ".tmp" in
4837 savedoc tmp;
4838 Unix.rename tmp path;
4841 let viewkeyboard key mask =
4842 let enttext te =
4843 let mode = state.mode in
4844 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4845 state.text <- E.s;
4846 enttext ();
4847 G.postRedisplay "view:enttext"
4849 let ctrl = Wsi.withctrl mask in
4850 let key =
4851 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
4853 match key with
4854 | @Q -> exit 0
4856 | @W ->
4857 if hasunsavedchanges ()
4858 then save ()
4860 | @insert ->
4861 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4862 then (
4863 state.mode <- LinkNav (Ltgendir 0);
4864 gotoy state.y;
4866 else showtext '!' "Keyboard link navigation does not work under rotation"
4868 | @escape | @q ->
4869 begin match state.mstate with
4870 | Mzoomrect _ ->
4871 resetmstate ();
4872 G.postRedisplay "kill rect";
4873 | Msel _
4874 | Mpan _
4875 | Mscrolly | Mscrollx
4876 | Mzoom _
4877 | Mnone ->
4878 begin match state.mode with
4879 | LinkNav _ ->
4880 state.mode <- View;
4881 G.postRedisplay "esc leave linknav"
4882 | Birdseye _
4883 | Textentry _
4884 | View ->
4885 match state.ranchors with
4886 | [] -> raise Quit
4887 | (path, password, anchor, origin) :: rest ->
4888 state.ranchors <- rest;
4889 state.anchor <- anchor;
4890 state.origin <- origin;
4891 state.nameddest <- E.s;
4892 opendoc path password
4893 end;
4894 end;
4896 | @backspace ->
4897 gotoghyll (getnav ~-1)
4899 | @o ->
4900 enteroutlinemode ()
4902 | @H ->
4903 enterhistmode ()
4905 | @u ->
4906 state.rects <- [];
4907 state.text <- E.s;
4908 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap;
4909 G.postRedisplay "dehighlight";
4911 | @slash | @question ->
4912 let ondone isforw s =
4913 cbput state.hists.pat s;
4914 state.searchpattern <- s;
4915 search s isforw
4917 let s = String.make 1 (Char.chr key) in
4918 enttext (s, E.s, Some (onhist state.hists.pat),
4919 textentry, ondone (key = @slash), true)
4921 | @plus | @kpplus | @equals when ctrl ->
4922 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4923 setzoom (conf.zoom +. incr)
4925 | @plus | @kpplus ->
4926 let ondone s =
4927 let n =
4928 try int_of_string s with exc ->
4929 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
4930 max_int
4932 if n != max_int
4933 then (
4934 conf.pagebias <- n;
4935 state.text <- "page bias is now " ^ string_of_int n;
4938 enttext ("page bias: ", E.s, None, intentry, ondone, true)
4940 | @minus | @kpminus when ctrl ->
4941 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4942 setzoom (max 0.01 (conf.zoom -. decr))
4944 | @minus | @kpminus ->
4945 let ondone msg = state.text <- msg in
4946 enttext (
4947 "option [acfhilpstvxACFPRSZTISM]: ", E.s, None,
4948 optentry state.mode, ondone, true
4951 | @0 when ctrl ->
4952 if conf.zoom = 1.0
4953 then (
4954 state.x <- 0;
4955 gotoy state.y
4957 else setzoom 1.0
4959 | (@1 | @2) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
4960 let cols =
4961 match conf.columns with
4962 | Csingle _ | Cmulti _ -> 1
4963 | Csplit (n, _) -> n
4965 let h = state.winh -
4966 conf.interpagespace lsl (if conf.presentation then 1 else 0)
4968 let zoom = zoomforh state.winw h (vscrollw ()) cols in
4969 if zoom > 0.0 && (key = 50 || zoom < 1.0)
4970 then setzoom zoom
4972 | @3 when ctrl ->
4973 let fm =
4974 match conf.fitmodel with
4975 | FitWidth -> FitProportional
4976 | FitProportional -> FitPage
4977 | FitPage -> FitWidth
4979 state.text <- "fit model: " ^ FMTE.to_string fm;
4980 reqlayout conf.angle fm
4982 | @F9 ->
4983 togglebirdseye ()
4985 | @9 when ctrl ->
4986 togglebirdseye ()
4988 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4989 when not ctrl -> (* 0..9 *)
4990 let ondone s =
4991 let n =
4992 try int_of_string s with exc ->
4993 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
4996 if n >= 0
4997 then (
4998 addnav ();
4999 cbput state.hists.pag (string_of_int n);
5000 gotopage1 (n + conf.pagebias - 1) 0;
5003 let pageentry text key =
5004 match Char.unsafe_chr key with
5005 | 'g' -> TEdone text
5006 | _ -> intentry text key
5008 let text = String.make 1 (Char.chr key) in
5009 enttext (":", text, Some (onhist state.hists.pag),
5010 pageentry, ondone, true)
5012 | @b ->
5013 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
5014 reshape state.winw state.winh;
5016 | @B ->
5017 state.bzoom <- not state.bzoom;
5018 state.rects <- [];
5019 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
5021 | @l ->
5022 conf.hlinks <- not conf.hlinks;
5023 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5024 G.postRedisplay "toggle highlightlinks";
5026 | @F ->
5027 state.glinks <- true;
5028 let mode = state.mode in
5029 state.mode <- Textentry (
5030 (":", E.s, None, linknentry, linknact gotounder, false),
5031 (fun _ ->
5032 state.glinks <- false;
5033 state.mode <- mode)
5035 state.text <- E.s;
5036 G.postRedisplay "view:linkent(F)"
5038 | @y ->
5039 state.glinks <- true;
5040 let mode = state.mode in
5041 state.mode <- Textentry (
5043 ":", E.s, None, linknentry, linknact (fun under ->
5044 selstring (undertext under);
5045 ), false
5047 fun _ ->
5048 state.glinks <- false;
5049 state.mode <- mode
5051 state.text <- E.s;
5052 G.postRedisplay "view:linkent"
5054 | @a ->
5055 begin match state.autoscroll with
5056 | Some step ->
5057 conf.autoscrollstep <- step;
5058 state.autoscroll <- None
5059 | None ->
5060 if conf.autoscrollstep = 0
5061 then state.autoscroll <- Some 1
5062 else state.autoscroll <- Some conf.autoscrollstep
5065 | @p when ctrl ->
5066 launchpath ()
5068 | @P ->
5069 setpresentationmode (not conf.presentation);
5070 showtext ' ' ("presentation mode " ^
5071 if conf.presentation then "on" else "off");
5073 | @f ->
5074 if List.mem Wsi.Fullscreen state.winstate
5075 then Wsi.reshape conf.cwinw conf.cwinh
5076 else Wsi.fullscreen ()
5078 | @p | @N ->
5079 search state.searchpattern false
5081 | @n | @F3 ->
5082 search state.searchpattern true
5084 | @t ->
5085 begin match state.layout with
5086 | [] -> ()
5087 | l :: _ ->
5088 gotoghyll (getpagey l.pageno)
5091 | @space ->
5092 nextpage ()
5094 | @delete | @kpdelete -> (* delete *)
5095 prevpage ()
5097 | @equals ->
5098 showtext ' ' (describe_location ());
5100 | @w ->
5101 begin match state.layout with
5102 | [] -> ()
5103 | l :: _ ->
5104 Wsi.reshape (l.pagew + vscrollw ()) l.pageh;
5105 G.postRedisplay "w"
5108 | @apos ->
5109 enterbookmarkmode ()
5111 | @h | @F1 ->
5112 enterhelpmode ()
5114 | @i ->
5115 enterinfomode ()
5117 | @e when Buffer.length state.errmsgs > 0 ->
5118 entermsgsmode ()
5120 | @m ->
5121 let ondone s =
5122 match state.layout with
5123 | l :: _ ->
5124 if nonemptystr s
5125 then
5126 state.bookmarks <-
5127 (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
5128 | _ -> ()
5130 enttext ("bookmark: ", E.s, None, textentry, ondone, true)
5132 | @tilde ->
5133 quickbookmark ();
5134 showtext ' ' "Quick bookmark added";
5136 | @z ->
5137 begin match state.layout with
5138 | l :: _ ->
5139 let rect = getpdimrect l.pagedimno in
5140 let w, h =
5141 if conf.crophack
5142 then
5143 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5144 truncate (1.2 *. (rect.(3) -. rect.(0))))
5145 else
5146 (truncate (rect.(1) -. rect.(0)),
5147 truncate (rect.(3) -. rect.(0)))
5149 let w = truncate ((float w)*.conf.zoom)
5150 and h = truncate ((float h)*.conf.zoom) in
5151 if w != 0 && h != 0
5152 then (
5153 state.anchor <- getanchor ();
5154 Wsi.reshape (w + vscrollw ()) (h + conf.interpagespace)
5156 G.postRedisplay "z";
5158 | [] -> ()
5161 | @x -> state.roam ()
5163 | @Lt | @Gt ->
5164 reqlayout (conf.angle +
5165 (if key = @Gt then 30 else -30)) conf.fitmodel
5167 | @Lb | @Rb ->
5168 conf.colorscale <-
5169 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5171 G.postRedisplay "brightness";
5173 | @c when state.mode = View ->
5174 if Wsi.withalt mask
5175 then (
5176 if conf.zoom > 1.0
5177 then
5178 let m = (wadjsb () + state.winw - state.w) / 2 in
5179 state.x <- m;
5180 gotoy_and_clear_text state.y
5182 else
5183 let (c, a, b), z =
5184 match state.prevcolumns with
5185 | None -> (1, 0, 0), 1.0
5186 | Some (columns, z) ->
5187 let cab =
5188 match columns with
5189 | Csplit (c, _) -> -c, 0, 0
5190 | Cmulti ((c, a, b), _) -> c, a, b
5191 | Csingle _ -> 1, 0, 0
5193 cab, z
5195 setcolumns View c a b;
5196 setzoom z
5198 | @down | @up when ctrl && Wsi.withshift mask ->
5199 let zoom, x = state.prevzoom in
5200 setzoom zoom;
5201 state.x <- x;
5203 | @k | @up | @kpup ->
5204 begin match state.autoscroll with
5205 | None ->
5206 begin match state.mode with
5207 | Birdseye beye -> upbirdseye 1 beye
5208 | Textentry _
5209 | View
5210 | LinkNav _ ->
5211 if ctrl
5212 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5213 else (
5214 if not (Wsi.withshift mask) && conf.presentation
5215 then prevpage ()
5216 else gotoghyll1 true (clamp (-conf.scrollstep))
5219 | Some n ->
5220 setautoscrollspeed n false
5223 | @j | @down | @kpdown ->
5224 begin match state.autoscroll with
5225 | None ->
5226 begin match state.mode with
5227 | Birdseye beye -> downbirdseye 1 beye
5228 | Textentry _
5229 | View
5230 | LinkNav _ ->
5231 if ctrl
5232 then gotoy_and_clear_text (clamp (state.winh/2))
5233 else (
5234 if not (Wsi.withshift mask) && conf.presentation
5235 then nextpage ()
5236 else gotoghyll1 true (clamp (conf.scrollstep))
5239 | Some n ->
5240 setautoscrollspeed n true
5243 | @left | @right | @kpleft | @kpright when not (Wsi.withalt mask) ->
5244 if canpan ()
5245 then
5246 let dx =
5247 if ctrl
5248 then state.winw / 2
5249 else conf.hscrollstep
5251 let dx = if key = @left || key = @kpleft then dx else -dx in
5252 state.x <- panbound (state.x + dx);
5253 gotoy_and_clear_text state.y
5254 else (
5255 state.text <- E.s;
5256 G.postRedisplay "left/right"
5259 | @prior | @kpprior ->
5260 let y =
5261 if ctrl
5262 then
5263 match state.layout with
5264 | [] -> state.y
5265 | l :: _ -> state.y - l.pagey
5266 else
5267 clamp (pgscale (-state.winh))
5269 gotoghyll y
5271 | @next | @kpnext ->
5272 let y =
5273 if ctrl
5274 then
5275 match List.rev state.layout with
5276 | [] -> state.y
5277 | l :: _ -> getpagey l.pageno
5278 else
5279 clamp (pgscale state.winh)
5281 gotoghyll y
5283 | @g | @home | @kphome ->
5284 addnav ();
5285 gotoghyll 0
5286 | @G | @jend | @kpend ->
5287 addnav ();
5288 gotoghyll (clamp state.maxy)
5290 | @right | @kpright when Wsi.withalt mask ->
5291 gotoghyll (getnav 1)
5292 | @left | @kpleft when Wsi.withalt mask ->
5293 gotoghyll (getnav ~-1)
5295 | @r ->
5296 reload ()
5298 | @v when conf.debug ->
5299 state.rects <- [];
5300 List.iter (fun l ->
5301 match getopaque l.pageno with
5302 | None -> ()
5303 | Some opaque ->
5304 let x0, y0, x1, y1 = pagebbox opaque in
5305 let a,b = float x0, float y0 in
5306 let c,d = float x1, float y0 in
5307 let e,f = float x1, float y1 in
5308 let h,j = float x0, float y1 in
5309 let rect = (a,b,c,d,e,f,h,j) in
5310 debugrect rect;
5311 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5312 ) state.layout;
5313 G.postRedisplay "v";
5315 | @pipe ->
5316 let mode = state.mode in
5317 let cmd = ref E.s in
5318 let onleave = function
5319 | Cancel -> state.mode <- mode
5320 | Confirm ->
5321 List.iter (fun l ->
5322 match getopaque l.pageno with
5323 | Some opaque -> pipesel opaque !cmd
5324 | None -> ()) state.layout;
5325 state.mode <- mode
5327 let ondone s =
5328 cbput state.hists.sel s;
5329 cmd := s
5331 let te =
5332 "| ", !cmd, Some (onhist state.hists.sel), textentry, ondone, true
5334 G.postRedisplay "|";
5335 state.mode <- Textentry (te, onleave);
5337 | _ ->
5338 vlog "huh? %s" (Wsi.keyname key)
5341 let linknavkeyboard key mask linknav =
5342 let getpage pageno =
5343 let rec loop = function
5344 | [] -> None
5345 | l :: _ when l.pageno = pageno -> Some l
5346 | _ :: rest -> loop rest
5347 in loop state.layout
5349 let doexact (pageno, n) =
5350 match getopaque pageno, getpage pageno with
5351 | Some opaque, Some l ->
5352 if key = @enter || key = @kpenter
5353 then
5354 let under = getlink opaque n in
5355 G.postRedisplay "link gotounder";
5356 gotounder under;
5357 state.mode <- View;
5358 else
5359 let opt, dir =
5360 match key with
5361 | @home ->
5362 Some (findlink opaque LDfirst), -1
5364 | @jend ->
5365 Some (findlink opaque LDlast), 1
5367 | @left ->
5368 Some (findlink opaque (LDleft n)), -1
5370 | @right ->
5371 Some (findlink opaque (LDright n)), 1
5373 | @up ->
5374 Some (findlink opaque (LDup n)), -1
5376 | @down ->
5377 Some (findlink opaque (LDdown n)), 1
5379 | _ -> None, 0
5381 let pwl l dir =
5382 begin match findpwl l.pageno dir with
5383 | Pwlnotfound -> ()
5384 | Pwl pageno ->
5385 let notfound dir =
5386 state.mode <- LinkNav (Ltgendir dir);
5387 let y, h = getpageyh pageno in
5388 let y =
5389 if dir < 0
5390 then y + h - state.winh
5391 else y
5393 gotoy y
5395 begin match getopaque pageno, getpage pageno with
5396 | Some opaque, Some _ ->
5397 let link =
5398 let ld = if dir > 0 then LDfirst else LDlast in
5399 findlink opaque ld
5401 begin match link with
5402 | Lfound m ->
5403 showlinktype (getlink opaque m);
5404 state.mode <- LinkNav (Ltexact (pageno, m));
5405 G.postRedisplay "linknav jpage";
5406 | Lnotfound -> notfound dir
5407 end;
5408 | _ -> notfound dir
5409 end;
5410 end;
5412 begin match opt with
5413 | Some Lnotfound -> pwl l dir;
5414 | Some (Lfound m) ->
5415 if m = n
5416 then pwl l dir
5417 else (
5418 let _, y0, _, y1 = getlinkrect opaque m in
5419 if y0 < l.pagey
5420 then gotopage1 l.pageno y0
5421 else (
5422 let d = fstate.fontsize + 1 in
5423 if y1 - l.pagey > l.pagevh - d
5424 then gotopage1 l.pageno (y1 - state.winh - hscrollh () + d)
5425 else G.postRedisplay "linknav";
5427 showlinktype (getlink opaque m);
5428 state.mode <- LinkNav (Ltexact (l.pageno, m));
5431 | None -> viewkeyboard key mask
5432 end;
5433 | _ -> viewkeyboard key mask
5435 if key = @insert
5436 then (
5437 state.mode <- View;
5438 G.postRedisplay "leave linknav"
5440 else
5441 match linknav with
5442 | Ltgendir _ | Ltnotready _ -> viewkeyboard key mask
5443 | Ltexact exact -> doexact exact
5446 let keyboard key mask =
5447 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5448 then wcmd "interrupt"
5449 else state.uioh <- state.uioh#key key mask
5452 let birdseyekeyboard key mask
5453 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5454 let incr =
5455 match conf.columns with
5456 | Csingle _ -> 1
5457 | Cmulti ((c, _, _), _) -> c
5458 | Csplit _ -> failwith "bird's eye split mode"
5460 let pgh layout = List.fold_left
5461 (fun m l -> max l.pageh m) state.winh layout in
5462 match key with
5463 | @l when Wsi.withctrl mask ->
5464 let y, h = getpageyh pageno in
5465 let top = (state.winh - h) / 2 in
5466 gotoy (max 0 (y - top))
5467 | @enter | @kpenter -> leavebirdseye beye false
5468 | @escape -> leavebirdseye beye true
5469 | @up -> upbirdseye incr beye
5470 | @down -> downbirdseye incr beye
5471 | @left -> upbirdseye 1 beye
5472 | @right -> downbirdseye 1 beye
5474 | @prior ->
5475 begin match state.layout with
5476 | l :: _ ->
5477 if l.pagey != 0
5478 then (
5479 state.mode <- Birdseye (
5480 oconf, leftx, l.pageno, hooverpageno, anchor
5482 gotopage1 l.pageno 0;
5484 else (
5485 let layout = layout (state.y-state.winh) (pgh state.layout) in
5486 match layout with
5487 | [] -> gotoy (clamp (-state.winh))
5488 | l :: _ ->
5489 state.mode <- Birdseye (
5490 oconf, leftx, l.pageno, hooverpageno, anchor
5492 gotopage1 l.pageno 0
5495 | [] -> gotoy (clamp (-state.winh))
5496 end;
5498 | @next ->
5499 begin match List.rev state.layout with
5500 | l :: _ ->
5501 let layout = layout (state.y + (pgh state.layout)) state.winh in
5502 begin match layout with
5503 | [] ->
5504 let incr = l.pageh - l.pagevh in
5505 if incr = 0
5506 then (
5507 state.mode <-
5508 Birdseye (
5509 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5511 G.postRedisplay "birdseye pagedown";
5513 else gotoy (clamp (incr + conf.interpagespace*2));
5515 | l :: _ ->
5516 state.mode <-
5517 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5518 gotopage1 l.pageno 0;
5521 | [] -> gotoy (clamp state.winh)
5522 end;
5524 | @home ->
5525 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5526 gotopage1 0 0
5528 | @jend ->
5529 let pageno = state.pagecount - 1 in
5530 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5531 if not (pagevisible state.layout pageno)
5532 then
5533 let h =
5534 match List.rev state.pdims with
5535 | [] -> state.winh
5536 | (_, _, h, _) :: _ -> h
5538 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5539 else G.postRedisplay "birdseye end";
5541 | _ -> viewkeyboard key mask
5544 let drawpage l =
5545 let color =
5546 match state.mode with
5547 | Textentry _ -> scalecolor 0.4
5548 | LinkNav _
5549 | View -> scalecolor 1.0
5550 | Birdseye (_, _, pageno, hooverpageno, _) ->
5551 if l.pageno = hooverpageno
5552 then scalecolor 0.9
5553 else (
5554 if l.pageno = pageno
5555 then (
5556 let c = scalecolor 1.0 in
5557 GlDraw.color c;
5558 GlDraw.line_width 3.0;
5559 let dispx = xadjsb () + l.pagedispx in
5560 linerect
5561 (float (dispx-1)) (float (l.pagedispy-1))
5562 (float (dispx+l.pagevw+1))
5563 (float (l.pagedispy+l.pagevh+1))
5565 GlDraw.line_width 1.0;
5568 else scalecolor 0.8
5571 drawtiles l color;
5574 let postdrawpage l linkindexbase =
5575 match getopaque l.pageno with
5576 | Some opaque ->
5577 if tileready l l.pagex l.pagey
5578 then
5579 let x = l.pagedispx - l.pagex + xadjsb ()
5580 and y = l.pagedispy - l.pagey in
5581 let hlmask =
5582 match conf.columns with
5583 | Csingle _ | Cmulti _ ->
5584 (if conf.hlinks then 1 else 0)
5585 + (if state.glinks
5586 && not (isbirdseye state.mode) then 2 else 0)
5587 | Csplit _ -> 0
5589 let s =
5590 match state.mode with
5591 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5592 | Textentry _
5593 | Birdseye _
5594 | View
5595 | LinkNav _ -> E.s
5597 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5598 else 0
5599 | _ -> 0
5602 let scrollindicator () =
5603 let sbw, ph, sh = state.uioh#scrollph in
5604 let sbh, pw, sw = state.uioh#scrollpw in
5606 let x0,x1,hx0 =
5607 if conf.leftscroll
5608 then (0, sbw, sbw)
5609 else ((state.winw - sbw), state.winw, 0)
5612 GlDraw.color (0.64, 0.64, 0.64);
5613 filledrect (float x0) 0. (float x1) (float state.winh);
5614 filledrect
5615 (float hx0) (float (state.winh - sbh))
5616 (float (hx0 + wadjsb () + state.winw)) (float state.winh)
5618 GlDraw.color (0.0, 0.0, 0.0);
5620 filledrect (float x0) ph (float x1) (ph +. sh);
5621 let pw = pw +. float hx0 in
5622 filledrect pw (float (state.winh - sbh)) (pw +. sw) (float state.winh);
5625 let showsel () =
5626 match state.mstate with
5627 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5630 | Msel ((x0, y0), (x1, y1)) ->
5631 let identify opaque l px py = Some (opaque, l.pageno, px, py) in
5632 let o0,n0,px0,py0 = onppundermouse identify x0 y0 (~< E.s, -1, 0, 0) in
5633 let _o1,n1,px1,py1 = onppundermouse identify x1 y1 (~< E.s, -1, 0, 0) in
5634 if n0 != -1 && n0 = n1 then seltext o0 (px0, py0, px1, py1);
5637 let showrects = function [] -> () | rects ->
5638 Gl.enable `blend;
5639 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5640 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5641 List.iter
5642 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5643 List.iter (fun l ->
5644 if l.pageno = pageno
5645 then (
5646 let dx = float (l.pagedispx - l.pagex) in
5647 let dy = float (l.pagedispy - l.pagey) in
5648 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5649 Raw.sets_float state.vraw ~pos:0
5650 [| x0+.dx; y0+.dy;
5651 x1+.dx; y1+.dy;
5652 x3+.dx; y3+.dy;
5653 x2+.dx; y2+.dy |];
5654 GlArray.vertex `two state.vraw;
5655 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
5657 ) state.layout
5658 ) rects
5660 Gl.disable `blend;
5663 let display () =
5664 GlClear.color (scalecolor2 conf.bgcolor);
5665 GlClear.clear [`color];
5666 List.iter drawpage state.layout;
5667 let rects =
5668 match state.mode with
5669 | LinkNav (Ltexact (pageno, linkno)) ->
5670 begin match getopaque pageno with
5671 | Some opaque ->
5672 let dx = xadjsb () in
5673 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5674 let x0 = x0 + dx and x1 = x1 + dx in
5675 (pageno, 5, (
5676 float x0, float y0,
5677 float x1, float y0,
5678 float x1, float y1,
5679 float x0, float y1)
5680 ) :: state.rects
5681 | None -> state.rects
5683 | LinkNav (Ltgendir _) | LinkNav (Ltnotready _)
5684 | Birdseye _
5685 | Textentry _
5686 | View -> state.rects
5688 showrects rects;
5689 let rec postloop linkindexbase = function
5690 | l :: rest ->
5691 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5692 postloop linkindexbase rest
5693 | [] -> ()
5695 showsel ();
5696 postloop 0 state.layout;
5697 state.uioh#display;
5698 begin match state.mstate with
5699 | Mzoomrect ((x0, y0), (x1, y1)) ->
5700 Gl.enable `blend;
5701 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5702 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5703 filledrect (float x0) (float y0) (float x1) (float y1);
5704 Gl.disable `blend;
5705 | Msel _
5706 | Mpan _
5707 | Mscrolly | Mscrollx
5708 | Mzoom _
5709 | Mnone -> ()
5710 end;
5711 enttext ();
5712 scrollindicator ();
5713 Wsi.swapb ();
5716 let zoomrect x y x1 y1 =
5717 let x0 = min x x1
5718 and x1 = max x x1
5719 and y0 = min y y1 in
5720 gotoy (state.y + y0);
5721 state.anchor <- getanchor ();
5722 let zoom = (float state.w) /. float (x1 - x0) in
5723 let margin =
5724 let simple () =
5725 let adjw = wadjsb () + state.winw in
5726 if state.w < adjw
5727 then (adjw - state.w) / 2
5728 else 0
5730 match conf.fitmodel with
5731 | FitWidth | FitProportional -> simple ()
5732 | FitPage ->
5733 match conf.columns with
5734 | Csplit _ ->
5735 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
5736 | Cmulti _ | Csingle _ -> simple ()
5738 state.x <- (state.x + margin) - x0;
5739 setzoom zoom;
5740 resetmstate ();
5743 let annot inline x y =
5744 match unproject x y with
5745 | Some (opaque, n, ux, uy) ->
5746 let add text =
5747 addannot opaque ux uy text;
5748 wcmd "freepage %s" (~> opaque);
5749 Hashtbl.remove state.pagemap (n, state.gen);
5750 flushtiles ();
5751 gotoy state.y
5753 if inline
5754 then
5755 let ondone s = add s in
5756 let mode = state.mode in
5757 state.mode <- Textentry (
5758 ("annotation: ", E.s, None, textentry, ondone, true),
5759 fun _ -> state.mode <- mode);
5760 state.text <- E.s;
5761 enttext ();
5762 G.postRedisplay "annot"
5763 else
5764 add @@ getusertext E.s
5765 | _ -> ()
5768 let zoomblock x y =
5769 let g opaque l px py =
5770 match rectofblock opaque px py with
5771 | Some a ->
5772 let x0 = a.(0) -. 20. in
5773 let x1 = a.(1) +. 20. in
5774 let y0 = a.(2) -. 20. in
5775 let zoom = (float state.w) /. (x1 -. x0) in
5776 let pagey = getpagey l.pageno in
5777 gotoy_and_clear_text (pagey + truncate y0);
5778 state.anchor <- getanchor ();
5779 let margin = (state.w - l.pagew)/2 in
5780 state.x <- -truncate x0 - margin;
5781 setzoom zoom;
5782 None
5783 | None -> None
5785 match conf.columns with
5786 | Csplit _ ->
5787 showtext '!' "block zooming does not work properly in split columns mode"
5788 | Cmulti _ | Csingle _ -> onppundermouse g x y ()
5791 let scrollx x =
5792 let winw = wadjsb () + state.winw - 1 in
5793 let s = float x /. float winw in
5794 let destx = truncate (float (state.w + winw) *. s) in
5795 state.x <- winw - destx;
5796 gotoy_and_clear_text state.y;
5797 state.mstate <- Mscrollx;
5800 let scrolly y =
5801 let s = float y /. float state.winh in
5802 let desty = truncate (float (state.maxy - state.winh) *. s) in
5803 gotoy_and_clear_text desty;
5804 state.mstate <- Mscrolly;
5807 let viewmulticlick clicks x y mask =
5808 let g opaque l px py =
5809 let mark =
5810 match clicks with
5811 | 2 -> Mark_word
5812 | 3 -> Mark_line
5813 | 4 -> Mark_block
5814 | _ -> Mark_page
5816 if markunder opaque px py mark
5817 then (
5818 Some (fun () ->
5819 let dopipe cmd =
5820 match getopaque l.pageno with
5821 | None -> ()
5822 | Some opaque -> pipesel opaque cmd
5824 state.roam <- (fun () -> dopipe conf.paxcmd);
5825 if not (Wsi.withctrl mask) then dopipe conf.selcmd;
5828 else None
5830 G.postRedisplay "viewmulticlick";
5831 onppundermouse g x y (fun () -> showtext '!' "Nothing to select") ();
5834 let canselect () =
5835 match conf.columns with
5836 | Csplit _ -> false
5837 | Csingle _ | Cmulti _ -> conf.angle mod 360 = 0
5840 let viewmouse button down x y mask =
5841 match button with
5842 | n when (n == 4 || n == 5) && not down ->
5843 if Wsi.withctrl mask
5844 then (
5845 match state.mstate with
5846 | Mzoom (oldn, i) ->
5847 if oldn = n
5848 then (
5849 if i = 2
5850 then
5851 let incr =
5852 match n with
5853 | 5 ->
5854 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5855 | _ ->
5856 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5858 let zoom = conf.zoom -. incr in
5859 setzoom zoom;
5860 state.mstate <- Mzoom (n, 0);
5861 else
5862 state.mstate <- Mzoom (n, i+1);
5864 else state.mstate <- Mzoom (n, 0)
5866 | Msel _
5867 | Mpan _
5868 | Mscrolly | Mscrollx
5869 | Mzoomrect _
5870 | Mnone -> state.mstate <- Mzoom (n, 0)
5872 else (
5873 match state.autoscroll with
5874 | Some step -> setautoscrollspeed step (n=4)
5875 | None ->
5876 if conf.wheelbypage || conf.presentation
5877 then (
5878 if n = 4
5879 then prevpage ()
5880 else nextpage ()
5882 else
5883 let incr =
5884 if n = 4
5885 then -conf.scrollstep
5886 else conf.scrollstep
5888 let incr = incr * 2 in
5889 let y = clamp incr in
5890 gotoy_and_clear_text y
5893 | n when (n = 6 || n = 7) && not down && canpan () ->
5894 state.x <-
5895 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
5896 gotoy_and_clear_text state.y
5898 | 1 when Wsi.withshift mask ->
5899 state.mstate <- Mnone;
5900 if not down
5901 then (
5902 match unproject x y with
5903 | Some (_, pageno, ux, uy) ->
5904 let cmd = Printf.sprintf
5905 "%s %s %d %d %d"
5906 conf.stcmd state.path pageno ux uy
5908 addpid @@ popen cmd []
5909 | None -> ()
5912 | 1 when Wsi.withctrl mask ->
5913 if down
5914 then (
5915 Wsi.setcursor Wsi.CURSOR_FLEUR;
5916 state.mstate <- Mpan (x, y)
5918 else
5919 state.mstate <- Mnone
5921 | 3 ->
5922 if down
5923 then (
5924 if Wsi.withshift mask
5925 then (
5926 annot conf.annotinline x y;
5927 G.postRedisplay "addannot"
5929 else
5930 let p = (x, y) in
5931 Wsi.setcursor Wsi.CURSOR_CYCLE;
5932 state.mstate <- Mzoomrect (p, p)
5934 else (
5935 match state.mstate with
5936 | Mzoomrect ((x0, y0), _) ->
5937 if abs (x-x0) > 10 && abs (y - y0) > 10
5938 then zoomrect x0 y0 x y
5939 else (
5940 resetmstate ();
5941 G.postRedisplay "kill accidental zoom rect";
5943 | Msel _
5944 | Mpan _
5945 | Mscrolly | Mscrollx
5946 | Mzoom _
5947 | Mnone ->
5948 resetmstate ()
5951 | 1 when x > state.winw - vscrollw () ->
5952 if down
5953 then
5954 let _, position, sh = state.uioh#scrollph in
5955 if y > truncate position && y < truncate (position +. sh)
5956 then state.mstate <- Mscrolly
5957 else scrolly y
5958 else
5959 state.mstate <- Mnone
5961 | 1 when y > state.winh - hscrollh () ->
5962 if down
5963 then
5964 let _, position, sw = state.uioh#scrollpw in
5965 if x > truncate position && x < truncate (position +. sw)
5966 then state.mstate <- Mscrollx
5967 else scrollx x
5968 else
5969 state.mstate <- Mnone
5971 | 1 when state.bzoom -> if not down then zoomblock x y
5973 | 1 ->
5974 let dest = if down then getunder x y else Unone in
5975 begin match dest with
5976 | Ulinkgoto _
5977 | Ulinkuri _
5978 | Uremote _ | Uremotedest _
5979 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5980 gotounder dest
5982 | Unone when down ->
5983 Wsi.setcursor Wsi.CURSOR_FLEUR;
5984 state.mstate <- Mpan (x, y);
5986 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
5988 | Unone | Utext _ ->
5989 if down
5990 then (
5991 if canselect ()
5992 then (
5993 state.mstate <- Msel ((x, y), (x, y));
5994 G.postRedisplay "mouse select";
5997 else (
5998 match state.mstate with
5999 | Mnone -> ()
6001 | Mzoom _ | Mscrollx | Mscrolly ->
6002 state.mstate <- Mnone
6004 | Mzoomrect ((x0, y0), _) ->
6005 zoomrect x0 y0 x y
6007 | Mpan _ ->
6008 Wsi.setcursor Wsi.CURSOR_INHERIT;
6009 state.mstate <- Mnone
6011 | Msel ((x0, y0), (x1, y1)) ->
6012 let rec loop = function
6013 | [] -> ()
6014 | l :: rest ->
6015 let inside =
6016 let a0 = l.pagedispy in
6017 let a1 = a0 + l.pagevh in
6018 let b0 = l.pagedispx in
6019 let b1 = b0 + l.pagevw in
6020 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
6021 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
6023 if inside
6024 then
6025 match getopaque l.pageno with
6026 | Some opaque ->
6027 let dosel cmd () =
6028 match Unix.pipe () with
6029 | exception exn ->
6030 showtext '!'
6031 (Printf.sprintf
6032 "can not create sel pipe: %s"
6033 (exntos exn));
6034 | (r, w) ->
6035 let clo what fd =
6036 Ne.clo fd (fun msg ->
6037 dolog "%s close failed: %s" what msg)
6039 let pid =
6040 try popen cmd [r, 0; w, -1]
6041 with exn ->
6042 dolog "can not execute %S: %s"
6043 cmd (exntos exn);
6046 if pid > 0
6047 then (
6048 copysel w opaque;
6049 G.postRedisplay "copysel";
6051 else clo "Msel pipe/w" w;
6052 clo "Msel pipe/r" r;
6054 dosel conf.selcmd ();
6055 state.roam <- dosel conf.paxcmd;
6056 | None -> ()
6057 else loop rest
6059 loop state.layout;
6060 resetmstate ();
6064 | _ -> ()
6067 let birdseyemouse button down x y mask
6068 (conf, leftx, _, hooverpageno, anchor) =
6069 match button with
6070 | 1 when down ->
6071 let rec loop = function
6072 | [] -> ()
6073 | l :: rest ->
6074 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6075 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6076 then (
6077 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
6079 else loop rest
6081 loop state.layout
6082 | 3 -> ()
6083 | _ -> viewmouse button down x y mask
6086 let uioh = object
6087 method display = ()
6089 method key key mask =
6090 begin match state.mode with
6091 | Textentry textentry -> textentrykeyboard key mask textentry
6092 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6093 | View -> viewkeyboard key mask
6094 | LinkNav linknav -> linknavkeyboard key mask linknav
6095 end;
6096 state.uioh
6098 method button button bstate x y mask =
6099 begin match state.mode with
6100 | LinkNav _
6101 | View -> viewmouse button bstate x y mask
6102 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6103 | Textentry _ -> ()
6104 end;
6105 state.uioh
6107 method multiclick clicks x y mask =
6108 begin match state.mode with
6109 | LinkNav _
6110 | View -> viewmulticlick clicks x y mask
6111 | Birdseye _
6112 | Textentry _ -> ()
6113 end;
6114 state.uioh
6116 method motion x y =
6117 begin match state.mode with
6118 | Textentry _ -> ()
6119 | View | Birdseye _ | LinkNav _ ->
6120 match state.mstate with
6121 | Mzoom _ | Mnone -> ()
6123 | Mpan (x0, y0) ->
6124 let dx = x - x0
6125 and dy = y0 - y in
6126 state.mstate <- Mpan (x, y);
6127 if canpan ()
6128 then state.x <- panbound (state.x + dx);
6129 let y = clamp dy in
6130 gotoy_and_clear_text y
6132 | Msel (a, _) ->
6133 state.mstate <- Msel (a, (x, y));
6134 G.postRedisplay "motion select";
6136 | Mscrolly ->
6137 let y = min state.winh (max 0 y) in
6138 scrolly y
6140 | Mscrollx ->
6141 let x = min state.winw (max 0 x) in
6142 scrollx x
6144 | Mzoomrect (p0, _) ->
6145 state.mstate <- Mzoomrect (p0, (x, y));
6146 G.postRedisplay "motion zoomrect";
6147 end;
6148 state.uioh
6150 method pmotion x y =
6151 begin match state.mode with
6152 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6153 let rec loop = function
6154 | [] ->
6155 if hooverpageno != -1
6156 then (
6157 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6158 G.postRedisplay "pmotion birdseye no hoover";
6160 | l :: rest ->
6161 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6162 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6163 then (
6164 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6165 G.postRedisplay "pmotion birdseye hoover";
6167 else loop rest
6169 loop state.layout
6171 | Textentry _ -> ()
6173 | LinkNav _
6174 | View ->
6175 match state.mstate with
6176 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ -> ()
6177 | Mnone ->
6178 updateunder x y;
6179 if canselect ()
6180 then
6181 match conf.pax with
6182 | None -> ()
6183 | Some r ->
6184 let past, _, _ = !r in
6185 let now = now () in
6186 let delta = now -. past in
6187 if delta > 0.01
6188 then paxunder x y
6189 else r := (now, x, y)
6190 end;
6191 state.uioh
6193 method infochanged _ = ()
6195 method scrollph =
6196 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6197 let p, h =
6198 if maxy = 0
6199 then 0.0, float state.winh
6200 else scrollph state.y maxy
6202 vscrollw (), p, h
6204 method scrollpw =
6205 let winw = wadjsb () + state.winw in
6206 let fwinw = float winw in
6207 let sw =
6208 let sw = fwinw /. float state.w in
6209 let sw = fwinw *. sw in
6210 max sw (float conf.scrollh)
6212 let position =
6213 let maxx = state.w + winw in
6214 let x = winw - state.x in
6215 let percent = float x /. float maxx in
6216 (fwinw -. sw) *. percent
6218 hscrollh (), position, sw
6220 method modehash =
6221 let modename =
6222 match state.mode with
6223 | LinkNav _ -> "links"
6224 | Textentry _ -> "textentry"
6225 | Birdseye _ -> "birdseye"
6226 | View -> "view"
6228 findkeyhash conf modename
6230 method eformsgs = true
6231 method alwaysscrolly = false
6232 end;;
6234 let adderrmsg src msg =
6235 Buffer.add_string state.errmsgs msg;
6236 state.newerrmsgs <- true;
6237 G.postRedisplay src
6240 let adderrfmt src fmt =
6241 Format.ksprintf (fun s -> adderrmsg src s) fmt;
6244 let ract cmds =
6245 let cl = splitatspace cmds in
6246 let scan s fmt f =
6247 try Scanf.sscanf s fmt f
6248 with exn ->
6249 adderrfmt "remote exec"
6250 "error processing '%S': %s\n" cmds (exntos exn)
6252 match cl with
6253 | "reload" :: [] -> reload ()
6254 | "goto" :: args :: [] ->
6255 scan args "%u %f %f"
6256 (fun pageno x y ->
6257 let cmd, _ = state.geomcmds in
6258 if emptystr cmd
6259 then gotopagexy pageno x y
6260 else
6261 let f prevf () =
6262 gotopagexy pageno x y;
6263 prevf ()
6265 state.reprf <- f state.reprf
6267 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
6268 | "gotor" :: args :: [] ->
6269 scan args "%S %u"
6270 (fun filename pageno -> gotounder (Uremote (filename, pageno)))
6271 | "gotord" :: args :: [] ->
6272 scan args "%S %S"
6273 (fun filename dest -> gotounder (Uremotedest (filename, dest)))
6274 | "rect" :: args :: [] ->
6275 scan args "%u %u %f %f %f %f"
6276 (fun pageno color x0 y0 x1 y1 ->
6277 onpagerect pageno (fun w h ->
6278 let _,w1,h1,_ = getpagedim pageno in
6279 let sw = float w1 /. float w
6280 and sh = float h1 /. float h in
6281 let x0s = x0 *. sw
6282 and x1s = x1 *. sw
6283 and y0s = y0 *. sh
6284 and y1s = y1 *. sh in
6285 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
6286 debugrect rect;
6287 state.rects <- (pageno, color, rect) :: state.rects;
6288 G.postRedisplay "rect";
6291 | "activatewin" :: [] -> Wsi.activatewin ()
6292 | "quit" :: [] -> raise Quit
6293 | _ ->
6294 adderrfmt "remote command"
6295 "error processing remote command: %S\n" cmds;
6298 let remote =
6299 let scratch = Bytes.create 80 in
6300 let buf = Buffer.create 80 in
6301 fun fd ->
6302 let rec tempfr () =
6303 try Some (Unix.read fd scratch 0 80)
6304 with
6305 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
6306 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
6307 | exn -> raise exn
6309 match tempfr () with
6310 | None -> Some fd
6311 | Some n ->
6312 if n = 0
6313 then (
6314 Unix.close fd;
6315 if Buffer.length buf > 0
6316 then (
6317 let s = Buffer.contents buf in
6318 Buffer.clear buf;
6319 ract s;
6321 None
6323 else
6324 let rec eat ppos =
6325 let nlpos =
6327 let pos = Bytes.index_from scratch ppos '\n' in
6328 if pos >= n then -1 else pos
6329 with Not_found -> -1
6331 if nlpos >= 0
6332 then (
6333 Buffer.add_subbytes buf scratch ppos (nlpos-ppos);
6334 let s = Buffer.contents buf in
6335 Buffer.clear buf;
6336 ract s;
6337 eat (nlpos+1);
6339 else (
6340 Buffer.add_subbytes buf scratch ppos (n-ppos);
6341 Some fd
6343 in eat 0
6346 let remoteopen path =
6347 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
6348 with exn ->
6349 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
6350 None
6353 let () =
6354 let gcconfig = ref E.s in
6355 let trimcachepath = ref E.s in
6356 let rcmdpath = ref E.s in
6357 let pageno = ref None in
6358 let rootwid = ref 0 in
6359 let openlast = ref false in
6360 let nofc = ref false in
6361 let doreap = ref false in
6362 selfexec := Sys.executable_name;
6363 Arg.parse
6364 (Arg.align
6365 [("-p", Arg.String (fun s -> state.password <- s),
6366 "<password> Set password");
6368 ("-f", Arg.String
6369 (fun s ->
6370 Config.fontpath := s;
6371 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
6373 "<path> Set path to the user interface font");
6375 ("-c", Arg.String
6376 (fun s ->
6377 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
6378 Config.confpath := s),
6379 "<path> Set path to the configuration file");
6381 ("-last", Arg.Set openlast, " Open last document");
6383 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
6384 "<page-number> Jump to page");
6386 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6387 "<path> Set path to the trim cache file");
6389 ("-dest", Arg.String (fun s -> state.nameddest <- s),
6390 "<named-destination> Set named destination");
6392 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
6393 ("-cxack", Arg.Set cxack, " Cut corners");
6395 ("-remote", Arg.String (fun s -> rcmdpath := s),
6396 "<path> Set path to the remote commands source");
6398 ("-origin", Arg.String (fun s -> state.origin <- s),
6399 "<original-path> Set original path");
6401 ("-gc", Arg.Set_string gcconfig,
6402 "<script-path> Collect garbage with the help of a script");
6404 ("-nofc", Arg.Set nofc, " Do not use fontconfig");
6406 ("-v", Arg.Unit (fun () ->
6407 Printf.printf
6408 "%s\nconfiguration path: %s\n"
6409 (version ())
6410 Config.defconfpath
6412 exit 0), " Print version and exit");
6414 ("-embed", Arg.Set_int rootwid,
6415 "<window-id> Embed into window")
6418 (fun s -> state.path <- s)
6419 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6421 if !wtmode
6422 then selfexec := !selfexec ^ " -wtmode";
6424 let histmode = emptystr state.path && not !openlast in
6426 if not (Config.load !openlast)
6427 then prerr_endline "failed to load configuration";
6428 begin match !pageno with
6429 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
6430 | None -> ()
6431 end;
6433 if nonemptystr !gcconfig
6434 then (
6435 let c, s =
6436 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6437 | exception exn ->
6438 error "gc socketpair failed: %s" (exntos exn)
6439 | rw -> rw
6441 match addpid @@ popen !gcconfig [(c, 0); (c, 1)] with
6442 | exception exn ->
6443 error "failed to popen gc script: %s" (exntos exn);
6444 | _ ->
6445 Config.gc s s;
6446 exit 0
6449 let wsfd, winw, winh = Wsi.init (object (self)
6450 val mutable m_clicks = 0
6451 val mutable m_click_x = 0
6452 val mutable m_click_y = 0
6453 val mutable m_lastclicktime = infinity
6455 method private cleanup =
6456 state.roam <- noroam;
6457 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap
6458 method expose = G.postRedisplay"expose"
6459 method visible v =
6460 let name =
6461 match v with
6462 | Wsi.Unobscured -> "unobscured"
6463 | Wsi.PartiallyObscured -> "partiallyobscured"
6464 | Wsi.FullyObscured -> "fullyobscured"
6466 vlog "visibility change %s" name
6467 method display = display ()
6468 method map mapped = vlog "mappped %b" mapped
6469 method reshape w h =
6470 self#cleanup;
6471 reshape w h
6472 method mouse b d x y m =
6473 if d && canselect ()
6474 then (
6475 (* http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx *)
6476 m_click_x <- x;
6477 m_click_y <- y;
6478 if b = 1
6479 then (
6480 let t = now () in
6481 if abs x - m_click_x > 10
6482 || abs y - m_click_y > 10
6483 || abs_float (t -. m_lastclicktime) > 0.3
6484 then m_clicks <- 0;
6485 m_clicks <- m_clicks + 1;
6486 m_lastclicktime <- t;
6487 if m_clicks = 1
6488 then (
6489 self#cleanup;
6490 G.postRedisplay "cleanup";
6491 state.uioh <- state.uioh#button b d x y m;
6493 else state.uioh <- state.uioh#multiclick m_clicks x y m
6495 else (
6496 self#cleanup;
6497 m_clicks <- 0;
6498 m_lastclicktime <- infinity;
6499 state.uioh <- state.uioh#button b d x y m
6502 else (
6503 state.uioh <- state.uioh#button b d x y m
6505 method motion x y =
6506 state.mpos <- (x, y);
6507 state.uioh <- state.uioh#motion x y
6508 method pmotion x y =
6509 state.mpos <- (x, y);
6510 state.uioh <- state.uioh#pmotion x y
6511 method key k m =
6512 let mascm = m land (
6513 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6514 ) in
6515 let keyboard k m =
6516 let x = state.x and y = state.y in
6517 keyboard k m;
6518 if x != state.x || y != state.y then self#cleanup
6520 match state.keystate with
6521 | KSnone ->
6522 let km = k, mascm in
6523 begin
6524 match
6525 let modehash = state.uioh#modehash in
6526 try Hashtbl.find modehash km
6527 with Not_found ->
6528 try Hashtbl.find (findkeyhash conf "global") km
6529 with Not_found -> KMinsrt (k, m)
6530 with
6531 | KMinsrt (k, m) -> keyboard k m
6532 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6533 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6535 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6536 List.iter (fun (k, m) -> keyboard k m) insrt;
6537 state.keystate <- KSnone
6538 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6539 state.keystate <- KSinto (keys, insrt)
6540 | KSinto _ -> state.keystate <- KSnone
6542 method enter x y =
6543 state.mpos <- (x, y);
6544 state.uioh <- state.uioh#pmotion x y
6545 method leave = state.mpos <- (-1, -1)
6546 method winstate wsl = state.winstate <- wsl
6547 method quit = raise Quit
6548 end) !rootwid conf.cwinw conf.cwinh platform in
6550 state.wsfd <- wsfd;
6552 if not (
6553 List.exists GlMisc.check_extension
6554 [ "GL_ARB_texture_rectangle"
6555 ; "GL_EXT_texture_recangle"
6556 ; "GL_NV_texture_rectangle" ]
6558 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6560 if (
6561 let r = GlMisc.get_string `renderer in
6562 let p = "Mesa DRI Intel(" in
6563 let l = String.length p in
6564 String.length r > l && String.sub r 0 l = p
6566 then (
6567 defconf.sliceheight <- 1024;
6568 defconf.texcount <- 32;
6569 defconf.usepbo <- true;
6572 let cs, ss =
6573 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6574 | exception exn ->
6575 dolog "socketpair failed: %s" (exntos exn);
6576 exit 1
6577 | (r, w) ->
6578 cloexec r;
6579 cloexec w;
6580 r, w
6583 setcheckers conf.checkers;
6584 redirectstderr ();
6585 if conf.redirectstderr
6586 then
6587 at_exit (fun () ->
6588 let s = Bytes.cat
6589 (Buffer.to_bytes state.errmsgs)
6590 (match state.errfd with
6591 | Some fd ->
6592 let s = Bytes.create (80*24) in
6593 let n =
6595 let r, _, _ = Unix.select [fd] [] [] 0.0 in
6596 if List.mem fd r
6597 then Unix.read fd s 0 (Bytes.length s)
6598 else 0
6599 with _ -> 0
6601 if n = 0
6602 then E.b
6603 else Bytes.sub s 0 n
6604 | None -> E.b
6607 try ignore (Unix.write state.stderr s 0 (Bytes.length s))
6608 with exn -> print_endline (exntos exn)
6612 init cs (
6613 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
6614 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6615 !Config.fontpath, !trimcachepath,
6616 GlMisc.check_extension "GL_ARB_pixel_buffer_object",
6617 not !nofc
6619 List.iter GlArray.enable [`texture_coord; `vertex];
6620 state.ss <- ss;
6621 reshape ~firsttime:true winw winh;
6622 state.uioh <- uioh;
6623 if histmode
6624 then (
6625 Wsi.settitle "llpp (history)";
6626 enterhistmode ();
6628 else (
6629 state.text <- "Opening " ^ (mbtoutf8 state.path);
6630 opendoc state.path state.password;
6632 display ();
6633 Wsi.mapwin ();
6634 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
6636 let rec reap () =
6637 match Unix.waitpid [Unix.WNOHANG] ~-1 with
6638 | (exception (Unix.Unix_error (Unix.ECHILD, _, _))) -> ()
6639 | (exception exn) -> dolog "Unix.waitpid: %s" @@ exntos exn
6640 | 0, _ -> ()
6641 | _pid, _status -> reap ()
6643 Sys.set_signal Sys.sigchld (Sys.Signal_handle (fun _ -> doreap := true));
6645 let optrfd =
6646 ref (
6647 if nonemptystr !rcmdpath
6648 then remoteopen !rcmdpath
6649 else None
6653 let rec loop deadline =
6654 if !doreap
6655 then (
6656 doreap := false;
6657 reap ()
6659 let r =
6660 match state.errfd with
6661 | None -> [state.ss; state.wsfd]
6662 | Some fd -> [state.ss; state.wsfd; fd]
6664 let r =
6665 match !optrfd with
6666 | None -> r
6667 | Some fd -> fd :: r
6669 if state.redisplay
6670 then (
6671 state.redisplay <- false;
6672 display ();
6674 let timeout =
6675 let now = now () in
6676 if deadline > now
6677 then (
6678 if deadline = infinity
6679 then ~-.1.0
6680 else max 0.0 (deadline -. now)
6682 else 0.0
6684 let r, _, _ =
6685 try Unix.select r [] [] timeout
6686 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6688 begin match r with
6689 | [] ->
6690 state.ghyll None;
6691 let newdeadline =
6692 if state.ghyll == noghyll
6693 then
6694 match state.autoscroll with
6695 | Some step when step != 0 ->
6696 let y = state.y + step in
6697 let y =
6698 if y < 0
6699 then state.maxy
6700 else if y >= state.maxy then 0 else y
6702 gotoy y;
6703 if state.mode = View
6704 then state.text <- E.s;
6705 deadline +. 0.01
6706 | _ -> infinity
6707 else deadline +. 0.01
6709 loop newdeadline
6711 | l ->
6712 let rec checkfds = function
6713 | [] -> ()
6714 | fd :: rest when fd = state.ss ->
6715 let cmd = readcmd state.ss in
6716 act cmd;
6717 checkfds rest
6719 | fd :: rest when fd = state.wsfd ->
6720 Wsi.readresp fd;
6721 checkfds rest
6723 | fd :: rest when Some fd = !optrfd ->
6724 begin match remote fd with
6725 | None -> optrfd := remoteopen !rcmdpath;
6726 | opt -> optrfd := opt
6727 end;
6728 checkfds rest
6730 | fd :: rest ->
6731 let s = Bytes.create 80 in
6732 let n = tempfailureretry (Unix.read fd s 0) 80 in
6733 if conf.redirectstderr
6734 then (
6735 Buffer.add_substring state.errmsgs (Bytes.to_string s) 0 n;
6736 state.newerrmsgs <- true;
6737 state.redisplay <- true;
6739 else (
6740 prerr_string (String.sub (Bytes.to_string s) 0 n);
6741 flush stderr;
6743 checkfds rest
6745 checkfds l;
6746 let newdeadline =
6747 let deadline1 =
6748 if deadline = infinity
6749 then now () +. 0.01
6750 else deadline
6752 match state.autoscroll with
6753 | Some step when step != 0 -> deadline1
6754 | _ -> if state.ghyll == noghyll then infinity else deadline1
6756 loop newdeadline
6757 end;
6760 loop infinity;
6761 with Quit ->
6762 Config.save leavebirdseye;
6763 if hasunsavedchanges ()
6764 then save ();