Stop open coding existing nonemptystr
[llpp.git] / main.ml
blob25c78a36fb49bd44691e09a0789b020c91709289
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 reenterhist = ref false;;
48 let selfexec = ref E.s;;
50 let drawstring size x y s =
51 Gl.enable `blend;
52 Gl.enable `texture_2d;
53 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
54 ignore (drawstr size x y s);
55 Gl.disable `blend;
56 Gl.disable `texture_2d;
59 let drawstring1 size x y s =
60 drawstr size x y s;
63 let drawstring2 size x y fmt =
64 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
67 let debugl l =
68 dolog "l %d dim=%d {" l.pageno l.pagedimno;
69 dolog " WxH %dx%d" l.pagew l.pageh;
70 dolog " vWxH %dx%d" l.pagevw l.pagevh;
71 dolog " pagex,y %d,%d" l.pagex l.pagey;
72 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
73 dolog " column %d" l.pagecol;
74 dolog "}";
77 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
78 dolog "rect {";
79 dolog " x0,y0=(% f, % f)" x0 y0;
80 dolog " x1,y1=(% f, % f)" x1 y1;
81 dolog " x2,y2=(% f, % f)" x2 y2;
82 dolog " x3,y3=(% f, % f)" x3 y3;
83 dolog "}";
86 let isbirdseye = function
87 | Birdseye _ -> true
88 | Textentry _
89 | View
90 | LinkNav _ -> false
93 let istextentry = function
94 | Textentry _ -> true
95 | Birdseye _
96 | View
97 | LinkNav _ -> false
100 let wtmode = ref false;;
101 let cxack = ref false;;
103 let pgscale h = truncate (float h *. conf.pgscale);;
105 let hscrollh () =
106 if not state.uioh#alwaysscrolly && (conf.scrollb land scrollbhv = 0)
107 || (state.x = 0 && state.w <= state.winw - conf.scrollbw)
108 then 0
109 else conf.scrollbw
112 let vscrollw () =
113 if not state.uioh#alwaysscrolly && (conf.scrollb land scrollbvv = 0)
114 then 0
115 else conf.scrollbw
118 let wadjsb () = -vscrollw ();;
119 let xadjsb () = if conf.leftscroll then vscrollw () else 0;;
121 let setfontsize n =
122 fstate.fontsize <- n;
123 fstate.wwidth <- measurestr fstate.fontsize "w";
124 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
127 let vlog fmt =
128 if conf.verbose
129 then
130 Printf.kprintf prerr_endline fmt
131 else
132 Printf.kprintf ignore fmt
135 let launchpath () =
136 if emptystr conf.pathlauncher
137 then print_endline state.path
138 else (
139 let command = Str.global_replace percentsre state.path conf.pathlauncher in
140 try addpid @@ popen command []
141 with exn ->
142 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
143 flush stderr;
147 let redirectstderr () =
148 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
149 if conf.redirectstderr
150 then
151 match Unix.pipe () with
152 | exception exn ->
153 dolog "failed to create stderr redirection pipes: %s" (exntos exn)
155 | (r, w) ->
156 begin match Unix.dup Unix.stderr with
157 | exception exn ->
158 dolog "failed to dup stderr: %s" (exntos exn);
159 Ne.clo r (clofail "pipe/r");
160 Ne.clo w (clofail "pipe/w");
162 | dupstderr ->
163 begin match Unix.dup2 w Unix.stderr with
164 | exception exn ->
165 dolog "failed to dup2 to stderr: %s" (exntos exn);
166 Ne.clo dupstderr (clofail "stderr duplicate");
167 Ne.clo r (clofail "redir pipe/r");
168 Ne.clo w (clofail "redir pipe/w");
170 | () ->
171 state.stderr <- dupstderr;
172 state.errfd <- Some r;
173 end;
175 else (
176 state.newerrmsgs <- false;
177 begin match state.errfd with
178 | Some fd ->
179 begin match Unix.dup2 state.stderr Unix.stderr with
180 | exception exn ->
181 dolog "failed to dup2 original stderr: %s" (exntos exn)
182 | () ->
183 Ne.clo fd (clofail "dup of stderr");
184 state.errfd <- None;
185 end;
186 | None -> ()
187 end;
188 prerr_string (Buffer.contents state.errmsgs);
189 flush stderr;
190 Buffer.clear state.errmsgs;
194 module G =
195 struct
196 let postRedisplay who =
197 if conf.verbose
198 then prerr_endline ("redisplay for " ^ who);
199 state.redisplay <- true;
201 end;;
203 let getopaque pageno =
204 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
205 with Not_found -> None
208 let putopaque pageno opaque =
209 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
212 let pagetranslatepoint l x y =
213 let dy = y - l.pagedispy in
214 let y = dy + l.pagey in
215 let dx = x - l.pagedispx in
216 let x = dx + l.pagex in
217 (x, y);
220 let onppundermouse g x y d =
221 let rec f = function
222 | l :: rest ->
223 begin match getopaque l.pageno with
224 | Some opaque ->
225 let x0 = l.pagedispx in
226 let x1 = x0 + l.pagevw in
227 let y0 = l.pagedispy in
228 let y1 = y0 + l.pagevh in
229 if y >= y0 && y <= y1 && x >= x0 && x <= x1
230 then
231 let px, py = pagetranslatepoint l x y in
232 match g opaque l px py with
233 | Some res -> res
234 | None -> f rest
235 else f rest
236 | _ ->
237 f rest
239 | [] -> d
241 f state.layout
244 let getunder x y =
245 let g opaque l px py =
246 if state.bzoom
247 then (
248 match rectofblock opaque px py with
249 | Some a ->
250 let rect = (a.(0),a.(2),a.(1),a.(2),a.(1),a.(3),a.(0),a.(3)) in
251 state.rects <- [l.pageno, l.pageno mod 3, rect];
252 G.postRedisplay "getunder";
253 | None -> ()
255 let under = whatsunder opaque px py in
256 if under = Unone then None else Some under
258 onppundermouse g x y Unone
261 let unproject x y =
262 let g opaque l x y =
263 match unproject opaque x y with
264 | Some (x, y) -> Some (Some (opaque, l.pageno, x, y))
265 | None -> None
267 onppundermouse g x y None;
270 let showtext c s =
271 state.text <- Printf.sprintf "%c%s" c s;
272 G.postRedisplay "showtext";
275 let pipesel opaque cmd =
276 if hassel opaque
277 then
278 match Unix.pipe () with
279 | exception exn ->
280 showtext '!'
281 (Printf.sprintf "pipesel can not create pipe: %s" (exntos exn));
282 | (r, w) ->
283 let doclose what fd =
284 Ne.clo fd (fun msg -> dolog "%s close failed: %s" what msg)
286 let pid =
287 try popen cmd [r, 0; w, -1]
288 with exn ->
289 dolog "can not execute %S: %s" cmd (exntos exn);
292 if pid > 0
293 then (
294 copysel w opaque;
295 G.postRedisplay "pipesel";
297 else doclose "pipesel pipe/w" w;
298 doclose "pipesel pipe/r" r;
301 let paxunder x y =
302 let g opaque l px py =
303 if markunder opaque px py conf.paxmark
304 then (
305 Some (fun () ->
306 match getopaque l.pageno with
307 | None -> ()
308 | Some opaque -> pipesel opaque conf.paxcmd
311 else None
313 G.postRedisplay "paxunder";
314 if conf.paxmark = Mark_page
315 then
316 List.iter (fun l ->
317 match getopaque l.pageno with
318 | None -> ()
319 | Some opaque -> clearmark opaque) state.layout;
320 state.roam <-
321 onppundermouse g x y (fun () -> showtext '!' "Whoopsie daisy");
324 let selstring s =
325 match Unix.pipe () with
326 | exception exn ->
327 showtext '!' (Printf.sprintf "pipe failed: %s" (exntos exn))
328 | (r, w) ->
329 let clo cap fd =
330 Ne.clo fd (fun msg ->
331 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
334 let pid =
335 try popen conf.selcmd [r, 0; w, -1]
336 with exn ->
337 showtext '!'
338 (Printf.sprintf "failed to execute %s: %s"
339 conf.selcmd (exntos exn));
342 if pid > 0
343 then (
345 let l = String.length s in
346 let bytes = Bytes.unsafe_of_string s in
347 let n = tempfailureretry (Unix.write w bytes 0) l in
348 if n != l
349 then
350 showtext '!'
351 (Printf.sprintf
352 "failed to write %d characters to sel pipe, wrote %d"
355 with exn ->
356 showtext '!'
357 (Printf.sprintf "failed to write to sel pipe: %s"
358 (exntos exn)
361 else dolog "%s" s;
362 clo "selstring pipe/r" r;
363 clo "selstring pipe/w" w;
366 let undertext ?(nopath=false) = function
367 | Unone -> "none"
368 | Ulinkuri s -> s
369 | Ulinkgoto (pageno, _) ->
370 if nopath
371 then "page " ^ string_of_int (pageno+1)
372 else Printf.sprintf "%s: page %d" state.path (pageno+1)
373 | Utext s -> "font: " ^ s
374 | Uunexpected s -> "unexpected: " ^ s
375 | Ulaunch s -> "launch: " ^ s
376 | Unamed s -> "named: " ^ s
377 | Uremote (filename, pageno) ->
378 Printf.sprintf "%s: page %d" filename (pageno+1)
379 | Uremotedest (filename, destname) ->
380 Printf.sprintf "%s: destination %S" filename destname
381 | Uannotation (opaque, slinkindex) ->
382 "annotation: " ^ getannotcontents opaque slinkindex
385 let updateunder x y =
386 match getunder x y with
387 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
388 | Ulinkuri uri ->
389 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
390 Wsi.setcursor Wsi.CURSOR_INFO
391 | Ulinkgoto (pageno, _) ->
392 if conf.underinfo
393 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
394 Wsi.setcursor Wsi.CURSOR_INFO
395 | Utext s ->
396 if conf.underinfo then showtext 'f' ("ont: " ^ s);
397 Wsi.setcursor Wsi.CURSOR_TEXT
398 | Uunexpected s ->
399 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
400 Wsi.setcursor Wsi.CURSOR_INHERIT
401 | Ulaunch s ->
402 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
403 Wsi.setcursor Wsi.CURSOR_INHERIT
404 | Unamed s ->
405 if conf.underinfo then showtext 'n' ("amed: " ^ s);
406 Wsi.setcursor Wsi.CURSOR_INHERIT
407 | Uremote (filename, pageno) ->
408 if conf.underinfo then showtext 'r'
409 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
410 Wsi.setcursor Wsi.CURSOR_INFO
411 | Uremotedest (filename, destname) ->
412 if conf.underinfo then showtext 'r'
413 (Printf.sprintf "emote destination: %s (%S)" filename destname);
414 Wsi.setcursor Wsi.CURSOR_INFO
415 | Uannotation _ ->
416 if conf.underinfo then showtext 'a' "nnotation";
417 Wsi.setcursor Wsi.CURSOR_INFO
420 let showlinktype under =
421 if conf.underinfo && under != Unone
422 then showtext ' ' @@ undertext under
425 let intentry_with_suffix text key =
426 let c =
427 if key >= 32 && key < 127
428 then Char.chr key
429 else '\000'
431 match Char.lowercase c with
432 | '0' .. '9' ->
433 let text = addchar text c in
434 TEcont text
436 | 'k' | 'm' | 'g' ->
437 let text = addchar text c in
438 TEcont text
440 | _ ->
441 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
442 TEcont text
445 let readcmd fd =
446 let s = Bytes.create 4 in
447 let n = tempfailureretry (Unix.read fd s 0) 4 in
448 if n != 4 then error "incomplete read(len) = %d" n;
449 let len = (Char.code (Bytes.get s 0) lsl 24)
450 lor (Char.code (Bytes.get s 1) lsl 16)
451 lor (Char.code (Bytes.get s 2) lsl 8)
452 lor (Char.code (Bytes.get s 3))
454 let s = Bytes.create len in
455 let n = tempfailureretry (Unix.read fd s 0) len in
456 if n != len then error "incomplete read(data) %d vs %d" n len;
457 Bytes.to_string s
460 let wcmd fmt =
461 let b = Buffer.create 16 in
462 Buffer.add_string b "llll";
463 Printf.kbprintf
464 (fun b ->
465 let s = Buffer.to_bytes b in
466 let n = Bytes.length s in
467 let len = n - 4 in
468 (* dolog "wcmd %S" (String.sub s 4 len); *)
469 Bytes.set s 0 (Char.chr ((len lsr 24) land 0xff));
470 Bytes.set s 1 (Char.chr ((len lsr 16) land 0xff));
471 Bytes.set s 2 (Char.chr ((len lsr 8) land 0xff));
472 Bytes.set s 3 (Char.chr (len land 0xff));
473 let n' = tempfailureretry (Unix.write state.ss s 0) n in
474 if n' != n then error "write failed %d vs %d" n' n;
475 ) b fmt;
478 let nogeomcmds cmds =
479 match cmds with
480 | s, [] -> emptystr s
481 | _ -> false
484 let layoutN ((columns, coverA, coverB), b) y sh =
485 let sh = sh - (hscrollh ()) in
486 let wadj = wadjsb () in
487 let rec fold accu n =
488 if n = Array.length b
489 then accu
490 else
491 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
492 if (vy - y) > sh &&
493 (n = coverA - 1
494 || n = state.pagecount - coverB
495 || (n - coverA) mod columns = columns - 1)
496 then accu
497 else
498 let accu =
499 if vy + h > y
500 then
501 let pagey = max 0 (y - vy) in
502 let pagedispy = if pagey > 0 then 0 else vy - y in
503 let pagedispx, pagex =
504 let pdx =
505 if n = coverA - 1 || n = state.pagecount - coverB
506 then state.x + (wadj + state.winw - w) / 2
507 else dx + xoff + state.x
509 if pdx < 0
510 then 0, -pdx
511 else pdx, 0
513 let pagevw =
514 let vw = wadj + state.winw - pagedispx in
515 let pw = w - pagex in
516 min vw pw
518 let pagevh = min (h - pagey) (sh - pagedispy) in
519 if pagevw > 0 && pagevh > 0
520 then
521 let e =
522 { pageno = n
523 ; pagedimno = pdimno
524 ; pagew = w
525 ; pageh = h
526 ; pagex = pagex
527 ; pagey = pagey
528 ; pagevw = pagevw
529 ; pagevh = pagevh
530 ; pagedispx = pagedispx
531 ; pagedispy = pagedispy
532 ; pagecol = 0
535 e :: accu
536 else
537 accu
538 else
539 accu
541 fold accu (n+1)
543 if Array.length b = 0
544 then []
545 else List.rev (fold [] (page_of_y y))
548 let layoutS (columns, b) y sh =
549 let sh = sh - hscrollh () in
550 let wadj = wadjsb () in
551 let rec fold accu n =
552 if n = Array.length b
553 then accu
554 else
555 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
556 if (vy - y) > sh
557 then accu
558 else
559 let accu =
560 if vy + pageh > y
561 then
562 let x = xoff + state.x in
563 let pagey = max 0 (y - vy) in
564 let pagedispy = if pagey > 0 then 0 else vy - y in
565 let pagedispx, pagex =
566 if px = 0
567 then (
568 if x < 0
569 then 0, -x
570 else x, 0
572 else (
573 let px = px - x in
574 if px < 0
575 then -px, 0
576 else 0, px
579 let pagecolw = pagew/columns in
580 let pagedispx =
581 if pagecolw < state.winw
582 then pagedispx + ((wadj + state.winw - pagecolw) / 2)
583 else pagedispx
585 let pagevw =
586 let vw = wadj + state.winw - pagedispx in
587 let pw = pagew - pagex in
588 min vw pw
590 let pagevw = min pagevw pagecolw in
591 let pagevh = min (pageh - pagey) (sh - pagedispy) in
592 if pagevw > 0 && pagevh > 0
593 then
594 let e =
595 { pageno = n/columns
596 ; pagedimno = pdimno
597 ; pagew = pagew
598 ; pageh = pageh
599 ; pagex = pagex
600 ; pagey = pagey
601 ; pagevw = pagevw
602 ; pagevh = pagevh
603 ; pagedispx = pagedispx
604 ; pagedispy = pagedispy
605 ; pagecol = n mod columns
608 e :: accu
609 else
610 accu
611 else
612 accu
614 fold accu (n+1)
616 List.rev (fold [] 0)
619 let layout y sh =
620 if nogeomcmds state.geomcmds
621 then
622 match conf.columns with
623 | Csingle b -> layoutN ((1, 0, 0), b) y sh
624 | Cmulti c -> layoutN c y sh
625 | Csplit s -> layoutS s y sh
626 else []
629 let clamp incr =
630 let y = state.y + incr in
631 let y = max 0 y in
632 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
636 let itertiles l f =
637 let tilex = l.pagex mod conf.tilew in
638 let tiley = l.pagey mod conf.tileh in
640 let col = l.pagex / conf.tilew in
641 let row = l.pagey / conf.tileh in
643 let xadj = xadjsb () in
644 let rec rowloop row y0 dispy h =
645 if h = 0
646 then ()
647 else (
648 let dh = conf.tileh - y0 in
649 let dh = min h dh in
650 let rec colloop col x0 dispx w =
651 if w = 0
652 then ()
653 else (
654 let dw = conf.tilew - x0 in
655 let dw = min w dw in
656 let dispx' = xadj + dispx in
657 f col row dispx' dispy x0 y0 dw dh;
658 colloop (col+1) 0 (dispx+dw) (w-dw)
661 colloop col tilex l.pagedispx l.pagevw;
662 rowloop (row+1) 0 (dispy+dh) (h-dh)
665 if l.pagevw > 0 && l.pagevh > 0
666 then rowloop row tiley l.pagedispy l.pagevh;
669 let gettileopaque l col row =
670 let key =
671 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
673 try Some (Hashtbl.find state.tilemap key)
674 with Not_found -> None
677 let puttileopaque l col row gen colorspace angle opaque size elapsed =
678 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
679 Hashtbl.add state.tilemap key (opaque, size, elapsed)
682 let filledrect x0 y0 x1 y1 =
683 GlArray.disable `texture_coord;
684 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
685 GlArray.vertex `two state.vraw;
686 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
687 GlArray.enable `texture_coord;
690 let linerect x0 y0 x1 y1 =
691 GlArray.disable `texture_coord;
692 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y1; x1; y0 |];
693 GlArray.vertex `two state.vraw;
694 GlArray.draw_arrays `line_loop ~first:0 ~count:4;
695 GlArray.enable `texture_coord;
698 let drawtiles l color =
699 GlDraw.color color;
700 let wadj = wadjsb () in
701 begintiles ();
702 let f col row x y tilex tiley w h =
703 match gettileopaque l col row with
704 | Some (opaque, _, t) ->
705 let params = x, y, w, h, tilex, tiley in
706 if conf.invert
707 then GlTex.env (`mode `blend);
708 drawtile params opaque;
709 if conf.invert
710 then GlTex.env (`mode `modulate);
711 if conf.debug
712 then (
713 endtiles ();
714 let s = Printf.sprintf
715 "%d[%d,%d] %f sec"
716 l.pageno col row t
718 let w = measurestr fstate.fontsize s in
719 GlDraw.color (0.0, 0.0, 0.0);
720 filledrect (float (x-2))
721 (float (y-2))
722 (float (x+2) +. w)
723 (float (y + fstate.fontsize + 2));
724 GlDraw.color (1.0, 1.0, 1.0);
725 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
726 begintiles ();
729 | None ->
730 endtiles ();
731 let w =
732 if conf.leftscroll
733 then w
734 else
735 let lw = wadj + state.winw - x in
736 min lw w
737 and h =
738 let lh = state.winh - y in
739 min lh h
741 if conf.invert
742 then GlTex.env (`mode `blend);
743 begin match state.checkerstexid with
744 | Some id ->
745 Gl.enable `texture_2d;
746 GlTex.bind_texture ~target:`texture_2d id;
747 let x0 = float x
748 and y0 = float y
749 and x1 = float (x+w)
750 and y1 = float (y+h) in
752 let tw = float w /. 16.0
753 and th = float h /. 16.0 in
754 let tx0 = float tilex /. 16.0
755 and ty0 = float tiley /. 16.0 in
756 let tx1 = tx0 +. tw
757 and ty1 = ty0 +. th in
758 Raw.sets_float state.vraw ~pos:0
759 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
760 Raw.sets_float state.traw ~pos:0
761 [| tx0; ty0; tx0; ty1; tx1; ty0; tx1; ty1 |];
762 GlArray.vertex `two state.vraw;
763 GlArray.tex_coord `two state.traw;
764 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
765 Gl.disable `texture_2d;
767 | None ->
768 GlDraw.color (1.0, 1.0, 1.0);
769 filledrect (float x) (float y) (float (x+w)) (float (y+h));
770 end;
771 if conf.invert
772 then GlTex.env (`mode `modulate);
773 if w > 128 && h > fstate.fontsize + 10
774 then (
775 let c = if conf.invert then 1.0 else 0.0 in
776 GlDraw.color (c, c, c);
777 let c, r =
778 if conf.verbose
779 then (col*conf.tilew, row*conf.tileh)
780 else col, row
782 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
784 GlDraw.color color;
785 begintiles ();
787 itertiles l f;
788 endtiles ();
791 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
793 let tilevisible1 l x y =
794 let ax0 = l.pagex
795 and ax1 = l.pagex + l.pagevw
796 and ay0 = l.pagey
797 and ay1 = l.pagey + l.pagevh in
799 let bx0 = x
800 and by0 = y in
801 let bx1 = min (bx0 + conf.tilew) l.pagew
802 and by1 = min (by0 + conf.tileh) l.pageh in
804 let rx0 = max ax0 bx0
805 and ry0 = max ay0 by0
806 and rx1 = min ax1 bx1
807 and ry1 = min ay1 by1 in
809 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
810 nonemptyintersection
813 let tilevisible layout n x y =
814 let rec findpageinlayout m = function
815 | l :: rest when l.pageno = n ->
816 tilevisible1 l x y || (
817 match conf.columns with
818 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
819 | Csplit _
820 | Csingle _
821 | Cmulti _ -> false
823 | _ :: rest -> findpageinlayout 0 rest
824 | [] -> false
826 findpageinlayout 0 layout;
829 let tileready l x y =
830 tilevisible1 l x y &&
831 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
834 let tilepage n p layout =
835 let rec loop = function
836 | l :: rest ->
837 if l.pageno = n
838 then
839 let f col row _ _ _ _ _ _ =
840 if state.currently = Idle
841 then
842 match gettileopaque l col row with
843 | Some _ -> ()
844 | None ->
845 let x = col*conf.tilew
846 and y = row*conf.tileh in
847 let w =
848 let w = l.pagew - x in
849 min w conf.tilew
851 let h =
852 let h = l.pageh - y in
853 min h conf.tileh
855 let pbo =
856 if conf.usepbo
857 then getpbo w h conf.colorspace
858 else ~< "0"
860 wcmd "tile %s %d %d %d %d %s"
861 (~> p) x y w h (~> pbo);
862 state.currently <-
863 Tiling (
864 l, p, conf.colorspace, conf.angle,
865 state.gen, col, row, conf.tilew, conf.tileh
868 itertiles l f;
869 else
870 loop rest
872 | [] -> ()
874 if nogeomcmds state.geomcmds
875 then loop layout;
878 let preloadlayout y =
879 let y = if y < state.winh then 0 else y - state.winh in
880 let h = state.winh*3 in
881 layout y h;
884 let load pages =
885 let rec loop pages =
886 if state.currently != Idle
887 then ()
888 else
889 match pages with
890 | l :: rest ->
891 begin match getopaque l.pageno with
892 | None ->
893 wcmd "page %d %d" l.pageno l.pagedimno;
894 state.currently <- Loading (l, state.gen);
895 | Some opaque ->
896 tilepage l.pageno opaque pages;
897 loop rest
898 end;
899 | _ -> ()
901 if nogeomcmds state.geomcmds
902 then loop pages
905 let preload pages =
906 load pages;
907 if conf.preload && state.currently = Idle
908 then load (preloadlayout state.y);
911 let layoutready layout =
912 let rec fold all ls =
913 all && match ls with
914 | l :: rest ->
915 let seen = ref false in
916 let allvisible = ref true in
917 let foo col row _ _ _ _ _ _ =
918 seen := true;
919 allvisible := !allvisible &&
920 begin match gettileopaque l col row with
921 | Some _ -> true
922 | None -> false
925 itertiles l foo;
926 fold (!seen && !allvisible) rest
927 | [] -> true
929 let alltilesvisible = fold true layout in
930 alltilesvisible;
933 let gotoy y =
934 let y = bound y 0 state.maxy in
935 let y, layout, proceed =
936 match conf.maxwait with
937 | Some time when state.ghyll == noghyll ->
938 begin match state.throttle with
939 | None ->
940 let layout = layout y state.winh in
941 let ready = layoutready layout in
942 if not ready
943 then (
944 load layout;
945 state.throttle <- Some (layout, y, now ());
947 else G.postRedisplay "gotoy showall (None)";
948 y, layout, ready
949 | Some (_, _, started) ->
950 let dt = now () -. started in
951 if dt > time
952 then (
953 state.throttle <- None;
954 let layout = layout y state.winh in
955 load layout;
956 G.postRedisplay "maxwait";
957 y, layout, true
959 else -1, [], false
962 | _ ->
963 let layout = layout y state.winh in
964 if not !wtmode || layoutready layout
965 then G.postRedisplay "gotoy ready";
966 y, layout, true
968 if proceed
969 then (
970 state.y <- y;
971 state.layout <- layout;
972 begin match state.mode with
973 | LinkNav ln ->
974 begin match ln with
975 | Ltexact (pageno, linkno) ->
976 let rec loop = function
977 | [] ->
978 state.mode <- LinkNav (Ltgendir 0)
979 | l :: _ when l.pageno = pageno ->
980 begin match getopaque pageno with
981 | None -> state.mode <- LinkNav (Ltnotready (pageno, 0))
982 | Some opaque ->
983 let x0, y0, x1, y1 = getlinkrect opaque linkno in
984 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
985 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
986 then state.mode <- LinkNav (Ltgendir 0)
988 | _ :: rest -> loop rest
990 loop layout
991 | Ltnotready _ | Ltgendir _ -> ()
993 | Birdseye _
994 | Textentry _
995 | View -> ()
996 end;
997 begin match state.mode with
998 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
999 if not (pagevisible layout pageno)
1000 then (
1001 match state.layout with
1002 | [] -> ()
1003 | l :: _ ->
1004 state.mode <- Birdseye (
1005 conf, leftx, l.pageno, hooverpageno, anchor
1008 | LinkNav lt ->
1009 begin match lt with
1010 | Ltnotready (_, dir)
1011 | Ltgendir dir ->
1012 let linknav =
1013 let rec loop = function
1014 | [] -> lt
1015 | l :: rest ->
1016 match getopaque l.pageno with
1017 | None -> Ltnotready (l.pageno, dir)
1018 | Some opaque ->
1019 let link =
1020 let ld =
1021 if dir = 0
1022 then LDfirstvisible (l.pagex, l.pagey, dir)
1023 else (
1024 if dir > 0 then LDfirst else LDlast
1027 findlink opaque ld
1029 match link with
1030 | Lnotfound -> loop rest
1031 | Lfound n ->
1032 showlinktype (getlink opaque n);
1033 Ltexact (l.pageno, n)
1035 loop state.layout
1037 state.mode <- LinkNav linknav
1038 | Ltexact _ -> ()
1040 | Textentry _
1041 | View -> ()
1042 end;
1043 preload layout;
1045 state.ghyll <- noghyll;
1046 if conf.updatecurs
1047 then (
1048 let mx, my = state.mpos in
1049 updateunder mx my;
1053 let conttiling pageno opaque =
1054 tilepage pageno opaque
1055 (if conf.preload then preloadlayout state.y else state.layout)
1058 let gotoy_and_clear_text y =
1059 if not conf.verbose then state.text <- E.s;
1060 gotoy y;
1063 let getanchory (n, top, dtop) =
1064 let y, h = getpageyh n in
1065 if conf.presentation
1066 then
1067 let ips = calcips h in
1068 y + truncate (top*.float h -. dtop*.float ips) + ips;
1069 else
1070 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1073 let gotoanchor anchor =
1074 gotoy (getanchory anchor);
1077 let addnav () =
1078 cbput state.hists.nav (getanchor ());
1081 let getnav dir =
1082 let anchor = cbgetc state.hists.nav dir in
1083 getanchory anchor;
1086 let gotoghyll1 single y =
1087 let scroll f n a b =
1088 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1089 let snake f a b =
1090 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1091 if f < a
1092 then s (float f /. float a)
1093 else (
1094 if f > b
1095 then 1.0 -. s ((float (f-b) /. float (n-b)))
1096 else 1.0
1099 snake f a b
1100 and summa n a b =
1101 let ins = float a *. 0.5
1102 and outs = float (n-b) *. 0.5 in
1103 let ones = b - a in
1104 ins +. outs +. float ones
1106 let rec set nab y sy =
1107 let (_N, _A, _B), y =
1108 if single
1109 then
1110 let scl = if y > sy then 2 else -2 in
1111 let _N, _, _ = nab in
1112 (_N,0,_N), y+conf.scrollstep*scl
1113 else nab,y in
1114 let sum = summa _N _A _B in
1115 let dy = float (y - sy) in
1116 state.ghyll <- (
1117 let rec gf n y1 o =
1118 if n >= _N
1119 then state.ghyll <- noghyll
1120 else
1121 let go n =
1122 let s = scroll n _N _A _B in
1123 let y1 = y1 +. ((s *. dy) /. sum) in
1124 gotoy_and_clear_text (truncate y1);
1125 state.ghyll <- gf (n+1) y1;
1127 match o with
1128 | None -> go n
1129 | Some y' when single -> set nab y' state.y
1130 | Some y' -> set (_N/2, 1, 1) y' state.y
1132 gf 0 (float state.y)
1135 match conf.ghyllscroll with
1136 | Some nab when not conf.presentation ->
1137 if state.ghyll == noghyll
1138 then set nab y state.y
1139 else state.ghyll (Some y)
1140 | _ ->
1141 gotoy_and_clear_text y
1144 let gotoghyll = gotoghyll1 false;;
1146 let gotopage n top =
1147 let y, h = getpageyh n in
1148 let y = y + (truncate (top *. float h)) in
1149 gotoghyll y
1152 let gotopage1 n top =
1153 let y = getpagey n in
1154 let y = y + top in
1155 gotoghyll y
1158 let invalidate s f =
1159 state.layout <- [];
1160 state.pdims <- [];
1161 state.rects <- [];
1162 state.rects1 <- [];
1163 match state.geomcmds with
1164 | ps, [] when emptystr ps ->
1165 f ();
1166 state.geomcmds <- s, [];
1168 | ps, [] ->
1169 state.geomcmds <- ps, [s, f];
1171 | ps, (s', _) :: rest when s' = s ->
1172 state.geomcmds <- ps, ((s, f) :: rest);
1174 | ps, cmds ->
1175 state.geomcmds <- ps, ((s, f) :: cmds);
1178 let flushpages () =
1179 Hashtbl.iter (fun _ opaque ->
1180 wcmd "freepage %s" (~> opaque);
1181 ) state.pagemap;
1182 Hashtbl.clear state.pagemap;
1185 let flushtiles () =
1186 if not (Queue.is_empty state.tilelru)
1187 then (
1188 Queue.iter (fun (k, p, s) ->
1189 wcmd "freetile %s" (~> p);
1190 state.memused <- state.memused - s;
1191 Hashtbl.remove state.tilemap k;
1192 ) state.tilelru;
1193 state.uioh#infochanged Memused;
1194 Queue.clear state.tilelru;
1196 load state.layout;
1199 let stateh h =
1200 let h = truncate (float h*.conf.zoom) in
1201 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
1202 h - d
1205 let opendoc path password =
1206 state.path <- path;
1207 state.password <- password;
1208 state.gen <- state.gen + 1;
1209 state.docinfo <- [];
1210 state.outlines <- [||];
1212 flushpages ();
1213 setaalevel conf.aalevel;
1214 let titlepath =
1215 if emptystr state.origin
1216 then path
1217 else state.origin
1219 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
1220 wcmd "open %d %d %s\000%s\000" (btod !wtmode) (btod !cxack) path password;
1221 invalidate "reqlayout"
1222 (fun () ->
1223 wcmd "reqlayout %d %d %d %s\000"
1224 conf.angle (FMTE.to_int conf.fitmodel)
1225 (stateh state.winh) state.nameddest
1229 let reload () =
1230 state.anchor <- getanchor ();
1231 opendoc state.path state.password;
1234 let scalecolor c =
1235 let c = c *. conf.colorscale in
1236 (c, c, c);
1239 let scalecolor2 (r, g, b) =
1240 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1243 let docolumns columns =
1244 let wadj = wadjsb () in
1245 match columns with
1246 | Csingle _ ->
1247 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1248 let wadj = wadjsb () in
1249 let rec loop pageno pdimno pdim y ph pdims =
1250 if pageno = state.pagecount
1251 then ()
1252 else
1253 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1254 match pdims with
1255 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1256 pdimno+1, pdim, rest
1257 | _ ->
1258 pdimno, pdim, pdims
1260 let x = max 0 (((wadj + state.winw - w) / 2) - xoff) in
1261 let y = y +
1262 (if conf.presentation
1263 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1264 else (if pageno = 0 then 0 else conf.interpagespace)
1267 a.(pageno) <- (pdimno, x, y, pdim);
1268 loop (pageno+1) pdimno pdim (y + h) h pdims
1270 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1271 conf.columns <- Csingle a;
1273 | Cmulti ((columns, coverA, coverB), _) ->
1274 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1275 let rec loop pageno pdimno pdim x y rowh pdims =
1276 let rec fixrow m = if m = pageno then () else
1277 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1278 if h < rowh
1279 then (
1280 let y = y + (rowh - h) / 2 in
1281 a.(m) <- (pdimno, x, y, pdim);
1283 fixrow (m+1)
1285 if pageno = state.pagecount
1286 then fixrow (((pageno - 1) / columns) * columns)
1287 else
1288 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1289 match pdims with
1290 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1291 pdimno+1, pdim, rest
1292 | _ ->
1293 pdimno, pdim, pdims
1295 let x, y, rowh' =
1296 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1297 then (
1298 let x = (wadj + state.winw - w) / 2 in
1299 let ips =
1300 if conf.presentation then calcips h else conf.interpagespace in
1301 x, y + ips + rowh, h
1303 else (
1304 if (pageno - coverA) mod columns = 0
1305 then (
1306 let x = max 0 (wadj + state.winw - state.w) / 2 in
1307 let y =
1308 if conf.presentation
1309 then
1310 let ips = calcips h in
1311 y + (if pageno = 0 then 0 else calcips rowh + ips)
1312 else
1313 y + (if pageno = 0 then 0 else conf.interpagespace)
1315 x, y + rowh, h
1317 else x, y, max rowh h
1320 let y =
1321 if pageno > 1 && (pageno - coverA) mod columns = 0
1322 then (
1323 let y =
1324 if pageno = columns && conf.presentation
1325 then (
1326 let ips = calcips rowh in
1327 for i = 0 to pred columns
1329 let (pdimno, x, y, pdim) = a.(i) in
1330 a.(i) <- (pdimno, x, y+ips, pdim)
1331 done;
1332 y+ips;
1334 else y
1336 fixrow (pageno - columns);
1339 else y
1341 a.(pageno) <- (pdimno, x, y, pdim);
1342 let x = x + w + xoff*2 + conf.interpagespace in
1343 loop (pageno+1) pdimno pdim x y rowh' pdims
1345 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1346 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1348 | Csplit (c, _) ->
1349 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1350 let rec loop pageno pdimno pdim y pdims =
1351 if pageno = state.pagecount
1352 then ()
1353 else
1354 let pdimno, ((_, w, h, _) as pdim), pdims =
1355 match pdims with
1356 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1357 pdimno+1, pdim, rest
1358 | _ ->
1359 pdimno, pdim, pdims
1361 let cw = w / c in
1362 let rec loop1 n x y =
1363 if n = c then y else (
1364 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1365 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1368 let y = loop1 0 0 y in
1369 loop (pageno+1) pdimno pdim y pdims
1371 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1372 conf.columns <- Csplit (c, a);
1375 let represent () =
1376 docolumns conf.columns;
1377 state.maxy <- calcheight ();
1378 if state.reprf == noreprf
1379 then (
1380 match state.mode with
1381 | Birdseye (_, _, pageno, _, _) ->
1382 let y, h = getpageyh pageno in
1383 let top = (state.winh - h) / 2 in
1384 gotoy (max 0 (y - top))
1385 | Textentry _
1386 | View
1387 | LinkNav _ -> gotoanchor state.anchor
1389 else (
1390 state.reprf ();
1391 state.reprf <- noreprf;
1395 let reshape ?(firsttime=false) w h =
1396 GlDraw.viewport ~x:0 ~y:0 ~w:w ~h:h;
1397 if not firsttime && nogeomcmds state.geomcmds
1398 then state.anchor <- getanchor ();
1400 state.winw <- w;
1401 let w = wadjsb () + (truncate (float w *. conf.zoom)) in
1402 let w = max w 2 in
1403 state.winh <- h;
1404 setfontsize fstate.fontsize;
1405 GlMat.mode `modelview;
1406 GlMat.load_identity ();
1408 GlMat.mode `projection;
1409 GlMat.load_identity ();
1410 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1411 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1412 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
1414 let relx =
1415 if conf.zoom <= 1.0
1416 then 0.0
1417 else float state.x /. float state.w
1419 invalidate "geometry"
1420 (fun () ->
1421 state.w <- w;
1422 if not firsttime
1423 then state.x <- truncate (relx *. float w);
1424 let w =
1425 match conf.columns with
1426 | Csingle _ -> w
1427 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1428 | Csplit (c, _) -> w * c
1430 wcmd "geometry %d %d %d"
1431 w (stateh h) (FMTE.to_int conf.fitmodel)
1435 let enttext () =
1436 let len = String.length state.text in
1437 let x0 = xadjsb () in
1438 let drawstring s =
1439 let hscrollh =
1440 match state.mode with
1441 | Textentry _ | View | LinkNav _ ->
1442 let h, _, _ = state.uioh#scrollpw in
1444 | Birdseye _ -> 0
1446 let rect x w =
1447 filledrect x (float (state.winh - (fstate.fontsize + 4) - hscrollh))
1448 (x+.w) (float (state.winh - hscrollh))
1451 let w = float (wadjsb () + state.winw - 1) in
1452 if state.progress >= 0.0 && state.progress < 1.0
1453 then (
1454 GlDraw.color (0.3, 0.3, 0.3);
1455 let w1 = w *. state.progress in
1456 rect (float x0) w1;
1457 GlDraw.color (0.0, 0.0, 0.0);
1458 rect (float x0+.w1) (float x0+.w-.w1)
1460 else (
1461 GlDraw.color (0.0, 0.0, 0.0);
1462 rect (float x0) w;
1465 GlDraw.color (1.0, 1.0, 1.0);
1466 drawstring fstate.fontsize
1467 (if conf.leftscroll then x0 + 2 else x0 + if len > 0 then 8 else 2)
1468 (state.winh - hscrollh - 5) s;
1470 let s =
1471 match state.mode with
1472 | Textentry ((prefix, text, _, _, _, _), _) ->
1473 let s =
1474 if len > 0
1475 then
1476 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1477 else
1478 Printf.sprintf "%s%s_" prefix text
1482 | Birdseye _
1483 | View
1484 | LinkNav _ -> state.text
1486 let s =
1487 if state.newerrmsgs
1488 then (
1489 if not (istextentry state.mode) && state.uioh#eformsgs
1490 then
1491 let s1 = "(press 'e' to review error messasges)" in
1492 if nonemptystr s then s ^ " " ^ s1 else s1
1493 else s
1495 else s
1497 if nonemptystr s
1498 then drawstring s
1501 let gctiles () =
1502 let len = Queue.length state.tilelru in
1503 let layout = lazy (
1504 match state.throttle with
1505 | None ->
1506 if conf.preload
1507 then preloadlayout state.y
1508 else state.layout
1509 | Some (layout, _, _) ->
1510 layout
1511 ) in
1512 let rec loop qpos =
1513 if state.memused <= conf.memlimit
1514 then ()
1515 else (
1516 if qpos < len
1517 then
1518 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1519 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1520 let (_, pw, ph, _) = getpagedim n in
1522 gen = state.gen
1523 && colorspace = conf.colorspace
1524 && angle = conf.angle
1525 && pagew = pw
1526 && pageh = ph
1527 && (
1528 let x = col*conf.tilew
1529 and y = row*conf.tileh in
1530 tilevisible (Lazy.force_val layout) n x y
1532 then Queue.push lruitem state.tilelru
1533 else (
1534 freepbo p;
1535 wcmd "freetile %s" (~> p);
1536 state.memused <- state.memused - s;
1537 state.uioh#infochanged Memused;
1538 Hashtbl.remove state.tilemap k;
1540 loop (qpos+1)
1543 loop 0
1546 let onpagerect pageno f =
1547 let b =
1548 match conf.columns with
1549 | Cmulti (_, b) -> b
1550 | Csingle b -> b
1551 | Csplit (_, b) -> b
1553 if pageno >= 0 && pageno < Array.length b
1554 then
1555 let (_, _, _, (_, w, h, _)) = b.(pageno) in
1556 f w h
1559 let gotopagexy1 pageno x y =
1560 let _,w1,h1,leftx = getpagedim pageno in
1561 let top = y /. (float h1) in
1562 let left = x /. (float w1) in
1563 let py, w, h = getpageywh pageno in
1564 let wh = state.winh - hscrollh () in
1565 let x = left *. (float w) in
1566 let x = leftx + state.x + truncate x in
1567 let wadj = wadjsb () in
1568 let sx =
1569 if x < 0 || x >= wadj + state.winw
1570 then state.x - x
1571 else state.x
1573 let pdy = truncate (top *. float h) in
1574 let y' = py + pdy in
1575 let dy = y' - state.y in
1576 let sy =
1577 if x != state.x || not (dy > 0 && dy < wh)
1578 then (
1579 if conf.presentation
1580 then
1581 if abs (py - y') > wh
1582 then y'
1583 else py
1584 else y';
1586 else state.y
1588 if state.x != sx || state.y != sy
1589 then (
1590 let x, y =
1591 if !wtmode
1592 then (
1593 let ww = wadj + state.winw in
1594 let qx = sx / ww
1595 and qy = pdy / wh in
1596 let x = qx * ww
1597 and y = py + qy * wh in
1598 let x = if -x + ww > w1 then -(w1-ww) else x
1599 and y' = if y + wh > state.maxy then state.maxy - wh else y in
1600 let y =
1601 if conf.presentation
1602 then
1603 if abs (py - y') > wh
1604 then y'
1605 else py
1606 else y';
1608 (x, y)
1610 else (sx, sy)
1612 state.x <- x;
1613 gotoy_and_clear_text y;
1615 else gotoy_and_clear_text state.y;
1618 let gotopagexy pageno x y =
1619 match state.mode with
1620 | Birdseye _ -> gotopage pageno 0.0
1621 | Textentry _
1622 | View
1623 | LinkNav _ -> gotopagexy1 pageno x y
1626 let getpassword () =
1627 let passcmd = getenvwithdef "LLPP_ASKPASS" conf.passcmd in
1628 if emptystr passcmd
1629 then E.s
1630 else getcmdoutput
1631 (fun s ->
1632 showtext '!' @@ "error getting password: " ^ s;
1633 dolog "%s" s) passcmd;
1636 let act cmds =
1637 (* dolog "%S" cmds; *)
1638 let cl = splitatspace cmds in
1639 let scan s fmt f =
1640 try Scanf.sscanf s fmt f
1641 with exn ->
1642 dolog "error processing '%S': %s" cmds (exntos exn);
1643 exit 1
1645 let addoutline outline =
1646 match state.currently with
1647 | Outlining outlines ->
1648 state.currently <- Outlining (outline :: outlines)
1649 | Idle -> state.currently <- Outlining [outline]
1650 | Loading _
1651 | Tiling _ ->
1652 dolog "invalid outlining state";
1653 logcurrently state.currently
1655 match cl with
1656 | "clear" :: [] ->
1657 state.uioh#infochanged Pdim;
1658 state.pdims <- [];
1660 | "clearrects" :: [] ->
1661 state.rects <- state.rects1;
1662 G.postRedisplay "clearrects";
1664 | "continue" :: args :: [] ->
1665 let n = scan args "%u" (fun n -> n) in
1666 state.pagecount <- n;
1667 begin match state.currently with
1668 | Outlining l ->
1669 state.currently <- Idle;
1670 state.outlines <- Array.of_list (List.rev l)
1671 | Idle
1672 | Loading _
1673 | Tiling _ -> ()
1674 end;
1676 let cur, cmds = state.geomcmds in
1677 if emptystr cur
1678 then failwith "umpossible";
1680 begin match List.rev cmds with
1681 | [] ->
1682 state.geomcmds <- E.s, [];
1683 state.throttle <- None;
1684 represent ();
1685 | (s, f) :: rest ->
1686 f ();
1687 state.geomcmds <- s, List.rev rest;
1688 end;
1689 if conf.maxwait = None && not !wtmode
1690 then G.postRedisplay "continue";
1692 | "msg" :: args :: [] ->
1693 showtext ' ' args
1695 | "vmsg" :: args :: [] ->
1696 if conf.verbose
1697 then showtext ' ' args
1699 | "emsg" :: args :: [] ->
1700 Buffer.add_string state.errmsgs args;
1701 state.newerrmsgs <- true;
1702 G.postRedisplay "error message"
1704 | "progress" :: args :: [] ->
1705 let progress, text =
1706 scan args "%f %n"
1707 (fun f pos ->
1708 f, String.sub args pos (String.length args - pos))
1710 state.text <- text;
1711 state.progress <- progress;
1712 G.postRedisplay "progress"
1714 | "firstmatch" :: args :: [] ->
1715 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1716 scan args "%u %d %f %f %f %f %f %f %f %f"
1717 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1718 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1720 let xoff = float (xadjsb ()) in
1721 let x0 = x0 +. xoff
1722 and x1 = x1 +. xoff
1723 and x2 = x2 +. xoff
1724 and x3 = x3 +. xoff in
1725 let y = (getpagey pageno) + truncate y0 in
1726 addnav ();
1727 gotoy y;
1728 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
1730 | "match" :: args :: [] ->
1731 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1732 scan args "%u %d %f %f %f %f %f %f %f %f"
1733 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1734 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1736 let xoff = float (xadjsb ()) in
1737 let x0 = x0 +. xoff
1738 and x1 = x1 +. xoff
1739 and x2 = x2 +. xoff
1740 and x3 = x3 +. xoff in
1741 state.rects1 <-
1742 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1744 | "page" :: args :: [] ->
1745 let pageopaques, t = scan args "%s %f" (fun p t -> p, t) in
1746 let pageopaque = ~< pageopaques in
1747 begin match state.currently with
1748 | Loading (l, gen) ->
1749 vlog "page %d took %f sec" l.pageno t;
1750 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1751 begin match state.throttle with
1752 | None ->
1753 let preloadedpages =
1754 if conf.preload
1755 then preloadlayout state.y
1756 else state.layout
1758 let evict () =
1759 let set =
1760 List.fold_left (fun s l -> IntSet.add l.pageno s)
1761 IntSet.empty preloadedpages
1763 let evictedpages =
1764 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1765 if not (IntSet.mem pageno set)
1766 then (
1767 wcmd "freepage %s" (~> opaque);
1768 key :: accu
1770 else accu
1771 ) state.pagemap []
1773 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1775 evict ();
1776 state.currently <- Idle;
1777 if gen = state.gen
1778 then (
1779 tilepage l.pageno pageopaque state.layout;
1780 load state.layout;
1781 load preloadedpages;
1782 let visible = pagevisible state.layout l.pageno in
1783 if visible
1784 then (
1785 match state.mode with
1786 | LinkNav (Ltnotready (pageno, dir)) ->
1787 if pageno = l.pageno
1788 then (
1789 let link =
1790 let ld =
1791 if dir = 0
1792 then LDfirstvisible (l.pagex, l.pagey, dir)
1793 else (
1794 if dir > 0 then LDfirst else LDlast
1797 findlink pageopaque ld
1799 match link with
1800 | Lnotfound -> ()
1801 | Lfound n ->
1802 showlinktype (getlink pageopaque n);
1803 state.mode <- LinkNav (Ltexact (l.pageno, n))
1805 | LinkNav (Ltgendir _)
1806 | LinkNav (Ltexact _)
1807 | View
1808 | Birdseye _
1809 | Textentry _ -> ()
1812 if visible && layoutready state.layout
1813 then (
1814 G.postRedisplay "page";
1818 | Some (layout, _, _) ->
1819 state.currently <- Idle;
1820 tilepage l.pageno pageopaque layout;
1821 load state.layout
1822 end;
1824 | Idle
1825 | Tiling _
1826 | Outlining _ ->
1827 dolog "Inconsistent loading state";
1828 logcurrently state.currently;
1829 exit 1
1832 | "tile" :: args :: [] ->
1833 let (x, y, opaques, size, t) =
1834 scan args "%u %u %s %u %f"
1835 (fun x y p size t -> (x, y, p, size, t))
1837 let opaque = ~< opaques in
1838 begin match state.currently with
1839 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1840 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1842 unmappbo opaque;
1843 if tilew != conf.tilew || tileh != conf.tileh
1844 then (
1845 wcmd "freetile %s" (~> opaque);
1846 state.currently <- Idle;
1847 load state.layout;
1849 else (
1850 puttileopaque l col row gen cs angle opaque size t;
1851 state.memused <- state.memused + size;
1852 state.uioh#infochanged Memused;
1853 gctiles ();
1854 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1855 opaque, size) state.tilelru;
1857 let layout =
1858 match state.throttle with
1859 | None -> state.layout
1860 | Some (layout, _, _) -> layout
1863 state.currently <- Idle;
1864 if gen = state.gen
1865 && conf.colorspace = cs
1866 && conf.angle = angle
1867 && tilevisible layout l.pageno x y
1868 then conttiling l.pageno pageopaque;
1870 begin match state.throttle with
1871 | None ->
1872 preload state.layout;
1873 if gen = state.gen
1874 && conf.colorspace = cs
1875 && conf.angle = angle
1876 && tilevisible state.layout l.pageno x y
1877 && (not !wtmode || layoutready state.layout)
1878 then G.postRedisplay "tile nothrottle";
1880 | Some (layout, y, _) ->
1881 let ready = layoutready layout in
1882 if ready
1883 then (
1884 state.y <- y;
1885 state.layout <- layout;
1886 state.throttle <- None;
1887 G.postRedisplay "throttle";
1889 else load layout;
1890 end;
1893 | Idle
1894 | Loading _
1895 | Outlining _ ->
1896 dolog "Inconsistent tiling state";
1897 logcurrently state.currently;
1898 exit 1
1901 | "pdim" :: args :: [] ->
1902 let (n, w, h, _) as pdim =
1903 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1905 let pdim =
1906 match conf.fitmodel with
1907 | FitWidth -> pdim
1908 | FitPage | FitProportional ->
1909 match conf.columns with
1910 | Csplit _ -> (n, w, h, 0)
1911 | Csingle _ | Cmulti _ -> pdim
1913 state.uioh#infochanged Pdim;
1914 state.pdims <- pdim :: state.pdims
1916 | "o" :: args :: [] ->
1917 let (l, n, t, h, pos) =
1918 scan args "%u %u %d %u %n"
1919 (fun l n t h pos -> l, n, t, h, pos)
1921 let s = String.sub args pos (String.length args - pos) in
1922 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
1924 | "ou" :: args :: [] ->
1925 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
1926 let s = String.sub args pos len in
1927 let pos2 = pos + len + 1 in
1928 let uri = String.sub args pos2 (String.length args - pos2) in
1929 addoutline (s, l, Ouri uri)
1931 | "on" :: args :: [] ->
1932 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
1933 let s = String.sub args pos (String.length args - pos) in
1934 addoutline (s, l, Onone)
1936 | "a" :: args :: [] ->
1937 let (n, l, t) =
1938 scan args "%u %d %d" (fun n l t -> n, l, t)
1940 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
1942 | "info" :: args :: [] ->
1943 let pos = nindex args '\t' in
1944 if pos >= 0 && String.sub args 0 pos = "Title"
1945 then (
1946 let s = String.sub args (pos+1) @@ String.length args - pos - 1 in
1947 conf.title <- s;
1948 Wsi.settitle s;
1950 state.docinfo <- (1, args) :: state.docinfo
1952 | "infoend" :: [] ->
1953 state.uioh#infochanged Docinfo;
1954 state.docinfo <- List.rev state.docinfo
1956 | "pass" :: l ->
1957 if l = "fail" :: []
1958 then Wsi.settitle "Wrong password";
1959 let password = getpassword () in
1960 if password == E.s
1961 then error "document is password protected"
1962 else opendoc state.path password
1964 | _ ->
1965 error "unknown cmd `%S'" cmds
1968 let onhist cb =
1969 let rc = cb.rc in
1970 let action = function
1971 | HCprev -> cbget cb ~-1
1972 | HCnext -> cbget cb 1
1973 | HCfirst -> cbget cb ~-(cb.rc)
1974 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1975 and cancel () = cb.rc <- rc
1976 in (action, cancel)
1979 let search pattern forward =
1980 match conf.columns with
1981 | Csplit _ ->
1982 showtext '!' "searching does not work properly in split columns mode"
1983 | Csingle _
1984 | Cmulti _ ->
1985 if nonemptystr pattern
1986 then
1987 let pn, py =
1988 match state.layout with
1989 | [] -> 0, 0
1990 | l :: _ ->
1991 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1993 wcmd "search %d %d %d %d,%s\000"
1994 (btod conf.icase) pn py (btod forward) pattern;
1997 let intentry text key =
1998 let c =
1999 if key >= 32 && key < 127
2000 then Char.chr key
2001 else '\000'
2003 match c with
2004 | '0' .. '9' ->
2005 let text = addchar text c in
2006 TEcont text
2008 | _ ->
2009 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2010 TEcont text
2013 let linknact f s =
2014 if nonemptystr s
2015 then (
2016 let n =
2017 let l = String.length s in
2018 let rec loop pos n = if pos = l then n else
2019 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2020 loop (pos+1) (n*26 + m)
2021 in loop 0 0
2023 let rec loop n = function
2024 | [] -> ()
2025 | l :: rest ->
2026 match getopaque l.pageno with
2027 | None -> loop n rest
2028 | Some opaque ->
2029 let m = getlinkcount opaque in
2030 if n < m
2031 then (
2032 let under = getlink opaque n in
2033 f under
2035 else loop (n-m) rest
2037 loop n state.layout;
2041 let linknentry text key =
2042 let c =
2043 if key >= 32 && key < 127
2044 then Char.chr key
2045 else '\000'
2047 match c with
2048 | 'a' .. 'z' ->
2049 let text = addchar text c in
2050 linknact (fun under -> state.text <- undertext ~nopath:true under) text;
2051 TEcont text
2053 | _ ->
2054 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2055 TEcont text
2058 let textentry text key =
2059 if key land 0xff00 = 0xff00
2060 then TEcont text
2061 else TEcont (text ^ toutf8 key)
2064 let reqlayout angle fitmodel =
2065 match state.throttle with
2066 | None ->
2067 if nogeomcmds state.geomcmds
2068 then state.anchor <- getanchor ();
2069 conf.angle <- angle mod 360;
2070 if conf.angle != 0
2071 then (
2072 match state.mode with
2073 | LinkNav _ -> state.mode <- View
2074 | Birdseye _
2075 | Textentry _
2076 | View -> ()
2078 conf.fitmodel <- fitmodel;
2079 invalidate "reqlayout"
2080 (fun () ->
2081 wcmd "reqlayout %d %d %d"
2082 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
2084 | _ -> ()
2087 let settrim trimmargins trimfuzz =
2088 if nogeomcmds state.geomcmds
2089 then state.anchor <- getanchor ();
2090 conf.trimmargins <- trimmargins;
2091 conf.trimfuzz <- trimfuzz;
2092 let x0, y0, x1, y1 = trimfuzz in
2093 invalidate "settrim"
2094 (fun () ->
2095 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2096 flushpages ();
2099 let setzoom zoom =
2100 match state.throttle with
2101 | None ->
2102 let zoom = max 0.0001 zoom in
2103 if zoom <> conf.zoom
2104 then (
2105 state.prevzoom <- (conf.zoom, state.x);
2106 conf.zoom <- zoom;
2107 reshape state.winw state.winh;
2108 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2111 | Some (layout, y, started) ->
2112 let time =
2113 match conf.maxwait with
2114 | None -> 0.0
2115 | Some t -> t
2117 let dt = now () -. started in
2118 if dt > time
2119 then (
2120 state.y <- y;
2121 load layout;
2125 let setcolumns mode columns coverA coverB =
2126 state.prevcolumns <- Some (conf.columns, conf.zoom);
2127 if columns < 0
2128 then (
2129 if isbirdseye mode
2130 then showtext '!' "split mode doesn't work in bird's eye"
2131 else (
2132 conf.columns <- Csplit (-columns, E.a);
2133 state.x <- 0;
2134 conf.zoom <- 1.0;
2137 else (
2138 if columns < 2
2139 then (
2140 conf.columns <- Csingle E.a;
2141 state.x <- 0;
2142 setzoom 1.0;
2144 else (
2145 conf.columns <- Cmulti ((columns, coverA, coverB), E.a);
2146 conf.zoom <- 1.0;
2149 reshape state.winw state.winh;
2152 let resetmstate () =
2153 state.mstate <- Mnone;
2154 Wsi.setcursor Wsi.CURSOR_INHERIT;
2157 let enterbirdseye () =
2158 let zoom = float conf.thumbw /. float state.winw in
2159 let birdseyepageno =
2160 let cy = state.winh / 2 in
2161 let fold = function
2162 | [] -> 0
2163 | l :: rest ->
2164 let rec fold best = function
2165 | [] -> best.pageno
2166 | l :: rest ->
2167 let d = cy - (l.pagedispy + l.pagevh/2)
2168 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2169 if abs d < abs dbest
2170 then fold l rest
2171 else best.pageno
2172 in fold l rest
2174 fold state.layout
2176 state.mode <- Birdseye (
2177 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2179 resetmstate ();
2180 conf.zoom <- zoom;
2181 conf.presentation <- false;
2182 conf.interpagespace <- 10;
2183 conf.hlinks <- false;
2184 conf.fitmodel <- FitPage;
2185 state.x <- 0;
2186 conf.maxwait <- None;
2187 conf.columns <- (
2188 match conf.beyecolumns with
2189 | Some c ->
2190 conf.zoom <- 1.0;
2191 Cmulti ((c, 0, 0), E.a)
2192 | None -> Csingle E.a
2194 if conf.verbose
2195 then
2196 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2197 (100.0*.zoom)
2198 else
2199 state.text <- E.s
2201 reshape state.winw state.winh;
2204 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2205 state.mode <- View;
2206 conf.zoom <- c.zoom;
2207 conf.presentation <- c.presentation;
2208 conf.interpagespace <- c.interpagespace;
2209 conf.maxwait <- c.maxwait;
2210 conf.hlinks <- c.hlinks;
2211 conf.fitmodel <- c.fitmodel;
2212 conf.beyecolumns <- (
2213 match conf.columns with
2214 | Cmulti ((c, _, _), _) -> Some c
2215 | Csingle _ -> None
2216 | Csplit _ -> failwith "leaving bird's eye split mode"
2218 conf.columns <- (
2219 match c.columns with
2220 | Cmulti (c, _) -> Cmulti (c, E.a)
2221 | Csingle _ -> Csingle E.a
2222 | Csplit (c, _) -> Csplit (c, E.a)
2224 if conf.verbose
2225 then
2226 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2227 (100.0*.conf.zoom)
2229 reshape state.winw state.winh;
2230 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2231 state.x <- leftx;
2234 let togglebirdseye () =
2235 match state.mode with
2236 | Birdseye vals -> leavebirdseye vals true
2237 | View -> enterbirdseye ()
2238 | Textentry _
2239 | LinkNav _ -> ()
2242 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2243 let pageno = max 0 (pageno - incr) in
2244 let rec loop = function
2245 | [] -> gotopage1 pageno 0
2246 | l :: _ when l.pageno = pageno ->
2247 if l.pagedispy >= 0 && l.pagey = 0
2248 then G.postRedisplay "upbirdseye"
2249 else gotopage1 pageno 0
2250 | _ :: rest -> loop rest
2252 loop state.layout;
2253 state.text <- E.s;
2254 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2257 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2258 let pageno = min (state.pagecount - 1) (pageno + incr) in
2259 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2260 let rec loop = function
2261 | [] ->
2262 let y, h = getpageyh pageno in
2263 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2264 gotoy (clamp dy)
2265 | l :: _ when l.pageno = pageno ->
2266 if l.pagevh != l.pageh
2267 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2268 else G.postRedisplay "downbirdseye"
2269 | _ :: rest -> loop rest
2271 loop state.layout;
2272 state.text <- E.s;
2275 let optentry mode _ key =
2276 let btos b = if b then "on" else "off" in
2277 if key >= 32 && key < 127
2278 then
2279 let c = Char.chr key in
2280 match c with
2281 | 's' ->
2282 let ondone s =
2283 try conf.scrollstep <- int_of_string s with exc ->
2284 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2286 TEswitch ("scroll step: ", E.s, None, intentry, ondone, true)
2288 | 'A' ->
2289 let ondone s =
2291 conf.autoscrollstep <- boundastep state.winh (int_of_string s);
2292 if state.autoscroll <> None
2293 then state.autoscroll <- Some conf.autoscrollstep
2294 with exc ->
2295 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2297 TEswitch ("auto scroll step: ", E.s, None, intentry, ondone, true)
2299 | 'C' ->
2300 let ondone s =
2302 let n, a, b = multicolumns_of_string s in
2303 setcolumns mode n a b;
2304 with exc ->
2305 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
2307 TEswitch ("columns: ", E.s, None, textentry, ondone, true)
2309 | 'Z' ->
2310 let ondone s =
2312 let zoom = float (int_of_string s) /. 100.0 in
2313 setzoom zoom
2314 with exc ->
2315 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2317 TEswitch ("zoom: ", E.s, None, intentry, ondone, true)
2319 | 't' ->
2320 let ondone s =
2322 conf.thumbw <- bound (int_of_string s) 2 4096;
2323 state.text <-
2324 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2325 begin match mode with
2326 | Birdseye beye ->
2327 leavebirdseye beye false;
2328 enterbirdseye ();
2329 | Textentry _
2330 | View
2331 | LinkNav _ -> ();
2333 with exc ->
2334 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2336 TEswitch ("thumbnail width: ", E.s, None, intentry, ondone, true)
2338 | 'R' ->
2339 let ondone s =
2340 match try
2341 Some (int_of_string s)
2342 with exc ->
2343 state.text <- Printf.sprintf "bad integer `%s': %s"
2344 s (exntos exc);
2345 None
2346 with
2347 | Some angle -> reqlayout angle conf.fitmodel
2348 | None -> ()
2350 TEswitch ("rotation: ", E.s, None, intentry, ondone, true)
2352 | 'i' ->
2353 conf.icase <- not conf.icase;
2354 TEdone ("case insensitive search " ^ (btos conf.icase))
2356 | 'p' ->
2357 conf.preload <- not conf.preload;
2358 gotoy state.y;
2359 TEdone ("preload " ^ (btos conf.preload))
2361 | 'v' ->
2362 conf.verbose <- not conf.verbose;
2363 TEdone ("verbose " ^ (btos conf.verbose))
2365 | 'd' ->
2366 conf.debug <- not conf.debug;
2367 TEdone ("debug " ^ (btos conf.debug))
2369 | 'h' ->
2370 conf.maxhfit <- not conf.maxhfit;
2371 state.maxy <- calcheight ();
2372 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2374 | 'c' ->
2375 conf.crophack <- not conf.crophack;
2376 TEdone ("crophack " ^ btos conf.crophack)
2378 | 'a' ->
2379 let s =
2380 match conf.maxwait with
2381 | None ->
2382 conf.maxwait <- Some infinity;
2383 "always wait for page to complete"
2384 | Some _ ->
2385 conf.maxwait <- None;
2386 "show placeholder if page is not ready"
2388 TEdone s
2390 | 'f' ->
2391 conf.underinfo <- not conf.underinfo;
2392 TEdone ("underinfo " ^ btos conf.underinfo)
2394 | 'P' ->
2395 conf.savebmarks <- not conf.savebmarks;
2396 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2398 | 'S' ->
2399 let ondone s =
2401 let pageno, py =
2402 match state.layout with
2403 | [] -> 0, 0
2404 | l :: _ ->
2405 l.pageno, l.pagey
2407 conf.interpagespace <- int_of_string s;
2408 docolumns conf.columns;
2409 state.maxy <- calcheight ();
2410 let y = getpagey pageno in
2411 gotoy (y + py)
2412 with exc ->
2413 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2415 TEswitch ("vertical margin: ", E.s, None, intentry, ondone, true)
2417 | 'l' ->
2418 let fm =
2419 match conf.fitmodel with
2420 | FitProportional -> FitWidth
2421 | FitWidth | FitPage -> FitProportional
2423 reqlayout conf.angle fm;
2424 TEdone ("proportional display " ^ btos (fm == FitProportional))
2426 | 'T' ->
2427 settrim (not conf.trimmargins) conf.trimfuzz;
2428 TEdone ("trim margins " ^ btos conf.trimmargins)
2430 | 'I' ->
2431 conf.invert <- not conf.invert;
2432 TEdone ("invert colors " ^ btos conf.invert)
2434 | 'x' ->
2435 let ondone s =
2436 cbput state.hists.sel s;
2437 conf.selcmd <- s;
2439 TEswitch ("selection command: ", E.s, Some (onhist state.hists.sel),
2440 textentry, ondone, true)
2442 | 'M' ->
2443 if conf.pax == None
2444 then conf.pax <- Some (ref (0.0, 0, 0))
2445 else conf.pax <- None;
2446 TEdone ("PAX " ^ btos (conf.pax != None))
2448 | _ ->
2449 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2450 TEstop
2451 else
2452 TEcont state.text
2455 class type lvsource = object
2456 method getitemcount : int
2457 method getitem : int -> (string * int)
2458 method hasaction : int -> bool
2459 method exit :
2460 uioh:uioh ->
2461 cancel:bool ->
2462 active:int ->
2463 first:int ->
2464 pan:int ->
2465 uioh option
2466 method getactive : int
2467 method getfirst : int
2468 method getpan : int
2469 method getminfo : (int * int) array
2470 end;;
2472 class virtual lvsourcebase = object
2473 val mutable m_active = 0
2474 val mutable m_first = 0
2475 val mutable m_pan = 0
2476 method getactive = m_active
2477 method getfirst = m_first
2478 method getpan = m_pan
2479 method getminfo : (int * int) array = E.a
2480 end;;
2482 let textentrykeyboard
2483 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2484 state.text <- E.s;
2485 let key =
2486 if key >= 0xffb0 && key <= 0xffb9
2487 then key - 0xffb0 + 48 else key
2489 let enttext te =
2490 state.mode <- Textentry (te, onleave);
2491 enttext ();
2492 G.postRedisplay "textentrykeyboard enttext";
2494 let histaction cmd =
2495 match opthist with
2496 | None -> ()
2497 | Some (action, _) ->
2498 state.mode <- Textentry (
2499 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
2501 G.postRedisplay "textentry histaction"
2503 match key with
2504 | @backspace ->
2505 if emptystr text && cancelonempty
2506 then (
2507 onleave Cancel;
2508 G.postRedisplay "textentrykeyboard after cancel";
2510 else
2511 let s = withoutlastutf8 text in
2512 enttext (c, s, opthist, onkey, ondone, cancelonempty)
2514 | @enter | @kpenter ->
2515 ondone text;
2516 onleave Confirm;
2517 G.postRedisplay "textentrykeyboard after confirm"
2519 | @up | @kpup -> histaction HCprev
2520 | @down | @kpdown -> histaction HCnext
2521 | @home | @kphome -> histaction HCfirst
2522 | @jend | @kpend -> histaction HClast
2524 | @escape ->
2525 if emptystr text
2526 then (
2527 begin match opthist with
2528 | None -> ()
2529 | Some (_, onhistcancel) -> onhistcancel ()
2530 end;
2531 onleave Cancel;
2532 state.text <- E.s;
2533 G.postRedisplay "textentrykeyboard after cancel2"
2535 else (
2536 enttext (c, E.s, opthist, onkey, ondone, cancelonempty)
2539 | @delete | @kpdelete -> ()
2541 | _ when key != 0
2542 && key land 0xff00 != 0xff00 (* keyboard *)
2543 && key land 0xfe00 != 0xfe00 (* xkb *)
2544 && key land 0xfd00 != 0xfd00 (* 3270 *)
2546 begin match onkey text key with
2547 | TEdone text ->
2548 ondone text;
2549 onleave Confirm;
2550 G.postRedisplay "textentrykeyboard after confirm2";
2552 | TEcont text ->
2553 enttext (c, text, opthist, onkey, ondone, cancelonempty);
2555 | TEstop ->
2556 onleave Cancel;
2557 G.postRedisplay "textentrykeyboard after cancel3"
2559 | TEswitch te ->
2560 state.mode <- Textentry (te, onleave);
2561 G.postRedisplay "textentrykeyboard switch";
2562 end;
2564 | _ ->
2565 vlog "unhandled key %s" (Wsi.keyname key)
2568 let firstof first active =
2569 if first > active || abs (first - active) > fstate.maxrows - 1
2570 then max 0 (active - (fstate.maxrows/2))
2571 else first
2574 let calcfirst first active =
2575 if active > first
2576 then
2577 let rows = active - first in
2578 if rows > fstate.maxrows then active - fstate.maxrows else first
2579 else active
2582 let scrollph y maxy =
2583 let sh = float (maxy + state.winh) /. float state.winh in
2584 let sh = float state.winh /. sh in
2585 let sh = max sh (float conf.scrollh) in
2587 let percent = float y /. float maxy in
2588 let position = (float state.winh -. sh) *. percent in
2590 let position =
2591 if position +. sh > float state.winh
2592 then float state.winh -. sh
2593 else position
2595 position, sh;
2598 let coe s = (s :> uioh);;
2600 class listview ~zebra ~helpmode ~(source:lvsource) ~trusted ~modehash =
2601 object (self)
2602 val m_pan = source#getpan
2603 val m_first = source#getfirst
2604 val m_active = source#getactive
2605 val m_qsearch = E.s
2606 val m_prev_uioh = state.uioh
2608 method private elemunder y =
2609 if y < 0
2610 then None
2611 else
2612 let n = y / (fstate.fontsize+1) in
2613 if m_first + n < source#getitemcount
2614 then (
2615 if source#hasaction (m_first + n)
2616 then Some (m_first + n)
2617 else None
2619 else None
2621 method display =
2622 Gl.enable `blend;
2623 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
2624 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2625 filledrect 0. 0. (float state.winw) (float state.winh);
2626 GlDraw.color (1., 1., 1.);
2627 Gl.enable `texture_2d;
2628 let fs = fstate.fontsize in
2629 let nfs = fs + 1 in
2630 let hw = (wadjsb () + xadjsb () + state.winw)/3 in
2631 let ww = fstate.wwidth in
2632 let tabw = 17.0*.ww in
2633 let itemcount = source#getitemcount in
2634 let minfo = source#getminfo in
2635 let x0, x1 =
2636 if conf.leftscroll
2637 then float (xadjsb ()), float (state.winw - 1)
2638 else 0.0, float (state.winw - conf.scrollbw - 1)
2640 let xadj = xadjsb () in
2641 let rec loop row =
2642 if (row - m_first) > fstate.maxrows
2643 then ()
2644 else (
2645 if row >= 0 && row < itemcount
2646 then (
2647 let (s, level) = source#getitem row in
2648 let y = (row - m_first) * nfs in
2649 let x =
2650 (if conf.leftscroll then float xadj else 5.0)
2651 +. (float (level + m_pan)) *. ww in
2652 if helpmode
2653 then GlDraw.color
2654 (let c = if row land 1 = 0 then 1.0 else 0.92 in (c,c,c));
2656 if row = m_active
2657 then (
2658 Gl.disable `texture_2d;
2659 let alpha = if source#hasaction row then 0.9 else 0.3 in
2660 GlDraw.color (1., 1., 1.) ~alpha;
2661 linerect (x0 +. 1.) (float (y + 1)) (x1) (float (y + fs + 3));
2662 Gl.enable `texture_2d;
2664 let c =
2665 if zebra && row land 1 = 1
2666 then 0.8
2667 else 1.0
2669 GlDraw.color (c,c,c);
2670 let drawtabularstring s =
2671 let drawstr x s =
2672 let x' = truncate (x0 +. x) in
2673 let pos = nindex s '\000' in
2674 if pos = -1
2675 then drawstring1 fs x' (y+nfs) s
2676 else
2677 let s1 = String.sub s 0 pos
2678 and s2 = String.sub s (pos+1) (String.length s - pos - 1) in
2679 let rec e s =
2680 if emptystr s
2681 then s
2682 else
2683 let s' = withoutlastutf8 s in
2684 let s = s' ^ "@Uellipsis" in
2685 let w = measurestr fs s in
2686 if float x' +. w +. ww < float (hw + x')
2687 then s
2688 else e s'
2690 let s1 =
2691 if float x' +. ww +. measurestr fs s1 > float (hw + x')
2692 then e s1
2693 else s1
2695 ignore (drawstring1 fs x' (y+nfs) s1);
2696 drawstring1 fs (hw + x') (y+nfs) s2
2698 if trusted
2699 then
2700 let x = if helpmode && row > 0 then x +. ww else x in
2701 let tabpos = nindex s '\t' in
2702 if tabpos > 0
2703 then
2704 let len = String.length s - tabpos - 1 in
2705 let s1 = String.sub s 0 tabpos
2706 and s2 = String.sub s (tabpos + 1) len in
2707 let nx = drawstr x s1 in
2708 let sw = nx -. x in
2709 let x = x +. (max tabw sw) in
2710 drawstr x s2
2711 else
2712 let len = String.length s - 2 in
2713 if len > 0 && s.[0] = '\xc2' && s.[1] = '\xb7'
2714 then
2715 let s = String.sub s 2 len in
2716 let x = if not helpmode then x +. ww else x in
2717 GlDraw.color (1.2, 1.2, 1.2);
2718 let vinc = drawstring1 (fs+fs/4)
2719 (truncate (x -. ww)) (y+nfs) s in
2720 GlDraw.color (1., 1., 1.);
2721 vinc +. (float fs *. 0.8)
2722 else
2723 drawstr x s
2724 else
2725 drawstr x s
2727 ignore (drawtabularstring s);
2728 loop (row+1)
2732 loop m_first;
2733 GlDraw.color (1.0, 1.0, 1.0) ~alpha:0.5;
2734 let xadj = float (xadjsb () + 5) in
2735 let rec loop row =
2736 if (row - m_first) > fstate.maxrows
2737 then ()
2738 else (
2739 if row >= 0 && row < itemcount
2740 then (
2741 let (s, level) = source#getitem row in
2742 let pos0 = nindex s '\000' in
2743 let y = (row - m_first) * nfs in
2744 let x = float (level + m_pan) *. ww in
2745 let (first, last) = minfo.(row) in
2746 let prefix =
2747 if pos0 > 0 && first > pos0
2748 then String.sub s (pos0+1) (first-pos0-1)
2749 else String.sub s 0 first
2751 let suffix = String.sub s first (last - first) in
2752 let w1 = measurestr fstate.fontsize prefix in
2753 let w2 = measurestr fstate.fontsize suffix in
2754 let x = x +. if conf.leftscroll then xadj else 5.0 in
2755 let x = if pos0 > 0 && first > pos0 then x +. float hw else x in
2756 let x0 = x +. w1
2757 and y0 = float (y+2) in
2758 let x1 = x0 +. w2
2759 and y1 = float (y+fs+3) in
2760 filledrect x0 y0 x1 y1;
2761 loop (row+1)
2765 Gl.disable `texture_2d;
2766 if Array.length minfo > 0 then loop m_first;
2767 Gl.disable `blend;
2769 method updownlevel incr =
2770 let len = source#getitemcount in
2771 let curlevel =
2772 if m_active >= 0 && m_active < len
2773 then snd (source#getitem m_active)
2774 else -1
2776 let rec flow i =
2777 if i = len then i-1 else if i = -1 then 0 else
2778 let _, l = source#getitem i in
2779 if l != curlevel then i else flow (i+incr)
2781 let active = flow m_active in
2782 let first = calcfirst m_first active in
2783 G.postRedisplay "outline updownlevel";
2784 {< m_active = active; m_first = first >}
2786 method private key1 key mask =
2787 let set1 active first qsearch =
2788 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2790 let search active pattern incr =
2791 let active = if active = -1 then m_first else active in
2792 let dosearch re =
2793 let rec loop n =
2794 if n >= 0 && n < source#getitemcount
2795 then (
2796 let s, _ = source#getitem n in
2798 (try ignore (Str.search_forward re s 0); true
2799 with Not_found -> false)
2800 then Some n
2801 else loop (n + incr)
2803 else None
2805 loop active
2808 let re = Str.regexp_case_fold pattern in
2809 dosearch re
2810 with Failure s ->
2811 state.text <- s;
2812 None
2814 let itemcount = source#getitemcount in
2815 let find start incr =
2816 let rec find i =
2817 if i = -1 || i = itemcount
2818 then -1
2819 else (
2820 if source#hasaction i
2821 then i
2822 else find (i + incr)
2825 find start
2827 let set active first =
2828 let first = bound first 0 (itemcount - fstate.maxrows) in
2829 state.text <- E.s;
2830 coe {< m_active = active; m_first = first; m_qsearch = E.s >}
2832 let navigate incr =
2833 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2834 let active, first =
2835 let incr1 = if incr > 0 then 1 else -1 in
2836 if isvisible m_first m_active
2837 then
2838 let next =
2839 let next = m_active + incr in
2840 let next =
2841 if next < 0 || next >= itemcount
2842 then -1
2843 else find next incr1
2845 if abs (m_active - next) > fstate.maxrows
2846 then -1
2847 else next
2849 if next = -1
2850 then
2851 let first = m_first + incr in
2852 let first = bound first 0 (itemcount - fstate.maxrows) in
2853 let next =
2854 let next = m_active + incr in
2855 let next = bound next 0 (itemcount - 1) in
2856 find next ~-incr1
2858 let active =
2859 if next = -1
2860 then m_active
2861 else (
2862 if isvisible first next
2863 then next
2864 else m_active
2867 active, first
2868 else
2869 let first = min next m_first in
2870 let first =
2871 if abs (next - first) > fstate.maxrows
2872 then first + incr
2873 else first
2875 next, first
2876 else
2877 let first = m_first + incr in
2878 let first = bound first 0 (itemcount - 1) in
2879 let active =
2880 let next = m_active + incr in
2881 let next = bound next 0 (itemcount - 1) in
2882 let next = find next incr1 in
2883 let active =
2884 if next = -1 || abs (m_active - first) > fstate.maxrows
2885 then (
2886 let active = if m_active = -1 then next else m_active in
2887 active
2889 else next
2891 if isvisible first active
2892 then active
2893 else -1
2895 active, first
2897 G.postRedisplay "listview navigate";
2898 set active first;
2900 match key with
2901 | (@r|@s) when Wsi.withctrl mask ->
2902 let incr = if key = @r then -1 else 1 in
2903 let active, first =
2904 match search (m_active + incr) m_qsearch incr with
2905 | None ->
2906 state.text <- m_qsearch ^ " [not found]";
2907 m_active, m_first
2908 | Some active ->
2909 state.text <- m_qsearch;
2910 active, firstof m_first active
2912 G.postRedisplay "listview ctrl-r/s";
2913 set1 active first m_qsearch;
2915 | @insert when Wsi.withctrl mask ->
2916 if m_active >= 0 && m_active < source#getitemcount
2917 then (
2918 let s, _ = source#getitem m_active in
2919 selstring s;
2921 coe self
2923 | @backspace ->
2924 if emptystr m_qsearch
2925 then coe self
2926 else (
2927 let qsearch = withoutlastutf8 m_qsearch in
2928 if emptystr qsearch
2929 then (
2930 state.text <- E.s;
2931 G.postRedisplay "listview empty qsearch";
2932 set1 m_active m_first E.s;
2934 else
2935 let active, first =
2936 match search m_active qsearch ~-1 with
2937 | None ->
2938 state.text <- qsearch ^ " [not found]";
2939 m_active, m_first
2940 | Some active ->
2941 state.text <- qsearch;
2942 active, firstof m_first active
2944 G.postRedisplay "listview backspace qsearch";
2945 set1 active first qsearch
2948 | key when (key != 0 && key land 0xff00 != 0xff00) ->
2949 let pattern = m_qsearch ^ toutf8 key in
2950 let active, first =
2951 match search m_active pattern 1 with
2952 | None ->
2953 state.text <- pattern ^ " [not found]";
2954 m_active, m_first
2955 | Some active ->
2956 state.text <- pattern;
2957 active, firstof m_first active
2959 G.postRedisplay "listview qsearch add";
2960 set1 active first pattern;
2962 | @escape ->
2963 state.text <- E.s;
2964 if emptystr m_qsearch
2965 then (
2966 G.postRedisplay "list view escape";
2967 let mx, my = state.mpos in
2968 updateunder mx my;
2969 begin
2970 match
2971 source#exit ~uioh:(coe self)
2972 ~cancel:true ~active:m_active ~first:m_first ~pan:m_pan
2973 with
2974 | None -> m_prev_uioh
2975 | Some uioh -> uioh
2978 else (
2979 G.postRedisplay "list view kill qsearch";
2980 coe {< m_qsearch = E.s >}
2983 | @enter | @kpenter ->
2984 state.text <- E.s;
2985 let self = {< m_qsearch = E.s >} in
2986 let opt =
2987 G.postRedisplay "listview enter";
2988 if m_active >= 0 && m_active < source#getitemcount
2989 then (
2990 source#exit ~uioh:(coe self) ~cancel:false
2991 ~active:m_active ~first:m_first ~pan:m_pan;
2993 else (
2994 source#exit ~uioh:(coe self) ~cancel:true
2995 ~active:m_active ~first:m_first ~pan:m_pan;
2998 begin match opt with
2999 | None -> m_prev_uioh
3000 | Some uioh -> uioh
3003 | @delete | @kpdelete ->
3004 coe self
3006 | @up | @kpup -> navigate ~-1
3007 | @down | @kpdown -> navigate 1
3008 | @prior | @kpprior -> navigate ~-(fstate.maxrows)
3009 | @next | @kpnext -> navigate fstate.maxrows
3011 | @right | @kpright ->
3012 state.text <- E.s;
3013 G.postRedisplay "listview right";
3014 coe {< m_pan = m_pan - 1 >}
3016 | @left | @kpleft ->
3017 state.text <- E.s;
3018 G.postRedisplay "listview left";
3019 coe {< m_pan = m_pan + 1 >}
3021 | @home | @kphome ->
3022 let active = find 0 1 in
3023 G.postRedisplay "listview home";
3024 set active 0;
3026 | @jend | @kpend ->
3027 let first = max 0 (itemcount - fstate.maxrows) in
3028 let active = find (itemcount - 1) ~-1 in
3029 G.postRedisplay "listview end";
3030 set active first;
3032 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3033 coe self
3035 | _ ->
3036 dolog "listview unknown key %#x" key; coe self
3038 method key key mask =
3039 match state.mode with
3040 | Textentry te -> textentrykeyboard key mask te; coe self
3041 | Birdseye _
3042 | View
3043 | LinkNav _ -> self#key1 key mask
3045 method button button down x y _ =
3046 let opt =
3047 match button with
3048 | 1 when x > state.winw - conf.scrollbw ->
3049 G.postRedisplay "listview scroll";
3050 if down
3051 then
3052 let _, position, sh = self#scrollph in
3053 if y > truncate position && y < truncate (position +. sh)
3054 then (
3055 state.mstate <- Mscrolly;
3056 Some (coe self)
3058 else
3059 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3060 let first = truncate (s *. float source#getitemcount) in
3061 let first = min source#getitemcount first in
3062 Some (coe {< m_first = first; m_active = first >})
3063 else (
3064 state.mstate <- Mnone;
3065 Some (coe self);
3067 | 1 when down ->
3068 begin match self#elemunder y with
3069 | Some n ->
3070 G.postRedisplay "listview click";
3071 source#exit ~uioh:(coe {< m_active = n >})
3072 ~cancel:false ~active:n ~first:m_first ~pan:m_pan
3073 | _ ->
3074 Some (coe self)
3076 | n when (n == 4 || n == 5) && not down ->
3077 let len = source#getitemcount in
3078 let first =
3079 if n = 5 && m_first + fstate.maxrows >= len
3080 then
3081 m_first
3082 else
3083 let first = m_first + (if n == 4 then -1 else 1) in
3084 bound first 0 (len - 1)
3086 G.postRedisplay "listview wheel";
3087 Some (coe {< m_first = first >})
3088 | n when (n = 6 || n = 7) && not down ->
3089 let inc = if n = 7 then -1 else 1 in
3090 G.postRedisplay "listview hwheel";
3091 Some (coe {< m_pan = m_pan + inc >})
3092 | _ ->
3093 Some (coe self)
3095 match opt with
3096 | None -> m_prev_uioh
3097 | Some uioh -> uioh
3099 method multiclick _ x y = self#button 1 true x y
3101 method motion _ y =
3102 match state.mstate with
3103 | Mscrolly ->
3104 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3105 let first = truncate (s *. float source#getitemcount) in
3106 let first = min source#getitemcount first in
3107 G.postRedisplay "listview motion";
3108 coe {< m_first = first; m_active = first >}
3109 | Msel _
3110 | Mpan _
3111 | Mscrollx
3112 | Mzoom _
3113 | Mzoomrect _
3114 | Mnone -> coe self
3116 method pmotion x y =
3117 if x < state.winw - conf.scrollbw
3118 then
3119 let n =
3120 match self#elemunder y with
3121 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3122 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3124 let o =
3125 if n != m_active
3126 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3127 else self
3129 coe o
3130 else (
3131 Wsi.setcursor Wsi.CURSOR_INHERIT;
3132 coe self
3135 method infochanged _ = ()
3137 method scrollpw = (0, 0.0, 0.0)
3138 method scrollph =
3139 let nfs = fstate.fontsize + 1 in
3140 let y = m_first * nfs in
3141 let itemcount = source#getitemcount in
3142 let maxi = max 0 (itemcount - fstate.maxrows) in
3143 let maxy = maxi * nfs in
3144 let p, h = scrollph y maxy in
3145 conf.scrollbw, p, h
3147 method modehash = modehash
3148 method eformsgs = false
3149 method alwaysscrolly = true
3150 end;;
3152 class outlinelistview ~zebra ~source =
3153 let settext autonarrow s =
3154 if autonarrow
3155 then
3156 let ss = source#statestr in
3157 state.text <-
3158 if emptystr ss
3159 then "[" ^ s ^ "]"
3160 else "{" ^ ss ^ "} [" ^ s ^ "]"
3161 else state.text <- s
3163 object (self)
3164 inherit listview
3165 ~zebra
3166 ~helpmode:false
3167 ~source:(source :> lvsource)
3168 ~trusted:false
3169 ~modehash:(findkeyhash conf "outline")
3170 as super
3172 val m_autonarrow = false
3174 method! key key mask =
3175 let maxrows =
3176 if emptystr state.text
3177 then fstate.maxrows
3178 else fstate.maxrows - 2
3180 let calcfirst first active =
3181 if active > first
3182 then
3183 let rows = active - first in
3184 if rows > maxrows then active - maxrows else first
3185 else active
3187 let navigate incr =
3188 let active = m_active + incr in
3189 let active = bound active 0 (source#getitemcount - 1) in
3190 let first = calcfirst m_first active in
3191 G.postRedisplay "outline navigate";
3192 coe {< m_active = active; m_first = first >}
3194 let navscroll first =
3195 let active =
3196 let dist = m_active - first in
3197 if dist < 0
3198 then first
3199 else (
3200 if dist < maxrows
3201 then m_active
3202 else first + maxrows
3205 G.postRedisplay "outline navscroll";
3206 coe {< m_first = first; m_active = active >}
3208 let ctrl = Wsi.withctrl mask in
3209 match key with
3210 | @a when ctrl ->
3211 let text =
3212 if m_autonarrow
3213 then (source#denarrow; E.s)
3214 else (
3215 let pattern = source#renarrow in
3216 if nonemptystr m_qsearch
3217 then (source#narrow m_qsearch; m_qsearch)
3218 else pattern
3221 settext (not m_autonarrow) text;
3222 G.postRedisplay "toggle auto narrowing";
3223 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
3225 | @slash when emptystr m_qsearch && not m_autonarrow ->
3226 settext true E.s;
3227 G.postRedisplay "toggle auto narrowing";
3228 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
3230 | @n when ctrl ->
3231 source#narrow m_qsearch;
3232 if not m_autonarrow
3233 then source#add_narrow_pattern m_qsearch;
3234 G.postRedisplay "outline ctrl-n";
3235 coe {< m_first = 0; m_active = 0 >}
3237 | @S when ctrl ->
3238 let active = source#calcactive (getanchor ()) in
3239 let first = firstof m_first active in
3240 G.postRedisplay "outline ctrl-s";
3241 coe {< m_first = first; m_active = active >}
3243 | @u when ctrl ->
3244 G.postRedisplay "outline ctrl-u";
3245 if m_autonarrow && nonemptystr m_qsearch
3246 then (
3247 ignore (source#renarrow);
3248 settext m_autonarrow E.s;
3249 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3251 else (
3252 source#del_narrow_pattern;
3253 let pattern = source#renarrow in
3254 let text =
3255 if emptystr pattern then E.s else "Narrowed to " ^ pattern
3257 settext m_autonarrow text;
3258 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3261 | @l when ctrl ->
3262 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3263 G.postRedisplay "outline ctrl-l";
3264 coe {< m_first = first >}
3266 | @tab when m_autonarrow ->
3267 if nonemptystr m_qsearch
3268 then (
3269 G.postRedisplay "outline list view tab";
3270 source#add_narrow_pattern m_qsearch;
3271 settext true E.s;
3272 coe {< m_qsearch = E.s >}
3274 else coe self
3276 | @escape when m_autonarrow ->
3277 if nonemptystr m_qsearch
3278 then source#add_narrow_pattern m_qsearch;
3279 super#key key mask
3281 | @enter | @kpenter when m_autonarrow ->
3282 if nonemptystr m_qsearch
3283 then source#add_narrow_pattern m_qsearch;
3284 super#key key mask
3286 | key when m_autonarrow && (key != 0 && key land 0xff00 != 0xff00) ->
3287 let pattern = m_qsearch ^ toutf8 key in
3288 G.postRedisplay "outlinelistview autonarrow add";
3289 source#narrow pattern;
3290 settext true pattern;
3291 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3293 | key when m_autonarrow && key = @backspace ->
3294 if emptystr m_qsearch
3295 then coe self
3296 else
3297 let pattern = withoutlastutf8 m_qsearch in
3298 G.postRedisplay "outlinelistview autonarrow backspace";
3299 ignore (source#renarrow);
3300 source#narrow pattern;
3301 settext true pattern;
3302 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3304 | @delete | @kpdelete ->
3305 source#remove m_active;
3306 G.postRedisplay "outline delete";
3307 let active = max 0 (m_active-1) in
3308 coe {< m_first = firstof m_first active;
3309 m_active = active >}
3311 | @up | @kpup when ctrl ->
3312 navscroll (max 0 (m_first - 1))
3314 | @down | @kpdown when ctrl ->
3315 navscroll (min (source#getitemcount - 1) (m_first + 1))
3317 | @up | @kpup -> navigate ~-1
3318 | @down | @kpdown -> navigate 1
3319 | @prior | @kpprior -> navigate ~-(fstate.maxrows)
3320 | @next | @kpnext -> navigate fstate.maxrows
3322 | @right | @kpright ->
3323 let o =
3324 if ctrl
3325 then (
3326 G.postRedisplay "outline ctrl right";
3327 {< m_pan = m_pan + 1 >}
3329 else self#updownlevel 1
3331 coe o
3333 | @left | @kpleft ->
3334 let o =
3335 if ctrl
3336 then (
3337 G.postRedisplay "outline ctrl left";
3338 {< m_pan = m_pan - 1 >}
3340 else self#updownlevel ~-1
3342 coe o
3344 | @home | @kphome ->
3345 G.postRedisplay "outline home";
3346 coe {< m_first = 0; m_active = 0 >}
3348 | @jend | @kpend ->
3349 let active = source#getitemcount - 1 in
3350 let first = max 0 (active - fstate.maxrows) in
3351 G.postRedisplay "outline end";
3352 coe {< m_active = active; m_first = first >}
3354 | _ -> super#key key mask
3357 let genhistoutlines =
3358 let order ty (p1, c1, _, _, _, _) (p2, c2, _, _, _, _) =
3359 match ty with
3360 | `lastvisit -> compare c1.lastvisit c2.lastvisit
3361 | `path -> compare p2 p1
3362 | `file -> compare (Filename.basename p2) (Filename.basename p1)
3363 | `title ->
3364 let e1 = emptystr c1.title
3365 and e2 = emptystr c2.title in
3366 (**) if e1 && e2
3367 then compare (Filename.basename p2) (Filename.basename p1)
3368 else if e1 then -1
3369 else if e2 then 1
3370 else compare c1.title c2.title
3372 let showfullpath = ref false in
3373 let showorigin = ref true in
3374 let orderty : historder ref = ref `lastvisit in
3375 fun () ->
3376 let setorty s t =
3377 let s = if !orderty = t then "[@Uradical] " ^ s else "[ ] " ^ s in
3378 s, 0, Oaction (fun () -> orderty := t; reenterhist := true)
3380 match Config.gethist () with
3381 | [] -> E.a
3382 | list ->
3383 let ol =
3384 List.fold_left
3385 (fun accu (path, c, b, x, a, o) ->
3386 let hist = (path, (c, b, x, a, o)) in
3387 let s =
3388 let s = if nonemptystr o && !showorigin then o else path in
3389 if !showfullpath then s else Filename.basename s
3391 let base = mbtoutf8 s in
3392 (base ^ "\000" ^ c.title, 1, Ohistory hist) :: accu
3394 [ setorty "Sort by time of last visit" `lastvisit;
3395 setorty "Sort by file name" `file;
3396 setorty "Sort by path" `path;
3397 setorty "Sort by title" `title;
3398 (if !showfullpath then "@Uradical "
3399 else " ") ^ "Show full path", 0, Oaction (fun () ->
3400 showfullpath := not !showfullpath; reenterhist := true);
3401 (if !showorigin then "@Uradical "
3402 else " ") ^ "Show origin", 0, Oaction (fun () ->
3403 showorigin := not !showorigin; reenterhist := true)
3404 ] (List.sort (order !orderty) list)
3406 Array.of_list ol
3409 let gotohist (path, (c, bookmarks, x, anchor, origin)) =
3410 Config.save leavebirdseye;
3411 state.anchor <- anchor;
3412 state.bookmarks <- bookmarks;
3413 state.origin <- origin;
3414 state.x <- x;
3415 setconf conf c;
3416 let x0, y0, x1, y1 = conf.trimfuzz in
3417 wcmd "trimset %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1;
3418 reshape ~firsttime:true state.winw state.winh;
3419 opendoc path origin;
3420 setzoom c.zoom;
3423 let makecheckers () =
3424 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3425 following to say:
3426 converted by Issac Trotts. July 25, 2002 *)
3427 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3428 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3429 let id = GlTex.gen_texture () in
3430 GlTex.bind_texture ~target:`texture_2d id;
3431 GlPix.store (`unpack_alignment 1);
3432 GlTex.image2d image;
3433 List.iter (GlTex.parameter ~target:`texture_2d)
3434 [ `mag_filter `nearest; `min_filter `nearest ];
3438 let setcheckers enabled =
3439 match state.checkerstexid with
3440 | None ->
3441 if enabled then state.checkerstexid <- Some (makecheckers ())
3443 | Some checkerstexid ->
3444 if not enabled
3445 then (
3446 GlTex.delete_texture checkerstexid;
3447 state.checkerstexid <- None;
3451 let describe_location () =
3452 let fn = page_of_y state.y in
3453 let ln = page_of_y (state.y + state.winh - hscrollh () - 1) in
3454 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
3455 let percent =
3456 if maxy <= 0
3457 then 100.
3458 else (100. *. (float state.y /. float maxy))
3460 if fn = ln
3461 then
3462 Printf.sprintf "page %d of %d [%.2f%%]"
3463 (fn+1) state.pagecount percent
3464 else
3465 Printf.sprintf
3466 "pages %d-%d of %d [%.2f%%]"
3467 (fn+1) (ln+1) state.pagecount percent
3470 let setpresentationmode v =
3471 let n = page_of_y state.y in
3472 state.anchor <- (n, 0.0, 1.0);
3473 conf.presentation <- v;
3474 if conf.fitmodel = FitPage
3475 then reqlayout conf.angle conf.fitmodel;
3476 represent ();
3479 let enterinfomode =
3480 let btos b = if b then "@Uradical" else E.s in
3481 let showextended = ref false in
3482 let leave mode _ = state.mode <- mode in
3483 let src =
3484 (object
3485 val mutable m_first_time = true
3486 val mutable m_l = []
3487 val mutable m_a = E.a
3488 val mutable m_prev_uioh = nouioh
3489 val mutable m_prev_mode = View
3491 inherit lvsourcebase
3493 method reset prev_mode prev_uioh =
3494 m_a <- Array.of_list (List.rev m_l);
3495 m_l <- [];
3496 m_prev_mode <- prev_mode;
3497 m_prev_uioh <- prev_uioh;
3498 if m_first_time
3499 then (
3500 let rec loop n =
3501 if n >= Array.length m_a
3502 then ()
3503 else
3504 match m_a.(n) with
3505 | _, _, _, Action _ -> m_active <- n
3506 | _, _, _, Noaction -> loop (n+1)
3508 loop 0;
3509 m_first_time <- false;
3512 method int name get set =
3513 m_l <-
3514 (name, `int get, 1, Action (
3515 fun u ->
3516 let ondone s =
3517 try set (int_of_string s)
3518 with exn ->
3519 state.text <- Printf.sprintf "bad integer `%s': %s"
3520 s (exntos exn)
3522 state.text <- E.s;
3523 let te = name ^ ": ", E.s, None, intentry, ondone, true in
3524 state.mode <- Textentry (te, leave m_prev_mode);
3526 )) :: m_l
3528 method int_with_suffix name get set =
3529 m_l <-
3530 (name, `intws get, 1, Action (
3531 fun u ->
3532 let ondone s =
3533 try set (int_of_string_with_suffix s)
3534 with exn ->
3535 state.text <- Printf.sprintf "bad integer `%s': %s"
3536 s (exntos exn)
3538 state.text <- E.s;
3539 let te =
3540 name ^ ": ", E.s, None, intentry_with_suffix, ondone, true
3542 state.mode <- Textentry (te, leave m_prev_mode);
3544 )) :: m_l
3546 method bool ?(offset=1) ?(btos=btos) name get set =
3547 m_l <-
3548 (name, `bool (btos, get), offset, Action (
3549 fun u ->
3550 let v = get () in
3551 set (not v);
3553 )) :: m_l
3555 method color name get set =
3556 m_l <-
3557 (name, `color get, 1, Action (
3558 fun u ->
3559 let invalid = (nan, nan, nan) in
3560 let ondone s =
3561 let c =
3562 try color_of_string s
3563 with exn ->
3564 state.text <- Printf.sprintf "bad color `%s': %s"
3565 s (exntos exn);
3566 invalid
3568 if c <> invalid
3569 then set c;
3571 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3572 state.text <- color_to_string (get ());
3573 state.mode <- Textentry (te, leave m_prev_mode);
3575 )) :: m_l
3577 method string name get set =
3578 m_l <-
3579 (name, `string get, 1, Action (
3580 fun u ->
3581 let ondone s = set s in
3582 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3583 state.mode <- Textentry (te, leave m_prev_mode);
3585 )) :: m_l
3587 method colorspace name get set =
3588 m_l <-
3589 (name, `string get, 1, Action (
3590 fun _ ->
3591 let source =
3592 (object
3593 inherit lvsourcebase
3595 initializer
3596 m_active <- CSTE.to_int conf.colorspace;
3597 m_first <- 0;
3599 method getitemcount =
3600 Array.length CSTE.names
3601 method getitem n =
3602 (CSTE.names.(n), 0)
3603 method exit ~uioh ~cancel ~active ~first ~pan =
3604 ignore (uioh, first, pan);
3605 if not cancel then set active;
3606 None
3607 method hasaction _ = true
3608 end)
3610 state.text <- E.s;
3611 let modehash = findkeyhash conf "info" in
3612 coe (new listview ~zebra:false ~helpmode:false
3613 ~source ~trusted:true ~modehash)
3614 )) :: m_l
3616 method paxmark name get set =
3617 m_l <-
3618 (name, `string get, 1, Action (
3619 fun _ ->
3620 let source =
3621 (object
3622 inherit lvsourcebase
3624 initializer
3625 m_active <- MTE.to_int conf.paxmark;
3626 m_first <- 0;
3628 method getitemcount = Array.length MTE.names
3629 method getitem n = (MTE.names.(n), 0)
3630 method exit ~uioh ~cancel ~active ~first ~pan =
3631 ignore (uioh, first, pan);
3632 if not cancel then set active;
3633 None
3634 method hasaction _ = true
3635 end)
3637 state.text <- E.s;
3638 let modehash = findkeyhash conf "info" in
3639 coe (new listview ~zebra:false ~helpmode:false
3640 ~source ~trusted:true ~modehash)
3641 )) :: m_l
3643 method fitmodel name get set =
3644 m_l <-
3645 (name, `string get, 1, Action (
3646 fun _ ->
3647 let source =
3648 (object
3649 inherit lvsourcebase
3651 initializer
3652 m_active <- FMTE.to_int conf.fitmodel;
3653 m_first <- 0;
3655 method getitemcount = Array.length FMTE.names
3656 method getitem n = (FMTE.names.(n), 0)
3657 method exit ~uioh ~cancel ~active ~first ~pan =
3658 ignore (uioh, first, pan);
3659 if not cancel then set active;
3660 None
3661 method hasaction _ = true
3662 end)
3664 state.text <- E.s;
3665 let modehash = findkeyhash conf "info" in
3666 coe (new listview ~zebra:false ~helpmode:false
3667 ~source ~trusted:true ~modehash)
3668 )) :: m_l
3670 method caption s offset =
3671 m_l <- (s, `empty, offset, Noaction) :: m_l
3673 method caption2 s f offset =
3674 m_l <- (s, `string f, offset, Noaction) :: m_l
3676 method getitemcount = Array.length m_a
3678 method getitem n =
3679 let tostr = function
3680 | `int f -> string_of_int (f ())
3681 | `intws f -> string_with_suffix_of_int (f ())
3682 | `string f -> f ()
3683 | `color f -> color_to_string (f ())
3684 | `bool (btos, f) -> btos (f ())
3685 | `empty -> E.s
3687 let name, t, offset, _ = m_a.(n) in
3688 ((let s = tostr t in
3689 if nonemptystr s
3690 then Printf.sprintf "%s\t%s" name s
3691 else name),
3692 offset)
3694 method exit ~uioh ~cancel ~active ~first ~pan =
3695 let uiohopt =
3696 if not cancel
3697 then (
3698 let uioh =
3699 match m_a.(active) with
3700 | _, _, _, Action f -> f uioh
3701 | _, _, _, Noaction -> uioh
3703 Some uioh
3705 else None
3707 m_active <- active;
3708 m_first <- first;
3709 m_pan <- pan;
3710 uiohopt
3712 method hasaction n =
3713 match m_a.(n) with
3714 | _, _, _, Action _ -> true
3715 | _, _, _, Noaction -> false
3716 end)
3718 let rec fillsrc prevmode prevuioh =
3719 let sep () = src#caption E.s 0 in
3720 let colorp name get set =
3721 src#string name
3722 (fun () -> color_to_string (get ()))
3723 (fun v ->
3725 let c = color_of_string v in
3726 set c
3727 with exn ->
3728 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
3731 let oldmode = state.mode in
3732 let birdseye = isbirdseye state.mode in
3734 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3736 src#bool "presentation mode"
3737 (fun () -> conf.presentation)
3738 (fun v -> setpresentationmode v);
3740 src#bool "ignore case in searches"
3741 (fun () -> conf.icase)
3742 (fun v -> conf.icase <- v);
3744 src#bool "preload"
3745 (fun () -> conf.preload)
3746 (fun v -> conf.preload <- v);
3748 src#bool "highlight links"
3749 (fun () -> conf.hlinks)
3750 (fun v -> conf.hlinks <- v);
3752 src#bool "under info"
3753 (fun () -> conf.underinfo)
3754 (fun v -> conf.underinfo <- v);
3756 src#bool "persistent bookmarks"
3757 (fun () -> conf.savebmarks)
3758 (fun v -> conf.savebmarks <- v);
3760 src#fitmodel "fit model"
3761 (fun () -> FMTE.to_string conf.fitmodel)
3762 (fun v -> reqlayout conf.angle (FMTE.of_int v));
3764 src#bool "trim margins"
3765 (fun () -> conf.trimmargins)
3766 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3768 src#bool "persistent location"
3769 (fun () -> conf.jumpback)
3770 (fun v -> conf.jumpback <- v);
3772 sep ();
3773 src#int "inter-page space"
3774 (fun () -> conf.interpagespace)
3775 (fun n ->
3776 conf.interpagespace <- n;
3777 docolumns conf.columns;
3778 let pageno, py =
3779 match state.layout with
3780 | [] -> 0, 0
3781 | l :: _ ->
3782 l.pageno, l.pagey
3784 state.maxy <- calcheight ();
3785 let y = getpagey pageno in
3786 gotoy (y + py)
3789 src#int "page bias"
3790 (fun () -> conf.pagebias)
3791 (fun v -> conf.pagebias <- v);
3793 src#int "scroll step"
3794 (fun () -> conf.scrollstep)
3795 (fun n -> conf.scrollstep <- n);
3797 src#int "horizontal scroll step"
3798 (fun () -> conf.hscrollstep)
3799 (fun v -> conf.hscrollstep <- v);
3801 src#int "auto scroll step"
3802 (fun () ->
3803 match state.autoscroll with
3804 | Some step -> step
3805 | _ -> conf.autoscrollstep)
3806 (fun n ->
3807 let n = boundastep state.winh n in
3808 if state.autoscroll <> None
3809 then state.autoscroll <- Some n;
3810 conf.autoscrollstep <- n);
3812 src#int "zoom"
3813 (fun () -> truncate (conf.zoom *. 100.))
3814 (fun v -> setzoom ((float v) /. 100.));
3816 src#int "rotation"
3817 (fun () -> conf.angle)
3818 (fun v -> reqlayout v conf.fitmodel);
3820 src#int "scroll bar width"
3821 (fun () -> conf.scrollbw)
3822 (fun v ->
3823 conf.scrollbw <- v;
3824 reshape state.winw state.winh;
3827 src#int "scroll handle height"
3828 (fun () -> conf.scrollh)
3829 (fun v -> conf.scrollh <- v;);
3831 src#int "thumbnail width"
3832 (fun () -> conf.thumbw)
3833 (fun v ->
3834 conf.thumbw <- min 4096 v;
3835 match oldmode with
3836 | Birdseye beye ->
3837 leavebirdseye beye false;
3838 enterbirdseye ()
3839 | Textentry _
3840 | View
3841 | LinkNav _ -> ()
3844 let mode = state.mode in
3845 src#string "columns"
3846 (fun () ->
3847 match conf.columns with
3848 | Csingle _ -> "1"
3849 | Cmulti (multi, _) -> multicolumns_to_string multi
3850 | Csplit (count, _) -> "-" ^ string_of_int count
3852 (fun v ->
3853 let n, a, b = multicolumns_of_string v in
3854 setcolumns mode n a b);
3856 sep ();
3857 src#caption "Pixmap cache" 0;
3858 src#int_with_suffix "size (advisory)"
3859 (fun () -> conf.memlimit)
3860 (fun v -> conf.memlimit <- v);
3862 src#caption2 "used"
3863 (fun () -> Printf.sprintf "%s bytes, %d tiles"
3864 (string_with_suffix_of_int state.memused)
3865 (Hashtbl.length state.tilemap)) 1;
3867 sep ();
3868 src#caption "Layout" 0;
3869 src#caption2 "Dimension"
3870 (fun () ->
3871 Printf.sprintf "%dx%d (virtual %dx%d)"
3872 state.winw state.winh
3873 state.w state.maxy)
3875 if conf.debug
3876 then
3877 src#caption2 "Position" (fun () ->
3878 Printf.sprintf "%dx%d" state.x state.y
3880 else
3881 src#caption2 "Position" (fun () -> describe_location ()) 1
3884 sep ();
3885 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3886 "Save these parameters as global defaults at exit"
3887 (fun () -> conf.bedefault)
3888 (fun v -> conf.bedefault <- v)
3891 sep ();
3892 let btos b = if b then "@Ulguillemet" else "@Urguillemet" in
3893 src#bool ~offset:0 ~btos "Extended parameters"
3894 (fun () -> !showextended)
3895 (fun v -> showextended := v; fillsrc prevmode prevuioh);
3896 if !showextended
3897 then (
3898 src#bool "checkers"
3899 (fun () -> conf.checkers)
3900 (fun v -> conf.checkers <- v; setcheckers v);
3901 src#bool "update cursor"
3902 (fun () -> conf.updatecurs)
3903 (fun v -> conf.updatecurs <- v);
3904 src#bool "scroll-bar on the left"
3905 (fun () -> conf.leftscroll)
3906 (fun v -> conf.leftscroll <- v);
3907 src#bool "verbose"
3908 (fun () -> conf.verbose)
3909 (fun v -> conf.verbose <- v);
3910 src#bool "invert colors"
3911 (fun () -> conf.invert)
3912 (fun v -> conf.invert <- v);
3913 src#bool "max fit"
3914 (fun () -> conf.maxhfit)
3915 (fun v -> conf.maxhfit <- v);
3916 src#bool "redirect stderr"
3917 (fun () -> conf.redirectstderr)
3918 (fun v -> conf.redirectstderr <- v; redirectstderr ());
3919 src#bool "pax mode"
3920 (fun () -> conf.pax != None)
3921 (fun v ->
3922 if v
3923 then conf.pax <- Some (ref (now (), 0, 0))
3924 else conf.pax <- None);
3925 src#string "uri launcher"
3926 (fun () -> conf.urilauncher)
3927 (fun v -> conf.urilauncher <- v);
3928 src#string "path launcher"
3929 (fun () -> conf.pathlauncher)
3930 (fun v -> conf.pathlauncher <- v);
3931 src#string "tile size"
3932 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3933 (fun v ->
3935 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
3936 conf.tilew <- max 64 w;
3937 conf.tileh <- max 64 h;
3938 flushtiles ();
3939 with exn ->
3940 state.text <- Printf.sprintf "bad tile size `%s': %s"
3941 v (exntos exn)
3943 src#int "texture count"
3944 (fun () -> conf.texcount)
3945 (fun v ->
3946 if realloctexts v
3947 then conf.texcount <- v
3948 else showtext '!' " Failed to set texture count please retry later"
3950 src#int "slice height"
3951 (fun () -> conf.sliceheight)
3952 (fun v ->
3953 conf.sliceheight <- v;
3954 wcmd "sliceh %d" conf.sliceheight;
3956 src#int "anti-aliasing level"
3957 (fun () -> conf.aalevel)
3958 (fun v ->
3959 conf.aalevel <- bound v 0 8;
3960 state.anchor <- getanchor ();
3961 opendoc state.path state.password;
3963 src#string "page scroll scaling factor"
3964 (fun () -> string_of_float conf.pgscale)
3965 (fun v ->
3967 let s = float_of_string v in
3968 conf.pgscale <- s
3969 with exn ->
3970 state.text <- Printf.sprintf
3971 "bad page scroll scaling factor `%s': %s" v (exntos exn)
3974 src#int "ui font size"
3975 (fun () -> fstate.fontsize)
3976 (fun v -> setfontsize (bound v 5 100));
3977 src#int "hint font size"
3978 (fun () -> conf.hfsize)
3979 (fun v -> conf.hfsize <- bound v 5 100);
3980 colorp "background color"
3981 (fun () -> conf.bgcolor)
3982 (fun v -> conf.bgcolor <- v);
3983 src#bool "crop hack"
3984 (fun () -> conf.crophack)
3985 (fun v -> conf.crophack <- v);
3986 src#string "trim fuzz"
3987 (fun () -> irect_to_string conf.trimfuzz)
3988 (fun v ->
3990 conf.trimfuzz <- irect_of_string v;
3991 if conf.trimmargins
3992 then settrim true conf.trimfuzz;
3993 with exn ->
3994 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
3996 src#string "throttle"
3997 (fun () ->
3998 match conf.maxwait with
3999 | None -> "show place holder if page is not ready"
4000 | Some time ->
4001 if time = infinity
4002 then "wait for page to fully render"
4003 else
4004 "wait " ^ string_of_float time
4005 ^ " seconds before showing placeholder"
4007 (fun v ->
4009 let f = float_of_string v in
4010 if f <= 0.0
4011 then conf.maxwait <- None
4012 else conf.maxwait <- Some f
4013 with exn ->
4014 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
4016 src#string "ghyll scroll"
4017 (fun () ->
4018 match conf.ghyllscroll with
4019 | None -> E.s
4020 | Some nab -> ghyllscroll_to_string nab
4022 (fun v ->
4023 try conf.ghyllscroll <- ghyllscroll_of_string v
4024 with exn ->
4025 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
4027 src#string "selection command"
4028 (fun () -> conf.selcmd)
4029 (fun v -> conf.selcmd <- v);
4030 src#string "synctex command"
4031 (fun () -> conf.stcmd)
4032 (fun v -> conf.stcmd <- v);
4033 src#string "pax command"
4034 (fun () -> conf.paxcmd)
4035 (fun v -> conf.paxcmd <- v);
4036 src#string "ask password command"
4037 (fun () -> conf.passcmd)
4038 (fun v -> conf.passcmd <- v);
4039 src#string "save path command"
4040 (fun () -> conf.savecmd)
4041 (fun v -> conf.savecmd <- v);
4042 src#colorspace "color space"
4043 (fun () -> CSTE.to_string conf.colorspace)
4044 (fun v ->
4045 conf.colorspace <- CSTE.of_int v;
4046 wcmd "cs %d" v;
4047 load state.layout;
4049 src#paxmark "pax mark method"
4050 (fun () -> MTE.to_string conf.paxmark)
4051 (fun v -> conf.paxmark <- MTE.of_int v);
4052 if pbousable ()
4053 then
4054 src#bool "use PBO"
4055 (fun () -> conf.usepbo)
4056 (fun v -> conf.usepbo <- v);
4057 src#bool "mouse wheel scrolls pages"
4058 (fun () -> conf.wheelbypage)
4059 (fun v -> conf.wheelbypage <- v);
4060 src#bool "open remote links in a new instance"
4061 (fun () -> conf.riani)
4062 (fun v -> conf.riani <- v);
4063 src#bool "edit annotations inline"
4064 (fun () -> conf.annotinline)
4065 (fun v -> conf.annotinline <- v);
4068 sep ();
4069 src#caption "Document" 0;
4070 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4071 src#caption2 "Pages"
4072 (fun () -> string_of_int state.pagecount) 1;
4073 src#caption2 "Dimensions"
4074 (fun () -> string_of_int (List.length state.pdims)) 1;
4075 if conf.trimmargins
4076 then (
4077 sep ();
4078 src#caption "Trimmed margins" 0;
4079 src#caption2 "Dimensions"
4080 (fun () -> string_of_int (List.length state.pdims)) 1;
4083 sep ();
4084 src#caption "OpenGL" 0;
4085 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4086 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4088 sep ();
4089 src#caption "Location" 0;
4090 if nonemptystr state.origin
4091 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
4092 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
4094 src#reset prevmode prevuioh;
4096 fun () ->
4097 state.text <- E.s;
4098 resetmstate ();
4099 let prevmode = state.mode
4100 and prevuioh = state.uioh in
4101 fillsrc prevmode prevuioh;
4102 let source = (src :> lvsource) in
4103 let modehash = findkeyhash conf "info" in
4104 state.uioh <- coe (object (self)
4105 inherit listview ~zebra:false ~helpmode:false
4106 ~source ~trusted:true ~modehash as super
4107 val mutable m_prevmemused = 0
4108 method! infochanged = function
4109 | Memused ->
4110 if m_prevmemused != state.memused
4111 then (
4112 m_prevmemused <- state.memused;
4113 G.postRedisplay "memusedchanged";
4115 | Pdim -> G.postRedisplay "pdimchanged"
4116 | Docinfo -> fillsrc prevmode prevuioh
4118 method! key key mask =
4119 if not (Wsi.withctrl mask)
4120 then
4121 match key with
4122 | @left | @kpleft -> coe (self#updownlevel ~-1)
4123 | @right | @kpright -> coe (self#updownlevel 1)
4124 | _ -> super#key key mask
4125 else super#key key mask
4126 end);
4127 G.postRedisplay "info";
4130 let enterhelpmode =
4131 let source =
4132 (object
4133 inherit lvsourcebase
4134 method getitemcount = Array.length state.help
4135 method getitem n =
4136 let s, l, _ = state.help.(n) in
4137 (s, l)
4139 method exit ~uioh ~cancel ~active ~first ~pan =
4140 let optuioh =
4141 if not cancel
4142 then (
4143 match state.help.(active) with
4144 | _, _, Action f -> Some (f uioh)
4145 | _, _, Noaction -> Some uioh
4147 else None
4149 m_active <- active;
4150 m_first <- first;
4151 m_pan <- pan;
4152 optuioh
4154 method hasaction n =
4155 match state.help.(n) with
4156 | _, _, Action _ -> true
4157 | _, _, Noaction -> false
4159 initializer
4160 m_active <- -1
4161 end)
4162 in fun () ->
4163 let modehash = findkeyhash conf "help" in
4164 resetmstate ();
4165 state.uioh <- coe (new listview
4166 ~zebra:false ~helpmode:true
4167 ~source ~trusted:true ~modehash);
4168 G.postRedisplay "help";
4171 let entermsgsmode =
4172 let msgsource =
4173 (object
4174 inherit lvsourcebase
4175 val mutable m_items = E.a
4177 method getitemcount = 1 + Array.length m_items
4179 method getitem n =
4180 if n = 0
4181 then "[Clear]", 0
4182 else m_items.(n-1), 0
4184 method exit ~uioh ~cancel ~active ~first ~pan =
4185 ignore uioh;
4186 if not cancel
4187 then (
4188 if active = 0
4189 then Buffer.clear state.errmsgs;
4191 m_active <- active;
4192 m_first <- first;
4193 m_pan <- pan;
4194 None
4196 method hasaction n =
4197 n = 0
4199 method reset =
4200 state.newerrmsgs <- false;
4201 let l = Str.split newlinere (Buffer.contents state.errmsgs) in
4202 m_items <- Array.of_list l
4204 initializer
4205 m_active <- 0
4206 end)
4207 in fun () ->
4208 state.text <- E.s;
4209 resetmstate ();
4210 msgsource#reset;
4211 let source = (msgsource :> lvsource) in
4212 let modehash = findkeyhash conf "listview" in
4213 state.uioh <- coe (object
4214 inherit listview ~zebra:false ~helpmode:false
4215 ~source ~trusted:false ~modehash as super
4216 method! display =
4217 if state.newerrmsgs
4218 then msgsource#reset;
4219 super#display
4220 end);
4221 G.postRedisplay "msgs";
4224 let getusertext s =
4225 let editor = getenvwithdef "EDITOR" E.s in
4226 if emptystr editor
4227 then E.s
4228 else
4229 let tmppath = Filename.temp_file "llpp" "note" in
4230 if nonemptystr s
4231 then (
4232 let oc = open_out tmppath in
4233 output_string oc s;
4234 close_out oc;
4236 let execstr = editor ^ " " ^ tmppath in
4237 let s =
4238 match popen execstr [] with
4239 | (exception exn) ->
4240 showtext '!' @@
4241 Printf.sprintf "popen(%S) failed: %s" execstr (exntos exn);
4243 | pid ->
4244 match Unix.waitpid [] pid
4245 with
4246 | (exception exn) ->
4247 showtext '!' @@
4248 Printf.sprintf "waitpid(%d) failed: %s" pid (exntos exn);
4250 | (_pid, status) ->
4251 match status with
4252 | Unix.WEXITED 0 -> filelines tmppath
4253 | Unix.WEXITED n ->
4254 showtext '!' @@
4255 Printf.sprintf "editor process(%s) exited abnormally: %d"
4256 execstr n;
4258 | Unix.WSIGNALED n ->
4259 showtext '!' @@
4260 Printf.sprintf "editor process(%s) was killed by signal %d"
4261 execstr n;
4263 | Unix.WSTOPPED n ->
4264 showtext '!' @@
4265 Printf.sprintf "editor(%s) process was stopped by signal %d"
4266 execstr n;
4269 match Unix.unlink tmppath with
4270 | (exception exn) ->
4271 showtext '!' @@ Printf.sprintf "failed to ulink %S: %s"
4272 tmppath (exntos exn);
4274 | () -> s
4277 let enterannotmode opaque slinkindex =
4278 let msgsource =
4279 (object
4280 inherit lvsourcebase
4281 val mutable m_text = E.s
4282 val mutable m_items = E.a
4284 method getitemcount = Array.length m_items
4286 method getitem n =
4287 let label, _func = m_items.(n) in
4288 label, 0
4290 method exit ~uioh ~cancel ~active ~first ~pan =
4291 ignore (uioh, first, pan);
4292 if not cancel
4293 then (
4294 let _label, func = m_items.(active) in
4295 func ()
4297 None
4299 method hasaction n = nonemptystr @@ fst m_items.(n)
4301 method reset s =
4302 let rec split accu b i =
4303 let p = b+i in
4304 if p = String.length s
4305 then (String.sub s b (p-b), unit) :: accu
4306 else
4307 if (i > 70 && s.[p] = ' ') || s.[p] = '\r' || s.[p] = '\n'
4308 then
4309 let ss = if i = 0 then E.s else String.sub s b i in
4310 split ((ss, unit)::accu) (p+1) 0
4311 else
4312 split accu b (i+1)
4314 let cleanup () =
4315 wcmd "freepage %s" (~> opaque);
4316 let keys =
4317 Hashtbl.fold (fun key opaque' accu ->
4318 if opaque' = opaque'
4319 then key :: accu else accu) state.pagemap []
4321 List.iter (Hashtbl.remove state.pagemap) keys;
4322 flushtiles ();
4323 gotoy state.y
4325 let dele () =
4326 delannot opaque slinkindex;
4327 cleanup ();
4329 let edit inline () =
4330 let update s =
4331 if emptystr s
4332 then dele ()
4333 else (
4334 modannot opaque slinkindex s;
4335 cleanup ();
4338 if inline
4339 then
4340 let mode = state.mode in
4341 state.mode <-
4342 Textentry (
4343 ("annotation: ", m_text, None, textentry, update, true),
4344 fun _ -> state.mode <- mode);
4345 state.text <- E.s;
4346 enttext ();
4347 else
4348 let s = getusertext m_text in
4349 update s
4351 m_text <- s;
4352 m_items <-
4353 ( "[Copy]", fun () -> selstring m_text)
4354 :: ("[Delete]", dele)
4355 :: ("[Edit]", edit conf.annotinline)
4356 :: (E.s, unit)
4357 :: split [] 0 0 |> List.rev |> Array.of_list
4359 initializer
4360 m_active <- 0
4361 end)
4363 state.text <- E.s;
4364 let s = getannotcontents opaque slinkindex in
4365 resetmstate ();
4366 msgsource#reset s;
4367 let source = (msgsource :> lvsource) in
4368 let modehash = findkeyhash conf "listview" in
4369 state.uioh <- coe (object
4370 inherit listview ~zebra:false ~helpmode:false
4371 ~source ~trusted:false ~modehash
4372 end);
4373 G.postRedisplay "enterannotmode";
4376 let gotounder under =
4377 let getpath filename =
4378 let path =
4379 if nonemptystr filename
4380 then
4381 if Filename.is_relative filename
4382 then
4383 let dir = Filename.dirname state.path in
4384 let dir =
4385 if Filename.is_implicit dir
4386 then Filename.concat (Sys.getcwd ()) dir
4387 else dir
4389 Filename.concat dir filename
4390 else filename
4391 else E.s
4393 if Sys.file_exists path
4394 then path
4395 else E.s
4397 match under with
4398 | Ulinkgoto (pageno, top) ->
4399 if pageno >= 0
4400 then (
4401 addnav ();
4402 gotopage1 pageno top;
4405 | Ulinkuri s ->
4406 addpid @@ gotouri s
4408 | Uremote (filename, pageno) ->
4409 let path = getpath filename in
4410 if nonemptystr path
4411 then (
4412 if conf.riani
4413 then
4414 let command = Printf.sprintf "%s -page %d %S" !selfexec pageno path in
4415 try addpid @@ popen command []
4416 with exn ->
4417 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
4418 flush stderr;
4419 else
4420 let anchor = getanchor () in
4421 let ranchor = state.path, state.password, anchor, state.origin in
4422 state.origin <- E.s;
4423 state.anchor <- (pageno, 0.0, 0.0);
4424 state.ranchors <- ranchor :: state.ranchors;
4425 opendoc path E.s;
4427 else showtext '!' ("Could not find " ^ filename)
4429 | Uremotedest (filename, destname) ->
4430 let path = getpath filename in
4431 if nonemptystr path
4432 then (
4433 if conf.riani
4434 then
4435 let command = !selfexec ^ " " ^ path ^ " -dest " ^ destname in
4436 try addpid @@ popen command []
4437 with exn ->
4438 Printf.eprintf
4439 "failed to execute `%s': %s\n" command (exntos exn);
4440 flush stderr;
4441 else
4442 let anchor = getanchor () in
4443 let ranchor = state.path, state.password, anchor, state.origin in
4444 state.origin <- E.s;
4445 state.nameddest <- destname;
4446 state.ranchors <- ranchor :: state.ranchors;
4447 opendoc path E.s;
4449 else showtext '!' ("Could not find " ^ filename)
4451 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4452 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
4455 let gotooutline (_, _, kind) =
4456 match kind with
4457 | Onone -> ()
4458 | Oanchor anchor ->
4459 let (pageno, y, _) = anchor in
4460 let y = getanchory
4461 (if conf.presentation then (pageno, y, 1.0) else anchor)
4463 addnav ();
4464 gotoghyll y
4465 | Ouri uri -> gotounder (Ulinkuri uri)
4466 | Olaunch cmd -> gotounder (Ulaunch cmd)
4467 | Oremote remote -> gotounder (Uremote remote)
4468 | Ohistory hist -> gotohist hist
4469 | Oremotedest remotedest -> gotounder (Uremotedest remotedest)
4470 | Oaction f -> f ()
4473 let outlinesource sourcetype =
4474 (object (self)
4475 inherit lvsourcebase
4476 val mutable m_items = E.a
4477 val mutable m_minfo = E.a
4478 val mutable m_orig_items = E.a
4479 val mutable m_orig_minfo = E.a
4480 val mutable m_narrow_patterns = []
4481 val mutable m_hadremovals = false
4482 val mutable m_gen = -1
4484 method getitemcount =
4485 Array.length m_items + (if m_hadremovals then 1 else 0)
4487 method getitem n =
4488 if n == Array.length m_items && m_hadremovals
4489 then
4490 ("[Confirm removal]", 0)
4491 else
4492 let s, n, _ = m_items.(n) in
4493 (s, n)
4495 method exit ~uioh ~cancel ~active ~first ~pan =
4496 ignore (uioh, first);
4497 let confrimremoval = m_hadremovals && active = Array.length m_items in
4498 let items, minfo =
4499 if m_narrow_patterns = []
4500 then m_orig_items, m_orig_minfo
4501 else m_items, m_minfo
4503 if not cancel
4504 then (
4505 if not confrimremoval
4506 then (
4507 gotooutline m_items.(active);
4508 m_items <- items;
4509 m_minfo <- minfo;
4511 else (
4512 state.bookmarks <- Array.to_list m_items;
4513 m_orig_items <- m_items;
4514 m_orig_minfo <- m_minfo;
4517 else (
4518 m_items <- items;
4519 m_minfo <- minfo;
4521 m_pan <- pan;
4522 None
4524 method hasaction _ = true
4526 method greetmsg =
4527 if Array.length m_items != Array.length m_orig_items
4528 then
4529 let s =
4530 match m_narrow_patterns with
4531 | one :: [] -> one
4532 | many -> String.concat "@Uellipsis" (List.rev many)
4534 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
4535 else E.s
4537 method statestr =
4538 match m_narrow_patterns with
4539 | [] -> E.s
4540 | one :: [] -> one
4541 | head :: _ -> "@Uellipsis" ^ head
4543 method narrow pattern =
4544 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
4545 match reopt with
4546 | None -> ()
4547 | Some re ->
4548 let rec loop accu minfo n =
4549 if n = -1
4550 then (
4551 m_items <- Array.of_list accu;
4552 m_minfo <- Array.of_list minfo;
4554 else
4555 let (s, _, t) as o = m_items.(n) in
4556 let accu, minfo =
4557 match t with
4558 | Oaction _ -> o :: accu, (0, 0) :: minfo
4559 | Onone | Oanchor _ | Ouri _ | Olaunch _
4560 | Oremote _ | Oremotedest _ | Ohistory _ ->
4561 let first =
4562 try Str.search_forward re s 0
4563 with Not_found -> -1
4565 if first >= 0
4566 then o :: accu, (first, Str.match_end ()) :: minfo
4567 else accu, minfo
4569 loop accu minfo (n-1)
4571 loop [] [] (Array.length m_items - 1)
4573 method! getminfo = m_minfo
4575 method denarrow =
4576 m_orig_items <- (
4577 match sourcetype with
4578 | `bookmarks -> Array.of_list state.bookmarks
4579 | `outlines -> state.outlines
4580 | `history -> genhistoutlines ()
4582 m_minfo <- m_orig_minfo;
4583 m_items <- m_orig_items
4585 method remove m =
4586 if sourcetype = `bookmarks
4587 then
4588 if m >= 0 && m < Array.length m_items
4589 then (
4590 m_hadremovals <- true;
4591 m_items <- Array.init (Array.length m_items - 1) (fun n ->
4592 let n = if n >= m then n+1 else n in
4593 m_items.(n)
4597 method add_narrow_pattern pattern =
4598 m_narrow_patterns <- pattern :: m_narrow_patterns
4600 method del_narrow_pattern =
4601 match m_narrow_patterns with
4602 | _ :: rest -> m_narrow_patterns <- rest
4603 | [] -> ()
4605 method renarrow =
4606 self#denarrow;
4607 match m_narrow_patterns with
4608 | pattern :: [] -> self#narrow pattern; pattern
4609 | list ->
4610 List.fold_left (fun accu pattern ->
4611 self#narrow pattern;
4612 pattern ^ "@Uellipsis" ^ accu) E.s list
4614 method calcactive anchor =
4615 let rely = getanchory anchor in
4616 let rec loop n best bestd =
4617 if n = Array.length m_items
4618 then best
4619 else
4620 let _, _, kind = m_items.(n) in
4621 match kind with
4622 | Oanchor anchor ->
4623 let orely = getanchory anchor in
4624 let d = abs (orely - rely) in
4625 if d < bestd
4626 then loop (n+1) n d
4627 else loop (n+1) best bestd
4628 | Onone | Oremote _ | Olaunch _
4629 | Oremotedest _ | Ouri _ | Ohistory _ | Oaction _ ->
4630 loop (n+1) best bestd
4632 loop 0 ~-1 max_int
4634 method reset anchor items =
4635 m_hadremovals <- false;
4636 if state.gen != m_gen
4637 then (
4638 m_orig_items <- items;
4639 m_items <- items;
4640 m_narrow_patterns <- [];
4641 m_minfo <- E.a;
4642 m_orig_minfo <- E.a;
4643 m_gen <- state.gen;
4645 else (
4646 if items != m_orig_items
4647 then (
4648 m_orig_items <- items;
4649 if m_narrow_patterns == []
4650 then m_items <- items;
4653 let active = self#calcactive anchor in
4654 m_active <- active;
4655 m_first <- firstof m_first active
4656 end)
4659 let enterselector sourcetype =
4660 resetmstate ();
4661 let source = outlinesource sourcetype in
4662 fun errmsg ->
4663 let outlines =
4664 match sourcetype with
4665 | `bookmarks -> Array.of_list state.bookmarks
4666 | `outlines -> state.outlines
4667 | `history -> genhistoutlines ()
4669 if Array.length outlines = 0
4670 then (
4671 showtext ' ' errmsg;
4673 else (
4674 state.text <- source#greetmsg;
4675 Wsi.setcursor Wsi.CURSOR_INHERIT;
4676 let anchor = getanchor () in
4677 source#reset anchor outlines;
4678 state.uioh <-
4679 coe (new outlinelistview ~zebra:(sourcetype=`history) ~source);
4680 G.postRedisplay "enter selector";
4684 let enteroutlinemode () = enterselector `outlines "Document has no outline";;
4685 let enterbookmarkmode () =
4686 enterselector `bookmarks "Document has no bookmarks (yet)"
4688 let enterhistmode () = enterselector `history "No history (yet)";;
4690 let quickbookmark ?title () =
4691 match state.layout with
4692 | [] -> ()
4693 | l :: _ ->
4694 let title =
4695 match title with
4696 | None ->
4697 let tm = Unix.localtime (now ()) in
4698 Printf.sprintf
4699 "Quick (page %d) (bookmarked at %02d/%02d/%d %02d:%02d)"
4700 (l.pageno+1)
4701 tm.Unix.tm_mday
4702 (tm.Unix.tm_mon+1)
4703 (tm.Unix.tm_year + 1900)
4704 tm.Unix.tm_hour
4705 tm.Unix.tm_min
4706 | Some title -> title
4708 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4711 let setautoscrollspeed step goingdown =
4712 let incr = max 1 ((abs step) / 2) in
4713 let incr = if goingdown then incr else -incr in
4714 let astep = boundastep state.winh (step + incr) in
4715 state.autoscroll <- Some astep;
4718 let canpan () =
4719 match conf.columns with
4720 | Csplit _ -> true
4721 | Csingle _ | Cmulti _ -> state.x != 0 || conf.zoom > 1.0
4724 let panbound x = bound x (-state.w) (wadjsb () + state.winw);;
4726 let existsinrow pageno (columns, coverA, coverB) p =
4727 let last = ((pageno - coverA) mod columns) + columns in
4728 let rec any = function
4729 | [] -> false
4730 | l :: rest ->
4731 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4732 then p l
4733 else (
4734 if not (p l)
4735 then (if l.pageno = last then false else any rest)
4736 else true
4739 any state.layout
4742 let nextpage () =
4743 match state.layout with
4744 | [] ->
4745 let pageno = page_of_y state.y in
4746 gotoghyll (getpagey (pageno+1))
4747 | l :: rest ->
4748 match conf.columns with
4749 | Csingle _ ->
4750 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4751 then
4752 let y = clamp (pgscale state.winh) in
4753 gotoghyll y
4754 else
4755 let pageno = min (l.pageno+1) (state.pagecount-1) in
4756 gotoghyll (getpagey pageno)
4757 | Cmulti ((c, _, _) as cl, _) ->
4758 if conf.presentation
4759 && (existsinrow l.pageno cl
4760 (fun l -> l.pageh > l.pagey + l.pagevh))
4761 then
4762 let y = clamp (pgscale state.winh) in
4763 gotoghyll y
4764 else
4765 let pageno = min (l.pageno+c) (state.pagecount-1) in
4766 gotoghyll (getpagey pageno)
4767 | Csplit (n, _) ->
4768 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4769 then
4770 let pagey, pageh = getpageyh l.pageno in
4771 let pagey = pagey + pageh * l.pagecol in
4772 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4773 gotoghyll (pagey + pageh + ips)
4776 let prevpage () =
4777 match state.layout with
4778 | [] ->
4779 let pageno = page_of_y state.y in
4780 gotoghyll (getpagey (pageno-1))
4781 | l :: _ ->
4782 match conf.columns with
4783 | Csingle _ ->
4784 if conf.presentation && l.pagey != 0
4785 then
4786 gotoghyll (clamp (pgscale ~-(state.winh)))
4787 else
4788 let pageno = max 0 (l.pageno-1) in
4789 gotoghyll (getpagey pageno)
4790 | Cmulti ((c, _, coverB) as cl, _) ->
4791 if conf.presentation &&
4792 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4793 then
4794 gotoghyll (clamp (pgscale ~-(state.winh)))
4795 else
4796 let decr =
4797 if l.pageno = state.pagecount - coverB
4798 then 1
4799 else c
4801 let pageno = max 0 (l.pageno-decr) in
4802 gotoghyll (getpagey pageno)
4803 | Csplit (n, _) ->
4804 let y =
4805 if l.pagecol = 0
4806 then
4807 if l.pageno = 0
4808 then l.pagey
4809 else
4810 let pageno = max 0 (l.pageno-1) in
4811 let pagey, pageh = getpageyh pageno in
4812 pagey + (n-1)*pageh
4813 else
4814 let pagey, pageh = getpageyh l.pageno in
4815 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4817 gotoghyll y
4820 let save () =
4821 if emptystr conf.savecmd
4822 then error "don't know where to save modified document"
4823 else
4824 let savecmd = Str.global_replace percentsre state.path conf.savecmd in
4825 let path =
4826 getcmdoutput
4827 (fun s -> error "failed to obtain path to the saved copy: %s" s)
4828 savecmd
4830 if nonemptystr path
4831 then
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, linknact 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, linknact (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 conf.annotinline 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 let doreap = ref false in
6363 selfexec := Sys.executable_name;
6364 Arg.parse
6365 (Arg.align
6366 [("-p", Arg.String (fun s -> state.password <- s),
6367 "<password> Set password");
6369 ("-f", Arg.String
6370 (fun s ->
6371 Config.fontpath := s;
6372 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
6374 "<path> Set path to the user interface font");
6376 ("-c", Arg.String
6377 (fun s ->
6378 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
6379 Config.confpath := s),
6380 "<path> Set path to the configuration file");
6382 ("-last", Arg.Set openlast, " Open last document");
6384 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
6385 "<page-number> Jump to page");
6387 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6388 "<path> Set path to the trim cache file");
6390 ("-dest", Arg.String (fun s -> state.nameddest <- s),
6391 "<named-destination> Set named destination");
6393 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
6394 ("-cxack", Arg.Set cxack, " Cut corners");
6396 ("-remote", Arg.String (fun s -> rcmdpath := s),
6397 "<path> Set path to the remote commands source");
6399 ("-origin", Arg.String (fun s -> state.origin <- s),
6400 "<original-path> Set original path");
6402 ("-gc", Arg.Set_string gcconfig,
6403 "<script-path> Collect garbage with the help of a script");
6405 ("-nofc", Arg.Set nofc, " Do not use fontconfig");
6407 ("-v", Arg.Unit (fun () ->
6408 Printf.printf
6409 "%s\nconfiguration path: %s\n"
6410 (version ())
6411 Config.defconfpath
6413 exit 0), " Print version and exit");
6415 ("-embed", Arg.Set_int rootwid,
6416 "<window-id> Embed into window")
6419 (fun s -> state.path <- s)
6420 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6422 if !wtmode
6423 then selfexec := !selfexec ^ " -wtmode";
6425 let histmode = emptystr state.path && not !openlast in
6427 if not (Config.load !openlast)
6428 then prerr_endline "failed to load configuration";
6429 begin match !pageno with
6430 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
6431 | None -> ()
6432 end;
6434 if nonemptystr !gcconfig
6435 then (
6436 let c, s =
6437 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6438 | exception exn ->
6439 error "gc socketpair failed: %s" (exntos exn)
6440 | rw -> rw
6442 match addpid @@ popen !gcconfig [(c, 0); (c, 1)] with
6443 | exception exn ->
6444 error "failed to popen gc script: %s" (exntos exn);
6445 | _ ->
6446 Config.gc s s;
6447 exit 0
6450 let wsfd, winw, winh = Wsi.init (object (self)
6451 val mutable m_clicks = 0
6452 val mutable m_click_x = 0
6453 val mutable m_click_y = 0
6454 val mutable m_lastclicktime = infinity
6456 method private cleanup =
6457 state.roam <- noroam;
6458 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap
6459 method expose = G.postRedisplay"expose"
6460 method visible v =
6461 let name =
6462 match v with
6463 | Wsi.Unobscured -> "unobscured"
6464 | Wsi.PartiallyObscured -> "partiallyobscured"
6465 | Wsi.FullyObscured -> "fullyobscured"
6467 vlog "visibility change %s" name
6468 method display = display ()
6469 method map mapped = vlog "mappped %b" mapped
6470 method reshape w h =
6471 self#cleanup;
6472 reshape w h
6473 method mouse b d x y m =
6474 if d && canselect ()
6475 then (
6476 (* http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx *)
6477 m_click_x <- x;
6478 m_click_y <- y;
6479 if b = 1
6480 then (
6481 let t = now () in
6482 if abs x - m_click_x > 10
6483 || abs y - m_click_y > 10
6484 || abs_float (t -. m_lastclicktime) > 0.3
6485 then m_clicks <- 0;
6486 m_clicks <- m_clicks + 1;
6487 m_lastclicktime <- t;
6488 if m_clicks = 1
6489 then (
6490 self#cleanup;
6491 G.postRedisplay "cleanup";
6492 state.uioh <- state.uioh#button b d x y m;
6494 else state.uioh <- state.uioh#multiclick m_clicks x y m
6496 else (
6497 self#cleanup;
6498 m_clicks <- 0;
6499 m_lastclicktime <- infinity;
6500 state.uioh <- state.uioh#button b d x y m
6503 else (
6504 state.uioh <- state.uioh#button b d x y m
6506 method motion x y =
6507 state.mpos <- (x, y);
6508 state.uioh <- state.uioh#motion x y
6509 method pmotion x y =
6510 state.mpos <- (x, y);
6511 state.uioh <- state.uioh#pmotion x y
6512 method key k m =
6513 let mascm = m land (
6514 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6515 ) in
6516 let keyboard k m =
6517 let x = state.x and y = state.y in
6518 keyboard k m;
6519 if x != state.x || y != state.y then self#cleanup
6521 match state.keystate with
6522 | KSnone ->
6523 let km = k, mascm in
6524 begin
6525 match
6526 let modehash = state.uioh#modehash in
6527 try Hashtbl.find modehash km
6528 with Not_found ->
6529 try Hashtbl.find (findkeyhash conf "global") km
6530 with Not_found -> KMinsrt (k, m)
6531 with
6532 | KMinsrt (k, m) -> keyboard k m
6533 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6534 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6536 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6537 List.iter (fun (k, m) -> keyboard k m) insrt;
6538 state.keystate <- KSnone
6539 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6540 state.keystate <- KSinto (keys, insrt)
6541 | KSinto _ -> state.keystate <- KSnone
6543 method enter x y =
6544 state.mpos <- (x, y);
6545 state.uioh <- state.uioh#pmotion x y
6546 method leave = state.mpos <- (-1, -1)
6547 method winstate wsl = state.winstate <- wsl
6548 method quit = raise Quit
6549 end) !rootwid conf.cwinw conf.cwinh platform in
6551 state.wsfd <- wsfd;
6553 if not (
6554 List.exists GlMisc.check_extension
6555 [ "GL_ARB_texture_rectangle"
6556 ; "GL_EXT_texture_recangle"
6557 ; "GL_NV_texture_rectangle" ]
6559 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6561 if (
6562 let r = GlMisc.get_string `renderer in
6563 let p = "Mesa DRI Intel(" in
6564 let l = String.length p in
6565 String.length r > l && String.sub r 0 l = p
6567 then (
6568 defconf.sliceheight <- 1024;
6569 defconf.texcount <- 32;
6570 defconf.usepbo <- true;
6573 let cs, ss =
6574 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6575 | exception exn ->
6576 Printf.eprintf "socketpair failed: %s" (exntos exn);
6577 exit 1
6578 | (r, w) ->
6579 cloexec r;
6580 cloexec w;
6581 r, w
6584 setcheckers conf.checkers;
6585 redirectstderr ();
6586 if conf.redirectstderr
6587 then
6588 at_exit (fun () ->
6589 let s = Bytes.cat
6590 (Buffer.to_bytes state.errmsgs)
6591 (match state.errfd with
6592 | Some fd ->
6593 let s = Bytes.create (80*24) in
6594 let n =
6596 let r, _, _ = Unix.select [fd] [] [] 0.0 in
6597 if List.mem fd r
6598 then Unix.read fd s 0 (Bytes.length s)
6599 else 0
6600 with _ -> 0
6602 if n = 0
6603 then E.b
6604 else Bytes.sub s 0 n
6605 | None -> E.b
6608 try ignore (Unix.write state.stderr s 0 (Bytes.length s))
6609 with exn -> print_endline (exntos exn)
6613 init cs (
6614 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
6615 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6616 !Config.fontpath, !trimcachepath,
6617 GlMisc.check_extension "GL_ARB_pixel_buffer_object",
6618 not !nofc
6620 List.iter GlArray.enable [`texture_coord; `vertex];
6621 state.ss <- ss;
6622 reshape ~firsttime:true winw winh;
6623 state.uioh <- uioh;
6624 if histmode
6625 then (
6626 Wsi.settitle "llpp (history)";
6627 enterhistmode ();
6629 else (
6630 state.text <- "Opening " ^ (mbtoutf8 state.path);
6631 opendoc state.path state.password;
6633 display ();
6634 Wsi.mapwin ();
6635 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
6637 let rec reap () =
6638 match Unix.waitpid [Unix.WNOHANG] ~-1 with
6639 | (exception (Unix.Unix_error (Unix.ECHILD, _, _))) -> ()
6640 | (exception exn) -> dolog "Unix.waitpid: %s" @@ exntos exn
6641 | 0, _ -> ()
6642 | _pid, _status -> reap ()
6644 Sys.set_signal Sys.sigchld (Sys.Signal_handle (fun _ -> doreap := true));
6646 let optrfd =
6647 ref (
6648 if nonemptystr !rcmdpath
6649 then remoteopen !rcmdpath
6650 else None
6654 let rec loop deadline =
6655 if !doreap
6656 then (
6657 doreap := false;
6658 reap ()
6660 let r =
6661 match state.errfd with
6662 | None -> [state.ss; state.wsfd]
6663 | Some fd -> [state.ss; state.wsfd; fd]
6665 let r =
6666 match !optrfd with
6667 | None -> r
6668 | Some fd -> fd :: r
6670 if state.redisplay
6671 then (
6672 state.redisplay <- false;
6673 display ();
6675 let timeout =
6676 let now = now () in
6677 if deadline > now
6678 then (
6679 if deadline = infinity
6680 then ~-.1.0
6681 else max 0.0 (deadline -. now)
6683 else 0.0
6685 let r, _, _ =
6686 try Unix.select r [] [] timeout
6687 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6689 begin match r with
6690 | [] ->
6691 state.ghyll None;
6692 let newdeadline =
6693 if state.ghyll == noghyll
6694 then
6695 match state.autoscroll with
6696 | Some step when step != 0 ->
6697 let y = state.y + step in
6698 let y =
6699 if y < 0
6700 then state.maxy
6701 else if y >= state.maxy then 0 else y
6703 gotoy y;
6704 if state.mode = View
6705 then state.text <- E.s;
6706 deadline +. 0.01
6707 | _ -> infinity
6708 else deadline +. 0.01
6710 loop newdeadline
6712 | l ->
6713 let rec checkfds = function
6714 | [] -> ()
6715 | fd :: rest when fd = state.ss ->
6716 let cmd = readcmd state.ss in
6717 act cmd;
6718 checkfds rest
6720 | fd :: rest when fd = state.wsfd ->
6721 Wsi.readresp fd;
6722 checkfds rest
6724 | fd :: rest when Some fd = !optrfd ->
6725 begin match remote fd with
6726 | None -> optrfd := remoteopen !rcmdpath;
6727 | opt -> optrfd := opt
6728 end;
6729 checkfds rest
6731 | fd :: rest ->
6732 let s = Bytes.create 80 in
6733 let n = tempfailureretry (Unix.read fd s 0) 80 in
6734 if conf.redirectstderr
6735 then (
6736 Buffer.add_substring state.errmsgs (Bytes.to_string s) 0 n;
6737 state.newerrmsgs <- true;
6738 state.redisplay <- true;
6740 else (
6741 prerr_string (String.sub (Bytes.to_string s) 0 n);
6742 flush stderr;
6744 checkfds rest
6746 checkfds l;
6747 if !reenterhist then (
6748 enterhistmode ();
6749 reenterhist := false;
6751 let newdeadline =
6752 let deadline1 =
6753 if deadline = infinity
6754 then now () +. 0.01
6755 else deadline
6757 match state.autoscroll with
6758 | Some step when step != 0 -> deadline1
6759 | _ -> if state.ghyll == noghyll then infinity else deadline1
6761 loop newdeadline
6762 end;
6765 loop infinity;
6766 with Quit ->
6767 Config.save leavebirdseye;
6768 if hasunsavedchanges ()
6769 then save ();