Add missing space
[llpp.git] / main.ml
blobf797742a62db8a4b7d9c6c512be682ee826405d0
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 getmaxw : unit -> float = "ml_getmaxw";;
16 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
17 external measurestr : int -> string -> float = "ml_measure_string";;
18 external postprocess :
19 opaque -> int -> int -> int -> (int * string * int) -> int
20 = "ml_postprocess";;
21 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
22 external setaalevel : int -> unit = "ml_setaalevel";;
23 external realloctexts : int -> bool = "ml_realloctexts";;
24 external findlink : opaque -> linkdir -> link = "ml_findlink";;
25 external getlink : opaque -> int -> under = "ml_getlink";;
26 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
27 external getlinkcount : opaque -> int = "ml_getlinkcount";;
28 external findpwl : int -> int -> pagewithlinks = "ml_find_page_with_links";;
29 external getpbo : width -> height -> colorspace -> opaque = "ml_getpbo";;
30 external freepbo : opaque -> unit = "ml_freepbo";;
31 external unmappbo : opaque -> unit = "ml_unmappbo";;
32 external pbousable : unit -> bool = "ml_pbo_usable";;
33 external unproject : opaque -> int -> int -> (int * int) option
34 = "ml_unproject";;
35 external project : opaque -> int -> int -> float -> float -> (float * float)
36 = "ml_project";;
37 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
38 external rectofblock : opaque -> int -> int -> float array option
39 = "ml_rectofblock";;
40 external begintiles : unit -> unit = "ml_begintiles";;
41 external endtiles : unit -> unit = "ml_endtiles";;
42 external addannot : opaque -> int -> int -> string -> unit = "ml_addannot";;
43 external modannot : opaque -> slinkindex -> string -> unit = "ml_modannot";;
44 external delannot : opaque -> slinkindex -> unit = "ml_delannot";;
45 external hasunsavedchanges : unit -> bool = "ml_hasunsavedchanges";;
46 external savedoc : string -> unit = "ml_savedoc";;
47 external getannotcontents : opaque -> slinkindex -> string
48 = "ml_getannotcontents";;
49 external drawprect : opaque -> int -> int -> float array -> unit =
50 "ml_drawprect";;
52 let selfexec = ref E.s;;
54 let drawstring size x y s =
55 Gl.enable `blend;
56 Gl.enable `texture_2d;
57 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
58 ignore (drawstr size x y s);
59 Gl.disable `blend;
60 Gl.disable `texture_2d;
63 let drawstring1 size x y s =
64 drawstr size x y s;
67 let drawstring2 size x y fmt =
68 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
71 let _debugl l =
72 dolog "l %d dim=%d {" l.pageno l.pagedimno;
73 dolog " WxH %dx%d" l.pagew l.pageh;
74 dolog " vWxH %dx%d" l.pagevw l.pagevh;
75 dolog " pagex,y %d,%d" l.pagex l.pagey;
76 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
77 dolog " column %d" l.pagecol;
78 dolog "}";
81 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
82 dolog "rect {";
83 dolog " x0,y0=(% f, % f)" x0 y0;
84 dolog " x1,y1=(% f, % f)" x1 y1;
85 dolog " x2,y2=(% f, % f)" x2 y2;
86 dolog " x3,y3=(% f, % f)" x3 y3;
87 dolog "}";
90 let isbirdseye = function
91 | Birdseye _ -> true
92 | Textentry _
93 | View
94 | LinkNav _ -> false
97 let istextentry = function
98 | Textentry _ -> true
99 | Birdseye _
100 | View
101 | LinkNav _ -> false
104 let wtmode = ref false;;
105 let cxack = ref false;;
107 let pgscale h = truncate (float h *. conf.pgscale);;
109 let hscrollh () =
110 if not state.uioh#alwaysscrolly && (conf.scrollb land scrollbhv = 0)
111 || (state.x = 0 && state.w <= state.winw - conf.scrollbw)
112 then 0
113 else conf.scrollbw
116 let vscrollw () =
117 if not state.uioh#alwaysscrolly && (conf.scrollb land scrollbvv = 0)
118 then 0
119 else conf.scrollbw
122 let vscrollhit x =
123 if conf.leftscroll
124 then x < vscrollw ()
125 else x > state.winw - vscrollw ()
128 let wadjsb () = -vscrollw ();;
129 let xadjsb () = if conf.leftscroll then vscrollw () else 0;;
131 let setfontsize n =
132 fstate.fontsize <- n;
133 fstate.wwidth <- measurestr fstate.fontsize "w";
134 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
137 let vlog fmt =
138 if conf.verbose
139 then dolog fmt
140 else Printf.kprintf ignore fmt
143 let launchpath () =
144 if emptystr conf.pathlauncher
145 then dolog "%s" state.path
146 else (
147 let command = Str.global_replace percentsre state.path conf.pathlauncher in
148 match spawn command [] with
149 | _pid -> ()
150 | (exception exn) ->
151 dolog "failed to execute `%s': %s" command @@ exntos exn
155 module G =
156 struct
157 let postRedisplay who =
158 vlog "redisplay for [%S]" who;
159 state.redisplay <- true;
161 end;;
163 let getopaque pageno =
164 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
165 with Not_found -> None
168 let pagetranslatepoint l x y =
169 let dy = y - l.pagedispy in
170 let y = dy + l.pagey in
171 let dx = x - l.pagedispx in
172 let x = dx + l.pagex in
173 (x, y);
176 let onppundermouse g x y d =
177 let rec f = function
178 | l :: rest ->
179 begin match getopaque l.pageno with
180 | Some opaque ->
181 let x0 = l.pagedispx in
182 let x1 = x0 + l.pagevw in
183 let y0 = l.pagedispy in
184 let y1 = y0 + l.pagevh in
185 if y >= y0 && y <= y1 && x >= x0 && x <= x1
186 then
187 let px, py = pagetranslatepoint l x y in
188 match g opaque l px py with
189 | Some res -> res
190 | None -> f rest
191 else f rest
192 | _ ->
193 f rest
195 | [] -> d
197 f state.layout
200 let getunder x y =
201 let g opaque l px py =
202 if state.bzoom
203 then (
204 match rectofblock opaque px py with
205 | Some [|x0;x1;y0;y1|] ->
206 let ox = xadjsb () |> float in
207 let rect = (x0+.ox, y0, x1+.ox, y0, x1+.ox, y1, x0+.ox, y1) in
208 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
209 state.rects <- [l.pageno, color, rect];
210 G.postRedisplay "getunder";
211 | _otherwise -> ()
213 let under = whatsunder opaque px py in
214 if under = Unone then None else Some under
216 onppundermouse g x y Unone
219 let unproject x y =
220 let g opaque l x y =
221 match unproject opaque x y with
222 | Some (x, y) -> Some (Some (opaque, l.pageno, x, y))
223 | None -> None
225 onppundermouse g x y None;
228 let showtext c s =
229 state.text <- Printf.sprintf "%c%s" c s;
230 G.postRedisplay "showtext";
233 let impmsg fmt =
234 Format.ksprintf (fun s -> showtext '!' s) fmt;
237 let pipesel opaque cmd =
238 if hassel opaque
239 then
240 match Unix.pipe () with
241 | (exception exn) -> dolog "pipesel cannot create pipe: %S" @@ exntos exn;
242 | (r, w) ->
243 let doclose what fd =
244 Ne.clo fd (fun msg -> dolog "%s close failed: %s" what msg)
246 let pid =
247 try spawn cmd [r, 0; w, -1]
248 with exn ->
249 dolog "cannot execute %S: %s" cmd @@ exntos exn;
252 if pid > 0
253 then (
254 copysel w opaque;
255 G.postRedisplay "pipesel";
257 else doclose "pipesel pipe/w" w;
258 doclose "pipesel pipe/r" r;
261 let paxunder x y =
262 let g opaque l px py =
263 if markunder opaque px py conf.paxmark
264 then (
265 Some (fun () ->
266 match getopaque l.pageno with
267 | None -> ()
268 | Some opaque -> pipesel opaque conf.paxcmd
271 else None
273 G.postRedisplay "paxunder";
274 if conf.paxmark = Mark_page
275 then
276 List.iter (fun l ->
277 match getopaque l.pageno with
278 | None -> ()
279 | Some opaque -> clearmark opaque) state.layout;
280 state.roam <- onppundermouse g x y (fun () -> impmsg "whoopsie daisy");
283 let selstring s =
284 match Unix.pipe () with
285 | (exception exn) -> impmsg "pipe failed: %s" @@ exntos exn
286 | (r, w) ->
287 let clo cap fd =
288 Ne.clo fd (fun msg -> impmsg "failed to close %s: %s" cap msg)
290 let pid =
291 try spawn conf.selcmd [r, 0; w, -1]
292 with exn ->
293 impmsg "failed to execute %s: %s" conf.selcmd @@ exntos exn;
296 if pid > 0
297 then (
299 let l = String.length s in
300 let bytes = Bytes.unsafe_of_string s in
301 let n = tempfailureretry (Unix.write w bytes 0) l in
302 if n != l
303 then impmsg "failed to write %d characters to sel pipe, wrote %d"
305 with exn ->
306 impmsg "failed to write to sel pipe: %s" @@ exntos exn
308 else dolog "%s" s;
309 clo "selstring pipe/r" r;
310 clo "selstring pipe/w" w;
313 let undertext ?(nopath=false) = function
314 | Unone -> "none"
315 | Ulinkuri s -> s
316 | Ulinkgoto (pageno, _) ->
317 if nopath
318 then "page " ^ string_of_int (pageno+1)
319 else Printf.sprintf "%s: page %d" state.path (pageno+1)
320 | Utext s -> "font: " ^ s
321 | Uunexpected s -> "unexpected: " ^ s
322 | Ulaunch s -> "launch: " ^ s
323 | Unamed s -> "named: " ^ s
324 | Uremote (filename, pageno) ->
325 Printf.sprintf "%s: page %d" filename (pageno+1)
326 | Uremotedest (filename, destname) ->
327 Printf.sprintf "%s: destination %S" filename destname
328 | Uannotation (opaque, slinkindex) ->
329 "annotation: " ^ getannotcontents opaque slinkindex
332 let updateunder x y =
333 match getunder x y with
334 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
335 | Ulinkuri uri ->
336 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
337 Wsi.setcursor Wsi.CURSOR_INFO
338 | Ulinkgoto (pageno, _) ->
339 if conf.underinfo
340 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
341 Wsi.setcursor Wsi.CURSOR_INFO
342 | Utext s ->
343 if conf.underinfo then showtext 'f' ("ont: " ^ s);
344 Wsi.setcursor Wsi.CURSOR_TEXT
345 | Uunexpected s ->
346 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
347 Wsi.setcursor Wsi.CURSOR_INHERIT
348 | Ulaunch s ->
349 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
350 Wsi.setcursor Wsi.CURSOR_INHERIT
351 | Unamed s ->
352 if conf.underinfo then showtext 'n' ("amed: " ^ s);
353 Wsi.setcursor Wsi.CURSOR_INHERIT
354 | Uremote (filename, pageno) ->
355 if conf.underinfo then showtext 'r'
356 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
357 Wsi.setcursor Wsi.CURSOR_INFO
358 | Uremotedest (filename, destname) ->
359 if conf.underinfo then showtext 'r'
360 (Printf.sprintf "emote destination: %s (%S)" filename destname);
361 Wsi.setcursor Wsi.CURSOR_INFO
362 | Uannotation _ ->
363 if conf.underinfo then showtext 'a' "nnotation";
364 Wsi.setcursor Wsi.CURSOR_INFO
367 let showlinktype under =
368 if conf.underinfo && under != Unone
369 then showtext ' ' @@ undertext under
372 let intentry_with_suffix text key =
373 let c =
374 if key >= 32 && key < 127
375 then Char.chr key
376 else '\000'
378 match Char.lowercase c with
379 | '0' .. '9' ->
380 let text = addchar text c in
381 TEcont text
383 | 'k' | 'm' | 'g' ->
384 let text = addchar text c in
385 TEcont text
387 | _ ->
388 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
389 TEcont text
392 let readcmd fd =
393 let s = Bytes.create 4 in
394 let n = tempfailureretry (Unix.read fd s 0) 4 in
395 if n != 4 then error "incomplete read(len) = %d" n;
396 let len = (Char.code (Bytes.get s 0) lsl 24)
397 lor (Char.code (Bytes.get s 1) lsl 16)
398 lor (Char.code (Bytes.get s 2) lsl 8)
399 lor (Char.code (Bytes.get s 3))
401 let s = Bytes.create len in
402 let n = tempfailureretry (Unix.read fd s 0) len in
403 if n != len then error "incomplete read(data) %d vs %d" n len;
404 Bytes.to_string s
407 let wcmd fmt =
408 let b = Buffer.create 16 in
409 Buffer.add_string b "llll";
410 Printf.kbprintf
411 (fun b ->
412 let s = Buffer.to_bytes b in
413 let n = Bytes.length s in
414 let len = n - 4 in
415 (* dolog "wcmd %S" (String.sub s 4 len); *)
416 Bytes.set s 0 (Char.chr ((len lsr 24) land 0xff));
417 Bytes.set s 1 (Char.chr ((len lsr 16) land 0xff));
418 Bytes.set s 2 (Char.chr ((len lsr 8) land 0xff));
419 Bytes.set s 3 (Char.chr (len land 0xff));
420 let n' = tempfailureretry (Unix.write state.ss s 0) n in
421 if n' != n then error "write failed %d vs %d" n' n;
422 ) b fmt;
425 let nogeomcmds cmds =
426 match cmds with
427 | s, [] -> emptystr s
428 | _ -> false
431 let layoutN ((columns, coverA, coverB), b) x y sw sh =
432 let sh = sh - (hscrollh ()) in
433 let wadj = wadjsb () in
434 let rec fold accu n =
435 if n = Array.length b
436 then accu
437 else
438 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
439 if (vy - y) > sh &&
440 (n = coverA - 1
441 || n = state.pagecount - coverB
442 || (n - coverA) mod columns = columns - 1)
443 then accu
444 else
445 let accu =
446 if vy + h > y
447 then
448 let pagey = max 0 (y - vy) in
449 let pagedispy = if pagey > 0 then 0 else vy - y in
450 let pagedispx, pagex =
451 let pdx =
452 if n = coverA - 1 || n = state.pagecount - coverB
453 then x + (wadj + sw - w) / 2
454 else dx + xoff + x
456 if pdx < 0
457 then 0, -pdx
458 else pdx, 0
460 let pagevw =
461 let vw = wadj + sw - pagedispx in
462 let pw = w - pagex in
463 min vw pw
465 let pagevh = min (h - pagey) (sh - pagedispy) in
466 if pagevw > 0 && pagevh > 0
467 then
468 let e =
469 { pageno = n
470 ; pagedimno = pdimno
471 ; pagew = w
472 ; pageh = h
473 ; pagex = pagex
474 ; pagey = pagey
475 ; pagevw = pagevw
476 ; pagevh = pagevh
477 ; pagedispx = pagedispx
478 ; pagedispy = pagedispy
479 ; pagecol = 0
482 e :: accu
483 else
484 accu
485 else
486 accu
488 fold accu (n+1)
490 if Array.length b = 0
491 then []
492 else List.rev (fold [] (page_of_y y))
495 let layoutS (columns, b) x y sw sh =
496 let sh = sh - hscrollh () in
497 let wadj = wadjsb () in
498 let rec fold accu n =
499 if n = Array.length b
500 then accu
501 else
502 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
503 if (vy - y) > sh
504 then accu
505 else
506 let accu =
507 if vy + pageh > y
508 then
509 let x = xoff + x in
510 let pagey = max 0 (y - vy) in
511 let pagedispy = if pagey > 0 then 0 else vy - y in
512 let pagedispx, pagex =
513 if px = 0
514 then (
515 if x < 0
516 then 0, -x
517 else x, 0
519 else (
520 let px = px - x in
521 if px < 0
522 then -px, 0
523 else 0, px
526 let pagecolw = pagew/columns in
527 let pagedispx =
528 if pagecolw < sw
529 then pagedispx + ((wadj + sw - pagecolw) / 2)
530 else pagedispx
532 let pagevw =
533 let vw = wadj + sw - pagedispx in
534 let pw = pagew - pagex in
535 min vw pw
537 let pagevw = min pagevw pagecolw in
538 let pagevh = min (pageh - pagey) (sh - pagedispy) in
539 if pagevw > 0 && pagevh > 0
540 then
541 let e =
542 { pageno = n/columns
543 ; pagedimno = pdimno
544 ; pagew = pagew
545 ; pageh = pageh
546 ; pagex = pagex
547 ; pagey = pagey
548 ; pagevw = pagevw
549 ; pagevh = pagevh
550 ; pagedispx = pagedispx
551 ; pagedispy = pagedispy
552 ; pagecol = n mod columns
555 e :: accu
556 else
557 accu
558 else
559 accu
561 fold accu (n+1)
563 List.rev (fold [] 0)
566 let layout x y sw sh =
567 if nogeomcmds state.geomcmds
568 then
569 match conf.columns with
570 | Csingle b -> layoutN ((1, 0, 0), b) x y sw sh
571 | Cmulti c -> layoutN c x y sw sh
572 | Csplit s -> layoutS s x y sw sh
573 else []
576 let clamp incr =
577 let y = state.y + incr in
578 let y = max 0 y in
579 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
583 let itertiles l f =
584 let tilex = l.pagex mod conf.tilew in
585 let tiley = l.pagey mod conf.tileh in
587 let col = l.pagex / conf.tilew in
588 let row = l.pagey / conf.tileh in
590 let xadj = xadjsb () in
591 let rec rowloop row y0 dispy h =
592 if h = 0
593 then ()
594 else (
595 let dh = conf.tileh - y0 in
596 let dh = min h dh in
597 let rec colloop col x0 dispx w =
598 if w = 0
599 then ()
600 else (
601 let dw = conf.tilew - x0 in
602 let dw = min w dw in
603 let dispx' = xadj + dispx in
604 f col row dispx' dispy x0 y0 dw dh;
605 colloop (col+1) 0 (dispx+dw) (w-dw)
608 colloop col tilex l.pagedispx l.pagevw;
609 rowloop (row+1) 0 (dispy+dh) (h-dh)
612 if l.pagevw > 0 && l.pagevh > 0
613 then rowloop row tiley l.pagedispy l.pagevh;
616 let gettileopaque l col row =
617 let key =
618 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
620 try Some (Hashtbl.find state.tilemap key)
621 with Not_found -> None
624 let puttileopaque l col row gen colorspace angle opaque size elapsed =
625 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
626 Hashtbl.add state.tilemap key (opaque, size, elapsed)
629 let filledrect x0 y0 x1 y1 =
630 GlArray.disable `texture_coord;
631 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
632 GlArray.vertex `two state.vraw;
633 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
634 GlArray.enable `texture_coord;
637 let linerect x0 y0 x1 y1 =
638 GlArray.disable `texture_coord;
639 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y1; x1; y0 |];
640 GlArray.vertex `two state.vraw;
641 GlArray.draw_arrays `line_loop ~first:0 ~count:4;
642 GlArray.enable `texture_coord;
645 let drawtiles l color =
646 GlDraw.color color;
647 let wadj = wadjsb () in
648 begintiles ();
649 let f col row x y tilex tiley w h =
650 match gettileopaque l col row with
651 | Some (opaque, _, t) ->
652 let params = x, y, w, h, tilex, tiley in
653 if conf.invert
654 then GlTex.env (`mode `blend);
655 drawtile params opaque;
656 if conf.invert
657 then GlTex.env (`mode `modulate);
658 if conf.debug
659 then (
660 endtiles ();
661 let s = Printf.sprintf
662 "%d[%d,%d] %f sec"
663 l.pageno col row t
665 let w = measurestr fstate.fontsize s in
666 GlDraw.color (0.0, 0.0, 0.0);
667 filledrect (float (x-2))
668 (float (y-2))
669 (float (x+2) +. w)
670 (float (y + fstate.fontsize + 2));
671 GlDraw.color (1.0, 1.0, 1.0);
672 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
673 begintiles ();
676 | None ->
677 endtiles ();
678 let w =
679 if conf.leftscroll
680 then w
681 else
682 let lw = wadj + state.winw - x in
683 min lw w
684 and h =
685 let lh = state.winh - y in
686 min lh h
688 if conf.invert
689 then GlTex.env (`mode `blend);
690 begin match state.checkerstexid with
691 | Some id ->
692 Gl.enable `texture_2d;
693 GlTex.bind_texture ~target:`texture_2d id;
694 let x0 = float x
695 and y0 = float y
696 and x1 = float (x+w)
697 and y1 = float (y+h) in
699 let tw = float w /. 16.0
700 and th = float h /. 16.0 in
701 let tx0 = float tilex /. 16.0
702 and ty0 = float tiley /. 16.0 in
703 let tx1 = tx0 +. tw
704 and ty1 = ty0 +. th in
705 Raw.sets_float state.vraw ~pos:0
706 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
707 Raw.sets_float state.traw ~pos:0
708 [| tx0; ty0; tx0; ty1; tx1; ty0; tx1; ty1 |];
709 GlArray.vertex `two state.vraw;
710 GlArray.tex_coord `two state.traw;
711 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
712 Gl.disable `texture_2d;
714 | None ->
715 GlDraw.color (1.0, 1.0, 1.0);
716 filledrect (float x) (float y) (float (x+w)) (float (y+h));
717 end;
718 if conf.invert
719 then GlTex.env (`mode `modulate);
720 if w > 128 && h > fstate.fontsize + 10
721 then (
722 let c = if conf.invert then 1.0 else 0.0 in
723 GlDraw.color (c, c, c);
724 let c, r =
725 if conf.verbose
726 then (col*conf.tilew, row*conf.tileh)
727 else col, row
729 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
731 GlDraw.color color;
732 begintiles ();
734 itertiles l f;
735 endtiles ();
738 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
740 let tilevisible1 l x y =
741 let ax0 = l.pagex
742 and ax1 = l.pagex + l.pagevw
743 and ay0 = l.pagey
744 and ay1 = l.pagey + l.pagevh in
746 let bx0 = x
747 and by0 = y in
748 let bx1 = min (bx0 + conf.tilew) l.pagew
749 and by1 = min (by0 + conf.tileh) l.pageh in
751 let rx0 = max ax0 bx0
752 and ry0 = max ay0 by0
753 and rx1 = min ax1 bx1
754 and ry1 = min ay1 by1 in
756 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
757 nonemptyintersection
760 let tilevisible layout n x y =
761 let rec findpageinlayout m = function
762 | l :: rest when l.pageno = n ->
763 tilevisible1 l x y || (
764 match conf.columns with
765 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
766 | Csplit _
767 | Csingle _
768 | Cmulti _ -> false
770 | _ :: rest -> findpageinlayout 0 rest
771 | [] -> false
773 findpageinlayout 0 layout;
776 let tileready l x y =
777 tilevisible1 l x y &&
778 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
781 let tilepage n p layout =
782 let rec loop = function
783 | l :: rest ->
784 if l.pageno = n
785 then
786 let f col row _ _ _ _ _ _ =
787 if state.currently = Idle
788 then
789 match gettileopaque l col row with
790 | Some _ -> ()
791 | None ->
792 let x = col*conf.tilew
793 and y = row*conf.tileh in
794 let w =
795 let w = l.pagew - x in
796 min w conf.tilew
798 let h =
799 let h = l.pageh - y in
800 min h conf.tileh
802 let pbo =
803 if conf.usepbo
804 then getpbo w h conf.colorspace
805 else ~< "0"
807 wcmd "tile %s %d %d %d %d %s"
808 (~> p) x y w h (~> pbo);
809 state.currently <-
810 Tiling (
811 l, p, conf.colorspace, conf.angle,
812 state.gen, col, row, conf.tilew, conf.tileh
815 itertiles l f;
816 else
817 loop rest
819 | [] -> ()
821 if nogeomcmds state.geomcmds
822 then loop layout;
825 let preloadlayout x y sw sh =
826 let y = if y < sh then 0 else y - sh in
827 let h = sh*3 in
828 layout x y sw h;
831 let load pages =
832 let rec loop pages =
833 if state.currently != Idle
834 then ()
835 else
836 match pages with
837 | l :: rest ->
838 begin match getopaque l.pageno with
839 | None ->
840 wcmd "page %d %d" l.pageno l.pagedimno;
841 state.currently <- Loading (l, state.gen);
842 | Some opaque ->
843 tilepage l.pageno opaque pages;
844 loop rest
845 end;
846 | _ -> ()
848 if nogeomcmds state.geomcmds
849 then loop pages
852 let preload pages =
853 load pages;
854 if conf.preload && state.currently = Idle
855 then load (preloadlayout state.x state.y state.winw state.winh);
858 let layoutready layout =
859 let rec fold all ls =
860 all && match ls with
861 | l :: rest ->
862 let seen = ref false in
863 let allvisible = ref true in
864 let foo col row _ _ _ _ _ _ =
865 seen := true;
866 allvisible := !allvisible &&
867 begin match gettileopaque l col row with
868 | Some _ -> true
869 | None -> false
872 itertiles l foo;
873 fold (!seen && !allvisible) rest
874 | [] -> true
876 let alltilesvisible = fold true layout in
877 alltilesvisible;
880 let gotoy y =
881 let y = bound y 0 state.maxy in
882 let y, layout, proceed =
883 match conf.maxwait with
884 | Some time when state.ghyll == noghyll ->
885 begin match state.throttle with
886 | None ->
887 let layout = layout state.x y state.winw state.winh in
888 let ready = layoutready layout in
889 if not ready
890 then (
891 load layout;
892 state.throttle <- Some (layout, y, now ());
894 else G.postRedisplay "gotoy showall (None)";
895 y, layout, ready
896 | Some (_, _, started) ->
897 let dt = now () -. started in
898 if dt > time
899 then (
900 state.throttle <- None;
901 let layout = layout state.x y state.winw state.winh in
902 load layout;
903 G.postRedisplay "maxwait";
904 y, layout, true
906 else -1, [], false
909 | _ ->
910 let layout = layout state.x y state.winw state.winh in
911 if not !wtmode || layoutready layout
912 then G.postRedisplay "gotoy ready";
913 y, layout, true
915 if proceed
916 then (
917 state.y <- y;
918 state.layout <- layout;
919 begin match state.mode with
920 | LinkNav ln ->
921 begin match ln with
922 | Ltexact (pageno, linkno) ->
923 let rec loop = function
924 | [] ->
925 state.mode <- LinkNav (Ltgendir 0)
926 | l :: _ when l.pageno = pageno ->
927 begin match getopaque pageno with
928 | None -> state.mode <- LinkNav (Ltnotready (pageno, 0))
929 | Some opaque ->
930 let x0, y0, x1, y1 = getlinkrect opaque linkno in
931 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
932 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
933 then state.mode <- LinkNav (Ltgendir 0)
935 | _ :: rest -> loop rest
937 loop layout
938 | Ltnotready _ | Ltgendir _ -> ()
940 | Birdseye _
941 | Textentry _
942 | View -> ()
943 end;
944 begin match state.mode with
945 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
946 if not (pagevisible layout pageno)
947 then (
948 match state.layout with
949 | [] -> ()
950 | l :: _ ->
951 state.mode <- Birdseye (
952 conf, leftx, l.pageno, hooverpageno, anchor
955 | LinkNav lt ->
956 begin match lt with
957 | Ltnotready (_, dir)
958 | Ltgendir dir ->
959 let linknav =
960 let rec loop = function
961 | [] -> lt
962 | l :: rest ->
963 match getopaque l.pageno with
964 | None -> Ltnotready (l.pageno, dir)
965 | Some opaque ->
966 let link =
967 let ld =
968 if dir = 0
969 then LDfirstvisible (l.pagex, l.pagey, dir)
970 else (
971 if dir > 0 then LDfirst else LDlast
974 findlink opaque ld
976 match link with
977 | Lnotfound -> loop rest
978 | Lfound n ->
979 showlinktype (getlink opaque n);
980 Ltexact (l.pageno, n)
982 loop state.layout
984 state.mode <- LinkNav linknav
985 | Ltexact _ -> ()
987 | Textentry _
988 | View -> ()
989 end;
990 preload layout;
992 state.ghyll <- noghyll;
993 if conf.updatecurs
994 then (
995 let mx, my = state.mpos in
996 updateunder mx my;
1000 let conttiling pageno opaque =
1001 tilepage pageno opaque
1002 (if conf.preload
1003 then preloadlayout state.x state.y state.winw state.winh
1004 else state.layout)
1007 let gotoy_and_clear_text y =
1008 if not conf.verbose then state.text <- E.s;
1009 gotoy y;
1012 let getanchory (n, top, dtop) =
1013 let y, h = getpageyh n in
1014 if conf.presentation
1015 then
1016 let ips = calcips h in
1017 y + truncate (top*.float h -. dtop*.float ips) + ips;
1018 else
1019 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1022 let gotoanchor anchor =
1023 gotoy (getanchory anchor);
1026 let addnav () =
1027 cbput state.hists.nav (getanchor ());
1030 let getnav dir =
1031 let anchor = cbgetc state.hists.nav dir in
1032 getanchory anchor;
1035 let gotoghyll1 single y =
1036 let scroll f n a b =
1037 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1038 let snake f a b =
1039 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1040 if f < a
1041 then s (float f /. float a)
1042 else (
1043 if f > b
1044 then 1.0 -. s ((float (f-b) /. float (n-b)))
1045 else 1.0
1048 snake f a b
1049 and summa n a b =
1050 let ins = float a *. 0.5
1051 and outs = float (n-b) *. 0.5 in
1052 let ones = b - a in
1053 ins +. outs +. float ones
1055 let rec set nab y sy =
1056 let (_N, _A, _B), y =
1057 if single
1058 then
1059 let scl = if y > sy then 2 else -2 in
1060 let _N, _, _ = nab in
1061 (_N,0,_N), y+conf.scrollstep*scl
1062 else nab,y in
1063 let sum = summa _N _A _B in
1064 let dy = float (y - sy) in
1065 state.ghyll <- (
1066 let rec gf n y1 o =
1067 if n >= _N
1068 then state.ghyll <- noghyll
1069 else
1070 let go n =
1071 let s = scroll n _N _A _B in
1072 let y1 = y1 +. ((s *. dy) /. sum) in
1073 gotoy_and_clear_text (truncate y1);
1074 state.ghyll <- gf (n+1) y1;
1076 match o with
1077 | None -> go n
1078 | Some y' when single -> set nab y' state.y
1079 | Some y' -> set (_N/2, 1, 1) y' state.y
1081 gf 0 (float state.y)
1084 match conf.ghyllscroll with
1085 | Some nab when not conf.presentation ->
1086 if state.ghyll == noghyll
1087 then set nab y state.y
1088 else state.ghyll (Some y)
1089 | _ ->
1090 gotoy_and_clear_text y
1093 let gotoghyll = gotoghyll1 false;;
1095 let gotopage n top =
1096 let y, h = getpageyh n in
1097 let y = y + (truncate (top *. float h)) in
1098 gotoghyll y
1101 let gotopage1 n top =
1102 let y = getpagey n in
1103 let y = y + top in
1104 gotoghyll y
1107 let invalidate s f =
1108 state.layout <- [];
1109 state.pdims <- [];
1110 state.rects <- [];
1111 state.rects1 <- [];
1112 match state.geomcmds with
1113 | ps, [] when emptystr ps ->
1114 f ();
1115 state.geomcmds <- s, [];
1117 | ps, [] ->
1118 state.geomcmds <- ps, [s, f];
1120 | ps, (s', _) :: rest when s' = s ->
1121 state.geomcmds <- ps, ((s, f) :: rest);
1123 | ps, cmds ->
1124 state.geomcmds <- ps, ((s, f) :: cmds);
1127 let flushpages () =
1128 Hashtbl.iter (fun _ opaque ->
1129 wcmd "freepage %s" (~> opaque);
1130 ) state.pagemap;
1131 Hashtbl.clear state.pagemap;
1134 let flushtiles () =
1135 if not (Queue.is_empty state.tilelru)
1136 then (
1137 Queue.iter (fun (k, p, s) ->
1138 wcmd "freetile %s" (~> p);
1139 state.memused <- state.memused - s;
1140 Hashtbl.remove state.tilemap k;
1141 ) state.tilelru;
1142 state.uioh#infochanged Memused;
1143 Queue.clear state.tilelru;
1145 load state.layout;
1148 let stateh h =
1149 let h = truncate (float h*.conf.zoom) in
1150 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
1151 h - d
1154 let opendoc path password =
1155 state.path <- path;
1156 state.password <- password;
1157 state.gen <- state.gen + 1;
1158 state.docinfo <- [];
1159 state.outlines <- [||];
1161 flushpages ();
1162 setaalevel conf.aalevel;
1163 let titlepath =
1164 if emptystr state.origin
1165 then path
1166 else state.origin
1168 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
1169 wcmd "open %d %d %s\000%s\000" (btod !wtmode) (btod !cxack) path password;
1170 invalidate "reqlayout"
1171 (fun () ->
1172 wcmd "reqlayout %d %d %d %s\000"
1173 conf.angle (FMTE.to_int conf.fitmodel)
1174 (stateh state.winh) state.nameddest
1178 let reload () =
1179 state.anchor <- getanchor ();
1180 opendoc state.path state.password;
1183 let scalecolor c =
1184 let c = c *. conf.colorscale in
1185 (c, c, c);
1188 let scalecolor2 (r, g, b) =
1189 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1192 let docolumns columns =
1193 let wadj = wadjsb () in
1194 match columns with
1195 | Csingle _ ->
1196 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1197 let wadj = wadjsb () in
1198 let rec loop pageno pdimno pdim y ph pdims =
1199 if pageno = state.pagecount
1200 then ()
1201 else
1202 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1203 match pdims with
1204 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1205 pdimno+1, pdim, rest
1206 | _ ->
1207 pdimno, pdim, pdims
1209 let x = max 0 (((wadj + state.winw - w) / 2) - xoff) in
1210 let y = y +
1211 (if conf.presentation
1212 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1213 else (if pageno = 0 then 0 else conf.interpagespace)
1216 a.(pageno) <- (pdimno, x, y, pdim);
1217 loop (pageno+1) pdimno pdim (y + h) h pdims
1219 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1220 conf.columns <- Csingle a;
1222 | Cmulti ((columns, coverA, coverB), _) ->
1223 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1224 let rec loop pageno pdimno pdim x y rowh pdims =
1225 let rec fixrow m = if m = pageno then () else
1226 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1227 if h < rowh
1228 then (
1229 let y = y + (rowh - h) / 2 in
1230 a.(m) <- (pdimno, x, y, pdim);
1232 fixrow (m+1)
1234 if pageno = state.pagecount
1235 then fixrow (((pageno - 1) / columns) * columns)
1236 else
1237 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1238 match pdims with
1239 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1240 pdimno+1, pdim, rest
1241 | _ ->
1242 pdimno, pdim, pdims
1244 let x, y, rowh' =
1245 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1246 then (
1247 let x = (wadj + state.winw - w) / 2 in
1248 let ips =
1249 if conf.presentation then calcips h else conf.interpagespace in
1250 x, y + ips + rowh, h
1252 else (
1253 if (pageno - coverA) mod columns = 0
1254 then (
1255 let x = max 0 (wadj + state.winw - state.w) / 2 in
1256 let y =
1257 if conf.presentation
1258 then
1259 let ips = calcips h in
1260 y + (if pageno = 0 then 0 else calcips rowh + ips)
1261 else
1262 y + (if pageno = 0 then 0 else conf.interpagespace)
1264 x, y + rowh, h
1266 else x, y, max rowh h
1269 let y =
1270 if pageno > 1 && (pageno - coverA) mod columns = 0
1271 then (
1272 let y =
1273 if pageno = columns && conf.presentation
1274 then (
1275 let ips = calcips rowh in
1276 for i = 0 to pred columns
1278 let (pdimno, x, y, pdim) = a.(i) in
1279 a.(i) <- (pdimno, x, y+ips, pdim)
1280 done;
1281 y+ips;
1283 else y
1285 fixrow (pageno - columns);
1288 else y
1290 a.(pageno) <- (pdimno, x, y, pdim);
1291 let x = x + w + xoff*2 + conf.interpagespace in
1292 loop (pageno+1) pdimno pdim x y rowh' pdims
1294 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1295 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1297 | Csplit (c, _) ->
1298 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1299 let rec loop pageno pdimno pdim y pdims =
1300 if pageno = state.pagecount
1301 then ()
1302 else
1303 let pdimno, ((_, w, h, _) as pdim), pdims =
1304 match pdims with
1305 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1306 pdimno+1, pdim, rest
1307 | _ ->
1308 pdimno, pdim, pdims
1310 let cw = w / c in
1311 let rec loop1 n x y =
1312 if n = c then y else (
1313 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1314 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1317 let y = loop1 0 0 y in
1318 loop (pageno+1) pdimno pdim y pdims
1320 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1321 conf.columns <- Csplit (c, a);
1324 let represent () =
1325 docolumns conf.columns;
1326 state.maxy <- calcheight ();
1327 if state.reprf == noreprf
1328 then (
1329 match state.mode with
1330 | Birdseye (_, _, pageno, _, _) ->
1331 let y, h = getpageyh pageno in
1332 let top = (state.winh - h) / 2 in
1333 gotoy (max 0 (y - top))
1334 | Textentry _
1335 | View
1336 | LinkNav _ ->
1337 let y = getanchory state.anchor in
1338 let y = min y (state.maxy - state.winw - hscrollh ()) in
1339 gotoy y;
1341 else (
1342 state.reprf ();
1343 state.reprf <- noreprf;
1347 let reshape ?(firsttime=false) w h =
1348 GlDraw.viewport ~x:0 ~y:0 ~w:w ~h:h;
1349 if not firsttime && nogeomcmds state.geomcmds
1350 then state.anchor <- getanchor ();
1352 state.winw <- w;
1353 let w = wadjsb () + (truncate (float w *. conf.zoom)) in
1354 let w = max w 2 in
1355 state.winh <- h;
1356 setfontsize fstate.fontsize;
1357 GlMat.mode `modelview;
1358 GlMat.load_identity ();
1360 GlMat.mode `projection;
1361 GlMat.load_identity ();
1362 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1363 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1364 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
1366 let relx =
1367 if conf.zoom <= 1.0
1368 then 0.0
1369 else float state.x /. float state.w
1371 invalidate "geometry"
1372 (fun () ->
1373 state.w <- w;
1374 if not firsttime
1375 then state.x <- truncate (relx *. float w);
1376 let w =
1377 match conf.columns with
1378 | Csingle _ -> w
1379 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1380 | Csplit (c, _) -> w * c
1382 wcmd "geometry %d %d %d"
1383 w (stateh h) (FMTE.to_int conf.fitmodel)
1387 let enttext () =
1388 let len = String.length state.text in
1389 let x0 = xadjsb () in
1390 let drawstring s =
1391 let hscrollh =
1392 match state.mode with
1393 | Textentry _ | View | LinkNav _ ->
1394 let h, _, _ = state.uioh#scrollpw in
1396 | Birdseye _ -> 0
1398 let rect x w =
1399 filledrect x (float (state.winh - (fstate.fontsize + 4) - hscrollh))
1400 (x+.w) (float (state.winh - hscrollh))
1403 let w = float (wadjsb () + state.winw - 1) in
1404 if state.progress >= 0.0 && state.progress < 1.0
1405 then (
1406 GlDraw.color (0.3, 0.3, 0.3);
1407 let w1 = w *. state.progress in
1408 rect (float x0) w1;
1409 GlDraw.color (0.0, 0.0, 0.0);
1410 rect (float x0+.w1) (float x0+.w-.w1)
1412 else (
1413 GlDraw.color (0.0, 0.0, 0.0);
1414 rect (float x0) w;
1417 GlDraw.color (1.0, 1.0, 1.0);
1418 drawstring fstate.fontsize
1419 (if conf.leftscroll then x0 + 2 else x0 + if len > 0 then 8 else 2)
1420 (state.winh - hscrollh - 5) s;
1422 let s =
1423 match state.mode with
1424 | Textentry ((prefix, text, _, _, _, _), _) ->
1425 let s =
1426 if len > 0
1427 then
1428 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1429 else
1430 Printf.sprintf "%s%s_" prefix text
1434 | Birdseye _
1435 | View
1436 | LinkNav _ -> state.text
1438 let s =
1439 if state.newerrmsgs
1440 then (
1441 if not (istextentry state.mode) && state.uioh#eformsgs
1442 then
1443 let s1 = "(press 'e' to review error messasges)" in
1444 if nonemptystr s then s ^ " " ^ s1 else s1
1445 else s
1447 else s
1449 if nonemptystr s
1450 then drawstring s
1453 let gctiles () =
1454 let len = Queue.length state.tilelru in
1455 let layout = lazy (
1456 match state.throttle with
1457 | None ->
1458 if conf.preload
1459 then preloadlayout state.x state.y state.winw state.winh
1460 else state.layout
1461 | Some (layout, _, _) ->
1462 layout
1463 ) in
1464 let rec loop qpos =
1465 if state.memused <= conf.memlimit
1466 then ()
1467 else (
1468 if qpos < len
1469 then
1470 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1471 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1472 let (_, pw, ph, _) = getpagedim n in
1474 gen = state.gen
1475 && colorspace = conf.colorspace
1476 && angle = conf.angle
1477 && pagew = pw
1478 && pageh = ph
1479 && (
1480 let x = col*conf.tilew
1481 and y = row*conf.tileh in
1482 tilevisible (Lazy.force_val layout) n x y
1484 then Queue.push lruitem state.tilelru
1485 else (
1486 freepbo p;
1487 wcmd "freetile %s" (~> p);
1488 state.memused <- state.memused - s;
1489 state.uioh#infochanged Memused;
1490 Hashtbl.remove state.tilemap k;
1492 loop (qpos+1)
1495 loop 0
1498 let onpagerect pageno f =
1499 let b =
1500 match conf.columns with
1501 | Cmulti (_, b) -> b
1502 | Csingle b -> b
1503 | Csplit (_, b) -> b
1505 if pageno >= 0 && pageno < Array.length b
1506 then
1507 let (_, _, _, (_, w, h, _)) = b.(pageno) in
1508 f w h
1511 let gotopagexy1 pageno x y =
1512 let _,w1,h1,leftx = getpagedim pageno in
1513 let top = y /. (float h1) in
1514 let left = x /. (float w1) in
1515 let py, w, h = getpageywh pageno in
1516 let wh = state.winh - hscrollh () in
1517 let x = left *. (float w) in
1518 let x = leftx + state.x + truncate x in
1519 let wadj = wadjsb () in
1520 let sx =
1521 if x < 0 || x >= wadj + state.winw
1522 then state.x - x
1523 else state.x
1525 let pdy = truncate (top *. float h) in
1526 let y' = py + pdy in
1527 let dy = y' - state.y in
1528 let sy =
1529 if x != state.x || not (dy > 0 && dy < wh)
1530 then (
1531 if conf.presentation
1532 then
1533 if abs (py - y') > wh
1534 then y'
1535 else py
1536 else y';
1538 else state.y
1540 if state.x != sx || state.y != sy
1541 then (
1542 let x, y =
1543 if !wtmode
1544 then (
1545 let ww = wadj + state.winw in
1546 let qx = sx / ww
1547 and qy = pdy / wh in
1548 let x = qx * ww
1549 and y = py + qy * wh in
1550 let x = if -x + ww > w1 then -(w1-ww) else x
1551 and y' = if y + wh > state.maxy then state.maxy - wh else y in
1552 let y =
1553 if conf.presentation
1554 then
1555 if abs (py - y') > wh
1556 then y'
1557 else py
1558 else y';
1560 (x, y)
1562 else (sx, sy)
1564 state.x <- x;
1565 gotoy_and_clear_text y;
1567 else gotoy_and_clear_text state.y;
1570 let gotopagexy pageno x y =
1571 match state.mode with
1572 | Birdseye _ -> gotopage pageno 0.0
1573 | Textentry _
1574 | View
1575 | LinkNav _ -> gotopagexy1 pageno x y
1578 let getpassword () =
1579 let passcmd = getenvwithdef "LLPP_ASKPASS" conf.passcmd in
1580 if emptystr passcmd
1581 then E.s
1582 else getcmdoutput
1583 (fun s ->
1584 impmsg "error getting password: %s" s;
1585 dolog "%s" s) passcmd;
1588 let pgoto opaque pageno x y =
1589 let pdimno = getpdimno pageno in
1590 let x, y = project opaque pageno pdimno x y in
1591 gotopagexy pageno x y;
1594 let act cmds =
1595 (* dolog "%S" cmds; *)
1596 let cl = splitatspace cmds in
1597 let scan s fmt f =
1598 try Scanf.sscanf s fmt f
1599 with exn ->
1600 dolog "error processing '%S': %s" cmds @@ exntos exn;
1601 exit 1
1603 let addoutline outline =
1604 match state.currently with
1605 | Outlining outlines ->
1606 state.currently <- Outlining (outline :: outlines)
1607 | Idle -> state.currently <- Outlining [outline]
1608 | Loading _
1609 | Tiling _ ->
1610 dolog "invalid outlining state";
1611 logcurrently state.currently
1613 match cl with
1614 | "clear" :: [] ->
1615 state.uioh#infochanged Pdim;
1616 state.pdims <- [];
1618 | "clearrects" :: [] ->
1619 state.rects <- state.rects1;
1620 G.postRedisplay "clearrects";
1622 | "continue" :: args :: [] ->
1623 let n = scan args "%u" (fun n -> n) in
1624 state.pagecount <- n;
1625 begin match state.currently with
1626 | Outlining l ->
1627 state.currently <- Idle;
1628 state.outlines <- Array.of_list (List.rev l)
1629 | Idle
1630 | Loading _
1631 | Tiling _ -> ()
1632 end;
1634 let cur, cmds = state.geomcmds in
1635 if emptystr cur
1636 then failwith "umpossible";
1638 begin match List.rev cmds with
1639 | [] ->
1640 state.geomcmds <- E.s, [];
1641 state.throttle <- None;
1642 represent ();
1643 | (s, f) :: rest ->
1644 f ();
1645 state.geomcmds <- s, List.rev rest;
1646 end;
1647 if conf.maxwait = None && not !wtmode
1648 then G.postRedisplay "continue";
1650 | "msg" :: args :: [] ->
1651 showtext ' ' args
1653 | "vmsg" :: args :: [] ->
1654 if conf.verbose
1655 then showtext ' ' args
1657 | "emsg" :: args :: [] ->
1658 Buffer.add_string state.errmsgs args;
1659 state.newerrmsgs <- true;
1660 G.postRedisplay "error message"
1662 | "progress" :: args :: [] ->
1663 let progress, text =
1664 scan args "%f %n"
1665 (fun f pos ->
1666 f, String.sub args pos (String.length args - pos))
1668 state.text <- text;
1669 state.progress <- progress;
1670 G.postRedisplay "progress"
1672 | "firstmatch" :: args :: [] ->
1673 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1674 scan args "%u %d %f %f %f %f %f %f %f %f"
1675 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1676 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1678 let xoff = float (xadjsb ()) in
1679 let x0 = x0 +. xoff
1680 and x1 = x1 +. xoff
1681 and x2 = x2 +. xoff
1682 and x3 = x3 +. xoff in
1683 let y = (getpagey pageno) + truncate y0 in
1684 if conf.zoom > 1.0
1685 then state.x <- truncate (xoff -. x0) + state.winw/2;
1686 addnav ();
1687 gotoy y;
1688 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1689 state.rects1 <- [pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)]
1691 | "match" :: args :: [] ->
1692 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1693 scan args "%u %d %f %f %f %f %f %f %f %f"
1694 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1695 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1697 let xoff = float (xadjsb ()) in
1698 let x0 = x0 +. xoff
1699 and x1 = x1 +. xoff
1700 and x2 = x2 +. xoff
1701 and x3 = x3 +. xoff in
1702 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1703 state.rects1 <-
1704 (pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1706 | "page" :: args :: [] ->
1707 let pageopaques, t = scan args "%s %f" (fun p t -> p, t) in
1708 let pageopaque = ~< pageopaques in
1709 begin match state.currently with
1710 | Loading (l, gen) ->
1711 vlog "page %d took %f sec" l.pageno t;
1712 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1713 begin match state.throttle with
1714 | None ->
1715 let preloadedpages =
1716 if conf.preload
1717 then preloadlayout state.x state.y state.winw state.winh
1718 else state.layout
1720 let evict () =
1721 let set =
1722 List.fold_left (fun s l -> IntSet.add l.pageno s)
1723 IntSet.empty preloadedpages
1725 let evictedpages =
1726 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1727 if not (IntSet.mem pageno set)
1728 then (
1729 wcmd "freepage %s" (~> opaque);
1730 key :: accu
1732 else accu
1733 ) state.pagemap []
1735 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1737 evict ();
1738 state.currently <- Idle;
1739 if gen = state.gen
1740 then (
1741 tilepage l.pageno pageopaque state.layout;
1742 load state.layout;
1743 load preloadedpages;
1744 let visible = pagevisible state.layout l.pageno in
1745 if visible
1746 then (
1747 match state.mode with
1748 | LinkNav (Ltnotready (pageno, dir)) ->
1749 if pageno = l.pageno
1750 then (
1751 let link =
1752 let ld =
1753 if dir = 0
1754 then LDfirstvisible (l.pagex, l.pagey, dir)
1755 else (
1756 if dir > 0 then LDfirst else LDlast
1759 findlink pageopaque ld
1761 match link with
1762 | Lnotfound -> ()
1763 | Lfound n ->
1764 showlinktype (getlink pageopaque n);
1765 state.mode <- LinkNav (Ltexact (l.pageno, n))
1767 | LinkNav (Ltgendir _)
1768 | LinkNav (Ltexact _)
1769 | View
1770 | Birdseye _
1771 | Textentry _ -> ()
1774 if visible && layoutready state.layout
1775 then (
1776 G.postRedisplay "page";
1780 | Some (layout, _, _) ->
1781 state.currently <- Idle;
1782 tilepage l.pageno pageopaque layout;
1783 load state.layout
1784 end;
1786 | Idle
1787 | Tiling _
1788 | Outlining _ ->
1789 dolog "Inconsistent loading state";
1790 logcurrently state.currently;
1791 exit 1
1794 | "tile" :: args :: [] ->
1795 let (x, y, opaques, size, t) =
1796 scan args "%u %u %s %u %f"
1797 (fun x y p size t -> (x, y, p, size, t))
1799 let opaque = ~< opaques in
1800 begin match state.currently with
1801 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1802 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1804 unmappbo opaque;
1805 if tilew != conf.tilew || tileh != conf.tileh
1806 then (
1807 wcmd "freetile %s" (~> opaque);
1808 state.currently <- Idle;
1809 load state.layout;
1811 else (
1812 puttileopaque l col row gen cs angle opaque size t;
1813 state.memused <- state.memused + size;
1814 state.uioh#infochanged Memused;
1815 gctiles ();
1816 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1817 opaque, size) state.tilelru;
1819 let layout =
1820 match state.throttle with
1821 | None -> state.layout
1822 | Some (layout, _, _) -> layout
1825 state.currently <- Idle;
1826 if gen = state.gen
1827 && conf.colorspace = cs
1828 && conf.angle = angle
1829 && tilevisible layout l.pageno x y
1830 then conttiling l.pageno pageopaque;
1832 begin match state.throttle with
1833 | None ->
1834 preload state.layout;
1835 if gen = state.gen
1836 && conf.colorspace = cs
1837 && conf.angle = angle
1838 && tilevisible state.layout l.pageno x y
1839 && (not !wtmode || layoutready state.layout)
1840 then G.postRedisplay "tile nothrottle";
1842 | Some (layout, y, _) ->
1843 let ready = layoutready layout in
1844 if ready
1845 then (
1846 state.y <- y;
1847 state.layout <- layout;
1848 state.throttle <- None;
1849 G.postRedisplay "throttle";
1851 else load layout;
1852 end;
1855 | Idle
1856 | Loading _
1857 | Outlining _ ->
1858 dolog "Inconsistent tiling state";
1859 logcurrently state.currently;
1860 exit 1
1863 | "pdim" :: args :: [] ->
1864 let (n, w, h, _) as pdim =
1865 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1867 let pdim =
1868 match conf.fitmodel with
1869 | FitWidth -> pdim
1870 | FitPage | FitProportional ->
1871 match conf.columns with
1872 | Csplit _ -> (n, w, h, 0)
1873 | Csingle _ | Cmulti _ -> pdim
1875 state.uioh#infochanged Pdim;
1876 state.pdims <- pdim :: state.pdims
1878 | "o" :: args :: [] ->
1879 let (l, n, t, h, pos) =
1880 scan args "%u %u %d %u %n"
1881 (fun l n t h pos -> l, n, t, h, pos)
1883 let s = String.sub args pos (String.length args - pos) in
1884 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
1886 | "ou" :: args :: [] ->
1887 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
1888 let s = String.sub args pos len in
1889 let pos2 = pos + len + 1 in
1890 let uri = String.sub args pos2 (String.length args - pos2) in
1891 addoutline (s, l, Ouri uri)
1893 | "on" :: args :: [] ->
1894 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
1895 let s = String.sub args pos (String.length args - pos) in
1896 addoutline (s, l, Onone)
1898 | "a" :: args :: [] ->
1899 let (n, l, t) =
1900 scan args "%u %d %d" (fun n l t -> n, l, t)
1902 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
1904 | "info" :: args :: [] ->
1905 let pos = nindex args '\t' in
1906 if pos >= 0 && String.sub args 0 pos = "Title"
1907 then (
1908 let s = String.sub args (pos+1) @@ String.length args - pos - 1 in
1909 conf.title <- s;
1910 Wsi.settitle s;
1912 state.docinfo <- (1, args) :: state.docinfo
1914 | "infoend" :: [] ->
1915 state.uioh#infochanged Docinfo;
1916 state.docinfo <- List.rev state.docinfo
1918 | "pass" :: l ->
1919 if l = "fail" :: []
1920 then Wsi.settitle "Wrong password";
1921 let password = getpassword () in
1922 if emptystr password
1923 then error "document is password protected"
1924 else opendoc state.path password
1925 | _ ->
1926 error "unknown cmd `%S'" cmds
1929 let onhist cb =
1930 let rc = cb.rc in
1931 let action = function
1932 | HCprev -> cbget cb ~-1
1933 | HCnext -> cbget cb 1
1934 | HCfirst -> cbget cb ~-(cb.rc)
1935 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1936 and cancel () = cb.rc <- rc
1937 in (action, cancel)
1940 let search pattern forward =
1941 match conf.columns with
1942 | Csplit _ -> impmsg "searching does not work properly in split columns mode"
1943 | Csingle _
1944 | Cmulti _ ->
1945 if nonemptystr pattern
1946 then
1947 let pn, py =
1948 match state.layout with
1949 | [] -> 0, 0
1950 | l :: _ ->
1951 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1953 wcmd "search %d %d %d %d,%s\000"
1954 (btod conf.icase) pn py (btod forward) pattern;
1957 let intentry text key =
1958 let c =
1959 if key >= 32 && key < 127
1960 then Char.chr key
1961 else '\000'
1963 match c with
1964 | '0' .. '9' ->
1965 let text = addchar text c in
1966 TEcont text
1968 | _ ->
1969 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1970 TEcont text
1973 let linknact f s =
1974 if nonemptystr s
1975 then (
1976 let n =
1977 let l = String.length s in
1978 let rec loop pos n = if pos = l then n else
1979 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
1980 loop (pos+1) (n*26 + m)
1981 in loop 0 0
1983 let rec loop n = function
1984 | [] -> ()
1985 | l :: rest ->
1986 match getopaque l.pageno with
1987 | None -> loop n rest
1988 | Some opaque ->
1989 let m = getlinkcount opaque in
1990 if n < m
1991 then (
1992 let under = getlink opaque n in
1993 f under
1995 else loop (n-m) rest
1997 loop n state.layout;
2001 let linknentry text key =
2002 let c =
2003 if key >= 32 && key < 127
2004 then Char.chr key
2005 else '\000'
2007 match c with
2008 | 'a' .. 'z' ->
2009 let text = addchar text c in
2010 linknact (fun under -> state.text <- undertext ~nopath:true under) text;
2011 TEcont text
2013 | _ ->
2014 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2015 TEcont text
2018 let textentry text key =
2019 if key land 0xff00 = 0xff00
2020 then TEcont text
2021 else TEcont (text ^ toutf8 key)
2024 let reqlayout angle fitmodel =
2025 match state.throttle with
2026 | None ->
2027 if nogeomcmds state.geomcmds
2028 then state.anchor <- getanchor ();
2029 conf.angle <- angle mod 360;
2030 if conf.angle != 0
2031 then (
2032 match state.mode with
2033 | LinkNav _ -> state.mode <- View
2034 | Birdseye _
2035 | Textentry _
2036 | View -> ()
2038 conf.fitmodel <- fitmodel;
2039 invalidate "reqlayout"
2040 (fun () ->
2041 wcmd "reqlayout %d %d %d"
2042 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
2044 | _ -> ()
2047 let settrim trimmargins trimfuzz =
2048 if nogeomcmds state.geomcmds
2049 then state.anchor <- getanchor ();
2050 conf.trimmargins <- trimmargins;
2051 conf.trimfuzz <- trimfuzz;
2052 let x0, y0, x1, y1 = trimfuzz in
2053 invalidate "settrim"
2054 (fun () ->
2055 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2056 flushpages ();
2059 let setzoom zoom =
2060 match state.throttle with
2061 | None ->
2062 let zoom = max 0.0001 zoom in
2063 if zoom <> conf.zoom
2064 then (
2065 state.prevzoom <- (conf.zoom, state.x);
2066 conf.zoom <- zoom;
2067 reshape state.winw state.winh;
2068 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2071 | Some (layout, y, started) ->
2072 let time =
2073 match conf.maxwait with
2074 | None -> 0.0
2075 | Some t -> t
2077 let dt = now () -. started in
2078 if dt > time
2079 then (
2080 state.y <- y;
2081 load layout;
2085 let setcolumns mode columns coverA coverB =
2086 state.prevcolumns <- Some (conf.columns, conf.zoom);
2087 if columns < 0
2088 then (
2089 if isbirdseye mode
2090 then impmsg "split mode doesn't work in bird's eye"
2091 else (
2092 conf.columns <- Csplit (-columns, E.a);
2093 state.x <- 0;
2094 conf.zoom <- 1.0;
2097 else (
2098 if columns < 2
2099 then (
2100 conf.columns <- Csingle E.a;
2101 state.x <- 0;
2102 setzoom 1.0;
2104 else (
2105 conf.columns <- Cmulti ((columns, coverA, coverB), E.a);
2106 conf.zoom <- 1.0;
2109 reshape state.winw state.winh;
2112 let resetmstate () =
2113 state.mstate <- Mnone;
2114 Wsi.setcursor Wsi.CURSOR_INHERIT;
2117 let enterbirdseye () =
2118 let zoom = float conf.thumbw /. float state.winw in
2119 let birdseyepageno =
2120 let cy = state.winh / 2 in
2121 let fold = function
2122 | [] -> 0
2123 | l :: rest ->
2124 let rec fold best = function
2125 | [] -> best.pageno
2126 | l :: rest ->
2127 let d = cy - (l.pagedispy + l.pagevh/2)
2128 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2129 if abs d < abs dbest
2130 then fold l rest
2131 else best.pageno
2132 in fold l rest
2134 fold state.layout
2136 state.mode <- Birdseye (
2137 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2139 resetmstate ();
2140 conf.zoom <- zoom;
2141 conf.presentation <- false;
2142 conf.interpagespace <- 10;
2143 conf.hlinks <- false;
2144 conf.fitmodel <- FitPage;
2145 state.x <- 0;
2146 conf.maxwait <- None;
2147 conf.columns <- (
2148 match conf.beyecolumns with
2149 | Some c ->
2150 conf.zoom <- 1.0;
2151 Cmulti ((c, 0, 0), E.a)
2152 | None -> Csingle E.a
2154 if conf.verbose
2155 then
2156 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2157 (100.0*.zoom)
2158 else
2159 state.text <- E.s
2161 reshape state.winw state.winh;
2164 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2165 state.mode <- View;
2166 conf.zoom <- c.zoom;
2167 conf.presentation <- c.presentation;
2168 conf.interpagespace <- c.interpagespace;
2169 conf.maxwait <- c.maxwait;
2170 conf.hlinks <- c.hlinks;
2171 conf.fitmodel <- c.fitmodel;
2172 conf.beyecolumns <- (
2173 match conf.columns with
2174 | Cmulti ((c, _, _), _) -> Some c
2175 | Csingle _ -> None
2176 | Csplit _ -> failwith "leaving bird's eye split mode"
2178 conf.columns <- (
2179 match c.columns with
2180 | Cmulti (c, _) -> Cmulti (c, E.a)
2181 | Csingle _ -> Csingle E.a
2182 | Csplit (c, _) -> Csplit (c, E.a)
2184 if conf.verbose
2185 then
2186 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2187 (100.0*.conf.zoom)
2189 reshape state.winw state.winh;
2190 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2191 state.x <- leftx;
2194 let togglebirdseye () =
2195 match state.mode with
2196 | Birdseye vals -> leavebirdseye vals true
2197 | View -> enterbirdseye ()
2198 | Textentry _
2199 | LinkNav _ -> ()
2202 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2203 let pageno = max 0 (pageno - incr) in
2204 let rec loop = function
2205 | [] -> gotopage1 pageno 0
2206 | l :: _ when l.pageno = pageno ->
2207 if l.pagedispy >= 0 && l.pagey = 0
2208 then G.postRedisplay "upbirdseye"
2209 else gotopage1 pageno 0
2210 | _ :: rest -> loop rest
2212 loop state.layout;
2213 state.text <- E.s;
2214 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2217 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2218 let pageno = min (state.pagecount - 1) (pageno + incr) in
2219 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2220 let rec loop = function
2221 | [] ->
2222 let y, h = getpageyh pageno in
2223 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2224 gotoy (clamp dy)
2225 | l :: _ when l.pageno = pageno ->
2226 if l.pagevh != l.pageh
2227 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2228 else G.postRedisplay "downbirdseye"
2229 | _ :: rest -> loop rest
2231 loop state.layout;
2232 state.text <- E.s;
2235 let optentry mode _ key =
2236 let btos b = if b then "on" else "off" in
2237 if key >= 32 && key < 127
2238 then
2239 let c = Char.chr key in
2240 match c with
2241 | 's' ->
2242 let ondone s =
2243 try conf.scrollstep <- int_of_string s with exc ->
2244 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exc
2246 TEswitch ("scroll step: ", E.s, None, intentry, ondone, true)
2248 | 'A' ->
2249 let ondone s =
2251 conf.autoscrollstep <- boundastep state.winh (int_of_string s);
2252 if state.autoscroll <> None
2253 then state.autoscroll <- Some conf.autoscrollstep
2254 with exc ->
2255 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exc
2257 TEswitch ("auto scroll step: ", E.s, None, intentry, ondone, true)
2259 | 'C' ->
2260 let ondone s =
2262 let n, a, b = multicolumns_of_string s in
2263 setcolumns mode n a b;
2264 with exc ->
2265 state.text <- Printf.sprintf "bad columns `%s': %s" s @@ exntos exc
2267 TEswitch ("columns: ", E.s, None, textentry, ondone, true)
2269 | 'Z' ->
2270 let ondone s =
2272 let zoom = float (int_of_string s) /. 100.0 in
2273 setzoom zoom
2274 with exc ->
2275 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exc
2277 TEswitch ("zoom: ", E.s, None, intentry, ondone, true)
2279 | 't' ->
2280 let ondone s =
2282 conf.thumbw <- bound (int_of_string s) 2 4096;
2283 state.text <-
2284 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2285 begin match mode with
2286 | Birdseye beye ->
2287 leavebirdseye beye false;
2288 enterbirdseye ();
2289 | Textentry _
2290 | View
2291 | LinkNav _ -> ();
2293 with exc ->
2294 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exc
2296 TEswitch ("thumbnail width: ", E.s, None, intentry, ondone, true)
2298 | 'R' ->
2299 let ondone s =
2300 match try
2301 Some (int_of_string s)
2302 with exc ->
2303 state.text <-
2304 Printf.sprintf "bad integer `%s': %s" s @@ exntos exc;
2305 None
2306 with
2307 | Some angle -> reqlayout angle conf.fitmodel
2308 | None -> ()
2310 TEswitch ("rotation: ", E.s, None, intentry, ondone, true)
2312 | 'i' ->
2313 conf.icase <- not conf.icase;
2314 TEdone ("case insensitive search " ^ (btos conf.icase))
2316 | 'p' ->
2317 conf.preload <- not conf.preload;
2318 gotoy state.y;
2319 TEdone ("preload " ^ (btos conf.preload))
2321 | 'v' ->
2322 conf.verbose <- not conf.verbose;
2323 TEdone ("verbose " ^ (btos conf.verbose))
2325 | 'd' ->
2326 conf.debug <- not conf.debug;
2327 TEdone ("debug " ^ (btos conf.debug))
2329 | 'h' ->
2330 conf.maxhfit <- not conf.maxhfit;
2331 state.maxy <- calcheight ();
2332 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2334 | 'c' ->
2335 conf.crophack <- not conf.crophack;
2336 TEdone ("crophack " ^ btos conf.crophack)
2338 | 'a' ->
2339 let s =
2340 match conf.maxwait with
2341 | None ->
2342 conf.maxwait <- Some infinity;
2343 "always wait for page to complete"
2344 | Some _ ->
2345 conf.maxwait <- None;
2346 "show placeholder if page is not ready"
2348 TEdone s
2350 | 'f' ->
2351 conf.underinfo <- not conf.underinfo;
2352 TEdone ("underinfo " ^ btos conf.underinfo)
2354 | 'P' ->
2355 conf.savebmarks <- not conf.savebmarks;
2356 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2358 | 'S' ->
2359 let ondone s =
2361 let pageno, py =
2362 match state.layout with
2363 | [] -> 0, 0
2364 | l :: _ ->
2365 l.pageno, l.pagey
2367 conf.interpagespace <- int_of_string s;
2368 docolumns conf.columns;
2369 state.maxy <- calcheight ();
2370 let y = getpagey pageno in
2371 gotoy (y + py)
2372 with exc ->
2373 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exc
2375 TEswitch ("vertical margin: ", E.s, None, intentry, ondone, true)
2377 | 'l' ->
2378 let fm =
2379 match conf.fitmodel with
2380 | FitProportional -> FitWidth
2381 | FitWidth | FitPage -> FitProportional
2383 reqlayout conf.angle fm;
2384 TEdone ("proportional display " ^ btos (fm == FitProportional))
2386 | 'T' ->
2387 settrim (not conf.trimmargins) conf.trimfuzz;
2388 TEdone ("trim margins " ^ btos conf.trimmargins)
2390 | 'I' ->
2391 conf.invert <- not conf.invert;
2392 TEdone ("invert colors " ^ btos conf.invert)
2394 | 'x' ->
2395 let ondone s =
2396 cbput state.hists.sel s;
2397 conf.selcmd <- s;
2399 TEswitch ("selection command: ", E.s, Some (onhist state.hists.sel),
2400 textentry, ondone, true)
2402 | 'M' ->
2403 if conf.pax == None
2404 then conf.pax <- Some (ref (0.0, 0, 0))
2405 else conf.pax <- None;
2406 TEdone ("PAX " ^ btos (conf.pax != None))
2408 | _ ->
2409 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2410 TEstop
2411 else
2412 TEcont state.text
2415 class type lvsource = object
2416 method getitemcount : int
2417 method getitem : int -> (string * int)
2418 method hasaction : int -> bool
2419 method exit :
2420 uioh:uioh ->
2421 cancel:bool ->
2422 active:int ->
2423 first:int ->
2424 pan:int ->
2425 uioh option
2426 method getactive : int
2427 method getfirst : int
2428 method getpan : int
2429 method getminfo : (int * int) array
2430 end;;
2432 class virtual lvsourcebase = object
2433 val mutable m_active = 0
2434 val mutable m_first = 0
2435 val mutable m_pan = 0
2436 method getactive = m_active
2437 method getfirst = m_first
2438 method getpan = m_pan
2439 method getminfo : (int * int) array = E.a
2440 end;;
2442 let textentrykeyboard
2443 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2444 state.text <- E.s;
2445 let key =
2446 if key >= 0xffb0 && key <= 0xffb9
2447 then key - 0xffb0 + 48 else key
2449 let enttext te =
2450 state.mode <- Textentry (te, onleave);
2451 enttext ();
2452 G.postRedisplay "textentrykeyboard enttext";
2454 let histaction cmd =
2455 match opthist with
2456 | None -> ()
2457 | Some (action, _) ->
2458 state.mode <- Textentry (
2459 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
2461 G.postRedisplay "textentry histaction"
2463 match key with
2464 | @backspace ->
2465 if emptystr text && cancelonempty
2466 then (
2467 onleave Cancel;
2468 G.postRedisplay "textentrykeyboard after cancel";
2470 else
2471 let s = withoutlastutf8 text in
2472 enttext (c, s, opthist, onkey, ondone, cancelonempty)
2474 | @enter | @kpenter ->
2475 ondone text;
2476 onleave Confirm;
2477 G.postRedisplay "textentrykeyboard after confirm"
2479 | @up | @kpup -> histaction HCprev
2480 | @down | @kpdown -> histaction HCnext
2481 | @home | @kphome -> histaction HCfirst
2482 | @jend | @kpend -> histaction HClast
2484 | @escape ->
2485 if emptystr text
2486 then (
2487 begin match opthist with
2488 | None -> ()
2489 | Some (_, onhistcancel) -> onhistcancel ()
2490 end;
2491 onleave Cancel;
2492 state.text <- E.s;
2493 G.postRedisplay "textentrykeyboard after cancel2"
2495 else (
2496 enttext (c, E.s, opthist, onkey, ondone, cancelonempty)
2499 | @delete | @kpdelete -> ()
2501 | _ when key != 0
2502 && key land 0xff00 != 0xff00 (* keyboard *)
2503 && key land 0xfe00 != 0xfe00 (* xkb *)
2504 && key land 0xfd00 != 0xfd00 (* 3270 *)
2506 begin match onkey text key with
2507 | TEdone text ->
2508 ondone text;
2509 onleave Confirm;
2510 G.postRedisplay "textentrykeyboard after confirm2";
2512 | TEcont text ->
2513 enttext (c, text, opthist, onkey, ondone, cancelonempty);
2515 | TEstop ->
2516 onleave Cancel;
2517 G.postRedisplay "textentrykeyboard after cancel3"
2519 | TEswitch te ->
2520 state.mode <- Textentry (te, onleave);
2521 G.postRedisplay "textentrykeyboard switch";
2522 end;
2524 | _ ->
2525 vlog "unhandled key %s" (Wsi.keyname key)
2528 let firstof first active =
2529 if first > active || abs (first - active) > fstate.maxrows - 1
2530 then max 0 (active - (fstate.maxrows/2))
2531 else first
2534 let calcfirst first active =
2535 if active > first
2536 then
2537 let rows = active - first in
2538 if rows > fstate.maxrows then active - fstate.maxrows else first
2539 else active
2542 let scrollph y maxy =
2543 let sh = float (maxy + state.winh) /. float state.winh in
2544 let sh = float state.winh /. sh in
2545 let sh = max sh (float conf.scrollh) in
2547 let percent = float y /. float maxy in
2548 let position = (float state.winh -. sh) *. percent in
2550 let position =
2551 if position +. sh > float state.winh
2552 then float state.winh -. sh
2553 else position
2555 position, sh;
2558 let coe s = (s :> uioh);;
2560 class listview ~zebra ~helpmode ~(source:lvsource) ~trusted ~modehash =
2561 object (self)
2562 val m_pan = source#getpan
2563 val m_first = source#getfirst
2564 val m_active = source#getactive
2565 val m_qsearch = E.s
2566 val m_prev_uioh = state.uioh
2568 method private elemunder y =
2569 if y < 0
2570 then None
2571 else
2572 let n = y / (fstate.fontsize+1) in
2573 if m_first + n < source#getitemcount
2574 then (
2575 if source#hasaction (m_first + n)
2576 then Some (m_first + n)
2577 else None
2579 else None
2581 method display =
2582 Gl.enable `blend;
2583 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
2584 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2585 filledrect 0. 0. (float state.winw) (float state.winh);
2586 GlDraw.color (1., 1., 1.);
2587 Gl.enable `texture_2d;
2588 let fs = fstate.fontsize in
2589 let nfs = fs + 1 in
2590 let hw = (wadjsb () + xadjsb () + state.winw)/3 in
2591 let ww = fstate.wwidth in
2592 let tabw = 17.0*.ww in
2593 let itemcount = source#getitemcount in
2594 let minfo = source#getminfo in
2595 let x0, x1 =
2596 if conf.leftscroll
2597 then float (xadjsb ()), float (state.winw - 1)
2598 else 0.0, float (state.winw - conf.scrollbw - 1)
2600 let xadj = xadjsb () in
2601 let rec loop row =
2602 if (row - m_first) > fstate.maxrows
2603 then ()
2604 else (
2605 if row >= 0 && row < itemcount
2606 then (
2607 let (s, level) = source#getitem row in
2608 let y = (row - m_first) * nfs in
2609 let x =
2610 (if conf.leftscroll then float xadj else 5.0)
2611 +. (float (level + m_pan)) *. ww in
2612 if helpmode
2613 then GlDraw.color
2614 (let c = if row land 1 = 0 then 1.0 else 0.92 in (c,c,c));
2616 if row = m_active
2617 then (
2618 Gl.disable `texture_2d;
2619 let alpha = if source#hasaction row then 0.9 else 0.3 in
2620 GlDraw.color (1., 1., 1.) ~alpha;
2621 linerect (x0 +. 1.) (float (y + 1)) (x1) (float (y + fs + 3));
2622 Gl.enable `texture_2d;
2624 let c =
2625 if zebra && row land 1 = 1
2626 then 0.8
2627 else 1.0
2629 GlDraw.color (c,c,c);
2630 let drawtabularstring s =
2631 let drawstr x s =
2632 let x' = truncate (x0 +. x) in
2633 let pos = nindex s '\000' in
2634 if pos = -1
2635 then drawstring1 fs x' (y+nfs) s
2636 else
2637 let s1 = String.sub s 0 pos
2638 and s2 = String.sub s (pos+1) (String.length s - pos - 1) in
2639 let rec e s =
2640 if emptystr s
2641 then s
2642 else
2643 let s' = withoutlastutf8 s in
2644 let s = s' ^ "@Uellipsis" in
2645 let w = measurestr fs s in
2646 if float x' +. w +. ww < float (hw + x')
2647 then s
2648 else e s'
2650 let s1 =
2651 if float x' +. ww +. measurestr fs s1 > float (hw + x')
2652 then e s1
2653 else s1
2655 ignore (drawstring1 fs x' (y+nfs) s1);
2656 drawstring1 fs (hw + x') (y+nfs) s2
2658 if trusted
2659 then
2660 let x = if helpmode && row > 0 then x +. ww else x in
2661 let tabpos = nindex s '\t' in
2662 if tabpos > 0
2663 then
2664 let len = String.length s - tabpos - 1 in
2665 let s1 = String.sub s 0 tabpos
2666 and s2 = String.sub s (tabpos + 1) len in
2667 let nx = drawstr x s1 in
2668 let sw = nx -. x in
2669 let x = x +. (max tabw sw) in
2670 drawstr x s2
2671 else
2672 let len = String.length s - 2 in
2673 if len > 0 && s.[0] = '\xc2' && s.[1] = '\xb7'
2674 then
2675 let s = String.sub s 2 len in
2676 let x = if not helpmode then x +. ww else x in
2677 GlDraw.color (1.2, 1.2, 1.2);
2678 let vinc = drawstring1 (fs+fs/4)
2679 (truncate (x -. ww)) (y+nfs) s in
2680 GlDraw.color (1., 1., 1.);
2681 vinc +. (float fs *. 0.8)
2682 else
2683 drawstr x s
2684 else
2685 drawstr x s
2687 ignore (drawtabularstring s);
2688 loop (row+1)
2692 loop m_first;
2693 GlDraw.color (1.0, 1.0, 1.0) ~alpha:0.5;
2694 let xadj = float (xadjsb () + 5) in
2695 let rec loop row =
2696 if (row - m_first) > fstate.maxrows
2697 then ()
2698 else (
2699 if row >= 0 && row < itemcount
2700 then (
2701 let (s, level) = source#getitem row in
2702 let pos0 = nindex s '\000' in
2703 let y = (row - m_first) * nfs in
2704 let x = float (level + m_pan) *. ww in
2705 let (first, last) = minfo.(row) in
2706 let prefix =
2707 if pos0 > 0 && first > pos0
2708 then String.sub s (pos0+1) (first-pos0-1)
2709 else String.sub s 0 first
2711 let suffix = String.sub s first (last - first) in
2712 let w1 = measurestr fstate.fontsize prefix in
2713 let w2 = measurestr fstate.fontsize suffix in
2714 let x = x +. if conf.leftscroll then xadj else 5.0 in
2715 let x = if pos0 > 0 && first > pos0 then x +. float hw else x in
2716 let x0 = x +. w1
2717 and y0 = float (y+2) in
2718 let x1 = x0 +. w2
2719 and y1 = float (y+fs+3) in
2720 filledrect x0 y0 x1 y1;
2721 loop (row+1)
2725 Gl.disable `texture_2d;
2726 if Array.length minfo > 0 then loop m_first;
2727 Gl.disable `blend;
2729 method updownlevel incr =
2730 let len = source#getitemcount in
2731 let curlevel =
2732 if m_active >= 0 && m_active < len
2733 then snd (source#getitem m_active)
2734 else -1
2736 let rec flow i =
2737 if i = len then i-1 else if i = -1 then 0 else
2738 let _, l = source#getitem i in
2739 if l != curlevel then i else flow (i+incr)
2741 let active = flow m_active in
2742 let first = calcfirst m_first active in
2743 G.postRedisplay "outline updownlevel";
2744 {< m_active = active; m_first = first >}
2746 method private key1 key mask =
2747 let set1 active first qsearch =
2748 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2750 let search active pattern incr =
2751 let active = if active = -1 then m_first else active in
2752 let dosearch re =
2753 let rec loop n =
2754 if n >= 0 && n < source#getitemcount
2755 then (
2756 let s, _ = source#getitem n in
2757 match Str.search_forward re s 0 with
2758 | (exception Not_found) -> loop (n + incr)
2759 | _ -> Some n
2761 else None
2763 loop active
2765 Str.regexp_case_fold pattern |> dosearch
2767 let itemcount = source#getitemcount in
2768 let find start incr =
2769 let rec find i =
2770 if i = -1 || i = itemcount
2771 then -1
2772 else (
2773 if source#hasaction i
2774 then i
2775 else find (i + incr)
2778 find start
2780 let set active first =
2781 let first = bound first 0 (itemcount - fstate.maxrows) in
2782 state.text <- E.s;
2783 coe {< m_active = active; m_first = first; m_qsearch = E.s >}
2785 let navigate incr =
2786 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2787 let active, first =
2788 let incr1 = if incr > 0 then 1 else -1 in
2789 if isvisible m_first m_active
2790 then
2791 let next =
2792 let next = m_active + incr in
2793 let next =
2794 if next < 0 || next >= itemcount
2795 then -1
2796 else find next incr1
2798 if abs (m_active - next) > fstate.maxrows
2799 then -1
2800 else next
2802 if next = -1
2803 then
2804 let first = m_first + incr in
2805 let first = bound first 0 (itemcount - fstate.maxrows) in
2806 let next =
2807 let next = m_active + incr in
2808 let next = bound next 0 (itemcount - 1) in
2809 find next ~-incr1
2811 let active =
2812 if next = -1
2813 then m_active
2814 else (
2815 if isvisible first next
2816 then next
2817 else m_active
2820 active, first
2821 else
2822 let first = min next m_first in
2823 let first =
2824 if abs (next - first) > fstate.maxrows
2825 then first + incr
2826 else first
2828 next, first
2829 else
2830 let first = m_first + incr in
2831 let first = bound first 0 (itemcount - 1) in
2832 let active =
2833 let next = m_active + incr in
2834 let next = bound next 0 (itemcount - 1) in
2835 let next = find next incr1 in
2836 let active =
2837 if next = -1 || abs (m_active - first) > fstate.maxrows
2838 then (
2839 let active = if m_active = -1 then next else m_active in
2840 active
2842 else next
2844 if isvisible first active
2845 then active
2846 else -1
2848 active, first
2850 G.postRedisplay "listview navigate";
2851 set active first;
2853 match key with
2854 | (@r|@s) when Wsi.withctrl mask ->
2855 let incr = if key = @r then -1 else 1 in
2856 let active, first =
2857 match search (m_active + incr) m_qsearch incr with
2858 | None ->
2859 state.text <- m_qsearch ^ " [not found]";
2860 m_active, m_first
2861 | Some active ->
2862 state.text <- m_qsearch;
2863 active, firstof m_first active
2865 G.postRedisplay "listview ctrl-r/s";
2866 set1 active first m_qsearch;
2868 | @insert when Wsi.withctrl mask ->
2869 if m_active >= 0 && m_active < source#getitemcount
2870 then (
2871 let s, _ = source#getitem m_active in
2872 selstring s;
2874 coe self
2876 | @backspace ->
2877 if emptystr m_qsearch
2878 then coe self
2879 else (
2880 let qsearch = withoutlastutf8 m_qsearch in
2881 if emptystr qsearch
2882 then (
2883 state.text <- E.s;
2884 G.postRedisplay "listview empty qsearch";
2885 set1 m_active m_first E.s;
2887 else
2888 let active, first =
2889 match search m_active qsearch ~-1 with
2890 | None ->
2891 state.text <- qsearch ^ " [not found]";
2892 m_active, m_first
2893 | Some active ->
2894 state.text <- qsearch;
2895 active, firstof m_first active
2897 G.postRedisplay "listview backspace qsearch";
2898 set1 active first qsearch
2901 | key when (key != 0 && key land 0xff00 != 0xff00) ->
2902 let pattern = m_qsearch ^ toutf8 key in
2903 let active, first =
2904 match search m_active pattern 1 with
2905 | None ->
2906 state.text <- pattern ^ " [not found]";
2907 m_active, m_first
2908 | Some active ->
2909 state.text <- pattern;
2910 active, firstof m_first active
2912 G.postRedisplay "listview qsearch add";
2913 set1 active first pattern;
2915 | @escape ->
2916 state.text <- E.s;
2917 if emptystr m_qsearch
2918 then (
2919 G.postRedisplay "list view escape";
2920 let mx, my = state.mpos in
2921 updateunder mx my;
2922 begin
2923 match
2924 source#exit ~uioh:(coe self)
2925 ~cancel:true ~active:m_active ~first:m_first ~pan:m_pan
2926 with
2927 | None -> m_prev_uioh
2928 | Some uioh -> uioh
2931 else (
2932 G.postRedisplay "list view kill qsearch";
2933 coe {< m_qsearch = E.s >}
2936 | @enter | @kpenter ->
2937 state.text <- E.s;
2938 let self = {< m_qsearch = E.s >} in
2939 let opt =
2940 G.postRedisplay "listview enter";
2941 if m_active >= 0 && m_active < source#getitemcount
2942 then (
2943 source#exit ~uioh:(coe self) ~cancel:false
2944 ~active:m_active ~first:m_first ~pan:m_pan;
2946 else (
2947 source#exit ~uioh:(coe self) ~cancel:true
2948 ~active:m_active ~first:m_first ~pan:m_pan;
2951 begin match opt with
2952 | None -> m_prev_uioh
2953 | Some uioh -> uioh
2956 | @delete | @kpdelete ->
2957 coe self
2959 | @up | @kpup -> navigate ~-1
2960 | @down | @kpdown -> navigate 1
2961 | @prior | @kpprior -> navigate ~-(fstate.maxrows)
2962 | @next | @kpnext -> navigate fstate.maxrows
2964 | @right | @kpright ->
2965 state.text <- E.s;
2966 G.postRedisplay "listview right";
2967 coe {< m_pan = m_pan - 1 >}
2969 | @left | @kpleft ->
2970 state.text <- E.s;
2971 G.postRedisplay "listview left";
2972 coe {< m_pan = m_pan + 1 >}
2974 | @home | @kphome ->
2975 let active = find 0 1 in
2976 G.postRedisplay "listview home";
2977 set active 0;
2979 | @jend | @kpend ->
2980 let first = max 0 (itemcount - fstate.maxrows) in
2981 let active = find (itemcount - 1) ~-1 in
2982 G.postRedisplay "listview end";
2983 set active first;
2985 | key when (key = 0 || key land 0xff00 = 0xff00) ->
2986 coe self
2988 | _ ->
2989 dolog "listview unknown key %#x" key; coe self
2991 method key key mask =
2992 match state.mode with
2993 | Textentry te -> textentrykeyboard key mask te; coe self
2994 | Birdseye _
2995 | View
2996 | LinkNav _ -> self#key1 key mask
2998 method button button down x y _ =
2999 let opt =
3000 match button with
3001 | 1 when vscrollhit x ->
3002 G.postRedisplay "listview scroll";
3003 if down
3004 then
3005 let _, position, sh = self#scrollph in
3006 if y > truncate position && y < truncate (position +. sh)
3007 then (
3008 state.mstate <- Mscrolly;
3009 Some (coe self)
3011 else
3012 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3013 let first = truncate (s *. float source#getitemcount) in
3014 let first = min source#getitemcount first in
3015 Some (coe {< m_first = first; m_active = first >})
3016 else (
3017 state.mstate <- Mnone;
3018 Some (coe self);
3020 | 1 when down ->
3021 begin match self#elemunder y with
3022 | Some n ->
3023 G.postRedisplay "listview click";
3024 source#exit ~uioh:(coe {< m_active = n >})
3025 ~cancel:false ~active:n ~first:m_first ~pan:m_pan
3026 | _ ->
3027 Some (coe self)
3029 | n when (n == 4 || n == 5) && not down ->
3030 let len = source#getitemcount in
3031 let first =
3032 if n = 5 && m_first + fstate.maxrows >= len
3033 then
3034 m_first
3035 else
3036 let first = m_first + (if n == 4 then -1 else 1) in
3037 bound first 0 (len - 1)
3039 G.postRedisplay "listview wheel";
3040 Some (coe {< m_first = first >})
3041 | n when (n = 6 || n = 7) && not down ->
3042 let inc = if n = 7 then -1 else 1 in
3043 G.postRedisplay "listview hwheel";
3044 Some (coe {< m_pan = m_pan + inc >})
3045 | _ ->
3046 Some (coe self)
3048 match opt with
3049 | None -> m_prev_uioh
3050 | Some uioh -> uioh
3052 method multiclick _ x y = self#button 1 true x y
3054 method motion _ y =
3055 match state.mstate with
3056 | Mscrolly ->
3057 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3058 let first = truncate (s *. float source#getitemcount) in
3059 let first = min source#getitemcount first in
3060 G.postRedisplay "listview motion";
3061 coe {< m_first = first; m_active = first >}
3062 | Msel _
3063 | Mpan _
3064 | Mscrollx
3065 | Mzoom _
3066 | Mzoomrect _
3067 | Mnone -> coe self
3069 method pmotion x y =
3070 if x < state.winw - conf.scrollbw
3071 then
3072 let n =
3073 match self#elemunder y with
3074 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3075 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3077 let o =
3078 if n != m_active
3079 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3080 else self
3082 coe o
3083 else (
3084 Wsi.setcursor Wsi.CURSOR_INHERIT;
3085 coe self
3088 method infochanged _ = ()
3090 method scrollpw = (0, 0.0, 0.0)
3091 method scrollph =
3092 let nfs = fstate.fontsize + 1 in
3093 let y = m_first * nfs in
3094 let itemcount = source#getitemcount in
3095 let maxi = max 0 (itemcount - fstate.maxrows) in
3096 let maxy = maxi * nfs in
3097 let p, h = scrollph y maxy in
3098 conf.scrollbw, p, h
3100 method modehash = modehash
3101 method eformsgs = false
3102 method alwaysscrolly = true
3103 end;;
3105 class outlinelistview ~zebra ~source =
3106 let settext autonarrow s =
3107 if autonarrow
3108 then
3109 let ss = source#statestr in
3110 state.text <-
3111 if emptystr ss
3112 then "[" ^ s ^ "]"
3113 else "{" ^ ss ^ "} [" ^ s ^ "]"
3114 else state.text <- s
3116 object (self)
3117 inherit listview
3118 ~zebra
3119 ~helpmode:false
3120 ~source:(source :> lvsource)
3121 ~trusted:false
3122 ~modehash:(findkeyhash conf "outline")
3123 as super
3125 val m_autonarrow = false
3127 method! key key mask =
3128 let maxrows =
3129 if emptystr state.text
3130 then fstate.maxrows
3131 else fstate.maxrows - 2
3133 let calcfirst first active =
3134 if active > first
3135 then
3136 let rows = active - first in
3137 if rows > maxrows then active - maxrows else first
3138 else active
3140 let navigate incr =
3141 let active = m_active + incr in
3142 let active = bound active 0 (source#getitemcount - 1) in
3143 let first = calcfirst m_first active in
3144 G.postRedisplay "outline navigate";
3145 coe {< m_active = active; m_first = first >}
3147 let navscroll first =
3148 let active =
3149 let dist = m_active - first in
3150 if dist < 0
3151 then first
3152 else (
3153 if dist < maxrows
3154 then m_active
3155 else first + maxrows
3158 G.postRedisplay "outline navscroll";
3159 coe {< m_first = first; m_active = active >}
3161 let ctrl = Wsi.withctrl mask in
3162 match key with
3163 | @a when ctrl ->
3164 let text =
3165 if m_autonarrow
3166 then (source#denarrow; E.s)
3167 else (
3168 let pattern = source#renarrow in
3169 if nonemptystr m_qsearch
3170 then (source#narrow m_qsearch; m_qsearch)
3171 else pattern
3174 settext (not m_autonarrow) text;
3175 G.postRedisplay "toggle auto narrowing";
3176 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
3178 | @slash when emptystr m_qsearch && not m_autonarrow ->
3179 settext true E.s;
3180 G.postRedisplay "toggle auto narrowing";
3181 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
3183 | @n when ctrl ->
3184 source#narrow m_qsearch;
3185 if not m_autonarrow
3186 then source#add_narrow_pattern m_qsearch;
3187 G.postRedisplay "outline ctrl-n";
3188 coe {< m_first = 0; m_active = 0 >}
3190 | @S when ctrl ->
3191 let active = source#calcactive (getanchor ()) in
3192 let first = firstof m_first active in
3193 G.postRedisplay "outline ctrl-s";
3194 coe {< m_first = first; m_active = active >}
3196 | @u when ctrl ->
3197 G.postRedisplay "outline ctrl-u";
3198 if m_autonarrow && nonemptystr m_qsearch
3199 then (
3200 ignore (source#renarrow);
3201 settext m_autonarrow E.s;
3202 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3204 else (
3205 source#del_narrow_pattern;
3206 let pattern = source#renarrow in
3207 let text =
3208 if emptystr pattern then E.s else "Narrowed to " ^ pattern
3210 settext m_autonarrow text;
3211 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3214 | @l when ctrl ->
3215 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3216 G.postRedisplay "outline ctrl-l";
3217 coe {< m_first = first >}
3219 | @tab when m_autonarrow ->
3220 if nonemptystr m_qsearch
3221 then (
3222 G.postRedisplay "outline list view tab";
3223 source#add_narrow_pattern m_qsearch;
3224 settext true E.s;
3225 coe {< m_qsearch = E.s >}
3227 else coe self
3229 | @escape when m_autonarrow ->
3230 if nonemptystr m_qsearch
3231 then source#add_narrow_pattern m_qsearch;
3232 super#key key mask
3234 | @enter | @kpenter when m_autonarrow ->
3235 if nonemptystr m_qsearch
3236 then source#add_narrow_pattern m_qsearch;
3237 super#key key mask
3239 | key when m_autonarrow && (key != 0 && key land 0xff00 != 0xff00) ->
3240 let pattern = m_qsearch ^ toutf8 key in
3241 G.postRedisplay "outlinelistview autonarrow add";
3242 source#narrow pattern;
3243 settext true pattern;
3244 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3246 | key when m_autonarrow && key = @backspace ->
3247 if emptystr m_qsearch
3248 then coe self
3249 else
3250 let pattern = withoutlastutf8 m_qsearch in
3251 G.postRedisplay "outlinelistview autonarrow backspace";
3252 ignore (source#renarrow);
3253 source#narrow pattern;
3254 settext true pattern;
3255 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3257 | @up | @kpup when ctrl ->
3258 navscroll (max 0 (m_first - 1))
3260 | @down | @kpdown when ctrl ->
3261 navscroll (min (source#getitemcount - 1) (m_first + 1))
3263 | @up | @kpup -> navigate ~-1
3264 | @down | @kpdown -> navigate 1
3265 | @prior | @kpprior -> navigate ~-(fstate.maxrows)
3266 | @next | @kpnext -> navigate fstate.maxrows
3268 | @right | @kpright ->
3269 let o =
3270 if ctrl
3271 then (
3272 G.postRedisplay "outline ctrl right";
3273 {< m_pan = m_pan + 1 >}
3275 else self#updownlevel 1
3277 coe o
3279 | @left | @kpleft ->
3280 let o =
3281 if ctrl
3282 then (
3283 G.postRedisplay "outline ctrl left";
3284 {< m_pan = m_pan - 1 >}
3286 else self#updownlevel ~-1
3288 coe o
3290 | @home | @kphome ->
3291 G.postRedisplay "outline home";
3292 coe {< m_first = 0; m_active = 0 >}
3294 | @jend | @kpend ->
3295 let active = source#getitemcount - 1 in
3296 let first = max 0 (active - fstate.maxrows) in
3297 G.postRedisplay "outline end";
3298 coe {< m_active = active; m_first = first >}
3300 | _ -> super#key key mask
3301 end;;
3303 let genhistoutlines () =
3304 Config.gethist ()
3305 |> List.sort (fun (_, c1, _, _, _, _) (_, c2, _, _, _, _) ->
3306 compare c2.lastvisit c1.lastvisit)
3307 |> List.map
3308 (fun ((path, c, _, _, _, origin) as hist) ->
3309 let path = if nonemptystr origin then origin else path in
3310 let base = mbtoutf8 @@ Filename.basename path in
3311 (base ^ "\000" ^ c.title, 1, Ohistory hist)
3313 |> Array.of_list
3316 let gotohist (path, c, bookmarks, x, anchor, origin) =
3317 Config.save leavebirdseye;
3318 state.anchor <- anchor;
3319 state.bookmarks <- bookmarks;
3320 state.origin <- origin;
3321 state.x <- x;
3322 setconf conf c;
3323 let x0, y0, x1, y1 = conf.trimfuzz in
3324 wcmd "trimset %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1;
3325 reshape ~firsttime:true state.winw state.winh;
3326 opendoc path origin;
3327 setzoom c.zoom;
3330 let makecheckers () =
3331 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3332 following to say:
3333 converted by Issac Trotts. July 25, 2002 *)
3334 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3335 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3336 let id = GlTex.gen_texture () in
3337 GlTex.bind_texture ~target:`texture_2d id;
3338 GlPix.store (`unpack_alignment 1);
3339 GlTex.image2d image;
3340 List.iter (GlTex.parameter ~target:`texture_2d)
3341 [ `mag_filter `nearest; `min_filter `nearest ];
3345 let setcheckers enabled =
3346 match state.checkerstexid with
3347 | None ->
3348 if enabled then state.checkerstexid <- Some (makecheckers ())
3350 | Some checkerstexid ->
3351 if not enabled
3352 then (
3353 GlTex.delete_texture checkerstexid;
3354 state.checkerstexid <- None;
3358 let describe_location () =
3359 let fn = page_of_y state.y in
3360 let ln = page_of_y (state.y + state.winh - hscrollh () - 1) in
3361 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
3362 let percent =
3363 if maxy <= 0
3364 then 100.
3365 else (100. *. (float state.y /. float maxy))
3367 if fn = ln
3368 then
3369 Printf.sprintf "page %d of %d [%.2f%%]"
3370 (fn+1) state.pagecount percent
3371 else
3372 Printf.sprintf
3373 "pages %d-%d of %d [%.2f%%]"
3374 (fn+1) (ln+1) state.pagecount percent
3377 let setpresentationmode v =
3378 let n = page_of_y state.y in
3379 state.anchor <- (n, 0.0, 1.0);
3380 conf.presentation <- v;
3381 if conf.fitmodel = FitPage
3382 then reqlayout conf.angle conf.fitmodel;
3383 represent ();
3386 let enterinfomode =
3387 let btos b = if b then "@Uradical" else E.s in
3388 let showextended = ref false in
3389 let leave mode _ = state.mode <- mode in
3390 let src =
3391 (object
3392 val mutable m_l = []
3393 val mutable m_a = E.a
3394 val mutable m_prev_uioh = nouioh
3395 val mutable m_prev_mode = View
3397 inherit lvsourcebase
3399 method reset prev_mode prev_uioh =
3400 m_a <- Array.of_list (List.rev m_l);
3401 m_l <- [];
3402 m_prev_mode <- prev_mode;
3403 m_prev_uioh <- prev_uioh;
3405 method int name get set =
3406 m_l <-
3407 (name, `int get, 1, Action (
3408 fun u ->
3409 let ondone s =
3410 try set (int_of_string s)
3411 with exn ->
3412 state.text <- Printf.sprintf "bad integer `%s': %s"
3413 s @@ exntos exn
3415 state.text <- E.s;
3416 let te = name ^ ": ", E.s, None, intentry, ondone, true in
3417 state.mode <- Textentry (te, leave m_prev_mode);
3419 )) :: m_l
3421 method int_with_suffix name get set =
3422 m_l <-
3423 (name, `intws get, 1, Action (
3424 fun u ->
3425 let ondone s =
3426 try set (int_of_string_with_suffix s)
3427 with exn ->
3428 state.text <- Printf.sprintf "bad integer `%s': %s"
3429 s @@ exntos exn
3431 state.text <- E.s;
3432 let te =
3433 name ^ ": ", E.s, None, intentry_with_suffix, ondone, true
3435 state.mode <- Textentry (te, leave m_prev_mode);
3437 )) :: m_l
3439 method bool ?(offset=1) ?(btos=btos) name get set =
3440 m_l <-
3441 (name, `bool (btos, get), offset, Action (
3442 fun u ->
3443 let v = get () in
3444 set (not v);
3446 )) :: m_l
3448 method color name get set =
3449 m_l <-
3450 (name, `color get, 1, Action (
3451 fun u ->
3452 let invalid = (nan, nan, nan) in
3453 let ondone s =
3454 let c =
3455 try color_of_string s
3456 with exn ->
3457 state.text <- Printf.sprintf "bad color `%s': %s"
3458 s @@ exntos exn;
3459 invalid
3461 if c <> invalid
3462 then set c;
3464 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3465 state.text <- color_to_string (get ());
3466 state.mode <- Textentry (te, leave m_prev_mode);
3468 )) :: m_l
3470 method string name get set =
3471 m_l <-
3472 (name, `string get, 1, Action (
3473 fun u ->
3474 let ondone s = set s in
3475 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3476 state.mode <- Textentry (te, leave m_prev_mode);
3478 )) :: m_l
3480 method colorspace name get set =
3481 m_l <-
3482 (name, `string get, 1, Action (
3483 fun _ ->
3484 let source =
3485 (object
3486 inherit lvsourcebase
3488 initializer
3489 m_active <- CSTE.to_int conf.colorspace;
3490 m_first <- 0;
3492 method getitemcount =
3493 Array.length CSTE.names
3494 method getitem n =
3495 (CSTE.names.(n), 0)
3496 method exit ~uioh ~cancel ~active ~first ~pan =
3497 ignore (uioh, first, pan);
3498 if not cancel then set active;
3499 None
3500 method hasaction _ = true
3501 end)
3503 state.text <- E.s;
3504 let modehash = findkeyhash conf "info" in
3505 coe (new listview ~zebra:false ~helpmode:false
3506 ~source ~trusted:true ~modehash)
3507 )) :: m_l
3509 method paxmark name get set =
3510 m_l <-
3511 (name, `string get, 1, Action (
3512 fun _ ->
3513 let source =
3514 (object
3515 inherit lvsourcebase
3517 initializer
3518 m_active <- MTE.to_int conf.paxmark;
3519 m_first <- 0;
3521 method getitemcount = Array.length MTE.names
3522 method getitem n = (MTE.names.(n), 0)
3523 method exit ~uioh ~cancel ~active ~first ~pan =
3524 ignore (uioh, first, pan);
3525 if not cancel then set active;
3526 None
3527 method hasaction _ = true
3528 end)
3530 state.text <- E.s;
3531 let modehash = findkeyhash conf "info" in
3532 coe (new listview ~zebra:false ~helpmode:false
3533 ~source ~trusted:true ~modehash)
3534 )) :: m_l
3536 method fitmodel name get set =
3537 m_l <-
3538 (name, `string get, 1, Action (
3539 fun _ ->
3540 let source =
3541 (object
3542 inherit lvsourcebase
3544 initializer
3545 m_active <- FMTE.to_int conf.fitmodel;
3546 m_first <- 0;
3548 method getitemcount = Array.length FMTE.names
3549 method getitem n = (FMTE.names.(n), 0)
3550 method exit ~uioh ~cancel ~active ~first ~pan =
3551 ignore (uioh, first, pan);
3552 if not cancel then set active;
3553 None
3554 method hasaction _ = true
3555 end)
3557 state.text <- E.s;
3558 let modehash = findkeyhash conf "info" in
3559 coe (new listview ~zebra:false ~helpmode:false
3560 ~source ~trusted:true ~modehash)
3561 )) :: m_l
3563 method caption s offset =
3564 m_l <- (s, `empty, offset, Noaction) :: m_l
3566 method caption2 s f offset =
3567 m_l <- (s, `string f, offset, Noaction) :: m_l
3569 method getitemcount = Array.length m_a
3571 method getitem n =
3572 let tostr = function
3573 | `int f -> string_of_int (f ())
3574 | `intws f -> string_with_suffix_of_int (f ())
3575 | `string f -> f ()
3576 | `color f -> color_to_string (f ())
3577 | `bool (btos, f) -> btos (f ())
3578 | `empty -> E.s
3580 let name, t, offset, _ = m_a.(n) in
3581 ((let s = tostr t in
3582 if nonemptystr s
3583 then Printf.sprintf "%s\t%s" name s
3584 else name),
3585 offset)
3587 method exit ~uioh ~cancel ~active ~first ~pan =
3588 let uiohopt =
3589 if not cancel
3590 then (
3591 let uioh =
3592 match m_a.(active) with
3593 | _, _, _, Action f -> f uioh
3594 | _, _, _, Noaction -> uioh
3596 Some uioh
3598 else None
3600 m_active <- active;
3601 m_first <- first;
3602 m_pan <- pan;
3603 uiohopt
3605 method hasaction n =
3606 match m_a.(n) with
3607 | _, _, _, Action _ -> true
3608 | _, _, _, Noaction -> false
3610 initializer m_active <- 1
3611 end)
3613 let rec fillsrc prevmode prevuioh =
3614 let sep () = src#caption E.s 0 in
3615 let colorp name get set =
3616 src#string name
3617 (fun () -> color_to_string (get ()))
3618 (fun v ->
3620 let c = color_of_string v in
3621 set c
3622 with exn ->
3623 state.text <- Printf.sprintf "bad color `%s': %s" v @@ exntos exn
3626 let oldmode = state.mode in
3627 let birdseye = isbirdseye state.mode in
3629 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3631 src#bool "presentation mode"
3632 (fun () -> conf.presentation)
3633 (fun v -> setpresentationmode v);
3635 src#bool "ignore case in searches"
3636 (fun () -> conf.icase)
3637 (fun v -> conf.icase <- v);
3639 src#bool "preload"
3640 (fun () -> conf.preload)
3641 (fun v -> conf.preload <- v);
3643 src#bool "highlight links"
3644 (fun () -> conf.hlinks)
3645 (fun v -> conf.hlinks <- v);
3647 src#bool "under info"
3648 (fun () -> conf.underinfo)
3649 (fun v -> conf.underinfo <- v);
3651 src#bool "persistent bookmarks"
3652 (fun () -> conf.savebmarks)
3653 (fun v -> conf.savebmarks <- v);
3655 src#fitmodel "fit model"
3656 (fun () -> FMTE.to_string conf.fitmodel)
3657 (fun v -> reqlayout conf.angle (FMTE.of_int v));
3659 src#bool "trim margins"
3660 (fun () -> conf.trimmargins)
3661 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3663 src#bool "persistent location"
3664 (fun () -> conf.jumpback)
3665 (fun v -> conf.jumpback <- v);
3667 sep ();
3668 src#int "inter-page space"
3669 (fun () -> conf.interpagespace)
3670 (fun n ->
3671 conf.interpagespace <- n;
3672 docolumns conf.columns;
3673 let pageno, py =
3674 match state.layout with
3675 | [] -> 0, 0
3676 | l :: _ ->
3677 l.pageno, l.pagey
3679 state.maxy <- calcheight ();
3680 let y = getpagey pageno in
3681 gotoy (y + py)
3684 src#int "page bias"
3685 (fun () -> conf.pagebias)
3686 (fun v -> conf.pagebias <- v);
3688 src#int "scroll step"
3689 (fun () -> conf.scrollstep)
3690 (fun n -> conf.scrollstep <- n);
3692 src#int "horizontal scroll step"
3693 (fun () -> conf.hscrollstep)
3694 (fun v -> conf.hscrollstep <- v);
3696 src#int "auto scroll step"
3697 (fun () ->
3698 match state.autoscroll with
3699 | Some step -> step
3700 | _ -> conf.autoscrollstep)
3701 (fun n ->
3702 let n = boundastep state.winh n in
3703 if state.autoscroll <> None
3704 then state.autoscroll <- Some n;
3705 conf.autoscrollstep <- n);
3707 src#int "zoom"
3708 (fun () -> truncate (conf.zoom *. 100.))
3709 (fun v -> setzoom ((float v) /. 100.));
3711 src#int "rotation"
3712 (fun () -> conf.angle)
3713 (fun v -> reqlayout v conf.fitmodel);
3715 src#int "scroll bar width"
3716 (fun () -> conf.scrollbw)
3717 (fun v ->
3718 conf.scrollbw <- v;
3719 reshape state.winw state.winh;
3722 src#int "scroll handle height"
3723 (fun () -> conf.scrollh)
3724 (fun v -> conf.scrollh <- v;);
3726 src#int "thumbnail width"
3727 (fun () -> conf.thumbw)
3728 (fun v ->
3729 conf.thumbw <- min 4096 v;
3730 match oldmode with
3731 | Birdseye beye ->
3732 leavebirdseye beye false;
3733 enterbirdseye ()
3734 | Textentry _
3735 | View
3736 | LinkNav _ -> ()
3739 let mode = state.mode in
3740 src#string "columns"
3741 (fun () ->
3742 match conf.columns with
3743 | Csingle _ -> "1"
3744 | Cmulti (multi, _) -> multicolumns_to_string multi
3745 | Csplit (count, _) -> "-" ^ string_of_int count
3747 (fun v ->
3748 let n, a, b = multicolumns_of_string v in
3749 setcolumns mode n a b);
3751 sep ();
3752 src#caption "Pixmap cache" 0;
3753 src#int_with_suffix "size (advisory)"
3754 (fun () -> conf.memlimit)
3755 (fun v -> conf.memlimit <- v);
3757 src#caption2 "used"
3758 (fun () -> Printf.sprintf "%s bytes, %d tiles"
3759 (string_with_suffix_of_int state.memused)
3760 (Hashtbl.length state.tilemap)) 1;
3762 sep ();
3763 src#caption "Layout" 0;
3764 src#caption2 "Dimension"
3765 (fun () ->
3766 Printf.sprintf "%dx%d (virtual %dx%d)"
3767 state.winw state.winh
3768 state.w state.maxy)
3770 if conf.debug
3771 then
3772 src#caption2 "Position" (fun () ->
3773 Printf.sprintf "%dx%d" state.x state.y
3775 else
3776 src#caption2 "Position" (fun () -> describe_location ()) 1
3779 sep ();
3780 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3781 "Save these parameters as global defaults at exit"
3782 (fun () -> conf.bedefault)
3783 (fun v -> conf.bedefault <- v)
3786 sep ();
3787 let btos b = if b then "@Ulguillemet" else "@Urguillemet" in
3788 src#bool ~offset:0 ~btos "Extended parameters"
3789 (fun () -> !showextended)
3790 (fun v -> showextended := v; fillsrc prevmode prevuioh);
3791 if !showextended
3792 then (
3793 src#bool "checkers"
3794 (fun () -> conf.checkers)
3795 (fun v -> conf.checkers <- v; setcheckers v);
3796 src#bool "update cursor"
3797 (fun () -> conf.updatecurs)
3798 (fun v -> conf.updatecurs <- v);
3799 src#bool "scroll-bar on the left"
3800 (fun () -> conf.leftscroll)
3801 (fun v -> conf.leftscroll <- v);
3802 src#bool "verbose"
3803 (fun () -> conf.verbose)
3804 (fun v -> conf.verbose <- v);
3805 src#bool "invert colors"
3806 (fun () -> conf.invert)
3807 (fun v -> conf.invert <- v);
3808 src#bool "max fit"
3809 (fun () -> conf.maxhfit)
3810 (fun v -> conf.maxhfit <- v);
3811 src#bool "pax mode"
3812 (fun () -> conf.pax != None)
3813 (fun v ->
3814 if v
3815 then conf.pax <- Some (ref (now (), 0, 0))
3816 else conf.pax <- None);
3817 src#string "uri launcher"
3818 (fun () -> conf.urilauncher)
3819 (fun v -> conf.urilauncher <- v);
3820 src#string "path launcher"
3821 (fun () -> conf.pathlauncher)
3822 (fun v -> conf.pathlauncher <- v);
3823 src#string "tile size"
3824 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3825 (fun v ->
3827 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
3828 conf.tilew <- max 64 w;
3829 conf.tileh <- max 64 h;
3830 flushtiles ();
3831 with exn ->
3832 state.text <- Printf.sprintf "bad tile size `%s': %s"
3833 v @@ exntos exn
3835 src#int "texture count"
3836 (fun () -> conf.texcount)
3837 (fun v ->
3838 if realloctexts v
3839 then conf.texcount <- v
3840 else impmsg "failed to set texture count please retry later"
3842 src#int "slice height"
3843 (fun () -> conf.sliceheight)
3844 (fun v ->
3845 conf.sliceheight <- v;
3846 wcmd "sliceh %d" conf.sliceheight;
3848 src#int "anti-aliasing level"
3849 (fun () -> conf.aalevel)
3850 (fun v ->
3851 conf.aalevel <- bound v 0 8;
3852 state.anchor <- getanchor ();
3853 opendoc state.path state.password;
3855 src#string "page scroll scaling factor"
3856 (fun () -> string_of_float conf.pgscale)
3857 (fun v ->
3859 let s = float_of_string v in
3860 conf.pgscale <- s
3861 with exn ->
3862 state.text <- Printf.sprintf
3863 "bad page scroll scaling factor `%s': %s" v @@ exntos exn
3866 src#int "ui font size"
3867 (fun () -> fstate.fontsize)
3868 (fun v -> setfontsize (bound v 5 100));
3869 src#int "hint font size"
3870 (fun () -> conf.hfsize)
3871 (fun v -> conf.hfsize <- bound v 5 100);
3872 colorp "background color"
3873 (fun () -> conf.bgcolor)
3874 (fun v -> conf.bgcolor <- v);
3875 src#bool "crop hack"
3876 (fun () -> conf.crophack)
3877 (fun v -> conf.crophack <- v);
3878 src#string "trim fuzz"
3879 (fun () -> irect_to_string conf.trimfuzz)
3880 (fun v ->
3882 conf.trimfuzz <- irect_of_string v;
3883 if conf.trimmargins
3884 then settrim true conf.trimfuzz;
3885 with exn ->
3886 state.text <- Printf.sprintf "bad irect `%s': %s" v @@ exntos exn
3888 src#string "throttle"
3889 (fun () ->
3890 match conf.maxwait with
3891 | None -> "show place holder if page is not ready"
3892 | Some time ->
3893 if time = infinity
3894 then "wait for page to fully render"
3895 else
3896 "wait " ^ string_of_float time
3897 ^ " seconds before showing placeholder"
3899 (fun v ->
3901 let f = float_of_string v in
3902 if f <= 0.0
3903 then conf.maxwait <- None
3904 else conf.maxwait <- Some f
3905 with exn ->
3906 state.text <- Printf.sprintf "bad time `%s': %s" v @@ exntos exn
3908 src#string "ghyll scroll"
3909 (fun () ->
3910 match conf.ghyllscroll with
3911 | None -> E.s
3912 | Some nab -> ghyllscroll_to_string nab
3914 (fun v ->
3915 try conf.ghyllscroll <- ghyllscroll_of_string v
3916 with
3917 | Failure msg ->
3918 state.text <- Printf.sprintf "bad ghyll `%s': %s" v msg
3919 | exn ->
3920 state.text <- Printf.sprintf "bad ghyll `%s': %s" v @@ exntos exn
3922 src#string "selection command"
3923 (fun () -> conf.selcmd)
3924 (fun v -> conf.selcmd <- v);
3925 src#string "synctex command"
3926 (fun () -> conf.stcmd)
3927 (fun v -> conf.stcmd <- v);
3928 src#string "pax command"
3929 (fun () -> conf.paxcmd)
3930 (fun v -> conf.paxcmd <- v);
3931 src#string "ask password command"
3932 (fun () -> conf.passcmd)
3933 (fun v -> conf.passcmd <- v);
3934 src#string "save path command"
3935 (fun () -> conf.savecmd)
3936 (fun v -> conf.savecmd <- v);
3937 src#colorspace "color space"
3938 (fun () -> CSTE.to_string conf.colorspace)
3939 (fun v ->
3940 conf.colorspace <- CSTE.of_int v;
3941 wcmd "cs %d" v;
3942 load state.layout;
3944 src#paxmark "pax mark method"
3945 (fun () -> MTE.to_string conf.paxmark)
3946 (fun v -> conf.paxmark <- MTE.of_int v);
3947 if pbousable ()
3948 then
3949 src#bool "use PBO"
3950 (fun () -> conf.usepbo)
3951 (fun v -> conf.usepbo <- v);
3952 src#bool "mouse wheel scrolls pages"
3953 (fun () -> conf.wheelbypage)
3954 (fun v -> conf.wheelbypage <- v);
3955 src#bool "open remote links in a new instance"
3956 (fun () -> conf.riani)
3957 (fun v -> conf.riani <- v);
3958 src#bool "edit annotations inline"
3959 (fun () -> conf.annotinline)
3960 (fun v -> conf.annotinline <- v);
3963 sep ();
3964 src#caption "Document" 0;
3965 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
3966 src#caption2 "Pages"
3967 (fun () -> string_of_int state.pagecount) 1;
3968 src#caption2 "Dimensions"
3969 (fun () -> string_of_int (List.length state.pdims)) 1;
3970 if conf.trimmargins
3971 then (
3972 sep ();
3973 src#caption "Trimmed margins" 0;
3974 src#caption2 "Dimensions"
3975 (fun () -> string_of_int (List.length state.pdims)) 1;
3978 sep ();
3979 src#caption "OpenGL" 0;
3980 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
3981 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
3983 sep ();
3984 src#caption "Location" 0;
3985 if nonemptystr state.origin
3986 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
3987 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
3989 src#reset prevmode prevuioh;
3991 fun () ->
3992 state.text <- E.s;
3993 resetmstate ();
3994 let prevmode = state.mode
3995 and prevuioh = state.uioh in
3996 fillsrc prevmode prevuioh;
3997 let source = (src :> lvsource) in
3998 let modehash = findkeyhash conf "info" in
3999 state.uioh <- coe (object (self)
4000 inherit listview ~zebra:false ~helpmode:false
4001 ~source ~trusted:true ~modehash as super
4002 val mutable m_prevmemused = 0
4003 method! infochanged = function
4004 | Memused ->
4005 if m_prevmemused != state.memused
4006 then (
4007 m_prevmemused <- state.memused;
4008 G.postRedisplay "memusedchanged";
4010 | Pdim -> G.postRedisplay "pdimchanged"
4011 | Docinfo -> fillsrc prevmode prevuioh
4013 method! key key mask =
4014 if not (Wsi.withctrl mask)
4015 then
4016 match key with
4017 | @left | @kpleft -> coe (self#updownlevel ~-1)
4018 | @right | @kpright -> coe (self#updownlevel 1)
4019 | _ -> super#key key mask
4020 else super#key key mask
4021 end);
4022 G.postRedisplay "info";
4025 let enterhelpmode =
4026 let source =
4027 (object
4028 inherit lvsourcebase
4029 method getitemcount = Array.length state.help
4030 method getitem n =
4031 let s, l, _ = state.help.(n) in
4032 (s, l)
4034 method exit ~uioh ~cancel ~active ~first ~pan =
4035 let optuioh =
4036 if not cancel
4037 then (
4038 match state.help.(active) with
4039 | _, _, Action f -> Some (f uioh)
4040 | _, _, Noaction -> Some uioh
4042 else None
4044 m_active <- active;
4045 m_first <- first;
4046 m_pan <- pan;
4047 optuioh
4049 method hasaction n =
4050 match state.help.(n) with
4051 | _, _, Action _ -> true
4052 | _, _, Noaction -> false
4054 initializer
4055 m_active <- -1
4056 end)
4057 in fun () ->
4058 let modehash = findkeyhash conf "help" in
4059 resetmstate ();
4060 state.uioh <- coe (new listview
4061 ~zebra:false ~helpmode:true
4062 ~source ~trusted:true ~modehash);
4063 G.postRedisplay "help";
4066 let entermsgsmode =
4067 let msgsource =
4068 (object
4069 inherit lvsourcebase
4070 val mutable m_items = E.a
4072 method getitemcount = 1 + Array.length m_items
4074 method getitem n =
4075 if n = 0
4076 then "[Clear]", 0
4077 else m_items.(n-1), 0
4079 method exit ~uioh ~cancel ~active ~first ~pan =
4080 ignore uioh;
4081 if not cancel
4082 then (
4083 if active = 0
4084 then Buffer.clear state.errmsgs;
4086 m_active <- active;
4087 m_first <- first;
4088 m_pan <- pan;
4089 None
4091 method hasaction n =
4092 n = 0
4094 method reset =
4095 state.newerrmsgs <- false;
4096 let l = Str.split newlinere (Buffer.contents state.errmsgs) in
4097 m_items <- Array.of_list l
4099 initializer
4100 m_active <- 0
4101 end)
4102 in fun () ->
4103 state.text <- E.s;
4104 resetmstate ();
4105 msgsource#reset;
4106 let source = (msgsource :> lvsource) in
4107 let modehash = findkeyhash conf "listview" in
4108 state.uioh <- coe (object
4109 inherit listview ~zebra:false ~helpmode:false
4110 ~source ~trusted:false ~modehash as super
4111 method! display =
4112 if state.newerrmsgs
4113 then msgsource#reset;
4114 super#display
4115 end);
4116 G.postRedisplay "msgs";
4119 let getusertext s =
4120 let editor = getenvwithdef "EDITOR" E.s in
4121 if emptystr editor
4122 then E.s
4123 else
4124 let tmppath = Filename.temp_file "llpp" "note" in
4125 if nonemptystr s
4126 then (
4127 let oc = open_out tmppath in
4128 output_string oc s;
4129 close_out oc;
4131 let execstr = editor ^ " " ^ tmppath in
4132 let s =
4133 match spawn execstr [] with
4134 | (exception exn) ->
4135 impmsg "spawn(%S) failed: %s" execstr @@ exntos exn;
4137 | pid ->
4138 match Unix.waitpid [] pid with
4139 | (exception exn) ->
4140 impmsg "waitpid(%d) failed: %s" pid @@ exntos exn;
4142 | (_pid, status) ->
4143 match status with
4144 | Unix.WEXITED 0 -> filecontents tmppath
4145 | Unix.WEXITED n ->
4146 impmsg "editor process(%s) exited abnormally: %d" execstr n;
4148 | Unix.WSIGNALED n ->
4149 impmsg "editor process(%s) was killed by signal %d" execstr n;
4151 | Unix.WSTOPPED n ->
4152 impmsg "editor(%s) process was stopped by signal %d" execstr n;
4155 match Unix.unlink tmppath with
4156 | (exception exn) ->
4157 impmsg "failed to ulink %S: %s" tmppath @@ exntos exn;
4159 | () -> s
4162 let enterannotmode opaque slinkindex =
4163 let msgsource =
4164 (object
4165 inherit lvsourcebase
4166 val mutable m_text = E.s
4167 val mutable m_items = E.a
4169 method getitemcount = Array.length m_items
4171 method getitem n =
4172 let label, _func = m_items.(n) in
4173 label, 0
4175 method exit ~uioh ~cancel ~active ~first ~pan =
4176 ignore (uioh, first, pan);
4177 if not cancel
4178 then (
4179 let _label, func = m_items.(active) in
4180 func ()
4182 None
4184 method hasaction n = nonemptystr @@ fst m_items.(n)
4186 method reset s =
4187 let rec split accu b i =
4188 let p = b+i in
4189 if p = String.length s
4190 then (String.sub s b (p-b), unit) :: accu
4191 else
4192 if (i > 70 && s.[p] = ' ') || s.[p] = '\r' || s.[p] = '\n'
4193 then
4194 let ss = if i = 0 then E.s else String.sub s b i in
4195 split ((ss, unit)::accu) (p+1) 0
4196 else
4197 split accu b (i+1)
4199 let cleanup () =
4200 wcmd "freepage %s" (~> opaque);
4201 let keys =
4202 Hashtbl.fold (fun key opaque' accu ->
4203 if opaque' = opaque'
4204 then key :: accu else accu) state.pagemap []
4206 List.iter (Hashtbl.remove state.pagemap) keys;
4207 flushtiles ();
4208 gotoy state.y
4210 let dele () =
4211 delannot opaque slinkindex;
4212 cleanup ();
4214 let edit inline () =
4215 let update s =
4216 if emptystr s
4217 then dele ()
4218 else (
4219 modannot opaque slinkindex s;
4220 cleanup ();
4223 if inline
4224 then
4225 let mode = state.mode in
4226 state.mode <-
4227 Textentry (
4228 ("annotation: ", m_text, None, textentry, update, true),
4229 fun _ -> state.mode <- mode);
4230 state.text <- E.s;
4231 enttext ();
4232 else
4233 let s = getusertext m_text in
4234 update s
4236 m_text <- s;
4237 m_items <-
4238 ( "[Copy]", fun () -> selstring m_text)
4239 :: ("[Delete]", dele)
4240 :: ("[Edit]", edit conf.annotinline)
4241 :: (E.s, unit)
4242 :: split [] 0 0 |> List.rev |> Array.of_list
4244 initializer
4245 m_active <- 0
4246 end)
4248 state.text <- E.s;
4249 let s = getannotcontents opaque slinkindex in
4250 resetmstate ();
4251 msgsource#reset s;
4252 let source = (msgsource :> lvsource) in
4253 let modehash = findkeyhash conf "listview" in
4254 state.uioh <- coe (object
4255 inherit listview ~zebra:false ~helpmode:false
4256 ~source ~trusted:false ~modehash
4257 end);
4258 G.postRedisplay "enterannotmode";
4261 let gotounder under =
4262 let getpath filename =
4263 let path =
4264 if nonemptystr filename
4265 then
4266 if Filename.is_relative filename
4267 then
4268 let dir = Filename.dirname state.path in
4269 let dir =
4270 if Filename.is_implicit dir
4271 then Filename.concat (Sys.getcwd ()) dir
4272 else dir
4274 Filename.concat dir filename
4275 else filename
4276 else E.s
4278 if Sys.file_exists path
4279 then path
4280 else E.s
4282 match under with
4283 | Ulinkgoto (pageno, top) ->
4284 if pageno >= 0
4285 then (
4286 addnav ();
4287 gotopage1 pageno top;
4290 | Ulinkuri s -> gotouri s
4292 | Uremote (filename, pageno) ->
4293 let path = getpath filename in
4294 if nonemptystr path
4295 then (
4296 if conf.riani
4297 then
4298 let command = Printf.sprintf "%s -page %d %S" !selfexec pageno path in
4299 match spawn command [] with
4300 | _pid -> ()
4301 | (exception exn) ->
4302 dolog "failed to execute `%s': %s" command @@ exntos exn
4303 else
4304 let anchor = getanchor () in
4305 let ranchor = state.path, state.password, anchor, state.origin in
4306 state.origin <- E.s;
4307 state.anchor <- (pageno, 0.0, 0.0);
4308 state.ranchors <- ranchor :: state.ranchors;
4309 opendoc path E.s;
4311 else impmsg "cannot find %s" filename
4313 | Uremotedest (filename, destname) ->
4314 let path = getpath filename in
4315 if nonemptystr path
4316 then (
4317 if conf.riani
4318 then
4319 let command = !selfexec ^ " " ^ path ^ " -dest " ^ destname in
4320 match spawn command [] with
4321 | (exception exn) ->
4322 dolog "failed to execute `%s': %s" command @@ exntos exn
4323 | _pid -> ()
4324 else
4325 let anchor = getanchor () in
4326 let ranchor = state.path, state.password, anchor, state.origin in
4327 state.origin <- E.s;
4328 state.nameddest <- destname;
4329 state.ranchors <- ranchor :: state.ranchors;
4330 opendoc path E.s;
4332 else impmsg "cannot find %s" filename
4334 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4335 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
4338 let gotooutline (_, _, kind) =
4339 match kind with
4340 | Onone -> ()
4341 | Oanchor anchor ->
4342 let (pageno, y, _) = anchor in
4343 let y = getanchory
4344 (if conf.presentation then (pageno, y, 1.0) else anchor)
4346 addnav ();
4347 gotoghyll y
4348 | Ouri uri -> gotounder (Ulinkuri uri)
4349 | Olaunch cmd -> gotounder (Ulaunch cmd)
4350 | Oremote remote -> gotounder (Uremote remote)
4351 | Ohistory hist -> gotohist hist
4352 | Oremotedest remotedest -> gotounder (Uremotedest remotedest)
4355 let outlinesource fetchoutlines =
4356 (object (self)
4357 inherit lvsourcebase
4358 val mutable m_items = E.a
4359 val mutable m_minfo = E.a
4360 val mutable m_orig_items = E.a
4361 val mutable m_orig_minfo = E.a
4362 val mutable m_narrow_patterns = []
4363 val mutable m_gen = -1
4365 method getitemcount = Array.length m_items
4367 method getitem n =
4368 let s, n, _ = m_items.(n) in
4369 (s, n)
4371 method exit ~uioh ~cancel ~active ~first ~pan =
4372 ignore (uioh, first);
4373 let items, minfo =
4374 if m_narrow_patterns = []
4375 then m_orig_items, m_orig_minfo
4376 else m_items, m_minfo
4378 m_pan <- pan;
4379 if not cancel
4380 then (
4381 m_items <- items;
4382 m_minfo <- minfo;
4383 gotooutline m_items.(active);
4385 else (
4386 m_items <- items;
4387 m_minfo <- minfo;
4389 None
4391 method hasaction _ = true
4393 method greetmsg =
4394 if Array.length m_items != Array.length m_orig_items
4395 then
4396 let s =
4397 match m_narrow_patterns with
4398 | one :: [] -> one
4399 | many -> String.concat "@Uellipsis" (List.rev many)
4401 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
4402 else E.s
4404 method statestr =
4405 match m_narrow_patterns with
4406 | [] -> E.s
4407 | one :: [] -> one
4408 | head :: _ -> "@Uellipsis" ^ head
4410 method narrow pattern =
4411 match Str.regexp_case_fold pattern with
4412 | (exception _) -> ()
4413 | re ->
4414 let rec loop accu minfo n =
4415 if n = -1
4416 then (
4417 m_items <- Array.of_list accu;
4418 m_minfo <- Array.of_list minfo;
4420 else
4421 let (s, _, _) as o = m_items.(n) in
4422 let accu, minfo =
4423 match Str.search_forward re s 0 with
4424 | (exception Not_found) -> accu, minfo
4425 | first -> o :: accu, (first, Str.match_end ()) :: minfo
4427 loop accu minfo (n-1)
4429 loop [] [] (Array.length m_items - 1)
4431 method! getminfo = m_minfo
4433 method denarrow =
4434 m_orig_items <- fetchoutlines ();
4435 m_minfo <- m_orig_minfo;
4436 m_items <- m_orig_items
4438 method add_narrow_pattern pattern =
4439 m_narrow_patterns <- pattern :: m_narrow_patterns
4441 method del_narrow_pattern =
4442 match m_narrow_patterns with
4443 | _ :: rest -> m_narrow_patterns <- rest
4444 | [] -> ()
4446 method renarrow =
4447 self#denarrow;
4448 match m_narrow_patterns with
4449 | pattern :: [] -> self#narrow pattern; pattern
4450 | list ->
4451 List.fold_left (fun accu pattern ->
4452 self#narrow pattern;
4453 pattern ^ "@Uellipsis" ^ accu) E.s list
4455 method calcactive anchor =
4456 let rely = getanchory anchor in
4457 let rec loop n best bestd =
4458 if n = Array.length m_items
4459 then best
4460 else
4461 let _, _, kind = m_items.(n) in
4462 match kind with
4463 | Oanchor anchor ->
4464 let orely = getanchory anchor in
4465 let d = abs (orely - rely) in
4466 if d < bestd
4467 then loop (n+1) n d
4468 else loop (n+1) best bestd
4469 | Onone | Oremote _ | Olaunch _
4470 | Oremotedest _ | Ouri _ | Ohistory _ ->
4471 loop (n+1) best bestd
4473 loop 0 ~-1 max_int
4475 method reset anchor items =
4476 if state.gen != m_gen
4477 then (
4478 m_orig_items <- items;
4479 m_items <- items;
4480 m_narrow_patterns <- [];
4481 m_minfo <- E.a;
4482 m_orig_minfo <- E.a;
4483 m_gen <- state.gen;
4485 else (
4486 if items != m_orig_items
4487 then (
4488 m_orig_items <- items;
4489 if m_narrow_patterns == []
4490 then m_items <- items;
4493 let active = self#calcactive anchor in
4494 m_active <- active;
4495 m_first <- firstof m_first active
4496 end)
4499 let enteroutlinemode, enterbookmarkmode, enterhistmode =
4500 let mkselector sourcetype =
4501 let fetchoutlines () =
4502 match sourcetype with
4503 | `bookmarks -> Array.of_list state.bookmarks
4504 | `outlines -> state.outlines
4505 | `history -> genhistoutlines ()
4507 let source = outlinesource fetchoutlines in
4508 fun errmsg ->
4509 let outlines = fetchoutlines () in
4510 if Array.length outlines = 0
4511 then (
4512 showtext ' ' errmsg;
4514 else (
4515 resetmstate ();
4516 Wsi.setcursor Wsi.CURSOR_INHERIT;
4517 let anchor = getanchor () in
4518 source#reset anchor outlines;
4519 state.text <- source#greetmsg;
4520 state.uioh <-
4521 coe (new outlinelistview ~zebra:(sourcetype=`history) ~source);
4522 G.postRedisplay "enter selector";
4525 let mkenter sourcetype errmsg =
4526 let enter = mkselector sourcetype in
4527 fun () -> enter errmsg
4529 (**)mkenter `outlines "document has no outline"
4530 , mkenter `bookmarks "document has no bookmarks (yet)"
4531 , mkenter `history "history is empty"
4534 let quickbookmark ?title () =
4535 match state.layout with
4536 | [] -> ()
4537 | l :: _ ->
4538 let title =
4539 match title with
4540 | None ->
4541 let tm = Unix.localtime (now ()) in
4542 Printf.sprintf
4543 "Quick (page %d) (bookmarked at %02d/%02d/%d %02d:%02d)"
4544 (l.pageno+1)
4545 tm.Unix.tm_mday
4546 (tm.Unix.tm_mon+1)
4547 (tm.Unix.tm_year + 1900)
4548 tm.Unix.tm_hour
4549 tm.Unix.tm_min
4550 | Some title -> title
4552 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4555 let setautoscrollspeed step goingdown =
4556 let incr = max 1 ((abs step) / 2) in
4557 let incr = if goingdown then incr else -incr in
4558 let astep = boundastep state.winh (step + incr) in
4559 state.autoscroll <- Some astep;
4562 let canpan () =
4563 match conf.columns with
4564 | Csplit _ -> true
4565 | Csingle _ | Cmulti _ -> state.x != 0 || conf.zoom > 1.0
4568 let panbound x = bound x (-state.w) (wadjsb () + state.winw);;
4570 let existsinrow pageno (columns, coverA, coverB) p =
4571 let last = ((pageno - coverA) mod columns) + columns in
4572 let rec any = function
4573 | [] -> false
4574 | l :: rest ->
4575 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4576 then p l
4577 else (
4578 if not (p l)
4579 then (if l.pageno = last then false else any rest)
4580 else true
4583 any state.layout
4586 let nextpage () =
4587 match state.layout with
4588 | [] ->
4589 let pageno = page_of_y state.y in
4590 gotoghyll (getpagey (pageno+1))
4591 | l :: rest ->
4592 match conf.columns with
4593 | Csingle _ ->
4594 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4595 then
4596 let y = clamp (pgscale state.winh) in
4597 gotoghyll y
4598 else
4599 let pageno = min (l.pageno+1) (state.pagecount-1) in
4600 gotoghyll (getpagey pageno)
4601 | Cmulti ((c, _, _) as cl, _) ->
4602 if conf.presentation
4603 && (existsinrow l.pageno cl
4604 (fun l -> l.pageh > l.pagey + l.pagevh))
4605 then
4606 let y = clamp (pgscale state.winh) in
4607 gotoghyll y
4608 else
4609 let pageno = min (l.pageno+c) (state.pagecount-1) in
4610 gotoghyll (getpagey pageno)
4611 | Csplit (n, _) ->
4612 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4613 then
4614 let pagey, pageh = getpageyh l.pageno in
4615 let pagey = pagey + pageh * l.pagecol in
4616 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4617 gotoghyll (pagey + pageh + ips)
4620 let prevpage () =
4621 match state.layout with
4622 | [] ->
4623 let pageno = page_of_y state.y in
4624 gotoghyll (getpagey (pageno-1))
4625 | l :: _ ->
4626 match conf.columns with
4627 | Csingle _ ->
4628 if conf.presentation && l.pagey != 0
4629 then
4630 gotoghyll (clamp (pgscale ~-(state.winh)))
4631 else
4632 let pageno = max 0 (l.pageno-1) in
4633 gotoghyll (getpagey pageno)
4634 | Cmulti ((c, _, coverB) as cl, _) ->
4635 if conf.presentation &&
4636 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4637 then
4638 gotoghyll (clamp (pgscale ~-(state.winh)))
4639 else
4640 let decr =
4641 if l.pageno = state.pagecount - coverB
4642 then 1
4643 else c
4645 let pageno = max 0 (l.pageno-decr) in
4646 gotoghyll (getpagey pageno)
4647 | Csplit (n, _) ->
4648 let y =
4649 if l.pagecol = 0
4650 then
4651 if l.pageno = 0
4652 then l.pagey
4653 else
4654 let pageno = max 0 (l.pageno-1) in
4655 let pagey, pageh = getpageyh pageno in
4656 pagey + (n-1)*pageh
4657 else
4658 let pagey, pageh = getpageyh l.pageno in
4659 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4661 gotoghyll y
4664 let save () =
4665 if emptystr conf.savecmd
4666 then error "don't know where to save modified document"
4667 else
4668 let savecmd = Str.global_replace percentsre state.path conf.savecmd in
4669 let path =
4670 getcmdoutput
4671 (fun s -> error "failed to obtain path to the saved copy: %s" s)
4672 savecmd
4674 if nonemptystr path
4675 then
4676 let tmp = path ^ ".tmp" in
4677 savedoc tmp;
4678 Unix.rename tmp path;
4681 let viewkeyboard key mask =
4682 let enttext te =
4683 let mode = state.mode in
4684 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4685 state.text <- E.s;
4686 enttext ();
4687 G.postRedisplay "view:enttext"
4689 let ctrl = Wsi.withctrl mask in
4690 let key =
4691 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
4693 match key with
4694 | @Q -> exit 0
4696 | @W ->
4697 if hasunsavedchanges ()
4698 then save ()
4700 | @insert ->
4701 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4702 then (
4703 state.mode <- LinkNav (Ltgendir 0);
4704 gotoy state.y;
4706 else impmsg "keyboard link navigation does not work under rotation"
4708 | @escape | @q ->
4709 begin match state.mstate with
4710 | Mzoomrect _ ->
4711 resetmstate ();
4712 G.postRedisplay "kill rect";
4713 | Msel _
4714 | Mpan _
4715 | Mscrolly | Mscrollx
4716 | Mzoom _
4717 | Mnone ->
4718 begin match state.mode with
4719 | LinkNav _ ->
4720 state.mode <- View;
4721 G.postRedisplay "esc leave linknav"
4722 | Birdseye _
4723 | Textentry _
4724 | View ->
4725 match state.ranchors with
4726 | [] -> raise Quit
4727 | (path, password, anchor, origin) :: rest ->
4728 state.ranchors <- rest;
4729 state.anchor <- anchor;
4730 state.origin <- origin;
4731 state.nameddest <- E.s;
4732 opendoc path password
4733 end;
4734 end;
4736 | @backspace ->
4737 gotoghyll (getnav ~-1)
4739 | @o ->
4740 enteroutlinemode ()
4742 | @H ->
4743 enterhistmode ()
4745 | @u ->
4746 state.rects <- [];
4747 state.text <- E.s;
4748 Hashtbl.iter (fun _ opaque ->
4749 clearmark opaque;
4750 Hashtbl.clear state.prects) state.pagemap;
4751 G.postRedisplay "dehighlight";
4753 | @slash | @question ->
4754 let ondone isforw s =
4755 cbput state.hists.pat s;
4756 state.searchpattern <- s;
4757 search s isforw
4759 let s = String.make 1 (Char.chr key) in
4760 enttext (s, E.s, Some (onhist state.hists.pat),
4761 textentry, ondone (key = @slash), true)
4763 | @plus | @kpplus | @equals when ctrl ->
4764 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4765 setzoom (conf.zoom +. incr)
4767 | @plus | @kpplus ->
4768 let ondone s =
4769 let n =
4770 try int_of_string s with exc ->
4771 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exc;
4772 max_int
4774 if n != max_int
4775 then (
4776 conf.pagebias <- n;
4777 state.text <- "page bias is now " ^ string_of_int n;
4780 enttext ("page bias: ", E.s, None, intentry, ondone, true)
4782 | @minus | @kpminus when ctrl ->
4783 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4784 setzoom (max 0.01 (conf.zoom -. decr))
4786 | @minus | @kpminus ->
4787 let ondone msg = state.text <- msg in
4788 enttext (
4789 "option [acfhilpstvxACFPRSZTISM]: ", E.s, None,
4790 optentry state.mode, ondone, true
4793 | @0 when ctrl ->
4794 if conf.zoom = 1.0
4795 then (
4796 state.x <- 0;
4797 gotoy state.y
4799 else setzoom 1.0
4801 | (@1 | @2) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
4802 let cols =
4803 match conf.columns with
4804 | Csingle _ | Cmulti _ -> 1
4805 | Csplit (n, _) -> n
4807 let h = state.winh -
4808 conf.interpagespace lsl (if conf.presentation then 1 else 0)
4810 let zoom = zoomforh state.winw h (vscrollw ()) cols in
4811 if zoom > 0.0 && (key = @2 || zoom < 1.0)
4812 then setzoom zoom
4814 | @3 when ctrl ->
4815 let fm =
4816 match conf.fitmodel with
4817 | FitWidth -> FitProportional
4818 | FitProportional -> FitPage
4819 | FitPage -> FitWidth
4821 state.text <- "fit model: " ^ FMTE.to_string fm;
4822 reqlayout conf.angle fm
4824 | @4 when ctrl -> (* ctrl-4 *)
4825 let zoom = getmaxw () /. float state.winw in
4826 if zoom > 0.0 then setzoom zoom
4828 | @F9 ->
4829 togglebirdseye ()
4831 | @9 when ctrl ->
4832 togglebirdseye ()
4834 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4835 when not ctrl -> (* 0..9 *)
4836 let ondone s =
4837 let n =
4838 try int_of_string s with exc ->
4839 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exc;
4842 if n >= 0
4843 then (
4844 addnav ();
4845 cbput state.hists.pag (string_of_int n);
4846 gotopage1 (n + conf.pagebias - 1) 0;
4849 let pageentry text key =
4850 match Char.unsafe_chr key with
4851 | 'g' -> TEdone text
4852 | _ -> intentry text key
4854 let text = String.make 1 (Char.chr key) in
4855 enttext (":", text, Some (onhist state.hists.pag),
4856 pageentry, ondone, true)
4858 | @b ->
4859 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
4860 reshape state.winw state.winh;
4862 | @B ->
4863 state.bzoom <- not state.bzoom;
4864 state.rects <- [];
4865 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
4867 | @l ->
4868 conf.hlinks <- not conf.hlinks;
4869 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4870 G.postRedisplay "toggle highlightlinks";
4872 | @F ->
4873 if conf.angle mod 360 = 0
4874 then (
4875 state.glinks <- true;
4876 let mode = state.mode in
4877 state.mode <-
4878 Textentry (
4879 (":", E.s, None, linknentry, linknact gotounder, false),
4880 (fun _ ->
4881 state.glinks <- false;
4882 state.mode <- mode)
4884 state.text <- E.s;
4885 G.postRedisplay "view:linkent(F)"
4887 else impmsg "hint mode does not work under rotation"
4889 | @y ->
4890 state.glinks <- true;
4891 let mode = state.mode in
4892 state.mode <- Textentry (
4894 ":", E.s, None, linknentry, linknact (fun under ->
4895 selstring (undertext under);
4896 ), false
4898 fun _ ->
4899 state.glinks <- false;
4900 state.mode <- mode
4902 state.text <- E.s;
4903 G.postRedisplay "view:linkent"
4905 | @a ->
4906 begin match state.autoscroll with
4907 | Some step ->
4908 conf.autoscrollstep <- step;
4909 state.autoscroll <- None
4910 | None ->
4911 if conf.autoscrollstep = 0
4912 then state.autoscroll <- Some 1
4913 else state.autoscroll <- Some conf.autoscrollstep
4916 | @p when ctrl ->
4917 launchpath () (* XXX where do error messages go? *)
4919 | @P ->
4920 setpresentationmode (not conf.presentation);
4921 showtext ' ' ("presentation mode " ^
4922 if conf.presentation then "on" else "off");
4924 | @f ->
4925 if List.mem Wsi.Fullscreen state.winstate
4926 then Wsi.reshape conf.cwinw conf.cwinh
4927 else Wsi.fullscreen ()
4929 | @p | @N ->
4930 search state.searchpattern false
4932 | @n | @F3 ->
4933 search state.searchpattern true
4935 | @t ->
4936 begin match state.layout with
4937 | [] -> ()
4938 | l :: _ ->
4939 gotoghyll (getpagey l.pageno)
4942 | @space ->
4943 nextpage ()
4945 | @delete | @kpdelete -> (* delete *)
4946 prevpage ()
4948 | @equals ->
4949 showtext ' ' (describe_location ());
4951 | @w ->
4952 begin match state.layout with
4953 | [] -> ()
4954 | l :: _ ->
4955 Wsi.reshape (l.pagew + vscrollw ()) l.pageh;
4956 G.postRedisplay "w"
4959 | @apos ->
4960 enterbookmarkmode ()
4962 | @h | @F1 ->
4963 enterhelpmode ()
4965 | @i ->
4966 enterinfomode ()
4968 | @e when Buffer.length state.errmsgs > 0 ->
4969 entermsgsmode ()
4971 | @m ->
4972 let ondone s =
4973 match state.layout with
4974 | l :: _ ->
4975 if nonemptystr s
4976 then
4977 state.bookmarks <-
4978 (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4979 | _ -> ()
4981 enttext ("bookmark: ", E.s, None, textentry, ondone, true)
4983 | @tilde ->
4984 quickbookmark ();
4985 showtext ' ' "Quick bookmark added";
4987 | @z ->
4988 begin match state.layout with
4989 | l :: _ ->
4990 let rect = getpdimrect l.pagedimno in
4991 let w, h =
4992 if conf.crophack
4993 then
4994 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4995 truncate (1.2 *. (rect.(3) -. rect.(0))))
4996 else
4997 (truncate (rect.(1) -. rect.(0)),
4998 truncate (rect.(3) -. rect.(0)))
5000 let w = truncate ((float w)*.conf.zoom)
5001 and h = truncate ((float h)*.conf.zoom) in
5002 if w != 0 && h != 0
5003 then (
5004 state.anchor <- getanchor ();
5005 Wsi.reshape (w + vscrollw ()) (h + conf.interpagespace)
5007 G.postRedisplay "z";
5009 | [] -> ()
5012 | @x -> state.roam ()
5014 | @Lt | @Gt ->
5015 reqlayout (conf.angle +
5016 (if key = @Gt then 30 else -30)) conf.fitmodel
5018 | @Lb | @Rb ->
5019 conf.colorscale <-
5020 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5022 G.postRedisplay "brightness";
5024 | @c when state.mode = View ->
5025 if Wsi.withalt mask
5026 then (
5027 if conf.zoom > 1.0
5028 then
5029 let m = (wadjsb () + state.winw - state.w) / 2 in
5030 state.x <- m;
5031 gotoy_and_clear_text state.y
5033 else
5034 let (c, a, b), z =
5035 match state.prevcolumns with
5036 | None -> (1, 0, 0), 1.0
5037 | Some (columns, z) ->
5038 let cab =
5039 match columns with
5040 | Csplit (c, _) -> -c, 0, 0
5041 | Cmulti ((c, a, b), _) -> c, a, b
5042 | Csingle _ -> 1, 0, 0
5044 cab, z
5046 setcolumns View c a b;
5047 setzoom z
5049 | @down | @up when ctrl && Wsi.withshift mask ->
5050 let zoom, x = state.prevzoom in
5051 setzoom zoom;
5052 state.x <- x;
5054 | @k | @up | @kpup ->
5055 begin match state.autoscroll with
5056 | None ->
5057 begin match state.mode with
5058 | Birdseye beye -> upbirdseye 1 beye
5059 | Textentry _
5060 | View
5061 | LinkNav _ ->
5062 if ctrl
5063 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5064 else (
5065 if not (Wsi.withshift mask) && conf.presentation
5066 then prevpage ()
5067 else gotoghyll1 true (clamp (-conf.scrollstep))
5070 | Some n ->
5071 setautoscrollspeed n false
5074 | @j | @down | @kpdown ->
5075 begin match state.autoscroll with
5076 | None ->
5077 begin match state.mode with
5078 | Birdseye beye -> downbirdseye 1 beye
5079 | Textentry _
5080 | View
5081 | LinkNav _ ->
5082 if ctrl
5083 then gotoy_and_clear_text (clamp (state.winh/2))
5084 else (
5085 if not (Wsi.withshift mask) && conf.presentation
5086 then nextpage ()
5087 else gotoghyll1 true (clamp (conf.scrollstep))
5090 | Some n ->
5091 setautoscrollspeed n true
5094 | @left | @right | @kpleft | @kpright when not (Wsi.withalt mask) ->
5095 if canpan ()
5096 then
5097 let dx =
5098 if ctrl
5099 then state.winw / 2
5100 else conf.hscrollstep
5102 let dx = if key = @left || key = @kpleft then dx else -dx in
5103 state.x <- panbound (state.x + dx);
5104 gotoy_and_clear_text state.y
5105 else (
5106 state.text <- E.s;
5107 G.postRedisplay "left/right"
5110 | @prior | @kpprior ->
5111 let y =
5112 if ctrl
5113 then
5114 match state.layout with
5115 | [] -> state.y
5116 | l :: _ -> state.y - l.pagey
5117 else
5118 clamp (pgscale (-state.winh))
5120 gotoghyll y
5122 | @next | @kpnext ->
5123 let y =
5124 if ctrl
5125 then
5126 match List.rev state.layout with
5127 | [] -> state.y
5128 | l :: _ -> getpagey l.pageno
5129 else
5130 clamp (pgscale state.winh)
5132 gotoghyll y
5134 | @g | @home | @kphome ->
5135 addnav ();
5136 gotoghyll 0
5137 | @G | @jend | @kpend ->
5138 addnav ();
5139 gotoghyll (clamp state.maxy)
5141 | @right | @kpright when Wsi.withalt mask ->
5142 gotoghyll (getnav 1)
5143 | @left | @kpleft when Wsi.withalt mask ->
5144 gotoghyll (getnav ~-1)
5146 | @r ->
5147 reload ()
5149 | @v when conf.debug ->
5150 state.rects <- [];
5151 List.iter (fun l ->
5152 match getopaque l.pageno with
5153 | None -> ()
5154 | Some opaque ->
5155 let x0, y0, x1, y1 = pagebbox opaque in
5156 let a,b = float x0, float y0 in
5157 let c,d = float x1, float y0 in
5158 let e,f = float x1, float y1 in
5159 let h,j = float x0, float y1 in
5160 let rect = (a,b,c,d,e,f,h,j) in
5161 debugrect rect;
5162 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
5163 state.rects <- (l.pageno, color, rect) :: state.rects;
5164 ) state.layout;
5165 G.postRedisplay "v";
5167 | @pipe ->
5168 let mode = state.mode in
5169 let cmd = ref E.s in
5170 let onleave = function
5171 | Cancel -> state.mode <- mode
5172 | Confirm ->
5173 List.iter (fun l ->
5174 match getopaque l.pageno with
5175 | Some opaque -> pipesel opaque !cmd
5176 | None -> ()) state.layout;
5177 state.mode <- mode
5179 let ondone s =
5180 cbput state.hists.sel s;
5181 cmd := s
5183 let te =
5184 "| ", !cmd, Some (onhist state.hists.sel), textentry, ondone, true
5186 G.postRedisplay "|";
5187 state.mode <- Textentry (te, onleave);
5189 | _ ->
5190 vlog "huh? %s" (Wsi.keyname key)
5193 let linknavkeyboard key mask linknav =
5194 let getpage pageno =
5195 let rec loop = function
5196 | [] -> None
5197 | l :: _ when l.pageno = pageno -> Some l
5198 | _ :: rest -> loop rest
5199 in loop state.layout
5201 let doexact (pageno, n) =
5202 match getopaque pageno, getpage pageno with
5203 | Some opaque, Some l ->
5204 if key = @enter || key = @kpenter
5205 then
5206 let under = getlink opaque n in
5207 G.postRedisplay "link gotounder";
5208 gotounder under;
5209 state.mode <- View;
5210 else
5211 let opt, dir =
5212 match key with
5213 | @home ->
5214 Some (findlink opaque LDfirst), -1
5216 | @jend ->
5217 Some (findlink opaque LDlast), 1
5219 | @left ->
5220 Some (findlink opaque (LDleft n)), -1
5222 | @right ->
5223 Some (findlink opaque (LDright n)), 1
5225 | @up ->
5226 Some (findlink opaque (LDup n)), -1
5228 | @down ->
5229 Some (findlink opaque (LDdown n)), 1
5231 | _ -> None, 0
5233 let pwl l dir =
5234 begin match findpwl l.pageno dir with
5235 | Pwlnotfound -> ()
5236 | Pwl pageno ->
5237 let notfound dir =
5238 state.mode <- LinkNav (Ltgendir dir);
5239 let y, h = getpageyh pageno in
5240 let y =
5241 if dir < 0
5242 then y + h - state.winh
5243 else y
5245 gotoy y
5247 begin match getopaque pageno, getpage pageno with
5248 | Some opaque, Some _ ->
5249 let link =
5250 let ld = if dir > 0 then LDfirst else LDlast in
5251 findlink opaque ld
5253 begin match link with
5254 | Lfound m ->
5255 showlinktype (getlink opaque m);
5256 state.mode <- LinkNav (Ltexact (pageno, m));
5257 G.postRedisplay "linknav jpage";
5258 | Lnotfound -> notfound dir
5259 end;
5260 | _ -> notfound dir
5261 end;
5262 end;
5264 begin match opt with
5265 | Some Lnotfound -> pwl l dir;
5266 | Some (Lfound m) ->
5267 if m = n
5268 then pwl l dir
5269 else (
5270 let _, y0, _, y1 = getlinkrect opaque m in
5271 if y0 < l.pagey
5272 then gotopage1 l.pageno y0
5273 else (
5274 let d = fstate.fontsize + 1 in
5275 if y1 - l.pagey > l.pagevh - d
5276 then gotopage1 l.pageno (y1 - state.winh - hscrollh () + d)
5277 else G.postRedisplay "linknav";
5279 showlinktype (getlink opaque m);
5280 state.mode <- LinkNav (Ltexact (l.pageno, m));
5283 | None -> viewkeyboard key mask
5284 end;
5285 | _ -> viewkeyboard key mask
5287 if key = @insert
5288 then (
5289 state.mode <- View;
5290 G.postRedisplay "leave linknav"
5292 else
5293 match linknav with
5294 | Ltgendir _ | Ltnotready _ -> viewkeyboard key mask
5295 | Ltexact exact -> doexact exact
5298 let keyboard key mask =
5299 if (key = @g && Wsi.withctrl mask) && not (istextentry state.mode)
5300 then wcmd "interrupt"
5301 else state.uioh <- state.uioh#key key mask
5304 let birdseyekeyboard key mask
5305 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5306 let incr =
5307 match conf.columns with
5308 | Csingle _ -> 1
5309 | Cmulti ((c, _, _), _) -> c
5310 | Csplit _ -> failwith "bird's eye split mode"
5312 let pgh layout = List.fold_left
5313 (fun m l -> max l.pageh m) state.winh layout in
5314 match key with
5315 | @l when Wsi.withctrl mask ->
5316 let y, h = getpageyh pageno in
5317 let top = (state.winh - h) / 2 in
5318 gotoy (max 0 (y - top))
5319 | @enter | @kpenter -> leavebirdseye beye false
5320 | @escape -> leavebirdseye beye true
5321 | @up -> upbirdseye incr beye
5322 | @down -> downbirdseye incr beye
5323 | @left -> upbirdseye 1 beye
5324 | @right -> downbirdseye 1 beye
5326 | @prior ->
5327 begin match state.layout with
5328 | l :: _ ->
5329 if l.pagey != 0
5330 then (
5331 state.mode <- Birdseye (
5332 oconf, leftx, l.pageno, hooverpageno, anchor
5334 gotopage1 l.pageno 0;
5336 else (
5337 let layout = layout state.x (state.y-state.winh)
5338 state.winw
5339 (pgh state.layout) in
5340 match layout with
5341 | [] -> gotoy (clamp (-state.winh))
5342 | l :: _ ->
5343 state.mode <- Birdseye (
5344 oconf, leftx, l.pageno, hooverpageno, anchor
5346 gotopage1 l.pageno 0
5349 | [] -> gotoy (clamp (-state.winh))
5350 end;
5352 | @next ->
5353 begin match List.rev state.layout with
5354 | l :: _ ->
5355 let layout = layout state.x
5356 (state.y + (pgh state.layout))
5357 state.winw state.winh in
5358 begin match layout with
5359 | [] ->
5360 let incr = l.pageh - l.pagevh in
5361 if incr = 0
5362 then (
5363 state.mode <-
5364 Birdseye (
5365 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5367 G.postRedisplay "birdseye pagedown";
5369 else gotoy (clamp (incr + conf.interpagespace*2));
5371 | l :: _ ->
5372 state.mode <-
5373 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5374 gotopage1 l.pageno 0;
5377 | [] -> gotoy (clamp state.winh)
5378 end;
5380 | @home ->
5381 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5382 gotopage1 0 0
5384 | @jend ->
5385 let pageno = state.pagecount - 1 in
5386 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5387 if not (pagevisible state.layout pageno)
5388 then
5389 let h =
5390 match List.rev state.pdims with
5391 | [] -> state.winh
5392 | (_, _, h, _) :: _ -> h
5394 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5395 else G.postRedisplay "birdseye end";
5397 | _ -> viewkeyboard key mask
5400 let drawpage l =
5401 let color =
5402 match state.mode with
5403 | Textentry _ -> scalecolor 0.4
5404 | LinkNav _
5405 | View -> scalecolor 1.0
5406 | Birdseye (_, _, pageno, hooverpageno, _) ->
5407 if l.pageno = hooverpageno
5408 then scalecolor 0.9
5409 else (
5410 if l.pageno = pageno
5411 then (
5412 let c = scalecolor 1.0 in
5413 GlDraw.color c;
5414 GlDraw.line_width 3.0;
5415 let dispx = xadjsb () + l.pagedispx in
5416 linerect
5417 (float (dispx-1)) (float (l.pagedispy-1))
5418 (float (dispx+l.pagevw+1))
5419 (float (l.pagedispy+l.pagevh+1))
5421 GlDraw.line_width 1.0;
5424 else scalecolor 0.8
5427 drawtiles l color;
5430 let postdrawpage l linkindexbase =
5431 match getopaque l.pageno with
5432 | Some opaque ->
5433 if tileready l l.pagex l.pagey
5434 then
5435 let x = l.pagedispx - l.pagex + xadjsb ()
5436 and y = l.pagedispy - l.pagey in
5437 let hlmask =
5438 match conf.columns with
5439 | Csingle _ | Cmulti _ ->
5440 (if conf.hlinks then 1 else 0)
5441 + (if state.glinks
5442 && not (isbirdseye state.mode) then 2 else 0)
5443 | Csplit _ -> 0
5445 let s =
5446 match state.mode with
5447 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5448 | Textentry _
5449 | Birdseye _
5450 | View
5451 | LinkNav _ -> E.s
5453 Hashtbl.find_all state.prects l.pageno |>
5454 List.iter (fun vals -> drawprect opaque x y vals);
5455 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5456 else 0
5457 | _ -> 0
5460 let scrollindicator () =
5461 let sbw, ph, sh = state.uioh#scrollph in
5462 let sbh, pw, sw = state.uioh#scrollpw in
5464 let x0,x1,hx0 =
5465 if conf.leftscroll
5466 then (0, sbw, sbw)
5467 else ((state.winw - sbw), state.winw, 0)
5470 GlDraw.color (0.64, 0.64, 0.64);
5471 filledrect (float x0) 0. (float x1) (float state.winh);
5472 filledrect
5473 (float hx0) (float (state.winh - sbh))
5474 (float (hx0 + wadjsb () + state.winw)) (float state.winh)
5476 GlDraw.color (0.0, 0.0, 0.0);
5478 filledrect (float x0) ph (float x1) (ph +. sh);
5479 let pw = pw +. float hx0 in
5480 filledrect pw (float (state.winh - sbh)) (pw +. sw) (float state.winh);
5483 let showsel () =
5484 match state.mstate with
5485 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5488 | Msel ((x0, y0), (x1, y1)) ->
5489 let identify opaque l px py = Some (opaque, l.pageno, px, py) in
5490 let o0,n0,px0,py0 = onppundermouse identify x0 y0 (~< E.s, -1, 0, 0) in
5491 let _o1,n1,px1,py1 = onppundermouse identify x1 y1 (~< E.s, -1, 0, 0) in
5492 if n0 != -1 && n0 = n1 then seltext o0 (px0, py0, px1, py1);
5495 let showrects = function [] -> () | rects ->
5496 Gl.enable `blend;
5497 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5498 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5499 List.iter
5500 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5501 List.iter (fun l ->
5502 if l.pageno = pageno
5503 then (
5504 let dx = float (l.pagedispx - l.pagex) in
5505 let dy = float (l.pagedispy - l.pagey) in
5506 let r, g, b, alpha = c in
5507 GlDraw.color (r, g, b) ~alpha;
5508 Raw.sets_float state.vraw ~pos:0
5509 [| x0+.dx; y0+.dy;
5510 x1+.dx; y1+.dy;
5511 x3+.dx; y3+.dy;
5512 x2+.dx; y2+.dy |];
5513 GlArray.vertex `two state.vraw;
5514 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
5516 ) state.layout
5517 ) rects
5519 Gl.disable `blend;
5522 let display () =
5523 GlClear.color (scalecolor2 conf.bgcolor);
5524 GlClear.clear [`color];
5525 List.iter drawpage state.layout;
5526 let rects =
5527 match state.mode with
5528 | LinkNav (Ltexact (pageno, linkno)) ->
5529 begin match getopaque pageno with
5530 | Some opaque ->
5531 let dx = xadjsb () in
5532 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5533 let x0 = x0 + dx and x1 = x1 + dx in
5534 let color = (0.0, 0.0, 0.5, 0.5) in
5535 (pageno, color, (
5536 float x0, float y0,
5537 float x1, float y0,
5538 float x1, float y1,
5539 float x0, float y1)
5540 ) :: state.rects
5541 | None -> state.rects
5543 | LinkNav (Ltgendir _) | LinkNav (Ltnotready _)
5544 | Birdseye _
5545 | Textentry _
5546 | View -> state.rects
5548 showrects rects;
5549 let rec postloop linkindexbase = function
5550 | l :: rest ->
5551 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5552 postloop linkindexbase rest
5553 | [] -> ()
5555 showsel ();
5556 postloop 0 state.layout;
5557 state.uioh#display;
5558 begin match state.mstate with
5559 | Mzoomrect ((x0, y0), (x1, y1)) ->
5560 Gl.enable `blend;
5561 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5562 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5563 filledrect (float x0) (float y0) (float x1) (float y1);
5564 Gl.disable `blend;
5565 | Msel _
5566 | Mpan _
5567 | Mscrolly | Mscrollx
5568 | Mzoom _
5569 | Mnone -> ()
5570 end;
5571 enttext ();
5572 scrollindicator ();
5573 Wsi.swapb ();
5576 let zoomrect x y x1 y1 =
5577 let x0 = min x x1
5578 and x1 = max x x1
5579 and y0 = min y y1 in
5580 gotoy (state.y + y0);
5581 state.anchor <- getanchor ();
5582 let zoom = (float state.w) /. float (x1 - x0) in
5583 let margin =
5584 let simple () =
5585 let adjw = wadjsb () + state.winw in
5586 if state.w < adjw
5587 then (adjw - state.w) / 2
5588 else 0
5590 match conf.fitmodel with
5591 | FitWidth | FitProportional -> simple ()
5592 | FitPage ->
5593 match conf.columns with
5594 | Csplit _ ->
5595 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
5596 | Cmulti _ | Csingle _ -> simple ()
5598 state.x <- (state.x + margin) - x0;
5599 setzoom zoom;
5600 resetmstate ();
5603 let annot inline x y =
5604 match unproject x y with
5605 | Some (opaque, n, ux, uy) ->
5606 let add text =
5607 addannot opaque ux uy text;
5608 wcmd "freepage %s" (~> opaque);
5609 Hashtbl.remove state.pagemap (n, state.gen);
5610 flushtiles ();
5611 gotoy state.y
5613 if inline
5614 then
5615 let ondone s = add s in
5616 let mode = state.mode in
5617 state.mode <- Textentry (
5618 ("annotation: ", E.s, None, textentry, ondone, true),
5619 fun _ -> state.mode <- mode);
5620 state.text <- E.s;
5621 enttext ();
5622 G.postRedisplay "annot"
5623 else
5624 add @@ getusertext E.s
5625 | _ -> ()
5628 let zoomblock x y =
5629 let g opaque l px py =
5630 match rectofblock opaque px py with
5631 | Some a ->
5632 let x0 = a.(0) -. 20. in
5633 let x1 = a.(1) +. 20. in
5634 let y0 = a.(2) -. 20. in
5635 let zoom = (float state.w) /. (x1 -. x0) in
5636 let pagey = getpagey l.pageno in
5637 gotoy_and_clear_text (pagey + truncate y0);
5638 state.anchor <- getanchor ();
5639 let margin = (state.w - l.pagew)/2 in
5640 state.x <- -truncate x0 - margin;
5641 setzoom zoom;
5642 None
5643 | None -> None
5645 match conf.columns with
5646 | Csplit _ ->
5647 impmsg "block zooming does not work properly in split columns mode"
5648 | Cmulti _ | Csingle _ -> onppundermouse g x y ()
5651 let scrollx x =
5652 let winw = wadjsb () + state.winw - 1 in
5653 let s = float x /. float winw in
5654 let destx = truncate (float (state.w + winw) *. s) in
5655 state.x <- winw - destx;
5656 gotoy_and_clear_text state.y;
5657 state.mstate <- Mscrollx;
5660 let scrolly y =
5661 let s = float y /. float state.winh in
5662 let desty = truncate (float (state.maxy - state.winh) *. s) in
5663 gotoy_and_clear_text desty;
5664 state.mstate <- Mscrolly;
5667 let viewmulticlick clicks x y mask =
5668 let g opaque l px py =
5669 let mark =
5670 match clicks with
5671 | 2 -> Mark_word
5672 | 3 -> Mark_line
5673 | 4 -> Mark_block
5674 | _ -> Mark_page
5676 if markunder opaque px py mark
5677 then (
5678 Some (fun () ->
5679 let dopipe cmd =
5680 match getopaque l.pageno with
5681 | None -> ()
5682 | Some opaque -> pipesel opaque cmd
5684 state.roam <- (fun () -> dopipe conf.paxcmd);
5685 if not (Wsi.withctrl mask) then dopipe conf.selcmd;
5688 else None
5690 G.postRedisplay "viewmulticlick";
5691 onppundermouse g x y (fun () -> impmsg "nothing to select") ();
5694 let canselect () =
5695 match conf.columns with
5696 | Csplit _ -> false
5697 | Csingle _ | Cmulti _ -> conf.angle mod 360 = 0
5700 let viewmouse button down x y mask =
5701 match button with
5702 | n when (n == 4 || n == 5) && not down ->
5703 if Wsi.withctrl mask
5704 then (
5705 match state.mstate with
5706 | Mzoom (oldn, i) ->
5707 if oldn = n
5708 then (
5709 if i = 2
5710 then
5711 let incr =
5712 match n with
5713 | 5 ->
5714 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5715 | _ ->
5716 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5718 let zoom = conf.zoom -. incr in
5719 setzoom zoom;
5720 state.mstate <- Mzoom (n, 0);
5721 else
5722 state.mstate <- Mzoom (n, i+1);
5724 else state.mstate <- Mzoom (n, 0)
5726 | Msel _
5727 | Mpan _
5728 | Mscrolly | Mscrollx
5729 | Mzoomrect _
5730 | Mnone -> state.mstate <- Mzoom (n, 0)
5732 else (
5733 match state.autoscroll with
5734 | Some step -> setautoscrollspeed step (n=4)
5735 | None ->
5736 if conf.wheelbypage || conf.presentation
5737 then (
5738 if n = 4
5739 then prevpage ()
5740 else nextpage ()
5742 else
5743 let incr =
5744 if n = 4
5745 then -conf.scrollstep
5746 else conf.scrollstep
5748 let incr = incr * 2 in
5749 let y = clamp incr in
5750 gotoy_and_clear_text y
5753 | n when (n = 6 || n = 7) && not down && canpan () ->
5754 state.x <-
5755 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
5756 gotoy_and_clear_text state.y
5758 | 1 when Wsi.withshift mask ->
5759 state.mstate <- Mnone;
5760 if not down
5761 then (
5762 match unproject x y with
5763 | None -> ()
5764 | Some (_, pageno, ux, uy) ->
5765 let cmd = Printf.sprintf
5766 "%s %s %d %d %d"
5767 conf.stcmd state.path pageno ux uy
5769 match spawn cmd [] with
5770 | (exception exn) ->
5771 impmsg "execution of synctex command(%S) failed: %S"
5772 conf.stcmd @@ exntos exn
5773 | _pid -> ()
5776 | 1 when Wsi.withctrl mask ->
5777 if down
5778 then (
5779 Wsi.setcursor Wsi.CURSOR_FLEUR;
5780 state.mstate <- Mpan (x, y)
5782 else
5783 state.mstate <- Mnone
5785 | 3 ->
5786 if down
5787 then (
5788 if Wsi.withshift mask
5789 then (
5790 annot conf.annotinline x y;
5791 G.postRedisplay "addannot"
5793 else
5794 let p = (x, y) in
5795 Wsi.setcursor Wsi.CURSOR_CYCLE;
5796 state.mstate <- Mzoomrect (p, p)
5798 else (
5799 match state.mstate with
5800 | Mzoomrect ((x0, y0), _) ->
5801 if abs (x-x0) > 10 && abs (y - y0) > 10
5802 then zoomrect x0 y0 x y
5803 else (
5804 resetmstate ();
5805 G.postRedisplay "kill accidental zoom rect";
5807 | Msel _
5808 | Mpan _
5809 | Mscrolly | Mscrollx
5810 | Mzoom _
5811 | Mnone ->
5812 resetmstate ()
5815 | 1 when vscrollhit x ->
5816 if down
5817 then
5818 let _, position, sh = state.uioh#scrollph in
5819 if y > truncate position && y < truncate (position +. sh)
5820 then state.mstate <- Mscrolly
5821 else scrolly y
5822 else
5823 state.mstate <- Mnone
5825 | 1 when y > state.winh - hscrollh () ->
5826 if down
5827 then
5828 let _, position, sw = state.uioh#scrollpw in
5829 if x > truncate position && x < truncate (position +. sw)
5830 then state.mstate <- Mscrollx
5831 else scrollx x
5832 else
5833 state.mstate <- Mnone
5835 | 1 when state.bzoom -> if not down then zoomblock x y
5837 | 1 ->
5838 let dest = if down then getunder x y else Unone in
5839 begin match dest with
5840 | Ulinkgoto _
5841 | Ulinkuri _
5842 | Uremote _ | Uremotedest _
5843 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5844 gotounder dest
5846 | Unone when down ->
5847 Wsi.setcursor Wsi.CURSOR_FLEUR;
5848 state.mstate <- Mpan (x, y);
5850 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
5852 | Unone | Utext _ ->
5853 if down
5854 then (
5855 if canselect ()
5856 then (
5857 state.mstate <- Msel ((x, y), (x, y));
5858 G.postRedisplay "mouse select";
5861 else (
5862 match state.mstate with
5863 | Mnone -> ()
5865 | Mzoom _ | Mscrollx | Mscrolly ->
5866 state.mstate <- Mnone
5868 | Mzoomrect ((x0, y0), _) ->
5869 zoomrect x0 y0 x y
5871 | Mpan _ ->
5872 Wsi.setcursor Wsi.CURSOR_INHERIT;
5873 state.mstate <- Mnone
5875 | Msel ((x0, y0), (x1, y1)) ->
5876 let rec loop = function
5877 | [] -> ()
5878 | l :: rest ->
5879 let inside =
5880 let a0 = l.pagedispy in
5881 let a1 = a0 + l.pagevh in
5882 let b0 = l.pagedispx in
5883 let b1 = b0 + l.pagevw in
5884 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5885 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5887 if inside
5888 then
5889 match getopaque l.pageno with
5890 | Some opaque ->
5891 let dosel cmd () =
5892 match Unix.pipe () with
5893 | (exception exn) ->
5894 impmsg "cannot create sel pipe: %s" @@
5895 exntos exn;
5896 | (r, w) ->
5897 let clo what fd =
5898 Ne.clo fd (fun msg ->
5899 dolog "%s close failed: %s" what msg)
5901 let pid =
5902 try spawn cmd [r, 0; w, -1]
5903 with exn ->
5904 dolog "cannot execute %S: %s"
5905 cmd @@ exntos exn;
5908 if pid > 0
5909 then (
5910 copysel w opaque;
5911 G.postRedisplay "copysel";
5913 else clo "Msel pipe/w" w;
5914 clo "Msel pipe/r" r;
5916 dosel conf.selcmd ();
5917 state.roam <- dosel conf.paxcmd;
5918 | None -> ()
5919 else loop rest
5921 loop state.layout;
5922 resetmstate ();
5926 | _ -> ()
5929 let birdseyemouse button down x y mask
5930 (conf, leftx, _, hooverpageno, anchor) =
5931 match button with
5932 | 1 when down ->
5933 let rec loop = function
5934 | [] -> ()
5935 | l :: rest ->
5936 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5937 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5938 then (
5939 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5941 else loop rest
5943 loop state.layout
5944 | 3 -> ()
5945 | _ -> viewmouse button down x y mask
5948 let uioh = object
5949 method display = ()
5951 method key key mask =
5952 begin match state.mode with
5953 | Textentry textentry -> textentrykeyboard key mask textentry
5954 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5955 | View -> viewkeyboard key mask
5956 | LinkNav linknav -> linknavkeyboard key mask linknav
5957 end;
5958 state.uioh
5960 method button button bstate x y mask =
5961 begin match state.mode with
5962 | LinkNav _
5963 | View -> viewmouse button bstate x y mask
5964 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5965 | Textentry _ -> ()
5966 end;
5967 state.uioh
5969 method multiclick clicks x y mask =
5970 begin match state.mode with
5971 | LinkNav _
5972 | View -> viewmulticlick clicks x y mask
5973 | Birdseye _
5974 | Textentry _ -> ()
5975 end;
5976 state.uioh
5978 method motion x y =
5979 begin match state.mode with
5980 | Textentry _ -> ()
5981 | View | Birdseye _ | LinkNav _ ->
5982 match state.mstate with
5983 | Mzoom _ | Mnone -> ()
5985 | Mpan (x0, y0) ->
5986 let dx = x - x0
5987 and dy = y0 - y in
5988 state.mstate <- Mpan (x, y);
5989 if canpan ()
5990 then state.x <- panbound (state.x + dx);
5991 let y = clamp dy in
5992 gotoy_and_clear_text y
5994 | Msel (a, _) ->
5995 state.mstate <- Msel (a, (x, y));
5996 G.postRedisplay "motion select";
5998 | Mscrolly ->
5999 let y = min state.winh (max 0 y) in
6000 scrolly y
6002 | Mscrollx ->
6003 let x = min state.winw (max 0 x) in
6004 scrollx x
6006 | Mzoomrect (p0, _) ->
6007 state.mstate <- Mzoomrect (p0, (x, y));
6008 G.postRedisplay "motion zoomrect";
6009 end;
6010 state.uioh
6012 method pmotion x y =
6013 begin match state.mode with
6014 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6015 let rec loop = function
6016 | [] ->
6017 if hooverpageno != -1
6018 then (
6019 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6020 G.postRedisplay "pmotion birdseye no hoover";
6022 | l :: rest ->
6023 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6024 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6025 then (
6026 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6027 G.postRedisplay "pmotion birdseye hoover";
6029 else loop rest
6031 loop state.layout
6033 | Textentry _ -> ()
6035 | LinkNav _
6036 | View ->
6037 match state.mstate with
6038 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ -> ()
6039 | Mnone ->
6040 updateunder x y;
6041 if canselect ()
6042 then
6043 match conf.pax with
6044 | None -> ()
6045 | Some r ->
6046 let past, _, _ = !r in
6047 let now = now () in
6048 let delta = now -. past in
6049 if delta > 0.01
6050 then paxunder x y
6051 else r := (now, x, y)
6052 end;
6053 state.uioh
6055 method infochanged _ = ()
6057 method scrollph =
6058 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6059 let p, h =
6060 if maxy = 0
6061 then 0.0, float state.winh
6062 else scrollph state.y maxy
6064 vscrollw (), p, h
6066 method scrollpw =
6067 let winw = wadjsb () + state.winw in
6068 let fwinw = float winw in
6069 let sw =
6070 let sw = fwinw /. float state.w in
6071 let sw = fwinw *. sw in
6072 max sw (float conf.scrollh)
6074 let position =
6075 let maxx = state.w + winw in
6076 let x = winw - state.x in
6077 let percent = float x /. float maxx in
6078 (fwinw -. sw) *. percent
6080 hscrollh (), position, sw
6082 method modehash =
6083 let modename =
6084 match state.mode with
6085 | LinkNav _ -> "links"
6086 | Textentry _ -> "textentry"
6087 | Birdseye _ -> "birdseye"
6088 | View -> "view"
6090 findkeyhash conf modename
6092 method eformsgs = true
6093 method alwaysscrolly = false
6094 end;;
6096 let adderrmsg src msg =
6097 Buffer.add_string state.errmsgs msg;
6098 state.newerrmsgs <- true;
6099 G.postRedisplay src
6102 let adderrfmt src fmt =
6103 Format.ksprintf (fun s -> adderrmsg src s) fmt;
6106 let addrect pageno r g b a x0 y0 x1 y1 =
6107 Hashtbl.add state.prects pageno [|r; g; b; a; x0; y0; x1; y1|];
6110 let ract cmds =
6111 let cl = splitatspace cmds in
6112 let scan s fmt f =
6113 try Scanf.sscanf s fmt f
6114 with exn ->
6115 adderrfmt "remote exec"
6116 "error processing '%S': %s\n" cmds @@ exntos exn
6118 let rectx s pageno (r, g, b, a) x0 y0 x1 y1 =
6119 vlog "%s page %d color (%f %f %f %f) x0,y0,x1,y1 = %f %f %f %f"
6120 s pageno r g b a x0 y0 x1 y1;
6121 onpagerect
6122 pageno
6123 (fun w h ->
6124 let _,w1,h1,_ = getpagedim pageno in
6125 let sw = float w1 /. float w
6126 and sh = float h1 /. float h in
6127 let x0s = x0 *. sw
6128 and x1s = x1 *. sw
6129 and y0s = y0 *. sh
6130 and y1s = y1 *. sh in
6131 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
6132 let color = (r, g, b, a) in
6133 if conf.verbose then debugrect rect;
6134 state.rects <- (pageno, color, rect) :: state.rects;
6135 G.postRedisplay s;
6138 match cl with
6139 | "reload" :: [] -> reload ()
6140 | "goto" :: args :: [] ->
6141 scan args "%u %f %f"
6142 (fun pageno x y ->
6143 let cmd, _ = state.geomcmds in
6144 if emptystr cmd
6145 then gotopagexy pageno x y
6146 else
6147 let f prevf () =
6148 gotopagexy pageno x y;
6149 prevf ()
6151 state.reprf <- f state.reprf
6153 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
6154 | "gotor" :: args :: [] ->
6155 scan args "%S %u"
6156 (fun filename pageno -> gotounder (Uremote (filename, pageno)))
6157 | "gotord" :: args :: [] ->
6158 scan args "%S %S"
6159 (fun filename dest -> gotounder (Uremotedest (filename, dest)))
6160 | "rect" :: args :: [] ->
6161 scan args "%u %u %f %f %f %f"
6162 (fun pageno c x0 y0 x1 y1 ->
6163 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
6164 rectx "rect" pageno color x0 y0 x1 y1;
6166 | "prect" :: args :: [] ->
6167 scan args "%u %f %f %f %f %f %f %f %f"
6168 (fun pageno r g b alpha x0 y0 x1 y1 ->
6169 addrect pageno r g b alpha x0 y0 x1 y1;
6170 G.postRedisplay "prect"
6172 | "pgoto" :: args :: [] ->
6173 scan args "%u %f %f"
6174 (fun pageno x y ->
6175 let optopaque =
6176 match getopaque pageno with
6177 | Some opaque -> opaque
6178 | None -> ~< ""
6180 pgoto optopaque pageno x y;
6181 let rec fixx = function
6182 | [] -> ()
6183 | l :: rest ->
6184 if l.pageno = pageno
6185 then (
6186 state.x <- state.x - l.pagedispx;
6187 gotoy state.y;
6189 else fixx rest
6191 let layout =
6192 let mult =
6193 match conf.columns with
6194 | Csingle _ | Csplit _ -> 1
6195 | Cmulti ((n, _, _), _) -> n
6197 layout 0 state.y (state.winw * mult) state.winh
6199 fixx layout
6201 | "activatewin" :: [] -> Wsi.activatewin ()
6202 | "quit" :: [] -> raise Quit
6203 | "clearrects" :: [] ->
6204 Hashtbl.clear state.prects;
6205 G.postRedisplay "clearrects"
6206 | _ ->
6207 adderrfmt "remote command"
6208 "error processing remote command: %S\n" cmds;
6211 let remote =
6212 let scratch = Bytes.create 80 in
6213 let buf = Buffer.create 80 in
6214 fun fd ->
6215 match tempfailureretry (Unix.read fd scratch 0) 80 with
6216 | (exception Unix.Unix_error (Unix.EAGAIN, _, _)) -> None
6217 | 0 ->
6218 Unix.close fd;
6219 if Buffer.length buf > 0
6220 then (
6221 let s = Buffer.contents buf in
6222 Buffer.clear buf;
6223 ract s;
6225 None
6226 | n ->
6227 let rec eat ppos =
6228 let nlpos =
6229 match Bytes.index_from scratch ppos '\n' with
6230 | pos -> if pos >= n then -1 else pos
6231 | (exception Not_found) -> -1
6233 if nlpos >= 0
6234 then (
6235 Buffer.add_subbytes buf scratch ppos (nlpos-ppos);
6236 let s = Buffer.contents buf in
6237 Buffer.clear buf;
6238 ract s;
6239 eat (nlpos+1);
6241 else (
6242 Buffer.add_subbytes buf scratch ppos (n-ppos);
6243 Some fd
6245 in eat 0
6248 let remoteopen path =
6249 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
6250 with exn ->
6251 adderrfmt "remoteopen" "error opening %S: %s" path @@ exntos exn;
6252 None
6255 let () =
6256 let gcconfig = ref E.s in
6257 let trimcachepath = ref E.s in
6258 let rcmdpath = ref E.s in
6259 let pageno = ref None in
6260 let rootwid = ref 0 in
6261 let openlast = ref false in
6262 let nofc = ref false in
6263 let doreap = ref false in
6264 selfexec := Sys.executable_name;
6265 Arg.parse
6266 (Arg.align
6267 [("-p", Arg.String (fun s -> state.password <- s),
6268 "<password> Set password");
6270 ("-f", Arg.String
6271 (fun s ->
6272 Config.fontpath := s;
6273 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
6275 "<path> Set path to the user interface font");
6277 ("-c", Arg.String
6278 (fun s ->
6279 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
6280 Config.confpath := s),
6281 "<path> Set path to the configuration file");
6283 ("-last", Arg.Set openlast, " Open last document");
6285 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
6286 "<page-number> Jump to page");
6288 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6289 "<path> Set path to the trim cache file");
6291 ("-dest", Arg.String (fun s -> state.nameddest <- s),
6292 "<named-destination> Set named destination");
6294 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
6295 ("-cxack", Arg.Set cxack, " Cut corners");
6297 ("-remote", Arg.String (fun s -> rcmdpath := s),
6298 "<path> Set path to the remote commands source");
6300 ("-origin", Arg.String (fun s -> state.origin <- s),
6301 "<original-path> Set original path");
6303 ("-gc", Arg.Set_string gcconfig,
6304 "<script-path> Collect garbage with the help of a script");
6306 ("-nofc", Arg.Set nofc, " Do not use fontconfig");
6308 ("-v", Arg.Unit (fun () ->
6309 Printf.printf
6310 "%s\nconfiguration path: %s\n"
6311 (version ())
6312 Config.defconfpath
6314 exit 0), " Print version and exit");
6316 ("-embed", Arg.Set_int rootwid,
6317 "<window-id> Embed into window")
6320 (fun s -> state.path <- s)
6321 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6323 if !wtmode
6324 then selfexec := !selfexec ^ " -wtmode";
6326 let histmode = emptystr state.path && not !openlast in
6328 if not (Config.load !openlast)
6329 then dolog "failed to load configuration";
6330 begin match !pageno with
6331 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
6332 | None -> ()
6333 end;
6335 if nonemptystr !gcconfig
6336 then (
6337 let (c, s) =
6338 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6339 | (exception exn) -> error "socketpair for gc failed: %s" @@ exntos exn
6340 | fds -> fds
6342 match spawn !gcconfig [(c, 0); (c, 1); (s, -1)] with
6343 | (exception exn) -> error "failed to execute gc script: %s" @@ exntos exn
6344 | _pid ->
6345 Ne.clo c @@ (fun s -> error "failed to close gc fd %s" s);
6346 Config.gc s;
6347 exit 0
6350 let wsfd, winw, winh = Wsi.init (object (self)
6351 val mutable m_clicks = 0
6352 val mutable m_click_x = 0
6353 val mutable m_click_y = 0
6354 val mutable m_lastclicktime = infinity
6356 method private cleanup =
6357 state.roam <- noroam;
6358 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap
6359 method expose = G.postRedisplay "expose"
6360 method visible v =
6361 let name =
6362 match v with
6363 | Wsi.Unobscured -> "unobscured"
6364 | Wsi.PartiallyObscured -> "partiallyobscured"
6365 | Wsi.FullyObscured -> "fullyobscured"
6367 vlog "visibility change %s" name
6368 method display = display ()
6369 method map mapped = vlog "mappped %b" mapped
6370 method reshape w h =
6371 self#cleanup;
6372 reshape w h
6373 method mouse b d x y m =
6374 if d && canselect ()
6375 then (
6376 (* http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx *)
6377 m_click_x <- x;
6378 m_click_y <- y;
6379 if b = 1
6380 then (
6381 let t = now () in
6382 if abs x - m_click_x > 10
6383 || abs y - m_click_y > 10
6384 || abs_float (t -. m_lastclicktime) > 0.3
6385 then m_clicks <- 0;
6386 m_clicks <- m_clicks + 1;
6387 m_lastclicktime <- t;
6388 if m_clicks = 1
6389 then (
6390 self#cleanup;
6391 G.postRedisplay "cleanup";
6392 state.uioh <- state.uioh#button b d x y m;
6394 else state.uioh <- state.uioh#multiclick m_clicks x y m
6396 else (
6397 self#cleanup;
6398 m_clicks <- 0;
6399 m_lastclicktime <- infinity;
6400 state.uioh <- state.uioh#button b d x y m
6403 else (
6404 state.uioh <- state.uioh#button b d x y m
6406 method motion x y =
6407 state.mpos <- (x, y);
6408 state.uioh <- state.uioh#motion x y
6409 method pmotion x y =
6410 state.mpos <- (x, y);
6411 state.uioh <- state.uioh#pmotion x y
6412 method key k m =
6413 let mascm = m land (
6414 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6415 ) in
6416 let keyboard k m =
6417 let x = state.x and y = state.y in
6418 keyboard k m;
6419 if x != state.x || y != state.y then self#cleanup
6421 match state.keystate with
6422 | KSnone ->
6423 let km = k, mascm in
6424 begin
6425 match
6426 let modehash = state.uioh#modehash in
6427 try Hashtbl.find modehash km
6428 with Not_found ->
6429 try Hashtbl.find (findkeyhash conf "global") km
6430 with Not_found -> KMinsrt (k, m)
6431 with
6432 | KMinsrt (k, m) -> keyboard k m
6433 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6434 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6436 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6437 List.iter (fun (k, m) -> keyboard k m) insrt;
6438 state.keystate <- KSnone
6439 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6440 state.keystate <- KSinto (keys, insrt)
6441 | KSinto _ -> state.keystate <- KSnone
6443 method enter x y =
6444 state.mpos <- (x, y);
6445 state.uioh <- state.uioh#pmotion x y
6446 method leave = state.mpos <- (-1, -1)
6447 method winstate wsl = state.winstate <- wsl
6448 method quit = raise Quit
6449 end) !rootwid conf.cwinw conf.cwinh platform in
6451 state.wsfd <- wsfd;
6453 if not (
6454 List.exists GlMisc.check_extension
6455 [ "GL_ARB_texture_rectangle"
6456 ; "GL_EXT_texture_recangle"
6457 ; "GL_NV_texture_rectangle" ]
6459 then (dolog "OpenGL does not suppport rectangular textures"; exit 1);
6461 if (
6462 let r = GlMisc.get_string `renderer in
6463 let p = "Mesa DRI Intel(" in
6464 let l = String.length p in
6465 String.length r > l && String.sub r 0 l = p
6467 then (
6468 defconf.sliceheight <- 1024;
6469 defconf.texcount <- 32;
6470 defconf.usepbo <- true;
6473 let cs, ss =
6474 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6475 | (exception exn) ->
6476 dolog "socketpair failed: %s" @@ exntos exn;
6477 exit 1
6478 | (r, w) ->
6479 cloexec r;
6480 cloexec w;
6481 r, w
6484 setcheckers conf.checkers;
6486 init cs (
6487 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
6488 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6489 !Config.fontpath, !trimcachepath,
6490 GlMisc.check_extension "GL_ARB_pixel_buffer_object",
6491 not !nofc
6493 List.iter GlArray.enable [`texture_coord; `vertex];
6494 state.ss <- ss;
6495 reshape ~firsttime:true winw winh;
6496 state.uioh <- uioh;
6497 if histmode
6498 then (
6499 Wsi.settitle "llpp (history)";
6500 enterhistmode ();
6502 else (
6503 state.text <- "Opening " ^ (mbtoutf8 state.path);
6504 opendoc state.path state.password;
6506 display ();
6507 Wsi.mapwin ();
6508 Wsi.setcursor Wsi.CURSOR_INHERIT;
6509 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
6511 let rec reap () =
6512 match Unix.waitpid [Unix.WNOHANG] ~-1 with
6513 | (exception (Unix.Unix_error (Unix.ECHILD, _, _))) -> ()
6514 | (exception exn) -> dolog "Unix.waitpid: %s" @@ exntos exn
6515 | 0, _ -> ()
6516 | _pid, _status -> reap ()
6518 Sys.set_signal Sys.sigchld (Sys.Signal_handle (fun _ -> doreap := true));
6520 let optrfd =
6521 ref (
6522 if nonemptystr !rcmdpath
6523 then remoteopen !rcmdpath
6524 else None
6528 let rec loop deadline =
6529 if !doreap
6530 then (
6531 doreap := false;
6532 reap ()
6534 let r = [state.ss; state.wsfd] in
6535 let r =
6536 match !optrfd with
6537 | None -> r
6538 | Some fd -> fd :: r
6540 if state.redisplay
6541 then (
6542 state.redisplay <- false;
6543 display ();
6545 let timeout =
6546 let now = now () in
6547 if deadline > now
6548 then (
6549 if deadline = infinity
6550 then ~-.1.0
6551 else max 0.0 (deadline -. now)
6553 else 0.0
6555 let r, _, _ =
6556 try Unix.select r [] [] timeout
6557 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6559 begin match r with
6560 | [] ->
6561 state.ghyll None;
6562 let newdeadline =
6563 if state.ghyll == noghyll
6564 then
6565 match state.autoscroll with
6566 | Some step when step != 0 ->
6567 let y = state.y + step in
6568 let y =
6569 if y < 0
6570 then state.maxy
6571 else if y >= state.maxy then 0 else y
6573 if state.mode = View
6574 then gotoy_and_clear_text y
6575 else gotoy y;
6576 deadline +. 0.01
6577 | _ -> infinity
6578 else deadline +. 0.01
6580 loop newdeadline
6582 | l ->
6583 let rec checkfds = function
6584 | [] -> ()
6585 | fd :: rest when fd = state.ss ->
6586 let cmd = readcmd state.ss in
6587 act cmd;
6588 checkfds rest
6590 | fd :: rest when fd = state.wsfd ->
6591 Wsi.readresp fd;
6592 checkfds rest
6594 | fd :: rest when Some fd = !optrfd ->
6595 begin match remote fd with
6596 | None -> optrfd := remoteopen !rcmdpath;
6597 | opt -> optrfd := opt
6598 end;
6599 checkfds rest
6601 | _ :: rest ->
6602 dolog "select returned unknown descriptor";
6603 checkfds rest
6605 checkfds l;
6606 let newdeadline =
6607 let deadline1 =
6608 if deadline = infinity
6609 then now () +. 0.01
6610 else deadline
6612 match state.autoscroll with
6613 | Some step when step != 0 -> deadline1
6614 | _ -> if state.ghyll == noghyll then infinity else deadline1
6616 loop newdeadline
6617 end;
6620 loop infinity;
6621 with Quit ->
6622 Config.save leavebirdseye;
6623 if hasunsavedchanges ()
6624 then save ();