Cosmetics
[llpp.git] / main.ml
blob61a7f3f57c3d0052d9c1608d16bd75f85a9f16d9
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 c with
382 | '0' .. '9' ->
383 let text = addchar text c in
384 TEcont text
386 | 'k' | 'm' | 'g' | 'K' | 'M' | 'G' ->
387 let text = addchar text @@ asciilower 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 spl = 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 spl 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", args->
1907 if args = "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 if key >= 32 && key < 127
1991 then
1992 let text = addchar text (Char.chr key) in
1993 linknact (fun under -> state.text <- undertext ~nopath:true under) text;
1994 TEcont text
1995 else (
1996 state.text <- Printf.sprintf "invalid char %d" key;
1997 TEcont text
2001 let textentry text key =
2002 if key land 0xff00 = 0xff00
2003 then TEcont text
2004 else TEcont (text ^ toutf8 key)
2007 let reqlayout angle fitmodel =
2008 match state.throttle with
2009 | None ->
2010 if nogeomcmds state.geomcmds
2011 then state.anchor <- getanchor ();
2012 conf.angle <- angle mod 360;
2013 if conf.angle != 0
2014 then (
2015 match state.mode with
2016 | LinkNav _ -> state.mode <- View
2017 | Birdseye _
2018 | Textentry _
2019 | View -> ()
2021 conf.fitmodel <- fitmodel;
2022 invalidate "reqlayout"
2023 (fun () ->
2024 wcmd "reqlayout %d %d %d"
2025 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
2027 | _ -> ()
2030 let settrim trimmargins trimfuzz =
2031 if nogeomcmds state.geomcmds
2032 then state.anchor <- getanchor ();
2033 conf.trimmargins <- trimmargins;
2034 conf.trimfuzz <- trimfuzz;
2035 let x0, y0, x1, y1 = trimfuzz in
2036 invalidate "settrim"
2037 (fun () ->
2038 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2039 flushpages ();
2042 let setzoom zoom =
2043 match state.throttle with
2044 | None ->
2045 let zoom = max 0.0001 zoom in
2046 if zoom <> conf.zoom
2047 then (
2048 state.prevzoom <- (conf.zoom, state.x);
2049 conf.zoom <- zoom;
2050 reshape state.winw state.winh;
2051 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2054 | Some (layout, y, started) ->
2055 let time =
2056 match conf.maxwait with
2057 | None -> 0.0
2058 | Some t -> t
2060 let dt = now () -. started in
2061 if dt > time
2062 then (
2063 state.y <- y;
2064 load layout;
2068 let setcolumns mode columns coverA coverB =
2069 state.prevcolumns <- Some (conf.columns, conf.zoom);
2070 if columns < 0
2071 then (
2072 if isbirdseye mode
2073 then impmsg "split mode doesn't work in bird's eye"
2074 else (
2075 conf.columns <- Csplit (-columns, E.a);
2076 state.x <- 0;
2077 conf.zoom <- 1.0;
2080 else (
2081 if columns < 2
2082 then (
2083 conf.columns <- Csingle E.a;
2084 state.x <- 0;
2085 setzoom 1.0;
2087 else (
2088 conf.columns <- Cmulti ((columns, coverA, coverB), E.a);
2089 conf.zoom <- 1.0;
2092 reshape state.winw state.winh;
2095 let resetmstate () =
2096 state.mstate <- Mnone;
2097 Wsi.setcursor Wsi.CURSOR_INHERIT;
2100 let enterbirdseye () =
2101 let zoom = float conf.thumbw /. float state.winw in
2102 let birdseyepageno =
2103 let cy = state.winh / 2 in
2104 let fold = function
2105 | [] -> 0
2106 | l :: rest ->
2107 let rec fold best = function
2108 | [] -> best.pageno
2109 | l :: rest ->
2110 let d = cy - (l.pagedispy + l.pagevh/2)
2111 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2112 if abs d < abs dbest
2113 then fold l rest
2114 else best.pageno
2115 in fold l rest
2117 fold state.layout
2119 state.mode <- Birdseye (
2120 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2122 resetmstate ();
2123 conf.zoom <- zoom;
2124 conf.presentation <- false;
2125 conf.interpagespace <- 10;
2126 conf.hlinks <- false;
2127 conf.fitmodel <- FitPage;
2128 state.x <- 0;
2129 conf.maxwait <- None;
2130 conf.columns <- (
2131 match conf.beyecolumns with
2132 | Some c ->
2133 conf.zoom <- 1.0;
2134 Cmulti ((c, 0, 0), E.a)
2135 | None -> Csingle E.a
2137 if conf.verbose
2138 then
2139 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2140 (100.0*.zoom)
2141 else
2142 state.text <- E.s
2144 reshape state.winw state.winh;
2147 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2148 state.mode <- View;
2149 conf.zoom <- c.zoom;
2150 conf.presentation <- c.presentation;
2151 conf.interpagespace <- c.interpagespace;
2152 conf.maxwait <- c.maxwait;
2153 conf.hlinks <- c.hlinks;
2154 conf.fitmodel <- c.fitmodel;
2155 conf.beyecolumns <- (
2156 match conf.columns with
2157 | Cmulti ((c, _, _), _) -> Some c
2158 | Csingle _ -> None
2159 | Csplit _ -> failwith "leaving bird's eye split mode"
2161 conf.columns <- (
2162 match c.columns with
2163 | Cmulti (c, _) -> Cmulti (c, E.a)
2164 | Csingle _ -> Csingle E.a
2165 | Csplit (c, _) -> Csplit (c, E.a)
2167 if conf.verbose
2168 then
2169 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2170 (100.0*.conf.zoom)
2172 reshape state.winw state.winh;
2173 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2174 state.x <- leftx;
2177 let togglebirdseye () =
2178 match state.mode with
2179 | Birdseye vals -> leavebirdseye vals true
2180 | View -> enterbirdseye ()
2181 | Textentry _
2182 | LinkNav _ -> ()
2185 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2186 let pageno = max 0 (pageno - incr) in
2187 let rec loop = function
2188 | [] -> gotopage1 pageno 0
2189 | l :: _ when l.pageno = pageno ->
2190 if l.pagedispy >= 0 && l.pagey = 0
2191 then G.postRedisplay "upbirdseye"
2192 else gotopage1 pageno 0
2193 | _ :: rest -> loop rest
2195 loop state.layout;
2196 state.text <- E.s;
2197 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2200 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2201 let pageno = min (state.pagecount - 1) (pageno + incr) in
2202 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2203 let rec loop = function
2204 | [] ->
2205 let y, h = getpageyh pageno in
2206 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2207 gotoy (clamp dy)
2208 | l :: _ when l.pageno = pageno ->
2209 if l.pagevh != l.pageh
2210 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2211 else G.postRedisplay "downbirdseye"
2212 | _ :: rest -> loop rest
2214 loop state.layout;
2215 state.text <- E.s;
2218 let optentry mode _ key =
2219 let btos b = if b then "on" else "off" in
2220 if key >= 32 && key < 127
2221 then
2222 let c = Char.chr key in
2223 match c with
2224 | 's' ->
2225 let ondone s =
2226 try conf.scrollstep <- int_of_string s with exc ->
2227 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exc
2229 TEswitch ("scroll step: ", E.s, None, intentry, ondone, true)
2231 | 'A' ->
2232 let ondone s =
2234 conf.autoscrollstep <- boundastep state.winh (int_of_string s);
2235 if state.autoscroll <> None
2236 then state.autoscroll <- Some conf.autoscrollstep
2237 with exc ->
2238 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exc
2240 TEswitch ("auto scroll step: ", E.s, None, intentry, ondone, true)
2242 | 'C' ->
2243 let ondone s =
2245 let n, a, b = multicolumns_of_string s in
2246 setcolumns mode n a b;
2247 with exc ->
2248 state.text <- Printf.sprintf "bad columns `%s': %s" s @@ exntos exc
2250 TEswitch ("columns: ", E.s, None, textentry, ondone, true)
2252 | 'Z' ->
2253 let ondone s =
2255 let zoom = float (int_of_string s) /. 100.0 in
2256 setzoom zoom
2257 with exc ->
2258 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exc
2260 TEswitch ("zoom: ", E.s, None, intentry, ondone, true)
2262 | 't' ->
2263 let ondone s =
2265 conf.thumbw <- bound (int_of_string s) 2 4096;
2266 state.text <-
2267 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2268 begin match mode with
2269 | Birdseye beye ->
2270 leavebirdseye beye false;
2271 enterbirdseye ();
2272 | Textentry _
2273 | View
2274 | LinkNav _ -> ();
2276 with exc ->
2277 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exc
2279 TEswitch ("thumbnail width: ", E.s, None, intentry, ondone, true)
2281 | 'R' ->
2282 let ondone s =
2283 match try
2284 Some (int_of_string s)
2285 with exc ->
2286 state.text <-
2287 Printf.sprintf "bad integer `%s': %s" s @@ exntos exc;
2288 None
2289 with
2290 | Some angle -> reqlayout angle conf.fitmodel
2291 | None -> ()
2293 TEswitch ("rotation: ", E.s, None, intentry, ondone, true)
2295 | 'i' ->
2296 conf.icase <- not conf.icase;
2297 TEdone ("case insensitive search " ^ (btos conf.icase))
2299 | 'p' ->
2300 conf.preload <- not conf.preload;
2301 gotoy state.y;
2302 TEdone ("preload " ^ (btos conf.preload))
2304 | 'v' ->
2305 conf.verbose <- not conf.verbose;
2306 TEdone ("verbose " ^ (btos conf.verbose))
2308 | 'd' ->
2309 conf.debug <- not conf.debug;
2310 TEdone ("debug " ^ (btos conf.debug))
2312 | 'h' ->
2313 conf.maxhfit <- not conf.maxhfit;
2314 state.maxy <- calcheight ();
2315 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2317 | 'c' ->
2318 conf.crophack <- not conf.crophack;
2319 TEdone ("crophack " ^ btos conf.crophack)
2321 | 'a' ->
2322 let s =
2323 match conf.maxwait with
2324 | None ->
2325 conf.maxwait <- Some infinity;
2326 "always wait for page to complete"
2327 | Some _ ->
2328 conf.maxwait <- None;
2329 "show placeholder if page is not ready"
2331 TEdone s
2333 | 'f' ->
2334 conf.underinfo <- not conf.underinfo;
2335 TEdone ("underinfo " ^ btos conf.underinfo)
2337 | 'P' ->
2338 conf.savebmarks <- not conf.savebmarks;
2339 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2341 | 'S' ->
2342 let ondone s =
2344 let pageno, py =
2345 match state.layout with
2346 | [] -> 0, 0
2347 | l :: _ ->
2348 l.pageno, l.pagey
2350 conf.interpagespace <- int_of_string s;
2351 docolumns conf.columns;
2352 state.maxy <- calcheight ();
2353 let y = getpagey pageno in
2354 gotoy (y + py)
2355 with exc ->
2356 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exc
2358 TEswitch ("vertical margin: ", E.s, None, intentry, ondone, true)
2360 | 'l' ->
2361 let fm =
2362 match conf.fitmodel with
2363 | FitProportional -> FitWidth
2364 | FitWidth | FitPage -> FitProportional
2366 reqlayout conf.angle fm;
2367 TEdone ("proportional display " ^ btos (fm == FitProportional))
2369 | 'T' ->
2370 settrim (not conf.trimmargins) conf.trimfuzz;
2371 TEdone ("trim margins " ^ btos conf.trimmargins)
2373 | 'I' ->
2374 conf.invert <- not conf.invert;
2375 TEdone ("invert colors " ^ btos conf.invert)
2377 | 'x' ->
2378 let ondone s =
2379 cbput state.hists.sel s;
2380 conf.selcmd <- s;
2382 TEswitch ("selection command: ", E.s, Some (onhist state.hists.sel),
2383 textentry, ondone, true)
2385 | 'M' ->
2386 if conf.pax == None
2387 then conf.pax <- Some (ref (0.0, 0, 0))
2388 else conf.pax <- None;
2389 TEdone ("PAX " ^ btos (conf.pax != None))
2391 | _ ->
2392 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2393 TEstop
2394 else
2395 TEcont state.text
2398 class type lvsource = object
2399 method getitemcount : int
2400 method getitem : int -> (string * int)
2401 method hasaction : int -> bool
2402 method exit :
2403 uioh:uioh ->
2404 cancel:bool ->
2405 active:int ->
2406 first:int ->
2407 pan:int ->
2408 uioh option
2409 method getactive : int
2410 method getfirst : int
2411 method getpan : int
2412 method getminfo : (int * int) array
2413 end;;
2415 class virtual lvsourcebase = object
2416 val mutable m_active = 0
2417 val mutable m_first = 0
2418 val mutable m_pan = 0
2419 method getactive = m_active
2420 method getfirst = m_first
2421 method getpan = m_pan
2422 method getminfo : (int * int) array = E.a
2423 end;;
2425 let textentrykeyboard
2426 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2427 state.text <- E.s;
2428 let key =
2429 if key >= 0xffb0 && key <= 0xffb9
2430 then key - 0xffb0 + 48 else key
2432 let enttext te =
2433 state.mode <- Textentry (te, onleave);
2434 enttext ();
2435 G.postRedisplay "textentrykeyboard enttext";
2437 let histaction cmd =
2438 match opthist with
2439 | None -> ()
2440 | Some (action, _) ->
2441 state.mode <- Textentry (
2442 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
2444 G.postRedisplay "textentry histaction"
2446 match key with
2447 | @backspace ->
2448 if emptystr text && cancelonempty
2449 then (
2450 onleave Cancel;
2451 G.postRedisplay "textentrykeyboard after cancel";
2453 else
2454 let s = withoutlastutf8 text in
2455 enttext (c, s, opthist, onkey, ondone, cancelonempty)
2457 | @enter | @kpenter ->
2458 ondone text;
2459 onleave Confirm;
2460 G.postRedisplay "textentrykeyboard after confirm"
2462 | @up | @kpup -> histaction HCprev
2463 | @down | @kpdown -> histaction HCnext
2464 | @home | @kphome -> histaction HCfirst
2465 | @jend | @kpend -> histaction HClast
2467 | @escape ->
2468 if emptystr text
2469 then (
2470 begin match opthist with
2471 | None -> ()
2472 | Some (_, onhistcancel) -> onhistcancel ()
2473 end;
2474 onleave Cancel;
2475 state.text <- E.s;
2476 G.postRedisplay "textentrykeyboard after cancel2"
2478 else (
2479 enttext (c, E.s, opthist, onkey, ondone, cancelonempty)
2482 | @delete | @kpdelete -> ()
2484 | _ when key != 0
2485 && key land 0xff00 != 0xff00 (* keyboard *)
2486 && key land 0xfe00 != 0xfe00 (* xkb *)
2487 && key land 0xfd00 != 0xfd00 (* 3270 *)
2489 begin match onkey text key with
2490 | TEdone text ->
2491 ondone text;
2492 onleave Confirm;
2493 G.postRedisplay "textentrykeyboard after confirm2";
2495 | TEcont text ->
2496 enttext (c, text, opthist, onkey, ondone, cancelonempty);
2498 | TEstop ->
2499 onleave Cancel;
2500 G.postRedisplay "textentrykeyboard after cancel3"
2502 | TEswitch te ->
2503 state.mode <- Textentry (te, onleave);
2504 G.postRedisplay "textentrykeyboard switch";
2505 end;
2507 | _ ->
2508 vlog "unhandled key %s" (Wsi.keyname key)
2511 let firstof first active =
2512 if first > active || abs (first - active) > fstate.maxrows - 1
2513 then max 0 (active - (fstate.maxrows/2))
2514 else first
2517 let calcfirst first active =
2518 if active > first
2519 then
2520 let rows = active - first in
2521 if rows > fstate.maxrows then active - fstate.maxrows else first
2522 else active
2525 let scrollph y maxy =
2526 let sh = float (maxy + state.winh) /. float state.winh in
2527 let sh = float state.winh /. sh in
2528 let sh = max sh (float conf.scrollh) in
2530 let percent = float y /. float maxy in
2531 let position = (float state.winh -. sh) *. percent in
2533 let position =
2534 if position +. sh > float state.winh
2535 then float state.winh -. sh
2536 else position
2538 position, sh;
2541 let adderrmsg src msg =
2542 Buffer.add_string state.errmsgs msg;
2543 state.newerrmsgs <- true;
2544 G.postRedisplay src
2547 let adderrfmt src fmt =
2548 Format.ksprintf (fun s -> adderrmsg src s) fmt;
2551 let coe s = (s :> uioh);;
2553 class listview ~zebra ~helpmode ~(source:lvsource) ~trusted ~modehash =
2554 object (self)
2555 val m_pan = source#getpan
2556 val m_first = source#getfirst
2557 val m_active = source#getactive
2558 val m_qsearch = E.s
2559 val m_prev_uioh = state.uioh
2561 method private elemunder y =
2562 if y < 0
2563 then None
2564 else
2565 let n = y / (fstate.fontsize+1) in
2566 if m_first + n < source#getitemcount
2567 then (
2568 if source#hasaction (m_first + n)
2569 then Some (m_first + n)
2570 else None
2572 else None
2574 method display =
2575 Gl.enable `blend;
2576 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
2577 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2578 filledrect 0. 0. (float state.winw) (float state.winh);
2579 GlDraw.color (1., 1., 1.);
2580 Gl.enable `texture_2d;
2581 let fs = fstate.fontsize in
2582 let nfs = fs + 1 in
2583 let hw = (wadjsb () + xadjsb () + state.winw)/3 in
2584 let ww = fstate.wwidth in
2585 let tabw = 17.0*.ww in
2586 let itemcount = source#getitemcount in
2587 let minfo = source#getminfo in
2588 let x0, x1 =
2589 if conf.leftscroll
2590 then float (xadjsb ()), float (state.winw - 1)
2591 else 0.0, float (state.winw - conf.scrollbw - 1)
2593 let xadj = xadjsb () in
2594 let rec loop row =
2595 if (row - m_first) > fstate.maxrows
2596 then ()
2597 else (
2598 if row >= 0 && row < itemcount
2599 then (
2600 let (s, level) = source#getitem row in
2601 let y = (row - m_first) * nfs in
2602 let x =
2603 (if conf.leftscroll then float xadj else 5.0)
2604 +. (float (level + m_pan)) *. ww in
2605 if helpmode
2606 then GlDraw.color
2607 (let c = if row land 1 = 0 then 1.0 else 0.92 in (c,c,c));
2609 if row = m_active
2610 then (
2611 Gl.disable `texture_2d;
2612 let alpha = if source#hasaction row then 0.9 else 0.3 in
2613 GlDraw.color (1., 1., 1.) ~alpha;
2614 linerect (x0 +. 1.) (float (y + 1)) (x1) (float (y + fs + 3));
2615 Gl.enable `texture_2d;
2617 let c =
2618 if zebra && row land 1 = 1
2619 then 0.8
2620 else 1.0
2622 GlDraw.color (c,c,c);
2623 let drawtabularstring s =
2624 let drawstr x s =
2625 let x' = truncate (x0 +. x) in
2626 let pos = nindex s '\000' in
2627 if pos = -1
2628 then drawstring1 fs x' (y+nfs) s
2629 else
2630 let s1 = String.sub s 0 pos
2631 and s2 = String.sub s (pos+1) (String.length s - pos - 1) in
2632 let rec e s =
2633 if emptystr s
2634 then s
2635 else
2636 let s' = withoutlastutf8 s in
2637 let s = s' ^ "@Uellipsis" in
2638 let w = measurestr fs s in
2639 if float x' +. w +. ww < float (hw + x')
2640 then s
2641 else e s'
2643 let s1 =
2644 if float x' +. ww +. measurestr fs s1 > float (hw + x')
2645 then e s1
2646 else s1
2648 ignore (drawstring1 fs x' (y+nfs) s1);
2649 drawstring1 fs (hw + x') (y+nfs) s2
2651 if trusted
2652 then
2653 let x = if helpmode && row > 0 then x +. ww else x in
2654 let tabpos = nindex s '\t' in
2655 if tabpos > 0
2656 then
2657 let len = String.length s - tabpos - 1 in
2658 let s1 = String.sub s 0 tabpos
2659 and s2 = String.sub s (tabpos + 1) len in
2660 let nx = drawstr x s1 in
2661 let sw = nx -. x in
2662 let x = x +. (max tabw sw) in
2663 drawstr x s2
2664 else
2665 let len = String.length s - 2 in
2666 if len > 0 && s.[0] = '\xc2' && s.[1] = '\xb7'
2667 then
2668 let s = String.sub s 2 len in
2669 let x = if not helpmode then x +. ww else x in
2670 GlDraw.color (1.2, 1.2, 1.2);
2671 let vinc = drawstring1 (fs+fs/4)
2672 (truncate (x -. ww)) (y+nfs) s in
2673 GlDraw.color (1., 1., 1.);
2674 vinc +. (float fs *. 0.8)
2675 else
2676 drawstr x s
2677 else
2678 drawstr x s
2680 ignore (drawtabularstring s);
2681 loop (row+1)
2685 loop m_first;
2686 GlDraw.color (1.0, 1.0, 1.0) ~alpha:0.5;
2687 let xadj = float (xadjsb () + 5) in
2688 let rec loop row =
2689 if (row - m_first) > fstate.maxrows
2690 then ()
2691 else (
2692 if row >= 0 && row < itemcount
2693 then (
2694 let (s, level) = source#getitem row in
2695 let pos0 = nindex s '\000' in
2696 let y = (row - m_first) * nfs in
2697 let x = float (level + m_pan) *. ww in
2698 let (first, last) = minfo.(row) in
2699 let prefix =
2700 if pos0 > 0 && first > pos0
2701 then String.sub s (pos0+1) (first-pos0-1)
2702 else String.sub s 0 first
2704 let suffix = String.sub s first (last - first) in
2705 let w1 = measurestr fstate.fontsize prefix in
2706 let w2 = measurestr fstate.fontsize suffix in
2707 let x = x +. if conf.leftscroll then xadj else 5.0 in
2708 let x = if pos0 > 0 && first > pos0 then x +. float hw else x in
2709 let x0 = x +. w1
2710 and y0 = float (y+2) in
2711 let x1 = x0 +. w2
2712 and y1 = float (y+fs+3) in
2713 filledrect x0 y0 x1 y1;
2714 loop (row+1)
2718 Gl.disable `texture_2d;
2719 if Array.length minfo > 0 then loop m_first;
2720 Gl.disable `blend;
2722 method updownlevel incr =
2723 let len = source#getitemcount in
2724 let curlevel =
2725 if m_active >= 0 && m_active < len
2726 then snd (source#getitem m_active)
2727 else -1
2729 let rec flow i =
2730 if i = len then i-1 else if i = -1 then 0 else
2731 let _, l = source#getitem i in
2732 if l != curlevel then i else flow (i+incr)
2734 let active = flow m_active in
2735 let first = calcfirst m_first active in
2736 G.postRedisplay "outline updownlevel";
2737 {< m_active = active; m_first = first >}
2739 method private key1 key mask =
2740 let set1 active first qsearch =
2741 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2743 let search active pattern incr =
2744 let active = if active = -1 then m_first else active in
2745 let dosearch re =
2746 let rec loop n =
2747 if n >= 0 && n < source#getitemcount
2748 then (
2749 let s, _ = source#getitem n in
2750 match Str.search_forward re s 0 with
2751 | (exception Not_found) -> loop (n + incr)
2752 | _ -> Some n
2754 else None
2756 loop active
2758 let qpat = Str.quote pattern in
2759 match Str.regexp_case_fold qpat with
2760 | s -> dosearch s
2761 | exception exn ->
2762 adderrfmt "listview key1" "regexp_case_fold for `%S' failed: %S\n"
2763 qpat @@ Printexc.to_string exn;
2764 None
2766 let itemcount = source#getitemcount in
2767 let find start incr =
2768 let rec find i =
2769 if i = -1 || i = itemcount
2770 then -1
2771 else (
2772 if source#hasaction i
2773 then i
2774 else find (i + incr)
2777 find start
2779 let set active first =
2780 let first = bound first 0 (itemcount - fstate.maxrows) in
2781 state.text <- E.s;
2782 coe {< m_active = active; m_first = first; m_qsearch = E.s >}
2784 let navigate incr =
2785 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2786 let active, first =
2787 let incr1 = if incr > 0 then 1 else -1 in
2788 if isvisible m_first m_active
2789 then
2790 let next =
2791 let next = m_active + incr in
2792 let next =
2793 if next < 0 || next >= itemcount
2794 then -1
2795 else find next incr1
2797 if abs (m_active - next) > fstate.maxrows
2798 then -1
2799 else next
2801 if next = -1
2802 then
2803 let first = m_first + incr in
2804 let first = bound first 0 (itemcount - fstate.maxrows) in
2805 let next =
2806 let next = m_active + incr in
2807 let next = bound next 0 (itemcount - 1) in
2808 find next ~-incr1
2810 let active =
2811 if next = -1
2812 then m_active
2813 else (
2814 if isvisible first next
2815 then next
2816 else m_active
2819 active, first
2820 else
2821 let first = min next m_first in
2822 let first =
2823 if abs (next - first) > fstate.maxrows
2824 then first + incr
2825 else first
2827 next, first
2828 else
2829 let first = m_first + incr in
2830 let first = bound first 0 (itemcount - 1) in
2831 let active =
2832 let next = m_active + incr in
2833 let next = bound next 0 (itemcount - 1) in
2834 let next = find next incr1 in
2835 let active =
2836 if next = -1 || abs (m_active - first) > fstate.maxrows
2837 then (
2838 let active = if m_active = -1 then next else m_active in
2839 active
2841 else next
2843 if isvisible first active
2844 then active
2845 else -1
2847 active, first
2849 G.postRedisplay "listview navigate";
2850 set active first;
2852 match key with
2853 | (@r|@s) when Wsi.withctrl mask ->
2854 let incr = if key = @r then -1 else 1 in
2855 let active, first =
2856 match search (m_active + incr) m_qsearch incr with
2857 | None ->
2858 state.text <- m_qsearch ^ " [not found]";
2859 m_active, m_first
2860 | Some active ->
2861 state.text <- m_qsearch;
2862 active, firstof m_first active
2864 G.postRedisplay "listview ctrl-r/s";
2865 set1 active first m_qsearch;
2867 | @insert when Wsi.withctrl mask ->
2868 if m_active >= 0 && m_active < source#getitemcount
2869 then (
2870 let s, _ = source#getitem m_active in
2871 selstring s;
2873 coe self
2875 | @backspace ->
2876 if emptystr m_qsearch
2877 then coe self
2878 else (
2879 let qsearch = withoutlastutf8 m_qsearch in
2880 if emptystr qsearch
2881 then (
2882 state.text <- E.s;
2883 G.postRedisplay "listview empty qsearch";
2884 set1 m_active m_first E.s;
2886 else
2887 let active, first =
2888 match search m_active qsearch ~-1 with
2889 | None ->
2890 state.text <- qsearch ^ " [not found]";
2891 m_active, m_first
2892 | Some active ->
2893 state.text <- qsearch;
2894 active, firstof m_first active
2896 G.postRedisplay "listview backspace qsearch";
2897 set1 active first qsearch
2900 | key when (key != 0 && key land 0xff00 != 0xff00) ->
2901 let pattern = m_qsearch ^ toutf8 key in
2902 let active, first =
2903 match search m_active pattern 1 with
2904 | None ->
2905 state.text <- pattern ^ " [not found]";
2906 m_active, m_first
2907 | Some active ->
2908 state.text <- pattern;
2909 active, firstof m_first active
2911 G.postRedisplay "listview qsearch add";
2912 set1 active first pattern;
2914 | @escape ->
2915 state.text <- E.s;
2916 if emptystr m_qsearch
2917 then (
2918 G.postRedisplay "list view escape";
2919 let mx, my = state.mpos in
2920 updateunder mx my;
2921 begin
2922 match
2923 source#exit ~uioh:(coe self)
2924 ~cancel:true ~active:m_active ~first:m_first ~pan:m_pan
2925 with
2926 | None -> m_prev_uioh
2927 | Some uioh -> uioh
2930 else (
2931 G.postRedisplay "list view kill qsearch";
2932 coe {< m_qsearch = E.s >}
2935 | @enter | @kpenter ->
2936 state.text <- E.s;
2937 let self = {< m_qsearch = E.s >} in
2938 let opt =
2939 G.postRedisplay "listview enter";
2940 if m_active >= 0 && m_active < source#getitemcount
2941 then (
2942 source#exit ~uioh:(coe self) ~cancel:false
2943 ~active:m_active ~first:m_first ~pan:m_pan;
2945 else (
2946 source#exit ~uioh:(coe self) ~cancel:true
2947 ~active:m_active ~first:m_first ~pan:m_pan;
2950 begin match opt with
2951 | None -> m_prev_uioh
2952 | Some uioh -> uioh
2955 | @delete | @kpdelete ->
2956 coe self
2958 | @up | @kpup -> navigate ~-1
2959 | @down | @kpdown -> navigate 1
2960 | @prior | @kpprior -> navigate ~-(fstate.maxrows)
2961 | @next | @kpnext -> navigate fstate.maxrows
2963 | @right | @kpright ->
2964 state.text <- E.s;
2965 G.postRedisplay "listview right";
2966 coe {< m_pan = m_pan - 1 >}
2968 | @left | @kpleft ->
2969 state.text <- E.s;
2970 G.postRedisplay "listview left";
2971 coe {< m_pan = m_pan + 1 >}
2973 | @home | @kphome ->
2974 let active = find 0 1 in
2975 G.postRedisplay "listview home";
2976 set active 0;
2978 | @jend | @kpend ->
2979 let first = max 0 (itemcount - fstate.maxrows) in
2980 let active = find (itemcount - 1) ~-1 in
2981 G.postRedisplay "listview end";
2982 set active first;
2984 | key when (key = 0 || key land 0xff00 = 0xff00) ->
2985 coe self
2987 | _ ->
2988 dolog "listview unknown key %#x" key; coe self
2990 method key key mask =
2991 match state.mode with
2992 | Textentry te -> textentrykeyboard key mask te; coe self
2993 | Birdseye _
2994 | View
2995 | LinkNav _ -> self#key1 key mask
2997 method button button down x y _ =
2998 let opt =
2999 match button with
3000 | 1 when vscrollhit x ->
3001 G.postRedisplay "listview scroll";
3002 if down
3003 then
3004 let _, position, sh = self#scrollph in
3005 if y > truncate position && y < truncate (position +. sh)
3006 then (
3007 state.mstate <- Mscrolly;
3008 Some (coe self)
3010 else
3011 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3012 let first = truncate (s *. float source#getitemcount) in
3013 let first = min source#getitemcount first in
3014 Some (coe {< m_first = first; m_active = first >})
3015 else (
3016 state.mstate <- Mnone;
3017 Some (coe self);
3019 | 1 when down ->
3020 begin match self#elemunder y with
3021 | Some n ->
3022 G.postRedisplay "listview click";
3023 source#exit ~uioh:(coe {< m_active = n >})
3024 ~cancel:false ~active:n ~first:m_first ~pan:m_pan
3025 | _ ->
3026 Some (coe self)
3028 | n when (n == 4 || n == 5) && not down ->
3029 let len = source#getitemcount in
3030 let first =
3031 if n = 5 && m_first + fstate.maxrows >= len
3032 then
3033 m_first
3034 else
3035 let first = m_first + (if n == 4 then -1 else 1) in
3036 bound first 0 (len - 1)
3038 G.postRedisplay "listview wheel";
3039 Some (coe {< m_first = first >})
3040 | n when (n = 6 || n = 7) && not down ->
3041 let inc = if n = 7 then -1 else 1 in
3042 G.postRedisplay "listview hwheel";
3043 Some (coe {< m_pan = m_pan + inc >})
3044 | _ ->
3045 Some (coe self)
3047 match opt with
3048 | None -> m_prev_uioh
3049 | Some uioh -> uioh
3051 method multiclick _ x y = self#button 1 true x y
3053 method motion _ y =
3054 match state.mstate with
3055 | Mscrolly ->
3056 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3057 let first = truncate (s *. float source#getitemcount) in
3058 let first = min source#getitemcount first in
3059 G.postRedisplay "listview motion";
3060 coe {< m_first = first; m_active = first >}
3061 | Msel _
3062 | Mpan _
3063 | Mscrollx
3064 | Mzoom _
3065 | Mzoomrect _
3066 | Mnone -> coe self
3068 method pmotion x y =
3069 if x < state.winw - conf.scrollbw
3070 then
3071 let n =
3072 match self#elemunder y with
3073 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3074 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3076 let o =
3077 if n != m_active
3078 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3079 else self
3081 coe o
3082 else (
3083 Wsi.setcursor Wsi.CURSOR_INHERIT;
3084 coe self
3087 method infochanged _ = ()
3089 method scrollpw = (0, 0.0, 0.0)
3090 method scrollph =
3091 let nfs = fstate.fontsize + 1 in
3092 let y = m_first * nfs in
3093 let itemcount = source#getitemcount in
3094 let maxi = max 0 (itemcount - fstate.maxrows) in
3095 let maxy = maxi * nfs in
3096 let p, h = scrollph y maxy in
3097 conf.scrollbw, p, h
3099 method modehash = modehash
3100 method eformsgs = false
3101 method alwaysscrolly = true
3102 end;;
3104 class outlinelistview ~zebra ~source =
3105 let settext autonarrow s =
3106 if autonarrow
3107 then
3108 let ss = source#statestr in
3109 state.text <-
3110 if emptystr ss
3111 then "[" ^ s ^ "]"
3112 else "{" ^ ss ^ "} [" ^ s ^ "]"
3113 else state.text <- s
3115 object (self)
3116 inherit listview
3117 ~zebra
3118 ~helpmode:false
3119 ~source:(source :> lvsource)
3120 ~trusted:false
3121 ~modehash:(findkeyhash conf "outline")
3122 as super
3124 val m_autonarrow = false
3126 method! key key mask =
3127 let maxrows =
3128 if emptystr state.text
3129 then fstate.maxrows
3130 else fstate.maxrows - 2
3132 let calcfirst first active =
3133 if active > first
3134 then
3135 let rows = active - first in
3136 if rows > maxrows then active - maxrows else first
3137 else active
3139 let navigate incr =
3140 let active = m_active + incr in
3141 let active = bound active 0 (source#getitemcount - 1) in
3142 let first = calcfirst m_first active in
3143 G.postRedisplay "outline navigate";
3144 coe {< m_active = active; m_first = first >}
3146 let navscroll first =
3147 let active =
3148 let dist = m_active - first in
3149 if dist < 0
3150 then first
3151 else (
3152 if dist < maxrows
3153 then m_active
3154 else first + maxrows
3157 G.postRedisplay "outline navscroll";
3158 coe {< m_first = first; m_active = active >}
3160 let ctrl = Wsi.withctrl mask in
3161 match key with
3162 | @a when ctrl ->
3163 let text =
3164 if m_autonarrow
3165 then (source#denarrow; E.s)
3166 else (
3167 let pattern = source#renarrow in
3168 if nonemptystr m_qsearch
3169 then (source#narrow m_qsearch; m_qsearch)
3170 else pattern
3173 settext (not m_autonarrow) text;
3174 G.postRedisplay "toggle auto narrowing";
3175 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
3177 | @slash when emptystr m_qsearch && not m_autonarrow ->
3178 settext true E.s;
3179 G.postRedisplay "toggle auto narrowing";
3180 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
3182 | @n when ctrl ->
3183 source#narrow m_qsearch;
3184 if not m_autonarrow
3185 then source#add_narrow_pattern m_qsearch;
3186 G.postRedisplay "outline ctrl-n";
3187 coe {< m_first = 0; m_active = 0 >}
3189 | @S when ctrl ->
3190 let active = source#calcactive (getanchor ()) in
3191 let first = firstof m_first active in
3192 G.postRedisplay "outline ctrl-s";
3193 coe {< m_first = first; m_active = active >}
3195 | @u when ctrl ->
3196 G.postRedisplay "outline ctrl-u";
3197 if m_autonarrow && nonemptystr m_qsearch
3198 then (
3199 ignore (source#renarrow);
3200 settext m_autonarrow E.s;
3201 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3203 else (
3204 source#del_narrow_pattern;
3205 let pattern = source#renarrow in
3206 let text =
3207 if emptystr pattern then E.s else "Narrowed to " ^ pattern
3209 settext m_autonarrow text;
3210 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3213 | @l when ctrl ->
3214 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3215 G.postRedisplay "outline ctrl-l";
3216 coe {< m_first = first >}
3218 | @tab when m_autonarrow ->
3219 if nonemptystr m_qsearch
3220 then (
3221 G.postRedisplay "outline list view tab";
3222 source#add_narrow_pattern m_qsearch;
3223 settext true E.s;
3224 coe {< m_qsearch = E.s >}
3226 else coe self
3228 | @escape when m_autonarrow ->
3229 if nonemptystr m_qsearch
3230 then source#add_narrow_pattern m_qsearch;
3231 super#key key mask
3233 | @enter | @kpenter when m_autonarrow ->
3234 if nonemptystr m_qsearch
3235 then source#add_narrow_pattern m_qsearch;
3236 super#key key mask
3238 | key when m_autonarrow && (key != 0 && key land 0xff00 != 0xff00) ->
3239 let pattern = m_qsearch ^ toutf8 key in
3240 G.postRedisplay "outlinelistview autonarrow add";
3241 source#narrow pattern;
3242 settext true pattern;
3243 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3245 | key when m_autonarrow && key = @backspace ->
3246 if emptystr m_qsearch
3247 then coe self
3248 else
3249 let pattern = withoutlastutf8 m_qsearch in
3250 G.postRedisplay "outlinelistview autonarrow backspace";
3251 ignore (source#renarrow);
3252 source#narrow pattern;
3253 settext true pattern;
3254 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3256 | @up | @kpup when ctrl ->
3257 navscroll (max 0 (m_first - 1))
3259 | @down | @kpdown when ctrl ->
3260 navscroll (min (source#getitemcount - 1) (m_first + 1))
3262 | @up | @kpup -> navigate ~-1
3263 | @down | @kpdown -> navigate 1
3264 | @prior | @kpprior -> navigate ~-(fstate.maxrows)
3265 | @next | @kpnext -> navigate fstate.maxrows
3267 | @right | @kpright ->
3268 let o =
3269 if ctrl
3270 then (
3271 G.postRedisplay "outline ctrl right";
3272 {< m_pan = m_pan + 1 >}
3274 else self#updownlevel 1
3276 coe o
3278 | @left | @kpleft ->
3279 let o =
3280 if ctrl
3281 then (
3282 G.postRedisplay "outline ctrl left";
3283 {< m_pan = m_pan - 1 >}
3285 else self#updownlevel ~-1
3287 coe o
3289 | @home | @kphome ->
3290 G.postRedisplay "outline home";
3291 coe {< m_first = 0; m_active = 0 >}
3293 | @jend | @kpend ->
3294 let active = source#getitemcount - 1 in
3295 let first = max 0 (active - fstate.maxrows) in
3296 G.postRedisplay "outline end";
3297 coe {< m_active = active; m_first = first >}
3299 | _ -> super#key key mask
3300 end;;
3302 let genhistoutlines () =
3303 Config.gethist ()
3304 |> List.sort (fun (_, c1, _, _, _, _) (_, c2, _, _, _, _) ->
3305 compare c2.lastvisit c1.lastvisit)
3306 |> List.map
3307 (fun ((path, c, _, _, _, origin) as hist) ->
3308 let path = if nonemptystr origin then origin else path in
3309 let base = mbtoutf8 @@ Filename.basename path in
3310 (base ^ "\000" ^ c.title, 1, Ohistory hist)
3312 |> Array.of_list
3315 let gotohist (path, c, bookmarks, x, anchor, origin) =
3316 Config.save leavebirdseye;
3317 state.anchor <- anchor;
3318 state.bookmarks <- bookmarks;
3319 state.origin <- origin;
3320 state.x <- x;
3321 setconf conf c;
3322 let x0, y0, x1, y1 = conf.trimfuzz in
3323 wcmd "trimset %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1;
3324 reshape ~firsttime:true state.winw state.winh;
3325 opendoc path origin;
3326 setzoom c.zoom;
3329 let makecheckers () =
3330 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3331 following to say:
3332 converted by Issac Trotts. July 25, 2002 *)
3333 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3334 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3335 let id = GlTex.gen_texture () in
3336 GlTex.bind_texture ~target:`texture_2d id;
3337 GlPix.store (`unpack_alignment 1);
3338 GlTex.image2d image;
3339 List.iter (GlTex.parameter ~target:`texture_2d)
3340 [ `mag_filter `nearest; `min_filter `nearest ];
3344 let setcheckers enabled =
3345 match state.checkerstexid with
3346 | None ->
3347 if enabled then state.checkerstexid <- Some (makecheckers ())
3349 | Some checkerstexid ->
3350 if not enabled
3351 then (
3352 GlTex.delete_texture checkerstexid;
3353 state.checkerstexid <- None;
3357 let describe_location () =
3358 let fn = page_of_y state.y in
3359 let ln = page_of_y (state.y + state.winh - hscrollh () - 1) in
3360 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
3361 let percent =
3362 if maxy <= 0
3363 then 100.
3364 else (100. *. (float state.y /. float maxy))
3366 if fn = ln
3367 then
3368 Printf.sprintf "page %d of %d [%.2f%%]"
3369 (fn+1) state.pagecount percent
3370 else
3371 Printf.sprintf
3372 "pages %d-%d of %d [%.2f%%]"
3373 (fn+1) (ln+1) state.pagecount percent
3376 let setpresentationmode v =
3377 let n = page_of_y state.y in
3378 state.anchor <- (n, 0.0, 1.0);
3379 conf.presentation <- v;
3380 if conf.fitmodel = FitPage
3381 then reqlayout conf.angle conf.fitmodel;
3382 represent ();
3385 let setbgcol (r, g, b) =
3386 let col =
3387 let r = r *. 255.0 |> truncate
3388 and g = g *. 255.0 |> truncate
3389 and b = b *. 255.0 |> truncate in
3390 r lsl 16 |> (lor) (g lsl 8) |> (lor) b
3392 Wsi.setwinbgcol col;
3395 let enterinfomode =
3396 let btos b = if b then "@Uradical" else E.s in
3397 let showextended = ref false in
3398 let leave mode _ = state.mode <- mode in
3399 let src =
3400 (object
3401 val mutable m_l = []
3402 val mutable m_a = E.a
3403 val mutable m_prev_uioh = nouioh
3404 val mutable m_prev_mode = View
3406 inherit lvsourcebase
3408 method reset prev_mode prev_uioh =
3409 m_a <- Array.of_list (List.rev m_l);
3410 m_l <- [];
3411 m_prev_mode <- prev_mode;
3412 m_prev_uioh <- prev_uioh;
3414 method int name get set =
3415 m_l <-
3416 (name, `int get, 1, Action (
3417 fun u ->
3418 let ondone s =
3419 try set (int_of_string s)
3420 with exn ->
3421 state.text <- Printf.sprintf "bad integer `%s': %s"
3422 s @@ exntos exn
3424 state.text <- E.s;
3425 let te = name ^ ": ", E.s, None, intentry, ondone, true in
3426 state.mode <- Textentry (te, leave m_prev_mode);
3428 )) :: m_l
3430 method int_with_suffix name get set =
3431 m_l <-
3432 (name, `intws get, 1, Action (
3433 fun u ->
3434 let ondone s =
3435 try set (int_of_string_with_suffix s)
3436 with exn ->
3437 state.text <- Printf.sprintf "bad integer `%s': %s"
3438 s @@ exntos exn
3440 state.text <- E.s;
3441 let te =
3442 name ^ ": ", E.s, None, intentry_with_suffix, ondone, true
3444 state.mode <- Textentry (te, leave m_prev_mode);
3446 )) :: m_l
3448 method bool ?(offset=1) ?(btos=btos) name get set =
3449 m_l <-
3450 (name, `bool (btos, get), offset, Action (
3451 fun u ->
3452 let v = get () in
3453 set (not v);
3455 )) :: m_l
3457 method color name get set =
3458 m_l <-
3459 (name, `color get, 1, Action (
3460 fun u ->
3461 let invalid = (nan, nan, nan) in
3462 let ondone s =
3463 let c =
3464 try color_of_string s
3465 with exn ->
3466 state.text <- Printf.sprintf "bad color `%s': %s"
3467 s @@ exntos exn;
3468 invalid
3470 if c <> invalid
3471 then set c;
3473 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3474 state.text <- color_to_string (get ());
3475 state.mode <- Textentry (te, leave m_prev_mode);
3477 )) :: m_l
3479 method string name get set =
3480 m_l <-
3481 (name, `string get, 1, Action (
3482 fun u ->
3483 let ondone s = set s in
3484 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3485 state.mode <- Textentry (te, leave m_prev_mode);
3487 )) :: m_l
3489 method colorspace name get set =
3490 m_l <-
3491 (name, `string get, 1, Action (
3492 fun _ ->
3493 let source =
3494 (object
3495 inherit lvsourcebase
3497 initializer
3498 m_active <- CSTE.to_int conf.colorspace;
3499 m_first <- 0;
3501 method getitemcount =
3502 Array.length CSTE.names
3503 method getitem n =
3504 (CSTE.names.(n), 0)
3505 method exit ~uioh ~cancel ~active ~first ~pan =
3506 ignore (uioh, first, pan);
3507 if not cancel then set active;
3508 None
3509 method hasaction _ = true
3510 end)
3512 state.text <- E.s;
3513 let modehash = findkeyhash conf "info" in
3514 coe (new listview ~zebra:false ~helpmode:false
3515 ~source ~trusted:true ~modehash)
3516 )) :: m_l
3518 method paxmark name get set =
3519 m_l <-
3520 (name, `string get, 1, Action (
3521 fun _ ->
3522 let source =
3523 (object
3524 inherit lvsourcebase
3526 initializer
3527 m_active <- MTE.to_int conf.paxmark;
3528 m_first <- 0;
3530 method getitemcount = Array.length MTE.names
3531 method getitem n = (MTE.names.(n), 0)
3532 method exit ~uioh ~cancel ~active ~first ~pan =
3533 ignore (uioh, first, pan);
3534 if not cancel then set active;
3535 None
3536 method hasaction _ = true
3537 end)
3539 state.text <- E.s;
3540 let modehash = findkeyhash conf "info" in
3541 coe (new listview ~zebra:false ~helpmode:false
3542 ~source ~trusted:true ~modehash)
3543 )) :: m_l
3545 method fitmodel name get set =
3546 m_l <-
3547 (name, `string get, 1, Action (
3548 fun _ ->
3549 let source =
3550 (object
3551 inherit lvsourcebase
3553 initializer
3554 m_active <- FMTE.to_int conf.fitmodel;
3555 m_first <- 0;
3557 method getitemcount = Array.length FMTE.names
3558 method getitem n = (FMTE.names.(n), 0)
3559 method exit ~uioh ~cancel ~active ~first ~pan =
3560 ignore (uioh, first, pan);
3561 if not cancel then set active;
3562 None
3563 method hasaction _ = true
3564 end)
3566 state.text <- E.s;
3567 let modehash = findkeyhash conf "info" in
3568 coe (new listview ~zebra:false ~helpmode:false
3569 ~source ~trusted:true ~modehash)
3570 )) :: m_l
3572 method caption s offset =
3573 m_l <- (s, `empty, offset, Noaction) :: m_l
3575 method caption2 s f offset =
3576 m_l <- (s, `string f, offset, Noaction) :: m_l
3578 method getitemcount = Array.length m_a
3580 method getitem n =
3581 let tostr = function
3582 | `int f -> string_of_int (f ())
3583 | `intws f -> string_with_suffix_of_int (f ())
3584 | `string f -> f ()
3585 | `color f -> color_to_string (f ())
3586 | `bool (btos, f) -> btos (f ())
3587 | `empty -> E.s
3589 let name, t, offset, _ = m_a.(n) in
3590 ((let s = tostr t in
3591 if nonemptystr s
3592 then Printf.sprintf "%s\t%s" name s
3593 else name),
3594 offset)
3596 method exit ~uioh ~cancel ~active ~first ~pan =
3597 let uiohopt =
3598 if not cancel
3599 then (
3600 let uioh =
3601 match m_a.(active) with
3602 | _, _, _, Action f -> f uioh
3603 | _, _, _, Noaction -> uioh
3605 Some uioh
3607 else None
3609 m_active <- active;
3610 m_first <- first;
3611 m_pan <- pan;
3612 uiohopt
3614 method hasaction n =
3615 match m_a.(n) with
3616 | _, _, _, Action _ -> true
3617 | _, _, _, Noaction -> false
3619 initializer m_active <- 1
3620 end)
3622 let rec fillsrc prevmode prevuioh =
3623 let sep () = src#caption E.s 0 in
3624 let colorp name get set =
3625 src#string name
3626 (fun () -> color_to_string (get ()))
3627 (fun v ->
3629 let c = color_of_string v in
3630 set c
3631 with exn ->
3632 state.text <- Printf.sprintf "bad color `%s': %s" v @@ exntos exn
3635 let oldmode = state.mode in
3636 let birdseye = isbirdseye state.mode in
3638 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3640 src#bool "presentation mode"
3641 (fun () -> conf.presentation)
3642 (fun v -> setpresentationmode v);
3644 src#bool "ignore case in searches"
3645 (fun () -> conf.icase)
3646 (fun v -> conf.icase <- v);
3648 src#bool "preload"
3649 (fun () -> conf.preload)
3650 (fun v -> conf.preload <- v);
3652 src#bool "highlight links"
3653 (fun () -> conf.hlinks)
3654 (fun v -> conf.hlinks <- v);
3656 src#bool "under info"
3657 (fun () -> conf.underinfo)
3658 (fun v -> conf.underinfo <- v);
3660 src#bool "persistent bookmarks"
3661 (fun () -> conf.savebmarks)
3662 (fun v -> conf.savebmarks <- v);
3664 src#fitmodel "fit model"
3665 (fun () -> FMTE.to_string conf.fitmodel)
3666 (fun v -> reqlayout conf.angle (FMTE.of_int v));
3668 src#bool "trim margins"
3669 (fun () -> conf.trimmargins)
3670 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3672 src#bool "persistent location"
3673 (fun () -> conf.jumpback)
3674 (fun v -> conf.jumpback <- v);
3676 sep ();
3677 src#int "inter-page space"
3678 (fun () -> conf.interpagespace)
3679 (fun n ->
3680 conf.interpagespace <- n;
3681 docolumns conf.columns;
3682 let pageno, py =
3683 match state.layout with
3684 | [] -> 0, 0
3685 | l :: _ ->
3686 l.pageno, l.pagey
3688 state.maxy <- calcheight ();
3689 let y = getpagey pageno in
3690 gotoy (y + py)
3693 src#int "page bias"
3694 (fun () -> conf.pagebias)
3695 (fun v -> conf.pagebias <- v);
3697 src#int "scroll step"
3698 (fun () -> conf.scrollstep)
3699 (fun n -> conf.scrollstep <- n);
3701 src#int "horizontal scroll step"
3702 (fun () -> conf.hscrollstep)
3703 (fun v -> conf.hscrollstep <- v);
3705 src#int "auto scroll step"
3706 (fun () ->
3707 match state.autoscroll with
3708 | Some step -> step
3709 | _ -> conf.autoscrollstep)
3710 (fun n ->
3711 let n = boundastep state.winh n in
3712 if state.autoscroll <> None
3713 then state.autoscroll <- Some n;
3714 conf.autoscrollstep <- n);
3716 src#int "zoom"
3717 (fun () -> truncate (conf.zoom *. 100.))
3718 (fun v -> setzoom ((float v) /. 100.));
3720 src#int "rotation"
3721 (fun () -> conf.angle)
3722 (fun v -> reqlayout v conf.fitmodel);
3724 src#int "scroll bar width"
3725 (fun () -> conf.scrollbw)
3726 (fun v ->
3727 conf.scrollbw <- v;
3728 reshape state.winw state.winh;
3731 src#int "scroll handle height"
3732 (fun () -> conf.scrollh)
3733 (fun v -> conf.scrollh <- v;);
3735 src#int "thumbnail width"
3736 (fun () -> conf.thumbw)
3737 (fun v ->
3738 conf.thumbw <- min 4096 v;
3739 match oldmode with
3740 | Birdseye beye ->
3741 leavebirdseye beye false;
3742 enterbirdseye ()
3743 | Textentry _
3744 | View
3745 | LinkNav _ -> ()
3748 let mode = state.mode in
3749 src#string "columns"
3750 (fun () ->
3751 match conf.columns with
3752 | Csingle _ -> "1"
3753 | Cmulti (multi, _) -> multicolumns_to_string multi
3754 | Csplit (count, _) -> "-" ^ string_of_int count
3756 (fun v ->
3757 let n, a, b = multicolumns_of_string v in
3758 setcolumns mode n a b);
3760 sep ();
3761 src#caption "Pixmap cache" 0;
3762 src#int_with_suffix "size (advisory)"
3763 (fun () -> conf.memlimit)
3764 (fun v -> conf.memlimit <- v);
3766 src#caption2 "used"
3767 (fun () -> Printf.sprintf "%s bytes, %d tiles"
3768 (string_with_suffix_of_int state.memused)
3769 (Hashtbl.length state.tilemap)) 1;
3771 sep ();
3772 src#caption "Layout" 0;
3773 src#caption2 "Dimension"
3774 (fun () ->
3775 Printf.sprintf "%dx%d (virtual %dx%d)"
3776 state.winw state.winh
3777 state.w state.maxy)
3779 if conf.debug
3780 then
3781 src#caption2 "Position" (fun () ->
3782 Printf.sprintf "%dx%d" state.x state.y
3784 else
3785 src#caption2 "Position" (fun () -> describe_location ()) 1
3788 sep ();
3789 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3790 "Save these parameters as global defaults at exit"
3791 (fun () -> conf.bedefault)
3792 (fun v -> conf.bedefault <- v)
3795 sep ();
3796 let btos b = if b then "@Ulguillemet" else "@Urguillemet" in
3797 src#bool ~offset:0 ~btos "Extended parameters"
3798 (fun () -> !showextended)
3799 (fun v -> showextended := v; fillsrc prevmode prevuioh);
3800 if !showextended
3801 then (
3802 src#bool "checkers"
3803 (fun () -> conf.checkers)
3804 (fun v -> conf.checkers <- v; setcheckers v);
3805 src#bool "update cursor"
3806 (fun () -> conf.updatecurs)
3807 (fun v -> conf.updatecurs <- v);
3808 src#bool "scroll-bar on the left"
3809 (fun () -> conf.leftscroll)
3810 (fun v -> conf.leftscroll <- v);
3811 src#bool "verbose"
3812 (fun () -> conf.verbose)
3813 (fun v -> conf.verbose <- v);
3814 src#bool "invert colors"
3815 (fun () -> conf.invert)
3816 (fun v -> conf.invert <- v);
3817 src#bool "max fit"
3818 (fun () -> conf.maxhfit)
3819 (fun v -> conf.maxhfit <- v);
3820 src#bool "pax mode"
3821 (fun () -> conf.pax != None)
3822 (fun v ->
3823 if v
3824 then conf.pax <- Some (ref (now (), 0, 0))
3825 else conf.pax <- None);
3826 src#string "uri launcher"
3827 (fun () -> conf.urilauncher)
3828 (fun v -> conf.urilauncher <- v);
3829 src#string "path launcher"
3830 (fun () -> conf.pathlauncher)
3831 (fun v -> conf.pathlauncher <- v);
3832 src#string "tile size"
3833 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3834 (fun v ->
3836 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
3837 conf.tilew <- max 64 w;
3838 conf.tileh <- max 64 h;
3839 flushtiles ();
3840 with exn ->
3841 state.text <- Printf.sprintf "bad tile size `%s': %s"
3842 v @@ exntos exn
3844 src#int "texture count"
3845 (fun () -> conf.texcount)
3846 (fun v ->
3847 if realloctexts v
3848 then conf.texcount <- v
3849 else impmsg "failed to set texture count please retry later"
3851 src#int "slice height"
3852 (fun () -> conf.sliceheight)
3853 (fun v ->
3854 conf.sliceheight <- v;
3855 wcmd "sliceh %d" conf.sliceheight;
3857 src#int "anti-aliasing level"
3858 (fun () -> conf.aalevel)
3859 (fun v ->
3860 conf.aalevel <- bound v 0 8;
3861 state.anchor <- getanchor ();
3862 opendoc state.path state.password;
3864 src#string "page scroll scaling factor"
3865 (fun () -> string_of_float conf.pgscale)
3866 (fun v ->
3868 let s = float_of_string v in
3869 conf.pgscale <- s
3870 with exn ->
3871 state.text <- Printf.sprintf
3872 "bad page scroll scaling factor `%s': %s" v @@ exntos exn
3875 src#int "ui font size"
3876 (fun () -> fstate.fontsize)
3877 (fun v -> setfontsize (bound v 5 100));
3878 src#int "hint font size"
3879 (fun () -> conf.hfsize)
3880 (fun v -> conf.hfsize <- bound v 5 100);
3881 colorp "background color"
3882 (fun () -> conf.bgcolor)
3883 (fun v -> conf.bgcolor <- v; setbgcol v);
3884 src#bool "crop hack"
3885 (fun () -> conf.crophack)
3886 (fun v -> conf.crophack <- v);
3887 src#string "trim fuzz"
3888 (fun () -> irect_to_string conf.trimfuzz)
3889 (fun v ->
3891 conf.trimfuzz <- irect_of_string v;
3892 if conf.trimmargins
3893 then settrim true conf.trimfuzz;
3894 with exn ->
3895 state.text <- Printf.sprintf "bad irect `%s': %s" v @@ exntos exn
3897 src#string "throttle"
3898 (fun () ->
3899 match conf.maxwait with
3900 | None -> "show place holder if page is not ready"
3901 | Some time ->
3902 if time = infinity
3903 then "wait for page to fully render"
3904 else
3905 "wait " ^ string_of_float time
3906 ^ " seconds before showing placeholder"
3908 (fun v ->
3910 let f = float_of_string v in
3911 if f <= 0.0
3912 then conf.maxwait <- None
3913 else conf.maxwait <- Some f
3914 with exn ->
3915 state.text <- Printf.sprintf "bad time `%s': %s" v @@ exntos exn
3917 src#string "ghyll scroll"
3918 (fun () ->
3919 match conf.ghyllscroll with
3920 | None -> E.s
3921 | Some nab -> ghyllscroll_to_string nab
3923 (fun v ->
3924 try conf.ghyllscroll <- ghyllscroll_of_string v
3925 with
3926 | Failure msg ->
3927 state.text <- Printf.sprintf "bad ghyll `%s': %s" v msg
3928 | exn ->
3929 state.text <- Printf.sprintf "bad ghyll `%s': %s" v @@ exntos exn
3931 src#string "selection command"
3932 (fun () -> conf.selcmd)
3933 (fun v -> conf.selcmd <- v);
3934 src#string "synctex command"
3935 (fun () -> conf.stcmd)
3936 (fun v -> conf.stcmd <- v);
3937 src#string "pax command"
3938 (fun () -> conf.paxcmd)
3939 (fun v -> conf.paxcmd <- v);
3940 src#string "ask password command"
3941 (fun () -> conf.passcmd)
3942 (fun v -> conf.passcmd <- v);
3943 src#string "save path command"
3944 (fun () -> conf.savecmd)
3945 (fun v -> conf.savecmd <- v);
3946 src#colorspace "color space"
3947 (fun () -> CSTE.to_string conf.colorspace)
3948 (fun v ->
3949 conf.colorspace <- CSTE.of_int v;
3950 wcmd "cs %d" v;
3951 load state.layout;
3953 src#paxmark "pax mark method"
3954 (fun () -> MTE.to_string conf.paxmark)
3955 (fun v -> conf.paxmark <- MTE.of_int v);
3956 if bousable () && !opengl_has_pbo
3957 then
3958 src#bool "use PBO"
3959 (fun () -> conf.usepbo)
3960 (fun v -> conf.usepbo <- v);
3961 src#bool "mouse wheel scrolls pages"
3962 (fun () -> conf.wheelbypage)
3963 (fun v -> conf.wheelbypage <- v);
3964 src#bool "open remote links in a new instance"
3965 (fun () -> conf.riani)
3966 (fun v -> conf.riani <- v);
3967 src#bool "edit annotations inline"
3968 (fun () -> conf.annotinline)
3969 (fun v -> conf.annotinline <- v);
3970 src#bool "coarse positioning in presentation mode"
3971 (fun () -> conf.coarseprespos)
3972 (fun v -> conf.coarseprespos <- v);
3975 sep ();
3976 src#caption "Document" 0;
3977 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
3978 src#caption2 "Pages"
3979 (fun () -> string_of_int state.pagecount) 1;
3980 src#caption2 "Dimensions"
3981 (fun () -> string_of_int (List.length state.pdims)) 1;
3982 if conf.trimmargins
3983 then (
3984 sep ();
3985 src#caption "Trimmed margins" 0;
3986 src#caption2 "Dimensions"
3987 (fun () -> string_of_int (List.length state.pdims)) 1;
3990 sep ();
3991 src#caption "OpenGL" 0;
3992 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
3993 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
3995 sep ();
3996 src#caption "Location" 0;
3997 if nonemptystr state.origin
3998 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
3999 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
4001 src#reset prevmode prevuioh;
4003 fun () ->
4004 state.text <- E.s;
4005 resetmstate ();
4006 let prevmode = state.mode
4007 and prevuioh = state.uioh in
4008 fillsrc prevmode prevuioh;
4009 let source = (src :> lvsource) in
4010 let modehash = findkeyhash conf "info" in
4011 state.uioh <- coe (object (self)
4012 inherit listview ~zebra:false ~helpmode:false
4013 ~source ~trusted:true ~modehash as super
4014 val mutable m_prevmemused = 0
4015 method! infochanged = function
4016 | Memused ->
4017 if m_prevmemused != state.memused
4018 then (
4019 m_prevmemused <- state.memused;
4020 G.postRedisplay "memusedchanged";
4022 | Pdim -> G.postRedisplay "pdimchanged"
4023 | Docinfo -> fillsrc prevmode prevuioh
4025 method! key key mask =
4026 if not (Wsi.withctrl mask)
4027 then
4028 match key with
4029 | @left | @kpleft -> coe (self#updownlevel ~-1)
4030 | @right | @kpright -> coe (self#updownlevel 1)
4031 | _ -> super#key key mask
4032 else super#key key mask
4033 end);
4034 G.postRedisplay "info";
4037 let enterhelpmode =
4038 let source =
4039 (object
4040 inherit lvsourcebase
4041 method getitemcount = Array.length state.help
4042 method getitem n =
4043 let s, l, _ = state.help.(n) in
4044 (s, l)
4046 method exit ~uioh ~cancel ~active ~first ~pan =
4047 let optuioh =
4048 if not cancel
4049 then (
4050 match state.help.(active) with
4051 | _, _, Action f -> Some (f uioh)
4052 | _, _, Noaction -> Some uioh
4054 else None
4056 m_active <- active;
4057 m_first <- first;
4058 m_pan <- pan;
4059 optuioh
4061 method hasaction n =
4062 match state.help.(n) with
4063 | _, _, Action _ -> true
4064 | _, _, Noaction -> false
4066 initializer
4067 m_active <- -1
4068 end)
4069 in fun () ->
4070 let modehash = findkeyhash conf "help" in
4071 resetmstate ();
4072 state.uioh <- coe (new listview
4073 ~zebra:false ~helpmode:true
4074 ~source ~trusted:true ~modehash);
4075 G.postRedisplay "help";
4078 let entermsgsmode =
4079 let msgsource =
4080 (object
4081 inherit lvsourcebase
4082 val mutable m_items = E.a
4084 method getitemcount = 1 + Array.length m_items
4086 method getitem n =
4087 if n = 0
4088 then "[Clear]", 0
4089 else m_items.(n-1), 0
4091 method exit ~uioh ~cancel ~active ~first ~pan =
4092 ignore uioh;
4093 if not cancel
4094 then (
4095 if active = 0
4096 then Buffer.clear state.errmsgs;
4098 m_active <- active;
4099 m_first <- first;
4100 m_pan <- pan;
4101 None
4103 method hasaction n =
4104 n = 0
4106 method reset =
4107 state.newerrmsgs <- false;
4108 let l = Str.split newlinere (Buffer.contents state.errmsgs) in
4109 m_items <- Array.of_list l
4111 initializer
4112 m_active <- 0
4113 end)
4114 in fun () ->
4115 state.text <- E.s;
4116 resetmstate ();
4117 msgsource#reset;
4118 let source = (msgsource :> lvsource) in
4119 let modehash = findkeyhash conf "listview" in
4120 state.uioh <- coe (object
4121 inherit listview ~zebra:false ~helpmode:false
4122 ~source ~trusted:false ~modehash as super
4123 method! display =
4124 if state.newerrmsgs
4125 then msgsource#reset;
4126 super#display
4127 end);
4128 G.postRedisplay "msgs";
4131 let getusertext s =
4132 let editor = getenvwithdef "EDITOR" E.s in
4133 if emptystr editor
4134 then E.s
4135 else
4136 let tmppath = Filename.temp_file "llpp" "note" in
4137 if nonemptystr s
4138 then (
4139 let oc = open_out tmppath in
4140 output_string oc s;
4141 close_out oc;
4143 let execstr = editor ^ " " ^ tmppath in
4144 let s =
4145 match spawn execstr [] with
4146 | (exception exn) ->
4147 impmsg "spawn(%S) failed: %s" execstr @@ exntos exn;
4149 | pid ->
4150 match Unix.waitpid [] pid with
4151 | (exception exn) ->
4152 impmsg "waitpid(%d) failed: %s" pid @@ exntos exn;
4154 | (_pid, status) ->
4155 match status with
4156 | Unix.WEXITED 0 -> filecontents tmppath
4157 | Unix.WEXITED n ->
4158 impmsg "editor process(%s) exited abnormally: %d" execstr n;
4160 | Unix.WSIGNALED n ->
4161 impmsg "editor process(%s) was killed by signal %d" execstr n;
4163 | Unix.WSTOPPED n ->
4164 impmsg "editor(%s) process was stopped by signal %d" execstr n;
4167 match Unix.unlink tmppath with
4168 | (exception exn) ->
4169 impmsg "failed to ulink %S: %s" tmppath @@ exntos exn;
4171 | () -> s
4174 let enterannotmode opaque slinkindex =
4175 let msgsource =
4176 (object
4177 inherit lvsourcebase
4178 val mutable m_text = E.s
4179 val mutable m_items = E.a
4181 method getitemcount = Array.length m_items
4183 method getitem n =
4184 let label, _func = m_items.(n) in
4185 label, 0
4187 method exit ~uioh ~cancel ~active ~first ~pan =
4188 ignore (uioh, first, pan);
4189 if not cancel
4190 then (
4191 let _label, func = m_items.(active) in
4192 func ()
4194 None
4196 method hasaction n = nonemptystr @@ fst m_items.(n)
4198 method reset s =
4199 let rec split accu b i =
4200 let p = b+i in
4201 if p = String.length s
4202 then (String.sub s b (p-b), unit) :: accu
4203 else
4204 if (i > 70 && s.[p] = ' ') || s.[p] = '\r' || s.[p] = '\n'
4205 then
4206 let ss = if i = 0 then E.s else String.sub s b i in
4207 split ((ss, unit)::accu) (p+1) 0
4208 else
4209 split accu b (i+1)
4211 let cleanup () =
4212 wcmd "freepage %s" (~> opaque);
4213 let keys =
4214 Hashtbl.fold (fun key opaque' accu ->
4215 if opaque' = opaque'
4216 then key :: accu else accu) state.pagemap []
4218 List.iter (Hashtbl.remove state.pagemap) keys;
4219 flushtiles ();
4220 gotoy state.y
4222 let dele () =
4223 delannot opaque slinkindex;
4224 cleanup ();
4226 let edit inline () =
4227 let update s =
4228 if emptystr s
4229 then dele ()
4230 else (
4231 modannot opaque slinkindex s;
4232 cleanup ();
4235 if inline
4236 then
4237 let mode = state.mode in
4238 state.mode <-
4239 Textentry (
4240 ("annotation: ", m_text, None, textentry, update, true),
4241 fun _ -> state.mode <- mode);
4242 state.text <- E.s;
4243 enttext ();
4244 else
4245 let s = getusertext m_text in
4246 update s
4248 m_text <- s;
4249 m_items <-
4250 ( "[Copy]", fun () -> selstring m_text)
4251 :: ("[Delete]", dele)
4252 :: ("[Edit]", edit conf.annotinline)
4253 :: (E.s, unit)
4254 :: split [] 0 0 |> List.rev |> Array.of_list
4256 initializer
4257 m_active <- 0
4258 end)
4260 state.text <- E.s;
4261 let s = getannotcontents opaque slinkindex in
4262 resetmstate ();
4263 msgsource#reset s;
4264 let source = (msgsource :> lvsource) in
4265 let modehash = findkeyhash conf "listview" in
4266 state.uioh <- coe (object
4267 inherit listview ~zebra:false ~helpmode:false
4268 ~source ~trusted:false ~modehash
4269 end);
4270 G.postRedisplay "enterannotmode";
4273 let gotounder under =
4274 let getpath filename =
4275 let path =
4276 if nonemptystr filename
4277 then
4278 if Filename.is_relative filename
4279 then
4280 let dir = Filename.dirname state.path in
4281 let dir =
4282 if Filename.is_implicit dir
4283 then Filename.concat (Sys.getcwd ()) dir
4284 else dir
4286 Filename.concat dir filename
4287 else filename
4288 else E.s
4290 if Sys.file_exists path
4291 then path
4292 else E.s
4294 match under with
4295 | Ulinkgoto (pageno, top) ->
4296 if pageno >= 0
4297 then (
4298 addnav ();
4299 let top =
4300 if conf.presentation && conf.coarseprespos
4301 then 0
4302 else top
4304 gotopage1 pageno top;
4307 | Ulinkuri s -> gotouri s
4309 | Uremote (filename, pageno) ->
4310 let path = getpath filename in
4311 if nonemptystr path
4312 then (
4313 if conf.riani
4314 then
4315 let command = Printf.sprintf "%s -page %d %S" !selfexec pageno path in
4316 match spawn command [] with
4317 | _pid -> ()
4318 | (exception exn) ->
4319 dolog "failed to execute `%s': %s" command @@ exntos exn
4320 else
4321 let anchor = getanchor () in
4322 let ranchor = state.path, state.password, anchor, state.origin in
4323 state.origin <- E.s;
4324 state.anchor <- (pageno, 0.0, 0.0);
4325 state.ranchors <- ranchor :: state.ranchors;
4326 opendoc path E.s;
4328 else impmsg "cannot find %s" filename
4330 | Uremotedest (filename, destname) ->
4331 let path = getpath filename in
4332 if nonemptystr path
4333 then (
4334 if conf.riani
4335 then
4336 let command = !selfexec ^ " " ^ path ^ " -dest " ^ destname in
4337 match spawn command [] with
4338 | (exception exn) ->
4339 dolog "failed to execute `%s': %s" command @@ exntos exn
4340 | _pid -> ()
4341 else
4342 let anchor = getanchor () in
4343 let ranchor = state.path, state.password, anchor, state.origin in
4344 state.origin <- E.s;
4345 state.nameddest <- destname;
4346 state.ranchors <- ranchor :: state.ranchors;
4347 opendoc path E.s;
4349 else impmsg "cannot find %s" filename
4351 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4352 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
4355 let gotooutline (_, _, kind) =
4356 match kind with
4357 | Onone -> ()
4358 | Oanchor anchor ->
4359 let (pageno, y, _) = anchor in
4360 let y = getanchory
4361 (if conf.presentation then (pageno, y, 1.0) else anchor)
4363 addnav ();
4364 gotoghyll y
4365 | Ouri uri -> gotounder (Ulinkuri uri)
4366 | Olaunch cmd -> gotounder (Ulaunch cmd)
4367 | Oremote remote -> gotounder (Uremote remote)
4368 | Ohistory hist -> gotohist hist
4369 | Oremotedest remotedest -> gotounder (Uremotedest remotedest)
4372 class outlinesoucebase fetchoutlines = object (self)
4373 inherit lvsourcebase
4374 val mutable m_items = E.a
4375 val mutable m_minfo = E.a
4376 val mutable m_orig_items = E.a
4377 val mutable m_orig_minfo = E.a
4378 val mutable m_narrow_patterns = []
4379 val mutable m_gen = -1
4381 method getitemcount = Array.length m_items
4383 method getitem n =
4384 let s, n, _ = m_items.(n) in
4385 (s, n+0)
4387 method exit ~(uioh:uioh) ~cancel ~active ~(first:int) ~pan :
4388 uioh option =
4389 ignore (uioh, first);
4390 let items, minfo =
4391 if m_narrow_patterns = []
4392 then m_orig_items, m_orig_minfo
4393 else m_items, m_minfo
4395 m_pan <- pan;
4396 if not cancel
4397 then (
4398 m_items <- items;
4399 m_minfo <- minfo;
4400 gotooutline m_items.(active);
4402 else (
4403 m_items <- items;
4404 m_minfo <- minfo;
4406 None
4408 method hasaction (_:int) = true
4410 method greetmsg =
4411 if Array.length m_items != Array.length m_orig_items
4412 then
4413 let s =
4414 match m_narrow_patterns with
4415 | one :: [] -> one
4416 | many -> String.concat "@Uellipsis" (List.rev many)
4418 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
4419 else E.s
4421 method statestr =
4422 match m_narrow_patterns with
4423 | [] -> E.s
4424 | one :: [] -> one
4425 | head :: _ -> "@Uellipsis" ^ head
4427 method narrow pattern =
4428 match Str.regexp_case_fold pattern with
4429 | (exception _) -> ()
4430 | re ->
4431 let rec loop accu minfo n =
4432 if n = -1
4433 then (
4434 m_items <- Array.of_list accu;
4435 m_minfo <- Array.of_list minfo;
4437 else
4438 let (s, _, _) as o = m_items.(n) in
4439 let accu, minfo =
4440 match Str.search_forward re s 0 with
4441 | (exception Not_found) -> accu, minfo
4442 | first -> o :: accu, (first, Str.match_end ()) :: minfo
4444 loop accu minfo (n-1)
4446 loop [] [] (Array.length m_items - 1)
4448 method! getminfo = m_minfo
4450 method denarrow =
4451 m_orig_items <- fetchoutlines ();
4452 m_minfo <- m_orig_minfo;
4453 m_items <- m_orig_items
4455 method add_narrow_pattern pattern =
4456 m_narrow_patterns <- pattern :: m_narrow_patterns
4458 method del_narrow_pattern =
4459 match m_narrow_patterns with
4460 | _ :: rest -> m_narrow_patterns <- rest
4461 | [] -> ()
4463 method renarrow =
4464 self#denarrow;
4465 match m_narrow_patterns with
4466 | pattern :: [] -> self#narrow pattern; pattern
4467 | list ->
4468 List.fold_left (fun accu pattern ->
4469 self#narrow pattern;
4470 pattern ^ "@Uellipsis" ^ accu) E.s list
4472 method calcactive (_:anchor) = 0
4474 method reset anchor items =
4475 if state.gen != m_gen
4476 then (
4477 m_orig_items <- items;
4478 m_items <- items;
4479 m_narrow_patterns <- [];
4480 m_minfo <- E.a;
4481 m_orig_minfo <- E.a;
4482 m_gen <- state.gen;
4484 else (
4485 if items != m_orig_items
4486 then (
4487 m_orig_items <- items;
4488 if m_narrow_patterns == []
4489 then m_items <- items;
4492 let active = self#calcactive anchor in
4493 m_active <- active;
4494 m_first <- firstof m_first active
4498 let outlinesource fetchoutlines =
4499 (object
4500 inherit outlinesoucebase fetchoutlines
4501 method! calcactive anchor =
4502 let rely = getanchory anchor in
4503 let rec loop n best bestd =
4504 if n = Array.length m_items
4505 then best
4506 else
4507 let _, _, kind = m_items.(n) in
4508 match kind with
4509 | Oanchor anchor ->
4510 let orely = getanchory anchor in
4511 let d = abs (orely - rely) in
4512 if d < bestd
4513 then loop (n+1) n d
4514 else loop (n+1) best bestd
4515 | Onone | Oremote _ | Olaunch _
4516 | Oremotedest _ | Ouri _ | Ohistory _ ->
4517 loop (n+1) best bestd
4519 loop 0 ~-1 max_int
4520 end)
4523 let enteroutlinemode, enterbookmarkmode, enterhistmode =
4524 let mkselector sourcetype =
4525 let fetchoutlines () =
4526 match sourcetype with
4527 | `bookmarks -> Array.of_list state.bookmarks
4528 | `outlines -> state.outlines
4529 | `history -> genhistoutlines ()
4531 let source =
4532 if sourcetype = `history
4533 then new outlinesoucebase fetchoutlines
4534 else outlinesource fetchoutlines
4536 fun errmsg ->
4537 let outlines = fetchoutlines () in
4538 if Array.length outlines = 0
4539 then (
4540 showtext ' ' errmsg;
4542 else (
4543 resetmstate ();
4544 Wsi.setcursor Wsi.CURSOR_INHERIT;
4545 let anchor = getanchor () in
4546 source#reset anchor outlines;
4547 state.text <- source#greetmsg;
4548 state.uioh <-
4549 coe (new outlinelistview ~zebra:(sourcetype=`history) ~source);
4550 G.postRedisplay "enter selector";
4553 let mkenter sourcetype errmsg =
4554 let enter = mkselector sourcetype in
4555 fun () -> enter errmsg
4557 (**)mkenter `outlines "document has no outline"
4558 , mkenter `bookmarks "document has no bookmarks (yet)"
4559 , mkenter `history "history is empty"
4562 let quickbookmark ?title () =
4563 match state.layout with
4564 | [] -> ()
4565 | l :: _ ->
4566 let title =
4567 match title with
4568 | None ->
4569 let tm = Unix.localtime (now ()) in
4570 Printf.sprintf
4571 "Quick (page %d) (bookmarked at %02d/%02d/%d %02d:%02d)"
4572 (l.pageno+1)
4573 tm.Unix.tm_mday
4574 (tm.Unix.tm_mon+1)
4575 (tm.Unix.tm_year + 1900)
4576 tm.Unix.tm_hour
4577 tm.Unix.tm_min
4578 | Some title -> title
4580 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4583 let setautoscrollspeed step goingdown =
4584 let incr = max 1 ((abs step) / 2) in
4585 let incr = if goingdown then incr else -incr in
4586 let astep = boundastep state.winh (step + incr) in
4587 state.autoscroll <- Some astep;
4590 let canpan () =
4591 match conf.columns with
4592 | Csplit _ -> true
4593 | Csingle _ | Cmulti _ -> state.x != 0 || conf.zoom > 1.0
4596 let panbound x = bound x (-state.w) (wadjsb () + state.winw);;
4598 let existsinrow pageno (columns, coverA, coverB) p =
4599 let last = ((pageno - coverA) mod columns) + columns in
4600 let rec any = function
4601 | [] -> false
4602 | l :: rest ->
4603 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4604 then p l
4605 else (
4606 if not (p l)
4607 then (if l.pageno = last then false else any rest)
4608 else true
4611 any state.layout
4614 let nextpage () =
4615 match state.layout with
4616 | [] ->
4617 let pageno = page_of_y state.y in
4618 gotoghyll (getpagey (pageno+1))
4619 | l :: rest ->
4620 match conf.columns with
4621 | Csingle _ ->
4622 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4623 then
4624 let y = clamp (pgscale state.winh) in
4625 gotoghyll y
4626 else
4627 let pageno = min (l.pageno+1) (state.pagecount-1) in
4628 gotoghyll (getpagey pageno)
4629 | Cmulti ((c, _, _) as cl, _) ->
4630 if conf.presentation
4631 && (existsinrow l.pageno cl
4632 (fun l -> l.pageh > l.pagey + l.pagevh))
4633 then
4634 let y = clamp (pgscale state.winh) in
4635 gotoghyll y
4636 else
4637 let pageno = min (l.pageno+c) (state.pagecount-1) in
4638 gotoghyll (getpagey pageno)
4639 | Csplit (n, _) ->
4640 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4641 then
4642 let pagey, pageh = getpageyh l.pageno in
4643 let pagey = pagey + pageh * l.pagecol in
4644 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4645 gotoghyll (pagey + pageh + ips)
4648 let prevpage () =
4649 match state.layout with
4650 | [] ->
4651 let pageno = page_of_y state.y in
4652 gotoghyll (getpagey (pageno-1))
4653 | l :: _ ->
4654 match conf.columns with
4655 | Csingle _ ->
4656 if conf.presentation && l.pagey != 0
4657 then
4658 gotoghyll (clamp (pgscale ~-(state.winh)))
4659 else
4660 let pageno = max 0 (l.pageno-1) in
4661 gotoghyll (getpagey pageno)
4662 | Cmulti ((c, _, coverB) as cl, _) ->
4663 if conf.presentation &&
4664 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4665 then
4666 gotoghyll (clamp (pgscale ~-(state.winh)))
4667 else
4668 let decr =
4669 if l.pageno = state.pagecount - coverB
4670 then 1
4671 else c
4673 let pageno = max 0 (l.pageno-decr) in
4674 gotoghyll (getpagey pageno)
4675 | Csplit (n, _) ->
4676 let y =
4677 if l.pagecol = 0
4678 then
4679 if l.pageno = 0
4680 then l.pagey
4681 else
4682 let pageno = max 0 (l.pageno-1) in
4683 let pagey, pageh = getpageyh pageno in
4684 pagey + (n-1)*pageh
4685 else
4686 let pagey, pageh = getpageyh l.pageno in
4687 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4689 gotoghyll y
4692 let save () =
4693 if emptystr conf.savecmd
4694 then error "don't know where to save modified document"
4695 else
4696 let savecmd = Str.global_replace percentsre state.path conf.savecmd in
4697 let path =
4698 getcmdoutput
4699 (fun s -> error "failed to obtain path to the saved copy: %s" s)
4700 savecmd
4702 if nonemptystr path
4703 then
4704 let tmp = path ^ ".tmp" in
4705 savedoc tmp;
4706 Unix.rename tmp path;
4709 let viewkeyboard key mask =
4710 let enttext te =
4711 let mode = state.mode in
4712 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4713 state.text <- E.s;
4714 enttext ();
4715 G.postRedisplay "view:enttext"
4717 let ctrl = Wsi.withctrl mask in
4718 let key =
4719 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
4721 match key with
4722 | @Q -> exit 0
4724 | @W ->
4725 if hasunsavedchanges ()
4726 then save ()
4728 | @insert ->
4729 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4730 then (
4731 state.mode <- LinkNav (Ltgendir 0);
4732 gotoy state.y;
4734 else impmsg "keyboard link navigation does not work under rotation"
4736 | @escape | @q ->
4737 begin match state.mstate with
4738 | Mzoomrect _ ->
4739 resetmstate ();
4740 G.postRedisplay "kill rect";
4741 | Msel _
4742 | Mpan _
4743 | Mscrolly | Mscrollx
4744 | Mzoom _
4745 | Mnone ->
4746 begin match state.mode with
4747 | LinkNav _ ->
4748 state.mode <- View;
4749 G.postRedisplay "esc leave linknav"
4750 | Birdseye _
4751 | Textentry _
4752 | View ->
4753 match state.ranchors with
4754 | [] -> raise Quit
4755 | (path, password, anchor, origin) :: rest ->
4756 state.ranchors <- rest;
4757 state.anchor <- anchor;
4758 state.origin <- origin;
4759 state.nameddest <- E.s;
4760 opendoc path password
4761 end;
4762 end;
4764 | @backspace ->
4765 gotoghyll (getnav ~-1)
4767 | @o ->
4768 enteroutlinemode ()
4770 | @H ->
4771 enterhistmode ()
4773 | @u ->
4774 state.rects <- [];
4775 state.text <- E.s;
4776 Hashtbl.iter (fun _ opaque ->
4777 clearmark opaque;
4778 Hashtbl.clear state.prects) state.pagemap;
4779 G.postRedisplay "dehighlight";
4781 | @slash | @question ->
4782 let ondone isforw s =
4783 cbput state.hists.pat s;
4784 state.searchpattern <- s;
4785 search s isforw
4787 let s = String.make 1 (Char.chr key) in
4788 enttext (s, E.s, Some (onhist state.hists.pat),
4789 textentry, ondone (key = @slash), true)
4791 | @plus | @kpplus | @equals when ctrl ->
4792 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4793 setzoom (conf.zoom +. incr)
4795 | @plus | @kpplus ->
4796 let ondone s =
4797 let n =
4798 try int_of_string s with exc ->
4799 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exc;
4800 max_int
4802 if n != max_int
4803 then (
4804 conf.pagebias <- n;
4805 state.text <- "page bias is now " ^ string_of_int n;
4808 enttext ("page bias: ", E.s, None, intentry, ondone, true)
4810 | @minus | @kpminus when ctrl ->
4811 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4812 setzoom (max 0.01 (conf.zoom -. decr))
4814 | @minus | @kpminus ->
4815 let ondone msg = state.text <- msg in
4816 enttext (
4817 "option [acfhilpstvxACFPRSZTISM]: ", E.s, None,
4818 optentry state.mode, ondone, true
4821 | @0 when ctrl ->
4822 if conf.zoom = 1.0
4823 then (
4824 state.x <- 0;
4825 gotoy state.y
4827 else setzoom 1.0
4829 | (@1 | @2) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
4830 let cols =
4831 match conf.columns with
4832 | Csingle _ | Cmulti _ -> 1
4833 | Csplit (n, _) -> n
4835 let h = state.winh -
4836 conf.interpagespace lsl (if conf.presentation then 1 else 0)
4838 let zoom = zoomforh state.winw h (vscrollw ()) cols in
4839 if zoom > 0.0 && (key = @2 || zoom < 1.0)
4840 then setzoom zoom
4842 | @3 when ctrl ->
4843 let fm =
4844 match conf.fitmodel with
4845 | FitWidth -> FitProportional
4846 | FitProportional -> FitPage
4847 | FitPage -> FitWidth
4849 state.text <- "fit model: " ^ FMTE.to_string fm;
4850 reqlayout conf.angle fm
4852 | @4 when ctrl -> (* ctrl-4 *)
4853 let zoom = getmaxw () /. float state.winw in
4854 if zoom > 0.0 then setzoom zoom
4856 | @F9 ->
4857 togglebirdseye ()
4859 | @9 when ctrl ->
4860 togglebirdseye ()
4862 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4863 when not ctrl -> (* 0..9 *)
4864 let ondone s =
4865 let n =
4866 try int_of_string s with exc ->
4867 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exc;
4870 if n >= 0
4871 then (
4872 addnav ();
4873 cbput state.hists.pag (string_of_int n);
4874 gotopage1 (n + conf.pagebias - 1) 0;
4877 let pageentry text key =
4878 match Char.unsafe_chr key with
4879 | 'g' -> TEdone text
4880 | _ -> intentry text key
4882 let text = String.make 1 (Char.chr key) in
4883 enttext (":", text, Some (onhist state.hists.pag),
4884 pageentry, ondone, true)
4886 | @b ->
4887 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
4888 reshape state.winw state.winh;
4890 | @B ->
4891 state.bzoom <- not state.bzoom;
4892 state.rects <- [];
4893 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
4895 | @l ->
4896 conf.hlinks <- not conf.hlinks;
4897 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4898 G.postRedisplay "toggle highlightlinks";
4900 | @F ->
4901 if conf.angle mod 360 = 0
4902 then (
4903 state.glinks <- true;
4904 let mode = state.mode in
4905 state.mode <-
4906 Textentry (
4907 (":", E.s, None, linknentry, linknact gotounder, false),
4908 (fun _ ->
4909 state.glinks <- false;
4910 state.mode <- mode)
4912 state.text <- E.s;
4913 G.postRedisplay "view:linkent(F)"
4915 else impmsg "hint mode does not work under rotation"
4917 | @y ->
4918 state.glinks <- true;
4919 let mode = state.mode in
4920 state.mode <- Textentry (
4922 ":", E.s, None, linknentry, linknact (fun under ->
4923 selstring (undertext under);
4924 ), false
4926 fun _ ->
4927 state.glinks <- false;
4928 state.mode <- mode
4930 state.text <- E.s;
4931 G.postRedisplay "view:linkent"
4933 | @a ->
4934 begin match state.autoscroll with
4935 | Some step ->
4936 conf.autoscrollstep <- step;
4937 state.autoscroll <- None
4938 | None ->
4939 if conf.autoscrollstep = 0
4940 then state.autoscroll <- Some 1
4941 else state.autoscroll <- Some conf.autoscrollstep
4944 | @p when ctrl ->
4945 launchpath () (* XXX where do error messages go? *)
4947 | @P ->
4948 setpresentationmode (not conf.presentation);
4949 showtext ' ' ("presentation mode " ^
4950 if conf.presentation then "on" else "off");
4952 | @f ->
4953 if List.mem Wsi.Fullscreen state.winstate
4954 then Wsi.reshape conf.cwinw conf.cwinh
4955 else Wsi.fullscreen ()
4957 | @p | @N ->
4958 search state.searchpattern false
4960 | @n | @F3 ->
4961 search state.searchpattern true
4963 | @t ->
4964 begin match state.layout with
4965 | [] -> ()
4966 | l :: _ ->
4967 gotoghyll (getpagey l.pageno)
4970 | @space ->
4971 nextpage ()
4973 | @delete | @kpdelete -> (* delete *)
4974 prevpage ()
4976 | @equals ->
4977 showtext ' ' (describe_location ());
4979 | @w ->
4980 begin match state.layout with
4981 | [] -> ()
4982 | l :: _ ->
4983 Wsi.reshape (l.pagew + vscrollw ()) l.pageh;
4984 G.postRedisplay "w"
4987 | @apos ->
4988 enterbookmarkmode ()
4990 | @h | @F1 ->
4991 enterhelpmode ()
4993 | @i ->
4994 enterinfomode ()
4996 | @e when Buffer.length state.errmsgs > 0 ->
4997 entermsgsmode ()
4999 | @m ->
5000 let ondone s =
5001 match state.layout with
5002 | l :: _ ->
5003 if nonemptystr s
5004 then
5005 state.bookmarks <-
5006 (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
5007 | _ -> ()
5009 enttext ("bookmark: ", E.s, None, textentry, ondone, true)
5011 | @tilde ->
5012 quickbookmark ();
5013 showtext ' ' "Quick bookmark added";
5015 | @z ->
5016 begin match state.layout with
5017 | l :: _ ->
5018 let rect = getpdimrect l.pagedimno in
5019 let w, h =
5020 if conf.crophack
5021 then
5022 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5023 truncate (1.2 *. (rect.(3) -. rect.(0))))
5024 else
5025 (truncate (rect.(1) -. rect.(0)),
5026 truncate (rect.(3) -. rect.(0)))
5028 let w = truncate ((float w)*.conf.zoom)
5029 and h = truncate ((float h)*.conf.zoom) in
5030 if w != 0 && h != 0
5031 then (
5032 state.anchor <- getanchor ();
5033 Wsi.reshape (w + vscrollw ()) (h + conf.interpagespace)
5035 G.postRedisplay "z";
5037 | [] -> ()
5040 | @x -> state.roam ()
5042 | @Lt | @Gt ->
5043 reqlayout (conf.angle +
5044 (if key = @Gt then 30 else -30)) conf.fitmodel
5046 | @Lb | @Rb ->
5047 conf.colorscale <-
5048 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5050 G.postRedisplay "brightness";
5052 | @c when state.mode = View ->
5053 if Wsi.withalt mask
5054 then (
5055 if conf.zoom > 1.0
5056 then
5057 let m = (wadjsb () + state.winw - state.w) / 2 in
5058 state.x <- m;
5059 gotoy_and_clear_text state.y
5061 else
5062 let (c, a, b), z =
5063 match state.prevcolumns with
5064 | None -> (1, 0, 0), 1.0
5065 | Some (columns, z) ->
5066 let cab =
5067 match columns with
5068 | Csplit (c, _) -> -c, 0, 0
5069 | Cmulti ((c, a, b), _) -> c, a, b
5070 | Csingle _ -> 1, 0, 0
5072 cab, z
5074 setcolumns View c a b;
5075 setzoom z
5077 | @down | @up when ctrl && Wsi.withshift mask ->
5078 let zoom, x = state.prevzoom in
5079 setzoom zoom;
5080 state.x <- x;
5082 | @k | @up | @kpup ->
5083 begin match state.autoscroll with
5084 | None ->
5085 begin match state.mode with
5086 | Birdseye beye -> upbirdseye 1 beye
5087 | Textentry _
5088 | View
5089 | LinkNav _ ->
5090 if ctrl
5091 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5092 else (
5093 if not (Wsi.withshift mask) && conf.presentation
5094 then prevpage ()
5095 else gotoghyll1 true (clamp (-conf.scrollstep))
5098 | Some n ->
5099 setautoscrollspeed n false
5102 | @j | @down | @kpdown ->
5103 begin match state.autoscroll with
5104 | None ->
5105 begin match state.mode with
5106 | Birdseye beye -> downbirdseye 1 beye
5107 | Textentry _
5108 | View
5109 | LinkNav _ ->
5110 if ctrl
5111 then gotoy_and_clear_text (clamp (state.winh/2))
5112 else (
5113 if not (Wsi.withshift mask) && conf.presentation
5114 then nextpage ()
5115 else gotoghyll1 true (clamp (conf.scrollstep))
5118 | Some n ->
5119 setautoscrollspeed n true
5122 | @left | @right | @kpleft | @kpright when not (Wsi.withalt mask) ->
5123 if canpan ()
5124 then
5125 let dx =
5126 if ctrl
5127 then state.winw / 2
5128 else conf.hscrollstep
5130 let dx = if key = @left || key = @kpleft then dx else -dx in
5131 state.x <- panbound (state.x + dx);
5132 gotoy_and_clear_text state.y
5133 else (
5134 state.text <- E.s;
5135 G.postRedisplay "left/right"
5138 | @prior | @kpprior ->
5139 let y =
5140 if ctrl
5141 then
5142 match state.layout with
5143 | [] -> state.y
5144 | l :: _ -> state.y - l.pagey
5145 else
5146 clamp (pgscale (-state.winh))
5148 gotoghyll y
5150 | @next | @kpnext ->
5151 let y =
5152 if ctrl
5153 then
5154 match List.rev state.layout with
5155 | [] -> state.y
5156 | l :: _ -> getpagey l.pageno
5157 else
5158 clamp (pgscale state.winh)
5160 gotoghyll y
5162 | @g | @home | @kphome ->
5163 addnav ();
5164 gotoghyll 0
5165 | @G | @jend | @kpend ->
5166 addnav ();
5167 gotoghyll (clamp state.maxy)
5169 | @right | @kpright when Wsi.withalt mask ->
5170 gotoghyll (getnav 1)
5171 | @left | @kpleft when Wsi.withalt mask ->
5172 gotoghyll (getnav ~-1)
5174 | @r ->
5175 reload ()
5177 | @v when conf.debug ->
5178 state.rects <- [];
5179 List.iter (fun l ->
5180 match getopaque l.pageno with
5181 | None -> ()
5182 | Some opaque ->
5183 let x0, y0, x1, y1 = pagebbox opaque in
5184 let rect = (float x0, float y0,
5185 float x1, float y0,
5186 float x1, float y1,
5187 float x0, float y1) in
5188 debugrect rect;
5189 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
5190 state.rects <- (l.pageno, color, rect) :: state.rects;
5191 ) state.layout;
5192 G.postRedisplay "v";
5194 | @pipe ->
5195 let mode = state.mode in
5196 let cmd = ref E.s in
5197 let onleave = function
5198 | Cancel -> state.mode <- mode
5199 | Confirm ->
5200 List.iter (fun l ->
5201 match getopaque l.pageno with
5202 | Some opaque -> pipesel opaque !cmd
5203 | None -> ()) state.layout;
5204 state.mode <- mode
5206 let ondone s =
5207 cbput state.hists.sel s;
5208 cmd := s
5210 let te =
5211 "| ", !cmd, Some (onhist state.hists.sel), textentry, ondone, true
5213 G.postRedisplay "|";
5214 state.mode <- Textentry (te, onleave);
5216 | _ ->
5217 vlog "huh? %s" (Wsi.keyname key)
5220 let linknavkeyboard key mask linknav =
5221 let getpage pageno =
5222 let rec loop = function
5223 | [] -> None
5224 | l :: _ when l.pageno = pageno -> Some l
5225 | _ :: rest -> loop rest
5226 in loop state.layout
5228 let doexact (pageno, n) =
5229 match getopaque pageno, getpage pageno with
5230 | Some opaque, Some l ->
5231 if key = @enter || key = @kpenter
5232 then
5233 let under = getlink opaque n in
5234 G.postRedisplay "link gotounder";
5235 gotounder under;
5236 state.mode <- View;
5237 else
5238 let opt, dir =
5239 match key with
5240 | @home ->
5241 Some (findlink opaque LDfirst), -1
5243 | @jend ->
5244 Some (findlink opaque LDlast), 1
5246 | @left ->
5247 Some (findlink opaque (LDleft n)), -1
5249 | @right ->
5250 Some (findlink opaque (LDright n)), 1
5252 | @up ->
5253 Some (findlink opaque (LDup n)), -1
5255 | @down ->
5256 Some (findlink opaque (LDdown n)), 1
5258 | _ -> None, 0
5260 let pwl l dir =
5261 begin match findpwl l.pageno dir with
5262 | Pwlnotfound -> ()
5263 | Pwl pageno ->
5264 let notfound dir =
5265 state.mode <- LinkNav (Ltgendir dir);
5266 let y, h = getpageyh pageno in
5267 let y =
5268 if dir < 0
5269 then y + h - state.winh
5270 else y
5272 gotoy y
5274 begin match getopaque pageno, getpage pageno with
5275 | Some opaque, Some _ ->
5276 let link =
5277 let ld = if dir > 0 then LDfirst else LDlast in
5278 findlink opaque ld
5280 begin match link with
5281 | Lfound m ->
5282 showlinktype (getlink opaque m);
5283 state.mode <- LinkNav (Ltexact (pageno, m));
5284 G.postRedisplay "linknav jpage";
5285 | Lnotfound -> notfound dir
5286 end;
5287 | _ -> notfound dir
5288 end;
5289 end;
5291 begin match opt with
5292 | Some Lnotfound -> pwl l dir;
5293 | Some (Lfound m) ->
5294 if m = n
5295 then pwl l dir
5296 else (
5297 let _, y0, _, y1 = getlinkrect opaque m in
5298 if y0 < l.pagey
5299 then gotopage1 l.pageno y0
5300 else (
5301 let d = fstate.fontsize + 1 in
5302 if y1 - l.pagey > l.pagevh - d
5303 then gotopage1 l.pageno (y1 - state.winh - hscrollh () + d)
5304 else G.postRedisplay "linknav";
5306 showlinktype (getlink opaque m);
5307 state.mode <- LinkNav (Ltexact (l.pageno, m));
5310 | None -> viewkeyboard key mask
5311 end;
5312 | _ -> viewkeyboard key mask
5314 if key = @insert
5315 then (
5316 state.mode <- View;
5317 G.postRedisplay "leave linknav"
5319 else
5320 match linknav with
5321 | Ltgendir _ | Ltnotready _ -> viewkeyboard key mask
5322 | Ltexact exact -> doexact exact
5325 let keyboard key mask =
5326 if (key = @g && Wsi.withctrl mask) && not (istextentry state.mode)
5327 then wcmd "interrupt"
5328 else state.uioh <- state.uioh#key key mask
5331 let birdseyekeyboard key mask
5332 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5333 let incr =
5334 match conf.columns with
5335 | Csingle _ -> 1
5336 | Cmulti ((c, _, _), _) -> c
5337 | Csplit _ -> failwith "bird's eye split mode"
5339 let pgh layout = List.fold_left
5340 (fun m l -> max l.pageh m) state.winh layout in
5341 match key with
5342 | @l when Wsi.withctrl mask ->
5343 let y, h = getpageyh pageno in
5344 let top = (state.winh - h) / 2 in
5345 gotoy (max 0 (y - top))
5346 | @enter | @kpenter -> leavebirdseye beye false
5347 | @escape -> leavebirdseye beye true
5348 | @up -> upbirdseye incr beye
5349 | @down -> downbirdseye incr beye
5350 | @left -> upbirdseye 1 beye
5351 | @right -> downbirdseye 1 beye
5353 | @prior ->
5354 begin match state.layout with
5355 | l :: _ ->
5356 if l.pagey != 0
5357 then (
5358 state.mode <- Birdseye (
5359 oconf, leftx, l.pageno, hooverpageno, anchor
5361 gotopage1 l.pageno 0;
5363 else (
5364 let layout = layout state.x (state.y-state.winh)
5365 state.winw
5366 (pgh state.layout) in
5367 match layout with
5368 | [] -> gotoy (clamp (-state.winh))
5369 | l :: _ ->
5370 state.mode <- Birdseye (
5371 oconf, leftx, l.pageno, hooverpageno, anchor
5373 gotopage1 l.pageno 0
5376 | [] -> gotoy (clamp (-state.winh))
5377 end;
5379 | @next ->
5380 begin match List.rev state.layout with
5381 | l :: _ ->
5382 let layout = layout state.x
5383 (state.y + (pgh state.layout))
5384 state.winw state.winh in
5385 begin match layout with
5386 | [] ->
5387 let incr = l.pageh - l.pagevh in
5388 if incr = 0
5389 then (
5390 state.mode <-
5391 Birdseye (
5392 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5394 G.postRedisplay "birdseye pagedown";
5396 else gotoy (clamp (incr + conf.interpagespace*2));
5398 | l :: _ ->
5399 state.mode <-
5400 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5401 gotopage1 l.pageno 0;
5404 | [] -> gotoy (clamp state.winh)
5405 end;
5407 | @home ->
5408 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5409 gotopage1 0 0
5411 | @jend ->
5412 let pageno = state.pagecount - 1 in
5413 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5414 if not (pagevisible state.layout pageno)
5415 then
5416 let h =
5417 match List.rev state.pdims with
5418 | [] -> state.winh
5419 | (_, _, h, _) :: _ -> h
5421 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5422 else G.postRedisplay "birdseye end";
5424 | _ -> viewkeyboard key mask
5427 let drawpage l =
5428 let color =
5429 match state.mode with
5430 | Textentry _ -> scalecolor 0.4
5431 | LinkNav _
5432 | View -> scalecolor 1.0
5433 | Birdseye (_, _, pageno, hooverpageno, _) ->
5434 if l.pageno = hooverpageno
5435 then scalecolor 0.9
5436 else (
5437 if l.pageno = pageno
5438 then (
5439 let c = scalecolor 1.0 in
5440 GlDraw.color c;
5441 GlDraw.line_width 3.0;
5442 let dispx = xadjsb () + l.pagedispx in
5443 linerect
5444 (float (dispx-1)) (float (l.pagedispy-1))
5445 (float (dispx+l.pagevw+1))
5446 (float (l.pagedispy+l.pagevh+1))
5448 GlDraw.line_width 1.0;
5451 else scalecolor 0.8
5454 drawtiles l color;
5457 let postdrawpage l linkindexbase =
5458 match getopaque l.pageno with
5459 | Some opaque ->
5460 if tileready l l.pagex l.pagey
5461 then
5462 let x = l.pagedispx - l.pagex + xadjsb ()
5463 and y = l.pagedispy - l.pagey in
5464 let hlmask =
5465 match conf.columns with
5466 | Csingle _ | Cmulti _ ->
5467 (if conf.hlinks then 1 else 0)
5468 + (if state.glinks
5469 && not (isbirdseye state.mode) then 2 else 0)
5470 | Csplit _ -> 0
5472 let s =
5473 match state.mode with
5474 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5475 | Textentry _
5476 | Birdseye _
5477 | View
5478 | LinkNav _ -> E.s
5480 Hashtbl.find_all state.prects l.pageno |>
5481 List.iter (fun vals -> drawprect opaque x y vals);
5482 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5483 else 0
5484 | _ -> 0
5487 let scrollindicator () =
5488 let sbw, ph, sh = state.uioh#scrollph in
5489 let sbh, pw, sw = state.uioh#scrollpw in
5491 let x0,x1,hx0 =
5492 if conf.leftscroll
5493 then (0, sbw, sbw)
5494 else ((state.winw - sbw), state.winw, 0)
5497 GlDraw.color (0.64, 0.64, 0.64);
5498 filledrect (float x0) 0. (float x1) (float state.winh);
5499 filledrect
5500 (float hx0) (float (state.winh - sbh))
5501 (float (hx0 + wadjsb () + state.winw)) (float state.winh)
5503 GlDraw.color (0.0, 0.0, 0.0);
5505 filledrect (float x0) ph (float x1) (ph +. sh);
5506 let pw = pw +. float hx0 in
5507 filledrect pw (float (state.winh - sbh)) (pw +. sw) (float state.winh);
5510 let showsel () =
5511 match state.mstate with
5512 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5515 | Msel ((x0, y0), (x1, y1)) ->
5516 let identify opaque l px py = Some (opaque, l.pageno, px, py) in
5517 let o0,n0,px0,py0 = onppundermouse identify x0 y0 (~< E.s, -1, 0, 0) in
5518 let _o1,n1,px1,py1 = onppundermouse identify x1 y1 (~< E.s, -1, 0, 0) in
5519 if n0 != -1 && n0 = n1 then seltext o0 (px0, py0, px1, py1);
5522 let showrects = function [] -> () | rects ->
5523 Gl.enable `blend;
5524 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5525 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5526 List.iter
5527 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5528 List.iter (fun l ->
5529 if l.pageno = pageno
5530 then (
5531 let dx = float (l.pagedispx - l.pagex) in
5532 let dy = float (l.pagedispy - l.pagey) in
5533 let r, g, b, alpha = c in
5534 GlDraw.color (r, g, b) ~alpha;
5535 filledrect2 (x0+.dx) (y0+.dy)
5536 (x1+.dx) (y1+.dy)
5537 (x3+.dx) (y3+.dy)
5538 (x2+.dx) (y2+.dy);
5540 ) state.layout
5541 ) rects
5543 Gl.disable `blend;
5546 let display () =
5547 begin match conf.columns, state.layout with
5548 | Csingle _, _ :: _ ->
5549 GlDraw.color (scalecolor2 conf.bgcolor);
5550 let y =
5551 List.fold_left (fun y l ->
5552 let x0 = 0 in
5553 let y0 = y in
5554 let x1 = l.pagedispx + xadjsb () in
5555 let y1 = (l.pagedispy + l.pagevh) in
5556 filledrect (float x0) (float y0) (float x1) (float y1);
5557 let x0 = x1 + l.pagevw in
5558 let x1 = state.winw in
5559 filledrect1 (float x0) (float y0) (float x1) (float y1);
5560 if y != l.pagedispy
5561 then (
5562 let x0 = 0
5563 and x1 = state.winw in
5564 let y0 = y
5565 and y1 = l.pagedispy in
5566 filledrect1 (float x0) (float y0) (float x1) (float y1);
5568 l.pagedispy + l.pagevh) 0 state.layout
5570 let x0 = 0
5571 and x1 = state.winw in
5572 let y0 = y
5573 and y1 = state.winh in
5574 filledrect1 (float x0) (float y0) (float x1) (float y1)
5575 | (Cmulti _ | Csplit _), _ | Csingle _, [] ->
5576 GlClear.color (scalecolor2 conf.bgcolor);
5577 GlClear.clear [`color];
5578 end;
5579 List.iter drawpage state.layout;
5580 let rects =
5581 match state.mode with
5582 | LinkNav (Ltexact (pageno, linkno)) ->
5583 begin match getopaque pageno with
5584 | Some opaque ->
5585 let dx = xadjsb () in
5586 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5587 let x0 = x0 + dx and x1 = x1 + dx in
5588 let color = (0.0, 0.0, 0.5, 0.5) in
5589 (pageno, color, (
5590 float x0, float y0,
5591 float x1, float y0,
5592 float x1, float y1,
5593 float x0, float y1)
5594 ) :: state.rects
5595 | None -> state.rects
5597 | LinkNav (Ltgendir _) | LinkNav (Ltnotready _)
5598 | Birdseye _
5599 | Textentry _
5600 | View -> state.rects
5602 showrects rects;
5603 let rec postloop linkindexbase = function
5604 | l :: rest ->
5605 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5606 postloop linkindexbase rest
5607 | [] -> ()
5609 showsel ();
5610 postloop 0 state.layout;
5611 state.uioh#display;
5612 begin match state.mstate with
5613 | Mzoomrect ((x0, y0), (x1, y1)) ->
5614 Gl.enable `blend;
5615 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5616 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5617 filledrect (float x0) (float y0) (float x1) (float y1);
5618 Gl.disable `blend;
5619 | Msel _
5620 | Mpan _
5621 | Mscrolly | Mscrollx
5622 | Mzoom _
5623 | Mnone -> ()
5624 end;
5625 enttext ();
5626 scrollindicator ();
5627 Wsi.swapb ();
5630 let zoomrect x y x1 y1 =
5631 let x0 = min x x1
5632 and x1 = max x x1
5633 and y0 = min y y1 in
5634 gotoy (state.y + y0);
5635 state.anchor <- getanchor ();
5636 let zoom = (float state.w) /. float (x1 - x0) in
5637 let margin =
5638 let simple () =
5639 let adjw = wadjsb () + state.winw in
5640 if state.w < adjw
5641 then (adjw - state.w) / 2
5642 else 0
5644 match conf.fitmodel with
5645 | FitWidth | FitProportional -> simple ()
5646 | FitPage ->
5647 match conf.columns with
5648 | Csplit _ ->
5649 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
5650 | Cmulti _ | Csingle _ -> simple ()
5652 state.x <- (state.x + margin) - x0;
5653 setzoom zoom;
5654 resetmstate ();
5657 let annot inline x y =
5658 match unproject x y with
5659 | Some (opaque, n, ux, uy) ->
5660 let add text =
5661 addannot opaque ux uy text;
5662 wcmd "freepage %s" (~> opaque);
5663 Hashtbl.remove state.pagemap (n, state.gen);
5664 flushtiles ();
5665 gotoy state.y
5667 if inline
5668 then
5669 let ondone s = add s in
5670 let mode = state.mode in
5671 state.mode <- Textentry (
5672 ("annotation: ", E.s, None, textentry, ondone, true),
5673 fun _ -> state.mode <- mode);
5674 state.text <- E.s;
5675 enttext ();
5676 G.postRedisplay "annot"
5677 else
5678 add @@ getusertext E.s
5679 | _ -> ()
5682 let zoomblock x y =
5683 let g opaque l px py =
5684 match rectofblock opaque px py with
5685 | Some a ->
5686 let x0 = a.(0) -. 20. in
5687 let x1 = a.(1) +. 20. in
5688 let y0 = a.(2) -. 20. in
5689 let zoom = (float state.w) /. (x1 -. x0) in
5690 let pagey = getpagey l.pageno in
5691 gotoy_and_clear_text (pagey + truncate y0);
5692 state.anchor <- getanchor ();
5693 let margin = (state.w - l.pagew)/2 in
5694 state.x <- -truncate x0 - margin;
5695 setzoom zoom;
5696 None
5697 | None -> None
5699 match conf.columns with
5700 | Csplit _ ->
5701 impmsg "block zooming does not work properly in split columns mode"
5702 | Cmulti _ | Csingle _ -> onppundermouse g x y ()
5705 let scrollx x =
5706 let winw = wadjsb () + state.winw - 1 in
5707 let s = float x /. float winw in
5708 let destx = truncate (float (state.w + winw) *. s) in
5709 state.x <- winw - destx;
5710 gotoy_and_clear_text state.y;
5711 state.mstate <- Mscrollx;
5714 let scrolly y =
5715 let s = float y /. float state.winh in
5716 let desty = truncate (float (state.maxy - state.winh) *. s) in
5717 gotoy_and_clear_text desty;
5718 state.mstate <- Mscrolly;
5721 let viewmulticlick clicks x y mask =
5722 let g opaque l px py =
5723 let mark =
5724 match clicks with
5725 | 2 -> Mark_word
5726 | 3 -> Mark_line
5727 | 4 -> Mark_block
5728 | _ -> Mark_page
5730 if markunder opaque px py mark
5731 then (
5732 Some (fun () ->
5733 let dopipe cmd =
5734 match getopaque l.pageno with
5735 | None -> ()
5736 | Some opaque -> pipesel opaque cmd
5738 state.roam <- (fun () -> dopipe conf.paxcmd);
5739 if not (Wsi.withctrl mask) then dopipe conf.selcmd;
5742 else None
5744 G.postRedisplay "viewmulticlick";
5745 onppundermouse g x y (fun () -> impmsg "nothing to select") ();
5748 let canselect () =
5749 match conf.columns with
5750 | Csplit _ -> false
5751 | Csingle _ | Cmulti _ -> conf.angle mod 360 = 0
5754 let viewmouse button down x y mask =
5755 match button with
5756 | n when (n == 4 || n == 5) && not down ->
5757 if Wsi.withctrl mask
5758 then (
5759 match state.mstate with
5760 | Mzoom (oldn, i) ->
5761 if oldn = n
5762 then (
5763 if i = 2
5764 then
5765 let incr =
5766 match n with
5767 | 5 ->
5768 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5769 | _ ->
5770 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5772 let zoom = conf.zoom -. incr in
5773 setzoom zoom;
5774 state.mstate <- Mzoom (n, 0);
5775 else
5776 state.mstate <- Mzoom (n, i+1);
5778 else state.mstate <- Mzoom (n, 0)
5780 | Msel _
5781 | Mpan _
5782 | Mscrolly | Mscrollx
5783 | Mzoomrect _
5784 | Mnone -> state.mstate <- Mzoom (n, 0)
5786 else (
5787 match state.autoscroll with
5788 | Some step -> setautoscrollspeed step (n=4)
5789 | None ->
5790 if conf.wheelbypage || conf.presentation
5791 then (
5792 if n = 4
5793 then prevpage ()
5794 else nextpage ()
5796 else
5797 let incr =
5798 if n = 4
5799 then -conf.scrollstep
5800 else conf.scrollstep
5802 let incr = incr * 2 in
5803 let y = clamp incr in
5804 gotoy_and_clear_text y
5807 | n when (n = 6 || n = 7) && not down && canpan () ->
5808 state.x <-
5809 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
5810 gotoy_and_clear_text state.y
5812 | 1 when Wsi.withshift mask ->
5813 state.mstate <- Mnone;
5814 if not down
5815 then (
5816 match unproject x y with
5817 | None -> ()
5818 | Some (_, pageno, ux, uy) ->
5819 let cmd = Printf.sprintf
5820 "%s %s %d %d %d"
5821 conf.stcmd state.path pageno ux uy
5823 match spawn cmd [] with
5824 | (exception exn) ->
5825 impmsg "execution of synctex command(%S) failed: %S"
5826 conf.stcmd @@ exntos exn
5827 | _pid -> ()
5830 | 1 when Wsi.withctrl mask ->
5831 if down
5832 then (
5833 Wsi.setcursor Wsi.CURSOR_FLEUR;
5834 state.mstate <- Mpan (x, y)
5836 else
5837 state.mstate <- Mnone
5839 | 3 ->
5840 if down
5841 then (
5842 if Wsi.withshift mask
5843 then (
5844 annot conf.annotinline x y;
5845 G.postRedisplay "addannot"
5847 else
5848 let p = (x, y) in
5849 Wsi.setcursor Wsi.CURSOR_CYCLE;
5850 state.mstate <- Mzoomrect (p, p)
5852 else (
5853 match state.mstate with
5854 | Mzoomrect ((x0, y0), _) ->
5855 if abs (x-x0) > 10 && abs (y - y0) > 10
5856 then zoomrect x0 y0 x y
5857 else (
5858 resetmstate ();
5859 G.postRedisplay "kill accidental zoom rect";
5861 | Msel _
5862 | Mpan _
5863 | Mscrolly | Mscrollx
5864 | Mzoom _
5865 | Mnone ->
5866 resetmstate ()
5869 | 1 when vscrollhit x ->
5870 if down
5871 then
5872 let _, position, sh = state.uioh#scrollph in
5873 if y > truncate position && y < truncate (position +. sh)
5874 then state.mstate <- Mscrolly
5875 else scrolly y
5876 else
5877 state.mstate <- Mnone
5879 | 1 when y > state.winh - hscrollh () ->
5880 if down
5881 then
5882 let _, position, sw = state.uioh#scrollpw in
5883 if x > truncate position && x < truncate (position +. sw)
5884 then state.mstate <- Mscrollx
5885 else scrollx x
5886 else
5887 state.mstate <- Mnone
5889 | 1 when state.bzoom -> if not down then zoomblock x y
5891 | 1 ->
5892 let dest = if down then getunder x y else Unone in
5893 begin match dest with
5894 | Ulinkgoto _
5895 | Ulinkuri _
5896 | Uremote _ | Uremotedest _
5897 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5898 gotounder dest
5900 | Unone when down ->
5901 Wsi.setcursor Wsi.CURSOR_FLEUR;
5902 state.mstate <- Mpan (x, y);
5904 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
5906 | Unone | Utext _ ->
5907 if down
5908 then (
5909 if canselect ()
5910 then (
5911 state.mstate <- Msel ((x, y), (x, y));
5912 G.postRedisplay "mouse select";
5915 else (
5916 match state.mstate with
5917 | Mnone -> ()
5919 | Mzoom _ | Mscrollx | Mscrolly ->
5920 state.mstate <- Mnone
5922 | Mzoomrect ((x0, y0), _) ->
5923 zoomrect x0 y0 x y
5925 | Mpan _ ->
5926 Wsi.setcursor Wsi.CURSOR_INHERIT;
5927 state.mstate <- Mnone
5929 | Msel ((x0, y0), (x1, y1)) ->
5930 let rec loop = function
5931 | [] -> ()
5932 | l :: rest ->
5933 let inside =
5934 let a0 = l.pagedispy in
5935 let a1 = a0 + l.pagevh in
5936 let b0 = l.pagedispx in
5937 let b1 = b0 + l.pagevw in
5938 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5939 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5941 if inside
5942 then
5943 match getopaque l.pageno with
5944 | Some opaque ->
5945 let dosel cmd () =
5946 match Unix.pipe () with
5947 | (exception exn) ->
5948 impmsg "cannot create sel pipe: %s" @@
5949 exntos exn;
5950 | (r, w) ->
5951 let clo what fd =
5952 Ne.clo fd (fun msg ->
5953 dolog "%s close failed: %s" what msg)
5955 let pid =
5956 try spawn cmd [r, 0; w, -1]
5957 with exn ->
5958 dolog "cannot execute %S: %s"
5959 cmd @@ exntos exn;
5962 if pid > 0
5963 then (
5964 copysel w opaque;
5965 G.postRedisplay "copysel";
5967 else clo "Msel pipe/w" w;
5968 clo "Msel pipe/r" r;
5970 dosel conf.selcmd ();
5971 state.roam <- dosel conf.paxcmd;
5972 | None -> ()
5973 else loop rest
5975 loop state.layout;
5976 resetmstate ();
5980 | _ -> ()
5983 let birdseyemouse button down x y mask
5984 (conf, leftx, _, hooverpageno, anchor) =
5985 match button with
5986 | 1 when down ->
5987 let rec loop = function
5988 | [] -> ()
5989 | l :: rest ->
5990 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5991 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5992 then (
5993 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5995 else loop rest
5997 loop state.layout
5998 | 3 -> ()
5999 | _ -> viewmouse button down x y mask
6002 let uioh = object
6003 method display = ()
6005 method key key mask =
6006 begin match state.mode with
6007 | Textentry textentry -> textentrykeyboard key mask textentry
6008 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6009 | View -> viewkeyboard key mask
6010 | LinkNav linknav -> linknavkeyboard key mask linknav
6011 end;
6012 state.uioh
6014 method button button bstate x y mask =
6015 begin match state.mode with
6016 | LinkNav _
6017 | View -> viewmouse button bstate x y mask
6018 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6019 | Textentry _ -> ()
6020 end;
6021 state.uioh
6023 method multiclick clicks x y mask =
6024 begin match state.mode with
6025 | LinkNav _
6026 | View -> viewmulticlick clicks x y mask
6027 | Birdseye _
6028 | Textentry _ -> ()
6029 end;
6030 state.uioh
6032 method motion x y =
6033 begin match state.mode with
6034 | Textentry _ -> ()
6035 | View | Birdseye _ | LinkNav _ ->
6036 match state.mstate with
6037 | Mzoom _ | Mnone -> ()
6039 | Mpan (x0, y0) ->
6040 let dx = x - x0
6041 and dy = y0 - y in
6042 state.mstate <- Mpan (x, y);
6043 if canpan ()
6044 then state.x <- panbound (state.x + dx);
6045 let y = clamp dy in
6046 gotoy_and_clear_text y
6048 | Msel (a, _) ->
6049 state.mstate <- Msel (a, (x, y));
6050 G.postRedisplay "motion select";
6052 | Mscrolly ->
6053 let y = min state.winh (max 0 y) in
6054 scrolly y
6056 | Mscrollx ->
6057 let x = min state.winw (max 0 x) in
6058 scrollx x
6060 | Mzoomrect (p0, _) ->
6061 state.mstate <- Mzoomrect (p0, (x, y));
6062 G.postRedisplay "motion zoomrect";
6063 end;
6064 state.uioh
6066 method pmotion x y =
6067 begin match state.mode with
6068 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6069 let rec loop = function
6070 | [] ->
6071 if hooverpageno != -1
6072 then (
6073 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6074 G.postRedisplay "pmotion birdseye no hoover";
6076 | l :: rest ->
6077 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6078 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6079 then (
6080 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6081 G.postRedisplay "pmotion birdseye hoover";
6083 else loop rest
6085 loop state.layout
6087 | Textentry _ -> ()
6089 | LinkNav _
6090 | View ->
6091 match state.mstate with
6092 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ -> ()
6093 | Mnone ->
6094 updateunder x y;
6095 if canselect ()
6096 then
6097 match conf.pax with
6098 | None -> ()
6099 | Some r ->
6100 let past, _, _ = !r in
6101 let now = now () in
6102 let delta = now -. past in
6103 if delta > 0.01
6104 then paxunder x y
6105 else r := (now, x, y)
6106 end;
6107 state.uioh
6109 method infochanged _ = ()
6111 method scrollph =
6112 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6113 let p, h =
6114 if maxy = 0
6115 then 0.0, float state.winh
6116 else scrollph state.y maxy
6118 vscrollw (), p, h
6120 method scrollpw =
6121 let winw = wadjsb () + state.winw in
6122 let fwinw = float winw in
6123 let sw =
6124 let sw = fwinw /. float state.w in
6125 let sw = fwinw *. sw in
6126 max sw (float conf.scrollh)
6128 let position =
6129 let maxx = state.w + winw in
6130 let x = winw - state.x in
6131 let percent = float x /. float maxx in
6132 (fwinw -. sw) *. percent
6134 hscrollh (), position, sw
6136 method modehash =
6137 let modename =
6138 match state.mode with
6139 | LinkNav _ -> "links"
6140 | Textentry _ -> "textentry"
6141 | Birdseye _ -> "birdseye"
6142 | View -> "view"
6144 findkeyhash conf modename
6146 method eformsgs = true
6147 method alwaysscrolly = false
6148 end;;
6150 let addrect pageno r g b a x0 y0 x1 y1 =
6151 Hashtbl.add state.prects pageno [|r; g; b; a; x0; y0; x1; y1|];
6154 let ract cmds =
6155 let cl = splitatspace cmds in
6156 let scan s fmt f =
6157 try Scanf.sscanf s fmt f
6158 with exn ->
6159 adderrfmt "remote exec"
6160 "error processing '%S': %s\n" cmds @@ exntos exn
6162 let rectx s pageno (r, g, b, a) x0 y0 x1 y1 =
6163 vlog "%s page %d color (%f %f %f %f) x0,y0,x1,y1 = %f %f %f %f"
6164 s pageno r g b a x0 y0 x1 y1;
6165 onpagerect
6166 pageno
6167 (fun w h ->
6168 let _,w1,h1,_ = getpagedim pageno in
6169 let sw = float w1 /. float w
6170 and sh = float h1 /. float h in
6171 let x0s = x0 *. sw
6172 and x1s = x1 *. sw
6173 and y0s = y0 *. sh
6174 and y1s = y1 *. sh in
6175 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
6176 let color = (r, g, b, a) in
6177 if conf.verbose then debugrect rect;
6178 state.rects <- (pageno, color, rect) :: state.rects;
6179 G.postRedisplay s;
6182 match cl with
6183 | "reload", "" -> reload ()
6184 | "goto", args ->
6185 scan args "%u %f %f"
6186 (fun pageno x y ->
6187 let cmd, _ = state.geomcmds in
6188 if emptystr cmd
6189 then gotopagexy !wtmode pageno x y
6190 else
6191 let f prevf () =
6192 gotopagexy !wtmode pageno x y;
6193 prevf ()
6195 state.reprf <- f state.reprf
6197 | "goto1", args -> scan args "%u %f" gotopage
6198 | "gotor", args ->
6199 scan args "%S %u"
6200 (fun filename pageno -> gotounder (Uremote (filename, pageno)))
6201 | "gotord", args ->
6202 scan args "%S %S"
6203 (fun filename dest -> gotounder (Uremotedest (filename, dest)))
6204 | "rect", args ->
6205 scan args "%u %u %f %f %f %f"
6206 (fun pageno c x0 y0 x1 y1 ->
6207 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
6208 rectx "rect" pageno color x0 y0 x1 y1;
6210 | "prect", args ->
6211 scan args "%u %f %f %f %f %f %f %f %f"
6212 (fun pageno r g b alpha x0 y0 x1 y1 ->
6213 addrect pageno r g b alpha x0 y0 x1 y1;
6214 G.postRedisplay "prect"
6216 | "pgoto", args ->
6217 scan args "%u %f %f"
6218 (fun pageno x y ->
6219 let optopaque =
6220 match getopaque pageno with
6221 | Some opaque -> opaque
6222 | None -> ~< E.s
6224 pgoto optopaque pageno x y;
6225 let rec fixx = function
6226 | [] -> ()
6227 | l :: rest ->
6228 if l.pageno = pageno
6229 then (
6230 state.x <- state.x - l.pagedispx;
6231 gotoy state.y;
6233 else fixx rest
6235 let layout =
6236 let mult =
6237 match conf.columns with
6238 | Csingle _ | Csplit _ -> 1
6239 | Cmulti ((n, _, _), _) -> n
6241 layout 0 state.y (state.winw * mult) state.winh
6243 fixx layout
6245 | "activatewin", "" -> Wsi.activatewin ()
6246 | "quit", "" -> raise Quit
6247 | "keys", keys ->
6248 begin try
6249 let l = Config.keys_of_string keys in
6250 List.iter (fun (k, m) -> keyboard k m) l
6251 with exn ->
6252 adderrfmt "error processing keys" "`%S': %s\n" cmds @@ exntos exn
6254 | "clearrects", "" ->
6255 Hashtbl.clear state.prects;
6256 G.postRedisplay "clearrects"
6257 | _ ->
6258 adderrfmt "remote command"
6259 "error processing remote command: %S\n" cmds;
6262 let remote =
6263 let scratch = Bytes.create 80 in
6264 let buf = Buffer.create 80 in
6265 fun fd ->
6266 match tempfailureretry (Unix.read fd scratch 0) 80 with
6267 | (exception Unix.Unix_error (Unix.EAGAIN, _, _)) -> None
6268 | 0 ->
6269 Unix.close fd;
6270 if Buffer.length buf > 0
6271 then (
6272 let s = Buffer.contents buf in
6273 Buffer.clear buf;
6274 ract s;
6276 None
6277 | n ->
6278 let rec eat ppos =
6279 let nlpos =
6280 match Bytes.index_from scratch ppos '\n' with
6281 | pos -> if pos >= n then -1 else pos
6282 | (exception Not_found) -> -1
6284 if nlpos >= 0
6285 then (
6286 Buffer.add_subbytes buf scratch ppos (nlpos-ppos);
6287 let s = Buffer.contents buf in
6288 Buffer.clear buf;
6289 ract s;
6290 eat (nlpos+1);
6292 else (
6293 Buffer.add_subbytes buf scratch ppos (n-ppos);
6294 Some fd
6296 in eat 0
6299 let remoteopen path =
6300 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
6301 with exn ->
6302 adderrfmt "remoteopen" "error opening %S: %s" path @@ exntos exn;
6303 None
6306 let () =
6307 let gcconfig = ref E.s in
6308 let trimcachepath = ref E.s in
6309 let rcmdpath = ref E.s in
6310 let pageno = ref None in
6311 let rootwid = ref 0 in
6312 let openlast = ref false in
6313 let nofc = ref false in
6314 let doreap = ref false in
6315 selfexec := Sys.executable_name;
6316 Arg.parse
6317 (Arg.align
6318 [("-p", Arg.String (fun s -> state.password <- s),
6319 "<password> Set password");
6321 ("-f", Arg.String
6322 (fun s ->
6323 Config.fontpath := s;
6324 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
6326 "<path> Set path to the user interface font");
6328 ("-c", Arg.String
6329 (fun s ->
6330 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
6331 Config.confpath := s),
6332 "<path> Set path to the configuration file");
6334 ("-last", Arg.Set openlast, " Open last document");
6336 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
6337 "<page-number> Jump to page");
6339 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6340 "<path> Set path to the trim cache file");
6342 ("-dest", Arg.String (fun s -> state.nameddest <- s),
6343 "<named-destination> Set named destination");
6345 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
6346 ("-cxack", Arg.Set cxack, " Cut corners");
6348 ("-remote", Arg.String (fun s -> rcmdpath := s),
6349 "<path> Set path to the remote commands source");
6351 ("-origin", Arg.String (fun s -> state.origin <- s),
6352 "<original-path> Set original path");
6354 ("-gc", Arg.Set_string gcconfig,
6355 "<script-path> Collect garbage with the help of a script");
6357 ("-nofc", Arg.Set nofc, " Do not use fontconfig");
6359 ("-v", Arg.Unit (fun () ->
6360 Printf.printf
6361 "%s\nconfiguration path: %s\n"
6362 (version ())
6363 Config.defconfpath
6365 exit 0), " Print version and exit");
6367 ("-embed", Arg.Set_int rootwid,
6368 "<window-id> Embed into window")
6371 (fun s -> state.path <- s)
6372 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6374 if !wtmode
6375 then selfexec := !selfexec ^ " -wtmode";
6377 let histmode = emptystr state.path && not !openlast in
6379 if not (Config.load !openlast)
6380 then dolog "failed to load configuration";
6382 begin match !pageno with
6383 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
6384 | None -> ()
6385 end;
6387 if nonemptystr !gcconfig
6388 then (
6389 let (c, s) =
6390 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6391 | (exception exn) -> error "socketpair for gc failed: %s" @@ exntos exn
6392 | fds -> fds
6394 match spawn !gcconfig [(c, 0); (c, 1); (s, -1)] with
6395 | (exception exn) -> error "failed to execute gc script: %s" @@ exntos exn
6396 | _pid ->
6397 Ne.clo c @@ (fun s -> error "failed to close gc fd %s" s);
6398 Config.gc s;
6399 exit 0
6402 let wsfd, winw, winh = Wsi.init (object (self)
6403 val mutable m_clicks = 0
6404 val mutable m_click_x = 0
6405 val mutable m_click_y = 0
6406 val mutable m_lastclicktime = infinity
6408 method private cleanup =
6409 state.roam <- noroam;
6410 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap
6411 method expose = G.postRedisplay "expose"
6412 method visible v =
6413 let name =
6414 match v with
6415 | Wsi.Unobscured -> "unobscured"
6416 | Wsi.PartiallyObscured -> "partiallyobscured"
6417 | Wsi.FullyObscured -> "fullyobscured"
6419 vlog "visibility change %s" name
6420 method display = display ()
6421 method map mapped = vlog "mapped %b" mapped
6422 method reshape w h =
6423 self#cleanup;
6424 reshape w h
6425 method mouse b d x y m =
6426 if d && canselect ()
6427 then (
6428 (* http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx *)
6429 m_click_x <- x;
6430 m_click_y <- y;
6431 if b = 1
6432 then (
6433 let t = now () in
6434 if abs x - m_click_x > 10
6435 || abs y - m_click_y > 10
6436 || abs_float (t -. m_lastclicktime) > 0.3
6437 then m_clicks <- 0;
6438 m_clicks <- m_clicks + 1;
6439 m_lastclicktime <- t;
6440 if m_clicks = 1
6441 then (
6442 self#cleanup;
6443 G.postRedisplay "cleanup";
6444 state.uioh <- state.uioh#button b d x y m;
6446 else state.uioh <- state.uioh#multiclick m_clicks x y m
6448 else (
6449 self#cleanup;
6450 m_clicks <- 0;
6451 m_lastclicktime <- infinity;
6452 state.uioh <- state.uioh#button b d x y m
6455 else (
6456 state.uioh <- state.uioh#button b d x y m
6458 method motion x y =
6459 state.mpos <- (x, y);
6460 state.uioh <- state.uioh#motion x y
6461 method pmotion x y =
6462 state.mpos <- (x, y);
6463 state.uioh <- state.uioh#pmotion x y
6464 method key k m =
6465 let mascm = m land (
6466 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6467 ) in
6468 let keyboard k m =
6469 let x = state.x and y = state.y in
6470 keyboard k m;
6471 if x != state.x || y != state.y then self#cleanup
6473 match state.keystate with
6474 | KSnone ->
6475 let km = k, mascm in
6476 begin
6477 match
6478 let modehash = state.uioh#modehash in
6479 try Hashtbl.find modehash km
6480 with Not_found ->
6481 try Hashtbl.find (findkeyhash conf "global") km
6482 with Not_found -> KMinsrt (k, m)
6483 with
6484 | KMinsrt (k, m) -> keyboard k m
6485 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6486 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6488 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6489 List.iter (fun (k, m) -> keyboard k m) insrt;
6490 state.keystate <- KSnone
6491 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6492 state.keystate <- KSinto (keys, insrt)
6493 | KSinto _ -> state.keystate <- KSnone
6495 method enter x y =
6496 state.mpos <- (x, y);
6497 state.uioh <- state.uioh#pmotion x y
6498 method leave = state.mpos <- (-1, -1)
6499 method winstate wsl = state.winstate <- wsl
6500 method quit = raise Quit
6501 end) !rootwid conf.cwinw conf.cwinh platform in
6503 setbgcol conf.bgcolor;
6504 state.wsfd <- wsfd;
6506 if not (
6507 List.exists GlMisc.check_extension
6508 [ "GL_ARB_texture_rectangle"
6509 ; "GL_EXT_texture_recangle"
6510 ; "GL_NV_texture_rectangle" ]
6512 then (dolog "OpenGL does not suppport rectangular textures"; exit 1);
6514 if (
6515 let r = GlMisc.get_string `renderer in
6516 let p = "Mesa DRI Intel(" in
6517 let l = String.length p in
6518 String.length r > l && String.sub r 0 l = p
6520 then (
6521 defconf.sliceheight <- 1024;
6522 defconf.texcount <- 32;
6523 defconf.usepbo <- true;
6526 let cs, ss =
6527 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6528 | (exception exn) ->
6529 dolog "socketpair failed: %s" @@ exntos exn;
6530 exit 1
6531 | (r, w) ->
6532 cloexec r;
6533 cloexec w;
6534 r, w
6537 setcheckers conf.checkers;
6539 opengl_has_pbo := GlMisc.check_extension "GL_ARB_pixel_buffer_object";
6541 init cs (
6542 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
6543 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6544 !Config.fontpath, !trimcachepath,
6545 !opengl_has_pbo,
6546 not !nofc
6548 List.iter GlArray.enable [`texture_coord; `vertex];
6549 state.ss <- ss;
6550 reshape ~firsttime:true winw winh;
6551 state.uioh <- uioh;
6552 if histmode
6553 then (
6554 Wsi.settitle "llpp (history)";
6555 enterhistmode ();
6557 else (
6558 state.text <- "Opening " ^ (mbtoutf8 state.path);
6559 opendoc state.path state.password;
6561 display ();
6562 Wsi.mapwin ();
6563 Wsi.setcursor Wsi.CURSOR_INHERIT;
6564 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
6566 let rec reap () =
6567 match Unix.waitpid [Unix.WNOHANG] ~-1 with
6568 | (exception (Unix.Unix_error (Unix.ECHILD, _, _))) -> ()
6569 | (exception exn) -> dolog "Unix.waitpid: %s" @@ exntos exn
6570 | 0, _ -> ()
6571 | _pid, _status -> reap ()
6573 Sys.set_signal Sys.sigchld (Sys.Signal_handle (fun _ -> doreap := true));
6575 let optrfd =
6576 ref (
6577 if nonemptystr !rcmdpath
6578 then remoteopen !rcmdpath
6579 else None
6583 let rec loop deadline =
6584 if !doreap
6585 then (
6586 doreap := false;
6587 reap ()
6589 let r = [state.ss; state.wsfd] in
6590 let r =
6591 match !optrfd with
6592 | None -> r
6593 | Some fd -> fd :: r
6595 if state.redisplay
6596 then (
6597 state.redisplay <- false;
6598 display ();
6600 let timeout =
6601 let now = now () in
6602 if deadline > now
6603 then (
6604 if deadline = infinity
6605 then ~-.1.0
6606 else max 0.0 (deadline -. now)
6608 else 0.0
6610 let r, _, _ =
6611 try Unix.select r [] [] timeout
6612 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6614 begin match r with
6615 | [] ->
6616 state.ghyll None;
6617 let newdeadline =
6618 if state.ghyll == noghyll
6619 then
6620 match state.autoscroll with
6621 | Some step when step != 0 ->
6622 let y = state.y + step in
6623 let fy = if conf.maxhfit then state.winh else 0 in
6624 let y =
6625 if y < 0
6626 then state.maxy - fy
6627 else if y >= state.maxy - fy then 0 else y
6629 if state.mode = View
6630 then gotoy_and_clear_text y
6631 else gotoy y;
6632 deadline +. 0.01
6633 | _ -> infinity
6634 else deadline +. 0.01
6636 loop newdeadline
6638 | l ->
6639 let rec checkfds = function
6640 | [] -> ()
6641 | fd :: rest when fd = state.ss ->
6642 let cmd = rcmd state.ss in
6643 act cmd;
6644 checkfds rest
6646 | fd :: rest when fd = state.wsfd ->
6647 Wsi.readresp fd;
6648 checkfds rest
6650 | fd :: rest when Some fd = !optrfd ->
6651 begin match remote fd with
6652 | None -> optrfd := remoteopen !rcmdpath;
6653 | opt -> optrfd := opt
6654 end;
6655 checkfds rest
6657 | _ :: rest ->
6658 dolog "select returned unknown descriptor";
6659 checkfds rest
6661 checkfds l;
6662 let newdeadline =
6663 let deadline1 =
6664 if deadline = infinity
6665 then now () +. 0.01
6666 else deadline
6668 match state.autoscroll with
6669 | Some step when step != 0 -> deadline1
6670 | _ -> if state.ghyll == noghyll then infinity else deadline1
6672 loop newdeadline
6673 end;
6676 loop infinity;
6677 with Quit ->
6678 Config.save leavebirdseye;
6679 if hasunsavedchanges ()
6680 then save ();