Refactoring
[llpp.git] / main.ml
blobdc10fc0dc861c508c89ca209bc82e95b47c81ea6
1 open Utils;;
2 open Config;;
4 exception Quit;;
6 external init : Unix.file_descr -> params -> unit = "ml_init";;
7 external seltext : opaque -> (int * int * int * int) -> unit = "ml_seltext";;
8 external hassel : opaque -> bool = "ml_hassel";;
9 external copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
10 external getpdimrect : int -> float array = "ml_getpdimrect";;
11 external whatsunder : opaque -> int -> int -> under = "ml_whatsunder";;
12 external markunder : opaque -> int -> int -> mark -> bool = "ml_markunder";;
13 external clearmark : opaque -> unit = "ml_clearmark";;
14 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
15 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
16 external measurestr : int -> string -> float = "ml_measure_string";;
17 external postprocess :
18 opaque -> int -> int -> int -> (int * string * int) -> int
19 = "ml_postprocess";;
20 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
21 external setaalevel : int -> unit = "ml_setaalevel";;
22 external realloctexts : int -> bool = "ml_realloctexts";;
23 external findlink : opaque -> linkdir -> link = "ml_findlink";;
24 external getlink : opaque -> int -> under = "ml_getlink";;
25 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
26 external getlinkcount : opaque -> int = "ml_getlinkcount";;
27 external findpwl : int -> int -> pagewithlinks = "ml_find_page_with_links";;
28 external getpbo : width -> height -> colorspace -> opaque = "ml_getpbo";;
29 external freepbo : opaque -> unit = "ml_freepbo";;
30 external unmappbo : opaque -> unit = "ml_unmappbo";;
31 external pbousable : unit -> bool = "ml_pbo_usable";;
32 external unproject : opaque -> int -> int -> (int * int) option
33 = "ml_unproject";;
34 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
35 external rectofblock : opaque -> int -> int -> float array option
36 = "ml_rectofblock";;
37 external begintiles : unit -> unit = "ml_begintiles";;
38 external endtiles : unit -> unit = "ml_endtiles";;
39 external addannot : opaque -> int -> int -> string -> unit = "ml_addannot";;
40 external modannot : opaque -> slinkindex -> string -> unit = "ml_modannot";;
41 external delannot : opaque -> slinkindex -> unit = "ml_delannot";;
42 external hasunsavedchanges : unit -> bool = "ml_hasunsavedchanges";;
43 external savedoc : string -> unit = "ml_savedoc";;
44 external getannotcontents : opaque -> slinkindex -> string
45 = "ml_getannotcontents";;
47 let reeenterhist = ref false;;
48 let selfexec = ref E.s;;
50 let drawstring size x y s =
51 Gl.enable `blend;
52 Gl.enable `texture_2d;
53 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
54 ignore (drawstr size x y s);
55 Gl.disable `blend;
56 Gl.disable `texture_2d;
59 let drawstring1 size x y s =
60 drawstr size x y s;
63 let drawstring2 size x y fmt =
64 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
67 let debugl l =
68 dolog "l %d dim=%d {" l.pageno l.pagedimno;
69 dolog " WxH %dx%d" l.pagew l.pageh;
70 dolog " vWxH %dx%d" l.pagevw l.pagevh;
71 dolog " pagex,y %d,%d" l.pagex l.pagey;
72 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
73 dolog " column %d" l.pagecol;
74 dolog "}";
77 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
78 dolog "rect {";
79 dolog " x0,y0=(% f, % f)" x0 y0;
80 dolog " x1,y1=(% f, % f)" x1 y1;
81 dolog " x2,y2=(% f, % f)" x2 y2;
82 dolog " x3,y3=(% f, % f)" x3 y3;
83 dolog "}";
86 let isbirdseye = function
87 | Birdseye _ -> true
88 | Textentry _
89 | View
90 | LinkNav _ -> false
93 let istextentry = function
94 | Textentry _ -> true
95 | Birdseye _
96 | View
97 | LinkNav _ -> false
100 let wtmode = ref false;;
101 let cxack = ref false;;
103 let pgscale h = truncate (float h *. conf.pgscale);;
105 let hscrollh () =
106 if not state.uioh#alwaysscrolly && (conf.scrollb land scrollbhv = 0)
107 || (state.x = 0 && state.w <= state.winw - conf.scrollbw)
108 then 0
109 else conf.scrollbw
112 let vscrollw () =
113 if not state.uioh#alwaysscrolly && (conf.scrollb land scrollbvv = 0)
114 then 0
115 else conf.scrollbw
118 let wadjsb () = -vscrollw ();;
119 let xadjsb () = if conf.leftscroll then vscrollw () else 0;;
121 let setfontsize n =
122 fstate.fontsize <- n;
123 fstate.wwidth <- measurestr fstate.fontsize "w";
124 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
127 let vlog fmt =
128 if conf.verbose
129 then
130 Printf.kprintf prerr_endline fmt
131 else
132 Printf.kprintf ignore fmt
135 let launchpath () =
136 if emptystr conf.pathlauncher
137 then print_endline state.path
138 else (
139 let command = Str.global_replace percentsre state.path conf.pathlauncher in
140 try addpid @@ popen command []
141 with exn ->
142 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
143 flush stderr;
147 let redirectstderr () =
148 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
149 if conf.redirectstderr
150 then
151 match Unix.pipe () with
152 | exception exn ->
153 dolog "failed to create stderr redirection pipes: %s" (exntos exn)
155 | (r, w) ->
156 begin match Unix.dup Unix.stderr with
157 | exception exn ->
158 dolog "failed to dup stderr: %s" (exntos exn);
159 Ne.clo r (clofail "pipe/r");
160 Ne.clo w (clofail "pipe/w");
162 | dupstderr ->
163 begin match Unix.dup2 w Unix.stderr with
164 | exception exn ->
165 dolog "failed to dup2 to stderr: %s" (exntos exn);
166 Ne.clo dupstderr (clofail "stderr duplicate");
167 Ne.clo r (clofail "redir pipe/r");
168 Ne.clo w (clofail "redir pipe/w");
170 | () ->
171 state.stderr <- dupstderr;
172 state.errfd <- Some r;
173 end;
175 else (
176 state.newerrmsgs <- false;
177 begin match state.errfd with
178 | Some fd ->
179 begin match Unix.dup2 state.stderr Unix.stderr with
180 | exception exn ->
181 dolog "failed to dup2 original stderr: %s" (exntos exn)
182 | () ->
183 Ne.clo fd (clofail "dup of stderr");
184 state.errfd <- None;
185 end;
186 | None -> ()
187 end;
188 prerr_string (Buffer.contents state.errmsgs);
189 flush stderr;
190 Buffer.clear state.errmsgs;
194 module G =
195 struct
196 let postRedisplay who =
197 if conf.verbose
198 then prerr_endline ("redisplay for " ^ who);
199 state.redisplay <- true;
201 end;;
203 let getopaque pageno =
204 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
205 with Not_found -> None
208 let putopaque pageno opaque =
209 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
212 let pagetranslatepoint l x y =
213 let dy = y - l.pagedispy in
214 let y = dy + l.pagey in
215 let dx = x - l.pagedispx in
216 let x = dx + l.pagex in
217 (x, y);
220 let onppundermouse g x y d =
221 let rec f = function
222 | l :: rest ->
223 begin match getopaque l.pageno with
224 | Some opaque ->
225 let x0 = l.pagedispx in
226 let x1 = x0 + l.pagevw in
227 let y0 = l.pagedispy in
228 let y1 = y0 + l.pagevh in
229 if y >= y0 && y <= y1 && x >= x0 && x <= x1
230 then
231 let px, py = pagetranslatepoint l x y in
232 match g opaque l px py with
233 | Some res -> res
234 | None -> f rest
235 else f rest
236 | _ ->
237 f rest
239 | [] -> d
241 f state.layout
244 let getunder x y =
245 let g opaque l px py =
246 if state.bzoom
247 then (
248 match rectofblock opaque px py with
249 | Some a ->
250 let rect = (a.(0),a.(2),a.(1),a.(2),a.(1),a.(3),a.(0),a.(3)) in
251 state.rects <- [l.pageno, l.pageno mod 3, rect];
252 G.postRedisplay "getunder";
253 | None -> ()
255 let under = whatsunder opaque px py in
256 if under = Unone then None else Some under
258 onppundermouse g x y Unone
261 let unproject x y =
262 let g opaque l x y =
263 match unproject opaque x y with
264 | Some (x, y) -> Some (Some (opaque, l.pageno, x, y))
265 | None -> None
267 onppundermouse g x y None;
270 let showtext c s =
271 state.text <- Printf.sprintf "%c%s" c s;
272 G.postRedisplay "showtext";
275 let pipesel opaque cmd =
276 if hassel opaque
277 then
278 match Unix.pipe () with
279 | exception exn ->
280 showtext '!'
281 (Printf.sprintf "pipesel can not create pipe: %s" (exntos exn));
282 | (r, w) ->
283 let doclose what fd =
284 Ne.clo fd (fun msg -> dolog "%s close failed: %s" what msg)
286 let pid =
287 try popen cmd [r, 0; w, -1]
288 with exn ->
289 dolog "can not execute %S: %s" cmd (exntos exn);
292 if pid > 0
293 then (
294 copysel w opaque;
295 G.postRedisplay "pipesel";
297 else doclose "pipesel pipe/w" w;
298 doclose "pipesel pipe/r" r;
301 let paxunder x y =
302 let g opaque l px py =
303 if markunder opaque px py conf.paxmark
304 then (
305 Some (fun () ->
306 match getopaque l.pageno with
307 | None -> ()
308 | Some opaque -> pipesel opaque conf.paxcmd
311 else None
313 G.postRedisplay "paxunder";
314 if conf.paxmark = Mark_page
315 then
316 List.iter (fun l ->
317 match getopaque l.pageno with
318 | None -> ()
319 | Some opaque -> clearmark opaque) state.layout;
320 state.roam <-
321 onppundermouse g x y (fun () -> showtext '!' "Whoopsie daisy");
324 let selstring s =
325 match Unix.pipe () with
326 | exception exn ->
327 showtext '!' (Printf.sprintf "pipe failed: %s" (exntos exn))
328 | (r, w) ->
329 let clo cap fd =
330 Ne.clo fd (fun msg ->
331 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
334 let pid =
335 try popen conf.selcmd [r, 0; w, -1]
336 with exn ->
337 showtext '!'
338 (Printf.sprintf "failed to execute %s: %s"
339 conf.selcmd (exntos exn));
342 if pid > 0
343 then (
345 let l = String.length s in
346 let bytes = Bytes.unsafe_of_string s in
347 let n = tempfailureretry (Unix.write w bytes 0) l in
348 if n != l
349 then
350 showtext '!'
351 (Printf.sprintf
352 "failed to write %d characters to sel pipe, wrote %d"
355 with exn ->
356 showtext '!'
357 (Printf.sprintf "failed to write to sel pipe: %s"
358 (exntos exn)
361 else dolog "%s" s;
362 clo "selstring pipe/r" r;
363 clo "selstring pipe/w" w;
366 let undertext = function
367 | Unone -> "none"
368 | Ulinkuri s -> s
369 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
370 | Utext s -> "font: " ^ s
371 | Uunexpected s -> "unexpected: " ^ s
372 | Ulaunch s -> "launch: " ^ s
373 | Unamed s -> "named: " ^ s
374 | Uremote (filename, pageno) ->
375 Printf.sprintf "%s: page %d" filename (pageno+1)
376 | Uremotedest (filename, destname) ->
377 Printf.sprintf "%s: destination %S" filename destname
378 | Uannotation (opaque, slinkindex) ->
379 "annotation: " ^ getannotcontents opaque slinkindex
382 let updateunder x y =
383 match getunder x y with
384 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
385 | Ulinkuri uri ->
386 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
387 Wsi.setcursor Wsi.CURSOR_INFO
388 | Ulinkgoto (pageno, _) ->
389 if conf.underinfo
390 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
391 Wsi.setcursor Wsi.CURSOR_INFO
392 | Utext s ->
393 if conf.underinfo then showtext 'f' ("ont: " ^ s);
394 Wsi.setcursor Wsi.CURSOR_TEXT
395 | Uunexpected s ->
396 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
397 Wsi.setcursor Wsi.CURSOR_INHERIT
398 | Ulaunch s ->
399 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
400 Wsi.setcursor Wsi.CURSOR_INHERIT
401 | Unamed s ->
402 if conf.underinfo then showtext 'n' ("amed: " ^ s);
403 Wsi.setcursor Wsi.CURSOR_INHERIT
404 | Uremote (filename, pageno) ->
405 if conf.underinfo then showtext 'r'
406 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
407 Wsi.setcursor Wsi.CURSOR_INFO
408 | Uremotedest (filename, destname) ->
409 if conf.underinfo then showtext 'r'
410 (Printf.sprintf "emote destination: %s (%S)" filename destname);
411 Wsi.setcursor Wsi.CURSOR_INFO
412 | Uannotation _ ->
413 if conf.underinfo then showtext 'a' "nnotation";
414 Wsi.setcursor Wsi.CURSOR_INFO
417 let showlinktype under =
418 if conf.underinfo && under != Unone
419 then showtext ' ' @@ undertext under
422 let intentry_with_suffix text key =
423 let c =
424 if key >= 32 && key < 127
425 then Char.chr key
426 else '\000'
428 match Char.lowercase c with
429 | '0' .. '9' ->
430 let text = addchar text c in
431 TEcont text
433 | 'k' | 'm' | 'g' ->
434 let text = addchar text c in
435 TEcont text
437 | _ ->
438 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
439 TEcont text
442 let readcmd fd =
443 let s = Bytes.create 4 in
444 let n = tempfailureretry (Unix.read fd s 0) 4 in
445 if n != 4 then error "incomplete read(len) = %d" n;
446 let len = (Char.code (Bytes.get s 0) lsl 24)
447 lor (Char.code (Bytes.get s 1) lsl 16)
448 lor (Char.code (Bytes.get s 2) lsl 8)
449 lor (Char.code (Bytes.get s 3))
451 let s = Bytes.create len in
452 let n = tempfailureretry (Unix.read fd s 0) len in
453 if n != len then error "incomplete read(data) %d vs %d" n len;
454 Bytes.to_string s
457 let wcmd fmt =
458 let b = Buffer.create 16 in
459 Buffer.add_string b "llll";
460 Printf.kbprintf
461 (fun b ->
462 let s = Buffer.to_bytes b in
463 let n = Bytes.length s in
464 let len = n - 4 in
465 (* dolog "wcmd %S" (String.sub s 4 len); *)
466 Bytes.set s 0 (Char.chr ((len lsr 24) land 0xff));
467 Bytes.set s 1 (Char.chr ((len lsr 16) land 0xff));
468 Bytes.set s 2 (Char.chr ((len lsr 8) land 0xff));
469 Bytes.set s 3 (Char.chr (len land 0xff));
470 let n' = tempfailureretry (Unix.write state.ss s 0) n in
471 if n' != n then error "write failed %d vs %d" n' n;
472 ) b fmt;
475 let nogeomcmds cmds =
476 match cmds with
477 | s, [] -> emptystr s
478 | _ -> false
481 let layoutN ((columns, coverA, coverB), b) y sh =
482 let sh = sh - (hscrollh ()) in
483 let wadj = wadjsb () in
484 let rec fold accu n =
485 if n = Array.length b
486 then accu
487 else
488 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
489 if (vy - y) > sh &&
490 (n = coverA - 1
491 || n = state.pagecount - coverB
492 || (n - coverA) mod columns = columns - 1)
493 then accu
494 else
495 let accu =
496 if vy + h > y
497 then
498 let pagey = max 0 (y - vy) in
499 let pagedispy = if pagey > 0 then 0 else vy - y in
500 let pagedispx, pagex =
501 let pdx =
502 if n = coverA - 1 || n = state.pagecount - coverB
503 then state.x + (wadj + state.winw - w) / 2
504 else dx + xoff + state.x
506 if pdx < 0
507 then 0, -pdx
508 else pdx, 0
510 let pagevw =
511 let vw = wadj + state.winw - pagedispx in
512 let pw = w - pagex in
513 min vw pw
515 let pagevh = min (h - pagey) (sh - pagedispy) in
516 if pagevw > 0 && pagevh > 0
517 then
518 let e =
519 { pageno = n
520 ; pagedimno = pdimno
521 ; pagew = w
522 ; pageh = h
523 ; pagex = pagex
524 ; pagey = pagey
525 ; pagevw = pagevw
526 ; pagevh = pagevh
527 ; pagedispx = pagedispx
528 ; pagedispy = pagedispy
529 ; pagecol = 0
532 e :: accu
533 else
534 accu
535 else
536 accu
538 fold accu (n+1)
540 if Array.length b = 0
541 then []
542 else List.rev (fold [] (page_of_y y))
545 let layoutS (columns, b) y sh =
546 let sh = sh - hscrollh () in
547 let wadj = wadjsb () in
548 let rec fold accu n =
549 if n = Array.length b
550 then accu
551 else
552 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
553 if (vy - y) > sh
554 then accu
555 else
556 let accu =
557 if vy + pageh > y
558 then
559 let x = xoff + state.x in
560 let pagey = max 0 (y - vy) in
561 let pagedispy = if pagey > 0 then 0 else vy - y in
562 let pagedispx, pagex =
563 if px = 0
564 then (
565 if x < 0
566 then 0, -x
567 else x, 0
569 else (
570 let px = px - x in
571 if px < 0
572 then -px, 0
573 else 0, px
576 let pagecolw = pagew/columns in
577 let pagedispx =
578 if pagecolw < state.winw
579 then pagedispx + ((wadj + state.winw - pagecolw) / 2)
580 else pagedispx
582 let pagevw =
583 let vw = wadj + state.winw - pagedispx in
584 let pw = pagew - pagex in
585 min vw pw
587 let pagevw = min pagevw pagecolw in
588 let pagevh = min (pageh - pagey) (sh - pagedispy) in
589 if pagevw > 0 && pagevh > 0
590 then
591 let e =
592 { pageno = n/columns
593 ; pagedimno = pdimno
594 ; pagew = pagew
595 ; pageh = pageh
596 ; pagex = pagex
597 ; pagey = pagey
598 ; pagevw = pagevw
599 ; pagevh = pagevh
600 ; pagedispx = pagedispx
601 ; pagedispy = pagedispy
602 ; pagecol = n mod columns
605 e :: accu
606 else
607 accu
608 else
609 accu
611 fold accu (n+1)
613 List.rev (fold [] 0)
616 let layout y sh =
617 if nogeomcmds state.geomcmds
618 then
619 match conf.columns with
620 | Csingle b -> layoutN ((1, 0, 0), b) y sh
621 | Cmulti c -> layoutN c y sh
622 | Csplit s -> layoutS s y sh
623 else []
626 let clamp incr =
627 let y = state.y + incr in
628 let y = max 0 y in
629 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
633 let itertiles l f =
634 let tilex = l.pagex mod conf.tilew in
635 let tiley = l.pagey mod conf.tileh in
637 let col = l.pagex / conf.tilew in
638 let row = l.pagey / conf.tileh in
640 let xadj = xadjsb () in
641 let rec rowloop row y0 dispy h =
642 if h = 0
643 then ()
644 else (
645 let dh = conf.tileh - y0 in
646 let dh = min h dh in
647 let rec colloop col x0 dispx w =
648 if w = 0
649 then ()
650 else (
651 let dw = conf.tilew - x0 in
652 let dw = min w dw in
653 let dispx' = xadj + dispx in
654 f col row dispx' dispy x0 y0 dw dh;
655 colloop (col+1) 0 (dispx+dw) (w-dw)
658 colloop col tilex l.pagedispx l.pagevw;
659 rowloop (row+1) 0 (dispy+dh) (h-dh)
662 if l.pagevw > 0 && l.pagevh > 0
663 then rowloop row tiley l.pagedispy l.pagevh;
666 let gettileopaque l col row =
667 let key =
668 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
670 try Some (Hashtbl.find state.tilemap key)
671 with Not_found -> None
674 let puttileopaque l col row gen colorspace angle opaque size elapsed =
675 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
676 Hashtbl.add state.tilemap key (opaque, size, elapsed)
679 let filledrect x0 y0 x1 y1 =
680 GlArray.disable `texture_coord;
681 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
682 GlArray.vertex `two state.vraw;
683 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
684 GlArray.enable `texture_coord;
687 let linerect x0 y0 x1 y1 =
688 GlArray.disable `texture_coord;
689 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y1; x1; y0 |];
690 GlArray.vertex `two state.vraw;
691 GlArray.draw_arrays `line_loop ~first:0 ~count:4;
692 GlArray.enable `texture_coord;
695 let drawtiles l color =
696 GlDraw.color color;
697 let wadj = wadjsb () in
698 begintiles ();
699 let f col row x y tilex tiley w h =
700 match gettileopaque l col row with
701 | Some (opaque, _, t) ->
702 let params = x, y, w, h, tilex, tiley in
703 if conf.invert
704 then GlTex.env (`mode `blend);
705 drawtile params opaque;
706 if conf.invert
707 then GlTex.env (`mode `modulate);
708 if conf.debug
709 then (
710 endtiles ();
711 let s = Printf.sprintf
712 "%d[%d,%d] %f sec"
713 l.pageno col row t
715 let w = measurestr fstate.fontsize s in
716 GlDraw.color (0.0, 0.0, 0.0);
717 filledrect (float (x-2))
718 (float (y-2))
719 (float (x+2) +. w)
720 (float (y + fstate.fontsize + 2));
721 GlDraw.color (1.0, 1.0, 1.0);
722 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
723 begintiles ();
726 | None ->
727 endtiles ();
728 let w =
729 if conf.leftscroll
730 then w
731 else
732 let lw = wadj + state.winw - x in
733 min lw w
734 and h =
735 let lh = state.winh - y in
736 min lh h
738 if conf.invert
739 then GlTex.env (`mode `blend);
740 begin match state.checkerstexid with
741 | Some id ->
742 Gl.enable `texture_2d;
743 GlTex.bind_texture ~target:`texture_2d id;
744 let x0 = float x
745 and y0 = float y
746 and x1 = float (x+w)
747 and y1 = float (y+h) in
749 let tw = float w /. 16.0
750 and th = float h /. 16.0 in
751 let tx0 = float tilex /. 16.0
752 and ty0 = float tiley /. 16.0 in
753 let tx1 = tx0 +. tw
754 and ty1 = ty0 +. th in
755 Raw.sets_float state.vraw ~pos:0
756 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
757 Raw.sets_float state.traw ~pos:0
758 [| tx0; ty0; tx0; ty1; tx1; ty0; tx1; ty1 |];
759 GlArray.vertex `two state.vraw;
760 GlArray.tex_coord `two state.traw;
761 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
762 Gl.disable `texture_2d;
764 | None ->
765 GlDraw.color (1.0, 1.0, 1.0);
766 filledrect (float x) (float y) (float (x+w)) (float (y+h));
767 end;
768 if conf.invert
769 then GlTex.env (`mode `modulate);
770 if w > 128 && h > fstate.fontsize + 10
771 then (
772 let c = if conf.invert then 1.0 else 0.0 in
773 GlDraw.color (c, c, c);
774 let c, r =
775 if conf.verbose
776 then (col*conf.tilew, row*conf.tileh)
777 else col, row
779 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
781 GlDraw.color color;
782 begintiles ();
784 itertiles l f;
785 endtiles ();
788 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
790 let tilevisible1 l x y =
791 let ax0 = l.pagex
792 and ax1 = l.pagex + l.pagevw
793 and ay0 = l.pagey
794 and ay1 = l.pagey + l.pagevh in
796 let bx0 = x
797 and by0 = y in
798 let bx1 = min (bx0 + conf.tilew) l.pagew
799 and by1 = min (by0 + conf.tileh) l.pageh in
801 let rx0 = max ax0 bx0
802 and ry0 = max ay0 by0
803 and rx1 = min ax1 bx1
804 and ry1 = min ay1 by1 in
806 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
807 nonemptyintersection
810 let tilevisible layout n x y =
811 let rec findpageinlayout m = function
812 | l :: rest when l.pageno = n ->
813 tilevisible1 l x y || (
814 match conf.columns with
815 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
816 | Csplit _
817 | Csingle _
818 | Cmulti _ -> false
820 | _ :: rest -> findpageinlayout 0 rest
821 | [] -> false
823 findpageinlayout 0 layout;
826 let tileready l x y =
827 tilevisible1 l x y &&
828 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
831 let tilepage n p layout =
832 let rec loop = function
833 | l :: rest ->
834 if l.pageno = n
835 then
836 let f col row _ _ _ _ _ _ =
837 if state.currently = Idle
838 then
839 match gettileopaque l col row with
840 | Some _ -> ()
841 | None ->
842 let x = col*conf.tilew
843 and y = row*conf.tileh in
844 let w =
845 let w = l.pagew - x in
846 min w conf.tilew
848 let h =
849 let h = l.pageh - y in
850 min h conf.tileh
852 let pbo =
853 if conf.usepbo
854 then getpbo w h conf.colorspace
855 else ~< "0"
857 wcmd "tile %s %d %d %d %d %s"
858 (~> p) x y w h (~> pbo);
859 state.currently <-
860 Tiling (
861 l, p, conf.colorspace, conf.angle,
862 state.gen, col, row, conf.tilew, conf.tileh
865 itertiles l f;
866 else
867 loop rest
869 | [] -> ()
871 if nogeomcmds state.geomcmds
872 then loop layout;
875 let preloadlayout y =
876 let y = if y < state.winh then 0 else y - state.winh in
877 let h = state.winh*3 in
878 layout y h;
881 let load pages =
882 let rec loop pages =
883 if state.currently != Idle
884 then ()
885 else
886 match pages with
887 | l :: rest ->
888 begin match getopaque l.pageno with
889 | None ->
890 wcmd "page %d %d" l.pageno l.pagedimno;
891 state.currently <- Loading (l, state.gen);
892 | Some opaque ->
893 tilepage l.pageno opaque pages;
894 loop rest
895 end;
896 | _ -> ()
898 if nogeomcmds state.geomcmds
899 then loop pages
902 let preload pages =
903 load pages;
904 if conf.preload && state.currently = Idle
905 then load (preloadlayout state.y);
908 let layoutready layout =
909 let rec fold all ls =
910 all && match ls with
911 | l :: rest ->
912 let seen = ref false in
913 let allvisible = ref true in
914 let foo col row _ _ _ _ _ _ =
915 seen := true;
916 allvisible := !allvisible &&
917 begin match gettileopaque l col row with
918 | Some _ -> true
919 | None -> false
922 itertiles l foo;
923 fold (!seen && !allvisible) rest
924 | [] -> true
926 let alltilesvisible = fold true layout in
927 alltilesvisible;
930 let gotoy y =
931 let y = bound y 0 state.maxy in
932 let y, layout, proceed =
933 match conf.maxwait with
934 | Some time when state.ghyll == noghyll ->
935 begin match state.throttle with
936 | None ->
937 let layout = layout y state.winh in
938 let ready = layoutready layout in
939 if not ready
940 then (
941 load layout;
942 state.throttle <- Some (layout, y, now ());
944 else G.postRedisplay "gotoy showall (None)";
945 y, layout, ready
946 | Some (_, _, started) ->
947 let dt = now () -. started in
948 if dt > time
949 then (
950 state.throttle <- None;
951 let layout = layout y state.winh in
952 load layout;
953 G.postRedisplay "maxwait";
954 y, layout, true
956 else -1, [], false
959 | _ ->
960 let layout = layout y state.winh in
961 if not !wtmode || layoutready layout
962 then G.postRedisplay "gotoy ready";
963 y, layout, true
965 if proceed
966 then (
967 state.y <- y;
968 state.layout <- layout;
969 begin match state.mode with
970 | LinkNav ln ->
971 begin match ln with
972 | Ltexact (pageno, linkno) ->
973 let rec loop = function
974 | [] ->
975 state.mode <- LinkNav (Ltgendir 0)
976 | l :: _ when l.pageno = pageno ->
977 begin match getopaque pageno with
978 | None -> state.mode <- LinkNav (Ltnotready (pageno, 0))
979 | Some opaque ->
980 let x0, y0, x1, y1 = getlinkrect opaque linkno in
981 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
982 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
983 then state.mode <- LinkNav (Ltgendir 0)
985 | _ :: rest -> loop rest
987 loop layout
988 | Ltnotready _ | Ltgendir _ -> ()
990 | Birdseye _
991 | Textentry _
992 | View -> ()
993 end;
994 begin match state.mode with
995 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
996 if not (pagevisible layout pageno)
997 then (
998 match state.layout with
999 | [] -> ()
1000 | l :: _ ->
1001 state.mode <- Birdseye (
1002 conf, leftx, l.pageno, hooverpageno, anchor
1005 | LinkNav lt ->
1006 begin match lt with
1007 | Ltnotready (_, dir)
1008 | Ltgendir dir ->
1009 let linknav =
1010 let rec loop = function
1011 | [] -> lt
1012 | l :: rest ->
1013 match getopaque l.pageno with
1014 | None -> Ltnotready (l.pageno, dir)
1015 | Some opaque ->
1016 let link =
1017 let ld =
1018 if dir = 0
1019 then LDfirstvisible (l.pagex, l.pagey, dir)
1020 else (
1021 if dir > 0 then LDfirst else LDlast
1024 findlink opaque ld
1026 match link with
1027 | Lnotfound -> loop rest
1028 | Lfound n ->
1029 showlinktype (getlink opaque n);
1030 Ltexact (l.pageno, n)
1032 loop state.layout
1034 state.mode <- LinkNav linknav
1035 | Ltexact _ -> ()
1037 | Textentry _
1038 | View -> ()
1039 end;
1040 preload layout;
1042 state.ghyll <- noghyll;
1043 if conf.updatecurs
1044 then (
1045 let mx, my = state.mpos in
1046 updateunder mx my;
1050 let conttiling pageno opaque =
1051 tilepage pageno opaque
1052 (if conf.preload then preloadlayout state.y else state.layout)
1055 let gotoy_and_clear_text y =
1056 if not conf.verbose then state.text <- E.s;
1057 gotoy y;
1060 let getanchory (n, top, dtop) =
1061 let y, h = getpageyh n in
1062 if conf.presentation
1063 then
1064 let ips = calcips h in
1065 y + truncate (top*.float h -. dtop*.float ips) + ips;
1066 else
1067 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1070 let gotoanchor anchor =
1071 gotoy (getanchory anchor);
1074 let addnav () =
1075 cbput state.hists.nav (getanchor ());
1078 let getnav dir =
1079 let anchor = cbgetc state.hists.nav dir in
1080 getanchory anchor;
1083 let gotoghyll1 single y =
1084 let scroll f n a b =
1085 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1086 let snake f a b =
1087 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1088 if f < a
1089 then s (float f /. float a)
1090 else (
1091 if f > b
1092 then 1.0 -. s ((float (f-b) /. float (n-b)))
1093 else 1.0
1096 snake f a b
1097 and summa n a b =
1098 let ins = float a *. 0.5
1099 and outs = float (n-b) *. 0.5 in
1100 let ones = b - a in
1101 ins +. outs +. float ones
1103 let rec set nab y sy =
1104 let (_N, _A, _B), y =
1105 if single
1106 then
1107 let scl = if y > sy then 2 else -2 in
1108 let _N, _, _ = nab in
1109 (_N,0,_N), y+conf.scrollstep*scl
1110 else nab,y in
1111 let sum = summa _N _A _B in
1112 let dy = float (y - sy) in
1113 state.ghyll <- (
1114 let rec gf n y1 o =
1115 if n >= _N
1116 then state.ghyll <- noghyll
1117 else
1118 let go n =
1119 let s = scroll n _N _A _B in
1120 let y1 = y1 +. ((s *. dy) /. sum) in
1121 gotoy_and_clear_text (truncate y1);
1122 state.ghyll <- gf (n+1) y1;
1124 match o with
1125 | None -> go n
1126 | Some y' when single -> set nab y' state.y
1127 | Some y' -> set (_N/2, 1, 1) y' state.y
1129 gf 0 (float state.y)
1132 match conf.ghyllscroll with
1133 | Some nab when not conf.presentation ->
1134 if state.ghyll == noghyll
1135 then set nab y state.y
1136 else state.ghyll (Some y)
1137 | _ ->
1138 gotoy_and_clear_text y
1141 let gotoghyll = gotoghyll1 false;;
1143 let gotopage n top =
1144 let y, h = getpageyh n in
1145 let y = y + (truncate (top *. float h)) in
1146 gotoghyll y
1149 let gotopage1 n top =
1150 let y = getpagey n in
1151 let y = y + top in
1152 gotoghyll y
1155 let invalidate s f =
1156 state.layout <- [];
1157 state.pdims <- [];
1158 state.rects <- [];
1159 state.rects1 <- [];
1160 match state.geomcmds with
1161 | ps, [] when emptystr ps ->
1162 f ();
1163 state.geomcmds <- s, [];
1165 | ps, [] ->
1166 state.geomcmds <- ps, [s, f];
1168 | ps, (s', _) :: rest when s' = s ->
1169 state.geomcmds <- ps, ((s, f) :: rest);
1171 | ps, cmds ->
1172 state.geomcmds <- ps, ((s, f) :: cmds);
1175 let flushpages () =
1176 Hashtbl.iter (fun _ opaque ->
1177 wcmd "freepage %s" (~> opaque);
1178 ) state.pagemap;
1179 Hashtbl.clear state.pagemap;
1182 let flushtiles () =
1183 if not (Queue.is_empty state.tilelru)
1184 then (
1185 Queue.iter (fun (k, p, s) ->
1186 wcmd "freetile %s" (~> p);
1187 state.memused <- state.memused - s;
1188 Hashtbl.remove state.tilemap k;
1189 ) state.tilelru;
1190 state.uioh#infochanged Memused;
1191 Queue.clear state.tilelru;
1193 load state.layout;
1196 let stateh h =
1197 let h = truncate (float h*.conf.zoom) in
1198 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
1199 h - d
1202 let opendoc path password =
1203 state.path <- path;
1204 state.password <- password;
1205 state.gen <- state.gen + 1;
1206 state.docinfo <- [];
1207 state.outlines <- [||];
1209 flushpages ();
1210 setaalevel conf.aalevel;
1211 let titlepath =
1212 if emptystr state.origin
1213 then path
1214 else state.origin
1216 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
1217 wcmd "open %d %d %s\000%s\000" (btod !wtmode) (btod !cxack) path password;
1218 invalidate "reqlayout"
1219 (fun () ->
1220 wcmd "reqlayout %d %d %d %s\000"
1221 conf.angle (FMTE.to_int conf.fitmodel)
1222 (stateh state.winh) state.nameddest
1226 let reload () =
1227 state.anchor <- getanchor ();
1228 opendoc state.path state.password;
1231 let scalecolor c =
1232 let c = c *. conf.colorscale in
1233 (c, c, c);
1236 let scalecolor2 (r, g, b) =
1237 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1240 let docolumns columns =
1241 let wadj = wadjsb () in
1242 match columns with
1243 | Csingle _ ->
1244 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1245 let wadj = wadjsb () in
1246 let rec loop pageno pdimno pdim y ph pdims =
1247 if pageno = state.pagecount
1248 then ()
1249 else
1250 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1251 match pdims with
1252 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1253 pdimno+1, pdim, rest
1254 | _ ->
1255 pdimno, pdim, pdims
1257 let x = max 0 (((wadj + state.winw - w) / 2) - xoff) in
1258 let y = y +
1259 (if conf.presentation
1260 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1261 else (if pageno = 0 then 0 else conf.interpagespace)
1264 a.(pageno) <- (pdimno, x, y, pdim);
1265 loop (pageno+1) pdimno pdim (y + h) h pdims
1267 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1268 conf.columns <- Csingle a;
1270 | Cmulti ((columns, coverA, coverB), _) ->
1271 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1272 let rec loop pageno pdimno pdim x y rowh pdims =
1273 let rec fixrow m = if m = pageno then () else
1274 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1275 if h < rowh
1276 then (
1277 let y = y + (rowh - h) / 2 in
1278 a.(m) <- (pdimno, x, y, pdim);
1280 fixrow (m+1)
1282 if pageno = state.pagecount
1283 then fixrow (((pageno - 1) / columns) * columns)
1284 else
1285 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1286 match pdims with
1287 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1288 pdimno+1, pdim, rest
1289 | _ ->
1290 pdimno, pdim, pdims
1292 let x, y, rowh' =
1293 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1294 then (
1295 let x = (wadj + state.winw - w) / 2 in
1296 let ips =
1297 if conf.presentation then calcips h else conf.interpagespace in
1298 x, y + ips + rowh, h
1300 else (
1301 if (pageno - coverA) mod columns = 0
1302 then (
1303 let x = max 0 (wadj + state.winw - state.w) / 2 in
1304 let y =
1305 if conf.presentation
1306 then
1307 let ips = calcips h in
1308 y + (if pageno = 0 then 0 else calcips rowh + ips)
1309 else
1310 y + (if pageno = 0 then 0 else conf.interpagespace)
1312 x, y + rowh, h
1314 else x, y, max rowh h
1317 let y =
1318 if pageno > 1 && (pageno - coverA) mod columns = 0
1319 then (
1320 let y =
1321 if pageno = columns && conf.presentation
1322 then (
1323 let ips = calcips rowh in
1324 for i = 0 to pred columns
1326 let (pdimno, x, y, pdim) = a.(i) in
1327 a.(i) <- (pdimno, x, y+ips, pdim)
1328 done;
1329 y+ips;
1331 else y
1333 fixrow (pageno - columns);
1336 else y
1338 a.(pageno) <- (pdimno, x, y, pdim);
1339 let x = x + w + xoff*2 + conf.interpagespace in
1340 loop (pageno+1) pdimno pdim x y rowh' pdims
1342 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1343 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1345 | Csplit (c, _) ->
1346 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1347 let rec loop pageno pdimno pdim y pdims =
1348 if pageno = state.pagecount
1349 then ()
1350 else
1351 let pdimno, ((_, w, h, _) as pdim), pdims =
1352 match pdims with
1353 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1354 pdimno+1, pdim, rest
1355 | _ ->
1356 pdimno, pdim, pdims
1358 let cw = w / c in
1359 let rec loop1 n x y =
1360 if n = c then y else (
1361 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1362 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1365 let y = loop1 0 0 y in
1366 loop (pageno+1) pdimno pdim y pdims
1368 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1369 conf.columns <- Csplit (c, a);
1372 let represent () =
1373 docolumns conf.columns;
1374 state.maxy <- calcheight ();
1375 if state.reprf == noreprf
1376 then (
1377 match state.mode with
1378 | Birdseye (_, _, pageno, _, _) ->
1379 let y, h = getpageyh pageno in
1380 let top = (state.winh - h) / 2 in
1381 gotoy (max 0 (y - top))
1382 | Textentry _
1383 | View
1384 | LinkNav _ -> gotoanchor state.anchor
1386 else (
1387 state.reprf ();
1388 state.reprf <- noreprf;
1392 let reshape w h =
1393 GlDraw.viewport ~x:0 ~y:0 ~w:w ~h:h;
1394 let firsttime = state.geomcmds == firstgeomcmds in
1395 if not firsttime && nogeomcmds state.geomcmds
1396 then state.anchor <- getanchor ();
1398 state.winw <- w;
1399 let w = wadjsb () + (truncate (float w *. conf.zoom)) in
1400 let w = max w 2 in
1401 state.winh <- h;
1402 setfontsize fstate.fontsize;
1403 GlMat.mode `modelview;
1404 GlMat.load_identity ();
1406 GlMat.mode `projection;
1407 GlMat.load_identity ();
1408 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1409 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1410 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
1412 let relx =
1413 if conf.zoom <= 1.0
1414 then 0.0
1415 else float state.x /. float state.w
1417 invalidate "geometry"
1418 (fun () ->
1419 state.w <- w;
1420 if not firsttime
1421 then state.x <- truncate (relx *. float w);
1422 let w =
1423 match conf.columns with
1424 | Csingle _ -> w
1425 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1426 | Csplit (c, _) -> w * c
1428 wcmd "geometry %d %d %d"
1429 w (stateh h) (FMTE.to_int conf.fitmodel)
1433 let enttext () =
1434 let len = String.length state.text in
1435 let x0 = xadjsb () in
1436 let drawstring s =
1437 let hscrollh =
1438 match state.mode with
1439 | Textentry _ | View | LinkNav _ ->
1440 let h, _, _ = state.uioh#scrollpw in
1442 | Birdseye _ -> 0
1444 let rect x w =
1445 filledrect x (float (state.winh - (fstate.fontsize + 4) - hscrollh))
1446 (x+.w) (float (state.winh - hscrollh))
1449 let w = float (wadjsb () + state.winw - 1) in
1450 if state.progress >= 0.0 && state.progress < 1.0
1451 then (
1452 GlDraw.color (0.3, 0.3, 0.3);
1453 let w1 = w *. state.progress in
1454 rect (float x0) w1;
1455 GlDraw.color (0.0, 0.0, 0.0);
1456 rect (float x0+.w1) (float x0+.w-.w1)
1458 else (
1459 GlDraw.color (0.0, 0.0, 0.0);
1460 rect (float x0) w;
1463 GlDraw.color (1.0, 1.0, 1.0);
1464 drawstring fstate.fontsize
1465 (if conf.leftscroll then x0 + 2 else x0 + if len > 0 then 8 else 2)
1466 (state.winh - hscrollh - 5) s;
1468 let s =
1469 match state.mode with
1470 | Textentry ((prefix, text, _, _, _, _), _) ->
1471 let s =
1472 if len > 0
1473 then
1474 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1475 else
1476 Printf.sprintf "%s%s_" prefix text
1480 | Birdseye _
1481 | View
1482 | LinkNav _ -> state.text
1484 let s =
1485 if state.newerrmsgs
1486 then (
1487 if not (istextentry state.mode) && state.uioh#eformsgs
1488 then
1489 let s1 = "(press 'e' to review error messasges)" in
1490 if nonemptystr s then s ^ " " ^ s1 else s1
1491 else s
1493 else s
1495 if nonemptystr s
1496 then drawstring s
1499 let gctiles () =
1500 let len = Queue.length state.tilelru in
1501 let layout = lazy (
1502 match state.throttle with
1503 | None ->
1504 if conf.preload
1505 then preloadlayout state.y
1506 else state.layout
1507 | Some (layout, _, _) ->
1508 layout
1509 ) in
1510 let rec loop qpos =
1511 if state.memused <= conf.memlimit
1512 then ()
1513 else (
1514 if qpos < len
1515 then
1516 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1517 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1518 let (_, pw, ph, _) = getpagedim n in
1520 gen = state.gen
1521 && colorspace = conf.colorspace
1522 && angle = conf.angle
1523 && pagew = pw
1524 && pageh = ph
1525 && (
1526 let x = col*conf.tilew
1527 and y = row*conf.tileh in
1528 tilevisible (Lazy.force_val layout) n x y
1530 then Queue.push lruitem state.tilelru
1531 else (
1532 freepbo p;
1533 wcmd "freetile %s" (~> p);
1534 state.memused <- state.memused - s;
1535 state.uioh#infochanged Memused;
1536 Hashtbl.remove state.tilemap k;
1538 loop (qpos+1)
1541 loop 0
1544 let onpagerect pageno f =
1545 let b =
1546 match conf.columns with
1547 | Cmulti (_, b) -> b
1548 | Csingle b -> b
1549 | Csplit (_, b) -> b
1551 if pageno >= 0 && pageno < Array.length b
1552 then
1553 let (_, _, _, (w, h, _, _)) = b.(pageno) in
1554 f w h
1557 let gotopagexy1 pageno x y =
1558 let _,w1,h1,leftx = getpagedim pageno in
1559 let top = y /. (float h1) in
1560 let left = x /. (float w1) in
1561 let py, w, h = getpageywh pageno in
1562 let wh = state.winh - hscrollh () in
1563 let x = left *. (float w) in
1564 let x = leftx + state.x + truncate x in
1565 let wadj = wadjsb () in
1566 let sx =
1567 if x < 0 || x >= wadj + state.winw
1568 then state.x - x
1569 else state.x
1571 let pdy = truncate (top *. float h) in
1572 let y' = py + pdy in
1573 let dy = y' - state.y in
1574 let sy =
1575 if x != state.x || not (dy > 0 && dy < wh)
1576 then (
1577 if conf.presentation
1578 then
1579 if abs (py - y') > wh
1580 then y'
1581 else py
1582 else y';
1584 else state.y
1586 if state.x != sx || state.y != sy
1587 then (
1588 let x, y =
1589 if !wtmode
1590 then (
1591 let ww = wadj + state.winw in
1592 let qx = sx / ww
1593 and qy = pdy / wh in
1594 let x = qx * ww
1595 and y = py + qy * wh in
1596 let x = if -x + ww > w1 then -(w1-ww) else x
1597 and y' = if y + wh > state.maxy then state.maxy - wh else y in
1598 let y =
1599 if conf.presentation
1600 then
1601 if abs (py - y') > wh
1602 then y'
1603 else py
1604 else y';
1606 (x, y)
1608 else (sx, sy)
1610 state.x <- x;
1611 gotoy_and_clear_text y;
1613 else gotoy_and_clear_text state.y;
1616 let gotopagexy pageno x y =
1617 match state.mode with
1618 | Birdseye _ -> gotopage pageno 0.0
1619 | Textentry _
1620 | View
1621 | LinkNav _ -> gotopagexy1 pageno x y
1624 let getpassword () =
1625 let passcmd = getenvwithdef "LLPP_ASKPASS" conf.passcmd in
1626 if emptystr passcmd
1627 then E.s
1628 else getcmdoutput
1629 (fun s ->
1630 showtext '!' @@ "error getting password: " ^ s;
1631 dolog "%s" s) passcmd;
1634 let act cmds =
1635 (* dolog "%S" cmds; *)
1636 let cl = splitatspace cmds in
1637 let scan s fmt f =
1638 try Scanf.sscanf s fmt f
1639 with exn ->
1640 dolog "error processing '%S': %s" cmds (exntos exn);
1641 exit 1
1643 let addoutline outline =
1644 match state.currently with
1645 | Outlining outlines ->
1646 state.currently <- Outlining (outline :: outlines)
1647 | Idle -> state.currently <- Outlining [outline]
1648 | Loading _
1649 | Tiling _ ->
1650 dolog "invalid outlining state";
1651 logcurrently state.currently
1653 match cl with
1654 | "clear" :: [] ->
1655 state.uioh#infochanged Pdim;
1656 state.pdims <- [];
1658 | "clearrects" :: [] ->
1659 state.rects <- state.rects1;
1660 G.postRedisplay "clearrects";
1662 | "continue" :: args :: [] ->
1663 let n = scan args "%u" (fun n -> n) in
1664 state.pagecount <- n;
1665 begin match state.currently with
1666 | Outlining l ->
1667 state.currently <- Idle;
1668 state.outlines <- Array.of_list (List.rev l)
1669 | Idle
1670 | Loading _
1671 | Tiling _ -> ()
1672 end;
1674 let cur, cmds = state.geomcmds in
1675 if emptystr cur
1676 then failwith "umpossible";
1678 begin match List.rev cmds with
1679 | [] ->
1680 state.geomcmds <- E.s, [];
1681 state.throttle <- None;
1682 represent ();
1683 | (s, f) :: rest ->
1684 f ();
1685 state.geomcmds <- s, List.rev rest;
1686 end;
1687 if conf.maxwait = None && not !wtmode
1688 then G.postRedisplay "continue";
1690 | "msg" :: args :: [] ->
1691 showtext ' ' args
1693 | "vmsg" :: args :: [] ->
1694 if conf.verbose
1695 then showtext ' ' args
1697 | "emsg" :: args :: [] ->
1698 Buffer.add_string state.errmsgs args;
1699 state.newerrmsgs <- true;
1700 G.postRedisplay "error message"
1702 | "progress" :: args :: [] ->
1703 let progress, text =
1704 scan args "%f %n"
1705 (fun f pos ->
1706 f, String.sub args pos (String.length args - pos))
1708 state.text <- text;
1709 state.progress <- progress;
1710 G.postRedisplay "progress"
1712 | "firstmatch" :: args :: [] ->
1713 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1714 scan args "%u %d %f %f %f %f %f %f %f %f"
1715 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1716 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1718 let xoff = float (xadjsb ()) in
1719 let x0 = x0 +. xoff
1720 and x1 = x1 +. xoff
1721 and x2 = x2 +. xoff
1722 and x3 = x3 +. xoff in
1723 let y = (getpagey pageno) + truncate y0 in
1724 addnav ();
1725 gotoy y;
1726 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
1728 | "match" :: args :: [] ->
1729 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1730 scan args "%u %d %f %f %f %f %f %f %f %f"
1731 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1732 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1734 let xoff = float (xadjsb ()) in
1735 let x0 = x0 +. xoff
1736 and x1 = x1 +. xoff
1737 and x2 = x2 +. xoff
1738 and x3 = x3 +. xoff in
1739 state.rects1 <-
1740 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1742 | "page" :: args :: [] ->
1743 let pageopaques, t = scan args "%s %f" (fun p t -> p, t) in
1744 let pageopaque = ~< pageopaques in
1745 begin match state.currently with
1746 | Loading (l, gen) ->
1747 vlog "page %d took %f sec" l.pageno t;
1748 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1749 begin match state.throttle with
1750 | None ->
1751 let preloadedpages =
1752 if conf.preload
1753 then preloadlayout state.y
1754 else state.layout
1756 let evict () =
1757 let set =
1758 List.fold_left (fun s l -> IntSet.add l.pageno s)
1759 IntSet.empty preloadedpages
1761 let evictedpages =
1762 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1763 if not (IntSet.mem pageno set)
1764 then (
1765 wcmd "freepage %s" (~> opaque);
1766 key :: accu
1768 else accu
1769 ) state.pagemap []
1771 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1773 evict ();
1774 state.currently <- Idle;
1775 if gen = state.gen
1776 then (
1777 tilepage l.pageno pageopaque state.layout;
1778 load state.layout;
1779 load preloadedpages;
1780 let visible = pagevisible state.layout l.pageno in
1781 if visible
1782 then (
1783 match state.mode with
1784 | LinkNav (Ltnotready (pageno, dir)) ->
1785 if pageno = l.pageno
1786 then (
1787 let link =
1788 let ld =
1789 if dir = 0
1790 then LDfirstvisible (l.pagex, l.pagey, dir)
1791 else (
1792 if dir > 0 then LDfirst else LDlast
1795 findlink pageopaque ld
1797 match link with
1798 | Lnotfound -> ()
1799 | Lfound n ->
1800 showlinktype (getlink pageopaque n);
1801 state.mode <- LinkNav (Ltexact (l.pageno, n))
1803 | LinkNav (Ltgendir _)
1804 | LinkNav (Ltexact _)
1805 | View
1806 | Birdseye _
1807 | Textentry _ -> ()
1810 if visible && layoutready state.layout
1811 then (
1812 G.postRedisplay "page";
1816 | Some (layout, _, _) ->
1817 state.currently <- Idle;
1818 tilepage l.pageno pageopaque layout;
1819 load state.layout
1820 end;
1822 | Idle
1823 | Tiling _
1824 | Outlining _ ->
1825 dolog "Inconsistent loading state";
1826 logcurrently state.currently;
1827 exit 1
1830 | "tile" :: args :: [] ->
1831 let (x, y, opaques, size, t) =
1832 scan args "%u %u %s %u %f"
1833 (fun x y p size t -> (x, y, p, size, t))
1835 let opaque = ~< opaques in
1836 begin match state.currently with
1837 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1838 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1840 unmappbo opaque;
1841 if tilew != conf.tilew || tileh != conf.tileh
1842 then (
1843 wcmd "freetile %s" (~> opaque);
1844 state.currently <- Idle;
1845 load state.layout;
1847 else (
1848 puttileopaque l col row gen cs angle opaque size t;
1849 state.memused <- state.memused + size;
1850 state.uioh#infochanged Memused;
1851 gctiles ();
1852 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1853 opaque, size) state.tilelru;
1855 let layout =
1856 match state.throttle with
1857 | None -> state.layout
1858 | Some (layout, _, _) -> layout
1861 state.currently <- Idle;
1862 if gen = state.gen
1863 && conf.colorspace = cs
1864 && conf.angle = angle
1865 && tilevisible layout l.pageno x y
1866 then conttiling l.pageno pageopaque;
1868 begin match state.throttle with
1869 | None ->
1870 preload state.layout;
1871 if gen = state.gen
1872 && conf.colorspace = cs
1873 && conf.angle = angle
1874 && tilevisible state.layout l.pageno x y
1875 && (not !wtmode || layoutready state.layout)
1876 then G.postRedisplay "tile nothrottle";
1878 | Some (layout, y, _) ->
1879 let ready = layoutready layout in
1880 if ready
1881 then (
1882 state.y <- y;
1883 state.layout <- layout;
1884 state.throttle <- None;
1885 G.postRedisplay "throttle";
1887 else load layout;
1888 end;
1891 | Idle
1892 | Loading _
1893 | Outlining _ ->
1894 dolog "Inconsistent tiling state";
1895 logcurrently state.currently;
1896 exit 1
1899 | "pdim" :: args :: [] ->
1900 let (n, w, h, _) as pdim =
1901 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1903 let pdim =
1904 match conf.fitmodel with
1905 | FitWidth -> pdim
1906 | FitPage | FitProportional ->
1907 match conf.columns with
1908 | Csplit _ -> (n, w, h, 0)
1909 | Csingle _ | Cmulti _ -> pdim
1911 state.uioh#infochanged Pdim;
1912 state.pdims <- pdim :: state.pdims
1914 | "o" :: args :: [] ->
1915 let (l, n, t, h, pos) =
1916 scan args "%u %u %d %u %n"
1917 (fun l n t h pos -> l, n, t, h, pos)
1919 let s = String.sub args pos (String.length args - pos) in
1920 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
1922 | "ou" :: args :: [] ->
1923 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
1924 let s = String.sub args pos len in
1925 let pos2 = pos + len + 1 in
1926 let uri = String.sub args pos2 (String.length args - pos2) in
1927 addoutline (s, l, Ouri uri)
1929 | "on" :: args :: [] ->
1930 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
1931 let s = String.sub args pos (String.length args - pos) in
1932 addoutline (s, l, Onone)
1934 | "a" :: args :: [] ->
1935 let (n, l, t) =
1936 scan args "%u %d %d" (fun n l t -> n, l, t)
1938 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
1940 | "info" :: args :: [] ->
1941 let pos = nindex args '\t' in
1942 if pos >= 0 && String.sub args 0 pos = "Title"
1943 then (
1944 let s = String.sub args (pos+1) @@ String.length args - pos - 1 in
1945 conf.title <- s;
1946 Wsi.settitle s;
1948 state.docinfo <- (1, args) :: state.docinfo
1950 | "infoend" :: [] ->
1951 state.uioh#infochanged Docinfo;
1952 state.docinfo <- List.rev state.docinfo
1954 | "pass" :: l ->
1955 if l = "fail" :: []
1956 then Wsi.settitle "Wrong password";
1957 let password = getpassword () in
1958 if password == E.s
1959 then error "document is password protected"
1960 else opendoc state.path password
1962 | _ ->
1963 error "unknown cmd `%S'" cmds
1966 let onhist cb =
1967 let rc = cb.rc in
1968 let action = function
1969 | HCprev -> cbget cb ~-1
1970 | HCnext -> cbget cb 1
1971 | HCfirst -> cbget cb ~-(cb.rc)
1972 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1973 and cancel () = cb.rc <- rc
1974 in (action, cancel)
1977 let search pattern forward =
1978 match conf.columns with
1979 | Csplit _ ->
1980 showtext '!' "searching does not work properly in split columns mode"
1981 | Csingle _
1982 | Cmulti _ ->
1983 if nonemptystr pattern
1984 then
1985 let pn, py =
1986 match state.layout with
1987 | [] -> 0, 0
1988 | l :: _ ->
1989 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1991 wcmd "search %d %d %d %d,%s\000"
1992 (btod conf.icase) pn py (btod forward) pattern;
1995 let intentry text key =
1996 let c =
1997 if key >= 32 && key < 127
1998 then Char.chr key
1999 else '\000'
2001 match c with
2002 | '0' .. '9' ->
2003 let text = addchar text c in
2004 TEcont text
2006 | _ ->
2007 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2008 TEcont text
2011 let linknentry text key =
2012 let c =
2013 if key >= 32 && key < 127
2014 then Char.chr key
2015 else '\000'
2017 match c with
2018 | 'a' .. 'z' ->
2019 let text = addchar text c in
2020 TEcont text
2022 | _ ->
2023 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2024 TEcont text
2027 let linkndone f s =
2028 if nonemptystr s
2029 then (
2030 let n =
2031 let l = String.length s in
2032 let rec loop pos n = if pos = l then n else
2033 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2034 loop (pos+1) (n*26 + m)
2035 in loop 0 0
2037 let rec loop n = function
2038 | [] -> ()
2039 | l :: rest ->
2040 match getopaque l.pageno with
2041 | None -> loop n rest
2042 | Some opaque ->
2043 let m = getlinkcount opaque in
2044 if n < m
2045 then (
2046 let under = getlink opaque n in
2047 f under
2049 else loop (n-m) rest
2051 loop n state.layout;
2055 let textentry text key =
2056 if key land 0xff00 = 0xff00
2057 then TEcont text
2058 else TEcont (text ^ toutf8 key)
2061 let reqlayout angle fitmodel =
2062 match state.throttle with
2063 | None ->
2064 if nogeomcmds state.geomcmds
2065 then state.anchor <- getanchor ();
2066 conf.angle <- angle mod 360;
2067 if conf.angle != 0
2068 then (
2069 match state.mode with
2070 | LinkNav _ -> state.mode <- View
2071 | Birdseye _
2072 | Textentry _
2073 | View -> ()
2075 conf.fitmodel <- fitmodel;
2076 invalidate "reqlayout"
2077 (fun () ->
2078 wcmd "reqlayout %d %d %d"
2079 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
2081 | _ -> ()
2084 let settrim trimmargins trimfuzz =
2085 if nogeomcmds state.geomcmds
2086 then state.anchor <- getanchor ();
2087 conf.trimmargins <- trimmargins;
2088 conf.trimfuzz <- trimfuzz;
2089 let x0, y0, x1, y1 = trimfuzz in
2090 invalidate "settrim"
2091 (fun () ->
2092 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2093 flushpages ();
2096 let setzoom zoom =
2097 match state.throttle with
2098 | None ->
2099 let zoom = max 0.0001 zoom in
2100 if zoom <> conf.zoom
2101 then (
2102 state.prevzoom <- (conf.zoom, state.x);
2103 conf.zoom <- zoom;
2104 reshape state.winw state.winh;
2105 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2108 | Some (layout, y, started) ->
2109 let time =
2110 match conf.maxwait with
2111 | None -> 0.0
2112 | Some t -> t
2114 let dt = now () -. started in
2115 if dt > time
2116 then (
2117 state.y <- y;
2118 load layout;
2122 let setcolumns mode columns coverA coverB =
2123 state.prevcolumns <- Some (conf.columns, conf.zoom);
2124 if columns < 0
2125 then (
2126 if isbirdseye mode
2127 then showtext '!' "split mode doesn't work in bird's eye"
2128 else (
2129 conf.columns <- Csplit (-columns, E.a);
2130 state.x <- 0;
2131 conf.zoom <- 1.0;
2134 else (
2135 if columns < 2
2136 then (
2137 conf.columns <- Csingle E.a;
2138 state.x <- 0;
2139 setzoom 1.0;
2141 else (
2142 conf.columns <- Cmulti ((columns, coverA, coverB), E.a);
2143 conf.zoom <- 1.0;
2146 reshape state.winw state.winh;
2149 let resetmstate () =
2150 state.mstate <- Mnone;
2151 Wsi.setcursor Wsi.CURSOR_INHERIT;
2154 let enterbirdseye () =
2155 let zoom = float conf.thumbw /. float state.winw in
2156 let birdseyepageno =
2157 let cy = state.winh / 2 in
2158 let fold = function
2159 | [] -> 0
2160 | l :: rest ->
2161 let rec fold best = function
2162 | [] -> best.pageno
2163 | l :: rest ->
2164 let d = cy - (l.pagedispy + l.pagevh/2)
2165 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2166 if abs d < abs dbest
2167 then fold l rest
2168 else best.pageno
2169 in fold l rest
2171 fold state.layout
2173 state.mode <- Birdseye (
2174 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2176 resetmstate ();
2177 conf.zoom <- zoom;
2178 conf.presentation <- false;
2179 conf.interpagespace <- 10;
2180 conf.hlinks <- false;
2181 conf.fitmodel <- FitPage;
2182 state.x <- 0;
2183 conf.maxwait <- None;
2184 conf.columns <- (
2185 match conf.beyecolumns with
2186 | Some c ->
2187 conf.zoom <- 1.0;
2188 Cmulti ((c, 0, 0), E.a)
2189 | None -> Csingle E.a
2191 if conf.verbose
2192 then
2193 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2194 (100.0*.zoom)
2195 else
2196 state.text <- E.s
2198 reshape state.winw state.winh;
2201 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2202 state.mode <- View;
2203 conf.zoom <- c.zoom;
2204 conf.presentation <- c.presentation;
2205 conf.interpagespace <- c.interpagespace;
2206 conf.maxwait <- c.maxwait;
2207 conf.hlinks <- c.hlinks;
2208 conf.fitmodel <- c.fitmodel;
2209 conf.beyecolumns <- (
2210 match conf.columns with
2211 | Cmulti ((c, _, _), _) -> Some c
2212 | Csingle _ -> None
2213 | Csplit _ -> failwith "leaving bird's eye split mode"
2215 conf.columns <- (
2216 match c.columns with
2217 | Cmulti (c, _) -> Cmulti (c, E.a)
2218 | Csingle _ -> Csingle E.a
2219 | Csplit (c, _) -> Csplit (c, E.a)
2221 if conf.verbose
2222 then
2223 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2224 (100.0*.conf.zoom)
2226 reshape state.winw state.winh;
2227 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2228 state.x <- leftx;
2231 let togglebirdseye () =
2232 match state.mode with
2233 | Birdseye vals -> leavebirdseye vals true
2234 | View -> enterbirdseye ()
2235 | Textentry _
2236 | LinkNav _ -> ()
2239 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2240 let pageno = max 0 (pageno - incr) in
2241 let rec loop = function
2242 | [] -> gotopage1 pageno 0
2243 | l :: _ when l.pageno = pageno ->
2244 if l.pagedispy >= 0 && l.pagey = 0
2245 then G.postRedisplay "upbirdseye"
2246 else gotopage1 pageno 0
2247 | _ :: rest -> loop rest
2249 loop state.layout;
2250 state.text <- E.s;
2251 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2254 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2255 let pageno = min (state.pagecount - 1) (pageno + incr) in
2256 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2257 let rec loop = function
2258 | [] ->
2259 let y, h = getpageyh pageno in
2260 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2261 gotoy (clamp dy)
2262 | l :: _ when l.pageno = pageno ->
2263 if l.pagevh != l.pageh
2264 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2265 else G.postRedisplay "downbirdseye"
2266 | _ :: rest -> loop rest
2268 loop state.layout;
2269 state.text <- E.s;
2272 let optentry mode _ key =
2273 let btos b = if b then "on" else "off" in
2274 if key >= 32 && key < 127
2275 then
2276 let c = Char.chr key in
2277 match c with
2278 | 's' ->
2279 let ondone s =
2280 try conf.scrollstep <- int_of_string s with exc ->
2281 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2283 TEswitch ("scroll step: ", E.s, None, intentry, ondone, true)
2285 | 'A' ->
2286 let ondone s =
2288 conf.autoscrollstep <- boundastep state.winh (int_of_string s);
2289 if state.autoscroll <> None
2290 then state.autoscroll <- Some conf.autoscrollstep
2291 with exc ->
2292 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2294 TEswitch ("auto scroll step: ", E.s, None, intentry, ondone, true)
2296 | 'C' ->
2297 let ondone s =
2299 let n, a, b = multicolumns_of_string s in
2300 setcolumns mode n a b;
2301 with exc ->
2302 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
2304 TEswitch ("columns: ", E.s, None, textentry, ondone, true)
2306 | 'Z' ->
2307 let ondone s =
2309 let zoom = float (int_of_string s) /. 100.0 in
2310 setzoom zoom
2311 with exc ->
2312 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2314 TEswitch ("zoom: ", E.s, None, intentry, ondone, true)
2316 | 't' ->
2317 let ondone s =
2319 conf.thumbw <- bound (int_of_string s) 2 4096;
2320 state.text <-
2321 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2322 begin match mode with
2323 | Birdseye beye ->
2324 leavebirdseye beye false;
2325 enterbirdseye ();
2326 | Textentry _
2327 | View
2328 | LinkNav _ -> ();
2330 with exc ->
2331 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2333 TEswitch ("thumbnail width: ", E.s, None, intentry, ondone, true)
2335 | 'R' ->
2336 let ondone s =
2337 match try
2338 Some (int_of_string s)
2339 with exc ->
2340 state.text <- Printf.sprintf "bad integer `%s': %s"
2341 s (exntos exc);
2342 None
2343 with
2344 | Some angle -> reqlayout angle conf.fitmodel
2345 | None -> ()
2347 TEswitch ("rotation: ", E.s, None, intentry, ondone, true)
2349 | 'i' ->
2350 conf.icase <- not conf.icase;
2351 TEdone ("case insensitive search " ^ (btos conf.icase))
2353 | 'p' ->
2354 conf.preload <- not conf.preload;
2355 gotoy state.y;
2356 TEdone ("preload " ^ (btos conf.preload))
2358 | 'v' ->
2359 conf.verbose <- not conf.verbose;
2360 TEdone ("verbose " ^ (btos conf.verbose))
2362 | 'd' ->
2363 conf.debug <- not conf.debug;
2364 TEdone ("debug " ^ (btos conf.debug))
2366 | 'h' ->
2367 conf.maxhfit <- not conf.maxhfit;
2368 state.maxy <- calcheight ();
2369 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2371 | 'c' ->
2372 conf.crophack <- not conf.crophack;
2373 TEdone ("crophack " ^ btos conf.crophack)
2375 | 'a' ->
2376 let s =
2377 match conf.maxwait with
2378 | None ->
2379 conf.maxwait <- Some infinity;
2380 "always wait for page to complete"
2381 | Some _ ->
2382 conf.maxwait <- None;
2383 "show placeholder if page is not ready"
2385 TEdone s
2387 | 'f' ->
2388 conf.underinfo <- not conf.underinfo;
2389 TEdone ("underinfo " ^ btos conf.underinfo)
2391 | 'P' ->
2392 conf.savebmarks <- not conf.savebmarks;
2393 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2395 | 'S' ->
2396 let ondone s =
2398 let pageno, py =
2399 match state.layout with
2400 | [] -> 0, 0
2401 | l :: _ ->
2402 l.pageno, l.pagey
2404 conf.interpagespace <- int_of_string s;
2405 docolumns conf.columns;
2406 state.maxy <- calcheight ();
2407 let y = getpagey pageno in
2408 gotoy (y + py)
2409 with exc ->
2410 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2412 TEswitch ("vertical margin: ", E.s, None, intentry, ondone, true)
2414 | 'l' ->
2415 let fm =
2416 match conf.fitmodel with
2417 | FitProportional -> FitWidth
2418 | FitWidth | FitPage -> FitProportional
2420 reqlayout conf.angle fm;
2421 TEdone ("proportional display " ^ btos (fm == FitProportional))
2423 | 'T' ->
2424 settrim (not conf.trimmargins) conf.trimfuzz;
2425 TEdone ("trim margins " ^ btos conf.trimmargins)
2427 | 'I' ->
2428 conf.invert <- not conf.invert;
2429 TEdone ("invert colors " ^ btos conf.invert)
2431 | 'x' ->
2432 let ondone s =
2433 cbput state.hists.sel s;
2434 conf.selcmd <- s;
2436 TEswitch ("selection command: ", E.s, Some (onhist state.hists.sel),
2437 textentry, ondone, true)
2439 | 'M' ->
2440 if conf.pax == None
2441 then conf.pax <- Some (ref (0.0, 0, 0))
2442 else conf.pax <- None;
2443 TEdone ("PAX " ^ btos (conf.pax != None))
2445 | _ ->
2446 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2447 TEstop
2448 else
2449 TEcont state.text
2452 class type lvsource = object
2453 method getitemcount : int
2454 method getitem : int -> (string * int)
2455 method hasaction : int -> bool
2456 method exit :
2457 uioh:uioh ->
2458 cancel:bool ->
2459 active:int ->
2460 first:int ->
2461 pan:int ->
2462 uioh option
2463 method getactive : int
2464 method getfirst : int
2465 method getpan : int
2466 method getminfo : (int * int) array
2467 end;;
2469 class virtual lvsourcebase = object
2470 val mutable m_active = 0
2471 val mutable m_first = 0
2472 val mutable m_pan = 0
2473 method getactive = m_active
2474 method getfirst = m_first
2475 method getpan = m_pan
2476 method getminfo : (int * int) array = E.a
2477 end;;
2479 let textentrykeyboard
2480 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2481 let key =
2482 if key >= 0xffb0 && key <= 0xffb9
2483 then key - 0xffb0 + 48 else key
2485 let enttext te =
2486 state.mode <- Textentry (te, onleave);
2487 state.text <- E.s;
2488 enttext ();
2489 G.postRedisplay "textentrykeyboard enttext";
2491 let histaction cmd =
2492 match opthist with
2493 | None -> ()
2494 | Some (action, _) ->
2495 state.mode <- Textentry (
2496 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
2498 G.postRedisplay "textentry histaction"
2500 match key with
2501 | @backspace ->
2502 if emptystr text && cancelonempty
2503 then (
2504 onleave Cancel;
2505 G.postRedisplay "textentrykeyboard after cancel";
2507 else
2508 let s = withoutlastutf8 text in
2509 enttext (c, s, opthist, onkey, ondone, cancelonempty)
2511 | @enter | @kpenter ->
2512 ondone text;
2513 onleave Confirm;
2514 G.postRedisplay "textentrykeyboard after confirm"
2516 | @up | @kpup -> histaction HCprev
2517 | @down | @kpdown -> histaction HCnext
2518 | @home | @kphome -> histaction HCfirst
2519 | @jend | @kpend -> histaction HClast
2521 | @escape ->
2522 if emptystr text
2523 then (
2524 begin match opthist with
2525 | None -> ()
2526 | Some (_, onhistcancel) -> onhistcancel ()
2527 end;
2528 onleave Cancel;
2529 state.text <- E.s;
2530 G.postRedisplay "textentrykeyboard after cancel2"
2532 else (
2533 enttext (c, E.s, opthist, onkey, ondone, cancelonempty)
2536 | @delete | @kpdelete -> ()
2538 | _ when key != 0
2539 && key land 0xff00 != 0xff00 (* keyboard *)
2540 && key land 0xfe00 != 0xfe00 (* xkb *)
2541 && key land 0xfd00 != 0xfd00 (* 3270 *)
2543 begin match onkey text key with
2544 | TEdone text ->
2545 ondone text;
2546 onleave Confirm;
2547 G.postRedisplay "textentrykeyboard after confirm2";
2549 | TEcont text ->
2550 enttext (c, text, opthist, onkey, ondone, cancelonempty);
2552 | TEstop ->
2553 onleave Cancel;
2554 G.postRedisplay "textentrykeyboard after cancel3"
2556 | TEswitch te ->
2557 state.mode <- Textentry (te, onleave);
2558 G.postRedisplay "textentrykeyboard switch";
2559 end;
2561 | _ ->
2562 vlog "unhandled key %s" (Wsi.keyname key)
2565 let firstof first active =
2566 if first > active || abs (first - active) > fstate.maxrows - 1
2567 then max 0 (active - (fstate.maxrows/2))
2568 else first
2571 let calcfirst first active =
2572 if active > first
2573 then
2574 let rows = active - first in
2575 if rows > fstate.maxrows then active - fstate.maxrows else first
2576 else active
2579 let scrollph y maxy =
2580 let sh = float (maxy + state.winh) /. float state.winh in
2581 let sh = float state.winh /. sh in
2582 let sh = max sh (float conf.scrollh) in
2584 let percent = float y /. float maxy in
2585 let position = (float state.winh -. sh) *. percent in
2587 let position =
2588 if position +. sh > float state.winh
2589 then float state.winh -. sh
2590 else position
2592 position, sh;
2595 let coe s = (s :> uioh);;
2597 class listview ~zebra ~helpmode ~(source:lvsource) ~trusted ~modehash =
2598 object (self)
2599 val m_pan = source#getpan
2600 val m_first = source#getfirst
2601 val m_active = source#getactive
2602 val m_qsearch = E.s
2603 val m_prev_uioh = state.uioh
2605 method private elemunder y =
2606 if y < 0
2607 then None
2608 else
2609 let n = y / (fstate.fontsize+1) in
2610 if m_first + n < source#getitemcount
2611 then (
2612 if source#hasaction (m_first + n)
2613 then Some (m_first + n)
2614 else None
2616 else None
2618 method display =
2619 Gl.enable `blend;
2620 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
2621 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2622 filledrect 0. 0. (float state.winw) (float state.winh);
2623 GlDraw.color (1., 1., 1.);
2624 Gl.enable `texture_2d;
2625 let fs = fstate.fontsize in
2626 let nfs = fs + 1 in
2627 let hw = (wadjsb () + xadjsb () + state.winw)/3 in
2628 let ww = fstate.wwidth in
2629 let tabw = 17.0*.ww in
2630 let itemcount = source#getitemcount in
2631 let minfo = source#getminfo in
2632 let x0, x1 =
2633 if conf.leftscroll
2634 then float (xadjsb ()), float (state.winw - 1)
2635 else 0.0, float (state.winw - conf.scrollbw - 1)
2637 let xadj = xadjsb () in
2638 let rec loop row =
2639 if (row - m_first) > fstate.maxrows
2640 then ()
2641 else (
2642 if row >= 0 && row < itemcount
2643 then (
2644 let (s, level) = source#getitem row in
2645 let y = (row - m_first) * nfs in
2646 let x =
2647 (if conf.leftscroll then float xadj else 5.0)
2648 +. (float (level + m_pan)) *. ww in
2649 if helpmode
2650 then GlDraw.color
2651 (let c = if row land 1 = 0 then 1.0 else 0.92 in (c,c,c));
2653 if row = m_active
2654 then (
2655 Gl.disable `texture_2d;
2656 let alpha = if source#hasaction row then 0.9 else 0.3 in
2657 GlDraw.color (1., 1., 1.) ~alpha;
2658 linerect (x0 +. 1.) (float (y + 1)) (x1) (float (y + fs + 3));
2659 Gl.enable `texture_2d;
2661 let c =
2662 if zebra && row land 1 = 1
2663 then 0.8
2664 else 1.0
2666 GlDraw.color (c,c,c);
2667 let drawtabularstring s =
2668 let drawstr x s =
2669 let x' = truncate (x0 +. x) in
2670 let pos = nindex s '\000' in
2671 if pos = -1
2672 then drawstring1 fs x' (y+nfs) s
2673 else
2674 let s1 = String.sub s 0 pos
2675 and s2 = String.sub s (pos+1) (String.length s - pos - 1) in
2676 let rec e s =
2677 if emptystr s
2678 then s
2679 else
2680 let s' = withoutlastutf8 s in
2681 let s = s' ^ "@Uellipsis" in
2682 let w = measurestr fs s in
2683 if float x' +. w +. ww < float (hw + x')
2684 then s
2685 else e s'
2687 let s1 =
2688 if float x' +. ww +. measurestr fs s1 > float (hw + x')
2689 then e s1
2690 else s1
2692 ignore (drawstring1 fs x' (y+nfs) s1);
2693 drawstring1 fs (hw + x') (y+nfs) s2
2695 if trusted
2696 then
2697 let x = if helpmode && row > 0 then x +. ww else x in
2698 let tabpos = nindex s '\t' in
2699 if tabpos > 0
2700 then
2701 let len = String.length s - tabpos - 1 in
2702 let s1 = String.sub s 0 tabpos
2703 and s2 = String.sub s (tabpos + 1) len in
2704 let nx = drawstr x s1 in
2705 let sw = nx -. x in
2706 let x = x +. (max tabw sw) in
2707 drawstr x s2
2708 else
2709 let len = String.length s - 2 in
2710 if len > 0 && s.[0] = '\xc2' && s.[1] = '\xb7'
2711 then
2712 let s = String.sub s 2 len in
2713 let x = if not helpmode then x +. ww else x in
2714 GlDraw.color (1.2, 1.2, 1.2);
2715 let vinc = drawstring1 (fs+fs/4)
2716 (truncate (x -. ww)) (y+nfs) s in
2717 GlDraw.color (1., 1., 1.);
2718 vinc +. (float fs *. 0.8)
2719 else
2720 drawstr x s
2721 else
2722 drawstr x s
2724 ignore (drawtabularstring s);
2725 loop (row+1)
2729 loop m_first;
2730 GlDraw.color (1.0, 1.0, 1.0) ~alpha:0.5;
2731 let xadj = float (xadjsb () + 5) in
2732 let rec loop row =
2733 if (row - m_first) > fstate.maxrows
2734 then ()
2735 else (
2736 if row >= 0 && row < itemcount
2737 then (
2738 let (s, level) = source#getitem row in
2739 let pos0 = nindex s '\000' in
2740 let y = (row - m_first) * nfs in
2741 let x = float (level + m_pan) *. ww in
2742 let (first, last) = minfo.(row) in
2743 let prefix =
2744 if pos0 > 0 && first > pos0
2745 then String.sub s (pos0+1) (first-pos0-1)
2746 else String.sub s 0 first
2748 let suffix = String.sub s first (last - first) in
2749 let w1 = measurestr fstate.fontsize prefix in
2750 let w2 = measurestr fstate.fontsize suffix in
2751 let x = x +. if conf.leftscroll then xadj else 5.0 in
2752 let x = if pos0 > 0 && first > pos0 then x +. float hw else x in
2753 let x0 = x +. w1
2754 and y0 = float (y+2) in
2755 let x1 = x0 +. w2
2756 and y1 = float (y+fs+3) in
2757 filledrect x0 y0 x1 y1;
2758 loop (row+1)
2762 Gl.disable `texture_2d;
2763 if Array.length minfo > 0 then loop m_first;
2764 Gl.disable `blend;
2766 method updownlevel incr =
2767 let len = source#getitemcount in
2768 let curlevel =
2769 if m_active >= 0 && m_active < len
2770 then snd (source#getitem m_active)
2771 else -1
2773 let rec flow i =
2774 if i = len then i-1 else if i = -1 then 0 else
2775 let _, l = source#getitem i in
2776 if l != curlevel then i else flow (i+incr)
2778 let active = flow m_active in
2779 let first = calcfirst m_first active in
2780 G.postRedisplay "outline updownlevel";
2781 {< m_active = active; m_first = first >}
2783 method private key1 key mask =
2784 let set1 active first qsearch =
2785 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2787 let search active pattern incr =
2788 let active = if active = -1 then m_first else active in
2789 let dosearch re =
2790 let rec loop n =
2791 if n >= 0 && n < source#getitemcount
2792 then (
2793 let s, _ = source#getitem n in
2795 (try ignore (Str.search_forward re s 0); true
2796 with Not_found -> false)
2797 then Some n
2798 else loop (n + incr)
2800 else None
2802 loop active
2805 let re = Str.regexp_case_fold pattern in
2806 dosearch re
2807 with Failure s ->
2808 state.text <- s;
2809 None
2811 let itemcount = source#getitemcount in
2812 let find start incr =
2813 let rec find i =
2814 if i = -1 || i = itemcount
2815 then -1
2816 else (
2817 if source#hasaction i
2818 then i
2819 else find (i + incr)
2822 find start
2824 let set active first =
2825 let first = bound first 0 (itemcount - fstate.maxrows) in
2826 state.text <- E.s;
2827 coe {< m_active = active; m_first = first; m_qsearch = E.s >}
2829 let navigate incr =
2830 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2831 let active, first =
2832 let incr1 = if incr > 0 then 1 else -1 in
2833 if isvisible m_first m_active
2834 then
2835 let next =
2836 let next = m_active + incr in
2837 let next =
2838 if next < 0 || next >= itemcount
2839 then -1
2840 else find next incr1
2842 if abs (m_active - next) > fstate.maxrows
2843 then -1
2844 else next
2846 if next = -1
2847 then
2848 let first = m_first + incr in
2849 let first = bound first 0 (itemcount - fstate.maxrows) in
2850 let next =
2851 let next = m_active + incr in
2852 let next = bound next 0 (itemcount - 1) in
2853 find next ~-incr1
2855 let active =
2856 if next = -1
2857 then m_active
2858 else (
2859 if isvisible first next
2860 then next
2861 else m_active
2864 active, first
2865 else
2866 let first = min next m_first in
2867 let first =
2868 if abs (next - first) > fstate.maxrows
2869 then first + incr
2870 else first
2872 next, first
2873 else
2874 let first = m_first + incr in
2875 let first = bound first 0 (itemcount - 1) in
2876 let active =
2877 let next = m_active + incr in
2878 let next = bound next 0 (itemcount - 1) in
2879 let next = find next incr1 in
2880 let active =
2881 if next = -1 || abs (m_active - first) > fstate.maxrows
2882 then (
2883 let active = if m_active = -1 then next else m_active in
2884 active
2886 else next
2888 if isvisible first active
2889 then active
2890 else -1
2892 active, first
2894 G.postRedisplay "listview navigate";
2895 set active first;
2897 match key with
2898 | (@r|@s) when Wsi.withctrl mask ->
2899 let incr = if key = @r then -1 else 1 in
2900 let active, first =
2901 match search (m_active + incr) m_qsearch incr with
2902 | None ->
2903 state.text <- m_qsearch ^ " [not found]";
2904 m_active, m_first
2905 | Some active ->
2906 state.text <- m_qsearch;
2907 active, firstof m_first active
2909 G.postRedisplay "listview ctrl-r/s";
2910 set1 active first m_qsearch;
2912 | @insert when Wsi.withctrl mask ->
2913 if m_active >= 0 && m_active < source#getitemcount
2914 then (
2915 let s, _ = source#getitem m_active in
2916 selstring s;
2918 coe self
2920 | @backspace ->
2921 if emptystr m_qsearch
2922 then coe self
2923 else (
2924 let qsearch = withoutlastutf8 m_qsearch in
2925 if emptystr qsearch
2926 then (
2927 state.text <- E.s;
2928 G.postRedisplay "listview empty qsearch";
2929 set1 m_active m_first E.s;
2931 else
2932 let active, first =
2933 match search m_active qsearch ~-1 with
2934 | None ->
2935 state.text <- qsearch ^ " [not found]";
2936 m_active, m_first
2937 | Some active ->
2938 state.text <- qsearch;
2939 active, firstof m_first active
2941 G.postRedisplay "listview backspace qsearch";
2942 set1 active first qsearch
2945 | key when (key != 0 && key land 0xff00 != 0xff00) ->
2946 let pattern = m_qsearch ^ toutf8 key in
2947 let active, first =
2948 match search m_active pattern 1 with
2949 | None ->
2950 state.text <- pattern ^ " [not found]";
2951 m_active, m_first
2952 | Some active ->
2953 state.text <- pattern;
2954 active, firstof m_first active
2956 G.postRedisplay "listview qsearch add";
2957 set1 active first pattern;
2959 | @escape ->
2960 state.text <- E.s;
2961 if emptystr m_qsearch
2962 then (
2963 G.postRedisplay "list view escape";
2964 begin
2965 match
2966 source#exit ~uioh:(coe self)
2967 ~cancel:true ~active:m_active ~first:m_first ~pan:m_pan
2968 with
2969 | None -> m_prev_uioh
2970 | Some uioh -> uioh
2973 else (
2974 G.postRedisplay "list view kill qsearch";
2975 coe {< m_qsearch = E.s >}
2978 | @enter | @kpenter ->
2979 state.text <- E.s;
2980 let self = {< m_qsearch = E.s >} in
2981 let opt =
2982 G.postRedisplay "listview enter";
2983 if m_active >= 0 && m_active < source#getitemcount
2984 then (
2985 source#exit ~uioh:(coe self) ~cancel:false
2986 ~active:m_active ~first:m_first ~pan:m_pan;
2988 else (
2989 source#exit ~uioh:(coe self) ~cancel:true
2990 ~active:m_active ~first:m_first ~pan:m_pan;
2993 begin match opt with
2994 | None -> m_prev_uioh
2995 | Some uioh -> uioh
2998 | @delete | @kpdelete ->
2999 coe self
3001 | @up | @kpup -> navigate ~-1
3002 | @down | @kpdown -> navigate 1
3003 | @prior | @kpprior -> navigate ~-(fstate.maxrows)
3004 | @next | @kpnext -> navigate fstate.maxrows
3006 | @right | @kpright ->
3007 state.text <- E.s;
3008 G.postRedisplay "listview right";
3009 coe {< m_pan = m_pan - 1 >}
3011 | @left | @kpleft ->
3012 state.text <- E.s;
3013 G.postRedisplay "listview left";
3014 coe {< m_pan = m_pan + 1 >}
3016 | @home | @kphome ->
3017 let active = find 0 1 in
3018 G.postRedisplay "listview home";
3019 set active 0;
3021 | @jend | @kpend ->
3022 let first = max 0 (itemcount - fstate.maxrows) in
3023 let active = find (itemcount - 1) ~-1 in
3024 G.postRedisplay "listview end";
3025 set active first;
3027 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3028 coe self
3030 | _ ->
3031 dolog "listview unknown key %#x" key; coe self
3033 method key key mask =
3034 match state.mode with
3035 | Textentry te -> textentrykeyboard key mask te; coe self
3036 | Birdseye _
3037 | View
3038 | LinkNav _ -> self#key1 key mask
3040 method button button down x y _ =
3041 let opt =
3042 match button with
3043 | 1 when x > state.winw - conf.scrollbw ->
3044 G.postRedisplay "listview scroll";
3045 if down
3046 then
3047 let _, position, sh = self#scrollph in
3048 if y > truncate position && y < truncate (position +. sh)
3049 then (
3050 state.mstate <- Mscrolly;
3051 Some (coe self)
3053 else
3054 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3055 let first = truncate (s *. float source#getitemcount) in
3056 let first = min source#getitemcount first in
3057 Some (coe {< m_first = first; m_active = first >})
3058 else (
3059 state.mstate <- Mnone;
3060 Some (coe self);
3062 | 1 when down ->
3063 begin match self#elemunder y with
3064 | Some n ->
3065 G.postRedisplay "listview click";
3066 source#exit ~uioh:(coe {< m_active = n >})
3067 ~cancel:false ~active:n ~first:m_first ~pan:m_pan
3068 | _ ->
3069 Some (coe self)
3071 | n when (n == 4 || n == 5) && not down ->
3072 let len = source#getitemcount in
3073 let first =
3074 if n = 5 && m_first + fstate.maxrows >= len
3075 then
3076 m_first
3077 else
3078 let first = m_first + (if n == 4 then -1 else 1) in
3079 bound first 0 (len - 1)
3081 G.postRedisplay "listview wheel";
3082 Some (coe {< m_first = first >})
3083 | n when (n = 6 || n = 7) && not down ->
3084 let inc = if n = 7 then -1 else 1 in
3085 G.postRedisplay "listview hwheel";
3086 Some (coe {< m_pan = m_pan + inc >})
3087 | _ ->
3088 Some (coe self)
3090 match opt with
3091 | None -> m_prev_uioh
3092 | Some uioh -> uioh
3094 method multiclick _ x y = self#button 1 true x y
3096 method motion _ y =
3097 match state.mstate with
3098 | Mscrolly ->
3099 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3100 let first = truncate (s *. float source#getitemcount) in
3101 let first = min source#getitemcount first in
3102 G.postRedisplay "listview motion";
3103 coe {< m_first = first; m_active = first >}
3104 | Msel _
3105 | Mpan _
3106 | Mscrollx
3107 | Mzoom _
3108 | Mzoomrect _
3109 | Mnone -> coe self
3111 method pmotion x y =
3112 if x < state.winw - conf.scrollbw
3113 then
3114 let n =
3115 match self#elemunder y with
3116 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3117 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3119 let o =
3120 if n != m_active
3121 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3122 else self
3124 coe o
3125 else (
3126 Wsi.setcursor Wsi.CURSOR_INHERIT;
3127 coe self
3130 method infochanged _ = ()
3132 method scrollpw = (0, 0.0, 0.0)
3133 method scrollph =
3134 let nfs = fstate.fontsize + 1 in
3135 let y = m_first * nfs in
3136 let itemcount = source#getitemcount in
3137 let maxi = max 0 (itemcount - fstate.maxrows) in
3138 let maxy = maxi * nfs in
3139 let p, h = scrollph y maxy in
3140 conf.scrollbw, p, h
3142 method modehash = modehash
3143 method eformsgs = false
3144 method alwaysscrolly = true
3145 end;;
3147 class outlinelistview ~zebra ~source =
3148 let settext autonarrow s =
3149 if autonarrow
3150 then
3151 let ss = source#statestr in
3152 state.text <-
3153 if emptystr ss
3154 then "[" ^ s ^ "]"
3155 else "{" ^ ss ^ "} [" ^ s ^ "]"
3156 else state.text <- s
3158 object (self)
3159 inherit listview
3160 ~zebra
3161 ~helpmode:false
3162 ~source:(source :> lvsource)
3163 ~trusted:false
3164 ~modehash:(findkeyhash conf "outline")
3165 as super
3167 val m_autonarrow = false
3169 method! key key mask =
3170 let maxrows =
3171 if emptystr state.text
3172 then fstate.maxrows
3173 else fstate.maxrows - 2
3175 let calcfirst first active =
3176 if active > first
3177 then
3178 let rows = active - first in
3179 if rows > maxrows then active - maxrows else first
3180 else active
3182 let navigate incr =
3183 let active = m_active + incr in
3184 let active = bound active 0 (source#getitemcount - 1) in
3185 let first = calcfirst m_first active in
3186 G.postRedisplay "outline navigate";
3187 coe {< m_active = active; m_first = first >}
3189 let navscroll first =
3190 let active =
3191 let dist = m_active - first in
3192 if dist < 0
3193 then first
3194 else (
3195 if dist < maxrows
3196 then m_active
3197 else first + maxrows
3200 G.postRedisplay "outline navscroll";
3201 coe {< m_first = first; m_active = active >}
3203 let ctrl = Wsi.withctrl mask in
3204 match key with
3205 | @a when ctrl ->
3206 let text =
3207 if m_autonarrow
3208 then (source#denarrow; E.s)
3209 else (
3210 let pattern = source#renarrow in
3211 if nonemptystr m_qsearch
3212 then (source#narrow m_qsearch; m_qsearch)
3213 else pattern
3216 settext (not m_autonarrow) text;
3217 G.postRedisplay "toggle auto narrowing";
3218 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
3220 | @slash when emptystr m_qsearch && not m_autonarrow ->
3221 settext true E.s;
3222 G.postRedisplay "toggle auto narrowing";
3223 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
3225 | @n when ctrl ->
3226 source#narrow m_qsearch;
3227 if not m_autonarrow
3228 then source#add_narrow_pattern m_qsearch;
3229 G.postRedisplay "outline ctrl-n";
3230 coe {< m_first = 0; m_active = 0 >}
3232 | @S when ctrl ->
3233 let active = source#calcactive (getanchor ()) in
3234 let first = firstof m_first active in
3235 G.postRedisplay "outline ctrl-s";
3236 coe {< m_first = first; m_active = active >}
3238 | @u when ctrl ->
3239 G.postRedisplay "outline ctrl-u";
3240 if m_autonarrow && nonemptystr m_qsearch
3241 then (
3242 ignore (source#renarrow);
3243 settext m_autonarrow E.s;
3244 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3246 else (
3247 source#del_narrow_pattern;
3248 let pattern = source#renarrow in
3249 let text =
3250 if emptystr pattern then E.s else "Narrowed to " ^ pattern
3252 settext m_autonarrow text;
3253 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3256 | @l when ctrl ->
3257 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3258 G.postRedisplay "outline ctrl-l";
3259 coe {< m_first = first >}
3261 | @tab when m_autonarrow ->
3262 if nonemptystr m_qsearch
3263 then (
3264 G.postRedisplay "outline list view tab";
3265 source#add_narrow_pattern m_qsearch;
3266 settext true E.s;
3267 coe {< m_qsearch = E.s >}
3269 else coe self
3271 | @escape when m_autonarrow ->
3272 if nonemptystr m_qsearch
3273 then source#add_narrow_pattern m_qsearch;
3274 super#key key mask
3276 | @enter | @kpenter when m_autonarrow ->
3277 if nonemptystr m_qsearch
3278 then source#add_narrow_pattern m_qsearch;
3279 super#key key mask
3281 | key when m_autonarrow && (key != 0 && key land 0xff00 != 0xff00) ->
3282 let pattern = m_qsearch ^ toutf8 key in
3283 G.postRedisplay "outlinelistview autonarrow add";
3284 source#narrow pattern;
3285 settext true pattern;
3286 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3288 | key when m_autonarrow && key = @backspace ->
3289 if emptystr m_qsearch
3290 then coe self
3291 else
3292 let pattern = withoutlastutf8 m_qsearch in
3293 G.postRedisplay "outlinelistview autonarrow backspace";
3294 ignore (source#renarrow);
3295 source#narrow pattern;
3296 settext true pattern;
3297 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3299 | @delete | @kpdelete ->
3300 source#remove m_active;
3301 G.postRedisplay "outline delete";
3302 let active = max 0 (m_active-1) in
3303 coe {< m_first = firstof m_first active;
3304 m_active = active >}
3306 | @up | @kpup when ctrl ->
3307 navscroll (max 0 (m_first - 1))
3309 | @down | @kpdown when ctrl ->
3310 navscroll (min (source#getitemcount - 1) (m_first + 1))
3312 | @up | @kpup -> navigate ~-1
3313 | @down | @kpdown -> navigate 1
3314 | @prior | @kpprior -> navigate ~-(fstate.maxrows)
3315 | @next | @kpnext -> navigate fstate.maxrows
3317 | @right | @kpright ->
3318 let o =
3319 if ctrl
3320 then (
3321 G.postRedisplay "outline ctrl right";
3322 {< m_pan = m_pan + 1 >}
3324 else self#updownlevel 1
3326 coe o
3328 | @left | @kpleft ->
3329 let o =
3330 if ctrl
3331 then (
3332 G.postRedisplay "outline ctrl left";
3333 {< m_pan = m_pan - 1 >}
3335 else self#updownlevel ~-1
3337 coe o
3339 | @home | @kphome ->
3340 G.postRedisplay "outline home";
3341 coe {< m_first = 0; m_active = 0 >}
3343 | @jend | @kpend ->
3344 let active = source#getitemcount - 1 in
3345 let first = max 0 (active - fstate.maxrows) in
3346 G.postRedisplay "outline end";
3347 coe {< m_active = active; m_first = first >}
3349 | _ -> super#key key mask
3352 let genhistoutlines =
3353 let order ty (p1, c1, _, _, _) (p2, c2, _, _, _) =
3354 match ty with
3355 | `lastvisit -> compare c1.lastvisit c2.lastvisit
3356 | `path -> compare p2 p1
3357 | `file -> compare (Filename.basename p2) (Filename.basename p1)
3358 | `title ->
3359 let e1 = emptystr c1.title
3360 and e2 = emptystr c2.title in
3361 (**) if e1 && e2
3362 then compare (Filename.basename p2) (Filename.basename p1)
3363 else if e1 then -1
3364 else if e2 then 1
3365 else compare c1.title c2.title
3367 let showfullpath = ref false in
3368 fun orderty ->
3369 let setorty s t =
3370 let s = if orderty = t then "[@Uradical] " ^ s else "[ ] " ^ s in
3371 s, 0, Oaction (fun () -> Config.historder := t; reeenterhist := true)
3373 let list = ref [] in
3374 if Config.gethist list
3375 then
3376 let ol =
3377 List.fold_left
3378 (fun accu (path, c, b, x, a) ->
3379 let hist = (path, (c, b, x, a)) in
3380 let s = if !showfullpath then path else Filename.basename path in
3381 let base = mbtoutf8 s in
3382 (base ^ "\000" ^ c.title, 1, Ohistory hist) :: accu
3384 [ setorty "Sort by time of last visit" `lastvisit;
3385 setorty "Sort by file name" `file;
3386 setorty "Sort by path" `path;
3387 setorty "Sort by title" `title;
3388 (if !showfullpath then "@Uradical "
3389 else " ") ^ "Show full path", 0, Oaction (fun () ->
3390 showfullpath := not !showfullpath; reeenterhist := true)
3391 ] (List.sort (order orderty) !list)
3393 Array.of_list ol
3394 else E.a;
3397 let gotohist (path, (c, bookmarks, x, anchor)) =
3398 Config.save leavebirdseye;
3399 state.anchor <- anchor;
3400 state.x <- x;
3401 state.bookmarks <- bookmarks;
3402 state.origin <- E.s;
3403 setconf conf c;
3404 let x0, y0, x1, y1 = conf.trimfuzz in
3405 wcmd "trimset %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1;
3406 opendoc path E.s;
3409 let makecheckers () =
3410 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3411 following to say:
3412 converted by Issac Trotts. July 25, 2002 *)
3413 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3414 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3415 let id = GlTex.gen_texture () in
3416 GlTex.bind_texture ~target:`texture_2d id;
3417 GlPix.store (`unpack_alignment 1);
3418 GlTex.image2d image;
3419 List.iter (GlTex.parameter ~target:`texture_2d)
3420 [ `mag_filter `nearest; `min_filter `nearest ];
3424 let setcheckers enabled =
3425 match state.checkerstexid with
3426 | None ->
3427 if enabled then state.checkerstexid <- Some (makecheckers ())
3429 | Some checkerstexid ->
3430 if not enabled
3431 then (
3432 GlTex.delete_texture checkerstexid;
3433 state.checkerstexid <- None;
3437 let describe_location () =
3438 let fn = page_of_y state.y in
3439 let ln = page_of_y (state.y + state.winh - hscrollh () - 1) in
3440 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
3441 let percent =
3442 if maxy <= 0
3443 then 100.
3444 else (100. *. (float state.y /. float maxy))
3446 if fn = ln
3447 then
3448 Printf.sprintf "page %d of %d [%.2f%%]"
3449 (fn+1) state.pagecount percent
3450 else
3451 Printf.sprintf
3452 "pages %d-%d of %d [%.2f%%]"
3453 (fn+1) (ln+1) state.pagecount percent
3456 let setpresentationmode v =
3457 let n = page_of_y state.y in
3458 state.anchor <- (n, 0.0, 1.0);
3459 conf.presentation <- v;
3460 if conf.fitmodel = FitPage
3461 then reqlayout conf.angle conf.fitmodel;
3462 represent ();
3465 let enterinfomode =
3466 let btos b = if b then "@Uradical" else E.s in
3467 let showextended = ref false in
3468 let leave mode _ = state.mode <- mode in
3469 let src =
3470 (object
3471 val mutable m_first_time = true
3472 val mutable m_l = []
3473 val mutable m_a = E.a
3474 val mutable m_prev_uioh = nouioh
3475 val mutable m_prev_mode = View
3477 inherit lvsourcebase
3479 method reset prev_mode prev_uioh =
3480 m_a <- Array.of_list (List.rev m_l);
3481 m_l <- [];
3482 m_prev_mode <- prev_mode;
3483 m_prev_uioh <- prev_uioh;
3484 if m_first_time
3485 then (
3486 let rec loop n =
3487 if n >= Array.length m_a
3488 then ()
3489 else
3490 match m_a.(n) with
3491 | _, _, _, Action _ -> m_active <- n
3492 | _, _, _, Noaction -> loop (n+1)
3494 loop 0;
3495 m_first_time <- false;
3498 method int name get set =
3499 m_l <-
3500 (name, `int get, 1, Action (
3501 fun u ->
3502 let ondone s =
3503 try set (int_of_string s)
3504 with exn ->
3505 state.text <- Printf.sprintf "bad integer `%s': %s"
3506 s (exntos exn)
3508 state.text <- E.s;
3509 let te = name ^ ": ", E.s, None, intentry, ondone, true in
3510 state.mode <- Textentry (te, leave m_prev_mode);
3512 )) :: m_l
3514 method int_with_suffix name get set =
3515 m_l <-
3516 (name, `intws get, 1, Action (
3517 fun u ->
3518 let ondone s =
3519 try set (int_of_string_with_suffix s)
3520 with exn ->
3521 state.text <- Printf.sprintf "bad integer `%s': %s"
3522 s (exntos exn)
3524 state.text <- E.s;
3525 let te =
3526 name ^ ": ", E.s, None, intentry_with_suffix, ondone, true
3528 state.mode <- Textentry (te, leave m_prev_mode);
3530 )) :: m_l
3532 method bool ?(offset=1) ?(btos=btos) name get set =
3533 m_l <-
3534 (name, `bool (btos, get), offset, Action (
3535 fun u ->
3536 let v = get () in
3537 set (not v);
3539 )) :: m_l
3541 method color name get set =
3542 m_l <-
3543 (name, `color get, 1, Action (
3544 fun u ->
3545 let invalid = (nan, nan, nan) in
3546 let ondone s =
3547 let c =
3548 try color_of_string s
3549 with exn ->
3550 state.text <- Printf.sprintf "bad color `%s': %s"
3551 s (exntos exn);
3552 invalid
3554 if c <> invalid
3555 then set c;
3557 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3558 state.text <- color_to_string (get ());
3559 state.mode <- Textentry (te, leave m_prev_mode);
3561 )) :: m_l
3563 method string name get set =
3564 m_l <-
3565 (name, `string get, 1, Action (
3566 fun u ->
3567 let ondone s = set s in
3568 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3569 state.mode <- Textentry (te, leave m_prev_mode);
3571 )) :: m_l
3573 method colorspace name get set =
3574 m_l <-
3575 (name, `string get, 1, Action (
3576 fun _ ->
3577 let source =
3578 (object
3579 inherit lvsourcebase
3581 initializer
3582 m_active <- CSTE.to_int conf.colorspace;
3583 m_first <- 0;
3585 method getitemcount =
3586 Array.length CSTE.names
3587 method getitem n =
3588 (CSTE.names.(n), 0)
3589 method exit ~uioh ~cancel ~active ~first ~pan =
3590 ignore (uioh, first, pan);
3591 if not cancel then set active;
3592 None
3593 method hasaction _ = true
3594 end)
3596 state.text <- E.s;
3597 let modehash = findkeyhash conf "info" in
3598 coe (new listview ~zebra:false ~helpmode:false
3599 ~source ~trusted:true ~modehash)
3600 )) :: m_l
3602 method paxmark name get set =
3603 m_l <-
3604 (name, `string get, 1, Action (
3605 fun _ ->
3606 let source =
3607 (object
3608 inherit lvsourcebase
3610 initializer
3611 m_active <- MTE.to_int conf.paxmark;
3612 m_first <- 0;
3614 method getitemcount = Array.length MTE.names
3615 method getitem n = (MTE.names.(n), 0)
3616 method exit ~uioh ~cancel ~active ~first ~pan =
3617 ignore (uioh, first, pan);
3618 if not cancel then set active;
3619 None
3620 method hasaction _ = true
3621 end)
3623 state.text <- E.s;
3624 let modehash = findkeyhash conf "info" in
3625 coe (new listview ~zebra:false ~helpmode:false
3626 ~source ~trusted:true ~modehash)
3627 )) :: m_l
3629 method fitmodel name get set =
3630 m_l <-
3631 (name, `string get, 1, Action (
3632 fun _ ->
3633 let source =
3634 (object
3635 inherit lvsourcebase
3637 initializer
3638 m_active <- FMTE.to_int conf.fitmodel;
3639 m_first <- 0;
3641 method getitemcount = Array.length FMTE.names
3642 method getitem n = (FMTE.names.(n), 0)
3643 method exit ~uioh ~cancel ~active ~first ~pan =
3644 ignore (uioh, first, pan);
3645 if not cancel then set active;
3646 None
3647 method hasaction _ = true
3648 end)
3650 state.text <- E.s;
3651 let modehash = findkeyhash conf "info" in
3652 coe (new listview ~zebra:false ~helpmode:false
3653 ~source ~trusted:true ~modehash)
3654 )) :: m_l
3656 method caption s offset =
3657 m_l <- (s, `empty, offset, Noaction) :: m_l
3659 method caption2 s f offset =
3660 m_l <- (s, `string f, offset, Noaction) :: m_l
3662 method getitemcount = Array.length m_a
3664 method getitem n =
3665 let tostr = function
3666 | `int f -> string_of_int (f ())
3667 | `intws f -> string_with_suffix_of_int (f ())
3668 | `string f -> f ()
3669 | `color f -> color_to_string (f ())
3670 | `bool (btos, f) -> btos (f ())
3671 | `empty -> E.s
3673 let name, t, offset, _ = m_a.(n) in
3674 ((let s = tostr t in
3675 if nonemptystr s
3676 then Printf.sprintf "%s\t%s" name s
3677 else name),
3678 offset)
3680 method exit ~uioh ~cancel ~active ~first ~pan =
3681 let uiohopt =
3682 if not cancel
3683 then (
3684 let uioh =
3685 match m_a.(active) with
3686 | _, _, _, Action f -> f uioh
3687 | _, _, _, Noaction -> uioh
3689 Some uioh
3691 else None
3693 m_active <- active;
3694 m_first <- first;
3695 m_pan <- pan;
3696 uiohopt
3698 method hasaction n =
3699 match m_a.(n) with
3700 | _, _, _, Action _ -> true
3701 | _, _, _, Noaction -> false
3702 end)
3704 let rec fillsrc prevmode prevuioh =
3705 let sep () = src#caption E.s 0 in
3706 let colorp name get set =
3707 src#string name
3708 (fun () -> color_to_string (get ()))
3709 (fun v ->
3711 let c = color_of_string v in
3712 set c
3713 with exn ->
3714 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
3717 let oldmode = state.mode in
3718 let birdseye = isbirdseye state.mode in
3720 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3722 src#bool "presentation mode"
3723 (fun () -> conf.presentation)
3724 (fun v -> setpresentationmode v);
3726 src#bool "ignore case in searches"
3727 (fun () -> conf.icase)
3728 (fun v -> conf.icase <- v);
3730 src#bool "preload"
3731 (fun () -> conf.preload)
3732 (fun v -> conf.preload <- v);
3734 src#bool "highlight links"
3735 (fun () -> conf.hlinks)
3736 (fun v -> conf.hlinks <- v);
3738 src#bool "under info"
3739 (fun () -> conf.underinfo)
3740 (fun v -> conf.underinfo <- v);
3742 src#bool "persistent bookmarks"
3743 (fun () -> conf.savebmarks)
3744 (fun v -> conf.savebmarks <- v);
3746 src#fitmodel "fit model"
3747 (fun () -> FMTE.to_string conf.fitmodel)
3748 (fun v -> reqlayout conf.angle (FMTE.of_int v));
3750 src#bool "trim margins"
3751 (fun () -> conf.trimmargins)
3752 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3754 src#bool "persistent location"
3755 (fun () -> conf.jumpback)
3756 (fun v -> conf.jumpback <- v);
3758 sep ();
3759 src#int "inter-page space"
3760 (fun () -> conf.interpagespace)
3761 (fun n ->
3762 conf.interpagespace <- n;
3763 docolumns conf.columns;
3764 let pageno, py =
3765 match state.layout with
3766 | [] -> 0, 0
3767 | l :: _ ->
3768 l.pageno, l.pagey
3770 state.maxy <- calcheight ();
3771 let y = getpagey pageno in
3772 gotoy (y + py)
3775 src#int "page bias"
3776 (fun () -> conf.pagebias)
3777 (fun v -> conf.pagebias <- v);
3779 src#int "scroll step"
3780 (fun () -> conf.scrollstep)
3781 (fun n -> conf.scrollstep <- n);
3783 src#int "horizontal scroll step"
3784 (fun () -> conf.hscrollstep)
3785 (fun v -> conf.hscrollstep <- v);
3787 src#int "auto scroll step"
3788 (fun () ->
3789 match state.autoscroll with
3790 | Some step -> step
3791 | _ -> conf.autoscrollstep)
3792 (fun n ->
3793 let n = boundastep state.winh n in
3794 if state.autoscroll <> None
3795 then state.autoscroll <- Some n;
3796 conf.autoscrollstep <- n);
3798 src#int "zoom"
3799 (fun () -> truncate (conf.zoom *. 100.))
3800 (fun v -> setzoom ((float v) /. 100.));
3802 src#int "rotation"
3803 (fun () -> conf.angle)
3804 (fun v -> reqlayout v conf.fitmodel);
3806 src#int "scroll bar width"
3807 (fun () -> conf.scrollbw)
3808 (fun v ->
3809 conf.scrollbw <- v;
3810 reshape state.winw state.winh;
3813 src#int "scroll handle height"
3814 (fun () -> conf.scrollh)
3815 (fun v -> conf.scrollh <- v;);
3817 src#int "thumbnail width"
3818 (fun () -> conf.thumbw)
3819 (fun v ->
3820 conf.thumbw <- min 4096 v;
3821 match oldmode with
3822 | Birdseye beye ->
3823 leavebirdseye beye false;
3824 enterbirdseye ()
3825 | Textentry _
3826 | View
3827 | LinkNav _ -> ()
3830 let mode = state.mode in
3831 src#string "columns"
3832 (fun () ->
3833 match conf.columns with
3834 | Csingle _ -> "1"
3835 | Cmulti (multi, _) -> multicolumns_to_string multi
3836 | Csplit (count, _) -> "-" ^ string_of_int count
3838 (fun v ->
3839 let n, a, b = multicolumns_of_string v in
3840 setcolumns mode n a b);
3842 sep ();
3843 src#caption "Pixmap cache" 0;
3844 src#int_with_suffix "size (advisory)"
3845 (fun () -> conf.memlimit)
3846 (fun v -> conf.memlimit <- v);
3848 src#caption2 "used"
3849 (fun () -> Printf.sprintf "%s bytes, %d tiles"
3850 (string_with_suffix_of_int state.memused)
3851 (Hashtbl.length state.tilemap)) 1;
3853 sep ();
3854 src#caption "Layout" 0;
3855 src#caption2 "Dimension"
3856 (fun () ->
3857 Printf.sprintf "%dx%d (virtual %dx%d)"
3858 state.winw state.winh
3859 state.w state.maxy)
3861 if conf.debug
3862 then
3863 src#caption2 "Position" (fun () ->
3864 Printf.sprintf "%dx%d" state.x state.y
3866 else
3867 src#caption2 "Position" (fun () -> describe_location ()) 1
3870 sep ();
3871 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3872 "Save these parameters as global defaults at exit"
3873 (fun () -> conf.bedefault)
3874 (fun v -> conf.bedefault <- v)
3877 sep ();
3878 let btos b = if b then "@Ulguillemet" else "@Urguillemet" in
3879 src#bool ~offset:0 ~btos "Extended parameters"
3880 (fun () -> !showextended)
3881 (fun v -> showextended := v; fillsrc prevmode prevuioh);
3882 if !showextended
3883 then (
3884 src#bool "checkers"
3885 (fun () -> conf.checkers)
3886 (fun v -> conf.checkers <- v; setcheckers v);
3887 src#bool "update cursor"
3888 (fun () -> conf.updatecurs)
3889 (fun v -> conf.updatecurs <- v);
3890 src#bool "scroll-bar on the left"
3891 (fun () -> conf.leftscroll)
3892 (fun v -> conf.leftscroll <- v);
3893 src#bool "verbose"
3894 (fun () -> conf.verbose)
3895 (fun v -> conf.verbose <- v);
3896 src#bool "invert colors"
3897 (fun () -> conf.invert)
3898 (fun v -> conf.invert <- v);
3899 src#bool "max fit"
3900 (fun () -> conf.maxhfit)
3901 (fun v -> conf.maxhfit <- v);
3902 src#bool "redirect stderr"
3903 (fun () -> conf.redirectstderr)
3904 (fun v -> conf.redirectstderr <- v; redirectstderr ());
3905 src#bool "pax mode"
3906 (fun () -> conf.pax != None)
3907 (fun v ->
3908 if v
3909 then conf.pax <- Some (ref (now (), 0, 0))
3910 else conf.pax <- None);
3911 src#string "uri launcher"
3912 (fun () -> conf.urilauncher)
3913 (fun v -> conf.urilauncher <- v);
3914 src#string "path launcher"
3915 (fun () -> conf.pathlauncher)
3916 (fun v -> conf.pathlauncher <- v);
3917 src#string "tile size"
3918 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3919 (fun v ->
3921 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
3922 conf.tilew <- max 64 w;
3923 conf.tileh <- max 64 h;
3924 flushtiles ();
3925 with exn ->
3926 state.text <- Printf.sprintf "bad tile size `%s': %s"
3927 v (exntos exn)
3929 src#int "texture count"
3930 (fun () -> conf.texcount)
3931 (fun v ->
3932 if realloctexts v
3933 then conf.texcount <- v
3934 else showtext '!' " Failed to set texture count please retry later"
3936 src#int "slice height"
3937 (fun () -> conf.sliceheight)
3938 (fun v ->
3939 conf.sliceheight <- v;
3940 wcmd "sliceh %d" conf.sliceheight;
3942 src#int "anti-aliasing level"
3943 (fun () -> conf.aalevel)
3944 (fun v ->
3945 conf.aalevel <- bound v 0 8;
3946 state.anchor <- getanchor ();
3947 opendoc state.path state.password;
3949 src#string "page scroll scaling factor"
3950 (fun () -> string_of_float conf.pgscale)
3951 (fun v ->
3953 let s = float_of_string v in
3954 conf.pgscale <- s
3955 with exn ->
3956 state.text <- Printf.sprintf
3957 "bad page scroll scaling factor `%s': %s" v (exntos exn)
3960 src#int "ui font size"
3961 (fun () -> fstate.fontsize)
3962 (fun v -> setfontsize (bound v 5 100));
3963 src#int "hint font size"
3964 (fun () -> conf.hfsize)
3965 (fun v -> conf.hfsize <- bound v 5 100);
3966 colorp "background color"
3967 (fun () -> conf.bgcolor)
3968 (fun v -> conf.bgcolor <- v);
3969 src#bool "crop hack"
3970 (fun () -> conf.crophack)
3971 (fun v -> conf.crophack <- v);
3972 src#string "trim fuzz"
3973 (fun () -> irect_to_string conf.trimfuzz)
3974 (fun v ->
3976 conf.trimfuzz <- irect_of_string v;
3977 if conf.trimmargins
3978 then settrim true conf.trimfuzz;
3979 with exn ->
3980 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
3982 src#string "throttle"
3983 (fun () ->
3984 match conf.maxwait with
3985 | None -> "show place holder if page is not ready"
3986 | Some time ->
3987 if time = infinity
3988 then "wait for page to fully render"
3989 else
3990 "wait " ^ string_of_float time
3991 ^ " seconds before showing placeholder"
3993 (fun v ->
3995 let f = float_of_string v in
3996 if f <= 0.0
3997 then conf.maxwait <- None
3998 else conf.maxwait <- Some f
3999 with exn ->
4000 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
4002 src#string "ghyll scroll"
4003 (fun () ->
4004 match conf.ghyllscroll with
4005 | None -> E.s
4006 | Some nab -> ghyllscroll_to_string nab
4008 (fun v ->
4009 try conf.ghyllscroll <- ghyllscroll_of_string v
4010 with exn ->
4011 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
4013 src#string "selection command"
4014 (fun () -> conf.selcmd)
4015 (fun v -> conf.selcmd <- v);
4016 src#string "synctex command"
4017 (fun () -> conf.stcmd)
4018 (fun v -> conf.stcmd <- v);
4019 src#string "pax command"
4020 (fun () -> conf.paxcmd)
4021 (fun v -> conf.paxcmd <- v);
4022 src#string "ask password command"
4023 (fun () -> conf.passcmd)
4024 (fun v -> conf.passcmd <- v);
4025 src#string "save path command"
4026 (fun () -> conf.savecmd)
4027 (fun v -> conf.savecmd <- v);
4028 src#colorspace "color space"
4029 (fun () -> CSTE.to_string conf.colorspace)
4030 (fun v ->
4031 conf.colorspace <- CSTE.of_int v;
4032 wcmd "cs %d" v;
4033 load state.layout;
4035 src#paxmark "pax mark method"
4036 (fun () -> MTE.to_string conf.paxmark)
4037 (fun v -> conf.paxmark <- MTE.of_int v);
4038 if pbousable ()
4039 then
4040 src#bool "use PBO"
4041 (fun () -> conf.usepbo)
4042 (fun v -> conf.usepbo <- v);
4043 src#bool "mouse wheel scrolls pages"
4044 (fun () -> conf.wheelbypage)
4045 (fun v -> conf.wheelbypage <- v);
4046 src#bool "open remote links in a new instance"
4047 (fun () -> conf.riani)
4048 (fun v -> conf.riani <- v);
4049 src#bool "edit annotations inline"
4050 (fun () -> conf.annotinline)
4051 (fun v -> conf.annotinline <- v);
4054 sep ();
4055 src#caption "Document" 0;
4056 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4057 src#caption2 "Pages"
4058 (fun () -> string_of_int state.pagecount) 1;
4059 src#caption2 "Dimensions"
4060 (fun () -> string_of_int (List.length state.pdims)) 1;
4061 if conf.trimmargins
4062 then (
4063 sep ();
4064 src#caption "Trimmed margins" 0;
4065 src#caption2 "Dimensions"
4066 (fun () -> string_of_int (List.length state.pdims)) 1;
4069 sep ();
4070 src#caption "OpenGL" 0;
4071 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4072 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4074 sep ();
4075 src#caption "Location" 0;
4076 if nonemptystr state.origin
4077 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
4078 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
4080 src#reset prevmode prevuioh;
4082 fun () ->
4083 state.text <- E.s;
4084 resetmstate ();
4085 let prevmode = state.mode
4086 and prevuioh = state.uioh in
4087 fillsrc prevmode prevuioh;
4088 let source = (src :> lvsource) in
4089 let modehash = findkeyhash conf "info" in
4090 state.uioh <- coe (object (self)
4091 inherit listview ~zebra:false ~helpmode:false
4092 ~source ~trusted:true ~modehash as super
4093 val mutable m_prevmemused = 0
4094 method! infochanged = function
4095 | Memused ->
4096 if m_prevmemused != state.memused
4097 then (
4098 m_prevmemused <- state.memused;
4099 G.postRedisplay "memusedchanged";
4101 | Pdim -> G.postRedisplay "pdimchanged"
4102 | Docinfo -> fillsrc prevmode prevuioh
4104 method! key key mask =
4105 if not (Wsi.withctrl mask)
4106 then
4107 match key with
4108 | @left | @kpleft -> coe (self#updownlevel ~-1)
4109 | @right | @kpright -> coe (self#updownlevel 1)
4110 | _ -> super#key key mask
4111 else super#key key mask
4112 end);
4113 G.postRedisplay "info";
4116 let enterhelpmode =
4117 let source =
4118 (object
4119 inherit lvsourcebase
4120 method getitemcount = Array.length state.help
4121 method getitem n =
4122 let s, l, _ = state.help.(n) in
4123 (s, l)
4125 method exit ~uioh ~cancel ~active ~first ~pan =
4126 let optuioh =
4127 if not cancel
4128 then (
4129 match state.help.(active) with
4130 | _, _, Action f -> Some (f uioh)
4131 | _, _, Noaction -> Some uioh
4133 else None
4135 m_active <- active;
4136 m_first <- first;
4137 m_pan <- pan;
4138 optuioh
4140 method hasaction n =
4141 match state.help.(n) with
4142 | _, _, Action _ -> true
4143 | _, _, Noaction -> false
4145 initializer
4146 m_active <- -1
4147 end)
4148 in fun () ->
4149 let modehash = findkeyhash conf "help" in
4150 resetmstate ();
4151 state.uioh <- coe (new listview
4152 ~zebra:false ~helpmode:true
4153 ~source ~trusted:true ~modehash);
4154 G.postRedisplay "help";
4157 let entermsgsmode =
4158 let msgsource =
4159 (object
4160 inherit lvsourcebase
4161 val mutable m_items = E.a
4163 method getitemcount = 1 + Array.length m_items
4165 method getitem n =
4166 if n = 0
4167 then "[Clear]", 0
4168 else m_items.(n-1), 0
4170 method exit ~uioh ~cancel ~active ~first ~pan =
4171 ignore uioh;
4172 if not cancel
4173 then (
4174 if active = 0
4175 then Buffer.clear state.errmsgs;
4177 m_active <- active;
4178 m_first <- first;
4179 m_pan <- pan;
4180 None
4182 method hasaction n =
4183 n = 0
4185 method reset =
4186 state.newerrmsgs <- false;
4187 let l = Str.split newlinere (Buffer.contents state.errmsgs) in
4188 m_items <- Array.of_list l
4190 initializer
4191 m_active <- 0
4192 end)
4193 in fun () ->
4194 state.text <- E.s;
4195 resetmstate ();
4196 msgsource#reset;
4197 let source = (msgsource :> lvsource) in
4198 let modehash = findkeyhash conf "listview" in
4199 state.uioh <- coe (object
4200 inherit listview ~zebra:false ~helpmode:false
4201 ~source ~trusted:false ~modehash as super
4202 method! display =
4203 if state.newerrmsgs
4204 then msgsource#reset;
4205 super#display
4206 end);
4207 G.postRedisplay "msgs";
4210 let getusertext s =
4211 let editor = getenvwithdef "EDITOR" E.s in
4212 if emptystr editor
4213 then E.s
4214 else
4215 let tmppath = Filename.temp_file "llpp" "note" in
4216 if not (emptystr s)
4217 then (
4218 let oc = open_out tmppath in
4219 output_string oc s;
4220 close_out oc;
4222 let execstr = editor ^ " " ^ tmppath in
4223 let s =
4224 match popen execstr [] with
4225 | (exception exn) ->
4226 showtext '!' @@
4227 Printf.sprintf "popen(%S) failed: %s" execstr (exntos exn);
4229 | pid ->
4230 match Unix.waitpid [] pid
4231 with
4232 | (exception exn) ->
4233 showtext '!' @@
4234 Printf.sprintf "waitpid(%d) failed: %s" pid (exntos exn);
4236 | (_pid, status) ->
4237 match status with
4238 | Unix.WEXITED 0 -> filelines tmppath
4239 | Unix.WEXITED n ->
4240 showtext '!' @@
4241 Printf.sprintf "editor process(%s) exited abnormally: %d"
4242 execstr n;
4244 | Unix.WSIGNALED n ->
4245 showtext '!' @@
4246 Printf.sprintf "editor process(%s) was killed by signal %d"
4247 execstr n;
4249 | Unix.WSTOPPED n ->
4250 showtext '!' @@
4251 Printf.sprintf "editor(%s) process was stopped by signal %d"
4252 execstr n;
4255 match Unix.unlink tmppath with
4256 | (exception exn) ->
4257 showtext '!' @@ Printf.sprintf "failed to ulink %S: %s"
4258 tmppath (exntos exn);
4260 | () -> s
4263 let enterannotmode opaque slinkindex =
4264 let msgsource =
4265 (object
4266 inherit lvsourcebase
4267 val mutable m_text = E.s
4268 val mutable m_items = E.a
4270 method getitemcount = Array.length m_items
4272 method getitem n =
4273 let label, _func = m_items.(n) in
4274 label, 0
4276 method exit ~uioh ~cancel ~active ~first ~pan =
4277 ignore (uioh, first, pan);
4278 if not cancel
4279 then (
4280 let _label, func = m_items.(active) in
4281 func ()
4283 None
4285 method hasaction n = not @@ emptystr @@ fst m_items.(n)
4287 method reset s =
4288 let rec split accu b i =
4289 let p = b+i in
4290 if p = String.length s
4291 then (String.sub s b (p-b), unit) :: accu
4292 else
4293 if (i > 70 && s.[p] = ' ') || s.[p] = '\r' || s.[p] = '\n'
4294 then
4295 let ss = if i = 0 then E.s else String.sub s b i in
4296 split ((ss, unit)::accu) (p+1) 0
4297 else
4298 split accu b (i+1)
4300 let cleanup () =
4301 wcmd "freepage %s" (~> opaque);
4302 let keys =
4303 Hashtbl.fold (fun key opaque' accu ->
4304 if opaque' = opaque'
4305 then key :: accu else accu) state.pagemap []
4307 List.iter (Hashtbl.remove state.pagemap) keys;
4308 flushtiles ();
4309 gotoy state.y
4311 let dele () =
4312 delannot opaque slinkindex;
4313 cleanup ();
4315 let edit inline () =
4316 let update s =
4317 if emptystr s
4318 then dele ()
4319 else (
4320 modannot opaque slinkindex s;
4321 cleanup ();
4324 if inline
4325 then
4326 let mode = state.mode in
4327 state.mode <-
4328 Textentry (
4329 ("annotation: ", m_text, None, textentry, update, true),
4330 fun _ -> state.mode <- mode);
4331 state.text <- E.s;
4332 enttext ();
4333 else
4334 let s = getusertext m_text in
4335 update s
4337 m_text <- s;
4338 m_items <-
4339 ( "[Copy]", fun () -> selstring m_text)
4340 :: ("[Delete]", dele)
4341 :: ("[Edit]", edit conf.annotinline)
4342 :: (E.s, unit)
4343 :: split [] 0 0 |> List.rev |> Array.of_list
4345 initializer
4346 m_active <- 0
4347 end)
4349 state.text <- E.s;
4350 let s = getannotcontents opaque slinkindex in
4351 resetmstate ();
4352 msgsource#reset s;
4353 let source = (msgsource :> lvsource) in
4354 let modehash = findkeyhash conf "listview" in
4355 state.uioh <- coe (object
4356 inherit listview ~zebra:false ~helpmode:false
4357 ~source ~trusted:false ~modehash
4358 end);
4359 G.postRedisplay "enterannotmode";
4362 let gotounder under =
4363 let getpath filename =
4364 let path =
4365 if nonemptystr filename
4366 then
4367 if Filename.is_relative filename
4368 then
4369 let dir = Filename.dirname state.path in
4370 let dir =
4371 if Filename.is_implicit dir
4372 then Filename.concat (Sys.getcwd ()) dir
4373 else dir
4375 Filename.concat dir filename
4376 else filename
4377 else E.s
4379 if Sys.file_exists path
4380 then path
4381 else E.s
4383 match under with
4384 | Ulinkgoto (pageno, top) ->
4385 if pageno >= 0
4386 then (
4387 addnav ();
4388 gotopage1 pageno top;
4391 | Ulinkuri s ->
4392 addpid @@ gotouri s
4394 | Uremote (filename, pageno) ->
4395 let path = getpath filename in
4396 if nonemptystr path
4397 then (
4398 if conf.riani
4399 then
4400 let command = Printf.sprintf "%s -page %d %S" !selfexec pageno path in
4401 try addpid @@ popen command []
4402 with exn ->
4403 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
4404 flush stderr;
4405 else
4406 let anchor = getanchor () in
4407 let ranchor = state.path, state.password, anchor, state.origin in
4408 state.origin <- E.s;
4409 state.anchor <- (pageno, 0.0, 0.0);
4410 state.ranchors <- ranchor :: state.ranchors;
4411 opendoc path E.s;
4413 else showtext '!' ("Could not find " ^ filename)
4415 | Uremotedest (filename, destname) ->
4416 let path = getpath filename in
4417 if nonemptystr path
4418 then (
4419 if conf.riani
4420 then
4421 let command = !selfexec ^ " " ^ path ^ " -dest " ^ destname in
4422 try addpid @@ popen command []
4423 with exn ->
4424 Printf.eprintf
4425 "failed to execute `%s': %s\n" command (exntos exn);
4426 flush stderr;
4427 else
4428 let anchor = getanchor () in
4429 let ranchor = state.path, state.password, anchor, state.origin in
4430 state.origin <- E.s;
4431 state.nameddest <- destname;
4432 state.ranchors <- ranchor :: state.ranchors;
4433 opendoc path E.s;
4435 else showtext '!' ("Could not find " ^ filename)
4437 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4438 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
4441 let gotooutline (_, _, kind) =
4442 match kind with
4443 | Onone -> ()
4444 | Oanchor anchor ->
4445 let (pageno, y, _) = anchor in
4446 let y = getanchory
4447 (if conf.presentation then (pageno, y, 1.0) else anchor)
4449 addnav ();
4450 gotoghyll y
4451 | Ouri uri -> gotounder (Ulinkuri uri)
4452 | Olaunch cmd -> gotounder (Ulaunch cmd)
4453 | Oremote remote -> gotounder (Uremote remote)
4454 | Ohistory hist -> gotohist hist
4455 | Oremotedest remotedest -> gotounder (Uremotedest remotedest)
4456 | Oaction f -> f ()
4459 let outlinesource sourcetype =
4460 (object (self)
4461 inherit lvsourcebase
4462 val mutable m_items = E.a
4463 val mutable m_minfo = E.a
4464 val mutable m_orig_items = E.a
4465 val mutable m_orig_minfo = E.a
4466 val mutable m_narrow_patterns = []
4467 val mutable m_hadremovals = false
4468 val mutable m_gen = -1
4470 method getitemcount =
4471 Array.length m_items + (if m_hadremovals then 1 else 0)
4473 method getitem n =
4474 if n == Array.length m_items && m_hadremovals
4475 then
4476 ("[Confirm removal]", 0)
4477 else
4478 let s, n, _ = m_items.(n) in
4479 (s, n)
4481 method exit ~uioh ~cancel ~active ~first ~pan =
4482 ignore (uioh, first);
4483 let confrimremoval = m_hadremovals && active = Array.length m_items in
4484 let items, minfo =
4485 if m_narrow_patterns = []
4486 then m_orig_items, m_orig_minfo
4487 else m_items, m_minfo
4489 if not cancel
4490 then (
4491 if not confrimremoval
4492 then (
4493 gotooutline m_items.(active);
4494 m_items <- items;
4495 m_minfo <- minfo;
4497 else (
4498 state.bookmarks <- Array.to_list m_items;
4499 m_orig_items <- m_items;
4500 m_orig_minfo <- m_minfo;
4503 else (
4504 m_items <- items;
4505 m_minfo <- minfo;
4507 m_pan <- pan;
4508 None
4510 method hasaction _ = true
4512 method greetmsg =
4513 if Array.length m_items != Array.length m_orig_items
4514 then
4515 let s =
4516 match m_narrow_patterns with
4517 | one :: [] -> one
4518 | many -> String.concat "@Uellipsis" (List.rev many)
4520 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
4521 else E.s
4523 method statestr =
4524 match m_narrow_patterns with
4525 | [] -> E.s
4526 | one :: [] -> one
4527 | head :: _ -> "@Uellipsis" ^ head
4529 method narrow pattern =
4530 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
4531 match reopt with
4532 | None -> ()
4533 | Some re ->
4534 let rec loop accu minfo n =
4535 if n = -1
4536 then (
4537 m_items <- Array.of_list accu;
4538 m_minfo <- Array.of_list minfo;
4540 else
4541 let (s, _, t) as o = m_items.(n) in
4542 let accu, minfo =
4543 match t with
4544 | Oaction _ -> o :: accu, (0, 0) :: minfo
4545 | Onone | Oanchor _ | Ouri _ | Olaunch _
4546 | Oremote _ | Oremotedest _ | Ohistory _ ->
4547 let first =
4548 try Str.search_forward re s 0
4549 with Not_found -> -1
4551 if first >= 0
4552 then o :: accu, (first, Str.match_end ()) :: minfo
4553 else accu, minfo
4555 loop accu minfo (n-1)
4557 loop [] [] (Array.length m_items - 1)
4559 method! getminfo = m_minfo
4561 method denarrow =
4562 m_orig_items <- (
4563 match sourcetype with
4564 | `bookmarks -> Array.of_list state.bookmarks
4565 | `outlines -> state.outlines
4566 | `history -> genhistoutlines !Config.historder
4568 m_minfo <- m_orig_minfo;
4569 m_items <- m_orig_items
4571 method remove m =
4572 if sourcetype = `bookmarks
4573 then
4574 if m >= 0 && m < Array.length m_items
4575 then (
4576 m_hadremovals <- true;
4577 m_items <- Array.init (Array.length m_items - 1) (fun n ->
4578 let n = if n >= m then n+1 else n in
4579 m_items.(n)
4583 method add_narrow_pattern pattern =
4584 m_narrow_patterns <- pattern :: m_narrow_patterns
4586 method del_narrow_pattern =
4587 match m_narrow_patterns with
4588 | _ :: rest -> m_narrow_patterns <- rest
4589 | [] -> ()
4591 method renarrow =
4592 self#denarrow;
4593 match m_narrow_patterns with
4594 | pattern :: [] -> self#narrow pattern; pattern
4595 | list ->
4596 List.fold_left (fun accu pattern ->
4597 self#narrow pattern;
4598 pattern ^ "@Uellipsis" ^ accu) E.s list
4600 method calcactive anchor =
4601 let rely = getanchory anchor in
4602 let rec loop n best bestd =
4603 if n = Array.length m_items
4604 then best
4605 else
4606 let _, _, kind = m_items.(n) in
4607 match kind with
4608 | Oanchor anchor ->
4609 let orely = getanchory anchor in
4610 let d = abs (orely - rely) in
4611 if d < bestd
4612 then loop (n+1) n d
4613 else loop (n+1) best bestd
4614 | Onone | Oremote _ | Olaunch _
4615 | Oremotedest _ | Ouri _ | Ohistory _ | Oaction _ ->
4616 loop (n+1) best bestd
4618 loop 0 ~-1 max_int
4620 method reset anchor items =
4621 m_hadremovals <- false;
4622 if state.gen != m_gen
4623 then (
4624 m_orig_items <- items;
4625 m_items <- items;
4626 m_narrow_patterns <- [];
4627 m_minfo <- E.a;
4628 m_orig_minfo <- E.a;
4629 m_gen <- state.gen;
4631 else (
4632 if items != m_orig_items
4633 then (
4634 m_orig_items <- items;
4635 if m_narrow_patterns == []
4636 then m_items <- items;
4639 let active = self#calcactive anchor in
4640 m_active <- active;
4641 m_first <- firstof m_first active
4642 end)
4645 let enterselector sourcetype =
4646 resetmstate ();
4647 let source = outlinesource sourcetype in
4648 fun errmsg ->
4649 let outlines =
4650 match sourcetype with
4651 | `bookmarks -> Array.of_list state.bookmarks
4652 | `outlines -> state.outlines
4653 | `history -> genhistoutlines !Config.historder
4655 if Array.length outlines = 0
4656 then (
4657 showtext ' ' errmsg;
4659 else (
4660 state.text <- source#greetmsg;
4661 Wsi.setcursor Wsi.CURSOR_INHERIT;
4662 let anchor = getanchor () in
4663 source#reset anchor outlines;
4664 state.uioh <-
4665 coe (new outlinelistview ~zebra:(sourcetype=`history) ~source);
4666 G.postRedisplay "enter selector";
4670 let enteroutlinemode =
4671 let f = enterselector `outlines in
4672 fun () -> f "Document has no outline";
4675 let enterbookmarkmode =
4676 let f = enterselector `bookmarks in
4677 fun () -> f "Document has no bookmarks (yet)";
4680 let enterhistmode () = enterselector `history "No history (yet)";;
4682 let quickbookmark ?title () =
4683 match state.layout with
4684 | [] -> ()
4685 | l :: _ ->
4686 let title =
4687 match title with
4688 | None ->
4689 let tm = Unix.localtime (now ()) in
4690 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4691 (l.pageno+1)
4692 tm.Unix.tm_mday
4693 tm.Unix.tm_mon
4694 (tm.Unix.tm_year + 1900)
4695 tm.Unix.tm_hour
4696 tm.Unix.tm_min
4697 | Some title -> title
4699 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4702 let setautoscrollspeed step goingdown =
4703 let incr = max 1 ((abs step) / 2) in
4704 let incr = if goingdown then incr else -incr in
4705 let astep = boundastep state.winh (step + incr) in
4706 state.autoscroll <- Some astep;
4709 let canpan () =
4710 match conf.columns with
4711 | Csplit _ -> true
4712 | Csingle _ | Cmulti _ -> state.x != 0 || conf.zoom > 1.0
4715 let panbound x = bound x (-state.w) (wadjsb () + state.winw);;
4717 let existsinrow pageno (columns, coverA, coverB) p =
4718 let last = ((pageno - coverA) mod columns) + columns in
4719 let rec any = function
4720 | [] -> false
4721 | l :: rest ->
4722 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4723 then p l
4724 else (
4725 if not (p l)
4726 then (if l.pageno = last then false else any rest)
4727 else true
4730 any state.layout
4733 let nextpage () =
4734 match state.layout with
4735 | [] ->
4736 let pageno = page_of_y state.y in
4737 gotoghyll (getpagey (pageno+1))
4738 | l :: rest ->
4739 match conf.columns with
4740 | Csingle _ ->
4741 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4742 then
4743 let y = clamp (pgscale state.winh) in
4744 gotoghyll y
4745 else
4746 let pageno = min (l.pageno+1) (state.pagecount-1) in
4747 gotoghyll (getpagey pageno)
4748 | Cmulti ((c, _, _) as cl, _) ->
4749 if conf.presentation
4750 && (existsinrow l.pageno cl
4751 (fun l -> l.pageh > l.pagey + l.pagevh))
4752 then
4753 let y = clamp (pgscale state.winh) in
4754 gotoghyll y
4755 else
4756 let pageno = min (l.pageno+c) (state.pagecount-1) in
4757 gotoghyll (getpagey pageno)
4758 | Csplit (n, _) ->
4759 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4760 then
4761 let pagey, pageh = getpageyh l.pageno in
4762 let pagey = pagey + pageh * l.pagecol in
4763 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4764 gotoghyll (pagey + pageh + ips)
4767 let prevpage () =
4768 match state.layout with
4769 | [] ->
4770 let pageno = page_of_y state.y in
4771 gotoghyll (getpagey (pageno-1))
4772 | l :: _ ->
4773 match conf.columns with
4774 | Csingle _ ->
4775 if conf.presentation && l.pagey != 0
4776 then
4777 gotoghyll (clamp (pgscale ~-(state.winh)))
4778 else
4779 let pageno = max 0 (l.pageno-1) in
4780 gotoghyll (getpagey pageno)
4781 | Cmulti ((c, _, coverB) as cl, _) ->
4782 if conf.presentation &&
4783 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4784 then
4785 gotoghyll (clamp (pgscale ~-(state.winh)))
4786 else
4787 let decr =
4788 if l.pageno = state.pagecount - coverB
4789 then 1
4790 else c
4792 let pageno = max 0 (l.pageno-decr) in
4793 gotoghyll (getpagey pageno)
4794 | Csplit (n, _) ->
4795 let y =
4796 if l.pagecol = 0
4797 then
4798 if l.pageno = 0
4799 then l.pagey
4800 else
4801 let pageno = max 0 (l.pageno-1) in
4802 let pagey, pageh = getpageyh pageno in
4803 pagey + (n-1)*pageh
4804 else
4805 let pagey, pageh = getpageyh l.pageno in
4806 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4808 gotoghyll y
4811 let save () =
4812 if emptystr conf.savecmd
4813 then error "don't know where to save modified document"
4814 else
4815 let savecmd = Str.global_replace percentsre state.path conf.savecmd in
4816 let path =
4817 getcmdoutput
4818 (fun s -> error "failed to obtain path to the saved copy: %s" s)
4819 savecmd
4821 if not (emptystr path)
4822 then
4823 let tmp = path ^ ".tmp" in
4824 savedoc tmp;
4825 Unix.rename tmp path;
4828 let viewkeyboard key mask =
4829 let enttext te =
4830 let mode = state.mode in
4831 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4832 state.text <- E.s;
4833 enttext ();
4834 G.postRedisplay "view:enttext"
4836 let ctrl = Wsi.withctrl mask in
4837 let key =
4838 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
4840 match key with
4841 | @Q -> exit 0
4843 | @W ->
4844 if hasunsavedchanges ()
4845 then save ()
4847 | @insert ->
4848 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4849 then (
4850 state.mode <- LinkNav (Ltgendir 0);
4851 gotoy state.y;
4853 else showtext '!' "Keyboard link navigation does not work under rotation"
4855 | @escape | @q ->
4856 begin match state.mstate with
4857 | Mzoomrect _ ->
4858 resetmstate ();
4859 G.postRedisplay "kill rect";
4860 | Msel _
4861 | Mpan _
4862 | Mscrolly | Mscrollx
4863 | Mzoom _
4864 | Mnone ->
4865 begin match state.mode with
4866 | LinkNav _ ->
4867 state.mode <- View;
4868 G.postRedisplay "esc leave linknav"
4869 | Birdseye _
4870 | Textentry _
4871 | View ->
4872 match state.ranchors with
4873 | [] -> raise Quit
4874 | (path, password, anchor, origin) :: rest ->
4875 state.ranchors <- rest;
4876 state.anchor <- anchor;
4877 state.origin <- origin;
4878 state.nameddest <- E.s;
4879 opendoc path password
4880 end;
4881 end;
4883 | @backspace ->
4884 gotoghyll (getnav ~-1)
4886 | @o ->
4887 enteroutlinemode ()
4889 | @H ->
4890 enterhistmode ()
4892 | @u ->
4893 state.rects <- [];
4894 state.text <- E.s;
4895 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap;
4896 G.postRedisplay "dehighlight";
4898 | @slash | @question ->
4899 let ondone isforw s =
4900 cbput state.hists.pat s;
4901 state.searchpattern <- s;
4902 search s isforw
4904 let s = String.make 1 (Char.chr key) in
4905 enttext (s, E.s, Some (onhist state.hists.pat),
4906 textentry, ondone (key = @slash), true)
4908 | @plus | @kpplus | @equals when ctrl ->
4909 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4910 setzoom (conf.zoom +. incr)
4912 | @plus | @kpplus ->
4913 let ondone s =
4914 let n =
4915 try int_of_string s with exc ->
4916 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
4917 max_int
4919 if n != max_int
4920 then (
4921 conf.pagebias <- n;
4922 state.text <- "page bias is now " ^ string_of_int n;
4925 enttext ("page bias: ", E.s, None, intentry, ondone, true)
4927 | @minus | @kpminus when ctrl ->
4928 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4929 setzoom (max 0.01 (conf.zoom -. decr))
4931 | @minus | @kpminus ->
4932 let ondone msg = state.text <- msg in
4933 enttext (
4934 "option [acfhilpstvxACFPRSZTISM]: ", E.s, None,
4935 optentry state.mode, ondone, true
4938 | @0 when ctrl ->
4939 if conf.zoom = 1.0
4940 then (
4941 state.x <- 0;
4942 gotoy state.y
4944 else setzoom 1.0
4946 | (@1 | @2) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
4947 let cols =
4948 match conf.columns with
4949 | Csingle _ | Cmulti _ -> 1
4950 | Csplit (n, _) -> n
4952 let h = state.winh -
4953 conf.interpagespace lsl (if conf.presentation then 1 else 0)
4955 let zoom = zoomforh state.winw h (vscrollw ()) cols in
4956 if zoom > 0.0 && (key = 50 || zoom < 1.0)
4957 then setzoom zoom
4959 | @3 when ctrl ->
4960 let fm =
4961 match conf.fitmodel with
4962 | FitWidth -> FitProportional
4963 | FitProportional -> FitPage
4964 | FitPage -> FitWidth
4966 state.text <- "fit model: " ^ FMTE.to_string fm;
4967 reqlayout conf.angle fm
4969 | @F9 ->
4970 togglebirdseye ()
4972 | @9 when ctrl ->
4973 togglebirdseye ()
4975 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4976 when not ctrl -> (* 0..9 *)
4977 let ondone s =
4978 let n =
4979 try int_of_string s with exc ->
4980 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
4983 if n >= 0
4984 then (
4985 addnav ();
4986 cbput state.hists.pag (string_of_int n);
4987 gotopage1 (n + conf.pagebias - 1) 0;
4990 let pageentry text key =
4991 match Char.unsafe_chr key with
4992 | 'g' -> TEdone text
4993 | _ -> intentry text key
4995 let text = String.make 1 (Char.chr key) in
4996 enttext (":", text, Some (onhist state.hists.pag),
4997 pageentry, ondone, true)
4999 | @b ->
5000 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
5001 reshape state.winw state.winh;
5003 | @B ->
5004 state.bzoom <- not state.bzoom;
5005 state.rects <- [];
5006 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
5008 | @l ->
5009 conf.hlinks <- not conf.hlinks;
5010 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5011 G.postRedisplay "toggle highlightlinks";
5013 | @F ->
5014 state.glinks <- true;
5015 let mode = state.mode in
5016 state.mode <- Textentry (
5017 (":", E.s, None, linknentry, linkndone gotounder, false),
5018 (fun _ ->
5019 state.glinks <- false;
5020 state.mode <- mode)
5022 state.text <- E.s;
5023 G.postRedisplay "view:linkent(F)"
5025 | @y ->
5026 state.glinks <- true;
5027 let mode = state.mode in
5028 state.mode <- Textentry (
5030 ":", E.s, None, linknentry, linkndone (fun under ->
5031 selstring (undertext under);
5032 ), false
5034 fun _ ->
5035 state.glinks <- false;
5036 state.mode <- mode
5038 state.text <- E.s;
5039 G.postRedisplay "view:linkent"
5041 | @a ->
5042 begin match state.autoscroll with
5043 | Some step ->
5044 conf.autoscrollstep <- step;
5045 state.autoscroll <- None
5046 | None ->
5047 if conf.autoscrollstep = 0
5048 then state.autoscroll <- Some 1
5049 else state.autoscroll <- Some conf.autoscrollstep
5052 | @p when ctrl ->
5053 launchpath ()
5055 | @P ->
5056 setpresentationmode (not conf.presentation);
5057 showtext ' ' ("presentation mode " ^
5058 if conf.presentation then "on" else "off");
5060 | @f ->
5061 if List.mem Wsi.Fullscreen state.winstate
5062 then Wsi.reshape conf.cwinw conf.cwinh
5063 else Wsi.fullscreen ()
5065 | @p | @N ->
5066 search state.searchpattern false
5068 | @n | @F3 ->
5069 search state.searchpattern true
5071 | @t ->
5072 begin match state.layout with
5073 | [] -> ()
5074 | l :: _ ->
5075 gotoghyll (getpagey l.pageno)
5078 | @space ->
5079 nextpage ()
5081 | @delete | @kpdelete -> (* delete *)
5082 prevpage ()
5084 | @equals ->
5085 showtext ' ' (describe_location ());
5087 | @w ->
5088 begin match state.layout with
5089 | [] -> ()
5090 | l :: _ ->
5091 Wsi.reshape (l.pagew + vscrollw ()) l.pageh;
5092 G.postRedisplay "w"
5095 | @apos ->
5096 enterbookmarkmode ()
5098 | @h | @F1 ->
5099 enterhelpmode ()
5101 | @i ->
5102 enterinfomode ()
5104 | @e when Buffer.length state.errmsgs > 0 ->
5105 entermsgsmode ()
5107 | @m ->
5108 let ondone s =
5109 match state.layout with
5110 | l :: _ ->
5111 if nonemptystr s
5112 then
5113 state.bookmarks <-
5114 (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
5115 | _ -> ()
5117 enttext ("bookmark: ", E.s, None, textentry, ondone, true)
5119 | @tilde ->
5120 quickbookmark ();
5121 showtext ' ' "Quick bookmark added";
5123 | @z ->
5124 begin match state.layout with
5125 | l :: _ ->
5126 let rect = getpdimrect l.pagedimno in
5127 let w, h =
5128 if conf.crophack
5129 then
5130 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5131 truncate (1.2 *. (rect.(3) -. rect.(0))))
5132 else
5133 (truncate (rect.(1) -. rect.(0)),
5134 truncate (rect.(3) -. rect.(0)))
5136 let w = truncate ((float w)*.conf.zoom)
5137 and h = truncate ((float h)*.conf.zoom) in
5138 if w != 0 && h != 0
5139 then (
5140 state.anchor <- getanchor ();
5141 Wsi.reshape (w + vscrollw ()) (h + conf.interpagespace)
5143 G.postRedisplay "z";
5145 | [] -> ()
5148 | @x -> state.roam ()
5150 | @Lt | @Gt ->
5151 reqlayout (conf.angle +
5152 (if key = @Gt then 30 else -30)) conf.fitmodel
5154 | @Lb | @Rb ->
5155 conf.colorscale <-
5156 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5158 G.postRedisplay "brightness";
5160 | @c when state.mode = View ->
5161 if Wsi.withalt mask
5162 then (
5163 if conf.zoom > 1.0
5164 then
5165 let m = (wadjsb () + state.winw - state.w) / 2 in
5166 state.x <- m;
5167 gotoy_and_clear_text state.y
5169 else
5170 let (c, a, b), z =
5171 match state.prevcolumns with
5172 | None -> (1, 0, 0), 1.0
5173 | Some (columns, z) ->
5174 let cab =
5175 match columns with
5176 | Csplit (c, _) -> -c, 0, 0
5177 | Cmulti ((c, a, b), _) -> c, a, b
5178 | Csingle _ -> 1, 0, 0
5180 cab, z
5182 setcolumns View c a b;
5183 setzoom z
5185 | @down | @up when ctrl && Wsi.withshift mask ->
5186 let zoom, x = state.prevzoom in
5187 setzoom zoom;
5188 state.x <- x;
5190 | @k | @up | @kpup ->
5191 begin match state.autoscroll with
5192 | None ->
5193 begin match state.mode with
5194 | Birdseye beye -> upbirdseye 1 beye
5195 | Textentry _
5196 | View
5197 | LinkNav _ ->
5198 if ctrl
5199 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5200 else (
5201 if not (Wsi.withshift mask) && conf.presentation
5202 then prevpage ()
5203 else gotoghyll1 true (clamp (-conf.scrollstep))
5206 | Some n ->
5207 setautoscrollspeed n false
5210 | @j | @down | @kpdown ->
5211 begin match state.autoscroll with
5212 | None ->
5213 begin match state.mode with
5214 | Birdseye beye -> downbirdseye 1 beye
5215 | Textentry _
5216 | View
5217 | LinkNav _ ->
5218 if ctrl
5219 then gotoy_and_clear_text (clamp (state.winh/2))
5220 else (
5221 if not (Wsi.withshift mask) && conf.presentation
5222 then nextpage ()
5223 else gotoghyll1 true (clamp (conf.scrollstep))
5226 | Some n ->
5227 setautoscrollspeed n true
5230 | @left | @right | @kpleft | @kpright when not (Wsi.withalt mask) ->
5231 if canpan ()
5232 then
5233 let dx =
5234 if ctrl
5235 then state.winw / 2
5236 else conf.hscrollstep
5238 let dx = if key = @left || key = @kpleft then dx else -dx in
5239 state.x <- panbound (state.x + dx);
5240 gotoy_and_clear_text state.y
5241 else (
5242 state.text <- E.s;
5243 G.postRedisplay "left/right"
5246 | @prior | @kpprior ->
5247 let y =
5248 if ctrl
5249 then
5250 match state.layout with
5251 | [] -> state.y
5252 | l :: _ -> state.y - l.pagey
5253 else
5254 clamp (pgscale (-state.winh))
5256 gotoghyll y
5258 | @next | @kpnext ->
5259 let y =
5260 if ctrl
5261 then
5262 match List.rev state.layout with
5263 | [] -> state.y
5264 | l :: _ -> getpagey l.pageno
5265 else
5266 clamp (pgscale state.winh)
5268 gotoghyll y
5270 | @g | @home | @kphome ->
5271 addnav ();
5272 gotoghyll 0
5273 | @G | @jend | @kpend ->
5274 addnav ();
5275 gotoghyll (clamp state.maxy)
5277 | @right | @kpright when Wsi.withalt mask ->
5278 gotoghyll (getnav 1)
5279 | @left | @kpleft when Wsi.withalt mask ->
5280 gotoghyll (getnav ~-1)
5282 | @r ->
5283 reload ()
5285 | @v when conf.debug ->
5286 state.rects <- [];
5287 List.iter (fun l ->
5288 match getopaque l.pageno with
5289 | None -> ()
5290 | Some opaque ->
5291 let x0, y0, x1, y1 = pagebbox opaque in
5292 let a,b = float x0, float y0 in
5293 let c,d = float x1, float y0 in
5294 let e,f = float x1, float y1 in
5295 let h,j = float x0, float y1 in
5296 let rect = (a,b,c,d,e,f,h,j) in
5297 debugrect rect;
5298 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5299 ) state.layout;
5300 G.postRedisplay "v";
5302 | @pipe ->
5303 let mode = state.mode in
5304 let cmd = ref E.s in
5305 let onleave = function
5306 | Cancel -> state.mode <- mode
5307 | Confirm ->
5308 List.iter (fun l ->
5309 match getopaque l.pageno with
5310 | Some opaque -> pipesel opaque !cmd
5311 | None -> ()) state.layout;
5312 state.mode <- mode
5314 let ondone s =
5315 cbput state.hists.sel s;
5316 cmd := s
5318 let te =
5319 "| ", !cmd, Some (onhist state.hists.sel), textentry, ondone, true
5321 G.postRedisplay "|";
5322 state.mode <- Textentry (te, onleave);
5324 | _ ->
5325 vlog "huh? %s" (Wsi.keyname key)
5328 let linknavkeyboard key mask linknav =
5329 let getpage pageno =
5330 let rec loop = function
5331 | [] -> None
5332 | l :: _ when l.pageno = pageno -> Some l
5333 | _ :: rest -> loop rest
5334 in loop state.layout
5336 let doexact (pageno, n) =
5337 match getopaque pageno, getpage pageno with
5338 | Some opaque, Some l ->
5339 if key = @enter || key = @kpenter
5340 then
5341 let under = getlink opaque n in
5342 G.postRedisplay "link gotounder";
5343 gotounder under;
5344 state.mode <- View;
5345 else
5346 let opt, dir =
5347 match key with
5348 | @home ->
5349 Some (findlink opaque LDfirst), -1
5351 | @jend ->
5352 Some (findlink opaque LDlast), 1
5354 | @left ->
5355 Some (findlink opaque (LDleft n)), -1
5357 | @right ->
5358 Some (findlink opaque (LDright n)), 1
5360 | @up ->
5361 Some (findlink opaque (LDup n)), -1
5363 | @down ->
5364 Some (findlink opaque (LDdown n)), 1
5366 | _ -> None, 0
5368 let pwl l dir =
5369 begin match findpwl l.pageno dir with
5370 | Pwlnotfound -> ()
5371 | Pwl pageno ->
5372 let notfound dir =
5373 state.mode <- LinkNav (Ltgendir dir);
5374 let y, h = getpageyh pageno in
5375 let y =
5376 if dir < 0
5377 then y + h - state.winh
5378 else y
5380 gotoy y
5382 begin match getopaque pageno, getpage pageno with
5383 | Some opaque, Some _ ->
5384 let link =
5385 let ld = if dir > 0 then LDfirst else LDlast in
5386 findlink opaque ld
5388 begin match link with
5389 | Lfound m ->
5390 showlinktype (getlink opaque m);
5391 state.mode <- LinkNav (Ltexact (pageno, m));
5392 G.postRedisplay "linknav jpage";
5393 | Lnotfound -> notfound dir
5394 end;
5395 | _ -> notfound dir
5396 end;
5397 end;
5399 begin match opt with
5400 | Some Lnotfound -> pwl l dir;
5401 | Some (Lfound m) ->
5402 if m = n
5403 then pwl l dir
5404 else (
5405 let _, y0, _, y1 = getlinkrect opaque m in
5406 if y0 < l.pagey
5407 then gotopage1 l.pageno y0
5408 else (
5409 let d = fstate.fontsize + 1 in
5410 if y1 - l.pagey > l.pagevh - d
5411 then gotopage1 l.pageno (y1 - state.winh - hscrollh () + d)
5412 else G.postRedisplay "linknav";
5414 showlinktype (getlink opaque m);
5415 state.mode <- LinkNav (Ltexact (l.pageno, m));
5418 | None -> viewkeyboard key mask
5419 end;
5420 | _ -> viewkeyboard key mask
5422 if key = @insert
5423 then (
5424 state.mode <- View;
5425 G.postRedisplay "leave linknav"
5427 else
5428 match linknav with
5429 | Ltgendir _ | Ltnotready _ -> viewkeyboard key mask
5430 | Ltexact exact -> doexact exact
5433 let keyboard key mask =
5434 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5435 then wcmd "interrupt"
5436 else state.uioh <- state.uioh#key key mask
5439 let birdseyekeyboard key mask
5440 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5441 let incr =
5442 match conf.columns with
5443 | Csingle _ -> 1
5444 | Cmulti ((c, _, _), _) -> c
5445 | Csplit _ -> failwith "bird's eye split mode"
5447 let pgh layout = List.fold_left
5448 (fun m l -> max l.pageh m) state.winh layout in
5449 match key with
5450 | @l when Wsi.withctrl mask ->
5451 let y, h = getpageyh pageno in
5452 let top = (state.winh - h) / 2 in
5453 gotoy (max 0 (y - top))
5454 | @enter | @kpenter -> leavebirdseye beye false
5455 | @escape -> leavebirdseye beye true
5456 | @up -> upbirdseye incr beye
5457 | @down -> downbirdseye incr beye
5458 | @left -> upbirdseye 1 beye
5459 | @right -> downbirdseye 1 beye
5461 | @prior ->
5462 begin match state.layout with
5463 | l :: _ ->
5464 if l.pagey != 0
5465 then (
5466 state.mode <- Birdseye (
5467 oconf, leftx, l.pageno, hooverpageno, anchor
5469 gotopage1 l.pageno 0;
5471 else (
5472 let layout = layout (state.y-state.winh) (pgh state.layout) in
5473 match layout with
5474 | [] -> gotoy (clamp (-state.winh))
5475 | l :: _ ->
5476 state.mode <- Birdseye (
5477 oconf, leftx, l.pageno, hooverpageno, anchor
5479 gotopage1 l.pageno 0
5482 | [] -> gotoy (clamp (-state.winh))
5483 end;
5485 | @next ->
5486 begin match List.rev state.layout with
5487 | l :: _ ->
5488 let layout = layout (state.y + (pgh state.layout)) state.winh in
5489 begin match layout with
5490 | [] ->
5491 let incr = l.pageh - l.pagevh in
5492 if incr = 0
5493 then (
5494 state.mode <-
5495 Birdseye (
5496 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5498 G.postRedisplay "birdseye pagedown";
5500 else gotoy (clamp (incr + conf.interpagespace*2));
5502 | l :: _ ->
5503 state.mode <-
5504 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5505 gotopage1 l.pageno 0;
5508 | [] -> gotoy (clamp state.winh)
5509 end;
5511 | @home ->
5512 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5513 gotopage1 0 0
5515 | @jend ->
5516 let pageno = state.pagecount - 1 in
5517 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5518 if not (pagevisible state.layout pageno)
5519 then
5520 let h =
5521 match List.rev state.pdims with
5522 | [] -> state.winh
5523 | (_, _, h, _) :: _ -> h
5525 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5526 else G.postRedisplay "birdseye end";
5528 | _ -> viewkeyboard key mask
5531 let drawpage l =
5532 let color =
5533 match state.mode with
5534 | Textentry _ -> scalecolor 0.4
5535 | LinkNav _
5536 | View -> scalecolor 1.0
5537 | Birdseye (_, _, pageno, hooverpageno, _) ->
5538 if l.pageno = hooverpageno
5539 then scalecolor 0.9
5540 else (
5541 if l.pageno = pageno
5542 then (
5543 let c = scalecolor 1.0 in
5544 GlDraw.color c;
5545 GlDraw.line_width 3.0;
5546 let dispx = xadjsb () + l.pagedispx in
5547 linerect
5548 (float (dispx-1)) (float (l.pagedispy-1))
5549 (float (dispx+l.pagevw+1))
5550 (float (l.pagedispy+l.pagevh+1))
5552 GlDraw.line_width 1.0;
5555 else scalecolor 0.8
5558 drawtiles l color;
5561 let postdrawpage l linkindexbase =
5562 match getopaque l.pageno with
5563 | Some opaque ->
5564 if tileready l l.pagex l.pagey
5565 then
5566 let x = l.pagedispx - l.pagex + xadjsb ()
5567 and y = l.pagedispy - l.pagey in
5568 let hlmask =
5569 match conf.columns with
5570 | Csingle _ | Cmulti _ ->
5571 (if conf.hlinks then 1 else 0)
5572 + (if state.glinks
5573 && not (isbirdseye state.mode) then 2 else 0)
5574 | Csplit _ -> 0
5576 let s =
5577 match state.mode with
5578 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5579 | Textentry _
5580 | Birdseye _
5581 | View
5582 | LinkNav _ -> E.s
5584 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5585 else 0
5586 | _ -> 0
5589 let scrollindicator () =
5590 let sbw, ph, sh = state.uioh#scrollph in
5591 let sbh, pw, sw = state.uioh#scrollpw in
5593 let x0,x1,hx0 =
5594 if conf.leftscroll
5595 then (0, sbw, sbw)
5596 else ((state.winw - sbw), state.winw, 0)
5599 GlDraw.color (0.64, 0.64, 0.64);
5600 filledrect (float x0) 0. (float x1) (float state.winh);
5601 filledrect
5602 (float hx0) (float (state.winh - sbh))
5603 (float (hx0 + wadjsb () + state.winw)) (float state.winh)
5605 GlDraw.color (0.0, 0.0, 0.0);
5607 filledrect (float x0) ph (float x1) (ph +. sh);
5608 let pw = pw +. float hx0 in
5609 filledrect pw (float (state.winh - sbh)) (pw +. sw) (float state.winh);
5612 let showsel () =
5613 match state.mstate with
5614 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5617 | Msel ((x0, y0), (x1, y1)) ->
5618 let identify opaque l px py = Some (opaque, l.pageno, px, py) in
5619 let o0,n0,px0,py0 = onppundermouse identify x0 y0 (~< E.s, -1, 0, 0) in
5620 let _o1,n1,px1,py1 = onppundermouse identify x1 y1 (~< E.s, -1, 0, 0) in
5621 if n0 != -1 && n0 = n1 then seltext o0 (px0, py0, px1, py1);
5624 let showrects = function [] -> () | rects ->
5625 Gl.enable `blend;
5626 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5627 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5628 List.iter
5629 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5630 List.iter (fun l ->
5631 if l.pageno = pageno
5632 then (
5633 let dx = float (l.pagedispx - l.pagex) in
5634 let dy = float (l.pagedispy - l.pagey) in
5635 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5636 Raw.sets_float state.vraw ~pos:0
5637 [| x0+.dx; y0+.dy;
5638 x1+.dx; y1+.dy;
5639 x3+.dx; y3+.dy;
5640 x2+.dx; y2+.dy |];
5641 GlArray.vertex `two state.vraw;
5642 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
5644 ) state.layout
5645 ) rects
5647 Gl.disable `blend;
5650 let display () =
5651 GlClear.color (scalecolor2 conf.bgcolor);
5652 GlClear.clear [`color];
5653 List.iter drawpage state.layout;
5654 let rects =
5655 match state.mode with
5656 | LinkNav (Ltexact (pageno, linkno)) ->
5657 begin match getopaque pageno with
5658 | Some opaque ->
5659 let dx = xadjsb () in
5660 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5661 let x0 = x0 + dx and x1 = x1 + dx in
5662 (pageno, 5, (
5663 float x0, float y0,
5664 float x1, float y0,
5665 float x1, float y1,
5666 float x0, float y1)
5667 ) :: state.rects
5668 | None -> state.rects
5670 | LinkNav (Ltgendir _) | LinkNav (Ltnotready _)
5671 | Birdseye _
5672 | Textentry _
5673 | View -> state.rects
5675 showrects rects;
5676 let rec postloop linkindexbase = function
5677 | l :: rest ->
5678 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5679 postloop linkindexbase rest
5680 | [] -> ()
5682 showsel ();
5683 postloop 0 state.layout;
5684 state.uioh#display;
5685 begin match state.mstate with
5686 | Mzoomrect ((x0, y0), (x1, y1)) ->
5687 Gl.enable `blend;
5688 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5689 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5690 filledrect (float x0) (float y0) (float x1) (float y1);
5691 Gl.disable `blend;
5692 | Msel _
5693 | Mpan _
5694 | Mscrolly | Mscrollx
5695 | Mzoom _
5696 | Mnone -> ()
5697 end;
5698 enttext ();
5699 scrollindicator ();
5700 Wsi.swapb ();
5703 let zoomrect x y x1 y1 =
5704 let x0 = min x x1
5705 and x1 = max x x1
5706 and y0 = min y y1 in
5707 gotoy (state.y + y0);
5708 state.anchor <- getanchor ();
5709 let zoom = (float state.w) /. float (x1 - x0) in
5710 let margin =
5711 let simple () =
5712 let adjw = wadjsb () + state.winw in
5713 if state.w < adjw
5714 then (adjw - state.w) / 2
5715 else 0
5717 match conf.fitmodel with
5718 | FitWidth | FitProportional -> simple ()
5719 | FitPage ->
5720 match conf.columns with
5721 | Csplit _ ->
5722 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
5723 | Cmulti _ | Csingle _ -> simple ()
5725 state.x <- (state.x + margin) - x0;
5726 setzoom zoom;
5727 resetmstate ();
5730 let annot inline x y =
5731 match unproject x y with
5732 | Some (opaque, n, ux, uy) ->
5733 let add text =
5734 addannot opaque ux uy text;
5735 wcmd "freepage %s" (~> opaque);
5736 Hashtbl.remove state.pagemap (n, state.gen);
5737 flushtiles ();
5738 gotoy state.y
5740 if inline
5741 then
5742 let ondone s = add s in
5743 let mode = state.mode in
5744 state.mode <- Textentry (
5745 ("annotation: ", E.s, None, textentry, ondone, true),
5746 fun _ -> state.mode <- mode);
5747 state.text <- E.s;
5748 enttext ();
5749 G.postRedisplay "annot"
5750 else
5751 let text =
5752 let s = getusertext E.s in
5753 let l = Str.split newlinere s in
5754 String.concat " " l
5756 add text
5757 | _ -> ()
5760 let zoomblock x y =
5761 let g opaque l px py =
5762 match rectofblock opaque px py with
5763 | Some a ->
5764 let x0 = a.(0) -. 20. in
5765 let x1 = a.(1) +. 20. in
5766 let y0 = a.(2) -. 20. in
5767 let zoom = (float state.w) /. (x1 -. x0) in
5768 let pagey = getpagey l.pageno in
5769 gotoy_and_clear_text (pagey + truncate y0);
5770 state.anchor <- getanchor ();
5771 let margin = (state.w - l.pagew)/2 in
5772 state.x <- -truncate x0 - margin;
5773 setzoom zoom;
5774 None
5775 | None -> None
5777 match conf.columns with
5778 | Csplit _ ->
5779 showtext '!' "block zooming does not work properly in split columns mode"
5780 | Cmulti _ | Csingle _ -> onppundermouse g x y ()
5783 let scrollx x =
5784 let winw = wadjsb () + state.winw - 1 in
5785 let s = float x /. float winw in
5786 let destx = truncate (float (state.w + winw) *. s) in
5787 state.x <- winw - destx;
5788 gotoy_and_clear_text state.y;
5789 state.mstate <- Mscrollx;
5792 let scrolly y =
5793 let s = float y /. float state.winh in
5794 let desty = truncate (float (state.maxy - state.winh) *. s) in
5795 gotoy_and_clear_text desty;
5796 state.mstate <- Mscrolly;
5799 let viewmulticlick clicks x y mask =
5800 let g opaque l px py =
5801 let mark =
5802 match clicks with
5803 | 2 -> Mark_word
5804 | 3 -> Mark_line
5805 | 4 -> Mark_block
5806 | _ -> Mark_page
5808 if markunder opaque px py mark
5809 then (
5810 Some (fun () ->
5811 let dopipe cmd =
5812 match getopaque l.pageno with
5813 | None -> ()
5814 | Some opaque -> pipesel opaque cmd
5816 state.roam <- (fun () -> dopipe conf.paxcmd);
5817 if not (Wsi.withctrl mask) then dopipe conf.selcmd;
5820 else None
5822 G.postRedisplay "viewmulticlick";
5823 onppundermouse g x y (fun () -> showtext '!' "Nothing to select") ();
5826 let canselect () =
5827 match conf.columns with
5828 | Csplit _ -> false
5829 | Csingle _ | Cmulti _ -> conf.angle mod 360 = 0
5832 let viewmouse button down x y mask =
5833 match button with
5834 | n when (n == 4 || n == 5) && not down ->
5835 if Wsi.withctrl mask
5836 then (
5837 match state.mstate with
5838 | Mzoom (oldn, i) ->
5839 if oldn = n
5840 then (
5841 if i = 2
5842 then
5843 let incr =
5844 match n with
5845 | 5 ->
5846 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5847 | _ ->
5848 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5850 let zoom = conf.zoom -. incr in
5851 setzoom zoom;
5852 state.mstate <- Mzoom (n, 0);
5853 else
5854 state.mstate <- Mzoom (n, i+1);
5856 else state.mstate <- Mzoom (n, 0)
5858 | Msel _
5859 | Mpan _
5860 | Mscrolly | Mscrollx
5861 | Mzoomrect _
5862 | Mnone -> state.mstate <- Mzoom (n, 0)
5864 else (
5865 match state.autoscroll with
5866 | Some step -> setautoscrollspeed step (n=4)
5867 | None ->
5868 if conf.wheelbypage || conf.presentation
5869 then (
5870 if n = 4
5871 then prevpage ()
5872 else nextpage ()
5874 else
5875 let incr =
5876 if n = 4
5877 then -conf.scrollstep
5878 else conf.scrollstep
5880 let incr = incr * 2 in
5881 let y = clamp incr in
5882 gotoy_and_clear_text y
5885 | n when (n = 6 || n = 7) && not down && canpan () ->
5886 state.x <-
5887 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
5888 gotoy_and_clear_text state.y
5890 | 1 when Wsi.withshift mask ->
5891 state.mstate <- Mnone;
5892 if not down
5893 then (
5894 match unproject x y with
5895 | Some (_, pageno, ux, uy) ->
5896 let cmd = Printf.sprintf
5897 "%s %s %d %d %d"
5898 conf.stcmd state.path pageno ux uy
5900 addpid @@ popen cmd []
5901 | None -> ()
5904 | 1 when Wsi.withctrl mask ->
5905 if down
5906 then (
5907 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5908 state.mstate <- Mpan (x, y)
5910 else
5911 state.mstate <- Mnone
5913 | 3 ->
5914 if down
5915 then (
5916 if Wsi.withshift mask
5917 then (
5918 annot conf.annotinline x y;
5919 G.postRedisplay "addannot"
5921 else
5922 let p = (x, y) in
5923 Wsi.setcursor Wsi.CURSOR_CYCLE;
5924 state.mstate <- Mzoomrect (p, p)
5926 else (
5927 match state.mstate with
5928 | Mzoomrect ((x0, y0), _) ->
5929 if abs (x-x0) > 10 && abs (y - y0) > 10
5930 then zoomrect x0 y0 x y
5931 else (
5932 resetmstate ();
5933 G.postRedisplay "kill accidental zoom rect";
5935 | Msel _
5936 | Mpan _
5937 | Mscrolly | Mscrollx
5938 | Mzoom _
5939 | Mnone ->
5940 resetmstate ()
5943 | 1 when x > state.winw - vscrollw () ->
5944 if down
5945 then
5946 let _, position, sh = state.uioh#scrollph in
5947 if y > truncate position && y < truncate (position +. sh)
5948 then state.mstate <- Mscrolly
5949 else scrolly y
5950 else
5951 state.mstate <- Mnone
5953 | 1 when y > state.winh - hscrollh () ->
5954 if down
5955 then
5956 let _, position, sw = state.uioh#scrollpw in
5957 if x > truncate position && x < truncate (position +. sw)
5958 then state.mstate <- Mscrollx
5959 else scrollx x
5960 else
5961 state.mstate <- Mnone
5963 | 1 when state.bzoom -> if not down then zoomblock x y
5965 | 1 ->
5966 let dest = if down then getunder x y else Unone in
5967 begin match dest with
5968 | Ulinkgoto _
5969 | Ulinkuri _
5970 | Uremote _ | Uremotedest _
5971 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5972 gotounder dest
5974 | Unone when down ->
5975 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5976 state.mstate <- Mpan (x, y);
5978 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
5980 | Unone | Utext _ ->
5981 if down
5982 then (
5983 if canselect ()
5984 then (
5985 state.mstate <- Msel ((x, y), (x, y));
5986 G.postRedisplay "mouse select";
5989 else (
5990 match state.mstate with
5991 | Mnone -> ()
5993 | Mzoom _ | Mscrollx | Mscrolly ->
5994 state.mstate <- Mnone
5996 | Mzoomrect ((x0, y0), _) ->
5997 zoomrect x0 y0 x y
5999 | Mpan _ ->
6000 Wsi.setcursor Wsi.CURSOR_INHERIT;
6001 state.mstate <- Mnone
6003 | Msel ((x0, y0), (x1, y1)) ->
6004 let rec loop = function
6005 | [] -> ()
6006 | l :: rest ->
6007 let inside =
6008 let a0 = l.pagedispy in
6009 let a1 = a0 + l.pagevh in
6010 let b0 = l.pagedispx in
6011 let b1 = b0 + l.pagevw in
6012 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
6013 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
6015 if inside
6016 then
6017 match getopaque l.pageno with
6018 | Some opaque ->
6019 let dosel cmd () =
6020 match Unix.pipe () with
6021 | exception exn ->
6022 showtext '!'
6023 (Printf.sprintf
6024 "can not create sel pipe: %s"
6025 (exntos exn));
6026 | (r, w) ->
6027 let clo what fd =
6028 Ne.clo fd (fun msg ->
6029 dolog "%s close failed: %s" what msg)
6031 let pid =
6032 try popen cmd [r, 0; w, -1]
6033 with exn ->
6034 dolog "can not execute %S: %s"
6035 cmd (exntos exn);
6038 if pid > 0
6039 then (
6040 copysel w opaque;
6041 G.postRedisplay "copysel";
6043 else clo "Msel pipe/w" w;
6044 clo "Msel pipe/r" r;
6046 dosel conf.selcmd ();
6047 state.roam <- dosel conf.paxcmd;
6048 | None -> ()
6049 else loop rest
6051 loop state.layout;
6052 resetmstate ();
6056 | _ -> ()
6059 let birdseyemouse button down x y mask
6060 (conf, leftx, _, hooverpageno, anchor) =
6061 match button with
6062 | 1 when down ->
6063 let rec loop = function
6064 | [] -> ()
6065 | l :: rest ->
6066 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6067 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6068 then (
6069 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
6071 else loop rest
6073 loop state.layout
6074 | 3 -> ()
6075 | _ -> viewmouse button down x y mask
6078 let uioh = object
6079 method display = ()
6081 method key key mask =
6082 begin match state.mode with
6083 | Textentry textentry -> textentrykeyboard key mask textentry
6084 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6085 | View -> viewkeyboard key mask
6086 | LinkNav linknav -> linknavkeyboard key mask linknav
6087 end;
6088 state.uioh
6090 method button button bstate x y mask =
6091 begin match state.mode with
6092 | LinkNav _
6093 | View -> viewmouse button bstate x y mask
6094 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6095 | Textentry _ -> ()
6096 end;
6097 state.uioh
6099 method multiclick clicks x y mask =
6100 begin match state.mode with
6101 | LinkNav _
6102 | View -> viewmulticlick clicks x y mask
6103 | Birdseye _
6104 | Textentry _ -> ()
6105 end;
6106 state.uioh
6108 method motion x y =
6109 begin match state.mode with
6110 | Textentry _ -> ()
6111 | View | Birdseye _ | LinkNav _ ->
6112 match state.mstate with
6113 | Mzoom _ | Mnone -> ()
6115 | Mpan (x0, y0) ->
6116 let dx = x - x0
6117 and dy = y0 - y in
6118 state.mstate <- Mpan (x, y);
6119 if canpan ()
6120 then state.x <- panbound (state.x + dx);
6121 let y = clamp dy in
6122 gotoy_and_clear_text y
6124 | Msel (a, _) ->
6125 state.mstate <- Msel (a, (x, y));
6126 G.postRedisplay "motion select";
6128 | Mscrolly ->
6129 let y = min state.winh (max 0 y) in
6130 scrolly y
6132 | Mscrollx ->
6133 let x = min state.winw (max 0 x) in
6134 scrollx x
6136 | Mzoomrect (p0, _) ->
6137 state.mstate <- Mzoomrect (p0, (x, y));
6138 G.postRedisplay "motion zoomrect";
6139 end;
6140 state.uioh
6142 method pmotion x y =
6143 begin match state.mode with
6144 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6145 let rec loop = function
6146 | [] ->
6147 if hooverpageno != -1
6148 then (
6149 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6150 G.postRedisplay "pmotion birdseye no hoover";
6152 | l :: rest ->
6153 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6154 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6155 then (
6156 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6157 G.postRedisplay "pmotion birdseye hoover";
6159 else loop rest
6161 loop state.layout
6163 | Textentry _ -> ()
6165 | LinkNav _
6166 | View ->
6167 match state.mstate with
6168 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ -> ()
6169 | Mnone ->
6170 updateunder x y;
6171 if canselect ()
6172 then
6173 match conf.pax with
6174 | None -> ()
6175 | Some r ->
6176 let past, _, _ = !r in
6177 let now = now () in
6178 let delta = now -. past in
6179 if delta > 0.01
6180 then paxunder x y
6181 else r := (now, x, y)
6182 end;
6183 state.uioh
6185 method infochanged _ = ()
6187 method scrollph =
6188 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6189 let p, h =
6190 if maxy = 0
6191 then 0.0, float state.winh
6192 else scrollph state.y maxy
6194 vscrollw (), p, h
6196 method scrollpw =
6197 let winw = wadjsb () + state.winw in
6198 let fwinw = float winw in
6199 let sw =
6200 let sw = fwinw /. float state.w in
6201 let sw = fwinw *. sw in
6202 max sw (float conf.scrollh)
6204 let position =
6205 let maxx = state.w + winw in
6206 let x = winw - state.x in
6207 let percent = float x /. float maxx in
6208 (fwinw -. sw) *. percent
6210 hscrollh (), position, sw
6212 method modehash =
6213 let modename =
6214 match state.mode with
6215 | LinkNav _ -> "links"
6216 | Textentry _ -> "textentry"
6217 | Birdseye _ -> "birdseye"
6218 | View -> "view"
6220 findkeyhash conf modename
6222 method eformsgs = true
6223 method alwaysscrolly = false
6224 end;;
6226 let adderrmsg src msg =
6227 Buffer.add_string state.errmsgs msg;
6228 state.newerrmsgs <- true;
6229 G.postRedisplay src
6232 let adderrfmt src fmt =
6233 Format.ksprintf (fun s -> adderrmsg src s) fmt;
6236 let ract cmds =
6237 let cl = splitatspace cmds in
6238 let scan s fmt f =
6239 try Scanf.sscanf s fmt f
6240 with exn ->
6241 adderrfmt "remote exec"
6242 "error processing '%S': %s\n" cmds (exntos exn)
6244 match cl with
6245 | "reload" :: [] -> reload ()
6246 | "goto" :: args :: [] ->
6247 scan args "%u %f %f"
6248 (fun pageno x y ->
6249 let cmd, _ = state.geomcmds in
6250 if emptystr cmd
6251 then gotopagexy pageno x y
6252 else
6253 let f prevf () =
6254 gotopagexy pageno x y;
6255 prevf ()
6257 state.reprf <- f state.reprf
6259 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
6260 | "gotor" :: args :: [] ->
6261 scan args "%S %u"
6262 (fun filename pageno -> gotounder (Uremote (filename, pageno)))
6263 | "gotord" :: args :: [] ->
6264 scan args "%S %S"
6265 (fun filename dest -> gotounder (Uremotedest (filename, dest)))
6266 | "rect" :: args :: [] ->
6267 scan args "%u %u %f %f %f %f"
6268 (fun pageno color x0 y0 x1 y1 ->
6269 onpagerect pageno (fun w h ->
6270 let _,w1,h1,_ = getpagedim pageno in
6271 let sw = float w1 /. float w
6272 and sh = float h1 /. float h in
6273 let x0s = x0 *. sw
6274 and x1s = x1 *. sw
6275 and y0s = y0 *. sh
6276 and y1s = y1 *. sh in
6277 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
6278 debugrect rect;
6279 state.rects <- (pageno, color, rect) :: state.rects;
6280 G.postRedisplay "rect";
6283 | "activatewin" :: [] -> Wsi.activatewin ()
6284 | "quit" :: [] -> raise Quit
6285 | _ ->
6286 adderrfmt "remote command"
6287 "error processing remote command: %S\n" cmds;
6290 let remote =
6291 let scratch = Bytes.create 80 in
6292 let buf = Buffer.create 80 in
6293 fun fd ->
6294 let rec tempfr () =
6295 try Some (Unix.read fd scratch 0 80)
6296 with
6297 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
6298 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
6299 | exn -> raise exn
6301 match tempfr () with
6302 | None -> Some fd
6303 | Some n ->
6304 if n = 0
6305 then (
6306 Unix.close fd;
6307 if Buffer.length buf > 0
6308 then (
6309 let s = Buffer.contents buf in
6310 Buffer.clear buf;
6311 ract s;
6313 None
6315 else
6316 let rec eat ppos =
6317 let nlpos =
6319 let pos = Bytes.index_from scratch ppos '\n' in
6320 if pos >= n then -1 else pos
6321 with Not_found -> -1
6323 if nlpos >= 0
6324 then (
6325 Buffer.add_subbytes buf scratch ppos (nlpos-ppos);
6326 let s = Buffer.contents buf in
6327 Buffer.clear buf;
6328 ract s;
6329 eat (nlpos+1);
6331 else (
6332 Buffer.add_subbytes buf scratch ppos (n-ppos);
6333 Some fd
6335 in eat 0
6338 let remoteopen path =
6339 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
6340 with exn ->
6341 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
6342 None
6345 let () =
6346 let gcconfig = ref E.s in
6347 let trimcachepath = ref E.s in
6348 let rcmdpath = ref E.s in
6349 let pageno = ref None in
6350 let rootwid = ref 0 in
6351 let openlast = ref false in
6352 let nofc = ref false in
6353 let doreap = ref false in
6354 selfexec := Sys.executable_name;
6355 Arg.parse
6356 (Arg.align
6357 [("-p", Arg.String (fun s -> state.password <- s),
6358 "<password> Set password");
6360 ("-f", Arg.String
6361 (fun s ->
6362 Config.fontpath := s;
6363 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
6365 "<path> Set path to the user interface font");
6367 ("-c", Arg.String
6368 (fun s ->
6369 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
6370 Config.confpath := s),
6371 "<path> Set path to the configuration file");
6373 ("-last", Arg.Set openlast, " Open last document");
6375 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
6376 "<page-number> Jump to page");
6378 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6379 "<path> Set path to the trim cache file");
6381 ("-dest", Arg.String (fun s -> state.nameddest <- s),
6382 "<named-destination> Set named destination");
6384 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
6385 ("-cxack", Arg.Set cxack, " Cut corners");
6387 ("-remote", Arg.String (fun s -> rcmdpath := s),
6388 "<path> Set path to the remote commands source");
6390 ("-origin", Arg.String (fun s -> state.origin <- s),
6391 "<original-path> Set original path");
6393 ("-gc", Arg.Set_string gcconfig,
6394 "<script-path> Collect garbage with the help of a script");
6396 ("-nofc", Arg.Set nofc, " Do not use fontconfig");
6398 ("-v", Arg.Unit (fun () ->
6399 Printf.printf
6400 "%s\nconfiguration path: %s\n"
6401 (version ())
6402 Config.defconfpath
6404 exit 0), " Print version and exit");
6406 ("-embed", Arg.Set_int rootwid,
6407 "<window-id> Embed into window")
6410 (fun s -> state.path <- s)
6411 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6413 if !wtmode
6414 then selfexec := !selfexec ^ " -wtmode";
6416 let histmode = emptystr state.path && not !openlast in
6418 if not (Config.load !openlast)
6419 then prerr_endline "failed to load configuration";
6420 begin match !pageno with
6421 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
6422 | None -> ()
6423 end;
6425 if not (emptystr !gcconfig)
6426 then (
6427 let c, s =
6428 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6429 | exception exn ->
6430 error "gc socketpair failed: %s" (exntos exn)
6431 | rw -> rw
6433 match addpid @@ popen !gcconfig [(c, 0); (c, 1)] with
6434 | exception exn ->
6435 error "failed to popen gc script: %s" (exntos exn);
6436 | _ ->
6437 Config.gc s s;
6438 exit 0
6441 let wsfd, winw, winh = Wsi.init (object (self)
6442 val mutable m_clicks = 0
6443 val mutable m_click_x = 0
6444 val mutable m_click_y = 0
6445 val mutable m_lastclicktime = infinity
6447 method private cleanup =
6448 state.roam <- noroam;
6449 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap
6450 method expose = G.postRedisplay"expose"
6451 method visible v =
6452 let name =
6453 match v with
6454 | Wsi.Unobscured -> "unobscured"
6455 | Wsi.PartiallyObscured -> "partiallyobscured"
6456 | Wsi.FullyObscured -> "fullyobscured"
6458 vlog "visibility change %s" name
6459 method display = display ()
6460 method map mapped = vlog "mappped %b" mapped
6461 method reshape w h =
6462 self#cleanup;
6463 reshape w h
6464 method mouse b d x y m =
6465 if d && canselect ()
6466 then (
6467 (* http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx *)
6468 m_click_x <- x;
6469 m_click_y <- y;
6470 if b = 1
6471 then (
6472 let t = now () in
6473 if abs x - m_click_x > 10
6474 || abs y - m_click_y > 10
6475 || abs_float (t -. m_lastclicktime) > 0.3
6476 then m_clicks <- 0;
6477 m_clicks <- m_clicks + 1;
6478 m_lastclicktime <- t;
6479 if m_clicks = 1
6480 then (
6481 self#cleanup;
6482 G.postRedisplay "cleanup";
6483 state.uioh <- state.uioh#button b d x y m;
6485 else state.uioh <- state.uioh#multiclick m_clicks x y m
6487 else (
6488 self#cleanup;
6489 m_clicks <- 0;
6490 m_lastclicktime <- infinity;
6491 state.uioh <- state.uioh#button b d x y m
6494 else (
6495 state.uioh <- state.uioh#button b d x y m
6497 method motion x y =
6498 state.mpos <- (x, y);
6499 state.uioh <- state.uioh#motion x y
6500 method pmotion x y =
6501 state.mpos <- (x, y);
6502 state.uioh <- state.uioh#pmotion x y
6503 method key k m =
6504 let mascm = m land (
6505 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6506 ) in
6507 let keyboard k m =
6508 let x = state.x and y = state.y in
6509 keyboard k m;
6510 if x != state.x || y != state.y then self#cleanup
6512 match state.keystate with
6513 | KSnone ->
6514 let km = k, mascm in
6515 begin
6516 match
6517 let modehash = state.uioh#modehash in
6518 try Hashtbl.find modehash km
6519 with Not_found ->
6520 try Hashtbl.find (findkeyhash conf "global") km
6521 with Not_found -> KMinsrt (k, m)
6522 with
6523 | KMinsrt (k, m) -> keyboard k m
6524 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6525 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6527 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6528 List.iter (fun (k, m) -> keyboard k m) insrt;
6529 state.keystate <- KSnone
6530 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6531 state.keystate <- KSinto (keys, insrt)
6532 | KSinto _ -> state.keystate <- KSnone
6534 method enter x y =
6535 state.mpos <- (x, y);
6536 state.uioh <- state.uioh#pmotion x y
6537 method leave = state.mpos <- (-1, -1)
6538 method winstate wsl = state.winstate <- wsl
6539 method quit = raise Quit
6540 end) !rootwid conf.cwinw conf.cwinh platform in
6542 state.wsfd <- wsfd;
6544 if not (
6545 List.exists GlMisc.check_extension
6546 [ "GL_ARB_texture_rectangle"
6547 ; "GL_EXT_texture_recangle"
6548 ; "GL_NV_texture_rectangle" ]
6550 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6552 if (
6553 let r = GlMisc.get_string `renderer in
6554 let p = "Mesa DRI Intel(" in
6555 let l = String.length p in
6556 String.length r > l && String.sub r 0 l = p
6558 then (
6559 defconf.sliceheight <- 1024;
6560 defconf.texcount <- 32;
6561 defconf.usepbo <- true;
6564 let cs, ss =
6565 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6566 | exception exn ->
6567 Printf.eprintf "socketpair failed: %s" (exntos exn);
6568 exit 1
6569 | (r, w) ->
6570 cloexec r;
6571 cloexec w;
6572 r, w
6575 setcheckers conf.checkers;
6576 redirectstderr ();
6577 if conf.redirectstderr
6578 then
6579 at_exit (fun () ->
6580 let s = Bytes.cat
6581 (Buffer.to_bytes state.errmsgs)
6582 (match state.errfd with
6583 | Some fd ->
6584 let s = Bytes.create (80*24) in
6585 let n =
6587 let r, _, _ = Unix.select [fd] [] [] 0.0 in
6588 if List.mem fd r
6589 then Unix.read fd s 0 (Bytes.length s)
6590 else 0
6591 with _ -> 0
6593 if n = 0
6594 then E.b
6595 else Bytes.sub s 0 n
6596 | None -> E.b
6599 try ignore (Unix.write state.stderr s 0 (Bytes.length s))
6600 with exn -> print_endline (exntos exn)
6604 init cs (
6605 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
6606 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6607 !Config.fontpath, !trimcachepath,
6608 GlMisc.check_extension "GL_ARB_pixel_buffer_object",
6609 not !nofc
6611 List.iter GlArray.enable [`texture_coord; `vertex];
6612 state.ss <- ss;
6613 reshape winw winh;
6614 state.uioh <- uioh;
6615 if histmode
6616 then (
6617 Wsi.settitle "llpp (history)";
6618 enterhistmode ();
6620 else (
6621 state.text <- "Opening " ^ (mbtoutf8 state.path);
6622 opendoc state.path state.password;
6624 display ();
6625 Wsi.mapwin ();
6626 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
6628 let rec reap () =
6629 match Unix.waitpid [Unix.WNOHANG] ~-1 with
6630 | (exception (Unix.Unix_error (Unix.ECHILD, _, _))) -> ()
6631 | (exception exn) -> dolog "Unix.waitpid: %s" @@ exntos exn
6632 | 0, _ -> ()
6633 | _pid, _status -> reap ()
6635 Sys.set_signal Sys.sigchld (Sys.Signal_handle (fun _ -> doreap := true));
6637 let optrfd =
6638 ref (
6639 if nonemptystr !rcmdpath
6640 then remoteopen !rcmdpath
6641 else None
6645 let rec loop deadline =
6646 if !doreap
6647 then (
6648 doreap := false;
6649 reap ()
6651 let r =
6652 match state.errfd with
6653 | None -> [state.ss; state.wsfd]
6654 | Some fd -> [state.ss; state.wsfd; fd]
6656 let r =
6657 match !optrfd with
6658 | None -> r
6659 | Some fd -> fd :: r
6661 if state.redisplay
6662 then (
6663 state.redisplay <- false;
6664 display ();
6666 let timeout =
6667 let now = now () in
6668 if deadline > now
6669 then (
6670 if deadline = infinity
6671 then ~-.1.0
6672 else max 0.0 (deadline -. now)
6674 else 0.0
6676 let r, _, _ =
6677 try Unix.select r [] [] timeout
6678 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6680 begin match r with
6681 | [] ->
6682 state.ghyll None;
6683 let newdeadline =
6684 if state.ghyll == noghyll
6685 then
6686 match state.autoscroll with
6687 | Some step when step != 0 ->
6688 let y = state.y + step in
6689 let y =
6690 if y < 0
6691 then state.maxy
6692 else if y >= state.maxy then 0 else y
6694 gotoy y;
6695 if state.mode = View
6696 then state.text <- E.s;
6697 deadline +. 0.01
6698 | _ -> infinity
6699 else deadline +. 0.01
6701 loop newdeadline
6703 | l ->
6704 let rec checkfds = function
6705 | [] -> ()
6706 | fd :: rest when fd = state.ss ->
6707 let cmd = readcmd state.ss in
6708 act cmd;
6709 checkfds rest
6711 | fd :: rest when fd = state.wsfd ->
6712 Wsi.readresp fd;
6713 checkfds rest
6715 | fd :: rest when Some fd = !optrfd ->
6716 begin match remote fd with
6717 | None -> optrfd := remoteopen !rcmdpath;
6718 | opt -> optrfd := opt
6719 end;
6720 checkfds rest
6722 | fd :: rest ->
6723 let s = Bytes.create 80 in
6724 let n = tempfailureretry (Unix.read fd s 0) 80 in
6725 if conf.redirectstderr
6726 then (
6727 Buffer.add_substring state.errmsgs (Bytes.to_string s) 0 n;
6728 state.newerrmsgs <- true;
6729 state.redisplay <- true;
6731 else (
6732 prerr_string (String.sub (Bytes.to_string s) 0 n);
6733 flush stderr;
6735 checkfds rest
6737 checkfds l;
6738 if !reeenterhist then (
6739 enterhistmode ();
6740 reeenterhist := false;
6742 let newdeadline =
6743 let deadline1 =
6744 if deadline = infinity
6745 then now () +. 0.01
6746 else deadline
6748 match state.autoscroll with
6749 | Some step when step != 0 -> deadline1
6750 | _ -> if state.ghyll == noghyll then infinity else deadline1
6752 loop newdeadline
6753 end;
6756 loop infinity;
6757 with Quit ->
6758 Config.save leavebirdseye;
6759 if hasunsavedchanges ()
6760 then save ();