Optional EGL support
[llpp.git] / main.ml
blob74d2432fd6883271597f29e53eb15c707e69fa8f
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 x = min 0 (x + sw) in
828 let h = sh*3 in
829 let w = sw*3 in
830 layout x y w h;
833 let load pages =
834 let rec loop pages =
835 if state.currently != Idle
836 then ()
837 else
838 match pages with
839 | l :: rest ->
840 begin match getopaque l.pageno with
841 | None ->
842 wcmd "page %d %d" l.pageno l.pagedimno;
843 state.currently <- Loading (l, state.gen);
844 | Some opaque ->
845 tilepage l.pageno opaque pages;
846 loop rest
847 end;
848 | _ -> ()
850 if nogeomcmds state.geomcmds
851 then loop pages
854 let preload pages =
855 load pages;
856 if conf.preload && state.currently = Idle
857 then load (preloadlayout state.x state.y state.winw state.winh);
860 let layoutready layout =
861 let rec fold all ls =
862 all && match ls with
863 | l :: rest ->
864 let seen = ref false in
865 let allvisible = ref true in
866 let foo col row _ _ _ _ _ _ =
867 seen := true;
868 allvisible := !allvisible &&
869 begin match gettileopaque l col row with
870 | Some _ -> true
871 | None -> false
874 itertiles l foo;
875 fold (!seen && !allvisible) rest
876 | [] -> true
878 let alltilesvisible = fold true layout in
879 alltilesvisible;
882 let gotoy y =
883 let y = bound y 0 state.maxy in
884 let y, layout, proceed =
885 match conf.maxwait with
886 | Some time when state.ghyll == noghyll ->
887 begin match state.throttle with
888 | None ->
889 let layout = layout state.x y state.winw state.winh in
890 let ready = layoutready layout in
891 if not ready
892 then (
893 load layout;
894 state.throttle <- Some (layout, y, now ());
896 else G.postRedisplay "gotoy showall (None)";
897 y, layout, ready
898 | Some (_, _, started) ->
899 let dt = now () -. started in
900 if dt > time
901 then (
902 state.throttle <- None;
903 let layout = layout state.x y state.winw state.winh in
904 load layout;
905 G.postRedisplay "maxwait";
906 y, layout, true
908 else -1, [], false
911 | _ ->
912 let layout = layout state.x y state.winw state.winh in
913 if not !wtmode || layoutready layout
914 then G.postRedisplay "gotoy ready";
915 y, layout, true
917 if proceed
918 then (
919 state.y <- y;
920 state.layout <- layout;
921 begin match state.mode with
922 | LinkNav ln ->
923 begin match ln with
924 | Ltexact (pageno, linkno) ->
925 let rec loop = function
926 | [] ->
927 state.mode <- LinkNav (Ltgendir 0)
928 | l :: _ when l.pageno = pageno ->
929 begin match getopaque pageno with
930 | None -> state.mode <- LinkNav (Ltnotready (pageno, 0))
931 | Some opaque ->
932 let x0, y0, x1, y1 = getlinkrect opaque linkno in
933 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
934 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
935 then state.mode <- LinkNav (Ltgendir 0)
937 | _ :: rest -> loop rest
939 loop layout
940 | Ltnotready _ | Ltgendir _ -> ()
942 | Birdseye _
943 | Textentry _
944 | View -> ()
945 end;
946 begin match state.mode with
947 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
948 if not (pagevisible layout pageno)
949 then (
950 match state.layout with
951 | [] -> ()
952 | l :: _ ->
953 state.mode <- Birdseye (
954 conf, leftx, l.pageno, hooverpageno, anchor
957 | LinkNav lt ->
958 begin match lt with
959 | Ltnotready (_, dir)
960 | Ltgendir dir ->
961 let linknav =
962 let rec loop = function
963 | [] -> lt
964 | l :: rest ->
965 match getopaque l.pageno with
966 | None -> Ltnotready (l.pageno, dir)
967 | Some opaque ->
968 let link =
969 let ld =
970 if dir = 0
971 then LDfirstvisible (l.pagex, l.pagey, dir)
972 else (
973 if dir > 0 then LDfirst else LDlast
976 findlink opaque ld
978 match link with
979 | Lnotfound -> loop rest
980 | Lfound n ->
981 showlinktype (getlink opaque n);
982 Ltexact (l.pageno, n)
984 loop state.layout
986 state.mode <- LinkNav linknav
987 | Ltexact _ -> ()
989 | Textentry _
990 | View -> ()
991 end;
992 preload layout;
994 state.ghyll <- noghyll;
995 if conf.updatecurs
996 then (
997 let mx, my = state.mpos in
998 updateunder mx my;
1002 let conttiling pageno opaque =
1003 tilepage pageno opaque
1004 (if conf.preload
1005 then preloadlayout state.x state.y state.winw state.winh
1006 else state.layout)
1009 let gotoy_and_clear_text y =
1010 if not conf.verbose then state.text <- E.s;
1011 gotoy y;
1014 let getanchory (n, top, dtop) =
1015 let y, h = getpageyh n in
1016 if conf.presentation
1017 then
1018 let ips = calcips h in
1019 y + truncate (top*.float h -. dtop*.float ips) + ips;
1020 else
1021 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1024 let gotoanchor anchor =
1025 gotoy (getanchory anchor);
1028 let addnav () =
1029 cbput state.hists.nav (getanchor ());
1032 let getnav dir =
1033 let anchor = cbgetc state.hists.nav dir in
1034 getanchory anchor;
1037 let gotoghyll1 single y =
1038 let scroll f n a b =
1039 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1040 let snake f a b =
1041 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1042 if f < a
1043 then s (float f /. float a)
1044 else (
1045 if f > b
1046 then 1.0 -. s ((float (f-b) /. float (n-b)))
1047 else 1.0
1050 snake f a b
1051 and summa n a b =
1052 let ins = float a *. 0.5
1053 and outs = float (n-b) *. 0.5 in
1054 let ones = b - a in
1055 ins +. outs +. float ones
1057 let rec set nab y sy =
1058 let (_N, _A, _B), y =
1059 if single
1060 then
1061 let scl = if y > sy then 2 else -2 in
1062 let _N, _, _ = nab in
1063 (_N,0,_N), y+conf.scrollstep*scl
1064 else nab,y in
1065 let sum = summa _N _A _B in
1066 let dy = float (y - sy) in
1067 state.ghyll <- (
1068 let rec gf n y1 o =
1069 if n >= _N
1070 then state.ghyll <- noghyll
1071 else
1072 let go n =
1073 let s = scroll n _N _A _B in
1074 let y1 = y1 +. ((s *. dy) /. sum) in
1075 gotoy_and_clear_text (truncate y1);
1076 state.ghyll <- gf (n+1) y1;
1078 match o with
1079 | None -> go n
1080 | Some y' when single -> set nab y' state.y
1081 | Some y' -> set (_N/2, 1, 1) y' state.y
1083 gf 0 (float state.y)
1086 match conf.ghyllscroll with
1087 | Some nab when not conf.presentation ->
1088 if state.ghyll == noghyll
1089 then set nab y state.y
1090 else state.ghyll (Some y)
1091 | _ ->
1092 gotoy_and_clear_text y
1095 let gotoghyll = gotoghyll1 false;;
1097 let gotopage n top =
1098 let y, h = getpageyh n in
1099 let y = y + (truncate (top *. float h)) in
1100 gotoghyll y
1103 let gotopage1 n top =
1104 let y = getpagey n in
1105 let y = y + top in
1106 gotoghyll y
1109 let invalidate s f =
1110 state.layout <- [];
1111 state.pdims <- [];
1112 state.rects <- [];
1113 state.rects1 <- [];
1114 match state.geomcmds with
1115 | ps, [] when emptystr ps ->
1116 f ();
1117 state.geomcmds <- s, [];
1119 | ps, [] ->
1120 state.geomcmds <- ps, [s, f];
1122 | ps, (s', _) :: rest when s' = s ->
1123 state.geomcmds <- ps, ((s, f) :: rest);
1125 | ps, cmds ->
1126 state.geomcmds <- ps, ((s, f) :: cmds);
1129 let flushpages () =
1130 Hashtbl.iter (fun _ opaque ->
1131 wcmd "freepage %s" (~> opaque);
1132 ) state.pagemap;
1133 Hashtbl.clear state.pagemap;
1136 let flushtiles () =
1137 if not (Queue.is_empty state.tilelru)
1138 then (
1139 Queue.iter (fun (k, p, s) ->
1140 wcmd "freetile %s" (~> p);
1141 state.memused <- state.memused - s;
1142 Hashtbl.remove state.tilemap k;
1143 ) state.tilelru;
1144 state.uioh#infochanged Memused;
1145 Queue.clear state.tilelru;
1147 load state.layout;
1150 let stateh h =
1151 let h = truncate (float h*.conf.zoom) in
1152 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
1153 h - d
1156 let opendoc path password =
1157 state.path <- path;
1158 state.password <- password;
1159 state.gen <- state.gen + 1;
1160 state.docinfo <- [];
1161 state.outlines <- [||];
1163 flushpages ();
1164 setaalevel conf.aalevel;
1165 let titlepath =
1166 if emptystr state.origin
1167 then path
1168 else state.origin
1170 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
1171 wcmd "open %d %d %s\000%s\000" (btod !wtmode) (btod !cxack) path password;
1172 invalidate "reqlayout"
1173 (fun () ->
1174 wcmd "reqlayout %d %d %d %s\000"
1175 conf.angle (FMTE.to_int conf.fitmodel)
1176 (stateh state.winh) state.nameddest
1180 let reload () =
1181 state.anchor <- getanchor ();
1182 opendoc state.path state.password;
1185 let scalecolor c =
1186 let c = c *. conf.colorscale in
1187 (c, c, c);
1190 let scalecolor2 (r, g, b) =
1191 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1194 let docolumns columns =
1195 let wadj = wadjsb () in
1196 match columns with
1197 | Csingle _ ->
1198 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1199 let wadj = wadjsb () in
1200 let rec loop pageno pdimno pdim y ph pdims =
1201 if pageno = state.pagecount
1202 then ()
1203 else
1204 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1205 match pdims with
1206 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1207 pdimno+1, pdim, rest
1208 | _ ->
1209 pdimno, pdim, pdims
1211 let x = max 0 (((wadj + state.winw - w) / 2) - xoff) in
1212 let y = y +
1213 (if conf.presentation
1214 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1215 else (if pageno = 0 then 0 else conf.interpagespace)
1218 a.(pageno) <- (pdimno, x, y, pdim);
1219 loop (pageno+1) pdimno pdim (y + h) h pdims
1221 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1222 conf.columns <- Csingle a;
1224 | Cmulti ((columns, coverA, coverB), _) ->
1225 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1226 let rec loop pageno pdimno pdim x y rowh pdims =
1227 let rec fixrow m = if m = pageno then () else
1228 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1229 if h < rowh
1230 then (
1231 let y = y + (rowh - h) / 2 in
1232 a.(m) <- (pdimno, x, y, pdim);
1234 fixrow (m+1)
1236 if pageno = state.pagecount
1237 then fixrow (((pageno - 1) / columns) * columns)
1238 else
1239 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1240 match pdims with
1241 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1242 pdimno+1, pdim, rest
1243 | _ ->
1244 pdimno, pdim, pdims
1246 let x, y, rowh' =
1247 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1248 then (
1249 let x = (wadj + state.winw - w) / 2 in
1250 let ips =
1251 if conf.presentation then calcips h else conf.interpagespace in
1252 x, y + ips + rowh, h
1254 else (
1255 if (pageno - coverA) mod columns = 0
1256 then (
1257 let x = max 0 (wadj + state.winw - state.w) / 2 in
1258 let y =
1259 if conf.presentation
1260 then
1261 let ips = calcips h in
1262 y + (if pageno = 0 then 0 else calcips rowh + ips)
1263 else
1264 y + (if pageno = 0 then 0 else conf.interpagespace)
1266 x, y + rowh, h
1268 else x, y, max rowh h
1271 let y =
1272 if pageno > 1 && (pageno - coverA) mod columns = 0
1273 then (
1274 let y =
1275 if pageno = columns && conf.presentation
1276 then (
1277 let ips = calcips rowh in
1278 for i = 0 to pred columns
1280 let (pdimno, x, y, pdim) = a.(i) in
1281 a.(i) <- (pdimno, x, y+ips, pdim)
1282 done;
1283 y+ips;
1285 else y
1287 fixrow (pageno - columns);
1290 else y
1292 a.(pageno) <- (pdimno, x, y, pdim);
1293 let x = x + w + xoff*2 + conf.interpagespace in
1294 loop (pageno+1) pdimno pdim x y rowh' pdims
1296 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1297 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1299 | Csplit (c, _) ->
1300 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1301 let rec loop pageno pdimno pdim y pdims =
1302 if pageno = state.pagecount
1303 then ()
1304 else
1305 let pdimno, ((_, w, h, _) as pdim), pdims =
1306 match pdims with
1307 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1308 pdimno+1, pdim, rest
1309 | _ ->
1310 pdimno, pdim, pdims
1312 let cw = w / c in
1313 let rec loop1 n x y =
1314 if n = c then y else (
1315 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1316 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1319 let y = loop1 0 0 y in
1320 loop (pageno+1) pdimno pdim y pdims
1322 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1323 conf.columns <- Csplit (c, a);
1326 let represent () =
1327 docolumns conf.columns;
1328 state.maxy <- calcheight ();
1329 if state.reprf == noreprf
1330 then (
1331 match state.mode with
1332 | Birdseye (_, _, pageno, _, _) ->
1333 let y, h = getpageyh pageno in
1334 let top = (state.winh - h) / 2 in
1335 gotoy (max 0 (y - top))
1336 | Textentry _
1337 | View
1338 | LinkNav _ ->
1339 let y = getanchory state.anchor in
1340 let y = min y (state.maxy - state.winw - hscrollh ()) in
1341 gotoy y;
1343 else (
1344 state.reprf ();
1345 state.reprf <- noreprf;
1349 let reshape ?(firsttime=false) w h =
1350 GlDraw.viewport ~x:0 ~y:0 ~w:w ~h:h;
1351 if not firsttime && nogeomcmds state.geomcmds
1352 then state.anchor <- getanchor ();
1354 state.winw <- w;
1355 let w = wadjsb () + (truncate (float w *. conf.zoom)) in
1356 let w = max w 2 in
1357 state.winh <- h;
1358 setfontsize fstate.fontsize;
1359 GlMat.mode `modelview;
1360 GlMat.load_identity ();
1362 GlMat.mode `projection;
1363 GlMat.load_identity ();
1364 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1365 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1366 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
1368 let relx =
1369 if conf.zoom <= 1.0
1370 then 0.0
1371 else float state.x /. float state.w
1373 invalidate "geometry"
1374 (fun () ->
1375 state.w <- w;
1376 if not firsttime
1377 then state.x <- truncate (relx *. float w);
1378 let w =
1379 match conf.columns with
1380 | Csingle _ -> w
1381 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1382 | Csplit (c, _) -> w * c
1384 wcmd "geometry %d %d %d"
1385 w (stateh h) (FMTE.to_int conf.fitmodel)
1389 let enttext () =
1390 let len = String.length state.text in
1391 let x0 = xadjsb () in
1392 let drawstring s =
1393 let hscrollh =
1394 match state.mode with
1395 | Textentry _ | View | LinkNav _ ->
1396 let h, _, _ = state.uioh#scrollpw in
1398 | Birdseye _ -> 0
1400 let rect x w =
1401 filledrect x (float (state.winh - (fstate.fontsize + 4) - hscrollh))
1402 (x+.w) (float (state.winh - hscrollh))
1405 let w = float (wadjsb () + state.winw - 1) in
1406 if state.progress >= 0.0 && state.progress < 1.0
1407 then (
1408 GlDraw.color (0.3, 0.3, 0.3);
1409 let w1 = w *. state.progress in
1410 rect (float x0) w1;
1411 GlDraw.color (0.0, 0.0, 0.0);
1412 rect (float x0+.w1) (float x0+.w-.w1)
1414 else (
1415 GlDraw.color (0.0, 0.0, 0.0);
1416 rect (float x0) w;
1419 GlDraw.color (1.0, 1.0, 1.0);
1420 drawstring fstate.fontsize
1421 (if conf.leftscroll then x0 + 2 else x0 + if len > 0 then 8 else 2)
1422 (state.winh - hscrollh - 5) s;
1424 let s =
1425 match state.mode with
1426 | Textentry ((prefix, text, _, _, _, _), _) ->
1427 let s =
1428 if len > 0
1429 then
1430 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1431 else
1432 Printf.sprintf "%s%s_" prefix text
1436 | Birdseye _
1437 | View
1438 | LinkNav _ -> state.text
1440 let s =
1441 if state.newerrmsgs
1442 then (
1443 if not (istextentry state.mode) && state.uioh#eformsgs
1444 then
1445 let s1 = "(press 'e' to review error messasges)" in
1446 if nonemptystr s then s ^ " " ^ s1 else s1
1447 else s
1449 else s
1451 if nonemptystr s
1452 then drawstring s
1455 let gctiles () =
1456 let len = Queue.length state.tilelru in
1457 let layout = lazy (
1458 match state.throttle with
1459 | None ->
1460 if conf.preload
1461 then preloadlayout state.x state.y state.winw state.winh
1462 else state.layout
1463 | Some (layout, _, _) ->
1464 layout
1465 ) in
1466 let rec loop qpos =
1467 if state.memused <= conf.memlimit
1468 then ()
1469 else (
1470 if qpos < len
1471 then
1472 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1473 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1474 let (_, pw, ph, _) = getpagedim n in
1476 gen = state.gen
1477 && colorspace = conf.colorspace
1478 && angle = conf.angle
1479 && pagew = pw
1480 && pageh = ph
1481 && (
1482 let x = col*conf.tilew
1483 and y = row*conf.tileh in
1484 tilevisible (Lazy.force_val layout) n x y
1486 then Queue.push lruitem state.tilelru
1487 else (
1488 freepbo p;
1489 wcmd "freetile %s" (~> p);
1490 state.memused <- state.memused - s;
1491 state.uioh#infochanged Memused;
1492 Hashtbl.remove state.tilemap k;
1494 loop (qpos+1)
1497 loop 0
1500 let onpagerect pageno f =
1501 let b =
1502 match conf.columns with
1503 | Cmulti (_, b) -> b
1504 | Csingle b -> b
1505 | Csplit (_, b) -> b
1507 if pageno >= 0 && pageno < Array.length b
1508 then
1509 let (_, _, _, (_, w, h, _)) = b.(pageno) in
1510 f w h
1513 let gotopagexy1 wtmode pageno x y =
1514 let _,w1,h1,leftx = getpagedim pageno in
1515 let top = y /. (float h1) in
1516 let left = x /. (float w1) in
1517 let py, w, h = getpageywh pageno in
1518 let wh = state.winh - hscrollh () in
1519 let x = left *. (float w) in
1520 let x = leftx + state.x + truncate x in
1521 let wadj = wadjsb () in
1522 let sx =
1523 if x < 0 || x >= wadj + state.winw
1524 then state.x - x
1525 else state.x
1527 let pdy = truncate (top *. float h) in
1528 let y' = py + pdy in
1529 let dy = y' - state.y in
1530 let sy =
1531 if x != state.x || not (dy > 0 && dy < wh)
1532 then (
1533 if conf.presentation
1534 then
1535 if abs (py - y') > wh
1536 then y'
1537 else py
1538 else y';
1540 else state.y
1542 if state.x != sx || state.y != sy
1543 then (
1544 let x, y =
1545 if wtmode
1546 then (
1547 let ww = wadj + state.winw in
1548 let qx = sx / ww
1549 and qy = pdy / wh in
1550 let x = qx * ww
1551 and y = py + qy * wh in
1552 let x = if -x + ww > w1 then -(w1-ww) else x
1553 and y' = if y + wh > state.maxy then state.maxy - wh else y in
1554 let y =
1555 if conf.presentation
1556 then
1557 if abs (py - y') > wh
1558 then y'
1559 else py
1560 else y';
1562 (x, y)
1564 else (sx, sy)
1566 state.x <- x;
1567 gotoy_and_clear_text y;
1569 else gotoy_and_clear_text state.y;
1572 let gotopagexy wtmode pageno x y =
1573 match state.mode with
1574 | Birdseye _ -> gotopage pageno 0.0
1575 | Textentry _
1576 | View
1577 | LinkNav _ -> gotopagexy1 wtmode pageno x y
1580 let getpassword () =
1581 let passcmd = getenvwithdef "LLPP_ASKPASS" conf.passcmd in
1582 if emptystr passcmd
1583 then E.s
1584 else getcmdoutput
1585 (fun s ->
1586 impmsg "error getting password: %s" s;
1587 dolog "%s" s) passcmd;
1590 let pgoto opaque pageno x y =
1591 let pdimno = getpdimno pageno in
1592 let x, y = project opaque pageno pdimno x y in
1593 gotopagexy false pageno x y;
1596 let act cmds =
1597 (* dolog "%S" cmds; *)
1598 let cl = splitatspace cmds in
1599 let scan s fmt f =
1600 try Scanf.sscanf s fmt f
1601 with exn ->
1602 dolog "error processing '%S': %s" cmds @@ exntos exn;
1603 exit 1
1605 let addoutline outline =
1606 match state.currently with
1607 | Outlining outlines ->
1608 state.currently <- Outlining (outline :: outlines)
1609 | Idle -> state.currently <- Outlining [outline]
1610 | Loading _
1611 | Tiling _ ->
1612 dolog "invalid outlining state";
1613 logcurrently state.currently
1615 match cl with
1616 | "clear" :: [] ->
1617 state.uioh#infochanged Pdim;
1618 state.pdims <- [];
1620 | "clearrects" :: [] ->
1621 state.rects <- state.rects1;
1622 G.postRedisplay "clearrects";
1624 | "continue" :: args :: [] ->
1625 let n = scan args "%u" (fun n -> n) in
1626 state.pagecount <- n;
1627 begin match state.currently with
1628 | Outlining l ->
1629 state.currently <- Idle;
1630 state.outlines <- Array.of_list (List.rev l)
1631 | Idle
1632 | Loading _
1633 | Tiling _ -> ()
1634 end;
1636 let cur, cmds = state.geomcmds in
1637 if emptystr cur
1638 then failwith "umpossible";
1640 begin match List.rev cmds with
1641 | [] ->
1642 state.geomcmds <- E.s, [];
1643 state.throttle <- None;
1644 represent ();
1645 | (s, f) :: rest ->
1646 f ();
1647 state.geomcmds <- s, List.rev rest;
1648 end;
1649 if conf.maxwait = None && not !wtmode
1650 then G.postRedisplay "continue";
1652 | "msg" :: args :: [] ->
1653 showtext ' ' args
1655 | "vmsg" :: args :: [] ->
1656 if conf.verbose
1657 then showtext ' ' args
1659 | "emsg" :: args :: [] ->
1660 Buffer.add_string state.errmsgs args;
1661 state.newerrmsgs <- true;
1662 G.postRedisplay "error message"
1664 | "progress" :: args :: [] ->
1665 let progress, text =
1666 scan args "%f %n"
1667 (fun f pos ->
1668 f, String.sub args pos (String.length args - pos))
1670 state.text <- text;
1671 state.progress <- progress;
1672 G.postRedisplay "progress"
1674 | "firstmatch" :: args :: [] ->
1675 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1676 scan args "%u %d %f %f %f %f %f %f %f %f"
1677 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1678 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1680 let xoff = float (xadjsb ()) in
1681 let x0 = x0 +. xoff
1682 and x1 = x1 +. xoff
1683 and x2 = x2 +. xoff
1684 and x3 = x3 +. xoff in
1685 let y = (getpagey pageno) + truncate y0 in
1686 if conf.zoom > 1.0
1687 then state.x <- truncate (xoff -. x0) + state.winw/2;
1688 addnav ();
1689 gotoy y;
1690 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1691 state.rects1 <- [pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)]
1693 | "match" :: args :: [] ->
1694 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1695 scan args "%u %d %f %f %f %f %f %f %f %f"
1696 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1697 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1699 let xoff = float (xadjsb ()) in
1700 let x0 = x0 +. xoff
1701 and x1 = x1 +. xoff
1702 and x2 = x2 +. xoff
1703 and x3 = x3 +. xoff in
1704 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1705 state.rects1 <-
1706 (pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1708 | "page" :: args :: [] ->
1709 let pageopaques, t = scan args "%s %f" (fun p t -> p, t) in
1710 let pageopaque = ~< pageopaques in
1711 begin match state.currently with
1712 | Loading (l, gen) ->
1713 vlog "page %d took %f sec" l.pageno t;
1714 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1715 begin match state.throttle with
1716 | None ->
1717 let preloadedpages =
1718 if conf.preload
1719 then preloadlayout state.x state.y state.winw state.winh
1720 else state.layout
1722 let evict () =
1723 let set =
1724 List.fold_left (fun s l -> IntSet.add l.pageno s)
1725 IntSet.empty preloadedpages
1727 let evictedpages =
1728 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1729 if not (IntSet.mem pageno set)
1730 then (
1731 wcmd "freepage %s" (~> opaque);
1732 key :: accu
1734 else accu
1735 ) state.pagemap []
1737 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1739 evict ();
1740 state.currently <- Idle;
1741 if gen = state.gen
1742 then (
1743 tilepage l.pageno pageopaque state.layout;
1744 load state.layout;
1745 load preloadedpages;
1746 let visible = pagevisible state.layout l.pageno in
1747 if visible
1748 then (
1749 match state.mode with
1750 | LinkNav (Ltnotready (pageno, dir)) ->
1751 if pageno = l.pageno
1752 then (
1753 let link =
1754 let ld =
1755 if dir = 0
1756 then LDfirstvisible (l.pagex, l.pagey, dir)
1757 else (
1758 if dir > 0 then LDfirst else LDlast
1761 findlink pageopaque ld
1763 match link with
1764 | Lnotfound -> ()
1765 | Lfound n ->
1766 showlinktype (getlink pageopaque n);
1767 state.mode <- LinkNav (Ltexact (l.pageno, n))
1769 | LinkNav (Ltgendir _)
1770 | LinkNav (Ltexact _)
1771 | View
1772 | Birdseye _
1773 | Textentry _ -> ()
1776 if visible && layoutready state.layout
1777 then (
1778 G.postRedisplay "page";
1782 | Some (layout, _, _) ->
1783 state.currently <- Idle;
1784 tilepage l.pageno pageopaque layout;
1785 load state.layout
1786 end;
1788 | Idle
1789 | Tiling _
1790 | Outlining _ ->
1791 dolog "Inconsistent loading state";
1792 logcurrently state.currently;
1793 exit 1
1796 | "tile" :: args :: [] ->
1797 let (x, y, opaques, size, t) =
1798 scan args "%u %u %s %u %f"
1799 (fun x y p size t -> (x, y, p, size, t))
1801 let opaque = ~< opaques in
1802 begin match state.currently with
1803 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1804 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1806 unmappbo opaque;
1807 if tilew != conf.tilew || tileh != conf.tileh
1808 then (
1809 wcmd "freetile %s" (~> opaque);
1810 state.currently <- Idle;
1811 load state.layout;
1813 else (
1814 puttileopaque l col row gen cs angle opaque size t;
1815 state.memused <- state.memused + size;
1816 state.uioh#infochanged Memused;
1817 gctiles ();
1818 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1819 opaque, size) state.tilelru;
1821 let layout =
1822 match state.throttle with
1823 | None -> state.layout
1824 | Some (layout, _, _) -> layout
1827 state.currently <- Idle;
1828 if gen = state.gen
1829 && conf.colorspace = cs
1830 && conf.angle = angle
1831 && tilevisible layout l.pageno x y
1832 then conttiling l.pageno pageopaque;
1834 begin match state.throttle with
1835 | None ->
1836 preload state.layout;
1837 if gen = state.gen
1838 && conf.colorspace = cs
1839 && conf.angle = angle
1840 && tilevisible state.layout l.pageno x y
1841 && (not !wtmode || layoutready state.layout)
1842 then G.postRedisplay "tile nothrottle";
1844 | Some (layout, y, _) ->
1845 let ready = layoutready layout in
1846 if ready
1847 then (
1848 state.y <- y;
1849 state.layout <- layout;
1850 state.throttle <- None;
1851 G.postRedisplay "throttle";
1853 else load layout;
1854 end;
1857 | Idle
1858 | Loading _
1859 | Outlining _ ->
1860 dolog "Inconsistent tiling state";
1861 logcurrently state.currently;
1862 exit 1
1865 | "pdim" :: args :: [] ->
1866 let (n, w, h, _) as pdim =
1867 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1869 let pdim =
1870 match conf.fitmodel with
1871 | FitWidth -> pdim
1872 | FitPage | FitProportional ->
1873 match conf.columns with
1874 | Csplit _ -> (n, w, h, 0)
1875 | Csingle _ | Cmulti _ -> pdim
1877 state.uioh#infochanged Pdim;
1878 state.pdims <- pdim :: state.pdims
1880 | "o" :: args :: [] ->
1881 let (l, n, t, h, pos) =
1882 scan args "%u %u %d %u %n"
1883 (fun l n t h pos -> l, n, t, h, pos)
1885 let s = String.sub args pos (String.length args - pos) in
1886 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
1888 | "ou" :: args :: [] ->
1889 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
1890 let s = String.sub args pos len in
1891 let pos2 = pos + len + 1 in
1892 let uri = String.sub args pos2 (String.length args - pos2) in
1893 addoutline (s, l, Ouri uri)
1895 | "on" :: args :: [] ->
1896 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
1897 let s = String.sub args pos (String.length args - pos) in
1898 addoutline (s, l, Onone)
1900 | "a" :: args :: [] ->
1901 let (n, l, t) =
1902 scan args "%u %d %d" (fun n l t -> n, l, t)
1904 state.reprf <- (fun () -> gotopagexy !wtmode n (float l) (float t))
1906 | "info" :: args :: [] ->
1907 let pos = nindex args '\t' in
1908 if pos >= 0 && String.sub args 0 pos = "Title"
1909 then (
1910 let s = String.sub args (pos+1) @@ String.length args - pos - 1 in
1911 conf.title <- s;
1912 Wsi.settitle s;
1914 state.docinfo <- (1, args) :: state.docinfo
1916 | "infoend" :: [] ->
1917 state.uioh#infochanged Docinfo;
1918 state.docinfo <- List.rev state.docinfo
1920 | "pass" :: l ->
1921 if l = "fail" :: []
1922 then Wsi.settitle "Wrong password";
1923 let password = getpassword () in
1924 if emptystr password
1925 then error "document is password protected"
1926 else opendoc state.path password
1927 | _ ->
1928 error "unknown cmd `%S'" cmds
1931 let onhist cb =
1932 let rc = cb.rc in
1933 let action = function
1934 | HCprev -> cbget cb ~-1
1935 | HCnext -> cbget cb 1
1936 | HCfirst -> cbget cb ~-(cb.rc)
1937 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1938 and cancel () = cb.rc <- rc
1939 in (action, cancel)
1942 let search pattern forward =
1943 match conf.columns with
1944 | Csplit _ -> impmsg "searching does not work properly in split columns mode"
1945 | Csingle _
1946 | Cmulti _ ->
1947 if nonemptystr pattern
1948 then
1949 let pn, py =
1950 match state.layout with
1951 | [] -> 0, 0
1952 | l :: _ ->
1953 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1955 wcmd "search %d %d %d %d,%s\000"
1956 (btod conf.icase) pn py (btod forward) pattern;
1959 let intentry text key =
1960 let c =
1961 if key >= 32 && key < 127
1962 then Char.chr key
1963 else '\000'
1965 match c with
1966 | '0' .. '9' ->
1967 let text = addchar text c in
1968 TEcont text
1970 | _ ->
1971 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1972 TEcont text
1975 let linknact f s =
1976 if nonemptystr s
1977 then (
1978 let n =
1979 let l = String.length s in
1980 let rec loop pos n = if pos = l then n else
1981 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
1982 loop (pos+1) (n*26 + m)
1983 in loop 0 0
1985 let rec loop n = function
1986 | [] -> ()
1987 | l :: rest ->
1988 match getopaque l.pageno with
1989 | None -> loop n rest
1990 | Some opaque ->
1991 let m = getlinkcount opaque in
1992 if n < m
1993 then (
1994 let under = getlink opaque n in
1995 f under
1997 else loop (n-m) rest
1999 loop n state.layout;
2003 let linknentry text key =
2004 let c =
2005 if key >= 32 && key < 127
2006 then Char.chr key
2007 else '\000'
2009 match c with
2010 | 'a' .. 'z' ->
2011 let text = addchar text c in
2012 linknact (fun under -> state.text <- undertext ~nopath:true under) text;
2013 TEcont text
2015 | _ ->
2016 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2017 TEcont text
2020 let textentry text key =
2021 if key land 0xff00 = 0xff00
2022 then TEcont text
2023 else TEcont (text ^ toutf8 key)
2026 let reqlayout angle fitmodel =
2027 match state.throttle with
2028 | None ->
2029 if nogeomcmds state.geomcmds
2030 then state.anchor <- getanchor ();
2031 conf.angle <- angle mod 360;
2032 if conf.angle != 0
2033 then (
2034 match state.mode with
2035 | LinkNav _ -> state.mode <- View
2036 | Birdseye _
2037 | Textentry _
2038 | View -> ()
2040 conf.fitmodel <- fitmodel;
2041 invalidate "reqlayout"
2042 (fun () ->
2043 wcmd "reqlayout %d %d %d"
2044 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
2046 | _ -> ()
2049 let settrim trimmargins trimfuzz =
2050 if nogeomcmds state.geomcmds
2051 then state.anchor <- getanchor ();
2052 conf.trimmargins <- trimmargins;
2053 conf.trimfuzz <- trimfuzz;
2054 let x0, y0, x1, y1 = trimfuzz in
2055 invalidate "settrim"
2056 (fun () ->
2057 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2058 flushpages ();
2061 let setzoom zoom =
2062 match state.throttle with
2063 | None ->
2064 let zoom = max 0.0001 zoom in
2065 if zoom <> conf.zoom
2066 then (
2067 state.prevzoom <- (conf.zoom, state.x);
2068 conf.zoom <- zoom;
2069 reshape state.winw state.winh;
2070 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2073 | Some (layout, y, started) ->
2074 let time =
2075 match conf.maxwait with
2076 | None -> 0.0
2077 | Some t -> t
2079 let dt = now () -. started in
2080 if dt > time
2081 then (
2082 state.y <- y;
2083 load layout;
2087 let setcolumns mode columns coverA coverB =
2088 state.prevcolumns <- Some (conf.columns, conf.zoom);
2089 if columns < 0
2090 then (
2091 if isbirdseye mode
2092 then impmsg "split mode doesn't work in bird's eye"
2093 else (
2094 conf.columns <- Csplit (-columns, E.a);
2095 state.x <- 0;
2096 conf.zoom <- 1.0;
2099 else (
2100 if columns < 2
2101 then (
2102 conf.columns <- Csingle E.a;
2103 state.x <- 0;
2104 setzoom 1.0;
2106 else (
2107 conf.columns <- Cmulti ((columns, coverA, coverB), E.a);
2108 conf.zoom <- 1.0;
2111 reshape state.winw state.winh;
2114 let resetmstate () =
2115 state.mstate <- Mnone;
2116 Wsi.setcursor Wsi.CURSOR_INHERIT;
2119 let enterbirdseye () =
2120 let zoom = float conf.thumbw /. float state.winw in
2121 let birdseyepageno =
2122 let cy = state.winh / 2 in
2123 let fold = function
2124 | [] -> 0
2125 | l :: rest ->
2126 let rec fold best = function
2127 | [] -> best.pageno
2128 | l :: rest ->
2129 let d = cy - (l.pagedispy + l.pagevh/2)
2130 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2131 if abs d < abs dbest
2132 then fold l rest
2133 else best.pageno
2134 in fold l rest
2136 fold state.layout
2138 state.mode <- Birdseye (
2139 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2141 resetmstate ();
2142 conf.zoom <- zoom;
2143 conf.presentation <- false;
2144 conf.interpagespace <- 10;
2145 conf.hlinks <- false;
2146 conf.fitmodel <- FitPage;
2147 state.x <- 0;
2148 conf.maxwait <- None;
2149 conf.columns <- (
2150 match conf.beyecolumns with
2151 | Some c ->
2152 conf.zoom <- 1.0;
2153 Cmulti ((c, 0, 0), E.a)
2154 | None -> Csingle E.a
2156 if conf.verbose
2157 then
2158 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2159 (100.0*.zoom)
2160 else
2161 state.text <- E.s
2163 reshape state.winw state.winh;
2166 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2167 state.mode <- View;
2168 conf.zoom <- c.zoom;
2169 conf.presentation <- c.presentation;
2170 conf.interpagespace <- c.interpagespace;
2171 conf.maxwait <- c.maxwait;
2172 conf.hlinks <- c.hlinks;
2173 conf.fitmodel <- c.fitmodel;
2174 conf.beyecolumns <- (
2175 match conf.columns with
2176 | Cmulti ((c, _, _), _) -> Some c
2177 | Csingle _ -> None
2178 | Csplit _ -> failwith "leaving bird's eye split mode"
2180 conf.columns <- (
2181 match c.columns with
2182 | Cmulti (c, _) -> Cmulti (c, E.a)
2183 | Csingle _ -> Csingle E.a
2184 | Csplit (c, _) -> Csplit (c, E.a)
2186 if conf.verbose
2187 then
2188 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2189 (100.0*.conf.zoom)
2191 reshape state.winw state.winh;
2192 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2193 state.x <- leftx;
2196 let togglebirdseye () =
2197 match state.mode with
2198 | Birdseye vals -> leavebirdseye vals true
2199 | View -> enterbirdseye ()
2200 | Textentry _
2201 | LinkNav _ -> ()
2204 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2205 let pageno = max 0 (pageno - incr) in
2206 let rec loop = function
2207 | [] -> gotopage1 pageno 0
2208 | l :: _ when l.pageno = pageno ->
2209 if l.pagedispy >= 0 && l.pagey = 0
2210 then G.postRedisplay "upbirdseye"
2211 else gotopage1 pageno 0
2212 | _ :: rest -> loop rest
2214 loop state.layout;
2215 state.text <- E.s;
2216 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2219 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2220 let pageno = min (state.pagecount - 1) (pageno + incr) in
2221 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2222 let rec loop = function
2223 | [] ->
2224 let y, h = getpageyh pageno in
2225 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2226 gotoy (clamp dy)
2227 | l :: _ when l.pageno = pageno ->
2228 if l.pagevh != l.pageh
2229 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2230 else G.postRedisplay "downbirdseye"
2231 | _ :: rest -> loop rest
2233 loop state.layout;
2234 state.text <- E.s;
2237 let optentry mode _ key =
2238 let btos b = if b then "on" else "off" in
2239 if key >= 32 && key < 127
2240 then
2241 let c = Char.chr key in
2242 match c with
2243 | 's' ->
2244 let ondone s =
2245 try conf.scrollstep <- int_of_string s with exc ->
2246 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exc
2248 TEswitch ("scroll step: ", E.s, None, intentry, ondone, true)
2250 | 'A' ->
2251 let ondone s =
2253 conf.autoscrollstep <- boundastep state.winh (int_of_string s);
2254 if state.autoscroll <> None
2255 then state.autoscroll <- Some conf.autoscrollstep
2256 with exc ->
2257 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exc
2259 TEswitch ("auto scroll step: ", E.s, None, intentry, ondone, true)
2261 | 'C' ->
2262 let ondone s =
2264 let n, a, b = multicolumns_of_string s in
2265 setcolumns mode n a b;
2266 with exc ->
2267 state.text <- Printf.sprintf "bad columns `%s': %s" s @@ exntos exc
2269 TEswitch ("columns: ", E.s, None, textentry, ondone, true)
2271 | 'Z' ->
2272 let ondone s =
2274 let zoom = float (int_of_string s) /. 100.0 in
2275 setzoom zoom
2276 with exc ->
2277 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exc
2279 TEswitch ("zoom: ", E.s, None, intentry, ondone, true)
2281 | 't' ->
2282 let ondone s =
2284 conf.thumbw <- bound (int_of_string s) 2 4096;
2285 state.text <-
2286 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2287 begin match mode with
2288 | Birdseye beye ->
2289 leavebirdseye beye false;
2290 enterbirdseye ();
2291 | Textentry _
2292 | View
2293 | LinkNav _ -> ();
2295 with exc ->
2296 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exc
2298 TEswitch ("thumbnail width: ", E.s, None, intentry, ondone, true)
2300 | 'R' ->
2301 let ondone s =
2302 match try
2303 Some (int_of_string s)
2304 with exc ->
2305 state.text <-
2306 Printf.sprintf "bad integer `%s': %s" s @@ exntos exc;
2307 None
2308 with
2309 | Some angle -> reqlayout angle conf.fitmodel
2310 | None -> ()
2312 TEswitch ("rotation: ", E.s, None, intentry, ondone, true)
2314 | 'i' ->
2315 conf.icase <- not conf.icase;
2316 TEdone ("case insensitive search " ^ (btos conf.icase))
2318 | 'p' ->
2319 conf.preload <- not conf.preload;
2320 gotoy state.y;
2321 TEdone ("preload " ^ (btos conf.preload))
2323 | 'v' ->
2324 conf.verbose <- not conf.verbose;
2325 TEdone ("verbose " ^ (btos conf.verbose))
2327 | 'd' ->
2328 conf.debug <- not conf.debug;
2329 TEdone ("debug " ^ (btos conf.debug))
2331 | 'h' ->
2332 conf.maxhfit <- not conf.maxhfit;
2333 state.maxy <- calcheight ();
2334 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2336 | 'c' ->
2337 conf.crophack <- not conf.crophack;
2338 TEdone ("crophack " ^ btos conf.crophack)
2340 | 'a' ->
2341 let s =
2342 match conf.maxwait with
2343 | None ->
2344 conf.maxwait <- Some infinity;
2345 "always wait for page to complete"
2346 | Some _ ->
2347 conf.maxwait <- None;
2348 "show placeholder if page is not ready"
2350 TEdone s
2352 | 'f' ->
2353 conf.underinfo <- not conf.underinfo;
2354 TEdone ("underinfo " ^ btos conf.underinfo)
2356 | 'P' ->
2357 conf.savebmarks <- not conf.savebmarks;
2358 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2360 | 'S' ->
2361 let ondone s =
2363 let pageno, py =
2364 match state.layout with
2365 | [] -> 0, 0
2366 | l :: _ ->
2367 l.pageno, l.pagey
2369 conf.interpagespace <- int_of_string s;
2370 docolumns conf.columns;
2371 state.maxy <- calcheight ();
2372 let y = getpagey pageno in
2373 gotoy (y + py)
2374 with exc ->
2375 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exc
2377 TEswitch ("vertical margin: ", E.s, None, intentry, ondone, true)
2379 | 'l' ->
2380 let fm =
2381 match conf.fitmodel with
2382 | FitProportional -> FitWidth
2383 | FitWidth | FitPage -> FitProportional
2385 reqlayout conf.angle fm;
2386 TEdone ("proportional display " ^ btos (fm == FitProportional))
2388 | 'T' ->
2389 settrim (not conf.trimmargins) conf.trimfuzz;
2390 TEdone ("trim margins " ^ btos conf.trimmargins)
2392 | 'I' ->
2393 conf.invert <- not conf.invert;
2394 TEdone ("invert colors " ^ btos conf.invert)
2396 | 'x' ->
2397 let ondone s =
2398 cbput state.hists.sel s;
2399 conf.selcmd <- s;
2401 TEswitch ("selection command: ", E.s, Some (onhist state.hists.sel),
2402 textentry, ondone, true)
2404 | 'M' ->
2405 if conf.pax == None
2406 then conf.pax <- Some (ref (0.0, 0, 0))
2407 else conf.pax <- None;
2408 TEdone ("PAX " ^ btos (conf.pax != None))
2410 | _ ->
2411 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2412 TEstop
2413 else
2414 TEcont state.text
2417 class type lvsource = object
2418 method getitemcount : int
2419 method getitem : int -> (string * int)
2420 method hasaction : int -> bool
2421 method exit :
2422 uioh:uioh ->
2423 cancel:bool ->
2424 active:int ->
2425 first:int ->
2426 pan:int ->
2427 uioh option
2428 method getactive : int
2429 method getfirst : int
2430 method getpan : int
2431 method getminfo : (int * int) array
2432 end;;
2434 class virtual lvsourcebase = object
2435 val mutable m_active = 0
2436 val mutable m_first = 0
2437 val mutable m_pan = 0
2438 method getactive = m_active
2439 method getfirst = m_first
2440 method getpan = m_pan
2441 method getminfo : (int * int) array = E.a
2442 end;;
2444 let textentrykeyboard
2445 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2446 state.text <- E.s;
2447 let key =
2448 if key >= 0xffb0 && key <= 0xffb9
2449 then key - 0xffb0 + 48 else key
2451 let enttext te =
2452 state.mode <- Textentry (te, onleave);
2453 enttext ();
2454 G.postRedisplay "textentrykeyboard enttext";
2456 let histaction cmd =
2457 match opthist with
2458 | None -> ()
2459 | Some (action, _) ->
2460 state.mode <- Textentry (
2461 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
2463 G.postRedisplay "textentry histaction"
2465 match key with
2466 | @backspace ->
2467 if emptystr text && cancelonempty
2468 then (
2469 onleave Cancel;
2470 G.postRedisplay "textentrykeyboard after cancel";
2472 else
2473 let s = withoutlastutf8 text in
2474 enttext (c, s, opthist, onkey, ondone, cancelonempty)
2476 | @enter | @kpenter ->
2477 ondone text;
2478 onleave Confirm;
2479 G.postRedisplay "textentrykeyboard after confirm"
2481 | @up | @kpup -> histaction HCprev
2482 | @down | @kpdown -> histaction HCnext
2483 | @home | @kphome -> histaction HCfirst
2484 | @jend | @kpend -> histaction HClast
2486 | @escape ->
2487 if emptystr text
2488 then (
2489 begin match opthist with
2490 | None -> ()
2491 | Some (_, onhistcancel) -> onhistcancel ()
2492 end;
2493 onleave Cancel;
2494 state.text <- E.s;
2495 G.postRedisplay "textentrykeyboard after cancel2"
2497 else (
2498 enttext (c, E.s, opthist, onkey, ondone, cancelonempty)
2501 | @delete | @kpdelete -> ()
2503 | _ when key != 0
2504 && key land 0xff00 != 0xff00 (* keyboard *)
2505 && key land 0xfe00 != 0xfe00 (* xkb *)
2506 && key land 0xfd00 != 0xfd00 (* 3270 *)
2508 begin match onkey text key with
2509 | TEdone text ->
2510 ondone text;
2511 onleave Confirm;
2512 G.postRedisplay "textentrykeyboard after confirm2";
2514 | TEcont text ->
2515 enttext (c, text, opthist, onkey, ondone, cancelonempty);
2517 | TEstop ->
2518 onleave Cancel;
2519 G.postRedisplay "textentrykeyboard after cancel3"
2521 | TEswitch te ->
2522 state.mode <- Textentry (te, onleave);
2523 G.postRedisplay "textentrykeyboard switch";
2524 end;
2526 | _ ->
2527 vlog "unhandled key %s" (Wsi.keyname key)
2530 let firstof first active =
2531 if first > active || abs (first - active) > fstate.maxrows - 1
2532 then max 0 (active - (fstate.maxrows/2))
2533 else first
2536 let calcfirst first active =
2537 if active > first
2538 then
2539 let rows = active - first in
2540 if rows > fstate.maxrows then active - fstate.maxrows else first
2541 else active
2544 let scrollph y maxy =
2545 let sh = float (maxy + state.winh) /. float state.winh in
2546 let sh = float state.winh /. sh in
2547 let sh = max sh (float conf.scrollh) in
2549 let percent = float y /. float maxy in
2550 let position = (float state.winh -. sh) *. percent in
2552 let position =
2553 if position +. sh > float state.winh
2554 then float state.winh -. sh
2555 else position
2557 position, sh;
2560 let coe s = (s :> uioh);;
2562 class listview ~zebra ~helpmode ~(source:lvsource) ~trusted ~modehash =
2563 object (self)
2564 val m_pan = source#getpan
2565 val m_first = source#getfirst
2566 val m_active = source#getactive
2567 val m_qsearch = E.s
2568 val m_prev_uioh = state.uioh
2570 method private elemunder y =
2571 if y < 0
2572 then None
2573 else
2574 let n = y / (fstate.fontsize+1) in
2575 if m_first + n < source#getitemcount
2576 then (
2577 if source#hasaction (m_first + n)
2578 then Some (m_first + n)
2579 else None
2581 else None
2583 method display =
2584 Gl.enable `blend;
2585 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
2586 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2587 filledrect 0. 0. (float state.winw) (float state.winh);
2588 GlDraw.color (1., 1., 1.);
2589 Gl.enable `texture_2d;
2590 let fs = fstate.fontsize in
2591 let nfs = fs + 1 in
2592 let hw = (wadjsb () + xadjsb () + state.winw)/3 in
2593 let ww = fstate.wwidth in
2594 let tabw = 17.0*.ww in
2595 let itemcount = source#getitemcount in
2596 let minfo = source#getminfo in
2597 let x0, x1 =
2598 if conf.leftscroll
2599 then float (xadjsb ()), float (state.winw - 1)
2600 else 0.0, float (state.winw - conf.scrollbw - 1)
2602 let xadj = xadjsb () in
2603 let rec loop row =
2604 if (row - m_first) > fstate.maxrows
2605 then ()
2606 else (
2607 if row >= 0 && row < itemcount
2608 then (
2609 let (s, level) = source#getitem row in
2610 let y = (row - m_first) * nfs in
2611 let x =
2612 (if conf.leftscroll then float xadj else 5.0)
2613 +. (float (level + m_pan)) *. ww in
2614 if helpmode
2615 then GlDraw.color
2616 (let c = if row land 1 = 0 then 1.0 else 0.92 in (c,c,c));
2618 if row = m_active
2619 then (
2620 Gl.disable `texture_2d;
2621 let alpha = if source#hasaction row then 0.9 else 0.3 in
2622 GlDraw.color (1., 1., 1.) ~alpha;
2623 linerect (x0 +. 1.) (float (y + 1)) (x1) (float (y + fs + 3));
2624 Gl.enable `texture_2d;
2626 let c =
2627 if zebra && row land 1 = 1
2628 then 0.8
2629 else 1.0
2631 GlDraw.color (c,c,c);
2632 let drawtabularstring s =
2633 let drawstr x s =
2634 let x' = truncate (x0 +. x) in
2635 let pos = nindex s '\000' in
2636 if pos = -1
2637 then drawstring1 fs x' (y+nfs) s
2638 else
2639 let s1 = String.sub s 0 pos
2640 and s2 = String.sub s (pos+1) (String.length s - pos - 1) in
2641 let rec e s =
2642 if emptystr s
2643 then s
2644 else
2645 let s' = withoutlastutf8 s in
2646 let s = s' ^ "@Uellipsis" in
2647 let w = measurestr fs s in
2648 if float x' +. w +. ww < float (hw + x')
2649 then s
2650 else e s'
2652 let s1 =
2653 if float x' +. ww +. measurestr fs s1 > float (hw + x')
2654 then e s1
2655 else s1
2657 ignore (drawstring1 fs x' (y+nfs) s1);
2658 drawstring1 fs (hw + x') (y+nfs) s2
2660 if trusted
2661 then
2662 let x = if helpmode && row > 0 then x +. ww else x in
2663 let tabpos = nindex s '\t' in
2664 if tabpos > 0
2665 then
2666 let len = String.length s - tabpos - 1 in
2667 let s1 = String.sub s 0 tabpos
2668 and s2 = String.sub s (tabpos + 1) len in
2669 let nx = drawstr x s1 in
2670 let sw = nx -. x in
2671 let x = x +. (max tabw sw) in
2672 drawstr x s2
2673 else
2674 let len = String.length s - 2 in
2675 if len > 0 && s.[0] = '\xc2' && s.[1] = '\xb7'
2676 then
2677 let s = String.sub s 2 len in
2678 let x = if not helpmode then x +. ww else x in
2679 GlDraw.color (1.2, 1.2, 1.2);
2680 let vinc = drawstring1 (fs+fs/4)
2681 (truncate (x -. ww)) (y+nfs) s in
2682 GlDraw.color (1., 1., 1.);
2683 vinc +. (float fs *. 0.8)
2684 else
2685 drawstr x s
2686 else
2687 drawstr x s
2689 ignore (drawtabularstring s);
2690 loop (row+1)
2694 loop m_first;
2695 GlDraw.color (1.0, 1.0, 1.0) ~alpha:0.5;
2696 let xadj = float (xadjsb () + 5) in
2697 let rec loop row =
2698 if (row - m_first) > fstate.maxrows
2699 then ()
2700 else (
2701 if row >= 0 && row < itemcount
2702 then (
2703 let (s, level) = source#getitem row in
2704 let pos0 = nindex s '\000' in
2705 let y = (row - m_first) * nfs in
2706 let x = float (level + m_pan) *. ww in
2707 let (first, last) = minfo.(row) in
2708 let prefix =
2709 if pos0 > 0 && first > pos0
2710 then String.sub s (pos0+1) (first-pos0-1)
2711 else String.sub s 0 first
2713 let suffix = String.sub s first (last - first) in
2714 let w1 = measurestr fstate.fontsize prefix in
2715 let w2 = measurestr fstate.fontsize suffix in
2716 let x = x +. if conf.leftscroll then xadj else 5.0 in
2717 let x = if pos0 > 0 && first > pos0 then x +. float hw else x in
2718 let x0 = x +. w1
2719 and y0 = float (y+2) in
2720 let x1 = x0 +. w2
2721 and y1 = float (y+fs+3) in
2722 filledrect x0 y0 x1 y1;
2723 loop (row+1)
2727 Gl.disable `texture_2d;
2728 if Array.length minfo > 0 then loop m_first;
2729 Gl.disable `blend;
2731 method updownlevel incr =
2732 let len = source#getitemcount in
2733 let curlevel =
2734 if m_active >= 0 && m_active < len
2735 then snd (source#getitem m_active)
2736 else -1
2738 let rec flow i =
2739 if i = len then i-1 else if i = -1 then 0 else
2740 let _, l = source#getitem i in
2741 if l != curlevel then i else flow (i+incr)
2743 let active = flow m_active in
2744 let first = calcfirst m_first active in
2745 G.postRedisplay "outline updownlevel";
2746 {< m_active = active; m_first = first >}
2748 method private key1 key mask =
2749 let set1 active first qsearch =
2750 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2752 let search active pattern incr =
2753 let active = if active = -1 then m_first else active in
2754 let dosearch re =
2755 let rec loop n =
2756 if n >= 0 && n < source#getitemcount
2757 then (
2758 let s, _ = source#getitem n in
2759 match Str.search_forward re s 0 with
2760 | (exception Not_found) -> loop (n + incr)
2761 | _ -> Some n
2763 else None
2765 loop active
2767 Str.regexp_case_fold pattern |> dosearch
2769 let itemcount = source#getitemcount in
2770 let find start incr =
2771 let rec find i =
2772 if i = -1 || i = itemcount
2773 then -1
2774 else (
2775 if source#hasaction i
2776 then i
2777 else find (i + incr)
2780 find start
2782 let set active first =
2783 let first = bound first 0 (itemcount - fstate.maxrows) in
2784 state.text <- E.s;
2785 coe {< m_active = active; m_first = first; m_qsearch = E.s >}
2787 let navigate incr =
2788 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2789 let active, first =
2790 let incr1 = if incr > 0 then 1 else -1 in
2791 if isvisible m_first m_active
2792 then
2793 let next =
2794 let next = m_active + incr in
2795 let next =
2796 if next < 0 || next >= itemcount
2797 then -1
2798 else find next incr1
2800 if abs (m_active - next) > fstate.maxrows
2801 then -1
2802 else next
2804 if next = -1
2805 then
2806 let first = m_first + incr in
2807 let first = bound first 0 (itemcount - fstate.maxrows) in
2808 let next =
2809 let next = m_active + incr in
2810 let next = bound next 0 (itemcount - 1) in
2811 find next ~-incr1
2813 let active =
2814 if next = -1
2815 then m_active
2816 else (
2817 if isvisible first next
2818 then next
2819 else m_active
2822 active, first
2823 else
2824 let first = min next m_first in
2825 let first =
2826 if abs (next - first) > fstate.maxrows
2827 then first + incr
2828 else first
2830 next, first
2831 else
2832 let first = m_first + incr in
2833 let first = bound first 0 (itemcount - 1) in
2834 let active =
2835 let next = m_active + incr in
2836 let next = bound next 0 (itemcount - 1) in
2837 let next = find next incr1 in
2838 let active =
2839 if next = -1 || abs (m_active - first) > fstate.maxrows
2840 then (
2841 let active = if m_active = -1 then next else m_active in
2842 active
2844 else next
2846 if isvisible first active
2847 then active
2848 else -1
2850 active, first
2852 G.postRedisplay "listview navigate";
2853 set active first;
2855 match key with
2856 | (@r|@s) when Wsi.withctrl mask ->
2857 let incr = if key = @r then -1 else 1 in
2858 let active, first =
2859 match search (m_active + incr) m_qsearch incr with
2860 | None ->
2861 state.text <- m_qsearch ^ " [not found]";
2862 m_active, m_first
2863 | Some active ->
2864 state.text <- m_qsearch;
2865 active, firstof m_first active
2867 G.postRedisplay "listview ctrl-r/s";
2868 set1 active first m_qsearch;
2870 | @insert when Wsi.withctrl mask ->
2871 if m_active >= 0 && m_active < source#getitemcount
2872 then (
2873 let s, _ = source#getitem m_active in
2874 selstring s;
2876 coe self
2878 | @backspace ->
2879 if emptystr m_qsearch
2880 then coe self
2881 else (
2882 let qsearch = withoutlastutf8 m_qsearch in
2883 if emptystr qsearch
2884 then (
2885 state.text <- E.s;
2886 G.postRedisplay "listview empty qsearch";
2887 set1 m_active m_first E.s;
2889 else
2890 let active, first =
2891 match search m_active qsearch ~-1 with
2892 | None ->
2893 state.text <- qsearch ^ " [not found]";
2894 m_active, m_first
2895 | Some active ->
2896 state.text <- qsearch;
2897 active, firstof m_first active
2899 G.postRedisplay "listview backspace qsearch";
2900 set1 active first qsearch
2903 | key when (key != 0 && key land 0xff00 != 0xff00) ->
2904 let pattern = m_qsearch ^ toutf8 key in
2905 let active, first =
2906 match search m_active pattern 1 with
2907 | None ->
2908 state.text <- pattern ^ " [not found]";
2909 m_active, m_first
2910 | Some active ->
2911 state.text <- pattern;
2912 active, firstof m_first active
2914 G.postRedisplay "listview qsearch add";
2915 set1 active first pattern;
2917 | @escape ->
2918 state.text <- E.s;
2919 if emptystr m_qsearch
2920 then (
2921 G.postRedisplay "list view escape";
2922 let mx, my = state.mpos in
2923 updateunder mx my;
2924 begin
2925 match
2926 source#exit ~uioh:(coe self)
2927 ~cancel:true ~active:m_active ~first:m_first ~pan:m_pan
2928 with
2929 | None -> m_prev_uioh
2930 | Some uioh -> uioh
2933 else (
2934 G.postRedisplay "list view kill qsearch";
2935 coe {< m_qsearch = E.s >}
2938 | @enter | @kpenter ->
2939 state.text <- E.s;
2940 let self = {< m_qsearch = E.s >} in
2941 let opt =
2942 G.postRedisplay "listview enter";
2943 if m_active >= 0 && m_active < source#getitemcount
2944 then (
2945 source#exit ~uioh:(coe self) ~cancel:false
2946 ~active:m_active ~first:m_first ~pan:m_pan;
2948 else (
2949 source#exit ~uioh:(coe self) ~cancel:true
2950 ~active:m_active ~first:m_first ~pan:m_pan;
2953 begin match opt with
2954 | None -> m_prev_uioh
2955 | Some uioh -> uioh
2958 | @delete | @kpdelete ->
2959 coe self
2961 | @up | @kpup -> navigate ~-1
2962 | @down | @kpdown -> navigate 1
2963 | @prior | @kpprior -> navigate ~-(fstate.maxrows)
2964 | @next | @kpnext -> navigate fstate.maxrows
2966 | @right | @kpright ->
2967 state.text <- E.s;
2968 G.postRedisplay "listview right";
2969 coe {< m_pan = m_pan - 1 >}
2971 | @left | @kpleft ->
2972 state.text <- E.s;
2973 G.postRedisplay "listview left";
2974 coe {< m_pan = m_pan + 1 >}
2976 | @home | @kphome ->
2977 let active = find 0 1 in
2978 G.postRedisplay "listview home";
2979 set active 0;
2981 | @jend | @kpend ->
2982 let first = max 0 (itemcount - fstate.maxrows) in
2983 let active = find (itemcount - 1) ~-1 in
2984 G.postRedisplay "listview end";
2985 set active first;
2987 | key when (key = 0 || key land 0xff00 = 0xff00) ->
2988 coe self
2990 | _ ->
2991 dolog "listview unknown key %#x" key; coe self
2993 method key key mask =
2994 match state.mode with
2995 | Textentry te -> textentrykeyboard key mask te; coe self
2996 | Birdseye _
2997 | View
2998 | LinkNav _ -> self#key1 key mask
3000 method button button down x y _ =
3001 let opt =
3002 match button with
3003 | 1 when vscrollhit x ->
3004 G.postRedisplay "listview scroll";
3005 if down
3006 then
3007 let _, position, sh = self#scrollph in
3008 if y > truncate position && y < truncate (position +. sh)
3009 then (
3010 state.mstate <- Mscrolly;
3011 Some (coe self)
3013 else
3014 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3015 let first = truncate (s *. float source#getitemcount) in
3016 let first = min source#getitemcount first in
3017 Some (coe {< m_first = first; m_active = first >})
3018 else (
3019 state.mstate <- Mnone;
3020 Some (coe self);
3022 | 1 when down ->
3023 begin match self#elemunder y with
3024 | Some n ->
3025 G.postRedisplay "listview click";
3026 source#exit ~uioh:(coe {< m_active = n >})
3027 ~cancel:false ~active:n ~first:m_first ~pan:m_pan
3028 | _ ->
3029 Some (coe self)
3031 | n when (n == 4 || n == 5) && not down ->
3032 let len = source#getitemcount in
3033 let first =
3034 if n = 5 && m_first + fstate.maxrows >= len
3035 then
3036 m_first
3037 else
3038 let first = m_first + (if n == 4 then -1 else 1) in
3039 bound first 0 (len - 1)
3041 G.postRedisplay "listview wheel";
3042 Some (coe {< m_first = first >})
3043 | n when (n = 6 || n = 7) && not down ->
3044 let inc = if n = 7 then -1 else 1 in
3045 G.postRedisplay "listview hwheel";
3046 Some (coe {< m_pan = m_pan + inc >})
3047 | _ ->
3048 Some (coe self)
3050 match opt with
3051 | None -> m_prev_uioh
3052 | Some uioh -> uioh
3054 method multiclick _ x y = self#button 1 true x y
3056 method motion _ y =
3057 match state.mstate with
3058 | Mscrolly ->
3059 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3060 let first = truncate (s *. float source#getitemcount) in
3061 let first = min source#getitemcount first in
3062 G.postRedisplay "listview motion";
3063 coe {< m_first = first; m_active = first >}
3064 | Msel _
3065 | Mpan _
3066 | Mscrollx
3067 | Mzoom _
3068 | Mzoomrect _
3069 | Mnone -> coe self
3071 method pmotion x y =
3072 if x < state.winw - conf.scrollbw
3073 then
3074 let n =
3075 match self#elemunder y with
3076 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3077 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3079 let o =
3080 if n != m_active
3081 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3082 else self
3084 coe o
3085 else (
3086 Wsi.setcursor Wsi.CURSOR_INHERIT;
3087 coe self
3090 method infochanged _ = ()
3092 method scrollpw = (0, 0.0, 0.0)
3093 method scrollph =
3094 let nfs = fstate.fontsize + 1 in
3095 let y = m_first * nfs in
3096 let itemcount = source#getitemcount in
3097 let maxi = max 0 (itemcount - fstate.maxrows) in
3098 let maxy = maxi * nfs in
3099 let p, h = scrollph y maxy in
3100 conf.scrollbw, p, h
3102 method modehash = modehash
3103 method eformsgs = false
3104 method alwaysscrolly = true
3105 end;;
3107 class outlinelistview ~zebra ~source =
3108 let settext autonarrow s =
3109 if autonarrow
3110 then
3111 let ss = source#statestr in
3112 state.text <-
3113 if emptystr ss
3114 then "[" ^ s ^ "]"
3115 else "{" ^ ss ^ "} [" ^ s ^ "]"
3116 else state.text <- s
3118 object (self)
3119 inherit listview
3120 ~zebra
3121 ~helpmode:false
3122 ~source:(source :> lvsource)
3123 ~trusted:false
3124 ~modehash:(findkeyhash conf "outline")
3125 as super
3127 val m_autonarrow = false
3129 method! key key mask =
3130 let maxrows =
3131 if emptystr state.text
3132 then fstate.maxrows
3133 else fstate.maxrows - 2
3135 let calcfirst first active =
3136 if active > first
3137 then
3138 let rows = active - first in
3139 if rows > maxrows then active - maxrows else first
3140 else active
3142 let navigate incr =
3143 let active = m_active + incr in
3144 let active = bound active 0 (source#getitemcount - 1) in
3145 let first = calcfirst m_first active in
3146 G.postRedisplay "outline navigate";
3147 coe {< m_active = active; m_first = first >}
3149 let navscroll first =
3150 let active =
3151 let dist = m_active - first in
3152 if dist < 0
3153 then first
3154 else (
3155 if dist < maxrows
3156 then m_active
3157 else first + maxrows
3160 G.postRedisplay "outline navscroll";
3161 coe {< m_first = first; m_active = active >}
3163 let ctrl = Wsi.withctrl mask in
3164 match key with
3165 | @a when ctrl ->
3166 let text =
3167 if m_autonarrow
3168 then (source#denarrow; E.s)
3169 else (
3170 let pattern = source#renarrow in
3171 if nonemptystr m_qsearch
3172 then (source#narrow m_qsearch; m_qsearch)
3173 else pattern
3176 settext (not m_autonarrow) text;
3177 G.postRedisplay "toggle auto narrowing";
3178 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
3180 | @slash when emptystr m_qsearch && not m_autonarrow ->
3181 settext true E.s;
3182 G.postRedisplay "toggle auto narrowing";
3183 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
3185 | @n when ctrl ->
3186 source#narrow m_qsearch;
3187 if not m_autonarrow
3188 then source#add_narrow_pattern m_qsearch;
3189 G.postRedisplay "outline ctrl-n";
3190 coe {< m_first = 0; m_active = 0 >}
3192 | @S when ctrl ->
3193 let active = source#calcactive (getanchor ()) in
3194 let first = firstof m_first active in
3195 G.postRedisplay "outline ctrl-s";
3196 coe {< m_first = first; m_active = active >}
3198 | @u when ctrl ->
3199 G.postRedisplay "outline ctrl-u";
3200 if m_autonarrow && nonemptystr m_qsearch
3201 then (
3202 ignore (source#renarrow);
3203 settext m_autonarrow E.s;
3204 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3206 else (
3207 source#del_narrow_pattern;
3208 let pattern = source#renarrow in
3209 let text =
3210 if emptystr pattern then E.s else "Narrowed to " ^ pattern
3212 settext m_autonarrow text;
3213 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3216 | @l when ctrl ->
3217 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3218 G.postRedisplay "outline ctrl-l";
3219 coe {< m_first = first >}
3221 | @tab when m_autonarrow ->
3222 if nonemptystr m_qsearch
3223 then (
3224 G.postRedisplay "outline list view tab";
3225 source#add_narrow_pattern m_qsearch;
3226 settext true E.s;
3227 coe {< m_qsearch = E.s >}
3229 else coe self
3231 | @escape when m_autonarrow ->
3232 if nonemptystr m_qsearch
3233 then source#add_narrow_pattern m_qsearch;
3234 super#key key mask
3236 | @enter | @kpenter when m_autonarrow ->
3237 if nonemptystr m_qsearch
3238 then source#add_narrow_pattern m_qsearch;
3239 super#key key mask
3241 | key when m_autonarrow && (key != 0 && key land 0xff00 != 0xff00) ->
3242 let pattern = m_qsearch ^ toutf8 key in
3243 G.postRedisplay "outlinelistview autonarrow add";
3244 source#narrow pattern;
3245 settext true pattern;
3246 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3248 | key when m_autonarrow && key = @backspace ->
3249 if emptystr m_qsearch
3250 then coe self
3251 else
3252 let pattern = withoutlastutf8 m_qsearch in
3253 G.postRedisplay "outlinelistview autonarrow backspace";
3254 ignore (source#renarrow);
3255 source#narrow pattern;
3256 settext true pattern;
3257 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3259 | @up | @kpup when ctrl ->
3260 navscroll (max 0 (m_first - 1))
3262 | @down | @kpdown when ctrl ->
3263 navscroll (min (source#getitemcount - 1) (m_first + 1))
3265 | @up | @kpup -> navigate ~-1
3266 | @down | @kpdown -> navigate 1
3267 | @prior | @kpprior -> navigate ~-(fstate.maxrows)
3268 | @next | @kpnext -> navigate fstate.maxrows
3270 | @right | @kpright ->
3271 let o =
3272 if ctrl
3273 then (
3274 G.postRedisplay "outline ctrl right";
3275 {< m_pan = m_pan + 1 >}
3277 else self#updownlevel 1
3279 coe o
3281 | @left | @kpleft ->
3282 let o =
3283 if ctrl
3284 then (
3285 G.postRedisplay "outline ctrl left";
3286 {< m_pan = m_pan - 1 >}
3288 else self#updownlevel ~-1
3290 coe o
3292 | @home | @kphome ->
3293 G.postRedisplay "outline home";
3294 coe {< m_first = 0; m_active = 0 >}
3296 | @jend | @kpend ->
3297 let active = source#getitemcount - 1 in
3298 let first = max 0 (active - fstate.maxrows) in
3299 G.postRedisplay "outline end";
3300 coe {< m_active = active; m_first = first >}
3302 | _ -> super#key key mask
3303 end;;
3305 let genhistoutlines () =
3306 Config.gethist ()
3307 |> List.sort (fun (_, c1, _, _, _, _) (_, c2, _, _, _, _) ->
3308 compare c2.lastvisit c1.lastvisit)
3309 |> List.map
3310 (fun ((path, c, _, _, _, origin) as hist) ->
3311 let path = if nonemptystr origin then origin else path in
3312 let base = mbtoutf8 @@ Filename.basename path in
3313 (base ^ "\000" ^ c.title, 1, Ohistory hist)
3315 |> Array.of_list
3318 let gotohist (path, c, bookmarks, x, anchor, origin) =
3319 Config.save leavebirdseye;
3320 state.anchor <- anchor;
3321 state.bookmarks <- bookmarks;
3322 state.origin <- origin;
3323 state.x <- x;
3324 setconf conf c;
3325 let x0, y0, x1, y1 = conf.trimfuzz in
3326 wcmd "trimset %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1;
3327 reshape ~firsttime:true state.winw state.winh;
3328 opendoc path origin;
3329 setzoom c.zoom;
3332 let makecheckers () =
3333 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3334 following to say:
3335 converted by Issac Trotts. July 25, 2002 *)
3336 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3337 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3338 let id = GlTex.gen_texture () in
3339 GlTex.bind_texture ~target:`texture_2d id;
3340 GlPix.store (`unpack_alignment 1);
3341 GlTex.image2d image;
3342 List.iter (GlTex.parameter ~target:`texture_2d)
3343 [ `mag_filter `nearest; `min_filter `nearest ];
3347 let setcheckers enabled =
3348 match state.checkerstexid with
3349 | None ->
3350 if enabled then state.checkerstexid <- Some (makecheckers ())
3352 | Some checkerstexid ->
3353 if not enabled
3354 then (
3355 GlTex.delete_texture checkerstexid;
3356 state.checkerstexid <- None;
3360 let describe_location () =
3361 let fn = page_of_y state.y in
3362 let ln = page_of_y (state.y + state.winh - hscrollh () - 1) in
3363 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
3364 let percent =
3365 if maxy <= 0
3366 then 100.
3367 else (100. *. (float state.y /. float maxy))
3369 if fn = ln
3370 then
3371 Printf.sprintf "page %d of %d [%.2f%%]"
3372 (fn+1) state.pagecount percent
3373 else
3374 Printf.sprintf
3375 "pages %d-%d of %d [%.2f%%]"
3376 (fn+1) (ln+1) state.pagecount percent
3379 let setpresentationmode v =
3380 let n = page_of_y state.y in
3381 state.anchor <- (n, 0.0, 1.0);
3382 conf.presentation <- v;
3383 if conf.fitmodel = FitPage
3384 then reqlayout conf.angle conf.fitmodel;
3385 represent ();
3388 let enterinfomode =
3389 let btos b = if b then "@Uradical" else E.s in
3390 let showextended = ref false in
3391 let leave mode _ = state.mode <- mode in
3392 let src =
3393 (object
3394 val mutable m_l = []
3395 val mutable m_a = E.a
3396 val mutable m_prev_uioh = nouioh
3397 val mutable m_prev_mode = View
3399 inherit lvsourcebase
3401 method reset prev_mode prev_uioh =
3402 m_a <- Array.of_list (List.rev m_l);
3403 m_l <- [];
3404 m_prev_mode <- prev_mode;
3405 m_prev_uioh <- prev_uioh;
3407 method int name get set =
3408 m_l <-
3409 (name, `int get, 1, Action (
3410 fun u ->
3411 let ondone s =
3412 try set (int_of_string s)
3413 with exn ->
3414 state.text <- Printf.sprintf "bad integer `%s': %s"
3415 s @@ exntos exn
3417 state.text <- E.s;
3418 let te = name ^ ": ", E.s, None, intentry, ondone, true in
3419 state.mode <- Textentry (te, leave m_prev_mode);
3421 )) :: m_l
3423 method int_with_suffix name get set =
3424 m_l <-
3425 (name, `intws get, 1, Action (
3426 fun u ->
3427 let ondone s =
3428 try set (int_of_string_with_suffix s)
3429 with exn ->
3430 state.text <- Printf.sprintf "bad integer `%s': %s"
3431 s @@ exntos exn
3433 state.text <- E.s;
3434 let te =
3435 name ^ ": ", E.s, None, intentry_with_suffix, ondone, true
3437 state.mode <- Textentry (te, leave m_prev_mode);
3439 )) :: m_l
3441 method bool ?(offset=1) ?(btos=btos) name get set =
3442 m_l <-
3443 (name, `bool (btos, get), offset, Action (
3444 fun u ->
3445 let v = get () in
3446 set (not v);
3448 )) :: m_l
3450 method color name get set =
3451 m_l <-
3452 (name, `color get, 1, Action (
3453 fun u ->
3454 let invalid = (nan, nan, nan) in
3455 let ondone s =
3456 let c =
3457 try color_of_string s
3458 with exn ->
3459 state.text <- Printf.sprintf "bad color `%s': %s"
3460 s @@ exntos exn;
3461 invalid
3463 if c <> invalid
3464 then set c;
3466 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3467 state.text <- color_to_string (get ());
3468 state.mode <- Textentry (te, leave m_prev_mode);
3470 )) :: m_l
3472 method string name get set =
3473 m_l <-
3474 (name, `string get, 1, Action (
3475 fun u ->
3476 let ondone s = set s in
3477 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3478 state.mode <- Textentry (te, leave m_prev_mode);
3480 )) :: m_l
3482 method colorspace name get set =
3483 m_l <-
3484 (name, `string get, 1, Action (
3485 fun _ ->
3486 let source =
3487 (object
3488 inherit lvsourcebase
3490 initializer
3491 m_active <- CSTE.to_int conf.colorspace;
3492 m_first <- 0;
3494 method getitemcount =
3495 Array.length CSTE.names
3496 method getitem n =
3497 (CSTE.names.(n), 0)
3498 method exit ~uioh ~cancel ~active ~first ~pan =
3499 ignore (uioh, first, pan);
3500 if not cancel then set active;
3501 None
3502 method hasaction _ = true
3503 end)
3505 state.text <- E.s;
3506 let modehash = findkeyhash conf "info" in
3507 coe (new listview ~zebra:false ~helpmode:false
3508 ~source ~trusted:true ~modehash)
3509 )) :: m_l
3511 method paxmark name get set =
3512 m_l <-
3513 (name, `string get, 1, Action (
3514 fun _ ->
3515 let source =
3516 (object
3517 inherit lvsourcebase
3519 initializer
3520 m_active <- MTE.to_int conf.paxmark;
3521 m_first <- 0;
3523 method getitemcount = Array.length MTE.names
3524 method getitem n = (MTE.names.(n), 0)
3525 method exit ~uioh ~cancel ~active ~first ~pan =
3526 ignore (uioh, first, pan);
3527 if not cancel then set active;
3528 None
3529 method hasaction _ = true
3530 end)
3532 state.text <- E.s;
3533 let modehash = findkeyhash conf "info" in
3534 coe (new listview ~zebra:false ~helpmode:false
3535 ~source ~trusted:true ~modehash)
3536 )) :: m_l
3538 method fitmodel name get set =
3539 m_l <-
3540 (name, `string get, 1, Action (
3541 fun _ ->
3542 let source =
3543 (object
3544 inherit lvsourcebase
3546 initializer
3547 m_active <- FMTE.to_int conf.fitmodel;
3548 m_first <- 0;
3550 method getitemcount = Array.length FMTE.names
3551 method getitem n = (FMTE.names.(n), 0)
3552 method exit ~uioh ~cancel ~active ~first ~pan =
3553 ignore (uioh, first, pan);
3554 if not cancel then set active;
3555 None
3556 method hasaction _ = true
3557 end)
3559 state.text <- E.s;
3560 let modehash = findkeyhash conf "info" in
3561 coe (new listview ~zebra:false ~helpmode:false
3562 ~source ~trusted:true ~modehash)
3563 )) :: m_l
3565 method caption s offset =
3566 m_l <- (s, `empty, offset, Noaction) :: m_l
3568 method caption2 s f offset =
3569 m_l <- (s, `string f, offset, Noaction) :: m_l
3571 method getitemcount = Array.length m_a
3573 method getitem n =
3574 let tostr = function
3575 | `int f -> string_of_int (f ())
3576 | `intws f -> string_with_suffix_of_int (f ())
3577 | `string f -> f ()
3578 | `color f -> color_to_string (f ())
3579 | `bool (btos, f) -> btos (f ())
3580 | `empty -> E.s
3582 let name, t, offset, _ = m_a.(n) in
3583 ((let s = tostr t in
3584 if nonemptystr s
3585 then Printf.sprintf "%s\t%s" name s
3586 else name),
3587 offset)
3589 method exit ~uioh ~cancel ~active ~first ~pan =
3590 let uiohopt =
3591 if not cancel
3592 then (
3593 let uioh =
3594 match m_a.(active) with
3595 | _, _, _, Action f -> f uioh
3596 | _, _, _, Noaction -> uioh
3598 Some uioh
3600 else None
3602 m_active <- active;
3603 m_first <- first;
3604 m_pan <- pan;
3605 uiohopt
3607 method hasaction n =
3608 match m_a.(n) with
3609 | _, _, _, Action _ -> true
3610 | _, _, _, Noaction -> false
3612 initializer m_active <- 1
3613 end)
3615 let rec fillsrc prevmode prevuioh =
3616 let sep () = src#caption E.s 0 in
3617 let colorp name get set =
3618 src#string name
3619 (fun () -> color_to_string (get ()))
3620 (fun v ->
3622 let c = color_of_string v in
3623 set c
3624 with exn ->
3625 state.text <- Printf.sprintf "bad color `%s': %s" v @@ exntos exn
3628 let oldmode = state.mode in
3629 let birdseye = isbirdseye state.mode in
3631 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3633 src#bool "presentation mode"
3634 (fun () -> conf.presentation)
3635 (fun v -> setpresentationmode v);
3637 src#bool "ignore case in searches"
3638 (fun () -> conf.icase)
3639 (fun v -> conf.icase <- v);
3641 src#bool "preload"
3642 (fun () -> conf.preload)
3643 (fun v -> conf.preload <- v);
3645 src#bool "highlight links"
3646 (fun () -> conf.hlinks)
3647 (fun v -> conf.hlinks <- v);
3649 src#bool "under info"
3650 (fun () -> conf.underinfo)
3651 (fun v -> conf.underinfo <- v);
3653 src#bool "persistent bookmarks"
3654 (fun () -> conf.savebmarks)
3655 (fun v -> conf.savebmarks <- v);
3657 src#fitmodel "fit model"
3658 (fun () -> FMTE.to_string conf.fitmodel)
3659 (fun v -> reqlayout conf.angle (FMTE.of_int v));
3661 src#bool "trim margins"
3662 (fun () -> conf.trimmargins)
3663 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3665 src#bool "persistent location"
3666 (fun () -> conf.jumpback)
3667 (fun v -> conf.jumpback <- v);
3669 sep ();
3670 src#int "inter-page space"
3671 (fun () -> conf.interpagespace)
3672 (fun n ->
3673 conf.interpagespace <- n;
3674 docolumns conf.columns;
3675 let pageno, py =
3676 match state.layout with
3677 | [] -> 0, 0
3678 | l :: _ ->
3679 l.pageno, l.pagey
3681 state.maxy <- calcheight ();
3682 let y = getpagey pageno in
3683 gotoy (y + py)
3686 src#int "page bias"
3687 (fun () -> conf.pagebias)
3688 (fun v -> conf.pagebias <- v);
3690 src#int "scroll step"
3691 (fun () -> conf.scrollstep)
3692 (fun n -> conf.scrollstep <- n);
3694 src#int "horizontal scroll step"
3695 (fun () -> conf.hscrollstep)
3696 (fun v -> conf.hscrollstep <- v);
3698 src#int "auto scroll step"
3699 (fun () ->
3700 match state.autoscroll with
3701 | Some step -> step
3702 | _ -> conf.autoscrollstep)
3703 (fun n ->
3704 let n = boundastep state.winh n in
3705 if state.autoscroll <> None
3706 then state.autoscroll <- Some n;
3707 conf.autoscrollstep <- n);
3709 src#int "zoom"
3710 (fun () -> truncate (conf.zoom *. 100.))
3711 (fun v -> setzoom ((float v) /. 100.));
3713 src#int "rotation"
3714 (fun () -> conf.angle)
3715 (fun v -> reqlayout v conf.fitmodel);
3717 src#int "scroll bar width"
3718 (fun () -> conf.scrollbw)
3719 (fun v ->
3720 conf.scrollbw <- v;
3721 reshape state.winw state.winh;
3724 src#int "scroll handle height"
3725 (fun () -> conf.scrollh)
3726 (fun v -> conf.scrollh <- v;);
3728 src#int "thumbnail width"
3729 (fun () -> conf.thumbw)
3730 (fun v ->
3731 conf.thumbw <- min 4096 v;
3732 match oldmode with
3733 | Birdseye beye ->
3734 leavebirdseye beye false;
3735 enterbirdseye ()
3736 | Textentry _
3737 | View
3738 | LinkNav _ -> ()
3741 let mode = state.mode in
3742 src#string "columns"
3743 (fun () ->
3744 match conf.columns with
3745 | Csingle _ -> "1"
3746 | Cmulti (multi, _) -> multicolumns_to_string multi
3747 | Csplit (count, _) -> "-" ^ string_of_int count
3749 (fun v ->
3750 let n, a, b = multicolumns_of_string v in
3751 setcolumns mode n a b);
3753 sep ();
3754 src#caption "Pixmap cache" 0;
3755 src#int_with_suffix "size (advisory)"
3756 (fun () -> conf.memlimit)
3757 (fun v -> conf.memlimit <- v);
3759 src#caption2 "used"
3760 (fun () -> Printf.sprintf "%s bytes, %d tiles"
3761 (string_with_suffix_of_int state.memused)
3762 (Hashtbl.length state.tilemap)) 1;
3764 sep ();
3765 src#caption "Layout" 0;
3766 src#caption2 "Dimension"
3767 (fun () ->
3768 Printf.sprintf "%dx%d (virtual %dx%d)"
3769 state.winw state.winh
3770 state.w state.maxy)
3772 if conf.debug
3773 then
3774 src#caption2 "Position" (fun () ->
3775 Printf.sprintf "%dx%d" state.x state.y
3777 else
3778 src#caption2 "Position" (fun () -> describe_location ()) 1
3781 sep ();
3782 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3783 "Save these parameters as global defaults at exit"
3784 (fun () -> conf.bedefault)
3785 (fun v -> conf.bedefault <- v)
3788 sep ();
3789 let btos b = if b then "@Ulguillemet" else "@Urguillemet" in
3790 src#bool ~offset:0 ~btos "Extended parameters"
3791 (fun () -> !showextended)
3792 (fun v -> showextended := v; fillsrc prevmode prevuioh);
3793 if !showextended
3794 then (
3795 src#bool "checkers"
3796 (fun () -> conf.checkers)
3797 (fun v -> conf.checkers <- v; setcheckers v);
3798 src#bool "update cursor"
3799 (fun () -> conf.updatecurs)
3800 (fun v -> conf.updatecurs <- v);
3801 src#bool "scroll-bar on the left"
3802 (fun () -> conf.leftscroll)
3803 (fun v -> conf.leftscroll <- v);
3804 src#bool "verbose"
3805 (fun () -> conf.verbose)
3806 (fun v -> conf.verbose <- v);
3807 src#bool "invert colors"
3808 (fun () -> conf.invert)
3809 (fun v -> conf.invert <- v);
3810 src#bool "max fit"
3811 (fun () -> conf.maxhfit)
3812 (fun v -> conf.maxhfit <- v);
3813 src#bool "pax mode"
3814 (fun () -> conf.pax != None)
3815 (fun v ->
3816 if v
3817 then conf.pax <- Some (ref (now (), 0, 0))
3818 else conf.pax <- None);
3819 src#string "uri launcher"
3820 (fun () -> conf.urilauncher)
3821 (fun v -> conf.urilauncher <- v);
3822 src#string "path launcher"
3823 (fun () -> conf.pathlauncher)
3824 (fun v -> conf.pathlauncher <- v);
3825 src#string "tile size"
3826 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3827 (fun v ->
3829 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
3830 conf.tilew <- max 64 w;
3831 conf.tileh <- max 64 h;
3832 flushtiles ();
3833 with exn ->
3834 state.text <- Printf.sprintf "bad tile size `%s': %s"
3835 v @@ exntos exn
3837 src#int "texture count"
3838 (fun () -> conf.texcount)
3839 (fun v ->
3840 if realloctexts v
3841 then conf.texcount <- v
3842 else impmsg "failed to set texture count please retry later"
3844 src#int "slice height"
3845 (fun () -> conf.sliceheight)
3846 (fun v ->
3847 conf.sliceheight <- v;
3848 wcmd "sliceh %d" conf.sliceheight;
3850 src#int "anti-aliasing level"
3851 (fun () -> conf.aalevel)
3852 (fun v ->
3853 conf.aalevel <- bound v 0 8;
3854 state.anchor <- getanchor ();
3855 opendoc state.path state.password;
3857 src#string "page scroll scaling factor"
3858 (fun () -> string_of_float conf.pgscale)
3859 (fun v ->
3861 let s = float_of_string v in
3862 conf.pgscale <- s
3863 with exn ->
3864 state.text <- Printf.sprintf
3865 "bad page scroll scaling factor `%s': %s" v @@ exntos exn
3868 src#int "ui font size"
3869 (fun () -> fstate.fontsize)
3870 (fun v -> setfontsize (bound v 5 100));
3871 src#int "hint font size"
3872 (fun () -> conf.hfsize)
3873 (fun v -> conf.hfsize <- bound v 5 100);
3874 colorp "background color"
3875 (fun () -> conf.bgcolor)
3876 (fun v -> conf.bgcolor <- v);
3877 src#bool "crop hack"
3878 (fun () -> conf.crophack)
3879 (fun v -> conf.crophack <- v);
3880 src#string "trim fuzz"
3881 (fun () -> irect_to_string conf.trimfuzz)
3882 (fun v ->
3884 conf.trimfuzz <- irect_of_string v;
3885 if conf.trimmargins
3886 then settrim true conf.trimfuzz;
3887 with exn ->
3888 state.text <- Printf.sprintf "bad irect `%s': %s" v @@ exntos exn
3890 src#string "throttle"
3891 (fun () ->
3892 match conf.maxwait with
3893 | None -> "show place holder if page is not ready"
3894 | Some time ->
3895 if time = infinity
3896 then "wait for page to fully render"
3897 else
3898 "wait " ^ string_of_float time
3899 ^ " seconds before showing placeholder"
3901 (fun v ->
3903 let f = float_of_string v in
3904 if f <= 0.0
3905 then conf.maxwait <- None
3906 else conf.maxwait <- Some f
3907 with exn ->
3908 state.text <- Printf.sprintf "bad time `%s': %s" v @@ exntos exn
3910 src#string "ghyll scroll"
3911 (fun () ->
3912 match conf.ghyllscroll with
3913 | None -> E.s
3914 | Some nab -> ghyllscroll_to_string nab
3916 (fun v ->
3917 try conf.ghyllscroll <- ghyllscroll_of_string v
3918 with
3919 | Failure msg ->
3920 state.text <- Printf.sprintf "bad ghyll `%s': %s" v msg
3921 | exn ->
3922 state.text <- Printf.sprintf "bad ghyll `%s': %s" v @@ exntos exn
3924 src#string "selection command"
3925 (fun () -> conf.selcmd)
3926 (fun v -> conf.selcmd <- v);
3927 src#string "synctex command"
3928 (fun () -> conf.stcmd)
3929 (fun v -> conf.stcmd <- v);
3930 src#string "pax command"
3931 (fun () -> conf.paxcmd)
3932 (fun v -> conf.paxcmd <- v);
3933 src#string "ask password command"
3934 (fun () -> conf.passcmd)
3935 (fun v -> conf.passcmd <- v);
3936 src#string "save path command"
3937 (fun () -> conf.savecmd)
3938 (fun v -> conf.savecmd <- v);
3939 src#colorspace "color space"
3940 (fun () -> CSTE.to_string conf.colorspace)
3941 (fun v ->
3942 conf.colorspace <- CSTE.of_int v;
3943 wcmd "cs %d" v;
3944 load state.layout;
3946 src#paxmark "pax mark method"
3947 (fun () -> MTE.to_string conf.paxmark)
3948 (fun v -> conf.paxmark <- MTE.of_int v);
3949 if pbousable ()
3950 then
3951 src#bool "use PBO"
3952 (fun () -> conf.usepbo)
3953 (fun v -> conf.usepbo <- v);
3954 src#bool "mouse wheel scrolls pages"
3955 (fun () -> conf.wheelbypage)
3956 (fun v -> conf.wheelbypage <- v);
3957 src#bool "open remote links in a new instance"
3958 (fun () -> conf.riani)
3959 (fun v -> conf.riani <- v);
3960 src#bool "edit annotations inline"
3961 (fun () -> conf.annotinline)
3962 (fun v -> conf.annotinline <- v);
3965 sep ();
3966 src#caption "Document" 0;
3967 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
3968 src#caption2 "Pages"
3969 (fun () -> string_of_int state.pagecount) 1;
3970 src#caption2 "Dimensions"
3971 (fun () -> string_of_int (List.length state.pdims)) 1;
3972 if conf.trimmargins
3973 then (
3974 sep ();
3975 src#caption "Trimmed margins" 0;
3976 src#caption2 "Dimensions"
3977 (fun () -> string_of_int (List.length state.pdims)) 1;
3980 sep ();
3981 src#caption "OpenGL" 0;
3982 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
3983 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
3985 sep ();
3986 src#caption "Location" 0;
3987 if nonemptystr state.origin
3988 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
3989 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
3991 src#reset prevmode prevuioh;
3993 fun () ->
3994 state.text <- E.s;
3995 resetmstate ();
3996 let prevmode = state.mode
3997 and prevuioh = state.uioh in
3998 fillsrc prevmode prevuioh;
3999 let source = (src :> lvsource) in
4000 let modehash = findkeyhash conf "info" in
4001 state.uioh <- coe (object (self)
4002 inherit listview ~zebra:false ~helpmode:false
4003 ~source ~trusted:true ~modehash as super
4004 val mutable m_prevmemused = 0
4005 method! infochanged = function
4006 | Memused ->
4007 if m_prevmemused != state.memused
4008 then (
4009 m_prevmemused <- state.memused;
4010 G.postRedisplay "memusedchanged";
4012 | Pdim -> G.postRedisplay "pdimchanged"
4013 | Docinfo -> fillsrc prevmode prevuioh
4015 method! key key mask =
4016 if not (Wsi.withctrl mask)
4017 then
4018 match key with
4019 | @left | @kpleft -> coe (self#updownlevel ~-1)
4020 | @right | @kpright -> coe (self#updownlevel 1)
4021 | _ -> super#key key mask
4022 else super#key key mask
4023 end);
4024 G.postRedisplay "info";
4027 let enterhelpmode =
4028 let source =
4029 (object
4030 inherit lvsourcebase
4031 method getitemcount = Array.length state.help
4032 method getitem n =
4033 let s, l, _ = state.help.(n) in
4034 (s, l)
4036 method exit ~uioh ~cancel ~active ~first ~pan =
4037 let optuioh =
4038 if not cancel
4039 then (
4040 match state.help.(active) with
4041 | _, _, Action f -> Some (f uioh)
4042 | _, _, Noaction -> Some uioh
4044 else None
4046 m_active <- active;
4047 m_first <- first;
4048 m_pan <- pan;
4049 optuioh
4051 method hasaction n =
4052 match state.help.(n) with
4053 | _, _, Action _ -> true
4054 | _, _, Noaction -> false
4056 initializer
4057 m_active <- -1
4058 end)
4059 in fun () ->
4060 let modehash = findkeyhash conf "help" in
4061 resetmstate ();
4062 state.uioh <- coe (new listview
4063 ~zebra:false ~helpmode:true
4064 ~source ~trusted:true ~modehash);
4065 G.postRedisplay "help";
4068 let entermsgsmode =
4069 let msgsource =
4070 (object
4071 inherit lvsourcebase
4072 val mutable m_items = E.a
4074 method getitemcount = 1 + Array.length m_items
4076 method getitem n =
4077 if n = 0
4078 then "[Clear]", 0
4079 else m_items.(n-1), 0
4081 method exit ~uioh ~cancel ~active ~first ~pan =
4082 ignore uioh;
4083 if not cancel
4084 then (
4085 if active = 0
4086 then Buffer.clear state.errmsgs;
4088 m_active <- active;
4089 m_first <- first;
4090 m_pan <- pan;
4091 None
4093 method hasaction n =
4094 n = 0
4096 method reset =
4097 state.newerrmsgs <- false;
4098 let l = Str.split newlinere (Buffer.contents state.errmsgs) in
4099 m_items <- Array.of_list l
4101 initializer
4102 m_active <- 0
4103 end)
4104 in fun () ->
4105 state.text <- E.s;
4106 resetmstate ();
4107 msgsource#reset;
4108 let source = (msgsource :> lvsource) in
4109 let modehash = findkeyhash conf "listview" in
4110 state.uioh <- coe (object
4111 inherit listview ~zebra:false ~helpmode:false
4112 ~source ~trusted:false ~modehash as super
4113 method! display =
4114 if state.newerrmsgs
4115 then msgsource#reset;
4116 super#display
4117 end);
4118 G.postRedisplay "msgs";
4121 let getusertext s =
4122 let editor = getenvwithdef "EDITOR" E.s in
4123 if emptystr editor
4124 then E.s
4125 else
4126 let tmppath = Filename.temp_file "llpp" "note" in
4127 if nonemptystr s
4128 then (
4129 let oc = open_out tmppath in
4130 output_string oc s;
4131 close_out oc;
4133 let execstr = editor ^ " " ^ tmppath in
4134 let s =
4135 match spawn execstr [] with
4136 | (exception exn) ->
4137 impmsg "spawn(%S) failed: %s" execstr @@ exntos exn;
4139 | pid ->
4140 match Unix.waitpid [] pid with
4141 | (exception exn) ->
4142 impmsg "waitpid(%d) failed: %s" pid @@ exntos exn;
4144 | (_pid, status) ->
4145 match status with
4146 | Unix.WEXITED 0 -> filecontents tmppath
4147 | Unix.WEXITED n ->
4148 impmsg "editor process(%s) exited abnormally: %d" execstr n;
4150 | Unix.WSIGNALED n ->
4151 impmsg "editor process(%s) was killed by signal %d" execstr n;
4153 | Unix.WSTOPPED n ->
4154 impmsg "editor(%s) process was stopped by signal %d" execstr n;
4157 match Unix.unlink tmppath with
4158 | (exception exn) ->
4159 impmsg "failed to ulink %S: %s" tmppath @@ exntos exn;
4161 | () -> s
4164 let enterannotmode opaque slinkindex =
4165 let msgsource =
4166 (object
4167 inherit lvsourcebase
4168 val mutable m_text = E.s
4169 val mutable m_items = E.a
4171 method getitemcount = Array.length m_items
4173 method getitem n =
4174 let label, _func = m_items.(n) in
4175 label, 0
4177 method exit ~uioh ~cancel ~active ~first ~pan =
4178 ignore (uioh, first, pan);
4179 if not cancel
4180 then (
4181 let _label, func = m_items.(active) in
4182 func ()
4184 None
4186 method hasaction n = nonemptystr @@ fst m_items.(n)
4188 method reset s =
4189 let rec split accu b i =
4190 let p = b+i in
4191 if p = String.length s
4192 then (String.sub s b (p-b), unit) :: accu
4193 else
4194 if (i > 70 && s.[p] = ' ') || s.[p] = '\r' || s.[p] = '\n'
4195 then
4196 let ss = if i = 0 then E.s else String.sub s b i in
4197 split ((ss, unit)::accu) (p+1) 0
4198 else
4199 split accu b (i+1)
4201 let cleanup () =
4202 wcmd "freepage %s" (~> opaque);
4203 let keys =
4204 Hashtbl.fold (fun key opaque' accu ->
4205 if opaque' = opaque'
4206 then key :: accu else accu) state.pagemap []
4208 List.iter (Hashtbl.remove state.pagemap) keys;
4209 flushtiles ();
4210 gotoy state.y
4212 let dele () =
4213 delannot opaque slinkindex;
4214 cleanup ();
4216 let edit inline () =
4217 let update s =
4218 if emptystr s
4219 then dele ()
4220 else (
4221 modannot opaque slinkindex s;
4222 cleanup ();
4225 if inline
4226 then
4227 let mode = state.mode in
4228 state.mode <-
4229 Textentry (
4230 ("annotation: ", m_text, None, textentry, update, true),
4231 fun _ -> state.mode <- mode);
4232 state.text <- E.s;
4233 enttext ();
4234 else
4235 let s = getusertext m_text in
4236 update s
4238 m_text <- s;
4239 m_items <-
4240 ( "[Copy]", fun () -> selstring m_text)
4241 :: ("[Delete]", dele)
4242 :: ("[Edit]", edit conf.annotinline)
4243 :: (E.s, unit)
4244 :: split [] 0 0 |> List.rev |> Array.of_list
4246 initializer
4247 m_active <- 0
4248 end)
4250 state.text <- E.s;
4251 let s = getannotcontents opaque slinkindex in
4252 resetmstate ();
4253 msgsource#reset s;
4254 let source = (msgsource :> lvsource) in
4255 let modehash = findkeyhash conf "listview" in
4256 state.uioh <- coe (object
4257 inherit listview ~zebra:false ~helpmode:false
4258 ~source ~trusted:false ~modehash
4259 end);
4260 G.postRedisplay "enterannotmode";
4263 let gotounder under =
4264 let getpath filename =
4265 let path =
4266 if nonemptystr filename
4267 then
4268 if Filename.is_relative filename
4269 then
4270 let dir = Filename.dirname state.path in
4271 let dir =
4272 if Filename.is_implicit dir
4273 then Filename.concat (Sys.getcwd ()) dir
4274 else dir
4276 Filename.concat dir filename
4277 else filename
4278 else E.s
4280 if Sys.file_exists path
4281 then path
4282 else E.s
4284 match under with
4285 | Ulinkgoto (pageno, top) ->
4286 if pageno >= 0
4287 then (
4288 addnav ();
4289 gotopage1 pageno top;
4292 | Ulinkuri s -> gotouri s
4294 | Uremote (filename, pageno) ->
4295 let path = getpath filename in
4296 if nonemptystr path
4297 then (
4298 if conf.riani
4299 then
4300 let command = Printf.sprintf "%s -page %d %S" !selfexec pageno path in
4301 match spawn command [] with
4302 | _pid -> ()
4303 | (exception exn) ->
4304 dolog "failed to execute `%s': %s" command @@ exntos exn
4305 else
4306 let anchor = getanchor () in
4307 let ranchor = state.path, state.password, anchor, state.origin in
4308 state.origin <- E.s;
4309 state.anchor <- (pageno, 0.0, 0.0);
4310 state.ranchors <- ranchor :: state.ranchors;
4311 opendoc path E.s;
4313 else impmsg "cannot find %s" filename
4315 | Uremotedest (filename, destname) ->
4316 let path = getpath filename in
4317 if nonemptystr path
4318 then (
4319 if conf.riani
4320 then
4321 let command = !selfexec ^ " " ^ path ^ " -dest " ^ destname in
4322 match spawn command [] with
4323 | (exception exn) ->
4324 dolog "failed to execute `%s': %s" command @@ exntos exn
4325 | _pid -> ()
4326 else
4327 let anchor = getanchor () in
4328 let ranchor = state.path, state.password, anchor, state.origin in
4329 state.origin <- E.s;
4330 state.nameddest <- destname;
4331 state.ranchors <- ranchor :: state.ranchors;
4332 opendoc path E.s;
4334 else impmsg "cannot find %s" filename
4336 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4337 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
4340 let gotooutline (_, _, kind) =
4341 match kind with
4342 | Onone -> ()
4343 | Oanchor anchor ->
4344 let (pageno, y, _) = anchor in
4345 let y = getanchory
4346 (if conf.presentation then (pageno, y, 1.0) else anchor)
4348 addnav ();
4349 gotoghyll y
4350 | Ouri uri -> gotounder (Ulinkuri uri)
4351 | Olaunch cmd -> gotounder (Ulaunch cmd)
4352 | Oremote remote -> gotounder (Uremote remote)
4353 | Ohistory hist -> gotohist hist
4354 | Oremotedest remotedest -> gotounder (Uremotedest remotedest)
4357 class outlinesoucebase fetchoutlines = object (self)
4358 inherit lvsourcebase
4359 val mutable m_items = E.a
4360 val mutable m_minfo = E.a
4361 val mutable m_orig_items = E.a
4362 val mutable m_orig_minfo = E.a
4363 val mutable m_narrow_patterns = []
4364 val mutable m_gen = -1
4366 method getitemcount = Array.length m_items
4368 method getitem n =
4369 let s, n, _ = m_items.(n) in
4370 (s, n+0)
4372 method exit ~(uioh:uioh) ~cancel ~active ~(first:int) ~pan :
4373 uioh option =
4374 ignore (uioh, first);
4375 let items, minfo =
4376 if m_narrow_patterns = []
4377 then m_orig_items, m_orig_minfo
4378 else m_items, m_minfo
4380 m_pan <- pan;
4381 if not cancel
4382 then (
4383 m_items <- items;
4384 m_minfo <- minfo;
4385 gotooutline m_items.(active);
4387 else (
4388 m_items <- items;
4389 m_minfo <- minfo;
4391 None
4393 method hasaction (_:int) = true
4395 method greetmsg =
4396 if Array.length m_items != Array.length m_orig_items
4397 then
4398 let s =
4399 match m_narrow_patterns with
4400 | one :: [] -> one
4401 | many -> String.concat "@Uellipsis" (List.rev many)
4403 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
4404 else E.s
4406 method statestr =
4407 match m_narrow_patterns with
4408 | [] -> E.s
4409 | one :: [] -> one
4410 | head :: _ -> "@Uellipsis" ^ head
4412 method narrow pattern =
4413 match Str.regexp_case_fold pattern with
4414 | (exception _) -> ()
4415 | re ->
4416 let rec loop accu minfo n =
4417 if n = -1
4418 then (
4419 m_items <- Array.of_list accu;
4420 m_minfo <- Array.of_list minfo;
4422 else
4423 let (s, _, _) as o = m_items.(n) in
4424 let accu, minfo =
4425 match Str.search_forward re s 0 with
4426 | (exception Not_found) -> accu, minfo
4427 | first -> o :: accu, (first, Str.match_end ()) :: minfo
4429 loop accu minfo (n-1)
4431 loop [] [] (Array.length m_items - 1)
4433 method! getminfo = m_minfo
4435 method denarrow =
4436 m_orig_items <- fetchoutlines ();
4437 m_minfo <- m_orig_minfo;
4438 m_items <- m_orig_items
4440 method add_narrow_pattern pattern =
4441 m_narrow_patterns <- pattern :: m_narrow_patterns
4443 method del_narrow_pattern =
4444 match m_narrow_patterns with
4445 | _ :: rest -> m_narrow_patterns <- rest
4446 | [] -> ()
4448 method renarrow =
4449 self#denarrow;
4450 match m_narrow_patterns with
4451 | pattern :: [] -> self#narrow pattern; pattern
4452 | list ->
4453 List.fold_left (fun accu pattern ->
4454 self#narrow pattern;
4455 pattern ^ "@Uellipsis" ^ accu) E.s list
4457 method calcactive (_:anchor) = 0
4459 method reset anchor items =
4460 if state.gen != m_gen
4461 then (
4462 m_orig_items <- items;
4463 m_items <- items;
4464 m_narrow_patterns <- [];
4465 m_minfo <- E.a;
4466 m_orig_minfo <- E.a;
4467 m_gen <- state.gen;
4469 else (
4470 if items != m_orig_items
4471 then (
4472 m_orig_items <- items;
4473 if m_narrow_patterns == []
4474 then m_items <- items;
4477 let active = self#calcactive anchor in
4478 m_active <- active;
4479 m_first <- firstof m_first active
4483 let outlinesource fetchoutlines =
4484 (object
4485 inherit outlinesoucebase fetchoutlines
4486 method! calcactive anchor =
4487 let rely = getanchory anchor in
4488 let rec loop n best bestd =
4489 if n = Array.length m_items
4490 then best
4491 else
4492 let _, _, kind = m_items.(n) in
4493 match kind with
4494 | Oanchor anchor ->
4495 let orely = getanchory anchor in
4496 let d = abs (orely - rely) in
4497 if d < bestd
4498 then loop (n+1) n d
4499 else loop (n+1) best bestd
4500 | Onone | Oremote _ | Olaunch _
4501 | Oremotedest _ | Ouri _ | Ohistory _ ->
4502 loop (n+1) best bestd
4504 loop 0 ~-1 max_int
4505 end)
4508 let enteroutlinemode, enterbookmarkmode, enterhistmode =
4509 let mkselector sourcetype =
4510 let fetchoutlines () =
4511 match sourcetype with
4512 | `bookmarks -> Array.of_list state.bookmarks
4513 | `outlines -> state.outlines
4514 | `history -> genhistoutlines ()
4516 let source =
4517 if sourcetype = `history
4518 then new outlinesoucebase fetchoutlines
4519 else outlinesource fetchoutlines
4521 fun errmsg ->
4522 let outlines = fetchoutlines () in
4523 if Array.length outlines = 0
4524 then (
4525 showtext ' ' errmsg;
4527 else (
4528 resetmstate ();
4529 Wsi.setcursor Wsi.CURSOR_INHERIT;
4530 let anchor = getanchor () in
4531 source#reset anchor outlines;
4532 state.text <- source#greetmsg;
4533 state.uioh <-
4534 coe (new outlinelistview ~zebra:(sourcetype=`history) ~source);
4535 G.postRedisplay "enter selector";
4538 let mkenter sourcetype errmsg =
4539 let enter = mkselector sourcetype in
4540 fun () -> enter errmsg
4542 (**)mkenter `outlines "document has no outline"
4543 , mkenter `bookmarks "document has no bookmarks (yet)"
4544 , mkenter `history "history is empty"
4547 let quickbookmark ?title () =
4548 match state.layout with
4549 | [] -> ()
4550 | l :: _ ->
4551 let title =
4552 match title with
4553 | None ->
4554 let tm = Unix.localtime (now ()) in
4555 Printf.sprintf
4556 "Quick (page %d) (bookmarked at %02d/%02d/%d %02d:%02d)"
4557 (l.pageno+1)
4558 tm.Unix.tm_mday
4559 (tm.Unix.tm_mon+1)
4560 (tm.Unix.tm_year + 1900)
4561 tm.Unix.tm_hour
4562 tm.Unix.tm_min
4563 | Some title -> title
4565 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4568 let setautoscrollspeed step goingdown =
4569 let incr = max 1 ((abs step) / 2) in
4570 let incr = if goingdown then incr else -incr in
4571 let astep = boundastep state.winh (step + incr) in
4572 state.autoscroll <- Some astep;
4575 let canpan () =
4576 match conf.columns with
4577 | Csplit _ -> true
4578 | Csingle _ | Cmulti _ -> state.x != 0 || conf.zoom > 1.0
4581 let panbound x = bound x (-state.w) (wadjsb () + state.winw);;
4583 let existsinrow pageno (columns, coverA, coverB) p =
4584 let last = ((pageno - coverA) mod columns) + columns in
4585 let rec any = function
4586 | [] -> false
4587 | l :: rest ->
4588 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4589 then p l
4590 else (
4591 if not (p l)
4592 then (if l.pageno = last then false else any rest)
4593 else true
4596 any state.layout
4599 let nextpage () =
4600 match state.layout with
4601 | [] ->
4602 let pageno = page_of_y state.y in
4603 gotoghyll (getpagey (pageno+1))
4604 | l :: rest ->
4605 match conf.columns with
4606 | Csingle _ ->
4607 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4608 then
4609 let y = clamp (pgscale state.winh) in
4610 gotoghyll y
4611 else
4612 let pageno = min (l.pageno+1) (state.pagecount-1) in
4613 gotoghyll (getpagey pageno)
4614 | Cmulti ((c, _, _) as cl, _) ->
4615 if conf.presentation
4616 && (existsinrow l.pageno cl
4617 (fun l -> l.pageh > l.pagey + l.pagevh))
4618 then
4619 let y = clamp (pgscale state.winh) in
4620 gotoghyll y
4621 else
4622 let pageno = min (l.pageno+c) (state.pagecount-1) in
4623 gotoghyll (getpagey pageno)
4624 | Csplit (n, _) ->
4625 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4626 then
4627 let pagey, pageh = getpageyh l.pageno in
4628 let pagey = pagey + pageh * l.pagecol in
4629 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4630 gotoghyll (pagey + pageh + ips)
4633 let prevpage () =
4634 match state.layout with
4635 | [] ->
4636 let pageno = page_of_y state.y in
4637 gotoghyll (getpagey (pageno-1))
4638 | l :: _ ->
4639 match conf.columns with
4640 | Csingle _ ->
4641 if conf.presentation && l.pagey != 0
4642 then
4643 gotoghyll (clamp (pgscale ~-(state.winh)))
4644 else
4645 let pageno = max 0 (l.pageno-1) in
4646 gotoghyll (getpagey pageno)
4647 | Cmulti ((c, _, coverB) as cl, _) ->
4648 if conf.presentation &&
4649 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4650 then
4651 gotoghyll (clamp (pgscale ~-(state.winh)))
4652 else
4653 let decr =
4654 if l.pageno = state.pagecount - coverB
4655 then 1
4656 else c
4658 let pageno = max 0 (l.pageno-decr) in
4659 gotoghyll (getpagey pageno)
4660 | Csplit (n, _) ->
4661 let y =
4662 if l.pagecol = 0
4663 then
4664 if l.pageno = 0
4665 then l.pagey
4666 else
4667 let pageno = max 0 (l.pageno-1) in
4668 let pagey, pageh = getpageyh pageno in
4669 pagey + (n-1)*pageh
4670 else
4671 let pagey, pageh = getpageyh l.pageno in
4672 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4674 gotoghyll y
4677 let save () =
4678 if emptystr conf.savecmd
4679 then error "don't know where to save modified document"
4680 else
4681 let savecmd = Str.global_replace percentsre state.path conf.savecmd in
4682 let path =
4683 getcmdoutput
4684 (fun s -> error "failed to obtain path to the saved copy: %s" s)
4685 savecmd
4687 if nonemptystr path
4688 then
4689 let tmp = path ^ ".tmp" in
4690 savedoc tmp;
4691 Unix.rename tmp path;
4694 let viewkeyboard key mask =
4695 let enttext te =
4696 let mode = state.mode in
4697 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4698 state.text <- E.s;
4699 enttext ();
4700 G.postRedisplay "view:enttext"
4702 let ctrl = Wsi.withctrl mask in
4703 let key =
4704 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
4706 match key with
4707 | @Q -> exit 0
4709 | @W ->
4710 if hasunsavedchanges ()
4711 then save ()
4713 | @insert ->
4714 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4715 then (
4716 state.mode <- LinkNav (Ltgendir 0);
4717 gotoy state.y;
4719 else impmsg "keyboard link navigation does not work under rotation"
4721 | @escape | @q ->
4722 begin match state.mstate with
4723 | Mzoomrect _ ->
4724 resetmstate ();
4725 G.postRedisplay "kill rect";
4726 | Msel _
4727 | Mpan _
4728 | Mscrolly | Mscrollx
4729 | Mzoom _
4730 | Mnone ->
4731 begin match state.mode with
4732 | LinkNav _ ->
4733 state.mode <- View;
4734 G.postRedisplay "esc leave linknav"
4735 | Birdseye _
4736 | Textentry _
4737 | View ->
4738 match state.ranchors with
4739 | [] -> raise Quit
4740 | (path, password, anchor, origin) :: rest ->
4741 state.ranchors <- rest;
4742 state.anchor <- anchor;
4743 state.origin <- origin;
4744 state.nameddest <- E.s;
4745 opendoc path password
4746 end;
4747 end;
4749 | @backspace ->
4750 gotoghyll (getnav ~-1)
4752 | @o ->
4753 enteroutlinemode ()
4755 | @H ->
4756 enterhistmode ()
4758 | @u ->
4759 state.rects <- [];
4760 state.text <- E.s;
4761 Hashtbl.iter (fun _ opaque ->
4762 clearmark opaque;
4763 Hashtbl.clear state.prects) state.pagemap;
4764 G.postRedisplay "dehighlight";
4766 | @slash | @question ->
4767 let ondone isforw s =
4768 cbput state.hists.pat s;
4769 state.searchpattern <- s;
4770 search s isforw
4772 let s = String.make 1 (Char.chr key) in
4773 enttext (s, E.s, Some (onhist state.hists.pat),
4774 textentry, ondone (key = @slash), true)
4776 | @plus | @kpplus | @equals when ctrl ->
4777 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4778 setzoom (conf.zoom +. incr)
4780 | @plus | @kpplus ->
4781 let ondone s =
4782 let n =
4783 try int_of_string s with exc ->
4784 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exc;
4785 max_int
4787 if n != max_int
4788 then (
4789 conf.pagebias <- n;
4790 state.text <- "page bias is now " ^ string_of_int n;
4793 enttext ("page bias: ", E.s, None, intentry, ondone, true)
4795 | @minus | @kpminus when ctrl ->
4796 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4797 setzoom (max 0.01 (conf.zoom -. decr))
4799 | @minus | @kpminus ->
4800 let ondone msg = state.text <- msg in
4801 enttext (
4802 "option [acfhilpstvxACFPRSZTISM]: ", E.s, None,
4803 optentry state.mode, ondone, true
4806 | @0 when ctrl ->
4807 if conf.zoom = 1.0
4808 then (
4809 state.x <- 0;
4810 gotoy state.y
4812 else setzoom 1.0
4814 | (@1 | @2) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
4815 let cols =
4816 match conf.columns with
4817 | Csingle _ | Cmulti _ -> 1
4818 | Csplit (n, _) -> n
4820 let h = state.winh -
4821 conf.interpagespace lsl (if conf.presentation then 1 else 0)
4823 let zoom = zoomforh state.winw h (vscrollw ()) cols in
4824 if zoom > 0.0 && (key = @2 || zoom < 1.0)
4825 then setzoom zoom
4827 | @3 when ctrl ->
4828 let fm =
4829 match conf.fitmodel with
4830 | FitWidth -> FitProportional
4831 | FitProportional -> FitPage
4832 | FitPage -> FitWidth
4834 state.text <- "fit model: " ^ FMTE.to_string fm;
4835 reqlayout conf.angle fm
4837 | @4 when ctrl -> (* ctrl-4 *)
4838 let zoom = getmaxw () /. float state.winw in
4839 if zoom > 0.0 then setzoom zoom
4841 | @F9 ->
4842 togglebirdseye ()
4844 | @9 when ctrl ->
4845 togglebirdseye ()
4847 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4848 when not ctrl -> (* 0..9 *)
4849 let ondone s =
4850 let n =
4851 try int_of_string s with exc ->
4852 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exc;
4855 if n >= 0
4856 then (
4857 addnav ();
4858 cbput state.hists.pag (string_of_int n);
4859 gotopage1 (n + conf.pagebias - 1) 0;
4862 let pageentry text key =
4863 match Char.unsafe_chr key with
4864 | 'g' -> TEdone text
4865 | _ -> intentry text key
4867 let text = String.make 1 (Char.chr key) in
4868 enttext (":", text, Some (onhist state.hists.pag),
4869 pageentry, ondone, true)
4871 | @b ->
4872 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
4873 reshape state.winw state.winh;
4875 | @B ->
4876 state.bzoom <- not state.bzoom;
4877 state.rects <- [];
4878 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
4880 | @l ->
4881 conf.hlinks <- not conf.hlinks;
4882 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4883 G.postRedisplay "toggle highlightlinks";
4885 | @F ->
4886 if conf.angle mod 360 = 0
4887 then (
4888 state.glinks <- true;
4889 let mode = state.mode in
4890 state.mode <-
4891 Textentry (
4892 (":", E.s, None, linknentry, linknact gotounder, false),
4893 (fun _ ->
4894 state.glinks <- false;
4895 state.mode <- mode)
4897 state.text <- E.s;
4898 G.postRedisplay "view:linkent(F)"
4900 else impmsg "hint mode does not work under rotation"
4902 | @y ->
4903 state.glinks <- true;
4904 let mode = state.mode in
4905 state.mode <- Textentry (
4907 ":", E.s, None, linknentry, linknact (fun under ->
4908 selstring (undertext under);
4909 ), false
4911 fun _ ->
4912 state.glinks <- false;
4913 state.mode <- mode
4915 state.text <- E.s;
4916 G.postRedisplay "view:linkent"
4918 | @a ->
4919 begin match state.autoscroll with
4920 | Some step ->
4921 conf.autoscrollstep <- step;
4922 state.autoscroll <- None
4923 | None ->
4924 if conf.autoscrollstep = 0
4925 then state.autoscroll <- Some 1
4926 else state.autoscroll <- Some conf.autoscrollstep
4929 | @p when ctrl ->
4930 launchpath () (* XXX where do error messages go? *)
4932 | @P ->
4933 setpresentationmode (not conf.presentation);
4934 showtext ' ' ("presentation mode " ^
4935 if conf.presentation then "on" else "off");
4937 | @f ->
4938 if List.mem Wsi.Fullscreen state.winstate
4939 then Wsi.reshape conf.cwinw conf.cwinh
4940 else Wsi.fullscreen ()
4942 | @p | @N ->
4943 search state.searchpattern false
4945 | @n | @F3 ->
4946 search state.searchpattern true
4948 | @t ->
4949 begin match state.layout with
4950 | [] -> ()
4951 | l :: _ ->
4952 gotoghyll (getpagey l.pageno)
4955 | @space ->
4956 nextpage ()
4958 | @delete | @kpdelete -> (* delete *)
4959 prevpage ()
4961 | @equals ->
4962 showtext ' ' (describe_location ());
4964 | @w ->
4965 begin match state.layout with
4966 | [] -> ()
4967 | l :: _ ->
4968 Wsi.reshape (l.pagew + vscrollw ()) l.pageh;
4969 G.postRedisplay "w"
4972 | @apos ->
4973 enterbookmarkmode ()
4975 | @h | @F1 ->
4976 enterhelpmode ()
4978 | @i ->
4979 enterinfomode ()
4981 | @e when Buffer.length state.errmsgs > 0 ->
4982 entermsgsmode ()
4984 | @m ->
4985 let ondone s =
4986 match state.layout with
4987 | l :: _ ->
4988 if nonemptystr s
4989 then
4990 state.bookmarks <-
4991 (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4992 | _ -> ()
4994 enttext ("bookmark: ", E.s, None, textentry, ondone, true)
4996 | @tilde ->
4997 quickbookmark ();
4998 showtext ' ' "Quick bookmark added";
5000 | @z ->
5001 begin match state.layout with
5002 | l :: _ ->
5003 let rect = getpdimrect l.pagedimno in
5004 let w, h =
5005 if conf.crophack
5006 then
5007 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5008 truncate (1.2 *. (rect.(3) -. rect.(0))))
5009 else
5010 (truncate (rect.(1) -. rect.(0)),
5011 truncate (rect.(3) -. rect.(0)))
5013 let w = truncate ((float w)*.conf.zoom)
5014 and h = truncate ((float h)*.conf.zoom) in
5015 if w != 0 && h != 0
5016 then (
5017 state.anchor <- getanchor ();
5018 Wsi.reshape (w + vscrollw ()) (h + conf.interpagespace)
5020 G.postRedisplay "z";
5022 | [] -> ()
5025 | @x -> state.roam ()
5027 | @Lt | @Gt ->
5028 reqlayout (conf.angle +
5029 (if key = @Gt then 30 else -30)) conf.fitmodel
5031 | @Lb | @Rb ->
5032 conf.colorscale <-
5033 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5035 G.postRedisplay "brightness";
5037 | @c when state.mode = View ->
5038 if Wsi.withalt mask
5039 then (
5040 if conf.zoom > 1.0
5041 then
5042 let m = (wadjsb () + state.winw - state.w) / 2 in
5043 state.x <- m;
5044 gotoy_and_clear_text state.y
5046 else
5047 let (c, a, b), z =
5048 match state.prevcolumns with
5049 | None -> (1, 0, 0), 1.0
5050 | Some (columns, z) ->
5051 let cab =
5052 match columns with
5053 | Csplit (c, _) -> -c, 0, 0
5054 | Cmulti ((c, a, b), _) -> c, a, b
5055 | Csingle _ -> 1, 0, 0
5057 cab, z
5059 setcolumns View c a b;
5060 setzoom z
5062 | @down | @up when ctrl && Wsi.withshift mask ->
5063 let zoom, x = state.prevzoom in
5064 setzoom zoom;
5065 state.x <- x;
5067 | @k | @up | @kpup ->
5068 begin match state.autoscroll with
5069 | None ->
5070 begin match state.mode with
5071 | Birdseye beye -> upbirdseye 1 beye
5072 | Textentry _
5073 | View
5074 | LinkNav _ ->
5075 if ctrl
5076 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5077 else (
5078 if not (Wsi.withshift mask) && conf.presentation
5079 then prevpage ()
5080 else gotoghyll1 true (clamp (-conf.scrollstep))
5083 | Some n ->
5084 setautoscrollspeed n false
5087 | @j | @down | @kpdown ->
5088 begin match state.autoscroll with
5089 | None ->
5090 begin match state.mode with
5091 | Birdseye beye -> downbirdseye 1 beye
5092 | Textentry _
5093 | View
5094 | LinkNav _ ->
5095 if ctrl
5096 then gotoy_and_clear_text (clamp (state.winh/2))
5097 else (
5098 if not (Wsi.withshift mask) && conf.presentation
5099 then nextpage ()
5100 else gotoghyll1 true (clamp (conf.scrollstep))
5103 | Some n ->
5104 setautoscrollspeed n true
5107 | @left | @right | @kpleft | @kpright when not (Wsi.withalt mask) ->
5108 if canpan ()
5109 then
5110 let dx =
5111 if ctrl
5112 then state.winw / 2
5113 else conf.hscrollstep
5115 let dx = if key = @left || key = @kpleft then dx else -dx in
5116 state.x <- panbound (state.x + dx);
5117 gotoy_and_clear_text state.y
5118 else (
5119 state.text <- E.s;
5120 G.postRedisplay "left/right"
5123 | @prior | @kpprior ->
5124 let y =
5125 if ctrl
5126 then
5127 match state.layout with
5128 | [] -> state.y
5129 | l :: _ -> state.y - l.pagey
5130 else
5131 clamp (pgscale (-state.winh))
5133 gotoghyll y
5135 | @next | @kpnext ->
5136 let y =
5137 if ctrl
5138 then
5139 match List.rev state.layout with
5140 | [] -> state.y
5141 | l :: _ -> getpagey l.pageno
5142 else
5143 clamp (pgscale state.winh)
5145 gotoghyll y
5147 | @g | @home | @kphome ->
5148 addnav ();
5149 gotoghyll 0
5150 | @G | @jend | @kpend ->
5151 addnav ();
5152 gotoghyll (clamp state.maxy)
5154 | @right | @kpright when Wsi.withalt mask ->
5155 gotoghyll (getnav 1)
5156 | @left | @kpleft when Wsi.withalt mask ->
5157 gotoghyll (getnav ~-1)
5159 | @r ->
5160 reload ()
5162 | @v when conf.debug ->
5163 state.rects <- [];
5164 List.iter (fun l ->
5165 match getopaque l.pageno with
5166 | None -> ()
5167 | Some opaque ->
5168 let x0, y0, x1, y1 = pagebbox opaque in
5169 let a,b = float x0, float y0 in
5170 let c,d = float x1, float y0 in
5171 let e,f = float x1, float y1 in
5172 let h,j = float x0, float y1 in
5173 let rect = (a,b,c,d,e,f,h,j) in
5174 debugrect rect;
5175 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
5176 state.rects <- (l.pageno, color, rect) :: state.rects;
5177 ) state.layout;
5178 G.postRedisplay "v";
5180 | @pipe ->
5181 let mode = state.mode in
5182 let cmd = ref E.s in
5183 let onleave = function
5184 | Cancel -> state.mode <- mode
5185 | Confirm ->
5186 List.iter (fun l ->
5187 match getopaque l.pageno with
5188 | Some opaque -> pipesel opaque !cmd
5189 | None -> ()) state.layout;
5190 state.mode <- mode
5192 let ondone s =
5193 cbput state.hists.sel s;
5194 cmd := s
5196 let te =
5197 "| ", !cmd, Some (onhist state.hists.sel), textentry, ondone, true
5199 G.postRedisplay "|";
5200 state.mode <- Textentry (te, onleave);
5202 | _ ->
5203 vlog "huh? %s" (Wsi.keyname key)
5206 let linknavkeyboard key mask linknav =
5207 let getpage pageno =
5208 let rec loop = function
5209 | [] -> None
5210 | l :: _ when l.pageno = pageno -> Some l
5211 | _ :: rest -> loop rest
5212 in loop state.layout
5214 let doexact (pageno, n) =
5215 match getopaque pageno, getpage pageno with
5216 | Some opaque, Some l ->
5217 if key = @enter || key = @kpenter
5218 then
5219 let under = getlink opaque n in
5220 G.postRedisplay "link gotounder";
5221 gotounder under;
5222 state.mode <- View;
5223 else
5224 let opt, dir =
5225 match key with
5226 | @home ->
5227 Some (findlink opaque LDfirst), -1
5229 | @jend ->
5230 Some (findlink opaque LDlast), 1
5232 | @left ->
5233 Some (findlink opaque (LDleft n)), -1
5235 | @right ->
5236 Some (findlink opaque (LDright n)), 1
5238 | @up ->
5239 Some (findlink opaque (LDup n)), -1
5241 | @down ->
5242 Some (findlink opaque (LDdown n)), 1
5244 | _ -> None, 0
5246 let pwl l dir =
5247 begin match findpwl l.pageno dir with
5248 | Pwlnotfound -> ()
5249 | Pwl pageno ->
5250 let notfound dir =
5251 state.mode <- LinkNav (Ltgendir dir);
5252 let y, h = getpageyh pageno in
5253 let y =
5254 if dir < 0
5255 then y + h - state.winh
5256 else y
5258 gotoy y
5260 begin match getopaque pageno, getpage pageno with
5261 | Some opaque, Some _ ->
5262 let link =
5263 let ld = if dir > 0 then LDfirst else LDlast in
5264 findlink opaque ld
5266 begin match link with
5267 | Lfound m ->
5268 showlinktype (getlink opaque m);
5269 state.mode <- LinkNav (Ltexact (pageno, m));
5270 G.postRedisplay "linknav jpage";
5271 | Lnotfound -> notfound dir
5272 end;
5273 | _ -> notfound dir
5274 end;
5275 end;
5277 begin match opt with
5278 | Some Lnotfound -> pwl l dir;
5279 | Some (Lfound m) ->
5280 if m = n
5281 then pwl l dir
5282 else (
5283 let _, y0, _, y1 = getlinkrect opaque m in
5284 if y0 < l.pagey
5285 then gotopage1 l.pageno y0
5286 else (
5287 let d = fstate.fontsize + 1 in
5288 if y1 - l.pagey > l.pagevh - d
5289 then gotopage1 l.pageno (y1 - state.winh - hscrollh () + d)
5290 else G.postRedisplay "linknav";
5292 showlinktype (getlink opaque m);
5293 state.mode <- LinkNav (Ltexact (l.pageno, m));
5296 | None -> viewkeyboard key mask
5297 end;
5298 | _ -> viewkeyboard key mask
5300 if key = @insert
5301 then (
5302 state.mode <- View;
5303 G.postRedisplay "leave linknav"
5305 else
5306 match linknav with
5307 | Ltgendir _ | Ltnotready _ -> viewkeyboard key mask
5308 | Ltexact exact -> doexact exact
5311 let keyboard key mask =
5312 if (key = @g && Wsi.withctrl mask) && not (istextentry state.mode)
5313 then wcmd "interrupt"
5314 else state.uioh <- state.uioh#key key mask
5317 let birdseyekeyboard key mask
5318 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5319 let incr =
5320 match conf.columns with
5321 | Csingle _ -> 1
5322 | Cmulti ((c, _, _), _) -> c
5323 | Csplit _ -> failwith "bird's eye split mode"
5325 let pgh layout = List.fold_left
5326 (fun m l -> max l.pageh m) state.winh layout in
5327 match key with
5328 | @l when Wsi.withctrl mask ->
5329 let y, h = getpageyh pageno in
5330 let top = (state.winh - h) / 2 in
5331 gotoy (max 0 (y - top))
5332 | @enter | @kpenter -> leavebirdseye beye false
5333 | @escape -> leavebirdseye beye true
5334 | @up -> upbirdseye incr beye
5335 | @down -> downbirdseye incr beye
5336 | @left -> upbirdseye 1 beye
5337 | @right -> downbirdseye 1 beye
5339 | @prior ->
5340 begin match state.layout with
5341 | l :: _ ->
5342 if l.pagey != 0
5343 then (
5344 state.mode <- Birdseye (
5345 oconf, leftx, l.pageno, hooverpageno, anchor
5347 gotopage1 l.pageno 0;
5349 else (
5350 let layout = layout state.x (state.y-state.winh)
5351 state.winw
5352 (pgh state.layout) in
5353 match layout with
5354 | [] -> gotoy (clamp (-state.winh))
5355 | l :: _ ->
5356 state.mode <- Birdseye (
5357 oconf, leftx, l.pageno, hooverpageno, anchor
5359 gotopage1 l.pageno 0
5362 | [] -> gotoy (clamp (-state.winh))
5363 end;
5365 | @next ->
5366 begin match List.rev state.layout with
5367 | l :: _ ->
5368 let layout = layout state.x
5369 (state.y + (pgh state.layout))
5370 state.winw state.winh in
5371 begin match layout with
5372 | [] ->
5373 let incr = l.pageh - l.pagevh in
5374 if incr = 0
5375 then (
5376 state.mode <-
5377 Birdseye (
5378 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5380 G.postRedisplay "birdseye pagedown";
5382 else gotoy (clamp (incr + conf.interpagespace*2));
5384 | l :: _ ->
5385 state.mode <-
5386 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5387 gotopage1 l.pageno 0;
5390 | [] -> gotoy (clamp state.winh)
5391 end;
5393 | @home ->
5394 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5395 gotopage1 0 0
5397 | @jend ->
5398 let pageno = state.pagecount - 1 in
5399 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5400 if not (pagevisible state.layout pageno)
5401 then
5402 let h =
5403 match List.rev state.pdims with
5404 | [] -> state.winh
5405 | (_, _, h, _) :: _ -> h
5407 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5408 else G.postRedisplay "birdseye end";
5410 | _ -> viewkeyboard key mask
5413 let drawpage l =
5414 let color =
5415 match state.mode with
5416 | Textentry _ -> scalecolor 0.4
5417 | LinkNav _
5418 | View -> scalecolor 1.0
5419 | Birdseye (_, _, pageno, hooverpageno, _) ->
5420 if l.pageno = hooverpageno
5421 then scalecolor 0.9
5422 else (
5423 if l.pageno = pageno
5424 then (
5425 let c = scalecolor 1.0 in
5426 GlDraw.color c;
5427 GlDraw.line_width 3.0;
5428 let dispx = xadjsb () + l.pagedispx in
5429 linerect
5430 (float (dispx-1)) (float (l.pagedispy-1))
5431 (float (dispx+l.pagevw+1))
5432 (float (l.pagedispy+l.pagevh+1))
5434 GlDraw.line_width 1.0;
5437 else scalecolor 0.8
5440 drawtiles l color;
5443 let postdrawpage l linkindexbase =
5444 match getopaque l.pageno with
5445 | Some opaque ->
5446 if tileready l l.pagex l.pagey
5447 then
5448 let x = l.pagedispx - l.pagex + xadjsb ()
5449 and y = l.pagedispy - l.pagey in
5450 let hlmask =
5451 match conf.columns with
5452 | Csingle _ | Cmulti _ ->
5453 (if conf.hlinks then 1 else 0)
5454 + (if state.glinks
5455 && not (isbirdseye state.mode) then 2 else 0)
5456 | Csplit _ -> 0
5458 let s =
5459 match state.mode with
5460 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5461 | Textentry _
5462 | Birdseye _
5463 | View
5464 | LinkNav _ -> E.s
5466 Hashtbl.find_all state.prects l.pageno |>
5467 List.iter (fun vals -> drawprect opaque x y vals);
5468 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5469 else 0
5470 | _ -> 0
5473 let scrollindicator () =
5474 let sbw, ph, sh = state.uioh#scrollph in
5475 let sbh, pw, sw = state.uioh#scrollpw in
5477 let x0,x1,hx0 =
5478 if conf.leftscroll
5479 then (0, sbw, sbw)
5480 else ((state.winw - sbw), state.winw, 0)
5483 GlDraw.color (0.64, 0.64, 0.64);
5484 filledrect (float x0) 0. (float x1) (float state.winh);
5485 filledrect
5486 (float hx0) (float (state.winh - sbh))
5487 (float (hx0 + wadjsb () + state.winw)) (float state.winh)
5489 GlDraw.color (0.0, 0.0, 0.0);
5491 filledrect (float x0) ph (float x1) (ph +. sh);
5492 let pw = pw +. float hx0 in
5493 filledrect pw (float (state.winh - sbh)) (pw +. sw) (float state.winh);
5496 let showsel () =
5497 match state.mstate with
5498 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5501 | Msel ((x0, y0), (x1, y1)) ->
5502 let identify opaque l px py = Some (opaque, l.pageno, px, py) in
5503 let o0,n0,px0,py0 = onppundermouse identify x0 y0 (~< E.s, -1, 0, 0) in
5504 let _o1,n1,px1,py1 = onppundermouse identify x1 y1 (~< E.s, -1, 0, 0) in
5505 if n0 != -1 && n0 = n1 then seltext o0 (px0, py0, px1, py1);
5508 let showrects = function [] -> () | rects ->
5509 Gl.enable `blend;
5510 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5511 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5512 List.iter
5513 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5514 List.iter (fun l ->
5515 if l.pageno = pageno
5516 then (
5517 let dx = float (l.pagedispx - l.pagex) in
5518 let dy = float (l.pagedispy - l.pagey) in
5519 let r, g, b, alpha = c in
5520 GlDraw.color (r, g, b) ~alpha;
5521 Raw.sets_float state.vraw ~pos:0
5522 [| x0+.dx; y0+.dy;
5523 x1+.dx; y1+.dy;
5524 x3+.dx; y3+.dy;
5525 x2+.dx; y2+.dy |];
5526 GlArray.vertex `two state.vraw;
5527 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
5529 ) state.layout
5530 ) rects
5532 Gl.disable `blend;
5535 let display () =
5536 GlClear.color (scalecolor2 conf.bgcolor);
5537 GlClear.clear [`color];
5538 List.iter drawpage state.layout;
5539 let rects =
5540 match state.mode with
5541 | LinkNav (Ltexact (pageno, linkno)) ->
5542 begin match getopaque pageno with
5543 | Some opaque ->
5544 let dx = xadjsb () in
5545 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5546 let x0 = x0 + dx and x1 = x1 + dx in
5547 let color = (0.0, 0.0, 0.5, 0.5) in
5548 (pageno, color, (
5549 float x0, float y0,
5550 float x1, float y0,
5551 float x1, float y1,
5552 float x0, float y1)
5553 ) :: state.rects
5554 | None -> state.rects
5556 | LinkNav (Ltgendir _) | LinkNav (Ltnotready _)
5557 | Birdseye _
5558 | Textentry _
5559 | View -> state.rects
5561 showrects rects;
5562 let rec postloop linkindexbase = function
5563 | l :: rest ->
5564 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5565 postloop linkindexbase rest
5566 | [] -> ()
5568 showsel ();
5569 postloop 0 state.layout;
5570 state.uioh#display;
5571 begin match state.mstate with
5572 | Mzoomrect ((x0, y0), (x1, y1)) ->
5573 Gl.enable `blend;
5574 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5575 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5576 filledrect (float x0) (float y0) (float x1) (float y1);
5577 Gl.disable `blend;
5578 | Msel _
5579 | Mpan _
5580 | Mscrolly | Mscrollx
5581 | Mzoom _
5582 | Mnone -> ()
5583 end;
5584 enttext ();
5585 scrollindicator ();
5586 Wsi.swapb ();
5589 let zoomrect x y x1 y1 =
5590 let x0 = min x x1
5591 and x1 = max x x1
5592 and y0 = min y y1 in
5593 gotoy (state.y + y0);
5594 state.anchor <- getanchor ();
5595 let zoom = (float state.w) /. float (x1 - x0) in
5596 let margin =
5597 let simple () =
5598 let adjw = wadjsb () + state.winw in
5599 if state.w < adjw
5600 then (adjw - state.w) / 2
5601 else 0
5603 match conf.fitmodel with
5604 | FitWidth | FitProportional -> simple ()
5605 | FitPage ->
5606 match conf.columns with
5607 | Csplit _ ->
5608 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
5609 | Cmulti _ | Csingle _ -> simple ()
5611 state.x <- (state.x + margin) - x0;
5612 setzoom zoom;
5613 resetmstate ();
5616 let annot inline x y =
5617 match unproject x y with
5618 | Some (opaque, n, ux, uy) ->
5619 let add text =
5620 addannot opaque ux uy text;
5621 wcmd "freepage %s" (~> opaque);
5622 Hashtbl.remove state.pagemap (n, state.gen);
5623 flushtiles ();
5624 gotoy state.y
5626 if inline
5627 then
5628 let ondone s = add s in
5629 let mode = state.mode in
5630 state.mode <- Textentry (
5631 ("annotation: ", E.s, None, textentry, ondone, true),
5632 fun _ -> state.mode <- mode);
5633 state.text <- E.s;
5634 enttext ();
5635 G.postRedisplay "annot"
5636 else
5637 add @@ getusertext E.s
5638 | _ -> ()
5641 let zoomblock x y =
5642 let g opaque l px py =
5643 match rectofblock opaque px py with
5644 | Some a ->
5645 let x0 = a.(0) -. 20. in
5646 let x1 = a.(1) +. 20. in
5647 let y0 = a.(2) -. 20. in
5648 let zoom = (float state.w) /. (x1 -. x0) in
5649 let pagey = getpagey l.pageno in
5650 gotoy_and_clear_text (pagey + truncate y0);
5651 state.anchor <- getanchor ();
5652 let margin = (state.w - l.pagew)/2 in
5653 state.x <- -truncate x0 - margin;
5654 setzoom zoom;
5655 None
5656 | None -> None
5658 match conf.columns with
5659 | Csplit _ ->
5660 impmsg "block zooming does not work properly in split columns mode"
5661 | Cmulti _ | Csingle _ -> onppundermouse g x y ()
5664 let scrollx x =
5665 let winw = wadjsb () + state.winw - 1 in
5666 let s = float x /. float winw in
5667 let destx = truncate (float (state.w + winw) *. s) in
5668 state.x <- winw - destx;
5669 gotoy_and_clear_text state.y;
5670 state.mstate <- Mscrollx;
5673 let scrolly y =
5674 let s = float y /. float state.winh in
5675 let desty = truncate (float (state.maxy - state.winh) *. s) in
5676 gotoy_and_clear_text desty;
5677 state.mstate <- Mscrolly;
5680 let viewmulticlick clicks x y mask =
5681 let g opaque l px py =
5682 let mark =
5683 match clicks with
5684 | 2 -> Mark_word
5685 | 3 -> Mark_line
5686 | 4 -> Mark_block
5687 | _ -> Mark_page
5689 if markunder opaque px py mark
5690 then (
5691 Some (fun () ->
5692 let dopipe cmd =
5693 match getopaque l.pageno with
5694 | None -> ()
5695 | Some opaque -> pipesel opaque cmd
5697 state.roam <- (fun () -> dopipe conf.paxcmd);
5698 if not (Wsi.withctrl mask) then dopipe conf.selcmd;
5701 else None
5703 G.postRedisplay "viewmulticlick";
5704 onppundermouse g x y (fun () -> impmsg "nothing to select") ();
5707 let canselect () =
5708 match conf.columns with
5709 | Csplit _ -> false
5710 | Csingle _ | Cmulti _ -> conf.angle mod 360 = 0
5713 let viewmouse button down x y mask =
5714 match button with
5715 | n when (n == 4 || n == 5) && not down ->
5716 if Wsi.withctrl mask
5717 then (
5718 match state.mstate with
5719 | Mzoom (oldn, i) ->
5720 if oldn = n
5721 then (
5722 if i = 2
5723 then
5724 let incr =
5725 match n with
5726 | 5 ->
5727 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5728 | _ ->
5729 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5731 let zoom = conf.zoom -. incr in
5732 setzoom zoom;
5733 state.mstate <- Mzoom (n, 0);
5734 else
5735 state.mstate <- Mzoom (n, i+1);
5737 else state.mstate <- Mzoom (n, 0)
5739 | Msel _
5740 | Mpan _
5741 | Mscrolly | Mscrollx
5742 | Mzoomrect _
5743 | Mnone -> state.mstate <- Mzoom (n, 0)
5745 else (
5746 match state.autoscroll with
5747 | Some step -> setautoscrollspeed step (n=4)
5748 | None ->
5749 if conf.wheelbypage || conf.presentation
5750 then (
5751 if n = 4
5752 then prevpage ()
5753 else nextpage ()
5755 else
5756 let incr =
5757 if n = 4
5758 then -conf.scrollstep
5759 else conf.scrollstep
5761 let incr = incr * 2 in
5762 let y = clamp incr in
5763 gotoy_and_clear_text y
5766 | n when (n = 6 || n = 7) && not down && canpan () ->
5767 state.x <-
5768 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
5769 gotoy_and_clear_text state.y
5771 | 1 when Wsi.withshift mask ->
5772 state.mstate <- Mnone;
5773 if not down
5774 then (
5775 match unproject x y with
5776 | None -> ()
5777 | Some (_, pageno, ux, uy) ->
5778 let cmd = Printf.sprintf
5779 "%s %s %d %d %d"
5780 conf.stcmd state.path pageno ux uy
5782 match spawn cmd [] with
5783 | (exception exn) ->
5784 impmsg "execution of synctex command(%S) failed: %S"
5785 conf.stcmd @@ exntos exn
5786 | _pid -> ()
5789 | 1 when Wsi.withctrl mask ->
5790 if down
5791 then (
5792 Wsi.setcursor Wsi.CURSOR_FLEUR;
5793 state.mstate <- Mpan (x, y)
5795 else
5796 state.mstate <- Mnone
5798 | 3 ->
5799 if down
5800 then (
5801 if Wsi.withshift mask
5802 then (
5803 annot conf.annotinline x y;
5804 G.postRedisplay "addannot"
5806 else
5807 let p = (x, y) in
5808 Wsi.setcursor Wsi.CURSOR_CYCLE;
5809 state.mstate <- Mzoomrect (p, p)
5811 else (
5812 match state.mstate with
5813 | Mzoomrect ((x0, y0), _) ->
5814 if abs (x-x0) > 10 && abs (y - y0) > 10
5815 then zoomrect x0 y0 x y
5816 else (
5817 resetmstate ();
5818 G.postRedisplay "kill accidental zoom rect";
5820 | Msel _
5821 | Mpan _
5822 | Mscrolly | Mscrollx
5823 | Mzoom _
5824 | Mnone ->
5825 resetmstate ()
5828 | 1 when vscrollhit x ->
5829 if down
5830 then
5831 let _, position, sh = state.uioh#scrollph in
5832 if y > truncate position && y < truncate (position +. sh)
5833 then state.mstate <- Mscrolly
5834 else scrolly y
5835 else
5836 state.mstate <- Mnone
5838 | 1 when y > state.winh - hscrollh () ->
5839 if down
5840 then
5841 let _, position, sw = state.uioh#scrollpw in
5842 if x > truncate position && x < truncate (position +. sw)
5843 then state.mstate <- Mscrollx
5844 else scrollx x
5845 else
5846 state.mstate <- Mnone
5848 | 1 when state.bzoom -> if not down then zoomblock x y
5850 | 1 ->
5851 let dest = if down then getunder x y else Unone in
5852 begin match dest with
5853 | Ulinkgoto _
5854 | Ulinkuri _
5855 | Uremote _ | Uremotedest _
5856 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5857 gotounder dest
5859 | Unone when down ->
5860 Wsi.setcursor Wsi.CURSOR_FLEUR;
5861 state.mstate <- Mpan (x, y);
5863 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
5865 | Unone | Utext _ ->
5866 if down
5867 then (
5868 if canselect ()
5869 then (
5870 state.mstate <- Msel ((x, y), (x, y));
5871 G.postRedisplay "mouse select";
5874 else (
5875 match state.mstate with
5876 | Mnone -> ()
5878 | Mzoom _ | Mscrollx | Mscrolly ->
5879 state.mstate <- Mnone
5881 | Mzoomrect ((x0, y0), _) ->
5882 zoomrect x0 y0 x y
5884 | Mpan _ ->
5885 Wsi.setcursor Wsi.CURSOR_INHERIT;
5886 state.mstate <- Mnone
5888 | Msel ((x0, y0), (x1, y1)) ->
5889 let rec loop = function
5890 | [] -> ()
5891 | l :: rest ->
5892 let inside =
5893 let a0 = l.pagedispy in
5894 let a1 = a0 + l.pagevh in
5895 let b0 = l.pagedispx in
5896 let b1 = b0 + l.pagevw in
5897 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5898 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5900 if inside
5901 then
5902 match getopaque l.pageno with
5903 | Some opaque ->
5904 let dosel cmd () =
5905 match Unix.pipe () with
5906 | (exception exn) ->
5907 impmsg "cannot create sel pipe: %s" @@
5908 exntos exn;
5909 | (r, w) ->
5910 let clo what fd =
5911 Ne.clo fd (fun msg ->
5912 dolog "%s close failed: %s" what msg)
5914 let pid =
5915 try spawn cmd [r, 0; w, -1]
5916 with exn ->
5917 dolog "cannot execute %S: %s"
5918 cmd @@ exntos exn;
5921 if pid > 0
5922 then (
5923 copysel w opaque;
5924 G.postRedisplay "copysel";
5926 else clo "Msel pipe/w" w;
5927 clo "Msel pipe/r" r;
5929 dosel conf.selcmd ();
5930 state.roam <- dosel conf.paxcmd;
5931 | None -> ()
5932 else loop rest
5934 loop state.layout;
5935 resetmstate ();
5939 | _ -> ()
5942 let birdseyemouse button down x y mask
5943 (conf, leftx, _, hooverpageno, anchor) =
5944 match button with
5945 | 1 when down ->
5946 let rec loop = function
5947 | [] -> ()
5948 | l :: rest ->
5949 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5950 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5951 then (
5952 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5954 else loop rest
5956 loop state.layout
5957 | 3 -> ()
5958 | _ -> viewmouse button down x y mask
5961 let uioh = object
5962 method display = ()
5964 method key key mask =
5965 begin match state.mode with
5966 | Textentry textentry -> textentrykeyboard key mask textentry
5967 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5968 | View -> viewkeyboard key mask
5969 | LinkNav linknav -> linknavkeyboard key mask linknav
5970 end;
5971 state.uioh
5973 method button button bstate x y mask =
5974 begin match state.mode with
5975 | LinkNav _
5976 | View -> viewmouse button bstate x y mask
5977 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5978 | Textentry _ -> ()
5979 end;
5980 state.uioh
5982 method multiclick clicks x y mask =
5983 begin match state.mode with
5984 | LinkNav _
5985 | View -> viewmulticlick clicks x y mask
5986 | Birdseye _
5987 | Textentry _ -> ()
5988 end;
5989 state.uioh
5991 method motion x y =
5992 begin match state.mode with
5993 | Textentry _ -> ()
5994 | View | Birdseye _ | LinkNav _ ->
5995 match state.mstate with
5996 | Mzoom _ | Mnone -> ()
5998 | Mpan (x0, y0) ->
5999 let dx = x - x0
6000 and dy = y0 - y in
6001 state.mstate <- Mpan (x, y);
6002 if canpan ()
6003 then state.x <- panbound (state.x + dx);
6004 let y = clamp dy in
6005 gotoy_and_clear_text y
6007 | Msel (a, _) ->
6008 state.mstate <- Msel (a, (x, y));
6009 G.postRedisplay "motion select";
6011 | Mscrolly ->
6012 let y = min state.winh (max 0 y) in
6013 scrolly y
6015 | Mscrollx ->
6016 let x = min state.winw (max 0 x) in
6017 scrollx x
6019 | Mzoomrect (p0, _) ->
6020 state.mstate <- Mzoomrect (p0, (x, y));
6021 G.postRedisplay "motion zoomrect";
6022 end;
6023 state.uioh
6025 method pmotion x y =
6026 begin match state.mode with
6027 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6028 let rec loop = function
6029 | [] ->
6030 if hooverpageno != -1
6031 then (
6032 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6033 G.postRedisplay "pmotion birdseye no hoover";
6035 | l :: rest ->
6036 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6037 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6038 then (
6039 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6040 G.postRedisplay "pmotion birdseye hoover";
6042 else loop rest
6044 loop state.layout
6046 | Textentry _ -> ()
6048 | LinkNav _
6049 | View ->
6050 match state.mstate with
6051 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ -> ()
6052 | Mnone ->
6053 updateunder x y;
6054 if canselect ()
6055 then
6056 match conf.pax with
6057 | None -> ()
6058 | Some r ->
6059 let past, _, _ = !r in
6060 let now = now () in
6061 let delta = now -. past in
6062 if delta > 0.01
6063 then paxunder x y
6064 else r := (now, x, y)
6065 end;
6066 state.uioh
6068 method infochanged _ = ()
6070 method scrollph =
6071 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6072 let p, h =
6073 if maxy = 0
6074 then 0.0, float state.winh
6075 else scrollph state.y maxy
6077 vscrollw (), p, h
6079 method scrollpw =
6080 let winw = wadjsb () + state.winw in
6081 let fwinw = float winw in
6082 let sw =
6083 let sw = fwinw /. float state.w in
6084 let sw = fwinw *. sw in
6085 max sw (float conf.scrollh)
6087 let position =
6088 let maxx = state.w + winw in
6089 let x = winw - state.x in
6090 let percent = float x /. float maxx in
6091 (fwinw -. sw) *. percent
6093 hscrollh (), position, sw
6095 method modehash =
6096 let modename =
6097 match state.mode with
6098 | LinkNav _ -> "links"
6099 | Textentry _ -> "textentry"
6100 | Birdseye _ -> "birdseye"
6101 | View -> "view"
6103 findkeyhash conf modename
6105 method eformsgs = true
6106 method alwaysscrolly = false
6107 end;;
6109 let adderrmsg src msg =
6110 Buffer.add_string state.errmsgs msg;
6111 state.newerrmsgs <- true;
6112 G.postRedisplay src
6115 let adderrfmt src fmt =
6116 Format.ksprintf (fun s -> adderrmsg src s) fmt;
6119 let addrect pageno r g b a x0 y0 x1 y1 =
6120 Hashtbl.add state.prects pageno [|r; g; b; a; x0; y0; x1; y1|];
6123 let ract cmds =
6124 let cl = splitatspace cmds in
6125 let scan s fmt f =
6126 try Scanf.sscanf s fmt f
6127 with exn ->
6128 adderrfmt "remote exec"
6129 "error processing '%S': %s\n" cmds @@ exntos exn
6131 let rectx s pageno (r, g, b, a) x0 y0 x1 y1 =
6132 vlog "%s page %d color (%f %f %f %f) x0,y0,x1,y1 = %f %f %f %f"
6133 s pageno r g b a x0 y0 x1 y1;
6134 onpagerect
6135 pageno
6136 (fun w h ->
6137 let _,w1,h1,_ = getpagedim pageno in
6138 let sw = float w1 /. float w
6139 and sh = float h1 /. float h in
6140 let x0s = x0 *. sw
6141 and x1s = x1 *. sw
6142 and y0s = y0 *. sh
6143 and y1s = y1 *. sh in
6144 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
6145 let color = (r, g, b, a) in
6146 if conf.verbose then debugrect rect;
6147 state.rects <- (pageno, color, rect) :: state.rects;
6148 G.postRedisplay s;
6151 match cl with
6152 | "reload" :: [] -> reload ()
6153 | "goto" :: args :: [] ->
6154 scan args "%u %f %f"
6155 (fun pageno x y ->
6156 let cmd, _ = state.geomcmds in
6157 if emptystr cmd
6158 then gotopagexy !wtmode pageno x y
6159 else
6160 let f prevf () =
6161 gotopagexy !wtmode pageno x y;
6162 prevf ()
6164 state.reprf <- f state.reprf
6166 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
6167 | "gotor" :: args :: [] ->
6168 scan args "%S %u"
6169 (fun filename pageno -> gotounder (Uremote (filename, pageno)))
6170 | "gotord" :: args :: [] ->
6171 scan args "%S %S"
6172 (fun filename dest -> gotounder (Uremotedest (filename, dest)))
6173 | "rect" :: args :: [] ->
6174 scan args "%u %u %f %f %f %f"
6175 (fun pageno c x0 y0 x1 y1 ->
6176 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
6177 rectx "rect" pageno color x0 y0 x1 y1;
6179 | "prect" :: args :: [] ->
6180 scan args "%u %f %f %f %f %f %f %f %f"
6181 (fun pageno r g b alpha x0 y0 x1 y1 ->
6182 addrect pageno r g b alpha x0 y0 x1 y1;
6183 G.postRedisplay "prect"
6185 | "pgoto" :: args :: [] ->
6186 scan args "%u %f %f"
6187 (fun pageno x y ->
6188 let optopaque =
6189 match getopaque pageno with
6190 | Some opaque -> opaque
6191 | None -> ~< E.s
6193 pgoto optopaque pageno x y;
6194 let rec fixx = function
6195 | [] -> ()
6196 | l :: rest ->
6197 if l.pageno = pageno
6198 then (
6199 state.x <- state.x - l.pagedispx;
6200 gotoy state.y;
6202 else fixx rest
6204 let layout =
6205 let mult =
6206 match conf.columns with
6207 | Csingle _ | Csplit _ -> 1
6208 | Cmulti ((n, _, _), _) -> n
6210 layout 0 state.y (state.winw * mult) state.winh
6212 fixx layout
6214 | "activatewin" :: [] -> Wsi.activatewin ()
6215 | "quit" :: [] -> raise Quit
6216 | "clearrects" :: [] ->
6217 Hashtbl.clear state.prects;
6218 G.postRedisplay "clearrects"
6219 | _ ->
6220 adderrfmt "remote command"
6221 "error processing remote command: %S\n" cmds;
6224 let remote =
6225 let scratch = Bytes.create 80 in
6226 let buf = Buffer.create 80 in
6227 fun fd ->
6228 match tempfailureretry (Unix.read fd scratch 0) 80 with
6229 | (exception Unix.Unix_error (Unix.EAGAIN, _, _)) -> None
6230 | 0 ->
6231 Unix.close fd;
6232 if Buffer.length buf > 0
6233 then (
6234 let s = Buffer.contents buf in
6235 Buffer.clear buf;
6236 ract s;
6238 None
6239 | n ->
6240 let rec eat ppos =
6241 let nlpos =
6242 match Bytes.index_from scratch ppos '\n' with
6243 | pos -> if pos >= n then -1 else pos
6244 | (exception Not_found) -> -1
6246 if nlpos >= 0
6247 then (
6248 Buffer.add_subbytes buf scratch ppos (nlpos-ppos);
6249 let s = Buffer.contents buf in
6250 Buffer.clear buf;
6251 ract s;
6252 eat (nlpos+1);
6254 else (
6255 Buffer.add_subbytes buf scratch ppos (n-ppos);
6256 Some fd
6258 in eat 0
6261 let remoteopen path =
6262 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
6263 with exn ->
6264 adderrfmt "remoteopen" "error opening %S: %s" path @@ exntos exn;
6265 None
6268 let () =
6269 let gcconfig = ref E.s in
6270 let trimcachepath = ref E.s in
6271 let rcmdpath = ref E.s in
6272 let pageno = ref None in
6273 let rootwid = ref 0 in
6274 let openlast = ref false in
6275 let nofc = ref false in
6276 let doreap = ref false in
6277 selfexec := Sys.executable_name;
6278 Arg.parse
6279 (Arg.align
6280 [("-p", Arg.String (fun s -> state.password <- s),
6281 "<password> Set password");
6283 ("-f", Arg.String
6284 (fun s ->
6285 Config.fontpath := s;
6286 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
6288 "<path> Set path to the user interface font");
6290 ("-c", Arg.String
6291 (fun s ->
6292 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
6293 Config.confpath := s),
6294 "<path> Set path to the configuration file");
6296 ("-last", Arg.Set openlast, " Open last document");
6298 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
6299 "<page-number> Jump to page");
6301 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6302 "<path> Set path to the trim cache file");
6304 ("-dest", Arg.String (fun s -> state.nameddest <- s),
6305 "<named-destination> Set named destination");
6307 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
6308 ("-cxack", Arg.Set cxack, " Cut corners");
6310 ("-remote", Arg.String (fun s -> rcmdpath := s),
6311 "<path> Set path to the remote commands source");
6313 ("-origin", Arg.String (fun s -> state.origin <- s),
6314 "<original-path> Set original path");
6316 ("-gc", Arg.Set_string gcconfig,
6317 "<script-path> Collect garbage with the help of a script");
6319 ("-nofc", Arg.Set nofc, " Do not use fontconfig");
6321 ("-v", Arg.Unit (fun () ->
6322 Printf.printf
6323 "%s\nconfiguration path: %s\n"
6324 (version ())
6325 Config.defconfpath
6327 exit 0), " Print version and exit");
6329 ("-embed", Arg.Set_int rootwid,
6330 "<window-id> Embed into window")
6333 (fun s -> state.path <- s)
6334 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6336 if !wtmode
6337 then selfexec := !selfexec ^ " -wtmode";
6339 let histmode = emptystr state.path && not !openlast in
6341 if not (Config.load !openlast)
6342 then dolog "failed to load configuration";
6343 begin match !pageno with
6344 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
6345 | None -> ()
6346 end;
6348 if nonemptystr !gcconfig
6349 then (
6350 let (c, s) =
6351 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6352 | (exception exn) -> error "socketpair for gc failed: %s" @@ exntos exn
6353 | fds -> fds
6355 match spawn !gcconfig [(c, 0); (c, 1); (s, -1)] with
6356 | (exception exn) -> error "failed to execute gc script: %s" @@ exntos exn
6357 | _pid ->
6358 Ne.clo c @@ (fun s -> error "failed to close gc fd %s" s);
6359 Config.gc s;
6360 exit 0
6363 let wsfd, winw, winh = Wsi.init (object (self)
6364 val mutable m_clicks = 0
6365 val mutable m_click_x = 0
6366 val mutable m_click_y = 0
6367 val mutable m_lastclicktime = infinity
6369 method private cleanup =
6370 state.roam <- noroam;
6371 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap
6372 method expose = G.postRedisplay "expose"
6373 method visible v =
6374 let name =
6375 match v with
6376 | Wsi.Unobscured -> "unobscured"
6377 | Wsi.PartiallyObscured -> "partiallyobscured"
6378 | Wsi.FullyObscured -> "fullyobscured"
6380 vlog "visibility change %s" name
6381 method display = display ()
6382 method map mapped = vlog "mapped %b" mapped
6383 method reshape w h =
6384 self#cleanup;
6385 reshape w h
6386 method mouse b d x y m =
6387 if d && canselect ()
6388 then (
6389 (* http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx *)
6390 m_click_x <- x;
6391 m_click_y <- y;
6392 if b = 1
6393 then (
6394 let t = now () in
6395 if abs x - m_click_x > 10
6396 || abs y - m_click_y > 10
6397 || abs_float (t -. m_lastclicktime) > 0.3
6398 then m_clicks <- 0;
6399 m_clicks <- m_clicks + 1;
6400 m_lastclicktime <- t;
6401 if m_clicks = 1
6402 then (
6403 self#cleanup;
6404 G.postRedisplay "cleanup";
6405 state.uioh <- state.uioh#button b d x y m;
6407 else state.uioh <- state.uioh#multiclick m_clicks x y m
6409 else (
6410 self#cleanup;
6411 m_clicks <- 0;
6412 m_lastclicktime <- infinity;
6413 state.uioh <- state.uioh#button b d x y m
6416 else (
6417 state.uioh <- state.uioh#button b d x y m
6419 method motion x y =
6420 state.mpos <- (x, y);
6421 state.uioh <- state.uioh#motion x y
6422 method pmotion x y =
6423 state.mpos <- (x, y);
6424 state.uioh <- state.uioh#pmotion x y
6425 method key k m =
6426 let mascm = m land (
6427 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6428 ) in
6429 let keyboard k m =
6430 let x = state.x and y = state.y in
6431 keyboard k m;
6432 if x != state.x || y != state.y then self#cleanup
6434 match state.keystate with
6435 | KSnone ->
6436 let km = k, mascm in
6437 begin
6438 match
6439 let modehash = state.uioh#modehash in
6440 try Hashtbl.find modehash km
6441 with Not_found ->
6442 try Hashtbl.find (findkeyhash conf "global") km
6443 with Not_found -> KMinsrt (k, m)
6444 with
6445 | KMinsrt (k, m) -> keyboard k m
6446 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6447 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6449 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6450 List.iter (fun (k, m) -> keyboard k m) insrt;
6451 state.keystate <- KSnone
6452 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6453 state.keystate <- KSinto (keys, insrt)
6454 | KSinto _ -> state.keystate <- KSnone
6456 method enter x y =
6457 state.mpos <- (x, y);
6458 state.uioh <- state.uioh#pmotion x y
6459 method leave = state.mpos <- (-1, -1)
6460 method winstate wsl = state.winstate <- wsl
6461 method quit = raise Quit
6462 end) !rootwid conf.cwinw conf.cwinh platform in
6464 state.wsfd <- wsfd;
6466 if not (
6467 List.exists GlMisc.check_extension
6468 [ "GL_ARB_texture_rectangle"
6469 ; "GL_EXT_texture_recangle"
6470 ; "GL_NV_texture_rectangle" ]
6472 then (dolog "OpenGL does not suppport rectangular textures"; exit 1);
6474 if (
6475 let r = GlMisc.get_string `renderer in
6476 let p = "Mesa DRI Intel(" in
6477 let l = String.length p in
6478 String.length r > l && String.sub r 0 l = p
6480 then (
6481 defconf.sliceheight <- 1024;
6482 defconf.texcount <- 32;
6483 defconf.usepbo <- true;
6486 let cs, ss =
6487 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6488 | (exception exn) ->
6489 dolog "socketpair failed: %s" @@ exntos exn;
6490 exit 1
6491 | (r, w) ->
6492 cloexec r;
6493 cloexec w;
6494 r, w
6497 setcheckers conf.checkers;
6499 init cs (
6500 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
6501 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6502 !Config.fontpath, !trimcachepath,
6503 GlMisc.check_extension "GL_ARB_pixel_buffer_object",
6504 not !nofc
6506 List.iter GlArray.enable [`texture_coord; `vertex];
6507 state.ss <- ss;
6508 reshape ~firsttime:true winw winh;
6509 state.uioh <- uioh;
6510 if histmode
6511 then (
6512 Wsi.settitle "llpp (history)";
6513 enterhistmode ();
6515 else (
6516 state.text <- "Opening " ^ (mbtoutf8 state.path);
6517 opendoc state.path state.password;
6519 display ();
6520 Wsi.mapwin ();
6521 Wsi.setcursor Wsi.CURSOR_INHERIT;
6522 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
6524 let rec reap () =
6525 match Unix.waitpid [Unix.WNOHANG] ~-1 with
6526 | (exception (Unix.Unix_error (Unix.ECHILD, _, _))) -> ()
6527 | (exception exn) -> dolog "Unix.waitpid: %s" @@ exntos exn
6528 | 0, _ -> ()
6529 | _pid, _status -> reap ()
6531 Sys.set_signal Sys.sigchld (Sys.Signal_handle (fun _ -> doreap := true));
6533 let optrfd =
6534 ref (
6535 if nonemptystr !rcmdpath
6536 then remoteopen !rcmdpath
6537 else None
6541 let rec loop deadline =
6542 if !doreap
6543 then (
6544 doreap := false;
6545 reap ()
6547 let r = [state.ss; state.wsfd] in
6548 let r =
6549 match !optrfd with
6550 | None -> r
6551 | Some fd -> fd :: r
6553 if state.redisplay
6554 then (
6555 state.redisplay <- false;
6556 display ();
6558 let timeout =
6559 let now = now () in
6560 if deadline > now
6561 then (
6562 if deadline = infinity
6563 then ~-.1.0
6564 else max 0.0 (deadline -. now)
6566 else 0.0
6568 let r, _, _ =
6569 try Unix.select r [] [] timeout
6570 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6572 begin match r with
6573 | [] ->
6574 state.ghyll None;
6575 let newdeadline =
6576 if state.ghyll == noghyll
6577 then
6578 match state.autoscroll with
6579 | Some step when step != 0 ->
6580 let y = state.y + step in
6581 let y =
6582 if y < 0
6583 then state.maxy
6584 else if y >= state.maxy then 0 else y
6586 if state.mode = View
6587 then gotoy_and_clear_text y
6588 else gotoy y;
6589 deadline +. 0.01
6590 | _ -> infinity
6591 else deadline +. 0.01
6593 loop newdeadline
6595 | l ->
6596 let rec checkfds = function
6597 | [] -> ()
6598 | fd :: rest when fd = state.ss ->
6599 let cmd = readcmd state.ss in
6600 act cmd;
6601 checkfds rest
6603 | fd :: rest when fd = state.wsfd ->
6604 Wsi.readresp fd;
6605 checkfds rest
6607 | fd :: rest when Some fd = !optrfd ->
6608 begin match remote fd with
6609 | None -> optrfd := remoteopen !rcmdpath;
6610 | opt -> optrfd := opt
6611 end;
6612 checkfds rest
6614 | _ :: rest ->
6615 dolog "select returned unknown descriptor";
6616 checkfds rest
6618 checkfds l;
6619 let newdeadline =
6620 let deadline1 =
6621 if deadline = infinity
6622 then now () +. 0.01
6623 else deadline
6625 match state.autoscroll with
6626 | Some step when step != 0 -> deadline1
6627 | _ -> if state.ghyll == noghyll then infinity else deadline1
6629 loop newdeadline
6630 end;
6633 loop infinity;
6634 with Quit ->
6635 Config.save leavebirdseye;
6636 if hasunsavedchanges ()
6637 then save ();