...
[llpp.git] / main.ml
blob5d5b34f007a907f1d0c684552454de42cb32c1bf
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 reeenterhist = ref false;;
43 let selfexec = ref E.s;;
45 let drawstring size x y s =
46 Gl.enable `blend;
47 Gl.enable `texture_2d;
48 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
49 ignore (drawstr size x y s);
50 Gl.disable `blend;
51 Gl.disable `texture_2d;
54 let drawstring1 size x y s =
55 drawstr size x y s;
58 let drawstring2 size x y fmt =
59 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
62 let debugl l =
63 dolog "l %d dim=%d {" l.pageno l.pagedimno;
64 dolog " WxH %dx%d" l.pagew l.pageh;
65 dolog " vWxH %dx%d" l.pagevw l.pagevh;
66 dolog " pagex,y %d,%d" l.pagex l.pagey;
67 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
68 dolog " column %d" l.pagecol;
69 dolog "}";
72 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
73 dolog "rect {";
74 dolog " x0,y0=(% f, % f)" x0 y0;
75 dolog " x1,y1=(% f, % f)" x1 y1;
76 dolog " x2,y2=(% f, % f)" x2 y2;
77 dolog " x3,y3=(% f, % f)" x3 y3;
78 dolog "}";
81 let isbirdseye = function Birdseye _ -> true | _ -> false;;
82 let istextentry = function Textentry _ -> true | _ -> false;;
84 let wtmode = ref false;;
85 let cxack = ref false;;
87 let pgscale h = truncate (float h *. conf.pgscale);;
89 let hscrollh () =
90 if (conf.scrollb land scrollbhv = 0)
91 || (state.x = 0 && state.w <= state.winw - conf.scrollbw)
92 then 0
93 else conf.scrollbw
96 let vscrollw () =
97 if (conf.scrollb land scrollbvv = 0)
98 then 0
99 else conf.scrollbw
102 let wadjsb w = w - vscrollw ();;
103 let xadjsb x = if conf.leftscroll then x + vscrollw () else x;;
105 let setfontsize n =
106 fstate.fontsize <- n;
107 fstate.wwidth <- measurestr fstate.fontsize "w";
108 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
111 let vlog fmt =
112 if conf.verbose
113 then
114 Printf.kprintf prerr_endline fmt
115 else
116 Printf.kprintf ignore fmt
119 let launchpath () =
120 if emptystr conf.pathlauncher
121 then print_endline state.path
122 else (
123 let re = Str.regexp "%s" in
124 let command = Str.global_replace re state.path conf.pathlauncher in
125 try popen command []
126 with exn ->
127 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
128 flush stderr;
132 module Ne = struct
133 type 'a t = | Res of 'a | Exn of exn;;
135 let res f =
136 try Res (f ())
137 with exn -> Exn exn
140 let clo fd f =
141 try tempfailureretry Unix.close fd
142 with exn -> f (exntos exn)
145 let dup fd =
146 try Res (tempfailureretry Unix.dup fd)
147 with exn -> Exn exn
150 let dup2 fd1 fd2 =
151 try Res (tempfailureretry (Unix.dup2 fd1) fd2)
152 with exn -> Exn exn
154 end;;
156 let redirectstderr () =
157 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
158 if conf.redirectstderr
159 then
160 match Ne.res Unix.pipe with
161 | Ne.Exn exn ->
162 dolog "failed to create stderr redirection pipes: %s" (exntos exn)
164 | Ne.Res (r, w) ->
165 begin match Ne.dup Unix.stderr with
166 | Ne.Exn exn ->
167 dolog "failed to dup stderr: %s" (exntos exn);
168 Ne.clo r (clofail "pipe/r");
169 Ne.clo w (clofail "pipe/w");
171 | Ne.Res dupstderr ->
172 begin match Ne.dup2 w Unix.stderr with
173 | Ne.Exn exn ->
174 dolog "failed to dup2 to stderr: %s" (exntos exn);
175 Ne.clo dupstderr (clofail "stderr duplicate");
176 Ne.clo r (clofail "redir pipe/r");
177 Ne.clo w (clofail "redir pipe/w");
179 | Ne.Res () ->
180 state.stderr <- dupstderr;
181 state.errfd <- Some r;
182 end;
184 else (
185 state.newerrmsgs <- false;
186 begin match state.errfd with
187 | Some fd ->
188 begin match Ne.dup2 state.stderr Unix.stderr with
189 | Ne.Exn exn ->
190 dolog "failed to dup2 original stderr: %s" (exntos exn)
191 | Ne.Res () ->
192 Ne.clo fd (clofail "dup of stderr");
193 state.errfd <- None;
194 end;
195 | None -> ()
196 end;
197 prerr_string (Buffer.contents state.errmsgs);
198 flush stderr;
199 Buffer.clear state.errmsgs;
203 module G =
204 struct
205 let postRedisplay who =
206 if conf.verbose
207 then prerr_endline ("redisplay for " ^ who);
208 state.redisplay <- true;
210 end;;
212 let getopaque pageno =
213 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
214 with Not_found -> None
217 let putopaque pageno opaque =
218 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
221 let pagetranslatepoint l x y =
222 let dy = y - l.pagedispy in
223 let y = dy + l.pagey in
224 let dx = x - l.pagedispx in
225 let x = dx + l.pagex in
226 (x, y);
229 let onppundermouse g x y d =
230 let rec f = function
231 | l :: rest ->
232 begin match getopaque l.pageno with
233 | Some opaque ->
234 let x0 = l.pagedispx in
235 let x1 = x0 + l.pagevw in
236 let y0 = l.pagedispy in
237 let y1 = y0 + l.pagevh in
238 if y >= y0 && y <= y1 && x >= x0 && x <= x1
239 then
240 let px, py = pagetranslatepoint l x y in
241 match g opaque l px py with
242 | Some res -> res
243 | None -> f rest
244 else f rest
245 | _ ->
246 f rest
248 | [] -> d
250 f state.layout
253 let getunder x y =
254 let g opaque l px py =
255 if state.bzoom
256 then (
257 match rectofblock opaque px py with
258 | Some a ->
259 let rect = (a.(0),a.(2),a.(1),a.(2),a.(1),a.(3),a.(0),a.(3)) in
260 state.rects <- [l.pageno, l.pageno mod 3, rect];
261 G.postRedisplay "getunder";
262 | None -> ()
264 match whatsunder opaque px py with
265 | Unone -> None
266 | under -> Some under
268 onppundermouse g x y Unone
271 let unproject x y =
272 let g opaque l x y =
273 match unproject opaque x y with
274 | Some (x, y) -> Some (Some (l.pageno, x, y))
275 | None -> None
277 onppundermouse g x y None;
280 let showtext c s =
281 state.text <- Printf.sprintf "%c%s" c s;
282 G.postRedisplay "showtext";
285 let pipesel opaque cmd =
286 if hassel opaque
287 then
288 match Ne.res Unix.pipe with
289 | Ne.Exn exn ->
290 showtext '!'
291 (Printf.sprintf "pipesel can not create pipe: %s" (exntos exn));
292 | Ne.Res (r, w) ->
293 let doclose what fd =
294 Ne.clo fd (fun msg -> dolog "%s close failed: %s" what msg)
296 let popened =
297 try popen cmd [r, 0; w, -1]; true
298 with exn ->
299 dolog "can not execute %S: %s" cmd (exntos exn);
300 false
302 if popened
303 then (
304 copysel w opaque;
305 G.postRedisplay "pipesel";
307 else doclose "pipesel pipe/w" w;
308 doclose "pipesel pipe/r" r;
311 let paxunder x y =
312 let g opaque l px py =
313 if markunder opaque px py conf.paxmark
314 then (
315 Some (fun () ->
316 match getopaque l.pageno with
317 | None -> ()
318 | Some opaque -> pipesel opaque conf.paxcmd
321 else None
323 G.postRedisplay "paxunder";
324 if conf.paxmark = Mark_page
325 then
326 List.iter (fun l ->
327 match getopaque l.pageno with
328 | None -> ()
329 | Some opaque -> clearmark opaque) state.layout;
330 state.roam <-
331 onppundermouse g x y (fun () -> showtext '!' "Whoopsie daisy");
334 let selstring s =
335 match Ne.res Unix.pipe with
336 | Ne.Exn exn ->
337 showtext '!' (Printf.sprintf "pipe failed: %s" (exntos exn))
338 | Ne.Res (r, w) ->
339 let clo cap fd =
340 Ne.clo fd (fun msg ->
341 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
344 let popened =
345 try popen conf.selcmd [r, 0; w, -1]; true
346 with exn ->
347 showtext '!'
348 (Printf.sprintf "failed to execute %s: %s"
349 conf.selcmd (exntos exn));
350 false
352 if popened
353 then (
355 let l = String.length s in
356 let n = tempfailureretry (Unix.write w s 0) l in
357 if n != l
358 then
359 showtext '!'
360 (Printf.sprintf
361 "failed to write %d characters to sel pipe, wrote %d"
364 with exn ->
365 showtext '!'
366 (Printf.sprintf "failed to write to sel pipe: %s"
367 (exntos exn)
370 else dolog "%s" s;
371 clo "selstring pipe/r" r;
372 clo "selstring pipe/w" w;
375 let undertext = function
376 | Unone -> "none"
377 | Ulinkuri s -> s
378 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
379 | Utext s -> "font: " ^ s
380 | Uunexpected s -> "unexpected: " ^ s
381 | Ulaunch s -> "launch: " ^ s
382 | Unamed s -> "named: " ^ s
383 | Uremote (filename, pageno) ->
384 Printf.sprintf "%s: page %d" filename (pageno+1)
385 | Uremotedest (filename, destname) ->
386 Printf.sprintf "%s: destination %S" filename destname
389 let updateunder x y =
390 match getunder x y with
391 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
392 | Ulinkuri uri ->
393 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
394 Wsi.setcursor Wsi.CURSOR_INFO
395 | Ulinkgoto (pageno, _) ->
396 if conf.underinfo
397 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
398 Wsi.setcursor Wsi.CURSOR_INFO
399 | Utext s ->
400 if conf.underinfo then showtext 'f' ("ont: " ^ s);
401 Wsi.setcursor Wsi.CURSOR_TEXT
402 | Uunexpected s ->
403 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
404 Wsi.setcursor Wsi.CURSOR_INHERIT
405 | Ulaunch s ->
406 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
407 Wsi.setcursor Wsi.CURSOR_INHERIT
408 | Unamed s ->
409 if conf.underinfo then showtext 'n' ("amed: " ^ s);
410 Wsi.setcursor Wsi.CURSOR_INHERIT
411 | Uremote (filename, pageno) ->
412 if conf.underinfo then showtext 'r'
413 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
414 Wsi.setcursor Wsi.CURSOR_INFO
415 | Uremotedest (filename, destname) ->
416 if conf.underinfo then showtext 'r'
417 (Printf.sprintf "emote destination: %s (%S)" filename destname);
418 Wsi.setcursor Wsi.CURSOR_INFO
421 let showlinktype under =
422 if conf.underinfo
423 then
424 match under with
425 | Unone -> ()
426 | under ->
427 let s = undertext under in
428 showtext ' ' s
431 let addchar s c =
432 let b = Buffer.create (String.length s + 1) in
433 Buffer.add_string b s;
434 Buffer.add_char b c;
435 Buffer.contents b;
438 let intentry_with_suffix text key =
439 let c =
440 if key >= 32 && key < 127
441 then Char.chr key
442 else '\000'
444 match Char.lowercase c with
445 | '0' .. '9' ->
446 let text = addchar text c in
447 TEcont text
449 | 'k' | 'm' | 'g' ->
450 let text = addchar text c in
451 TEcont text
453 | _ ->
454 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
455 TEcont text
458 let readcmd fd =
459 let s = "xxxx" in
460 let n = tempfailureretry (Unix.read fd s 0) 4 in
461 if n != 4 then error "incomplete read(len) = %d" n;
462 let len = 0
463 lor (Char.code s.[0] lsl 24)
464 lor (Char.code s.[1] lsl 16)
465 lor (Char.code s.[2] lsl 8)
466 lor (Char.code s.[3] lsl 0)
468 let s = String.create len in
469 let n = tempfailureretry (Unix.read fd s 0) len in
470 if n != len then error "incomplete read(data) %d vs %d" n len;
474 let btod b = if b then 1 else 0;;
476 let wcmd fmt =
477 let b = Buffer.create 16 in
478 Buffer.add_string b "llll";
479 Printf.kbprintf
480 (fun b ->
481 let s = Buffer.contents b in
482 let n = String.length s in
483 let len = n - 4 in
484 (* dolog "wcmd %S" (String.sub s 4 len); *)
485 s.[0] <- Char.chr ((len lsr 24) land 0xff);
486 s.[1] <- Char.chr ((len lsr 16) land 0xff);
487 s.[2] <- Char.chr ((len lsr 8) land 0xff);
488 s.[3] <- Char.chr (len land 0xff);
489 let n' = tempfailureretry (Unix.write state.sw s 0) n in
490 if n' != n then error "write failed %d vs %d" n' n;
491 ) b fmt;
494 let nogeomcmds cmds =
495 match cmds with
496 | s, [] -> emptystr s
497 | _ -> false
500 let layoutN ((columns, coverA, coverB), b) y sh =
501 let sh = sh - (hscrollh ()) in
502 let rec fold accu n =
503 if n = Array.length b
504 then accu
505 else
506 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
507 if (vy - y) > sh &&
508 (n = coverA - 1
509 || n = state.pagecount - coverB
510 || (n - coverA) mod columns = columns - 1)
511 then accu
512 else
513 let accu =
514 if vy + h > y
515 then
516 let pagey = max 0 (y - vy) in
517 let pagedispy = if pagey > 0 then 0 else vy - y in
518 let pagedispx, pagex =
519 let pdx =
520 if n = coverA - 1 || n = state.pagecount - coverB
521 then state.x + (wadjsb state.winw - w) / 2
522 else dx + xoff + state.x
524 if pdx < 0
525 then 0, -pdx
526 else pdx, 0
528 let pagevw =
529 let vw = wadjsb state.winw - pagedispx in
530 let pw = w - pagex in
531 min vw pw
533 let pagevh = min (h - pagey) (sh - pagedispy) in
534 if pagevw > 0 && pagevh > 0
535 then
536 let e =
537 { pageno = n
538 ; pagedimno = pdimno
539 ; pagew = w
540 ; pageh = h
541 ; pagex = pagex
542 ; pagey = pagey
543 ; pagevw = pagevw
544 ; pagevh = pagevh
545 ; pagedispx = pagedispx
546 ; pagedispy = pagedispy
547 ; pagecol = 0
550 e :: accu
551 else
552 accu
553 else
554 accu
556 fold accu (n+1)
558 if Array.length b = 0
559 then []
560 else List.rev (fold [] (page_of_y y))
563 let layoutS (columns, b) y sh =
564 let sh = sh - hscrollh () in
565 let rec fold accu n =
566 if n = Array.length b
567 then accu
568 else
569 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
570 if (vy - y) > sh
571 then accu
572 else
573 let accu =
574 if vy + pageh > y
575 then
576 let x = xoff + state.x in
577 let pagey = max 0 (y - vy) in
578 let pagedispy = if pagey > 0 then 0 else vy - y in
579 let pagedispx, pagex =
580 if px = 0
581 then (
582 if x < 0
583 then 0, -x
584 else x, 0
586 else (
587 let px = px - x in
588 if px < 0
589 then -px, 0
590 else 0, px
593 let pagecolw = pagew/columns in
594 let pagedispx =
595 if pagecolw < state.winw
596 then pagedispx + ((wadjsb state.winw - pagecolw) / 2)
597 else pagedispx
599 let pagevw =
600 let vw = wadjsb state.winw - pagedispx in
601 let pw = pagew - pagex in
602 min vw pw
604 let pagevw = min pagevw pagecolw in
605 let pagevh = min (pageh - pagey) (sh - pagedispy) in
606 if pagevw > 0 && pagevh > 0
607 then
608 let e =
609 { pageno = n/columns
610 ; pagedimno = pdimno
611 ; pagew = pagew
612 ; pageh = pageh
613 ; pagex = pagex
614 ; pagey = pagey
615 ; pagevw = pagevw
616 ; pagevh = pagevh
617 ; pagedispx = pagedispx
618 ; pagedispy = pagedispy
619 ; pagecol = n mod columns
622 e :: accu
623 else
624 accu
625 else
626 accu
628 fold accu (n+1)
630 List.rev (fold [] 0)
633 let layout y sh =
634 if nogeomcmds state.geomcmds
635 then
636 match conf.columns with
637 | Csingle b -> layoutN ((1, 0, 0), b) y sh
638 | Cmulti c -> layoutN c y sh
639 | Csplit s -> layoutS s y sh
640 else []
643 let clamp incr =
644 let y = state.y + incr in
645 let y = max 0 y in
646 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
650 let itertiles l f =
651 let tilex = l.pagex mod conf.tilew in
652 let tiley = l.pagey mod conf.tileh in
654 let col = l.pagex / conf.tilew in
655 let row = l.pagey / conf.tileh in
657 let rec rowloop row y0 dispy h =
658 if h = 0
659 then ()
660 else (
661 let dh = conf.tileh - y0 in
662 let dh = min h dh in
663 let rec colloop col x0 dispx w =
664 if w = 0
665 then ()
666 else (
667 let dw = conf.tilew - x0 in
668 let dw = min w dw in
669 let dispx' = xadjsb dispx in
670 f col row dispx' dispy x0 y0 dw dh;
671 colloop (col+1) 0 (dispx+dw) (w-dw)
674 colloop col tilex l.pagedispx l.pagevw;
675 rowloop (row+1) 0 (dispy+dh) (h-dh)
678 if l.pagevw > 0 && l.pagevh > 0
679 then rowloop row tiley l.pagedispy l.pagevh;
682 let gettileopaque l col row =
683 let key =
684 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
686 try Some (Hashtbl.find state.tilemap key)
687 with Not_found -> None
690 let puttileopaque l col row gen colorspace angle opaque size elapsed =
691 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
692 Hashtbl.add state.tilemap key (opaque, size, elapsed)
695 let filledrect x0 y0 x1 y1 =
696 GlArray.disable `texture_coord;
697 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
698 GlArray.vertex `two state.vraw;
699 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
700 GlArray.enable `texture_coord;
703 let linerect x0 y0 x1 y1 =
704 GlArray.disable `texture_coord;
705 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y1; x1; y0 |];
706 GlArray.vertex `two state.vraw;
707 GlArray.draw_arrays `line_loop ~first:0 ~count:4;
708 GlArray.enable `texture_coord;
711 let drawtiles l color =
712 GlDraw.color color;
713 begintiles ();
714 let f col row x y tilex tiley w h =
715 match gettileopaque l col row with
716 | Some (opaque, _, t) ->
717 let params = x, y, w, h, tilex, tiley in
718 if conf.invert
719 then GlTex.env (`mode `blend);
720 drawtile params opaque;
721 if conf.invert
722 then GlTex.env (`mode `modulate);
723 if conf.debug
724 then (
725 endtiles ();
726 let s = Printf.sprintf
727 "%d[%d,%d] %f sec"
728 l.pageno col row t
730 let w = measurestr fstate.fontsize s in
731 GlDraw.color (0.0, 0.0, 0.0);
732 filledrect (float (x-2))
733 (float (y-2))
734 (float (x+2) +. w)
735 (float (y + fstate.fontsize + 2));
736 GlDraw.color (1.0, 1.0, 1.0);
737 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
738 begintiles ();
741 | None ->
742 endtiles ();
743 let w =
744 if conf.leftscroll
745 then w
746 else
747 let lw = wadjsb state.winw - x in
748 min lw w
749 and h =
750 let lh = state.winh - y in
751 min lh h
753 if conf.invert
754 then GlTex.env (`mode `blend);
755 begin match state.checkerstexid with
756 | Some id ->
757 Gl.enable `texture_2d;
758 GlTex.bind_texture ~target:`texture_2d id;
759 let x0 = float x
760 and y0 = float y
761 and x1 = float (x+w)
762 and y1 = float (y+h) in
764 let tw = float w /. 16.0
765 and th = float h /. 16.0 in
766 let tx0 = float tilex /. 16.0
767 and ty0 = float tiley /. 16.0 in
768 let tx1 = tx0 +. tw
769 and ty1 = ty0 +. th in
770 Raw.sets_float state.vraw ~pos:0
771 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
772 Raw.sets_float state.traw ~pos:0
773 [| tx0; ty0; tx0; ty1; tx1; ty0; tx1; ty1 |];
774 GlArray.vertex `two state.vraw;
775 GlArray.tex_coord `two state.traw;
776 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
777 Gl.disable `texture_2d;
779 | None ->
780 GlDraw.color (1.0, 1.0, 1.0);
781 filledrect (float x) (float y) (float (x+w)) (float (y+h));
782 end;
783 if conf.invert
784 then GlTex.env (`mode `modulate);
785 if w > 128 && h > fstate.fontsize + 10
786 then (
787 let c = if conf.invert then 1.0 else 0.0 in
788 GlDraw.color (c, c, c);
789 let c, r =
790 if conf.verbose
791 then (col*conf.tilew, row*conf.tileh)
792 else col, row
794 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
796 GlDraw.color color;
797 begintiles ();
799 itertiles l f;
800 endtiles ();
803 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
805 let tilevisible1 l x y =
806 let ax0 = l.pagex
807 and ax1 = l.pagex + l.pagevw
808 and ay0 = l.pagey
809 and ay1 = l.pagey + l.pagevh in
811 let bx0 = x
812 and by0 = y in
813 let bx1 = min (bx0 + conf.tilew) l.pagew
814 and by1 = min (by0 + conf.tileh) l.pageh in
816 let rx0 = max ax0 bx0
817 and ry0 = max ay0 by0
818 and rx1 = min ax1 bx1
819 and ry1 = min ay1 by1 in
821 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
822 nonemptyintersection
825 let tilevisible layout n x y =
826 let rec findpageinlayout m = function
827 | l :: rest when l.pageno = n ->
828 tilevisible1 l x y || (
829 match conf.columns with
830 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
831 | _ -> false
833 | _ :: rest -> findpageinlayout 0 rest
834 | [] -> false
836 findpageinlayout 0 layout;
839 let tileready l x y =
840 tilevisible1 l x y &&
841 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
844 let tilepage n p layout =
845 let rec loop = function
846 | l :: rest ->
847 if l.pageno = n
848 then
849 let f col row _ _ _ _ _ _ =
850 if state.currently = Idle
851 then
852 match gettileopaque l col row with
853 | Some _ -> ()
854 | None ->
855 let x = col*conf.tilew
856 and y = row*conf.tileh in
857 let w =
858 let w = l.pagew - x in
859 min w conf.tilew
861 let h =
862 let h = l.pageh - y in
863 min h conf.tileh
865 let pbo =
866 if conf.usepbo
867 then getpbo w h conf.colorspace
868 else ~< "0"
870 wcmd "tile %s %d %d %d %d %s"
871 (~> p) x y w h (~> pbo);
872 state.currently <-
873 Tiling (
874 l, p, conf.colorspace, conf.angle,
875 state.gen, col, row, conf.tilew, conf.tileh
878 itertiles l f;
879 else
880 loop rest
882 | [] -> ()
884 if nogeomcmds state.geomcmds
885 then loop layout;
888 let preloadlayout y =
889 let y = if y < state.winh then 0 else y - state.winh in
890 let h = state.winh*3 in
891 layout y h;
894 let load pages =
895 let rec loop pages =
896 if state.currently != Idle
897 then ()
898 else
899 match pages with
900 | l :: rest ->
901 begin match getopaque l.pageno with
902 | None ->
903 wcmd "page %d %d" l.pageno l.pagedimno;
904 state.currently <- Loading (l, state.gen);
905 | Some opaque ->
906 tilepage l.pageno opaque pages;
907 loop rest
908 end;
909 | _ -> ()
911 if nogeomcmds state.geomcmds
912 then loop pages
915 let preload pages =
916 load pages;
917 if conf.preload && state.currently = Idle
918 then load (preloadlayout state.y);
921 let layoutready layout =
922 let rec fold all ls =
923 all && match ls with
924 | l :: rest ->
925 let seen = ref false in
926 let allvisible = ref true in
927 let foo col row _ _ _ _ _ _ =
928 seen := true;
929 allvisible := !allvisible &&
930 begin match gettileopaque l col row with
931 | Some _ -> true
932 | None -> false
935 itertiles l foo;
936 fold (!seen && !allvisible) rest
937 | [] -> true
939 let alltilesvisible = fold true layout in
940 alltilesvisible;
943 let gotoy y =
944 let y = bound y 0 state.maxy in
945 let y, layout, proceed =
946 match conf.maxwait with
947 | Some time when state.ghyll == noghyll ->
948 begin match state.throttle with
949 | None ->
950 let layout = layout y state.winh in
951 let ready = layoutready layout in
952 if not ready
953 then (
954 load layout;
955 state.throttle <- Some (layout, y, now ());
957 else G.postRedisplay "gotoy showall (None)";
958 y, layout, ready
959 | Some (_, _, started) ->
960 let dt = now () -. started in
961 if dt > time
962 then (
963 state.throttle <- None;
964 let layout = layout y state.winh in
965 load layout;
966 G.postRedisplay "maxwait";
967 y, layout, true
969 else -1, [], false
972 | _ ->
973 let layout = layout y state.winh in
974 if not !wtmode || layoutready layout
975 then G.postRedisplay "gotoy ready";
976 y, layout, true
978 if proceed
979 then (
980 state.y <- y;
981 state.layout <- layout;
982 begin match state.mode with
983 | LinkNav (Ltexact (pageno, linkno)) ->
984 let rec loop = function
985 | [] ->
986 state.mode <- LinkNav (Ltgendir 0)
987 | l :: _ when l.pageno = pageno ->
988 begin match getopaque pageno with
989 | None ->
990 state.mode <- LinkNav (Ltgendir 0)
991 | Some opaque ->
992 let x0, y0, x1, y1 = getlinkrect opaque linkno in
993 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
994 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
995 then state.mode <- LinkNav (Ltgendir 0)
997 | _ :: rest -> loop rest
999 loop layout
1000 | _ -> ()
1001 end;
1002 begin match state.mode with
1003 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1004 if not (pagevisible layout pageno)
1005 then (
1006 match state.layout with
1007 | [] -> ()
1008 | l :: _ ->
1009 state.mode <- Birdseye (
1010 conf, leftx, l.pageno, hooverpageno, anchor
1013 | LinkNav (Ltgendir dir as lt) ->
1014 let linknav =
1015 let rec loop = function
1016 | [] -> lt
1017 | l :: rest ->
1018 match getopaque l.pageno with
1019 | None -> loop rest
1020 | Some opaque ->
1021 let link =
1022 let ld =
1023 if dir = 0
1024 then LDfirstvisible (l.pagex, l.pagey, dir)
1025 else (
1026 if dir > 0 then LDfirst else LDlast
1029 findlink opaque ld
1031 match link with
1032 | Lnotfound -> loop rest
1033 | Lfound n ->
1034 showlinktype (getlink opaque n);
1035 Ltexact (l.pageno, n)
1037 loop state.layout
1039 state.mode <- LinkNav linknav
1040 | _ -> ()
1041 end;
1042 preload layout;
1044 state.ghyll <- noghyll;
1045 if conf.updatecurs
1046 then (
1047 let mx, my = state.mpos in
1048 updateunder mx my;
1052 let conttiling pageno opaque =
1053 tilepage pageno opaque
1054 (if conf.preload then preloadlayout state.y else state.layout)
1057 let gotoy_and_clear_text y =
1058 if not conf.verbose then state.text <- E.s;
1059 gotoy y;
1062 let getanchory (n, top, dtop) =
1063 let y, h = getpageyh n in
1064 if conf.presentation
1065 then
1066 let ips = calcips h in
1067 y + truncate (top*.float h -. dtop*.float ips) + ips;
1068 else
1069 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1072 let gotoanchor anchor =
1073 gotoy (getanchory anchor);
1076 let addnav () =
1077 cbput state.hists.nav (getanchor ());
1080 let getnav dir =
1081 let anchor = cbgetc state.hists.nav dir in
1082 getanchory anchor;
1085 let gotoghyll1 single y =
1086 let scroll f n a b =
1087 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1088 let snake f a b =
1089 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1090 if f < a
1091 then s (float f /. float a)
1092 else (
1093 if f > b
1094 then 1.0 -. s ((float (f-b) /. float (n-b)))
1095 else 1.0
1098 snake f a b
1099 and summa n a b =
1100 let ins = float a *. 0.5
1101 and outs = float (n-b) *. 0.5 in
1102 let ones = b - a in
1103 ins +. outs +. float ones
1105 let rec set nab y sy =
1106 let (_N, _A, _B), y =
1107 if single
1108 then
1109 let scl = if y > sy then 2 else -2 in
1110 let _N, _, _ = nab in
1111 (_N,0,_N), y+conf.scrollstep*scl
1112 else nab,y in
1113 let sum = summa _N _A _B in
1114 let dy = float (y - sy) in
1115 state.ghyll <- (
1116 let rec gf n y1 o =
1117 if n >= _N
1118 then state.ghyll <- noghyll
1119 else
1120 let go n =
1121 let s = scroll n _N _A _B in
1122 let y1 = y1 +. ((s *. dy) /. sum) in
1123 gotoy_and_clear_text (truncate y1);
1124 state.ghyll <- gf (n+1) y1;
1126 match o with
1127 | None -> go n
1128 | Some y' when single -> set nab y' state.y
1129 | Some y' -> set (_N/2, 1, 1) y' state.y
1131 gf 0 (float state.y)
1134 match conf.ghyllscroll with
1135 | Some nab when not conf.presentation ->
1136 if state.ghyll == noghyll
1137 then set nab y state.y
1138 else state.ghyll (Some y)
1139 | _ ->
1140 gotoy_and_clear_text y
1143 let gotoghyll = gotoghyll1 false;;
1145 let gotopage n top =
1146 let y, h = getpageyh n in
1147 let y = y + (truncate (top *. float h)) in
1148 gotoghyll y
1151 let gotopage1 n top =
1152 let y = getpagey n in
1153 let y = y + top in
1154 gotoghyll y
1157 let invalidate s f =
1158 state.layout <- [];
1159 state.pdims <- [];
1160 state.rects <- [];
1161 state.rects1 <- [];
1162 match state.geomcmds with
1163 | ps, [] when emptystr ps ->
1164 f ();
1165 state.geomcmds <- s, [];
1167 | ps, [] ->
1168 state.geomcmds <- ps, [s, f];
1170 | ps, (s', _) :: rest when s' = s ->
1171 state.geomcmds <- ps, ((s, f) :: rest);
1173 | ps, cmds ->
1174 state.geomcmds <- ps, ((s, f) :: cmds);
1177 let flushpages () =
1178 Hashtbl.iter (fun _ opaque ->
1179 wcmd "freepage %s" (~> opaque);
1180 ) state.pagemap;
1181 Hashtbl.clear state.pagemap;
1184 let flushtiles () =
1185 if not (Queue.is_empty state.tilelru)
1186 then (
1187 Queue.iter (fun (k, p, s) ->
1188 wcmd "freetile %s" (~> p);
1189 state.memused <- state.memused - s;
1190 Hashtbl.remove state.tilemap k;
1191 ) state.tilelru;
1192 state.uioh#infochanged Memused;
1193 Queue.clear state.tilelru;
1195 load state.layout;
1198 let stateh h =
1199 let h = truncate (float h*.conf.zoom) in
1200 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
1201 h - d
1204 let opendoc path password =
1205 state.path <- path;
1206 state.password <- password;
1207 state.gen <- state.gen + 1;
1208 state.docinfo <- [];
1210 flushpages ();
1211 setaalevel conf.aalevel;
1212 let titlepath =
1213 if emptystr state.origin
1214 then path
1215 else state.origin
1217 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
1218 wcmd "open %d %d %s\000%s\000" (btod !wtmode) (btod !cxack) path password;
1219 invalidate "reqlayout"
1220 (fun () ->
1221 wcmd "reqlayout %d %d %d %s\000"
1222 conf.angle (FMTE.to_int conf.fitmodel)
1223 (stateh state.winh) state.nameddest
1227 let reload () =
1228 state.anchor <- getanchor ();
1229 opendoc state.path state.password;
1232 let scalecolor c =
1233 let c = c *. conf.colorscale in
1234 (c, c, c);
1237 let scalecolor2 (r, g, b) =
1238 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1241 let docolumns = function
1242 | Csingle _ ->
1243 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1244 let rec loop pageno pdimno pdim y ph pdims =
1245 if pageno = state.pagecount
1246 then ()
1247 else
1248 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1249 match pdims with
1250 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1251 pdimno+1, pdim, rest
1252 | _ ->
1253 pdimno, pdim, pdims
1255 let x = max 0 (((wadjsb state.winw - w) / 2) - xoff) in
1256 let y = y +
1257 (if conf.presentation
1258 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1259 else (if pageno = 0 then 0 else conf.interpagespace)
1262 a.(pageno) <- (pdimno, x, y, pdim);
1263 loop (pageno+1) pdimno pdim (y + h) h pdims
1265 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1266 conf.columns <- Csingle a;
1268 | Cmulti ((columns, coverA, coverB), _) ->
1269 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1270 let rec loop pageno pdimno pdim x y rowh pdims =
1271 let rec fixrow m = if m = pageno then () else
1272 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1273 if h < rowh
1274 then (
1275 let y = y + (rowh - h) / 2 in
1276 a.(m) <- (pdimno, x, y, pdim);
1278 fixrow (m+1)
1280 if pageno = state.pagecount
1281 then fixrow (((pageno - 1) / columns) * columns)
1282 else
1283 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1284 match pdims with
1285 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1286 pdimno+1, pdim, rest
1287 | _ ->
1288 pdimno, pdim, pdims
1290 let x, y, rowh' =
1291 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1292 then (
1293 let x = (wadjsb state.winw - w) / 2 in
1294 let ips =
1295 if conf.presentation then calcips h else conf.interpagespace in
1296 x, y + ips + rowh, h
1298 else (
1299 if (pageno - coverA) mod columns = 0
1300 then (
1301 let x = max 0 (wadjsb state.winw - state.w) / 2 in
1302 let y =
1303 if conf.presentation
1304 then
1305 let ips = calcips h in
1306 y + (if pageno = 0 then 0 else calcips rowh + ips)
1307 else
1308 y + (if pageno = 0 then 0 else conf.interpagespace)
1310 x, y + rowh, h
1312 else x, y, max rowh h
1315 let y =
1316 if pageno > 1 && (pageno - coverA) mod columns = 0
1317 then (
1318 let y =
1319 if pageno = columns && conf.presentation
1320 then (
1321 let ips = calcips rowh in
1322 for i = 0 to pred columns
1324 let (pdimno, x, y, pdim) = a.(i) in
1325 a.(i) <- (pdimno, x, y+ips, pdim)
1326 done;
1327 y+ips;
1329 else y
1331 fixrow (pageno - columns);
1334 else y
1336 a.(pageno) <- (pdimno, x, y, pdim);
1337 let x = x + w + xoff*2 + conf.interpagespace in
1338 loop (pageno+1) pdimno pdim x y rowh' pdims
1340 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1341 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1343 | Csplit (c, _) ->
1344 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1345 let rec loop pageno pdimno pdim y pdims =
1346 if pageno = state.pagecount
1347 then ()
1348 else
1349 let pdimno, ((_, w, h, _) as pdim), pdims =
1350 match pdims with
1351 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1352 pdimno+1, pdim, rest
1353 | _ ->
1354 pdimno, pdim, pdims
1356 let cw = w / c in
1357 let rec loop1 n x y =
1358 if n = c then y else (
1359 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1360 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1363 let y = loop1 0 0 y in
1364 loop (pageno+1) pdimno pdim y pdims
1366 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1367 conf.columns <- Csplit (c, a);
1370 let represent () =
1371 docolumns conf.columns;
1372 state.maxy <- calcheight ();
1373 if state.reprf == noreprf
1374 then (
1375 match state.mode with
1376 | Birdseye (_, _, pageno, _, _) ->
1377 let y, h = getpageyh pageno in
1378 let top = (state.winh - h) / 2 in
1379 gotoy (max 0 (y - top))
1380 | _ -> gotoanchor state.anchor
1382 else (
1383 state.reprf ();
1384 state.reprf <- noreprf;
1388 let reshape w h =
1389 GlDraw.viewport ~x:0 ~y:0 ~w:w ~h:h;
1390 let firsttime = state.geomcmds == firstgeomcmds in
1391 if not firsttime && nogeomcmds state.geomcmds
1392 then state.anchor <- getanchor ();
1394 state.winw <- w;
1395 let w = wadjsb (truncate (float w *. conf.zoom)) in
1396 let w = max w 2 in
1397 state.winh <- h;
1398 setfontsize fstate.fontsize;
1399 GlMat.mode `modelview;
1400 GlMat.load_identity ();
1402 GlMat.mode `projection;
1403 GlMat.load_identity ();
1404 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1405 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1406 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
1408 let relx =
1409 if conf.zoom <= 1.0
1410 then 0.0
1411 else float state.x /. float state.w
1413 invalidate "geometry"
1414 (fun () ->
1415 state.w <- w;
1416 if not firsttime
1417 then state.x <- truncate (relx *. float w);
1418 let w =
1419 match conf.columns with
1420 | Csingle _ -> w
1421 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1422 | Csplit (c, _) -> w * c
1424 wcmd "geometry %d %d %d"
1425 w (stateh h) (FMTE.to_int conf.fitmodel)
1429 let enttext () =
1430 let len = String.length state.text in
1431 let x0 = xadjsb 0 in
1432 let drawstring s =
1433 let hscrollh =
1434 match state.mode with
1435 | Textentry _ | View | LinkNav _ ->
1436 let h, _, _ = state.uioh#scrollpw in
1438 | _ -> 0
1440 let rect x w =
1441 filledrect x (float (state.winh - (fstate.fontsize + 4) - hscrollh))
1442 (x+.w) (float (state.winh - hscrollh))
1445 let w = float (wadjsb state.winw - 1) in
1446 if state.progress >= 0.0 && state.progress < 1.0
1447 then (
1448 GlDraw.color (0.3, 0.3, 0.3);
1449 let w1 = w *. state.progress in
1450 rect (float x0) w1;
1451 GlDraw.color (0.0, 0.0, 0.0);
1452 rect (float x0+.w1) (float x0+.w-.w1)
1454 else (
1455 GlDraw.color (0.0, 0.0, 0.0);
1456 rect (float x0) w;
1459 GlDraw.color (1.0, 1.0, 1.0);
1460 drawstring fstate.fontsize
1461 (if conf.leftscroll then x0 else x0 + if len > 0 then 8 else 2)
1462 (state.winh - hscrollh - 5) s;
1464 let s =
1465 match state.mode with
1466 | Textentry ((prefix, text, _, _, _, _), _) ->
1467 let s =
1468 if len > 0
1469 then
1470 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1471 else
1472 Printf.sprintf "%s%s_" prefix text
1476 | _ -> state.text
1478 let s =
1479 if state.newerrmsgs
1480 then (
1481 if not (istextentry state.mode) && state.uioh#eformsgs
1482 then
1483 let s1 = "(press 'e' to review error messasges)" in
1484 if nonemptystr s then s ^ " " ^ s1 else s1
1485 else s
1487 else s
1489 if nonemptystr s
1490 then drawstring s
1493 let gctiles () =
1494 let len = Queue.length state.tilelru in
1495 let layout = lazy (
1496 match state.throttle with
1497 | None ->
1498 if conf.preload
1499 then preloadlayout state.y
1500 else state.layout
1501 | Some (layout, _, _) ->
1502 layout
1503 ) in
1504 let rec loop qpos =
1505 if state.memused <= conf.memlimit
1506 then ()
1507 else (
1508 if qpos < len
1509 then
1510 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1511 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1512 let (_, pw, ph, _) = getpagedim n in
1514 gen = state.gen
1515 && colorspace = conf.colorspace
1516 && angle = conf.angle
1517 && pagew = pw
1518 && pageh = ph
1519 && (
1520 let x = col*conf.tilew
1521 and y = row*conf.tileh in
1522 tilevisible (Lazy.force_val layout) n x y
1524 then Queue.push lruitem state.tilelru
1525 else (
1526 freepbo p;
1527 wcmd "freetile %s" (~> p);
1528 state.memused <- state.memused - s;
1529 state.uioh#infochanged Memused;
1530 Hashtbl.remove state.tilemap k;
1532 loop (qpos+1)
1535 loop 0
1538 let logcurrently = function
1539 | Idle -> dolog "Idle"
1540 | Loading (l, gen) ->
1541 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
1542 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
1543 dolog
1544 "Tiling %d[%d,%d] page=%s cs=%s angle"
1545 l.pageno col row (~> pageopaque)
1546 (CSTE.to_string colorspace)
1548 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
1549 angle gen conf.angle state.gen
1550 tilew tileh
1551 conf.tilew conf.tileh
1553 | Outlining _ ->
1554 dolog "outlining"
1557 let splitatspace =
1558 let r = Str.regexp " " in
1559 fun s -> Str.bounded_split r s 2;
1562 let onpagerect pageno f =
1563 let b =
1564 match conf.columns with
1565 | Cmulti (_, b) -> b
1566 | Csingle b -> b
1567 | Csplit (_, b) -> b
1569 if pageno >= 0 && pageno < Array.length b
1570 then
1571 let (_, _, _, (w, h, _, _)) = b.(pageno) in
1572 f w h
1575 let gotopagexy1 pageno x y =
1576 let _,w1,h1,leftx = getpagedim pageno in
1577 let top = y /. (float h1) in
1578 let left = x /. (float w1) in
1579 let py, w, h = getpageywh pageno in
1580 let wh = state.winh - hscrollh () in
1581 let x = left *. (float w) in
1582 let x = leftx + state.x + truncate x in
1583 let sx =
1584 if x < 0 || x >= wadjsb state.winw
1585 then state.x - x
1586 else state.x
1588 let pdy = truncate (top *. float h) in
1589 let y' = py + pdy in
1590 let dy = y' - state.y in
1591 let sy =
1592 if x != state.x || not (dy > 0 && dy < wh)
1593 then (
1594 if conf.presentation
1595 then
1596 if abs (py - y') > wh
1597 then y'
1598 else py
1599 else y';
1601 else state.y
1603 if state.x != sx || state.y != sy
1604 then (
1605 let x, y =
1606 if !wtmode
1607 then (
1608 let ww = wadjsb state.winw in
1609 let qx = sx / ww
1610 and qy = pdy / wh in
1611 let x = qx * ww
1612 and y = py + qy * wh in
1613 let x = if -x + ww > w1 then -(w1-ww) else x
1614 and y' = if y + wh > state.maxy then state.maxy - wh else y in
1615 let y =
1616 if conf.presentation
1617 then
1618 if abs (py - y') > wh
1619 then y'
1620 else py
1621 else y';
1623 (x, y)
1625 else (sx, sy)
1627 state.x <- x;
1628 gotoy_and_clear_text y;
1630 else gotoy_and_clear_text state.y;
1633 let gotopagexy pageno x y =
1634 match state.mode with
1635 | Birdseye _ -> gotopage pageno 0.0
1636 | _ -> gotopagexy1 pageno x y
1639 let act cmds =
1640 (* dolog "%S" cmds; *)
1641 let cl = splitatspace cmds in
1642 let scan s fmt f =
1643 try Scanf.sscanf s fmt f
1644 with exn ->
1645 dolog "error processing '%S': %s" cmds (exntos exn);
1646 exit 1
1648 let addoutline outline =
1649 match state.currently with
1650 | Outlining outlines ->
1651 state.currently <- Outlining (outline :: outlines)
1652 | Idle -> state.currently <- Outlining [outline]
1653 | currently ->
1654 dolog "invalid outlining state";
1655 logcurrently currently
1657 match cl with
1658 | "clear" :: [] ->
1659 state.uioh#infochanged Pdim;
1660 state.pdims <- [];
1662 | "clearrects" :: [] ->
1663 state.rects <- state.rects1;
1664 G.postRedisplay "clearrects";
1666 | "continue" :: args :: [] ->
1667 let n = scan args "%u" (fun n -> n) in
1668 state.pagecount <- n;
1669 begin match state.currently with
1670 | Outlining l ->
1671 state.currently <- Idle;
1672 state.outlines <- Array.of_list (List.rev l)
1673 | _ -> ()
1674 end;
1676 let cur, cmds = state.geomcmds in
1677 if emptystr cur
1678 then failwith "umpossible";
1680 begin match List.rev cmds with
1681 | [] ->
1682 state.geomcmds <- E.s, [];
1683 state.throttle <- None;
1684 represent ();
1685 | (s, f) :: rest ->
1686 f ();
1687 state.geomcmds <- s, List.rev rest;
1688 end;
1689 if conf.maxwait = None && not !wtmode
1690 then G.postRedisplay "continue";
1692 | "title" :: args :: [] ->
1693 conf.title <- args;
1694 Wsi.settitle args
1696 | "msg" :: args :: [] ->
1697 showtext ' ' args
1699 | "vmsg" :: args :: [] ->
1700 if conf.verbose
1701 then showtext ' ' args
1703 | "emsg" :: args :: [] ->
1704 Buffer.add_string state.errmsgs args;
1705 state.newerrmsgs <- true;
1706 G.postRedisplay "error message"
1708 | "progress" :: args :: [] ->
1709 let progress, text =
1710 scan args "%f %n"
1711 (fun f pos ->
1712 f, String.sub args pos (String.length args - pos))
1714 state.text <- text;
1715 state.progress <- progress;
1716 G.postRedisplay "progress"
1718 | "firstmatch" :: args :: [] ->
1719 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1720 scan args "%u %d %f %f %f %f %f %f %f %f"
1721 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1722 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1724 let xoff = float (xadjsb 0) in
1725 let x0 = x0 +. xoff
1726 and x1 = x1 +. xoff
1727 and x2 = x2 +. xoff
1728 and x3 = x3 +. xoff in
1729 let y = (getpagey pageno) + truncate y0 in
1730 addnav ();
1731 gotoy y;
1732 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
1734 | "match" :: args :: [] ->
1735 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1736 scan args "%u %d %f %f %f %f %f %f %f %f"
1737 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1738 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1740 let xoff = float (xadjsb 0) in
1741 let x0 = x0 +. xoff
1742 and x1 = x1 +. xoff
1743 and x2 = x2 +. xoff
1744 and x3 = x3 +. xoff in
1745 state.rects1 <-
1746 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1748 | "page" :: args :: [] ->
1749 let pageopaques, t = scan args "%s %f" (fun p t -> p, t) in
1750 let pageopaque = ~< pageopaques in
1751 begin match state.currently with
1752 | Loading (l, gen) ->
1753 vlog "page %d took %f sec" l.pageno t;
1754 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1755 begin match state.throttle with
1756 | None ->
1757 let preloadedpages =
1758 if conf.preload
1759 then preloadlayout state.y
1760 else state.layout
1762 let evict () =
1763 let set =
1764 List.fold_left (fun s l -> IntSet.add l.pageno s)
1765 IntSet.empty preloadedpages
1767 let evictedpages =
1768 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1769 if not (IntSet.mem pageno set)
1770 then (
1771 wcmd "freepage %s" (~> opaque);
1772 key :: accu
1774 else accu
1775 ) state.pagemap []
1777 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1779 evict ();
1780 state.currently <- Idle;
1781 if gen = state.gen
1782 then (
1783 tilepage l.pageno pageopaque state.layout;
1784 load state.layout;
1785 load preloadedpages;
1786 if pagevisible state.layout l.pageno
1787 && layoutready state.layout
1788 then G.postRedisplay "page";
1791 | Some (layout, _, _) ->
1792 state.currently <- Idle;
1793 tilepage l.pageno pageopaque layout;
1794 load state.layout
1795 end;
1797 | _ ->
1798 dolog "Inconsistent loading state";
1799 logcurrently state.currently;
1800 exit 1
1803 | "tile" :: args :: [] ->
1804 let (x, y, opaques, size, t) =
1805 scan args "%u %u %s %u %f"
1806 (fun x y p size t -> (x, y, p, size, t))
1808 let opaque = ~< opaques in
1809 begin match state.currently with
1810 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1811 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1813 unmappbo opaque;
1814 if tilew != conf.tilew || tileh != conf.tileh
1815 then (
1816 wcmd "freetile %s" (~> opaque);
1817 state.currently <- Idle;
1818 load state.layout;
1820 else (
1821 puttileopaque l col row gen cs angle opaque size t;
1822 state.memused <- state.memused + size;
1823 state.uioh#infochanged Memused;
1824 gctiles ();
1825 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1826 opaque, size) state.tilelru;
1828 let layout =
1829 match state.throttle with
1830 | None -> state.layout
1831 | Some (layout, _, _) -> layout
1834 state.currently <- Idle;
1835 if gen = state.gen
1836 && conf.colorspace = cs
1837 && conf.angle = angle
1838 && tilevisible layout l.pageno x y
1839 then conttiling l.pageno pageopaque;
1841 begin match state.throttle with
1842 | None ->
1843 preload state.layout;
1844 if gen = state.gen
1845 && conf.colorspace = cs
1846 && conf.angle = angle
1847 && tilevisible state.layout l.pageno x y
1848 && (not !wtmode || layoutready state.layout)
1849 then G.postRedisplay "tile nothrottle";
1851 | Some (layout, y, _) ->
1852 let ready = layoutready layout in
1853 if ready
1854 then (
1855 state.y <- y;
1856 state.layout <- layout;
1857 state.throttle <- None;
1858 G.postRedisplay "throttle";
1860 else load layout;
1861 end;
1864 | _ ->
1865 dolog "Inconsistent tiling state";
1866 logcurrently state.currently;
1867 exit 1
1870 | "pdim" :: args :: [] ->
1871 let (n, w, h, _) as pdim =
1872 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1874 let pdim =
1875 match conf.fitmodel, conf.columns with
1876 | (FitPage | FitProportional), Csplit _ -> (n, w, h, 0)
1877 | _ -> pdim
1879 state.uioh#infochanged Pdim;
1880 state.pdims <- pdim :: state.pdims
1882 | "o" :: args :: [] ->
1883 let (l, n, t, h, pos) =
1884 scan args "%u %u %d %u %n"
1885 (fun l n t h pos -> l, n, t, h, pos)
1887 let s = String.sub args pos (String.length args - pos) in
1888 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
1890 | "ou" :: args :: [] ->
1891 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
1892 let s = String.sub args pos len in
1893 let pos2 = pos + len + 1 in
1894 let uri = String.sub args pos2 (String.length args - pos2) in
1895 addoutline (s, l, Ouri uri)
1897 | "on" :: args :: [] ->
1898 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
1899 let s = String.sub args pos (String.length args - pos) in
1900 addoutline (s, l, Onone)
1902 | "a" :: args :: [] ->
1903 let (n, l, t) =
1904 scan args "%u %d %d" (fun n l t -> n, l, t)
1906 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
1908 | "info" :: args :: [] ->
1909 state.docinfo <- (1, args) :: state.docinfo
1911 | "infoend" :: [] ->
1912 state.uioh#infochanged Docinfo;
1913 state.docinfo <- List.rev state.docinfo
1915 | _ ->
1916 error "unknown cmd `%S'" cmds
1919 let onhist cb =
1920 let rc = cb.rc in
1921 let action = function
1922 | HCprev -> cbget cb ~-1
1923 | HCnext -> cbget cb 1
1924 | HCfirst -> cbget cb ~-(cb.rc)
1925 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1926 and cancel () = cb.rc <- rc
1927 in (action, cancel)
1930 let search pattern forward =
1931 match conf.columns with
1932 | Csplit _ ->
1933 showtext '!' "searching does not work properly in split columns mode"
1934 | _ ->
1935 if nonemptystr pattern
1936 then
1937 let pn, py =
1938 match state.layout with
1939 | [] -> 0, 0
1940 | l :: _ ->
1941 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1943 wcmd "search %d %d %d %d,%s\000"
1944 (btod conf.icase) pn py (btod forward) pattern;
1947 let intentry text key =
1948 let c =
1949 if key >= 32 && key < 127
1950 then Char.chr key
1951 else '\000'
1953 match c with
1954 | '0' .. '9' ->
1955 let text = addchar text c in
1956 TEcont text
1958 | _ ->
1959 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1960 TEcont text
1963 let linknentry text key =
1964 let c =
1965 if key >= 32 && key < 127
1966 then Char.chr key
1967 else '\000'
1969 match c with
1970 | 'a' .. 'z' ->
1971 let text = addchar text c in
1972 TEcont text
1974 | _ ->
1975 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1976 TEcont text
1979 let linkndone f s =
1980 if nonemptystr s
1981 then (
1982 let n =
1983 let l = String.length s in
1984 let rec loop pos n = if pos = l then n else
1985 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
1986 loop (pos+1) (n*26 + m)
1987 in loop 0 0
1989 let rec loop n = function
1990 | [] -> ()
1991 | l :: rest ->
1992 match getopaque l.pageno with
1993 | None -> loop n rest
1994 | Some opaque ->
1995 let m = getlinkcount opaque in
1996 if n < m
1997 then (
1998 let under = getlink opaque n in
1999 f under
2001 else loop (n-m) rest
2003 loop n state.layout;
2007 let textentry text key =
2008 if key land 0xff00 = 0xff00
2009 then TEcont text
2010 else TEcont (text ^ toutf8 key)
2013 let reqlayout angle fitmodel =
2014 match state.throttle with
2015 | None ->
2016 if nogeomcmds state.geomcmds
2017 then state.anchor <- getanchor ();
2018 conf.angle <- angle mod 360;
2019 if conf.angle != 0
2020 then (
2021 match state.mode with
2022 | LinkNav _ -> state.mode <- View
2023 | _ -> ()
2025 conf.fitmodel <- fitmodel;
2026 invalidate "reqlayout"
2027 (fun () ->
2028 wcmd "reqlayout %d %d %d"
2029 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
2031 | _ -> ()
2034 let settrim trimmargins trimfuzz =
2035 if nogeomcmds state.geomcmds
2036 then state.anchor <- getanchor ();
2037 conf.trimmargins <- trimmargins;
2038 conf.trimfuzz <- trimfuzz;
2039 let x0, y0, x1, y1 = trimfuzz in
2040 invalidate "settrim"
2041 (fun () ->
2042 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2043 flushpages ();
2046 let setzoom zoom =
2047 match state.throttle with
2048 | None ->
2049 let zoom = max 0.0001 zoom in
2050 if zoom <> conf.zoom
2051 then (
2052 state.prevzoom <- (conf.zoom, state.x);
2053 conf.zoom <- zoom;
2054 reshape state.winw state.winh;
2055 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2058 | Some (layout, y, started) ->
2059 let time =
2060 match conf.maxwait with
2061 | None -> 0.0
2062 | Some t -> t
2064 let dt = now () -. started in
2065 if dt > time
2066 then (
2067 state.y <- y;
2068 load layout;
2072 let setcolumns mode columns coverA coverB =
2073 state.prevcolumns <- Some (conf.columns, conf.zoom);
2074 if columns < 0
2075 then (
2076 if isbirdseye mode
2077 then showtext '!' "split mode doesn't work in bird's eye"
2078 else (
2079 conf.columns <- Csplit (-columns, E.a);
2080 state.x <- 0;
2081 conf.zoom <- 1.0;
2084 else (
2085 if columns < 2
2086 then (
2087 conf.columns <- Csingle E.a;
2088 state.x <- 0;
2089 setzoom 1.0;
2091 else (
2092 conf.columns <- Cmulti ((columns, coverA, coverB), E.a);
2093 conf.zoom <- 1.0;
2096 reshape state.winw state.winh;
2099 let resetmstate () =
2100 state.mstate <- Mnone;
2101 Wsi.setcursor Wsi.CURSOR_INHERIT;
2104 let enterbirdseye () =
2105 let zoom = float conf.thumbw /. float state.winw in
2106 let birdseyepageno =
2107 let cy = state.winh / 2 in
2108 let fold = function
2109 | [] -> 0
2110 | l :: rest ->
2111 let rec fold best = function
2112 | [] -> best.pageno
2113 | l :: rest ->
2114 let d = cy - (l.pagedispy + l.pagevh/2)
2115 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2116 if abs d < abs dbest
2117 then fold l rest
2118 else best.pageno
2119 in fold l rest
2121 fold state.layout
2123 state.mode <- Birdseye (
2124 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2126 resetmstate ();
2127 conf.zoom <- zoom;
2128 conf.presentation <- false;
2129 conf.interpagespace <- 10;
2130 conf.hlinks <- false;
2131 conf.fitmodel <- FitProportional;
2132 state.x <- 0;
2133 conf.maxwait <- None;
2134 conf.columns <- (
2135 match conf.beyecolumns with
2136 | Some c ->
2137 conf.zoom <- 1.0;
2138 Cmulti ((c, 0, 0), E.a)
2139 | None -> Csingle E.a
2141 if conf.verbose
2142 then
2143 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2144 (100.0*.zoom)
2145 else
2146 state.text <- E.s
2148 reshape state.winw state.winh;
2151 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2152 state.mode <- View;
2153 conf.zoom <- c.zoom;
2154 conf.presentation <- c.presentation;
2155 conf.interpagespace <- c.interpagespace;
2156 conf.maxwait <- c.maxwait;
2157 conf.hlinks <- c.hlinks;
2158 conf.fitmodel <- c.fitmodel;
2159 conf.beyecolumns <- (
2160 match conf.columns with
2161 | Cmulti ((c, _, _), _) -> Some c
2162 | Csingle _ -> None
2163 | Csplit _ -> failwith "leaving bird's eye split mode"
2165 conf.columns <- (
2166 match c.columns with
2167 | Cmulti (c, _) -> Cmulti (c, E.a)
2168 | Csingle _ -> Csingle E.a
2169 | Csplit (c, _) -> Csplit (c, E.a)
2171 if conf.verbose
2172 then
2173 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2174 (100.0*.conf.zoom)
2176 reshape state.winw state.winh;
2177 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2178 state.x <- leftx;
2181 let togglebirdseye () =
2182 match state.mode with
2183 | Birdseye vals -> leavebirdseye vals true
2184 | View -> enterbirdseye ()
2185 | _ -> ()
2188 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2189 let pageno = max 0 (pageno - incr) in
2190 let rec loop = function
2191 | [] -> gotopage1 pageno 0
2192 | l :: _ when l.pageno = pageno ->
2193 if l.pagedispy >= 0 && l.pagey = 0
2194 then G.postRedisplay "upbirdseye"
2195 else gotopage1 pageno 0
2196 | _ :: rest -> loop rest
2198 loop state.layout;
2199 state.text <- E.s;
2200 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2203 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2204 let pageno = min (state.pagecount - 1) (pageno + incr) in
2205 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2206 let rec loop = function
2207 | [] ->
2208 let y, h = getpageyh pageno in
2209 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2210 gotoy (clamp dy)
2211 | l :: _ when l.pageno = pageno ->
2212 if l.pagevh != l.pageh
2213 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2214 else G.postRedisplay "downbirdseye"
2215 | _ :: rest -> loop rest
2217 loop state.layout;
2218 state.text <- E.s;
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 | @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 | @enter | @kpenter ->
2487 ondone text;
2488 onleave Confirm;
2489 G.postRedisplay "textentrykeyboard after confirm"
2491 | @up | @kpup -> histaction HCprev
2492 | @down | @kpdown -> histaction HCnext
2493 | @home | @kphome -> histaction HCfirst
2494 | @jend | @kpend -> histaction HClast
2496 | @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 | @delete | @kpdelete -> ()
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 ~zebra ~helpmode ~(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:`src_alpha ~dst:`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 hw = (wadjsb (xadjsb state.winw))/3 in
2603 let ww = fstate.wwidth in
2604 let tabw = 17.0*.ww in
2605 let itemcount = source#getitemcount in
2606 let minfo = source#getminfo in
2607 let x0, x1 =
2608 if conf.leftscroll
2609 then float (xadjsb 0), float (state.winw - 1)
2610 else 0.0, float (state.winw - conf.scrollbw - 1)
2612 let rec loop row =
2613 if (row - m_first) > fstate.maxrows
2614 then ()
2615 else (
2616 if row >= 0 && row < itemcount
2617 then (
2618 let (s, level) = source#getitem row in
2619 let y = (row - m_first) * nfs in
2620 let x =
2621 (if conf.leftscroll then float (xadjsb 0) else 5.0)
2622 +. (float (level + m_pan)) *. ww in
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;
2635 let c =
2636 if zebra && row land 1 = 1
2637 then 0.8
2638 else 1.0
2640 GlDraw.color (c,c,c);
2641 let drawtabularstring s =
2642 let drawstr x s =
2643 let x' = truncate (x0 +. x) in
2644 let pos = nindex s '\000' in
2645 if pos = -1
2646 then drawstring1 fs x' (y+nfs) s
2647 else
2648 let s1 = String.sub s 0 pos
2649 and s2 = String.sub s (pos+1) (String.length s - pos - 1) in
2650 let rec e s =
2651 if emptystr s
2652 then s
2653 else
2654 let s' = withoutlastutf8 s in
2655 let s = s' ^ "@Uellipsis" in
2656 let w = measurestr fs s in
2657 if float x' +. w +. ww < float (hw + x')
2658 then s
2659 else e s'
2661 let s1 =
2662 if float x' +. ww +. measurestr fs s1 > float (hw + x')
2663 then e s1
2664 else s1
2666 ignore (drawstring1 fs x' (y+nfs) s1);
2667 drawstring1 fs (hw + x') (y+nfs) s2
2669 if trusted
2670 then
2671 let x = if helpmode && row > 0 then x +. ww else x in
2672 let tabpos = nindex s '\t' in
2673 if tabpos > 0
2674 then
2675 let len = String.length s - tabpos - 1 in
2676 let s1 = String.sub s 0 tabpos
2677 and s2 = String.sub s (tabpos + 1) len in
2678 let nx = drawstr x s1 in
2679 let sw = nx -. x in
2680 let x = x +. (max tabw sw) in
2681 drawstr x s2
2682 else
2683 let len = String.length s - 2 in
2684 if len > 0 && s.[0] = '\xc2' && s.[1] = '\xb7'
2685 then
2686 let s = String.sub s 2 len in
2687 let x = if not helpmode then x +. ww else x in
2688 GlDraw.color (1.2, 1.2, 1.2);
2689 let vinc = drawstring1 (fs+fs/4)
2690 (truncate (x -. ww)) (y+nfs) s in
2691 GlDraw.color (1., 1., 1.);
2692 vinc +. (float fs *. 0.8)
2693 else
2694 drawstr x s
2695 else
2696 drawstr x s
2698 ignore (drawtabularstring s);
2699 loop (row+1)
2703 loop m_first;
2704 GlDraw.color (1.0, 1.0, 1.0) ~alpha:0.5;
2705 let rec loop row =
2706 if (row - m_first) > fstate.maxrows
2707 then ()
2708 else (
2709 if row >= 0 && row < itemcount
2710 then (
2711 let (s, level) = source#getitem row in
2712 let pos0 = nindex s '\000' in
2713 let y = (row - m_first) * nfs in
2714 let x = float (level + m_pan) *. ww in
2715 let (first, last) = minfo.(row) in
2716 let prefix =
2717 if pos0 > 0 && first > pos0
2718 then String.sub s (pos0+1) (first-pos0-1)
2719 else String.sub s 0 first
2721 let suffix = String.sub s first (last - first) in
2722 let w1 = measurestr fstate.fontsize prefix in
2723 let w2 = measurestr fstate.fontsize suffix in
2724 let x = x +. if conf.leftscroll then float (xadjsb 5) else 5.0 in
2725 let x = if pos0 > 0 && first > pos0 then x +. float hw else x in
2726 let x0 = x +. w1
2727 and y0 = float (y+2) in
2728 let x1 = x0 +. w2
2729 and y1 = float (y+fs+3) in
2730 filledrect x0 y0 x1 y1;
2731 loop (row+1)
2735 Gl.disable `texture_2d;
2736 if Array.length minfo > 0 then loop m_first;
2737 Gl.disable `blend;
2739 method updownlevel incr =
2740 let len = source#getitemcount in
2741 let curlevel =
2742 if m_active >= 0 && m_active < len
2743 then snd (source#getitem m_active)
2744 else -1
2746 let rec flow i =
2747 if i = len then i-1 else if i = -1 then 0 else
2748 let _, l = source#getitem i in
2749 if l != curlevel then i else flow (i+incr)
2751 let active = flow m_active in
2752 let first = calcfirst m_first active in
2753 G.postRedisplay "outline updownlevel";
2754 {< m_active = active; m_first = first >}
2756 method private key1 key mask =
2757 let set1 active first qsearch =
2758 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2760 let search active pattern incr =
2761 let active = if active = -1 then m_first else active in
2762 let dosearch re =
2763 let rec loop n =
2764 if n >= 0 && n < source#getitemcount
2765 then (
2766 let s, _ = source#getitem n in
2768 (try ignore (Str.search_forward re s 0); true
2769 with Not_found -> false)
2770 then Some n
2771 else loop (n + incr)
2773 else None
2775 loop active
2778 let re = Str.regexp_case_fold pattern in
2779 dosearch re
2780 with Failure s ->
2781 state.text <- s;
2782 None
2784 let itemcount = source#getitemcount in
2785 let find start incr =
2786 let rec find i =
2787 if i = -1 || i = itemcount
2788 then -1
2789 else (
2790 if source#hasaction i
2791 then i
2792 else find (i + incr)
2795 find start
2797 let set active first =
2798 let first = bound first 0 (itemcount - fstate.maxrows) in
2799 state.text <- E.s;
2800 coe {< m_active = active; m_first = first; m_qsearch = E.s >}
2802 let navigate incr =
2803 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2804 let active, first =
2805 let incr1 = if incr > 0 then 1 else -1 in
2806 if isvisible m_first m_active
2807 then
2808 let next =
2809 let next = m_active + incr in
2810 let next =
2811 if next < 0 || next >= itemcount
2812 then -1
2813 else find next incr1
2815 if abs (m_active - next) > fstate.maxrows
2816 then -1
2817 else next
2819 if next = -1
2820 then
2821 let first = m_first + incr in
2822 let first = bound first 0 (itemcount - fstate.maxrows) in
2823 let next =
2824 let next = m_active + incr in
2825 let next = bound next 0 (itemcount - 1) in
2826 find next ~-incr1
2828 let active =
2829 if next = -1
2830 then m_active
2831 else (
2832 if isvisible first next
2833 then next
2834 else m_active
2837 active, first
2838 else
2839 let first = min next m_first in
2840 let first =
2841 if abs (next - first) > fstate.maxrows
2842 then first + incr
2843 else first
2845 next, first
2846 else
2847 let first = m_first + incr in
2848 let first = bound first 0 (itemcount - 1) in
2849 let active =
2850 let next = m_active + incr in
2851 let next = bound next 0 (itemcount - 1) in
2852 let next = find next incr1 in
2853 let active =
2854 if next = -1 || abs (m_active - first) > fstate.maxrows
2855 then (
2856 let active = if m_active = -1 then next else m_active in
2857 active
2859 else next
2861 if isvisible first active
2862 then active
2863 else -1
2865 active, first
2867 G.postRedisplay "listview navigate";
2868 set active first;
2870 match key with
2871 | (@r|@s) when Wsi.withctrl mask ->
2872 let incr = if key = @r then -1 else 1 in
2873 let active, first =
2874 match search (m_active + incr) m_qsearch incr with
2875 | None ->
2876 state.text <- m_qsearch ^ " [not found]";
2877 m_active, m_first
2878 | Some active ->
2879 state.text <- m_qsearch;
2880 active, firstof m_first active
2882 G.postRedisplay "listview ctrl-r/s";
2883 set1 active first m_qsearch;
2885 | @insert when Wsi.withctrl mask ->
2886 if m_active >= 0 && m_active < source#getitemcount
2887 then (
2888 let s, _ = source#getitem m_active in
2889 selstring s;
2891 coe self
2893 | @backspace ->
2894 if emptystr m_qsearch
2895 then coe self
2896 else (
2897 let qsearch = withoutlastutf8 m_qsearch in
2898 if emptystr qsearch
2899 then (
2900 state.text <- E.s;
2901 G.postRedisplay "listview empty qsearch";
2902 set1 m_active m_first E.s;
2904 else
2905 let active, first =
2906 match search m_active qsearch ~-1 with
2907 | None ->
2908 state.text <- qsearch ^ " [not found]";
2909 m_active, m_first
2910 | Some active ->
2911 state.text <- qsearch;
2912 active, firstof m_first active
2914 G.postRedisplay "listview backspace qsearch";
2915 set1 active first qsearch
2918 | key when (key != 0 && key land 0xff00 != 0xff00) ->
2919 let pattern = m_qsearch ^ toutf8 key in
2920 let active, first =
2921 match search m_active pattern 1 with
2922 | None ->
2923 state.text <- pattern ^ " [not found]";
2924 m_active, m_first
2925 | Some active ->
2926 state.text <- pattern;
2927 active, firstof m_first active
2929 G.postRedisplay "listview qsearch add";
2930 set1 active first pattern;
2932 | @escape ->
2933 state.text <- E.s;
2934 if emptystr m_qsearch
2935 then (
2936 G.postRedisplay "list view escape";
2937 begin
2938 match
2939 source#exit ~uioh:(coe self)
2940 ~cancel:true ~active:m_active ~first:m_first ~pan:m_pan
2941 with
2942 | None -> m_prev_uioh
2943 | Some uioh -> uioh
2946 else (
2947 G.postRedisplay "list view kill qsearch";
2948 coe {< m_qsearch = E.s >}
2951 | @enter | @kpenter ->
2952 state.text <- E.s;
2953 let self = {< m_qsearch = E.s >} in
2954 let opt =
2955 G.postRedisplay "listview enter";
2956 if m_active >= 0 && m_active < source#getitemcount
2957 then (
2958 source#exit ~uioh:(coe self) ~cancel:false
2959 ~active:m_active ~first:m_first ~pan:m_pan;
2961 else (
2962 source#exit ~uioh:(coe self) ~cancel:true
2963 ~active:m_active ~first:m_first ~pan:m_pan;
2966 begin match opt with
2967 | None -> m_prev_uioh
2968 | Some uioh -> uioh
2971 | @delete | @kpdelete ->
2972 coe self
2974 | @up | @kpup -> navigate ~-1
2975 | @down | @kpdown -> navigate 1
2976 | @prior | @kpprior -> navigate ~-(fstate.maxrows)
2977 | @next | @kpnext -> navigate fstate.maxrows
2979 | @right | @kpright ->
2980 state.text <- E.s;
2981 G.postRedisplay "listview right";
2982 coe {< m_pan = m_pan - 1 >}
2984 | @left | @kpleft ->
2985 state.text <- E.s;
2986 G.postRedisplay "listview left";
2987 coe {< m_pan = m_pan + 1 >}
2989 | @home | @kphome ->
2990 let active = find 0 1 in
2991 G.postRedisplay "listview home";
2992 set active 0;
2994 | @jend | @kpend ->
2995 let first = max 0 (itemcount - fstate.maxrows) in
2996 let active = find (itemcount - 1) ~-1 in
2997 G.postRedisplay "listview end";
2998 set active first;
3000 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3001 coe self
3003 | _ ->
3004 dolog "listview unknown key %#x" key; coe self
3006 method key key mask =
3007 match state.mode with
3008 | Textentry te -> textentrykeyboard key mask te; coe self
3009 | _ -> self#key1 key mask
3011 method button button down x y _ =
3012 let opt =
3013 match button with
3014 | 1 when x > state.winw - conf.scrollbw ->
3015 G.postRedisplay "listview scroll";
3016 if down
3017 then
3018 let _, position, sh = self#scrollph in
3019 if y > truncate position && y < truncate (position +. sh)
3020 then (
3021 state.mstate <- Mscrolly;
3022 Some (coe self)
3024 else
3025 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3026 let first = truncate (s *. float source#getitemcount) in
3027 let first = min source#getitemcount first in
3028 Some (coe {< m_first = first; m_active = first >})
3029 else (
3030 state.mstate <- Mnone;
3031 Some (coe self);
3033 | 1 when not down ->
3034 begin match self#elemunder y with
3035 | Some n ->
3036 G.postRedisplay "listview click";
3037 source#exit ~uioh:(coe {< m_active = n >})
3038 ~cancel:false ~active:n ~first:m_first ~pan:m_pan
3039 | _ ->
3040 Some (coe self)
3042 | n when (n == 4 || n == 5) && not down ->
3043 let len = source#getitemcount in
3044 let first =
3045 if n = 5 && m_first + fstate.maxrows >= len
3046 then
3047 m_first
3048 else
3049 let first = m_first + (if n == 4 then -1 else 1) in
3050 bound first 0 (len - 1)
3052 G.postRedisplay "listview wheel";
3053 Some (coe {< m_first = first >})
3054 | n when (n = 6 || n = 7) && not down ->
3055 let inc = if n = 7 then -1 else 1 in
3056 G.postRedisplay "listview hwheel";
3057 Some (coe {< m_pan = m_pan + inc >})
3058 | _ ->
3059 Some (coe self)
3061 match opt with
3062 | None -> m_prev_uioh
3063 | Some uioh -> uioh
3065 method multiclick _ x y = self#button 1 true x y
3067 method motion _ y =
3068 match state.mstate with
3069 | Mscrolly ->
3070 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3071 let first = truncate (s *. float source#getitemcount) in
3072 let first = min source#getitemcount first in
3073 G.postRedisplay "listview motion";
3074 coe {< m_first = first; m_active = first >}
3075 | _ -> coe self
3077 method pmotion x y =
3078 if x < state.winw - conf.scrollbw
3079 then
3080 let n =
3081 match self#elemunder y with
3082 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3083 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3085 let o =
3086 if n != m_active
3087 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3088 else self
3090 coe o
3091 else (
3092 Wsi.setcursor Wsi.CURSOR_INHERIT;
3093 coe self
3096 method infochanged _ = ()
3098 method scrollpw = (0, 0.0, 0.0)
3099 method scrollph =
3100 let nfs = fstate.fontsize + 1 in
3101 let y = m_first * nfs in
3102 let itemcount = source#getitemcount in
3103 let maxi = max 0 (itemcount - fstate.maxrows) in
3104 let maxy = maxi * nfs in
3105 let p, h = scrollph y maxy in
3106 conf.scrollbw, p, h
3108 method modehash = modehash
3109 method eformsgs = false
3110 end;;
3112 class outlinelistview ~zebra ~source =
3113 let settext autonarrow s =
3114 if autonarrow
3115 then
3116 let ss = source#statestr in
3117 state.text <-
3118 if emptystr ss
3119 then "[" ^ s ^ "]"
3120 else "{" ^ ss ^ "} [" ^ s ^ "]"
3121 else state.text <- s
3123 object (self)
3124 inherit listview
3125 ~zebra
3126 ~helpmode:false
3127 ~source:(source :> lvsource)
3128 ~trusted:false
3129 ~modehash:(findkeyhash conf "outline")
3130 as super
3132 val m_autonarrow = false
3134 method! key key mask =
3135 let maxrows =
3136 if emptystr state.text
3137 then fstate.maxrows
3138 else fstate.maxrows - 2
3140 let calcfirst first active =
3141 if active > first
3142 then
3143 let rows = active - first in
3144 if rows > maxrows then active - maxrows else first
3145 else active
3147 let navigate incr =
3148 let active = m_active + incr in
3149 let active = bound active 0 (source#getitemcount - 1) in
3150 let first = calcfirst m_first active in
3151 G.postRedisplay "outline navigate";
3152 coe {< m_active = active; m_first = first >}
3154 let navscroll first =
3155 let active =
3156 let dist = m_active - first in
3157 if dist < 0
3158 then first
3159 else (
3160 if dist < maxrows
3161 then m_active
3162 else first + maxrows
3165 G.postRedisplay "outline navscroll";
3166 coe {< m_first = first; m_active = active >}
3168 let ctrl = Wsi.withctrl mask in
3169 match key with
3170 | @a when ctrl ->
3171 let text =
3172 if m_autonarrow
3173 then (source#denarrow; E.s)
3174 else (
3175 let pattern = source#renarrow in
3176 if nonemptystr m_qsearch
3177 then (source#narrow m_qsearch; m_qsearch)
3178 else pattern
3181 settext (not m_autonarrow) text;
3182 G.postRedisplay "toggle auto narrowing";
3183 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
3185 | @slash when emptystr m_qsearch && not m_autonarrow ->
3186 settext true E.s;
3187 G.postRedisplay "toggle auto narrowing";
3188 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
3190 | @n when ctrl ->
3191 source#narrow m_qsearch;
3192 if not m_autonarrow
3193 then source#add_narrow_pattern m_qsearch;
3194 G.postRedisplay "outline ctrl-n";
3195 coe {< m_first = 0; m_active = 0 >}
3197 | @S when ctrl ->
3198 let active = source#calcactive (getanchor ()) in
3199 let first = firstof m_first active in
3200 G.postRedisplay "outline ctrl-s";
3201 coe {< m_first = first; m_active = active >}
3203 | @u when ctrl ->
3204 G.postRedisplay "outline ctrl-u";
3205 if m_autonarrow && nonemptystr m_qsearch
3206 then (
3207 ignore (source#renarrow);
3208 settext m_autonarrow E.s;
3209 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3211 else (
3212 source#del_narrow_pattern;
3213 let pattern = source#renarrow in
3214 let text =
3215 if emptystr pattern then E.s else "Narrowed to " ^ pattern
3217 settext m_autonarrow text;
3218 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3221 | @l when ctrl ->
3222 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3223 G.postRedisplay "outline ctrl-l";
3224 coe {< m_first = first >}
3226 | @tab when m_autonarrow ->
3227 if nonemptystr m_qsearch
3228 then (
3229 G.postRedisplay "outline list view tab";
3230 source#add_narrow_pattern m_qsearch;
3231 settext true E.s;
3232 coe {< m_qsearch = E.s >}
3234 else coe self
3236 | @escape when m_autonarrow ->
3237 if nonemptystr m_qsearch
3238 then source#add_narrow_pattern m_qsearch;
3239 super#key key mask
3241 | @enter | @kpenter when m_autonarrow ->
3242 if nonemptystr m_qsearch
3243 then source#add_narrow_pattern m_qsearch;
3244 super#key key mask
3246 | key when m_autonarrow && (key != 0 && key land 0xff00 != 0xff00) ->
3247 let pattern = m_qsearch ^ toutf8 key in
3248 G.postRedisplay "outlinelistview autonarrow add";
3249 source#narrow pattern;
3250 settext true pattern;
3251 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3253 | key when m_autonarrow && key = @backspace ->
3254 if emptystr m_qsearch
3255 then coe self
3256 else
3257 let pattern = withoutlastutf8 m_qsearch in
3258 G.postRedisplay "outlinelistview autonarrow backspace";
3259 ignore (source#renarrow);
3260 source#narrow pattern;
3261 settext true pattern;
3262 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3264 | @delete | @kpdelete ->
3265 source#remove m_active;
3266 G.postRedisplay "outline delete";
3267 let active = max 0 (m_active-1) in
3268 coe {< m_first = firstof m_first active;
3269 m_active = active >}
3271 | @up | @kpup when ctrl ->
3272 navscroll (max 0 (m_first - 1))
3274 | @down | @kpdown when ctrl ->
3275 navscroll (min (source#getitemcount - 1) (m_first + 1))
3277 | @up | @kpup -> navigate ~-1
3278 | @down | @kpdown -> navigate 1
3279 | @prior | @kpprior -> navigate ~-(fstate.maxrows)
3280 | @next | @kpnext -> navigate fstate.maxrows
3282 | @right | @kpright ->
3283 let o =
3284 if ctrl
3285 then (
3286 G.postRedisplay "outline ctrl right";
3287 {< m_pan = m_pan + 1 >}
3289 else self#updownlevel 1
3291 coe o
3293 | @left | @kpleft ->
3294 let o =
3295 if ctrl
3296 then (
3297 G.postRedisplay "outline ctrl left";
3298 {< m_pan = m_pan - 1 >}
3300 else self#updownlevel ~-1
3302 coe o
3304 | @home | @kphome ->
3305 G.postRedisplay "outline home";
3306 coe {< m_first = 0; m_active = 0 >}
3308 | @jend | @kpend ->
3309 let active = source#getitemcount - 1 in
3310 let first = max 0 (active - fstate.maxrows) in
3311 G.postRedisplay "outline end";
3312 coe {< m_active = active; m_first = first >}
3314 | _ -> super#key key mask
3317 let gotounder under =
3318 let getpath filename =
3319 let path =
3320 if nonemptystr filename
3321 then
3322 if Filename.is_relative filename
3323 then
3324 let dir = Filename.dirname state.path in
3325 let dir =
3326 if Filename.is_implicit dir
3327 then Filename.concat (Sys.getcwd ()) dir
3328 else dir
3330 Filename.concat dir filename
3331 else filename
3332 else E.s
3334 if Sys.file_exists path
3335 then path
3336 else E.s
3338 match under with
3339 | Ulinkgoto (pageno, top) ->
3340 if pageno >= 0
3341 then (
3342 addnav ();
3343 gotopage1 pageno top;
3346 | Ulinkuri s ->
3347 gotouri s
3349 | Uremote (filename, pageno) ->
3350 let path = getpath filename in
3351 if nonemptystr path
3352 then (
3353 if conf.riani
3354 then
3355 let command = Printf.sprintf "%s -page %d %S" !selfexec pageno path in
3356 try popen command []
3357 with exn ->
3358 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
3359 flush stderr;
3360 else
3361 let anchor = getanchor () in
3362 let ranchor = state.path, state.password, anchor, state.origin in
3363 state.origin <- E.s;
3364 state.anchor <- (pageno, 0.0, 0.0);
3365 state.ranchors <- ranchor :: state.ranchors;
3366 opendoc path E.s;
3368 else showtext '!' ("Could not find " ^ filename)
3370 | Uremotedest (filename, destname) ->
3371 let path = getpath filename in
3372 if nonemptystr path
3373 then (
3374 if conf.riani
3375 then
3376 let command = !selfexec ^ " " ^ path ^ " -dest " ^ destname in
3377 try popen command []
3378 with exn ->
3379 Printf.eprintf
3380 "failed to execute `%s': %s\n" command (exntos exn);
3381 flush stderr;
3382 else
3383 let anchor = getanchor () in
3384 let ranchor = state.path, state.password, anchor, state.origin in
3385 state.origin <- E.s;
3386 state.nameddest <- destname;
3387 state.ranchors <- ranchor :: state.ranchors;
3388 opendoc path E.s;
3390 else showtext '!' ("Could not find " ^ filename)
3392 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
3395 let gotohist (path, (c, bookmarks, x, anchor)) =
3396 Config.save leavebirdseye;
3397 state.anchor <- anchor;
3398 state.x <- x;
3399 state.bookmarks <- bookmarks;
3400 state.origin <- E.s;
3401 setconf conf c;
3402 opendoc path E.s;
3405 let gotooutline (_, _, kind) =
3406 match kind with
3407 | Onone -> ()
3408 | Oanchor anchor ->
3409 let (pageno, y, _) = anchor in
3410 let y = getanchory
3411 (if conf.presentation then (pageno, y, 1.0) else anchor)
3413 addnav ();
3414 gotoghyll y
3415 | Ouri uri -> gotounder (Ulinkuri uri)
3416 | Olaunch cmd -> gotounder (Ulaunch cmd)
3417 | Oremote remote -> gotounder (Uremote remote)
3418 | Ohistory hist -> gotohist hist
3419 | Oremotedest remotedest -> gotounder (Uremotedest remotedest)
3420 | Oaction f -> f ()
3423 let genhistoutlines =
3424 let order ty (p1, c1, _, _, _) (p2, c2, _, _, _) =
3425 match ty with
3426 | `lastvisit -> compare c1.lastvisit c2.lastvisit
3427 | `path -> compare p2 p1
3428 | `file -> compare (Filename.basename p2) (Filename.basename p1)
3429 | `title ->
3430 let e1 = emptystr c1.title
3431 and e2 = emptystr c2.title in
3432 (**) if e1 && e2
3433 then compare (Filename.basename p2) (Filename.basename p1)
3434 else if e1 then -1
3435 else if e2 then 1
3436 else compare c1.title c2.title
3438 let showfullpath = ref false in
3439 fun orderty ->
3440 let setorty s t =
3441 let s = if orderty = t then "[@Uradical] " ^ s else "[ ] " ^ s in
3442 s, 0, Oaction (fun () -> Config.historder := t; reeenterhist := true)
3444 let list = ref [] in
3445 if Config.gethist list
3446 then
3447 let ol =
3448 List.fold_left
3449 (fun accu (path, c, b, x, a) ->
3450 let hist = (path, (c, b, x, a)) in
3451 let s = if !showfullpath then path else Filename.basename path in
3452 let base = mbtoutf8 s in
3453 (base ^ "\000" ^ c.title, 1, Ohistory hist) :: accu
3455 [ setorty "Sort by time of last visit" `lastvisit;
3456 setorty "Sort by file name" `file;
3457 setorty "Sort by path" `path;
3458 setorty "Sort by title" `title;
3459 (if !showfullpath then "@Uradical "
3460 else " ") ^ "Show full path", 0, Oaction (fun () ->
3461 showfullpath := not !showfullpath; reeenterhist := true)
3462 ] (List.sort (order orderty) !list)
3464 Array.of_list ol
3465 else E.a;
3468 let outlinesource sourcetype =
3469 (object (self)
3470 inherit lvsourcebase
3471 val mutable m_items = E.a
3472 val mutable m_minfo = E.a
3473 val mutable m_orig_items = E.a
3474 val mutable m_orig_minfo = E.a
3475 val mutable m_narrow_patterns = []
3476 val mutable m_hadremovals = false
3477 val mutable m_gen = -1
3479 method getitemcount =
3480 Array.length m_items + (if m_hadremovals then 1 else 0)
3482 method getitem n =
3483 if n == Array.length m_items && m_hadremovals
3484 then
3485 ("[Confirm removal]", 0)
3486 else
3487 let s, n, _ = m_items.(n) in
3488 (s, n)
3490 method exit ~uioh ~cancel ~active ~first ~pan =
3491 ignore (uioh, first);
3492 let confrimremoval = m_hadremovals && active = Array.length m_items in
3493 let items, minfo =
3494 if m_narrow_patterns = []
3495 then m_orig_items, m_orig_minfo
3496 else m_items, m_minfo
3498 if not cancel
3499 then (
3500 if not confrimremoval
3501 then (
3502 gotooutline m_items.(active);
3503 m_items <- items;
3504 m_minfo <- minfo;
3506 else (
3507 state.bookmarks <- Array.to_list m_items;
3508 m_orig_items <- m_items;
3509 m_orig_minfo <- m_minfo;
3512 else (
3513 m_items <- items;
3514 m_minfo <- minfo;
3516 m_pan <- pan;
3517 None
3519 method hasaction _ = true
3521 method greetmsg =
3522 if Array.length m_items != Array.length m_orig_items
3523 then
3524 let s =
3525 match m_narrow_patterns with
3526 | one :: [] -> one
3527 | many -> String.concat "@Uellipsis" (List.rev many)
3529 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
3530 else E.s
3532 method statestr =
3533 match m_narrow_patterns with
3534 | [] -> E.s
3535 | one :: [] -> one
3536 | head :: _ -> "@Uellipsis" ^ head
3538 method narrow pattern =
3539 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3540 match reopt with
3541 | None -> ()
3542 | Some re ->
3543 let rec loop accu minfo n =
3544 if n = -1
3545 then (
3546 m_items <- Array.of_list accu;
3547 m_minfo <- Array.of_list minfo;
3549 else
3550 let (s, _, t) as o = m_items.(n) in
3551 let accu, minfo =
3552 match t with
3553 | Oaction _ -> o :: accu, (0, 0) :: minfo
3554 | Onone | Oanchor _ | Ouri _ | Olaunch _
3555 | Oremote _ | Oremotedest _ | Ohistory _ ->
3556 let first =
3557 try Str.search_forward re s 0
3558 with Not_found -> -1
3560 if first >= 0
3561 then o :: accu, (first, Str.match_end ()) :: minfo
3562 else accu, minfo
3564 loop accu minfo (n-1)
3566 loop [] [] (Array.length m_items - 1)
3568 method! getminfo = m_minfo
3570 method denarrow =
3571 m_orig_items <- (
3572 match sourcetype with
3573 | `bookmarks -> Array.of_list state.bookmarks
3574 | `outlines -> state.outlines
3575 | `history -> genhistoutlines !Config.historder
3577 m_minfo <- m_orig_minfo;
3578 m_items <- m_orig_items
3580 method remove m =
3581 if sourcetype = `bookmarks
3582 then
3583 if m >= 0 && m < Array.length m_items
3584 then (
3585 m_hadremovals <- true;
3586 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3587 let n = if n >= m then n+1 else n in
3588 m_items.(n)
3592 method add_narrow_pattern pattern =
3593 m_narrow_patterns <- pattern :: m_narrow_patterns
3595 method del_narrow_pattern =
3596 match m_narrow_patterns with
3597 | _ :: rest -> m_narrow_patterns <- rest
3598 | [] -> ()
3600 method renarrow =
3601 self#denarrow;
3602 match m_narrow_patterns with
3603 | pattern :: [] -> self#narrow pattern; pattern
3604 | list ->
3605 List.fold_left (fun accu pattern ->
3606 self#narrow pattern;
3607 pattern ^ "@Uellipsis" ^ accu) E.s list
3609 method calcactive anchor =
3610 let rely = getanchory anchor in
3611 let rec loop n best bestd =
3612 if n = Array.length m_items
3613 then best
3614 else
3615 let _, _, kind = m_items.(n) in
3616 match kind with
3617 | Oanchor anchor ->
3618 let orely = getanchory anchor in
3619 let d = abs (orely - rely) in
3620 if d < bestd
3621 then loop (n+1) n d
3622 else loop (n+1) best bestd
3623 | Onone | Oremote _ | Olaunch _
3624 | Oremotedest _ | Ouri _ | Ohistory _ | Oaction _ ->
3625 loop (n+1) best bestd
3627 loop 0 ~-1 max_int
3629 method reset anchor items =
3630 m_hadremovals <- false;
3631 if state.gen != m_gen
3632 then (
3633 m_orig_items <- items;
3634 m_items <- items;
3635 m_narrow_patterns <- [];
3636 m_minfo <- E.a;
3637 m_orig_minfo <- E.a;
3638 m_gen <- state.gen;
3640 else (
3641 if items != m_orig_items
3642 then (
3643 m_orig_items <- items;
3644 if m_narrow_patterns == []
3645 then m_items <- items;
3648 let active = self#calcactive anchor in
3649 m_active <- active;
3650 m_first <- firstof m_first active
3651 end)
3654 let enterselector sourcetype =
3655 resetmstate ();
3656 let source = outlinesource sourcetype in
3657 fun errmsg ->
3658 let outlines =
3659 match sourcetype with
3660 | `bookmarks -> Array.of_list state.bookmarks
3661 | `outlines -> state.outlines
3662 | `history -> genhistoutlines !Config.historder
3664 if Array.length outlines = 0
3665 then (
3666 showtext ' ' errmsg;
3668 else (
3669 state.text <- source#greetmsg;
3670 Wsi.setcursor Wsi.CURSOR_INHERIT;
3671 let anchor = getanchor () in
3672 source#reset anchor outlines;
3673 state.uioh <-
3674 coe (new outlinelistview ~zebra:(sourcetype=`history) ~source);
3675 G.postRedisplay "enter selector";
3679 let enteroutlinemode =
3680 let f = enterselector `outlines in
3681 fun () -> f "Document has no outline";
3684 let enterbookmarkmode =
3685 let f = enterselector `bookmarks in
3686 fun () -> f "Document has no bookmarks (yet)";
3689 let enterhistmode () = enterselector `history "No history (yet)";;
3691 let makecheckers () =
3692 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3693 following to say:
3694 converted by Issac Trotts. July 25, 2002 *)
3695 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3696 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3697 let id = GlTex.gen_texture () in
3698 GlTex.bind_texture ~target:`texture_2d id;
3699 GlPix.store (`unpack_alignment 1);
3700 GlTex.image2d image;
3701 List.iter (GlTex.parameter ~target:`texture_2d)
3702 [ `mag_filter `nearest; `min_filter `nearest ];
3706 let setcheckers enabled =
3707 match state.checkerstexid with
3708 | None ->
3709 if enabled then state.checkerstexid <- Some (makecheckers ())
3711 | Some checkerstexid ->
3712 if not enabled
3713 then (
3714 GlTex.delete_texture checkerstexid;
3715 state.checkerstexid <- None;
3719 let describe_location () =
3720 let fn = page_of_y state.y in
3721 let ln = page_of_y (state.y + state.winh - hscrollh () - 1) in
3722 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
3723 let percent =
3724 if maxy <= 0
3725 then 100.
3726 else (100. *. (float state.y /. float maxy))
3728 if fn = ln
3729 then
3730 Printf.sprintf "page %d of %d [%.2f%%]"
3731 (fn+1) state.pagecount percent
3732 else
3733 Printf.sprintf
3734 "pages %d-%d of %d [%.2f%%]"
3735 (fn+1) (ln+1) state.pagecount percent
3738 let setpresentationmode v =
3739 let n = page_of_y state.y in
3740 state.anchor <- (n, 0.0, 1.0);
3741 conf.presentation <- v;
3742 if conf.fitmodel = FitPage
3743 then reqlayout conf.angle conf.fitmodel;
3744 represent ();
3747 let enterinfomode =
3748 let btos b = if b then "@Uradical" else E.s in
3749 let showextended = ref false in
3750 let leave mode = function
3751 | Confirm -> state.mode <- mode
3752 | Cancel -> state.mode <- mode in
3753 let src =
3754 (object
3755 val mutable m_first_time = true
3756 val mutable m_l = []
3757 val mutable m_a = E.a
3758 val mutable m_prev_uioh = nouioh
3759 val mutable m_prev_mode = View
3761 inherit lvsourcebase
3763 method reset prev_mode prev_uioh =
3764 m_a <- Array.of_list (List.rev m_l);
3765 m_l <- [];
3766 m_prev_mode <- prev_mode;
3767 m_prev_uioh <- prev_uioh;
3768 if m_first_time
3769 then (
3770 let rec loop n =
3771 if n >= Array.length m_a
3772 then ()
3773 else
3774 match m_a.(n) with
3775 | _, _, _, Action _ -> m_active <- n
3776 | _ -> loop (n+1)
3778 loop 0;
3779 m_first_time <- false;
3782 method int name get set =
3783 m_l <-
3784 (name, `int get, 1, Action (
3785 fun u ->
3786 let ondone s =
3787 try set (int_of_string s)
3788 with exn ->
3789 state.text <- Printf.sprintf "bad integer `%s': %s"
3790 s (exntos exn)
3792 state.text <- E.s;
3793 let te = name ^ ": ", E.s, None, intentry, ondone, true in
3794 state.mode <- Textentry (te, leave m_prev_mode);
3796 )) :: m_l
3798 method int_with_suffix name get set =
3799 m_l <-
3800 (name, `intws get, 1, Action (
3801 fun u ->
3802 let ondone s =
3803 try set (int_of_string_with_suffix s)
3804 with exn ->
3805 state.text <- Printf.sprintf "bad integer `%s': %s"
3806 s (exntos exn)
3808 state.text <- E.s;
3809 let te =
3810 name ^ ": ", E.s, None, intentry_with_suffix, ondone, true
3812 state.mode <- Textentry (te, leave m_prev_mode);
3814 )) :: m_l
3816 method bool ?(offset=1) ?(btos=btos) name get set =
3817 m_l <-
3818 (name, `bool (btos, get), offset, Action (
3819 fun u ->
3820 let v = get () in
3821 set (not v);
3823 )) :: m_l
3825 method color name get set =
3826 m_l <-
3827 (name, `color get, 1, Action (
3828 fun u ->
3829 let invalid = (nan, nan, nan) in
3830 let ondone s =
3831 let c =
3832 try color_of_string s
3833 with exn ->
3834 state.text <- Printf.sprintf "bad color `%s': %s"
3835 s (exntos exn);
3836 invalid
3838 if c <> invalid
3839 then set c;
3841 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3842 state.text <- color_to_string (get ());
3843 state.mode <- Textentry (te, leave m_prev_mode);
3845 )) :: m_l
3847 method string name get set =
3848 m_l <-
3849 (name, `string get, 1, Action (
3850 fun u ->
3851 let ondone s = set s in
3852 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3853 state.mode <- Textentry (te, leave m_prev_mode);
3855 )) :: m_l
3857 method colorspace name get set =
3858 m_l <-
3859 (name, `string get, 1, Action (
3860 fun _ ->
3861 let source =
3862 (object
3863 inherit lvsourcebase
3865 initializer
3866 m_active <- CSTE.to_int conf.colorspace;
3867 m_first <- 0;
3869 method getitemcount =
3870 Array.length CSTE.names
3871 method getitem n =
3872 (CSTE.names.(n), 0)
3873 method exit ~uioh ~cancel ~active ~first ~pan =
3874 ignore (uioh, first, pan);
3875 if not cancel then set active;
3876 None
3877 method hasaction _ = true
3878 end)
3880 state.text <- E.s;
3881 let modehash = findkeyhash conf "info" in
3882 coe (new listview ~zebra:false ~helpmode:false
3883 ~source ~trusted:true ~modehash)
3884 )) :: m_l
3886 method paxmark name get set =
3887 m_l <-
3888 (name, `string get, 1, Action (
3889 fun _ ->
3890 let source =
3891 (object
3892 inherit lvsourcebase
3894 initializer
3895 m_active <- MTE.to_int conf.paxmark;
3896 m_first <- 0;
3898 method getitemcount = Array.length MTE.names
3899 method getitem n = (MTE.names.(n), 0)
3900 method exit ~uioh ~cancel ~active ~first ~pan =
3901 ignore (uioh, first, pan);
3902 if not cancel then set active;
3903 None
3904 method hasaction _ = true
3905 end)
3907 state.text <- E.s;
3908 let modehash = findkeyhash conf "info" in
3909 coe (new listview ~zebra:false ~helpmode:false
3910 ~source ~trusted:true ~modehash)
3911 )) :: m_l
3913 method fitmodel name get set =
3914 m_l <-
3915 (name, `string get, 1, Action (
3916 fun _ ->
3917 let source =
3918 (object
3919 inherit lvsourcebase
3921 initializer
3922 m_active <- FMTE.to_int conf.fitmodel;
3923 m_first <- 0;
3925 method getitemcount = Array.length FMTE.names
3926 method getitem n = (FMTE.names.(n), 0)
3927 method exit ~uioh ~cancel ~active ~first ~pan =
3928 ignore (uioh, first, pan);
3929 if not cancel then set active;
3930 None
3931 method hasaction _ = true
3932 end)
3934 state.text <- E.s;
3935 let modehash = findkeyhash conf "info" in
3936 coe (new listview ~zebra:false ~helpmode:false
3937 ~source ~trusted:true ~modehash)
3938 )) :: m_l
3940 method caption s offset =
3941 m_l <- (s, `empty, offset, Noaction) :: m_l
3943 method caption2 s f offset =
3944 m_l <- (s, `string f, offset, Noaction) :: m_l
3946 method getitemcount = Array.length m_a
3948 method getitem n =
3949 let tostr = function
3950 | `int f -> string_of_int (f ())
3951 | `intws f -> string_with_suffix_of_int (f ())
3952 | `string f -> f ()
3953 | `color f -> color_to_string (f ())
3954 | `bool (btos, f) -> btos (f ())
3955 | `empty -> E.s
3957 let name, t, offset, _ = m_a.(n) in
3958 ((let s = tostr t in
3959 if nonemptystr s
3960 then Printf.sprintf "%s\t%s" name s
3961 else name),
3962 offset)
3964 method exit ~uioh ~cancel ~active ~first ~pan =
3965 let uiohopt =
3966 if not cancel
3967 then (
3968 let uioh =
3969 match m_a.(active) with
3970 | _, _, _, Action f -> f uioh
3971 | _ -> uioh
3973 Some uioh
3975 else None
3977 m_active <- active;
3978 m_first <- first;
3979 m_pan <- pan;
3980 uiohopt
3982 method hasaction n =
3983 match m_a.(n) with
3984 | _, _, _, Action _ -> true
3985 | _ -> false
3986 end)
3988 let rec fillsrc prevmode prevuioh =
3989 let sep () = src#caption E.s 0 in
3990 let colorp name get set =
3991 src#string name
3992 (fun () -> color_to_string (get ()))
3993 (fun v ->
3995 let c = color_of_string v in
3996 set c
3997 with exn ->
3998 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
4001 let oldmode = state.mode in
4002 let birdseye = isbirdseye state.mode in
4004 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4006 src#bool "presentation mode"
4007 (fun () -> conf.presentation)
4008 (fun v -> setpresentationmode v);
4010 src#bool "ignore case in searches"
4011 (fun () -> conf.icase)
4012 (fun v -> conf.icase <- v);
4014 src#bool "preload"
4015 (fun () -> conf.preload)
4016 (fun v -> conf.preload <- v);
4018 src#bool "highlight links"
4019 (fun () -> conf.hlinks)
4020 (fun v -> conf.hlinks <- v);
4022 src#bool "under info"
4023 (fun () -> conf.underinfo)
4024 (fun v -> conf.underinfo <- v);
4026 src#bool "persistent bookmarks"
4027 (fun () -> conf.savebmarks)
4028 (fun v -> conf.savebmarks <- v);
4030 src#fitmodel "fit model"
4031 (fun () -> FMTE.to_string conf.fitmodel)
4032 (fun v -> reqlayout conf.angle (FMTE.of_int v));
4034 src#bool "trim margins"
4035 (fun () -> conf.trimmargins)
4036 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4038 src#bool "persistent location"
4039 (fun () -> conf.jumpback)
4040 (fun v -> conf.jumpback <- v);
4042 sep ();
4043 src#int "inter-page space"
4044 (fun () -> conf.interpagespace)
4045 (fun n ->
4046 conf.interpagespace <- n;
4047 docolumns conf.columns;
4048 let pageno, py =
4049 match state.layout with
4050 | [] -> 0, 0
4051 | l :: _ ->
4052 l.pageno, l.pagey
4054 state.maxy <- calcheight ();
4055 let y = getpagey pageno in
4056 gotoy (y + py)
4059 src#int "page bias"
4060 (fun () -> conf.pagebias)
4061 (fun v -> conf.pagebias <- v);
4063 src#int "scroll step"
4064 (fun () -> conf.scrollstep)
4065 (fun n -> conf.scrollstep <- n);
4067 src#int "horizontal scroll step"
4068 (fun () -> conf.hscrollstep)
4069 (fun v -> conf.hscrollstep <- v);
4071 src#int "auto scroll step"
4072 (fun () ->
4073 match state.autoscroll with
4074 | Some step -> step
4075 | _ -> conf.autoscrollstep)
4076 (fun n ->
4077 let n = boundastep state.winh n in
4078 if state.autoscroll <> None
4079 then state.autoscroll <- Some n;
4080 conf.autoscrollstep <- n);
4082 src#int "zoom"
4083 (fun () -> truncate (conf.zoom *. 100.))
4084 (fun v -> setzoom ((float v) /. 100.));
4086 src#int "rotation"
4087 (fun () -> conf.angle)
4088 (fun v -> reqlayout v conf.fitmodel);
4090 src#int "scroll bar width"
4091 (fun () -> conf.scrollbw)
4092 (fun v ->
4093 conf.scrollbw <- v;
4094 reshape state.winw state.winh;
4097 src#int "scroll handle height"
4098 (fun () -> conf.scrollh)
4099 (fun v -> conf.scrollh <- v;);
4101 src#int "thumbnail width"
4102 (fun () -> conf.thumbw)
4103 (fun v ->
4104 conf.thumbw <- min 4096 v;
4105 match oldmode with
4106 | Birdseye beye ->
4107 leavebirdseye beye false;
4108 enterbirdseye ()
4109 | _ -> ()
4112 let mode = state.mode in
4113 src#string "columns"
4114 (fun () ->
4115 match conf.columns with
4116 | Csingle _ -> "1"
4117 | Cmulti (multi, _) -> multicolumns_to_string multi
4118 | Csplit (count, _) -> "-" ^ string_of_int count
4120 (fun v ->
4121 let n, a, b = multicolumns_of_string v in
4122 setcolumns mode n a b);
4124 sep ();
4125 src#caption "Pixmap cache" 0;
4126 src#int_with_suffix "size (advisory)"
4127 (fun () -> conf.memlimit)
4128 (fun v -> conf.memlimit <- v);
4130 src#caption2 "used"
4131 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4132 (string_with_suffix_of_int state.memused)
4133 (Hashtbl.length state.tilemap)) 1;
4135 sep ();
4136 src#caption "Layout" 0;
4137 src#caption2 "Dimension"
4138 (fun () ->
4139 Printf.sprintf "%dx%d (virtual %dx%d)"
4140 state.winw state.winh
4141 state.w state.maxy)
4143 if conf.debug
4144 then
4145 src#caption2 "Position" (fun () ->
4146 Printf.sprintf "%dx%d" state.x state.y
4148 else
4149 src#caption2 "Position" (fun () -> describe_location ()) 1
4152 sep ();
4153 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4154 "Save these parameters as global defaults at exit"
4155 (fun () -> conf.bedefault)
4156 (fun v -> conf.bedefault <- v)
4159 sep ();
4160 let btos b = if b then "@Ulguillemet" else "@Urguillemet" in
4161 src#bool ~offset:0 ~btos "Extended parameters"
4162 (fun () -> !showextended)
4163 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4164 if !showextended
4165 then (
4166 src#bool "checkers"
4167 (fun () -> conf.checkers)
4168 (fun v -> conf.checkers <- v; setcheckers v);
4169 src#bool "update cursor"
4170 (fun () -> conf.updatecurs)
4171 (fun v -> conf.updatecurs <- v);
4172 src#bool "scroll-bar on the left"
4173 (fun () -> conf.leftscroll)
4174 (fun v -> conf.leftscroll <- v);
4175 src#bool "verbose"
4176 (fun () -> conf.verbose)
4177 (fun v -> conf.verbose <- v);
4178 src#bool "invert colors"
4179 (fun () -> conf.invert)
4180 (fun v -> conf.invert <- v);
4181 src#bool "max fit"
4182 (fun () -> conf.maxhfit)
4183 (fun v -> conf.maxhfit <- v);
4184 src#bool "redirect stderr"
4185 (fun () -> conf.redirectstderr)
4186 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4187 src#bool "pax mode"
4188 (fun () -> conf.pax != None)
4189 (fun v ->
4190 if v
4191 then conf.pax <- Some (ref (now (), 0, 0))
4192 else conf.pax <- None);
4193 src#string "uri launcher"
4194 (fun () -> conf.urilauncher)
4195 (fun v -> conf.urilauncher <- v);
4196 src#string "path launcher"
4197 (fun () -> conf.pathlauncher)
4198 (fun v -> conf.pathlauncher <- v);
4199 src#string "tile size"
4200 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4201 (fun v ->
4203 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4204 conf.tilew <- max 64 w;
4205 conf.tileh <- max 64 h;
4206 flushtiles ();
4207 with exn ->
4208 state.text <- Printf.sprintf "bad tile size `%s': %s"
4209 v (exntos exn)
4211 src#int "texture count"
4212 (fun () -> conf.texcount)
4213 (fun v ->
4214 if realloctexts v
4215 then conf.texcount <- v
4216 else showtext '!' " Failed to set texture count please retry later"
4218 src#int "slice height"
4219 (fun () -> conf.sliceheight)
4220 (fun v ->
4221 conf.sliceheight <- v;
4222 wcmd "sliceh %d" conf.sliceheight;
4224 src#int "anti-aliasing level"
4225 (fun () -> conf.aalevel)
4226 (fun v ->
4227 conf.aalevel <- bound v 0 8;
4228 state.anchor <- getanchor ();
4229 opendoc state.path state.password;
4231 src#string "page scroll scaling factor"
4232 (fun () -> string_of_float conf.pgscale)
4233 (fun v ->
4235 let s = float_of_string v in
4236 conf.pgscale <- s
4237 with exn ->
4238 state.text <- Printf.sprintf
4239 "bad page scroll scaling factor `%s': %s" v (exntos exn)
4242 src#int "ui font size"
4243 (fun () -> fstate.fontsize)
4244 (fun v -> setfontsize (bound v 5 100));
4245 src#int "hint font size"
4246 (fun () -> conf.hfsize)
4247 (fun v -> conf.hfsize <- bound v 5 100);
4248 colorp "background color"
4249 (fun () -> conf.bgcolor)
4250 (fun v -> conf.bgcolor <- v);
4251 src#bool "crop hack"
4252 (fun () -> conf.crophack)
4253 (fun v -> conf.crophack <- v);
4254 src#string "trim fuzz"
4255 (fun () -> irect_to_string conf.trimfuzz)
4256 (fun v ->
4258 conf.trimfuzz <- irect_of_string v;
4259 if conf.trimmargins
4260 then settrim true conf.trimfuzz;
4261 with exn ->
4262 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
4264 src#string "throttle"
4265 (fun () ->
4266 match conf.maxwait with
4267 | None -> "show place holder if page is not ready"
4268 | Some time ->
4269 if time = infinity
4270 then "wait for page to fully render"
4271 else
4272 "wait " ^ string_of_float time
4273 ^ " seconds before showing placeholder"
4275 (fun v ->
4277 let f = float_of_string v in
4278 if f <= 0.0
4279 then conf.maxwait <- None
4280 else conf.maxwait <- Some f
4281 with exn ->
4282 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
4284 src#string "ghyll scroll"
4285 (fun () ->
4286 match conf.ghyllscroll with
4287 | None -> E.s
4288 | Some nab -> ghyllscroll_to_string nab
4290 (fun v ->
4291 try conf.ghyllscroll <- ghyllscroll_of_string v
4292 with exn ->
4293 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
4295 src#string "selection command"
4296 (fun () -> conf.selcmd)
4297 (fun v -> conf.selcmd <- v);
4298 src#string "synctex command"
4299 (fun () -> conf.stcmd)
4300 (fun v -> conf.stcmd <- v);
4301 src#string "pax command"
4302 (fun () -> conf.paxcmd)
4303 (fun v -> conf.paxcmd <- v);
4304 src#colorspace "color space"
4305 (fun () -> CSTE.to_string conf.colorspace)
4306 (fun v ->
4307 conf.colorspace <- CSTE.of_int v;
4308 wcmd "cs %d" v;
4309 load state.layout;
4311 src#paxmark "pax mark method"
4312 (fun () -> MTE.to_string conf.paxmark)
4313 (fun v -> conf.paxmark <- MTE.of_int v);
4314 if pbousable ()
4315 then
4316 src#bool "use PBO"
4317 (fun () -> conf.usepbo)
4318 (fun v -> conf.usepbo <- v);
4319 src#bool "mouse wheel scrolls pages"
4320 (fun () -> conf.wheelbypage)
4321 (fun v -> conf.wheelbypage <- v);
4322 src#bool "open remote links in a new instance"
4323 (fun () -> conf.riani)
4324 (fun v -> conf.riani <- v);
4327 sep ();
4328 src#caption "Document" 0;
4329 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4330 src#caption2 "Pages"
4331 (fun () -> string_of_int state.pagecount) 1;
4332 src#caption2 "Dimensions"
4333 (fun () -> string_of_int (List.length state.pdims)) 1;
4334 if conf.trimmargins
4335 then (
4336 sep ();
4337 src#caption "Trimmed margins" 0;
4338 src#caption2 "Dimensions"
4339 (fun () -> string_of_int (List.length state.pdims)) 1;
4342 sep ();
4343 src#caption "OpenGL" 0;
4344 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4345 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4347 sep ();
4348 src#caption "Location" 0;
4349 if nonemptystr state.origin
4350 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
4351 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
4353 src#reset prevmode prevuioh;
4355 fun () ->
4356 state.text <- E.s;
4357 resetmstate ();
4358 let prevmode = state.mode
4359 and prevuioh = state.uioh in
4360 fillsrc prevmode prevuioh;
4361 let source = (src :> lvsource) in
4362 let modehash = findkeyhash conf "info" in
4363 state.uioh <- coe (object (self)
4364 inherit listview ~zebra:false ~helpmode:false
4365 ~source ~trusted:true ~modehash as super
4366 val mutable m_prevmemused = 0
4367 method! infochanged = function
4368 | Memused ->
4369 if m_prevmemused != state.memused
4370 then (
4371 m_prevmemused <- state.memused;
4372 G.postRedisplay "memusedchanged";
4374 | Pdim -> G.postRedisplay "pdimchanged"
4375 | Docinfo -> fillsrc prevmode prevuioh
4377 method! key key mask =
4378 if not (Wsi.withctrl mask)
4379 then
4380 match key with
4381 | @left | @kpleft -> coe (self#updownlevel ~-1)
4382 | @right | @kpright -> coe (self#updownlevel 1)
4383 | _ -> super#key key mask
4384 else super#key key mask
4385 end);
4386 G.postRedisplay "info";
4389 let enterhelpmode =
4390 let source =
4391 (object
4392 inherit lvsourcebase
4393 method getitemcount = Array.length state.help
4394 method getitem n =
4395 let s, l, _ = state.help.(n) in
4396 (s, l)
4398 method exit ~uioh ~cancel ~active ~first ~pan =
4399 let optuioh =
4400 if not cancel
4401 then (
4402 match state.help.(active) with
4403 | _, _, Action f -> Some (f uioh)
4404 | _ -> Some (uioh)
4406 else None
4408 m_active <- active;
4409 m_first <- first;
4410 m_pan <- pan;
4411 optuioh
4413 method hasaction n =
4414 match state.help.(n) with
4415 | _, _, Action _ -> true
4416 | _ -> false
4418 initializer
4419 m_active <- -1
4420 end)
4421 in fun () ->
4422 let modehash = findkeyhash conf "help" in
4423 resetmstate ();
4424 state.uioh <- coe (new listview
4425 ~zebra:false ~helpmode:true
4426 ~source ~trusted:true ~modehash);
4427 G.postRedisplay "help";
4430 let entermsgsmode =
4431 let msgsource =
4432 let re = Str.regexp "[\r\n]" in
4433 (object
4434 inherit lvsourcebase
4435 val mutable m_items = E.a
4437 method getitemcount = 1 + Array.length m_items
4439 method getitem n =
4440 if n = 0
4441 then "[Clear]", 0
4442 else m_items.(n-1), 0
4444 method exit ~uioh ~cancel ~active ~first ~pan =
4445 ignore uioh;
4446 if not cancel
4447 then (
4448 if active = 0
4449 then Buffer.clear state.errmsgs;
4451 m_active <- active;
4452 m_first <- first;
4453 m_pan <- pan;
4454 None
4456 method hasaction n =
4457 n = 0
4459 method reset =
4460 state.newerrmsgs <- false;
4461 let l = Str.split re (Buffer.contents state.errmsgs) in
4462 m_items <- Array.of_list l
4464 initializer
4465 m_active <- 0
4466 end)
4467 in fun () ->
4468 state.text <- E.s;
4469 resetmstate ();
4470 msgsource#reset;
4471 let source = (msgsource :> lvsource) in
4472 let modehash = findkeyhash conf "listview" in
4473 state.uioh <- coe (object
4474 inherit listview ~zebra:false ~helpmode:false
4475 ~source ~trusted:false ~modehash as super
4476 method! display =
4477 if state.newerrmsgs
4478 then msgsource#reset;
4479 super#display
4480 end);
4481 G.postRedisplay "msgs";
4484 let quickbookmark ?title () =
4485 match state.layout with
4486 | [] -> ()
4487 | l :: _ ->
4488 let title =
4489 match title with
4490 | None ->
4491 let tm = Unix.localtime (now ()) in
4492 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4493 (l.pageno+1)
4494 tm.Unix.tm_mday
4495 tm.Unix.tm_mon
4496 (tm.Unix.tm_year + 1900)
4497 tm.Unix.tm_hour
4498 tm.Unix.tm_min
4499 | Some title -> title
4501 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4504 let setautoscrollspeed step goingdown =
4505 let incr = max 1 ((abs step) / 2) in
4506 let incr = if goingdown then incr else -incr in
4507 let astep = boundastep state.winh (step + incr) in
4508 state.autoscroll <- Some astep;
4511 let canpan () =
4512 match conf.columns with
4513 | Csplit _ -> true
4514 | _ -> state.x != 0 || conf.zoom > 1.0
4517 let panbound x = bound x (-state.w) (wadjsb state.winw);;
4519 let existsinrow pageno (columns, coverA, coverB) p =
4520 let last = ((pageno - coverA) mod columns) + columns in
4521 let rec any = function
4522 | [] -> false
4523 | l :: rest ->
4524 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4525 then p l
4526 else (
4527 if not (p l)
4528 then (if l.pageno = last then false else any rest)
4529 else true
4532 any state.layout
4535 let nextpage () =
4536 match state.layout with
4537 | [] ->
4538 let pageno = page_of_y state.y in
4539 gotoghyll (getpagey (pageno+1))
4540 | l :: rest ->
4541 match conf.columns with
4542 | Csingle _ ->
4543 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4544 then
4545 let y = clamp (pgscale state.winh) in
4546 gotoghyll y
4547 else
4548 let pageno = min (l.pageno+1) (state.pagecount-1) in
4549 gotoghyll (getpagey pageno)
4550 | Cmulti ((c, _, _) as cl, _) ->
4551 if conf.presentation
4552 && (existsinrow l.pageno cl
4553 (fun l -> l.pageh > l.pagey + l.pagevh))
4554 then
4555 let y = clamp (pgscale state.winh) in
4556 gotoghyll y
4557 else
4558 let pageno = min (l.pageno+c) (state.pagecount-1) in
4559 gotoghyll (getpagey pageno)
4560 | Csplit (n, _) ->
4561 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4562 then
4563 let pagey, pageh = getpageyh l.pageno in
4564 let pagey = pagey + pageh * l.pagecol in
4565 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4566 gotoghyll (pagey + pageh + ips)
4569 let prevpage () =
4570 match state.layout with
4571 | [] ->
4572 let pageno = page_of_y state.y in
4573 gotoghyll (getpagey (pageno-1))
4574 | l :: _ ->
4575 match conf.columns with
4576 | Csingle _ ->
4577 if conf.presentation && l.pagey != 0
4578 then
4579 gotoghyll (clamp (pgscale ~-(state.winh)))
4580 else
4581 let pageno = max 0 (l.pageno-1) in
4582 gotoghyll (getpagey pageno)
4583 | Cmulti ((c, _, coverB) as cl, _) ->
4584 if conf.presentation &&
4585 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4586 then
4587 gotoghyll (clamp (pgscale ~-(state.winh)))
4588 else
4589 let decr =
4590 if l.pageno = state.pagecount - coverB
4591 then 1
4592 else c
4594 let pageno = max 0 (l.pageno-decr) in
4595 gotoghyll (getpagey pageno)
4596 | Csplit (n, _) ->
4597 let y =
4598 if l.pagecol = 0
4599 then
4600 if l.pageno = 0
4601 then l.pagey
4602 else
4603 let pageno = max 0 (l.pageno-1) in
4604 let pagey, pageh = getpageyh pageno in
4605 pagey + (n-1)*pageh
4606 else
4607 let pagey, pageh = getpageyh l.pageno in
4608 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4610 gotoghyll y
4613 let viewkeyboard key mask =
4614 let enttext te =
4615 let mode = state.mode in
4616 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4617 state.text <- E.s;
4618 enttext ();
4619 G.postRedisplay "view:enttext"
4621 let ctrl = Wsi.withctrl mask in
4622 let key =
4623 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
4625 match key with
4626 | @Q -> exit 0
4627 | @insert ->
4628 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4629 then (
4630 state.mode <- LinkNav (Ltgendir 0);
4631 gotoy state.y;
4633 else showtext '!' "Keyboard link navigation does not work under rotation"
4635 | @escape | @q ->
4636 begin match state.mstate with
4637 | Mzoomrect _ ->
4638 resetmstate ();
4639 G.postRedisplay "kill zoom rect";
4640 | _ ->
4641 begin match state.mode with
4642 | LinkNav _ ->
4643 state.mode <- View;
4644 G.postRedisplay "esc leave linknav"
4645 | _ ->
4646 match state.ranchors with
4647 | [] -> raise Quit
4648 | (path, password, anchor, origin) :: rest ->
4649 state.ranchors <- rest;
4650 state.anchor <- anchor;
4651 state.origin <- origin;
4652 state.nameddest <- E.s;
4653 opendoc path password
4654 end;
4655 end;
4657 | @backspace ->
4658 gotoghyll (getnav ~-1)
4660 | @o ->
4661 enteroutlinemode ()
4663 | @H ->
4664 enterhistmode ()
4666 | @u ->
4667 state.rects <- [];
4668 state.text <- E.s;
4669 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap;
4670 G.postRedisplay "dehighlight";
4672 | @slash | @question ->
4673 let ondone isforw s =
4674 cbput state.hists.pat s;
4675 state.searchpattern <- s;
4676 search s isforw
4678 let s = String.create 1 in
4679 s.[0] <- Char.chr key;
4680 enttext (s, E.s, Some (onhist state.hists.pat),
4681 textentry, ondone (key = @slash), true)
4683 | @plus | @kpplus | @equals when ctrl ->
4684 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4685 setzoom (conf.zoom +. incr)
4687 | @plus | @kpplus ->
4688 let ondone s =
4689 let n =
4690 try int_of_string s with exc ->
4691 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
4692 max_int
4694 if n != max_int
4695 then (
4696 conf.pagebias <- n;
4697 state.text <- "page bias is now " ^ string_of_int n;
4700 enttext ("page bias: ", E.s, None, intentry, ondone, true)
4702 | @minus | @kpminus when ctrl ->
4703 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4704 setzoom (max 0.01 (conf.zoom -. decr))
4706 | @minus | @kpminus ->
4707 let ondone msg = state.text <- msg in
4708 enttext (
4709 "option [acfhilpstvxACFPRSZTISM]: ", E.s, None,
4710 optentry state.mode, ondone, true
4713 | @0 when ctrl ->
4714 if conf.zoom = 1.0
4715 then (
4716 state.x <- 0;
4717 gotoy state.y
4719 else setzoom 1.0
4721 | (@1 | @2) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
4722 let cols =
4723 match conf.columns with
4724 | Csingle _ | Cmulti _ -> 1
4725 | Csplit (n, _) -> n
4727 let h = state.winh -
4728 conf.interpagespace lsl (if conf.presentation then 1 else 0)
4730 let zoom = zoomforh state.winw h (vscrollw ()) cols in
4731 if zoom > 0.0 && (key = 50 || zoom < 1.0)
4732 then setzoom zoom
4734 | @3 when ctrl ->
4735 let fm =
4736 match conf.fitmodel with
4737 | FitWidth -> FitProportional
4738 | FitProportional -> FitPage
4739 | FitPage -> FitWidth
4741 state.text <- "fit model: " ^ FMTE.to_string fm;
4742 reqlayout conf.angle fm
4744 | @F9 ->
4745 togglebirdseye ()
4747 | @9 when ctrl ->
4748 togglebirdseye ()
4750 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4751 when not ctrl -> (* 0..9 *)
4752 let ondone s =
4753 let n =
4754 try int_of_string s with exc ->
4755 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
4758 if n >= 0
4759 then (
4760 addnav ();
4761 cbput state.hists.pag (string_of_int n);
4762 gotopage1 (n + conf.pagebias - 1) 0;
4765 let pageentry text key =
4766 match Char.unsafe_chr key with
4767 | 'g' -> TEdone text
4768 | _ -> intentry text key
4770 let text = "x" in text.[0] <- Char.chr key;
4771 enttext (":", text, Some (onhist state.hists.pag),
4772 pageentry, ondone, true)
4774 | @b ->
4775 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
4776 reshape state.winw state.winh;
4778 | @B ->
4779 state.bzoom <- not state.bzoom;
4780 state.rects <- [];
4781 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
4783 | @l ->
4784 conf.hlinks <- not conf.hlinks;
4785 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4786 G.postRedisplay "toggle highlightlinks";
4788 | @F ->
4789 state.glinks <- true;
4790 let mode = state.mode in
4791 state.mode <- Textentry (
4792 (":", E.s, None, linknentry, linkndone gotounder, false),
4793 (fun _ ->
4794 state.glinks <- false;
4795 state.mode <- mode)
4797 state.text <- E.s;
4798 G.postRedisplay "view:linkent(F)"
4800 | @y ->
4801 state.glinks <- true;
4802 let mode = state.mode in
4803 state.mode <- Textentry (
4805 ":", E.s, None, linknentry, linkndone (fun under ->
4806 selstring (undertext under);
4807 ), false
4809 fun _ ->
4810 state.glinks <- false;
4811 state.mode <- mode
4813 state.text <- E.s;
4814 G.postRedisplay "view:linkent"
4816 | @a ->
4817 begin match state.autoscroll with
4818 | Some step ->
4819 conf.autoscrollstep <- step;
4820 state.autoscroll <- None
4821 | None ->
4822 if conf.autoscrollstep = 0
4823 then state.autoscroll <- Some 1
4824 else state.autoscroll <- Some conf.autoscrollstep
4827 | @p when ctrl ->
4828 launchpath ()
4830 | @P ->
4831 setpresentationmode (not conf.presentation);
4832 showtext ' ' ("presentation mode " ^
4833 if conf.presentation then "on" else "off");
4835 | @f ->
4836 if List.mem Wsi.Fullscreen state.winstate
4837 then Wsi.reshape conf.cwinw conf.cwinh
4838 else Wsi.fullscreen ()
4840 | @p | @N ->
4841 search state.searchpattern false
4843 | @n | @F3 ->
4844 search state.searchpattern true
4846 | @t ->
4847 begin match state.layout with
4848 | [] -> ()
4849 | l :: _ ->
4850 gotoghyll (getpagey l.pageno)
4853 | @space ->
4854 nextpage ()
4856 | @delete | @kpdelete -> (* delete *)
4857 prevpage ()
4859 | @equals ->
4860 showtext ' ' (describe_location ());
4862 | @w ->
4863 begin match state.layout with
4864 | [] -> ()
4865 | l :: _ ->
4866 Wsi.reshape (l.pagew + vscrollw ()) l.pageh;
4867 G.postRedisplay "w"
4870 | @apos ->
4871 enterbookmarkmode ()
4873 | @h | @F1 ->
4874 enterhelpmode ()
4876 | @i ->
4877 enterinfomode ()
4879 | @e when Buffer.length state.errmsgs > 0 ->
4880 entermsgsmode ()
4882 | @m ->
4883 let ondone s =
4884 match state.layout with
4885 | l :: _ ->
4886 if nonemptystr s
4887 then
4888 state.bookmarks <-
4889 (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4890 | _ -> ()
4892 enttext ("bookmark: ", E.s, None, textentry, ondone, true)
4894 | @tilde ->
4895 quickbookmark ();
4896 showtext ' ' "Quick bookmark added";
4898 | @z ->
4899 begin match state.layout with
4900 | l :: _ ->
4901 let rect = getpdimrect l.pagedimno in
4902 let w, h =
4903 if conf.crophack
4904 then
4905 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4906 truncate (1.2 *. (rect.(3) -. rect.(0))))
4907 else
4908 (truncate (rect.(1) -. rect.(0)),
4909 truncate (rect.(3) -. rect.(0)))
4911 let w = truncate ((float w)*.conf.zoom)
4912 and h = truncate ((float h)*.conf.zoom) in
4913 if w != 0 && h != 0
4914 then (
4915 state.anchor <- getanchor ();
4916 Wsi.reshape (w + vscrollw ()) (h + conf.interpagespace)
4918 G.postRedisplay "z";
4920 | [] -> ()
4923 | @x -> state.roam ()
4925 | @Lt | @Gt ->
4926 reqlayout (conf.angle +
4927 (if key = @question then 30 else -30)) conf.fitmodel
4929 | @Lb | @Rb ->
4930 conf.colorscale <-
4931 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
4933 G.postRedisplay "brightness";
4935 | @c when state.mode = View ->
4936 if Wsi.withalt mask
4937 then (
4938 if conf.zoom > 1.0
4939 then
4940 let m = (wadjsb state.winw - state.w) / 2 in
4941 state.x <- m;
4942 gotoy_and_clear_text state.y
4944 else
4945 let (c, a, b), z =
4946 match state.prevcolumns with
4947 | None -> (1, 0, 0), 1.0
4948 | Some (columns, z) ->
4949 let cab =
4950 match columns with
4951 | Csplit (c, _) -> -c, 0, 0
4952 | Cmulti ((c, a, b), _) -> c, a, b
4953 | Csingle _ -> 1, 0, 0
4955 cab, z
4957 setcolumns View c a b;
4958 setzoom z
4960 | @down | @up when ctrl && Wsi.withshift mask ->
4961 let zoom, x = state.prevzoom in
4962 setzoom zoom;
4963 state.x <- x;
4965 | @k | @up | @kpup ->
4966 begin match state.autoscroll with
4967 | None ->
4968 begin match state.mode with
4969 | Birdseye beye -> upbirdseye 1 beye
4970 | _ ->
4971 if ctrl
4972 then gotoy_and_clear_text (clamp ~-(state.winh/2))
4973 else (
4974 if not (Wsi.withshift mask) && conf.presentation
4975 then prevpage ()
4976 else gotoghyll1 true (clamp (-conf.scrollstep))
4979 | Some n ->
4980 setautoscrollspeed n false
4983 | @j | @down | @kpdown ->
4984 begin match state.autoscroll with
4985 | None ->
4986 begin match state.mode with
4987 | Birdseye beye -> downbirdseye 1 beye
4988 | _ ->
4989 if ctrl
4990 then gotoy_and_clear_text (clamp (state.winh/2))
4991 else (
4992 if not (Wsi.withshift mask) && conf.presentation
4993 then nextpage ()
4994 else gotoghyll1 true (clamp (conf.scrollstep))
4997 | Some n ->
4998 setautoscrollspeed n true
5001 | @left | @right | @kpleft | @kpright when not (Wsi.withalt mask) ->
5002 if canpan ()
5003 then
5004 let dx =
5005 if ctrl
5006 then state.winw / 2
5007 else conf.hscrollstep
5009 let dx = if key = @left || key = @kpleft then dx else -dx in
5010 state.x <- panbound (state.x + dx);
5011 gotoy_and_clear_text state.y
5012 else (
5013 state.text <- E.s;
5014 G.postRedisplay "left/right"
5017 | @prior | @kpprior ->
5018 let y =
5019 if ctrl
5020 then
5021 match state.layout with
5022 | [] -> state.y
5023 | l :: _ -> state.y - l.pagey
5024 else
5025 clamp (pgscale (-state.winh))
5027 gotoghyll y
5029 | @next | @kpnext ->
5030 let y =
5031 if ctrl
5032 then
5033 match List.rev state.layout with
5034 | [] -> state.y
5035 | l :: _ -> getpagey l.pageno
5036 else
5037 clamp (pgscale state.winh)
5039 gotoghyll y
5041 | @g | @home | @kphome ->
5042 addnav ();
5043 gotoghyll 0
5044 | @G | @jend | @kpend ->
5045 addnav ();
5046 gotoghyll (clamp state.maxy)
5048 | @right | @kpright when Wsi.withalt mask ->
5049 gotoghyll (getnav 1)
5050 | @left | @kpleft when Wsi.withalt mask ->
5051 gotoghyll (getnav ~-1)
5053 | @r ->
5054 reload ()
5056 | @v when conf.debug ->
5057 state.rects <- [];
5058 List.iter (fun l ->
5059 match getopaque l.pageno with
5060 | None -> ()
5061 | Some opaque ->
5062 let x0, y0, x1, y1 = pagebbox opaque in
5063 let a,b = float x0, float y0 in
5064 let c,d = float x1, float y0 in
5065 let e,f = float x1, float y1 in
5066 let h,j = float x0, float y1 in
5067 let rect = (a,b,c,d,e,f,h,j) in
5068 debugrect rect;
5069 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5070 ) state.layout;
5071 G.postRedisplay "v";
5073 | @pipe ->
5074 let mode = state.mode in
5075 let cmd = ref E.s in
5076 let onleave = function
5077 | Cancel -> state.mode <- mode
5078 | Confirm ->
5079 List.iter (fun l ->
5080 match getopaque l.pageno with
5081 | Some opaque -> pipesel opaque !cmd
5082 | None -> ()) state.layout;
5083 state.mode <- mode
5085 let ondone s =
5086 cbput state.hists.sel s;
5087 cmd := s
5089 let te =
5090 "| ", !cmd, Some (onhist state.hists.sel), textentry, ondone, true
5092 G.postRedisplay "|";
5093 state.mode <- Textentry (te, onleave);
5095 | _ ->
5096 vlog "huh? %s" (Wsi.keyname key)
5099 let linknavkeyboard key mask linknav =
5100 let getpage pageno =
5101 let rec loop = function
5102 | [] -> None
5103 | l :: _ when l.pageno = pageno -> Some l
5104 | _ :: rest -> loop rest
5105 in loop state.layout
5107 let doexact (pageno, n) =
5108 match getopaque pageno, getpage pageno with
5109 | Some opaque, Some l ->
5110 if key = @enter || key = @kpenter
5111 then
5112 let under = getlink opaque n in
5113 G.postRedisplay "link gotounder";
5114 gotounder under;
5115 state.mode <- View;
5116 else
5117 let opt, dir =
5118 match key with
5119 | @home ->
5120 Some (findlink opaque LDfirst), -1
5122 | @jend ->
5123 Some (findlink opaque LDlast), 1
5125 | @left ->
5126 Some (findlink opaque (LDleft n)), -1
5128 | @right ->
5129 Some (findlink opaque (LDright n)), 1
5131 | @up ->
5132 Some (findlink opaque (LDup n)), -1
5134 | @down ->
5135 Some (findlink opaque (LDdown n)), 1
5137 | _ -> None, 0
5139 let pwl l dir =
5140 begin match findpwl l.pageno dir with
5141 | Pwlnotfound -> ()
5142 | Pwl pageno ->
5143 let notfound dir =
5144 state.mode <- LinkNav (Ltgendir dir);
5145 let y, h = getpageyh pageno in
5146 let y =
5147 if dir < 0
5148 then y + h - state.winh
5149 else y
5151 gotoy y
5153 begin match getopaque pageno, getpage pageno with
5154 | Some opaque, Some _ ->
5155 let link =
5156 let ld = if dir > 0 then LDfirst else LDlast in
5157 findlink opaque ld
5159 begin match link with
5160 | Lfound m ->
5161 showlinktype (getlink opaque m);
5162 state.mode <- LinkNav (Ltexact (pageno, m));
5163 G.postRedisplay "linknav jpage";
5164 | _ -> notfound dir
5165 end;
5166 | _ -> notfound dir
5167 end;
5168 end;
5170 begin match opt with
5171 | Some Lnotfound -> pwl l dir;
5172 | Some (Lfound m) ->
5173 if m = n
5174 then pwl l dir
5175 else (
5176 let _, y0, _, y1 = getlinkrect opaque m in
5177 if y0 < l.pagey
5178 then gotopage1 l.pageno y0
5179 else (
5180 let d = fstate.fontsize + 1 in
5181 if y1 - l.pagey > l.pagevh - d
5182 then gotopage1 l.pageno (y1 - state.winh - hscrollh () + d)
5183 else G.postRedisplay "linknav";
5185 showlinktype (getlink opaque m);
5186 state.mode <- LinkNav (Ltexact (l.pageno, m));
5189 | None -> viewkeyboard key mask
5190 end;
5191 | _ -> viewkeyboard key mask
5193 if key = @insert
5194 then (
5195 state.mode <- View;
5196 G.postRedisplay "leave linknav"
5198 else
5199 match linknav with
5200 | Ltgendir _ -> viewkeyboard key mask
5201 | Ltexact exact -> doexact exact
5204 let keyboard key mask =
5205 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5206 then wcmd "interrupt"
5207 else state.uioh <- state.uioh#key key mask
5210 let birdseyekeyboard key mask
5211 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5212 let incr =
5213 match conf.columns with
5214 | Csingle _ -> 1
5215 | Cmulti ((c, _, _), _) -> c
5216 | Csplit _ -> failwith "bird's eye split mode"
5218 let pgh layout = List.fold_left
5219 (fun m l -> max l.pageh m) state.winh layout in
5220 match key with
5221 | @l when Wsi.withctrl mask ->
5222 let y, h = getpageyh pageno in
5223 let top = (state.winh - h) / 2 in
5224 gotoy (max 0 (y - top))
5225 | @enter | @kpenter -> leavebirdseye beye false
5226 | @escape -> leavebirdseye beye true
5227 | @up -> upbirdseye incr beye
5228 | @down -> downbirdseye incr beye
5229 | @left -> upbirdseye 1 beye
5230 | @right -> downbirdseye 1 beye
5232 | @prior ->
5233 begin match state.layout with
5234 | l :: _ ->
5235 if l.pagey != 0
5236 then (
5237 state.mode <- Birdseye (
5238 oconf, leftx, l.pageno, hooverpageno, anchor
5240 gotopage1 l.pageno 0;
5242 else (
5243 let layout = layout (state.y-state.winh) (pgh state.layout) in
5244 match layout with
5245 | [] -> gotoy (clamp (-state.winh))
5246 | l :: _ ->
5247 state.mode <- Birdseye (
5248 oconf, leftx, l.pageno, hooverpageno, anchor
5250 gotopage1 l.pageno 0
5253 | [] -> gotoy (clamp (-state.winh))
5254 end;
5256 | @next ->
5257 begin match List.rev state.layout with
5258 | l :: _ ->
5259 let layout = layout (state.y + (pgh state.layout)) state.winh in
5260 begin match layout with
5261 | [] ->
5262 let incr = l.pageh - l.pagevh in
5263 if incr = 0
5264 then (
5265 state.mode <-
5266 Birdseye (
5267 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5269 G.postRedisplay "birdseye pagedown";
5271 else gotoy (clamp (incr + conf.interpagespace*2));
5273 | l :: _ ->
5274 state.mode <-
5275 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5276 gotopage1 l.pageno 0;
5279 | [] -> gotoy (clamp state.winh)
5280 end;
5282 | @home ->
5283 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5284 gotopage1 0 0
5286 | @jend ->
5287 let pageno = state.pagecount - 1 in
5288 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5289 if not (pagevisible state.layout pageno)
5290 then
5291 let h =
5292 match List.rev state.pdims with
5293 | [] -> state.winh
5294 | (_, _, h, _) :: _ -> h
5296 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5297 else G.postRedisplay "birdseye end";
5299 | _ -> viewkeyboard key mask
5302 let drawpage l =
5303 let color =
5304 match state.mode with
5305 | Textentry _ -> scalecolor 0.4
5306 | LinkNav _
5307 | View -> scalecolor 1.0
5308 | Birdseye (_, _, pageno, hooverpageno, _) ->
5309 if l.pageno = hooverpageno
5310 then scalecolor 0.9
5311 else (
5312 if l.pageno = pageno
5313 then (
5314 let c = scalecolor 1.0 in
5315 GlDraw.color c;
5316 GlDraw.line_width 3.0;
5317 let dispx = xadjsb l.pagedispx in
5318 linerect
5319 (float (dispx-1)) (float (l.pagedispy-1))
5320 (float (dispx+l.pagevw+1))
5321 (float (l.pagedispy+l.pagevh+1))
5323 GlDraw.line_width 1.0;
5326 else scalecolor 0.8
5329 drawtiles l color;
5332 let postdrawpage l linkindexbase =
5333 match getopaque l.pageno with
5334 | Some opaque ->
5335 if tileready l l.pagex l.pagey
5336 then
5337 let x = l.pagedispx - l.pagex + xadjsb 0
5338 and y = l.pagedispy - l.pagey in
5339 let hlmask =
5340 match conf.columns with
5341 | Csingle _ | Cmulti _ ->
5342 (if conf.hlinks then 1 else 0)
5343 + (if state.glinks
5344 && not (isbirdseye state.mode) then 2 else 0)
5345 | _ -> 0
5347 let s =
5348 match state.mode with
5349 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5350 | _ -> E.s
5352 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5353 else 0
5354 | _ -> 0
5357 let scrollindicator () =
5358 let sbw, ph, sh = state.uioh#scrollph in
5359 let sbh, pw, sw = state.uioh#scrollpw in
5361 let x0,x1 =
5362 if conf.leftscroll
5363 then (0, sbw)
5364 else (state.winw - sbw), state.winw
5367 GlDraw.color (0.64, 0.64, 0.64);
5368 filledrect (float x0) 0. (float x1) (float state.winh);
5369 filledrect
5370 0. (float (state.winh - sbh))
5371 (float (wadjsb state.winw - 1)) (float state.winh)
5373 GlDraw.color (0.0, 0.0, 0.0);
5375 filledrect (float x0) ph (float x1) (ph +. sh);
5376 filledrect pw (float (state.winh - sbh)) (pw +. sw) (float state.winh);
5379 let showsel () =
5380 match state.mstate with
5381 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5384 | Msel ((x0, y0), (x1, y1)) ->
5385 let identify opaque l px py = Some (opaque, l.pageno, px, py) in
5386 let o0,n0,px0,py0 = onppundermouse identify x0 y0 (~< E.s, -1, 0, 0) in
5387 let _o1,n1,px1,py1 = onppundermouse identify x1 y1 (~< E.s, -1, 0, 0) in
5388 if n0 != -1 && n0 = n1 then seltext o0 (px0, py0, px1, py1);
5391 let showrects = function [] -> () | rects ->
5392 Gl.enable `blend;
5393 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5394 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5395 List.iter
5396 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5397 List.iter (fun l ->
5398 if l.pageno = pageno
5399 then (
5400 let dx = float (l.pagedispx - l.pagex) in
5401 let dy = float (l.pagedispy - l.pagey) in
5402 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5403 Raw.sets_float state.vraw ~pos:0
5404 [| x0+.dx; y0+.dy;
5405 x1+.dx; y1+.dy;
5406 x3+.dx; y3+.dy;
5407 x2+.dx; y2+.dy |];
5408 GlArray.vertex `two state.vraw;
5409 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
5411 ) state.layout
5412 ) rects
5414 Gl.disable `blend;
5417 let display () =
5418 GlClear.color (scalecolor2 conf.bgcolor);
5419 GlClear.clear [`color];
5420 List.iter drawpage state.layout;
5421 let rects =
5422 match state.mode with
5423 | LinkNav (Ltexact (pageno, linkno)) ->
5424 begin match getopaque pageno with
5425 | Some opaque ->
5426 let dx = xadjsb 0 in
5427 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5428 let x0 = x0 + dx and x1 = x1 + dx in
5429 (pageno, 5, (
5430 float x0, float y0,
5431 float x1, float y0,
5432 float x1, float y1,
5433 float x0, float y1)
5434 ) :: state.rects
5435 | None -> state.rects
5437 | _ -> state.rects
5439 showrects rects;
5440 let rec postloop linkindexbase = function
5441 | l :: rest ->
5442 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5443 postloop linkindexbase rest
5444 | [] -> ()
5446 showsel ();
5447 postloop 0 state.layout;
5448 state.uioh#display;
5449 begin match state.mstate with
5450 | Mzoomrect ((x0, y0), (x1, y1)) ->
5451 Gl.enable `blend;
5452 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5453 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5454 filledrect (float x0) (float y0) (float x1) (float y1);
5455 Gl.disable `blend;
5456 | _ -> ()
5457 end;
5458 enttext ();
5459 scrollindicator ();
5460 Wsi.swapb ();
5463 let zoomrect x y x1 y1 =
5464 let x0 = min x x1
5465 and x1 = max x x1
5466 and y0 = min y y1 in
5467 gotoy (state.y + y0);
5468 state.anchor <- getanchor ();
5469 let zoom = (float state.w) /. float (x1 - x0) in
5470 let margin =
5471 match conf.fitmodel, conf.columns with
5472 | FitPage, Csplit _ ->
5473 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
5475 | _, _ ->
5476 let adjw = wadjsb state.winw in
5477 if state.w < adjw
5478 then (adjw - state.w) / 2
5479 else 0
5481 state.x <- (state.x + margin) - x0;
5482 setzoom zoom;
5483 resetmstate ();
5486 let zoomblock x y =
5487 let g opaque l px py =
5488 match rectofblock opaque px py with
5489 | Some a ->
5490 let x0 = a.(0) -. 20. in
5491 let x1 = a.(1) +. 20. in
5492 let y0 = a.(2) -. 20. in
5493 let zoom = (float state.w) /. (x1 -. x0) in
5494 let pagey = getpagey l.pageno in
5495 gotoy_and_clear_text (pagey + truncate y0);
5496 state.anchor <- getanchor ();
5497 let margin = (state.w - l.pagew)/2 in
5498 state.x <- -truncate x0 - margin;
5499 setzoom zoom;
5500 None
5501 | None -> None
5503 match conf.columns with
5504 | Csplit _ ->
5505 showtext '!' "block zooming does not work properly in split columns mode"
5506 | _ -> onppundermouse g x y ()
5509 let scrollx x =
5510 let winw = wadjsb state.winw - 1 in
5511 let s = float x /. float winw in
5512 let destx = truncate (float (state.w + winw) *. s) in
5513 state.x <- winw - destx;
5514 gotoy_and_clear_text state.y;
5515 state.mstate <- Mscrollx;
5518 let scrolly y =
5519 let s = float y /. float state.winh in
5520 let desty = truncate (float (state.maxy - state.winh) *. s) in
5521 gotoy_and_clear_text desty;
5522 state.mstate <- Mscrolly;
5525 let viewmulticlick clicks x y mask =
5526 let g opaque l px py =
5527 let mark =
5528 match clicks with
5529 | 2 -> Mark_word
5530 | 3 -> Mark_line
5531 | 4 -> Mark_block
5532 | _ -> Mark_page
5534 if markunder opaque px py mark
5535 then (
5536 Some (fun () ->
5537 let dopipe cmd =
5538 match getopaque l.pageno with
5539 | None -> ()
5540 | Some opaque -> pipesel opaque cmd
5542 state.roam <- (fun () -> dopipe conf.paxcmd);
5543 if not (Wsi.withctrl mask) then dopipe conf.selcmd;
5546 else None
5548 G.postRedisplay "viewmulticlick";
5549 onppundermouse g x y (fun () -> showtext '!' "Nothing to select") ();
5552 let canselect () =
5553 match conf.columns with
5554 | Csplit _ -> false
5555 | Csingle _ | Cmulti _ -> conf.angle mod 360 = 0
5558 let viewmouse button down x y mask =
5559 match button with
5560 | n when (n == 4 || n == 5) && not down ->
5561 if Wsi.withctrl mask
5562 then (
5563 match state.mstate with
5564 | Mzoom (oldn, i) ->
5565 if oldn = n
5566 then (
5567 if i = 2
5568 then
5569 let incr =
5570 match n with
5571 | 5 ->
5572 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5573 | _ ->
5574 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5576 let zoom = conf.zoom -. incr in
5577 setzoom zoom;
5578 state.mstate <- Mzoom (n, 0);
5579 else
5580 state.mstate <- Mzoom (n, i+1);
5582 else state.mstate <- Mzoom (n, 0)
5584 | _ -> state.mstate <- Mzoom (n, 0)
5586 else (
5587 match state.autoscroll with
5588 | Some step -> setautoscrollspeed step (n=4)
5589 | None ->
5590 if conf.wheelbypage || conf.presentation
5591 then (
5592 if n = 4
5593 then prevpage ()
5594 else nextpage ()
5596 else
5597 let incr =
5598 if n = 4
5599 then -conf.scrollstep
5600 else conf.scrollstep
5602 let incr = incr * 2 in
5603 let y = clamp incr in
5604 gotoy_and_clear_text y
5607 | n when (n = 6 || n = 7) && not down && canpan () ->
5608 state.x <-
5609 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
5610 gotoy_and_clear_text state.y
5612 | 1 when Wsi.withshift mask ->
5613 state.mstate <- Mnone;
5614 if not down
5615 then (
5616 match unproject x y with
5617 | Some (pageno, ux, uy) ->
5618 let cmd = Printf.sprintf
5619 "%s %s %d %d %d"
5620 conf.stcmd state.path pageno ux uy
5622 popen cmd []
5623 | None -> ()
5626 | 1 when Wsi.withctrl mask ->
5627 if down
5628 then (
5629 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5630 state.mstate <- Mpan (x, y)
5632 else
5633 state.mstate <- Mnone
5635 | 3 ->
5636 if down
5637 then (
5638 Wsi.setcursor Wsi.CURSOR_CYCLE;
5639 let p = (x, y) in
5640 state.mstate <- Mzoomrect (p, p)
5642 else (
5643 match state.mstate with
5644 | Mzoomrect ((x0, y0), _) ->
5645 if abs (x-x0) > 10 && abs (y - y0) > 10
5646 then zoomrect x0 y0 x y
5647 else (
5648 resetmstate ();
5649 G.postRedisplay "kill accidental zoom rect";
5651 | _ ->
5652 resetmstate ()
5655 | 1 when x > state.winw - vscrollw () ->
5656 if down
5657 then
5658 let _, position, sh = state.uioh#scrollph in
5659 if y > truncate position && y < truncate (position +. sh)
5660 then state.mstate <- Mscrolly
5661 else scrolly y
5662 else
5663 state.mstate <- Mnone
5665 | 1 when y > state.winh - hscrollh () ->
5666 if down
5667 then
5668 let _, position, sw = state.uioh#scrollpw in
5669 if x > truncate position && x < truncate (position +. sw)
5670 then state.mstate <- Mscrollx
5671 else scrollx x
5672 else
5673 state.mstate <- Mnone
5675 | 1 when state.bzoom -> if not down then zoomblock x y
5677 | 1 ->
5678 let dest = if down then getunder x y else Unone in
5679 begin match dest with
5680 | Ulinkgoto _
5681 | Ulinkuri _
5682 | Uremote _ | Uremotedest _
5683 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5684 gotounder dest
5686 | Unone when down ->
5687 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5688 state.mstate <- Mpan (x, y);
5690 | Unone | Utext _ ->
5691 if down
5692 then (
5693 if canselect ()
5694 then (
5695 state.mstate <- Msel ((x, y), (x, y));
5696 G.postRedisplay "mouse select";
5699 else (
5700 match state.mstate with
5701 | Mnone -> ()
5703 | Mzoom _ | Mscrollx | Mscrolly ->
5704 state.mstate <- Mnone
5706 | Mzoomrect ((x0, y0), _) ->
5707 zoomrect x0 y0 x y
5709 | Mpan _ ->
5710 Wsi.setcursor Wsi.CURSOR_INHERIT;
5711 state.mstate <- Mnone
5713 | Msel ((x0, y0), (x1, y1)) ->
5714 let rec loop = function
5715 | [] -> ()
5716 | l :: rest ->
5717 let inside =
5718 let a0 = l.pagedispy in
5719 let a1 = a0 + l.pagevh in
5720 let b0 = l.pagedispx in
5721 let b1 = b0 + l.pagevw in
5722 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5723 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5725 if inside
5726 then
5727 match getopaque l.pageno with
5728 | Some opaque ->
5729 let dosel cmd () =
5730 match Ne.res Unix.pipe with
5731 | Ne.Exn exn ->
5732 showtext '!'
5733 (Printf.sprintf
5734 "can not create sel pipe: %s"
5735 (exntos exn));
5736 | Ne.Res (r, w) ->
5737 let clo what fd =
5738 Ne.clo fd (fun msg ->
5739 dolog "%s close failed: %s" what msg)
5741 let popened =
5742 try popen cmd [r, 0; w, -1]; true
5743 with exn ->
5744 dolog "can not execute %S: %s"
5745 cmd (exntos exn);
5746 false
5748 if popened
5749 then (
5750 copysel w opaque;
5751 G.postRedisplay "copysel";
5753 else clo "Msel pipe/w" w;
5754 clo "Msel pipe/r" r;
5756 dosel conf.selcmd ();
5757 state.roam <- dosel conf.paxcmd;
5758 | None -> ()
5759 else loop rest
5761 loop state.layout;
5762 resetmstate ();
5766 | _ -> ()
5769 let birdseyemouse button down x y mask
5770 (conf, leftx, _, hooverpageno, anchor) =
5771 match button with
5772 | 1 when down ->
5773 let rec loop = function
5774 | [] -> ()
5775 | l :: rest ->
5776 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5777 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5778 then (
5779 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5781 else loop rest
5783 loop state.layout
5784 | 3 -> ()
5785 | _ -> viewmouse button down x y mask
5788 let uioh = object
5789 method display = ()
5791 method key key mask =
5792 begin match state.mode with
5793 | Textentry textentry -> textentrykeyboard key mask textentry
5794 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5795 | View -> viewkeyboard key mask
5796 | LinkNav linknav -> linknavkeyboard key mask linknav
5797 end;
5798 state.uioh
5800 method button button bstate x y mask =
5801 begin match state.mode with
5802 | LinkNav _
5803 | View -> viewmouse button bstate x y mask
5804 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5805 | Textentry _ -> ()
5806 end;
5807 state.uioh
5809 method multiclick clicks x y mask =
5810 begin match state.mode with
5811 | LinkNav _
5812 | View -> viewmulticlick clicks x y mask
5813 | Birdseye _
5814 | Textentry _ -> ()
5815 end;
5816 state.uioh
5818 method motion x y =
5819 begin match state.mode with
5820 | Textentry _ -> ()
5821 | View | Birdseye _ | LinkNav _ ->
5822 match state.mstate with
5823 | Mzoom _ | Mnone -> ()
5825 | Mpan (x0, y0) ->
5826 let dx = x - x0
5827 and dy = y0 - y in
5828 state.mstate <- Mpan (x, y);
5829 if canpan ()
5830 then state.x <- panbound (state.x + dx);
5831 let y = clamp dy in
5832 gotoy_and_clear_text y
5834 | Msel (a, _) ->
5835 state.mstate <- Msel (a, (x, y));
5836 G.postRedisplay "motion select";
5838 | Mscrolly ->
5839 let y = min state.winh (max 0 y) in
5840 scrolly y
5842 | Mscrollx ->
5843 let x = min state.winw (max 0 x) in
5844 scrollx x
5846 | Mzoomrect (p0, _) ->
5847 state.mstate <- Mzoomrect (p0, (x, y));
5848 G.postRedisplay "motion zoomrect";
5849 end;
5850 state.uioh
5852 method pmotion x y =
5853 begin match state.mode with
5854 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5855 let rec loop = function
5856 | [] ->
5857 if hooverpageno != -1
5858 then (
5859 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5860 G.postRedisplay "pmotion birdseye no hoover";
5862 | l :: rest ->
5863 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5864 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5865 then (
5866 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5867 G.postRedisplay "pmotion birdseye hoover";
5869 else loop rest
5871 loop state.layout
5873 | Textentry _ -> ()
5875 | LinkNav _
5876 | View ->
5877 match state.mstate with
5878 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5880 | Mnone ->
5881 updateunder x y;
5882 if canselect ()
5883 then
5884 match conf.pax with
5885 | None -> ()
5886 | Some r ->
5887 let past, _, _ = !r in
5888 let now = now () in
5889 let delta = now -. past in
5890 if delta > 0.01
5891 then paxunder x y
5892 else r := (now, x, y)
5893 end;
5894 state.uioh
5896 method infochanged _ = ()
5898 method scrollph =
5899 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
5900 let p, h =
5901 if maxy = 0
5902 then 0.0, float state.winh
5903 else scrollph state.y maxy
5905 vscrollw (), p, h
5907 method scrollpw =
5908 let winw = wadjsb state.winw in
5909 let fwinw = float winw in
5910 let sw =
5911 let sw = fwinw /. float state.w in
5912 let sw = fwinw *. sw in
5913 max sw (float conf.scrollh)
5915 let position =
5916 let maxx = state.w + winw in
5917 let x = winw - state.x in
5918 let percent = float x /. float maxx in
5919 (fwinw -. sw) *. percent
5921 hscrollh (), position, sw
5923 method modehash =
5924 let modename =
5925 match state.mode with
5926 | LinkNav _ -> "links"
5927 | Textentry _ -> "textentry"
5928 | Birdseye _ -> "birdseye"
5929 | View -> "view"
5931 findkeyhash conf modename
5933 method eformsgs = true
5934 end;;
5936 let adderrmsg src msg =
5937 Buffer.add_string state.errmsgs msg;
5938 state.newerrmsgs <- true;
5939 G.postRedisplay src
5942 let adderrfmt src fmt =
5943 Format.kprintf (fun s -> adderrmsg src s) fmt;
5946 let ract cmds =
5947 let cl = splitatspace cmds in
5948 let scan s fmt f =
5949 try Scanf.sscanf s fmt f
5950 with exn ->
5951 adderrfmt "remote exec"
5952 "error processing '%S': %s\n" cmds (exntos exn)
5954 match cl with
5955 | "reload" :: [] -> reload ()
5956 | "goto" :: args :: [] ->
5957 scan args "%u %f %f"
5958 (fun pageno x y ->
5959 let cmd, _ = state.geomcmds in
5960 if emptystr cmd
5961 then gotopagexy pageno x y
5962 else
5963 let f prevf () =
5964 gotopagexy pageno x y;
5965 prevf ()
5967 state.reprf <- f state.reprf
5969 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
5970 | "gotor" :: args :: [] ->
5971 scan args "%S %u"
5972 (fun filename pageno -> gotounder (Uremote (filename, pageno)))
5973 | "gotord" :: args :: [] ->
5974 scan args "%S %S"
5975 (fun filename dest -> gotounder (Uremotedest (filename, dest)))
5976 | "rect" :: args :: [] ->
5977 scan args "%u %u %f %f %f %f"
5978 (fun pageno color x0 y0 x1 y1 ->
5979 onpagerect pageno (fun w h ->
5980 let _,w1,h1,_ = getpagedim pageno in
5981 let sw = float w1 /. float w
5982 and sh = float h1 /. float h in
5983 let x0s = x0 *. sw
5984 and x1s = x1 *. sw
5985 and y0s = y0 *. sh
5986 and y1s = y1 *. sh in
5987 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
5988 debugrect rect;
5989 state.rects <- (pageno, color, rect) :: state.rects;
5990 G.postRedisplay "rect";
5993 | "activatewin" :: [] -> Wsi.activatewin ()
5994 | "quit" :: [] -> raise Quit
5995 | _ ->
5996 adderrfmt "remote command"
5997 "error processing remote command: %S\n" cmds;
6000 let remote =
6001 let scratch = String.create 80 in
6002 let buf = Buffer.create 80 in
6003 fun fd ->
6004 let rec tempfr () =
6005 try Some (Unix.read fd scratch 0 80)
6006 with
6007 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
6008 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
6009 | exn -> raise exn
6011 match tempfr () with
6012 | None -> Some fd
6013 | Some n ->
6014 if n = 0
6015 then (
6016 Unix.close fd;
6017 if Buffer.length buf > 0
6018 then (
6019 let s = Buffer.contents buf in
6020 Buffer.clear buf;
6021 ract s;
6023 None
6025 else
6026 let rec eat ppos =
6027 let nlpos =
6029 let pos = String.index_from scratch ppos '\n' in
6030 if pos >= n then -1 else pos
6031 with Not_found -> -1
6033 if nlpos >= 0
6034 then (
6035 Buffer.add_substring buf scratch ppos (nlpos-ppos);
6036 let s = Buffer.contents buf in
6037 Buffer.clear buf;
6038 ract s;
6039 eat (nlpos+1);
6041 else (
6042 Buffer.add_substring buf scratch ppos (n-ppos);
6043 Some fd
6045 in eat 0
6048 let remoteopen path =
6049 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
6050 with exn ->
6051 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
6052 None
6055 let () =
6056 let trimcachepath = ref E.s in
6057 let rcmdpath = ref E.s in
6058 let pageno = ref None in
6059 selfexec := Sys.executable_name;
6060 Arg.parse
6061 (Arg.align
6062 [("-p", Arg.String (fun s -> state.password <- s),
6063 "<password> Set password");
6065 ("-f", Arg.String
6066 (fun s ->
6067 Config.fontpath := s;
6068 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
6070 "<path> Set path to the user interface font");
6072 ("-c", Arg.String
6073 (fun s ->
6074 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
6075 Config.confpath := s),
6076 "<path> Set path to the configuration file");
6078 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
6079 "<page-number> Jump to page");
6081 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6082 "<path> Set path to the trim cache file");
6084 ("-dest", Arg.String (fun s -> state.nameddest <- s),
6085 "<named-destination> Set named destination");
6087 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
6088 ("-cxack", Arg.Set cxack, " Cut corners");
6090 ("-remote", Arg.String (fun s -> rcmdpath := s),
6091 "<path> Set path to the remote commands source");
6093 ("-origin", Arg.String (fun s -> state.origin <- s),
6094 "<original-path> Set original path");
6096 ("-v", Arg.Unit (fun () ->
6097 Printf.printf
6098 "%s\nconfiguration path: %s\n"
6099 (version ())
6100 Config.defconfpath
6102 exit 0), " Print version and exit");
6105 (fun s -> state.path <- s)
6106 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6108 if !wtmode
6109 then selfexec := !selfexec ^ " -wtmode";
6111 let histmode = emptystr state.path in
6113 if not (Config.load ())
6114 then prerr_endline "failed to load configuration";
6115 begin match !pageno with
6116 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
6117 | None -> ()
6118 end;
6120 let wsfd, winw, winh = Wsi.init (object (self)
6121 val mutable m_hack = false
6122 val mutable m_clicks = 0
6123 val mutable m_click_x = 0
6124 val mutable m_click_y = 0
6125 val mutable m_lastclicktime = infinity
6127 method private cleanup =
6128 state.roam <- noroam;
6129 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap;
6130 method expose = if not m_hack then G.postRedisplay "expose"
6131 method visible = G.postRedisplay "visible"
6132 method display = m_hack <- false; display ()
6133 method reshape w h =
6134 self#cleanup;
6135 m_hack <- w < state.winw && h < state.winh;
6136 reshape w h
6137 method mouse b d x y m =
6138 if d && canselect ()
6139 then (
6140 (* http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx *)
6141 m_click_x <- x;
6142 m_click_y <- y;
6143 if b = 1
6144 then (
6145 let t = now () in
6146 if abs x - m_click_x > 10
6147 || abs y - m_click_y > 10
6148 || abs_float (t -. m_lastclicktime) > 0.3
6149 then m_clicks <- 0;
6150 m_clicks <- m_clicks + 1;
6151 m_lastclicktime <- t;
6152 if m_clicks = 1
6153 then (
6154 self#cleanup;
6155 G.postRedisplay "cleanup";
6156 state.uioh <- state.uioh#button b d x y m;
6158 else state.uioh <- state.uioh#multiclick m_clicks x y m
6160 else (
6161 self#cleanup;
6162 m_clicks <- 0;
6163 m_lastclicktime <- infinity;
6164 state.uioh <- state.uioh#button b d x y m
6167 else (
6168 state.uioh <- state.uioh#button b d x y m
6170 method motion x y =
6171 state.mpos <- (x, y);
6172 state.uioh <- state.uioh#motion x y
6173 method pmotion x y =
6174 state.mpos <- (x, y);
6175 state.uioh <- state.uioh#pmotion x y
6176 method key k m =
6177 let mascm = m land (
6178 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6179 ) in
6180 let keyboard k m =
6181 let x = state.x and y = state.y in
6182 keyboard k m;
6183 if x != state.x || y != state.y then self#cleanup
6185 match state.keystate with
6186 | KSnone ->
6187 let km = k, mascm in
6188 begin
6189 match
6190 let modehash = state.uioh#modehash in
6191 try Hashtbl.find modehash km
6192 with Not_found ->
6193 try Hashtbl.find (findkeyhash conf "global") km
6194 with Not_found -> KMinsrt (k, m)
6195 with
6196 | KMinsrt (k, m) -> keyboard k m
6197 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6198 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6200 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6201 List.iter (fun (k, m) -> keyboard k m) insrt;
6202 state.keystate <- KSnone
6203 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6204 state.keystate <- KSinto (keys, insrt)
6205 | _ ->
6206 state.keystate <- KSnone
6208 method enter x y =
6209 state.mpos <- (x, y);
6210 state.uioh <- state.uioh#pmotion x y
6211 method leave = state.mpos <- (-1, -1)
6212 method winstate wsl = state.winstate <- wsl; m_hack <- false
6213 method quit = raise Quit
6214 end) conf.cwinw conf.cwinh (platform = Posx) in
6216 state.wsfd <- wsfd;
6218 if not (
6219 List.exists GlMisc.check_extension
6220 [ "GL_ARB_texture_rectangle"
6221 ; "GL_EXT_texture_recangle"
6222 ; "GL_NV_texture_rectangle" ]
6224 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6226 if (
6227 let r = GlMisc.get_string `renderer in
6228 let p = "Mesa DRI Intel(" in
6229 let l = String.length p in
6230 String.length r > l && String.sub r 0 l = p
6232 then (
6233 defconf.sliceheight <- 1024;
6234 defconf.texcount <- 32;
6235 defconf.usepbo <- true;
6238 let cr, sw =
6239 match Ne.res Unix.pipe with
6240 | Ne.Exn exn ->
6241 Printf.eprintf "pipe/crsw failed: %s" (exntos exn);
6242 exit 1
6243 | Ne.Res rw -> rw
6244 and sr, cw =
6245 match Ne.res Unix.pipe with
6246 | Ne.Exn exn ->
6247 Printf.eprintf "pipe/srcw failed: %s" (exntos exn);
6248 exit 1
6249 | Ne.Res rw -> rw
6252 cloexec cr;
6253 cloexec sw;
6254 cloexec sr;
6255 cloexec cw;
6257 setcheckers conf.checkers;
6258 redirectstderr ();
6259 if conf.redirectstderr
6260 then
6261 at_exit (fun () ->
6262 let s = Buffer.contents state.errmsgs ^
6263 (match state.errfd with
6264 | Some fd ->
6265 let s = String.create (80*24) in
6266 let n =
6268 let r, _, _ = Unix.select [fd] [] [] 0.0 in
6269 if List.mem fd r
6270 then Unix.read fd s 0 (String.length s)
6271 else 0
6272 with _ -> 0
6274 if n = 0
6275 then E.s
6276 else String.sub s 0 n
6277 | None -> E.s
6280 try ignore (Unix.write state.stderr s 0 (String.length s))
6281 with exn -> print_endline (exntos exn)
6285 init (cr, cw) (
6286 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
6287 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6288 !Config.fontpath, !trimcachepath,
6289 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
6291 List.iter GlArray.enable [`texture_coord; `vertex];
6292 state.sr <- sr;
6293 state.sw <- sw;
6294 reshape winw winh;
6295 if histmode
6296 then (
6297 state.uioh <- uioh;
6298 enterhistmode ();
6300 else (
6301 state.text <- "Opening " ^ (mbtoutf8 state.path);
6302 opendoc state.path state.password;
6303 state.uioh <- uioh;
6305 display ();
6306 Wsi.mapwin ();
6307 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
6308 let optrfd =
6309 ref (
6310 if nonemptystr !rcmdpath
6311 then remoteopen !rcmdpath
6312 else None
6316 let rec loop deadline =
6317 let r =
6318 match state.errfd with
6319 | None -> [state.sr; state.wsfd]
6320 | Some fd -> [state.sr; state.wsfd; fd]
6322 let r =
6323 match !optrfd with
6324 | None -> r
6325 | Some fd -> fd :: r
6327 if state.redisplay
6328 then (
6329 state.redisplay <- false;
6330 display ();
6332 let timeout =
6333 let now = now () in
6334 if deadline > now
6335 then (
6336 if deadline = infinity
6337 then ~-.1.0
6338 else max 0.0 (deadline -. now)
6340 else 0.0
6342 let r, _, _ =
6343 try Unix.select r [] [] timeout
6344 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6346 begin match r with
6347 | [] ->
6348 state.ghyll None;
6349 let newdeadline =
6350 if state.ghyll == noghyll
6351 then
6352 match state.autoscroll with
6353 | Some step when step != 0 ->
6354 let y = state.y + step in
6355 let y =
6356 if y < 0
6357 then state.maxy
6358 else if y >= state.maxy then 0 else y
6360 gotoy y;
6361 if state.mode = View
6362 then state.text <- E.s;
6363 deadline +. 0.01
6364 | _ -> infinity
6365 else deadline +. 0.01
6367 loop newdeadline
6369 | l ->
6370 let rec checkfds = function
6371 | [] -> ()
6372 | fd :: rest when fd = state.sr ->
6373 let cmd = readcmd state.sr in
6374 act cmd;
6375 checkfds rest
6377 | fd :: rest when fd = state.wsfd ->
6378 Wsi.readresp fd;
6379 checkfds rest
6381 | fd :: rest when Some fd = !optrfd ->
6382 begin match remote fd with
6383 | None -> optrfd := remoteopen !rcmdpath;
6384 | opt -> optrfd := opt
6385 end;
6386 checkfds rest
6388 | fd :: rest ->
6389 let s = String.create 80 in
6390 let n = tempfailureretry (Unix.read fd s 0) 80 in
6391 if conf.redirectstderr
6392 then (
6393 Buffer.add_substring state.errmsgs s 0 n;
6394 state.newerrmsgs <- true;
6395 state.redisplay <- true;
6397 else (
6398 prerr_string (String.sub s 0 n);
6399 flush stderr;
6401 checkfds rest
6403 checkfds l;
6404 if !reeenterhist then (
6405 enterhistmode ();
6406 reeenterhist := false;
6408 let newdeadline =
6409 let deadline1 =
6410 if deadline = infinity
6411 then now () +. 0.01
6412 else deadline
6414 match state.autoscroll with
6415 | Some step when step != 0 -> deadline1
6416 | _ -> if state.ghyll == noghyll then infinity else deadline1
6418 loop newdeadline
6419 end;
6422 loop infinity;
6423 with Quit ->
6424 Config.save leavebirdseye;