Remove superfluous parentheses
[llpp.git] / main.ml
blob479897a5d646bd661109a2a8028b97edeb5e3b57
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_alpha `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 0 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 0 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 `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 0 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 0 0 w 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.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2202 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2203 let pageno = min (state.pagecount - 1) (pageno + incr) in
2204 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2205 let rec loop = function
2206 | [] ->
2207 let y, h = getpageyh pageno in
2208 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2209 gotoy (clamp dy)
2210 | l :: _ when l.pageno = pageno ->
2211 if l.pagevh != l.pageh
2212 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2213 else G.postRedisplay "downbirdseye"
2214 | _ :: rest -> loop rest
2216 loop state.layout
2219 let boundastep h step =
2220 if step < 0
2221 then bound step ~-h 0
2222 else bound step 0 h
2225 let optentry mode _ key =
2226 let btos b = if b then "on" else "off" in
2227 if key >= 32 && key < 127
2228 then
2229 let c = Char.chr key in
2230 match c with
2231 | 's' ->
2232 let ondone s =
2233 try conf.scrollstep <- int_of_string s with exc ->
2234 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2236 TEswitch ("scroll step: ", E.s, None, intentry, ondone, true)
2238 | 'A' ->
2239 let ondone s =
2241 conf.autoscrollstep <- boundastep state.winh (int_of_string s);
2242 if state.autoscroll <> None
2243 then state.autoscroll <- Some conf.autoscrollstep
2244 with exc ->
2245 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2247 TEswitch ("auto scroll step: ", E.s, None, intentry, ondone, true)
2249 | 'C' ->
2250 let ondone s =
2252 let n, a, b = multicolumns_of_string s in
2253 setcolumns mode n a b;
2254 with exc ->
2255 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
2257 TEswitch ("columns: ", E.s, None, textentry, ondone, true)
2259 | 'Z' ->
2260 let ondone s =
2262 let zoom = float (int_of_string s) /. 100.0 in
2263 setzoom zoom
2264 with exc ->
2265 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2267 TEswitch ("zoom: ", E.s, None, intentry, ondone, true)
2269 | 't' ->
2270 let ondone s =
2272 conf.thumbw <- bound (int_of_string s) 2 4096;
2273 state.text <-
2274 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2275 begin match mode with
2276 | Birdseye beye ->
2277 leavebirdseye beye false;
2278 enterbirdseye ();
2279 | _ -> ();
2281 with exc ->
2282 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2284 TEswitch ("thumbnail width: ", E.s, None, intentry, ondone, true)
2286 | 'R' ->
2287 let ondone s =
2288 match try
2289 Some (int_of_string s)
2290 with exc ->
2291 state.text <- Printf.sprintf "bad integer `%s': %s"
2292 s (exntos exc);
2293 None
2294 with
2295 | Some angle -> reqlayout angle conf.fitmodel
2296 | None -> ()
2298 TEswitch ("rotation: ", E.s, None, intentry, ondone, true)
2300 | 'i' ->
2301 conf.icase <- not conf.icase;
2302 TEdone ("case insensitive search " ^ (btos conf.icase))
2304 | 'p' ->
2305 conf.preload <- not conf.preload;
2306 gotoy state.y;
2307 TEdone ("preload " ^ (btos conf.preload))
2309 | 'v' ->
2310 conf.verbose <- not conf.verbose;
2311 TEdone ("verbose " ^ (btos conf.verbose))
2313 | 'd' ->
2314 conf.debug <- not conf.debug;
2315 TEdone ("debug " ^ (btos conf.debug))
2317 | 'h' ->
2318 conf.maxhfit <- not conf.maxhfit;
2319 state.maxy <- calcheight ();
2320 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2322 | 'c' ->
2323 conf.crophack <- not conf.crophack;
2324 TEdone ("crophack " ^ btos conf.crophack)
2326 | 'a' ->
2327 let s =
2328 match conf.maxwait with
2329 | None ->
2330 conf.maxwait <- Some infinity;
2331 "always wait for page to complete"
2332 | Some _ ->
2333 conf.maxwait <- None;
2334 "show placeholder if page is not ready"
2336 TEdone s
2338 | 'f' ->
2339 conf.underinfo <- not conf.underinfo;
2340 TEdone ("underinfo " ^ btos conf.underinfo)
2342 | 'P' ->
2343 conf.savebmarks <- not conf.savebmarks;
2344 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2346 | 'S' ->
2347 let ondone s =
2349 let pageno, py =
2350 match state.layout with
2351 | [] -> 0, 0
2352 | l :: _ ->
2353 l.pageno, l.pagey
2355 conf.interpagespace <- int_of_string s;
2356 docolumns conf.columns;
2357 state.maxy <- calcheight ();
2358 let y = getpagey pageno in
2359 gotoy (y + py)
2360 with exc ->
2361 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2363 TEswitch ("vertical margin: ", E.s, None, intentry, ondone, true)
2365 | 'l' ->
2366 let fm =
2367 match conf.fitmodel with
2368 | FitProportional -> FitWidth
2369 | _ -> FitProportional
2371 reqlayout conf.angle fm;
2372 TEdone ("proportional display " ^ btos (fm == FitProportional))
2374 | 'T' ->
2375 settrim (not conf.trimmargins) conf.trimfuzz;
2376 TEdone ("trim margins " ^ btos conf.trimmargins)
2378 | 'I' ->
2379 conf.invert <- not conf.invert;
2380 TEdone ("invert colors " ^ btos conf.invert)
2382 | 'x' ->
2383 let ondone s =
2384 cbput state.hists.sel s;
2385 conf.selcmd <- s;
2387 TEswitch ("selection command: ", E.s, Some (onhist state.hists.sel),
2388 textentry, ondone, true)
2390 | 'M' ->
2391 if conf.pax == None
2392 then conf.pax <- Some (ref (0.0, 0, 0))
2393 else conf.pax <- None;
2394 TEdone ("PAX " ^ btos (conf.pax != None))
2396 | _ ->
2397 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2398 TEstop
2399 else
2400 TEcont state.text
2403 class type lvsource = object
2404 method getitemcount : int
2405 method getitem : int -> (string * int)
2406 method hasaction : int -> bool
2407 method exit :
2408 uioh:uioh ->
2409 cancel:bool ->
2410 active:int ->
2411 first:int ->
2412 pan:int ->
2413 uioh option
2414 method getactive : int
2415 method getfirst : int
2416 method getpan : int
2417 method getminfo : (int * int) array
2418 end;;
2420 class virtual lvsourcebase = object
2421 val mutable m_active = 0
2422 val mutable m_first = 0
2423 val mutable m_pan = 0
2424 method getactive = m_active
2425 method getfirst = m_first
2426 method getpan = m_pan
2427 method getminfo : (int * int) array = E.a
2428 end;;
2430 let withoutlastutf8 s =
2431 let len = String.length s in
2432 if len = 0
2433 then s
2434 else
2435 let rec find pos =
2436 if pos = 0
2437 then pos
2438 else
2439 let b = Char.code s.[pos] in
2440 if b land 0b11000000 = 0b11000000
2441 then pos
2442 else find (pos-1)
2444 let first =
2445 if Char.code s.[len-1] land 0x80 = 0
2446 then len-1
2447 else find (len-1)
2449 String.sub s 0 first;
2452 let textentrykeyboard
2453 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2454 let key =
2455 if key >= 0xffb0 && key <= 0xffb9
2456 then key - 0xffb0 + 48 else key
2458 let enttext te =
2459 state.mode <- Textentry (te, onleave);
2460 state.text <- E.s;
2461 enttext ();
2462 G.postRedisplay "textentrykeyboard enttext";
2464 let histaction cmd =
2465 match opthist with
2466 | None -> ()
2467 | Some (action, _) ->
2468 state.mode <- Textentry (
2469 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
2471 G.postRedisplay "textentry histaction"
2473 match key with
2474 | 0xff08 -> (* backspace *)
2475 if emptystr text && cancelonempty
2476 then (
2477 onleave Cancel;
2478 G.postRedisplay "textentrykeyboard after cancel";
2480 else
2481 let s = withoutlastutf8 text in
2482 enttext (c, s, opthist, onkey, ondone, cancelonempty)
2484 | 0xff0d | 0xff8d -> (* (kp) enter *)
2485 ondone text;
2486 onleave Confirm;
2487 G.postRedisplay "textentrykeyboard after confirm"
2489 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
2490 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
2491 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
2492 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
2494 | 0xff1b -> (* escape*)
2495 if emptystr text
2496 then (
2497 begin match opthist with
2498 | None -> ()
2499 | Some (_, onhistcancel) -> onhistcancel ()
2500 end;
2501 onleave Cancel;
2502 state.text <- E.s;
2503 G.postRedisplay "textentrykeyboard after cancel2"
2505 else (
2506 enttext (c, E.s, opthist, onkey, ondone, cancelonempty)
2509 | 0xff9f | 0xffff -> () (* delete *)
2511 | _ when key != 0
2512 && key land 0xff00 != 0xff00 (* keyboard *)
2513 && key land 0xfe00 != 0xfe00 (* xkb *)
2514 && key land 0xfd00 != 0xfd00 (* 3270 *)
2516 begin match onkey text key with
2517 | TEdone text ->
2518 ondone text;
2519 onleave Confirm;
2520 G.postRedisplay "textentrykeyboard after confirm2";
2522 | TEcont text ->
2523 enttext (c, text, opthist, onkey, ondone, cancelonempty);
2525 | TEstop ->
2526 onleave Cancel;
2527 G.postRedisplay "textentrykeyboard after cancel3"
2529 | TEswitch te ->
2530 state.mode <- Textentry (te, onleave);
2531 G.postRedisplay "textentrykeyboard switch";
2532 end;
2534 | _ ->
2535 vlog "unhandled key %s" (Wsi.keyname key)
2538 let firstof first active =
2539 if first > active || abs (first - active) > fstate.maxrows - 1
2540 then max 0 (active - (fstate.maxrows/2))
2541 else first
2544 let calcfirst first active =
2545 if active > first
2546 then
2547 let rows = active - first in
2548 if rows > fstate.maxrows then active - fstate.maxrows else first
2549 else active
2552 let scrollph y maxy =
2553 let sh = float (maxy + state.winh) /. float state.winh in
2554 let sh = float state.winh /. sh in
2555 let sh = max sh (float conf.scrollh) in
2557 let percent = float y /. float maxy in
2558 let position = (float state.winh -. sh) *. percent in
2560 let position =
2561 if position +. sh > float state.winh
2562 then float state.winh -. sh
2563 else position
2565 position, sh;
2568 let coe s = (s :> uioh);;
2570 class listview ~zebra ~helpmode ~(source:lvsource) ~trusted ~modehash =
2571 object (self)
2572 val m_pan = source#getpan
2573 val m_first = source#getfirst
2574 val m_active = source#getactive
2575 val m_qsearch = E.s
2576 val m_prev_uioh = state.uioh
2578 method private elemunder y =
2579 if y < 0
2580 then None
2581 else
2582 let n = y / (fstate.fontsize+1) in
2583 if m_first + n < source#getitemcount
2584 then (
2585 if source#hasaction (m_first + n)
2586 then Some (m_first + n)
2587 else None
2589 else None
2591 method display =
2592 Gl.enable `blend;
2593 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2594 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2595 filledrect 0. 0. (float state.winw) (float state.winh);
2596 GlDraw.color (1., 1., 1.);
2597 Gl.enable `texture_2d;
2598 let fs = fstate.fontsize in
2599 let nfs = fs + 1 in
2600 let hw = (wadjsb (xadjsb state.winw))/3 in
2601 let ww = fstate.wwidth in
2602 let tabw = 17.0*.ww in
2603 let itemcount = source#getitemcount in
2604 let minfo = source#getminfo in
2605 let x0, x1 =
2606 if conf.leftscroll
2607 then float (xadjsb 0), float (state.winw - 1)
2608 else 0.0, float (state.winw - conf.scrollbw - 1)
2610 let rec loop row =
2611 if (row - m_first) > fstate.maxrows
2612 then ()
2613 else (
2614 if row >= 0 && row < itemcount
2615 then (
2616 let (s, level) = source#getitem row in
2617 let y = (row - m_first) * nfs in
2618 let x =
2619 (if conf.leftscroll then float (xadjsb 0) else 5.0)
2620 +. (float (level + m_pan)) *. ww in
2621 if helpmode
2622 then GlDraw.color
2623 (let c = if row land 1 = 0 then 1.0 else 0.92 in (c,c,c));
2625 if row = m_active
2626 then (
2627 Gl.disable `texture_2d;
2628 let alpha = if source#hasaction row then 0.9 else 0.3 in
2629 GlDraw.color (1., 1., 1.) ~alpha;
2630 linerect (x0 +. 1.) (float (y + 1)) (x1) (float (y + fs + 3));
2631 Gl.enable `texture_2d;
2633 let c =
2634 if zebra && row land 1 = 1
2635 then 0.8
2636 else 1.0
2638 GlDraw.color (c,c,c);
2639 let drawtabularstring s =
2640 let drawstr x s =
2641 let x' = truncate (x0 +. x) in
2642 let pos = nindex s '\000' in
2643 if pos = -1
2644 then drawstring1 fs x' (y+nfs) s
2645 else
2646 let s1 = String.sub s 0 pos
2647 and s2 = String.sub s (pos+1) (String.length s - pos - 1) in
2648 let rec e s =
2649 if emptystr s
2650 then s
2651 else
2652 let s' = withoutlastutf8 s in
2653 let s = s' ^ "\xe2\x80\xa6" in
2654 let w = measurestr fs s in
2655 if float x' +. w +. ww < float (hw + x')
2656 then s
2657 else e s'
2659 let s1 =
2660 if float x' +. ww +. measurestr fs s1 > float (hw + x')
2661 then e s1
2662 else s1
2664 ignore (drawstring1 fs x' (y+nfs) s1);
2665 drawstring1 fs (hw + x') (y+nfs) s2
2667 if trusted
2668 then
2669 let x = if helpmode && row > 0 then x +. ww else x in
2670 let tabpos = nindex s '\t' in
2671 if tabpos > 0
2672 then
2673 let len = String.length s - tabpos - 1 in
2674 let s1 = String.sub s 0 tabpos
2675 and s2 = String.sub s (tabpos + 1) len in
2676 let nx = drawstr x s1 in
2677 let sw = nx -. x in
2678 let x = x +. (max tabw sw) in
2679 drawstr x s2
2680 else
2681 let len = String.length s - 2 in
2682 if len > 0 && s.[0] = '\xc2' && s.[1] = '\xb7'
2683 then
2684 let s = String.sub s 2 len in
2685 let x = if not helpmode then x +. ww else x in
2686 GlDraw.color (1.2, 1.2, 1.2);
2687 let vinc = drawstring1 (fs+fs/4)
2688 (truncate (x -. ww)) (y+nfs) s in
2689 GlDraw.color (1., 1., 1.);
2690 vinc +. (float fs *. 0.8)
2691 else
2692 drawstr x s
2693 else
2694 drawstr x s
2696 ignore (drawtabularstring s);
2697 loop (row+1)
2701 loop m_first;
2702 GlDraw.color (1.0, 1.0, 1.0) ~alpha:0.5;
2703 let rec loop row =
2704 if (row - m_first) > fstate.maxrows
2705 then ()
2706 else (
2707 if row >= 0 && row < itemcount
2708 then (
2709 let (s, level) = source#getitem row in
2710 let pos0 = nindex s '\000' in
2711 let y = (row - m_first) * nfs in
2712 let x = float (level + m_pan) *. ww in
2713 let (first, last) = minfo.(row) in
2714 let prefix =
2715 if pos0 > 0 && first > pos0
2716 then String.sub s (pos0+1) (first-pos0-1)
2717 else String.sub s 0 first
2719 let suffix = String.sub s first (last - first) in
2720 let w1 = measurestr fstate.fontsize prefix in
2721 let w2 = measurestr fstate.fontsize suffix in
2722 let x = x +. if conf.leftscroll then float (xadjsb 5) else 5.0 in
2723 let x = if pos0 > 0 && first > pos0 then x +. float hw else x in
2724 let x0 = x +. w1
2725 and y0 = float (y+2) in
2726 let x1 = x0 +. w2
2727 and y1 = float (y+fs+3) in
2728 filledrect x0 y0 x1 y1;
2729 loop (row+1)
2733 Gl.disable `texture_2d;
2734 if Array.length minfo > 0 then loop m_first;
2735 Gl.disable `blend;
2737 method updownlevel incr =
2738 let len = source#getitemcount in
2739 let curlevel =
2740 if m_active >= 0 && m_active < len
2741 then snd (source#getitem m_active)
2742 else -1
2744 let rec flow i =
2745 if i = len then i-1 else if i = -1 then 0 else
2746 let _, l = source#getitem i in
2747 if l != curlevel then i else flow (i+incr)
2749 let active = flow m_active in
2750 let first = calcfirst m_first active in
2751 G.postRedisplay "outline updownlevel";
2752 {< m_active = active; m_first = first >}
2754 method private key1 key mask =
2755 let set1 active first qsearch =
2756 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2758 let search active pattern incr =
2759 let active = if active = -1 then m_first else active in
2760 let dosearch re =
2761 let rec loop n =
2762 if n >= 0 && n < source#getitemcount
2763 then (
2764 let s, _ = source#getitem n in
2766 (try ignore (Str.search_forward re s 0); true
2767 with Not_found -> false)
2768 then Some n
2769 else loop (n + incr)
2771 else None
2773 loop active
2776 let re = Str.regexp_case_fold pattern in
2777 dosearch re
2778 with Failure s ->
2779 state.text <- s;
2780 None
2782 let itemcount = source#getitemcount in
2783 let find start incr =
2784 let rec find i =
2785 if i = -1 || i = itemcount
2786 then -1
2787 else (
2788 if source#hasaction i
2789 then i
2790 else find (i + incr)
2793 find start
2795 let set active first =
2796 let first = bound first 0 (itemcount - fstate.maxrows) in
2797 state.text <- E.s;
2798 coe {< m_active = active; m_first = first; m_qsearch = E.s >}
2800 let navigate incr =
2801 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2802 let active, first =
2803 let incr1 = if incr > 0 then 1 else -1 in
2804 if isvisible m_first m_active
2805 then
2806 let next =
2807 let next = m_active + incr in
2808 let next =
2809 if next < 0 || next >= itemcount
2810 then -1
2811 else find next incr1
2813 if abs (m_active - next) > fstate.maxrows
2814 then -1
2815 else next
2817 if next = -1
2818 then
2819 let first = m_first + incr in
2820 let first = bound first 0 (itemcount - fstate.maxrows) in
2821 let next =
2822 let next = m_active + incr in
2823 let next = bound next 0 (itemcount - 1) in
2824 find next ~-incr1
2826 let active =
2827 if next = -1
2828 then m_active
2829 else (
2830 if isvisible first next
2831 then next
2832 else m_active
2835 active, first
2836 else
2837 let first = min next m_first in
2838 let first =
2839 if abs (next - first) > fstate.maxrows
2840 then first + incr
2841 else first
2843 next, first
2844 else
2845 let first = m_first + incr in
2846 let first = bound first 0 (itemcount - 1) in
2847 let active =
2848 let next = m_active + incr in
2849 let next = bound next 0 (itemcount - 1) in
2850 let next = find next incr1 in
2851 let active =
2852 if next = -1 || abs (m_active - first) > fstate.maxrows
2853 then (
2854 let active = if m_active = -1 then next else m_active in
2855 active
2857 else next
2859 if isvisible first active
2860 then active
2861 else -1
2863 active, first
2865 G.postRedisplay "listview navigate";
2866 set active first;
2868 match key with
2869 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
2870 let incr = if key = 0x72 then -1 else 1 in
2871 let active, first =
2872 match search (m_active + incr) m_qsearch incr with
2873 | None ->
2874 state.text <- m_qsearch ^ " [not found]";
2875 m_active, m_first
2876 | Some active ->
2877 state.text <- m_qsearch;
2878 active, firstof m_first active
2880 G.postRedisplay "listview ctrl-r/s";
2881 set1 active first m_qsearch;
2883 | 0xff63 when Wsi.withctrl mask -> (* ctrl-insert *)
2884 if m_active >= 0 && m_active < source#getitemcount
2885 then (
2886 let s, _ = source#getitem m_active in
2887 selstring s;
2889 coe self
2891 | 0xff08 -> (* backspace *)
2892 if emptystr m_qsearch
2893 then coe self
2894 else (
2895 let qsearch = withoutlastutf8 m_qsearch in
2896 if emptystr qsearch
2897 then (
2898 state.text <- E.s;
2899 G.postRedisplay "listview empty qsearch";
2900 set1 m_active m_first E.s;
2902 else
2903 let active, first =
2904 match search m_active qsearch ~-1 with
2905 | None ->
2906 state.text <- qsearch ^ " [not found]";
2907 m_active, m_first
2908 | Some active ->
2909 state.text <- qsearch;
2910 active, firstof m_first active
2912 G.postRedisplay "listview backspace qsearch";
2913 set1 active first qsearch
2916 | key when (key != 0 && key land 0xff00 != 0xff00) ->
2917 let pattern = m_qsearch ^ toutf8 key in
2918 let active, first =
2919 match search m_active pattern 1 with
2920 | None ->
2921 state.text <- pattern ^ " [not found]";
2922 m_active, m_first
2923 | Some active ->
2924 state.text <- pattern;
2925 active, firstof m_first active
2927 G.postRedisplay "listview qsearch add";
2928 set1 active first pattern;
2930 | 0xff1b -> (* escape *)
2931 state.text <- E.s;
2932 if emptystr m_qsearch
2933 then (
2934 G.postRedisplay "list view escape";
2935 begin
2936 match
2937 source#exit (coe self) true m_active m_first m_pan
2938 with
2939 | None -> m_prev_uioh
2940 | Some uioh -> uioh
2943 else (
2944 G.postRedisplay "list view kill qsearch";
2945 coe {< m_qsearch = E.s >}
2948 | 0xff0d | 0xff8d -> (* (kp) enter *)
2949 state.text <- E.s;
2950 let self = {< m_qsearch = E.s >} in
2951 let opt =
2952 G.postRedisplay "listview enter";
2953 if m_active >= 0 && m_active < source#getitemcount
2954 then (
2955 source#exit (coe self) false m_active m_first m_pan;
2957 else (
2958 source#exit (coe self) true m_active m_first m_pan;
2961 begin match opt with
2962 | None -> m_prev_uioh
2963 | Some uioh -> uioh
2966 | 0xff9f | 0xffff -> (* (kp) delete *)
2967 coe self
2969 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
2970 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
2971 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
2972 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
2974 | 0xff53 | 0xff98 -> (* (kp) right *)
2975 state.text <- E.s;
2976 G.postRedisplay "listview right";
2977 coe {< m_pan = m_pan - 1 >}
2979 | 0xff51 | 0xff96 -> (* (kp) left *)
2980 state.text <- E.s;
2981 G.postRedisplay "listview left";
2982 coe {< m_pan = m_pan + 1 >}
2984 | 0xff50 | 0xff95 -> (* (kp) home *)
2985 let active = find 0 1 in
2986 G.postRedisplay "listview home";
2987 set active 0;
2989 | 0xff57 | 0xff9c -> (* (kp) end *)
2990 let first = max 0 (itemcount - fstate.maxrows) in
2991 let active = find (itemcount - 1) ~-1 in
2992 G.postRedisplay "listview end";
2993 set active first;
2995 | key when (key = 0 || key land 0xff00 = 0xff00) ->
2996 coe self
2998 | _ ->
2999 dolog "listview unknown key %#x" key; coe self
3001 method key key mask =
3002 match state.mode with
3003 | Textentry te -> textentrykeyboard key mask te; coe self
3004 | _ -> self#key1 key mask
3006 method button button down x y _ =
3007 let opt =
3008 match button with
3009 | 1 when x > state.winw - conf.scrollbw ->
3010 G.postRedisplay "listview scroll";
3011 if down
3012 then
3013 let _, position, sh = self#scrollph in
3014 if y > truncate position && y < truncate (position +. sh)
3015 then (
3016 state.mstate <- Mscrolly;
3017 Some (coe self)
3019 else
3020 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3021 let first = truncate (s *. float source#getitemcount) in
3022 let first = min source#getitemcount first in
3023 Some (coe {< m_first = first; m_active = first >})
3024 else (
3025 state.mstate <- Mnone;
3026 Some (coe self);
3028 | 1 when not down ->
3029 begin match self#elemunder y with
3030 | Some n ->
3031 G.postRedisplay "listview click";
3032 source#exit (coe {< m_active = n >}) false n m_first m_pan
3033 | _ ->
3034 Some (coe self)
3036 | n when (n == 4 || n == 5) && not down ->
3037 let len = source#getitemcount in
3038 let first =
3039 if n = 5 && m_first + fstate.maxrows >= len
3040 then
3041 m_first
3042 else
3043 let first = m_first + (if n == 4 then -1 else 1) in
3044 bound first 0 (len - 1)
3046 G.postRedisplay "listview wheel";
3047 Some (coe {< m_first = first >})
3048 | n when (n = 6 || n = 7) && not down ->
3049 let inc = if n = 7 then -1 else 1 in
3050 G.postRedisplay "listview hwheel";
3051 Some (coe {< m_pan = m_pan + inc >})
3052 | _ ->
3053 Some (coe self)
3055 match opt with
3056 | None -> m_prev_uioh
3057 | Some uioh -> uioh
3059 method multiclick _ x y = self#button 1 true x y
3061 method motion _ y =
3062 match state.mstate with
3063 | Mscrolly ->
3064 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3065 let first = truncate (s *. float source#getitemcount) in
3066 let first = min source#getitemcount first in
3067 G.postRedisplay "listview motion";
3068 coe {< m_first = first; m_active = first >}
3069 | _ -> coe self
3071 method pmotion x y =
3072 if x < state.winw - conf.scrollbw
3073 then
3074 let n =
3075 match self#elemunder y with
3076 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3077 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3079 let o =
3080 if n != m_active
3081 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3082 else self
3084 coe o
3085 else (
3086 Wsi.setcursor Wsi.CURSOR_INHERIT;
3087 coe self
3090 method infochanged _ = ()
3092 method scrollpw = (0, 0.0, 0.0)
3093 method scrollph =
3094 let nfs = fstate.fontsize + 1 in
3095 let y = m_first * nfs in
3096 let itemcount = source#getitemcount in
3097 let maxi = max 0 (itemcount - fstate.maxrows) in
3098 let maxy = maxi * nfs in
3099 let p, h = scrollph y maxy in
3100 conf.scrollbw, p, h
3102 method modehash = modehash
3103 method eformsgs = false
3104 end;;
3106 class outlinelistview ~zebra ~source =
3107 let settext autonarrow s =
3108 if autonarrow
3109 then
3110 let ss = source#statestr in
3111 state.text <-
3112 if emptystr ss
3113 then "[" ^ s ^ "]"
3114 else "{" ^ ss ^ "} [" ^ s ^ "]"
3115 else state.text <- s
3117 object (self)
3118 inherit listview
3119 ~zebra
3120 ~helpmode:false
3121 ~source:(source :> lvsource)
3122 ~trusted:false
3123 ~modehash:(findkeyhash conf "outline")
3124 as super
3126 val m_autonarrow = false
3128 method key key mask =
3129 let maxrows =
3130 if emptystr state.text
3131 then fstate.maxrows
3132 else fstate.maxrows - 2
3134 let calcfirst first active =
3135 if active > first
3136 then
3137 let rows = active - first in
3138 if rows > maxrows then active - maxrows else first
3139 else active
3141 let navigate incr =
3142 let active = m_active + incr in
3143 let active = bound active 0 (source#getitemcount - 1) in
3144 let first = calcfirst m_first active in
3145 G.postRedisplay "outline navigate";
3146 coe {< m_active = active; m_first = first >}
3148 let navscroll first =
3149 let active =
3150 let dist = m_active - first in
3151 if dist < 0
3152 then first
3153 else (
3154 if dist < maxrows
3155 then m_active
3156 else first + maxrows
3159 G.postRedisplay "outline navscroll";
3160 coe {< m_first = first; m_active = active >}
3162 let ctrl = Wsi.withctrl mask in
3163 match key with
3164 | 97 when ctrl -> (* ctrl-a *)
3165 let text =
3166 if m_autonarrow
3167 then (source#denarrow; E.s)
3168 else (
3169 let pattern = source#renarrow in
3170 if nonemptystr m_qsearch
3171 then (source#narrow m_qsearch; m_qsearch)
3172 else pattern
3175 settext (not m_autonarrow) text;
3176 G.postRedisplay "toggle auto narrowing";
3177 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
3179 | 47 when emptystr m_qsearch && not m_autonarrow -> (* / *)
3180 settext true E.s;
3181 G.postRedisplay "toggle auto narrowing";
3182 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
3184 | 110 when ctrl -> (* ctrl-n *)
3185 source#narrow m_qsearch;
3186 if not m_autonarrow
3187 then source#add_narrow_pattern m_qsearch;
3188 G.postRedisplay "outline ctrl-n";
3189 coe {< m_first = 0; m_active = 0 >}
3191 | 83 when ctrl -> (* ctrl-S *)
3192 let active = source#calcactive (getanchor ()) in
3193 let first = firstof m_first active in
3194 G.postRedisplay "outline ctrl-s";
3195 coe {< m_first = first; m_active = active >}
3197 | 117 when ctrl -> (* ctrl-u *)
3198 G.postRedisplay "outline ctrl-u";
3199 if m_autonarrow && nonemptystr m_qsearch
3200 then (
3201 ignore (source#renarrow);
3202 settext m_autonarrow E.s;
3203 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3205 else (
3206 source#del_narrow_pattern;
3207 let pattern = source#renarrow in
3208 let text =
3209 if emptystr pattern then E.s else "Narrowed to " ^ pattern
3211 settext m_autonarrow text;
3212 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3215 | 108 when ctrl -> (* ctrl-l *)
3216 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3217 G.postRedisplay "outline ctrl-l";
3218 coe {< m_first = first >}
3220 | 0xff09 when m_autonarrow -> (* tab *)
3221 if nonemptystr m_qsearch
3222 then (
3223 G.postRedisplay "outline list view tab";
3224 source#add_narrow_pattern m_qsearch;
3225 settext true E.s;
3226 coe {< m_qsearch = E.s >}
3228 else coe self
3230 | 0xff1b when m_autonarrow -> (* escape *)
3231 if nonemptystr m_qsearch
3232 then source#add_narrow_pattern m_qsearch;
3233 super#key key mask
3235 | 0xff0d | 0xff8d when m_autonarrow -> (* (kp) enter *)
3236 if nonemptystr m_qsearch
3237 then source#add_narrow_pattern m_qsearch;
3238 super#key key mask
3240 | key when m_autonarrow && (key != 0 && key land 0xff00 != 0xff00) ->
3241 let pattern = m_qsearch ^ toutf8 key in
3242 G.postRedisplay "outlinelistview autonarrow add";
3243 source#narrow pattern;
3244 settext true pattern;
3245 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3247 | key when m_autonarrow && key = 0xff08 -> (* backspace *)
3248 if emptystr m_qsearch
3249 then coe self
3250 else
3251 let pattern = withoutlastutf8 m_qsearch in
3252 G.postRedisplay "outlinelistview autonarrow backspace";
3253 ignore (source#renarrow);
3254 source#narrow pattern;
3255 settext true pattern;
3256 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3258 | 0xff9f | 0xffff -> (* (kp) delete *)
3259 source#remove m_active;
3260 G.postRedisplay "outline delete";
3261 let active = max 0 (m_active-1) in
3262 coe {< m_first = firstof m_first active;
3263 m_active = active >}
3265 | 0xff52 | 0xff97 when ctrl -> (* ctrl-(kp) up *)
3266 navscroll (max 0 (m_first - 1))
3268 | 0xff54 | 0xff99 when ctrl -> (* ctrl-(kp) down *)
3269 navscroll (min (source#getitemcount - 1) (m_first + 1))
3271 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3272 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3273 | 0xff55 | 0xff9a -> (* (kp) prior *)
3274 navigate ~-(fstate.maxrows)
3275 | 0xff56 | 0xff9b -> (* (kp) next *)
3276 navigate fstate.maxrows
3278 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
3279 let o =
3280 if ctrl
3281 then (
3282 G.postRedisplay "outline ctrl right";
3283 {< m_pan = m_pan + 1 >}
3285 else self#updownlevel 1
3287 coe o
3289 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
3290 let o =
3291 if ctrl
3292 then (
3293 G.postRedisplay "outline ctrl left";
3294 {< m_pan = m_pan - 1 >}
3296 else self#updownlevel ~-1
3298 coe o
3300 | 0xff50 | 0xff95 -> (* (kp) home *)
3301 G.postRedisplay "outline home";
3302 coe {< m_first = 0; m_active = 0 >}
3304 | 0xff57 | 0xff9c -> (* (kp) end *)
3305 let active = source#getitemcount - 1 in
3306 let first = max 0 (active - fstate.maxrows) in
3307 G.postRedisplay "outline end";
3308 coe {< m_active = active; m_first = first >}
3310 | _ -> super#key key mask
3313 let gotounder under =
3314 let getpath filename =
3315 let path =
3316 if nonemptystr filename
3317 then
3318 if Filename.is_relative filename
3319 then
3320 let dir = Filename.dirname state.path in
3321 let dir =
3322 if Filename.is_implicit dir
3323 then Filename.concat (Sys.getcwd ()) dir
3324 else dir
3326 Filename.concat dir filename
3327 else filename
3328 else E.s
3330 if Sys.file_exists path
3331 then path
3332 else E.s
3334 match under with
3335 | Ulinkgoto (pageno, top) ->
3336 if pageno >= 0
3337 then (
3338 addnav ();
3339 gotopage1 pageno top;
3342 | Ulinkuri s ->
3343 gotouri s
3345 | Uremote (filename, pageno) ->
3346 let path = getpath filename in
3347 if nonemptystr path
3348 then (
3349 if conf.riani
3350 then
3351 let command = Printf.sprintf "%s -page %d %S" !selfexec pageno path in
3352 try popen command []
3353 with exn ->
3354 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
3355 flush stderr;
3356 else
3357 let anchor = getanchor () in
3358 let ranchor = state.path, state.password, anchor, state.origin in
3359 state.origin <- E.s;
3360 state.anchor <- (pageno, 0.0, 0.0);
3361 state.ranchors <- ranchor :: state.ranchors;
3362 opendoc path E.s;
3364 else showtext '!' ("Could not find " ^ filename)
3366 | Uremotedest (filename, destname) ->
3367 let path = getpath filename in
3368 if nonemptystr path
3369 then (
3370 if conf.riani
3371 then
3372 let command = !selfexec ^ " " ^ path ^ " -dest " ^ destname in
3373 try popen command []
3374 with exn ->
3375 Printf.eprintf
3376 "failed to execute `%s': %s\n" command (exntos exn);
3377 flush stderr;
3378 else
3379 let anchor = getanchor () in
3380 let ranchor = state.path, state.password, anchor, state.origin in
3381 state.origin <- E.s;
3382 state.nameddest <- destname;
3383 state.ranchors <- ranchor :: state.ranchors;
3384 opendoc path E.s;
3386 else showtext '!' ("Could not find " ^ filename)
3388 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
3391 let gotohist (path, (c, bookmarks, x, anchor)) =
3392 Config.save leavebirdseye;
3393 state.anchor <- anchor;
3394 state.x <- x;
3395 state.bookmarks <- bookmarks;
3396 state.origin <- E.s;
3397 setconf conf c;
3398 opendoc path E.s;
3401 let gotooutline (_, _, kind) =
3402 match kind with
3403 | Onone -> ()
3404 | Oanchor anchor ->
3405 let (pageno, y, _) = anchor in
3406 let y = getanchory
3407 (if conf.presentation then (pageno, y, 1.0) else anchor)
3409 addnav ();
3410 gotoghyll y
3411 | Ouri uri -> gotounder (Ulinkuri uri)
3412 | Olaunch cmd -> gotounder (Ulaunch cmd)
3413 | Oremote remote -> gotounder (Uremote remote)
3414 | Ohistory hist -> gotohist hist
3415 | Oremotedest remotedest -> gotounder (Uremotedest remotedest)
3416 | Oaction f -> f ()
3419 let genhistoutlines =
3420 let order ty (p1, c1, _, _, _) (p2, c2, _, _, _) =
3421 match ty with
3422 | `lastvisit -> compare c1.lastvisit c2.lastvisit
3423 | `path -> compare p2 p1
3424 | `file -> compare (Filename.basename p2) (Filename.basename p1)
3425 | `title ->
3426 let e1 = emptystr c1.title
3427 and e2 = emptystr c2.title in
3428 (**) if e1 && e2
3429 then compare (Filename.basename p2) (Filename.basename p1)
3430 else if e1 then -1
3431 else if e2 then 1
3432 else compare c1.title c2.title
3434 let showfullpath = ref false in
3435 fun orderty ->
3436 let setorty s t =
3437 let s = if orderty = t then "[\xe2\x88\x9a] " ^ s else "[ ] " ^ s in
3438 s, 0, Oaction (fun () -> Config.historder := t; reeenterhist := true)
3440 let list = ref [] in
3441 if Config.gethist list
3442 then
3443 let ol =
3444 List.fold_left
3445 (fun accu (path, c, b, x, a) ->
3446 let hist = (path, (c, b, x, a)) in
3447 let s = if !showfullpath then path else Filename.basename path in
3448 let base = mbtoutf8 s in
3449 (base ^ "\000" ^ c.title, 1, Ohistory hist) :: accu
3451 [ setorty "Sort by time of last visit" `lastvisit;
3452 setorty "Sort by file name" `file;
3453 setorty "Sort by path" `path;
3454 setorty "Sort by title" `title;
3455 (if !showfullpath then "\xe2\x88\x9a "
3456 else " ") ^ "Show full path", 0, Oaction (fun () ->
3457 showfullpath := not !showfullpath; reeenterhist := true)
3458 ] (List.sort (order orderty) !list)
3460 Array.of_list ol
3461 else E.a;
3464 let outlinesource sourcetype =
3465 (object (self)
3466 inherit lvsourcebase
3467 val mutable m_items = E.a
3468 val mutable m_minfo = E.a
3469 val mutable m_orig_items = E.a
3470 val mutable m_orig_minfo = E.a
3471 val mutable m_narrow_patterns = []
3472 val mutable m_hadremovals = false
3473 val mutable m_gen = -1
3475 method getitemcount =
3476 Array.length m_items + (if m_hadremovals then 1 else 0)
3478 method getitem n =
3479 if n == Array.length m_items && m_hadremovals
3480 then
3481 ("[Confirm removal]", 0)
3482 else
3483 let s, n, _ = m_items.(n) in
3484 (s, n)
3486 method exit ~uioh ~cancel ~active ~first ~pan =
3487 ignore (uioh, first);
3488 let confrimremoval = m_hadremovals && active = Array.length m_items in
3489 let items, minfo =
3490 if m_narrow_patterns = []
3491 then m_orig_items, m_orig_minfo
3492 else m_items, m_minfo
3494 if not cancel
3495 then (
3496 if not confrimremoval
3497 then (
3498 gotooutline m_items.(active);
3499 m_items <- items;
3500 m_minfo <- minfo;
3502 else (
3503 state.bookmarks <- Array.to_list m_items;
3504 m_orig_items <- m_items;
3505 m_orig_minfo <- m_minfo;
3508 else (
3509 m_items <- items;
3510 m_minfo <- minfo;
3512 m_pan <- pan;
3513 None
3515 method hasaction _ = true
3517 method greetmsg =
3518 if Array.length m_items != Array.length m_orig_items
3519 then
3520 let s =
3521 match m_narrow_patterns with
3522 | one :: [] -> one
3523 | many -> String.concat "\xe2\x80\xa6" (List.rev many)
3525 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
3526 else E.s
3528 method statestr =
3529 match m_narrow_patterns with
3530 | [] -> E.s
3531 | one :: [] -> one
3532 | head :: _ -> "\xe2\x80\xa6" ^ head
3534 method narrow pattern =
3535 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3536 match reopt with
3537 | None -> ()
3538 | Some re ->
3539 let rec loop accu minfo n =
3540 if n = -1
3541 then (
3542 m_items <- Array.of_list accu;
3543 m_minfo <- Array.of_list minfo;
3545 else
3546 let (s, _, t) as o = m_items.(n) in
3547 let accu, minfo =
3548 match t with
3549 | Oaction _ -> o :: accu, (0, 0) :: minfo
3550 | Onone | Oanchor _ | Ouri _ | Olaunch _
3551 | Oremote _ | Oremotedest _ | Ohistory _ ->
3552 let first =
3553 try Str.search_forward re s 0
3554 with Not_found -> -1
3556 if first >= 0
3557 then o :: accu, (first, Str.match_end ()) :: minfo
3558 else accu, minfo
3560 loop accu minfo (n-1)
3562 loop [] [] (Array.length m_items - 1)
3564 method getminfo = m_minfo
3566 method denarrow =
3567 m_orig_items <- (
3568 match sourcetype with
3569 | `bookmarks -> Array.of_list state.bookmarks
3570 | `outlines -> state.outlines
3571 | `history -> genhistoutlines !Config.historder
3573 m_minfo <- m_orig_minfo;
3574 m_items <- m_orig_items
3576 method remove m =
3577 if sourcetype = `bookmarks
3578 then
3579 if m >= 0 && m < Array.length m_items
3580 then (
3581 m_hadremovals <- true;
3582 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3583 let n = if n >= m then n+1 else n in
3584 m_items.(n)
3588 method add_narrow_pattern pattern =
3589 m_narrow_patterns <- pattern :: m_narrow_patterns
3591 method del_narrow_pattern =
3592 match m_narrow_patterns with
3593 | _ :: rest -> m_narrow_patterns <- rest
3594 | [] -> ()
3596 method renarrow =
3597 self#denarrow;
3598 match m_narrow_patterns with
3599 | pattern :: [] -> self#narrow pattern; pattern
3600 | list ->
3601 List.fold_left (fun accu pattern ->
3602 self#narrow pattern;
3603 pattern ^ "\xe2\x80\xa6" ^ accu) E.s list
3605 method calcactive anchor =
3606 let rely = getanchory anchor in
3607 let rec loop n best bestd =
3608 if n = Array.length m_items
3609 then best
3610 else
3611 let _, _, kind = m_items.(n) in
3612 match kind with
3613 | Oanchor anchor ->
3614 let orely = getanchory anchor in
3615 let d = abs (orely - rely) in
3616 if d < bestd
3617 then loop (n+1) n d
3618 else loop (n+1) best bestd
3619 | Onone | Oremote _ | Olaunch _
3620 | Oremotedest _ | Ouri _ | Ohistory _ | Oaction _ ->
3621 loop (n+1) best bestd
3623 loop 0 ~-1 max_int
3625 method reset anchor items =
3626 m_hadremovals <- false;
3627 if state.gen != m_gen
3628 then (
3629 m_orig_items <- items;
3630 m_items <- items;
3631 m_narrow_patterns <- [];
3632 m_minfo <- E.a;
3633 m_orig_minfo <- E.a;
3634 m_gen <- state.gen;
3636 else (
3637 if items != m_orig_items
3638 then (
3639 m_orig_items <- items;
3640 if m_narrow_patterns == []
3641 then m_items <- items;
3644 let active = self#calcactive anchor in
3645 m_active <- active;
3646 m_first <- firstof m_first active
3647 end)
3650 let enterselector sourcetype =
3651 resetmstate ();
3652 let source = outlinesource sourcetype in
3653 fun errmsg ->
3654 let outlines =
3655 match sourcetype with
3656 | `bookmarks -> Array.of_list state.bookmarks
3657 | `outlines -> state.outlines
3658 | `history -> genhistoutlines !Config.historder
3660 if Array.length outlines = 0
3661 then (
3662 showtext ' ' errmsg;
3664 else (
3665 state.text <- source#greetmsg;
3666 Wsi.setcursor Wsi.CURSOR_INHERIT;
3667 let anchor = getanchor () in
3668 source#reset anchor outlines;
3669 state.uioh <-
3670 coe (new outlinelistview ~zebra:(sourcetype=`history) ~source);
3671 G.postRedisplay "enter selector";
3675 let enteroutlinemode =
3676 let f = enterselector `outlines in
3677 fun () -> f "Document has no outline";
3680 let enterbookmarkmode =
3681 let f = enterselector `bookmarks in
3682 fun () -> f "Document has no bookmarks (yet)";
3685 let enterhistmode () = enterselector `history "No history (yet)";;
3687 let makecheckers () =
3688 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3689 following to say:
3690 converted by Issac Trotts. July 25, 2002 *)
3691 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3692 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3693 let id = GlTex.gen_texture () in
3694 GlTex.bind_texture `texture_2d id;
3695 GlPix.store (`unpack_alignment 1);
3696 GlTex.image2d image;
3697 List.iter (GlTex.parameter ~target:`texture_2d)
3698 [ `mag_filter `nearest; `min_filter `nearest ];
3702 let setcheckers enabled =
3703 match state.checkerstexid with
3704 | None ->
3705 if enabled then state.checkerstexid <- Some (makecheckers ())
3707 | Some checkerstexid ->
3708 if not enabled
3709 then (
3710 GlTex.delete_texture checkerstexid;
3711 state.checkerstexid <- None;
3715 let describe_location () =
3716 let fn = page_of_y state.y in
3717 let ln = page_of_y (state.y + state.winh - hscrollh () - 1) in
3718 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
3719 let percent =
3720 if maxy <= 0
3721 then 100.
3722 else (100. *. (float state.y /. float maxy))
3724 if fn = ln
3725 then
3726 Printf.sprintf "page %d of %d [%.2f%%]"
3727 (fn+1) state.pagecount percent
3728 else
3729 Printf.sprintf
3730 "pages %d-%d of %d [%.2f%%]"
3731 (fn+1) (ln+1) state.pagecount percent
3734 let setpresentationmode v =
3735 let n = page_of_y state.y in
3736 state.anchor <- (n, 0.0, 1.0);
3737 conf.presentation <- v;
3738 if conf.fitmodel = FitPage
3739 then reqlayout conf.angle conf.fitmodel;
3740 represent ();
3743 let enterinfomode =
3744 let btos b = if b then "\xe2\x88\x9a" else E.s in
3745 let showextended = ref false in
3746 let leave mode = function
3747 | Confirm -> state.mode <- mode
3748 | Cancel -> state.mode <- mode in
3749 let src =
3750 (object
3751 val mutable m_first_time = true
3752 val mutable m_l = []
3753 val mutable m_a = E.a
3754 val mutable m_prev_uioh = nouioh
3755 val mutable m_prev_mode = View
3757 inherit lvsourcebase
3759 method reset prev_mode prev_uioh =
3760 m_a <- Array.of_list (List.rev m_l);
3761 m_l <- [];
3762 m_prev_mode <- prev_mode;
3763 m_prev_uioh <- prev_uioh;
3764 if m_first_time
3765 then (
3766 let rec loop n =
3767 if n >= Array.length m_a
3768 then ()
3769 else
3770 match m_a.(n) with
3771 | _, _, _, Action _ -> m_active <- n
3772 | _ -> loop (n+1)
3774 loop 0;
3775 m_first_time <- false;
3778 method int name get set =
3779 m_l <-
3780 (name, `int get, 1, Action (
3781 fun u ->
3782 let ondone s =
3783 try set (int_of_string s)
3784 with exn ->
3785 state.text <- Printf.sprintf "bad integer `%s': %s"
3786 s (exntos exn)
3788 state.text <- E.s;
3789 let te = name ^ ": ", E.s, None, intentry, ondone, true in
3790 state.mode <- Textentry (te, leave m_prev_mode);
3792 )) :: m_l
3794 method int_with_suffix name get set =
3795 m_l <-
3796 (name, `intws get, 1, Action (
3797 fun u ->
3798 let ondone s =
3799 try set (int_of_string_with_suffix s)
3800 with exn ->
3801 state.text <- Printf.sprintf "bad integer `%s': %s"
3802 s (exntos exn)
3804 state.text <- E.s;
3805 let te =
3806 name ^ ": ", E.s, None, intentry_with_suffix, ondone, true
3808 state.mode <- Textentry (te, leave m_prev_mode);
3810 )) :: m_l
3812 method bool ?(offset=1) ?(btos=btos) name get set =
3813 m_l <-
3814 (name, `bool (btos, get), offset, Action (
3815 fun u ->
3816 let v = get () in
3817 set (not v);
3819 )) :: m_l
3821 method color name get set =
3822 m_l <-
3823 (name, `color get, 1, Action (
3824 fun u ->
3825 let invalid = (nan, nan, nan) in
3826 let ondone s =
3827 let c =
3828 try color_of_string s
3829 with exn ->
3830 state.text <- Printf.sprintf "bad color `%s': %s"
3831 s (exntos exn);
3832 invalid
3834 if c <> invalid
3835 then set c;
3837 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3838 state.text <- color_to_string (get ());
3839 state.mode <- Textentry (te, leave m_prev_mode);
3841 )) :: m_l
3843 method string name get set =
3844 m_l <-
3845 (name, `string get, 1, Action (
3846 fun u ->
3847 let ondone s = set s in
3848 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3849 state.mode <- Textentry (te, leave m_prev_mode);
3851 )) :: m_l
3853 method colorspace name get set =
3854 m_l <-
3855 (name, `string get, 1, Action (
3856 fun _ ->
3857 let source =
3858 (object
3859 inherit lvsourcebase
3861 initializer
3862 m_active <- CSTE.to_int conf.colorspace;
3863 m_first <- 0;
3865 method getitemcount =
3866 Array.length CSTE.names
3867 method getitem n =
3868 (CSTE.names.(n), 0)
3869 method exit ~uioh ~cancel ~active ~first ~pan =
3870 ignore (uioh, first, pan);
3871 if not cancel then set active;
3872 None
3873 method hasaction _ = true
3874 end)
3876 state.text <- E.s;
3877 let modehash = findkeyhash conf "info" in
3878 coe (new listview ~zebra:false ~helpmode:false
3879 ~source ~trusted:true ~modehash)
3880 )) :: m_l
3882 method paxmark name get set =
3883 m_l <-
3884 (name, `string get, 1, Action (
3885 fun _ ->
3886 let source =
3887 (object
3888 inherit lvsourcebase
3890 initializer
3891 m_active <- MTE.to_int conf.paxmark;
3892 m_first <- 0;
3894 method getitemcount = Array.length MTE.names
3895 method getitem n = (MTE.names.(n), 0)
3896 method exit ~uioh ~cancel ~active ~first ~pan =
3897 ignore (uioh, first, pan);
3898 if not cancel then set active;
3899 None
3900 method hasaction _ = true
3901 end)
3903 state.text <- E.s;
3904 let modehash = findkeyhash conf "info" in
3905 coe (new listview ~zebra:false ~helpmode:false
3906 ~source ~trusted:true ~modehash)
3907 )) :: m_l
3909 method fitmodel name get set =
3910 m_l <-
3911 (name, `string get, 1, Action (
3912 fun _ ->
3913 let source =
3914 (object
3915 inherit lvsourcebase
3917 initializer
3918 m_active <- FMTE.to_int conf.fitmodel;
3919 m_first <- 0;
3921 method getitemcount = Array.length FMTE.names
3922 method getitem n = (FMTE.names.(n), 0)
3923 method exit ~uioh ~cancel ~active ~first ~pan =
3924 ignore (uioh, first, pan);
3925 if not cancel then set active;
3926 None
3927 method hasaction _ = true
3928 end)
3930 state.text <- E.s;
3931 let modehash = findkeyhash conf "info" in
3932 coe (new listview ~zebra:false ~helpmode:false
3933 ~source ~trusted:true ~modehash)
3934 )) :: m_l
3936 method caption s offset =
3937 m_l <- (s, `empty, offset, Noaction) :: m_l
3939 method caption2 s f offset =
3940 m_l <- (s, `string f, offset, Noaction) :: m_l
3942 method getitemcount = Array.length m_a
3944 method getitem n =
3945 let tostr = function
3946 | `int f -> string_of_int (f ())
3947 | `intws f -> string_with_suffix_of_int (f ())
3948 | `string f -> f ()
3949 | `color f -> color_to_string (f ())
3950 | `bool (btos, f) -> btos (f ())
3951 | `empty -> E.s
3953 let name, t, offset, _ = m_a.(n) in
3954 ((let s = tostr t in
3955 if nonemptystr s
3956 then Printf.sprintf "%s\t%s" name s
3957 else name),
3958 offset)
3960 method exit ~uioh ~cancel ~active ~first ~pan =
3961 let uiohopt =
3962 if not cancel
3963 then (
3964 let uioh =
3965 match m_a.(active) with
3966 | _, _, _, Action f -> f uioh
3967 | _ -> uioh
3969 Some uioh
3971 else None
3973 m_active <- active;
3974 m_first <- first;
3975 m_pan <- pan;
3976 uiohopt
3978 method hasaction n =
3979 match m_a.(n) with
3980 | _, _, _, Action _ -> true
3981 | _ -> false
3982 end)
3984 let rec fillsrc prevmode prevuioh =
3985 let sep () = src#caption E.s 0 in
3986 let colorp name get set =
3987 src#string name
3988 (fun () -> color_to_string (get ()))
3989 (fun v ->
3991 let c = color_of_string v in
3992 set c
3993 with exn ->
3994 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
3997 let oldmode = state.mode in
3998 let birdseye = isbirdseye state.mode in
4000 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4002 src#bool "presentation mode"
4003 (fun () -> conf.presentation)
4004 (fun v -> setpresentationmode v);
4006 src#bool "ignore case in searches"
4007 (fun () -> conf.icase)
4008 (fun v -> conf.icase <- v);
4010 src#bool "preload"
4011 (fun () -> conf.preload)
4012 (fun v -> conf.preload <- v);
4014 src#bool "highlight links"
4015 (fun () -> conf.hlinks)
4016 (fun v -> conf.hlinks <- v);
4018 src#bool "under info"
4019 (fun () -> conf.underinfo)
4020 (fun v -> conf.underinfo <- v);
4022 src#bool "persistent bookmarks"
4023 (fun () -> conf.savebmarks)
4024 (fun v -> conf.savebmarks <- v);
4026 src#fitmodel "fit model"
4027 (fun () -> FMTE.to_string conf.fitmodel)
4028 (fun v -> reqlayout conf.angle (FMTE.of_int v));
4030 src#bool "trim margins"
4031 (fun () -> conf.trimmargins)
4032 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4034 src#bool "persistent location"
4035 (fun () -> conf.jumpback)
4036 (fun v -> conf.jumpback <- v);
4038 sep ();
4039 src#int "inter-page space"
4040 (fun () -> conf.interpagespace)
4041 (fun n ->
4042 conf.interpagespace <- n;
4043 docolumns conf.columns;
4044 let pageno, py =
4045 match state.layout with
4046 | [] -> 0, 0
4047 | l :: _ ->
4048 l.pageno, l.pagey
4050 state.maxy <- calcheight ();
4051 let y = getpagey pageno in
4052 gotoy (y + py)
4055 src#int "page bias"
4056 (fun () -> conf.pagebias)
4057 (fun v -> conf.pagebias <- v);
4059 src#int "scroll step"
4060 (fun () -> conf.scrollstep)
4061 (fun n -> conf.scrollstep <- n);
4063 src#int "horizontal scroll step"
4064 (fun () -> conf.hscrollstep)
4065 (fun v -> conf.hscrollstep <- v);
4067 src#int "auto scroll step"
4068 (fun () ->
4069 match state.autoscroll with
4070 | Some step -> step
4071 | _ -> conf.autoscrollstep)
4072 (fun n ->
4073 let n = boundastep state.winh n in
4074 if state.autoscroll <> None
4075 then state.autoscroll <- Some n;
4076 conf.autoscrollstep <- n);
4078 src#int "zoom"
4079 (fun () -> truncate (conf.zoom *. 100.))
4080 (fun v -> setzoom ((float v) /. 100.));
4082 src#int "rotation"
4083 (fun () -> conf.angle)
4084 (fun v -> reqlayout v conf.fitmodel);
4086 src#int "scroll bar width"
4087 (fun () -> conf.scrollbw)
4088 (fun v ->
4089 conf.scrollbw <- v;
4090 reshape state.winw state.winh;
4093 src#int "scroll handle height"
4094 (fun () -> conf.scrollh)
4095 (fun v -> conf.scrollh <- v;);
4097 src#int "thumbnail width"
4098 (fun () -> conf.thumbw)
4099 (fun v ->
4100 conf.thumbw <- min 4096 v;
4101 match oldmode with
4102 | Birdseye beye ->
4103 leavebirdseye beye false;
4104 enterbirdseye ()
4105 | _ -> ()
4108 let mode = state.mode in
4109 src#string "columns"
4110 (fun () ->
4111 match conf.columns with
4112 | Csingle _ -> "1"
4113 | Cmulti (multi, _) -> multicolumns_to_string multi
4114 | Csplit (count, _) -> "-" ^ string_of_int count
4116 (fun v ->
4117 let n, a, b = multicolumns_of_string v in
4118 setcolumns mode n a b);
4120 sep ();
4121 src#caption "Pixmap cache" 0;
4122 src#int_with_suffix "size (advisory)"
4123 (fun () -> conf.memlimit)
4124 (fun v -> conf.memlimit <- v);
4126 src#caption2 "used"
4127 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4128 (string_with_suffix_of_int state.memused)
4129 (Hashtbl.length state.tilemap)) 1;
4131 sep ();
4132 src#caption "Layout" 0;
4133 src#caption2 "Dimension"
4134 (fun () ->
4135 Printf.sprintf "%dx%d (virtual %dx%d)"
4136 state.winw state.winh
4137 state.w state.maxy)
4139 if conf.debug
4140 then
4141 src#caption2 "Position" (fun () ->
4142 Printf.sprintf "%dx%d" state.x state.y
4144 else
4145 src#caption2 "Position" (fun () -> describe_location ()) 1
4148 sep ();
4149 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4150 "Save these parameters as global defaults at exit"
4151 (fun () -> conf.bedefault)
4152 (fun v -> conf.bedefault <- v)
4155 sep ();
4156 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4157 src#bool ~offset:0 ~btos "Extended parameters"
4158 (fun () -> !showextended)
4159 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4160 if !showextended
4161 then (
4162 src#bool "checkers"
4163 (fun () -> conf.checkers)
4164 (fun v -> conf.checkers <- v; setcheckers v);
4165 src#bool "update cursor"
4166 (fun () -> conf.updatecurs)
4167 (fun v -> conf.updatecurs <- v);
4168 src#bool "scroll-bar on the left"
4169 (fun () -> conf.leftscroll)
4170 (fun v -> conf.leftscroll <- v);
4171 src#bool "verbose"
4172 (fun () -> conf.verbose)
4173 (fun v -> conf.verbose <- v);
4174 src#bool "invert colors"
4175 (fun () -> conf.invert)
4176 (fun v -> conf.invert <- v);
4177 src#bool "max fit"
4178 (fun () -> conf.maxhfit)
4179 (fun v -> conf.maxhfit <- v);
4180 src#bool "redirect stderr"
4181 (fun () -> conf.redirectstderr)
4182 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4183 src#bool "pax mode"
4184 (fun () -> conf.pax != None)
4185 (fun v ->
4186 if v
4187 then conf.pax <- Some (ref (now (), 0, 0))
4188 else conf.pax <- None);
4189 src#string "uri launcher"
4190 (fun () -> conf.urilauncher)
4191 (fun v -> conf.urilauncher <- v);
4192 src#string "path launcher"
4193 (fun () -> conf.pathlauncher)
4194 (fun v -> conf.pathlauncher <- v);
4195 src#string "tile size"
4196 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4197 (fun v ->
4199 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4200 conf.tilew <- max 64 w;
4201 conf.tileh <- max 64 h;
4202 flushtiles ();
4203 with exn ->
4204 state.text <- Printf.sprintf "bad tile size `%s': %s"
4205 v (exntos exn)
4207 src#int "texture count"
4208 (fun () -> conf.texcount)
4209 (fun v ->
4210 if realloctexts v
4211 then conf.texcount <- v
4212 else showtext '!' " Failed to set texture count please retry later"
4214 src#int "slice height"
4215 (fun () -> conf.sliceheight)
4216 (fun v ->
4217 conf.sliceheight <- v;
4218 wcmd "sliceh %d" conf.sliceheight;
4220 src#int "anti-aliasing level"
4221 (fun () -> conf.aalevel)
4222 (fun v ->
4223 conf.aalevel <- bound v 0 8;
4224 state.anchor <- getanchor ();
4225 opendoc state.path state.password;
4227 src#string "page scroll scaling factor"
4228 (fun () -> string_of_float conf.pgscale)
4229 (fun v ->
4231 let s = float_of_string v in
4232 conf.pgscale <- s
4233 with exn ->
4234 state.text <- Printf.sprintf
4235 "bad page scroll scaling factor `%s': %s" v (exntos exn)
4238 src#int "ui font size"
4239 (fun () -> fstate.fontsize)
4240 (fun v -> setfontsize (bound v 5 100));
4241 src#int "hint font size"
4242 (fun () -> conf.hfsize)
4243 (fun v -> conf.hfsize <- bound v 5 100);
4244 colorp "background color"
4245 (fun () -> conf.bgcolor)
4246 (fun v -> conf.bgcolor <- v);
4247 src#bool "crop hack"
4248 (fun () -> conf.crophack)
4249 (fun v -> conf.crophack <- v);
4250 src#string "trim fuzz"
4251 (fun () -> irect_to_string conf.trimfuzz)
4252 (fun v ->
4254 conf.trimfuzz <- irect_of_string v;
4255 if conf.trimmargins
4256 then settrim true conf.trimfuzz;
4257 with exn ->
4258 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
4260 src#string "throttle"
4261 (fun () ->
4262 match conf.maxwait with
4263 | None -> "show place holder if page is not ready"
4264 | Some time ->
4265 if time = infinity
4266 then "wait for page to fully render"
4267 else
4268 "wait " ^ string_of_float time
4269 ^ " seconds before showing placeholder"
4271 (fun v ->
4273 let f = float_of_string v in
4274 if f <= 0.0
4275 then conf.maxwait <- None
4276 else conf.maxwait <- Some f
4277 with exn ->
4278 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
4280 src#string "ghyll scroll"
4281 (fun () ->
4282 match conf.ghyllscroll with
4283 | None -> E.s
4284 | Some nab -> ghyllscroll_to_string nab
4286 (fun v ->
4287 try conf.ghyllscroll <- ghyllscroll_of_string v
4288 with exn ->
4289 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
4291 src#string "selection command"
4292 (fun () -> conf.selcmd)
4293 (fun v -> conf.selcmd <- v);
4294 src#string "synctex command"
4295 (fun () -> conf.stcmd)
4296 (fun v -> conf.stcmd <- v);
4297 src#string "pax command"
4298 (fun () -> conf.paxcmd)
4299 (fun v -> conf.paxcmd <- v);
4300 src#colorspace "color space"
4301 (fun () -> CSTE.to_string conf.colorspace)
4302 (fun v ->
4303 conf.colorspace <- CSTE.of_int v;
4304 wcmd "cs %d" v;
4305 load state.layout;
4307 src#paxmark "pax mark method"
4308 (fun () -> MTE.to_string conf.paxmark)
4309 (fun v -> conf.paxmark <- MTE.of_int v);
4310 if pbousable ()
4311 then
4312 src#bool "use PBO"
4313 (fun () -> conf.usepbo)
4314 (fun v -> conf.usepbo <- v);
4315 src#bool "mouse wheel scrolls pages"
4316 (fun () -> conf.wheelbypage)
4317 (fun v -> conf.wheelbypage <- v);
4318 src#bool "open remote links in a new instance"
4319 (fun () -> conf.riani)
4320 (fun v -> conf.riani <- v);
4323 sep ();
4324 src#caption "Document" 0;
4325 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4326 src#caption2 "Pages"
4327 (fun () -> string_of_int state.pagecount) 1;
4328 src#caption2 "Dimensions"
4329 (fun () -> string_of_int (List.length state.pdims)) 1;
4330 if conf.trimmargins
4331 then (
4332 sep ();
4333 src#caption "Trimmed margins" 0;
4334 src#caption2 "Dimensions"
4335 (fun () -> string_of_int (List.length state.pdims)) 1;
4338 sep ();
4339 src#caption "OpenGL" 0;
4340 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4341 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4343 sep ();
4344 src#caption "Location" 0;
4345 if nonemptystr state.origin
4346 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
4347 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
4349 src#reset prevmode prevuioh;
4351 fun () ->
4352 state.text <- E.s;
4353 resetmstate ();
4354 let prevmode = state.mode
4355 and prevuioh = state.uioh in
4356 fillsrc prevmode prevuioh;
4357 let source = (src :> lvsource) in
4358 let modehash = findkeyhash conf "info" in
4359 state.uioh <- coe (object (self)
4360 inherit listview ~zebra:false ~helpmode:false
4361 ~source ~trusted:true ~modehash as super
4362 val mutable m_prevmemused = 0
4363 method infochanged = function
4364 | Memused ->
4365 if m_prevmemused != state.memused
4366 then (
4367 m_prevmemused <- state.memused;
4368 G.postRedisplay "memusedchanged";
4370 | Pdim -> G.postRedisplay "pdimchanged"
4371 | Docinfo -> fillsrc prevmode prevuioh
4373 method key key mask =
4374 if not (Wsi.withctrl mask)
4375 then
4376 match key with
4377 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
4378 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
4379 | _ -> super#key key mask
4380 else super#key key mask
4381 end);
4382 G.postRedisplay "info";
4385 let enterhelpmode =
4386 let source =
4387 (object
4388 inherit lvsourcebase
4389 method getitemcount = Array.length state.help
4390 method getitem n =
4391 let s, l, _ = state.help.(n) in
4392 (s, l)
4394 method exit ~uioh ~cancel ~active ~first ~pan =
4395 let optuioh =
4396 if not cancel
4397 then (
4398 match state.help.(active) with
4399 | _, _, Action f -> Some (f uioh)
4400 | _ -> Some (uioh)
4402 else None
4404 m_active <- active;
4405 m_first <- first;
4406 m_pan <- pan;
4407 optuioh
4409 method hasaction n =
4410 match state.help.(n) with
4411 | _, _, Action _ -> true
4412 | _ -> false
4414 initializer
4415 m_active <- -1
4416 end)
4417 in fun () ->
4418 let modehash = findkeyhash conf "help" in
4419 resetmstate ();
4420 state.uioh <- coe (new listview
4421 ~zebra:false ~helpmode:true
4422 ~source ~trusted:true ~modehash);
4423 G.postRedisplay "help";
4426 let entermsgsmode =
4427 let msgsource =
4428 let re = Str.regexp "[\r\n]" in
4429 (object
4430 inherit lvsourcebase
4431 val mutable m_items = E.a
4433 method getitemcount = 1 + Array.length m_items
4435 method getitem n =
4436 if n = 0
4437 then "[Clear]", 0
4438 else m_items.(n-1), 0
4440 method exit ~uioh ~cancel ~active ~first ~pan =
4441 ignore uioh;
4442 if not cancel
4443 then (
4444 if active = 0
4445 then Buffer.clear state.errmsgs;
4447 m_active <- active;
4448 m_first <- first;
4449 m_pan <- pan;
4450 None
4452 method hasaction n =
4453 n = 0
4455 method reset =
4456 state.newerrmsgs <- false;
4457 let l = Str.split re (Buffer.contents state.errmsgs) in
4458 m_items <- Array.of_list l
4460 initializer
4461 m_active <- 0
4462 end)
4463 in fun () ->
4464 state.text <- E.s;
4465 resetmstate ();
4466 msgsource#reset;
4467 let source = (msgsource :> lvsource) in
4468 let modehash = findkeyhash conf "listview" in
4469 state.uioh <- coe (object
4470 inherit listview ~zebra:false ~helpmode:false
4471 ~source ~trusted:false ~modehash as super
4472 method display =
4473 if state.newerrmsgs
4474 then msgsource#reset;
4475 super#display
4476 end);
4477 G.postRedisplay "msgs";
4480 let quickbookmark ?title () =
4481 match state.layout with
4482 | [] -> ()
4483 | l :: _ ->
4484 let title =
4485 match title with
4486 | None ->
4487 let tm = Unix.localtime (now ()) in
4488 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4489 (l.pageno+1)
4490 tm.Unix.tm_mday
4491 tm.Unix.tm_mon
4492 (tm.Unix.tm_year + 1900)
4493 tm.Unix.tm_hour
4494 tm.Unix.tm_min
4495 | Some title -> title
4497 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4500 let setautoscrollspeed step goingdown =
4501 let incr = max 1 ((abs step) / 2) in
4502 let incr = if goingdown then incr else -incr in
4503 let astep = boundastep state.winh (step + incr) in
4504 state.autoscroll <- Some astep;
4507 let canpan () =
4508 match conf.columns with
4509 | Csplit _ -> true
4510 | _ -> state.x != 0 || conf.zoom > 1.0
4513 let panbound x = bound x (-state.w) (wadjsb state.winw);;
4515 let existsinrow pageno (columns, coverA, coverB) p =
4516 let last = ((pageno - coverA) mod columns) + columns in
4517 let rec any = function
4518 | [] -> false
4519 | l :: rest ->
4520 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4521 then p l
4522 else (
4523 if not (p l)
4524 then (if l.pageno = last then false else any rest)
4525 else true
4528 any state.layout
4531 let nextpage () =
4532 match state.layout with
4533 | [] ->
4534 let pageno = page_of_y state.y in
4535 gotoghyll (getpagey (pageno+1))
4536 | l :: rest ->
4537 match conf.columns with
4538 | Csingle _ ->
4539 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4540 then
4541 let y = clamp (pgscale state.winh) in
4542 gotoghyll y
4543 else
4544 let pageno = min (l.pageno+1) (state.pagecount-1) in
4545 gotoghyll (getpagey pageno)
4546 | Cmulti ((c, _, _) as cl, _) ->
4547 if conf.presentation
4548 && (existsinrow l.pageno cl
4549 (fun l -> l.pageh > l.pagey + l.pagevh))
4550 then
4551 let y = clamp (pgscale state.winh) in
4552 gotoghyll y
4553 else
4554 let pageno = min (l.pageno+c) (state.pagecount-1) in
4555 gotoghyll (getpagey pageno)
4556 | Csplit (n, _) ->
4557 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4558 then
4559 let pagey, pageh = getpageyh l.pageno in
4560 let pagey = pagey + pageh * l.pagecol in
4561 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4562 gotoghyll (pagey + pageh + ips)
4565 let prevpage () =
4566 match state.layout with
4567 | [] ->
4568 let pageno = page_of_y state.y in
4569 gotoghyll (getpagey (pageno-1))
4570 | l :: _ ->
4571 match conf.columns with
4572 | Csingle _ ->
4573 if conf.presentation && l.pagey != 0
4574 then
4575 gotoghyll (clamp (pgscale ~-(state.winh)))
4576 else
4577 let pageno = max 0 (l.pageno-1) in
4578 gotoghyll (getpagey pageno)
4579 | Cmulti ((c, _, coverB) as cl, _) ->
4580 if conf.presentation &&
4581 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4582 then
4583 gotoghyll (clamp (pgscale ~-(state.winh)))
4584 else
4585 let decr =
4586 if l.pageno = state.pagecount - coverB
4587 then 1
4588 else c
4590 let pageno = max 0 (l.pageno-decr) in
4591 gotoghyll (getpagey pageno)
4592 | Csplit (n, _) ->
4593 let y =
4594 if l.pagecol = 0
4595 then
4596 if l.pageno = 0
4597 then l.pagey
4598 else
4599 let pageno = max 0 (l.pageno-1) in
4600 let pagey, pageh = getpageyh pageno in
4601 pagey + (n-1)*pageh
4602 else
4603 let pagey, pageh = getpageyh l.pageno in
4604 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4606 gotoghyll y
4609 let viewkeyboard key mask =
4610 let enttext te =
4611 let mode = state.mode in
4612 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4613 state.text <- E.s;
4614 enttext ();
4615 G.postRedisplay "view:enttext"
4617 let ctrl = Wsi.withctrl mask in
4618 let key =
4619 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
4621 match key with
4622 | 81 -> (* Q *)
4623 exit 0
4625 | 0xff63 -> (* insert *)
4626 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4627 then (
4628 state.mode <- LinkNav (Ltgendir 0);
4629 gotoy state.y;
4631 else showtext '!' "Keyboard link navigation does not work under rotation"
4633 | 0xff1b | 113 -> (* escape / q *)
4634 begin match state.mstate with
4635 | Mzoomrect _ ->
4636 resetmstate ();
4637 G.postRedisplay "kill zoom rect";
4638 | _ ->
4639 begin match state.mode with
4640 | LinkNav _ ->
4641 state.mode <- View;
4642 G.postRedisplay "esc leave linknav"
4643 | _ ->
4644 match state.ranchors with
4645 | [] -> raise Quit
4646 | (path, password, anchor, origin) :: rest ->
4647 state.ranchors <- rest;
4648 state.anchor <- anchor;
4649 state.origin <- origin;
4650 state.nameddest <- E.s;
4651 opendoc path password
4652 end;
4653 end;
4655 | 0xff08 -> (* backspace *)
4656 gotoghyll (getnav ~-1)
4658 | 111 -> (* o *)
4659 enteroutlinemode ()
4661 | 72 -> (* H *)
4662 enterhistmode ()
4664 | 117 -> (* u *)
4665 state.rects <- [];
4666 state.text <- E.s;
4667 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap;
4668 G.postRedisplay "dehighlight";
4670 | 47 | 63 -> (* / ? *)
4671 let ondone isforw s =
4672 cbput state.hists.pat s;
4673 state.searchpattern <- s;
4674 search s isforw
4676 let s = String.create 1 in
4677 s.[0] <- Char.chr key;
4678 enttext (s, E.s, Some (onhist state.hists.pat),
4679 textentry, ondone (key = 47), true)
4681 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
4682 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4683 setzoom (conf.zoom +. incr)
4685 | 43 | 0xffab -> (* + *)
4686 let ondone s =
4687 let n =
4688 try int_of_string s with exc ->
4689 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
4690 max_int
4692 if n != max_int
4693 then (
4694 conf.pagebias <- n;
4695 state.text <- "page bias is now " ^ string_of_int n;
4698 enttext ("page bias: ", E.s, None, intentry, ondone, true)
4700 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4701 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4702 setzoom (max 0.01 (conf.zoom -. decr))
4704 | 45 | 0xffad -> (* - *)
4705 let ondone msg = state.text <- msg in
4706 enttext (
4707 "option [acfhilpstvxACFPRSZTISM]: ", E.s, None,
4708 optentry state.mode, ondone, true
4711 | 48 when ctrl -> (* ctrl-0 *)
4712 if conf.zoom = 1.0
4713 then (
4714 state.x <- 0;
4715 gotoy state.y
4717 else setzoom 1.0
4719 | (49 | 50) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
4720 let cols =
4721 match conf.columns with
4722 | Csingle _ | Cmulti _ -> 1
4723 | Csplit (n, _) -> n
4725 let h = state.winh -
4726 conf.interpagespace lsl (if conf.presentation then 1 else 0)
4728 let zoom = zoomforh state.winw h (vscrollw ()) cols in
4729 if zoom > 0.0 && (key = 50 || zoom < 1.0)
4730 then setzoom zoom
4732 | 51 when ctrl -> (* ctrl-3 *)
4733 let fm =
4734 match conf.fitmodel with
4735 | FitWidth -> FitProportional
4736 | FitProportional -> FitPage
4737 | FitPage -> FitWidth
4739 state.text <- "fit model: " ^ FMTE.to_string fm;
4740 reqlayout conf.angle fm
4742 | 0xffc6 -> (* f9 *)
4743 togglebirdseye ()
4745 | 57 when ctrl -> (* ctrl-9 *)
4746 togglebirdseye ()
4748 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4749 when not ctrl -> (* 0..9 *)
4750 let ondone s =
4751 let n =
4752 try int_of_string s with exc ->
4753 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
4756 if n >= 0
4757 then (
4758 addnav ();
4759 cbput state.hists.pag (string_of_int n);
4760 gotopage1 (n + conf.pagebias - 1) 0;
4763 let pageentry text key =
4764 match Char.unsafe_chr key with
4765 | 'g' -> TEdone text
4766 | _ -> intentry text key
4768 let text = "x" in text.[0] <- Char.chr key;
4769 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
4771 | 98 -> (* b *)
4772 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
4773 reshape state.winw state.winh;
4775 | 66 -> (* B *)
4776 state.bzoom <- not state.bzoom;
4777 state.rects <- [];
4778 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
4780 | 108 -> (* l *)
4781 conf.hlinks <- not conf.hlinks;
4782 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4783 G.postRedisplay "toggle highlightlinks";
4785 | 70 -> (* F *)
4786 state.glinks <- true;
4787 let mode = state.mode in
4788 state.mode <- Textentry (
4789 (":", E.s, None, linknentry, linkndone gotounder, false),
4790 (fun _ ->
4791 state.glinks <- false;
4792 state.mode <- mode)
4794 state.text <- E.s;
4795 G.postRedisplay "view:linkent(F)"
4797 | 121 -> (* y *)
4798 state.glinks <- true;
4799 let mode = state.mode in
4800 state.mode <- Textentry (
4802 ":", E.s, None, linknentry, linkndone (fun under ->
4803 selstring (undertext under);
4804 ), false
4806 fun _ ->
4807 state.glinks <- false;
4808 state.mode <- mode
4810 state.text <- E.s;
4811 G.postRedisplay "view:linkent"
4813 | 97 -> (* a *)
4814 begin match state.autoscroll with
4815 | Some step ->
4816 conf.autoscrollstep <- step;
4817 state.autoscroll <- None
4818 | None ->
4819 if conf.autoscrollstep = 0
4820 then state.autoscroll <- Some 1
4821 else state.autoscroll <- Some conf.autoscrollstep
4824 | 112 when ctrl -> (* ctrl-p *)
4825 launchpath ()
4827 | 80 -> (* P *)
4828 setpresentationmode (not conf.presentation);
4829 showtext ' ' ("presentation mode " ^
4830 if conf.presentation then "on" else "off");
4832 | 102 -> (* f *)
4833 if List.mem Wsi.Fullscreen state.winstate
4834 then Wsi.reshape conf.cwinw conf.cwinh
4835 else Wsi.fullscreen ()
4837 | 112 | 78 -> (* p|N *)
4838 search state.searchpattern false
4840 | 110 | 0xffc0 -> (* n|F3 *)
4841 search state.searchpattern true
4843 | 116 -> (* t *)
4844 begin match state.layout with
4845 | [] -> ()
4846 | l :: _ ->
4847 gotoghyll (getpagey l.pageno)
4850 | 32 -> (* space *)
4851 nextpage ()
4853 | 0xff9f | 0xffff -> (* delete *)
4854 prevpage ()
4856 | 61 -> (* = *)
4857 showtext ' ' (describe_location ());
4859 | 119 -> (* w *)
4860 begin match state.layout with
4861 | [] -> ()
4862 | l :: _ ->
4863 Wsi.reshape (l.pagew + vscrollw ()) l.pageh;
4864 G.postRedisplay "w"
4867 | 39 -> (* ' *)
4868 enterbookmarkmode ()
4870 | 104 | 0xffbe -> (* h|F1 *)
4871 enterhelpmode ()
4873 | 105 -> (* i *)
4874 enterinfomode ()
4876 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
4877 entermsgsmode ()
4879 | 109 -> (* m *)
4880 let ondone s =
4881 match state.layout with
4882 | l :: _ ->
4883 if nonemptystr s
4884 then
4885 state.bookmarks <-
4886 (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4887 | _ -> ()
4889 enttext ("bookmark: ", E.s, None, textentry, ondone, true)
4891 | 126 -> (* ~ *)
4892 quickbookmark ();
4893 showtext ' ' "Quick bookmark added";
4895 | 122 -> (* z *)
4896 begin match state.layout with
4897 | l :: _ ->
4898 let rect = getpdimrect l.pagedimno in
4899 let w, h =
4900 if conf.crophack
4901 then
4902 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4903 truncate (1.2 *. (rect.(3) -. rect.(0))))
4904 else
4905 (truncate (rect.(1) -. rect.(0)),
4906 truncate (rect.(3) -. rect.(0)))
4908 let w = truncate ((float w)*.conf.zoom)
4909 and h = truncate ((float h)*.conf.zoom) in
4910 if w != 0 && h != 0
4911 then (
4912 state.anchor <- getanchor ();
4913 Wsi.reshape (w + vscrollw ()) (h + conf.interpagespace)
4915 G.postRedisplay "z";
4917 | [] -> ()
4920 | 120 -> (* x *)
4921 state.roam ()
4922 | 60 | 62 -> (* < > *)
4923 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.fitmodel
4925 | 91 | 93 -> (* [ ] *)
4926 conf.colorscale <-
4927 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
4929 G.postRedisplay "brightness";
4931 | 99 when state.mode = View -> (* [alt-]c *)
4932 if Wsi.withalt mask
4933 then (
4934 if conf.zoom > 1.0
4935 then
4936 let m = (wadjsb state.winw - state.w) / 2 in
4937 state.x <- m;
4938 gotoy_and_clear_text state.y
4940 else
4941 let (c, a, b), z =
4942 match state.prevcolumns with
4943 | None -> (1, 0, 0), 1.0
4944 | Some (columns, z) ->
4945 let cab =
4946 match columns with
4947 | Csplit (c, _) -> -c, 0, 0
4948 | Cmulti ((c, a, b), _) -> c, a, b
4949 | Csingle _ -> 1, 0, 0
4951 cab, z
4953 setcolumns View c a b;
4954 setzoom z
4956 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask
4957 -> (* ctrl-shift- (kp) [up|down] *)
4958 let zoom, x = state.prevzoom in
4959 setzoom zoom;
4960 state.x <- x;
4962 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
4963 begin match state.autoscroll with
4964 | None ->
4965 begin match state.mode with
4966 | Birdseye beye -> upbirdseye 1 beye
4967 | _ ->
4968 if ctrl
4969 then gotoy_and_clear_text (clamp ~-(state.winh/2))
4970 else (
4971 if not (Wsi.withshift mask) && conf.presentation
4972 then prevpage ()
4973 else gotoghyll1 true (clamp (-conf.scrollstep))
4976 | Some n ->
4977 setautoscrollspeed n false
4980 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
4981 begin match state.autoscroll with
4982 | None ->
4983 begin match state.mode with
4984 | Birdseye beye -> downbirdseye 1 beye
4985 | _ ->
4986 if ctrl
4987 then gotoy_and_clear_text (clamp (state.winh/2))
4988 else (
4989 if not (Wsi.withshift mask) && conf.presentation
4990 then nextpage ()
4991 else gotoghyll1 true (clamp (conf.scrollstep))
4994 | Some n ->
4995 setautoscrollspeed n true
4998 | 0xff51 | 0xff53 | 0xff96 | 0xff98
4999 when not (Wsi.withalt mask) -> (* (kp) left / right *)
5000 if canpan ()
5001 then
5002 let dx =
5003 if ctrl
5004 then state.winw / 2
5005 else conf.hscrollstep
5007 let dx = if key = 0xff51 || key = 0xff96 then dx else -dx in
5008 state.x <- panbound (state.x + dx);
5009 gotoy_and_clear_text state.y
5010 else (
5011 state.text <- E.s;
5012 G.postRedisplay "left/right"
5015 | 0xff55 | 0xff9a -> (* (kp) prior *)
5016 let y =
5017 if ctrl
5018 then
5019 match state.layout with
5020 | [] -> state.y
5021 | l :: _ -> state.y - l.pagey
5022 else
5023 clamp (pgscale (-state.winh))
5025 gotoghyll y
5027 | 0xff56 | 0xff9b -> (* (kp) next *)
5028 let y =
5029 if ctrl
5030 then
5031 match List.rev state.layout with
5032 | [] -> state.y
5033 | l :: _ -> getpagey l.pageno
5034 else
5035 clamp (pgscale state.winh)
5037 gotoghyll y
5039 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5040 gotoghyll 0
5041 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
5042 gotoghyll (clamp state.maxy)
5044 | 0xff53 | 0xff98
5045 when Wsi.withalt mask -> (* alt-(kp) right *)
5046 gotoghyll (getnav 1)
5047 | 0xff51 | 0xff96
5048 when Wsi.withalt mask -> (* alt-(kp) left *)
5049 gotoghyll (getnav ~-1)
5051 | 114 -> (* r *)
5052 reload ()
5054 | 118 when conf.debug -> (* v *)
5055 state.rects <- [];
5056 List.iter (fun l ->
5057 match getopaque l.pageno with
5058 | None -> ()
5059 | Some opaque ->
5060 let x0, y0, x1, y1 = pagebbox opaque in
5061 let a,b = float x0, float y0 in
5062 let c,d = float x1, float y0 in
5063 let e,f = float x1, float y1 in
5064 let h,j = float x0, float y1 in
5065 let rect = (a,b,c,d,e,f,h,j) in
5066 debugrect rect;
5067 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5068 ) state.layout;
5069 G.postRedisplay "v";
5071 | 124 -> (* | *)
5072 let mode = state.mode in
5073 let cmd = ref E.s in
5074 let onleave = function
5075 | Cancel -> state.mode <- mode
5076 | Confirm ->
5077 List.iter (fun l ->
5078 match getopaque l.pageno with
5079 | Some opaque -> pipesel opaque !cmd
5080 | None -> ()) state.layout;
5081 state.mode <- mode
5083 let ondone s =
5084 cbput state.hists.sel s;
5085 cmd := s
5087 let te =
5088 "| ", !cmd, Some (onhist state.hists.sel), textentry, ondone, true
5090 G.postRedisplay "|";
5091 state.mode <- Textentry (te, onleave);
5093 | _ ->
5094 vlog "huh? %s" (Wsi.keyname key)
5097 let linknavkeyboard key mask linknav =
5098 let getpage pageno =
5099 let rec loop = function
5100 | [] -> None
5101 | l :: _ when l.pageno = pageno -> Some l
5102 | _ :: rest -> loop rest
5103 in loop state.layout
5105 let doexact (pageno, n) =
5106 match getopaque pageno, getpage pageno with
5107 | Some opaque, Some l ->
5108 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5109 then
5110 let under = getlink opaque n in
5111 G.postRedisplay "link gotounder";
5112 gotounder under;
5113 state.mode <- View;
5114 else
5115 let opt, dir =
5116 match key with
5117 | 0xff50 -> (* home *)
5118 Some (findlink opaque LDfirst), -1
5120 | 0xff57 -> (* end *)
5121 Some (findlink opaque LDlast), 1
5123 | 0xff51 -> (* left *)
5124 Some (findlink opaque (LDleft n)), -1
5126 | 0xff53 -> (* right *)
5127 Some (findlink opaque (LDright n)), 1
5129 | 0xff52 -> (* up *)
5130 Some (findlink opaque (LDup n)), -1
5132 | 0xff54 -> (* down *)
5133 Some (findlink opaque (LDdown n)), 1
5135 | _ -> None, 0
5137 let pwl l dir =
5138 begin match findpwl l.pageno dir with
5139 | Pwlnotfound -> ()
5140 | Pwl pageno ->
5141 let notfound dir =
5142 state.mode <- LinkNav (Ltgendir dir);
5143 let y, h = getpageyh pageno in
5144 let y =
5145 if dir < 0
5146 then y + h - state.winh
5147 else y
5149 gotoy y
5151 begin match getopaque pageno, getpage pageno with
5152 | Some opaque, Some _ ->
5153 let link =
5154 let ld = if dir > 0 then LDfirst else LDlast in
5155 findlink opaque ld
5157 begin match link with
5158 | Lfound m ->
5159 showlinktype (getlink opaque m);
5160 state.mode <- LinkNav (Ltexact (pageno, m));
5161 G.postRedisplay "linknav jpage";
5162 | _ -> notfound dir
5163 end;
5164 | _ -> notfound dir
5165 end;
5166 end;
5168 begin match opt with
5169 | Some Lnotfound -> pwl l dir;
5170 | Some (Lfound m) ->
5171 if m = n
5172 then pwl l dir
5173 else (
5174 let _, y0, _, y1 = getlinkrect opaque m in
5175 if y0 < l.pagey
5176 then gotopage1 l.pageno y0
5177 else (
5178 let d = fstate.fontsize + 1 in
5179 if y1 - l.pagey > l.pagevh - d
5180 then gotopage1 l.pageno (y1 - state.winh - hscrollh () + d)
5181 else G.postRedisplay "linknav";
5183 showlinktype (getlink opaque m);
5184 state.mode <- LinkNav (Ltexact (l.pageno, m));
5187 | None -> viewkeyboard key mask
5188 end;
5189 | _ -> viewkeyboard key mask
5191 if key = 0xff63
5192 then (
5193 state.mode <- View;
5194 G.postRedisplay "leave linknav"
5196 else
5197 match linknav with
5198 | Ltgendir _ -> viewkeyboard key mask
5199 | Ltexact exact -> doexact exact
5202 let keyboard key mask =
5203 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5204 then wcmd "interrupt"
5205 else state.uioh <- state.uioh#key key mask
5208 let birdseyekeyboard key mask
5209 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5210 let incr =
5211 match conf.columns with
5212 | Csingle _ -> 1
5213 | Cmulti ((c, _, _), _) -> c
5214 | Csplit _ -> failwith "bird's eye split mode"
5216 let pgh layout = List.fold_left (fun m l -> max l.pageh m) state.winh layout in
5217 match key with
5218 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5219 let y, h = getpageyh pageno in
5220 let top = (state.winh - h) / 2 in
5221 gotoy (max 0 (y - top))
5222 | 0xff0d (* enter *)
5223 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5224 | 0xff1b -> leavebirdseye beye true (* escape *)
5225 | 0xff52 -> upbirdseye incr beye (* up *)
5226 | 0xff54 -> downbirdseye incr beye (* down *)
5227 | 0xff51 -> upbirdseye 1 beye (* left *)
5228 | 0xff53 -> downbirdseye 1 beye (* right *)
5230 | 0xff55 -> (* prior *)
5231 begin match state.layout with
5232 | l :: _ ->
5233 if l.pagey != 0
5234 then (
5235 state.mode <- Birdseye (
5236 oconf, leftx, l.pageno, hooverpageno, anchor
5238 gotopage1 l.pageno 0;
5240 else (
5241 let layout = layout (state.y-state.winh) (pgh state.layout) in
5242 match layout with
5243 | [] -> gotoy (clamp (-state.winh))
5244 | l :: _ ->
5245 state.mode <- Birdseye (
5246 oconf, leftx, l.pageno, hooverpageno, anchor
5248 gotopage1 l.pageno 0
5251 | [] -> gotoy (clamp (-state.winh))
5252 end;
5254 | 0xff56 -> (* next *)
5255 begin match List.rev state.layout with
5256 | l :: _ ->
5257 let layout = layout (state.y + (pgh state.layout)) state.winh in
5258 begin match layout with
5259 | [] ->
5260 let incr = l.pageh - l.pagevh in
5261 if incr = 0
5262 then (
5263 state.mode <-
5264 Birdseye (
5265 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5267 G.postRedisplay "birdseye pagedown";
5269 else gotoy (clamp (incr + conf.interpagespace*2));
5271 | l :: _ ->
5272 state.mode <-
5273 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5274 gotopage1 l.pageno 0;
5277 | [] -> gotoy (clamp state.winh)
5278 end;
5280 | 0xff50 -> (* home *)
5281 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5282 gotopage1 0 0
5284 | 0xff57 -> (* end *)
5285 let pageno = state.pagecount - 1 in
5286 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5287 if not (pagevisible state.layout pageno)
5288 then
5289 let h =
5290 match List.rev state.pdims with
5291 | [] -> state.winh
5292 | (_, _, h, _) :: _ -> h
5294 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5295 else G.postRedisplay "birdseye end";
5296 | _ -> viewkeyboard key mask
5299 let drawpage l =
5300 let color =
5301 match state.mode with
5302 | Textentry _ -> scalecolor 0.4
5303 | LinkNav _
5304 | View -> scalecolor 1.0
5305 | Birdseye (_, _, pageno, hooverpageno, _) ->
5306 if l.pageno = hooverpageno
5307 then scalecolor 0.9
5308 else (
5309 if l.pageno = pageno
5310 then scalecolor 1.0
5311 else scalecolor 0.8
5314 drawtiles l color;
5317 let postdrawpage l linkindexbase =
5318 match getopaque l.pageno with
5319 | Some opaque ->
5320 if tileready l l.pagex l.pagey
5321 then
5322 let x = l.pagedispx - l.pagex + xadjsb 0
5323 and y = l.pagedispy - l.pagey in
5324 let hlmask =
5325 match conf.columns with
5326 | Csingle _ | Cmulti _ ->
5327 (if conf.hlinks then 1 else 0)
5328 + (if state.glinks
5329 && not (isbirdseye state.mode) then 2 else 0)
5330 | _ -> 0
5332 let s =
5333 match state.mode with
5334 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5335 | _ -> E.s
5337 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5338 else 0
5339 | _ -> 0
5342 let scrollindicator () =
5343 let sbw, ph, sh = state.uioh#scrollph in
5344 let sbh, pw, sw = state.uioh#scrollpw in
5346 let x0,x1 =
5347 if conf.leftscroll
5348 then (0, sbw)
5349 else (state.winw - sbw), state.winw
5352 GlDraw.color (0.64, 0.64, 0.64);
5353 filledrect (float x0) 0. (float x1) (float state.winh);
5354 filledrect
5355 0. (float (state.winh - sbh))
5356 (float (wadjsb state.winw - 1)) (float state.winh)
5358 GlDraw.color (0.0, 0.0, 0.0);
5360 filledrect (float x0) ph (float x1) (ph +. sh);
5361 filledrect pw (float (state.winh - sbh)) (pw +. sw) (float state.winh);
5364 let showsel () =
5365 match state.mstate with
5366 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5369 | Msel ((x0, y0), (x1, y1)) ->
5370 let identify opaque l px py = Some (opaque, l.pageno, px, py) in
5371 let o0,n0,px0,py0 = onppundermouse identify x0 y0 (~< E.s, -1, 0, 0) in
5372 let _o1,n1,px1,py1 = onppundermouse identify x1 y1 (~< E.s, -1, 0, 0) in
5373 if n0 != -1 && n0 = n1 then seltext o0 (px0, py0, px1, py1);
5376 let showrects = function [] -> () | rects ->
5377 Gl.enable `blend;
5378 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5379 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5380 List.iter
5381 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5382 List.iter (fun l ->
5383 if l.pageno = pageno
5384 then (
5385 let dx = float (l.pagedispx - l.pagex) in
5386 let dy = float (l.pagedispy - l.pagey) in
5387 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5388 Raw.sets_float state.vraw ~pos:0
5389 [| x0+.dx; y0+.dy;
5390 x1+.dx; y1+.dy;
5391 x3+.dx; y3+.dy;
5392 x2+.dx; y2+.dy |];
5393 GlArray.vertex `two state.vraw;
5394 GlArray.draw_arrays `triangle_strip 0 4;
5396 ) state.layout
5397 ) rects
5399 Gl.disable `blend;
5402 let display () =
5403 GlClear.color (scalecolor2 conf.bgcolor);
5404 GlClear.clear [`color];
5405 List.iter drawpage state.layout;
5406 let rects =
5407 match state.mode with
5408 | LinkNav (Ltexact (pageno, linkno)) ->
5409 begin match getopaque pageno with
5410 | Some opaque ->
5411 let dx = xadjsb 0 in
5412 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5413 let x0 = x0 + dx and x1 = x1 + dx in
5414 (pageno, 5, (
5415 float x0, float y0,
5416 float x1, float y0,
5417 float x1, float y1,
5418 float x0, float y1)
5419 ) :: state.rects
5420 | None -> state.rects
5422 | _ -> state.rects
5424 showrects rects;
5425 let rec postloop linkindexbase = function
5426 | l :: rest ->
5427 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5428 postloop linkindexbase rest
5429 | [] -> ()
5431 showsel ();
5432 postloop 0 state.layout;
5433 state.uioh#display;
5434 begin match state.mstate with
5435 | Mzoomrect ((x0, y0), (x1, y1)) ->
5436 Gl.enable `blend;
5437 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5438 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5439 filledrect (float x0) (float y0) (float x1) (float y1);
5440 Gl.disable `blend;
5441 | _ -> ()
5442 end;
5443 enttext ();
5444 scrollindicator ();
5445 Wsi.swapb ();
5448 let zoomrect x y x1 y1 =
5449 let x0 = min x x1
5450 and x1 = max x x1
5451 and y0 = min y y1 in
5452 gotoy (state.y + y0);
5453 state.anchor <- getanchor ();
5454 let zoom = (float state.w) /. float (x1 - x0) in
5455 let margin =
5456 match conf.fitmodel, conf.columns with
5457 | FitPage, Csplit _ ->
5458 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
5460 | _, _ ->
5461 let adjw = wadjsb state.winw in
5462 if state.w < adjw
5463 then (adjw - state.w) / 2
5464 else 0
5466 state.x <- (state.x + margin) - x0;
5467 setzoom zoom;
5468 resetmstate ();
5471 let zoomblock x y =
5472 let g opaque l px py =
5473 match rectofblock opaque px py with
5474 | Some a ->
5475 let x0 = a.(0) -. 20. in
5476 let x1 = a.(1) +. 20. in
5477 let y0 = a.(2) -. 20. in
5478 let zoom = (float state.w) /. (x1 -. x0) in
5479 let pagey = getpagey l.pageno in
5480 gotoy_and_clear_text (pagey + truncate y0);
5481 state.anchor <- getanchor ();
5482 let margin = (state.w - l.pagew)/2 in
5483 state.x <- -truncate x0 - margin;
5484 setzoom zoom;
5485 None
5486 | None -> None
5488 match conf.columns with
5489 | Csplit _ ->
5490 showtext '!' "block zooming does not work properly in split columns mode"
5491 | _ -> onppundermouse g x y ()
5494 let scrollx x =
5495 let winw = wadjsb state.winw - 1 in
5496 let s = float x /. float winw in
5497 let destx = truncate (float (state.w + winw) *. s) in
5498 state.x <- winw - destx;
5499 gotoy_and_clear_text state.y;
5500 state.mstate <- Mscrollx;
5503 let scrolly y =
5504 let s = float y /. float state.winh in
5505 let desty = truncate (float (state.maxy - state.winh) *. s) in
5506 gotoy_and_clear_text desty;
5507 state.mstate <- Mscrolly;
5510 let viewmulticlick clicks x y mask =
5511 let g opaque l px py =
5512 let mark =
5513 match clicks with
5514 | 2 -> Mark_word
5515 | 3 -> Mark_line
5516 | 4 -> Mark_block
5517 | _ -> Mark_page
5519 if markunder opaque px py mark
5520 then (
5521 Some (fun () ->
5522 let dopipe cmd =
5523 match getopaque l.pageno with
5524 | None -> ()
5525 | Some opaque -> pipesel opaque cmd
5527 state.roam <- (fun () -> dopipe conf.paxcmd);
5528 if not (Wsi.withctrl mask) then dopipe conf.selcmd;
5531 else None
5533 G.postRedisplay "viewmulticlick";
5534 onppundermouse g x y (fun () -> showtext '!' "Nothing to select") ();
5537 let canselect () =
5538 match conf.columns with
5539 | Csplit _ -> false
5540 | Csingle _ | Cmulti _ -> conf.angle mod 360 = 0
5543 let viewmouse button down x y mask =
5544 match button with
5545 | n when (n == 4 || n == 5) && not down ->
5546 if Wsi.withctrl mask
5547 then (
5548 match state.mstate with
5549 | Mzoom (oldn, i) ->
5550 if oldn = n
5551 then (
5552 if i = 2
5553 then
5554 let incr =
5555 match n with
5556 | 5 ->
5557 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5558 | _ ->
5559 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5561 let zoom = conf.zoom -. incr in
5562 setzoom zoom;
5563 state.mstate <- Mzoom (n, 0);
5564 else
5565 state.mstate <- Mzoom (n, i+1);
5567 else state.mstate <- Mzoom (n, 0)
5569 | _ -> state.mstate <- Mzoom (n, 0)
5571 else (
5572 match state.autoscroll with
5573 | Some step -> setautoscrollspeed step (n=4)
5574 | None ->
5575 if conf.wheelbypage || conf.presentation
5576 then (
5577 if n = 4
5578 then prevpage ()
5579 else nextpage ()
5581 else
5582 let incr =
5583 if n = 4
5584 then -conf.scrollstep
5585 else conf.scrollstep
5587 let incr = incr * 2 in
5588 let y = clamp incr in
5589 gotoy_and_clear_text y
5592 | n when (n = 6 || n = 7) && not down && canpan () ->
5593 state.x <-
5594 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
5595 gotoy_and_clear_text state.y
5597 | 1 when Wsi.withshift mask ->
5598 state.mstate <- Mnone;
5599 if not down
5600 then (
5601 match unproject x y with
5602 | Some (pageno, ux, uy) ->
5603 let cmd = Printf.sprintf
5604 "%s %s %d %d %d"
5605 conf.stcmd state.path pageno ux uy
5607 popen cmd []
5608 | None -> ()
5611 | 1 when Wsi.withctrl mask ->
5612 if down
5613 then (
5614 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5615 state.mstate <- Mpan (x, y)
5617 else
5618 state.mstate <- Mnone
5620 | 3 ->
5621 if down
5622 then (
5623 Wsi.setcursor Wsi.CURSOR_CYCLE;
5624 let p = (x, y) in
5625 state.mstate <- Mzoomrect (p, p)
5627 else (
5628 match state.mstate with
5629 | Mzoomrect ((x0, y0), _) ->
5630 if abs (x-x0) > 10 && abs (y - y0) > 10
5631 then zoomrect x0 y0 x y
5632 else (
5633 resetmstate ();
5634 G.postRedisplay "kill accidental zoom rect";
5636 | _ ->
5637 resetmstate ()
5640 | 1 when x > state.winw - vscrollw () ->
5641 if down
5642 then
5643 let _, position, sh = state.uioh#scrollph in
5644 if y > truncate position && y < truncate (position +. sh)
5645 then state.mstate <- Mscrolly
5646 else scrolly y
5647 else
5648 state.mstate <- Mnone
5650 | 1 when y > state.winh - hscrollh () ->
5651 if down
5652 then
5653 let _, position, sw = state.uioh#scrollpw in
5654 if x > truncate position && x < truncate (position +. sw)
5655 then state.mstate <- Mscrollx
5656 else scrollx x
5657 else
5658 state.mstate <- Mnone
5660 | 1 when state.bzoom -> if not down then zoomblock x y
5662 | 1 ->
5663 let dest = if down then getunder x y else Unone in
5664 begin match dest with
5665 | Ulinkgoto _
5666 | Ulinkuri _
5667 | Uremote _ | Uremotedest _
5668 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5669 gotounder dest
5671 | Unone when down ->
5672 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5673 state.mstate <- Mpan (x, y);
5675 | Unone | Utext _ ->
5676 if down
5677 then (
5678 if canselect ()
5679 then (
5680 state.mstate <- Msel ((x, y), (x, y));
5681 G.postRedisplay "mouse select";
5684 else (
5685 match state.mstate with
5686 | Mnone -> ()
5688 | Mzoom _ | Mscrollx | Mscrolly ->
5689 state.mstate <- Mnone
5691 | Mzoomrect ((x0, y0), _) ->
5692 zoomrect x0 y0 x y
5694 | Mpan _ ->
5695 Wsi.setcursor Wsi.CURSOR_INHERIT;
5696 state.mstate <- Mnone
5698 | Msel ((x0, y0), (x1, y1)) ->
5699 let rec loop = function
5700 | [] -> ()
5701 | l :: rest ->
5702 let inside =
5703 let a0 = l.pagedispy in
5704 let a1 = a0 + l.pagevh in
5705 let b0 = l.pagedispx in
5706 let b1 = b0 + l.pagevw in
5707 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5708 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5710 if inside
5711 then
5712 match getopaque l.pageno with
5713 | Some opaque ->
5714 let dosel cmd () =
5715 match Ne.res Unix.pipe with
5716 | Ne.Exn exn ->
5717 showtext '!'
5718 (Printf.sprintf
5719 "can not create sel pipe: %s"
5720 (exntos exn));
5721 | Ne.Res (r, w) ->
5722 let clo what fd =
5723 Ne.clo fd (fun msg ->
5724 dolog "%s close failed: %s" what msg)
5726 let popened =
5727 try popen cmd [r, 0; w, -1]; true
5728 with exn ->
5729 dolog "can not execute %S: %s"
5730 cmd (exntos exn);
5731 false
5733 if popened
5734 then (
5735 copysel w opaque;
5736 G.postRedisplay "copysel";
5738 else clo "Msel pipe/w" w;
5739 clo "Msel pipe/r" r;
5741 dosel conf.selcmd ();
5742 state.roam <- dosel conf.paxcmd;
5743 | None -> ()
5744 else loop rest
5746 loop state.layout;
5747 resetmstate ();
5751 | _ -> ()
5754 let birdseyemouse button down x y mask
5755 (conf, leftx, _, hooverpageno, anchor) =
5756 match button with
5757 | 1 when down ->
5758 let rec loop = function
5759 | [] -> ()
5760 | l :: rest ->
5761 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5762 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5763 then (
5764 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5766 else loop rest
5768 loop state.layout
5769 | 3 -> ()
5770 | _ -> viewmouse button down x y mask
5773 let uioh = object
5774 method display = ()
5776 method key key mask =
5777 begin match state.mode with
5778 | Textentry textentry -> textentrykeyboard key mask textentry
5779 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5780 | View -> viewkeyboard key mask
5781 | LinkNav linknav -> linknavkeyboard key mask linknav
5782 end;
5783 state.uioh
5785 method button button bstate x y mask =
5786 begin match state.mode with
5787 | LinkNav _
5788 | View -> viewmouse button bstate x y mask
5789 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5790 | Textentry _ -> ()
5791 end;
5792 state.uioh
5794 method multiclick clicks x y mask =
5795 begin match state.mode with
5796 | LinkNav _
5797 | View -> viewmulticlick clicks x y mask
5798 | Birdseye _
5799 | Textentry _ -> ()
5800 end;
5801 state.uioh
5803 method motion x y =
5804 begin match state.mode with
5805 | Textentry _ -> ()
5806 | View | Birdseye _ | LinkNav _ ->
5807 match state.mstate with
5808 | Mzoom _ | Mnone -> ()
5810 | Mpan (x0, y0) ->
5811 let dx = x - x0
5812 and dy = y0 - y in
5813 state.mstate <- Mpan (x, y);
5814 if canpan ()
5815 then state.x <- panbound (state.x + dx);
5816 let y = clamp dy in
5817 gotoy_and_clear_text y
5819 | Msel (a, _) ->
5820 state.mstate <- Msel (a, (x, y));
5821 G.postRedisplay "motion select";
5823 | Mscrolly ->
5824 let y = min state.winh (max 0 y) in
5825 scrolly y
5827 | Mscrollx ->
5828 let x = min state.winw (max 0 x) in
5829 scrollx x
5831 | Mzoomrect (p0, _) ->
5832 state.mstate <- Mzoomrect (p0, (x, y));
5833 G.postRedisplay "motion zoomrect";
5834 end;
5835 state.uioh
5837 method pmotion x y =
5838 begin match state.mode with
5839 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5840 let rec loop = function
5841 | [] ->
5842 if hooverpageno != -1
5843 then (
5844 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5845 G.postRedisplay "pmotion birdseye no hoover";
5847 | l :: rest ->
5848 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5849 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5850 then (
5851 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5852 G.postRedisplay "pmotion birdseye hoover";
5854 else loop rest
5856 loop state.layout
5858 | Textentry _ -> ()
5860 | LinkNav _
5861 | View ->
5862 match state.mstate with
5863 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5865 | Mnone ->
5866 updateunder x y;
5867 if canselect ()
5868 then
5869 match conf.pax with
5870 | None -> ()
5871 | Some r ->
5872 let past, _, _ = !r in
5873 let now = now () in
5874 let delta = now -. past in
5875 if delta > 0.01
5876 then paxunder x y
5877 else r := (now, x, y)
5878 end;
5879 state.uioh
5881 method infochanged _ = ()
5883 method scrollph =
5884 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
5885 let p, h =
5886 if maxy = 0
5887 then 0.0, float state.winh
5888 else scrollph state.y maxy
5890 vscrollw (), p, h
5892 method scrollpw =
5893 let winw = wadjsb state.winw in
5894 let fwinw = float winw in
5895 let sw =
5896 let sw = fwinw /. float state.w in
5897 let sw = fwinw *. sw in
5898 max sw (float conf.scrollh)
5900 let position =
5901 let maxx = state.w + winw in
5902 let x = winw - state.x in
5903 let percent = float x /. float maxx in
5904 (fwinw -. sw) *. percent
5906 hscrollh (), position, sw
5908 method modehash =
5909 let modename =
5910 match state.mode with
5911 | LinkNav _ -> "links"
5912 | Textentry _ -> "textentry"
5913 | Birdseye _ -> "birdseye"
5914 | View -> "view"
5916 findkeyhash conf modename
5918 method eformsgs = true
5919 end;;
5921 let adderrmsg src msg =
5922 Buffer.add_string state.errmsgs msg;
5923 state.newerrmsgs <- true;
5924 G.postRedisplay src
5927 let adderrfmt src fmt =
5928 Format.kprintf (fun s -> adderrmsg src s) fmt;
5931 let ract cmds =
5932 let cl = splitatspace cmds in
5933 let scan s fmt f =
5934 try Scanf.sscanf s fmt f
5935 with exn ->
5936 adderrfmt "remote exec"
5937 "error processing '%S': %s\n" cmds (exntos exn)
5939 match cl with
5940 | "reload" :: [] -> reload ()
5941 | "goto" :: args :: [] ->
5942 scan args "%u %f %f"
5943 (fun pageno x y ->
5944 let cmd, _ = state.geomcmds in
5945 if emptystr cmd
5946 then gotopagexy pageno x y
5947 else
5948 let f prevf () =
5949 gotopagexy pageno x y;
5950 prevf ()
5952 state.reprf <- f state.reprf
5954 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
5955 | "gotor" :: args :: [] ->
5956 scan args "%S %u"
5957 (fun filename pageno -> gotounder (Uremote (filename, pageno)))
5958 | "gotord" :: args :: [] ->
5959 scan args "%S %S"
5960 (fun filename dest -> gotounder (Uremotedest (filename, dest)))
5961 | "rect" :: args :: [] ->
5962 scan args "%u %u %f %f %f %f"
5963 (fun pageno color x0 y0 x1 y1 ->
5964 onpagerect pageno (fun w h ->
5965 let _,w1,h1,_ = getpagedim pageno in
5966 let sw = float w1 /. float w
5967 and sh = float h1 /. float h in
5968 let x0s = x0 *. sw
5969 and x1s = x1 *. sw
5970 and y0s = y0 *. sh
5971 and y1s = y1 *. sh in
5972 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
5973 debugrect rect;
5974 state.rects <- (pageno, color, rect) :: state.rects;
5975 G.postRedisplay "rect";
5978 | "activatewin" :: [] -> Wsi.activatewin ()
5979 | "quit" :: [] -> raise Quit
5980 | _ ->
5981 adderrfmt "remote command"
5982 "error processing remote command: %S\n" cmds;
5985 let remote =
5986 let scratch = String.create 80 in
5987 let buf = Buffer.create 80 in
5988 fun fd ->
5989 let rec tempfr () =
5990 try Some (Unix.read fd scratch 0 80)
5991 with
5992 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
5993 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
5994 | exn -> raise exn
5996 match tempfr () with
5997 | None -> Some fd
5998 | Some n ->
5999 if n = 0
6000 then (
6001 Unix.close fd;
6002 if Buffer.length buf > 0
6003 then (
6004 let s = Buffer.contents buf in
6005 Buffer.clear buf;
6006 ract s;
6008 None
6010 else
6011 let rec eat ppos =
6012 let nlpos =
6014 let pos = String.index_from scratch ppos '\n' in
6015 if pos >= n then -1 else pos
6016 with Not_found -> -1
6018 if nlpos >= 0
6019 then (
6020 Buffer.add_substring buf scratch ppos (nlpos-ppos);
6021 let s = Buffer.contents buf in
6022 Buffer.clear buf;
6023 ract s;
6024 eat (nlpos+1);
6026 else (
6027 Buffer.add_substring buf scratch ppos (n-ppos);
6028 Some fd
6030 in eat 0
6033 let remoteopen path =
6034 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
6035 with exn ->
6036 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
6037 None
6040 let () =
6041 let trimcachepath = ref E.s in
6042 let rcmdpath = ref E.s in
6043 let pageno = ref None in
6044 selfexec := Sys.executable_name;
6045 Arg.parse
6046 (Arg.align
6047 [("-p", Arg.String (fun s -> state.password <- s),
6048 "<password> Set password");
6050 ("-f", Arg.String
6051 (fun s ->
6052 Config.fontpath := s;
6053 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
6055 "<path> Set path to the user interface font");
6057 ("-c", Arg.String
6058 (fun s ->
6059 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
6060 Config.confpath := s),
6061 "<path> Set path to the configuration file");
6063 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
6064 "<page-number> Jump to page");
6066 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6067 "<path> Set path to the trim cache file");
6069 ("-dest", Arg.String (fun s -> state.nameddest <- s),
6070 "<named-destination> Set named destination");
6072 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
6073 ("-cxack", Arg.Set cxack, " Cut corners");
6075 ("-remote", Arg.String (fun s -> rcmdpath := s),
6076 "<path> Set path to the remote commands source");
6078 ("-origin", Arg.String (fun s -> state.origin <- s),
6079 "<original-path> Set original path");
6081 ("-v", Arg.Unit (fun () ->
6082 Printf.printf
6083 "%s\nconfiguration path: %s\n"
6084 (version ())
6085 Config.defconfpath
6087 exit 0), " Print version and exit");
6090 (fun s -> state.path <- s)
6091 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6093 if !wtmode
6094 then selfexec := !selfexec ^ " -wtmode";
6096 let histmode = emptystr state.path in
6098 if not (Config.load ())
6099 then prerr_endline "failed to load configuration";
6100 begin match !pageno with
6101 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
6102 | None -> ()
6103 end;
6105 let wsfd, winw, winh = Wsi.init (object (self)
6106 val mutable m_hack = false
6107 val mutable m_clicks = 0
6108 val mutable m_click_x = 0
6109 val mutable m_click_y = 0
6110 val mutable m_lastclicktime = infinity
6112 method private cleanup =
6113 state.roam <- noroam;
6114 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap;
6115 method expose = if not m_hack then G.postRedisplay "expose"
6116 method visible = G.postRedisplay "visible"
6117 method display = m_hack <- false; display ()
6118 method reshape w h =
6119 self#cleanup;
6120 m_hack <- w < state.winw && h < state.winh;
6121 reshape w h
6122 method mouse b d x y m =
6123 if d && canselect ()
6124 then (
6125 (* http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx *)
6126 m_click_x <- x;
6127 m_click_y <- y;
6128 if b = 1
6129 then (
6130 let t = now () in
6131 if abs x - m_click_x > 10
6132 || abs y - m_click_y > 10
6133 || abs_float (t -. m_lastclicktime) > 0.3
6134 then m_clicks <- 0;
6135 m_clicks <- m_clicks + 1;
6136 m_lastclicktime <- t;
6137 if m_clicks = 1
6138 then (
6139 self#cleanup;
6140 G.postRedisplay "cleanup";
6141 state.uioh <- state.uioh#button b d x y m;
6143 else state.uioh <- state.uioh#multiclick m_clicks x y m
6145 else (
6146 self#cleanup;
6147 m_clicks <- 0;
6148 m_lastclicktime <- infinity;
6149 state.uioh <- state.uioh#button b d x y m
6152 else (
6153 state.uioh <- state.uioh#button b d x y m
6155 method motion x y =
6156 state.mpos <- (x, y);
6157 state.uioh <- state.uioh#motion x y
6158 method pmotion x y =
6159 state.mpos <- (x, y);
6160 state.uioh <- state.uioh#pmotion x y
6161 method key k m =
6162 let mascm = m land (
6163 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6164 ) in
6165 let keyboard k m =
6166 let x = state.x and y = state.y in
6167 keyboard k m;
6168 if x != state.x || y != state.y then self#cleanup
6170 match state.keystate with
6171 | KSnone ->
6172 let km = k, mascm in
6173 begin
6174 match
6175 let modehash = state.uioh#modehash in
6176 try Hashtbl.find modehash km
6177 with Not_found ->
6178 try Hashtbl.find (findkeyhash conf "global") km
6179 with Not_found -> KMinsrt (k, m)
6180 with
6181 | KMinsrt (k, m) -> keyboard k m
6182 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6183 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6185 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6186 List.iter (fun (k, m) -> keyboard k m) insrt;
6187 state.keystate <- KSnone
6188 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6189 state.keystate <- KSinto (keys, insrt)
6190 | _ ->
6191 state.keystate <- KSnone
6193 method enter x y =
6194 state.mpos <- (x, y);
6195 state.uioh <- state.uioh#pmotion x y
6196 method leave = state.mpos <- (-1, -1)
6197 method winstate wsl = state.winstate <- wsl; m_hack <- false
6198 method quit = raise Quit
6199 end) conf.cwinw conf.cwinh (platform = Posx) in
6201 state.wsfd <- wsfd;
6203 if not (
6204 List.exists GlMisc.check_extension
6205 [ "GL_ARB_texture_rectangle"
6206 ; "GL_EXT_texture_recangle"
6207 ; "GL_NV_texture_rectangle" ]
6209 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6211 if (
6212 let r = GlMisc.get_string `renderer in
6213 let p = "Mesa DRI Intel(" in
6214 let l = String.length p in
6215 String.length r > l && String.sub r 0 l = p
6217 then (
6218 defconf.sliceheight <- 1024;
6219 defconf.texcount <- 32;
6220 defconf.usepbo <- true;
6223 let cr, sw =
6224 match Ne.res Unix.pipe with
6225 | Ne.Exn exn ->
6226 Printf.eprintf "pipe/crsw failed: %s" (exntos exn);
6227 exit 1
6228 | Ne.Res rw -> rw
6229 and sr, cw =
6230 match Ne.res Unix.pipe with
6231 | Ne.Exn exn ->
6232 Printf.eprintf "pipe/srcw failed: %s" (exntos exn);
6233 exit 1
6234 | Ne.Res rw -> rw
6237 cloexec cr;
6238 cloexec sw;
6239 cloexec sr;
6240 cloexec cw;
6242 setcheckers conf.checkers;
6243 redirectstderr ();
6244 if conf.redirectstderr
6245 then
6246 at_exit (fun () ->
6247 let s = Buffer.contents state.errmsgs ^
6248 (match state.errfd with
6249 | Some fd ->
6250 let s = String.create (80*24) in
6251 let n =
6253 let r, _, _ = Unix.select [fd] [] [] 0.0 in
6254 if List.mem fd r
6255 then Unix.read fd s 0 (String.length s)
6256 else 0
6257 with _ -> 0
6259 if n = 0
6260 then E.s
6261 else String.sub s 0 n
6262 | None -> E.s
6265 try ignore (Unix.write state.stderr s 0 (String.length s))
6266 with exn -> print_endline (exntos exn)
6270 init (cr, cw) (
6271 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
6272 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6273 !Config.fontpath, !trimcachepath,
6274 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
6276 List.iter GlArray.enable [`texture_coord; `vertex];
6277 state.sr <- sr;
6278 state.sw <- sw;
6279 reshape winw winh;
6280 if histmode
6281 then (
6282 state.uioh <- uioh;
6283 enterhistmode ();
6285 else (
6286 state.text <- "Opening " ^ (mbtoutf8 state.path);
6287 opendoc state.path state.password;
6288 state.uioh <- uioh;
6290 display ();
6291 Wsi.mapwin ();
6292 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
6293 let optrfd =
6294 ref (
6295 if nonemptystr !rcmdpath
6296 then remoteopen !rcmdpath
6297 else None
6301 let rec loop deadline =
6302 let r =
6303 match state.errfd with
6304 | None -> [state.sr; state.wsfd]
6305 | Some fd -> [state.sr; state.wsfd; fd]
6307 let r =
6308 match !optrfd with
6309 | None -> r
6310 | Some fd -> fd :: r
6312 if state.redisplay
6313 then (
6314 state.redisplay <- false;
6315 display ();
6317 let timeout =
6318 let now = now () in
6319 if deadline > now
6320 then (
6321 if deadline = infinity
6322 then ~-.1.0
6323 else max 0.0 (deadline -. now)
6325 else 0.0
6327 let r, _, _ =
6328 try Unix.select r [] [] timeout
6329 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6331 begin match r with
6332 | [] ->
6333 state.ghyll None;
6334 let newdeadline =
6335 if state.ghyll == noghyll
6336 then
6337 match state.autoscroll with
6338 | Some step when step != 0 ->
6339 let y = state.y + step in
6340 let y =
6341 if y < 0
6342 then state.maxy
6343 else if y >= state.maxy then 0 else y
6345 gotoy y;
6346 if state.mode = View
6347 then state.text <- E.s;
6348 deadline +. 0.01
6349 | _ -> infinity
6350 else deadline +. 0.01
6352 loop newdeadline
6354 | l ->
6355 let rec checkfds = function
6356 | [] -> ()
6357 | fd :: rest when fd = state.sr ->
6358 let cmd = readcmd state.sr in
6359 act cmd;
6360 checkfds rest
6362 | fd :: rest when fd = state.wsfd ->
6363 Wsi.readresp fd;
6364 checkfds rest
6366 | fd :: rest when Some fd = !optrfd ->
6367 begin match remote fd with
6368 | None -> optrfd := remoteopen !rcmdpath;
6369 | opt -> optrfd := opt
6370 end;
6371 checkfds rest
6373 | fd :: rest ->
6374 let s = String.create 80 in
6375 let n = tempfailureretry (Unix.read fd s 0) 80 in
6376 if conf.redirectstderr
6377 then (
6378 Buffer.add_substring state.errmsgs s 0 n;
6379 state.newerrmsgs <- true;
6380 state.redisplay <- true;
6382 else (
6383 prerr_string (String.sub s 0 n);
6384 flush stderr;
6386 checkfds rest
6388 checkfds l;
6389 if !reeenterhist then (
6390 enterhistmode ();
6391 reeenterhist := false;
6393 let newdeadline =
6394 let deadline1 =
6395 if deadline = infinity
6396 then now () +. 0.01
6397 else deadline
6399 match state.autoscroll with
6400 | Some step when step != 0 -> deadline1
6401 | _ -> if state.ghyll == noghyll then infinity else deadline1
6403 loop newdeadline
6404 end;
6407 loop infinity;
6408 with Quit ->
6409 Config.save leavebirdseye;