Kill redundant method
[llpp.git] / main.ml
blobf958ea359aaa7c5f5463a549f4aa70f26474940a
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 ();;
103 let setfontsize n =
104 fstate.fontsize <- n;
105 fstate.wwidth <- measurestr fstate.fontsize "w";
106 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
109 let vlog fmt =
110 if conf.verbose
111 then
112 Printf.kprintf prerr_endline fmt
113 else
114 Printf.kprintf ignore fmt
117 let launchpath () =
118 if emptystr conf.pathlauncher
119 then print_endline state.path
120 else (
121 let re = Str.regexp "%s" in
122 let command = Str.global_replace re state.path conf.pathlauncher in
123 try popen command []
124 with exn ->
125 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
126 flush stderr;
130 module Ne = struct
131 type 'a t = | Res of 'a | Exn of exn;;
133 let res f =
134 try Res (f ())
135 with exn -> Exn exn
138 let clo fd f =
139 try tempfailureretry Unix.close fd
140 with exn -> f (exntos exn)
143 let dup fd =
144 try Res (tempfailureretry Unix.dup fd)
145 with exn -> Exn exn
148 let dup2 fd1 fd2 =
149 try Res (tempfailureretry (Unix.dup2 fd1) fd2)
150 with exn -> Exn exn
152 end;;
154 let redirectstderr () =
155 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
156 if conf.redirectstderr
157 then
158 match Ne.res Unix.pipe with
159 | Ne.Exn exn ->
160 dolog "failed to create stderr redirection pipes: %s" (exntos exn)
162 | Ne.Res (r, w) ->
163 begin match Ne.dup Unix.stderr with
164 | Ne.Exn exn ->
165 dolog "failed to dup stderr: %s" (exntos exn);
166 Ne.clo r (clofail "pipe/r");
167 Ne.clo w (clofail "pipe/w");
169 | Ne.Res dupstderr ->
170 begin match Ne.dup2 w Unix.stderr with
171 | Ne.Exn exn ->
172 dolog "failed to dup2 to stderr: %s" (exntos exn);
173 Ne.clo dupstderr (clofail "stderr duplicate");
174 Ne.clo r (clofail "redir pipe/r");
175 Ne.clo w (clofail "redir pipe/w");
177 | Ne.Res () ->
178 state.stderr <- dupstderr;
179 state.errfd <- Some r;
180 end;
182 else (
183 state.newerrmsgs <- false;
184 begin match state.errfd with
185 | Some fd ->
186 begin match Ne.dup2 state.stderr Unix.stderr with
187 | Ne.Exn exn ->
188 dolog "failed to dup2 original stderr: %s" (exntos exn)
189 | Ne.Res () ->
190 Ne.clo fd (clofail "dup of stderr");
191 state.errfd <- None;
192 end;
193 | None -> ()
194 end;
195 prerr_string (Buffer.contents state.errmsgs);
196 flush stderr;
197 Buffer.clear state.errmsgs;
201 module G =
202 struct
203 let postRedisplay who =
204 if conf.verbose
205 then prerr_endline ("redisplay for " ^ who);
206 state.redisplay <- true;
208 end;;
210 let getopaque pageno =
211 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
212 with Not_found -> None
215 let putopaque pageno opaque =
216 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
219 let pagetranslatepoint l x y =
220 let dy = y - l.pagedispy in
221 let y = dy + l.pagey in
222 let dx = x - l.pagedispx in
223 let x = dx + l.pagex in
224 (x, y);
227 let onppundermouse g x y d =
228 let rec f = function
229 | l :: rest ->
230 begin match getopaque l.pageno with
231 | Some opaque ->
232 let x0 = l.pagedispx in
233 let x1 = x0 + l.pagevw in
234 let y0 = l.pagedispy in
235 let y1 = y0 + l.pagevh in
236 if y >= y0 && y <= y1 && x >= x0 && x <= x1
237 then
238 let px, py = pagetranslatepoint l x y in
239 match g opaque l px py with
240 | Some res -> res
241 | None -> f rest
242 else f rest
243 | _ ->
244 f rest
246 | [] -> d
248 f state.layout
251 let getunder x y =
252 let g opaque l px py =
253 if state.bzoom
254 then (
255 match rectofblock opaque px py with
256 | Some a ->
257 let rect = (a.(0),a.(2),a.(1),a.(2),a.(1),a.(3),a.(0),a.(3)) in
258 state.rects <- [l.pageno, l.pageno mod 3, rect];
259 G.postRedisplay "getunder";
260 | None -> ()
262 match whatsunder opaque px py with
263 | Unone -> None
264 | under -> Some under
266 onppundermouse g x y Unone
269 let unproject x y =
270 let g opaque l x y =
271 match unproject opaque x y with
272 | Some (x, y) -> Some (Some (l.pageno, x, y))
273 | None -> None
275 onppundermouse g x y None;
278 let showtext c s =
279 state.text <- Printf.sprintf "%c%s" c s;
280 G.postRedisplay "showtext";
283 let pipesel opaque cmd =
284 if hassel opaque
285 then
286 match Ne.res Unix.pipe with
287 | Ne.Exn exn ->
288 showtext '!'
289 (Printf.sprintf "pipesel can not create pipe: %s" (exntos exn));
290 | Ne.Res (r, w) ->
291 let doclose what fd =
292 Ne.clo fd (fun msg -> dolog "%s close failed: %s" what msg)
294 let popened =
295 try popen cmd [r, 0; w, -1]; true
296 with exn ->
297 dolog "can not execute %S: %s" cmd (exntos exn);
298 false
300 if popened
301 then (
302 copysel w opaque;
303 G.postRedisplay "pipesel";
305 else doclose "pipesel pipe/w" w;
306 doclose "pipesel pipe/r" r;
309 let paxunder x y =
310 let g opaque l px py =
311 if markunder opaque px py conf.paxmark
312 then (
313 Some (fun () ->
314 match getopaque l.pageno with
315 | None -> ()
316 | Some opaque -> pipesel opaque conf.paxcmd
319 else None
321 G.postRedisplay "paxunder";
322 if conf.paxmark = Mark_page
323 then
324 List.iter (fun l ->
325 match getopaque l.pageno with
326 | None -> ()
327 | Some opaque -> clearmark opaque) state.layout;
328 state.roam <-
329 onppundermouse g x y (fun () -> showtext '!' "Whoopsie daisy");
332 let selstring s =
333 match Ne.res Unix.pipe with
334 | Ne.Exn exn ->
335 showtext '!' (Printf.sprintf "pipe failed: %s" (exntos exn))
336 | Ne.Res (r, w) ->
337 let clo cap fd =
338 Ne.clo fd (fun msg ->
339 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
342 let popened =
343 try popen conf.selcmd [r, 0; w, -1]; true
344 with exn ->
345 showtext '!'
346 (Printf.sprintf "failed to execute %s: %s"
347 conf.selcmd (exntos exn));
348 false
350 if popened
351 then (
353 let l = String.length s in
354 let n = tempfailureretry (Unix.write w s 0) l in
355 if n != l
356 then
357 showtext '!'
358 (Printf.sprintf
359 "failed to write %d characters to sel pipe, wrote %d"
362 with exn ->
363 showtext '!'
364 (Printf.sprintf "failed to write to sel pipe: %s"
365 (exntos exn)
368 else dolog "%s" s;
369 clo "selstring pipe/r" r;
370 clo "selstring pipe/w" w;
373 let undertext = function
374 | Unone -> "none"
375 | Ulinkuri s -> s
376 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
377 | Utext s -> "font: " ^ s
378 | Uunexpected s -> "unexpected: " ^ s
379 | Ulaunch s -> "launch: " ^ s
380 | Unamed s -> "named: " ^ s
381 | Uremote (filename, pageno) ->
382 Printf.sprintf "%s: page %d" filename (pageno+1)
383 | Uremotedest (filename, destname) ->
384 Printf.sprintf "%s: destination %S" filename destname
387 let updateunder x y =
388 match getunder x y with
389 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
390 | Ulinkuri uri ->
391 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
392 Wsi.setcursor Wsi.CURSOR_INFO
393 | Ulinkgoto (pageno, _) ->
394 if conf.underinfo
395 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
396 Wsi.setcursor Wsi.CURSOR_INFO
397 | Utext s ->
398 if conf.underinfo then showtext 'f' ("ont: " ^ s);
399 Wsi.setcursor Wsi.CURSOR_TEXT
400 | Uunexpected s ->
401 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
402 Wsi.setcursor Wsi.CURSOR_INHERIT
403 | Ulaunch s ->
404 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
405 Wsi.setcursor Wsi.CURSOR_INHERIT
406 | Unamed s ->
407 if conf.underinfo then showtext 'n' ("amed: " ^ s);
408 Wsi.setcursor Wsi.CURSOR_INHERIT
409 | Uremote (filename, pageno) ->
410 if conf.underinfo then showtext 'r'
411 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
412 Wsi.setcursor Wsi.CURSOR_INFO
413 | Uremotedest (filename, destname) ->
414 if conf.underinfo then showtext 'r'
415 (Printf.sprintf "emote destination: %s (%S)" filename destname);
416 Wsi.setcursor Wsi.CURSOR_INFO
419 let showlinktype under =
420 if conf.underinfo
421 then
422 match under with
423 | Unone -> ()
424 | under ->
425 let s = undertext under in
426 showtext ' ' s
429 let addchar s c =
430 let b = Buffer.create (String.length s + 1) in
431 Buffer.add_string b s;
432 Buffer.add_char b c;
433 Buffer.contents b;
436 let intentry_with_suffix text key =
437 let c =
438 if key >= 32 && key < 127
439 then Char.chr key
440 else '\000'
442 match Char.lowercase c with
443 | '0' .. '9' ->
444 let text = addchar text c in
445 TEcont text
447 | 'k' | 'm' | 'g' ->
448 let text = addchar text c in
449 TEcont text
451 | _ ->
452 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
453 TEcont text
456 let readcmd fd =
457 let s = "xxxx" in
458 let n = tempfailureretry (Unix.read fd s 0) 4 in
459 if n != 4 then error "incomplete read(len) = %d" n;
460 let len = 0
461 lor (Char.code s.[0] lsl 24)
462 lor (Char.code s.[1] lsl 16)
463 lor (Char.code s.[2] lsl 8)
464 lor (Char.code s.[3] lsl 0)
466 let s = String.create len in
467 let n = tempfailureretry (Unix.read fd s 0) len in
468 if n != len then error "incomplete read(data) %d vs %d" n len;
472 let btod b = if b then 1 else 0;;
474 let wcmd fmt =
475 let b = Buffer.create 16 in
476 Buffer.add_string b "llll";
477 Printf.kbprintf
478 (fun b ->
479 let s = Buffer.contents b in
480 let n = String.length s in
481 let len = n - 4 in
482 (* dolog "wcmd %S" (String.sub s 4 len); *)
483 s.[0] <- Char.chr ((len lsr 24) land 0xff);
484 s.[1] <- Char.chr ((len lsr 16) land 0xff);
485 s.[2] <- Char.chr ((len lsr 8) land 0xff);
486 s.[3] <- Char.chr (len land 0xff);
487 let n' = tempfailureretry (Unix.write state.sw s 0) n in
488 if n' != n then error "write failed %d vs %d" n' n;
489 ) b fmt;
492 let nogeomcmds cmds =
493 match cmds with
494 | s, [] -> emptystr s
495 | _ -> false
498 let layoutN ((columns, coverA, coverB), b) y sh =
499 let sh = sh - (hscrollh ()) in
500 let rec fold accu n =
501 if n = Array.length b
502 then accu
503 else
504 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
505 if (vy - y) > sh &&
506 (n = coverA - 1
507 || n = state.pagecount - coverB
508 || (n - coverA) mod columns = columns - 1)
509 then accu
510 else
511 let accu =
512 if vy + h > y
513 then
514 let pagey = max 0 (y - vy) in
515 let pagedispy = if pagey > 0 then 0 else vy - y in
516 let pagedispx, pagex =
517 let pdx =
518 if n = coverA - 1 || n = state.pagecount - coverB
519 then state.x + (wadjsb state.winw - w) / 2
520 else dx + xoff + state.x
522 if pdx < 0
523 then 0, -pdx
524 else pdx, 0
526 let pagevw =
527 let vw = wadjsb state.winw - pagedispx in
528 let pw = w - pagex in
529 min vw pw
531 let pagevh = min (h - pagey) (sh - pagedispy) in
532 if pagevw > 0 && pagevh > 0
533 then
534 let e =
535 { pageno = n
536 ; pagedimno = pdimno
537 ; pagew = w
538 ; pageh = h
539 ; pagex = pagex
540 ; pagey = pagey
541 ; pagevw = pagevw
542 ; pagevh = pagevh
543 ; pagedispx = pagedispx
544 ; pagedispy = pagedispy
545 ; pagecol = 0
548 e :: accu
549 else
550 accu
551 else
552 accu
554 fold accu (n+1)
556 if Array.length b = 0
557 then []
558 else List.rev (fold [] (page_of_y y))
561 let layoutS (columns, b) y sh =
562 let sh = sh - hscrollh () in
563 let rec fold accu n =
564 if n = Array.length b
565 then accu
566 else
567 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
568 if (vy - y) > sh
569 then accu
570 else
571 let accu =
572 if vy + pageh > y
573 then
574 let x = xoff + state.x in
575 let pagey = max 0 (y - vy) in
576 let pagedispy = if pagey > 0 then 0 else vy - y in
577 let pagedispx, pagex =
578 if px = 0
579 then (
580 if x < 0
581 then 0, -x
582 else x, 0
584 else (
585 let px = px - x in
586 if px < 0
587 then -px, 0
588 else 0, px
591 let pagecolw = pagew/columns in
592 let pagedispx =
593 if pagecolw < state.winw
594 then pagedispx + ((wadjsb state.winw - pagecolw) / 2)
595 else pagedispx
597 let pagevw =
598 let vw = wadjsb state.winw - pagedispx in
599 let pw = pagew - pagex in
600 min vw pw
602 let pagevw = min pagevw pagecolw in
603 let pagevh = min (pageh - pagey) (sh - pagedispy) in
604 if pagevw > 0 && pagevh > 0
605 then
606 let e =
607 { pageno = n/columns
608 ; pagedimno = pdimno
609 ; pagew = pagew
610 ; pageh = pageh
611 ; pagex = pagex
612 ; pagey = pagey
613 ; pagevw = pagevw
614 ; pagevh = pagevh
615 ; pagedispx = pagedispx
616 ; pagedispy = pagedispy
617 ; pagecol = n mod columns
620 e :: accu
621 else
622 accu
623 else
624 accu
626 fold accu (n+1)
628 List.rev (fold [] 0)
631 let layout y sh =
632 if nogeomcmds state.geomcmds
633 then
634 match conf.columns with
635 | Csingle b -> layoutN ((1, 0, 0), b) y sh
636 | Cmulti c -> layoutN c y sh
637 | Csplit s -> layoutS s y sh
638 else []
641 let clamp incr =
642 let y = state.y + incr in
643 let y = max 0 y in
644 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
648 let itertiles l f =
649 let tilex = l.pagex mod conf.tilew in
650 let tiley = l.pagey mod conf.tileh in
652 let col = l.pagex / conf.tilew in
653 let row = l.pagey / conf.tileh in
655 let rec rowloop row y0 dispy h =
656 if h = 0
657 then ()
658 else (
659 let dh = conf.tileh - y0 in
660 let dh = min h dh in
661 let rec colloop col x0 dispx w =
662 if w = 0
663 then ()
664 else (
665 let dw = conf.tilew - x0 in
666 let dw = min w dw in
667 let dispx' =
668 if conf.leftscroll
669 then dispx+conf.scrollbw
670 else dispx
672 f col row dispx' dispy x0 y0 dw dh;
673 colloop (col+1) 0 (dispx+dw) (w-dw)
676 colloop col tilex l.pagedispx l.pagevw;
677 rowloop (row+1) 0 (dispy+dh) (h-dh)
680 if l.pagevw > 0 && l.pagevh > 0
681 then rowloop row tiley l.pagedispy l.pagevh;
684 let gettileopaque l col row =
685 let key =
686 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
688 try Some (Hashtbl.find state.tilemap key)
689 with Not_found -> None
692 let puttileopaque l col row gen colorspace angle opaque size elapsed =
693 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
694 Hashtbl.add state.tilemap key (opaque, size, elapsed)
697 let filledrect x0 y0 x1 y1 =
698 GlArray.disable `texture_coord;
699 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
700 GlArray.vertex `two state.vraw;
701 GlArray.draw_arrays `triangle_strip 0 4;
702 GlArray.enable `texture_coord;
705 let linerect x0 y0 x1 y1 =
706 GlArray.disable `texture_coord;
707 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y1; x1; y0 |];
708 GlArray.vertex `two state.vraw;
709 GlArray.draw_arrays `line_loop 0 4;
710 GlArray.enable `texture_coord;
713 let drawtiles l color =
714 GlDraw.color color;
715 begintiles ();
716 let f col row x y tilex tiley w h =
717 match gettileopaque l col row with
718 | Some (opaque, _, t) ->
719 let params = x, y, w, h, tilex, tiley in
720 if conf.invert
721 then GlTex.env (`mode `blend);
722 drawtile params opaque;
723 if conf.invert
724 then GlTex.env (`mode `modulate);
725 if conf.debug
726 then (
727 endtiles ();
728 let s = Printf.sprintf
729 "%d[%d,%d] %f sec"
730 l.pageno col row t
732 let w = measurestr fstate.fontsize s in
733 GlDraw.color (0.0, 0.0, 0.0);
734 filledrect (float (x-2))
735 (float (y-2))
736 (float (x+2) +. w)
737 (float (y + fstate.fontsize + 2));
738 GlDraw.color (1.0, 1.0, 1.0);
739 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
740 begintiles ();
743 | None ->
744 endtiles ();
745 let w =
746 if conf.leftscroll
747 then w
748 else
749 let lw = wadjsb state.winw - x in
750 min lw w
751 and h =
752 let lh = state.winh - y in
753 min lh h
755 if conf.invert
756 then GlTex.env (`mode `blend);
757 begin match state.checkerstexid with
758 | Some id ->
759 Gl.enable `texture_2d;
760 GlTex.bind_texture `texture_2d id;
761 let x0 = float x
762 and y0 = float y
763 and x1 = float (x+w)
764 and y1 = float (y+h) in
766 let tw = float w /. 16.0
767 and th = float h /. 16.0 in
768 let tx0 = float tilex /. 16.0
769 and ty0 = float tiley /. 16.0 in
770 let tx1 = tx0 +. tw
771 and ty1 = ty0 +. th in
772 Raw.sets_float state.vraw ~pos:0
773 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
774 Raw.sets_float state.traw ~pos:0
775 [| tx0; ty0; tx0; ty1; tx1; ty0; tx1; ty1 |];
776 GlArray.vertex `two state.vraw;
777 GlArray.tex_coord `two state.traw;
778 GlArray.draw_arrays `triangle_strip 0 4;
779 Gl.disable `texture_2d;
781 | None ->
782 GlDraw.color (1.0, 1.0, 1.0);
783 filledrect (float x) (float y) (float (x+w)) (float (y+h));
784 end;
785 if conf.invert
786 then GlTex.env (`mode `modulate);
787 if w > 128 && h > fstate.fontsize + 10
788 then (
789 let c = if conf.invert then 1.0 else 0.0 in
790 GlDraw.color (c, c, c);
791 let c, r =
792 if conf.verbose
793 then (col*conf.tilew, row*conf.tileh)
794 else col, row
796 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
798 GlDraw.color color;
799 begintiles ();
801 itertiles l f;
802 endtiles ();
805 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
807 let tilevisible1 l x y =
808 let ax0 = l.pagex
809 and ax1 = l.pagex + l.pagevw
810 and ay0 = l.pagey
811 and ay1 = l.pagey + l.pagevh in
813 let bx0 = x
814 and by0 = y in
815 let bx1 = min (bx0 + conf.tilew) l.pagew
816 and by1 = min (by0 + conf.tileh) l.pageh in
818 let rx0 = max ax0 bx0
819 and ry0 = max ay0 by0
820 and rx1 = min ax1 bx1
821 and ry1 = min ay1 by1 in
823 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
824 nonemptyintersection
827 let tilevisible layout n x y =
828 let rec findpageinlayout m = function
829 | l :: rest when l.pageno = n ->
830 tilevisible1 l x y || (
831 match conf.columns with
832 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
833 | _ -> false
835 | _ :: rest -> findpageinlayout 0 rest
836 | [] -> false
838 findpageinlayout 0 layout;
841 let tileready l x y =
842 tilevisible1 l x y &&
843 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
846 let tilepage n p layout =
847 let rec loop = function
848 | l :: rest ->
849 if l.pageno = n
850 then
851 let f col row _ _ _ _ _ _ =
852 if state.currently = Idle
853 then
854 match gettileopaque l col row with
855 | Some _ -> ()
856 | None ->
857 let x = col*conf.tilew
858 and y = row*conf.tileh in
859 let w =
860 let w = l.pagew - x in
861 min w conf.tilew
863 let h =
864 let h = l.pageh - y in
865 min h conf.tileh
867 let pbo =
868 if conf.usepbo
869 then getpbo w h conf.colorspace
870 else ~< "0"
872 wcmd "tile %s %d %d %d %d %s"
873 (~> p) x y w h (~> pbo);
874 state.currently <-
875 Tiling (
876 l, p, conf.colorspace, conf.angle,
877 state.gen, col, row, conf.tilew, conf.tileh
880 itertiles l f;
881 else
882 loop rest
884 | [] -> ()
886 if nogeomcmds state.geomcmds
887 then loop layout;
890 let preloadlayout y =
891 let y = if y < state.winh then 0 else y - state.winh in
892 let h = state.winh*3 in
893 layout y h;
896 let load pages =
897 let rec loop pages =
898 if state.currently != Idle
899 then ()
900 else
901 match pages with
902 | l :: rest ->
903 begin match getopaque l.pageno with
904 | None ->
905 wcmd "page %d %d" l.pageno l.pagedimno;
906 state.currently <- Loading (l, state.gen);
907 | Some opaque ->
908 tilepage l.pageno opaque pages;
909 loop rest
910 end;
911 | _ -> ()
913 if nogeomcmds state.geomcmds
914 then loop pages
917 let preload pages =
918 load pages;
919 if conf.preload && state.currently = Idle
920 then load (preloadlayout state.y);
923 let layoutready layout =
924 let rec fold all ls =
925 all && match ls with
926 | l :: rest ->
927 let seen = ref false in
928 let allvisible = ref true in
929 let foo col row _ _ _ _ _ _ =
930 seen := true;
931 allvisible := !allvisible &&
932 begin match gettileopaque l col row with
933 | Some _ -> true
934 | None -> false
937 itertiles l foo;
938 fold (!seen && !allvisible) rest
939 | [] -> true
941 let alltilesvisible = fold true layout in
942 alltilesvisible;
945 let gotoy y =
946 let y = bound y 0 state.maxy in
947 let y, layout, proceed =
948 match conf.maxwait with
949 | Some time when state.ghyll == noghyll ->
950 begin match state.throttle with
951 | None ->
952 let layout = layout y state.winh in
953 let ready = layoutready layout in
954 if not ready
955 then (
956 load layout;
957 state.throttle <- Some (layout, y, now ());
959 else G.postRedisplay "gotoy showall (None)";
960 y, layout, ready
961 | Some (_, _, started) ->
962 let dt = now () -. started in
963 if dt > time
964 then (
965 state.throttle <- None;
966 let layout = layout y state.winh in
967 load layout;
968 G.postRedisplay "maxwait";
969 y, layout, true
971 else -1, [], false
974 | _ ->
975 let layout = layout y state.winh in
976 if not !wtmode || layoutready layout
977 then G.postRedisplay "gotoy ready";
978 y, layout, true
980 if proceed
981 then (
982 state.y <- y;
983 state.layout <- layout;
984 begin match state.mode with
985 | LinkNav (Ltexact (pageno, linkno)) ->
986 let rec loop = function
987 | [] ->
988 state.mode <- LinkNav (Ltgendir 0)
989 | l :: _ when l.pageno = pageno ->
990 begin match getopaque pageno with
991 | None ->
992 state.mode <- LinkNav (Ltgendir 0)
993 | Some opaque ->
994 let x0, y0, x1, y1 = getlinkrect opaque linkno in
995 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
996 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
997 then state.mode <- LinkNav (Ltgendir 0)
999 | _ :: rest -> loop rest
1001 loop layout
1002 | _ -> ()
1003 end;
1004 begin match state.mode with
1005 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1006 if not (pagevisible layout pageno)
1007 then (
1008 match state.layout with
1009 | [] -> ()
1010 | l :: _ ->
1011 state.mode <- Birdseye (
1012 conf, leftx, l.pageno, hooverpageno, anchor
1015 | LinkNav (Ltgendir dir as lt) ->
1016 let linknav =
1017 let rec loop = function
1018 | [] -> lt
1019 | l :: rest ->
1020 match getopaque l.pageno with
1021 | None -> loop rest
1022 | Some opaque ->
1023 let link =
1024 let ld =
1025 if dir = 0
1026 then LDfirstvisible (l.pagex, l.pagey, dir)
1027 else (
1028 if dir > 0 then LDfirst else LDlast
1031 findlink opaque ld
1033 match link with
1034 | Lnotfound -> loop rest
1035 | Lfound n ->
1036 showlinktype (getlink opaque n);
1037 Ltexact (l.pageno, n)
1039 loop state.layout
1041 state.mode <- LinkNav linknav
1042 | _ -> ()
1043 end;
1044 preload layout;
1046 state.ghyll <- noghyll;
1047 if conf.updatecurs
1048 then (
1049 let mx, my = state.mpos in
1050 updateunder mx my;
1054 let conttiling pageno opaque =
1055 tilepage pageno opaque
1056 (if conf.preload then preloadlayout state.y else state.layout)
1059 let gotoy_and_clear_text y =
1060 if not conf.verbose then state.text <- E.s;
1061 gotoy y;
1064 let getanchory (n, top, dtop) =
1065 let y, h = getpageyh n in
1066 if conf.presentation
1067 then
1068 let ips = calcips h in
1069 y + truncate (top*.float h -. dtop*.float ips) + ips;
1070 else
1071 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1074 let gotoanchor anchor =
1075 gotoy (getanchory anchor);
1078 let addnav () =
1079 cbput state.hists.nav (getanchor ());
1082 let getnav dir =
1083 let anchor = cbgetc state.hists.nav dir in
1084 getanchory anchor;
1087 let gotoghyll1 single y =
1088 let scroll f n a b =
1089 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1090 let snake f a b =
1091 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1092 if f < a
1093 then s (float f /. float a)
1094 else (
1095 if f > b
1096 then 1.0 -. s ((float (f-b) /. float (n-b)))
1097 else 1.0
1100 snake f a b
1101 and summa n a b =
1102 let ins = float a *. 0.5
1103 and outs = float (n-b) *. 0.5 in
1104 let ones = b - a in
1105 ins +. outs +. float ones
1107 let rec set nab y sy =
1108 let (_N, _A, _B), y =
1109 if single
1110 then
1111 let scl = if y > sy then 2 else -2 in
1112 let _N, _, _ = nab in
1113 (_N,0,_N), y+conf.scrollstep*scl
1114 else nab,y in
1115 let sum = summa _N _A _B in
1116 let dy = float (y - sy) in
1117 state.ghyll <- (
1118 let rec gf n y1 o =
1119 if n >= _N
1120 then state.ghyll <- noghyll
1121 else
1122 let go n =
1123 let s = scroll n _N _A _B in
1124 let y1 = y1 +. ((s *. dy) /. sum) in
1125 gotoy_and_clear_text (truncate y1);
1126 state.ghyll <- gf (n+1) y1;
1128 match o with
1129 | None -> go n
1130 | Some y' when single -> set nab y' state.y
1131 | Some y' -> set (_N/2, 1, 1) y' state.y
1133 gf 0 (float state.y)
1136 match conf.ghyllscroll with
1137 | Some nab when not conf.presentation ->
1138 if state.ghyll == noghyll
1139 then set nab y state.y
1140 else state.ghyll (Some y)
1141 | _ ->
1142 gotoy_and_clear_text y
1145 let gotoghyll = gotoghyll1 false;;
1147 let gotopage n top =
1148 let y, h = getpageyh n in
1149 let y = y + (truncate (top *. float h)) in
1150 gotoghyll y
1153 let gotopage1 n top =
1154 let y = getpagey n in
1155 let y = y + top in
1156 gotoghyll y
1159 let invalidate s f =
1160 state.layout <- [];
1161 state.pdims <- [];
1162 state.rects <- [];
1163 state.rects1 <- [];
1164 match state.geomcmds with
1165 | ps, [] when emptystr ps ->
1166 f ();
1167 state.geomcmds <- s, [];
1169 | ps, [] ->
1170 state.geomcmds <- ps, [s, f];
1172 | ps, (s', _) :: rest when s' = s ->
1173 state.geomcmds <- ps, ((s, f) :: rest);
1175 | ps, cmds ->
1176 state.geomcmds <- ps, ((s, f) :: cmds);
1179 let flushpages () =
1180 Hashtbl.iter (fun _ opaque ->
1181 wcmd "freepage %s" (~> opaque);
1182 ) state.pagemap;
1183 Hashtbl.clear state.pagemap;
1186 let flushtiles () =
1187 if not (Queue.is_empty state.tilelru)
1188 then (
1189 Queue.iter (fun (k, p, s) ->
1190 wcmd "freetile %s" (~> p);
1191 state.memused <- state.memused - s;
1192 Hashtbl.remove state.tilemap k;
1193 ) state.tilelru;
1194 state.uioh#infochanged Memused;
1195 Queue.clear state.tilelru;
1197 load state.layout;
1200 let stateh h =
1201 let h = truncate (float h*.conf.zoom) in
1202 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
1203 h - d
1206 let opendoc path password =
1207 state.path <- path;
1208 state.password <- password;
1209 state.gen <- state.gen + 1;
1210 state.docinfo <- [];
1212 flushpages ();
1213 setaalevel conf.aalevel;
1214 let titlepath =
1215 if emptystr state.origin
1216 then path
1217 else state.origin
1219 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
1220 wcmd "open %d %d %s\000%s\000" (btod !wtmode) (btod !cxack) path password;
1221 invalidate "reqlayout"
1222 (fun () ->
1223 wcmd "reqlayout %d %d %d %s\000"
1224 conf.angle (FMTE.to_int conf.fitmodel)
1225 (stateh state.winh) state.nameddest
1229 let reload () =
1230 state.anchor <- getanchor ();
1231 opendoc state.path state.password;
1234 let scalecolor c =
1235 let c = c *. conf.colorscale in
1236 (c, c, c);
1239 let scalecolor2 (r, g, b) =
1240 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1243 let docolumns = function
1244 | Csingle _ ->
1245 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1246 let rec loop pageno pdimno pdim y ph pdims =
1247 if pageno = state.pagecount
1248 then ()
1249 else
1250 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1251 match pdims with
1252 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1253 pdimno+1, pdim, rest
1254 | _ ->
1255 pdimno, pdim, pdims
1257 let x = max 0 (((wadjsb state.winw - w) / 2) - xoff) in
1258 let y = y +
1259 (if conf.presentation
1260 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1261 else (if pageno = 0 then 0 else conf.interpagespace)
1264 a.(pageno) <- (pdimno, x, y, pdim);
1265 loop (pageno+1) pdimno pdim (y + h) h pdims
1267 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1268 conf.columns <- Csingle a;
1270 | Cmulti ((columns, coverA, coverB), _) ->
1271 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1272 let rec loop pageno pdimno pdim x y rowh pdims =
1273 let rec fixrow m = if m = pageno then () else
1274 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1275 if h < rowh
1276 then (
1277 let y = y + (rowh - h) / 2 in
1278 a.(m) <- (pdimno, x, y, pdim);
1280 fixrow (m+1)
1282 if pageno = state.pagecount
1283 then fixrow (((pageno - 1) / columns) * columns)
1284 else
1285 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1286 match pdims with
1287 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1288 pdimno+1, pdim, rest
1289 | _ ->
1290 pdimno, pdim, pdims
1292 let x, y, rowh' =
1293 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1294 then (
1295 let x = (wadjsb state.winw - w) / 2 in
1296 let ips =
1297 if conf.presentation then calcips h else conf.interpagespace in
1298 x, y + ips + rowh, h
1300 else (
1301 if (pageno - coverA) mod columns = 0
1302 then (
1303 let x = max 0 (wadjsb state.winw - state.w) / 2 in
1304 let y =
1305 if conf.presentation
1306 then
1307 let ips = calcips h in
1308 y + (if pageno = 0 then 0 else calcips rowh + ips)
1309 else
1310 y + (if pageno = 0 then 0 else conf.interpagespace)
1312 x, y + rowh, h
1314 else x, y, max rowh h
1317 let y =
1318 if pageno > 1 && (pageno - coverA) mod columns = 0
1319 then (
1320 let y =
1321 if pageno = columns && conf.presentation
1322 then (
1323 let ips = calcips rowh in
1324 for i = 0 to pred columns
1326 let (pdimno, x, y, pdim) = a.(i) in
1327 a.(i) <- (pdimno, x, y+ips, pdim)
1328 done;
1329 y+ips;
1331 else y
1333 fixrow (pageno - columns);
1336 else y
1338 a.(pageno) <- (pdimno, x, y, pdim);
1339 let x = x + w + xoff*2 + conf.interpagespace in
1340 loop (pageno+1) pdimno pdim x y rowh' pdims
1342 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1343 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1345 | Csplit (c, _) ->
1346 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1347 let rec loop pageno pdimno pdim y pdims =
1348 if pageno = state.pagecount
1349 then ()
1350 else
1351 let pdimno, ((_, w, h, _) as pdim), pdims =
1352 match pdims with
1353 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1354 pdimno+1, pdim, rest
1355 | _ ->
1356 pdimno, pdim, pdims
1358 let cw = w / c in
1359 let rec loop1 n x y =
1360 if n = c then y else (
1361 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1362 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1365 let y = loop1 0 0 y in
1366 loop (pageno+1) pdimno pdim y pdims
1368 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1369 conf.columns <- Csplit (c, a);
1372 let represent () =
1373 docolumns conf.columns;
1374 state.maxy <- calcheight ();
1375 if state.reprf == noreprf
1376 then (
1377 match state.mode with
1378 | Birdseye (_, _, pageno, _, _) ->
1379 let y, h = getpageyh pageno in
1380 let top = (state.winh - h) / 2 in
1381 gotoy (max 0 (y - top))
1382 | _ -> gotoanchor state.anchor
1384 else (
1385 state.reprf ();
1386 state.reprf <- noreprf;
1390 let reshape w h =
1391 GlDraw.viewport 0 0 w h;
1392 let firsttime = state.geomcmds == firstgeomcmds in
1393 if not firsttime && nogeomcmds state.geomcmds
1394 then state.anchor <- getanchor ();
1396 state.winw <- w;
1397 let w = wadjsb (truncate (float w *. conf.zoom)) in
1398 let w = max w 2 in
1399 state.winh <- h;
1400 setfontsize fstate.fontsize;
1401 GlMat.mode `modelview;
1402 GlMat.load_identity ();
1404 GlMat.mode `projection;
1405 GlMat.load_identity ();
1406 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1407 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1408 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
1410 let relx =
1411 if conf.zoom <= 1.0
1412 then 0.0
1413 else float state.x /. float state.w
1415 invalidate "geometry"
1416 (fun () ->
1417 state.w <- w;
1418 if not firsttime
1419 then state.x <- truncate (relx *. float w);
1420 let w =
1421 match conf.columns with
1422 | Csingle _ -> w
1423 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1424 | Csplit (c, _) -> w * c
1426 wcmd "geometry %d %d %d"
1427 w (stateh h) (FMTE.to_int conf.fitmodel)
1431 let enttext () =
1432 let len = String.length state.text in
1433 let x0 = if conf.leftscroll then conf.scrollbw else 0 in
1434 let drawstring s =
1435 let hscrollh =
1436 match state.mode with
1437 | Textentry _ | View | LinkNav _ ->
1438 let h, _, _ = state.uioh#scrollpw in
1440 | _ -> 0
1442 let rect x w =
1443 filledrect x (float (state.winh - (fstate.fontsize + 4) - hscrollh))
1444 (x+.w) (float (state.winh - hscrollh))
1447 let w = float (wadjsb state.winw - 1) in
1448 if state.progress >= 0.0 && state.progress < 1.0
1449 then (
1450 GlDraw.color (0.3, 0.3, 0.3);
1451 let w1 = w *. state.progress in
1452 rect (float x0) w1;
1453 GlDraw.color (0.0, 0.0, 0.0);
1454 rect (float x0+.w1) (float x0+.w-.w1)
1456 else (
1457 GlDraw.color (0.0, 0.0, 0.0);
1458 rect (float x0) w;
1461 GlDraw.color (1.0, 1.0, 1.0);
1462 drawstring fstate.fontsize
1463 (if conf.leftscroll then x0 else x0 + if len > 0 then 8 else 2)
1464 (state.winh - hscrollh - 5) s;
1466 let s =
1467 match state.mode with
1468 | Textentry ((prefix, text, _, _, _, _), _) ->
1469 let s =
1470 if len > 0
1471 then
1472 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1473 else
1474 Printf.sprintf "%s%s_" prefix text
1478 | _ -> state.text
1480 let s =
1481 if state.newerrmsgs
1482 then (
1483 if not (istextentry state.mode) && state.uioh#eformsgs
1484 then
1485 let s1 = "(press 'e' to review error messasges)" in
1486 if nonemptystr s then s ^ " " ^ s1 else s1
1487 else s
1489 else s
1491 if nonemptystr s
1492 then drawstring s
1495 let gctiles () =
1496 let len = Queue.length state.tilelru in
1497 let layout = lazy (
1498 match state.throttle with
1499 | None ->
1500 if conf.preload
1501 then preloadlayout state.y
1502 else state.layout
1503 | Some (layout, _, _) ->
1504 layout
1505 ) in
1506 let rec loop qpos =
1507 if state.memused <= conf.memlimit
1508 then ()
1509 else (
1510 if qpos < len
1511 then
1512 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1513 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1514 let (_, pw, ph, _) = getpagedim n in
1516 gen = state.gen
1517 && colorspace = conf.colorspace
1518 && angle = conf.angle
1519 && pagew = pw
1520 && pageh = ph
1521 && (
1522 let x = col*conf.tilew
1523 and y = row*conf.tileh in
1524 tilevisible (Lazy.force_val layout) n x y
1526 then Queue.push lruitem state.tilelru
1527 else (
1528 freepbo p;
1529 wcmd "freetile %s" (~> p);
1530 state.memused <- state.memused - s;
1531 state.uioh#infochanged Memused;
1532 Hashtbl.remove state.tilemap k;
1534 loop (qpos+1)
1537 loop 0
1540 let logcurrently = function
1541 | Idle -> dolog "Idle"
1542 | Loading (l, gen) ->
1543 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
1544 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
1545 dolog
1546 "Tiling %d[%d,%d] page=%s cs=%s angle"
1547 l.pageno col row (~> pageopaque)
1548 (CSTE.to_string colorspace)
1550 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
1551 angle gen conf.angle state.gen
1552 tilew tileh
1553 conf.tilew conf.tileh
1555 | Outlining _ ->
1556 dolog "outlining"
1559 let splitatspace =
1560 let r = Str.regexp " " in
1561 fun s -> Str.bounded_split r s 2;
1564 let onpagerect pageno f =
1565 let b =
1566 match conf.columns with
1567 | Cmulti (_, b) -> b
1568 | Csingle b -> b
1569 | Csplit (_, b) -> b
1571 if pageno >= 0 && pageno < Array.length b
1572 then
1573 let (_, _, _, (w, h, _, _)) = b.(pageno) in
1574 f w h
1577 let gotopagexy1 pageno x y =
1578 let _,w1,h1,leftx = getpagedim pageno in
1579 let top = y /. (float h1) in
1580 let left = x /. (float w1) in
1581 let py, w, h = getpageywh pageno in
1582 let wh = state.winh - hscrollh () in
1583 let x = left *. (float w) in
1584 let x = leftx + state.x + truncate x in
1585 let sx =
1586 if x < 0 || x >= wadjsb state.winw
1587 then state.x - x
1588 else state.x
1590 let pdy = truncate (top *. float h) in
1591 let y' = py + pdy in
1592 let dy = y' - state.y in
1593 let sy =
1594 if x != state.x || not (dy > 0 && dy < wh)
1595 then (
1596 if conf.presentation
1597 then
1598 if abs (py - y') > wh
1599 then y'
1600 else py
1601 else y';
1603 else state.y
1605 if state.x != sx || state.y != sy
1606 then (
1607 let x, y =
1608 if !wtmode
1609 then (
1610 let ww = wadjsb state.winw in
1611 let qx = sx / ww
1612 and qy = pdy / wh in
1613 let x = qx * ww
1614 and y = py + qy * wh in
1615 let x = if -x + ww > w1 then -(w1-ww) else x
1616 and y' = if y + wh > state.maxy then state.maxy - wh else y in
1617 let y =
1618 if conf.presentation
1619 then
1620 if abs (py - y') > wh
1621 then y'
1622 else py
1623 else y';
1625 (x, y)
1627 else (sx, sy)
1629 state.x <- x;
1630 gotoy_and_clear_text y;
1632 else gotoy_and_clear_text state.y;
1635 let gotopagexy pageno x y =
1636 match state.mode with
1637 | Birdseye _ -> gotopage pageno 0.0
1638 | _ -> gotopagexy1 pageno x y
1641 let act cmds =
1642 (* dolog "%S" cmds; *)
1643 let cl = splitatspace cmds in
1644 let scan s fmt f =
1645 try Scanf.sscanf s fmt f
1646 with exn ->
1647 dolog "error processing '%S': %s" cmds (exntos exn);
1648 exit 1
1650 let addoutline outline =
1651 match state.currently with
1652 | Outlining outlines ->
1653 state.currently <- Outlining (outline :: outlines)
1654 | Idle -> state.currently <- Outlining [outline]
1655 | currently ->
1656 dolog "invalid outlining state";
1657 logcurrently currently
1659 match cl with
1660 | "clear" :: [] ->
1661 state.uioh#infochanged Pdim;
1662 state.pdims <- [];
1664 | "clearrects" :: [] ->
1665 state.rects <- state.rects1;
1666 G.postRedisplay "clearrects";
1668 | "continue" :: args :: [] ->
1669 let n = scan args "%u" (fun n -> n) in
1670 state.pagecount <- n;
1671 begin match state.currently with
1672 | Outlining l ->
1673 state.currently <- Idle;
1674 state.outlines <- Array.of_list (List.rev l)
1675 | _ -> ()
1676 end;
1678 let cur, cmds = state.geomcmds in
1679 if emptystr cur
1680 then failwith "umpossible";
1682 begin match List.rev cmds with
1683 | [] ->
1684 state.geomcmds <- E.s, [];
1685 state.throttle <- None;
1686 represent ();
1687 | (s, f) :: rest ->
1688 f ();
1689 state.geomcmds <- s, List.rev rest;
1690 end;
1691 if conf.maxwait = None && not !wtmode
1692 then G.postRedisplay "continue";
1694 | "title" :: args :: [] ->
1695 conf.title <- args;
1696 Wsi.settitle args
1698 | "msg" :: args :: [] ->
1699 showtext ' ' args
1701 | "vmsg" :: args :: [] ->
1702 if conf.verbose
1703 then showtext ' ' args
1705 | "emsg" :: args :: [] ->
1706 Buffer.add_string state.errmsgs args;
1707 state.newerrmsgs <- true;
1708 G.postRedisplay "error message"
1710 | "progress" :: args :: [] ->
1711 let progress, text =
1712 scan args "%f %n"
1713 (fun f pos ->
1714 f, String.sub args pos (String.length args - pos))
1716 state.text <- text;
1717 state.progress <- progress;
1718 G.postRedisplay "progress"
1720 | "firstmatch" :: args :: [] ->
1721 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1722 scan args "%u %d %f %f %f %f %f %f %f %f"
1723 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1724 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1726 let xoff = if conf.leftscroll then float conf.scrollbw else 0.0 in
1727 let x0 = x0 +. xoff
1728 and x1 = x1 +. xoff
1729 and x2 = x2 +. xoff
1730 and x3 = x3 +. xoff in
1731 let y = (getpagey pageno) + truncate y0 in
1732 addnav ();
1733 gotoy y;
1734 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
1736 | "match" :: args :: [] ->
1737 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1738 scan args "%u %d %f %f %f %f %f %f %f %f"
1739 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1740 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1742 let xoff = if conf.leftscroll then float conf.scrollbw else 0.0 in
1743 let x0 = x0 +. xoff
1744 and x1 = x1 +. xoff
1745 and x2 = x2 +. xoff
1746 and x3 = x3 +. xoff in
1747 state.rects1 <-
1748 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1750 | "page" :: args :: [] ->
1751 let pageopaques, t = scan args "%s %f" (fun p t -> p, t) in
1752 let pageopaque = ~< pageopaques in
1753 begin match state.currently with
1754 | Loading (l, gen) ->
1755 vlog "page %d took %f sec" l.pageno t;
1756 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1757 begin match state.throttle with
1758 | None ->
1759 let preloadedpages =
1760 if conf.preload
1761 then preloadlayout state.y
1762 else state.layout
1764 let evict () =
1765 let set =
1766 List.fold_left (fun s l -> IntSet.add l.pageno s)
1767 IntSet.empty preloadedpages
1769 let evictedpages =
1770 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1771 if not (IntSet.mem pageno set)
1772 then (
1773 wcmd "freepage %s" (~> opaque);
1774 key :: accu
1776 else accu
1777 ) state.pagemap []
1779 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1781 evict ();
1782 state.currently <- Idle;
1783 if gen = state.gen
1784 then (
1785 tilepage l.pageno pageopaque state.layout;
1786 load state.layout;
1787 load preloadedpages;
1788 if pagevisible state.layout l.pageno
1789 && layoutready state.layout
1790 then G.postRedisplay "page";
1793 | Some (layout, _, _) ->
1794 state.currently <- Idle;
1795 tilepage l.pageno pageopaque layout;
1796 load state.layout
1797 end;
1799 | _ ->
1800 dolog "Inconsistent loading state";
1801 logcurrently state.currently;
1802 exit 1
1805 | "tile" :: args :: [] ->
1806 let (x, y, opaques, size, t) =
1807 scan args "%u %u %s %u %f"
1808 (fun x y p size t -> (x, y, p, size, t))
1810 let opaque = ~< opaques in
1811 begin match state.currently with
1812 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1813 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1815 unmappbo opaque;
1816 if tilew != conf.tilew || tileh != conf.tileh
1817 then (
1818 wcmd "freetile %s" (~> opaque);
1819 state.currently <- Idle;
1820 load state.layout;
1822 else (
1823 puttileopaque l col row gen cs angle opaque size t;
1824 state.memused <- state.memused + size;
1825 state.uioh#infochanged Memused;
1826 gctiles ();
1827 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1828 opaque, size) state.tilelru;
1830 let layout =
1831 match state.throttle with
1832 | None -> state.layout
1833 | Some (layout, _, _) -> layout
1836 state.currently <- Idle;
1837 if gen = state.gen
1838 && conf.colorspace = cs
1839 && conf.angle = angle
1840 && tilevisible layout l.pageno x y
1841 then conttiling l.pageno pageopaque;
1843 begin match state.throttle with
1844 | None ->
1845 preload state.layout;
1846 if gen = state.gen
1847 && conf.colorspace = cs
1848 && conf.angle = angle
1849 && tilevisible state.layout l.pageno x y
1850 && (not !wtmode || layoutready state.layout)
1851 then G.postRedisplay "tile nothrottle";
1853 | Some (layout, y, _) ->
1854 let ready = layoutready layout in
1855 if ready
1856 then (
1857 state.y <- y;
1858 state.layout <- layout;
1859 state.throttle <- None;
1860 G.postRedisplay "throttle";
1862 else load layout;
1863 end;
1866 | _ ->
1867 dolog "Inconsistent tiling state";
1868 logcurrently state.currently;
1869 exit 1
1872 | "pdim" :: args :: [] ->
1873 let (n, w, h, _) as pdim =
1874 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1876 let pdim =
1877 match conf.fitmodel, conf.columns with
1878 | (FitPage | FitProportional), Csplit _ -> (n, w, h, 0)
1879 | _ -> pdim
1881 state.uioh#infochanged Pdim;
1882 state.pdims <- pdim :: state.pdims
1884 | "o" :: args :: [] ->
1885 let (l, n, t, h, pos) =
1886 scan args "%u %u %d %u %n"
1887 (fun l n t h pos -> l, n, t, h, pos)
1889 let s = String.sub args pos (String.length args - pos) in
1890 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
1892 | "ou" :: args :: [] ->
1893 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
1894 let s = String.sub args pos len in
1895 let pos2 = pos + len + 1 in
1896 let uri = String.sub args pos2 (String.length args - pos2) in
1897 addoutline (s, l, Ouri uri)
1899 | "on" :: args :: [] ->
1900 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
1901 let s = String.sub args pos (String.length args - pos) in
1902 addoutline (s, l, Onone)
1904 | "a" :: args :: [] ->
1905 let (n, l, t) =
1906 scan args "%u %d %d" (fun n l t -> n, l, t)
1908 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
1910 | "info" :: args :: [] ->
1911 state.docinfo <- (1, args) :: state.docinfo
1913 | "infoend" :: [] ->
1914 state.uioh#infochanged Docinfo;
1915 state.docinfo <- List.rev state.docinfo
1917 | _ ->
1918 error "unknown cmd `%S'" cmds
1921 let onhist cb =
1922 let rc = cb.rc in
1923 let action = function
1924 | HCprev -> cbget cb ~-1
1925 | HCnext -> cbget cb 1
1926 | HCfirst -> cbget cb ~-(cb.rc)
1927 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1928 and cancel () = cb.rc <- rc
1929 in (action, cancel)
1932 let search pattern forward =
1933 match conf.columns with
1934 | Csplit _ ->
1935 showtext '!' "searching does not work properly in split columns mode"
1936 | _ ->
1937 if nonemptystr pattern
1938 then
1939 let pn, py =
1940 match state.layout with
1941 | [] -> 0, 0
1942 | l :: _ ->
1943 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1945 wcmd "search %d %d %d %d,%s\000"
1946 (btod conf.icase) pn py (btod forward) pattern;
1949 let intentry text key =
1950 let c =
1951 if key >= 32 && key < 127
1952 then Char.chr key
1953 else '\000'
1955 match c with
1956 | '0' .. '9' ->
1957 let text = addchar text c in
1958 TEcont text
1960 | _ ->
1961 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1962 TEcont text
1965 let linknentry text key =
1966 let c =
1967 if key >= 32 && key < 127
1968 then Char.chr key
1969 else '\000'
1971 match c with
1972 | 'a' .. 'z' ->
1973 let text = addchar text c in
1974 TEcont text
1976 | _ ->
1977 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1978 TEcont text
1981 let linkndone f s =
1982 if nonemptystr s
1983 then (
1984 let n =
1985 let l = String.length s in
1986 let rec loop pos n = if pos = l then n else
1987 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
1988 loop (pos+1) (n*26 + m)
1989 in loop 0 0
1991 let rec loop n = function
1992 | [] -> ()
1993 | l :: rest ->
1994 match getopaque l.pageno with
1995 | None -> loop n rest
1996 | Some opaque ->
1997 let m = getlinkcount opaque in
1998 if n < m
1999 then (
2000 let under = getlink opaque n in
2001 f under
2003 else loop (n-m) rest
2005 loop n state.layout;
2009 let textentry text key =
2010 if key land 0xff00 = 0xff00
2011 then TEcont text
2012 else TEcont (text ^ toutf8 key)
2015 let reqlayout angle fitmodel =
2016 match state.throttle with
2017 | None ->
2018 if nogeomcmds state.geomcmds
2019 then state.anchor <- getanchor ();
2020 conf.angle <- angle mod 360;
2021 if conf.angle != 0
2022 then (
2023 match state.mode with
2024 | LinkNav _ -> state.mode <- View
2025 | _ -> ()
2027 conf.fitmodel <- fitmodel;
2028 invalidate "reqlayout"
2029 (fun () ->
2030 wcmd "reqlayout %d %d %d"
2031 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
2033 | _ -> ()
2036 let settrim trimmargins trimfuzz =
2037 if nogeomcmds state.geomcmds
2038 then state.anchor <- getanchor ();
2039 conf.trimmargins <- trimmargins;
2040 conf.trimfuzz <- trimfuzz;
2041 let x0, y0, x1, y1 = trimfuzz in
2042 invalidate "settrim"
2043 (fun () ->
2044 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2045 flushpages ();
2048 let setzoom zoom =
2049 match state.throttle with
2050 | None ->
2051 let zoom = max 0.0001 zoom in
2052 if zoom <> conf.zoom
2053 then (
2054 state.prevzoom <- (conf.zoom, state.x);
2055 conf.zoom <- zoom;
2056 reshape state.winw state.winh;
2057 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2060 | Some (layout, y, started) ->
2061 let time =
2062 match conf.maxwait with
2063 | None -> 0.0
2064 | Some t -> t
2066 let dt = now () -. started in
2067 if dt > time
2068 then (
2069 state.y <- y;
2070 load layout;
2074 let setcolumns mode columns coverA coverB =
2075 state.prevcolumns <- Some (conf.columns, conf.zoom);
2076 if columns < 0
2077 then (
2078 if isbirdseye mode
2079 then showtext '!' "split mode doesn't work in bird's eye"
2080 else (
2081 conf.columns <- Csplit (-columns, E.a);
2082 state.x <- 0;
2083 conf.zoom <- 1.0;
2086 else (
2087 if columns < 2
2088 then (
2089 conf.columns <- Csingle E.a;
2090 state.x <- 0;
2091 setzoom 1.0;
2093 else (
2094 conf.columns <- Cmulti ((columns, coverA, coverB), E.a);
2095 conf.zoom <- 1.0;
2098 reshape state.winw state.winh;
2101 let resetmstate () =
2102 state.mstate <- Mnone;
2103 Wsi.setcursor Wsi.CURSOR_INHERIT;
2106 let enterbirdseye () =
2107 let zoom = float conf.thumbw /. float state.winw in
2108 let birdseyepageno =
2109 let cy = state.winh / 2 in
2110 let fold = function
2111 | [] -> 0
2112 | l :: rest ->
2113 let rec fold best = function
2114 | [] -> best.pageno
2115 | l :: rest ->
2116 let d = cy - (l.pagedispy + l.pagevh/2)
2117 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2118 if abs d < abs dbest
2119 then fold l rest
2120 else best.pageno
2121 in fold l rest
2123 fold state.layout
2125 state.mode <- Birdseye (
2126 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2128 resetmstate ();
2129 conf.zoom <- zoom;
2130 conf.presentation <- false;
2131 conf.interpagespace <- 10;
2132 conf.hlinks <- false;
2133 conf.fitmodel <- FitProportional;
2134 state.x <- 0;
2135 conf.maxwait <- None;
2136 conf.columns <- (
2137 match conf.beyecolumns with
2138 | Some c ->
2139 conf.zoom <- 1.0;
2140 Cmulti ((c, 0, 0), E.a)
2141 | None -> Csingle E.a
2143 if conf.verbose
2144 then
2145 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2146 (100.0*.zoom)
2147 else
2148 state.text <- E.s
2150 reshape state.winw state.winh;
2153 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2154 state.mode <- View;
2155 conf.zoom <- c.zoom;
2156 conf.presentation <- c.presentation;
2157 conf.interpagespace <- c.interpagespace;
2158 conf.maxwait <- c.maxwait;
2159 conf.hlinks <- c.hlinks;
2160 conf.fitmodel <- c.fitmodel;
2161 conf.beyecolumns <- (
2162 match conf.columns with
2163 | Cmulti ((c, _, _), _) -> Some c
2164 | Csingle _ -> None
2165 | Csplit _ -> failwith "leaving bird's eye split mode"
2167 conf.columns <- (
2168 match c.columns with
2169 | Cmulti (c, _) -> Cmulti (c, E.a)
2170 | Csingle _ -> Csingle E.a
2171 | Csplit (c, _) -> Csplit (c, E.a)
2173 if conf.verbose
2174 then
2175 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2176 (100.0*.conf.zoom)
2178 reshape state.winw state.winh;
2179 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2180 state.x <- leftx;
2183 let togglebirdseye () =
2184 match state.mode with
2185 | Birdseye vals -> leavebirdseye vals true
2186 | View -> enterbirdseye ()
2187 | _ -> ()
2190 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2191 let pageno = max 0 (pageno - incr) in
2192 let rec loop = function
2193 | [] -> gotopage1 pageno 0
2194 | l :: _ when l.pageno = pageno ->
2195 if l.pagedispy >= 0 && l.pagey = 0
2196 then G.postRedisplay "upbirdseye"
2197 else gotopage1 pageno 0
2198 | _ :: rest -> loop rest
2200 loop state.layout;
2201 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2204 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2205 let pageno = min (state.pagecount - 1) (pageno + incr) in
2206 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2207 let rec loop = function
2208 | [] ->
2209 let y, h = getpageyh pageno in
2210 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2211 gotoy (clamp dy)
2212 | l :: _ when l.pageno = pageno ->
2213 if l.pagevh != l.pageh
2214 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2215 else G.postRedisplay "downbirdseye"
2216 | _ :: rest -> loop rest
2218 loop state.layout
2221 let boundastep h step =
2222 if step < 0
2223 then bound step ~-h 0
2224 else bound step 0 h
2227 let optentry mode _ key =
2228 let btos b = if b then "on" else "off" in
2229 if key >= 32 && key < 127
2230 then
2231 let c = Char.chr key in
2232 match c with
2233 | 's' ->
2234 let ondone s =
2235 try conf.scrollstep <- int_of_string s with exc ->
2236 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2238 TEswitch ("scroll step: ", E.s, None, intentry, ondone, true)
2240 | 'A' ->
2241 let ondone s =
2243 conf.autoscrollstep <- boundastep state.winh (int_of_string s);
2244 if state.autoscroll <> None
2245 then state.autoscroll <- Some conf.autoscrollstep
2246 with exc ->
2247 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2249 TEswitch ("auto scroll step: ", E.s, None, intentry, ondone, true)
2251 | 'C' ->
2252 let ondone s =
2254 let n, a, b = multicolumns_of_string s in
2255 setcolumns mode n a b;
2256 with exc ->
2257 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
2259 TEswitch ("columns: ", E.s, None, textentry, ondone, true)
2261 | 'Z' ->
2262 let ondone s =
2264 let zoom = float (int_of_string s) /. 100.0 in
2265 setzoom zoom
2266 with exc ->
2267 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2269 TEswitch ("zoom: ", E.s, None, intentry, ondone, true)
2271 | 't' ->
2272 let ondone s =
2274 conf.thumbw <- bound (int_of_string s) 2 4096;
2275 state.text <-
2276 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2277 begin match mode with
2278 | Birdseye beye ->
2279 leavebirdseye beye false;
2280 enterbirdseye ();
2281 | _ -> ();
2283 with exc ->
2284 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2286 TEswitch ("thumbnail width: ", E.s, None, intentry, ondone, true)
2288 | 'R' ->
2289 let ondone s =
2290 match try
2291 Some (int_of_string s)
2292 with exc ->
2293 state.text <- Printf.sprintf "bad integer `%s': %s"
2294 s (exntos exc);
2295 None
2296 with
2297 | Some angle -> reqlayout angle conf.fitmodel
2298 | None -> ()
2300 TEswitch ("rotation: ", E.s, None, intentry, ondone, true)
2302 | 'i' ->
2303 conf.icase <- not conf.icase;
2304 TEdone ("case insensitive search " ^ (btos conf.icase))
2306 | 'p' ->
2307 conf.preload <- not conf.preload;
2308 gotoy state.y;
2309 TEdone ("preload " ^ (btos conf.preload))
2311 | 'v' ->
2312 conf.verbose <- not conf.verbose;
2313 TEdone ("verbose " ^ (btos conf.verbose))
2315 | 'd' ->
2316 conf.debug <- not conf.debug;
2317 TEdone ("debug " ^ (btos conf.debug))
2319 | 'h' ->
2320 conf.maxhfit <- not conf.maxhfit;
2321 state.maxy <- calcheight ();
2322 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2324 | 'c' ->
2325 conf.crophack <- not conf.crophack;
2326 TEdone ("crophack " ^ btos conf.crophack)
2328 | 'a' ->
2329 let s =
2330 match conf.maxwait with
2331 | None ->
2332 conf.maxwait <- Some infinity;
2333 "always wait for page to complete"
2334 | Some _ ->
2335 conf.maxwait <- None;
2336 "show placeholder if page is not ready"
2338 TEdone s
2340 | 'f' ->
2341 conf.underinfo <- not conf.underinfo;
2342 TEdone ("underinfo " ^ btos conf.underinfo)
2344 | 'P' ->
2345 conf.savebmarks <- not conf.savebmarks;
2346 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2348 | 'S' ->
2349 let ondone s =
2351 let pageno, py =
2352 match state.layout with
2353 | [] -> 0, 0
2354 | l :: _ ->
2355 l.pageno, l.pagey
2357 conf.interpagespace <- int_of_string s;
2358 docolumns conf.columns;
2359 state.maxy <- calcheight ();
2360 let y = getpagey pageno in
2361 gotoy (y + py)
2362 with exc ->
2363 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2365 TEswitch ("vertical margin: ", E.s, None, intentry, ondone, true)
2367 | 'l' ->
2368 let fm =
2369 match conf.fitmodel with
2370 | FitProportional -> FitWidth
2371 | _ -> FitProportional
2373 reqlayout conf.angle fm;
2374 TEdone ("proportional display " ^ btos (fm == FitProportional))
2376 | 'T' ->
2377 settrim (not conf.trimmargins) conf.trimfuzz;
2378 TEdone ("trim margins " ^ btos conf.trimmargins)
2380 | 'I' ->
2381 conf.invert <- not conf.invert;
2382 TEdone ("invert colors " ^ btos conf.invert)
2384 | 'x' ->
2385 let ondone s =
2386 cbput state.hists.sel s;
2387 conf.selcmd <- s;
2389 TEswitch ("selection command: ", E.s, Some (onhist state.hists.sel),
2390 textentry, ondone, true)
2392 | 'M' ->
2393 if conf.pax == None
2394 then conf.pax <- Some (ref (0.0, 0, 0))
2395 else conf.pax <- None;
2396 TEdone ("PAX " ^ btos (conf.pax != None))
2398 | _ ->
2399 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2400 TEstop
2401 else
2402 TEcont state.text
2405 class type lvsource = object
2406 method getitemcount : int
2407 method getitem : int -> (string * int)
2408 method hasaction : int -> bool
2409 method exit :
2410 uioh:uioh ->
2411 cancel:bool ->
2412 active:int ->
2413 first:int ->
2414 pan:int ->
2415 uioh option
2416 method getactive : int
2417 method getfirst : int
2418 method getpan : int
2419 method getminfo : (int * int) array
2420 end;;
2422 class virtual lvsourcebase = object
2423 val mutable m_active = 0
2424 val mutable m_first = 0
2425 val mutable m_pan = 0
2426 method getactive = m_active
2427 method getfirst = m_first
2428 method getpan = m_pan
2429 method getminfo : (int * int) array = E.a
2430 end;;
2432 let withoutlastutf8 s =
2433 let len = String.length s in
2434 if len = 0
2435 then s
2436 else
2437 let rec find pos =
2438 if pos = 0
2439 then pos
2440 else
2441 let b = Char.code s.[pos] in
2442 if b land 0b11000000 = 0b11000000
2443 then pos
2444 else find (pos-1)
2446 let first =
2447 if Char.code s.[len-1] land 0x80 = 0
2448 then len-1
2449 else find (len-1)
2451 String.sub s 0 first;
2454 let textentrykeyboard
2455 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2456 let key =
2457 if key >= 0xffb0 && key <= 0xffb9
2458 then key - 0xffb0 + 48 else key
2460 let enttext te =
2461 state.mode <- Textentry (te, onleave);
2462 state.text <- E.s;
2463 enttext ();
2464 G.postRedisplay "textentrykeyboard enttext";
2466 let histaction cmd =
2467 match opthist with
2468 | None -> ()
2469 | Some (action, _) ->
2470 state.mode <- Textentry (
2471 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
2473 G.postRedisplay "textentry histaction"
2475 match key with
2476 | 0xff08 -> (* backspace *)
2477 if emptystr text && cancelonempty
2478 then (
2479 onleave Cancel;
2480 G.postRedisplay "textentrykeyboard after cancel";
2482 else
2483 let s = withoutlastutf8 text in
2484 enttext (c, s, opthist, onkey, ondone, cancelonempty)
2486 | 0xff0d | 0xff8d -> (* (kp) enter *)
2487 ondone text;
2488 onleave Confirm;
2489 G.postRedisplay "textentrykeyboard after confirm"
2491 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
2492 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
2493 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
2494 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
2496 | 0xff1b -> (* escape*)
2497 if emptystr text
2498 then (
2499 begin match opthist with
2500 | None -> ()
2501 | Some (_, onhistcancel) -> onhistcancel ()
2502 end;
2503 onleave Cancel;
2504 state.text <- E.s;
2505 G.postRedisplay "textentrykeyboard after cancel2"
2507 else (
2508 enttext (c, E.s, opthist, onkey, ondone, cancelonempty)
2511 | 0xff9f | 0xffff -> () (* delete *)
2513 | _ when key != 0
2514 && key land 0xff00 != 0xff00 (* keyboard *)
2515 && key land 0xfe00 != 0xfe00 (* xkb *)
2516 && key land 0xfd00 != 0xfd00 (* 3270 *)
2518 begin match onkey text key with
2519 | TEdone text ->
2520 ondone text;
2521 onleave Confirm;
2522 G.postRedisplay "textentrykeyboard after confirm2";
2524 | TEcont text ->
2525 enttext (c, text, opthist, onkey, ondone, cancelonempty);
2527 | TEstop ->
2528 onleave Cancel;
2529 G.postRedisplay "textentrykeyboard after cancel3"
2531 | TEswitch te ->
2532 state.mode <- Textentry (te, onleave);
2533 G.postRedisplay "textentrykeyboard switch";
2534 end;
2536 | _ ->
2537 vlog "unhandled key %s" (Wsi.keyname key)
2540 let firstof first active =
2541 if first > active || abs (first - active) > fstate.maxrows - 1
2542 then max 0 (active - (fstate.maxrows/2))
2543 else first
2546 let calcfirst first active =
2547 if active > first
2548 then
2549 let rows = active - first in
2550 if rows > fstate.maxrows then active - fstate.maxrows else first
2551 else active
2554 let scrollph y maxy =
2555 let sh = float (maxy + state.winh) /. float state.winh in
2556 let sh = float state.winh /. sh in
2557 let sh = max sh (float conf.scrollh) in
2559 let percent = float y /. float maxy in
2560 let position = (float state.winh -. sh) *. percent in
2562 let position =
2563 if position +. sh > float state.winh
2564 then float state.winh -. sh
2565 else position
2567 position, sh;
2570 let coe s = (s :> uioh);;
2572 class listview ?(helpmode=false) ~(source:lvsource) ~trusted ~modehash =
2573 object (self)
2574 val m_pan = source#getpan
2575 val m_first = source#getfirst
2576 val m_active = source#getactive
2577 val m_qsearch = E.s
2578 val m_prev_uioh = state.uioh
2580 method private elemunder y =
2581 if y < 0
2582 then None
2583 else
2584 let n = y / (fstate.fontsize+1) in
2585 if m_first + n < source#getitemcount
2586 then (
2587 if source#hasaction (m_first + n)
2588 then Some (m_first + n)
2589 else None
2591 else None
2593 method display =
2594 Gl.enable `blend;
2595 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2596 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2597 filledrect 0. 0. (float state.winw) (float state.winh);
2598 GlDraw.color (1., 1., 1.);
2599 Gl.enable `texture_2d;
2600 let fs = fstate.fontsize in
2601 let nfs = fs + 1 in
2602 let ww = fstate.wwidth in
2603 let tabw = 17.0*.ww in
2604 let itemcount = source#getitemcount in
2605 let minfo = source#getminfo in
2606 let x0, x1 =
2607 if conf.leftscroll
2608 then float conf.scrollbw, float (state.winw - 1)
2609 else 0.0, float (state.winw - conf.scrollbw - 1)
2611 let rec loop row =
2612 if (row - m_first) > fstate.maxrows
2613 then ()
2614 else (
2615 if row >= 0 && row < itemcount
2616 then (
2617 let (s, level) = source#getitem row in
2618 let y = (row - m_first) * nfs in
2619 let x =
2620 (if conf.leftscroll then float (conf.scrollbw + 5) else 5.0)
2621 +. float (level + m_pan) *. ww
2623 if helpmode
2624 then GlDraw.color
2625 (let c = if row land 1 = 0 then 1.0 else 0.92 in (c,c,c));
2627 if row = m_active
2628 then (
2629 Gl.disable `texture_2d;
2630 let alpha = if source#hasaction row then 0.9 else 0.3 in
2631 GlDraw.color (1., 1., 1.) ~alpha;
2632 linerect (x0 +. 1.) (float (y + 1)) (x1) (float (y + fs + 3));
2633 Gl.enable `texture_2d;
2634 GlDraw.color (1., 1., 1.);
2636 let drawtabularstring s =
2637 let drawstr x s = drawstring1 fs (truncate (x +. x0)) (y+nfs) s in
2638 if trusted
2639 then
2640 let x = if helpmode && row > 0 then x +. ww else x in
2641 let tabpos = try String.index s '\t' with Not_found -> -1 in
2642 if tabpos > 0
2643 then
2644 let len = String.length s - tabpos - 1 in
2645 let s1 = String.sub s 0 tabpos
2646 and s2 = String.sub s (tabpos + 1) len in
2647 let nx = drawstr x s1 in
2648 let sw = nx -. x in
2649 let x = x +. (max tabw sw) in
2650 drawstr x s2
2651 else
2652 let len = String.length s - 2 in
2653 if len > 0 && s.[0] = '\xc2' && s.[1] = '\xb7'
2654 then
2655 let s = String.sub s 2 len in
2656 let x = if not helpmode then x +. ww else x in
2657 GlDraw.color (1.2, 1.2, 1.2);
2658 let vinc = drawstring1 (fs+fs/4)
2659 (truncate (x -. ww)) (y+nfs) s in
2660 GlDraw.color (1., 1., 1.);
2661 vinc +. (float fs *. 0.8)
2662 else
2663 drawstr x s
2664 else
2665 drawstr x s
2667 let _ = drawtabularstring s in
2668 loop (row+1)
2672 loop m_first;
2673 GlDraw.color (1.0, 1.0, 1.0) ~alpha:0.5;
2674 let rec loop row =
2675 if (row - m_first) > fstate.maxrows
2676 then ()
2677 else (
2678 if row >= 0 && row < itemcount
2679 then (
2680 let (s, level) = source#getitem row in
2681 let y = (row - m_first) * nfs in
2682 let x = 5.0 +. float (level + m_pan) *. ww in
2683 let (first, last) = minfo.(row) in
2684 let prefix = String.sub s 0 first in
2685 let suffix = String.sub s first (last - first) in
2686 let w1 = measurestr fstate.fontsize prefix in
2687 let w2 = measurestr fstate.fontsize suffix in
2688 let x0 = x +. w1
2689 and y0 = (float (y+2)) in
2690 let x1 = x0 +. w2
2691 and y1 = (float (y+fs+3)) in
2692 filledrect x0 y0 x1 y1;
2693 loop (row+1)
2697 Gl.disable `texture_2d;
2698 if Array.length minfo > 0 then loop m_first;
2699 Gl.disable `blend;
2701 method updownlevel incr =
2702 let len = source#getitemcount in
2703 let curlevel =
2704 if m_active >= 0 && m_active < len
2705 then snd (source#getitem m_active)
2706 else -1
2708 let rec flow i =
2709 if i = len then i-1 else if i = -1 then 0 else
2710 let _, l = source#getitem i in
2711 if l != curlevel then i else flow (i+incr)
2713 let active = flow m_active in
2714 let first = calcfirst m_first active in
2715 G.postRedisplay "outline updownlevel";
2716 {< m_active = active; m_first = first >}
2718 method private key1 key mask =
2719 let set1 active first qsearch =
2720 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2722 let search active pattern incr =
2723 let active = if active = -1 then m_first else active in
2724 let dosearch re =
2725 let rec loop n =
2726 if n >= 0 && n < source#getitemcount
2727 then (
2728 let s, _ = source#getitem n in
2730 (try ignore (Str.search_forward re s 0); true
2731 with Not_found -> false)
2732 then Some n
2733 else loop (n + incr)
2735 else None
2737 loop active
2740 let re = Str.regexp_case_fold pattern in
2741 dosearch re
2742 with Failure s ->
2743 state.text <- s;
2744 None
2746 let itemcount = source#getitemcount in
2747 let find start incr =
2748 let rec find i =
2749 if i = -1 || i = itemcount
2750 then -1
2751 else (
2752 if source#hasaction i
2753 then i
2754 else find (i + incr)
2757 find start
2759 let set active first =
2760 let first = bound first 0 (itemcount - fstate.maxrows) in
2761 state.text <- E.s;
2762 coe {< m_active = active; m_first = first; m_qsearch = E.s >}
2764 let navigate incr =
2765 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2766 let active, first =
2767 let incr1 = if incr > 0 then 1 else -1 in
2768 if isvisible m_first m_active
2769 then
2770 let next =
2771 let next = m_active + incr in
2772 let next =
2773 if next < 0 || next >= itemcount
2774 then -1
2775 else find next incr1
2777 if abs (m_active - next) > fstate.maxrows
2778 then -1
2779 else next
2781 if next = -1
2782 then
2783 let first = m_first + incr in
2784 let first = bound first 0 (itemcount - fstate.maxrows) in
2785 let next =
2786 let next = m_active + incr in
2787 let next = bound next 0 (itemcount - 1) in
2788 find next ~-incr1
2790 let active =
2791 if next = -1
2792 then m_active
2793 else (
2794 if isvisible first next
2795 then next
2796 else m_active
2799 active, first
2800 else
2801 let first = min next m_first in
2802 let first =
2803 if abs (next - first) > fstate.maxrows
2804 then first + incr
2805 else first
2807 next, first
2808 else
2809 let first = m_first + incr in
2810 let first = bound first 0 (itemcount - 1) in
2811 let active =
2812 let next = m_active + incr in
2813 let next = bound next 0 (itemcount - 1) in
2814 let next = find next incr1 in
2815 let active =
2816 if next = -1 || abs (m_active - first) > fstate.maxrows
2817 then (
2818 let active = if m_active = -1 then next else m_active in
2819 active
2821 else next
2823 if isvisible first active
2824 then active
2825 else -1
2827 active, first
2829 G.postRedisplay "listview navigate";
2830 set active first;
2832 match key with
2833 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
2834 let incr = if key = 0x72 then -1 else 1 in
2835 let active, first =
2836 match search (m_active + incr) m_qsearch incr with
2837 | None ->
2838 state.text <- m_qsearch ^ " [not found]";
2839 m_active, m_first
2840 | Some active ->
2841 state.text <- m_qsearch;
2842 active, firstof m_first active
2844 G.postRedisplay "listview ctrl-r/s";
2845 set1 active first m_qsearch;
2847 | 0xff63 when Wsi.withctrl mask -> (* ctrl-insert *)
2848 if m_active >= 0 && m_active < source#getitemcount
2849 then (
2850 let s, _ = source#getitem m_active in
2851 selstring s;
2853 coe self
2855 | 0xff08 -> (* backspace *)
2856 if emptystr m_qsearch
2857 then coe self
2858 else (
2859 let qsearch = withoutlastutf8 m_qsearch in
2860 if emptystr qsearch
2861 then (
2862 state.text <- E.s;
2863 G.postRedisplay "listview empty qsearch";
2864 set1 m_active m_first E.s;
2866 else
2867 let active, first =
2868 match search m_active qsearch ~-1 with
2869 | None ->
2870 state.text <- qsearch ^ " [not found]";
2871 m_active, m_first
2872 | Some active ->
2873 state.text <- qsearch;
2874 active, firstof m_first active
2876 G.postRedisplay "listview backspace qsearch";
2877 set1 active first qsearch
2880 | key when (key != 0 && key land 0xff00 != 0xff00) ->
2881 let pattern = m_qsearch ^ toutf8 key in
2882 let active, first =
2883 match search m_active pattern 1 with
2884 | None ->
2885 state.text <- pattern ^ " [not found]";
2886 m_active, m_first
2887 | Some active ->
2888 state.text <- pattern;
2889 active, firstof m_first active
2891 G.postRedisplay "listview qsearch add";
2892 set1 active first pattern;
2894 | 0xff1b -> (* escape *)
2895 state.text <- E.s;
2896 if emptystr m_qsearch
2897 then (
2898 G.postRedisplay "list view escape";
2899 begin
2900 match
2901 source#exit (coe self) true m_active m_first m_pan
2902 with
2903 | None -> m_prev_uioh
2904 | Some uioh -> uioh
2907 else (
2908 G.postRedisplay "list view kill qsearch";
2909 coe {< m_qsearch = E.s >}
2912 | 0xff0d | 0xff8d -> (* (kp) enter *)
2913 state.text <- E.s;
2914 let self = {< m_qsearch = E.s >} in
2915 let opt =
2916 G.postRedisplay "listview enter";
2917 if m_active >= 0 && m_active < source#getitemcount
2918 then (
2919 source#exit (coe self) false m_active m_first m_pan;
2921 else (
2922 source#exit (coe self) true m_active m_first m_pan;
2925 begin match opt with
2926 | None -> m_prev_uioh
2927 | Some uioh -> uioh
2930 | 0xff9f | 0xffff -> (* (kp) delete *)
2931 coe self
2933 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
2934 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
2935 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
2936 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
2938 | 0xff53 | 0xff98 -> (* (kp) right *)
2939 state.text <- E.s;
2940 G.postRedisplay "listview right";
2941 coe {< m_pan = m_pan - 1 >}
2943 | 0xff51 | 0xff96 -> (* (kp) left *)
2944 state.text <- E.s;
2945 G.postRedisplay "listview left";
2946 coe {< m_pan = m_pan + 1 >}
2948 | 0xff50 | 0xff95 -> (* (kp) home *)
2949 let active = find 0 1 in
2950 G.postRedisplay "listview home";
2951 set active 0;
2953 | 0xff57 | 0xff9c -> (* (kp) end *)
2954 let first = max 0 (itemcount - fstate.maxrows) in
2955 let active = find (itemcount - 1) ~-1 in
2956 G.postRedisplay "listview end";
2957 set active first;
2959 | key when (key = 0 || key land 0xff00 = 0xff00) ->
2960 coe self
2962 | _ ->
2963 dolog "listview unknown key %#x" key; coe self
2965 method key key mask =
2966 match state.mode with
2967 | Textentry te -> textentrykeyboard key mask te; coe self
2968 | _ -> self#key1 key mask
2970 method button button down x y _ =
2971 let opt =
2972 match button with
2973 | 1 when x > state.winw - conf.scrollbw ->
2974 G.postRedisplay "listview scroll";
2975 if down
2976 then
2977 let _, position, sh = self#scrollph in
2978 if y > truncate position && y < truncate (position +. sh)
2979 then (
2980 state.mstate <- Mscrolly;
2981 Some (coe self)
2983 else
2984 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
2985 let first = truncate (s *. float source#getitemcount) in
2986 let first = min source#getitemcount first in
2987 Some (coe {< m_first = first; m_active = first >})
2988 else (
2989 state.mstate <- Mnone;
2990 Some (coe self);
2992 | 1 when not down ->
2993 begin match self#elemunder y with
2994 | Some n ->
2995 G.postRedisplay "listview click";
2996 source#exit (coe {< m_active = n >}) false n m_first m_pan
2997 | _ ->
2998 Some (coe self)
3000 | n when (n == 4 || n == 5) && not down ->
3001 let len = source#getitemcount in
3002 let first =
3003 if n = 5 && m_first + fstate.maxrows >= len
3004 then
3005 m_first
3006 else
3007 let first = m_first + (if n == 4 then -1 else 1) in
3008 bound first 0 (len - 1)
3010 G.postRedisplay "listview wheel";
3011 Some (coe {< m_first = first >})
3012 | n when (n = 6 || n = 7) && not down ->
3013 let inc = if n = 7 then -1 else 1 in
3014 G.postRedisplay "listview hwheel";
3015 Some (coe {< m_pan = m_pan + inc >})
3016 | _ ->
3017 Some (coe self)
3019 match opt with
3020 | None -> m_prev_uioh
3021 | Some uioh -> uioh
3023 method multiclick _ x y = self#button 1 true x y
3025 method motion _ y =
3026 match state.mstate with
3027 | Mscrolly ->
3028 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3029 let first = truncate (s *. float source#getitemcount) in
3030 let first = min source#getitemcount first in
3031 G.postRedisplay "listview motion";
3032 coe {< m_first = first; m_active = first >}
3033 | _ -> coe self
3035 method pmotion x y =
3036 if x < state.winw - conf.scrollbw
3037 then
3038 let n =
3039 match self#elemunder y with
3040 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3041 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3043 let o =
3044 if n != m_active
3045 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3046 else self
3048 coe o
3049 else (
3050 Wsi.setcursor Wsi.CURSOR_INHERIT;
3051 coe self
3054 method infochanged _ = ()
3056 method scrollpw = (0, 0.0, 0.0)
3057 method scrollph =
3058 let nfs = fstate.fontsize + 1 in
3059 let y = m_first * nfs in
3060 let itemcount = source#getitemcount in
3061 let maxi = max 0 (itemcount - fstate.maxrows) in
3062 let maxy = maxi * nfs in
3063 let p, h = scrollph y maxy in
3064 conf.scrollbw, p, h
3066 method modehash = modehash
3067 method eformsgs = false
3068 end;;
3070 class outlinelistview ~source =
3071 let settext autonarrow s =
3072 if autonarrow
3073 then
3074 let ss = source#statestr in
3075 state.text <-
3076 if emptystr ss
3077 then "[" ^ s ^ "]"
3078 else "{" ^ ss ^ "} [" ^ s ^ "]"
3079 else state.text <- s
3081 object (self)
3082 inherit listview
3083 ~helpmode:false
3084 ~source:(source :> lvsource)
3085 ~trusted:false
3086 ~modehash:(findkeyhash conf "outline")
3087 as super
3089 val m_autonarrow = false
3091 method key key mask =
3092 let maxrows =
3093 if emptystr state.text
3094 then fstate.maxrows
3095 else fstate.maxrows - 2
3097 let calcfirst first active =
3098 if active > first
3099 then
3100 let rows = active - first in
3101 if rows > maxrows then active - maxrows else first
3102 else active
3104 let navigate incr =
3105 let active = m_active + incr in
3106 let active = bound active 0 (source#getitemcount - 1) in
3107 let first = calcfirst m_first active in
3108 G.postRedisplay "outline navigate";
3109 coe {< m_active = active; m_first = first >}
3111 let navscroll first =
3112 let active =
3113 let dist = m_active - first in
3114 if dist < 0
3115 then first
3116 else (
3117 if dist < maxrows
3118 then m_active
3119 else first + maxrows
3122 G.postRedisplay "outline navscroll";
3123 coe {< m_first = first; m_active = active >}
3125 let ctrl = Wsi.withctrl mask in
3126 match key with
3127 | 97 when ctrl -> (* ctrl-a *)
3128 let text =
3129 if m_autonarrow
3130 then (source#denarrow; E.s)
3131 else (
3132 let pattern = source#renarrow in
3133 if nonemptystr m_qsearch
3134 then (source#narrow m_qsearch; m_qsearch)
3135 else pattern
3138 settext (not m_autonarrow) text;
3139 G.postRedisplay "toggle auto narrowing";
3140 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
3142 | 47 when emptystr m_qsearch && not m_autonarrow -> (* / *)
3143 settext true E.s;
3144 G.postRedisplay "toggle auto narrowing";
3145 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
3147 | 110 when ctrl -> (* ctrl-n *)
3148 source#narrow m_qsearch;
3149 if not m_autonarrow
3150 then source#add_narrow_pattern m_qsearch;
3151 G.postRedisplay "outline ctrl-n";
3152 coe {< m_first = 0; m_active = 0 >}
3154 | 83 when ctrl -> (* ctrl-S *)
3155 let active = source#calcactive (getanchor ()) in
3156 let first = firstof m_first active in
3157 G.postRedisplay "outline ctrl-s";
3158 coe {< m_first = first; m_active = active >}
3160 | 117 when ctrl -> (* ctrl-u *)
3161 G.postRedisplay "outline ctrl-u";
3162 if m_autonarrow && nonemptystr m_qsearch
3163 then (
3164 ignore (source#renarrow);
3165 settext m_autonarrow E.s;
3166 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3168 else (
3169 source#del_narrow_pattern;
3170 let pattern = source#renarrow in
3171 let text =
3172 if emptystr pattern then E.s else "Narrowed to " ^ pattern
3174 settext m_autonarrow text;
3175 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3178 | 108 when ctrl -> (* ctrl-l *)
3179 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3180 G.postRedisplay "outline ctrl-l";
3181 coe {< m_first = first >}
3183 | 0xff09 when m_autonarrow -> (* tab *)
3184 if nonemptystr m_qsearch
3185 then (
3186 G.postRedisplay "outline list view tab";
3187 source#add_narrow_pattern m_qsearch;
3188 settext true E.s;
3189 coe {< m_qsearch = E.s >}
3191 else coe self
3193 | 0xff1b when m_autonarrow -> (* escape *)
3194 if nonemptystr m_qsearch
3195 then source#add_narrow_pattern m_qsearch;
3196 super#key key mask
3198 | 0xff0d | 0xff8d when m_autonarrow -> (* (kp) enter *)
3199 if nonemptystr m_qsearch
3200 then source#add_narrow_pattern m_qsearch;
3201 super#key key mask
3203 | key when m_autonarrow && (key != 0 && key land 0xff00 != 0xff00) ->
3204 let pattern = m_qsearch ^ toutf8 key in
3205 G.postRedisplay "outlinelistview autonarrow add";
3206 source#narrow pattern;
3207 settext true pattern;
3208 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3210 | key when m_autonarrow && key = 0xff08 -> (* backspace *)
3211 if emptystr m_qsearch
3212 then coe self
3213 else
3214 let pattern = withoutlastutf8 m_qsearch in
3215 G.postRedisplay "outlinelistview autonarrow backspace";
3216 ignore (source#renarrow);
3217 source#narrow pattern;
3218 settext true pattern;
3219 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3221 | 0xff9f | 0xffff -> (* (kp) delete *)
3222 source#remove m_active;
3223 G.postRedisplay "outline delete";
3224 let active = max 0 (m_active-1) in
3225 coe {< m_first = firstof m_first active;
3226 m_active = active >}
3228 | 0xff52 | 0xff97 when ctrl -> (* ctrl-(kp) up *)
3229 navscroll (max 0 (m_first - 1))
3231 | 0xff54 | 0xff99 when ctrl -> (* ctrl-(kp) down *)
3232 navscroll (min (source#getitemcount - 1) (m_first + 1))
3234 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3235 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3236 | 0xff55 | 0xff9a -> (* (kp) prior *)
3237 navigate ~-(fstate.maxrows)
3238 | 0xff56 | 0xff9b -> (* (kp) next *)
3239 navigate fstate.maxrows
3241 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
3242 let o =
3243 if ctrl
3244 then (
3245 G.postRedisplay "outline ctrl right";
3246 {< m_pan = m_pan + 1 >}
3248 else self#updownlevel 1
3250 coe o
3252 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
3253 let o =
3254 if ctrl
3255 then (
3256 G.postRedisplay "outline ctrl left";
3257 {< m_pan = m_pan - 1 >}
3259 else self#updownlevel ~-1
3261 coe o
3263 | 0xff50 | 0xff95 -> (* (kp) home *)
3264 G.postRedisplay "outline home";
3265 coe {< m_first = 0; m_active = 0 >}
3267 | 0xff57 | 0xff9c -> (* (kp) end *)
3268 let active = source#getitemcount - 1 in
3269 let first = max 0 (active - fstate.maxrows) in
3270 G.postRedisplay "outline end";
3271 coe {< m_active = active; m_first = first >}
3273 | _ -> super#key key mask
3276 let gotounder under =
3277 let getpath filename =
3278 let path =
3279 if nonemptystr filename
3280 then
3281 if Filename.is_relative filename
3282 then
3283 let dir = Filename.dirname state.path in
3284 let dir =
3285 if Filename.is_implicit dir
3286 then Filename.concat (Sys.getcwd ()) dir
3287 else dir
3289 Filename.concat dir filename
3290 else filename
3291 else E.s
3293 if Sys.file_exists path
3294 then path
3295 else E.s
3297 match under with
3298 | Ulinkgoto (pageno, top) ->
3299 if pageno >= 0
3300 then (
3301 addnav ();
3302 gotopage1 pageno top;
3305 | Ulinkuri s ->
3306 gotouri s
3308 | Uremote (filename, pageno) ->
3309 let path = getpath filename in
3310 if nonemptystr path
3311 then (
3312 if conf.riani
3313 then
3314 let command = Printf.sprintf "%s -page %d %S" !selfexec pageno path in
3315 try popen command []
3316 with exn ->
3317 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
3318 flush stderr;
3319 else
3320 let anchor = getanchor () in
3321 let ranchor = state.path, state.password, anchor, state.origin in
3322 state.origin <- E.s;
3323 state.anchor <- (pageno, 0.0, 0.0);
3324 state.ranchors <- ranchor :: state.ranchors;
3325 opendoc path E.s;
3327 else showtext '!' ("Could not find " ^ filename)
3329 | Uremotedest (filename, destname) ->
3330 let path = getpath filename in
3331 if nonemptystr path
3332 then (
3333 if conf.riani
3334 then
3335 let command = !selfexec ^ " " ^ path ^ " -dest " ^ destname in
3336 try popen command []
3337 with exn ->
3338 Printf.eprintf
3339 "failed to execute `%s': %s\n" command (exntos exn);
3340 flush stderr;
3341 else
3342 let anchor = getanchor () in
3343 let ranchor = state.path, state.password, anchor, state.origin in
3344 state.origin <- E.s;
3345 state.nameddest <- destname;
3346 state.ranchors <- ranchor :: state.ranchors;
3347 opendoc path E.s;
3349 else showtext '!' ("Could not find " ^ filename)
3351 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
3354 let gotohist (path, (c, bookmarks, x, anchor)) =
3355 Config.save leavebirdseye;
3356 state.anchor <- anchor;
3357 state.x <- x;
3358 state.bookmarks <- bookmarks;
3359 state.origin <- E.s;
3360 opendoc path E.s;
3361 conf.presentation <- c.presentation;
3364 let gotooutline (_, _, kind) =
3365 match kind with
3366 | Onone -> ()
3367 | Oanchor anchor ->
3368 let (pageno, y, _) = anchor in
3369 let y = getanchory
3370 (if conf.presentation then (pageno, y, 1.0) else anchor)
3372 addnav ();
3373 gotoghyll y
3374 | Ouri uri -> gotounder (Ulinkuri uri)
3375 | Olaunch cmd -> gotounder (Ulaunch cmd)
3376 | Oremote remote -> gotounder (Uremote remote)
3377 | Ohistory hist -> gotohist hist
3378 | Oremotedest remotedest -> gotounder (Uremotedest remotedest)
3381 let genhistoutlines () =
3382 let list = ref [] in
3383 if Config.gethist list
3384 then
3385 let ol =
3386 List.fold_left (fun accu (path, c, b, x, a) ->
3387 let hist = (path, (c, b, x, a)) in
3388 let base = mbtoutf8 (Filename.basename path) in
3389 (base ^ "\000" ^ c.title, 0, Ohistory hist) :: accu
3390 ) [] !list
3392 Array.of_list ol;
3393 else E.a;
3396 let outlinesource sourcetype =
3397 let empty = E.a in
3398 (object (self)
3399 inherit lvsourcebase
3400 val mutable m_items = empty
3401 val mutable m_minfo = empty
3402 val mutable m_orig_items = empty
3403 val mutable m_orig_minfo = empty
3404 val mutable m_narrow_patterns = []
3405 val mutable m_hadremovals = false
3406 val mutable m_gen = -1
3408 method getitemcount =
3409 Array.length m_items + (if m_hadremovals then 1 else 0)
3411 method getitem n =
3412 if n == Array.length m_items && m_hadremovals
3413 then
3414 ("[Confirm removal]", 0)
3415 else
3416 let s, n, _ = m_items.(n) in
3417 (s, n)
3419 method exit ~uioh ~cancel ~active ~first ~pan =
3420 ignore (uioh, first);
3421 let confrimremoval = m_hadremovals && active = Array.length m_items in
3422 let items, minfo =
3423 if m_narrow_patterns = []
3424 then m_orig_items, m_orig_minfo
3425 else m_items, m_minfo
3427 if not cancel
3428 then (
3429 if not confrimremoval
3430 then (
3431 gotooutline m_items.(active);
3432 m_items <- items;
3433 m_minfo <- minfo;
3435 else (
3436 state.bookmarks <- Array.to_list m_items;
3437 m_orig_items <- m_items;
3438 m_orig_minfo <- m_minfo;
3441 else (
3442 m_items <- items;
3443 m_minfo <- minfo;
3445 m_pan <- pan;
3446 None
3448 method hasaction _ = true
3450 method greetmsg =
3451 if Array.length m_items != Array.length m_orig_items
3452 then
3453 let s =
3454 match m_narrow_patterns with
3455 | one :: [] -> one
3456 | many -> String.concat "\xe2\x80\xa6" (List.rev many)
3458 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
3459 else E.s
3461 method statestr =
3462 match m_narrow_patterns with
3463 | [] -> E.s
3464 | one :: [] -> one
3465 | head :: _ -> "\xe2\x80\xa6" ^ head
3467 method narrow pattern =
3468 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3469 match reopt with
3470 | None -> ()
3471 | Some re ->
3472 let rec loop accu minfo n =
3473 if n = -1
3474 then (
3475 m_items <- Array.of_list accu;
3476 m_minfo <- Array.of_list minfo;
3478 else
3479 let (s, _, _) as o = m_items.(n) in
3480 let accu, minfo =
3481 let first =
3482 try Str.search_forward re s 0
3483 with Not_found -> -1
3485 if first >= 0
3486 then o :: accu, (first, Str.match_end ()) :: minfo
3487 else accu, minfo
3489 loop accu minfo (n-1)
3491 loop [] [] (Array.length m_items - 1)
3493 method getminfo = m_minfo
3495 method denarrow =
3496 m_orig_items <- (
3497 match sourcetype with
3498 | `bookmarks -> Array.of_list state.bookmarks
3499 | `outlines -> state.outlines
3500 | `history -> genhistoutlines ()
3502 m_minfo <- m_orig_minfo;
3503 m_items <- m_orig_items
3505 method remove m =
3506 if sourcetype = `bookmarks
3507 then
3508 if m >= 0 && m < Array.length m_items
3509 then (
3510 m_hadremovals <- true;
3511 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3512 let n = if n >= m then n+1 else n in
3513 m_items.(n)
3517 method add_narrow_pattern pattern =
3518 m_narrow_patterns <- pattern :: m_narrow_patterns
3520 method del_narrow_pattern =
3521 match m_narrow_patterns with
3522 | _ :: rest -> m_narrow_patterns <- rest
3523 | [] -> ()
3525 method renarrow =
3526 self#denarrow;
3527 match m_narrow_patterns with
3528 | pattern :: [] -> self#narrow pattern; pattern
3529 | list ->
3530 List.fold_left (fun accu pattern ->
3531 self#narrow pattern;
3532 pattern ^ "\xe2\x80\xa6" ^ accu) E.s list
3534 method calcactive anchor =
3535 let rely = getanchory anchor in
3536 let rec loop n best bestd =
3537 if n = Array.length m_items
3538 then best
3539 else
3540 let _, _, kind = m_items.(n) in
3541 match kind with
3542 | Oanchor anchor ->
3543 let orely = getanchory anchor in
3544 let d = abs (orely - rely) in
3545 if d < bestd
3546 then loop (n+1) n d
3547 else loop (n+1) best bestd
3548 | Onone | Oremote _ | Olaunch _
3549 | Oremotedest _ | Ouri _ | Ohistory _ ->
3550 loop (n+1) best bestd
3552 loop 0 ~-1 max_int
3554 method reset anchor items =
3555 m_hadremovals <- false;
3556 if state.gen != m_gen
3557 then (
3558 m_orig_items <- items;
3559 m_items <- items;
3560 m_narrow_patterns <- [];
3561 m_minfo <- empty;
3562 m_orig_minfo <- empty;
3563 m_gen <- state.gen;
3565 else (
3566 if items != m_orig_items
3567 then (
3568 m_orig_items <- items;
3569 if m_narrow_patterns == []
3570 then m_items <- items;
3573 let active = self#calcactive anchor in
3574 m_active <- active;
3575 m_first <- firstof m_first active
3576 end)
3579 let enterselector sourcetype =
3580 resetmstate ();
3581 let source = outlinesource sourcetype in
3582 fun errmsg ->
3583 let outlines =
3584 match sourcetype with
3585 | `bookmarks -> Array.of_list state.bookmarks
3586 | `outlines -> state.outlines
3587 | `history -> genhistoutlines ()
3589 if Array.length outlines = 0
3590 then (
3591 showtext ' ' errmsg;
3593 else (
3594 state.text <- source#greetmsg;
3595 Wsi.setcursor Wsi.CURSOR_INHERIT;
3596 let anchor = getanchor () in
3597 source#reset anchor outlines;
3598 state.uioh <- coe (new outlinelistview ~source);
3599 G.postRedisplay "enter selector";
3603 let enteroutlinemode =
3604 let f = enterselector `outlines in
3605 fun () -> f "Document has no outline";
3608 let enterbookmarkmode =
3609 let f = enterselector `bookmarks in
3610 fun () -> f "Document has no bookmarks (yet)";
3613 let enterhistmode =
3614 let f = enterselector `history in
3615 fun () -> f "No history (yet)";
3618 let makecheckers () =
3619 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3620 following to say:
3621 converted by Issac Trotts. July 25, 2002 *)
3622 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3623 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3624 let id = GlTex.gen_texture () in
3625 GlTex.bind_texture `texture_2d id;
3626 GlPix.store (`unpack_alignment 1);
3627 GlTex.image2d image;
3628 List.iter (GlTex.parameter ~target:`texture_2d)
3629 [ `mag_filter `nearest; `min_filter `nearest ];
3633 let setcheckers enabled =
3634 match state.checkerstexid with
3635 | None ->
3636 if enabled then state.checkerstexid <- Some (makecheckers ())
3638 | Some checkerstexid ->
3639 if not enabled
3640 then (
3641 GlTex.delete_texture checkerstexid;
3642 state.checkerstexid <- None;
3646 let describe_location () =
3647 let fn = page_of_y state.y in
3648 let ln = page_of_y (state.y + state.winh - hscrollh () - 1) in
3649 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
3650 let percent =
3651 if maxy <= 0
3652 then 100.
3653 else (100. *. (float state.y /. float maxy))
3655 if fn = ln
3656 then
3657 Printf.sprintf "page %d of %d [%.2f%%]"
3658 (fn+1) state.pagecount percent
3659 else
3660 Printf.sprintf
3661 "pages %d-%d of %d [%.2f%%]"
3662 (fn+1) (ln+1) state.pagecount percent
3665 let setpresentationmode v =
3666 let n = page_of_y state.y in
3667 state.anchor <- (n, 0.0, 1.0);
3668 conf.presentation <- v;
3669 if conf.fitmodel = FitPage
3670 then reqlayout conf.angle conf.fitmodel;
3671 represent ();
3674 let enterinfomode =
3675 let btos b = if b then "\xe2\x88\x9a" else E.s in
3676 let showextended = ref false in
3677 let leave mode = function
3678 | Confirm -> state.mode <- mode
3679 | Cancel -> state.mode <- mode in
3680 let src =
3681 (object
3682 val mutable m_first_time = true
3683 val mutable m_l = []
3684 val mutable m_a = E.a
3685 val mutable m_prev_uioh = nouioh
3686 val mutable m_prev_mode = View
3688 inherit lvsourcebase
3690 method reset prev_mode prev_uioh =
3691 m_a <- Array.of_list (List.rev m_l);
3692 m_l <- [];
3693 m_prev_mode <- prev_mode;
3694 m_prev_uioh <- prev_uioh;
3695 if m_first_time
3696 then (
3697 let rec loop n =
3698 if n >= Array.length m_a
3699 then ()
3700 else
3701 match m_a.(n) with
3702 | _, _, _, Action _ -> m_active <- n
3703 | _ -> loop (n+1)
3705 loop 0;
3706 m_first_time <- false;
3709 method int name get set =
3710 m_l <-
3711 (name, `int get, 1, Action (
3712 fun u ->
3713 let ondone s =
3714 try set (int_of_string s)
3715 with exn ->
3716 state.text <- Printf.sprintf "bad integer `%s': %s"
3717 s (exntos exn)
3719 state.text <- E.s;
3720 let te = name ^ ": ", E.s, None, intentry, ondone, true in
3721 state.mode <- Textentry (te, leave m_prev_mode);
3723 )) :: m_l
3725 method int_with_suffix name get set =
3726 m_l <-
3727 (name, `intws get, 1, Action (
3728 fun u ->
3729 let ondone s =
3730 try set (int_of_string_with_suffix s)
3731 with exn ->
3732 state.text <- Printf.sprintf "bad integer `%s': %s"
3733 s (exntos exn)
3735 state.text <- E.s;
3736 let te =
3737 name ^ ": ", E.s, None, intentry_with_suffix, ondone, true
3739 state.mode <- Textentry (te, leave m_prev_mode);
3741 )) :: m_l
3743 method bool ?(offset=1) ?(btos=btos) name get set =
3744 m_l <-
3745 (name, `bool (btos, get), offset, Action (
3746 fun u ->
3747 let v = get () in
3748 set (not v);
3750 )) :: m_l
3752 method color name get set =
3753 m_l <-
3754 (name, `color get, 1, Action (
3755 fun u ->
3756 let invalid = (nan, nan, nan) in
3757 let ondone s =
3758 let c =
3759 try color_of_string s
3760 with exn ->
3761 state.text <- Printf.sprintf "bad color `%s': %s"
3762 s (exntos exn);
3763 invalid
3765 if c <> invalid
3766 then set c;
3768 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3769 state.text <- color_to_string (get ());
3770 state.mode <- Textentry (te, leave m_prev_mode);
3772 )) :: m_l
3774 method string name get set =
3775 m_l <-
3776 (name, `string get, 1, Action (
3777 fun u ->
3778 let ondone s = set s in
3779 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3780 state.mode <- Textentry (te, leave m_prev_mode);
3782 )) :: m_l
3784 method colorspace name get set =
3785 m_l <-
3786 (name, `string get, 1, Action (
3787 fun _ ->
3788 let source =
3789 (object
3790 inherit lvsourcebase
3792 initializer
3793 m_active <- CSTE.to_int conf.colorspace;
3794 m_first <- 0;
3796 method getitemcount =
3797 Array.length CSTE.names
3798 method getitem n =
3799 (CSTE.names.(n), 0)
3800 method exit ~uioh ~cancel ~active ~first ~pan =
3801 ignore (uioh, first, pan);
3802 if not cancel then set active;
3803 None
3804 method hasaction _ = true
3805 end)
3807 state.text <- E.s;
3808 let modehash = findkeyhash conf "info" in
3809 coe (new listview ~helpmode:false ~source ~trusted:true ~modehash)
3810 )) :: m_l
3812 method paxmark name get set =
3813 m_l <-
3814 (name, `string get, 1, Action (
3815 fun _ ->
3816 let source =
3817 (object
3818 inherit lvsourcebase
3820 initializer
3821 m_active <- MTE.to_int conf.paxmark;
3822 m_first <- 0;
3824 method getitemcount = Array.length MTE.names
3825 method getitem n = (MTE.names.(n), 0)
3826 method exit ~uioh ~cancel ~active ~first ~pan =
3827 ignore (uioh, first, pan);
3828 if not cancel then set active;
3829 None
3830 method hasaction _ = true
3831 end)
3833 state.text <- E.s;
3834 let modehash = findkeyhash conf "info" in
3835 coe (new listview ~helpmode:false ~source ~trusted:true ~modehash)
3836 )) :: m_l
3838 method fitmodel name get set =
3839 m_l <-
3840 (name, `string get, 1, Action (
3841 fun _ ->
3842 let source =
3843 (object
3844 inherit lvsourcebase
3846 initializer
3847 m_active <- FMTE.to_int conf.fitmodel;
3848 m_first <- 0;
3850 method getitemcount = Array.length FMTE.names
3851 method getitem n = (FMTE.names.(n), 0)
3852 method exit ~uioh ~cancel ~active ~first ~pan =
3853 ignore (uioh, first, pan);
3854 if not cancel then set active;
3855 None
3856 method hasaction _ = true
3857 end)
3859 state.text <- E.s;
3860 let modehash = findkeyhash conf "info" in
3861 coe (new listview ~helpmode:false ~source ~trusted:true ~modehash)
3862 )) :: m_l
3864 method caption s offset =
3865 m_l <- (s, `empty, offset, Noaction) :: m_l
3867 method caption2 s f offset =
3868 m_l <- (s, `string f, offset, Noaction) :: m_l
3870 method getitemcount = Array.length m_a
3872 method getitem n =
3873 let tostr = function
3874 | `int f -> string_of_int (f ())
3875 | `intws f -> string_with_suffix_of_int (f ())
3876 | `string f -> f ()
3877 | `color f -> color_to_string (f ())
3878 | `bool (btos, f) -> btos (f ())
3879 | `empty -> E.s
3881 let name, t, offset, _ = m_a.(n) in
3882 ((let s = tostr t in
3883 if nonemptystr s
3884 then Printf.sprintf "%s\t%s" name s
3885 else name),
3886 offset)
3888 method exit ~uioh ~cancel ~active ~first ~pan =
3889 let uiohopt =
3890 if not cancel
3891 then (
3892 let uioh =
3893 match m_a.(active) with
3894 | _, _, _, Action f -> f uioh
3895 | _ -> uioh
3897 Some uioh
3899 else None
3901 m_active <- active;
3902 m_first <- first;
3903 m_pan <- pan;
3904 uiohopt
3906 method hasaction n =
3907 match m_a.(n) with
3908 | _, _, _, Action _ -> true
3909 | _ -> false
3910 end)
3912 let rec fillsrc prevmode prevuioh =
3913 let sep () = src#caption E.s 0 in
3914 let colorp name get set =
3915 src#string name
3916 (fun () -> color_to_string (get ()))
3917 (fun v ->
3919 let c = color_of_string v in
3920 set c
3921 with exn ->
3922 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
3925 let oldmode = state.mode in
3926 let birdseye = isbirdseye state.mode in
3928 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3930 src#bool "presentation mode"
3931 (fun () -> conf.presentation)
3932 (fun v -> setpresentationmode v);
3934 src#bool "ignore case in searches"
3935 (fun () -> conf.icase)
3936 (fun v -> conf.icase <- v);
3938 src#bool "preload"
3939 (fun () -> conf.preload)
3940 (fun v -> conf.preload <- v);
3942 src#bool "highlight links"
3943 (fun () -> conf.hlinks)
3944 (fun v -> conf.hlinks <- v);
3946 src#bool "under info"
3947 (fun () -> conf.underinfo)
3948 (fun v -> conf.underinfo <- v);
3950 src#bool "persistent bookmarks"
3951 (fun () -> conf.savebmarks)
3952 (fun v -> conf.savebmarks <- v);
3954 src#fitmodel "fit model"
3955 (fun () -> FMTE.to_string conf.fitmodel)
3956 (fun v -> reqlayout conf.angle (FMTE.of_int v));
3958 src#bool "trim margins"
3959 (fun () -> conf.trimmargins)
3960 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3962 src#bool "persistent location"
3963 (fun () -> conf.jumpback)
3964 (fun v -> conf.jumpback <- v);
3966 sep ();
3967 src#int "inter-page space"
3968 (fun () -> conf.interpagespace)
3969 (fun n ->
3970 conf.interpagespace <- n;
3971 docolumns conf.columns;
3972 let pageno, py =
3973 match state.layout with
3974 | [] -> 0, 0
3975 | l :: _ ->
3976 l.pageno, l.pagey
3978 state.maxy <- calcheight ();
3979 let y = getpagey pageno in
3980 gotoy (y + py)
3983 src#int "page bias"
3984 (fun () -> conf.pagebias)
3985 (fun v -> conf.pagebias <- v);
3987 src#int "scroll step"
3988 (fun () -> conf.scrollstep)
3989 (fun n -> conf.scrollstep <- n);
3991 src#int "horizontal scroll step"
3992 (fun () -> conf.hscrollstep)
3993 (fun v -> conf.hscrollstep <- v);
3995 src#int "auto scroll step"
3996 (fun () ->
3997 match state.autoscroll with
3998 | Some step -> step
3999 | _ -> conf.autoscrollstep)
4000 (fun n ->
4001 let n = boundastep state.winh n in
4002 if state.autoscroll <> None
4003 then state.autoscroll <- Some n;
4004 conf.autoscrollstep <- n);
4006 src#int "zoom"
4007 (fun () -> truncate (conf.zoom *. 100.))
4008 (fun v -> setzoom ((float v) /. 100.));
4010 src#int "rotation"
4011 (fun () -> conf.angle)
4012 (fun v -> reqlayout v conf.fitmodel);
4014 src#int "scroll bar width"
4015 (fun () -> conf.scrollbw)
4016 (fun v ->
4017 conf.scrollbw <- v;
4018 reshape state.winw state.winh;
4021 src#int "scroll handle height"
4022 (fun () -> conf.scrollh)
4023 (fun v -> conf.scrollh <- v;);
4025 src#int "thumbnail width"
4026 (fun () -> conf.thumbw)
4027 (fun v ->
4028 conf.thumbw <- min 4096 v;
4029 match oldmode with
4030 | Birdseye beye ->
4031 leavebirdseye beye false;
4032 enterbirdseye ()
4033 | _ -> ()
4036 let mode = state.mode in
4037 src#string "columns"
4038 (fun () ->
4039 match conf.columns with
4040 | Csingle _ -> "1"
4041 | Cmulti (multi, _) -> multicolumns_to_string multi
4042 | Csplit (count, _) -> "-" ^ string_of_int count
4044 (fun v ->
4045 let n, a, b = multicolumns_of_string v in
4046 setcolumns mode n a b);
4048 sep ();
4049 src#caption "Pixmap cache" 0;
4050 src#int_with_suffix "size (advisory)"
4051 (fun () -> conf.memlimit)
4052 (fun v -> conf.memlimit <- v);
4054 src#caption2 "used"
4055 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4056 (string_with_suffix_of_int state.memused)
4057 (Hashtbl.length state.tilemap)) 1;
4059 sep ();
4060 src#caption "Layout" 0;
4061 src#caption2 "Dimension"
4062 (fun () ->
4063 Printf.sprintf "%dx%d (virtual %dx%d)"
4064 state.winw state.winh
4065 state.w state.maxy)
4067 if conf.debug
4068 then
4069 src#caption2 "Position" (fun () ->
4070 Printf.sprintf "%dx%d" state.x state.y
4072 else
4073 src#caption2 "Position" (fun () -> describe_location ()) 1
4076 sep ();
4077 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4078 "Save these parameters as global defaults at exit"
4079 (fun () -> conf.bedefault)
4080 (fun v -> conf.bedefault <- v)
4083 sep ();
4084 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4085 src#bool ~offset:0 ~btos "Extended parameters"
4086 (fun () -> !showextended)
4087 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4088 if !showextended
4089 then (
4090 src#bool "checkers"
4091 (fun () -> conf.checkers)
4092 (fun v -> conf.checkers <- v; setcheckers v);
4093 src#bool "update cursor"
4094 (fun () -> conf.updatecurs)
4095 (fun v -> conf.updatecurs <- v);
4096 src#bool "scroll-bar on the left"
4097 (fun () -> conf.leftscroll)
4098 (fun v -> conf.leftscroll <- v);
4099 src#bool "verbose"
4100 (fun () -> conf.verbose)
4101 (fun v -> conf.verbose <- v);
4102 src#bool "invert colors"
4103 (fun () -> conf.invert)
4104 (fun v -> conf.invert <- v);
4105 src#bool "max fit"
4106 (fun () -> conf.maxhfit)
4107 (fun v -> conf.maxhfit <- v);
4108 src#bool "redirect stderr"
4109 (fun () -> conf.redirectstderr)
4110 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4111 src#bool "pax mode"
4112 (fun () -> conf.pax != None)
4113 (fun v ->
4114 if v
4115 then conf.pax <- Some (ref (now (), 0, 0))
4116 else conf.pax <- None);
4117 src#string "uri launcher"
4118 (fun () -> conf.urilauncher)
4119 (fun v -> conf.urilauncher <- v);
4120 src#string "path launcher"
4121 (fun () -> conf.pathlauncher)
4122 (fun v -> conf.pathlauncher <- v);
4123 src#string "tile size"
4124 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4125 (fun v ->
4127 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4128 conf.tilew <- max 64 w;
4129 conf.tileh <- max 64 h;
4130 flushtiles ();
4131 with exn ->
4132 state.text <- Printf.sprintf "bad tile size `%s': %s"
4133 v (exntos exn)
4135 src#int "texture count"
4136 (fun () -> conf.texcount)
4137 (fun v ->
4138 if realloctexts v
4139 then conf.texcount <- v
4140 else showtext '!' " Failed to set texture count please retry later"
4142 src#int "slice height"
4143 (fun () -> conf.sliceheight)
4144 (fun v ->
4145 conf.sliceheight <- v;
4146 wcmd "sliceh %d" conf.sliceheight;
4148 src#int "anti-aliasing level"
4149 (fun () -> conf.aalevel)
4150 (fun v ->
4151 conf.aalevel <- bound v 0 8;
4152 state.anchor <- getanchor ();
4153 opendoc state.path state.password;
4155 src#string "page scroll scaling factor"
4156 (fun () -> string_of_float conf.pgscale)
4157 (fun v ->
4159 let s = float_of_string v in
4160 conf.pgscale <- s
4161 with exn ->
4162 state.text <- Printf.sprintf
4163 "bad page scroll scaling factor `%s': %s" v (exntos exn)
4166 src#int "ui font size"
4167 (fun () -> fstate.fontsize)
4168 (fun v -> setfontsize (bound v 5 100));
4169 src#int "hint font size"
4170 (fun () -> conf.hfsize)
4171 (fun v -> conf.hfsize <- bound v 5 100);
4172 colorp "background color"
4173 (fun () -> conf.bgcolor)
4174 (fun v -> conf.bgcolor <- v);
4175 src#bool "crop hack"
4176 (fun () -> conf.crophack)
4177 (fun v -> conf.crophack <- v);
4178 src#string "trim fuzz"
4179 (fun () -> irect_to_string conf.trimfuzz)
4180 (fun v ->
4182 conf.trimfuzz <- irect_of_string v;
4183 if conf.trimmargins
4184 then settrim true conf.trimfuzz;
4185 with exn ->
4186 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
4188 src#string "throttle"
4189 (fun () ->
4190 match conf.maxwait with
4191 | None -> "show place holder if page is not ready"
4192 | Some time ->
4193 if time = infinity
4194 then "wait for page to fully render"
4195 else
4196 "wait " ^ string_of_float time
4197 ^ " seconds before showing placeholder"
4199 (fun v ->
4201 let f = float_of_string v in
4202 if f <= 0.0
4203 then conf.maxwait <- None
4204 else conf.maxwait <- Some f
4205 with exn ->
4206 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
4208 src#string "ghyll scroll"
4209 (fun () ->
4210 match conf.ghyllscroll with
4211 | None -> E.s
4212 | Some nab -> ghyllscroll_to_string nab
4214 (fun v ->
4215 try conf.ghyllscroll <- ghyllscroll_of_string v
4216 with exn ->
4217 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
4219 src#string "selection command"
4220 (fun () -> conf.selcmd)
4221 (fun v -> conf.selcmd <- v);
4222 src#string "synctex command"
4223 (fun () -> conf.stcmd)
4224 (fun v -> conf.stcmd <- v);
4225 src#string "pax command"
4226 (fun () -> conf.paxcmd)
4227 (fun v -> conf.paxcmd <- v);
4228 src#colorspace "color space"
4229 (fun () -> CSTE.to_string conf.colorspace)
4230 (fun v ->
4231 conf.colorspace <- CSTE.of_int v;
4232 wcmd "cs %d" v;
4233 load state.layout;
4235 src#paxmark "pax mark method"
4236 (fun () -> MTE.to_string conf.paxmark)
4237 (fun v -> conf.paxmark <- MTE.of_int v);
4238 if pbousable ()
4239 then
4240 src#bool "use PBO"
4241 (fun () -> conf.usepbo)
4242 (fun v -> conf.usepbo <- v);
4243 src#bool "mouse wheel scrolls pages"
4244 (fun () -> conf.wheelbypage)
4245 (fun v -> conf.wheelbypage <- v);
4246 src#bool "open remote links in a new instance"
4247 (fun () -> conf.riani)
4248 (fun v -> conf.riani <- v);
4251 sep ();
4252 src#caption "Document" 0;
4253 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4254 src#caption2 "Pages"
4255 (fun () -> string_of_int state.pagecount) 1;
4256 src#caption2 "Dimensions"
4257 (fun () -> string_of_int (List.length state.pdims)) 1;
4258 if conf.trimmargins
4259 then (
4260 sep ();
4261 src#caption "Trimmed margins" 0;
4262 src#caption2 "Dimensions"
4263 (fun () -> string_of_int (List.length state.pdims)) 1;
4266 sep ();
4267 src#caption "OpenGL" 0;
4268 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4269 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4271 sep ();
4272 src#caption "Location" 0;
4273 if nonemptystr state.origin
4274 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
4275 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
4277 src#reset prevmode prevuioh;
4279 fun () ->
4280 state.text <- E.s;
4281 resetmstate ();
4282 let prevmode = state.mode
4283 and prevuioh = state.uioh in
4284 fillsrc prevmode prevuioh;
4285 let source = (src :> lvsource) in
4286 let modehash = findkeyhash conf "info" in
4287 state.uioh <- coe (object (self)
4288 inherit listview ~helpmode:false ~source ~trusted:true ~modehash as super
4289 val mutable m_prevmemused = 0
4290 method infochanged = function
4291 | Memused ->
4292 if m_prevmemused != state.memused
4293 then (
4294 m_prevmemused <- state.memused;
4295 G.postRedisplay "memusedchanged";
4297 | Pdim -> G.postRedisplay "pdimchanged"
4298 | Docinfo -> fillsrc prevmode prevuioh
4300 method key key mask =
4301 if not (Wsi.withctrl mask)
4302 then
4303 match key with
4304 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
4305 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
4306 | _ -> super#key key mask
4307 else super#key key mask
4308 end);
4309 G.postRedisplay "info";
4312 let enterhelpmode =
4313 let source =
4314 (object
4315 inherit lvsourcebase
4316 method getitemcount = Array.length state.help
4317 method getitem n =
4318 let s, l, _ = state.help.(n) in
4319 (s, l)
4321 method exit ~uioh ~cancel ~active ~first ~pan =
4322 let optuioh =
4323 if not cancel
4324 then (
4325 match state.help.(active) with
4326 | _, _, Action f -> Some (f uioh)
4327 | _ -> Some (uioh)
4329 else None
4331 m_active <- active;
4332 m_first <- first;
4333 m_pan <- pan;
4334 optuioh
4336 method hasaction n =
4337 match state.help.(n) with
4338 | _, _, Action _ -> true
4339 | _ -> false
4341 initializer
4342 m_active <- -1
4343 end)
4344 in fun () ->
4345 let modehash = findkeyhash conf "help" in
4346 resetmstate ();
4347 state.uioh <- coe (new listview
4348 ~helpmode:true ~source ~trusted:true ~modehash);
4349 G.postRedisplay "help";
4352 let entermsgsmode =
4353 let msgsource =
4354 let re = Str.regexp "[\r\n]" in
4355 (object
4356 inherit lvsourcebase
4357 val mutable m_items = E.a
4359 method getitemcount = 1 + Array.length m_items
4361 method getitem n =
4362 if n = 0
4363 then "[Clear]", 0
4364 else m_items.(n-1), 0
4366 method exit ~uioh ~cancel ~active ~first ~pan =
4367 ignore uioh;
4368 if not cancel
4369 then (
4370 if active = 0
4371 then Buffer.clear state.errmsgs;
4373 m_active <- active;
4374 m_first <- first;
4375 m_pan <- pan;
4376 None
4378 method hasaction n =
4379 n = 0
4381 method reset =
4382 state.newerrmsgs <- false;
4383 let l = Str.split re (Buffer.contents state.errmsgs) in
4384 m_items <- Array.of_list l
4386 initializer
4387 m_active <- 0
4388 end)
4389 in fun () ->
4390 state.text <- E.s;
4391 resetmstate ();
4392 msgsource#reset;
4393 let source = (msgsource :> lvsource) in
4394 let modehash = findkeyhash conf "listview" in
4395 state.uioh <- coe (object
4396 inherit listview ~helpmode:false ~source ~trusted:false ~modehash as super
4397 method display =
4398 if state.newerrmsgs
4399 then msgsource#reset;
4400 super#display
4401 end);
4402 G.postRedisplay "msgs";
4405 let quickbookmark ?title () =
4406 match state.layout with
4407 | [] -> ()
4408 | l :: _ ->
4409 let title =
4410 match title with
4411 | None ->
4412 let tm = Unix.localtime (now ()) in
4413 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4414 (l.pageno+1)
4415 tm.Unix.tm_mday
4416 tm.Unix.tm_mon
4417 (tm.Unix.tm_year + 1900)
4418 tm.Unix.tm_hour
4419 tm.Unix.tm_min
4420 | Some title -> title
4422 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4425 let setautoscrollspeed step goingdown =
4426 let incr = max 1 ((abs step) / 2) in
4427 let incr = if goingdown then incr else -incr in
4428 let astep = boundastep state.winh (step + incr) in
4429 state.autoscroll <- Some astep;
4432 let canpan () =
4433 match conf.columns with
4434 | Csplit _ -> true
4435 | _ -> state.x != 0 || conf.zoom > 1.0
4438 let panbound x = bound x (-state.w) (wadjsb state.winw);;
4440 let existsinrow pageno (columns, coverA, coverB) p =
4441 let last = ((pageno - coverA) mod columns) + columns in
4442 let rec any = function
4443 | [] -> false
4444 | l :: rest ->
4445 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4446 then p l
4447 else (
4448 if not (p l)
4449 then (if l.pageno = last then false else any rest)
4450 else true
4453 any state.layout
4456 let nextpage () =
4457 match state.layout with
4458 | [] ->
4459 let pageno = page_of_y state.y in
4460 gotoghyll (getpagey (pageno+1))
4461 | l :: rest ->
4462 match conf.columns with
4463 | Csingle _ ->
4464 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4465 then
4466 let y = clamp (pgscale state.winh) in
4467 gotoghyll y
4468 else
4469 let pageno = min (l.pageno+1) (state.pagecount-1) in
4470 gotoghyll (getpagey pageno)
4471 | Cmulti ((c, _, _) as cl, _) ->
4472 if conf.presentation
4473 && (existsinrow l.pageno cl
4474 (fun l -> l.pageh > l.pagey + l.pagevh))
4475 then
4476 let y = clamp (pgscale state.winh) in
4477 gotoghyll y
4478 else
4479 let pageno = min (l.pageno+c) (state.pagecount-1) in
4480 gotoghyll (getpagey pageno)
4481 | Csplit (n, _) ->
4482 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4483 then
4484 let pagey, pageh = getpageyh l.pageno in
4485 let pagey = pagey + pageh * l.pagecol in
4486 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4487 gotoghyll (pagey + pageh + ips)
4490 let prevpage () =
4491 match state.layout with
4492 | [] ->
4493 let pageno = page_of_y state.y in
4494 gotoghyll (getpagey (pageno-1))
4495 | l :: _ ->
4496 match conf.columns with
4497 | Csingle _ ->
4498 if conf.presentation && l.pagey != 0
4499 then
4500 gotoghyll (clamp (pgscale ~-(state.winh)))
4501 else
4502 let pageno = max 0 (l.pageno-1) in
4503 gotoghyll (getpagey pageno)
4504 | Cmulti ((c, _, coverB) as cl, _) ->
4505 if conf.presentation &&
4506 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4507 then
4508 gotoghyll (clamp (pgscale ~-(state.winh)))
4509 else
4510 let decr =
4511 if l.pageno = state.pagecount - coverB
4512 then 1
4513 else c
4515 let pageno = max 0 (l.pageno-decr) in
4516 gotoghyll (getpagey pageno)
4517 | Csplit (n, _) ->
4518 let y =
4519 if l.pagecol = 0
4520 then
4521 if l.pageno = 0
4522 then l.pagey
4523 else
4524 let pageno = max 0 (l.pageno-1) in
4525 let pagey, pageh = getpageyh pageno in
4526 pagey + (n-1)*pageh
4527 else
4528 let pagey, pageh = getpageyh l.pageno in
4529 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4531 gotoghyll y
4534 let viewkeyboard key mask =
4535 let enttext te =
4536 let mode = state.mode in
4537 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4538 state.text <- E.s;
4539 enttext ();
4540 G.postRedisplay "view:enttext"
4542 let ctrl = Wsi.withctrl mask in
4543 let key =
4544 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
4546 match key with
4547 | 81 -> (* Q *)
4548 exit 0
4550 | 0xff63 -> (* insert *)
4551 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4552 then (
4553 state.mode <- LinkNav (Ltgendir 0);
4554 gotoy state.y;
4556 else showtext '!' "Keyboard link navigation does not work under rotation"
4558 | 0xff1b | 113 -> (* escape / q *)
4559 begin match state.mstate with
4560 | Mzoomrect _ ->
4561 resetmstate ();
4562 G.postRedisplay "kill zoom rect";
4563 | _ ->
4564 begin match state.mode with
4565 | LinkNav _ ->
4566 state.mode <- View;
4567 G.postRedisplay "esc leave linknav"
4568 | _ ->
4569 match state.ranchors with
4570 | [] -> raise Quit
4571 | (path, password, anchor, origin) :: rest ->
4572 state.ranchors <- rest;
4573 state.anchor <- anchor;
4574 state.origin <- origin;
4575 state.nameddest <- E.s;
4576 opendoc path password
4577 end;
4578 end;
4580 | 0xff08 -> (* backspace *)
4581 gotoghyll (getnav ~-1)
4583 | 111 -> (* o *)
4584 enteroutlinemode ()
4586 | 72 -> (* H *)
4587 enterhistmode ()
4589 | 117 -> (* u *)
4590 state.rects <- [];
4591 state.text <- E.s;
4592 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap;
4593 G.postRedisplay "dehighlight";
4595 | 47 | 63 -> (* / ? *)
4596 let ondone isforw s =
4597 cbput state.hists.pat s;
4598 state.searchpattern <- s;
4599 search s isforw
4601 let s = String.create 1 in
4602 s.[0] <- Char.chr key;
4603 enttext (s, E.s, Some (onhist state.hists.pat),
4604 textentry, ondone (key = 47), true)
4606 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
4607 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4608 setzoom (conf.zoom +. incr)
4610 | 43 | 0xffab -> (* + *)
4611 let ondone s =
4612 let n =
4613 try int_of_string s with exc ->
4614 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
4615 max_int
4617 if n != max_int
4618 then (
4619 conf.pagebias <- n;
4620 state.text <- "page bias is now " ^ string_of_int n;
4623 enttext ("page bias: ", E.s, None, intentry, ondone, true)
4625 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4626 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4627 setzoom (max 0.01 (conf.zoom -. decr))
4629 | 45 | 0xffad -> (* - *)
4630 let ondone msg = state.text <- msg in
4631 enttext (
4632 "option [acfhilpstvxACFPRSZTISM]: ", E.s, None,
4633 optentry state.mode, ondone, true
4636 | 48 when ctrl -> (* ctrl-0 *)
4637 if conf.zoom = 1.0
4638 then (
4639 state.x <- 0;
4640 gotoy state.y
4642 else setzoom 1.0
4644 | (49 | 50) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
4645 let cols =
4646 match conf.columns with
4647 | Csingle _ | Cmulti _ -> 1
4648 | Csplit (n, _) -> n
4650 let h = state.winh -
4651 conf.interpagespace lsl (if conf.presentation then 1 else 0)
4653 let zoom = zoomforh state.winw h (vscrollw ()) cols in
4654 if zoom > 0.0 && (key = 50 || zoom < 1.0)
4655 then setzoom zoom
4657 | 51 when ctrl -> (* ctrl-3 *)
4658 let fm =
4659 match conf.fitmodel with
4660 | FitWidth -> FitProportional
4661 | FitProportional -> FitPage
4662 | FitPage -> FitWidth
4664 state.text <- "fit model: " ^ FMTE.to_string fm;
4665 reqlayout conf.angle fm
4667 | 0xffc6 -> (* f9 *)
4668 togglebirdseye ()
4670 | 57 when ctrl -> (* ctrl-9 *)
4671 togglebirdseye ()
4673 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4674 when not ctrl -> (* 0..9 *)
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);
4681 if n >= 0
4682 then (
4683 addnav ();
4684 cbput state.hists.pag (string_of_int n);
4685 gotopage1 (n + conf.pagebias - 1) 0;
4688 let pageentry text key =
4689 match Char.unsafe_chr key with
4690 | 'g' -> TEdone text
4691 | _ -> intentry text key
4693 let text = "x" in text.[0] <- Char.chr key;
4694 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
4696 | 98 -> (* b *)
4697 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
4698 reshape state.winw state.winh;
4700 | 66 -> (* B *)
4701 state.bzoom <- not state.bzoom;
4702 state.rects <- [];
4703 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
4705 | 108 -> (* l *)
4706 conf.hlinks <- not conf.hlinks;
4707 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4708 G.postRedisplay "toggle highlightlinks";
4710 | 70 -> (* F *)
4711 state.glinks <- true;
4712 let mode = state.mode in
4713 state.mode <- Textentry (
4714 (":", E.s, None, linknentry, linkndone gotounder, false),
4715 (fun _ ->
4716 state.glinks <- false;
4717 state.mode <- mode)
4719 state.text <- E.s;
4720 G.postRedisplay "view:linkent(F)"
4722 | 121 -> (* y *)
4723 state.glinks <- true;
4724 let mode = state.mode in
4725 state.mode <- Textentry (
4727 ":", E.s, None, linknentry, linkndone (fun under ->
4728 selstring (undertext under);
4729 ), false
4731 fun _ ->
4732 state.glinks <- false;
4733 state.mode <- mode
4735 state.text <- E.s;
4736 G.postRedisplay "view:linkent"
4738 | 97 -> (* a *)
4739 begin match state.autoscroll with
4740 | Some step ->
4741 conf.autoscrollstep <- step;
4742 state.autoscroll <- None
4743 | None ->
4744 if conf.autoscrollstep = 0
4745 then state.autoscroll <- Some 1
4746 else state.autoscroll <- Some conf.autoscrollstep
4749 | 112 when ctrl -> (* ctrl-p *)
4750 launchpath ()
4752 | 80 -> (* P *)
4753 setpresentationmode (not conf.presentation);
4754 showtext ' ' ("presentation mode " ^
4755 if conf.presentation then "on" else "off");
4757 | 102 -> (* f *)
4758 if List.mem Wsi.Fullscreen state.winstate
4759 then Wsi.reshape conf.cwinw conf.cwinh
4760 else Wsi.fullscreen ()
4762 | 112 | 78 -> (* p|N *)
4763 search state.searchpattern false
4765 | 110 | 0xffc0 -> (* n|F3 *)
4766 search state.searchpattern true
4768 | 116 -> (* t *)
4769 begin match state.layout with
4770 | [] -> ()
4771 | l :: _ ->
4772 gotoghyll (getpagey l.pageno)
4775 | 32 -> (* space *)
4776 nextpage ()
4778 | 0xff9f | 0xffff -> (* delete *)
4779 prevpage ()
4781 | 61 -> (* = *)
4782 showtext ' ' (describe_location ());
4784 | 119 -> (* w *)
4785 begin match state.layout with
4786 | [] -> ()
4787 | l :: _ ->
4788 Wsi.reshape (l.pagew + vscrollw ()) l.pageh;
4789 G.postRedisplay "w"
4792 | 39 -> (* ' *)
4793 enterbookmarkmode ()
4795 | 104 | 0xffbe -> (* h|F1 *)
4796 enterhelpmode ()
4798 | 105 -> (* i *)
4799 enterinfomode ()
4801 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
4802 entermsgsmode ()
4804 | 109 -> (* m *)
4805 let ondone s =
4806 match state.layout with
4807 | l :: _ ->
4808 if nonemptystr s
4809 then
4810 state.bookmarks <-
4811 (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4812 | _ -> ()
4814 enttext ("bookmark: ", E.s, None, textentry, ondone, true)
4816 | 126 -> (* ~ *)
4817 quickbookmark ();
4818 showtext ' ' "Quick bookmark added";
4820 | 122 -> (* z *)
4821 begin match state.layout with
4822 | l :: _ ->
4823 let rect = getpdimrect l.pagedimno in
4824 let w, h =
4825 if conf.crophack
4826 then
4827 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4828 truncate (1.2 *. (rect.(3) -. rect.(0))))
4829 else
4830 (truncate (rect.(1) -. rect.(0)),
4831 truncate (rect.(3) -. rect.(0)))
4833 let w = truncate ((float w)*.conf.zoom)
4834 and h = truncate ((float h)*.conf.zoom) in
4835 if w != 0 && h != 0
4836 then (
4837 state.anchor <- getanchor ();
4838 Wsi.reshape (w + vscrollw ()) (h + conf.interpagespace)
4840 G.postRedisplay "z";
4842 | [] -> ()
4845 | 120 -> (* x *)
4846 state.roam ()
4847 | 60 | 62 -> (* < > *)
4848 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.fitmodel
4850 | 91 | 93 -> (* [ ] *)
4851 conf.colorscale <-
4852 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
4854 G.postRedisplay "brightness";
4856 | 99 when state.mode = View -> (* [alt-]c *)
4857 if Wsi.withalt mask
4858 then (
4859 if conf.zoom > 1.0
4860 then
4861 let m = (wadjsb state.winw - state.w) / 2 in
4862 state.x <- m;
4863 gotoy_and_clear_text state.y
4865 else
4866 let (c, a, b), z =
4867 match state.prevcolumns with
4868 | None -> (1, 0, 0), 1.0
4869 | Some (columns, z) ->
4870 let cab =
4871 match columns with
4872 | Csplit (c, _) -> -c, 0, 0
4873 | Cmulti ((c, a, b), _) -> c, a, b
4874 | Csingle _ -> 1, 0, 0
4876 cab, z
4878 setcolumns View c a b;
4879 setzoom z
4881 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask
4882 -> (* ctrl-shift- (kp) [up|down] *)
4883 let zoom, x = state.prevzoom in
4884 setzoom zoom;
4885 state.x <- x;
4887 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
4888 begin match state.autoscroll with
4889 | None ->
4890 begin match state.mode with
4891 | Birdseye beye -> upbirdseye 1 beye
4892 | _ ->
4893 if ctrl
4894 then gotoy_and_clear_text (clamp ~-(state.winh/2))
4895 else (
4896 if not (Wsi.withshift mask) && conf.presentation
4897 then prevpage ()
4898 else gotoghyll1 true (clamp (-conf.scrollstep))
4901 | Some n ->
4902 setautoscrollspeed n false
4905 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
4906 begin match state.autoscroll with
4907 | None ->
4908 begin match state.mode with
4909 | Birdseye beye -> downbirdseye 1 beye
4910 | _ ->
4911 if ctrl
4912 then gotoy_and_clear_text (clamp (state.winh/2))
4913 else (
4914 if not (Wsi.withshift mask) && conf.presentation
4915 then nextpage ()
4916 else gotoghyll1 true (clamp (conf.scrollstep))
4919 | Some n ->
4920 setautoscrollspeed n true
4923 | 0xff51 | 0xff53 | 0xff96 | 0xff98
4924 when not (Wsi.withalt mask) -> (* (kp) left / right *)
4925 if canpan ()
4926 then
4927 let dx =
4928 if ctrl
4929 then state.winw / 2
4930 else conf.hscrollstep
4932 let dx = if key = 0xff51 || key = 0xff96 then dx else -dx in
4933 state.x <- panbound (state.x + dx);
4934 gotoy_and_clear_text state.y
4935 else (
4936 state.text <- E.s;
4937 G.postRedisplay "left/right"
4940 | 0xff55 | 0xff9a -> (* (kp) prior *)
4941 let y =
4942 if ctrl
4943 then
4944 match state.layout with
4945 | [] -> state.y
4946 | l :: _ -> state.y - l.pagey
4947 else
4948 clamp (pgscale (-state.winh))
4950 gotoghyll y
4952 | 0xff56 | 0xff9b -> (* (kp) next *)
4953 let y =
4954 if ctrl
4955 then
4956 match List.rev state.layout with
4957 | [] -> state.y
4958 | l :: _ -> getpagey l.pageno
4959 else
4960 clamp (pgscale state.winh)
4962 gotoghyll y
4964 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
4965 gotoghyll 0
4966 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
4967 gotoghyll (clamp state.maxy)
4969 | 0xff53 | 0xff98
4970 when Wsi.withalt mask -> (* alt-(kp) right *)
4971 gotoghyll (getnav 1)
4972 | 0xff51 | 0xff96
4973 when Wsi.withalt mask -> (* alt-(kp) left *)
4974 gotoghyll (getnav ~-1)
4976 | 114 -> (* r *)
4977 reload ()
4979 | 118 when conf.debug -> (* v *)
4980 state.rects <- [];
4981 List.iter (fun l ->
4982 match getopaque l.pageno with
4983 | None -> ()
4984 | Some opaque ->
4985 let x0, y0, x1, y1 = pagebbox opaque in
4986 let a,b = float x0, float y0 in
4987 let c,d = float x1, float y0 in
4988 let e,f = float x1, float y1 in
4989 let h,j = float x0, float y1 in
4990 let rect = (a,b,c,d,e,f,h,j) in
4991 debugrect rect;
4992 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
4993 ) state.layout;
4994 G.postRedisplay "v";
4996 | 124 -> (* | *)
4997 let mode = state.mode in
4998 let cmd = ref E.s in
4999 let onleave = function
5000 | Cancel -> state.mode <- mode
5001 | Confirm ->
5002 List.iter (fun l ->
5003 match getopaque l.pageno with
5004 | Some opaque -> pipesel opaque !cmd
5005 | None -> ()) state.layout;
5006 state.mode <- mode
5008 let ondone s =
5009 cbput state.hists.sel s;
5010 cmd := s
5012 let te =
5013 "| ", !cmd, Some (onhist state.hists.sel), textentry, ondone, true
5015 G.postRedisplay "|";
5016 state.mode <- Textentry (te, onleave);
5018 | _ ->
5019 vlog "huh? %s" (Wsi.keyname key)
5022 let linknavkeyboard key mask linknav =
5023 let getpage pageno =
5024 let rec loop = function
5025 | [] -> None
5026 | l :: _ when l.pageno = pageno -> Some l
5027 | _ :: rest -> loop rest
5028 in loop state.layout
5030 let doexact (pageno, n) =
5031 match getopaque pageno, getpage pageno with
5032 | Some opaque, Some l ->
5033 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5034 then
5035 let under = getlink opaque n in
5036 G.postRedisplay "link gotounder";
5037 gotounder under;
5038 state.mode <- View;
5039 else
5040 let opt, dir =
5041 match key with
5042 | 0xff50 -> (* home *)
5043 Some (findlink opaque LDfirst), -1
5045 | 0xff57 -> (* end *)
5046 Some (findlink opaque LDlast), 1
5048 | 0xff51 -> (* left *)
5049 Some (findlink opaque (LDleft n)), -1
5051 | 0xff53 -> (* right *)
5052 Some (findlink opaque (LDright n)), 1
5054 | 0xff52 -> (* up *)
5055 Some (findlink opaque (LDup n)), -1
5057 | 0xff54 -> (* down *)
5058 Some (findlink opaque (LDdown n)), 1
5060 | _ -> None, 0
5062 let pwl l dir =
5063 begin match findpwl l.pageno dir with
5064 | Pwlnotfound -> ()
5065 | Pwl pageno ->
5066 let notfound dir =
5067 state.mode <- LinkNav (Ltgendir dir);
5068 let y, h = getpageyh pageno in
5069 let y =
5070 if dir < 0
5071 then y + h - state.winh
5072 else y
5074 gotoy y
5076 begin match getopaque pageno, getpage pageno with
5077 | Some opaque, Some _ ->
5078 let link =
5079 let ld = if dir > 0 then LDfirst else LDlast in
5080 findlink opaque ld
5082 begin match link with
5083 | Lfound m ->
5084 showlinktype (getlink opaque m);
5085 state.mode <- LinkNav (Ltexact (pageno, m));
5086 G.postRedisplay "linknav jpage";
5087 | _ -> notfound dir
5088 end;
5089 | _ -> notfound dir
5090 end;
5091 end;
5093 begin match opt with
5094 | Some Lnotfound -> pwl l dir;
5095 | Some (Lfound m) ->
5096 if m = n
5097 then pwl l dir
5098 else (
5099 let _, y0, _, y1 = getlinkrect opaque m in
5100 if y0 < l.pagey
5101 then gotopage1 l.pageno y0
5102 else (
5103 let d = fstate.fontsize + 1 in
5104 if y1 - l.pagey > l.pagevh - d
5105 then gotopage1 l.pageno (y1 - state.winh - hscrollh () + d)
5106 else G.postRedisplay "linknav";
5108 showlinktype (getlink opaque m);
5109 state.mode <- LinkNav (Ltexact (l.pageno, m));
5112 | None -> viewkeyboard key mask
5113 end;
5114 | _ -> viewkeyboard key mask
5116 if key = 0xff63
5117 then (
5118 state.mode <- View;
5119 G.postRedisplay "leave linknav"
5121 else
5122 match linknav with
5123 | Ltgendir _ -> viewkeyboard key mask
5124 | Ltexact exact -> doexact exact
5127 let keyboard key mask =
5128 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5129 then wcmd "interrupt"
5130 else state.uioh <- state.uioh#key key mask
5133 let birdseyekeyboard key mask
5134 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5135 let incr =
5136 match conf.columns with
5137 | Csingle _ -> 1
5138 | Cmulti ((c, _, _), _) -> c
5139 | Csplit _ -> failwith "bird's eye split mode"
5141 let pgh layout = List.fold_left (fun m l -> max l.pageh m) state.winh layout in
5142 match key with
5143 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5144 let y, h = getpageyh pageno in
5145 let top = (state.winh - h) / 2 in
5146 gotoy (max 0 (y - top))
5147 | 0xff0d (* enter *)
5148 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5149 | 0xff1b -> leavebirdseye beye true (* escape *)
5150 | 0xff52 -> upbirdseye incr beye (* up *)
5151 | 0xff54 -> downbirdseye incr beye (* down *)
5152 | 0xff51 -> upbirdseye 1 beye (* left *)
5153 | 0xff53 -> downbirdseye 1 beye (* right *)
5155 | 0xff55 -> (* prior *)
5156 begin match state.layout with
5157 | l :: _ ->
5158 if l.pagey != 0
5159 then (
5160 state.mode <- Birdseye (
5161 oconf, leftx, l.pageno, hooverpageno, anchor
5163 gotopage1 l.pageno 0;
5165 else (
5166 let layout = layout (state.y-state.winh) (pgh state.layout) in
5167 match layout with
5168 | [] -> gotoy (clamp (-state.winh))
5169 | l :: _ ->
5170 state.mode <- Birdseye (
5171 oconf, leftx, l.pageno, hooverpageno, anchor
5173 gotopage1 l.pageno 0
5176 | [] -> gotoy (clamp (-state.winh))
5177 end;
5179 | 0xff56 -> (* next *)
5180 begin match List.rev state.layout with
5181 | l :: _ ->
5182 let layout = layout (state.y + (pgh state.layout)) state.winh in
5183 begin match layout with
5184 | [] ->
5185 let incr = l.pageh - l.pagevh in
5186 if incr = 0
5187 then (
5188 state.mode <-
5189 Birdseye (
5190 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5192 G.postRedisplay "birdseye pagedown";
5194 else gotoy (clamp (incr + conf.interpagespace*2));
5196 | l :: _ ->
5197 state.mode <-
5198 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5199 gotopage1 l.pageno 0;
5202 | [] -> gotoy (clamp state.winh)
5203 end;
5205 | 0xff50 -> (* home *)
5206 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5207 gotopage1 0 0
5209 | 0xff57 -> (* end *)
5210 let pageno = state.pagecount - 1 in
5211 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5212 if not (pagevisible state.layout pageno)
5213 then
5214 let h =
5215 match List.rev state.pdims with
5216 | [] -> state.winh
5217 | (_, _, h, _) :: _ -> h
5219 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5220 else G.postRedisplay "birdseye end";
5221 | _ -> viewkeyboard key mask
5224 let drawpage l =
5225 let color =
5226 match state.mode with
5227 | Textentry _ -> scalecolor 0.4
5228 | LinkNav _
5229 | View -> scalecolor 1.0
5230 | Birdseye (_, _, pageno, hooverpageno, _) ->
5231 if l.pageno = hooverpageno
5232 then scalecolor 0.9
5233 else (
5234 if l.pageno = pageno
5235 then scalecolor 1.0
5236 else scalecolor 0.8
5239 drawtiles l color;
5242 let postdrawpage l linkindexbase =
5243 match getopaque l.pageno with
5244 | Some opaque ->
5245 if tileready l l.pagex l.pagey
5246 then
5247 let x = l.pagedispx - l.pagex
5248 + (if conf.leftscroll then conf.scrollbw else 0)
5249 and y = l.pagedispy - l.pagey in
5250 let hlmask =
5251 match conf.columns with
5252 | Csingle _ | Cmulti _ ->
5253 (if conf.hlinks then 1 else 0)
5254 + (if state.glinks
5255 && not (isbirdseye state.mode) then 2 else 0)
5256 | _ -> 0
5258 let s =
5259 match state.mode with
5260 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5261 | _ -> E.s
5263 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5264 else 0
5265 | _ -> 0
5268 let scrollindicator () =
5269 let sbw, ph, sh = state.uioh#scrollph in
5270 let sbh, pw, sw = state.uioh#scrollpw in
5272 let x0,x1 =
5273 if conf.leftscroll
5274 then (0, sbw)
5275 else (state.winw - sbw), state.winw
5278 GlDraw.color (0.64, 0.64, 0.64);
5279 filledrect (float x0) 0. (float x1) (float state.winh);
5280 filledrect
5281 0. (float (state.winh - sbh))
5282 (float (wadjsb state.winw - 1)) (float state.winh)
5284 GlDraw.color (0.0, 0.0, 0.0);
5286 filledrect (float x0) ph (float x1) (ph +. sh);
5287 filledrect pw (float (state.winh - sbh)) (pw +. sw) (float state.winh);
5290 let showsel () =
5291 match state.mstate with
5292 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5295 | Msel ((x0, y0), (x1, y1)) ->
5296 let identify opaque l px py = Some (opaque, l.pageno, px, py) in
5297 let o0,n0,px0,py0 = onppundermouse identify x0 y0 (~< E.s, -1, 0, 0) in
5298 let _o1,n1,px1,py1 = onppundermouse identify x1 y1 (~< E.s, -1, 0, 0) in
5299 if n0 != -1 && n0 = n1 then seltext o0 (px0, py0, px1, py1);
5302 let showrects = function [] -> () | rects ->
5303 Gl.enable `blend;
5304 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5305 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5306 List.iter
5307 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5308 List.iter (fun l ->
5309 if l.pageno = pageno
5310 then (
5311 let dx = float (l.pagedispx - l.pagex) in
5312 let dy = float (l.pagedispy - l.pagey) in
5313 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5314 Raw.sets_float state.vraw ~pos:0
5315 [| x0+.dx; y0+.dy;
5316 x1+.dx; y1+.dy;
5317 x3+.dx; y3+.dy;
5318 x2+.dx; y2+.dy |];
5319 GlArray.vertex `two state.vraw;
5320 GlArray.draw_arrays `triangle_strip 0 4;
5322 ) state.layout
5323 ) rects
5325 Gl.disable `blend;
5328 let display () =
5329 GlClear.color (scalecolor2 conf.bgcolor);
5330 GlClear.clear [`color];
5331 List.iter drawpage state.layout;
5332 let rects =
5333 match state.mode with
5334 | LinkNav (Ltexact (pageno, linkno)) ->
5335 begin match getopaque pageno with
5336 | Some opaque ->
5337 let dx = if conf.leftscroll then conf.scrollbw else 0 in
5338 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5339 let x0 = x0 + dx and x1 = x1 + dx in
5340 (pageno, 5, (
5341 float x0, float y0,
5342 float x1, float y0,
5343 float x1, float y1,
5344 float x0, float y1)
5345 ) :: state.rects
5346 | None -> state.rects
5348 | _ -> state.rects
5350 showrects rects;
5351 let rec postloop linkindexbase = function
5352 | l :: rest ->
5353 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5354 postloop linkindexbase rest
5355 | [] -> ()
5357 showsel ();
5358 postloop 0 state.layout;
5359 state.uioh#display;
5360 begin match state.mstate with
5361 | Mzoomrect ((x0, y0), (x1, y1)) ->
5362 Gl.enable `blend;
5363 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5364 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5365 filledrect (float x0) (float y0) (float x1) (float y1);
5366 Gl.disable `blend;
5367 | _ -> ()
5368 end;
5369 enttext ();
5370 scrollindicator ();
5371 Wsi.swapb ();
5374 let zoomrect x y x1 y1 =
5375 let x0 = min x x1
5376 and x1 = max x x1
5377 and y0 = min y y1 in
5378 gotoy (state.y + y0);
5379 state.anchor <- getanchor ();
5380 let zoom = (float state.w) /. float (x1 - x0) in
5381 let margin =
5382 match conf.fitmodel, conf.columns with
5383 | FitPage, Csplit _ ->
5384 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
5386 | _, _ ->
5387 let adjw = wadjsb state.winw in
5388 if state.w < adjw
5389 then (adjw - state.w) / 2
5390 else 0
5392 state.x <- (state.x + margin) - x0;
5393 setzoom zoom;
5394 resetmstate ();
5397 let zoomblock x y =
5398 let g opaque l px py =
5399 match rectofblock opaque px py with
5400 | Some a ->
5401 let x0 = a.(0) -. 20. in
5402 let x1 = a.(1) +. 20. in
5403 let y0 = a.(2) -. 20. in
5404 let zoom = (float state.w) /. (x1 -. x0) in
5405 let pagey = getpagey l.pageno in
5406 gotoy_and_clear_text (pagey + truncate y0);
5407 state.anchor <- getanchor ();
5408 let margin = (state.w - l.pagew)/2 in
5409 state.x <- -truncate x0 - margin;
5410 setzoom zoom;
5411 None
5412 | None -> None
5414 match conf.columns with
5415 | Csplit _ ->
5416 showtext '!' "block zooming does not work properly in split columns mode"
5417 | _ -> onppundermouse g x y ()
5420 let scrollx x =
5421 let winw = wadjsb state.winw - 1 in
5422 let s = float x /. float winw in
5423 let destx = truncate (float (state.w + winw) *. s) in
5424 state.x <- winw - destx;
5425 gotoy_and_clear_text state.y;
5426 state.mstate <- Mscrollx;
5429 let scrolly y =
5430 let s = float y /. float state.winh in
5431 let desty = truncate (float (state.maxy - state.winh) *. s) in
5432 gotoy_and_clear_text desty;
5433 state.mstate <- Mscrolly;
5436 let viewmulticlick clicks x y mask =
5437 let g opaque l px py =
5438 let mark =
5439 match clicks with
5440 | 2 -> Mark_word
5441 | 3 -> Mark_line
5442 | 4 -> Mark_block
5443 | _ -> Mark_page
5445 if markunder opaque px py mark
5446 then (
5447 Some (fun () ->
5448 let dopipe cmd =
5449 match getopaque l.pageno with
5450 | None -> ()
5451 | Some opaque -> pipesel opaque cmd
5453 state.roam <- (fun () -> dopipe conf.paxcmd);
5454 if not (Wsi.withctrl mask) then dopipe conf.selcmd;
5457 else None
5459 G.postRedisplay "viewmulticlick";
5460 onppundermouse g x y (fun () -> showtext '!' "Nothing to select") ();
5463 let canselect () =
5464 match conf.columns with
5465 | Csplit _ -> false
5466 | Csingle _ | Cmulti _ -> conf.angle mod 360 = 0
5469 let viewmouse button down x y mask =
5470 match button with
5471 | n when (n == 4 || n == 5) && not down ->
5472 if Wsi.withctrl mask
5473 then (
5474 match state.mstate with
5475 | Mzoom (oldn, i) ->
5476 if oldn = n
5477 then (
5478 if i = 2
5479 then
5480 let incr =
5481 match n with
5482 | 5 ->
5483 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5484 | _ ->
5485 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5487 let zoom = conf.zoom -. incr in
5488 setzoom zoom;
5489 state.mstate <- Mzoom (n, 0);
5490 else
5491 state.mstate <- Mzoom (n, i+1);
5493 else state.mstate <- Mzoom (n, 0)
5495 | _ -> state.mstate <- Mzoom (n, 0)
5497 else (
5498 match state.autoscroll with
5499 | Some step -> setautoscrollspeed step (n=4)
5500 | None ->
5501 if conf.wheelbypage || conf.presentation
5502 then (
5503 if n = 4
5504 then prevpage ()
5505 else nextpage ()
5507 else
5508 let incr =
5509 if n = 4
5510 then -conf.scrollstep
5511 else conf.scrollstep
5513 let incr = incr * 2 in
5514 let y = clamp incr in
5515 gotoy_and_clear_text y
5518 | n when (n = 6 || n = 7) && not down && canpan () ->
5519 state.x <-
5520 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
5521 gotoy_and_clear_text state.y
5523 | 1 when Wsi.withshift mask ->
5524 state.mstate <- Mnone;
5525 if not down
5526 then (
5527 match unproject x y with
5528 | Some (pageno, ux, uy) ->
5529 let cmd = Printf.sprintf
5530 "%s %s %d %d %d"
5531 conf.stcmd state.path pageno ux uy
5533 popen cmd []
5534 | None -> ()
5537 | 1 when Wsi.withctrl mask ->
5538 if down
5539 then (
5540 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5541 state.mstate <- Mpan (x, y)
5543 else
5544 state.mstate <- Mnone
5546 | 3 ->
5547 if down
5548 then (
5549 Wsi.setcursor Wsi.CURSOR_CYCLE;
5550 let p = (x, y) in
5551 state.mstate <- Mzoomrect (p, p)
5553 else (
5554 match state.mstate with
5555 | Mzoomrect ((x0, y0), _) ->
5556 if abs (x-x0) > 10 && abs (y - y0) > 10
5557 then zoomrect x0 y0 x y
5558 else (
5559 resetmstate ();
5560 G.postRedisplay "kill accidental zoom rect";
5562 | _ ->
5563 resetmstate ()
5566 | 1 when x > state.winw - vscrollw () ->
5567 if down
5568 then
5569 let _, position, sh = state.uioh#scrollph in
5570 if y > truncate position && y < truncate (position +. sh)
5571 then state.mstate <- Mscrolly
5572 else scrolly y
5573 else
5574 state.mstate <- Mnone
5576 | 1 when y > state.winh - hscrollh () ->
5577 if down
5578 then
5579 let _, position, sw = state.uioh#scrollpw in
5580 if x > truncate position && x < truncate (position +. sw)
5581 then state.mstate <- Mscrollx
5582 else scrollx x
5583 else
5584 state.mstate <- Mnone
5586 | 1 when state.bzoom -> if not down then zoomblock x y
5588 | 1 ->
5589 let dest = if down then getunder x y else Unone in
5590 begin match dest with
5591 | Ulinkgoto _
5592 | Ulinkuri _
5593 | Uremote _ | Uremotedest _
5594 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5595 gotounder dest
5597 | Unone when down ->
5598 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5599 state.mstate <- Mpan (x, y);
5601 | Unone | Utext _ ->
5602 if down
5603 then (
5604 if canselect ()
5605 then (
5606 state.mstate <- Msel ((x, y), (x, y));
5607 G.postRedisplay "mouse select";
5610 else (
5611 match state.mstate with
5612 | Mnone -> ()
5614 | Mzoom _ | Mscrollx | Mscrolly ->
5615 state.mstate <- Mnone
5617 | Mzoomrect ((x0, y0), _) ->
5618 zoomrect x0 y0 x y
5620 | Mpan _ ->
5621 Wsi.setcursor Wsi.CURSOR_INHERIT;
5622 state.mstate <- Mnone
5624 | Msel ((x0, y0), (x1, y1)) ->
5625 let rec loop = function
5626 | [] -> ()
5627 | l :: rest ->
5628 let inside =
5629 let a0 = l.pagedispy in
5630 let a1 = a0 + l.pagevh in
5631 let b0 = l.pagedispx in
5632 let b1 = b0 + l.pagevw in
5633 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5634 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5636 if inside
5637 then
5638 match getopaque l.pageno with
5639 | Some opaque ->
5640 let dosel cmd () =
5641 match Ne.res Unix.pipe with
5642 | Ne.Exn exn ->
5643 showtext '!'
5644 (Printf.sprintf
5645 "can not create sel pipe: %s"
5646 (exntos exn));
5647 | Ne.Res (r, w) ->
5648 let clo what fd =
5649 Ne.clo fd (fun msg ->
5650 dolog "%s close failed: %s" what msg)
5652 let popened =
5653 try popen cmd [r, 0; w, -1]; true
5654 with exn ->
5655 dolog "can not execute %S: %s"
5656 cmd (exntos exn);
5657 false
5659 if popened
5660 then (
5661 copysel w opaque;
5662 G.postRedisplay "copysel";
5664 else clo "Msel pipe/w" w;
5665 clo "Msel pipe/r" r;
5667 dosel conf.selcmd ();
5668 state.roam <- dosel conf.paxcmd;
5669 | None -> ()
5670 else loop rest
5672 loop state.layout;
5673 resetmstate ();
5677 | _ -> ()
5680 let birdseyemouse button down x y mask
5681 (conf, leftx, _, hooverpageno, anchor) =
5682 match button with
5683 | 1 when down ->
5684 let rec loop = function
5685 | [] -> ()
5686 | l :: rest ->
5687 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5688 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5689 then (
5690 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5692 else loop rest
5694 loop state.layout
5695 | 3 -> ()
5696 | _ -> viewmouse button down x y mask
5699 let uioh = object
5700 method display = ()
5702 method key key mask =
5703 begin match state.mode with
5704 | Textentry textentry -> textentrykeyboard key mask textentry
5705 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5706 | View -> viewkeyboard key mask
5707 | LinkNav linknav -> linknavkeyboard key mask linknav
5708 end;
5709 state.uioh
5711 method button button bstate x y mask =
5712 begin match state.mode with
5713 | LinkNav _
5714 | View -> viewmouse button bstate x y mask
5715 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5716 | Textentry _ -> ()
5717 end;
5718 state.uioh
5720 method multiclick clicks x y mask =
5721 begin match state.mode with
5722 | LinkNav _
5723 | View -> viewmulticlick clicks x y mask
5724 | Birdseye _
5725 | Textentry _ -> ()
5726 end;
5727 state.uioh
5729 method motion x y =
5730 begin match state.mode with
5731 | Textentry _ -> ()
5732 | View | Birdseye _ | LinkNav _ ->
5733 match state.mstate with
5734 | Mzoom _ | Mnone -> ()
5736 | Mpan (x0, y0) ->
5737 let dx = x - x0
5738 and dy = y0 - y in
5739 state.mstate <- Mpan (x, y);
5740 if canpan ()
5741 then state.x <- panbound (state.x + dx);
5742 let y = clamp dy in
5743 gotoy_and_clear_text y
5745 | Msel (a, _) ->
5746 state.mstate <- Msel (a, (x, y));
5747 G.postRedisplay "motion select";
5749 | Mscrolly ->
5750 let y = min state.winh (max 0 y) in
5751 scrolly y
5753 | Mscrollx ->
5754 let x = min state.winw (max 0 x) in
5755 scrollx x
5757 | Mzoomrect (p0, _) ->
5758 state.mstate <- Mzoomrect (p0, (x, y));
5759 G.postRedisplay "motion zoomrect";
5760 end;
5761 state.uioh
5763 method pmotion x y =
5764 begin match state.mode with
5765 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5766 let rec loop = function
5767 | [] ->
5768 if hooverpageno != -1
5769 then (
5770 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5771 G.postRedisplay "pmotion birdseye no hoover";
5773 | l :: rest ->
5774 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5775 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5776 then (
5777 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5778 G.postRedisplay "pmotion birdseye hoover";
5780 else loop rest
5782 loop state.layout
5784 | Textentry _ -> ()
5786 | LinkNav _
5787 | View ->
5788 match state.mstate with
5789 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5791 | Mnone ->
5792 updateunder x y;
5793 if canselect ()
5794 then
5795 match conf.pax with
5796 | None -> ()
5797 | Some r ->
5798 let past, _, _ = !r in
5799 let now = now () in
5800 let delta = now -. past in
5801 if delta > 0.01
5802 then paxunder x y
5803 else r := (now, x, y)
5804 end;
5805 state.uioh
5807 method infochanged _ = ()
5809 method scrollph =
5810 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
5811 let p, h =
5812 if maxy = 0
5813 then 0.0, float state.winh
5814 else scrollph state.y maxy
5816 vscrollw (), p, h
5818 method scrollpw =
5819 let winw = wadjsb state.winw in
5820 let fwinw = float winw in
5821 let sw =
5822 let sw = fwinw /. float state.w in
5823 let sw = fwinw *. sw in
5824 max sw (float conf.scrollh)
5826 let position =
5827 let maxx = state.w + winw in
5828 let x = winw - state.x in
5829 let percent = float x /. float maxx in
5830 (fwinw -. sw) *. percent
5832 hscrollh (), position, sw
5834 method modehash =
5835 let modename =
5836 match state.mode with
5837 | LinkNav _ -> "links"
5838 | Textentry _ -> "textentry"
5839 | Birdseye _ -> "birdseye"
5840 | View -> "view"
5842 findkeyhash conf modename
5844 method eformsgs = true
5845 end;;
5847 let adderrmsg src msg =
5848 Buffer.add_string state.errmsgs msg;
5849 state.newerrmsgs <- true;
5850 G.postRedisplay src
5853 let adderrfmt src fmt =
5854 Format.kprintf (fun s -> adderrmsg src s) fmt;
5857 let ract cmds =
5858 let cl = splitatspace cmds in
5859 let scan s fmt f =
5860 try Scanf.sscanf s fmt f
5861 with exn ->
5862 adderrfmt "remote exec"
5863 "error processing '%S': %s\n" cmds (exntos exn)
5865 match cl with
5866 | "reload" :: [] -> reload ()
5867 | "goto" :: args :: [] ->
5868 scan args "%u %f %f"
5869 (fun pageno x y ->
5870 let cmd, _ = state.geomcmds in
5871 if emptystr cmd
5872 then gotopagexy pageno x y
5873 else
5874 let f prevf () =
5875 gotopagexy pageno x y;
5876 prevf ()
5878 state.reprf <- f state.reprf
5880 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
5881 | "gotor" :: args :: [] ->
5882 scan args "%S %u"
5883 (fun filename pageno -> gotounder (Uremote (filename, pageno)))
5884 | "gotord" :: args :: [] ->
5885 scan args "%S %S"
5886 (fun filename dest -> gotounder (Uremotedest (filename, dest)))
5887 | "rect" :: args :: [] ->
5888 scan args "%u %u %f %f %f %f"
5889 (fun pageno color x0 y0 x1 y1 ->
5890 onpagerect pageno (fun w h ->
5891 let _,w1,h1,_ = getpagedim pageno in
5892 let sw = float w1 /. float w
5893 and sh = float h1 /. float h in
5894 let x0s = x0 *. sw
5895 and x1s = x1 *. sw
5896 and y0s = y0 *. sh
5897 and y1s = y1 *. sh in
5898 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
5899 debugrect rect;
5900 state.rects <- (pageno, color, rect) :: state.rects;
5901 G.postRedisplay "rect";
5904 | "activatewin" :: [] -> Wsi.activatewin ()
5905 | "quit" :: [] -> raise Quit
5906 | _ ->
5907 adderrfmt "remote command"
5908 "error processing remote command: %S\n" cmds;
5911 let remote =
5912 let scratch = String.create 80 in
5913 let buf = Buffer.create 80 in
5914 fun fd ->
5915 let rec tempfr () =
5916 try Some (Unix.read fd scratch 0 80)
5917 with
5918 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
5919 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
5920 | exn -> raise exn
5922 match tempfr () with
5923 | None -> Some fd
5924 | Some n ->
5925 if n = 0
5926 then (
5927 Unix.close fd;
5928 if Buffer.length buf > 0
5929 then (
5930 let s = Buffer.contents buf in
5931 Buffer.clear buf;
5932 ract s;
5934 None
5936 else
5937 let rec eat ppos =
5938 let nlpos =
5940 let pos = String.index_from scratch ppos '\n' in
5941 if pos >= n then -1 else pos
5942 with Not_found -> -1
5944 if nlpos >= 0
5945 then (
5946 Buffer.add_substring buf scratch ppos (nlpos-ppos);
5947 let s = Buffer.contents buf in
5948 Buffer.clear buf;
5949 ract s;
5950 eat (nlpos+1);
5952 else (
5953 Buffer.add_substring buf scratch ppos (n-ppos);
5954 Some fd
5956 in eat 0
5959 let remoteopen path =
5960 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
5961 with exn ->
5962 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
5963 None
5966 let () =
5967 let trimcachepath = ref E.s in
5968 let rcmdpath = ref E.s in
5969 let pageno = ref None in
5970 let histmode = ref false in
5971 selfexec := Sys.executable_name;
5972 Arg.parse
5973 (Arg.align
5974 [("-p", Arg.String (fun s -> state.password <- s),
5975 "<password> Set password");
5977 ("-f", Arg.String
5978 (fun s ->
5979 Config.fontpath := s;
5980 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
5982 "<path> Set path to the user interface font");
5984 ("-c", Arg.String
5985 (fun s ->
5986 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
5987 Config.confpath := s),
5988 "<path> Set path to the configuration file");
5990 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
5991 "<page-number> Jump to page");
5993 ("-tcf", Arg.String (fun s -> trimcachepath := s),
5994 "<path> Set path to the trim cache file");
5996 ("-dest", Arg.String (fun s -> state.nameddest <- s),
5997 "<named-destination> Set named destination");
5999 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
6000 ("-cxack", Arg.Set cxack, " Cut corners");
6002 ("-remote", Arg.String (fun s -> rcmdpath := s),
6003 "<path> Set path to the remote commands source");
6005 ("-origin", Arg.String (fun s -> state.origin <- s),
6006 "<original-path> Set original path");
6008 ("-hist", Arg.Set histmode, " Browse history");
6010 ("-v", Arg.Unit (fun () ->
6011 Printf.printf
6012 "%s\nconfiguration path: %s\n"
6013 (version ())
6014 Config.defconfpath
6016 exit 0), " Print version and exit");
6019 (fun s -> state.path <- s)
6020 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6022 if !wtmode
6023 then selfexec := !selfexec ^ " -wtmode";
6025 if emptystr state.path
6026 then (if not !histmode then (prerr_endline "file name missing"; exit 1))
6027 else (if !histmode then (prerr_endline "extra file name"; exit 1));
6029 if not (Config.load ())
6030 then prerr_endline "failed to load configuration";
6031 begin match !pageno with
6032 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
6033 | None -> ()
6034 end;
6036 let wsfd, winw, winh = Wsi.init (object (self)
6037 val mutable m_hack = false
6038 val mutable m_clicks = 0
6039 val mutable m_click_x = 0
6040 val mutable m_click_y = 0
6041 val mutable m_lastclicktime = infinity
6043 method private cleanup =
6044 state.roam <- noroam;
6045 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap;
6046 method expose = if not m_hack then G.postRedisplay "expose"
6047 method visible = G.postRedisplay "visible"
6048 method display = m_hack <- false; display ()
6049 method reshape w h =
6050 self#cleanup;
6051 m_hack <- w < state.winw && h < state.winh;
6052 reshape w h
6053 method mouse b d x y m =
6054 if d && canselect ()
6055 then (
6056 (* http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx *)
6057 m_click_x <- x;
6058 m_click_y <- y;
6059 if b = 1
6060 then (
6061 let t = now () in
6062 if abs x - m_click_x > 10
6063 || abs y - m_click_y > 10
6064 || abs_float (t -. m_lastclicktime) > 0.3
6065 then m_clicks <- 0;
6066 m_clicks <- m_clicks + 1;
6067 m_lastclicktime <- t;
6068 if m_clicks = 1
6069 then (
6070 self#cleanup;
6071 G.postRedisplay "cleanup";
6072 state.uioh <- state.uioh#button b d x y m;
6074 else state.uioh <- state.uioh#multiclick m_clicks x y m
6076 else (
6077 self#cleanup;
6078 m_clicks <- 0;
6079 m_lastclicktime <- infinity;
6080 state.uioh <- state.uioh#button b d x y m
6083 else (
6084 state.uioh <- state.uioh#button b d x y m
6086 method motion x y =
6087 state.mpos <- (x, y);
6088 state.uioh <- state.uioh#motion x y
6089 method pmotion x y =
6090 state.mpos <- (x, y);
6091 state.uioh <- state.uioh#pmotion x y
6092 method key k m =
6093 let mascm = m land (
6094 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6095 ) in
6096 let keyboard k m =
6097 let x = state.x and y = state.y in
6098 keyboard k m;
6099 if x != state.x || y != state.y then self#cleanup
6101 match state.keystate with
6102 | KSnone ->
6103 let km = k, mascm in
6104 begin
6105 match
6106 let modehash = state.uioh#modehash in
6107 try Hashtbl.find modehash km
6108 with Not_found ->
6109 try Hashtbl.find (findkeyhash conf "global") km
6110 with Not_found -> KMinsrt (k, m)
6111 with
6112 | KMinsrt (k, m) -> keyboard k m
6113 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6114 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6116 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6117 List.iter (fun (k, m) -> keyboard k m) insrt;
6118 state.keystate <- KSnone
6119 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6120 state.keystate <- KSinto (keys, insrt)
6121 | _ ->
6122 state.keystate <- KSnone
6124 method enter x y =
6125 state.mpos <- (x, y);
6126 state.uioh <- state.uioh#pmotion x y
6127 method leave = state.mpos <- (-1, -1)
6128 method winstate wsl = state.winstate <- wsl; m_hack <- false
6129 method quit = raise Quit
6130 end) conf.cwinw conf.cwinh (platform = Posx) in
6132 state.wsfd <- wsfd;
6134 if not (
6135 List.exists GlMisc.check_extension
6136 [ "GL_ARB_texture_rectangle"
6137 ; "GL_EXT_texture_recangle"
6138 ; "GL_NV_texture_rectangle" ]
6140 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6142 if (
6143 let r = GlMisc.get_string `renderer in
6144 let p = "Mesa DRI Intel(" in
6145 let l = String.length p in
6146 String.length r > l && String.sub r 0 l = p
6148 then (
6149 defconf.sliceheight <- 1024;
6150 defconf.texcount <- 32;
6151 defconf.usepbo <- true;
6154 let cr, sw =
6155 match Ne.res Unix.pipe with
6156 | Ne.Exn exn ->
6157 Printf.eprintf "pipe/crsw failed: %s" (exntos exn);
6158 exit 1
6159 | Ne.Res rw -> rw
6160 and sr, cw =
6161 match Ne.res Unix.pipe with
6162 | Ne.Exn exn ->
6163 Printf.eprintf "pipe/srcw failed: %s" (exntos exn);
6164 exit 1
6165 | Ne.Res rw -> rw
6168 cloexec cr;
6169 cloexec sw;
6170 cloexec sr;
6171 cloexec cw;
6173 setcheckers conf.checkers;
6174 redirectstderr ();
6175 if conf.redirectstderr
6176 then
6177 at_exit (fun () ->
6178 let s = Buffer.contents state.errmsgs ^
6179 (match state.errfd with
6180 | Some fd ->
6181 let s = String.create (80*24) in
6182 let n =
6184 let r, _, _ = Unix.select [fd] [] [] 0.0 in
6185 if List.mem fd r
6186 then Unix.read fd s 0 (String.length s)
6187 else 0
6188 with _ -> 0
6190 if n = 0
6191 then E.s
6192 else String.sub s 0 n
6193 | None -> E.s
6196 try ignore (Unix.write state.stderr s 0 (String.length s))
6197 with exn -> print_endline (exntos exn)
6201 init (cr, cw) (
6202 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
6203 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6204 !Config.fontpath, !trimcachepath,
6205 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
6207 List.iter GlArray.enable [`texture_coord; `vertex];
6208 state.sr <- sr;
6209 state.sw <- sw;
6210 reshape winw winh;
6211 if !histmode
6212 then (
6213 state.uioh <- uioh;
6214 enterhistmode ();
6216 else (
6217 state.text <- "Opening " ^ (mbtoutf8 state.path);
6218 opendoc state.path state.password;
6219 state.uioh <- uioh;
6221 display ();
6222 Wsi.mapwin ();
6223 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
6224 let optrfd =
6225 ref (
6226 if nonemptystr !rcmdpath
6227 then remoteopen !rcmdpath
6228 else None
6232 let rec loop deadline =
6233 let r =
6234 match state.errfd with
6235 | None -> [state.sr; state.wsfd]
6236 | Some fd -> [state.sr; state.wsfd; fd]
6238 let r =
6239 match !optrfd with
6240 | None -> r
6241 | Some fd -> fd :: r
6243 if state.redisplay
6244 then (
6245 state.redisplay <- false;
6246 display ();
6248 let timeout =
6249 let now = now () in
6250 if deadline > now
6251 then (
6252 if deadline = infinity
6253 then ~-.1.0
6254 else max 0.0 (deadline -. now)
6256 else 0.0
6258 let r, _, _ =
6259 try Unix.select r [] [] timeout
6260 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6262 begin match r with
6263 | [] ->
6264 state.ghyll None;
6265 let newdeadline =
6266 if state.ghyll == noghyll
6267 then
6268 match state.autoscroll with
6269 | Some step when step != 0 ->
6270 let y = state.y + step in
6271 let y =
6272 if y < 0
6273 then state.maxy
6274 else if y >= state.maxy then 0 else y
6276 gotoy y;
6277 if state.mode = View
6278 then state.text <- E.s;
6279 deadline +. 0.01
6280 | _ -> infinity
6281 else deadline +. 0.01
6283 loop newdeadline
6285 | l ->
6286 let rec checkfds = function
6287 | [] -> ()
6288 | fd :: rest when fd = state.sr ->
6289 let cmd = readcmd state.sr in
6290 act cmd;
6291 checkfds rest
6293 | fd :: rest when fd = state.wsfd ->
6294 Wsi.readresp fd;
6295 checkfds rest
6297 | fd :: rest when Some fd = !optrfd ->
6298 begin match remote fd with
6299 | None -> optrfd := remoteopen !rcmdpath;
6300 | opt -> optrfd := opt
6301 end;
6302 checkfds rest
6304 | fd :: rest ->
6305 let s = String.create 80 in
6306 let n = tempfailureretry (Unix.read fd s 0) 80 in
6307 if conf.redirectstderr
6308 then (
6309 Buffer.add_substring state.errmsgs s 0 n;
6310 state.newerrmsgs <- true;
6311 state.redisplay <- true;
6313 else (
6314 prerr_string (String.sub s 0 n);
6315 flush stderr;
6317 checkfds rest
6319 checkfds l;
6320 let newdeadline =
6321 let deadline1 =
6322 if deadline = infinity
6323 then now () +. 0.01
6324 else deadline
6326 match state.autoscroll with
6327 | Some step when step != 0 -> deadline1
6328 | _ -> if state.ghyll == noghyll then infinity else deadline1
6330 loop newdeadline
6331 end;
6334 loop infinity;
6335 with Quit ->
6336 Config.save leavebirdseye;