Indentation
[llpp.git] / main.ml
blob10b86984e47c4597f15eafc82eeec9e20cd1e2c3
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 c =
377 if key >= 32 && key < 127
378 then Char.chr key
379 else '\000'
381 match Char.lowercase c with
382 | '0' .. '9' ->
383 let text = addchar text c in
384 TEcont text
386 | 'k' | 'm' | 'g' ->
387 let text = addchar text c in
388 TEcont text
390 | _ ->
391 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
392 TEcont text
395 let wcmd fmt =
396 let b = Buffer.create 16 in
397 Buffer.add_string b "llll";
398 Printf.kbprintf
399 (fun b ->
400 let b = Buffer.to_bytes b in
401 wcmd state.ss b @@ Bytes.length b
402 ) b fmt
405 let nogeomcmds cmds =
406 match cmds with
407 | s, [] -> emptystr s
408 | _ -> false
411 let layoutN ((columns, coverA, coverB), b) x y sw sh =
412 let sh = sh - (hscrollh ()) in
413 let wadj = wadjsb () in
414 let rec fold accu n =
415 if n = Array.length b
416 then accu
417 else
418 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
419 if (vy - y) > sh &&
420 (n = coverA - 1
421 || n = state.pagecount - coverB
422 || (n - coverA) mod columns = columns - 1)
423 then accu
424 else
425 let accu =
426 if vy + h > y
427 then
428 let pagey = max 0 (y - vy) in
429 let pagedispy = if pagey > 0 then 0 else vy - y in
430 let pagedispx, pagex =
431 let pdx =
432 if n = coverA - 1 || n = state.pagecount - coverB
433 then x + (wadj + sw - w) / 2
434 else dx + xoff + x
436 if pdx < 0
437 then 0, -pdx
438 else pdx, 0
440 let pagevw =
441 let vw = wadj + sw - pagedispx in
442 let pw = w - pagex in
443 min vw pw
445 let pagevh = min (h - pagey) (sh - pagedispy) in
446 if pagevw > 0 && pagevh > 0
447 then
448 let e =
449 { pageno = n
450 ; pagedimno = pdimno
451 ; pagew = w
452 ; pageh = h
453 ; pagex = pagex
454 ; pagey = pagey
455 ; pagevw = pagevw
456 ; pagevh = pagevh
457 ; pagedispx = pagedispx
458 ; pagedispy = pagedispy
459 ; pagecol = 0
462 e :: accu
463 else
464 accu
465 else
466 accu
468 fold accu (n+1)
470 if Array.length b = 0
471 then []
472 else List.rev (fold [] (page_of_y y))
475 let layoutS (columns, b) x y sw sh =
476 let sh = sh - hscrollh () in
477 let wadj = wadjsb () in
478 let rec fold accu n =
479 if n = Array.length b
480 then accu
481 else
482 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
483 if (vy - y) > sh
484 then accu
485 else
486 let accu =
487 if vy + pageh > y
488 then
489 let x = xoff + x in
490 let pagey = max 0 (y - vy) in
491 let pagedispy = if pagey > 0 then 0 else vy - y in
492 let pagedispx, pagex =
493 if px = 0
494 then (
495 if x < 0
496 then 0, -x
497 else x, 0
499 else (
500 let px = px - x in
501 if px < 0
502 then -px, 0
503 else 0, px
506 let pagecolw = pagew/columns in
507 let pagedispx =
508 if pagecolw < sw
509 then pagedispx + ((wadj + sw - pagecolw) / 2)
510 else pagedispx
512 let pagevw =
513 let vw = wadj + sw - pagedispx in
514 let pw = pagew - pagex in
515 min vw pw
517 let pagevw = min pagevw pagecolw in
518 let pagevh = min (pageh - pagey) (sh - pagedispy) in
519 if pagevw > 0 && pagevh > 0
520 then
521 let e =
522 { pageno = n/columns
523 ; pagedimno = pdimno
524 ; pagew = pagew
525 ; pageh = pageh
526 ; pagex = pagex
527 ; pagey = pagey
528 ; pagevw = pagevw
529 ; pagevh = pagevh
530 ; pagedispx = pagedispx
531 ; pagedispy = pagedispy
532 ; pagecol = n mod columns
535 e :: accu
536 else
537 accu
538 else
539 accu
541 fold accu (n+1)
543 List.rev (fold [] 0)
546 let layout x y sw sh =
547 if nogeomcmds state.geomcmds
548 then
549 match conf.columns with
550 | Csingle b -> layoutN ((1, 0, 0), b) x y sw sh
551 | Cmulti c -> layoutN c x y sw sh
552 | Csplit s -> layoutS s x y sw sh
553 else []
556 let clamp incr =
557 let y = state.y + incr in
558 let y = max 0 y in
559 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
563 let itertiles l f =
564 let tilex = l.pagex mod conf.tilew in
565 let tiley = l.pagey mod conf.tileh in
567 let col = l.pagex / conf.tilew in
568 let row = l.pagey / conf.tileh in
570 let xadj = xadjsb () in
571 let rec rowloop row y0 dispy h =
572 if h = 0
573 then ()
574 else (
575 let dh = conf.tileh - y0 in
576 let dh = min h dh in
577 let rec colloop col x0 dispx w =
578 if w = 0
579 then ()
580 else (
581 let dw = conf.tilew - x0 in
582 let dw = min w dw in
583 let dispx' = xadj + dispx in
584 f col row dispx' dispy x0 y0 dw dh;
585 colloop (col+1) 0 (dispx+dw) (w-dw)
588 colloop col tilex l.pagedispx l.pagevw;
589 rowloop (row+1) 0 (dispy+dh) (h-dh)
592 if l.pagevw > 0 && l.pagevh > 0
593 then rowloop row tiley l.pagedispy l.pagevh;
596 let gettileopaque l col row =
597 let key =
598 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
600 try Some (Hashtbl.find state.tilemap key)
601 with Not_found -> None
604 let puttileopaque l col row gen colorspace angle opaque size elapsed =
605 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
606 Hashtbl.add state.tilemap key (opaque, size, elapsed)
609 let filledrect2 x0 y0 x1 y1 x2 y2 x3 y3 =
610 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x1; y1; x2; y2; x3; y3 |];
611 GlArray.vertex `two state.vraw;
612 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
615 let filledrect1 x0 y0 x1 y1 = filledrect2 x0 y0 x0 y1 x1 y0 x1 y1;;
617 let filledrect x0 y0 x1 y1 =
618 GlArray.disable `texture_coord;
619 filledrect1 x0 y0 x1 y1;
620 GlArray.enable `texture_coord;
623 let linerect x0 y0 x1 y1 =
624 GlArray.disable `texture_coord;
625 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y1; x1; y0 |];
626 GlArray.vertex `two state.vraw;
627 GlArray.draw_arrays `line_loop ~first:0 ~count:4;
628 GlArray.enable `texture_coord;
631 let drawtiles l color =
632 GlDraw.color color;
633 let wadj = wadjsb () in
634 begintiles ();
635 let f col row x y tilex tiley w h =
636 match gettileopaque l col row with
637 | Some (opaque, _, t) ->
638 let params = x, y, w, h, tilex, tiley in
639 if conf.invert
640 then GlTex.env (`mode `blend);
641 drawtile params opaque;
642 if conf.invert
643 then GlTex.env (`mode `modulate);
644 if conf.debug
645 then (
646 endtiles ();
647 let s = Printf.sprintf
648 "%d[%d,%d] %f sec"
649 l.pageno col row t
651 let w = measurestr fstate.fontsize s in
652 GlDraw.color (0.0, 0.0, 0.0);
653 filledrect (float (x-2))
654 (float (y-2))
655 (float (x+2) +. w)
656 (float (y + fstate.fontsize + 2));
657 GlDraw.color color;
658 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
659 begintiles ();
662 | None ->
663 endtiles ();
664 let w =
665 if conf.leftscroll
666 then w
667 else
668 let lw = wadj + state.winw - x in
669 min lw w
670 and h =
671 let lh = state.winh - y in
672 min lh h
674 if conf.invert
675 then GlTex.env (`mode `blend);
676 begin match state.checkerstexid with
677 | Some id ->
678 Gl.enable `texture_2d;
679 GlTex.bind_texture ~target:`texture_2d id;
680 let x0 = float x
681 and y0 = float y
682 and x1 = float (x+w)
683 and y1 = float (y+h) in
685 let tw = float w /. 16.0
686 and th = float h /. 16.0 in
687 let tx0 = float tilex /. 16.0
688 and ty0 = float tiley /. 16.0 in
689 let tx1 = tx0 +. tw
690 and ty1 = ty0 +. th in
691 Raw.sets_float state.vraw ~pos:0
692 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
693 Raw.sets_float state.traw ~pos:0
694 [| tx0; ty0; tx0; ty1; tx1; ty0; tx1; ty1 |];
695 GlArray.vertex `two state.vraw;
696 GlArray.tex_coord `two state.traw;
697 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
698 Gl.disable `texture_2d;
700 | None ->
701 GlDraw.color (1.0, 1.0, 1.0);
702 filledrect (float x) (float y) (float (x+w)) (float (y+h));
703 end;
704 if conf.invert
705 then GlTex.env (`mode `modulate);
706 if w > 128 && h > fstate.fontsize + 10
707 then (
708 let c = if conf.invert then 1.0 else 0.0 in
709 GlDraw.color (c, c, c);
710 let c, r =
711 if conf.verbose
712 then (col*conf.tilew, row*conf.tileh)
713 else col, row
715 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
717 GlDraw.color color;
718 begintiles ();
720 itertiles l f;
721 endtiles ();
724 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
726 let tilevisible1 l x y =
727 let ax0 = l.pagex
728 and ax1 = l.pagex + l.pagevw
729 and ay0 = l.pagey
730 and ay1 = l.pagey + l.pagevh in
732 let bx0 = x
733 and by0 = y in
734 let bx1 = min (bx0 + conf.tilew) l.pagew
735 and by1 = min (by0 + conf.tileh) l.pageh in
737 let rx0 = max ax0 bx0
738 and ry0 = max ay0 by0
739 and rx1 = min ax1 bx1
740 and ry1 = min ay1 by1 in
742 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
743 nonemptyintersection
746 let tilevisible layout n x y =
747 let rec findpageinlayout m = function
748 | l :: rest when l.pageno = n ->
749 tilevisible1 l x y || (
750 match conf.columns with
751 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
752 | Csplit _
753 | Csingle _
754 | Cmulti _ -> false
756 | _ :: rest -> findpageinlayout 0 rest
757 | [] -> false
759 findpageinlayout 0 layout;
762 let tileready l x y =
763 tilevisible1 l x y &&
764 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
767 let tilepage n p layout =
768 let rec loop = function
769 | l :: rest ->
770 if l.pageno = n
771 then
772 let f col row _ _ _ _ _ _ =
773 if state.currently = Idle
774 then
775 match gettileopaque l col row with
776 | Some _ -> ()
777 | None ->
778 let x = col*conf.tilew
779 and y = row*conf.tileh in
780 let w =
781 let w = l.pagew - x in
782 min w conf.tilew
784 let h =
785 let h = l.pageh - y in
786 min h conf.tileh
788 let pbo =
789 if conf.usepbo
790 then getpbo w h conf.colorspace
791 else ~< "0"
793 wcmd "tile %s %d %d %d %d %s"
794 (~> p) x y w h (~> pbo);
795 state.currently <-
796 Tiling (
797 l, p, conf.colorspace, conf.angle,
798 state.gen, col, row, conf.tilew, conf.tileh
801 itertiles l f;
802 else
803 loop rest
805 | [] -> ()
807 if nogeomcmds state.geomcmds
808 then loop layout;
811 let preloadlayout x y sw sh =
812 let y = if y < sh then 0 else y - sh in
813 let x = min 0 (x + sw) in
814 let h = sh*3 in
815 let w = sw*3 in
816 layout x y w h;
819 let load pages =
820 let rec loop pages =
821 if state.currently != Idle
822 then ()
823 else
824 match pages with
825 | l :: rest ->
826 begin match getopaque l.pageno with
827 | None ->
828 wcmd "page %d %d" l.pageno l.pagedimno;
829 state.currently <- Loading (l, state.gen);
830 | Some opaque ->
831 tilepage l.pageno opaque pages;
832 loop rest
833 end;
834 | _ -> ()
836 if nogeomcmds state.geomcmds
837 then loop pages
840 let preload pages =
841 load pages;
842 if conf.preload && state.currently = Idle
843 then load (preloadlayout state.x state.y state.winw state.winh);
846 let layoutready layout =
847 let rec fold all ls =
848 all && match ls with
849 | l :: rest ->
850 let seen = ref false in
851 let allvisible = ref true in
852 let foo col row _ _ _ _ _ _ =
853 seen := true;
854 allvisible := !allvisible &&
855 begin match gettileopaque l col row with
856 | Some _ -> true
857 | None -> false
860 itertiles l foo;
861 fold (!seen && !allvisible) rest
862 | [] -> true
864 let alltilesvisible = fold true layout in
865 alltilesvisible;
868 let gotoy y =
869 let y = bound y 0 state.maxy in
870 let y, layout, proceed =
871 match conf.maxwait with
872 | Some time when state.ghyll == noghyll ->
873 begin match state.throttle with
874 | None ->
875 let layout = layout state.x y state.winw state.winh in
876 let ready = layoutready layout in
877 if not ready
878 then (
879 load layout;
880 state.throttle <- Some (layout, y, now ());
882 else G.postRedisplay "gotoy showall (None)";
883 y, layout, ready
884 | Some (_, _, started) ->
885 let dt = now () -. started in
886 if dt > time
887 then (
888 state.throttle <- None;
889 let layout = layout state.x y state.winw state.winh in
890 load layout;
891 G.postRedisplay "maxwait";
892 y, layout, true
894 else -1, [], false
897 | _ ->
898 let layout = layout state.x y state.winw state.winh in
899 if not !wtmode || layoutready layout
900 then G.postRedisplay "gotoy ready";
901 y, layout, true
903 if proceed
904 then (
905 state.y <- y;
906 state.layout <- layout;
907 begin match state.mode with
908 | LinkNav ln ->
909 begin match ln with
910 | Ltexact (pageno, linkno) ->
911 let rec loop = function
912 | [] ->
913 state.mode <- LinkNav (Ltgendir 0)
914 | l :: _ when l.pageno = pageno ->
915 begin match getopaque pageno with
916 | None -> state.mode <- LinkNav (Ltnotready (pageno, 0))
917 | Some opaque ->
918 let x0, y0, x1, y1 = getlinkrect opaque linkno in
919 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
920 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
921 then state.mode <- LinkNav (Ltgendir 0)
923 | _ :: rest -> loop rest
925 loop layout
926 | Ltnotready _ | Ltgendir _ -> ()
928 | Birdseye _
929 | Textentry _
930 | View -> ()
931 end;
932 begin match state.mode with
933 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
934 if not (pagevisible layout pageno)
935 then (
936 match state.layout with
937 | [] -> ()
938 | l :: _ ->
939 state.mode <- Birdseye (
940 conf, leftx, l.pageno, hooverpageno, anchor
943 | LinkNav lt ->
944 begin match lt with
945 | Ltnotready (_, dir)
946 | Ltgendir dir ->
947 let linknav =
948 let rec loop = function
949 | [] -> lt
950 | l :: rest ->
951 match getopaque l.pageno with
952 | None -> Ltnotready (l.pageno, dir)
953 | Some opaque ->
954 let link =
955 let ld =
956 if dir = 0
957 then LDfirstvisible (l.pagex, l.pagey, dir)
958 else (
959 if dir > 0 then LDfirst else LDlast
962 findlink opaque ld
964 match link with
965 | Lnotfound -> loop rest
966 | Lfound n ->
967 showlinktype (getlink opaque n);
968 Ltexact (l.pageno, n)
970 loop state.layout
972 state.mode <- LinkNav linknav
973 | Ltexact _ -> ()
975 | Textentry _
976 | View -> ()
977 end;
978 preload layout;
980 state.ghyll <- noghyll;
981 if conf.updatecurs
982 then (
983 let mx, my = state.mpos in
984 updateunder mx my;
988 let conttiling pageno opaque =
989 tilepage pageno opaque
990 (if conf.preload
991 then preloadlayout state.x state.y state.winw state.winh
992 else state.layout)
995 let gotoy_and_clear_text y =
996 if not conf.verbose then state.text <- E.s;
997 gotoy y;
1000 let getanchory (n, top, dtop) =
1001 let y, h = getpageyh n in
1002 if conf.presentation
1003 then
1004 let ips = calcips h in
1005 y + truncate (top*.float h -. dtop*.float ips) + ips;
1006 else
1007 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1010 let gotoanchor anchor =
1011 gotoy (getanchory anchor);
1014 let addnav () =
1015 cbput state.hists.nav (getanchor ());
1018 let getnav dir =
1019 let anchor = cbgetc state.hists.nav dir in
1020 getanchory anchor;
1023 let gotoghyll1 single y =
1024 let scroll f n a b =
1025 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1026 let snake f a b =
1027 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1028 if f < a
1029 then s (float f /. float a)
1030 else (
1031 if f > b
1032 then 1.0 -. s ((float (f-b) /. float (n-b)))
1033 else 1.0
1036 snake f a b
1037 and summa n a b =
1038 let ins = float a *. 0.5
1039 and outs = float (n-b) *. 0.5 in
1040 let ones = b - a in
1041 ins +. outs +. float ones
1043 let rec set nab y sy =
1044 let (_N, _A, _B), y =
1045 if single
1046 then
1047 let scl = if y > sy then 2 else -2 in
1048 let _N, _, _ = nab in
1049 (_N,0,_N), y+conf.scrollstep*scl
1050 else nab,y in
1051 let sum = summa _N _A _B in
1052 let dy = float (y - sy) in
1053 state.ghyll <- (
1054 let rec gf n y1 o =
1055 if n >= _N
1056 then state.ghyll <- noghyll
1057 else
1058 let go n =
1059 let s = scroll n _N _A _B in
1060 let y1 = y1 +. ((s *. dy) /. sum) in
1061 gotoy_and_clear_text (truncate y1);
1062 state.ghyll <- gf (n+1) y1;
1064 match o with
1065 | None -> go n
1066 | Some y' when single -> set nab y' state.y
1067 | Some y' -> set (_N/2, 1, 1) y' state.y
1069 gf 0 (float state.y)
1072 match conf.ghyllscroll with
1073 | Some nab when not conf.presentation ->
1074 if state.ghyll == noghyll
1075 then set nab y state.y
1076 else state.ghyll (Some y)
1077 | _ ->
1078 gotoy_and_clear_text y
1081 let gotoghyll = gotoghyll1 false;;
1083 let gotopage n top =
1084 let y, h = getpageyh n in
1085 let y = y + (truncate (top *. float h)) in
1086 gotoghyll y
1089 let gotopage1 n top =
1090 let y = getpagey n in
1091 let y = y + top in
1092 gotoghyll y
1095 let invalidate s f =
1096 state.layout <- [];
1097 state.pdims <- [];
1098 state.rects <- [];
1099 state.rects1 <- [];
1100 match state.geomcmds with
1101 | ps, [] when emptystr ps ->
1102 f ();
1103 state.geomcmds <- s, [];
1105 | ps, [] ->
1106 state.geomcmds <- ps, [s, f];
1108 | ps, (s', _) :: rest when s' = s ->
1109 state.geomcmds <- ps, ((s, f) :: rest);
1111 | ps, cmds ->
1112 state.geomcmds <- ps, ((s, f) :: cmds);
1115 let flushpages () =
1116 Hashtbl.iter (fun _ opaque ->
1117 wcmd "freepage %s" (~> opaque);
1118 ) state.pagemap;
1119 Hashtbl.clear state.pagemap;
1122 let flushtiles () =
1123 if not (Queue.is_empty state.tilelru)
1124 then (
1125 Queue.iter (fun (k, p, s) ->
1126 wcmd "freetile %s" (~> p);
1127 state.memused <- state.memused - s;
1128 Hashtbl.remove state.tilemap k;
1129 ) state.tilelru;
1130 state.uioh#infochanged Memused;
1131 Queue.clear state.tilelru;
1133 load state.layout;
1136 let stateh h =
1137 let h = truncate (float h*.conf.zoom) in
1138 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
1139 h - d
1142 let opendoc path password =
1143 state.path <- path;
1144 state.password <- password;
1145 state.gen <- state.gen + 1;
1146 state.docinfo <- [];
1147 state.outlines <- [||];
1149 flushpages ();
1150 setaalevel conf.aalevel;
1151 let titlepath =
1152 if emptystr state.origin
1153 then path
1154 else state.origin
1156 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
1157 wcmd "open %d %d %s\000%s\000" (btod !wtmode) (btod !cxack) path password;
1158 invalidate "reqlayout"
1159 (fun () ->
1160 wcmd "reqlayout %d %d %d %s\000"
1161 conf.angle (FMTE.to_int conf.fitmodel)
1162 (stateh state.winh) state.nameddest
1166 let reload () =
1167 state.anchor <- getanchor ();
1168 opendoc state.path state.password;
1171 let scalecolor c =
1172 let c = c *. conf.colorscale in
1173 (c, c, c);
1176 let scalecolor2 (r, g, b) =
1177 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1180 let docolumns columns =
1181 let wadj = wadjsb () in
1182 match columns with
1183 | Csingle _ ->
1184 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1185 let wadj = wadjsb () in
1186 let rec loop pageno pdimno pdim y ph pdims =
1187 if pageno = state.pagecount
1188 then ()
1189 else
1190 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1191 match pdims with
1192 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1193 pdimno+1, pdim, rest
1194 | _ ->
1195 pdimno, pdim, pdims
1197 let x = max 0 (((wadj + state.winw - w) / 2) - xoff) in
1198 let y = y +
1199 (if conf.presentation
1200 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1201 else (if pageno = 0 then 0 else conf.interpagespace)
1204 a.(pageno) <- (pdimno, x, y, pdim);
1205 loop (pageno+1) pdimno pdim (y + h) h pdims
1207 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1208 conf.columns <- Csingle a;
1210 | Cmulti ((columns, coverA, coverB), _) ->
1211 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1212 let rec loop pageno pdimno pdim x y rowh pdims =
1213 let rec fixrow m = if m = pageno then () else
1214 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1215 if h < rowh
1216 then (
1217 let y = y + (rowh - h) / 2 in
1218 a.(m) <- (pdimno, x, y, pdim);
1220 fixrow (m+1)
1222 if pageno = state.pagecount
1223 then fixrow (((pageno - 1) / columns) * columns)
1224 else
1225 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1226 match pdims with
1227 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1228 pdimno+1, pdim, rest
1229 | _ ->
1230 pdimno, pdim, pdims
1232 let x, y, rowh' =
1233 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1234 then (
1235 let x = (wadj + state.winw - w) / 2 in
1236 let ips =
1237 if conf.presentation then calcips h else conf.interpagespace in
1238 x, y + ips + rowh, h
1240 else (
1241 if (pageno - coverA) mod columns = 0
1242 then (
1243 let x = max 0 (wadj + state.winw - state.w) / 2 in
1244 let y =
1245 if conf.presentation
1246 then
1247 let ips = calcips h in
1248 y + (if pageno = 0 then 0 else calcips rowh + ips)
1249 else
1250 y + (if pageno = 0 then 0 else conf.interpagespace)
1252 x, y + rowh, h
1254 else x, y, max rowh h
1257 let y =
1258 if pageno > 1 && (pageno - coverA) mod columns = 0
1259 then (
1260 let y =
1261 if pageno = columns && conf.presentation
1262 then (
1263 let ips = calcips rowh in
1264 for i = 0 to pred columns
1266 let (pdimno, x, y, pdim) = a.(i) in
1267 a.(i) <- (pdimno, x, y+ips, pdim)
1268 done;
1269 y+ips;
1271 else y
1273 fixrow (pageno - columns);
1276 else y
1278 a.(pageno) <- (pdimno, x, y, pdim);
1279 let x = x + w + xoff*2 + conf.interpagespace in
1280 loop (pageno+1) pdimno pdim x y rowh' pdims
1282 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1283 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1285 | Csplit (c, _) ->
1286 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1287 let rec loop pageno pdimno pdim y pdims =
1288 if pageno = state.pagecount
1289 then ()
1290 else
1291 let pdimno, ((_, w, h, _) as pdim), pdims =
1292 match pdims with
1293 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1294 pdimno+1, pdim, rest
1295 | _ ->
1296 pdimno, pdim, pdims
1298 let cw = w / c in
1299 let rec loop1 n x y =
1300 if n = c then y else (
1301 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1302 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1305 let y = loop1 0 0 y in
1306 loop (pageno+1) pdimno pdim y pdims
1308 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1309 conf.columns <- Csplit (c, a);
1312 let represent () =
1313 docolumns conf.columns;
1314 state.maxy <- calcheight ();
1315 if state.reprf == noreprf
1316 then (
1317 match state.mode with
1318 | Birdseye (_, _, pageno, _, _) ->
1319 let y, h = getpageyh pageno in
1320 let top = (state.winh - h) / 2 in
1321 gotoy (max 0 (y - top))
1322 | Textentry _
1323 | View
1324 | LinkNav _ ->
1325 let y = getanchory state.anchor in
1326 let y = min y (state.maxy - state.winw - hscrollh ()) in
1327 gotoy y;
1329 else (
1330 state.reprf ();
1331 state.reprf <- noreprf;
1335 let reshape ?(firsttime=false) w h =
1336 GlDraw.viewport ~x:0 ~y:0 ~w:w ~h:h;
1337 if not firsttime && nogeomcmds state.geomcmds
1338 then state.anchor <- getanchor ();
1340 state.winw <- w;
1341 let w = wadjsb () + (truncate (float w *. conf.zoom)) in
1342 let w = max w 2 in
1343 state.winh <- h;
1344 setfontsize fstate.fontsize;
1345 GlMat.mode `modelview;
1346 GlMat.load_identity ();
1348 GlMat.mode `projection;
1349 GlMat.load_identity ();
1350 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1351 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1352 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
1354 let relx =
1355 if conf.zoom <= 1.0
1356 then 0.0
1357 else float state.x /. float state.w
1359 invalidate "geometry"
1360 (fun () ->
1361 state.w <- w;
1362 if not firsttime
1363 then state.x <- truncate (relx *. float w);
1364 let w =
1365 match conf.columns with
1366 | Csingle _ -> w
1367 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1368 | Csplit (c, _) -> w * c
1370 wcmd "geometry %d %d %d"
1371 w (stateh h) (FMTE.to_int conf.fitmodel)
1375 let enttext () =
1376 let len = String.length state.text in
1377 let x0 = xadjsb () in
1378 let drawstring s =
1379 let hscrollh =
1380 match state.mode with
1381 | Textentry _ | View | LinkNav _ ->
1382 let h, _, _ = state.uioh#scrollpw in
1384 | Birdseye _ -> 0
1386 let rect x w =
1387 filledrect x (float (state.winh - (fstate.fontsize + 4) - hscrollh))
1388 (x+.w) (float (state.winh - hscrollh))
1391 let w = float (wadjsb () + state.winw - 1) in
1392 if state.progress >= 0.0 && state.progress < 1.0
1393 then (
1394 GlDraw.color (0.3, 0.3, 0.3);
1395 let w1 = w *. state.progress in
1396 rect (float x0) w1;
1397 GlDraw.color (0.0, 0.0, 0.0);
1398 rect (float x0+.w1) (float x0+.w-.w1)
1400 else (
1401 GlDraw.color (0.0, 0.0, 0.0);
1402 rect (float x0) w;
1405 GlDraw.color (1.0, 1.0, 1.0);
1406 drawstring fstate.fontsize
1407 (if conf.leftscroll then x0 + 2 else x0 + if len > 0 then 8 else 2)
1408 (state.winh - hscrollh - 5) s;
1410 let s =
1411 match state.mode with
1412 | Textentry ((prefix, text, _, _, _, _), _) ->
1413 let s =
1414 if len > 0
1415 then
1416 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1417 else
1418 Printf.sprintf "%s%s_" prefix text
1422 | Birdseye _
1423 | View
1424 | LinkNav _ -> state.text
1426 let s =
1427 if state.newerrmsgs
1428 then (
1429 if not (istextentry state.mode) && state.uioh#eformsgs
1430 then
1431 let s1 = "(press 'e' to review error messasges)" in
1432 if nonemptystr s then s ^ " " ^ s1 else s1
1433 else s
1435 else s
1437 if nonemptystr s
1438 then drawstring s
1441 let gctiles () =
1442 let len = Queue.length state.tilelru in
1443 let layout = lazy (
1444 match state.throttle with
1445 | None ->
1446 if conf.preload
1447 then preloadlayout state.x state.y state.winw state.winh
1448 else state.layout
1449 | Some (layout, _, _) ->
1450 layout
1451 ) in
1452 let rec loop qpos =
1453 if state.memused <= conf.memlimit
1454 then ()
1455 else (
1456 if qpos < len
1457 then
1458 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1459 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1460 let (_, pw, ph, _) = getpagedim n in
1462 gen = state.gen
1463 && colorspace = conf.colorspace
1464 && angle = conf.angle
1465 && pagew = pw
1466 && pageh = ph
1467 && (
1468 let x = col*conf.tilew
1469 and y = row*conf.tileh in
1470 tilevisible (Lazy.force_val layout) n x y
1472 then Queue.push lruitem state.tilelru
1473 else (
1474 freepbo p;
1475 wcmd "freetile %s" (~> p);
1476 state.memused <- state.memused - s;
1477 state.uioh#infochanged Memused;
1478 Hashtbl.remove state.tilemap k;
1480 loop (qpos+1)
1483 loop 0
1486 let onpagerect pageno f =
1487 let b =
1488 match conf.columns with
1489 | Cmulti (_, b) -> b
1490 | Csingle b -> b
1491 | Csplit (_, b) -> b
1493 if pageno >= 0 && pageno < Array.length b
1494 then
1495 let (_, _, _, (_, w, h, _)) = b.(pageno) in
1496 f w h
1499 let gotopagexy1 wtmode pageno x y =
1500 let _,w1,h1,leftx = getpagedim pageno in
1501 let top = y /. (float h1) in
1502 let left = x /. (float w1) in
1503 let py, w, h = getpageywh pageno in
1504 let wh = state.winh - hscrollh () in
1505 let x = left *. (float w) in
1506 let x = leftx + state.x + truncate x in
1507 let wadj = wadjsb () in
1508 let sx =
1509 if x < 0 || x >= wadj + state.winw
1510 then state.x - x
1511 else state.x
1513 let pdy = truncate (top *. float h) in
1514 let y' = py + pdy in
1515 let dy = y' - state.y in
1516 let sy =
1517 if x != state.x || not (dy > 0 && dy < wh)
1518 then (
1519 if conf.presentation
1520 then
1521 if abs (py - y') > wh
1522 then y'
1523 else py
1524 else y';
1526 else state.y
1528 if state.x != sx || state.y != sy
1529 then (
1530 let x, y =
1531 if wtmode
1532 then (
1533 let ww = wadj + state.winw in
1534 let qx = sx / ww
1535 and qy = pdy / wh in
1536 let x = qx * ww
1537 and y = py + qy * wh in
1538 let x = if -x + ww > w1 then -(w1-ww) else x
1539 and y' = if y + wh > state.maxy then state.maxy - wh else y in
1540 let y =
1541 if conf.presentation
1542 then
1543 if abs (py - y') > wh
1544 then y'
1545 else py
1546 else y';
1548 (x, y)
1550 else (sx, sy)
1552 state.x <- x;
1553 gotoy_and_clear_text y;
1555 else gotoy_and_clear_text state.y;
1558 let gotopagexy wtmode pageno x y =
1559 match state.mode with
1560 | Birdseye _ -> gotopage pageno 0.0
1561 | Textentry _
1562 | View
1563 | LinkNav _ -> gotopagexy1 wtmode pageno x y
1566 let getpassword () =
1567 let passcmd = getenvwithdef "LLPP_ASKPASS" conf.passcmd in
1568 if emptystr passcmd
1569 then E.s
1570 else getcmdoutput
1571 (fun s ->
1572 impmsg "error getting password: %s" s;
1573 dolog "%s" s) passcmd;
1576 let pgoto opaque pageno x y =
1577 let pdimno = getpdimno pageno in
1578 let x, y = project opaque pageno pdimno x y in
1579 gotopagexy false pageno x y;
1582 let act cmds =
1583 (* dolog "%S" cmds; *)
1584 let cl = splitatspace cmds in
1585 let scan s fmt f =
1586 try Scanf.sscanf s fmt f
1587 with exn ->
1588 dolog "error processing '%S': %s" cmds @@ exntos exn;
1589 exit 1
1591 let addoutline outline =
1592 match state.currently with
1593 | Outlining outlines ->
1594 state.currently <- Outlining (outline :: outlines)
1595 | Idle -> state.currently <- Outlining [outline]
1596 | Loading _
1597 | Tiling _ ->
1598 dolog "invalid outlining state";
1599 logcurrently state.currently
1601 match cl with
1602 | "clear" :: [] ->
1603 state.uioh#infochanged Pdim;
1604 state.pdims <- [];
1606 | "clearrects" :: [] ->
1607 state.rects <- state.rects1;
1608 G.postRedisplay "clearrects";
1610 | "continue" :: args :: [] ->
1611 let n = scan args "%u" (fun n -> n) in
1612 state.pagecount <- n;
1613 begin match state.currently with
1614 | Outlining l ->
1615 state.currently <- Idle;
1616 state.outlines <- Array.of_list (List.rev l)
1617 | Idle
1618 | Loading _
1619 | Tiling _ -> ()
1620 end;
1622 let cur, cmds = state.geomcmds in
1623 if emptystr cur
1624 then failwith "umpossible";
1626 begin match List.rev cmds with
1627 | [] ->
1628 state.geomcmds <- E.s, [];
1629 state.throttle <- None;
1630 represent ();
1631 | (s, f) :: rest ->
1632 f ();
1633 state.geomcmds <- s, List.rev rest;
1634 end;
1635 if conf.maxwait = None && not !wtmode
1636 then G.postRedisplay "continue";
1638 | "msg" :: args :: [] ->
1639 showtext ' ' args
1641 | "vmsg" :: args :: [] ->
1642 if conf.verbose
1643 then showtext ' ' args
1645 | "emsg" :: args :: [] ->
1646 Buffer.add_string state.errmsgs args;
1647 state.newerrmsgs <- true;
1648 G.postRedisplay "error message"
1650 | "progress" :: args :: [] ->
1651 let progress, text =
1652 scan args "%f %n"
1653 (fun f pos ->
1654 f, String.sub args pos (String.length args - pos))
1656 state.text <- text;
1657 state.progress <- progress;
1658 G.postRedisplay "progress"
1660 | "firstmatch" :: args :: [] ->
1661 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1662 scan args "%u %d %f %f %f %f %f %f %f %f"
1663 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1664 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1666 let xoff = float (xadjsb ()) in
1667 let x0 = x0 +. xoff
1668 and x1 = x1 +. xoff
1669 and x2 = x2 +. xoff
1670 and x3 = x3 +. xoff in
1671 let y = (getpagey pageno) + truncate y0 in
1672 if conf.zoom > 1.0
1673 then state.x <- truncate (xoff -. x0) + state.winw/2;
1674 addnav ();
1675 gotoy y;
1676 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1677 state.rects1 <- [pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)]
1679 | "match" :: args :: [] ->
1680 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1681 scan args "%u %d %f %f %f %f %f %f %f %f"
1682 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1683 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1685 let xoff = float (xadjsb ()) in
1686 let x0 = x0 +. xoff
1687 and x1 = x1 +. xoff
1688 and x2 = x2 +. xoff
1689 and x3 = x3 +. xoff in
1690 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1691 state.rects1 <-
1692 (pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1694 | "page" :: args :: [] ->
1695 let pageopaques, t = scan args "%s %f" (fun p t -> p, t) in
1696 let pageopaque = ~< pageopaques in
1697 begin match state.currently with
1698 | Loading (l, gen) ->
1699 vlog "page %d took %f sec" l.pageno t;
1700 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1701 begin match state.throttle with
1702 | None ->
1703 let preloadedpages =
1704 if conf.preload
1705 then preloadlayout state.x state.y state.winw state.winh
1706 else state.layout
1708 let evict () =
1709 let set =
1710 List.fold_left (fun s l -> IntSet.add l.pageno s)
1711 IntSet.empty preloadedpages
1713 let evictedpages =
1714 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1715 if not (IntSet.mem pageno set)
1716 then (
1717 wcmd "freepage %s" (~> opaque);
1718 key :: accu
1720 else accu
1721 ) state.pagemap []
1723 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1725 evict ();
1726 state.currently <- Idle;
1727 if gen = state.gen
1728 then (
1729 tilepage l.pageno pageopaque state.layout;
1730 load state.layout;
1731 load preloadedpages;
1732 let visible = pagevisible state.layout l.pageno in
1733 if visible
1734 then (
1735 match state.mode with
1736 | LinkNav (Ltnotready (pageno, dir)) ->
1737 if pageno = l.pageno
1738 then (
1739 let link =
1740 let ld =
1741 if dir = 0
1742 then LDfirstvisible (l.pagex, l.pagey, dir)
1743 else (
1744 if dir > 0 then LDfirst else LDlast
1747 findlink pageopaque ld
1749 match link with
1750 | Lnotfound -> ()
1751 | Lfound n ->
1752 showlinktype (getlink pageopaque n);
1753 state.mode <- LinkNav (Ltexact (l.pageno, n))
1755 | LinkNav (Ltgendir _)
1756 | LinkNav (Ltexact _)
1757 | View
1758 | Birdseye _
1759 | Textentry _ -> ()
1762 if visible && layoutready state.layout
1763 then (
1764 G.postRedisplay "page";
1768 | Some (layout, _, _) ->
1769 state.currently <- Idle;
1770 tilepage l.pageno pageopaque layout;
1771 load state.layout
1772 end;
1774 | Idle
1775 | Tiling _
1776 | Outlining _ ->
1777 dolog "Inconsistent loading state";
1778 logcurrently state.currently;
1779 exit 1
1782 | "tile" :: args :: [] ->
1783 let (x, y, opaques, size, t) =
1784 scan args "%u %u %s %u %f"
1785 (fun x y p size t -> (x, y, p, size, t))
1787 let opaque = ~< opaques in
1788 begin match state.currently with
1789 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1790 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1792 unmappbo opaque;
1793 if tilew != conf.tilew || tileh != conf.tileh
1794 then (
1795 wcmd "freetile %s" (~> opaque);
1796 state.currently <- Idle;
1797 load state.layout;
1799 else (
1800 puttileopaque l col row gen cs angle opaque size t;
1801 state.memused <- state.memused + size;
1802 state.uioh#infochanged Memused;
1803 gctiles ();
1804 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1805 opaque, size) state.tilelru;
1807 let layout =
1808 match state.throttle with
1809 | None -> state.layout
1810 | Some (layout, _, _) -> layout
1813 state.currently <- Idle;
1814 if gen = state.gen
1815 && conf.colorspace = cs
1816 && conf.angle = angle
1817 && tilevisible layout l.pageno x y
1818 then conttiling l.pageno pageopaque;
1820 begin match state.throttle with
1821 | None ->
1822 preload state.layout;
1823 if gen = state.gen
1824 && conf.colorspace = cs
1825 && conf.angle = angle
1826 && tilevisible state.layout l.pageno x y
1827 && (not !wtmode || layoutready state.layout)
1828 then G.postRedisplay "tile nothrottle";
1830 | Some (layout, y, _) ->
1831 let ready = layoutready layout in
1832 if ready
1833 then (
1834 state.y <- y;
1835 state.layout <- layout;
1836 state.throttle <- None;
1837 G.postRedisplay "throttle";
1839 else load layout;
1840 end;
1843 | Idle
1844 | Loading _
1845 | Outlining _ ->
1846 dolog "Inconsistent tiling state";
1847 logcurrently state.currently;
1848 exit 1
1851 | "pdim" :: args :: [] ->
1852 let (n, w, h, _) as pdim =
1853 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1855 let pdim =
1856 match conf.fitmodel with
1857 | FitWidth -> pdim
1858 | FitPage | FitProportional ->
1859 match conf.columns with
1860 | Csplit _ -> (n, w, h, 0)
1861 | Csingle _ | Cmulti _ -> pdim
1863 state.uioh#infochanged Pdim;
1864 state.pdims <- pdim :: state.pdims
1866 | "o" :: args :: [] ->
1867 let (l, n, t, h, pos) =
1868 scan args "%u %u %d %u %n"
1869 (fun l n t h pos -> l, n, t, h, pos)
1871 let s = String.sub args pos (String.length args - pos) in
1872 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
1874 | "ou" :: args :: [] ->
1875 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
1876 let s = String.sub args pos len in
1877 let pos2 = pos + len + 1 in
1878 let uri = String.sub args pos2 (String.length args - pos2) in
1879 addoutline (s, l, Ouri uri)
1881 | "on" :: args :: [] ->
1882 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
1883 let s = String.sub args pos (String.length args - pos) in
1884 addoutline (s, l, Onone)
1886 | "a" :: args :: [] ->
1887 let (n, l, t) =
1888 scan args "%u %d %d" (fun n l t -> n, l, t)
1890 state.reprf <- (fun () -> gotopagexy !wtmode n (float l) (float t))
1892 | "info" :: args :: [] ->
1893 let pos = nindex args '\t' in
1894 if pos >= 0 && String.sub args 0 pos = "Title"
1895 then (
1896 let s = String.sub args (pos+1) @@ String.length args - pos - 1 in
1897 conf.title <- s;
1898 Wsi.settitle s;
1900 state.docinfo <- (1, args) :: state.docinfo
1902 | "infoend" :: [] ->
1903 state.uioh#infochanged Docinfo;
1904 state.docinfo <- List.rev state.docinfo
1906 | "pass" :: l ->
1907 if l = "fail" :: []
1908 then Wsi.settitle "Wrong password";
1909 let password = getpassword () in
1910 if emptystr password
1911 then error "document is password protected"
1912 else opendoc state.path password
1913 | _ ->
1914 error "unknown cmd `%S'" cmds
1917 let onhist cb =
1918 let rc = cb.rc in
1919 let action = function
1920 | HCprev -> cbget cb ~-1
1921 | HCnext -> cbget cb 1
1922 | HCfirst -> cbget cb ~-(cb.rc)
1923 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1924 and cancel () = cb.rc <- rc
1925 in (action, cancel)
1928 let search pattern forward =
1929 match conf.columns with
1930 | Csplit _ -> impmsg "searching does not work properly in split columns mode"
1931 | Csingle _
1932 | Cmulti _ ->
1933 if nonemptystr pattern
1934 then
1935 let pn, py =
1936 match state.layout with
1937 | [] -> 0, 0
1938 | l :: _ ->
1939 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1941 wcmd "search %d %d %d %d,%s\000"
1942 (btod conf.icase) pn py (btod forward) pattern;
1945 let intentry text key =
1946 let c =
1947 if key >= 32 && key < 127
1948 then Char.chr key
1949 else '\000'
1951 match c with
1952 | '0' .. '9' ->
1953 let text = addchar text c in
1954 TEcont text
1956 | _ ->
1957 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1958 TEcont text
1961 let linknact f s =
1962 if nonemptystr s
1963 then (
1964 let n =
1965 let l = String.length s in
1966 let rec loop pos n = if pos = l then n else
1967 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
1968 loop (pos+1) (n*26 + m)
1969 in loop 0 0
1971 let rec loop n = function
1972 | [] -> ()
1973 | l :: rest ->
1974 match getopaque l.pageno with
1975 | None -> loop n rest
1976 | Some opaque ->
1977 let m = getlinkcount opaque in
1978 if n < m
1979 then (
1980 let under = getlink opaque n in
1981 f under
1983 else loop (n-m) rest
1985 loop n state.layout;
1989 let linknentry text key =
1990 let c =
1991 if key >= 32 && key < 127
1992 then Char.chr key
1993 else '\000'
1995 match c with
1996 | 'a' .. 'z' ->
1997 let text = addchar text c in
1998 linknact (fun under -> state.text <- undertext ~nopath:true under) text;
1999 TEcont text
2001 | _ ->
2002 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2003 TEcont text
2006 let textentry text key =
2007 if key land 0xff00 = 0xff00
2008 then TEcont text
2009 else TEcont (text ^ toutf8 key)
2012 let reqlayout angle fitmodel =
2013 match state.throttle with
2014 | None ->
2015 if nogeomcmds state.geomcmds
2016 then state.anchor <- getanchor ();
2017 conf.angle <- angle mod 360;
2018 if conf.angle != 0
2019 then (
2020 match state.mode with
2021 | LinkNav _ -> state.mode <- View
2022 | Birdseye _
2023 | Textentry _
2024 | View -> ()
2026 conf.fitmodel <- fitmodel;
2027 invalidate "reqlayout"
2028 (fun () ->
2029 wcmd "reqlayout %d %d %d"
2030 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
2032 | _ -> ()
2035 let settrim trimmargins trimfuzz =
2036 if nogeomcmds state.geomcmds
2037 then state.anchor <- getanchor ();
2038 conf.trimmargins <- trimmargins;
2039 conf.trimfuzz <- trimfuzz;
2040 let x0, y0, x1, y1 = trimfuzz in
2041 invalidate "settrim"
2042 (fun () ->
2043 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2044 flushpages ();
2047 let setzoom zoom =
2048 match state.throttle with
2049 | None ->
2050 let zoom = max 0.0001 zoom in
2051 if zoom <> conf.zoom
2052 then (
2053 state.prevzoom <- (conf.zoom, state.x);
2054 conf.zoom <- zoom;
2055 reshape state.winw state.winh;
2056 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2059 | Some (layout, y, started) ->
2060 let time =
2061 match conf.maxwait with
2062 | None -> 0.0
2063 | Some t -> t
2065 let dt = now () -. started in
2066 if dt > time
2067 then (
2068 state.y <- y;
2069 load layout;
2073 let setcolumns mode columns coverA coverB =
2074 state.prevcolumns <- Some (conf.columns, conf.zoom);
2075 if columns < 0
2076 then (
2077 if isbirdseye mode
2078 then impmsg "split mode doesn't work in bird's eye"
2079 else (
2080 conf.columns <- Csplit (-columns, E.a);
2081 state.x <- 0;
2082 conf.zoom <- 1.0;
2085 else (
2086 if columns < 2
2087 then (
2088 conf.columns <- Csingle E.a;
2089 state.x <- 0;
2090 setzoom 1.0;
2092 else (
2093 conf.columns <- Cmulti ((columns, coverA, coverB), E.a);
2094 conf.zoom <- 1.0;
2097 reshape state.winw state.winh;
2100 let resetmstate () =
2101 state.mstate <- Mnone;
2102 Wsi.setcursor Wsi.CURSOR_INHERIT;
2105 let enterbirdseye () =
2106 let zoom = float conf.thumbw /. float state.winw in
2107 let birdseyepageno =
2108 let cy = state.winh / 2 in
2109 let fold = function
2110 | [] -> 0
2111 | l :: rest ->
2112 let rec fold best = function
2113 | [] -> best.pageno
2114 | l :: rest ->
2115 let d = cy - (l.pagedispy + l.pagevh/2)
2116 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2117 if abs d < abs dbest
2118 then fold l rest
2119 else best.pageno
2120 in fold l rest
2122 fold state.layout
2124 state.mode <- Birdseye (
2125 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2127 resetmstate ();
2128 conf.zoom <- zoom;
2129 conf.presentation <- false;
2130 conf.interpagespace <- 10;
2131 conf.hlinks <- false;
2132 conf.fitmodel <- FitPage;
2133 state.x <- 0;
2134 conf.maxwait <- None;
2135 conf.columns <- (
2136 match conf.beyecolumns with
2137 | Some c ->
2138 conf.zoom <- 1.0;
2139 Cmulti ((c, 0, 0), E.a)
2140 | None -> Csingle E.a
2142 if conf.verbose
2143 then
2144 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2145 (100.0*.zoom)
2146 else
2147 state.text <- E.s
2149 reshape state.winw state.winh;
2152 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2153 state.mode <- View;
2154 conf.zoom <- c.zoom;
2155 conf.presentation <- c.presentation;
2156 conf.interpagespace <- c.interpagespace;
2157 conf.maxwait <- c.maxwait;
2158 conf.hlinks <- c.hlinks;
2159 conf.fitmodel <- c.fitmodel;
2160 conf.beyecolumns <- (
2161 match conf.columns with
2162 | Cmulti ((c, _, _), _) -> Some c
2163 | Csingle _ -> None
2164 | Csplit _ -> failwith "leaving bird's eye split mode"
2166 conf.columns <- (
2167 match c.columns with
2168 | Cmulti (c, _) -> Cmulti (c, E.a)
2169 | Csingle _ -> Csingle E.a
2170 | Csplit (c, _) -> Csplit (c, E.a)
2172 if conf.verbose
2173 then
2174 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2175 (100.0*.conf.zoom)
2177 reshape state.winw state.winh;
2178 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2179 state.x <- leftx;
2182 let togglebirdseye () =
2183 match state.mode with
2184 | Birdseye vals -> leavebirdseye vals true
2185 | View -> enterbirdseye ()
2186 | Textentry _
2187 | LinkNav _ -> ()
2190 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2191 let pageno = max 0 (pageno - incr) in
2192 let rec loop = function
2193 | [] -> gotopage1 pageno 0
2194 | l :: _ when l.pageno = pageno ->
2195 if l.pagedispy >= 0 && l.pagey = 0
2196 then G.postRedisplay "upbirdseye"
2197 else gotopage1 pageno 0
2198 | _ :: rest -> loop rest
2200 loop state.layout;
2201 state.text <- E.s;
2202 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2205 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2206 let pageno = min (state.pagecount - 1) (pageno + incr) in
2207 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2208 let rec loop = function
2209 | [] ->
2210 let y, h = getpageyh pageno in
2211 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2212 gotoy (clamp dy)
2213 | l :: _ when l.pageno = pageno ->
2214 if l.pagevh != l.pageh
2215 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2216 else G.postRedisplay "downbirdseye"
2217 | _ :: rest -> loop rest
2219 loop state.layout;
2220 state.text <- E.s;
2223 let optentry mode _ key =
2224 let btos b = if b then "on" else "off" in
2225 if key >= 32 && key < 127
2226 then
2227 let c = Char.chr key in
2228 match c with
2229 | 's' ->
2230 let ondone s =
2231 try conf.scrollstep <- int_of_string s with exc ->
2232 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exc
2234 TEswitch ("scroll step: ", E.s, None, intentry, ondone, true)
2236 | 'A' ->
2237 let ondone s =
2239 conf.autoscrollstep <- boundastep state.winh (int_of_string s);
2240 if state.autoscroll <> None
2241 then state.autoscroll <- Some conf.autoscrollstep
2242 with exc ->
2243 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exc
2245 TEswitch ("auto scroll step: ", E.s, None, intentry, ondone, true)
2247 | 'C' ->
2248 let ondone s =
2250 let n, a, b = multicolumns_of_string s in
2251 setcolumns mode n a b;
2252 with exc ->
2253 state.text <- Printf.sprintf "bad columns `%s': %s" s @@ exntos exc
2255 TEswitch ("columns: ", E.s, None, textentry, ondone, true)
2257 | 'Z' ->
2258 let ondone s =
2260 let zoom = float (int_of_string s) /. 100.0 in
2261 setzoom zoom
2262 with exc ->
2263 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exc
2265 TEswitch ("zoom: ", E.s, None, intentry, ondone, true)
2267 | 't' ->
2268 let ondone s =
2270 conf.thumbw <- bound (int_of_string s) 2 4096;
2271 state.text <-
2272 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2273 begin match mode with
2274 | Birdseye beye ->
2275 leavebirdseye beye false;
2276 enterbirdseye ();
2277 | Textentry _
2278 | View
2279 | LinkNav _ -> ();
2281 with exc ->
2282 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exc
2284 TEswitch ("thumbnail width: ", E.s, None, intentry, ondone, true)
2286 | 'R' ->
2287 let ondone s =
2288 match try
2289 Some (int_of_string s)
2290 with exc ->
2291 state.text <-
2292 Printf.sprintf "bad integer `%s': %s" s @@ exntos exc;
2293 None
2294 with
2295 | Some angle -> reqlayout angle conf.fitmodel
2296 | None -> ()
2298 TEswitch ("rotation: ", E.s, None, intentry, ondone, true)
2300 | 'i' ->
2301 conf.icase <- not conf.icase;
2302 TEdone ("case insensitive search " ^ (btos conf.icase))
2304 | 'p' ->
2305 conf.preload <- not conf.preload;
2306 gotoy state.y;
2307 TEdone ("preload " ^ (btos conf.preload))
2309 | 'v' ->
2310 conf.verbose <- not conf.verbose;
2311 TEdone ("verbose " ^ (btos conf.verbose))
2313 | 'd' ->
2314 conf.debug <- not conf.debug;
2315 TEdone ("debug " ^ (btos conf.debug))
2317 | 'h' ->
2318 conf.maxhfit <- not conf.maxhfit;
2319 state.maxy <- calcheight ();
2320 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2322 | 'c' ->
2323 conf.crophack <- not conf.crophack;
2324 TEdone ("crophack " ^ btos conf.crophack)
2326 | 'a' ->
2327 let s =
2328 match conf.maxwait with
2329 | None ->
2330 conf.maxwait <- Some infinity;
2331 "always wait for page to complete"
2332 | Some _ ->
2333 conf.maxwait <- None;
2334 "show placeholder if page is not ready"
2336 TEdone s
2338 | 'f' ->
2339 conf.underinfo <- not conf.underinfo;
2340 TEdone ("underinfo " ^ btos conf.underinfo)
2342 | 'P' ->
2343 conf.savebmarks <- not conf.savebmarks;
2344 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2346 | 'S' ->
2347 let ondone s =
2349 let pageno, py =
2350 match state.layout with
2351 | [] -> 0, 0
2352 | l :: _ ->
2353 l.pageno, l.pagey
2355 conf.interpagespace <- int_of_string s;
2356 docolumns conf.columns;
2357 state.maxy <- calcheight ();
2358 let y = getpagey pageno in
2359 gotoy (y + py)
2360 with exc ->
2361 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exc
2363 TEswitch ("vertical margin: ", E.s, None, intentry, ondone, true)
2365 | 'l' ->
2366 let fm =
2367 match conf.fitmodel with
2368 | FitProportional -> FitWidth
2369 | FitWidth | FitPage -> FitProportional
2371 reqlayout conf.angle fm;
2372 TEdone ("proportional display " ^ btos (fm == FitProportional))
2374 | 'T' ->
2375 settrim (not conf.trimmargins) conf.trimfuzz;
2376 TEdone ("trim margins " ^ btos conf.trimmargins)
2378 | 'I' ->
2379 conf.invert <- not conf.invert;
2380 TEdone ("invert colors " ^ btos conf.invert)
2382 | 'x' ->
2383 let ondone s =
2384 cbput state.hists.sel s;
2385 conf.selcmd <- s;
2387 TEswitch ("selection command: ", E.s, Some (onhist state.hists.sel),
2388 textentry, ondone, true)
2390 | 'M' ->
2391 if conf.pax == None
2392 then conf.pax <- Some (ref (0.0, 0, 0))
2393 else conf.pax <- None;
2394 TEdone ("PAX " ^ btos (conf.pax != None))
2396 | _ ->
2397 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2398 TEstop
2399 else
2400 TEcont state.text
2403 class type lvsource = object
2404 method getitemcount : int
2405 method getitem : int -> (string * int)
2406 method hasaction : int -> bool
2407 method exit :
2408 uioh:uioh ->
2409 cancel:bool ->
2410 active:int ->
2411 first:int ->
2412 pan:int ->
2413 uioh option
2414 method getactive : int
2415 method getfirst : int
2416 method getpan : int
2417 method getminfo : (int * int) array
2418 end;;
2420 class virtual lvsourcebase = object
2421 val mutable m_active = 0
2422 val mutable m_first = 0
2423 val mutable m_pan = 0
2424 method getactive = m_active
2425 method getfirst = m_first
2426 method getpan = m_pan
2427 method getminfo : (int * int) array = E.a
2428 end;;
2430 let textentrykeyboard
2431 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2432 state.text <- E.s;
2433 let key =
2434 if key >= 0xffb0 && key <= 0xffb9
2435 then key - 0xffb0 + 48 else key
2437 let enttext te =
2438 state.mode <- Textentry (te, onleave);
2439 enttext ();
2440 G.postRedisplay "textentrykeyboard enttext";
2442 let histaction cmd =
2443 match opthist with
2444 | None -> ()
2445 | Some (action, _) ->
2446 state.mode <- Textentry (
2447 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
2449 G.postRedisplay "textentry histaction"
2451 match key with
2452 | @backspace ->
2453 if emptystr text && cancelonempty
2454 then (
2455 onleave Cancel;
2456 G.postRedisplay "textentrykeyboard after cancel";
2458 else
2459 let s = withoutlastutf8 text in
2460 enttext (c, s, opthist, onkey, ondone, cancelonempty)
2462 | @enter | @kpenter ->
2463 ondone text;
2464 onleave Confirm;
2465 G.postRedisplay "textentrykeyboard after confirm"
2467 | @up | @kpup -> histaction HCprev
2468 | @down | @kpdown -> histaction HCnext
2469 | @home | @kphome -> histaction HCfirst
2470 | @jend | @kpend -> histaction HClast
2472 | @escape ->
2473 if emptystr text
2474 then (
2475 begin match opthist with
2476 | None -> ()
2477 | Some (_, onhistcancel) -> onhistcancel ()
2478 end;
2479 onleave Cancel;
2480 state.text <- E.s;
2481 G.postRedisplay "textentrykeyboard after cancel2"
2483 else (
2484 enttext (c, E.s, opthist, onkey, ondone, cancelonempty)
2487 | @delete | @kpdelete -> ()
2489 | _ when key != 0
2490 && key land 0xff00 != 0xff00 (* keyboard *)
2491 && key land 0xfe00 != 0xfe00 (* xkb *)
2492 && key land 0xfd00 != 0xfd00 (* 3270 *)
2494 begin match onkey text key with
2495 | TEdone text ->
2496 ondone text;
2497 onleave Confirm;
2498 G.postRedisplay "textentrykeyboard after confirm2";
2500 | TEcont text ->
2501 enttext (c, text, opthist, onkey, ondone, cancelonempty);
2503 | TEstop ->
2504 onleave Cancel;
2505 G.postRedisplay "textentrykeyboard after cancel3"
2507 | TEswitch te ->
2508 state.mode <- Textentry (te, onleave);
2509 G.postRedisplay "textentrykeyboard switch";
2510 end;
2512 | _ ->
2513 vlog "unhandled key %s" (Wsi.keyname key)
2516 let firstof first active =
2517 if first > active || abs (first - active) > fstate.maxrows - 1
2518 then max 0 (active - (fstate.maxrows/2))
2519 else first
2522 let calcfirst first active =
2523 if active > first
2524 then
2525 let rows = active - first in
2526 if rows > fstate.maxrows then active - fstate.maxrows else first
2527 else active
2530 let scrollph y maxy =
2531 let sh = float (maxy + state.winh) /. float state.winh in
2532 let sh = float state.winh /. sh in
2533 let sh = max sh (float conf.scrollh) in
2535 let percent = float y /. float maxy in
2536 let position = (float state.winh -. sh) *. percent in
2538 let position =
2539 if position +. sh > float state.winh
2540 then float state.winh -. sh
2541 else position
2543 position, sh;
2546 let adderrmsg src msg =
2547 Buffer.add_string state.errmsgs msg;
2548 state.newerrmsgs <- true;
2549 G.postRedisplay src
2552 let adderrfmt src fmt =
2553 Format.ksprintf (fun s -> adderrmsg src s) fmt;
2556 let coe s = (s :> uioh);;
2558 class listview ~zebra ~helpmode ~(source:lvsource) ~trusted ~modehash =
2559 object (self)
2560 val m_pan = source#getpan
2561 val m_first = source#getfirst
2562 val m_active = source#getactive
2563 val m_qsearch = E.s
2564 val m_prev_uioh = state.uioh
2566 method private elemunder y =
2567 if y < 0
2568 then None
2569 else
2570 let n = y / (fstate.fontsize+1) in
2571 if m_first + n < source#getitemcount
2572 then (
2573 if source#hasaction (m_first + n)
2574 then Some (m_first + n)
2575 else None
2577 else None
2579 method display =
2580 Gl.enable `blend;
2581 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
2582 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2583 filledrect 0. 0. (float state.winw) (float state.winh);
2584 GlDraw.color (1., 1., 1.);
2585 Gl.enable `texture_2d;
2586 let fs = fstate.fontsize in
2587 let nfs = fs + 1 in
2588 let hw = (wadjsb () + xadjsb () + state.winw)/3 in
2589 let ww = fstate.wwidth in
2590 let tabw = 17.0*.ww in
2591 let itemcount = source#getitemcount in
2592 let minfo = source#getminfo in
2593 let x0, x1 =
2594 if conf.leftscroll
2595 then float (xadjsb ()), float (state.winw - 1)
2596 else 0.0, float (state.winw - conf.scrollbw - 1)
2598 let xadj = xadjsb () in
2599 let rec loop row =
2600 if (row - m_first) > fstate.maxrows
2601 then ()
2602 else (
2603 if row >= 0 && row < itemcount
2604 then (
2605 let (s, level) = source#getitem row in
2606 let y = (row - m_first) * nfs in
2607 let x =
2608 (if conf.leftscroll then float xadj else 5.0)
2609 +. (float (level + m_pan)) *. ww in
2610 if helpmode
2611 then GlDraw.color
2612 (let c = if row land 1 = 0 then 1.0 else 0.92 in (c,c,c));
2614 if row = m_active
2615 then (
2616 Gl.disable `texture_2d;
2617 let alpha = if source#hasaction row then 0.9 else 0.3 in
2618 GlDraw.color (1., 1., 1.) ~alpha;
2619 linerect (x0 +. 1.) (float (y + 1)) (x1) (float (y + fs + 3));
2620 Gl.enable `texture_2d;
2622 let c =
2623 if zebra && row land 1 = 1
2624 then 0.8
2625 else 1.0
2627 GlDraw.color (c,c,c);
2628 let drawtabularstring s =
2629 let drawstr x s =
2630 let x' = truncate (x0 +. x) in
2631 let pos = nindex s '\000' in
2632 if pos = -1
2633 then drawstring1 fs x' (y+nfs) s
2634 else
2635 let s1 = String.sub s 0 pos
2636 and s2 = String.sub s (pos+1) (String.length s - pos - 1) in
2637 let rec e s =
2638 if emptystr s
2639 then s
2640 else
2641 let s' = withoutlastutf8 s in
2642 let s = s' ^ "@Uellipsis" in
2643 let w = measurestr fs s in
2644 if float x' +. w +. ww < float (hw + x')
2645 then s
2646 else e s'
2648 let s1 =
2649 if float x' +. ww +. measurestr fs s1 > float (hw + x')
2650 then e s1
2651 else s1
2653 ignore (drawstring1 fs x' (y+nfs) s1);
2654 drawstring1 fs (hw + x') (y+nfs) s2
2656 if trusted
2657 then
2658 let x = if helpmode && row > 0 then x +. ww else x in
2659 let tabpos = nindex s '\t' in
2660 if tabpos > 0
2661 then
2662 let len = String.length s - tabpos - 1 in
2663 let s1 = String.sub s 0 tabpos
2664 and s2 = String.sub s (tabpos + 1) len in
2665 let nx = drawstr x s1 in
2666 let sw = nx -. x in
2667 let x = x +. (max tabw sw) in
2668 drawstr x s2
2669 else
2670 let len = String.length s - 2 in
2671 if len > 0 && s.[0] = '\xc2' && s.[1] = '\xb7'
2672 then
2673 let s = String.sub s 2 len in
2674 let x = if not helpmode then x +. ww else x in
2675 GlDraw.color (1.2, 1.2, 1.2);
2676 let vinc = drawstring1 (fs+fs/4)
2677 (truncate (x -. ww)) (y+nfs) s in
2678 GlDraw.color (1., 1., 1.);
2679 vinc +. (float fs *. 0.8)
2680 else
2681 drawstr x s
2682 else
2683 drawstr x s
2685 ignore (drawtabularstring s);
2686 loop (row+1)
2690 loop m_first;
2691 GlDraw.color (1.0, 1.0, 1.0) ~alpha:0.5;
2692 let xadj = float (xadjsb () + 5) in
2693 let rec loop row =
2694 if (row - m_first) > fstate.maxrows
2695 then ()
2696 else (
2697 if row >= 0 && row < itemcount
2698 then (
2699 let (s, level) = source#getitem row in
2700 let pos0 = nindex s '\000' in
2701 let y = (row - m_first) * nfs in
2702 let x = float (level + m_pan) *. ww in
2703 let (first, last) = minfo.(row) in
2704 let prefix =
2705 if pos0 > 0 && first > pos0
2706 then String.sub s (pos0+1) (first-pos0-1)
2707 else String.sub s 0 first
2709 let suffix = String.sub s first (last - first) in
2710 let w1 = measurestr fstate.fontsize prefix in
2711 let w2 = measurestr fstate.fontsize suffix in
2712 let x = x +. if conf.leftscroll then xadj else 5.0 in
2713 let x = if pos0 > 0 && first > pos0 then x +. float hw else x in
2714 let x0 = x +. w1
2715 and y0 = float (y+2) in
2716 let x1 = x0 +. w2
2717 and y1 = float (y+fs+3) in
2718 filledrect x0 y0 x1 y1;
2719 loop (row+1)
2723 Gl.disable `texture_2d;
2724 if Array.length minfo > 0 then loop m_first;
2725 Gl.disable `blend;
2727 method updownlevel incr =
2728 let len = source#getitemcount in
2729 let curlevel =
2730 if m_active >= 0 && m_active < len
2731 then snd (source#getitem m_active)
2732 else -1
2734 let rec flow i =
2735 if i = len then i-1 else if i = -1 then 0 else
2736 let _, l = source#getitem i in
2737 if l != curlevel then i else flow (i+incr)
2739 let active = flow m_active in
2740 let first = calcfirst m_first active in
2741 G.postRedisplay "outline updownlevel";
2742 {< m_active = active; m_first = first >}
2744 method private key1 key mask =
2745 let set1 active first qsearch =
2746 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2748 let search active pattern incr =
2749 let active = if active = -1 then m_first else active in
2750 let dosearch re =
2751 let rec loop n =
2752 if n >= 0 && n < source#getitemcount
2753 then (
2754 let s, _ = source#getitem n in
2755 match Str.search_forward re s 0 with
2756 | (exception Not_found) -> loop (n + incr)
2757 | _ -> Some n
2759 else None
2761 loop active
2763 let qpat = Str.quote pattern in
2764 match Str.regexp_case_fold qpat with
2765 | s -> dosearch s
2766 | exception exn ->
2767 adderrfmt "listview key1" "regexp_case_fold for `%S' failed: %S\n"
2768 qpat @@ Printexc.to_string exn;
2769 None
2771 let itemcount = source#getitemcount in
2772 let find start incr =
2773 let rec find i =
2774 if i = -1 || i = itemcount
2775 then -1
2776 else (
2777 if source#hasaction i
2778 then i
2779 else find (i + incr)
2782 find start
2784 let set active first =
2785 let first = bound first 0 (itemcount - fstate.maxrows) in
2786 state.text <- E.s;
2787 coe {< m_active = active; m_first = first; m_qsearch = E.s >}
2789 let navigate incr =
2790 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2791 let active, first =
2792 let incr1 = if incr > 0 then 1 else -1 in
2793 if isvisible m_first m_active
2794 then
2795 let next =
2796 let next = m_active + incr in
2797 let next =
2798 if next < 0 || next >= itemcount
2799 then -1
2800 else find next incr1
2802 if abs (m_active - next) > fstate.maxrows
2803 then -1
2804 else next
2806 if next = -1
2807 then
2808 let first = m_first + incr in
2809 let first = bound first 0 (itemcount - fstate.maxrows) in
2810 let next =
2811 let next = m_active + incr in
2812 let next = bound next 0 (itemcount - 1) in
2813 find next ~-incr1
2815 let active =
2816 if next = -1
2817 then m_active
2818 else (
2819 if isvisible first next
2820 then next
2821 else m_active
2824 active, first
2825 else
2826 let first = min next m_first in
2827 let first =
2828 if abs (next - first) > fstate.maxrows
2829 then first + incr
2830 else first
2832 next, first
2833 else
2834 let first = m_first + incr in
2835 let first = bound first 0 (itemcount - 1) in
2836 let active =
2837 let next = m_active + incr in
2838 let next = bound next 0 (itemcount - 1) in
2839 let next = find next incr1 in
2840 let active =
2841 if next = -1 || abs (m_active - first) > fstate.maxrows
2842 then (
2843 let active = if m_active = -1 then next else m_active in
2844 active
2846 else next
2848 if isvisible first active
2849 then active
2850 else -1
2852 active, first
2854 G.postRedisplay "listview navigate";
2855 set active first;
2857 match key with
2858 | (@r|@s) when Wsi.withctrl mask ->
2859 let incr = if key = @r then -1 else 1 in
2860 let active, first =
2861 match search (m_active + incr) m_qsearch incr with
2862 | None ->
2863 state.text <- m_qsearch ^ " [not found]";
2864 m_active, m_first
2865 | Some active ->
2866 state.text <- m_qsearch;
2867 active, firstof m_first active
2869 G.postRedisplay "listview ctrl-r/s";
2870 set1 active first m_qsearch;
2872 | @insert when Wsi.withctrl mask ->
2873 if m_active >= 0 && m_active < source#getitemcount
2874 then (
2875 let s, _ = source#getitem m_active in
2876 selstring s;
2878 coe self
2880 | @backspace ->
2881 if emptystr m_qsearch
2882 then coe self
2883 else (
2884 let qsearch = withoutlastutf8 m_qsearch in
2885 if emptystr qsearch
2886 then (
2887 state.text <- E.s;
2888 G.postRedisplay "listview empty qsearch";
2889 set1 m_active m_first E.s;
2891 else
2892 let active, first =
2893 match search m_active qsearch ~-1 with
2894 | None ->
2895 state.text <- qsearch ^ " [not found]";
2896 m_active, m_first
2897 | Some active ->
2898 state.text <- qsearch;
2899 active, firstof m_first active
2901 G.postRedisplay "listview backspace qsearch";
2902 set1 active first qsearch
2905 | key when (key != 0 && key land 0xff00 != 0xff00) ->
2906 let pattern = m_qsearch ^ toutf8 key in
2907 let active, first =
2908 match search m_active pattern 1 with
2909 | None ->
2910 state.text <- pattern ^ " [not found]";
2911 m_active, m_first
2912 | Some active ->
2913 state.text <- pattern;
2914 active, firstof m_first active
2916 G.postRedisplay "listview qsearch add";
2917 set1 active first pattern;
2919 | @escape ->
2920 state.text <- E.s;
2921 if emptystr m_qsearch
2922 then (
2923 G.postRedisplay "list view escape";
2924 let mx, my = state.mpos in
2925 updateunder mx my;
2926 begin
2927 match
2928 source#exit ~uioh:(coe self)
2929 ~cancel:true ~active:m_active ~first:m_first ~pan:m_pan
2930 with
2931 | None -> m_prev_uioh
2932 | Some uioh -> uioh
2935 else (
2936 G.postRedisplay "list view kill qsearch";
2937 coe {< m_qsearch = E.s >}
2940 | @enter | @kpenter ->
2941 state.text <- E.s;
2942 let self = {< m_qsearch = E.s >} in
2943 let opt =
2944 G.postRedisplay "listview enter";
2945 if m_active >= 0 && m_active < source#getitemcount
2946 then (
2947 source#exit ~uioh:(coe self) ~cancel:false
2948 ~active:m_active ~first:m_first ~pan:m_pan;
2950 else (
2951 source#exit ~uioh:(coe self) ~cancel:true
2952 ~active:m_active ~first:m_first ~pan:m_pan;
2955 begin match opt with
2956 | None -> m_prev_uioh
2957 | Some uioh -> uioh
2960 | @delete | @kpdelete ->
2961 coe self
2963 | @up | @kpup -> navigate ~-1
2964 | @down | @kpdown -> navigate 1
2965 | @prior | @kpprior -> navigate ~-(fstate.maxrows)
2966 | @next | @kpnext -> navigate fstate.maxrows
2968 | @right | @kpright ->
2969 state.text <- E.s;
2970 G.postRedisplay "listview right";
2971 coe {< m_pan = m_pan - 1 >}
2973 | @left | @kpleft ->
2974 state.text <- E.s;
2975 G.postRedisplay "listview left";
2976 coe {< m_pan = m_pan + 1 >}
2978 | @home | @kphome ->
2979 let active = find 0 1 in
2980 G.postRedisplay "listview home";
2981 set active 0;
2983 | @jend | @kpend ->
2984 let first = max 0 (itemcount - fstate.maxrows) in
2985 let active = find (itemcount - 1) ~-1 in
2986 G.postRedisplay "listview end";
2987 set active first;
2989 | key when (key = 0 || key land 0xff00 = 0xff00) ->
2990 coe self
2992 | _ ->
2993 dolog "listview unknown key %#x" key; coe self
2995 method key key mask =
2996 match state.mode with
2997 | Textentry te -> textentrykeyboard key mask te; coe self
2998 | Birdseye _
2999 | View
3000 | LinkNav _ -> self#key1 key mask
3002 method button button down x y _ =
3003 let opt =
3004 match button with
3005 | 1 when vscrollhit x ->
3006 G.postRedisplay "listview scroll";
3007 if down
3008 then
3009 let _, position, sh = self#scrollph in
3010 if y > truncate position && y < truncate (position +. sh)
3011 then (
3012 state.mstate <- Mscrolly;
3013 Some (coe self)
3015 else
3016 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3017 let first = truncate (s *. float source#getitemcount) in
3018 let first = min source#getitemcount first in
3019 Some (coe {< m_first = first; m_active = first >})
3020 else (
3021 state.mstate <- Mnone;
3022 Some (coe self);
3024 | 1 when down ->
3025 begin match self#elemunder y with
3026 | Some n ->
3027 G.postRedisplay "listview click";
3028 source#exit ~uioh:(coe {< m_active = n >})
3029 ~cancel:false ~active:n ~first:m_first ~pan:m_pan
3030 | _ ->
3031 Some (coe self)
3033 | n when (n == 4 || n == 5) && not down ->
3034 let len = source#getitemcount in
3035 let first =
3036 if n = 5 && m_first + fstate.maxrows >= len
3037 then
3038 m_first
3039 else
3040 let first = m_first + (if n == 4 then -1 else 1) in
3041 bound first 0 (len - 1)
3043 G.postRedisplay "listview wheel";
3044 Some (coe {< m_first = first >})
3045 | n when (n = 6 || n = 7) && not down ->
3046 let inc = if n = 7 then -1 else 1 in
3047 G.postRedisplay "listview hwheel";
3048 Some (coe {< m_pan = m_pan + inc >})
3049 | _ ->
3050 Some (coe self)
3052 match opt with
3053 | None -> m_prev_uioh
3054 | Some uioh -> uioh
3056 method multiclick _ x y = self#button 1 true x y
3058 method motion _ y =
3059 match state.mstate with
3060 | Mscrolly ->
3061 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3062 let first = truncate (s *. float source#getitemcount) in
3063 let first = min source#getitemcount first in
3064 G.postRedisplay "listview motion";
3065 coe {< m_first = first; m_active = first >}
3066 | Msel _
3067 | Mpan _
3068 | Mscrollx
3069 | Mzoom _
3070 | Mzoomrect _
3071 | Mnone -> coe self
3073 method pmotion x y =
3074 if x < state.winw - conf.scrollbw
3075 then
3076 let n =
3077 match self#elemunder y with
3078 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3079 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3081 let o =
3082 if n != m_active
3083 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3084 else self
3086 coe o
3087 else (
3088 Wsi.setcursor Wsi.CURSOR_INHERIT;
3089 coe self
3092 method infochanged _ = ()
3094 method scrollpw = (0, 0.0, 0.0)
3095 method scrollph =
3096 let nfs = fstate.fontsize + 1 in
3097 let y = m_first * nfs in
3098 let itemcount = source#getitemcount in
3099 let maxi = max 0 (itemcount - fstate.maxrows) in
3100 let maxy = maxi * nfs in
3101 let p, h = scrollph y maxy in
3102 conf.scrollbw, p, h
3104 method modehash = modehash
3105 method eformsgs = false
3106 method alwaysscrolly = true
3107 end;;
3109 class outlinelistview ~zebra ~source =
3110 let settext autonarrow s =
3111 if autonarrow
3112 then
3113 let ss = source#statestr in
3114 state.text <-
3115 if emptystr ss
3116 then "[" ^ s ^ "]"
3117 else "{" ^ ss ^ "} [" ^ s ^ "]"
3118 else state.text <- s
3120 object (self)
3121 inherit listview
3122 ~zebra
3123 ~helpmode:false
3124 ~source:(source :> lvsource)
3125 ~trusted:false
3126 ~modehash:(findkeyhash conf "outline")
3127 as super
3129 val m_autonarrow = false
3131 method! key key mask =
3132 let maxrows =
3133 if emptystr state.text
3134 then fstate.maxrows
3135 else fstate.maxrows - 2
3137 let calcfirst first active =
3138 if active > first
3139 then
3140 let rows = active - first in
3141 if rows > maxrows then active - maxrows else first
3142 else active
3144 let navigate incr =
3145 let active = m_active + incr in
3146 let active = bound active 0 (source#getitemcount - 1) in
3147 let first = calcfirst m_first active in
3148 G.postRedisplay "outline navigate";
3149 coe {< m_active = active; m_first = first >}
3151 let navscroll first =
3152 let active =
3153 let dist = m_active - first in
3154 if dist < 0
3155 then first
3156 else (
3157 if dist < maxrows
3158 then m_active
3159 else first + maxrows
3162 G.postRedisplay "outline navscroll";
3163 coe {< m_first = first; m_active = active >}
3165 let ctrl = Wsi.withctrl mask in
3166 match key with
3167 | @a when ctrl ->
3168 let text =
3169 if m_autonarrow
3170 then (source#denarrow; E.s)
3171 else (
3172 let pattern = source#renarrow in
3173 if nonemptystr m_qsearch
3174 then (source#narrow m_qsearch; m_qsearch)
3175 else pattern
3178 settext (not m_autonarrow) text;
3179 G.postRedisplay "toggle auto narrowing";
3180 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
3182 | @slash when emptystr m_qsearch && not m_autonarrow ->
3183 settext true E.s;
3184 G.postRedisplay "toggle auto narrowing";
3185 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
3187 | @n when ctrl ->
3188 source#narrow m_qsearch;
3189 if not m_autonarrow
3190 then source#add_narrow_pattern m_qsearch;
3191 G.postRedisplay "outline ctrl-n";
3192 coe {< m_first = 0; m_active = 0 >}
3194 | @S when ctrl ->
3195 let active = source#calcactive (getanchor ()) in
3196 let first = firstof m_first active in
3197 G.postRedisplay "outline ctrl-s";
3198 coe {< m_first = first; m_active = active >}
3200 | @u when ctrl ->
3201 G.postRedisplay "outline ctrl-u";
3202 if m_autonarrow && nonemptystr m_qsearch
3203 then (
3204 ignore (source#renarrow);
3205 settext m_autonarrow E.s;
3206 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3208 else (
3209 source#del_narrow_pattern;
3210 let pattern = source#renarrow in
3211 let text =
3212 if emptystr pattern then E.s else "Narrowed to " ^ pattern
3214 settext m_autonarrow text;
3215 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3218 | @l when ctrl ->
3219 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3220 G.postRedisplay "outline ctrl-l";
3221 coe {< m_first = first >}
3223 | @tab when m_autonarrow ->
3224 if nonemptystr m_qsearch
3225 then (
3226 G.postRedisplay "outline list view tab";
3227 source#add_narrow_pattern m_qsearch;
3228 settext true E.s;
3229 coe {< m_qsearch = E.s >}
3231 else coe self
3233 | @escape when m_autonarrow ->
3234 if nonemptystr m_qsearch
3235 then source#add_narrow_pattern m_qsearch;
3236 super#key key mask
3238 | @enter | @kpenter when m_autonarrow ->
3239 if nonemptystr m_qsearch
3240 then source#add_narrow_pattern m_qsearch;
3241 super#key key mask
3243 | key when m_autonarrow && (key != 0 && key land 0xff00 != 0xff00) ->
3244 let pattern = m_qsearch ^ toutf8 key in
3245 G.postRedisplay "outlinelistview autonarrow add";
3246 source#narrow pattern;
3247 settext true pattern;
3248 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3250 | key when m_autonarrow && key = @backspace ->
3251 if emptystr m_qsearch
3252 then coe self
3253 else
3254 let pattern = withoutlastutf8 m_qsearch in
3255 G.postRedisplay "outlinelistview autonarrow backspace";
3256 ignore (source#renarrow);
3257 source#narrow pattern;
3258 settext true pattern;
3259 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3261 | @up | @kpup when ctrl ->
3262 navscroll (max 0 (m_first - 1))
3264 | @down | @kpdown when ctrl ->
3265 navscroll (min (source#getitemcount - 1) (m_first + 1))
3267 | @up | @kpup -> navigate ~-1
3268 | @down | @kpdown -> navigate 1
3269 | @prior | @kpprior -> navigate ~-(fstate.maxrows)
3270 | @next | @kpnext -> navigate fstate.maxrows
3272 | @right | @kpright ->
3273 let o =
3274 if ctrl
3275 then (
3276 G.postRedisplay "outline ctrl right";
3277 {< m_pan = m_pan + 1 >}
3279 else self#updownlevel 1
3281 coe o
3283 | @left | @kpleft ->
3284 let o =
3285 if ctrl
3286 then (
3287 G.postRedisplay "outline ctrl left";
3288 {< m_pan = m_pan - 1 >}
3290 else self#updownlevel ~-1
3292 coe o
3294 | @home | @kphome ->
3295 G.postRedisplay "outline home";
3296 coe {< m_first = 0; m_active = 0 >}
3298 | @jend | @kpend ->
3299 let active = source#getitemcount - 1 in
3300 let first = max 0 (active - fstate.maxrows) in
3301 G.postRedisplay "outline end";
3302 coe {< m_active = active; m_first = first >}
3304 | _ -> super#key key mask
3305 end;;
3307 let genhistoutlines () =
3308 Config.gethist ()
3309 |> List.sort (fun (_, c1, _, _, _, _) (_, c2, _, _, _, _) ->
3310 compare c2.lastvisit c1.lastvisit)
3311 |> List.map
3312 (fun ((path, c, _, _, _, origin) as hist) ->
3313 let path = if nonemptystr origin then origin else path in
3314 let base = mbtoutf8 @@ Filename.basename path in
3315 (base ^ "\000" ^ c.title, 1, Ohistory hist)
3317 |> Array.of_list
3320 let gotohist (path, c, bookmarks, x, anchor, origin) =
3321 Config.save leavebirdseye;
3322 state.anchor <- anchor;
3323 state.bookmarks <- bookmarks;
3324 state.origin <- origin;
3325 state.x <- x;
3326 setconf conf c;
3327 let x0, y0, x1, y1 = conf.trimfuzz in
3328 wcmd "trimset %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1;
3329 reshape ~firsttime:true state.winw state.winh;
3330 opendoc path origin;
3331 setzoom c.zoom;
3334 let makecheckers () =
3335 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3336 following to say:
3337 converted by Issac Trotts. July 25, 2002 *)
3338 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3339 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3340 let id = GlTex.gen_texture () in
3341 GlTex.bind_texture ~target:`texture_2d id;
3342 GlPix.store (`unpack_alignment 1);
3343 GlTex.image2d image;
3344 List.iter (GlTex.parameter ~target:`texture_2d)
3345 [ `mag_filter `nearest; `min_filter `nearest ];
3349 let setcheckers enabled =
3350 match state.checkerstexid with
3351 | None ->
3352 if enabled then state.checkerstexid <- Some (makecheckers ())
3354 | Some checkerstexid ->
3355 if not enabled
3356 then (
3357 GlTex.delete_texture checkerstexid;
3358 state.checkerstexid <- None;
3362 let describe_location () =
3363 let fn = page_of_y state.y in
3364 let ln = page_of_y (state.y + state.winh - hscrollh () - 1) in
3365 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
3366 let percent =
3367 if maxy <= 0
3368 then 100.
3369 else (100. *. (float state.y /. float maxy))
3371 if fn = ln
3372 then
3373 Printf.sprintf "page %d of %d [%.2f%%]"
3374 (fn+1) state.pagecount percent
3375 else
3376 Printf.sprintf
3377 "pages %d-%d of %d [%.2f%%]"
3378 (fn+1) (ln+1) state.pagecount percent
3381 let setpresentationmode v =
3382 let n = page_of_y state.y in
3383 state.anchor <- (n, 0.0, 1.0);
3384 conf.presentation <- v;
3385 if conf.fitmodel = FitPage
3386 then reqlayout conf.angle conf.fitmodel;
3387 represent ();
3390 let setbgcol (r, g, b) =
3391 let col =
3392 let r = r *. 255.0 |> truncate
3393 and g = g *. 255.0 |> truncate
3394 and b = b *. 255.0 |> truncate in
3395 r lsl 16 |> (lor) (g lsl 8) |> (lor) b
3397 Wsi.setwinbgcol col;
3400 let enterinfomode =
3401 let btos b = if b then "@Uradical" else E.s in
3402 let showextended = ref false in
3403 let leave mode _ = state.mode <- mode in
3404 let src =
3405 (object
3406 val mutable m_l = []
3407 val mutable m_a = E.a
3408 val mutable m_prev_uioh = nouioh
3409 val mutable m_prev_mode = View
3411 inherit lvsourcebase
3413 method reset prev_mode prev_uioh =
3414 m_a <- Array.of_list (List.rev m_l);
3415 m_l <- [];
3416 m_prev_mode <- prev_mode;
3417 m_prev_uioh <- prev_uioh;
3419 method int name get set =
3420 m_l <-
3421 (name, `int get, 1, Action (
3422 fun u ->
3423 let ondone s =
3424 try set (int_of_string s)
3425 with exn ->
3426 state.text <- Printf.sprintf "bad integer `%s': %s"
3427 s @@ exntos exn
3429 state.text <- E.s;
3430 let te = name ^ ": ", E.s, None, intentry, ondone, true in
3431 state.mode <- Textentry (te, leave m_prev_mode);
3433 )) :: m_l
3435 method int_with_suffix name get set =
3436 m_l <-
3437 (name, `intws get, 1, Action (
3438 fun u ->
3439 let ondone s =
3440 try set (int_of_string_with_suffix s)
3441 with exn ->
3442 state.text <- Printf.sprintf "bad integer `%s': %s"
3443 s @@ exntos exn
3445 state.text <- E.s;
3446 let te =
3447 name ^ ": ", E.s, None, intentry_with_suffix, ondone, true
3449 state.mode <- Textentry (te, leave m_prev_mode);
3451 )) :: m_l
3453 method bool ?(offset=1) ?(btos=btos) name get set =
3454 m_l <-
3455 (name, `bool (btos, get), offset, Action (
3456 fun u ->
3457 let v = get () in
3458 set (not v);
3460 )) :: m_l
3462 method color name get set =
3463 m_l <-
3464 (name, `color get, 1, Action (
3465 fun u ->
3466 let invalid = (nan, nan, nan) in
3467 let ondone s =
3468 let c =
3469 try color_of_string s
3470 with exn ->
3471 state.text <- Printf.sprintf "bad color `%s': %s"
3472 s @@ exntos exn;
3473 invalid
3475 if c <> invalid
3476 then set c;
3478 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3479 state.text <- color_to_string (get ());
3480 state.mode <- Textentry (te, leave m_prev_mode);
3482 )) :: m_l
3484 method string name get set =
3485 m_l <-
3486 (name, `string get, 1, Action (
3487 fun u ->
3488 let ondone s = set s in
3489 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3490 state.mode <- Textentry (te, leave m_prev_mode);
3492 )) :: m_l
3494 method colorspace name get set =
3495 m_l <-
3496 (name, `string get, 1, Action (
3497 fun _ ->
3498 let source =
3499 (object
3500 inherit lvsourcebase
3502 initializer
3503 m_active <- CSTE.to_int conf.colorspace;
3504 m_first <- 0;
3506 method getitemcount =
3507 Array.length CSTE.names
3508 method getitem n =
3509 (CSTE.names.(n), 0)
3510 method exit ~uioh ~cancel ~active ~first ~pan =
3511 ignore (uioh, first, pan);
3512 if not cancel then set active;
3513 None
3514 method hasaction _ = true
3515 end)
3517 state.text <- E.s;
3518 let modehash = findkeyhash conf "info" in
3519 coe (new listview ~zebra:false ~helpmode:false
3520 ~source ~trusted:true ~modehash)
3521 )) :: m_l
3523 method paxmark name get set =
3524 m_l <-
3525 (name, `string get, 1, Action (
3526 fun _ ->
3527 let source =
3528 (object
3529 inherit lvsourcebase
3531 initializer
3532 m_active <- MTE.to_int conf.paxmark;
3533 m_first <- 0;
3535 method getitemcount = Array.length MTE.names
3536 method getitem n = (MTE.names.(n), 0)
3537 method exit ~uioh ~cancel ~active ~first ~pan =
3538 ignore (uioh, first, pan);
3539 if not cancel then set active;
3540 None
3541 method hasaction _ = true
3542 end)
3544 state.text <- E.s;
3545 let modehash = findkeyhash conf "info" in
3546 coe (new listview ~zebra:false ~helpmode:false
3547 ~source ~trusted:true ~modehash)
3548 )) :: m_l
3550 method fitmodel name get set =
3551 m_l <-
3552 (name, `string get, 1, Action (
3553 fun _ ->
3554 let source =
3555 (object
3556 inherit lvsourcebase
3558 initializer
3559 m_active <- FMTE.to_int conf.fitmodel;
3560 m_first <- 0;
3562 method getitemcount = Array.length FMTE.names
3563 method getitem n = (FMTE.names.(n), 0)
3564 method exit ~uioh ~cancel ~active ~first ~pan =
3565 ignore (uioh, first, pan);
3566 if not cancel then set active;
3567 None
3568 method hasaction _ = true
3569 end)
3571 state.text <- E.s;
3572 let modehash = findkeyhash conf "info" in
3573 coe (new listview ~zebra:false ~helpmode:false
3574 ~source ~trusted:true ~modehash)
3575 )) :: m_l
3577 method caption s offset =
3578 m_l <- (s, `empty, offset, Noaction) :: m_l
3580 method caption2 s f offset =
3581 m_l <- (s, `string f, offset, Noaction) :: m_l
3583 method getitemcount = Array.length m_a
3585 method getitem n =
3586 let tostr = function
3587 | `int f -> string_of_int (f ())
3588 | `intws f -> string_with_suffix_of_int (f ())
3589 | `string f -> f ()
3590 | `color f -> color_to_string (f ())
3591 | `bool (btos, f) -> btos (f ())
3592 | `empty -> E.s
3594 let name, t, offset, _ = m_a.(n) in
3595 ((let s = tostr t in
3596 if nonemptystr s
3597 then Printf.sprintf "%s\t%s" name s
3598 else name),
3599 offset)
3601 method exit ~uioh ~cancel ~active ~first ~pan =
3602 let uiohopt =
3603 if not cancel
3604 then (
3605 let uioh =
3606 match m_a.(active) with
3607 | _, _, _, Action f -> f uioh
3608 | _, _, _, Noaction -> uioh
3610 Some uioh
3612 else None
3614 m_active <- active;
3615 m_first <- first;
3616 m_pan <- pan;
3617 uiohopt
3619 method hasaction n =
3620 match m_a.(n) with
3621 | _, _, _, Action _ -> true
3622 | _, _, _, Noaction -> false
3624 initializer m_active <- 1
3625 end)
3627 let rec fillsrc prevmode prevuioh =
3628 let sep () = src#caption E.s 0 in
3629 let colorp name get set =
3630 src#string name
3631 (fun () -> color_to_string (get ()))
3632 (fun v ->
3634 let c = color_of_string v in
3635 set c
3636 with exn ->
3637 state.text <- Printf.sprintf "bad color `%s': %s" v @@ exntos exn
3640 let oldmode = state.mode in
3641 let birdseye = isbirdseye state.mode in
3643 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3645 src#bool "presentation mode"
3646 (fun () -> conf.presentation)
3647 (fun v -> setpresentationmode v);
3649 src#bool "ignore case in searches"
3650 (fun () -> conf.icase)
3651 (fun v -> conf.icase <- v);
3653 src#bool "preload"
3654 (fun () -> conf.preload)
3655 (fun v -> conf.preload <- v);
3657 src#bool "highlight links"
3658 (fun () -> conf.hlinks)
3659 (fun v -> conf.hlinks <- v);
3661 src#bool "under info"
3662 (fun () -> conf.underinfo)
3663 (fun v -> conf.underinfo <- v);
3665 src#bool "persistent bookmarks"
3666 (fun () -> conf.savebmarks)
3667 (fun v -> conf.savebmarks <- v);
3669 src#fitmodel "fit model"
3670 (fun () -> FMTE.to_string conf.fitmodel)
3671 (fun v -> reqlayout conf.angle (FMTE.of_int v));
3673 src#bool "trim margins"
3674 (fun () -> conf.trimmargins)
3675 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3677 src#bool "persistent location"
3678 (fun () -> conf.jumpback)
3679 (fun v -> conf.jumpback <- v);
3681 sep ();
3682 src#int "inter-page space"
3683 (fun () -> conf.interpagespace)
3684 (fun n ->
3685 conf.interpagespace <- n;
3686 docolumns conf.columns;
3687 let pageno, py =
3688 match state.layout with
3689 | [] -> 0, 0
3690 | l :: _ ->
3691 l.pageno, l.pagey
3693 state.maxy <- calcheight ();
3694 let y = getpagey pageno in
3695 gotoy (y + py)
3698 src#int "page bias"
3699 (fun () -> conf.pagebias)
3700 (fun v -> conf.pagebias <- v);
3702 src#int "scroll step"
3703 (fun () -> conf.scrollstep)
3704 (fun n -> conf.scrollstep <- n);
3706 src#int "horizontal scroll step"
3707 (fun () -> conf.hscrollstep)
3708 (fun v -> conf.hscrollstep <- v);
3710 src#int "auto scroll step"
3711 (fun () ->
3712 match state.autoscroll with
3713 | Some step -> step
3714 | _ -> conf.autoscrollstep)
3715 (fun n ->
3716 let n = boundastep state.winh n in
3717 if state.autoscroll <> None
3718 then state.autoscroll <- Some n;
3719 conf.autoscrollstep <- n);
3721 src#int "zoom"
3722 (fun () -> truncate (conf.zoom *. 100.))
3723 (fun v -> setzoom ((float v) /. 100.));
3725 src#int "rotation"
3726 (fun () -> conf.angle)
3727 (fun v -> reqlayout v conf.fitmodel);
3729 src#int "scroll bar width"
3730 (fun () -> conf.scrollbw)
3731 (fun v ->
3732 conf.scrollbw <- v;
3733 reshape state.winw state.winh;
3736 src#int "scroll handle height"
3737 (fun () -> conf.scrollh)
3738 (fun v -> conf.scrollh <- v;);
3740 src#int "thumbnail width"
3741 (fun () -> conf.thumbw)
3742 (fun v ->
3743 conf.thumbw <- min 4096 v;
3744 match oldmode with
3745 | Birdseye beye ->
3746 leavebirdseye beye false;
3747 enterbirdseye ()
3748 | Textentry _
3749 | View
3750 | LinkNav _ -> ()
3753 let mode = state.mode in
3754 src#string "columns"
3755 (fun () ->
3756 match conf.columns with
3757 | Csingle _ -> "1"
3758 | Cmulti (multi, _) -> multicolumns_to_string multi
3759 | Csplit (count, _) -> "-" ^ string_of_int count
3761 (fun v ->
3762 let n, a, b = multicolumns_of_string v in
3763 setcolumns mode n a b);
3765 sep ();
3766 src#caption "Pixmap cache" 0;
3767 src#int_with_suffix "size (advisory)"
3768 (fun () -> conf.memlimit)
3769 (fun v -> conf.memlimit <- v);
3771 src#caption2 "used"
3772 (fun () -> Printf.sprintf "%s bytes, %d tiles"
3773 (string_with_suffix_of_int state.memused)
3774 (Hashtbl.length state.tilemap)) 1;
3776 sep ();
3777 src#caption "Layout" 0;
3778 src#caption2 "Dimension"
3779 (fun () ->
3780 Printf.sprintf "%dx%d (virtual %dx%d)"
3781 state.winw state.winh
3782 state.w state.maxy)
3784 if conf.debug
3785 then
3786 src#caption2 "Position" (fun () ->
3787 Printf.sprintf "%dx%d" state.x state.y
3789 else
3790 src#caption2 "Position" (fun () -> describe_location ()) 1
3793 sep ();
3794 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3795 "Save these parameters as global defaults at exit"
3796 (fun () -> conf.bedefault)
3797 (fun v -> conf.bedefault <- v)
3800 sep ();
3801 let btos b = if b then "@Ulguillemet" else "@Urguillemet" in
3802 src#bool ~offset:0 ~btos "Extended parameters"
3803 (fun () -> !showextended)
3804 (fun v -> showextended := v; fillsrc prevmode prevuioh);
3805 if !showextended
3806 then (
3807 src#bool "checkers"
3808 (fun () -> conf.checkers)
3809 (fun v -> conf.checkers <- v; setcheckers v);
3810 src#bool "update cursor"
3811 (fun () -> conf.updatecurs)
3812 (fun v -> conf.updatecurs <- v);
3813 src#bool "scroll-bar on the left"
3814 (fun () -> conf.leftscroll)
3815 (fun v -> conf.leftscroll <- v);
3816 src#bool "verbose"
3817 (fun () -> conf.verbose)
3818 (fun v -> conf.verbose <- v);
3819 src#bool "invert colors"
3820 (fun () -> conf.invert)
3821 (fun v -> conf.invert <- v);
3822 src#bool "max fit"
3823 (fun () -> conf.maxhfit)
3824 (fun v -> conf.maxhfit <- v);
3825 src#bool "pax mode"
3826 (fun () -> conf.pax != None)
3827 (fun v ->
3828 if v
3829 then conf.pax <- Some (ref (now (), 0, 0))
3830 else conf.pax <- None);
3831 src#string "uri launcher"
3832 (fun () -> conf.urilauncher)
3833 (fun v -> conf.urilauncher <- v);
3834 src#string "path launcher"
3835 (fun () -> conf.pathlauncher)
3836 (fun v -> conf.pathlauncher <- v);
3837 src#string "tile size"
3838 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3839 (fun v ->
3841 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
3842 conf.tilew <- max 64 w;
3843 conf.tileh <- max 64 h;
3844 flushtiles ();
3845 with exn ->
3846 state.text <- Printf.sprintf "bad tile size `%s': %s"
3847 v @@ exntos exn
3849 src#int "texture count"
3850 (fun () -> conf.texcount)
3851 (fun v ->
3852 if realloctexts v
3853 then conf.texcount <- v
3854 else impmsg "failed to set texture count please retry later"
3856 src#int "slice height"
3857 (fun () -> conf.sliceheight)
3858 (fun v ->
3859 conf.sliceheight <- v;
3860 wcmd "sliceh %d" conf.sliceheight;
3862 src#int "anti-aliasing level"
3863 (fun () -> conf.aalevel)
3864 (fun v ->
3865 conf.aalevel <- bound v 0 8;
3866 state.anchor <- getanchor ();
3867 opendoc state.path state.password;
3869 src#string "page scroll scaling factor"
3870 (fun () -> string_of_float conf.pgscale)
3871 (fun v ->
3873 let s = float_of_string v in
3874 conf.pgscale <- s
3875 with exn ->
3876 state.text <- Printf.sprintf
3877 "bad page scroll scaling factor `%s': %s" v @@ exntos exn
3880 src#int "ui font size"
3881 (fun () -> fstate.fontsize)
3882 (fun v -> setfontsize (bound v 5 100));
3883 src#int "hint font size"
3884 (fun () -> conf.hfsize)
3885 (fun v -> conf.hfsize <- bound v 5 100);
3886 colorp "background color"
3887 (fun () -> conf.bgcolor)
3888 (fun v -> conf.bgcolor <- v; setbgcol v);
3889 src#bool "crop hack"
3890 (fun () -> conf.crophack)
3891 (fun v -> conf.crophack <- v);
3892 src#string "trim fuzz"
3893 (fun () -> irect_to_string conf.trimfuzz)
3894 (fun v ->
3896 conf.trimfuzz <- irect_of_string v;
3897 if conf.trimmargins
3898 then settrim true conf.trimfuzz;
3899 with exn ->
3900 state.text <- Printf.sprintf "bad irect `%s': %s" v @@ exntos exn
3902 src#string "throttle"
3903 (fun () ->
3904 match conf.maxwait with
3905 | None -> "show place holder if page is not ready"
3906 | Some time ->
3907 if time = infinity
3908 then "wait for page to fully render"
3909 else
3910 "wait " ^ string_of_float time
3911 ^ " seconds before showing placeholder"
3913 (fun v ->
3915 let f = float_of_string v in
3916 if f <= 0.0
3917 then conf.maxwait <- None
3918 else conf.maxwait <- Some f
3919 with exn ->
3920 state.text <- Printf.sprintf "bad time `%s': %s" v @@ exntos exn
3922 src#string "ghyll scroll"
3923 (fun () ->
3924 match conf.ghyllscroll with
3925 | None -> E.s
3926 | Some nab -> ghyllscroll_to_string nab
3928 (fun v ->
3929 try conf.ghyllscroll <- ghyllscroll_of_string v
3930 with
3931 | Failure msg ->
3932 state.text <- Printf.sprintf "bad ghyll `%s': %s" v msg
3933 | exn ->
3934 state.text <- Printf.sprintf "bad ghyll `%s': %s" v @@ exntos exn
3936 src#string "selection command"
3937 (fun () -> conf.selcmd)
3938 (fun v -> conf.selcmd <- v);
3939 src#string "synctex command"
3940 (fun () -> conf.stcmd)
3941 (fun v -> conf.stcmd <- v);
3942 src#string "pax command"
3943 (fun () -> conf.paxcmd)
3944 (fun v -> conf.paxcmd <- v);
3945 src#string "ask password command"
3946 (fun () -> conf.passcmd)
3947 (fun v -> conf.passcmd <- v);
3948 src#string "save path command"
3949 (fun () -> conf.savecmd)
3950 (fun v -> conf.savecmd <- v);
3951 src#colorspace "color space"
3952 (fun () -> CSTE.to_string conf.colorspace)
3953 (fun v ->
3954 conf.colorspace <- CSTE.of_int v;
3955 wcmd "cs %d" v;
3956 load state.layout;
3958 src#paxmark "pax mark method"
3959 (fun () -> MTE.to_string conf.paxmark)
3960 (fun v -> conf.paxmark <- MTE.of_int v);
3961 if bousable () && !opengl_has_pbo
3962 then
3963 src#bool "use PBO"
3964 (fun () -> conf.usepbo)
3965 (fun v -> conf.usepbo <- v);
3966 src#bool "mouse wheel scrolls pages"
3967 (fun () -> conf.wheelbypage)
3968 (fun v -> conf.wheelbypage <- v);
3969 src#bool "open remote links in a new instance"
3970 (fun () -> conf.riani)
3971 (fun v -> conf.riani <- v);
3972 src#bool "edit annotations inline"
3973 (fun () -> conf.annotinline)
3974 (fun v -> conf.annotinline <- v);
3975 src#bool "coarse positioning in presentation mode"
3976 (fun () -> conf.coarseprespos)
3977 (fun v -> conf.coarseprespos <- v);
3980 sep ();
3981 src#caption "Document" 0;
3982 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
3983 src#caption2 "Pages"
3984 (fun () -> string_of_int state.pagecount) 1;
3985 src#caption2 "Dimensions"
3986 (fun () -> string_of_int (List.length state.pdims)) 1;
3987 if conf.trimmargins
3988 then (
3989 sep ();
3990 src#caption "Trimmed margins" 0;
3991 src#caption2 "Dimensions"
3992 (fun () -> string_of_int (List.length state.pdims)) 1;
3995 sep ();
3996 src#caption "OpenGL" 0;
3997 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
3998 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4000 sep ();
4001 src#caption "Location" 0;
4002 if nonemptystr state.origin
4003 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
4004 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
4006 src#reset prevmode prevuioh;
4008 fun () ->
4009 state.text <- E.s;
4010 resetmstate ();
4011 let prevmode = state.mode
4012 and prevuioh = state.uioh in
4013 fillsrc prevmode prevuioh;
4014 let source = (src :> lvsource) in
4015 let modehash = findkeyhash conf "info" in
4016 state.uioh <- coe (object (self)
4017 inherit listview ~zebra:false ~helpmode:false
4018 ~source ~trusted:true ~modehash as super
4019 val mutable m_prevmemused = 0
4020 method! infochanged = function
4021 | Memused ->
4022 if m_prevmemused != state.memused
4023 then (
4024 m_prevmemused <- state.memused;
4025 G.postRedisplay "memusedchanged";
4027 | Pdim -> G.postRedisplay "pdimchanged"
4028 | Docinfo -> fillsrc prevmode prevuioh
4030 method! key key mask =
4031 if not (Wsi.withctrl mask)
4032 then
4033 match key with
4034 | @left | @kpleft -> coe (self#updownlevel ~-1)
4035 | @right | @kpright -> coe (self#updownlevel 1)
4036 | _ -> super#key key mask
4037 else super#key key mask
4038 end);
4039 G.postRedisplay "info";
4042 let enterhelpmode =
4043 let source =
4044 (object
4045 inherit lvsourcebase
4046 method getitemcount = Array.length state.help
4047 method getitem n =
4048 let s, l, _ = state.help.(n) in
4049 (s, l)
4051 method exit ~uioh ~cancel ~active ~first ~pan =
4052 let optuioh =
4053 if not cancel
4054 then (
4055 match state.help.(active) with
4056 | _, _, Action f -> Some (f uioh)
4057 | _, _, Noaction -> Some uioh
4059 else None
4061 m_active <- active;
4062 m_first <- first;
4063 m_pan <- pan;
4064 optuioh
4066 method hasaction n =
4067 match state.help.(n) with
4068 | _, _, Action _ -> true
4069 | _, _, Noaction -> false
4071 initializer
4072 m_active <- -1
4073 end)
4074 in fun () ->
4075 let modehash = findkeyhash conf "help" in
4076 resetmstate ();
4077 state.uioh <- coe (new listview
4078 ~zebra:false ~helpmode:true
4079 ~source ~trusted:true ~modehash);
4080 G.postRedisplay "help";
4083 let entermsgsmode =
4084 let msgsource =
4085 (object
4086 inherit lvsourcebase
4087 val mutable m_items = E.a
4089 method getitemcount = 1 + Array.length m_items
4091 method getitem n =
4092 if n = 0
4093 then "[Clear]", 0
4094 else m_items.(n-1), 0
4096 method exit ~uioh ~cancel ~active ~first ~pan =
4097 ignore uioh;
4098 if not cancel
4099 then (
4100 if active = 0
4101 then Buffer.clear state.errmsgs;
4103 m_active <- active;
4104 m_first <- first;
4105 m_pan <- pan;
4106 None
4108 method hasaction n =
4109 n = 0
4111 method reset =
4112 state.newerrmsgs <- false;
4113 let l = Str.split newlinere (Buffer.contents state.errmsgs) in
4114 m_items <- Array.of_list l
4116 initializer
4117 m_active <- 0
4118 end)
4119 in fun () ->
4120 state.text <- E.s;
4121 resetmstate ();
4122 msgsource#reset;
4123 let source = (msgsource :> lvsource) in
4124 let modehash = findkeyhash conf "listview" in
4125 state.uioh <- coe (object
4126 inherit listview ~zebra:false ~helpmode:false
4127 ~source ~trusted:false ~modehash as super
4128 method! display =
4129 if state.newerrmsgs
4130 then msgsource#reset;
4131 super#display
4132 end);
4133 G.postRedisplay "msgs";
4136 let getusertext s =
4137 let editor = getenvwithdef "EDITOR" E.s in
4138 if emptystr editor
4139 then E.s
4140 else
4141 let tmppath = Filename.temp_file "llpp" "note" in
4142 if nonemptystr s
4143 then (
4144 let oc = open_out tmppath in
4145 output_string oc s;
4146 close_out oc;
4148 let execstr = editor ^ " " ^ tmppath in
4149 let s =
4150 match spawn execstr [] with
4151 | (exception exn) ->
4152 impmsg "spawn(%S) failed: %s" execstr @@ exntos exn;
4154 | pid ->
4155 match Unix.waitpid [] pid with
4156 | (exception exn) ->
4157 impmsg "waitpid(%d) failed: %s" pid @@ exntos exn;
4159 | (_pid, status) ->
4160 match status with
4161 | Unix.WEXITED 0 -> filecontents tmppath
4162 | Unix.WEXITED n ->
4163 impmsg "editor process(%s) exited abnormally: %d" execstr n;
4165 | Unix.WSIGNALED n ->
4166 impmsg "editor process(%s) was killed by signal %d" execstr n;
4168 | Unix.WSTOPPED n ->
4169 impmsg "editor(%s) process was stopped by signal %d" execstr n;
4172 match Unix.unlink tmppath with
4173 | (exception exn) ->
4174 impmsg "failed to ulink %S: %s" tmppath @@ exntos exn;
4176 | () -> s
4179 let enterannotmode opaque slinkindex =
4180 let msgsource =
4181 (object
4182 inherit lvsourcebase
4183 val mutable m_text = E.s
4184 val mutable m_items = E.a
4186 method getitemcount = Array.length m_items
4188 method getitem n =
4189 let label, _func = m_items.(n) in
4190 label, 0
4192 method exit ~uioh ~cancel ~active ~first ~pan =
4193 ignore (uioh, first, pan);
4194 if not cancel
4195 then (
4196 let _label, func = m_items.(active) in
4197 func ()
4199 None
4201 method hasaction n = nonemptystr @@ fst m_items.(n)
4203 method reset s =
4204 let rec split accu b i =
4205 let p = b+i in
4206 if p = String.length s
4207 then (String.sub s b (p-b), unit) :: accu
4208 else
4209 if (i > 70 && s.[p] = ' ') || s.[p] = '\r' || s.[p] = '\n'
4210 then
4211 let ss = if i = 0 then E.s else String.sub s b i in
4212 split ((ss, unit)::accu) (p+1) 0
4213 else
4214 split accu b (i+1)
4216 let cleanup () =
4217 wcmd "freepage %s" (~> opaque);
4218 let keys =
4219 Hashtbl.fold (fun key opaque' accu ->
4220 if opaque' = opaque'
4221 then key :: accu else accu) state.pagemap []
4223 List.iter (Hashtbl.remove state.pagemap) keys;
4224 flushtiles ();
4225 gotoy state.y
4227 let dele () =
4228 delannot opaque slinkindex;
4229 cleanup ();
4231 let edit inline () =
4232 let update s =
4233 if emptystr s
4234 then dele ()
4235 else (
4236 modannot opaque slinkindex s;
4237 cleanup ();
4240 if inline
4241 then
4242 let mode = state.mode in
4243 state.mode <-
4244 Textentry (
4245 ("annotation: ", m_text, None, textentry, update, true),
4246 fun _ -> state.mode <- mode);
4247 state.text <- E.s;
4248 enttext ();
4249 else
4250 let s = getusertext m_text in
4251 update s
4253 m_text <- s;
4254 m_items <-
4255 ( "[Copy]", fun () -> selstring m_text)
4256 :: ("[Delete]", dele)
4257 :: ("[Edit]", edit conf.annotinline)
4258 :: (E.s, unit)
4259 :: split [] 0 0 |> List.rev |> Array.of_list
4261 initializer
4262 m_active <- 0
4263 end)
4265 state.text <- E.s;
4266 let s = getannotcontents opaque slinkindex in
4267 resetmstate ();
4268 msgsource#reset s;
4269 let source = (msgsource :> lvsource) in
4270 let modehash = findkeyhash conf "listview" in
4271 state.uioh <- coe (object
4272 inherit listview ~zebra:false ~helpmode:false
4273 ~source ~trusted:false ~modehash
4274 end);
4275 G.postRedisplay "enterannotmode";
4278 let gotounder under =
4279 let getpath filename =
4280 let path =
4281 if nonemptystr filename
4282 then
4283 if Filename.is_relative filename
4284 then
4285 let dir = Filename.dirname state.path in
4286 let dir =
4287 if Filename.is_implicit dir
4288 then Filename.concat (Sys.getcwd ()) dir
4289 else dir
4291 Filename.concat dir filename
4292 else filename
4293 else E.s
4295 if Sys.file_exists path
4296 then path
4297 else E.s
4299 match under with
4300 | Ulinkgoto (pageno, top) ->
4301 if pageno >= 0
4302 then (
4303 addnav ();
4304 let top =
4305 if conf.presentation && conf.coarseprespos
4306 then 0
4307 else top
4309 gotopage1 pageno top;
4312 | Ulinkuri s -> gotouri s
4314 | Uremote (filename, pageno) ->
4315 let path = getpath filename in
4316 if nonemptystr path
4317 then (
4318 if conf.riani
4319 then
4320 let command = Printf.sprintf "%s -page %d %S" !selfexec pageno path in
4321 match spawn command [] with
4322 | _pid -> ()
4323 | (exception exn) ->
4324 dolog "failed to execute `%s': %s" command @@ exntos exn
4325 else
4326 let anchor = getanchor () in
4327 let ranchor = state.path, state.password, anchor, state.origin in
4328 state.origin <- E.s;
4329 state.anchor <- (pageno, 0.0, 0.0);
4330 state.ranchors <- ranchor :: state.ranchors;
4331 opendoc path E.s;
4333 else impmsg "cannot find %s" filename
4335 | Uremotedest (filename, destname) ->
4336 let path = getpath filename in
4337 if nonemptystr path
4338 then (
4339 if conf.riani
4340 then
4341 let command = !selfexec ^ " " ^ path ^ " -dest " ^ destname in
4342 match spawn command [] with
4343 | (exception exn) ->
4344 dolog "failed to execute `%s': %s" command @@ exntos exn
4345 | _pid -> ()
4346 else
4347 let anchor = getanchor () in
4348 let ranchor = state.path, state.password, anchor, state.origin in
4349 state.origin <- E.s;
4350 state.nameddest <- destname;
4351 state.ranchors <- ranchor :: state.ranchors;
4352 opendoc path E.s;
4354 else impmsg "cannot find %s" filename
4356 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4357 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
4360 let gotooutline (_, _, kind) =
4361 match kind with
4362 | Onone -> ()
4363 | Oanchor anchor ->
4364 let (pageno, y, _) = anchor in
4365 let y = getanchory
4366 (if conf.presentation then (pageno, y, 1.0) else anchor)
4368 addnav ();
4369 gotoghyll y
4370 | Ouri uri -> gotounder (Ulinkuri uri)
4371 | Olaunch cmd -> gotounder (Ulaunch cmd)
4372 | Oremote remote -> gotounder (Uremote remote)
4373 | Ohistory hist -> gotohist hist
4374 | Oremotedest remotedest -> gotounder (Uremotedest remotedest)
4377 class outlinesoucebase fetchoutlines = object (self)
4378 inherit lvsourcebase
4379 val mutable m_items = E.a
4380 val mutable m_minfo = E.a
4381 val mutable m_orig_items = E.a
4382 val mutable m_orig_minfo = E.a
4383 val mutable m_narrow_patterns = []
4384 val mutable m_gen = -1
4386 method getitemcount = Array.length m_items
4388 method getitem n =
4389 let s, n, _ = m_items.(n) in
4390 (s, n+0)
4392 method exit ~(uioh:uioh) ~cancel ~active ~(first:int) ~pan :
4393 uioh option =
4394 ignore (uioh, first);
4395 let items, minfo =
4396 if m_narrow_patterns = []
4397 then m_orig_items, m_orig_minfo
4398 else m_items, m_minfo
4400 m_pan <- pan;
4401 if not cancel
4402 then (
4403 m_items <- items;
4404 m_minfo <- minfo;
4405 gotooutline m_items.(active);
4407 else (
4408 m_items <- items;
4409 m_minfo <- minfo;
4411 None
4413 method hasaction (_:int) = true
4415 method greetmsg =
4416 if Array.length m_items != Array.length m_orig_items
4417 then
4418 let s =
4419 match m_narrow_patterns with
4420 | one :: [] -> one
4421 | many -> String.concat "@Uellipsis" (List.rev many)
4423 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
4424 else E.s
4426 method statestr =
4427 match m_narrow_patterns with
4428 | [] -> E.s
4429 | one :: [] -> one
4430 | head :: _ -> "@Uellipsis" ^ head
4432 method narrow pattern =
4433 match Str.regexp_case_fold pattern with
4434 | (exception _) -> ()
4435 | re ->
4436 let rec loop accu minfo n =
4437 if n = -1
4438 then (
4439 m_items <- Array.of_list accu;
4440 m_minfo <- Array.of_list minfo;
4442 else
4443 let (s, _, _) as o = m_items.(n) in
4444 let accu, minfo =
4445 match Str.search_forward re s 0 with
4446 | (exception Not_found) -> accu, minfo
4447 | first -> o :: accu, (first, Str.match_end ()) :: minfo
4449 loop accu minfo (n-1)
4451 loop [] [] (Array.length m_items - 1)
4453 method! getminfo = m_minfo
4455 method denarrow =
4456 m_orig_items <- fetchoutlines ();
4457 m_minfo <- m_orig_minfo;
4458 m_items <- m_orig_items
4460 method add_narrow_pattern pattern =
4461 m_narrow_patterns <- pattern :: m_narrow_patterns
4463 method del_narrow_pattern =
4464 match m_narrow_patterns with
4465 | _ :: rest -> m_narrow_patterns <- rest
4466 | [] -> ()
4468 method renarrow =
4469 self#denarrow;
4470 match m_narrow_patterns with
4471 | pattern :: [] -> self#narrow pattern; pattern
4472 | list ->
4473 List.fold_left (fun accu pattern ->
4474 self#narrow pattern;
4475 pattern ^ "@Uellipsis" ^ accu) E.s list
4477 method calcactive (_:anchor) = 0
4479 method reset anchor items =
4480 if state.gen != m_gen
4481 then (
4482 m_orig_items <- items;
4483 m_items <- items;
4484 m_narrow_patterns <- [];
4485 m_minfo <- E.a;
4486 m_orig_minfo <- E.a;
4487 m_gen <- state.gen;
4489 else (
4490 if items != m_orig_items
4491 then (
4492 m_orig_items <- items;
4493 if m_narrow_patterns == []
4494 then m_items <- items;
4497 let active = self#calcactive anchor in
4498 m_active <- active;
4499 m_first <- firstof m_first active
4503 let outlinesource fetchoutlines =
4504 (object
4505 inherit outlinesoucebase fetchoutlines
4506 method! calcactive anchor =
4507 let rely = getanchory anchor in
4508 let rec loop n best bestd =
4509 if n = Array.length m_items
4510 then best
4511 else
4512 let _, _, kind = m_items.(n) in
4513 match kind with
4514 | Oanchor anchor ->
4515 let orely = getanchory anchor in
4516 let d = abs (orely - rely) in
4517 if d < bestd
4518 then loop (n+1) n d
4519 else loop (n+1) best bestd
4520 | Onone | Oremote _ | Olaunch _
4521 | Oremotedest _ | Ouri _ | Ohistory _ ->
4522 loop (n+1) best bestd
4524 loop 0 ~-1 max_int
4525 end)
4528 let enteroutlinemode, enterbookmarkmode, enterhistmode =
4529 let mkselector sourcetype =
4530 let fetchoutlines () =
4531 match sourcetype with
4532 | `bookmarks -> Array.of_list state.bookmarks
4533 | `outlines -> state.outlines
4534 | `history -> genhistoutlines ()
4536 let source =
4537 if sourcetype = `history
4538 then new outlinesoucebase fetchoutlines
4539 else outlinesource fetchoutlines
4541 fun errmsg ->
4542 let outlines = fetchoutlines () in
4543 if Array.length outlines = 0
4544 then (
4545 showtext ' ' errmsg;
4547 else (
4548 resetmstate ();
4549 Wsi.setcursor Wsi.CURSOR_INHERIT;
4550 let anchor = getanchor () in
4551 source#reset anchor outlines;
4552 state.text <- source#greetmsg;
4553 state.uioh <-
4554 coe (new outlinelistview ~zebra:(sourcetype=`history) ~source);
4555 G.postRedisplay "enter selector";
4558 let mkenter sourcetype errmsg =
4559 let enter = mkselector sourcetype in
4560 fun () -> enter errmsg
4562 (**)mkenter `outlines "document has no outline"
4563 , mkenter `bookmarks "document has no bookmarks (yet)"
4564 , mkenter `history "history is empty"
4567 let quickbookmark ?title () =
4568 match state.layout with
4569 | [] -> ()
4570 | l :: _ ->
4571 let title =
4572 match title with
4573 | None ->
4574 let tm = Unix.localtime (now ()) in
4575 Printf.sprintf
4576 "Quick (page %d) (bookmarked at %02d/%02d/%d %02d:%02d)"
4577 (l.pageno+1)
4578 tm.Unix.tm_mday
4579 (tm.Unix.tm_mon+1)
4580 (tm.Unix.tm_year + 1900)
4581 tm.Unix.tm_hour
4582 tm.Unix.tm_min
4583 | Some title -> title
4585 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4588 let setautoscrollspeed step goingdown =
4589 let incr = max 1 ((abs step) / 2) in
4590 let incr = if goingdown then incr else -incr in
4591 let astep = boundastep state.winh (step + incr) in
4592 state.autoscroll <- Some astep;
4595 let canpan () =
4596 match conf.columns with
4597 | Csplit _ -> true
4598 | Csingle _ | Cmulti _ -> state.x != 0 || conf.zoom > 1.0
4601 let panbound x = bound x (-state.w) (wadjsb () + state.winw);;
4603 let existsinrow pageno (columns, coverA, coverB) p =
4604 let last = ((pageno - coverA) mod columns) + columns in
4605 let rec any = function
4606 | [] -> false
4607 | l :: rest ->
4608 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4609 then p l
4610 else (
4611 if not (p l)
4612 then (if l.pageno = last then false else any rest)
4613 else true
4616 any state.layout
4619 let nextpage () =
4620 match state.layout with
4621 | [] ->
4622 let pageno = page_of_y state.y in
4623 gotoghyll (getpagey (pageno+1))
4624 | l :: rest ->
4625 match conf.columns with
4626 | Csingle _ ->
4627 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4628 then
4629 let y = clamp (pgscale state.winh) in
4630 gotoghyll y
4631 else
4632 let pageno = min (l.pageno+1) (state.pagecount-1) in
4633 gotoghyll (getpagey pageno)
4634 | Cmulti ((c, _, _) as cl, _) ->
4635 if conf.presentation
4636 && (existsinrow l.pageno cl
4637 (fun l -> 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+c) (state.pagecount-1) in
4643 gotoghyll (getpagey pageno)
4644 | Csplit (n, _) ->
4645 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4646 then
4647 let pagey, pageh = getpageyh l.pageno in
4648 let pagey = pagey + pageh * l.pagecol in
4649 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4650 gotoghyll (pagey + pageh + ips)
4653 let prevpage () =
4654 match state.layout with
4655 | [] ->
4656 let pageno = page_of_y state.y in
4657 gotoghyll (getpagey (pageno-1))
4658 | l :: _ ->
4659 match conf.columns with
4660 | Csingle _ ->
4661 if conf.presentation && l.pagey != 0
4662 then
4663 gotoghyll (clamp (pgscale ~-(state.winh)))
4664 else
4665 let pageno = max 0 (l.pageno-1) in
4666 gotoghyll (getpagey pageno)
4667 | Cmulti ((c, _, coverB) as cl, _) ->
4668 if conf.presentation &&
4669 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4670 then
4671 gotoghyll (clamp (pgscale ~-(state.winh)))
4672 else
4673 let decr =
4674 if l.pageno = state.pagecount - coverB
4675 then 1
4676 else c
4678 let pageno = max 0 (l.pageno-decr) in
4679 gotoghyll (getpagey pageno)
4680 | Csplit (n, _) ->
4681 let y =
4682 if l.pagecol = 0
4683 then
4684 if l.pageno = 0
4685 then l.pagey
4686 else
4687 let pageno = max 0 (l.pageno-1) in
4688 let pagey, pageh = getpageyh pageno in
4689 pagey + (n-1)*pageh
4690 else
4691 let pagey, pageh = getpageyh l.pageno in
4692 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4694 gotoghyll y
4697 let save () =
4698 if emptystr conf.savecmd
4699 then error "don't know where to save modified document"
4700 else
4701 let savecmd = Str.global_replace percentsre state.path conf.savecmd in
4702 let path =
4703 getcmdoutput
4704 (fun s -> error "failed to obtain path to the saved copy: %s" s)
4705 savecmd
4707 if nonemptystr path
4708 then
4709 let tmp = path ^ ".tmp" in
4710 savedoc tmp;
4711 Unix.rename tmp path;
4714 let viewkeyboard key mask =
4715 let enttext te =
4716 let mode = state.mode in
4717 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4718 state.text <- E.s;
4719 enttext ();
4720 G.postRedisplay "view:enttext"
4722 let ctrl = Wsi.withctrl mask in
4723 let key =
4724 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
4726 match key with
4727 | @Q -> exit 0
4729 | @W ->
4730 if hasunsavedchanges ()
4731 then save ()
4733 | @insert ->
4734 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4735 then (
4736 state.mode <- LinkNav (Ltgendir 0);
4737 gotoy state.y;
4739 else impmsg "keyboard link navigation does not work under rotation"
4741 | @escape | @q ->
4742 begin match state.mstate with
4743 | Mzoomrect _ ->
4744 resetmstate ();
4745 G.postRedisplay "kill rect";
4746 | Msel _
4747 | Mpan _
4748 | Mscrolly | Mscrollx
4749 | Mzoom _
4750 | Mnone ->
4751 begin match state.mode with
4752 | LinkNav _ ->
4753 state.mode <- View;
4754 G.postRedisplay "esc leave linknav"
4755 | Birdseye _
4756 | Textentry _
4757 | View ->
4758 match state.ranchors with
4759 | [] -> raise Quit
4760 | (path, password, anchor, origin) :: rest ->
4761 state.ranchors <- rest;
4762 state.anchor <- anchor;
4763 state.origin <- origin;
4764 state.nameddest <- E.s;
4765 opendoc path password
4766 end;
4767 end;
4769 | @backspace ->
4770 gotoghyll (getnav ~-1)
4772 | @o ->
4773 enteroutlinemode ()
4775 | @H ->
4776 enterhistmode ()
4778 | @u ->
4779 state.rects <- [];
4780 state.text <- E.s;
4781 Hashtbl.iter (fun _ opaque ->
4782 clearmark opaque;
4783 Hashtbl.clear state.prects) state.pagemap;
4784 G.postRedisplay "dehighlight";
4786 | @slash | @question ->
4787 let ondone isforw s =
4788 cbput state.hists.pat s;
4789 state.searchpattern <- s;
4790 search s isforw
4792 let s = String.make 1 (Char.chr key) in
4793 enttext (s, E.s, Some (onhist state.hists.pat),
4794 textentry, ondone (key = @slash), true)
4796 | @plus | @kpplus | @equals when ctrl ->
4797 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4798 setzoom (conf.zoom +. incr)
4800 | @plus | @kpplus ->
4801 let ondone s =
4802 let n =
4803 try int_of_string s with exc ->
4804 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exc;
4805 max_int
4807 if n != max_int
4808 then (
4809 conf.pagebias <- n;
4810 state.text <- "page bias is now " ^ string_of_int n;
4813 enttext ("page bias: ", E.s, None, intentry, ondone, true)
4815 | @minus | @kpminus when ctrl ->
4816 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4817 setzoom (max 0.01 (conf.zoom -. decr))
4819 | @minus | @kpminus ->
4820 let ondone msg = state.text <- msg in
4821 enttext (
4822 "option [acfhilpstvxACFPRSZTISM]: ", E.s, None,
4823 optentry state.mode, ondone, true
4826 | @0 when ctrl ->
4827 if conf.zoom = 1.0
4828 then (
4829 state.x <- 0;
4830 gotoy state.y
4832 else setzoom 1.0
4834 | (@1 | @2) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
4835 let cols =
4836 match conf.columns with
4837 | Csingle _ | Cmulti _ -> 1
4838 | Csplit (n, _) -> n
4840 let h = state.winh -
4841 conf.interpagespace lsl (if conf.presentation then 1 else 0)
4843 let zoom = zoomforh state.winw h (vscrollw ()) cols in
4844 if zoom > 0.0 && (key = @2 || zoom < 1.0)
4845 then setzoom zoom
4847 | @3 when ctrl ->
4848 let fm =
4849 match conf.fitmodel with
4850 | FitWidth -> FitProportional
4851 | FitProportional -> FitPage
4852 | FitPage -> FitWidth
4854 state.text <- "fit model: " ^ FMTE.to_string fm;
4855 reqlayout conf.angle fm
4857 | @4 when ctrl -> (* ctrl-4 *)
4858 let zoom = getmaxw () /. float state.winw in
4859 if zoom > 0.0 then setzoom zoom
4861 | @F9 ->
4862 togglebirdseye ()
4864 | @9 when ctrl ->
4865 togglebirdseye ()
4867 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4868 when not ctrl -> (* 0..9 *)
4869 let ondone s =
4870 let n =
4871 try int_of_string s with exc ->
4872 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exc;
4875 if n >= 0
4876 then (
4877 addnav ();
4878 cbput state.hists.pag (string_of_int n);
4879 gotopage1 (n + conf.pagebias - 1) 0;
4882 let pageentry text key =
4883 match Char.unsafe_chr key with
4884 | 'g' -> TEdone text
4885 | _ -> intentry text key
4887 let text = String.make 1 (Char.chr key) in
4888 enttext (":", text, Some (onhist state.hists.pag),
4889 pageentry, ondone, true)
4891 | @b ->
4892 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
4893 reshape state.winw state.winh;
4895 | @B ->
4896 state.bzoom <- not state.bzoom;
4897 state.rects <- [];
4898 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
4900 | @l ->
4901 conf.hlinks <- not conf.hlinks;
4902 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4903 G.postRedisplay "toggle highlightlinks";
4905 | @F ->
4906 if conf.angle mod 360 = 0
4907 then (
4908 state.glinks <- true;
4909 let mode = state.mode in
4910 state.mode <-
4911 Textentry (
4912 (":", E.s, None, linknentry, linknact gotounder, false),
4913 (fun _ ->
4914 state.glinks <- false;
4915 state.mode <- mode)
4917 state.text <- E.s;
4918 G.postRedisplay "view:linkent(F)"
4920 else impmsg "hint mode does not work under rotation"
4922 | @y ->
4923 state.glinks <- true;
4924 let mode = state.mode in
4925 state.mode <- Textentry (
4927 ":", E.s, None, linknentry, linknact (fun under ->
4928 selstring (undertext under);
4929 ), false
4931 fun _ ->
4932 state.glinks <- false;
4933 state.mode <- mode
4935 state.text <- E.s;
4936 G.postRedisplay "view:linkent"
4938 | @a ->
4939 begin match state.autoscroll with
4940 | Some step ->
4941 conf.autoscrollstep <- step;
4942 state.autoscroll <- None
4943 | None ->
4944 if conf.autoscrollstep = 0
4945 then state.autoscroll <- Some 1
4946 else state.autoscroll <- Some conf.autoscrollstep
4949 | @p when ctrl ->
4950 launchpath () (* XXX where do error messages go? *)
4952 | @P ->
4953 setpresentationmode (not conf.presentation);
4954 showtext ' ' ("presentation mode " ^
4955 if conf.presentation then "on" else "off");
4957 | @f ->
4958 if List.mem Wsi.Fullscreen state.winstate
4959 then Wsi.reshape conf.cwinw conf.cwinh
4960 else Wsi.fullscreen ()
4962 | @p | @N ->
4963 search state.searchpattern false
4965 | @n | @F3 ->
4966 search state.searchpattern true
4968 | @t ->
4969 begin match state.layout with
4970 | [] -> ()
4971 | l :: _ ->
4972 gotoghyll (getpagey l.pageno)
4975 | @space ->
4976 nextpage ()
4978 | @delete | @kpdelete -> (* delete *)
4979 prevpage ()
4981 | @equals ->
4982 showtext ' ' (describe_location ());
4984 | @w ->
4985 begin match state.layout with
4986 | [] -> ()
4987 | l :: _ ->
4988 Wsi.reshape (l.pagew + vscrollw ()) l.pageh;
4989 G.postRedisplay "w"
4992 | @apos ->
4993 enterbookmarkmode ()
4995 | @h | @F1 ->
4996 enterhelpmode ()
4998 | @i ->
4999 enterinfomode ()
5001 | @e when Buffer.length state.errmsgs > 0 ->
5002 entermsgsmode ()
5004 | @m ->
5005 let ondone s =
5006 match state.layout with
5007 | l :: _ ->
5008 if nonemptystr s
5009 then
5010 state.bookmarks <-
5011 (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
5012 | _ -> ()
5014 enttext ("bookmark: ", E.s, None, textentry, ondone, true)
5016 | @tilde ->
5017 quickbookmark ();
5018 showtext ' ' "Quick bookmark added";
5020 | @z ->
5021 begin match state.layout with
5022 | l :: _ ->
5023 let rect = getpdimrect l.pagedimno in
5024 let w, h =
5025 if conf.crophack
5026 then
5027 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5028 truncate (1.2 *. (rect.(3) -. rect.(0))))
5029 else
5030 (truncate (rect.(1) -. rect.(0)),
5031 truncate (rect.(3) -. rect.(0)))
5033 let w = truncate ((float w)*.conf.zoom)
5034 and h = truncate ((float h)*.conf.zoom) in
5035 if w != 0 && h != 0
5036 then (
5037 state.anchor <- getanchor ();
5038 Wsi.reshape (w + vscrollw ()) (h + conf.interpagespace)
5040 G.postRedisplay "z";
5042 | [] -> ()
5045 | @x -> state.roam ()
5047 | @Lt | @Gt ->
5048 reqlayout (conf.angle +
5049 (if key = @Gt then 30 else -30)) conf.fitmodel
5051 | @Lb | @Rb ->
5052 conf.colorscale <-
5053 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5055 G.postRedisplay "brightness";
5057 | @c when state.mode = View ->
5058 if Wsi.withalt mask
5059 then (
5060 if conf.zoom > 1.0
5061 then
5062 let m = (wadjsb () + state.winw - state.w) / 2 in
5063 state.x <- m;
5064 gotoy_and_clear_text state.y
5066 else
5067 let (c, a, b), z =
5068 match state.prevcolumns with
5069 | None -> (1, 0, 0), 1.0
5070 | Some (columns, z) ->
5071 let cab =
5072 match columns with
5073 | Csplit (c, _) -> -c, 0, 0
5074 | Cmulti ((c, a, b), _) -> c, a, b
5075 | Csingle _ -> 1, 0, 0
5077 cab, z
5079 setcolumns View c a b;
5080 setzoom z
5082 | @down | @up when ctrl && Wsi.withshift mask ->
5083 let zoom, x = state.prevzoom in
5084 setzoom zoom;
5085 state.x <- x;
5087 | @k | @up | @kpup ->
5088 begin match state.autoscroll with
5089 | None ->
5090 begin match state.mode with
5091 | Birdseye beye -> upbirdseye 1 beye
5092 | Textentry _
5093 | View
5094 | LinkNav _ ->
5095 if ctrl
5096 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5097 else (
5098 if not (Wsi.withshift mask) && conf.presentation
5099 then prevpage ()
5100 else gotoghyll1 true (clamp (-conf.scrollstep))
5103 | Some n ->
5104 setautoscrollspeed n false
5107 | @j | @down | @kpdown ->
5108 begin match state.autoscroll with
5109 | None ->
5110 begin match state.mode with
5111 | Birdseye beye -> downbirdseye 1 beye
5112 | Textentry _
5113 | View
5114 | LinkNav _ ->
5115 if ctrl
5116 then gotoy_and_clear_text (clamp (state.winh/2))
5117 else (
5118 if not (Wsi.withshift mask) && conf.presentation
5119 then nextpage ()
5120 else gotoghyll1 true (clamp (conf.scrollstep))
5123 | Some n ->
5124 setautoscrollspeed n true
5127 | @left | @right | @kpleft | @kpright when not (Wsi.withalt mask) ->
5128 if canpan ()
5129 then
5130 let dx =
5131 if ctrl
5132 then state.winw / 2
5133 else conf.hscrollstep
5135 let dx = if key = @left || key = @kpleft then dx else -dx in
5136 state.x <- panbound (state.x + dx);
5137 gotoy_and_clear_text state.y
5138 else (
5139 state.text <- E.s;
5140 G.postRedisplay "left/right"
5143 | @prior | @kpprior ->
5144 let y =
5145 if ctrl
5146 then
5147 match state.layout with
5148 | [] -> state.y
5149 | l :: _ -> state.y - l.pagey
5150 else
5151 clamp (pgscale (-state.winh))
5153 gotoghyll y
5155 | @next | @kpnext ->
5156 let y =
5157 if ctrl
5158 then
5159 match List.rev state.layout with
5160 | [] -> state.y
5161 | l :: _ -> getpagey l.pageno
5162 else
5163 clamp (pgscale state.winh)
5165 gotoghyll y
5167 | @g | @home | @kphome ->
5168 addnav ();
5169 gotoghyll 0
5170 | @G | @jend | @kpend ->
5171 addnav ();
5172 gotoghyll (clamp state.maxy)
5174 | @right | @kpright when Wsi.withalt mask ->
5175 gotoghyll (getnav 1)
5176 | @left | @kpleft when Wsi.withalt mask ->
5177 gotoghyll (getnav ~-1)
5179 | @r ->
5180 reload ()
5182 | @v when conf.debug ->
5183 state.rects <- [];
5184 List.iter (fun l ->
5185 match getopaque l.pageno with
5186 | None -> ()
5187 | Some opaque ->
5188 let x0, y0, x1, y1 = pagebbox opaque in
5189 let a,b = float x0, float y0 in
5190 let c,d = float x1, float y0 in
5191 let e,f = float x1, float y1 in
5192 let h,j = float x0, float y1 in
5193 let rect = (a,b,c,d,e,f,h,j) in
5194 debugrect rect;
5195 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
5196 state.rects <- (l.pageno, color, rect) :: state.rects;
5197 ) state.layout;
5198 G.postRedisplay "v";
5200 | @pipe ->
5201 let mode = state.mode in
5202 let cmd = ref E.s in
5203 let onleave = function
5204 | Cancel -> state.mode <- mode
5205 | Confirm ->
5206 List.iter (fun l ->
5207 match getopaque l.pageno with
5208 | Some opaque -> pipesel opaque !cmd
5209 | None -> ()) state.layout;
5210 state.mode <- mode
5212 let ondone s =
5213 cbput state.hists.sel s;
5214 cmd := s
5216 let te =
5217 "| ", !cmd, Some (onhist state.hists.sel), textentry, ondone, true
5219 G.postRedisplay "|";
5220 state.mode <- Textentry (te, onleave);
5222 | _ ->
5223 vlog "huh? %s" (Wsi.keyname key)
5226 let linknavkeyboard key mask linknav =
5227 let getpage pageno =
5228 let rec loop = function
5229 | [] -> None
5230 | l :: _ when l.pageno = pageno -> Some l
5231 | _ :: rest -> loop rest
5232 in loop state.layout
5234 let doexact (pageno, n) =
5235 match getopaque pageno, getpage pageno with
5236 | Some opaque, Some l ->
5237 if key = @enter || key = @kpenter
5238 then
5239 let under = getlink opaque n in
5240 G.postRedisplay "link gotounder";
5241 gotounder under;
5242 state.mode <- View;
5243 else
5244 let opt, dir =
5245 match key with
5246 | @home ->
5247 Some (findlink opaque LDfirst), -1
5249 | @jend ->
5250 Some (findlink opaque LDlast), 1
5252 | @left ->
5253 Some (findlink opaque (LDleft n)), -1
5255 | @right ->
5256 Some (findlink opaque (LDright n)), 1
5258 | @up ->
5259 Some (findlink opaque (LDup n)), -1
5261 | @down ->
5262 Some (findlink opaque (LDdown n)), 1
5264 | _ -> None, 0
5266 let pwl l dir =
5267 begin match findpwl l.pageno dir with
5268 | Pwlnotfound -> ()
5269 | Pwl pageno ->
5270 let notfound dir =
5271 state.mode <- LinkNav (Ltgendir dir);
5272 let y, h = getpageyh pageno in
5273 let y =
5274 if dir < 0
5275 then y + h - state.winh
5276 else y
5278 gotoy y
5280 begin match getopaque pageno, getpage pageno with
5281 | Some opaque, Some _ ->
5282 let link =
5283 let ld = if dir > 0 then LDfirst else LDlast in
5284 findlink opaque ld
5286 begin match link with
5287 | Lfound m ->
5288 showlinktype (getlink opaque m);
5289 state.mode <- LinkNav (Ltexact (pageno, m));
5290 G.postRedisplay "linknav jpage";
5291 | Lnotfound -> notfound dir
5292 end;
5293 | _ -> notfound dir
5294 end;
5295 end;
5297 begin match opt with
5298 | Some Lnotfound -> pwl l dir;
5299 | Some (Lfound m) ->
5300 if m = n
5301 then pwl l dir
5302 else (
5303 let _, y0, _, y1 = getlinkrect opaque m in
5304 if y0 < l.pagey
5305 then gotopage1 l.pageno y0
5306 else (
5307 let d = fstate.fontsize + 1 in
5308 if y1 - l.pagey > l.pagevh - d
5309 then gotopage1 l.pageno (y1 - state.winh - hscrollh () + d)
5310 else G.postRedisplay "linknav";
5312 showlinktype (getlink opaque m);
5313 state.mode <- LinkNav (Ltexact (l.pageno, m));
5316 | None -> viewkeyboard key mask
5317 end;
5318 | _ -> viewkeyboard key mask
5320 if key = @insert
5321 then (
5322 state.mode <- View;
5323 G.postRedisplay "leave linknav"
5325 else
5326 match linknav with
5327 | Ltgendir _ | Ltnotready _ -> viewkeyboard key mask
5328 | Ltexact exact -> doexact exact
5331 let keyboard key mask =
5332 if (key = @g && Wsi.withctrl mask) && not (istextentry state.mode)
5333 then wcmd "interrupt"
5334 else state.uioh <- state.uioh#key key mask
5337 let birdseyekeyboard key mask
5338 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5339 let incr =
5340 match conf.columns with
5341 | Csingle _ -> 1
5342 | Cmulti ((c, _, _), _) -> c
5343 | Csplit _ -> failwith "bird's eye split mode"
5345 let pgh layout = List.fold_left
5346 (fun m l -> max l.pageh m) state.winh layout in
5347 match key with
5348 | @l when Wsi.withctrl mask ->
5349 let y, h = getpageyh pageno in
5350 let top = (state.winh - h) / 2 in
5351 gotoy (max 0 (y - top))
5352 | @enter | @kpenter -> leavebirdseye beye false
5353 | @escape -> leavebirdseye beye true
5354 | @up -> upbirdseye incr beye
5355 | @down -> downbirdseye incr beye
5356 | @left -> upbirdseye 1 beye
5357 | @right -> downbirdseye 1 beye
5359 | @prior ->
5360 begin match state.layout with
5361 | l :: _ ->
5362 if l.pagey != 0
5363 then (
5364 state.mode <- Birdseye (
5365 oconf, leftx, l.pageno, hooverpageno, anchor
5367 gotopage1 l.pageno 0;
5369 else (
5370 let layout = layout state.x (state.y-state.winh)
5371 state.winw
5372 (pgh state.layout) in
5373 match layout with
5374 | [] -> gotoy (clamp (-state.winh))
5375 | l :: _ ->
5376 state.mode <- Birdseye (
5377 oconf, leftx, l.pageno, hooverpageno, anchor
5379 gotopage1 l.pageno 0
5382 | [] -> gotoy (clamp (-state.winh))
5383 end;
5385 | @next ->
5386 begin match List.rev state.layout with
5387 | l :: _ ->
5388 let layout = layout state.x
5389 (state.y + (pgh state.layout))
5390 state.winw state.winh in
5391 begin match layout with
5392 | [] ->
5393 let incr = l.pageh - l.pagevh in
5394 if incr = 0
5395 then (
5396 state.mode <-
5397 Birdseye (
5398 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5400 G.postRedisplay "birdseye pagedown";
5402 else gotoy (clamp (incr + conf.interpagespace*2));
5404 | l :: _ ->
5405 state.mode <-
5406 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5407 gotopage1 l.pageno 0;
5410 | [] -> gotoy (clamp state.winh)
5411 end;
5413 | @home ->
5414 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5415 gotopage1 0 0
5417 | @jend ->
5418 let pageno = state.pagecount - 1 in
5419 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5420 if not (pagevisible state.layout pageno)
5421 then
5422 let h =
5423 match List.rev state.pdims with
5424 | [] -> state.winh
5425 | (_, _, h, _) :: _ -> h
5427 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5428 else G.postRedisplay "birdseye end";
5430 | _ -> viewkeyboard key mask
5433 let drawpage l =
5434 let color =
5435 match state.mode with
5436 | Textentry _ -> scalecolor 0.4
5437 | LinkNav _
5438 | View -> scalecolor 1.0
5439 | Birdseye (_, _, pageno, hooverpageno, _) ->
5440 if l.pageno = hooverpageno
5441 then scalecolor 0.9
5442 else (
5443 if l.pageno = pageno
5444 then (
5445 let c = scalecolor 1.0 in
5446 GlDraw.color c;
5447 GlDraw.line_width 3.0;
5448 let dispx = xadjsb () + l.pagedispx in
5449 linerect
5450 (float (dispx-1)) (float (l.pagedispy-1))
5451 (float (dispx+l.pagevw+1))
5452 (float (l.pagedispy+l.pagevh+1))
5454 GlDraw.line_width 1.0;
5457 else scalecolor 0.8
5460 drawtiles l color;
5463 let postdrawpage l linkindexbase =
5464 match getopaque l.pageno with
5465 | Some opaque ->
5466 if tileready l l.pagex l.pagey
5467 then
5468 let x = l.pagedispx - l.pagex + xadjsb ()
5469 and y = l.pagedispy - l.pagey in
5470 let hlmask =
5471 match conf.columns with
5472 | Csingle _ | Cmulti _ ->
5473 (if conf.hlinks then 1 else 0)
5474 + (if state.glinks
5475 && not (isbirdseye state.mode) then 2 else 0)
5476 | Csplit _ -> 0
5478 let s =
5479 match state.mode with
5480 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5481 | Textentry _
5482 | Birdseye _
5483 | View
5484 | LinkNav _ -> E.s
5486 Hashtbl.find_all state.prects l.pageno |>
5487 List.iter (fun vals -> drawprect opaque x y vals);
5488 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5489 else 0
5490 | _ -> 0
5493 let scrollindicator () =
5494 let sbw, ph, sh = state.uioh#scrollph in
5495 let sbh, pw, sw = state.uioh#scrollpw in
5497 let x0,x1,hx0 =
5498 if conf.leftscroll
5499 then (0, sbw, sbw)
5500 else ((state.winw - sbw), state.winw, 0)
5503 GlDraw.color (0.64, 0.64, 0.64);
5504 filledrect (float x0) 0. (float x1) (float state.winh);
5505 filledrect
5506 (float hx0) (float (state.winh - sbh))
5507 (float (hx0 + wadjsb () + state.winw)) (float state.winh)
5509 GlDraw.color (0.0, 0.0, 0.0);
5511 filledrect (float x0) ph (float x1) (ph +. sh);
5512 let pw = pw +. float hx0 in
5513 filledrect pw (float (state.winh - sbh)) (pw +. sw) (float state.winh);
5516 let showsel () =
5517 match state.mstate with
5518 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5521 | Msel ((x0, y0), (x1, y1)) ->
5522 let identify opaque l px py = Some (opaque, l.pageno, px, py) in
5523 let o0,n0,px0,py0 = onppundermouse identify x0 y0 (~< E.s, -1, 0, 0) in
5524 let _o1,n1,px1,py1 = onppundermouse identify x1 y1 (~< E.s, -1, 0, 0) in
5525 if n0 != -1 && n0 = n1 then seltext o0 (px0, py0, px1, py1);
5528 let showrects = function [] -> () | rects ->
5529 Gl.enable `blend;
5530 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5531 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5532 List.iter
5533 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5534 List.iter (fun l ->
5535 if l.pageno = pageno
5536 then (
5537 let dx = float (l.pagedispx - l.pagex) in
5538 let dy = float (l.pagedispy - l.pagey) in
5539 let r, g, b, alpha = c in
5540 GlDraw.color (r, g, b) ~alpha;
5541 filledrect2 (x0+.dx) (y0+.dy)
5542 (x1+.dx) (y1+.dy)
5543 (x3+.dx) (y3+.dy)
5544 (x2+.dx) (y2+.dy);
5546 ) state.layout
5547 ) rects
5549 Gl.disable `blend;
5552 let display () =
5553 begin match conf.columns, state.layout with
5554 | Csingle _, _ :: _ ->
5555 GlDraw.color (scalecolor2 conf.bgcolor);
5556 let y =
5557 List.fold_left (fun y l ->
5558 let x0 = 0 in
5559 let y0 = y in
5560 let x1 = l.pagedispx + xadjsb () in
5561 let y1 = (l.pagedispy + l.pagevh) in
5562 filledrect (float x0) (float y0) (float x1) (float y1);
5563 let x0 = x1 + l.pagevw in
5564 let x1 = state.winw in
5565 filledrect1 (float x0) (float y0) (float x1) (float y1);
5566 if y != l.pagedispy
5567 then (
5568 let x0 = 0
5569 and x1 = state.winw in
5570 let y0 = y
5571 and y1 = l.pagedispy in
5572 filledrect1 (float x0) (float y0) (float x1) (float y1);
5574 l.pagedispy + l.pagevh) 0 state.layout
5576 let x0 = 0
5577 and x1 = state.winw in
5578 let y0 = y
5579 and y1 = state.winh in
5580 filledrect1 (float x0) (float y0) (float x1) (float y1)
5581 | (Cmulti _ | Csplit _), _ | Csingle _, [] ->
5582 GlClear.color (scalecolor2 conf.bgcolor);
5583 GlClear.clear [`color];
5584 end;
5585 List.iter drawpage state.layout;
5586 let rects =
5587 match state.mode with
5588 | LinkNav (Ltexact (pageno, linkno)) ->
5589 begin match getopaque pageno with
5590 | Some opaque ->
5591 let dx = xadjsb () in
5592 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5593 let x0 = x0 + dx and x1 = x1 + dx in
5594 let color = (0.0, 0.0, 0.5, 0.5) in
5595 (pageno, color, (
5596 float x0, float y0,
5597 float x1, float y0,
5598 float x1, float y1,
5599 float x0, float y1)
5600 ) :: state.rects
5601 | None -> state.rects
5603 | LinkNav (Ltgendir _) | LinkNav (Ltnotready _)
5604 | Birdseye _
5605 | Textentry _
5606 | View -> state.rects
5608 showrects rects;
5609 let rec postloop linkindexbase = function
5610 | l :: rest ->
5611 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5612 postloop linkindexbase rest
5613 | [] -> ()
5615 showsel ();
5616 postloop 0 state.layout;
5617 state.uioh#display;
5618 begin match state.mstate with
5619 | Mzoomrect ((x0, y0), (x1, y1)) ->
5620 Gl.enable `blend;
5621 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5622 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5623 filledrect (float x0) (float y0) (float x1) (float y1);
5624 Gl.disable `blend;
5625 | Msel _
5626 | Mpan _
5627 | Mscrolly | Mscrollx
5628 | Mzoom _
5629 | Mnone -> ()
5630 end;
5631 enttext ();
5632 scrollindicator ();
5633 Wsi.swapb ();
5636 let zoomrect x y x1 y1 =
5637 let x0 = min x x1
5638 and x1 = max x x1
5639 and y0 = min y y1 in
5640 gotoy (state.y + y0);
5641 state.anchor <- getanchor ();
5642 let zoom = (float state.w) /. float (x1 - x0) in
5643 let margin =
5644 let simple () =
5645 let adjw = wadjsb () + state.winw in
5646 if state.w < adjw
5647 then (adjw - state.w) / 2
5648 else 0
5650 match conf.fitmodel with
5651 | FitWidth | FitProportional -> simple ()
5652 | FitPage ->
5653 match conf.columns with
5654 | Csplit _ ->
5655 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
5656 | Cmulti _ | Csingle _ -> simple ()
5658 state.x <- (state.x + margin) - x0;
5659 setzoom zoom;
5660 resetmstate ();
5663 let annot inline x y =
5664 match unproject x y with
5665 | Some (opaque, n, ux, uy) ->
5666 let add text =
5667 addannot opaque ux uy text;
5668 wcmd "freepage %s" (~> opaque);
5669 Hashtbl.remove state.pagemap (n, state.gen);
5670 flushtiles ();
5671 gotoy state.y
5673 if inline
5674 then
5675 let ondone s = add s in
5676 let mode = state.mode in
5677 state.mode <- Textentry (
5678 ("annotation: ", E.s, None, textentry, ondone, true),
5679 fun _ -> state.mode <- mode);
5680 state.text <- E.s;
5681 enttext ();
5682 G.postRedisplay "annot"
5683 else
5684 add @@ getusertext E.s
5685 | _ -> ()
5688 let zoomblock x y =
5689 let g opaque l px py =
5690 match rectofblock opaque px py with
5691 | Some a ->
5692 let x0 = a.(0) -. 20. in
5693 let x1 = a.(1) +. 20. in
5694 let y0 = a.(2) -. 20. in
5695 let zoom = (float state.w) /. (x1 -. x0) in
5696 let pagey = getpagey l.pageno in
5697 gotoy_and_clear_text (pagey + truncate y0);
5698 state.anchor <- getanchor ();
5699 let margin = (state.w - l.pagew)/2 in
5700 state.x <- -truncate x0 - margin;
5701 setzoom zoom;
5702 None
5703 | None -> None
5705 match conf.columns with
5706 | Csplit _ ->
5707 impmsg "block zooming does not work properly in split columns mode"
5708 | Cmulti _ | Csingle _ -> onppundermouse g x y ()
5711 let scrollx x =
5712 let winw = wadjsb () + state.winw - 1 in
5713 let s = float x /. float winw in
5714 let destx = truncate (float (state.w + winw) *. s) in
5715 state.x <- winw - destx;
5716 gotoy_and_clear_text state.y;
5717 state.mstate <- Mscrollx;
5720 let scrolly y =
5721 let s = float y /. float state.winh in
5722 let desty = truncate (float (state.maxy - state.winh) *. s) in
5723 gotoy_and_clear_text desty;
5724 state.mstate <- Mscrolly;
5727 let viewmulticlick clicks x y mask =
5728 let g opaque l px py =
5729 let mark =
5730 match clicks with
5731 | 2 -> Mark_word
5732 | 3 -> Mark_line
5733 | 4 -> Mark_block
5734 | _ -> Mark_page
5736 if markunder opaque px py mark
5737 then (
5738 Some (fun () ->
5739 let dopipe cmd =
5740 match getopaque l.pageno with
5741 | None -> ()
5742 | Some opaque -> pipesel opaque cmd
5744 state.roam <- (fun () -> dopipe conf.paxcmd);
5745 if not (Wsi.withctrl mask) then dopipe conf.selcmd;
5748 else None
5750 G.postRedisplay "viewmulticlick";
5751 onppundermouse g x y (fun () -> impmsg "nothing to select") ();
5754 let canselect () =
5755 match conf.columns with
5756 | Csplit _ -> false
5757 | Csingle _ | Cmulti _ -> conf.angle mod 360 = 0
5760 let viewmouse button down x y mask =
5761 match button with
5762 | n when (n == 4 || n == 5) && not down ->
5763 if Wsi.withctrl mask
5764 then (
5765 match state.mstate with
5766 | Mzoom (oldn, i) ->
5767 if oldn = n
5768 then (
5769 if i = 2
5770 then
5771 let incr =
5772 match n with
5773 | 5 ->
5774 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5775 | _ ->
5776 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5778 let zoom = conf.zoom -. incr in
5779 setzoom zoom;
5780 state.mstate <- Mzoom (n, 0);
5781 else
5782 state.mstate <- Mzoom (n, i+1);
5784 else state.mstate <- Mzoom (n, 0)
5786 | Msel _
5787 | Mpan _
5788 | Mscrolly | Mscrollx
5789 | Mzoomrect _
5790 | Mnone -> state.mstate <- Mzoom (n, 0)
5792 else (
5793 match state.autoscroll with
5794 | Some step -> setautoscrollspeed step (n=4)
5795 | None ->
5796 if conf.wheelbypage || conf.presentation
5797 then (
5798 if n = 4
5799 then prevpage ()
5800 else nextpage ()
5802 else
5803 let incr =
5804 if n = 4
5805 then -conf.scrollstep
5806 else conf.scrollstep
5808 let incr = incr * 2 in
5809 let y = clamp incr in
5810 gotoy_and_clear_text y
5813 | n when (n = 6 || n = 7) && not down && canpan () ->
5814 state.x <-
5815 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
5816 gotoy_and_clear_text state.y
5818 | 1 when Wsi.withshift mask ->
5819 state.mstate <- Mnone;
5820 if not down
5821 then (
5822 match unproject x y with
5823 | None -> ()
5824 | Some (_, pageno, ux, uy) ->
5825 let cmd = Printf.sprintf
5826 "%s %s %d %d %d"
5827 conf.stcmd state.path pageno ux uy
5829 match spawn cmd [] with
5830 | (exception exn) ->
5831 impmsg "execution of synctex command(%S) failed: %S"
5832 conf.stcmd @@ exntos exn
5833 | _pid -> ()
5836 | 1 when Wsi.withctrl mask ->
5837 if down
5838 then (
5839 Wsi.setcursor Wsi.CURSOR_FLEUR;
5840 state.mstate <- Mpan (x, y)
5842 else
5843 state.mstate <- Mnone
5845 | 3 ->
5846 if down
5847 then (
5848 if Wsi.withshift mask
5849 then (
5850 annot conf.annotinline x y;
5851 G.postRedisplay "addannot"
5853 else
5854 let p = (x, y) in
5855 Wsi.setcursor Wsi.CURSOR_CYCLE;
5856 state.mstate <- Mzoomrect (p, p)
5858 else (
5859 match state.mstate with
5860 | Mzoomrect ((x0, y0), _) ->
5861 if abs (x-x0) > 10 && abs (y - y0) > 10
5862 then zoomrect x0 y0 x y
5863 else (
5864 resetmstate ();
5865 G.postRedisplay "kill accidental zoom rect";
5867 | Msel _
5868 | Mpan _
5869 | Mscrolly | Mscrollx
5870 | Mzoom _
5871 | Mnone ->
5872 resetmstate ()
5875 | 1 when vscrollhit x ->
5876 if down
5877 then
5878 let _, position, sh = state.uioh#scrollph in
5879 if y > truncate position && y < truncate (position +. sh)
5880 then state.mstate <- Mscrolly
5881 else scrolly y
5882 else
5883 state.mstate <- Mnone
5885 | 1 when y > state.winh - hscrollh () ->
5886 if down
5887 then
5888 let _, position, sw = state.uioh#scrollpw in
5889 if x > truncate position && x < truncate (position +. sw)
5890 then state.mstate <- Mscrollx
5891 else scrollx x
5892 else
5893 state.mstate <- Mnone
5895 | 1 when state.bzoom -> if not down then zoomblock x y
5897 | 1 ->
5898 let dest = if down then getunder x y else Unone in
5899 begin match dest with
5900 | Ulinkgoto _
5901 | Ulinkuri _
5902 | Uremote _ | Uremotedest _
5903 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5904 gotounder dest
5906 | Unone when down ->
5907 Wsi.setcursor Wsi.CURSOR_FLEUR;
5908 state.mstate <- Mpan (x, y);
5910 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
5912 | Unone | Utext _ ->
5913 if down
5914 then (
5915 if canselect ()
5916 then (
5917 state.mstate <- Msel ((x, y), (x, y));
5918 G.postRedisplay "mouse select";
5921 else (
5922 match state.mstate with
5923 | Mnone -> ()
5925 | Mzoom _ | Mscrollx | Mscrolly ->
5926 state.mstate <- Mnone
5928 | Mzoomrect ((x0, y0), _) ->
5929 zoomrect x0 y0 x y
5931 | Mpan _ ->
5932 Wsi.setcursor Wsi.CURSOR_INHERIT;
5933 state.mstate <- Mnone
5935 | Msel ((x0, y0), (x1, y1)) ->
5936 let rec loop = function
5937 | [] -> ()
5938 | l :: rest ->
5939 let inside =
5940 let a0 = l.pagedispy in
5941 let a1 = a0 + l.pagevh in
5942 let b0 = l.pagedispx in
5943 let b1 = b0 + l.pagevw in
5944 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5945 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5947 if inside
5948 then
5949 match getopaque l.pageno with
5950 | Some opaque ->
5951 let dosel cmd () =
5952 match Unix.pipe () with
5953 | (exception exn) ->
5954 impmsg "cannot create sel pipe: %s" @@
5955 exntos exn;
5956 | (r, w) ->
5957 let clo what fd =
5958 Ne.clo fd (fun msg ->
5959 dolog "%s close failed: %s" what msg)
5961 let pid =
5962 try spawn cmd [r, 0; w, -1]
5963 with exn ->
5964 dolog "cannot execute %S: %s"
5965 cmd @@ exntos exn;
5968 if pid > 0
5969 then (
5970 copysel w opaque;
5971 G.postRedisplay "copysel";
5973 else clo "Msel pipe/w" w;
5974 clo "Msel pipe/r" r;
5976 dosel conf.selcmd ();
5977 state.roam <- dosel conf.paxcmd;
5978 | None -> ()
5979 else loop rest
5981 loop state.layout;
5982 resetmstate ();
5986 | _ -> ()
5989 let birdseyemouse button down x y mask
5990 (conf, leftx, _, hooverpageno, anchor) =
5991 match button with
5992 | 1 when down ->
5993 let rec loop = function
5994 | [] -> ()
5995 | l :: rest ->
5996 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5997 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5998 then (
5999 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
6001 else loop rest
6003 loop state.layout
6004 | 3 -> ()
6005 | _ -> viewmouse button down x y mask
6008 let uioh = object
6009 method display = ()
6011 method key key mask =
6012 begin match state.mode with
6013 | Textentry textentry -> textentrykeyboard key mask textentry
6014 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6015 | View -> viewkeyboard key mask
6016 | LinkNav linknav -> linknavkeyboard key mask linknav
6017 end;
6018 state.uioh
6020 method button button bstate x y mask =
6021 begin match state.mode with
6022 | LinkNav _
6023 | View -> viewmouse button bstate x y mask
6024 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6025 | Textentry _ -> ()
6026 end;
6027 state.uioh
6029 method multiclick clicks x y mask =
6030 begin match state.mode with
6031 | LinkNav _
6032 | View -> viewmulticlick clicks x y mask
6033 | Birdseye _
6034 | Textentry _ -> ()
6035 end;
6036 state.uioh
6038 method motion x y =
6039 begin match state.mode with
6040 | Textentry _ -> ()
6041 | View | Birdseye _ | LinkNav _ ->
6042 match state.mstate with
6043 | Mzoom _ | Mnone -> ()
6045 | Mpan (x0, y0) ->
6046 let dx = x - x0
6047 and dy = y0 - y in
6048 state.mstate <- Mpan (x, y);
6049 if canpan ()
6050 then state.x <- panbound (state.x + dx);
6051 let y = clamp dy in
6052 gotoy_and_clear_text y
6054 | Msel (a, _) ->
6055 state.mstate <- Msel (a, (x, y));
6056 G.postRedisplay "motion select";
6058 | Mscrolly ->
6059 let y = min state.winh (max 0 y) in
6060 scrolly y
6062 | Mscrollx ->
6063 let x = min state.winw (max 0 x) in
6064 scrollx x
6066 | Mzoomrect (p0, _) ->
6067 state.mstate <- Mzoomrect (p0, (x, y));
6068 G.postRedisplay "motion zoomrect";
6069 end;
6070 state.uioh
6072 method pmotion x y =
6073 begin match state.mode with
6074 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6075 let rec loop = function
6076 | [] ->
6077 if hooverpageno != -1
6078 then (
6079 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6080 G.postRedisplay "pmotion birdseye no hoover";
6082 | l :: rest ->
6083 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6084 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6085 then (
6086 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6087 G.postRedisplay "pmotion birdseye hoover";
6089 else loop rest
6091 loop state.layout
6093 | Textentry _ -> ()
6095 | LinkNav _
6096 | View ->
6097 match state.mstate with
6098 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ -> ()
6099 | Mnone ->
6100 updateunder x y;
6101 if canselect ()
6102 then
6103 match conf.pax with
6104 | None -> ()
6105 | Some r ->
6106 let past, _, _ = !r in
6107 let now = now () in
6108 let delta = now -. past in
6109 if delta > 0.01
6110 then paxunder x y
6111 else r := (now, x, y)
6112 end;
6113 state.uioh
6115 method infochanged _ = ()
6117 method scrollph =
6118 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6119 let p, h =
6120 if maxy = 0
6121 then 0.0, float state.winh
6122 else scrollph state.y maxy
6124 vscrollw (), p, h
6126 method scrollpw =
6127 let winw = wadjsb () + state.winw in
6128 let fwinw = float winw in
6129 let sw =
6130 let sw = fwinw /. float state.w in
6131 let sw = fwinw *. sw in
6132 max sw (float conf.scrollh)
6134 let position =
6135 let maxx = state.w + winw in
6136 let x = winw - state.x in
6137 let percent = float x /. float maxx in
6138 (fwinw -. sw) *. percent
6140 hscrollh (), position, sw
6142 method modehash =
6143 let modename =
6144 match state.mode with
6145 | LinkNav _ -> "links"
6146 | Textentry _ -> "textentry"
6147 | Birdseye _ -> "birdseye"
6148 | View -> "view"
6150 findkeyhash conf modename
6152 method eformsgs = true
6153 method alwaysscrolly = false
6154 end;;
6156 let addrect pageno r g b a x0 y0 x1 y1 =
6157 Hashtbl.add state.prects pageno [|r; g; b; a; x0; y0; x1; y1|];
6160 let ract cmds =
6161 let cl = splitatspace cmds in
6162 let scan s fmt f =
6163 try Scanf.sscanf s fmt f
6164 with exn ->
6165 adderrfmt "remote exec"
6166 "error processing '%S': %s\n" cmds @@ exntos exn
6168 let rectx s pageno (r, g, b, a) x0 y0 x1 y1 =
6169 vlog "%s page %d color (%f %f %f %f) x0,y0,x1,y1 = %f %f %f %f"
6170 s pageno r g b a x0 y0 x1 y1;
6171 onpagerect
6172 pageno
6173 (fun w h ->
6174 let _,w1,h1,_ = getpagedim pageno in
6175 let sw = float w1 /. float w
6176 and sh = float h1 /. float h in
6177 let x0s = x0 *. sw
6178 and x1s = x1 *. sw
6179 and y0s = y0 *. sh
6180 and y1s = y1 *. sh in
6181 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
6182 let color = (r, g, b, a) in
6183 if conf.verbose then debugrect rect;
6184 state.rects <- (pageno, color, rect) :: state.rects;
6185 G.postRedisplay s;
6188 match cl with
6189 | "reload" :: [] -> reload ()
6190 | "goto" :: args :: [] ->
6191 scan args "%u %f %f"
6192 (fun pageno x y ->
6193 let cmd, _ = state.geomcmds in
6194 if emptystr cmd
6195 then gotopagexy !wtmode pageno x y
6196 else
6197 let f prevf () =
6198 gotopagexy !wtmode pageno x y;
6199 prevf ()
6201 state.reprf <- f state.reprf
6203 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
6204 | "gotor" :: args :: [] ->
6205 scan args "%S %u"
6206 (fun filename pageno -> gotounder (Uremote (filename, pageno)))
6207 | "gotord" :: args :: [] ->
6208 scan args "%S %S"
6209 (fun filename dest -> gotounder (Uremotedest (filename, dest)))
6210 | "rect" :: args :: [] ->
6211 scan args "%u %u %f %f %f %f"
6212 (fun pageno c x0 y0 x1 y1 ->
6213 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
6214 rectx "rect" pageno color x0 y0 x1 y1;
6216 | "prect" :: args :: [] ->
6217 scan args "%u %f %f %f %f %f %f %f %f"
6218 (fun pageno r g b alpha x0 y0 x1 y1 ->
6219 addrect pageno r g b alpha x0 y0 x1 y1;
6220 G.postRedisplay "prect"
6222 | "pgoto" :: args :: [] ->
6223 scan args "%u %f %f"
6224 (fun pageno x y ->
6225 let optopaque =
6226 match getopaque pageno with
6227 | Some opaque -> opaque
6228 | None -> ~< E.s
6230 pgoto optopaque pageno x y;
6231 let rec fixx = function
6232 | [] -> ()
6233 | l :: rest ->
6234 if l.pageno = pageno
6235 then (
6236 state.x <- state.x - l.pagedispx;
6237 gotoy state.y;
6239 else fixx rest
6241 let layout =
6242 let mult =
6243 match conf.columns with
6244 | Csingle _ | Csplit _ -> 1
6245 | Cmulti ((n, _, _), _) -> n
6247 layout 0 state.y (state.winw * mult) state.winh
6249 fixx layout
6251 | "activatewin" :: [] -> Wsi.activatewin ()
6252 | "quit" :: [] -> raise Quit
6253 | "clearrects" :: [] ->
6254 Hashtbl.clear state.prects;
6255 G.postRedisplay "clearrects"
6256 | _ ->
6257 adderrfmt "remote command"
6258 "error processing remote command: %S\n" cmds;
6261 let remote =
6262 let scratch = Bytes.create 80 in
6263 let buf = Buffer.create 80 in
6264 fun fd ->
6265 match tempfailureretry (Unix.read fd scratch 0) 80 with
6266 | (exception Unix.Unix_error (Unix.EAGAIN, _, _)) -> None
6267 | 0 ->
6268 Unix.close fd;
6269 if Buffer.length buf > 0
6270 then (
6271 let s = Buffer.contents buf in
6272 Buffer.clear buf;
6273 ract s;
6275 None
6276 | n ->
6277 let rec eat ppos =
6278 let nlpos =
6279 match Bytes.index_from scratch ppos '\n' with
6280 | pos -> if pos >= n then -1 else pos
6281 | (exception Not_found) -> -1
6283 if nlpos >= 0
6284 then (
6285 Buffer.add_subbytes buf scratch ppos (nlpos-ppos);
6286 let s = Buffer.contents buf in
6287 Buffer.clear buf;
6288 ract s;
6289 eat (nlpos+1);
6291 else (
6292 Buffer.add_subbytes buf scratch ppos (n-ppos);
6293 Some fd
6295 in eat 0
6298 let remoteopen path =
6299 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
6300 with exn ->
6301 adderrfmt "remoteopen" "error opening %S: %s" path @@ exntos exn;
6302 None
6305 let () =
6306 let gcconfig = ref E.s in
6307 let trimcachepath = ref E.s in
6308 let rcmdpath = ref E.s in
6309 let pageno = ref None in
6310 let rootwid = ref 0 in
6311 let openlast = ref false in
6312 let nofc = ref false in
6313 let doreap = ref false in
6314 selfexec := Sys.executable_name;
6315 Arg.parse
6316 (Arg.align
6317 [("-p", Arg.String (fun s -> state.password <- s),
6318 "<password> Set password");
6320 ("-f", Arg.String
6321 (fun s ->
6322 Config.fontpath := s;
6323 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
6325 "<path> Set path to the user interface font");
6327 ("-c", Arg.String
6328 (fun s ->
6329 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
6330 Config.confpath := s),
6331 "<path> Set path to the configuration file");
6333 ("-last", Arg.Set openlast, " Open last document");
6335 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
6336 "<page-number> Jump to page");
6338 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6339 "<path> Set path to the trim cache file");
6341 ("-dest", Arg.String (fun s -> state.nameddest <- s),
6342 "<named-destination> Set named destination");
6344 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
6345 ("-cxack", Arg.Set cxack, " Cut corners");
6347 ("-remote", Arg.String (fun s -> rcmdpath := s),
6348 "<path> Set path to the remote commands source");
6350 ("-origin", Arg.String (fun s -> state.origin <- s),
6351 "<original-path> Set original path");
6353 ("-gc", Arg.Set_string gcconfig,
6354 "<script-path> Collect garbage with the help of a script");
6356 ("-nofc", Arg.Set nofc, " Do not use fontconfig");
6358 ("-v", Arg.Unit (fun () ->
6359 Printf.printf
6360 "%s\nconfiguration path: %s\n"
6361 (version ())
6362 Config.defconfpath
6364 exit 0), " Print version and exit");
6366 ("-embed", Arg.Set_int rootwid,
6367 "<window-id> Embed into window")
6370 (fun s -> state.path <- s)
6371 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6373 if !wtmode
6374 then selfexec := !selfexec ^ " -wtmode";
6376 let histmode = emptystr state.path && not !openlast in
6378 if not (Config.load !openlast)
6379 then dolog "failed to load configuration";
6380 begin match !pageno with
6381 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
6382 | None -> ()
6383 end;
6385 if nonemptystr !gcconfig
6386 then (
6387 let (c, s) =
6388 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6389 | (exception exn) -> error "socketpair for gc failed: %s" @@ exntos exn
6390 | fds -> fds
6392 match spawn !gcconfig [(c, 0); (c, 1); (s, -1)] with
6393 | (exception exn) -> error "failed to execute gc script: %s" @@ exntos exn
6394 | _pid ->
6395 Ne.clo c @@ (fun s -> error "failed to close gc fd %s" s);
6396 Config.gc s;
6397 exit 0
6400 let wsfd, winw, winh = Wsi.init (object (self)
6401 val mutable m_clicks = 0
6402 val mutable m_click_x = 0
6403 val mutable m_click_y = 0
6404 val mutable m_lastclicktime = infinity
6406 method private cleanup =
6407 state.roam <- noroam;
6408 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap
6409 method expose = G.postRedisplay "expose"
6410 method visible v =
6411 let name =
6412 match v with
6413 | Wsi.Unobscured -> "unobscured"
6414 | Wsi.PartiallyObscured -> "partiallyobscured"
6415 | Wsi.FullyObscured -> "fullyobscured"
6417 vlog "visibility change %s" name
6418 method display = display ()
6419 method map mapped = vlog "mapped %b" mapped
6420 method reshape w h =
6421 self#cleanup;
6422 reshape w h
6423 method mouse b d x y m =
6424 if d && canselect ()
6425 then (
6426 (* http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx *)
6427 m_click_x <- x;
6428 m_click_y <- y;
6429 if b = 1
6430 then (
6431 let t = now () in
6432 if abs x - m_click_x > 10
6433 || abs y - m_click_y > 10
6434 || abs_float (t -. m_lastclicktime) > 0.3
6435 then m_clicks <- 0;
6436 m_clicks <- m_clicks + 1;
6437 m_lastclicktime <- t;
6438 if m_clicks = 1
6439 then (
6440 self#cleanup;
6441 G.postRedisplay "cleanup";
6442 state.uioh <- state.uioh#button b d x y m;
6444 else state.uioh <- state.uioh#multiclick m_clicks x y m
6446 else (
6447 self#cleanup;
6448 m_clicks <- 0;
6449 m_lastclicktime <- infinity;
6450 state.uioh <- state.uioh#button b d x y m
6453 else (
6454 state.uioh <- state.uioh#button b d x y m
6456 method motion x y =
6457 state.mpos <- (x, y);
6458 state.uioh <- state.uioh#motion x y
6459 method pmotion x y =
6460 state.mpos <- (x, y);
6461 state.uioh <- state.uioh#pmotion x y
6462 method key k m =
6463 let mascm = m land (
6464 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6465 ) in
6466 let keyboard k m =
6467 let x = state.x and y = state.y in
6468 keyboard k m;
6469 if x != state.x || y != state.y then self#cleanup
6471 match state.keystate with
6472 | KSnone ->
6473 let km = k, mascm in
6474 begin
6475 match
6476 let modehash = state.uioh#modehash in
6477 try Hashtbl.find modehash km
6478 with Not_found ->
6479 try Hashtbl.find (findkeyhash conf "global") km
6480 with Not_found -> KMinsrt (k, m)
6481 with
6482 | KMinsrt (k, m) -> keyboard k m
6483 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6484 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6486 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6487 List.iter (fun (k, m) -> keyboard k m) insrt;
6488 state.keystate <- KSnone
6489 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6490 state.keystate <- KSinto (keys, insrt)
6491 | KSinto _ -> state.keystate <- KSnone
6493 method enter x y =
6494 state.mpos <- (x, y);
6495 state.uioh <- state.uioh#pmotion x y
6496 method leave = state.mpos <- (-1, -1)
6497 method winstate wsl = state.winstate <- wsl
6498 method quit = raise Quit
6499 end) !rootwid conf.cwinw conf.cwinh platform in
6501 setbgcol conf.bgcolor;
6502 state.wsfd <- wsfd;
6504 if not (
6505 List.exists GlMisc.check_extension
6506 [ "GL_ARB_texture_rectangle"
6507 ; "GL_EXT_texture_recangle"
6508 ; "GL_NV_texture_rectangle" ]
6510 then (dolog "OpenGL does not suppport rectangular textures"; exit 1);
6512 if (
6513 let r = GlMisc.get_string `renderer in
6514 let p = "Mesa DRI Intel(" in
6515 let l = String.length p in
6516 String.length r > l && String.sub r 0 l = p
6518 then (
6519 defconf.sliceheight <- 1024;
6520 defconf.texcount <- 32;
6521 defconf.usepbo <- true;
6524 let cs, ss =
6525 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6526 | (exception exn) ->
6527 dolog "socketpair failed: %s" @@ exntos exn;
6528 exit 1
6529 | (r, w) ->
6530 cloexec r;
6531 cloexec w;
6532 r, w
6535 setcheckers conf.checkers;
6537 opengl_has_pbo := GlMisc.check_extension "GL_ARB_pixel_buffer_object";
6539 init cs (
6540 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
6541 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6542 !Config.fontpath, !trimcachepath,
6543 !opengl_has_pbo,
6544 not !nofc
6546 List.iter GlArray.enable [`texture_coord; `vertex];
6547 state.ss <- ss;
6548 reshape ~firsttime:true winw winh;
6549 state.uioh <- uioh;
6550 if histmode
6551 then (
6552 Wsi.settitle "llpp (history)";
6553 enterhistmode ();
6555 else (
6556 state.text <- "Opening " ^ (mbtoutf8 state.path);
6557 opendoc state.path state.password;
6559 display ();
6560 Wsi.mapwin ();
6561 Wsi.setcursor Wsi.CURSOR_INHERIT;
6562 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
6564 let rec reap () =
6565 match Unix.waitpid [Unix.WNOHANG] ~-1 with
6566 | (exception (Unix.Unix_error (Unix.ECHILD, _, _))) -> ()
6567 | (exception exn) -> dolog "Unix.waitpid: %s" @@ exntos exn
6568 | 0, _ -> ()
6569 | _pid, _status -> reap ()
6571 Sys.set_signal Sys.sigchld (Sys.Signal_handle (fun _ -> doreap := true));
6573 let optrfd =
6574 ref (
6575 if nonemptystr !rcmdpath
6576 then remoteopen !rcmdpath
6577 else None
6581 let rec loop deadline =
6582 if !doreap
6583 then (
6584 doreap := false;
6585 reap ()
6587 let r = [state.ss; state.wsfd] in
6588 let r =
6589 match !optrfd with
6590 | None -> r
6591 | Some fd -> fd :: r
6593 if state.redisplay
6594 then (
6595 state.redisplay <- false;
6596 display ();
6598 let timeout =
6599 let now = now () in
6600 if deadline > now
6601 then (
6602 if deadline = infinity
6603 then ~-.1.0
6604 else max 0.0 (deadline -. now)
6606 else 0.0
6608 let r, _, _ =
6609 try Unix.select r [] [] timeout
6610 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6612 begin match r with
6613 | [] ->
6614 state.ghyll None;
6615 let newdeadline =
6616 if state.ghyll == noghyll
6617 then
6618 match state.autoscroll with
6619 | Some step when step != 0 ->
6620 let y = state.y + step in
6621 let fy = if conf.maxhfit then state.winh else 0 in
6622 let y =
6623 if y < 0
6624 then state.maxy - fy
6625 else if y >= state.maxy - fy then 0 else y
6627 if state.mode = View
6628 then gotoy_and_clear_text y
6629 else gotoy y;
6630 deadline +. 0.01
6631 | _ -> infinity
6632 else deadline +. 0.01
6634 loop newdeadline
6636 | l ->
6637 let rec checkfds = function
6638 | [] -> ()
6639 | fd :: rest when fd = state.ss ->
6640 let cmd = rcmd state.ss in
6641 act cmd;
6642 checkfds rest
6644 | fd :: rest when fd = state.wsfd ->
6645 Wsi.readresp fd;
6646 checkfds rest
6648 | fd :: rest when Some fd = !optrfd ->
6649 begin match remote fd with
6650 | None -> optrfd := remoteopen !rcmdpath;
6651 | opt -> optrfd := opt
6652 end;
6653 checkfds rest
6655 | _ :: rest ->
6656 dolog "select returned unknown descriptor";
6657 checkfds rest
6659 checkfds l;
6660 let newdeadline =
6661 let deadline1 =
6662 if deadline = infinity
6663 then now () +. 0.01
6664 else deadline
6666 match state.autoscroll with
6667 | Some step when step != 0 -> deadline1
6668 | _ -> if state.ghyll == noghyll then infinity else deadline1
6670 loop newdeadline
6671 end;
6674 loop infinity;
6675 with Quit ->
6676 Config.save leavebirdseye;
6677 if hasunsavedchanges ()
6678 then save ();