Haphazzard hodgepdge
[llpp.git] / main.ml
blob9eddf3f59b2f954314e435649a2135f1d47f22ea
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 Birdseye _ -> true | _ -> false;;
80 let istextentry = function Textentry _ -> true | _ -> false;;
82 let wtmode = ref false;;
83 let cxack = ref false;;
85 let pgscale h = truncate (float h *. conf.pgscale);;
87 let hscrollh () =
88 if (conf.scrollb land scrollbhv = 0)
89 || (state.x = 0 && state.w <= state.winw - conf.scrollbw)
90 then 0
91 else conf.scrollbw
94 let vscrollw () =
95 if (conf.scrollb land scrollbvv = 0)
96 then 0
97 else conf.scrollbw
100 let wadjsb w = w - vscrollw ();;
101 let xadjsb x = if conf.leftscroll then x + vscrollw () else x;;
103 let setfontsize n =
104 fstate.fontsize <- n;
105 fstate.wwidth <- measurestr fstate.fontsize "w";
106 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
109 let vlog fmt =
110 if conf.verbose
111 then
112 Printf.kprintf prerr_endline fmt
113 else
114 Printf.kprintf ignore fmt
117 let launchpath () =
118 if emptystr conf.pathlauncher
119 then print_endline state.path
120 else (
121 let re = Str.regexp "%s" in
122 let command = Str.global_replace re state.path conf.pathlauncher in
123 try popen command []
124 with exn ->
125 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
126 flush stderr;
130 let redirectstderr () =
131 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
132 if conf.redirectstderr
133 then
134 match Ne.res Unix.pipe () with
135 | Ne.Exn exn ->
136 dolog "failed to create stderr redirection pipes: %s" (exntos exn)
138 | Ne.Res (r, w) ->
139 begin match Ne.dup Unix.stderr with
140 | Ne.Exn exn ->
141 dolog "failed to dup stderr: %s" (exntos exn);
142 Ne.clo r (clofail "pipe/r");
143 Ne.clo w (clofail "pipe/w");
145 | Ne.Res dupstderr ->
146 begin match Ne.dup2 w Unix.stderr with
147 | Ne.Exn exn ->
148 dolog "failed to dup2 to stderr: %s" (exntos exn);
149 Ne.clo dupstderr (clofail "stderr duplicate");
150 Ne.clo r (clofail "redir pipe/r");
151 Ne.clo w (clofail "redir pipe/w");
153 | Ne.Res () ->
154 state.stderr <- dupstderr;
155 state.errfd <- Some r;
156 end;
158 else (
159 state.newerrmsgs <- false;
160 begin match state.errfd with
161 | Some fd ->
162 begin match Ne.dup2 state.stderr Unix.stderr with
163 | Ne.Exn exn ->
164 dolog "failed to dup2 original stderr: %s" (exntos exn)
165 | Ne.Res () ->
166 Ne.clo fd (clofail "dup of stderr");
167 state.errfd <- None;
168 end;
169 | None -> ()
170 end;
171 prerr_string (Buffer.contents state.errmsgs);
172 flush stderr;
173 Buffer.clear state.errmsgs;
177 module G =
178 struct
179 let postRedisplay who =
180 if conf.verbose
181 then prerr_endline ("redisplay for " ^ who);
182 state.redisplay <- true;
184 end;;
186 let getopaque pageno =
187 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
188 with Not_found -> None
191 let putopaque pageno opaque =
192 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
195 let pagetranslatepoint l x y =
196 let dy = y - l.pagedispy in
197 let y = dy + l.pagey in
198 let dx = x - l.pagedispx in
199 let x = dx + l.pagex in
200 (x, y);
203 let onppundermouse g x y d =
204 let rec f = function
205 | l :: rest ->
206 begin match getopaque l.pageno with
207 | Some opaque ->
208 let x0 = l.pagedispx in
209 let x1 = x0 + l.pagevw in
210 let y0 = l.pagedispy in
211 let y1 = y0 + l.pagevh in
212 if y >= y0 && y <= y1 && x >= x0 && x <= x1
213 then
214 let px, py = pagetranslatepoint l x y in
215 match g opaque l px py with
216 | Some res -> res
217 | None -> f rest
218 else f rest
219 | _ ->
220 f rest
222 | [] -> d
224 f state.layout
227 let getunder x y =
228 let g opaque l px py =
229 if state.bzoom
230 then (
231 match rectofblock opaque px py with
232 | Some a ->
233 let rect = (a.(0),a.(2),a.(1),a.(2),a.(1),a.(3),a.(0),a.(3)) in
234 state.rects <- [l.pageno, l.pageno mod 3, rect];
235 G.postRedisplay "getunder";
236 | None -> ()
238 match whatsunder opaque px py with
239 | Unone -> None
240 | under -> Some under
242 onppundermouse g x y Unone
245 let unproject x y =
246 let g opaque l x y =
247 match unproject opaque x y with
248 | Some (x, y) -> Some (Some (l.pageno, x, y))
249 | None -> None
251 onppundermouse g x y None;
254 let showtext c s =
255 state.text <- Printf.sprintf "%c%s" c s;
256 G.postRedisplay "showtext";
259 let pipesel opaque cmd =
260 if hassel opaque
261 then
262 match Ne.res Unix.pipe () with
263 | Ne.Exn exn ->
264 showtext '!'
265 (Printf.sprintf "pipesel can not create pipe: %s" (exntos exn));
266 | Ne.Res (r, w) ->
267 let doclose what fd =
268 Ne.clo fd (fun msg -> dolog "%s close failed: %s" what msg)
270 let popened =
271 try popen cmd [r, 0; w, -1]; true
272 with exn ->
273 dolog "can not execute %S: %s" cmd (exntos exn);
274 false
276 if popened
277 then (
278 copysel w opaque;
279 G.postRedisplay "pipesel";
281 else doclose "pipesel pipe/w" w;
282 doclose "pipesel pipe/r" r;
285 let paxunder x y =
286 let g opaque l px py =
287 if markunder opaque px py conf.paxmark
288 then (
289 Some (fun () ->
290 match getopaque l.pageno with
291 | None -> ()
292 | Some opaque -> pipesel opaque conf.paxcmd
295 else None
297 G.postRedisplay "paxunder";
298 if conf.paxmark = Mark_page
299 then
300 List.iter (fun l ->
301 match getopaque l.pageno with
302 | None -> ()
303 | Some opaque -> clearmark opaque) state.layout;
304 state.roam <-
305 onppundermouse g x y (fun () -> showtext '!' "Whoopsie daisy");
308 let selstring s =
309 match Ne.res Unix.pipe () with
310 | Ne.Exn exn ->
311 showtext '!' (Printf.sprintf "pipe failed: %s" (exntos exn))
312 | Ne.Res (r, w) ->
313 let clo cap fd =
314 Ne.clo fd (fun msg ->
315 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
318 let popened =
319 try popen conf.selcmd [r, 0; w, -1]; true
320 with exn ->
321 showtext '!'
322 (Printf.sprintf "failed to execute %s: %s"
323 conf.selcmd (exntos exn));
324 false
326 if popened
327 then (
329 let l = String.length s in
330 let n = tempfailureretry (Unix.write w s 0) l in
331 if n != l
332 then
333 showtext '!'
334 (Printf.sprintf
335 "failed to write %d characters to sel pipe, wrote %d"
338 with exn ->
339 showtext '!'
340 (Printf.sprintf "failed to write to sel pipe: %s"
341 (exntos exn)
344 else dolog "%s" s;
345 clo "selstring pipe/r" r;
346 clo "selstring pipe/w" w;
349 let undertext = function
350 | Unone -> "none"
351 | Ulinkuri s -> s
352 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
353 | Utext s -> "font: " ^ s
354 | Uunexpected s -> "unexpected: " ^ s
355 | Ulaunch s -> "launch: " ^ s
356 | Unamed s -> "named: " ^ s
357 | Uremote (filename, pageno) ->
358 Printf.sprintf "%s: page %d" filename (pageno+1)
359 | Uremotedest (filename, destname) ->
360 Printf.sprintf "%s: destination %S" filename destname
363 let updateunder x y =
364 match getunder x y with
365 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
366 | Ulinkuri uri ->
367 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
368 Wsi.setcursor Wsi.CURSOR_INFO
369 | Ulinkgoto (pageno, _) ->
370 if conf.underinfo
371 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
372 Wsi.setcursor Wsi.CURSOR_INFO
373 | Utext s ->
374 if conf.underinfo then showtext 'f' ("ont: " ^ s);
375 Wsi.setcursor Wsi.CURSOR_TEXT
376 | Uunexpected s ->
377 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
378 Wsi.setcursor Wsi.CURSOR_INHERIT
379 | Ulaunch s ->
380 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
381 Wsi.setcursor Wsi.CURSOR_INHERIT
382 | Unamed s ->
383 if conf.underinfo then showtext 'n' ("amed: " ^ s);
384 Wsi.setcursor Wsi.CURSOR_INHERIT
385 | Uremote (filename, pageno) ->
386 if conf.underinfo then showtext 'r'
387 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
388 Wsi.setcursor Wsi.CURSOR_INFO
389 | Uremotedest (filename, destname) ->
390 if conf.underinfo then showtext 'r'
391 (Printf.sprintf "emote destination: %s (%S)" filename destname);
392 Wsi.setcursor Wsi.CURSOR_INFO
395 let showlinktype under =
396 if conf.underinfo
397 then
398 match under with
399 | Unone -> ()
400 | under ->
401 let s = undertext under in
402 showtext ' ' s
405 let addchar s c =
406 let b = Buffer.create (String.length s + 1) in
407 Buffer.add_string b s;
408 Buffer.add_char b c;
409 Buffer.contents b;
412 let intentry_with_suffix text key =
413 let c =
414 if key >= 32 && key < 127
415 then Char.chr key
416 else '\000'
418 match Char.lowercase c with
419 | '0' .. '9' ->
420 let text = addchar text c in
421 TEcont text
423 | 'k' | 'm' | 'g' ->
424 let text = addchar text c in
425 TEcont text
427 | _ ->
428 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
429 TEcont text
432 let readcmd fd =
433 let s = "xxxx" in
434 let n = tempfailureretry (Unix.read fd s 0) 4 in
435 if n != 4 then error "incomplete read(len) = %d" n;
436 let len = 0
437 lor (Char.code s.[0] lsl 24)
438 lor (Char.code s.[1] lsl 16)
439 lor (Char.code s.[2] lsl 8)
440 lor (Char.code s.[3] lsl 0)
442 let s = String.create len in
443 let n = tempfailureretry (Unix.read fd s 0) len in
444 if n != len then error "incomplete read(data) %d vs %d" n len;
448 let btod b = if b then 1 else 0;;
450 let wcmd fmt =
451 let b = Buffer.create 16 in
452 Buffer.add_string b "llll";
453 Printf.kbprintf
454 (fun b ->
455 let s = Buffer.contents b in
456 let n = String.length s in
457 let len = n - 4 in
458 (* dolog "wcmd %S" (String.sub s 4 len); *)
459 s.[0] <- Char.chr ((len lsr 24) land 0xff);
460 s.[1] <- Char.chr ((len lsr 16) land 0xff);
461 s.[2] <- Char.chr ((len lsr 8) land 0xff);
462 s.[3] <- Char.chr (len land 0xff);
463 let n' = tempfailureretry (Unix.write state.ss s 0) n in
464 if n' != n then error "write failed %d vs %d" n' n;
465 ) b fmt;
468 let nogeomcmds cmds =
469 match cmds with
470 | s, [] -> emptystr s
471 | _ -> false
474 let layoutN ((columns, coverA, coverB), b) y sh =
475 let sh = sh - (hscrollh ()) in
476 let rec fold accu n =
477 if n = Array.length b
478 then accu
479 else
480 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
481 if (vy - y) > sh &&
482 (n = coverA - 1
483 || n = state.pagecount - coverB
484 || (n - coverA) mod columns = columns - 1)
485 then accu
486 else
487 let accu =
488 if vy + h > y
489 then
490 let pagey = max 0 (y - vy) in
491 let pagedispy = if pagey > 0 then 0 else vy - y in
492 let pagedispx, pagex =
493 let pdx =
494 if n = coverA - 1 || n = state.pagecount - coverB
495 then state.x + (wadjsb state.winw - w) / 2
496 else dx + xoff + state.x
498 if pdx < 0
499 then 0, -pdx
500 else pdx, 0
502 let pagevw =
503 let vw = wadjsb state.winw - pagedispx in
504 let pw = w - pagex in
505 min vw pw
507 let pagevh = min (h - pagey) (sh - pagedispy) in
508 if pagevw > 0 && pagevh > 0
509 then
510 let e =
511 { pageno = n
512 ; pagedimno = pdimno
513 ; pagew = w
514 ; pageh = h
515 ; pagex = pagex
516 ; pagey = pagey
517 ; pagevw = pagevw
518 ; pagevh = pagevh
519 ; pagedispx = pagedispx
520 ; pagedispy = pagedispy
521 ; pagecol = 0
524 e :: accu
525 else
526 accu
527 else
528 accu
530 fold accu (n+1)
532 if Array.length b = 0
533 then []
534 else List.rev (fold [] (page_of_y y))
537 let layoutS (columns, b) y sh =
538 let sh = sh - hscrollh () in
539 let rec fold accu n =
540 if n = Array.length b
541 then accu
542 else
543 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
544 if (vy - y) > sh
545 then accu
546 else
547 let accu =
548 if vy + pageh > y
549 then
550 let x = xoff + state.x in
551 let pagey = max 0 (y - vy) in
552 let pagedispy = if pagey > 0 then 0 else vy - y in
553 let pagedispx, pagex =
554 if px = 0
555 then (
556 if x < 0
557 then 0, -x
558 else x, 0
560 else (
561 let px = px - x in
562 if px < 0
563 then -px, 0
564 else 0, px
567 let pagecolw = pagew/columns in
568 let pagedispx =
569 if pagecolw < state.winw
570 then pagedispx + ((wadjsb state.winw - pagecolw) / 2)
571 else pagedispx
573 let pagevw =
574 let vw = wadjsb state.winw - pagedispx in
575 let pw = pagew - pagex in
576 min vw pw
578 let pagevw = min pagevw pagecolw in
579 let pagevh = min (pageh - pagey) (sh - pagedispy) in
580 if pagevw > 0 && pagevh > 0
581 then
582 let e =
583 { pageno = n/columns
584 ; pagedimno = pdimno
585 ; pagew = pagew
586 ; pageh = pageh
587 ; pagex = pagex
588 ; pagey = pagey
589 ; pagevw = pagevw
590 ; pagevh = pagevh
591 ; pagedispx = pagedispx
592 ; pagedispy = pagedispy
593 ; pagecol = n mod columns
596 e :: accu
597 else
598 accu
599 else
600 accu
602 fold accu (n+1)
604 List.rev (fold [] 0)
607 let layout y sh =
608 if nogeomcmds state.geomcmds
609 then
610 match conf.columns with
611 | Csingle b -> layoutN ((1, 0, 0), b) y sh
612 | Cmulti c -> layoutN c y sh
613 | Csplit s -> layoutS s y sh
614 else []
617 let clamp incr =
618 let y = state.y + incr in
619 let y = max 0 y in
620 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
624 let itertiles l f =
625 let tilex = l.pagex mod conf.tilew in
626 let tiley = l.pagey mod conf.tileh in
628 let col = l.pagex / conf.tilew in
629 let row = l.pagey / conf.tileh in
631 let rec rowloop row y0 dispy h =
632 if h = 0
633 then ()
634 else (
635 let dh = conf.tileh - y0 in
636 let dh = min h dh in
637 let rec colloop col x0 dispx w =
638 if w = 0
639 then ()
640 else (
641 let dw = conf.tilew - x0 in
642 let dw = min w dw in
643 let dispx' = xadjsb dispx in
644 f col row dispx' dispy x0 y0 dw dh;
645 colloop (col+1) 0 (dispx+dw) (w-dw)
648 colloop col tilex l.pagedispx l.pagevw;
649 rowloop (row+1) 0 (dispy+dh) (h-dh)
652 if l.pagevw > 0 && l.pagevh > 0
653 then rowloop row tiley l.pagedispy l.pagevh;
656 let gettileopaque l col row =
657 let key =
658 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
660 try Some (Hashtbl.find state.tilemap key)
661 with Not_found -> None
664 let puttileopaque l col row gen colorspace angle opaque size elapsed =
665 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
666 Hashtbl.add state.tilemap key (opaque, size, elapsed)
669 let filledrect x0 y0 x1 y1 =
670 GlArray.disable `texture_coord;
671 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
672 GlArray.vertex `two state.vraw;
673 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
674 GlArray.enable `texture_coord;
677 let linerect x0 y0 x1 y1 =
678 GlArray.disable `texture_coord;
679 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y1; x1; y0 |];
680 GlArray.vertex `two state.vraw;
681 GlArray.draw_arrays `line_loop ~first:0 ~count:4;
682 GlArray.enable `texture_coord;
685 let drawtiles l color =
686 GlDraw.color color;
687 begintiles ();
688 let f col row x y tilex tiley w h =
689 match gettileopaque l col row with
690 | Some (opaque, _, t) ->
691 let params = x, y, w, h, tilex, tiley in
692 if conf.invert
693 then GlTex.env (`mode `blend);
694 drawtile params opaque;
695 if conf.invert
696 then GlTex.env (`mode `modulate);
697 if conf.debug
698 then (
699 endtiles ();
700 let s = Printf.sprintf
701 "%d[%d,%d] %f sec"
702 l.pageno col row t
704 let w = measurestr fstate.fontsize s in
705 GlDraw.color (0.0, 0.0, 0.0);
706 filledrect (float (x-2))
707 (float (y-2))
708 (float (x+2) +. w)
709 (float (y + fstate.fontsize + 2));
710 GlDraw.color (1.0, 1.0, 1.0);
711 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
712 begintiles ();
715 | None ->
716 endtiles ();
717 let w =
718 if conf.leftscroll
719 then w
720 else
721 let lw = wadjsb state.winw - x in
722 min lw w
723 and h =
724 let lh = state.winh - y in
725 min lh h
727 if conf.invert
728 then GlTex.env (`mode `blend);
729 begin match state.checkerstexid with
730 | Some id ->
731 Gl.enable `texture_2d;
732 GlTex.bind_texture ~target:`texture_2d id;
733 let x0 = float x
734 and y0 = float y
735 and x1 = float (x+w)
736 and y1 = float (y+h) in
738 let tw = float w /. 16.0
739 and th = float h /. 16.0 in
740 let tx0 = float tilex /. 16.0
741 and ty0 = float tiley /. 16.0 in
742 let tx1 = tx0 +. tw
743 and ty1 = ty0 +. th in
744 Raw.sets_float state.vraw ~pos:0
745 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
746 Raw.sets_float state.traw ~pos:0
747 [| tx0; ty0; tx0; ty1; tx1; ty0; tx1; ty1 |];
748 GlArray.vertex `two state.vraw;
749 GlArray.tex_coord `two state.traw;
750 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
751 Gl.disable `texture_2d;
753 | None ->
754 GlDraw.color (1.0, 1.0, 1.0);
755 filledrect (float x) (float y) (float (x+w)) (float (y+h));
756 end;
757 if conf.invert
758 then GlTex.env (`mode `modulate);
759 if w > 128 && h > fstate.fontsize + 10
760 then (
761 let c = if conf.invert then 1.0 else 0.0 in
762 GlDraw.color (c, c, c);
763 let c, r =
764 if conf.verbose
765 then (col*conf.tilew, row*conf.tileh)
766 else col, row
768 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
770 GlDraw.color color;
771 begintiles ();
773 itertiles l f;
774 endtiles ();
777 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
779 let tilevisible1 l x y =
780 let ax0 = l.pagex
781 and ax1 = l.pagex + l.pagevw
782 and ay0 = l.pagey
783 and ay1 = l.pagey + l.pagevh in
785 let bx0 = x
786 and by0 = y in
787 let bx1 = min (bx0 + conf.tilew) l.pagew
788 and by1 = min (by0 + conf.tileh) l.pageh in
790 let rx0 = max ax0 bx0
791 and ry0 = max ay0 by0
792 and rx1 = min ax1 bx1
793 and ry1 = min ay1 by1 in
795 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
796 nonemptyintersection
799 let tilevisible layout n x y =
800 let rec findpageinlayout m = function
801 | l :: rest when l.pageno = n ->
802 tilevisible1 l x y || (
803 match conf.columns with
804 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
805 | _ -> false
807 | _ :: rest -> findpageinlayout 0 rest
808 | [] -> false
810 findpageinlayout 0 layout;
813 let tileready l x y =
814 tilevisible1 l x y &&
815 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
818 let tilepage n p layout =
819 let rec loop = function
820 | l :: rest ->
821 if l.pageno = n
822 then
823 let f col row _ _ _ _ _ _ =
824 if state.currently = Idle
825 then
826 match gettileopaque l col row with
827 | Some _ -> ()
828 | None ->
829 let x = col*conf.tilew
830 and y = row*conf.tileh in
831 let w =
832 let w = l.pagew - x in
833 min w conf.tilew
835 let h =
836 let h = l.pageh - y in
837 min h conf.tileh
839 let pbo =
840 if conf.usepbo
841 then getpbo w h conf.colorspace
842 else ~< "0"
844 wcmd "tile %s %d %d %d %d %s"
845 (~> p) x y w h (~> pbo);
846 state.currently <-
847 Tiling (
848 l, p, conf.colorspace, conf.angle,
849 state.gen, col, row, conf.tilew, conf.tileh
852 itertiles l f;
853 else
854 loop rest
856 | [] -> ()
858 if nogeomcmds state.geomcmds
859 then loop layout;
862 let preloadlayout y =
863 let y = if y < state.winh then 0 else y - state.winh in
864 let h = state.winh*3 in
865 layout y h;
868 let load pages =
869 let rec loop pages =
870 if state.currently != Idle
871 then ()
872 else
873 match pages with
874 | l :: rest ->
875 begin match getopaque l.pageno with
876 | None ->
877 wcmd "page %d %d" l.pageno l.pagedimno;
878 state.currently <- Loading (l, state.gen);
879 | Some opaque ->
880 tilepage l.pageno opaque pages;
881 loop rest
882 end;
883 | _ -> ()
885 if nogeomcmds state.geomcmds
886 then loop pages
889 let preload pages =
890 load pages;
891 if conf.preload && state.currently = Idle
892 then load (preloadlayout state.y);
895 let layoutready layout =
896 let rec fold all ls =
897 all && match ls with
898 | l :: rest ->
899 let seen = ref false in
900 let allvisible = ref true in
901 let foo col row _ _ _ _ _ _ =
902 seen := true;
903 allvisible := !allvisible &&
904 begin match gettileopaque l col row with
905 | Some _ -> true
906 | None -> false
909 itertiles l foo;
910 fold (!seen && !allvisible) rest
911 | [] -> true
913 let alltilesvisible = fold true layout in
914 alltilesvisible;
917 let gotoy y =
918 let y = bound y 0 state.maxy in
919 let y, layout, proceed =
920 match conf.maxwait with
921 | Some time when state.ghyll == noghyll ->
922 begin match state.throttle with
923 | None ->
924 let layout = layout y state.winh in
925 let ready = layoutready layout in
926 if not ready
927 then (
928 load layout;
929 state.throttle <- Some (layout, y, now ());
931 else G.postRedisplay "gotoy showall (None)";
932 y, layout, ready
933 | Some (_, _, started) ->
934 let dt = now () -. started in
935 if dt > time
936 then (
937 state.throttle <- None;
938 let layout = layout y state.winh in
939 load layout;
940 G.postRedisplay "maxwait";
941 y, layout, true
943 else -1, [], false
946 | _ ->
947 let layout = layout y state.winh in
948 if not !wtmode || layoutready layout
949 then G.postRedisplay "gotoy ready";
950 y, layout, true
952 if proceed
953 then (
954 state.y <- y;
955 state.layout <- layout;
956 begin match state.mode with
957 | LinkNav (Ltexact (pageno, linkno)) ->
958 let rec loop = function
959 | [] ->
960 state.mode <- LinkNav (Ltgendir 0)
961 | l :: _ when l.pageno = pageno ->
962 begin match getopaque pageno with
963 | None ->
964 state.mode <- LinkNav (Ltgendir 0)
965 | Some opaque ->
966 let x0, y0, x1, y1 = getlinkrect opaque linkno in
967 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
968 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
969 then state.mode <- LinkNav (Ltgendir 0)
971 | _ :: rest -> loop rest
973 loop layout
974 | _ -> ()
975 end;
976 begin match state.mode with
977 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
978 if not (pagevisible layout pageno)
979 then (
980 match state.layout with
981 | [] -> ()
982 | l :: _ ->
983 state.mode <- Birdseye (
984 conf, leftx, l.pageno, hooverpageno, anchor
987 | LinkNav (Ltgendir dir as lt) ->
988 let linknav =
989 let rec loop = function
990 | [] -> lt
991 | l :: rest ->
992 match getopaque l.pageno with
993 | None -> loop rest
994 | Some opaque ->
995 let link =
996 let ld =
997 if dir = 0
998 then LDfirstvisible (l.pagex, l.pagey, dir)
999 else (
1000 if dir > 0 then LDfirst else LDlast
1003 findlink opaque ld
1005 match link with
1006 | Lnotfound -> loop rest
1007 | Lfound n ->
1008 showlinktype (getlink opaque n);
1009 Ltexact (l.pageno, n)
1011 loop state.layout
1013 state.mode <- LinkNav linknav
1014 | _ -> ()
1015 end;
1016 preload layout;
1018 state.ghyll <- noghyll;
1019 if conf.updatecurs
1020 then (
1021 let mx, my = state.mpos in
1022 updateunder mx my;
1026 let conttiling pageno opaque =
1027 tilepage pageno opaque
1028 (if conf.preload then preloadlayout state.y else state.layout)
1031 let gotoy_and_clear_text y =
1032 if not conf.verbose then state.text <- E.s;
1033 gotoy y;
1036 let getanchory (n, top, dtop) =
1037 let y, h = getpageyh n in
1038 if conf.presentation
1039 then
1040 let ips = calcips h in
1041 y + truncate (top*.float h -. dtop*.float ips) + ips;
1042 else
1043 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1046 let gotoanchor anchor =
1047 gotoy (getanchory anchor);
1050 let addnav () =
1051 cbput state.hists.nav (getanchor ());
1054 let getnav dir =
1055 let anchor = cbgetc state.hists.nav dir in
1056 getanchory anchor;
1059 let gotoghyll1 single y =
1060 let scroll f n a b =
1061 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1062 let snake f a b =
1063 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1064 if f < a
1065 then s (float f /. float a)
1066 else (
1067 if f > b
1068 then 1.0 -. s ((float (f-b) /. float (n-b)))
1069 else 1.0
1072 snake f a b
1073 and summa n a b =
1074 let ins = float a *. 0.5
1075 and outs = float (n-b) *. 0.5 in
1076 let ones = b - a in
1077 ins +. outs +. float ones
1079 let rec set nab y sy =
1080 let (_N, _A, _B), y =
1081 if single
1082 then
1083 let scl = if y > sy then 2 else -2 in
1084 let _N, _, _ = nab in
1085 (_N,0,_N), y+conf.scrollstep*scl
1086 else nab,y in
1087 let sum = summa _N _A _B in
1088 let dy = float (y - sy) in
1089 state.ghyll <- (
1090 let rec gf n y1 o =
1091 if n >= _N
1092 then state.ghyll <- noghyll
1093 else
1094 let go n =
1095 let s = scroll n _N _A _B in
1096 let y1 = y1 +. ((s *. dy) /. sum) in
1097 gotoy_and_clear_text (truncate y1);
1098 state.ghyll <- gf (n+1) y1;
1100 match o with
1101 | None -> go n
1102 | Some y' when single -> set nab y' state.y
1103 | Some y' -> set (_N/2, 1, 1) y' state.y
1105 gf 0 (float state.y)
1108 match conf.ghyllscroll with
1109 | Some nab when not conf.presentation ->
1110 if state.ghyll == noghyll
1111 then set nab y state.y
1112 else state.ghyll (Some y)
1113 | _ ->
1114 gotoy_and_clear_text y
1117 let gotoghyll = gotoghyll1 false;;
1119 let gotopage n top =
1120 let y, h = getpageyh n in
1121 let y = y + (truncate (top *. float h)) in
1122 gotoghyll y
1125 let gotopage1 n top =
1126 let y = getpagey n in
1127 let y = y + top in
1128 gotoghyll y
1131 let invalidate s f =
1132 state.layout <- [];
1133 state.pdims <- [];
1134 state.rects <- [];
1135 state.rects1 <- [];
1136 match state.geomcmds with
1137 | ps, [] when emptystr ps ->
1138 f ();
1139 state.geomcmds <- s, [];
1141 | ps, [] ->
1142 state.geomcmds <- ps, [s, f];
1144 | ps, (s', _) :: rest when s' = s ->
1145 state.geomcmds <- ps, ((s, f) :: rest);
1147 | ps, cmds ->
1148 state.geomcmds <- ps, ((s, f) :: cmds);
1151 let flushpages () =
1152 Hashtbl.iter (fun _ opaque ->
1153 wcmd "freepage %s" (~> opaque);
1154 ) state.pagemap;
1155 Hashtbl.clear state.pagemap;
1158 let flushtiles () =
1159 if not (Queue.is_empty state.tilelru)
1160 then (
1161 Queue.iter (fun (k, p, s) ->
1162 wcmd "freetile %s" (~> p);
1163 state.memused <- state.memused - s;
1164 Hashtbl.remove state.tilemap k;
1165 ) state.tilelru;
1166 state.uioh#infochanged Memused;
1167 Queue.clear state.tilelru;
1169 load state.layout;
1172 let stateh h =
1173 let h = truncate (float h*.conf.zoom) in
1174 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
1175 h - d
1178 let opendoc path password =
1179 state.path <- path;
1180 state.password <- password;
1181 state.gen <- state.gen + 1;
1182 state.docinfo <- [];
1184 flushpages ();
1185 setaalevel conf.aalevel;
1186 let titlepath =
1187 if emptystr state.origin
1188 then path
1189 else state.origin
1191 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
1192 wcmd "open %d %d %s\000%s\000" (btod !wtmode) (btod !cxack) path password;
1193 invalidate "reqlayout"
1194 (fun () ->
1195 wcmd "reqlayout %d %d %d %s\000"
1196 conf.angle (FMTE.to_int conf.fitmodel)
1197 (stateh state.winh) state.nameddest
1201 let reload () =
1202 state.anchor <- getanchor ();
1203 opendoc state.path state.password;
1206 let scalecolor c =
1207 let c = c *. conf.colorscale in
1208 (c, c, c);
1211 let scalecolor2 (r, g, b) =
1212 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1215 let docolumns = function
1216 | Csingle _ ->
1217 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1218 let rec loop pageno pdimno pdim y ph pdims =
1219 if pageno = state.pagecount
1220 then ()
1221 else
1222 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1223 match pdims with
1224 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1225 pdimno+1, pdim, rest
1226 | _ ->
1227 pdimno, pdim, pdims
1229 let x = max 0 (((wadjsb state.winw - w) / 2) - xoff) in
1230 let y = y +
1231 (if conf.presentation
1232 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1233 else (if pageno = 0 then 0 else conf.interpagespace)
1236 a.(pageno) <- (pdimno, x, y, pdim);
1237 loop (pageno+1) pdimno pdim (y + h) h pdims
1239 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1240 conf.columns <- Csingle a;
1242 | Cmulti ((columns, coverA, coverB), _) ->
1243 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1244 let rec loop pageno pdimno pdim x y rowh pdims =
1245 let rec fixrow m = if m = pageno then () else
1246 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1247 if h < rowh
1248 then (
1249 let y = y + (rowh - h) / 2 in
1250 a.(m) <- (pdimno, x, y, pdim);
1252 fixrow (m+1)
1254 if pageno = state.pagecount
1255 then fixrow (((pageno - 1) / columns) * columns)
1256 else
1257 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1258 match pdims with
1259 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1260 pdimno+1, pdim, rest
1261 | _ ->
1262 pdimno, pdim, pdims
1264 let x, y, rowh' =
1265 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1266 then (
1267 let x = (wadjsb state.winw - w) / 2 in
1268 let ips =
1269 if conf.presentation then calcips h else conf.interpagespace in
1270 x, y + ips + rowh, h
1272 else (
1273 if (pageno - coverA) mod columns = 0
1274 then (
1275 let x = max 0 (wadjsb state.winw - state.w) / 2 in
1276 let y =
1277 if conf.presentation
1278 then
1279 let ips = calcips h in
1280 y + (if pageno = 0 then 0 else calcips rowh + ips)
1281 else
1282 y + (if pageno = 0 then 0 else conf.interpagespace)
1284 x, y + rowh, h
1286 else x, y, max rowh h
1289 let y =
1290 if pageno > 1 && (pageno - coverA) mod columns = 0
1291 then (
1292 let y =
1293 if pageno = columns && conf.presentation
1294 then (
1295 let ips = calcips rowh in
1296 for i = 0 to pred columns
1298 let (pdimno, x, y, pdim) = a.(i) in
1299 a.(i) <- (pdimno, x, y+ips, pdim)
1300 done;
1301 y+ips;
1303 else y
1305 fixrow (pageno - columns);
1308 else y
1310 a.(pageno) <- (pdimno, x, y, pdim);
1311 let x = x + w + xoff*2 + conf.interpagespace in
1312 loop (pageno+1) pdimno pdim x y rowh' pdims
1314 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1315 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1317 | Csplit (c, _) ->
1318 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1319 let rec loop pageno pdimno pdim y pdims =
1320 if pageno = state.pagecount
1321 then ()
1322 else
1323 let pdimno, ((_, w, h, _) as pdim), pdims =
1324 match pdims with
1325 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1326 pdimno+1, pdim, rest
1327 | _ ->
1328 pdimno, pdim, pdims
1330 let cw = w / c in
1331 let rec loop1 n x y =
1332 if n = c then y else (
1333 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1334 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1337 let y = loop1 0 0 y in
1338 loop (pageno+1) pdimno pdim y pdims
1340 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1341 conf.columns <- Csplit (c, a);
1344 let represent () =
1345 docolumns conf.columns;
1346 state.maxy <- calcheight ();
1347 if state.reprf == noreprf
1348 then (
1349 match state.mode with
1350 | Birdseye (_, _, pageno, _, _) ->
1351 let y, h = getpageyh pageno in
1352 let top = (state.winh - h) / 2 in
1353 gotoy (max 0 (y - top))
1354 | _ -> gotoanchor state.anchor
1356 else (
1357 state.reprf ();
1358 state.reprf <- noreprf;
1362 let reshape w h =
1363 GlDraw.viewport ~x:0 ~y:0 ~w:w ~h:h;
1364 let firsttime = state.geomcmds == firstgeomcmds in
1365 if not firsttime && nogeomcmds state.geomcmds
1366 then state.anchor <- getanchor ();
1368 state.winw <- w;
1369 let w = wadjsb (truncate (float w *. conf.zoom)) in
1370 let w = max w 2 in
1371 state.winh <- h;
1372 setfontsize fstate.fontsize;
1373 GlMat.mode `modelview;
1374 GlMat.load_identity ();
1376 GlMat.mode `projection;
1377 GlMat.load_identity ();
1378 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1379 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1380 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
1382 let relx =
1383 if conf.zoom <= 1.0
1384 then 0.0
1385 else float state.x /. float state.w
1387 invalidate "geometry"
1388 (fun () ->
1389 state.w <- w;
1390 if not firsttime
1391 then state.x <- truncate (relx *. float w);
1392 let w =
1393 match conf.columns with
1394 | Csingle _ -> w
1395 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1396 | Csplit (c, _) -> w * c
1398 wcmd "geometry %d %d %d"
1399 w (stateh h) (FMTE.to_int conf.fitmodel)
1403 let enttext () =
1404 let len = String.length state.text in
1405 let x0 = xadjsb 0 in
1406 let drawstring s =
1407 let hscrollh =
1408 match state.mode with
1409 | Textentry _ | View | LinkNav _ ->
1410 let h, _, _ = state.uioh#scrollpw in
1412 | _ -> 0
1414 let rect x w =
1415 filledrect x (float (state.winh - (fstate.fontsize + 4) - hscrollh))
1416 (x+.w) (float (state.winh - hscrollh))
1419 let w = float (wadjsb state.winw - 1) in
1420 if state.progress >= 0.0 && state.progress < 1.0
1421 then (
1422 GlDraw.color (0.3, 0.3, 0.3);
1423 let w1 = w *. state.progress in
1424 rect (float x0) w1;
1425 GlDraw.color (0.0, 0.0, 0.0);
1426 rect (float x0+.w1) (float x0+.w-.w1)
1428 else (
1429 GlDraw.color (0.0, 0.0, 0.0);
1430 rect (float x0) w;
1433 GlDraw.color (1.0, 1.0, 1.0);
1434 drawstring fstate.fontsize
1435 (if conf.leftscroll then x0 + 2 else x0 + if len > 0 then 8 else 2)
1436 (state.winh - hscrollh - 5) s;
1438 let s =
1439 match state.mode with
1440 | Textentry ((prefix, text, _, _, _, _), _) ->
1441 let s =
1442 if len > 0
1443 then
1444 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1445 else
1446 Printf.sprintf "%s%s_" prefix text
1450 | _ -> state.text
1452 let s =
1453 if state.newerrmsgs
1454 then (
1455 if not (istextentry state.mode) && state.uioh#eformsgs
1456 then
1457 let s1 = "(press 'e' to review error messasges)" in
1458 if nonemptystr s then s ^ " " ^ s1 else s1
1459 else s
1461 else s
1463 if nonemptystr s
1464 then drawstring s
1467 let gctiles () =
1468 let len = Queue.length state.tilelru in
1469 let layout = lazy (
1470 match state.throttle with
1471 | None ->
1472 if conf.preload
1473 then preloadlayout state.y
1474 else state.layout
1475 | Some (layout, _, _) ->
1476 layout
1477 ) in
1478 let rec loop qpos =
1479 if state.memused <= conf.memlimit
1480 then ()
1481 else (
1482 if qpos < len
1483 then
1484 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1485 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1486 let (_, pw, ph, _) = getpagedim n in
1488 gen = state.gen
1489 && colorspace = conf.colorspace
1490 && angle = conf.angle
1491 && pagew = pw
1492 && pageh = ph
1493 && (
1494 let x = col*conf.tilew
1495 and y = row*conf.tileh in
1496 tilevisible (Lazy.force_val layout) n x y
1498 then Queue.push lruitem state.tilelru
1499 else (
1500 freepbo p;
1501 wcmd "freetile %s" (~> p);
1502 state.memused <- state.memused - s;
1503 state.uioh#infochanged Memused;
1504 Hashtbl.remove state.tilemap k;
1506 loop (qpos+1)
1509 loop 0
1512 let logcurrently = function
1513 | Idle -> dolog "Idle"
1514 | Loading (l, gen) ->
1515 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
1516 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
1517 dolog
1518 "Tiling %d[%d,%d] page=%s cs=%s angle"
1519 l.pageno col row (~> pageopaque)
1520 (CSTE.to_string colorspace)
1522 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
1523 angle gen conf.angle state.gen
1524 tilew tileh
1525 conf.tilew conf.tileh
1527 | Outlining _ ->
1528 dolog "outlining"
1531 let splitatspace =
1532 let r = Str.regexp " " in
1533 fun s -> Str.bounded_split r s 2;
1536 let onpagerect pageno f =
1537 let b =
1538 match conf.columns with
1539 | Cmulti (_, b) -> b
1540 | Csingle b -> b
1541 | Csplit (_, b) -> b
1543 if pageno >= 0 && pageno < Array.length b
1544 then
1545 let (_, _, _, (w, h, _, _)) = b.(pageno) in
1546 f w h
1549 let gotopagexy1 pageno x y =
1550 let _,w1,h1,leftx = getpagedim pageno in
1551 let top = y /. (float h1) in
1552 let left = x /. (float w1) in
1553 let py, w, h = getpageywh pageno in
1554 let wh = state.winh - hscrollh () in
1555 let x = left *. (float w) in
1556 let x = leftx + state.x + truncate x in
1557 let sx =
1558 if x < 0 || x >= wadjsb state.winw
1559 then state.x - x
1560 else state.x
1562 let pdy = truncate (top *. float h) in
1563 let y' = py + pdy in
1564 let dy = y' - state.y in
1565 let sy =
1566 if x != state.x || not (dy > 0 && dy < wh)
1567 then (
1568 if conf.presentation
1569 then
1570 if abs (py - y') > wh
1571 then y'
1572 else py
1573 else y';
1575 else state.y
1577 if state.x != sx || state.y != sy
1578 then (
1579 let x, y =
1580 if !wtmode
1581 then (
1582 let ww = wadjsb state.winw in
1583 let qx = sx / ww
1584 and qy = pdy / wh in
1585 let x = qx * ww
1586 and y = py + qy * wh in
1587 let x = if -x + ww > w1 then -(w1-ww) else x
1588 and y' = if y + wh > state.maxy then state.maxy - wh else y in
1589 let y =
1590 if conf.presentation
1591 then
1592 if abs (py - y') > wh
1593 then y'
1594 else py
1595 else y';
1597 (x, y)
1599 else (sx, sy)
1601 state.x <- x;
1602 gotoy_and_clear_text y;
1604 else gotoy_and_clear_text state.y;
1607 let gotopagexy pageno x y =
1608 match state.mode with
1609 | Birdseye _ -> gotopage pageno 0.0
1610 | _ -> gotopagexy1 pageno x y
1613 let act cmds =
1614 (* dolog "%S" cmds; *)
1615 let cl = splitatspace cmds in
1616 let scan s fmt f =
1617 try Scanf.sscanf s fmt f
1618 with exn ->
1619 dolog "error processing '%S': %s" cmds (exntos exn);
1620 exit 1
1622 let addoutline outline =
1623 match state.currently with
1624 | Outlining outlines ->
1625 state.currently <- Outlining (outline :: outlines)
1626 | Idle -> state.currently <- Outlining [outline]
1627 | currently ->
1628 dolog "invalid outlining state";
1629 logcurrently currently
1631 match cl with
1632 | "clear" :: [] ->
1633 state.uioh#infochanged Pdim;
1634 state.pdims <- [];
1636 | "clearrects" :: [] ->
1637 state.rects <- state.rects1;
1638 G.postRedisplay "clearrects";
1640 | "continue" :: args :: [] ->
1641 let n = scan args "%u" (fun n -> n) in
1642 state.pagecount <- n;
1643 begin match state.currently with
1644 | Outlining l ->
1645 state.currently <- Idle;
1646 state.outlines <- Array.of_list (List.rev l)
1647 | _ -> ()
1648 end;
1650 let cur, cmds = state.geomcmds in
1651 if emptystr cur
1652 then failwith "umpossible";
1654 begin match List.rev cmds with
1655 | [] ->
1656 state.geomcmds <- E.s, [];
1657 state.throttle <- None;
1658 represent ();
1659 | (s, f) :: rest ->
1660 f ();
1661 state.geomcmds <- s, List.rev rest;
1662 end;
1663 if conf.maxwait = None && not !wtmode
1664 then G.postRedisplay "continue";
1666 | "title" :: args :: [] ->
1667 conf.title <- args;
1668 Wsi.settitle args
1670 | "msg" :: args :: [] ->
1671 showtext ' ' args
1673 | "vmsg" :: args :: [] ->
1674 if conf.verbose
1675 then showtext ' ' args
1677 | "emsg" :: args :: [] ->
1678 Buffer.add_string state.errmsgs args;
1679 state.newerrmsgs <- true;
1680 G.postRedisplay "error message"
1682 | "progress" :: args :: [] ->
1683 let progress, text =
1684 scan args "%f %n"
1685 (fun f pos ->
1686 f, String.sub args pos (String.length args - pos))
1688 state.text <- text;
1689 state.progress <- progress;
1690 G.postRedisplay "progress"
1692 | "firstmatch" :: args :: [] ->
1693 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1694 scan args "%u %d %f %f %f %f %f %f %f %f"
1695 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1696 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1698 let xoff = float (xadjsb 0) in
1699 let x0 = x0 +. xoff
1700 and x1 = x1 +. xoff
1701 and x2 = x2 +. xoff
1702 and x3 = x3 +. xoff in
1703 let y = (getpagey pageno) + truncate y0 in
1704 addnav ();
1705 gotoy y;
1706 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
1708 | "match" :: args :: [] ->
1709 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1710 scan args "%u %d %f %f %f %f %f %f %f %f"
1711 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1712 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1714 let xoff = float (xadjsb 0) in
1715 let x0 = x0 +. xoff
1716 and x1 = x1 +. xoff
1717 and x2 = x2 +. xoff
1718 and x3 = x3 +. xoff in
1719 state.rects1 <-
1720 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1722 | "page" :: args :: [] ->
1723 let pageopaques, t = scan args "%s %f" (fun p t -> p, t) in
1724 let pageopaque = ~< pageopaques in
1725 begin match state.currently with
1726 | Loading (l, gen) ->
1727 vlog "page %d took %f sec" l.pageno t;
1728 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1729 begin match state.throttle with
1730 | None ->
1731 let preloadedpages =
1732 if conf.preload
1733 then preloadlayout state.y
1734 else state.layout
1736 let evict () =
1737 let set =
1738 List.fold_left (fun s l -> IntSet.add l.pageno s)
1739 IntSet.empty preloadedpages
1741 let evictedpages =
1742 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1743 if not (IntSet.mem pageno set)
1744 then (
1745 wcmd "freepage %s" (~> opaque);
1746 key :: accu
1748 else accu
1749 ) state.pagemap []
1751 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1753 evict ();
1754 state.currently <- Idle;
1755 if gen = state.gen
1756 then (
1757 tilepage l.pageno pageopaque state.layout;
1758 load state.layout;
1759 load preloadedpages;
1760 if pagevisible state.layout l.pageno
1761 && layoutready state.layout
1762 then G.postRedisplay "page";
1765 | Some (layout, _, _) ->
1766 state.currently <- Idle;
1767 tilepage l.pageno pageopaque layout;
1768 load state.layout
1769 end;
1771 | _ ->
1772 dolog "Inconsistent loading state";
1773 logcurrently state.currently;
1774 exit 1
1777 | "tile" :: args :: [] ->
1778 let (x, y, opaques, size, t) =
1779 scan args "%u %u %s %u %f"
1780 (fun x y p size t -> (x, y, p, size, t))
1782 let opaque = ~< opaques in
1783 begin match state.currently with
1784 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1785 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1787 unmappbo opaque;
1788 if tilew != conf.tilew || tileh != conf.tileh
1789 then (
1790 wcmd "freetile %s" (~> opaque);
1791 state.currently <- Idle;
1792 load state.layout;
1794 else (
1795 puttileopaque l col row gen cs angle opaque size t;
1796 state.memused <- state.memused + size;
1797 state.uioh#infochanged Memused;
1798 gctiles ();
1799 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1800 opaque, size) state.tilelru;
1802 let layout =
1803 match state.throttle with
1804 | None -> state.layout
1805 | Some (layout, _, _) -> layout
1808 state.currently <- Idle;
1809 if gen = state.gen
1810 && conf.colorspace = cs
1811 && conf.angle = angle
1812 && tilevisible layout l.pageno x y
1813 then conttiling l.pageno pageopaque;
1815 begin match state.throttle with
1816 | None ->
1817 preload state.layout;
1818 if gen = state.gen
1819 && conf.colorspace = cs
1820 && conf.angle = angle
1821 && tilevisible state.layout l.pageno x y
1822 && (not !wtmode || layoutready state.layout)
1823 then G.postRedisplay "tile nothrottle";
1825 | Some (layout, y, _) ->
1826 let ready = layoutready layout in
1827 if ready
1828 then (
1829 state.y <- y;
1830 state.layout <- layout;
1831 state.throttle <- None;
1832 G.postRedisplay "throttle";
1834 else load layout;
1835 end;
1838 | _ ->
1839 dolog "Inconsistent tiling state";
1840 logcurrently state.currently;
1841 exit 1
1844 | "pdim" :: args :: [] ->
1845 let (n, w, h, _) as pdim =
1846 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1848 let pdim =
1849 match conf.fitmodel, conf.columns with
1850 | (FitPage | FitProportional), Csplit _ -> (n, w, h, 0)
1851 | _ -> pdim
1853 state.uioh#infochanged Pdim;
1854 state.pdims <- pdim :: state.pdims
1856 | "o" :: args :: [] ->
1857 let (l, n, t, h, pos) =
1858 scan args "%u %u %d %u %n"
1859 (fun l n t h pos -> l, n, t, h, pos)
1861 let s = String.sub args pos (String.length args - pos) in
1862 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
1864 | "ou" :: args :: [] ->
1865 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
1866 let s = String.sub args pos len in
1867 let pos2 = pos + len + 1 in
1868 let uri = String.sub args pos2 (String.length args - pos2) in
1869 addoutline (s, l, Ouri uri)
1871 | "on" :: args :: [] ->
1872 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
1873 let s = String.sub args pos (String.length args - pos) in
1874 addoutline (s, l, Onone)
1876 | "a" :: args :: [] ->
1877 let (n, l, t) =
1878 scan args "%u %d %d" (fun n l t -> n, l, t)
1880 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
1882 | "info" :: args :: [] ->
1883 state.docinfo <- (1, args) :: state.docinfo
1885 | "infoend" :: [] ->
1886 state.uioh#infochanged Docinfo;
1887 state.docinfo <- List.rev state.docinfo
1889 | _ ->
1890 error "unknown cmd `%S'" cmds
1893 let onhist cb =
1894 let rc = cb.rc in
1895 let action = function
1896 | HCprev -> cbget cb ~-1
1897 | HCnext -> cbget cb 1
1898 | HCfirst -> cbget cb ~-(cb.rc)
1899 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1900 and cancel () = cb.rc <- rc
1901 in (action, cancel)
1904 let search pattern forward =
1905 match conf.columns with
1906 | Csplit _ ->
1907 showtext '!' "searching does not work properly in split columns mode"
1908 | _ ->
1909 if nonemptystr pattern
1910 then
1911 let pn, py =
1912 match state.layout with
1913 | [] -> 0, 0
1914 | l :: _ ->
1915 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1917 wcmd "search %d %d %d %d,%s\000"
1918 (btod conf.icase) pn py (btod forward) pattern;
1921 let intentry text key =
1922 let c =
1923 if key >= 32 && key < 127
1924 then Char.chr key
1925 else '\000'
1927 match c with
1928 | '0' .. '9' ->
1929 let text = addchar text c in
1930 TEcont text
1932 | _ ->
1933 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1934 TEcont text
1937 let linknentry text key =
1938 let c =
1939 if key >= 32 && key < 127
1940 then Char.chr key
1941 else '\000'
1943 match c with
1944 | 'a' .. 'z' ->
1945 let text = addchar text c in
1946 TEcont text
1948 | _ ->
1949 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1950 TEcont text
1953 let linkndone f s =
1954 if nonemptystr s
1955 then (
1956 let n =
1957 let l = String.length s in
1958 let rec loop pos n = if pos = l then n else
1959 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
1960 loop (pos+1) (n*26 + m)
1961 in loop 0 0
1963 let rec loop n = function
1964 | [] -> ()
1965 | l :: rest ->
1966 match getopaque l.pageno with
1967 | None -> loop n rest
1968 | Some opaque ->
1969 let m = getlinkcount opaque in
1970 if n < m
1971 then (
1972 let under = getlink opaque n in
1973 f under
1975 else loop (n-m) rest
1977 loop n state.layout;
1981 let textentry text key =
1982 if key land 0xff00 = 0xff00
1983 then TEcont text
1984 else TEcont (text ^ toutf8 key)
1987 let reqlayout angle fitmodel =
1988 match state.throttle with
1989 | None ->
1990 if nogeomcmds state.geomcmds
1991 then state.anchor <- getanchor ();
1992 conf.angle <- angle mod 360;
1993 if conf.angle != 0
1994 then (
1995 match state.mode with
1996 | LinkNav _ -> state.mode <- View
1997 | _ -> ()
1999 conf.fitmodel <- fitmodel;
2000 invalidate "reqlayout"
2001 (fun () ->
2002 wcmd "reqlayout %d %d %d"
2003 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
2005 | _ -> ()
2008 let settrim trimmargins trimfuzz =
2009 if nogeomcmds state.geomcmds
2010 then state.anchor <- getanchor ();
2011 conf.trimmargins <- trimmargins;
2012 conf.trimfuzz <- trimfuzz;
2013 let x0, y0, x1, y1 = trimfuzz in
2014 invalidate "settrim"
2015 (fun () ->
2016 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2017 flushpages ();
2020 let setzoom zoom =
2021 match state.throttle with
2022 | None ->
2023 let zoom = max 0.0001 zoom in
2024 if zoom <> conf.zoom
2025 then (
2026 state.prevzoom <- (conf.zoom, state.x);
2027 conf.zoom <- zoom;
2028 reshape state.winw state.winh;
2029 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2032 | Some (layout, y, started) ->
2033 let time =
2034 match conf.maxwait with
2035 | None -> 0.0
2036 | Some t -> t
2038 let dt = now () -. started in
2039 if dt > time
2040 then (
2041 state.y <- y;
2042 load layout;
2046 let setcolumns mode columns coverA coverB =
2047 state.prevcolumns <- Some (conf.columns, conf.zoom);
2048 if columns < 0
2049 then (
2050 if isbirdseye mode
2051 then showtext '!' "split mode doesn't work in bird's eye"
2052 else (
2053 conf.columns <- Csplit (-columns, E.a);
2054 state.x <- 0;
2055 conf.zoom <- 1.0;
2058 else (
2059 if columns < 2
2060 then (
2061 conf.columns <- Csingle E.a;
2062 state.x <- 0;
2063 setzoom 1.0;
2065 else (
2066 conf.columns <- Cmulti ((columns, coverA, coverB), E.a);
2067 conf.zoom <- 1.0;
2070 reshape state.winw state.winh;
2073 let resetmstate () =
2074 state.mstate <- Mnone;
2075 Wsi.setcursor Wsi.CURSOR_INHERIT;
2078 let enterbirdseye () =
2079 let zoom = float conf.thumbw /. float state.winw in
2080 let birdseyepageno =
2081 let cy = state.winh / 2 in
2082 let fold = function
2083 | [] -> 0
2084 | l :: rest ->
2085 let rec fold best = function
2086 | [] -> best.pageno
2087 | l :: rest ->
2088 let d = cy - (l.pagedispy + l.pagevh/2)
2089 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2090 if abs d < abs dbest
2091 then fold l rest
2092 else best.pageno
2093 in fold l rest
2095 fold state.layout
2097 state.mode <- Birdseye (
2098 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2100 resetmstate ();
2101 conf.zoom <- zoom;
2102 conf.presentation <- false;
2103 conf.interpagespace <- 10;
2104 conf.hlinks <- false;
2105 conf.fitmodel <- FitProportional;
2106 state.x <- 0;
2107 conf.maxwait <- None;
2108 conf.columns <- (
2109 match conf.beyecolumns with
2110 | Some c ->
2111 conf.zoom <- 1.0;
2112 Cmulti ((c, 0, 0), E.a)
2113 | None -> Csingle E.a
2115 if conf.verbose
2116 then
2117 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2118 (100.0*.zoom)
2119 else
2120 state.text <- E.s
2122 reshape state.winw state.winh;
2125 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2126 state.mode <- View;
2127 conf.zoom <- c.zoom;
2128 conf.presentation <- c.presentation;
2129 conf.interpagespace <- c.interpagespace;
2130 conf.maxwait <- c.maxwait;
2131 conf.hlinks <- c.hlinks;
2132 conf.fitmodel <- c.fitmodel;
2133 conf.beyecolumns <- (
2134 match conf.columns with
2135 | Cmulti ((c, _, _), _) -> Some c
2136 | Csingle _ -> None
2137 | Csplit _ -> failwith "leaving bird's eye split mode"
2139 conf.columns <- (
2140 match c.columns with
2141 | Cmulti (c, _) -> Cmulti (c, E.a)
2142 | Csingle _ -> Csingle E.a
2143 | Csplit (c, _) -> Csplit (c, E.a)
2145 if conf.verbose
2146 then
2147 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2148 (100.0*.conf.zoom)
2150 reshape state.winw state.winh;
2151 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2152 state.x <- leftx;
2155 let togglebirdseye () =
2156 match state.mode with
2157 | Birdseye vals -> leavebirdseye vals true
2158 | View -> enterbirdseye ()
2159 | _ -> ()
2162 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2163 let pageno = max 0 (pageno - incr) in
2164 let rec loop = function
2165 | [] -> gotopage1 pageno 0
2166 | l :: _ when l.pageno = pageno ->
2167 if l.pagedispy >= 0 && l.pagey = 0
2168 then G.postRedisplay "upbirdseye"
2169 else gotopage1 pageno 0
2170 | _ :: rest -> loop rest
2172 loop state.layout;
2173 state.text <- E.s;
2174 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2177 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2178 let pageno = min (state.pagecount - 1) (pageno + incr) in
2179 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2180 let rec loop = function
2181 | [] ->
2182 let y, h = getpageyh pageno in
2183 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2184 gotoy (clamp dy)
2185 | l :: _ when l.pageno = pageno ->
2186 if l.pagevh != l.pageh
2187 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2188 else G.postRedisplay "downbirdseye"
2189 | _ :: rest -> loop rest
2191 loop state.layout;
2192 state.text <- E.s;
2195 let boundastep h step =
2196 if step < 0
2197 then bound step ~-h 0
2198 else bound step 0 h
2201 let optentry mode _ key =
2202 let btos b = if b then "on" else "off" in
2203 if key >= 32 && key < 127
2204 then
2205 let c = Char.chr key in
2206 match c with
2207 | 's' ->
2208 let ondone s =
2209 try conf.scrollstep <- int_of_string s with exc ->
2210 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2212 TEswitch ("scroll step: ", E.s, None, intentry, ondone, true)
2214 | 'A' ->
2215 let ondone s =
2217 conf.autoscrollstep <- boundastep state.winh (int_of_string s);
2218 if state.autoscroll <> None
2219 then state.autoscroll <- Some conf.autoscrollstep
2220 with exc ->
2221 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2223 TEswitch ("auto scroll step: ", E.s, None, intentry, ondone, true)
2225 | 'C' ->
2226 let ondone s =
2228 let n, a, b = multicolumns_of_string s in
2229 setcolumns mode n a b;
2230 with exc ->
2231 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
2233 TEswitch ("columns: ", E.s, None, textentry, ondone, true)
2235 | 'Z' ->
2236 let ondone s =
2238 let zoom = float (int_of_string s) /. 100.0 in
2239 setzoom zoom
2240 with exc ->
2241 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2243 TEswitch ("zoom: ", E.s, None, intentry, ondone, true)
2245 | 't' ->
2246 let ondone s =
2248 conf.thumbw <- bound (int_of_string s) 2 4096;
2249 state.text <-
2250 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2251 begin match mode with
2252 | Birdseye beye ->
2253 leavebirdseye beye false;
2254 enterbirdseye ();
2255 | _ -> ();
2257 with exc ->
2258 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2260 TEswitch ("thumbnail width: ", E.s, None, intentry, ondone, true)
2262 | 'R' ->
2263 let ondone s =
2264 match try
2265 Some (int_of_string s)
2266 with exc ->
2267 state.text <- Printf.sprintf "bad integer `%s': %s"
2268 s (exntos exc);
2269 None
2270 with
2271 | Some angle -> reqlayout angle conf.fitmodel
2272 | None -> ()
2274 TEswitch ("rotation: ", E.s, None, intentry, ondone, true)
2276 | 'i' ->
2277 conf.icase <- not conf.icase;
2278 TEdone ("case insensitive search " ^ (btos conf.icase))
2280 | 'p' ->
2281 conf.preload <- not conf.preload;
2282 gotoy state.y;
2283 TEdone ("preload " ^ (btos conf.preload))
2285 | 'v' ->
2286 conf.verbose <- not conf.verbose;
2287 TEdone ("verbose " ^ (btos conf.verbose))
2289 | 'd' ->
2290 conf.debug <- not conf.debug;
2291 TEdone ("debug " ^ (btos conf.debug))
2293 | 'h' ->
2294 conf.maxhfit <- not conf.maxhfit;
2295 state.maxy <- calcheight ();
2296 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2298 | 'c' ->
2299 conf.crophack <- not conf.crophack;
2300 TEdone ("crophack " ^ btos conf.crophack)
2302 | 'a' ->
2303 let s =
2304 match conf.maxwait with
2305 | None ->
2306 conf.maxwait <- Some infinity;
2307 "always wait for page to complete"
2308 | Some _ ->
2309 conf.maxwait <- None;
2310 "show placeholder if page is not ready"
2312 TEdone s
2314 | 'f' ->
2315 conf.underinfo <- not conf.underinfo;
2316 TEdone ("underinfo " ^ btos conf.underinfo)
2318 | 'P' ->
2319 conf.savebmarks <- not conf.savebmarks;
2320 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2322 | 'S' ->
2323 let ondone s =
2325 let pageno, py =
2326 match state.layout with
2327 | [] -> 0, 0
2328 | l :: _ ->
2329 l.pageno, l.pagey
2331 conf.interpagespace <- int_of_string s;
2332 docolumns conf.columns;
2333 state.maxy <- calcheight ();
2334 let y = getpagey pageno in
2335 gotoy (y + py)
2336 with exc ->
2337 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
2339 TEswitch ("vertical margin: ", E.s, None, intentry, ondone, true)
2341 | 'l' ->
2342 let fm =
2343 match conf.fitmodel with
2344 | FitProportional -> FitWidth
2345 | _ -> FitProportional
2347 reqlayout conf.angle fm;
2348 TEdone ("proportional display " ^ btos (fm == FitProportional))
2350 | 'T' ->
2351 settrim (not conf.trimmargins) conf.trimfuzz;
2352 TEdone ("trim margins " ^ btos conf.trimmargins)
2354 | 'I' ->
2355 conf.invert <- not conf.invert;
2356 TEdone ("invert colors " ^ btos conf.invert)
2358 | 'x' ->
2359 let ondone s =
2360 cbput state.hists.sel s;
2361 conf.selcmd <- s;
2363 TEswitch ("selection command: ", E.s, Some (onhist state.hists.sel),
2364 textentry, ondone, true)
2366 | 'M' ->
2367 if conf.pax == None
2368 then conf.pax <- Some (ref (0.0, 0, 0))
2369 else conf.pax <- None;
2370 TEdone ("PAX " ^ btos (conf.pax != None))
2372 | _ ->
2373 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2374 TEstop
2375 else
2376 TEcont state.text
2379 class type lvsource = object
2380 method getitemcount : int
2381 method getitem : int -> (string * int)
2382 method hasaction : int -> bool
2383 method exit :
2384 uioh:uioh ->
2385 cancel:bool ->
2386 active:int ->
2387 first:int ->
2388 pan:int ->
2389 uioh option
2390 method getactive : int
2391 method getfirst : int
2392 method getpan : int
2393 method getminfo : (int * int) array
2394 end;;
2396 class virtual lvsourcebase = object
2397 val mutable m_active = 0
2398 val mutable m_first = 0
2399 val mutable m_pan = 0
2400 method getactive = m_active
2401 method getfirst = m_first
2402 method getpan = m_pan
2403 method getminfo : (int * int) array = E.a
2404 end;;
2406 let withoutlastutf8 s =
2407 let len = String.length s in
2408 if len = 0
2409 then s
2410 else
2411 let rec find pos =
2412 if pos = 0
2413 then pos
2414 else
2415 let b = Char.code s.[pos] in
2416 if b land 0b11000000 = 0b11000000
2417 then pos
2418 else find (pos-1)
2420 let first =
2421 if Char.code s.[len-1] land 0x80 = 0
2422 then len-1
2423 else find (len-1)
2425 String.sub s 0 first;
2428 let textentrykeyboard
2429 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2430 let key =
2431 if key >= 0xffb0 && key <= 0xffb9
2432 then key - 0xffb0 + 48 else key
2434 let enttext te =
2435 state.mode <- Textentry (te, onleave);
2436 state.text <- E.s;
2437 enttext ();
2438 G.postRedisplay "textentrykeyboard enttext";
2440 let histaction cmd =
2441 match opthist with
2442 | None -> ()
2443 | Some (action, _) ->
2444 state.mode <- Textentry (
2445 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
2447 G.postRedisplay "textentry histaction"
2449 match key with
2450 | @backspace ->
2451 if emptystr text && cancelonempty
2452 then (
2453 onleave Cancel;
2454 G.postRedisplay "textentrykeyboard after cancel";
2456 else
2457 let s = withoutlastutf8 text in
2458 enttext (c, s, opthist, onkey, ondone, cancelonempty)
2460 | @enter | @kpenter ->
2461 ondone text;
2462 onleave Confirm;
2463 G.postRedisplay "textentrykeyboard after confirm"
2465 | @up | @kpup -> histaction HCprev
2466 | @down | @kpdown -> histaction HCnext
2467 | @home | @kphome -> histaction HCfirst
2468 | @jend | @kpend -> histaction HClast
2470 | @escape ->
2471 if emptystr text
2472 then (
2473 begin match opthist with
2474 | None -> ()
2475 | Some (_, onhistcancel) -> onhistcancel ()
2476 end;
2477 onleave Cancel;
2478 state.text <- E.s;
2479 G.postRedisplay "textentrykeyboard after cancel2"
2481 else (
2482 enttext (c, E.s, opthist, onkey, ondone, cancelonempty)
2485 | @delete | @kpdelete -> ()
2487 | _ when key != 0
2488 && key land 0xff00 != 0xff00 (* keyboard *)
2489 && key land 0xfe00 != 0xfe00 (* xkb *)
2490 && key land 0xfd00 != 0xfd00 (* 3270 *)
2492 begin match onkey text key with
2493 | TEdone text ->
2494 ondone text;
2495 onleave Confirm;
2496 G.postRedisplay "textentrykeyboard after confirm2";
2498 | TEcont text ->
2499 enttext (c, text, opthist, onkey, ondone, cancelonempty);
2501 | TEstop ->
2502 onleave Cancel;
2503 G.postRedisplay "textentrykeyboard after cancel3"
2505 | TEswitch te ->
2506 state.mode <- Textentry (te, onleave);
2507 G.postRedisplay "textentrykeyboard switch";
2508 end;
2510 | _ ->
2511 vlog "unhandled key %s" (Wsi.keyname key)
2514 let firstof first active =
2515 if first > active || abs (first - active) > fstate.maxrows - 1
2516 then max 0 (active - (fstate.maxrows/2))
2517 else first
2520 let calcfirst first active =
2521 if active > first
2522 then
2523 let rows = active - first in
2524 if rows > fstate.maxrows then active - fstate.maxrows else first
2525 else active
2528 let scrollph y maxy =
2529 let sh = float (maxy + state.winh) /. float state.winh in
2530 let sh = float state.winh /. sh in
2531 let sh = max sh (float conf.scrollh) in
2533 let percent = float y /. float maxy in
2534 let position = (float state.winh -. sh) *. percent in
2536 let position =
2537 if position +. sh > float state.winh
2538 then float state.winh -. sh
2539 else position
2541 position, sh;
2544 let coe s = (s :> uioh);;
2546 class listview ~zebra ~helpmode ~(source:lvsource) ~trusted ~modehash =
2547 object (self)
2548 val m_pan = source#getpan
2549 val m_first = source#getfirst
2550 val m_active = source#getactive
2551 val m_qsearch = E.s
2552 val m_prev_uioh = state.uioh
2554 method private elemunder y =
2555 if y < 0
2556 then None
2557 else
2558 let n = y / (fstate.fontsize+1) in
2559 if m_first + n < source#getitemcount
2560 then (
2561 if source#hasaction (m_first + n)
2562 then Some (m_first + n)
2563 else None
2565 else None
2567 method display =
2568 Gl.enable `blend;
2569 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
2570 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2571 filledrect 0. 0. (float state.winw) (float state.winh);
2572 GlDraw.color (1., 1., 1.);
2573 Gl.enable `texture_2d;
2574 let fs = fstate.fontsize in
2575 let nfs = fs + 1 in
2576 let hw = (wadjsb (xadjsb state.winw))/3 in
2577 let ww = fstate.wwidth in
2578 let tabw = 17.0*.ww in
2579 let itemcount = source#getitemcount in
2580 let minfo = source#getminfo in
2581 let x0, x1 =
2582 if conf.leftscroll
2583 then float (xadjsb 0), float (state.winw - 1)
2584 else 0.0, float (state.winw - conf.scrollbw - 1)
2586 let rec loop row =
2587 if (row - m_first) > fstate.maxrows
2588 then ()
2589 else (
2590 if row >= 0 && row < itemcount
2591 then (
2592 let (s, level) = source#getitem row in
2593 let y = (row - m_first) * nfs in
2594 let x =
2595 (if conf.leftscroll then float (xadjsb 0) else 5.0)
2596 +. (float (level + m_pan)) *. ww in
2597 if helpmode
2598 then GlDraw.color
2599 (let c = if row land 1 = 0 then 1.0 else 0.92 in (c,c,c));
2601 if row = m_active
2602 then (
2603 Gl.disable `texture_2d;
2604 let alpha = if source#hasaction row then 0.9 else 0.3 in
2605 GlDraw.color (1., 1., 1.) ~alpha;
2606 linerect (x0 +. 1.) (float (y + 1)) (x1) (float (y + fs + 3));
2607 Gl.enable `texture_2d;
2609 let c =
2610 if zebra && row land 1 = 1
2611 then 0.8
2612 else 1.0
2614 GlDraw.color (c,c,c);
2615 let drawtabularstring s =
2616 let drawstr x s =
2617 let x' = truncate (x0 +. x) in
2618 let pos = nindex s '\000' in
2619 if pos = -1
2620 then drawstring1 fs x' (y+nfs) s
2621 else
2622 let s1 = String.sub s 0 pos
2623 and s2 = String.sub s (pos+1) (String.length s - pos - 1) in
2624 let rec e s =
2625 if emptystr s
2626 then s
2627 else
2628 let s' = withoutlastutf8 s in
2629 let s = s' ^ "@Uellipsis" in
2630 let w = measurestr fs s in
2631 if float x' +. w +. ww < float (hw + x')
2632 then s
2633 else e s'
2635 let s1 =
2636 if float x' +. ww +. measurestr fs s1 > float (hw + x')
2637 then e s1
2638 else s1
2640 ignore (drawstring1 fs x' (y+nfs) s1);
2641 drawstring1 fs (hw + x') (y+nfs) s2
2643 if trusted
2644 then
2645 let x = if helpmode && row > 0 then x +. ww else x in
2646 let tabpos = nindex s '\t' in
2647 if tabpos > 0
2648 then
2649 let len = String.length s - tabpos - 1 in
2650 let s1 = String.sub s 0 tabpos
2651 and s2 = String.sub s (tabpos + 1) len in
2652 let nx = drawstr x s1 in
2653 let sw = nx -. x in
2654 let x = x +. (max tabw sw) in
2655 drawstr x s2
2656 else
2657 let len = String.length s - 2 in
2658 if len > 0 && s.[0] = '\xc2' && s.[1] = '\xb7'
2659 then
2660 let s = String.sub s 2 len in
2661 let x = if not helpmode then x +. ww else x in
2662 GlDraw.color (1.2, 1.2, 1.2);
2663 let vinc = drawstring1 (fs+fs/4)
2664 (truncate (x -. ww)) (y+nfs) s in
2665 GlDraw.color (1., 1., 1.);
2666 vinc +. (float fs *. 0.8)
2667 else
2668 drawstr x s
2669 else
2670 drawstr x s
2672 ignore (drawtabularstring s);
2673 loop (row+1)
2677 loop m_first;
2678 GlDraw.color (1.0, 1.0, 1.0) ~alpha:0.5;
2679 let rec loop row =
2680 if (row - m_first) > fstate.maxrows
2681 then ()
2682 else (
2683 if row >= 0 && row < itemcount
2684 then (
2685 let (s, level) = source#getitem row in
2686 let pos0 = nindex s '\000' in
2687 let y = (row - m_first) * nfs in
2688 let x = float (level + m_pan) *. ww in
2689 let (first, last) = minfo.(row) in
2690 let prefix =
2691 if pos0 > 0 && first > pos0
2692 then String.sub s (pos0+1) (first-pos0-1)
2693 else String.sub s 0 first
2695 let suffix = String.sub s first (last - first) in
2696 let w1 = measurestr fstate.fontsize prefix in
2697 let w2 = measurestr fstate.fontsize suffix in
2698 let x = x +. if conf.leftscroll then float (xadjsb 5) else 5.0 in
2699 let x = if pos0 > 0 && first > pos0 then x +. float hw else x in
2700 let x0 = x +. w1
2701 and y0 = float (y+2) in
2702 let x1 = x0 +. w2
2703 and y1 = float (y+fs+3) in
2704 filledrect x0 y0 x1 y1;
2705 loop (row+1)
2709 Gl.disable `texture_2d;
2710 if Array.length minfo > 0 then loop m_first;
2711 Gl.disable `blend;
2713 method updownlevel incr =
2714 let len = source#getitemcount in
2715 let curlevel =
2716 if m_active >= 0 && m_active < len
2717 then snd (source#getitem m_active)
2718 else -1
2720 let rec flow i =
2721 if i = len then i-1 else if i = -1 then 0 else
2722 let _, l = source#getitem i in
2723 if l != curlevel then i else flow (i+incr)
2725 let active = flow m_active in
2726 let first = calcfirst m_first active in
2727 G.postRedisplay "outline updownlevel";
2728 {< m_active = active; m_first = first >}
2730 method private key1 key mask =
2731 let set1 active first qsearch =
2732 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2734 let search active pattern incr =
2735 let active = if active = -1 then m_first else active in
2736 let dosearch re =
2737 let rec loop n =
2738 if n >= 0 && n < source#getitemcount
2739 then (
2740 let s, _ = source#getitem n in
2742 (try ignore (Str.search_forward re s 0); true
2743 with Not_found -> false)
2744 then Some n
2745 else loop (n + incr)
2747 else None
2749 loop active
2752 let re = Str.regexp_case_fold pattern in
2753 dosearch re
2754 with Failure s ->
2755 state.text <- s;
2756 None
2758 let itemcount = source#getitemcount in
2759 let find start incr =
2760 let rec find i =
2761 if i = -1 || i = itemcount
2762 then -1
2763 else (
2764 if source#hasaction i
2765 then i
2766 else find (i + incr)
2769 find start
2771 let set active first =
2772 let first = bound first 0 (itemcount - fstate.maxrows) in
2773 state.text <- E.s;
2774 coe {< m_active = active; m_first = first; m_qsearch = E.s >}
2776 let navigate incr =
2777 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2778 let active, first =
2779 let incr1 = if incr > 0 then 1 else -1 in
2780 if isvisible m_first m_active
2781 then
2782 let next =
2783 let next = m_active + incr in
2784 let next =
2785 if next < 0 || next >= itemcount
2786 then -1
2787 else find next incr1
2789 if abs (m_active - next) > fstate.maxrows
2790 then -1
2791 else next
2793 if next = -1
2794 then
2795 let first = m_first + incr in
2796 let first = bound first 0 (itemcount - fstate.maxrows) in
2797 let next =
2798 let next = m_active + incr in
2799 let next = bound next 0 (itemcount - 1) in
2800 find next ~-incr1
2802 let active =
2803 if next = -1
2804 then m_active
2805 else (
2806 if isvisible first next
2807 then next
2808 else m_active
2811 active, first
2812 else
2813 let first = min next m_first in
2814 let first =
2815 if abs (next - first) > fstate.maxrows
2816 then first + incr
2817 else first
2819 next, first
2820 else
2821 let first = m_first + incr in
2822 let first = bound first 0 (itemcount - 1) in
2823 let active =
2824 let next = m_active + incr in
2825 let next = bound next 0 (itemcount - 1) in
2826 let next = find next incr1 in
2827 let active =
2828 if next = -1 || abs (m_active - first) > fstate.maxrows
2829 then (
2830 let active = if m_active = -1 then next else m_active in
2831 active
2833 else next
2835 if isvisible first active
2836 then active
2837 else -1
2839 active, first
2841 G.postRedisplay "listview navigate";
2842 set active first;
2844 match key with
2845 | (@r|@s) when Wsi.withctrl mask ->
2846 let incr = if key = @r then -1 else 1 in
2847 let active, first =
2848 match search (m_active + incr) m_qsearch incr with
2849 | None ->
2850 state.text <- m_qsearch ^ " [not found]";
2851 m_active, m_first
2852 | Some active ->
2853 state.text <- m_qsearch;
2854 active, firstof m_first active
2856 G.postRedisplay "listview ctrl-r/s";
2857 set1 active first m_qsearch;
2859 | @insert when Wsi.withctrl mask ->
2860 if m_active >= 0 && m_active < source#getitemcount
2861 then (
2862 let s, _ = source#getitem m_active in
2863 selstring s;
2865 coe self
2867 | @backspace ->
2868 if emptystr m_qsearch
2869 then coe self
2870 else (
2871 let qsearch = withoutlastutf8 m_qsearch in
2872 if emptystr qsearch
2873 then (
2874 state.text <- E.s;
2875 G.postRedisplay "listview empty qsearch";
2876 set1 m_active m_first E.s;
2878 else
2879 let active, first =
2880 match search m_active qsearch ~-1 with
2881 | None ->
2882 state.text <- qsearch ^ " [not found]";
2883 m_active, m_first
2884 | Some active ->
2885 state.text <- qsearch;
2886 active, firstof m_first active
2888 G.postRedisplay "listview backspace qsearch";
2889 set1 active first qsearch
2892 | key when (key != 0 && key land 0xff00 != 0xff00) ->
2893 let pattern = m_qsearch ^ toutf8 key in
2894 let active, first =
2895 match search m_active pattern 1 with
2896 | None ->
2897 state.text <- pattern ^ " [not found]";
2898 m_active, m_first
2899 | Some active ->
2900 state.text <- pattern;
2901 active, firstof m_first active
2903 G.postRedisplay "listview qsearch add";
2904 set1 active first pattern;
2906 | @escape ->
2907 state.text <- E.s;
2908 if emptystr m_qsearch
2909 then (
2910 G.postRedisplay "list view escape";
2911 begin
2912 match
2913 source#exit ~uioh:(coe self)
2914 ~cancel:true ~active:m_active ~first:m_first ~pan:m_pan
2915 with
2916 | None -> m_prev_uioh
2917 | Some uioh -> uioh
2920 else (
2921 G.postRedisplay "list view kill qsearch";
2922 coe {< m_qsearch = E.s >}
2925 | @enter | @kpenter ->
2926 state.text <- E.s;
2927 let self = {< m_qsearch = E.s >} in
2928 let opt =
2929 G.postRedisplay "listview enter";
2930 if m_active >= 0 && m_active < source#getitemcount
2931 then (
2932 source#exit ~uioh:(coe self) ~cancel:false
2933 ~active:m_active ~first:m_first ~pan:m_pan;
2935 else (
2936 source#exit ~uioh:(coe self) ~cancel:true
2937 ~active:m_active ~first:m_first ~pan:m_pan;
2940 begin match opt with
2941 | None -> m_prev_uioh
2942 | Some uioh -> uioh
2945 | @delete | @kpdelete ->
2946 coe self
2948 | @up | @kpup -> navigate ~-1
2949 | @down | @kpdown -> navigate 1
2950 | @prior | @kpprior -> navigate ~-(fstate.maxrows)
2951 | @next | @kpnext -> navigate fstate.maxrows
2953 | @right | @kpright ->
2954 state.text <- E.s;
2955 G.postRedisplay "listview right";
2956 coe {< m_pan = m_pan - 1 >}
2958 | @left | @kpleft ->
2959 state.text <- E.s;
2960 G.postRedisplay "listview left";
2961 coe {< m_pan = m_pan + 1 >}
2963 | @home | @kphome ->
2964 let active = find 0 1 in
2965 G.postRedisplay "listview home";
2966 set active 0;
2968 | @jend | @kpend ->
2969 let first = max 0 (itemcount - fstate.maxrows) in
2970 let active = find (itemcount - 1) ~-1 in
2971 G.postRedisplay "listview end";
2972 set active first;
2974 | key when (key = 0 || key land 0xff00 = 0xff00) ->
2975 coe self
2977 | _ ->
2978 dolog "listview unknown key %#x" key; coe self
2980 method key key mask =
2981 match state.mode with
2982 | Textentry te -> textentrykeyboard key mask te; coe self
2983 | _ -> self#key1 key mask
2985 method button button down x y _ =
2986 let opt =
2987 match button with
2988 | 1 when x > state.winw - conf.scrollbw ->
2989 G.postRedisplay "listview scroll";
2990 if down
2991 then
2992 let _, position, sh = self#scrollph in
2993 if y > truncate position && y < truncate (position +. sh)
2994 then (
2995 state.mstate <- Mscrolly;
2996 Some (coe self)
2998 else
2999 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3000 let first = truncate (s *. float source#getitemcount) in
3001 let first = min source#getitemcount first in
3002 Some (coe {< m_first = first; m_active = first >})
3003 else (
3004 state.mstate <- Mnone;
3005 Some (coe self);
3007 | 1 when not down ->
3008 begin match self#elemunder y with
3009 | Some n ->
3010 G.postRedisplay "listview click";
3011 source#exit ~uioh:(coe {< m_active = n >})
3012 ~cancel:false ~active:n ~first:m_first ~pan:m_pan
3013 | _ ->
3014 Some (coe self)
3016 | n when (n == 4 || n == 5) && not down ->
3017 let len = source#getitemcount in
3018 let first =
3019 if n = 5 && m_first + fstate.maxrows >= len
3020 then
3021 m_first
3022 else
3023 let first = m_first + (if n == 4 then -1 else 1) in
3024 bound first 0 (len - 1)
3026 G.postRedisplay "listview wheel";
3027 Some (coe {< m_first = first >})
3028 | n when (n = 6 || n = 7) && not down ->
3029 let inc = if n = 7 then -1 else 1 in
3030 G.postRedisplay "listview hwheel";
3031 Some (coe {< m_pan = m_pan + inc >})
3032 | _ ->
3033 Some (coe self)
3035 match opt with
3036 | None -> m_prev_uioh
3037 | Some uioh -> uioh
3039 method multiclick _ x y = self#button 1 true x y
3041 method motion _ y =
3042 match state.mstate with
3043 | Mscrolly ->
3044 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3045 let first = truncate (s *. float source#getitemcount) in
3046 let first = min source#getitemcount first in
3047 G.postRedisplay "listview motion";
3048 coe {< m_first = first; m_active = first >}
3049 | _ -> coe self
3051 method pmotion x y =
3052 if x < state.winw - conf.scrollbw
3053 then
3054 let n =
3055 match self#elemunder y with
3056 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3057 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3059 let o =
3060 if n != m_active
3061 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3062 else self
3064 coe o
3065 else (
3066 Wsi.setcursor Wsi.CURSOR_INHERIT;
3067 coe self
3070 method infochanged _ = ()
3072 method scrollpw = (0, 0.0, 0.0)
3073 method scrollph =
3074 let nfs = fstate.fontsize + 1 in
3075 let y = m_first * nfs in
3076 let itemcount = source#getitemcount in
3077 let maxi = max 0 (itemcount - fstate.maxrows) in
3078 let maxy = maxi * nfs in
3079 let p, h = scrollph y maxy in
3080 conf.scrollbw, p, h
3082 method modehash = modehash
3083 method eformsgs = false
3084 end;;
3086 class outlinelistview ~zebra ~source =
3087 let settext autonarrow s =
3088 if autonarrow
3089 then
3090 let ss = source#statestr in
3091 state.text <-
3092 if emptystr ss
3093 then "[" ^ s ^ "]"
3094 else "{" ^ ss ^ "} [" ^ s ^ "]"
3095 else state.text <- s
3097 object (self)
3098 inherit listview
3099 ~zebra
3100 ~helpmode:false
3101 ~source:(source :> lvsource)
3102 ~trusted:false
3103 ~modehash:(findkeyhash conf "outline")
3104 as super
3106 val m_autonarrow = false
3108 method! key key mask =
3109 let maxrows =
3110 if emptystr state.text
3111 then fstate.maxrows
3112 else fstate.maxrows - 2
3114 let calcfirst first active =
3115 if active > first
3116 then
3117 let rows = active - first in
3118 if rows > maxrows then active - maxrows else first
3119 else active
3121 let navigate incr =
3122 let active = m_active + incr in
3123 let active = bound active 0 (source#getitemcount - 1) in
3124 let first = calcfirst m_first active in
3125 G.postRedisplay "outline navigate";
3126 coe {< m_active = active; m_first = first >}
3128 let navscroll first =
3129 let active =
3130 let dist = m_active - first in
3131 if dist < 0
3132 then first
3133 else (
3134 if dist < maxrows
3135 then m_active
3136 else first + maxrows
3139 G.postRedisplay "outline navscroll";
3140 coe {< m_first = first; m_active = active >}
3142 let ctrl = Wsi.withctrl mask in
3143 match key with
3144 | @a when ctrl ->
3145 let text =
3146 if m_autonarrow
3147 then (source#denarrow; E.s)
3148 else (
3149 let pattern = source#renarrow in
3150 if nonemptystr m_qsearch
3151 then (source#narrow m_qsearch; m_qsearch)
3152 else pattern
3155 settext (not m_autonarrow) text;
3156 G.postRedisplay "toggle auto narrowing";
3157 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
3159 | @slash when emptystr m_qsearch && not m_autonarrow ->
3160 settext true E.s;
3161 G.postRedisplay "toggle auto narrowing";
3162 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
3164 | @n when ctrl ->
3165 source#narrow m_qsearch;
3166 if not m_autonarrow
3167 then source#add_narrow_pattern m_qsearch;
3168 G.postRedisplay "outline ctrl-n";
3169 coe {< m_first = 0; m_active = 0 >}
3171 | @S when ctrl ->
3172 let active = source#calcactive (getanchor ()) in
3173 let first = firstof m_first active in
3174 G.postRedisplay "outline ctrl-s";
3175 coe {< m_first = first; m_active = active >}
3177 | @u when ctrl ->
3178 G.postRedisplay "outline ctrl-u";
3179 if m_autonarrow && nonemptystr m_qsearch
3180 then (
3181 ignore (source#renarrow);
3182 settext m_autonarrow E.s;
3183 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3185 else (
3186 source#del_narrow_pattern;
3187 let pattern = source#renarrow in
3188 let text =
3189 if emptystr pattern then E.s else "Narrowed to " ^ pattern
3191 settext m_autonarrow text;
3192 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3195 | @l when ctrl ->
3196 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3197 G.postRedisplay "outline ctrl-l";
3198 coe {< m_first = first >}
3200 | @tab when m_autonarrow ->
3201 if nonemptystr m_qsearch
3202 then (
3203 G.postRedisplay "outline list view tab";
3204 source#add_narrow_pattern m_qsearch;
3205 settext true E.s;
3206 coe {< m_qsearch = E.s >}
3208 else coe self
3210 | @escape when m_autonarrow ->
3211 if nonemptystr m_qsearch
3212 then source#add_narrow_pattern m_qsearch;
3213 super#key key mask
3215 | @enter | @kpenter when m_autonarrow ->
3216 if nonemptystr m_qsearch
3217 then source#add_narrow_pattern m_qsearch;
3218 super#key key mask
3220 | key when m_autonarrow && (key != 0 && key land 0xff00 != 0xff00) ->
3221 let pattern = m_qsearch ^ toutf8 key in
3222 G.postRedisplay "outlinelistview autonarrow add";
3223 source#narrow pattern;
3224 settext true pattern;
3225 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3227 | key when m_autonarrow && key = @backspace ->
3228 if emptystr m_qsearch
3229 then coe self
3230 else
3231 let pattern = withoutlastutf8 m_qsearch in
3232 G.postRedisplay "outlinelistview autonarrow backspace";
3233 ignore (source#renarrow);
3234 source#narrow pattern;
3235 settext true pattern;
3236 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3238 | @delete | @kpdelete ->
3239 source#remove m_active;
3240 G.postRedisplay "outline delete";
3241 let active = max 0 (m_active-1) in
3242 coe {< m_first = firstof m_first active;
3243 m_active = active >}
3245 | @up | @kpup when ctrl ->
3246 navscroll (max 0 (m_first - 1))
3248 | @down | @kpdown when ctrl ->
3249 navscroll (min (source#getitemcount - 1) (m_first + 1))
3251 | @up | @kpup -> navigate ~-1
3252 | @down | @kpdown -> navigate 1
3253 | @prior | @kpprior -> navigate ~-(fstate.maxrows)
3254 | @next | @kpnext -> navigate fstate.maxrows
3256 | @right | @kpright ->
3257 let o =
3258 if ctrl
3259 then (
3260 G.postRedisplay "outline ctrl right";
3261 {< m_pan = m_pan + 1 >}
3263 else self#updownlevel 1
3265 coe o
3267 | @left | @kpleft ->
3268 let o =
3269 if ctrl
3270 then (
3271 G.postRedisplay "outline ctrl left";
3272 {< m_pan = m_pan - 1 >}
3274 else self#updownlevel ~-1
3276 coe o
3278 | @home | @kphome ->
3279 G.postRedisplay "outline home";
3280 coe {< m_first = 0; m_active = 0 >}
3282 | @jend | @kpend ->
3283 let active = source#getitemcount - 1 in
3284 let first = max 0 (active - fstate.maxrows) in
3285 G.postRedisplay "outline end";
3286 coe {< m_active = active; m_first = first >}
3288 | _ -> super#key key mask
3291 let gotounder under =
3292 let getpath filename =
3293 let path =
3294 if nonemptystr filename
3295 then
3296 if Filename.is_relative filename
3297 then
3298 let dir = Filename.dirname state.path in
3299 let dir =
3300 if Filename.is_implicit dir
3301 then Filename.concat (Sys.getcwd ()) dir
3302 else dir
3304 Filename.concat dir filename
3305 else filename
3306 else E.s
3308 if Sys.file_exists path
3309 then path
3310 else E.s
3312 match under with
3313 | Ulinkgoto (pageno, top) ->
3314 if pageno >= 0
3315 then (
3316 addnav ();
3317 gotopage1 pageno top;
3320 | Ulinkuri s ->
3321 gotouri s
3323 | Uremote (filename, pageno) ->
3324 let path = getpath filename in
3325 if nonemptystr path
3326 then (
3327 if conf.riani
3328 then
3329 let command = Printf.sprintf "%s -page %d %S" !selfexec pageno path in
3330 try popen command []
3331 with exn ->
3332 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
3333 flush stderr;
3334 else
3335 let anchor = getanchor () in
3336 let ranchor = state.path, state.password, anchor, state.origin in
3337 state.origin <- E.s;
3338 state.anchor <- (pageno, 0.0, 0.0);
3339 state.ranchors <- ranchor :: state.ranchors;
3340 opendoc path E.s;
3342 else showtext '!' ("Could not find " ^ filename)
3344 | Uremotedest (filename, destname) ->
3345 let path = getpath filename in
3346 if nonemptystr path
3347 then (
3348 if conf.riani
3349 then
3350 let command = !selfexec ^ " " ^ path ^ " -dest " ^ destname in
3351 try popen command []
3352 with exn ->
3353 Printf.eprintf
3354 "failed to execute `%s': %s\n" command (exntos exn);
3355 flush stderr;
3356 else
3357 let anchor = getanchor () in
3358 let ranchor = state.path, state.password, anchor, state.origin in
3359 state.origin <- E.s;
3360 state.nameddest <- destname;
3361 state.ranchors <- ranchor :: state.ranchors;
3362 opendoc path E.s;
3364 else showtext '!' ("Could not find " ^ filename)
3366 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
3369 let gotohist (path, (c, bookmarks, x, anchor)) =
3370 Config.save leavebirdseye;
3371 state.anchor <- anchor;
3372 state.x <- x;
3373 state.bookmarks <- bookmarks;
3374 state.origin <- E.s;
3375 setconf conf c;
3376 opendoc path E.s;
3379 let gotooutline (_, _, kind) =
3380 match kind with
3381 | Onone -> ()
3382 | Oanchor anchor ->
3383 let (pageno, y, _) = anchor in
3384 let y = getanchory
3385 (if conf.presentation then (pageno, y, 1.0) else anchor)
3387 addnav ();
3388 gotoghyll y
3389 | Ouri uri -> gotounder (Ulinkuri uri)
3390 | Olaunch cmd -> gotounder (Ulaunch cmd)
3391 | Oremote remote -> gotounder (Uremote remote)
3392 | Ohistory hist -> gotohist hist
3393 | Oremotedest remotedest -> gotounder (Uremotedest remotedest)
3394 | Oaction f -> f ()
3397 let genhistoutlines =
3398 let order ty (p1, c1, _, _, _) (p2, c2, _, _, _) =
3399 match ty with
3400 | `lastvisit -> compare c1.lastvisit c2.lastvisit
3401 | `path -> compare p2 p1
3402 | `file -> compare (Filename.basename p2) (Filename.basename p1)
3403 | `title ->
3404 let e1 = emptystr c1.title
3405 and e2 = emptystr c2.title in
3406 (**) if e1 && e2
3407 then compare (Filename.basename p2) (Filename.basename p1)
3408 else if e1 then -1
3409 else if e2 then 1
3410 else compare c1.title c2.title
3412 let showfullpath = ref false in
3413 fun orderty ->
3414 let setorty s t =
3415 let s = if orderty = t then "[@Uradical] " ^ s else "[ ] " ^ s in
3416 s, 0, Oaction (fun () -> Config.historder := t; reeenterhist := true)
3418 let list = ref [] in
3419 if Config.gethist list
3420 then
3421 let ol =
3422 List.fold_left
3423 (fun accu (path, c, b, x, a) ->
3424 let hist = (path, (c, b, x, a)) in
3425 let s = if !showfullpath then path else Filename.basename path in
3426 let base = mbtoutf8 s in
3427 (base ^ "\000" ^ c.title, 1, Ohistory hist) :: accu
3429 [ setorty "Sort by time of last visit" `lastvisit;
3430 setorty "Sort by file name" `file;
3431 setorty "Sort by path" `path;
3432 setorty "Sort by title" `title;
3433 (if !showfullpath then "@Uradical "
3434 else " ") ^ "Show full path", 0, Oaction (fun () ->
3435 showfullpath := not !showfullpath; reeenterhist := true)
3436 ] (List.sort (order orderty) !list)
3438 Array.of_list ol
3439 else E.a;
3442 let outlinesource sourcetype =
3443 (object (self)
3444 inherit lvsourcebase
3445 val mutable m_items = E.a
3446 val mutable m_minfo = E.a
3447 val mutable m_orig_items = E.a
3448 val mutable m_orig_minfo = E.a
3449 val mutable m_narrow_patterns = []
3450 val mutable m_hadremovals = false
3451 val mutable m_gen = -1
3453 method getitemcount =
3454 Array.length m_items + (if m_hadremovals then 1 else 0)
3456 method getitem n =
3457 if n == Array.length m_items && m_hadremovals
3458 then
3459 ("[Confirm removal]", 0)
3460 else
3461 let s, n, _ = m_items.(n) in
3462 (s, n)
3464 method exit ~uioh ~cancel ~active ~first ~pan =
3465 ignore (uioh, first);
3466 let confrimremoval = m_hadremovals && active = Array.length m_items in
3467 let items, minfo =
3468 if m_narrow_patterns = []
3469 then m_orig_items, m_orig_minfo
3470 else m_items, m_minfo
3472 if not cancel
3473 then (
3474 if not confrimremoval
3475 then (
3476 gotooutline m_items.(active);
3477 m_items <- items;
3478 m_minfo <- minfo;
3480 else (
3481 state.bookmarks <- Array.to_list m_items;
3482 m_orig_items <- m_items;
3483 m_orig_minfo <- m_minfo;
3486 else (
3487 m_items <- items;
3488 m_minfo <- minfo;
3490 m_pan <- pan;
3491 None
3493 method hasaction _ = true
3495 method greetmsg =
3496 if Array.length m_items != Array.length m_orig_items
3497 then
3498 let s =
3499 match m_narrow_patterns with
3500 | one :: [] -> one
3501 | many -> String.concat "@Uellipsis" (List.rev many)
3503 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
3504 else E.s
3506 method statestr =
3507 match m_narrow_patterns with
3508 | [] -> E.s
3509 | one :: [] -> one
3510 | head :: _ -> "@Uellipsis" ^ head
3512 method narrow pattern =
3513 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3514 match reopt with
3515 | None -> ()
3516 | Some re ->
3517 let rec loop accu minfo n =
3518 if n = -1
3519 then (
3520 m_items <- Array.of_list accu;
3521 m_minfo <- Array.of_list minfo;
3523 else
3524 let (s, _, t) as o = m_items.(n) in
3525 let accu, minfo =
3526 match t with
3527 | Oaction _ -> o :: accu, (0, 0) :: minfo
3528 | Onone | Oanchor _ | Ouri _ | Olaunch _
3529 | Oremote _ | Oremotedest _ | Ohistory _ ->
3530 let first =
3531 try Str.search_forward re s 0
3532 with Not_found -> -1
3534 if first >= 0
3535 then o :: accu, (first, Str.match_end ()) :: minfo
3536 else accu, minfo
3538 loop accu minfo (n-1)
3540 loop [] [] (Array.length m_items - 1)
3542 method! getminfo = m_minfo
3544 method denarrow =
3545 m_orig_items <- (
3546 match sourcetype with
3547 | `bookmarks -> Array.of_list state.bookmarks
3548 | `outlines -> state.outlines
3549 | `history -> genhistoutlines !Config.historder
3551 m_minfo <- m_orig_minfo;
3552 m_items <- m_orig_items
3554 method remove m =
3555 if sourcetype = `bookmarks
3556 then
3557 if m >= 0 && m < Array.length m_items
3558 then (
3559 m_hadremovals <- true;
3560 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3561 let n = if n >= m then n+1 else n in
3562 m_items.(n)
3566 method add_narrow_pattern pattern =
3567 m_narrow_patterns <- pattern :: m_narrow_patterns
3569 method del_narrow_pattern =
3570 match m_narrow_patterns with
3571 | _ :: rest -> m_narrow_patterns <- rest
3572 | [] -> ()
3574 method renarrow =
3575 self#denarrow;
3576 match m_narrow_patterns with
3577 | pattern :: [] -> self#narrow pattern; pattern
3578 | list ->
3579 List.fold_left (fun accu pattern ->
3580 self#narrow pattern;
3581 pattern ^ "@Uellipsis" ^ accu) E.s list
3583 method calcactive anchor =
3584 let rely = getanchory anchor in
3585 let rec loop n best bestd =
3586 if n = Array.length m_items
3587 then best
3588 else
3589 let _, _, kind = m_items.(n) in
3590 match kind with
3591 | Oanchor anchor ->
3592 let orely = getanchory anchor in
3593 let d = abs (orely - rely) in
3594 if d < bestd
3595 then loop (n+1) n d
3596 else loop (n+1) best bestd
3597 | Onone | Oremote _ | Olaunch _
3598 | Oremotedest _ | Ouri _ | Ohistory _ | Oaction _ ->
3599 loop (n+1) best bestd
3601 loop 0 ~-1 max_int
3603 method reset anchor items =
3604 m_hadremovals <- false;
3605 if state.gen != m_gen
3606 then (
3607 m_orig_items <- items;
3608 m_items <- items;
3609 m_narrow_patterns <- [];
3610 m_minfo <- E.a;
3611 m_orig_minfo <- E.a;
3612 m_gen <- state.gen;
3614 else (
3615 if items != m_orig_items
3616 then (
3617 m_orig_items <- items;
3618 if m_narrow_patterns == []
3619 then m_items <- items;
3622 let active = self#calcactive anchor in
3623 m_active <- active;
3624 m_first <- firstof m_first active
3625 end)
3628 let enterselector sourcetype =
3629 resetmstate ();
3630 let source = outlinesource sourcetype in
3631 fun errmsg ->
3632 let outlines =
3633 match sourcetype with
3634 | `bookmarks -> Array.of_list state.bookmarks
3635 | `outlines -> state.outlines
3636 | `history -> genhistoutlines !Config.historder
3638 if Array.length outlines = 0
3639 then (
3640 showtext ' ' errmsg;
3642 else (
3643 state.text <- source#greetmsg;
3644 Wsi.setcursor Wsi.CURSOR_INHERIT;
3645 let anchor = getanchor () in
3646 source#reset anchor outlines;
3647 state.uioh <-
3648 coe (new outlinelistview ~zebra:(sourcetype=`history) ~source);
3649 G.postRedisplay "enter selector";
3653 let enteroutlinemode =
3654 let f = enterselector `outlines in
3655 fun () -> f "Document has no outline";
3658 let enterbookmarkmode =
3659 let f = enterselector `bookmarks in
3660 fun () -> f "Document has no bookmarks (yet)";
3663 let enterhistmode () = enterselector `history "No history (yet)";;
3665 let makecheckers () =
3666 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3667 following to say:
3668 converted by Issac Trotts. July 25, 2002 *)
3669 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3670 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3671 let id = GlTex.gen_texture () in
3672 GlTex.bind_texture ~target:`texture_2d id;
3673 GlPix.store (`unpack_alignment 1);
3674 GlTex.image2d image;
3675 List.iter (GlTex.parameter ~target:`texture_2d)
3676 [ `mag_filter `nearest; `min_filter `nearest ];
3680 let setcheckers enabled =
3681 match state.checkerstexid with
3682 | None ->
3683 if enabled then state.checkerstexid <- Some (makecheckers ())
3685 | Some checkerstexid ->
3686 if not enabled
3687 then (
3688 GlTex.delete_texture checkerstexid;
3689 state.checkerstexid <- None;
3693 let describe_location () =
3694 let fn = page_of_y state.y in
3695 let ln = page_of_y (state.y + state.winh - hscrollh () - 1) in
3696 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
3697 let percent =
3698 if maxy <= 0
3699 then 100.
3700 else (100. *. (float state.y /. float maxy))
3702 if fn = ln
3703 then
3704 Printf.sprintf "page %d of %d [%.2f%%]"
3705 (fn+1) state.pagecount percent
3706 else
3707 Printf.sprintf
3708 "pages %d-%d of %d [%.2f%%]"
3709 (fn+1) (ln+1) state.pagecount percent
3712 let setpresentationmode v =
3713 let n = page_of_y state.y in
3714 state.anchor <- (n, 0.0, 1.0);
3715 conf.presentation <- v;
3716 if conf.fitmodel = FitPage
3717 then reqlayout conf.angle conf.fitmodel;
3718 represent ();
3721 let enterinfomode =
3722 let btos b = if b then "@Uradical" else E.s in
3723 let showextended = ref false in
3724 let leave mode = function
3725 | Confirm -> state.mode <- mode
3726 | Cancel -> state.mode <- mode in
3727 let src =
3728 (object
3729 val mutable m_first_time = true
3730 val mutable m_l = []
3731 val mutable m_a = E.a
3732 val mutable m_prev_uioh = nouioh
3733 val mutable m_prev_mode = View
3735 inherit lvsourcebase
3737 method reset prev_mode prev_uioh =
3738 m_a <- Array.of_list (List.rev m_l);
3739 m_l <- [];
3740 m_prev_mode <- prev_mode;
3741 m_prev_uioh <- prev_uioh;
3742 if m_first_time
3743 then (
3744 let rec loop n =
3745 if n >= Array.length m_a
3746 then ()
3747 else
3748 match m_a.(n) with
3749 | _, _, _, Action _ -> m_active <- n
3750 | _ -> loop (n+1)
3752 loop 0;
3753 m_first_time <- false;
3756 method int name get set =
3757 m_l <-
3758 (name, `int get, 1, Action (
3759 fun u ->
3760 let ondone s =
3761 try set (int_of_string s)
3762 with exn ->
3763 state.text <- Printf.sprintf "bad integer `%s': %s"
3764 s (exntos exn)
3766 state.text <- E.s;
3767 let te = name ^ ": ", E.s, None, intentry, ondone, true in
3768 state.mode <- Textentry (te, leave m_prev_mode);
3770 )) :: m_l
3772 method int_with_suffix name get set =
3773 m_l <-
3774 (name, `intws get, 1, Action (
3775 fun u ->
3776 let ondone s =
3777 try set (int_of_string_with_suffix s)
3778 with exn ->
3779 state.text <- Printf.sprintf "bad integer `%s': %s"
3780 s (exntos exn)
3782 state.text <- E.s;
3783 let te =
3784 name ^ ": ", E.s, None, intentry_with_suffix, ondone, true
3786 state.mode <- Textentry (te, leave m_prev_mode);
3788 )) :: m_l
3790 method bool ?(offset=1) ?(btos=btos) name get set =
3791 m_l <-
3792 (name, `bool (btos, get), offset, Action (
3793 fun u ->
3794 let v = get () in
3795 set (not v);
3797 )) :: m_l
3799 method color name get set =
3800 m_l <-
3801 (name, `color get, 1, Action (
3802 fun u ->
3803 let invalid = (nan, nan, nan) in
3804 let ondone s =
3805 let c =
3806 try color_of_string s
3807 with exn ->
3808 state.text <- Printf.sprintf "bad color `%s': %s"
3809 s (exntos exn);
3810 invalid
3812 if c <> invalid
3813 then set c;
3815 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3816 state.text <- color_to_string (get ());
3817 state.mode <- Textentry (te, leave m_prev_mode);
3819 )) :: m_l
3821 method string name get set =
3822 m_l <-
3823 (name, `string get, 1, Action (
3824 fun u ->
3825 let ondone s = set s in
3826 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3827 state.mode <- Textentry (te, leave m_prev_mode);
3829 )) :: m_l
3831 method colorspace name get set =
3832 m_l <-
3833 (name, `string get, 1, Action (
3834 fun _ ->
3835 let source =
3836 (object
3837 inherit lvsourcebase
3839 initializer
3840 m_active <- CSTE.to_int conf.colorspace;
3841 m_first <- 0;
3843 method getitemcount =
3844 Array.length CSTE.names
3845 method getitem n =
3846 (CSTE.names.(n), 0)
3847 method exit ~uioh ~cancel ~active ~first ~pan =
3848 ignore (uioh, first, pan);
3849 if not cancel then set active;
3850 None
3851 method hasaction _ = true
3852 end)
3854 state.text <- E.s;
3855 let modehash = findkeyhash conf "info" in
3856 coe (new listview ~zebra:false ~helpmode:false
3857 ~source ~trusted:true ~modehash)
3858 )) :: m_l
3860 method paxmark name get set =
3861 m_l <-
3862 (name, `string get, 1, Action (
3863 fun _ ->
3864 let source =
3865 (object
3866 inherit lvsourcebase
3868 initializer
3869 m_active <- MTE.to_int conf.paxmark;
3870 m_first <- 0;
3872 method getitemcount = Array.length MTE.names
3873 method getitem n = (MTE.names.(n), 0)
3874 method exit ~uioh ~cancel ~active ~first ~pan =
3875 ignore (uioh, first, pan);
3876 if not cancel then set active;
3877 None
3878 method hasaction _ = true
3879 end)
3881 state.text <- E.s;
3882 let modehash = findkeyhash conf "info" in
3883 coe (new listview ~zebra:false ~helpmode:false
3884 ~source ~trusted:true ~modehash)
3885 )) :: m_l
3887 method fitmodel name get set =
3888 m_l <-
3889 (name, `string get, 1, Action (
3890 fun _ ->
3891 let source =
3892 (object
3893 inherit lvsourcebase
3895 initializer
3896 m_active <- FMTE.to_int conf.fitmodel;
3897 m_first <- 0;
3899 method getitemcount = Array.length FMTE.names
3900 method getitem n = (FMTE.names.(n), 0)
3901 method exit ~uioh ~cancel ~active ~first ~pan =
3902 ignore (uioh, first, pan);
3903 if not cancel then set active;
3904 None
3905 method hasaction _ = true
3906 end)
3908 state.text <- E.s;
3909 let modehash = findkeyhash conf "info" in
3910 coe (new listview ~zebra:false ~helpmode:false
3911 ~source ~trusted:true ~modehash)
3912 )) :: m_l
3914 method caption s offset =
3915 m_l <- (s, `empty, offset, Noaction) :: m_l
3917 method caption2 s f offset =
3918 m_l <- (s, `string f, offset, Noaction) :: m_l
3920 method getitemcount = Array.length m_a
3922 method getitem n =
3923 let tostr = function
3924 | `int f -> string_of_int (f ())
3925 | `intws f -> string_with_suffix_of_int (f ())
3926 | `string f -> f ()
3927 | `color f -> color_to_string (f ())
3928 | `bool (btos, f) -> btos (f ())
3929 | `empty -> E.s
3931 let name, t, offset, _ = m_a.(n) in
3932 ((let s = tostr t in
3933 if nonemptystr s
3934 then Printf.sprintf "%s\t%s" name s
3935 else name),
3936 offset)
3938 method exit ~uioh ~cancel ~active ~first ~pan =
3939 let uiohopt =
3940 if not cancel
3941 then (
3942 let uioh =
3943 match m_a.(active) with
3944 | _, _, _, Action f -> f uioh
3945 | _ -> uioh
3947 Some uioh
3949 else None
3951 m_active <- active;
3952 m_first <- first;
3953 m_pan <- pan;
3954 uiohopt
3956 method hasaction n =
3957 match m_a.(n) with
3958 | _, _, _, Action _ -> true
3959 | _ -> false
3960 end)
3962 let rec fillsrc prevmode prevuioh =
3963 let sep () = src#caption E.s 0 in
3964 let colorp name get set =
3965 src#string name
3966 (fun () -> color_to_string (get ()))
3967 (fun v ->
3969 let c = color_of_string v in
3970 set c
3971 with exn ->
3972 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
3975 let oldmode = state.mode in
3976 let birdseye = isbirdseye state.mode in
3978 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3980 src#bool "presentation mode"
3981 (fun () -> conf.presentation)
3982 (fun v -> setpresentationmode v);
3984 src#bool "ignore case in searches"
3985 (fun () -> conf.icase)
3986 (fun v -> conf.icase <- v);
3988 src#bool "preload"
3989 (fun () -> conf.preload)
3990 (fun v -> conf.preload <- v);
3992 src#bool "highlight links"
3993 (fun () -> conf.hlinks)
3994 (fun v -> conf.hlinks <- v);
3996 src#bool "under info"
3997 (fun () -> conf.underinfo)
3998 (fun v -> conf.underinfo <- v);
4000 src#bool "persistent bookmarks"
4001 (fun () -> conf.savebmarks)
4002 (fun v -> conf.savebmarks <- v);
4004 src#fitmodel "fit model"
4005 (fun () -> FMTE.to_string conf.fitmodel)
4006 (fun v -> reqlayout conf.angle (FMTE.of_int v));
4008 src#bool "trim margins"
4009 (fun () -> conf.trimmargins)
4010 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4012 src#bool "persistent location"
4013 (fun () -> conf.jumpback)
4014 (fun v -> conf.jumpback <- v);
4016 sep ();
4017 src#int "inter-page space"
4018 (fun () -> conf.interpagespace)
4019 (fun n ->
4020 conf.interpagespace <- n;
4021 docolumns conf.columns;
4022 let pageno, py =
4023 match state.layout with
4024 | [] -> 0, 0
4025 | l :: _ ->
4026 l.pageno, l.pagey
4028 state.maxy <- calcheight ();
4029 let y = getpagey pageno in
4030 gotoy (y + py)
4033 src#int "page bias"
4034 (fun () -> conf.pagebias)
4035 (fun v -> conf.pagebias <- v);
4037 src#int "scroll step"
4038 (fun () -> conf.scrollstep)
4039 (fun n -> conf.scrollstep <- n);
4041 src#int "horizontal scroll step"
4042 (fun () -> conf.hscrollstep)
4043 (fun v -> conf.hscrollstep <- v);
4045 src#int "auto scroll step"
4046 (fun () ->
4047 match state.autoscroll with
4048 | Some step -> step
4049 | _ -> conf.autoscrollstep)
4050 (fun n ->
4051 let n = boundastep state.winh n in
4052 if state.autoscroll <> None
4053 then state.autoscroll <- Some n;
4054 conf.autoscrollstep <- n);
4056 src#int "zoom"
4057 (fun () -> truncate (conf.zoom *. 100.))
4058 (fun v -> setzoom ((float v) /. 100.));
4060 src#int "rotation"
4061 (fun () -> conf.angle)
4062 (fun v -> reqlayout v conf.fitmodel);
4064 src#int "scroll bar width"
4065 (fun () -> conf.scrollbw)
4066 (fun v ->
4067 conf.scrollbw <- v;
4068 reshape state.winw state.winh;
4071 src#int "scroll handle height"
4072 (fun () -> conf.scrollh)
4073 (fun v -> conf.scrollh <- v;);
4075 src#int "thumbnail width"
4076 (fun () -> conf.thumbw)
4077 (fun v ->
4078 conf.thumbw <- min 4096 v;
4079 match oldmode with
4080 | Birdseye beye ->
4081 leavebirdseye beye false;
4082 enterbirdseye ()
4083 | _ -> ()
4086 let mode = state.mode in
4087 src#string "columns"
4088 (fun () ->
4089 match conf.columns with
4090 | Csingle _ -> "1"
4091 | Cmulti (multi, _) -> multicolumns_to_string multi
4092 | Csplit (count, _) -> "-" ^ string_of_int count
4094 (fun v ->
4095 let n, a, b = multicolumns_of_string v in
4096 setcolumns mode n a b);
4098 sep ();
4099 src#caption "Pixmap cache" 0;
4100 src#int_with_suffix "size (advisory)"
4101 (fun () -> conf.memlimit)
4102 (fun v -> conf.memlimit <- v);
4104 src#caption2 "used"
4105 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4106 (string_with_suffix_of_int state.memused)
4107 (Hashtbl.length state.tilemap)) 1;
4109 sep ();
4110 src#caption "Layout" 0;
4111 src#caption2 "Dimension"
4112 (fun () ->
4113 Printf.sprintf "%dx%d (virtual %dx%d)"
4114 state.winw state.winh
4115 state.w state.maxy)
4117 if conf.debug
4118 then
4119 src#caption2 "Position" (fun () ->
4120 Printf.sprintf "%dx%d" state.x state.y
4122 else
4123 src#caption2 "Position" (fun () -> describe_location ()) 1
4126 sep ();
4127 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4128 "Save these parameters as global defaults at exit"
4129 (fun () -> conf.bedefault)
4130 (fun v -> conf.bedefault <- v)
4133 sep ();
4134 let btos b = if b then "@Ulguillemet" else "@Urguillemet" in
4135 src#bool ~offset:0 ~btos "Extended parameters"
4136 (fun () -> !showextended)
4137 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4138 if !showextended
4139 then (
4140 src#bool "checkers"
4141 (fun () -> conf.checkers)
4142 (fun v -> conf.checkers <- v; setcheckers v);
4143 src#bool "update cursor"
4144 (fun () -> conf.updatecurs)
4145 (fun v -> conf.updatecurs <- v);
4146 src#bool "scroll-bar on the left"
4147 (fun () -> conf.leftscroll)
4148 (fun v -> conf.leftscroll <- v);
4149 src#bool "verbose"
4150 (fun () -> conf.verbose)
4151 (fun v -> conf.verbose <- v);
4152 src#bool "invert colors"
4153 (fun () -> conf.invert)
4154 (fun v -> conf.invert <- v);
4155 src#bool "max fit"
4156 (fun () -> conf.maxhfit)
4157 (fun v -> conf.maxhfit <- v);
4158 src#bool "redirect stderr"
4159 (fun () -> conf.redirectstderr)
4160 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4161 src#bool "pax mode"
4162 (fun () -> conf.pax != None)
4163 (fun v ->
4164 if v
4165 then conf.pax <- Some (ref (now (), 0, 0))
4166 else conf.pax <- None);
4167 src#string "uri launcher"
4168 (fun () -> conf.urilauncher)
4169 (fun v -> conf.urilauncher <- v);
4170 src#string "path launcher"
4171 (fun () -> conf.pathlauncher)
4172 (fun v -> conf.pathlauncher <- v);
4173 src#string "tile size"
4174 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4175 (fun v ->
4177 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4178 conf.tilew <- max 64 w;
4179 conf.tileh <- max 64 h;
4180 flushtiles ();
4181 with exn ->
4182 state.text <- Printf.sprintf "bad tile size `%s': %s"
4183 v (exntos exn)
4185 src#int "texture count"
4186 (fun () -> conf.texcount)
4187 (fun v ->
4188 if realloctexts v
4189 then conf.texcount <- v
4190 else showtext '!' " Failed to set texture count please retry later"
4192 src#int "slice height"
4193 (fun () -> conf.sliceheight)
4194 (fun v ->
4195 conf.sliceheight <- v;
4196 wcmd "sliceh %d" conf.sliceheight;
4198 src#int "anti-aliasing level"
4199 (fun () -> conf.aalevel)
4200 (fun v ->
4201 conf.aalevel <- bound v 0 8;
4202 state.anchor <- getanchor ();
4203 opendoc state.path state.password;
4205 src#string "page scroll scaling factor"
4206 (fun () -> string_of_float conf.pgscale)
4207 (fun v ->
4209 let s = float_of_string v in
4210 conf.pgscale <- s
4211 with exn ->
4212 state.text <- Printf.sprintf
4213 "bad page scroll scaling factor `%s': %s" v (exntos exn)
4216 src#int "ui font size"
4217 (fun () -> fstate.fontsize)
4218 (fun v -> setfontsize (bound v 5 100));
4219 src#int "hint font size"
4220 (fun () -> conf.hfsize)
4221 (fun v -> conf.hfsize <- bound v 5 100);
4222 colorp "background color"
4223 (fun () -> conf.bgcolor)
4224 (fun v -> conf.bgcolor <- v);
4225 src#bool "crop hack"
4226 (fun () -> conf.crophack)
4227 (fun v -> conf.crophack <- v);
4228 src#string "trim fuzz"
4229 (fun () -> irect_to_string conf.trimfuzz)
4230 (fun v ->
4232 conf.trimfuzz <- irect_of_string v;
4233 if conf.trimmargins
4234 then settrim true conf.trimfuzz;
4235 with exn ->
4236 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
4238 src#string "throttle"
4239 (fun () ->
4240 match conf.maxwait with
4241 | None -> "show place holder if page is not ready"
4242 | Some time ->
4243 if time = infinity
4244 then "wait for page to fully render"
4245 else
4246 "wait " ^ string_of_float time
4247 ^ " seconds before showing placeholder"
4249 (fun v ->
4251 let f = float_of_string v in
4252 if f <= 0.0
4253 then conf.maxwait <- None
4254 else conf.maxwait <- Some f
4255 with exn ->
4256 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
4258 src#string "ghyll scroll"
4259 (fun () ->
4260 match conf.ghyllscroll with
4261 | None -> E.s
4262 | Some nab -> ghyllscroll_to_string nab
4264 (fun v ->
4265 try conf.ghyllscroll <- ghyllscroll_of_string v
4266 with exn ->
4267 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
4269 src#string "selection command"
4270 (fun () -> conf.selcmd)
4271 (fun v -> conf.selcmd <- v);
4272 src#string "synctex command"
4273 (fun () -> conf.stcmd)
4274 (fun v -> conf.stcmd <- v);
4275 src#string "pax command"
4276 (fun () -> conf.paxcmd)
4277 (fun v -> conf.paxcmd <- v);
4278 src#colorspace "color space"
4279 (fun () -> CSTE.to_string conf.colorspace)
4280 (fun v ->
4281 conf.colorspace <- CSTE.of_int v;
4282 wcmd "cs %d" v;
4283 load state.layout;
4285 src#paxmark "pax mark method"
4286 (fun () -> MTE.to_string conf.paxmark)
4287 (fun v -> conf.paxmark <- MTE.of_int v);
4288 if pbousable ()
4289 then
4290 src#bool "use PBO"
4291 (fun () -> conf.usepbo)
4292 (fun v -> conf.usepbo <- v);
4293 src#bool "mouse wheel scrolls pages"
4294 (fun () -> conf.wheelbypage)
4295 (fun v -> conf.wheelbypage <- v);
4296 src#bool "open remote links in a new instance"
4297 (fun () -> conf.riani)
4298 (fun v -> conf.riani <- v);
4301 sep ();
4302 src#caption "Document" 0;
4303 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4304 src#caption2 "Pages"
4305 (fun () -> string_of_int state.pagecount) 1;
4306 src#caption2 "Dimensions"
4307 (fun () -> string_of_int (List.length state.pdims)) 1;
4308 if conf.trimmargins
4309 then (
4310 sep ();
4311 src#caption "Trimmed margins" 0;
4312 src#caption2 "Dimensions"
4313 (fun () -> string_of_int (List.length state.pdims)) 1;
4316 sep ();
4317 src#caption "OpenGL" 0;
4318 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4319 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4321 sep ();
4322 src#caption "Location" 0;
4323 if nonemptystr state.origin
4324 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
4325 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
4327 src#reset prevmode prevuioh;
4329 fun () ->
4330 state.text <- E.s;
4331 resetmstate ();
4332 let prevmode = state.mode
4333 and prevuioh = state.uioh in
4334 fillsrc prevmode prevuioh;
4335 let source = (src :> lvsource) in
4336 let modehash = findkeyhash conf "info" in
4337 state.uioh <- coe (object (self)
4338 inherit listview ~zebra:false ~helpmode:false
4339 ~source ~trusted:true ~modehash as super
4340 val mutable m_prevmemused = 0
4341 method! infochanged = function
4342 | Memused ->
4343 if m_prevmemused != state.memused
4344 then (
4345 m_prevmemused <- state.memused;
4346 G.postRedisplay "memusedchanged";
4348 | Pdim -> G.postRedisplay "pdimchanged"
4349 | Docinfo -> fillsrc prevmode prevuioh
4351 method! key key mask =
4352 if not (Wsi.withctrl mask)
4353 then
4354 match key with
4355 | @left | @kpleft -> coe (self#updownlevel ~-1)
4356 | @right | @kpright -> coe (self#updownlevel 1)
4357 | _ -> super#key key mask
4358 else super#key key mask
4359 end);
4360 G.postRedisplay "info";
4363 let enterhelpmode =
4364 let source =
4365 (object
4366 inherit lvsourcebase
4367 method getitemcount = Array.length state.help
4368 method getitem n =
4369 let s, l, _ = state.help.(n) in
4370 (s, l)
4372 method exit ~uioh ~cancel ~active ~first ~pan =
4373 let optuioh =
4374 if not cancel
4375 then (
4376 match state.help.(active) with
4377 | _, _, Action f -> Some (f uioh)
4378 | _ -> Some (uioh)
4380 else None
4382 m_active <- active;
4383 m_first <- first;
4384 m_pan <- pan;
4385 optuioh
4387 method hasaction n =
4388 match state.help.(n) with
4389 | _, _, Action _ -> true
4390 | _ -> false
4392 initializer
4393 m_active <- -1
4394 end)
4395 in fun () ->
4396 let modehash = findkeyhash conf "help" in
4397 resetmstate ();
4398 state.uioh <- coe (new listview
4399 ~zebra:false ~helpmode:true
4400 ~source ~trusted:true ~modehash);
4401 G.postRedisplay "help";
4404 let entermsgsmode =
4405 let msgsource =
4406 let re = Str.regexp "[\r\n]" in
4407 (object
4408 inherit lvsourcebase
4409 val mutable m_items = E.a
4411 method getitemcount = 1 + Array.length m_items
4413 method getitem n =
4414 if n = 0
4415 then "[Clear]", 0
4416 else m_items.(n-1), 0
4418 method exit ~uioh ~cancel ~active ~first ~pan =
4419 ignore uioh;
4420 if not cancel
4421 then (
4422 if active = 0
4423 then Buffer.clear state.errmsgs;
4425 m_active <- active;
4426 m_first <- first;
4427 m_pan <- pan;
4428 None
4430 method hasaction n =
4431 n = 0
4433 method reset =
4434 state.newerrmsgs <- false;
4435 let l = Str.split re (Buffer.contents state.errmsgs) in
4436 m_items <- Array.of_list l
4438 initializer
4439 m_active <- 0
4440 end)
4441 in fun () ->
4442 state.text <- E.s;
4443 resetmstate ();
4444 msgsource#reset;
4445 let source = (msgsource :> lvsource) in
4446 let modehash = findkeyhash conf "listview" in
4447 state.uioh <- coe (object
4448 inherit listview ~zebra:false ~helpmode:false
4449 ~source ~trusted:false ~modehash as super
4450 method! display =
4451 if state.newerrmsgs
4452 then msgsource#reset;
4453 super#display
4454 end);
4455 G.postRedisplay "msgs";
4458 let quickbookmark ?title () =
4459 match state.layout with
4460 | [] -> ()
4461 | l :: _ ->
4462 let title =
4463 match title with
4464 | None ->
4465 let tm = Unix.localtime (now ()) in
4466 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4467 (l.pageno+1)
4468 tm.Unix.tm_mday
4469 tm.Unix.tm_mon
4470 (tm.Unix.tm_year + 1900)
4471 tm.Unix.tm_hour
4472 tm.Unix.tm_min
4473 | Some title -> title
4475 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4478 let setautoscrollspeed step goingdown =
4479 let incr = max 1 ((abs step) / 2) in
4480 let incr = if goingdown then incr else -incr in
4481 let astep = boundastep state.winh (step + incr) in
4482 state.autoscroll <- Some astep;
4485 let canpan () =
4486 match conf.columns with
4487 | Csplit _ -> true
4488 | _ -> state.x != 0 || conf.zoom > 1.0
4491 let panbound x = bound x (-state.w) (wadjsb state.winw);;
4493 let existsinrow pageno (columns, coverA, coverB) p =
4494 let last = ((pageno - coverA) mod columns) + columns in
4495 let rec any = function
4496 | [] -> false
4497 | l :: rest ->
4498 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4499 then p l
4500 else (
4501 if not (p l)
4502 then (if l.pageno = last then false else any rest)
4503 else true
4506 any state.layout
4509 let nextpage () =
4510 match state.layout with
4511 | [] ->
4512 let pageno = page_of_y state.y in
4513 gotoghyll (getpagey (pageno+1))
4514 | l :: rest ->
4515 match conf.columns with
4516 | Csingle _ ->
4517 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4518 then
4519 let y = clamp (pgscale state.winh) in
4520 gotoghyll y
4521 else
4522 let pageno = min (l.pageno+1) (state.pagecount-1) in
4523 gotoghyll (getpagey pageno)
4524 | Cmulti ((c, _, _) as cl, _) ->
4525 if conf.presentation
4526 && (existsinrow l.pageno cl
4527 (fun l -> l.pageh > l.pagey + l.pagevh))
4528 then
4529 let y = clamp (pgscale state.winh) in
4530 gotoghyll y
4531 else
4532 let pageno = min (l.pageno+c) (state.pagecount-1) in
4533 gotoghyll (getpagey pageno)
4534 | Csplit (n, _) ->
4535 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4536 then
4537 let pagey, pageh = getpageyh l.pageno in
4538 let pagey = pagey + pageh * l.pagecol in
4539 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4540 gotoghyll (pagey + pageh + ips)
4543 let prevpage () =
4544 match state.layout with
4545 | [] ->
4546 let pageno = page_of_y state.y in
4547 gotoghyll (getpagey (pageno-1))
4548 | l :: _ ->
4549 match conf.columns with
4550 | Csingle _ ->
4551 if conf.presentation && l.pagey != 0
4552 then
4553 gotoghyll (clamp (pgscale ~-(state.winh)))
4554 else
4555 let pageno = max 0 (l.pageno-1) in
4556 gotoghyll (getpagey pageno)
4557 | Cmulti ((c, _, coverB) as cl, _) ->
4558 if conf.presentation &&
4559 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4560 then
4561 gotoghyll (clamp (pgscale ~-(state.winh)))
4562 else
4563 let decr =
4564 if l.pageno = state.pagecount - coverB
4565 then 1
4566 else c
4568 let pageno = max 0 (l.pageno-decr) in
4569 gotoghyll (getpagey pageno)
4570 | Csplit (n, _) ->
4571 let y =
4572 if l.pagecol = 0
4573 then
4574 if l.pageno = 0
4575 then l.pagey
4576 else
4577 let pageno = max 0 (l.pageno-1) in
4578 let pagey, pageh = getpageyh pageno in
4579 pagey + (n-1)*pageh
4580 else
4581 let pagey, pageh = getpageyh l.pageno in
4582 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4584 gotoghyll y
4587 let viewkeyboard key mask =
4588 let enttext te =
4589 let mode = state.mode in
4590 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4591 state.text <- E.s;
4592 enttext ();
4593 G.postRedisplay "view:enttext"
4595 let ctrl = Wsi.withctrl mask in
4596 let key =
4597 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
4599 match key with
4600 | @Q -> exit 0
4601 | @insert ->
4602 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4603 then (
4604 state.mode <- LinkNav (Ltgendir 0);
4605 gotoy state.y;
4607 else showtext '!' "Keyboard link navigation does not work under rotation"
4609 | @escape | @q ->
4610 begin match state.mstate with
4611 | Mzoomrect _ ->
4612 resetmstate ();
4613 G.postRedisplay "kill zoom rect";
4614 | _ ->
4615 begin match state.mode with
4616 | LinkNav _ ->
4617 state.mode <- View;
4618 G.postRedisplay "esc leave linknav"
4619 | _ ->
4620 match state.ranchors with
4621 | [] -> raise Quit
4622 | (path, password, anchor, origin) :: rest ->
4623 state.ranchors <- rest;
4624 state.anchor <- anchor;
4625 state.origin <- origin;
4626 state.nameddest <- E.s;
4627 opendoc path password
4628 end;
4629 end;
4631 | @backspace ->
4632 gotoghyll (getnav ~-1)
4634 | @o ->
4635 enteroutlinemode ()
4637 | @H ->
4638 enterhistmode ()
4640 | @u ->
4641 state.rects <- [];
4642 state.text <- E.s;
4643 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap;
4644 G.postRedisplay "dehighlight";
4646 | @slash | @question ->
4647 let ondone isforw s =
4648 cbput state.hists.pat s;
4649 state.searchpattern <- s;
4650 search s isforw
4652 let s = String.create 1 in
4653 s.[0] <- Char.chr key;
4654 enttext (s, E.s, Some (onhist state.hists.pat),
4655 textentry, ondone (key = @slash), true)
4657 | @plus | @kpplus | @equals when ctrl ->
4658 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4659 setzoom (conf.zoom +. incr)
4661 | @plus | @kpplus ->
4662 let ondone s =
4663 let n =
4664 try int_of_string s with exc ->
4665 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
4666 max_int
4668 if n != max_int
4669 then (
4670 conf.pagebias <- n;
4671 state.text <- "page bias is now " ^ string_of_int n;
4674 enttext ("page bias: ", E.s, None, intentry, ondone, true)
4676 | @minus | @kpminus when ctrl ->
4677 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4678 setzoom (max 0.01 (conf.zoom -. decr))
4680 | @minus | @kpminus ->
4681 let ondone msg = state.text <- msg in
4682 enttext (
4683 "option [acfhilpstvxACFPRSZTISM]: ", E.s, None,
4684 optentry state.mode, ondone, true
4687 | @0 when ctrl ->
4688 if conf.zoom = 1.0
4689 then (
4690 state.x <- 0;
4691 gotoy state.y
4693 else setzoom 1.0
4695 | (@1 | @2) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
4696 let cols =
4697 match conf.columns with
4698 | Csingle _ | Cmulti _ -> 1
4699 | Csplit (n, _) -> n
4701 let h = state.winh -
4702 conf.interpagespace lsl (if conf.presentation then 1 else 0)
4704 let zoom = zoomforh state.winw h (vscrollw ()) cols in
4705 if zoom > 0.0 && (key = 50 || zoom < 1.0)
4706 then setzoom zoom
4708 | @3 when ctrl ->
4709 let fm =
4710 match conf.fitmodel with
4711 | FitWidth -> FitProportional
4712 | FitProportional -> FitPage
4713 | FitPage -> FitWidth
4715 state.text <- "fit model: " ^ FMTE.to_string fm;
4716 reqlayout conf.angle fm
4718 | @F9 ->
4719 togglebirdseye ()
4721 | @9 when ctrl ->
4722 togglebirdseye ()
4724 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4725 when not ctrl -> (* 0..9 *)
4726 let ondone s =
4727 let n =
4728 try int_of_string s with exc ->
4729 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
4732 if n >= 0
4733 then (
4734 addnav ();
4735 cbput state.hists.pag (string_of_int n);
4736 gotopage1 (n + conf.pagebias - 1) 0;
4739 let pageentry text key =
4740 match Char.unsafe_chr key with
4741 | 'g' -> TEdone text
4742 | _ -> intentry text key
4744 let text = "x" in text.[0] <- Char.chr key;
4745 enttext (":", text, Some (onhist state.hists.pag),
4746 pageentry, ondone, true)
4748 | @b ->
4749 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
4750 reshape state.winw state.winh;
4752 | @B ->
4753 state.bzoom <- not state.bzoom;
4754 state.rects <- [];
4755 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
4757 | @l ->
4758 conf.hlinks <- not conf.hlinks;
4759 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4760 G.postRedisplay "toggle highlightlinks";
4762 | @F ->
4763 state.glinks <- true;
4764 let mode = state.mode in
4765 state.mode <- Textentry (
4766 (":", E.s, None, linknentry, linkndone gotounder, false),
4767 (fun _ ->
4768 state.glinks <- false;
4769 state.mode <- mode)
4771 state.text <- E.s;
4772 G.postRedisplay "view:linkent(F)"
4774 | @y ->
4775 state.glinks <- true;
4776 let mode = state.mode in
4777 state.mode <- Textentry (
4779 ":", E.s, None, linknentry, linkndone (fun under ->
4780 selstring (undertext under);
4781 ), false
4783 fun _ ->
4784 state.glinks <- false;
4785 state.mode <- mode
4787 state.text <- E.s;
4788 G.postRedisplay "view:linkent"
4790 | @a ->
4791 begin match state.autoscroll with
4792 | Some step ->
4793 conf.autoscrollstep <- step;
4794 state.autoscroll <- None
4795 | None ->
4796 if conf.autoscrollstep = 0
4797 then state.autoscroll <- Some 1
4798 else state.autoscroll <- Some conf.autoscrollstep
4801 | @p when ctrl ->
4802 launchpath ()
4804 | @P ->
4805 setpresentationmode (not conf.presentation);
4806 showtext ' ' ("presentation mode " ^
4807 if conf.presentation then "on" else "off");
4809 | @f ->
4810 if List.mem Wsi.Fullscreen state.winstate
4811 then Wsi.reshape conf.cwinw conf.cwinh
4812 else Wsi.fullscreen ()
4814 | @p | @N ->
4815 search state.searchpattern false
4817 | @n | @F3 ->
4818 search state.searchpattern true
4820 | @t ->
4821 begin match state.layout with
4822 | [] -> ()
4823 | l :: _ ->
4824 gotoghyll (getpagey l.pageno)
4827 | @space ->
4828 nextpage ()
4830 | @delete | @kpdelete -> (* delete *)
4831 prevpage ()
4833 | @equals ->
4834 showtext ' ' (describe_location ());
4836 | @w ->
4837 begin match state.layout with
4838 | [] -> ()
4839 | l :: _ ->
4840 Wsi.reshape (l.pagew + vscrollw ()) l.pageh;
4841 G.postRedisplay "w"
4844 | @apos ->
4845 enterbookmarkmode ()
4847 | @h | @F1 ->
4848 enterhelpmode ()
4850 | @i ->
4851 enterinfomode ()
4853 | @e when Buffer.length state.errmsgs > 0 ->
4854 entermsgsmode ()
4856 | @m ->
4857 let ondone s =
4858 match state.layout with
4859 | l :: _ ->
4860 if nonemptystr s
4861 then
4862 state.bookmarks <-
4863 (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4864 | _ -> ()
4866 enttext ("bookmark: ", E.s, None, textentry, ondone, true)
4868 | @tilde ->
4869 quickbookmark ();
4870 showtext ' ' "Quick bookmark added";
4872 | @z ->
4873 begin match state.layout with
4874 | l :: _ ->
4875 let rect = getpdimrect l.pagedimno in
4876 let w, h =
4877 if conf.crophack
4878 then
4879 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4880 truncate (1.2 *. (rect.(3) -. rect.(0))))
4881 else
4882 (truncate (rect.(1) -. rect.(0)),
4883 truncate (rect.(3) -. rect.(0)))
4885 let w = truncate ((float w)*.conf.zoom)
4886 and h = truncate ((float h)*.conf.zoom) in
4887 if w != 0 && h != 0
4888 then (
4889 state.anchor <- getanchor ();
4890 Wsi.reshape (w + vscrollw ()) (h + conf.interpagespace)
4892 G.postRedisplay "z";
4894 | [] -> ()
4897 | @x -> state.roam ()
4899 | @Lt | @Gt ->
4900 reqlayout (conf.angle +
4901 (if key = @question then 30 else -30)) conf.fitmodel
4903 | @Lb | @Rb ->
4904 conf.colorscale <-
4905 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
4907 G.postRedisplay "brightness";
4909 | @c when state.mode = View ->
4910 if Wsi.withalt mask
4911 then (
4912 if conf.zoom > 1.0
4913 then
4914 let m = (wadjsb state.winw - state.w) / 2 in
4915 state.x <- m;
4916 gotoy_and_clear_text state.y
4918 else
4919 let (c, a, b), z =
4920 match state.prevcolumns with
4921 | None -> (1, 0, 0), 1.0
4922 | Some (columns, z) ->
4923 let cab =
4924 match columns with
4925 | Csplit (c, _) -> -c, 0, 0
4926 | Cmulti ((c, a, b), _) -> c, a, b
4927 | Csingle _ -> 1, 0, 0
4929 cab, z
4931 setcolumns View c a b;
4932 setzoom z
4934 | @down | @up when ctrl && Wsi.withshift mask ->
4935 let zoom, x = state.prevzoom in
4936 setzoom zoom;
4937 state.x <- x;
4939 | @k | @up | @kpup ->
4940 begin match state.autoscroll with
4941 | None ->
4942 begin match state.mode with
4943 | Birdseye beye -> upbirdseye 1 beye
4944 | _ ->
4945 if ctrl
4946 then gotoy_and_clear_text (clamp ~-(state.winh/2))
4947 else (
4948 if not (Wsi.withshift mask) && conf.presentation
4949 then prevpage ()
4950 else gotoghyll1 true (clamp (-conf.scrollstep))
4953 | Some n ->
4954 setautoscrollspeed n false
4957 | @j | @down | @kpdown ->
4958 begin match state.autoscroll with
4959 | None ->
4960 begin match state.mode with
4961 | Birdseye beye -> downbirdseye 1 beye
4962 | _ ->
4963 if ctrl
4964 then gotoy_and_clear_text (clamp (state.winh/2))
4965 else (
4966 if not (Wsi.withshift mask) && conf.presentation
4967 then nextpage ()
4968 else gotoghyll1 true (clamp (conf.scrollstep))
4971 | Some n ->
4972 setautoscrollspeed n true
4975 | @left | @right | @kpleft | @kpright when not (Wsi.withalt mask) ->
4976 if canpan ()
4977 then
4978 let dx =
4979 if ctrl
4980 then state.winw / 2
4981 else conf.hscrollstep
4983 let dx = if key = @left || key = @kpleft then dx else -dx in
4984 state.x <- panbound (state.x + dx);
4985 gotoy_and_clear_text state.y
4986 else (
4987 state.text <- E.s;
4988 G.postRedisplay "left/right"
4991 | @prior | @kpprior ->
4992 let y =
4993 if ctrl
4994 then
4995 match state.layout with
4996 | [] -> state.y
4997 | l :: _ -> state.y - l.pagey
4998 else
4999 clamp (pgscale (-state.winh))
5001 gotoghyll y
5003 | @next | @kpnext ->
5004 let y =
5005 if ctrl
5006 then
5007 match List.rev state.layout with
5008 | [] -> state.y
5009 | l :: _ -> getpagey l.pageno
5010 else
5011 clamp (pgscale state.winh)
5013 gotoghyll y
5015 | @g | @home | @kphome ->
5016 addnav ();
5017 gotoghyll 0
5018 | @G | @jend | @kpend ->
5019 addnav ();
5020 gotoghyll (clamp state.maxy)
5022 | @right | @kpright when Wsi.withalt mask ->
5023 gotoghyll (getnav 1)
5024 | @left | @kpleft when Wsi.withalt mask ->
5025 gotoghyll (getnav ~-1)
5027 | @r ->
5028 reload ()
5030 | @v when conf.debug ->
5031 state.rects <- [];
5032 List.iter (fun l ->
5033 match getopaque l.pageno with
5034 | None -> ()
5035 | Some opaque ->
5036 let x0, y0, x1, y1 = pagebbox opaque in
5037 let a,b = float x0, float y0 in
5038 let c,d = float x1, float y0 in
5039 let e,f = float x1, float y1 in
5040 let h,j = float x0, float y1 in
5041 let rect = (a,b,c,d,e,f,h,j) in
5042 debugrect rect;
5043 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5044 ) state.layout;
5045 G.postRedisplay "v";
5047 | @pipe ->
5048 let mode = state.mode in
5049 let cmd = ref E.s in
5050 let onleave = function
5051 | Cancel -> state.mode <- mode
5052 | Confirm ->
5053 List.iter (fun l ->
5054 match getopaque l.pageno with
5055 | Some opaque -> pipesel opaque !cmd
5056 | None -> ()) state.layout;
5057 state.mode <- mode
5059 let ondone s =
5060 cbput state.hists.sel s;
5061 cmd := s
5063 let te =
5064 "| ", !cmd, Some (onhist state.hists.sel), textentry, ondone, true
5066 G.postRedisplay "|";
5067 state.mode <- Textentry (te, onleave);
5069 | _ ->
5070 vlog "huh? %s" (Wsi.keyname key)
5073 let linknavkeyboard key mask linknav =
5074 let getpage pageno =
5075 let rec loop = function
5076 | [] -> None
5077 | l :: _ when l.pageno = pageno -> Some l
5078 | _ :: rest -> loop rest
5079 in loop state.layout
5081 let doexact (pageno, n) =
5082 match getopaque pageno, getpage pageno with
5083 | Some opaque, Some l ->
5084 if key = @enter || key = @kpenter
5085 then
5086 let under = getlink opaque n in
5087 G.postRedisplay "link gotounder";
5088 gotounder under;
5089 state.mode <- View;
5090 else
5091 let opt, dir =
5092 match key with
5093 | @home ->
5094 Some (findlink opaque LDfirst), -1
5096 | @jend ->
5097 Some (findlink opaque LDlast), 1
5099 | @left ->
5100 Some (findlink opaque (LDleft n)), -1
5102 | @right ->
5103 Some (findlink opaque (LDright n)), 1
5105 | @up ->
5106 Some (findlink opaque (LDup n)), -1
5108 | @down ->
5109 Some (findlink opaque (LDdown n)), 1
5111 | _ -> None, 0
5113 let pwl l dir =
5114 begin match findpwl l.pageno dir with
5115 | Pwlnotfound -> ()
5116 | Pwl pageno ->
5117 let notfound dir =
5118 state.mode <- LinkNav (Ltgendir dir);
5119 let y, h = getpageyh pageno in
5120 let y =
5121 if dir < 0
5122 then y + h - state.winh
5123 else y
5125 gotoy y
5127 begin match getopaque pageno, getpage pageno with
5128 | Some opaque, Some _ ->
5129 let link =
5130 let ld = if dir > 0 then LDfirst else LDlast in
5131 findlink opaque ld
5133 begin match link with
5134 | Lfound m ->
5135 showlinktype (getlink opaque m);
5136 state.mode <- LinkNav (Ltexact (pageno, m));
5137 G.postRedisplay "linknav jpage";
5138 | _ -> notfound dir
5139 end;
5140 | _ -> notfound dir
5141 end;
5142 end;
5144 begin match opt with
5145 | Some Lnotfound -> pwl l dir;
5146 | Some (Lfound m) ->
5147 if m = n
5148 then pwl l dir
5149 else (
5150 let _, y0, _, y1 = getlinkrect opaque m in
5151 if y0 < l.pagey
5152 then gotopage1 l.pageno y0
5153 else (
5154 let d = fstate.fontsize + 1 in
5155 if y1 - l.pagey > l.pagevh - d
5156 then gotopage1 l.pageno (y1 - state.winh - hscrollh () + d)
5157 else G.postRedisplay "linknav";
5159 showlinktype (getlink opaque m);
5160 state.mode <- LinkNav (Ltexact (l.pageno, m));
5163 | None -> viewkeyboard key mask
5164 end;
5165 | _ -> viewkeyboard key mask
5167 if key = @insert
5168 then (
5169 state.mode <- View;
5170 G.postRedisplay "leave linknav"
5172 else
5173 match linknav with
5174 | Ltgendir _ -> viewkeyboard key mask
5175 | Ltexact exact -> doexact exact
5178 let keyboard key mask =
5179 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5180 then wcmd "interrupt"
5181 else state.uioh <- state.uioh#key key mask
5184 let birdseyekeyboard key mask
5185 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5186 let incr =
5187 match conf.columns with
5188 | Csingle _ -> 1
5189 | Cmulti ((c, _, _), _) -> c
5190 | Csplit _ -> failwith "bird's eye split mode"
5192 let pgh layout = List.fold_left
5193 (fun m l -> max l.pageh m) state.winh layout in
5194 match key with
5195 | @l when Wsi.withctrl mask ->
5196 let y, h = getpageyh pageno in
5197 let top = (state.winh - h) / 2 in
5198 gotoy (max 0 (y - top))
5199 | @enter | @kpenter -> leavebirdseye beye false
5200 | @escape -> leavebirdseye beye true
5201 | @up -> upbirdseye incr beye
5202 | @down -> downbirdseye incr beye
5203 | @left -> upbirdseye 1 beye
5204 | @right -> downbirdseye 1 beye
5206 | @prior ->
5207 begin match state.layout with
5208 | l :: _ ->
5209 if l.pagey != 0
5210 then (
5211 state.mode <- Birdseye (
5212 oconf, leftx, l.pageno, hooverpageno, anchor
5214 gotopage1 l.pageno 0;
5216 else (
5217 let layout = layout (state.y-state.winh) (pgh state.layout) in
5218 match layout with
5219 | [] -> gotoy (clamp (-state.winh))
5220 | l :: _ ->
5221 state.mode <- Birdseye (
5222 oconf, leftx, l.pageno, hooverpageno, anchor
5224 gotopage1 l.pageno 0
5227 | [] -> gotoy (clamp (-state.winh))
5228 end;
5230 | @next ->
5231 begin match List.rev state.layout with
5232 | l :: _ ->
5233 let layout = layout (state.y + (pgh state.layout)) state.winh in
5234 begin match layout with
5235 | [] ->
5236 let incr = l.pageh - l.pagevh in
5237 if incr = 0
5238 then (
5239 state.mode <-
5240 Birdseye (
5241 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5243 G.postRedisplay "birdseye pagedown";
5245 else gotoy (clamp (incr + conf.interpagespace*2));
5247 | l :: _ ->
5248 state.mode <-
5249 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5250 gotopage1 l.pageno 0;
5253 | [] -> gotoy (clamp state.winh)
5254 end;
5256 | @home ->
5257 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5258 gotopage1 0 0
5260 | @jend ->
5261 let pageno = state.pagecount - 1 in
5262 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5263 if not (pagevisible state.layout pageno)
5264 then
5265 let h =
5266 match List.rev state.pdims with
5267 | [] -> state.winh
5268 | (_, _, h, _) :: _ -> h
5270 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5271 else G.postRedisplay "birdseye end";
5273 | _ -> viewkeyboard key mask
5276 let drawpage l =
5277 let color =
5278 match state.mode with
5279 | Textentry _ -> scalecolor 0.4
5280 | LinkNav _
5281 | View -> scalecolor 1.0
5282 | Birdseye (_, _, pageno, hooverpageno, _) ->
5283 if l.pageno = hooverpageno
5284 then scalecolor 0.9
5285 else (
5286 if l.pageno = pageno
5287 then (
5288 let c = scalecolor 1.0 in
5289 GlDraw.color c;
5290 GlDraw.line_width 3.0;
5291 let dispx = xadjsb l.pagedispx in
5292 linerect
5293 (float (dispx-1)) (float (l.pagedispy-1))
5294 (float (dispx+l.pagevw+1))
5295 (float (l.pagedispy+l.pagevh+1))
5297 GlDraw.line_width 1.0;
5300 else scalecolor 0.8
5303 drawtiles l color;
5306 let postdrawpage l linkindexbase =
5307 match getopaque l.pageno with
5308 | Some opaque ->
5309 if tileready l l.pagex l.pagey
5310 then
5311 let x = l.pagedispx - l.pagex + xadjsb 0
5312 and y = l.pagedispy - l.pagey in
5313 let hlmask =
5314 match conf.columns with
5315 | Csingle _ | Cmulti _ ->
5316 (if conf.hlinks then 1 else 0)
5317 + (if state.glinks
5318 && not (isbirdseye state.mode) then 2 else 0)
5319 | _ -> 0
5321 let s =
5322 match state.mode with
5323 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5324 | _ -> E.s
5326 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5327 else 0
5328 | _ -> 0
5331 let scrollindicator () =
5332 let sbw, ph, sh = state.uioh#scrollph in
5333 let sbh, pw, sw = state.uioh#scrollpw in
5335 let x0,x1 =
5336 if conf.leftscroll
5337 then (0, sbw)
5338 else (state.winw - sbw), state.winw
5341 GlDraw.color (0.64, 0.64, 0.64);
5342 filledrect (float x0) 0. (float x1) (float state.winh);
5343 filledrect
5344 0. (float (state.winh - sbh))
5345 (float (wadjsb state.winw - 1)) (float state.winh)
5347 GlDraw.color (0.0, 0.0, 0.0);
5349 filledrect (float x0) ph (float x1) (ph +. sh);
5350 filledrect pw (float (state.winh - sbh)) (pw +. sw) (float state.winh);
5353 let showsel () =
5354 match state.mstate with
5355 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5358 | Msel ((x0, y0), (x1, y1)) ->
5359 let identify opaque l px py = Some (opaque, l.pageno, px, py) in
5360 let o0,n0,px0,py0 = onppundermouse identify x0 y0 (~< E.s, -1, 0, 0) in
5361 let _o1,n1,px1,py1 = onppundermouse identify x1 y1 (~< E.s, -1, 0, 0) in
5362 if n0 != -1 && n0 = n1 then seltext o0 (px0, py0, px1, py1);
5365 let showrects = function [] -> () | rects ->
5366 Gl.enable `blend;
5367 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5368 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5369 List.iter
5370 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5371 List.iter (fun l ->
5372 if l.pageno = pageno
5373 then (
5374 let dx = float (l.pagedispx - l.pagex) in
5375 let dy = float (l.pagedispy - l.pagey) in
5376 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5377 Raw.sets_float state.vraw ~pos:0
5378 [| x0+.dx; y0+.dy;
5379 x1+.dx; y1+.dy;
5380 x3+.dx; y3+.dy;
5381 x2+.dx; y2+.dy |];
5382 GlArray.vertex `two state.vraw;
5383 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
5385 ) state.layout
5386 ) rects
5388 Gl.disable `blend;
5391 let display () =
5392 GlClear.color (scalecolor2 conf.bgcolor);
5393 GlClear.clear [`color];
5394 List.iter drawpage state.layout;
5395 let rects =
5396 match state.mode with
5397 | LinkNav (Ltexact (pageno, linkno)) ->
5398 begin match getopaque pageno with
5399 | Some opaque ->
5400 let dx = xadjsb 0 in
5401 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5402 let x0 = x0 + dx and x1 = x1 + dx in
5403 (pageno, 5, (
5404 float x0, float y0,
5405 float x1, float y0,
5406 float x1, float y1,
5407 float x0, float y1)
5408 ) :: state.rects
5409 | None -> state.rects
5411 | _ -> state.rects
5413 showrects rects;
5414 let rec postloop linkindexbase = function
5415 | l :: rest ->
5416 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5417 postloop linkindexbase rest
5418 | [] -> ()
5420 showsel ();
5421 postloop 0 state.layout;
5422 state.uioh#display;
5423 begin match state.mstate with
5424 | Mzoomrect ((x0, y0), (x1, y1)) ->
5425 Gl.enable `blend;
5426 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5427 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5428 filledrect (float x0) (float y0) (float x1) (float y1);
5429 Gl.disable `blend;
5430 | _ -> ()
5431 end;
5432 enttext ();
5433 scrollindicator ();
5434 Wsi.swapb ();
5437 let zoomrect x y x1 y1 =
5438 let x0 = min x x1
5439 and x1 = max x x1
5440 and y0 = min y y1 in
5441 gotoy (state.y + y0);
5442 state.anchor <- getanchor ();
5443 let zoom = (float state.w) /. float (x1 - x0) in
5444 let margin =
5445 match conf.fitmodel, conf.columns with
5446 | FitPage, Csplit _ ->
5447 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
5449 | _, _ ->
5450 let adjw = wadjsb state.winw in
5451 if state.w < adjw
5452 then (adjw - state.w) / 2
5453 else 0
5455 state.x <- (state.x + margin) - x0;
5456 setzoom zoom;
5457 resetmstate ();
5460 let zoomblock x y =
5461 let g opaque l px py =
5462 match rectofblock opaque px py with
5463 | Some a ->
5464 let x0 = a.(0) -. 20. in
5465 let x1 = a.(1) +. 20. in
5466 let y0 = a.(2) -. 20. in
5467 let zoom = (float state.w) /. (x1 -. x0) in
5468 let pagey = getpagey l.pageno in
5469 gotoy_and_clear_text (pagey + truncate y0);
5470 state.anchor <- getanchor ();
5471 let margin = (state.w - l.pagew)/2 in
5472 state.x <- -truncate x0 - margin;
5473 setzoom zoom;
5474 None
5475 | None -> None
5477 match conf.columns with
5478 | Csplit _ ->
5479 showtext '!' "block zooming does not work properly in split columns mode"
5480 | _ -> onppundermouse g x y ()
5483 let scrollx x =
5484 let winw = wadjsb state.winw - 1 in
5485 let s = float x /. float winw in
5486 let destx = truncate (float (state.w + winw) *. s) in
5487 state.x <- winw - destx;
5488 gotoy_and_clear_text state.y;
5489 state.mstate <- Mscrollx;
5492 let scrolly y =
5493 let s = float y /. float state.winh in
5494 let desty = truncate (float (state.maxy - state.winh) *. s) in
5495 gotoy_and_clear_text desty;
5496 state.mstate <- Mscrolly;
5499 let viewmulticlick clicks x y mask =
5500 let g opaque l px py =
5501 let mark =
5502 match clicks with
5503 | 2 -> Mark_word
5504 | 3 -> Mark_line
5505 | 4 -> Mark_block
5506 | _ -> Mark_page
5508 if markunder opaque px py mark
5509 then (
5510 Some (fun () ->
5511 let dopipe cmd =
5512 match getopaque l.pageno with
5513 | None -> ()
5514 | Some opaque -> pipesel opaque cmd
5516 state.roam <- (fun () -> dopipe conf.paxcmd);
5517 if not (Wsi.withctrl mask) then dopipe conf.selcmd;
5520 else None
5522 G.postRedisplay "viewmulticlick";
5523 onppundermouse g x y (fun () -> showtext '!' "Nothing to select") ();
5526 let canselect () =
5527 match conf.columns with
5528 | Csplit _ -> false
5529 | Csingle _ | Cmulti _ -> conf.angle mod 360 = 0
5532 let viewmouse button down x y mask =
5533 match button with
5534 | n when (n == 4 || n == 5) && not down ->
5535 if Wsi.withctrl mask
5536 then (
5537 match state.mstate with
5538 | Mzoom (oldn, i) ->
5539 if oldn = n
5540 then (
5541 if i = 2
5542 then
5543 let incr =
5544 match n with
5545 | 5 ->
5546 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5547 | _ ->
5548 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5550 let zoom = conf.zoom -. incr in
5551 setzoom zoom;
5552 state.mstate <- Mzoom (n, 0);
5553 else
5554 state.mstate <- Mzoom (n, i+1);
5556 else state.mstate <- Mzoom (n, 0)
5558 | _ -> state.mstate <- Mzoom (n, 0)
5560 else (
5561 match state.autoscroll with
5562 | Some step -> setautoscrollspeed step (n=4)
5563 | None ->
5564 if conf.wheelbypage || conf.presentation
5565 then (
5566 if n = 4
5567 then prevpage ()
5568 else nextpage ()
5570 else
5571 let incr =
5572 if n = 4
5573 then -conf.scrollstep
5574 else conf.scrollstep
5576 let incr = incr * 2 in
5577 let y = clamp incr in
5578 gotoy_and_clear_text y
5581 | n when (n = 6 || n = 7) && not down && canpan () ->
5582 state.x <-
5583 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
5584 gotoy_and_clear_text state.y
5586 | 1 when Wsi.withshift mask ->
5587 state.mstate <- Mnone;
5588 if not down
5589 then (
5590 match unproject x y with
5591 | Some (pageno, ux, uy) ->
5592 let cmd = Printf.sprintf
5593 "%s %s %d %d %d"
5594 conf.stcmd state.path pageno ux uy
5596 popen cmd []
5597 | None -> ()
5600 | 1 when Wsi.withctrl mask ->
5601 if down
5602 then (
5603 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5604 state.mstate <- Mpan (x, y)
5606 else
5607 state.mstate <- Mnone
5609 | 3 ->
5610 if down
5611 then (
5612 Wsi.setcursor Wsi.CURSOR_CYCLE;
5613 let p = (x, y) in
5614 state.mstate <- Mzoomrect (p, p)
5616 else (
5617 match state.mstate with
5618 | Mzoomrect ((x0, y0), _) ->
5619 if abs (x-x0) > 10 && abs (y - y0) > 10
5620 then zoomrect x0 y0 x y
5621 else (
5622 resetmstate ();
5623 G.postRedisplay "kill accidental zoom rect";
5625 | _ ->
5626 resetmstate ()
5629 | 1 when x > state.winw - vscrollw () ->
5630 if down
5631 then
5632 let _, position, sh = state.uioh#scrollph in
5633 if y > truncate position && y < truncate (position +. sh)
5634 then state.mstate <- Mscrolly
5635 else scrolly y
5636 else
5637 state.mstate <- Mnone
5639 | 1 when y > state.winh - hscrollh () ->
5640 if down
5641 then
5642 let _, position, sw = state.uioh#scrollpw in
5643 if x > truncate position && x < truncate (position +. sw)
5644 then state.mstate <- Mscrollx
5645 else scrollx x
5646 else
5647 state.mstate <- Mnone
5649 | 1 when state.bzoom -> if not down then zoomblock x y
5651 | 1 ->
5652 let dest = if down then getunder x y else Unone in
5653 begin match dest with
5654 | Ulinkgoto _
5655 | Ulinkuri _
5656 | Uremote _ | Uremotedest _
5657 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5658 gotounder dest
5660 | Unone when down ->
5661 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5662 state.mstate <- Mpan (x, y);
5664 | Unone | Utext _ ->
5665 if down
5666 then (
5667 if canselect ()
5668 then (
5669 state.mstate <- Msel ((x, y), (x, y));
5670 G.postRedisplay "mouse select";
5673 else (
5674 match state.mstate with
5675 | Mnone -> ()
5677 | Mzoom _ | Mscrollx | Mscrolly ->
5678 state.mstate <- Mnone
5680 | Mzoomrect ((x0, y0), _) ->
5681 zoomrect x0 y0 x y
5683 | Mpan _ ->
5684 Wsi.setcursor Wsi.CURSOR_INHERIT;
5685 state.mstate <- Mnone
5687 | Msel ((x0, y0), (x1, y1)) ->
5688 let rec loop = function
5689 | [] -> ()
5690 | l :: rest ->
5691 let inside =
5692 let a0 = l.pagedispy in
5693 let a1 = a0 + l.pagevh in
5694 let b0 = l.pagedispx in
5695 let b1 = b0 + l.pagevw in
5696 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5697 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5699 if inside
5700 then
5701 match getopaque l.pageno with
5702 | Some opaque ->
5703 let dosel cmd () =
5704 match Ne.res Unix.pipe () with
5705 | Ne.Exn exn ->
5706 showtext '!'
5707 (Printf.sprintf
5708 "can not create sel pipe: %s"
5709 (exntos exn));
5710 | Ne.Res (r, w) ->
5711 let clo what fd =
5712 Ne.clo fd (fun msg ->
5713 dolog "%s close failed: %s" what msg)
5715 let popened =
5716 try popen cmd [r, 0; w, -1]; true
5717 with exn ->
5718 dolog "can not execute %S: %s"
5719 cmd (exntos exn);
5720 false
5722 if popened
5723 then (
5724 copysel w opaque;
5725 G.postRedisplay "copysel";
5727 else clo "Msel pipe/w" w;
5728 clo "Msel pipe/r" r;
5730 dosel conf.selcmd ();
5731 state.roam <- dosel conf.paxcmd;
5732 | None -> ()
5733 else loop rest
5735 loop state.layout;
5736 resetmstate ();
5740 | _ -> ()
5743 let birdseyemouse button down x y mask
5744 (conf, leftx, _, hooverpageno, anchor) =
5745 match button with
5746 | 1 when down ->
5747 let rec loop = function
5748 | [] -> ()
5749 | l :: rest ->
5750 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5751 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5752 then (
5753 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5755 else loop rest
5757 loop state.layout
5758 | 3 -> ()
5759 | _ -> viewmouse button down x y mask
5762 let uioh = object
5763 method display = ()
5765 method key key mask =
5766 begin match state.mode with
5767 | Textentry textentry -> textentrykeyboard key mask textentry
5768 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5769 | View -> viewkeyboard key mask
5770 | LinkNav linknav -> linknavkeyboard key mask linknav
5771 end;
5772 state.uioh
5774 method button button bstate x y mask =
5775 begin match state.mode with
5776 | LinkNav _
5777 | View -> viewmouse button bstate x y mask
5778 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5779 | Textentry _ -> ()
5780 end;
5781 state.uioh
5783 method multiclick clicks x y mask =
5784 begin match state.mode with
5785 | LinkNav _
5786 | View -> viewmulticlick clicks x y mask
5787 | Birdseye _
5788 | Textentry _ -> ()
5789 end;
5790 state.uioh
5792 method motion x y =
5793 begin match state.mode with
5794 | Textentry _ -> ()
5795 | View | Birdseye _ | LinkNav _ ->
5796 match state.mstate with
5797 | Mzoom _ | Mnone -> ()
5799 | Mpan (x0, y0) ->
5800 let dx = x - x0
5801 and dy = y0 - y in
5802 state.mstate <- Mpan (x, y);
5803 if canpan ()
5804 then state.x <- panbound (state.x + dx);
5805 let y = clamp dy in
5806 gotoy_and_clear_text y
5808 | Msel (a, _) ->
5809 state.mstate <- Msel (a, (x, y));
5810 G.postRedisplay "motion select";
5812 | Mscrolly ->
5813 let y = min state.winh (max 0 y) in
5814 scrolly y
5816 | Mscrollx ->
5817 let x = min state.winw (max 0 x) in
5818 scrollx x
5820 | Mzoomrect (p0, _) ->
5821 state.mstate <- Mzoomrect (p0, (x, y));
5822 G.postRedisplay "motion zoomrect";
5823 end;
5824 state.uioh
5826 method pmotion x y =
5827 begin match state.mode with
5828 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5829 let rec loop = function
5830 | [] ->
5831 if hooverpageno != -1
5832 then (
5833 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5834 G.postRedisplay "pmotion birdseye no hoover";
5836 | l :: rest ->
5837 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5838 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5839 then (
5840 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5841 G.postRedisplay "pmotion birdseye hoover";
5843 else loop rest
5845 loop state.layout
5847 | Textentry _ -> ()
5849 | LinkNav _
5850 | View ->
5851 match state.mstate with
5852 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5854 | Mnone ->
5855 updateunder x y;
5856 if canselect ()
5857 then
5858 match conf.pax with
5859 | None -> ()
5860 | Some r ->
5861 let past, _, _ = !r in
5862 let now = now () in
5863 let delta = now -. past in
5864 if delta > 0.01
5865 then paxunder x y
5866 else r := (now, x, y)
5867 end;
5868 state.uioh
5870 method infochanged _ = ()
5872 method scrollph =
5873 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
5874 let p, h =
5875 if maxy = 0
5876 then 0.0, float state.winh
5877 else scrollph state.y maxy
5879 vscrollw (), p, h
5881 method scrollpw =
5882 let winw = wadjsb state.winw in
5883 let fwinw = float winw in
5884 let sw =
5885 let sw = fwinw /. float state.w in
5886 let sw = fwinw *. sw in
5887 max sw (float conf.scrollh)
5889 let position =
5890 let maxx = state.w + winw in
5891 let x = winw - state.x in
5892 let percent = float x /. float maxx in
5893 (fwinw -. sw) *. percent
5895 hscrollh (), position, sw
5897 method modehash =
5898 let modename =
5899 match state.mode with
5900 | LinkNav _ -> "links"
5901 | Textentry _ -> "textentry"
5902 | Birdseye _ -> "birdseye"
5903 | View -> "view"
5905 findkeyhash conf modename
5907 method eformsgs = true
5908 end;;
5910 let adderrmsg src msg =
5911 Buffer.add_string state.errmsgs msg;
5912 state.newerrmsgs <- true;
5913 G.postRedisplay src
5916 let adderrfmt src fmt =
5917 Format.kprintf (fun s -> adderrmsg src s) fmt;
5920 let ract cmds =
5921 let cl = splitatspace cmds in
5922 let scan s fmt f =
5923 try Scanf.sscanf s fmt f
5924 with exn ->
5925 adderrfmt "remote exec"
5926 "error processing '%S': %s\n" cmds (exntos exn)
5928 match cl with
5929 | "reload" :: [] -> reload ()
5930 | "goto" :: args :: [] ->
5931 scan args "%u %f %f"
5932 (fun pageno x y ->
5933 let cmd, _ = state.geomcmds in
5934 if emptystr cmd
5935 then gotopagexy pageno x y
5936 else
5937 let f prevf () =
5938 gotopagexy pageno x y;
5939 prevf ()
5941 state.reprf <- f state.reprf
5943 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
5944 | "gotor" :: args :: [] ->
5945 scan args "%S %u"
5946 (fun filename pageno -> gotounder (Uremote (filename, pageno)))
5947 | "gotord" :: args :: [] ->
5948 scan args "%S %S"
5949 (fun filename dest -> gotounder (Uremotedest (filename, dest)))
5950 | "rect" :: args :: [] ->
5951 scan args "%u %u %f %f %f %f"
5952 (fun pageno color x0 y0 x1 y1 ->
5953 onpagerect pageno (fun w h ->
5954 let _,w1,h1,_ = getpagedim pageno in
5955 let sw = float w1 /. float w
5956 and sh = float h1 /. float h in
5957 let x0s = x0 *. sw
5958 and x1s = x1 *. sw
5959 and y0s = y0 *. sh
5960 and y1s = y1 *. sh in
5961 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
5962 debugrect rect;
5963 state.rects <- (pageno, color, rect) :: state.rects;
5964 G.postRedisplay "rect";
5967 | "activatewin" :: [] -> Wsi.activatewin ()
5968 | "quit" :: [] -> raise Quit
5969 | _ ->
5970 adderrfmt "remote command"
5971 "error processing remote command: %S\n" cmds;
5974 let remote =
5975 let scratch = String.create 80 in
5976 let buf = Buffer.create 80 in
5977 fun fd ->
5978 let rec tempfr () =
5979 try Some (Unix.read fd scratch 0 80)
5980 with
5981 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
5982 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
5983 | exn -> raise exn
5985 match tempfr () with
5986 | None -> Some fd
5987 | Some n ->
5988 if n = 0
5989 then (
5990 Unix.close fd;
5991 if Buffer.length buf > 0
5992 then (
5993 let s = Buffer.contents buf in
5994 Buffer.clear buf;
5995 ract s;
5997 None
5999 else
6000 let rec eat ppos =
6001 let nlpos =
6003 let pos = String.index_from scratch ppos '\n' in
6004 if pos >= n then -1 else pos
6005 with Not_found -> -1
6007 if nlpos >= 0
6008 then (
6009 Buffer.add_substring buf scratch ppos (nlpos-ppos);
6010 let s = Buffer.contents buf in
6011 Buffer.clear buf;
6012 ract s;
6013 eat (nlpos+1);
6015 else (
6016 Buffer.add_substring buf scratch ppos (n-ppos);
6017 Some fd
6019 in eat 0
6022 let remoteopen path =
6023 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
6024 with exn ->
6025 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
6026 None
6029 let () =
6030 let gcconfig = ref E.s in
6031 let trimcachepath = ref E.s in
6032 let rcmdpath = ref E.s in
6033 let pageno = ref None in
6034 selfexec := Sys.executable_name;
6035 Arg.parse
6036 (Arg.align
6037 [("-p", Arg.String (fun s -> state.password <- s),
6038 "<password> Set password");
6040 ("-f", Arg.String
6041 (fun s ->
6042 Config.fontpath := s;
6043 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
6045 "<path> Set path to the user interface font");
6047 ("-c", Arg.String
6048 (fun s ->
6049 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
6050 Config.confpath := s),
6051 "<path> Set path to the configuration file");
6053 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
6054 "<page-number> Jump to page");
6056 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6057 "<path> Set path to the trim cache file");
6059 ("-dest", Arg.String (fun s -> state.nameddest <- s),
6060 "<named-destination> Set named destination");
6062 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
6063 ("-cxack", Arg.Set cxack, " Cut corners");
6065 ("-remote", Arg.String (fun s -> rcmdpath := s),
6066 "<path> Set path to the remote commands source");
6068 ("-origin", Arg.String (fun s -> state.origin <- s),
6069 "<original-path> Set original path");
6071 ("-gc", Arg.Set_string gcconfig, " collect garbage");
6073 ("-v", Arg.Unit (fun () ->
6074 Printf.printf
6075 "%s\nconfiguration path: %s\n"
6076 (version ())
6077 Config.defconfpath
6079 exit 0), " Print version and exit");
6082 (fun s -> state.path <- s)
6083 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6085 if !wtmode
6086 then selfexec := !selfexec ^ " -wtmode";
6088 let histmode = emptystr state.path in
6090 if not (Config.load ())
6091 then prerr_endline "failed to load configuration";
6092 begin match !pageno with
6093 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
6094 | None -> ()
6095 end;
6097 if not (emptystr !gcconfig)
6098 then (
6099 let c, s =
6100 match Ne.res
6101 (Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM) 0 with
6102 | Ne.Exn exn ->
6103 error "gc socketpair failed: %s" (exntos exn)
6104 | Ne.Res rw -> rw
6106 match Ne.res (popen !gcconfig) [(c, 0); (c, 1)] with
6107 | Ne.Res () ->
6108 Config.gc s s;
6109 exit 0
6110 | Ne.Exn exn ->
6111 error "failed to popen gc script: %s" (exntos exn);
6114 let wsfd, winw, winh = Wsi.init (object (self)
6115 val mutable m_clicks = 0
6116 val mutable m_click_x = 0
6117 val mutable m_click_y = 0
6118 val mutable m_lastclicktime = infinity
6120 method private cleanup =
6121 state.roam <- noroam;
6122 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap
6123 method expose = G.postRedisplay"expose"
6124 method visible v =
6125 let name =
6126 match v with
6127 | Wsi.Unobscured -> "unobscured"
6128 | Wsi.PartiallyObscured -> "partiallyobscured"
6129 | Wsi.FullyObscured -> "fullyobjscured"
6131 vlog "visibility change %s" name
6132 method display = display ()
6133 method map mapped = vlog "mappped %b" mapped
6134 method reshape w h =
6135 self#cleanup;
6136 reshape w h
6137 method mouse b d x y m =
6138 if d && canselect ()
6139 then (
6140 (* http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx *)
6141 m_click_x <- x;
6142 m_click_y <- y;
6143 if b = 1
6144 then (
6145 let t = now () in
6146 if abs x - m_click_x > 10
6147 || abs y - m_click_y > 10
6148 || abs_float (t -. m_lastclicktime) > 0.3
6149 then m_clicks <- 0;
6150 m_clicks <- m_clicks + 1;
6151 m_lastclicktime <- t;
6152 if m_clicks = 1
6153 then (
6154 self#cleanup;
6155 G.postRedisplay "cleanup";
6156 state.uioh <- state.uioh#button b d x y m;
6158 else state.uioh <- state.uioh#multiclick m_clicks x y m
6160 else (
6161 self#cleanup;
6162 m_clicks <- 0;
6163 m_lastclicktime <- infinity;
6164 state.uioh <- state.uioh#button b d x y m
6167 else (
6168 state.uioh <- state.uioh#button b d x y m
6170 method motion x y =
6171 state.mpos <- (x, y);
6172 state.uioh <- state.uioh#motion x y
6173 method pmotion x y =
6174 state.mpos <- (x, y);
6175 state.uioh <- state.uioh#pmotion x y
6176 method key k m =
6177 let mascm = m land (
6178 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6179 ) in
6180 let keyboard k m =
6181 let x = state.x and y = state.y in
6182 keyboard k m;
6183 if x != state.x || y != state.y then self#cleanup
6185 match state.keystate with
6186 | KSnone ->
6187 let km = k, mascm in
6188 begin
6189 match
6190 let modehash = state.uioh#modehash in
6191 try Hashtbl.find modehash km
6192 with Not_found ->
6193 try Hashtbl.find (findkeyhash conf "global") km
6194 with Not_found -> KMinsrt (k, m)
6195 with
6196 | KMinsrt (k, m) -> keyboard k m
6197 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6198 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6200 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6201 List.iter (fun (k, m) -> keyboard k m) insrt;
6202 state.keystate <- KSnone
6203 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6204 state.keystate <- KSinto (keys, insrt)
6205 | _ ->
6206 state.keystate <- KSnone
6208 method enter x y =
6209 state.mpos <- (x, y);
6210 state.uioh <- state.uioh#pmotion x y
6211 method leave = state.mpos <- (-1, -1)
6212 method winstate wsl = state.winstate <- wsl
6213 method quit = raise Quit
6214 end) conf.cwinw conf.cwinh (platform = Posx) in
6216 state.wsfd <- wsfd;
6218 if not (
6219 List.exists GlMisc.check_extension
6220 [ "GL_ARB_texture_rectangle"
6221 ; "GL_EXT_texture_recangle"
6222 ; "GL_NV_texture_rectangle" ]
6224 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6226 if (
6227 let r = GlMisc.get_string `renderer in
6228 let p = "Mesa DRI Intel(" in
6229 let l = String.length p in
6230 String.length r > l && String.sub r 0 l = p
6232 then (
6233 defconf.sliceheight <- 1024;
6234 defconf.texcount <- 32;
6235 defconf.usepbo <- true;
6238 let cs, ss =
6239 match Ne.res (Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM) 0 with
6240 | Ne.Exn exn ->
6241 Printf.eprintf "socketpair failed: %s" (exntos exn);
6242 exit 1
6243 | Ne.Res (r, w) ->
6244 cloexec r;
6245 cloexec w;
6246 r, w
6249 setcheckers conf.checkers;
6250 redirectstderr ();
6251 if conf.redirectstderr
6252 then
6253 at_exit (fun () ->
6254 let s = Buffer.contents state.errmsgs ^
6255 (match state.errfd with
6256 | Some fd ->
6257 let s = String.create (80*24) in
6258 let n =
6260 let r, _, _ = Unix.select [fd] [] [] 0.0 in
6261 if List.mem fd r
6262 then Unix.read fd s 0 (String.length s)
6263 else 0
6264 with _ -> 0
6266 if n = 0
6267 then E.s
6268 else String.sub s 0 n
6269 | None -> E.s
6272 try ignore (Unix.write state.stderr s 0 (String.length s))
6273 with exn -> print_endline (exntos exn)
6277 init cs (
6278 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
6279 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6280 !Config.fontpath, !trimcachepath,
6281 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
6283 List.iter GlArray.enable [`texture_coord; `vertex];
6284 state.ss <- ss;
6285 reshape winw winh;
6286 if histmode
6287 then (
6288 state.uioh <- uioh;
6289 Wsi.settitle "llpp (history)";
6290 enterhistmode ();
6292 else (
6293 state.text <- "Opening " ^ (mbtoutf8 state.path);
6294 opendoc state.path state.password;
6295 state.uioh <- uioh;
6297 display ();
6298 Wsi.mapwin ();
6299 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
6300 let optrfd =
6301 ref (
6302 if nonemptystr !rcmdpath
6303 then remoteopen !rcmdpath
6304 else None
6308 let rec loop deadline =
6309 let r =
6310 match state.errfd with
6311 | None -> [state.ss; state.wsfd]
6312 | Some fd -> [state.ss; state.wsfd; fd]
6314 let r =
6315 match !optrfd with
6316 | None -> r
6317 | Some fd -> fd :: r
6319 if state.redisplay
6320 then (
6321 state.redisplay <- false;
6322 display ();
6324 let timeout =
6325 let now = now () in
6326 if deadline > now
6327 then (
6328 if deadline = infinity
6329 then ~-.1.0
6330 else max 0.0 (deadline -. now)
6332 else 0.0
6334 let r, _, _ =
6335 try Unix.select r [] [] timeout
6336 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6338 begin match r with
6339 | [] ->
6340 state.ghyll None;
6341 let newdeadline =
6342 if state.ghyll == noghyll
6343 then
6344 match state.autoscroll with
6345 | Some step when step != 0 ->
6346 let y = state.y + step in
6347 let y =
6348 if y < 0
6349 then state.maxy
6350 else if y >= state.maxy then 0 else y
6352 gotoy y;
6353 if state.mode = View
6354 then state.text <- E.s;
6355 deadline +. 0.01
6356 | _ -> infinity
6357 else deadline +. 0.01
6359 loop newdeadline
6361 | l ->
6362 let rec checkfds = function
6363 | [] -> ()
6364 | fd :: rest when fd = state.ss ->
6365 let cmd = readcmd state.ss in
6366 act cmd;
6367 checkfds rest
6369 | fd :: rest when fd = state.wsfd ->
6370 Wsi.readresp fd;
6371 checkfds rest
6373 | fd :: rest when Some fd = !optrfd ->
6374 begin match remote fd with
6375 | None -> optrfd := remoteopen !rcmdpath;
6376 | opt -> optrfd := opt
6377 end;
6378 checkfds rest
6380 | fd :: rest ->
6381 let s = String.create 80 in
6382 let n = tempfailureretry (Unix.read fd s 0) 80 in
6383 if conf.redirectstderr
6384 then (
6385 Buffer.add_substring state.errmsgs s 0 n;
6386 state.newerrmsgs <- true;
6387 state.redisplay <- true;
6389 else (
6390 prerr_string (String.sub s 0 n);
6391 flush stderr;
6393 checkfds rest
6395 checkfds l;
6396 if !reeenterhist then (
6397 enterhistmode ();
6398 reeenterhist := false;
6400 let newdeadline =
6401 let deadline1 =
6402 if deadline = infinity
6403 then now () +. 0.01
6404 else deadline
6406 match state.autoscroll with
6407 | Some step when step != 0 -> deadline1
6408 | _ -> if state.ghyll == noghyll then infinity else deadline1
6410 loop newdeadline
6411 end;
6414 loop infinity;
6415 with Quit ->
6416 Config.save leavebirdseye;