Cosmetics
[llpp.git] / main.ml
blob3e82e5a00cfbf2bea35ba7beb7337be681ae7b67
1 open Utils;;
2 open Config;;
4 exception Quit;;
6 type pipe = (Unix.file_descr * Unix.file_descr);;
8 external init : pipe -> params -> unit = "ml_init";;
9 external seltext : opaque -> (int * int * int * int) -> unit = "ml_seltext";;
10 external hassel : opaque -> bool = "ml_hassel";;
11 external copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
12 external getpdimrect : int -> float array = "ml_getpdimrect";;
13 external whatsunder : opaque -> int -> int -> under = "ml_whatsunder";;
14 external markunder : opaque -> int -> int -> mark -> bool = "ml_markunder";;
15 external clearmark : opaque -> unit = "ml_clearmark";;
16 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
17 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
18 external measurestr : int -> string -> float = "ml_measure_string";;
19 external postprocess :
20 opaque -> int -> int -> int -> (int * string * int) -> int
21 = "ml_postprocess";;
22 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
23 external setaalevel : int -> unit = "ml_setaalevel";;
24 external realloctexts : int -> bool = "ml_realloctexts";;
25 external findlink : opaque -> linkdir -> link = "ml_findlink";;
26 external getlink : opaque -> int -> under = "ml_getlink";;
27 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
28 external getlinkcount : opaque -> int = "ml_getlinkcount";;
29 external findpwl : int -> int -> pagewithlinks = "ml_find_page_with_links"
30 external getpbo : width -> height -> colorspace -> opaque = "ml_getpbo";;
31 external freepbo : opaque -> unit = "ml_freepbo";;
32 external unmappbo : opaque -> unit = "ml_unmappbo";;
33 external pbousable : unit -> bool = "ml_pbo_usable";;
34 external unproject : opaque -> int -> int -> (int * int) option
35 = "ml_unproject";;
36 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
37 external rectofblock : opaque -> int -> int -> float array option
38 = "ml_rectofblock";;
39 external begintiles : unit -> unit = "ml_begintiles";;
40 external endtiles : unit -> unit = "ml_endtiles";;
42 let selfexec = ref E.s;;
44 let drawstring size x y s =
45 Gl.enable `blend;
46 Gl.enable `texture_2d;
47 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
48 ignore (drawstr size x y s);
49 Gl.disable `blend;
50 Gl.disable `texture_2d;
53 let drawstring1 size x y s =
54 drawstr size x y s;
57 let drawstring2 size x y fmt =
58 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
61 let debugl l =
62 dolog "l %d dim=%d {" l.pageno l.pagedimno;
63 dolog " WxH %dx%d" l.pagew l.pageh;
64 dolog " vWxH %dx%d" l.pagevw l.pagevh;
65 dolog " pagex,y %d,%d" l.pagex l.pagey;
66 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
67 dolog " column %d" l.pagecol;
68 dolog "}";
71 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
72 dolog "rect {";
73 dolog " x0,y0=(% f, % f)" x0 y0;
74 dolog " x1,y1=(% f, % f)" x1 y1;
75 dolog " x2,y2=(% f, % f)" x2 y2;
76 dolog " x3,y3=(% f, % f)" x3 y3;
77 dolog "}";
80 let isbirdseye = function Birdseye _ -> true | _ -> false;;
81 let istextentry = function Textentry _ -> true | _ -> false;;
83 let wtmode = ref false;;
84 let cxack = ref false;;
86 let pgscale h = truncate (float h *. conf.pgscale);;
88 let hscrollh () =
89 if (conf.scrollb land scrollbhv = 0)
90 || (state.x = 0 && state.w <= state.winw - conf.scrollbw)
91 then 0
92 else conf.scrollbw
95 let vscrollw () =
96 if (conf.scrollb land scrollbvv = 0)
97 then 0
98 else conf.scrollbw
101 let wadjsb w = w - vscrollw ();;
102 let xadjsb x = if conf.leftscroll then x + vscrollw () else x;;
104 let setfontsize n =
105 fstate.fontsize <- n;
106 fstate.wwidth <- measurestr fstate.fontsize "w";
107 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
110 let vlog fmt =
111 if conf.verbose
112 then
113 Printf.kprintf prerr_endline fmt
114 else
115 Printf.kprintf ignore fmt
118 let launchpath () =
119 if emptystr conf.pathlauncher
120 then print_endline state.path
121 else (
122 let re = Str.regexp "%s" in
123 let command = Str.global_replace re state.path conf.pathlauncher in
124 try popen command []
125 with exn ->
126 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
127 flush stderr;
131 module Ne = struct
132 type 'a t = | Res of 'a | Exn of exn;;
134 let res f =
135 try Res (f ())
136 with exn -> Exn exn
139 let clo fd f =
140 try tempfailureretry Unix.close fd
141 with exn -> f (exntos exn)
144 let dup fd =
145 try Res (tempfailureretry Unix.dup fd)
146 with exn -> Exn exn
149 let dup2 fd1 fd2 =
150 try Res (tempfailureretry (Unix.dup2 fd1) fd2)
151 with exn -> Exn exn
153 end;;
155 let redirectstderr () =
156 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
157 if conf.redirectstderr
158 then
159 match Ne.res Unix.pipe with
160 | Ne.Exn exn ->
161 dolog "failed to create stderr redirection pipes: %s" (exntos exn)
163 | Ne.Res (r, w) ->
164 begin match Ne.dup Unix.stderr with
165 | Ne.Exn exn ->
166 dolog "failed to dup stderr: %s" (exntos exn);
167 Ne.clo r (clofail "pipe/r");
168 Ne.clo w (clofail "pipe/w");
170 | Ne.Res dupstderr ->
171 begin match Ne.dup2 w Unix.stderr with
172 | Ne.Exn exn ->
173 dolog "failed to dup2 to stderr: %s" (exntos exn);
174 Ne.clo dupstderr (clofail "stderr duplicate");
175 Ne.clo r (clofail "redir pipe/r");
176 Ne.clo w (clofail "redir pipe/w");
178 | Ne.Res () ->
179 state.stderr <- dupstderr;
180 state.errfd <- Some r;
181 end;
183 else (
184 state.newerrmsgs <- false;
185 begin match state.errfd with
186 | Some fd ->
187 begin match Ne.dup2 state.stderr Unix.stderr with
188 | Ne.Exn exn ->
189 dolog "failed to dup2 original stderr: %s" (exntos exn)
190 | Ne.Res () ->
191 Ne.clo fd (clofail "dup of stderr");
192 state.errfd <- None;
193 end;
194 | None -> ()
195 end;
196 prerr_string (Buffer.contents state.errmsgs);
197 flush stderr;
198 Buffer.clear state.errmsgs;
202 module G =
203 struct
204 let postRedisplay who =
205 if conf.verbose
206 then prerr_endline ("redisplay for " ^ who);
207 state.redisplay <- true;
209 end;;
211 let getopaque pageno =
212 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
213 with Not_found -> None
216 let putopaque pageno opaque =
217 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
220 let pagetranslatepoint l x y =
221 let dy = y - l.pagedispy in
222 let y = dy + l.pagey in
223 let dx = x - l.pagedispx in
224 let x = dx + l.pagex in
225 (x, y);
228 let onppundermouse g x y d =
229 let rec f = function
230 | l :: rest ->
231 begin match getopaque l.pageno with
232 | Some opaque ->
233 let x0 = l.pagedispx in
234 let x1 = x0 + l.pagevw in
235 let y0 = l.pagedispy in
236 let y1 = y0 + l.pagevh in
237 if y >= y0 && y <= y1 && x >= x0 && x <= x1
238 then
239 let px, py = pagetranslatepoint l x y in
240 match g opaque l px py with
241 | Some res -> res
242 | None -> f rest
243 else f rest
244 | _ ->
245 f rest
247 | [] -> d
249 f state.layout
252 let getunder x y =
253 let g opaque l px py =
254 if state.bzoom
255 then (
256 match rectofblock opaque px py with
257 | Some a ->
258 let rect = (a.(0),a.(2),a.(1),a.(2),a.(1),a.(3),a.(0),a.(3)) in
259 state.rects <- [l.pageno, l.pageno mod 3, rect];
260 G.postRedisplay "getunder";
261 | None -> ()
263 match whatsunder opaque px py with
264 | Unone -> None
265 | under -> Some under
267 onppundermouse g x y Unone
270 let unproject x y =
271 let g opaque l x y =
272 match unproject opaque x y with
273 | Some (x, y) -> Some (Some (l.pageno, x, y))
274 | None -> None
276 onppundermouse g x y None;
279 let showtext c s =
280 state.text <- Printf.sprintf "%c%s" c s;
281 G.postRedisplay "showtext";
284 let pipesel opaque cmd =
285 if hassel opaque
286 then
287 match Ne.res Unix.pipe with
288 | Ne.Exn exn ->
289 showtext '!'
290 (Printf.sprintf "pipesel can not create pipe: %s" (exntos exn));
291 | Ne.Res (r, w) ->
292 let doclose what fd =
293 Ne.clo fd (fun msg -> dolog "%s close failed: %s" what msg)
295 let popened =
296 try popen cmd [r, 0; w, -1]; true
297 with exn ->
298 dolog "can not execute %S: %s" cmd (exntos exn);
299 false
301 if popened
302 then (
303 copysel w opaque;
304 G.postRedisplay "pipesel";
306 else doclose "pipesel pipe/w" w;
307 doclose "pipesel pipe/r" r;
310 let paxunder x y =
311 let g opaque l px py =
312 if markunder opaque px py conf.paxmark
313 then (
314 Some (fun () ->
315 match getopaque l.pageno with
316 | None -> ()
317 | Some opaque -> pipesel opaque conf.paxcmd
320 else None
322 G.postRedisplay "paxunder";
323 if conf.paxmark = Mark_page
324 then
325 List.iter (fun l ->
326 match getopaque l.pageno with
327 | None -> ()
328 | Some opaque -> clearmark opaque) state.layout;
329 state.roam <-
330 onppundermouse g x y (fun () -> showtext '!' "Whoopsie daisy");
333 let selstring s =
334 match Ne.res Unix.pipe with
335 | Ne.Exn exn ->
336 showtext '!' (Printf.sprintf "pipe failed: %s" (exntos exn))
337 | Ne.Res (r, w) ->
338 let clo cap fd =
339 Ne.clo fd (fun msg ->
340 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
343 let popened =
344 try popen conf.selcmd [r, 0; w, -1]; true
345 with exn ->
346 showtext '!'
347 (Printf.sprintf "failed to execute %s: %s"
348 conf.selcmd (exntos exn));
349 false
351 if popened
352 then (
354 let l = String.length s in
355 let n = tempfailureretry (Unix.write w s 0) l in
356 if n != l
357 then
358 showtext '!'
359 (Printf.sprintf
360 "failed to write %d characters to sel pipe, wrote %d"
363 with exn ->
364 showtext '!'
365 (Printf.sprintf "failed to write to sel pipe: %s"
366 (exntos exn)
369 else dolog "%s" s;
370 clo "selstring pipe/r" r;
371 clo "selstring pipe/w" w;
374 let undertext = function
375 | Unone -> "none"
376 | Ulinkuri s -> s
377 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
378 | Utext s -> "font: " ^ s
379 | Uunexpected s -> "unexpected: " ^ s
380 | Ulaunch s -> "launch: " ^ s
381 | Unamed s -> "named: " ^ s
382 | Uremote (filename, pageno) ->
383 Printf.sprintf "%s: page %d" filename (pageno+1)
384 | Uremotedest (filename, destname) ->
385 Printf.sprintf "%s: destination %S" filename destname
388 let updateunder x y =
389 match getunder x y with
390 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
391 | Ulinkuri uri ->
392 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
393 Wsi.setcursor Wsi.CURSOR_INFO
394 | Ulinkgoto (pageno, _) ->
395 if conf.underinfo
396 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
397 Wsi.setcursor Wsi.CURSOR_INFO
398 | Utext s ->
399 if conf.underinfo then showtext 'f' ("ont: " ^ s);
400 Wsi.setcursor Wsi.CURSOR_TEXT
401 | Uunexpected s ->
402 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
403 Wsi.setcursor Wsi.CURSOR_INHERIT
404 | Ulaunch s ->
405 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
406 Wsi.setcursor Wsi.CURSOR_INHERIT
407 | Unamed s ->
408 if conf.underinfo then showtext 'n' ("amed: " ^ s);
409 Wsi.setcursor Wsi.CURSOR_INHERIT
410 | Uremote (filename, pageno) ->
411 if conf.underinfo then showtext 'r'
412 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
413 Wsi.setcursor Wsi.CURSOR_INFO
414 | Uremotedest (filename, destname) ->
415 if conf.underinfo then showtext 'r'
416 (Printf.sprintf "emote destination: %s (%S)" filename destname);
417 Wsi.setcursor Wsi.CURSOR_INFO
420 let showlinktype under =
421 if conf.underinfo
422 then
423 match under with
424 | Unone -> ()
425 | under ->
426 let s = undertext under in
427 showtext ' ' s
430 let addchar s c =
431 let b = Buffer.create (String.length s + 1) in
432 Buffer.add_string b s;
433 Buffer.add_char b c;
434 Buffer.contents b;
437 let intentry_with_suffix text key =
438 let c =
439 if key >= 32 && key < 127
440 then Char.chr key
441 else '\000'
443 match Char.lowercase c with
444 | '0' .. '9' ->
445 let text = addchar text c in
446 TEcont text
448 | 'k' | 'm' | 'g' ->
449 let text = addchar text c in
450 TEcont text
452 | _ ->
453 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
454 TEcont text
457 let readcmd fd =
458 let s = "xxxx" in
459 let n = tempfailureretry (Unix.read fd s 0) 4 in
460 if n != 4 then error "incomplete read(len) = %d" n;
461 let len = 0
462 lor (Char.code s.[0] lsl 24)
463 lor (Char.code s.[1] lsl 16)
464 lor (Char.code s.[2] lsl 8)
465 lor (Char.code s.[3] lsl 0)
467 let s = String.create len in
468 let n = tempfailureretry (Unix.read fd s 0) len in
469 if n != len then error "incomplete read(data) %d vs %d" n len;
473 let btod b = if b then 1 else 0;;
475 let wcmd fmt =
476 let b = Buffer.create 16 in
477 Buffer.add_string b "llll";
478 Printf.kbprintf
479 (fun b ->
480 let s = Buffer.contents b in
481 let n = String.length s in
482 let len = n - 4 in
483 (* dolog "wcmd %S" (String.sub s 4 len); *)
484 s.[0] <- Char.chr ((len lsr 24) land 0xff);
485 s.[1] <- Char.chr ((len lsr 16) land 0xff);
486 s.[2] <- Char.chr ((len lsr 8) land 0xff);
487 s.[3] <- Char.chr (len land 0xff);
488 let n' = tempfailureretry (Unix.write state.sw s 0) n in
489 if n' != n then error "write failed %d vs %d" n' n;
490 ) b fmt;
493 let nogeomcmds cmds =
494 match cmds with
495 | s, [] -> emptystr s
496 | _ -> false
499 let layoutN ((columns, coverA, coverB), b) y sh =
500 let sh = sh - (hscrollh ()) in
501 let rec fold accu n =
502 if n = Array.length b
503 then accu
504 else
505 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
506 if (vy - y) > sh &&
507 (n = coverA - 1
508 || n = state.pagecount - coverB
509 || (n - coverA) mod columns = columns - 1)
510 then accu
511 else
512 let accu =
513 if vy + h > y
514 then
515 let pagey = max 0 (y - vy) in
516 let pagedispy = if pagey > 0 then 0 else vy - y in
517 let pagedispx, pagex =
518 let pdx =
519 if n = coverA - 1 || n = state.pagecount - coverB
520 then state.x + (wadjsb state.winw - w) / 2
521 else dx + xoff + state.x
523 if pdx < 0
524 then 0, -pdx
525 else pdx, 0
527 let pagevw =
528 let vw = wadjsb state.winw - pagedispx in
529 let pw = w - pagex in
530 min vw pw
532 let pagevh = min (h - pagey) (sh - pagedispy) in
533 if pagevw > 0 && pagevh > 0
534 then
535 let e =
536 { pageno = n
537 ; pagedimno = pdimno
538 ; pagew = w
539 ; pageh = h
540 ; pagex = pagex
541 ; pagey = pagey
542 ; pagevw = pagevw
543 ; pagevh = pagevh
544 ; pagedispx = pagedispx
545 ; pagedispy = pagedispy
546 ; pagecol = 0
549 e :: accu
550 else
551 accu
552 else
553 accu
555 fold accu (n+1)
557 if Array.length b = 0
558 then []
559 else List.rev (fold [] (page_of_y y))
562 let layoutS (columns, b) y sh =
563 let sh = sh - hscrollh () in
564 let rec fold accu n =
565 if n = Array.length b
566 then accu
567 else
568 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
569 if (vy - y) > sh
570 then accu
571 else
572 let accu =
573 if vy + pageh > y
574 then
575 let x = xoff + state.x in
576 let pagey = max 0 (y - vy) in
577 let pagedispy = if pagey > 0 then 0 else vy - y in
578 let pagedispx, pagex =
579 if px = 0
580 then (
581 if x < 0
582 then 0, -x
583 else x, 0
585 else (
586 let px = px - x in
587 if px < 0
588 then -px, 0
589 else 0, px
592 let pagecolw = pagew/columns in
593 let pagedispx =
594 if pagecolw < state.winw
595 then pagedispx + ((wadjsb state.winw - pagecolw) / 2)
596 else pagedispx
598 let pagevw =
599 let vw = wadjsb state.winw - pagedispx in
600 let pw = pagew - pagex in
601 min vw pw
603 let pagevw = min pagevw pagecolw in
604 let pagevh = min (pageh - pagey) (sh - pagedispy) in
605 if pagevw > 0 && pagevh > 0
606 then
607 let e =
608 { pageno = n/columns
609 ; pagedimno = pdimno
610 ; pagew = pagew
611 ; pageh = pageh
612 ; pagex = pagex
613 ; pagey = pagey
614 ; pagevw = pagevw
615 ; pagevh = pagevh
616 ; pagedispx = pagedispx
617 ; pagedispy = pagedispy
618 ; pagecol = n mod columns
621 e :: accu
622 else
623 accu
624 else
625 accu
627 fold accu (n+1)
629 List.rev (fold [] 0)
632 let layout y sh =
633 if nogeomcmds state.geomcmds
634 then
635 match conf.columns with
636 | Csingle b -> layoutN ((1, 0, 0), b) y sh
637 | Cmulti c -> layoutN c y sh
638 | Csplit s -> layoutS s y sh
639 else []
642 let clamp incr =
643 let y = state.y + incr in
644 let y = max 0 y in
645 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
649 let itertiles l f =
650 let tilex = l.pagex mod conf.tilew in
651 let tiley = l.pagey mod conf.tileh in
653 let col = l.pagex / conf.tilew in
654 let row = l.pagey / conf.tileh in
656 let rec rowloop row y0 dispy h =
657 if h = 0
658 then ()
659 else (
660 let dh = conf.tileh - y0 in
661 let dh = min h dh in
662 let rec colloop col x0 dispx w =
663 if w = 0
664 then ()
665 else (
666 let dw = conf.tilew - x0 in
667 let dw = min w dw in
668 let dispx' = xadjsb dispx in
669 f col row dispx' dispy x0 y0 dw dh;
670 colloop (col+1) 0 (dispx+dw) (w-dw)
673 colloop col tilex l.pagedispx l.pagevw;
674 rowloop (row+1) 0 (dispy+dh) (h-dh)
677 if l.pagevw > 0 && l.pagevh > 0
678 then rowloop row tiley l.pagedispy l.pagevh;
681 let gettileopaque l col row =
682 let key =
683 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
685 try Some (Hashtbl.find state.tilemap key)
686 with Not_found -> None
689 let puttileopaque l col row gen colorspace angle opaque size elapsed =
690 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
691 Hashtbl.add state.tilemap key (opaque, size, elapsed)
694 let filledrect x0 y0 x1 y1 =
695 GlArray.disable `texture_coord;
696 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
697 GlArray.vertex `two state.vraw;
698 GlArray.draw_arrays `triangle_strip 0 4;
699 GlArray.enable `texture_coord;
702 let linerect x0 y0 x1 y1 =
703 GlArray.disable `texture_coord;
704 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y1; x1; y0 |];
705 GlArray.vertex `two state.vraw;
706 GlArray.draw_arrays `line_loop 0 4;
707 GlArray.enable `texture_coord;
710 let drawtiles l color =
711 GlDraw.color color;
712 begintiles ();
713 let f col row x y tilex tiley w h =
714 match gettileopaque l col row with
715 | Some (opaque, _, t) ->
716 let params = x, y, w, h, tilex, tiley in
717 if conf.invert
718 then GlTex.env (`mode `blend);
719 drawtile params opaque;
720 if conf.invert
721 then GlTex.env (`mode `modulate);
722 if conf.debug
723 then (
724 endtiles ();
725 let s = Printf.sprintf
726 "%d[%d,%d] %f sec"
727 l.pageno col row t
729 let w = measurestr fstate.fontsize s in
730 GlDraw.color (0.0, 0.0, 0.0);
731 filledrect (float (x-2))
732 (float (y-2))
733 (float (x+2) +. w)
734 (float (y + fstate.fontsize + 2));
735 GlDraw.color (1.0, 1.0, 1.0);
736 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
737 begintiles ();
740 | None ->
741 endtiles ();
742 let w =
743 if conf.leftscroll
744 then w
745 else
746 let lw = wadjsb state.winw - x in
747 min lw w
748 and h =
749 let lh = state.winh - y in
750 min lh h
752 if conf.invert
753 then GlTex.env (`mode `blend);
754 begin match state.checkerstexid with
755 | Some id ->
756 Gl.enable `texture_2d;
757 GlTex.bind_texture `texture_2d id;
758 let x0 = float x
759 and y0 = float y
760 and x1 = float (x+w)
761 and y1 = float (y+h) in
763 let tw = float w /. 16.0
764 and th = float h /. 16.0 in
765 let tx0 = float tilex /. 16.0
766 and ty0 = float tiley /. 16.0 in
767 let tx1 = tx0 +. tw
768 and ty1 = ty0 +. th in
769 Raw.sets_float state.vraw ~pos:0
770 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
771 Raw.sets_float state.traw ~pos:0
772 [| tx0; ty0; tx0; ty1; tx1; ty0; tx1; ty1 |];
773 GlArray.vertex `two state.vraw;
774 GlArray.tex_coord `two state.traw;
775 GlArray.draw_arrays `triangle_strip 0 4;
776 Gl.disable `texture_2d;
778 | None ->
779 GlDraw.color (1.0, 1.0, 1.0);
780 filledrect (float x) (float y) (float (x+w)) (float (y+h));
781 end;
782 if conf.invert
783 then GlTex.env (`mode `modulate);
784 if w > 128 && h > fstate.fontsize + 10
785 then (
786 let c = if conf.invert then 1.0 else 0.0 in
787 GlDraw.color (c, c, c);
788 let c, r =
789 if conf.verbose
790 then (col*conf.tilew, row*conf.tileh)
791 else col, row
793 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
795 GlDraw.color color;
796 begintiles ();
798 itertiles l f;
799 endtiles ();
802 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
804 let tilevisible1 l x y =
805 let ax0 = l.pagex
806 and ax1 = l.pagex + l.pagevw
807 and ay0 = l.pagey
808 and ay1 = l.pagey + l.pagevh in
810 let bx0 = x
811 and by0 = y in
812 let bx1 = min (bx0 + conf.tilew) l.pagew
813 and by1 = min (by0 + conf.tileh) l.pageh in
815 let rx0 = max ax0 bx0
816 and ry0 = max ay0 by0
817 and rx1 = min ax1 bx1
818 and ry1 = min ay1 by1 in
820 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
821 nonemptyintersection
824 let tilevisible layout n x y =
825 let rec findpageinlayout m = function
826 | l :: rest when l.pageno = n ->
827 tilevisible1 l x y || (
828 match conf.columns with
829 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
830 | _ -> false
832 | _ :: rest -> findpageinlayout 0 rest
833 | [] -> false
835 findpageinlayout 0 layout;
838 let tileready l x y =
839 tilevisible1 l x y &&
840 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
843 let tilepage n p layout =
844 let rec loop = function
845 | l :: rest ->
846 if l.pageno = n
847 then
848 let f col row _ _ _ _ _ _ =
849 if state.currently = Idle
850 then
851 match gettileopaque l col row with
852 | Some _ -> ()
853 | None ->
854 let x = col*conf.tilew
855 and y = row*conf.tileh in
856 let w =
857 let w = l.pagew - x in
858 min w conf.tilew
860 let h =
861 let h = l.pageh - y in
862 min h conf.tileh
864 let pbo =
865 if conf.usepbo
866 then getpbo w h conf.colorspace
867 else ~< "0"
869 wcmd "tile %s %d %d %d %d %s"
870 (~> p) x y w h (~> pbo);
871 state.currently <-
872 Tiling (
873 l, p, conf.colorspace, conf.angle,
874 state.gen, col, row, conf.tilew, conf.tileh
877 itertiles l f;
878 else
879 loop rest
881 | [] -> ()
883 if nogeomcmds state.geomcmds
884 then loop layout;
887 let preloadlayout y =
888 let y = if y < state.winh then 0 else y - state.winh in
889 let h = state.winh*3 in
890 layout y h;
893 let load pages =
894 let rec loop pages =
895 if state.currently != Idle
896 then ()
897 else
898 match pages with
899 | l :: rest ->
900 begin match getopaque l.pageno with
901 | None ->
902 wcmd "page %d %d" l.pageno l.pagedimno;
903 state.currently <- Loading (l, state.gen);
904 | Some opaque ->
905 tilepage l.pageno opaque pages;
906 loop rest
907 end;
908 | _ -> ()
910 if nogeomcmds state.geomcmds
911 then loop pages
914 let preload pages =
915 load pages;
916 if conf.preload && state.currently = Idle
917 then load (preloadlayout state.y);
920 let layoutready layout =
921 let rec fold all ls =
922 all && match ls with
923 | l :: rest ->
924 let seen = ref false in
925 let allvisible = ref true in
926 let foo col row _ _ _ _ _ _ =
927 seen := true;
928 allvisible := !allvisible &&
929 begin match gettileopaque l col row with
930 | Some _ -> true
931 | None -> false
934 itertiles l foo;
935 fold (!seen && !allvisible) rest
936 | [] -> true
938 let alltilesvisible = fold true layout in
939 alltilesvisible;
942 let gotoy y =
943 let y = bound y 0 state.maxy in
944 let y, layout, proceed =
945 match conf.maxwait with
946 | Some time when state.ghyll == noghyll ->
947 begin match state.throttle with
948 | None ->
949 let layout = layout y state.winh in
950 let ready = layoutready layout in
951 if not ready
952 then (
953 load layout;
954 state.throttle <- Some (layout, y, now ());
956 else G.postRedisplay "gotoy showall (None)";
957 y, layout, ready
958 | Some (_, _, started) ->
959 let dt = now () -. started in
960 if dt > time
961 then (
962 state.throttle <- None;
963 let layout = layout y state.winh in
964 load layout;
965 G.postRedisplay "maxwait";
966 y, layout, true
968 else -1, [], false
971 | _ ->
972 let layout = layout y state.winh in
973 if not !wtmode || layoutready layout
974 then G.postRedisplay "gotoy ready";
975 y, layout, true
977 if proceed
978 then (
979 state.y <- y;
980 state.layout <- layout;
981 begin match state.mode with
982 | LinkNav (Ltexact (pageno, linkno)) ->
983 let rec loop = function
984 | [] ->
985 state.mode <- LinkNav (Ltgendir 0)
986 | l :: _ when l.pageno = pageno ->
987 begin match getopaque pageno with
988 | None ->
989 state.mode <- LinkNav (Ltgendir 0)
990 | Some opaque ->
991 let x0, y0, x1, y1 = getlinkrect opaque linkno in
992 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
993 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
994 then state.mode <- LinkNav (Ltgendir 0)
996 | _ :: rest -> loop rest
998 loop layout
999 | _ -> ()
1000 end;
1001 begin match state.mode with
1002 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1003 if not (pagevisible layout pageno)
1004 then (
1005 match state.layout with
1006 | [] -> ()
1007 | l :: _ ->
1008 state.mode <- Birdseye (
1009 conf, leftx, l.pageno, hooverpageno, anchor
1012 | LinkNav (Ltgendir dir as lt) ->
1013 let linknav =
1014 let rec loop = function
1015 | [] -> lt
1016 | l :: rest ->
1017 match getopaque l.pageno with
1018 | None -> loop rest
1019 | Some opaque ->
1020 let link =
1021 let ld =
1022 if dir = 0
1023 then LDfirstvisible (l.pagex, l.pagey, dir)
1024 else (
1025 if dir > 0 then LDfirst else LDlast
1028 findlink opaque ld
1030 match link with
1031 | Lnotfound -> loop rest
1032 | Lfound n ->
1033 showlinktype (getlink opaque n);
1034 Ltexact (l.pageno, n)
1036 loop state.layout
1038 state.mode <- LinkNav linknav
1039 | _ -> ()
1040 end;
1041 preload layout;
1043 state.ghyll <- noghyll;
1044 if conf.updatecurs
1045 then (
1046 let mx, my = state.mpos in
1047 updateunder mx my;
1051 let conttiling pageno opaque =
1052 tilepage pageno opaque
1053 (if conf.preload then preloadlayout state.y else state.layout)
1056 let gotoy_and_clear_text y =
1057 if not conf.verbose then state.text <- E.s;
1058 gotoy y;
1061 let getanchory (n, top, dtop) =
1062 let y, h = getpageyh n in
1063 if conf.presentation
1064 then
1065 let ips = calcips h in
1066 y + truncate (top*.float h -. dtop*.float ips) + ips;
1067 else
1068 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1071 let gotoanchor anchor =
1072 gotoy (getanchory anchor);
1075 let addnav () =
1076 cbput state.hists.nav (getanchor ());
1079 let getnav dir =
1080 let anchor = cbgetc state.hists.nav dir in
1081 getanchory anchor;
1084 let gotoghyll1 single y =
1085 let scroll f n a b =
1086 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1087 let snake f a b =
1088 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1089 if f < a
1090 then s (float f /. float a)
1091 else (
1092 if f > b
1093 then 1.0 -. s ((float (f-b) /. float (n-b)))
1094 else 1.0
1097 snake f a b
1098 and summa n a b =
1099 let ins = float a *. 0.5
1100 and outs = float (n-b) *. 0.5 in
1101 let ones = b - a in
1102 ins +. outs +. float ones
1104 let rec set nab y sy =
1105 let (_N, _A, _B), y =
1106 if single
1107 then
1108 let scl = if y > sy then 2 else -2 in
1109 let _N, _, _ = nab in
1110 (_N,0,_N), y+conf.scrollstep*scl
1111 else nab,y in
1112 let sum = summa _N _A _B in
1113 let dy = float (y - sy) in
1114 state.ghyll <- (
1115 let rec gf n y1 o =
1116 if n >= _N
1117 then state.ghyll <- noghyll
1118 else
1119 let go n =
1120 let s = scroll n _N _A _B in
1121 let y1 = y1 +. ((s *. dy) /. sum) in
1122 gotoy_and_clear_text (truncate y1);
1123 state.ghyll <- gf (n+1) y1;
1125 match o with
1126 | None -> go n
1127 | Some y' when single -> set nab y' state.y
1128 | Some y' -> set (_N/2, 1, 1) y' state.y
1130 gf 0 (float state.y)
1133 match conf.ghyllscroll with
1134 | Some nab when not conf.presentation ->
1135 if state.ghyll == noghyll
1136 then set nab y state.y
1137 else state.ghyll (Some y)
1138 | _ ->
1139 gotoy_and_clear_text y
1142 let gotoghyll = gotoghyll1 false;;
1144 let gotopage n top =
1145 let y, h = getpageyh n in
1146 let y = y + (truncate (top *. float h)) in
1147 gotoghyll y
1150 let gotopage1 n top =
1151 let y = getpagey n in
1152 let y = y + top in
1153 gotoghyll y
1156 let invalidate s f =
1157 state.layout <- [];
1158 state.pdims <- [];
1159 state.rects <- [];
1160 state.rects1 <- [];
1161 match state.geomcmds with
1162 | ps, [] when emptystr ps ->
1163 f ();
1164 state.geomcmds <- s, [];
1166 | ps, [] ->
1167 state.geomcmds <- ps, [s, f];
1169 | ps, (s', _) :: rest when s' = s ->
1170 state.geomcmds <- ps, ((s, f) :: rest);
1172 | ps, cmds ->
1173 state.geomcmds <- ps, ((s, f) :: cmds);
1176 let flushpages () =
1177 Hashtbl.iter (fun _ opaque ->
1178 wcmd "freepage %s" (~> opaque);
1179 ) state.pagemap;
1180 Hashtbl.clear state.pagemap;
1183 let flushtiles () =
1184 if not (Queue.is_empty state.tilelru)
1185 then (
1186 Queue.iter (fun (k, p, s) ->
1187 wcmd "freetile %s" (~> p);
1188 state.memused <- state.memused - s;
1189 Hashtbl.remove state.tilemap k;
1190 ) state.tilelru;
1191 state.uioh#infochanged Memused;
1192 Queue.clear state.tilelru;
1194 load state.layout;
1197 let stateh h =
1198 let h = truncate (float h*.conf.zoom) in
1199 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
1200 h - d
1203 let opendoc path password =
1204 state.path <- path;
1205 state.password <- password;
1206 state.gen <- state.gen + 1;
1207 state.docinfo <- [];
1209 flushpages ();
1210 setaalevel conf.aalevel;
1211 let titlepath =
1212 if emptystr state.origin
1213 then path
1214 else state.origin
1216 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
1217 wcmd "open %d %d %s\000%s\000" (btod !wtmode) (btod !cxack) path password;
1218 invalidate "reqlayout"
1219 (fun () ->
1220 wcmd "reqlayout %d %d %d %s\000"
1221 conf.angle (FMTE.to_int conf.fitmodel)
1222 (stateh state.winh) state.nameddest
1226 let reload () =
1227 state.anchor <- getanchor ();
1228 opendoc state.path state.password;
1231 let scalecolor c =
1232 let c = c *. conf.colorscale in
1233 (c, c, c);
1236 let scalecolor2 (r, g, b) =
1237 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1240 let docolumns = function
1241 | Csingle _ ->
1242 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1243 let rec loop pageno pdimno pdim y ph pdims =
1244 if pageno = state.pagecount
1245 then ()
1246 else
1247 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1248 match pdims with
1249 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1250 pdimno+1, pdim, rest
1251 | _ ->
1252 pdimno, pdim, pdims
1254 let x = max 0 (((wadjsb state.winw - w) / 2) - xoff) in
1255 let y = y +
1256 (if conf.presentation
1257 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1258 else (if pageno = 0 then 0 else conf.interpagespace)
1261 a.(pageno) <- (pdimno, x, y, pdim);
1262 loop (pageno+1) pdimno pdim (y + h) h pdims
1264 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1265 conf.columns <- Csingle a;
1267 | Cmulti ((columns, coverA, coverB), _) ->
1268 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1269 let rec loop pageno pdimno pdim x y rowh pdims =
1270 let rec fixrow m = if m = pageno then () else
1271 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1272 if h < rowh
1273 then (
1274 let y = y + (rowh - h) / 2 in
1275 a.(m) <- (pdimno, x, y, pdim);
1277 fixrow (m+1)
1279 if pageno = state.pagecount
1280 then fixrow (((pageno - 1) / columns) * columns)
1281 else
1282 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1283 match pdims with
1284 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1285 pdimno+1, pdim, rest
1286 | _ ->
1287 pdimno, pdim, pdims
1289 let x, y, rowh' =
1290 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1291 then (
1292 let x = (wadjsb state.winw - w) / 2 in
1293 let ips =
1294 if conf.presentation then calcips h else conf.interpagespace in
1295 x, y + ips + rowh, h
1297 else (
1298 if (pageno - coverA) mod columns = 0
1299 then (
1300 let x = max 0 (wadjsb state.winw - state.w) / 2 in
1301 let y =
1302 if conf.presentation
1303 then
1304 let ips = calcips h in
1305 y + (if pageno = 0 then 0 else calcips rowh + ips)
1306 else
1307 y + (if pageno = 0 then 0 else conf.interpagespace)
1309 x, y + rowh, h
1311 else x, y, max rowh h
1314 let y =
1315 if pageno > 1 && (pageno - coverA) mod columns = 0
1316 then (
1317 let y =
1318 if pageno = columns && conf.presentation
1319 then (
1320 let ips = calcips rowh in
1321 for i = 0 to pred columns
1323 let (pdimno, x, y, pdim) = a.(i) in
1324 a.(i) <- (pdimno, x, y+ips, pdim)
1325 done;
1326 y+ips;
1328 else y
1330 fixrow (pageno - columns);
1333 else y
1335 a.(pageno) <- (pdimno, x, y, pdim);
1336 let x = x + w + xoff*2 + conf.interpagespace in
1337 loop (pageno+1) pdimno pdim x y rowh' pdims
1339 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1340 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1342 | Csplit (c, _) ->
1343 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1344 let rec loop pageno pdimno pdim y pdims =
1345 if pageno = state.pagecount
1346 then ()
1347 else
1348 let pdimno, ((_, w, h, _) as pdim), pdims =
1349 match pdims with
1350 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1351 pdimno+1, pdim, rest
1352 | _ ->
1353 pdimno, pdim, pdims
1355 let cw = w / c in
1356 let rec loop1 n x y =
1357 if n = c then y else (
1358 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1359 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1362 let y = loop1 0 0 y in
1363 loop (pageno+1) pdimno pdim y pdims
1365 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1366 conf.columns <- Csplit (c, a);
1369 let represent () =
1370 docolumns conf.columns;
1371 state.maxy <- calcheight ();
1372 if state.reprf == noreprf
1373 then (
1374 match state.mode with
1375 | Birdseye (_, _, pageno, _, _) ->
1376 let y, h = getpageyh pageno in
1377 let top = (state.winh - h) / 2 in
1378 gotoy (max 0 (y - top))
1379 | _ -> gotoanchor state.anchor
1381 else (
1382 state.reprf ();
1383 state.reprf <- noreprf;
1387 let reshape w h =
1388 GlDraw.viewport 0 0 w h;
1389 let firsttime = state.geomcmds == firstgeomcmds in
1390 if not firsttime && nogeomcmds state.geomcmds
1391 then state.anchor <- getanchor ();
1393 state.winw <- w;
1394 let w = wadjsb (truncate (float w *. conf.zoom)) in
1395 let w = max w 2 in
1396 state.winh <- h;
1397 setfontsize fstate.fontsize;
1398 GlMat.mode `modelview;
1399 GlMat.load_identity ();
1401 GlMat.mode `projection;
1402 GlMat.load_identity ();
1403 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1404 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1405 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
1407 let relx =
1408 if conf.zoom <= 1.0
1409 then 0.0
1410 else float state.x /. float state.w
1412 invalidate "geometry"
1413 (fun () ->
1414 state.w <- w;
1415 if not firsttime
1416 then state.x <- truncate (relx *. float w);
1417 let w =
1418 match conf.columns with
1419 | Csingle _ -> w
1420 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1421 | Csplit (c, _) -> w * c
1423 wcmd "geometry %d %d %d"
1424 w (stateh h) (FMTE.to_int conf.fitmodel)
1428 let enttext () =
1429 let len = String.length state.text in
1430 let x0 = xadjsb 0 in
1431 let drawstring s =
1432 let hscrollh =
1433 match state.mode with
1434 | Textentry _ | View | LinkNav _ ->
1435 let h, _, _ = state.uioh#scrollpw in
1437 | _ -> 0
1439 let rect x w =
1440 filledrect x (float (state.winh - (fstate.fontsize + 4) - hscrollh))
1441 (x+.w) (float (state.winh - hscrollh))
1444 let w = float (wadjsb state.winw - 1) in
1445 if state.progress >= 0.0 && state.progress < 1.0
1446 then (
1447 GlDraw.color (0.3, 0.3, 0.3);
1448 let w1 = w *. state.progress in
1449 rect (float x0) w1;
1450 GlDraw.color (0.0, 0.0, 0.0);
1451 rect (float x0+.w1) (float x0+.w-.w1)
1453 else (
1454 GlDraw.color (0.0, 0.0, 0.0);
1455 rect (float x0) w;
1458 GlDraw.color (1.0, 1.0, 1.0);
1459 drawstring fstate.fontsize
1460 (if conf.leftscroll then x0 else x0 + if len > 0 then 8 else 2)
1461 (state.winh - hscrollh - 5) s;
1463 let s =
1464 match state.mode with
1465 | Textentry ((prefix, text, _, _, _, _), _) ->
1466 let s =
1467 if len > 0
1468 then
1469 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1470 else
1471 Printf.sprintf "%s%s_" prefix text
1475 | _ -> state.text
1477 let s =
1478 if state.newerrmsgs
1479 then (
1480 if not (istextentry state.mode) && state.uioh#eformsgs
1481 then
1482 let s1 = "(press 'e' to review error messasges)" in
1483 if nonemptystr s then s ^ " " ^ s1 else s1
1484 else s
1486 else s
1488 if nonemptystr s
1489 then drawstring s
1492 let gctiles () =
1493 let len = Queue.length state.tilelru in
1494 let layout = lazy (
1495 match state.throttle with
1496 | None ->
1497 if conf.preload
1498 then preloadlayout state.y
1499 else state.layout
1500 | Some (layout, _, _) ->
1501 layout
1502 ) in
1503 let rec loop qpos =
1504 if state.memused <= conf.memlimit
1505 then ()
1506 else (
1507 if qpos < len
1508 then
1509 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1510 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1511 let (_, pw, ph, _) = getpagedim n in
1513 gen = state.gen
1514 && colorspace = conf.colorspace
1515 && angle = conf.angle
1516 && pagew = pw
1517 && pageh = ph
1518 && (
1519 let x = col*conf.tilew
1520 and y = row*conf.tileh in
1521 tilevisible (Lazy.force_val layout) n x y
1523 then Queue.push lruitem state.tilelru
1524 else (
1525 freepbo p;
1526 wcmd "freetile %s" (~> p);
1527 state.memused <- state.memused - s;
1528 state.uioh#infochanged Memused;
1529 Hashtbl.remove state.tilemap k;
1531 loop (qpos+1)
1534 loop 0
1537 let logcurrently = function
1538 | Idle -> dolog "Idle"
1539 | Loading (l, gen) ->
1540 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
1541 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
1542 dolog
1543 "Tiling %d[%d,%d] page=%s cs=%s angle"
1544 l.pageno col row (~> pageopaque)
1545 (CSTE.to_string colorspace)
1547 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
1548 angle gen conf.angle state.gen
1549 tilew tileh
1550 conf.tilew conf.tileh
1552 | Outlining _ ->
1553 dolog "outlining"
1556 let splitatspace =
1557 let r = Str.regexp " " in
1558 fun s -> Str.bounded_split r s 2;
1561 let onpagerect pageno f =
1562 let b =
1563 match conf.columns with
1564 | Cmulti (_, b) -> b
1565 | Csingle b -> b
1566 | Csplit (_, b) -> b
1568 if pageno >= 0 && pageno < Array.length b
1569 then
1570 let (_, _, _, (w, h, _, _)) = b.(pageno) in
1571 f w h
1574 let gotopagexy1 pageno x y =
1575 let _,w1,h1,leftx = getpagedim pageno in
1576 let top = y /. (float h1) in
1577 let left = x /. (float w1) in
1578 let py, w, h = getpageywh pageno in
1579 let wh = state.winh - hscrollh () in
1580 let x = left *. (float w) in
1581 let x = leftx + state.x + truncate x in
1582 let sx =
1583 if x < 0 || x >= wadjsb state.winw
1584 then state.x - x
1585 else state.x
1587 let pdy = truncate (top *. float h) in
1588 let y' = py + pdy in
1589 let dy = y' - state.y in
1590 let sy =
1591 if x != state.x || not (dy > 0 && dy < wh)
1592 then (
1593 if conf.presentation
1594 then
1595 if abs (py - y') > wh
1596 then y'
1597 else py
1598 else y';
1600 else state.y
1602 if state.x != sx || state.y != sy
1603 then (
1604 let x, y =
1605 if !wtmode
1606 then (
1607 let ww = wadjsb state.winw in
1608 let qx = sx / ww
1609 and qy = pdy / wh in
1610 let x = qx * ww
1611 and y = py + qy * wh in
1612 let x = if -x + ww > w1 then -(w1-ww) else x
1613 and y' = if y + wh > state.maxy then state.maxy - wh else y in
1614 let y =
1615 if conf.presentation
1616 then
1617 if abs (py - y') > wh
1618 then y'
1619 else py
1620 else y';
1622 (x, y)
1624 else (sx, sy)
1626 state.x <- x;
1627 gotoy_and_clear_text y;
1629 else gotoy_and_clear_text state.y;
1632 let gotopagexy pageno x y =
1633 match state.mode with
1634 | Birdseye _ -> gotopage pageno 0.0
1635 | _ -> gotopagexy1 pageno x y
1638 let act cmds =
1639 (* dolog "%S" cmds; *)
1640 let cl = splitatspace cmds in
1641 let scan s fmt f =
1642 try Scanf.sscanf s fmt f
1643 with exn ->
1644 dolog "error processing '%S': %s" cmds (exntos exn);
1645 exit 1
1647 let addoutline outline =
1648 match state.currently with
1649 | Outlining outlines ->
1650 state.currently <- Outlining (outline :: outlines)
1651 | Idle -> state.currently <- Outlining [outline]
1652 | currently ->
1653 dolog "invalid outlining state";
1654 logcurrently currently
1656 match cl with
1657 | "clear" :: [] ->
1658 state.uioh#infochanged Pdim;
1659 state.pdims <- [];
1661 | "clearrects" :: [] ->
1662 state.rects <- state.rects1;
1663 G.postRedisplay "clearrects";
1665 | "continue" :: args :: [] ->
1666 let n = scan args "%u" (fun n -> n) in
1667 state.pagecount <- n;
1668 begin match state.currently with
1669 | Outlining l ->
1670 state.currently <- Idle;
1671 state.outlines <- Array.of_list (List.rev l)
1672 | _ -> ()
1673 end;
1675 let cur, cmds = state.geomcmds in
1676 if emptystr cur
1677 then failwith "umpossible";
1679 begin match List.rev cmds with
1680 | [] ->
1681 state.geomcmds <- E.s, [];
1682 state.throttle <- None;
1683 represent ();
1684 | (s, f) :: rest ->
1685 f ();
1686 state.geomcmds <- s, List.rev rest;
1687 end;
1688 if conf.maxwait = None && not !wtmode
1689 then G.postRedisplay "continue";
1691 | "title" :: args :: [] ->
1692 conf.title <- args;
1693 Wsi.settitle args
1695 | "msg" :: args :: [] ->
1696 showtext ' ' args
1698 | "vmsg" :: args :: [] ->
1699 if conf.verbose
1700 then showtext ' ' args
1702 | "emsg" :: args :: [] ->
1703 Buffer.add_string state.errmsgs args;
1704 state.newerrmsgs <- true;
1705 G.postRedisplay "error message"
1707 | "progress" :: args :: [] ->
1708 let progress, text =
1709 scan args "%f %n"
1710 (fun f pos ->
1711 f, String.sub args pos (String.length args - pos))
1713 state.text <- text;
1714 state.progress <- progress;
1715 G.postRedisplay "progress"
1717 | "firstmatch" :: args :: [] ->
1718 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1719 scan args "%u %d %f %f %f %f %f %f %f %f"
1720 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1721 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1723 let xoff = float (xadjsb 0) in
1724 let x0 = x0 +. xoff
1725 and x1 = x1 +. xoff
1726 and x2 = x2 +. xoff
1727 and x3 = x3 +. xoff in
1728 let y = (getpagey pageno) + truncate y0 in
1729 addnav ();
1730 gotoy y;
1731 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
1733 | "match" :: args :: [] ->
1734 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1735 scan args "%u %d %f %f %f %f %f %f %f %f"
1736 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1737 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1739 let xoff = float (xadjsb 0) in
1740 let x0 = x0 +. xoff
1741 and x1 = x1 +. xoff
1742 and x2 = x2 +. xoff
1743 and x3 = x3 +. xoff in
1744 state.rects1 <-
1745 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1747 | "page" :: args :: [] ->
1748 let pageopaques, t = scan args "%s %f" (fun p t -> p, t) in
1749 let pageopaque = ~< pageopaques in
1750 begin match state.currently with
1751 | Loading (l, gen) ->
1752 vlog "page %d took %f sec" l.pageno t;
1753 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1754 begin match state.throttle with
1755 | None ->
1756 let preloadedpages =
1757 if conf.preload
1758 then preloadlayout state.y
1759 else state.layout
1761 let evict () =
1762 let set =
1763 List.fold_left (fun s l -> IntSet.add l.pageno s)
1764 IntSet.empty preloadedpages
1766 let evictedpages =
1767 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1768 if not (IntSet.mem pageno set)
1769 then (
1770 wcmd "freepage %s" (~> opaque);
1771 key :: accu
1773 else accu
1774 ) state.pagemap []
1776 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1778 evict ();
1779 state.currently <- Idle;
1780 if gen = state.gen
1781 then (
1782 tilepage l.pageno pageopaque state.layout;
1783 load state.layout;
1784 load preloadedpages;
1785 if pagevisible state.layout l.pageno
1786 && layoutready state.layout
1787 then G.postRedisplay "page";
1790 | Some (layout, _, _) ->
1791 state.currently <- Idle;
1792 tilepage l.pageno pageopaque layout;
1793 load state.layout
1794 end;
1796 | _ ->
1797 dolog "Inconsistent loading state";
1798 logcurrently state.currently;
1799 exit 1
1802 | "tile" :: args :: [] ->
1803 let (x, y, opaques, size, t) =
1804 scan args "%u %u %s %u %f"
1805 (fun x y p size t -> (x, y, p, size, t))
1807 let opaque = ~< opaques in
1808 begin match state.currently with
1809 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1810 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1812 unmappbo opaque;
1813 if tilew != conf.tilew || tileh != conf.tileh
1814 then (
1815 wcmd "freetile %s" (~> opaque);
1816 state.currently <- Idle;
1817 load state.layout;
1819 else (
1820 puttileopaque l col row gen cs angle opaque size t;
1821 state.memused <- state.memused + size;
1822 state.uioh#infochanged Memused;
1823 gctiles ();
1824 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1825 opaque, size) state.tilelru;
1827 let layout =
1828 match state.throttle with
1829 | None -> state.layout
1830 | Some (layout, _, _) -> layout
1833 state.currently <- Idle;
1834 if gen = state.gen
1835 && conf.colorspace = cs
1836 && conf.angle = angle
1837 && tilevisible layout l.pageno x y
1838 then conttiling l.pageno pageopaque;
1840 begin match state.throttle with
1841 | None ->
1842 preload state.layout;
1843 if gen = state.gen
1844 && conf.colorspace = cs
1845 && conf.angle = angle
1846 && tilevisible state.layout l.pageno x y
1847 && (not !wtmode || layoutready state.layout)
1848 then G.postRedisplay "tile nothrottle";
1850 | Some (layout, y, _) ->
1851 let ready = layoutready layout in
1852 if ready
1853 then (
1854 state.y <- y;
1855 state.layout <- layout;
1856 state.throttle <- None;
1857 G.postRedisplay "throttle";
1859 else load layout;
1860 end;
1863 | _ ->
1864 dolog "Inconsistent tiling state";
1865 logcurrently state.currently;
1866 exit 1
1869 | "pdim" :: args :: [] ->
1870 let (n, w, h, _) as pdim =
1871 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1873 let pdim =
1874 match conf.fitmodel, conf.columns with
1875 | (FitPage | FitProportional), Csplit _ -> (n, w, h, 0)
1876 | _ -> pdim
1878 state.uioh#infochanged Pdim;
1879 state.pdims <- pdim :: state.pdims
1881 | "o" :: args :: [] ->
1882 let (l, n, t, h, pos) =
1883 scan args "%u %u %d %u %n"
1884 (fun l n t h pos -> l, n, t, h, pos)
1886 let s = String.sub args pos (String.length args - pos) in
1887 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
1889 | "ou" :: args :: [] ->
1890 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
1891 let s = String.sub args pos len in
1892 let pos2 = pos + len + 1 in
1893 let uri = String.sub args pos2 (String.length args - pos2) in
1894 addoutline (s, l, Ouri uri)
1896 | "on" :: args :: [] ->
1897 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
1898 let s = String.sub args pos (String.length args - pos) in
1899 addoutline (s, l, Onone)
1901 | "a" :: args :: [] ->
1902 let (n, l, t) =
1903 scan args "%u %d %d" (fun n l t -> n, l, t)
1905 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
1907 | "info" :: args :: [] ->
1908 state.docinfo <- (1, args) :: state.docinfo
1910 | "infoend" :: [] ->
1911 state.uioh#infochanged Docinfo;
1912 state.docinfo <- List.rev state.docinfo
1914 | _ ->
1915 error "unknown cmd `%S'" cmds
1918 let onhist cb =
1919 let rc = cb.rc in
1920 let action = function
1921 | HCprev -> cbget cb ~-1
1922 | HCnext -> cbget cb 1
1923 | HCfirst -> cbget cb ~-(cb.rc)
1924 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1925 and cancel () = cb.rc <- rc
1926 in (action, cancel)
1929 let search pattern forward =
1930 match conf.columns with
1931 | Csplit _ ->
1932 showtext '!' "searching does not work properly in split columns mode"
1933 | _ ->
1934 if nonemptystr pattern
1935 then
1936 let pn, py =
1937 match state.layout with
1938 | [] -> 0, 0
1939 | l :: _ ->
1940 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1942 wcmd "search %d %d %d %d,%s\000"
1943 (btod conf.icase) pn py (btod forward) pattern;
1946 let intentry text key =
1947 let c =
1948 if key >= 32 && key < 127
1949 then Char.chr key
1950 else '\000'
1952 match c with
1953 | '0' .. '9' ->
1954 let text = addchar text c in
1955 TEcont text
1957 | _ ->
1958 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1959 TEcont text
1962 let linknentry text key =
1963 let c =
1964 if key >= 32 && key < 127
1965 then Char.chr key
1966 else '\000'
1968 match c with
1969 | 'a' .. 'z' ->
1970 let text = addchar text c in
1971 TEcont text
1973 | _ ->
1974 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1975 TEcont text
1978 let linkndone f s =
1979 if nonemptystr s
1980 then (
1981 let n =
1982 let l = String.length s in
1983 let rec loop pos n = if pos = l then n else
1984 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
1985 loop (pos+1) (n*26 + m)
1986 in loop 0 0
1988 let rec loop n = function
1989 | [] -> ()
1990 | l :: rest ->
1991 match getopaque l.pageno with
1992 | None -> loop n rest
1993 | Some opaque ->
1994 let m = getlinkcount opaque in
1995 if n < m
1996 then (
1997 let under = getlink opaque n in
1998 f under
2000 else loop (n-m) rest
2002 loop n state.layout;
2006 let textentry text key =
2007 if key land 0xff00 = 0xff00
2008 then TEcont text
2009 else TEcont (text ^ toutf8 key)
2012 let reqlayout angle fitmodel =
2013 match state.throttle with
2014 | None ->
2015 if nogeomcmds state.geomcmds
2016 then state.anchor <- getanchor ();
2017 conf.angle <- angle mod 360;
2018 if conf.angle != 0
2019 then (
2020 match state.mode with
2021 | LinkNav _ -> state.mode <- View
2022 | _ -> ()
2024 conf.fitmodel <- fitmodel;
2025 invalidate "reqlayout"
2026 (fun () ->
2027 wcmd "reqlayout %d %d %d"
2028 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
2030 | _ -> ()
2033 let settrim trimmargins trimfuzz =
2034 if nogeomcmds state.geomcmds
2035 then state.anchor <- getanchor ();
2036 conf.trimmargins <- trimmargins;
2037 conf.trimfuzz <- trimfuzz;
2038 let x0, y0, x1, y1 = trimfuzz in
2039 invalidate "settrim"
2040 (fun () ->
2041 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2042 flushpages ();
2045 let setzoom zoom =
2046 match state.throttle with
2047 | None ->
2048 let zoom = max 0.0001 zoom in
2049 if zoom <> conf.zoom
2050 then (
2051 state.prevzoom <- (conf.zoom, state.x);
2052 conf.zoom <- zoom;
2053 reshape state.winw state.winh;
2054 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2057 | Some (layout, y, started) ->
2058 let time =
2059 match conf.maxwait with
2060 | None -> 0.0
2061 | Some t -> t
2063 let dt = now () -. started in
2064 if dt > time
2065 then (
2066 state.y <- y;
2067 load layout;
2071 let setcolumns mode columns coverA coverB =
2072 state.prevcolumns <- Some (conf.columns, conf.zoom);
2073 if columns < 0
2074 then (
2075 if isbirdseye mode
2076 then showtext '!' "split mode doesn't work in bird's eye"
2077 else (
2078 conf.columns <- Csplit (-columns, E.a);
2079 state.x <- 0;
2080 conf.zoom <- 1.0;
2083 else (
2084 if columns < 2
2085 then (
2086 conf.columns <- Csingle E.a;
2087 state.x <- 0;
2088 setzoom 1.0;
2090 else (
2091 conf.columns <- Cmulti ((columns, coverA, coverB), E.a);
2092 conf.zoom <- 1.0;
2095 reshape state.winw state.winh;
2098 let resetmstate () =
2099 state.mstate <- Mnone;
2100 Wsi.setcursor Wsi.CURSOR_INHERIT;
2103 let enterbirdseye () =
2104 let zoom = float conf.thumbw /. float state.winw in
2105 let birdseyepageno =
2106 let cy = state.winh / 2 in
2107 let fold = function
2108 | [] -> 0
2109 | l :: rest ->
2110 let rec fold best = function
2111 | [] -> best.pageno
2112 | l :: rest ->
2113 let d = cy - (l.pagedispy + l.pagevh/2)
2114 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2115 if abs d < abs dbest
2116 then fold l rest
2117 else best.pageno
2118 in fold l rest
2120 fold state.layout
2122 state.mode <- Birdseye (
2123 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2125 resetmstate ();
2126 conf.zoom <- zoom;
2127 conf.presentation <- false;
2128 conf.interpagespace <- 10;
2129 conf.hlinks <- false;
2130 conf.fitmodel <- FitProportional;
2131 state.x <- 0;
2132 conf.maxwait <- None;
2133 conf.columns <- (
2134 match conf.beyecolumns with
2135 | Some c ->
2136 conf.zoom <- 1.0;
2137 Cmulti ((c, 0, 0), E.a)
2138 | None -> Csingle E.a
2140 if conf.verbose
2141 then
2142 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2143 (100.0*.zoom)
2144 else
2145 state.text <- E.s
2147 reshape state.winw state.winh;
2150 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2151 state.mode <- View;
2152 conf.zoom <- c.zoom;
2153 conf.presentation <- c.presentation;
2154 conf.interpagespace <- c.interpagespace;
2155 conf.maxwait <- c.maxwait;
2156 conf.hlinks <- c.hlinks;
2157 conf.fitmodel <- c.fitmodel;
2158 conf.beyecolumns <- (
2159 match conf.columns with
2160 | Cmulti ((c, _, _), _) -> Some c
2161 | Csingle _ -> None
2162 | Csplit _ -> failwith "leaving bird's eye split mode"
2164 conf.columns <- (
2165 match c.columns with
2166 | Cmulti (c, _) -> Cmulti (c, E.a)
2167 | Csingle _ -> Csingle E.a
2168 | Csplit (c, _) -> Csplit (c, E.a)
2170 if conf.verbose
2171 then
2172 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2173 (100.0*.conf.zoom)
2175 reshape state.winw state.winh;
2176 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2177 state.x <- leftx;
2180 let togglebirdseye () =
2181 match state.mode with
2182 | Birdseye vals -> leavebirdseye vals true
2183 | View -> enterbirdseye ()
2184 | _ -> ()
2187 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2188 let pageno = max 0 (pageno - incr) in
2189 let rec loop = function
2190 | [] -> gotopage1 pageno 0
2191 | l :: _ when l.pageno = pageno ->
2192 if l.pagedispy >= 0 && l.pagey = 0
2193 then G.postRedisplay "upbirdseye"
2194 else gotopage1 pageno 0
2195 | _ :: rest -> loop rest
2197 loop state.layout;
2198 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2201 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2202 let pageno = min (state.pagecount - 1) (pageno + incr) in
2203 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2204 let rec loop = function
2205 | [] ->
2206 let y, h = getpageyh pageno in
2207 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2208 gotoy (clamp dy)
2209 | l :: _ when l.pageno = pageno ->
2210 if l.pagevh != l.pageh
2211 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2212 else G.postRedisplay "downbirdseye"
2213 | _ :: rest -> loop rest
2215 loop state.layout
2218 let boundastep h step =
2219 if step < 0
2220 then bound step ~-h 0
2221 else bound step 0 h
2224 let optentry mode _ key =
2225 let btos b = if b then "on" else "off" in
2226 if key >= 32 && key < 127
2227 then
2228 let c = Char.chr key in
2229 match c with
2230 | 's' ->
2231 let ondone s =
2232 try conf.scrollstep <- int_of_string s with exc ->
2233 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2235 TEswitch ("scroll step: ", E.s, None, intentry, ondone, true)
2237 | 'A' ->
2238 let ondone s =
2240 conf.autoscrollstep <- boundastep state.winh (int_of_string s);
2241 if state.autoscroll <> None
2242 then state.autoscroll <- Some conf.autoscrollstep
2243 with exc ->
2244 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2246 TEswitch ("auto scroll step: ", E.s, None, intentry, ondone, true)
2248 | 'C' ->
2249 let ondone s =
2251 let n, a, b = multicolumns_of_string s in
2252 setcolumns mode n a b;
2253 with exc ->
2254 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
2256 TEswitch ("columns: ", E.s, None, textentry, ondone, true)
2258 | 'Z' ->
2259 let ondone s =
2261 let zoom = float (int_of_string s) /. 100.0 in
2262 setzoom zoom
2263 with exc ->
2264 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2266 TEswitch ("zoom: ", E.s, None, intentry, ondone, true)
2268 | 't' ->
2269 let ondone s =
2271 conf.thumbw <- bound (int_of_string s) 2 4096;
2272 state.text <-
2273 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2274 begin match mode with
2275 | Birdseye beye ->
2276 leavebirdseye beye false;
2277 enterbirdseye ();
2278 | _ -> ();
2280 with exc ->
2281 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2283 TEswitch ("thumbnail width: ", E.s, None, intentry, ondone, true)
2285 | 'R' ->
2286 let ondone s =
2287 match try
2288 Some (int_of_string s)
2289 with exc ->
2290 state.text <- Printf.sprintf "bad integer `%s': %s"
2291 s (exntos exc);
2292 None
2293 with
2294 | Some angle -> reqlayout angle conf.fitmodel
2295 | None -> ()
2297 TEswitch ("rotation: ", E.s, None, intentry, ondone, true)
2299 | 'i' ->
2300 conf.icase <- not conf.icase;
2301 TEdone ("case insensitive search " ^ (btos conf.icase))
2303 | 'p' ->
2304 conf.preload <- not conf.preload;
2305 gotoy state.y;
2306 TEdone ("preload " ^ (btos conf.preload))
2308 | 'v' ->
2309 conf.verbose <- not conf.verbose;
2310 TEdone ("verbose " ^ (btos conf.verbose))
2312 | 'd' ->
2313 conf.debug <- not conf.debug;
2314 TEdone ("debug " ^ (btos conf.debug))
2316 | 'h' ->
2317 conf.maxhfit <- not conf.maxhfit;
2318 state.maxy <- calcheight ();
2319 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2321 | 'c' ->
2322 conf.crophack <- not conf.crophack;
2323 TEdone ("crophack " ^ btos conf.crophack)
2325 | 'a' ->
2326 let s =
2327 match conf.maxwait with
2328 | None ->
2329 conf.maxwait <- Some infinity;
2330 "always wait for page to complete"
2331 | Some _ ->
2332 conf.maxwait <- None;
2333 "show placeholder if page is not ready"
2335 TEdone s
2337 | 'f' ->
2338 conf.underinfo <- not conf.underinfo;
2339 TEdone ("underinfo " ^ btos conf.underinfo)
2341 | 'P' ->
2342 conf.savebmarks <- not conf.savebmarks;
2343 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2345 | 'S' ->
2346 let ondone s =
2348 let pageno, py =
2349 match state.layout with
2350 | [] -> 0, 0
2351 | l :: _ ->
2352 l.pageno, l.pagey
2354 conf.interpagespace <- int_of_string s;
2355 docolumns conf.columns;
2356 state.maxy <- calcheight ();
2357 let y = getpagey pageno in
2358 gotoy (y + py)
2359 with exc ->
2360 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2362 TEswitch ("vertical margin: ", E.s, None, intentry, ondone, true)
2364 | 'l' ->
2365 let fm =
2366 match conf.fitmodel with
2367 | FitProportional -> FitWidth
2368 | _ -> FitProportional
2370 reqlayout conf.angle fm;
2371 TEdone ("proportional display " ^ btos (fm == FitProportional))
2373 | 'T' ->
2374 settrim (not conf.trimmargins) conf.trimfuzz;
2375 TEdone ("trim margins " ^ btos conf.trimmargins)
2377 | 'I' ->
2378 conf.invert <- not conf.invert;
2379 TEdone ("invert colors " ^ btos conf.invert)
2381 | 'x' ->
2382 let ondone s =
2383 cbput state.hists.sel s;
2384 conf.selcmd <- s;
2386 TEswitch ("selection command: ", E.s, Some (onhist state.hists.sel),
2387 textentry, ondone, true)
2389 | 'M' ->
2390 if conf.pax == None
2391 then conf.pax <- Some (ref (0.0, 0, 0))
2392 else conf.pax <- None;
2393 TEdone ("PAX " ^ btos (conf.pax != None))
2395 | _ ->
2396 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2397 TEstop
2398 else
2399 TEcont state.text
2402 class type lvsource = object
2403 method getitemcount : int
2404 method getitem : int -> (string * int)
2405 method hasaction : int -> bool
2406 method exit :
2407 uioh:uioh ->
2408 cancel:bool ->
2409 active:int ->
2410 first:int ->
2411 pan:int ->
2412 uioh option
2413 method getactive : int
2414 method getfirst : int
2415 method getpan : int
2416 method getminfo : (int * int) array
2417 end;;
2419 class virtual lvsourcebase = object
2420 val mutable m_active = 0
2421 val mutable m_first = 0
2422 val mutable m_pan = 0
2423 method getactive = m_active
2424 method getfirst = m_first
2425 method getpan = m_pan
2426 method getminfo : (int * int) array = E.a
2427 end;;
2429 let withoutlastutf8 s =
2430 let len = String.length s in
2431 if len = 0
2432 then s
2433 else
2434 let rec find pos =
2435 if pos = 0
2436 then pos
2437 else
2438 let b = Char.code s.[pos] in
2439 if b land 0b11000000 = 0b11000000
2440 then pos
2441 else find (pos-1)
2443 let first =
2444 if Char.code s.[len-1] land 0x80 = 0
2445 then len-1
2446 else find (len-1)
2448 String.sub s 0 first;
2451 let textentrykeyboard
2452 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2453 let key =
2454 if key >= 0xffb0 && key <= 0xffb9
2455 then key - 0xffb0 + 48 else key
2457 let enttext te =
2458 state.mode <- Textentry (te, onleave);
2459 state.text <- E.s;
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 | 0xff08 -> (* 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 | 0xff0d | 0xff8d -> (* (kp) enter *)
2484 ondone text;
2485 onleave Confirm;
2486 G.postRedisplay "textentrykeyboard after confirm"
2488 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
2489 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
2490 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
2491 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
2493 | 0xff1b -> (* 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 | 0xff9f | 0xffff -> () (* delete *)
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 coe s = (s :> uioh);;
2569 class listview ~zebra ~helpmode ~(source:lvsource) ~trusted ~modehash =
2570 object (self)
2571 val m_pan = source#getpan
2572 val m_first = source#getfirst
2573 val m_active = source#getactive
2574 val m_qsearch = E.s
2575 val m_prev_uioh = state.uioh
2577 method private elemunder y =
2578 if y < 0
2579 then None
2580 else
2581 let n = y / (fstate.fontsize+1) in
2582 if m_first + n < source#getitemcount
2583 then (
2584 if source#hasaction (m_first + n)
2585 then Some (m_first + n)
2586 else None
2588 else None
2590 method display =
2591 Gl.enable `blend;
2592 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2593 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2594 filledrect 0. 0. (float state.winw) (float state.winh);
2595 GlDraw.color (1., 1., 1.);
2596 Gl.enable `texture_2d;
2597 let fs = fstate.fontsize in
2598 let nfs = fs + 1 in
2599 let hw = (wadjsb (xadjsb state.winw))/3 in
2600 let ww = fstate.wwidth in
2601 let tabw = 17.0*.ww in
2602 let itemcount = source#getitemcount in
2603 let minfo = source#getminfo in
2604 let x0, x1 =
2605 if conf.leftscroll
2606 then float (xadjsb 0), float (state.winw - 1)
2607 else 0.0, float (state.winw - conf.scrollbw - 1)
2609 let rec loop row =
2610 if (row - m_first) > fstate.maxrows
2611 then ()
2612 else (
2613 if row >= 0 && row < itemcount
2614 then (
2615 let (s, level) = source#getitem row in
2616 let y = (row - m_first) * nfs in
2617 let x =
2618 (if conf.leftscroll then float (xadjsb 0) else 5.0)
2619 +. (float (level + m_pan)) *. ww in
2620 if helpmode
2621 then GlDraw.color
2622 (let c = if row land 1 = 0 then 1.0 else 0.92 in (c,c,c));
2624 if row = m_active
2625 then (
2626 Gl.disable `texture_2d;
2627 let alpha = if source#hasaction row then 0.9 else 0.3 in
2628 GlDraw.color (1., 1., 1.) ~alpha;
2629 linerect (x0 +. 1.) (float (y + 1)) (x1) (float (y + fs + 3));
2630 Gl.enable `texture_2d;
2632 let c =
2633 if zebra && row land 1 = 1
2634 then 0.8
2635 else 1.0
2637 GlDraw.color (c,c,c);
2638 let drawtabularstring s =
2639 let drawstr x s =
2640 let x' = truncate (x0 +. x) in
2641 let pos = nindex s '\000' in
2642 if pos = -1
2643 then drawstring1 fs x' (y+nfs) s
2644 else
2645 let s1 = String.sub s 0 pos
2646 and s2 = String.sub s (pos+1) (String.length s - pos - 1) in
2647 let rec e s =
2648 if emptystr s
2649 then s
2650 else
2651 let s' = withoutlastutf8 s in
2652 let s = s' ^ "\xe2\x80\xa6" in
2653 let w = measurestr fs s in
2654 if float x' +. w +. ww < float (hw + x')
2655 then s
2656 else e s'
2658 let s1 =
2659 if float x' +. ww +. measurestr fs s1 > float (hw + x')
2660 then e s1
2661 else s1
2663 ignore (drawstring1 fs x' (y+nfs) s1);
2664 drawstring1 fs (hw + x') (y+nfs) s2
2666 if trusted
2667 then
2668 let x = if helpmode && row > 0 then x +. ww else x in
2669 let tabpos = nindex s '\t' in
2670 if tabpos > 0
2671 then
2672 let len = String.length s - tabpos - 1 in
2673 let s1 = String.sub s 0 tabpos
2674 and s2 = String.sub s (tabpos + 1) len in
2675 let nx = drawstr x s1 in
2676 let sw = nx -. x in
2677 let x = x +. (max tabw sw) in
2678 drawstr x s2
2679 else
2680 let len = String.length s - 2 in
2681 if len > 0 && s.[0] = '\xc2' && s.[1] = '\xb7'
2682 then
2683 let s = String.sub s 2 len in
2684 let x = if not helpmode then x +. ww else x in
2685 GlDraw.color (1.2, 1.2, 1.2);
2686 let vinc = drawstring1 (fs+fs/4)
2687 (truncate (x -. ww)) (y+nfs) s in
2688 GlDraw.color (1., 1., 1.);
2689 vinc +. (float fs *. 0.8)
2690 else
2691 drawstr x s
2692 else
2693 drawstr x s
2695 ignore (drawtabularstring s);
2696 loop (row+1)
2700 loop m_first;
2701 GlDraw.color (1.0, 1.0, 1.0) ~alpha:0.5;
2702 let rec loop row =
2703 if (row - m_first) > fstate.maxrows
2704 then ()
2705 else (
2706 if row >= 0 && row < itemcount
2707 then (
2708 let (s, level) = source#getitem row in
2709 let pos0 = nindex s '\000' in
2710 let y = (row - m_first) * nfs in
2711 let x = float (level + m_pan) *. ww in
2712 let (first, last) = minfo.(row) in
2713 let prefix =
2714 if pos0 > 0 && first > pos0
2715 then String.sub s (pos0+1) (first-pos0-1)
2716 else String.sub s 0 first
2718 let suffix = String.sub s first (last - first) in
2719 let w1 = measurestr fstate.fontsize prefix in
2720 let w2 = measurestr fstate.fontsize suffix in
2721 let x = x +. if conf.leftscroll then float (xadjsb 5) else 5.0 in
2722 let x = if pos0 > 0 && first > pos0 then x +. float hw else x in
2723 let x0 = x +. w1
2724 and y0 = float (y+2) in
2725 let x1 = x0 +. w2
2726 and y1 = float (y+fs+3) in
2727 filledrect x0 y0 x1 y1;
2728 loop (row+1)
2732 Gl.disable `texture_2d;
2733 if Array.length minfo > 0 then loop m_first;
2734 Gl.disable `blend;
2736 method updownlevel incr =
2737 let len = source#getitemcount in
2738 let curlevel =
2739 if m_active >= 0 && m_active < len
2740 then snd (source#getitem m_active)
2741 else -1
2743 let rec flow i =
2744 if i = len then i-1 else if i = -1 then 0 else
2745 let _, l = source#getitem i in
2746 if l != curlevel then i else flow (i+incr)
2748 let active = flow m_active in
2749 let first = calcfirst m_first active in
2750 G.postRedisplay "outline updownlevel";
2751 {< m_active = active; m_first = first >}
2753 method private key1 key mask =
2754 let set1 active first qsearch =
2755 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2757 let search active pattern incr =
2758 let active = if active = -1 then m_first else active in
2759 let dosearch re =
2760 let rec loop n =
2761 if n >= 0 && n < source#getitemcount
2762 then (
2763 let s, _ = source#getitem n in
2765 (try ignore (Str.search_forward re s 0); true
2766 with Not_found -> false)
2767 then Some n
2768 else loop (n + incr)
2770 else None
2772 loop active
2775 let re = Str.regexp_case_fold pattern in
2776 dosearch re
2777 with Failure s ->
2778 state.text <- s;
2779 None
2781 let itemcount = source#getitemcount in
2782 let find start incr =
2783 let rec find i =
2784 if i = -1 || i = itemcount
2785 then -1
2786 else (
2787 if source#hasaction i
2788 then i
2789 else find (i + incr)
2792 find start
2794 let set active first =
2795 let first = bound first 0 (itemcount - fstate.maxrows) in
2796 state.text <- E.s;
2797 coe {< m_active = active; m_first = first; m_qsearch = E.s >}
2799 let navigate incr =
2800 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2801 let active, first =
2802 let incr1 = if incr > 0 then 1 else -1 in
2803 if isvisible m_first m_active
2804 then
2805 let next =
2806 let next = m_active + incr in
2807 let next =
2808 if next < 0 || next >= itemcount
2809 then -1
2810 else find next incr1
2812 if abs (m_active - next) > fstate.maxrows
2813 then -1
2814 else next
2816 if next = -1
2817 then
2818 let first = m_first + incr in
2819 let first = bound first 0 (itemcount - fstate.maxrows) in
2820 let next =
2821 let next = m_active + incr in
2822 let next = bound next 0 (itemcount - 1) in
2823 find next ~-incr1
2825 let active =
2826 if next = -1
2827 then m_active
2828 else (
2829 if isvisible first next
2830 then next
2831 else m_active
2834 active, first
2835 else
2836 let first = min next m_first in
2837 let first =
2838 if abs (next - first) > fstate.maxrows
2839 then first + incr
2840 else first
2842 next, first
2843 else
2844 let first = m_first + incr in
2845 let first = bound first 0 (itemcount - 1) in
2846 let active =
2847 let next = m_active + incr in
2848 let next = bound next 0 (itemcount - 1) in
2849 let next = find next incr1 in
2850 let active =
2851 if next = -1 || abs (m_active - first) > fstate.maxrows
2852 then (
2853 let active = if m_active = -1 then next else m_active in
2854 active
2856 else next
2858 if isvisible first active
2859 then active
2860 else -1
2862 active, first
2864 G.postRedisplay "listview navigate";
2865 set active first;
2867 match key with
2868 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
2869 let incr = if key = 0x72 then -1 else 1 in
2870 let active, first =
2871 match search (m_active + incr) m_qsearch incr with
2872 | None ->
2873 state.text <- m_qsearch ^ " [not found]";
2874 m_active, m_first
2875 | Some active ->
2876 state.text <- m_qsearch;
2877 active, firstof m_first active
2879 G.postRedisplay "listview ctrl-r/s";
2880 set1 active first m_qsearch;
2882 | 0xff63 when Wsi.withctrl mask -> (* ctrl-insert *)
2883 if m_active >= 0 && m_active < source#getitemcount
2884 then (
2885 let s, _ = source#getitem m_active in
2886 selstring s;
2888 coe self
2890 | 0xff08 -> (* backspace *)
2891 if emptystr m_qsearch
2892 then coe self
2893 else (
2894 let qsearch = withoutlastutf8 m_qsearch in
2895 if emptystr qsearch
2896 then (
2897 state.text <- E.s;
2898 G.postRedisplay "listview empty qsearch";
2899 set1 m_active m_first E.s;
2901 else
2902 let active, first =
2903 match search m_active qsearch ~-1 with
2904 | None ->
2905 state.text <- qsearch ^ " [not found]";
2906 m_active, m_first
2907 | Some active ->
2908 state.text <- qsearch;
2909 active, firstof m_first active
2911 G.postRedisplay "listview backspace qsearch";
2912 set1 active first qsearch
2915 | key when (key != 0 && key land 0xff00 != 0xff00) ->
2916 let pattern = m_qsearch ^ toutf8 key in
2917 let active, first =
2918 match search m_active pattern 1 with
2919 | None ->
2920 state.text <- pattern ^ " [not found]";
2921 m_active, m_first
2922 | Some active ->
2923 state.text <- pattern;
2924 active, firstof m_first active
2926 G.postRedisplay "listview qsearch add";
2927 set1 active first pattern;
2929 | 0xff1b -> (* escape *)
2930 state.text <- E.s;
2931 if emptystr m_qsearch
2932 then (
2933 G.postRedisplay "list view escape";
2934 begin
2935 match
2936 source#exit (coe self) true m_active m_first m_pan
2937 with
2938 | None -> m_prev_uioh
2939 | Some uioh -> uioh
2942 else (
2943 G.postRedisplay "list view kill qsearch";
2944 coe {< m_qsearch = E.s >}
2947 | 0xff0d | 0xff8d -> (* (kp) enter *)
2948 state.text <- E.s;
2949 let self = {< m_qsearch = E.s >} in
2950 let opt =
2951 G.postRedisplay "listview enter";
2952 if m_active >= 0 && m_active < source#getitemcount
2953 then (
2954 source#exit (coe self) false m_active m_first m_pan;
2956 else (
2957 source#exit (coe self) true m_active m_first m_pan;
2960 begin match opt with
2961 | None -> m_prev_uioh
2962 | Some uioh -> uioh
2965 | 0xff9f | 0xffff -> (* (kp) delete *)
2966 coe self
2968 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
2969 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
2970 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
2971 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
2973 | 0xff53 | 0xff98 -> (* (kp) right *)
2974 state.text <- E.s;
2975 G.postRedisplay "listview right";
2976 coe {< m_pan = m_pan - 1 >}
2978 | 0xff51 | 0xff96 -> (* (kp) left *)
2979 state.text <- E.s;
2980 G.postRedisplay "listview left";
2981 coe {< m_pan = m_pan + 1 >}
2983 | 0xff50 | 0xff95 -> (* (kp) home *)
2984 let active = find 0 1 in
2985 G.postRedisplay "listview home";
2986 set active 0;
2988 | 0xff57 | 0xff9c -> (* (kp) end *)
2989 let first = max 0 (itemcount - fstate.maxrows) in
2990 let active = find (itemcount - 1) ~-1 in
2991 G.postRedisplay "listview end";
2992 set active first;
2994 | key when (key = 0 || key land 0xff00 = 0xff00) ->
2995 coe self
2997 | _ ->
2998 dolog "listview unknown key %#x" key; coe self
3000 method key key mask =
3001 match state.mode with
3002 | Textentry te -> textentrykeyboard key mask te; coe self
3003 | _ -> self#key1 key mask
3005 method button button down x y _ =
3006 let opt =
3007 match button with
3008 | 1 when x > state.winw - conf.scrollbw ->
3009 G.postRedisplay "listview scroll";
3010 if down
3011 then
3012 let _, position, sh = self#scrollph in
3013 if y > truncate position && y < truncate (position +. sh)
3014 then (
3015 state.mstate <- Mscrolly;
3016 Some (coe self)
3018 else
3019 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3020 let first = truncate (s *. float source#getitemcount) in
3021 let first = min source#getitemcount first in
3022 Some (coe {< m_first = first; m_active = first >})
3023 else (
3024 state.mstate <- Mnone;
3025 Some (coe self);
3027 | 1 when not down ->
3028 begin match self#elemunder y with
3029 | Some n ->
3030 G.postRedisplay "listview click";
3031 source#exit (coe {< m_active = n >}) false n m_first m_pan
3032 | _ ->
3033 Some (coe self)
3035 | n when (n == 4 || n == 5) && not down ->
3036 let len = source#getitemcount in
3037 let first =
3038 if n = 5 && m_first + fstate.maxrows >= len
3039 then
3040 m_first
3041 else
3042 let first = m_first + (if n == 4 then -1 else 1) in
3043 bound first 0 (len - 1)
3045 G.postRedisplay "listview wheel";
3046 Some (coe {< m_first = first >})
3047 | n when (n = 6 || n = 7) && not down ->
3048 let inc = if n = 7 then -1 else 1 in
3049 G.postRedisplay "listview hwheel";
3050 Some (coe {< m_pan = m_pan + inc >})
3051 | _ ->
3052 Some (coe self)
3054 match opt with
3055 | None -> m_prev_uioh
3056 | Some uioh -> uioh
3058 method multiclick _ x y = self#button 1 true x y
3060 method motion _ y =
3061 match state.mstate with
3062 | Mscrolly ->
3063 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3064 let first = truncate (s *. float source#getitemcount) in
3065 let first = min source#getitemcount first in
3066 G.postRedisplay "listview motion";
3067 coe {< m_first = first; m_active = first >}
3068 | _ -> coe self
3070 method pmotion x y =
3071 if x < state.winw - conf.scrollbw
3072 then
3073 let n =
3074 match self#elemunder y with
3075 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3076 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3078 let o =
3079 if n != m_active
3080 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3081 else self
3083 coe o
3084 else (
3085 Wsi.setcursor Wsi.CURSOR_INHERIT;
3086 coe self
3089 method infochanged _ = ()
3091 method scrollpw = (0, 0.0, 0.0)
3092 method scrollph =
3093 let nfs = fstate.fontsize + 1 in
3094 let y = m_first * nfs in
3095 let itemcount = source#getitemcount in
3096 let maxi = max 0 (itemcount - fstate.maxrows) in
3097 let maxy = maxi * nfs in
3098 let p, h = scrollph y maxy in
3099 conf.scrollbw, p, h
3101 method modehash = modehash
3102 method eformsgs = false
3103 end;;
3105 class outlinelistview ~zebra ~source =
3106 let settext autonarrow s =
3107 if autonarrow
3108 then
3109 let ss = source#statestr in
3110 state.text <-
3111 if emptystr ss
3112 then "[" ^ s ^ "]"
3113 else "{" ^ ss ^ "} [" ^ s ^ "]"
3114 else state.text <- s
3116 object (self)
3117 inherit listview
3118 ~zebra
3119 ~helpmode:false
3120 ~source:(source :> lvsource)
3121 ~trusted:false
3122 ~modehash:(findkeyhash conf "outline")
3123 as super
3125 val m_autonarrow = false
3127 method key key mask =
3128 let maxrows =
3129 if emptystr state.text
3130 then fstate.maxrows
3131 else fstate.maxrows - 2
3133 let calcfirst first active =
3134 if active > first
3135 then
3136 let rows = active - first in
3137 if rows > maxrows then active - maxrows else first
3138 else active
3140 let navigate incr =
3141 let active = m_active + incr in
3142 let active = bound active 0 (source#getitemcount - 1) in
3143 let first = calcfirst m_first active in
3144 G.postRedisplay "outline navigate";
3145 coe {< m_active = active; m_first = first >}
3147 let navscroll first =
3148 let active =
3149 let dist = m_active - first in
3150 if dist < 0
3151 then first
3152 else (
3153 if dist < maxrows
3154 then m_active
3155 else first + maxrows
3158 G.postRedisplay "outline navscroll";
3159 coe {< m_first = first; m_active = active >}
3161 let ctrl = Wsi.withctrl mask in
3162 match key with
3163 | 97 when ctrl -> (* ctrl-a *)
3164 let text =
3165 if m_autonarrow
3166 then (source#denarrow; E.s)
3167 else (
3168 let pattern = source#renarrow in
3169 if nonemptystr m_qsearch
3170 then (source#narrow m_qsearch; m_qsearch)
3171 else pattern
3174 settext (not m_autonarrow) text;
3175 G.postRedisplay "toggle auto narrowing";
3176 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
3178 | 47 when emptystr m_qsearch && not m_autonarrow -> (* / *)
3179 settext true E.s;
3180 G.postRedisplay "toggle auto narrowing";
3181 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
3183 | 110 when ctrl -> (* ctrl-n *)
3184 source#narrow m_qsearch;
3185 if not m_autonarrow
3186 then source#add_narrow_pattern m_qsearch;
3187 G.postRedisplay "outline ctrl-n";
3188 coe {< m_first = 0; m_active = 0 >}
3190 | 83 when ctrl -> (* ctrl-S *)
3191 let active = source#calcactive (getanchor ()) in
3192 let first = firstof m_first active in
3193 G.postRedisplay "outline ctrl-s";
3194 coe {< m_first = first; m_active = active >}
3196 | 117 when ctrl -> (* ctrl-u *)
3197 G.postRedisplay "outline ctrl-u";
3198 if m_autonarrow && nonemptystr m_qsearch
3199 then (
3200 ignore (source#renarrow);
3201 settext m_autonarrow E.s;
3202 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3204 else (
3205 source#del_narrow_pattern;
3206 let pattern = source#renarrow in
3207 let text =
3208 if emptystr pattern then E.s else "Narrowed to " ^ pattern
3210 settext m_autonarrow text;
3211 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3214 | 108 when ctrl -> (* ctrl-l *)
3215 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3216 G.postRedisplay "outline ctrl-l";
3217 coe {< m_first = first >}
3219 | 0xff09 when m_autonarrow -> (* tab *)
3220 if nonemptystr m_qsearch
3221 then (
3222 G.postRedisplay "outline list view tab";
3223 source#add_narrow_pattern m_qsearch;
3224 settext true E.s;
3225 coe {< m_qsearch = E.s >}
3227 else coe self
3229 | 0xff1b when m_autonarrow -> (* escape *)
3230 if nonemptystr m_qsearch
3231 then source#add_narrow_pattern m_qsearch;
3232 super#key key mask
3234 | 0xff0d | 0xff8d when m_autonarrow -> (* (kp) enter *)
3235 if nonemptystr m_qsearch
3236 then source#add_narrow_pattern m_qsearch;
3237 super#key key mask
3239 | key when m_autonarrow && (key != 0 && key land 0xff00 != 0xff00) ->
3240 let pattern = m_qsearch ^ toutf8 key in
3241 G.postRedisplay "outlinelistview autonarrow add";
3242 source#narrow pattern;
3243 settext true pattern;
3244 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3246 | key when m_autonarrow && key = 0xff08 -> (* backspace *)
3247 if emptystr m_qsearch
3248 then coe self
3249 else
3250 let pattern = withoutlastutf8 m_qsearch in
3251 G.postRedisplay "outlinelistview autonarrow backspace";
3252 ignore (source#renarrow);
3253 source#narrow pattern;
3254 settext true pattern;
3255 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3257 | 0xff9f | 0xffff -> (* (kp) delete *)
3258 source#remove m_active;
3259 G.postRedisplay "outline delete";
3260 let active = max 0 (m_active-1) in
3261 coe {< m_first = firstof m_first active;
3262 m_active = active >}
3264 | 0xff52 | 0xff97 when ctrl -> (* ctrl-(kp) up *)
3265 navscroll (max 0 (m_first - 1))
3267 | 0xff54 | 0xff99 when ctrl -> (* ctrl-(kp) down *)
3268 navscroll (min (source#getitemcount - 1) (m_first + 1))
3270 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3271 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3272 | 0xff55 | 0xff9a -> (* (kp) prior *)
3273 navigate ~-(fstate.maxrows)
3274 | 0xff56 | 0xff9b -> (* (kp) next *)
3275 navigate fstate.maxrows
3277 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
3278 let o =
3279 if ctrl
3280 then (
3281 G.postRedisplay "outline ctrl right";
3282 {< m_pan = m_pan + 1 >}
3284 else self#updownlevel 1
3286 coe o
3288 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
3289 let o =
3290 if ctrl
3291 then (
3292 G.postRedisplay "outline ctrl left";
3293 {< m_pan = m_pan - 1 >}
3295 else self#updownlevel ~-1
3297 coe o
3299 | 0xff50 | 0xff95 -> (* (kp) home *)
3300 G.postRedisplay "outline home";
3301 coe {< m_first = 0; m_active = 0 >}
3303 | 0xff57 | 0xff9c -> (* (kp) end *)
3304 let active = source#getitemcount - 1 in
3305 let first = max 0 (active - fstate.maxrows) in
3306 G.postRedisplay "outline end";
3307 coe {< m_active = active; m_first = first >}
3309 | _ -> super#key key mask
3312 let gotounder under =
3313 let getpath filename =
3314 let path =
3315 if nonemptystr filename
3316 then
3317 if Filename.is_relative filename
3318 then
3319 let dir = Filename.dirname state.path in
3320 let dir =
3321 if Filename.is_implicit dir
3322 then Filename.concat (Sys.getcwd ()) dir
3323 else dir
3325 Filename.concat dir filename
3326 else filename
3327 else E.s
3329 if Sys.file_exists path
3330 then path
3331 else E.s
3333 match under with
3334 | Ulinkgoto (pageno, top) ->
3335 if pageno >= 0
3336 then (
3337 addnav ();
3338 gotopage1 pageno top;
3341 | Ulinkuri s ->
3342 gotouri s
3344 | Uremote (filename, pageno) ->
3345 let path = getpath filename in
3346 if nonemptystr path
3347 then (
3348 if conf.riani
3349 then
3350 let command = Printf.sprintf "%s -page %d %S" !selfexec pageno path in
3351 try popen command []
3352 with exn ->
3353 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
3354 flush stderr;
3355 else
3356 let anchor = getanchor () in
3357 let ranchor = state.path, state.password, anchor, state.origin in
3358 state.origin <- E.s;
3359 state.anchor <- (pageno, 0.0, 0.0);
3360 state.ranchors <- ranchor :: state.ranchors;
3361 opendoc path E.s;
3363 else showtext '!' ("Could not find " ^ filename)
3365 | Uremotedest (filename, destname) ->
3366 let path = getpath filename in
3367 if nonemptystr path
3368 then (
3369 if conf.riani
3370 then
3371 let command = !selfexec ^ " " ^ path ^ " -dest " ^ destname in
3372 try popen command []
3373 with exn ->
3374 Printf.eprintf
3375 "failed to execute `%s': %s\n" command (exntos exn);
3376 flush stderr;
3377 else
3378 let anchor = getanchor () in
3379 let ranchor = state.path, state.password, anchor, state.origin in
3380 state.origin <- E.s;
3381 state.nameddest <- destname;
3382 state.ranchors <- ranchor :: state.ranchors;
3383 opendoc path E.s;
3385 else showtext '!' ("Could not find " ^ filename)
3387 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
3390 let gotohist (path, (c, bookmarks, x, anchor)) =
3391 Config.save leavebirdseye;
3392 state.anchor <- anchor;
3393 state.x <- x;
3394 state.bookmarks <- bookmarks;
3395 state.origin <- E.s;
3396 opendoc path E.s;
3397 conf.presentation <- c.presentation;
3400 let gotooutline (_, _, kind) =
3401 match kind with
3402 | Onone -> ()
3403 | Oanchor anchor ->
3404 let (pageno, y, _) = anchor in
3405 let y = getanchory
3406 (if conf.presentation then (pageno, y, 1.0) else anchor)
3408 addnav ();
3409 gotoghyll y
3410 | Ouri uri -> gotounder (Ulinkuri uri)
3411 | Olaunch cmd -> gotounder (Ulaunch cmd)
3412 | Oremote remote -> gotounder (Uremote remote)
3413 | Ohistory hist -> gotohist hist
3414 | Oremotedest remotedest -> gotounder (Uremotedest remotedest)
3415 | Oaction f -> f ()
3418 let genhistoutlines =
3419 let order ty (p1, c1, _, _, _) (p2, c2, _, _, _) =
3420 match ty with
3421 | `lastvisit -> compare c2.lastvisit c1.lastvisit
3422 | `path -> compare p2 p1
3423 | `file -> compare (Filename.basename p2) (Filename.basename p1)
3424 | `title -> compare c2.title c1.title
3426 fun orderty ->
3427 let setorty s t =
3428 let s =
3429 if orderty = t then "{ " ^ s ^ " }" else " " ^ s
3430 in s, 0,
3431 Oaction (fun () -> Config.historder := t)
3433 let list = ref [] in
3434 if Config.gethist list
3435 then
3436 let ol =
3437 List.fold_left
3438 (fun accu (path, c, b, x, a) ->
3439 let hist = (path, (c, b, x, a)) in
3440 let base = mbtoutf8 (Filename.basename path) in
3441 (base ^ "\000" ^ c.title, 1, Ohistory hist) :: accu
3443 [ setorty "Sort by time of last visit" `lastvisit;
3444 setorty "Sort by file name" `file;
3445 setorty "Sort by path" `path;
3446 setorty "Sort by title" `title
3447 ] (List.sort (order orderty) !list)
3449 Array.of_list ol
3450 else E.a;
3453 let outlinesource sourcetype =
3454 (object (self)
3455 inherit lvsourcebase
3456 val mutable m_items = E.a
3457 val mutable m_minfo = E.a
3458 val mutable m_orig_items = E.a
3459 val mutable m_orig_minfo = E.a
3460 val mutable m_narrow_patterns = []
3461 val mutable m_hadremovals = false
3462 val mutable m_gen = -1
3464 method getitemcount =
3465 Array.length m_items + (if m_hadremovals then 1 else 0)
3467 method getitem n =
3468 if n == Array.length m_items && m_hadremovals
3469 then
3470 ("[Confirm removal]", 0)
3471 else
3472 let s, n, _ = m_items.(n) in
3473 (s, n)
3475 method exit ~uioh ~cancel ~active ~first ~pan =
3476 ignore (uioh, first);
3477 let confrimremoval = m_hadremovals && active = Array.length m_items in
3478 let items, minfo =
3479 if m_narrow_patterns = []
3480 then m_orig_items, m_orig_minfo
3481 else m_items, m_minfo
3483 if not cancel
3484 then (
3485 if not confrimremoval
3486 then (
3487 gotooutline m_items.(active);
3488 m_items <- items;
3489 m_minfo <- minfo;
3491 else (
3492 state.bookmarks <- Array.to_list m_items;
3493 m_orig_items <- m_items;
3494 m_orig_minfo <- m_minfo;
3497 else (
3498 m_items <- items;
3499 m_minfo <- minfo;
3501 m_pan <- pan;
3502 None
3504 method hasaction _ = true
3506 method greetmsg =
3507 if Array.length m_items != Array.length m_orig_items
3508 then
3509 let s =
3510 match m_narrow_patterns with
3511 | one :: [] -> one
3512 | many -> String.concat "\xe2\x80\xa6" (List.rev many)
3514 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
3515 else E.s
3517 method statestr =
3518 match m_narrow_patterns with
3519 | [] -> E.s
3520 | one :: [] -> one
3521 | head :: _ -> "\xe2\x80\xa6" ^ head
3523 method narrow pattern =
3524 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3525 match reopt with
3526 | None -> ()
3527 | Some re ->
3528 let rec loop accu minfo n =
3529 if n = -1
3530 then (
3531 m_items <- Array.of_list accu;
3532 m_minfo <- Array.of_list minfo;
3534 else
3535 let (s, _, t) as o = m_items.(n) in
3536 let accu, minfo =
3537 match t with
3538 | Oaction _ -> o :: accu, (0, 0) :: minfo
3539 | Onone | Oanchor _ | Ouri _ | Olaunch _
3540 | Oremote _ | Oremotedest _ | Ohistory _ ->
3541 let first =
3542 try Str.search_forward re s 0
3543 with Not_found -> -1
3545 if first >= 0
3546 then o :: accu, (first, Str.match_end ()) :: minfo
3547 else accu, minfo
3549 loop accu minfo (n-1)
3551 loop [] [] (Array.length m_items - 1)
3553 method getminfo = m_minfo
3555 method denarrow =
3556 m_orig_items <- (
3557 match sourcetype with
3558 | `bookmarks -> Array.of_list state.bookmarks
3559 | `outlines -> state.outlines
3560 | `history -> genhistoutlines !Config.historder
3562 m_minfo <- m_orig_minfo;
3563 m_items <- m_orig_items
3565 method remove m =
3566 if sourcetype = `bookmarks
3567 then
3568 if m >= 0 && m < Array.length m_items
3569 then (
3570 m_hadremovals <- true;
3571 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3572 let n = if n >= m then n+1 else n in
3573 m_items.(n)
3577 method add_narrow_pattern pattern =
3578 m_narrow_patterns <- pattern :: m_narrow_patterns
3580 method del_narrow_pattern =
3581 match m_narrow_patterns with
3582 | _ :: rest -> m_narrow_patterns <- rest
3583 | [] -> ()
3585 method renarrow =
3586 self#denarrow;
3587 match m_narrow_patterns with
3588 | pattern :: [] -> self#narrow pattern; pattern
3589 | list ->
3590 List.fold_left (fun accu pattern ->
3591 self#narrow pattern;
3592 pattern ^ "\xe2\x80\xa6" ^ accu) E.s list
3594 method calcactive anchor =
3595 let rely = getanchory anchor in
3596 let rec loop n best bestd =
3597 if n = Array.length m_items
3598 then best
3599 else
3600 let _, _, kind = m_items.(n) in
3601 match kind with
3602 | Oanchor anchor ->
3603 let orely = getanchory anchor in
3604 let d = abs (orely - rely) in
3605 if d < bestd
3606 then loop (n+1) n d
3607 else loop (n+1) best bestd
3608 | Onone | Oremote _ | Olaunch _
3609 | Oremotedest _ | Ouri _ | Ohistory _ | Oaction _ ->
3610 loop (n+1) best bestd
3612 loop 0 ~-1 max_int
3614 method reset anchor items =
3615 m_hadremovals <- false;
3616 if state.gen != m_gen
3617 then (
3618 m_orig_items <- items;
3619 m_items <- items;
3620 m_narrow_patterns <- [];
3621 m_minfo <- E.a;
3622 m_orig_minfo <- E.a;
3623 m_gen <- state.gen;
3625 else (
3626 if items != m_orig_items
3627 then (
3628 m_orig_items <- items;
3629 if m_narrow_patterns == []
3630 then m_items <- items;
3633 let active = self#calcactive anchor in
3634 m_active <- active;
3635 m_first <- firstof m_first active
3636 end)
3639 let enterselector sourcetype =
3640 resetmstate ();
3641 let source = outlinesource sourcetype in
3642 fun errmsg ->
3643 let outlines =
3644 match sourcetype with
3645 | `bookmarks -> Array.of_list state.bookmarks
3646 | `outlines -> state.outlines
3647 | `history -> genhistoutlines !Config.historder
3649 if Array.length outlines = 0
3650 then (
3651 showtext ' ' errmsg;
3653 else (
3654 state.text <- source#greetmsg;
3655 Wsi.setcursor Wsi.CURSOR_INHERIT;
3656 let anchor = getanchor () in
3657 source#reset anchor outlines;
3658 state.uioh <-
3659 coe (new outlinelistview ~zebra:(sourcetype=`history) ~source);
3660 G.postRedisplay "enter selector";
3664 let enteroutlinemode =
3665 let f = enterselector `outlines in
3666 fun () -> f "Document has no outline";
3669 let enterbookmarkmode =
3670 let f = enterselector `bookmarks in
3671 fun () -> f "Document has no bookmarks (yet)";
3674 let enterhistmode () = enterselector `history "No history (yet)";;
3676 let makecheckers () =
3677 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3678 following to say:
3679 converted by Issac Trotts. July 25, 2002 *)
3680 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3681 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3682 let id = GlTex.gen_texture () in
3683 GlTex.bind_texture `texture_2d id;
3684 GlPix.store (`unpack_alignment 1);
3685 GlTex.image2d image;
3686 List.iter (GlTex.parameter ~target:`texture_2d)
3687 [ `mag_filter `nearest; `min_filter `nearest ];
3691 let setcheckers enabled =
3692 match state.checkerstexid with
3693 | None ->
3694 if enabled then state.checkerstexid <- Some (makecheckers ())
3696 | Some checkerstexid ->
3697 if not enabled
3698 then (
3699 GlTex.delete_texture checkerstexid;
3700 state.checkerstexid <- None;
3704 let describe_location () =
3705 let fn = page_of_y state.y in
3706 let ln = page_of_y (state.y + state.winh - hscrollh () - 1) in
3707 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
3708 let percent =
3709 if maxy <= 0
3710 then 100.
3711 else (100. *. (float state.y /. float maxy))
3713 if fn = ln
3714 then
3715 Printf.sprintf "page %d of %d [%.2f%%]"
3716 (fn+1) state.pagecount percent
3717 else
3718 Printf.sprintf
3719 "pages %d-%d of %d [%.2f%%]"
3720 (fn+1) (ln+1) state.pagecount percent
3723 let setpresentationmode v =
3724 let n = page_of_y state.y in
3725 state.anchor <- (n, 0.0, 1.0);
3726 conf.presentation <- v;
3727 if conf.fitmodel = FitPage
3728 then reqlayout conf.angle conf.fitmodel;
3729 represent ();
3732 let enterinfomode =
3733 let btos b = if b then "\xe2\x88\x9a" else E.s in
3734 let showextended = ref false in
3735 let leave mode = function
3736 | Confirm -> state.mode <- mode
3737 | Cancel -> state.mode <- mode in
3738 let src =
3739 (object
3740 val mutable m_first_time = true
3741 val mutable m_l = []
3742 val mutable m_a = E.a
3743 val mutable m_prev_uioh = nouioh
3744 val mutable m_prev_mode = View
3746 inherit lvsourcebase
3748 method reset prev_mode prev_uioh =
3749 m_a <- Array.of_list (List.rev m_l);
3750 m_l <- [];
3751 m_prev_mode <- prev_mode;
3752 m_prev_uioh <- prev_uioh;
3753 if m_first_time
3754 then (
3755 let rec loop n =
3756 if n >= Array.length m_a
3757 then ()
3758 else
3759 match m_a.(n) with
3760 | _, _, _, Action _ -> m_active <- n
3761 | _ -> loop (n+1)
3763 loop 0;
3764 m_first_time <- false;
3767 method int name get set =
3768 m_l <-
3769 (name, `int get, 1, Action (
3770 fun u ->
3771 let ondone s =
3772 try set (int_of_string s)
3773 with exn ->
3774 state.text <- Printf.sprintf "bad integer `%s': %s"
3775 s (exntos exn)
3777 state.text <- E.s;
3778 let te = name ^ ": ", E.s, None, intentry, ondone, true in
3779 state.mode <- Textentry (te, leave m_prev_mode);
3781 )) :: m_l
3783 method int_with_suffix name get set =
3784 m_l <-
3785 (name, `intws get, 1, Action (
3786 fun u ->
3787 let ondone s =
3788 try set (int_of_string_with_suffix s)
3789 with exn ->
3790 state.text <- Printf.sprintf "bad integer `%s': %s"
3791 s (exntos exn)
3793 state.text <- E.s;
3794 let te =
3795 name ^ ": ", E.s, None, intentry_with_suffix, ondone, true
3797 state.mode <- Textentry (te, leave m_prev_mode);
3799 )) :: m_l
3801 method bool ?(offset=1) ?(btos=btos) name get set =
3802 m_l <-
3803 (name, `bool (btos, get), offset, Action (
3804 fun u ->
3805 let v = get () in
3806 set (not v);
3808 )) :: m_l
3810 method color name get set =
3811 m_l <-
3812 (name, `color get, 1, Action (
3813 fun u ->
3814 let invalid = (nan, nan, nan) in
3815 let ondone s =
3816 let c =
3817 try color_of_string s
3818 with exn ->
3819 state.text <- Printf.sprintf "bad color `%s': %s"
3820 s (exntos exn);
3821 invalid
3823 if c <> invalid
3824 then set c;
3826 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3827 state.text <- color_to_string (get ());
3828 state.mode <- Textentry (te, leave m_prev_mode);
3830 )) :: m_l
3832 method string name get set =
3833 m_l <-
3834 (name, `string get, 1, Action (
3835 fun u ->
3836 let ondone s = set s in
3837 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3838 state.mode <- Textentry (te, leave m_prev_mode);
3840 )) :: m_l
3842 method colorspace name get set =
3843 m_l <-
3844 (name, `string get, 1, Action (
3845 fun _ ->
3846 let source =
3847 (object
3848 inherit lvsourcebase
3850 initializer
3851 m_active <- CSTE.to_int conf.colorspace;
3852 m_first <- 0;
3854 method getitemcount =
3855 Array.length CSTE.names
3856 method getitem n =
3857 (CSTE.names.(n), 0)
3858 method exit ~uioh ~cancel ~active ~first ~pan =
3859 ignore (uioh, first, pan);
3860 if not cancel then set active;
3861 None
3862 method hasaction _ = true
3863 end)
3865 state.text <- E.s;
3866 let modehash = findkeyhash conf "info" in
3867 coe (new listview ~zebra:false ~helpmode:false
3868 ~source ~trusted:true ~modehash)
3869 )) :: m_l
3871 method paxmark name get set =
3872 m_l <-
3873 (name, `string get, 1, Action (
3874 fun _ ->
3875 let source =
3876 (object
3877 inherit lvsourcebase
3879 initializer
3880 m_active <- MTE.to_int conf.paxmark;
3881 m_first <- 0;
3883 method getitemcount = Array.length MTE.names
3884 method getitem n = (MTE.names.(n), 0)
3885 method exit ~uioh ~cancel ~active ~first ~pan =
3886 ignore (uioh, first, pan);
3887 if not cancel then set active;
3888 None
3889 method hasaction _ = true
3890 end)
3892 state.text <- E.s;
3893 let modehash = findkeyhash conf "info" in
3894 coe (new listview ~zebra:false ~helpmode:false
3895 ~source ~trusted:true ~modehash)
3896 )) :: m_l
3898 method fitmodel name get set =
3899 m_l <-
3900 (name, `string get, 1, Action (
3901 fun _ ->
3902 let source =
3903 (object
3904 inherit lvsourcebase
3906 initializer
3907 m_active <- FMTE.to_int conf.fitmodel;
3908 m_first <- 0;
3910 method getitemcount = Array.length FMTE.names
3911 method getitem n = (FMTE.names.(n), 0)
3912 method exit ~uioh ~cancel ~active ~first ~pan =
3913 ignore (uioh, first, pan);
3914 if not cancel then set active;
3915 None
3916 method hasaction _ = true
3917 end)
3919 state.text <- E.s;
3920 let modehash = findkeyhash conf "info" in
3921 coe (new listview ~zebra:false ~helpmode:false
3922 ~source ~trusted:true ~modehash)
3923 )) :: m_l
3925 method caption s offset =
3926 m_l <- (s, `empty, offset, Noaction) :: m_l
3928 method caption2 s f offset =
3929 m_l <- (s, `string f, offset, Noaction) :: m_l
3931 method getitemcount = Array.length m_a
3933 method getitem n =
3934 let tostr = function
3935 | `int f -> string_of_int (f ())
3936 | `intws f -> string_with_suffix_of_int (f ())
3937 | `string f -> f ()
3938 | `color f -> color_to_string (f ())
3939 | `bool (btos, f) -> btos (f ())
3940 | `empty -> E.s
3942 let name, t, offset, _ = m_a.(n) in
3943 ((let s = tostr t in
3944 if nonemptystr s
3945 then Printf.sprintf "%s\t%s" name s
3946 else name),
3947 offset)
3949 method exit ~uioh ~cancel ~active ~first ~pan =
3950 let uiohopt =
3951 if not cancel
3952 then (
3953 let uioh =
3954 match m_a.(active) with
3955 | _, _, _, Action f -> f uioh
3956 | _ -> uioh
3958 Some uioh
3960 else None
3962 m_active <- active;
3963 m_first <- first;
3964 m_pan <- pan;
3965 uiohopt
3967 method hasaction n =
3968 match m_a.(n) with
3969 | _, _, _, Action _ -> true
3970 | _ -> false
3971 end)
3973 let rec fillsrc prevmode prevuioh =
3974 let sep () = src#caption E.s 0 in
3975 let colorp name get set =
3976 src#string name
3977 (fun () -> color_to_string (get ()))
3978 (fun v ->
3980 let c = color_of_string v in
3981 set c
3982 with exn ->
3983 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
3986 let oldmode = state.mode in
3987 let birdseye = isbirdseye state.mode in
3989 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3991 src#bool "presentation mode"
3992 (fun () -> conf.presentation)
3993 (fun v -> setpresentationmode v);
3995 src#bool "ignore case in searches"
3996 (fun () -> conf.icase)
3997 (fun v -> conf.icase <- v);
3999 src#bool "preload"
4000 (fun () -> conf.preload)
4001 (fun v -> conf.preload <- v);
4003 src#bool "highlight links"
4004 (fun () -> conf.hlinks)
4005 (fun v -> conf.hlinks <- v);
4007 src#bool "under info"
4008 (fun () -> conf.underinfo)
4009 (fun v -> conf.underinfo <- v);
4011 src#bool "persistent bookmarks"
4012 (fun () -> conf.savebmarks)
4013 (fun v -> conf.savebmarks <- v);
4015 src#fitmodel "fit model"
4016 (fun () -> FMTE.to_string conf.fitmodel)
4017 (fun v -> reqlayout conf.angle (FMTE.of_int v));
4019 src#bool "trim margins"
4020 (fun () -> conf.trimmargins)
4021 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4023 src#bool "persistent location"
4024 (fun () -> conf.jumpback)
4025 (fun v -> conf.jumpback <- v);
4027 sep ();
4028 src#int "inter-page space"
4029 (fun () -> conf.interpagespace)
4030 (fun n ->
4031 conf.interpagespace <- n;
4032 docolumns conf.columns;
4033 let pageno, py =
4034 match state.layout with
4035 | [] -> 0, 0
4036 | l :: _ ->
4037 l.pageno, l.pagey
4039 state.maxy <- calcheight ();
4040 let y = getpagey pageno in
4041 gotoy (y + py)
4044 src#int "page bias"
4045 (fun () -> conf.pagebias)
4046 (fun v -> conf.pagebias <- v);
4048 src#int "scroll step"
4049 (fun () -> conf.scrollstep)
4050 (fun n -> conf.scrollstep <- n);
4052 src#int "horizontal scroll step"
4053 (fun () -> conf.hscrollstep)
4054 (fun v -> conf.hscrollstep <- v);
4056 src#int "auto scroll step"
4057 (fun () ->
4058 match state.autoscroll with
4059 | Some step -> step
4060 | _ -> conf.autoscrollstep)
4061 (fun n ->
4062 let n = boundastep state.winh n in
4063 if state.autoscroll <> None
4064 then state.autoscroll <- Some n;
4065 conf.autoscrollstep <- n);
4067 src#int "zoom"
4068 (fun () -> truncate (conf.zoom *. 100.))
4069 (fun v -> setzoom ((float v) /. 100.));
4071 src#int "rotation"
4072 (fun () -> conf.angle)
4073 (fun v -> reqlayout v conf.fitmodel);
4075 src#int "scroll bar width"
4076 (fun () -> conf.scrollbw)
4077 (fun v ->
4078 conf.scrollbw <- v;
4079 reshape state.winw state.winh;
4082 src#int "scroll handle height"
4083 (fun () -> conf.scrollh)
4084 (fun v -> conf.scrollh <- v;);
4086 src#int "thumbnail width"
4087 (fun () -> conf.thumbw)
4088 (fun v ->
4089 conf.thumbw <- min 4096 v;
4090 match oldmode with
4091 | Birdseye beye ->
4092 leavebirdseye beye false;
4093 enterbirdseye ()
4094 | _ -> ()
4097 let mode = state.mode in
4098 src#string "columns"
4099 (fun () ->
4100 match conf.columns with
4101 | Csingle _ -> "1"
4102 | Cmulti (multi, _) -> multicolumns_to_string multi
4103 | Csplit (count, _) -> "-" ^ string_of_int count
4105 (fun v ->
4106 let n, a, b = multicolumns_of_string v in
4107 setcolumns mode n a b);
4109 sep ();
4110 src#caption "Pixmap cache" 0;
4111 src#int_with_suffix "size (advisory)"
4112 (fun () -> conf.memlimit)
4113 (fun v -> conf.memlimit <- v);
4115 src#caption2 "used"
4116 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4117 (string_with_suffix_of_int state.memused)
4118 (Hashtbl.length state.tilemap)) 1;
4120 sep ();
4121 src#caption "Layout" 0;
4122 src#caption2 "Dimension"
4123 (fun () ->
4124 Printf.sprintf "%dx%d (virtual %dx%d)"
4125 state.winw state.winh
4126 state.w state.maxy)
4128 if conf.debug
4129 then
4130 src#caption2 "Position" (fun () ->
4131 Printf.sprintf "%dx%d" state.x state.y
4133 else
4134 src#caption2 "Position" (fun () -> describe_location ()) 1
4137 sep ();
4138 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4139 "Save these parameters as global defaults at exit"
4140 (fun () -> conf.bedefault)
4141 (fun v -> conf.bedefault <- v)
4144 sep ();
4145 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4146 src#bool ~offset:0 ~btos "Extended parameters"
4147 (fun () -> !showextended)
4148 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4149 if !showextended
4150 then (
4151 src#bool "checkers"
4152 (fun () -> conf.checkers)
4153 (fun v -> conf.checkers <- v; setcheckers v);
4154 src#bool "update cursor"
4155 (fun () -> conf.updatecurs)
4156 (fun v -> conf.updatecurs <- v);
4157 src#bool "scroll-bar on the left"
4158 (fun () -> conf.leftscroll)
4159 (fun v -> conf.leftscroll <- v);
4160 src#bool "verbose"
4161 (fun () -> conf.verbose)
4162 (fun v -> conf.verbose <- v);
4163 src#bool "invert colors"
4164 (fun () -> conf.invert)
4165 (fun v -> conf.invert <- v);
4166 src#bool "max fit"
4167 (fun () -> conf.maxhfit)
4168 (fun v -> conf.maxhfit <- v);
4169 src#bool "redirect stderr"
4170 (fun () -> conf.redirectstderr)
4171 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4172 src#bool "pax mode"
4173 (fun () -> conf.pax != None)
4174 (fun v ->
4175 if v
4176 then conf.pax <- Some (ref (now (), 0, 0))
4177 else conf.pax <- None);
4178 src#string "uri launcher"
4179 (fun () -> conf.urilauncher)
4180 (fun v -> conf.urilauncher <- v);
4181 src#string "path launcher"
4182 (fun () -> conf.pathlauncher)
4183 (fun v -> conf.pathlauncher <- v);
4184 src#string "tile size"
4185 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4186 (fun v ->
4188 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4189 conf.tilew <- max 64 w;
4190 conf.tileh <- max 64 h;
4191 flushtiles ();
4192 with exn ->
4193 state.text <- Printf.sprintf "bad tile size `%s': %s"
4194 v (exntos exn)
4196 src#int "texture count"
4197 (fun () -> conf.texcount)
4198 (fun v ->
4199 if realloctexts v
4200 then conf.texcount <- v
4201 else showtext '!' " Failed to set texture count please retry later"
4203 src#int "slice height"
4204 (fun () -> conf.sliceheight)
4205 (fun v ->
4206 conf.sliceheight <- v;
4207 wcmd "sliceh %d" conf.sliceheight;
4209 src#int "anti-aliasing level"
4210 (fun () -> conf.aalevel)
4211 (fun v ->
4212 conf.aalevel <- bound v 0 8;
4213 state.anchor <- getanchor ();
4214 opendoc state.path state.password;
4216 src#string "page scroll scaling factor"
4217 (fun () -> string_of_float conf.pgscale)
4218 (fun v ->
4220 let s = float_of_string v in
4221 conf.pgscale <- s
4222 with exn ->
4223 state.text <- Printf.sprintf
4224 "bad page scroll scaling factor `%s': %s" v (exntos exn)
4227 src#int "ui font size"
4228 (fun () -> fstate.fontsize)
4229 (fun v -> setfontsize (bound v 5 100));
4230 src#int "hint font size"
4231 (fun () -> conf.hfsize)
4232 (fun v -> conf.hfsize <- bound v 5 100);
4233 colorp "background color"
4234 (fun () -> conf.bgcolor)
4235 (fun v -> conf.bgcolor <- v);
4236 src#bool "crop hack"
4237 (fun () -> conf.crophack)
4238 (fun v -> conf.crophack <- v);
4239 src#string "trim fuzz"
4240 (fun () -> irect_to_string conf.trimfuzz)
4241 (fun v ->
4243 conf.trimfuzz <- irect_of_string v;
4244 if conf.trimmargins
4245 then settrim true conf.trimfuzz;
4246 with exn ->
4247 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
4249 src#string "throttle"
4250 (fun () ->
4251 match conf.maxwait with
4252 | None -> "show place holder if page is not ready"
4253 | Some time ->
4254 if time = infinity
4255 then "wait for page to fully render"
4256 else
4257 "wait " ^ string_of_float time
4258 ^ " seconds before showing placeholder"
4260 (fun v ->
4262 let f = float_of_string v in
4263 if f <= 0.0
4264 then conf.maxwait <- None
4265 else conf.maxwait <- Some f
4266 with exn ->
4267 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
4269 src#string "ghyll scroll"
4270 (fun () ->
4271 match conf.ghyllscroll with
4272 | None -> E.s
4273 | Some nab -> ghyllscroll_to_string nab
4275 (fun v ->
4276 try conf.ghyllscroll <- ghyllscroll_of_string v
4277 with exn ->
4278 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
4280 src#string "selection command"
4281 (fun () -> conf.selcmd)
4282 (fun v -> conf.selcmd <- v);
4283 src#string "synctex command"
4284 (fun () -> conf.stcmd)
4285 (fun v -> conf.stcmd <- v);
4286 src#string "pax command"
4287 (fun () -> conf.paxcmd)
4288 (fun v -> conf.paxcmd <- v);
4289 src#colorspace "color space"
4290 (fun () -> CSTE.to_string conf.colorspace)
4291 (fun v ->
4292 conf.colorspace <- CSTE.of_int v;
4293 wcmd "cs %d" v;
4294 load state.layout;
4296 src#paxmark "pax mark method"
4297 (fun () -> MTE.to_string conf.paxmark)
4298 (fun v -> conf.paxmark <- MTE.of_int v);
4299 if pbousable ()
4300 then
4301 src#bool "use PBO"
4302 (fun () -> conf.usepbo)
4303 (fun v -> conf.usepbo <- v);
4304 src#bool "mouse wheel scrolls pages"
4305 (fun () -> conf.wheelbypage)
4306 (fun v -> conf.wheelbypage <- v);
4307 src#bool "open remote links in a new instance"
4308 (fun () -> conf.riani)
4309 (fun v -> conf.riani <- v);
4312 sep ();
4313 src#caption "Document" 0;
4314 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4315 src#caption2 "Pages"
4316 (fun () -> string_of_int state.pagecount) 1;
4317 src#caption2 "Dimensions"
4318 (fun () -> string_of_int (List.length state.pdims)) 1;
4319 if conf.trimmargins
4320 then (
4321 sep ();
4322 src#caption "Trimmed margins" 0;
4323 src#caption2 "Dimensions"
4324 (fun () -> string_of_int (List.length state.pdims)) 1;
4327 sep ();
4328 src#caption "OpenGL" 0;
4329 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4330 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4332 sep ();
4333 src#caption "Location" 0;
4334 if nonemptystr state.origin
4335 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
4336 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
4338 src#reset prevmode prevuioh;
4340 fun () ->
4341 state.text <- E.s;
4342 resetmstate ();
4343 let prevmode = state.mode
4344 and prevuioh = state.uioh in
4345 fillsrc prevmode prevuioh;
4346 let source = (src :> lvsource) in
4347 let modehash = findkeyhash conf "info" in
4348 state.uioh <- coe (object (self)
4349 inherit listview ~zebra:false ~helpmode:false
4350 ~source ~trusted:true ~modehash as super
4351 val mutable m_prevmemused = 0
4352 method infochanged = function
4353 | Memused ->
4354 if m_prevmemused != state.memused
4355 then (
4356 m_prevmemused <- state.memused;
4357 G.postRedisplay "memusedchanged";
4359 | Pdim -> G.postRedisplay "pdimchanged"
4360 | Docinfo -> fillsrc prevmode prevuioh
4362 method key key mask =
4363 if not (Wsi.withctrl mask)
4364 then
4365 match key with
4366 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
4367 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
4368 | _ -> super#key key mask
4369 else super#key key mask
4370 end);
4371 G.postRedisplay "info";
4374 let enterhelpmode =
4375 let source =
4376 (object
4377 inherit lvsourcebase
4378 method getitemcount = Array.length state.help
4379 method getitem n =
4380 let s, l, _ = state.help.(n) in
4381 (s, l)
4383 method exit ~uioh ~cancel ~active ~first ~pan =
4384 let optuioh =
4385 if not cancel
4386 then (
4387 match state.help.(active) with
4388 | _, _, Action f -> Some (f uioh)
4389 | _ -> Some (uioh)
4391 else None
4393 m_active <- active;
4394 m_first <- first;
4395 m_pan <- pan;
4396 optuioh
4398 method hasaction n =
4399 match state.help.(n) with
4400 | _, _, Action _ -> true
4401 | _ -> false
4403 initializer
4404 m_active <- -1
4405 end)
4406 in fun () ->
4407 let modehash = findkeyhash conf "help" in
4408 resetmstate ();
4409 state.uioh <- coe (new listview
4410 ~zebra:false ~helpmode:true
4411 ~source ~trusted:true ~modehash);
4412 G.postRedisplay "help";
4415 let entermsgsmode =
4416 let msgsource =
4417 let re = Str.regexp "[\r\n]" in
4418 (object
4419 inherit lvsourcebase
4420 val mutable m_items = E.a
4422 method getitemcount = 1 + Array.length m_items
4424 method getitem n =
4425 if n = 0
4426 then "[Clear]", 0
4427 else m_items.(n-1), 0
4429 method exit ~uioh ~cancel ~active ~first ~pan =
4430 ignore uioh;
4431 if not cancel
4432 then (
4433 if active = 0
4434 then Buffer.clear state.errmsgs;
4436 m_active <- active;
4437 m_first <- first;
4438 m_pan <- pan;
4439 None
4441 method hasaction n =
4442 n = 0
4444 method reset =
4445 state.newerrmsgs <- false;
4446 let l = Str.split re (Buffer.contents state.errmsgs) in
4447 m_items <- Array.of_list l
4449 initializer
4450 m_active <- 0
4451 end)
4452 in fun () ->
4453 state.text <- E.s;
4454 resetmstate ();
4455 msgsource#reset;
4456 let source = (msgsource :> lvsource) in
4457 let modehash = findkeyhash conf "listview" in
4458 state.uioh <- coe (object
4459 inherit listview ~zebra:false ~helpmode:false
4460 ~source ~trusted:false ~modehash as super
4461 method display =
4462 if state.newerrmsgs
4463 then msgsource#reset;
4464 super#display
4465 end);
4466 G.postRedisplay "msgs";
4469 let quickbookmark ?title () =
4470 match state.layout with
4471 | [] -> ()
4472 | l :: _ ->
4473 let title =
4474 match title with
4475 | None ->
4476 let tm = Unix.localtime (now ()) in
4477 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4478 (l.pageno+1)
4479 tm.Unix.tm_mday
4480 tm.Unix.tm_mon
4481 (tm.Unix.tm_year + 1900)
4482 tm.Unix.tm_hour
4483 tm.Unix.tm_min
4484 | Some title -> title
4486 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4489 let setautoscrollspeed step goingdown =
4490 let incr = max 1 ((abs step) / 2) in
4491 let incr = if goingdown then incr else -incr in
4492 let astep = boundastep state.winh (step + incr) in
4493 state.autoscroll <- Some astep;
4496 let canpan () =
4497 match conf.columns with
4498 | Csplit _ -> true
4499 | _ -> state.x != 0 || conf.zoom > 1.0
4502 let panbound x = bound x (-state.w) (wadjsb state.winw);;
4504 let existsinrow pageno (columns, coverA, coverB) p =
4505 let last = ((pageno - coverA) mod columns) + columns in
4506 let rec any = function
4507 | [] -> false
4508 | l :: rest ->
4509 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4510 then p l
4511 else (
4512 if not (p l)
4513 then (if l.pageno = last then false else any rest)
4514 else true
4517 any state.layout
4520 let nextpage () =
4521 match state.layout with
4522 | [] ->
4523 let pageno = page_of_y state.y in
4524 gotoghyll (getpagey (pageno+1))
4525 | l :: rest ->
4526 match conf.columns with
4527 | Csingle _ ->
4528 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4529 then
4530 let y = clamp (pgscale state.winh) in
4531 gotoghyll y
4532 else
4533 let pageno = min (l.pageno+1) (state.pagecount-1) in
4534 gotoghyll (getpagey pageno)
4535 | Cmulti ((c, _, _) as cl, _) ->
4536 if conf.presentation
4537 && (existsinrow l.pageno cl
4538 (fun l -> l.pageh > l.pagey + l.pagevh))
4539 then
4540 let y = clamp (pgscale state.winh) in
4541 gotoghyll y
4542 else
4543 let pageno = min (l.pageno+c) (state.pagecount-1) in
4544 gotoghyll (getpagey pageno)
4545 | Csplit (n, _) ->
4546 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4547 then
4548 let pagey, pageh = getpageyh l.pageno in
4549 let pagey = pagey + pageh * l.pagecol in
4550 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4551 gotoghyll (pagey + pageh + ips)
4554 let prevpage () =
4555 match state.layout with
4556 | [] ->
4557 let pageno = page_of_y state.y in
4558 gotoghyll (getpagey (pageno-1))
4559 | l :: _ ->
4560 match conf.columns with
4561 | Csingle _ ->
4562 if conf.presentation && l.pagey != 0
4563 then
4564 gotoghyll (clamp (pgscale ~-(state.winh)))
4565 else
4566 let pageno = max 0 (l.pageno-1) in
4567 gotoghyll (getpagey pageno)
4568 | Cmulti ((c, _, coverB) as cl, _) ->
4569 if conf.presentation &&
4570 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4571 then
4572 gotoghyll (clamp (pgscale ~-(state.winh)))
4573 else
4574 let decr =
4575 if l.pageno = state.pagecount - coverB
4576 then 1
4577 else c
4579 let pageno = max 0 (l.pageno-decr) in
4580 gotoghyll (getpagey pageno)
4581 | Csplit (n, _) ->
4582 let y =
4583 if l.pagecol = 0
4584 then
4585 if l.pageno = 0
4586 then l.pagey
4587 else
4588 let pageno = max 0 (l.pageno-1) in
4589 let pagey, pageh = getpageyh pageno in
4590 pagey + (n-1)*pageh
4591 else
4592 let pagey, pageh = getpageyh l.pageno in
4593 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4595 gotoghyll y
4598 let viewkeyboard key mask =
4599 let enttext te =
4600 let mode = state.mode in
4601 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4602 state.text <- E.s;
4603 enttext ();
4604 G.postRedisplay "view:enttext"
4606 let ctrl = Wsi.withctrl mask in
4607 let key =
4608 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
4610 match key with
4611 | 81 -> (* Q *)
4612 exit 0
4614 | 0xff63 -> (* insert *)
4615 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4616 then (
4617 state.mode <- LinkNav (Ltgendir 0);
4618 gotoy state.y;
4620 else showtext '!' "Keyboard link navigation does not work under rotation"
4622 | 0xff1b | 113 -> (* escape / q *)
4623 begin match state.mstate with
4624 | Mzoomrect _ ->
4625 resetmstate ();
4626 G.postRedisplay "kill zoom rect";
4627 | _ ->
4628 begin match state.mode with
4629 | LinkNav _ ->
4630 state.mode <- View;
4631 G.postRedisplay "esc leave linknav"
4632 | _ ->
4633 match state.ranchors with
4634 | [] -> raise Quit
4635 | (path, password, anchor, origin) :: rest ->
4636 state.ranchors <- rest;
4637 state.anchor <- anchor;
4638 state.origin <- origin;
4639 state.nameddest <- E.s;
4640 opendoc path password
4641 end;
4642 end;
4644 | 0xff08 -> (* backspace *)
4645 gotoghyll (getnav ~-1)
4647 | 111 -> (* o *)
4648 enteroutlinemode ()
4650 | 72 -> (* H *)
4651 enterhistmode ()
4653 | 117 -> (* u *)
4654 state.rects <- [];
4655 state.text <- E.s;
4656 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap;
4657 G.postRedisplay "dehighlight";
4659 | 47 | 63 -> (* / ? *)
4660 let ondone isforw s =
4661 cbput state.hists.pat s;
4662 state.searchpattern <- s;
4663 search s isforw
4665 let s = String.create 1 in
4666 s.[0] <- Char.chr key;
4667 enttext (s, E.s, Some (onhist state.hists.pat),
4668 textentry, ondone (key = 47), true)
4670 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
4671 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4672 setzoom (conf.zoom +. incr)
4674 | 43 | 0xffab -> (* + *)
4675 let ondone s =
4676 let n =
4677 try int_of_string s with exc ->
4678 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
4679 max_int
4681 if n != max_int
4682 then (
4683 conf.pagebias <- n;
4684 state.text <- "page bias is now " ^ string_of_int n;
4687 enttext ("page bias: ", E.s, None, intentry, ondone, true)
4689 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4690 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4691 setzoom (max 0.01 (conf.zoom -. decr))
4693 | 45 | 0xffad -> (* - *)
4694 let ondone msg = state.text <- msg in
4695 enttext (
4696 "option [acfhilpstvxACFPRSZTISM]: ", E.s, None,
4697 optentry state.mode, ondone, true
4700 | 48 when ctrl -> (* ctrl-0 *)
4701 if conf.zoom = 1.0
4702 then (
4703 state.x <- 0;
4704 gotoy state.y
4706 else setzoom 1.0
4708 | (49 | 50) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
4709 let cols =
4710 match conf.columns with
4711 | Csingle _ | Cmulti _ -> 1
4712 | Csplit (n, _) -> n
4714 let h = state.winh -
4715 conf.interpagespace lsl (if conf.presentation then 1 else 0)
4717 let zoom = zoomforh state.winw h (vscrollw ()) cols in
4718 if zoom > 0.0 && (key = 50 || zoom < 1.0)
4719 then setzoom zoom
4721 | 51 when ctrl -> (* ctrl-3 *)
4722 let fm =
4723 match conf.fitmodel with
4724 | FitWidth -> FitProportional
4725 | FitProportional -> FitPage
4726 | FitPage -> FitWidth
4728 state.text <- "fit model: " ^ FMTE.to_string fm;
4729 reqlayout conf.angle fm
4731 | 0xffc6 -> (* f9 *)
4732 togglebirdseye ()
4734 | 57 when ctrl -> (* ctrl-9 *)
4735 togglebirdseye ()
4737 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4738 when not ctrl -> (* 0..9 *)
4739 let ondone s =
4740 let n =
4741 try int_of_string s with exc ->
4742 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
4745 if n >= 0
4746 then (
4747 addnav ();
4748 cbput state.hists.pag (string_of_int n);
4749 gotopage1 (n + conf.pagebias - 1) 0;
4752 let pageentry text key =
4753 match Char.unsafe_chr key with
4754 | 'g' -> TEdone text
4755 | _ -> intentry text key
4757 let text = "x" in text.[0] <- Char.chr key;
4758 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
4760 | 98 -> (* b *)
4761 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
4762 reshape state.winw state.winh;
4764 | 66 -> (* B *)
4765 state.bzoom <- not state.bzoom;
4766 state.rects <- [];
4767 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
4769 | 108 -> (* l *)
4770 conf.hlinks <- not conf.hlinks;
4771 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4772 G.postRedisplay "toggle highlightlinks";
4774 | 70 -> (* F *)
4775 state.glinks <- true;
4776 let mode = state.mode in
4777 state.mode <- Textentry (
4778 (":", E.s, None, linknentry, linkndone gotounder, false),
4779 (fun _ ->
4780 state.glinks <- false;
4781 state.mode <- mode)
4783 state.text <- E.s;
4784 G.postRedisplay "view:linkent(F)"
4786 | 121 -> (* y *)
4787 state.glinks <- true;
4788 let mode = state.mode in
4789 state.mode <- Textentry (
4791 ":", E.s, None, linknentry, linkndone (fun under ->
4792 selstring (undertext under);
4793 ), false
4795 fun _ ->
4796 state.glinks <- false;
4797 state.mode <- mode
4799 state.text <- E.s;
4800 G.postRedisplay "view:linkent"
4802 | 97 -> (* a *)
4803 begin match state.autoscroll with
4804 | Some step ->
4805 conf.autoscrollstep <- step;
4806 state.autoscroll <- None
4807 | None ->
4808 if conf.autoscrollstep = 0
4809 then state.autoscroll <- Some 1
4810 else state.autoscroll <- Some conf.autoscrollstep
4813 | 112 when ctrl -> (* ctrl-p *)
4814 launchpath ()
4816 | 80 -> (* P *)
4817 setpresentationmode (not conf.presentation);
4818 showtext ' ' ("presentation mode " ^
4819 if conf.presentation then "on" else "off");
4821 | 102 -> (* f *)
4822 if List.mem Wsi.Fullscreen state.winstate
4823 then Wsi.reshape conf.cwinw conf.cwinh
4824 else Wsi.fullscreen ()
4826 | 112 | 78 -> (* p|N *)
4827 search state.searchpattern false
4829 | 110 | 0xffc0 -> (* n|F3 *)
4830 search state.searchpattern true
4832 | 116 -> (* t *)
4833 begin match state.layout with
4834 | [] -> ()
4835 | l :: _ ->
4836 gotoghyll (getpagey l.pageno)
4839 | 32 -> (* space *)
4840 nextpage ()
4842 | 0xff9f | 0xffff -> (* delete *)
4843 prevpage ()
4845 | 61 -> (* = *)
4846 showtext ' ' (describe_location ());
4848 | 119 -> (* w *)
4849 begin match state.layout with
4850 | [] -> ()
4851 | l :: _ ->
4852 Wsi.reshape (l.pagew + vscrollw ()) l.pageh;
4853 G.postRedisplay "w"
4856 | 39 -> (* ' *)
4857 enterbookmarkmode ()
4859 | 104 | 0xffbe -> (* h|F1 *)
4860 enterhelpmode ()
4862 | 105 -> (* i *)
4863 enterinfomode ()
4865 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
4866 entermsgsmode ()
4868 | 109 -> (* m *)
4869 let ondone s =
4870 match state.layout with
4871 | l :: _ ->
4872 if nonemptystr s
4873 then
4874 state.bookmarks <-
4875 (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4876 | _ -> ()
4878 enttext ("bookmark: ", E.s, None, textentry, ondone, true)
4880 | 126 -> (* ~ *)
4881 quickbookmark ();
4882 showtext ' ' "Quick bookmark added";
4884 | 122 -> (* z *)
4885 begin match state.layout with
4886 | l :: _ ->
4887 let rect = getpdimrect l.pagedimno in
4888 let w, h =
4889 if conf.crophack
4890 then
4891 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4892 truncate (1.2 *. (rect.(3) -. rect.(0))))
4893 else
4894 (truncate (rect.(1) -. rect.(0)),
4895 truncate (rect.(3) -. rect.(0)))
4897 let w = truncate ((float w)*.conf.zoom)
4898 and h = truncate ((float h)*.conf.zoom) in
4899 if w != 0 && h != 0
4900 then (
4901 state.anchor <- getanchor ();
4902 Wsi.reshape (w + vscrollw ()) (h + conf.interpagespace)
4904 G.postRedisplay "z";
4906 | [] -> ()
4909 | 120 -> (* x *)
4910 state.roam ()
4911 | 60 | 62 -> (* < > *)
4912 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.fitmodel
4914 | 91 | 93 -> (* [ ] *)
4915 conf.colorscale <-
4916 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
4918 G.postRedisplay "brightness";
4920 | 99 when state.mode = View -> (* [alt-]c *)
4921 if Wsi.withalt mask
4922 then (
4923 if conf.zoom > 1.0
4924 then
4925 let m = (wadjsb state.winw - state.w) / 2 in
4926 state.x <- m;
4927 gotoy_and_clear_text state.y
4929 else
4930 let (c, a, b), z =
4931 match state.prevcolumns with
4932 | None -> (1, 0, 0), 1.0
4933 | Some (columns, z) ->
4934 let cab =
4935 match columns with
4936 | Csplit (c, _) -> -c, 0, 0
4937 | Cmulti ((c, a, b), _) -> c, a, b
4938 | Csingle _ -> 1, 0, 0
4940 cab, z
4942 setcolumns View c a b;
4943 setzoom z
4945 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask
4946 -> (* ctrl-shift- (kp) [up|down] *)
4947 let zoom, x = state.prevzoom in
4948 setzoom zoom;
4949 state.x <- x;
4951 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
4952 begin match state.autoscroll with
4953 | None ->
4954 begin match state.mode with
4955 | Birdseye beye -> upbirdseye 1 beye
4956 | _ ->
4957 if ctrl
4958 then gotoy_and_clear_text (clamp ~-(state.winh/2))
4959 else (
4960 if not (Wsi.withshift mask) && conf.presentation
4961 then prevpage ()
4962 else gotoghyll1 true (clamp (-conf.scrollstep))
4965 | Some n ->
4966 setautoscrollspeed n false
4969 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
4970 begin match state.autoscroll with
4971 | None ->
4972 begin match state.mode with
4973 | Birdseye beye -> downbirdseye 1 beye
4974 | _ ->
4975 if ctrl
4976 then gotoy_and_clear_text (clamp (state.winh/2))
4977 else (
4978 if not (Wsi.withshift mask) && conf.presentation
4979 then nextpage ()
4980 else gotoghyll1 true (clamp (conf.scrollstep))
4983 | Some n ->
4984 setautoscrollspeed n true
4987 | 0xff51 | 0xff53 | 0xff96 | 0xff98
4988 when not (Wsi.withalt mask) -> (* (kp) left / right *)
4989 if canpan ()
4990 then
4991 let dx =
4992 if ctrl
4993 then state.winw / 2
4994 else conf.hscrollstep
4996 let dx = if key = 0xff51 || key = 0xff96 then dx else -dx in
4997 state.x <- panbound (state.x + dx);
4998 gotoy_and_clear_text state.y
4999 else (
5000 state.text <- E.s;
5001 G.postRedisplay "left/right"
5004 | 0xff55 | 0xff9a -> (* (kp) prior *)
5005 let y =
5006 if ctrl
5007 then
5008 match state.layout with
5009 | [] -> state.y
5010 | l :: _ -> state.y - l.pagey
5011 else
5012 clamp (pgscale (-state.winh))
5014 gotoghyll y
5016 | 0xff56 | 0xff9b -> (* (kp) next *)
5017 let y =
5018 if ctrl
5019 then
5020 match List.rev state.layout with
5021 | [] -> state.y
5022 | l :: _ -> getpagey l.pageno
5023 else
5024 clamp (pgscale state.winh)
5026 gotoghyll y
5028 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5029 gotoghyll 0
5030 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
5031 gotoghyll (clamp state.maxy)
5033 | 0xff53 | 0xff98
5034 when Wsi.withalt mask -> (* alt-(kp) right *)
5035 gotoghyll (getnav 1)
5036 | 0xff51 | 0xff96
5037 when Wsi.withalt mask -> (* alt-(kp) left *)
5038 gotoghyll (getnav ~-1)
5040 | 114 -> (* r *)
5041 reload ()
5043 | 118 when conf.debug -> (* v *)
5044 state.rects <- [];
5045 List.iter (fun l ->
5046 match getopaque l.pageno with
5047 | None -> ()
5048 | Some opaque ->
5049 let x0, y0, x1, y1 = pagebbox opaque in
5050 let a,b = float x0, float y0 in
5051 let c,d = float x1, float y0 in
5052 let e,f = float x1, float y1 in
5053 let h,j = float x0, float y1 in
5054 let rect = (a,b,c,d,e,f,h,j) in
5055 debugrect rect;
5056 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5057 ) state.layout;
5058 G.postRedisplay "v";
5060 | 124 -> (* | *)
5061 let mode = state.mode in
5062 let cmd = ref E.s in
5063 let onleave = function
5064 | Cancel -> state.mode <- mode
5065 | Confirm ->
5066 List.iter (fun l ->
5067 match getopaque l.pageno with
5068 | Some opaque -> pipesel opaque !cmd
5069 | None -> ()) state.layout;
5070 state.mode <- mode
5072 let ondone s =
5073 cbput state.hists.sel s;
5074 cmd := s
5076 let te =
5077 "| ", !cmd, Some (onhist state.hists.sel), textentry, ondone, true
5079 G.postRedisplay "|";
5080 state.mode <- Textentry (te, onleave);
5082 | _ ->
5083 vlog "huh? %s" (Wsi.keyname key)
5086 let linknavkeyboard key mask linknav =
5087 let getpage pageno =
5088 let rec loop = function
5089 | [] -> None
5090 | l :: _ when l.pageno = pageno -> Some l
5091 | _ :: rest -> loop rest
5092 in loop state.layout
5094 let doexact (pageno, n) =
5095 match getopaque pageno, getpage pageno with
5096 | Some opaque, Some l ->
5097 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5098 then
5099 let under = getlink opaque n in
5100 G.postRedisplay "link gotounder";
5101 gotounder under;
5102 state.mode <- View;
5103 else
5104 let opt, dir =
5105 match key with
5106 | 0xff50 -> (* home *)
5107 Some (findlink opaque LDfirst), -1
5109 | 0xff57 -> (* end *)
5110 Some (findlink opaque LDlast), 1
5112 | 0xff51 -> (* left *)
5113 Some (findlink opaque (LDleft n)), -1
5115 | 0xff53 -> (* right *)
5116 Some (findlink opaque (LDright n)), 1
5118 | 0xff52 -> (* up *)
5119 Some (findlink opaque (LDup n)), -1
5121 | 0xff54 -> (* down *)
5122 Some (findlink opaque (LDdown n)), 1
5124 | _ -> None, 0
5126 let pwl l dir =
5127 begin match findpwl l.pageno dir with
5128 | Pwlnotfound -> ()
5129 | Pwl pageno ->
5130 let notfound dir =
5131 state.mode <- LinkNav (Ltgendir dir);
5132 let y, h = getpageyh pageno in
5133 let y =
5134 if dir < 0
5135 then y + h - state.winh
5136 else y
5138 gotoy y
5140 begin match getopaque pageno, getpage pageno with
5141 | Some opaque, Some _ ->
5142 let link =
5143 let ld = if dir > 0 then LDfirst else LDlast in
5144 findlink opaque ld
5146 begin match link with
5147 | Lfound m ->
5148 showlinktype (getlink opaque m);
5149 state.mode <- LinkNav (Ltexact (pageno, m));
5150 G.postRedisplay "linknav jpage";
5151 | _ -> notfound dir
5152 end;
5153 | _ -> notfound dir
5154 end;
5155 end;
5157 begin match opt with
5158 | Some Lnotfound -> pwl l dir;
5159 | Some (Lfound m) ->
5160 if m = n
5161 then pwl l dir
5162 else (
5163 let _, y0, _, y1 = getlinkrect opaque m in
5164 if y0 < l.pagey
5165 then gotopage1 l.pageno y0
5166 else (
5167 let d = fstate.fontsize + 1 in
5168 if y1 - l.pagey > l.pagevh - d
5169 then gotopage1 l.pageno (y1 - state.winh - hscrollh () + d)
5170 else G.postRedisplay "linknav";
5172 showlinktype (getlink opaque m);
5173 state.mode <- LinkNav (Ltexact (l.pageno, m));
5176 | None -> viewkeyboard key mask
5177 end;
5178 | _ -> viewkeyboard key mask
5180 if key = 0xff63
5181 then (
5182 state.mode <- View;
5183 G.postRedisplay "leave linknav"
5185 else
5186 match linknav with
5187 | Ltgendir _ -> viewkeyboard key mask
5188 | Ltexact exact -> doexact exact
5191 let keyboard key mask =
5192 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5193 then wcmd "interrupt"
5194 else state.uioh <- state.uioh#key key mask
5197 let birdseyekeyboard key mask
5198 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5199 let incr =
5200 match conf.columns with
5201 | Csingle _ -> 1
5202 | Cmulti ((c, _, _), _) -> c
5203 | Csplit _ -> failwith "bird's eye split mode"
5205 let pgh layout = List.fold_left (fun m l -> max l.pageh m) state.winh layout in
5206 match key with
5207 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5208 let y, h = getpageyh pageno in
5209 let top = (state.winh - h) / 2 in
5210 gotoy (max 0 (y - top))
5211 | 0xff0d (* enter *)
5212 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5213 | 0xff1b -> leavebirdseye beye true (* escape *)
5214 | 0xff52 -> upbirdseye incr beye (* up *)
5215 | 0xff54 -> downbirdseye incr beye (* down *)
5216 | 0xff51 -> upbirdseye 1 beye (* left *)
5217 | 0xff53 -> downbirdseye 1 beye (* right *)
5219 | 0xff55 -> (* prior *)
5220 begin match state.layout with
5221 | l :: _ ->
5222 if l.pagey != 0
5223 then (
5224 state.mode <- Birdseye (
5225 oconf, leftx, l.pageno, hooverpageno, anchor
5227 gotopage1 l.pageno 0;
5229 else (
5230 let layout = layout (state.y-state.winh) (pgh state.layout) in
5231 match layout with
5232 | [] -> gotoy (clamp (-state.winh))
5233 | l :: _ ->
5234 state.mode <- Birdseye (
5235 oconf, leftx, l.pageno, hooverpageno, anchor
5237 gotopage1 l.pageno 0
5240 | [] -> gotoy (clamp (-state.winh))
5241 end;
5243 | 0xff56 -> (* next *)
5244 begin match List.rev state.layout with
5245 | l :: _ ->
5246 let layout = layout (state.y + (pgh state.layout)) state.winh in
5247 begin match layout with
5248 | [] ->
5249 let incr = l.pageh - l.pagevh in
5250 if incr = 0
5251 then (
5252 state.mode <-
5253 Birdseye (
5254 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5256 G.postRedisplay "birdseye pagedown";
5258 else gotoy (clamp (incr + conf.interpagespace*2));
5260 | l :: _ ->
5261 state.mode <-
5262 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5263 gotopage1 l.pageno 0;
5266 | [] -> gotoy (clamp state.winh)
5267 end;
5269 | 0xff50 -> (* home *)
5270 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5271 gotopage1 0 0
5273 | 0xff57 -> (* end *)
5274 let pageno = state.pagecount - 1 in
5275 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5276 if not (pagevisible state.layout pageno)
5277 then
5278 let h =
5279 match List.rev state.pdims with
5280 | [] -> state.winh
5281 | (_, _, h, _) :: _ -> h
5283 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5284 else G.postRedisplay "birdseye end";
5285 | _ -> viewkeyboard key mask
5288 let drawpage l =
5289 let color =
5290 match state.mode with
5291 | Textentry _ -> scalecolor 0.4
5292 | LinkNav _
5293 | View -> scalecolor 1.0
5294 | Birdseye (_, _, pageno, hooverpageno, _) ->
5295 if l.pageno = hooverpageno
5296 then scalecolor 0.9
5297 else (
5298 if l.pageno = pageno
5299 then scalecolor 1.0
5300 else scalecolor 0.8
5303 drawtiles l color;
5306 let postdrawpage l linkindexbase =
5307 match getopaque l.pageno with
5308 | Some opaque ->
5309 if tileready l l.pagex l.pagey
5310 then
5311 let x = l.pagedispx - l.pagex + xadjsb 0
5312 and y = l.pagedispy - l.pagey in
5313 let hlmask =
5314 match conf.columns with
5315 | Csingle _ | Cmulti _ ->
5316 (if conf.hlinks then 1 else 0)
5317 + (if state.glinks
5318 && not (isbirdseye state.mode) then 2 else 0)
5319 | _ -> 0
5321 let s =
5322 match state.mode with
5323 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5324 | _ -> E.s
5326 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5327 else 0
5328 | _ -> 0
5331 let scrollindicator () =
5332 let sbw, ph, sh = state.uioh#scrollph in
5333 let sbh, pw, sw = state.uioh#scrollpw in
5335 let x0,x1 =
5336 if conf.leftscroll
5337 then (0, sbw)
5338 else (state.winw - sbw), state.winw
5341 GlDraw.color (0.64, 0.64, 0.64);
5342 filledrect (float x0) 0. (float x1) (float state.winh);
5343 filledrect
5344 0. (float (state.winh - sbh))
5345 (float (wadjsb state.winw - 1)) (float state.winh)
5347 GlDraw.color (0.0, 0.0, 0.0);
5349 filledrect (float x0) ph (float x1) (ph +. sh);
5350 filledrect pw (float (state.winh - sbh)) (pw +. sw) (float state.winh);
5353 let showsel () =
5354 match state.mstate with
5355 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5358 | Msel ((x0, y0), (x1, y1)) ->
5359 let identify opaque l px py = Some (opaque, l.pageno, px, py) in
5360 let o0,n0,px0,py0 = onppundermouse identify x0 y0 (~< E.s, -1, 0, 0) in
5361 let _o1,n1,px1,py1 = onppundermouse identify x1 y1 (~< E.s, -1, 0, 0) in
5362 if n0 != -1 && n0 = n1 then seltext o0 (px0, py0, px1, py1);
5365 let showrects = function [] -> () | rects ->
5366 Gl.enable `blend;
5367 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5368 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5369 List.iter
5370 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5371 List.iter (fun l ->
5372 if l.pageno = pageno
5373 then (
5374 let dx = float (l.pagedispx - l.pagex) in
5375 let dy = float (l.pagedispy - l.pagey) in
5376 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5377 Raw.sets_float state.vraw ~pos:0
5378 [| x0+.dx; y0+.dy;
5379 x1+.dx; y1+.dy;
5380 x3+.dx; y3+.dy;
5381 x2+.dx; y2+.dy |];
5382 GlArray.vertex `two state.vraw;
5383 GlArray.draw_arrays `triangle_strip 0 4;
5385 ) state.layout
5386 ) rects
5388 Gl.disable `blend;
5391 let display () =
5392 GlClear.color (scalecolor2 conf.bgcolor);
5393 GlClear.clear [`color];
5394 List.iter drawpage state.layout;
5395 let rects =
5396 match state.mode with
5397 | LinkNav (Ltexact (pageno, linkno)) ->
5398 begin match getopaque pageno with
5399 | Some opaque ->
5400 let dx = xadjsb 0 in
5401 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5402 let x0 = x0 + dx and x1 = x1 + dx in
5403 (pageno, 5, (
5404 float x0, float y0,
5405 float x1, float y0,
5406 float x1, float y1,
5407 float x0, float y1)
5408 ) :: state.rects
5409 | None -> state.rects
5411 | _ -> state.rects
5413 showrects rects;
5414 let rec postloop linkindexbase = function
5415 | l :: rest ->
5416 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5417 postloop linkindexbase rest
5418 | [] -> ()
5420 showsel ();
5421 postloop 0 state.layout;
5422 state.uioh#display;
5423 begin match state.mstate with
5424 | Mzoomrect ((x0, y0), (x1, y1)) ->
5425 Gl.enable `blend;
5426 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5427 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5428 filledrect (float x0) (float y0) (float x1) (float y1);
5429 Gl.disable `blend;
5430 | _ -> ()
5431 end;
5432 enttext ();
5433 scrollindicator ();
5434 Wsi.swapb ();
5437 let zoomrect x y x1 y1 =
5438 let x0 = min x x1
5439 and x1 = max x x1
5440 and y0 = min y y1 in
5441 gotoy (state.y + y0);
5442 state.anchor <- getanchor ();
5443 let zoom = (float state.w) /. float (x1 - x0) in
5444 let margin =
5445 match conf.fitmodel, conf.columns with
5446 | FitPage, Csplit _ ->
5447 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
5449 | _, _ ->
5450 let adjw = wadjsb state.winw in
5451 if state.w < adjw
5452 then (adjw - state.w) / 2
5453 else 0
5455 state.x <- (state.x + margin) - x0;
5456 setzoom zoom;
5457 resetmstate ();
5460 let zoomblock x y =
5461 let g opaque l px py =
5462 match rectofblock opaque px py with
5463 | Some a ->
5464 let x0 = a.(0) -. 20. in
5465 let x1 = a.(1) +. 20. in
5466 let y0 = a.(2) -. 20. in
5467 let zoom = (float state.w) /. (x1 -. x0) in
5468 let pagey = getpagey l.pageno in
5469 gotoy_and_clear_text (pagey + truncate y0);
5470 state.anchor <- getanchor ();
5471 let margin = (state.w - l.pagew)/2 in
5472 state.x <- -truncate x0 - margin;
5473 setzoom zoom;
5474 None
5475 | None -> None
5477 match conf.columns with
5478 | Csplit _ ->
5479 showtext '!' "block zooming does not work properly in split columns mode"
5480 | _ -> onppundermouse g x y ()
5483 let scrollx x =
5484 let winw = wadjsb state.winw - 1 in
5485 let s = float x /. float winw in
5486 let destx = truncate (float (state.w + winw) *. s) in
5487 state.x <- winw - destx;
5488 gotoy_and_clear_text state.y;
5489 state.mstate <- Mscrollx;
5492 let scrolly y =
5493 let s = float y /. float state.winh in
5494 let desty = truncate (float (state.maxy - state.winh) *. s) in
5495 gotoy_and_clear_text desty;
5496 state.mstate <- Mscrolly;
5499 let viewmulticlick clicks x y mask =
5500 let g opaque l px py =
5501 let mark =
5502 match clicks with
5503 | 2 -> Mark_word
5504 | 3 -> Mark_line
5505 | 4 -> Mark_block
5506 | _ -> Mark_page
5508 if markunder opaque px py mark
5509 then (
5510 Some (fun () ->
5511 let dopipe cmd =
5512 match getopaque l.pageno with
5513 | None -> ()
5514 | Some opaque -> pipesel opaque cmd
5516 state.roam <- (fun () -> dopipe conf.paxcmd);
5517 if not (Wsi.withctrl mask) then dopipe conf.selcmd;
5520 else None
5522 G.postRedisplay "viewmulticlick";
5523 onppundermouse g x y (fun () -> showtext '!' "Nothing to select") ();
5526 let canselect () =
5527 match conf.columns with
5528 | Csplit _ -> false
5529 | Csingle _ | Cmulti _ -> conf.angle mod 360 = 0
5532 let viewmouse button down x y mask =
5533 match button with
5534 | n when (n == 4 || n == 5) && not down ->
5535 if Wsi.withctrl mask
5536 then (
5537 match state.mstate with
5538 | Mzoom (oldn, i) ->
5539 if oldn = n
5540 then (
5541 if i = 2
5542 then
5543 let incr =
5544 match n with
5545 | 5 ->
5546 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5547 | _ ->
5548 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5550 let zoom = conf.zoom -. incr in
5551 setzoom zoom;
5552 state.mstate <- Mzoom (n, 0);
5553 else
5554 state.mstate <- Mzoom (n, i+1);
5556 else state.mstate <- Mzoom (n, 0)
5558 | _ -> state.mstate <- Mzoom (n, 0)
5560 else (
5561 match state.autoscroll with
5562 | Some step -> setautoscrollspeed step (n=4)
5563 | None ->
5564 if conf.wheelbypage || conf.presentation
5565 then (
5566 if n = 4
5567 then prevpage ()
5568 else nextpage ()
5570 else
5571 let incr =
5572 if n = 4
5573 then -conf.scrollstep
5574 else conf.scrollstep
5576 let incr = incr * 2 in
5577 let y = clamp incr in
5578 gotoy_and_clear_text y
5581 | n when (n = 6 || n = 7) && not down && canpan () ->
5582 state.x <-
5583 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
5584 gotoy_and_clear_text state.y
5586 | 1 when Wsi.withshift mask ->
5587 state.mstate <- Mnone;
5588 if not down
5589 then (
5590 match unproject x y with
5591 | Some (pageno, ux, uy) ->
5592 let cmd = Printf.sprintf
5593 "%s %s %d %d %d"
5594 conf.stcmd state.path pageno ux uy
5596 popen cmd []
5597 | None -> ()
5600 | 1 when Wsi.withctrl mask ->
5601 if down
5602 then (
5603 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5604 state.mstate <- Mpan (x, y)
5606 else
5607 state.mstate <- Mnone
5609 | 3 ->
5610 if down
5611 then (
5612 Wsi.setcursor Wsi.CURSOR_CYCLE;
5613 let p = (x, y) in
5614 state.mstate <- Mzoomrect (p, p)
5616 else (
5617 match state.mstate with
5618 | Mzoomrect ((x0, y0), _) ->
5619 if abs (x-x0) > 10 && abs (y - y0) > 10
5620 then zoomrect x0 y0 x y
5621 else (
5622 resetmstate ();
5623 G.postRedisplay "kill accidental zoom rect";
5625 | _ ->
5626 resetmstate ()
5629 | 1 when x > state.winw - vscrollw () ->
5630 if down
5631 then
5632 let _, position, sh = state.uioh#scrollph in
5633 if y > truncate position && y < truncate (position +. sh)
5634 then state.mstate <- Mscrolly
5635 else scrolly y
5636 else
5637 state.mstate <- Mnone
5639 | 1 when y > state.winh - hscrollh () ->
5640 if down
5641 then
5642 let _, position, sw = state.uioh#scrollpw in
5643 if x > truncate position && x < truncate (position +. sw)
5644 then state.mstate <- Mscrollx
5645 else scrollx x
5646 else
5647 state.mstate <- Mnone
5649 | 1 when state.bzoom -> if not down then zoomblock x y
5651 | 1 ->
5652 let dest = if down then getunder x y else Unone in
5653 begin match dest with
5654 | Ulinkgoto _
5655 | Ulinkuri _
5656 | Uremote _ | Uremotedest _
5657 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5658 gotounder dest
5660 | Unone when down ->
5661 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5662 state.mstate <- Mpan (x, y);
5664 | Unone | Utext _ ->
5665 if down
5666 then (
5667 if canselect ()
5668 then (
5669 state.mstate <- Msel ((x, y), (x, y));
5670 G.postRedisplay "mouse select";
5673 else (
5674 match state.mstate with
5675 | Mnone -> ()
5677 | Mzoom _ | Mscrollx | Mscrolly ->
5678 state.mstate <- Mnone
5680 | Mzoomrect ((x0, y0), _) ->
5681 zoomrect x0 y0 x y
5683 | Mpan _ ->
5684 Wsi.setcursor Wsi.CURSOR_INHERIT;
5685 state.mstate <- Mnone
5687 | Msel ((x0, y0), (x1, y1)) ->
5688 let rec loop = function
5689 | [] -> ()
5690 | l :: rest ->
5691 let inside =
5692 let a0 = l.pagedispy in
5693 let a1 = a0 + l.pagevh in
5694 let b0 = l.pagedispx in
5695 let b1 = b0 + l.pagevw in
5696 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5697 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5699 if inside
5700 then
5701 match getopaque l.pageno with
5702 | Some opaque ->
5703 let dosel cmd () =
5704 match Ne.res Unix.pipe with
5705 | Ne.Exn exn ->
5706 showtext '!'
5707 (Printf.sprintf
5708 "can not create sel pipe: %s"
5709 (exntos exn));
5710 | Ne.Res (r, w) ->
5711 let clo what fd =
5712 Ne.clo fd (fun msg ->
5713 dolog "%s close failed: %s" what msg)
5715 let popened =
5716 try popen cmd [r, 0; w, -1]; true
5717 with exn ->
5718 dolog "can not execute %S: %s"
5719 cmd (exntos exn);
5720 false
5722 if popened
5723 then (
5724 copysel w opaque;
5725 G.postRedisplay "copysel";
5727 else clo "Msel pipe/w" w;
5728 clo "Msel pipe/r" r;
5730 dosel conf.selcmd ();
5731 state.roam <- dosel conf.paxcmd;
5732 | None -> ()
5733 else loop rest
5735 loop state.layout;
5736 resetmstate ();
5740 | _ -> ()
5743 let birdseyemouse button down x y mask
5744 (conf, leftx, _, hooverpageno, anchor) =
5745 match button with
5746 | 1 when down ->
5747 let rec loop = function
5748 | [] -> ()
5749 | l :: rest ->
5750 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5751 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5752 then (
5753 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5755 else loop rest
5757 loop state.layout
5758 | 3 -> ()
5759 | _ -> viewmouse button down x y mask
5762 let uioh = object
5763 method display = ()
5765 method key key mask =
5766 begin match state.mode with
5767 | Textentry textentry -> textentrykeyboard key mask textentry
5768 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5769 | View -> viewkeyboard key mask
5770 | LinkNav linknav -> linknavkeyboard key mask linknav
5771 end;
5772 state.uioh
5774 method button button bstate x y mask =
5775 begin match state.mode with
5776 | LinkNav _
5777 | View -> viewmouse button bstate x y mask
5778 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5779 | Textentry _ -> ()
5780 end;
5781 state.uioh
5783 method multiclick clicks x y mask =
5784 begin match state.mode with
5785 | LinkNav _
5786 | View -> viewmulticlick clicks x y mask
5787 | Birdseye _
5788 | Textentry _ -> ()
5789 end;
5790 state.uioh
5792 method motion x y =
5793 begin match state.mode with
5794 | Textentry _ -> ()
5795 | View | Birdseye _ | LinkNav _ ->
5796 match state.mstate with
5797 | Mzoom _ | Mnone -> ()
5799 | Mpan (x0, y0) ->
5800 let dx = x - x0
5801 and dy = y0 - y in
5802 state.mstate <- Mpan (x, y);
5803 if canpan ()
5804 then state.x <- panbound (state.x + dx);
5805 let y = clamp dy in
5806 gotoy_and_clear_text y
5808 | Msel (a, _) ->
5809 state.mstate <- Msel (a, (x, y));
5810 G.postRedisplay "motion select";
5812 | Mscrolly ->
5813 let y = min state.winh (max 0 y) in
5814 scrolly y
5816 | Mscrollx ->
5817 let x = min state.winw (max 0 x) in
5818 scrollx x
5820 | Mzoomrect (p0, _) ->
5821 state.mstate <- Mzoomrect (p0, (x, y));
5822 G.postRedisplay "motion zoomrect";
5823 end;
5824 state.uioh
5826 method pmotion x y =
5827 begin match state.mode with
5828 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5829 let rec loop = function
5830 | [] ->
5831 if hooverpageno != -1
5832 then (
5833 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5834 G.postRedisplay "pmotion birdseye no hoover";
5836 | l :: rest ->
5837 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5838 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5839 then (
5840 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5841 G.postRedisplay "pmotion birdseye hoover";
5843 else loop rest
5845 loop state.layout
5847 | Textentry _ -> ()
5849 | LinkNav _
5850 | View ->
5851 match state.mstate with
5852 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5854 | Mnone ->
5855 updateunder x y;
5856 if canselect ()
5857 then
5858 match conf.pax with
5859 | None -> ()
5860 | Some r ->
5861 let past, _, _ = !r in
5862 let now = now () in
5863 let delta = now -. past in
5864 if delta > 0.01
5865 then paxunder x y
5866 else r := (now, x, y)
5867 end;
5868 state.uioh
5870 method infochanged _ = ()
5872 method scrollph =
5873 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
5874 let p, h =
5875 if maxy = 0
5876 then 0.0, float state.winh
5877 else scrollph state.y maxy
5879 vscrollw (), p, h
5881 method scrollpw =
5882 let winw = wadjsb state.winw in
5883 let fwinw = float winw in
5884 let sw =
5885 let sw = fwinw /. float state.w in
5886 let sw = fwinw *. sw in
5887 max sw (float conf.scrollh)
5889 let position =
5890 let maxx = state.w + winw in
5891 let x = winw - state.x in
5892 let percent = float x /. float maxx in
5893 (fwinw -. sw) *. percent
5895 hscrollh (), position, sw
5897 method modehash =
5898 let modename =
5899 match state.mode with
5900 | LinkNav _ -> "links"
5901 | Textentry _ -> "textentry"
5902 | Birdseye _ -> "birdseye"
5903 | View -> "view"
5905 findkeyhash conf modename
5907 method eformsgs = true
5908 end;;
5910 let adderrmsg src msg =
5911 Buffer.add_string state.errmsgs msg;
5912 state.newerrmsgs <- true;
5913 G.postRedisplay src
5916 let adderrfmt src fmt =
5917 Format.kprintf (fun s -> adderrmsg src s) fmt;
5920 let ract cmds =
5921 let cl = splitatspace cmds in
5922 let scan s fmt f =
5923 try Scanf.sscanf s fmt f
5924 with exn ->
5925 adderrfmt "remote exec"
5926 "error processing '%S': %s\n" cmds (exntos exn)
5928 match cl with
5929 | "reload" :: [] -> reload ()
5930 | "goto" :: args :: [] ->
5931 scan args "%u %f %f"
5932 (fun pageno x y ->
5933 let cmd, _ = state.geomcmds in
5934 if emptystr cmd
5935 then gotopagexy pageno x y
5936 else
5937 let f prevf () =
5938 gotopagexy pageno x y;
5939 prevf ()
5941 state.reprf <- f state.reprf
5943 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
5944 | "gotor" :: args :: [] ->
5945 scan args "%S %u"
5946 (fun filename pageno -> gotounder (Uremote (filename, pageno)))
5947 | "gotord" :: args :: [] ->
5948 scan args "%S %S"
5949 (fun filename dest -> gotounder (Uremotedest (filename, dest)))
5950 | "rect" :: args :: [] ->
5951 scan args "%u %u %f %f %f %f"
5952 (fun pageno color x0 y0 x1 y1 ->
5953 onpagerect pageno (fun w h ->
5954 let _,w1,h1,_ = getpagedim pageno in
5955 let sw = float w1 /. float w
5956 and sh = float h1 /. float h in
5957 let x0s = x0 *. sw
5958 and x1s = x1 *. sw
5959 and y0s = y0 *. sh
5960 and y1s = y1 *. sh in
5961 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
5962 debugrect rect;
5963 state.rects <- (pageno, color, rect) :: state.rects;
5964 G.postRedisplay "rect";
5967 | "activatewin" :: [] -> Wsi.activatewin ()
5968 | "quit" :: [] -> raise Quit
5969 | _ ->
5970 adderrfmt "remote command"
5971 "error processing remote command: %S\n" cmds;
5974 let remote =
5975 let scratch = String.create 80 in
5976 let buf = Buffer.create 80 in
5977 fun fd ->
5978 let rec tempfr () =
5979 try Some (Unix.read fd scratch 0 80)
5980 with
5981 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
5982 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
5983 | exn -> raise exn
5985 match tempfr () with
5986 | None -> Some fd
5987 | Some n ->
5988 if n = 0
5989 then (
5990 Unix.close fd;
5991 if Buffer.length buf > 0
5992 then (
5993 let s = Buffer.contents buf in
5994 Buffer.clear buf;
5995 ract s;
5997 None
5999 else
6000 let rec eat ppos =
6001 let nlpos =
6003 let pos = String.index_from scratch ppos '\n' in
6004 if pos >= n then -1 else pos
6005 with Not_found -> -1
6007 if nlpos >= 0
6008 then (
6009 Buffer.add_substring buf scratch ppos (nlpos-ppos);
6010 let s = Buffer.contents buf in
6011 Buffer.clear buf;
6012 ract s;
6013 eat (nlpos+1);
6015 else (
6016 Buffer.add_substring buf scratch ppos (n-ppos);
6017 Some fd
6019 in eat 0
6022 let remoteopen path =
6023 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
6024 with exn ->
6025 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
6026 None
6029 let () =
6030 let trimcachepath = ref E.s in
6031 let rcmdpath = ref E.s in
6032 let pageno = ref None in
6033 selfexec := Sys.executable_name;
6034 Arg.parse
6035 (Arg.align
6036 [("-p", Arg.String (fun s -> state.password <- s),
6037 "<password> Set password");
6039 ("-f", Arg.String
6040 (fun s ->
6041 Config.fontpath := s;
6042 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
6044 "<path> Set path to the user interface font");
6046 ("-c", Arg.String
6047 (fun s ->
6048 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
6049 Config.confpath := s),
6050 "<path> Set path to the configuration file");
6052 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
6053 "<page-number> Jump to page");
6055 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6056 "<path> Set path to the trim cache file");
6058 ("-dest", Arg.String (fun s -> state.nameddest <- s),
6059 "<named-destination> Set named destination");
6061 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
6062 ("-cxack", Arg.Set cxack, " Cut corners");
6064 ("-remote", Arg.String (fun s -> rcmdpath := s),
6065 "<path> Set path to the remote commands source");
6067 ("-origin", Arg.String (fun s -> state.origin <- s),
6068 "<original-path> Set original path");
6070 ("-v", Arg.Unit (fun () ->
6071 Printf.printf
6072 "%s\nconfiguration path: %s\n"
6073 (version ())
6074 Config.defconfpath
6076 exit 0), " Print version and exit");
6079 (fun s -> state.path <- s)
6080 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6082 if !wtmode
6083 then selfexec := !selfexec ^ " -wtmode";
6085 let histmode = emptystr state.path in
6087 if not (Config.load ())
6088 then prerr_endline "failed to load configuration";
6089 begin match !pageno with
6090 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
6091 | None -> ()
6092 end;
6094 let wsfd, winw, winh = Wsi.init (object (self)
6095 val mutable m_hack = false
6096 val mutable m_clicks = 0
6097 val mutable m_click_x = 0
6098 val mutable m_click_y = 0
6099 val mutable m_lastclicktime = infinity
6101 method private cleanup =
6102 state.roam <- noroam;
6103 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap;
6104 method expose = if not m_hack then G.postRedisplay "expose"
6105 method visible = G.postRedisplay "visible"
6106 method display = m_hack <- false; display ()
6107 method reshape w h =
6108 self#cleanup;
6109 m_hack <- w < state.winw && h < state.winh;
6110 reshape w h
6111 method mouse b d x y m =
6112 if d && canselect ()
6113 then (
6114 (* http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx *)
6115 m_click_x <- x;
6116 m_click_y <- y;
6117 if b = 1
6118 then (
6119 let t = now () in
6120 if abs x - m_click_x > 10
6121 || abs y - m_click_y > 10
6122 || abs_float (t -. m_lastclicktime) > 0.3
6123 then m_clicks <- 0;
6124 m_clicks <- m_clicks + 1;
6125 m_lastclicktime <- t;
6126 if m_clicks = 1
6127 then (
6128 self#cleanup;
6129 G.postRedisplay "cleanup";
6130 state.uioh <- state.uioh#button b d x y m;
6132 else state.uioh <- state.uioh#multiclick m_clicks x y m
6134 else (
6135 self#cleanup;
6136 m_clicks <- 0;
6137 m_lastclicktime <- infinity;
6138 state.uioh <- state.uioh#button b d x y m
6141 else (
6142 state.uioh <- state.uioh#button b d x y m
6144 method motion x y =
6145 state.mpos <- (x, y);
6146 state.uioh <- state.uioh#motion x y
6147 method pmotion x y =
6148 state.mpos <- (x, y);
6149 state.uioh <- state.uioh#pmotion x y
6150 method key k m =
6151 let mascm = m land (
6152 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6153 ) in
6154 let keyboard k m =
6155 let x = state.x and y = state.y in
6156 keyboard k m;
6157 if x != state.x || y != state.y then self#cleanup
6159 match state.keystate with
6160 | KSnone ->
6161 let km = k, mascm in
6162 begin
6163 match
6164 let modehash = state.uioh#modehash in
6165 try Hashtbl.find modehash km
6166 with Not_found ->
6167 try Hashtbl.find (findkeyhash conf "global") km
6168 with Not_found -> KMinsrt (k, m)
6169 with
6170 | KMinsrt (k, m) -> keyboard k m
6171 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6172 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6174 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6175 List.iter (fun (k, m) -> keyboard k m) insrt;
6176 state.keystate <- KSnone
6177 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6178 state.keystate <- KSinto (keys, insrt)
6179 | _ ->
6180 state.keystate <- KSnone
6182 method enter x y =
6183 state.mpos <- (x, y);
6184 state.uioh <- state.uioh#pmotion x y
6185 method leave = state.mpos <- (-1, -1)
6186 method winstate wsl = state.winstate <- wsl; m_hack <- false
6187 method quit = raise Quit
6188 end) conf.cwinw conf.cwinh (platform = Posx) in
6190 state.wsfd <- wsfd;
6192 if not (
6193 List.exists GlMisc.check_extension
6194 [ "GL_ARB_texture_rectangle"
6195 ; "GL_EXT_texture_recangle"
6196 ; "GL_NV_texture_rectangle" ]
6198 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6200 if (
6201 let r = GlMisc.get_string `renderer in
6202 let p = "Mesa DRI Intel(" in
6203 let l = String.length p in
6204 String.length r > l && String.sub r 0 l = p
6206 then (
6207 defconf.sliceheight <- 1024;
6208 defconf.texcount <- 32;
6209 defconf.usepbo <- true;
6212 let cr, sw =
6213 match Ne.res Unix.pipe with
6214 | Ne.Exn exn ->
6215 Printf.eprintf "pipe/crsw failed: %s" (exntos exn);
6216 exit 1
6217 | Ne.Res rw -> rw
6218 and sr, cw =
6219 match Ne.res Unix.pipe with
6220 | Ne.Exn exn ->
6221 Printf.eprintf "pipe/srcw failed: %s" (exntos exn);
6222 exit 1
6223 | Ne.Res rw -> rw
6226 cloexec cr;
6227 cloexec sw;
6228 cloexec sr;
6229 cloexec cw;
6231 setcheckers conf.checkers;
6232 redirectstderr ();
6233 if conf.redirectstderr
6234 then
6235 at_exit (fun () ->
6236 let s = Buffer.contents state.errmsgs ^
6237 (match state.errfd with
6238 | Some fd ->
6239 let s = String.create (80*24) in
6240 let n =
6242 let r, _, _ = Unix.select [fd] [] [] 0.0 in
6243 if List.mem fd r
6244 then Unix.read fd s 0 (String.length s)
6245 else 0
6246 with _ -> 0
6248 if n = 0
6249 then E.s
6250 else String.sub s 0 n
6251 | None -> E.s
6254 try ignore (Unix.write state.stderr s 0 (String.length s))
6255 with exn -> print_endline (exntos exn)
6259 init (cr, cw) (
6260 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
6261 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6262 !Config.fontpath, !trimcachepath,
6263 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
6265 List.iter GlArray.enable [`texture_coord; `vertex];
6266 state.sr <- sr;
6267 state.sw <- sw;
6268 reshape winw winh;
6269 if histmode
6270 then (
6271 state.uioh <- uioh;
6272 enterhistmode ();
6274 else (
6275 state.text <- "Opening " ^ (mbtoutf8 state.path);
6276 opendoc state.path state.password;
6277 state.uioh <- uioh;
6279 display ();
6280 Wsi.mapwin ();
6281 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
6282 let optrfd =
6283 ref (
6284 if nonemptystr !rcmdpath
6285 then remoteopen !rcmdpath
6286 else None
6290 let rec loop deadline =
6291 let r =
6292 match state.errfd with
6293 | None -> [state.sr; state.wsfd]
6294 | Some fd -> [state.sr; state.wsfd; fd]
6296 let r =
6297 match !optrfd with
6298 | None -> r
6299 | Some fd -> fd :: r
6301 if state.redisplay
6302 then (
6303 state.redisplay <- false;
6304 display ();
6306 let timeout =
6307 let now = now () in
6308 if deadline > now
6309 then (
6310 if deadline = infinity
6311 then ~-.1.0
6312 else max 0.0 (deadline -. now)
6314 else 0.0
6316 let r, _, _ =
6317 try Unix.select r [] [] timeout
6318 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6320 begin match r with
6321 | [] ->
6322 state.ghyll None;
6323 let newdeadline =
6324 if state.ghyll == noghyll
6325 then
6326 match state.autoscroll with
6327 | Some step when step != 0 ->
6328 let y = state.y + step in
6329 let y =
6330 if y < 0
6331 then state.maxy
6332 else if y >= state.maxy then 0 else y
6334 gotoy y;
6335 if state.mode = View
6336 then state.text <- E.s;
6337 deadline +. 0.01
6338 | _ -> infinity
6339 else deadline +. 0.01
6341 loop newdeadline
6343 | l ->
6344 let rec checkfds = function
6345 | [] -> ()
6346 | fd :: rest when fd = state.sr ->
6347 let cmd = readcmd state.sr in
6348 act cmd;
6349 checkfds rest
6351 | fd :: rest when fd = state.wsfd ->
6352 Wsi.readresp fd;
6353 checkfds rest
6355 | fd :: rest when Some fd = !optrfd ->
6356 begin match remote fd with
6357 | None -> optrfd := remoteopen !rcmdpath;
6358 | opt -> optrfd := opt
6359 end;
6360 checkfds rest
6362 | fd :: rest ->
6363 let s = String.create 80 in
6364 let n = tempfailureretry (Unix.read fd s 0) 80 in
6365 if conf.redirectstderr
6366 then (
6367 Buffer.add_substring state.errmsgs s 0 n;
6368 state.newerrmsgs <- true;
6369 state.redisplay <- true;
6371 else (
6372 prerr_string (String.sub s 0 n);
6373 flush stderr;
6375 checkfds rest
6377 let hoty = !Config.historder in
6378 checkfds l;
6379 if hoty <> !Config.historder
6380 then enterhistmode ();
6381 let newdeadline =
6382 let deadline1 =
6383 if deadline = infinity
6384 then now () +. 0.01
6385 else deadline
6387 match state.autoscroll with
6388 | Some step when step != 0 -> deadline1
6389 | _ -> if state.ghyll == noghyll then infinity else deadline1
6391 loop newdeadline
6392 end;
6395 loop infinity;
6396 with Quit ->
6397 Config.save leavebirdseye;