Unbreak unoconv
[llpp.git] / main.ml
blobb0c1f718333ca386b4a4a9913a15f70755ef3309
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";;
52 let selfexec = ref E.s;;
53 let opengl_has_pbo = ref false;;
55 let drawstring size x y s =
56 Gl.enable `blend;
57 Gl.enable `texture_2d;
58 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
59 ignore (drawstr size x y s);
60 Gl.disable `blend;
61 Gl.disable `texture_2d;
64 let drawstring1 size x y s =
65 drawstr size x y s;
68 let drawstring2 size x y fmt =
69 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
72 let _debugl l =
73 dolog "l %d dim=%d {" l.pageno l.pagedimno;
74 dolog " WxH %dx%d" l.pagew l.pageh;
75 dolog " vWxH %dx%d" l.pagevw l.pagevh;
76 dolog " pagex,y %d,%d" l.pagex l.pagey;
77 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
78 dolog " column %d" l.pagecol;
79 dolog "}";
82 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
83 dolog "rect {";
84 dolog " x0,y0=(% f, % f)" x0 y0;
85 dolog " x1,y1=(% f, % f)" x1 y1;
86 dolog " x2,y2=(% f, % f)" x2 y2;
87 dolog " x3,y3=(% f, % f)" x3 y3;
88 dolog "}";
91 let isbirdseye = function
92 | Birdseye _ -> true
93 | Textentry _
94 | View
95 | LinkNav _ -> false
98 let istextentry = function
99 | Textentry _ -> true
100 | Birdseye _
101 | View
102 | LinkNav _ -> false
105 let wtmode = ref false;;
106 let cxack = ref false;;
108 let pgscale h = truncate (float h *. conf.pgscale);;
110 let hscrollh () =
111 if not state.uioh#alwaysscrolly && (conf.scrollb land scrollbhv = 0)
112 || (state.x = 0 && state.w <= state.winw - conf.scrollbw)
113 then 0
114 else conf.scrollbw
117 let vscrollw () =
118 if not state.uioh#alwaysscrolly && (conf.scrollb land scrollbvv = 0)
119 then 0
120 else conf.scrollbw
123 let vscrollhit x =
124 if conf.leftscroll
125 then x < vscrollw ()
126 else x > state.winw - vscrollw ()
129 let wadjsb () = -vscrollw ();;
130 let xadjsb () = if conf.leftscroll then vscrollw () else 0;;
132 let setfontsize n =
133 fstate.fontsize <- n;
134 fstate.wwidth <- measurestr fstate.fontsize "w";
135 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
138 let vlog fmt =
139 if conf.verbose
140 then dolog fmt
141 else Printf.kprintf ignore fmt
144 let launchpath () =
145 if emptystr conf.pathlauncher
146 then dolog "%s" state.path
147 else (
148 let command = Str.global_replace percentsre state.path conf.pathlauncher in
149 match spawn command [] with
150 | _pid -> ()
151 | (exception exn) ->
152 dolog "failed to execute `%s': %s" command @@ exntos exn
156 module G =
157 struct
158 let postRedisplay who =
159 vlog "redisplay for [%S]" who;
160 state.redisplay <- true;
162 end;;
164 let getopaque pageno =
165 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
166 with Not_found -> None
169 let pagetranslatepoint l x y =
170 let dy = y - l.pagedispy in
171 let y = dy + l.pagey in
172 let dx = x - l.pagedispx in
173 let x = dx + l.pagex in
174 (x, y);
177 let onppundermouse g x y d =
178 let rec f = function
179 | l :: rest ->
180 begin match getopaque l.pageno with
181 | Some opaque ->
182 let x0 = l.pagedispx in
183 let x1 = x0 + l.pagevw in
184 let y0 = l.pagedispy in
185 let y1 = y0 + l.pagevh in
186 if y >= y0 && y <= y1 && x >= x0 && x <= x1
187 then
188 let px, py = pagetranslatepoint l x y in
189 match g opaque l px py with
190 | Some res -> res
191 | None -> f rest
192 else f rest
193 | _ ->
194 f rest
196 | [] -> d
198 f state.layout
201 let getunder x y =
202 let g opaque l px py =
203 if state.bzoom
204 then (
205 match rectofblock opaque px py with
206 | Some [|x0;x1;y0;y1|] ->
207 let ox = xadjsb () |> float in
208 let rect = (x0+.ox, y0, x1+.ox, y0, x1+.ox, y1, x0+.ox, y1) in
209 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
210 state.rects <- [l.pageno, color, rect];
211 G.postRedisplay "getunder";
212 | _otherwise -> ()
214 let under = whatsunder opaque px py in
215 if under = Unone then None else Some under
217 onppundermouse g x y Unone
220 let unproject x y =
221 let g opaque l x y =
222 match unproject opaque x y with
223 | Some (x, y) -> Some (Some (opaque, l.pageno, x, y))
224 | None -> None
226 onppundermouse g x y None;
229 let showtext c s =
230 state.text <- Printf.sprintf "%c%s" c s;
231 G.postRedisplay "showtext";
234 let impmsg fmt =
235 Format.ksprintf (fun s -> showtext '!' s) fmt;
238 let pipesel opaque cmd =
239 if hassel opaque
240 then
241 match Unix.pipe () with
242 | (exception exn) -> dolog "pipesel cannot create pipe: %S" @@ exntos exn;
243 | (r, w) ->
244 let doclose what fd =
245 Ne.clo fd (fun msg -> dolog "%s close failed: %s" what msg)
247 let pid =
248 try spawn cmd [r, 0; w, -1]
249 with exn ->
250 dolog "cannot execute %S: %s" cmd @@ exntos exn;
253 if pid > 0
254 then (
255 copysel w opaque;
256 G.postRedisplay "pipesel";
258 else doclose "pipesel pipe/w" w;
259 doclose "pipesel pipe/r" r;
262 let paxunder x y =
263 let g opaque l px py =
264 if markunder opaque px py conf.paxmark
265 then (
266 Some (fun () ->
267 match getopaque l.pageno with
268 | None -> ()
269 | Some opaque -> pipesel opaque conf.paxcmd
272 else None
274 G.postRedisplay "paxunder";
275 if conf.paxmark = Mark_page
276 then
277 List.iter (fun l ->
278 match getopaque l.pageno with
279 | None -> ()
280 | Some opaque -> clearmark opaque) state.layout;
281 state.roam <- onppundermouse g x y (fun () -> impmsg "whoopsie daisy");
284 let selstring s =
285 match Unix.pipe () with
286 | (exception exn) -> impmsg "pipe failed: %s" @@ exntos exn
287 | (r, w) ->
288 let clo cap fd =
289 Ne.clo fd (fun msg -> impmsg "failed to close %s: %s" cap msg)
291 let pid =
292 try spawn conf.selcmd [r, 0; w, -1]
293 with exn ->
294 impmsg "failed to execute %s: %s" conf.selcmd @@ exntos exn;
297 if pid > 0
298 then (
300 let l = String.length s in
301 let bytes = Bytes.unsafe_of_string s in
302 let n = tempfailureretry (Unix.write w bytes 0) l in
303 if n != l
304 then impmsg "failed to write %d characters to sel pipe, wrote %d"
306 with exn ->
307 impmsg "failed to write to sel pipe: %s" @@ exntos exn
309 else dolog "%s" s;
310 clo "selstring pipe/r" r;
311 clo "selstring pipe/w" w;
314 let undertext ?(nopath=false) = function
315 | Unone -> "none"
316 | Ulinkuri s -> s
317 | Ulinkgoto (pageno, _) ->
318 if nopath
319 then "page " ^ string_of_int (pageno+1)
320 else Printf.sprintf "%s: page %d" state.path (pageno+1)
321 | Utext s -> "font: " ^ s
322 | Uunexpected s -> "unexpected: " ^ s
323 | Ulaunch s -> "launch: " ^ s
324 | Unamed s -> "named: " ^ s
325 | Uremote (filename, pageno) ->
326 Printf.sprintf "%s: page %d" filename (pageno+1)
327 | Uremotedest (filename, destname) ->
328 Printf.sprintf "%s: destination %S" filename destname
329 | Uannotation (opaque, slinkindex) ->
330 "annotation: " ^ getannotcontents opaque slinkindex
333 let updateunder x y =
334 match getunder x y with
335 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
336 | Ulinkuri uri ->
337 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
338 Wsi.setcursor Wsi.CURSOR_INFO
339 | Ulinkgoto (pageno, _) ->
340 if conf.underinfo
341 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
342 Wsi.setcursor Wsi.CURSOR_INFO
343 | Utext s ->
344 if conf.underinfo then showtext 'f' ("ont: " ^ s);
345 Wsi.setcursor Wsi.CURSOR_TEXT
346 | Uunexpected s ->
347 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
348 Wsi.setcursor Wsi.CURSOR_INHERIT
349 | Ulaunch s ->
350 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
351 Wsi.setcursor Wsi.CURSOR_INHERIT
352 | Unamed s ->
353 if conf.underinfo then showtext 'n' ("amed: " ^ s);
354 Wsi.setcursor Wsi.CURSOR_INHERIT
355 | Uremote (filename, pageno) ->
356 if conf.underinfo then showtext 'r'
357 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
358 Wsi.setcursor Wsi.CURSOR_INFO
359 | Uremotedest (filename, destname) ->
360 if conf.underinfo then showtext 'r'
361 (Printf.sprintf "emote destination: %s (%S)" filename destname);
362 Wsi.setcursor Wsi.CURSOR_INFO
363 | Uannotation _ ->
364 if conf.underinfo then showtext 'a' "nnotation";
365 Wsi.setcursor Wsi.CURSOR_INFO
368 let showlinktype under =
369 if conf.underinfo && under != Unone
370 then showtext ' ' @@ undertext under
373 let intentry_with_suffix text key =
374 let c =
375 if key >= 32 && key < 127
376 then Char.chr key
377 else '\000'
379 match Char.lowercase c with
380 | '0' .. '9' ->
381 let text = addchar text c in
382 TEcont text
384 | 'k' | 'm' | 'g' ->
385 let text = addchar text c in
386 TEcont text
388 | _ ->
389 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
390 TEcont text
393 let readcmd fd =
394 let s = Bytes.create 4 in
395 let n = tempfailureretry (Unix.read fd s 0) 4 in
396 if n != 4 then error "incomplete read(len) = %d" n;
397 let len = (Char.code (Bytes.get s 0) lsl 24)
398 lor (Char.code (Bytes.get s 1) lsl 16)
399 lor (Char.code (Bytes.get s 2) lsl 8)
400 lor (Char.code (Bytes.get s 3))
402 let s = Bytes.create len in
403 let n = tempfailureretry (Unix.read fd s 0) len in
404 if n != len then error "incomplete read(data) %d vs %d" n len;
405 Bytes.to_string s
408 let wcmd fmt =
409 let b = Buffer.create 16 in
410 Buffer.add_string b "llll";
411 Printf.kbprintf
412 (fun b ->
413 let s = Buffer.to_bytes b in
414 let n = Bytes.length s in
415 let len = n - 4 in
416 (* dolog "wcmd %S" (String.sub s 4 len); *)
417 Bytes.set s 0 (Char.chr ((len lsr 24) land 0xff));
418 Bytes.set s 1 (Char.chr ((len lsr 16) land 0xff));
419 Bytes.set s 2 (Char.chr ((len lsr 8) land 0xff));
420 Bytes.set s 3 (Char.chr (len land 0xff));
421 let n' = tempfailureretry (Unix.write state.ss s 0) n in
422 if n' != n then error "write failed %d vs %d" n' n;
423 ) b fmt;
426 let nogeomcmds cmds =
427 match cmds with
428 | s, [] -> emptystr s
429 | _ -> false
432 let layoutN ((columns, coverA, coverB), b) x y sw sh =
433 let sh = sh - (hscrollh ()) in
434 let wadj = wadjsb () in
435 let rec fold accu n =
436 if n = Array.length b
437 then accu
438 else
439 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
440 if (vy - y) > sh &&
441 (n = coverA - 1
442 || n = state.pagecount - coverB
443 || (n - coverA) mod columns = columns - 1)
444 then accu
445 else
446 let accu =
447 if vy + h > y
448 then
449 let pagey = max 0 (y - vy) in
450 let pagedispy = if pagey > 0 then 0 else vy - y in
451 let pagedispx, pagex =
452 let pdx =
453 if n = coverA - 1 || n = state.pagecount - coverB
454 then x + (wadj + sw - w) / 2
455 else dx + xoff + x
457 if pdx < 0
458 then 0, -pdx
459 else pdx, 0
461 let pagevw =
462 let vw = wadj + sw - pagedispx in
463 let pw = w - pagex in
464 min vw pw
466 let pagevh = min (h - pagey) (sh - pagedispy) in
467 if pagevw > 0 && pagevh > 0
468 then
469 let e =
470 { pageno = n
471 ; pagedimno = pdimno
472 ; pagew = w
473 ; pageh = h
474 ; pagex = pagex
475 ; pagey = pagey
476 ; pagevw = pagevw
477 ; pagevh = pagevh
478 ; pagedispx = pagedispx
479 ; pagedispy = pagedispy
480 ; pagecol = 0
483 e :: accu
484 else
485 accu
486 else
487 accu
489 fold accu (n+1)
491 if Array.length b = 0
492 then []
493 else List.rev (fold [] (page_of_y y))
496 let layoutS (columns, b) x y sw sh =
497 let sh = sh - hscrollh () in
498 let wadj = wadjsb () in
499 let rec fold accu n =
500 if n = Array.length b
501 then accu
502 else
503 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
504 if (vy - y) > sh
505 then accu
506 else
507 let accu =
508 if vy + pageh > y
509 then
510 let x = xoff + x in
511 let pagey = max 0 (y - vy) in
512 let pagedispy = if pagey > 0 then 0 else vy - y in
513 let pagedispx, pagex =
514 if px = 0
515 then (
516 if x < 0
517 then 0, -x
518 else x, 0
520 else (
521 let px = px - x in
522 if px < 0
523 then -px, 0
524 else 0, px
527 let pagecolw = pagew/columns in
528 let pagedispx =
529 if pagecolw < sw
530 then pagedispx + ((wadj + sw - pagecolw) / 2)
531 else pagedispx
533 let pagevw =
534 let vw = wadj + sw - pagedispx in
535 let pw = pagew - pagex in
536 min vw pw
538 let pagevw = min pagevw pagecolw in
539 let pagevh = min (pageh - pagey) (sh - pagedispy) in
540 if pagevw > 0 && pagevh > 0
541 then
542 let e =
543 { pageno = n/columns
544 ; pagedimno = pdimno
545 ; pagew = pagew
546 ; pageh = pageh
547 ; pagex = pagex
548 ; pagey = pagey
549 ; pagevw = pagevw
550 ; pagevh = pagevh
551 ; pagedispx = pagedispx
552 ; pagedispy = pagedispy
553 ; pagecol = n mod columns
556 e :: accu
557 else
558 accu
559 else
560 accu
562 fold accu (n+1)
564 List.rev (fold [] 0)
567 let layout x y sw sh =
568 if nogeomcmds state.geomcmds
569 then
570 match conf.columns with
571 | Csingle b -> layoutN ((1, 0, 0), b) x y sw sh
572 | Cmulti c -> layoutN c x y sw sh
573 | Csplit s -> layoutS s x y sw sh
574 else []
577 let clamp incr =
578 let y = state.y + incr in
579 let y = max 0 y in
580 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
584 let itertiles l f =
585 let tilex = l.pagex mod conf.tilew in
586 let tiley = l.pagey mod conf.tileh in
588 let col = l.pagex / conf.tilew in
589 let row = l.pagey / conf.tileh in
591 let xadj = xadjsb () in
592 let rec rowloop row y0 dispy h =
593 if h = 0
594 then ()
595 else (
596 let dh = conf.tileh - y0 in
597 let dh = min h dh in
598 let rec colloop col x0 dispx w =
599 if w = 0
600 then ()
601 else (
602 let dw = conf.tilew - x0 in
603 let dw = min w dw in
604 let dispx' = xadj + dispx in
605 f col row dispx' dispy x0 y0 dw dh;
606 colloop (col+1) 0 (dispx+dw) (w-dw)
609 colloop col tilex l.pagedispx l.pagevw;
610 rowloop (row+1) 0 (dispy+dh) (h-dh)
613 if l.pagevw > 0 && l.pagevh > 0
614 then rowloop row tiley l.pagedispy l.pagevh;
617 let gettileopaque l col row =
618 let key =
619 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
621 try Some (Hashtbl.find state.tilemap key)
622 with Not_found -> None
625 let puttileopaque l col row gen colorspace angle opaque size elapsed =
626 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
627 Hashtbl.add state.tilemap key (opaque, size, elapsed)
630 let filledrect2 x0 y0 x1 y1 x2 y2 x3 y3 =
631 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x1; y1; x2; y2; x3; y3 |];
632 GlArray.vertex `two state.vraw;
633 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
636 let filledrect1 x0 y0 x1 y1 = filledrect2 x0 y0 x0 y1 x1 y0 x1 y1;;
638 let filledrect x0 y0 x1 y1 =
639 GlArray.disable `texture_coord;
640 filledrect1 x0 y0 x1 y1;
641 GlArray.enable `texture_coord;
644 let linerect x0 y0 x1 y1 =
645 GlArray.disable `texture_coord;
646 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y1; x1; y0 |];
647 GlArray.vertex `two state.vraw;
648 GlArray.draw_arrays `line_loop ~first:0 ~count:4;
649 GlArray.enable `texture_coord;
652 let drawtiles l color =
653 GlDraw.color color;
654 let wadj = wadjsb () in
655 begintiles ();
656 let f col row x y tilex tiley w h =
657 match gettileopaque l col row with
658 | Some (opaque, _, t) ->
659 let params = x, y, w, h, tilex, tiley in
660 if conf.invert
661 then GlTex.env (`mode `blend);
662 drawtile params opaque;
663 if conf.invert
664 then GlTex.env (`mode `modulate);
665 if conf.debug
666 then (
667 endtiles ();
668 let s = Printf.sprintf
669 "%d[%d,%d] %f sec"
670 l.pageno col row t
672 let w = measurestr fstate.fontsize s in
673 GlDraw.color (0.0, 0.0, 0.0);
674 filledrect (float (x-2))
675 (float (y-2))
676 (float (x+2) +. w)
677 (float (y + fstate.fontsize + 2));
678 GlDraw.color color;
679 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
680 begintiles ();
683 | None ->
684 endtiles ();
685 let w =
686 if conf.leftscroll
687 then w
688 else
689 let lw = wadj + state.winw - x in
690 min lw w
691 and h =
692 let lh = state.winh - y in
693 min lh h
695 if conf.invert
696 then GlTex.env (`mode `blend);
697 begin match state.checkerstexid with
698 | Some id ->
699 Gl.enable `texture_2d;
700 GlTex.bind_texture ~target:`texture_2d id;
701 let x0 = float x
702 and y0 = float y
703 and x1 = float (x+w)
704 and y1 = float (y+h) in
706 let tw = float w /. 16.0
707 and th = float h /. 16.0 in
708 let tx0 = float tilex /. 16.0
709 and ty0 = float tiley /. 16.0 in
710 let tx1 = tx0 +. tw
711 and ty1 = ty0 +. th in
712 Raw.sets_float state.vraw ~pos:0
713 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
714 Raw.sets_float state.traw ~pos:0
715 [| tx0; ty0; tx0; ty1; tx1; ty0; tx1; ty1 |];
716 GlArray.vertex `two state.vraw;
717 GlArray.tex_coord `two state.traw;
718 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
719 Gl.disable `texture_2d;
721 | None ->
722 GlDraw.color (1.0, 1.0, 1.0);
723 filledrect (float x) (float y) (float (x+w)) (float (y+h));
724 end;
725 if conf.invert
726 then GlTex.env (`mode `modulate);
727 if w > 128 && h > fstate.fontsize + 10
728 then (
729 let c = if conf.invert then 1.0 else 0.0 in
730 GlDraw.color (c, c, c);
731 let c, r =
732 if conf.verbose
733 then (col*conf.tilew, row*conf.tileh)
734 else col, row
736 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
738 GlDraw.color color;
739 begintiles ();
741 itertiles l f;
742 endtiles ();
745 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
747 let tilevisible1 l x y =
748 let ax0 = l.pagex
749 and ax1 = l.pagex + l.pagevw
750 and ay0 = l.pagey
751 and ay1 = l.pagey + l.pagevh in
753 let bx0 = x
754 and by0 = y in
755 let bx1 = min (bx0 + conf.tilew) l.pagew
756 and by1 = min (by0 + conf.tileh) l.pageh in
758 let rx0 = max ax0 bx0
759 and ry0 = max ay0 by0
760 and rx1 = min ax1 bx1
761 and ry1 = min ay1 by1 in
763 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
764 nonemptyintersection
767 let tilevisible layout n x y =
768 let rec findpageinlayout m = function
769 | l :: rest when l.pageno = n ->
770 tilevisible1 l x y || (
771 match conf.columns with
772 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
773 | Csplit _
774 | Csingle _
775 | Cmulti _ -> false
777 | _ :: rest -> findpageinlayout 0 rest
778 | [] -> false
780 findpageinlayout 0 layout;
783 let tileready l x y =
784 tilevisible1 l x y &&
785 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
788 let tilepage n p layout =
789 let rec loop = function
790 | l :: rest ->
791 if l.pageno = n
792 then
793 let f col row _ _ _ _ _ _ =
794 if state.currently = Idle
795 then
796 match gettileopaque l col row with
797 | Some _ -> ()
798 | None ->
799 let x = col*conf.tilew
800 and y = row*conf.tileh in
801 let w =
802 let w = l.pagew - x in
803 min w conf.tilew
805 let h =
806 let h = l.pageh - y in
807 min h conf.tileh
809 let pbo =
810 if conf.usepbo
811 then getpbo w h conf.colorspace
812 else ~< "0"
814 wcmd "tile %s %d %d %d %d %s"
815 (~> p) x y w h (~> pbo);
816 state.currently <-
817 Tiling (
818 l, p, conf.colorspace, conf.angle,
819 state.gen, col, row, conf.tilew, conf.tileh
822 itertiles l f;
823 else
824 loop rest
826 | [] -> ()
828 if nogeomcmds state.geomcmds
829 then loop layout;
832 let preloadlayout x y sw sh =
833 let y = if y < sh then 0 else y - sh in
834 let x = min 0 (x + sw) in
835 let h = sh*3 in
836 let w = sw*3 in
837 layout x y w h;
840 let load pages =
841 let rec loop pages =
842 if state.currently != Idle
843 then ()
844 else
845 match pages with
846 | l :: rest ->
847 begin match getopaque l.pageno with
848 | None ->
849 wcmd "page %d %d" l.pageno l.pagedimno;
850 state.currently <- Loading (l, state.gen);
851 | Some opaque ->
852 tilepage l.pageno opaque pages;
853 loop rest
854 end;
855 | _ -> ()
857 if nogeomcmds state.geomcmds
858 then loop pages
861 let preload pages =
862 load pages;
863 if conf.preload && state.currently = Idle
864 then load (preloadlayout state.x state.y state.winw state.winh);
867 let layoutready layout =
868 let rec fold all ls =
869 all && match ls with
870 | l :: rest ->
871 let seen = ref false in
872 let allvisible = ref true in
873 let foo col row _ _ _ _ _ _ =
874 seen := true;
875 allvisible := !allvisible &&
876 begin match gettileopaque l col row with
877 | Some _ -> true
878 | None -> false
881 itertiles l foo;
882 fold (!seen && !allvisible) rest
883 | [] -> true
885 let alltilesvisible = fold true layout in
886 alltilesvisible;
889 let gotoy y =
890 let y = bound y 0 state.maxy in
891 let y, layout, proceed =
892 match conf.maxwait with
893 | Some time when state.ghyll == noghyll ->
894 begin match state.throttle with
895 | None ->
896 let layout = layout state.x y state.winw state.winh in
897 let ready = layoutready layout in
898 if not ready
899 then (
900 load layout;
901 state.throttle <- Some (layout, y, now ());
903 else G.postRedisplay "gotoy showall (None)";
904 y, layout, ready
905 | Some (_, _, started) ->
906 let dt = now () -. started in
907 if dt > time
908 then (
909 state.throttle <- None;
910 let layout = layout state.x y state.winw state.winh in
911 load layout;
912 G.postRedisplay "maxwait";
913 y, layout, true
915 else -1, [], false
918 | _ ->
919 let layout = layout state.x y state.winw state.winh in
920 if not !wtmode || layoutready layout
921 then G.postRedisplay "gotoy ready";
922 y, layout, true
924 if proceed
925 then (
926 state.y <- y;
927 state.layout <- layout;
928 begin match state.mode with
929 | LinkNav ln ->
930 begin match ln with
931 | Ltexact (pageno, linkno) ->
932 let rec loop = function
933 | [] ->
934 state.mode <- LinkNav (Ltgendir 0)
935 | l :: _ when l.pageno = pageno ->
936 begin match getopaque pageno with
937 | None -> state.mode <- LinkNav (Ltnotready (pageno, 0))
938 | Some opaque ->
939 let x0, y0, x1, y1 = getlinkrect opaque linkno in
940 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
941 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
942 then state.mode <- LinkNav (Ltgendir 0)
944 | _ :: rest -> loop rest
946 loop layout
947 | Ltnotready _ | Ltgendir _ -> ()
949 | Birdseye _
950 | Textentry _
951 | View -> ()
952 end;
953 begin match state.mode with
954 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
955 if not (pagevisible layout pageno)
956 then (
957 match state.layout with
958 | [] -> ()
959 | l :: _ ->
960 state.mode <- Birdseye (
961 conf, leftx, l.pageno, hooverpageno, anchor
964 | LinkNav lt ->
965 begin match lt with
966 | Ltnotready (_, dir)
967 | Ltgendir dir ->
968 let linknav =
969 let rec loop = function
970 | [] -> lt
971 | l :: rest ->
972 match getopaque l.pageno with
973 | None -> Ltnotready (l.pageno, dir)
974 | Some opaque ->
975 let link =
976 let ld =
977 if dir = 0
978 then LDfirstvisible (l.pagex, l.pagey, dir)
979 else (
980 if dir > 0 then LDfirst else LDlast
983 findlink opaque ld
985 match link with
986 | Lnotfound -> loop rest
987 | Lfound n ->
988 showlinktype (getlink opaque n);
989 Ltexact (l.pageno, n)
991 loop state.layout
993 state.mode <- LinkNav linknav
994 | Ltexact _ -> ()
996 | Textentry _
997 | View -> ()
998 end;
999 preload layout;
1001 state.ghyll <- noghyll;
1002 if conf.updatecurs
1003 then (
1004 let mx, my = state.mpos in
1005 updateunder mx my;
1009 let conttiling pageno opaque =
1010 tilepage pageno opaque
1011 (if conf.preload
1012 then preloadlayout state.x state.y state.winw state.winh
1013 else state.layout)
1016 let gotoy_and_clear_text y =
1017 if not conf.verbose then state.text <- E.s;
1018 gotoy y;
1021 let getanchory (n, top, dtop) =
1022 let y, h = getpageyh n in
1023 if conf.presentation
1024 then
1025 let ips = calcips h in
1026 y + truncate (top*.float h -. dtop*.float ips) + ips;
1027 else
1028 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1031 let gotoanchor anchor =
1032 gotoy (getanchory anchor);
1035 let addnav () =
1036 cbput state.hists.nav (getanchor ());
1039 let getnav dir =
1040 let anchor = cbgetc state.hists.nav dir in
1041 getanchory anchor;
1044 let gotoghyll1 single y =
1045 let scroll f n a b =
1046 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1047 let snake f a b =
1048 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1049 if f < a
1050 then s (float f /. float a)
1051 else (
1052 if f > b
1053 then 1.0 -. s ((float (f-b) /. float (n-b)))
1054 else 1.0
1057 snake f a b
1058 and summa n a b =
1059 let ins = float a *. 0.5
1060 and outs = float (n-b) *. 0.5 in
1061 let ones = b - a in
1062 ins +. outs +. float ones
1064 let rec set nab y sy =
1065 let (_N, _A, _B), y =
1066 if single
1067 then
1068 let scl = if y > sy then 2 else -2 in
1069 let _N, _, _ = nab in
1070 (_N,0,_N), y+conf.scrollstep*scl
1071 else nab,y in
1072 let sum = summa _N _A _B in
1073 let dy = float (y - sy) in
1074 state.ghyll <- (
1075 let rec gf n y1 o =
1076 if n >= _N
1077 then state.ghyll <- noghyll
1078 else
1079 let go n =
1080 let s = scroll n _N _A _B in
1081 let y1 = y1 +. ((s *. dy) /. sum) in
1082 gotoy_and_clear_text (truncate y1);
1083 state.ghyll <- gf (n+1) y1;
1085 match o with
1086 | None -> go n
1087 | Some y' when single -> set nab y' state.y
1088 | Some y' -> set (_N/2, 1, 1) y' state.y
1090 gf 0 (float state.y)
1093 match conf.ghyllscroll with
1094 | Some nab when not conf.presentation ->
1095 if state.ghyll == noghyll
1096 then set nab y state.y
1097 else state.ghyll (Some y)
1098 | _ ->
1099 gotoy_and_clear_text y
1102 let gotoghyll = gotoghyll1 false;;
1104 let gotopage n top =
1105 let y, h = getpageyh n in
1106 let y = y + (truncate (top *. float h)) in
1107 gotoghyll y
1110 let gotopage1 n top =
1111 let y = getpagey n in
1112 let y = y + top in
1113 gotoghyll y
1116 let invalidate s f =
1117 state.layout <- [];
1118 state.pdims <- [];
1119 state.rects <- [];
1120 state.rects1 <- [];
1121 match state.geomcmds with
1122 | ps, [] when emptystr ps ->
1123 f ();
1124 state.geomcmds <- s, [];
1126 | ps, [] ->
1127 state.geomcmds <- ps, [s, f];
1129 | ps, (s', _) :: rest when s' = s ->
1130 state.geomcmds <- ps, ((s, f) :: rest);
1132 | ps, cmds ->
1133 state.geomcmds <- ps, ((s, f) :: cmds);
1136 let flushpages () =
1137 Hashtbl.iter (fun _ opaque ->
1138 wcmd "freepage %s" (~> opaque);
1139 ) state.pagemap;
1140 Hashtbl.clear state.pagemap;
1143 let flushtiles () =
1144 if not (Queue.is_empty state.tilelru)
1145 then (
1146 Queue.iter (fun (k, p, s) ->
1147 wcmd "freetile %s" (~> p);
1148 state.memused <- state.memused - s;
1149 Hashtbl.remove state.tilemap k;
1150 ) state.tilelru;
1151 state.uioh#infochanged Memused;
1152 Queue.clear state.tilelru;
1154 load state.layout;
1157 let stateh h =
1158 let h = truncate (float h*.conf.zoom) in
1159 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
1160 h - d
1163 let opendoc path password =
1164 state.path <- path;
1165 state.password <- password;
1166 state.gen <- state.gen + 1;
1167 state.docinfo <- [];
1168 state.outlines <- [||];
1170 flushpages ();
1171 setaalevel conf.aalevel;
1172 let titlepath =
1173 if emptystr state.origin
1174 then path
1175 else state.origin
1177 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
1178 wcmd "open %d %d %s\000%s\000" (btod !wtmode) (btod !cxack) path password;
1179 invalidate "reqlayout"
1180 (fun () ->
1181 wcmd "reqlayout %d %d %d %s\000"
1182 conf.angle (FMTE.to_int conf.fitmodel)
1183 (stateh state.winh) state.nameddest
1187 let reload () =
1188 state.anchor <- getanchor ();
1189 opendoc state.path state.password;
1192 let scalecolor c =
1193 let c = c *. conf.colorscale in
1194 (c, c, c);
1197 let scalecolor2 (r, g, b) =
1198 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1201 let docolumns columns =
1202 let wadj = wadjsb () in
1203 match columns with
1204 | Csingle _ ->
1205 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1206 let wadj = wadjsb () in
1207 let rec loop pageno pdimno pdim y ph pdims =
1208 if pageno = state.pagecount
1209 then ()
1210 else
1211 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1212 match pdims with
1213 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1214 pdimno+1, pdim, rest
1215 | _ ->
1216 pdimno, pdim, pdims
1218 let x = max 0 (((wadj + state.winw - w) / 2) - xoff) in
1219 let y = y +
1220 (if conf.presentation
1221 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1222 else (if pageno = 0 then 0 else conf.interpagespace)
1225 a.(pageno) <- (pdimno, x, y, pdim);
1226 loop (pageno+1) pdimno pdim (y + h) h pdims
1228 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1229 conf.columns <- Csingle a;
1231 | Cmulti ((columns, coverA, coverB), _) ->
1232 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1233 let rec loop pageno pdimno pdim x y rowh pdims =
1234 let rec fixrow m = if m = pageno then () else
1235 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1236 if h < rowh
1237 then (
1238 let y = y + (rowh - h) / 2 in
1239 a.(m) <- (pdimno, x, y, pdim);
1241 fixrow (m+1)
1243 if pageno = state.pagecount
1244 then fixrow (((pageno - 1) / columns) * columns)
1245 else
1246 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1247 match pdims with
1248 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1249 pdimno+1, pdim, rest
1250 | _ ->
1251 pdimno, pdim, pdims
1253 let x, y, rowh' =
1254 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1255 then (
1256 let x = (wadj + state.winw - w) / 2 in
1257 let ips =
1258 if conf.presentation then calcips h else conf.interpagespace in
1259 x, y + ips + rowh, h
1261 else (
1262 if (pageno - coverA) mod columns = 0
1263 then (
1264 let x = max 0 (wadj + state.winw - state.w) / 2 in
1265 let y =
1266 if conf.presentation
1267 then
1268 let ips = calcips h in
1269 y + (if pageno = 0 then 0 else calcips rowh + ips)
1270 else
1271 y + (if pageno = 0 then 0 else conf.interpagespace)
1273 x, y + rowh, h
1275 else x, y, max rowh h
1278 let y =
1279 if pageno > 1 && (pageno - coverA) mod columns = 0
1280 then (
1281 let y =
1282 if pageno = columns && conf.presentation
1283 then (
1284 let ips = calcips rowh in
1285 for i = 0 to pred columns
1287 let (pdimno, x, y, pdim) = a.(i) in
1288 a.(i) <- (pdimno, x, y+ips, pdim)
1289 done;
1290 y+ips;
1292 else y
1294 fixrow (pageno - columns);
1297 else y
1299 a.(pageno) <- (pdimno, x, y, pdim);
1300 let x = x + w + xoff*2 + conf.interpagespace in
1301 loop (pageno+1) pdimno pdim x y rowh' pdims
1303 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1304 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1306 | Csplit (c, _) ->
1307 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1308 let rec loop pageno pdimno pdim y pdims =
1309 if pageno = state.pagecount
1310 then ()
1311 else
1312 let pdimno, ((_, w, h, _) as pdim), pdims =
1313 match pdims with
1314 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1315 pdimno+1, pdim, rest
1316 | _ ->
1317 pdimno, pdim, pdims
1319 let cw = w / c in
1320 let rec loop1 n x y =
1321 if n = c then y else (
1322 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1323 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1326 let y = loop1 0 0 y in
1327 loop (pageno+1) pdimno pdim y pdims
1329 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1330 conf.columns <- Csplit (c, a);
1333 let represent () =
1334 docolumns conf.columns;
1335 state.maxy <- calcheight ();
1336 if state.reprf == noreprf
1337 then (
1338 match state.mode with
1339 | Birdseye (_, _, pageno, _, _) ->
1340 let y, h = getpageyh pageno in
1341 let top = (state.winh - h) / 2 in
1342 gotoy (max 0 (y - top))
1343 | Textentry _
1344 | View
1345 | LinkNav _ ->
1346 let y = getanchory state.anchor in
1347 let y = min y (state.maxy - state.winw - hscrollh ()) in
1348 gotoy y;
1350 else (
1351 state.reprf ();
1352 state.reprf <- noreprf;
1356 let reshape ?(firsttime=false) w h =
1357 GlDraw.viewport ~x:0 ~y:0 ~w:w ~h:h;
1358 if not firsttime && nogeomcmds state.geomcmds
1359 then state.anchor <- getanchor ();
1361 state.winw <- w;
1362 let w = wadjsb () + (truncate (float w *. conf.zoom)) in
1363 let w = max w 2 in
1364 state.winh <- h;
1365 setfontsize fstate.fontsize;
1366 GlMat.mode `modelview;
1367 GlMat.load_identity ();
1369 GlMat.mode `projection;
1370 GlMat.load_identity ();
1371 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1372 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1373 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
1375 let relx =
1376 if conf.zoom <= 1.0
1377 then 0.0
1378 else float state.x /. float state.w
1380 invalidate "geometry"
1381 (fun () ->
1382 state.w <- w;
1383 if not firsttime
1384 then state.x <- truncate (relx *. float w);
1385 let w =
1386 match conf.columns with
1387 | Csingle _ -> w
1388 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1389 | Csplit (c, _) -> w * c
1391 wcmd "geometry %d %d %d"
1392 w (stateh h) (FMTE.to_int conf.fitmodel)
1396 let enttext () =
1397 let len = String.length state.text in
1398 let x0 = xadjsb () in
1399 let drawstring s =
1400 let hscrollh =
1401 match state.mode with
1402 | Textentry _ | View | LinkNav _ ->
1403 let h, _, _ = state.uioh#scrollpw in
1405 | Birdseye _ -> 0
1407 let rect x w =
1408 filledrect x (float (state.winh - (fstate.fontsize + 4) - hscrollh))
1409 (x+.w) (float (state.winh - hscrollh))
1412 let w = float (wadjsb () + state.winw - 1) in
1413 if state.progress >= 0.0 && state.progress < 1.0
1414 then (
1415 GlDraw.color (0.3, 0.3, 0.3);
1416 let w1 = w *. state.progress in
1417 rect (float x0) w1;
1418 GlDraw.color (0.0, 0.0, 0.0);
1419 rect (float x0+.w1) (float x0+.w-.w1)
1421 else (
1422 GlDraw.color (0.0, 0.0, 0.0);
1423 rect (float x0) w;
1426 GlDraw.color (1.0, 1.0, 1.0);
1427 drawstring fstate.fontsize
1428 (if conf.leftscroll then x0 + 2 else x0 + if len > 0 then 8 else 2)
1429 (state.winh - hscrollh - 5) s;
1431 let s =
1432 match state.mode with
1433 | Textentry ((prefix, text, _, _, _, _), _) ->
1434 let s =
1435 if len > 0
1436 then
1437 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1438 else
1439 Printf.sprintf "%s%s_" prefix text
1443 | Birdseye _
1444 | View
1445 | LinkNav _ -> state.text
1447 let s =
1448 if state.newerrmsgs
1449 then (
1450 if not (istextentry state.mode) && state.uioh#eformsgs
1451 then
1452 let s1 = "(press 'e' to review error messasges)" in
1453 if nonemptystr s then s ^ " " ^ s1 else s1
1454 else s
1456 else s
1458 if nonemptystr s
1459 then drawstring s
1462 let gctiles () =
1463 let len = Queue.length state.tilelru in
1464 let layout = lazy (
1465 match state.throttle with
1466 | None ->
1467 if conf.preload
1468 then preloadlayout state.x state.y state.winw state.winh
1469 else state.layout
1470 | Some (layout, _, _) ->
1471 layout
1472 ) in
1473 let rec loop qpos =
1474 if state.memused <= conf.memlimit
1475 then ()
1476 else (
1477 if qpos < len
1478 then
1479 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1480 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1481 let (_, pw, ph, _) = getpagedim n in
1483 gen = state.gen
1484 && colorspace = conf.colorspace
1485 && angle = conf.angle
1486 && pagew = pw
1487 && pageh = ph
1488 && (
1489 let x = col*conf.tilew
1490 and y = row*conf.tileh in
1491 tilevisible (Lazy.force_val layout) n x y
1493 then Queue.push lruitem state.tilelru
1494 else (
1495 freepbo p;
1496 wcmd "freetile %s" (~> p);
1497 state.memused <- state.memused - s;
1498 state.uioh#infochanged Memused;
1499 Hashtbl.remove state.tilemap k;
1501 loop (qpos+1)
1504 loop 0
1507 let onpagerect pageno f =
1508 let b =
1509 match conf.columns with
1510 | Cmulti (_, b) -> b
1511 | Csingle b -> b
1512 | Csplit (_, b) -> b
1514 if pageno >= 0 && pageno < Array.length b
1515 then
1516 let (_, _, _, (_, w, h, _)) = b.(pageno) in
1517 f w h
1520 let gotopagexy1 wtmode pageno x y =
1521 let _,w1,h1,leftx = getpagedim pageno in
1522 let top = y /. (float h1) in
1523 let left = x /. (float w1) in
1524 let py, w, h = getpageywh pageno in
1525 let wh = state.winh - hscrollh () in
1526 let x = left *. (float w) in
1527 let x = leftx + state.x + truncate x in
1528 let wadj = wadjsb () in
1529 let sx =
1530 if x < 0 || x >= wadj + state.winw
1531 then state.x - x
1532 else state.x
1534 let pdy = truncate (top *. float h) in
1535 let y' = py + pdy in
1536 let dy = y' - state.y in
1537 let sy =
1538 if x != state.x || not (dy > 0 && dy < wh)
1539 then (
1540 if conf.presentation
1541 then
1542 if abs (py - y') > wh
1543 then y'
1544 else py
1545 else y';
1547 else state.y
1549 if state.x != sx || state.y != sy
1550 then (
1551 let x, y =
1552 if wtmode
1553 then (
1554 let ww = wadj + state.winw in
1555 let qx = sx / ww
1556 and qy = pdy / wh in
1557 let x = qx * ww
1558 and y = py + qy * wh in
1559 let x = if -x + ww > w1 then -(w1-ww) else x
1560 and y' = if y + wh > state.maxy then state.maxy - wh else y in
1561 let y =
1562 if conf.presentation
1563 then
1564 if abs (py - y') > wh
1565 then y'
1566 else py
1567 else y';
1569 (x, y)
1571 else (sx, sy)
1573 state.x <- x;
1574 gotoy_and_clear_text y;
1576 else gotoy_and_clear_text state.y;
1579 let gotopagexy wtmode pageno x y =
1580 match state.mode with
1581 | Birdseye _ -> gotopage pageno 0.0
1582 | Textentry _
1583 | View
1584 | LinkNav _ -> gotopagexy1 wtmode pageno x y
1587 let getpassword () =
1588 let passcmd = getenvwithdef "LLPP_ASKPASS" conf.passcmd in
1589 if emptystr passcmd
1590 then E.s
1591 else getcmdoutput
1592 (fun s ->
1593 impmsg "error getting password: %s" s;
1594 dolog "%s" s) passcmd;
1597 let pgoto opaque pageno x y =
1598 let pdimno = getpdimno pageno in
1599 let x, y = project opaque pageno pdimno x y in
1600 gotopagexy false pageno x y;
1603 let act cmds =
1604 (* dolog "%S" cmds; *)
1605 let cl = splitatspace cmds in
1606 let scan s fmt f =
1607 try Scanf.sscanf s fmt f
1608 with exn ->
1609 dolog "error processing '%S': %s" cmds @@ exntos exn;
1610 exit 1
1612 let addoutline outline =
1613 match state.currently with
1614 | Outlining outlines ->
1615 state.currently <- Outlining (outline :: outlines)
1616 | Idle -> state.currently <- Outlining [outline]
1617 | Loading _
1618 | Tiling _ ->
1619 dolog "invalid outlining state";
1620 logcurrently state.currently
1622 match cl with
1623 | "clear" :: [] ->
1624 state.uioh#infochanged Pdim;
1625 state.pdims <- [];
1627 | "clearrects" :: [] ->
1628 state.rects <- state.rects1;
1629 G.postRedisplay "clearrects";
1631 | "continue" :: args :: [] ->
1632 let n = scan args "%u" (fun n -> n) in
1633 state.pagecount <- n;
1634 begin match state.currently with
1635 | Outlining l ->
1636 state.currently <- Idle;
1637 state.outlines <- Array.of_list (List.rev l)
1638 | Idle
1639 | Loading _
1640 | Tiling _ -> ()
1641 end;
1643 let cur, cmds = state.geomcmds in
1644 if emptystr cur
1645 then failwith "umpossible";
1647 begin match List.rev cmds with
1648 | [] ->
1649 state.geomcmds <- E.s, [];
1650 state.throttle <- None;
1651 represent ();
1652 | (s, f) :: rest ->
1653 f ();
1654 state.geomcmds <- s, List.rev rest;
1655 end;
1656 if conf.maxwait = None && not !wtmode
1657 then G.postRedisplay "continue";
1659 | "msg" :: args :: [] ->
1660 showtext ' ' args
1662 | "vmsg" :: args :: [] ->
1663 if conf.verbose
1664 then showtext ' ' args
1666 | "emsg" :: args :: [] ->
1667 Buffer.add_string state.errmsgs args;
1668 state.newerrmsgs <- true;
1669 G.postRedisplay "error message"
1671 | "progress" :: args :: [] ->
1672 let progress, text =
1673 scan args "%f %n"
1674 (fun f pos ->
1675 f, String.sub args pos (String.length args - pos))
1677 state.text <- text;
1678 state.progress <- progress;
1679 G.postRedisplay "progress"
1681 | "firstmatch" :: args :: [] ->
1682 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1683 scan args "%u %d %f %f %f %f %f %f %f %f"
1684 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1685 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1687 let xoff = float (xadjsb ()) in
1688 let x0 = x0 +. xoff
1689 and x1 = x1 +. xoff
1690 and x2 = x2 +. xoff
1691 and x3 = x3 +. xoff in
1692 let y = (getpagey pageno) + truncate y0 in
1693 if conf.zoom > 1.0
1694 then state.x <- truncate (xoff -. x0) + state.winw/2;
1695 addnav ();
1696 gotoy y;
1697 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1698 state.rects1 <- [pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)]
1700 | "match" :: args :: [] ->
1701 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1702 scan args "%u %d %f %f %f %f %f %f %f %f"
1703 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1704 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1706 let xoff = float (xadjsb ()) in
1707 let x0 = x0 +. xoff
1708 and x1 = x1 +. xoff
1709 and x2 = x2 +. xoff
1710 and x3 = x3 +. xoff in
1711 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1712 state.rects1 <-
1713 (pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1715 | "page" :: args :: [] ->
1716 let pageopaques, t = scan args "%s %f" (fun p t -> p, t) in
1717 let pageopaque = ~< pageopaques in
1718 begin match state.currently with
1719 | Loading (l, gen) ->
1720 vlog "page %d took %f sec" l.pageno t;
1721 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1722 begin match state.throttle with
1723 | None ->
1724 let preloadedpages =
1725 if conf.preload
1726 then preloadlayout state.x state.y state.winw state.winh
1727 else state.layout
1729 let evict () =
1730 let set =
1731 List.fold_left (fun s l -> IntSet.add l.pageno s)
1732 IntSet.empty preloadedpages
1734 let evictedpages =
1735 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1736 if not (IntSet.mem pageno set)
1737 then (
1738 wcmd "freepage %s" (~> opaque);
1739 key :: accu
1741 else accu
1742 ) state.pagemap []
1744 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1746 evict ();
1747 state.currently <- Idle;
1748 if gen = state.gen
1749 then (
1750 tilepage l.pageno pageopaque state.layout;
1751 load state.layout;
1752 load preloadedpages;
1753 let visible = pagevisible state.layout l.pageno in
1754 if visible
1755 then (
1756 match state.mode with
1757 | LinkNav (Ltnotready (pageno, dir)) ->
1758 if pageno = l.pageno
1759 then (
1760 let link =
1761 let ld =
1762 if dir = 0
1763 then LDfirstvisible (l.pagex, l.pagey, dir)
1764 else (
1765 if dir > 0 then LDfirst else LDlast
1768 findlink pageopaque ld
1770 match link with
1771 | Lnotfound -> ()
1772 | Lfound n ->
1773 showlinktype (getlink pageopaque n);
1774 state.mode <- LinkNav (Ltexact (l.pageno, n))
1776 | LinkNav (Ltgendir _)
1777 | LinkNav (Ltexact _)
1778 | View
1779 | Birdseye _
1780 | Textentry _ -> ()
1783 if visible && layoutready state.layout
1784 then (
1785 G.postRedisplay "page";
1789 | Some (layout, _, _) ->
1790 state.currently <- Idle;
1791 tilepage l.pageno pageopaque layout;
1792 load state.layout
1793 end;
1795 | Idle
1796 | Tiling _
1797 | Outlining _ ->
1798 dolog "Inconsistent loading state";
1799 logcurrently state.currently;
1800 exit 1
1803 | "tile" :: args :: [] ->
1804 let (x, y, opaques, size, t) =
1805 scan args "%u %u %s %u %f"
1806 (fun x y p size t -> (x, y, p, size, t))
1808 let opaque = ~< opaques in
1809 begin match state.currently with
1810 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1811 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1813 unmappbo opaque;
1814 if tilew != conf.tilew || tileh != conf.tileh
1815 then (
1816 wcmd "freetile %s" (~> opaque);
1817 state.currently <- Idle;
1818 load state.layout;
1820 else (
1821 puttileopaque l col row gen cs angle opaque size t;
1822 state.memused <- state.memused + size;
1823 state.uioh#infochanged Memused;
1824 gctiles ();
1825 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1826 opaque, size) state.tilelru;
1828 let layout =
1829 match state.throttle with
1830 | None -> state.layout
1831 | Some (layout, _, _) -> layout
1834 state.currently <- Idle;
1835 if gen = state.gen
1836 && conf.colorspace = cs
1837 && conf.angle = angle
1838 && tilevisible layout l.pageno x y
1839 then conttiling l.pageno pageopaque;
1841 begin match state.throttle with
1842 | None ->
1843 preload state.layout;
1844 if gen = state.gen
1845 && conf.colorspace = cs
1846 && conf.angle = angle
1847 && tilevisible state.layout l.pageno x y
1848 && (not !wtmode || layoutready state.layout)
1849 then G.postRedisplay "tile nothrottle";
1851 | Some (layout, y, _) ->
1852 let ready = layoutready layout in
1853 if ready
1854 then (
1855 state.y <- y;
1856 state.layout <- layout;
1857 state.throttle <- None;
1858 G.postRedisplay "throttle";
1860 else load layout;
1861 end;
1864 | Idle
1865 | Loading _
1866 | Outlining _ ->
1867 dolog "Inconsistent tiling state";
1868 logcurrently state.currently;
1869 exit 1
1872 | "pdim" :: args :: [] ->
1873 let (n, w, h, _) as pdim =
1874 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1876 let pdim =
1877 match conf.fitmodel with
1878 | FitWidth -> pdim
1879 | FitPage | FitProportional ->
1880 match conf.columns with
1881 | Csplit _ -> (n, w, h, 0)
1882 | Csingle _ | Cmulti _ -> pdim
1884 state.uioh#infochanged Pdim;
1885 state.pdims <- pdim :: state.pdims
1887 | "o" :: args :: [] ->
1888 let (l, n, t, h, pos) =
1889 scan args "%u %u %d %u %n"
1890 (fun l n t h pos -> l, n, t, h, pos)
1892 let s = String.sub args pos (String.length args - pos) in
1893 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
1895 | "ou" :: args :: [] ->
1896 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
1897 let s = String.sub args pos len in
1898 let pos2 = pos + len + 1 in
1899 let uri = String.sub args pos2 (String.length args - pos2) in
1900 addoutline (s, l, Ouri uri)
1902 | "on" :: args :: [] ->
1903 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
1904 let s = String.sub args pos (String.length args - pos) in
1905 addoutline (s, l, Onone)
1907 | "a" :: args :: [] ->
1908 let (n, l, t) =
1909 scan args "%u %d %d" (fun n l t -> n, l, t)
1911 state.reprf <- (fun () -> gotopagexy !wtmode n (float l) (float t))
1913 | "info" :: args :: [] ->
1914 let pos = nindex args '\t' in
1915 if pos >= 0 && String.sub args 0 pos = "Title"
1916 then (
1917 let s = String.sub args (pos+1) @@ String.length args - pos - 1 in
1918 conf.title <- s;
1919 Wsi.settitle s;
1921 state.docinfo <- (1, args) :: state.docinfo
1923 | "infoend" :: [] ->
1924 state.uioh#infochanged Docinfo;
1925 state.docinfo <- List.rev state.docinfo
1927 | "pass" :: l ->
1928 if l = "fail" :: []
1929 then Wsi.settitle "Wrong password";
1930 let password = getpassword () in
1931 if emptystr password
1932 then error "document is password protected"
1933 else opendoc state.path password
1934 | _ ->
1935 error "unknown cmd `%S'" cmds
1938 let onhist cb =
1939 let rc = cb.rc in
1940 let action = function
1941 | HCprev -> cbget cb ~-1
1942 | HCnext -> cbget cb 1
1943 | HCfirst -> cbget cb ~-(cb.rc)
1944 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1945 and cancel () = cb.rc <- rc
1946 in (action, cancel)
1949 let search pattern forward =
1950 match conf.columns with
1951 | Csplit _ -> impmsg "searching does not work properly in split columns mode"
1952 | Csingle _
1953 | Cmulti _ ->
1954 if nonemptystr pattern
1955 then
1956 let pn, py =
1957 match state.layout with
1958 | [] -> 0, 0
1959 | l :: _ ->
1960 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1962 wcmd "search %d %d %d %d,%s\000"
1963 (btod conf.icase) pn py (btod forward) pattern;
1966 let intentry text key =
1967 let c =
1968 if key >= 32 && key < 127
1969 then Char.chr key
1970 else '\000'
1972 match c with
1973 | '0' .. '9' ->
1974 let text = addchar text c in
1975 TEcont text
1977 | _ ->
1978 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1979 TEcont text
1982 let linknact f s =
1983 if nonemptystr s
1984 then (
1985 let n =
1986 let l = String.length s in
1987 let rec loop pos n = if pos = l then n else
1988 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
1989 loop (pos+1) (n*26 + m)
1990 in loop 0 0
1992 let rec loop n = function
1993 | [] -> ()
1994 | l :: rest ->
1995 match getopaque l.pageno with
1996 | None -> loop n rest
1997 | Some opaque ->
1998 let m = getlinkcount opaque in
1999 if n < m
2000 then (
2001 let under = getlink opaque n in
2002 f under
2004 else loop (n-m) rest
2006 loop n state.layout;
2010 let linknentry text key =
2011 let c =
2012 if key >= 32 && key < 127
2013 then Char.chr key
2014 else '\000'
2016 match c with
2017 | 'a' .. 'z' ->
2018 let text = addchar text c in
2019 linknact (fun under -> state.text <- undertext ~nopath:true under) text;
2020 TEcont text
2022 | _ ->
2023 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2024 TEcont text
2027 let textentry text key =
2028 if key land 0xff00 = 0xff00
2029 then TEcont text
2030 else TEcont (text ^ toutf8 key)
2033 let reqlayout angle fitmodel =
2034 match state.throttle with
2035 | None ->
2036 if nogeomcmds state.geomcmds
2037 then state.anchor <- getanchor ();
2038 conf.angle <- angle mod 360;
2039 if conf.angle != 0
2040 then (
2041 match state.mode with
2042 | LinkNav _ -> state.mode <- View
2043 | Birdseye _
2044 | Textentry _
2045 | View -> ()
2047 conf.fitmodel <- fitmodel;
2048 invalidate "reqlayout"
2049 (fun () ->
2050 wcmd "reqlayout %d %d %d"
2051 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
2053 | _ -> ()
2056 let settrim trimmargins trimfuzz =
2057 if nogeomcmds state.geomcmds
2058 then state.anchor <- getanchor ();
2059 conf.trimmargins <- trimmargins;
2060 conf.trimfuzz <- trimfuzz;
2061 let x0, y0, x1, y1 = trimfuzz in
2062 invalidate "settrim"
2063 (fun () ->
2064 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2065 flushpages ();
2068 let setzoom zoom =
2069 match state.throttle with
2070 | None ->
2071 let zoom = max 0.0001 zoom in
2072 if zoom <> conf.zoom
2073 then (
2074 state.prevzoom <- (conf.zoom, state.x);
2075 conf.zoom <- zoom;
2076 reshape state.winw state.winh;
2077 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2080 | Some (layout, y, started) ->
2081 let time =
2082 match conf.maxwait with
2083 | None -> 0.0
2084 | Some t -> t
2086 let dt = now () -. started in
2087 if dt > time
2088 then (
2089 state.y <- y;
2090 load layout;
2094 let setcolumns mode columns coverA coverB =
2095 state.prevcolumns <- Some (conf.columns, conf.zoom);
2096 if columns < 0
2097 then (
2098 if isbirdseye mode
2099 then impmsg "split mode doesn't work in bird's eye"
2100 else (
2101 conf.columns <- Csplit (-columns, E.a);
2102 state.x <- 0;
2103 conf.zoom <- 1.0;
2106 else (
2107 if columns < 2
2108 then (
2109 conf.columns <- Csingle E.a;
2110 state.x <- 0;
2111 setzoom 1.0;
2113 else (
2114 conf.columns <- Cmulti ((columns, coverA, coverB), E.a);
2115 conf.zoom <- 1.0;
2118 reshape state.winw state.winh;
2121 let resetmstate () =
2122 state.mstate <- Mnone;
2123 Wsi.setcursor Wsi.CURSOR_INHERIT;
2126 let enterbirdseye () =
2127 let zoom = float conf.thumbw /. float state.winw in
2128 let birdseyepageno =
2129 let cy = state.winh / 2 in
2130 let fold = function
2131 | [] -> 0
2132 | l :: rest ->
2133 let rec fold best = function
2134 | [] -> best.pageno
2135 | l :: rest ->
2136 let d = cy - (l.pagedispy + l.pagevh/2)
2137 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2138 if abs d < abs dbest
2139 then fold l rest
2140 else best.pageno
2141 in fold l rest
2143 fold state.layout
2145 state.mode <- Birdseye (
2146 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2148 resetmstate ();
2149 conf.zoom <- zoom;
2150 conf.presentation <- false;
2151 conf.interpagespace <- 10;
2152 conf.hlinks <- false;
2153 conf.fitmodel <- FitPage;
2154 state.x <- 0;
2155 conf.maxwait <- None;
2156 conf.columns <- (
2157 match conf.beyecolumns with
2158 | Some c ->
2159 conf.zoom <- 1.0;
2160 Cmulti ((c, 0, 0), E.a)
2161 | None -> Csingle E.a
2163 if conf.verbose
2164 then
2165 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2166 (100.0*.zoom)
2167 else
2168 state.text <- E.s
2170 reshape state.winw state.winh;
2173 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2174 state.mode <- View;
2175 conf.zoom <- c.zoom;
2176 conf.presentation <- c.presentation;
2177 conf.interpagespace <- c.interpagespace;
2178 conf.maxwait <- c.maxwait;
2179 conf.hlinks <- c.hlinks;
2180 conf.fitmodel <- c.fitmodel;
2181 conf.beyecolumns <- (
2182 match conf.columns with
2183 | Cmulti ((c, _, _), _) -> Some c
2184 | Csingle _ -> None
2185 | Csplit _ -> failwith "leaving bird's eye split mode"
2187 conf.columns <- (
2188 match c.columns with
2189 | Cmulti (c, _) -> Cmulti (c, E.a)
2190 | Csingle _ -> Csingle E.a
2191 | Csplit (c, _) -> Csplit (c, E.a)
2193 if conf.verbose
2194 then
2195 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2196 (100.0*.conf.zoom)
2198 reshape state.winw state.winh;
2199 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2200 state.x <- leftx;
2203 let togglebirdseye () =
2204 match state.mode with
2205 | Birdseye vals -> leavebirdseye vals true
2206 | View -> enterbirdseye ()
2207 | Textentry _
2208 | LinkNav _ -> ()
2211 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2212 let pageno = max 0 (pageno - incr) in
2213 let rec loop = function
2214 | [] -> gotopage1 pageno 0
2215 | l :: _ when l.pageno = pageno ->
2216 if l.pagedispy >= 0 && l.pagey = 0
2217 then G.postRedisplay "upbirdseye"
2218 else gotopage1 pageno 0
2219 | _ :: rest -> loop rest
2221 loop state.layout;
2222 state.text <- E.s;
2223 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2226 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2227 let pageno = min (state.pagecount - 1) (pageno + incr) in
2228 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2229 let rec loop = function
2230 | [] ->
2231 let y, h = getpageyh pageno in
2232 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2233 gotoy (clamp dy)
2234 | l :: _ when l.pageno = pageno ->
2235 if l.pagevh != l.pageh
2236 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2237 else G.postRedisplay "downbirdseye"
2238 | _ :: rest -> loop rest
2240 loop state.layout;
2241 state.text <- E.s;
2244 let optentry mode _ key =
2245 let btos b = if b then "on" else "off" in
2246 if key >= 32 && key < 127
2247 then
2248 let c = Char.chr key in
2249 match c with
2250 | 's' ->
2251 let ondone s =
2252 try conf.scrollstep <- int_of_string s with exc ->
2253 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exc
2255 TEswitch ("scroll step: ", E.s, None, intentry, ondone, true)
2257 | 'A' ->
2258 let ondone s =
2260 conf.autoscrollstep <- boundastep state.winh (int_of_string s);
2261 if state.autoscroll <> None
2262 then state.autoscroll <- Some conf.autoscrollstep
2263 with exc ->
2264 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exc
2266 TEswitch ("auto scroll step: ", E.s, None, intentry, ondone, true)
2268 | 'C' ->
2269 let ondone s =
2271 let n, a, b = multicolumns_of_string s in
2272 setcolumns mode n a b;
2273 with exc ->
2274 state.text <- Printf.sprintf "bad columns `%s': %s" s @@ exntos exc
2276 TEswitch ("columns: ", E.s, None, textentry, ondone, true)
2278 | 'Z' ->
2279 let ondone s =
2281 let zoom = float (int_of_string s) /. 100.0 in
2282 setzoom zoom
2283 with exc ->
2284 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exc
2286 TEswitch ("zoom: ", E.s, None, intentry, ondone, true)
2288 | 't' ->
2289 let ondone s =
2291 conf.thumbw <- bound (int_of_string s) 2 4096;
2292 state.text <-
2293 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2294 begin match mode with
2295 | Birdseye beye ->
2296 leavebirdseye beye false;
2297 enterbirdseye ();
2298 | Textentry _
2299 | View
2300 | LinkNav _ -> ();
2302 with exc ->
2303 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exc
2305 TEswitch ("thumbnail width: ", E.s, None, intentry, ondone, true)
2307 | 'R' ->
2308 let ondone s =
2309 match try
2310 Some (int_of_string s)
2311 with exc ->
2312 state.text <-
2313 Printf.sprintf "bad integer `%s': %s" s @@ exntos exc;
2314 None
2315 with
2316 | Some angle -> reqlayout angle conf.fitmodel
2317 | None -> ()
2319 TEswitch ("rotation: ", E.s, None, intentry, ondone, true)
2321 | 'i' ->
2322 conf.icase <- not conf.icase;
2323 TEdone ("case insensitive search " ^ (btos conf.icase))
2325 | 'p' ->
2326 conf.preload <- not conf.preload;
2327 gotoy state.y;
2328 TEdone ("preload " ^ (btos conf.preload))
2330 | 'v' ->
2331 conf.verbose <- not conf.verbose;
2332 TEdone ("verbose " ^ (btos conf.verbose))
2334 | 'd' ->
2335 conf.debug <- not conf.debug;
2336 TEdone ("debug " ^ (btos conf.debug))
2338 | 'h' ->
2339 conf.maxhfit <- not conf.maxhfit;
2340 state.maxy <- calcheight ();
2341 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2343 | 'c' ->
2344 conf.crophack <- not conf.crophack;
2345 TEdone ("crophack " ^ btos conf.crophack)
2347 | 'a' ->
2348 let s =
2349 match conf.maxwait with
2350 | None ->
2351 conf.maxwait <- Some infinity;
2352 "always wait for page to complete"
2353 | Some _ ->
2354 conf.maxwait <- None;
2355 "show placeholder if page is not ready"
2357 TEdone s
2359 | 'f' ->
2360 conf.underinfo <- not conf.underinfo;
2361 TEdone ("underinfo " ^ btos conf.underinfo)
2363 | 'P' ->
2364 conf.savebmarks <- not conf.savebmarks;
2365 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2367 | 'S' ->
2368 let ondone s =
2370 let pageno, py =
2371 match state.layout with
2372 | [] -> 0, 0
2373 | l :: _ ->
2374 l.pageno, l.pagey
2376 conf.interpagespace <- int_of_string s;
2377 docolumns conf.columns;
2378 state.maxy <- calcheight ();
2379 let y = getpagey pageno in
2380 gotoy (y + py)
2381 with exc ->
2382 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exc
2384 TEswitch ("vertical margin: ", E.s, None, intentry, ondone, true)
2386 | 'l' ->
2387 let fm =
2388 match conf.fitmodel with
2389 | FitProportional -> FitWidth
2390 | FitWidth | FitPage -> FitProportional
2392 reqlayout conf.angle fm;
2393 TEdone ("proportional display " ^ btos (fm == FitProportional))
2395 | 'T' ->
2396 settrim (not conf.trimmargins) conf.trimfuzz;
2397 TEdone ("trim margins " ^ btos conf.trimmargins)
2399 | 'I' ->
2400 conf.invert <- not conf.invert;
2401 TEdone ("invert colors " ^ btos conf.invert)
2403 | 'x' ->
2404 let ondone s =
2405 cbput state.hists.sel s;
2406 conf.selcmd <- s;
2408 TEswitch ("selection command: ", E.s, Some (onhist state.hists.sel),
2409 textentry, ondone, true)
2411 | 'M' ->
2412 if conf.pax == None
2413 then conf.pax <- Some (ref (0.0, 0, 0))
2414 else conf.pax <- None;
2415 TEdone ("PAX " ^ btos (conf.pax != None))
2417 | _ ->
2418 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2419 TEstop
2420 else
2421 TEcont state.text
2424 class type lvsource = object
2425 method getitemcount : int
2426 method getitem : int -> (string * int)
2427 method hasaction : int -> bool
2428 method exit :
2429 uioh:uioh ->
2430 cancel:bool ->
2431 active:int ->
2432 first:int ->
2433 pan:int ->
2434 uioh option
2435 method getactive : int
2436 method getfirst : int
2437 method getpan : int
2438 method getminfo : (int * int) array
2439 end;;
2441 class virtual lvsourcebase = object
2442 val mutable m_active = 0
2443 val mutable m_first = 0
2444 val mutable m_pan = 0
2445 method getactive = m_active
2446 method getfirst = m_first
2447 method getpan = m_pan
2448 method getminfo : (int * int) array = E.a
2449 end;;
2451 let textentrykeyboard
2452 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2453 state.text <- E.s;
2454 let key =
2455 if key >= 0xffb0 && key <= 0xffb9
2456 then key - 0xffb0 + 48 else key
2458 let enttext te =
2459 state.mode <- Textentry (te, onleave);
2460 enttext ();
2461 G.postRedisplay "textentrykeyboard enttext";
2463 let histaction cmd =
2464 match opthist with
2465 | None -> ()
2466 | Some (action, _) ->
2467 state.mode <- Textentry (
2468 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
2470 G.postRedisplay "textentry histaction"
2472 match key with
2473 | @backspace ->
2474 if emptystr text && cancelonempty
2475 then (
2476 onleave Cancel;
2477 G.postRedisplay "textentrykeyboard after cancel";
2479 else
2480 let s = withoutlastutf8 text in
2481 enttext (c, s, opthist, onkey, ondone, cancelonempty)
2483 | @enter | @kpenter ->
2484 ondone text;
2485 onleave Confirm;
2486 G.postRedisplay "textentrykeyboard after confirm"
2488 | @up | @kpup -> histaction HCprev
2489 | @down | @kpdown -> histaction HCnext
2490 | @home | @kphome -> histaction HCfirst
2491 | @jend | @kpend -> histaction HClast
2493 | @escape ->
2494 if emptystr text
2495 then (
2496 begin match opthist with
2497 | None -> ()
2498 | Some (_, onhistcancel) -> onhistcancel ()
2499 end;
2500 onleave Cancel;
2501 state.text <- E.s;
2502 G.postRedisplay "textentrykeyboard after cancel2"
2504 else (
2505 enttext (c, E.s, opthist, onkey, ondone, cancelonempty)
2508 | @delete | @kpdelete -> ()
2510 | _ when key != 0
2511 && key land 0xff00 != 0xff00 (* keyboard *)
2512 && key land 0xfe00 != 0xfe00 (* xkb *)
2513 && key land 0xfd00 != 0xfd00 (* 3270 *)
2515 begin match onkey text key with
2516 | TEdone text ->
2517 ondone text;
2518 onleave Confirm;
2519 G.postRedisplay "textentrykeyboard after confirm2";
2521 | TEcont text ->
2522 enttext (c, text, opthist, onkey, ondone, cancelonempty);
2524 | TEstop ->
2525 onleave Cancel;
2526 G.postRedisplay "textentrykeyboard after cancel3"
2528 | TEswitch te ->
2529 state.mode <- Textentry (te, onleave);
2530 G.postRedisplay "textentrykeyboard switch";
2531 end;
2533 | _ ->
2534 vlog "unhandled key %s" (Wsi.keyname key)
2537 let firstof first active =
2538 if first > active || abs (first - active) > fstate.maxrows - 1
2539 then max 0 (active - (fstate.maxrows/2))
2540 else first
2543 let calcfirst first active =
2544 if active > first
2545 then
2546 let rows = active - first in
2547 if rows > fstate.maxrows then active - fstate.maxrows else first
2548 else active
2551 let scrollph y maxy =
2552 let sh = float (maxy + state.winh) /. float state.winh in
2553 let sh = float state.winh /. sh in
2554 let sh = max sh (float conf.scrollh) in
2556 let percent = float y /. float maxy in
2557 let position = (float state.winh -. sh) *. percent in
2559 let position =
2560 if position +. sh > float state.winh
2561 then float state.winh -. sh
2562 else position
2564 position, sh;
2567 let adderrmsg src msg =
2568 Buffer.add_string state.errmsgs msg;
2569 state.newerrmsgs <- true;
2570 G.postRedisplay src
2573 let adderrfmt src fmt =
2574 Format.ksprintf (fun s -> adderrmsg src s) fmt;
2577 let coe s = (s :> uioh);;
2579 class listview ~zebra ~helpmode ~(source:lvsource) ~trusted ~modehash =
2580 object (self)
2581 val m_pan = source#getpan
2582 val m_first = source#getfirst
2583 val m_active = source#getactive
2584 val m_qsearch = E.s
2585 val m_prev_uioh = state.uioh
2587 method private elemunder y =
2588 if y < 0
2589 then None
2590 else
2591 let n = y / (fstate.fontsize+1) in
2592 if m_first + n < source#getitemcount
2593 then (
2594 if source#hasaction (m_first + n)
2595 then Some (m_first + n)
2596 else None
2598 else None
2600 method display =
2601 Gl.enable `blend;
2602 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
2603 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2604 filledrect 0. 0. (float state.winw) (float state.winh);
2605 GlDraw.color (1., 1., 1.);
2606 Gl.enable `texture_2d;
2607 let fs = fstate.fontsize in
2608 let nfs = fs + 1 in
2609 let hw = (wadjsb () + xadjsb () + state.winw)/3 in
2610 let ww = fstate.wwidth in
2611 let tabw = 17.0*.ww in
2612 let itemcount = source#getitemcount in
2613 let minfo = source#getminfo in
2614 let x0, x1 =
2615 if conf.leftscroll
2616 then float (xadjsb ()), float (state.winw - 1)
2617 else 0.0, float (state.winw - conf.scrollbw - 1)
2619 let xadj = xadjsb () in
2620 let rec loop row =
2621 if (row - m_first) > fstate.maxrows
2622 then ()
2623 else (
2624 if row >= 0 && row < itemcount
2625 then (
2626 let (s, level) = source#getitem row in
2627 let y = (row - m_first) * nfs in
2628 let x =
2629 (if conf.leftscroll then float xadj else 5.0)
2630 +. (float (level + m_pan)) *. ww in
2631 if helpmode
2632 then GlDraw.color
2633 (let c = if row land 1 = 0 then 1.0 else 0.92 in (c,c,c));
2635 if row = m_active
2636 then (
2637 Gl.disable `texture_2d;
2638 let alpha = if source#hasaction row then 0.9 else 0.3 in
2639 GlDraw.color (1., 1., 1.) ~alpha;
2640 linerect (x0 +. 1.) (float (y + 1)) (x1) (float (y + fs + 3));
2641 Gl.enable `texture_2d;
2643 let c =
2644 if zebra && row land 1 = 1
2645 then 0.8
2646 else 1.0
2648 GlDraw.color (c,c,c);
2649 let drawtabularstring s =
2650 let drawstr x s =
2651 let x' = truncate (x0 +. x) in
2652 let pos = nindex s '\000' in
2653 if pos = -1
2654 then drawstring1 fs x' (y+nfs) s
2655 else
2656 let s1 = String.sub s 0 pos
2657 and s2 = String.sub s (pos+1) (String.length s - pos - 1) in
2658 let rec e s =
2659 if emptystr s
2660 then s
2661 else
2662 let s' = withoutlastutf8 s in
2663 let s = s' ^ "@Uellipsis" in
2664 let w = measurestr fs s in
2665 if float x' +. w +. ww < float (hw + x')
2666 then s
2667 else e s'
2669 let s1 =
2670 if float x' +. ww +. measurestr fs s1 > float (hw + x')
2671 then e s1
2672 else s1
2674 ignore (drawstring1 fs x' (y+nfs) s1);
2675 drawstring1 fs (hw + x') (y+nfs) s2
2677 if trusted
2678 then
2679 let x = if helpmode && row > 0 then x +. ww else x in
2680 let tabpos = nindex s '\t' in
2681 if tabpos > 0
2682 then
2683 let len = String.length s - tabpos - 1 in
2684 let s1 = String.sub s 0 tabpos
2685 and s2 = String.sub s (tabpos + 1) len in
2686 let nx = drawstr x s1 in
2687 let sw = nx -. x in
2688 let x = x +. (max tabw sw) in
2689 drawstr x s2
2690 else
2691 let len = String.length s - 2 in
2692 if len > 0 && s.[0] = '\xc2' && s.[1] = '\xb7'
2693 then
2694 let s = String.sub s 2 len in
2695 let x = if not helpmode then x +. ww else x in
2696 GlDraw.color (1.2, 1.2, 1.2);
2697 let vinc = drawstring1 (fs+fs/4)
2698 (truncate (x -. ww)) (y+nfs) s in
2699 GlDraw.color (1., 1., 1.);
2700 vinc +. (float fs *. 0.8)
2701 else
2702 drawstr x s
2703 else
2704 drawstr x s
2706 ignore (drawtabularstring s);
2707 loop (row+1)
2711 loop m_first;
2712 GlDraw.color (1.0, 1.0, 1.0) ~alpha:0.5;
2713 let xadj = float (xadjsb () + 5) in
2714 let rec loop row =
2715 if (row - m_first) > fstate.maxrows
2716 then ()
2717 else (
2718 if row >= 0 && row < itemcount
2719 then (
2720 let (s, level) = source#getitem row in
2721 let pos0 = nindex s '\000' in
2722 let y = (row - m_first) * nfs in
2723 let x = float (level + m_pan) *. ww in
2724 let (first, last) = minfo.(row) in
2725 let prefix =
2726 if pos0 > 0 && first > pos0
2727 then String.sub s (pos0+1) (first-pos0-1)
2728 else String.sub s 0 first
2730 let suffix = String.sub s first (last - first) in
2731 let w1 = measurestr fstate.fontsize prefix in
2732 let w2 = measurestr fstate.fontsize suffix in
2733 let x = x +. if conf.leftscroll then xadj else 5.0 in
2734 let x = if pos0 > 0 && first > pos0 then x +. float hw else x in
2735 let x0 = x +. w1
2736 and y0 = float (y+2) in
2737 let x1 = x0 +. w2
2738 and y1 = float (y+fs+3) in
2739 filledrect x0 y0 x1 y1;
2740 loop (row+1)
2744 Gl.disable `texture_2d;
2745 if Array.length minfo > 0 then loop m_first;
2746 Gl.disable `blend;
2748 method updownlevel incr =
2749 let len = source#getitemcount in
2750 let curlevel =
2751 if m_active >= 0 && m_active < len
2752 then snd (source#getitem m_active)
2753 else -1
2755 let rec flow i =
2756 if i = len then i-1 else if i = -1 then 0 else
2757 let _, l = source#getitem i in
2758 if l != curlevel then i else flow (i+incr)
2760 let active = flow m_active in
2761 let first = calcfirst m_first active in
2762 G.postRedisplay "outline updownlevel";
2763 {< m_active = active; m_first = first >}
2765 method private key1 key mask =
2766 let set1 active first qsearch =
2767 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2769 let search active pattern incr =
2770 let active = if active = -1 then m_first else active in
2771 let dosearch re =
2772 let rec loop n =
2773 if n >= 0 && n < source#getitemcount
2774 then (
2775 let s, _ = source#getitem n in
2776 match Str.search_forward re s 0 with
2777 | (exception Not_found) -> loop (n + incr)
2778 | _ -> Some n
2780 else None
2782 loop active
2784 let qpat = Str.quote pattern in
2785 match Str.regexp_case_fold qpat with
2786 | s -> dosearch s
2787 | exception exn ->
2788 adderrfmt "listview key1" "regexp_case_fold for `%S' failed: %S\n"
2789 qpat @@ Printexc.to_string exn;
2790 None
2792 let itemcount = source#getitemcount in
2793 let find start incr =
2794 let rec find i =
2795 if i = -1 || i = itemcount
2796 then -1
2797 else (
2798 if source#hasaction i
2799 then i
2800 else find (i + incr)
2803 find start
2805 let set active first =
2806 let first = bound first 0 (itemcount - fstate.maxrows) in
2807 state.text <- E.s;
2808 coe {< m_active = active; m_first = first; m_qsearch = E.s >}
2810 let navigate incr =
2811 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2812 let active, first =
2813 let incr1 = if incr > 0 then 1 else -1 in
2814 if isvisible m_first m_active
2815 then
2816 let next =
2817 let next = m_active + incr in
2818 let next =
2819 if next < 0 || next >= itemcount
2820 then -1
2821 else find next incr1
2823 if abs (m_active - next) > fstate.maxrows
2824 then -1
2825 else next
2827 if next = -1
2828 then
2829 let first = m_first + incr in
2830 let first = bound first 0 (itemcount - fstate.maxrows) in
2831 let next =
2832 let next = m_active + incr in
2833 let next = bound next 0 (itemcount - 1) in
2834 find next ~-incr1
2836 let active =
2837 if next = -1
2838 then m_active
2839 else (
2840 if isvisible first next
2841 then next
2842 else m_active
2845 active, first
2846 else
2847 let first = min next m_first in
2848 let first =
2849 if abs (next - first) > fstate.maxrows
2850 then first + incr
2851 else first
2853 next, first
2854 else
2855 let first = m_first + incr in
2856 let first = bound first 0 (itemcount - 1) in
2857 let active =
2858 let next = m_active + incr in
2859 let next = bound next 0 (itemcount - 1) in
2860 let next = find next incr1 in
2861 let active =
2862 if next = -1 || abs (m_active - first) > fstate.maxrows
2863 then (
2864 let active = if m_active = -1 then next else m_active in
2865 active
2867 else next
2869 if isvisible first active
2870 then active
2871 else -1
2873 active, first
2875 G.postRedisplay "listview navigate";
2876 set active first;
2878 match key with
2879 | (@r|@s) when Wsi.withctrl mask ->
2880 let incr = if key = @r then -1 else 1 in
2881 let active, first =
2882 match search (m_active + incr) m_qsearch incr with
2883 | None ->
2884 state.text <- m_qsearch ^ " [not found]";
2885 m_active, m_first
2886 | Some active ->
2887 state.text <- m_qsearch;
2888 active, firstof m_first active
2890 G.postRedisplay "listview ctrl-r/s";
2891 set1 active first m_qsearch;
2893 | @insert when Wsi.withctrl mask ->
2894 if m_active >= 0 && m_active < source#getitemcount
2895 then (
2896 let s, _ = source#getitem m_active in
2897 selstring s;
2899 coe self
2901 | @backspace ->
2902 if emptystr m_qsearch
2903 then coe self
2904 else (
2905 let qsearch = withoutlastutf8 m_qsearch in
2906 if emptystr qsearch
2907 then (
2908 state.text <- E.s;
2909 G.postRedisplay "listview empty qsearch";
2910 set1 m_active m_first E.s;
2912 else
2913 let active, first =
2914 match search m_active qsearch ~-1 with
2915 | None ->
2916 state.text <- qsearch ^ " [not found]";
2917 m_active, m_first
2918 | Some active ->
2919 state.text <- qsearch;
2920 active, firstof m_first active
2922 G.postRedisplay "listview backspace qsearch";
2923 set1 active first qsearch
2926 | key when (key != 0 && key land 0xff00 != 0xff00) ->
2927 let pattern = m_qsearch ^ toutf8 key in
2928 let active, first =
2929 match search m_active pattern 1 with
2930 | None ->
2931 state.text <- pattern ^ " [not found]";
2932 m_active, m_first
2933 | Some active ->
2934 state.text <- pattern;
2935 active, firstof m_first active
2937 G.postRedisplay "listview qsearch add";
2938 set1 active first pattern;
2940 | @escape ->
2941 state.text <- E.s;
2942 if emptystr m_qsearch
2943 then (
2944 G.postRedisplay "list view escape";
2945 let mx, my = state.mpos in
2946 updateunder mx my;
2947 begin
2948 match
2949 source#exit ~uioh:(coe self)
2950 ~cancel:true ~active:m_active ~first:m_first ~pan:m_pan
2951 with
2952 | None -> m_prev_uioh
2953 | Some uioh -> uioh
2956 else (
2957 G.postRedisplay "list view kill qsearch";
2958 coe {< m_qsearch = E.s >}
2961 | @enter | @kpenter ->
2962 state.text <- E.s;
2963 let self = {< m_qsearch = E.s >} in
2964 let opt =
2965 G.postRedisplay "listview enter";
2966 if m_active >= 0 && m_active < source#getitemcount
2967 then (
2968 source#exit ~uioh:(coe self) ~cancel:false
2969 ~active:m_active ~first:m_first ~pan:m_pan;
2971 else (
2972 source#exit ~uioh:(coe self) ~cancel:true
2973 ~active:m_active ~first:m_first ~pan:m_pan;
2976 begin match opt with
2977 | None -> m_prev_uioh
2978 | Some uioh -> uioh
2981 | @delete | @kpdelete ->
2982 coe self
2984 | @up | @kpup -> navigate ~-1
2985 | @down | @kpdown -> navigate 1
2986 | @prior | @kpprior -> navigate ~-(fstate.maxrows)
2987 | @next | @kpnext -> navigate fstate.maxrows
2989 | @right | @kpright ->
2990 state.text <- E.s;
2991 G.postRedisplay "listview right";
2992 coe {< m_pan = m_pan - 1 >}
2994 | @left | @kpleft ->
2995 state.text <- E.s;
2996 G.postRedisplay "listview left";
2997 coe {< m_pan = m_pan + 1 >}
2999 | @home | @kphome ->
3000 let active = find 0 1 in
3001 G.postRedisplay "listview home";
3002 set active 0;
3004 | @jend | @kpend ->
3005 let first = max 0 (itemcount - fstate.maxrows) in
3006 let active = find (itemcount - 1) ~-1 in
3007 G.postRedisplay "listview end";
3008 set active first;
3010 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3011 coe self
3013 | _ ->
3014 dolog "listview unknown key %#x" key; coe self
3016 method key key mask =
3017 match state.mode with
3018 | Textentry te -> textentrykeyboard key mask te; coe self
3019 | Birdseye _
3020 | View
3021 | LinkNav _ -> self#key1 key mask
3023 method button button down x y _ =
3024 let opt =
3025 match button with
3026 | 1 when vscrollhit x ->
3027 G.postRedisplay "listview scroll";
3028 if down
3029 then
3030 let _, position, sh = self#scrollph in
3031 if y > truncate position && y < truncate (position +. sh)
3032 then (
3033 state.mstate <- Mscrolly;
3034 Some (coe self)
3036 else
3037 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3038 let first = truncate (s *. float source#getitemcount) in
3039 let first = min source#getitemcount first in
3040 Some (coe {< m_first = first; m_active = first >})
3041 else (
3042 state.mstate <- Mnone;
3043 Some (coe self);
3045 | 1 when down ->
3046 begin match self#elemunder y with
3047 | Some n ->
3048 G.postRedisplay "listview click";
3049 source#exit ~uioh:(coe {< m_active = n >})
3050 ~cancel:false ~active:n ~first:m_first ~pan:m_pan
3051 | _ ->
3052 Some (coe self)
3054 | n when (n == 4 || n == 5) && not down ->
3055 let len = source#getitemcount in
3056 let first =
3057 if n = 5 && m_first + fstate.maxrows >= len
3058 then
3059 m_first
3060 else
3061 let first = m_first + (if n == 4 then -1 else 1) in
3062 bound first 0 (len - 1)
3064 G.postRedisplay "listview wheel";
3065 Some (coe {< m_first = first >})
3066 | n when (n = 6 || n = 7) && not down ->
3067 let inc = if n = 7 then -1 else 1 in
3068 G.postRedisplay "listview hwheel";
3069 Some (coe {< m_pan = m_pan + inc >})
3070 | _ ->
3071 Some (coe self)
3073 match opt with
3074 | None -> m_prev_uioh
3075 | Some uioh -> uioh
3077 method multiclick _ x y = self#button 1 true x y
3079 method motion _ y =
3080 match state.mstate with
3081 | Mscrolly ->
3082 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3083 let first = truncate (s *. float source#getitemcount) in
3084 let first = min source#getitemcount first in
3085 G.postRedisplay "listview motion";
3086 coe {< m_first = first; m_active = first >}
3087 | Msel _
3088 | Mpan _
3089 | Mscrollx
3090 | Mzoom _
3091 | Mzoomrect _
3092 | Mnone -> coe self
3094 method pmotion x y =
3095 if x < state.winw - conf.scrollbw
3096 then
3097 let n =
3098 match self#elemunder y with
3099 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3100 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3102 let o =
3103 if n != m_active
3104 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3105 else self
3107 coe o
3108 else (
3109 Wsi.setcursor Wsi.CURSOR_INHERIT;
3110 coe self
3113 method infochanged _ = ()
3115 method scrollpw = (0, 0.0, 0.0)
3116 method scrollph =
3117 let nfs = fstate.fontsize + 1 in
3118 let y = m_first * nfs in
3119 let itemcount = source#getitemcount in
3120 let maxi = max 0 (itemcount - fstate.maxrows) in
3121 let maxy = maxi * nfs in
3122 let p, h = scrollph y maxy in
3123 conf.scrollbw, p, h
3125 method modehash = modehash
3126 method eformsgs = false
3127 method alwaysscrolly = true
3128 end;;
3130 class outlinelistview ~zebra ~source =
3131 let settext autonarrow s =
3132 if autonarrow
3133 then
3134 let ss = source#statestr in
3135 state.text <-
3136 if emptystr ss
3137 then "[" ^ s ^ "]"
3138 else "{" ^ ss ^ "} [" ^ s ^ "]"
3139 else state.text <- s
3141 object (self)
3142 inherit listview
3143 ~zebra
3144 ~helpmode:false
3145 ~source:(source :> lvsource)
3146 ~trusted:false
3147 ~modehash:(findkeyhash conf "outline")
3148 as super
3150 val m_autonarrow = false
3152 method! key key mask =
3153 let maxrows =
3154 if emptystr state.text
3155 then fstate.maxrows
3156 else fstate.maxrows - 2
3158 let calcfirst first active =
3159 if active > first
3160 then
3161 let rows = active - first in
3162 if rows > maxrows then active - maxrows else first
3163 else active
3165 let navigate incr =
3166 let active = m_active + incr in
3167 let active = bound active 0 (source#getitemcount - 1) in
3168 let first = calcfirst m_first active in
3169 G.postRedisplay "outline navigate";
3170 coe {< m_active = active; m_first = first >}
3172 let navscroll first =
3173 let active =
3174 let dist = m_active - first in
3175 if dist < 0
3176 then first
3177 else (
3178 if dist < maxrows
3179 then m_active
3180 else first + maxrows
3183 G.postRedisplay "outline navscroll";
3184 coe {< m_first = first; m_active = active >}
3186 let ctrl = Wsi.withctrl mask in
3187 match key with
3188 | @a when ctrl ->
3189 let text =
3190 if m_autonarrow
3191 then (source#denarrow; E.s)
3192 else (
3193 let pattern = source#renarrow in
3194 if nonemptystr m_qsearch
3195 then (source#narrow m_qsearch; m_qsearch)
3196 else pattern
3199 settext (not m_autonarrow) text;
3200 G.postRedisplay "toggle auto narrowing";
3201 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
3203 | @slash when emptystr m_qsearch && not m_autonarrow ->
3204 settext true E.s;
3205 G.postRedisplay "toggle auto narrowing";
3206 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
3208 | @n when ctrl ->
3209 source#narrow m_qsearch;
3210 if not m_autonarrow
3211 then source#add_narrow_pattern m_qsearch;
3212 G.postRedisplay "outline ctrl-n";
3213 coe {< m_first = 0; m_active = 0 >}
3215 | @S when ctrl ->
3216 let active = source#calcactive (getanchor ()) in
3217 let first = firstof m_first active in
3218 G.postRedisplay "outline ctrl-s";
3219 coe {< m_first = first; m_active = active >}
3221 | @u when ctrl ->
3222 G.postRedisplay "outline ctrl-u";
3223 if m_autonarrow && nonemptystr m_qsearch
3224 then (
3225 ignore (source#renarrow);
3226 settext m_autonarrow E.s;
3227 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3229 else (
3230 source#del_narrow_pattern;
3231 let pattern = source#renarrow in
3232 let text =
3233 if emptystr pattern then E.s else "Narrowed to " ^ pattern
3235 settext m_autonarrow text;
3236 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3239 | @l when ctrl ->
3240 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3241 G.postRedisplay "outline ctrl-l";
3242 coe {< m_first = first >}
3244 | @tab when m_autonarrow ->
3245 if nonemptystr m_qsearch
3246 then (
3247 G.postRedisplay "outline list view tab";
3248 source#add_narrow_pattern m_qsearch;
3249 settext true E.s;
3250 coe {< m_qsearch = E.s >}
3252 else coe self
3254 | @escape when m_autonarrow ->
3255 if nonemptystr m_qsearch
3256 then source#add_narrow_pattern m_qsearch;
3257 super#key key mask
3259 | @enter | @kpenter when m_autonarrow ->
3260 if nonemptystr m_qsearch
3261 then source#add_narrow_pattern m_qsearch;
3262 super#key key mask
3264 | key when m_autonarrow && (key != 0 && key land 0xff00 != 0xff00) ->
3265 let pattern = m_qsearch ^ toutf8 key in
3266 G.postRedisplay "outlinelistview autonarrow add";
3267 source#narrow pattern;
3268 settext true pattern;
3269 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3271 | key when m_autonarrow && key = @backspace ->
3272 if emptystr m_qsearch
3273 then coe self
3274 else
3275 let pattern = withoutlastutf8 m_qsearch in
3276 G.postRedisplay "outlinelistview autonarrow backspace";
3277 ignore (source#renarrow);
3278 source#narrow pattern;
3279 settext true pattern;
3280 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3282 | @up | @kpup when ctrl ->
3283 navscroll (max 0 (m_first - 1))
3285 | @down | @kpdown when ctrl ->
3286 navscroll (min (source#getitemcount - 1) (m_first + 1))
3288 | @up | @kpup -> navigate ~-1
3289 | @down | @kpdown -> navigate 1
3290 | @prior | @kpprior -> navigate ~-(fstate.maxrows)
3291 | @next | @kpnext -> navigate fstate.maxrows
3293 | @right | @kpright ->
3294 let o =
3295 if ctrl
3296 then (
3297 G.postRedisplay "outline ctrl right";
3298 {< m_pan = m_pan + 1 >}
3300 else self#updownlevel 1
3302 coe o
3304 | @left | @kpleft ->
3305 let o =
3306 if ctrl
3307 then (
3308 G.postRedisplay "outline ctrl left";
3309 {< m_pan = m_pan - 1 >}
3311 else self#updownlevel ~-1
3313 coe o
3315 | @home | @kphome ->
3316 G.postRedisplay "outline home";
3317 coe {< m_first = 0; m_active = 0 >}
3319 | @jend | @kpend ->
3320 let active = source#getitemcount - 1 in
3321 let first = max 0 (active - fstate.maxrows) in
3322 G.postRedisplay "outline end";
3323 coe {< m_active = active; m_first = first >}
3325 | _ -> super#key key mask
3326 end;;
3328 let genhistoutlines () =
3329 Config.gethist ()
3330 |> List.sort (fun (_, c1, _, _, _, _) (_, c2, _, _, _, _) ->
3331 compare c2.lastvisit c1.lastvisit)
3332 |> List.map
3333 (fun ((path, c, _, _, _, origin) as hist) ->
3334 let path = if nonemptystr origin then origin else path in
3335 let base = mbtoutf8 @@ Filename.basename path in
3336 (base ^ "\000" ^ c.title, 1, Ohistory hist)
3338 |> Array.of_list
3341 let gotohist (path, c, bookmarks, x, anchor, origin) =
3342 Config.save leavebirdseye;
3343 state.anchor <- anchor;
3344 state.bookmarks <- bookmarks;
3345 state.origin <- origin;
3346 state.x <- x;
3347 setconf conf c;
3348 let x0, y0, x1, y1 = conf.trimfuzz in
3349 wcmd "trimset %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1;
3350 reshape ~firsttime:true state.winw state.winh;
3351 opendoc path origin;
3352 setzoom c.zoom;
3355 let makecheckers () =
3356 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3357 following to say:
3358 converted by Issac Trotts. July 25, 2002 *)
3359 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3360 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3361 let id = GlTex.gen_texture () in
3362 GlTex.bind_texture ~target:`texture_2d id;
3363 GlPix.store (`unpack_alignment 1);
3364 GlTex.image2d image;
3365 List.iter (GlTex.parameter ~target:`texture_2d)
3366 [ `mag_filter `nearest; `min_filter `nearest ];
3370 let setcheckers enabled =
3371 match state.checkerstexid with
3372 | None ->
3373 if enabled then state.checkerstexid <- Some (makecheckers ())
3375 | Some checkerstexid ->
3376 if not enabled
3377 then (
3378 GlTex.delete_texture checkerstexid;
3379 state.checkerstexid <- None;
3383 let describe_location () =
3384 let fn = page_of_y state.y in
3385 let ln = page_of_y (state.y + state.winh - hscrollh () - 1) in
3386 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
3387 let percent =
3388 if maxy <= 0
3389 then 100.
3390 else (100. *. (float state.y /. float maxy))
3392 if fn = ln
3393 then
3394 Printf.sprintf "page %d of %d [%.2f%%]"
3395 (fn+1) state.pagecount percent
3396 else
3397 Printf.sprintf
3398 "pages %d-%d of %d [%.2f%%]"
3399 (fn+1) (ln+1) state.pagecount percent
3402 let setpresentationmode v =
3403 let n = page_of_y state.y in
3404 state.anchor <- (n, 0.0, 1.0);
3405 conf.presentation <- v;
3406 if conf.fitmodel = FitPage
3407 then reqlayout conf.angle conf.fitmodel;
3408 represent ();
3411 let setbgcol (r, g, b) =
3412 let col =
3413 let r = r *. 255.0 |> truncate
3414 and g = g *. 255.0 |> truncate
3415 and b = b *. 255.0 |> truncate in
3416 r lsl 16 |> (lor) (g lsl 8) |> (lor) b
3418 Wsi.setwinbgcol col;
3421 let enterinfomode =
3422 let btos b = if b then "@Uradical" else E.s in
3423 let showextended = ref false in
3424 let leave mode _ = state.mode <- mode in
3425 let src =
3426 (object
3427 val mutable m_l = []
3428 val mutable m_a = E.a
3429 val mutable m_prev_uioh = nouioh
3430 val mutable m_prev_mode = View
3432 inherit lvsourcebase
3434 method reset prev_mode prev_uioh =
3435 m_a <- Array.of_list (List.rev m_l);
3436 m_l <- [];
3437 m_prev_mode <- prev_mode;
3438 m_prev_uioh <- prev_uioh;
3440 method int name get set =
3441 m_l <-
3442 (name, `int get, 1, Action (
3443 fun u ->
3444 let ondone s =
3445 try set (int_of_string s)
3446 with exn ->
3447 state.text <- Printf.sprintf "bad integer `%s': %s"
3448 s @@ exntos exn
3450 state.text <- E.s;
3451 let te = name ^ ": ", E.s, None, intentry, ondone, true in
3452 state.mode <- Textentry (te, leave m_prev_mode);
3454 )) :: m_l
3456 method int_with_suffix name get set =
3457 m_l <-
3458 (name, `intws get, 1, Action (
3459 fun u ->
3460 let ondone s =
3461 try set (int_of_string_with_suffix s)
3462 with exn ->
3463 state.text <- Printf.sprintf "bad integer `%s': %s"
3464 s @@ exntos exn
3466 state.text <- E.s;
3467 let te =
3468 name ^ ": ", E.s, None, intentry_with_suffix, ondone, true
3470 state.mode <- Textentry (te, leave m_prev_mode);
3472 )) :: m_l
3474 method bool ?(offset=1) ?(btos=btos) name get set =
3475 m_l <-
3476 (name, `bool (btos, get), offset, Action (
3477 fun u ->
3478 let v = get () in
3479 set (not v);
3481 )) :: m_l
3483 method color name get set =
3484 m_l <-
3485 (name, `color get, 1, Action (
3486 fun u ->
3487 let invalid = (nan, nan, nan) in
3488 let ondone s =
3489 let c =
3490 try color_of_string s
3491 with exn ->
3492 state.text <- Printf.sprintf "bad color `%s': %s"
3493 s @@ exntos exn;
3494 invalid
3496 if c <> invalid
3497 then set c;
3499 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3500 state.text <- color_to_string (get ());
3501 state.mode <- Textentry (te, leave m_prev_mode);
3503 )) :: m_l
3505 method string name get set =
3506 m_l <-
3507 (name, `string get, 1, Action (
3508 fun u ->
3509 let ondone s = set s in
3510 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3511 state.mode <- Textentry (te, leave m_prev_mode);
3513 )) :: m_l
3515 method colorspace name get set =
3516 m_l <-
3517 (name, `string get, 1, Action (
3518 fun _ ->
3519 let source =
3520 (object
3521 inherit lvsourcebase
3523 initializer
3524 m_active <- CSTE.to_int conf.colorspace;
3525 m_first <- 0;
3527 method getitemcount =
3528 Array.length CSTE.names
3529 method getitem n =
3530 (CSTE.names.(n), 0)
3531 method exit ~uioh ~cancel ~active ~first ~pan =
3532 ignore (uioh, first, pan);
3533 if not cancel then set active;
3534 None
3535 method hasaction _ = true
3536 end)
3538 state.text <- E.s;
3539 let modehash = findkeyhash conf "info" in
3540 coe (new listview ~zebra:false ~helpmode:false
3541 ~source ~trusted:true ~modehash)
3542 )) :: m_l
3544 method paxmark name get set =
3545 m_l <-
3546 (name, `string get, 1, Action (
3547 fun _ ->
3548 let source =
3549 (object
3550 inherit lvsourcebase
3552 initializer
3553 m_active <- MTE.to_int conf.paxmark;
3554 m_first <- 0;
3556 method getitemcount = Array.length MTE.names
3557 method getitem n = (MTE.names.(n), 0)
3558 method exit ~uioh ~cancel ~active ~first ~pan =
3559 ignore (uioh, first, pan);
3560 if not cancel then set active;
3561 None
3562 method hasaction _ = true
3563 end)
3565 state.text <- E.s;
3566 let modehash = findkeyhash conf "info" in
3567 coe (new listview ~zebra:false ~helpmode:false
3568 ~source ~trusted:true ~modehash)
3569 )) :: m_l
3571 method fitmodel name get set =
3572 m_l <-
3573 (name, `string get, 1, Action (
3574 fun _ ->
3575 let source =
3576 (object
3577 inherit lvsourcebase
3579 initializer
3580 m_active <- FMTE.to_int conf.fitmodel;
3581 m_first <- 0;
3583 method getitemcount = Array.length FMTE.names
3584 method getitem n = (FMTE.names.(n), 0)
3585 method exit ~uioh ~cancel ~active ~first ~pan =
3586 ignore (uioh, first, pan);
3587 if not cancel then set active;
3588 None
3589 method hasaction _ = true
3590 end)
3592 state.text <- E.s;
3593 let modehash = findkeyhash conf "info" in
3594 coe (new listview ~zebra:false ~helpmode:false
3595 ~source ~trusted:true ~modehash)
3596 )) :: m_l
3598 method caption s offset =
3599 m_l <- (s, `empty, offset, Noaction) :: m_l
3601 method caption2 s f offset =
3602 m_l <- (s, `string f, offset, Noaction) :: m_l
3604 method getitemcount = Array.length m_a
3606 method getitem n =
3607 let tostr = function
3608 | `int f -> string_of_int (f ())
3609 | `intws f -> string_with_suffix_of_int (f ())
3610 | `string f -> f ()
3611 | `color f -> color_to_string (f ())
3612 | `bool (btos, f) -> btos (f ())
3613 | `empty -> E.s
3615 let name, t, offset, _ = m_a.(n) in
3616 ((let s = tostr t in
3617 if nonemptystr s
3618 then Printf.sprintf "%s\t%s" name s
3619 else name),
3620 offset)
3622 method exit ~uioh ~cancel ~active ~first ~pan =
3623 let uiohopt =
3624 if not cancel
3625 then (
3626 let uioh =
3627 match m_a.(active) with
3628 | _, _, _, Action f -> f uioh
3629 | _, _, _, Noaction -> uioh
3631 Some uioh
3633 else None
3635 m_active <- active;
3636 m_first <- first;
3637 m_pan <- pan;
3638 uiohopt
3640 method hasaction n =
3641 match m_a.(n) with
3642 | _, _, _, Action _ -> true
3643 | _, _, _, Noaction -> false
3645 initializer m_active <- 1
3646 end)
3648 let rec fillsrc prevmode prevuioh =
3649 let sep () = src#caption E.s 0 in
3650 let colorp name get set =
3651 src#string name
3652 (fun () -> color_to_string (get ()))
3653 (fun v ->
3655 let c = color_of_string v in
3656 set c
3657 with exn ->
3658 state.text <- Printf.sprintf "bad color `%s': %s" v @@ exntos exn
3661 let oldmode = state.mode in
3662 let birdseye = isbirdseye state.mode in
3664 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3666 src#bool "presentation mode"
3667 (fun () -> conf.presentation)
3668 (fun v -> setpresentationmode v);
3670 src#bool "ignore case in searches"
3671 (fun () -> conf.icase)
3672 (fun v -> conf.icase <- v);
3674 src#bool "preload"
3675 (fun () -> conf.preload)
3676 (fun v -> conf.preload <- v);
3678 src#bool "highlight links"
3679 (fun () -> conf.hlinks)
3680 (fun v -> conf.hlinks <- v);
3682 src#bool "under info"
3683 (fun () -> conf.underinfo)
3684 (fun v -> conf.underinfo <- v);
3686 src#bool "persistent bookmarks"
3687 (fun () -> conf.savebmarks)
3688 (fun v -> conf.savebmarks <- v);
3690 src#fitmodel "fit model"
3691 (fun () -> FMTE.to_string conf.fitmodel)
3692 (fun v -> reqlayout conf.angle (FMTE.of_int v));
3694 src#bool "trim margins"
3695 (fun () -> conf.trimmargins)
3696 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3698 src#bool "persistent location"
3699 (fun () -> conf.jumpback)
3700 (fun v -> conf.jumpback <- v);
3702 sep ();
3703 src#int "inter-page space"
3704 (fun () -> conf.interpagespace)
3705 (fun n ->
3706 conf.interpagespace <- n;
3707 docolumns conf.columns;
3708 let pageno, py =
3709 match state.layout with
3710 | [] -> 0, 0
3711 | l :: _ ->
3712 l.pageno, l.pagey
3714 state.maxy <- calcheight ();
3715 let y = getpagey pageno in
3716 gotoy (y + py)
3719 src#int "page bias"
3720 (fun () -> conf.pagebias)
3721 (fun v -> conf.pagebias <- v);
3723 src#int "scroll step"
3724 (fun () -> conf.scrollstep)
3725 (fun n -> conf.scrollstep <- n);
3727 src#int "horizontal scroll step"
3728 (fun () -> conf.hscrollstep)
3729 (fun v -> conf.hscrollstep <- v);
3731 src#int "auto scroll step"
3732 (fun () ->
3733 match state.autoscroll with
3734 | Some step -> step
3735 | _ -> conf.autoscrollstep)
3736 (fun n ->
3737 let n = boundastep state.winh n in
3738 if state.autoscroll <> None
3739 then state.autoscroll <- Some n;
3740 conf.autoscrollstep <- n);
3742 src#int "zoom"
3743 (fun () -> truncate (conf.zoom *. 100.))
3744 (fun v -> setzoom ((float v) /. 100.));
3746 src#int "rotation"
3747 (fun () -> conf.angle)
3748 (fun v -> reqlayout v conf.fitmodel);
3750 src#int "scroll bar width"
3751 (fun () -> conf.scrollbw)
3752 (fun v ->
3753 conf.scrollbw <- v;
3754 reshape state.winw state.winh;
3757 src#int "scroll handle height"
3758 (fun () -> conf.scrollh)
3759 (fun v -> conf.scrollh <- v;);
3761 src#int "thumbnail width"
3762 (fun () -> conf.thumbw)
3763 (fun v ->
3764 conf.thumbw <- min 4096 v;
3765 match oldmode with
3766 | Birdseye beye ->
3767 leavebirdseye beye false;
3768 enterbirdseye ()
3769 | Textentry _
3770 | View
3771 | LinkNav _ -> ()
3774 let mode = state.mode in
3775 src#string "columns"
3776 (fun () ->
3777 match conf.columns with
3778 | Csingle _ -> "1"
3779 | Cmulti (multi, _) -> multicolumns_to_string multi
3780 | Csplit (count, _) -> "-" ^ string_of_int count
3782 (fun v ->
3783 let n, a, b = multicolumns_of_string v in
3784 setcolumns mode n a b);
3786 sep ();
3787 src#caption "Pixmap cache" 0;
3788 src#int_with_suffix "size (advisory)"
3789 (fun () -> conf.memlimit)
3790 (fun v -> conf.memlimit <- v);
3792 src#caption2 "used"
3793 (fun () -> Printf.sprintf "%s bytes, %d tiles"
3794 (string_with_suffix_of_int state.memused)
3795 (Hashtbl.length state.tilemap)) 1;
3797 sep ();
3798 src#caption "Layout" 0;
3799 src#caption2 "Dimension"
3800 (fun () ->
3801 Printf.sprintf "%dx%d (virtual %dx%d)"
3802 state.winw state.winh
3803 state.w state.maxy)
3805 if conf.debug
3806 then
3807 src#caption2 "Position" (fun () ->
3808 Printf.sprintf "%dx%d" state.x state.y
3810 else
3811 src#caption2 "Position" (fun () -> describe_location ()) 1
3814 sep ();
3815 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3816 "Save these parameters as global defaults at exit"
3817 (fun () -> conf.bedefault)
3818 (fun v -> conf.bedefault <- v)
3821 sep ();
3822 let btos b = if b then "@Ulguillemet" else "@Urguillemet" in
3823 src#bool ~offset:0 ~btos "Extended parameters"
3824 (fun () -> !showextended)
3825 (fun v -> showextended := v; fillsrc prevmode prevuioh);
3826 if !showextended
3827 then (
3828 src#bool "checkers"
3829 (fun () -> conf.checkers)
3830 (fun v -> conf.checkers <- v; setcheckers v);
3831 src#bool "update cursor"
3832 (fun () -> conf.updatecurs)
3833 (fun v -> conf.updatecurs <- v);
3834 src#bool "scroll-bar on the left"
3835 (fun () -> conf.leftscroll)
3836 (fun v -> conf.leftscroll <- v);
3837 src#bool "verbose"
3838 (fun () -> conf.verbose)
3839 (fun v -> conf.verbose <- v);
3840 src#bool "invert colors"
3841 (fun () -> conf.invert)
3842 (fun v -> conf.invert <- v);
3843 src#bool "max fit"
3844 (fun () -> conf.maxhfit)
3845 (fun v -> conf.maxhfit <- v);
3846 src#bool "pax mode"
3847 (fun () -> conf.pax != None)
3848 (fun v ->
3849 if v
3850 then conf.pax <- Some (ref (now (), 0, 0))
3851 else conf.pax <- None);
3852 src#string "uri launcher"
3853 (fun () -> conf.urilauncher)
3854 (fun v -> conf.urilauncher <- v);
3855 src#string "path launcher"
3856 (fun () -> conf.pathlauncher)
3857 (fun v -> conf.pathlauncher <- v);
3858 src#string "tile size"
3859 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3860 (fun v ->
3862 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
3863 conf.tilew <- max 64 w;
3864 conf.tileh <- max 64 h;
3865 flushtiles ();
3866 with exn ->
3867 state.text <- Printf.sprintf "bad tile size `%s': %s"
3868 v @@ exntos exn
3870 src#int "texture count"
3871 (fun () -> conf.texcount)
3872 (fun v ->
3873 if realloctexts v
3874 then conf.texcount <- v
3875 else impmsg "failed to set texture count please retry later"
3877 src#int "slice height"
3878 (fun () -> conf.sliceheight)
3879 (fun v ->
3880 conf.sliceheight <- v;
3881 wcmd "sliceh %d" conf.sliceheight;
3883 src#int "anti-aliasing level"
3884 (fun () -> conf.aalevel)
3885 (fun v ->
3886 conf.aalevel <- bound v 0 8;
3887 state.anchor <- getanchor ();
3888 opendoc state.path state.password;
3890 src#string "page scroll scaling factor"
3891 (fun () -> string_of_float conf.pgscale)
3892 (fun v ->
3894 let s = float_of_string v in
3895 conf.pgscale <- s
3896 with exn ->
3897 state.text <- Printf.sprintf
3898 "bad page scroll scaling factor `%s': %s" v @@ exntos exn
3901 src#int "ui font size"
3902 (fun () -> fstate.fontsize)
3903 (fun v -> setfontsize (bound v 5 100));
3904 src#int "hint font size"
3905 (fun () -> conf.hfsize)
3906 (fun v -> conf.hfsize <- bound v 5 100);
3907 colorp "background color"
3908 (fun () -> conf.bgcolor)
3909 (fun v -> conf.bgcolor <- v; setbgcol v);
3910 src#bool "crop hack"
3911 (fun () -> conf.crophack)
3912 (fun v -> conf.crophack <- v);
3913 src#string "trim fuzz"
3914 (fun () -> irect_to_string conf.trimfuzz)
3915 (fun v ->
3917 conf.trimfuzz <- irect_of_string v;
3918 if conf.trimmargins
3919 then settrim true conf.trimfuzz;
3920 with exn ->
3921 state.text <- Printf.sprintf "bad irect `%s': %s" v @@ exntos exn
3923 src#string "throttle"
3924 (fun () ->
3925 match conf.maxwait with
3926 | None -> "show place holder if page is not ready"
3927 | Some time ->
3928 if time = infinity
3929 then "wait for page to fully render"
3930 else
3931 "wait " ^ string_of_float time
3932 ^ " seconds before showing placeholder"
3934 (fun v ->
3936 let f = float_of_string v in
3937 if f <= 0.0
3938 then conf.maxwait <- None
3939 else conf.maxwait <- Some f
3940 with exn ->
3941 state.text <- Printf.sprintf "bad time `%s': %s" v @@ exntos exn
3943 src#string "ghyll scroll"
3944 (fun () ->
3945 match conf.ghyllscroll with
3946 | None -> E.s
3947 | Some nab -> ghyllscroll_to_string nab
3949 (fun v ->
3950 try conf.ghyllscroll <- ghyllscroll_of_string v
3951 with
3952 | Failure msg ->
3953 state.text <- Printf.sprintf "bad ghyll `%s': %s" v msg
3954 | exn ->
3955 state.text <- Printf.sprintf "bad ghyll `%s': %s" v @@ exntos exn
3957 src#string "selection command"
3958 (fun () -> conf.selcmd)
3959 (fun v -> conf.selcmd <- v);
3960 src#string "synctex command"
3961 (fun () -> conf.stcmd)
3962 (fun v -> conf.stcmd <- v);
3963 src#string "pax command"
3964 (fun () -> conf.paxcmd)
3965 (fun v -> conf.paxcmd <- v);
3966 src#string "ask password command"
3967 (fun () -> conf.passcmd)
3968 (fun v -> conf.passcmd <- v);
3969 src#string "save path command"
3970 (fun () -> conf.savecmd)
3971 (fun v -> conf.savecmd <- v);
3972 src#colorspace "color space"
3973 (fun () -> CSTE.to_string conf.colorspace)
3974 (fun v ->
3975 conf.colorspace <- CSTE.of_int v;
3976 wcmd "cs %d" v;
3977 load state.layout;
3979 src#paxmark "pax mark method"
3980 (fun () -> MTE.to_string conf.paxmark)
3981 (fun v -> conf.paxmark <- MTE.of_int v);
3982 if bousable () && !opengl_has_pbo
3983 then
3984 src#bool "use PBO"
3985 (fun () -> conf.usepbo)
3986 (fun v -> conf.usepbo <- v);
3987 src#bool "mouse wheel scrolls pages"
3988 (fun () -> conf.wheelbypage)
3989 (fun v -> conf.wheelbypage <- v);
3990 src#bool "open remote links in a new instance"
3991 (fun () -> conf.riani)
3992 (fun v -> conf.riani <- v);
3993 src#bool "edit annotations inline"
3994 (fun () -> conf.annotinline)
3995 (fun v -> conf.annotinline <- v);
3996 src#bool "coarse positioning in presentation mode"
3997 (fun () -> conf.coarseprespos)
3998 (fun v -> conf.coarseprespos <- v);
4001 sep ();
4002 src#caption "Document" 0;
4003 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4004 src#caption2 "Pages"
4005 (fun () -> string_of_int state.pagecount) 1;
4006 src#caption2 "Dimensions"
4007 (fun () -> string_of_int (List.length state.pdims)) 1;
4008 if conf.trimmargins
4009 then (
4010 sep ();
4011 src#caption "Trimmed margins" 0;
4012 src#caption2 "Dimensions"
4013 (fun () -> string_of_int (List.length state.pdims)) 1;
4016 sep ();
4017 src#caption "OpenGL" 0;
4018 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4019 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4021 sep ();
4022 src#caption "Location" 0;
4023 if nonemptystr state.origin
4024 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
4025 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
4027 src#reset prevmode prevuioh;
4029 fun () ->
4030 state.text <- E.s;
4031 resetmstate ();
4032 let prevmode = state.mode
4033 and prevuioh = state.uioh in
4034 fillsrc prevmode prevuioh;
4035 let source = (src :> lvsource) in
4036 let modehash = findkeyhash conf "info" in
4037 state.uioh <- coe (object (self)
4038 inherit listview ~zebra:false ~helpmode:false
4039 ~source ~trusted:true ~modehash as super
4040 val mutable m_prevmemused = 0
4041 method! infochanged = function
4042 | Memused ->
4043 if m_prevmemused != state.memused
4044 then (
4045 m_prevmemused <- state.memused;
4046 G.postRedisplay "memusedchanged";
4048 | Pdim -> G.postRedisplay "pdimchanged"
4049 | Docinfo -> fillsrc prevmode prevuioh
4051 method! key key mask =
4052 if not (Wsi.withctrl mask)
4053 then
4054 match key with
4055 | @left | @kpleft -> coe (self#updownlevel ~-1)
4056 | @right | @kpright -> coe (self#updownlevel 1)
4057 | _ -> super#key key mask
4058 else super#key key mask
4059 end);
4060 G.postRedisplay "info";
4063 let enterhelpmode =
4064 let source =
4065 (object
4066 inherit lvsourcebase
4067 method getitemcount = Array.length state.help
4068 method getitem n =
4069 let s, l, _ = state.help.(n) in
4070 (s, l)
4072 method exit ~uioh ~cancel ~active ~first ~pan =
4073 let optuioh =
4074 if not cancel
4075 then (
4076 match state.help.(active) with
4077 | _, _, Action f -> Some (f uioh)
4078 | _, _, Noaction -> Some uioh
4080 else None
4082 m_active <- active;
4083 m_first <- first;
4084 m_pan <- pan;
4085 optuioh
4087 method hasaction n =
4088 match state.help.(n) with
4089 | _, _, Action _ -> true
4090 | _, _, Noaction -> false
4092 initializer
4093 m_active <- -1
4094 end)
4095 in fun () ->
4096 let modehash = findkeyhash conf "help" in
4097 resetmstate ();
4098 state.uioh <- coe (new listview
4099 ~zebra:false ~helpmode:true
4100 ~source ~trusted:true ~modehash);
4101 G.postRedisplay "help";
4104 let entermsgsmode =
4105 let msgsource =
4106 (object
4107 inherit lvsourcebase
4108 val mutable m_items = E.a
4110 method getitemcount = 1 + Array.length m_items
4112 method getitem n =
4113 if n = 0
4114 then "[Clear]", 0
4115 else m_items.(n-1), 0
4117 method exit ~uioh ~cancel ~active ~first ~pan =
4118 ignore uioh;
4119 if not cancel
4120 then (
4121 if active = 0
4122 then Buffer.clear state.errmsgs;
4124 m_active <- active;
4125 m_first <- first;
4126 m_pan <- pan;
4127 None
4129 method hasaction n =
4130 n = 0
4132 method reset =
4133 state.newerrmsgs <- false;
4134 let l = Str.split newlinere (Buffer.contents state.errmsgs) in
4135 m_items <- Array.of_list l
4137 initializer
4138 m_active <- 0
4139 end)
4140 in fun () ->
4141 state.text <- E.s;
4142 resetmstate ();
4143 msgsource#reset;
4144 let source = (msgsource :> lvsource) in
4145 let modehash = findkeyhash conf "listview" in
4146 state.uioh <- coe (object
4147 inherit listview ~zebra:false ~helpmode:false
4148 ~source ~trusted:false ~modehash as super
4149 method! display =
4150 if state.newerrmsgs
4151 then msgsource#reset;
4152 super#display
4153 end);
4154 G.postRedisplay "msgs";
4157 let getusertext s =
4158 let editor = getenvwithdef "EDITOR" E.s in
4159 if emptystr editor
4160 then E.s
4161 else
4162 let tmppath = Filename.temp_file "llpp" "note" in
4163 if nonemptystr s
4164 then (
4165 let oc = open_out tmppath in
4166 output_string oc s;
4167 close_out oc;
4169 let execstr = editor ^ " " ^ tmppath in
4170 let s =
4171 match spawn execstr [] with
4172 | (exception exn) ->
4173 impmsg "spawn(%S) failed: %s" execstr @@ exntos exn;
4175 | pid ->
4176 match Unix.waitpid [] pid with
4177 | (exception exn) ->
4178 impmsg "waitpid(%d) failed: %s" pid @@ exntos exn;
4180 | (_pid, status) ->
4181 match status with
4182 | Unix.WEXITED 0 -> filecontents tmppath
4183 | Unix.WEXITED n ->
4184 impmsg "editor process(%s) exited abnormally: %d" execstr n;
4186 | Unix.WSIGNALED n ->
4187 impmsg "editor process(%s) was killed by signal %d" execstr n;
4189 | Unix.WSTOPPED n ->
4190 impmsg "editor(%s) process was stopped by signal %d" execstr n;
4193 match Unix.unlink tmppath with
4194 | (exception exn) ->
4195 impmsg "failed to ulink %S: %s" tmppath @@ exntos exn;
4197 | () -> s
4200 let enterannotmode opaque slinkindex =
4201 let msgsource =
4202 (object
4203 inherit lvsourcebase
4204 val mutable m_text = E.s
4205 val mutable m_items = E.a
4207 method getitemcount = Array.length m_items
4209 method getitem n =
4210 let label, _func = m_items.(n) in
4211 label, 0
4213 method exit ~uioh ~cancel ~active ~first ~pan =
4214 ignore (uioh, first, pan);
4215 if not cancel
4216 then (
4217 let _label, func = m_items.(active) in
4218 func ()
4220 None
4222 method hasaction n = nonemptystr @@ fst m_items.(n)
4224 method reset s =
4225 let rec split accu b i =
4226 let p = b+i in
4227 if p = String.length s
4228 then (String.sub s b (p-b), unit) :: accu
4229 else
4230 if (i > 70 && s.[p] = ' ') || s.[p] = '\r' || s.[p] = '\n'
4231 then
4232 let ss = if i = 0 then E.s else String.sub s b i in
4233 split ((ss, unit)::accu) (p+1) 0
4234 else
4235 split accu b (i+1)
4237 let cleanup () =
4238 wcmd "freepage %s" (~> opaque);
4239 let keys =
4240 Hashtbl.fold (fun key opaque' accu ->
4241 if opaque' = opaque'
4242 then key :: accu else accu) state.pagemap []
4244 List.iter (Hashtbl.remove state.pagemap) keys;
4245 flushtiles ();
4246 gotoy state.y
4248 let dele () =
4249 delannot opaque slinkindex;
4250 cleanup ();
4252 let edit inline () =
4253 let update s =
4254 if emptystr s
4255 then dele ()
4256 else (
4257 modannot opaque slinkindex s;
4258 cleanup ();
4261 if inline
4262 then
4263 let mode = state.mode in
4264 state.mode <-
4265 Textentry (
4266 ("annotation: ", m_text, None, textentry, update, true),
4267 fun _ -> state.mode <- mode);
4268 state.text <- E.s;
4269 enttext ();
4270 else
4271 let s = getusertext m_text in
4272 update s
4274 m_text <- s;
4275 m_items <-
4276 ( "[Copy]", fun () -> selstring m_text)
4277 :: ("[Delete]", dele)
4278 :: ("[Edit]", edit conf.annotinline)
4279 :: (E.s, unit)
4280 :: split [] 0 0 |> List.rev |> Array.of_list
4282 initializer
4283 m_active <- 0
4284 end)
4286 state.text <- E.s;
4287 let s = getannotcontents opaque slinkindex in
4288 resetmstate ();
4289 msgsource#reset s;
4290 let source = (msgsource :> lvsource) in
4291 let modehash = findkeyhash conf "listview" in
4292 state.uioh <- coe (object
4293 inherit listview ~zebra:false ~helpmode:false
4294 ~source ~trusted:false ~modehash
4295 end);
4296 G.postRedisplay "enterannotmode";
4299 let gotounder under =
4300 let getpath filename =
4301 let path =
4302 if nonemptystr filename
4303 then
4304 if Filename.is_relative filename
4305 then
4306 let dir = Filename.dirname state.path in
4307 let dir =
4308 if Filename.is_implicit dir
4309 then Filename.concat (Sys.getcwd ()) dir
4310 else dir
4312 Filename.concat dir filename
4313 else filename
4314 else E.s
4316 if Sys.file_exists path
4317 then path
4318 else E.s
4320 match under with
4321 | Ulinkgoto (pageno, top) ->
4322 if pageno >= 0
4323 then (
4324 addnav ();
4325 let top =
4326 if conf.presentation && conf.coarseprespos
4327 then 0
4328 else top
4330 gotopage1 pageno top;
4333 | Ulinkuri s -> gotouri s
4335 | Uremote (filename, pageno) ->
4336 let path = getpath filename in
4337 if nonemptystr path
4338 then (
4339 if conf.riani
4340 then
4341 let command = Printf.sprintf "%s -page %d %S" !selfexec pageno path in
4342 match spawn command [] with
4343 | _pid -> ()
4344 | (exception exn) ->
4345 dolog "failed to execute `%s': %s" command @@ exntos exn
4346 else
4347 let anchor = getanchor () in
4348 let ranchor = state.path, state.password, anchor, state.origin in
4349 state.origin <- E.s;
4350 state.anchor <- (pageno, 0.0, 0.0);
4351 state.ranchors <- ranchor :: state.ranchors;
4352 opendoc path E.s;
4354 else impmsg "cannot find %s" filename
4356 | Uremotedest (filename, destname) ->
4357 let path = getpath filename in
4358 if nonemptystr path
4359 then (
4360 if conf.riani
4361 then
4362 let command = !selfexec ^ " " ^ path ^ " -dest " ^ destname in
4363 match spawn command [] with
4364 | (exception exn) ->
4365 dolog "failed to execute `%s': %s" command @@ exntos exn
4366 | _pid -> ()
4367 else
4368 let anchor = getanchor () in
4369 let ranchor = state.path, state.password, anchor, state.origin in
4370 state.origin <- E.s;
4371 state.nameddest <- destname;
4372 state.ranchors <- ranchor :: state.ranchors;
4373 opendoc path E.s;
4375 else impmsg "cannot find %s" filename
4377 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4378 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
4381 let gotooutline (_, _, kind) =
4382 match kind with
4383 | Onone -> ()
4384 | Oanchor anchor ->
4385 let (pageno, y, _) = anchor in
4386 let y = getanchory
4387 (if conf.presentation then (pageno, y, 1.0) else anchor)
4389 addnav ();
4390 gotoghyll y
4391 | Ouri uri -> gotounder (Ulinkuri uri)
4392 | Olaunch cmd -> gotounder (Ulaunch cmd)
4393 | Oremote remote -> gotounder (Uremote remote)
4394 | Ohistory hist -> gotohist hist
4395 | Oremotedest remotedest -> gotounder (Uremotedest remotedest)
4398 class outlinesoucebase fetchoutlines = object (self)
4399 inherit lvsourcebase
4400 val mutable m_items = E.a
4401 val mutable m_minfo = E.a
4402 val mutable m_orig_items = E.a
4403 val mutable m_orig_minfo = E.a
4404 val mutable m_narrow_patterns = []
4405 val mutable m_gen = -1
4407 method getitemcount = Array.length m_items
4409 method getitem n =
4410 let s, n, _ = m_items.(n) in
4411 (s, n+0)
4413 method exit ~(uioh:uioh) ~cancel ~active ~(first:int) ~pan :
4414 uioh option =
4415 ignore (uioh, first);
4416 let items, minfo =
4417 if m_narrow_patterns = []
4418 then m_orig_items, m_orig_minfo
4419 else m_items, m_minfo
4421 m_pan <- pan;
4422 if not cancel
4423 then (
4424 m_items <- items;
4425 m_minfo <- minfo;
4426 gotooutline m_items.(active);
4428 else (
4429 m_items <- items;
4430 m_minfo <- minfo;
4432 None
4434 method hasaction (_:int) = true
4436 method greetmsg =
4437 if Array.length m_items != Array.length m_orig_items
4438 then
4439 let s =
4440 match m_narrow_patterns with
4441 | one :: [] -> one
4442 | many -> String.concat "@Uellipsis" (List.rev many)
4444 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
4445 else E.s
4447 method statestr =
4448 match m_narrow_patterns with
4449 | [] -> E.s
4450 | one :: [] -> one
4451 | head :: _ -> "@Uellipsis" ^ head
4453 method narrow pattern =
4454 match Str.regexp_case_fold pattern with
4455 | (exception _) -> ()
4456 | re ->
4457 let rec loop accu minfo n =
4458 if n = -1
4459 then (
4460 m_items <- Array.of_list accu;
4461 m_minfo <- Array.of_list minfo;
4463 else
4464 let (s, _, _) as o = m_items.(n) in
4465 let accu, minfo =
4466 match Str.search_forward re s 0 with
4467 | (exception Not_found) -> accu, minfo
4468 | first -> o :: accu, (first, Str.match_end ()) :: minfo
4470 loop accu minfo (n-1)
4472 loop [] [] (Array.length m_items - 1)
4474 method! getminfo = m_minfo
4476 method denarrow =
4477 m_orig_items <- fetchoutlines ();
4478 m_minfo <- m_orig_minfo;
4479 m_items <- m_orig_items
4481 method add_narrow_pattern pattern =
4482 m_narrow_patterns <- pattern :: m_narrow_patterns
4484 method del_narrow_pattern =
4485 match m_narrow_patterns with
4486 | _ :: rest -> m_narrow_patterns <- rest
4487 | [] -> ()
4489 method renarrow =
4490 self#denarrow;
4491 match m_narrow_patterns with
4492 | pattern :: [] -> self#narrow pattern; pattern
4493 | list ->
4494 List.fold_left (fun accu pattern ->
4495 self#narrow pattern;
4496 pattern ^ "@Uellipsis" ^ accu) E.s list
4498 method calcactive (_:anchor) = 0
4500 method reset anchor items =
4501 if state.gen != m_gen
4502 then (
4503 m_orig_items <- items;
4504 m_items <- items;
4505 m_narrow_patterns <- [];
4506 m_minfo <- E.a;
4507 m_orig_minfo <- E.a;
4508 m_gen <- state.gen;
4510 else (
4511 if items != m_orig_items
4512 then (
4513 m_orig_items <- items;
4514 if m_narrow_patterns == []
4515 then m_items <- items;
4518 let active = self#calcactive anchor in
4519 m_active <- active;
4520 m_first <- firstof m_first active
4524 let outlinesource fetchoutlines =
4525 (object
4526 inherit outlinesoucebase fetchoutlines
4527 method! calcactive anchor =
4528 let rely = getanchory anchor in
4529 let rec loop n best bestd =
4530 if n = Array.length m_items
4531 then best
4532 else
4533 let _, _, kind = m_items.(n) in
4534 match kind with
4535 | Oanchor anchor ->
4536 let orely = getanchory anchor in
4537 let d = abs (orely - rely) in
4538 if d < bestd
4539 then loop (n+1) n d
4540 else loop (n+1) best bestd
4541 | Onone | Oremote _ | Olaunch _
4542 | Oremotedest _ | Ouri _ | Ohistory _ ->
4543 loop (n+1) best bestd
4545 loop 0 ~-1 max_int
4546 end)
4549 let enteroutlinemode, enterbookmarkmode, enterhistmode =
4550 let mkselector sourcetype =
4551 let fetchoutlines () =
4552 match sourcetype with
4553 | `bookmarks -> Array.of_list state.bookmarks
4554 | `outlines -> state.outlines
4555 | `history -> genhistoutlines ()
4557 let source =
4558 if sourcetype = `history
4559 then new outlinesoucebase fetchoutlines
4560 else outlinesource fetchoutlines
4562 fun errmsg ->
4563 let outlines = fetchoutlines () in
4564 if Array.length outlines = 0
4565 then (
4566 showtext ' ' errmsg;
4568 else (
4569 resetmstate ();
4570 Wsi.setcursor Wsi.CURSOR_INHERIT;
4571 let anchor = getanchor () in
4572 source#reset anchor outlines;
4573 state.text <- source#greetmsg;
4574 state.uioh <-
4575 coe (new outlinelistview ~zebra:(sourcetype=`history) ~source);
4576 G.postRedisplay "enter selector";
4579 let mkenter sourcetype errmsg =
4580 let enter = mkselector sourcetype in
4581 fun () -> enter errmsg
4583 (**)mkenter `outlines "document has no outline"
4584 , mkenter `bookmarks "document has no bookmarks (yet)"
4585 , mkenter `history "history is empty"
4588 let quickbookmark ?title () =
4589 match state.layout with
4590 | [] -> ()
4591 | l :: _ ->
4592 let title =
4593 match title with
4594 | None ->
4595 let tm = Unix.localtime (now ()) in
4596 Printf.sprintf
4597 "Quick (page %d) (bookmarked at %02d/%02d/%d %02d:%02d)"
4598 (l.pageno+1)
4599 tm.Unix.tm_mday
4600 (tm.Unix.tm_mon+1)
4601 (tm.Unix.tm_year + 1900)
4602 tm.Unix.tm_hour
4603 tm.Unix.tm_min
4604 | Some title -> title
4606 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4609 let setautoscrollspeed step goingdown =
4610 let incr = max 1 ((abs step) / 2) in
4611 let incr = if goingdown then incr else -incr in
4612 let astep = boundastep state.winh (step + incr) in
4613 state.autoscroll <- Some astep;
4616 let canpan () =
4617 match conf.columns with
4618 | Csplit _ -> true
4619 | Csingle _ | Cmulti _ -> state.x != 0 || conf.zoom > 1.0
4622 let panbound x = bound x (-state.w) (wadjsb () + state.winw);;
4624 let existsinrow pageno (columns, coverA, coverB) p =
4625 let last = ((pageno - coverA) mod columns) + columns in
4626 let rec any = function
4627 | [] -> false
4628 | l :: rest ->
4629 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4630 then p l
4631 else (
4632 if not (p l)
4633 then (if l.pageno = last then false else any rest)
4634 else true
4637 any state.layout
4640 let nextpage () =
4641 match state.layout with
4642 | [] ->
4643 let pageno = page_of_y state.y in
4644 gotoghyll (getpagey (pageno+1))
4645 | l :: rest ->
4646 match conf.columns with
4647 | Csingle _ ->
4648 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4649 then
4650 let y = clamp (pgscale state.winh) in
4651 gotoghyll y
4652 else
4653 let pageno = min (l.pageno+1) (state.pagecount-1) in
4654 gotoghyll (getpagey pageno)
4655 | Cmulti ((c, _, _) as cl, _) ->
4656 if conf.presentation
4657 && (existsinrow l.pageno cl
4658 (fun l -> l.pageh > l.pagey + l.pagevh))
4659 then
4660 let y = clamp (pgscale state.winh) in
4661 gotoghyll y
4662 else
4663 let pageno = min (l.pageno+c) (state.pagecount-1) in
4664 gotoghyll (getpagey pageno)
4665 | Csplit (n, _) ->
4666 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4667 then
4668 let pagey, pageh = getpageyh l.pageno in
4669 let pagey = pagey + pageh * l.pagecol in
4670 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4671 gotoghyll (pagey + pageh + ips)
4674 let prevpage () =
4675 match state.layout with
4676 | [] ->
4677 let pageno = page_of_y state.y in
4678 gotoghyll (getpagey (pageno-1))
4679 | l :: _ ->
4680 match conf.columns with
4681 | Csingle _ ->
4682 if conf.presentation && l.pagey != 0
4683 then
4684 gotoghyll (clamp (pgscale ~-(state.winh)))
4685 else
4686 let pageno = max 0 (l.pageno-1) in
4687 gotoghyll (getpagey pageno)
4688 | Cmulti ((c, _, coverB) as cl, _) ->
4689 if conf.presentation &&
4690 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4691 then
4692 gotoghyll (clamp (pgscale ~-(state.winh)))
4693 else
4694 let decr =
4695 if l.pageno = state.pagecount - coverB
4696 then 1
4697 else c
4699 let pageno = max 0 (l.pageno-decr) in
4700 gotoghyll (getpagey pageno)
4701 | Csplit (n, _) ->
4702 let y =
4703 if l.pagecol = 0
4704 then
4705 if l.pageno = 0
4706 then l.pagey
4707 else
4708 let pageno = max 0 (l.pageno-1) in
4709 let pagey, pageh = getpageyh pageno in
4710 pagey + (n-1)*pageh
4711 else
4712 let pagey, pageh = getpageyh l.pageno in
4713 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4715 gotoghyll y
4718 let save () =
4719 if emptystr conf.savecmd
4720 then error "don't know where to save modified document"
4721 else
4722 let savecmd = Str.global_replace percentsre state.path conf.savecmd in
4723 let path =
4724 getcmdoutput
4725 (fun s -> error "failed to obtain path to the saved copy: %s" s)
4726 savecmd
4728 if nonemptystr path
4729 then
4730 let tmp = path ^ ".tmp" in
4731 savedoc tmp;
4732 Unix.rename tmp path;
4735 let viewkeyboard key mask =
4736 let enttext te =
4737 let mode = state.mode in
4738 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4739 state.text <- E.s;
4740 enttext ();
4741 G.postRedisplay "view:enttext"
4743 let ctrl = Wsi.withctrl mask in
4744 let key =
4745 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
4747 match key with
4748 | @Q -> exit 0
4750 | @W ->
4751 if hasunsavedchanges ()
4752 then save ()
4754 | @insert ->
4755 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4756 then (
4757 state.mode <- LinkNav (Ltgendir 0);
4758 gotoy state.y;
4760 else impmsg "keyboard link navigation does not work under rotation"
4762 | @escape | @q ->
4763 begin match state.mstate with
4764 | Mzoomrect _ ->
4765 resetmstate ();
4766 G.postRedisplay "kill rect";
4767 | Msel _
4768 | Mpan _
4769 | Mscrolly | Mscrollx
4770 | Mzoom _
4771 | Mnone ->
4772 begin match state.mode with
4773 | LinkNav _ ->
4774 state.mode <- View;
4775 G.postRedisplay "esc leave linknav"
4776 | Birdseye _
4777 | Textentry _
4778 | View ->
4779 match state.ranchors with
4780 | [] -> raise Quit
4781 | (path, password, anchor, origin) :: rest ->
4782 state.ranchors <- rest;
4783 state.anchor <- anchor;
4784 state.origin <- origin;
4785 state.nameddest <- E.s;
4786 opendoc path password
4787 end;
4788 end;
4790 | @backspace ->
4791 gotoghyll (getnav ~-1)
4793 | @o ->
4794 enteroutlinemode ()
4796 | @H ->
4797 enterhistmode ()
4799 | @u ->
4800 state.rects <- [];
4801 state.text <- E.s;
4802 Hashtbl.iter (fun _ opaque ->
4803 clearmark opaque;
4804 Hashtbl.clear state.prects) state.pagemap;
4805 G.postRedisplay "dehighlight";
4807 | @slash | @question ->
4808 let ondone isforw s =
4809 cbput state.hists.pat s;
4810 state.searchpattern <- s;
4811 search s isforw
4813 let s = String.make 1 (Char.chr key) in
4814 enttext (s, E.s, Some (onhist state.hists.pat),
4815 textentry, ondone (key = @slash), true)
4817 | @plus | @kpplus | @equals when ctrl ->
4818 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4819 setzoom (conf.zoom +. incr)
4821 | @plus | @kpplus ->
4822 let ondone s =
4823 let n =
4824 try int_of_string s with exc ->
4825 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exc;
4826 max_int
4828 if n != max_int
4829 then (
4830 conf.pagebias <- n;
4831 state.text <- "page bias is now " ^ string_of_int n;
4834 enttext ("page bias: ", E.s, None, intentry, ondone, true)
4836 | @minus | @kpminus when ctrl ->
4837 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4838 setzoom (max 0.01 (conf.zoom -. decr))
4840 | @minus | @kpminus ->
4841 let ondone msg = state.text <- msg in
4842 enttext (
4843 "option [acfhilpstvxACFPRSZTISM]: ", E.s, None,
4844 optentry state.mode, ondone, true
4847 | @0 when ctrl ->
4848 if conf.zoom = 1.0
4849 then (
4850 state.x <- 0;
4851 gotoy state.y
4853 else setzoom 1.0
4855 | (@1 | @2) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
4856 let cols =
4857 match conf.columns with
4858 | Csingle _ | Cmulti _ -> 1
4859 | Csplit (n, _) -> n
4861 let h = state.winh -
4862 conf.interpagespace lsl (if conf.presentation then 1 else 0)
4864 let zoom = zoomforh state.winw h (vscrollw ()) cols in
4865 if zoom > 0.0 && (key = @2 || zoom < 1.0)
4866 then setzoom zoom
4868 | @3 when ctrl ->
4869 let fm =
4870 match conf.fitmodel with
4871 | FitWidth -> FitProportional
4872 | FitProportional -> FitPage
4873 | FitPage -> FitWidth
4875 state.text <- "fit model: " ^ FMTE.to_string fm;
4876 reqlayout conf.angle fm
4878 | @4 when ctrl -> (* ctrl-4 *)
4879 let zoom = getmaxw () /. float state.winw in
4880 if zoom > 0.0 then setzoom zoom
4882 | @F9 ->
4883 togglebirdseye ()
4885 | @9 when ctrl ->
4886 togglebirdseye ()
4888 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4889 when not ctrl -> (* 0..9 *)
4890 let ondone s =
4891 let n =
4892 try int_of_string s with exc ->
4893 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exc;
4896 if n >= 0
4897 then (
4898 addnav ();
4899 cbput state.hists.pag (string_of_int n);
4900 gotopage1 (n + conf.pagebias - 1) 0;
4903 let pageentry text key =
4904 match Char.unsafe_chr key with
4905 | 'g' -> TEdone text
4906 | _ -> intentry text key
4908 let text = String.make 1 (Char.chr key) in
4909 enttext (":", text, Some (onhist state.hists.pag),
4910 pageentry, ondone, true)
4912 | @b ->
4913 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
4914 reshape state.winw state.winh;
4916 | @B ->
4917 state.bzoom <- not state.bzoom;
4918 state.rects <- [];
4919 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
4921 | @l ->
4922 conf.hlinks <- not conf.hlinks;
4923 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4924 G.postRedisplay "toggle highlightlinks";
4926 | @F ->
4927 if conf.angle mod 360 = 0
4928 then (
4929 state.glinks <- true;
4930 let mode = state.mode in
4931 state.mode <-
4932 Textentry (
4933 (":", E.s, None, linknentry, linknact gotounder, false),
4934 (fun _ ->
4935 state.glinks <- false;
4936 state.mode <- mode)
4938 state.text <- E.s;
4939 G.postRedisplay "view:linkent(F)"
4941 else impmsg "hint mode does not work under rotation"
4943 | @y ->
4944 state.glinks <- true;
4945 let mode = state.mode in
4946 state.mode <- Textentry (
4948 ":", E.s, None, linknentry, linknact (fun under ->
4949 selstring (undertext under);
4950 ), false
4952 fun _ ->
4953 state.glinks <- false;
4954 state.mode <- mode
4956 state.text <- E.s;
4957 G.postRedisplay "view:linkent"
4959 | @a ->
4960 begin match state.autoscroll with
4961 | Some step ->
4962 conf.autoscrollstep <- step;
4963 state.autoscroll <- None
4964 | None ->
4965 if conf.autoscrollstep = 0
4966 then state.autoscroll <- Some 1
4967 else state.autoscroll <- Some conf.autoscrollstep
4970 | @p when ctrl ->
4971 launchpath () (* XXX where do error messages go? *)
4973 | @P ->
4974 setpresentationmode (not conf.presentation);
4975 showtext ' ' ("presentation mode " ^
4976 if conf.presentation then "on" else "off");
4978 | @f ->
4979 if List.mem Wsi.Fullscreen state.winstate
4980 then Wsi.reshape conf.cwinw conf.cwinh
4981 else Wsi.fullscreen ()
4983 | @p | @N ->
4984 search state.searchpattern false
4986 | @n | @F3 ->
4987 search state.searchpattern true
4989 | @t ->
4990 begin match state.layout with
4991 | [] -> ()
4992 | l :: _ ->
4993 gotoghyll (getpagey l.pageno)
4996 | @space ->
4997 nextpage ()
4999 | @delete | @kpdelete -> (* delete *)
5000 prevpage ()
5002 | @equals ->
5003 showtext ' ' (describe_location ());
5005 | @w ->
5006 begin match state.layout with
5007 | [] -> ()
5008 | l :: _ ->
5009 Wsi.reshape (l.pagew + vscrollw ()) l.pageh;
5010 G.postRedisplay "w"
5013 | @apos ->
5014 enterbookmarkmode ()
5016 | @h | @F1 ->
5017 enterhelpmode ()
5019 | @i ->
5020 enterinfomode ()
5022 | @e when Buffer.length state.errmsgs > 0 ->
5023 entermsgsmode ()
5025 | @m ->
5026 let ondone s =
5027 match state.layout with
5028 | l :: _ ->
5029 if nonemptystr s
5030 then
5031 state.bookmarks <-
5032 (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
5033 | _ -> ()
5035 enttext ("bookmark: ", E.s, None, textentry, ondone, true)
5037 | @tilde ->
5038 quickbookmark ();
5039 showtext ' ' "Quick bookmark added";
5041 | @z ->
5042 begin match state.layout with
5043 | l :: _ ->
5044 let rect = getpdimrect l.pagedimno in
5045 let w, h =
5046 if conf.crophack
5047 then
5048 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5049 truncate (1.2 *. (rect.(3) -. rect.(0))))
5050 else
5051 (truncate (rect.(1) -. rect.(0)),
5052 truncate (rect.(3) -. rect.(0)))
5054 let w = truncate ((float w)*.conf.zoom)
5055 and h = truncate ((float h)*.conf.zoom) in
5056 if w != 0 && h != 0
5057 then (
5058 state.anchor <- getanchor ();
5059 Wsi.reshape (w + vscrollw ()) (h + conf.interpagespace)
5061 G.postRedisplay "z";
5063 | [] -> ()
5066 | @x -> state.roam ()
5068 | @Lt | @Gt ->
5069 reqlayout (conf.angle +
5070 (if key = @Gt then 30 else -30)) conf.fitmodel
5072 | @Lb | @Rb ->
5073 conf.colorscale <-
5074 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5076 G.postRedisplay "brightness";
5078 | @c when state.mode = View ->
5079 if Wsi.withalt mask
5080 then (
5081 if conf.zoom > 1.0
5082 then
5083 let m = (wadjsb () + state.winw - state.w) / 2 in
5084 state.x <- m;
5085 gotoy_and_clear_text state.y
5087 else
5088 let (c, a, b), z =
5089 match state.prevcolumns with
5090 | None -> (1, 0, 0), 1.0
5091 | Some (columns, z) ->
5092 let cab =
5093 match columns with
5094 | Csplit (c, _) -> -c, 0, 0
5095 | Cmulti ((c, a, b), _) -> c, a, b
5096 | Csingle _ -> 1, 0, 0
5098 cab, z
5100 setcolumns View c a b;
5101 setzoom z
5103 | @down | @up when ctrl && Wsi.withshift mask ->
5104 let zoom, x = state.prevzoom in
5105 setzoom zoom;
5106 state.x <- x;
5108 | @k | @up | @kpup ->
5109 begin match state.autoscroll with
5110 | None ->
5111 begin match state.mode with
5112 | Birdseye beye -> upbirdseye 1 beye
5113 | Textentry _
5114 | View
5115 | LinkNav _ ->
5116 if ctrl
5117 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5118 else (
5119 if not (Wsi.withshift mask) && conf.presentation
5120 then prevpage ()
5121 else gotoghyll1 true (clamp (-conf.scrollstep))
5124 | Some n ->
5125 setautoscrollspeed n false
5128 | @j | @down | @kpdown ->
5129 begin match state.autoscroll with
5130 | None ->
5131 begin match state.mode with
5132 | Birdseye beye -> downbirdseye 1 beye
5133 | Textentry _
5134 | View
5135 | LinkNav _ ->
5136 if ctrl
5137 then gotoy_and_clear_text (clamp (state.winh/2))
5138 else (
5139 if not (Wsi.withshift mask) && conf.presentation
5140 then nextpage ()
5141 else gotoghyll1 true (clamp (conf.scrollstep))
5144 | Some n ->
5145 setautoscrollspeed n true
5148 | @left | @right | @kpleft | @kpright when not (Wsi.withalt mask) ->
5149 if canpan ()
5150 then
5151 let dx =
5152 if ctrl
5153 then state.winw / 2
5154 else conf.hscrollstep
5156 let dx = if key = @left || key = @kpleft then dx else -dx in
5157 state.x <- panbound (state.x + dx);
5158 gotoy_and_clear_text state.y
5159 else (
5160 state.text <- E.s;
5161 G.postRedisplay "left/right"
5164 | @prior | @kpprior ->
5165 let y =
5166 if ctrl
5167 then
5168 match state.layout with
5169 | [] -> state.y
5170 | l :: _ -> state.y - l.pagey
5171 else
5172 clamp (pgscale (-state.winh))
5174 gotoghyll y
5176 | @next | @kpnext ->
5177 let y =
5178 if ctrl
5179 then
5180 match List.rev state.layout with
5181 | [] -> state.y
5182 | l :: _ -> getpagey l.pageno
5183 else
5184 clamp (pgscale state.winh)
5186 gotoghyll y
5188 | @g | @home | @kphome ->
5189 addnav ();
5190 gotoghyll 0
5191 | @G | @jend | @kpend ->
5192 addnav ();
5193 gotoghyll (clamp state.maxy)
5195 | @right | @kpright when Wsi.withalt mask ->
5196 gotoghyll (getnav 1)
5197 | @left | @kpleft when Wsi.withalt mask ->
5198 gotoghyll (getnav ~-1)
5200 | @r ->
5201 reload ()
5203 | @v when conf.debug ->
5204 state.rects <- [];
5205 List.iter (fun l ->
5206 match getopaque l.pageno with
5207 | None -> ()
5208 | Some opaque ->
5209 let x0, y0, x1, y1 = pagebbox opaque in
5210 let a,b = float x0, float y0 in
5211 let c,d = float x1, float y0 in
5212 let e,f = float x1, float y1 in
5213 let h,j = float x0, float y1 in
5214 let rect = (a,b,c,d,e,f,h,j) in
5215 debugrect rect;
5216 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
5217 state.rects <- (l.pageno, color, rect) :: state.rects;
5218 ) state.layout;
5219 G.postRedisplay "v";
5221 | @pipe ->
5222 let mode = state.mode in
5223 let cmd = ref E.s in
5224 let onleave = function
5225 | Cancel -> state.mode <- mode
5226 | Confirm ->
5227 List.iter (fun l ->
5228 match getopaque l.pageno with
5229 | Some opaque -> pipesel opaque !cmd
5230 | None -> ()) state.layout;
5231 state.mode <- mode
5233 let ondone s =
5234 cbput state.hists.sel s;
5235 cmd := s
5237 let te =
5238 "| ", !cmd, Some (onhist state.hists.sel), textentry, ondone, true
5240 G.postRedisplay "|";
5241 state.mode <- Textentry (te, onleave);
5243 | _ ->
5244 vlog "huh? %s" (Wsi.keyname key)
5247 let linknavkeyboard key mask linknav =
5248 let getpage pageno =
5249 let rec loop = function
5250 | [] -> None
5251 | l :: _ when l.pageno = pageno -> Some l
5252 | _ :: rest -> loop rest
5253 in loop state.layout
5255 let doexact (pageno, n) =
5256 match getopaque pageno, getpage pageno with
5257 | Some opaque, Some l ->
5258 if key = @enter || key = @kpenter
5259 then
5260 let under = getlink opaque n in
5261 G.postRedisplay "link gotounder";
5262 gotounder under;
5263 state.mode <- View;
5264 else
5265 let opt, dir =
5266 match key with
5267 | @home ->
5268 Some (findlink opaque LDfirst), -1
5270 | @jend ->
5271 Some (findlink opaque LDlast), 1
5273 | @left ->
5274 Some (findlink opaque (LDleft n)), -1
5276 | @right ->
5277 Some (findlink opaque (LDright n)), 1
5279 | @up ->
5280 Some (findlink opaque (LDup n)), -1
5282 | @down ->
5283 Some (findlink opaque (LDdown n)), 1
5285 | _ -> None, 0
5287 let pwl l dir =
5288 begin match findpwl l.pageno dir with
5289 | Pwlnotfound -> ()
5290 | Pwl pageno ->
5291 let notfound dir =
5292 state.mode <- LinkNav (Ltgendir dir);
5293 let y, h = getpageyh pageno in
5294 let y =
5295 if dir < 0
5296 then y + h - state.winh
5297 else y
5299 gotoy y
5301 begin match getopaque pageno, getpage pageno with
5302 | Some opaque, Some _ ->
5303 let link =
5304 let ld = if dir > 0 then LDfirst else LDlast in
5305 findlink opaque ld
5307 begin match link with
5308 | Lfound m ->
5309 showlinktype (getlink opaque m);
5310 state.mode <- LinkNav (Ltexact (pageno, m));
5311 G.postRedisplay "linknav jpage";
5312 | Lnotfound -> notfound dir
5313 end;
5314 | _ -> notfound dir
5315 end;
5316 end;
5318 begin match opt with
5319 | Some Lnotfound -> pwl l dir;
5320 | Some (Lfound m) ->
5321 if m = n
5322 then pwl l dir
5323 else (
5324 let _, y0, _, y1 = getlinkrect opaque m in
5325 if y0 < l.pagey
5326 then gotopage1 l.pageno y0
5327 else (
5328 let d = fstate.fontsize + 1 in
5329 if y1 - l.pagey > l.pagevh - d
5330 then gotopage1 l.pageno (y1 - state.winh - hscrollh () + d)
5331 else G.postRedisplay "linknav";
5333 showlinktype (getlink opaque m);
5334 state.mode <- LinkNav (Ltexact (l.pageno, m));
5337 | None -> viewkeyboard key mask
5338 end;
5339 | _ -> viewkeyboard key mask
5341 if key = @insert
5342 then (
5343 state.mode <- View;
5344 G.postRedisplay "leave linknav"
5346 else
5347 match linknav with
5348 | Ltgendir _ | Ltnotready _ -> viewkeyboard key mask
5349 | Ltexact exact -> doexact exact
5352 let keyboard key mask =
5353 if (key = @g && Wsi.withctrl mask) && not (istextentry state.mode)
5354 then wcmd "interrupt"
5355 else state.uioh <- state.uioh#key key mask
5358 let birdseyekeyboard key mask
5359 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5360 let incr =
5361 match conf.columns with
5362 | Csingle _ -> 1
5363 | Cmulti ((c, _, _), _) -> c
5364 | Csplit _ -> failwith "bird's eye split mode"
5366 let pgh layout = List.fold_left
5367 (fun m l -> max l.pageh m) state.winh layout in
5368 match key with
5369 | @l when Wsi.withctrl mask ->
5370 let y, h = getpageyh pageno in
5371 let top = (state.winh - h) / 2 in
5372 gotoy (max 0 (y - top))
5373 | @enter | @kpenter -> leavebirdseye beye false
5374 | @escape -> leavebirdseye beye true
5375 | @up -> upbirdseye incr beye
5376 | @down -> downbirdseye incr beye
5377 | @left -> upbirdseye 1 beye
5378 | @right -> downbirdseye 1 beye
5380 | @prior ->
5381 begin match state.layout with
5382 | l :: _ ->
5383 if l.pagey != 0
5384 then (
5385 state.mode <- Birdseye (
5386 oconf, leftx, l.pageno, hooverpageno, anchor
5388 gotopage1 l.pageno 0;
5390 else (
5391 let layout = layout state.x (state.y-state.winh)
5392 state.winw
5393 (pgh state.layout) in
5394 match layout with
5395 | [] -> gotoy (clamp (-state.winh))
5396 | l :: _ ->
5397 state.mode <- Birdseye (
5398 oconf, leftx, l.pageno, hooverpageno, anchor
5400 gotopage1 l.pageno 0
5403 | [] -> gotoy (clamp (-state.winh))
5404 end;
5406 | @next ->
5407 begin match List.rev state.layout with
5408 | l :: _ ->
5409 let layout = layout state.x
5410 (state.y + (pgh state.layout))
5411 state.winw state.winh in
5412 begin match layout with
5413 | [] ->
5414 let incr = l.pageh - l.pagevh in
5415 if incr = 0
5416 then (
5417 state.mode <-
5418 Birdseye (
5419 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5421 G.postRedisplay "birdseye pagedown";
5423 else gotoy (clamp (incr + conf.interpagespace*2));
5425 | l :: _ ->
5426 state.mode <-
5427 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5428 gotopage1 l.pageno 0;
5431 | [] -> gotoy (clamp state.winh)
5432 end;
5434 | @home ->
5435 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5436 gotopage1 0 0
5438 | @jend ->
5439 let pageno = state.pagecount - 1 in
5440 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5441 if not (pagevisible state.layout pageno)
5442 then
5443 let h =
5444 match List.rev state.pdims with
5445 | [] -> state.winh
5446 | (_, _, h, _) :: _ -> h
5448 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5449 else G.postRedisplay "birdseye end";
5451 | _ -> viewkeyboard key mask
5454 let drawpage l =
5455 let color =
5456 match state.mode with
5457 | Textentry _ -> scalecolor 0.4
5458 | LinkNav _
5459 | View -> scalecolor 1.0
5460 | Birdseye (_, _, pageno, hooverpageno, _) ->
5461 if l.pageno = hooverpageno
5462 then scalecolor 0.9
5463 else (
5464 if l.pageno = pageno
5465 then (
5466 let c = scalecolor 1.0 in
5467 GlDraw.color c;
5468 GlDraw.line_width 3.0;
5469 let dispx = xadjsb () + l.pagedispx in
5470 linerect
5471 (float (dispx-1)) (float (l.pagedispy-1))
5472 (float (dispx+l.pagevw+1))
5473 (float (l.pagedispy+l.pagevh+1))
5475 GlDraw.line_width 1.0;
5478 else scalecolor 0.8
5481 drawtiles l color;
5484 let postdrawpage l linkindexbase =
5485 match getopaque l.pageno with
5486 | Some opaque ->
5487 if tileready l l.pagex l.pagey
5488 then
5489 let x = l.pagedispx - l.pagex + xadjsb ()
5490 and y = l.pagedispy - l.pagey in
5491 let hlmask =
5492 match conf.columns with
5493 | Csingle _ | Cmulti _ ->
5494 (if conf.hlinks then 1 else 0)
5495 + (if state.glinks
5496 && not (isbirdseye state.mode) then 2 else 0)
5497 | Csplit _ -> 0
5499 let s =
5500 match state.mode with
5501 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5502 | Textentry _
5503 | Birdseye _
5504 | View
5505 | LinkNav _ -> E.s
5507 Hashtbl.find_all state.prects l.pageno |>
5508 List.iter (fun vals -> drawprect opaque x y vals);
5509 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5510 else 0
5511 | _ -> 0
5514 let scrollindicator () =
5515 let sbw, ph, sh = state.uioh#scrollph in
5516 let sbh, pw, sw = state.uioh#scrollpw in
5518 let x0,x1,hx0 =
5519 if conf.leftscroll
5520 then (0, sbw, sbw)
5521 else ((state.winw - sbw), state.winw, 0)
5524 GlDraw.color (0.64, 0.64, 0.64);
5525 filledrect (float x0) 0. (float x1) (float state.winh);
5526 filledrect
5527 (float hx0) (float (state.winh - sbh))
5528 (float (hx0 + wadjsb () + state.winw)) (float state.winh)
5530 GlDraw.color (0.0, 0.0, 0.0);
5532 filledrect (float x0) ph (float x1) (ph +. sh);
5533 let pw = pw +. float hx0 in
5534 filledrect pw (float (state.winh - sbh)) (pw +. sw) (float state.winh);
5537 let showsel () =
5538 match state.mstate with
5539 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5542 | Msel ((x0, y0), (x1, y1)) ->
5543 let identify opaque l px py = Some (opaque, l.pageno, px, py) in
5544 let o0,n0,px0,py0 = onppundermouse identify x0 y0 (~< E.s, -1, 0, 0) in
5545 let _o1,n1,px1,py1 = onppundermouse identify x1 y1 (~< E.s, -1, 0, 0) in
5546 if n0 != -1 && n0 = n1 then seltext o0 (px0, py0, px1, py1);
5549 let showrects = function [] -> () | rects ->
5550 Gl.enable `blend;
5551 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5552 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5553 List.iter
5554 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5555 List.iter (fun l ->
5556 if l.pageno = pageno
5557 then (
5558 let dx = float (l.pagedispx - l.pagex) in
5559 let dy = float (l.pagedispy - l.pagey) in
5560 let r, g, b, alpha = c in
5561 GlDraw.color (r, g, b) ~alpha;
5562 filledrect2 (x0+.dx) (y0+.dy)
5563 (x1+.dx) (y1+.dy)
5564 (x3+.dx) (y3+.dy)
5565 (x2+.dx) (y2+.dy);
5567 ) state.layout
5568 ) rects
5570 Gl.disable `blend;
5573 let display () =
5574 begin match conf.columns, state.layout with
5575 | Csingle _, _ :: _ ->
5576 GlDraw.color (scalecolor2 conf.bgcolor);
5577 let y =
5578 List.fold_left (fun y l ->
5579 let x0 = 0 in
5580 let y0 = y in
5581 let x1 = l.pagedispx + xadjsb () in
5582 let y1 = (l.pagedispy + l.pagevh) in
5583 filledrect (float x0) (float y0) (float x1) (float y1);
5584 let x0 = x1 + l.pagevw in
5585 let x1 = state.winw in
5586 filledrect1 (float x0) (float y0) (float x1) (float y1);
5587 if y != l.pagedispy
5588 then (
5589 let x0 = 0
5590 and x1 = state.winw in
5591 let y0 = y
5592 and y1 = l.pagedispy in
5593 filledrect1 (float x0) (float y0) (float x1) (float y1);
5595 l.pagedispy + l.pagevh) 0 state.layout
5597 let x0 = 0
5598 and x1 = state.winw in
5599 let y0 = y
5600 and y1 = state.winh in
5601 filledrect1 (float x0) (float y0) (float x1) (float y1)
5602 | (Cmulti _ | Csplit _), _ | Csingle _, [] ->
5603 GlClear.color (scalecolor2 conf.bgcolor);
5604 GlClear.clear [`color];
5605 end;
5606 List.iter drawpage state.layout;
5607 let rects =
5608 match state.mode with
5609 | LinkNav (Ltexact (pageno, linkno)) ->
5610 begin match getopaque pageno with
5611 | Some opaque ->
5612 let dx = xadjsb () in
5613 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5614 let x0 = x0 + dx and x1 = x1 + dx in
5615 let color = (0.0, 0.0, 0.5, 0.5) in
5616 (pageno, color, (
5617 float x0, float y0,
5618 float x1, float y0,
5619 float x1, float y1,
5620 float x0, float y1)
5621 ) :: state.rects
5622 | None -> state.rects
5624 | LinkNav (Ltgendir _) | LinkNav (Ltnotready _)
5625 | Birdseye _
5626 | Textentry _
5627 | View -> state.rects
5629 showrects rects;
5630 let rec postloop linkindexbase = function
5631 | l :: rest ->
5632 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5633 postloop linkindexbase rest
5634 | [] -> ()
5636 showsel ();
5637 postloop 0 state.layout;
5638 state.uioh#display;
5639 begin match state.mstate with
5640 | Mzoomrect ((x0, y0), (x1, y1)) ->
5641 Gl.enable `blend;
5642 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5643 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5644 filledrect (float x0) (float y0) (float x1) (float y1);
5645 Gl.disable `blend;
5646 | Msel _
5647 | Mpan _
5648 | Mscrolly | Mscrollx
5649 | Mzoom _
5650 | Mnone -> ()
5651 end;
5652 enttext ();
5653 scrollindicator ();
5654 Wsi.swapb ();
5657 let zoomrect x y x1 y1 =
5658 let x0 = min x x1
5659 and x1 = max x x1
5660 and y0 = min y y1 in
5661 gotoy (state.y + y0);
5662 state.anchor <- getanchor ();
5663 let zoom = (float state.w) /. float (x1 - x0) in
5664 let margin =
5665 let simple () =
5666 let adjw = wadjsb () + state.winw in
5667 if state.w < adjw
5668 then (adjw - state.w) / 2
5669 else 0
5671 match conf.fitmodel with
5672 | FitWidth | FitProportional -> simple ()
5673 | FitPage ->
5674 match conf.columns with
5675 | Csplit _ ->
5676 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
5677 | Cmulti _ | Csingle _ -> simple ()
5679 state.x <- (state.x + margin) - x0;
5680 setzoom zoom;
5681 resetmstate ();
5684 let annot inline x y =
5685 match unproject x y with
5686 | Some (opaque, n, ux, uy) ->
5687 let add text =
5688 addannot opaque ux uy text;
5689 wcmd "freepage %s" (~> opaque);
5690 Hashtbl.remove state.pagemap (n, state.gen);
5691 flushtiles ();
5692 gotoy state.y
5694 if inline
5695 then
5696 let ondone s = add s in
5697 let mode = state.mode in
5698 state.mode <- Textentry (
5699 ("annotation: ", E.s, None, textentry, ondone, true),
5700 fun _ -> state.mode <- mode);
5701 state.text <- E.s;
5702 enttext ();
5703 G.postRedisplay "annot"
5704 else
5705 add @@ getusertext E.s
5706 | _ -> ()
5709 let zoomblock x y =
5710 let g opaque l px py =
5711 match rectofblock opaque px py with
5712 | Some a ->
5713 let x0 = a.(0) -. 20. in
5714 let x1 = a.(1) +. 20. in
5715 let y0 = a.(2) -. 20. in
5716 let zoom = (float state.w) /. (x1 -. x0) in
5717 let pagey = getpagey l.pageno in
5718 gotoy_and_clear_text (pagey + truncate y0);
5719 state.anchor <- getanchor ();
5720 let margin = (state.w - l.pagew)/2 in
5721 state.x <- -truncate x0 - margin;
5722 setzoom zoom;
5723 None
5724 | None -> None
5726 match conf.columns with
5727 | Csplit _ ->
5728 impmsg "block zooming does not work properly in split columns mode"
5729 | Cmulti _ | Csingle _ -> onppundermouse g x y ()
5732 let scrollx x =
5733 let winw = wadjsb () + state.winw - 1 in
5734 let s = float x /. float winw in
5735 let destx = truncate (float (state.w + winw) *. s) in
5736 state.x <- winw - destx;
5737 gotoy_and_clear_text state.y;
5738 state.mstate <- Mscrollx;
5741 let scrolly y =
5742 let s = float y /. float state.winh in
5743 let desty = truncate (float (state.maxy - state.winh) *. s) in
5744 gotoy_and_clear_text desty;
5745 state.mstate <- Mscrolly;
5748 let viewmulticlick clicks x y mask =
5749 let g opaque l px py =
5750 let mark =
5751 match clicks with
5752 | 2 -> Mark_word
5753 | 3 -> Mark_line
5754 | 4 -> Mark_block
5755 | _ -> Mark_page
5757 if markunder opaque px py mark
5758 then (
5759 Some (fun () ->
5760 let dopipe cmd =
5761 match getopaque l.pageno with
5762 | None -> ()
5763 | Some opaque -> pipesel opaque cmd
5765 state.roam <- (fun () -> dopipe conf.paxcmd);
5766 if not (Wsi.withctrl mask) then dopipe conf.selcmd;
5769 else None
5771 G.postRedisplay "viewmulticlick";
5772 onppundermouse g x y (fun () -> impmsg "nothing to select") ();
5775 let canselect () =
5776 match conf.columns with
5777 | Csplit _ -> false
5778 | Csingle _ | Cmulti _ -> conf.angle mod 360 = 0
5781 let viewmouse button down x y mask =
5782 match button with
5783 | n when (n == 4 || n == 5) && not down ->
5784 if Wsi.withctrl mask
5785 then (
5786 match state.mstate with
5787 | Mzoom (oldn, i) ->
5788 if oldn = n
5789 then (
5790 if i = 2
5791 then
5792 let incr =
5793 match n with
5794 | 5 ->
5795 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5796 | _ ->
5797 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5799 let zoom = conf.zoom -. incr in
5800 setzoom zoom;
5801 state.mstate <- Mzoom (n, 0);
5802 else
5803 state.mstate <- Mzoom (n, i+1);
5805 else state.mstate <- Mzoom (n, 0)
5807 | Msel _
5808 | Mpan _
5809 | Mscrolly | Mscrollx
5810 | Mzoomrect _
5811 | Mnone -> state.mstate <- Mzoom (n, 0)
5813 else (
5814 match state.autoscroll with
5815 | Some step -> setautoscrollspeed step (n=4)
5816 | None ->
5817 if conf.wheelbypage || conf.presentation
5818 then (
5819 if n = 4
5820 then prevpage ()
5821 else nextpage ()
5823 else
5824 let incr =
5825 if n = 4
5826 then -conf.scrollstep
5827 else conf.scrollstep
5829 let incr = incr * 2 in
5830 let y = clamp incr in
5831 gotoy_and_clear_text y
5834 | n when (n = 6 || n = 7) && not down && canpan () ->
5835 state.x <-
5836 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
5837 gotoy_and_clear_text state.y
5839 | 1 when Wsi.withshift mask ->
5840 state.mstate <- Mnone;
5841 if not down
5842 then (
5843 match unproject x y with
5844 | None -> ()
5845 | Some (_, pageno, ux, uy) ->
5846 let cmd = Printf.sprintf
5847 "%s %s %d %d %d"
5848 conf.stcmd state.path pageno ux uy
5850 match spawn cmd [] with
5851 | (exception exn) ->
5852 impmsg "execution of synctex command(%S) failed: %S"
5853 conf.stcmd @@ exntos exn
5854 | _pid -> ()
5857 | 1 when Wsi.withctrl mask ->
5858 if down
5859 then (
5860 Wsi.setcursor Wsi.CURSOR_FLEUR;
5861 state.mstate <- Mpan (x, y)
5863 else
5864 state.mstate <- Mnone
5866 | 3 ->
5867 if down
5868 then (
5869 if Wsi.withshift mask
5870 then (
5871 annot conf.annotinline x y;
5872 G.postRedisplay "addannot"
5874 else
5875 let p = (x, y) in
5876 Wsi.setcursor Wsi.CURSOR_CYCLE;
5877 state.mstate <- Mzoomrect (p, p)
5879 else (
5880 match state.mstate with
5881 | Mzoomrect ((x0, y0), _) ->
5882 if abs (x-x0) > 10 && abs (y - y0) > 10
5883 then zoomrect x0 y0 x y
5884 else (
5885 resetmstate ();
5886 G.postRedisplay "kill accidental zoom rect";
5888 | Msel _
5889 | Mpan _
5890 | Mscrolly | Mscrollx
5891 | Mzoom _
5892 | Mnone ->
5893 resetmstate ()
5896 | 1 when vscrollhit x ->
5897 if down
5898 then
5899 let _, position, sh = state.uioh#scrollph in
5900 if y > truncate position && y < truncate (position +. sh)
5901 then state.mstate <- Mscrolly
5902 else scrolly y
5903 else
5904 state.mstate <- Mnone
5906 | 1 when y > state.winh - hscrollh () ->
5907 if down
5908 then
5909 let _, position, sw = state.uioh#scrollpw in
5910 if x > truncate position && x < truncate (position +. sw)
5911 then state.mstate <- Mscrollx
5912 else scrollx x
5913 else
5914 state.mstate <- Mnone
5916 | 1 when state.bzoom -> if not down then zoomblock x y
5918 | 1 ->
5919 let dest = if down then getunder x y else Unone in
5920 begin match dest with
5921 | Ulinkgoto _
5922 | Ulinkuri _
5923 | Uremote _ | Uremotedest _
5924 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5925 gotounder dest
5927 | Unone when down ->
5928 Wsi.setcursor Wsi.CURSOR_FLEUR;
5929 state.mstate <- Mpan (x, y);
5931 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
5933 | Unone | Utext _ ->
5934 if down
5935 then (
5936 if canselect ()
5937 then (
5938 state.mstate <- Msel ((x, y), (x, y));
5939 G.postRedisplay "mouse select";
5942 else (
5943 match state.mstate with
5944 | Mnone -> ()
5946 | Mzoom _ | Mscrollx | Mscrolly ->
5947 state.mstate <- Mnone
5949 | Mzoomrect ((x0, y0), _) ->
5950 zoomrect x0 y0 x y
5952 | Mpan _ ->
5953 Wsi.setcursor Wsi.CURSOR_INHERIT;
5954 state.mstate <- Mnone
5956 | Msel ((x0, y0), (x1, y1)) ->
5957 let rec loop = function
5958 | [] -> ()
5959 | l :: rest ->
5960 let inside =
5961 let a0 = l.pagedispy in
5962 let a1 = a0 + l.pagevh in
5963 let b0 = l.pagedispx in
5964 let b1 = b0 + l.pagevw in
5965 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5966 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5968 if inside
5969 then
5970 match getopaque l.pageno with
5971 | Some opaque ->
5972 let dosel cmd () =
5973 match Unix.pipe () with
5974 | (exception exn) ->
5975 impmsg "cannot create sel pipe: %s" @@
5976 exntos exn;
5977 | (r, w) ->
5978 let clo what fd =
5979 Ne.clo fd (fun msg ->
5980 dolog "%s close failed: %s" what msg)
5982 let pid =
5983 try spawn cmd [r, 0; w, -1]
5984 with exn ->
5985 dolog "cannot execute %S: %s"
5986 cmd @@ exntos exn;
5989 if pid > 0
5990 then (
5991 copysel w opaque;
5992 G.postRedisplay "copysel";
5994 else clo "Msel pipe/w" w;
5995 clo "Msel pipe/r" r;
5997 dosel conf.selcmd ();
5998 state.roam <- dosel conf.paxcmd;
5999 | None -> ()
6000 else loop rest
6002 loop state.layout;
6003 resetmstate ();
6007 | _ -> ()
6010 let birdseyemouse button down x y mask
6011 (conf, leftx, _, hooverpageno, anchor) =
6012 match button with
6013 | 1 when down ->
6014 let rec loop = function
6015 | [] -> ()
6016 | l :: rest ->
6017 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6018 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6019 then (
6020 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
6022 else loop rest
6024 loop state.layout
6025 | 3 -> ()
6026 | _ -> viewmouse button down x y mask
6029 let uioh = object
6030 method display = ()
6032 method key key mask =
6033 begin match state.mode with
6034 | Textentry textentry -> textentrykeyboard key mask textentry
6035 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6036 | View -> viewkeyboard key mask
6037 | LinkNav linknav -> linknavkeyboard key mask linknav
6038 end;
6039 state.uioh
6041 method button button bstate x y mask =
6042 begin match state.mode with
6043 | LinkNav _
6044 | View -> viewmouse button bstate x y mask
6045 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6046 | Textentry _ -> ()
6047 end;
6048 state.uioh
6050 method multiclick clicks x y mask =
6051 begin match state.mode with
6052 | LinkNav _
6053 | View -> viewmulticlick clicks x y mask
6054 | Birdseye _
6055 | Textentry _ -> ()
6056 end;
6057 state.uioh
6059 method motion x y =
6060 begin match state.mode with
6061 | Textentry _ -> ()
6062 | View | Birdseye _ | LinkNav _ ->
6063 match state.mstate with
6064 | Mzoom _ | Mnone -> ()
6066 | Mpan (x0, y0) ->
6067 let dx = x - x0
6068 and dy = y0 - y in
6069 state.mstate <- Mpan (x, y);
6070 if canpan ()
6071 then state.x <- panbound (state.x + dx);
6072 let y = clamp dy in
6073 gotoy_and_clear_text y
6075 | Msel (a, _) ->
6076 state.mstate <- Msel (a, (x, y));
6077 G.postRedisplay "motion select";
6079 | Mscrolly ->
6080 let y = min state.winh (max 0 y) in
6081 scrolly y
6083 | Mscrollx ->
6084 let x = min state.winw (max 0 x) in
6085 scrollx x
6087 | Mzoomrect (p0, _) ->
6088 state.mstate <- Mzoomrect (p0, (x, y));
6089 G.postRedisplay "motion zoomrect";
6090 end;
6091 state.uioh
6093 method pmotion x y =
6094 begin match state.mode with
6095 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6096 let rec loop = function
6097 | [] ->
6098 if hooverpageno != -1
6099 then (
6100 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6101 G.postRedisplay "pmotion birdseye no hoover";
6103 | l :: rest ->
6104 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6105 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6106 then (
6107 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6108 G.postRedisplay "pmotion birdseye hoover";
6110 else loop rest
6112 loop state.layout
6114 | Textentry _ -> ()
6116 | LinkNav _
6117 | View ->
6118 match state.mstate with
6119 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ -> ()
6120 | Mnone ->
6121 updateunder x y;
6122 if canselect ()
6123 then
6124 match conf.pax with
6125 | None -> ()
6126 | Some r ->
6127 let past, _, _ = !r in
6128 let now = now () in
6129 let delta = now -. past in
6130 if delta > 0.01
6131 then paxunder x y
6132 else r := (now, x, y)
6133 end;
6134 state.uioh
6136 method infochanged _ = ()
6138 method scrollph =
6139 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6140 let p, h =
6141 if maxy = 0
6142 then 0.0, float state.winh
6143 else scrollph state.y maxy
6145 vscrollw (), p, h
6147 method scrollpw =
6148 let winw = wadjsb () + state.winw in
6149 let fwinw = float winw in
6150 let sw =
6151 let sw = fwinw /. float state.w in
6152 let sw = fwinw *. sw in
6153 max sw (float conf.scrollh)
6155 let position =
6156 let maxx = state.w + winw in
6157 let x = winw - state.x in
6158 let percent = float x /. float maxx in
6159 (fwinw -. sw) *. percent
6161 hscrollh (), position, sw
6163 method modehash =
6164 let modename =
6165 match state.mode with
6166 | LinkNav _ -> "links"
6167 | Textentry _ -> "textentry"
6168 | Birdseye _ -> "birdseye"
6169 | View -> "view"
6171 findkeyhash conf modename
6173 method eformsgs = true
6174 method alwaysscrolly = false
6175 end;;
6177 let addrect pageno r g b a x0 y0 x1 y1 =
6178 Hashtbl.add state.prects pageno [|r; g; b; a; x0; y0; x1; y1|];
6181 let ract cmds =
6182 let cl = splitatspace cmds in
6183 let scan s fmt f =
6184 try Scanf.sscanf s fmt f
6185 with exn ->
6186 adderrfmt "remote exec"
6187 "error processing '%S': %s\n" cmds @@ exntos exn
6189 let rectx s pageno (r, g, b, a) x0 y0 x1 y1 =
6190 vlog "%s page %d color (%f %f %f %f) x0,y0,x1,y1 = %f %f %f %f"
6191 s pageno r g b a x0 y0 x1 y1;
6192 onpagerect
6193 pageno
6194 (fun w h ->
6195 let _,w1,h1,_ = getpagedim pageno in
6196 let sw = float w1 /. float w
6197 and sh = float h1 /. float h in
6198 let x0s = x0 *. sw
6199 and x1s = x1 *. sw
6200 and y0s = y0 *. sh
6201 and y1s = y1 *. sh in
6202 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
6203 let color = (r, g, b, a) in
6204 if conf.verbose then debugrect rect;
6205 state.rects <- (pageno, color, rect) :: state.rects;
6206 G.postRedisplay s;
6209 match cl with
6210 | "reload" :: [] -> reload ()
6211 | "goto" :: args :: [] ->
6212 scan args "%u %f %f"
6213 (fun pageno x y ->
6214 let cmd, _ = state.geomcmds in
6215 if emptystr cmd
6216 then gotopagexy !wtmode pageno x y
6217 else
6218 let f prevf () =
6219 gotopagexy !wtmode pageno x y;
6220 prevf ()
6222 state.reprf <- f state.reprf
6224 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
6225 | "gotor" :: args :: [] ->
6226 scan args "%S %u"
6227 (fun filename pageno -> gotounder (Uremote (filename, pageno)))
6228 | "gotord" :: args :: [] ->
6229 scan args "%S %S"
6230 (fun filename dest -> gotounder (Uremotedest (filename, dest)))
6231 | "rect" :: args :: [] ->
6232 scan args "%u %u %f %f %f %f"
6233 (fun pageno c x0 y0 x1 y1 ->
6234 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
6235 rectx "rect" pageno color x0 y0 x1 y1;
6237 | "prect" :: args :: [] ->
6238 scan args "%u %f %f %f %f %f %f %f %f"
6239 (fun pageno r g b alpha x0 y0 x1 y1 ->
6240 addrect pageno r g b alpha x0 y0 x1 y1;
6241 G.postRedisplay "prect"
6243 | "pgoto" :: args :: [] ->
6244 scan args "%u %f %f"
6245 (fun pageno x y ->
6246 let optopaque =
6247 match getopaque pageno with
6248 | Some opaque -> opaque
6249 | None -> ~< E.s
6251 pgoto optopaque pageno x y;
6252 let rec fixx = function
6253 | [] -> ()
6254 | l :: rest ->
6255 if l.pageno = pageno
6256 then (
6257 state.x <- state.x - l.pagedispx;
6258 gotoy state.y;
6260 else fixx rest
6262 let layout =
6263 let mult =
6264 match conf.columns with
6265 | Csingle _ | Csplit _ -> 1
6266 | Cmulti ((n, _, _), _) -> n
6268 layout 0 state.y (state.winw * mult) state.winh
6270 fixx layout
6272 | "activatewin" :: [] -> Wsi.activatewin ()
6273 | "quit" :: [] -> raise Quit
6274 | "clearrects" :: [] ->
6275 Hashtbl.clear state.prects;
6276 G.postRedisplay "clearrects"
6277 | _ ->
6278 adderrfmt "remote command"
6279 "error processing remote command: %S\n" cmds;
6282 let remote =
6283 let scratch = Bytes.create 80 in
6284 let buf = Buffer.create 80 in
6285 fun fd ->
6286 match tempfailureretry (Unix.read fd scratch 0) 80 with
6287 | (exception Unix.Unix_error (Unix.EAGAIN, _, _)) -> None
6288 | 0 ->
6289 Unix.close fd;
6290 if Buffer.length buf > 0
6291 then (
6292 let s = Buffer.contents buf in
6293 Buffer.clear buf;
6294 ract s;
6296 None
6297 | n ->
6298 let rec eat ppos =
6299 let nlpos =
6300 match Bytes.index_from scratch ppos '\n' with
6301 | pos -> if pos >= n then -1 else pos
6302 | (exception Not_found) -> -1
6304 if nlpos >= 0
6305 then (
6306 Buffer.add_subbytes buf scratch ppos (nlpos-ppos);
6307 let s = Buffer.contents buf in
6308 Buffer.clear buf;
6309 ract s;
6310 eat (nlpos+1);
6312 else (
6313 Buffer.add_subbytes buf scratch ppos (n-ppos);
6314 Some fd
6316 in eat 0
6319 let remoteopen path =
6320 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
6321 with exn ->
6322 adderrfmt "remoteopen" "error opening %S: %s" path @@ exntos exn;
6323 None
6326 let () =
6327 let gcconfig = ref E.s in
6328 let trimcachepath = ref E.s in
6329 let rcmdpath = ref E.s in
6330 let pageno = ref None in
6331 let rootwid = ref 0 in
6332 let openlast = ref false in
6333 let nofc = ref false in
6334 let doreap = ref false in
6335 selfexec := Sys.executable_name;
6336 Arg.parse
6337 (Arg.align
6338 [("-p", Arg.String (fun s -> state.password <- s),
6339 "<password> Set password");
6341 ("-f", Arg.String
6342 (fun s ->
6343 Config.fontpath := s;
6344 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
6346 "<path> Set path to the user interface font");
6348 ("-c", Arg.String
6349 (fun s ->
6350 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
6351 Config.confpath := s),
6352 "<path> Set path to the configuration file");
6354 ("-last", Arg.Set openlast, " Open last document");
6356 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
6357 "<page-number> Jump to page");
6359 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6360 "<path> Set path to the trim cache file");
6362 ("-dest", Arg.String (fun s -> state.nameddest <- s),
6363 "<named-destination> Set named destination");
6365 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
6366 ("-cxack", Arg.Set cxack, " Cut corners");
6368 ("-remote", Arg.String (fun s -> rcmdpath := s),
6369 "<path> Set path to the remote commands source");
6371 ("-origin", Arg.String (fun s -> state.origin <- s),
6372 "<original-path> Set original path");
6374 ("-gc", Arg.Set_string gcconfig,
6375 "<script-path> Collect garbage with the help of a script");
6377 ("-nofc", Arg.Set nofc, " Do not use fontconfig");
6379 ("-v", Arg.Unit (fun () ->
6380 Printf.printf
6381 "%s\nconfiguration path: %s\n"
6382 (version ())
6383 Config.defconfpath
6385 exit 0), " Print version and exit");
6387 ("-embed", Arg.Set_int rootwid,
6388 "<window-id> Embed into window")
6391 (fun s -> state.path <- s)
6392 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6394 if !wtmode
6395 then selfexec := !selfexec ^ " -wtmode";
6397 let histmode = emptystr state.path && not !openlast in
6399 if not (Config.load !openlast)
6400 then dolog "failed to load configuration";
6401 begin match !pageno with
6402 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
6403 | None -> ()
6404 end;
6406 if nonemptystr !gcconfig
6407 then (
6408 let (c, s) =
6409 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6410 | (exception exn) -> error "socketpair for gc failed: %s" @@ exntos exn
6411 | fds -> fds
6413 match spawn !gcconfig [(c, 0); (c, 1); (s, -1)] with
6414 | (exception exn) -> error "failed to execute gc script: %s" @@ exntos exn
6415 | _pid ->
6416 Ne.clo c @@ (fun s -> error "failed to close gc fd %s" s);
6417 Config.gc s;
6418 exit 0
6421 let wsfd, winw, winh = Wsi.init (object (self)
6422 val mutable m_clicks = 0
6423 val mutable m_click_x = 0
6424 val mutable m_click_y = 0
6425 val mutable m_lastclicktime = infinity
6427 method private cleanup =
6428 state.roam <- noroam;
6429 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap
6430 method expose = G.postRedisplay "expose"
6431 method visible v =
6432 let name =
6433 match v with
6434 | Wsi.Unobscured -> "unobscured"
6435 | Wsi.PartiallyObscured -> "partiallyobscured"
6436 | Wsi.FullyObscured -> "fullyobscured"
6438 vlog "visibility change %s" name
6439 method display = display ()
6440 method map mapped = vlog "mapped %b" mapped
6441 method reshape w h =
6442 self#cleanup;
6443 reshape w h
6444 method mouse b d x y m =
6445 if d && canselect ()
6446 then (
6447 (* http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx *)
6448 m_click_x <- x;
6449 m_click_y <- y;
6450 if b = 1
6451 then (
6452 let t = now () in
6453 if abs x - m_click_x > 10
6454 || abs y - m_click_y > 10
6455 || abs_float (t -. m_lastclicktime) > 0.3
6456 then m_clicks <- 0;
6457 m_clicks <- m_clicks + 1;
6458 m_lastclicktime <- t;
6459 if m_clicks = 1
6460 then (
6461 self#cleanup;
6462 G.postRedisplay "cleanup";
6463 state.uioh <- state.uioh#button b d x y m;
6465 else state.uioh <- state.uioh#multiclick m_clicks x y m
6467 else (
6468 self#cleanup;
6469 m_clicks <- 0;
6470 m_lastclicktime <- infinity;
6471 state.uioh <- state.uioh#button b d x y m
6474 else (
6475 state.uioh <- state.uioh#button b d x y m
6477 method motion x y =
6478 state.mpos <- (x, y);
6479 state.uioh <- state.uioh#motion x y
6480 method pmotion x y =
6481 state.mpos <- (x, y);
6482 state.uioh <- state.uioh#pmotion x y
6483 method key k m =
6484 let mascm = m land (
6485 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6486 ) in
6487 let keyboard k m =
6488 let x = state.x and y = state.y in
6489 keyboard k m;
6490 if x != state.x || y != state.y then self#cleanup
6492 match state.keystate with
6493 | KSnone ->
6494 let km = k, mascm in
6495 begin
6496 match
6497 let modehash = state.uioh#modehash in
6498 try Hashtbl.find modehash km
6499 with Not_found ->
6500 try Hashtbl.find (findkeyhash conf "global") km
6501 with Not_found -> KMinsrt (k, m)
6502 with
6503 | KMinsrt (k, m) -> keyboard k m
6504 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6505 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6507 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6508 List.iter (fun (k, m) -> keyboard k m) insrt;
6509 state.keystate <- KSnone
6510 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6511 state.keystate <- KSinto (keys, insrt)
6512 | KSinto _ -> state.keystate <- KSnone
6514 method enter x y =
6515 state.mpos <- (x, y);
6516 state.uioh <- state.uioh#pmotion x y
6517 method leave = state.mpos <- (-1, -1)
6518 method winstate wsl = state.winstate <- wsl
6519 method quit = raise Quit
6520 end) !rootwid conf.cwinw conf.cwinh platform in
6522 setbgcol conf.bgcolor;
6523 state.wsfd <- wsfd;
6525 if not (
6526 List.exists GlMisc.check_extension
6527 [ "GL_ARB_texture_rectangle"
6528 ; "GL_EXT_texture_recangle"
6529 ; "GL_NV_texture_rectangle" ]
6531 then (dolog "OpenGL does not suppport rectangular textures"; exit 1);
6533 if (
6534 let r = GlMisc.get_string `renderer in
6535 let p = "Mesa DRI Intel(" in
6536 let l = String.length p in
6537 String.length r > l && String.sub r 0 l = p
6539 then (
6540 defconf.sliceheight <- 1024;
6541 defconf.texcount <- 32;
6542 defconf.usepbo <- true;
6545 let cs, ss =
6546 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6547 | (exception exn) ->
6548 dolog "socketpair failed: %s" @@ exntos exn;
6549 exit 1
6550 | (r, w) ->
6551 cloexec r;
6552 cloexec w;
6553 r, w
6556 setcheckers conf.checkers;
6558 opengl_has_pbo := GlMisc.check_extension "GL_ARB_pixel_buffer_object";
6560 init cs (
6561 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
6562 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6563 !Config.fontpath, !trimcachepath,
6564 !opengl_has_pbo,
6565 not !nofc
6567 List.iter GlArray.enable [`texture_coord; `vertex];
6568 state.ss <- ss;
6569 reshape ~firsttime:true winw winh;
6570 state.uioh <- uioh;
6571 if histmode
6572 then (
6573 Wsi.settitle "llpp (history)";
6574 enterhistmode ();
6576 else (
6577 state.text <- "Opening " ^ (mbtoutf8 state.path);
6578 opendoc state.path state.password;
6580 display ();
6581 Wsi.mapwin ();
6582 Wsi.setcursor Wsi.CURSOR_INHERIT;
6583 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
6585 let rec reap () =
6586 match Unix.waitpid [Unix.WNOHANG] ~-1 with
6587 | (exception (Unix.Unix_error (Unix.ECHILD, _, _))) -> ()
6588 | (exception exn) -> dolog "Unix.waitpid: %s" @@ exntos exn
6589 | 0, _ -> ()
6590 | _pid, _status -> reap ()
6592 Sys.set_signal Sys.sigchld (Sys.Signal_handle (fun _ -> doreap := true));
6594 let optrfd =
6595 ref (
6596 if nonemptystr !rcmdpath
6597 then remoteopen !rcmdpath
6598 else None
6602 let rec loop deadline =
6603 if !doreap
6604 then (
6605 doreap := false;
6606 reap ()
6608 let r = [state.ss; state.wsfd] in
6609 let r =
6610 match !optrfd with
6611 | None -> r
6612 | Some fd -> fd :: r
6614 if state.redisplay
6615 then (
6616 state.redisplay <- false;
6617 display ();
6619 let timeout =
6620 let now = now () in
6621 if deadline > now
6622 then (
6623 if deadline = infinity
6624 then ~-.1.0
6625 else max 0.0 (deadline -. now)
6627 else 0.0
6629 let r, _, _ =
6630 try Unix.select r [] [] timeout
6631 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6633 begin match r with
6634 | [] ->
6635 state.ghyll None;
6636 let newdeadline =
6637 if state.ghyll == noghyll
6638 then
6639 match state.autoscroll with
6640 | Some step when step != 0 ->
6641 let y = state.y + step in
6642 let fy = if conf.maxhfit then state.winh else 0 in
6643 let y =
6644 if y < 0
6645 then state.maxy - fy
6646 else if y >= state.maxy - fy then 0 else y
6648 if state.mode = View
6649 then gotoy_and_clear_text y
6650 else gotoy y;
6651 deadline +. 0.01
6652 | _ -> infinity
6653 else deadline +. 0.01
6655 loop newdeadline
6657 | l ->
6658 let rec checkfds = function
6659 | [] -> ()
6660 | fd :: rest when fd = state.ss ->
6661 let cmd = readcmd state.ss in
6662 act cmd;
6663 checkfds rest
6665 | fd :: rest when fd = state.wsfd ->
6666 Wsi.readresp fd;
6667 checkfds rest
6669 | fd :: rest when Some fd = !optrfd ->
6670 begin match remote fd with
6671 | None -> optrfd := remoteopen !rcmdpath;
6672 | opt -> optrfd := opt
6673 end;
6674 checkfds rest
6676 | _ :: rest ->
6677 dolog "select returned unknown descriptor";
6678 checkfds rest
6680 checkfds l;
6681 let newdeadline =
6682 let deadline1 =
6683 if deadline = infinity
6684 then now () +. 0.01
6685 else deadline
6687 match state.autoscroll with
6688 | Some step when step != 0 -> deadline1
6689 | _ -> if state.ghyll == noghyll then infinity else deadline1
6691 loop newdeadline
6692 end;
6695 loop infinity;
6696 with Quit ->
6697 Config.save leavebirdseye;
6698 if hasunsavedchanges ()
6699 then save ();