Address #33
[llpp.git] / main.ml
blob976c2c3333a026309988a3082b32015cc8c474f9
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 bousable : unit -> bool = "ml_bo_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";;
51 external wcmd : Unix.file_descr -> bytes -> int -> unit = "ml_wcmd";;
52 external rcmd : Unix.file_descr -> string = "ml_rcmd";;
54 let selfexec = ref E.s;;
55 let opengl_has_pbo = ref false;;
57 let drawstring size x y s =
58 Gl.enable `blend;
59 Gl.enable `texture_2d;
60 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
61 ignore (drawstr size x y s);
62 Gl.disable `blend;
63 Gl.disable `texture_2d;
66 let drawstring1 size x y s =
67 drawstr size x y s;
70 let drawstring2 size x y fmt =
71 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
74 let _debugl l =
75 dolog "l %d dim=%d {" l.pageno l.pagedimno;
76 dolog " WxH %dx%d" l.pagew l.pageh;
77 dolog " vWxH %dx%d" l.pagevw l.pagevh;
78 dolog " pagex,y %d,%d" l.pagex l.pagey;
79 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
80 dolog " column %d" l.pagecol;
81 dolog "}";
84 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
85 dolog "rect {";
86 dolog " x0,y0=(% f, % f)" x0 y0;
87 dolog " x1,y1=(% f, % f)" x1 y1;
88 dolog " x2,y2=(% f, % f)" x2 y2;
89 dolog " x3,y3=(% f, % f)" x3 y3;
90 dolog "}";
93 let isbirdseye = function
94 | Birdseye _ -> true
95 | Textentry _
96 | View
97 | LinkNav _ -> false
100 let istextentry = function
101 | Textentry _ -> true
102 | Birdseye _
103 | View
104 | LinkNav _ -> false
107 let wtmode = ref false;;
108 let cxack = ref false;;
110 let pgscale h = truncate (float h *. conf.pgscale);;
112 let hscrollh () =
113 if not state.uioh#alwaysscrolly && (conf.scrollb land scrollbhv = 0)
114 || (state.x = 0 && state.w <= state.winw - conf.scrollbw)
115 then 0
116 else conf.scrollbw
119 let vscrollw () =
120 if not state.uioh#alwaysscrolly && (conf.scrollb land scrollbvv = 0)
121 then 0
122 else conf.scrollbw
125 let vscrollhit x =
126 if conf.leftscroll
127 then x < vscrollw ()
128 else x > state.winw - vscrollw ()
131 let wadjsb () = -vscrollw ();;
132 let xadjsb () = if conf.leftscroll then vscrollw () else 0;;
134 let setfontsize n =
135 fstate.fontsize <- n;
136 fstate.wwidth <- measurestr fstate.fontsize "w";
137 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
140 let vlog fmt =
141 if conf.verbose
142 then dolog fmt
143 else Printf.kprintf ignore fmt
146 let launchpath () =
147 if emptystr conf.pathlauncher
148 then dolog "%s" state.path
149 else (
150 let command = Str.global_replace percentsre state.path conf.pathlauncher in
151 match spawn command [] with
152 | _pid -> ()
153 | (exception exn) ->
154 dolog "failed to execute `%s': %s" command @@ exntos exn
158 module G =
159 struct
160 let postRedisplay who =
161 vlog "redisplay for [%S]" who;
162 state.redisplay <- true;
164 end;;
166 let getopaque pageno =
167 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
168 with Not_found -> None
171 let pagetranslatepoint l x y =
172 let dy = y - l.pagedispy in
173 let y = dy + l.pagey in
174 let dx = x - l.pagedispx in
175 let x = dx + l.pagex in
176 (x, y);
179 let onppundermouse g x y d =
180 let rec f = function
181 | l :: rest ->
182 begin match getopaque l.pageno with
183 | Some opaque ->
184 let x0 = l.pagedispx in
185 let x1 = x0 + l.pagevw in
186 let y0 = l.pagedispy in
187 let y1 = y0 + l.pagevh in
188 if y >= y0 && y <= y1 && x >= x0 && x <= x1
189 then
190 let px, py = pagetranslatepoint l x y in
191 match g opaque l px py with
192 | Some res -> res
193 | None -> f rest
194 else f rest
195 | _ ->
196 f rest
198 | [] -> d
200 f state.layout
203 let getunder x y =
204 let g opaque l px py =
205 if state.bzoom
206 then (
207 match rectofblock opaque px py with
208 | Some [|x0;x1;y0;y1|] ->
209 let ox = xadjsb () |> float in
210 let rect = (x0+.ox, y0, x1+.ox, y0, x1+.ox, y1, x0+.ox, y1) in
211 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
212 state.rects <- [l.pageno, color, rect];
213 G.postRedisplay "getunder";
214 | _otherwise -> ()
216 let under = whatsunder opaque px py in
217 if under = Unone then None else Some under
219 onppundermouse g x y Unone
222 let unproject x y =
223 let g opaque l x y =
224 match unproject opaque x y with
225 | Some (x, y) -> Some (Some (opaque, l.pageno, x, y))
226 | None -> None
228 onppundermouse g x y None;
231 let showtext c s =
232 state.text <- Printf.sprintf "%c%s" c s;
233 G.postRedisplay "showtext";
236 let impmsg fmt =
237 Format.ksprintf (fun s -> showtext '!' s) fmt;
240 let pipesel opaque cmd =
241 if hassel opaque
242 then
243 match Unix.pipe () with
244 | (exception exn) -> dolog "pipesel cannot create pipe: %S" @@ exntos exn;
245 | (r, w) ->
246 let doclose what fd =
247 Ne.clo fd (fun msg -> dolog "%s close failed: %s" what msg)
249 let pid =
250 try spawn cmd [r, 0; w, -1]
251 with exn ->
252 dolog "cannot execute %S: %s" cmd @@ exntos exn;
255 if pid > 0
256 then (
257 copysel w opaque;
258 G.postRedisplay "pipesel";
260 else doclose "pipesel pipe/w" w;
261 doclose "pipesel pipe/r" r;
264 let paxunder x y =
265 let g opaque l px py =
266 if markunder opaque px py conf.paxmark
267 then (
268 Some (fun () ->
269 match getopaque l.pageno with
270 | None -> ()
271 | Some opaque -> pipesel opaque conf.paxcmd
274 else None
276 G.postRedisplay "paxunder";
277 if conf.paxmark = Mark_page
278 then
279 List.iter (fun l ->
280 match getopaque l.pageno with
281 | None -> ()
282 | Some opaque -> clearmark opaque) state.layout;
283 state.roam <- onppundermouse g x y (fun () -> impmsg "whoopsie daisy");
286 let selstring s =
287 match Unix.pipe () with
288 | (exception exn) -> impmsg "pipe failed: %s" @@ exntos exn
289 | (r, w) ->
290 let clo cap fd =
291 Ne.clo fd (fun msg -> impmsg "failed to close %s: %s" cap msg)
293 let pid =
294 try spawn conf.selcmd [r, 0; w, -1]
295 with exn ->
296 impmsg "failed to execute %s: %s" conf.selcmd @@ exntos exn;
299 if pid > 0
300 then (
302 let l = String.length s in
303 let bytes = Bytes.unsafe_of_string s in
304 let n = tempfailureretry (Unix.write w bytes 0) l in
305 if n != l
306 then impmsg "failed to write %d characters to sel pipe, wrote %d"
308 with exn ->
309 impmsg "failed to write to sel pipe: %s" @@ exntos exn
311 else dolog "%s" s;
312 clo "selstring pipe/r" r;
313 clo "selstring pipe/w" w;
316 let undertext ?(nopath=false) = function
317 | Unone -> "none"
318 | Ulinkuri s -> s
319 | Ulinkgoto (pageno, _) ->
320 if nopath
321 then "page " ^ string_of_int (pageno+1)
322 else Printf.sprintf "%s: page %d" state.path (pageno+1)
323 | Utext s -> "font: " ^ s
324 | Uunexpected s -> "unexpected: " ^ s
325 | Ulaunch s -> "launch: " ^ s
326 | Unamed s -> "named: " ^ s
327 | Uremote (filename, pageno) ->
328 Printf.sprintf "%s: page %d" filename (pageno+1)
329 | Uremotedest (filename, destname) ->
330 Printf.sprintf "%s: destination %S" filename destname
331 | Uannotation (opaque, slinkindex) ->
332 "annotation: " ^ getannotcontents opaque slinkindex
335 let updateunder x y =
336 match getunder x y with
337 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
338 | Ulinkuri uri ->
339 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
340 Wsi.setcursor Wsi.CURSOR_INFO
341 | Ulinkgoto (pageno, _) ->
342 if conf.underinfo
343 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
344 Wsi.setcursor Wsi.CURSOR_INFO
345 | Utext s ->
346 if conf.underinfo then showtext 'f' ("ont: " ^ s);
347 Wsi.setcursor Wsi.CURSOR_TEXT
348 | Uunexpected s ->
349 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
350 Wsi.setcursor Wsi.CURSOR_INHERIT
351 | Ulaunch s ->
352 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
353 Wsi.setcursor Wsi.CURSOR_INHERIT
354 | Unamed s ->
355 if conf.underinfo then showtext 'n' ("amed: " ^ s);
356 Wsi.setcursor Wsi.CURSOR_INHERIT
357 | Uremote (filename, pageno) ->
358 if conf.underinfo then showtext 'r'
359 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
360 Wsi.setcursor Wsi.CURSOR_INFO
361 | Uremotedest (filename, destname) ->
362 if conf.underinfo then showtext 'r'
363 (Printf.sprintf "emote destination: %s (%S)" filename destname);
364 Wsi.setcursor Wsi.CURSOR_INFO
365 | Uannotation _ ->
366 if conf.underinfo then showtext 'a' "nnotation";
367 Wsi.setcursor Wsi.CURSOR_INFO
370 let showlinktype under =
371 if conf.underinfo && under != Unone
372 then showtext ' ' @@ undertext under
375 let intentry_with_suffix text key =
376 let text =
377 if key >= 32 && key < 127
378 then
379 let c = Char.chr key in
380 match c with
381 | '0' .. '9' ->
382 addchar text c
384 | 'k' | 'm' | 'g' | 'K' | 'M' | 'G' ->
385 addchar text @@ asciilower c
386 | _ ->
387 state.text <- Printf.sprintf "invalid key (%d, `%c')" key c;
388 text
389 else (
390 state.text <- Printf.sprintf "invalid key %d" key;
391 text
394 TEcont text
397 let wcmd fmt =
398 let b = Buffer.create 16 in
399 Buffer.add_string b "llll";
400 Printf.kbprintf
401 (fun b ->
402 let b = Buffer.to_bytes b in
403 wcmd state.ss b @@ Bytes.length b
404 ) b fmt
407 let nogeomcmds cmds =
408 match cmds with
409 | s, [] -> emptystr s
410 | _ -> false
413 let layoutN ((columns, coverA, coverB), b) x y sw sh =
414 let sh = sh - (hscrollh ()) in
415 let wadj = wadjsb () in
416 let rec fold accu n =
417 if n = Array.length b
418 then accu
419 else
420 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
421 if (vy - y) > sh &&
422 (n = coverA - 1
423 || n = state.pagecount - coverB
424 || (n - coverA) mod columns = columns - 1)
425 then accu
426 else
427 let accu =
428 if vy + h > y
429 then
430 let pagey = max 0 (y - vy) in
431 let pagedispy = if pagey > 0 then 0 else vy - y in
432 let pagedispx, pagex =
433 let pdx =
434 if n = coverA - 1 || n = state.pagecount - coverB
435 then x + (wadj + sw - w) / 2
436 else dx + xoff + x
438 if pdx < 0
439 then 0, -pdx
440 else pdx, 0
442 let pagevw =
443 let vw = wadj + sw - pagedispx in
444 let pw = w - pagex in
445 min vw pw
447 let pagevh = min (h - pagey) (sh - pagedispy) in
448 if pagevw > 0 && pagevh > 0
449 then
450 let e =
451 { pageno = n
452 ; pagedimno = pdimno
453 ; pagew = w
454 ; pageh = h
455 ; pagex = pagex
456 ; pagey = pagey
457 ; pagevw = pagevw
458 ; pagevh = pagevh
459 ; pagedispx = pagedispx
460 ; pagedispy = pagedispy
461 ; pagecol = 0
464 e :: accu
465 else
466 accu
467 else
468 accu
470 fold accu (n+1)
472 if Array.length b = 0
473 then []
474 else List.rev (fold [] (page_of_y y))
477 let layoutS (columns, b) x y sw sh =
478 let sh = sh - hscrollh () in
479 let wadj = wadjsb () in
480 let rec fold accu n =
481 if n = Array.length b
482 then accu
483 else
484 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
485 if (vy - y) > sh
486 then accu
487 else
488 let accu =
489 if vy + pageh > y
490 then
491 let x = xoff + x in
492 let pagey = max 0 (y - vy) in
493 let pagedispy = if pagey > 0 then 0 else vy - y in
494 let pagedispx, pagex =
495 if px = 0
496 then (
497 if x < 0
498 then 0, -x
499 else x, 0
501 else (
502 let px = px - x in
503 if px < 0
504 then -px, 0
505 else 0, px
508 let pagecolw = pagew/columns in
509 let pagedispx =
510 if pagecolw < sw
511 then pagedispx + ((wadj + sw - pagecolw) / 2)
512 else pagedispx
514 let pagevw =
515 let vw = wadj + sw - pagedispx in
516 let pw = pagew - pagex in
517 min vw pw
519 let pagevw = min pagevw pagecolw in
520 let pagevh = min (pageh - pagey) (sh - pagedispy) in
521 if pagevw > 0 && pagevh > 0
522 then
523 let e =
524 { pageno = n/columns
525 ; pagedimno = pdimno
526 ; pagew = pagew
527 ; pageh = pageh
528 ; pagex = pagex
529 ; pagey = pagey
530 ; pagevw = pagevw
531 ; pagevh = pagevh
532 ; pagedispx = pagedispx
533 ; pagedispy = pagedispy
534 ; pagecol = n mod columns
537 e :: accu
538 else
539 accu
540 else
541 accu
543 fold accu (n+1)
545 List.rev (fold [] 0)
548 let layout x y sw sh =
549 if nogeomcmds state.geomcmds
550 then
551 match conf.columns with
552 | Csingle b -> layoutN ((1, 0, 0), b) x y sw sh
553 | Cmulti c -> layoutN c x y sw sh
554 | Csplit s -> layoutS s x y sw sh
555 else []
558 let clamp incr =
559 let y = state.y + incr in
560 let y = max 0 y in
561 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
565 let itertiles l f =
566 let tilex = l.pagex mod conf.tilew in
567 let tiley = l.pagey mod conf.tileh in
569 let col = l.pagex / conf.tilew in
570 let row = l.pagey / conf.tileh in
572 let xadj = xadjsb () in
573 let rec rowloop row y0 dispy h =
574 if h = 0
575 then ()
576 else (
577 let dh = conf.tileh - y0 in
578 let dh = min h dh in
579 let rec colloop col x0 dispx w =
580 if w = 0
581 then ()
582 else (
583 let dw = conf.tilew - x0 in
584 let dw = min w dw in
585 let dispx' = xadj + dispx in
586 f col row dispx' dispy x0 y0 dw dh;
587 colloop (col+1) 0 (dispx+dw) (w-dw)
590 colloop col tilex l.pagedispx l.pagevw;
591 rowloop (row+1) 0 (dispy+dh) (h-dh)
594 if l.pagevw > 0 && l.pagevh > 0
595 then rowloop row tiley l.pagedispy l.pagevh;
598 let gettileopaque l col row =
599 let key =
600 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
602 try Some (Hashtbl.find state.tilemap key)
603 with Not_found -> None
606 let puttileopaque l col row gen colorspace angle opaque size elapsed =
607 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
608 Hashtbl.add state.tilemap key (opaque, size, elapsed)
611 let filledrect2 x0 y0 x1 y1 x2 y2 x3 y3 =
612 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x1; y1; x2; y2; x3; y3 |];
613 GlArray.vertex `two state.vraw;
614 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
617 let filledrect1 x0 y0 x1 y1 = filledrect2 x0 y0 x0 y1 x1 y0 x1 y1;;
619 let filledrect x0 y0 x1 y1 =
620 GlArray.disable `texture_coord;
621 filledrect1 x0 y0 x1 y1;
622 GlArray.enable `texture_coord;
625 let linerect x0 y0 x1 y1 =
626 GlArray.disable `texture_coord;
627 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y1; x1; y0 |];
628 GlArray.vertex `two state.vraw;
629 GlArray.draw_arrays `line_loop ~first:0 ~count:4;
630 GlArray.enable `texture_coord;
633 let drawtiles l color =
634 GlDraw.color color;
635 let wadj = wadjsb () in
636 begintiles ();
637 let f col row x y tilex tiley w h =
638 match gettileopaque l col row with
639 | Some (opaque, _, t) ->
640 let params = x, y, w, h, tilex, tiley in
641 if conf.invert
642 then GlTex.env (`mode `blend);
643 drawtile params opaque;
644 if conf.invert
645 then GlTex.env (`mode `modulate);
646 if conf.debug
647 then (
648 endtiles ();
649 let s = Printf.sprintf
650 "%d[%d,%d] %f sec"
651 l.pageno col row t
653 let w = measurestr fstate.fontsize s in
654 GlDraw.color (0.0, 0.0, 0.0);
655 filledrect (float (x-2))
656 (float (y-2))
657 (float (x+2) +. w)
658 (float (y + fstate.fontsize + 2));
659 GlDraw.color color;
660 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
661 begintiles ();
664 | None ->
665 endtiles ();
666 let w =
667 if conf.leftscroll
668 then w
669 else
670 let lw = wadj + state.winw - x in
671 min lw w
672 and h =
673 let lh = state.winh - y in
674 min lh h
676 if conf.invert
677 then GlTex.env (`mode `blend);
678 begin match state.checkerstexid with
679 | Some id ->
680 Gl.enable `texture_2d;
681 GlTex.bind_texture ~target:`texture_2d id;
682 let x0 = float x
683 and y0 = float y
684 and x1 = float (x+w)
685 and y1 = float (y+h) in
687 let tw = float w /. 16.0
688 and th = float h /. 16.0 in
689 let tx0 = float tilex /. 16.0
690 and ty0 = float tiley /. 16.0 in
691 let tx1 = tx0 +. tw
692 and ty1 = ty0 +. th in
693 Raw.sets_float state.vraw ~pos:0
694 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
695 Raw.sets_float state.traw ~pos:0
696 [| tx0; ty0; tx0; ty1; tx1; ty0; tx1; ty1 |];
697 GlArray.vertex `two state.vraw;
698 GlArray.tex_coord `two state.traw;
699 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
700 Gl.disable `texture_2d;
702 | None ->
703 GlDraw.color (1.0, 1.0, 1.0);
704 filledrect (float x) (float y) (float (x+w)) (float (y+h));
705 end;
706 if conf.invert
707 then GlTex.env (`mode `modulate);
708 if w > 128 && h > fstate.fontsize + 10
709 then (
710 let c = if conf.invert then 1.0 else 0.0 in
711 GlDraw.color (c, c, c);
712 let c, r =
713 if conf.verbose
714 then (col*conf.tilew, row*conf.tileh)
715 else col, row
717 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
719 GlDraw.color color;
720 begintiles ();
722 itertiles l f;
723 endtiles ();
726 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
728 let tilevisible1 l x y =
729 let ax0 = l.pagex
730 and ax1 = l.pagex + l.pagevw
731 and ay0 = l.pagey
732 and ay1 = l.pagey + l.pagevh in
734 let bx0 = x
735 and by0 = y in
736 let bx1 = min (bx0 + conf.tilew) l.pagew
737 and by1 = min (by0 + conf.tileh) l.pageh in
739 let rx0 = max ax0 bx0
740 and ry0 = max ay0 by0
741 and rx1 = min ax1 bx1
742 and ry1 = min ay1 by1 in
744 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
745 nonemptyintersection
748 let tilevisible layout n x y =
749 let rec findpageinlayout m = function
750 | l :: rest when l.pageno = n ->
751 tilevisible1 l x y || (
752 match conf.columns with
753 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
754 | Csplit _
755 | Csingle _
756 | Cmulti _ -> false
758 | _ :: rest -> findpageinlayout 0 rest
759 | [] -> false
761 findpageinlayout 0 layout;
764 let tileready l x y =
765 tilevisible1 l x y &&
766 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
769 let tilepage n p layout =
770 let rec loop = function
771 | l :: rest ->
772 if l.pageno = n
773 then
774 let f col row _ _ _ _ _ _ =
775 if state.currently = Idle
776 then
777 match gettileopaque l col row with
778 | Some _ -> ()
779 | None ->
780 let x = col*conf.tilew
781 and y = row*conf.tileh in
782 let w =
783 let w = l.pagew - x in
784 min w conf.tilew
786 let h =
787 let h = l.pageh - y in
788 min h conf.tileh
790 let pbo =
791 if conf.usepbo
792 then getpbo w h conf.colorspace
793 else ~< "0"
795 wcmd "tile %s %d %d %d %d %s"
796 (~> p) x y w h (~> pbo);
797 state.currently <-
798 Tiling (
799 l, p, conf.colorspace, conf.angle,
800 state.gen, col, row, conf.tilew, conf.tileh
803 itertiles l f;
804 else
805 loop rest
807 | [] -> ()
809 if nogeomcmds state.geomcmds
810 then loop layout;
813 let preloadlayout x y sw sh =
814 let y = if y < sh then 0 else y - sh in
815 let x = min 0 (x + sw) in
816 let h = sh*3 in
817 let w = sw*3 in
818 layout x y w h;
821 let load pages =
822 let rec loop pages =
823 if state.currently != Idle
824 then ()
825 else
826 match pages with
827 | l :: rest ->
828 begin match getopaque l.pageno with
829 | None ->
830 wcmd "page %d %d" l.pageno l.pagedimno;
831 state.currently <- Loading (l, state.gen);
832 | Some opaque ->
833 tilepage l.pageno opaque pages;
834 loop rest
835 end;
836 | _ -> ()
838 if nogeomcmds state.geomcmds
839 then loop pages
842 let preload pages =
843 load pages;
844 if conf.preload && state.currently = Idle
845 then load (preloadlayout state.x state.y state.winw state.winh);
848 let layoutready layout =
849 let rec fold all ls =
850 all && match ls with
851 | l :: rest ->
852 let seen = ref false in
853 let allvisible = ref true in
854 let foo col row _ _ _ _ _ _ =
855 seen := true;
856 allvisible := !allvisible &&
857 begin match gettileopaque l col row with
858 | Some _ -> true
859 | None -> false
862 itertiles l foo;
863 fold (!seen && !allvisible) rest
864 | [] -> true
866 let alltilesvisible = fold true layout in
867 alltilesvisible;
870 let gotoxy x y =
871 let y = bound y 0 state.maxy in
872 let y, layout, proceed =
873 match conf.maxwait with
874 | Some time when state.ghyll == noghyll ->
875 begin match state.throttle with
876 | None ->
877 let layout = layout x y state.winw state.winh in
878 let ready = layoutready layout in
879 if not ready
880 then (
881 load layout;
882 state.throttle <- Some (layout, y, now ());
884 else G.postRedisplay "gotoxy showall (None)";
885 y, layout, ready
886 | Some (_, _, started) ->
887 let dt = now () -. started in
888 if dt > time
889 then (
890 state.throttle <- None;
891 let layout = layout x y state.winw state.winh in
892 load layout;
893 G.postRedisplay "maxwait";
894 y, layout, true
896 else -1, [], false
899 | _ ->
900 let layout = layout x y state.winw state.winh in
901 if not !wtmode || layoutready layout
902 then G.postRedisplay "gotoxy ready";
903 y, layout, true
905 if proceed
906 then (
907 state.x <- x;
908 state.y <- y;
909 state.layout <- layout;
910 begin match state.mode with
911 | LinkNav ln ->
912 begin match ln with
913 | Ltexact (pageno, linkno) ->
914 let rec loop = function
915 | [] ->
916 state.mode <- LinkNav (Ltgendir 0)
917 | l :: _ when l.pageno = pageno ->
918 begin match getopaque pageno with
919 | None -> state.mode <- LinkNav (Ltnotready (pageno, 0))
920 | Some opaque ->
921 let x0, y0, x1, y1 = getlinkrect opaque linkno in
922 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
923 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
924 then state.mode <- LinkNav (Ltgendir 0)
926 | _ :: rest -> loop rest
928 loop layout
929 | Ltnotready _ | Ltgendir _ -> ()
931 | Birdseye _
932 | Textentry _
933 | View -> ()
934 end;
935 begin match state.mode with
936 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
937 if not (pagevisible layout pageno)
938 then (
939 match state.layout with
940 | [] -> ()
941 | l :: _ ->
942 state.mode <- Birdseye (
943 conf, leftx, l.pageno, hooverpageno, anchor
946 | LinkNav lt ->
947 begin match lt with
948 | Ltnotready (_, dir)
949 | Ltgendir dir ->
950 let linknav =
951 let rec loop = function
952 | [] -> lt
953 | l :: rest ->
954 match getopaque l.pageno with
955 | None -> Ltnotready (l.pageno, dir)
956 | Some opaque ->
957 let link =
958 let ld =
959 if dir = 0
960 then LDfirstvisible (l.pagex, l.pagey, dir)
961 else (
962 if dir > 0 then LDfirst else LDlast
965 findlink opaque ld
967 match link with
968 | Lnotfound -> loop rest
969 | Lfound n ->
970 showlinktype (getlink opaque n);
971 Ltexact (l.pageno, n)
973 loop state.layout
975 state.mode <- LinkNav linknav
976 | Ltexact _ -> ()
978 | Textentry _
979 | View -> ()
980 end;
981 preload layout;
983 state.ghyll <- noghyll;
984 if conf.updatecurs
985 then (
986 let mx, my = state.mpos in
987 updateunder mx my;
991 let conttiling pageno opaque =
992 tilepage pageno opaque
993 (if conf.preload
994 then preloadlayout state.x state.y state.winw state.winh
995 else state.layout)
998 let gotoxy_and_clear_text x y =
999 if not conf.verbose then state.text <- E.s;
1000 gotoxy x y;
1003 let getanchory (n, top, dtop) =
1004 let y, h = getpageyh n in
1005 if conf.presentation
1006 then
1007 let ips = calcips h in
1008 y + truncate (top*.float h -. dtop*.float ips) + ips;
1009 else
1010 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1013 let gotoanchor anchor =
1014 gotoxy state.x (getanchory anchor);
1017 let addnav () =
1018 cbput state.hists.nav (getanchor ());
1021 let getnav dir =
1022 let anchor = cbgetc state.hists.nav dir in
1023 getanchory anchor;
1026 let gotoghyll1 single y =
1027 let scroll f n a b =
1028 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1029 let snake f a b =
1030 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1031 if f < a
1032 then s (float f /. float a)
1033 else (
1034 if f > b
1035 then 1.0 -. s ((float (f-b) /. float (n-b)))
1036 else 1.0
1039 snake f a b
1040 and summa n a b =
1041 let ins = float a *. 0.5
1042 and outs = float (n-b) *. 0.5 in
1043 let ones = b - a in
1044 ins +. outs +. float ones
1046 let rec set nab y sy =
1047 let (_N, _A, _B), y =
1048 if single
1049 then
1050 let scl = if y > sy then 2 else -2 in
1051 let _N, _, _ = nab in
1052 (_N,0,_N), y+conf.scrollstep*scl
1053 else nab,y in
1054 let sum = summa _N _A _B in
1055 let dy = float (y - sy) in
1056 state.ghyll <- (
1057 let rec gf n y1 o =
1058 if n >= _N
1059 then state.ghyll <- noghyll
1060 else
1061 let go n =
1062 let s = scroll n _N _A _B in
1063 let y1 = y1 +. ((s *. dy) /. sum) in
1064 gotoxy_and_clear_text state.x (truncate y1);
1065 state.ghyll <- gf (n+1) y1;
1067 match o with
1068 | None -> go n
1069 | Some y' when single -> set nab y' state.y
1070 | Some y' -> set (_N/2, 1, 1) y' state.y
1072 gf 0 (float state.y)
1075 match conf.ghyllscroll with
1076 | Some nab when not conf.presentation ->
1077 if state.ghyll == noghyll
1078 then set nab y state.y
1079 else state.ghyll (Some y)
1080 | _ ->
1081 gotoxy_and_clear_text state.x y
1084 let gotoghyll = gotoghyll1 false;;
1086 let gotopage n top =
1087 let y, h = getpageyh n in
1088 let y = y + (truncate (top *. float h)) in
1089 gotoghyll y
1092 let gotopage1 n top =
1093 let y = getpagey n in
1094 let y = y + top in
1095 gotoghyll y
1098 let invalidate s f =
1099 state.redisplay <- false;
1100 state.layout <- [];
1101 state.pdims <- [];
1102 state.rects <- [];
1103 state.rects1 <- [];
1104 match state.geomcmds with
1105 | ps, [] when emptystr ps ->
1106 f ();
1107 state.geomcmds <- s, [];
1109 | ps, [] ->
1110 state.geomcmds <- ps, [s, f];
1112 | ps, (s', _) :: rest when s' = s ->
1113 state.geomcmds <- ps, ((s, f) :: rest);
1115 | ps, cmds ->
1116 state.geomcmds <- ps, ((s, f) :: cmds);
1119 let flushpages () =
1120 Hashtbl.iter (fun _ opaque ->
1121 wcmd "freepage %s" (~> opaque);
1122 ) state.pagemap;
1123 Hashtbl.clear state.pagemap;
1126 let flushtiles () =
1127 if not (Queue.is_empty state.tilelru)
1128 then (
1129 Queue.iter (fun (k, p, s) ->
1130 wcmd "freetile %s" (~> p);
1131 state.memused <- state.memused - s;
1132 Hashtbl.remove state.tilemap k;
1133 ) state.tilelru;
1134 state.uioh#infochanged Memused;
1135 Queue.clear state.tilelru;
1137 load state.layout;
1140 let stateh h =
1141 let h = truncate (float (h-hscrollh ())*.conf.zoom) in
1142 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
1143 h - d
1146 let opendoc path password =
1147 state.path <- path;
1148 state.password <- password;
1149 state.gen <- state.gen + 1;
1150 state.docinfo <- [];
1151 state.outlines <- [||];
1153 flushpages ();
1154 setaalevel conf.aalevel;
1155 let titlepath =
1156 if emptystr state.origin
1157 then path
1158 else state.origin
1160 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
1161 wcmd "open %d %d %s\000%s\000" (btod !wtmode) (btod !cxack) path password;
1162 invalidate "reqlayout"
1163 (fun () ->
1164 wcmd "reqlayout %d %d %d %s\000"
1165 conf.angle (FMTE.to_int conf.fitmodel)
1166 (stateh state.winh) state.nameddest
1170 let reload () =
1171 state.anchor <- getanchor ();
1172 opendoc state.path state.password;
1175 let scalecolor c =
1176 let c = c *. conf.colorscale in
1177 (c, c, c);
1180 let scalecolor2 (r, g, b) =
1181 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1184 let docolumns columns =
1185 let wadj = wadjsb () in
1186 match columns with
1187 | Csingle _ ->
1188 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1189 let wadj = wadjsb () in
1190 let rec loop pageno pdimno pdim y ph pdims =
1191 if pageno = state.pagecount
1192 then ()
1193 else
1194 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1195 match pdims with
1196 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1197 pdimno+1, pdim, rest
1198 | _ ->
1199 pdimno, pdim, pdims
1201 let x = max 0 (((wadj + state.winw - w) / 2) - xoff) in
1202 let y = y +
1203 (if conf.presentation
1204 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1205 else (if pageno = 0 then 0 else conf.interpagespace)
1208 a.(pageno) <- (pdimno, x, y, pdim);
1209 loop (pageno+1) pdimno pdim (y + h) h pdims
1211 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1212 conf.columns <- Csingle a;
1214 | Cmulti ((columns, coverA, coverB), _) ->
1215 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1216 let rec loop pageno pdimno pdim x y rowh pdims =
1217 let rec fixrow m = if m = pageno then () else
1218 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1219 if h < rowh
1220 then (
1221 let y = y + (rowh - h) / 2 in
1222 a.(m) <- (pdimno, x, y, pdim);
1224 fixrow (m+1)
1226 if pageno = state.pagecount
1227 then fixrow (((pageno - 1) / columns) * columns)
1228 else
1229 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1230 match pdims with
1231 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1232 pdimno+1, pdim, rest
1233 | _ ->
1234 pdimno, pdim, pdims
1236 let x, y, rowh' =
1237 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1238 then (
1239 let x = (wadj + state.winw - w) / 2 in
1240 let ips =
1241 if conf.presentation then calcips h else conf.interpagespace in
1242 x, y + ips + rowh, h
1244 else (
1245 if (pageno - coverA) mod columns = 0
1246 then (
1247 let x = max 0 (wadj + state.winw - state.w) / 2 in
1248 let y =
1249 if conf.presentation
1250 then
1251 let ips = calcips h in
1252 y + (if pageno = 0 then 0 else calcips rowh + ips)
1253 else
1254 y + (if pageno = 0 then 0 else conf.interpagespace)
1256 x, y + rowh, h
1258 else x, y, max rowh h
1261 let y =
1262 if pageno > 1 && (pageno - coverA) mod columns = 0
1263 then (
1264 let y =
1265 if pageno = columns && conf.presentation
1266 then (
1267 let ips = calcips rowh in
1268 for i = 0 to pred columns
1270 let (pdimno, x, y, pdim) = a.(i) in
1271 a.(i) <- (pdimno, x, y+ips, pdim)
1272 done;
1273 y+ips;
1275 else y
1277 fixrow (pageno - columns);
1280 else y
1282 a.(pageno) <- (pdimno, x, y, pdim);
1283 let x = x + w + xoff*2 + conf.interpagespace in
1284 loop (pageno+1) pdimno pdim x y rowh' pdims
1286 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1287 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1289 | Csplit (c, _) ->
1290 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1291 let rec loop pageno pdimno pdim y pdims =
1292 if pageno = state.pagecount
1293 then ()
1294 else
1295 let pdimno, ((_, w, h, _) as pdim), pdims =
1296 match pdims with
1297 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1298 pdimno+1, pdim, rest
1299 | _ ->
1300 pdimno, pdim, pdims
1302 let cw = w / c in
1303 let rec loop1 n x y =
1304 if n = c then y else (
1305 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1306 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1309 let y = loop1 0 0 y in
1310 loop (pageno+1) pdimno pdim y pdims
1312 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1313 conf.columns <- Csplit (c, a);
1316 let represent () =
1317 docolumns conf.columns;
1318 state.maxy <- calcheight ();
1319 if state.reprf == noreprf
1320 then (
1321 match state.mode with
1322 | Birdseye (_, _, pageno, _, _) ->
1323 let y, h = getpageyh pageno in
1324 let top = (state.winh - h) / 2 in
1325 gotoxy state.x (max 0 (y - top))
1326 | Textentry _
1327 | View
1328 | LinkNav _ ->
1329 let y = getanchory state.anchor in
1330 let y = min y (state.maxy - (state.winh - hscrollh ())) in
1331 gotoxy state.x y;
1333 else (
1334 state.reprf ();
1335 state.reprf <- noreprf;
1339 let reshape ?(firsttime=false) w h =
1340 GlDraw.viewport ~x:0 ~y:0 ~w:w ~h:h;
1341 if not firsttime && nogeomcmds state.geomcmds
1342 then state.anchor <- getanchor ();
1344 state.winw <- w;
1345 let w = truncate (float (w - vscrollw ()) *. conf.zoom) in
1346 let w = max w 2 in
1347 state.winh <- h;
1348 setfontsize fstate.fontsize;
1349 GlMat.mode `modelview;
1350 GlMat.load_identity ();
1352 GlMat.mode `projection;
1353 GlMat.load_identity ();
1354 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1355 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1356 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
1358 let relx =
1359 if conf.zoom <= 1.0
1360 then 0.0
1361 else float state.x /. float state.w
1363 invalidate "geometry"
1364 (fun () ->
1365 state.w <- w;
1366 if not firsttime
1367 then state.x <- truncate (relx *. float w);
1368 let w =
1369 match conf.columns with
1370 | Csingle _ -> w
1371 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1372 | Csplit (c, _) -> w * c
1374 wcmd "geometry %d %d %d"
1375 w (stateh h) (FMTE.to_int conf.fitmodel)
1379 let enttext () =
1380 let len = String.length state.text in
1381 let x0 = xadjsb () in
1382 let drawstring s =
1383 let hscrollh =
1384 match state.mode with
1385 | Textentry _ | View | LinkNav _ ->
1386 let h, _, _ = state.uioh#scrollpw in
1388 | Birdseye _ -> 0
1390 let rect x w =
1391 filledrect x (float (state.winh - (fstate.fontsize + 4) - hscrollh))
1392 (x+.w) (float (state.winh - hscrollh))
1395 let w = float (wadjsb () + state.winw - 1) in
1396 if state.progress >= 0.0 && state.progress < 1.0
1397 then (
1398 GlDraw.color (0.3, 0.3, 0.3);
1399 let w1 = w *. state.progress in
1400 rect (float x0) w1;
1401 GlDraw.color (0.0, 0.0, 0.0);
1402 rect (float x0+.w1) (float x0+.w-.w1)
1404 else (
1405 GlDraw.color (0.0, 0.0, 0.0);
1406 rect (float x0) w;
1409 GlDraw.color (1.0, 1.0, 1.0);
1410 drawstring fstate.fontsize
1411 (if conf.leftscroll then x0 + 2 else x0 + if len > 0 then 8 else 2)
1412 (state.winh - hscrollh - 5) s;
1414 let s =
1415 match state.mode with
1416 | Textentry ((prefix, text, _, _, _, _), _) ->
1417 let s =
1418 if len > 0
1419 then
1420 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1421 else
1422 Printf.sprintf "%s%s_" prefix text
1426 | Birdseye _
1427 | View
1428 | LinkNav _ -> state.text
1430 let s =
1431 if state.newerrmsgs
1432 then (
1433 if not (istextentry state.mode) && state.uioh#eformsgs
1434 then
1435 let s1 = "(press 'e' to review error messasges)" in
1436 if nonemptystr s then s ^ " " ^ s1 else s1
1437 else s
1439 else s
1441 if nonemptystr s
1442 then drawstring s
1445 let gctiles () =
1446 let len = Queue.length state.tilelru in
1447 let layout = lazy (
1448 match state.throttle with
1449 | None ->
1450 if conf.preload
1451 then preloadlayout state.x state.y state.winw state.winh
1452 else state.layout
1453 | Some (layout, _, _) ->
1454 layout
1455 ) in
1456 let rec loop qpos =
1457 if state.memused <= conf.memlimit
1458 then ()
1459 else (
1460 if qpos < len
1461 then
1462 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1463 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1464 let (_, pw, ph, _) = getpagedim n in
1466 gen = state.gen
1467 && colorspace = conf.colorspace
1468 && angle = conf.angle
1469 && pagew = pw
1470 && pageh = ph
1471 && (
1472 let x = col*conf.tilew
1473 and y = row*conf.tileh in
1474 tilevisible (Lazy.force_val layout) n x y
1476 then Queue.push lruitem state.tilelru
1477 else (
1478 freepbo p;
1479 wcmd "freetile %s" (~> p);
1480 state.memused <- state.memused - s;
1481 state.uioh#infochanged Memused;
1482 Hashtbl.remove state.tilemap k;
1484 loop (qpos+1)
1487 loop 0
1490 let onpagerect pageno f =
1491 let b =
1492 match conf.columns with
1493 | Cmulti (_, b) -> b
1494 | Csingle b -> b
1495 | Csplit (_, b) -> b
1497 if pageno >= 0 && pageno < Array.length b
1498 then
1499 let (_, _, _, (_, w, h, _)) = b.(pageno) in
1500 f w h
1503 let gotopagexy1 wtmode pageno x y =
1504 let _,w1,h1,leftx = getpagedim pageno in
1505 let top = y /. (float h1) in
1506 let left = x /. (float w1) in
1507 let py, w, h = getpageywh pageno in
1508 let wh = state.winh - hscrollh () in
1509 let x = left *. (float w) in
1510 let x = leftx + state.x + truncate x in
1511 let wadj = wadjsb () in
1512 let sx =
1513 if x < 0 || x >= wadj + state.winw
1514 then state.x - x
1515 else state.x
1517 let pdy = truncate (top *. float h) in
1518 let y' = py + pdy in
1519 let dy = y' - state.y in
1520 let sy =
1521 if x != state.x || not (dy > 0 && dy < wh)
1522 then (
1523 if conf.presentation
1524 then
1525 if abs (py - y') > wh
1526 then y'
1527 else py
1528 else y';
1530 else state.y
1532 if state.x != sx || state.y != sy
1533 then (
1534 let x, y =
1535 if wtmode
1536 then (
1537 let ww = wadj + state.winw in
1538 let qx = sx / ww
1539 and qy = pdy / wh in
1540 let x = qx * ww
1541 and y = py + qy * wh in
1542 let x = if -x + ww > w1 then -(w1-ww) else x
1543 and y' = if y + wh > state.maxy then state.maxy - wh else y in
1544 let y =
1545 if conf.presentation
1546 then
1547 if abs (py - y') > wh
1548 then y'
1549 else py
1550 else y';
1552 (x, y)
1554 else (sx, sy)
1556 gotoxy_and_clear_text x y;
1558 else gotoxy_and_clear_text state.x state.y;
1561 let gotopagexy wtmode pageno x y =
1562 match state.mode with
1563 | Birdseye _ -> gotopage pageno 0.0
1564 | Textentry _
1565 | View
1566 | LinkNav _ -> gotopagexy1 wtmode pageno x y
1569 let getpassword () =
1570 let passcmd = getenvwithdef "LLPP_ASKPASS" conf.passcmd in
1571 if emptystr passcmd
1572 then E.s
1573 else getcmdoutput
1574 (fun s ->
1575 impmsg "error getting password: %s" s;
1576 dolog "%s" s) passcmd;
1579 let pgoto opaque pageno x y =
1580 let pdimno = getpdimno pageno in
1581 let x, y = project opaque pageno pdimno x y in
1582 gotopagexy false pageno x y;
1585 let act cmds =
1586 (* dolog "%S" cmds; *)
1587 let spl = splitatspace cmds in
1588 let scan s fmt f =
1589 try Scanf.sscanf s fmt f
1590 with exn ->
1591 dolog "error processing '%S': %s" cmds @@ exntos exn;
1592 exit 1
1594 let addoutline outline =
1595 match state.currently with
1596 | Outlining outlines ->
1597 state.currently <- Outlining (outline :: outlines)
1598 | Idle -> state.currently <- Outlining [outline]
1599 | Loading _
1600 | Tiling _ ->
1601 dolog "invalid outlining state";
1602 logcurrently state.currently
1604 match spl with
1605 | "clear", "" ->
1606 state.uioh#infochanged Pdim;
1607 state.pdims <- [];
1609 | "clearrects", "" ->
1610 state.rects <- state.rects1;
1611 G.postRedisplay "clearrects";
1613 | "continue", args ->
1614 let n = scan args "%u" (fun n -> n) in
1615 state.pagecount <- n;
1616 begin match state.currently with
1617 | Outlining l ->
1618 state.currently <- Idle;
1619 state.outlines <- Array.of_list (List.rev l)
1620 | Idle
1621 | Loading _
1622 | Tiling _ -> ()
1623 end;
1625 let cur, cmds = state.geomcmds in
1626 if emptystr cur
1627 then failwith "umpossible";
1629 begin match List.rev cmds with
1630 | [] ->
1631 state.geomcmds <- E.s, [];
1632 state.throttle <- None;
1633 represent ();
1634 | (s, f) :: rest ->
1635 f ();
1636 state.geomcmds <- s, List.rev rest;
1637 end;
1638 if conf.maxwait = None && not !wtmode
1639 then G.postRedisplay "continue";
1641 | "msg", args ->
1642 showtext ' ' args
1644 | "vmsg", args ->
1645 if conf.verbose
1646 then showtext ' ' args
1648 | "emsg", args ->
1649 Buffer.add_string state.errmsgs args;
1650 state.newerrmsgs <- true;
1651 G.postRedisplay "error message"
1653 | "progress", args ->
1654 let progress, text =
1655 scan args "%f %n"
1656 (fun f pos ->
1657 f, String.sub args pos (String.length args - pos))
1659 state.text <- text;
1660 state.progress <- progress;
1661 G.postRedisplay "progress"
1663 | "firstmatch", args ->
1664 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1665 scan args "%u %d %f %f %f %f %f %f %f %f"
1666 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1667 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1669 let xoff = float (xadjsb ()) in
1670 let x0 = x0 +. xoff
1671 and x1 = x1 +. xoff
1672 and x2 = x2 +. xoff
1673 and x3 = x3 +. xoff in
1674 let y = (getpagey pageno) + truncate y0 in
1675 let x =
1676 if conf.zoom > 1.0
1677 then truncate (xoff -. x0) + state.winw/2
1678 else state.x
1680 addnav ();
1681 gotoxy x y;
1682 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1683 state.rects1 <- [pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)]
1685 | "match", args ->
1686 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1687 scan args "%u %d %f %f %f %f %f %f %f %f"
1688 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1689 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1691 let xoff = float (xadjsb ()) in
1692 let x0 = x0 +. xoff
1693 and x1 = x1 +. xoff
1694 and x2 = x2 +. xoff
1695 and x3 = x3 +. xoff in
1696 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1697 state.rects1 <-
1698 (pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1700 | "page", args ->
1701 let pageopaques, t = scan args "%s %f" (fun p t -> p, t) in
1702 let pageopaque = ~< pageopaques in
1703 begin match state.currently with
1704 | Loading (l, gen) ->
1705 vlog "page %d took %f sec" l.pageno t;
1706 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1707 begin match state.throttle with
1708 | None ->
1709 let preloadedpages =
1710 if conf.preload
1711 then preloadlayout state.x state.y state.winw state.winh
1712 else state.layout
1714 let evict () =
1715 let set =
1716 List.fold_left (fun s l -> IntSet.add l.pageno s)
1717 IntSet.empty preloadedpages
1719 let evictedpages =
1720 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1721 if not (IntSet.mem pageno set)
1722 then (
1723 wcmd "freepage %s" (~> opaque);
1724 key :: accu
1726 else accu
1727 ) state.pagemap []
1729 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1731 evict ();
1732 state.currently <- Idle;
1733 if gen = state.gen
1734 then (
1735 tilepage l.pageno pageopaque state.layout;
1736 load state.layout;
1737 load preloadedpages;
1738 let visible = pagevisible state.layout l.pageno in
1739 if visible
1740 then (
1741 match state.mode with
1742 | LinkNav (Ltnotready (pageno, dir)) ->
1743 if pageno = l.pageno
1744 then (
1745 let link =
1746 let ld =
1747 if dir = 0
1748 then LDfirstvisible (l.pagex, l.pagey, dir)
1749 else (
1750 if dir > 0 then LDfirst else LDlast
1753 findlink pageopaque ld
1755 match link with
1756 | Lnotfound -> ()
1757 | Lfound n ->
1758 showlinktype (getlink pageopaque n);
1759 state.mode <- LinkNav (Ltexact (l.pageno, n))
1761 | LinkNav (Ltgendir _)
1762 | LinkNav (Ltexact _)
1763 | View
1764 | Birdseye _
1765 | Textentry _ -> ()
1768 if visible && layoutready state.layout
1769 then (
1770 G.postRedisplay "page";
1774 | Some (layout, _, _) ->
1775 state.currently <- Idle;
1776 tilepage l.pageno pageopaque layout;
1777 load state.layout
1778 end;
1780 | Idle
1781 | Tiling _
1782 | Outlining _ ->
1783 dolog "Inconsistent loading state";
1784 logcurrently state.currently;
1785 exit 1
1788 | "tile" , args ->
1789 let (x, y, opaques, size, t) =
1790 scan args "%u %u %s %u %f"
1791 (fun x y p size t -> (x, y, p, size, t))
1793 let opaque = ~< opaques in
1794 begin match state.currently with
1795 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1796 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1798 unmappbo opaque;
1799 if tilew != conf.tilew || tileh != conf.tileh
1800 then (
1801 wcmd "freetile %s" (~> opaque);
1802 state.currently <- Idle;
1803 load state.layout;
1805 else (
1806 puttileopaque l col row gen cs angle opaque size t;
1807 state.memused <- state.memused + size;
1808 state.uioh#infochanged Memused;
1809 gctiles ();
1810 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1811 opaque, size) state.tilelru;
1813 let layout =
1814 match state.throttle with
1815 | None -> state.layout
1816 | Some (layout, _, _) -> layout
1819 state.currently <- Idle;
1820 if gen = state.gen
1821 && conf.colorspace = cs
1822 && conf.angle = angle
1823 && tilevisible layout l.pageno x y
1824 then conttiling l.pageno pageopaque;
1826 begin match state.throttle with
1827 | None ->
1828 preload state.layout;
1829 if gen = state.gen
1830 && conf.colorspace = cs
1831 && conf.angle = angle
1832 && tilevisible state.layout l.pageno x y
1833 && (not !wtmode || layoutready state.layout)
1834 then G.postRedisplay "tile nothrottle";
1836 | Some (layout, y, _) ->
1837 let ready = layoutready layout in
1838 if ready
1839 then (
1840 state.y <- y;
1841 state.layout <- layout;
1842 state.throttle <- None;
1843 G.postRedisplay "throttle";
1845 else load layout;
1846 end;
1849 | Idle
1850 | Loading _
1851 | Outlining _ ->
1852 dolog "Inconsistent tiling state";
1853 logcurrently state.currently;
1854 exit 1
1857 | "pdim", args ->
1858 let (n, w, h, _) as pdim =
1859 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1861 let pdim =
1862 match conf.fitmodel with
1863 | FitWidth -> pdim
1864 | FitPage | FitProportional ->
1865 match conf.columns with
1866 | Csplit _ -> (n, w, h, 0)
1867 | Csingle _ | Cmulti _ -> pdim
1869 state.uioh#infochanged Pdim;
1870 state.pdims <- pdim :: state.pdims
1872 | "o", args ->
1873 let (l, n, t, h, pos) =
1874 scan args "%u %u %d %u %n"
1875 (fun l n t h pos -> l, n, t, h, pos)
1877 let s = String.sub args pos (String.length args - pos) in
1878 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
1880 | "ou", args ->
1881 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
1882 let s = String.sub args pos len in
1883 let pos2 = pos + len + 1 in
1884 let uri = String.sub args pos2 (String.length args - pos2) in
1885 addoutline (s, l, Ouri uri)
1887 | "on", args ->
1888 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
1889 let s = String.sub args pos (String.length args - pos) in
1890 addoutline (s, l, Onone)
1892 | "a", args ->
1893 let (n, l, t) =
1894 scan args "%u %d %d" (fun n l t -> n, l, t)
1896 state.reprf <- (fun () -> gotopagexy !wtmode n (float l) (float t))
1898 | "info", args ->
1899 let pos = nindex args '\t' in
1900 if pos >= 0 && String.sub args 0 pos = "Title"
1901 then (
1902 let s = String.sub args (pos+1) @@ String.length args - pos - 1 in
1903 conf.title <- s;
1904 Wsi.settitle s;
1906 state.docinfo <- (1, args) :: state.docinfo
1908 | "infoend", "" ->
1909 state.uioh#infochanged Docinfo;
1910 state.docinfo <- List.rev state.docinfo
1912 | "pass", args ->
1913 if args = "fail"
1914 then Wsi.settitle "Wrong password";
1915 let password = getpassword () in
1916 if emptystr password
1917 then error "document is password protected"
1918 else opendoc state.path password
1920 | _ ->
1921 error "unknown cmd `%S'" cmds
1924 let onhist cb =
1925 let rc = cb.rc in
1926 let action = function
1927 | HCprev -> cbget cb ~-1
1928 | HCnext -> cbget cb 1
1929 | HCfirst -> cbget cb ~-(cb.rc)
1930 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1931 and cancel () = cb.rc <- rc
1932 in (action, cancel)
1935 let search pattern forward =
1936 match conf.columns with
1937 | Csplit _ -> impmsg "searching does not work properly in split columns mode"
1938 | Csingle _
1939 | Cmulti _ ->
1940 if nonemptystr pattern
1941 then
1942 let pn, py =
1943 match state.layout with
1944 | [] -> 0, 0
1945 | l :: _ ->
1946 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1948 wcmd "search %d %d %d %d,%s\000"
1949 (btod conf.icase) pn py (btod forward) pattern;
1952 let intentry text key =
1953 let text =
1954 if key >= 32 && key < 127
1955 then
1956 let c = Char.chr key in
1957 match c with
1958 | '0' .. '9' -> addchar text c
1959 | _ ->
1960 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1961 text
1962 else (
1963 state.text <- Printf.sprintf "invalid key (%d)" key;
1964 text
1967 TEcont text
1970 let linknact f s =
1971 if nonemptystr s
1972 then (
1973 let n =
1974 let l = String.length s in
1975 let rec loop pos n = if pos = l then n else
1976 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
1977 loop (pos+1) (n*26 + m)
1978 in loop 0 0
1980 let rec loop n = function
1981 | [] -> ()
1982 | l :: rest ->
1983 match getopaque l.pageno with
1984 | None -> loop n rest
1985 | Some opaque ->
1986 let m = getlinkcount opaque in
1987 if n < m
1988 then (
1989 let under = getlink opaque n in
1990 f under
1992 else loop (n-m) rest
1994 loop n state.layout;
1998 let linknentry text key =
1999 if key >= 32 && key < 127
2000 then
2001 let text = addchar text (Char.chr key) in
2002 linknact (fun under -> state.text <- undertext ~nopath:true under) text;
2003 TEcont text
2004 else (
2005 state.text <- Printf.sprintf "invalid key %d" key;
2006 TEcont text
2010 let textentry text key =
2011 if Wsi.isspecialkey key
2012 then TEcont text
2013 else TEcont (text ^ toutf8 key)
2016 let reqlayout angle fitmodel =
2017 match state.throttle with
2018 | None ->
2019 if nogeomcmds state.geomcmds
2020 then state.anchor <- getanchor ();
2021 conf.angle <- angle mod 360;
2022 if conf.angle != 0
2023 then (
2024 match state.mode with
2025 | LinkNav _ -> state.mode <- View
2026 | Birdseye _
2027 | Textentry _
2028 | View -> ()
2030 conf.fitmodel <- fitmodel;
2031 invalidate "reqlayout"
2032 (fun () ->
2033 wcmd "reqlayout %d %d %d"
2034 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
2036 | _ -> ()
2039 let settrim trimmargins trimfuzz =
2040 if nogeomcmds state.geomcmds
2041 then state.anchor <- getanchor ();
2042 conf.trimmargins <- trimmargins;
2043 conf.trimfuzz <- trimfuzz;
2044 let x0, y0, x1, y1 = trimfuzz in
2045 invalidate "settrim"
2046 (fun () ->
2047 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2048 flushpages ();
2051 let setzoom zoom =
2052 match state.throttle with
2053 | None ->
2054 let zoom = max 0.0001 zoom in
2055 if zoom <> conf.zoom
2056 then (
2057 state.prevzoom <- (conf.zoom, state.x);
2058 conf.zoom <- zoom;
2059 reshape state.winw state.winh;
2060 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2063 | Some (layout, y, started) ->
2064 let time =
2065 match conf.maxwait with
2066 | None -> 0.0
2067 | Some t -> t
2069 let dt = now () -. started in
2070 if dt > time
2071 then (
2072 state.y <- y;
2073 load layout;
2077 let pivotzoom ?(vw=state.winw - vscrollw ())
2078 ?(vh=min state.maxy (state.winh - hscrollh ()))
2079 ?(x=vw/2) ?(y=vh/2) zoom =
2080 if nogeomcmds state.geomcmds
2081 then
2082 let w = float state.w /. zoom in
2083 let hw = w /. 2.0 in
2084 let ratio = float vh /. float vw in
2085 let hh = hw *. ratio in
2086 let x0 = float x -. hw in
2087 let y0 = float y -. hh in
2088 gotoxy (state.x - truncate x0) (state.y + truncate y0);
2089 setzoom zoom;
2092 let setcolumns mode columns coverA coverB =
2093 state.prevcolumns <- Some (conf.columns, conf.zoom);
2094 if columns < 0
2095 then (
2096 if isbirdseye mode
2097 then impmsg "split mode doesn't work in bird's eye"
2098 else (
2099 conf.columns <- Csplit (-columns, E.a);
2100 state.x <- 0;
2101 conf.zoom <- 1.0;
2104 else (
2105 if columns < 2
2106 then (
2107 conf.columns <- Csingle E.a;
2108 state.x <- 0;
2109 setzoom 1.0;
2111 else (
2112 conf.columns <- Cmulti ((columns, coverA, coverB), E.a);
2113 conf.zoom <- 1.0;
2116 reshape state.winw state.winh;
2119 let resetmstate () =
2120 state.mstate <- Mnone;
2121 Wsi.setcursor Wsi.CURSOR_INHERIT;
2124 let enterbirdseye () =
2125 let zoom = float conf.thumbw /. float state.winw in
2126 let birdseyepageno =
2127 let cy = state.winh / 2 in
2128 let fold = function
2129 | [] -> 0
2130 | l :: rest ->
2131 let rec fold best = function
2132 | [] -> best.pageno
2133 | l :: rest ->
2134 let d = cy - (l.pagedispy + l.pagevh/2)
2135 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2136 if abs d < abs dbest
2137 then fold l rest
2138 else best.pageno
2139 in fold l rest
2141 fold state.layout
2143 state.mode <- Birdseye (
2144 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2146 resetmstate ();
2147 conf.zoom <- zoom;
2148 conf.presentation <- false;
2149 conf.interpagespace <- 10;
2150 conf.hlinks <- false;
2151 conf.fitmodel <- FitPage;
2152 state.x <- 0;
2153 conf.maxwait <- None;
2154 conf.columns <- (
2155 match conf.beyecolumns with
2156 | Some c ->
2157 conf.zoom <- 1.0;
2158 Cmulti ((c, 0, 0), E.a)
2159 | None -> Csingle E.a
2161 if conf.verbose
2162 then
2163 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2164 (100.0*.zoom)
2165 else
2166 state.text <- E.s
2168 reshape state.winw state.winh;
2171 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2172 state.mode <- View;
2173 conf.zoom <- c.zoom;
2174 conf.presentation <- c.presentation;
2175 conf.interpagespace <- c.interpagespace;
2176 conf.maxwait <- c.maxwait;
2177 conf.hlinks <- c.hlinks;
2178 conf.fitmodel <- c.fitmodel;
2179 conf.beyecolumns <- (
2180 match conf.columns with
2181 | Cmulti ((c, _, _), _) -> Some c
2182 | Csingle _ -> None
2183 | Csplit _ -> failwith "leaving bird's eye split mode"
2185 conf.columns <- (
2186 match c.columns with
2187 | Cmulti (c, _) -> Cmulti (c, E.a)
2188 | Csingle _ -> Csingle E.a
2189 | Csplit (c, _) -> Csplit (c, E.a)
2191 if conf.verbose
2192 then
2193 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2194 (100.0*.conf.zoom)
2196 reshape state.winw state.winh;
2197 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2198 state.x <- leftx;
2201 let togglebirdseye () =
2202 match state.mode with
2203 | Birdseye vals -> leavebirdseye vals true
2204 | View -> enterbirdseye ()
2205 | Textentry _
2206 | LinkNav _ -> ()
2209 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2210 let pageno = max 0 (pageno - incr) in
2211 let rec loop = function
2212 | [] -> gotopage1 pageno 0
2213 | l :: _ when l.pageno = pageno ->
2214 if l.pagedispy >= 0 && l.pagey = 0
2215 then G.postRedisplay "upbirdseye"
2216 else gotopage1 pageno 0
2217 | _ :: rest -> loop rest
2219 loop state.layout;
2220 state.text <- E.s;
2221 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2224 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2225 let pageno = min (state.pagecount - 1) (pageno + incr) in
2226 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2227 let rec loop = function
2228 | [] ->
2229 let y, h = getpageyh pageno in
2230 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2231 gotoxy state.x (clamp dy)
2232 | l :: _ when l.pageno = pageno ->
2233 if l.pagevh != l.pageh
2234 then gotoxy state.x (clamp (l.pageh - l.pagevh + conf.interpagespace))
2235 else G.postRedisplay "downbirdseye"
2236 | _ :: rest -> loop rest
2238 loop state.layout;
2239 state.text <- E.s;
2242 let optentry mode _ key =
2243 let btos b = if b then "on" else "off" in
2244 if key >= 32 && key < 127
2245 then
2246 let c = Char.chr key in
2247 match c with
2248 | 's' ->
2249 let ondone s =
2250 try conf.scrollstep <- int_of_string s with exn ->
2251 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2253 TEswitch ("scroll step: ", E.s, None, intentry, ondone, true)
2255 | 'A' ->
2256 let ondone s =
2258 conf.autoscrollstep <- boundastep state.winh (int_of_string s);
2259 if state.autoscroll <> None
2260 then state.autoscroll <- Some conf.autoscrollstep
2261 with exn ->
2262 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2264 TEswitch ("auto scroll step: ", E.s, None, intentry, ondone, true)
2266 | 'C' ->
2267 let ondone s =
2269 let n, a, b = multicolumns_of_string s in
2270 setcolumns mode n a b;
2271 with exn ->
2272 state.text <- Printf.sprintf "bad columns `%s': %s" s @@ exntos exn
2274 TEswitch ("columns: ", E.s, None, textentry, ondone, true)
2276 | 'Z' ->
2277 let ondone s =
2279 let zoom = float (int_of_string s) /. 100.0 in
2280 setzoom zoom
2281 with exn ->
2282 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2284 TEswitch ("zoom: ", E.s, None, intentry, ondone, true)
2286 | 't' ->
2287 let ondone s =
2289 conf.thumbw <- bound (int_of_string s) 2 4096;
2290 state.text <-
2291 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2292 begin match mode with
2293 | Birdseye beye ->
2294 leavebirdseye beye false;
2295 enterbirdseye ();
2296 | Textentry _
2297 | View
2298 | LinkNav _ -> ();
2300 with exn ->
2301 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2303 TEswitch ("thumbnail width: ", E.s, None, intentry, ondone, true)
2305 | 'R' ->
2306 let ondone s =
2307 match int_of_string s with
2308 | angle -> reqlayout angle conf.fitmodel
2309 | exception exn ->
2310 state.text <-
2311 Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2313 TEswitch ("rotation: ", E.s, None, intentry, ondone, true)
2315 | 'i' ->
2316 conf.icase <- not conf.icase;
2317 TEdone ("case insensitive search " ^ (btos conf.icase))
2319 | 'p' ->
2320 conf.preload <- not conf.preload;
2321 gotoxy state.x state.y;
2322 TEdone ("preload " ^ (btos conf.preload))
2324 | 'v' ->
2325 conf.verbose <- not conf.verbose;
2326 TEdone ("verbose " ^ (btos conf.verbose))
2328 | 'd' ->
2329 conf.debug <- not conf.debug;
2330 TEdone ("debug " ^ (btos conf.debug))
2332 | 'h' ->
2333 conf.maxhfit <- not conf.maxhfit;
2334 state.maxy <- calcheight ();
2335 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2337 | 'c' ->
2338 conf.crophack <- not conf.crophack;
2339 TEdone ("crophack " ^ btos conf.crophack)
2341 | 'a' ->
2342 let s =
2343 match conf.maxwait with
2344 | None ->
2345 conf.maxwait <- Some infinity;
2346 "always wait for page to complete"
2347 | Some _ ->
2348 conf.maxwait <- None;
2349 "show placeholder if page is not ready"
2351 TEdone s
2353 | 'f' ->
2354 conf.underinfo <- not conf.underinfo;
2355 TEdone ("underinfo " ^ btos conf.underinfo)
2357 | 'P' ->
2358 conf.savebmarks <- not conf.savebmarks;
2359 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2361 | 'S' ->
2362 let ondone s =
2364 let pageno, py =
2365 match state.layout with
2366 | [] -> 0, 0
2367 | l :: _ ->
2368 l.pageno, l.pagey
2370 conf.interpagespace <- int_of_string s;
2371 docolumns conf.columns;
2372 state.maxy <- calcheight ();
2373 let y = getpagey pageno in
2374 gotoxy state.x (y + py)
2375 with exn ->
2376 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn
2378 TEswitch ("vertical margin: ", E.s, None, intentry, ondone, true)
2380 | 'l' ->
2381 let fm =
2382 match conf.fitmodel with
2383 | FitProportional -> FitWidth
2384 | FitWidth | FitPage -> FitProportional
2386 reqlayout conf.angle fm;
2387 TEdone ("proportional display " ^ btos (fm == FitProportional))
2389 | 'T' ->
2390 settrim (not conf.trimmargins) conf.trimfuzz;
2391 TEdone ("trim margins " ^ btos conf.trimmargins)
2393 | 'I' ->
2394 conf.invert <- not conf.invert;
2395 TEdone ("invert colors " ^ btos conf.invert)
2397 | 'x' ->
2398 let ondone s =
2399 cbput state.hists.sel s;
2400 conf.selcmd <- s;
2402 TEswitch ("selection command: ", E.s, Some (onhist state.hists.sel),
2403 textentry, ondone, true)
2405 | 'M' ->
2406 if conf.pax == None
2407 then conf.pax <- Some (ref (0.0, 0, 0))
2408 else conf.pax <- None;
2409 TEdone ("PAX " ^ btos (conf.pax != None))
2411 | _ ->
2412 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2413 TEstop
2414 else
2415 TEcont state.text
2418 class type lvsource = object
2419 method getitemcount : int
2420 method getitem : int -> (string * int)
2421 method hasaction : int -> bool
2422 method exit :
2423 uioh:uioh ->
2424 cancel:bool ->
2425 active:int ->
2426 first:int ->
2427 pan:int ->
2428 uioh option
2429 method getactive : int
2430 method getfirst : int
2431 method getpan : int
2432 method getminfo : (int * int) array
2433 end;;
2435 class virtual lvsourcebase = object
2436 val mutable m_active = 0
2437 val mutable m_first = 0
2438 val mutable m_pan = 0
2439 method getactive = m_active
2440 method getfirst = m_first
2441 method getpan = m_pan
2442 method getminfo : (int * int) array = E.a
2443 end;;
2445 let textentrykeyboard
2446 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2447 state.text <- E.s;
2448 let key = Wsi.keypadtodigitkey key in
2449 let enttext te =
2450 state.mode <- Textentry (te, onleave);
2451 enttext ();
2452 G.postRedisplay "textentrykeyboard enttext";
2454 let histaction cmd =
2455 match opthist with
2456 | None -> ()
2457 | Some (action, _) ->
2458 state.mode <- Textentry (
2459 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
2461 G.postRedisplay "textentry histaction"
2463 match key with
2464 | @backspace ->
2465 if emptystr text && cancelonempty
2466 then (
2467 onleave Cancel;
2468 G.postRedisplay "textentrykeyboard after cancel";
2470 else
2471 let s = withoutlastutf8 text in
2472 enttext (c, s, opthist, onkey, ondone, cancelonempty)
2474 | @enter | @kpenter ->
2475 ondone text;
2476 onleave Confirm;
2477 G.postRedisplay "textentrykeyboard after confirm"
2479 | @up | @kpup -> histaction HCprev
2480 | @down | @kpdown -> histaction HCnext
2481 | @home | @kphome -> histaction HCfirst
2482 | @jend | @kpend -> histaction HClast
2484 | @escape ->
2485 if emptystr text
2486 then (
2487 begin match opthist with
2488 | None -> ()
2489 | Some (_, onhistcancel) -> onhistcancel ()
2490 end;
2491 onleave Cancel;
2492 state.text <- E.s;
2493 G.postRedisplay "textentrykeyboard after cancel2"
2495 else (
2496 enttext (c, E.s, opthist, onkey, ondone, cancelonempty)
2499 | @delete | @kpdelete -> ()
2501 | _ when key != 0 && not (Wsi.isspecialkey key) ->
2502 begin match onkey text key with
2503 | TEdone text ->
2504 ondone text;
2505 onleave Confirm;
2506 G.postRedisplay "textentrykeyboard after confirm2";
2508 | TEcont text ->
2509 enttext (c, text, opthist, onkey, ondone, cancelonempty);
2511 | TEstop ->
2512 onleave Cancel;
2513 G.postRedisplay "textentrykeyboard after cancel3"
2515 | TEswitch te ->
2516 state.mode <- Textentry (te, onleave);
2517 G.postRedisplay "textentrykeyboard switch";
2518 end;
2520 | _ ->
2521 vlog "unhandled key %s" (Wsi.keyname key)
2524 let firstof first active =
2525 if first > active || abs (first - active) > fstate.maxrows - 1
2526 then max 0 (active - (fstate.maxrows/2))
2527 else first
2530 let calcfirst first active =
2531 if active > first
2532 then
2533 let rows = active - first in
2534 if rows > fstate.maxrows then active - fstate.maxrows else first
2535 else active
2538 let scrollph y maxy =
2539 let sh = float (maxy + state.winh) /. float state.winh in
2540 let sh = float state.winh /. sh in
2541 let sh = max sh (float conf.scrollh) in
2543 let percent = float y /. float maxy in
2544 let position = (float state.winh -. sh) *. percent in
2546 let position =
2547 if position +. sh > float state.winh
2548 then float state.winh -. sh
2549 else position
2551 position, sh;
2554 let adderrmsg src msg =
2555 Buffer.add_string state.errmsgs msg;
2556 state.newerrmsgs <- true;
2557 G.postRedisplay src
2560 let adderrfmt src fmt =
2561 Format.ksprintf (fun s -> adderrmsg src s) fmt;
2564 let coe s = (s :> uioh);;
2566 class listview ~zebra ~helpmode ~(source:lvsource) ~trusted ~modehash =
2567 object (self)
2568 val m_pan = source#getpan
2569 val m_first = source#getfirst
2570 val m_active = source#getactive
2571 val m_qsearch = E.s
2572 val m_prev_uioh = state.uioh
2574 method private elemunder y =
2575 if y < 0
2576 then None
2577 else
2578 let n = y / (fstate.fontsize+1) in
2579 if m_first + n < source#getitemcount
2580 then (
2581 if source#hasaction (m_first + n)
2582 then Some (m_first + n)
2583 else None
2585 else None
2587 method display =
2588 Gl.enable `blend;
2589 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
2590 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2591 filledrect 0. 0. (float state.winw) (float state.winh);
2592 GlDraw.color (1., 1., 1.);
2593 Gl.enable `texture_2d;
2594 let fs = fstate.fontsize in
2595 let nfs = fs + 1 in
2596 let hw = (wadjsb () + xadjsb () + state.winw)/3 in
2597 let ww = fstate.wwidth in
2598 let tabw = 17.0*.ww in
2599 let itemcount = source#getitemcount in
2600 let minfo = source#getminfo in
2601 let x0 = 0.0
2602 and x1 =
2603 if conf.leftscroll
2604 then float (state.winw - 1)
2605 else float (state.winw - conf.scrollbw - 1)
2607 let xadj = xadjsb () in
2608 let rec loop row =
2609 if (row - m_first) > fstate.maxrows
2610 then ()
2611 else (
2612 if row >= 0 && row < itemcount
2613 then (
2614 let (s, level) = source#getitem row in
2615 let y = (row - m_first) * nfs in
2616 let x =
2617 (if conf.leftscroll then float xadj else 5.0)
2618 +. (float (level + m_pan)) *. ww in
2619 if helpmode
2620 then GlDraw.color
2621 (let c = if row land 1 = 0 then 1.0 else 0.92 in (c,c,c));
2623 if row = m_active
2624 then (
2625 Gl.disable `texture_2d;
2626 let alpha = if source#hasaction row then 0.9 else 0.3 in
2627 GlDraw.color (1., 1., 1.) ~alpha;
2628 linerect (float xadj +. x0 +. 1.)
2629 (float (y + 1)) (x1) (float (y + fs + 3));
2630 Gl.enable `texture_2d;
2632 let c =
2633 if zebra && row land 1 = 1
2634 then 0.8
2635 else 1.0
2637 GlDraw.color (c,c,c);
2638 let drawtabularstring s =
2639 let drawstr x s =
2640 let x' = truncate (x0 +. x) in
2641 let pos = nindex s '\000' in
2642 if pos = -1
2643 then drawstring1 fs x' (y+nfs) s
2644 else
2645 let s1 = String.sub s 0 pos
2646 and s2 = String.sub s (pos+1) (String.length s - pos - 1) in
2647 let rec e s =
2648 if emptystr s
2649 then s
2650 else
2651 let s' = withoutlastutf8 s in
2652 let s = s' ^ "@Uellipsis" in
2653 let w = measurestr fs s in
2654 if float x' +. w +. ww < float (hw + x')
2655 then s
2656 else e s'
2658 let s1 =
2659 if float x' +. ww +. measurestr fs s1 > float (hw + x')
2660 then e s1
2661 else s1
2663 ignore (drawstring1 fs x' (y+nfs) s1);
2664 drawstring1 fs (hw + x') (y+nfs) s2
2666 if trusted
2667 then
2668 let x = if helpmode && row > 0 then x +. ww else x in
2669 let tabpos = nindex s '\t' in
2670 if tabpos > 0
2671 then
2672 let len = String.length s - tabpos - 1 in
2673 let s1 = String.sub s 0 tabpos
2674 and s2 = String.sub s (tabpos + 1) len in
2675 let nx = drawstr x s1 in
2676 let sw = nx -. x in
2677 let x = x +. (max tabw sw) in
2678 drawstr x s2
2679 else
2680 let len = String.length s - 2 in
2681 if len > 0 && s.[0] = '\xc2' && s.[1] = '\xb7'
2682 then
2683 let s = String.sub s 2 len in
2684 let x = if not helpmode then x +. ww else x in
2685 GlDraw.color (1.2, 1.2, 1.2);
2686 let vinc = drawstring1 (fs+fs/4)
2687 (truncate (x -. ww)) (y+nfs) s in
2688 GlDraw.color (1., 1., 1.);
2689 vinc +. (float fs *. 0.8)
2690 else
2691 drawstr x s
2692 else
2693 drawstr x s
2695 ignore (drawtabularstring s);
2696 loop (row+1)
2700 loop m_first;
2701 GlDraw.color (1.0, 1.0, 1.0) ~alpha:0.5;
2702 let xadj = float (xadjsb () + 5) in
2703 let rec loop row =
2704 if (row - m_first) > fstate.maxrows
2705 then ()
2706 else (
2707 if row >= 0 && row < itemcount
2708 then (
2709 let (s, level) = source#getitem row in
2710 let pos0 = nindex s '\000' in
2711 let y = (row - m_first) * nfs in
2712 let x = float (level + m_pan) *. ww in
2713 let (first, last) = minfo.(row) in
2714 let prefix =
2715 if pos0 > 0 && first > pos0
2716 then String.sub s (pos0+1) (first-pos0-1)
2717 else String.sub s 0 first
2719 let suffix = String.sub s first (last - first) in
2720 let w1 = measurestr fstate.fontsize prefix in
2721 let w2 = measurestr fstate.fontsize suffix in
2722 let x = x +. if conf.leftscroll then xadj else 5.0 in
2723 let x = if pos0 > 0 && first > pos0 then x +. float hw else x in
2724 let x0 = x +. w1
2725 and y0 = float (y+2) in
2726 let x1 = x0 +. w2
2727 and y1 = float (y+fs+3) in
2728 filledrect x0 y0 x1 y1;
2729 loop (row+1)
2733 Gl.disable `texture_2d;
2734 if Array.length minfo > 0 then loop m_first;
2735 Gl.disable `blend;
2737 method updownlevel incr =
2738 let len = source#getitemcount in
2739 let curlevel =
2740 if m_active >= 0 && m_active < len
2741 then snd (source#getitem m_active)
2742 else -1
2744 let rec flow i =
2745 if i = len then i-1 else if i = -1 then 0 else
2746 let _, l = source#getitem i in
2747 if l != curlevel then i else flow (i+incr)
2749 let active = flow m_active in
2750 let first = calcfirst m_first active in
2751 G.postRedisplay "outline updownlevel";
2752 {< m_active = active; m_first = first >}
2754 method private key1 key mask =
2755 let set1 active first qsearch =
2756 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2758 let search active pattern incr =
2759 let active = if active = -1 then m_first else active in
2760 let dosearch re =
2761 let rec loop n =
2762 if n >= 0 && n < source#getitemcount
2763 then (
2764 let s, _ = source#getitem n in
2765 match Str.search_forward re s 0 with
2766 | (exception Not_found) -> loop (n + incr)
2767 | _ -> Some n
2769 else None
2771 loop active
2773 let qpat = Str.quote pattern in
2774 match Str.regexp_case_fold qpat with
2775 | s -> dosearch s
2776 | exception exn ->
2777 adderrfmt "listview key1" "regexp_case_fold for `%S' failed: %S\n"
2778 qpat @@ Printexc.to_string exn;
2779 None
2781 let itemcount = source#getitemcount in
2782 let find start incr =
2783 let rec find i =
2784 if i = -1 || i = itemcount
2785 then -1
2786 else (
2787 if source#hasaction i
2788 then i
2789 else find (i + incr)
2792 find start
2794 let set active first =
2795 let first = bound first 0 (itemcount - fstate.maxrows) in
2796 state.text <- E.s;
2797 coe {< m_active = active; m_first = first; m_qsearch = E.s >}
2799 let navigate incr =
2800 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2801 let active, first =
2802 let incr1 = if incr > 0 then 1 else -1 in
2803 if isvisible m_first m_active
2804 then
2805 let next =
2806 let next = m_active + incr in
2807 let next =
2808 if next < 0 || next >= itemcount
2809 then -1
2810 else find next incr1
2812 if abs (m_active - next) > fstate.maxrows
2813 then -1
2814 else next
2816 if next = -1
2817 then
2818 let first = m_first + incr in
2819 let first = bound first 0 (itemcount - fstate.maxrows) in
2820 let next =
2821 let next = m_active + incr in
2822 let next = bound next 0 (itemcount - 1) in
2823 find next ~-incr1
2825 let active =
2826 if next = -1
2827 then m_active
2828 else (
2829 if isvisible first next
2830 then next
2831 else m_active
2834 active, first
2835 else
2836 let first = min next m_first in
2837 let first =
2838 if abs (next - first) > fstate.maxrows
2839 then first + incr
2840 else first
2842 next, first
2843 else
2844 let first = m_first + incr in
2845 let first = bound first 0 (itemcount - 1) in
2846 let active =
2847 let next = m_active + incr in
2848 let next = bound next 0 (itemcount - 1) in
2849 let next = find next incr1 in
2850 let active =
2851 if next = -1 || abs (m_active - first) > fstate.maxrows
2852 then (
2853 let active = if m_active = -1 then next else m_active in
2854 active
2856 else next
2858 if isvisible first active
2859 then active
2860 else -1
2862 active, first
2864 G.postRedisplay "listview navigate";
2865 set active first;
2867 match key with
2868 | (@r|@s) when Wsi.withctrl mask ->
2869 let incr = if key = @r then -1 else 1 in
2870 let active, first =
2871 match search (m_active + incr) m_qsearch incr with
2872 | None ->
2873 state.text <- m_qsearch ^ " [not found]";
2874 m_active, m_first
2875 | Some active ->
2876 state.text <- m_qsearch;
2877 active, firstof m_first active
2879 G.postRedisplay "listview ctrl-r/s";
2880 set1 active first m_qsearch;
2882 | @insert when Wsi.withctrl mask ->
2883 if m_active >= 0 && m_active < source#getitemcount
2884 then (
2885 let s, _ = source#getitem m_active in
2886 selstring s;
2888 coe self
2890 | @backspace ->
2891 if emptystr m_qsearch
2892 then coe self
2893 else (
2894 let qsearch = withoutlastutf8 m_qsearch in
2895 if emptystr qsearch
2896 then (
2897 state.text <- E.s;
2898 G.postRedisplay "listview empty qsearch";
2899 set1 m_active m_first E.s;
2901 else
2902 let active, first =
2903 match search m_active qsearch ~-1 with
2904 | None ->
2905 state.text <- qsearch ^ " [not found]";
2906 m_active, m_first
2907 | Some active ->
2908 state.text <- qsearch;
2909 active, firstof m_first active
2911 G.postRedisplay "listview backspace qsearch";
2912 set1 active first qsearch
2915 | key when (key != 0 && not (Wsi.isspecialkey key)) ->
2916 let pattern = m_qsearch ^ toutf8 key in
2917 let active, first =
2918 match search m_active pattern 1 with
2919 | None ->
2920 state.text <- pattern ^ " [not found]";
2921 m_active, m_first
2922 | Some active ->
2923 state.text <- pattern;
2924 active, firstof m_first active
2926 G.postRedisplay "listview qsearch add";
2927 set1 active first pattern;
2929 | @escape ->
2930 state.text <- E.s;
2931 if emptystr m_qsearch
2932 then (
2933 G.postRedisplay "list view escape";
2934 let mx, my = state.mpos in
2935 updateunder mx my;
2936 begin
2937 match
2938 source#exit ~uioh:(coe self)
2939 ~cancel:true ~active:m_active ~first:m_first ~pan:m_pan
2940 with
2941 | None -> m_prev_uioh
2942 | Some uioh -> uioh
2945 else (
2946 G.postRedisplay "list view kill qsearch";
2947 coe {< m_qsearch = E.s >}
2950 | @enter | @kpenter ->
2951 state.text <- E.s;
2952 let self = {< m_qsearch = E.s >} in
2953 let opt =
2954 G.postRedisplay "listview enter";
2955 if m_active >= 0 && m_active < source#getitemcount
2956 then (
2957 source#exit ~uioh:(coe self) ~cancel:false
2958 ~active:m_active ~first:m_first ~pan:m_pan;
2960 else (
2961 source#exit ~uioh:(coe self) ~cancel:true
2962 ~active:m_active ~first:m_first ~pan:m_pan;
2965 begin match opt with
2966 | None -> m_prev_uioh
2967 | Some uioh -> uioh
2970 | @delete | @kpdelete ->
2971 coe self
2973 | @up | @kpup -> navigate ~-1
2974 | @down | @kpdown -> navigate 1
2975 | @prior | @kpprior -> navigate ~-(fstate.maxrows)
2976 | @next | @kpnext -> navigate fstate.maxrows
2978 | @right | @kpright ->
2979 state.text <- E.s;
2980 G.postRedisplay "listview right";
2981 coe {< m_pan = m_pan - 1 >}
2983 | @left | @kpleft ->
2984 state.text <- E.s;
2985 G.postRedisplay "listview left";
2986 coe {< m_pan = m_pan + 1 >}
2988 | @home | @kphome ->
2989 let active = find 0 1 in
2990 G.postRedisplay "listview home";
2991 set active 0;
2993 | @jend | @kpend ->
2994 let first = max 0 (itemcount - fstate.maxrows) in
2995 let active = find (itemcount - 1) ~-1 in
2996 G.postRedisplay "listview end";
2997 set active first;
2999 | key when (key = 0 || Wsi.isspecialkey key) ->
3000 coe self
3002 | _ ->
3003 dolog "listview unknown key %#x" key; coe self
3005 method key key mask =
3006 match state.mode with
3007 | Textentry te -> textentrykeyboard key mask te; coe self
3008 | Birdseye _
3009 | View
3010 | LinkNav _ -> self#key1 key mask
3012 method button button down x y _ =
3013 let opt =
3014 match button with
3015 | 1 when vscrollhit x ->
3016 G.postRedisplay "listview scroll";
3017 if down
3018 then
3019 let _, position, sh = self#scrollph in
3020 if y > truncate position && y < truncate (position +. sh)
3021 then (
3022 state.mstate <- Mscrolly;
3023 Some (coe self)
3025 else
3026 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3027 let first = truncate (s *. float source#getitemcount) in
3028 let first = min source#getitemcount first in
3029 Some (coe {< m_first = first; m_active = first >})
3030 else (
3031 state.mstate <- Mnone;
3032 Some (coe self);
3034 | 1 when down ->
3035 begin match self#elemunder y with
3036 | Some n ->
3037 G.postRedisplay "listview click";
3038 source#exit ~uioh:(coe {< m_active = n >})
3039 ~cancel:false ~active:n ~first:m_first ~pan:m_pan
3040 | _ ->
3041 Some (coe self)
3043 | n when (n == 4 || n == 5) && not down ->
3044 let len = source#getitemcount in
3045 let first =
3046 if n = 5 && m_first + fstate.maxrows >= len
3047 then
3048 m_first
3049 else
3050 let first = m_first + (if n == 4 then -1 else 1) in
3051 bound first 0 (len - 1)
3053 G.postRedisplay "listview wheel";
3054 Some (coe {< m_first = first >})
3055 | n when (n = 6 || n = 7) && not down ->
3056 let inc = if n = 7 then -1 else 1 in
3057 G.postRedisplay "listview hwheel";
3058 Some (coe {< m_pan = m_pan + inc >})
3059 | _ ->
3060 Some (coe self)
3062 match opt with
3063 | None -> m_prev_uioh
3064 | Some uioh -> uioh
3066 method multiclick _ x y = self#button 1 true x y
3068 method motion _ y =
3069 match state.mstate with
3070 | Mscrolly ->
3071 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3072 let first = truncate (s *. float source#getitemcount) in
3073 let first = min source#getitemcount first in
3074 G.postRedisplay "listview motion";
3075 coe {< m_first = first; m_active = first >}
3076 | Msel _
3077 | Mpan _
3078 | Mscrollx
3079 | Mzoom _
3080 | Mzoomrect _
3081 | Mnone -> coe self
3083 method pmotion x y =
3084 if x < state.winw - conf.scrollbw
3085 then
3086 let n =
3087 match self#elemunder y with
3088 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3089 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3091 let o =
3092 if n != m_active
3093 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3094 else self
3096 coe o
3097 else (
3098 Wsi.setcursor Wsi.CURSOR_INHERIT;
3099 coe self
3102 method infochanged _ = ()
3104 method scrollpw = (0, 0.0, 0.0)
3105 method scrollph =
3106 let nfs = fstate.fontsize + 1 in
3107 let y = m_first * nfs in
3108 let itemcount = source#getitemcount in
3109 let maxi = max 0 (itemcount - fstate.maxrows) in
3110 let maxy = maxi * nfs in
3111 let p, h = scrollph y maxy in
3112 conf.scrollbw, p, h
3114 method modehash = modehash
3115 method eformsgs = false
3116 method alwaysscrolly = true
3117 end;;
3119 class outlinelistview ~zebra ~source =
3120 let settext autonarrow s =
3121 if autonarrow
3122 then
3123 let ss = source#statestr in
3124 state.text <-
3125 if emptystr ss
3126 then "[" ^ s ^ "]"
3127 else "{" ^ ss ^ "} [" ^ s ^ "]"
3128 else state.text <- s
3130 object (self)
3131 inherit listview
3132 ~zebra
3133 ~helpmode:false
3134 ~source:(source :> lvsource)
3135 ~trusted:false
3136 ~modehash:(findkeyhash conf "outline")
3137 as super
3139 val m_autonarrow = false
3141 method! key key mask =
3142 let maxrows =
3143 if emptystr state.text
3144 then fstate.maxrows
3145 else fstate.maxrows - 2
3147 let calcfirst first active =
3148 if active > first
3149 then
3150 let rows = active - first in
3151 if rows > maxrows then active - maxrows else first
3152 else active
3154 let navigate incr =
3155 let active = m_active + incr in
3156 let active = bound active 0 (source#getitemcount - 1) in
3157 let first = calcfirst m_first active in
3158 G.postRedisplay "outline navigate";
3159 coe {< m_active = active; m_first = first >}
3161 let navscroll first =
3162 let active =
3163 let dist = m_active - first in
3164 if dist < 0
3165 then first
3166 else (
3167 if dist < maxrows
3168 then m_active
3169 else first + maxrows
3172 G.postRedisplay "outline navscroll";
3173 coe {< m_first = first; m_active = active >}
3175 let ctrl = Wsi.withctrl mask in
3176 match key with
3177 | @a when ctrl ->
3178 let text =
3179 if m_autonarrow
3180 then (source#denarrow; E.s)
3181 else (
3182 let pattern = source#renarrow in
3183 if nonemptystr m_qsearch
3184 then (source#narrow m_qsearch; m_qsearch)
3185 else pattern
3188 settext (not m_autonarrow) text;
3189 G.postRedisplay "toggle auto narrowing";
3190 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
3192 | @slash when emptystr m_qsearch && not m_autonarrow ->
3193 settext true E.s;
3194 G.postRedisplay "toggle auto narrowing";
3195 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
3197 | @n when ctrl ->
3198 source#narrow m_qsearch;
3199 if not m_autonarrow
3200 then source#add_narrow_pattern m_qsearch;
3201 G.postRedisplay "outline ctrl-n";
3202 coe {< m_first = 0; m_active = 0 >}
3204 | @S when ctrl ->
3205 let active = source#calcactive (getanchor ()) in
3206 let first = firstof m_first active in
3207 G.postRedisplay "outline ctrl-s";
3208 coe {< m_first = first; m_active = active >}
3210 | @u when ctrl ->
3211 G.postRedisplay "outline ctrl-u";
3212 if m_autonarrow && nonemptystr m_qsearch
3213 then (
3214 ignore (source#renarrow);
3215 settext m_autonarrow E.s;
3216 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3218 else (
3219 source#del_narrow_pattern;
3220 let pattern = source#renarrow in
3221 let text =
3222 if emptystr pattern then E.s else "Narrowed to " ^ pattern
3224 settext m_autonarrow text;
3225 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3228 | @l when ctrl ->
3229 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3230 G.postRedisplay "outline ctrl-l";
3231 coe {< m_first = first >}
3233 | @tab when m_autonarrow ->
3234 if nonemptystr m_qsearch
3235 then (
3236 G.postRedisplay "outline list view tab";
3237 source#add_narrow_pattern m_qsearch;
3238 settext true E.s;
3239 coe {< m_qsearch = E.s >}
3241 else coe self
3243 | @escape when m_autonarrow ->
3244 if nonemptystr m_qsearch
3245 then source#add_narrow_pattern m_qsearch;
3246 super#key key mask
3248 | @enter | @kpenter when m_autonarrow ->
3249 if nonemptystr m_qsearch
3250 then source#add_narrow_pattern m_qsearch;
3251 super#key key mask
3253 | key when m_autonarrow && (not (Wsi.isspecialkey key)) ->
3254 let pattern = m_qsearch ^ toutf8 key in
3255 G.postRedisplay "outlinelistview autonarrow add";
3256 source#narrow pattern;
3257 settext true pattern;
3258 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3260 | key when m_autonarrow && key = @backspace ->
3261 if emptystr m_qsearch
3262 then coe self
3263 else
3264 let pattern = withoutlastutf8 m_qsearch in
3265 G.postRedisplay "outlinelistview autonarrow backspace";
3266 ignore (source#renarrow);
3267 source#narrow pattern;
3268 settext true pattern;
3269 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3271 | @up | @kpup when ctrl ->
3272 navscroll (max 0 (m_first - 1))
3274 | @down | @kpdown when ctrl ->
3275 navscroll (min (source#getitemcount - 1) (m_first + 1))
3277 | @up | @kpup -> navigate ~-1
3278 | @down | @kpdown -> navigate 1
3279 | @prior | @kpprior -> navigate ~-(fstate.maxrows)
3280 | @next | @kpnext -> navigate fstate.maxrows
3282 | @right | @kpright ->
3283 let o =
3284 if ctrl
3285 then (
3286 G.postRedisplay "outline ctrl right";
3287 {< m_pan = m_pan + 1 >}
3289 else self#updownlevel 1
3291 coe o
3293 | @left | @kpleft ->
3294 let o =
3295 if ctrl
3296 then (
3297 G.postRedisplay "outline ctrl left";
3298 {< m_pan = m_pan - 1 >}
3300 else self#updownlevel ~-1
3302 coe o
3304 | @home | @kphome ->
3305 G.postRedisplay "outline home";
3306 coe {< m_first = 0; m_active = 0 >}
3308 | @jend | @kpend ->
3309 let active = source#getitemcount - 1 in
3310 let first = max 0 (active - fstate.maxrows) in
3311 G.postRedisplay "outline end";
3312 coe {< m_active = active; m_first = first >}
3314 | _ -> super#key key mask
3315 end;;
3317 let genhistoutlines () =
3318 Config.gethist ()
3319 |> List.sort (fun (_, c1, _, _, _, _) (_, c2, _, _, _, _) ->
3320 compare c2.lastvisit c1.lastvisit)
3321 |> List.map
3322 (fun ((path, c, _, _, _, origin) as hist) ->
3323 let path = if nonemptystr origin then origin else path in
3324 let base = mbtoutf8 @@ Filename.basename path in
3325 (base ^ "\000" ^ c.title, 1, Ohistory hist)
3327 |> Array.of_list
3330 let gotohist (path, c, bookmarks, x, anchor, origin) =
3331 Config.save leavebirdseye;
3332 state.anchor <- anchor;
3333 state.bookmarks <- bookmarks;
3334 state.origin <- origin;
3335 state.x <- x;
3336 setconf conf c;
3337 let x0, y0, x1, y1 = conf.trimfuzz in
3338 wcmd "trimset %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1;
3339 reshape ~firsttime:true state.winw state.winh;
3340 opendoc path origin;
3341 setzoom c.zoom;
3344 let makecheckers () =
3345 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3346 following to say:
3347 converted by Issac Trotts. July 25, 2002 *)
3348 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3349 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3350 let id = GlTex.gen_texture () in
3351 GlTex.bind_texture ~target:`texture_2d id;
3352 GlPix.store (`unpack_alignment 1);
3353 GlTex.image2d image;
3354 List.iter (GlTex.parameter ~target:`texture_2d)
3355 [ `mag_filter `nearest; `min_filter `nearest ];
3359 let setcheckers enabled =
3360 match state.checkerstexid with
3361 | None ->
3362 if enabled then state.checkerstexid <- Some (makecheckers ())
3364 | Some checkerstexid ->
3365 if not enabled
3366 then (
3367 GlTex.delete_texture checkerstexid;
3368 state.checkerstexid <- None;
3372 let describe_location () =
3373 let fn = page_of_y state.y in
3374 let ln = page_of_y (state.y + state.winh - hscrollh () - 1) in
3375 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
3376 let percent =
3377 if maxy <= 0
3378 then 100.
3379 else (100. *. (float state.y /. float maxy))
3381 if fn = ln
3382 then
3383 Printf.sprintf "page %d of %d [%.2f%%]"
3384 (fn+1) state.pagecount percent
3385 else
3386 Printf.sprintf
3387 "pages %d-%d of %d [%.2f%%]"
3388 (fn+1) (ln+1) state.pagecount percent
3391 let setpresentationmode v =
3392 let n = page_of_y state.y in
3393 state.anchor <- (n, 0.0, 1.0);
3394 conf.presentation <- v;
3395 if conf.fitmodel = FitPage
3396 then reqlayout conf.angle conf.fitmodel;
3397 represent ();
3400 let setbgcol (r, g, b) =
3401 let col =
3402 let r = r *. 255.0 |> truncate
3403 and g = g *. 255.0 |> truncate
3404 and b = b *. 255.0 |> truncate in
3405 r lsl 16 |> (lor) (g lsl 8) |> (lor) b
3407 Wsi.setwinbgcol col;
3410 let enterinfomode =
3411 let btos b = if b then "@Uradical" else E.s in
3412 let showextended = ref false in
3413 let leave mode _ = state.mode <- mode in
3414 let src =
3415 (object
3416 val mutable m_l = []
3417 val mutable m_a = E.a
3418 val mutable m_prev_uioh = nouioh
3419 val mutable m_prev_mode = View
3421 inherit lvsourcebase
3423 method reset prev_mode prev_uioh =
3424 m_a <- Array.of_list (List.rev m_l);
3425 m_l <- [];
3426 m_prev_mode <- prev_mode;
3427 m_prev_uioh <- prev_uioh;
3429 method int name get set =
3430 m_l <-
3431 (name, `int get, 1, Action (
3432 fun u ->
3433 let ondone s =
3434 try set (int_of_string s)
3435 with exn ->
3436 state.text <- Printf.sprintf "bad integer `%s': %s"
3437 s @@ exntos exn
3439 state.text <- E.s;
3440 let te = name ^ ": ", E.s, None, intentry, ondone, true in
3441 state.mode <- Textentry (te, leave m_prev_mode);
3443 )) :: m_l
3445 method int_with_suffix name get set =
3446 m_l <-
3447 (name, `intws get, 1, Action (
3448 fun u ->
3449 let ondone s =
3450 try set (int_of_string_with_suffix s)
3451 with exn ->
3452 state.text <- Printf.sprintf "bad integer `%s': %s"
3453 s @@ exntos exn
3455 state.text <- E.s;
3456 let te =
3457 name ^ ": ", E.s, None, intentry_with_suffix, ondone, true
3459 state.mode <- Textentry (te, leave m_prev_mode);
3461 )) :: m_l
3463 method bool ?(offset=1) ?(btos=btos) name get set =
3464 m_l <-
3465 (name, `bool (btos, get), offset, Action (
3466 fun u ->
3467 let v = get () in
3468 set (not v);
3470 )) :: m_l
3472 method color name get set =
3473 m_l <-
3474 (name, `color get, 1, Action (
3475 fun u ->
3476 let invalid = (nan, nan, nan) in
3477 let ondone s =
3478 let c =
3479 try color_of_string s
3480 with exn ->
3481 state.text <- Printf.sprintf "bad color `%s': %s"
3482 s @@ exntos exn;
3483 invalid
3485 if c <> invalid
3486 then set c;
3488 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3489 state.text <- color_to_string (get ());
3490 state.mode <- Textentry (te, leave m_prev_mode);
3492 )) :: m_l
3494 method string name get set =
3495 m_l <-
3496 (name, `string get, 1, Action (
3497 fun u ->
3498 let ondone s = set s in
3499 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3500 state.mode <- Textentry (te, leave m_prev_mode);
3502 )) :: m_l
3504 method colorspace name get set =
3505 m_l <-
3506 (name, `string get, 1, Action (
3507 fun _ ->
3508 let source =
3509 (object
3510 inherit lvsourcebase
3512 initializer
3513 m_active <- CSTE.to_int conf.colorspace;
3514 m_first <- 0;
3516 method getitemcount =
3517 Array.length CSTE.names
3518 method getitem n =
3519 (CSTE.names.(n), 0)
3520 method exit ~uioh ~cancel ~active ~first ~pan =
3521 ignore (uioh, first, pan);
3522 if not cancel then set active;
3523 None
3524 method hasaction _ = true
3525 end)
3527 state.text <- E.s;
3528 let modehash = findkeyhash conf "info" in
3529 coe (new listview ~zebra:false ~helpmode:false
3530 ~source ~trusted:true ~modehash)
3531 )) :: m_l
3533 method paxmark name get set =
3534 m_l <-
3535 (name, `string get, 1, Action (
3536 fun _ ->
3537 let source =
3538 (object
3539 inherit lvsourcebase
3541 initializer
3542 m_active <- MTE.to_int conf.paxmark;
3543 m_first <- 0;
3545 method getitemcount = Array.length MTE.names
3546 method getitem n = (MTE.names.(n), 0)
3547 method exit ~uioh ~cancel ~active ~first ~pan =
3548 ignore (uioh, first, pan);
3549 if not cancel then set active;
3550 None
3551 method hasaction _ = true
3552 end)
3554 state.text <- E.s;
3555 let modehash = findkeyhash conf "info" in
3556 coe (new listview ~zebra:false ~helpmode:false
3557 ~source ~trusted:true ~modehash)
3558 )) :: m_l
3560 method fitmodel name get set =
3561 m_l <-
3562 (name, `string get, 1, Action (
3563 fun _ ->
3564 let source =
3565 (object
3566 inherit lvsourcebase
3568 initializer
3569 m_active <- FMTE.to_int conf.fitmodel;
3570 m_first <- 0;
3572 method getitemcount = Array.length FMTE.names
3573 method getitem n = (FMTE.names.(n), 0)
3574 method exit ~uioh ~cancel ~active ~first ~pan =
3575 ignore (uioh, first, pan);
3576 if not cancel then set active;
3577 None
3578 method hasaction _ = true
3579 end)
3581 state.text <- E.s;
3582 let modehash = findkeyhash conf "info" in
3583 coe (new listview ~zebra:false ~helpmode:false
3584 ~source ~trusted:true ~modehash)
3585 )) :: m_l
3587 method caption s offset =
3588 m_l <- (s, `empty, offset, Noaction) :: m_l
3590 method caption2 s f offset =
3591 m_l <- (s, `string f, offset, Noaction) :: m_l
3593 method getitemcount = Array.length m_a
3595 method getitem n =
3596 let tostr = function
3597 | `int f -> string_of_int (f ())
3598 | `intws f -> string_with_suffix_of_int (f ())
3599 | `string f -> f ()
3600 | `color f -> color_to_string (f ())
3601 | `bool (btos, f) -> btos (f ())
3602 | `empty -> E.s
3604 let name, t, offset, _ = m_a.(n) in
3605 ((let s = tostr t in
3606 if nonemptystr s
3607 then Printf.sprintf "%s\t%s" name s
3608 else name),
3609 offset)
3611 method exit ~uioh ~cancel ~active ~first ~pan =
3612 let uiohopt =
3613 if not cancel
3614 then (
3615 let uioh =
3616 match m_a.(active) with
3617 | _, _, _, Action f -> f uioh
3618 | _, _, _, Noaction -> uioh
3620 Some uioh
3622 else None
3624 m_active <- active;
3625 m_first <- first;
3626 m_pan <- pan;
3627 uiohopt
3629 method hasaction n =
3630 match m_a.(n) with
3631 | _, _, _, Action _ -> true
3632 | _, _, _, Noaction -> false
3634 initializer m_active <- 1
3635 end)
3637 let rec fillsrc prevmode prevuioh =
3638 let sep () = src#caption E.s 0 in
3639 let colorp name get set =
3640 src#string name
3641 (fun () -> color_to_string (get ()))
3642 (fun v ->
3644 let c = color_of_string v in
3645 set c
3646 with exn ->
3647 state.text <- Printf.sprintf "bad color `%s': %s" v @@ exntos exn
3650 let oldmode = state.mode in
3651 let birdseye = isbirdseye state.mode in
3653 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3655 src#bool "presentation mode"
3656 (fun () -> conf.presentation)
3657 (fun v -> setpresentationmode v);
3659 src#bool "ignore case in searches"
3660 (fun () -> conf.icase)
3661 (fun v -> conf.icase <- v);
3663 src#bool "preload"
3664 (fun () -> conf.preload)
3665 (fun v -> conf.preload <- v);
3667 src#bool "highlight links"
3668 (fun () -> conf.hlinks)
3669 (fun v -> conf.hlinks <- v);
3671 src#bool "under info"
3672 (fun () -> conf.underinfo)
3673 (fun v -> conf.underinfo <- v);
3675 src#bool "persistent bookmarks"
3676 (fun () -> conf.savebmarks)
3677 (fun v -> conf.savebmarks <- v);
3679 src#fitmodel "fit model"
3680 (fun () -> FMTE.to_string conf.fitmodel)
3681 (fun v -> reqlayout conf.angle (FMTE.of_int v));
3683 src#bool "trim margins"
3684 (fun () -> conf.trimmargins)
3685 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3687 src#bool "persistent location"
3688 (fun () -> conf.jumpback)
3689 (fun v -> conf.jumpback <- v);
3691 sep ();
3692 src#int "inter-page space"
3693 (fun () -> conf.interpagespace)
3694 (fun n ->
3695 conf.interpagespace <- n;
3696 docolumns conf.columns;
3697 let pageno, py =
3698 match state.layout with
3699 | [] -> 0, 0
3700 | l :: _ ->
3701 l.pageno, l.pagey
3703 state.maxy <- calcheight ();
3704 let y = getpagey pageno in
3705 gotoxy state.x (y + py)
3708 src#int "page bias"
3709 (fun () -> conf.pagebias)
3710 (fun v -> conf.pagebias <- v);
3712 src#int "scroll step"
3713 (fun () -> conf.scrollstep)
3714 (fun n -> conf.scrollstep <- n);
3716 src#int "horizontal scroll step"
3717 (fun () -> conf.hscrollstep)
3718 (fun v -> conf.hscrollstep <- v);
3720 src#int "auto scroll step"
3721 (fun () ->
3722 match state.autoscroll with
3723 | Some step -> step
3724 | _ -> conf.autoscrollstep)
3725 (fun n ->
3726 let n = boundastep state.winh n in
3727 if state.autoscroll <> None
3728 then state.autoscroll <- Some n;
3729 conf.autoscrollstep <- n);
3731 src#int "zoom"
3732 (fun () -> truncate (conf.zoom *. 100.))
3733 (fun v -> setzoom ((float v) /. 100.));
3735 src#int "rotation"
3736 (fun () -> conf.angle)
3737 (fun v -> reqlayout v conf.fitmodel);
3739 src#int "scroll bar width"
3740 (fun () -> conf.scrollbw)
3741 (fun v ->
3742 conf.scrollbw <- v;
3743 reshape state.winw state.winh;
3746 src#int "scroll handle height"
3747 (fun () -> conf.scrollh)
3748 (fun v -> conf.scrollh <- v;);
3750 src#int "thumbnail width"
3751 (fun () -> conf.thumbw)
3752 (fun v ->
3753 conf.thumbw <- min 4096 v;
3754 match oldmode with
3755 | Birdseye beye ->
3756 leavebirdseye beye false;
3757 enterbirdseye ()
3758 | Textentry _
3759 | View
3760 | LinkNav _ -> ()
3763 let mode = state.mode in
3764 src#string "columns"
3765 (fun () ->
3766 match conf.columns with
3767 | Csingle _ -> "1"
3768 | Cmulti (multi, _) -> multicolumns_to_string multi
3769 | Csplit (count, _) -> "-" ^ string_of_int count
3771 (fun v ->
3772 let n, a, b = multicolumns_of_string v in
3773 setcolumns mode n a b);
3775 sep ();
3776 src#caption "Pixmap cache" 0;
3777 src#int_with_suffix "size (advisory)"
3778 (fun () -> conf.memlimit)
3779 (fun v -> conf.memlimit <- v);
3781 src#caption2 "used"
3782 (fun () -> Printf.sprintf "%s bytes, %d tiles"
3783 (string_with_suffix_of_int state.memused)
3784 (Hashtbl.length state.tilemap)) 1;
3786 sep ();
3787 src#caption "Layout" 0;
3788 src#caption2 "Dimension"
3789 (fun () ->
3790 Printf.sprintf "%dx%d (virtual %dx%d)"
3791 state.winw state.winh
3792 state.w state.maxy)
3794 if conf.debug
3795 then
3796 src#caption2 "Position" (fun () ->
3797 Printf.sprintf "%dx%d" state.x state.y
3799 else
3800 src#caption2 "Position" (fun () -> describe_location ()) 1
3803 sep ();
3804 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3805 "Save these parameters as global defaults at exit"
3806 (fun () -> conf.bedefault)
3807 (fun v -> conf.bedefault <- v)
3810 sep ();
3811 let btos b = if b then "@Ulguillemet" else "@Urguillemet" in
3812 src#bool ~offset:0 ~btos "Extended parameters"
3813 (fun () -> !showextended)
3814 (fun v -> showextended := v; fillsrc prevmode prevuioh);
3815 if !showextended
3816 then (
3817 src#bool "checkers"
3818 (fun () -> conf.checkers)
3819 (fun v -> conf.checkers <- v; setcheckers v);
3820 src#bool "update cursor"
3821 (fun () -> conf.updatecurs)
3822 (fun v -> conf.updatecurs <- v);
3823 src#bool "scroll-bar on the left"
3824 (fun () -> conf.leftscroll)
3825 (fun v -> conf.leftscroll <- v);
3826 src#bool "verbose"
3827 (fun () -> conf.verbose)
3828 (fun v -> conf.verbose <- v);
3829 src#bool "invert colors"
3830 (fun () -> conf.invert)
3831 (fun v -> conf.invert <- v);
3832 src#bool "max fit"
3833 (fun () -> conf.maxhfit)
3834 (fun v -> conf.maxhfit <- v);
3835 src#bool "pax mode"
3836 (fun () -> conf.pax != None)
3837 (fun v ->
3838 if v
3839 then conf.pax <- Some (ref (now (), 0, 0))
3840 else conf.pax <- None);
3841 src#string "uri launcher"
3842 (fun () -> conf.urilauncher)
3843 (fun v -> conf.urilauncher <- v);
3844 src#string "path launcher"
3845 (fun () -> conf.pathlauncher)
3846 (fun v -> conf.pathlauncher <- v);
3847 src#string "tile size"
3848 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3849 (fun v ->
3851 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
3852 conf.tilew <- max 64 w;
3853 conf.tileh <- max 64 h;
3854 flushtiles ();
3855 with exn ->
3856 state.text <- Printf.sprintf "bad tile size `%s': %s"
3857 v @@ exntos exn
3859 src#int "texture count"
3860 (fun () -> conf.texcount)
3861 (fun v ->
3862 if realloctexts v
3863 then conf.texcount <- v
3864 else impmsg "failed to set texture count please retry later"
3866 src#int "slice height"
3867 (fun () -> conf.sliceheight)
3868 (fun v ->
3869 conf.sliceheight <- v;
3870 wcmd "sliceh %d" conf.sliceheight;
3872 src#int "anti-aliasing level"
3873 (fun () -> conf.aalevel)
3874 (fun v ->
3875 conf.aalevel <- bound v 0 8;
3876 state.anchor <- getanchor ();
3877 opendoc state.path state.password;
3879 src#string "page scroll scaling factor"
3880 (fun () -> string_of_float conf.pgscale)
3881 (fun v ->
3883 let s = float_of_string v in
3884 conf.pgscale <- s
3885 with exn ->
3886 state.text <- Printf.sprintf
3887 "bad page scroll scaling factor `%s': %s" v @@ exntos exn
3890 src#int "ui font size"
3891 (fun () -> fstate.fontsize)
3892 (fun v -> setfontsize (bound v 5 100));
3893 src#int "hint font size"
3894 (fun () -> conf.hfsize)
3895 (fun v -> conf.hfsize <- bound v 5 100);
3896 colorp "background color"
3897 (fun () -> conf.bgcolor)
3898 (fun v -> conf.bgcolor <- v; setbgcol v);
3899 src#bool "crop hack"
3900 (fun () -> conf.crophack)
3901 (fun v -> conf.crophack <- v);
3902 src#string "trim fuzz"
3903 (fun () -> irect_to_string conf.trimfuzz)
3904 (fun v ->
3906 conf.trimfuzz <- irect_of_string v;
3907 if conf.trimmargins
3908 then settrim true conf.trimfuzz;
3909 with exn ->
3910 state.text <- Printf.sprintf "bad irect `%s': %s" v @@ exntos exn
3912 src#string "throttle"
3913 (fun () ->
3914 match conf.maxwait with
3915 | None -> "show place holder if page is not ready"
3916 | Some time ->
3917 if time = infinity
3918 then "wait for page to fully render"
3919 else
3920 "wait " ^ string_of_float time
3921 ^ " seconds before showing placeholder"
3923 (fun v ->
3925 let f = float_of_string v in
3926 if f <= 0.0
3927 then conf.maxwait <- None
3928 else conf.maxwait <- Some f
3929 with exn ->
3930 state.text <- Printf.sprintf "bad time `%s': %s" v @@ exntos exn
3932 src#string "ghyll scroll"
3933 (fun () ->
3934 match conf.ghyllscroll with
3935 | None -> E.s
3936 | Some nab -> ghyllscroll_to_string nab
3938 (fun v ->
3939 try conf.ghyllscroll <- ghyllscroll_of_string v
3940 with
3941 | Failure msg ->
3942 state.text <- Printf.sprintf "bad ghyll `%s': %s" v msg
3943 | exn ->
3944 state.text <- Printf.sprintf "bad ghyll `%s': %s" v @@ exntos exn
3946 src#string "selection command"
3947 (fun () -> conf.selcmd)
3948 (fun v -> conf.selcmd <- v);
3949 src#string "synctex command"
3950 (fun () -> conf.stcmd)
3951 (fun v -> conf.stcmd <- v);
3952 src#string "pax command"
3953 (fun () -> conf.paxcmd)
3954 (fun v -> conf.paxcmd <- v);
3955 src#string "ask password command"
3956 (fun () -> conf.passcmd)
3957 (fun v -> conf.passcmd <- v);
3958 src#string "save path command"
3959 (fun () -> conf.savecmd)
3960 (fun v -> conf.savecmd <- v);
3961 src#colorspace "color space"
3962 (fun () -> CSTE.to_string conf.colorspace)
3963 (fun v ->
3964 conf.colorspace <- CSTE.of_int v;
3965 wcmd "cs %d" v;
3966 load state.layout;
3968 src#paxmark "pax mark method"
3969 (fun () -> MTE.to_string conf.paxmark)
3970 (fun v -> conf.paxmark <- MTE.of_int v);
3971 if bousable () && !opengl_has_pbo
3972 then
3973 src#bool "use PBO"
3974 (fun () -> conf.usepbo)
3975 (fun v -> conf.usepbo <- v);
3976 src#bool "mouse wheel scrolls pages"
3977 (fun () -> conf.wheelbypage)
3978 (fun v -> conf.wheelbypage <- v);
3979 src#bool "open remote links in a new instance"
3980 (fun () -> conf.riani)
3981 (fun v -> conf.riani <- v);
3982 src#bool "edit annotations inline"
3983 (fun () -> conf.annotinline)
3984 (fun v -> conf.annotinline <- v);
3985 src#bool "coarse positioning in presentation mode"
3986 (fun () -> conf.coarseprespos)
3987 (fun v -> conf.coarseprespos <- v);
3990 sep ();
3991 src#caption "Document" 0;
3992 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
3993 src#caption2 "Pages"
3994 (fun () -> string_of_int state.pagecount) 1;
3995 src#caption2 "Dimensions"
3996 (fun () -> string_of_int (List.length state.pdims)) 1;
3997 if conf.trimmargins
3998 then (
3999 sep ();
4000 src#caption "Trimmed margins" 0;
4001 src#caption2 "Dimensions"
4002 (fun () -> string_of_int (List.length state.pdims)) 1;
4005 sep ();
4006 src#caption "OpenGL" 0;
4007 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4008 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4010 sep ();
4011 src#caption "Location" 0;
4012 if nonemptystr state.origin
4013 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
4014 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
4016 src#reset prevmode prevuioh;
4018 fun () ->
4019 state.text <- E.s;
4020 resetmstate ();
4021 let prevmode = state.mode
4022 and prevuioh = state.uioh in
4023 fillsrc prevmode prevuioh;
4024 let source = (src :> lvsource) in
4025 let modehash = findkeyhash conf "info" in
4026 state.uioh <- coe (object (self)
4027 inherit listview ~zebra:false ~helpmode:false
4028 ~source ~trusted:true ~modehash as super
4029 val mutable m_prevmemused = 0
4030 method! infochanged = function
4031 | Memused ->
4032 if m_prevmemused != state.memused
4033 then (
4034 m_prevmemused <- state.memused;
4035 G.postRedisplay "memusedchanged";
4037 | Pdim -> G.postRedisplay "pdimchanged"
4038 | Docinfo -> fillsrc prevmode prevuioh
4040 method! key key mask =
4041 if not (Wsi.withctrl mask)
4042 then
4043 match key with
4044 | @left | @kpleft -> coe (self#updownlevel ~-1)
4045 | @right | @kpright -> coe (self#updownlevel 1)
4046 | _ -> super#key key mask
4047 else super#key key mask
4048 end);
4049 G.postRedisplay "info";
4052 let enterhelpmode =
4053 let source =
4054 (object
4055 inherit lvsourcebase
4056 method getitemcount = Array.length state.help
4057 method getitem n =
4058 let s, l, _ = state.help.(n) in
4059 (s, l)
4061 method exit ~uioh ~cancel ~active ~first ~pan =
4062 let optuioh =
4063 if not cancel
4064 then (
4065 match state.help.(active) with
4066 | _, _, Action f -> Some (f uioh)
4067 | _, _, Noaction -> Some uioh
4069 else None
4071 m_active <- active;
4072 m_first <- first;
4073 m_pan <- pan;
4074 optuioh
4076 method hasaction n =
4077 match state.help.(n) with
4078 | _, _, Action _ -> true
4079 | _, _, Noaction -> false
4081 initializer
4082 m_active <- -1
4083 end)
4084 in fun () ->
4085 let modehash = findkeyhash conf "help" in
4086 resetmstate ();
4087 state.uioh <- coe (new listview
4088 ~zebra:false ~helpmode:true
4089 ~source ~trusted:true ~modehash);
4090 G.postRedisplay "help";
4093 let entermsgsmode =
4094 let msgsource =
4095 (object
4096 inherit lvsourcebase
4097 val mutable m_items = E.a
4099 method getitemcount = 1 + Array.length m_items
4101 method getitem n =
4102 if n = 0
4103 then "[Clear]", 0
4104 else m_items.(n-1), 0
4106 method exit ~uioh ~cancel ~active ~first ~pan =
4107 ignore uioh;
4108 if not cancel
4109 then (
4110 if active = 0
4111 then Buffer.clear state.errmsgs;
4113 m_active <- active;
4114 m_first <- first;
4115 m_pan <- pan;
4116 None
4118 method hasaction n =
4119 n = 0
4121 method reset =
4122 state.newerrmsgs <- false;
4123 let l = Str.split newlinere (Buffer.contents state.errmsgs) in
4124 m_items <- Array.of_list l
4126 initializer
4127 m_active <- 0
4128 end)
4129 in fun () ->
4130 state.text <- E.s;
4131 resetmstate ();
4132 msgsource#reset;
4133 let source = (msgsource :> lvsource) in
4134 let modehash = findkeyhash conf "listview" in
4135 state.uioh <- coe (object
4136 inherit listview ~zebra:false ~helpmode:false
4137 ~source ~trusted:false ~modehash as super
4138 method! display =
4139 if state.newerrmsgs
4140 then msgsource#reset;
4141 super#display
4142 end);
4143 G.postRedisplay "msgs";
4146 let getusertext s =
4147 let editor = getenvwithdef "EDITOR" E.s in
4148 if emptystr editor
4149 then E.s
4150 else
4151 let tmppath = Filename.temp_file "llpp" "note" in
4152 if nonemptystr s
4153 then (
4154 let oc = open_out tmppath in
4155 output_string oc s;
4156 close_out oc;
4158 let execstr = editor ^ " " ^ tmppath in
4159 let s =
4160 match spawn execstr [] with
4161 | (exception exn) ->
4162 impmsg "spawn(%S) failed: %s" execstr @@ exntos exn;
4164 | pid ->
4165 match Unix.waitpid [] pid with
4166 | (exception exn) ->
4167 impmsg "waitpid(%d) failed: %s" pid @@ exntos exn;
4169 | (_pid, status) ->
4170 match status with
4171 | Unix.WEXITED 0 -> filecontents tmppath
4172 | Unix.WEXITED n ->
4173 impmsg "editor process(%s) exited abnormally: %d" execstr n;
4175 | Unix.WSIGNALED n ->
4176 impmsg "editor process(%s) was killed by signal %d" execstr n;
4178 | Unix.WSTOPPED n ->
4179 impmsg "editor(%s) process was stopped by signal %d" execstr n;
4182 match Unix.unlink tmppath with
4183 | (exception exn) ->
4184 impmsg "failed to ulink %S: %s" tmppath @@ exntos exn;
4186 | () -> s
4189 let enterannotmode opaque slinkindex =
4190 let msgsource =
4191 (object
4192 inherit lvsourcebase
4193 val mutable m_text = E.s
4194 val mutable m_items = E.a
4196 method getitemcount = Array.length m_items
4198 method getitem n =
4199 let label, _func = m_items.(n) in
4200 label, 0
4202 method exit ~uioh ~cancel ~active ~first ~pan =
4203 ignore (uioh, first, pan);
4204 if not cancel
4205 then (
4206 let _label, func = m_items.(active) in
4207 func ()
4209 None
4211 method hasaction n = nonemptystr @@ fst m_items.(n)
4213 method reset s =
4214 let rec split accu b i =
4215 let p = b+i in
4216 if p = String.length s
4217 then (String.sub s b (p-b), unit) :: accu
4218 else
4219 if (i > 70 && s.[p] = ' ') || s.[p] = '\r' || s.[p] = '\n'
4220 then
4221 let ss = if i = 0 then E.s else String.sub s b i in
4222 split ((ss, unit)::accu) (p+1) 0
4223 else
4224 split accu b (i+1)
4226 let cleanup () =
4227 wcmd "freepage %s" (~> opaque);
4228 let keys =
4229 Hashtbl.fold (fun key opaque' accu ->
4230 if opaque' = opaque'
4231 then key :: accu else accu) state.pagemap []
4233 List.iter (Hashtbl.remove state.pagemap) keys;
4234 flushtiles ();
4235 gotoxy state.x state.y
4237 let dele () =
4238 delannot opaque slinkindex;
4239 cleanup ();
4241 let edit inline () =
4242 let update s =
4243 if emptystr s
4244 then dele ()
4245 else (
4246 modannot opaque slinkindex s;
4247 cleanup ();
4250 if inline
4251 then
4252 let mode = state.mode in
4253 state.mode <-
4254 Textentry (
4255 ("annotation: ", m_text, None, textentry, update, true),
4256 fun _ -> state.mode <- mode);
4257 state.text <- E.s;
4258 enttext ();
4259 else
4260 let s = getusertext m_text in
4261 update s
4263 m_text <- s;
4264 m_items <-
4265 ( "[Copy]", fun () -> selstring m_text)
4266 :: ("[Delete]", dele)
4267 :: ("[Edit]", edit conf.annotinline)
4268 :: (E.s, unit)
4269 :: split [] 0 0 |> List.rev |> Array.of_list
4271 initializer
4272 m_active <- 0
4273 end)
4275 state.text <- E.s;
4276 let s = getannotcontents opaque slinkindex in
4277 resetmstate ();
4278 msgsource#reset s;
4279 let source = (msgsource :> lvsource) in
4280 let modehash = findkeyhash conf "listview" in
4281 state.uioh <- coe (object
4282 inherit listview ~zebra:false ~helpmode:false
4283 ~source ~trusted:false ~modehash
4284 end);
4285 G.postRedisplay "enterannotmode";
4288 let gotounder under =
4289 let getpath filename =
4290 let path =
4291 if nonemptystr filename
4292 then
4293 if Filename.is_relative filename
4294 then
4295 let dir = Filename.dirname state.path in
4296 let dir =
4297 if Filename.is_implicit dir
4298 then Filename.concat (Sys.getcwd ()) dir
4299 else dir
4301 Filename.concat dir filename
4302 else filename
4303 else E.s
4305 if Sys.file_exists path
4306 then path
4307 else E.s
4309 match under with
4310 | Ulinkgoto (pageno, top) ->
4311 if pageno >= 0
4312 then (
4313 addnav ();
4314 let top =
4315 if conf.presentation && conf.coarseprespos
4316 then 0
4317 else top
4319 gotopage1 pageno top;
4322 | Ulinkuri s -> gotouri s
4324 | Uremote (filename, pageno) ->
4325 let path = getpath filename in
4326 if nonemptystr path
4327 then (
4328 if conf.riani
4329 then
4330 let command = Printf.sprintf "%s -page %d %S" !selfexec pageno path in
4331 match spawn command [] with
4332 | _pid -> ()
4333 | (exception exn) ->
4334 dolog "failed to execute `%s': %s" command @@ exntos exn
4335 else
4336 let anchor = getanchor () in
4337 let ranchor = state.path, state.password, anchor, state.origin in
4338 state.origin <- E.s;
4339 state.anchor <- (pageno, 0.0, 0.0);
4340 state.ranchors <- ranchor :: state.ranchors;
4341 opendoc path E.s;
4343 else impmsg "cannot find %s" filename
4345 | Uremotedest (filename, destname) ->
4346 let path = getpath filename in
4347 if nonemptystr path
4348 then (
4349 if conf.riani
4350 then
4351 let command = !selfexec ^ " " ^ path ^ " -dest " ^ destname in
4352 match spawn command [] with
4353 | (exception exn) ->
4354 dolog "failed to execute `%s': %s" command @@ exntos exn
4355 | _pid -> ()
4356 else
4357 let anchor = getanchor () in
4358 let ranchor = state.path, state.password, anchor, state.origin in
4359 state.origin <- E.s;
4360 state.nameddest <- destname;
4361 state.ranchors <- ranchor :: state.ranchors;
4362 opendoc path E.s;
4364 else impmsg "cannot find %s" filename
4366 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4367 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
4370 let gotooutline (_, _, kind) =
4371 match kind with
4372 | Onone -> ()
4373 | Oanchor anchor ->
4374 let (pageno, y, _) = anchor in
4375 let y = getanchory
4376 (if conf.presentation then (pageno, y, 1.0) else anchor)
4378 addnav ();
4379 gotoghyll y
4380 | Ouri uri -> gotounder (Ulinkuri uri)
4381 | Olaunch cmd -> gotounder (Ulaunch cmd)
4382 | Oremote remote -> gotounder (Uremote remote)
4383 | Ohistory hist -> gotohist hist
4384 | Oremotedest remotedest -> gotounder (Uremotedest remotedest)
4387 class outlinesoucebase fetchoutlines = object (self)
4388 inherit lvsourcebase
4389 val mutable m_items = E.a
4390 val mutable m_minfo = E.a
4391 val mutable m_orig_items = E.a
4392 val mutable m_orig_minfo = E.a
4393 val mutable m_narrow_patterns = []
4394 val mutable m_gen = -1
4396 method getitemcount = Array.length m_items
4398 method getitem n =
4399 let s, n, _ = m_items.(n) in
4400 (s, n+0)
4402 method exit ~(uioh:uioh) ~cancel ~active ~(first:int) ~pan :
4403 uioh option =
4404 ignore (uioh, first);
4405 let items, minfo =
4406 if m_narrow_patterns = []
4407 then m_orig_items, m_orig_minfo
4408 else m_items, m_minfo
4410 m_pan <- pan;
4411 if not cancel
4412 then (
4413 m_items <- items;
4414 m_minfo <- minfo;
4415 gotooutline m_items.(active);
4417 else (
4418 m_items <- items;
4419 m_minfo <- minfo;
4421 None
4423 method hasaction (_:int) = true
4425 method greetmsg =
4426 if Array.length m_items != Array.length m_orig_items
4427 then
4428 let s =
4429 match m_narrow_patterns with
4430 | one :: [] -> one
4431 | many -> String.concat "@Uellipsis" (List.rev many)
4433 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
4434 else E.s
4436 method statestr =
4437 match m_narrow_patterns with
4438 | [] -> E.s
4439 | one :: [] -> one
4440 | head :: _ -> "@Uellipsis" ^ head
4442 method narrow pattern =
4443 match Str.regexp_case_fold pattern with
4444 | (exception _) -> ()
4445 | re ->
4446 let rec loop accu minfo n =
4447 if n = -1
4448 then (
4449 m_items <- Array.of_list accu;
4450 m_minfo <- Array.of_list minfo;
4452 else
4453 let (s, _, _) as o = m_items.(n) in
4454 let accu, minfo =
4455 match Str.search_forward re s 0 with
4456 | (exception Not_found) -> accu, minfo
4457 | first -> o :: accu, (first, Str.match_end ()) :: minfo
4459 loop accu minfo (n-1)
4461 loop [] [] (Array.length m_items - 1)
4463 method! getminfo = m_minfo
4465 method denarrow =
4466 m_orig_items <- fetchoutlines ();
4467 m_minfo <- m_orig_minfo;
4468 m_items <- m_orig_items
4470 method add_narrow_pattern pattern =
4471 m_narrow_patterns <- pattern :: m_narrow_patterns
4473 method del_narrow_pattern =
4474 match m_narrow_patterns with
4475 | _ :: rest -> m_narrow_patterns <- rest
4476 | [] -> ()
4478 method renarrow =
4479 self#denarrow;
4480 match m_narrow_patterns with
4481 | pattern :: [] -> self#narrow pattern; pattern
4482 | list ->
4483 List.fold_left (fun accu pattern ->
4484 self#narrow pattern;
4485 pattern ^ "@Uellipsis" ^ accu) E.s list
4487 method calcactive (_:anchor) = 0
4489 method reset anchor items =
4490 if state.gen != m_gen
4491 then (
4492 m_orig_items <- items;
4493 m_items <- items;
4494 m_narrow_patterns <- [];
4495 m_minfo <- E.a;
4496 m_orig_minfo <- E.a;
4497 m_gen <- state.gen;
4499 else (
4500 if items != m_orig_items
4501 then (
4502 m_orig_items <- items;
4503 if m_narrow_patterns == []
4504 then m_items <- items;
4507 let active = self#calcactive anchor in
4508 m_active <- active;
4509 m_first <- firstof m_first active
4513 let outlinesource fetchoutlines =
4514 (object
4515 inherit outlinesoucebase fetchoutlines
4516 method! calcactive anchor =
4517 let rely = getanchory anchor in
4518 let rec loop n best bestd =
4519 if n = Array.length m_items
4520 then best
4521 else
4522 let _, _, kind = m_items.(n) in
4523 match kind with
4524 | Oanchor anchor ->
4525 let orely = getanchory anchor in
4526 let d = abs (orely - rely) in
4527 if d < bestd
4528 then loop (n+1) n d
4529 else loop (n+1) best bestd
4530 | Onone | Oremote _ | Olaunch _
4531 | Oremotedest _ | Ouri _ | Ohistory _ ->
4532 loop (n+1) best bestd
4534 loop 0 ~-1 max_int
4535 end)
4538 let enteroutlinemode, enterbookmarkmode, enterhistmode =
4539 let mkselector sourcetype =
4540 let fetchoutlines () =
4541 match sourcetype with
4542 | `bookmarks -> Array.of_list state.bookmarks
4543 | `outlines -> state.outlines
4544 | `history -> genhistoutlines ()
4546 let source =
4547 if sourcetype = `history
4548 then new outlinesoucebase fetchoutlines
4549 else outlinesource fetchoutlines
4551 fun errmsg ->
4552 let outlines = fetchoutlines () in
4553 if Array.length outlines = 0
4554 then (
4555 showtext ' ' errmsg;
4557 else (
4558 resetmstate ();
4559 Wsi.setcursor Wsi.CURSOR_INHERIT;
4560 let anchor = getanchor () in
4561 source#reset anchor outlines;
4562 state.text <- source#greetmsg;
4563 state.uioh <-
4564 coe (new outlinelistview ~zebra:(sourcetype=`history) ~source);
4565 G.postRedisplay "enter selector";
4568 let mkenter sourcetype errmsg =
4569 let enter = mkselector sourcetype in
4570 fun () -> enter errmsg
4572 (**)mkenter `outlines "document has no outline"
4573 , mkenter `bookmarks "document has no bookmarks (yet)"
4574 , mkenter `history "history is empty"
4577 let quickbookmark ?title () =
4578 match state.layout with
4579 | [] -> ()
4580 | l :: _ ->
4581 let title =
4582 match title with
4583 | None ->
4584 let tm = Unix.localtime (now ()) in
4585 Printf.sprintf
4586 "Quick (page %d) (bookmarked at %02d/%02d/%d %02d:%02d)"
4587 (l.pageno+1)
4588 tm.Unix.tm_mday
4589 (tm.Unix.tm_mon+1)
4590 (tm.Unix.tm_year + 1900)
4591 tm.Unix.tm_hour
4592 tm.Unix.tm_min
4593 | Some title -> title
4595 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4598 let setautoscrollspeed step goingdown =
4599 let incr = max 1 ((abs step) / 2) in
4600 let incr = if goingdown then incr else -incr in
4601 let astep = boundastep state.winh (step + incr) in
4602 state.autoscroll <- Some astep;
4605 let canpan () =
4606 match conf.columns with
4607 | Csplit _ -> true
4608 | Csingle _ | Cmulti _ -> state.x != 0 || conf.zoom > 1.0
4611 let panbound x = bound x (-state.w) (wadjsb () + state.winw);;
4613 let existsinrow pageno (columns, coverA, coverB) p =
4614 let last = ((pageno - coverA) mod columns) + columns in
4615 let rec any = function
4616 | [] -> false
4617 | l :: rest ->
4618 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4619 then p l
4620 else (
4621 if not (p l)
4622 then (if l.pageno = last then false else any rest)
4623 else true
4626 any state.layout
4629 let nextpage () =
4630 match state.layout with
4631 | [] ->
4632 let pageno = page_of_y state.y in
4633 gotoghyll (getpagey (pageno+1))
4634 | l :: rest ->
4635 match conf.columns with
4636 | Csingle _ ->
4637 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4638 then
4639 let y = clamp (pgscale state.winh) in
4640 gotoghyll y
4641 else
4642 let pageno = min (l.pageno+1) (state.pagecount-1) in
4643 gotoghyll (getpagey pageno)
4644 | Cmulti ((c, _, _) as cl, _) ->
4645 if conf.presentation
4646 && (existsinrow l.pageno cl
4647 (fun l -> l.pageh > l.pagey + l.pagevh))
4648 then
4649 let y = clamp (pgscale state.winh) in
4650 gotoghyll y
4651 else
4652 let pageno = min (l.pageno+c) (state.pagecount-1) in
4653 gotoghyll (getpagey pageno)
4654 | Csplit (n, _) ->
4655 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4656 then
4657 let pagey, pageh = getpageyh l.pageno in
4658 let pagey = pagey + pageh * l.pagecol in
4659 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4660 gotoghyll (pagey + pageh + ips)
4663 let prevpage () =
4664 match state.layout with
4665 | [] ->
4666 let pageno = page_of_y state.y in
4667 gotoghyll (getpagey (pageno-1))
4668 | l :: _ ->
4669 match conf.columns with
4670 | Csingle _ ->
4671 if conf.presentation && l.pagey != 0
4672 then
4673 gotoghyll (clamp (pgscale ~-(state.winh)))
4674 else
4675 let pageno = max 0 (l.pageno-1) in
4676 gotoghyll (getpagey pageno)
4677 | Cmulti ((c, _, coverB) as cl, _) ->
4678 if conf.presentation &&
4679 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4680 then
4681 gotoghyll (clamp (pgscale ~-(state.winh)))
4682 else
4683 let decr =
4684 if l.pageno = state.pagecount - coverB
4685 then 1
4686 else c
4688 let pageno = max 0 (l.pageno-decr) in
4689 gotoghyll (getpagey pageno)
4690 | Csplit (n, _) ->
4691 let y =
4692 if l.pagecol = 0
4693 then
4694 if l.pageno = 0
4695 then l.pagey
4696 else
4697 let pageno = max 0 (l.pageno-1) in
4698 let pagey, pageh = getpageyh pageno in
4699 pagey + (n-1)*pageh
4700 else
4701 let pagey, pageh = getpageyh l.pageno in
4702 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4704 gotoghyll y
4707 let save () =
4708 if emptystr conf.savecmd
4709 then error "don't know where to save modified document"
4710 else
4711 let savecmd = Str.global_replace percentsre state.path conf.savecmd in
4712 let path =
4713 getcmdoutput
4714 (fun s -> error "failed to obtain path to the saved copy: %s" s)
4715 savecmd
4717 if nonemptystr path
4718 then
4719 let tmp = path ^ ".tmp" in
4720 savedoc tmp;
4721 Unix.rename tmp path;
4724 let viewkeyboard key mask =
4725 let enttext te =
4726 let mode = state.mode in
4727 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4728 state.text <- E.s;
4729 enttext ();
4730 G.postRedisplay "view:enttext"
4732 let ctrl = Wsi.withctrl mask in
4733 let key = Wsi.keypadtodigitkey key in
4734 match key with
4735 | @Q -> exit 0
4737 | @W ->
4738 if hasunsavedchanges ()
4739 then save ()
4741 | @insert ->
4742 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4743 then (
4744 state.mode <- LinkNav (Ltgendir 0);
4745 gotoxy state.x state.y;
4747 else impmsg "keyboard link navigation does not work under rotation"
4749 | @escape | @q ->
4750 begin match state.mstate with
4751 | Mzoomrect _ ->
4752 resetmstate ();
4753 G.postRedisplay "kill rect";
4754 | Msel _
4755 | Mpan _
4756 | Mscrolly | Mscrollx
4757 | Mzoom _
4758 | Mnone ->
4759 begin match state.mode with
4760 | LinkNav _ ->
4761 state.mode <- View;
4762 G.postRedisplay "esc leave linknav"
4763 | Birdseye _
4764 | Textentry _
4765 | View ->
4766 match state.ranchors with
4767 | [] -> raise Quit
4768 | (path, password, anchor, origin) :: rest ->
4769 state.ranchors <- rest;
4770 state.anchor <- anchor;
4771 state.origin <- origin;
4772 state.nameddest <- E.s;
4773 opendoc path password
4774 end;
4775 end;
4777 | @backspace ->
4778 gotoghyll (getnav ~-1)
4780 | @o ->
4781 enteroutlinemode ()
4783 | @H ->
4784 enterhistmode ()
4786 | @u ->
4787 state.rects <- [];
4788 state.text <- E.s;
4789 Hashtbl.iter (fun _ opaque ->
4790 clearmark opaque;
4791 Hashtbl.clear state.prects) state.pagemap;
4792 G.postRedisplay "dehighlight";
4794 | @slash | @question ->
4795 let ondone isforw s =
4796 cbput state.hists.pat s;
4797 state.searchpattern <- s;
4798 search s isforw
4800 let s = String.make 1 (Char.chr key) in
4801 enttext (s, E.s, Some (onhist state.hists.pat),
4802 textentry, ondone (key = @slash), true)
4804 | @plus | @kpplus | @equals when ctrl ->
4805 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4806 pivotzoom (conf.zoom +. incr)
4808 | @plus | @kpplus ->
4809 let ondone s =
4810 let n =
4811 try int_of_string s with exn ->
4812 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn;
4813 max_int
4815 if n != max_int
4816 then (
4817 conf.pagebias <- n;
4818 state.text <- "page bias is now " ^ string_of_int n;
4821 enttext ("page bias: ", E.s, None, intentry, ondone, true)
4823 | @minus | @kpminus when ctrl ->
4824 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4825 pivotzoom (max 0.01 (conf.zoom -. decr))
4827 | @minus | @kpminus ->
4828 let ondone msg = state.text <- msg in
4829 enttext (
4830 "option [acfhilpstvxACFPRSZTISM]: ", E.s, None,
4831 optentry state.mode, ondone, true
4834 | @0 when ctrl ->
4835 if conf.zoom = 1.0
4836 then gotoxy 0 state.y
4837 else setzoom 1.0
4839 | (@1 | @2) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
4840 let cols =
4841 match conf.columns with
4842 | Csingle _ | Cmulti _ -> 1
4843 | Csplit (n, _) -> n
4845 let h = state.winh -
4846 conf.interpagespace lsl (if conf.presentation then 1 else 0)
4848 let zoom = zoomforh state.winw h (vscrollw ()) cols in
4849 if zoom > 0.0 && (key = @2 || zoom < 1.0)
4850 then setzoom zoom
4852 | @3 when ctrl ->
4853 let fm =
4854 match conf.fitmodel with
4855 | FitWidth -> FitProportional
4856 | FitProportional -> FitPage
4857 | FitPage -> FitWidth
4859 state.text <- "fit model: " ^ FMTE.to_string fm;
4860 reqlayout conf.angle fm
4862 | @4 when ctrl -> (* ctrl-4 *)
4863 let zoom = getmaxw () /. float state.winw in
4864 if zoom > 0.0 then setzoom zoom
4866 | @F9 ->
4867 togglebirdseye ()
4869 | @9 when ctrl ->
4870 togglebirdseye ()
4872 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4873 when not ctrl -> (* 0..9 *)
4874 let ondone s =
4875 let n =
4876 try int_of_string s with exn ->
4877 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exn;
4880 if n >= 0
4881 then (
4882 addnav ();
4883 cbput state.hists.pag (string_of_int n);
4884 gotopage1 (n + conf.pagebias - 1) 0;
4887 let pageentry text key =
4888 match Char.unsafe_chr key with
4889 | 'g' -> TEdone text
4890 | _ -> intentry text key
4892 let text = String.make 1 (Char.chr key) in
4893 enttext (":", text, Some (onhist state.hists.pag),
4894 pageentry, ondone, true)
4896 | @b ->
4897 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
4898 reshape state.winw state.winh;
4900 | @B ->
4901 state.bzoom <- not state.bzoom;
4902 state.rects <- [];
4903 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
4905 | @l ->
4906 conf.hlinks <- not conf.hlinks;
4907 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4908 G.postRedisplay "toggle highlightlinks";
4910 | @F ->
4911 if conf.angle mod 360 = 0
4912 then (
4913 state.glinks <- true;
4914 let mode = state.mode in
4915 state.mode <-
4916 Textentry (
4917 (":", E.s, None, linknentry, linknact gotounder, false),
4918 (fun _ ->
4919 state.glinks <- false;
4920 state.mode <- mode)
4922 state.text <- E.s;
4923 G.postRedisplay "view:linkent(F)"
4925 else impmsg "hint mode does not work under rotation"
4927 | @y ->
4928 state.glinks <- true;
4929 let mode = state.mode in
4930 state.mode <- Textentry (
4932 ":", E.s, None, linknentry, linknact (fun under ->
4933 selstring (undertext under);
4934 ), false
4936 fun _ ->
4937 state.glinks <- false;
4938 state.mode <- mode
4940 state.text <- E.s;
4941 G.postRedisplay "view:linkent"
4943 | @a ->
4944 begin match state.autoscroll with
4945 | Some step ->
4946 conf.autoscrollstep <- step;
4947 state.autoscroll <- None
4948 | None ->
4949 if conf.autoscrollstep = 0
4950 then state.autoscroll <- Some 1
4951 else state.autoscroll <- Some conf.autoscrollstep
4954 | @p when ctrl ->
4955 launchpath () (* XXX where do error messages go? *)
4957 | @P ->
4958 setpresentationmode (not conf.presentation);
4959 showtext ' ' ("presentation mode " ^
4960 if conf.presentation then "on" else "off");
4962 | @f ->
4963 if List.mem Wsi.Fullscreen state.winstate
4964 then Wsi.reshape conf.cwinw conf.cwinh
4965 else Wsi.fullscreen ()
4967 | @p | @N ->
4968 search state.searchpattern false
4970 | @n | @F3 ->
4971 search state.searchpattern true
4973 | @t ->
4974 begin match state.layout with
4975 | [] -> ()
4976 | l :: _ ->
4977 gotoghyll (getpagey l.pageno)
4980 | @space ->
4981 nextpage ()
4983 | @delete | @kpdelete -> (* delete *)
4984 prevpage ()
4986 | @equals ->
4987 showtext ' ' (describe_location ());
4989 | @w ->
4990 begin match state.layout with
4991 | [] -> ()
4992 | l :: _ ->
4993 Wsi.reshape (l.pagew + vscrollw ()) l.pageh;
4994 G.postRedisplay "w"
4997 | @apos ->
4998 enterbookmarkmode ()
5000 | @h | @F1 ->
5001 enterhelpmode ()
5003 | @i ->
5004 enterinfomode ()
5006 | @e when Buffer.length state.errmsgs > 0 ->
5007 entermsgsmode ()
5009 | @m ->
5010 let ondone s =
5011 match state.layout with
5012 | l :: _ ->
5013 if nonemptystr s
5014 then
5015 state.bookmarks <-
5016 (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
5017 | _ -> ()
5019 enttext ("bookmark: ", E.s, None, textentry, ondone, true)
5021 | @tilde ->
5022 quickbookmark ();
5023 showtext ' ' "Quick bookmark added";
5025 | @z ->
5026 begin match state.layout with
5027 | l :: _ ->
5028 let rect = getpdimrect l.pagedimno in
5029 let w, h =
5030 if conf.crophack
5031 then
5032 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5033 truncate (1.2 *. (rect.(3) -. rect.(0))))
5034 else
5035 (truncate (rect.(1) -. rect.(0)),
5036 truncate (rect.(3) -. rect.(0)))
5038 let w = truncate ((float w)*.conf.zoom)
5039 and h = truncate ((float h)*.conf.zoom) in
5040 if w != 0 && h != 0
5041 then (
5042 state.anchor <- getanchor ();
5043 Wsi.reshape (w + vscrollw ()) (h + conf.interpagespace)
5045 G.postRedisplay "z";
5047 | [] -> ()
5050 | @x -> state.roam ()
5052 | @Lt | @Gt ->
5053 reqlayout (conf.angle +
5054 (if key = @Gt then 30 else -30)) conf.fitmodel
5056 | @Lb | @Rb ->
5057 conf.colorscale <-
5058 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5060 G.postRedisplay "brightness";
5062 | @c when state.mode = View ->
5063 if Wsi.withalt mask
5064 then (
5065 if conf.zoom > 1.0
5066 then
5067 let m = (wadjsb () + state.winw - state.w) / 2 in
5068 gotoxy_and_clear_text m state.y
5070 else
5071 let (c, a, b), z =
5072 match state.prevcolumns with
5073 | None -> (1, 0, 0), 1.0
5074 | Some (columns, z) ->
5075 let cab =
5076 match columns with
5077 | Csplit (c, _) -> -c, 0, 0
5078 | Cmulti ((c, a, b), _) -> c, a, b
5079 | Csingle _ -> 1, 0, 0
5081 cab, z
5083 setcolumns View c a b;
5084 setzoom z
5086 | @down | @up when ctrl && Wsi.withshift mask ->
5087 let zoom, x = state.prevzoom in
5088 setzoom zoom;
5089 state.x <- x;
5091 | @k | @up | @kpup ->
5092 begin match state.autoscroll with
5093 | None ->
5094 begin match state.mode with
5095 | Birdseye beye -> upbirdseye 1 beye
5096 | Textentry _
5097 | View
5098 | LinkNav _ ->
5099 if ctrl
5100 then gotoxy_and_clear_text state.x (clamp ~-(state.winh/2))
5101 else (
5102 if not (Wsi.withshift mask) && conf.presentation
5103 then prevpage ()
5104 else gotoghyll1 true (clamp (-conf.scrollstep))
5107 | Some n ->
5108 setautoscrollspeed n false
5111 | @j | @down | @kpdown ->
5112 begin match state.autoscroll with
5113 | None ->
5114 begin match state.mode with
5115 | Birdseye beye -> downbirdseye 1 beye
5116 | Textentry _
5117 | View
5118 | LinkNav _ ->
5119 if ctrl
5120 then gotoxy_and_clear_text state.x (clamp (state.winh/2))
5121 else (
5122 if not (Wsi.withshift mask) && conf.presentation
5123 then nextpage ()
5124 else gotoghyll1 true (clamp (conf.scrollstep))
5127 | Some n ->
5128 setautoscrollspeed n true
5131 | @left | @right | @kpleft | @kpright when not (Wsi.withalt mask) ->
5132 if canpan ()
5133 then
5134 let dx =
5135 if ctrl
5136 then state.winw / 2
5137 else conf.hscrollstep
5139 let dx = if key = @left || key = @kpleft then dx else -dx in
5140 gotoxy_and_clear_text (panbound (state.x + dx)) state.y
5141 else (
5142 state.text <- E.s;
5143 G.postRedisplay "left/right"
5146 | @prior | @kpprior ->
5147 let y =
5148 if ctrl
5149 then
5150 match state.layout with
5151 | [] -> state.y
5152 | l :: _ -> state.y - l.pagey
5153 else
5154 clamp (pgscale (-state.winh))
5156 gotoghyll y
5158 | @next | @kpnext ->
5159 let y =
5160 if ctrl
5161 then
5162 match List.rev state.layout with
5163 | [] -> state.y
5164 | l :: _ -> getpagey l.pageno
5165 else
5166 clamp (pgscale state.winh)
5168 gotoghyll y
5170 | @g | @home | @kphome ->
5171 addnav ();
5172 gotoghyll 0
5173 | @G | @jend | @kpend ->
5174 addnav ();
5175 gotoghyll (clamp state.maxy)
5177 | @right | @kpright when Wsi.withalt mask ->
5178 gotoghyll (getnav 1)
5179 | @left | @kpleft when Wsi.withalt mask ->
5180 gotoghyll (getnav ~-1)
5182 | @r ->
5183 reload ()
5185 | @v when conf.debug ->
5186 state.rects <- [];
5187 List.iter (fun l ->
5188 match getopaque l.pageno with
5189 | None -> ()
5190 | Some opaque ->
5191 let x0, y0, x1, y1 = pagebbox opaque in
5192 let rect = (float x0, float y0,
5193 float x1, float y0,
5194 float x1, float y1,
5195 float x0, float y1) in
5196 debugrect rect;
5197 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
5198 state.rects <- (l.pageno, color, rect) :: state.rects;
5199 ) state.layout;
5200 G.postRedisplay "v";
5202 | @pipe ->
5203 let mode = state.mode in
5204 let cmd = ref E.s in
5205 let onleave = function
5206 | Cancel -> state.mode <- mode
5207 | Confirm ->
5208 List.iter (fun l ->
5209 match getopaque l.pageno with
5210 | Some opaque -> pipesel opaque !cmd
5211 | None -> ()) state.layout;
5212 state.mode <- mode
5214 let ondone s =
5215 cbput state.hists.sel s;
5216 cmd := s
5218 let te =
5219 "| ", !cmd, Some (onhist state.hists.sel), textentry, ondone, true
5221 G.postRedisplay "|";
5222 state.mode <- Textentry (te, onleave);
5224 | _ ->
5225 vlog "huh? %s" (Wsi.keyname key)
5228 let linknavkeyboard key mask linknav =
5229 let getpage pageno =
5230 let rec loop = function
5231 | [] -> None
5232 | l :: _ when l.pageno = pageno -> Some l
5233 | _ :: rest -> loop rest
5234 in loop state.layout
5236 let doexact (pageno, n) =
5237 match getopaque pageno, getpage pageno with
5238 | Some opaque, Some l ->
5239 if key = @enter || key = @kpenter
5240 then
5241 let under = getlink opaque n in
5242 G.postRedisplay "link gotounder";
5243 gotounder under;
5244 state.mode <- View;
5245 else
5246 let opt, dir =
5247 match key with
5248 | @home ->
5249 Some (findlink opaque LDfirst), -1
5251 | @jend ->
5252 Some (findlink opaque LDlast), 1
5254 | @left ->
5255 Some (findlink opaque (LDleft n)), -1
5257 | @right ->
5258 Some (findlink opaque (LDright n)), 1
5260 | @up ->
5261 Some (findlink opaque (LDup n)), -1
5263 | @down ->
5264 Some (findlink opaque (LDdown n)), 1
5266 | _ -> None, 0
5268 let pwl l dir =
5269 begin match findpwl l.pageno dir with
5270 | Pwlnotfound -> ()
5271 | Pwl pageno ->
5272 let notfound dir =
5273 state.mode <- LinkNav (Ltgendir dir);
5274 let y, h = getpageyh pageno in
5275 let y =
5276 if dir < 0
5277 then y + h - state.winh
5278 else y
5280 gotoxy state.x y
5282 begin match getopaque pageno, getpage pageno with
5283 | Some opaque, Some _ ->
5284 let link =
5285 let ld = if dir > 0 then LDfirst else LDlast in
5286 findlink opaque ld
5288 begin match link with
5289 | Lfound m ->
5290 showlinktype (getlink opaque m);
5291 state.mode <- LinkNav (Ltexact (pageno, m));
5292 G.postRedisplay "linknav jpage";
5293 | Lnotfound -> notfound dir
5294 end;
5295 | _ -> notfound dir
5296 end;
5297 end;
5299 begin match opt with
5300 | Some Lnotfound -> pwl l dir;
5301 | Some (Lfound m) ->
5302 if m = n
5303 then pwl l dir
5304 else (
5305 let _, y0, _, y1 = getlinkrect opaque m in
5306 if y0 < l.pagey
5307 then gotopage1 l.pageno y0
5308 else (
5309 let d = fstate.fontsize + 1 in
5310 if y1 - l.pagey > l.pagevh - d
5311 then gotopage1 l.pageno (y1 - state.winh - hscrollh () + d)
5312 else G.postRedisplay "linknav";
5314 showlinktype (getlink opaque m);
5315 state.mode <- LinkNav (Ltexact (l.pageno, m));
5318 | None -> viewkeyboard key mask
5319 end;
5320 | _ -> viewkeyboard key mask
5322 if key = @insert
5323 then (
5324 state.mode <- View;
5325 G.postRedisplay "leave linknav"
5327 else
5328 match linknav with
5329 | Ltgendir _ | Ltnotready _ -> viewkeyboard key mask
5330 | Ltexact exact -> doexact exact
5333 let keyboard key mask =
5334 if (key = @g && Wsi.withctrl mask) && not (istextentry state.mode)
5335 then wcmd "interrupt"
5336 else state.uioh <- state.uioh#key key mask
5339 let birdseyekeyboard key mask
5340 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5341 let incr =
5342 match conf.columns with
5343 | Csingle _ -> 1
5344 | Cmulti ((c, _, _), _) -> c
5345 | Csplit _ -> failwith "bird's eye split mode"
5347 let pgh layout = List.fold_left
5348 (fun m l -> max l.pageh m) state.winh layout in
5349 match key with
5350 | @l when Wsi.withctrl mask ->
5351 let y, h = getpageyh pageno in
5352 let top = (state.winh - h) / 2 in
5353 gotoxy state.x (max 0 (y - top))
5354 | @enter | @kpenter -> leavebirdseye beye false
5355 | @escape -> leavebirdseye beye true
5356 | @up -> upbirdseye incr beye
5357 | @down -> downbirdseye incr beye
5358 | @left -> upbirdseye 1 beye
5359 | @right -> downbirdseye 1 beye
5361 | @prior ->
5362 begin match state.layout with
5363 | l :: _ ->
5364 if l.pagey != 0
5365 then (
5366 state.mode <- Birdseye (
5367 oconf, leftx, l.pageno, hooverpageno, anchor
5369 gotopage1 l.pageno 0;
5371 else (
5372 let layout = layout state.x (state.y-state.winh)
5373 state.winw
5374 (pgh state.layout) in
5375 match layout with
5376 | [] -> gotoxy state.x (clamp (-state.winh))
5377 | l :: _ ->
5378 state.mode <- Birdseye (
5379 oconf, leftx, l.pageno, hooverpageno, anchor
5381 gotopage1 l.pageno 0
5384 | [] -> gotoxy state.x (clamp (-state.winh))
5385 end;
5387 | @next ->
5388 begin match List.rev state.layout with
5389 | l :: _ ->
5390 let layout = layout state.x
5391 (state.y + (pgh state.layout))
5392 state.winw state.winh in
5393 begin match layout with
5394 | [] ->
5395 let incr = l.pageh - l.pagevh in
5396 if incr = 0
5397 then (
5398 state.mode <-
5399 Birdseye (
5400 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5402 G.postRedisplay "birdseye pagedown";
5404 else gotoxy state.x (clamp (incr + conf.interpagespace*2));
5406 | l :: _ ->
5407 state.mode <-
5408 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5409 gotopage1 l.pageno 0;
5412 | [] -> gotoxy state.x (clamp state.winh)
5413 end;
5415 | @home ->
5416 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5417 gotopage1 0 0
5419 | @jend ->
5420 let pageno = state.pagecount - 1 in
5421 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5422 if not (pagevisible state.layout pageno)
5423 then
5424 let h =
5425 match List.rev state.pdims with
5426 | [] -> state.winh
5427 | (_, _, h, _) :: _ -> h
5429 gotoxy
5430 state.x
5431 (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5432 else G.postRedisplay "birdseye end";
5434 | _ -> viewkeyboard key mask
5437 let drawpage l =
5438 let color =
5439 match state.mode with
5440 | Textentry _ -> scalecolor 0.4
5441 | LinkNav _
5442 | View -> scalecolor 1.0
5443 | Birdseye (_, _, pageno, hooverpageno, _) ->
5444 if l.pageno = hooverpageno
5445 then scalecolor 0.9
5446 else (
5447 if l.pageno = pageno
5448 then (
5449 let c = scalecolor 1.0 in
5450 GlDraw.color c;
5451 GlDraw.line_width 3.0;
5452 let dispx = xadjsb () + l.pagedispx in
5453 linerect
5454 (float (dispx-1)) (float (l.pagedispy-1))
5455 (float (dispx+l.pagevw+1))
5456 (float (l.pagedispy+l.pagevh+1))
5458 GlDraw.line_width 1.0;
5461 else scalecolor 0.8
5464 drawtiles l color;
5467 let postdrawpage l linkindexbase =
5468 match getopaque l.pageno with
5469 | Some opaque ->
5470 if tileready l l.pagex l.pagey
5471 then
5472 let x = l.pagedispx - l.pagex + xadjsb ()
5473 and y = l.pagedispy - l.pagey in
5474 let hlmask =
5475 match conf.columns with
5476 | Csingle _ | Cmulti _ ->
5477 (if conf.hlinks then 1 else 0)
5478 + (if state.glinks
5479 && not (isbirdseye state.mode) then 2 else 0)
5480 | Csplit _ -> 0
5482 let s =
5483 match state.mode with
5484 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5485 | Textentry _
5486 | Birdseye _
5487 | View
5488 | LinkNav _ -> E.s
5490 Hashtbl.find_all state.prects l.pageno |>
5491 List.iter (fun vals -> drawprect opaque x y vals);
5492 let n = postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize) in
5493 if n < 0
5494 then (state.redisplay <- true; 0)
5495 else n
5496 else 0
5497 | _ -> 0
5500 let scrollindicator () =
5501 let sbw, ph, sh = state.uioh#scrollph in
5502 let sbh, pw, sw = state.uioh#scrollpw in
5504 let x0,x1,hx0 =
5505 if conf.leftscroll
5506 then (0, sbw, sbw)
5507 else ((state.winw - sbw), state.winw, 0)
5510 GlDraw.color (0.64, 0.64, 0.64);
5511 filledrect (float x0) 0. (float x1) (float state.winh);
5512 filledrect
5513 (float hx0) (float (state.winh - sbh))
5514 (float (hx0 + wadjsb () + state.winw)) (float state.winh)
5516 GlDraw.color (0.0, 0.0, 0.0);
5518 filledrect (float x0) ph (float x1) (ph +. sh);
5519 let pw = pw +. float hx0 in
5520 filledrect pw (float (state.winh - sbh)) (pw +. sw) (float state.winh);
5523 let showsel () =
5524 match state.mstate with
5525 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5528 | Msel ((x0, y0), (x1, y1)) ->
5529 let identify opaque l px py = Some (opaque, l.pageno, px, py) in
5530 let o0,n0,px0,py0 = onppundermouse identify x0 y0 (~< E.s, -1, 0, 0) in
5531 let _o1,n1,px1,py1 = onppundermouse identify x1 y1 (~< E.s, -1, 0, 0) in
5532 if n0 != -1 && n0 = n1 then seltext o0 (px0, py0, px1, py1);
5535 let showrects = function [] -> () | rects ->
5536 Gl.enable `blend;
5537 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5538 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5539 List.iter
5540 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5541 List.iter (fun l ->
5542 if l.pageno = pageno
5543 then (
5544 let dx = float (l.pagedispx - l.pagex) in
5545 let dy = float (l.pagedispy - l.pagey) in
5546 let r, g, b, alpha = c in
5547 GlDraw.color (r, g, b) ~alpha;
5548 filledrect2 (x0+.dx) (y0+.dy)
5549 (x1+.dx) (y1+.dy)
5550 (x3+.dx) (y3+.dy)
5551 (x2+.dx) (y2+.dy);
5553 ) state.layout
5554 ) rects
5556 Gl.disable `blend;
5559 let display () =
5560 begin match conf.columns, state.layout with
5561 | Csingle _, _ :: _ ->
5562 GlDraw.color (scalecolor2 conf.bgcolor);
5563 let y =
5564 List.fold_left (fun y l ->
5565 let x0 = 0 in
5566 let y0 = y in
5567 let x1 = l.pagedispx + xadjsb () in
5568 let y1 = (l.pagedispy + l.pagevh) in
5569 filledrect (float x0) (float y0) (float x1) (float y1);
5570 let x0 = x1 + l.pagevw in
5571 let x1 = state.winw in
5572 filledrect1 (float x0) (float y0) (float x1) (float y1);
5573 if y != l.pagedispy
5574 then (
5575 let x0 = 0
5576 and x1 = state.winw in
5577 let y0 = y
5578 and y1 = l.pagedispy in
5579 filledrect1 (float x0) (float y0) (float x1) (float y1);
5581 l.pagedispy + l.pagevh) 0 state.layout
5583 let x0 = 0
5584 and x1 = state.winw in
5585 let y0 = y
5586 and y1 = state.winh in
5587 filledrect1 (float x0) (float y0) (float x1) (float y1)
5588 | (Cmulti _ | Csplit _), _ | Csingle _, [] ->
5589 GlClear.color (scalecolor2 conf.bgcolor);
5590 GlClear.clear [`color];
5591 end;
5592 List.iter drawpage state.layout;
5593 let rects =
5594 match state.mode with
5595 | LinkNav (Ltexact (pageno, linkno)) ->
5596 begin match getopaque pageno with
5597 | Some opaque ->
5598 let dx = xadjsb () in
5599 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5600 let x0 = x0 + dx and x1 = x1 + dx in
5601 let color = (0.0, 0.0, 0.5, 0.5) in
5602 (pageno, color, (
5603 float x0, float y0,
5604 float x1, float y0,
5605 float x1, float y1,
5606 float x0, float y1)
5607 ) :: state.rects
5608 | None -> state.rects
5610 | LinkNav (Ltgendir _) | LinkNav (Ltnotready _)
5611 | Birdseye _
5612 | Textentry _
5613 | View -> state.rects
5615 showrects rects;
5616 let rec postloop linkindexbase = function
5617 | l :: rest ->
5618 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5619 postloop linkindexbase rest
5620 | [] -> ()
5622 showsel ();
5623 postloop 0 state.layout;
5624 state.uioh#display;
5625 begin match state.mstate with
5626 | Mzoomrect ((x0, y0), (x1, y1)) ->
5627 Gl.enable `blend;
5628 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5629 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5630 filledrect (float x0) (float y0) (float x1) (float y1);
5631 Gl.disable `blend;
5632 | Msel _
5633 | Mpan _
5634 | Mscrolly | Mscrollx
5635 | Mzoom _
5636 | Mnone -> ()
5637 end;
5638 enttext ();
5639 scrollindicator ();
5640 Wsi.swapb ();
5643 let zoomrect x y x1 y1 =
5644 let x0 = min x x1
5645 and x1 = max x x1
5646 and y0 = min y y1 in
5647 let zoom = (float state.w) /. float (x1 - x0) in
5648 let margin =
5649 let simple () =
5650 let adjw = wadjsb () + state.winw in
5651 if state.w < adjw
5652 then (adjw - state.w) / 2
5653 else xadjsb ()
5655 match conf.fitmodel with
5656 | FitWidth | FitProportional -> simple ()
5657 | FitPage ->
5658 match conf.columns with
5659 | Csplit _ ->
5660 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
5661 | Cmulti _ | Csingle _ -> simple ()
5663 gotoxy ((state.x + margin) - x0) (state.y + y0);
5664 state.anchor <- getanchor ();
5665 setzoom zoom;
5666 resetmstate ();
5669 let annot inline x y =
5670 match unproject x y with
5671 | Some (opaque, n, ux, uy) ->
5672 let add text =
5673 addannot opaque ux uy text;
5674 wcmd "freepage %s" (~> opaque);
5675 Hashtbl.remove state.pagemap (n, state.gen);
5676 flushtiles ();
5677 gotoxy state.x state.y
5679 if inline
5680 then
5681 let ondone s = add s in
5682 let mode = state.mode in
5683 state.mode <- Textentry (
5684 ("annotation: ", E.s, None, textentry, ondone, true),
5685 fun _ -> state.mode <- mode);
5686 state.text <- E.s;
5687 enttext ();
5688 G.postRedisplay "annot"
5689 else
5690 add @@ getusertext E.s
5691 | _ -> ()
5694 let zoomblock x y =
5695 let g opaque l px py =
5696 match rectofblock opaque px py with
5697 | Some a ->
5698 let x0 = a.(0) -. 20. in
5699 let x1 = a.(1) +. 20. in
5700 let y0 = a.(2) -. 20. in
5701 let zoom = (float state.w) /. (x1 -. x0) in
5702 let pagey = getpagey l.pageno in
5703 let margin = (state.w - l.pagew)/2 in
5704 let nx = -truncate x0 - margin in
5705 gotoxy_and_clear_text nx (pagey + truncate y0);
5706 state.anchor <- getanchor ();
5707 setzoom zoom;
5708 None
5709 | None -> None
5711 match conf.columns with
5712 | Csplit _ ->
5713 impmsg "block zooming does not work properly in split columns mode"
5714 | Cmulti _ | Csingle _ -> onppundermouse g x y ()
5717 let scrollx x =
5718 let winw = wadjsb () + state.winw - 1 in
5719 let s = float x /. float winw in
5720 let destx = truncate (float (state.w + winw) *. s) in
5721 gotoxy_and_clear_text (winw - destx) state.y;
5722 state.mstate <- Mscrollx;
5725 let scrolly y =
5726 let s = float y /. float state.winh in
5727 let desty = truncate (float (state.maxy - state.winh) *. s) in
5728 gotoxy_and_clear_text state.x desty;
5729 state.mstate <- Mscrolly;
5732 let viewmulticlick clicks x y mask =
5733 let g opaque l px py =
5734 let mark =
5735 match clicks with
5736 | 2 -> Mark_word
5737 | 3 -> Mark_line
5738 | 4 -> Mark_block
5739 | _ -> Mark_page
5741 if markunder opaque px py mark
5742 then (
5743 Some (fun () ->
5744 let dopipe cmd =
5745 match getopaque l.pageno with
5746 | None -> ()
5747 | Some opaque -> pipesel opaque cmd
5749 state.roam <- (fun () -> dopipe conf.paxcmd);
5750 if not (Wsi.withctrl mask) then dopipe conf.selcmd;
5753 else None
5755 G.postRedisplay "viewmulticlick";
5756 onppundermouse g x y (fun () -> impmsg "nothing to select") ();
5759 let canselect () =
5760 match conf.columns with
5761 | Csplit _ -> false
5762 | Csingle _ | Cmulti _ -> conf.angle mod 360 = 0
5765 let viewmouse button down x y mask =
5766 match button with
5767 | n when (n == 4 || n == 5) && not down ->
5768 if Wsi.withctrl mask
5769 then (
5770 match state.mstate with
5771 | Mzoom (oldn, i) ->
5772 if oldn = n
5773 then (
5774 if i = 2
5775 then
5776 let incr =
5777 match n with
5778 | 5 ->
5779 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5780 | _ ->
5781 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5783 let zoom = conf.zoom -. incr in
5784 pivotzoom ~x ~y zoom;
5785 state.mstate <- Mzoom (n, 0);
5786 else
5787 state.mstate <- Mzoom (n, i+1);
5789 else state.mstate <- Mzoom (n, 0)
5791 | Msel _
5792 | Mpan _
5793 | Mscrolly | Mscrollx
5794 | Mzoomrect _
5795 | Mnone -> state.mstate <- Mzoom (n, 0)
5797 else (
5798 match state.autoscroll with
5799 | Some step -> setautoscrollspeed step (n=4)
5800 | None ->
5801 if conf.wheelbypage || conf.presentation
5802 then (
5803 if n = 4
5804 then prevpage ()
5805 else nextpage ()
5807 else
5808 let incr =
5809 if n = 4
5810 then -conf.scrollstep
5811 else conf.scrollstep
5813 let incr = incr * 2 in
5814 let y = clamp incr in
5815 gotoxy_and_clear_text state.x y
5818 | n when (n = 6 || n = 7) && not down && canpan () ->
5819 let x =
5820 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep) in
5821 gotoxy_and_clear_text x state.y
5823 | 1 when Wsi.withshift mask ->
5824 state.mstate <- Mnone;
5825 if not down
5826 then (
5827 match unproject x y with
5828 | None -> ()
5829 | Some (_, pageno, ux, uy) ->
5830 let cmd = Printf.sprintf
5831 "%s %s %d %d %d"
5832 conf.stcmd state.path pageno ux uy
5834 match spawn cmd [] with
5835 | (exception exn) ->
5836 impmsg "execution of synctex command(%S) failed: %S"
5837 conf.stcmd @@ exntos exn
5838 | _pid -> ()
5841 | 1 when Wsi.withctrl mask ->
5842 if down
5843 then (
5844 Wsi.setcursor Wsi.CURSOR_FLEUR;
5845 state.mstate <- Mpan (x, y)
5847 else
5848 state.mstate <- Mnone
5850 | 3 ->
5851 if down
5852 then (
5853 if Wsi.withshift mask
5854 then (
5855 annot conf.annotinline x y;
5856 G.postRedisplay "addannot"
5858 else
5859 let p = (x, y) in
5860 Wsi.setcursor Wsi.CURSOR_CYCLE;
5861 state.mstate <- Mzoomrect (p, p)
5863 else (
5864 match state.mstate with
5865 | Mzoomrect ((x0, y0), _) ->
5866 if abs (x-x0) > 10 && abs (y - y0) > 10
5867 then zoomrect x0 y0 x y
5868 else (
5869 resetmstate ();
5870 G.postRedisplay "kill accidental zoom rect";
5872 | Msel _
5873 | Mpan _
5874 | Mscrolly | Mscrollx
5875 | Mzoom _
5876 | Mnone ->
5877 resetmstate ()
5880 | 1 when vscrollhit x ->
5881 if down
5882 then
5883 let _, position, sh = state.uioh#scrollph in
5884 if y > truncate position && y < truncate (position +. sh)
5885 then state.mstate <- Mscrolly
5886 else scrolly y
5887 else
5888 state.mstate <- Mnone
5890 | 1 when y > state.winh - hscrollh () ->
5891 if down
5892 then
5893 let _, position, sw = state.uioh#scrollpw in
5894 if x > truncate position && x < truncate (position +. sw)
5895 then state.mstate <- Mscrollx
5896 else scrollx x
5897 else
5898 state.mstate <- Mnone
5900 | 1 when state.bzoom -> if not down then zoomblock x y
5902 | 1 ->
5903 let dest = if down then getunder x y else Unone in
5904 begin match dest with
5905 | Ulinkgoto _
5906 | Ulinkuri _
5907 | Uremote _ | Uremotedest _
5908 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5909 gotounder dest
5911 | Unone when down ->
5912 Wsi.setcursor Wsi.CURSOR_FLEUR;
5913 state.mstate <- Mpan (x, y);
5915 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
5917 | Unone | Utext _ ->
5918 if down
5919 then (
5920 if canselect ()
5921 then (
5922 state.mstate <- Msel ((x, y), (x, y));
5923 G.postRedisplay "mouse select";
5926 else (
5927 match state.mstate with
5928 | Mnone -> ()
5930 | Mzoom _ | Mscrollx | Mscrolly ->
5931 state.mstate <- Mnone
5933 | Mzoomrect ((x0, y0), _) ->
5934 zoomrect x0 y0 x y
5936 | Mpan _ ->
5937 Wsi.setcursor Wsi.CURSOR_INHERIT;
5938 state.mstate <- Mnone
5940 | Msel ((x0, y0), (x1, y1)) ->
5941 let rec loop = function
5942 | [] -> ()
5943 | l :: rest ->
5944 let inside =
5945 let a0 = l.pagedispy in
5946 let a1 = a0 + l.pagevh in
5947 let b0 = l.pagedispx in
5948 let b1 = b0 + l.pagevw in
5949 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5950 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5952 if inside
5953 then
5954 match getopaque l.pageno with
5955 | Some opaque ->
5956 let dosel cmd () =
5957 match Unix.pipe () with
5958 | (exception exn) ->
5959 impmsg "cannot create sel pipe: %s" @@
5960 exntos exn;
5961 | (r, w) ->
5962 let clo what fd =
5963 Ne.clo fd (fun msg ->
5964 dolog "%s close failed: %s" what msg)
5966 let pid =
5967 try spawn cmd [r, 0; w, -1]
5968 with exn ->
5969 dolog "cannot execute %S: %s"
5970 cmd @@ exntos exn;
5973 if pid > 0
5974 then (
5975 copysel w opaque;
5976 G.postRedisplay "copysel";
5978 else clo "Msel pipe/w" w;
5979 clo "Msel pipe/r" r;
5981 dosel conf.selcmd ();
5982 state.roam <- dosel conf.paxcmd;
5983 | None -> ()
5984 else loop rest
5986 loop state.layout;
5987 resetmstate ();
5991 | _ -> ()
5994 let birdseyemouse button down x y mask
5995 (conf, leftx, _, hooverpageno, anchor) =
5996 match button with
5997 | 1 when down ->
5998 let rec loop = function
5999 | [] -> ()
6000 | l :: rest ->
6001 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6002 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6003 then (
6004 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
6006 else loop rest
6008 loop state.layout
6009 | 3 -> ()
6010 | _ -> viewmouse button down x y mask
6013 let uioh = object
6014 method display = ()
6016 method key key mask =
6017 begin match state.mode with
6018 | Textentry textentry -> textentrykeyboard key mask textentry
6019 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6020 | View -> viewkeyboard key mask
6021 | LinkNav linknav -> linknavkeyboard key mask linknav
6022 end;
6023 state.uioh
6025 method button button bstate x y mask =
6026 begin match state.mode with
6027 | LinkNav _
6028 | View -> viewmouse button bstate x y mask
6029 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6030 | Textentry _ -> ()
6031 end;
6032 state.uioh
6034 method multiclick clicks x y mask =
6035 begin match state.mode with
6036 | LinkNav _
6037 | View -> viewmulticlick clicks x y mask
6038 | Birdseye _
6039 | Textentry _ -> ()
6040 end;
6041 state.uioh
6043 method motion x y =
6044 begin match state.mode with
6045 | Textentry _ -> ()
6046 | View | Birdseye _ | LinkNav _ ->
6047 match state.mstate with
6048 | Mzoom _ | Mnone -> ()
6050 | Mpan (x0, y0) ->
6051 let dx = x - x0
6052 and dy = y0 - y in
6053 state.mstate <- Mpan (x, y);
6054 let x = if canpan () then panbound (state.x + dx) else state.x in
6055 let y = clamp dy in
6056 gotoxy_and_clear_text x y
6058 | Msel (a, _) ->
6059 state.mstate <- Msel (a, (x, y));
6060 G.postRedisplay "motion select";
6062 | Mscrolly ->
6063 let y = min state.winh (max 0 y) in
6064 scrolly y
6066 | Mscrollx ->
6067 let x = min state.winw (max 0 x) in
6068 scrollx x
6070 | Mzoomrect (p0, _) ->
6071 state.mstate <- Mzoomrect (p0, (x, y));
6072 G.postRedisplay "motion zoomrect";
6073 end;
6074 state.uioh
6076 method pmotion x y =
6077 begin match state.mode with
6078 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6079 let rec loop = function
6080 | [] ->
6081 if hooverpageno != -1
6082 then (
6083 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6084 G.postRedisplay "pmotion birdseye no hoover";
6086 | l :: rest ->
6087 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6088 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6089 then (
6090 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6091 G.postRedisplay "pmotion birdseye hoover";
6093 else loop rest
6095 loop state.layout
6097 | Textentry _ -> ()
6099 | LinkNav _
6100 | View ->
6101 match state.mstate with
6102 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ -> ()
6103 | Mnone ->
6104 updateunder x y;
6105 if canselect ()
6106 then
6107 match conf.pax with
6108 | None -> ()
6109 | Some r ->
6110 let past, _, _ = !r in
6111 let now = now () in
6112 let delta = now -. past in
6113 if delta > 0.01
6114 then paxunder x y
6115 else r := (now, x, y)
6116 end;
6117 state.uioh
6119 method infochanged _ = ()
6121 method scrollph =
6122 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6123 let p, h =
6124 if maxy = 0
6125 then 0.0, float state.winh
6126 else scrollph state.y maxy
6128 vscrollw (), p, h
6130 method scrollpw =
6131 let winw = wadjsb () + state.winw in
6132 let fwinw = float winw in
6133 let sw =
6134 let sw = fwinw /. float state.w in
6135 let sw = fwinw *. sw in
6136 max sw (float conf.scrollh)
6138 let position =
6139 let maxx = state.w + winw in
6140 let x = winw - state.x in
6141 let percent = float x /. float maxx in
6142 (fwinw -. sw) *. percent
6144 hscrollh (), position, sw
6146 method modehash =
6147 let modename =
6148 match state.mode with
6149 | LinkNav _ -> "links"
6150 | Textentry _ -> "textentry"
6151 | Birdseye _ -> "birdseye"
6152 | View -> "view"
6154 findkeyhash conf modename
6156 method eformsgs = true
6157 method alwaysscrolly = false
6158 end;;
6160 let addrect pageno r g b a x0 y0 x1 y1 =
6161 Hashtbl.add state.prects pageno [|r; g; b; a; x0; y0; x1; y1|];
6164 let ract cmds =
6165 let cl = splitatspace cmds in
6166 let scan s fmt f =
6167 try Scanf.sscanf s fmt f
6168 with exn ->
6169 adderrfmt "remote exec"
6170 "error processing '%S': %s\n" cmds @@ exntos exn
6172 let rectx s pageno (r, g, b, a) x0 y0 x1 y1 =
6173 vlog "%s page %d color (%f %f %f %f) x0,y0,x1,y1 = %f %f %f %f"
6174 s pageno r g b a x0 y0 x1 y1;
6175 onpagerect
6176 pageno
6177 (fun w h ->
6178 let _,w1,h1,_ = getpagedim pageno in
6179 let sw = float w1 /. float w
6180 and sh = float h1 /. float h in
6181 let x0s = x0 *. sw
6182 and x1s = x1 *. sw
6183 and y0s = y0 *. sh
6184 and y1s = y1 *. sh in
6185 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
6186 let color = (r, g, b, a) in
6187 if conf.verbose then debugrect rect;
6188 state.rects <- (pageno, color, rect) :: state.rects;
6189 G.postRedisplay s;
6192 match cl with
6193 | "reload", "" -> reload ()
6194 | "goto", args ->
6195 scan args "%u %f %f"
6196 (fun pageno x y ->
6197 let cmd, _ = state.geomcmds in
6198 if emptystr cmd
6199 then gotopagexy !wtmode pageno x y
6200 else
6201 let f prevf () =
6202 gotopagexy !wtmode pageno x y;
6203 prevf ()
6205 state.reprf <- f state.reprf
6207 | "goto1", args -> scan args "%u %f" gotopage
6208 | "gotor", args ->
6209 scan args "%S %u"
6210 (fun filename pageno -> gotounder (Uremote (filename, pageno)))
6211 | "gotord", args ->
6212 scan args "%S %S"
6213 (fun filename dest -> gotounder (Uremotedest (filename, dest)))
6214 | "rect", args ->
6215 scan args "%u %u %f %f %f %f"
6216 (fun pageno c x0 y0 x1 y1 ->
6217 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
6218 rectx "rect" pageno color x0 y0 x1 y1;
6220 | "prect", args ->
6221 scan args "%u %f %f %f %f %f %f %f %f"
6222 (fun pageno r g b alpha x0 y0 x1 y1 ->
6223 addrect pageno r g b alpha x0 y0 x1 y1;
6224 G.postRedisplay "prect"
6226 | "pgoto", args ->
6227 scan args "%u %f %f"
6228 (fun pageno x y ->
6229 let optopaque =
6230 match getopaque pageno with
6231 | Some opaque -> opaque
6232 | None -> ~< E.s
6234 pgoto optopaque pageno x y;
6235 let rec fixx = function
6236 | [] -> ()
6237 | l :: rest ->
6238 if l.pageno = pageno
6239 then gotoxy (state.x - l.pagedispx) state.y
6240 else fixx rest
6242 let layout =
6243 let mult =
6244 match conf.columns with
6245 | Csingle _ | Csplit _ -> 1
6246 | Cmulti ((n, _, _), _) -> n
6248 layout 0 state.y (state.winw * mult) state.winh
6250 fixx layout
6252 | "activatewin", "" -> Wsi.activatewin ()
6253 | "quit", "" -> raise Quit
6254 | "keys", keys ->
6255 begin try
6256 let l = Config.keys_of_string keys in
6257 List.iter (fun (k, m) -> keyboard k m) l
6258 with exn ->
6259 adderrfmt "error processing keys" "`%S': %s\n" cmds @@ exntos exn
6261 | "clearrects", "" ->
6262 Hashtbl.clear state.prects;
6263 G.postRedisplay "clearrects"
6264 | _ ->
6265 adderrfmt "remote command"
6266 "error processing remote command: %S\n" cmds;
6269 let remote =
6270 let scratch = Bytes.create 80 in
6271 let buf = Buffer.create 80 in
6272 fun fd ->
6273 match tempfailureretry (Unix.read fd scratch 0) 80 with
6274 | (exception Unix.Unix_error (Unix.EAGAIN, _, _)) -> None
6275 | 0 ->
6276 Unix.close fd;
6277 if Buffer.length buf > 0
6278 then (
6279 let s = Buffer.contents buf in
6280 Buffer.clear buf;
6281 ract s;
6283 None
6284 | n ->
6285 let rec eat ppos =
6286 let nlpos =
6287 match Bytes.index_from scratch ppos '\n' with
6288 | pos -> if pos >= n then -1 else pos
6289 | (exception Not_found) -> -1
6291 if nlpos >= 0
6292 then (
6293 Buffer.add_subbytes buf scratch ppos (nlpos-ppos);
6294 let s = Buffer.contents buf in
6295 Buffer.clear buf;
6296 ract s;
6297 eat (nlpos+1);
6299 else (
6300 Buffer.add_subbytes buf scratch ppos (n-ppos);
6301 Some fd
6303 in eat 0
6306 let remoteopen path =
6307 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
6308 with exn ->
6309 adderrfmt "remoteopen" "error opening %S: %s" path @@ exntos exn;
6310 None
6313 let () =
6314 let gcconfig = ref E.s in
6315 let trimcachepath = ref E.s in
6316 let rcmdpath = ref E.s in
6317 let pageno = ref None in
6318 let rootwid = ref 0 in
6319 let openlast = ref false in
6320 let nofc = ref false in
6321 let doreap = ref false in
6322 selfexec := Sys.executable_name;
6323 Arg.parse
6324 (Arg.align
6325 [("-p", Arg.String (fun s -> state.password <- s),
6326 "<password> Set password");
6328 ("-f", Arg.String
6329 (fun s ->
6330 Config.fontpath := s;
6331 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
6333 "<path> Set path to the user interface font");
6335 ("-c", Arg.String
6336 (fun s ->
6337 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
6338 Config.confpath := s),
6339 "<path> Set path to the configuration file");
6341 ("-last", Arg.Set openlast, " Open last document");
6343 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
6344 "<page-number> Jump to page");
6346 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6347 "<path> Set path to the trim cache file");
6349 ("-dest", Arg.String (fun s -> state.nameddest <- s),
6350 "<named-destination> Set named destination");
6352 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
6353 ("-cxack", Arg.Set cxack, " Cut corners");
6355 ("-remote", Arg.String (fun s -> rcmdpath := s),
6356 "<path> Set path to the remote commands source");
6358 ("-origin", Arg.String (fun s -> state.origin <- s),
6359 "<original-path> Set original path");
6361 ("-gc", Arg.Set_string gcconfig,
6362 "<script-path> Collect garbage with the help of a script");
6364 ("-nofc", Arg.Set nofc, " Do not use fontconfig");
6366 ("-v", Arg.Unit (fun () ->
6367 Printf.printf
6368 "%s\nconfiguration path: %s\n"
6369 (version ())
6370 Config.defconfpath
6372 exit 0), " Print version and exit");
6374 ("-embed", Arg.Set_int rootwid,
6375 "<window-id> Embed into window")
6378 (fun s -> state.path <- s)
6379 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6381 if !wtmode
6382 then selfexec := !selfexec ^ " -wtmode";
6384 let histmode = emptystr state.path && not !openlast in
6386 if not (Config.load !openlast)
6387 then dolog "failed to load configuration";
6389 begin match !pageno with
6390 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
6391 | None -> ()
6392 end;
6394 if nonemptystr !gcconfig
6395 then (
6396 let (c, s) =
6397 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6398 | (exception exn) -> error "socketpair for gc failed: %s" @@ exntos exn
6399 | fds -> fds
6401 match spawn !gcconfig [(c, 0); (c, 1); (s, -1)] with
6402 | (exception exn) -> error "failed to execute gc script: %s" @@ exntos exn
6403 | _pid ->
6404 Ne.clo c @@ (fun s -> error "failed to close gc fd %s" s);
6405 Config.gc s;
6406 exit 0
6409 let wsfd, winw, winh = Wsi.init (object (self)
6410 val mutable m_clicks = 0
6411 val mutable m_click_x = 0
6412 val mutable m_click_y = 0
6413 val mutable m_lastclicktime = infinity
6415 method private cleanup =
6416 state.roam <- noroam;
6417 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap
6418 method expose = G.postRedisplay "expose"
6419 method visible v =
6420 let name =
6421 match v with
6422 | Wsi.Unobscured -> "unobscured"
6423 | Wsi.PartiallyObscured -> "partiallyobscured"
6424 | Wsi.FullyObscured -> "fullyobscured"
6426 vlog "visibility change %s" name
6427 method display = display ()
6428 method map mapped = vlog "mapped %b" mapped
6429 method reshape w h =
6430 self#cleanup;
6431 reshape w h
6432 method mouse b d x y m =
6433 if d && canselect ()
6434 then (
6435 (* http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx *)
6436 m_click_x <- x;
6437 m_click_y <- y;
6438 if b = 1
6439 then (
6440 let t = now () in
6441 if abs x - m_click_x > 10
6442 || abs y - m_click_y > 10
6443 || abs_float (t -. m_lastclicktime) > 0.3
6444 then m_clicks <- 0;
6445 m_clicks <- m_clicks + 1;
6446 m_lastclicktime <- t;
6447 if m_clicks = 1
6448 then (
6449 self#cleanup;
6450 G.postRedisplay "cleanup";
6451 state.uioh <- state.uioh#button b d x y m;
6453 else state.uioh <- state.uioh#multiclick m_clicks x y m
6455 else (
6456 self#cleanup;
6457 m_clicks <- 0;
6458 m_lastclicktime <- infinity;
6459 state.uioh <- state.uioh#button b d x y m
6462 else (
6463 state.uioh <- state.uioh#button b d x y m
6465 method motion x y =
6466 state.mpos <- (x, y);
6467 state.uioh <- state.uioh#motion x y
6468 method pmotion x y =
6469 state.mpos <- (x, y);
6470 state.uioh <- state.uioh#pmotion x y
6471 method key k m =
6472 let mascm = m land (
6473 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6474 ) in
6475 let keyboard k m =
6476 let x = state.x and y = state.y in
6477 keyboard k m;
6478 if x != state.x || y != state.y then self#cleanup
6480 match state.keystate with
6481 | KSnone ->
6482 let km = k, mascm in
6483 begin
6484 match
6485 let modehash = state.uioh#modehash in
6486 try Hashtbl.find modehash km
6487 with Not_found ->
6488 try Hashtbl.find (findkeyhash conf "global") km
6489 with Not_found -> KMinsrt (k, m)
6490 with
6491 | KMinsrt (k, m) -> keyboard k m
6492 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6493 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6495 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6496 List.iter (fun (k, m) -> keyboard k m) insrt;
6497 state.keystate <- KSnone
6498 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6499 state.keystate <- KSinto (keys, insrt)
6500 | KSinto _ -> state.keystate <- KSnone
6502 method enter x y =
6503 state.mpos <- (x, y);
6504 state.uioh <- state.uioh#pmotion x y
6505 method leave = state.mpos <- (-1, -1)
6506 method winstate wsl = state.winstate <- wsl
6507 method quit = raise Quit
6508 end) !rootwid conf.cwinw conf.cwinh platform in
6510 setbgcol conf.bgcolor;
6511 state.wsfd <- wsfd;
6513 if not (
6514 List.exists GlMisc.check_extension
6515 [ "GL_ARB_texture_rectangle"
6516 ; "GL_EXT_texture_recangle"
6517 ; "GL_NV_texture_rectangle" ]
6519 then (dolog "OpenGL does not suppport rectangular textures"; exit 1);
6521 if (
6522 let r = GlMisc.get_string `renderer in
6523 let p = "Mesa DRI Intel(" in
6524 let l = String.length p in
6525 String.length r > l && String.sub r 0 l = p
6527 then (
6528 defconf.sliceheight <- 1024;
6529 defconf.texcount <- 32;
6530 defconf.usepbo <- true;
6533 let cs, ss =
6534 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6535 | (exception exn) ->
6536 dolog "socketpair failed: %s" @@ exntos exn;
6537 exit 1
6538 | (r, w) ->
6539 cloexec r;
6540 cloexec w;
6541 r, w
6544 setcheckers conf.checkers;
6546 opengl_has_pbo := GlMisc.check_extension "GL_ARB_pixel_buffer_object";
6548 init cs (
6549 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
6550 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6551 !Config.fontpath, !trimcachepath,
6552 !opengl_has_pbo,
6553 not !nofc
6555 List.iter GlArray.enable [`texture_coord; `vertex];
6556 state.ss <- ss;
6557 reshape ~firsttime:true winw winh;
6558 state.uioh <- uioh;
6559 if histmode
6560 then (
6561 Wsi.settitle "llpp (history)";
6562 enterhistmode ();
6564 else (
6565 state.text <- "Opening " ^ (mbtoutf8 state.path);
6566 opendoc state.path state.password;
6568 display ();
6569 Wsi.mapwin ();
6570 Wsi.setcursor Wsi.CURSOR_INHERIT;
6571 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
6573 let rec reap () =
6574 match Unix.waitpid [Unix.WNOHANG] ~-1 with
6575 | (exception (Unix.Unix_error (Unix.ECHILD, _, _))) -> ()
6576 | (exception exn) -> dolog "Unix.waitpid: %s" @@ exntos exn
6577 | 0, _ -> ()
6578 | _pid, _status -> reap ()
6580 Sys.set_signal Sys.sigchld (Sys.Signal_handle (fun _ -> doreap := true));
6582 let optrfd =
6583 ref (
6584 if nonemptystr !rcmdpath
6585 then remoteopen !rcmdpath
6586 else None
6590 let rec loop deadline =
6591 if !doreap
6592 then (
6593 doreap := false;
6594 reap ()
6596 let r = [state.ss; state.wsfd] in
6597 let r =
6598 match !optrfd with
6599 | None -> r
6600 | Some fd -> fd :: r
6602 if state.redisplay
6603 then (
6604 state.redisplay <- false;
6605 display ();
6607 let timeout =
6608 let now = now () in
6609 if deadline > now
6610 then (
6611 if deadline = infinity
6612 then ~-.1.0
6613 else max 0.0 (deadline -. now)
6615 else 0.0
6617 let r, _, _ =
6618 try Unix.select r [] [] timeout
6619 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6621 begin match r with
6622 | [] ->
6623 state.ghyll None;
6624 let newdeadline =
6625 if state.ghyll == noghyll
6626 then
6627 match state.autoscroll with
6628 | Some step when step != 0 ->
6629 let y = state.y + step in
6630 let fy = if conf.maxhfit then state.winh else 0 in
6631 let y =
6632 if y < 0
6633 then state.maxy - fy
6634 else if y >= state.maxy - fy then 0 else y
6636 if state.mode = View
6637 then gotoxy_and_clear_text state.x y
6638 else gotoxy state.x y;
6639 deadline +. 0.01
6640 | _ -> infinity
6641 else deadline +. 0.01
6643 loop newdeadline
6645 | l ->
6646 let rec checkfds = function
6647 | [] -> ()
6648 | fd :: rest when fd = state.ss ->
6649 let cmd = rcmd state.ss in
6650 act cmd;
6651 checkfds rest
6653 | fd :: rest when fd = state.wsfd ->
6654 Wsi.readresp fd;
6655 checkfds rest
6657 | fd :: rest when Some fd = !optrfd ->
6658 begin match remote fd with
6659 | None -> optrfd := remoteopen !rcmdpath;
6660 | opt -> optrfd := opt
6661 end;
6662 checkfds rest
6664 | _ :: rest ->
6665 dolog "select returned unknown descriptor";
6666 checkfds rest
6668 checkfds l;
6669 let newdeadline =
6670 let deadline1 =
6671 if deadline = infinity
6672 then now () +. 0.01
6673 else deadline
6675 match state.autoscroll with
6676 | Some step when step != 0 -> deadline1
6677 | _ -> if state.ghyll == noghyll then infinity else deadline1
6679 loop newdeadline
6680 end;
6683 loop infinity;
6684 with Quit ->
6685 Config.save leavebirdseye;
6686 if hasunsavedchanges ()
6687 then save ();