Simplify
[llpp.git] / main.ml
blob3a71e0156999ed955935b3c80e4fb8af7433e916
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 project : opaque -> int -> int -> float -> float -> (float * float)
35 = "ml_project";;
36 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
37 external rectofblock : opaque -> int -> int -> float array option
38 = "ml_rectofblock";;
39 external begintiles : unit -> unit = "ml_begintiles";;
40 external endtiles : unit -> unit = "ml_endtiles";;
41 external addannot : opaque -> int -> int -> string -> unit = "ml_addannot";;
42 external modannot : opaque -> slinkindex -> string -> unit = "ml_modannot";;
43 external delannot : opaque -> slinkindex -> unit = "ml_delannot";;
44 external hasunsavedchanges : unit -> bool = "ml_hasunsavedchanges";;
45 external savedoc : string -> unit = "ml_savedoc";;
46 external getannotcontents : opaque -> slinkindex -> string
47 = "ml_getannotcontents";;
48 external drawprect : opaque -> int -> int -> float array -> unit =
49 "ml_drawprect";;
51 let selfexec = ref E.s;;
53 let drawstring size x y s =
54 Gl.enable `blend;
55 Gl.enable `texture_2d;
56 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
57 ignore (drawstr size x y s);
58 Gl.disable `blend;
59 Gl.disable `texture_2d;
62 let drawstring1 size x y s =
63 drawstr size x y s;
66 let drawstring2 size x y fmt =
67 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
70 let _debugl l =
71 dolog "l %d dim=%d {" l.pageno l.pagedimno;
72 dolog " WxH %dx%d" l.pagew l.pageh;
73 dolog " vWxH %dx%d" l.pagevw l.pagevh;
74 dolog " pagex,y %d,%d" l.pagex l.pagey;
75 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
76 dolog " column %d" l.pagecol;
77 dolog "}";
80 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
81 dolog "rect {";
82 dolog " x0,y0=(% f, % f)" x0 y0;
83 dolog " x1,y1=(% f, % f)" x1 y1;
84 dolog " x2,y2=(% f, % f)" x2 y2;
85 dolog " x3,y3=(% f, % f)" x3 y3;
86 dolog "}";
89 let isbirdseye = function
90 | Birdseye _ -> true
91 | Textentry _
92 | View
93 | LinkNav _ -> false
96 let istextentry = function
97 | Textentry _ -> true
98 | Birdseye _
99 | View
100 | LinkNav _ -> false
103 let wtmode = ref false;;
104 let cxack = ref false;;
106 let pgscale h = truncate (float h *. conf.pgscale);;
108 let hscrollh () =
109 if not state.uioh#alwaysscrolly && (conf.scrollb land scrollbhv = 0)
110 || (state.x = 0 && state.w <= state.winw - conf.scrollbw)
111 then 0
112 else conf.scrollbw
115 let vscrollw () =
116 if not state.uioh#alwaysscrolly && (conf.scrollb land scrollbvv = 0)
117 then 0
118 else conf.scrollbw
121 let vscrollhit x =
122 if conf.leftscroll
123 then x < vscrollw ()
124 else x > state.winw - vscrollw ()
127 let wadjsb () = -vscrollw ();;
128 let xadjsb () = if conf.leftscroll then vscrollw () else 0;;
130 let setfontsize n =
131 fstate.fontsize <- n;
132 fstate.wwidth <- measurestr fstate.fontsize "w";
133 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
136 let vlog fmt =
137 if conf.verbose
138 then dolog fmt
139 else Printf.kprintf ignore fmt
142 let launchpath () =
143 if emptystr conf.pathlauncher
144 then dolog "%s" state.path
145 else (
146 let command = Str.global_replace percentsre state.path conf.pathlauncher in
147 match spawn command [] with
148 | _pid -> ()
149 | (exception exn) ->
150 dolog "failed to execute `%s': %s" command @@ exntos exn
154 module G =
155 struct
156 let postRedisplay who =
157 vlog "redisplay for [%S]" who;
158 state.redisplay <- true;
160 end;;
162 let getopaque pageno =
163 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
164 with Not_found -> None
167 let pagetranslatepoint l x y =
168 let dy = y - l.pagedispy in
169 let y = dy + l.pagey in
170 let dx = x - l.pagedispx in
171 let x = dx + l.pagex in
172 (x, y);
175 let onppundermouse g x y d =
176 let rec f = function
177 | l :: rest ->
178 begin match getopaque l.pageno with
179 | Some opaque ->
180 let x0 = l.pagedispx in
181 let x1 = x0 + l.pagevw in
182 let y0 = l.pagedispy in
183 let y1 = y0 + l.pagevh in
184 if y >= y0 && y <= y1 && x >= x0 && x <= x1
185 then
186 let px, py = pagetranslatepoint l x y in
187 match g opaque l px py with
188 | Some res -> res
189 | None -> f rest
190 else f rest
191 | _ ->
192 f rest
194 | [] -> d
196 f state.layout
199 let getunder x y =
200 let g opaque l px py =
201 if state.bzoom
202 then (
203 match rectofblock opaque px py with
204 | Some [|x0;x1;y0;y1|] ->
205 let ox = xadjsb () |> float in
206 let rect = (x0+.ox, y0, x1+.ox, y0, x1+.ox, y1, x0+.ox, y1) in
207 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
208 state.rects <- [l.pageno, color, rect];
209 G.postRedisplay "getunder";
210 | _otherwise -> ()
212 let under = whatsunder opaque px py in
213 if under = Unone then None else Some under
215 onppundermouse g x y Unone
218 let unproject x y =
219 let g opaque l x y =
220 match unproject opaque x y with
221 | Some (x, y) -> Some (Some (opaque, l.pageno, x, y))
222 | None -> None
224 onppundermouse g x y None;
227 let showtext c s =
228 state.text <- Printf.sprintf "%c%s" c s;
229 G.postRedisplay "showtext";
232 let impmsg fmt =
233 Format.ksprintf (fun s -> showtext '!' s) fmt;
236 let pipesel opaque cmd =
237 if hassel opaque
238 then
239 match Unix.pipe () with
240 | (exception exn) -> dolog "pipesel cannot create pipe: %S" @@ exntos exn;
241 | (r, w) ->
242 let doclose what fd =
243 Ne.clo fd (fun msg -> dolog "%s close failed: %s" what msg)
245 let pid =
246 try spawn cmd [r, 0; w, -1]
247 with exn ->
248 dolog "cannot execute %S: %s" cmd @@ exntos exn;
251 if pid > 0
252 then (
253 copysel w opaque;
254 G.postRedisplay "pipesel";
256 else doclose "pipesel pipe/w" w;
257 doclose "pipesel pipe/r" r;
260 let paxunder x y =
261 let g opaque l px py =
262 if markunder opaque px py conf.paxmark
263 then (
264 Some (fun () ->
265 match getopaque l.pageno with
266 | None -> ()
267 | Some opaque -> pipesel opaque conf.paxcmd
270 else None
272 G.postRedisplay "paxunder";
273 if conf.paxmark = Mark_page
274 then
275 List.iter (fun l ->
276 match getopaque l.pageno with
277 | None -> ()
278 | Some opaque -> clearmark opaque) state.layout;
279 state.roam <- onppundermouse g x y (fun () -> impmsg "whoopsie daisy");
282 let selstring s =
283 match Unix.pipe () with
284 | (exception exn) -> impmsg "pipe failed: %s" @@ exntos exn
285 | (r, w) ->
286 let clo cap fd =
287 Ne.clo fd (fun msg -> impmsg "failed to close %s: %s" cap msg)
289 let pid =
290 try spawn conf.selcmd [r, 0; w, -1]
291 with exn ->
292 impmsg "failed to execute %s: %s" conf.selcmd @@ exntos exn;
295 if pid > 0
296 then (
298 let l = String.length s in
299 let bytes = Bytes.unsafe_of_string s in
300 let n = tempfailureretry (Unix.write w bytes 0) l in
301 if n != l
302 then impmsg "failed to write %d characters to sel pipe, wrote %d"
304 with exn ->
305 impmsg "failed to write to sel pipe: %s" @@ exntos exn
307 else dolog "%s" s;
308 clo "selstring pipe/r" r;
309 clo "selstring pipe/w" w;
312 let undertext ?(nopath=false) = function
313 | Unone -> "none"
314 | Ulinkuri s -> s
315 | Ulinkgoto (pageno, _) ->
316 if nopath
317 then "page " ^ string_of_int (pageno+1)
318 else Printf.sprintf "%s: page %d" state.path (pageno+1)
319 | Utext s -> "font: " ^ s
320 | Uunexpected s -> "unexpected: " ^ s
321 | Ulaunch s -> "launch: " ^ s
322 | Unamed s -> "named: " ^ s
323 | Uremote (filename, pageno) ->
324 Printf.sprintf "%s: page %d" filename (pageno+1)
325 | Uremotedest (filename, destname) ->
326 Printf.sprintf "%s: destination %S" filename destname
327 | Uannotation (opaque, slinkindex) ->
328 "annotation: " ^ getannotcontents opaque slinkindex
331 let updateunder x y =
332 match getunder x y with
333 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
334 | Ulinkuri uri ->
335 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
336 Wsi.setcursor Wsi.CURSOR_INFO
337 | Ulinkgoto (pageno, _) ->
338 if conf.underinfo
339 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
340 Wsi.setcursor Wsi.CURSOR_INFO
341 | Utext s ->
342 if conf.underinfo then showtext 'f' ("ont: " ^ s);
343 Wsi.setcursor Wsi.CURSOR_TEXT
344 | Uunexpected s ->
345 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
346 Wsi.setcursor Wsi.CURSOR_INHERIT
347 | Ulaunch s ->
348 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
349 Wsi.setcursor Wsi.CURSOR_INHERIT
350 | Unamed s ->
351 if conf.underinfo then showtext 'n' ("amed: " ^ s);
352 Wsi.setcursor Wsi.CURSOR_INHERIT
353 | Uremote (filename, pageno) ->
354 if conf.underinfo then showtext 'r'
355 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
356 Wsi.setcursor Wsi.CURSOR_INFO
357 | Uremotedest (filename, destname) ->
358 if conf.underinfo then showtext 'r'
359 (Printf.sprintf "emote destination: %s (%S)" filename destname);
360 Wsi.setcursor Wsi.CURSOR_INFO
361 | Uannotation _ ->
362 if conf.underinfo then showtext 'a' "nnotation";
363 Wsi.setcursor Wsi.CURSOR_INFO
366 let showlinktype under =
367 if conf.underinfo && under != Unone
368 then showtext ' ' @@ undertext under
371 let intentry_with_suffix text key =
372 let c =
373 if key >= 32 && key < 127
374 then Char.chr key
375 else '\000'
377 match Char.lowercase c with
378 | '0' .. '9' ->
379 let text = addchar text c in
380 TEcont text
382 | 'k' | 'm' | 'g' ->
383 let text = addchar text c in
384 TEcont text
386 | _ ->
387 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
388 TEcont text
391 let readcmd fd =
392 let s = Bytes.create 4 in
393 let n = tempfailureretry (Unix.read fd s 0) 4 in
394 if n != 4 then error "incomplete read(len) = %d" n;
395 let len = (Char.code (Bytes.get s 0) lsl 24)
396 lor (Char.code (Bytes.get s 1) lsl 16)
397 lor (Char.code (Bytes.get s 2) lsl 8)
398 lor (Char.code (Bytes.get s 3))
400 let s = Bytes.create len in
401 let n = tempfailureretry (Unix.read fd s 0) len in
402 if n != len then error "incomplete read(data) %d vs %d" n len;
403 Bytes.to_string s
406 let wcmd fmt =
407 let b = Buffer.create 16 in
408 Buffer.add_string b "llll";
409 Printf.kbprintf
410 (fun b ->
411 let s = Buffer.to_bytes b in
412 let n = Bytes.length s in
413 let len = n - 4 in
414 (* dolog "wcmd %S" (String.sub s 4 len); *)
415 Bytes.set s 0 (Char.chr ((len lsr 24) land 0xff));
416 Bytes.set s 1 (Char.chr ((len lsr 16) land 0xff));
417 Bytes.set s 2 (Char.chr ((len lsr 8) land 0xff));
418 Bytes.set s 3 (Char.chr (len land 0xff));
419 let n' = tempfailureretry (Unix.write state.ss s 0) n in
420 if n' != n then error "write failed %d vs %d" n' n;
421 ) b fmt;
424 let nogeomcmds cmds =
425 match cmds with
426 | s, [] -> emptystr s
427 | _ -> false
430 let layoutN ((columns, coverA, coverB), b) y sh =
431 let sh = sh - (hscrollh ()) in
432 let wadj = wadjsb () in
433 let rec fold accu n =
434 if n = Array.length b
435 then accu
436 else
437 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
438 if (vy - y) > sh &&
439 (n = coverA - 1
440 || n = state.pagecount - coverB
441 || (n - coverA) mod columns = columns - 1)
442 then accu
443 else
444 let accu =
445 if vy + h > y
446 then
447 let pagey = max 0 (y - vy) in
448 let pagedispy = if pagey > 0 then 0 else vy - y in
449 let pagedispx, pagex =
450 let pdx =
451 if n = coverA - 1 || n = state.pagecount - coverB
452 then state.x + (wadj + state.winw - w) / 2
453 else dx + xoff + state.x
455 if pdx < 0
456 then 0, -pdx
457 else pdx, 0
459 let pagevw =
460 let vw = wadj + state.winw - pagedispx in
461 let pw = w - pagex in
462 min vw pw
464 let pagevh = min (h - pagey) (sh - pagedispy) in
465 if pagevw > 0 && pagevh > 0
466 then
467 let e =
468 { pageno = n
469 ; pagedimno = pdimno
470 ; pagew = w
471 ; pageh = h
472 ; pagex = pagex
473 ; pagey = pagey
474 ; pagevw = pagevw
475 ; pagevh = pagevh
476 ; pagedispx = pagedispx
477 ; pagedispy = pagedispy
478 ; pagecol = 0
481 e :: accu
482 else
483 accu
484 else
485 accu
487 fold accu (n+1)
489 if Array.length b = 0
490 then []
491 else List.rev (fold [] (page_of_y y))
494 let layoutS (columns, b) y sh =
495 let sh = sh - hscrollh () in
496 let wadj = wadjsb () in
497 let rec fold accu n =
498 if n = Array.length b
499 then accu
500 else
501 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
502 if (vy - y) > sh
503 then accu
504 else
505 let accu =
506 if vy + pageh > y
507 then
508 let x = xoff + state.x in
509 let pagey = max 0 (y - vy) in
510 let pagedispy = if pagey > 0 then 0 else vy - y in
511 let pagedispx, pagex =
512 if px = 0
513 then (
514 if x < 0
515 then 0, -x
516 else x, 0
518 else (
519 let px = px - x in
520 if px < 0
521 then -px, 0
522 else 0, px
525 let pagecolw = pagew/columns in
526 let pagedispx =
527 if pagecolw < state.winw
528 then pagedispx + ((wadj + state.winw - pagecolw) / 2)
529 else pagedispx
531 let pagevw =
532 let vw = wadj + state.winw - pagedispx in
533 let pw = pagew - pagex in
534 min vw pw
536 let pagevw = min pagevw pagecolw in
537 let pagevh = min (pageh - pagey) (sh - pagedispy) in
538 if pagevw > 0 && pagevh > 0
539 then
540 let e =
541 { pageno = n/columns
542 ; pagedimno = pdimno
543 ; pagew = pagew
544 ; pageh = pageh
545 ; pagex = pagex
546 ; pagey = pagey
547 ; pagevw = pagevw
548 ; pagevh = pagevh
549 ; pagedispx = pagedispx
550 ; pagedispy = pagedispy
551 ; pagecol = n mod columns
554 e :: accu
555 else
556 accu
557 else
558 accu
560 fold accu (n+1)
562 List.rev (fold [] 0)
565 let layout y sh =
566 if nogeomcmds state.geomcmds
567 then
568 match conf.columns with
569 | Csingle b -> layoutN ((1, 0, 0), b) y sh
570 | Cmulti c -> layoutN c y sh
571 | Csplit s -> layoutS s y sh
572 else []
575 let clamp incr =
576 let y = state.y + incr in
577 let y = max 0 y in
578 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
582 let itertiles l f =
583 let tilex = l.pagex mod conf.tilew in
584 let tiley = l.pagey mod conf.tileh in
586 let col = l.pagex / conf.tilew in
587 let row = l.pagey / conf.tileh in
589 let xadj = xadjsb () in
590 let rec rowloop row y0 dispy h =
591 if h = 0
592 then ()
593 else (
594 let dh = conf.tileh - y0 in
595 let dh = min h dh in
596 let rec colloop col x0 dispx w =
597 if w = 0
598 then ()
599 else (
600 let dw = conf.tilew - x0 in
601 let dw = min w dw in
602 let dispx' = xadj + dispx in
603 f col row dispx' dispy x0 y0 dw dh;
604 colloop (col+1) 0 (dispx+dw) (w-dw)
607 colloop col tilex l.pagedispx l.pagevw;
608 rowloop (row+1) 0 (dispy+dh) (h-dh)
611 if l.pagevw > 0 && l.pagevh > 0
612 then rowloop row tiley l.pagedispy l.pagevh;
615 let gettileopaque l col row =
616 let key =
617 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
619 try Some (Hashtbl.find state.tilemap key)
620 with Not_found -> None
623 let puttileopaque l col row gen colorspace angle opaque size elapsed =
624 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
625 Hashtbl.add state.tilemap key (opaque, size, elapsed)
628 let filledrect x0 y0 x1 y1 =
629 GlArray.disable `texture_coord;
630 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
631 GlArray.vertex `two state.vraw;
632 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
633 GlArray.enable `texture_coord;
636 let linerect x0 y0 x1 y1 =
637 GlArray.disable `texture_coord;
638 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y1; x1; y0 |];
639 GlArray.vertex `two state.vraw;
640 GlArray.draw_arrays `line_loop ~first:0 ~count:4;
641 GlArray.enable `texture_coord;
644 let drawtiles l color =
645 GlDraw.color color;
646 let wadj = wadjsb () in
647 begintiles ();
648 let f col row x y tilex tiley w h =
649 match gettileopaque l col row with
650 | Some (opaque, _, t) ->
651 let params = x, y, w, h, tilex, tiley in
652 if conf.invert
653 then GlTex.env (`mode `blend);
654 drawtile params opaque;
655 if conf.invert
656 then GlTex.env (`mode `modulate);
657 if conf.debug
658 then (
659 endtiles ();
660 let s = Printf.sprintf
661 "%d[%d,%d] %f sec"
662 l.pageno col row t
664 let w = measurestr fstate.fontsize s in
665 GlDraw.color (0.0, 0.0, 0.0);
666 filledrect (float (x-2))
667 (float (y-2))
668 (float (x+2) +. w)
669 (float (y + fstate.fontsize + 2));
670 GlDraw.color (1.0, 1.0, 1.0);
671 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
672 begintiles ();
675 | None ->
676 endtiles ();
677 let w =
678 if conf.leftscroll
679 then w
680 else
681 let lw = wadj + state.winw - x in
682 min lw w
683 and h =
684 let lh = state.winh - y in
685 min lh h
687 if conf.invert
688 then GlTex.env (`mode `blend);
689 begin match state.checkerstexid with
690 | Some id ->
691 Gl.enable `texture_2d;
692 GlTex.bind_texture ~target:`texture_2d id;
693 let x0 = float x
694 and y0 = float y
695 and x1 = float (x+w)
696 and y1 = float (y+h) in
698 let tw = float w /. 16.0
699 and th = float h /. 16.0 in
700 let tx0 = float tilex /. 16.0
701 and ty0 = float tiley /. 16.0 in
702 let tx1 = tx0 +. tw
703 and ty1 = ty0 +. th in
704 Raw.sets_float state.vraw ~pos:0
705 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
706 Raw.sets_float state.traw ~pos:0
707 [| tx0; ty0; tx0; ty1; tx1; ty0; tx1; ty1 |];
708 GlArray.vertex `two state.vraw;
709 GlArray.tex_coord `two state.traw;
710 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
711 Gl.disable `texture_2d;
713 | None ->
714 GlDraw.color (1.0, 1.0, 1.0);
715 filledrect (float x) (float y) (float (x+w)) (float (y+h));
716 end;
717 if conf.invert
718 then GlTex.env (`mode `modulate);
719 if w > 128 && h > fstate.fontsize + 10
720 then (
721 let c = if conf.invert then 1.0 else 0.0 in
722 GlDraw.color (c, c, c);
723 let c, r =
724 if conf.verbose
725 then (col*conf.tilew, row*conf.tileh)
726 else col, row
728 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
730 GlDraw.color color;
731 begintiles ();
733 itertiles l f;
734 endtiles ();
737 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
739 let tilevisible1 l x y =
740 let ax0 = l.pagex
741 and ax1 = l.pagex + l.pagevw
742 and ay0 = l.pagey
743 and ay1 = l.pagey + l.pagevh in
745 let bx0 = x
746 and by0 = y in
747 let bx1 = min (bx0 + conf.tilew) l.pagew
748 and by1 = min (by0 + conf.tileh) l.pageh in
750 let rx0 = max ax0 bx0
751 and ry0 = max ay0 by0
752 and rx1 = min ax1 bx1
753 and ry1 = min ay1 by1 in
755 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
756 nonemptyintersection
759 let tilevisible layout n x y =
760 let rec findpageinlayout m = function
761 | l :: rest when l.pageno = n ->
762 tilevisible1 l x y || (
763 match conf.columns with
764 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
765 | Csplit _
766 | Csingle _
767 | Cmulti _ -> false
769 | _ :: rest -> findpageinlayout 0 rest
770 | [] -> false
772 findpageinlayout 0 layout;
775 let tileready l x y =
776 tilevisible1 l x y &&
777 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
780 let tilepage n p layout =
781 let rec loop = function
782 | l :: rest ->
783 if l.pageno = n
784 then
785 let f col row _ _ _ _ _ _ =
786 if state.currently = Idle
787 then
788 match gettileopaque l col row with
789 | Some _ -> ()
790 | None ->
791 let x = col*conf.tilew
792 and y = row*conf.tileh in
793 let w =
794 let w = l.pagew - x in
795 min w conf.tilew
797 let h =
798 let h = l.pageh - y in
799 min h conf.tileh
801 let pbo =
802 if conf.usepbo
803 then getpbo w h conf.colorspace
804 else ~< "0"
806 wcmd "tile %s %d %d %d %d %s"
807 (~> p) x y w h (~> pbo);
808 state.currently <-
809 Tiling (
810 l, p, conf.colorspace, conf.angle,
811 state.gen, col, row, conf.tilew, conf.tileh
814 itertiles l f;
815 else
816 loop rest
818 | [] -> ()
820 if nogeomcmds state.geomcmds
821 then loop layout;
824 let preloadlayout y =
825 let y = if y < state.winh then 0 else y - state.winh in
826 let h = state.winh*3 in
827 layout y h;
830 let load pages =
831 let rec loop pages =
832 if state.currently != Idle
833 then ()
834 else
835 match pages with
836 | l :: rest ->
837 begin match getopaque l.pageno with
838 | None ->
839 wcmd "page %d %d" l.pageno l.pagedimno;
840 state.currently <- Loading (l, state.gen);
841 | Some opaque ->
842 tilepage l.pageno opaque pages;
843 loop rest
844 end;
845 | _ -> ()
847 if nogeomcmds state.geomcmds
848 then loop pages
851 let preload pages =
852 load pages;
853 if conf.preload && state.currently = Idle
854 then load (preloadlayout state.y);
857 let layoutready layout =
858 let rec fold all ls =
859 all && match ls with
860 | l :: rest ->
861 let seen = ref false in
862 let allvisible = ref true in
863 let foo col row _ _ _ _ _ _ =
864 seen := true;
865 allvisible := !allvisible &&
866 begin match gettileopaque l col row with
867 | Some _ -> true
868 | None -> false
871 itertiles l foo;
872 fold (!seen && !allvisible) rest
873 | [] -> true
875 let alltilesvisible = fold true layout in
876 alltilesvisible;
879 let gotoy y =
880 let y = bound y 0 state.maxy in
881 let y, layout, proceed =
882 match conf.maxwait with
883 | Some time when state.ghyll == noghyll ->
884 begin match state.throttle with
885 | None ->
886 let layout = layout y state.winh in
887 let ready = layoutready layout in
888 if not ready
889 then (
890 load layout;
891 state.throttle <- Some (layout, y, now ());
893 else G.postRedisplay "gotoy showall (None)";
894 y, layout, ready
895 | Some (_, _, started) ->
896 let dt = now () -. started in
897 if dt > time
898 then (
899 state.throttle <- None;
900 let layout = layout y state.winh in
901 load layout;
902 G.postRedisplay "maxwait";
903 y, layout, true
905 else -1, [], false
908 | _ ->
909 let layout = layout y state.winh in
910 if not !wtmode || layoutready layout
911 then G.postRedisplay "gotoy ready";
912 y, layout, true
914 if proceed
915 then (
916 state.y <- y;
917 state.layout <- layout;
918 begin match state.mode with
919 | LinkNav ln ->
920 begin match ln with
921 | Ltexact (pageno, linkno) ->
922 let rec loop = function
923 | [] ->
924 state.mode <- LinkNav (Ltgendir 0)
925 | l :: _ when l.pageno = pageno ->
926 begin match getopaque pageno with
927 | None -> state.mode <- LinkNav (Ltnotready (pageno, 0))
928 | Some opaque ->
929 let x0, y0, x1, y1 = getlinkrect opaque linkno in
930 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
931 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
932 then state.mode <- LinkNav (Ltgendir 0)
934 | _ :: rest -> loop rest
936 loop layout
937 | Ltnotready _ | Ltgendir _ -> ()
939 | Birdseye _
940 | Textentry _
941 | View -> ()
942 end;
943 begin match state.mode with
944 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
945 if not (pagevisible layout pageno)
946 then (
947 match state.layout with
948 | [] -> ()
949 | l :: _ ->
950 state.mode <- Birdseye (
951 conf, leftx, l.pageno, hooverpageno, anchor
954 | LinkNav lt ->
955 begin match lt with
956 | Ltnotready (_, dir)
957 | Ltgendir dir ->
958 let linknav =
959 let rec loop = function
960 | [] -> lt
961 | l :: rest ->
962 match getopaque l.pageno with
963 | None -> Ltnotready (l.pageno, dir)
964 | Some opaque ->
965 let link =
966 let ld =
967 if dir = 0
968 then LDfirstvisible (l.pagex, l.pagey, dir)
969 else (
970 if dir > 0 then LDfirst else LDlast
973 findlink opaque ld
975 match link with
976 | Lnotfound -> loop rest
977 | Lfound n ->
978 showlinktype (getlink opaque n);
979 Ltexact (l.pageno, n)
981 loop state.layout
983 state.mode <- LinkNav linknav
984 | Ltexact _ -> ()
986 | Textentry _
987 | View -> ()
988 end;
989 preload layout;
991 state.ghyll <- noghyll;
992 if conf.updatecurs
993 then (
994 let mx, my = state.mpos in
995 updateunder mx my;
999 let conttiling pageno opaque =
1000 tilepage pageno opaque
1001 (if conf.preload then preloadlayout state.y else state.layout)
1004 let gotoy_and_clear_text y =
1005 if not conf.verbose then state.text <- E.s;
1006 gotoy y;
1009 let getanchory (n, top, dtop) =
1010 let y, h = getpageyh n in
1011 if conf.presentation
1012 then
1013 let ips = calcips h in
1014 y + truncate (top*.float h -. dtop*.float ips) + ips;
1015 else
1016 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1019 let gotoanchor anchor =
1020 gotoy (getanchory anchor);
1023 let addnav () =
1024 cbput state.hists.nav (getanchor ());
1027 let getnav dir =
1028 let anchor = cbgetc state.hists.nav dir in
1029 getanchory anchor;
1032 let gotoghyll1 single y =
1033 let scroll f n a b =
1034 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1035 let snake f a b =
1036 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1037 if f < a
1038 then s (float f /. float a)
1039 else (
1040 if f > b
1041 then 1.0 -. s ((float (f-b) /. float (n-b)))
1042 else 1.0
1045 snake f a b
1046 and summa n a b =
1047 let ins = float a *. 0.5
1048 and outs = float (n-b) *. 0.5 in
1049 let ones = b - a in
1050 ins +. outs +. float ones
1052 let rec set nab y sy =
1053 let (_N, _A, _B), y =
1054 if single
1055 then
1056 let scl = if y > sy then 2 else -2 in
1057 let _N, _, _ = nab in
1058 (_N,0,_N), y+conf.scrollstep*scl
1059 else nab,y in
1060 let sum = summa _N _A _B in
1061 let dy = float (y - sy) in
1062 state.ghyll <- (
1063 let rec gf n y1 o =
1064 if n >= _N
1065 then state.ghyll <- noghyll
1066 else
1067 let go n =
1068 let s = scroll n _N _A _B in
1069 let y1 = y1 +. ((s *. dy) /. sum) in
1070 gotoy_and_clear_text (truncate y1);
1071 state.ghyll <- gf (n+1) y1;
1073 match o with
1074 | None -> go n
1075 | Some y' when single -> set nab y' state.y
1076 | Some y' -> set (_N/2, 1, 1) y' state.y
1078 gf 0 (float state.y)
1081 match conf.ghyllscroll with
1082 | Some nab when not conf.presentation ->
1083 if state.ghyll == noghyll
1084 then set nab y state.y
1085 else state.ghyll (Some y)
1086 | _ ->
1087 gotoy_and_clear_text y
1090 let gotoghyll = gotoghyll1 false;;
1092 let gotopage n top =
1093 let y, h = getpageyh n in
1094 let y = y + (truncate (top *. float h)) in
1095 gotoghyll y
1098 let gotopage1 n top =
1099 let y = getpagey n in
1100 let y = y + top in
1101 gotoghyll y
1104 let invalidate s f =
1105 state.layout <- [];
1106 state.pdims <- [];
1107 state.rects <- [];
1108 state.rects1 <- [];
1109 match state.geomcmds with
1110 | ps, [] when emptystr ps ->
1111 f ();
1112 state.geomcmds <- s, [];
1114 | ps, [] ->
1115 state.geomcmds <- ps, [s, f];
1117 | ps, (s', _) :: rest when s' = s ->
1118 state.geomcmds <- ps, ((s, f) :: rest);
1120 | ps, cmds ->
1121 state.geomcmds <- ps, ((s, f) :: cmds);
1124 let flushpages () =
1125 Hashtbl.iter (fun _ opaque ->
1126 wcmd "freepage %s" (~> opaque);
1127 ) state.pagemap;
1128 Hashtbl.clear state.pagemap;
1131 let flushtiles () =
1132 if not (Queue.is_empty state.tilelru)
1133 then (
1134 Queue.iter (fun (k, p, s) ->
1135 wcmd "freetile %s" (~> p);
1136 state.memused <- state.memused - s;
1137 Hashtbl.remove state.tilemap k;
1138 ) state.tilelru;
1139 state.uioh#infochanged Memused;
1140 Queue.clear state.tilelru;
1142 load state.layout;
1145 let stateh h =
1146 let h = truncate (float h*.conf.zoom) in
1147 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
1148 h - d
1151 let opendoc path password =
1152 state.path <- path;
1153 state.password <- password;
1154 state.gen <- state.gen + 1;
1155 state.docinfo <- [];
1156 state.outlines <- [||];
1158 flushpages ();
1159 setaalevel conf.aalevel;
1160 let titlepath =
1161 if emptystr state.origin
1162 then path
1163 else state.origin
1165 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
1166 wcmd "open %d %d %s\000%s\000" (btod !wtmode) (btod !cxack) path password;
1167 invalidate "reqlayout"
1168 (fun () ->
1169 wcmd "reqlayout %d %d %d %s\000"
1170 conf.angle (FMTE.to_int conf.fitmodel)
1171 (stateh state.winh) state.nameddest
1175 let reload () =
1176 state.anchor <- getanchor ();
1177 opendoc state.path state.password;
1180 let scalecolor c =
1181 let c = c *. conf.colorscale in
1182 (c, c, c);
1185 let scalecolor2 (r, g, b) =
1186 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1189 let docolumns columns =
1190 let wadj = wadjsb () in
1191 match columns with
1192 | Csingle _ ->
1193 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1194 let wadj = wadjsb () in
1195 let rec loop pageno pdimno pdim y ph pdims =
1196 if pageno = state.pagecount
1197 then ()
1198 else
1199 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1200 match pdims with
1201 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1202 pdimno+1, pdim, rest
1203 | _ ->
1204 pdimno, pdim, pdims
1206 let x = max 0 (((wadj + state.winw - w) / 2) - xoff) in
1207 let y = y +
1208 (if conf.presentation
1209 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1210 else (if pageno = 0 then 0 else conf.interpagespace)
1213 a.(pageno) <- (pdimno, x, y, pdim);
1214 loop (pageno+1) pdimno pdim (y + h) h pdims
1216 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1217 conf.columns <- Csingle a;
1219 | Cmulti ((columns, coverA, coverB), _) ->
1220 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1221 let rec loop pageno pdimno pdim x y rowh pdims =
1222 let rec fixrow m = if m = pageno then () else
1223 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1224 if h < rowh
1225 then (
1226 let y = y + (rowh - h) / 2 in
1227 a.(m) <- (pdimno, x, y, pdim);
1229 fixrow (m+1)
1231 if pageno = state.pagecount
1232 then fixrow (((pageno - 1) / columns) * columns)
1233 else
1234 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1235 match pdims with
1236 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1237 pdimno+1, pdim, rest
1238 | _ ->
1239 pdimno, pdim, pdims
1241 let x, y, rowh' =
1242 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1243 then (
1244 let x = (wadj + state.winw - w) / 2 in
1245 let ips =
1246 if conf.presentation then calcips h else conf.interpagespace in
1247 x, y + ips + rowh, h
1249 else (
1250 if (pageno - coverA) mod columns = 0
1251 then (
1252 let x = max 0 (wadj + state.winw - state.w) / 2 in
1253 let y =
1254 if conf.presentation
1255 then
1256 let ips = calcips h in
1257 y + (if pageno = 0 then 0 else calcips rowh + ips)
1258 else
1259 y + (if pageno = 0 then 0 else conf.interpagespace)
1261 x, y + rowh, h
1263 else x, y, max rowh h
1266 let y =
1267 if pageno > 1 && (pageno - coverA) mod columns = 0
1268 then (
1269 let y =
1270 if pageno = columns && conf.presentation
1271 then (
1272 let ips = calcips rowh in
1273 for i = 0 to pred columns
1275 let (pdimno, x, y, pdim) = a.(i) in
1276 a.(i) <- (pdimno, x, y+ips, pdim)
1277 done;
1278 y+ips;
1280 else y
1282 fixrow (pageno - columns);
1285 else y
1287 a.(pageno) <- (pdimno, x, y, pdim);
1288 let x = x + w + xoff*2 + conf.interpagespace in
1289 loop (pageno+1) pdimno pdim x y rowh' pdims
1291 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1292 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1294 | Csplit (c, _) ->
1295 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1296 let rec loop pageno pdimno pdim y pdims =
1297 if pageno = state.pagecount
1298 then ()
1299 else
1300 let pdimno, ((_, w, h, _) as pdim), pdims =
1301 match pdims with
1302 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1303 pdimno+1, pdim, rest
1304 | _ ->
1305 pdimno, pdim, pdims
1307 let cw = w / c in
1308 let rec loop1 n x y =
1309 if n = c then y else (
1310 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1311 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1314 let y = loop1 0 0 y in
1315 loop (pageno+1) pdimno pdim y pdims
1317 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1318 conf.columns <- Csplit (c, a);
1321 let represent () =
1322 docolumns conf.columns;
1323 state.maxy <- calcheight ();
1324 if state.reprf == noreprf
1325 then (
1326 match state.mode with
1327 | Birdseye (_, _, pageno, _, _) ->
1328 let y, h = getpageyh pageno in
1329 let top = (state.winh - h) / 2 in
1330 gotoy (max 0 (y - top))
1331 | Textentry _
1332 | View
1333 | LinkNav _ -> gotoanchor state.anchor
1335 else (
1336 state.reprf ();
1337 state.reprf <- noreprf;
1341 let reshape ?(firsttime=false) w h =
1342 GlDraw.viewport ~x:0 ~y:0 ~w:w ~h:h;
1343 if not firsttime && nogeomcmds state.geomcmds
1344 then state.anchor <- getanchor ();
1346 state.winw <- w;
1347 let w = wadjsb () + (truncate (float w *. conf.zoom)) in
1348 let w = max w 2 in
1349 state.winh <- h;
1350 setfontsize fstate.fontsize;
1351 GlMat.mode `modelview;
1352 GlMat.load_identity ();
1354 GlMat.mode `projection;
1355 GlMat.load_identity ();
1356 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1357 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1358 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
1360 let relx =
1361 if conf.zoom <= 1.0
1362 then 0.0
1363 else float state.x /. float state.w
1365 invalidate "geometry"
1366 (fun () ->
1367 state.w <- w;
1368 if not firsttime
1369 then state.x <- truncate (relx *. float w);
1370 let w =
1371 match conf.columns with
1372 | Csingle _ -> w
1373 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1374 | Csplit (c, _) -> w * c
1376 wcmd "geometry %d %d %d"
1377 w (stateh h) (FMTE.to_int conf.fitmodel)
1381 let enttext () =
1382 let len = String.length state.text in
1383 let x0 = xadjsb () in
1384 let drawstring s =
1385 let hscrollh =
1386 match state.mode with
1387 | Textentry _ | View | LinkNav _ ->
1388 let h, _, _ = state.uioh#scrollpw in
1390 | Birdseye _ -> 0
1392 let rect x w =
1393 filledrect x (float (state.winh - (fstate.fontsize + 4) - hscrollh))
1394 (x+.w) (float (state.winh - hscrollh))
1397 let w = float (wadjsb () + state.winw - 1) in
1398 if state.progress >= 0.0 && state.progress < 1.0
1399 then (
1400 GlDraw.color (0.3, 0.3, 0.3);
1401 let w1 = w *. state.progress in
1402 rect (float x0) w1;
1403 GlDraw.color (0.0, 0.0, 0.0);
1404 rect (float x0+.w1) (float x0+.w-.w1)
1406 else (
1407 GlDraw.color (0.0, 0.0, 0.0);
1408 rect (float x0) w;
1411 GlDraw.color (1.0, 1.0, 1.0);
1412 drawstring fstate.fontsize
1413 (if conf.leftscroll then x0 + 2 else x0 + if len > 0 then 8 else 2)
1414 (state.winh - hscrollh - 5) s;
1416 let s =
1417 match state.mode with
1418 | Textentry ((prefix, text, _, _, _, _), _) ->
1419 let s =
1420 if len > 0
1421 then
1422 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1423 else
1424 Printf.sprintf "%s%s_" prefix text
1428 | Birdseye _
1429 | View
1430 | LinkNav _ -> state.text
1432 let s =
1433 if state.newerrmsgs
1434 then (
1435 if not (istextentry state.mode) && state.uioh#eformsgs
1436 then
1437 let s1 = "(press 'e' to review error messasges)" in
1438 if nonemptystr s then s ^ " " ^ s1 else s1
1439 else s
1441 else s
1443 if nonemptystr s
1444 then drawstring s
1447 let gctiles () =
1448 let len = Queue.length state.tilelru in
1449 let layout = lazy (
1450 match state.throttle with
1451 | None ->
1452 if conf.preload
1453 then preloadlayout state.y
1454 else state.layout
1455 | Some (layout, _, _) ->
1456 layout
1457 ) in
1458 let rec loop qpos =
1459 if state.memused <= conf.memlimit
1460 then ()
1461 else (
1462 if qpos < len
1463 then
1464 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1465 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1466 let (_, pw, ph, _) = getpagedim n in
1468 gen = state.gen
1469 && colorspace = conf.colorspace
1470 && angle = conf.angle
1471 && pagew = pw
1472 && pageh = ph
1473 && (
1474 let x = col*conf.tilew
1475 and y = row*conf.tileh in
1476 tilevisible (Lazy.force_val layout) n x y
1478 then Queue.push lruitem state.tilelru
1479 else (
1480 freepbo p;
1481 wcmd "freetile %s" (~> p);
1482 state.memused <- state.memused - s;
1483 state.uioh#infochanged Memused;
1484 Hashtbl.remove state.tilemap k;
1486 loop (qpos+1)
1489 loop 0
1492 let onpagerect pageno f =
1493 let b =
1494 match conf.columns with
1495 | Cmulti (_, b) -> b
1496 | Csingle b -> b
1497 | Csplit (_, b) -> b
1499 if pageno >= 0 && pageno < Array.length b
1500 then
1501 let (_, _, _, (_, w, h, _)) = b.(pageno) in
1502 f w h
1505 let gotopagexy1 pageno x y =
1506 let _,w1,h1,leftx = getpagedim pageno in
1507 let top = y /. (float h1) in
1508 let left = x /. (float w1) in
1509 let py, w, h = getpageywh pageno in
1510 let wh = state.winh - hscrollh () in
1511 let x = left *. (float w) in
1512 let x = leftx + state.x + truncate x in
1513 let wadj = wadjsb () in
1514 let sx =
1515 if x < 0 || x >= wadj + state.winw
1516 then state.x - x
1517 else state.x
1519 let pdy = truncate (top *. float h) in
1520 let y' = py + pdy in
1521 let dy = y' - state.y in
1522 let sy =
1523 if x != state.x || not (dy > 0 && dy < wh)
1524 then (
1525 if conf.presentation
1526 then
1527 if abs (py - y') > wh
1528 then y'
1529 else py
1530 else y';
1532 else state.y
1534 if state.x != sx || state.y != sy
1535 then (
1536 let x, y =
1537 if !wtmode
1538 then (
1539 let ww = wadj + state.winw in
1540 let qx = sx / ww
1541 and qy = pdy / wh in
1542 let x = qx * ww
1543 and y = py + qy * wh in
1544 let x = if -x + ww > w1 then -(w1-ww) else x
1545 and y' = if y + wh > state.maxy then state.maxy - wh else y in
1546 let y =
1547 if conf.presentation
1548 then
1549 if abs (py - y') > wh
1550 then y'
1551 else py
1552 else y';
1554 (x, y)
1556 else (sx, sy)
1558 state.x <- x;
1559 gotoy_and_clear_text y;
1561 else gotoy_and_clear_text state.y;
1564 let gotopagexy pageno x y =
1565 match state.mode with
1566 | Birdseye _ -> gotopage pageno 0.0
1567 | Textentry _
1568 | View
1569 | LinkNav _ -> gotopagexy1 pageno x y
1572 let getpassword () =
1573 let passcmd = getenvwithdef "LLPP_ASKPASS" conf.passcmd in
1574 if emptystr passcmd
1575 then E.s
1576 else getcmdoutput
1577 (fun s ->
1578 impmsg "error getting password: %s" s;
1579 dolog "%s" s) passcmd;
1582 let pgoto opaque pageno x y =
1583 let pdimno = getpdimno pageno in
1584 let x, y = project opaque pageno pdimno x y in
1585 gotopagexy pageno x y;
1588 let act cmds =
1589 (* dolog "%S" cmds; *)
1590 let cl = splitatspace cmds in
1591 let scan s fmt f =
1592 try Scanf.sscanf s fmt f
1593 with exn ->
1594 dolog "error processing '%S': %s" cmds @@ exntos exn;
1595 exit 1
1597 let addoutline outline =
1598 match state.currently with
1599 | Outlining outlines ->
1600 state.currently <- Outlining (outline :: outlines)
1601 | Idle -> state.currently <- Outlining [outline]
1602 | Loading _
1603 | Tiling _ ->
1604 dolog "invalid outlining state";
1605 logcurrently state.currently
1607 match cl with
1608 | "clear" :: [] ->
1609 state.uioh#infochanged Pdim;
1610 state.pdims <- [];
1612 | "clearrects" :: [] ->
1613 state.rects <- state.rects1;
1614 G.postRedisplay "clearrects";
1616 | "continue" :: args :: [] ->
1617 let n = scan args "%u" (fun n -> n) in
1618 state.pagecount <- n;
1619 begin match state.currently with
1620 | Outlining l ->
1621 state.currently <- Idle;
1622 state.outlines <- Array.of_list (List.rev l)
1623 | Idle
1624 | Loading _
1625 | Tiling _ -> ()
1626 end;
1628 let cur, cmds = state.geomcmds in
1629 if emptystr cur
1630 then failwith "umpossible";
1632 begin match List.rev cmds with
1633 | [] ->
1634 state.geomcmds <- E.s, [];
1635 state.throttle <- None;
1636 represent ();
1637 | (s, f) :: rest ->
1638 f ();
1639 state.geomcmds <- s, List.rev rest;
1640 end;
1641 if conf.maxwait = None && not !wtmode
1642 then G.postRedisplay "continue";
1644 | "msg" :: args :: [] ->
1645 showtext ' ' args
1647 | "vmsg" :: args :: [] ->
1648 if conf.verbose
1649 then showtext ' ' args
1651 | "emsg" :: args :: [] ->
1652 Buffer.add_string state.errmsgs args;
1653 state.newerrmsgs <- true;
1654 G.postRedisplay "error message"
1656 | "progress" :: args :: [] ->
1657 let progress, text =
1658 scan args "%f %n"
1659 (fun f pos ->
1660 f, String.sub args pos (String.length args - pos))
1662 state.text <- text;
1663 state.progress <- progress;
1664 G.postRedisplay "progress"
1666 | "firstmatch" :: args :: [] ->
1667 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1668 scan args "%u %d %f %f %f %f %f %f %f %f"
1669 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1670 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1672 let xoff = float (xadjsb ()) in
1673 let x0 = x0 +. xoff
1674 and x1 = x1 +. xoff
1675 and x2 = x2 +. xoff
1676 and x3 = x3 +. xoff in
1677 let y = (getpagey pageno) + truncate y0 in
1678 addnav ();
1679 gotoy y;
1680 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1681 state.rects1 <- [pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)]
1683 | "match" :: args :: [] ->
1684 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1685 scan args "%u %d %f %f %f %f %f %f %f %f"
1686 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1687 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1689 let xoff = float (xadjsb ()) in
1690 let x0 = x0 +. xoff
1691 and x1 = x1 +. xoff
1692 and x2 = x2 +. xoff
1693 and x3 = x3 +. xoff in
1694 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1695 state.rects1 <-
1696 (pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1698 | "page" :: args :: [] ->
1699 let pageopaques, t = scan args "%s %f" (fun p t -> p, t) in
1700 let pageopaque = ~< pageopaques in
1701 begin match state.currently with
1702 | Loading (l, gen) ->
1703 vlog "page %d took %f sec" l.pageno t;
1704 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1705 begin match state.throttle with
1706 | None ->
1707 let preloadedpages =
1708 if conf.preload
1709 then preloadlayout state.y
1710 else state.layout
1712 let evict () =
1713 let set =
1714 List.fold_left (fun s l -> IntSet.add l.pageno s)
1715 IntSet.empty preloadedpages
1717 let evictedpages =
1718 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1719 if not (IntSet.mem pageno set)
1720 then (
1721 wcmd "freepage %s" (~> opaque);
1722 key :: accu
1724 else accu
1725 ) state.pagemap []
1727 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1729 evict ();
1730 state.currently <- Idle;
1731 if gen = state.gen
1732 then (
1733 tilepage l.pageno pageopaque state.layout;
1734 load state.layout;
1735 load preloadedpages;
1736 let visible = pagevisible state.layout l.pageno in
1737 if visible
1738 then (
1739 match state.mode with
1740 | LinkNav (Ltnotready (pageno, dir)) ->
1741 if pageno = l.pageno
1742 then (
1743 let link =
1744 let ld =
1745 if dir = 0
1746 then LDfirstvisible (l.pagex, l.pagey, dir)
1747 else (
1748 if dir > 0 then LDfirst else LDlast
1751 findlink pageopaque ld
1753 match link with
1754 | Lnotfound -> ()
1755 | Lfound n ->
1756 showlinktype (getlink pageopaque n);
1757 state.mode <- LinkNav (Ltexact (l.pageno, n))
1759 | LinkNav (Ltgendir _)
1760 | LinkNav (Ltexact _)
1761 | View
1762 | Birdseye _
1763 | Textentry _ -> ()
1766 if visible && layoutready state.layout
1767 then (
1768 G.postRedisplay "page";
1772 | Some (layout, _, _) ->
1773 state.currently <- Idle;
1774 tilepage l.pageno pageopaque layout;
1775 load state.layout
1776 end;
1778 | Idle
1779 | Tiling _
1780 | Outlining _ ->
1781 dolog "Inconsistent loading state";
1782 logcurrently state.currently;
1783 exit 1
1786 | "tile" :: args :: [] ->
1787 let (x, y, opaques, size, t) =
1788 scan args "%u %u %s %u %f"
1789 (fun x y p size t -> (x, y, p, size, t))
1791 let opaque = ~< opaques in
1792 begin match state.currently with
1793 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1794 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1796 unmappbo opaque;
1797 if tilew != conf.tilew || tileh != conf.tileh
1798 then (
1799 wcmd "freetile %s" (~> opaque);
1800 state.currently <- Idle;
1801 load state.layout;
1803 else (
1804 puttileopaque l col row gen cs angle opaque size t;
1805 state.memused <- state.memused + size;
1806 state.uioh#infochanged Memused;
1807 gctiles ();
1808 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1809 opaque, size) state.tilelru;
1811 let layout =
1812 match state.throttle with
1813 | None -> state.layout
1814 | Some (layout, _, _) -> layout
1817 state.currently <- Idle;
1818 if gen = state.gen
1819 && conf.colorspace = cs
1820 && conf.angle = angle
1821 && tilevisible layout l.pageno x y
1822 then conttiling l.pageno pageopaque;
1824 begin match state.throttle with
1825 | None ->
1826 preload state.layout;
1827 if gen = state.gen
1828 && conf.colorspace = cs
1829 && conf.angle = angle
1830 && tilevisible state.layout l.pageno x y
1831 && (not !wtmode || layoutready state.layout)
1832 then G.postRedisplay "tile nothrottle";
1834 | Some (layout, y, _) ->
1835 let ready = layoutready layout in
1836 if ready
1837 then (
1838 state.y <- y;
1839 state.layout <- layout;
1840 state.throttle <- None;
1841 G.postRedisplay "throttle";
1843 else load layout;
1844 end;
1847 | Idle
1848 | Loading _
1849 | Outlining _ ->
1850 dolog "Inconsistent tiling state";
1851 logcurrently state.currently;
1852 exit 1
1855 | "pdim" :: args :: [] ->
1856 let (n, w, h, _) as pdim =
1857 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1859 let pdim =
1860 match conf.fitmodel with
1861 | FitWidth -> pdim
1862 | FitPage | FitProportional ->
1863 match conf.columns with
1864 | Csplit _ -> (n, w, h, 0)
1865 | Csingle _ | Cmulti _ -> pdim
1867 state.uioh#infochanged Pdim;
1868 state.pdims <- pdim :: state.pdims
1870 | "o" :: args :: [] ->
1871 let (l, n, t, h, pos) =
1872 scan args "%u %u %d %u %n"
1873 (fun l n t h pos -> l, n, t, h, pos)
1875 let s = String.sub args pos (String.length args - pos) in
1876 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
1878 | "ou" :: args :: [] ->
1879 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
1880 let s = String.sub args pos len in
1881 let pos2 = pos + len + 1 in
1882 let uri = String.sub args pos2 (String.length args - pos2) in
1883 addoutline (s, l, Ouri uri)
1885 | "on" :: args :: [] ->
1886 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
1887 let s = String.sub args pos (String.length args - pos) in
1888 addoutline (s, l, Onone)
1890 | "a" :: args :: [] ->
1891 let (n, l, t) =
1892 scan args "%u %d %d" (fun n l t -> n, l, t)
1894 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
1896 | "info" :: args :: [] ->
1897 let pos = nindex args '\t' in
1898 if pos >= 0 && String.sub args 0 pos = "Title"
1899 then (
1900 let s = String.sub args (pos+1) @@ String.length args - pos - 1 in
1901 conf.title <- s;
1902 Wsi.settitle s;
1904 state.docinfo <- (1, args) :: state.docinfo
1906 | "infoend" :: [] ->
1907 state.uioh#infochanged Docinfo;
1908 state.docinfo <- List.rev state.docinfo
1910 | "pass" :: l ->
1911 if l = "fail" :: []
1912 then Wsi.settitle "Wrong password";
1913 let password = getpassword () in
1914 if emptystr password
1915 then error "document is password protected"
1916 else opendoc state.path password
1917 | _ ->
1918 error "unknown cmd `%S'" cmds
1921 let onhist cb =
1922 let rc = cb.rc in
1923 let action = function
1924 | HCprev -> cbget cb ~-1
1925 | HCnext -> cbget cb 1
1926 | HCfirst -> cbget cb ~-(cb.rc)
1927 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1928 and cancel () = cb.rc <- rc
1929 in (action, cancel)
1932 let search pattern forward =
1933 match conf.columns with
1934 | Csplit _ -> impmsg "searching does not work properly in split columns mode"
1935 | Csingle _
1936 | Cmulti _ ->
1937 if nonemptystr pattern
1938 then
1939 let pn, py =
1940 match state.layout with
1941 | [] -> 0, 0
1942 | l :: _ ->
1943 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1945 wcmd "search %d %d %d %d,%s\000"
1946 (btod conf.icase) pn py (btod forward) pattern;
1949 let intentry text key =
1950 let c =
1951 if key >= 32 && key < 127
1952 then Char.chr key
1953 else '\000'
1955 match c with
1956 | '0' .. '9' ->
1957 let text = addchar text c in
1958 TEcont text
1960 | _ ->
1961 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1962 TEcont text
1965 let linknact f s =
1966 if nonemptystr s
1967 then (
1968 let n =
1969 let l = String.length s in
1970 let rec loop pos n = if pos = l then n else
1971 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
1972 loop (pos+1) (n*26 + m)
1973 in loop 0 0
1975 let rec loop n = function
1976 | [] -> ()
1977 | l :: rest ->
1978 match getopaque l.pageno with
1979 | None -> loop n rest
1980 | Some opaque ->
1981 let m = getlinkcount opaque in
1982 if n < m
1983 then (
1984 let under = getlink opaque n in
1985 f under
1987 else loop (n-m) rest
1989 loop n state.layout;
1993 let linknentry text key =
1994 let c =
1995 if key >= 32 && key < 127
1996 then Char.chr key
1997 else '\000'
1999 match c with
2000 | 'a' .. 'z' ->
2001 let text = addchar text c in
2002 linknact (fun under -> state.text <- undertext ~nopath:true under) text;
2003 TEcont text
2005 | _ ->
2006 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2007 TEcont text
2010 let textentry text key =
2011 if key land 0xff00 = 0xff00
2012 then TEcont text
2013 else TEcont (text ^ toutf8 key)
2016 let reqlayout angle fitmodel =
2017 match state.throttle with
2018 | None ->
2019 if nogeomcmds state.geomcmds
2020 then state.anchor <- getanchor ();
2021 conf.angle <- angle mod 360;
2022 if conf.angle != 0
2023 then (
2024 match state.mode with
2025 | LinkNav _ -> state.mode <- View
2026 | Birdseye _
2027 | Textentry _
2028 | View -> ()
2030 conf.fitmodel <- fitmodel;
2031 invalidate "reqlayout"
2032 (fun () ->
2033 wcmd "reqlayout %d %d %d"
2034 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
2036 | _ -> ()
2039 let settrim trimmargins trimfuzz =
2040 if nogeomcmds state.geomcmds
2041 then state.anchor <- getanchor ();
2042 conf.trimmargins <- trimmargins;
2043 conf.trimfuzz <- trimfuzz;
2044 let x0, y0, x1, y1 = trimfuzz in
2045 invalidate "settrim"
2046 (fun () ->
2047 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2048 flushpages ();
2051 let setzoom zoom =
2052 match state.throttle with
2053 | None ->
2054 let zoom = max 0.0001 zoom in
2055 if zoom <> conf.zoom
2056 then (
2057 state.prevzoom <- (conf.zoom, state.x);
2058 conf.zoom <- zoom;
2059 reshape state.winw state.winh;
2060 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2063 | Some (layout, y, started) ->
2064 let time =
2065 match conf.maxwait with
2066 | None -> 0.0
2067 | Some t -> t
2069 let dt = now () -. started in
2070 if dt > time
2071 then (
2072 state.y <- y;
2073 load layout;
2077 let setcolumns mode columns coverA coverB =
2078 state.prevcolumns <- Some (conf.columns, conf.zoom);
2079 if columns < 0
2080 then (
2081 if isbirdseye mode
2082 then impmsg "split mode doesn't work in bird's eye"
2083 else (
2084 conf.columns <- Csplit (-columns, E.a);
2085 state.x <- 0;
2086 conf.zoom <- 1.0;
2089 else (
2090 if columns < 2
2091 then (
2092 conf.columns <- Csingle E.a;
2093 state.x <- 0;
2094 setzoom 1.0;
2096 else (
2097 conf.columns <- Cmulti ((columns, coverA, coverB), E.a);
2098 conf.zoom <- 1.0;
2101 reshape state.winw state.winh;
2104 let resetmstate () =
2105 state.mstate <- Mnone;
2106 Wsi.setcursor Wsi.CURSOR_INHERIT;
2109 let enterbirdseye () =
2110 let zoom = float conf.thumbw /. float state.winw in
2111 let birdseyepageno =
2112 let cy = state.winh / 2 in
2113 let fold = function
2114 | [] -> 0
2115 | l :: rest ->
2116 let rec fold best = function
2117 | [] -> best.pageno
2118 | l :: rest ->
2119 let d = cy - (l.pagedispy + l.pagevh/2)
2120 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2121 if abs d < abs dbest
2122 then fold l rest
2123 else best.pageno
2124 in fold l rest
2126 fold state.layout
2128 state.mode <- Birdseye (
2129 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2131 resetmstate ();
2132 conf.zoom <- zoom;
2133 conf.presentation <- false;
2134 conf.interpagespace <- 10;
2135 conf.hlinks <- false;
2136 conf.fitmodel <- FitPage;
2137 state.x <- 0;
2138 conf.maxwait <- None;
2139 conf.columns <- (
2140 match conf.beyecolumns with
2141 | Some c ->
2142 conf.zoom <- 1.0;
2143 Cmulti ((c, 0, 0), E.a)
2144 | None -> Csingle E.a
2146 if conf.verbose
2147 then
2148 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2149 (100.0*.zoom)
2150 else
2151 state.text <- E.s
2153 reshape state.winw state.winh;
2156 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2157 state.mode <- View;
2158 conf.zoom <- c.zoom;
2159 conf.presentation <- c.presentation;
2160 conf.interpagespace <- c.interpagespace;
2161 conf.maxwait <- c.maxwait;
2162 conf.hlinks <- c.hlinks;
2163 conf.fitmodel <- c.fitmodel;
2164 conf.beyecolumns <- (
2165 match conf.columns with
2166 | Cmulti ((c, _, _), _) -> Some c
2167 | Csingle _ -> None
2168 | Csplit _ -> failwith "leaving bird's eye split mode"
2170 conf.columns <- (
2171 match c.columns with
2172 | Cmulti (c, _) -> Cmulti (c, E.a)
2173 | Csingle _ -> Csingle E.a
2174 | Csplit (c, _) -> Csplit (c, E.a)
2176 if conf.verbose
2177 then
2178 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2179 (100.0*.conf.zoom)
2181 reshape state.winw state.winh;
2182 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2183 state.x <- leftx;
2186 let togglebirdseye () =
2187 match state.mode with
2188 | Birdseye vals -> leavebirdseye vals true
2189 | View -> enterbirdseye ()
2190 | Textentry _
2191 | LinkNav _ -> ()
2194 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2195 let pageno = max 0 (pageno - incr) in
2196 let rec loop = function
2197 | [] -> gotopage1 pageno 0
2198 | l :: _ when l.pageno = pageno ->
2199 if l.pagedispy >= 0 && l.pagey = 0
2200 then G.postRedisplay "upbirdseye"
2201 else gotopage1 pageno 0
2202 | _ :: rest -> loop rest
2204 loop state.layout;
2205 state.text <- E.s;
2206 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2209 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2210 let pageno = min (state.pagecount - 1) (pageno + incr) in
2211 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2212 let rec loop = function
2213 | [] ->
2214 let y, h = getpageyh pageno in
2215 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2216 gotoy (clamp dy)
2217 | l :: _ when l.pageno = pageno ->
2218 if l.pagevh != l.pageh
2219 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2220 else G.postRedisplay "downbirdseye"
2221 | _ :: rest -> loop rest
2223 loop state.layout;
2224 state.text <- E.s;
2227 let optentry mode _ key =
2228 let btos b = if b then "on" else "off" in
2229 if key >= 32 && key < 127
2230 then
2231 let c = Char.chr key in
2232 match c with
2233 | 's' ->
2234 let ondone s =
2235 try conf.scrollstep <- int_of_string s with exc ->
2236 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exc
2238 TEswitch ("scroll step: ", E.s, None, intentry, ondone, true)
2240 | 'A' ->
2241 let ondone s =
2243 conf.autoscrollstep <- boundastep state.winh (int_of_string s);
2244 if state.autoscroll <> None
2245 then state.autoscroll <- Some conf.autoscrollstep
2246 with exc ->
2247 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exc
2249 TEswitch ("auto scroll step: ", E.s, None, intentry, ondone, true)
2251 | 'C' ->
2252 let ondone s =
2254 let n, a, b = multicolumns_of_string s in
2255 setcolumns mode n a b;
2256 with exc ->
2257 state.text <- Printf.sprintf "bad columns `%s': %s" s @@ exntos exc
2259 TEswitch ("columns: ", E.s, None, textentry, ondone, true)
2261 | 'Z' ->
2262 let ondone s =
2264 let zoom = float (int_of_string s) /. 100.0 in
2265 setzoom zoom
2266 with exc ->
2267 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exc
2269 TEswitch ("zoom: ", E.s, None, intentry, ondone, true)
2271 | 't' ->
2272 let ondone s =
2274 conf.thumbw <- bound (int_of_string s) 2 4096;
2275 state.text <-
2276 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2277 begin match mode with
2278 | Birdseye beye ->
2279 leavebirdseye beye false;
2280 enterbirdseye ();
2281 | Textentry _
2282 | View
2283 | LinkNav _ -> ();
2285 with exc ->
2286 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exc
2288 TEswitch ("thumbnail width: ", E.s, None, intentry, ondone, true)
2290 | 'R' ->
2291 let ondone s =
2292 match try
2293 Some (int_of_string s)
2294 with exc ->
2295 state.text <-
2296 Printf.sprintf "bad integer `%s': %s" s @@ exntos exc;
2297 None
2298 with
2299 | Some angle -> reqlayout angle conf.fitmodel
2300 | None -> ()
2302 TEswitch ("rotation: ", E.s, None, intentry, ondone, true)
2304 | 'i' ->
2305 conf.icase <- not conf.icase;
2306 TEdone ("case insensitive search " ^ (btos conf.icase))
2308 | 'p' ->
2309 conf.preload <- not conf.preload;
2310 gotoy state.y;
2311 TEdone ("preload " ^ (btos conf.preload))
2313 | 'v' ->
2314 conf.verbose <- not conf.verbose;
2315 TEdone ("verbose " ^ (btos conf.verbose))
2317 | 'd' ->
2318 conf.debug <- not conf.debug;
2319 TEdone ("debug " ^ (btos conf.debug))
2321 | 'h' ->
2322 conf.maxhfit <- not conf.maxhfit;
2323 state.maxy <- calcheight ();
2324 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2326 | 'c' ->
2327 conf.crophack <- not conf.crophack;
2328 TEdone ("crophack " ^ btos conf.crophack)
2330 | 'a' ->
2331 let s =
2332 match conf.maxwait with
2333 | None ->
2334 conf.maxwait <- Some infinity;
2335 "always wait for page to complete"
2336 | Some _ ->
2337 conf.maxwait <- None;
2338 "show placeholder if page is not ready"
2340 TEdone s
2342 | 'f' ->
2343 conf.underinfo <- not conf.underinfo;
2344 TEdone ("underinfo " ^ btos conf.underinfo)
2346 | 'P' ->
2347 conf.savebmarks <- not conf.savebmarks;
2348 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2350 | 'S' ->
2351 let ondone s =
2353 let pageno, py =
2354 match state.layout with
2355 | [] -> 0, 0
2356 | l :: _ ->
2357 l.pageno, l.pagey
2359 conf.interpagespace <- int_of_string s;
2360 docolumns conf.columns;
2361 state.maxy <- calcheight ();
2362 let y = getpagey pageno in
2363 gotoy (y + py)
2364 with exc ->
2365 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exc
2367 TEswitch ("vertical margin: ", E.s, None, intentry, ondone, true)
2369 | 'l' ->
2370 let fm =
2371 match conf.fitmodel with
2372 | FitProportional -> FitWidth
2373 | FitWidth | FitPage -> FitProportional
2375 reqlayout conf.angle fm;
2376 TEdone ("proportional display " ^ btos (fm == FitProportional))
2378 | 'T' ->
2379 settrim (not conf.trimmargins) conf.trimfuzz;
2380 TEdone ("trim margins " ^ btos conf.trimmargins)
2382 | 'I' ->
2383 conf.invert <- not conf.invert;
2384 TEdone ("invert colors " ^ btos conf.invert)
2386 | 'x' ->
2387 let ondone s =
2388 cbput state.hists.sel s;
2389 conf.selcmd <- s;
2391 TEswitch ("selection command: ", E.s, Some (onhist state.hists.sel),
2392 textentry, ondone, true)
2394 | 'M' ->
2395 if conf.pax == None
2396 then conf.pax <- Some (ref (0.0, 0, 0))
2397 else conf.pax <- None;
2398 TEdone ("PAX " ^ btos (conf.pax != None))
2400 | _ ->
2401 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2402 TEstop
2403 else
2404 TEcont state.text
2407 class type lvsource = object
2408 method getitemcount : int
2409 method getitem : int -> (string * int)
2410 method hasaction : int -> bool
2411 method exit :
2412 uioh:uioh ->
2413 cancel:bool ->
2414 active:int ->
2415 first:int ->
2416 pan:int ->
2417 uioh option
2418 method getactive : int
2419 method getfirst : int
2420 method getpan : int
2421 method getminfo : (int * int) array
2422 end;;
2424 class virtual lvsourcebase = object
2425 val mutable m_active = 0
2426 val mutable m_first = 0
2427 val mutable m_pan = 0
2428 method getactive = m_active
2429 method getfirst = m_first
2430 method getpan = m_pan
2431 method getminfo : (int * int) array = E.a
2432 end;;
2434 let textentrykeyboard
2435 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2436 state.text <- E.s;
2437 let key =
2438 if key >= 0xffb0 && key <= 0xffb9
2439 then key - 0xffb0 + 48 else key
2441 let enttext te =
2442 state.mode <- Textentry (te, onleave);
2443 enttext ();
2444 G.postRedisplay "textentrykeyboard enttext";
2446 let histaction cmd =
2447 match opthist with
2448 | None -> ()
2449 | Some (action, _) ->
2450 state.mode <- Textentry (
2451 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
2453 G.postRedisplay "textentry histaction"
2455 match key with
2456 | @backspace ->
2457 if emptystr text && cancelonempty
2458 then (
2459 onleave Cancel;
2460 G.postRedisplay "textentrykeyboard after cancel";
2462 else
2463 let s = withoutlastutf8 text in
2464 enttext (c, s, opthist, onkey, ondone, cancelonempty)
2466 | @enter | @kpenter ->
2467 ondone text;
2468 onleave Confirm;
2469 G.postRedisplay "textentrykeyboard after confirm"
2471 | @up | @kpup -> histaction HCprev
2472 | @down | @kpdown -> histaction HCnext
2473 | @home | @kphome -> histaction HCfirst
2474 | @jend | @kpend -> histaction HClast
2476 | @escape ->
2477 if emptystr text
2478 then (
2479 begin match opthist with
2480 | None -> ()
2481 | Some (_, onhistcancel) -> onhistcancel ()
2482 end;
2483 onleave Cancel;
2484 state.text <- E.s;
2485 G.postRedisplay "textentrykeyboard after cancel2"
2487 else (
2488 enttext (c, E.s, opthist, onkey, ondone, cancelonempty)
2491 | @delete | @kpdelete -> ()
2493 | _ when key != 0
2494 && key land 0xff00 != 0xff00 (* keyboard *)
2495 && key land 0xfe00 != 0xfe00 (* xkb *)
2496 && key land 0xfd00 != 0xfd00 (* 3270 *)
2498 begin match onkey text key with
2499 | TEdone text ->
2500 ondone text;
2501 onleave Confirm;
2502 G.postRedisplay "textentrykeyboard after confirm2";
2504 | TEcont text ->
2505 enttext (c, text, opthist, onkey, ondone, cancelonempty);
2507 | TEstop ->
2508 onleave Cancel;
2509 G.postRedisplay "textentrykeyboard after cancel3"
2511 | TEswitch te ->
2512 state.mode <- Textentry (te, onleave);
2513 G.postRedisplay "textentrykeyboard switch";
2514 end;
2516 | _ ->
2517 vlog "unhandled key %s" (Wsi.keyname key)
2520 let firstof first active =
2521 if first > active || abs (first - active) > fstate.maxrows - 1
2522 then max 0 (active - (fstate.maxrows/2))
2523 else first
2526 let calcfirst first active =
2527 if active > first
2528 then
2529 let rows = active - first in
2530 if rows > fstate.maxrows then active - fstate.maxrows else first
2531 else active
2534 let scrollph y maxy =
2535 let sh = float (maxy + state.winh) /. float state.winh in
2536 let sh = float state.winh /. sh in
2537 let sh = max sh (float conf.scrollh) in
2539 let percent = float y /. float maxy in
2540 let position = (float state.winh -. sh) *. percent in
2542 let position =
2543 if position +. sh > float state.winh
2544 then float state.winh -. sh
2545 else position
2547 position, sh;
2550 let coe s = (s :> uioh);;
2552 class listview ~zebra ~helpmode ~(source:lvsource) ~trusted ~modehash =
2553 object (self)
2554 val m_pan = source#getpan
2555 val m_first = source#getfirst
2556 val m_active = source#getactive
2557 val m_qsearch = E.s
2558 val m_prev_uioh = state.uioh
2560 method private elemunder y =
2561 if y < 0
2562 then None
2563 else
2564 let n = y / (fstate.fontsize+1) in
2565 if m_first + n < source#getitemcount
2566 then (
2567 if source#hasaction (m_first + n)
2568 then Some (m_first + n)
2569 else None
2571 else None
2573 method display =
2574 Gl.enable `blend;
2575 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
2576 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2577 filledrect 0. 0. (float state.winw) (float state.winh);
2578 GlDraw.color (1., 1., 1.);
2579 Gl.enable `texture_2d;
2580 let fs = fstate.fontsize in
2581 let nfs = fs + 1 in
2582 let hw = (wadjsb () + xadjsb () + state.winw)/3 in
2583 let ww = fstate.wwidth in
2584 let tabw = 17.0*.ww in
2585 let itemcount = source#getitemcount in
2586 let minfo = source#getminfo in
2587 let x0, x1 =
2588 if conf.leftscroll
2589 then float (xadjsb ()), float (state.winw - 1)
2590 else 0.0, float (state.winw - conf.scrollbw - 1)
2592 let xadj = xadjsb () in
2593 let rec loop row =
2594 if (row - m_first) > fstate.maxrows
2595 then ()
2596 else (
2597 if row >= 0 && row < itemcount
2598 then (
2599 let (s, level) = source#getitem row in
2600 let y = (row - m_first) * nfs in
2601 let x =
2602 (if conf.leftscroll then float xadj else 5.0)
2603 +. (float (level + m_pan)) *. ww in
2604 if helpmode
2605 then GlDraw.color
2606 (let c = if row land 1 = 0 then 1.0 else 0.92 in (c,c,c));
2608 if row = m_active
2609 then (
2610 Gl.disable `texture_2d;
2611 let alpha = if source#hasaction row then 0.9 else 0.3 in
2612 GlDraw.color (1., 1., 1.) ~alpha;
2613 linerect (x0 +. 1.) (float (y + 1)) (x1) (float (y + fs + 3));
2614 Gl.enable `texture_2d;
2616 let c =
2617 if zebra && row land 1 = 1
2618 then 0.8
2619 else 1.0
2621 GlDraw.color (c,c,c);
2622 let drawtabularstring s =
2623 let drawstr x s =
2624 let x' = truncate (x0 +. x) in
2625 let pos = nindex s '\000' in
2626 if pos = -1
2627 then drawstring1 fs x' (y+nfs) s
2628 else
2629 let s1 = String.sub s 0 pos
2630 and s2 = String.sub s (pos+1) (String.length s - pos - 1) in
2631 let rec e s =
2632 if emptystr s
2633 then s
2634 else
2635 let s' = withoutlastutf8 s in
2636 let s = s' ^ "@Uellipsis" in
2637 let w = measurestr fs s in
2638 if float x' +. w +. ww < float (hw + x')
2639 then s
2640 else e s'
2642 let s1 =
2643 if float x' +. ww +. measurestr fs s1 > float (hw + x')
2644 then e s1
2645 else s1
2647 ignore (drawstring1 fs x' (y+nfs) s1);
2648 drawstring1 fs (hw + x') (y+nfs) s2
2650 if trusted
2651 then
2652 let x = if helpmode && row > 0 then x +. ww else x in
2653 let tabpos = nindex s '\t' in
2654 if tabpos > 0
2655 then
2656 let len = String.length s - tabpos - 1 in
2657 let s1 = String.sub s 0 tabpos
2658 and s2 = String.sub s (tabpos + 1) len in
2659 let nx = drawstr x s1 in
2660 let sw = nx -. x in
2661 let x = x +. (max tabw sw) in
2662 drawstr x s2
2663 else
2664 let len = String.length s - 2 in
2665 if len > 0 && s.[0] = '\xc2' && s.[1] = '\xb7'
2666 then
2667 let s = String.sub s 2 len in
2668 let x = if not helpmode then x +. ww else x in
2669 GlDraw.color (1.2, 1.2, 1.2);
2670 let vinc = drawstring1 (fs+fs/4)
2671 (truncate (x -. ww)) (y+nfs) s in
2672 GlDraw.color (1., 1., 1.);
2673 vinc +. (float fs *. 0.8)
2674 else
2675 drawstr x s
2676 else
2677 drawstr x s
2679 ignore (drawtabularstring s);
2680 loop (row+1)
2684 loop m_first;
2685 GlDraw.color (1.0, 1.0, 1.0) ~alpha:0.5;
2686 let xadj = float (xadjsb () + 5) in
2687 let rec loop row =
2688 if (row - m_first) > fstate.maxrows
2689 then ()
2690 else (
2691 if row >= 0 && row < itemcount
2692 then (
2693 let (s, level) = source#getitem row in
2694 let pos0 = nindex s '\000' in
2695 let y = (row - m_first) * nfs in
2696 let x = float (level + m_pan) *. ww in
2697 let (first, last) = minfo.(row) in
2698 let prefix =
2699 if pos0 > 0 && first > pos0
2700 then String.sub s (pos0+1) (first-pos0-1)
2701 else String.sub s 0 first
2703 let suffix = String.sub s first (last - first) in
2704 let w1 = measurestr fstate.fontsize prefix in
2705 let w2 = measurestr fstate.fontsize suffix in
2706 let x = x +. if conf.leftscroll then xadj else 5.0 in
2707 let x = if pos0 > 0 && first > pos0 then x +. float hw else x in
2708 let x0 = x +. w1
2709 and y0 = float (y+2) in
2710 let x1 = x0 +. w2
2711 and y1 = float (y+fs+3) in
2712 filledrect x0 y0 x1 y1;
2713 loop (row+1)
2717 Gl.disable `texture_2d;
2718 if Array.length minfo > 0 then loop m_first;
2719 Gl.disable `blend;
2721 method updownlevel incr =
2722 let len = source#getitemcount in
2723 let curlevel =
2724 if m_active >= 0 && m_active < len
2725 then snd (source#getitem m_active)
2726 else -1
2728 let rec flow i =
2729 if i = len then i-1 else if i = -1 then 0 else
2730 let _, l = source#getitem i in
2731 if l != curlevel then i else flow (i+incr)
2733 let active = flow m_active in
2734 let first = calcfirst m_first active in
2735 G.postRedisplay "outline updownlevel";
2736 {< m_active = active; m_first = first >}
2738 method private key1 key mask =
2739 let set1 active first qsearch =
2740 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2742 let search active pattern incr =
2743 let active = if active = -1 then m_first else active in
2744 let dosearch re =
2745 let rec loop n =
2746 if n >= 0 && n < source#getitemcount
2747 then (
2748 let s, _ = source#getitem n in
2749 match Str.search_forward re s 0 with
2750 | (exception Not_found) -> loop (n + incr)
2751 | _ -> Some n
2753 else None
2755 loop active
2757 Str.regexp_case_fold pattern |> dosearch
2759 let itemcount = source#getitemcount in
2760 let find start incr =
2761 let rec find i =
2762 if i = -1 || i = itemcount
2763 then -1
2764 else (
2765 if source#hasaction i
2766 then i
2767 else find (i + incr)
2770 find start
2772 let set active first =
2773 let first = bound first 0 (itemcount - fstate.maxrows) in
2774 state.text <- E.s;
2775 coe {< m_active = active; m_first = first; m_qsearch = E.s >}
2777 let navigate incr =
2778 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2779 let active, first =
2780 let incr1 = if incr > 0 then 1 else -1 in
2781 if isvisible m_first m_active
2782 then
2783 let next =
2784 let next = m_active + incr in
2785 let next =
2786 if next < 0 || next >= itemcount
2787 then -1
2788 else find next incr1
2790 if abs (m_active - next) > fstate.maxrows
2791 then -1
2792 else next
2794 if next = -1
2795 then
2796 let first = m_first + incr in
2797 let first = bound first 0 (itemcount - fstate.maxrows) in
2798 let next =
2799 let next = m_active + incr in
2800 let next = bound next 0 (itemcount - 1) in
2801 find next ~-incr1
2803 let active =
2804 if next = -1
2805 then m_active
2806 else (
2807 if isvisible first next
2808 then next
2809 else m_active
2812 active, first
2813 else
2814 let first = min next m_first in
2815 let first =
2816 if abs (next - first) > fstate.maxrows
2817 then first + incr
2818 else first
2820 next, first
2821 else
2822 let first = m_first + incr in
2823 let first = bound first 0 (itemcount - 1) in
2824 let active =
2825 let next = m_active + incr in
2826 let next = bound next 0 (itemcount - 1) in
2827 let next = find next incr1 in
2828 let active =
2829 if next = -1 || abs (m_active - first) > fstate.maxrows
2830 then (
2831 let active = if m_active = -1 then next else m_active in
2832 active
2834 else next
2836 if isvisible first active
2837 then active
2838 else -1
2840 active, first
2842 G.postRedisplay "listview navigate";
2843 set active first;
2845 match key with
2846 | (@r|@s) when Wsi.withctrl mask ->
2847 let incr = if key = @r then -1 else 1 in
2848 let active, first =
2849 match search (m_active + incr) m_qsearch incr with
2850 | None ->
2851 state.text <- m_qsearch ^ " [not found]";
2852 m_active, m_first
2853 | Some active ->
2854 state.text <- m_qsearch;
2855 active, firstof m_first active
2857 G.postRedisplay "listview ctrl-r/s";
2858 set1 active first m_qsearch;
2860 | @insert when Wsi.withctrl mask ->
2861 if m_active >= 0 && m_active < source#getitemcount
2862 then (
2863 let s, _ = source#getitem m_active in
2864 selstring s;
2866 coe self
2868 | @backspace ->
2869 if emptystr m_qsearch
2870 then coe self
2871 else (
2872 let qsearch = withoutlastutf8 m_qsearch in
2873 if emptystr qsearch
2874 then (
2875 state.text <- E.s;
2876 G.postRedisplay "listview empty qsearch";
2877 set1 m_active m_first E.s;
2879 else
2880 let active, first =
2881 match search m_active qsearch ~-1 with
2882 | None ->
2883 state.text <- qsearch ^ " [not found]";
2884 m_active, m_first
2885 | Some active ->
2886 state.text <- qsearch;
2887 active, firstof m_first active
2889 G.postRedisplay "listview backspace qsearch";
2890 set1 active first qsearch
2893 | key when (key != 0 && key land 0xff00 != 0xff00) ->
2894 let pattern = m_qsearch ^ toutf8 key in
2895 let active, first =
2896 match search m_active pattern 1 with
2897 | None ->
2898 state.text <- pattern ^ " [not found]";
2899 m_active, m_first
2900 | Some active ->
2901 state.text <- pattern;
2902 active, firstof m_first active
2904 G.postRedisplay "listview qsearch add";
2905 set1 active first pattern;
2907 | @escape ->
2908 state.text <- E.s;
2909 if emptystr m_qsearch
2910 then (
2911 G.postRedisplay "list view escape";
2912 let mx, my = state.mpos in
2913 updateunder mx my;
2914 begin
2915 match
2916 source#exit ~uioh:(coe self)
2917 ~cancel:true ~active:m_active ~first:m_first ~pan:m_pan
2918 with
2919 | None -> m_prev_uioh
2920 | Some uioh -> uioh
2923 else (
2924 G.postRedisplay "list view kill qsearch";
2925 coe {< m_qsearch = E.s >}
2928 | @enter | @kpenter ->
2929 state.text <- E.s;
2930 let self = {< m_qsearch = E.s >} in
2931 let opt =
2932 G.postRedisplay "listview enter";
2933 if m_active >= 0 && m_active < source#getitemcount
2934 then (
2935 source#exit ~uioh:(coe self) ~cancel:false
2936 ~active:m_active ~first:m_first ~pan:m_pan;
2938 else (
2939 source#exit ~uioh:(coe self) ~cancel:true
2940 ~active:m_active ~first:m_first ~pan:m_pan;
2943 begin match opt with
2944 | None -> m_prev_uioh
2945 | Some uioh -> uioh
2948 | @delete | @kpdelete ->
2949 coe self
2951 | @up | @kpup -> navigate ~-1
2952 | @down | @kpdown -> navigate 1
2953 | @prior | @kpprior -> navigate ~-(fstate.maxrows)
2954 | @next | @kpnext -> navigate fstate.maxrows
2956 | @right | @kpright ->
2957 state.text <- E.s;
2958 G.postRedisplay "listview right";
2959 coe {< m_pan = m_pan - 1 >}
2961 | @left | @kpleft ->
2962 state.text <- E.s;
2963 G.postRedisplay "listview left";
2964 coe {< m_pan = m_pan + 1 >}
2966 | @home | @kphome ->
2967 let active = find 0 1 in
2968 G.postRedisplay "listview home";
2969 set active 0;
2971 | @jend | @kpend ->
2972 let first = max 0 (itemcount - fstate.maxrows) in
2973 let active = find (itemcount - 1) ~-1 in
2974 G.postRedisplay "listview end";
2975 set active first;
2977 | key when (key = 0 || key land 0xff00 = 0xff00) ->
2978 coe self
2980 | _ ->
2981 dolog "listview unknown key %#x" key; coe self
2983 method key key mask =
2984 match state.mode with
2985 | Textentry te -> textentrykeyboard key mask te; coe self
2986 | Birdseye _
2987 | View
2988 | LinkNav _ -> self#key1 key mask
2990 method button button down x y _ =
2991 let opt =
2992 match button with
2993 | 1 when vscrollhit x ->
2994 G.postRedisplay "listview scroll";
2995 if down
2996 then
2997 let _, position, sh = self#scrollph in
2998 if y > truncate position && y < truncate (position +. sh)
2999 then (
3000 state.mstate <- Mscrolly;
3001 Some (coe self)
3003 else
3004 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3005 let first = truncate (s *. float source#getitemcount) in
3006 let first = min source#getitemcount first in
3007 Some (coe {< m_first = first; m_active = first >})
3008 else (
3009 state.mstate <- Mnone;
3010 Some (coe self);
3012 | 1 when down ->
3013 begin match self#elemunder y with
3014 | Some n ->
3015 G.postRedisplay "listview click";
3016 source#exit ~uioh:(coe {< m_active = n >})
3017 ~cancel:false ~active:n ~first:m_first ~pan:m_pan
3018 | _ ->
3019 Some (coe self)
3021 | n when (n == 4 || n == 5) && not down ->
3022 let len = source#getitemcount in
3023 let first =
3024 if n = 5 && m_first + fstate.maxrows >= len
3025 then
3026 m_first
3027 else
3028 let first = m_first + (if n == 4 then -1 else 1) in
3029 bound first 0 (len - 1)
3031 G.postRedisplay "listview wheel";
3032 Some (coe {< m_first = first >})
3033 | n when (n = 6 || n = 7) && not down ->
3034 let inc = if n = 7 then -1 else 1 in
3035 G.postRedisplay "listview hwheel";
3036 Some (coe {< m_pan = m_pan + inc >})
3037 | _ ->
3038 Some (coe self)
3040 match opt with
3041 | None -> m_prev_uioh
3042 | Some uioh -> uioh
3044 method multiclick _ x y = self#button 1 true x y
3046 method motion _ y =
3047 match state.mstate with
3048 | Mscrolly ->
3049 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3050 let first = truncate (s *. float source#getitemcount) in
3051 let first = min source#getitemcount first in
3052 G.postRedisplay "listview motion";
3053 coe {< m_first = first; m_active = first >}
3054 | Msel _
3055 | Mpan _
3056 | Mscrollx
3057 | Mzoom _
3058 | Mzoomrect _
3059 | Mnone -> coe self
3061 method pmotion x y =
3062 if x < state.winw - conf.scrollbw
3063 then
3064 let n =
3065 match self#elemunder y with
3066 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3067 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3069 let o =
3070 if n != m_active
3071 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3072 else self
3074 coe o
3075 else (
3076 Wsi.setcursor Wsi.CURSOR_INHERIT;
3077 coe self
3080 method infochanged _ = ()
3082 method scrollpw = (0, 0.0, 0.0)
3083 method scrollph =
3084 let nfs = fstate.fontsize + 1 in
3085 let y = m_first * nfs in
3086 let itemcount = source#getitemcount in
3087 let maxi = max 0 (itemcount - fstate.maxrows) in
3088 let maxy = maxi * nfs in
3089 let p, h = scrollph y maxy in
3090 conf.scrollbw, p, h
3092 method modehash = modehash
3093 method eformsgs = false
3094 method alwaysscrolly = true
3095 end;;
3097 class outlinelistview ~zebra ~source =
3098 let settext autonarrow s =
3099 if autonarrow
3100 then
3101 let ss = source#statestr in
3102 state.text <-
3103 if emptystr ss
3104 then "[" ^ s ^ "]"
3105 else "{" ^ ss ^ "} [" ^ s ^ "]"
3106 else state.text <- s
3108 object (self)
3109 inherit listview
3110 ~zebra
3111 ~helpmode:false
3112 ~source:(source :> lvsource)
3113 ~trusted:false
3114 ~modehash:(findkeyhash conf "outline")
3115 as super
3117 val m_autonarrow = false
3119 method! key key mask =
3120 let maxrows =
3121 if emptystr state.text
3122 then fstate.maxrows
3123 else fstate.maxrows - 2
3125 let calcfirst first active =
3126 if active > first
3127 then
3128 let rows = active - first in
3129 if rows > maxrows then active - maxrows else first
3130 else active
3132 let navigate incr =
3133 let active = m_active + incr in
3134 let active = bound active 0 (source#getitemcount - 1) in
3135 let first = calcfirst m_first active in
3136 G.postRedisplay "outline navigate";
3137 coe {< m_active = active; m_first = first >}
3139 let navscroll first =
3140 let active =
3141 let dist = m_active - first in
3142 if dist < 0
3143 then first
3144 else (
3145 if dist < maxrows
3146 then m_active
3147 else first + maxrows
3150 G.postRedisplay "outline navscroll";
3151 coe {< m_first = first; m_active = active >}
3153 let ctrl = Wsi.withctrl mask in
3154 match key with
3155 | @a when ctrl ->
3156 let text =
3157 if m_autonarrow
3158 then (source#denarrow; E.s)
3159 else (
3160 let pattern = source#renarrow in
3161 if nonemptystr m_qsearch
3162 then (source#narrow m_qsearch; m_qsearch)
3163 else pattern
3166 settext (not m_autonarrow) text;
3167 G.postRedisplay "toggle auto narrowing";
3168 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
3170 | @slash when emptystr m_qsearch && not m_autonarrow ->
3171 settext true E.s;
3172 G.postRedisplay "toggle auto narrowing";
3173 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
3175 | @n when ctrl ->
3176 source#narrow m_qsearch;
3177 if not m_autonarrow
3178 then source#add_narrow_pattern m_qsearch;
3179 G.postRedisplay "outline ctrl-n";
3180 coe {< m_first = 0; m_active = 0 >}
3182 | @S when ctrl ->
3183 let active = source#calcactive (getanchor ()) in
3184 let first = firstof m_first active in
3185 G.postRedisplay "outline ctrl-s";
3186 coe {< m_first = first; m_active = active >}
3188 | @u when ctrl ->
3189 G.postRedisplay "outline ctrl-u";
3190 if m_autonarrow && nonemptystr m_qsearch
3191 then (
3192 ignore (source#renarrow);
3193 settext m_autonarrow E.s;
3194 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3196 else (
3197 source#del_narrow_pattern;
3198 let pattern = source#renarrow in
3199 let text =
3200 if emptystr pattern then E.s else "Narrowed to " ^ pattern
3202 settext m_autonarrow text;
3203 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3206 | @l when ctrl ->
3207 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3208 G.postRedisplay "outline ctrl-l";
3209 coe {< m_first = first >}
3211 | @tab when m_autonarrow ->
3212 if nonemptystr m_qsearch
3213 then (
3214 G.postRedisplay "outline list view tab";
3215 source#add_narrow_pattern m_qsearch;
3216 settext true E.s;
3217 coe {< m_qsearch = E.s >}
3219 else coe self
3221 | @escape when m_autonarrow ->
3222 if nonemptystr m_qsearch
3223 then source#add_narrow_pattern m_qsearch;
3224 super#key key mask
3226 | @enter | @kpenter when m_autonarrow ->
3227 if nonemptystr m_qsearch
3228 then source#add_narrow_pattern m_qsearch;
3229 super#key key mask
3231 | key when m_autonarrow && (key != 0 && key land 0xff00 != 0xff00) ->
3232 let pattern = m_qsearch ^ toutf8 key in
3233 G.postRedisplay "outlinelistview autonarrow add";
3234 source#narrow pattern;
3235 settext true pattern;
3236 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3238 | key when m_autonarrow && key = @backspace ->
3239 if emptystr m_qsearch
3240 then coe self
3241 else
3242 let pattern = withoutlastutf8 m_qsearch in
3243 G.postRedisplay "outlinelistview autonarrow backspace";
3244 ignore (source#renarrow);
3245 source#narrow pattern;
3246 settext true pattern;
3247 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3249 | @up | @kpup when ctrl ->
3250 navscroll (max 0 (m_first - 1))
3252 | @down | @kpdown when ctrl ->
3253 navscroll (min (source#getitemcount - 1) (m_first + 1))
3255 | @up | @kpup -> navigate ~-1
3256 | @down | @kpdown -> navigate 1
3257 | @prior | @kpprior -> navigate ~-(fstate.maxrows)
3258 | @next | @kpnext -> navigate fstate.maxrows
3260 | @right | @kpright ->
3261 let o =
3262 if ctrl
3263 then (
3264 G.postRedisplay "outline ctrl right";
3265 {< m_pan = m_pan + 1 >}
3267 else self#updownlevel 1
3269 coe o
3271 | @left | @kpleft ->
3272 let o =
3273 if ctrl
3274 then (
3275 G.postRedisplay "outline ctrl left";
3276 {< m_pan = m_pan - 1 >}
3278 else self#updownlevel ~-1
3280 coe o
3282 | @home | @kphome ->
3283 G.postRedisplay "outline home";
3284 coe {< m_first = 0; m_active = 0 >}
3286 | @jend | @kpend ->
3287 let active = source#getitemcount - 1 in
3288 let first = max 0 (active - fstate.maxrows) in
3289 G.postRedisplay "outline end";
3290 coe {< m_active = active; m_first = first >}
3292 | _ -> super#key key mask
3293 end;;
3295 let genhistoutlines () =
3296 Config.gethist ()
3297 |> List.sort (fun (_, c1, _, _, _, _) (_, c2, _, _, _, _) ->
3298 compare c2.lastvisit c1.lastvisit)
3299 |> List.map
3300 (fun ((path, c, _, _, _, origin) as hist) ->
3301 let path = if nonemptystr origin then origin else path in
3302 let base = mbtoutf8 @@ Filename.basename path in
3303 (base ^ "\000" ^ c.title, 1, Ohistory hist)
3305 |> Array.of_list
3308 let gotohist (path, c, bookmarks, x, anchor, origin) =
3309 Config.save leavebirdseye;
3310 state.anchor <- anchor;
3311 state.bookmarks <- bookmarks;
3312 state.origin <- origin;
3313 state.x <- x;
3314 setconf conf c;
3315 let x0, y0, x1, y1 = conf.trimfuzz in
3316 wcmd "trimset %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1;
3317 reshape ~firsttime:true state.winw state.winh;
3318 opendoc path origin;
3319 setzoom c.zoom;
3322 let makecheckers () =
3323 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3324 following to say:
3325 converted by Issac Trotts. July 25, 2002 *)
3326 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3327 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3328 let id = GlTex.gen_texture () in
3329 GlTex.bind_texture ~target:`texture_2d id;
3330 GlPix.store (`unpack_alignment 1);
3331 GlTex.image2d image;
3332 List.iter (GlTex.parameter ~target:`texture_2d)
3333 [ `mag_filter `nearest; `min_filter `nearest ];
3337 let setcheckers enabled =
3338 match state.checkerstexid with
3339 | None ->
3340 if enabled then state.checkerstexid <- Some (makecheckers ())
3342 | Some checkerstexid ->
3343 if not enabled
3344 then (
3345 GlTex.delete_texture checkerstexid;
3346 state.checkerstexid <- None;
3350 let describe_location () =
3351 let fn = page_of_y state.y in
3352 let ln = page_of_y (state.y + state.winh - hscrollh () - 1) in
3353 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
3354 let percent =
3355 if maxy <= 0
3356 then 100.
3357 else (100. *. (float state.y /. float maxy))
3359 if fn = ln
3360 then
3361 Printf.sprintf "page %d of %d [%.2f%%]"
3362 (fn+1) state.pagecount percent
3363 else
3364 Printf.sprintf
3365 "pages %d-%d of %d [%.2f%%]"
3366 (fn+1) (ln+1) state.pagecount percent
3369 let setpresentationmode v =
3370 let n = page_of_y state.y in
3371 state.anchor <- (n, 0.0, 1.0);
3372 conf.presentation <- v;
3373 if conf.fitmodel = FitPage
3374 then reqlayout conf.angle conf.fitmodel;
3375 represent ();
3378 let enterinfomode =
3379 let btos b = if b then "@Uradical" else E.s in
3380 let showextended = ref false in
3381 let leave mode _ = state.mode <- mode in
3382 let src =
3383 (object
3384 val mutable m_l = []
3385 val mutable m_a = E.a
3386 val mutable m_prev_uioh = nouioh
3387 val mutable m_prev_mode = View
3389 inherit lvsourcebase
3391 method reset prev_mode prev_uioh =
3392 m_a <- Array.of_list (List.rev m_l);
3393 m_l <- [];
3394 m_prev_mode <- prev_mode;
3395 m_prev_uioh <- prev_uioh;
3397 method int name get set =
3398 m_l <-
3399 (name, `int get, 1, Action (
3400 fun u ->
3401 let ondone s =
3402 try set (int_of_string s)
3403 with exn ->
3404 state.text <- Printf.sprintf "bad integer `%s': %s"
3405 s @@ exntos exn
3407 state.text <- E.s;
3408 let te = name ^ ": ", E.s, None, intentry, ondone, true in
3409 state.mode <- Textentry (te, leave m_prev_mode);
3411 )) :: m_l
3413 method int_with_suffix name get set =
3414 m_l <-
3415 (name, `intws get, 1, Action (
3416 fun u ->
3417 let ondone s =
3418 try set (int_of_string_with_suffix s)
3419 with exn ->
3420 state.text <- Printf.sprintf "bad integer `%s': %s"
3421 s @@ exntos exn
3423 state.text <- E.s;
3424 let te =
3425 name ^ ": ", E.s, None, intentry_with_suffix, ondone, true
3427 state.mode <- Textentry (te, leave m_prev_mode);
3429 )) :: m_l
3431 method bool ?(offset=1) ?(btos=btos) name get set =
3432 m_l <-
3433 (name, `bool (btos, get), offset, Action (
3434 fun u ->
3435 let v = get () in
3436 set (not v);
3438 )) :: m_l
3440 method color name get set =
3441 m_l <-
3442 (name, `color get, 1, Action (
3443 fun u ->
3444 let invalid = (nan, nan, nan) in
3445 let ondone s =
3446 let c =
3447 try color_of_string s
3448 with exn ->
3449 state.text <- Printf.sprintf "bad color `%s': %s"
3450 s @@ exntos exn;
3451 invalid
3453 if c <> invalid
3454 then set c;
3456 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3457 state.text <- color_to_string (get ());
3458 state.mode <- Textentry (te, leave m_prev_mode);
3460 )) :: m_l
3462 method string name get set =
3463 m_l <-
3464 (name, `string get, 1, Action (
3465 fun u ->
3466 let ondone s = set s in
3467 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3468 state.mode <- Textentry (te, leave m_prev_mode);
3470 )) :: m_l
3472 method colorspace name get set =
3473 m_l <-
3474 (name, `string get, 1, Action (
3475 fun _ ->
3476 let source =
3477 (object
3478 inherit lvsourcebase
3480 initializer
3481 m_active <- CSTE.to_int conf.colorspace;
3482 m_first <- 0;
3484 method getitemcount =
3485 Array.length CSTE.names
3486 method getitem n =
3487 (CSTE.names.(n), 0)
3488 method exit ~uioh ~cancel ~active ~first ~pan =
3489 ignore (uioh, first, pan);
3490 if not cancel then set active;
3491 None
3492 method hasaction _ = true
3493 end)
3495 state.text <- E.s;
3496 let modehash = findkeyhash conf "info" in
3497 coe (new listview ~zebra:false ~helpmode:false
3498 ~source ~trusted:true ~modehash)
3499 )) :: m_l
3501 method paxmark name get set =
3502 m_l <-
3503 (name, `string get, 1, Action (
3504 fun _ ->
3505 let source =
3506 (object
3507 inherit lvsourcebase
3509 initializer
3510 m_active <- MTE.to_int conf.paxmark;
3511 m_first <- 0;
3513 method getitemcount = Array.length MTE.names
3514 method getitem n = (MTE.names.(n), 0)
3515 method exit ~uioh ~cancel ~active ~first ~pan =
3516 ignore (uioh, first, pan);
3517 if not cancel then set active;
3518 None
3519 method hasaction _ = true
3520 end)
3522 state.text <- E.s;
3523 let modehash = findkeyhash conf "info" in
3524 coe (new listview ~zebra:false ~helpmode:false
3525 ~source ~trusted:true ~modehash)
3526 )) :: m_l
3528 method fitmodel name get set =
3529 m_l <-
3530 (name, `string get, 1, Action (
3531 fun _ ->
3532 let source =
3533 (object
3534 inherit lvsourcebase
3536 initializer
3537 m_active <- FMTE.to_int conf.fitmodel;
3538 m_first <- 0;
3540 method getitemcount = Array.length FMTE.names
3541 method getitem n = (FMTE.names.(n), 0)
3542 method exit ~uioh ~cancel ~active ~first ~pan =
3543 ignore (uioh, first, pan);
3544 if not cancel then set active;
3545 None
3546 method hasaction _ = true
3547 end)
3549 state.text <- E.s;
3550 let modehash = findkeyhash conf "info" in
3551 coe (new listview ~zebra:false ~helpmode:false
3552 ~source ~trusted:true ~modehash)
3553 )) :: m_l
3555 method caption s offset =
3556 m_l <- (s, `empty, offset, Noaction) :: m_l
3558 method caption2 s f offset =
3559 m_l <- (s, `string f, offset, Noaction) :: m_l
3561 method getitemcount = Array.length m_a
3563 method getitem n =
3564 let tostr = function
3565 | `int f -> string_of_int (f ())
3566 | `intws f -> string_with_suffix_of_int (f ())
3567 | `string f -> f ()
3568 | `color f -> color_to_string (f ())
3569 | `bool (btos, f) -> btos (f ())
3570 | `empty -> E.s
3572 let name, t, offset, _ = m_a.(n) in
3573 ((let s = tostr t in
3574 if nonemptystr s
3575 then Printf.sprintf "%s\t%s" name s
3576 else name),
3577 offset)
3579 method exit ~uioh ~cancel ~active ~first ~pan =
3580 let uiohopt =
3581 if not cancel
3582 then (
3583 let uioh =
3584 match m_a.(active) with
3585 | _, _, _, Action f -> f uioh
3586 | _, _, _, Noaction -> uioh
3588 Some uioh
3590 else None
3592 m_active <- active;
3593 m_first <- first;
3594 m_pan <- pan;
3595 uiohopt
3597 method hasaction n =
3598 match m_a.(n) with
3599 | _, _, _, Action _ -> true
3600 | _, _, _, Noaction -> false
3602 initializer m_active <- 1
3603 end)
3605 let rec fillsrc prevmode prevuioh =
3606 let sep () = src#caption E.s 0 in
3607 let colorp name get set =
3608 src#string name
3609 (fun () -> color_to_string (get ()))
3610 (fun v ->
3612 let c = color_of_string v in
3613 set c
3614 with exn ->
3615 state.text <- Printf.sprintf "bad color `%s': %s" v @@ exntos exn
3618 let oldmode = state.mode in
3619 let birdseye = isbirdseye state.mode in
3621 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3623 src#bool "presentation mode"
3624 (fun () -> conf.presentation)
3625 (fun v -> setpresentationmode v);
3627 src#bool "ignore case in searches"
3628 (fun () -> conf.icase)
3629 (fun v -> conf.icase <- v);
3631 src#bool "preload"
3632 (fun () -> conf.preload)
3633 (fun v -> conf.preload <- v);
3635 src#bool "highlight links"
3636 (fun () -> conf.hlinks)
3637 (fun v -> conf.hlinks <- v);
3639 src#bool "under info"
3640 (fun () -> conf.underinfo)
3641 (fun v -> conf.underinfo <- v);
3643 src#bool "persistent bookmarks"
3644 (fun () -> conf.savebmarks)
3645 (fun v -> conf.savebmarks <- v);
3647 src#fitmodel "fit model"
3648 (fun () -> FMTE.to_string conf.fitmodel)
3649 (fun v -> reqlayout conf.angle (FMTE.of_int v));
3651 src#bool "trim margins"
3652 (fun () -> conf.trimmargins)
3653 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3655 src#bool "persistent location"
3656 (fun () -> conf.jumpback)
3657 (fun v -> conf.jumpback <- v);
3659 sep ();
3660 src#int "inter-page space"
3661 (fun () -> conf.interpagespace)
3662 (fun n ->
3663 conf.interpagespace <- n;
3664 docolumns conf.columns;
3665 let pageno, py =
3666 match state.layout with
3667 | [] -> 0, 0
3668 | l :: _ ->
3669 l.pageno, l.pagey
3671 state.maxy <- calcheight ();
3672 let y = getpagey pageno in
3673 gotoy (y + py)
3676 src#int "page bias"
3677 (fun () -> conf.pagebias)
3678 (fun v -> conf.pagebias <- v);
3680 src#int "scroll step"
3681 (fun () -> conf.scrollstep)
3682 (fun n -> conf.scrollstep <- n);
3684 src#int "horizontal scroll step"
3685 (fun () -> conf.hscrollstep)
3686 (fun v -> conf.hscrollstep <- v);
3688 src#int "auto scroll step"
3689 (fun () ->
3690 match state.autoscroll with
3691 | Some step -> step
3692 | _ -> conf.autoscrollstep)
3693 (fun n ->
3694 let n = boundastep state.winh n in
3695 if state.autoscroll <> None
3696 then state.autoscroll <- Some n;
3697 conf.autoscrollstep <- n);
3699 src#int "zoom"
3700 (fun () -> truncate (conf.zoom *. 100.))
3701 (fun v -> setzoom ((float v) /. 100.));
3703 src#int "rotation"
3704 (fun () -> conf.angle)
3705 (fun v -> reqlayout v conf.fitmodel);
3707 src#int "scroll bar width"
3708 (fun () -> conf.scrollbw)
3709 (fun v ->
3710 conf.scrollbw <- v;
3711 reshape state.winw state.winh;
3714 src#int "scroll handle height"
3715 (fun () -> conf.scrollh)
3716 (fun v -> conf.scrollh <- v;);
3718 src#int "thumbnail width"
3719 (fun () -> conf.thumbw)
3720 (fun v ->
3721 conf.thumbw <- min 4096 v;
3722 match oldmode with
3723 | Birdseye beye ->
3724 leavebirdseye beye false;
3725 enterbirdseye ()
3726 | Textentry _
3727 | View
3728 | LinkNav _ -> ()
3731 let mode = state.mode in
3732 src#string "columns"
3733 (fun () ->
3734 match conf.columns with
3735 | Csingle _ -> "1"
3736 | Cmulti (multi, _) -> multicolumns_to_string multi
3737 | Csplit (count, _) -> "-" ^ string_of_int count
3739 (fun v ->
3740 let n, a, b = multicolumns_of_string v in
3741 setcolumns mode n a b);
3743 sep ();
3744 src#caption "Pixmap cache" 0;
3745 src#int_with_suffix "size (advisory)"
3746 (fun () -> conf.memlimit)
3747 (fun v -> conf.memlimit <- v);
3749 src#caption2 "used"
3750 (fun () -> Printf.sprintf "%s bytes, %d tiles"
3751 (string_with_suffix_of_int state.memused)
3752 (Hashtbl.length state.tilemap)) 1;
3754 sep ();
3755 src#caption "Layout" 0;
3756 src#caption2 "Dimension"
3757 (fun () ->
3758 Printf.sprintf "%dx%d (virtual %dx%d)"
3759 state.winw state.winh
3760 state.w state.maxy)
3762 if conf.debug
3763 then
3764 src#caption2 "Position" (fun () ->
3765 Printf.sprintf "%dx%d" state.x state.y
3767 else
3768 src#caption2 "Position" (fun () -> describe_location ()) 1
3771 sep ();
3772 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3773 "Save these parameters as global defaults at exit"
3774 (fun () -> conf.bedefault)
3775 (fun v -> conf.bedefault <- v)
3778 sep ();
3779 let btos b = if b then "@Ulguillemet" else "@Urguillemet" in
3780 src#bool ~offset:0 ~btos "Extended parameters"
3781 (fun () -> !showextended)
3782 (fun v -> showextended := v; fillsrc prevmode prevuioh);
3783 if !showextended
3784 then (
3785 src#bool "checkers"
3786 (fun () -> conf.checkers)
3787 (fun v -> conf.checkers <- v; setcheckers v);
3788 src#bool "update cursor"
3789 (fun () -> conf.updatecurs)
3790 (fun v -> conf.updatecurs <- v);
3791 src#bool "scroll-bar on the left"
3792 (fun () -> conf.leftscroll)
3793 (fun v -> conf.leftscroll <- v);
3794 src#bool "verbose"
3795 (fun () -> conf.verbose)
3796 (fun v -> conf.verbose <- v);
3797 src#bool "invert colors"
3798 (fun () -> conf.invert)
3799 (fun v -> conf.invert <- v);
3800 src#bool "max fit"
3801 (fun () -> conf.maxhfit)
3802 (fun v -> conf.maxhfit <- v);
3803 src#bool "pax mode"
3804 (fun () -> conf.pax != None)
3805 (fun v ->
3806 if v
3807 then conf.pax <- Some (ref (now (), 0, 0))
3808 else conf.pax <- None);
3809 src#string "uri launcher"
3810 (fun () -> conf.urilauncher)
3811 (fun v -> conf.urilauncher <- v);
3812 src#string "path launcher"
3813 (fun () -> conf.pathlauncher)
3814 (fun v -> conf.pathlauncher <- v);
3815 src#string "tile size"
3816 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3817 (fun v ->
3819 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
3820 conf.tilew <- max 64 w;
3821 conf.tileh <- max 64 h;
3822 flushtiles ();
3823 with exn ->
3824 state.text <- Printf.sprintf "bad tile size `%s': %s"
3825 v @@ exntos exn
3827 src#int "texture count"
3828 (fun () -> conf.texcount)
3829 (fun v ->
3830 if realloctexts v
3831 then conf.texcount <- v
3832 else impmsg "failed to set texture count please retry later"
3834 src#int "slice height"
3835 (fun () -> conf.sliceheight)
3836 (fun v ->
3837 conf.sliceheight <- v;
3838 wcmd "sliceh %d" conf.sliceheight;
3840 src#int "anti-aliasing level"
3841 (fun () -> conf.aalevel)
3842 (fun v ->
3843 conf.aalevel <- bound v 0 8;
3844 state.anchor <- getanchor ();
3845 opendoc state.path state.password;
3847 src#string "page scroll scaling factor"
3848 (fun () -> string_of_float conf.pgscale)
3849 (fun v ->
3851 let s = float_of_string v in
3852 conf.pgscale <- s
3853 with exn ->
3854 state.text <- Printf.sprintf
3855 "bad page scroll scaling factor `%s': %s" v @@ exntos exn
3858 src#int "ui font size"
3859 (fun () -> fstate.fontsize)
3860 (fun v -> setfontsize (bound v 5 100));
3861 src#int "hint font size"
3862 (fun () -> conf.hfsize)
3863 (fun v -> conf.hfsize <- bound v 5 100);
3864 colorp "background color"
3865 (fun () -> conf.bgcolor)
3866 (fun v -> conf.bgcolor <- v);
3867 src#bool "crop hack"
3868 (fun () -> conf.crophack)
3869 (fun v -> conf.crophack <- v);
3870 src#string "trim fuzz"
3871 (fun () -> irect_to_string conf.trimfuzz)
3872 (fun v ->
3874 conf.trimfuzz <- irect_of_string v;
3875 if conf.trimmargins
3876 then settrim true conf.trimfuzz;
3877 with exn ->
3878 state.text <- Printf.sprintf "bad irect `%s': %s" v @@ exntos exn
3880 src#string "throttle"
3881 (fun () ->
3882 match conf.maxwait with
3883 | None -> "show place holder if page is not ready"
3884 | Some time ->
3885 if time = infinity
3886 then "wait for page to fully render"
3887 else
3888 "wait " ^ string_of_float time
3889 ^ " seconds before showing placeholder"
3891 (fun v ->
3893 let f = float_of_string v in
3894 if f <= 0.0
3895 then conf.maxwait <- None
3896 else conf.maxwait <- Some f
3897 with exn ->
3898 state.text <- Printf.sprintf "bad time `%s': %s" v @@ exntos exn
3900 src#string "ghyll scroll"
3901 (fun () ->
3902 match conf.ghyllscroll with
3903 | None -> E.s
3904 | Some nab -> ghyllscroll_to_string nab
3906 (fun v ->
3907 try conf.ghyllscroll <- ghyllscroll_of_string v
3908 with
3909 | Failure msg ->
3910 state.text <- Printf.sprintf "bad ghyll `%s': %s" v msg
3911 | exn ->
3912 state.text <- Printf.sprintf "bad ghyll `%s': %s" v @@ exntos exn
3914 src#string "selection command"
3915 (fun () -> conf.selcmd)
3916 (fun v -> conf.selcmd <- v);
3917 src#string "synctex command"
3918 (fun () -> conf.stcmd)
3919 (fun v -> conf.stcmd <- v);
3920 src#string "pax command"
3921 (fun () -> conf.paxcmd)
3922 (fun v -> conf.paxcmd <- v);
3923 src#string "ask password command"
3924 (fun () -> conf.passcmd)
3925 (fun v -> conf.passcmd <- v);
3926 src#string "save path command"
3927 (fun () -> conf.savecmd)
3928 (fun v -> conf.savecmd <- v);
3929 src#colorspace "color space"
3930 (fun () -> CSTE.to_string conf.colorspace)
3931 (fun v ->
3932 conf.colorspace <- CSTE.of_int v;
3933 wcmd "cs %d" v;
3934 load state.layout;
3936 src#paxmark "pax mark method"
3937 (fun () -> MTE.to_string conf.paxmark)
3938 (fun v -> conf.paxmark <- MTE.of_int v);
3939 if pbousable ()
3940 then
3941 src#bool "use PBO"
3942 (fun () -> conf.usepbo)
3943 (fun v -> conf.usepbo <- v);
3944 src#bool "mouse wheel scrolls pages"
3945 (fun () -> conf.wheelbypage)
3946 (fun v -> conf.wheelbypage <- v);
3947 src#bool "open remote links in a new instance"
3948 (fun () -> conf.riani)
3949 (fun v -> conf.riani <- v);
3950 src#bool "edit annotations inline"
3951 (fun () -> conf.annotinline)
3952 (fun v -> conf.annotinline <- v);
3955 sep ();
3956 src#caption "Document" 0;
3957 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
3958 src#caption2 "Pages"
3959 (fun () -> string_of_int state.pagecount) 1;
3960 src#caption2 "Dimensions"
3961 (fun () -> string_of_int (List.length state.pdims)) 1;
3962 if conf.trimmargins
3963 then (
3964 sep ();
3965 src#caption "Trimmed margins" 0;
3966 src#caption2 "Dimensions"
3967 (fun () -> string_of_int (List.length state.pdims)) 1;
3970 sep ();
3971 src#caption "OpenGL" 0;
3972 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
3973 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
3975 sep ();
3976 src#caption "Location" 0;
3977 if nonemptystr state.origin
3978 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
3979 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
3981 src#reset prevmode prevuioh;
3983 fun () ->
3984 state.text <- E.s;
3985 resetmstate ();
3986 let prevmode = state.mode
3987 and prevuioh = state.uioh in
3988 fillsrc prevmode prevuioh;
3989 let source = (src :> lvsource) in
3990 let modehash = findkeyhash conf "info" in
3991 state.uioh <- coe (object (self)
3992 inherit listview ~zebra:false ~helpmode:false
3993 ~source ~trusted:true ~modehash as super
3994 val mutable m_prevmemused = 0
3995 method! infochanged = function
3996 | Memused ->
3997 if m_prevmemused != state.memused
3998 then (
3999 m_prevmemused <- state.memused;
4000 G.postRedisplay "memusedchanged";
4002 | Pdim -> G.postRedisplay "pdimchanged"
4003 | Docinfo -> fillsrc prevmode prevuioh
4005 method! key key mask =
4006 if not (Wsi.withctrl mask)
4007 then
4008 match key with
4009 | @left | @kpleft -> coe (self#updownlevel ~-1)
4010 | @right | @kpright -> coe (self#updownlevel 1)
4011 | _ -> super#key key mask
4012 else super#key key mask
4013 end);
4014 G.postRedisplay "info";
4017 let enterhelpmode =
4018 let source =
4019 (object
4020 inherit lvsourcebase
4021 method getitemcount = Array.length state.help
4022 method getitem n =
4023 let s, l, _ = state.help.(n) in
4024 (s, l)
4026 method exit ~uioh ~cancel ~active ~first ~pan =
4027 let optuioh =
4028 if not cancel
4029 then (
4030 match state.help.(active) with
4031 | _, _, Action f -> Some (f uioh)
4032 | _, _, Noaction -> Some uioh
4034 else None
4036 m_active <- active;
4037 m_first <- first;
4038 m_pan <- pan;
4039 optuioh
4041 method hasaction n =
4042 match state.help.(n) with
4043 | _, _, Action _ -> true
4044 | _, _, Noaction -> false
4046 initializer
4047 m_active <- -1
4048 end)
4049 in fun () ->
4050 let modehash = findkeyhash conf "help" in
4051 resetmstate ();
4052 state.uioh <- coe (new listview
4053 ~zebra:false ~helpmode:true
4054 ~source ~trusted:true ~modehash);
4055 G.postRedisplay "help";
4058 let entermsgsmode =
4059 let msgsource =
4060 (object
4061 inherit lvsourcebase
4062 val mutable m_items = E.a
4064 method getitemcount = 1 + Array.length m_items
4066 method getitem n =
4067 if n = 0
4068 then "[Clear]", 0
4069 else m_items.(n-1), 0
4071 method exit ~uioh ~cancel ~active ~first ~pan =
4072 ignore uioh;
4073 if not cancel
4074 then (
4075 if active = 0
4076 then Buffer.clear state.errmsgs;
4078 m_active <- active;
4079 m_first <- first;
4080 m_pan <- pan;
4081 None
4083 method hasaction n =
4084 n = 0
4086 method reset =
4087 state.newerrmsgs <- false;
4088 let l = Str.split newlinere (Buffer.contents state.errmsgs) in
4089 m_items <- Array.of_list l
4091 initializer
4092 m_active <- 0
4093 end)
4094 in fun () ->
4095 state.text <- E.s;
4096 resetmstate ();
4097 msgsource#reset;
4098 let source = (msgsource :> lvsource) in
4099 let modehash = findkeyhash conf "listview" in
4100 state.uioh <- coe (object
4101 inherit listview ~zebra:false ~helpmode:false
4102 ~source ~trusted:false ~modehash as super
4103 method! display =
4104 if state.newerrmsgs
4105 then msgsource#reset;
4106 super#display
4107 end);
4108 G.postRedisplay "msgs";
4111 let getusertext s =
4112 let editor = getenvwithdef "EDITOR" E.s in
4113 if emptystr editor
4114 then E.s
4115 else
4116 let tmppath = Filename.temp_file "llpp" "note" in
4117 if nonemptystr s
4118 then (
4119 let oc = open_out tmppath in
4120 output_string oc s;
4121 close_out oc;
4123 let execstr = editor ^ " " ^ tmppath in
4124 let s =
4125 match spawn execstr [] with
4126 | (exception exn) ->
4127 impmsg "spawn(%S) failed: %s" execstr @@ exntos exn;
4129 | pid ->
4130 match Unix.waitpid [] pid with
4131 | (exception exn) ->
4132 impmsg "waitpid(%d) failed: %s" pid @@ exntos exn;
4134 | (_pid, status) ->
4135 match status with
4136 | Unix.WEXITED 0 -> filecontents tmppath
4137 | Unix.WEXITED n ->
4138 impmsg "editor process(%s) exited abnormally: %d" execstr n;
4140 | Unix.WSIGNALED n ->
4141 impmsg "editor process(%s) was killed by signal %d" execstr n;
4143 | Unix.WSTOPPED n ->
4144 impmsg "editor(%s) process was stopped by signal %d" execstr n;
4147 match Unix.unlink tmppath with
4148 | (exception exn) ->
4149 impmsg "failed to ulink %S: %s" tmppath @@ exntos exn;
4151 | () -> s
4154 let enterannotmode opaque slinkindex =
4155 let msgsource =
4156 (object
4157 inherit lvsourcebase
4158 val mutable m_text = E.s
4159 val mutable m_items = E.a
4161 method getitemcount = Array.length m_items
4163 method getitem n =
4164 let label, _func = m_items.(n) in
4165 label, 0
4167 method exit ~uioh ~cancel ~active ~first ~pan =
4168 ignore (uioh, first, pan);
4169 if not cancel
4170 then (
4171 let _label, func = m_items.(active) in
4172 func ()
4174 None
4176 method hasaction n = nonemptystr @@ fst m_items.(n)
4178 method reset s =
4179 let rec split accu b i =
4180 let p = b+i in
4181 if p = String.length s
4182 then (String.sub s b (p-b), unit) :: accu
4183 else
4184 if (i > 70 && s.[p] = ' ') || s.[p] = '\r' || s.[p] = '\n'
4185 then
4186 let ss = if i = 0 then E.s else String.sub s b i in
4187 split ((ss, unit)::accu) (p+1) 0
4188 else
4189 split accu b (i+1)
4191 let cleanup () =
4192 wcmd "freepage %s" (~> opaque);
4193 let keys =
4194 Hashtbl.fold (fun key opaque' accu ->
4195 if opaque' = opaque'
4196 then key :: accu else accu) state.pagemap []
4198 List.iter (Hashtbl.remove state.pagemap) keys;
4199 flushtiles ();
4200 gotoy state.y
4202 let dele () =
4203 delannot opaque slinkindex;
4204 cleanup ();
4206 let edit inline () =
4207 let update s =
4208 if emptystr s
4209 then dele ()
4210 else (
4211 modannot opaque slinkindex s;
4212 cleanup ();
4215 if inline
4216 then
4217 let mode = state.mode in
4218 state.mode <-
4219 Textentry (
4220 ("annotation: ", m_text, None, textentry, update, true),
4221 fun _ -> state.mode <- mode);
4222 state.text <- E.s;
4223 enttext ();
4224 else
4225 let s = getusertext m_text in
4226 update s
4228 m_text <- s;
4229 m_items <-
4230 ( "[Copy]", fun () -> selstring m_text)
4231 :: ("[Delete]", dele)
4232 :: ("[Edit]", edit conf.annotinline)
4233 :: (E.s, unit)
4234 :: split [] 0 0 |> List.rev |> Array.of_list
4236 initializer
4237 m_active <- 0
4238 end)
4240 state.text <- E.s;
4241 let s = getannotcontents opaque slinkindex in
4242 resetmstate ();
4243 msgsource#reset s;
4244 let source = (msgsource :> lvsource) in
4245 let modehash = findkeyhash conf "listview" in
4246 state.uioh <- coe (object
4247 inherit listview ~zebra:false ~helpmode:false
4248 ~source ~trusted:false ~modehash
4249 end);
4250 G.postRedisplay "enterannotmode";
4253 let gotounder under =
4254 let getpath filename =
4255 let path =
4256 if nonemptystr filename
4257 then
4258 if Filename.is_relative filename
4259 then
4260 let dir = Filename.dirname state.path in
4261 let dir =
4262 if Filename.is_implicit dir
4263 then Filename.concat (Sys.getcwd ()) dir
4264 else dir
4266 Filename.concat dir filename
4267 else filename
4268 else E.s
4270 if Sys.file_exists path
4271 then path
4272 else E.s
4274 match under with
4275 | Ulinkgoto (pageno, top) ->
4276 if pageno >= 0
4277 then (
4278 addnav ();
4279 gotopage1 pageno top;
4282 | Ulinkuri s -> gotouri s
4284 | Uremote (filename, pageno) ->
4285 let path = getpath filename in
4286 if nonemptystr path
4287 then (
4288 if conf.riani
4289 then
4290 let command = Printf.sprintf "%s -page %d %S" !selfexec pageno path in
4291 match spawn command [] with
4292 | _pid -> ()
4293 | (exception exn) ->
4294 dolog "failed to execute `%s': %s" command @@ exntos exn
4295 else
4296 let anchor = getanchor () in
4297 let ranchor = state.path, state.password, anchor, state.origin in
4298 state.origin <- E.s;
4299 state.anchor <- (pageno, 0.0, 0.0);
4300 state.ranchors <- ranchor :: state.ranchors;
4301 opendoc path E.s;
4303 else impmsg "cannot find %s" filename
4305 | Uremotedest (filename, destname) ->
4306 let path = getpath filename in
4307 if nonemptystr path
4308 then (
4309 if conf.riani
4310 then
4311 let command = !selfexec ^ " " ^ path ^ " -dest " ^ destname in
4312 match spawn command [] with
4313 | (exception exn) ->
4314 dolog "failed to execute `%s': %s" command @@ exntos exn
4315 | _pid -> ()
4316 else
4317 let anchor = getanchor () in
4318 let ranchor = state.path, state.password, anchor, state.origin in
4319 state.origin <- E.s;
4320 state.nameddest <- destname;
4321 state.ranchors <- ranchor :: state.ranchors;
4322 opendoc path E.s;
4324 else impmsg "cannot find %s" filename
4326 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4327 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
4330 let gotooutline (_, _, kind) =
4331 match kind with
4332 | Onone -> ()
4333 | Oanchor anchor ->
4334 let (pageno, y, _) = anchor in
4335 let y = getanchory
4336 (if conf.presentation then (pageno, y, 1.0) else anchor)
4338 addnav ();
4339 gotoghyll y
4340 | Ouri uri -> gotounder (Ulinkuri uri)
4341 | Olaunch cmd -> gotounder (Ulaunch cmd)
4342 | Oremote remote -> gotounder (Uremote remote)
4343 | Ohistory hist -> gotohist hist
4344 | Oremotedest remotedest -> gotounder (Uremotedest remotedest)
4347 let outlinesource fetchoutlines =
4348 (object (self)
4349 inherit lvsourcebase
4350 val mutable m_items = E.a
4351 val mutable m_minfo = E.a
4352 val mutable m_orig_items = E.a
4353 val mutable m_orig_minfo = E.a
4354 val mutable m_narrow_patterns = []
4355 val mutable m_gen = -1
4357 method getitemcount = Array.length m_items
4359 method getitem n =
4360 let s, n, _ = m_items.(n) in
4361 (s, n)
4363 method exit ~uioh ~cancel ~active ~first ~pan =
4364 ignore (uioh, first);
4365 let items, minfo =
4366 if m_narrow_patterns = []
4367 then m_orig_items, m_orig_minfo
4368 else m_items, m_minfo
4370 m_pan <- pan;
4371 if not cancel
4372 then (
4373 m_items <- items;
4374 m_minfo <- minfo;
4375 gotooutline m_items.(active);
4377 else (
4378 m_items <- items;
4379 m_minfo <- minfo;
4381 None
4383 method hasaction _ = true
4385 method greetmsg =
4386 if Array.length m_items != Array.length m_orig_items
4387 then
4388 let s =
4389 match m_narrow_patterns with
4390 | one :: [] -> one
4391 | many -> String.concat "@Uellipsis" (List.rev many)
4393 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
4394 else E.s
4396 method statestr =
4397 match m_narrow_patterns with
4398 | [] -> E.s
4399 | one :: [] -> one
4400 | head :: _ -> "@Uellipsis" ^ head
4402 method narrow pattern =
4403 match Str.regexp_case_fold pattern with
4404 | (exception _) -> ()
4405 | re ->
4406 let rec loop accu minfo n =
4407 if n = -1
4408 then (
4409 m_items <- Array.of_list accu;
4410 m_minfo <- Array.of_list minfo;
4412 else
4413 let (s, _, _) as o = m_items.(n) in
4414 let accu, minfo =
4415 match Str.search_forward re s 0 with
4416 | (exception Not_found) -> accu, minfo
4417 | first -> o :: accu, (first, Str.match_end ()) :: minfo
4419 loop accu minfo (n-1)
4421 loop [] [] (Array.length m_items - 1)
4423 method! getminfo = m_minfo
4425 method denarrow =
4426 m_orig_items <- fetchoutlines ();
4427 m_minfo <- m_orig_minfo;
4428 m_items <- m_orig_items
4430 method add_narrow_pattern pattern =
4431 m_narrow_patterns <- pattern :: m_narrow_patterns
4433 method del_narrow_pattern =
4434 match m_narrow_patterns with
4435 | _ :: rest -> m_narrow_patterns <- rest
4436 | [] -> ()
4438 method renarrow =
4439 self#denarrow;
4440 match m_narrow_patterns with
4441 | pattern :: [] -> self#narrow pattern; pattern
4442 | list ->
4443 List.fold_left (fun accu pattern ->
4444 self#narrow pattern;
4445 pattern ^ "@Uellipsis" ^ accu) E.s list
4447 method calcactive anchor =
4448 let rely = getanchory anchor in
4449 let rec loop n best bestd =
4450 if n = Array.length m_items
4451 then best
4452 else
4453 let _, _, kind = m_items.(n) in
4454 match kind with
4455 | Oanchor anchor ->
4456 let orely = getanchory anchor in
4457 let d = abs (orely - rely) in
4458 if d < bestd
4459 then loop (n+1) n d
4460 else loop (n+1) best bestd
4461 | Onone | Oremote _ | Olaunch _
4462 | Oremotedest _ | Ouri _ | Ohistory _ ->
4463 loop (n+1) best bestd
4465 loop 0 ~-1 max_int
4467 method reset anchor items =
4468 if state.gen != m_gen
4469 then (
4470 m_orig_items <- items;
4471 m_items <- items;
4472 m_narrow_patterns <- [];
4473 m_minfo <- E.a;
4474 m_orig_minfo <- E.a;
4475 m_gen <- state.gen;
4477 else (
4478 if items != m_orig_items
4479 then (
4480 m_orig_items <- items;
4481 if m_narrow_patterns == []
4482 then m_items <- items;
4485 let active = self#calcactive anchor in
4486 m_active <- active;
4487 m_first <- firstof m_first active
4488 end)
4491 let enteroutlinemode, enterbookmarkmode, enterhistmode =
4492 let mkselector sourcetype =
4493 let fetchoutlines () =
4494 match sourcetype with
4495 | `bookmarks -> Array.of_list state.bookmarks
4496 | `outlines -> state.outlines
4497 | `history -> genhistoutlines ()
4499 let source = outlinesource fetchoutlines in
4500 fun errmsg ->
4501 let outlines = fetchoutlines () in
4502 if Array.length outlines = 0
4503 then (
4504 showtext ' ' errmsg;
4506 else (
4507 resetmstate ();
4508 Wsi.setcursor Wsi.CURSOR_INHERIT;
4509 let anchor = getanchor () in
4510 source#reset anchor outlines;
4511 state.text <- source#greetmsg;
4512 state.uioh <-
4513 coe (new outlinelistview ~zebra:(sourcetype=`history) ~source);
4514 G.postRedisplay "enter selector";
4517 let mkenter sourcetype errmsg =
4518 let enter = mkselector sourcetype in
4519 fun () -> enter errmsg
4521 (**)mkenter `outlines "document has no outline"
4522 , mkenter `bookmarks "document has no bookmarks (yet)"
4523 , mkenter `history "history is empty"
4526 let quickbookmark ?title () =
4527 match state.layout with
4528 | [] -> ()
4529 | l :: _ ->
4530 let title =
4531 match title with
4532 | None ->
4533 let tm = Unix.localtime (now ()) in
4534 Printf.sprintf
4535 "Quick (page %d) (bookmarked at %02d/%02d/%d %02d:%02d)"
4536 (l.pageno+1)
4537 tm.Unix.tm_mday
4538 (tm.Unix.tm_mon+1)
4539 (tm.Unix.tm_year + 1900)
4540 tm.Unix.tm_hour
4541 tm.Unix.tm_min
4542 | Some title -> title
4544 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4547 let setautoscrollspeed step goingdown =
4548 let incr = max 1 ((abs step) / 2) in
4549 let incr = if goingdown then incr else -incr in
4550 let astep = boundastep state.winh (step + incr) in
4551 state.autoscroll <- Some astep;
4554 let canpan () =
4555 match conf.columns with
4556 | Csplit _ -> true
4557 | Csingle _ | Cmulti _ -> state.x != 0 || conf.zoom > 1.0
4560 let panbound x = bound x (-state.w) (wadjsb () + state.winw);;
4562 let existsinrow pageno (columns, coverA, coverB) p =
4563 let last = ((pageno - coverA) mod columns) + columns in
4564 let rec any = function
4565 | [] -> false
4566 | l :: rest ->
4567 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4568 then p l
4569 else (
4570 if not (p l)
4571 then (if l.pageno = last then false else any rest)
4572 else true
4575 any state.layout
4578 let nextpage () =
4579 match state.layout with
4580 | [] ->
4581 let pageno = page_of_y state.y in
4582 gotoghyll (getpagey (pageno+1))
4583 | l :: rest ->
4584 match conf.columns with
4585 | Csingle _ ->
4586 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4587 then
4588 let y = clamp (pgscale state.winh) in
4589 gotoghyll y
4590 else
4591 let pageno = min (l.pageno+1) (state.pagecount-1) in
4592 gotoghyll (getpagey pageno)
4593 | Cmulti ((c, _, _) as cl, _) ->
4594 if conf.presentation
4595 && (existsinrow l.pageno cl
4596 (fun l -> l.pageh > l.pagey + l.pagevh))
4597 then
4598 let y = clamp (pgscale state.winh) in
4599 gotoghyll y
4600 else
4601 let pageno = min (l.pageno+c) (state.pagecount-1) in
4602 gotoghyll (getpagey pageno)
4603 | Csplit (n, _) ->
4604 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4605 then
4606 let pagey, pageh = getpageyh l.pageno in
4607 let pagey = pagey + pageh * l.pagecol in
4608 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4609 gotoghyll (pagey + pageh + ips)
4612 let prevpage () =
4613 match state.layout with
4614 | [] ->
4615 let pageno = page_of_y state.y in
4616 gotoghyll (getpagey (pageno-1))
4617 | l :: _ ->
4618 match conf.columns with
4619 | Csingle _ ->
4620 if conf.presentation && l.pagey != 0
4621 then
4622 gotoghyll (clamp (pgscale ~-(state.winh)))
4623 else
4624 let pageno = max 0 (l.pageno-1) in
4625 gotoghyll (getpagey pageno)
4626 | Cmulti ((c, _, coverB) as cl, _) ->
4627 if conf.presentation &&
4628 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4629 then
4630 gotoghyll (clamp (pgscale ~-(state.winh)))
4631 else
4632 let decr =
4633 if l.pageno = state.pagecount - coverB
4634 then 1
4635 else c
4637 let pageno = max 0 (l.pageno-decr) in
4638 gotoghyll (getpagey pageno)
4639 | Csplit (n, _) ->
4640 let y =
4641 if l.pagecol = 0
4642 then
4643 if l.pageno = 0
4644 then l.pagey
4645 else
4646 let pageno = max 0 (l.pageno-1) in
4647 let pagey, pageh = getpageyh pageno in
4648 pagey + (n-1)*pageh
4649 else
4650 let pagey, pageh = getpageyh l.pageno in
4651 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4653 gotoghyll y
4656 let save () =
4657 if emptystr conf.savecmd
4658 then error "don't know where to save modified document"
4659 else
4660 let savecmd = Str.global_replace percentsre state.path conf.savecmd in
4661 let path =
4662 getcmdoutput
4663 (fun s -> error "failed to obtain path to the saved copy: %s" s)
4664 savecmd
4666 if nonemptystr path
4667 then
4668 let tmp = path ^ ".tmp" in
4669 savedoc tmp;
4670 Unix.rename tmp path;
4673 let viewkeyboard key mask =
4674 let enttext te =
4675 let mode = state.mode in
4676 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4677 state.text <- E.s;
4678 enttext ();
4679 G.postRedisplay "view:enttext"
4681 let ctrl = Wsi.withctrl mask in
4682 let key =
4683 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
4685 match key with
4686 | @Q -> exit 0
4688 | @W ->
4689 if hasunsavedchanges ()
4690 then save ()
4692 | @insert ->
4693 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4694 then (
4695 state.mode <- LinkNav (Ltgendir 0);
4696 gotoy state.y;
4698 else impmsg "keyboard link navigation does not work under rotation"
4700 | @escape | @q ->
4701 begin match state.mstate with
4702 | Mzoomrect _ ->
4703 resetmstate ();
4704 G.postRedisplay "kill rect";
4705 | Msel _
4706 | Mpan _
4707 | Mscrolly | Mscrollx
4708 | Mzoom _
4709 | Mnone ->
4710 begin match state.mode with
4711 | LinkNav _ ->
4712 state.mode <- View;
4713 G.postRedisplay "esc leave linknav"
4714 | Birdseye _
4715 | Textentry _
4716 | View ->
4717 match state.ranchors with
4718 | [] -> raise Quit
4719 | (path, password, anchor, origin) :: rest ->
4720 state.ranchors <- rest;
4721 state.anchor <- anchor;
4722 state.origin <- origin;
4723 state.nameddest <- E.s;
4724 opendoc path password
4725 end;
4726 end;
4728 | @backspace ->
4729 gotoghyll (getnav ~-1)
4731 | @o ->
4732 enteroutlinemode ()
4734 | @H ->
4735 enterhistmode ()
4737 | @u ->
4738 state.rects <- [];
4739 state.text <- E.s;
4740 Hashtbl.iter (fun _ opaque ->
4741 clearmark opaque;
4742 Hashtbl.clear state.prects) state.pagemap;
4743 G.postRedisplay "dehighlight";
4745 | @slash | @question ->
4746 let ondone isforw s =
4747 cbput state.hists.pat s;
4748 state.searchpattern <- s;
4749 search s isforw
4751 let s = String.make 1 (Char.chr key) in
4752 enttext (s, E.s, Some (onhist state.hists.pat),
4753 textentry, ondone (key = @slash), true)
4755 | @plus | @kpplus | @equals when ctrl ->
4756 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4757 setzoom (conf.zoom +. incr)
4759 | @plus | @kpplus ->
4760 let ondone s =
4761 let n =
4762 try int_of_string s with exc ->
4763 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exc;
4764 max_int
4766 if n != max_int
4767 then (
4768 conf.pagebias <- n;
4769 state.text <- "page bias is now " ^ string_of_int n;
4772 enttext ("page bias: ", E.s, None, intentry, ondone, true)
4774 | @minus | @kpminus when ctrl ->
4775 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4776 setzoom (max 0.01 (conf.zoom -. decr))
4778 | @minus | @kpminus ->
4779 let ondone msg = state.text <- msg in
4780 enttext (
4781 "option [acfhilpstvxACFPRSZTISM]: ", E.s, None,
4782 optentry state.mode, ondone, true
4785 | @0 when ctrl ->
4786 if conf.zoom = 1.0
4787 then (
4788 state.x <- 0;
4789 gotoy state.y
4791 else setzoom 1.0
4793 | (@1 | @2) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
4794 let cols =
4795 match conf.columns with
4796 | Csingle _ | Cmulti _ -> 1
4797 | Csplit (n, _) -> n
4799 let h = state.winh -
4800 conf.interpagespace lsl (if conf.presentation then 1 else 0)
4802 let zoom = zoomforh state.winw h (vscrollw ()) cols in
4803 if zoom > 0.0 && (key = 50 || zoom < 1.0)
4804 then setzoom zoom
4806 | @3 when ctrl ->
4807 let fm =
4808 match conf.fitmodel with
4809 | FitWidth -> FitProportional
4810 | FitProportional -> FitPage
4811 | FitPage -> FitWidth
4813 state.text <- "fit model: " ^ FMTE.to_string fm;
4814 reqlayout conf.angle fm
4816 | @F9 ->
4817 togglebirdseye ()
4819 | @9 when ctrl ->
4820 togglebirdseye ()
4822 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4823 when not ctrl -> (* 0..9 *)
4824 let ondone s =
4825 let n =
4826 try int_of_string s with exc ->
4827 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exc;
4830 if n >= 0
4831 then (
4832 addnav ();
4833 cbput state.hists.pag (string_of_int n);
4834 gotopage1 (n + conf.pagebias - 1) 0;
4837 let pageentry text key =
4838 match Char.unsafe_chr key with
4839 | 'g' -> TEdone text
4840 | _ -> intentry text key
4842 let text = String.make 1 (Char.chr key) in
4843 enttext (":", text, Some (onhist state.hists.pag),
4844 pageentry, ondone, true)
4846 | @b ->
4847 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
4848 reshape state.winw state.winh;
4850 | @B ->
4851 state.bzoom <- not state.bzoom;
4852 state.rects <- [];
4853 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
4855 | @l ->
4856 conf.hlinks <- not conf.hlinks;
4857 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4858 G.postRedisplay "toggle highlightlinks";
4860 | @F ->
4861 if conf.angle mod 360 = 0
4862 then (
4863 state.glinks <- true;
4864 let mode = state.mode in
4865 state.mode <-
4866 Textentry (
4867 (":", E.s, None, linknentry, linknact gotounder, false),
4868 (fun _ ->
4869 state.glinks <- false;
4870 state.mode <- mode)
4872 state.text <- E.s;
4873 G.postRedisplay "view:linkent(F)"
4875 else impmsg "hint mode does not work under rotation"
4877 | @y ->
4878 state.glinks <- true;
4879 let mode = state.mode in
4880 state.mode <- Textentry (
4882 ":", E.s, None, linknentry, linknact (fun under ->
4883 selstring (undertext under);
4884 ), false
4886 fun _ ->
4887 state.glinks <- false;
4888 state.mode <- mode
4890 state.text <- E.s;
4891 G.postRedisplay "view:linkent"
4893 | @a ->
4894 begin match state.autoscroll with
4895 | Some step ->
4896 conf.autoscrollstep <- step;
4897 state.autoscroll <- None
4898 | None ->
4899 if conf.autoscrollstep = 0
4900 then state.autoscroll <- Some 1
4901 else state.autoscroll <- Some conf.autoscrollstep
4904 | @p when ctrl ->
4905 launchpath () (* XXX where do error messages go? *)
4907 | @P ->
4908 setpresentationmode (not conf.presentation);
4909 showtext ' ' ("presentation mode " ^
4910 if conf.presentation then "on" else "off");
4912 | @f ->
4913 if List.mem Wsi.Fullscreen state.winstate
4914 then Wsi.reshape conf.cwinw conf.cwinh
4915 else Wsi.fullscreen ()
4917 | @p | @N ->
4918 search state.searchpattern false
4920 | @n | @F3 ->
4921 search state.searchpattern true
4923 | @t ->
4924 begin match state.layout with
4925 | [] -> ()
4926 | l :: _ ->
4927 gotoghyll (getpagey l.pageno)
4930 | @space ->
4931 nextpage ()
4933 | @delete | @kpdelete -> (* delete *)
4934 prevpage ()
4936 | @equals ->
4937 showtext ' ' (describe_location ());
4939 | @w ->
4940 begin match state.layout with
4941 | [] -> ()
4942 | l :: _ ->
4943 Wsi.reshape (l.pagew + vscrollw ()) l.pageh;
4944 G.postRedisplay "w"
4947 | @apos ->
4948 enterbookmarkmode ()
4950 | @h | @F1 ->
4951 enterhelpmode ()
4953 | @i ->
4954 enterinfomode ()
4956 | @e when Buffer.length state.errmsgs > 0 ->
4957 entermsgsmode ()
4959 | @m ->
4960 let ondone s =
4961 match state.layout with
4962 | l :: _ ->
4963 if nonemptystr s
4964 then
4965 state.bookmarks <-
4966 (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4967 | _ -> ()
4969 enttext ("bookmark: ", E.s, None, textentry, ondone, true)
4971 | @tilde ->
4972 quickbookmark ();
4973 showtext ' ' "Quick bookmark added";
4975 | @z ->
4976 begin match state.layout with
4977 | l :: _ ->
4978 let rect = getpdimrect l.pagedimno in
4979 let w, h =
4980 if conf.crophack
4981 then
4982 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4983 truncate (1.2 *. (rect.(3) -. rect.(0))))
4984 else
4985 (truncate (rect.(1) -. rect.(0)),
4986 truncate (rect.(3) -. rect.(0)))
4988 let w = truncate ((float w)*.conf.zoom)
4989 and h = truncate ((float h)*.conf.zoom) in
4990 if w != 0 && h != 0
4991 then (
4992 state.anchor <- getanchor ();
4993 Wsi.reshape (w + vscrollw ()) (h + conf.interpagespace)
4995 G.postRedisplay "z";
4997 | [] -> ()
5000 | @x -> state.roam ()
5002 | @Lt | @Gt ->
5003 reqlayout (conf.angle +
5004 (if key = @Gt then 30 else -30)) conf.fitmodel
5006 | @Lb | @Rb ->
5007 conf.colorscale <-
5008 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5010 G.postRedisplay "brightness";
5012 | @c when state.mode = View ->
5013 if Wsi.withalt mask
5014 then (
5015 if conf.zoom > 1.0
5016 then
5017 let m = (wadjsb () + state.winw - state.w) / 2 in
5018 state.x <- m;
5019 gotoy_and_clear_text state.y
5021 else
5022 let (c, a, b), z =
5023 match state.prevcolumns with
5024 | None -> (1, 0, 0), 1.0
5025 | Some (columns, z) ->
5026 let cab =
5027 match columns with
5028 | Csplit (c, _) -> -c, 0, 0
5029 | Cmulti ((c, a, b), _) -> c, a, b
5030 | Csingle _ -> 1, 0, 0
5032 cab, z
5034 setcolumns View c a b;
5035 setzoom z
5037 | @down | @up when ctrl && Wsi.withshift mask ->
5038 let zoom, x = state.prevzoom in
5039 setzoom zoom;
5040 state.x <- x;
5042 | @k | @up | @kpup ->
5043 begin match state.autoscroll with
5044 | None ->
5045 begin match state.mode with
5046 | Birdseye beye -> upbirdseye 1 beye
5047 | Textentry _
5048 | View
5049 | LinkNav _ ->
5050 if ctrl
5051 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5052 else (
5053 if not (Wsi.withshift mask) && conf.presentation
5054 then prevpage ()
5055 else gotoghyll1 true (clamp (-conf.scrollstep))
5058 | Some n ->
5059 setautoscrollspeed n false
5062 | @j | @down | @kpdown ->
5063 begin match state.autoscroll with
5064 | None ->
5065 begin match state.mode with
5066 | Birdseye beye -> downbirdseye 1 beye
5067 | Textentry _
5068 | View
5069 | LinkNav _ ->
5070 if ctrl
5071 then gotoy_and_clear_text (clamp (state.winh/2))
5072 else (
5073 if not (Wsi.withshift mask) && conf.presentation
5074 then nextpage ()
5075 else gotoghyll1 true (clamp (conf.scrollstep))
5078 | Some n ->
5079 setautoscrollspeed n true
5082 | @left | @right | @kpleft | @kpright when not (Wsi.withalt mask) ->
5083 if canpan ()
5084 then
5085 let dx =
5086 if ctrl
5087 then state.winw / 2
5088 else conf.hscrollstep
5090 let dx = if key = @left || key = @kpleft then dx else -dx in
5091 state.x <- panbound (state.x + dx);
5092 gotoy_and_clear_text state.y
5093 else (
5094 state.text <- E.s;
5095 G.postRedisplay "left/right"
5098 | @prior | @kpprior ->
5099 let y =
5100 if ctrl
5101 then
5102 match state.layout with
5103 | [] -> state.y
5104 | l :: _ -> state.y - l.pagey
5105 else
5106 clamp (pgscale (-state.winh))
5108 gotoghyll y
5110 | @next | @kpnext ->
5111 let y =
5112 if ctrl
5113 then
5114 match List.rev state.layout with
5115 | [] -> state.y
5116 | l :: _ -> getpagey l.pageno
5117 else
5118 clamp (pgscale state.winh)
5120 gotoghyll y
5122 | @g | @home | @kphome ->
5123 addnav ();
5124 gotoghyll 0
5125 | @G | @jend | @kpend ->
5126 addnav ();
5127 gotoghyll (clamp state.maxy)
5129 | @right | @kpright when Wsi.withalt mask ->
5130 gotoghyll (getnav 1)
5131 | @left | @kpleft when Wsi.withalt mask ->
5132 gotoghyll (getnav ~-1)
5134 | @r ->
5135 reload ()
5137 | @v when conf.debug ->
5138 state.rects <- [];
5139 List.iter (fun l ->
5140 match getopaque l.pageno with
5141 | None -> ()
5142 | Some opaque ->
5143 let x0, y0, x1, y1 = pagebbox opaque in
5144 let a,b = float x0, float y0 in
5145 let c,d = float x1, float y0 in
5146 let e,f = float x1, float y1 in
5147 let h,j = float x0, float y1 in
5148 let rect = (a,b,c,d,e,f,h,j) in
5149 debugrect rect;
5150 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
5151 state.rects <- (l.pageno, color, rect) :: state.rects;
5152 ) state.layout;
5153 G.postRedisplay "v";
5155 | @pipe ->
5156 let mode = state.mode in
5157 let cmd = ref E.s in
5158 let onleave = function
5159 | Cancel -> state.mode <- mode
5160 | Confirm ->
5161 List.iter (fun l ->
5162 match getopaque l.pageno with
5163 | Some opaque -> pipesel opaque !cmd
5164 | None -> ()) state.layout;
5165 state.mode <- mode
5167 let ondone s =
5168 cbput state.hists.sel s;
5169 cmd := s
5171 let te =
5172 "| ", !cmd, Some (onhist state.hists.sel), textentry, ondone, true
5174 G.postRedisplay "|";
5175 state.mode <- Textentry (te, onleave);
5177 | _ ->
5178 vlog "huh? %s" (Wsi.keyname key)
5181 let linknavkeyboard key mask linknav =
5182 let getpage pageno =
5183 let rec loop = function
5184 | [] -> None
5185 | l :: _ when l.pageno = pageno -> Some l
5186 | _ :: rest -> loop rest
5187 in loop state.layout
5189 let doexact (pageno, n) =
5190 match getopaque pageno, getpage pageno with
5191 | Some opaque, Some l ->
5192 if key = @enter || key = @kpenter
5193 then
5194 let under = getlink opaque n in
5195 G.postRedisplay "link gotounder";
5196 gotounder under;
5197 state.mode <- View;
5198 else
5199 let opt, dir =
5200 match key with
5201 | @home ->
5202 Some (findlink opaque LDfirst), -1
5204 | @jend ->
5205 Some (findlink opaque LDlast), 1
5207 | @left ->
5208 Some (findlink opaque (LDleft n)), -1
5210 | @right ->
5211 Some (findlink opaque (LDright n)), 1
5213 | @up ->
5214 Some (findlink opaque (LDup n)), -1
5216 | @down ->
5217 Some (findlink opaque (LDdown n)), 1
5219 | _ -> None, 0
5221 let pwl l dir =
5222 begin match findpwl l.pageno dir with
5223 | Pwlnotfound -> ()
5224 | Pwl pageno ->
5225 let notfound dir =
5226 state.mode <- LinkNav (Ltgendir dir);
5227 let y, h = getpageyh pageno in
5228 let y =
5229 if dir < 0
5230 then y + h - state.winh
5231 else y
5233 gotoy y
5235 begin match getopaque pageno, getpage pageno with
5236 | Some opaque, Some _ ->
5237 let link =
5238 let ld = if dir > 0 then LDfirst else LDlast in
5239 findlink opaque ld
5241 begin match link with
5242 | Lfound m ->
5243 showlinktype (getlink opaque m);
5244 state.mode <- LinkNav (Ltexact (pageno, m));
5245 G.postRedisplay "linknav jpage";
5246 | Lnotfound -> notfound dir
5247 end;
5248 | _ -> notfound dir
5249 end;
5250 end;
5252 begin match opt with
5253 | Some Lnotfound -> pwl l dir;
5254 | Some (Lfound m) ->
5255 if m = n
5256 then pwl l dir
5257 else (
5258 let _, y0, _, y1 = getlinkrect opaque m in
5259 if y0 < l.pagey
5260 then gotopage1 l.pageno y0
5261 else (
5262 let d = fstate.fontsize + 1 in
5263 if y1 - l.pagey > l.pagevh - d
5264 then gotopage1 l.pageno (y1 - state.winh - hscrollh () + d)
5265 else G.postRedisplay "linknav";
5267 showlinktype (getlink opaque m);
5268 state.mode <- LinkNav (Ltexact (l.pageno, m));
5271 | None -> viewkeyboard key mask
5272 end;
5273 | _ -> viewkeyboard key mask
5275 if key = @insert
5276 then (
5277 state.mode <- View;
5278 G.postRedisplay "leave linknav"
5280 else
5281 match linknav with
5282 | Ltgendir _ | Ltnotready _ -> viewkeyboard key mask
5283 | Ltexact exact -> doexact exact
5286 let keyboard key mask =
5287 if (key = @g && Wsi.withctrl mask) && not (istextentry state.mode)
5288 then wcmd "interrupt"
5289 else state.uioh <- state.uioh#key key mask
5292 let birdseyekeyboard key mask
5293 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5294 let incr =
5295 match conf.columns with
5296 | Csingle _ -> 1
5297 | Cmulti ((c, _, _), _) -> c
5298 | Csplit _ -> failwith "bird's eye split mode"
5300 let pgh layout = List.fold_left
5301 (fun m l -> max l.pageh m) state.winh layout in
5302 match key with
5303 | @l when Wsi.withctrl mask ->
5304 let y, h = getpageyh pageno in
5305 let top = (state.winh - h) / 2 in
5306 gotoy (max 0 (y - top))
5307 | @enter | @kpenter -> leavebirdseye beye false
5308 | @escape -> leavebirdseye beye true
5309 | @up -> upbirdseye incr beye
5310 | @down -> downbirdseye incr beye
5311 | @left -> upbirdseye 1 beye
5312 | @right -> downbirdseye 1 beye
5314 | @prior ->
5315 begin match state.layout with
5316 | l :: _ ->
5317 if l.pagey != 0
5318 then (
5319 state.mode <- Birdseye (
5320 oconf, leftx, l.pageno, hooverpageno, anchor
5322 gotopage1 l.pageno 0;
5324 else (
5325 let layout = layout (state.y-state.winh) (pgh state.layout) in
5326 match layout with
5327 | [] -> gotoy (clamp (-state.winh))
5328 | l :: _ ->
5329 state.mode <- Birdseye (
5330 oconf, leftx, l.pageno, hooverpageno, anchor
5332 gotopage1 l.pageno 0
5335 | [] -> gotoy (clamp (-state.winh))
5336 end;
5338 | @next ->
5339 begin match List.rev state.layout with
5340 | l :: _ ->
5341 let layout = layout (state.y + (pgh state.layout)) state.winh in
5342 begin match layout with
5343 | [] ->
5344 let incr = l.pageh - l.pagevh in
5345 if incr = 0
5346 then (
5347 state.mode <-
5348 Birdseye (
5349 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5351 G.postRedisplay "birdseye pagedown";
5353 else gotoy (clamp (incr + conf.interpagespace*2));
5355 | l :: _ ->
5356 state.mode <-
5357 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5358 gotopage1 l.pageno 0;
5361 | [] -> gotoy (clamp state.winh)
5362 end;
5364 | @home ->
5365 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5366 gotopage1 0 0
5368 | @jend ->
5369 let pageno = state.pagecount - 1 in
5370 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5371 if not (pagevisible state.layout pageno)
5372 then
5373 let h =
5374 match List.rev state.pdims with
5375 | [] -> state.winh
5376 | (_, _, h, _) :: _ -> h
5378 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5379 else G.postRedisplay "birdseye end";
5381 | _ -> viewkeyboard key mask
5384 let drawpage l =
5385 let color =
5386 match state.mode with
5387 | Textentry _ -> scalecolor 0.4
5388 | LinkNav _
5389 | View -> scalecolor 1.0
5390 | Birdseye (_, _, pageno, hooverpageno, _) ->
5391 if l.pageno = hooverpageno
5392 then scalecolor 0.9
5393 else (
5394 if l.pageno = pageno
5395 then (
5396 let c = scalecolor 1.0 in
5397 GlDraw.color c;
5398 GlDraw.line_width 3.0;
5399 let dispx = xadjsb () + l.pagedispx in
5400 linerect
5401 (float (dispx-1)) (float (l.pagedispy-1))
5402 (float (dispx+l.pagevw+1))
5403 (float (l.pagedispy+l.pagevh+1))
5405 GlDraw.line_width 1.0;
5408 else scalecolor 0.8
5411 drawtiles l color;
5414 let postdrawpage l linkindexbase =
5415 match getopaque l.pageno with
5416 | Some opaque ->
5417 if tileready l l.pagex l.pagey
5418 then
5419 let x = l.pagedispx - l.pagex + xadjsb ()
5420 and y = l.pagedispy - l.pagey in
5421 let hlmask =
5422 match conf.columns with
5423 | Csingle _ | Cmulti _ ->
5424 (if conf.hlinks then 1 else 0)
5425 + (if state.glinks
5426 && not (isbirdseye state.mode) then 2 else 0)
5427 | Csplit _ -> 0
5429 let s =
5430 match state.mode with
5431 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5432 | Textentry _
5433 | Birdseye _
5434 | View
5435 | LinkNav _ -> E.s
5437 Hashtbl.find_all state.prects l.pageno |>
5438 List.iter (fun vals -> drawprect opaque x y vals);
5439 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5440 else 0
5441 | _ -> 0
5444 let scrollindicator () =
5445 let sbw, ph, sh = state.uioh#scrollph in
5446 let sbh, pw, sw = state.uioh#scrollpw in
5448 let x0,x1,hx0 =
5449 if conf.leftscroll
5450 then (0, sbw, sbw)
5451 else ((state.winw - sbw), state.winw, 0)
5454 GlDraw.color (0.64, 0.64, 0.64);
5455 filledrect (float x0) 0. (float x1) (float state.winh);
5456 filledrect
5457 (float hx0) (float (state.winh - sbh))
5458 (float (hx0 + wadjsb () + state.winw)) (float state.winh)
5460 GlDraw.color (0.0, 0.0, 0.0);
5462 filledrect (float x0) ph (float x1) (ph +. sh);
5463 let pw = pw +. float hx0 in
5464 filledrect pw (float (state.winh - sbh)) (pw +. sw) (float state.winh);
5467 let showsel () =
5468 match state.mstate with
5469 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5472 | Msel ((x0, y0), (x1, y1)) ->
5473 let identify opaque l px py = Some (opaque, l.pageno, px, py) in
5474 let o0,n0,px0,py0 = onppundermouse identify x0 y0 (~< E.s, -1, 0, 0) in
5475 let _o1,n1,px1,py1 = onppundermouse identify x1 y1 (~< E.s, -1, 0, 0) in
5476 if n0 != -1 && n0 = n1 then seltext o0 (px0, py0, px1, py1);
5479 let showrects = function [] -> () | rects ->
5480 Gl.enable `blend;
5481 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5482 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5483 List.iter
5484 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5485 List.iter (fun l ->
5486 if l.pageno = pageno
5487 then (
5488 let dx = float (l.pagedispx - l.pagex) in
5489 let dy = float (l.pagedispy - l.pagey) in
5490 let r, g, b, alpha = c in
5491 GlDraw.color (r, g, b) ~alpha;
5492 Raw.sets_float state.vraw ~pos:0
5493 [| x0+.dx; y0+.dy;
5494 x1+.dx; y1+.dy;
5495 x3+.dx; y3+.dy;
5496 x2+.dx; y2+.dy |];
5497 GlArray.vertex `two state.vraw;
5498 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
5500 ) state.layout
5501 ) rects
5503 Gl.disable `blend;
5506 let display () =
5507 GlClear.color (scalecolor2 conf.bgcolor);
5508 GlClear.clear [`color];
5509 List.iter drawpage state.layout;
5510 let rects =
5511 match state.mode with
5512 | LinkNav (Ltexact (pageno, linkno)) ->
5513 begin match getopaque pageno with
5514 | Some opaque ->
5515 let dx = xadjsb () in
5516 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5517 let x0 = x0 + dx and x1 = x1 + dx in
5518 let color = (0.0, 0.0, 0.5, 0.5) in
5519 (pageno, color, (
5520 float x0, float y0,
5521 float x1, float y0,
5522 float x1, float y1,
5523 float x0, float y1)
5524 ) :: state.rects
5525 | None -> state.rects
5527 | LinkNav (Ltgendir _) | LinkNav (Ltnotready _)
5528 | Birdseye _
5529 | Textentry _
5530 | View -> state.rects
5532 showrects rects;
5533 let rec postloop linkindexbase = function
5534 | l :: rest ->
5535 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5536 postloop linkindexbase rest
5537 | [] -> ()
5539 showsel ();
5540 postloop 0 state.layout;
5541 state.uioh#display;
5542 begin match state.mstate with
5543 | Mzoomrect ((x0, y0), (x1, y1)) ->
5544 Gl.enable `blend;
5545 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5546 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5547 filledrect (float x0) (float y0) (float x1) (float y1);
5548 Gl.disable `blend;
5549 | Msel _
5550 | Mpan _
5551 | Mscrolly | Mscrollx
5552 | Mzoom _
5553 | Mnone -> ()
5554 end;
5555 enttext ();
5556 scrollindicator ();
5557 Wsi.swapb ();
5560 let zoomrect x y x1 y1 =
5561 let x0 = min x x1
5562 and x1 = max x x1
5563 and y0 = min y y1 in
5564 gotoy (state.y + y0);
5565 state.anchor <- getanchor ();
5566 let zoom = (float state.w) /. float (x1 - x0) in
5567 let margin =
5568 let simple () =
5569 let adjw = wadjsb () + state.winw in
5570 if state.w < adjw
5571 then (adjw - state.w) / 2
5572 else 0
5574 match conf.fitmodel with
5575 | FitWidth | FitProportional -> simple ()
5576 | FitPage ->
5577 match conf.columns with
5578 | Csplit _ ->
5579 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
5580 | Cmulti _ | Csingle _ -> simple ()
5582 state.x <- (state.x + margin) - x0;
5583 setzoom zoom;
5584 resetmstate ();
5587 let annot inline x y =
5588 match unproject x y with
5589 | Some (opaque, n, ux, uy) ->
5590 let add text =
5591 addannot opaque ux uy text;
5592 wcmd "freepage %s" (~> opaque);
5593 Hashtbl.remove state.pagemap (n, state.gen);
5594 flushtiles ();
5595 gotoy state.y
5597 if inline
5598 then
5599 let ondone s = add s in
5600 let mode = state.mode in
5601 state.mode <- Textentry (
5602 ("annotation: ", E.s, None, textentry, ondone, true),
5603 fun _ -> state.mode <- mode);
5604 state.text <- E.s;
5605 enttext ();
5606 G.postRedisplay "annot"
5607 else
5608 add @@ getusertext E.s
5609 | _ -> ()
5612 let zoomblock x y =
5613 let g opaque l px py =
5614 match rectofblock opaque px py with
5615 | Some a ->
5616 let x0 = a.(0) -. 20. in
5617 let x1 = a.(1) +. 20. in
5618 let y0 = a.(2) -. 20. in
5619 let zoom = (float state.w) /. (x1 -. x0) in
5620 let pagey = getpagey l.pageno in
5621 gotoy_and_clear_text (pagey + truncate y0);
5622 state.anchor <- getanchor ();
5623 let margin = (state.w - l.pagew)/2 in
5624 state.x <- -truncate x0 - margin;
5625 setzoom zoom;
5626 None
5627 | None -> None
5629 match conf.columns with
5630 | Csplit _ ->
5631 impmsg "block zooming does not work properly in split columns mode"
5632 | Cmulti _ | Csingle _ -> onppundermouse g x y ()
5635 let scrollx x =
5636 let winw = wadjsb () + state.winw - 1 in
5637 let s = float x /. float winw in
5638 let destx = truncate (float (state.w + winw) *. s) in
5639 state.x <- winw - destx;
5640 gotoy_and_clear_text state.y;
5641 state.mstate <- Mscrollx;
5644 let scrolly y =
5645 let s = float y /. float state.winh in
5646 let desty = truncate (float (state.maxy - state.winh) *. s) in
5647 gotoy_and_clear_text desty;
5648 state.mstate <- Mscrolly;
5651 let viewmulticlick clicks x y mask =
5652 let g opaque l px py =
5653 let mark =
5654 match clicks with
5655 | 2 -> Mark_word
5656 | 3 -> Mark_line
5657 | 4 -> Mark_block
5658 | _ -> Mark_page
5660 if markunder opaque px py mark
5661 then (
5662 Some (fun () ->
5663 let dopipe cmd =
5664 match getopaque l.pageno with
5665 | None -> ()
5666 | Some opaque -> pipesel opaque cmd
5668 state.roam <- (fun () -> dopipe conf.paxcmd);
5669 if not (Wsi.withctrl mask) then dopipe conf.selcmd;
5672 else None
5674 G.postRedisplay "viewmulticlick";
5675 onppundermouse g x y (fun () -> impmsg "nothing to select") ();
5678 let canselect () =
5679 match conf.columns with
5680 | Csplit _ -> false
5681 | Csingle _ | Cmulti _ -> conf.angle mod 360 = 0
5684 let viewmouse button down x y mask =
5685 match button with
5686 | n when (n == 4 || n == 5) && not down ->
5687 if Wsi.withctrl mask
5688 then (
5689 match state.mstate with
5690 | Mzoom (oldn, i) ->
5691 if oldn = n
5692 then (
5693 if i = 2
5694 then
5695 let incr =
5696 match n with
5697 | 5 ->
5698 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5699 | _ ->
5700 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5702 let zoom = conf.zoom -. incr in
5703 setzoom zoom;
5704 state.mstate <- Mzoom (n, 0);
5705 else
5706 state.mstate <- Mzoom (n, i+1);
5708 else state.mstate <- Mzoom (n, 0)
5710 | Msel _
5711 | Mpan _
5712 | Mscrolly | Mscrollx
5713 | Mzoomrect _
5714 | Mnone -> state.mstate <- Mzoom (n, 0)
5716 else (
5717 match state.autoscroll with
5718 | Some step -> setautoscrollspeed step (n=4)
5719 | None ->
5720 if conf.wheelbypage || conf.presentation
5721 then (
5722 if n = 4
5723 then prevpage ()
5724 else nextpage ()
5726 else
5727 let incr =
5728 if n = 4
5729 then -conf.scrollstep
5730 else conf.scrollstep
5732 let incr = incr * 2 in
5733 let y = clamp incr in
5734 gotoy_and_clear_text y
5737 | n when (n = 6 || n = 7) && not down && canpan () ->
5738 state.x <-
5739 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
5740 gotoy_and_clear_text state.y
5742 | 1 when Wsi.withshift mask ->
5743 state.mstate <- Mnone;
5744 if not down
5745 then (
5746 match unproject x y with
5747 | None -> ()
5748 | Some (_, pageno, ux, uy) ->
5749 let cmd = Printf.sprintf
5750 "%s %s %d %d %d"
5751 conf.stcmd state.path pageno ux uy
5753 match spawn cmd [] with
5754 | (exception exn) ->
5755 impmsg "execution of synctex command(%S) failed: %S"
5756 conf.stcmd @@ exntos exn
5757 | _pid -> ()
5760 | 1 when Wsi.withctrl mask ->
5761 if down
5762 then (
5763 Wsi.setcursor Wsi.CURSOR_FLEUR;
5764 state.mstate <- Mpan (x, y)
5766 else
5767 state.mstate <- Mnone
5769 | 3 ->
5770 if down
5771 then (
5772 if Wsi.withshift mask
5773 then (
5774 annot conf.annotinline x y;
5775 G.postRedisplay "addannot"
5777 else
5778 let p = (x, y) in
5779 Wsi.setcursor Wsi.CURSOR_CYCLE;
5780 state.mstate <- Mzoomrect (p, p)
5782 else (
5783 match state.mstate with
5784 | Mzoomrect ((x0, y0), _) ->
5785 if abs (x-x0) > 10 && abs (y - y0) > 10
5786 then zoomrect x0 y0 x y
5787 else (
5788 resetmstate ();
5789 G.postRedisplay "kill accidental zoom rect";
5791 | Msel _
5792 | Mpan _
5793 | Mscrolly | Mscrollx
5794 | Mzoom _
5795 | Mnone ->
5796 resetmstate ()
5799 | 1 when vscrollhit x ->
5800 if down
5801 then
5802 let _, position, sh = state.uioh#scrollph in
5803 if y > truncate position && y < truncate (position +. sh)
5804 then state.mstate <- Mscrolly
5805 else scrolly y
5806 else
5807 state.mstate <- Mnone
5809 | 1 when y > state.winh - hscrollh () ->
5810 if down
5811 then
5812 let _, position, sw = state.uioh#scrollpw in
5813 if x > truncate position && x < truncate (position +. sw)
5814 then state.mstate <- Mscrollx
5815 else scrollx x
5816 else
5817 state.mstate <- Mnone
5819 | 1 when state.bzoom -> if not down then zoomblock x y
5821 | 1 ->
5822 let dest = if down then getunder x y else Unone in
5823 begin match dest with
5824 | Ulinkgoto _
5825 | Ulinkuri _
5826 | Uremote _ | Uremotedest _
5827 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5828 gotounder dest
5830 | Unone when down ->
5831 Wsi.setcursor Wsi.CURSOR_FLEUR;
5832 state.mstate <- Mpan (x, y);
5834 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
5836 | Unone | Utext _ ->
5837 if down
5838 then (
5839 if canselect ()
5840 then (
5841 state.mstate <- Msel ((x, y), (x, y));
5842 G.postRedisplay "mouse select";
5845 else (
5846 match state.mstate with
5847 | Mnone -> ()
5849 | Mzoom _ | Mscrollx | Mscrolly ->
5850 state.mstate <- Mnone
5852 | Mzoomrect ((x0, y0), _) ->
5853 zoomrect x0 y0 x y
5855 | Mpan _ ->
5856 Wsi.setcursor Wsi.CURSOR_INHERIT;
5857 state.mstate <- Mnone
5859 | Msel ((x0, y0), (x1, y1)) ->
5860 let rec loop = function
5861 | [] -> ()
5862 | l :: rest ->
5863 let inside =
5864 let a0 = l.pagedispy in
5865 let a1 = a0 + l.pagevh in
5866 let b0 = l.pagedispx in
5867 let b1 = b0 + l.pagevw in
5868 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5869 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5871 if inside
5872 then
5873 match getopaque l.pageno with
5874 | Some opaque ->
5875 let dosel cmd () =
5876 match Unix.pipe () with
5877 | (exception exn) ->
5878 impmsg "cannot create sel pipe: %s" @@
5879 exntos exn;
5880 | (r, w) ->
5881 let clo what fd =
5882 Ne.clo fd (fun msg ->
5883 dolog "%s close failed: %s" what msg)
5885 let pid =
5886 try spawn cmd [r, 0; w, -1]
5887 with exn ->
5888 dolog "cannot execute %S: %s"
5889 cmd @@ exntos exn;
5892 if pid > 0
5893 then (
5894 copysel w opaque;
5895 G.postRedisplay "copysel";
5897 else clo "Msel pipe/w" w;
5898 clo "Msel pipe/r" r;
5900 dosel conf.selcmd ();
5901 state.roam <- dosel conf.paxcmd;
5902 | None -> ()
5903 else loop rest
5905 loop state.layout;
5906 resetmstate ();
5910 | _ -> ()
5913 let birdseyemouse button down x y mask
5914 (conf, leftx, _, hooverpageno, anchor) =
5915 match button with
5916 | 1 when down ->
5917 let rec loop = function
5918 | [] -> ()
5919 | l :: rest ->
5920 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5921 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5922 then (
5923 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5925 else loop rest
5927 loop state.layout
5928 | 3 -> ()
5929 | _ -> viewmouse button down x y mask
5932 let uioh = object
5933 method display = ()
5935 method key key mask =
5936 begin match state.mode with
5937 | Textentry textentry -> textentrykeyboard key mask textentry
5938 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5939 | View -> viewkeyboard key mask
5940 | LinkNav linknav -> linknavkeyboard key mask linknav
5941 end;
5942 state.uioh
5944 method button button bstate x y mask =
5945 begin match state.mode with
5946 | LinkNav _
5947 | View -> viewmouse button bstate x y mask
5948 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5949 | Textentry _ -> ()
5950 end;
5951 state.uioh
5953 method multiclick clicks x y mask =
5954 begin match state.mode with
5955 | LinkNav _
5956 | View -> viewmulticlick clicks x y mask
5957 | Birdseye _
5958 | Textentry _ -> ()
5959 end;
5960 state.uioh
5962 method motion x y =
5963 begin match state.mode with
5964 | Textentry _ -> ()
5965 | View | Birdseye _ | LinkNav _ ->
5966 match state.mstate with
5967 | Mzoom _ | Mnone -> ()
5969 | Mpan (x0, y0) ->
5970 let dx = x - x0
5971 and dy = y0 - y in
5972 state.mstate <- Mpan (x, y);
5973 if canpan ()
5974 then state.x <- panbound (state.x + dx);
5975 let y = clamp dy in
5976 gotoy_and_clear_text y
5978 | Msel (a, _) ->
5979 state.mstate <- Msel (a, (x, y));
5980 G.postRedisplay "motion select";
5982 | Mscrolly ->
5983 let y = min state.winh (max 0 y) in
5984 scrolly y
5986 | Mscrollx ->
5987 let x = min state.winw (max 0 x) in
5988 scrollx x
5990 | Mzoomrect (p0, _) ->
5991 state.mstate <- Mzoomrect (p0, (x, y));
5992 G.postRedisplay "motion zoomrect";
5993 end;
5994 state.uioh
5996 method pmotion x y =
5997 begin match state.mode with
5998 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5999 let rec loop = function
6000 | [] ->
6001 if hooverpageno != -1
6002 then (
6003 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6004 G.postRedisplay "pmotion birdseye no hoover";
6006 | l :: rest ->
6007 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6008 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6009 then (
6010 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6011 G.postRedisplay "pmotion birdseye hoover";
6013 else loop rest
6015 loop state.layout
6017 | Textentry _ -> ()
6019 | LinkNav _
6020 | View ->
6021 match state.mstate with
6022 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ -> ()
6023 | Mnone ->
6024 updateunder x y;
6025 if canselect ()
6026 then
6027 match conf.pax with
6028 | None -> ()
6029 | Some r ->
6030 let past, _, _ = !r in
6031 let now = now () in
6032 let delta = now -. past in
6033 if delta > 0.01
6034 then paxunder x y
6035 else r := (now, x, y)
6036 end;
6037 state.uioh
6039 method infochanged _ = ()
6041 method scrollph =
6042 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6043 let p, h =
6044 if maxy = 0
6045 then 0.0, float state.winh
6046 else scrollph state.y maxy
6048 vscrollw (), p, h
6050 method scrollpw =
6051 let winw = wadjsb () + state.winw in
6052 let fwinw = float winw in
6053 let sw =
6054 let sw = fwinw /. float state.w in
6055 let sw = fwinw *. sw in
6056 max sw (float conf.scrollh)
6058 let position =
6059 let maxx = state.w + winw in
6060 let x = winw - state.x in
6061 let percent = float x /. float maxx in
6062 (fwinw -. sw) *. percent
6064 hscrollh (), position, sw
6066 method modehash =
6067 let modename =
6068 match state.mode with
6069 | LinkNav _ -> "links"
6070 | Textentry _ -> "textentry"
6071 | Birdseye _ -> "birdseye"
6072 | View -> "view"
6074 findkeyhash conf modename
6076 method eformsgs = true
6077 method alwaysscrolly = false
6078 end;;
6080 let adderrmsg src msg =
6081 Buffer.add_string state.errmsgs msg;
6082 state.newerrmsgs <- true;
6083 G.postRedisplay src
6086 let adderrfmt src fmt =
6087 Format.ksprintf (fun s -> adderrmsg src s) fmt;
6090 let addrect pageno r g b a x0 y0 x1 y1 =
6091 Hashtbl.add state.prects pageno [|r; g; b; a; x0; y0; x1; y1|];
6094 let ract cmds =
6095 let cl = splitatspace cmds in
6096 let scan s fmt f =
6097 try Scanf.sscanf s fmt f
6098 with exn ->
6099 adderrfmt "remote exec"
6100 "error processing '%S': %s\n" cmds @@ exntos exn
6102 let rectx s pageno (r, g, b, a) x0 y0 x1 y1 =
6103 vlog "%s page %d color (%f %f %f %f) x0,y0,x1,y1 = %f %f %f %f"
6104 s pageno r g b a x0 y0 x1 y1;
6105 onpagerect
6106 pageno
6107 (fun w h ->
6108 let _,w1,h1,_ = getpagedim pageno in
6109 let sw = float w1 /. float w
6110 and sh = float h1 /. float h in
6111 let x0s = x0 *. sw
6112 and x1s = x1 *. sw
6113 and y0s = y0 *. sh
6114 and y1s = y1 *. sh in
6115 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
6116 let color = (r, g, b, a) in
6117 if conf.verbose then debugrect rect;
6118 state.rects <- (pageno, color, rect) :: state.rects;
6119 G.postRedisplay s;
6122 match cl with
6123 | "reload" :: [] -> reload ()
6124 | "goto" :: args :: [] ->
6125 scan args "%u %f %f"
6126 (fun pageno x y ->
6127 let cmd, _ = state.geomcmds in
6128 if emptystr cmd
6129 then gotopagexy pageno x y
6130 else
6131 let f prevf () =
6132 gotopagexy pageno x y;
6133 prevf ()
6135 state.reprf <- f state.reprf
6137 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
6138 | "gotor" :: args :: [] ->
6139 scan args "%S %u"
6140 (fun filename pageno -> gotounder (Uremote (filename, pageno)))
6141 | "gotord" :: args :: [] ->
6142 scan args "%S %S"
6143 (fun filename dest -> gotounder (Uremotedest (filename, dest)))
6144 | "rect" :: args :: [] ->
6145 scan args "%u %u %f %f %f %f"
6146 (fun pageno c x0 y0 x1 y1 ->
6147 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
6148 rectx "rect" pageno color x0 y0 x1 y1;
6150 | "prect" :: args :: [] ->
6151 scan args "%u %f %f %f %f %f %f %f %f"
6152 (fun pageno r g b alpha x0 y0 x1 y1 ->
6153 addrect pageno r g b alpha x0 y0 x1 y1;
6154 G.postRedisplay "prect"
6156 | "pgoto" :: args :: [] ->
6157 scan args "%u %f %f"
6158 (fun pageno x y ->
6159 let optopaque =
6160 match getopaque pageno with
6161 | Some opaque -> opaque
6162 | None -> ~< ""
6164 pgoto optopaque pageno x y;
6165 let rec fixx = function
6166 | [] -> ()
6167 | l :: rest ->
6168 if l.pageno = pageno
6169 then (
6170 state.x <- state.x - l.pagedispx;
6171 gotoy state.y;
6173 else fixx rest
6175 let layout =
6176 let sx = state.x in
6177 let ww = state.winw in
6178 let mult =
6179 match conf.columns with
6180 | Csingle _ | Csplit _ -> 1
6181 | Cmulti ((n, _, _), _) -> n
6183 state.winw <- state.winw * mult;
6184 state.x <- 0;
6185 let res = layout state.y state.winh in
6186 state.winw <- ww;
6187 state.x <- sx;
6190 fixx layout
6192 | "activatewin" :: [] -> Wsi.activatewin ()
6193 | "quit" :: [] -> raise Quit
6194 | "clearrects" :: [] ->
6195 Hashtbl.clear state.prects;
6196 G.postRedisplay "clearrects"
6197 | _ ->
6198 adderrfmt "remote command"
6199 "error processing remote command: %S\n" cmds;
6202 let remote =
6203 let scratch = Bytes.create 80 in
6204 let buf = Buffer.create 80 in
6205 fun fd ->
6206 match tempfailureretry (Unix.read fd scratch 0) 80 with
6207 | (exception Unix.Unix_error (Unix.EAGAIN, _, _)) -> None
6208 | 0 ->
6209 Unix.close fd;
6210 if Buffer.length buf > 0
6211 then (
6212 let s = Buffer.contents buf in
6213 Buffer.clear buf;
6214 ract s;
6216 None
6217 | n ->
6218 let rec eat ppos =
6219 let nlpos =
6220 match Bytes.index_from scratch ppos '\n' with
6221 | pos -> if pos >= n then -1 else pos
6222 | (exception Not_found) -> -1
6224 if nlpos >= 0
6225 then (
6226 Buffer.add_subbytes buf scratch ppos (nlpos-ppos);
6227 let s = Buffer.contents buf in
6228 Buffer.clear buf;
6229 ract s;
6230 eat (nlpos+1);
6232 else (
6233 Buffer.add_subbytes buf scratch ppos (n-ppos);
6234 Some fd
6236 in eat 0
6239 let remoteopen path =
6240 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
6241 with exn ->
6242 adderrfmt "remoteopen" "error opening %S: %s" path @@ exntos exn;
6243 None
6246 let () =
6247 let gcconfig = ref E.s in
6248 let trimcachepath = ref E.s in
6249 let rcmdpath = ref E.s in
6250 let pageno = ref None in
6251 let rootwid = ref 0 in
6252 let openlast = ref false in
6253 let nofc = ref false in
6254 let doreap = ref false in
6255 selfexec := Sys.executable_name;
6256 Arg.parse
6257 (Arg.align
6258 [("-p", Arg.String (fun s -> state.password <- s),
6259 "<password> Set password");
6261 ("-f", Arg.String
6262 (fun s ->
6263 Config.fontpath := s;
6264 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
6266 "<path> Set path to the user interface font");
6268 ("-c", Arg.String
6269 (fun s ->
6270 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
6271 Config.confpath := s),
6272 "<path> Set path to the configuration file");
6274 ("-last", Arg.Set openlast, " Open last document");
6276 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
6277 "<page-number> Jump to page");
6279 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6280 "<path> Set path to the trim cache file");
6282 ("-dest", Arg.String (fun s -> state.nameddest <- s),
6283 "<named-destination> Set named destination");
6285 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
6286 ("-cxack", Arg.Set cxack, " Cut corners");
6288 ("-remote", Arg.String (fun s -> rcmdpath := s),
6289 "<path> Set path to the remote commands source");
6291 ("-origin", Arg.String (fun s -> state.origin <- s),
6292 "<original-path> Set original path");
6294 ("-gc", Arg.Set_string gcconfig,
6295 "<script-path> Collect garbage with the help of a script");
6297 ("-nofc", Arg.Set nofc, " Do not use fontconfig");
6299 ("-v", Arg.Unit (fun () ->
6300 Printf.printf
6301 "%s\nconfiguration path: %s\n"
6302 (version ())
6303 Config.defconfpath
6305 exit 0), " Print version and exit");
6307 ("-embed", Arg.Set_int rootwid,
6308 "<window-id> Embed into window")
6311 (fun s -> state.path <- s)
6312 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6314 if !wtmode
6315 then selfexec := !selfexec ^ " -wtmode";
6317 let histmode = emptystr state.path && not !openlast in
6319 if not (Config.load !openlast)
6320 then dolog "failed to load configuration";
6321 begin match !pageno with
6322 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
6323 | None -> ()
6324 end;
6326 if nonemptystr !gcconfig
6327 then (
6328 let (c, s) =
6329 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6330 | (exception exn) -> error "socketpair for gc failed: %s" @@ exntos exn
6331 | fds -> fds
6333 match spawn !gcconfig [(c, 0); (c, 1); (s, -1)] with
6334 | (exception exn) -> error "failed to execute gc script: %s" @@ exntos exn
6335 | _pid ->
6336 Ne.clo c @@ (fun s -> error "failed to close gc fd %s" s);
6337 Config.gc s;
6338 exit 0
6341 let wsfd, winw, winh = Wsi.init (object (self)
6342 val mutable m_clicks = 0
6343 val mutable m_click_x = 0
6344 val mutable m_click_y = 0
6345 val mutable m_lastclicktime = infinity
6347 method private cleanup =
6348 state.roam <- noroam;
6349 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap
6350 method expose = G.postRedisplay"expose"
6351 method visible v =
6352 let name =
6353 match v with
6354 | Wsi.Unobscured -> "unobscured"
6355 | Wsi.PartiallyObscured -> "partiallyobscured"
6356 | Wsi.FullyObscured -> "fullyobscured"
6358 vlog "visibility change %s" name
6359 method display = display ()
6360 method map mapped = vlog "mappped %b" mapped
6361 method reshape w h =
6362 self#cleanup;
6363 reshape w h
6364 method mouse b d x y m =
6365 if d && canselect ()
6366 then (
6367 (* http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx *)
6368 m_click_x <- x;
6369 m_click_y <- y;
6370 if b = 1
6371 then (
6372 let t = now () in
6373 if abs x - m_click_x > 10
6374 || abs y - m_click_y > 10
6375 || abs_float (t -. m_lastclicktime) > 0.3
6376 then m_clicks <- 0;
6377 m_clicks <- m_clicks + 1;
6378 m_lastclicktime <- t;
6379 if m_clicks = 1
6380 then (
6381 self#cleanup;
6382 G.postRedisplay "cleanup";
6383 state.uioh <- state.uioh#button b d x y m;
6385 else state.uioh <- state.uioh#multiclick m_clicks x y m
6387 else (
6388 self#cleanup;
6389 m_clicks <- 0;
6390 m_lastclicktime <- infinity;
6391 state.uioh <- state.uioh#button b d x y m
6394 else (
6395 state.uioh <- state.uioh#button b d x y m
6397 method motion x y =
6398 state.mpos <- (x, y);
6399 state.uioh <- state.uioh#motion x y
6400 method pmotion x y =
6401 state.mpos <- (x, y);
6402 state.uioh <- state.uioh#pmotion x y
6403 method key k m =
6404 let mascm = m land (
6405 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6406 ) in
6407 let keyboard k m =
6408 let x = state.x and y = state.y in
6409 keyboard k m;
6410 if x != state.x || y != state.y then self#cleanup
6412 match state.keystate with
6413 | KSnone ->
6414 let km = k, mascm in
6415 begin
6416 match
6417 let modehash = state.uioh#modehash in
6418 try Hashtbl.find modehash km
6419 with Not_found ->
6420 try Hashtbl.find (findkeyhash conf "global") km
6421 with Not_found -> KMinsrt (k, m)
6422 with
6423 | KMinsrt (k, m) -> keyboard k m
6424 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6425 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6427 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6428 List.iter (fun (k, m) -> keyboard k m) insrt;
6429 state.keystate <- KSnone
6430 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6431 state.keystate <- KSinto (keys, insrt)
6432 | KSinto _ -> state.keystate <- KSnone
6434 method enter x y =
6435 state.mpos <- (x, y);
6436 state.uioh <- state.uioh#pmotion x y
6437 method leave = state.mpos <- (-1, -1)
6438 method winstate wsl = state.winstate <- wsl
6439 method quit = raise Quit
6440 end) !rootwid conf.cwinw conf.cwinh platform in
6442 state.wsfd <- wsfd;
6444 if not (
6445 List.exists GlMisc.check_extension
6446 [ "GL_ARB_texture_rectangle"
6447 ; "GL_EXT_texture_recangle"
6448 ; "GL_NV_texture_rectangle" ]
6450 then (dolog "OpenGL does not suppport rectangular textures"; exit 1);
6452 if (
6453 let r = GlMisc.get_string `renderer in
6454 let p = "Mesa DRI Intel(" in
6455 let l = String.length p in
6456 String.length r > l && String.sub r 0 l = p
6458 then (
6459 defconf.sliceheight <- 1024;
6460 defconf.texcount <- 32;
6461 defconf.usepbo <- true;
6464 let cs, ss =
6465 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6466 | (exception exn) ->
6467 dolog "socketpair failed: %s" @@ exntos exn;
6468 exit 1
6469 | (r, w) ->
6470 cloexec r;
6471 cloexec w;
6472 r, w
6475 setcheckers conf.checkers;
6477 init cs (
6478 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
6479 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6480 !Config.fontpath, !trimcachepath,
6481 GlMisc.check_extension "GL_ARB_pixel_buffer_object",
6482 not !nofc
6484 List.iter GlArray.enable [`texture_coord; `vertex];
6485 state.ss <- ss;
6486 reshape ~firsttime:true winw winh;
6487 state.uioh <- uioh;
6488 if histmode
6489 then (
6490 Wsi.settitle "llpp (history)";
6491 enterhistmode ();
6493 else (
6494 state.text <- "Opening " ^ (mbtoutf8 state.path);
6495 opendoc state.path state.password;
6497 display ();
6498 Wsi.mapwin ();
6499 Wsi.setcursor Wsi.CURSOR_INHERIT;
6500 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
6502 let rec reap () =
6503 match Unix.waitpid [Unix.WNOHANG] ~-1 with
6504 | (exception (Unix.Unix_error (Unix.ECHILD, _, _))) -> ()
6505 | (exception exn) -> dolog "Unix.waitpid: %s" @@ exntos exn
6506 | 0, _ -> ()
6507 | _pid, _status -> reap ()
6509 Sys.set_signal Sys.sigchld (Sys.Signal_handle (fun _ -> doreap := true));
6511 let optrfd =
6512 ref (
6513 if nonemptystr !rcmdpath
6514 then remoteopen !rcmdpath
6515 else None
6519 let rec loop deadline =
6520 if !doreap
6521 then (
6522 doreap := false;
6523 reap ()
6525 let r = [state.ss; state.wsfd] in
6526 let r =
6527 match !optrfd with
6528 | None -> r
6529 | Some fd -> fd :: r
6531 if state.redisplay
6532 then (
6533 state.redisplay <- false;
6534 display ();
6536 let timeout =
6537 let now = now () in
6538 if deadline > now
6539 then (
6540 if deadline = infinity
6541 then ~-.1.0
6542 else max 0.0 (deadline -. now)
6544 else 0.0
6546 let r, _, _ =
6547 try Unix.select r [] [] timeout
6548 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6550 begin match r with
6551 | [] ->
6552 state.ghyll None;
6553 let newdeadline =
6554 if state.ghyll == noghyll
6555 then
6556 match state.autoscroll with
6557 | Some step when step != 0 ->
6558 let y = state.y + step in
6559 let y =
6560 if y < 0
6561 then state.maxy
6562 else if y >= state.maxy then 0 else y
6564 if state.mode = View
6565 then gotoy_and_clear_text y
6566 else gotoy y;
6567 deadline +. 0.01
6568 | _ -> infinity
6569 else deadline +. 0.01
6571 loop newdeadline
6573 | l ->
6574 let rec checkfds = function
6575 | [] -> ()
6576 | fd :: rest when fd = state.ss ->
6577 let cmd = readcmd state.ss in
6578 act cmd;
6579 checkfds rest
6581 | fd :: rest when fd = state.wsfd ->
6582 Wsi.readresp fd;
6583 checkfds rest
6585 | fd :: rest when Some fd = !optrfd ->
6586 begin match remote fd with
6587 | None -> optrfd := remoteopen !rcmdpath;
6588 | opt -> optrfd := opt
6589 end;
6590 checkfds rest
6592 | _ :: rest ->
6593 dolog "select returned unknown descriptor";
6594 checkfds rest
6596 checkfds l;
6597 let newdeadline =
6598 let deadline1 =
6599 if deadline = infinity
6600 then now () +. 0.01
6601 else deadline
6603 match state.autoscroll with
6604 | Some step when step != 0 -> deadline1
6605 | _ -> if state.ghyll == noghyll then infinity else deadline1
6607 loop newdeadline
6608 end;
6611 loop infinity;
6612 with Quit ->
6613 Config.save leavebirdseye;
6614 if hasunsavedchanges ()
6615 then save ();