Cosmetics
[llpp.git] / main.ml
blob1b44f6f32258ccbafc413fdeb0d62f0a73e09600
1 open Utils;;
2 open Config;;
4 exception Quit;;
6 external init : Unix.file_descr -> params -> unit = "ml_init";;
7 external seltext : opaque -> (int * int * int * int) -> unit = "ml_seltext";;
8 external hassel : opaque -> bool = "ml_hassel";;
9 external copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
10 external getpdimrect : int -> float array = "ml_getpdimrect";;
11 external whatsunder : opaque -> int -> int -> under = "ml_whatsunder";;
12 external markunder : opaque -> int -> int -> mark -> bool = "ml_markunder";;
13 external clearmark : opaque -> unit = "ml_clearmark";;
14 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
15 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
16 external measurestr : int -> string -> float = "ml_measure_string";;
17 external postprocess :
18 opaque -> int -> int -> int -> (int * string * int) -> int
19 = "ml_postprocess";;
20 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
21 external setaalevel : int -> unit = "ml_setaalevel";;
22 external realloctexts : int -> bool = "ml_realloctexts";;
23 external findlink : opaque -> linkdir -> link = "ml_findlink";;
24 external getlink : opaque -> int -> under = "ml_getlink";;
25 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
26 external getlinkcount : opaque -> int = "ml_getlinkcount";;
27 external findpwl : int -> int -> pagewithlinks = "ml_find_page_with_links";;
28 external getpbo : width -> height -> colorspace -> opaque = "ml_getpbo";;
29 external freepbo : opaque -> unit = "ml_freepbo";;
30 external unmappbo : opaque -> unit = "ml_unmappbo";;
31 external pbousable : unit -> bool = "ml_pbo_usable";;
32 external unproject : opaque -> int -> int -> (int * int) option
33 = "ml_unproject";;
34 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
35 external rectofblock : opaque -> int -> int -> float array option
36 = "ml_rectofblock";;
37 external begintiles : unit -> unit = "ml_begintiles";;
38 external endtiles : unit -> unit = "ml_endtiles";;
39 external addannot : opaque -> int -> int -> string -> unit = "ml_addannot";;
40 external modannot : opaque -> slinkindex -> string -> unit = "ml_modannot";;
41 external delannot : opaque -> slinkindex -> unit = "ml_delannot";;
42 external hasunsavedchanges : unit -> bool = "ml_hasunsavedchanges";;
43 external savedoc : string -> unit = "ml_savedoc";;
44 external getannotcontents : opaque -> slinkindex -> string
45 = "ml_getannotcontents";;
47 let reeenterhist = ref false;;
48 let selfexec = ref E.s;;
50 let drawstring size x y s =
51 Gl.enable `blend;
52 Gl.enable `texture_2d;
53 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
54 ignore (drawstr size x y s);
55 Gl.disable `blend;
56 Gl.disable `texture_2d;
59 let drawstring1 size x y s =
60 drawstr size x y s;
63 let drawstring2 size x y fmt =
64 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
67 let debugl l =
68 dolog "l %d dim=%d {" l.pageno l.pagedimno;
69 dolog " WxH %dx%d" l.pagew l.pageh;
70 dolog " vWxH %dx%d" l.pagevw l.pagevh;
71 dolog " pagex,y %d,%d" l.pagex l.pagey;
72 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
73 dolog " column %d" l.pagecol;
74 dolog "}";
77 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
78 dolog "rect {";
79 dolog " x0,y0=(% f, % f)" x0 y0;
80 dolog " x1,y1=(% f, % f)" x1 y1;
81 dolog " x2,y2=(% f, % f)" x2 y2;
82 dolog " x3,y3=(% f, % f)" x3 y3;
83 dolog "}";
86 let isbirdseye = function
87 | Birdseye _ -> true
88 | Textentry _
89 | View
90 | LinkNav _ -> false
93 let istextentry = function
94 | Textentry _ -> true
95 | Birdseye _
96 | View
97 | LinkNav _ -> false
100 let wtmode = ref false;;
101 let cxack = ref false;;
103 let pgscale h = truncate (float h *. conf.pgscale);;
105 let hscrollh () =
106 if not state.uioh#alwaysscrolly && (conf.scrollb land scrollbhv = 0)
107 || (state.x = 0 && state.w <= state.winw - conf.scrollbw)
108 then 0
109 else conf.scrollbw
112 let vscrollw () =
113 if not state.uioh#alwaysscrolly && (conf.scrollb land scrollbvv = 0)
114 then 0
115 else conf.scrollbw
118 let wadjsb () = -vscrollw ();;
119 let xadjsb () = if conf.leftscroll then vscrollw () else 0;;
121 let setfontsize n =
122 fstate.fontsize <- n;
123 fstate.wwidth <- measurestr fstate.fontsize "w";
124 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
127 let vlog fmt =
128 if conf.verbose
129 then
130 Printf.kprintf prerr_endline fmt
131 else
132 Printf.kprintf ignore fmt
135 let launchpath () =
136 if emptystr conf.pathlauncher
137 then print_endline state.path
138 else (
139 let command = Str.global_replace percentsre state.path conf.pathlauncher in
140 try addpid @@ popen command []
141 with exn ->
142 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
143 flush stderr;
147 let redirectstderr () =
148 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
149 if conf.redirectstderr
150 then
151 match Unix.pipe () with
152 | exception exn ->
153 dolog "failed to create stderr redirection pipes: %s" (exntos exn)
155 | (r, w) ->
156 begin match Unix.dup Unix.stderr with
157 | exception exn ->
158 dolog "failed to dup stderr: %s" (exntos exn);
159 Ne.clo r (clofail "pipe/r");
160 Ne.clo w (clofail "pipe/w");
162 | dupstderr ->
163 begin match Unix.dup2 w Unix.stderr with
164 | exception exn ->
165 dolog "failed to dup2 to stderr: %s" (exntos exn);
166 Ne.clo dupstderr (clofail "stderr duplicate");
167 Ne.clo r (clofail "redir pipe/r");
168 Ne.clo w (clofail "redir pipe/w");
170 | () ->
171 state.stderr <- dupstderr;
172 state.errfd <- Some r;
173 end;
175 else (
176 state.newerrmsgs <- false;
177 begin match state.errfd with
178 | Some fd ->
179 begin match Unix.dup2 state.stderr Unix.stderr with
180 | exception exn ->
181 dolog "failed to dup2 original stderr: %s" (exntos exn)
182 | () ->
183 Ne.clo fd (clofail "dup of stderr");
184 state.errfd <- None;
185 end;
186 | None -> ()
187 end;
188 prerr_string (Buffer.contents state.errmsgs);
189 flush stderr;
190 Buffer.clear state.errmsgs;
194 module G =
195 struct
196 let postRedisplay who =
197 if conf.verbose
198 then prerr_endline ("redisplay for " ^ who);
199 state.redisplay <- true;
201 end;;
203 let getopaque pageno =
204 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
205 with Not_found -> None
208 let putopaque pageno opaque =
209 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
212 let pagetranslatepoint l x y =
213 let dy = y - l.pagedispy in
214 let y = dy + l.pagey in
215 let dx = x - l.pagedispx in
216 let x = dx + l.pagex in
217 (x, y);
220 let onppundermouse g x y d =
221 let rec f = function
222 | l :: rest ->
223 begin match getopaque l.pageno with
224 | Some opaque ->
225 let x0 = l.pagedispx in
226 let x1 = x0 + l.pagevw in
227 let y0 = l.pagedispy in
228 let y1 = y0 + l.pagevh in
229 if y >= y0 && y <= y1 && x >= x0 && x <= x1
230 then
231 let px, py = pagetranslatepoint l x y in
232 match g opaque l px py with
233 | Some res -> res
234 | None -> f rest
235 else f rest
236 | _ ->
237 f rest
239 | [] -> d
241 f state.layout
244 let getunder x y =
245 let g opaque l px py =
246 if state.bzoom
247 then (
248 match rectofblock opaque px py with
249 | Some a ->
250 let rect = (a.(0),a.(2),a.(1),a.(2),a.(1),a.(3),a.(0),a.(3)) in
251 state.rects <- [l.pageno, l.pageno mod 3, rect];
252 G.postRedisplay "getunder";
253 | None -> ()
255 let under = whatsunder opaque px py in
256 if under = Unone then None else Some under
258 onppundermouse g x y Unone
261 let unproject x y =
262 let g opaque l x y =
263 match unproject opaque x y with
264 | Some (x, y) -> Some (Some (opaque, l.pageno, x, y))
265 | None -> None
267 onppundermouse g x y None;
270 let showtext c s =
271 state.text <- Printf.sprintf "%c%s" c s;
272 G.postRedisplay "showtext";
275 let pipesel opaque cmd =
276 if hassel opaque
277 then
278 match Unix.pipe () with
279 | exception exn ->
280 showtext '!'
281 (Printf.sprintf "pipesel can not create pipe: %s" (exntos exn));
282 | (r, w) ->
283 let doclose what fd =
284 Ne.clo fd (fun msg -> dolog "%s close failed: %s" what msg)
286 let pid =
287 try popen cmd [r, 0; w, -1]
288 with exn ->
289 dolog "can not execute %S: %s" cmd (exntos exn);
292 if pid > 0
293 then (
294 copysel w opaque;
295 G.postRedisplay "pipesel";
297 else doclose "pipesel pipe/w" w;
298 doclose "pipesel pipe/r" r;
301 let paxunder x y =
302 let g opaque l px py =
303 if markunder opaque px py conf.paxmark
304 then (
305 Some (fun () ->
306 match getopaque l.pageno with
307 | None -> ()
308 | Some opaque -> pipesel opaque conf.paxcmd
311 else None
313 G.postRedisplay "paxunder";
314 if conf.paxmark = Mark_page
315 then
316 List.iter (fun l ->
317 match getopaque l.pageno with
318 | None -> ()
319 | Some opaque -> clearmark opaque) state.layout;
320 state.roam <-
321 onppundermouse g x y (fun () -> showtext '!' "Whoopsie daisy");
324 let selstring s =
325 match Unix.pipe () with
326 | exception exn ->
327 showtext '!' (Printf.sprintf "pipe failed: %s" (exntos exn))
328 | (r, w) ->
329 let clo cap fd =
330 Ne.clo fd (fun msg ->
331 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
334 let pid =
335 try popen conf.selcmd [r, 0; w, -1]
336 with exn ->
337 showtext '!'
338 (Printf.sprintf "failed to execute %s: %s"
339 conf.selcmd (exntos exn));
342 if pid > 0
343 then (
345 let l = String.length s in
346 let bytes = Bytes.unsafe_of_string s in
347 let n = tempfailureretry (Unix.write w bytes 0) l in
348 if n != l
349 then
350 showtext '!'
351 (Printf.sprintf
352 "failed to write %d characters to sel pipe, wrote %d"
355 with exn ->
356 showtext '!'
357 (Printf.sprintf "failed to write to sel pipe: %s"
358 (exntos exn)
361 else dolog "%s" s;
362 clo "selstring pipe/r" r;
363 clo "selstring pipe/w" w;
366 let undertext = function
367 | Unone -> "none"
368 | Ulinkuri s -> s
369 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
370 | Utext s -> "font: " ^ s
371 | Uunexpected s -> "unexpected: " ^ s
372 | Ulaunch s -> "launch: " ^ s
373 | Unamed s -> "named: " ^ s
374 | Uremote (filename, pageno) ->
375 Printf.sprintf "%s: page %d" filename (pageno+1)
376 | Uremotedest (filename, destname) ->
377 Printf.sprintf "%s: destination %S" filename destname
378 | Uannotation (opaque, slinkindex) ->
379 "annotation: " ^ getannotcontents opaque slinkindex
382 let updateunder x y =
383 match getunder x y with
384 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
385 | Ulinkuri uri ->
386 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
387 Wsi.setcursor Wsi.CURSOR_INFO
388 | Ulinkgoto (pageno, _) ->
389 if conf.underinfo
390 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
391 Wsi.setcursor Wsi.CURSOR_INFO
392 | Utext s ->
393 if conf.underinfo then showtext 'f' ("ont: " ^ s);
394 Wsi.setcursor Wsi.CURSOR_TEXT
395 | Uunexpected s ->
396 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
397 Wsi.setcursor Wsi.CURSOR_INHERIT
398 | Ulaunch s ->
399 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
400 Wsi.setcursor Wsi.CURSOR_INHERIT
401 | Unamed s ->
402 if conf.underinfo then showtext 'n' ("amed: " ^ s);
403 Wsi.setcursor Wsi.CURSOR_INHERIT
404 | Uremote (filename, pageno) ->
405 if conf.underinfo then showtext 'r'
406 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
407 Wsi.setcursor Wsi.CURSOR_INFO
408 | Uremotedest (filename, destname) ->
409 if conf.underinfo then showtext 'r'
410 (Printf.sprintf "emote destination: %s (%S)" filename destname);
411 Wsi.setcursor Wsi.CURSOR_INFO
412 | Uannotation _ ->
413 if conf.underinfo then showtext 'a' "nnotation";
414 Wsi.setcursor Wsi.CURSOR_INFO
417 let showlinktype under =
418 if conf.underinfo && under != Unone
419 then showtext ' ' @@ undertext under
422 let intentry_with_suffix text key =
423 let c =
424 if key >= 32 && key < 127
425 then Char.chr key
426 else '\000'
428 match Char.lowercase c with
429 | '0' .. '9' ->
430 let text = addchar text c in
431 TEcont text
433 | 'k' | 'm' | 'g' ->
434 let text = addchar text c in
435 TEcont text
437 | _ ->
438 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
439 TEcont text
442 let readcmd fd =
443 let s = Bytes.create 4 in
444 let n = tempfailureretry (Unix.read fd s 0) 4 in
445 if n != 4 then error "incomplete read(len) = %d" n;
446 let len = (Char.code (Bytes.get s 0) lsl 24)
447 lor (Char.code (Bytes.get s 1) lsl 16)
448 lor (Char.code (Bytes.get s 2) lsl 8)
449 lor (Char.code (Bytes.get s 3))
451 let s = Bytes.create len in
452 let n = tempfailureretry (Unix.read fd s 0) len in
453 if n != len then error "incomplete read(data) %d vs %d" n len;
454 Bytes.to_string s
457 let wcmd fmt =
458 let b = Buffer.create 16 in
459 Buffer.add_string b "llll";
460 Printf.kbprintf
461 (fun b ->
462 let s = Buffer.to_bytes b in
463 let n = Bytes.length s in
464 let len = n - 4 in
465 (* dolog "wcmd %S" (String.sub s 4 len); *)
466 Bytes.set s 0 (Char.chr ((len lsr 24) land 0xff));
467 Bytes.set s 1 (Char.chr ((len lsr 16) land 0xff));
468 Bytes.set s 2 (Char.chr ((len lsr 8) land 0xff));
469 Bytes.set s 3 (Char.chr (len land 0xff));
470 let n' = tempfailureretry (Unix.write state.ss s 0) n in
471 if n' != n then error "write failed %d vs %d" n' n;
472 ) b fmt;
475 let nogeomcmds cmds =
476 match cmds with
477 | s, [] -> emptystr s
478 | _ -> false
481 let layoutN ((columns, coverA, coverB), b) y sh =
482 let sh = sh - (hscrollh ()) in
483 let wadj = wadjsb () in
484 let rec fold accu n =
485 if n = Array.length b
486 then accu
487 else
488 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
489 if (vy - y) > sh &&
490 (n = coverA - 1
491 || n = state.pagecount - coverB
492 || (n - coverA) mod columns = columns - 1)
493 then accu
494 else
495 let accu =
496 if vy + h > y
497 then
498 let pagey = max 0 (y - vy) in
499 let pagedispy = if pagey > 0 then 0 else vy - y in
500 let pagedispx, pagex =
501 let pdx =
502 if n = coverA - 1 || n = state.pagecount - coverB
503 then state.x + (wadj + state.winw - w) / 2
504 else dx + xoff + state.x
506 if pdx < 0
507 then 0, -pdx
508 else pdx, 0
510 let pagevw =
511 let vw = wadj + state.winw - pagedispx in
512 let pw = w - pagex in
513 min vw pw
515 let pagevh = min (h - pagey) (sh - pagedispy) in
516 if pagevw > 0 && pagevh > 0
517 then
518 let e =
519 { pageno = n
520 ; pagedimno = pdimno
521 ; pagew = w
522 ; pageh = h
523 ; pagex = pagex
524 ; pagey = pagey
525 ; pagevw = pagevw
526 ; pagevh = pagevh
527 ; pagedispx = pagedispx
528 ; pagedispy = pagedispy
529 ; pagecol = 0
532 e :: accu
533 else
534 accu
535 else
536 accu
538 fold accu (n+1)
540 if Array.length b = 0
541 then []
542 else List.rev (fold [] (page_of_y y))
545 let layoutS (columns, b) y sh =
546 let sh = sh - hscrollh () in
547 let wadj = wadjsb () in
548 let rec fold accu n =
549 if n = Array.length b
550 then accu
551 else
552 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
553 if (vy - y) > sh
554 then accu
555 else
556 let accu =
557 if vy + pageh > y
558 then
559 let x = xoff + state.x in
560 let pagey = max 0 (y - vy) in
561 let pagedispy = if pagey > 0 then 0 else vy - y in
562 let pagedispx, pagex =
563 if px = 0
564 then (
565 if x < 0
566 then 0, -x
567 else x, 0
569 else (
570 let px = px - x in
571 if px < 0
572 then -px, 0
573 else 0, px
576 let pagecolw = pagew/columns in
577 let pagedispx =
578 if pagecolw < state.winw
579 then pagedispx + ((wadj + state.winw - pagecolw) / 2)
580 else pagedispx
582 let pagevw =
583 let vw = wadj + state.winw - pagedispx in
584 let pw = pagew - pagex in
585 min vw pw
587 let pagevw = min pagevw pagecolw in
588 let pagevh = min (pageh - pagey) (sh - pagedispy) in
589 if pagevw > 0 && pagevh > 0
590 then
591 let e =
592 { pageno = n/columns
593 ; pagedimno = pdimno
594 ; pagew = pagew
595 ; pageh = pageh
596 ; pagex = pagex
597 ; pagey = pagey
598 ; pagevw = pagevw
599 ; pagevh = pagevh
600 ; pagedispx = pagedispx
601 ; pagedispy = pagedispy
602 ; pagecol = n mod columns
605 e :: accu
606 else
607 accu
608 else
609 accu
611 fold accu (n+1)
613 List.rev (fold [] 0)
616 let layout y sh =
617 if nogeomcmds state.geomcmds
618 then
619 match conf.columns with
620 | Csingle b -> layoutN ((1, 0, 0), b) y sh
621 | Cmulti c -> layoutN c y sh
622 | Csplit s -> layoutS s y sh
623 else []
626 let clamp incr =
627 let y = state.y + incr in
628 let y = max 0 y in
629 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
633 let itertiles l f =
634 let tilex = l.pagex mod conf.tilew in
635 let tiley = l.pagey mod conf.tileh in
637 let col = l.pagex / conf.tilew in
638 let row = l.pagey / conf.tileh in
640 let xadj = xadjsb () in
641 let rec rowloop row y0 dispy h =
642 if h = 0
643 then ()
644 else (
645 let dh = conf.tileh - y0 in
646 let dh = min h dh in
647 let rec colloop col x0 dispx w =
648 if w = 0
649 then ()
650 else (
651 let dw = conf.tilew - x0 in
652 let dw = min w dw in
653 let dispx' = xadj + dispx in
654 f col row dispx' dispy x0 y0 dw dh;
655 colloop (col+1) 0 (dispx+dw) (w-dw)
658 colloop col tilex l.pagedispx l.pagevw;
659 rowloop (row+1) 0 (dispy+dh) (h-dh)
662 if l.pagevw > 0 && l.pagevh > 0
663 then rowloop row tiley l.pagedispy l.pagevh;
666 let gettileopaque l col row =
667 let key =
668 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
670 try Some (Hashtbl.find state.tilemap key)
671 with Not_found -> None
674 let puttileopaque l col row gen colorspace angle opaque size elapsed =
675 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
676 Hashtbl.add state.tilemap key (opaque, size, elapsed)
679 let filledrect x0 y0 x1 y1 =
680 GlArray.disable `texture_coord;
681 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
682 GlArray.vertex `two state.vraw;
683 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
684 GlArray.enable `texture_coord;
687 let linerect x0 y0 x1 y1 =
688 GlArray.disable `texture_coord;
689 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y1; x1; y0 |];
690 GlArray.vertex `two state.vraw;
691 GlArray.draw_arrays `line_loop ~first:0 ~count:4;
692 GlArray.enable `texture_coord;
695 let drawtiles l color =
696 GlDraw.color color;
697 let wadj = wadjsb () in
698 begintiles ();
699 let f col row x y tilex tiley w h =
700 match gettileopaque l col row with
701 | Some (opaque, _, t) ->
702 let params = x, y, w, h, tilex, tiley in
703 if conf.invert
704 then GlTex.env (`mode `blend);
705 drawtile params opaque;
706 if conf.invert
707 then GlTex.env (`mode `modulate);
708 if conf.debug
709 then (
710 endtiles ();
711 let s = Printf.sprintf
712 "%d[%d,%d] %f sec"
713 l.pageno col row t
715 let w = measurestr fstate.fontsize s in
716 GlDraw.color (0.0, 0.0, 0.0);
717 filledrect (float (x-2))
718 (float (y-2))
719 (float (x+2) +. w)
720 (float (y + fstate.fontsize + 2));
721 GlDraw.color (1.0, 1.0, 1.0);
722 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
723 begintiles ();
726 | None ->
727 endtiles ();
728 let w =
729 if conf.leftscroll
730 then w
731 else
732 let lw = wadj + state.winw - x in
733 min lw w
734 and h =
735 let lh = state.winh - y in
736 min lh h
738 if conf.invert
739 then GlTex.env (`mode `blend);
740 begin match state.checkerstexid with
741 | Some id ->
742 Gl.enable `texture_2d;
743 GlTex.bind_texture ~target:`texture_2d id;
744 let x0 = float x
745 and y0 = float y
746 and x1 = float (x+w)
747 and y1 = float (y+h) in
749 let tw = float w /. 16.0
750 and th = float h /. 16.0 in
751 let tx0 = float tilex /. 16.0
752 and ty0 = float tiley /. 16.0 in
753 let tx1 = tx0 +. tw
754 and ty1 = ty0 +. th in
755 Raw.sets_float state.vraw ~pos:0
756 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
757 Raw.sets_float state.traw ~pos:0
758 [| tx0; ty0; tx0; ty1; tx1; ty0; tx1; ty1 |];
759 GlArray.vertex `two state.vraw;
760 GlArray.tex_coord `two state.traw;
761 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
762 Gl.disable `texture_2d;
764 | None ->
765 GlDraw.color (1.0, 1.0, 1.0);
766 filledrect (float x) (float y) (float (x+w)) (float (y+h));
767 end;
768 if conf.invert
769 then GlTex.env (`mode `modulate);
770 if w > 128 && h > fstate.fontsize + 10
771 then (
772 let c = if conf.invert then 1.0 else 0.0 in
773 GlDraw.color (c, c, c);
774 let c, r =
775 if conf.verbose
776 then (col*conf.tilew, row*conf.tileh)
777 else col, row
779 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
781 GlDraw.color color;
782 begintiles ();
784 itertiles l f;
785 endtiles ();
788 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
790 let tilevisible1 l x y =
791 let ax0 = l.pagex
792 and ax1 = l.pagex + l.pagevw
793 and ay0 = l.pagey
794 and ay1 = l.pagey + l.pagevh in
796 let bx0 = x
797 and by0 = y in
798 let bx1 = min (bx0 + conf.tilew) l.pagew
799 and by1 = min (by0 + conf.tileh) l.pageh in
801 let rx0 = max ax0 bx0
802 and ry0 = max ay0 by0
803 and rx1 = min ax1 bx1
804 and ry1 = min ay1 by1 in
806 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
807 nonemptyintersection
810 let tilevisible layout n x y =
811 let rec findpageinlayout m = function
812 | l :: rest when l.pageno = n ->
813 tilevisible1 l x y || (
814 match conf.columns with
815 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
816 | Csplit _
817 | Csingle _
818 | Cmulti _ -> false
820 | _ :: rest -> findpageinlayout 0 rest
821 | [] -> false
823 findpageinlayout 0 layout;
826 let tileready l x y =
827 tilevisible1 l x y &&
828 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
831 let tilepage n p layout =
832 let rec loop = function
833 | l :: rest ->
834 if l.pageno = n
835 then
836 let f col row _ _ _ _ _ _ =
837 if state.currently = Idle
838 then
839 match gettileopaque l col row with
840 | Some _ -> ()
841 | None ->
842 let x = col*conf.tilew
843 and y = row*conf.tileh in
844 let w =
845 let w = l.pagew - x in
846 min w conf.tilew
848 let h =
849 let h = l.pageh - y in
850 min h conf.tileh
852 let pbo =
853 if conf.usepbo
854 then getpbo w h conf.colorspace
855 else ~< "0"
857 wcmd "tile %s %d %d %d %d %s"
858 (~> p) x y w h (~> pbo);
859 state.currently <-
860 Tiling (
861 l, p, conf.colorspace, conf.angle,
862 state.gen, col, row, conf.tilew, conf.tileh
865 itertiles l f;
866 else
867 loop rest
869 | [] -> ()
871 if nogeomcmds state.geomcmds
872 then loop layout;
875 let preloadlayout y =
876 let y = if y < state.winh then 0 else y - state.winh in
877 let h = state.winh*3 in
878 layout y h;
881 let load pages =
882 let rec loop pages =
883 if state.currently != Idle
884 then ()
885 else
886 match pages with
887 | l :: rest ->
888 begin match getopaque l.pageno with
889 | None ->
890 wcmd "page %d %d" l.pageno l.pagedimno;
891 state.currently <- Loading (l, state.gen);
892 | Some opaque ->
893 tilepage l.pageno opaque pages;
894 loop rest
895 end;
896 | _ -> ()
898 if nogeomcmds state.geomcmds
899 then loop pages
902 let preload pages =
903 load pages;
904 if conf.preload && state.currently = Idle
905 then load (preloadlayout state.y);
908 let layoutready layout =
909 let rec fold all ls =
910 all && match ls with
911 | l :: rest ->
912 let seen = ref false in
913 let allvisible = ref true in
914 let foo col row _ _ _ _ _ _ =
915 seen := true;
916 allvisible := !allvisible &&
917 begin match gettileopaque l col row with
918 | Some _ -> true
919 | None -> false
922 itertiles l foo;
923 fold (!seen && !allvisible) rest
924 | [] -> true
926 let alltilesvisible = fold true layout in
927 alltilesvisible;
930 let gotoy y =
931 let y = bound y 0 state.maxy in
932 let y, layout, proceed =
933 match conf.maxwait with
934 | Some time when state.ghyll == noghyll ->
935 begin match state.throttle with
936 | None ->
937 let layout = layout y state.winh in
938 let ready = layoutready layout in
939 if not ready
940 then (
941 load layout;
942 state.throttle <- Some (layout, y, now ());
944 else G.postRedisplay "gotoy showall (None)";
945 y, layout, ready
946 | Some (_, _, started) ->
947 let dt = now () -. started in
948 if dt > time
949 then (
950 state.throttle <- None;
951 let layout = layout y state.winh in
952 load layout;
953 G.postRedisplay "maxwait";
954 y, layout, true
956 else -1, [], false
959 | _ ->
960 let layout = layout y state.winh in
961 if not !wtmode || layoutready layout
962 then G.postRedisplay "gotoy ready";
963 y, layout, true
965 if proceed
966 then (
967 state.y <- y;
968 state.layout <- layout;
969 begin match state.mode with
970 | LinkNav ln ->
971 begin match ln with
972 | Ltexact (pageno, linkno) ->
973 let rec loop = function
974 | [] ->
975 state.mode <- LinkNav (Ltgendir 0)
976 | l :: _ when l.pageno = pageno ->
977 begin match getopaque pageno with
978 | None -> state.mode <- LinkNav (Ltnotready (pageno, 0))
979 | Some opaque ->
980 let x0, y0, x1, y1 = getlinkrect opaque linkno in
981 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
982 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
983 then state.mode <- LinkNav (Ltgendir 0)
985 | _ :: rest -> loop rest
987 loop layout
988 | Ltnotready _ | Ltgendir _ -> ()
990 | Birdseye _
991 | Textentry _
992 | View -> ()
993 end;
994 begin match state.mode with
995 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
996 if not (pagevisible layout pageno)
997 then (
998 match state.layout with
999 | [] -> ()
1000 | l :: _ ->
1001 state.mode <- Birdseye (
1002 conf, leftx, l.pageno, hooverpageno, anchor
1005 | LinkNav lt ->
1006 begin match lt with
1007 | Ltnotready (_, dir)
1008 | Ltgendir dir ->
1009 let linknav =
1010 let rec loop = function
1011 | [] -> lt
1012 | l :: rest ->
1013 match getopaque l.pageno with
1014 | None -> Ltnotready (l.pageno, dir)
1015 | Some opaque ->
1016 let link =
1017 let ld =
1018 if dir = 0
1019 then LDfirstvisible (l.pagex, l.pagey, dir)
1020 else (
1021 if dir > 0 then LDfirst else LDlast
1024 findlink opaque ld
1026 match link with
1027 | Lnotfound -> loop rest
1028 | Lfound n ->
1029 showlinktype (getlink opaque n);
1030 Ltexact (l.pageno, n)
1032 loop state.layout
1034 state.mode <- LinkNav linknav
1035 | Ltexact _ -> ()
1037 | Textentry _
1038 | View -> ()
1039 end;
1040 preload layout;
1042 state.ghyll <- noghyll;
1043 if conf.updatecurs
1044 then (
1045 let mx, my = state.mpos in
1046 updateunder mx my;
1050 let conttiling pageno opaque =
1051 tilepage pageno opaque
1052 (if conf.preload then preloadlayout state.y else state.layout)
1055 let gotoy_and_clear_text y =
1056 if not conf.verbose then state.text <- E.s;
1057 gotoy y;
1060 let getanchory (n, top, dtop) =
1061 let y, h = getpageyh n in
1062 if conf.presentation
1063 then
1064 let ips = calcips h in
1065 y + truncate (top*.float h -. dtop*.float ips) + ips;
1066 else
1067 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1070 let gotoanchor anchor =
1071 gotoy (getanchory anchor);
1074 let addnav () =
1075 cbput state.hists.nav (getanchor ());
1078 let getnav dir =
1079 let anchor = cbgetc state.hists.nav dir in
1080 getanchory anchor;
1083 let gotoghyll1 single y =
1084 let scroll f n a b =
1085 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1086 let snake f a b =
1087 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1088 if f < a
1089 then s (float f /. float a)
1090 else (
1091 if f > b
1092 then 1.0 -. s ((float (f-b) /. float (n-b)))
1093 else 1.0
1096 snake f a b
1097 and summa n a b =
1098 let ins = float a *. 0.5
1099 and outs = float (n-b) *. 0.5 in
1100 let ones = b - a in
1101 ins +. outs +. float ones
1103 let rec set nab y sy =
1104 let (_N, _A, _B), y =
1105 if single
1106 then
1107 let scl = if y > sy then 2 else -2 in
1108 let _N, _, _ = nab in
1109 (_N,0,_N), y+conf.scrollstep*scl
1110 else nab,y in
1111 let sum = summa _N _A _B in
1112 let dy = float (y - sy) in
1113 state.ghyll <- (
1114 let rec gf n y1 o =
1115 if n >= _N
1116 then state.ghyll <- noghyll
1117 else
1118 let go n =
1119 let s = scroll n _N _A _B in
1120 let y1 = y1 +. ((s *. dy) /. sum) in
1121 gotoy_and_clear_text (truncate y1);
1122 state.ghyll <- gf (n+1) y1;
1124 match o with
1125 | None -> go n
1126 | Some y' when single -> set nab y' state.y
1127 | Some y' -> set (_N/2, 1, 1) y' state.y
1129 gf 0 (float state.y)
1132 match conf.ghyllscroll with
1133 | Some nab when not conf.presentation ->
1134 if state.ghyll == noghyll
1135 then set nab y state.y
1136 else state.ghyll (Some y)
1137 | _ ->
1138 gotoy_and_clear_text y
1141 let gotoghyll = gotoghyll1 false;;
1143 let gotopage n top =
1144 let y, h = getpageyh n in
1145 let y = y + (truncate (top *. float h)) in
1146 gotoghyll y
1149 let gotopage1 n top =
1150 let y = getpagey n in
1151 let y = y + top in
1152 gotoghyll y
1155 let invalidate s f =
1156 state.layout <- [];
1157 state.pdims <- [];
1158 state.rects <- [];
1159 state.rects1 <- [];
1160 match state.geomcmds with
1161 | ps, [] when emptystr ps ->
1162 f ();
1163 state.geomcmds <- s, [];
1165 | ps, [] ->
1166 state.geomcmds <- ps, [s, f];
1168 | ps, (s', _) :: rest when s' = s ->
1169 state.geomcmds <- ps, ((s, f) :: rest);
1171 | ps, cmds ->
1172 state.geomcmds <- ps, ((s, f) :: cmds);
1175 let flushpages () =
1176 Hashtbl.iter (fun _ opaque ->
1177 wcmd "freepage %s" (~> opaque);
1178 ) state.pagemap;
1179 Hashtbl.clear state.pagemap;
1182 let flushtiles () =
1183 if not (Queue.is_empty state.tilelru)
1184 then (
1185 Queue.iter (fun (k, p, s) ->
1186 wcmd "freetile %s" (~> p);
1187 state.memused <- state.memused - s;
1188 Hashtbl.remove state.tilemap k;
1189 ) state.tilelru;
1190 state.uioh#infochanged Memused;
1191 Queue.clear state.tilelru;
1193 load state.layout;
1196 let stateh h =
1197 let h = truncate (float h*.conf.zoom) in
1198 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
1199 h - d
1202 let opendoc path password =
1203 state.path <- path;
1204 state.password <- password;
1205 state.gen <- state.gen + 1;
1206 state.docinfo <- [];
1207 state.outlines <- [||];
1209 flushpages ();
1210 setaalevel conf.aalevel;
1211 let titlepath =
1212 if emptystr state.origin
1213 then path
1214 else state.origin
1216 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
1217 wcmd "open %d %d %s\000%s\000" (btod !wtmode) (btod !cxack) path password;
1218 invalidate "reqlayout"
1219 (fun () ->
1220 wcmd "reqlayout %d %d %d %s\000"
1221 conf.angle (FMTE.to_int conf.fitmodel)
1222 (stateh state.winh) state.nameddest
1226 let reload () =
1227 state.anchor <- getanchor ();
1228 opendoc state.path state.password;
1231 let scalecolor c =
1232 let c = c *. conf.colorscale in
1233 (c, c, c);
1236 let scalecolor2 (r, g, b) =
1237 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1240 let docolumns columns =
1241 let wadj = wadjsb () in
1242 match columns with
1243 | Csingle _ ->
1244 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1245 let wadj = wadjsb () in
1246 let rec loop pageno pdimno pdim y ph pdims =
1247 if pageno = state.pagecount
1248 then ()
1249 else
1250 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1251 match pdims with
1252 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1253 pdimno+1, pdim, rest
1254 | _ ->
1255 pdimno, pdim, pdims
1257 let x = max 0 (((wadj + state.winw - w) / 2) - xoff) in
1258 let y = y +
1259 (if conf.presentation
1260 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1261 else (if pageno = 0 then 0 else conf.interpagespace)
1264 a.(pageno) <- (pdimno, x, y, pdim);
1265 loop (pageno+1) pdimno pdim (y + h) h pdims
1267 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1268 conf.columns <- Csingle a;
1270 | Cmulti ((columns, coverA, coverB), _) ->
1271 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1272 let rec loop pageno pdimno pdim x y rowh pdims =
1273 let rec fixrow m = if m = pageno then () else
1274 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1275 if h < rowh
1276 then (
1277 let y = y + (rowh - h) / 2 in
1278 a.(m) <- (pdimno, x, y, pdim);
1280 fixrow (m+1)
1282 if pageno = state.pagecount
1283 then fixrow (((pageno - 1) / columns) * columns)
1284 else
1285 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1286 match pdims with
1287 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1288 pdimno+1, pdim, rest
1289 | _ ->
1290 pdimno, pdim, pdims
1292 let x, y, rowh' =
1293 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1294 then (
1295 let x = (wadj + state.winw - w) / 2 in
1296 let ips =
1297 if conf.presentation then calcips h else conf.interpagespace in
1298 x, y + ips + rowh, h
1300 else (
1301 if (pageno - coverA) mod columns = 0
1302 then (
1303 let x = max 0 (wadj + state.winw - state.w) / 2 in
1304 let y =
1305 if conf.presentation
1306 then
1307 let ips = calcips h in
1308 y + (if pageno = 0 then 0 else calcips rowh + ips)
1309 else
1310 y + (if pageno = 0 then 0 else conf.interpagespace)
1312 x, y + rowh, h
1314 else x, y, max rowh h
1317 let y =
1318 if pageno > 1 && (pageno - coverA) mod columns = 0
1319 then (
1320 let y =
1321 if pageno = columns && conf.presentation
1322 then (
1323 let ips = calcips rowh in
1324 for i = 0 to pred columns
1326 let (pdimno, x, y, pdim) = a.(i) in
1327 a.(i) <- (pdimno, x, y+ips, pdim)
1328 done;
1329 y+ips;
1331 else y
1333 fixrow (pageno - columns);
1336 else y
1338 a.(pageno) <- (pdimno, x, y, pdim);
1339 let x = x + w + xoff*2 + conf.interpagespace in
1340 loop (pageno+1) pdimno pdim x y rowh' pdims
1342 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1343 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1345 | Csplit (c, _) ->
1346 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1347 let rec loop pageno pdimno pdim y pdims =
1348 if pageno = state.pagecount
1349 then ()
1350 else
1351 let pdimno, ((_, w, h, _) as pdim), pdims =
1352 match pdims with
1353 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1354 pdimno+1, pdim, rest
1355 | _ ->
1356 pdimno, pdim, pdims
1358 let cw = w / c in
1359 let rec loop1 n x y =
1360 if n = c then y else (
1361 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1362 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1365 let y = loop1 0 0 y in
1366 loop (pageno+1) pdimno pdim y pdims
1368 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1369 conf.columns <- Csplit (c, a);
1372 let represent () =
1373 docolumns conf.columns;
1374 state.maxy <- calcheight ();
1375 if state.reprf == noreprf
1376 then (
1377 match state.mode with
1378 | Birdseye (_, _, pageno, _, _) ->
1379 let y, h = getpageyh pageno in
1380 let top = (state.winh - h) / 2 in
1381 gotoy (max 0 (y - top))
1382 | Textentry _
1383 | View
1384 | LinkNav _ -> gotoanchor state.anchor
1386 else (
1387 state.reprf ();
1388 state.reprf <- noreprf;
1392 let reshape w h =
1393 GlDraw.viewport ~x:0 ~y:0 ~w:w ~h:h;
1394 let firsttime = state.geomcmds == firstgeomcmds in
1395 if not firsttime && nogeomcmds state.geomcmds
1396 then state.anchor <- getanchor ();
1398 state.winw <- w;
1399 let w = wadjsb () + (truncate (float w *. conf.zoom)) in
1400 let w = max w 2 in
1401 state.winh <- h;
1402 setfontsize fstate.fontsize;
1403 GlMat.mode `modelview;
1404 GlMat.load_identity ();
1406 GlMat.mode `projection;
1407 GlMat.load_identity ();
1408 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1409 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1410 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
1412 let relx =
1413 if conf.zoom <= 1.0
1414 then 0.0
1415 else float state.x /. float state.w
1417 invalidate "geometry"
1418 (fun () ->
1419 state.w <- w;
1420 if not firsttime
1421 then state.x <- truncate (relx *. float w);
1422 let w =
1423 match conf.columns with
1424 | Csingle _ -> w
1425 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1426 | Csplit (c, _) -> w * c
1428 wcmd "geometry %d %d %d"
1429 w (stateh h) (FMTE.to_int conf.fitmodel)
1433 let enttext () =
1434 let len = String.length state.text in
1435 let x0 = xadjsb () in
1436 let drawstring s =
1437 let hscrollh =
1438 match state.mode with
1439 | Textentry _ | View | LinkNav _ ->
1440 let h, _, _ = state.uioh#scrollpw in
1442 | Birdseye _ -> 0
1444 let rect x w =
1445 filledrect x (float (state.winh - (fstate.fontsize + 4) - hscrollh))
1446 (x+.w) (float (state.winh - hscrollh))
1449 let w = float (wadjsb () + state.winw - 1) in
1450 if state.progress >= 0.0 && state.progress < 1.0
1451 then (
1452 GlDraw.color (0.3, 0.3, 0.3);
1453 let w1 = w *. state.progress in
1454 rect (float x0) w1;
1455 GlDraw.color (0.0, 0.0, 0.0);
1456 rect (float x0+.w1) (float x0+.w-.w1)
1458 else (
1459 GlDraw.color (0.0, 0.0, 0.0);
1460 rect (float x0) w;
1463 GlDraw.color (1.0, 1.0, 1.0);
1464 drawstring fstate.fontsize
1465 (if conf.leftscroll then x0 + 2 else x0 + if len > 0 then 8 else 2)
1466 (state.winh - hscrollh - 5) s;
1468 let s =
1469 match state.mode with
1470 | Textentry ((prefix, text, _, _, _, _), _) ->
1471 let s =
1472 if len > 0
1473 then
1474 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1475 else
1476 Printf.sprintf "%s%s_" prefix text
1480 | Birdseye _
1481 | View
1482 | LinkNav _ -> state.text
1484 let s =
1485 if state.newerrmsgs
1486 then (
1487 if not (istextentry state.mode) && state.uioh#eformsgs
1488 then
1489 let s1 = "(press 'e' to review error messasges)" in
1490 if nonemptystr s then s ^ " " ^ s1 else s1
1491 else s
1493 else s
1495 if nonemptystr s
1496 then drawstring s
1499 let gctiles () =
1500 let len = Queue.length state.tilelru in
1501 let layout = lazy (
1502 match state.throttle with
1503 | None ->
1504 if conf.preload
1505 then preloadlayout state.y
1506 else state.layout
1507 | Some (layout, _, _) ->
1508 layout
1509 ) in
1510 let rec loop qpos =
1511 if state.memused <= conf.memlimit
1512 then ()
1513 else (
1514 if qpos < len
1515 then
1516 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1517 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1518 let (_, pw, ph, _) = getpagedim n in
1520 gen = state.gen
1521 && colorspace = conf.colorspace
1522 && angle = conf.angle
1523 && pagew = pw
1524 && pageh = ph
1525 && (
1526 let x = col*conf.tilew
1527 and y = row*conf.tileh in
1528 tilevisible (Lazy.force_val layout) n x y
1530 then Queue.push lruitem state.tilelru
1531 else (
1532 freepbo p;
1533 wcmd "freetile %s" (~> p);
1534 state.memused <- state.memused - s;
1535 state.uioh#infochanged Memused;
1536 Hashtbl.remove state.tilemap k;
1538 loop (qpos+1)
1541 loop 0
1544 let onpagerect pageno f =
1545 let b =
1546 match conf.columns with
1547 | Cmulti (_, b) -> b
1548 | Csingle b -> b
1549 | Csplit (_, b) -> b
1551 if pageno >= 0 && pageno < Array.length b
1552 then
1553 let (_, _, _, (w, h, _, _)) = b.(pageno) in
1554 f w h
1557 let gotopagexy1 pageno x y =
1558 let _,w1,h1,leftx = getpagedim pageno in
1559 let top = y /. (float h1) in
1560 let left = x /. (float w1) in
1561 let py, w, h = getpageywh pageno in
1562 let wh = state.winh - hscrollh () in
1563 let x = left *. (float w) in
1564 let x = leftx + state.x + truncate x in
1565 let wadj = wadjsb () in
1566 let sx =
1567 if x < 0 || x >= wadj + state.winw
1568 then state.x - x
1569 else state.x
1571 let pdy = truncate (top *. float h) in
1572 let y' = py + pdy in
1573 let dy = y' - state.y in
1574 let sy =
1575 if x != state.x || not (dy > 0 && dy < wh)
1576 then (
1577 if conf.presentation
1578 then
1579 if abs (py - y') > wh
1580 then y'
1581 else py
1582 else y';
1584 else state.y
1586 if state.x != sx || state.y != sy
1587 then (
1588 let x, y =
1589 if !wtmode
1590 then (
1591 let ww = wadj + state.winw in
1592 let qx = sx / ww
1593 and qy = pdy / wh in
1594 let x = qx * ww
1595 and y = py + qy * wh in
1596 let x = if -x + ww > w1 then -(w1-ww) else x
1597 and y' = if y + wh > state.maxy then state.maxy - wh else y in
1598 let y =
1599 if conf.presentation
1600 then
1601 if abs (py - y') > wh
1602 then y'
1603 else py
1604 else y';
1606 (x, y)
1608 else (sx, sy)
1610 state.x <- x;
1611 gotoy_and_clear_text y;
1613 else gotoy_and_clear_text state.y;
1616 let gotopagexy pageno x y =
1617 match state.mode with
1618 | Birdseye _ -> gotopage pageno 0.0
1619 | Textentry _
1620 | View
1621 | LinkNav _ -> gotopagexy1 pageno x y
1624 let getpassword () =
1625 let passcmd = getenvwithdef "LLPP_ASKPASS" conf.passcmd in
1626 if emptystr passcmd
1627 then E.s
1628 else
1629 match Unix.open_process_in passcmd with
1630 | (exception exn) ->
1631 showtext '!'
1632 (Printf.sprintf
1633 "getpassword: open_process_in failed: %s" (exntos exn));
1635 | ic ->
1636 let s = try input_line ic with End_of_file -> E.s in
1637 let s =
1638 match Unix.close_process_in ic with
1639 | (exception exn) ->
1640 showtext '!'
1641 (Printf.sprintf "getpassword: close_process_in failed: %s"
1642 (exntos exn));
1644 | _ -> s
1649 let act cmds =
1650 (* dolog "%S" cmds; *)
1651 let cl = splitatspace cmds in
1652 let scan s fmt f =
1653 try Scanf.sscanf s fmt f
1654 with exn ->
1655 dolog "error processing '%S': %s" cmds (exntos exn);
1656 exit 1
1658 let addoutline outline =
1659 match state.currently with
1660 | Outlining outlines ->
1661 state.currently <- Outlining (outline :: outlines)
1662 | Idle -> state.currently <- Outlining [outline]
1663 | Loading _
1664 | Tiling _ ->
1665 dolog "invalid outlining state";
1666 logcurrently state.currently
1668 match cl with
1669 | "clear" :: [] ->
1670 state.uioh#infochanged Pdim;
1671 state.pdims <- [];
1673 | "clearrects" :: [] ->
1674 state.rects <- state.rects1;
1675 G.postRedisplay "clearrects";
1677 | "continue" :: args :: [] ->
1678 let n = scan args "%u" (fun n -> n) in
1679 state.pagecount <- n;
1680 begin match state.currently with
1681 | Outlining l ->
1682 state.currently <- Idle;
1683 state.outlines <- Array.of_list (List.rev l)
1684 | Idle
1685 | Loading _
1686 | Tiling _ -> ()
1687 end;
1689 let cur, cmds = state.geomcmds in
1690 if emptystr cur
1691 then failwith "umpossible";
1693 begin match List.rev cmds with
1694 | [] ->
1695 state.geomcmds <- E.s, [];
1696 state.throttle <- None;
1697 represent ();
1698 | (s, f) :: rest ->
1699 f ();
1700 state.geomcmds <- s, List.rev rest;
1701 end;
1702 if conf.maxwait = None && not !wtmode
1703 then G.postRedisplay "continue";
1705 | "msg" :: args :: [] ->
1706 showtext ' ' args
1708 | "vmsg" :: args :: [] ->
1709 if conf.verbose
1710 then showtext ' ' args
1712 | "emsg" :: args :: [] ->
1713 Buffer.add_string state.errmsgs args;
1714 state.newerrmsgs <- true;
1715 G.postRedisplay "error message"
1717 | "progress" :: args :: [] ->
1718 let progress, text =
1719 scan args "%f %n"
1720 (fun f pos ->
1721 f, String.sub args pos (String.length args - pos))
1723 state.text <- text;
1724 state.progress <- progress;
1725 G.postRedisplay "progress"
1727 | "firstmatch" :: 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 let y = (getpagey pageno) + truncate y0 in
1739 addnav ();
1740 gotoy y;
1741 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
1743 | "match" :: args :: [] ->
1744 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1745 scan args "%u %d %f %f %f %f %f %f %f %f"
1746 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1747 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1749 let xoff = float (xadjsb ()) in
1750 let x0 = x0 +. xoff
1751 and x1 = x1 +. xoff
1752 and x2 = x2 +. xoff
1753 and x3 = x3 +. xoff in
1754 state.rects1 <-
1755 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1757 | "page" :: args :: [] ->
1758 let pageopaques, t = scan args "%s %f" (fun p t -> p, t) in
1759 let pageopaque = ~< pageopaques in
1760 begin match state.currently with
1761 | Loading (l, gen) ->
1762 vlog "page %d took %f sec" l.pageno t;
1763 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1764 begin match state.throttle with
1765 | None ->
1766 let preloadedpages =
1767 if conf.preload
1768 then preloadlayout state.y
1769 else state.layout
1771 let evict () =
1772 let set =
1773 List.fold_left (fun s l -> IntSet.add l.pageno s)
1774 IntSet.empty preloadedpages
1776 let evictedpages =
1777 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1778 if not (IntSet.mem pageno set)
1779 then (
1780 wcmd "freepage %s" (~> opaque);
1781 key :: accu
1783 else accu
1784 ) state.pagemap []
1786 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1788 evict ();
1789 state.currently <- Idle;
1790 if gen = state.gen
1791 then (
1792 tilepage l.pageno pageopaque state.layout;
1793 load state.layout;
1794 load preloadedpages;
1795 let visible = pagevisible state.layout l.pageno in
1796 if visible
1797 then (
1798 match state.mode with
1799 | LinkNav (Ltnotready (pageno, dir)) ->
1800 if pageno = l.pageno
1801 then (
1802 let link =
1803 let ld =
1804 if dir = 0
1805 then LDfirstvisible (l.pagex, l.pagey, dir)
1806 else (
1807 if dir > 0 then LDfirst else LDlast
1810 findlink pageopaque ld
1812 match link with
1813 | Lnotfound -> ()
1814 | Lfound n ->
1815 showlinktype (getlink pageopaque n);
1816 state.mode <- LinkNav (Ltexact (l.pageno, n))
1818 | LinkNav (Ltgendir _)
1819 | LinkNav (Ltexact _)
1820 | View
1821 | Birdseye _
1822 | Textentry _ -> ()
1825 if visible && layoutready state.layout
1826 then (
1827 G.postRedisplay "page";
1831 | Some (layout, _, _) ->
1832 state.currently <- Idle;
1833 tilepage l.pageno pageopaque layout;
1834 load state.layout
1835 end;
1837 | Idle
1838 | Tiling _
1839 | Outlining _ ->
1840 dolog "Inconsistent loading state";
1841 logcurrently state.currently;
1842 exit 1
1845 | "tile" :: args :: [] ->
1846 let (x, y, opaques, size, t) =
1847 scan args "%u %u %s %u %f"
1848 (fun x y p size t -> (x, y, p, size, t))
1850 let opaque = ~< opaques in
1851 begin match state.currently with
1852 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1853 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1855 unmappbo opaque;
1856 if tilew != conf.tilew || tileh != conf.tileh
1857 then (
1858 wcmd "freetile %s" (~> opaque);
1859 state.currently <- Idle;
1860 load state.layout;
1862 else (
1863 puttileopaque l col row gen cs angle opaque size t;
1864 state.memused <- state.memused + size;
1865 state.uioh#infochanged Memused;
1866 gctiles ();
1867 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1868 opaque, size) state.tilelru;
1870 let layout =
1871 match state.throttle with
1872 | None -> state.layout
1873 | Some (layout, _, _) -> layout
1876 state.currently <- Idle;
1877 if gen = state.gen
1878 && conf.colorspace = cs
1879 && conf.angle = angle
1880 && tilevisible layout l.pageno x y
1881 then conttiling l.pageno pageopaque;
1883 begin match state.throttle with
1884 | None ->
1885 preload state.layout;
1886 if gen = state.gen
1887 && conf.colorspace = cs
1888 && conf.angle = angle
1889 && tilevisible state.layout l.pageno x y
1890 && (not !wtmode || layoutready state.layout)
1891 then G.postRedisplay "tile nothrottle";
1893 | Some (layout, y, _) ->
1894 let ready = layoutready layout in
1895 if ready
1896 then (
1897 state.y <- y;
1898 state.layout <- layout;
1899 state.throttle <- None;
1900 G.postRedisplay "throttle";
1902 else load layout;
1903 end;
1906 | Idle
1907 | Loading _
1908 | Outlining _ ->
1909 dolog "Inconsistent tiling state";
1910 logcurrently state.currently;
1911 exit 1
1914 | "pdim" :: args :: [] ->
1915 let (n, w, h, _) as pdim =
1916 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1918 let pdim =
1919 match conf.fitmodel with
1920 | FitWidth -> pdim
1921 | FitPage | FitProportional ->
1922 match conf.columns with
1923 | Csplit _ -> (n, w, h, 0)
1924 | Csingle _ | Cmulti _ -> pdim
1926 state.uioh#infochanged Pdim;
1927 state.pdims <- pdim :: state.pdims
1929 | "o" :: args :: [] ->
1930 let (l, n, t, h, pos) =
1931 scan args "%u %u %d %u %n"
1932 (fun l n t h pos -> l, n, t, h, pos)
1934 let s = String.sub args pos (String.length args - pos) in
1935 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
1937 | "ou" :: args :: [] ->
1938 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
1939 let s = String.sub args pos len in
1940 let pos2 = pos + len + 1 in
1941 let uri = String.sub args pos2 (String.length args - pos2) in
1942 addoutline (s, l, Ouri uri)
1944 | "on" :: args :: [] ->
1945 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
1946 let s = String.sub args pos (String.length args - pos) in
1947 addoutline (s, l, Onone)
1949 | "a" :: args :: [] ->
1950 let (n, l, t) =
1951 scan args "%u %d %d" (fun n l t -> n, l, t)
1953 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
1955 | "info" :: args :: [] ->
1956 let pos = nindex args '\t' in
1957 if pos >= 0 && String.sub args 0 pos = "Title"
1958 then
1959 let s = String.sub args (pos+1) @@ String.length args - pos - 1 in
1960 conf.title <- s;
1961 Wsi.settitle s;
1962 state.docinfo <- (1, args) :: state.docinfo
1964 | "infoend" :: [] ->
1965 state.uioh#infochanged Docinfo;
1966 state.docinfo <- List.rev state.docinfo
1968 | "pass" :: l ->
1969 if l = "fail" :: []
1970 then Wsi.settitle "Wrong password";
1971 let password = getpassword () in
1972 if password == E.s
1973 then error "document is password protected"
1974 else opendoc state.path password
1976 | _ ->
1977 error "unknown cmd `%S'" cmds
1980 let onhist cb =
1981 let rc = cb.rc in
1982 let action = function
1983 | HCprev -> cbget cb ~-1
1984 | HCnext -> cbget cb 1
1985 | HCfirst -> cbget cb ~-(cb.rc)
1986 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1987 and cancel () = cb.rc <- rc
1988 in (action, cancel)
1991 let search pattern forward =
1992 match conf.columns with
1993 | Csplit _ ->
1994 showtext '!' "searching does not work properly in split columns mode"
1995 | Csingle _
1996 | Cmulti _ ->
1997 if nonemptystr pattern
1998 then
1999 let pn, py =
2000 match state.layout with
2001 | [] -> 0, 0
2002 | l :: _ ->
2003 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2005 wcmd "search %d %d %d %d,%s\000"
2006 (btod conf.icase) pn py (btod forward) pattern;
2009 let intentry text key =
2010 let c =
2011 if key >= 32 && key < 127
2012 then Char.chr key
2013 else '\000'
2015 match c with
2016 | '0' .. '9' ->
2017 let text = addchar text c in
2018 TEcont text
2020 | _ ->
2021 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2022 TEcont text
2025 let linknentry text key =
2026 let c =
2027 if key >= 32 && key < 127
2028 then Char.chr key
2029 else '\000'
2031 match c with
2032 | 'a' .. 'z' ->
2033 let text = addchar text c in
2034 TEcont text
2036 | _ ->
2037 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2038 TEcont text
2041 let linkndone f s =
2042 if nonemptystr s
2043 then (
2044 let n =
2045 let l = String.length s in
2046 let rec loop pos n = if pos = l then n else
2047 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2048 loop (pos+1) (n*26 + m)
2049 in loop 0 0
2051 let rec loop n = function
2052 | [] -> ()
2053 | l :: rest ->
2054 match getopaque l.pageno with
2055 | None -> loop n rest
2056 | Some opaque ->
2057 let m = getlinkcount opaque in
2058 if n < m
2059 then (
2060 let under = getlink opaque n in
2061 f under
2063 else loop (n-m) rest
2065 loop n state.layout;
2069 let textentry text key =
2070 if key land 0xff00 = 0xff00
2071 then TEcont text
2072 else TEcont (text ^ toutf8 key)
2075 let reqlayout angle fitmodel =
2076 match state.throttle with
2077 | None ->
2078 if nogeomcmds state.geomcmds
2079 then state.anchor <- getanchor ();
2080 conf.angle <- angle mod 360;
2081 if conf.angle != 0
2082 then (
2083 match state.mode with
2084 | LinkNav _ -> state.mode <- View
2085 | Birdseye _
2086 | Textentry _
2087 | View -> ()
2089 conf.fitmodel <- fitmodel;
2090 invalidate "reqlayout"
2091 (fun () ->
2092 wcmd "reqlayout %d %d %d"
2093 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
2095 | _ -> ()
2098 let settrim trimmargins trimfuzz =
2099 if nogeomcmds state.geomcmds
2100 then state.anchor <- getanchor ();
2101 conf.trimmargins <- trimmargins;
2102 conf.trimfuzz <- trimfuzz;
2103 let x0, y0, x1, y1 = trimfuzz in
2104 invalidate "settrim"
2105 (fun () ->
2106 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2107 flushpages ();
2110 let setzoom zoom =
2111 match state.throttle with
2112 | None ->
2113 let zoom = max 0.0001 zoom in
2114 if zoom <> conf.zoom
2115 then (
2116 state.prevzoom <- (conf.zoom, state.x);
2117 conf.zoom <- zoom;
2118 reshape state.winw state.winh;
2119 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2122 | Some (layout, y, started) ->
2123 let time =
2124 match conf.maxwait with
2125 | None -> 0.0
2126 | Some t -> t
2128 let dt = now () -. started in
2129 if dt > time
2130 then (
2131 state.y <- y;
2132 load layout;
2136 let setcolumns mode columns coverA coverB =
2137 state.prevcolumns <- Some (conf.columns, conf.zoom);
2138 if columns < 0
2139 then (
2140 if isbirdseye mode
2141 then showtext '!' "split mode doesn't work in bird's eye"
2142 else (
2143 conf.columns <- Csplit (-columns, E.a);
2144 state.x <- 0;
2145 conf.zoom <- 1.0;
2148 else (
2149 if columns < 2
2150 then (
2151 conf.columns <- Csingle E.a;
2152 state.x <- 0;
2153 setzoom 1.0;
2155 else (
2156 conf.columns <- Cmulti ((columns, coverA, coverB), E.a);
2157 conf.zoom <- 1.0;
2160 reshape state.winw state.winh;
2163 let resetmstate () =
2164 state.mstate <- Mnone;
2165 Wsi.setcursor Wsi.CURSOR_INHERIT;
2168 let enterbirdseye () =
2169 let zoom = float conf.thumbw /. float state.winw in
2170 let birdseyepageno =
2171 let cy = state.winh / 2 in
2172 let fold = function
2173 | [] -> 0
2174 | l :: rest ->
2175 let rec fold best = function
2176 | [] -> best.pageno
2177 | l :: rest ->
2178 let d = cy - (l.pagedispy + l.pagevh/2)
2179 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2180 if abs d < abs dbest
2181 then fold l rest
2182 else best.pageno
2183 in fold l rest
2185 fold state.layout
2187 state.mode <- Birdseye (
2188 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2190 resetmstate ();
2191 conf.zoom <- zoom;
2192 conf.presentation <- false;
2193 conf.interpagespace <- 10;
2194 conf.hlinks <- false;
2195 conf.fitmodel <- FitPage;
2196 state.x <- 0;
2197 conf.maxwait <- None;
2198 conf.columns <- (
2199 match conf.beyecolumns with
2200 | Some c ->
2201 conf.zoom <- 1.0;
2202 Cmulti ((c, 0, 0), E.a)
2203 | None -> Csingle E.a
2205 if conf.verbose
2206 then
2207 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2208 (100.0*.zoom)
2209 else
2210 state.text <- E.s
2212 reshape state.winw state.winh;
2215 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2216 state.mode <- View;
2217 conf.zoom <- c.zoom;
2218 conf.presentation <- c.presentation;
2219 conf.interpagespace <- c.interpagespace;
2220 conf.maxwait <- c.maxwait;
2221 conf.hlinks <- c.hlinks;
2222 conf.fitmodel <- c.fitmodel;
2223 conf.beyecolumns <- (
2224 match conf.columns with
2225 | Cmulti ((c, _, _), _) -> Some c
2226 | Csingle _ -> None
2227 | Csplit _ -> failwith "leaving bird's eye split mode"
2229 conf.columns <- (
2230 match c.columns with
2231 | Cmulti (c, _) -> Cmulti (c, E.a)
2232 | Csingle _ -> Csingle E.a
2233 | Csplit (c, _) -> Csplit (c, E.a)
2235 if conf.verbose
2236 then
2237 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2238 (100.0*.conf.zoom)
2240 reshape state.winw state.winh;
2241 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2242 state.x <- leftx;
2245 let togglebirdseye () =
2246 match state.mode with
2247 | Birdseye vals -> leavebirdseye vals true
2248 | View -> enterbirdseye ()
2249 | Textentry _
2250 | LinkNav _ -> ()
2253 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2254 let pageno = max 0 (pageno - incr) in
2255 let rec loop = function
2256 | [] -> gotopage1 pageno 0
2257 | l :: _ when l.pageno = pageno ->
2258 if l.pagedispy >= 0 && l.pagey = 0
2259 then G.postRedisplay "upbirdseye"
2260 else gotopage1 pageno 0
2261 | _ :: rest -> loop rest
2263 loop state.layout;
2264 state.text <- E.s;
2265 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2268 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2269 let pageno = min (state.pagecount - 1) (pageno + incr) in
2270 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2271 let rec loop = function
2272 | [] ->
2273 let y, h = getpageyh pageno in
2274 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2275 gotoy (clamp dy)
2276 | l :: _ when l.pageno = pageno ->
2277 if l.pagevh != l.pageh
2278 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2279 else G.postRedisplay "downbirdseye"
2280 | _ :: rest -> loop rest
2282 loop state.layout;
2283 state.text <- E.s;
2286 let optentry mode _ key =
2287 let btos b = if b then "on" else "off" in
2288 if key >= 32 && key < 127
2289 then
2290 let c = Char.chr key in
2291 match c with
2292 | 's' ->
2293 let ondone s =
2294 try conf.scrollstep <- int_of_string s with exc ->
2295 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2297 TEswitch ("scroll step: ", E.s, None, intentry, ondone, true)
2299 | 'A' ->
2300 let ondone s =
2302 conf.autoscrollstep <- boundastep state.winh (int_of_string s);
2303 if state.autoscroll <> None
2304 then state.autoscroll <- Some conf.autoscrollstep
2305 with exc ->
2306 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2308 TEswitch ("auto scroll step: ", E.s, None, intentry, ondone, true)
2310 | 'C' ->
2311 let ondone s =
2313 let n, a, b = multicolumns_of_string s in
2314 setcolumns mode n a b;
2315 with exc ->
2316 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
2318 TEswitch ("columns: ", E.s, None, textentry, ondone, true)
2320 | 'Z' ->
2321 let ondone s =
2323 let zoom = float (int_of_string s) /. 100.0 in
2324 setzoom zoom
2325 with exc ->
2326 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2328 TEswitch ("zoom: ", E.s, None, intentry, ondone, true)
2330 | 't' ->
2331 let ondone s =
2333 conf.thumbw <- bound (int_of_string s) 2 4096;
2334 state.text <-
2335 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2336 begin match mode with
2337 | Birdseye beye ->
2338 leavebirdseye beye false;
2339 enterbirdseye ();
2340 | Textentry _
2341 | View
2342 | LinkNav _ -> ();
2344 with exc ->
2345 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2347 TEswitch ("thumbnail width: ", E.s, None, intentry, ondone, true)
2349 | 'R' ->
2350 let ondone s =
2351 match try
2352 Some (int_of_string s)
2353 with exc ->
2354 state.text <- Printf.sprintf "bad integer `%s': %s"
2355 s (exntos exc);
2356 None
2357 with
2358 | Some angle -> reqlayout angle conf.fitmodel
2359 | None -> ()
2361 TEswitch ("rotation: ", E.s, None, intentry, ondone, true)
2363 | 'i' ->
2364 conf.icase <- not conf.icase;
2365 TEdone ("case insensitive search " ^ (btos conf.icase))
2367 | 'p' ->
2368 conf.preload <- not conf.preload;
2369 gotoy state.y;
2370 TEdone ("preload " ^ (btos conf.preload))
2372 | 'v' ->
2373 conf.verbose <- not conf.verbose;
2374 TEdone ("verbose " ^ (btos conf.verbose))
2376 | 'd' ->
2377 conf.debug <- not conf.debug;
2378 TEdone ("debug " ^ (btos conf.debug))
2380 | 'h' ->
2381 conf.maxhfit <- not conf.maxhfit;
2382 state.maxy <- calcheight ();
2383 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2385 | 'c' ->
2386 conf.crophack <- not conf.crophack;
2387 TEdone ("crophack " ^ btos conf.crophack)
2389 | 'a' ->
2390 let s =
2391 match conf.maxwait with
2392 | None ->
2393 conf.maxwait <- Some infinity;
2394 "always wait for page to complete"
2395 | Some _ ->
2396 conf.maxwait <- None;
2397 "show placeholder if page is not ready"
2399 TEdone s
2401 | 'f' ->
2402 conf.underinfo <- not conf.underinfo;
2403 TEdone ("underinfo " ^ btos conf.underinfo)
2405 | 'P' ->
2406 conf.savebmarks <- not conf.savebmarks;
2407 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2409 | 'S' ->
2410 let ondone s =
2412 let pageno, py =
2413 match state.layout with
2414 | [] -> 0, 0
2415 | l :: _ ->
2416 l.pageno, l.pagey
2418 conf.interpagespace <- int_of_string s;
2419 docolumns conf.columns;
2420 state.maxy <- calcheight ();
2421 let y = getpagey pageno in
2422 gotoy (y + py)
2423 with exc ->
2424 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2426 TEswitch ("vertical margin: ", E.s, None, intentry, ondone, true)
2428 | 'l' ->
2429 let fm =
2430 match conf.fitmodel with
2431 | FitProportional -> FitWidth
2432 | FitWidth | FitPage -> FitProportional
2434 reqlayout conf.angle fm;
2435 TEdone ("proportional display " ^ btos (fm == FitProportional))
2437 | 'T' ->
2438 settrim (not conf.trimmargins) conf.trimfuzz;
2439 TEdone ("trim margins " ^ btos conf.trimmargins)
2441 | 'I' ->
2442 conf.invert <- not conf.invert;
2443 TEdone ("invert colors " ^ btos conf.invert)
2445 | 'x' ->
2446 let ondone s =
2447 cbput state.hists.sel s;
2448 conf.selcmd <- s;
2450 TEswitch ("selection command: ", E.s, Some (onhist state.hists.sel),
2451 textentry, ondone, true)
2453 | 'M' ->
2454 if conf.pax == None
2455 then conf.pax <- Some (ref (0.0, 0, 0))
2456 else conf.pax <- None;
2457 TEdone ("PAX " ^ btos (conf.pax != None))
2459 | _ ->
2460 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2461 TEstop
2462 else
2463 TEcont state.text
2466 class type lvsource = object
2467 method getitemcount : int
2468 method getitem : int -> (string * int)
2469 method hasaction : int -> bool
2470 method exit :
2471 uioh:uioh ->
2472 cancel:bool ->
2473 active:int ->
2474 first:int ->
2475 pan:int ->
2476 uioh option
2477 method getactive : int
2478 method getfirst : int
2479 method getpan : int
2480 method getminfo : (int * int) array
2481 end;;
2483 class virtual lvsourcebase = object
2484 val mutable m_active = 0
2485 val mutable m_first = 0
2486 val mutable m_pan = 0
2487 method getactive = m_active
2488 method getfirst = m_first
2489 method getpan = m_pan
2490 method getminfo : (int * int) array = E.a
2491 end;;
2493 let textentrykeyboard
2494 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2495 let key =
2496 if key >= 0xffb0 && key <= 0xffb9
2497 then key - 0xffb0 + 48 else key
2499 let enttext te =
2500 state.mode <- Textentry (te, onleave);
2501 state.text <- E.s;
2502 enttext ();
2503 G.postRedisplay "textentrykeyboard enttext";
2505 let histaction cmd =
2506 match opthist with
2507 | None -> ()
2508 | Some (action, _) ->
2509 state.mode <- Textentry (
2510 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
2512 G.postRedisplay "textentry histaction"
2514 match key with
2515 | @backspace ->
2516 if emptystr text && cancelonempty
2517 then (
2518 onleave Cancel;
2519 G.postRedisplay "textentrykeyboard after cancel";
2521 else
2522 let s = withoutlastutf8 text in
2523 enttext (c, s, opthist, onkey, ondone, cancelonempty)
2525 | @enter | @kpenter ->
2526 ondone text;
2527 onleave Confirm;
2528 G.postRedisplay "textentrykeyboard after confirm"
2530 | @up | @kpup -> histaction HCprev
2531 | @down | @kpdown -> histaction HCnext
2532 | @home | @kphome -> histaction HCfirst
2533 | @jend | @kpend -> histaction HClast
2535 | @escape ->
2536 if emptystr text
2537 then (
2538 begin match opthist with
2539 | None -> ()
2540 | Some (_, onhistcancel) -> onhistcancel ()
2541 end;
2542 onleave Cancel;
2543 state.text <- E.s;
2544 G.postRedisplay "textentrykeyboard after cancel2"
2546 else (
2547 enttext (c, E.s, opthist, onkey, ondone, cancelonempty)
2550 | @delete | @kpdelete -> ()
2552 | _ when key != 0
2553 && key land 0xff00 != 0xff00 (* keyboard *)
2554 && key land 0xfe00 != 0xfe00 (* xkb *)
2555 && key land 0xfd00 != 0xfd00 (* 3270 *)
2557 begin match onkey text key with
2558 | TEdone text ->
2559 ondone text;
2560 onleave Confirm;
2561 G.postRedisplay "textentrykeyboard after confirm2";
2563 | TEcont text ->
2564 enttext (c, text, opthist, onkey, ondone, cancelonempty);
2566 | TEstop ->
2567 onleave Cancel;
2568 G.postRedisplay "textentrykeyboard after cancel3"
2570 | TEswitch te ->
2571 state.mode <- Textentry (te, onleave);
2572 G.postRedisplay "textentrykeyboard switch";
2573 end;
2575 | _ ->
2576 vlog "unhandled key %s" (Wsi.keyname key)
2579 let firstof first active =
2580 if first > active || abs (first - active) > fstate.maxrows - 1
2581 then max 0 (active - (fstate.maxrows/2))
2582 else first
2585 let calcfirst first active =
2586 if active > first
2587 then
2588 let rows = active - first in
2589 if rows > fstate.maxrows then active - fstate.maxrows else first
2590 else active
2593 let scrollph y maxy =
2594 let sh = float (maxy + state.winh) /. float state.winh in
2595 let sh = float state.winh /. sh in
2596 let sh = max sh (float conf.scrollh) in
2598 let percent = float y /. float maxy in
2599 let position = (float state.winh -. sh) *. percent in
2601 let position =
2602 if position +. sh > float state.winh
2603 then float state.winh -. sh
2604 else position
2606 position, sh;
2609 let coe s = (s :> uioh);;
2611 class listview ~zebra ~helpmode ~(source:lvsource) ~trusted ~modehash =
2612 object (self)
2613 val m_pan = source#getpan
2614 val m_first = source#getfirst
2615 val m_active = source#getactive
2616 val m_qsearch = E.s
2617 val m_prev_uioh = state.uioh
2619 method private elemunder y =
2620 if y < 0
2621 then None
2622 else
2623 let n = y / (fstate.fontsize+1) in
2624 if m_first + n < source#getitemcount
2625 then (
2626 if source#hasaction (m_first + n)
2627 then Some (m_first + n)
2628 else None
2630 else None
2632 method display =
2633 Gl.enable `blend;
2634 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
2635 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2636 filledrect 0. 0. (float state.winw) (float state.winh);
2637 GlDraw.color (1., 1., 1.);
2638 Gl.enable `texture_2d;
2639 let fs = fstate.fontsize in
2640 let nfs = fs + 1 in
2641 let hw = (wadjsb () + xadjsb () + state.winw)/3 in
2642 let ww = fstate.wwidth in
2643 let tabw = 17.0*.ww in
2644 let itemcount = source#getitemcount in
2645 let minfo = source#getminfo in
2646 let x0, x1 =
2647 if conf.leftscroll
2648 then float (xadjsb ()), float (state.winw - 1)
2649 else 0.0, float (state.winw - conf.scrollbw - 1)
2651 let xadj = xadjsb () in
2652 let rec loop row =
2653 if (row - m_first) > fstate.maxrows
2654 then ()
2655 else (
2656 if row >= 0 && row < itemcount
2657 then (
2658 let (s, level) = source#getitem row in
2659 let y = (row - m_first) * nfs in
2660 let x =
2661 (if conf.leftscroll then float xadj else 5.0)
2662 +. (float (level + m_pan)) *. ww in
2663 if helpmode
2664 then GlDraw.color
2665 (let c = if row land 1 = 0 then 1.0 else 0.92 in (c,c,c));
2667 if row = m_active
2668 then (
2669 Gl.disable `texture_2d;
2670 let alpha = if source#hasaction row then 0.9 else 0.3 in
2671 GlDraw.color (1., 1., 1.) ~alpha;
2672 linerect (x0 +. 1.) (float (y + 1)) (x1) (float (y + fs + 3));
2673 Gl.enable `texture_2d;
2675 let c =
2676 if zebra && row land 1 = 1
2677 then 0.8
2678 else 1.0
2680 GlDraw.color (c,c,c);
2681 let drawtabularstring s =
2682 let drawstr x s =
2683 let x' = truncate (x0 +. x) in
2684 let pos = nindex s '\000' in
2685 if pos = -1
2686 then drawstring1 fs x' (y+nfs) s
2687 else
2688 let s1 = String.sub s 0 pos
2689 and s2 = String.sub s (pos+1) (String.length s - pos - 1) in
2690 let rec e s =
2691 if emptystr s
2692 then s
2693 else
2694 let s' = withoutlastutf8 s in
2695 let s = s' ^ "@Uellipsis" in
2696 let w = measurestr fs s in
2697 if float x' +. w +. ww < float (hw + x')
2698 then s
2699 else e s'
2701 let s1 =
2702 if float x' +. ww +. measurestr fs s1 > float (hw + x')
2703 then e s1
2704 else s1
2706 ignore (drawstring1 fs x' (y+nfs) s1);
2707 drawstring1 fs (hw + x') (y+nfs) s2
2709 if trusted
2710 then
2711 let x = if helpmode && row > 0 then x +. ww else x in
2712 let tabpos = nindex s '\t' in
2713 if tabpos > 0
2714 then
2715 let len = String.length s - tabpos - 1 in
2716 let s1 = String.sub s 0 tabpos
2717 and s2 = String.sub s (tabpos + 1) len in
2718 let nx = drawstr x s1 in
2719 let sw = nx -. x in
2720 let x = x +. (max tabw sw) in
2721 drawstr x s2
2722 else
2723 let len = String.length s - 2 in
2724 if len > 0 && s.[0] = '\xc2' && s.[1] = '\xb7'
2725 then
2726 let s = String.sub s 2 len in
2727 let x = if not helpmode then x +. ww else x in
2728 GlDraw.color (1.2, 1.2, 1.2);
2729 let vinc = drawstring1 (fs+fs/4)
2730 (truncate (x -. ww)) (y+nfs) s in
2731 GlDraw.color (1., 1., 1.);
2732 vinc +. (float fs *. 0.8)
2733 else
2734 drawstr x s
2735 else
2736 drawstr x s
2738 ignore (drawtabularstring s);
2739 loop (row+1)
2743 loop m_first;
2744 GlDraw.color (1.0, 1.0, 1.0) ~alpha:0.5;
2745 let xadj = float (xadjsb () + 5) in
2746 let rec loop row =
2747 if (row - m_first) > fstate.maxrows
2748 then ()
2749 else (
2750 if row >= 0 && row < itemcount
2751 then (
2752 let (s, level) = source#getitem row in
2753 let pos0 = nindex s '\000' in
2754 let y = (row - m_first) * nfs in
2755 let x = float (level + m_pan) *. ww in
2756 let (first, last) = minfo.(row) in
2757 let prefix =
2758 if pos0 > 0 && first > pos0
2759 then String.sub s (pos0+1) (first-pos0-1)
2760 else String.sub s 0 first
2762 let suffix = String.sub s first (last - first) in
2763 let w1 = measurestr fstate.fontsize prefix in
2764 let w2 = measurestr fstate.fontsize suffix in
2765 let x = x +. if conf.leftscroll then xadj else 5.0 in
2766 let x = if pos0 > 0 && first > pos0 then x +. float hw else x in
2767 let x0 = x +. w1
2768 and y0 = float (y+2) in
2769 let x1 = x0 +. w2
2770 and y1 = float (y+fs+3) in
2771 filledrect x0 y0 x1 y1;
2772 loop (row+1)
2776 Gl.disable `texture_2d;
2777 if Array.length minfo > 0 then loop m_first;
2778 Gl.disable `blend;
2780 method updownlevel incr =
2781 let len = source#getitemcount in
2782 let curlevel =
2783 if m_active >= 0 && m_active < len
2784 then snd (source#getitem m_active)
2785 else -1
2787 let rec flow i =
2788 if i = len then i-1 else if i = -1 then 0 else
2789 let _, l = source#getitem i in
2790 if l != curlevel then i else flow (i+incr)
2792 let active = flow m_active in
2793 let first = calcfirst m_first active in
2794 G.postRedisplay "outline updownlevel";
2795 {< m_active = active; m_first = first >}
2797 method private key1 key mask =
2798 let set1 active first qsearch =
2799 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2801 let search active pattern incr =
2802 let active = if active = -1 then m_first else active in
2803 let dosearch re =
2804 let rec loop n =
2805 if n >= 0 && n < source#getitemcount
2806 then (
2807 let s, _ = source#getitem n in
2809 (try ignore (Str.search_forward re s 0); true
2810 with Not_found -> false)
2811 then Some n
2812 else loop (n + incr)
2814 else None
2816 loop active
2819 let re = Str.regexp_case_fold pattern in
2820 dosearch re
2821 with Failure s ->
2822 state.text <- s;
2823 None
2825 let itemcount = source#getitemcount in
2826 let find start incr =
2827 let rec find i =
2828 if i = -1 || i = itemcount
2829 then -1
2830 else (
2831 if source#hasaction i
2832 then i
2833 else find (i + incr)
2836 find start
2838 let set active first =
2839 let first = bound first 0 (itemcount - fstate.maxrows) in
2840 state.text <- E.s;
2841 coe {< m_active = active; m_first = first; m_qsearch = E.s >}
2843 let navigate incr =
2844 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2845 let active, first =
2846 let incr1 = if incr > 0 then 1 else -1 in
2847 if isvisible m_first m_active
2848 then
2849 let next =
2850 let next = m_active + incr in
2851 let next =
2852 if next < 0 || next >= itemcount
2853 then -1
2854 else find next incr1
2856 if abs (m_active - next) > fstate.maxrows
2857 then -1
2858 else next
2860 if next = -1
2861 then
2862 let first = m_first + incr in
2863 let first = bound first 0 (itemcount - fstate.maxrows) in
2864 let next =
2865 let next = m_active + incr in
2866 let next = bound next 0 (itemcount - 1) in
2867 find next ~-incr1
2869 let active =
2870 if next = -1
2871 then m_active
2872 else (
2873 if isvisible first next
2874 then next
2875 else m_active
2878 active, first
2879 else
2880 let first = min next m_first in
2881 let first =
2882 if abs (next - first) > fstate.maxrows
2883 then first + incr
2884 else first
2886 next, first
2887 else
2888 let first = m_first + incr in
2889 let first = bound first 0 (itemcount - 1) in
2890 let active =
2891 let next = m_active + incr in
2892 let next = bound next 0 (itemcount - 1) in
2893 let next = find next incr1 in
2894 let active =
2895 if next = -1 || abs (m_active - first) > fstate.maxrows
2896 then (
2897 let active = if m_active = -1 then next else m_active in
2898 active
2900 else next
2902 if isvisible first active
2903 then active
2904 else -1
2906 active, first
2908 G.postRedisplay "listview navigate";
2909 set active first;
2911 match key with
2912 | (@r|@s) when Wsi.withctrl mask ->
2913 let incr = if key = @r then -1 else 1 in
2914 let active, first =
2915 match search (m_active + incr) m_qsearch incr with
2916 | None ->
2917 state.text <- m_qsearch ^ " [not found]";
2918 m_active, m_first
2919 | Some active ->
2920 state.text <- m_qsearch;
2921 active, firstof m_first active
2923 G.postRedisplay "listview ctrl-r/s";
2924 set1 active first m_qsearch;
2926 | @insert when Wsi.withctrl mask ->
2927 if m_active >= 0 && m_active < source#getitemcount
2928 then (
2929 let s, _ = source#getitem m_active in
2930 selstring s;
2932 coe self
2934 | @backspace ->
2935 if emptystr m_qsearch
2936 then coe self
2937 else (
2938 let qsearch = withoutlastutf8 m_qsearch in
2939 if emptystr qsearch
2940 then (
2941 state.text <- E.s;
2942 G.postRedisplay "listview empty qsearch";
2943 set1 m_active m_first E.s;
2945 else
2946 let active, first =
2947 match search m_active qsearch ~-1 with
2948 | None ->
2949 state.text <- qsearch ^ " [not found]";
2950 m_active, m_first
2951 | Some active ->
2952 state.text <- qsearch;
2953 active, firstof m_first active
2955 G.postRedisplay "listview backspace qsearch";
2956 set1 active first qsearch
2959 | key when (key != 0 && key land 0xff00 != 0xff00) ->
2960 let pattern = m_qsearch ^ toutf8 key in
2961 let active, first =
2962 match search m_active pattern 1 with
2963 | None ->
2964 state.text <- pattern ^ " [not found]";
2965 m_active, m_first
2966 | Some active ->
2967 state.text <- pattern;
2968 active, firstof m_first active
2970 G.postRedisplay "listview qsearch add";
2971 set1 active first pattern;
2973 | @escape ->
2974 state.text <- E.s;
2975 if emptystr m_qsearch
2976 then (
2977 G.postRedisplay "list view escape";
2978 begin
2979 match
2980 source#exit ~uioh:(coe self)
2981 ~cancel:true ~active:m_active ~first:m_first ~pan:m_pan
2982 with
2983 | None -> m_prev_uioh
2984 | Some uioh -> uioh
2987 else (
2988 G.postRedisplay "list view kill qsearch";
2989 coe {< m_qsearch = E.s >}
2992 | @enter | @kpenter ->
2993 state.text <- E.s;
2994 let self = {< m_qsearch = E.s >} in
2995 let opt =
2996 G.postRedisplay "listview enter";
2997 if m_active >= 0 && m_active < source#getitemcount
2998 then (
2999 source#exit ~uioh:(coe self) ~cancel:false
3000 ~active:m_active ~first:m_first ~pan:m_pan;
3002 else (
3003 source#exit ~uioh:(coe self) ~cancel:true
3004 ~active:m_active ~first:m_first ~pan:m_pan;
3007 begin match opt with
3008 | None -> m_prev_uioh
3009 | Some uioh -> uioh
3012 | @delete | @kpdelete ->
3013 coe self
3015 | @up | @kpup -> navigate ~-1
3016 | @down | @kpdown -> navigate 1
3017 | @prior | @kpprior -> navigate ~-(fstate.maxrows)
3018 | @next | @kpnext -> navigate fstate.maxrows
3020 | @right | @kpright ->
3021 state.text <- E.s;
3022 G.postRedisplay "listview right";
3023 coe {< m_pan = m_pan - 1 >}
3025 | @left | @kpleft ->
3026 state.text <- E.s;
3027 G.postRedisplay "listview left";
3028 coe {< m_pan = m_pan + 1 >}
3030 | @home | @kphome ->
3031 let active = find 0 1 in
3032 G.postRedisplay "listview home";
3033 set active 0;
3035 | @jend | @kpend ->
3036 let first = max 0 (itemcount - fstate.maxrows) in
3037 let active = find (itemcount - 1) ~-1 in
3038 G.postRedisplay "listview end";
3039 set active first;
3041 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3042 coe self
3044 | _ ->
3045 dolog "listview unknown key %#x" key; coe self
3047 method key key mask =
3048 match state.mode with
3049 | Textentry te -> textentrykeyboard key mask te; coe self
3050 | Birdseye _
3051 | View
3052 | LinkNav _ -> self#key1 key mask
3054 method button button down x y _ =
3055 let opt =
3056 match button with
3057 | 1 when x > state.winw - conf.scrollbw ->
3058 G.postRedisplay "listview scroll";
3059 if down
3060 then
3061 let _, position, sh = self#scrollph in
3062 if y > truncate position && y < truncate (position +. sh)
3063 then (
3064 state.mstate <- Mscrolly;
3065 Some (coe self)
3067 else
3068 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3069 let first = truncate (s *. float source#getitemcount) in
3070 let first = min source#getitemcount first in
3071 Some (coe {< m_first = first; m_active = first >})
3072 else (
3073 state.mstate <- Mnone;
3074 Some (coe self);
3076 | 1 when down ->
3077 begin match self#elemunder y with
3078 | Some n ->
3079 G.postRedisplay "listview click";
3080 source#exit ~uioh:(coe {< m_active = n >})
3081 ~cancel:false ~active:n ~first:m_first ~pan:m_pan
3082 | _ ->
3083 Some (coe self)
3085 | n when (n == 4 || n == 5) && not down ->
3086 let len = source#getitemcount in
3087 let first =
3088 if n = 5 && m_first + fstate.maxrows >= len
3089 then
3090 m_first
3091 else
3092 let first = m_first + (if n == 4 then -1 else 1) in
3093 bound first 0 (len - 1)
3095 G.postRedisplay "listview wheel";
3096 Some (coe {< m_first = first >})
3097 | n when (n = 6 || n = 7) && not down ->
3098 let inc = if n = 7 then -1 else 1 in
3099 G.postRedisplay "listview hwheel";
3100 Some (coe {< m_pan = m_pan + inc >})
3101 | _ ->
3102 Some (coe self)
3104 match opt with
3105 | None -> m_prev_uioh
3106 | Some uioh -> uioh
3108 method multiclick _ x y = self#button 1 true x y
3110 method motion _ y =
3111 match state.mstate with
3112 | Mscrolly ->
3113 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3114 let first = truncate (s *. float source#getitemcount) in
3115 let first = min source#getitemcount first in
3116 G.postRedisplay "listview motion";
3117 coe {< m_first = first; m_active = first >}
3118 | Msel _
3119 | Mpan _
3120 | Mscrollx
3121 | Mzoom _
3122 | Mzoomrect _
3123 | Mnone -> coe self
3125 method pmotion x y =
3126 if x < state.winw - conf.scrollbw
3127 then
3128 let n =
3129 match self#elemunder y with
3130 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3131 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3133 let o =
3134 if n != m_active
3135 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3136 else self
3138 coe o
3139 else (
3140 Wsi.setcursor Wsi.CURSOR_INHERIT;
3141 coe self
3144 method infochanged _ = ()
3146 method scrollpw = (0, 0.0, 0.0)
3147 method scrollph =
3148 let nfs = fstate.fontsize + 1 in
3149 let y = m_first * nfs in
3150 let itemcount = source#getitemcount in
3151 let maxi = max 0 (itemcount - fstate.maxrows) in
3152 let maxy = maxi * nfs in
3153 let p, h = scrollph y maxy in
3154 conf.scrollbw, p, h
3156 method modehash = modehash
3157 method eformsgs = false
3158 method alwaysscrolly = true
3159 end;;
3161 class outlinelistview ~zebra ~source =
3162 let settext autonarrow s =
3163 if autonarrow
3164 then
3165 let ss = source#statestr in
3166 state.text <-
3167 if emptystr ss
3168 then "[" ^ s ^ "]"
3169 else "{" ^ ss ^ "} [" ^ s ^ "]"
3170 else state.text <- s
3172 object (self)
3173 inherit listview
3174 ~zebra
3175 ~helpmode:false
3176 ~source:(source :> lvsource)
3177 ~trusted:false
3178 ~modehash:(findkeyhash conf "outline")
3179 as super
3181 val m_autonarrow = false
3183 method! key key mask =
3184 let maxrows =
3185 if emptystr state.text
3186 then fstate.maxrows
3187 else fstate.maxrows - 2
3189 let calcfirst first active =
3190 if active > first
3191 then
3192 let rows = active - first in
3193 if rows > maxrows then active - maxrows else first
3194 else active
3196 let navigate incr =
3197 let active = m_active + incr in
3198 let active = bound active 0 (source#getitemcount - 1) in
3199 let first = calcfirst m_first active in
3200 G.postRedisplay "outline navigate";
3201 coe {< m_active = active; m_first = first >}
3203 let navscroll first =
3204 let active =
3205 let dist = m_active - first in
3206 if dist < 0
3207 then first
3208 else (
3209 if dist < maxrows
3210 then m_active
3211 else first + maxrows
3214 G.postRedisplay "outline navscroll";
3215 coe {< m_first = first; m_active = active >}
3217 let ctrl = Wsi.withctrl mask in
3218 match key with
3219 | @a when ctrl ->
3220 let text =
3221 if m_autonarrow
3222 then (source#denarrow; E.s)
3223 else (
3224 let pattern = source#renarrow in
3225 if nonemptystr m_qsearch
3226 then (source#narrow m_qsearch; m_qsearch)
3227 else pattern
3230 settext (not m_autonarrow) text;
3231 G.postRedisplay "toggle auto narrowing";
3232 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
3234 | @slash when emptystr m_qsearch && not m_autonarrow ->
3235 settext true E.s;
3236 G.postRedisplay "toggle auto narrowing";
3237 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
3239 | @n when ctrl ->
3240 source#narrow m_qsearch;
3241 if not m_autonarrow
3242 then source#add_narrow_pattern m_qsearch;
3243 G.postRedisplay "outline ctrl-n";
3244 coe {< m_first = 0; m_active = 0 >}
3246 | @S when ctrl ->
3247 let active = source#calcactive (getanchor ()) in
3248 let first = firstof m_first active in
3249 G.postRedisplay "outline ctrl-s";
3250 coe {< m_first = first; m_active = active >}
3252 | @u when ctrl ->
3253 G.postRedisplay "outline ctrl-u";
3254 if m_autonarrow && nonemptystr m_qsearch
3255 then (
3256 ignore (source#renarrow);
3257 settext m_autonarrow E.s;
3258 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3260 else (
3261 source#del_narrow_pattern;
3262 let pattern = source#renarrow in
3263 let text =
3264 if emptystr pattern then E.s else "Narrowed to " ^ pattern
3266 settext m_autonarrow text;
3267 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3270 | @l when ctrl ->
3271 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3272 G.postRedisplay "outline ctrl-l";
3273 coe {< m_first = first >}
3275 | @tab when m_autonarrow ->
3276 if nonemptystr m_qsearch
3277 then (
3278 G.postRedisplay "outline list view tab";
3279 source#add_narrow_pattern m_qsearch;
3280 settext true E.s;
3281 coe {< m_qsearch = E.s >}
3283 else coe self
3285 | @escape when m_autonarrow ->
3286 if nonemptystr m_qsearch
3287 then source#add_narrow_pattern m_qsearch;
3288 super#key key mask
3290 | @enter | @kpenter when m_autonarrow ->
3291 if nonemptystr m_qsearch
3292 then source#add_narrow_pattern m_qsearch;
3293 super#key key mask
3295 | key when m_autonarrow && (key != 0 && key land 0xff00 != 0xff00) ->
3296 let pattern = m_qsearch ^ toutf8 key in
3297 G.postRedisplay "outlinelistview autonarrow add";
3298 source#narrow pattern;
3299 settext true pattern;
3300 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3302 | key when m_autonarrow && key = @backspace ->
3303 if emptystr m_qsearch
3304 then coe self
3305 else
3306 let pattern = withoutlastutf8 m_qsearch in
3307 G.postRedisplay "outlinelistview autonarrow backspace";
3308 ignore (source#renarrow);
3309 source#narrow pattern;
3310 settext true pattern;
3311 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3313 | @delete | @kpdelete ->
3314 source#remove m_active;
3315 G.postRedisplay "outline delete";
3316 let active = max 0 (m_active-1) in
3317 coe {< m_first = firstof m_first active;
3318 m_active = active >}
3320 | @up | @kpup when ctrl ->
3321 navscroll (max 0 (m_first - 1))
3323 | @down | @kpdown when ctrl ->
3324 navscroll (min (source#getitemcount - 1) (m_first + 1))
3326 | @up | @kpup -> navigate ~-1
3327 | @down | @kpdown -> navigate 1
3328 | @prior | @kpprior -> navigate ~-(fstate.maxrows)
3329 | @next | @kpnext -> navigate fstate.maxrows
3331 | @right | @kpright ->
3332 let o =
3333 if ctrl
3334 then (
3335 G.postRedisplay "outline ctrl right";
3336 {< m_pan = m_pan + 1 >}
3338 else self#updownlevel 1
3340 coe o
3342 | @left | @kpleft ->
3343 let o =
3344 if ctrl
3345 then (
3346 G.postRedisplay "outline ctrl left";
3347 {< m_pan = m_pan - 1 >}
3349 else self#updownlevel ~-1
3351 coe o
3353 | @home | @kphome ->
3354 G.postRedisplay "outline home";
3355 coe {< m_first = 0; m_active = 0 >}
3357 | @jend | @kpend ->
3358 let active = source#getitemcount - 1 in
3359 let first = max 0 (active - fstate.maxrows) in
3360 G.postRedisplay "outline end";
3361 coe {< m_active = active; m_first = first >}
3363 | _ -> super#key key mask
3366 let genhistoutlines =
3367 let order ty (p1, c1, _, _, _) (p2, c2, _, _, _) =
3368 match ty with
3369 | `lastvisit -> compare c1.lastvisit c2.lastvisit
3370 | `path -> compare p2 p1
3371 | `file -> compare (Filename.basename p2) (Filename.basename p1)
3372 | `title ->
3373 let e1 = emptystr c1.title
3374 and e2 = emptystr c2.title in
3375 (**) if e1 && e2
3376 then compare (Filename.basename p2) (Filename.basename p1)
3377 else if e1 then -1
3378 else if e2 then 1
3379 else compare c1.title c2.title
3381 let showfullpath = ref false in
3382 fun orderty ->
3383 let setorty s t =
3384 let s = if orderty = t then "[@Uradical] " ^ s else "[ ] " ^ s in
3385 s, 0, Oaction (fun () -> Config.historder := t; reeenterhist := true)
3387 let list = ref [] in
3388 if Config.gethist list
3389 then
3390 let ol =
3391 List.fold_left
3392 (fun accu (path, c, b, x, a) ->
3393 let hist = (path, (c, b, x, a)) in
3394 let s = if !showfullpath then path else Filename.basename path in
3395 let base = mbtoutf8 s in
3396 (base ^ "\000" ^ c.title, 1, Ohistory hist) :: accu
3398 [ setorty "Sort by time of last visit" `lastvisit;
3399 setorty "Sort by file name" `file;
3400 setorty "Sort by path" `path;
3401 setorty "Sort by title" `title;
3402 (if !showfullpath then "@Uradical "
3403 else " ") ^ "Show full path", 0, Oaction (fun () ->
3404 showfullpath := not !showfullpath; reeenterhist := true)
3405 ] (List.sort (order orderty) !list)
3407 Array.of_list ol
3408 else E.a;
3411 let gotohist (path, (c, bookmarks, x, anchor)) =
3412 Config.save leavebirdseye;
3413 state.anchor <- anchor;
3414 state.x <- x;
3415 state.bookmarks <- bookmarks;
3416 state.origin <- E.s;
3417 setconf conf c;
3418 let x0, y0, x1, y1 = conf.trimfuzz in
3419 wcmd "trimset %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1;
3420 opendoc path E.s;
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);
4065 sep ();
4066 src#caption "Document" 0;
4067 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4068 src#caption2 "Pages"
4069 (fun () -> string_of_int state.pagecount) 1;
4070 src#caption2 "Dimensions"
4071 (fun () -> string_of_int (List.length state.pdims)) 1;
4072 if conf.trimmargins
4073 then (
4074 sep ();
4075 src#caption "Trimmed margins" 0;
4076 src#caption2 "Dimensions"
4077 (fun () -> string_of_int (List.length state.pdims)) 1;
4080 sep ();
4081 src#caption "OpenGL" 0;
4082 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4083 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4085 sep ();
4086 src#caption "Location" 0;
4087 if nonemptystr state.origin
4088 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
4089 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
4091 src#reset prevmode prevuioh;
4093 fun () ->
4094 state.text <- E.s;
4095 resetmstate ();
4096 let prevmode = state.mode
4097 and prevuioh = state.uioh in
4098 fillsrc prevmode prevuioh;
4099 let source = (src :> lvsource) in
4100 let modehash = findkeyhash conf "info" in
4101 state.uioh <- coe (object (self)
4102 inherit listview ~zebra:false ~helpmode:false
4103 ~source ~trusted:true ~modehash as super
4104 val mutable m_prevmemused = 0
4105 method! infochanged = function
4106 | Memused ->
4107 if m_prevmemused != state.memused
4108 then (
4109 m_prevmemused <- state.memused;
4110 G.postRedisplay "memusedchanged";
4112 | Pdim -> G.postRedisplay "pdimchanged"
4113 | Docinfo -> fillsrc prevmode prevuioh
4115 method! key key mask =
4116 if not (Wsi.withctrl mask)
4117 then
4118 match key with
4119 | @left | @kpleft -> coe (self#updownlevel ~-1)
4120 | @right | @kpright -> coe (self#updownlevel 1)
4121 | _ -> super#key key mask
4122 else super#key key mask
4123 end);
4124 G.postRedisplay "info";
4127 let enterhelpmode =
4128 let source =
4129 (object
4130 inherit lvsourcebase
4131 method getitemcount = Array.length state.help
4132 method getitem n =
4133 let s, l, _ = state.help.(n) in
4134 (s, l)
4136 method exit ~uioh ~cancel ~active ~first ~pan =
4137 let optuioh =
4138 if not cancel
4139 then (
4140 match state.help.(active) with
4141 | _, _, Action f -> Some (f uioh)
4142 | _, _, Noaction -> Some uioh
4144 else None
4146 m_active <- active;
4147 m_first <- first;
4148 m_pan <- pan;
4149 optuioh
4151 method hasaction n =
4152 match state.help.(n) with
4153 | _, _, Action _ -> true
4154 | _, _, Noaction -> false
4156 initializer
4157 m_active <- -1
4158 end)
4159 in fun () ->
4160 let modehash = findkeyhash conf "help" in
4161 resetmstate ();
4162 state.uioh <- coe (new listview
4163 ~zebra:false ~helpmode:true
4164 ~source ~trusted:true ~modehash);
4165 G.postRedisplay "help";
4168 let entermsgsmode =
4169 let msgsource =
4170 (object
4171 inherit lvsourcebase
4172 val mutable m_items = E.a
4174 method getitemcount = 1 + Array.length m_items
4176 method getitem n =
4177 if n = 0
4178 then "[Clear]", 0
4179 else m_items.(n-1), 0
4181 method exit ~uioh ~cancel ~active ~first ~pan =
4182 ignore uioh;
4183 if not cancel
4184 then (
4185 if active = 0
4186 then Buffer.clear state.errmsgs;
4188 m_active <- active;
4189 m_first <- first;
4190 m_pan <- pan;
4191 None
4193 method hasaction n =
4194 n = 0
4196 method reset =
4197 state.newerrmsgs <- false;
4198 let l = Str.split newlinere (Buffer.contents state.errmsgs) in
4199 m_items <- Array.of_list l
4201 initializer
4202 m_active <- 0
4203 end)
4204 in fun () ->
4205 state.text <- E.s;
4206 resetmstate ();
4207 msgsource#reset;
4208 let source = (msgsource :> lvsource) in
4209 let modehash = findkeyhash conf "listview" in
4210 state.uioh <- coe (object
4211 inherit listview ~zebra:false ~helpmode:false
4212 ~source ~trusted:false ~modehash as super
4213 method! display =
4214 if state.newerrmsgs
4215 then msgsource#reset;
4216 super#display
4217 end);
4218 G.postRedisplay "msgs";
4221 let getusertext s =
4222 let editor = getenvwithdef "EDITOR" E.s in
4223 if emptystr editor
4224 then E.s
4225 else
4226 let tmppath = Filename.temp_file "llpp" "note" in
4227 if not (emptystr s)
4228 then (
4229 let oc = open_out tmppath in
4230 output_string oc s;
4231 close_out oc;
4233 let execstr = editor ^ " " ^ tmppath in
4234 let s =
4235 match Unix.system execstr with
4236 | (exception exn) ->
4237 showtext '!' @@
4238 Printf.sprintf "Unix.system(%S) failed: %s" execstr (exntos exn);
4240 | Unix.WEXITED 0 -> filelines tmppath
4241 | Unix.WEXITED n ->
4242 showtext '!' @@
4243 Printf.sprintf "editor process(%s) exited abnormally: %d"
4244 execstr n;
4246 | Unix.WSIGNALED n ->
4247 showtext '!' @@
4248 Printf.sprintf "editor process(%s) was killed by signal %d"
4249 execstr n;
4251 | Unix.WSTOPPED n ->
4252 showtext '!' @@
4253 Printf.sprintf "editor(%s) process was stopped by signal %d"
4254 execstr n;
4257 match Unix.unlink tmppath with
4258 | (exception exn) ->
4259 showtext '!' @@
4260 Printf.sprintf "failed to ulink %S: %s"
4261 tmppath (exntos exn);
4263 | () -> s
4266 let enterannotmode opaque slinkindex =
4267 let msgsource =
4268 (object
4269 inherit lvsourcebase
4270 val mutable m_text = E.s
4271 val mutable m_items = E.a
4273 method getitemcount = Array.length m_items
4275 method getitem n =
4276 let label, _func = m_items.(n) in
4277 label, 0
4279 method exit ~uioh ~cancel ~active ~first ~pan =
4280 ignore (uioh, first, pan);
4281 if not cancel
4282 then (
4283 let _label, func = m_items.(active) in
4284 func ()
4286 None
4288 method hasaction n = not @@ emptystr @@ fst m_items.(n)
4290 method reset s =
4291 let rec split accu b i =
4292 let p = b+i in
4293 if p = String.length s
4294 then (String.sub s b (p-b), unit) :: accu
4295 else
4296 if (i > 70 && s.[p] = ' ') || s.[p] = '\r' || s.[p] = '\n'
4297 then
4298 let ss = if i = 0 then E.s else String.sub s b i in
4299 split ((ss, unit)::accu) (p+1) 0
4300 else
4301 split accu b (i+1)
4303 let cleanup () =
4304 wcmd "freepage %s" (~> opaque);
4305 let keys =
4306 Hashtbl.fold (fun key opaque' accu ->
4307 if opaque' = opaque'
4308 then key :: accu else accu) state.pagemap []
4310 List.iter (Hashtbl.remove state.pagemap) keys;
4311 flushtiles ();
4312 gotoy state.y
4314 let dele () =
4315 delannot opaque slinkindex;
4316 cleanup ();
4318 let edit inline () =
4319 let update s =
4320 if emptystr s
4321 then dele ()
4322 else (
4323 modannot opaque slinkindex s;
4324 cleanup ();
4327 if inline
4328 then
4329 let mode = state.mode in
4330 state.mode <-
4331 Textentry (
4332 ("annotation: ", m_text, None, textentry, update, true),
4333 fun _ -> state.mode <- mode);
4334 state.text <- E.s;
4335 enttext ();
4336 else
4337 let s = getusertext m_text in
4338 update s
4340 m_text <- s;
4341 m_items <-
4342 ( "[Copy]", fun () -> selstring m_text)
4343 :: ("[Delete]", dele)
4344 :: ("[Edit]", edit true)
4345 :: (E.s, unit)
4346 :: split [] 0 0 |> List.rev |> Array.of_list
4348 initializer
4349 m_active <- 0
4350 end)
4352 state.text <- E.s;
4353 let s = getannotcontents opaque slinkindex in
4354 resetmstate ();
4355 msgsource#reset s;
4356 let source = (msgsource :> lvsource) in
4357 let modehash = findkeyhash conf "listview" in
4358 state.uioh <- coe (object
4359 inherit listview ~zebra:false ~helpmode:false
4360 ~source ~trusted:false ~modehash
4361 end);
4362 G.postRedisplay "enterannotmode";
4365 let gotounder under =
4366 let getpath filename =
4367 let path =
4368 if nonemptystr filename
4369 then
4370 if Filename.is_relative filename
4371 then
4372 let dir = Filename.dirname state.path in
4373 let dir =
4374 if Filename.is_implicit dir
4375 then Filename.concat (Sys.getcwd ()) dir
4376 else dir
4378 Filename.concat dir filename
4379 else filename
4380 else E.s
4382 if Sys.file_exists path
4383 then path
4384 else E.s
4386 match under with
4387 | Ulinkgoto (pageno, top) ->
4388 if pageno >= 0
4389 then (
4390 addnav ();
4391 gotopage1 pageno top;
4394 | Ulinkuri s ->
4395 addpid @@ gotouri s
4397 | Uremote (filename, pageno) ->
4398 let path = getpath filename in
4399 if nonemptystr path
4400 then (
4401 if conf.riani
4402 then
4403 let command = Printf.sprintf "%s -page %d %S" !selfexec pageno path in
4404 try addpid @@ popen command []
4405 with exn ->
4406 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
4407 flush stderr;
4408 else
4409 let anchor = getanchor () in
4410 let ranchor = state.path, state.password, anchor, state.origin in
4411 state.origin <- E.s;
4412 state.anchor <- (pageno, 0.0, 0.0);
4413 state.ranchors <- ranchor :: state.ranchors;
4414 opendoc path E.s;
4416 else showtext '!' ("Could not find " ^ filename)
4418 | Uremotedest (filename, destname) ->
4419 let path = getpath filename in
4420 if nonemptystr path
4421 then (
4422 if conf.riani
4423 then
4424 let command = !selfexec ^ " " ^ path ^ " -dest " ^ destname in
4425 try addpid @@ popen command []
4426 with exn ->
4427 Printf.eprintf
4428 "failed to execute `%s': %s\n" command (exntos exn);
4429 flush stderr;
4430 else
4431 let anchor = getanchor () in
4432 let ranchor = state.path, state.password, anchor, state.origin in
4433 state.origin <- E.s;
4434 state.nameddest <- destname;
4435 state.ranchors <- ranchor :: state.ranchors;
4436 opendoc path E.s;
4438 else showtext '!' ("Could not find " ^ filename)
4440 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4441 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
4444 let gotooutline (_, _, kind) =
4445 match kind with
4446 | Onone -> ()
4447 | Oanchor anchor ->
4448 let (pageno, y, _) = anchor in
4449 let y = getanchory
4450 (if conf.presentation then (pageno, y, 1.0) else anchor)
4452 addnav ();
4453 gotoghyll y
4454 | Ouri uri -> gotounder (Ulinkuri uri)
4455 | Olaunch cmd -> gotounder (Ulaunch cmd)
4456 | Oremote remote -> gotounder (Uremote remote)
4457 | Ohistory hist -> gotohist hist
4458 | Oremotedest remotedest -> gotounder (Uremotedest remotedest)
4459 | Oaction f -> f ()
4462 let outlinesource sourcetype =
4463 (object (self)
4464 inherit lvsourcebase
4465 val mutable m_items = E.a
4466 val mutable m_minfo = E.a
4467 val mutable m_orig_items = E.a
4468 val mutable m_orig_minfo = E.a
4469 val mutable m_narrow_patterns = []
4470 val mutable m_hadremovals = false
4471 val mutable m_gen = -1
4473 method getitemcount =
4474 Array.length m_items + (if m_hadremovals then 1 else 0)
4476 method getitem n =
4477 if n == Array.length m_items && m_hadremovals
4478 then
4479 ("[Confirm removal]", 0)
4480 else
4481 let s, n, _ = m_items.(n) in
4482 (s, n)
4484 method exit ~uioh ~cancel ~active ~first ~pan =
4485 ignore (uioh, first);
4486 let confrimremoval = m_hadremovals && active = Array.length m_items in
4487 let items, minfo =
4488 if m_narrow_patterns = []
4489 then m_orig_items, m_orig_minfo
4490 else m_items, m_minfo
4492 if not cancel
4493 then (
4494 if not confrimremoval
4495 then (
4496 gotooutline m_items.(active);
4497 m_items <- items;
4498 m_minfo <- minfo;
4500 else (
4501 state.bookmarks <- Array.to_list m_items;
4502 m_orig_items <- m_items;
4503 m_orig_minfo <- m_minfo;
4506 else (
4507 m_items <- items;
4508 m_minfo <- minfo;
4510 m_pan <- pan;
4511 None
4513 method hasaction _ = true
4515 method greetmsg =
4516 if Array.length m_items != Array.length m_orig_items
4517 then
4518 let s =
4519 match m_narrow_patterns with
4520 | one :: [] -> one
4521 | many -> String.concat "@Uellipsis" (List.rev many)
4523 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
4524 else E.s
4526 method statestr =
4527 match m_narrow_patterns with
4528 | [] -> E.s
4529 | one :: [] -> one
4530 | head :: _ -> "@Uellipsis" ^ head
4532 method narrow pattern =
4533 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
4534 match reopt with
4535 | None -> ()
4536 | Some re ->
4537 let rec loop accu minfo n =
4538 if n = -1
4539 then (
4540 m_items <- Array.of_list accu;
4541 m_minfo <- Array.of_list minfo;
4543 else
4544 let (s, _, t) as o = m_items.(n) in
4545 let accu, minfo =
4546 match t with
4547 | Oaction _ -> o :: accu, (0, 0) :: minfo
4548 | Onone | Oanchor _ | Ouri _ | Olaunch _
4549 | Oremote _ | Oremotedest _ | Ohistory _ ->
4550 let first =
4551 try Str.search_forward re s 0
4552 with Not_found -> -1
4554 if first >= 0
4555 then o :: accu, (first, Str.match_end ()) :: minfo
4556 else accu, minfo
4558 loop accu minfo (n-1)
4560 loop [] [] (Array.length m_items - 1)
4562 method! getminfo = m_minfo
4564 method denarrow =
4565 m_orig_items <- (
4566 match sourcetype with
4567 | `bookmarks -> Array.of_list state.bookmarks
4568 | `outlines -> state.outlines
4569 | `history -> genhistoutlines !Config.historder
4571 m_minfo <- m_orig_minfo;
4572 m_items <- m_orig_items
4574 method remove m =
4575 if sourcetype = `bookmarks
4576 then
4577 if m >= 0 && m < Array.length m_items
4578 then (
4579 m_hadremovals <- true;
4580 m_items <- Array.init (Array.length m_items - 1) (fun n ->
4581 let n = if n >= m then n+1 else n in
4582 m_items.(n)
4586 method add_narrow_pattern pattern =
4587 m_narrow_patterns <- pattern :: m_narrow_patterns
4589 method del_narrow_pattern =
4590 match m_narrow_patterns with
4591 | _ :: rest -> m_narrow_patterns <- rest
4592 | [] -> ()
4594 method renarrow =
4595 self#denarrow;
4596 match m_narrow_patterns with
4597 | pattern :: [] -> self#narrow pattern; pattern
4598 | list ->
4599 List.fold_left (fun accu pattern ->
4600 self#narrow pattern;
4601 pattern ^ "@Uellipsis" ^ accu) E.s list
4603 method calcactive anchor =
4604 let rely = getanchory anchor in
4605 let rec loop n best bestd =
4606 if n = Array.length m_items
4607 then best
4608 else
4609 let _, _, kind = m_items.(n) in
4610 match kind with
4611 | Oanchor anchor ->
4612 let orely = getanchory anchor in
4613 let d = abs (orely - rely) in
4614 if d < bestd
4615 then loop (n+1) n d
4616 else loop (n+1) best bestd
4617 | Onone | Oremote _ | Olaunch _
4618 | Oremotedest _ | Ouri _ | Ohistory _ | Oaction _ ->
4619 loop (n+1) best bestd
4621 loop 0 ~-1 max_int
4623 method reset anchor items =
4624 m_hadremovals <- false;
4625 if state.gen != m_gen
4626 then (
4627 m_orig_items <- items;
4628 m_items <- items;
4629 m_narrow_patterns <- [];
4630 m_minfo <- E.a;
4631 m_orig_minfo <- E.a;
4632 m_gen <- state.gen;
4634 else (
4635 if items != m_orig_items
4636 then (
4637 m_orig_items <- items;
4638 if m_narrow_patterns == []
4639 then m_items <- items;
4642 let active = self#calcactive anchor in
4643 m_active <- active;
4644 m_first <- firstof m_first active
4645 end)
4648 let enterselector sourcetype =
4649 resetmstate ();
4650 let source = outlinesource sourcetype in
4651 fun errmsg ->
4652 let outlines =
4653 match sourcetype with
4654 | `bookmarks -> Array.of_list state.bookmarks
4655 | `outlines -> state.outlines
4656 | `history -> genhistoutlines !Config.historder
4658 if Array.length outlines = 0
4659 then (
4660 showtext ' ' errmsg;
4662 else (
4663 state.text <- source#greetmsg;
4664 Wsi.setcursor Wsi.CURSOR_INHERIT;
4665 let anchor = getanchor () in
4666 source#reset anchor outlines;
4667 state.uioh <-
4668 coe (new outlinelistview ~zebra:(sourcetype=`history) ~source);
4669 G.postRedisplay "enter selector";
4673 let enteroutlinemode =
4674 let f = enterselector `outlines in
4675 fun () -> f "Document has no outline";
4678 let enterbookmarkmode =
4679 let f = enterselector `bookmarks in
4680 fun () -> f "Document has no bookmarks (yet)";
4683 let enterhistmode () = enterselector `history "No history (yet)";;
4685 let quickbookmark ?title () =
4686 match state.layout with
4687 | [] -> ()
4688 | l :: _ ->
4689 let title =
4690 match title with
4691 | None ->
4692 let tm = Unix.localtime (now ()) in
4693 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4694 (l.pageno+1)
4695 tm.Unix.tm_mday
4696 tm.Unix.tm_mon
4697 (tm.Unix.tm_year + 1900)
4698 tm.Unix.tm_hour
4699 tm.Unix.tm_min
4700 | Some title -> title
4702 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4705 let setautoscrollspeed step goingdown =
4706 let incr = max 1 ((abs step) / 2) in
4707 let incr = if goingdown then incr else -incr in
4708 let astep = boundastep state.winh (step + incr) in
4709 state.autoscroll <- Some astep;
4712 let canpan () =
4713 match conf.columns with
4714 | Csplit _ -> true
4715 | Csingle _ | Cmulti _ -> state.x != 0 || conf.zoom > 1.0
4718 let panbound x = bound x (-state.w) (wadjsb () + state.winw);;
4720 let existsinrow pageno (columns, coverA, coverB) p =
4721 let last = ((pageno - coverA) mod columns) + columns in
4722 let rec any = function
4723 | [] -> false
4724 | l :: rest ->
4725 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4726 then p l
4727 else (
4728 if not (p l)
4729 then (if l.pageno = last then false else any rest)
4730 else true
4733 any state.layout
4736 let nextpage () =
4737 match state.layout with
4738 | [] ->
4739 let pageno = page_of_y state.y in
4740 gotoghyll (getpagey (pageno+1))
4741 | l :: rest ->
4742 match conf.columns with
4743 | Csingle _ ->
4744 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4745 then
4746 let y = clamp (pgscale state.winh) in
4747 gotoghyll y
4748 else
4749 let pageno = min (l.pageno+1) (state.pagecount-1) in
4750 gotoghyll (getpagey pageno)
4751 | Cmulti ((c, _, _) as cl, _) ->
4752 if conf.presentation
4753 && (existsinrow l.pageno cl
4754 (fun l -> 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+c) (state.pagecount-1) in
4760 gotoghyll (getpagey pageno)
4761 | Csplit (n, _) ->
4762 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4763 then
4764 let pagey, pageh = getpageyh l.pageno in
4765 let pagey = pagey + pageh * l.pagecol in
4766 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4767 gotoghyll (pagey + pageh + ips)
4770 let prevpage () =
4771 match state.layout with
4772 | [] ->
4773 let pageno = page_of_y state.y in
4774 gotoghyll (getpagey (pageno-1))
4775 | l :: _ ->
4776 match conf.columns with
4777 | Csingle _ ->
4778 if conf.presentation && l.pagey != 0
4779 then
4780 gotoghyll (clamp (pgscale ~-(state.winh)))
4781 else
4782 let pageno = max 0 (l.pageno-1) in
4783 gotoghyll (getpagey pageno)
4784 | Cmulti ((c, _, coverB) as cl, _) ->
4785 if conf.presentation &&
4786 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4787 then
4788 gotoghyll (clamp (pgscale ~-(state.winh)))
4789 else
4790 let decr =
4791 if l.pageno = state.pagecount - coverB
4792 then 1
4793 else c
4795 let pageno = max 0 (l.pageno-decr) in
4796 gotoghyll (getpagey pageno)
4797 | Csplit (n, _) ->
4798 let y =
4799 if l.pagecol = 0
4800 then
4801 if l.pageno = 0
4802 then l.pagey
4803 else
4804 let pageno = max 0 (l.pageno-1) in
4805 let pagey, pageh = getpageyh pageno in
4806 pagey + (n-1)*pageh
4807 else
4808 let pagey, pageh = getpageyh l.pageno in
4809 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4811 gotoghyll y
4814 let save () =
4815 if emptystr conf.savecmd
4816 then error "don't know where to save modified document"
4817 else
4818 let command = Str.global_replace percentsre state.path conf.savecmd in
4819 match Unix.open_process_in command with
4820 | (exception exn) ->
4821 showtext '!'
4822 (Printf.sprintf "savecmd open_process_in failed: %s"
4823 (exntos exn));
4824 | ic ->
4825 let path = try input_line ic with End_of_file -> E.s in
4826 let path =
4827 match Unix.close_process_in ic with
4828 | (exception exn) ->
4829 error "error obtaining save path: %s" (exntos exn)
4830 | _ -> path
4832 let tmp = path ^ ".tmp" in
4833 savedoc tmp;
4834 Unix.rename tmp path;
4837 let viewkeyboard key mask =
4838 let enttext te =
4839 let mode = state.mode in
4840 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4841 state.text <- E.s;
4842 enttext ();
4843 G.postRedisplay "view:enttext"
4845 let ctrl = Wsi.withctrl mask in
4846 let key =
4847 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
4849 match key with
4850 | @Q -> exit 0
4852 | @W ->
4853 if hasunsavedchanges ()
4854 then save ()
4856 | @insert ->
4857 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4858 then (
4859 state.mode <- LinkNav (Ltgendir 0);
4860 gotoy state.y;
4862 else showtext '!' "Keyboard link navigation does not work under rotation"
4864 | @escape | @q ->
4865 begin match state.mstate with
4866 | Mzoomrect _ ->
4867 resetmstate ();
4868 G.postRedisplay "kill rect";
4869 | Msel _
4870 | Mpan _
4871 | Mscrolly | Mscrollx
4872 | Mzoom _
4873 | Mnone ->
4874 begin match state.mode with
4875 | LinkNav _ ->
4876 state.mode <- View;
4877 G.postRedisplay "esc leave linknav"
4878 | Birdseye _
4879 | Textentry _
4880 | View ->
4881 match state.ranchors with
4882 | [] -> raise Quit
4883 | (path, password, anchor, origin) :: rest ->
4884 state.ranchors <- rest;
4885 state.anchor <- anchor;
4886 state.origin <- origin;
4887 state.nameddest <- E.s;
4888 opendoc path password
4889 end;
4890 end;
4892 | @backspace ->
4893 gotoghyll (getnav ~-1)
4895 | @o ->
4896 enteroutlinemode ()
4898 | @H ->
4899 enterhistmode ()
4901 | @u ->
4902 state.rects <- [];
4903 state.text <- E.s;
4904 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap;
4905 G.postRedisplay "dehighlight";
4907 | @slash | @question ->
4908 let ondone isforw s =
4909 cbput state.hists.pat s;
4910 state.searchpattern <- s;
4911 search s isforw
4913 let s = String.make 1 (Char.chr key) in
4914 enttext (s, E.s, Some (onhist state.hists.pat),
4915 textentry, ondone (key = @slash), true)
4917 | @plus | @kpplus | @equals when ctrl ->
4918 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4919 setzoom (conf.zoom +. incr)
4921 | @plus | @kpplus ->
4922 let ondone s =
4923 let n =
4924 try int_of_string s with exc ->
4925 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
4926 max_int
4928 if n != max_int
4929 then (
4930 conf.pagebias <- n;
4931 state.text <- "page bias is now " ^ string_of_int n;
4934 enttext ("page bias: ", E.s, None, intentry, ondone, true)
4936 | @minus | @kpminus when ctrl ->
4937 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4938 setzoom (max 0.01 (conf.zoom -. decr))
4940 | @minus | @kpminus ->
4941 let ondone msg = state.text <- msg in
4942 enttext (
4943 "option [acfhilpstvxACFPRSZTISM]: ", E.s, None,
4944 optentry state.mode, ondone, true
4947 | @0 when ctrl ->
4948 if conf.zoom = 1.0
4949 then (
4950 state.x <- 0;
4951 gotoy state.y
4953 else setzoom 1.0
4955 | (@1 | @2) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
4956 let cols =
4957 match conf.columns with
4958 | Csingle _ | Cmulti _ -> 1
4959 | Csplit (n, _) -> n
4961 let h = state.winh -
4962 conf.interpagespace lsl (if conf.presentation then 1 else 0)
4964 let zoom = zoomforh state.winw h (vscrollw ()) cols in
4965 if zoom > 0.0 && (key = 50 || zoom < 1.0)
4966 then setzoom zoom
4968 | @3 when ctrl ->
4969 let fm =
4970 match conf.fitmodel with
4971 | FitWidth -> FitProportional
4972 | FitProportional -> FitPage
4973 | FitPage -> FitWidth
4975 state.text <- "fit model: " ^ FMTE.to_string fm;
4976 reqlayout conf.angle fm
4978 | @F9 ->
4979 togglebirdseye ()
4981 | @9 when ctrl ->
4982 togglebirdseye ()
4984 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4985 when not ctrl -> (* 0..9 *)
4986 let ondone s =
4987 let n =
4988 try int_of_string s with exc ->
4989 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
4992 if n >= 0
4993 then (
4994 addnav ();
4995 cbput state.hists.pag (string_of_int n);
4996 gotopage1 (n + conf.pagebias - 1) 0;
4999 let pageentry text key =
5000 match Char.unsafe_chr key with
5001 | 'g' -> TEdone text
5002 | _ -> intentry text key
5004 let text = String.make 1 (Char.chr key) in
5005 enttext (":", text, Some (onhist state.hists.pag),
5006 pageentry, ondone, true)
5008 | @b ->
5009 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
5010 reshape state.winw state.winh;
5012 | @B ->
5013 state.bzoom <- not state.bzoom;
5014 state.rects <- [];
5015 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
5017 | @l ->
5018 conf.hlinks <- not conf.hlinks;
5019 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5020 G.postRedisplay "toggle highlightlinks";
5022 | @F ->
5023 state.glinks <- true;
5024 let mode = state.mode in
5025 state.mode <- Textentry (
5026 (":", E.s, None, linknentry, linkndone gotounder, false),
5027 (fun _ ->
5028 state.glinks <- false;
5029 state.mode <- mode)
5031 state.text <- E.s;
5032 G.postRedisplay "view:linkent(F)"
5034 | @y ->
5035 state.glinks <- true;
5036 let mode = state.mode in
5037 state.mode <- Textentry (
5039 ":", E.s, None, linknentry, linkndone (fun under ->
5040 selstring (undertext under);
5041 ), false
5043 fun _ ->
5044 state.glinks <- false;
5045 state.mode <- mode
5047 state.text <- E.s;
5048 G.postRedisplay "view:linkent"
5050 | @a ->
5051 begin match state.autoscroll with
5052 | Some step ->
5053 conf.autoscrollstep <- step;
5054 state.autoscroll <- None
5055 | None ->
5056 if conf.autoscrollstep = 0
5057 then state.autoscroll <- Some 1
5058 else state.autoscroll <- Some conf.autoscrollstep
5061 | @p when ctrl ->
5062 launchpath ()
5064 | @P ->
5065 setpresentationmode (not conf.presentation);
5066 showtext ' ' ("presentation mode " ^
5067 if conf.presentation then "on" else "off");
5069 | @f ->
5070 if List.mem Wsi.Fullscreen state.winstate
5071 then Wsi.reshape conf.cwinw conf.cwinh
5072 else Wsi.fullscreen ()
5074 | @p | @N ->
5075 search state.searchpattern false
5077 | @n | @F3 ->
5078 search state.searchpattern true
5080 | @t ->
5081 begin match state.layout with
5082 | [] -> ()
5083 | l :: _ ->
5084 gotoghyll (getpagey l.pageno)
5087 | @space ->
5088 nextpage ()
5090 | @delete | @kpdelete -> (* delete *)
5091 prevpage ()
5093 | @equals ->
5094 showtext ' ' (describe_location ());
5096 | @w ->
5097 begin match state.layout with
5098 | [] -> ()
5099 | l :: _ ->
5100 Wsi.reshape (l.pagew + vscrollw ()) l.pageh;
5101 G.postRedisplay "w"
5104 | @apos ->
5105 enterbookmarkmode ()
5107 | @h | @F1 ->
5108 enterhelpmode ()
5110 | @i ->
5111 enterinfomode ()
5113 | @e when Buffer.length state.errmsgs > 0 ->
5114 entermsgsmode ()
5116 | @m ->
5117 let ondone s =
5118 match state.layout with
5119 | l :: _ ->
5120 if nonemptystr s
5121 then
5122 state.bookmarks <-
5123 (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
5124 | _ -> ()
5126 enttext ("bookmark: ", E.s, None, textentry, ondone, true)
5128 | @tilde ->
5129 quickbookmark ();
5130 showtext ' ' "Quick bookmark added";
5132 | @z ->
5133 begin match state.layout with
5134 | l :: _ ->
5135 let rect = getpdimrect l.pagedimno in
5136 let w, h =
5137 if conf.crophack
5138 then
5139 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5140 truncate (1.2 *. (rect.(3) -. rect.(0))))
5141 else
5142 (truncate (rect.(1) -. rect.(0)),
5143 truncate (rect.(3) -. rect.(0)))
5145 let w = truncate ((float w)*.conf.zoom)
5146 and h = truncate ((float h)*.conf.zoom) in
5147 if w != 0 && h != 0
5148 then (
5149 state.anchor <- getanchor ();
5150 Wsi.reshape (w + vscrollw ()) (h + conf.interpagespace)
5152 G.postRedisplay "z";
5154 | [] -> ()
5157 | @x -> state.roam ()
5159 | @Lt | @Gt ->
5160 reqlayout (conf.angle +
5161 (if key = @Gt then 30 else -30)) conf.fitmodel
5163 | @Lb | @Rb ->
5164 conf.colorscale <-
5165 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5167 G.postRedisplay "brightness";
5169 | @c when state.mode = View ->
5170 if Wsi.withalt mask
5171 then (
5172 if conf.zoom > 1.0
5173 then
5174 let m = (wadjsb () + state.winw - state.w) / 2 in
5175 state.x <- m;
5176 gotoy_and_clear_text state.y
5178 else
5179 let (c, a, b), z =
5180 match state.prevcolumns with
5181 | None -> (1, 0, 0), 1.0
5182 | Some (columns, z) ->
5183 let cab =
5184 match columns with
5185 | Csplit (c, _) -> -c, 0, 0
5186 | Cmulti ((c, a, b), _) -> c, a, b
5187 | Csingle _ -> 1, 0, 0
5189 cab, z
5191 setcolumns View c a b;
5192 setzoom z
5194 | @down | @up when ctrl && Wsi.withshift mask ->
5195 let zoom, x = state.prevzoom in
5196 setzoom zoom;
5197 state.x <- x;
5199 | @k | @up | @kpup ->
5200 begin match state.autoscroll with
5201 | None ->
5202 begin match state.mode with
5203 | Birdseye beye -> upbirdseye 1 beye
5204 | Textentry _
5205 | View
5206 | LinkNav _ ->
5207 if ctrl
5208 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5209 else (
5210 if not (Wsi.withshift mask) && conf.presentation
5211 then prevpage ()
5212 else gotoghyll1 true (clamp (-conf.scrollstep))
5215 | Some n ->
5216 setautoscrollspeed n false
5219 | @j | @down | @kpdown ->
5220 begin match state.autoscroll with
5221 | None ->
5222 begin match state.mode with
5223 | Birdseye beye -> downbirdseye 1 beye
5224 | Textentry _
5225 | View
5226 | LinkNav _ ->
5227 if ctrl
5228 then gotoy_and_clear_text (clamp (state.winh/2))
5229 else (
5230 if not (Wsi.withshift mask) && conf.presentation
5231 then nextpage ()
5232 else gotoghyll1 true (clamp (conf.scrollstep))
5235 | Some n ->
5236 setautoscrollspeed n true
5239 | @left | @right | @kpleft | @kpright when not (Wsi.withalt mask) ->
5240 if canpan ()
5241 then
5242 let dx =
5243 if ctrl
5244 then state.winw / 2
5245 else conf.hscrollstep
5247 let dx = if key = @left || key = @kpleft then dx else -dx in
5248 state.x <- panbound (state.x + dx);
5249 gotoy_and_clear_text state.y
5250 else (
5251 state.text <- E.s;
5252 G.postRedisplay "left/right"
5255 | @prior | @kpprior ->
5256 let y =
5257 if ctrl
5258 then
5259 match state.layout with
5260 | [] -> state.y
5261 | l :: _ -> state.y - l.pagey
5262 else
5263 clamp (pgscale (-state.winh))
5265 gotoghyll y
5267 | @next | @kpnext ->
5268 let y =
5269 if ctrl
5270 then
5271 match List.rev state.layout with
5272 | [] -> state.y
5273 | l :: _ -> getpagey l.pageno
5274 else
5275 clamp (pgscale state.winh)
5277 gotoghyll y
5279 | @g | @home | @kphome ->
5280 addnav ();
5281 gotoghyll 0
5282 | @G | @jend | @kpend ->
5283 addnav ();
5284 gotoghyll (clamp state.maxy)
5286 | @right | @kpright when Wsi.withalt mask ->
5287 gotoghyll (getnav 1)
5288 | @left | @kpleft when Wsi.withalt mask ->
5289 gotoghyll (getnav ~-1)
5291 | @r ->
5292 reload ()
5294 | @v when conf.debug ->
5295 state.rects <- [];
5296 List.iter (fun l ->
5297 match getopaque l.pageno with
5298 | None -> ()
5299 | Some opaque ->
5300 let x0, y0, x1, y1 = pagebbox opaque in
5301 let a,b = float x0, float y0 in
5302 let c,d = float x1, float y0 in
5303 let e,f = float x1, float y1 in
5304 let h,j = float x0, float y1 in
5305 let rect = (a,b,c,d,e,f,h,j) in
5306 debugrect rect;
5307 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5308 ) state.layout;
5309 G.postRedisplay "v";
5311 | @pipe ->
5312 let mode = state.mode in
5313 let cmd = ref E.s in
5314 let onleave = function
5315 | Cancel -> state.mode <- mode
5316 | Confirm ->
5317 List.iter (fun l ->
5318 match getopaque l.pageno with
5319 | Some opaque -> pipesel opaque !cmd
5320 | None -> ()) state.layout;
5321 state.mode <- mode
5323 let ondone s =
5324 cbput state.hists.sel s;
5325 cmd := s
5327 let te =
5328 "| ", !cmd, Some (onhist state.hists.sel), textentry, ondone, true
5330 G.postRedisplay "|";
5331 state.mode <- Textentry (te, onleave);
5333 | _ ->
5334 vlog "huh? %s" (Wsi.keyname key)
5337 let linknavkeyboard key mask linknav =
5338 let getpage pageno =
5339 let rec loop = function
5340 | [] -> None
5341 | l :: _ when l.pageno = pageno -> Some l
5342 | _ :: rest -> loop rest
5343 in loop state.layout
5345 let doexact (pageno, n) =
5346 match getopaque pageno, getpage pageno with
5347 | Some opaque, Some l ->
5348 if key = @enter || key = @kpenter
5349 then
5350 let under = getlink opaque n in
5351 G.postRedisplay "link gotounder";
5352 gotounder under;
5353 state.mode <- View;
5354 else
5355 let opt, dir =
5356 match key with
5357 | @home ->
5358 Some (findlink opaque LDfirst), -1
5360 | @jend ->
5361 Some (findlink opaque LDlast), 1
5363 | @left ->
5364 Some (findlink opaque (LDleft n)), -1
5366 | @right ->
5367 Some (findlink opaque (LDright n)), 1
5369 | @up ->
5370 Some (findlink opaque (LDup n)), -1
5372 | @down ->
5373 Some (findlink opaque (LDdown n)), 1
5375 | _ -> None, 0
5377 let pwl l dir =
5378 begin match findpwl l.pageno dir with
5379 | Pwlnotfound -> ()
5380 | Pwl pageno ->
5381 let notfound dir =
5382 state.mode <- LinkNav (Ltgendir dir);
5383 let y, h = getpageyh pageno in
5384 let y =
5385 if dir < 0
5386 then y + h - state.winh
5387 else y
5389 gotoy y
5391 begin match getopaque pageno, getpage pageno with
5392 | Some opaque, Some _ ->
5393 let link =
5394 let ld = if dir > 0 then LDfirst else LDlast in
5395 findlink opaque ld
5397 begin match link with
5398 | Lfound m ->
5399 showlinktype (getlink opaque m);
5400 state.mode <- LinkNav (Ltexact (pageno, m));
5401 G.postRedisplay "linknav jpage";
5402 | Lnotfound -> notfound dir
5403 end;
5404 | _ -> notfound dir
5405 end;
5406 end;
5408 begin match opt with
5409 | Some Lnotfound -> pwl l dir;
5410 | Some (Lfound m) ->
5411 if m = n
5412 then pwl l dir
5413 else (
5414 let _, y0, _, y1 = getlinkrect opaque m in
5415 if y0 < l.pagey
5416 then gotopage1 l.pageno y0
5417 else (
5418 let d = fstate.fontsize + 1 in
5419 if y1 - l.pagey > l.pagevh - d
5420 then gotopage1 l.pageno (y1 - state.winh - hscrollh () + d)
5421 else G.postRedisplay "linknav";
5423 showlinktype (getlink opaque m);
5424 state.mode <- LinkNav (Ltexact (l.pageno, m));
5427 | None -> viewkeyboard key mask
5428 end;
5429 | _ -> viewkeyboard key mask
5431 if key = @insert
5432 then (
5433 state.mode <- View;
5434 G.postRedisplay "leave linknav"
5436 else
5437 match linknav with
5438 | Ltgendir _ | Ltnotready _ -> viewkeyboard key mask
5439 | Ltexact exact -> doexact exact
5442 let keyboard key mask =
5443 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5444 then wcmd "interrupt"
5445 else state.uioh <- state.uioh#key key mask
5448 let birdseyekeyboard key mask
5449 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5450 let incr =
5451 match conf.columns with
5452 | Csingle _ -> 1
5453 | Cmulti ((c, _, _), _) -> c
5454 | Csplit _ -> failwith "bird's eye split mode"
5456 let pgh layout = List.fold_left
5457 (fun m l -> max l.pageh m) state.winh layout in
5458 match key with
5459 | @l when Wsi.withctrl mask ->
5460 let y, h = getpageyh pageno in
5461 let top = (state.winh - h) / 2 in
5462 gotoy (max 0 (y - top))
5463 | @enter | @kpenter -> leavebirdseye beye false
5464 | @escape -> leavebirdseye beye true
5465 | @up -> upbirdseye incr beye
5466 | @down -> downbirdseye incr beye
5467 | @left -> upbirdseye 1 beye
5468 | @right -> downbirdseye 1 beye
5470 | @prior ->
5471 begin match state.layout with
5472 | l :: _ ->
5473 if l.pagey != 0
5474 then (
5475 state.mode <- Birdseye (
5476 oconf, leftx, l.pageno, hooverpageno, anchor
5478 gotopage1 l.pageno 0;
5480 else (
5481 let layout = layout (state.y-state.winh) (pgh state.layout) in
5482 match layout with
5483 | [] -> gotoy (clamp (-state.winh))
5484 | l :: _ ->
5485 state.mode <- Birdseye (
5486 oconf, leftx, l.pageno, hooverpageno, anchor
5488 gotopage1 l.pageno 0
5491 | [] -> gotoy (clamp (-state.winh))
5492 end;
5494 | @next ->
5495 begin match List.rev state.layout with
5496 | l :: _ ->
5497 let layout = layout (state.y + (pgh state.layout)) state.winh in
5498 begin match layout with
5499 | [] ->
5500 let incr = l.pageh - l.pagevh in
5501 if incr = 0
5502 then (
5503 state.mode <-
5504 Birdseye (
5505 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5507 G.postRedisplay "birdseye pagedown";
5509 else gotoy (clamp (incr + conf.interpagespace*2));
5511 | l :: _ ->
5512 state.mode <-
5513 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5514 gotopage1 l.pageno 0;
5517 | [] -> gotoy (clamp state.winh)
5518 end;
5520 | @home ->
5521 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5522 gotopage1 0 0
5524 | @jend ->
5525 let pageno = state.pagecount - 1 in
5526 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5527 if not (pagevisible state.layout pageno)
5528 then
5529 let h =
5530 match List.rev state.pdims with
5531 | [] -> state.winh
5532 | (_, _, h, _) :: _ -> h
5534 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5535 else G.postRedisplay "birdseye end";
5537 | _ -> viewkeyboard key mask
5540 let drawpage l =
5541 let color =
5542 match state.mode with
5543 | Textentry _ -> scalecolor 0.4
5544 | LinkNav _
5545 | View -> scalecolor 1.0
5546 | Birdseye (_, _, pageno, hooverpageno, _) ->
5547 if l.pageno = hooverpageno
5548 then scalecolor 0.9
5549 else (
5550 if l.pageno = pageno
5551 then (
5552 let c = scalecolor 1.0 in
5553 GlDraw.color c;
5554 GlDraw.line_width 3.0;
5555 let dispx = xadjsb () + l.pagedispx in
5556 linerect
5557 (float (dispx-1)) (float (l.pagedispy-1))
5558 (float (dispx+l.pagevw+1))
5559 (float (l.pagedispy+l.pagevh+1))
5561 GlDraw.line_width 1.0;
5564 else scalecolor 0.8
5567 drawtiles l color;
5570 let postdrawpage l linkindexbase =
5571 match getopaque l.pageno with
5572 | Some opaque ->
5573 if tileready l l.pagex l.pagey
5574 then
5575 let x = l.pagedispx - l.pagex + xadjsb ()
5576 and y = l.pagedispy - l.pagey in
5577 let hlmask =
5578 match conf.columns with
5579 | Csingle _ | Cmulti _ ->
5580 (if conf.hlinks then 1 else 0)
5581 + (if state.glinks
5582 && not (isbirdseye state.mode) then 2 else 0)
5583 | Csplit _ -> 0
5585 let s =
5586 match state.mode with
5587 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5588 | Textentry _
5589 | Birdseye _
5590 | View
5591 | LinkNav _ -> E.s
5593 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5594 else 0
5595 | _ -> 0
5598 let scrollindicator () =
5599 let sbw, ph, sh = state.uioh#scrollph in
5600 let sbh, pw, sw = state.uioh#scrollpw in
5602 let x0,x1,hx0 =
5603 if conf.leftscroll
5604 then (0, sbw, sbw)
5605 else ((state.winw - sbw), state.winw, 0)
5608 GlDraw.color (0.64, 0.64, 0.64);
5609 filledrect (float x0) 0. (float x1) (float state.winh);
5610 filledrect
5611 (float hx0) (float (state.winh - sbh))
5612 (float (hx0 + wadjsb () + state.winw)) (float state.winh)
5614 GlDraw.color (0.0, 0.0, 0.0);
5616 filledrect (float x0) ph (float x1) (ph +. sh);
5617 let pw = pw +. float hx0 in
5618 filledrect pw (float (state.winh - sbh)) (pw +. sw) (float state.winh);
5621 let showsel () =
5622 match state.mstate with
5623 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5626 | Msel ((x0, y0), (x1, y1)) ->
5627 let identify opaque l px py = Some (opaque, l.pageno, px, py) in
5628 let o0,n0,px0,py0 = onppundermouse identify x0 y0 (~< E.s, -1, 0, 0) in
5629 let _o1,n1,px1,py1 = onppundermouse identify x1 y1 (~< E.s, -1, 0, 0) in
5630 if n0 != -1 && n0 = n1 then seltext o0 (px0, py0, px1, py1);
5633 let showrects = function [] -> () | rects ->
5634 Gl.enable `blend;
5635 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5636 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5637 List.iter
5638 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5639 List.iter (fun l ->
5640 if l.pageno = pageno
5641 then (
5642 let dx = float (l.pagedispx - l.pagex) in
5643 let dy = float (l.pagedispy - l.pagey) in
5644 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5645 Raw.sets_float state.vraw ~pos:0
5646 [| x0+.dx; y0+.dy;
5647 x1+.dx; y1+.dy;
5648 x3+.dx; y3+.dy;
5649 x2+.dx; y2+.dy |];
5650 GlArray.vertex `two state.vraw;
5651 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
5653 ) state.layout
5654 ) rects
5656 Gl.disable `blend;
5659 let display () =
5660 GlClear.color (scalecolor2 conf.bgcolor);
5661 GlClear.clear [`color];
5662 List.iter drawpage state.layout;
5663 let rects =
5664 match state.mode with
5665 | LinkNav (Ltexact (pageno, linkno)) ->
5666 begin match getopaque pageno with
5667 | Some opaque ->
5668 let dx = xadjsb () in
5669 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5670 let x0 = x0 + dx and x1 = x1 + dx in
5671 (pageno, 5, (
5672 float x0, float y0,
5673 float x1, float y0,
5674 float x1, float y1,
5675 float x0, float y1)
5676 ) :: state.rects
5677 | None -> state.rects
5679 | LinkNav (Ltgendir _) | LinkNav (Ltnotready _)
5680 | Birdseye _
5681 | Textentry _
5682 | View -> state.rects
5684 showrects rects;
5685 let rec postloop linkindexbase = function
5686 | l :: rest ->
5687 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5688 postloop linkindexbase rest
5689 | [] -> ()
5691 showsel ();
5692 postloop 0 state.layout;
5693 state.uioh#display;
5694 begin match state.mstate with
5695 | Mzoomrect ((x0, y0), (x1, y1)) ->
5696 Gl.enable `blend;
5697 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5698 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5699 filledrect (float x0) (float y0) (float x1) (float y1);
5700 Gl.disable `blend;
5701 | Msel _
5702 | Mpan _
5703 | Mscrolly | Mscrollx
5704 | Mzoom _
5705 | Mnone -> ()
5706 end;
5707 enttext ();
5708 scrollindicator ();
5709 Wsi.swapb ();
5712 let zoomrect x y x1 y1 =
5713 let x0 = min x x1
5714 and x1 = max x x1
5715 and y0 = min y y1 in
5716 gotoy (state.y + y0);
5717 state.anchor <- getanchor ();
5718 let zoom = (float state.w) /. float (x1 - x0) in
5719 let margin =
5720 let simple () =
5721 let adjw = wadjsb () + state.winw in
5722 if state.w < adjw
5723 then (adjw - state.w) / 2
5724 else 0
5726 match conf.fitmodel with
5727 | FitWidth | FitProportional -> simple ()
5728 | FitPage ->
5729 match conf.columns with
5730 | Csplit _ ->
5731 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
5732 | Cmulti _ | Csingle _ -> simple ()
5734 state.x <- (state.x + margin) - x0;
5735 setzoom zoom;
5736 resetmstate ();
5739 let annot inline x y =
5740 match unproject x y with
5741 | Some (opaque, n, ux, uy) ->
5742 let add text =
5743 addannot opaque ux uy text;
5744 wcmd "freepage %s" (~> opaque);
5745 Hashtbl.remove state.pagemap (n, state.gen);
5746 flushtiles ();
5747 gotoy state.y
5749 if inline
5750 then
5751 let ondone s = add s in
5752 let mode = state.mode in
5753 state.mode <- Textentry (
5754 ("annotation: ", E.s, None, textentry, ondone, true),
5755 fun _ -> state.mode <- mode);
5756 state.text <- E.s;
5757 enttext ();
5758 G.postRedisplay "annot"
5759 else
5760 let text =
5761 let s = getusertext E.s in
5762 let l = Str.split newlinere s in
5763 String.concat " " l
5765 add text
5766 | _ -> ()
5769 let zoomblock x y =
5770 let g opaque l px py =
5771 match rectofblock opaque px py with
5772 | Some a ->
5773 let x0 = a.(0) -. 20. in
5774 let x1 = a.(1) +. 20. in
5775 let y0 = a.(2) -. 20. in
5776 let zoom = (float state.w) /. (x1 -. x0) in
5777 let pagey = getpagey l.pageno in
5778 gotoy_and_clear_text (pagey + truncate y0);
5779 state.anchor <- getanchor ();
5780 let margin = (state.w - l.pagew)/2 in
5781 state.x <- -truncate x0 - margin;
5782 setzoom zoom;
5783 None
5784 | None -> None
5786 match conf.columns with
5787 | Csplit _ ->
5788 showtext '!' "block zooming does not work properly in split columns mode"
5789 | Cmulti _ | Csingle _ -> onppundermouse g x y ()
5792 let scrollx x =
5793 let winw = wadjsb () + state.winw - 1 in
5794 let s = float x /. float winw in
5795 let destx = truncate (float (state.w + winw) *. s) in
5796 state.x <- winw - destx;
5797 gotoy_and_clear_text state.y;
5798 state.mstate <- Mscrollx;
5801 let scrolly y =
5802 let s = float y /. float state.winh in
5803 let desty = truncate (float (state.maxy - state.winh) *. s) in
5804 gotoy_and_clear_text desty;
5805 state.mstate <- Mscrolly;
5808 let viewmulticlick clicks x y mask =
5809 let g opaque l px py =
5810 let mark =
5811 match clicks with
5812 | 2 -> Mark_word
5813 | 3 -> Mark_line
5814 | 4 -> Mark_block
5815 | _ -> Mark_page
5817 if markunder opaque px py mark
5818 then (
5819 Some (fun () ->
5820 let dopipe cmd =
5821 match getopaque l.pageno with
5822 | None -> ()
5823 | Some opaque -> pipesel opaque cmd
5825 state.roam <- (fun () -> dopipe conf.paxcmd);
5826 if not (Wsi.withctrl mask) then dopipe conf.selcmd;
5829 else None
5831 G.postRedisplay "viewmulticlick";
5832 onppundermouse g x y (fun () -> showtext '!' "Nothing to select") ();
5835 let canselect () =
5836 match conf.columns with
5837 | Csplit _ -> false
5838 | Csingle _ | Cmulti _ -> conf.angle mod 360 = 0
5841 let viewmouse button down x y mask =
5842 match button with
5843 | n when (n == 4 || n == 5) && not down ->
5844 if Wsi.withctrl mask
5845 then (
5846 match state.mstate with
5847 | Mzoom (oldn, i) ->
5848 if oldn = n
5849 then (
5850 if i = 2
5851 then
5852 let incr =
5853 match n with
5854 | 5 ->
5855 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5856 | _ ->
5857 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5859 let zoom = conf.zoom -. incr in
5860 setzoom zoom;
5861 state.mstate <- Mzoom (n, 0);
5862 else
5863 state.mstate <- Mzoom (n, i+1);
5865 else state.mstate <- Mzoom (n, 0)
5867 | Msel _
5868 | Mpan _
5869 | Mscrolly | Mscrollx
5870 | Mzoomrect _
5871 | Mnone -> state.mstate <- Mzoom (n, 0)
5873 else (
5874 match state.autoscroll with
5875 | Some step -> setautoscrollspeed step (n=4)
5876 | None ->
5877 if conf.wheelbypage || conf.presentation
5878 then (
5879 if n = 4
5880 then prevpage ()
5881 else nextpage ()
5883 else
5884 let incr =
5885 if n = 4
5886 then -conf.scrollstep
5887 else conf.scrollstep
5889 let incr = incr * 2 in
5890 let y = clamp incr in
5891 gotoy_and_clear_text y
5894 | n when (n = 6 || n = 7) && not down && canpan () ->
5895 state.x <-
5896 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
5897 gotoy_and_clear_text state.y
5899 | 1 when Wsi.withshift mask ->
5900 state.mstate <- Mnone;
5901 if not down
5902 then (
5903 match unproject x y with
5904 | Some (_, pageno, ux, uy) ->
5905 let cmd = Printf.sprintf
5906 "%s %s %d %d %d"
5907 conf.stcmd state.path pageno ux uy
5909 addpid @@ popen cmd []
5910 | None -> ()
5913 | 1 when Wsi.withctrl mask ->
5914 if down
5915 then (
5916 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5917 state.mstate <- Mpan (x, y)
5919 else
5920 state.mstate <- Mnone
5922 | 3 ->
5923 if down
5924 then (
5925 if Wsi.withshift mask
5926 then (
5927 annot (not (Wsi.withctrl mask)) x y;
5928 G.postRedisplay "addannot"
5930 else
5931 let p = (x, y) in
5932 Wsi.setcursor Wsi.CURSOR_CYCLE;
5933 state.mstate <- Mzoomrect (p, p)
5935 else (
5936 match state.mstate with
5937 | Mzoomrect ((x0, y0), _) ->
5938 if abs (x-x0) > 10 && abs (y - y0) > 10
5939 then zoomrect x0 y0 x y
5940 else (
5941 resetmstate ();
5942 G.postRedisplay "kill accidental zoom rect";
5944 | Msel _
5945 | Mpan _
5946 | Mscrolly | Mscrollx
5947 | Mzoom _
5948 | Mnone ->
5949 resetmstate ()
5952 | 1 when x > state.winw - vscrollw () ->
5953 if down
5954 then
5955 let _, position, sh = state.uioh#scrollph in
5956 if y > truncate position && y < truncate (position +. sh)
5957 then state.mstate <- Mscrolly
5958 else scrolly y
5959 else
5960 state.mstate <- Mnone
5962 | 1 when y > state.winh - hscrollh () ->
5963 if down
5964 then
5965 let _, position, sw = state.uioh#scrollpw in
5966 if x > truncate position && x < truncate (position +. sw)
5967 then state.mstate <- Mscrollx
5968 else scrollx x
5969 else
5970 state.mstate <- Mnone
5972 | 1 when state.bzoom -> if not down then zoomblock x y
5974 | 1 ->
5975 let dest = if down then getunder x y else Unone in
5976 begin match dest with
5977 | Ulinkgoto _
5978 | Ulinkuri _
5979 | Uremote _ | Uremotedest _
5980 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5981 gotounder dest
5983 | Unone when down ->
5984 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5985 state.mstate <- Mpan (x, y);
5987 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
5989 | Unone | Utext _ ->
5990 if down
5991 then (
5992 if canselect ()
5993 then (
5994 state.mstate <- Msel ((x, y), (x, y));
5995 G.postRedisplay "mouse select";
5998 else (
5999 match state.mstate with
6000 | Mnone -> ()
6002 | Mzoom _ | Mscrollx | Mscrolly ->
6003 state.mstate <- Mnone
6005 | Mzoomrect ((x0, y0), _) ->
6006 zoomrect x0 y0 x y
6008 | Mpan _ ->
6009 Wsi.setcursor Wsi.CURSOR_INHERIT;
6010 state.mstate <- Mnone
6012 | Msel ((x0, y0), (x1, y1)) ->
6013 let rec loop = function
6014 | [] -> ()
6015 | l :: rest ->
6016 let inside =
6017 let a0 = l.pagedispy in
6018 let a1 = a0 + l.pagevh in
6019 let b0 = l.pagedispx in
6020 let b1 = b0 + l.pagevw in
6021 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
6022 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
6024 if inside
6025 then
6026 match getopaque l.pageno with
6027 | Some opaque ->
6028 let dosel cmd () =
6029 match Unix.pipe () with
6030 | exception exn ->
6031 showtext '!'
6032 (Printf.sprintf
6033 "can not create sel pipe: %s"
6034 (exntos exn));
6035 | (r, w) ->
6036 let clo what fd =
6037 Ne.clo fd (fun msg ->
6038 dolog "%s close failed: %s" what msg)
6040 let pid =
6041 try popen cmd [r, 0; w, -1]
6042 with exn ->
6043 dolog "can not execute %S: %s"
6044 cmd (exntos exn);
6047 if pid > 0
6048 then (
6049 copysel w opaque;
6050 G.postRedisplay "copysel";
6052 else clo "Msel pipe/w" w;
6053 clo "Msel pipe/r" r;
6055 dosel conf.selcmd ();
6056 state.roam <- dosel conf.paxcmd;
6057 | None -> ()
6058 else loop rest
6060 loop state.layout;
6061 resetmstate ();
6065 | _ -> ()
6068 let birdseyemouse button down x y mask
6069 (conf, leftx, _, hooverpageno, anchor) =
6070 match button with
6071 | 1 when down ->
6072 let rec loop = function
6073 | [] -> ()
6074 | l :: rest ->
6075 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6076 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6077 then (
6078 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
6080 else loop rest
6082 loop state.layout
6083 | 3 -> ()
6084 | _ -> viewmouse button down x y mask
6087 let uioh = object
6088 method display = ()
6090 method key key mask =
6091 begin match state.mode with
6092 | Textentry textentry -> textentrykeyboard key mask textentry
6093 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6094 | View -> viewkeyboard key mask
6095 | LinkNav linknav -> linknavkeyboard key mask linknav
6096 end;
6097 state.uioh
6099 method button button bstate x y mask =
6100 begin match state.mode with
6101 | LinkNav _
6102 | View -> viewmouse button bstate x y mask
6103 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6104 | Textentry _ -> ()
6105 end;
6106 state.uioh
6108 method multiclick clicks x y mask =
6109 begin match state.mode with
6110 | LinkNav _
6111 | View -> viewmulticlick clicks x y mask
6112 | Birdseye _
6113 | Textentry _ -> ()
6114 end;
6115 state.uioh
6117 method motion x y =
6118 begin match state.mode with
6119 | Textentry _ -> ()
6120 | View | Birdseye _ | LinkNav _ ->
6121 match state.mstate with
6122 | Mzoom _ | Mnone -> ()
6124 | Mpan (x0, y0) ->
6125 let dx = x - x0
6126 and dy = y0 - y in
6127 state.mstate <- Mpan (x, y);
6128 if canpan ()
6129 then state.x <- panbound (state.x + dx);
6130 let y = clamp dy in
6131 gotoy_and_clear_text y
6133 | Msel (a, _) ->
6134 state.mstate <- Msel (a, (x, y));
6135 G.postRedisplay "motion select";
6137 | Mscrolly ->
6138 let y = min state.winh (max 0 y) in
6139 scrolly y
6141 | Mscrollx ->
6142 let x = min state.winw (max 0 x) in
6143 scrollx x
6145 | Mzoomrect (p0, _) ->
6146 state.mstate <- Mzoomrect (p0, (x, y));
6147 G.postRedisplay "motion zoomrect";
6148 end;
6149 state.uioh
6151 method pmotion x y =
6152 begin match state.mode with
6153 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6154 let rec loop = function
6155 | [] ->
6156 if hooverpageno != -1
6157 then (
6158 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6159 G.postRedisplay "pmotion birdseye no hoover";
6161 | l :: rest ->
6162 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6163 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6164 then (
6165 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6166 G.postRedisplay "pmotion birdseye hoover";
6168 else loop rest
6170 loop state.layout
6172 | Textentry _ -> ()
6174 | LinkNav _
6175 | View ->
6176 match state.mstate with
6177 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ -> ()
6178 | Mnone ->
6179 updateunder x y;
6180 if canselect ()
6181 then
6182 match conf.pax with
6183 | None -> ()
6184 | Some r ->
6185 let past, _, _ = !r in
6186 let now = now () in
6187 let delta = now -. past in
6188 if delta > 0.01
6189 then paxunder x y
6190 else r := (now, x, y)
6191 end;
6192 state.uioh
6194 method infochanged _ = ()
6196 method scrollph =
6197 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6198 let p, h =
6199 if maxy = 0
6200 then 0.0, float state.winh
6201 else scrollph state.y maxy
6203 vscrollw (), p, h
6205 method scrollpw =
6206 let winw = wadjsb () + state.winw in
6207 let fwinw = float winw in
6208 let sw =
6209 let sw = fwinw /. float state.w in
6210 let sw = fwinw *. sw in
6211 max sw (float conf.scrollh)
6213 let position =
6214 let maxx = state.w + winw in
6215 let x = winw - state.x in
6216 let percent = float x /. float maxx in
6217 (fwinw -. sw) *. percent
6219 hscrollh (), position, sw
6221 method modehash =
6222 let modename =
6223 match state.mode with
6224 | LinkNav _ -> "links"
6225 | Textentry _ -> "textentry"
6226 | Birdseye _ -> "birdseye"
6227 | View -> "view"
6229 findkeyhash conf modename
6231 method eformsgs = true
6232 method alwaysscrolly = false
6233 end;;
6235 let adderrmsg src msg =
6236 Buffer.add_string state.errmsgs msg;
6237 state.newerrmsgs <- true;
6238 G.postRedisplay src
6241 let adderrfmt src fmt =
6242 Format.ksprintf (fun s -> adderrmsg src s) fmt;
6245 let ract cmds =
6246 let cl = splitatspace cmds in
6247 let scan s fmt f =
6248 try Scanf.sscanf s fmt f
6249 with exn ->
6250 adderrfmt "remote exec"
6251 "error processing '%S': %s\n" cmds (exntos exn)
6253 match cl with
6254 | "reload" :: [] -> reload ()
6255 | "goto" :: args :: [] ->
6256 scan args "%u %f %f"
6257 (fun pageno x y ->
6258 let cmd, _ = state.geomcmds in
6259 if emptystr cmd
6260 then gotopagexy pageno x y
6261 else
6262 let f prevf () =
6263 gotopagexy pageno x y;
6264 prevf ()
6266 state.reprf <- f state.reprf
6268 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
6269 | "gotor" :: args :: [] ->
6270 scan args "%S %u"
6271 (fun filename pageno -> gotounder (Uremote (filename, pageno)))
6272 | "gotord" :: args :: [] ->
6273 scan args "%S %S"
6274 (fun filename dest -> gotounder (Uremotedest (filename, dest)))
6275 | "rect" :: args :: [] ->
6276 scan args "%u %u %f %f %f %f"
6277 (fun pageno color x0 y0 x1 y1 ->
6278 onpagerect pageno (fun w h ->
6279 let _,w1,h1,_ = getpagedim pageno in
6280 let sw = float w1 /. float w
6281 and sh = float h1 /. float h in
6282 let x0s = x0 *. sw
6283 and x1s = x1 *. sw
6284 and y0s = y0 *. sh
6285 and y1s = y1 *. sh in
6286 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
6287 debugrect rect;
6288 state.rects <- (pageno, color, rect) :: state.rects;
6289 G.postRedisplay "rect";
6292 | "activatewin" :: [] -> Wsi.activatewin ()
6293 | "quit" :: [] -> raise Quit
6294 | _ ->
6295 adderrfmt "remote command"
6296 "error processing remote command: %S\n" cmds;
6299 let remote =
6300 let scratch = Bytes.create 80 in
6301 let buf = Buffer.create 80 in
6302 fun fd ->
6303 let rec tempfr () =
6304 try Some (Unix.read fd scratch 0 80)
6305 with
6306 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
6307 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
6308 | exn -> raise exn
6310 match tempfr () with
6311 | None -> Some fd
6312 | Some n ->
6313 if n = 0
6314 then (
6315 Unix.close fd;
6316 if Buffer.length buf > 0
6317 then (
6318 let s = Buffer.contents buf in
6319 Buffer.clear buf;
6320 ract s;
6322 None
6324 else
6325 let rec eat ppos =
6326 let nlpos =
6328 let pos = Bytes.index_from scratch ppos '\n' in
6329 if pos >= n then -1 else pos
6330 with Not_found -> -1
6332 if nlpos >= 0
6333 then (
6334 Buffer.add_subbytes buf scratch ppos (nlpos-ppos);
6335 let s = Buffer.contents buf in
6336 Buffer.clear buf;
6337 ract s;
6338 eat (nlpos+1);
6340 else (
6341 Buffer.add_subbytes buf scratch ppos (n-ppos);
6342 Some fd
6344 in eat 0
6347 let remoteopen path =
6348 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
6349 with exn ->
6350 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
6351 None
6354 let () =
6355 let gcconfig = ref E.s in
6356 let trimcachepath = ref E.s in
6357 let rcmdpath = ref E.s in
6358 let pageno = ref None in
6359 let rootwid = ref 0 in
6360 let openlast = ref false in
6361 let nofc = 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 not (emptystr !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 Printf.eprintf "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 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 signum =
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 signum
6643 Sys.set_signal Sys.sigchld (Sys.Signal_handle reap);
6645 let optrfd =
6646 ref (
6647 if nonemptystr !rcmdpath
6648 then remoteopen !rcmdpath
6649 else None
6653 let rec loop deadline =
6654 let r =
6655 match state.errfd with
6656 | None -> [state.ss; state.wsfd]
6657 | Some fd -> [state.ss; state.wsfd; fd]
6659 let r =
6660 match !optrfd with
6661 | None -> r
6662 | Some fd -> fd :: r
6664 if state.redisplay
6665 then (
6666 state.redisplay <- false;
6667 display ();
6669 let timeout =
6670 let now = now () in
6671 if deadline > now
6672 then (
6673 if deadline = infinity
6674 then ~-.1.0
6675 else max 0.0 (deadline -. now)
6677 else 0.0
6679 let r, _, _ =
6680 try Unix.select r [] [] timeout
6681 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6683 begin match r with
6684 | [] ->
6685 state.ghyll None;
6686 let newdeadline =
6687 if state.ghyll == noghyll
6688 then
6689 match state.autoscroll with
6690 | Some step when step != 0 ->
6691 let y = state.y + step in
6692 let y =
6693 if y < 0
6694 then state.maxy
6695 else if y >= state.maxy then 0 else y
6697 gotoy y;
6698 if state.mode = View
6699 then state.text <- E.s;
6700 deadline +. 0.01
6701 | _ -> infinity
6702 else deadline +. 0.01
6704 loop newdeadline
6706 | l ->
6707 let rec checkfds = function
6708 | [] -> ()
6709 | fd :: rest when fd = state.ss ->
6710 let cmd = readcmd state.ss in
6711 act cmd;
6712 checkfds rest
6714 | fd :: rest when fd = state.wsfd ->
6715 Wsi.readresp fd;
6716 checkfds rest
6718 | fd :: rest when Some fd = !optrfd ->
6719 begin match remote fd with
6720 | None -> optrfd := remoteopen !rcmdpath;
6721 | opt -> optrfd := opt
6722 end;
6723 checkfds rest
6725 | fd :: rest ->
6726 let s = Bytes.create 80 in
6727 let n = tempfailureretry (Unix.read fd s 0) 80 in
6728 if conf.redirectstderr
6729 then (
6730 Buffer.add_substring state.errmsgs (Bytes.to_string s) 0 n;
6731 state.newerrmsgs <- true;
6732 state.redisplay <- true;
6734 else (
6735 prerr_string (String.sub (Bytes.to_string s) 0 n);
6736 flush stderr;
6738 checkfds rest
6740 checkfds l;
6741 if !reeenterhist then (
6742 enterhistmode ();
6743 reeenterhist := false;
6745 let newdeadline =
6746 let deadline1 =
6747 if deadline = infinity
6748 then now () +. 0.01
6749 else deadline
6751 match state.autoscroll with
6752 | Some step when step != 0 -> deadline1
6753 | _ -> if state.ghyll == noghyll then infinity else deadline1
6755 loop newdeadline
6756 end;
6759 loop infinity;
6760 with Quit ->
6761 Config.save leavebirdseye;
6762 if hasunsavedchanges ()
6763 then save ();