Compile C with some optimizations
[llpp.git] / main.ml
blob42ce9185988e99444d6b4ab0d01684e7f3c0e179
1 open Utils;;
2 open Config;;
4 exception Quit;;
6 external init : Unix.file_descr -> params -> unit = "ml_init";;
7 external seltext : opaque -> (int * int * int * int) -> unit = "ml_seltext";;
8 external hassel : opaque -> bool = "ml_hassel";;
9 external copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
10 external getpdimrect : int -> float array = "ml_getpdimrect";;
11 external whatsunder : opaque -> int -> int -> under = "ml_whatsunder";;
12 external markunder : opaque -> int -> int -> mark -> bool = "ml_markunder";;
13 external clearmark : opaque -> unit = "ml_clearmark";;
14 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
15 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
16 external measurestr : int -> string -> float = "ml_measure_string";;
17 external postprocess :
18 opaque -> int -> int -> int -> (int * string * int) -> int
19 = "ml_postprocess";;
20 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
21 external setaalevel : int -> unit = "ml_setaalevel";;
22 external realloctexts : int -> bool = "ml_realloctexts";;
23 external findlink : opaque -> linkdir -> link = "ml_findlink";;
24 external getlink : opaque -> int -> under = "ml_getlink";;
25 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
26 external getlinkcount : opaque -> int = "ml_getlinkcount";;
27 external findpwl : int -> int -> pagewithlinks = "ml_find_page_with_links";;
28 external getpbo : width -> height -> colorspace -> opaque = "ml_getpbo";;
29 external freepbo : opaque -> unit = "ml_freepbo";;
30 external unmappbo : opaque -> unit = "ml_unmappbo";;
31 external pbousable : unit -> bool = "ml_pbo_usable";;
32 external unproject : opaque -> int -> int -> (int * int) option
33 = "ml_unproject";;
34 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
35 external rectofblock : opaque -> int -> int -> float array option
36 = "ml_rectofblock";;
37 external begintiles : unit -> unit = "ml_begintiles";;
38 external endtiles : unit -> unit = "ml_endtiles";;
40 let reeenterhist = ref false;;
41 let selfexec = ref E.s;;
43 let drawstring size x y s =
44 Gl.enable `blend;
45 Gl.enable `texture_2d;
46 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
47 ignore (drawstr size x y s);
48 Gl.disable `blend;
49 Gl.disable `texture_2d;
52 let drawstring1 size x y s =
53 drawstr size x y s;
56 let drawstring2 size x y fmt =
57 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
60 let debugl l =
61 dolog "l %d dim=%d {" l.pageno l.pagedimno;
62 dolog " WxH %dx%d" l.pagew l.pageh;
63 dolog " vWxH %dx%d" l.pagevw l.pagevh;
64 dolog " pagex,y %d,%d" l.pagex l.pagey;
65 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
66 dolog " column %d" l.pagecol;
67 dolog "}";
70 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
71 dolog "rect {";
72 dolog " x0,y0=(% f, % f)" x0 y0;
73 dolog " x1,y1=(% f, % f)" x1 y1;
74 dolog " x2,y2=(% f, % f)" x2 y2;
75 dolog " x3,y3=(% f, % f)" x3 y3;
76 dolog "}";
79 let isbirdseye = function
80 | Birdseye _ -> true
81 | Textentry _
82 | View
83 | LinkNav _ -> false
86 let istextentry = function
87 | Textentry _ -> true
88 | Birdseye _
89 | View
90 | LinkNav _ -> false
93 let wtmode = ref false;;
94 let cxack = ref false;;
96 let pgscale h = truncate (float h *. conf.pgscale);;
98 let hscrollh () =
99 if (conf.scrollb land scrollbhv = 0)
100 || (state.x = 0 && state.w <= state.winw - conf.scrollbw)
101 then 0
102 else conf.scrollbw
105 let vscrollw () =
106 if (conf.scrollb land scrollbvv = 0)
107 then 0
108 else conf.scrollbw
111 let wadjsb w = w - vscrollw ();;
112 let xadjsb x = if conf.leftscroll then x + vscrollw () else x;;
114 let setfontsize n =
115 fstate.fontsize <- n;
116 fstate.wwidth <- measurestr fstate.fontsize "w";
117 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
120 let vlog fmt =
121 if conf.verbose
122 then
123 Printf.kprintf prerr_endline fmt
124 else
125 Printf.kprintf ignore fmt
128 let launchpath () =
129 if emptystr conf.pathlauncher
130 then print_endline state.path
131 else (
132 let re = Str.regexp "%s" in
133 let command = Str.global_replace re state.path conf.pathlauncher in
134 try popen command []
135 with exn ->
136 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
137 flush stderr;
141 let redirectstderr () =
142 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
143 if conf.redirectstderr
144 then
145 match Unix.pipe () with
146 | exception exn ->
147 dolog "failed to create stderr redirection pipes: %s" (exntos exn)
149 | (r, w) ->
150 begin match Unix.dup Unix.stderr with
151 | exception exn ->
152 dolog "failed to dup stderr: %s" (exntos exn);
153 Ne.clo r (clofail "pipe/r");
154 Ne.clo w (clofail "pipe/w");
156 | dupstderr ->
157 begin match Unix.dup2 w Unix.stderr with
158 | exception exn ->
159 dolog "failed to dup2 to stderr: %s" (exntos exn);
160 Ne.clo dupstderr (clofail "stderr duplicate");
161 Ne.clo r (clofail "redir pipe/r");
162 Ne.clo w (clofail "redir pipe/w");
164 | () ->
165 state.stderr <- dupstderr;
166 state.errfd <- Some r;
167 end;
169 else (
170 state.newerrmsgs <- false;
171 begin match state.errfd with
172 | Some fd ->
173 begin match Unix.dup2 state.stderr Unix.stderr with
174 | exception exn ->
175 dolog "failed to dup2 original stderr: %s" (exntos exn)
176 | () ->
177 Ne.clo fd (clofail "dup of stderr");
178 state.errfd <- None;
179 end;
180 | None -> ()
181 end;
182 prerr_string (Buffer.contents state.errmsgs);
183 flush stderr;
184 Buffer.clear state.errmsgs;
188 module G =
189 struct
190 let postRedisplay who =
191 if conf.verbose
192 then prerr_endline ("redisplay for " ^ who);
193 state.redisplay <- true;
195 end;;
197 let getopaque pageno =
198 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
199 with Not_found -> None
202 let putopaque pageno opaque =
203 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
206 let pagetranslatepoint l x y =
207 let dy = y - l.pagedispy in
208 let y = dy + l.pagey in
209 let dx = x - l.pagedispx in
210 let x = dx + l.pagex in
211 (x, y);
214 let onppundermouse g x y d =
215 let rec f = function
216 | l :: rest ->
217 begin match getopaque l.pageno with
218 | Some opaque ->
219 let x0 = l.pagedispx in
220 let x1 = x0 + l.pagevw in
221 let y0 = l.pagedispy in
222 let y1 = y0 + l.pagevh in
223 if y >= y0 && y <= y1 && x >= x0 && x <= x1
224 then
225 let px, py = pagetranslatepoint l x y in
226 match g opaque l px py with
227 | Some res -> res
228 | None -> f rest
229 else f rest
230 | _ ->
231 f rest
233 | [] -> d
235 f state.layout
238 let getunder x y =
239 let g opaque l px py =
240 if state.bzoom
241 then (
242 match rectofblock opaque px py with
243 | Some a ->
244 let rect = (a.(0),a.(2),a.(1),a.(2),a.(1),a.(3),a.(0),a.(3)) in
245 state.rects <- [l.pageno, l.pageno mod 3, rect];
246 G.postRedisplay "getunder";
247 | None -> ()
249 let under = whatsunder opaque px py in
250 match under with
251 | Unone -> None
252 | Ulinkuri _
253 | Ulinkgoto _
254 | Utext _
255 | Uunexpected _
256 | Ulaunch _
257 | Unamed _
258 | Uremote _
259 | Uremotedest _ -> Some under
261 onppundermouse g x y Unone
264 let unproject x y =
265 let g opaque l x y =
266 match unproject opaque x y with
267 | Some (x, y) -> Some (Some (l.pageno, x, y))
268 | None -> None
270 onppundermouse g x y None;
273 let showtext c s =
274 state.text <- Printf.sprintf "%c%s" c s;
275 G.postRedisplay "showtext";
278 let pipesel opaque cmd =
279 if hassel opaque
280 then
281 match Unix.pipe () with
282 | exception exn ->
283 showtext '!'
284 (Printf.sprintf "pipesel can not create pipe: %s" (exntos exn));
285 | (r, w) ->
286 let doclose what fd =
287 Ne.clo fd (fun msg -> dolog "%s close failed: %s" what msg)
289 let popened =
290 try popen cmd [r, 0; w, -1]; true
291 with exn ->
292 dolog "can not execute %S: %s" cmd (exntos exn);
293 false
295 if popened
296 then (
297 copysel w opaque;
298 G.postRedisplay "pipesel";
300 else doclose "pipesel pipe/w" w;
301 doclose "pipesel pipe/r" r;
304 let paxunder x y =
305 let g opaque l px py =
306 if markunder opaque px py conf.paxmark
307 then (
308 Some (fun () ->
309 match getopaque l.pageno with
310 | None -> ()
311 | Some opaque -> pipesel opaque conf.paxcmd
314 else None
316 G.postRedisplay "paxunder";
317 if conf.paxmark = Mark_page
318 then
319 List.iter (fun l ->
320 match getopaque l.pageno with
321 | None -> ()
322 | Some opaque -> clearmark opaque) state.layout;
323 state.roam <-
324 onppundermouse g x y (fun () -> showtext '!' "Whoopsie daisy");
327 let selstring s =
328 match Unix.pipe () with
329 | exception exn ->
330 showtext '!' (Printf.sprintf "pipe failed: %s" (exntos exn))
331 | (r, w) ->
332 let clo cap fd =
333 Ne.clo fd (fun msg ->
334 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
337 let popened =
338 try popen conf.selcmd [r, 0; w, -1]; true
339 with exn ->
340 showtext '!'
341 (Printf.sprintf "failed to execute %s: %s"
342 conf.selcmd (exntos exn));
343 false
345 if popened
346 then (
348 let l = String.length s in
349 let bytes = Bytes.unsafe_of_string s in
350 let n = tempfailureretry (Unix.write w bytes 0) l in
351 if n != l
352 then
353 showtext '!'
354 (Printf.sprintf
355 "failed to write %d characters to sel pipe, wrote %d"
358 with exn ->
359 showtext '!'
360 (Printf.sprintf "failed to write to sel pipe: %s"
361 (exntos exn)
364 else dolog "%s" s;
365 clo "selstring pipe/r" r;
366 clo "selstring pipe/w" w;
369 let undertext = function
370 | Unone -> "none"
371 | Ulinkuri s -> s
372 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
373 | Utext s -> "font: " ^ s
374 | Uunexpected s -> "unexpected: " ^ s
375 | Ulaunch s -> "launch: " ^ s
376 | Unamed s -> "named: " ^ s
377 | Uremote (filename, pageno) ->
378 Printf.sprintf "%s: page %d" filename (pageno+1)
379 | Uremotedest (filename, destname) ->
380 Printf.sprintf "%s: destination %S" filename destname
383 let updateunder x y =
384 match getunder x y with
385 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
386 | Ulinkuri uri ->
387 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
388 Wsi.setcursor Wsi.CURSOR_INFO
389 | Ulinkgoto (pageno, _) ->
390 if conf.underinfo
391 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
392 Wsi.setcursor Wsi.CURSOR_INFO
393 | Utext s ->
394 if conf.underinfo then showtext 'f' ("ont: " ^ s);
395 Wsi.setcursor Wsi.CURSOR_TEXT
396 | Uunexpected s ->
397 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
398 Wsi.setcursor Wsi.CURSOR_INHERIT
399 | Ulaunch s ->
400 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
401 Wsi.setcursor Wsi.CURSOR_INHERIT
402 | Unamed s ->
403 if conf.underinfo then showtext 'n' ("amed: " ^ s);
404 Wsi.setcursor Wsi.CURSOR_INHERIT
405 | Uremote (filename, pageno) ->
406 if conf.underinfo then showtext 'r'
407 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
408 Wsi.setcursor Wsi.CURSOR_INFO
409 | Uremotedest (filename, destname) ->
410 if conf.underinfo then showtext 'r'
411 (Printf.sprintf "emote destination: %s (%S)" filename destname);
412 Wsi.setcursor Wsi.CURSOR_INFO
415 let showlinktype under =
416 if conf.underinfo
417 then
418 match under with
419 | Unone -> ()
420 | Ulinkuri _
421 | Ulinkgoto _
422 | Utext _
423 | Uunexpected _
424 | Ulaunch _
425 | Unamed _
426 | Uremote _
427 | Uremotedest _ ->
428 let s = undertext under in
429 showtext ' ' s
432 let addchar s c =
433 let b = Buffer.create (String.length s + 1) in
434 Buffer.add_string b s;
435 Buffer.add_char b c;
436 Buffer.contents b;
439 let intentry_with_suffix text key =
440 let c =
441 if key >= 32 && key < 127
442 then Char.chr key
443 else '\000'
445 match Char.lowercase c with
446 | '0' .. '9' ->
447 let text = addchar text c in
448 TEcont text
450 | 'k' | 'm' | 'g' ->
451 let text = addchar text c in
452 TEcont text
454 | _ ->
455 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
456 TEcont text
459 let readcmd fd =
460 let s = Bytes.create 4 in
461 let n = tempfailureretry (Unix.read fd s 0) 4 in
462 if n != 4 then error "incomplete read(len) = %d" n;
463 let len = (Char.code (Bytes.get s 0) lsl 24)
464 lor (Char.code (Bytes.get s 1) lsl 16)
465 lor (Char.code (Bytes.get s 2) lsl 8)
466 lor (Char.code (Bytes.get s 3))
468 let s = Bytes.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;
471 Bytes.to_string s
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.to_bytes b in
482 let n = Bytes.length s in
483 let len = n - 4 in
484 (* dolog "wcmd %S" (String.sub s 4 len); *)
485 Bytes.set s 0 (Char.chr ((len lsr 24) land 0xff));
486 Bytes.set s 1 (Char.chr ((len lsr 16) land 0xff));
487 Bytes.set s 2 (Char.chr ((len lsr 8) land 0xff));
488 Bytes.set s 3 (Char.chr (len land 0xff));
489 let n' = tempfailureretry (Unix.write state.ss s 0) n in
490 if n' != n then error "write failed %d vs %d" n' n;
491 ) b fmt;
494 let nogeomcmds cmds =
495 match cmds with
496 | s, [] -> emptystr s
497 | _ -> false
500 let layoutN ((columns, coverA, coverB), b) y sh =
501 let sh = sh - (hscrollh ()) in
502 let rec fold accu n =
503 if n = Array.length b
504 then accu
505 else
506 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
507 if (vy - y) > sh &&
508 (n = coverA - 1
509 || n = state.pagecount - coverB
510 || (n - coverA) mod columns = columns - 1)
511 then accu
512 else
513 let accu =
514 if vy + h > y
515 then
516 let pagey = max 0 (y - vy) in
517 let pagedispy = if pagey > 0 then 0 else vy - y in
518 let pagedispx, pagex =
519 let pdx =
520 if n = coverA - 1 || n = state.pagecount - coverB
521 then state.x + (wadjsb state.winw - w) / 2
522 else dx + xoff + state.x
524 if pdx < 0
525 then 0, -pdx
526 else pdx, 0
528 let pagevw =
529 let vw = wadjsb state.winw - pagedispx in
530 let pw = w - pagex in
531 min vw pw
533 let pagevh = min (h - pagey) (sh - pagedispy) in
534 if pagevw > 0 && pagevh > 0
535 then
536 let e =
537 { pageno = n
538 ; pagedimno = pdimno
539 ; pagew = w
540 ; pageh = h
541 ; pagex = pagex
542 ; pagey = pagey
543 ; pagevw = pagevw
544 ; pagevh = pagevh
545 ; pagedispx = pagedispx
546 ; pagedispy = pagedispy
547 ; pagecol = 0
550 e :: accu
551 else
552 accu
553 else
554 accu
556 fold accu (n+1)
558 if Array.length b = 0
559 then []
560 else List.rev (fold [] (page_of_y y))
563 let layoutS (columns, b) y sh =
564 let sh = sh - hscrollh () in
565 let rec fold accu n =
566 if n = Array.length b
567 then accu
568 else
569 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
570 if (vy - y) > sh
571 then accu
572 else
573 let accu =
574 if vy + pageh > y
575 then
576 let x = xoff + state.x in
577 let pagey = max 0 (y - vy) in
578 let pagedispy = if pagey > 0 then 0 else vy - y in
579 let pagedispx, pagex =
580 if px = 0
581 then (
582 if x < 0
583 then 0, -x
584 else x, 0
586 else (
587 let px = px - x in
588 if px < 0
589 then -px, 0
590 else 0, px
593 let pagecolw = pagew/columns in
594 let pagedispx =
595 if pagecolw < state.winw
596 then pagedispx + ((wadjsb state.winw - pagecolw) / 2)
597 else pagedispx
599 let pagevw =
600 let vw = wadjsb state.winw - pagedispx in
601 let pw = pagew - pagex in
602 min vw pw
604 let pagevw = min pagevw pagecolw in
605 let pagevh = min (pageh - pagey) (sh - pagedispy) in
606 if pagevw > 0 && pagevh > 0
607 then
608 let e =
609 { pageno = n/columns
610 ; pagedimno = pdimno
611 ; pagew = pagew
612 ; pageh = pageh
613 ; pagex = pagex
614 ; pagey = pagey
615 ; pagevw = pagevw
616 ; pagevh = pagevh
617 ; pagedispx = pagedispx
618 ; pagedispy = pagedispy
619 ; pagecol = n mod columns
622 e :: accu
623 else
624 accu
625 else
626 accu
628 fold accu (n+1)
630 List.rev (fold [] 0)
633 let layout y sh =
634 if nogeomcmds state.geomcmds
635 then
636 match conf.columns with
637 | Csingle b -> layoutN ((1, 0, 0), b) y sh
638 | Cmulti c -> layoutN c y sh
639 | Csplit s -> layoutS s y sh
640 else []
643 let clamp incr =
644 let y = state.y + incr in
645 let y = max 0 y in
646 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
650 let itertiles l f =
651 let tilex = l.pagex mod conf.tilew in
652 let tiley = l.pagey mod conf.tileh in
654 let col = l.pagex / conf.tilew in
655 let row = l.pagey / conf.tileh in
657 let rec rowloop row y0 dispy h =
658 if h = 0
659 then ()
660 else (
661 let dh = conf.tileh - y0 in
662 let dh = min h dh in
663 let rec colloop col x0 dispx w =
664 if w = 0
665 then ()
666 else (
667 let dw = conf.tilew - x0 in
668 let dw = min w dw in
669 let dispx' = xadjsb dispx in
670 f col row dispx' dispy x0 y0 dw dh;
671 colloop (col+1) 0 (dispx+dw) (w-dw)
674 colloop col tilex l.pagedispx l.pagevw;
675 rowloop (row+1) 0 (dispy+dh) (h-dh)
678 if l.pagevw > 0 && l.pagevh > 0
679 then rowloop row tiley l.pagedispy l.pagevh;
682 let gettileopaque l col row =
683 let key =
684 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
686 try Some (Hashtbl.find state.tilemap key)
687 with Not_found -> None
690 let puttileopaque l col row gen colorspace angle opaque size elapsed =
691 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
692 Hashtbl.add state.tilemap key (opaque, size, elapsed)
695 let filledrect x0 y0 x1 y1 =
696 GlArray.disable `texture_coord;
697 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
698 GlArray.vertex `two state.vraw;
699 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
700 GlArray.enable `texture_coord;
703 let linerect x0 y0 x1 y1 =
704 GlArray.disable `texture_coord;
705 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y1; x1; y0 |];
706 GlArray.vertex `two state.vraw;
707 GlArray.draw_arrays `line_loop ~first:0 ~count:4;
708 GlArray.enable `texture_coord;
711 let drawtiles l color =
712 GlDraw.color color;
713 begintiles ();
714 let f col row x y tilex tiley w h =
715 match gettileopaque l col row with
716 | Some (opaque, _, t) ->
717 let params = x, y, w, h, tilex, tiley in
718 if conf.invert
719 then GlTex.env (`mode `blend);
720 drawtile params opaque;
721 if conf.invert
722 then GlTex.env (`mode `modulate);
723 if conf.debug
724 then (
725 endtiles ();
726 let s = Printf.sprintf
727 "%d[%d,%d] %f sec"
728 l.pageno col row t
730 let w = measurestr fstate.fontsize s in
731 GlDraw.color (0.0, 0.0, 0.0);
732 filledrect (float (x-2))
733 (float (y-2))
734 (float (x+2) +. w)
735 (float (y + fstate.fontsize + 2));
736 GlDraw.color (1.0, 1.0, 1.0);
737 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
738 begintiles ();
741 | None ->
742 endtiles ();
743 let w =
744 if conf.leftscroll
745 then w
746 else
747 let lw = wadjsb state.winw - x in
748 min lw w
749 and h =
750 let lh = state.winh - y in
751 min lh h
753 if conf.invert
754 then GlTex.env (`mode `blend);
755 begin match state.checkerstexid with
756 | Some id ->
757 Gl.enable `texture_2d;
758 GlTex.bind_texture ~target:`texture_2d id;
759 let x0 = float x
760 and y0 = float y
761 and x1 = float (x+w)
762 and y1 = float (y+h) in
764 let tw = float w /. 16.0
765 and th = float h /. 16.0 in
766 let tx0 = float tilex /. 16.0
767 and ty0 = float tiley /. 16.0 in
768 let tx1 = tx0 +. tw
769 and ty1 = ty0 +. th in
770 Raw.sets_float state.vraw ~pos:0
771 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
772 Raw.sets_float state.traw ~pos:0
773 [| tx0; ty0; tx0; ty1; tx1; ty0; tx1; ty1 |];
774 GlArray.vertex `two state.vraw;
775 GlArray.tex_coord `two state.traw;
776 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
777 Gl.disable `texture_2d;
779 | None ->
780 GlDraw.color (1.0, 1.0, 1.0);
781 filledrect (float x) (float y) (float (x+w)) (float (y+h));
782 end;
783 if conf.invert
784 then GlTex.env (`mode `modulate);
785 if w > 128 && h > fstate.fontsize + 10
786 then (
787 let c = if conf.invert then 1.0 else 0.0 in
788 GlDraw.color (c, c, c);
789 let c, r =
790 if conf.verbose
791 then (col*conf.tilew, row*conf.tileh)
792 else col, row
794 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
796 GlDraw.color color;
797 begintiles ();
799 itertiles l f;
800 endtiles ();
803 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
805 let tilevisible1 l x y =
806 let ax0 = l.pagex
807 and ax1 = l.pagex + l.pagevw
808 and ay0 = l.pagey
809 and ay1 = l.pagey + l.pagevh in
811 let bx0 = x
812 and by0 = y in
813 let bx1 = min (bx0 + conf.tilew) l.pagew
814 and by1 = min (by0 + conf.tileh) l.pageh in
816 let rx0 = max ax0 bx0
817 and ry0 = max ay0 by0
818 and rx1 = min ax1 bx1
819 and ry1 = min ay1 by1 in
821 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
822 nonemptyintersection
825 let tilevisible layout n x y =
826 let rec findpageinlayout m = function
827 | l :: rest when l.pageno = n ->
828 tilevisible1 l x y || (
829 match conf.columns with
830 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
831 | Csplit _
832 | Csingle _
833 | Cmulti _ -> false
835 | _ :: rest -> findpageinlayout 0 rest
836 | [] -> false
838 findpageinlayout 0 layout;
841 let tileready l x y =
842 tilevisible1 l x y &&
843 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
846 let tilepage n p layout =
847 let rec loop = function
848 | l :: rest ->
849 if l.pageno = n
850 then
851 let f col row _ _ _ _ _ _ =
852 if state.currently = Idle
853 then
854 match gettileopaque l col row with
855 | Some _ -> ()
856 | None ->
857 let x = col*conf.tilew
858 and y = row*conf.tileh in
859 let w =
860 let w = l.pagew - x in
861 min w conf.tilew
863 let h =
864 let h = l.pageh - y in
865 min h conf.tileh
867 let pbo =
868 if conf.usepbo
869 then getpbo w h conf.colorspace
870 else ~< "0"
872 wcmd "tile %s %d %d %d %d %s"
873 (~> p) x y w h (~> pbo);
874 state.currently <-
875 Tiling (
876 l, p, conf.colorspace, conf.angle,
877 state.gen, col, row, conf.tilew, conf.tileh
880 itertiles l f;
881 else
882 loop rest
884 | [] -> ()
886 if nogeomcmds state.geomcmds
887 then loop layout;
890 let preloadlayout y =
891 let y = if y < state.winh then 0 else y - state.winh in
892 let h = state.winh*3 in
893 layout y h;
896 let load pages =
897 let rec loop pages =
898 if state.currently != Idle
899 then ()
900 else
901 match pages with
902 | l :: rest ->
903 begin match getopaque l.pageno with
904 | None ->
905 wcmd "page %d %d" l.pageno l.pagedimno;
906 state.currently <- Loading (l, state.gen);
907 | Some opaque ->
908 tilepage l.pageno opaque pages;
909 loop rest
910 end;
911 | _ -> ()
913 if nogeomcmds state.geomcmds
914 then loop pages
917 let preload pages =
918 load pages;
919 if conf.preload && state.currently = Idle
920 then load (preloadlayout state.y);
923 let layoutready layout =
924 let rec fold all ls =
925 all && match ls with
926 | l :: rest ->
927 let seen = ref false in
928 let allvisible = ref true in
929 let foo col row _ _ _ _ _ _ =
930 seen := true;
931 allvisible := !allvisible &&
932 begin match gettileopaque l col row with
933 | Some _ -> true
934 | None -> false
937 itertiles l foo;
938 fold (!seen && !allvisible) rest
939 | [] -> true
941 let alltilesvisible = fold true layout in
942 alltilesvisible;
945 let gotoy y =
946 let y = bound y 0 state.maxy in
947 let y, layout, proceed =
948 match conf.maxwait with
949 | Some time when state.ghyll == noghyll ->
950 begin match state.throttle with
951 | None ->
952 let layout = layout y state.winh in
953 let ready = layoutready layout in
954 if not ready
955 then (
956 load layout;
957 state.throttle <- Some (layout, y, now ());
959 else G.postRedisplay "gotoy showall (None)";
960 y, layout, ready
961 | Some (_, _, started) ->
962 let dt = now () -. started in
963 if dt > time
964 then (
965 state.throttle <- None;
966 let layout = layout y state.winh in
967 load layout;
968 G.postRedisplay "maxwait";
969 y, layout, true
971 else -1, [], false
974 | _ ->
975 let layout = layout y state.winh in
976 if not !wtmode || layoutready layout
977 then G.postRedisplay "gotoy ready";
978 y, layout, true
980 if proceed
981 then (
982 state.y <- y;
983 state.layout <- layout;
984 begin match state.mode with
985 | LinkNav ln ->
986 begin match ln with
987 | Ltexact (pageno, linkno) ->
988 let rec loop = function
989 | [] ->
990 state.mode <- LinkNav (Ltgendir 0)
991 | l :: _ when l.pageno = pageno ->
992 begin match getopaque pageno with
993 | None ->
994 state.mode <- LinkNav (Ltgendir 0)
995 | Some opaque ->
996 let x0, y0, x1, y1 = getlinkrect opaque linkno in
997 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
998 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
999 then state.mode <- LinkNav (Ltgendir 0)
1001 | _ :: rest -> loop rest
1003 loop layout
1004 | Ltgendir _ -> ()
1006 | Birdseye _
1007 | Textentry _
1008 | View -> ()
1009 end;
1010 begin match state.mode with
1011 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1012 if not (pagevisible layout pageno)
1013 then (
1014 match state.layout with
1015 | [] -> ()
1016 | l :: _ ->
1017 state.mode <- Birdseye (
1018 conf, leftx, l.pageno, hooverpageno, anchor
1021 | LinkNav lt ->
1022 begin match lt with
1023 | Ltgendir dir ->
1024 let linknav =
1025 let rec loop = function
1026 | [] -> lt
1027 | l :: rest ->
1028 match getopaque l.pageno with
1029 | None -> loop rest
1030 | Some opaque ->
1031 let link =
1032 let ld =
1033 if dir = 0
1034 then LDfirstvisible (l.pagex, l.pagey, dir)
1035 else (
1036 if dir > 0 then LDfirst else LDlast
1039 findlink opaque ld
1041 match link with
1042 | Lnotfound -> loop rest
1043 | Lfound n ->
1044 showlinktype (getlink opaque n);
1045 Ltexact (l.pageno, n)
1047 loop state.layout
1049 state.mode <- LinkNav linknav
1050 | Ltexact _ -> ()
1052 | Textentry _
1053 | View -> ()
1054 end;
1055 preload layout;
1057 state.ghyll <- noghyll;
1058 if conf.updatecurs
1059 then (
1060 let mx, my = state.mpos in
1061 updateunder mx my;
1065 let conttiling pageno opaque =
1066 tilepage pageno opaque
1067 (if conf.preload then preloadlayout state.y else state.layout)
1070 let gotoy_and_clear_text y =
1071 if not conf.verbose then state.text <- E.s;
1072 gotoy y;
1075 let getanchory (n, top, dtop) =
1076 let y, h = getpageyh n in
1077 if conf.presentation
1078 then
1079 let ips = calcips h in
1080 y + truncate (top*.float h -. dtop*.float ips) + ips;
1081 else
1082 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1085 let gotoanchor anchor =
1086 gotoy (getanchory anchor);
1089 let addnav () =
1090 cbput state.hists.nav (getanchor ());
1093 let getnav dir =
1094 let anchor = cbgetc state.hists.nav dir in
1095 getanchory anchor;
1098 let gotoghyll1 single y =
1099 let scroll f n a b =
1100 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1101 let snake f a b =
1102 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1103 if f < a
1104 then s (float f /. float a)
1105 else (
1106 if f > b
1107 then 1.0 -. s ((float (f-b) /. float (n-b)))
1108 else 1.0
1111 snake f a b
1112 and summa n a b =
1113 let ins = float a *. 0.5
1114 and outs = float (n-b) *. 0.5 in
1115 let ones = b - a in
1116 ins +. outs +. float ones
1118 let rec set nab y sy =
1119 let (_N, _A, _B), y =
1120 if single
1121 then
1122 let scl = if y > sy then 2 else -2 in
1123 let _N, _, _ = nab in
1124 (_N,0,_N), y+conf.scrollstep*scl
1125 else nab,y in
1126 let sum = summa _N _A _B in
1127 let dy = float (y - sy) in
1128 state.ghyll <- (
1129 let rec gf n y1 o =
1130 if n >= _N
1131 then state.ghyll <- noghyll
1132 else
1133 let go n =
1134 let s = scroll n _N _A _B in
1135 let y1 = y1 +. ((s *. dy) /. sum) in
1136 gotoy_and_clear_text (truncate y1);
1137 state.ghyll <- gf (n+1) y1;
1139 match o with
1140 | None -> go n
1141 | Some y' when single -> set nab y' state.y
1142 | Some y' -> set (_N/2, 1, 1) y' state.y
1144 gf 0 (float state.y)
1147 match conf.ghyllscroll with
1148 | Some nab when not conf.presentation ->
1149 if state.ghyll == noghyll
1150 then set nab y state.y
1151 else state.ghyll (Some y)
1152 | _ ->
1153 gotoy_and_clear_text y
1156 let gotoghyll = gotoghyll1 false;;
1158 let gotopage n top =
1159 let y, h = getpageyh n in
1160 let y = y + (truncate (top *. float h)) in
1161 gotoghyll y
1164 let gotopage1 n top =
1165 let y = getpagey n in
1166 let y = y + top in
1167 gotoghyll y
1170 let invalidate s f =
1171 state.layout <- [];
1172 state.pdims <- [];
1173 state.rects <- [];
1174 state.rects1 <- [];
1175 match state.geomcmds with
1176 | ps, [] when emptystr ps ->
1177 f ();
1178 state.geomcmds <- s, [];
1180 | ps, [] ->
1181 state.geomcmds <- ps, [s, f];
1183 | ps, (s', _) :: rest when s' = s ->
1184 state.geomcmds <- ps, ((s, f) :: rest);
1186 | ps, cmds ->
1187 state.geomcmds <- ps, ((s, f) :: cmds);
1190 let flushpages () =
1191 Hashtbl.iter (fun _ opaque ->
1192 wcmd "freepage %s" (~> opaque);
1193 ) state.pagemap;
1194 Hashtbl.clear state.pagemap;
1197 let flushtiles () =
1198 if not (Queue.is_empty state.tilelru)
1199 then (
1200 Queue.iter (fun (k, p, s) ->
1201 wcmd "freetile %s" (~> p);
1202 state.memused <- state.memused - s;
1203 Hashtbl.remove state.tilemap k;
1204 ) state.tilelru;
1205 state.uioh#infochanged Memused;
1206 Queue.clear state.tilelru;
1208 load state.layout;
1211 let stateh h =
1212 let h = truncate (float h*.conf.zoom) in
1213 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
1214 h - d
1217 let opendoc path password =
1218 state.path <- path;
1219 state.password <- password;
1220 state.gen <- state.gen + 1;
1221 state.docinfo <- [];
1222 state.outlines <- [||];
1224 flushpages ();
1225 setaalevel conf.aalevel;
1226 let titlepath =
1227 if emptystr state.origin
1228 then path
1229 else state.origin
1231 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
1232 wcmd "open %d %d %s\000%s\000" (btod !wtmode) (btod !cxack) path password;
1233 invalidate "reqlayout"
1234 (fun () ->
1235 wcmd "reqlayout %d %d %d %s\000"
1236 conf.angle (FMTE.to_int conf.fitmodel)
1237 (stateh state.winh) state.nameddest
1241 let reload () =
1242 state.anchor <- getanchor ();
1243 opendoc state.path state.password;
1246 let scalecolor c =
1247 let c = c *. conf.colorscale in
1248 (c, c, c);
1251 let scalecolor2 (r, g, b) =
1252 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1255 let docolumns = function
1256 | Csingle _ ->
1257 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1258 let rec loop pageno pdimno pdim y ph pdims =
1259 if pageno = state.pagecount
1260 then ()
1261 else
1262 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1263 match pdims with
1264 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1265 pdimno+1, pdim, rest
1266 | _ ->
1267 pdimno, pdim, pdims
1269 let x = max 0 (((wadjsb state.winw - w) / 2) - xoff) in
1270 let y = y +
1271 (if conf.presentation
1272 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1273 else (if pageno = 0 then 0 else conf.interpagespace)
1276 a.(pageno) <- (pdimno, x, y, pdim);
1277 loop (pageno+1) pdimno pdim (y + h) h pdims
1279 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1280 conf.columns <- Csingle a;
1282 | Cmulti ((columns, coverA, coverB), _) ->
1283 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1284 let rec loop pageno pdimno pdim x y rowh pdims =
1285 let rec fixrow m = if m = pageno then () else
1286 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1287 if h < rowh
1288 then (
1289 let y = y + (rowh - h) / 2 in
1290 a.(m) <- (pdimno, x, y, pdim);
1292 fixrow (m+1)
1294 if pageno = state.pagecount
1295 then fixrow (((pageno - 1) / columns) * columns)
1296 else
1297 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1298 match pdims with
1299 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1300 pdimno+1, pdim, rest
1301 | _ ->
1302 pdimno, pdim, pdims
1304 let x, y, rowh' =
1305 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1306 then (
1307 let x = (wadjsb state.winw - w) / 2 in
1308 let ips =
1309 if conf.presentation then calcips h else conf.interpagespace in
1310 x, y + ips + rowh, h
1312 else (
1313 if (pageno - coverA) mod columns = 0
1314 then (
1315 let x = max 0 (wadjsb state.winw - state.w) / 2 in
1316 let y =
1317 if conf.presentation
1318 then
1319 let ips = calcips h in
1320 y + (if pageno = 0 then 0 else calcips rowh + ips)
1321 else
1322 y + (if pageno = 0 then 0 else conf.interpagespace)
1324 x, y + rowh, h
1326 else x, y, max rowh h
1329 let y =
1330 if pageno > 1 && (pageno - coverA) mod columns = 0
1331 then (
1332 let y =
1333 if pageno = columns && conf.presentation
1334 then (
1335 let ips = calcips rowh in
1336 for i = 0 to pred columns
1338 let (pdimno, x, y, pdim) = a.(i) in
1339 a.(i) <- (pdimno, x, y+ips, pdim)
1340 done;
1341 y+ips;
1343 else y
1345 fixrow (pageno - columns);
1348 else y
1350 a.(pageno) <- (pdimno, x, y, pdim);
1351 let x = x + w + xoff*2 + conf.interpagespace in
1352 loop (pageno+1) pdimno pdim x y rowh' pdims
1354 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1355 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1357 | Csplit (c, _) ->
1358 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1359 let rec loop pageno pdimno pdim y pdims =
1360 if pageno = state.pagecount
1361 then ()
1362 else
1363 let pdimno, ((_, w, h, _) as pdim), pdims =
1364 match pdims with
1365 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1366 pdimno+1, pdim, rest
1367 | _ ->
1368 pdimno, pdim, pdims
1370 let cw = w / c in
1371 let rec loop1 n x y =
1372 if n = c then y else (
1373 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1374 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1377 let y = loop1 0 0 y in
1378 loop (pageno+1) pdimno pdim y pdims
1380 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1381 conf.columns <- Csplit (c, a);
1384 let represent () =
1385 docolumns conf.columns;
1386 state.maxy <- calcheight ();
1387 if state.reprf == noreprf
1388 then (
1389 match state.mode with
1390 | Birdseye (_, _, pageno, _, _) ->
1391 let y, h = getpageyh pageno in
1392 let top = (state.winh - h) / 2 in
1393 gotoy (max 0 (y - top))
1394 | Textentry _
1395 | View
1396 | LinkNav _ -> gotoanchor state.anchor
1398 else (
1399 state.reprf ();
1400 state.reprf <- noreprf;
1404 let reshape w h =
1405 GlDraw.viewport ~x:0 ~y:0 ~w:w ~h:h;
1406 let firsttime = state.geomcmds == firstgeomcmds in
1407 if not firsttime && nogeomcmds state.geomcmds
1408 then state.anchor <- getanchor ();
1410 state.winw <- w;
1411 let w = wadjsb (truncate (float w *. conf.zoom)) in
1412 let w = max w 2 in
1413 state.winh <- h;
1414 setfontsize fstate.fontsize;
1415 GlMat.mode `modelview;
1416 GlMat.load_identity ();
1418 GlMat.mode `projection;
1419 GlMat.load_identity ();
1420 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1421 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1422 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
1424 let relx =
1425 if conf.zoom <= 1.0
1426 then 0.0
1427 else float state.x /. float state.w
1429 invalidate "geometry"
1430 (fun () ->
1431 state.w <- w;
1432 if not firsttime
1433 then state.x <- truncate (relx *. float w);
1434 let w =
1435 match conf.columns with
1436 | Csingle _ -> w
1437 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1438 | Csplit (c, _) -> w * c
1440 wcmd "geometry %d %d %d"
1441 w (stateh h) (FMTE.to_int conf.fitmodel)
1445 let enttext () =
1446 let len = String.length state.text in
1447 let x0 = xadjsb 0 in
1448 let drawstring s =
1449 let hscrollh =
1450 match state.mode with
1451 | Textentry _ | View | LinkNav _ ->
1452 let h, _, _ = state.uioh#scrollpw in
1454 | Birdseye _ -> 0
1456 let rect x w =
1457 filledrect x (float (state.winh - (fstate.fontsize + 4) - hscrollh))
1458 (x+.w) (float (state.winh - hscrollh))
1461 let w = float (wadjsb state.winw - 1) in
1462 if state.progress >= 0.0 && state.progress < 1.0
1463 then (
1464 GlDraw.color (0.3, 0.3, 0.3);
1465 let w1 = w *. state.progress in
1466 rect (float x0) w1;
1467 GlDraw.color (0.0, 0.0, 0.0);
1468 rect (float x0+.w1) (float x0+.w-.w1)
1470 else (
1471 GlDraw.color (0.0, 0.0, 0.0);
1472 rect (float x0) w;
1475 GlDraw.color (1.0, 1.0, 1.0);
1476 drawstring fstate.fontsize
1477 (if conf.leftscroll then x0 + 2 else x0 + if len > 0 then 8 else 2)
1478 (state.winh - hscrollh - 5) s;
1480 let s =
1481 match state.mode with
1482 | Textentry ((prefix, text, _, _, _, _), _) ->
1483 let s =
1484 if len > 0
1485 then
1486 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1487 else
1488 Printf.sprintf "%s%s_" prefix text
1492 | Birdseye _
1493 | View
1494 | LinkNav _ -> state.text
1496 let s =
1497 if state.newerrmsgs
1498 then (
1499 if not (istextentry state.mode) && state.uioh#eformsgs
1500 then
1501 let s1 = "(press 'e' to review error messasges)" in
1502 if nonemptystr s then s ^ " " ^ s1 else s1
1503 else s
1505 else s
1507 if nonemptystr s
1508 then drawstring s
1511 let gctiles () =
1512 let len = Queue.length state.tilelru in
1513 let layout = lazy (
1514 match state.throttle with
1515 | None ->
1516 if conf.preload
1517 then preloadlayout state.y
1518 else state.layout
1519 | Some (layout, _, _) ->
1520 layout
1521 ) in
1522 let rec loop qpos =
1523 if state.memused <= conf.memlimit
1524 then ()
1525 else (
1526 if qpos < len
1527 then
1528 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1529 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1530 let (_, pw, ph, _) = getpagedim n in
1532 gen = state.gen
1533 && colorspace = conf.colorspace
1534 && angle = conf.angle
1535 && pagew = pw
1536 && pageh = ph
1537 && (
1538 let x = col*conf.tilew
1539 and y = row*conf.tileh in
1540 tilevisible (Lazy.force_val layout) n x y
1542 then Queue.push lruitem state.tilelru
1543 else (
1544 freepbo p;
1545 wcmd "freetile %s" (~> p);
1546 state.memused <- state.memused - s;
1547 state.uioh#infochanged Memused;
1548 Hashtbl.remove state.tilemap k;
1550 loop (qpos+1)
1553 loop 0
1556 let logcurrently = function
1557 | Idle -> dolog "Idle"
1558 | Loading (l, gen) ->
1559 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
1560 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
1561 dolog
1562 "Tiling %d[%d,%d] page=%s cs=%s angle"
1563 l.pageno col row (~> pageopaque)
1564 (CSTE.to_string colorspace)
1566 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
1567 angle gen conf.angle state.gen
1568 tilew tileh
1569 conf.tilew conf.tileh
1571 | Outlining _ ->
1572 dolog "outlining"
1575 let splitatspace =
1576 let r = Str.regexp " " in
1577 fun s -> Str.bounded_split r s 2;
1580 let onpagerect pageno f =
1581 let b =
1582 match conf.columns with
1583 | Cmulti (_, b) -> b
1584 | Csingle b -> b
1585 | Csplit (_, b) -> b
1587 if pageno >= 0 && pageno < Array.length b
1588 then
1589 let (_, _, _, (w, h, _, _)) = b.(pageno) in
1590 f w h
1593 let gotopagexy1 pageno x y =
1594 let _,w1,h1,leftx = getpagedim pageno in
1595 let top = y /. (float h1) in
1596 let left = x /. (float w1) in
1597 let py, w, h = getpageywh pageno in
1598 let wh = state.winh - hscrollh () in
1599 let x = left *. (float w) in
1600 let x = leftx + state.x + truncate x in
1601 let sx =
1602 if x < 0 || x >= wadjsb state.winw
1603 then state.x - x
1604 else state.x
1606 let pdy = truncate (top *. float h) in
1607 let y' = py + pdy in
1608 let dy = y' - state.y in
1609 let sy =
1610 if x != state.x || not (dy > 0 && dy < wh)
1611 then (
1612 if conf.presentation
1613 then
1614 if abs (py - y') > wh
1615 then y'
1616 else py
1617 else y';
1619 else state.y
1621 if state.x != sx || state.y != sy
1622 then (
1623 let x, y =
1624 if !wtmode
1625 then (
1626 let ww = wadjsb state.winw in
1627 let qx = sx / ww
1628 and qy = pdy / wh in
1629 let x = qx * ww
1630 and y = py + qy * wh in
1631 let x = if -x + ww > w1 then -(w1-ww) else x
1632 and y' = if y + wh > state.maxy then state.maxy - wh else y in
1633 let y =
1634 if conf.presentation
1635 then
1636 if abs (py - y') > wh
1637 then y'
1638 else py
1639 else y';
1641 (x, y)
1643 else (sx, sy)
1645 state.x <- x;
1646 gotoy_and_clear_text y;
1648 else gotoy_and_clear_text state.y;
1651 let gotopagexy pageno x y =
1652 match state.mode with
1653 | Birdseye _ -> gotopage pageno 0.0
1654 | Textentry _
1655 | View
1656 | LinkNav _ -> gotopagexy1 pageno x y
1659 let act cmds =
1660 (* dolog "%S" cmds; *)
1661 let cl = splitatspace cmds in
1662 let scan s fmt f =
1663 try Scanf.sscanf s fmt f
1664 with exn ->
1665 dolog "error processing '%S': %s" cmds (exntos exn);
1666 exit 1
1668 let addoutline outline =
1669 match state.currently with
1670 | Outlining outlines ->
1671 state.currently <- Outlining (outline :: outlines)
1672 | Idle -> state.currently <- Outlining [outline]
1673 | Loading _
1674 | Tiling _ ->
1675 dolog "invalid outlining state";
1676 logcurrently state.currently
1678 match cl with
1679 | "clear" :: [] ->
1680 state.uioh#infochanged Pdim;
1681 state.pdims <- [];
1683 | "clearrects" :: [] ->
1684 state.rects <- state.rects1;
1685 G.postRedisplay "clearrects";
1687 | "continue" :: args :: [] ->
1688 let n = scan args "%u" (fun n -> n) in
1689 state.pagecount <- n;
1690 begin match state.currently with
1691 | Outlining l ->
1692 state.currently <- Idle;
1693 state.outlines <- Array.of_list (List.rev l)
1694 | Idle
1695 | Loading _
1696 | Tiling _ -> ()
1697 end;
1699 let cur, cmds = state.geomcmds in
1700 if emptystr cur
1701 then failwith "umpossible";
1703 begin match List.rev cmds with
1704 | [] ->
1705 state.geomcmds <- E.s, [];
1706 state.throttle <- None;
1707 represent ();
1708 | (s, f) :: rest ->
1709 f ();
1710 state.geomcmds <- s, List.rev rest;
1711 end;
1712 if conf.maxwait = None && not !wtmode
1713 then G.postRedisplay "continue";
1715 | "title" :: args :: [] ->
1716 conf.title <- args;
1717 Wsi.settitle args
1719 | "msg" :: args :: [] ->
1720 showtext ' ' args
1722 | "vmsg" :: args :: [] ->
1723 if conf.verbose
1724 then showtext ' ' args
1726 | "emsg" :: args :: [] ->
1727 Buffer.add_string state.errmsgs args;
1728 state.newerrmsgs <- true;
1729 G.postRedisplay "error message"
1731 | "progress" :: args :: [] ->
1732 let progress, text =
1733 scan args "%f %n"
1734 (fun f pos ->
1735 f, String.sub args pos (String.length args - pos))
1737 state.text <- text;
1738 state.progress <- progress;
1739 G.postRedisplay "progress"
1741 | "firstmatch" :: args :: [] ->
1742 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1743 scan args "%u %d %f %f %f %f %f %f %f %f"
1744 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1745 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1747 let xoff = float (xadjsb 0) in
1748 let x0 = x0 +. xoff
1749 and x1 = x1 +. xoff
1750 and x2 = x2 +. xoff
1751 and x3 = x3 +. xoff in
1752 let y = (getpagey pageno) + truncate y0 in
1753 addnav ();
1754 gotoy y;
1755 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
1757 | "match" :: args :: [] ->
1758 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1759 scan args "%u %d %f %f %f %f %f %f %f %f"
1760 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1761 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1763 let xoff = float (xadjsb 0) in
1764 let x0 = x0 +. xoff
1765 and x1 = x1 +. xoff
1766 and x2 = x2 +. xoff
1767 and x3 = x3 +. xoff in
1768 state.rects1 <-
1769 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1771 | "page" :: args :: [] ->
1772 let pageopaques, t = scan args "%s %f" (fun p t -> p, t) in
1773 let pageopaque = ~< pageopaques in
1774 begin match state.currently with
1775 | Loading (l, gen) ->
1776 vlog "page %d took %f sec" l.pageno t;
1777 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1778 begin match state.throttle with
1779 | None ->
1780 let preloadedpages =
1781 if conf.preload
1782 then preloadlayout state.y
1783 else state.layout
1785 let evict () =
1786 let set =
1787 List.fold_left (fun s l -> IntSet.add l.pageno s)
1788 IntSet.empty preloadedpages
1790 let evictedpages =
1791 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1792 if not (IntSet.mem pageno set)
1793 then (
1794 wcmd "freepage %s" (~> opaque);
1795 key :: accu
1797 else accu
1798 ) state.pagemap []
1800 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1802 evict ();
1803 state.currently <- Idle;
1804 if gen = state.gen
1805 then (
1806 tilepage l.pageno pageopaque state.layout;
1807 load state.layout;
1808 load preloadedpages;
1809 if pagevisible state.layout l.pageno
1810 && layoutready state.layout
1811 then G.postRedisplay "page";
1814 | Some (layout, _, _) ->
1815 state.currently <- Idle;
1816 tilepage l.pageno pageopaque layout;
1817 load state.layout
1818 end;
1820 | Idle
1821 | Tiling _
1822 | Outlining _ ->
1823 dolog "Inconsistent loading state";
1824 logcurrently state.currently;
1825 exit 1
1828 | "tile" :: args :: [] ->
1829 let (x, y, opaques, size, t) =
1830 scan args "%u %u %s %u %f"
1831 (fun x y p size t -> (x, y, p, size, t))
1833 let opaque = ~< opaques in
1834 begin match state.currently with
1835 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1836 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1838 unmappbo opaque;
1839 if tilew != conf.tilew || tileh != conf.tileh
1840 then (
1841 wcmd "freetile %s" (~> opaque);
1842 state.currently <- Idle;
1843 load state.layout;
1845 else (
1846 puttileopaque l col row gen cs angle opaque size t;
1847 state.memused <- state.memused + size;
1848 state.uioh#infochanged Memused;
1849 gctiles ();
1850 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1851 opaque, size) state.tilelru;
1853 let layout =
1854 match state.throttle with
1855 | None -> state.layout
1856 | Some (layout, _, _) -> layout
1859 state.currently <- Idle;
1860 if gen = state.gen
1861 && conf.colorspace = cs
1862 && conf.angle = angle
1863 && tilevisible layout l.pageno x y
1864 then conttiling l.pageno pageopaque;
1866 begin match state.throttle with
1867 | None ->
1868 preload state.layout;
1869 if gen = state.gen
1870 && conf.colorspace = cs
1871 && conf.angle = angle
1872 && tilevisible state.layout l.pageno x y
1873 && (not !wtmode || layoutready state.layout)
1874 then G.postRedisplay "tile nothrottle";
1876 | Some (layout, y, _) ->
1877 let ready = layoutready layout in
1878 if ready
1879 then (
1880 state.y <- y;
1881 state.layout <- layout;
1882 state.throttle <- None;
1883 G.postRedisplay "throttle";
1885 else load layout;
1886 end;
1889 | Idle
1890 | Loading _
1891 | Outlining _ ->
1892 dolog "Inconsistent tiling state";
1893 logcurrently state.currently;
1894 exit 1
1897 | "pdim" :: args :: [] ->
1898 let (n, w, h, _) as pdim =
1899 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1901 let pdim =
1902 match conf.fitmodel with
1903 | FitWidth -> pdim
1904 | FitPage | FitProportional ->
1905 match conf.columns with
1906 | Csplit _ -> (n, w, h, 0)
1907 | Csingle _ | Cmulti _ -> pdim
1909 state.uioh#infochanged Pdim;
1910 state.pdims <- pdim :: state.pdims
1912 | "o" :: args :: [] ->
1913 let (l, n, t, h, pos) =
1914 scan args "%u %u %d %u %n"
1915 (fun l n t h pos -> l, n, t, h, pos)
1917 let s = String.sub args pos (String.length args - pos) in
1918 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
1920 | "ou" :: args :: [] ->
1921 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
1922 let s = String.sub args pos len in
1923 let pos2 = pos + len + 1 in
1924 let uri = String.sub args pos2 (String.length args - pos2) in
1925 addoutline (s, l, Ouri uri)
1927 | "on" :: args :: [] ->
1928 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
1929 let s = String.sub args pos (String.length args - pos) in
1930 addoutline (s, l, Onone)
1932 | "a" :: args :: [] ->
1933 let (n, l, t) =
1934 scan args "%u %d %d" (fun n l t -> n, l, t)
1936 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
1938 | "info" :: args :: [] ->
1939 state.docinfo <- (1, args) :: state.docinfo
1941 | "infoend" :: [] ->
1942 state.uioh#infochanged Docinfo;
1943 state.docinfo <- List.rev state.docinfo
1945 | _ ->
1946 error "unknown cmd `%S'" cmds
1949 let onhist cb =
1950 let rc = cb.rc in
1951 let action = function
1952 | HCprev -> cbget cb ~-1
1953 | HCnext -> cbget cb 1
1954 | HCfirst -> cbget cb ~-(cb.rc)
1955 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1956 and cancel () = cb.rc <- rc
1957 in (action, cancel)
1960 let search pattern forward =
1961 match conf.columns with
1962 | Csplit _ ->
1963 showtext '!' "searching does not work properly in split columns mode"
1964 | Csingle _
1965 | Cmulti _ ->
1966 if nonemptystr pattern
1967 then
1968 let pn, py =
1969 match state.layout with
1970 | [] -> 0, 0
1971 | l :: _ ->
1972 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1974 wcmd "search %d %d %d %d,%s\000"
1975 (btod conf.icase) pn py (btod forward) pattern;
1978 let intentry text key =
1979 let c =
1980 if key >= 32 && key < 127
1981 then Char.chr key
1982 else '\000'
1984 match c with
1985 | '0' .. '9' ->
1986 let text = addchar text c in
1987 TEcont text
1989 | _ ->
1990 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1991 TEcont text
1994 let linknentry text key =
1995 let c =
1996 if key >= 32 && key < 127
1997 then Char.chr key
1998 else '\000'
2000 match c with
2001 | 'a' .. 'z' ->
2002 let text = addchar text c in
2003 TEcont text
2005 | _ ->
2006 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2007 TEcont text
2010 let linkndone f s =
2011 if nonemptystr s
2012 then (
2013 let n =
2014 let l = String.length s in
2015 let rec loop pos n = if pos = l then n else
2016 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2017 loop (pos+1) (n*26 + m)
2018 in loop 0 0
2020 let rec loop n = function
2021 | [] -> ()
2022 | l :: rest ->
2023 match getopaque l.pageno with
2024 | None -> loop n rest
2025 | Some opaque ->
2026 let m = getlinkcount opaque in
2027 if n < m
2028 then (
2029 let under = getlink opaque n in
2030 f under
2032 else loop (n-m) rest
2034 loop n state.layout;
2038 let textentry text key =
2039 if key land 0xff00 = 0xff00
2040 then TEcont text
2041 else TEcont (text ^ toutf8 key)
2044 let reqlayout angle fitmodel =
2045 match state.throttle with
2046 | None ->
2047 if nogeomcmds state.geomcmds
2048 then state.anchor <- getanchor ();
2049 conf.angle <- angle mod 360;
2050 if conf.angle != 0
2051 then (
2052 match state.mode with
2053 | LinkNav _ -> state.mode <- View
2054 | Birdseye _
2055 | Textentry _
2056 | View -> ()
2058 conf.fitmodel <- fitmodel;
2059 invalidate "reqlayout"
2060 (fun () ->
2061 wcmd "reqlayout %d %d %d"
2062 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
2064 | _ -> ()
2067 let settrim trimmargins trimfuzz =
2068 if nogeomcmds state.geomcmds
2069 then state.anchor <- getanchor ();
2070 conf.trimmargins <- trimmargins;
2071 conf.trimfuzz <- trimfuzz;
2072 let x0, y0, x1, y1 = trimfuzz in
2073 invalidate "settrim"
2074 (fun () ->
2075 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2076 flushpages ();
2079 let setzoom zoom =
2080 match state.throttle with
2081 | None ->
2082 let zoom = max 0.0001 zoom in
2083 if zoom <> conf.zoom
2084 then (
2085 state.prevzoom <- (conf.zoom, state.x);
2086 conf.zoom <- zoom;
2087 reshape state.winw state.winh;
2088 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2091 | Some (layout, y, started) ->
2092 let time =
2093 match conf.maxwait with
2094 | None -> 0.0
2095 | Some t -> t
2097 let dt = now () -. started in
2098 if dt > time
2099 then (
2100 state.y <- y;
2101 load layout;
2105 let setcolumns mode columns coverA coverB =
2106 state.prevcolumns <- Some (conf.columns, conf.zoom);
2107 if columns < 0
2108 then (
2109 if isbirdseye mode
2110 then showtext '!' "split mode doesn't work in bird's eye"
2111 else (
2112 conf.columns <- Csplit (-columns, E.a);
2113 state.x <- 0;
2114 conf.zoom <- 1.0;
2117 else (
2118 if columns < 2
2119 then (
2120 conf.columns <- Csingle E.a;
2121 state.x <- 0;
2122 setzoom 1.0;
2124 else (
2125 conf.columns <- Cmulti ((columns, coverA, coverB), E.a);
2126 conf.zoom <- 1.0;
2129 reshape state.winw state.winh;
2132 let resetmstate () =
2133 state.mstate <- Mnone;
2134 Wsi.setcursor Wsi.CURSOR_INHERIT;
2137 let enterbirdseye () =
2138 let zoom = float conf.thumbw /. float state.winw in
2139 let birdseyepageno =
2140 let cy = state.winh / 2 in
2141 let fold = function
2142 | [] -> 0
2143 | l :: rest ->
2144 let rec fold best = function
2145 | [] -> best.pageno
2146 | l :: rest ->
2147 let d = cy - (l.pagedispy + l.pagevh/2)
2148 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2149 if abs d < abs dbest
2150 then fold l rest
2151 else best.pageno
2152 in fold l rest
2154 fold state.layout
2156 state.mode <- Birdseye (
2157 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2159 resetmstate ();
2160 conf.zoom <- zoom;
2161 conf.presentation <- false;
2162 conf.interpagespace <- 10;
2163 conf.hlinks <- false;
2164 conf.fitmodel <- FitPage;
2165 state.x <- 0;
2166 conf.maxwait <- None;
2167 conf.columns <- (
2168 match conf.beyecolumns with
2169 | Some c ->
2170 conf.zoom <- 1.0;
2171 Cmulti ((c, 0, 0), E.a)
2172 | None -> Csingle E.a
2174 if conf.verbose
2175 then
2176 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2177 (100.0*.zoom)
2178 else
2179 state.text <- E.s
2181 reshape state.winw state.winh;
2184 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2185 state.mode <- View;
2186 conf.zoom <- c.zoom;
2187 conf.presentation <- c.presentation;
2188 conf.interpagespace <- c.interpagespace;
2189 conf.maxwait <- c.maxwait;
2190 conf.hlinks <- c.hlinks;
2191 conf.fitmodel <- c.fitmodel;
2192 conf.beyecolumns <- (
2193 match conf.columns with
2194 | Cmulti ((c, _, _), _) -> Some c
2195 | Csingle _ -> None
2196 | Csplit _ -> failwith "leaving bird's eye split mode"
2198 conf.columns <- (
2199 match c.columns with
2200 | Cmulti (c, _) -> Cmulti (c, E.a)
2201 | Csingle _ -> Csingle E.a
2202 | Csplit (c, _) -> Csplit (c, E.a)
2204 if conf.verbose
2205 then
2206 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2207 (100.0*.conf.zoom)
2209 reshape state.winw state.winh;
2210 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2211 state.x <- leftx;
2214 let togglebirdseye () =
2215 match state.mode with
2216 | Birdseye vals -> leavebirdseye vals true
2217 | View -> enterbirdseye ()
2218 | Textentry _
2219 | LinkNav _ -> ()
2222 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2223 let pageno = max 0 (pageno - incr) in
2224 let rec loop = function
2225 | [] -> gotopage1 pageno 0
2226 | l :: _ when l.pageno = pageno ->
2227 if l.pagedispy >= 0 && l.pagey = 0
2228 then G.postRedisplay "upbirdseye"
2229 else gotopage1 pageno 0
2230 | _ :: rest -> loop rest
2232 loop state.layout;
2233 state.text <- E.s;
2234 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2237 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2238 let pageno = min (state.pagecount - 1) (pageno + incr) in
2239 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2240 let rec loop = function
2241 | [] ->
2242 let y, h = getpageyh pageno in
2243 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2244 gotoy (clamp dy)
2245 | l :: _ when l.pageno = pageno ->
2246 if l.pagevh != l.pageh
2247 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2248 else G.postRedisplay "downbirdseye"
2249 | _ :: rest -> loop rest
2251 loop state.layout;
2252 state.text <- E.s;
2255 let boundastep h step =
2256 if step < 0
2257 then bound step ~-h 0
2258 else bound step 0 h
2261 let optentry mode _ key =
2262 let btos b = if b then "on" else "off" in
2263 if key >= 32 && key < 127
2264 then
2265 let c = Char.chr key in
2266 match c with
2267 | 's' ->
2268 let ondone s =
2269 try conf.scrollstep <- int_of_string s with exc ->
2270 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2272 TEswitch ("scroll step: ", E.s, None, intentry, ondone, true)
2274 | 'A' ->
2275 let ondone s =
2277 conf.autoscrollstep <- boundastep state.winh (int_of_string s);
2278 if state.autoscroll <> None
2279 then state.autoscroll <- Some conf.autoscrollstep
2280 with exc ->
2281 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2283 TEswitch ("auto scroll step: ", E.s, None, intentry, ondone, true)
2285 | 'C' ->
2286 let ondone s =
2288 let n, a, b = multicolumns_of_string s in
2289 setcolumns mode n a b;
2290 with exc ->
2291 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
2293 TEswitch ("columns: ", E.s, None, textentry, ondone, true)
2295 | 'Z' ->
2296 let ondone s =
2298 let zoom = float (int_of_string s) /. 100.0 in
2299 setzoom zoom
2300 with exc ->
2301 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2303 TEswitch ("zoom: ", E.s, None, intentry, ondone, true)
2305 | 't' ->
2306 let ondone s =
2308 conf.thumbw <- bound (int_of_string s) 2 4096;
2309 state.text <-
2310 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2311 begin match mode with
2312 | Birdseye beye ->
2313 leavebirdseye beye false;
2314 enterbirdseye ();
2315 | Textentry _
2316 | View
2317 | LinkNav _ -> ();
2319 with exc ->
2320 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2322 TEswitch ("thumbnail width: ", E.s, None, intentry, ondone, true)
2324 | 'R' ->
2325 let ondone s =
2326 match try
2327 Some (int_of_string s)
2328 with exc ->
2329 state.text <- Printf.sprintf "bad integer `%s': %s"
2330 s (exntos exc);
2331 None
2332 with
2333 | Some angle -> reqlayout angle conf.fitmodel
2334 | None -> ()
2336 TEswitch ("rotation: ", E.s, None, intentry, ondone, true)
2338 | 'i' ->
2339 conf.icase <- not conf.icase;
2340 TEdone ("case insensitive search " ^ (btos conf.icase))
2342 | 'p' ->
2343 conf.preload <- not conf.preload;
2344 gotoy state.y;
2345 TEdone ("preload " ^ (btos conf.preload))
2347 | 'v' ->
2348 conf.verbose <- not conf.verbose;
2349 TEdone ("verbose " ^ (btos conf.verbose))
2351 | 'd' ->
2352 conf.debug <- not conf.debug;
2353 TEdone ("debug " ^ (btos conf.debug))
2355 | 'h' ->
2356 conf.maxhfit <- not conf.maxhfit;
2357 state.maxy <- calcheight ();
2358 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2360 | 'c' ->
2361 conf.crophack <- not conf.crophack;
2362 TEdone ("crophack " ^ btos conf.crophack)
2364 | 'a' ->
2365 let s =
2366 match conf.maxwait with
2367 | None ->
2368 conf.maxwait <- Some infinity;
2369 "always wait for page to complete"
2370 | Some _ ->
2371 conf.maxwait <- None;
2372 "show placeholder if page is not ready"
2374 TEdone s
2376 | 'f' ->
2377 conf.underinfo <- not conf.underinfo;
2378 TEdone ("underinfo " ^ btos conf.underinfo)
2380 | 'P' ->
2381 conf.savebmarks <- not conf.savebmarks;
2382 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2384 | 'S' ->
2385 let ondone s =
2387 let pageno, py =
2388 match state.layout with
2389 | [] -> 0, 0
2390 | l :: _ ->
2391 l.pageno, l.pagey
2393 conf.interpagespace <- int_of_string s;
2394 docolumns conf.columns;
2395 state.maxy <- calcheight ();
2396 let y = getpagey pageno in
2397 gotoy (y + py)
2398 with exc ->
2399 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2401 TEswitch ("vertical margin: ", E.s, None, intentry, ondone, true)
2403 | 'l' ->
2404 let fm =
2405 match conf.fitmodel with
2406 | FitProportional -> FitWidth
2407 | FitWidth | FitPage -> FitProportional
2409 reqlayout conf.angle fm;
2410 TEdone ("proportional display " ^ btos (fm == FitProportional))
2412 | 'T' ->
2413 settrim (not conf.trimmargins) conf.trimfuzz;
2414 TEdone ("trim margins " ^ btos conf.trimmargins)
2416 | 'I' ->
2417 conf.invert <- not conf.invert;
2418 TEdone ("invert colors " ^ btos conf.invert)
2420 | 'x' ->
2421 let ondone s =
2422 cbput state.hists.sel s;
2423 conf.selcmd <- s;
2425 TEswitch ("selection command: ", E.s, Some (onhist state.hists.sel),
2426 textentry, ondone, true)
2428 | 'M' ->
2429 if conf.pax == None
2430 then conf.pax <- Some (ref (0.0, 0, 0))
2431 else conf.pax <- None;
2432 TEdone ("PAX " ^ btos (conf.pax != None))
2434 | _ ->
2435 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2436 TEstop
2437 else
2438 TEcont state.text
2441 class type lvsource = object
2442 method getitemcount : int
2443 method getitem : int -> (string * int)
2444 method hasaction : int -> bool
2445 method exit :
2446 uioh:uioh ->
2447 cancel:bool ->
2448 active:int ->
2449 first:int ->
2450 pan:int ->
2451 uioh option
2452 method getactive : int
2453 method getfirst : int
2454 method getpan : int
2455 method getminfo : (int * int) array
2456 end;;
2458 class virtual lvsourcebase = object
2459 val mutable m_active = 0
2460 val mutable m_first = 0
2461 val mutable m_pan = 0
2462 method getactive = m_active
2463 method getfirst = m_first
2464 method getpan = m_pan
2465 method getminfo : (int * int) array = E.a
2466 end;;
2468 let withoutlastutf8 s =
2469 let len = String.length s in
2470 if len = 0
2471 then s
2472 else
2473 let rec find pos =
2474 if pos = 0
2475 then pos
2476 else
2477 let b = Char.code s.[pos] in
2478 if b land 0b11000000 = 0b11000000
2479 then pos
2480 else find (pos-1)
2482 let first =
2483 if Char.code s.[len-1] land 0x80 = 0
2484 then len-1
2485 else find (len-1)
2487 String.sub s 0 first;
2490 let textentrykeyboard
2491 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2492 let key =
2493 if key >= 0xffb0 && key <= 0xffb9
2494 then key - 0xffb0 + 48 else key
2496 let enttext te =
2497 state.mode <- Textentry (te, onleave);
2498 state.text <- E.s;
2499 enttext ();
2500 G.postRedisplay "textentrykeyboard enttext";
2502 let histaction cmd =
2503 match opthist with
2504 | None -> ()
2505 | Some (action, _) ->
2506 state.mode <- Textentry (
2507 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
2509 G.postRedisplay "textentry histaction"
2511 match key with
2512 | @backspace ->
2513 if emptystr text && cancelonempty
2514 then (
2515 onleave Cancel;
2516 G.postRedisplay "textentrykeyboard after cancel";
2518 else
2519 let s = withoutlastutf8 text in
2520 enttext (c, s, opthist, onkey, ondone, cancelonempty)
2522 | @enter | @kpenter ->
2523 ondone text;
2524 onleave Confirm;
2525 G.postRedisplay "textentrykeyboard after confirm"
2527 | @up | @kpup -> histaction HCprev
2528 | @down | @kpdown -> histaction HCnext
2529 | @home | @kphome -> histaction HCfirst
2530 | @jend | @kpend -> histaction HClast
2532 | @escape ->
2533 if emptystr text
2534 then (
2535 begin match opthist with
2536 | None -> ()
2537 | Some (_, onhistcancel) -> onhistcancel ()
2538 end;
2539 onleave Cancel;
2540 state.text <- E.s;
2541 G.postRedisplay "textentrykeyboard after cancel2"
2543 else (
2544 enttext (c, E.s, opthist, onkey, ondone, cancelonempty)
2547 | @delete | @kpdelete -> ()
2549 | _ when key != 0
2550 && key land 0xff00 != 0xff00 (* keyboard *)
2551 && key land 0xfe00 != 0xfe00 (* xkb *)
2552 && key land 0xfd00 != 0xfd00 (* 3270 *)
2554 begin match onkey text key with
2555 | TEdone text ->
2556 ondone text;
2557 onleave Confirm;
2558 G.postRedisplay "textentrykeyboard after confirm2";
2560 | TEcont text ->
2561 enttext (c, text, opthist, onkey, ondone, cancelonempty);
2563 | TEstop ->
2564 onleave Cancel;
2565 G.postRedisplay "textentrykeyboard after cancel3"
2567 | TEswitch te ->
2568 state.mode <- Textentry (te, onleave);
2569 G.postRedisplay "textentrykeyboard switch";
2570 end;
2572 | _ ->
2573 vlog "unhandled key %s" (Wsi.keyname key)
2576 let firstof first active =
2577 if first > active || abs (first - active) > fstate.maxrows - 1
2578 then max 0 (active - (fstate.maxrows/2))
2579 else first
2582 let calcfirst first active =
2583 if active > first
2584 then
2585 let rows = active - first in
2586 if rows > fstate.maxrows then active - fstate.maxrows else first
2587 else active
2590 let scrollph y maxy =
2591 let sh = float (maxy + state.winh) /. float state.winh in
2592 let sh = float state.winh /. sh in
2593 let sh = max sh (float conf.scrollh) in
2595 let percent = float y /. float maxy in
2596 let position = (float state.winh -. sh) *. percent in
2598 let position =
2599 if position +. sh > float state.winh
2600 then float state.winh -. sh
2601 else position
2603 position, sh;
2606 let coe s = (s :> uioh);;
2608 class listview ~zebra ~helpmode ~(source:lvsource) ~trusted ~modehash =
2609 object (self)
2610 val m_pan = source#getpan
2611 val m_first = source#getfirst
2612 val m_active = source#getactive
2613 val m_qsearch = E.s
2614 val m_prev_uioh = state.uioh
2616 method private elemunder y =
2617 if y < 0
2618 then None
2619 else
2620 let n = y / (fstate.fontsize+1) in
2621 if m_first + n < source#getitemcount
2622 then (
2623 if source#hasaction (m_first + n)
2624 then Some (m_first + n)
2625 else None
2627 else None
2629 method display =
2630 Gl.enable `blend;
2631 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
2632 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2633 filledrect 0. 0. (float state.winw) (float state.winh);
2634 GlDraw.color (1., 1., 1.);
2635 Gl.enable `texture_2d;
2636 let fs = fstate.fontsize in
2637 let nfs = fs + 1 in
2638 let hw = (wadjsb (xadjsb state.winw))/3 in
2639 let ww = fstate.wwidth in
2640 let tabw = 17.0*.ww in
2641 let itemcount = source#getitemcount in
2642 let minfo = source#getminfo in
2643 let x0, x1 =
2644 if conf.leftscroll
2645 then float (xadjsb 0), float (state.winw - 1)
2646 else 0.0, float (state.winw - conf.scrollbw - 1)
2648 let rec loop row =
2649 if (row - m_first) > fstate.maxrows
2650 then ()
2651 else (
2652 if row >= 0 && row < itemcount
2653 then (
2654 let (s, level) = source#getitem row in
2655 let y = (row - m_first) * nfs in
2656 let x =
2657 (if conf.leftscroll then float (xadjsb 0) else 5.0)
2658 +. (float (level + m_pan)) *. ww in
2659 if helpmode
2660 then GlDraw.color
2661 (let c = if row land 1 = 0 then 1.0 else 0.92 in (c,c,c));
2663 if row = m_active
2664 then (
2665 Gl.disable `texture_2d;
2666 let alpha = if source#hasaction row then 0.9 else 0.3 in
2667 GlDraw.color (1., 1., 1.) ~alpha;
2668 linerect (x0 +. 1.) (float (y + 1)) (x1) (float (y + fs + 3));
2669 Gl.enable `texture_2d;
2671 let c =
2672 if zebra && row land 1 = 1
2673 then 0.8
2674 else 1.0
2676 GlDraw.color (c,c,c);
2677 let drawtabularstring s =
2678 let drawstr x s =
2679 let x' = truncate (x0 +. x) in
2680 let pos = nindex s '\000' in
2681 if pos = -1
2682 then drawstring1 fs x' (y+nfs) s
2683 else
2684 let s1 = String.sub s 0 pos
2685 and s2 = String.sub s (pos+1) (String.length s - pos - 1) in
2686 let rec e s =
2687 if emptystr s
2688 then s
2689 else
2690 let s' = withoutlastutf8 s in
2691 let s = s' ^ "@Uellipsis" in
2692 let w = measurestr fs s in
2693 if float x' +. w +. ww < float (hw + x')
2694 then s
2695 else e s'
2697 let s1 =
2698 if float x' +. ww +. measurestr fs s1 > float (hw + x')
2699 then e s1
2700 else s1
2702 ignore (drawstring1 fs x' (y+nfs) s1);
2703 drawstring1 fs (hw + x') (y+nfs) s2
2705 if trusted
2706 then
2707 let x = if helpmode && row > 0 then x +. ww else x in
2708 let tabpos = nindex s '\t' in
2709 if tabpos > 0
2710 then
2711 let len = String.length s - tabpos - 1 in
2712 let s1 = String.sub s 0 tabpos
2713 and s2 = String.sub s (tabpos + 1) len in
2714 let nx = drawstr x s1 in
2715 let sw = nx -. x in
2716 let x = x +. (max tabw sw) in
2717 drawstr x s2
2718 else
2719 let len = String.length s - 2 in
2720 if len > 0 && s.[0] = '\xc2' && s.[1] = '\xb7'
2721 then
2722 let s = String.sub s 2 len in
2723 let x = if not helpmode then x +. ww else x in
2724 GlDraw.color (1.2, 1.2, 1.2);
2725 let vinc = drawstring1 (fs+fs/4)
2726 (truncate (x -. ww)) (y+nfs) s in
2727 GlDraw.color (1., 1., 1.);
2728 vinc +. (float fs *. 0.8)
2729 else
2730 drawstr x s
2731 else
2732 drawstr x s
2734 ignore (drawtabularstring s);
2735 loop (row+1)
2739 loop m_first;
2740 GlDraw.color (1.0, 1.0, 1.0) ~alpha:0.5;
2741 let rec loop row =
2742 if (row - m_first) > fstate.maxrows
2743 then ()
2744 else (
2745 if row >= 0 && row < itemcount
2746 then (
2747 let (s, level) = source#getitem row in
2748 let pos0 = nindex s '\000' in
2749 let y = (row - m_first) * nfs in
2750 let x = float (level + m_pan) *. ww in
2751 let (first, last) = minfo.(row) in
2752 let prefix =
2753 if pos0 > 0 && first > pos0
2754 then String.sub s (pos0+1) (first-pos0-1)
2755 else String.sub s 0 first
2757 let suffix = String.sub s first (last - first) in
2758 let w1 = measurestr fstate.fontsize prefix in
2759 let w2 = measurestr fstate.fontsize suffix in
2760 let x = x +. if conf.leftscroll then float (xadjsb 5) else 5.0 in
2761 let x = if pos0 > 0 && first > pos0 then x +. float hw else x in
2762 let x0 = x +. w1
2763 and y0 = float (y+2) in
2764 let x1 = x0 +. w2
2765 and y1 = float (y+fs+3) in
2766 filledrect x0 y0 x1 y1;
2767 loop (row+1)
2771 Gl.disable `texture_2d;
2772 if Array.length minfo > 0 then loop m_first;
2773 Gl.disable `blend;
2775 method updownlevel incr =
2776 let len = source#getitemcount in
2777 let curlevel =
2778 if m_active >= 0 && m_active < len
2779 then snd (source#getitem m_active)
2780 else -1
2782 let rec flow i =
2783 if i = len then i-1 else if i = -1 then 0 else
2784 let _, l = source#getitem i in
2785 if l != curlevel then i else flow (i+incr)
2787 let active = flow m_active in
2788 let first = calcfirst m_first active in
2789 G.postRedisplay "outline updownlevel";
2790 {< m_active = active; m_first = first >}
2792 method private key1 key mask =
2793 let set1 active first qsearch =
2794 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2796 let search active pattern incr =
2797 let active = if active = -1 then m_first else active in
2798 let dosearch re =
2799 let rec loop n =
2800 if n >= 0 && n < source#getitemcount
2801 then (
2802 let s, _ = source#getitem n in
2804 (try ignore (Str.search_forward re s 0); true
2805 with Not_found -> false)
2806 then Some n
2807 else loop (n + incr)
2809 else None
2811 loop active
2814 let re = Str.regexp_case_fold pattern in
2815 dosearch re
2816 with Failure s ->
2817 state.text <- s;
2818 None
2820 let itemcount = source#getitemcount in
2821 let find start incr =
2822 let rec find i =
2823 if i = -1 || i = itemcount
2824 then -1
2825 else (
2826 if source#hasaction i
2827 then i
2828 else find (i + incr)
2831 find start
2833 let set active first =
2834 let first = bound first 0 (itemcount - fstate.maxrows) in
2835 state.text <- E.s;
2836 coe {< m_active = active; m_first = first; m_qsearch = E.s >}
2838 let navigate incr =
2839 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2840 let active, first =
2841 let incr1 = if incr > 0 then 1 else -1 in
2842 if isvisible m_first m_active
2843 then
2844 let next =
2845 let next = m_active + incr in
2846 let next =
2847 if next < 0 || next >= itemcount
2848 then -1
2849 else find next incr1
2851 if abs (m_active - next) > fstate.maxrows
2852 then -1
2853 else next
2855 if next = -1
2856 then
2857 let first = m_first + incr in
2858 let first = bound first 0 (itemcount - fstate.maxrows) in
2859 let next =
2860 let next = m_active + incr in
2861 let next = bound next 0 (itemcount - 1) in
2862 find next ~-incr1
2864 let active =
2865 if next = -1
2866 then m_active
2867 else (
2868 if isvisible first next
2869 then next
2870 else m_active
2873 active, first
2874 else
2875 let first = min next m_first in
2876 let first =
2877 if abs (next - first) > fstate.maxrows
2878 then first + incr
2879 else first
2881 next, first
2882 else
2883 let first = m_first + incr in
2884 let first = bound first 0 (itemcount - 1) in
2885 let active =
2886 let next = m_active + incr in
2887 let next = bound next 0 (itemcount - 1) in
2888 let next = find next incr1 in
2889 let active =
2890 if next = -1 || abs (m_active - first) > fstate.maxrows
2891 then (
2892 let active = if m_active = -1 then next else m_active in
2893 active
2895 else next
2897 if isvisible first active
2898 then active
2899 else -1
2901 active, first
2903 G.postRedisplay "listview navigate";
2904 set active first;
2906 match key with
2907 | (@r|@s) when Wsi.withctrl mask ->
2908 let incr = if key = @r then -1 else 1 in
2909 let active, first =
2910 match search (m_active + incr) m_qsearch incr with
2911 | None ->
2912 state.text <- m_qsearch ^ " [not found]";
2913 m_active, m_first
2914 | Some active ->
2915 state.text <- m_qsearch;
2916 active, firstof m_first active
2918 G.postRedisplay "listview ctrl-r/s";
2919 set1 active first m_qsearch;
2921 | @insert when Wsi.withctrl mask ->
2922 if m_active >= 0 && m_active < source#getitemcount
2923 then (
2924 let s, _ = source#getitem m_active in
2925 selstring s;
2927 coe self
2929 | @backspace ->
2930 if emptystr m_qsearch
2931 then coe self
2932 else (
2933 let qsearch = withoutlastutf8 m_qsearch in
2934 if emptystr qsearch
2935 then (
2936 state.text <- E.s;
2937 G.postRedisplay "listview empty qsearch";
2938 set1 m_active m_first E.s;
2940 else
2941 let active, first =
2942 match search m_active qsearch ~-1 with
2943 | None ->
2944 state.text <- qsearch ^ " [not found]";
2945 m_active, m_first
2946 | Some active ->
2947 state.text <- qsearch;
2948 active, firstof m_first active
2950 G.postRedisplay "listview backspace qsearch";
2951 set1 active first qsearch
2954 | key when (key != 0 && key land 0xff00 != 0xff00) ->
2955 let pattern = m_qsearch ^ toutf8 key in
2956 let active, first =
2957 match search m_active pattern 1 with
2958 | None ->
2959 state.text <- pattern ^ " [not found]";
2960 m_active, m_first
2961 | Some active ->
2962 state.text <- pattern;
2963 active, firstof m_first active
2965 G.postRedisplay "listview qsearch add";
2966 set1 active first pattern;
2968 | @escape ->
2969 state.text <- E.s;
2970 if emptystr m_qsearch
2971 then (
2972 G.postRedisplay "list view escape";
2973 begin
2974 match
2975 source#exit ~uioh:(coe self)
2976 ~cancel:true ~active:m_active ~first:m_first ~pan:m_pan
2977 with
2978 | None -> m_prev_uioh
2979 | Some uioh -> uioh
2982 else (
2983 G.postRedisplay "list view kill qsearch";
2984 coe {< m_qsearch = E.s >}
2987 | @enter | @kpenter ->
2988 state.text <- E.s;
2989 let self = {< m_qsearch = E.s >} in
2990 let opt =
2991 G.postRedisplay "listview enter";
2992 if m_active >= 0 && m_active < source#getitemcount
2993 then (
2994 source#exit ~uioh:(coe self) ~cancel:false
2995 ~active:m_active ~first:m_first ~pan:m_pan;
2997 else (
2998 source#exit ~uioh:(coe self) ~cancel:true
2999 ~active:m_active ~first:m_first ~pan:m_pan;
3002 begin match opt with
3003 | None -> m_prev_uioh
3004 | Some uioh -> uioh
3007 | @delete | @kpdelete ->
3008 coe self
3010 | @up | @kpup -> navigate ~-1
3011 | @down | @kpdown -> navigate 1
3012 | @prior | @kpprior -> navigate ~-(fstate.maxrows)
3013 | @next | @kpnext -> navigate fstate.maxrows
3015 | @right | @kpright ->
3016 state.text <- E.s;
3017 G.postRedisplay "listview right";
3018 coe {< m_pan = m_pan - 1 >}
3020 | @left | @kpleft ->
3021 state.text <- E.s;
3022 G.postRedisplay "listview left";
3023 coe {< m_pan = m_pan + 1 >}
3025 | @home | @kphome ->
3026 let active = find 0 1 in
3027 G.postRedisplay "listview home";
3028 set active 0;
3030 | @jend | @kpend ->
3031 let first = max 0 (itemcount - fstate.maxrows) in
3032 let active = find (itemcount - 1) ~-1 in
3033 G.postRedisplay "listview end";
3034 set active first;
3036 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3037 coe self
3039 | _ ->
3040 dolog "listview unknown key %#x" key; coe self
3042 method key key mask =
3043 match state.mode with
3044 | Textentry te -> textentrykeyboard key mask te; coe self
3045 | Birdseye _
3046 | View
3047 | LinkNav _ -> self#key1 key mask
3049 method button button down x y _ =
3050 let opt =
3051 match button with
3052 | 1 when x > state.winw - conf.scrollbw ->
3053 G.postRedisplay "listview scroll";
3054 if down
3055 then
3056 let _, position, sh = self#scrollph in
3057 if y > truncate position && y < truncate (position +. sh)
3058 then (
3059 state.mstate <- Mscrolly;
3060 Some (coe self)
3062 else
3063 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3064 let first = truncate (s *. float source#getitemcount) in
3065 let first = min source#getitemcount first in
3066 Some (coe {< m_first = first; m_active = first >})
3067 else (
3068 state.mstate <- Mnone;
3069 Some (coe self);
3071 | 1 when not down ->
3072 begin match self#elemunder y with
3073 | Some n ->
3074 G.postRedisplay "listview click";
3075 source#exit ~uioh:(coe {< m_active = n >})
3076 ~cancel:false ~active:n ~first:m_first ~pan:m_pan
3077 | _ ->
3078 Some (coe self)
3080 | n when (n == 4 || n == 5) && not down ->
3081 let len = source#getitemcount in
3082 let first =
3083 if n = 5 && m_first + fstate.maxrows >= len
3084 then
3085 m_first
3086 else
3087 let first = m_first + (if n == 4 then -1 else 1) in
3088 bound first 0 (len - 1)
3090 G.postRedisplay "listview wheel";
3091 Some (coe {< m_first = first >})
3092 | n when (n = 6 || n = 7) && not down ->
3093 let inc = if n = 7 then -1 else 1 in
3094 G.postRedisplay "listview hwheel";
3095 Some (coe {< m_pan = m_pan + inc >})
3096 | _ ->
3097 Some (coe self)
3099 match opt with
3100 | None -> m_prev_uioh
3101 | Some uioh -> uioh
3103 method multiclick _ x y = self#button 1 true x y
3105 method motion _ y =
3106 match state.mstate with
3107 | Mscrolly ->
3108 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3109 let first = truncate (s *. float source#getitemcount) in
3110 let first = min source#getitemcount first in
3111 G.postRedisplay "listview motion";
3112 coe {< m_first = first; m_active = first >}
3113 | Msel _
3114 | Mpan _
3115 | Mscrollx
3116 | Mzoom _
3117 | Mzoomrect _
3118 | Mnone -> coe self
3120 method pmotion x y =
3121 if x < state.winw - conf.scrollbw
3122 then
3123 let n =
3124 match self#elemunder y with
3125 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3126 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3128 let o =
3129 if n != m_active
3130 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3131 else self
3133 coe o
3134 else (
3135 Wsi.setcursor Wsi.CURSOR_INHERIT;
3136 coe self
3139 method infochanged _ = ()
3141 method scrollpw = (0, 0.0, 0.0)
3142 method scrollph =
3143 let nfs = fstate.fontsize + 1 in
3144 let y = m_first * nfs in
3145 let itemcount = source#getitemcount in
3146 let maxi = max 0 (itemcount - fstate.maxrows) in
3147 let maxy = maxi * nfs in
3148 let p, h = scrollph y maxy in
3149 conf.scrollbw, p, h
3151 method modehash = modehash
3152 method eformsgs = false
3153 end;;
3155 class outlinelistview ~zebra ~source =
3156 let settext autonarrow s =
3157 if autonarrow
3158 then
3159 let ss = source#statestr in
3160 state.text <-
3161 if emptystr ss
3162 then "[" ^ s ^ "]"
3163 else "{" ^ ss ^ "} [" ^ s ^ "]"
3164 else state.text <- s
3166 object (self)
3167 inherit listview
3168 ~zebra
3169 ~helpmode:false
3170 ~source:(source :> lvsource)
3171 ~trusted:false
3172 ~modehash:(findkeyhash conf "outline")
3173 as super
3175 val m_autonarrow = false
3177 method! key key mask =
3178 let maxrows =
3179 if emptystr state.text
3180 then fstate.maxrows
3181 else fstate.maxrows - 2
3183 let calcfirst first active =
3184 if active > first
3185 then
3186 let rows = active - first in
3187 if rows > maxrows then active - maxrows else first
3188 else active
3190 let navigate incr =
3191 let active = m_active + incr in
3192 let active = bound active 0 (source#getitemcount - 1) in
3193 let first = calcfirst m_first active in
3194 G.postRedisplay "outline navigate";
3195 coe {< m_active = active; m_first = first >}
3197 let navscroll first =
3198 let active =
3199 let dist = m_active - first in
3200 if dist < 0
3201 then first
3202 else (
3203 if dist < maxrows
3204 then m_active
3205 else first + maxrows
3208 G.postRedisplay "outline navscroll";
3209 coe {< m_first = first; m_active = active >}
3211 let ctrl = Wsi.withctrl mask in
3212 match key with
3213 | @a when ctrl ->
3214 let text =
3215 if m_autonarrow
3216 then (source#denarrow; E.s)
3217 else (
3218 let pattern = source#renarrow in
3219 if nonemptystr m_qsearch
3220 then (source#narrow m_qsearch; m_qsearch)
3221 else pattern
3224 settext (not m_autonarrow) text;
3225 G.postRedisplay "toggle auto narrowing";
3226 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
3228 | @slash when emptystr m_qsearch && not m_autonarrow ->
3229 settext true E.s;
3230 G.postRedisplay "toggle auto narrowing";
3231 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
3233 | @n when ctrl ->
3234 source#narrow m_qsearch;
3235 if not m_autonarrow
3236 then source#add_narrow_pattern m_qsearch;
3237 G.postRedisplay "outline ctrl-n";
3238 coe {< m_first = 0; m_active = 0 >}
3240 | @S when ctrl ->
3241 let active = source#calcactive (getanchor ()) in
3242 let first = firstof m_first active in
3243 G.postRedisplay "outline ctrl-s";
3244 coe {< m_first = first; m_active = active >}
3246 | @u when ctrl ->
3247 G.postRedisplay "outline ctrl-u";
3248 if m_autonarrow && nonemptystr m_qsearch
3249 then (
3250 ignore (source#renarrow);
3251 settext m_autonarrow E.s;
3252 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3254 else (
3255 source#del_narrow_pattern;
3256 let pattern = source#renarrow in
3257 let text =
3258 if emptystr pattern then E.s else "Narrowed to " ^ pattern
3260 settext m_autonarrow text;
3261 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3264 | @l when ctrl ->
3265 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3266 G.postRedisplay "outline ctrl-l";
3267 coe {< m_first = first >}
3269 | @tab when m_autonarrow ->
3270 if nonemptystr m_qsearch
3271 then (
3272 G.postRedisplay "outline list view tab";
3273 source#add_narrow_pattern m_qsearch;
3274 settext true E.s;
3275 coe {< m_qsearch = E.s >}
3277 else coe self
3279 | @escape when m_autonarrow ->
3280 if nonemptystr m_qsearch
3281 then source#add_narrow_pattern m_qsearch;
3282 super#key key mask
3284 | @enter | @kpenter when m_autonarrow ->
3285 if nonemptystr m_qsearch
3286 then source#add_narrow_pattern m_qsearch;
3287 super#key key mask
3289 | key when m_autonarrow && (key != 0 && key land 0xff00 != 0xff00) ->
3290 let pattern = m_qsearch ^ toutf8 key in
3291 G.postRedisplay "outlinelistview autonarrow add";
3292 source#narrow pattern;
3293 settext true pattern;
3294 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3296 | key when m_autonarrow && key = @backspace ->
3297 if emptystr m_qsearch
3298 then coe self
3299 else
3300 let pattern = withoutlastutf8 m_qsearch in
3301 G.postRedisplay "outlinelistview autonarrow backspace";
3302 ignore (source#renarrow);
3303 source#narrow pattern;
3304 settext true pattern;
3305 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3307 | @delete | @kpdelete ->
3308 source#remove m_active;
3309 G.postRedisplay "outline delete";
3310 let active = max 0 (m_active-1) in
3311 coe {< m_first = firstof m_first active;
3312 m_active = active >}
3314 | @up | @kpup when ctrl ->
3315 navscroll (max 0 (m_first - 1))
3317 | @down | @kpdown when ctrl ->
3318 navscroll (min (source#getitemcount - 1) (m_first + 1))
3320 | @up | @kpup -> navigate ~-1
3321 | @down | @kpdown -> navigate 1
3322 | @prior | @kpprior -> navigate ~-(fstate.maxrows)
3323 | @next | @kpnext -> navigate fstate.maxrows
3325 | @right | @kpright ->
3326 let o =
3327 if ctrl
3328 then (
3329 G.postRedisplay "outline ctrl right";
3330 {< m_pan = m_pan + 1 >}
3332 else self#updownlevel 1
3334 coe o
3336 | @left | @kpleft ->
3337 let o =
3338 if ctrl
3339 then (
3340 G.postRedisplay "outline ctrl left";
3341 {< m_pan = m_pan - 1 >}
3343 else self#updownlevel ~-1
3345 coe o
3347 | @home | @kphome ->
3348 G.postRedisplay "outline home";
3349 coe {< m_first = 0; m_active = 0 >}
3351 | @jend | @kpend ->
3352 let active = source#getitemcount - 1 in
3353 let first = max 0 (active - fstate.maxrows) in
3354 G.postRedisplay "outline end";
3355 coe {< m_active = active; m_first = first >}
3357 | _ -> super#key key mask
3360 let gotounder under =
3361 let getpath filename =
3362 let path =
3363 if nonemptystr filename
3364 then
3365 if Filename.is_relative filename
3366 then
3367 let dir = Filename.dirname state.path in
3368 let dir =
3369 if Filename.is_implicit dir
3370 then Filename.concat (Sys.getcwd ()) dir
3371 else dir
3373 Filename.concat dir filename
3374 else filename
3375 else E.s
3377 if Sys.file_exists path
3378 then path
3379 else E.s
3381 match under with
3382 | Ulinkgoto (pageno, top) ->
3383 if pageno >= 0
3384 then (
3385 addnav ();
3386 gotopage1 pageno top;
3389 | Ulinkuri s ->
3390 gotouri s
3392 | Uremote (filename, pageno) ->
3393 let path = getpath filename in
3394 if nonemptystr path
3395 then (
3396 if conf.riani
3397 then
3398 let command = Printf.sprintf "%s -page %d %S" !selfexec pageno path in
3399 try popen command []
3400 with exn ->
3401 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
3402 flush stderr;
3403 else
3404 let anchor = getanchor () in
3405 let ranchor = state.path, state.password, anchor, state.origin in
3406 state.origin <- E.s;
3407 state.anchor <- (pageno, 0.0, 0.0);
3408 state.ranchors <- ranchor :: state.ranchors;
3409 opendoc path E.s;
3411 else showtext '!' ("Could not find " ^ filename)
3413 | Uremotedest (filename, destname) ->
3414 let path = getpath filename in
3415 if nonemptystr path
3416 then (
3417 if conf.riani
3418 then
3419 let command = !selfexec ^ " " ^ path ^ " -dest " ^ destname in
3420 try popen command []
3421 with exn ->
3422 Printf.eprintf
3423 "failed to execute `%s': %s\n" command (exntos exn);
3424 flush stderr;
3425 else
3426 let anchor = getanchor () in
3427 let ranchor = state.path, state.password, anchor, state.origin in
3428 state.origin <- E.s;
3429 state.nameddest <- destname;
3430 state.ranchors <- ranchor :: state.ranchors;
3431 opendoc path E.s;
3433 else showtext '!' ("Could not find " ^ filename)
3435 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
3438 let gotohist (path, (c, bookmarks, x, anchor)) =
3439 Config.save leavebirdseye;
3440 state.anchor <- anchor;
3441 state.x <- x;
3442 state.bookmarks <- bookmarks;
3443 state.origin <- E.s;
3444 setconf conf c;
3445 let x0, y0, x1, y1 = conf.trimfuzz in
3446 wcmd "trimset %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1;
3447 opendoc path E.s;
3450 let gotooutline (_, _, kind) =
3451 match kind with
3452 | Onone -> ()
3453 | Oanchor anchor ->
3454 let (pageno, y, _) = anchor in
3455 let y = getanchory
3456 (if conf.presentation then (pageno, y, 1.0) else anchor)
3458 addnav ();
3459 gotoghyll y
3460 | Ouri uri -> gotounder (Ulinkuri uri)
3461 | Olaunch cmd -> gotounder (Ulaunch cmd)
3462 | Oremote remote -> gotounder (Uremote remote)
3463 | Ohistory hist -> gotohist hist
3464 | Oremotedest remotedest -> gotounder (Uremotedest remotedest)
3465 | Oaction f -> f ()
3468 let genhistoutlines =
3469 let order ty (p1, c1, _, _, _) (p2, c2, _, _, _) =
3470 match ty with
3471 | `lastvisit -> compare c1.lastvisit c2.lastvisit
3472 | `path -> compare p2 p1
3473 | `file -> compare (Filename.basename p2) (Filename.basename p1)
3474 | `title ->
3475 let e1 = emptystr c1.title
3476 and e2 = emptystr c2.title in
3477 (**) if e1 && e2
3478 then compare (Filename.basename p2) (Filename.basename p1)
3479 else if e1 then -1
3480 else if e2 then 1
3481 else compare c1.title c2.title
3483 let showfullpath = ref false in
3484 fun orderty ->
3485 let setorty s t =
3486 let s = if orderty = t then "[@Uradical] " ^ s else "[ ] " ^ s in
3487 s, 0, Oaction (fun () -> Config.historder := t; reeenterhist := true)
3489 let list = ref [] in
3490 if Config.gethist list
3491 then
3492 let ol =
3493 List.fold_left
3494 (fun accu (path, c, b, x, a) ->
3495 let hist = (path, (c, b, x, a)) in
3496 let s = if !showfullpath then path else Filename.basename path in
3497 let base = mbtoutf8 s in
3498 (base ^ "\000" ^ c.title, 1, Ohistory hist) :: accu
3500 [ setorty "Sort by time of last visit" `lastvisit;
3501 setorty "Sort by file name" `file;
3502 setorty "Sort by path" `path;
3503 setorty "Sort by title" `title;
3504 (if !showfullpath then "@Uradical "
3505 else " ") ^ "Show full path", 0, Oaction (fun () ->
3506 showfullpath := not !showfullpath; reeenterhist := true)
3507 ] (List.sort (order orderty) !list)
3509 Array.of_list ol
3510 else E.a;
3513 let outlinesource sourcetype =
3514 (object (self)
3515 inherit lvsourcebase
3516 val mutable m_items = E.a
3517 val mutable m_minfo = E.a
3518 val mutable m_orig_items = E.a
3519 val mutable m_orig_minfo = E.a
3520 val mutable m_narrow_patterns = []
3521 val mutable m_hadremovals = false
3522 val mutable m_gen = -1
3524 method getitemcount =
3525 Array.length m_items + (if m_hadremovals then 1 else 0)
3527 method getitem n =
3528 if n == Array.length m_items && m_hadremovals
3529 then
3530 ("[Confirm removal]", 0)
3531 else
3532 let s, n, _ = m_items.(n) in
3533 (s, n)
3535 method exit ~uioh ~cancel ~active ~first ~pan =
3536 ignore (uioh, first);
3537 let confrimremoval = m_hadremovals && active = Array.length m_items in
3538 let items, minfo =
3539 if m_narrow_patterns = []
3540 then m_orig_items, m_orig_minfo
3541 else m_items, m_minfo
3543 if not cancel
3544 then (
3545 if not confrimremoval
3546 then (
3547 gotooutline m_items.(active);
3548 m_items <- items;
3549 m_minfo <- minfo;
3551 else (
3552 state.bookmarks <- Array.to_list m_items;
3553 m_orig_items <- m_items;
3554 m_orig_minfo <- m_minfo;
3557 else (
3558 m_items <- items;
3559 m_minfo <- minfo;
3561 m_pan <- pan;
3562 None
3564 method hasaction _ = true
3566 method greetmsg =
3567 if Array.length m_items != Array.length m_orig_items
3568 then
3569 let s =
3570 match m_narrow_patterns with
3571 | one :: [] -> one
3572 | many -> String.concat "@Uellipsis" (List.rev many)
3574 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
3575 else E.s
3577 method statestr =
3578 match m_narrow_patterns with
3579 | [] -> E.s
3580 | one :: [] -> one
3581 | head :: _ -> "@Uellipsis" ^ head
3583 method narrow pattern =
3584 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3585 match reopt with
3586 | None -> ()
3587 | Some re ->
3588 let rec loop accu minfo n =
3589 if n = -1
3590 then (
3591 m_items <- Array.of_list accu;
3592 m_minfo <- Array.of_list minfo;
3594 else
3595 let (s, _, t) as o = m_items.(n) in
3596 let accu, minfo =
3597 match t with
3598 | Oaction _ -> o :: accu, (0, 0) :: minfo
3599 | Onone | Oanchor _ | Ouri _ | Olaunch _
3600 | Oremote _ | Oremotedest _ | Ohistory _ ->
3601 let first =
3602 try Str.search_forward re s 0
3603 with Not_found -> -1
3605 if first >= 0
3606 then o :: accu, (first, Str.match_end ()) :: minfo
3607 else accu, minfo
3609 loop accu minfo (n-1)
3611 loop [] [] (Array.length m_items - 1)
3613 method! getminfo = m_minfo
3615 method denarrow =
3616 m_orig_items <- (
3617 match sourcetype with
3618 | `bookmarks -> Array.of_list state.bookmarks
3619 | `outlines -> state.outlines
3620 | `history -> genhistoutlines !Config.historder
3622 m_minfo <- m_orig_minfo;
3623 m_items <- m_orig_items
3625 method remove m =
3626 if sourcetype = `bookmarks
3627 then
3628 if m >= 0 && m < Array.length m_items
3629 then (
3630 m_hadremovals <- true;
3631 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3632 let n = if n >= m then n+1 else n in
3633 m_items.(n)
3637 method add_narrow_pattern pattern =
3638 m_narrow_patterns <- pattern :: m_narrow_patterns
3640 method del_narrow_pattern =
3641 match m_narrow_patterns with
3642 | _ :: rest -> m_narrow_patterns <- rest
3643 | [] -> ()
3645 method renarrow =
3646 self#denarrow;
3647 match m_narrow_patterns with
3648 | pattern :: [] -> self#narrow pattern; pattern
3649 | list ->
3650 List.fold_left (fun accu pattern ->
3651 self#narrow pattern;
3652 pattern ^ "@Uellipsis" ^ accu) E.s list
3654 method calcactive anchor =
3655 let rely = getanchory anchor in
3656 let rec loop n best bestd =
3657 if n = Array.length m_items
3658 then best
3659 else
3660 let _, _, kind = m_items.(n) in
3661 match kind with
3662 | Oanchor anchor ->
3663 let orely = getanchory anchor in
3664 let d = abs (orely - rely) in
3665 if d < bestd
3666 then loop (n+1) n d
3667 else loop (n+1) best bestd
3668 | Onone | Oremote _ | Olaunch _
3669 | Oremotedest _ | Ouri _ | Ohistory _ | Oaction _ ->
3670 loop (n+1) best bestd
3672 loop 0 ~-1 max_int
3674 method reset anchor items =
3675 m_hadremovals <- false;
3676 if state.gen != m_gen
3677 then (
3678 m_orig_items <- items;
3679 m_items <- items;
3680 m_narrow_patterns <- [];
3681 m_minfo <- E.a;
3682 m_orig_minfo <- E.a;
3683 m_gen <- state.gen;
3685 else (
3686 if items != m_orig_items
3687 then (
3688 m_orig_items <- items;
3689 if m_narrow_patterns == []
3690 then m_items <- items;
3693 let active = self#calcactive anchor in
3694 m_active <- active;
3695 m_first <- firstof m_first active
3696 end)
3699 let enterselector sourcetype =
3700 resetmstate ();
3701 let source = outlinesource sourcetype in
3702 fun errmsg ->
3703 let outlines =
3704 match sourcetype with
3705 | `bookmarks -> Array.of_list state.bookmarks
3706 | `outlines -> state.outlines
3707 | `history -> genhistoutlines !Config.historder
3709 if Array.length outlines = 0
3710 then (
3711 showtext ' ' errmsg;
3713 else (
3714 state.text <- source#greetmsg;
3715 Wsi.setcursor Wsi.CURSOR_INHERIT;
3716 let anchor = getanchor () in
3717 source#reset anchor outlines;
3718 state.uioh <-
3719 coe (new outlinelistview ~zebra:(sourcetype=`history) ~source);
3720 G.postRedisplay "enter selector";
3724 let enteroutlinemode =
3725 let f = enterselector `outlines in
3726 fun () -> f "Document has no outline";
3729 let enterbookmarkmode =
3730 let f = enterselector `bookmarks in
3731 fun () -> f "Document has no bookmarks (yet)";
3734 let enterhistmode () = enterselector `history "No history (yet)";;
3736 let makecheckers () =
3737 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3738 following to say:
3739 converted by Issac Trotts. July 25, 2002 *)
3740 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3741 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3742 let id = GlTex.gen_texture () in
3743 GlTex.bind_texture ~target:`texture_2d id;
3744 GlPix.store (`unpack_alignment 1);
3745 GlTex.image2d image;
3746 List.iter (GlTex.parameter ~target:`texture_2d)
3747 [ `mag_filter `nearest; `min_filter `nearest ];
3751 let setcheckers enabled =
3752 match state.checkerstexid with
3753 | None ->
3754 if enabled then state.checkerstexid <- Some (makecheckers ())
3756 | Some checkerstexid ->
3757 if not enabled
3758 then (
3759 GlTex.delete_texture checkerstexid;
3760 state.checkerstexid <- None;
3764 let describe_location () =
3765 let fn = page_of_y state.y in
3766 let ln = page_of_y (state.y + state.winh - hscrollh () - 1) in
3767 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
3768 let percent =
3769 if maxy <= 0
3770 then 100.
3771 else (100. *. (float state.y /. float maxy))
3773 if fn = ln
3774 then
3775 Printf.sprintf "page %d of %d [%.2f%%]"
3776 (fn+1) state.pagecount percent
3777 else
3778 Printf.sprintf
3779 "pages %d-%d of %d [%.2f%%]"
3780 (fn+1) (ln+1) state.pagecount percent
3783 let setpresentationmode v =
3784 let n = page_of_y state.y in
3785 state.anchor <- (n, 0.0, 1.0);
3786 conf.presentation <- v;
3787 if conf.fitmodel = FitPage
3788 then reqlayout conf.angle conf.fitmodel;
3789 represent ();
3792 let enterinfomode =
3793 let btos b = if b then "@Uradical" else E.s in
3794 let showextended = ref false in
3795 let leave mode _ = state.mode <- mode in
3796 let src =
3797 (object
3798 val mutable m_first_time = true
3799 val mutable m_l = []
3800 val mutable m_a = E.a
3801 val mutable m_prev_uioh = nouioh
3802 val mutable m_prev_mode = View
3804 inherit lvsourcebase
3806 method reset prev_mode prev_uioh =
3807 m_a <- Array.of_list (List.rev m_l);
3808 m_l <- [];
3809 m_prev_mode <- prev_mode;
3810 m_prev_uioh <- prev_uioh;
3811 if m_first_time
3812 then (
3813 let rec loop n =
3814 if n >= Array.length m_a
3815 then ()
3816 else
3817 match m_a.(n) with
3818 | _, _, _, Action _ -> m_active <- n
3819 | _, _, _, Noaction -> loop (n+1)
3821 loop 0;
3822 m_first_time <- false;
3825 method int name get set =
3826 m_l <-
3827 (name, `int get, 1, Action (
3828 fun u ->
3829 let ondone s =
3830 try set (int_of_string s)
3831 with exn ->
3832 state.text <- Printf.sprintf "bad integer `%s': %s"
3833 s (exntos exn)
3835 state.text <- E.s;
3836 let te = name ^ ": ", E.s, None, intentry, ondone, true in
3837 state.mode <- Textentry (te, leave m_prev_mode);
3839 )) :: m_l
3841 method int_with_suffix name get set =
3842 m_l <-
3843 (name, `intws get, 1, Action (
3844 fun u ->
3845 let ondone s =
3846 try set (int_of_string_with_suffix s)
3847 with exn ->
3848 state.text <- Printf.sprintf "bad integer `%s': %s"
3849 s (exntos exn)
3851 state.text <- E.s;
3852 let te =
3853 name ^ ": ", E.s, None, intentry_with_suffix, ondone, true
3855 state.mode <- Textentry (te, leave m_prev_mode);
3857 )) :: m_l
3859 method bool ?(offset=1) ?(btos=btos) name get set =
3860 m_l <-
3861 (name, `bool (btos, get), offset, Action (
3862 fun u ->
3863 let v = get () in
3864 set (not v);
3866 )) :: m_l
3868 method color name get set =
3869 m_l <-
3870 (name, `color get, 1, Action (
3871 fun u ->
3872 let invalid = (nan, nan, nan) in
3873 let ondone s =
3874 let c =
3875 try color_of_string s
3876 with exn ->
3877 state.text <- Printf.sprintf "bad color `%s': %s"
3878 s (exntos exn);
3879 invalid
3881 if c <> invalid
3882 then set c;
3884 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3885 state.text <- color_to_string (get ());
3886 state.mode <- Textentry (te, leave m_prev_mode);
3888 )) :: m_l
3890 method string name get set =
3891 m_l <-
3892 (name, `string get, 1, Action (
3893 fun u ->
3894 let ondone s = set s in
3895 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3896 state.mode <- Textentry (te, leave m_prev_mode);
3898 )) :: m_l
3900 method colorspace name get set =
3901 m_l <-
3902 (name, `string get, 1, Action (
3903 fun _ ->
3904 let source =
3905 (object
3906 inherit lvsourcebase
3908 initializer
3909 m_active <- CSTE.to_int conf.colorspace;
3910 m_first <- 0;
3912 method getitemcount =
3913 Array.length CSTE.names
3914 method getitem n =
3915 (CSTE.names.(n), 0)
3916 method exit ~uioh ~cancel ~active ~first ~pan =
3917 ignore (uioh, first, pan);
3918 if not cancel then set active;
3919 None
3920 method hasaction _ = true
3921 end)
3923 state.text <- E.s;
3924 let modehash = findkeyhash conf "info" in
3925 coe (new listview ~zebra:false ~helpmode:false
3926 ~source ~trusted:true ~modehash)
3927 )) :: m_l
3929 method paxmark name get set =
3930 m_l <-
3931 (name, `string get, 1, Action (
3932 fun _ ->
3933 let source =
3934 (object
3935 inherit lvsourcebase
3937 initializer
3938 m_active <- MTE.to_int conf.paxmark;
3939 m_first <- 0;
3941 method getitemcount = Array.length MTE.names
3942 method getitem n = (MTE.names.(n), 0)
3943 method exit ~uioh ~cancel ~active ~first ~pan =
3944 ignore (uioh, first, pan);
3945 if not cancel then set active;
3946 None
3947 method hasaction _ = true
3948 end)
3950 state.text <- E.s;
3951 let modehash = findkeyhash conf "info" in
3952 coe (new listview ~zebra:false ~helpmode:false
3953 ~source ~trusted:true ~modehash)
3954 )) :: m_l
3956 method fitmodel name get set =
3957 m_l <-
3958 (name, `string get, 1, Action (
3959 fun _ ->
3960 let source =
3961 (object
3962 inherit lvsourcebase
3964 initializer
3965 m_active <- FMTE.to_int conf.fitmodel;
3966 m_first <- 0;
3968 method getitemcount = Array.length FMTE.names
3969 method getitem n = (FMTE.names.(n), 0)
3970 method exit ~uioh ~cancel ~active ~first ~pan =
3971 ignore (uioh, first, pan);
3972 if not cancel then set active;
3973 None
3974 method hasaction _ = true
3975 end)
3977 state.text <- E.s;
3978 let modehash = findkeyhash conf "info" in
3979 coe (new listview ~zebra:false ~helpmode:false
3980 ~source ~trusted:true ~modehash)
3981 )) :: m_l
3983 method caption s offset =
3984 m_l <- (s, `empty, offset, Noaction) :: m_l
3986 method caption2 s f offset =
3987 m_l <- (s, `string f, offset, Noaction) :: m_l
3989 method getitemcount = Array.length m_a
3991 method getitem n =
3992 let tostr = function
3993 | `int f -> string_of_int (f ())
3994 | `intws f -> string_with_suffix_of_int (f ())
3995 | `string f -> f ()
3996 | `color f -> color_to_string (f ())
3997 | `bool (btos, f) -> btos (f ())
3998 | `empty -> E.s
4000 let name, t, offset, _ = m_a.(n) in
4001 ((let s = tostr t in
4002 if nonemptystr s
4003 then Printf.sprintf "%s\t%s" name s
4004 else name),
4005 offset)
4007 method exit ~uioh ~cancel ~active ~first ~pan =
4008 let uiohopt =
4009 if not cancel
4010 then (
4011 let uioh =
4012 match m_a.(active) with
4013 | _, _, _, Action f -> f uioh
4014 | _, _, _, Noaction -> uioh
4016 Some uioh
4018 else None
4020 m_active <- active;
4021 m_first <- first;
4022 m_pan <- pan;
4023 uiohopt
4025 method hasaction n =
4026 match m_a.(n) with
4027 | _, _, _, Action _ -> true
4028 | _, _, _, Noaction -> false
4029 end)
4031 let rec fillsrc prevmode prevuioh =
4032 let sep () = src#caption E.s 0 in
4033 let colorp name get set =
4034 src#string name
4035 (fun () -> color_to_string (get ()))
4036 (fun v ->
4038 let c = color_of_string v in
4039 set c
4040 with exn ->
4041 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
4044 let oldmode = state.mode in
4045 let birdseye = isbirdseye state.mode in
4047 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4049 src#bool "presentation mode"
4050 (fun () -> conf.presentation)
4051 (fun v -> setpresentationmode v);
4053 src#bool "ignore case in searches"
4054 (fun () -> conf.icase)
4055 (fun v -> conf.icase <- v);
4057 src#bool "preload"
4058 (fun () -> conf.preload)
4059 (fun v -> conf.preload <- v);
4061 src#bool "highlight links"
4062 (fun () -> conf.hlinks)
4063 (fun v -> conf.hlinks <- v);
4065 src#bool "under info"
4066 (fun () -> conf.underinfo)
4067 (fun v -> conf.underinfo <- v);
4069 src#bool "persistent bookmarks"
4070 (fun () -> conf.savebmarks)
4071 (fun v -> conf.savebmarks <- v);
4073 src#fitmodel "fit model"
4074 (fun () -> FMTE.to_string conf.fitmodel)
4075 (fun v -> reqlayout conf.angle (FMTE.of_int v));
4077 src#bool "trim margins"
4078 (fun () -> conf.trimmargins)
4079 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4081 src#bool "persistent location"
4082 (fun () -> conf.jumpback)
4083 (fun v -> conf.jumpback <- v);
4085 sep ();
4086 src#int "inter-page space"
4087 (fun () -> conf.interpagespace)
4088 (fun n ->
4089 conf.interpagespace <- n;
4090 docolumns conf.columns;
4091 let pageno, py =
4092 match state.layout with
4093 | [] -> 0, 0
4094 | l :: _ ->
4095 l.pageno, l.pagey
4097 state.maxy <- calcheight ();
4098 let y = getpagey pageno in
4099 gotoy (y + py)
4102 src#int "page bias"
4103 (fun () -> conf.pagebias)
4104 (fun v -> conf.pagebias <- v);
4106 src#int "scroll step"
4107 (fun () -> conf.scrollstep)
4108 (fun n -> conf.scrollstep <- n);
4110 src#int "horizontal scroll step"
4111 (fun () -> conf.hscrollstep)
4112 (fun v -> conf.hscrollstep <- v);
4114 src#int "auto scroll step"
4115 (fun () ->
4116 match state.autoscroll with
4117 | Some step -> step
4118 | _ -> conf.autoscrollstep)
4119 (fun n ->
4120 let n = boundastep state.winh n in
4121 if state.autoscroll <> None
4122 then state.autoscroll <- Some n;
4123 conf.autoscrollstep <- n);
4125 src#int "zoom"
4126 (fun () -> truncate (conf.zoom *. 100.))
4127 (fun v -> setzoom ((float v) /. 100.));
4129 src#int "rotation"
4130 (fun () -> conf.angle)
4131 (fun v -> reqlayout v conf.fitmodel);
4133 src#int "scroll bar width"
4134 (fun () -> conf.scrollbw)
4135 (fun v ->
4136 conf.scrollbw <- v;
4137 reshape state.winw state.winh;
4140 src#int "scroll handle height"
4141 (fun () -> conf.scrollh)
4142 (fun v -> conf.scrollh <- v;);
4144 src#int "thumbnail width"
4145 (fun () -> conf.thumbw)
4146 (fun v ->
4147 conf.thumbw <- min 4096 v;
4148 match oldmode with
4149 | Birdseye beye ->
4150 leavebirdseye beye false;
4151 enterbirdseye ()
4152 | Textentry _
4153 | View
4154 | LinkNav _ -> ()
4157 let mode = state.mode in
4158 src#string "columns"
4159 (fun () ->
4160 match conf.columns with
4161 | Csingle _ -> "1"
4162 | Cmulti (multi, _) -> multicolumns_to_string multi
4163 | Csplit (count, _) -> "-" ^ string_of_int count
4165 (fun v ->
4166 let n, a, b = multicolumns_of_string v in
4167 setcolumns mode n a b);
4169 sep ();
4170 src#caption "Pixmap cache" 0;
4171 src#int_with_suffix "size (advisory)"
4172 (fun () -> conf.memlimit)
4173 (fun v -> conf.memlimit <- v);
4175 src#caption2 "used"
4176 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4177 (string_with_suffix_of_int state.memused)
4178 (Hashtbl.length state.tilemap)) 1;
4180 sep ();
4181 src#caption "Layout" 0;
4182 src#caption2 "Dimension"
4183 (fun () ->
4184 Printf.sprintf "%dx%d (virtual %dx%d)"
4185 state.winw state.winh
4186 state.w state.maxy)
4188 if conf.debug
4189 then
4190 src#caption2 "Position" (fun () ->
4191 Printf.sprintf "%dx%d" state.x state.y
4193 else
4194 src#caption2 "Position" (fun () -> describe_location ()) 1
4197 sep ();
4198 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4199 "Save these parameters as global defaults at exit"
4200 (fun () -> conf.bedefault)
4201 (fun v -> conf.bedefault <- v)
4204 sep ();
4205 let btos b = if b then "@Ulguillemet" else "@Urguillemet" in
4206 src#bool ~offset:0 ~btos "Extended parameters"
4207 (fun () -> !showextended)
4208 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4209 if !showextended
4210 then (
4211 src#bool "checkers"
4212 (fun () -> conf.checkers)
4213 (fun v -> conf.checkers <- v; setcheckers v);
4214 src#bool "update cursor"
4215 (fun () -> conf.updatecurs)
4216 (fun v -> conf.updatecurs <- v);
4217 src#bool "scroll-bar on the left"
4218 (fun () -> conf.leftscroll)
4219 (fun v -> conf.leftscroll <- v);
4220 src#bool "verbose"
4221 (fun () -> conf.verbose)
4222 (fun v -> conf.verbose <- v);
4223 src#bool "invert colors"
4224 (fun () -> conf.invert)
4225 (fun v -> conf.invert <- v);
4226 src#bool "max fit"
4227 (fun () -> conf.maxhfit)
4228 (fun v -> conf.maxhfit <- v);
4229 src#bool "redirect stderr"
4230 (fun () -> conf.redirectstderr)
4231 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4232 src#bool "pax mode"
4233 (fun () -> conf.pax != None)
4234 (fun v ->
4235 if v
4236 then conf.pax <- Some (ref (now (), 0, 0))
4237 else conf.pax <- None);
4238 src#string "uri launcher"
4239 (fun () -> conf.urilauncher)
4240 (fun v -> conf.urilauncher <- v);
4241 src#string "path launcher"
4242 (fun () -> conf.pathlauncher)
4243 (fun v -> conf.pathlauncher <- v);
4244 src#string "tile size"
4245 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4246 (fun v ->
4248 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4249 conf.tilew <- max 64 w;
4250 conf.tileh <- max 64 h;
4251 flushtiles ();
4252 with exn ->
4253 state.text <- Printf.sprintf "bad tile size `%s': %s"
4254 v (exntos exn)
4256 src#int "texture count"
4257 (fun () -> conf.texcount)
4258 (fun v ->
4259 if realloctexts v
4260 then conf.texcount <- v
4261 else showtext '!' " Failed to set texture count please retry later"
4263 src#int "slice height"
4264 (fun () -> conf.sliceheight)
4265 (fun v ->
4266 conf.sliceheight <- v;
4267 wcmd "sliceh %d" conf.sliceheight;
4269 src#int "anti-aliasing level"
4270 (fun () -> conf.aalevel)
4271 (fun v ->
4272 conf.aalevel <- bound v 0 8;
4273 state.anchor <- getanchor ();
4274 opendoc state.path state.password;
4276 src#string "page scroll scaling factor"
4277 (fun () -> string_of_float conf.pgscale)
4278 (fun v ->
4280 let s = float_of_string v in
4281 conf.pgscale <- s
4282 with exn ->
4283 state.text <- Printf.sprintf
4284 "bad page scroll scaling factor `%s': %s" v (exntos exn)
4287 src#int "ui font size"
4288 (fun () -> fstate.fontsize)
4289 (fun v -> setfontsize (bound v 5 100));
4290 src#int "hint font size"
4291 (fun () -> conf.hfsize)
4292 (fun v -> conf.hfsize <- bound v 5 100);
4293 colorp "background color"
4294 (fun () -> conf.bgcolor)
4295 (fun v -> conf.bgcolor <- v);
4296 src#bool "crop hack"
4297 (fun () -> conf.crophack)
4298 (fun v -> conf.crophack <- v);
4299 src#string "trim fuzz"
4300 (fun () -> irect_to_string conf.trimfuzz)
4301 (fun v ->
4303 conf.trimfuzz <- irect_of_string v;
4304 if conf.trimmargins
4305 then settrim true conf.trimfuzz;
4306 with exn ->
4307 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
4309 src#string "throttle"
4310 (fun () ->
4311 match conf.maxwait with
4312 | None -> "show place holder if page is not ready"
4313 | Some time ->
4314 if time = infinity
4315 then "wait for page to fully render"
4316 else
4317 "wait " ^ string_of_float time
4318 ^ " seconds before showing placeholder"
4320 (fun v ->
4322 let f = float_of_string v in
4323 if f <= 0.0
4324 then conf.maxwait <- None
4325 else conf.maxwait <- Some f
4326 with exn ->
4327 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
4329 src#string "ghyll scroll"
4330 (fun () ->
4331 match conf.ghyllscroll with
4332 | None -> E.s
4333 | Some nab -> ghyllscroll_to_string nab
4335 (fun v ->
4336 try conf.ghyllscroll <- ghyllscroll_of_string v
4337 with exn ->
4338 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
4340 src#string "selection command"
4341 (fun () -> conf.selcmd)
4342 (fun v -> conf.selcmd <- v);
4343 src#string "synctex command"
4344 (fun () -> conf.stcmd)
4345 (fun v -> conf.stcmd <- v);
4346 src#string "pax command"
4347 (fun () -> conf.paxcmd)
4348 (fun v -> conf.paxcmd <- v);
4349 src#colorspace "color space"
4350 (fun () -> CSTE.to_string conf.colorspace)
4351 (fun v ->
4352 conf.colorspace <- CSTE.of_int v;
4353 wcmd "cs %d" v;
4354 load state.layout;
4356 src#paxmark "pax mark method"
4357 (fun () -> MTE.to_string conf.paxmark)
4358 (fun v -> conf.paxmark <- MTE.of_int v);
4359 if pbousable ()
4360 then
4361 src#bool "use PBO"
4362 (fun () -> conf.usepbo)
4363 (fun v -> conf.usepbo <- v);
4364 src#bool "mouse wheel scrolls pages"
4365 (fun () -> conf.wheelbypage)
4366 (fun v -> conf.wheelbypage <- v);
4367 src#bool "open remote links in a new instance"
4368 (fun () -> conf.riani)
4369 (fun v -> conf.riani <- v);
4372 sep ();
4373 src#caption "Document" 0;
4374 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4375 src#caption2 "Pages"
4376 (fun () -> string_of_int state.pagecount) 1;
4377 src#caption2 "Dimensions"
4378 (fun () -> string_of_int (List.length state.pdims)) 1;
4379 if conf.trimmargins
4380 then (
4381 sep ();
4382 src#caption "Trimmed margins" 0;
4383 src#caption2 "Dimensions"
4384 (fun () -> string_of_int (List.length state.pdims)) 1;
4387 sep ();
4388 src#caption "OpenGL" 0;
4389 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4390 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4392 sep ();
4393 src#caption "Location" 0;
4394 if nonemptystr state.origin
4395 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
4396 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
4398 src#reset prevmode prevuioh;
4400 fun () ->
4401 state.text <- E.s;
4402 resetmstate ();
4403 let prevmode = state.mode
4404 and prevuioh = state.uioh in
4405 fillsrc prevmode prevuioh;
4406 let source = (src :> lvsource) in
4407 let modehash = findkeyhash conf "info" in
4408 state.uioh <- coe (object (self)
4409 inherit listview ~zebra:false ~helpmode:false
4410 ~source ~trusted:true ~modehash as super
4411 val mutable m_prevmemused = 0
4412 method! infochanged = function
4413 | Memused ->
4414 if m_prevmemused != state.memused
4415 then (
4416 m_prevmemused <- state.memused;
4417 G.postRedisplay "memusedchanged";
4419 | Pdim -> G.postRedisplay "pdimchanged"
4420 | Docinfo -> fillsrc prevmode prevuioh
4422 method! key key mask =
4423 if not (Wsi.withctrl mask)
4424 then
4425 match key with
4426 | @left | @kpleft -> coe (self#updownlevel ~-1)
4427 | @right | @kpright -> coe (self#updownlevel 1)
4428 | _ -> super#key key mask
4429 else super#key key mask
4430 end);
4431 G.postRedisplay "info";
4434 let enterhelpmode =
4435 let source =
4436 (object
4437 inherit lvsourcebase
4438 method getitemcount = Array.length state.help
4439 method getitem n =
4440 let s, l, _ = state.help.(n) in
4441 (s, l)
4443 method exit ~uioh ~cancel ~active ~first ~pan =
4444 let optuioh =
4445 if not cancel
4446 then (
4447 match state.help.(active) with
4448 | _, _, Action f -> Some (f uioh)
4449 | _, _, Noaction -> Some uioh
4451 else None
4453 m_active <- active;
4454 m_first <- first;
4455 m_pan <- pan;
4456 optuioh
4458 method hasaction n =
4459 match state.help.(n) with
4460 | _, _, Action _ -> true
4461 | _, _, Noaction -> false
4463 initializer
4464 m_active <- -1
4465 end)
4466 in fun () ->
4467 let modehash = findkeyhash conf "help" in
4468 resetmstate ();
4469 state.uioh <- coe (new listview
4470 ~zebra:false ~helpmode:true
4471 ~source ~trusted:true ~modehash);
4472 G.postRedisplay "help";
4475 let entermsgsmode =
4476 let msgsource =
4477 let re = Str.regexp "[\r\n]" in
4478 (object
4479 inherit lvsourcebase
4480 val mutable m_items = E.a
4482 method getitemcount = 1 + Array.length m_items
4484 method getitem n =
4485 if n = 0
4486 then "[Clear]", 0
4487 else m_items.(n-1), 0
4489 method exit ~uioh ~cancel ~active ~first ~pan =
4490 ignore uioh;
4491 if not cancel
4492 then (
4493 if active = 0
4494 then Buffer.clear state.errmsgs;
4496 m_active <- active;
4497 m_first <- first;
4498 m_pan <- pan;
4499 None
4501 method hasaction n =
4502 n = 0
4504 method reset =
4505 state.newerrmsgs <- false;
4506 let l = Str.split re (Buffer.contents state.errmsgs) in
4507 m_items <- Array.of_list l
4509 initializer
4510 m_active <- 0
4511 end)
4512 in fun () ->
4513 state.text <- E.s;
4514 resetmstate ();
4515 msgsource#reset;
4516 let source = (msgsource :> lvsource) in
4517 let modehash = findkeyhash conf "listview" in
4518 state.uioh <- coe (object
4519 inherit listview ~zebra:false ~helpmode:false
4520 ~source ~trusted:false ~modehash as super
4521 method! display =
4522 if state.newerrmsgs
4523 then msgsource#reset;
4524 super#display
4525 end);
4526 G.postRedisplay "msgs";
4529 let quickbookmark ?title () =
4530 match state.layout with
4531 | [] -> ()
4532 | l :: _ ->
4533 let title =
4534 match title with
4535 | None ->
4536 let tm = Unix.localtime (now ()) in
4537 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4538 (l.pageno+1)
4539 tm.Unix.tm_mday
4540 tm.Unix.tm_mon
4541 (tm.Unix.tm_year + 1900)
4542 tm.Unix.tm_hour
4543 tm.Unix.tm_min
4544 | Some title -> title
4546 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4549 let setautoscrollspeed step goingdown =
4550 let incr = max 1 ((abs step) / 2) in
4551 let incr = if goingdown then incr else -incr in
4552 let astep = boundastep state.winh (step + incr) in
4553 state.autoscroll <- Some astep;
4556 let canpan () =
4557 match conf.columns with
4558 | Csplit _ -> true
4559 | Csingle _ | Cmulti _ -> state.x != 0 || conf.zoom > 1.0
4562 let panbound x = bound x (-state.w) (wadjsb state.winw);;
4564 let existsinrow pageno (columns, coverA, coverB) p =
4565 let last = ((pageno - coverA) mod columns) + columns in
4566 let rec any = function
4567 | [] -> false
4568 | l :: rest ->
4569 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4570 then p l
4571 else (
4572 if not (p l)
4573 then (if l.pageno = last then false else any rest)
4574 else true
4577 any state.layout
4580 let nextpage () =
4581 match state.layout with
4582 | [] ->
4583 let pageno = page_of_y state.y in
4584 gotoghyll (getpagey (pageno+1))
4585 | l :: rest ->
4586 match conf.columns with
4587 | Csingle _ ->
4588 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4589 then
4590 let y = clamp (pgscale state.winh) in
4591 gotoghyll y
4592 else
4593 let pageno = min (l.pageno+1) (state.pagecount-1) in
4594 gotoghyll (getpagey pageno)
4595 | Cmulti ((c, _, _) as cl, _) ->
4596 if conf.presentation
4597 && (existsinrow l.pageno cl
4598 (fun l -> l.pageh > l.pagey + l.pagevh))
4599 then
4600 let y = clamp (pgscale state.winh) in
4601 gotoghyll y
4602 else
4603 let pageno = min (l.pageno+c) (state.pagecount-1) in
4604 gotoghyll (getpagey pageno)
4605 | Csplit (n, _) ->
4606 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4607 then
4608 let pagey, pageh = getpageyh l.pageno in
4609 let pagey = pagey + pageh * l.pagecol in
4610 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4611 gotoghyll (pagey + pageh + ips)
4614 let prevpage () =
4615 match state.layout with
4616 | [] ->
4617 let pageno = page_of_y state.y in
4618 gotoghyll (getpagey (pageno-1))
4619 | l :: _ ->
4620 match conf.columns with
4621 | Csingle _ ->
4622 if conf.presentation && l.pagey != 0
4623 then
4624 gotoghyll (clamp (pgscale ~-(state.winh)))
4625 else
4626 let pageno = max 0 (l.pageno-1) in
4627 gotoghyll (getpagey pageno)
4628 | Cmulti ((c, _, coverB) as cl, _) ->
4629 if conf.presentation &&
4630 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4631 then
4632 gotoghyll (clamp (pgscale ~-(state.winh)))
4633 else
4634 let decr =
4635 if l.pageno = state.pagecount - coverB
4636 then 1
4637 else c
4639 let pageno = max 0 (l.pageno-decr) in
4640 gotoghyll (getpagey pageno)
4641 | Csplit (n, _) ->
4642 let y =
4643 if l.pagecol = 0
4644 then
4645 if l.pageno = 0
4646 then l.pagey
4647 else
4648 let pageno = max 0 (l.pageno-1) in
4649 let pagey, pageh = getpageyh pageno in
4650 pagey + (n-1)*pageh
4651 else
4652 let pagey, pageh = getpageyh l.pageno in
4653 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4655 gotoghyll y
4658 let viewkeyboard key mask =
4659 let enttext te =
4660 let mode = state.mode in
4661 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4662 state.text <- E.s;
4663 enttext ();
4664 G.postRedisplay "view:enttext"
4666 let ctrl = Wsi.withctrl mask in
4667 let key =
4668 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
4670 match key with
4671 | @Q -> exit 0
4672 | @insert ->
4673 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4674 then (
4675 state.mode <- LinkNav (Ltgendir 0);
4676 gotoy state.y;
4678 else showtext '!' "Keyboard link navigation does not work under rotation"
4680 | @escape | @q ->
4681 begin match state.mstate with
4682 | Mzoomrect _ ->
4683 resetmstate ();
4684 G.postRedisplay "kill zoom rect";
4685 | Msel _
4686 | Mpan _
4687 | Mscrolly | Mscrollx
4688 | Mzoom _
4689 | Mnone ->
4690 begin match state.mode with
4691 | LinkNav _ ->
4692 state.mode <- View;
4693 G.postRedisplay "esc leave linknav"
4694 | Birdseye _
4695 | Textentry _
4696 | View ->
4697 match state.ranchors with
4698 | [] -> raise Quit
4699 | (path, password, anchor, origin) :: rest ->
4700 state.ranchors <- rest;
4701 state.anchor <- anchor;
4702 state.origin <- origin;
4703 state.nameddest <- E.s;
4704 opendoc path password
4705 end;
4706 end;
4708 | @backspace ->
4709 gotoghyll (getnav ~-1)
4711 | @o ->
4712 enteroutlinemode ()
4714 | @H ->
4715 enterhistmode ()
4717 | @u ->
4718 state.rects <- [];
4719 state.text <- E.s;
4720 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap;
4721 G.postRedisplay "dehighlight";
4723 | @slash | @question ->
4724 let ondone isforw s =
4725 cbput state.hists.pat s;
4726 state.searchpattern <- s;
4727 search s isforw
4729 let s = String.make 1 (Char.chr key) in
4730 enttext (s, E.s, Some (onhist state.hists.pat),
4731 textentry, ondone (key = @slash), true)
4733 | @plus | @kpplus | @equals when ctrl ->
4734 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4735 setzoom (conf.zoom +. incr)
4737 | @plus | @kpplus ->
4738 let ondone s =
4739 let n =
4740 try int_of_string s with exc ->
4741 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
4742 max_int
4744 if n != max_int
4745 then (
4746 conf.pagebias <- n;
4747 state.text <- "page bias is now " ^ string_of_int n;
4750 enttext ("page bias: ", E.s, None, intentry, ondone, true)
4752 | @minus | @kpminus when ctrl ->
4753 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4754 setzoom (max 0.01 (conf.zoom -. decr))
4756 | @minus | @kpminus ->
4757 let ondone msg = state.text <- msg in
4758 enttext (
4759 "option [acfhilpstvxACFPRSZTISM]: ", E.s, None,
4760 optentry state.mode, ondone, true
4763 | @0 when ctrl ->
4764 if conf.zoom = 1.0
4765 then (
4766 state.x <- 0;
4767 gotoy state.y
4769 else setzoom 1.0
4771 | (@1 | @2) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
4772 let cols =
4773 match conf.columns with
4774 | Csingle _ | Cmulti _ -> 1
4775 | Csplit (n, _) -> n
4777 let h = state.winh -
4778 conf.interpagespace lsl (if conf.presentation then 1 else 0)
4780 let zoom = zoomforh state.winw h (vscrollw ()) cols in
4781 if zoom > 0.0 && (key = 50 || zoom < 1.0)
4782 then setzoom zoom
4784 | @3 when ctrl ->
4785 let fm =
4786 match conf.fitmodel with
4787 | FitWidth -> FitProportional
4788 | FitProportional -> FitPage
4789 | FitPage -> FitWidth
4791 state.text <- "fit model: " ^ FMTE.to_string fm;
4792 reqlayout conf.angle fm
4794 | @F9 ->
4795 togglebirdseye ()
4797 | @9 when ctrl ->
4798 togglebirdseye ()
4800 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4801 when not ctrl -> (* 0..9 *)
4802 let ondone s =
4803 let n =
4804 try int_of_string s with exc ->
4805 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
4808 if n >= 0
4809 then (
4810 addnav ();
4811 cbput state.hists.pag (string_of_int n);
4812 gotopage1 (n + conf.pagebias - 1) 0;
4815 let pageentry text key =
4816 match Char.unsafe_chr key with
4817 | 'g' -> TEdone text
4818 | _ -> intentry text key
4820 let text = String.make 1 (Char.chr key) in
4821 enttext (":", text, Some (onhist state.hists.pag),
4822 pageentry, ondone, true)
4824 | @b ->
4825 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
4826 reshape state.winw state.winh;
4828 | @B ->
4829 state.bzoom <- not state.bzoom;
4830 state.rects <- [];
4831 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
4833 | @l ->
4834 conf.hlinks <- not conf.hlinks;
4835 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4836 G.postRedisplay "toggle highlightlinks";
4838 | @F ->
4839 state.glinks <- true;
4840 let mode = state.mode in
4841 state.mode <- Textentry (
4842 (":", E.s, None, linknentry, linkndone gotounder, false),
4843 (fun _ ->
4844 state.glinks <- false;
4845 state.mode <- mode)
4847 state.text <- E.s;
4848 G.postRedisplay "view:linkent(F)"
4850 | @y ->
4851 state.glinks <- true;
4852 let mode = state.mode in
4853 state.mode <- Textentry (
4855 ":", E.s, None, linknentry, linkndone (fun under ->
4856 selstring (undertext under);
4857 ), false
4859 fun _ ->
4860 state.glinks <- false;
4861 state.mode <- mode
4863 state.text <- E.s;
4864 G.postRedisplay "view:linkent"
4866 | @a ->
4867 begin match state.autoscroll with
4868 | Some step ->
4869 conf.autoscrollstep <- step;
4870 state.autoscroll <- None
4871 | None ->
4872 if conf.autoscrollstep = 0
4873 then state.autoscroll <- Some 1
4874 else state.autoscroll <- Some conf.autoscrollstep
4877 | @p when ctrl ->
4878 launchpath ()
4880 | @P ->
4881 setpresentationmode (not conf.presentation);
4882 showtext ' ' ("presentation mode " ^
4883 if conf.presentation then "on" else "off");
4885 | @f ->
4886 if List.mem Wsi.Fullscreen state.winstate
4887 then Wsi.reshape conf.cwinw conf.cwinh
4888 else Wsi.fullscreen ()
4890 | @p | @N ->
4891 search state.searchpattern false
4893 | @n | @F3 ->
4894 search state.searchpattern true
4896 | @t ->
4897 begin match state.layout with
4898 | [] -> ()
4899 | l :: _ ->
4900 gotoghyll (getpagey l.pageno)
4903 | @space ->
4904 nextpage ()
4906 | @delete | @kpdelete -> (* delete *)
4907 prevpage ()
4909 | @equals ->
4910 showtext ' ' (describe_location ());
4912 | @w ->
4913 begin match state.layout with
4914 | [] -> ()
4915 | l :: _ ->
4916 Wsi.reshape (l.pagew + vscrollw ()) l.pageh;
4917 G.postRedisplay "w"
4920 | @apos ->
4921 enterbookmarkmode ()
4923 | @h | @F1 ->
4924 enterhelpmode ()
4926 | @i ->
4927 enterinfomode ()
4929 | @e when Buffer.length state.errmsgs > 0 ->
4930 entermsgsmode ()
4932 | @m ->
4933 let ondone s =
4934 match state.layout with
4935 | l :: _ ->
4936 if nonemptystr s
4937 then
4938 state.bookmarks <-
4939 (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4940 | _ -> ()
4942 enttext ("bookmark: ", E.s, None, textentry, ondone, true)
4944 | @tilde ->
4945 quickbookmark ();
4946 showtext ' ' "Quick bookmark added";
4948 | @z ->
4949 begin match state.layout with
4950 | l :: _ ->
4951 let rect = getpdimrect l.pagedimno in
4952 let w, h =
4953 if conf.crophack
4954 then
4955 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4956 truncate (1.2 *. (rect.(3) -. rect.(0))))
4957 else
4958 (truncate (rect.(1) -. rect.(0)),
4959 truncate (rect.(3) -. rect.(0)))
4961 let w = truncate ((float w)*.conf.zoom)
4962 and h = truncate ((float h)*.conf.zoom) in
4963 if w != 0 && h != 0
4964 then (
4965 state.anchor <- getanchor ();
4966 Wsi.reshape (w + vscrollw ()) (h + conf.interpagespace)
4968 G.postRedisplay "z";
4970 | [] -> ()
4973 | @x -> state.roam ()
4975 | @Lt | @Gt ->
4976 reqlayout (conf.angle +
4977 (if key = @Gt then 30 else -30)) conf.fitmodel
4979 | @Lb | @Rb ->
4980 conf.colorscale <-
4981 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
4983 G.postRedisplay "brightness";
4985 | @c when state.mode = View ->
4986 if Wsi.withalt mask
4987 then (
4988 if conf.zoom > 1.0
4989 then
4990 let m = (wadjsb state.winw - state.w) / 2 in
4991 state.x <- m;
4992 gotoy_and_clear_text state.y
4994 else
4995 let (c, a, b), z =
4996 match state.prevcolumns with
4997 | None -> (1, 0, 0), 1.0
4998 | Some (columns, z) ->
4999 let cab =
5000 match columns with
5001 | Csplit (c, _) -> -c, 0, 0
5002 | Cmulti ((c, a, b), _) -> c, a, b
5003 | Csingle _ -> 1, 0, 0
5005 cab, z
5007 setcolumns View c a b;
5008 setzoom z
5010 | @down | @up when ctrl && Wsi.withshift mask ->
5011 let zoom, x = state.prevzoom in
5012 setzoom zoom;
5013 state.x <- x;
5015 | @k | @up | @kpup ->
5016 begin match state.autoscroll with
5017 | None ->
5018 begin match state.mode with
5019 | Birdseye beye -> upbirdseye 1 beye
5020 | Textentry _
5021 | View
5022 | LinkNav _ ->
5023 if ctrl
5024 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5025 else (
5026 if not (Wsi.withshift mask) && conf.presentation
5027 then prevpage ()
5028 else gotoghyll1 true (clamp (-conf.scrollstep))
5031 | Some n ->
5032 setautoscrollspeed n false
5035 | @j | @down | @kpdown ->
5036 begin match state.autoscroll with
5037 | None ->
5038 begin match state.mode with
5039 | Birdseye beye -> downbirdseye 1 beye
5040 | Textentry _
5041 | View
5042 | LinkNav _ ->
5043 if ctrl
5044 then gotoy_and_clear_text (clamp (state.winh/2))
5045 else (
5046 if not (Wsi.withshift mask) && conf.presentation
5047 then nextpage ()
5048 else gotoghyll1 true (clamp (conf.scrollstep))
5051 | Some n ->
5052 setautoscrollspeed n true
5055 | @left | @right | @kpleft | @kpright when not (Wsi.withalt mask) ->
5056 if canpan ()
5057 then
5058 let dx =
5059 if ctrl
5060 then state.winw / 2
5061 else conf.hscrollstep
5063 let dx = if key = @left || key = @kpleft then dx else -dx in
5064 state.x <- panbound (state.x + dx);
5065 gotoy_and_clear_text state.y
5066 else (
5067 state.text <- E.s;
5068 G.postRedisplay "left/right"
5071 | @prior | @kpprior ->
5072 let y =
5073 if ctrl
5074 then
5075 match state.layout with
5076 | [] -> state.y
5077 | l :: _ -> state.y - l.pagey
5078 else
5079 clamp (pgscale (-state.winh))
5081 gotoghyll y
5083 | @next | @kpnext ->
5084 let y =
5085 if ctrl
5086 then
5087 match List.rev state.layout with
5088 | [] -> state.y
5089 | l :: _ -> getpagey l.pageno
5090 else
5091 clamp (pgscale state.winh)
5093 gotoghyll y
5095 | @g | @home | @kphome ->
5096 addnav ();
5097 gotoghyll 0
5098 | @G | @jend | @kpend ->
5099 addnav ();
5100 gotoghyll (clamp state.maxy)
5102 | @right | @kpright when Wsi.withalt mask ->
5103 gotoghyll (getnav 1)
5104 | @left | @kpleft when Wsi.withalt mask ->
5105 gotoghyll (getnav ~-1)
5107 | @r ->
5108 reload ()
5110 | @v when conf.debug ->
5111 state.rects <- [];
5112 List.iter (fun l ->
5113 match getopaque l.pageno with
5114 | None -> ()
5115 | Some opaque ->
5116 let x0, y0, x1, y1 = pagebbox opaque in
5117 let a,b = float x0, float y0 in
5118 let c,d = float x1, float y0 in
5119 let e,f = float x1, float y1 in
5120 let h,j = float x0, float y1 in
5121 let rect = (a,b,c,d,e,f,h,j) in
5122 debugrect rect;
5123 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5124 ) state.layout;
5125 G.postRedisplay "v";
5127 | @pipe ->
5128 let mode = state.mode in
5129 let cmd = ref E.s in
5130 let onleave = function
5131 | Cancel -> state.mode <- mode
5132 | Confirm ->
5133 List.iter (fun l ->
5134 match getopaque l.pageno with
5135 | Some opaque -> pipesel opaque !cmd
5136 | None -> ()) state.layout;
5137 state.mode <- mode
5139 let ondone s =
5140 cbput state.hists.sel s;
5141 cmd := s
5143 let te =
5144 "| ", !cmd, Some (onhist state.hists.sel), textentry, ondone, true
5146 G.postRedisplay "|";
5147 state.mode <- Textentry (te, onleave);
5149 | _ ->
5150 vlog "huh? %s" (Wsi.keyname key)
5153 let linknavkeyboard key mask linknav =
5154 let getpage pageno =
5155 let rec loop = function
5156 | [] -> None
5157 | l :: _ when l.pageno = pageno -> Some l
5158 | _ :: rest -> loop rest
5159 in loop state.layout
5161 let doexact (pageno, n) =
5162 match getopaque pageno, getpage pageno with
5163 | Some opaque, Some l ->
5164 if key = @enter || key = @kpenter
5165 then
5166 let under = getlink opaque n in
5167 G.postRedisplay "link gotounder";
5168 gotounder under;
5169 state.mode <- View;
5170 else
5171 let opt, dir =
5172 match key with
5173 | @home ->
5174 Some (findlink opaque LDfirst), -1
5176 | @jend ->
5177 Some (findlink opaque LDlast), 1
5179 | @left ->
5180 Some (findlink opaque (LDleft n)), -1
5182 | @right ->
5183 Some (findlink opaque (LDright n)), 1
5185 | @up ->
5186 Some (findlink opaque (LDup n)), -1
5188 | @down ->
5189 Some (findlink opaque (LDdown n)), 1
5191 | _ -> None, 0
5193 let pwl l dir =
5194 begin match findpwl l.pageno dir with
5195 | Pwlnotfound -> ()
5196 | Pwl pageno ->
5197 let notfound dir =
5198 state.mode <- LinkNav (Ltgendir dir);
5199 let y, h = getpageyh pageno in
5200 let y =
5201 if dir < 0
5202 then y + h - state.winh
5203 else y
5205 gotoy y
5207 begin match getopaque pageno, getpage pageno with
5208 | Some opaque, Some _ ->
5209 let link =
5210 let ld = if dir > 0 then LDfirst else LDlast in
5211 findlink opaque ld
5213 begin match link with
5214 | Lfound m ->
5215 showlinktype (getlink opaque m);
5216 state.mode <- LinkNav (Ltexact (pageno, m));
5217 G.postRedisplay "linknav jpage";
5218 | Lnotfound -> notfound dir
5219 end;
5220 | _ -> notfound dir
5221 end;
5222 end;
5224 begin match opt with
5225 | Some Lnotfound -> pwl l dir;
5226 | Some (Lfound m) ->
5227 if m = n
5228 then pwl l dir
5229 else (
5230 let _, y0, _, y1 = getlinkrect opaque m in
5231 if y0 < l.pagey
5232 then gotopage1 l.pageno y0
5233 else (
5234 let d = fstate.fontsize + 1 in
5235 if y1 - l.pagey > l.pagevh - d
5236 then gotopage1 l.pageno (y1 - state.winh - hscrollh () + d)
5237 else G.postRedisplay "linknav";
5239 showlinktype (getlink opaque m);
5240 state.mode <- LinkNav (Ltexact (l.pageno, m));
5243 | None -> viewkeyboard key mask
5244 end;
5245 | _ -> viewkeyboard key mask
5247 if key = @insert
5248 then (
5249 state.mode <- View;
5250 G.postRedisplay "leave linknav"
5252 else
5253 match linknav with
5254 | Ltgendir _ -> viewkeyboard key mask
5255 | Ltexact exact -> doexact exact
5258 let keyboard key mask =
5259 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5260 then wcmd "interrupt"
5261 else state.uioh <- state.uioh#key key mask
5264 let birdseyekeyboard key mask
5265 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5266 let incr =
5267 match conf.columns with
5268 | Csingle _ -> 1
5269 | Cmulti ((c, _, _), _) -> c
5270 | Csplit _ -> failwith "bird's eye split mode"
5272 let pgh layout = List.fold_left
5273 (fun m l -> max l.pageh m) state.winh layout in
5274 match key with
5275 | @l when Wsi.withctrl mask ->
5276 let y, h = getpageyh pageno in
5277 let top = (state.winh - h) / 2 in
5278 gotoy (max 0 (y - top))
5279 | @enter | @kpenter -> leavebirdseye beye false
5280 | @escape -> leavebirdseye beye true
5281 | @up -> upbirdseye incr beye
5282 | @down -> downbirdseye incr beye
5283 | @left -> upbirdseye 1 beye
5284 | @right -> downbirdseye 1 beye
5286 | @prior ->
5287 begin match state.layout with
5288 | l :: _ ->
5289 if l.pagey != 0
5290 then (
5291 state.mode <- Birdseye (
5292 oconf, leftx, l.pageno, hooverpageno, anchor
5294 gotopage1 l.pageno 0;
5296 else (
5297 let layout = layout (state.y-state.winh) (pgh state.layout) in
5298 match layout with
5299 | [] -> gotoy (clamp (-state.winh))
5300 | l :: _ ->
5301 state.mode <- Birdseye (
5302 oconf, leftx, l.pageno, hooverpageno, anchor
5304 gotopage1 l.pageno 0
5307 | [] -> gotoy (clamp (-state.winh))
5308 end;
5310 | @next ->
5311 begin match List.rev state.layout with
5312 | l :: _ ->
5313 let layout = layout (state.y + (pgh state.layout)) state.winh in
5314 begin match layout with
5315 | [] ->
5316 let incr = l.pageh - l.pagevh in
5317 if incr = 0
5318 then (
5319 state.mode <-
5320 Birdseye (
5321 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5323 G.postRedisplay "birdseye pagedown";
5325 else gotoy (clamp (incr + conf.interpagespace*2));
5327 | l :: _ ->
5328 state.mode <-
5329 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5330 gotopage1 l.pageno 0;
5333 | [] -> gotoy (clamp state.winh)
5334 end;
5336 | @home ->
5337 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5338 gotopage1 0 0
5340 | @jend ->
5341 let pageno = state.pagecount - 1 in
5342 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5343 if not (pagevisible state.layout pageno)
5344 then
5345 let h =
5346 match List.rev state.pdims with
5347 | [] -> state.winh
5348 | (_, _, h, _) :: _ -> h
5350 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5351 else G.postRedisplay "birdseye end";
5353 | _ -> viewkeyboard key mask
5356 let drawpage l =
5357 let color =
5358 match state.mode with
5359 | Textentry _ -> scalecolor 0.4
5360 | LinkNav _
5361 | View -> scalecolor 1.0
5362 | Birdseye (_, _, pageno, hooverpageno, _) ->
5363 if l.pageno = hooverpageno
5364 then scalecolor 0.9
5365 else (
5366 if l.pageno = pageno
5367 then (
5368 let c = scalecolor 1.0 in
5369 GlDraw.color c;
5370 GlDraw.line_width 3.0;
5371 let dispx = xadjsb l.pagedispx in
5372 linerect
5373 (float (dispx-1)) (float (l.pagedispy-1))
5374 (float (dispx+l.pagevw+1))
5375 (float (l.pagedispy+l.pagevh+1))
5377 GlDraw.line_width 1.0;
5380 else scalecolor 0.8
5383 drawtiles l color;
5386 let postdrawpage l linkindexbase =
5387 match getopaque l.pageno with
5388 | Some opaque ->
5389 if tileready l l.pagex l.pagey
5390 then
5391 let x = l.pagedispx - l.pagex + xadjsb 0
5392 and y = l.pagedispy - l.pagey in
5393 let hlmask =
5394 match conf.columns with
5395 | Csingle _ | Cmulti _ ->
5396 (if conf.hlinks then 1 else 0)
5397 + (if state.glinks
5398 && not (isbirdseye state.mode) then 2 else 0)
5399 | Csplit _ -> 0
5401 let s =
5402 match state.mode with
5403 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5404 | Textentry _
5405 | Birdseye _
5406 | View
5407 | LinkNav _ -> E.s
5409 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5410 else 0
5411 | _ -> 0
5414 let scrollindicator () =
5415 let sbw, ph, sh = state.uioh#scrollph in
5416 let sbh, pw, sw = state.uioh#scrollpw in
5418 let x0,x1 =
5419 if conf.leftscroll
5420 then (0, sbw)
5421 else (state.winw - sbw), state.winw
5424 GlDraw.color (0.64, 0.64, 0.64);
5425 filledrect (float x0) 0. (float x1) (float state.winh);
5426 filledrect
5427 0. (float (state.winh - sbh))
5428 (float (wadjsb state.winw - 1)) (float state.winh)
5430 GlDraw.color (0.0, 0.0, 0.0);
5432 filledrect (float x0) ph (float x1) (ph +. sh);
5433 filledrect pw (float (state.winh - sbh)) (pw +. sw) (float state.winh);
5436 let showsel () =
5437 match state.mstate with
5438 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5441 | Msel ((x0, y0), (x1, y1)) ->
5442 let identify opaque l px py = Some (opaque, l.pageno, px, py) in
5443 let o0,n0,px0,py0 = onppundermouse identify x0 y0 (~< E.s, -1, 0, 0) in
5444 let _o1,n1,px1,py1 = onppundermouse identify x1 y1 (~< E.s, -1, 0, 0) in
5445 if n0 != -1 && n0 = n1 then seltext o0 (px0, py0, px1, py1);
5448 let showrects = function [] -> () | rects ->
5449 Gl.enable `blend;
5450 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5451 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5452 List.iter
5453 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5454 List.iter (fun l ->
5455 if l.pageno = pageno
5456 then (
5457 let dx = float (l.pagedispx - l.pagex) in
5458 let dy = float (l.pagedispy - l.pagey) in
5459 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5460 Raw.sets_float state.vraw ~pos:0
5461 [| x0+.dx; y0+.dy;
5462 x1+.dx; y1+.dy;
5463 x3+.dx; y3+.dy;
5464 x2+.dx; y2+.dy |];
5465 GlArray.vertex `two state.vraw;
5466 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
5468 ) state.layout
5469 ) rects
5471 Gl.disable `blend;
5474 let display () =
5475 GlClear.color (scalecolor2 conf.bgcolor);
5476 GlClear.clear [`color];
5477 List.iter drawpage state.layout;
5478 let rects =
5479 match state.mode with
5480 | LinkNav (Ltexact (pageno, linkno)) ->
5481 begin match getopaque pageno with
5482 | Some opaque ->
5483 let dx = xadjsb 0 in
5484 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5485 let x0 = x0 + dx and x1 = x1 + dx in
5486 (pageno, 5, (
5487 float x0, float y0,
5488 float x1, float y0,
5489 float x1, float y1,
5490 float x0, float y1)
5491 ) :: state.rects
5492 | None -> state.rects
5494 | LinkNav (Ltgendir _)
5495 | Birdseye _
5496 | Textentry _
5497 | View -> state.rects
5499 showrects rects;
5500 let rec postloop linkindexbase = function
5501 | l :: rest ->
5502 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5503 postloop linkindexbase rest
5504 | [] -> ()
5506 showsel ();
5507 postloop 0 state.layout;
5508 state.uioh#display;
5509 begin match state.mstate with
5510 | Mzoomrect ((x0, y0), (x1, y1)) ->
5511 Gl.enable `blend;
5512 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5513 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5514 filledrect (float x0) (float y0) (float x1) (float y1);
5515 Gl.disable `blend;
5516 | Msel _
5517 | Mpan _
5518 | Mscrolly | Mscrollx
5519 | Mzoom _
5520 | Mnone -> ()
5521 end;
5522 enttext ();
5523 scrollindicator ();
5524 Wsi.swapb ();
5527 let zoomrect x y x1 y1 =
5528 let x0 = min x x1
5529 and x1 = max x x1
5530 and y0 = min y y1 in
5531 gotoy (state.y + y0);
5532 state.anchor <- getanchor ();
5533 let zoom = (float state.w) /. float (x1 - x0) in
5534 let margin =
5535 let simple () =
5536 let adjw = wadjsb state.winw in
5537 if state.w < adjw
5538 then (adjw - state.w) / 2
5539 else 0
5541 match conf.fitmodel with
5542 | FitWidth | FitProportional -> simple ()
5543 | FitPage ->
5544 match conf.columns with
5545 | Csplit _ ->
5546 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
5547 | Cmulti _ | Csingle _ -> simple ()
5549 state.x <- (state.x + margin) - x0;
5550 setzoom zoom;
5551 resetmstate ();
5554 let zoomblock x y =
5555 let g opaque l px py =
5556 match rectofblock opaque px py with
5557 | Some a ->
5558 let x0 = a.(0) -. 20. in
5559 let x1 = a.(1) +. 20. in
5560 let y0 = a.(2) -. 20. in
5561 let zoom = (float state.w) /. (x1 -. x0) in
5562 let pagey = getpagey l.pageno in
5563 gotoy_and_clear_text (pagey + truncate y0);
5564 state.anchor <- getanchor ();
5565 let margin = (state.w - l.pagew)/2 in
5566 state.x <- -truncate x0 - margin;
5567 setzoom zoom;
5568 None
5569 | None -> None
5571 match conf.columns with
5572 | Csplit _ ->
5573 showtext '!' "block zooming does not work properly in split columns mode"
5574 | Cmulti _ | Csingle _ -> onppundermouse g x y ()
5577 let scrollx x =
5578 let winw = wadjsb state.winw - 1 in
5579 let s = float x /. float winw in
5580 let destx = truncate (float (state.w + winw) *. s) in
5581 state.x <- winw - destx;
5582 gotoy_and_clear_text state.y;
5583 state.mstate <- Mscrollx;
5586 let scrolly y =
5587 let s = float y /. float state.winh in
5588 let desty = truncate (float (state.maxy - state.winh) *. s) in
5589 gotoy_and_clear_text desty;
5590 state.mstate <- Mscrolly;
5593 let viewmulticlick clicks x y mask =
5594 let g opaque l px py =
5595 let mark =
5596 match clicks with
5597 | 2 -> Mark_word
5598 | 3 -> Mark_line
5599 | 4 -> Mark_block
5600 | _ -> Mark_page
5602 if markunder opaque px py mark
5603 then (
5604 Some (fun () ->
5605 let dopipe cmd =
5606 match getopaque l.pageno with
5607 | None -> ()
5608 | Some opaque -> pipesel opaque cmd
5610 state.roam <- (fun () -> dopipe conf.paxcmd);
5611 if not (Wsi.withctrl mask) then dopipe conf.selcmd;
5614 else None
5616 G.postRedisplay "viewmulticlick";
5617 onppundermouse g x y (fun () -> showtext '!' "Nothing to select") ();
5620 let canselect () =
5621 match conf.columns with
5622 | Csplit _ -> false
5623 | Csingle _ | Cmulti _ -> conf.angle mod 360 = 0
5626 let viewmouse button down x y mask =
5627 match button with
5628 | n when (n == 4 || n == 5) && not down ->
5629 if Wsi.withctrl mask
5630 then (
5631 match state.mstate with
5632 | Mzoom (oldn, i) ->
5633 if oldn = n
5634 then (
5635 if i = 2
5636 then
5637 let incr =
5638 match n with
5639 | 5 ->
5640 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5641 | _ ->
5642 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5644 let zoom = conf.zoom -. incr in
5645 setzoom zoom;
5646 state.mstate <- Mzoom (n, 0);
5647 else
5648 state.mstate <- Mzoom (n, i+1);
5650 else state.mstate <- Mzoom (n, 0)
5652 | Msel _
5653 | Mpan _
5654 | Mscrolly | Mscrollx
5655 | Mzoomrect _
5656 | Mnone -> state.mstate <- Mzoom (n, 0)
5658 else (
5659 match state.autoscroll with
5660 | Some step -> setautoscrollspeed step (n=4)
5661 | None ->
5662 if conf.wheelbypage || conf.presentation
5663 then (
5664 if n = 4
5665 then prevpage ()
5666 else nextpage ()
5668 else
5669 let incr =
5670 if n = 4
5671 then -conf.scrollstep
5672 else conf.scrollstep
5674 let incr = incr * 2 in
5675 let y = clamp incr in
5676 gotoy_and_clear_text y
5679 | n when (n = 6 || n = 7) && not down && canpan () ->
5680 state.x <-
5681 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
5682 gotoy_and_clear_text state.y
5684 | 1 when Wsi.withshift mask ->
5685 state.mstate <- Mnone;
5686 if not down
5687 then (
5688 match unproject x y with
5689 | Some (pageno, ux, uy) ->
5690 let cmd = Printf.sprintf
5691 "%s %s %d %d %d"
5692 conf.stcmd state.path pageno ux uy
5694 popen cmd []
5695 | None -> ()
5698 | 1 when Wsi.withctrl mask ->
5699 if down
5700 then (
5701 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5702 state.mstate <- Mpan (x, y)
5704 else
5705 state.mstate <- Mnone
5707 | 3 ->
5708 if down
5709 then (
5710 Wsi.setcursor Wsi.CURSOR_CYCLE;
5711 let p = (x, y) in
5712 state.mstate <- Mzoomrect (p, p)
5714 else (
5715 match state.mstate with
5716 | Mzoomrect ((x0, y0), _) ->
5717 if abs (x-x0) > 10 && abs (y - y0) > 10
5718 then zoomrect x0 y0 x y
5719 else (
5720 resetmstate ();
5721 G.postRedisplay "kill accidental zoom rect";
5723 | Msel _
5724 | Mpan _
5725 | Mscrolly | Mscrollx
5726 | Mzoom _
5727 | Mnone ->
5728 resetmstate ()
5731 | 1 when x > state.winw - vscrollw () ->
5732 if down
5733 then
5734 let _, position, sh = state.uioh#scrollph in
5735 if y > truncate position && y < truncate (position +. sh)
5736 then state.mstate <- Mscrolly
5737 else scrolly y
5738 else
5739 state.mstate <- Mnone
5741 | 1 when y > state.winh - hscrollh () ->
5742 if down
5743 then
5744 let _, position, sw = state.uioh#scrollpw in
5745 if x > truncate position && x < truncate (position +. sw)
5746 then state.mstate <- Mscrollx
5747 else scrollx x
5748 else
5749 state.mstate <- Mnone
5751 | 1 when state.bzoom -> if not down then zoomblock x y
5753 | 1 ->
5754 let dest = if down then getunder x y else Unone in
5755 begin match dest with
5756 | Ulinkgoto _
5757 | Ulinkuri _
5758 | Uremote _ | Uremotedest _
5759 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5760 gotounder dest
5762 | Unone when down ->
5763 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5764 state.mstate <- Mpan (x, y);
5766 | Unone | Utext _ ->
5767 if down
5768 then (
5769 if canselect ()
5770 then (
5771 state.mstate <- Msel ((x, y), (x, y));
5772 G.postRedisplay "mouse select";
5775 else (
5776 match state.mstate with
5777 | Mnone -> ()
5779 | Mzoom _ | Mscrollx | Mscrolly ->
5780 state.mstate <- Mnone
5782 | Mzoomrect ((x0, y0), _) ->
5783 zoomrect x0 y0 x y
5785 | Mpan _ ->
5786 Wsi.setcursor Wsi.CURSOR_INHERIT;
5787 state.mstate <- Mnone
5789 | Msel ((x0, y0), (x1, y1)) ->
5790 let rec loop = function
5791 | [] -> ()
5792 | l :: rest ->
5793 let inside =
5794 let a0 = l.pagedispy in
5795 let a1 = a0 + l.pagevh in
5796 let b0 = l.pagedispx in
5797 let b1 = b0 + l.pagevw in
5798 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5799 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5801 if inside
5802 then
5803 match getopaque l.pageno with
5804 | Some opaque ->
5805 let dosel cmd () =
5806 match Unix.pipe () with
5807 | exception exn ->
5808 showtext '!'
5809 (Printf.sprintf
5810 "can not create sel pipe: %s"
5811 (exntos exn));
5812 | (r, w) ->
5813 let clo what fd =
5814 Ne.clo fd (fun msg ->
5815 dolog "%s close failed: %s" what msg)
5817 let popened =
5818 try popen cmd [r, 0; w, -1]; true
5819 with exn ->
5820 dolog "can not execute %S: %s"
5821 cmd (exntos exn);
5822 false
5824 if popened
5825 then (
5826 copysel w opaque;
5827 G.postRedisplay "copysel";
5829 else clo "Msel pipe/w" w;
5830 clo "Msel pipe/r" r;
5832 dosel conf.selcmd ();
5833 state.roam <- dosel conf.paxcmd;
5834 | None -> ()
5835 else loop rest
5837 loop state.layout;
5838 resetmstate ();
5842 | _ -> ()
5845 let birdseyemouse button down x y mask
5846 (conf, leftx, _, hooverpageno, anchor) =
5847 match button with
5848 | 1 when down ->
5849 let rec loop = function
5850 | [] -> ()
5851 | l :: rest ->
5852 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5853 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5854 then (
5855 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5857 else loop rest
5859 loop state.layout
5860 | 3 -> ()
5861 | _ -> viewmouse button down x y mask
5864 let uioh = object
5865 method display = ()
5867 method key key mask =
5868 begin match state.mode with
5869 | Textentry textentry -> textentrykeyboard key mask textentry
5870 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5871 | View -> viewkeyboard key mask
5872 | LinkNav linknav -> linknavkeyboard key mask linknav
5873 end;
5874 state.uioh
5876 method button button bstate x y mask =
5877 begin match state.mode with
5878 | LinkNav _
5879 | View -> viewmouse button bstate x y mask
5880 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5881 | Textentry _ -> ()
5882 end;
5883 state.uioh
5885 method multiclick clicks x y mask =
5886 begin match state.mode with
5887 | LinkNav _
5888 | View -> viewmulticlick clicks x y mask
5889 | Birdseye _
5890 | Textentry _ -> ()
5891 end;
5892 state.uioh
5894 method motion x y =
5895 begin match state.mode with
5896 | Textentry _ -> ()
5897 | View | Birdseye _ | LinkNav _ ->
5898 match state.mstate with
5899 | Mzoom _ | Mnone -> ()
5901 | Mpan (x0, y0) ->
5902 let dx = x - x0
5903 and dy = y0 - y in
5904 state.mstate <- Mpan (x, y);
5905 if canpan ()
5906 then state.x <- panbound (state.x + dx);
5907 let y = clamp dy in
5908 gotoy_and_clear_text y
5910 | Msel (a, _) ->
5911 state.mstate <- Msel (a, (x, y));
5912 G.postRedisplay "motion select";
5914 | Mscrolly ->
5915 let y = min state.winh (max 0 y) in
5916 scrolly y
5918 | Mscrollx ->
5919 let x = min state.winw (max 0 x) in
5920 scrollx x
5922 | Mzoomrect (p0, _) ->
5923 state.mstate <- Mzoomrect (p0, (x, y));
5924 G.postRedisplay "motion zoomrect";
5925 end;
5926 state.uioh
5928 method pmotion x y =
5929 begin match state.mode with
5930 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5931 let rec loop = function
5932 | [] ->
5933 if hooverpageno != -1
5934 then (
5935 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5936 G.postRedisplay "pmotion birdseye no hoover";
5938 | l :: rest ->
5939 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5940 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5941 then (
5942 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5943 G.postRedisplay "pmotion birdseye hoover";
5945 else loop rest
5947 loop state.layout
5949 | Textentry _ -> ()
5951 | LinkNav _
5952 | View ->
5953 match state.mstate with
5954 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5956 | Mnone ->
5957 updateunder x y;
5958 if canselect ()
5959 then
5960 match conf.pax with
5961 | None -> ()
5962 | Some r ->
5963 let past, _, _ = !r in
5964 let now = now () in
5965 let delta = now -. past in
5966 if delta > 0.01
5967 then paxunder x y
5968 else r := (now, x, y)
5969 end;
5970 state.uioh
5972 method infochanged _ = ()
5974 method scrollph =
5975 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
5976 let p, h =
5977 if maxy = 0
5978 then 0.0, float state.winh
5979 else scrollph state.y maxy
5981 vscrollw (), p, h
5983 method scrollpw =
5984 let winw = wadjsb state.winw in
5985 let fwinw = float winw in
5986 let sw =
5987 let sw = fwinw /. float state.w in
5988 let sw = fwinw *. sw in
5989 max sw (float conf.scrollh)
5991 let position =
5992 let maxx = state.w + winw in
5993 let x = winw - state.x in
5994 let percent = float x /. float maxx in
5995 (fwinw -. sw) *. percent
5997 hscrollh (), position, sw
5999 method modehash =
6000 let modename =
6001 match state.mode with
6002 | LinkNav _ -> "links"
6003 | Textentry _ -> "textentry"
6004 | Birdseye _ -> "birdseye"
6005 | View -> "view"
6007 findkeyhash conf modename
6009 method eformsgs = true
6010 end;;
6012 let adderrmsg src msg =
6013 Buffer.add_string state.errmsgs msg;
6014 state.newerrmsgs <- true;
6015 G.postRedisplay src
6018 let adderrfmt src fmt =
6019 Format.ksprintf (fun s -> adderrmsg src s) fmt;
6022 let ract cmds =
6023 let cl = splitatspace cmds in
6024 let scan s fmt f =
6025 try Scanf.sscanf s fmt f
6026 with exn ->
6027 adderrfmt "remote exec"
6028 "error processing '%S': %s\n" cmds (exntos exn)
6030 match cl with
6031 | "reload" :: [] -> reload ()
6032 | "goto" :: args :: [] ->
6033 scan args "%u %f %f"
6034 (fun pageno x y ->
6035 let cmd, _ = state.geomcmds in
6036 if emptystr cmd
6037 then gotopagexy pageno x y
6038 else
6039 let f prevf () =
6040 gotopagexy pageno x y;
6041 prevf ()
6043 state.reprf <- f state.reprf
6045 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
6046 | "gotor" :: args :: [] ->
6047 scan args "%S %u"
6048 (fun filename pageno -> gotounder (Uremote (filename, pageno)))
6049 | "gotord" :: args :: [] ->
6050 scan args "%S %S"
6051 (fun filename dest -> gotounder (Uremotedest (filename, dest)))
6052 | "rect" :: args :: [] ->
6053 scan args "%u %u %f %f %f %f"
6054 (fun pageno color x0 y0 x1 y1 ->
6055 onpagerect pageno (fun w h ->
6056 let _,w1,h1,_ = getpagedim pageno in
6057 let sw = float w1 /. float w
6058 and sh = float h1 /. float h in
6059 let x0s = x0 *. sw
6060 and x1s = x1 *. sw
6061 and y0s = y0 *. sh
6062 and y1s = y1 *. sh in
6063 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
6064 debugrect rect;
6065 state.rects <- (pageno, color, rect) :: state.rects;
6066 G.postRedisplay "rect";
6069 | "activatewin" :: [] -> Wsi.activatewin ()
6070 | "quit" :: [] -> raise Quit
6071 | _ ->
6072 adderrfmt "remote command"
6073 "error processing remote command: %S\n" cmds;
6076 let remote =
6077 let scratch = Bytes.create 80 in
6078 let buf = Buffer.create 80 in
6079 fun fd ->
6080 let rec tempfr () =
6081 try Some (Unix.read fd scratch 0 80)
6082 with
6083 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
6084 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
6085 | exn -> raise exn
6087 match tempfr () with
6088 | None -> Some fd
6089 | Some n ->
6090 if n = 0
6091 then (
6092 Unix.close fd;
6093 if Buffer.length buf > 0
6094 then (
6095 let s = Buffer.contents buf in
6096 Buffer.clear buf;
6097 ract s;
6099 None
6101 else
6102 let rec eat ppos =
6103 let nlpos =
6105 let pos = Bytes.index_from scratch ppos '\n' in
6106 if pos >= n then -1 else pos
6107 with Not_found -> -1
6109 if nlpos >= 0
6110 then (
6111 Buffer.add_subbytes buf scratch ppos (nlpos-ppos);
6112 let s = Buffer.contents buf in
6113 Buffer.clear buf;
6114 ract s;
6115 eat (nlpos+1);
6117 else (
6118 Buffer.add_subbytes buf scratch ppos (n-ppos);
6119 Some fd
6121 in eat 0
6124 let remoteopen path =
6125 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
6126 with exn ->
6127 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
6128 None
6131 let () =
6132 let gcconfig = ref E.s in
6133 let trimcachepath = ref E.s in
6134 let rcmdpath = ref E.s in
6135 let pageno = ref None in
6136 let rootwid = ref 0 in
6137 let openlast = ref false in
6138 let nofc = ref false in
6139 selfexec := Sys.executable_name;
6140 Arg.parse
6141 (Arg.align
6142 [("-p", Arg.String (fun s -> state.password <- s),
6143 "<password> Set password");
6145 ("-f", Arg.String
6146 (fun s ->
6147 Config.fontpath := s;
6148 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
6150 "<path> Set path to the user interface font");
6152 ("-c", Arg.String
6153 (fun s ->
6154 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
6155 Config.confpath := s),
6156 "<path> Set path to the configuration file");
6158 ("-last", Arg.Set openlast, " Open last document");
6160 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
6161 "<page-number> Jump to page");
6163 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6164 "<path> Set path to the trim cache file");
6166 ("-dest", Arg.String (fun s -> state.nameddest <- s),
6167 "<named-destination> Set named destination");
6169 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
6170 ("-cxack", Arg.Set cxack, " Cut corners");
6172 ("-remote", Arg.String (fun s -> rcmdpath := s),
6173 "<path> Set path to the remote commands source");
6175 ("-origin", Arg.String (fun s -> state.origin <- s),
6176 "<original-path> Set original path");
6178 ("-gc", Arg.Set_string gcconfig,
6179 "<script-path> Collect garbage with the help of a script");
6181 ("-nofc", Arg.Set nofc, " Do not use fontconfig");
6183 ("-v", Arg.Unit (fun () ->
6184 Printf.printf
6185 "%s\nconfiguration path: %s\n"
6186 (version ())
6187 Config.defconfpath
6189 exit 0), " Print version and exit");
6191 ("-embed", Arg.Set_int rootwid,
6192 "<window-id> Embed into window")
6195 (fun s -> state.path <- s)
6196 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6198 if !wtmode
6199 then selfexec := !selfexec ^ " -wtmode";
6201 let histmode = emptystr state.path && not !openlast in
6203 if not (Config.load !openlast)
6204 then prerr_endline "failed to load configuration";
6205 begin match !pageno with
6206 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
6207 | None -> ()
6208 end;
6210 if not (emptystr !gcconfig)
6211 then (
6212 let c, s =
6213 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6214 | exception exn ->
6215 error "gc socketpair failed: %s" (exntos exn)
6216 | rw -> rw
6218 match popen !gcconfig [(c, 0); (c, 1)] with
6219 | () ->
6220 Config.gc s s;
6221 exit 0
6222 | exception exn ->
6223 error "failed to popen gc script: %s" (exntos exn);
6226 let wsfd, winw, winh = Wsi.init (object (self)
6227 val mutable m_clicks = 0
6228 val mutable m_click_x = 0
6229 val mutable m_click_y = 0
6230 val mutable m_lastclicktime = infinity
6232 method private cleanup =
6233 state.roam <- noroam;
6234 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap
6235 method expose = G.postRedisplay"expose"
6236 method visible v =
6237 let name =
6238 match v with
6239 | Wsi.Unobscured -> "unobscured"
6240 | Wsi.PartiallyObscured -> "partiallyobscured"
6241 | Wsi.FullyObscured -> "fullyobscured"
6243 vlog "visibility change %s" name
6244 method display = display ()
6245 method map mapped = vlog "mappped %b" mapped
6246 method reshape w h =
6247 self#cleanup;
6248 reshape w h
6249 method mouse b d x y m =
6250 if d && canselect ()
6251 then (
6252 (* http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx *)
6253 m_click_x <- x;
6254 m_click_y <- y;
6255 if b = 1
6256 then (
6257 let t = now () in
6258 if abs x - m_click_x > 10
6259 || abs y - m_click_y > 10
6260 || abs_float (t -. m_lastclicktime) > 0.3
6261 then m_clicks <- 0;
6262 m_clicks <- m_clicks + 1;
6263 m_lastclicktime <- t;
6264 if m_clicks = 1
6265 then (
6266 self#cleanup;
6267 G.postRedisplay "cleanup";
6268 state.uioh <- state.uioh#button b d x y m;
6270 else state.uioh <- state.uioh#multiclick m_clicks x y m
6272 else (
6273 self#cleanup;
6274 m_clicks <- 0;
6275 m_lastclicktime <- infinity;
6276 state.uioh <- state.uioh#button b d x y m
6279 else (
6280 state.uioh <- state.uioh#button b d x y m
6282 method motion x y =
6283 state.mpos <- (x, y);
6284 state.uioh <- state.uioh#motion x y
6285 method pmotion x y =
6286 state.mpos <- (x, y);
6287 state.uioh <- state.uioh#pmotion x y
6288 method key k m =
6289 let mascm = m land (
6290 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6291 ) in
6292 let keyboard k m =
6293 let x = state.x and y = state.y in
6294 keyboard k m;
6295 if x != state.x || y != state.y then self#cleanup
6297 match state.keystate with
6298 | KSnone ->
6299 let km = k, mascm in
6300 begin
6301 match
6302 let modehash = state.uioh#modehash in
6303 try Hashtbl.find modehash km
6304 with Not_found ->
6305 try Hashtbl.find (findkeyhash conf "global") km
6306 with Not_found -> KMinsrt (k, m)
6307 with
6308 | KMinsrt (k, m) -> keyboard k m
6309 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6310 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6312 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6313 List.iter (fun (k, m) -> keyboard k m) insrt;
6314 state.keystate <- KSnone
6315 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6316 state.keystate <- KSinto (keys, insrt)
6317 | KSinto _ -> state.keystate <- KSnone
6319 method enter x y =
6320 state.mpos <- (x, y);
6321 state.uioh <- state.uioh#pmotion x y
6322 method leave = state.mpos <- (-1, -1)
6323 method winstate wsl = state.winstate <- wsl
6324 method quit = raise Quit
6325 end) !rootwid conf.cwinw conf.cwinh platform in
6327 state.wsfd <- wsfd;
6329 if not (
6330 List.exists GlMisc.check_extension
6331 [ "GL_ARB_texture_rectangle"
6332 ; "GL_EXT_texture_recangle"
6333 ; "GL_NV_texture_rectangle" ]
6335 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6337 if (
6338 let r = GlMisc.get_string `renderer in
6339 let p = "Mesa DRI Intel(" in
6340 let l = String.length p in
6341 String.length r > l && String.sub r 0 l = p
6343 then (
6344 defconf.sliceheight <- 1024;
6345 defconf.texcount <- 32;
6346 defconf.usepbo <- true;
6349 let cs, ss =
6350 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6351 | exception exn ->
6352 Printf.eprintf "socketpair failed: %s" (exntos exn);
6353 exit 1
6354 | (r, w) ->
6355 cloexec r;
6356 cloexec w;
6357 r, w
6360 setcheckers conf.checkers;
6361 redirectstderr ();
6362 if conf.redirectstderr
6363 then
6364 at_exit (fun () ->
6365 let s = Bytes.cat
6366 (Buffer.to_bytes state.errmsgs)
6367 (match state.errfd with
6368 | Some fd ->
6369 let s = Bytes.create (80*24) in
6370 let n =
6372 let r, _, _ = Unix.select [fd] [] [] 0.0 in
6373 if List.mem fd r
6374 then Unix.read fd s 0 (Bytes.length s)
6375 else 0
6376 with _ -> 0
6378 if n = 0
6379 then E.b
6380 else Bytes.sub s 0 n
6381 | None -> E.b
6384 try ignore (Unix.write state.stderr s 0 (Bytes.length s))
6385 with exn -> print_endline (exntos exn)
6389 init cs (
6390 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
6391 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6392 !Config.fontpath, !trimcachepath,
6393 GlMisc.check_extension "GL_ARB_pixel_buffer_object",
6394 not !nofc
6396 List.iter GlArray.enable [`texture_coord; `vertex];
6397 state.ss <- ss;
6398 reshape winw winh;
6399 if histmode
6400 then (
6401 state.uioh <- uioh;
6402 Wsi.settitle "llpp (history)";
6403 enterhistmode ();
6405 else (
6406 state.text <- "Opening " ^ (mbtoutf8 state.path);
6407 opendoc state.path state.password;
6408 state.uioh <- uioh;
6410 display ();
6411 Wsi.mapwin ();
6412 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
6413 let optrfd =
6414 ref (
6415 if nonemptystr !rcmdpath
6416 then remoteopen !rcmdpath
6417 else None
6421 let rec loop deadline =
6422 let r =
6423 match state.errfd with
6424 | None -> [state.ss; state.wsfd]
6425 | Some fd -> [state.ss; state.wsfd; fd]
6427 let r =
6428 match !optrfd with
6429 | None -> r
6430 | Some fd -> fd :: r
6432 if state.redisplay
6433 then (
6434 state.redisplay <- false;
6435 display ();
6437 let timeout =
6438 let now = now () in
6439 if deadline > now
6440 then (
6441 if deadline = infinity
6442 then ~-.1.0
6443 else max 0.0 (deadline -. now)
6445 else 0.0
6447 let r, _, _ =
6448 try Unix.select r [] [] timeout
6449 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6451 begin match r with
6452 | [] ->
6453 state.ghyll None;
6454 let newdeadline =
6455 if state.ghyll == noghyll
6456 then
6457 match state.autoscroll with
6458 | Some step when step != 0 ->
6459 let y = state.y + step in
6460 let y =
6461 if y < 0
6462 then state.maxy
6463 else if y >= state.maxy then 0 else y
6465 gotoy y;
6466 if state.mode = View
6467 then state.text <- E.s;
6468 deadline +. 0.01
6469 | _ -> infinity
6470 else deadline +. 0.01
6472 loop newdeadline
6474 | l ->
6475 let rec checkfds = function
6476 | [] -> ()
6477 | fd :: rest when fd = state.ss ->
6478 let cmd = readcmd state.ss in
6479 act cmd;
6480 checkfds rest
6482 | fd :: rest when fd = state.wsfd ->
6483 Wsi.readresp fd;
6484 checkfds rest
6486 | fd :: rest when Some fd = !optrfd ->
6487 begin match remote fd with
6488 | None -> optrfd := remoteopen !rcmdpath;
6489 | opt -> optrfd := opt
6490 end;
6491 checkfds rest
6493 | fd :: rest ->
6494 let s = Bytes.create 80 in
6495 let n = tempfailureretry (Unix.read fd s 0) 80 in
6496 if conf.redirectstderr
6497 then (
6498 Buffer.add_substring state.errmsgs (Bytes.to_string s) 0 n;
6499 state.newerrmsgs <- true;
6500 state.redisplay <- true;
6502 else (
6503 prerr_string (String.sub (Bytes.to_string s) 0 n);
6504 flush stderr;
6506 checkfds rest
6508 checkfds l;
6509 if !reeenterhist then (
6510 enterhistmode ();
6511 reeenterhist := false;
6513 let newdeadline =
6514 let deadline1 =
6515 if deadline = infinity
6516 then now () +. 0.01
6517 else deadline
6519 match state.autoscroll with
6520 | Some step when step != 0 -> deadline1
6521 | _ -> if state.ghyll == noghyll then infinity else deadline1
6523 loop newdeadline
6524 end;
6527 loop infinity;
6528 with Quit ->
6529 Config.save leavebirdseye;