Adjust firstmatch again
[llpp.git] / main.ml
blobbdf2db1a74e15a91772e602e6c6c4a6ec1f096ff
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 project : opaque -> int -> int -> float -> float -> (float * float)
35 = "ml_project";;
36 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
37 external rectofblock : opaque -> int -> int -> float array option
38 = "ml_rectofblock";;
39 external begintiles : unit -> unit = "ml_begintiles";;
40 external endtiles : unit -> unit = "ml_endtiles";;
41 external addannot : opaque -> int -> int -> string -> unit = "ml_addannot";;
42 external modannot : opaque -> slinkindex -> string -> unit = "ml_modannot";;
43 external delannot : opaque -> slinkindex -> unit = "ml_delannot";;
44 external hasunsavedchanges : unit -> bool = "ml_hasunsavedchanges";;
45 external savedoc : string -> unit = "ml_savedoc";;
46 external getannotcontents : opaque -> slinkindex -> string
47 = "ml_getannotcontents";;
48 external drawprect : opaque -> int -> int -> float array -> unit =
49 "ml_drawprect";;
51 let selfexec = ref E.s;;
53 let drawstring size x y s =
54 Gl.enable `blend;
55 Gl.enable `texture_2d;
56 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
57 ignore (drawstr size x y s);
58 Gl.disable `blend;
59 Gl.disable `texture_2d;
62 let drawstring1 size x y s =
63 drawstr size x y s;
66 let drawstring2 size x y fmt =
67 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
70 let _debugl l =
71 dolog "l %d dim=%d {" l.pageno l.pagedimno;
72 dolog " WxH %dx%d" l.pagew l.pageh;
73 dolog " vWxH %dx%d" l.pagevw l.pagevh;
74 dolog " pagex,y %d,%d" l.pagex l.pagey;
75 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
76 dolog " column %d" l.pagecol;
77 dolog "}";
80 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
81 dolog "rect {";
82 dolog " x0,y0=(% f, % f)" x0 y0;
83 dolog " x1,y1=(% f, % f)" x1 y1;
84 dolog " x2,y2=(% f, % f)" x2 y2;
85 dolog " x3,y3=(% f, % f)" x3 y3;
86 dolog "}";
89 let isbirdseye = function
90 | Birdseye _ -> true
91 | Textentry _
92 | View
93 | LinkNav _ -> false
96 let istextentry = function
97 | Textentry _ -> true
98 | Birdseye _
99 | View
100 | LinkNav _ -> false
103 let wtmode = ref false;;
104 let cxack = ref false;;
106 let pgscale h = truncate (float h *. conf.pgscale);;
108 let hscrollh () =
109 if not state.uioh#alwaysscrolly && (conf.scrollb land scrollbhv = 0)
110 || (state.x = 0 && state.w <= state.winw - conf.scrollbw)
111 then 0
112 else conf.scrollbw
115 let vscrollw () =
116 if not state.uioh#alwaysscrolly && (conf.scrollb land scrollbvv = 0)
117 then 0
118 else conf.scrollbw
121 let vscrollhit x =
122 if conf.leftscroll
123 then x < vscrollw ()
124 else x > state.winw - vscrollw ()
127 let wadjsb () = -vscrollw ();;
128 let xadjsb () = if conf.leftscroll then vscrollw () else 0;;
130 let setfontsize n =
131 fstate.fontsize <- n;
132 fstate.wwidth <- measurestr fstate.fontsize "w";
133 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
136 let vlog fmt =
137 if conf.verbose
138 then dolog fmt
139 else Printf.kprintf ignore fmt
142 let launchpath () =
143 if emptystr conf.pathlauncher
144 then dolog "%s" state.path
145 else (
146 let command = Str.global_replace percentsre state.path conf.pathlauncher in
147 match spawn command [] with
148 | _pid -> ()
149 | (exception exn) ->
150 dolog "failed to execute `%s': %s" command @@ exntos exn
154 module G =
155 struct
156 let postRedisplay who =
157 vlog "redisplay for [%S]" who;
158 state.redisplay <- true;
160 end;;
162 let getopaque pageno =
163 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
164 with Not_found -> None
167 let pagetranslatepoint l x y =
168 let dy = y - l.pagedispy in
169 let y = dy + l.pagey in
170 let dx = x - l.pagedispx in
171 let x = dx + l.pagex in
172 (x, y);
175 let onppundermouse g x y d =
176 let rec f = function
177 | l :: rest ->
178 begin match getopaque l.pageno with
179 | Some opaque ->
180 let x0 = l.pagedispx in
181 let x1 = x0 + l.pagevw in
182 let y0 = l.pagedispy in
183 let y1 = y0 + l.pagevh in
184 if y >= y0 && y <= y1 && x >= x0 && x <= x1
185 then
186 let px, py = pagetranslatepoint l x y in
187 match g opaque l px py with
188 | Some res -> res
189 | None -> f rest
190 else f rest
191 | _ ->
192 f rest
194 | [] -> d
196 f state.layout
199 let getunder x y =
200 let g opaque l px py =
201 if state.bzoom
202 then (
203 match rectofblock opaque px py with
204 | Some [|x0;x1;y0;y1|] ->
205 let ox = xadjsb () |> float in
206 let rect = (x0+.ox, y0, x1+.ox, y0, x1+.ox, y1, x0+.ox, y1) in
207 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
208 state.rects <- [l.pageno, color, rect];
209 G.postRedisplay "getunder";
210 | _otherwise -> ()
212 let under = whatsunder opaque px py in
213 if under = Unone then None else Some under
215 onppundermouse g x y Unone
218 let unproject x y =
219 let g opaque l x y =
220 match unproject opaque x y with
221 | Some (x, y) -> Some (Some (opaque, l.pageno, x, y))
222 | None -> None
224 onppundermouse g x y None;
227 let showtext c s =
228 state.text <- Printf.sprintf "%c%s" c s;
229 G.postRedisplay "showtext";
232 let impmsg fmt =
233 Format.ksprintf (fun s -> showtext '!' s) fmt;
236 let pipesel opaque cmd =
237 if hassel opaque
238 then
239 match Unix.pipe () with
240 | (exception exn) -> dolog "pipesel cannot create pipe: %S" @@ exntos exn;
241 | (r, w) ->
242 let doclose what fd =
243 Ne.clo fd (fun msg -> dolog "%s close failed: %s" what msg)
245 let pid =
246 try spawn cmd [r, 0; w, -1]
247 with exn ->
248 dolog "cannot execute %S: %s" cmd @@ exntos exn;
251 if pid > 0
252 then (
253 copysel w opaque;
254 G.postRedisplay "pipesel";
256 else doclose "pipesel pipe/w" w;
257 doclose "pipesel pipe/r" r;
260 let paxunder x y =
261 let g opaque l px py =
262 if markunder opaque px py conf.paxmark
263 then (
264 Some (fun () ->
265 match getopaque l.pageno with
266 | None -> ()
267 | Some opaque -> pipesel opaque conf.paxcmd
270 else None
272 G.postRedisplay "paxunder";
273 if conf.paxmark = Mark_page
274 then
275 List.iter (fun l ->
276 match getopaque l.pageno with
277 | None -> ()
278 | Some opaque -> clearmark opaque) state.layout;
279 state.roam <- onppundermouse g x y (fun () -> impmsg "whoopsie daisy");
282 let selstring s =
283 match Unix.pipe () with
284 | (exception exn) -> impmsg "pipe failed: %s" @@ exntos exn
285 | (r, w) ->
286 let clo cap fd =
287 Ne.clo fd (fun msg -> impmsg "failed to close %s: %s" cap msg)
289 let pid =
290 try spawn conf.selcmd [r, 0; w, -1]
291 with exn ->
292 impmsg "failed to execute %s: %s" conf.selcmd @@ exntos exn;
295 if pid > 0
296 then (
298 let l = String.length s in
299 let bytes = Bytes.unsafe_of_string s in
300 let n = tempfailureretry (Unix.write w bytes 0) l in
301 if n != l
302 then impmsg "failed to write %d characters to sel pipe, wrote %d"
304 with exn ->
305 impmsg "failed to write to sel pipe: %s" @@ exntos exn
307 else dolog "%s" s;
308 clo "selstring pipe/r" r;
309 clo "selstring pipe/w" w;
312 let undertext ?(nopath=false) = function
313 | Unone -> "none"
314 | Ulinkuri s -> s
315 | Ulinkgoto (pageno, _) ->
316 if nopath
317 then "page " ^ string_of_int (pageno+1)
318 else Printf.sprintf "%s: page %d" state.path (pageno+1)
319 | Utext s -> "font: " ^ s
320 | Uunexpected s -> "unexpected: " ^ s
321 | Ulaunch s -> "launch: " ^ s
322 | Unamed s -> "named: " ^ s
323 | Uremote (filename, pageno) ->
324 Printf.sprintf "%s: page %d" filename (pageno+1)
325 | Uremotedest (filename, destname) ->
326 Printf.sprintf "%s: destination %S" filename destname
327 | Uannotation (opaque, slinkindex) ->
328 "annotation: " ^ getannotcontents opaque slinkindex
331 let updateunder x y =
332 match getunder x y with
333 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
334 | Ulinkuri uri ->
335 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
336 Wsi.setcursor Wsi.CURSOR_INFO
337 | Ulinkgoto (pageno, _) ->
338 if conf.underinfo
339 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
340 Wsi.setcursor Wsi.CURSOR_INFO
341 | Utext s ->
342 if conf.underinfo then showtext 'f' ("ont: " ^ s);
343 Wsi.setcursor Wsi.CURSOR_TEXT
344 | Uunexpected s ->
345 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
346 Wsi.setcursor Wsi.CURSOR_INHERIT
347 | Ulaunch s ->
348 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
349 Wsi.setcursor Wsi.CURSOR_INHERIT
350 | Unamed s ->
351 if conf.underinfo then showtext 'n' ("amed: " ^ s);
352 Wsi.setcursor Wsi.CURSOR_INHERIT
353 | Uremote (filename, pageno) ->
354 if conf.underinfo then showtext 'r'
355 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
356 Wsi.setcursor Wsi.CURSOR_INFO
357 | Uremotedest (filename, destname) ->
358 if conf.underinfo then showtext 'r'
359 (Printf.sprintf "emote destination: %s (%S)" filename destname);
360 Wsi.setcursor Wsi.CURSOR_INFO
361 | Uannotation _ ->
362 if conf.underinfo then showtext 'a' "nnotation";
363 Wsi.setcursor Wsi.CURSOR_INFO
366 let showlinktype under =
367 if conf.underinfo && under != Unone
368 then showtext ' ' @@ undertext under
371 let intentry_with_suffix text key =
372 let c =
373 if key >= 32 && key < 127
374 then Char.chr key
375 else '\000'
377 match Char.lowercase c with
378 | '0' .. '9' ->
379 let text = addchar text c in
380 TEcont text
382 | 'k' | 'm' | 'g' ->
383 let text = addchar text c in
384 TEcont text
386 | _ ->
387 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
388 TEcont text
391 let readcmd fd =
392 let s = Bytes.create 4 in
393 let n = tempfailureretry (Unix.read fd s 0) 4 in
394 if n != 4 then error "incomplete read(len) = %d" n;
395 let len = (Char.code (Bytes.get s 0) lsl 24)
396 lor (Char.code (Bytes.get s 1) lsl 16)
397 lor (Char.code (Bytes.get s 2) lsl 8)
398 lor (Char.code (Bytes.get s 3))
400 let s = Bytes.create len in
401 let n = tempfailureretry (Unix.read fd s 0) len in
402 if n != len then error "incomplete read(data) %d vs %d" n len;
403 Bytes.to_string s
406 let wcmd fmt =
407 let b = Buffer.create 16 in
408 Buffer.add_string b "llll";
409 Printf.kbprintf
410 (fun b ->
411 let s = Buffer.to_bytes b in
412 let n = Bytes.length s in
413 let len = n - 4 in
414 (* dolog "wcmd %S" (String.sub s 4 len); *)
415 Bytes.set s 0 (Char.chr ((len lsr 24) land 0xff));
416 Bytes.set s 1 (Char.chr ((len lsr 16) land 0xff));
417 Bytes.set s 2 (Char.chr ((len lsr 8) land 0xff));
418 Bytes.set s 3 (Char.chr (len land 0xff));
419 let n' = tempfailureretry (Unix.write state.ss s 0) n in
420 if n' != n then error "write failed %d vs %d" n' n;
421 ) b fmt;
424 let nogeomcmds cmds =
425 match cmds with
426 | s, [] -> emptystr s
427 | _ -> false
430 let layoutN ((columns, coverA, coverB), b) x y sw sh =
431 let sh = sh - (hscrollh ()) in
432 let wadj = wadjsb () in
433 let rec fold accu n =
434 if n = Array.length b
435 then accu
436 else
437 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
438 if (vy - y) > sh &&
439 (n = coverA - 1
440 || n = state.pagecount - coverB
441 || (n - coverA) mod columns = columns - 1)
442 then accu
443 else
444 let accu =
445 if vy + h > y
446 then
447 let pagey = max 0 (y - vy) in
448 let pagedispy = if pagey > 0 then 0 else vy - y in
449 let pagedispx, pagex =
450 let pdx =
451 if n = coverA - 1 || n = state.pagecount - coverB
452 then x + (wadj + sw - w) / 2
453 else dx + xoff + x
455 if pdx < 0
456 then 0, -pdx
457 else pdx, 0
459 let pagevw =
460 let vw = wadj + sw - pagedispx in
461 let pw = w - pagex in
462 min vw pw
464 let pagevh = min (h - pagey) (sh - pagedispy) in
465 if pagevw > 0 && pagevh > 0
466 then
467 let e =
468 { pageno = n
469 ; pagedimno = pdimno
470 ; pagew = w
471 ; pageh = h
472 ; pagex = pagex
473 ; pagey = pagey
474 ; pagevw = pagevw
475 ; pagevh = pagevh
476 ; pagedispx = pagedispx
477 ; pagedispy = pagedispy
478 ; pagecol = 0
481 e :: accu
482 else
483 accu
484 else
485 accu
487 fold accu (n+1)
489 if Array.length b = 0
490 then []
491 else List.rev (fold [] (page_of_y y))
494 let layoutS (columns, b) x y sw sh =
495 let sh = sh - hscrollh () in
496 let wadj = wadjsb () in
497 let rec fold accu n =
498 if n = Array.length b
499 then accu
500 else
501 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
502 if (vy - y) > sh
503 then accu
504 else
505 let accu =
506 if vy + pageh > y
507 then
508 let x = xoff + x in
509 let pagey = max 0 (y - vy) in
510 let pagedispy = if pagey > 0 then 0 else vy - y in
511 let pagedispx, pagex =
512 if px = 0
513 then (
514 if x < 0
515 then 0, -x
516 else x, 0
518 else (
519 let px = px - x in
520 if px < 0
521 then -px, 0
522 else 0, px
525 let pagecolw = pagew/columns in
526 let pagedispx =
527 if pagecolw < sw
528 then pagedispx + ((wadj + sw - pagecolw) / 2)
529 else pagedispx
531 let pagevw =
532 let vw = wadj + sw - pagedispx in
533 let pw = pagew - pagex in
534 min vw pw
536 let pagevw = min pagevw pagecolw in
537 let pagevh = min (pageh - pagey) (sh - pagedispy) in
538 if pagevw > 0 && pagevh > 0
539 then
540 let e =
541 { pageno = n/columns
542 ; pagedimno = pdimno
543 ; pagew = pagew
544 ; pageh = pageh
545 ; pagex = pagex
546 ; pagey = pagey
547 ; pagevw = pagevw
548 ; pagevh = pagevh
549 ; pagedispx = pagedispx
550 ; pagedispy = pagedispy
551 ; pagecol = n mod columns
554 e :: accu
555 else
556 accu
557 else
558 accu
560 fold accu (n+1)
562 List.rev (fold [] 0)
565 let layout x y sw sh =
566 if nogeomcmds state.geomcmds
567 then
568 match conf.columns with
569 | Csingle b -> layoutN ((1, 0, 0), b) x y sw sh
570 | Cmulti c -> layoutN c x y sw sh
571 | Csplit s -> layoutS s x y sw sh
572 else []
575 let clamp incr =
576 let y = state.y + incr in
577 let y = max 0 y in
578 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
582 let itertiles l f =
583 let tilex = l.pagex mod conf.tilew in
584 let tiley = l.pagey mod conf.tileh in
586 let col = l.pagex / conf.tilew in
587 let row = l.pagey / conf.tileh in
589 let xadj = xadjsb () in
590 let rec rowloop row y0 dispy h =
591 if h = 0
592 then ()
593 else (
594 let dh = conf.tileh - y0 in
595 let dh = min h dh in
596 let rec colloop col x0 dispx w =
597 if w = 0
598 then ()
599 else (
600 let dw = conf.tilew - x0 in
601 let dw = min w dw in
602 let dispx' = xadj + dispx in
603 f col row dispx' dispy x0 y0 dw dh;
604 colloop (col+1) 0 (dispx+dw) (w-dw)
607 colloop col tilex l.pagedispx l.pagevw;
608 rowloop (row+1) 0 (dispy+dh) (h-dh)
611 if l.pagevw > 0 && l.pagevh > 0
612 then rowloop row tiley l.pagedispy l.pagevh;
615 let gettileopaque l col row =
616 let key =
617 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
619 try Some (Hashtbl.find state.tilemap key)
620 with Not_found -> None
623 let puttileopaque l col row gen colorspace angle opaque size elapsed =
624 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
625 Hashtbl.add state.tilemap key (opaque, size, elapsed)
628 let filledrect x0 y0 x1 y1 =
629 GlArray.disable `texture_coord;
630 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
631 GlArray.vertex `two state.vraw;
632 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
633 GlArray.enable `texture_coord;
636 let linerect x0 y0 x1 y1 =
637 GlArray.disable `texture_coord;
638 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y1; x1; y0 |];
639 GlArray.vertex `two state.vraw;
640 GlArray.draw_arrays `line_loop ~first:0 ~count:4;
641 GlArray.enable `texture_coord;
644 let drawtiles l color =
645 GlDraw.color color;
646 let wadj = wadjsb () in
647 begintiles ();
648 let f col row x y tilex tiley w h =
649 match gettileopaque l col row with
650 | Some (opaque, _, t) ->
651 let params = x, y, w, h, tilex, tiley in
652 if conf.invert
653 then GlTex.env (`mode `blend);
654 drawtile params opaque;
655 if conf.invert
656 then GlTex.env (`mode `modulate);
657 if conf.debug
658 then (
659 endtiles ();
660 let s = Printf.sprintf
661 "%d[%d,%d] %f sec"
662 l.pageno col row t
664 let w = measurestr fstate.fontsize s in
665 GlDraw.color (0.0, 0.0, 0.0);
666 filledrect (float (x-2))
667 (float (y-2))
668 (float (x+2) +. w)
669 (float (y + fstate.fontsize + 2));
670 GlDraw.color (1.0, 1.0, 1.0);
671 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
672 begintiles ();
675 | None ->
676 endtiles ();
677 let w =
678 if conf.leftscroll
679 then w
680 else
681 let lw = wadj + state.winw - x in
682 min lw w
683 and h =
684 let lh = state.winh - y in
685 min lh h
687 if conf.invert
688 then GlTex.env (`mode `blend);
689 begin match state.checkerstexid with
690 | Some id ->
691 Gl.enable `texture_2d;
692 GlTex.bind_texture ~target:`texture_2d id;
693 let x0 = float x
694 and y0 = float y
695 and x1 = float (x+w)
696 and y1 = float (y+h) in
698 let tw = float w /. 16.0
699 and th = float h /. 16.0 in
700 let tx0 = float tilex /. 16.0
701 and ty0 = float tiley /. 16.0 in
702 let tx1 = tx0 +. tw
703 and ty1 = ty0 +. th in
704 Raw.sets_float state.vraw ~pos:0
705 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
706 Raw.sets_float state.traw ~pos:0
707 [| tx0; ty0; tx0; ty1; tx1; ty0; tx1; ty1 |];
708 GlArray.vertex `two state.vraw;
709 GlArray.tex_coord `two state.traw;
710 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
711 Gl.disable `texture_2d;
713 | None ->
714 GlDraw.color (1.0, 1.0, 1.0);
715 filledrect (float x) (float y) (float (x+w)) (float (y+h));
716 end;
717 if conf.invert
718 then GlTex.env (`mode `modulate);
719 if w > 128 && h > fstate.fontsize + 10
720 then (
721 let c = if conf.invert then 1.0 else 0.0 in
722 GlDraw.color (c, c, c);
723 let c, r =
724 if conf.verbose
725 then (col*conf.tilew, row*conf.tileh)
726 else col, row
728 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
730 GlDraw.color color;
731 begintiles ();
733 itertiles l f;
734 endtiles ();
737 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
739 let tilevisible1 l x y =
740 let ax0 = l.pagex
741 and ax1 = l.pagex + l.pagevw
742 and ay0 = l.pagey
743 and ay1 = l.pagey + l.pagevh in
745 let bx0 = x
746 and by0 = y in
747 let bx1 = min (bx0 + conf.tilew) l.pagew
748 and by1 = min (by0 + conf.tileh) l.pageh in
750 let rx0 = max ax0 bx0
751 and ry0 = max ay0 by0
752 and rx1 = min ax1 bx1
753 and ry1 = min ay1 by1 in
755 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
756 nonemptyintersection
759 let tilevisible layout n x y =
760 let rec findpageinlayout m = function
761 | l :: rest when l.pageno = n ->
762 tilevisible1 l x y || (
763 match conf.columns with
764 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
765 | Csplit _
766 | Csingle _
767 | Cmulti _ -> false
769 | _ :: rest -> findpageinlayout 0 rest
770 | [] -> false
772 findpageinlayout 0 layout;
775 let tileready l x y =
776 tilevisible1 l x y &&
777 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
780 let tilepage n p layout =
781 let rec loop = function
782 | l :: rest ->
783 if l.pageno = n
784 then
785 let f col row _ _ _ _ _ _ =
786 if state.currently = Idle
787 then
788 match gettileopaque l col row with
789 | Some _ -> ()
790 | None ->
791 let x = col*conf.tilew
792 and y = row*conf.tileh in
793 let w =
794 let w = l.pagew - x in
795 min w conf.tilew
797 let h =
798 let h = l.pageh - y in
799 min h conf.tileh
801 let pbo =
802 if conf.usepbo
803 then getpbo w h conf.colorspace
804 else ~< "0"
806 wcmd "tile %s %d %d %d %d %s"
807 (~> p) x y w h (~> pbo);
808 state.currently <-
809 Tiling (
810 l, p, conf.colorspace, conf.angle,
811 state.gen, col, row, conf.tilew, conf.tileh
814 itertiles l f;
815 else
816 loop rest
818 | [] -> ()
820 if nogeomcmds state.geomcmds
821 then loop layout;
824 let preloadlayout x y sw sh =
825 let y = if y < sh then 0 else y - sh in
826 let h = sh*3 in
827 layout x y sw h;
830 let load pages =
831 let rec loop pages =
832 if state.currently != Idle
833 then ()
834 else
835 match pages with
836 | l :: rest ->
837 begin match getopaque l.pageno with
838 | None ->
839 wcmd "page %d %d" l.pageno l.pagedimno;
840 state.currently <- Loading (l, state.gen);
841 | Some opaque ->
842 tilepage l.pageno opaque pages;
843 loop rest
844 end;
845 | _ -> ()
847 if nogeomcmds state.geomcmds
848 then loop pages
851 let preload pages =
852 load pages;
853 if conf.preload && state.currently = Idle
854 then load (preloadlayout state.x state.y state.winw state.winh);
857 let layoutready layout =
858 let rec fold all ls =
859 all && match ls with
860 | l :: rest ->
861 let seen = ref false in
862 let allvisible = ref true in
863 let foo col row _ _ _ _ _ _ =
864 seen := true;
865 allvisible := !allvisible &&
866 begin match gettileopaque l col row with
867 | Some _ -> true
868 | None -> false
871 itertiles l foo;
872 fold (!seen && !allvisible) rest
873 | [] -> true
875 let alltilesvisible = fold true layout in
876 alltilesvisible;
879 let gotoy y =
880 let y = bound y 0 state.maxy in
881 let y, layout, proceed =
882 match conf.maxwait with
883 | Some time when state.ghyll == noghyll ->
884 begin match state.throttle with
885 | None ->
886 let layout = layout state.x y state.winw state.winh in
887 let ready = layoutready layout in
888 if not ready
889 then (
890 load layout;
891 state.throttle <- Some (layout, y, now ());
893 else G.postRedisplay "gotoy showall (None)";
894 y, layout, ready
895 | Some (_, _, started) ->
896 let dt = now () -. started in
897 if dt > time
898 then (
899 state.throttle <- None;
900 let layout = layout state.x y state.winw state.winh in
901 load layout;
902 G.postRedisplay "maxwait";
903 y, layout, true
905 else -1, [], false
908 | _ ->
909 let layout = layout state.x y state.winw state.winh in
910 if not !wtmode || layoutready layout
911 then G.postRedisplay "gotoy ready";
912 y, layout, true
914 if proceed
915 then (
916 state.y <- y;
917 state.layout <- layout;
918 begin match state.mode with
919 | LinkNav ln ->
920 begin match ln with
921 | Ltexact (pageno, linkno) ->
922 let rec loop = function
923 | [] ->
924 state.mode <- LinkNav (Ltgendir 0)
925 | l :: _ when l.pageno = pageno ->
926 begin match getopaque pageno with
927 | None -> state.mode <- LinkNav (Ltnotready (pageno, 0))
928 | Some opaque ->
929 let x0, y0, x1, y1 = getlinkrect opaque linkno in
930 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
931 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
932 then state.mode <- LinkNav (Ltgendir 0)
934 | _ :: rest -> loop rest
936 loop layout
937 | Ltnotready _ | Ltgendir _ -> ()
939 | Birdseye _
940 | Textentry _
941 | View -> ()
942 end;
943 begin match state.mode with
944 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
945 if not (pagevisible layout pageno)
946 then (
947 match state.layout with
948 | [] -> ()
949 | l :: _ ->
950 state.mode <- Birdseye (
951 conf, leftx, l.pageno, hooverpageno, anchor
954 | LinkNav lt ->
955 begin match lt with
956 | Ltnotready (_, dir)
957 | Ltgendir dir ->
958 let linknav =
959 let rec loop = function
960 | [] -> lt
961 | l :: rest ->
962 match getopaque l.pageno with
963 | None -> Ltnotready (l.pageno, dir)
964 | Some opaque ->
965 let link =
966 let ld =
967 if dir = 0
968 then LDfirstvisible (l.pagex, l.pagey, dir)
969 else (
970 if dir > 0 then LDfirst else LDlast
973 findlink opaque ld
975 match link with
976 | Lnotfound -> loop rest
977 | Lfound n ->
978 showlinktype (getlink opaque n);
979 Ltexact (l.pageno, n)
981 loop state.layout
983 state.mode <- LinkNav linknav
984 | Ltexact _ -> ()
986 | Textentry _
987 | View -> ()
988 end;
989 preload layout;
991 state.ghyll <- noghyll;
992 if conf.updatecurs
993 then (
994 let mx, my = state.mpos in
995 updateunder mx my;
999 let conttiling pageno opaque =
1000 tilepage pageno opaque
1001 (if conf.preload
1002 then preloadlayout state.x state.y state.winw state.winh
1003 else state.layout)
1006 let gotoy_and_clear_text y =
1007 if not conf.verbose then state.text <- E.s;
1008 gotoy y;
1011 let getanchory (n, top, dtop) =
1012 let y, h = getpageyh n in
1013 if conf.presentation
1014 then
1015 let ips = calcips h in
1016 y + truncate (top*.float h -. dtop*.float ips) + ips;
1017 else
1018 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1021 let gotoanchor anchor =
1022 gotoy (getanchory anchor);
1025 let addnav () =
1026 cbput state.hists.nav (getanchor ());
1029 let getnav dir =
1030 let anchor = cbgetc state.hists.nav dir in
1031 getanchory anchor;
1034 let gotoghyll1 single y =
1035 let scroll f n a b =
1036 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1037 let snake f a b =
1038 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1039 if f < a
1040 then s (float f /. float a)
1041 else (
1042 if f > b
1043 then 1.0 -. s ((float (f-b) /. float (n-b)))
1044 else 1.0
1047 snake f a b
1048 and summa n a b =
1049 let ins = float a *. 0.5
1050 and outs = float (n-b) *. 0.5 in
1051 let ones = b - a in
1052 ins +. outs +. float ones
1054 let rec set nab y sy =
1055 let (_N, _A, _B), y =
1056 if single
1057 then
1058 let scl = if y > sy then 2 else -2 in
1059 let _N, _, _ = nab in
1060 (_N,0,_N), y+conf.scrollstep*scl
1061 else nab,y in
1062 let sum = summa _N _A _B in
1063 let dy = float (y - sy) in
1064 state.ghyll <- (
1065 let rec gf n y1 o =
1066 if n >= _N
1067 then state.ghyll <- noghyll
1068 else
1069 let go n =
1070 let s = scroll n _N _A _B in
1071 let y1 = y1 +. ((s *. dy) /. sum) in
1072 gotoy_and_clear_text (truncate y1);
1073 state.ghyll <- gf (n+1) y1;
1075 match o with
1076 | None -> go n
1077 | Some y' when single -> set nab y' state.y
1078 | Some y' -> set (_N/2, 1, 1) y' state.y
1080 gf 0 (float state.y)
1083 match conf.ghyllscroll with
1084 | Some nab when not conf.presentation ->
1085 if state.ghyll == noghyll
1086 then set nab y state.y
1087 else state.ghyll (Some y)
1088 | _ ->
1089 gotoy_and_clear_text y
1092 let gotoghyll = gotoghyll1 false;;
1094 let gotopage n top =
1095 let y, h = getpageyh n in
1096 let y = y + (truncate (top *. float h)) in
1097 gotoghyll y
1100 let gotopage1 n top =
1101 let y = getpagey n in
1102 let y = y + top in
1103 gotoghyll y
1106 let invalidate s f =
1107 state.layout <- [];
1108 state.pdims <- [];
1109 state.rects <- [];
1110 state.rects1 <- [];
1111 match state.geomcmds with
1112 | ps, [] when emptystr ps ->
1113 f ();
1114 state.geomcmds <- s, [];
1116 | ps, [] ->
1117 state.geomcmds <- ps, [s, f];
1119 | ps, (s', _) :: rest when s' = s ->
1120 state.geomcmds <- ps, ((s, f) :: rest);
1122 | ps, cmds ->
1123 state.geomcmds <- ps, ((s, f) :: cmds);
1126 let flushpages () =
1127 Hashtbl.iter (fun _ opaque ->
1128 wcmd "freepage %s" (~> opaque);
1129 ) state.pagemap;
1130 Hashtbl.clear state.pagemap;
1133 let flushtiles () =
1134 if not (Queue.is_empty state.tilelru)
1135 then (
1136 Queue.iter (fun (k, p, s) ->
1137 wcmd "freetile %s" (~> p);
1138 state.memused <- state.memused - s;
1139 Hashtbl.remove state.tilemap k;
1140 ) state.tilelru;
1141 state.uioh#infochanged Memused;
1142 Queue.clear state.tilelru;
1144 load state.layout;
1147 let stateh h =
1148 let h = truncate (float h*.conf.zoom) in
1149 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
1150 h - d
1153 let opendoc path password =
1154 state.path <- path;
1155 state.password <- password;
1156 state.gen <- state.gen + 1;
1157 state.docinfo <- [];
1158 state.outlines <- [||];
1160 flushpages ();
1161 setaalevel conf.aalevel;
1162 let titlepath =
1163 if emptystr state.origin
1164 then path
1165 else state.origin
1167 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
1168 wcmd "open %d %d %s\000%s\000" (btod !wtmode) (btod !cxack) path password;
1169 invalidate "reqlayout"
1170 (fun () ->
1171 wcmd "reqlayout %d %d %d %s\000"
1172 conf.angle (FMTE.to_int conf.fitmodel)
1173 (stateh state.winh) state.nameddest
1177 let reload () =
1178 state.anchor <- getanchor ();
1179 opendoc state.path state.password;
1182 let scalecolor c =
1183 let c = c *. conf.colorscale in
1184 (c, c, c);
1187 let scalecolor2 (r, g, b) =
1188 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1191 let docolumns columns =
1192 let wadj = wadjsb () in
1193 match columns with
1194 | Csingle _ ->
1195 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1196 let wadj = wadjsb () in
1197 let rec loop pageno pdimno pdim y ph pdims =
1198 if pageno = state.pagecount
1199 then ()
1200 else
1201 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1202 match pdims with
1203 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1204 pdimno+1, pdim, rest
1205 | _ ->
1206 pdimno, pdim, pdims
1208 let x = max 0 (((wadj + state.winw - w) / 2) - xoff) in
1209 let y = y +
1210 (if conf.presentation
1211 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1212 else (if pageno = 0 then 0 else conf.interpagespace)
1215 a.(pageno) <- (pdimno, x, y, pdim);
1216 loop (pageno+1) pdimno pdim (y + h) h pdims
1218 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1219 conf.columns <- Csingle a;
1221 | Cmulti ((columns, coverA, coverB), _) ->
1222 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1223 let rec loop pageno pdimno pdim x y rowh pdims =
1224 let rec fixrow m = if m = pageno then () else
1225 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1226 if h < rowh
1227 then (
1228 let y = y + (rowh - h) / 2 in
1229 a.(m) <- (pdimno, x, y, pdim);
1231 fixrow (m+1)
1233 if pageno = state.pagecount
1234 then fixrow (((pageno - 1) / columns) * columns)
1235 else
1236 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1237 match pdims with
1238 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1239 pdimno+1, pdim, rest
1240 | _ ->
1241 pdimno, pdim, pdims
1243 let x, y, rowh' =
1244 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1245 then (
1246 let x = (wadj + state.winw - w) / 2 in
1247 let ips =
1248 if conf.presentation then calcips h else conf.interpagespace in
1249 x, y + ips + rowh, h
1251 else (
1252 if (pageno - coverA) mod columns = 0
1253 then (
1254 let x = max 0 (wadj + state.winw - state.w) / 2 in
1255 let y =
1256 if conf.presentation
1257 then
1258 let ips = calcips h in
1259 y + (if pageno = 0 then 0 else calcips rowh + ips)
1260 else
1261 y + (if pageno = 0 then 0 else conf.interpagespace)
1263 x, y + rowh, h
1265 else x, y, max rowh h
1268 let y =
1269 if pageno > 1 && (pageno - coverA) mod columns = 0
1270 then (
1271 let y =
1272 if pageno = columns && conf.presentation
1273 then (
1274 let ips = calcips rowh in
1275 for i = 0 to pred columns
1277 let (pdimno, x, y, pdim) = a.(i) in
1278 a.(i) <- (pdimno, x, y+ips, pdim)
1279 done;
1280 y+ips;
1282 else y
1284 fixrow (pageno - columns);
1287 else y
1289 a.(pageno) <- (pdimno, x, y, pdim);
1290 let x = x + w + xoff*2 + conf.interpagespace in
1291 loop (pageno+1) pdimno pdim x y rowh' pdims
1293 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1294 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1296 | Csplit (c, _) ->
1297 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1298 let rec loop pageno pdimno pdim y pdims =
1299 if pageno = state.pagecount
1300 then ()
1301 else
1302 let pdimno, ((_, w, h, _) as pdim), pdims =
1303 match pdims with
1304 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1305 pdimno+1, pdim, rest
1306 | _ ->
1307 pdimno, pdim, pdims
1309 let cw = w / c in
1310 let rec loop1 n x y =
1311 if n = c then y else (
1312 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1313 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1316 let y = loop1 0 0 y in
1317 loop (pageno+1) pdimno pdim y pdims
1319 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1320 conf.columns <- Csplit (c, a);
1323 let represent () =
1324 docolumns conf.columns;
1325 state.maxy <- calcheight ();
1326 if state.reprf == noreprf
1327 then (
1328 match state.mode with
1329 | Birdseye (_, _, pageno, _, _) ->
1330 let y, h = getpageyh pageno in
1331 let top = (state.winh - h) / 2 in
1332 gotoy (max 0 (y - top))
1333 | Textentry _
1334 | View
1335 | LinkNav _ -> gotoanchor state.anchor
1337 else (
1338 state.reprf ();
1339 state.reprf <- noreprf;
1343 let reshape ?(firsttime=false) w h =
1344 GlDraw.viewport ~x:0 ~y:0 ~w:w ~h:h;
1345 if not firsttime && nogeomcmds state.geomcmds
1346 then state.anchor <- getanchor ();
1348 state.winw <- w;
1349 let w = wadjsb () + (truncate (float w *. conf.zoom)) in
1350 let w = max w 2 in
1351 state.winh <- h;
1352 setfontsize fstate.fontsize;
1353 GlMat.mode `modelview;
1354 GlMat.load_identity ();
1356 GlMat.mode `projection;
1357 GlMat.load_identity ();
1358 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1359 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1360 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
1362 let relx =
1363 if conf.zoom <= 1.0
1364 then 0.0
1365 else float state.x /. float state.w
1367 invalidate "geometry"
1368 (fun () ->
1369 state.w <- w;
1370 if not firsttime
1371 then state.x <- truncate (relx *. float w);
1372 let w =
1373 match conf.columns with
1374 | Csingle _ -> w
1375 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1376 | Csplit (c, _) -> w * c
1378 wcmd "geometry %d %d %d"
1379 w (stateh h) (FMTE.to_int conf.fitmodel)
1383 let enttext () =
1384 let len = String.length state.text in
1385 let x0 = xadjsb () in
1386 let drawstring s =
1387 let hscrollh =
1388 match state.mode with
1389 | Textentry _ | View | LinkNav _ ->
1390 let h, _, _ = state.uioh#scrollpw in
1392 | Birdseye _ -> 0
1394 let rect x w =
1395 filledrect x (float (state.winh - (fstate.fontsize + 4) - hscrollh))
1396 (x+.w) (float (state.winh - hscrollh))
1399 let w = float (wadjsb () + state.winw - 1) in
1400 if state.progress >= 0.0 && state.progress < 1.0
1401 then (
1402 GlDraw.color (0.3, 0.3, 0.3);
1403 let w1 = w *. state.progress in
1404 rect (float x0) w1;
1405 GlDraw.color (0.0, 0.0, 0.0);
1406 rect (float x0+.w1) (float x0+.w-.w1)
1408 else (
1409 GlDraw.color (0.0, 0.0, 0.0);
1410 rect (float x0) w;
1413 GlDraw.color (1.0, 1.0, 1.0);
1414 drawstring fstate.fontsize
1415 (if conf.leftscroll then x0 + 2 else x0 + if len > 0 then 8 else 2)
1416 (state.winh - hscrollh - 5) s;
1418 let s =
1419 match state.mode with
1420 | Textentry ((prefix, text, _, _, _, _), _) ->
1421 let s =
1422 if len > 0
1423 then
1424 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1425 else
1426 Printf.sprintf "%s%s_" prefix text
1430 | Birdseye _
1431 | View
1432 | LinkNav _ -> state.text
1434 let s =
1435 if state.newerrmsgs
1436 then (
1437 if not (istextentry state.mode) && state.uioh#eformsgs
1438 then
1439 let s1 = "(press 'e' to review error messasges)" in
1440 if nonemptystr s then s ^ " " ^ s1 else s1
1441 else s
1443 else s
1445 if nonemptystr s
1446 then drawstring s
1449 let gctiles () =
1450 let len = Queue.length state.tilelru in
1451 let layout = lazy (
1452 match state.throttle with
1453 | None ->
1454 if conf.preload
1455 then preloadlayout state.x state.y state.winw state.winh
1456 else state.layout
1457 | Some (layout, _, _) ->
1458 layout
1459 ) in
1460 let rec loop qpos =
1461 if state.memused <= conf.memlimit
1462 then ()
1463 else (
1464 if qpos < len
1465 then
1466 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1467 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1468 let (_, pw, ph, _) = getpagedim n in
1470 gen = state.gen
1471 && colorspace = conf.colorspace
1472 && angle = conf.angle
1473 && pagew = pw
1474 && pageh = ph
1475 && (
1476 let x = col*conf.tilew
1477 and y = row*conf.tileh in
1478 tilevisible (Lazy.force_val layout) n x y
1480 then Queue.push lruitem state.tilelru
1481 else (
1482 freepbo p;
1483 wcmd "freetile %s" (~> p);
1484 state.memused <- state.memused - s;
1485 state.uioh#infochanged Memused;
1486 Hashtbl.remove state.tilemap k;
1488 loop (qpos+1)
1491 loop 0
1494 let onpagerect pageno f =
1495 let b =
1496 match conf.columns with
1497 | Cmulti (_, b) -> b
1498 | Csingle b -> b
1499 | Csplit (_, b) -> b
1501 if pageno >= 0 && pageno < Array.length b
1502 then
1503 let (_, _, _, (_, w, h, _)) = b.(pageno) in
1504 f w h
1507 let gotopagexy1 pageno x y =
1508 let _,w1,h1,leftx = getpagedim pageno in
1509 let top = y /. (float h1) in
1510 let left = x /. (float w1) in
1511 let py, w, h = getpageywh pageno in
1512 let wh = state.winh - hscrollh () in
1513 let x = left *. (float w) in
1514 let x = leftx + state.x + truncate x in
1515 let wadj = wadjsb () in
1516 let sx =
1517 if x < 0 || x >= wadj + state.winw
1518 then state.x - x
1519 else state.x
1521 let pdy = truncate (top *. float h) in
1522 let y' = py + pdy in
1523 let dy = y' - state.y in
1524 let sy =
1525 if x != state.x || not (dy > 0 && dy < wh)
1526 then (
1527 if conf.presentation
1528 then
1529 if abs (py - y') > wh
1530 then y'
1531 else py
1532 else y';
1534 else state.y
1536 if state.x != sx || state.y != sy
1537 then (
1538 let x, y =
1539 if !wtmode
1540 then (
1541 let ww = wadj + state.winw in
1542 let qx = sx / ww
1543 and qy = pdy / wh in
1544 let x = qx * ww
1545 and y = py + qy * wh in
1546 let x = if -x + ww > w1 then -(w1-ww) else x
1547 and y' = if y + wh > state.maxy then state.maxy - wh else y in
1548 let y =
1549 if conf.presentation
1550 then
1551 if abs (py - y') > wh
1552 then y'
1553 else py
1554 else y';
1556 (x, y)
1558 else (sx, sy)
1560 state.x <- x;
1561 gotoy_and_clear_text y;
1563 else gotoy_and_clear_text state.y;
1566 let gotopagexy pageno x y =
1567 match state.mode with
1568 | Birdseye _ -> gotopage pageno 0.0
1569 | Textentry _
1570 | View
1571 | LinkNav _ -> gotopagexy1 pageno x y
1574 let getpassword () =
1575 let passcmd = getenvwithdef "LLPP_ASKPASS" conf.passcmd in
1576 if emptystr passcmd
1577 then E.s
1578 else getcmdoutput
1579 (fun s ->
1580 impmsg "error getting password: %s" s;
1581 dolog "%s" s) passcmd;
1584 let pgoto opaque pageno x y =
1585 let pdimno = getpdimno pageno in
1586 let x, y = project opaque pageno pdimno x y in
1587 gotopagexy pageno x y;
1590 let act cmds =
1591 (* dolog "%S" cmds; *)
1592 let cl = splitatspace cmds in
1593 let scan s fmt f =
1594 try Scanf.sscanf s fmt f
1595 with exn ->
1596 dolog "error processing '%S': %s" cmds @@ exntos exn;
1597 exit 1
1599 let addoutline outline =
1600 match state.currently with
1601 | Outlining outlines ->
1602 state.currently <- Outlining (outline :: outlines)
1603 | Idle -> state.currently <- Outlining [outline]
1604 | Loading _
1605 | Tiling _ ->
1606 dolog "invalid outlining state";
1607 logcurrently state.currently
1609 match cl with
1610 | "clear" :: [] ->
1611 state.uioh#infochanged Pdim;
1612 state.pdims <- [];
1614 | "clearrects" :: [] ->
1615 state.rects <- state.rects1;
1616 G.postRedisplay "clearrects";
1618 | "continue" :: args :: [] ->
1619 let n = scan args "%u" (fun n -> n) in
1620 state.pagecount <- n;
1621 begin match state.currently with
1622 | Outlining l ->
1623 state.currently <- Idle;
1624 state.outlines <- Array.of_list (List.rev l)
1625 | Idle
1626 | Loading _
1627 | Tiling _ -> ()
1628 end;
1630 let cur, cmds = state.geomcmds in
1631 if emptystr cur
1632 then failwith "umpossible";
1634 begin match List.rev cmds with
1635 | [] ->
1636 state.geomcmds <- E.s, [];
1637 state.throttle <- None;
1638 represent ();
1639 | (s, f) :: rest ->
1640 f ();
1641 state.geomcmds <- s, List.rev rest;
1642 end;
1643 if conf.maxwait = None && not !wtmode
1644 then G.postRedisplay "continue";
1646 | "msg" :: args :: [] ->
1647 showtext ' ' args
1649 | "vmsg" :: args :: [] ->
1650 if conf.verbose
1651 then showtext ' ' args
1653 | "emsg" :: args :: [] ->
1654 Buffer.add_string state.errmsgs args;
1655 state.newerrmsgs <- true;
1656 G.postRedisplay "error message"
1658 | "progress" :: args :: [] ->
1659 let progress, text =
1660 scan args "%f %n"
1661 (fun f pos ->
1662 f, String.sub args pos (String.length args - pos))
1664 state.text <- text;
1665 state.progress <- progress;
1666 G.postRedisplay "progress"
1668 | "firstmatch" :: args :: [] ->
1669 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1670 scan args "%u %d %f %f %f %f %f %f %f %f"
1671 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1672 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1674 let xoff = float (xadjsb ()) in
1675 let x0 = x0 +. xoff
1676 and x1 = x1 +. xoff
1677 and x2 = x2 +. xoff
1678 and x3 = x3 +. xoff in
1679 let y = (getpagey pageno) + truncate y0 in
1680 if conf.zoom > 1.0
1681 then state.x <- truncate (xoff -. x0) + state.winw/2;
1682 addnav ();
1683 gotoy y;
1684 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1685 state.rects1 <- [pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)]
1687 | "match" :: args :: [] ->
1688 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1689 scan args "%u %d %f %f %f %f %f %f %f %f"
1690 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1691 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1693 let xoff = float (xadjsb ()) in
1694 let x0 = x0 +. xoff
1695 and x1 = x1 +. xoff
1696 and x2 = x2 +. xoff
1697 and x3 = x3 +. xoff in
1698 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1699 state.rects1 <-
1700 (pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1702 | "page" :: args :: [] ->
1703 let pageopaques, t = scan args "%s %f" (fun p t -> p, t) in
1704 let pageopaque = ~< pageopaques in
1705 begin match state.currently with
1706 | Loading (l, gen) ->
1707 vlog "page %d took %f sec" l.pageno t;
1708 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1709 begin match state.throttle with
1710 | None ->
1711 let preloadedpages =
1712 if conf.preload
1713 then preloadlayout state.x state.y state.winw state.winh
1714 else state.layout
1716 let evict () =
1717 let set =
1718 List.fold_left (fun s l -> IntSet.add l.pageno s)
1719 IntSet.empty preloadedpages
1721 let evictedpages =
1722 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1723 if not (IntSet.mem pageno set)
1724 then (
1725 wcmd "freepage %s" (~> opaque);
1726 key :: accu
1728 else accu
1729 ) state.pagemap []
1731 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1733 evict ();
1734 state.currently <- Idle;
1735 if gen = state.gen
1736 then (
1737 tilepage l.pageno pageopaque state.layout;
1738 load state.layout;
1739 load preloadedpages;
1740 let visible = pagevisible state.layout l.pageno in
1741 if visible
1742 then (
1743 match state.mode with
1744 | LinkNav (Ltnotready (pageno, dir)) ->
1745 if pageno = l.pageno
1746 then (
1747 let link =
1748 let ld =
1749 if dir = 0
1750 then LDfirstvisible (l.pagex, l.pagey, dir)
1751 else (
1752 if dir > 0 then LDfirst else LDlast
1755 findlink pageopaque ld
1757 match link with
1758 | Lnotfound -> ()
1759 | Lfound n ->
1760 showlinktype (getlink pageopaque n);
1761 state.mode <- LinkNav (Ltexact (l.pageno, n))
1763 | LinkNav (Ltgendir _)
1764 | LinkNav (Ltexact _)
1765 | View
1766 | Birdseye _
1767 | Textentry _ -> ()
1770 if visible && layoutready state.layout
1771 then (
1772 G.postRedisplay "page";
1776 | Some (layout, _, _) ->
1777 state.currently <- Idle;
1778 tilepage l.pageno pageopaque layout;
1779 load state.layout
1780 end;
1782 | Idle
1783 | Tiling _
1784 | Outlining _ ->
1785 dolog "Inconsistent loading state";
1786 logcurrently state.currently;
1787 exit 1
1790 | "tile" :: args :: [] ->
1791 let (x, y, opaques, size, t) =
1792 scan args "%u %u %s %u %f"
1793 (fun x y p size t -> (x, y, p, size, t))
1795 let opaque = ~< opaques in
1796 begin match state.currently with
1797 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1798 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1800 unmappbo opaque;
1801 if tilew != conf.tilew || tileh != conf.tileh
1802 then (
1803 wcmd "freetile %s" (~> opaque);
1804 state.currently <- Idle;
1805 load state.layout;
1807 else (
1808 puttileopaque l col row gen cs angle opaque size t;
1809 state.memused <- state.memused + size;
1810 state.uioh#infochanged Memused;
1811 gctiles ();
1812 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1813 opaque, size) state.tilelru;
1815 let layout =
1816 match state.throttle with
1817 | None -> state.layout
1818 | Some (layout, _, _) -> layout
1821 state.currently <- Idle;
1822 if gen = state.gen
1823 && conf.colorspace = cs
1824 && conf.angle = angle
1825 && tilevisible layout l.pageno x y
1826 then conttiling l.pageno pageopaque;
1828 begin match state.throttle with
1829 | None ->
1830 preload state.layout;
1831 if gen = state.gen
1832 && conf.colorspace = cs
1833 && conf.angle = angle
1834 && tilevisible state.layout l.pageno x y
1835 && (not !wtmode || layoutready state.layout)
1836 then G.postRedisplay "tile nothrottle";
1838 | Some (layout, y, _) ->
1839 let ready = layoutready layout in
1840 if ready
1841 then (
1842 state.y <- y;
1843 state.layout <- layout;
1844 state.throttle <- None;
1845 G.postRedisplay "throttle";
1847 else load layout;
1848 end;
1851 | Idle
1852 | Loading _
1853 | Outlining _ ->
1854 dolog "Inconsistent tiling state";
1855 logcurrently state.currently;
1856 exit 1
1859 | "pdim" :: args :: [] ->
1860 let (n, w, h, _) as pdim =
1861 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1863 let pdim =
1864 match conf.fitmodel with
1865 | FitWidth -> pdim
1866 | FitPage | FitProportional ->
1867 match conf.columns with
1868 | Csplit _ -> (n, w, h, 0)
1869 | Csingle _ | Cmulti _ -> pdim
1871 state.uioh#infochanged Pdim;
1872 state.pdims <- pdim :: state.pdims
1874 | "o" :: args :: [] ->
1875 let (l, n, t, h, pos) =
1876 scan args "%u %u %d %u %n"
1877 (fun l n t h pos -> l, n, t, h, pos)
1879 let s = String.sub args pos (String.length args - pos) in
1880 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
1882 | "ou" :: args :: [] ->
1883 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
1884 let s = String.sub args pos len in
1885 let pos2 = pos + len + 1 in
1886 let uri = String.sub args pos2 (String.length args - pos2) in
1887 addoutline (s, l, Ouri uri)
1889 | "on" :: args :: [] ->
1890 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
1891 let s = String.sub args pos (String.length args - pos) in
1892 addoutline (s, l, Onone)
1894 | "a" :: args :: [] ->
1895 let (n, l, t) =
1896 scan args "%u %d %d" (fun n l t -> n, l, t)
1898 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
1900 | "info" :: args :: [] ->
1901 let pos = nindex args '\t' in
1902 if pos >= 0 && String.sub args 0 pos = "Title"
1903 then (
1904 let s = String.sub args (pos+1) @@ String.length args - pos - 1 in
1905 conf.title <- s;
1906 Wsi.settitle s;
1908 state.docinfo <- (1, args) :: state.docinfo
1910 | "infoend" :: [] ->
1911 state.uioh#infochanged Docinfo;
1912 state.docinfo <- List.rev state.docinfo
1914 | "pass" :: l ->
1915 if l = "fail" :: []
1916 then Wsi.settitle "Wrong password";
1917 let password = getpassword () in
1918 if emptystr password
1919 then error "document is password protected"
1920 else opendoc state.path password
1921 | _ ->
1922 error "unknown cmd `%S'" cmds
1925 let onhist cb =
1926 let rc = cb.rc in
1927 let action = function
1928 | HCprev -> cbget cb ~-1
1929 | HCnext -> cbget cb 1
1930 | HCfirst -> cbget cb ~-(cb.rc)
1931 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1932 and cancel () = cb.rc <- rc
1933 in (action, cancel)
1936 let search pattern forward =
1937 match conf.columns with
1938 | Csplit _ -> impmsg "searching does not work properly in split columns mode"
1939 | Csingle _
1940 | Cmulti _ ->
1941 if nonemptystr pattern
1942 then
1943 let pn, py =
1944 match state.layout with
1945 | [] -> 0, 0
1946 | l :: _ ->
1947 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1949 wcmd "search %d %d %d %d,%s\000"
1950 (btod conf.icase) pn py (btod forward) pattern;
1953 let intentry text key =
1954 let c =
1955 if key >= 32 && key < 127
1956 then Char.chr key
1957 else '\000'
1959 match c with
1960 | '0' .. '9' ->
1961 let text = addchar text c in
1962 TEcont text
1964 | _ ->
1965 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1966 TEcont text
1969 let linknact f s =
1970 if nonemptystr s
1971 then (
1972 let n =
1973 let l = String.length s in
1974 let rec loop pos n = if pos = l then n else
1975 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
1976 loop (pos+1) (n*26 + m)
1977 in loop 0 0
1979 let rec loop n = function
1980 | [] -> ()
1981 | l :: rest ->
1982 match getopaque l.pageno with
1983 | None -> loop n rest
1984 | Some opaque ->
1985 let m = getlinkcount opaque in
1986 if n < m
1987 then (
1988 let under = getlink opaque n in
1989 f under
1991 else loop (n-m) rest
1993 loop n state.layout;
1997 let linknentry text key =
1998 let c =
1999 if key >= 32 && key < 127
2000 then Char.chr key
2001 else '\000'
2003 match c with
2004 | 'a' .. 'z' ->
2005 let text = addchar text c in
2006 linknact (fun under -> state.text <- undertext ~nopath:true under) text;
2007 TEcont text
2009 | _ ->
2010 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2011 TEcont text
2014 let textentry text key =
2015 if key land 0xff00 = 0xff00
2016 then TEcont text
2017 else TEcont (text ^ toutf8 key)
2020 let reqlayout angle fitmodel =
2021 match state.throttle with
2022 | None ->
2023 if nogeomcmds state.geomcmds
2024 then state.anchor <- getanchor ();
2025 conf.angle <- angle mod 360;
2026 if conf.angle != 0
2027 then (
2028 match state.mode with
2029 | LinkNav _ -> state.mode <- View
2030 | Birdseye _
2031 | Textentry _
2032 | View -> ()
2034 conf.fitmodel <- fitmodel;
2035 invalidate "reqlayout"
2036 (fun () ->
2037 wcmd "reqlayout %d %d %d"
2038 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
2040 | _ -> ()
2043 let settrim trimmargins trimfuzz =
2044 if nogeomcmds state.geomcmds
2045 then state.anchor <- getanchor ();
2046 conf.trimmargins <- trimmargins;
2047 conf.trimfuzz <- trimfuzz;
2048 let x0, y0, x1, y1 = trimfuzz in
2049 invalidate "settrim"
2050 (fun () ->
2051 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2052 flushpages ();
2055 let setzoom zoom =
2056 match state.throttle with
2057 | None ->
2058 let zoom = max 0.0001 zoom in
2059 if zoom <> conf.zoom
2060 then (
2061 state.prevzoom <- (conf.zoom, state.x);
2062 conf.zoom <- zoom;
2063 reshape state.winw state.winh;
2064 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2067 | Some (layout, y, started) ->
2068 let time =
2069 match conf.maxwait with
2070 | None -> 0.0
2071 | Some t -> t
2073 let dt = now () -. started in
2074 if dt > time
2075 then (
2076 state.y <- y;
2077 load layout;
2081 let setcolumns mode columns coverA coverB =
2082 state.prevcolumns <- Some (conf.columns, conf.zoom);
2083 if columns < 0
2084 then (
2085 if isbirdseye mode
2086 then impmsg "split mode doesn't work in bird's eye"
2087 else (
2088 conf.columns <- Csplit (-columns, E.a);
2089 state.x <- 0;
2090 conf.zoom <- 1.0;
2093 else (
2094 if columns < 2
2095 then (
2096 conf.columns <- Csingle E.a;
2097 state.x <- 0;
2098 setzoom 1.0;
2100 else (
2101 conf.columns <- Cmulti ((columns, coverA, coverB), E.a);
2102 conf.zoom <- 1.0;
2105 reshape state.winw state.winh;
2108 let resetmstate () =
2109 state.mstate <- Mnone;
2110 Wsi.setcursor Wsi.CURSOR_INHERIT;
2113 let enterbirdseye () =
2114 let zoom = float conf.thumbw /. float state.winw in
2115 let birdseyepageno =
2116 let cy = state.winh / 2 in
2117 let fold = function
2118 | [] -> 0
2119 | l :: rest ->
2120 let rec fold best = function
2121 | [] -> best.pageno
2122 | l :: rest ->
2123 let d = cy - (l.pagedispy + l.pagevh/2)
2124 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2125 if abs d < abs dbest
2126 then fold l rest
2127 else best.pageno
2128 in fold l rest
2130 fold state.layout
2132 state.mode <- Birdseye (
2133 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2135 resetmstate ();
2136 conf.zoom <- zoom;
2137 conf.presentation <- false;
2138 conf.interpagespace <- 10;
2139 conf.hlinks <- false;
2140 conf.fitmodel <- FitPage;
2141 state.x <- 0;
2142 conf.maxwait <- None;
2143 conf.columns <- (
2144 match conf.beyecolumns with
2145 | Some c ->
2146 conf.zoom <- 1.0;
2147 Cmulti ((c, 0, 0), E.a)
2148 | None -> Csingle E.a
2150 if conf.verbose
2151 then
2152 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2153 (100.0*.zoom)
2154 else
2155 state.text <- E.s
2157 reshape state.winw state.winh;
2160 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2161 state.mode <- View;
2162 conf.zoom <- c.zoom;
2163 conf.presentation <- c.presentation;
2164 conf.interpagespace <- c.interpagespace;
2165 conf.maxwait <- c.maxwait;
2166 conf.hlinks <- c.hlinks;
2167 conf.fitmodel <- c.fitmodel;
2168 conf.beyecolumns <- (
2169 match conf.columns with
2170 | Cmulti ((c, _, _), _) -> Some c
2171 | Csingle _ -> None
2172 | Csplit _ -> failwith "leaving bird's eye split mode"
2174 conf.columns <- (
2175 match c.columns with
2176 | Cmulti (c, _) -> Cmulti (c, E.a)
2177 | Csingle _ -> Csingle E.a
2178 | Csplit (c, _) -> Csplit (c, E.a)
2180 if conf.verbose
2181 then
2182 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2183 (100.0*.conf.zoom)
2185 reshape state.winw state.winh;
2186 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2187 state.x <- leftx;
2190 let togglebirdseye () =
2191 match state.mode with
2192 | Birdseye vals -> leavebirdseye vals true
2193 | View -> enterbirdseye ()
2194 | Textentry _
2195 | LinkNav _ -> ()
2198 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2199 let pageno = max 0 (pageno - incr) in
2200 let rec loop = function
2201 | [] -> gotopage1 pageno 0
2202 | l :: _ when l.pageno = pageno ->
2203 if l.pagedispy >= 0 && l.pagey = 0
2204 then G.postRedisplay "upbirdseye"
2205 else gotopage1 pageno 0
2206 | _ :: rest -> loop rest
2208 loop state.layout;
2209 state.text <- E.s;
2210 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2213 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2214 let pageno = min (state.pagecount - 1) (pageno + incr) in
2215 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2216 let rec loop = function
2217 | [] ->
2218 let y, h = getpageyh pageno in
2219 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2220 gotoy (clamp dy)
2221 | l :: _ when l.pageno = pageno ->
2222 if l.pagevh != l.pageh
2223 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2224 else G.postRedisplay "downbirdseye"
2225 | _ :: rest -> loop rest
2227 loop state.layout;
2228 state.text <- E.s;
2231 let optentry mode _ key =
2232 let btos b = if b then "on" else "off" in
2233 if key >= 32 && key < 127
2234 then
2235 let c = Char.chr key in
2236 match c with
2237 | 's' ->
2238 let ondone s =
2239 try conf.scrollstep <- int_of_string s with exc ->
2240 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exc
2242 TEswitch ("scroll step: ", E.s, None, intentry, ondone, true)
2244 | 'A' ->
2245 let ondone s =
2247 conf.autoscrollstep <- boundastep state.winh (int_of_string s);
2248 if state.autoscroll <> None
2249 then state.autoscroll <- Some conf.autoscrollstep
2250 with exc ->
2251 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exc
2253 TEswitch ("auto scroll step: ", E.s, None, intentry, ondone, true)
2255 | 'C' ->
2256 let ondone s =
2258 let n, a, b = multicolumns_of_string s in
2259 setcolumns mode n a b;
2260 with exc ->
2261 state.text <- Printf.sprintf "bad columns `%s': %s" s @@ exntos exc
2263 TEswitch ("columns: ", E.s, None, textentry, ondone, true)
2265 | 'Z' ->
2266 let ondone s =
2268 let zoom = float (int_of_string s) /. 100.0 in
2269 setzoom zoom
2270 with exc ->
2271 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exc
2273 TEswitch ("zoom: ", E.s, None, intentry, ondone, true)
2275 | 't' ->
2276 let ondone s =
2278 conf.thumbw <- bound (int_of_string s) 2 4096;
2279 state.text <-
2280 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2281 begin match mode with
2282 | Birdseye beye ->
2283 leavebirdseye beye false;
2284 enterbirdseye ();
2285 | Textentry _
2286 | View
2287 | LinkNav _ -> ();
2289 with exc ->
2290 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exc
2292 TEswitch ("thumbnail width: ", E.s, None, intentry, ondone, true)
2294 | 'R' ->
2295 let ondone s =
2296 match try
2297 Some (int_of_string s)
2298 with exc ->
2299 state.text <-
2300 Printf.sprintf "bad integer `%s': %s" s @@ exntos exc;
2301 None
2302 with
2303 | Some angle -> reqlayout angle conf.fitmodel
2304 | None -> ()
2306 TEswitch ("rotation: ", E.s, None, intentry, ondone, true)
2308 | 'i' ->
2309 conf.icase <- not conf.icase;
2310 TEdone ("case insensitive search " ^ (btos conf.icase))
2312 | 'p' ->
2313 conf.preload <- not conf.preload;
2314 gotoy state.y;
2315 TEdone ("preload " ^ (btos conf.preload))
2317 | 'v' ->
2318 conf.verbose <- not conf.verbose;
2319 TEdone ("verbose " ^ (btos conf.verbose))
2321 | 'd' ->
2322 conf.debug <- not conf.debug;
2323 TEdone ("debug " ^ (btos conf.debug))
2325 | 'h' ->
2326 conf.maxhfit <- not conf.maxhfit;
2327 state.maxy <- calcheight ();
2328 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2330 | 'c' ->
2331 conf.crophack <- not conf.crophack;
2332 TEdone ("crophack " ^ btos conf.crophack)
2334 | 'a' ->
2335 let s =
2336 match conf.maxwait with
2337 | None ->
2338 conf.maxwait <- Some infinity;
2339 "always wait for page to complete"
2340 | Some _ ->
2341 conf.maxwait <- None;
2342 "show placeholder if page is not ready"
2344 TEdone s
2346 | 'f' ->
2347 conf.underinfo <- not conf.underinfo;
2348 TEdone ("underinfo " ^ btos conf.underinfo)
2350 | 'P' ->
2351 conf.savebmarks <- not conf.savebmarks;
2352 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2354 | 'S' ->
2355 let ondone s =
2357 let pageno, py =
2358 match state.layout with
2359 | [] -> 0, 0
2360 | l :: _ ->
2361 l.pageno, l.pagey
2363 conf.interpagespace <- int_of_string s;
2364 docolumns conf.columns;
2365 state.maxy <- calcheight ();
2366 let y = getpagey pageno in
2367 gotoy (y + py)
2368 with exc ->
2369 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exc
2371 TEswitch ("vertical margin: ", E.s, None, intentry, ondone, true)
2373 | 'l' ->
2374 let fm =
2375 match conf.fitmodel with
2376 | FitProportional -> FitWidth
2377 | FitWidth | FitPage -> FitProportional
2379 reqlayout conf.angle fm;
2380 TEdone ("proportional display " ^ btos (fm == FitProportional))
2382 | 'T' ->
2383 settrim (not conf.trimmargins) conf.trimfuzz;
2384 TEdone ("trim margins " ^ btos conf.trimmargins)
2386 | 'I' ->
2387 conf.invert <- not conf.invert;
2388 TEdone ("invert colors " ^ btos conf.invert)
2390 | 'x' ->
2391 let ondone s =
2392 cbput state.hists.sel s;
2393 conf.selcmd <- s;
2395 TEswitch ("selection command: ", E.s, Some (onhist state.hists.sel),
2396 textentry, ondone, true)
2398 | 'M' ->
2399 if conf.pax == None
2400 then conf.pax <- Some (ref (0.0, 0, 0))
2401 else conf.pax <- None;
2402 TEdone ("PAX " ^ btos (conf.pax != None))
2404 | _ ->
2405 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2406 TEstop
2407 else
2408 TEcont state.text
2411 class type lvsource = object
2412 method getitemcount : int
2413 method getitem : int -> (string * int)
2414 method hasaction : int -> bool
2415 method exit :
2416 uioh:uioh ->
2417 cancel:bool ->
2418 active:int ->
2419 first:int ->
2420 pan:int ->
2421 uioh option
2422 method getactive : int
2423 method getfirst : int
2424 method getpan : int
2425 method getminfo : (int * int) array
2426 end;;
2428 class virtual lvsourcebase = object
2429 val mutable m_active = 0
2430 val mutable m_first = 0
2431 val mutable m_pan = 0
2432 method getactive = m_active
2433 method getfirst = m_first
2434 method getpan = m_pan
2435 method getminfo : (int * int) array = E.a
2436 end;;
2438 let textentrykeyboard
2439 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2440 state.text <- E.s;
2441 let key =
2442 if key >= 0xffb0 && key <= 0xffb9
2443 then key - 0xffb0 + 48 else key
2445 let enttext te =
2446 state.mode <- Textentry (te, onleave);
2447 enttext ();
2448 G.postRedisplay "textentrykeyboard enttext";
2450 let histaction cmd =
2451 match opthist with
2452 | None -> ()
2453 | Some (action, _) ->
2454 state.mode <- Textentry (
2455 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
2457 G.postRedisplay "textentry histaction"
2459 match key with
2460 | @backspace ->
2461 if emptystr text && cancelonempty
2462 then (
2463 onleave Cancel;
2464 G.postRedisplay "textentrykeyboard after cancel";
2466 else
2467 let s = withoutlastutf8 text in
2468 enttext (c, s, opthist, onkey, ondone, cancelonempty)
2470 | @enter | @kpenter ->
2471 ondone text;
2472 onleave Confirm;
2473 G.postRedisplay "textentrykeyboard after confirm"
2475 | @up | @kpup -> histaction HCprev
2476 | @down | @kpdown -> histaction HCnext
2477 | @home | @kphome -> histaction HCfirst
2478 | @jend | @kpend -> histaction HClast
2480 | @escape ->
2481 if emptystr text
2482 then (
2483 begin match opthist with
2484 | None -> ()
2485 | Some (_, onhistcancel) -> onhistcancel ()
2486 end;
2487 onleave Cancel;
2488 state.text <- E.s;
2489 G.postRedisplay "textentrykeyboard after cancel2"
2491 else (
2492 enttext (c, E.s, opthist, onkey, ondone, cancelonempty)
2495 | @delete | @kpdelete -> ()
2497 | _ when key != 0
2498 && key land 0xff00 != 0xff00 (* keyboard *)
2499 && key land 0xfe00 != 0xfe00 (* xkb *)
2500 && key land 0xfd00 != 0xfd00 (* 3270 *)
2502 begin match onkey text key with
2503 | TEdone text ->
2504 ondone text;
2505 onleave Confirm;
2506 G.postRedisplay "textentrykeyboard after confirm2";
2508 | TEcont text ->
2509 enttext (c, text, opthist, onkey, ondone, cancelonempty);
2511 | TEstop ->
2512 onleave Cancel;
2513 G.postRedisplay "textentrykeyboard after cancel3"
2515 | TEswitch te ->
2516 state.mode <- Textentry (te, onleave);
2517 G.postRedisplay "textentrykeyboard switch";
2518 end;
2520 | _ ->
2521 vlog "unhandled key %s" (Wsi.keyname key)
2524 let firstof first active =
2525 if first > active || abs (first - active) > fstate.maxrows - 1
2526 then max 0 (active - (fstate.maxrows/2))
2527 else first
2530 let calcfirst first active =
2531 if active > first
2532 then
2533 let rows = active - first in
2534 if rows > fstate.maxrows then active - fstate.maxrows else first
2535 else active
2538 let scrollph y maxy =
2539 let sh = float (maxy + state.winh) /. float state.winh in
2540 let sh = float state.winh /. sh in
2541 let sh = max sh (float conf.scrollh) in
2543 let percent = float y /. float maxy in
2544 let position = (float state.winh -. sh) *. percent in
2546 let position =
2547 if position +. sh > float state.winh
2548 then float state.winh -. sh
2549 else position
2551 position, sh;
2554 let coe s = (s :> uioh);;
2556 class listview ~zebra ~helpmode ~(source:lvsource) ~trusted ~modehash =
2557 object (self)
2558 val m_pan = source#getpan
2559 val m_first = source#getfirst
2560 val m_active = source#getactive
2561 val m_qsearch = E.s
2562 val m_prev_uioh = state.uioh
2564 method private elemunder y =
2565 if y < 0
2566 then None
2567 else
2568 let n = y / (fstate.fontsize+1) in
2569 if m_first + n < source#getitemcount
2570 then (
2571 if source#hasaction (m_first + n)
2572 then Some (m_first + n)
2573 else None
2575 else None
2577 method display =
2578 Gl.enable `blend;
2579 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
2580 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2581 filledrect 0. 0. (float state.winw) (float state.winh);
2582 GlDraw.color (1., 1., 1.);
2583 Gl.enable `texture_2d;
2584 let fs = fstate.fontsize in
2585 let nfs = fs + 1 in
2586 let hw = (wadjsb () + xadjsb () + state.winw)/3 in
2587 let ww = fstate.wwidth in
2588 let tabw = 17.0*.ww in
2589 let itemcount = source#getitemcount in
2590 let minfo = source#getminfo in
2591 let x0, x1 =
2592 if conf.leftscroll
2593 then float (xadjsb ()), float (state.winw - 1)
2594 else 0.0, float (state.winw - conf.scrollbw - 1)
2596 let xadj = xadjsb () in
2597 let rec loop row =
2598 if (row - m_first) > fstate.maxrows
2599 then ()
2600 else (
2601 if row >= 0 && row < itemcount
2602 then (
2603 let (s, level) = source#getitem row in
2604 let y = (row - m_first) * nfs in
2605 let x =
2606 (if conf.leftscroll then float xadj else 5.0)
2607 +. (float (level + m_pan)) *. ww in
2608 if helpmode
2609 then GlDraw.color
2610 (let c = if row land 1 = 0 then 1.0 else 0.92 in (c,c,c));
2612 if row = m_active
2613 then (
2614 Gl.disable `texture_2d;
2615 let alpha = if source#hasaction row then 0.9 else 0.3 in
2616 GlDraw.color (1., 1., 1.) ~alpha;
2617 linerect (x0 +. 1.) (float (y + 1)) (x1) (float (y + fs + 3));
2618 Gl.enable `texture_2d;
2620 let c =
2621 if zebra && row land 1 = 1
2622 then 0.8
2623 else 1.0
2625 GlDraw.color (c,c,c);
2626 let drawtabularstring s =
2627 let drawstr x s =
2628 let x' = truncate (x0 +. x) in
2629 let pos = nindex s '\000' in
2630 if pos = -1
2631 then drawstring1 fs x' (y+nfs) s
2632 else
2633 let s1 = String.sub s 0 pos
2634 and s2 = String.sub s (pos+1) (String.length s - pos - 1) in
2635 let rec e s =
2636 if emptystr s
2637 then s
2638 else
2639 let s' = withoutlastutf8 s in
2640 let s = s' ^ "@Uellipsis" in
2641 let w = measurestr fs s in
2642 if float x' +. w +. ww < float (hw + x')
2643 then s
2644 else e s'
2646 let s1 =
2647 if float x' +. ww +. measurestr fs s1 > float (hw + x')
2648 then e s1
2649 else s1
2651 ignore (drawstring1 fs x' (y+nfs) s1);
2652 drawstring1 fs (hw + x') (y+nfs) s2
2654 if trusted
2655 then
2656 let x = if helpmode && row > 0 then x +. ww else x in
2657 let tabpos = nindex s '\t' in
2658 if tabpos > 0
2659 then
2660 let len = String.length s - tabpos - 1 in
2661 let s1 = String.sub s 0 tabpos
2662 and s2 = String.sub s (tabpos + 1) len in
2663 let nx = drawstr x s1 in
2664 let sw = nx -. x in
2665 let x = x +. (max tabw sw) in
2666 drawstr x s2
2667 else
2668 let len = String.length s - 2 in
2669 if len > 0 && s.[0] = '\xc2' && s.[1] = '\xb7'
2670 then
2671 let s = String.sub s 2 len in
2672 let x = if not helpmode then x +. ww else x in
2673 GlDraw.color (1.2, 1.2, 1.2);
2674 let vinc = drawstring1 (fs+fs/4)
2675 (truncate (x -. ww)) (y+nfs) s in
2676 GlDraw.color (1., 1., 1.);
2677 vinc +. (float fs *. 0.8)
2678 else
2679 drawstr x s
2680 else
2681 drawstr x s
2683 ignore (drawtabularstring s);
2684 loop (row+1)
2688 loop m_first;
2689 GlDraw.color (1.0, 1.0, 1.0) ~alpha:0.5;
2690 let xadj = float (xadjsb () + 5) in
2691 let rec loop row =
2692 if (row - m_first) > fstate.maxrows
2693 then ()
2694 else (
2695 if row >= 0 && row < itemcount
2696 then (
2697 let (s, level) = source#getitem row in
2698 let pos0 = nindex s '\000' in
2699 let y = (row - m_first) * nfs in
2700 let x = float (level + m_pan) *. ww in
2701 let (first, last) = minfo.(row) in
2702 let prefix =
2703 if pos0 > 0 && first > pos0
2704 then String.sub s (pos0+1) (first-pos0-1)
2705 else String.sub s 0 first
2707 let suffix = String.sub s first (last - first) in
2708 let w1 = measurestr fstate.fontsize prefix in
2709 let w2 = measurestr fstate.fontsize suffix in
2710 let x = x +. if conf.leftscroll then xadj else 5.0 in
2711 let x = if pos0 > 0 && first > pos0 then x +. float hw else x in
2712 let x0 = x +. w1
2713 and y0 = float (y+2) in
2714 let x1 = x0 +. w2
2715 and y1 = float (y+fs+3) in
2716 filledrect x0 y0 x1 y1;
2717 loop (row+1)
2721 Gl.disable `texture_2d;
2722 if Array.length minfo > 0 then loop m_first;
2723 Gl.disable `blend;
2725 method updownlevel incr =
2726 let len = source#getitemcount in
2727 let curlevel =
2728 if m_active >= 0 && m_active < len
2729 then snd (source#getitem m_active)
2730 else -1
2732 let rec flow i =
2733 if i = len then i-1 else if i = -1 then 0 else
2734 let _, l = source#getitem i in
2735 if l != curlevel then i else flow (i+incr)
2737 let active = flow m_active in
2738 let first = calcfirst m_first active in
2739 G.postRedisplay "outline updownlevel";
2740 {< m_active = active; m_first = first >}
2742 method private key1 key mask =
2743 let set1 active first qsearch =
2744 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2746 let search active pattern incr =
2747 let active = if active = -1 then m_first else active in
2748 let dosearch re =
2749 let rec loop n =
2750 if n >= 0 && n < source#getitemcount
2751 then (
2752 let s, _ = source#getitem n in
2753 match Str.search_forward re s 0 with
2754 | (exception Not_found) -> loop (n + incr)
2755 | _ -> Some n
2757 else None
2759 loop active
2761 Str.regexp_case_fold pattern |> dosearch
2763 let itemcount = source#getitemcount in
2764 let find start incr =
2765 let rec find i =
2766 if i = -1 || i = itemcount
2767 then -1
2768 else (
2769 if source#hasaction i
2770 then i
2771 else find (i + incr)
2774 find start
2776 let set active first =
2777 let first = bound first 0 (itemcount - fstate.maxrows) in
2778 state.text <- E.s;
2779 coe {< m_active = active; m_first = first; m_qsearch = E.s >}
2781 let navigate incr =
2782 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2783 let active, first =
2784 let incr1 = if incr > 0 then 1 else -1 in
2785 if isvisible m_first m_active
2786 then
2787 let next =
2788 let next = m_active + incr in
2789 let next =
2790 if next < 0 || next >= itemcount
2791 then -1
2792 else find next incr1
2794 if abs (m_active - next) > fstate.maxrows
2795 then -1
2796 else next
2798 if next = -1
2799 then
2800 let first = m_first + incr in
2801 let first = bound first 0 (itemcount - fstate.maxrows) in
2802 let next =
2803 let next = m_active + incr in
2804 let next = bound next 0 (itemcount - 1) in
2805 find next ~-incr1
2807 let active =
2808 if next = -1
2809 then m_active
2810 else (
2811 if isvisible first next
2812 then next
2813 else m_active
2816 active, first
2817 else
2818 let first = min next m_first in
2819 let first =
2820 if abs (next - first) > fstate.maxrows
2821 then first + incr
2822 else first
2824 next, first
2825 else
2826 let first = m_first + incr in
2827 let first = bound first 0 (itemcount - 1) in
2828 let active =
2829 let next = m_active + incr in
2830 let next = bound next 0 (itemcount - 1) in
2831 let next = find next incr1 in
2832 let active =
2833 if next = -1 || abs (m_active - first) > fstate.maxrows
2834 then (
2835 let active = if m_active = -1 then next else m_active in
2836 active
2838 else next
2840 if isvisible first active
2841 then active
2842 else -1
2844 active, first
2846 G.postRedisplay "listview navigate";
2847 set active first;
2849 match key with
2850 | (@r|@s) when Wsi.withctrl mask ->
2851 let incr = if key = @r then -1 else 1 in
2852 let active, first =
2853 match search (m_active + incr) m_qsearch incr with
2854 | None ->
2855 state.text <- m_qsearch ^ " [not found]";
2856 m_active, m_first
2857 | Some active ->
2858 state.text <- m_qsearch;
2859 active, firstof m_first active
2861 G.postRedisplay "listview ctrl-r/s";
2862 set1 active first m_qsearch;
2864 | @insert when Wsi.withctrl mask ->
2865 if m_active >= 0 && m_active < source#getitemcount
2866 then (
2867 let s, _ = source#getitem m_active in
2868 selstring s;
2870 coe self
2872 | @backspace ->
2873 if emptystr m_qsearch
2874 then coe self
2875 else (
2876 let qsearch = withoutlastutf8 m_qsearch in
2877 if emptystr qsearch
2878 then (
2879 state.text <- E.s;
2880 G.postRedisplay "listview empty qsearch";
2881 set1 m_active m_first E.s;
2883 else
2884 let active, first =
2885 match search m_active qsearch ~-1 with
2886 | None ->
2887 state.text <- qsearch ^ " [not found]";
2888 m_active, m_first
2889 | Some active ->
2890 state.text <- qsearch;
2891 active, firstof m_first active
2893 G.postRedisplay "listview backspace qsearch";
2894 set1 active first qsearch
2897 | key when (key != 0 && key land 0xff00 != 0xff00) ->
2898 let pattern = m_qsearch ^ toutf8 key in
2899 let active, first =
2900 match search m_active pattern 1 with
2901 | None ->
2902 state.text <- pattern ^ " [not found]";
2903 m_active, m_first
2904 | Some active ->
2905 state.text <- pattern;
2906 active, firstof m_first active
2908 G.postRedisplay "listview qsearch add";
2909 set1 active first pattern;
2911 | @escape ->
2912 state.text <- E.s;
2913 if emptystr m_qsearch
2914 then (
2915 G.postRedisplay "list view escape";
2916 let mx, my = state.mpos in
2917 updateunder mx my;
2918 begin
2919 match
2920 source#exit ~uioh:(coe self)
2921 ~cancel:true ~active:m_active ~first:m_first ~pan:m_pan
2922 with
2923 | None -> m_prev_uioh
2924 | Some uioh -> uioh
2927 else (
2928 G.postRedisplay "list view kill qsearch";
2929 coe {< m_qsearch = E.s >}
2932 | @enter | @kpenter ->
2933 state.text <- E.s;
2934 let self = {< m_qsearch = E.s >} in
2935 let opt =
2936 G.postRedisplay "listview enter";
2937 if m_active >= 0 && m_active < source#getitemcount
2938 then (
2939 source#exit ~uioh:(coe self) ~cancel:false
2940 ~active:m_active ~first:m_first ~pan:m_pan;
2942 else (
2943 source#exit ~uioh:(coe self) ~cancel:true
2944 ~active:m_active ~first:m_first ~pan:m_pan;
2947 begin match opt with
2948 | None -> m_prev_uioh
2949 | Some uioh -> uioh
2952 | @delete | @kpdelete ->
2953 coe self
2955 | @up | @kpup -> navigate ~-1
2956 | @down | @kpdown -> navigate 1
2957 | @prior | @kpprior -> navigate ~-(fstate.maxrows)
2958 | @next | @kpnext -> navigate fstate.maxrows
2960 | @right | @kpright ->
2961 state.text <- E.s;
2962 G.postRedisplay "listview right";
2963 coe {< m_pan = m_pan - 1 >}
2965 | @left | @kpleft ->
2966 state.text <- E.s;
2967 G.postRedisplay "listview left";
2968 coe {< m_pan = m_pan + 1 >}
2970 | @home | @kphome ->
2971 let active = find 0 1 in
2972 G.postRedisplay "listview home";
2973 set active 0;
2975 | @jend | @kpend ->
2976 let first = max 0 (itemcount - fstate.maxrows) in
2977 let active = find (itemcount - 1) ~-1 in
2978 G.postRedisplay "listview end";
2979 set active first;
2981 | key when (key = 0 || key land 0xff00 = 0xff00) ->
2982 coe self
2984 | _ ->
2985 dolog "listview unknown key %#x" key; coe self
2987 method key key mask =
2988 match state.mode with
2989 | Textentry te -> textentrykeyboard key mask te; coe self
2990 | Birdseye _
2991 | View
2992 | LinkNav _ -> self#key1 key mask
2994 method button button down x y _ =
2995 let opt =
2996 match button with
2997 | 1 when vscrollhit x ->
2998 G.postRedisplay "listview scroll";
2999 if down
3000 then
3001 let _, position, sh = self#scrollph in
3002 if y > truncate position && y < truncate (position +. sh)
3003 then (
3004 state.mstate <- Mscrolly;
3005 Some (coe self)
3007 else
3008 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3009 let first = truncate (s *. float source#getitemcount) in
3010 let first = min source#getitemcount first in
3011 Some (coe {< m_first = first; m_active = first >})
3012 else (
3013 state.mstate <- Mnone;
3014 Some (coe self);
3016 | 1 when down ->
3017 begin match self#elemunder y with
3018 | Some n ->
3019 G.postRedisplay "listview click";
3020 source#exit ~uioh:(coe {< m_active = n >})
3021 ~cancel:false ~active:n ~first:m_first ~pan:m_pan
3022 | _ ->
3023 Some (coe self)
3025 | n when (n == 4 || n == 5) && not down ->
3026 let len = source#getitemcount in
3027 let first =
3028 if n = 5 && m_first + fstate.maxrows >= len
3029 then
3030 m_first
3031 else
3032 let first = m_first + (if n == 4 then -1 else 1) in
3033 bound first 0 (len - 1)
3035 G.postRedisplay "listview wheel";
3036 Some (coe {< m_first = first >})
3037 | n when (n = 6 || n = 7) && not down ->
3038 let inc = if n = 7 then -1 else 1 in
3039 G.postRedisplay "listview hwheel";
3040 Some (coe {< m_pan = m_pan + inc >})
3041 | _ ->
3042 Some (coe self)
3044 match opt with
3045 | None -> m_prev_uioh
3046 | Some uioh -> uioh
3048 method multiclick _ x y = self#button 1 true x y
3050 method motion _ y =
3051 match state.mstate with
3052 | Mscrolly ->
3053 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3054 let first = truncate (s *. float source#getitemcount) in
3055 let first = min source#getitemcount first in
3056 G.postRedisplay "listview motion";
3057 coe {< m_first = first; m_active = first >}
3058 | Msel _
3059 | Mpan _
3060 | Mscrollx
3061 | Mzoom _
3062 | Mzoomrect _
3063 | Mnone -> coe self
3065 method pmotion x y =
3066 if x < state.winw - conf.scrollbw
3067 then
3068 let n =
3069 match self#elemunder y with
3070 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3071 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3073 let o =
3074 if n != m_active
3075 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3076 else self
3078 coe o
3079 else (
3080 Wsi.setcursor Wsi.CURSOR_INHERIT;
3081 coe self
3084 method infochanged _ = ()
3086 method scrollpw = (0, 0.0, 0.0)
3087 method scrollph =
3088 let nfs = fstate.fontsize + 1 in
3089 let y = m_first * nfs in
3090 let itemcount = source#getitemcount in
3091 let maxi = max 0 (itemcount - fstate.maxrows) in
3092 let maxy = maxi * nfs in
3093 let p, h = scrollph y maxy in
3094 conf.scrollbw, p, h
3096 method modehash = modehash
3097 method eformsgs = false
3098 method alwaysscrolly = true
3099 end;;
3101 class outlinelistview ~zebra ~source =
3102 let settext autonarrow s =
3103 if autonarrow
3104 then
3105 let ss = source#statestr in
3106 state.text <-
3107 if emptystr ss
3108 then "[" ^ s ^ "]"
3109 else "{" ^ ss ^ "} [" ^ s ^ "]"
3110 else state.text <- s
3112 object (self)
3113 inherit listview
3114 ~zebra
3115 ~helpmode:false
3116 ~source:(source :> lvsource)
3117 ~trusted:false
3118 ~modehash:(findkeyhash conf "outline")
3119 as super
3121 val m_autonarrow = false
3123 method! key key mask =
3124 let maxrows =
3125 if emptystr state.text
3126 then fstate.maxrows
3127 else fstate.maxrows - 2
3129 let calcfirst first active =
3130 if active > first
3131 then
3132 let rows = active - first in
3133 if rows > maxrows then active - maxrows else first
3134 else active
3136 let navigate incr =
3137 let active = m_active + incr in
3138 let active = bound active 0 (source#getitemcount - 1) in
3139 let first = calcfirst m_first active in
3140 G.postRedisplay "outline navigate";
3141 coe {< m_active = active; m_first = first >}
3143 let navscroll first =
3144 let active =
3145 let dist = m_active - first in
3146 if dist < 0
3147 then first
3148 else (
3149 if dist < maxrows
3150 then m_active
3151 else first + maxrows
3154 G.postRedisplay "outline navscroll";
3155 coe {< m_first = first; m_active = active >}
3157 let ctrl = Wsi.withctrl mask in
3158 match key with
3159 | @a when ctrl ->
3160 let text =
3161 if m_autonarrow
3162 then (source#denarrow; E.s)
3163 else (
3164 let pattern = source#renarrow in
3165 if nonemptystr m_qsearch
3166 then (source#narrow m_qsearch; m_qsearch)
3167 else pattern
3170 settext (not m_autonarrow) text;
3171 G.postRedisplay "toggle auto narrowing";
3172 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
3174 | @slash when emptystr m_qsearch && not m_autonarrow ->
3175 settext true E.s;
3176 G.postRedisplay "toggle auto narrowing";
3177 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
3179 | @n when ctrl ->
3180 source#narrow m_qsearch;
3181 if not m_autonarrow
3182 then source#add_narrow_pattern m_qsearch;
3183 G.postRedisplay "outline ctrl-n";
3184 coe {< m_first = 0; m_active = 0 >}
3186 | @S when ctrl ->
3187 let active = source#calcactive (getanchor ()) in
3188 let first = firstof m_first active in
3189 G.postRedisplay "outline ctrl-s";
3190 coe {< m_first = first; m_active = active >}
3192 | @u when ctrl ->
3193 G.postRedisplay "outline ctrl-u";
3194 if m_autonarrow && nonemptystr m_qsearch
3195 then (
3196 ignore (source#renarrow);
3197 settext m_autonarrow E.s;
3198 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3200 else (
3201 source#del_narrow_pattern;
3202 let pattern = source#renarrow in
3203 let text =
3204 if emptystr pattern then E.s else "Narrowed to " ^ pattern
3206 settext m_autonarrow text;
3207 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3210 | @l when ctrl ->
3211 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3212 G.postRedisplay "outline ctrl-l";
3213 coe {< m_first = first >}
3215 | @tab when m_autonarrow ->
3216 if nonemptystr m_qsearch
3217 then (
3218 G.postRedisplay "outline list view tab";
3219 source#add_narrow_pattern m_qsearch;
3220 settext true E.s;
3221 coe {< m_qsearch = E.s >}
3223 else coe self
3225 | @escape when m_autonarrow ->
3226 if nonemptystr m_qsearch
3227 then source#add_narrow_pattern m_qsearch;
3228 super#key key mask
3230 | @enter | @kpenter when m_autonarrow ->
3231 if nonemptystr m_qsearch
3232 then source#add_narrow_pattern m_qsearch;
3233 super#key key mask
3235 | key when m_autonarrow && (key != 0 && key land 0xff00 != 0xff00) ->
3236 let pattern = m_qsearch ^ toutf8 key in
3237 G.postRedisplay "outlinelistview autonarrow add";
3238 source#narrow pattern;
3239 settext true pattern;
3240 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3242 | key when m_autonarrow && key = @backspace ->
3243 if emptystr m_qsearch
3244 then coe self
3245 else
3246 let pattern = withoutlastutf8 m_qsearch in
3247 G.postRedisplay "outlinelistview autonarrow backspace";
3248 ignore (source#renarrow);
3249 source#narrow pattern;
3250 settext true pattern;
3251 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3253 | @up | @kpup when ctrl ->
3254 navscroll (max 0 (m_first - 1))
3256 | @down | @kpdown when ctrl ->
3257 navscroll (min (source#getitemcount - 1) (m_first + 1))
3259 | @up | @kpup -> navigate ~-1
3260 | @down | @kpdown -> navigate 1
3261 | @prior | @kpprior -> navigate ~-(fstate.maxrows)
3262 | @next | @kpnext -> navigate fstate.maxrows
3264 | @right | @kpright ->
3265 let o =
3266 if ctrl
3267 then (
3268 G.postRedisplay "outline ctrl right";
3269 {< m_pan = m_pan + 1 >}
3271 else self#updownlevel 1
3273 coe o
3275 | @left | @kpleft ->
3276 let o =
3277 if ctrl
3278 then (
3279 G.postRedisplay "outline ctrl left";
3280 {< m_pan = m_pan - 1 >}
3282 else self#updownlevel ~-1
3284 coe o
3286 | @home | @kphome ->
3287 G.postRedisplay "outline home";
3288 coe {< m_first = 0; m_active = 0 >}
3290 | @jend | @kpend ->
3291 let active = source#getitemcount - 1 in
3292 let first = max 0 (active - fstate.maxrows) in
3293 G.postRedisplay "outline end";
3294 coe {< m_active = active; m_first = first >}
3296 | _ -> super#key key mask
3297 end;;
3299 let genhistoutlines () =
3300 Config.gethist ()
3301 |> List.sort (fun (_, c1, _, _, _, _) (_, c2, _, _, _, _) ->
3302 compare c2.lastvisit c1.lastvisit)
3303 |> List.map
3304 (fun ((path, c, _, _, _, origin) as hist) ->
3305 let path = if nonemptystr origin then origin else path in
3306 let base = mbtoutf8 @@ Filename.basename path in
3307 (base ^ "\000" ^ c.title, 1, Ohistory hist)
3309 |> Array.of_list
3312 let gotohist (path, c, bookmarks, x, anchor, origin) =
3313 Config.save leavebirdseye;
3314 state.anchor <- anchor;
3315 state.bookmarks <- bookmarks;
3316 state.origin <- origin;
3317 state.x <- x;
3318 setconf conf c;
3319 let x0, y0, x1, y1 = conf.trimfuzz in
3320 wcmd "trimset %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1;
3321 reshape ~firsttime:true state.winw state.winh;
3322 opendoc path origin;
3323 setzoom c.zoom;
3326 let makecheckers () =
3327 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3328 following to say:
3329 converted by Issac Trotts. July 25, 2002 *)
3330 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3331 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3332 let id = GlTex.gen_texture () in
3333 GlTex.bind_texture ~target:`texture_2d id;
3334 GlPix.store (`unpack_alignment 1);
3335 GlTex.image2d image;
3336 List.iter (GlTex.parameter ~target:`texture_2d)
3337 [ `mag_filter `nearest; `min_filter `nearest ];
3341 let setcheckers enabled =
3342 match state.checkerstexid with
3343 | None ->
3344 if enabled then state.checkerstexid <- Some (makecheckers ())
3346 | Some checkerstexid ->
3347 if not enabled
3348 then (
3349 GlTex.delete_texture checkerstexid;
3350 state.checkerstexid <- None;
3354 let describe_location () =
3355 let fn = page_of_y state.y in
3356 let ln = page_of_y (state.y + state.winh - hscrollh () - 1) in
3357 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
3358 let percent =
3359 if maxy <= 0
3360 then 100.
3361 else (100. *. (float state.y /. float maxy))
3363 if fn = ln
3364 then
3365 Printf.sprintf "page %d of %d [%.2f%%]"
3366 (fn+1) state.pagecount percent
3367 else
3368 Printf.sprintf
3369 "pages %d-%d of %d [%.2f%%]"
3370 (fn+1) (ln+1) state.pagecount percent
3373 let setpresentationmode v =
3374 let n = page_of_y state.y in
3375 state.anchor <- (n, 0.0, 1.0);
3376 conf.presentation <- v;
3377 if conf.fitmodel = FitPage
3378 then reqlayout conf.angle conf.fitmodel;
3379 represent ();
3382 let enterinfomode =
3383 let btos b = if b then "@Uradical" else E.s in
3384 let showextended = ref false in
3385 let leave mode _ = state.mode <- mode in
3386 let src =
3387 (object
3388 val mutable m_l = []
3389 val mutable m_a = E.a
3390 val mutable m_prev_uioh = nouioh
3391 val mutable m_prev_mode = View
3393 inherit lvsourcebase
3395 method reset prev_mode prev_uioh =
3396 m_a <- Array.of_list (List.rev m_l);
3397 m_l <- [];
3398 m_prev_mode <- prev_mode;
3399 m_prev_uioh <- prev_uioh;
3401 method int name get set =
3402 m_l <-
3403 (name, `int get, 1, Action (
3404 fun u ->
3405 let ondone s =
3406 try set (int_of_string s)
3407 with exn ->
3408 state.text <- Printf.sprintf "bad integer `%s': %s"
3409 s @@ exntos exn
3411 state.text <- E.s;
3412 let te = name ^ ": ", E.s, None, intentry, ondone, true in
3413 state.mode <- Textentry (te, leave m_prev_mode);
3415 )) :: m_l
3417 method int_with_suffix name get set =
3418 m_l <-
3419 (name, `intws get, 1, Action (
3420 fun u ->
3421 let ondone s =
3422 try set (int_of_string_with_suffix s)
3423 with exn ->
3424 state.text <- Printf.sprintf "bad integer `%s': %s"
3425 s @@ exntos exn
3427 state.text <- E.s;
3428 let te =
3429 name ^ ": ", E.s, None, intentry_with_suffix, ondone, true
3431 state.mode <- Textentry (te, leave m_prev_mode);
3433 )) :: m_l
3435 method bool ?(offset=1) ?(btos=btos) name get set =
3436 m_l <-
3437 (name, `bool (btos, get), offset, Action (
3438 fun u ->
3439 let v = get () in
3440 set (not v);
3442 )) :: m_l
3444 method color name get set =
3445 m_l <-
3446 (name, `color get, 1, Action (
3447 fun u ->
3448 let invalid = (nan, nan, nan) in
3449 let ondone s =
3450 let c =
3451 try color_of_string s
3452 with exn ->
3453 state.text <- Printf.sprintf "bad color `%s': %s"
3454 s @@ exntos exn;
3455 invalid
3457 if c <> invalid
3458 then set c;
3460 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3461 state.text <- color_to_string (get ());
3462 state.mode <- Textentry (te, leave m_prev_mode);
3464 )) :: m_l
3466 method string name get set =
3467 m_l <-
3468 (name, `string get, 1, Action (
3469 fun u ->
3470 let ondone s = set s in
3471 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3472 state.mode <- Textentry (te, leave m_prev_mode);
3474 )) :: m_l
3476 method colorspace name get set =
3477 m_l <-
3478 (name, `string get, 1, Action (
3479 fun _ ->
3480 let source =
3481 (object
3482 inherit lvsourcebase
3484 initializer
3485 m_active <- CSTE.to_int conf.colorspace;
3486 m_first <- 0;
3488 method getitemcount =
3489 Array.length CSTE.names
3490 method getitem n =
3491 (CSTE.names.(n), 0)
3492 method exit ~uioh ~cancel ~active ~first ~pan =
3493 ignore (uioh, first, pan);
3494 if not cancel then set active;
3495 None
3496 method hasaction _ = true
3497 end)
3499 state.text <- E.s;
3500 let modehash = findkeyhash conf "info" in
3501 coe (new listview ~zebra:false ~helpmode:false
3502 ~source ~trusted:true ~modehash)
3503 )) :: m_l
3505 method paxmark name get set =
3506 m_l <-
3507 (name, `string get, 1, Action (
3508 fun _ ->
3509 let source =
3510 (object
3511 inherit lvsourcebase
3513 initializer
3514 m_active <- MTE.to_int conf.paxmark;
3515 m_first <- 0;
3517 method getitemcount = Array.length MTE.names
3518 method getitem n = (MTE.names.(n), 0)
3519 method exit ~uioh ~cancel ~active ~first ~pan =
3520 ignore (uioh, first, pan);
3521 if not cancel then set active;
3522 None
3523 method hasaction _ = true
3524 end)
3526 state.text <- E.s;
3527 let modehash = findkeyhash conf "info" in
3528 coe (new listview ~zebra:false ~helpmode:false
3529 ~source ~trusted:true ~modehash)
3530 )) :: m_l
3532 method fitmodel name get set =
3533 m_l <-
3534 (name, `string get, 1, Action (
3535 fun _ ->
3536 let source =
3537 (object
3538 inherit lvsourcebase
3540 initializer
3541 m_active <- FMTE.to_int conf.fitmodel;
3542 m_first <- 0;
3544 method getitemcount = Array.length FMTE.names
3545 method getitem n = (FMTE.names.(n), 0)
3546 method exit ~uioh ~cancel ~active ~first ~pan =
3547 ignore (uioh, first, pan);
3548 if not cancel then set active;
3549 None
3550 method hasaction _ = true
3551 end)
3553 state.text <- E.s;
3554 let modehash = findkeyhash conf "info" in
3555 coe (new listview ~zebra:false ~helpmode:false
3556 ~source ~trusted:true ~modehash)
3557 )) :: m_l
3559 method caption s offset =
3560 m_l <- (s, `empty, offset, Noaction) :: m_l
3562 method caption2 s f offset =
3563 m_l <- (s, `string f, offset, Noaction) :: m_l
3565 method getitemcount = Array.length m_a
3567 method getitem n =
3568 let tostr = function
3569 | `int f -> string_of_int (f ())
3570 | `intws f -> string_with_suffix_of_int (f ())
3571 | `string f -> f ()
3572 | `color f -> color_to_string (f ())
3573 | `bool (btos, f) -> btos (f ())
3574 | `empty -> E.s
3576 let name, t, offset, _ = m_a.(n) in
3577 ((let s = tostr t in
3578 if nonemptystr s
3579 then Printf.sprintf "%s\t%s" name s
3580 else name),
3581 offset)
3583 method exit ~uioh ~cancel ~active ~first ~pan =
3584 let uiohopt =
3585 if not cancel
3586 then (
3587 let uioh =
3588 match m_a.(active) with
3589 | _, _, _, Action f -> f uioh
3590 | _, _, _, Noaction -> uioh
3592 Some uioh
3594 else None
3596 m_active <- active;
3597 m_first <- first;
3598 m_pan <- pan;
3599 uiohopt
3601 method hasaction n =
3602 match m_a.(n) with
3603 | _, _, _, Action _ -> true
3604 | _, _, _, Noaction -> false
3606 initializer m_active <- 1
3607 end)
3609 let rec fillsrc prevmode prevuioh =
3610 let sep () = src#caption E.s 0 in
3611 let colorp name get set =
3612 src#string name
3613 (fun () -> color_to_string (get ()))
3614 (fun v ->
3616 let c = color_of_string v in
3617 set c
3618 with exn ->
3619 state.text <- Printf.sprintf "bad color `%s': %s" v @@ exntos exn
3622 let oldmode = state.mode in
3623 let birdseye = isbirdseye state.mode in
3625 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3627 src#bool "presentation mode"
3628 (fun () -> conf.presentation)
3629 (fun v -> setpresentationmode v);
3631 src#bool "ignore case in searches"
3632 (fun () -> conf.icase)
3633 (fun v -> conf.icase <- v);
3635 src#bool "preload"
3636 (fun () -> conf.preload)
3637 (fun v -> conf.preload <- v);
3639 src#bool "highlight links"
3640 (fun () -> conf.hlinks)
3641 (fun v -> conf.hlinks <- v);
3643 src#bool "under info"
3644 (fun () -> conf.underinfo)
3645 (fun v -> conf.underinfo <- v);
3647 src#bool "persistent bookmarks"
3648 (fun () -> conf.savebmarks)
3649 (fun v -> conf.savebmarks <- v);
3651 src#fitmodel "fit model"
3652 (fun () -> FMTE.to_string conf.fitmodel)
3653 (fun v -> reqlayout conf.angle (FMTE.of_int v));
3655 src#bool "trim margins"
3656 (fun () -> conf.trimmargins)
3657 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3659 src#bool "persistent location"
3660 (fun () -> conf.jumpback)
3661 (fun v -> conf.jumpback <- v);
3663 sep ();
3664 src#int "inter-page space"
3665 (fun () -> conf.interpagespace)
3666 (fun n ->
3667 conf.interpagespace <- n;
3668 docolumns conf.columns;
3669 let pageno, py =
3670 match state.layout with
3671 | [] -> 0, 0
3672 | l :: _ ->
3673 l.pageno, l.pagey
3675 state.maxy <- calcheight ();
3676 let y = getpagey pageno in
3677 gotoy (y + py)
3680 src#int "page bias"
3681 (fun () -> conf.pagebias)
3682 (fun v -> conf.pagebias <- v);
3684 src#int "scroll step"
3685 (fun () -> conf.scrollstep)
3686 (fun n -> conf.scrollstep <- n);
3688 src#int "horizontal scroll step"
3689 (fun () -> conf.hscrollstep)
3690 (fun v -> conf.hscrollstep <- v);
3692 src#int "auto scroll step"
3693 (fun () ->
3694 match state.autoscroll with
3695 | Some step -> step
3696 | _ -> conf.autoscrollstep)
3697 (fun n ->
3698 let n = boundastep state.winh n in
3699 if state.autoscroll <> None
3700 then state.autoscroll <- Some n;
3701 conf.autoscrollstep <- n);
3703 src#int "zoom"
3704 (fun () -> truncate (conf.zoom *. 100.))
3705 (fun v -> setzoom ((float v) /. 100.));
3707 src#int "rotation"
3708 (fun () -> conf.angle)
3709 (fun v -> reqlayout v conf.fitmodel);
3711 src#int "scroll bar width"
3712 (fun () -> conf.scrollbw)
3713 (fun v ->
3714 conf.scrollbw <- v;
3715 reshape state.winw state.winh;
3718 src#int "scroll handle height"
3719 (fun () -> conf.scrollh)
3720 (fun v -> conf.scrollh <- v;);
3722 src#int "thumbnail width"
3723 (fun () -> conf.thumbw)
3724 (fun v ->
3725 conf.thumbw <- min 4096 v;
3726 match oldmode with
3727 | Birdseye beye ->
3728 leavebirdseye beye false;
3729 enterbirdseye ()
3730 | Textentry _
3731 | View
3732 | LinkNav _ -> ()
3735 let mode = state.mode in
3736 src#string "columns"
3737 (fun () ->
3738 match conf.columns with
3739 | Csingle _ -> "1"
3740 | Cmulti (multi, _) -> multicolumns_to_string multi
3741 | Csplit (count, _) -> "-" ^ string_of_int count
3743 (fun v ->
3744 let n, a, b = multicolumns_of_string v in
3745 setcolumns mode n a b);
3747 sep ();
3748 src#caption "Pixmap cache" 0;
3749 src#int_with_suffix "size (advisory)"
3750 (fun () -> conf.memlimit)
3751 (fun v -> conf.memlimit <- v);
3753 src#caption2 "used"
3754 (fun () -> Printf.sprintf "%s bytes, %d tiles"
3755 (string_with_suffix_of_int state.memused)
3756 (Hashtbl.length state.tilemap)) 1;
3758 sep ();
3759 src#caption "Layout" 0;
3760 src#caption2 "Dimension"
3761 (fun () ->
3762 Printf.sprintf "%dx%d (virtual %dx%d)"
3763 state.winw state.winh
3764 state.w state.maxy)
3766 if conf.debug
3767 then
3768 src#caption2 "Position" (fun () ->
3769 Printf.sprintf "%dx%d" state.x state.y
3771 else
3772 src#caption2 "Position" (fun () -> describe_location ()) 1
3775 sep ();
3776 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3777 "Save these parameters as global defaults at exit"
3778 (fun () -> conf.bedefault)
3779 (fun v -> conf.bedefault <- v)
3782 sep ();
3783 let btos b = if b then "@Ulguillemet" else "@Urguillemet" in
3784 src#bool ~offset:0 ~btos "Extended parameters"
3785 (fun () -> !showextended)
3786 (fun v -> showextended := v; fillsrc prevmode prevuioh);
3787 if !showextended
3788 then (
3789 src#bool "checkers"
3790 (fun () -> conf.checkers)
3791 (fun v -> conf.checkers <- v; setcheckers v);
3792 src#bool "update cursor"
3793 (fun () -> conf.updatecurs)
3794 (fun v -> conf.updatecurs <- v);
3795 src#bool "scroll-bar on the left"
3796 (fun () -> conf.leftscroll)
3797 (fun v -> conf.leftscroll <- v);
3798 src#bool "verbose"
3799 (fun () -> conf.verbose)
3800 (fun v -> conf.verbose <- v);
3801 src#bool "invert colors"
3802 (fun () -> conf.invert)
3803 (fun v -> conf.invert <- v);
3804 src#bool "max fit"
3805 (fun () -> conf.maxhfit)
3806 (fun v -> conf.maxhfit <- v);
3807 src#bool "pax mode"
3808 (fun () -> conf.pax != None)
3809 (fun v ->
3810 if v
3811 then conf.pax <- Some (ref (now (), 0, 0))
3812 else conf.pax <- None);
3813 src#string "uri launcher"
3814 (fun () -> conf.urilauncher)
3815 (fun v -> conf.urilauncher <- v);
3816 src#string "path launcher"
3817 (fun () -> conf.pathlauncher)
3818 (fun v -> conf.pathlauncher <- v);
3819 src#string "tile size"
3820 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3821 (fun v ->
3823 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
3824 conf.tilew <- max 64 w;
3825 conf.tileh <- max 64 h;
3826 flushtiles ();
3827 with exn ->
3828 state.text <- Printf.sprintf "bad tile size `%s': %s"
3829 v @@ exntos exn
3831 src#int "texture count"
3832 (fun () -> conf.texcount)
3833 (fun v ->
3834 if realloctexts v
3835 then conf.texcount <- v
3836 else impmsg "failed to set texture count please retry later"
3838 src#int "slice height"
3839 (fun () -> conf.sliceheight)
3840 (fun v ->
3841 conf.sliceheight <- v;
3842 wcmd "sliceh %d" conf.sliceheight;
3844 src#int "anti-aliasing level"
3845 (fun () -> conf.aalevel)
3846 (fun v ->
3847 conf.aalevel <- bound v 0 8;
3848 state.anchor <- getanchor ();
3849 opendoc state.path state.password;
3851 src#string "page scroll scaling factor"
3852 (fun () -> string_of_float conf.pgscale)
3853 (fun v ->
3855 let s = float_of_string v in
3856 conf.pgscale <- s
3857 with exn ->
3858 state.text <- Printf.sprintf
3859 "bad page scroll scaling factor `%s': %s" v @@ exntos exn
3862 src#int "ui font size"
3863 (fun () -> fstate.fontsize)
3864 (fun v -> setfontsize (bound v 5 100));
3865 src#int "hint font size"
3866 (fun () -> conf.hfsize)
3867 (fun v -> conf.hfsize <- bound v 5 100);
3868 colorp "background color"
3869 (fun () -> conf.bgcolor)
3870 (fun v -> conf.bgcolor <- v);
3871 src#bool "crop hack"
3872 (fun () -> conf.crophack)
3873 (fun v -> conf.crophack <- v);
3874 src#string "trim fuzz"
3875 (fun () -> irect_to_string conf.trimfuzz)
3876 (fun v ->
3878 conf.trimfuzz <- irect_of_string v;
3879 if conf.trimmargins
3880 then settrim true conf.trimfuzz;
3881 with exn ->
3882 state.text <- Printf.sprintf "bad irect `%s': %s" v @@ exntos exn
3884 src#string "throttle"
3885 (fun () ->
3886 match conf.maxwait with
3887 | None -> "show place holder if page is not ready"
3888 | Some time ->
3889 if time = infinity
3890 then "wait for page to fully render"
3891 else
3892 "wait " ^ string_of_float time
3893 ^ " seconds before showing placeholder"
3895 (fun v ->
3897 let f = float_of_string v in
3898 if f <= 0.0
3899 then conf.maxwait <- None
3900 else conf.maxwait <- Some f
3901 with exn ->
3902 state.text <- Printf.sprintf "bad time `%s': %s" v @@ exntos exn
3904 src#string "ghyll scroll"
3905 (fun () ->
3906 match conf.ghyllscroll with
3907 | None -> E.s
3908 | Some nab -> ghyllscroll_to_string nab
3910 (fun v ->
3911 try conf.ghyllscroll <- ghyllscroll_of_string v
3912 with
3913 | Failure msg ->
3914 state.text <- Printf.sprintf "bad ghyll `%s': %s" v msg
3915 | exn ->
3916 state.text <- Printf.sprintf "bad ghyll `%s': %s" v @@ exntos exn
3918 src#string "selection command"
3919 (fun () -> conf.selcmd)
3920 (fun v -> conf.selcmd <- v);
3921 src#string "synctex command"
3922 (fun () -> conf.stcmd)
3923 (fun v -> conf.stcmd <- v);
3924 src#string "pax command"
3925 (fun () -> conf.paxcmd)
3926 (fun v -> conf.paxcmd <- v);
3927 src#string "ask password command"
3928 (fun () -> conf.passcmd)
3929 (fun v -> conf.passcmd <- v);
3930 src#string "save path command"
3931 (fun () -> conf.savecmd)
3932 (fun v -> conf.savecmd <- v);
3933 src#colorspace "color space"
3934 (fun () -> CSTE.to_string conf.colorspace)
3935 (fun v ->
3936 conf.colorspace <- CSTE.of_int v;
3937 wcmd "cs %d" v;
3938 load state.layout;
3940 src#paxmark "pax mark method"
3941 (fun () -> MTE.to_string conf.paxmark)
3942 (fun v -> conf.paxmark <- MTE.of_int v);
3943 if pbousable ()
3944 then
3945 src#bool "use PBO"
3946 (fun () -> conf.usepbo)
3947 (fun v -> conf.usepbo <- v);
3948 src#bool "mouse wheel scrolls pages"
3949 (fun () -> conf.wheelbypage)
3950 (fun v -> conf.wheelbypage <- v);
3951 src#bool "open remote links in a new instance"
3952 (fun () -> conf.riani)
3953 (fun v -> conf.riani <- v);
3954 src#bool "edit annotations inline"
3955 (fun () -> conf.annotinline)
3956 (fun v -> conf.annotinline <- v);
3959 sep ();
3960 src#caption "Document" 0;
3961 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
3962 src#caption2 "Pages"
3963 (fun () -> string_of_int state.pagecount) 1;
3964 src#caption2 "Dimensions"
3965 (fun () -> string_of_int (List.length state.pdims)) 1;
3966 if conf.trimmargins
3967 then (
3968 sep ();
3969 src#caption "Trimmed margins" 0;
3970 src#caption2 "Dimensions"
3971 (fun () -> string_of_int (List.length state.pdims)) 1;
3974 sep ();
3975 src#caption "OpenGL" 0;
3976 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
3977 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
3979 sep ();
3980 src#caption "Location" 0;
3981 if nonemptystr state.origin
3982 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
3983 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
3985 src#reset prevmode prevuioh;
3987 fun () ->
3988 state.text <- E.s;
3989 resetmstate ();
3990 let prevmode = state.mode
3991 and prevuioh = state.uioh in
3992 fillsrc prevmode prevuioh;
3993 let source = (src :> lvsource) in
3994 let modehash = findkeyhash conf "info" in
3995 state.uioh <- coe (object (self)
3996 inherit listview ~zebra:false ~helpmode:false
3997 ~source ~trusted:true ~modehash as super
3998 val mutable m_prevmemused = 0
3999 method! infochanged = function
4000 | Memused ->
4001 if m_prevmemused != state.memused
4002 then (
4003 m_prevmemused <- state.memused;
4004 G.postRedisplay "memusedchanged";
4006 | Pdim -> G.postRedisplay "pdimchanged"
4007 | Docinfo -> fillsrc prevmode prevuioh
4009 method! key key mask =
4010 if not (Wsi.withctrl mask)
4011 then
4012 match key with
4013 | @left | @kpleft -> coe (self#updownlevel ~-1)
4014 | @right | @kpright -> coe (self#updownlevel 1)
4015 | _ -> super#key key mask
4016 else super#key key mask
4017 end);
4018 G.postRedisplay "info";
4021 let enterhelpmode =
4022 let source =
4023 (object
4024 inherit lvsourcebase
4025 method getitemcount = Array.length state.help
4026 method getitem n =
4027 let s, l, _ = state.help.(n) in
4028 (s, l)
4030 method exit ~uioh ~cancel ~active ~first ~pan =
4031 let optuioh =
4032 if not cancel
4033 then (
4034 match state.help.(active) with
4035 | _, _, Action f -> Some (f uioh)
4036 | _, _, Noaction -> Some uioh
4038 else None
4040 m_active <- active;
4041 m_first <- first;
4042 m_pan <- pan;
4043 optuioh
4045 method hasaction n =
4046 match state.help.(n) with
4047 | _, _, Action _ -> true
4048 | _, _, Noaction -> false
4050 initializer
4051 m_active <- -1
4052 end)
4053 in fun () ->
4054 let modehash = findkeyhash conf "help" in
4055 resetmstate ();
4056 state.uioh <- coe (new listview
4057 ~zebra:false ~helpmode:true
4058 ~source ~trusted:true ~modehash);
4059 G.postRedisplay "help";
4062 let entermsgsmode =
4063 let msgsource =
4064 (object
4065 inherit lvsourcebase
4066 val mutable m_items = E.a
4068 method getitemcount = 1 + Array.length m_items
4070 method getitem n =
4071 if n = 0
4072 then "[Clear]", 0
4073 else m_items.(n-1), 0
4075 method exit ~uioh ~cancel ~active ~first ~pan =
4076 ignore uioh;
4077 if not cancel
4078 then (
4079 if active = 0
4080 then Buffer.clear state.errmsgs;
4082 m_active <- active;
4083 m_first <- first;
4084 m_pan <- pan;
4085 None
4087 method hasaction n =
4088 n = 0
4090 method reset =
4091 state.newerrmsgs <- false;
4092 let l = Str.split newlinere (Buffer.contents state.errmsgs) in
4093 m_items <- Array.of_list l
4095 initializer
4096 m_active <- 0
4097 end)
4098 in fun () ->
4099 state.text <- E.s;
4100 resetmstate ();
4101 msgsource#reset;
4102 let source = (msgsource :> lvsource) in
4103 let modehash = findkeyhash conf "listview" in
4104 state.uioh <- coe (object
4105 inherit listview ~zebra:false ~helpmode:false
4106 ~source ~trusted:false ~modehash as super
4107 method! display =
4108 if state.newerrmsgs
4109 then msgsource#reset;
4110 super#display
4111 end);
4112 G.postRedisplay "msgs";
4115 let getusertext s =
4116 let editor = getenvwithdef "EDITOR" E.s in
4117 if emptystr editor
4118 then E.s
4119 else
4120 let tmppath = Filename.temp_file "llpp" "note" in
4121 if nonemptystr s
4122 then (
4123 let oc = open_out tmppath in
4124 output_string oc s;
4125 close_out oc;
4127 let execstr = editor ^ " " ^ tmppath in
4128 let s =
4129 match spawn execstr [] with
4130 | (exception exn) ->
4131 impmsg "spawn(%S) failed: %s" execstr @@ exntos exn;
4133 | pid ->
4134 match Unix.waitpid [] pid with
4135 | (exception exn) ->
4136 impmsg "waitpid(%d) failed: %s" pid @@ exntos exn;
4138 | (_pid, status) ->
4139 match status with
4140 | Unix.WEXITED 0 -> filecontents tmppath
4141 | Unix.WEXITED n ->
4142 impmsg "editor process(%s) exited abnormally: %d" execstr n;
4144 | Unix.WSIGNALED n ->
4145 impmsg "editor process(%s) was killed by signal %d" execstr n;
4147 | Unix.WSTOPPED n ->
4148 impmsg "editor(%s) process was stopped by signal %d" execstr n;
4151 match Unix.unlink tmppath with
4152 | (exception exn) ->
4153 impmsg "failed to ulink %S: %s" tmppath @@ exntos exn;
4155 | () -> s
4158 let enterannotmode opaque slinkindex =
4159 let msgsource =
4160 (object
4161 inherit lvsourcebase
4162 val mutable m_text = E.s
4163 val mutable m_items = E.a
4165 method getitemcount = Array.length m_items
4167 method getitem n =
4168 let label, _func = m_items.(n) in
4169 label, 0
4171 method exit ~uioh ~cancel ~active ~first ~pan =
4172 ignore (uioh, first, pan);
4173 if not cancel
4174 then (
4175 let _label, func = m_items.(active) in
4176 func ()
4178 None
4180 method hasaction n = nonemptystr @@ fst m_items.(n)
4182 method reset s =
4183 let rec split accu b i =
4184 let p = b+i in
4185 if p = String.length s
4186 then (String.sub s b (p-b), unit) :: accu
4187 else
4188 if (i > 70 && s.[p] = ' ') || s.[p] = '\r' || s.[p] = '\n'
4189 then
4190 let ss = if i = 0 then E.s else String.sub s b i in
4191 split ((ss, unit)::accu) (p+1) 0
4192 else
4193 split accu b (i+1)
4195 let cleanup () =
4196 wcmd "freepage %s" (~> opaque);
4197 let keys =
4198 Hashtbl.fold (fun key opaque' accu ->
4199 if opaque' = opaque'
4200 then key :: accu else accu) state.pagemap []
4202 List.iter (Hashtbl.remove state.pagemap) keys;
4203 flushtiles ();
4204 gotoy state.y
4206 let dele () =
4207 delannot opaque slinkindex;
4208 cleanup ();
4210 let edit inline () =
4211 let update s =
4212 if emptystr s
4213 then dele ()
4214 else (
4215 modannot opaque slinkindex s;
4216 cleanup ();
4219 if inline
4220 then
4221 let mode = state.mode in
4222 state.mode <-
4223 Textentry (
4224 ("annotation: ", m_text, None, textentry, update, true),
4225 fun _ -> state.mode <- mode);
4226 state.text <- E.s;
4227 enttext ();
4228 else
4229 let s = getusertext m_text in
4230 update s
4232 m_text <- s;
4233 m_items <-
4234 ( "[Copy]", fun () -> selstring m_text)
4235 :: ("[Delete]", dele)
4236 :: ("[Edit]", edit conf.annotinline)
4237 :: (E.s, unit)
4238 :: split [] 0 0 |> List.rev |> Array.of_list
4240 initializer
4241 m_active <- 0
4242 end)
4244 state.text <- E.s;
4245 let s = getannotcontents opaque slinkindex in
4246 resetmstate ();
4247 msgsource#reset s;
4248 let source = (msgsource :> lvsource) in
4249 let modehash = findkeyhash conf "listview" in
4250 state.uioh <- coe (object
4251 inherit listview ~zebra:false ~helpmode:false
4252 ~source ~trusted:false ~modehash
4253 end);
4254 G.postRedisplay "enterannotmode";
4257 let gotounder under =
4258 let getpath filename =
4259 let path =
4260 if nonemptystr filename
4261 then
4262 if Filename.is_relative filename
4263 then
4264 let dir = Filename.dirname state.path in
4265 let dir =
4266 if Filename.is_implicit dir
4267 then Filename.concat (Sys.getcwd ()) dir
4268 else dir
4270 Filename.concat dir filename
4271 else filename
4272 else E.s
4274 if Sys.file_exists path
4275 then path
4276 else E.s
4278 match under with
4279 | Ulinkgoto (pageno, top) ->
4280 if pageno >= 0
4281 then (
4282 addnav ();
4283 gotopage1 pageno top;
4286 | Ulinkuri s -> gotouri s
4288 | Uremote (filename, pageno) ->
4289 let path = getpath filename in
4290 if nonemptystr path
4291 then (
4292 if conf.riani
4293 then
4294 let command = Printf.sprintf "%s -page %d %S" !selfexec pageno path in
4295 match spawn command [] with
4296 | _pid -> ()
4297 | (exception exn) ->
4298 dolog "failed to execute `%s': %s" command @@ exntos exn
4299 else
4300 let anchor = getanchor () in
4301 let ranchor = state.path, state.password, anchor, state.origin in
4302 state.origin <- E.s;
4303 state.anchor <- (pageno, 0.0, 0.0);
4304 state.ranchors <- ranchor :: state.ranchors;
4305 opendoc path E.s;
4307 else impmsg "cannot find %s" filename
4309 | Uremotedest (filename, destname) ->
4310 let path = getpath filename in
4311 if nonemptystr path
4312 then (
4313 if conf.riani
4314 then
4315 let command = !selfexec ^ " " ^ path ^ " -dest " ^ destname in
4316 match spawn command [] with
4317 | (exception exn) ->
4318 dolog "failed to execute `%s': %s" command @@ exntos exn
4319 | _pid -> ()
4320 else
4321 let anchor = getanchor () in
4322 let ranchor = state.path, state.password, anchor, state.origin in
4323 state.origin <- E.s;
4324 state.nameddest <- destname;
4325 state.ranchors <- ranchor :: state.ranchors;
4326 opendoc path E.s;
4328 else impmsg "cannot find %s" filename
4330 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4331 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
4334 let gotooutline (_, _, kind) =
4335 match kind with
4336 | Onone -> ()
4337 | Oanchor anchor ->
4338 let (pageno, y, _) = anchor in
4339 let y = getanchory
4340 (if conf.presentation then (pageno, y, 1.0) else anchor)
4342 addnav ();
4343 gotoghyll y
4344 | Ouri uri -> gotounder (Ulinkuri uri)
4345 | Olaunch cmd -> gotounder (Ulaunch cmd)
4346 | Oremote remote -> gotounder (Uremote remote)
4347 | Ohistory hist -> gotohist hist
4348 | Oremotedest remotedest -> gotounder (Uremotedest remotedest)
4351 let outlinesource fetchoutlines =
4352 (object (self)
4353 inherit lvsourcebase
4354 val mutable m_items = E.a
4355 val mutable m_minfo = E.a
4356 val mutable m_orig_items = E.a
4357 val mutable m_orig_minfo = E.a
4358 val mutable m_narrow_patterns = []
4359 val mutable m_gen = -1
4361 method getitemcount = Array.length m_items
4363 method getitem n =
4364 let s, n, _ = m_items.(n) in
4365 (s, n)
4367 method exit ~uioh ~cancel ~active ~first ~pan =
4368 ignore (uioh, first);
4369 let items, minfo =
4370 if m_narrow_patterns = []
4371 then m_orig_items, m_orig_minfo
4372 else m_items, m_minfo
4374 m_pan <- pan;
4375 if not cancel
4376 then (
4377 m_items <- items;
4378 m_minfo <- minfo;
4379 gotooutline m_items.(active);
4381 else (
4382 m_items <- items;
4383 m_minfo <- minfo;
4385 None
4387 method hasaction _ = true
4389 method greetmsg =
4390 if Array.length m_items != Array.length m_orig_items
4391 then
4392 let s =
4393 match m_narrow_patterns with
4394 | one :: [] -> one
4395 | many -> String.concat "@Uellipsis" (List.rev many)
4397 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
4398 else E.s
4400 method statestr =
4401 match m_narrow_patterns with
4402 | [] -> E.s
4403 | one :: [] -> one
4404 | head :: _ -> "@Uellipsis" ^ head
4406 method narrow pattern =
4407 match Str.regexp_case_fold pattern with
4408 | (exception _) -> ()
4409 | re ->
4410 let rec loop accu minfo n =
4411 if n = -1
4412 then (
4413 m_items <- Array.of_list accu;
4414 m_minfo <- Array.of_list minfo;
4416 else
4417 let (s, _, _) as o = m_items.(n) in
4418 let accu, minfo =
4419 match Str.search_forward re s 0 with
4420 | (exception Not_found) -> accu, minfo
4421 | first -> o :: accu, (first, Str.match_end ()) :: minfo
4423 loop accu minfo (n-1)
4425 loop [] [] (Array.length m_items - 1)
4427 method! getminfo = m_minfo
4429 method denarrow =
4430 m_orig_items <- fetchoutlines ();
4431 m_minfo <- m_orig_minfo;
4432 m_items <- m_orig_items
4434 method add_narrow_pattern pattern =
4435 m_narrow_patterns <- pattern :: m_narrow_patterns
4437 method del_narrow_pattern =
4438 match m_narrow_patterns with
4439 | _ :: rest -> m_narrow_patterns <- rest
4440 | [] -> ()
4442 method renarrow =
4443 self#denarrow;
4444 match m_narrow_patterns with
4445 | pattern :: [] -> self#narrow pattern; pattern
4446 | list ->
4447 List.fold_left (fun accu pattern ->
4448 self#narrow pattern;
4449 pattern ^ "@Uellipsis" ^ accu) E.s list
4451 method calcactive anchor =
4452 let rely = getanchory anchor in
4453 let rec loop n best bestd =
4454 if n = Array.length m_items
4455 then best
4456 else
4457 let _, _, kind = m_items.(n) in
4458 match kind with
4459 | Oanchor anchor ->
4460 let orely = getanchory anchor in
4461 let d = abs (orely - rely) in
4462 if d < bestd
4463 then loop (n+1) n d
4464 else loop (n+1) best bestd
4465 | Onone | Oremote _ | Olaunch _
4466 | Oremotedest _ | Ouri _ | Ohistory _ ->
4467 loop (n+1) best bestd
4469 loop 0 ~-1 max_int
4471 method reset anchor items =
4472 if state.gen != m_gen
4473 then (
4474 m_orig_items <- items;
4475 m_items <- items;
4476 m_narrow_patterns <- [];
4477 m_minfo <- E.a;
4478 m_orig_minfo <- E.a;
4479 m_gen <- state.gen;
4481 else (
4482 if items != m_orig_items
4483 then (
4484 m_orig_items <- items;
4485 if m_narrow_patterns == []
4486 then m_items <- items;
4489 let active = self#calcactive anchor in
4490 m_active <- active;
4491 m_first <- firstof m_first active
4492 end)
4495 let enteroutlinemode, enterbookmarkmode, enterhistmode =
4496 let mkselector sourcetype =
4497 let fetchoutlines () =
4498 match sourcetype with
4499 | `bookmarks -> Array.of_list state.bookmarks
4500 | `outlines -> state.outlines
4501 | `history -> genhistoutlines ()
4503 let source = outlinesource fetchoutlines in
4504 fun errmsg ->
4505 let outlines = fetchoutlines () in
4506 if Array.length outlines = 0
4507 then (
4508 showtext ' ' errmsg;
4510 else (
4511 resetmstate ();
4512 Wsi.setcursor Wsi.CURSOR_INHERIT;
4513 let anchor = getanchor () in
4514 source#reset anchor outlines;
4515 state.text <- source#greetmsg;
4516 state.uioh <-
4517 coe (new outlinelistview ~zebra:(sourcetype=`history) ~source);
4518 G.postRedisplay "enter selector";
4521 let mkenter sourcetype errmsg =
4522 let enter = mkselector sourcetype in
4523 fun () -> enter errmsg
4525 (**)mkenter `outlines "document has no outline"
4526 , mkenter `bookmarks "document has no bookmarks (yet)"
4527 , mkenter `history "history is empty"
4530 let quickbookmark ?title () =
4531 match state.layout with
4532 | [] -> ()
4533 | l :: _ ->
4534 let title =
4535 match title with
4536 | None ->
4537 let tm = Unix.localtime (now ()) in
4538 Printf.sprintf
4539 "Quick (page %d) (bookmarked at %02d/%02d/%d %02d:%02d)"
4540 (l.pageno+1)
4541 tm.Unix.tm_mday
4542 (tm.Unix.tm_mon+1)
4543 (tm.Unix.tm_year + 1900)
4544 tm.Unix.tm_hour
4545 tm.Unix.tm_min
4546 | Some title -> title
4548 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4551 let setautoscrollspeed step goingdown =
4552 let incr = max 1 ((abs step) / 2) in
4553 let incr = if goingdown then incr else -incr in
4554 let astep = boundastep state.winh (step + incr) in
4555 state.autoscroll <- Some astep;
4558 let canpan () =
4559 match conf.columns with
4560 | Csplit _ -> true
4561 | Csingle _ | Cmulti _ -> state.x != 0 || conf.zoom > 1.0
4564 let panbound x = bound x (-state.w) (wadjsb () + state.winw);;
4566 let existsinrow pageno (columns, coverA, coverB) p =
4567 let last = ((pageno - coverA) mod columns) + columns in
4568 let rec any = function
4569 | [] -> false
4570 | l :: rest ->
4571 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4572 then p l
4573 else (
4574 if not (p l)
4575 then (if l.pageno = last then false else any rest)
4576 else true
4579 any state.layout
4582 let nextpage () =
4583 match state.layout with
4584 | [] ->
4585 let pageno = page_of_y state.y in
4586 gotoghyll (getpagey (pageno+1))
4587 | l :: rest ->
4588 match conf.columns with
4589 | Csingle _ ->
4590 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4591 then
4592 let y = clamp (pgscale state.winh) in
4593 gotoghyll y
4594 else
4595 let pageno = min (l.pageno+1) (state.pagecount-1) in
4596 gotoghyll (getpagey pageno)
4597 | Cmulti ((c, _, _) as cl, _) ->
4598 if conf.presentation
4599 && (existsinrow l.pageno cl
4600 (fun l -> l.pageh > l.pagey + l.pagevh))
4601 then
4602 let y = clamp (pgscale state.winh) in
4603 gotoghyll y
4604 else
4605 let pageno = min (l.pageno+c) (state.pagecount-1) in
4606 gotoghyll (getpagey pageno)
4607 | Csplit (n, _) ->
4608 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4609 then
4610 let pagey, pageh = getpageyh l.pageno in
4611 let pagey = pagey + pageh * l.pagecol in
4612 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4613 gotoghyll (pagey + pageh + ips)
4616 let prevpage () =
4617 match state.layout with
4618 | [] ->
4619 let pageno = page_of_y state.y in
4620 gotoghyll (getpagey (pageno-1))
4621 | l :: _ ->
4622 match conf.columns with
4623 | Csingle _ ->
4624 if conf.presentation && l.pagey != 0
4625 then
4626 gotoghyll (clamp (pgscale ~-(state.winh)))
4627 else
4628 let pageno = max 0 (l.pageno-1) in
4629 gotoghyll (getpagey pageno)
4630 | Cmulti ((c, _, coverB) as cl, _) ->
4631 if conf.presentation &&
4632 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4633 then
4634 gotoghyll (clamp (pgscale ~-(state.winh)))
4635 else
4636 let decr =
4637 if l.pageno = state.pagecount - coverB
4638 then 1
4639 else c
4641 let pageno = max 0 (l.pageno-decr) in
4642 gotoghyll (getpagey pageno)
4643 | Csplit (n, _) ->
4644 let y =
4645 if l.pagecol = 0
4646 then
4647 if l.pageno = 0
4648 then l.pagey
4649 else
4650 let pageno = max 0 (l.pageno-1) in
4651 let pagey, pageh = getpageyh pageno in
4652 pagey + (n-1)*pageh
4653 else
4654 let pagey, pageh = getpageyh l.pageno in
4655 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4657 gotoghyll y
4660 let save () =
4661 if emptystr conf.savecmd
4662 then error "don't know where to save modified document"
4663 else
4664 let savecmd = Str.global_replace percentsre state.path conf.savecmd in
4665 let path =
4666 getcmdoutput
4667 (fun s -> error "failed to obtain path to the saved copy: %s" s)
4668 savecmd
4670 if nonemptystr path
4671 then
4672 let tmp = path ^ ".tmp" in
4673 savedoc tmp;
4674 Unix.rename tmp path;
4677 let viewkeyboard key mask =
4678 let enttext te =
4679 let mode = state.mode in
4680 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4681 state.text <- E.s;
4682 enttext ();
4683 G.postRedisplay "view:enttext"
4685 let ctrl = Wsi.withctrl mask in
4686 let key =
4687 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
4689 match key with
4690 | @Q -> exit 0
4692 | @W ->
4693 if hasunsavedchanges ()
4694 then save ()
4696 | @insert ->
4697 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4698 then (
4699 state.mode <- LinkNav (Ltgendir 0);
4700 gotoy state.y;
4702 else impmsg "keyboard link navigation does not work under rotation"
4704 | @escape | @q ->
4705 begin match state.mstate with
4706 | Mzoomrect _ ->
4707 resetmstate ();
4708 G.postRedisplay "kill rect";
4709 | Msel _
4710 | Mpan _
4711 | Mscrolly | Mscrollx
4712 | Mzoom _
4713 | Mnone ->
4714 begin match state.mode with
4715 | LinkNav _ ->
4716 state.mode <- View;
4717 G.postRedisplay "esc leave linknav"
4718 | Birdseye _
4719 | Textentry _
4720 | View ->
4721 match state.ranchors with
4722 | [] -> raise Quit
4723 | (path, password, anchor, origin) :: rest ->
4724 state.ranchors <- rest;
4725 state.anchor <- anchor;
4726 state.origin <- origin;
4727 state.nameddest <- E.s;
4728 opendoc path password
4729 end;
4730 end;
4732 | @backspace ->
4733 gotoghyll (getnav ~-1)
4735 | @o ->
4736 enteroutlinemode ()
4738 | @H ->
4739 enterhistmode ()
4741 | @u ->
4742 state.rects <- [];
4743 state.text <- E.s;
4744 Hashtbl.iter (fun _ opaque ->
4745 clearmark opaque;
4746 Hashtbl.clear state.prects) state.pagemap;
4747 G.postRedisplay "dehighlight";
4749 | @slash | @question ->
4750 let ondone isforw s =
4751 cbput state.hists.pat s;
4752 state.searchpattern <- s;
4753 search s isforw
4755 let s = String.make 1 (Char.chr key) in
4756 enttext (s, E.s, Some (onhist state.hists.pat),
4757 textentry, ondone (key = @slash), true)
4759 | @plus | @kpplus | @equals when ctrl ->
4760 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4761 setzoom (conf.zoom +. incr)
4763 | @plus | @kpplus ->
4764 let ondone s =
4765 let n =
4766 try int_of_string s with exc ->
4767 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exc;
4768 max_int
4770 if n != max_int
4771 then (
4772 conf.pagebias <- n;
4773 state.text <- "page bias is now " ^ string_of_int n;
4776 enttext ("page bias: ", E.s, None, intentry, ondone, true)
4778 | @minus | @kpminus when ctrl ->
4779 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4780 setzoom (max 0.01 (conf.zoom -. decr))
4782 | @minus | @kpminus ->
4783 let ondone msg = state.text <- msg in
4784 enttext (
4785 "option [acfhilpstvxACFPRSZTISM]: ", E.s, None,
4786 optentry state.mode, ondone, true
4789 | @0 when ctrl ->
4790 if conf.zoom = 1.0
4791 then (
4792 state.x <- 0;
4793 gotoy state.y
4795 else setzoom 1.0
4797 | (@1 | @2) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
4798 let cols =
4799 match conf.columns with
4800 | Csingle _ | Cmulti _ -> 1
4801 | Csplit (n, _) -> n
4803 let h = state.winh -
4804 conf.interpagespace lsl (if conf.presentation then 1 else 0)
4806 let zoom = zoomforh state.winw h (vscrollw ()) cols in
4807 if zoom > 0.0 && (key = 50 || zoom < 1.0)
4808 then setzoom zoom
4810 | @3 when ctrl ->
4811 let fm =
4812 match conf.fitmodel with
4813 | FitWidth -> FitProportional
4814 | FitProportional -> FitPage
4815 | FitPage -> FitWidth
4817 state.text <- "fit model: " ^ FMTE.to_string fm;
4818 reqlayout conf.angle fm
4820 | @F9 ->
4821 togglebirdseye ()
4823 | @9 when ctrl ->
4824 togglebirdseye ()
4826 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4827 when not ctrl -> (* 0..9 *)
4828 let ondone s =
4829 let n =
4830 try int_of_string s with exc ->
4831 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exc;
4834 if n >= 0
4835 then (
4836 addnav ();
4837 cbput state.hists.pag (string_of_int n);
4838 gotopage1 (n + conf.pagebias - 1) 0;
4841 let pageentry text key =
4842 match Char.unsafe_chr key with
4843 | 'g' -> TEdone text
4844 | _ -> intentry text key
4846 let text = String.make 1 (Char.chr key) in
4847 enttext (":", text, Some (onhist state.hists.pag),
4848 pageentry, ondone, true)
4850 | @b ->
4851 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
4852 reshape state.winw state.winh;
4854 | @B ->
4855 state.bzoom <- not state.bzoom;
4856 state.rects <- [];
4857 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
4859 | @l ->
4860 conf.hlinks <- not conf.hlinks;
4861 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4862 G.postRedisplay "toggle highlightlinks";
4864 | @F ->
4865 if conf.angle mod 360 = 0
4866 then (
4867 state.glinks <- true;
4868 let mode = state.mode in
4869 state.mode <-
4870 Textentry (
4871 (":", E.s, None, linknentry, linknact gotounder, false),
4872 (fun _ ->
4873 state.glinks <- false;
4874 state.mode <- mode)
4876 state.text <- E.s;
4877 G.postRedisplay "view:linkent(F)"
4879 else impmsg "hint mode does not work under rotation"
4881 | @y ->
4882 state.glinks <- true;
4883 let mode = state.mode in
4884 state.mode <- Textentry (
4886 ":", E.s, None, linknentry, linknact (fun under ->
4887 selstring (undertext under);
4888 ), false
4890 fun _ ->
4891 state.glinks <- false;
4892 state.mode <- mode
4894 state.text <- E.s;
4895 G.postRedisplay "view:linkent"
4897 | @a ->
4898 begin match state.autoscroll with
4899 | Some step ->
4900 conf.autoscrollstep <- step;
4901 state.autoscroll <- None
4902 | None ->
4903 if conf.autoscrollstep = 0
4904 then state.autoscroll <- Some 1
4905 else state.autoscroll <- Some conf.autoscrollstep
4908 | @p when ctrl ->
4909 launchpath () (* XXX where do error messages go? *)
4911 | @P ->
4912 setpresentationmode (not conf.presentation);
4913 showtext ' ' ("presentation mode " ^
4914 if conf.presentation then "on" else "off");
4916 | @f ->
4917 if List.mem Wsi.Fullscreen state.winstate
4918 then Wsi.reshape conf.cwinw conf.cwinh
4919 else Wsi.fullscreen ()
4921 | @p | @N ->
4922 search state.searchpattern false
4924 | @n | @F3 ->
4925 search state.searchpattern true
4927 | @t ->
4928 begin match state.layout with
4929 | [] -> ()
4930 | l :: _ ->
4931 gotoghyll (getpagey l.pageno)
4934 | @space ->
4935 nextpage ()
4937 | @delete | @kpdelete -> (* delete *)
4938 prevpage ()
4940 | @equals ->
4941 showtext ' ' (describe_location ());
4943 | @w ->
4944 begin match state.layout with
4945 | [] -> ()
4946 | l :: _ ->
4947 Wsi.reshape (l.pagew + vscrollw ()) l.pageh;
4948 G.postRedisplay "w"
4951 | @apos ->
4952 enterbookmarkmode ()
4954 | @h | @F1 ->
4955 enterhelpmode ()
4957 | @i ->
4958 enterinfomode ()
4960 | @e when Buffer.length state.errmsgs > 0 ->
4961 entermsgsmode ()
4963 | @m ->
4964 let ondone s =
4965 match state.layout with
4966 | l :: _ ->
4967 if nonemptystr s
4968 then
4969 state.bookmarks <-
4970 (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4971 | _ -> ()
4973 enttext ("bookmark: ", E.s, None, textentry, ondone, true)
4975 | @tilde ->
4976 quickbookmark ();
4977 showtext ' ' "Quick bookmark added";
4979 | @z ->
4980 begin match state.layout with
4981 | l :: _ ->
4982 let rect = getpdimrect l.pagedimno in
4983 let w, h =
4984 if conf.crophack
4985 then
4986 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4987 truncate (1.2 *. (rect.(3) -. rect.(0))))
4988 else
4989 (truncate (rect.(1) -. rect.(0)),
4990 truncate (rect.(3) -. rect.(0)))
4992 let w = truncate ((float w)*.conf.zoom)
4993 and h = truncate ((float h)*.conf.zoom) in
4994 if w != 0 && h != 0
4995 then (
4996 state.anchor <- getanchor ();
4997 Wsi.reshape (w + vscrollw ()) (h + conf.interpagespace)
4999 G.postRedisplay "z";
5001 | [] -> ()
5004 | @x -> state.roam ()
5006 | @Lt | @Gt ->
5007 reqlayout (conf.angle +
5008 (if key = @Gt then 30 else -30)) conf.fitmodel
5010 | @Lb | @Rb ->
5011 conf.colorscale <-
5012 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5014 G.postRedisplay "brightness";
5016 | @c when state.mode = View ->
5017 if Wsi.withalt mask
5018 then (
5019 if conf.zoom > 1.0
5020 then
5021 let m = (wadjsb () + state.winw - state.w) / 2 in
5022 state.x <- m;
5023 gotoy_and_clear_text state.y
5025 else
5026 let (c, a, b), z =
5027 match state.prevcolumns with
5028 | None -> (1, 0, 0), 1.0
5029 | Some (columns, z) ->
5030 let cab =
5031 match columns with
5032 | Csplit (c, _) -> -c, 0, 0
5033 | Cmulti ((c, a, b), _) -> c, a, b
5034 | Csingle _ -> 1, 0, 0
5036 cab, z
5038 setcolumns View c a b;
5039 setzoom z
5041 | @down | @up when ctrl && Wsi.withshift mask ->
5042 let zoom, x = state.prevzoom in
5043 setzoom zoom;
5044 state.x <- x;
5046 | @k | @up | @kpup ->
5047 begin match state.autoscroll with
5048 | None ->
5049 begin match state.mode with
5050 | Birdseye beye -> upbirdseye 1 beye
5051 | Textentry _
5052 | View
5053 | LinkNav _ ->
5054 if ctrl
5055 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5056 else (
5057 if not (Wsi.withshift mask) && conf.presentation
5058 then prevpage ()
5059 else gotoghyll1 true (clamp (-conf.scrollstep))
5062 | Some n ->
5063 setautoscrollspeed n false
5066 | @j | @down | @kpdown ->
5067 begin match state.autoscroll with
5068 | None ->
5069 begin match state.mode with
5070 | Birdseye beye -> downbirdseye 1 beye
5071 | Textentry _
5072 | View
5073 | LinkNav _ ->
5074 if ctrl
5075 then gotoy_and_clear_text (clamp (state.winh/2))
5076 else (
5077 if not (Wsi.withshift mask) && conf.presentation
5078 then nextpage ()
5079 else gotoghyll1 true (clamp (conf.scrollstep))
5082 | Some n ->
5083 setautoscrollspeed n true
5086 | @left | @right | @kpleft | @kpright when not (Wsi.withalt mask) ->
5087 if canpan ()
5088 then
5089 let dx =
5090 if ctrl
5091 then state.winw / 2
5092 else conf.hscrollstep
5094 let dx = if key = @left || key = @kpleft then dx else -dx in
5095 state.x <- panbound (state.x + dx);
5096 gotoy_and_clear_text state.y
5097 else (
5098 state.text <- E.s;
5099 G.postRedisplay "left/right"
5102 | @prior | @kpprior ->
5103 let y =
5104 if ctrl
5105 then
5106 match state.layout with
5107 | [] -> state.y
5108 | l :: _ -> state.y - l.pagey
5109 else
5110 clamp (pgscale (-state.winh))
5112 gotoghyll y
5114 | @next | @kpnext ->
5115 let y =
5116 if ctrl
5117 then
5118 match List.rev state.layout with
5119 | [] -> state.y
5120 | l :: _ -> getpagey l.pageno
5121 else
5122 clamp (pgscale state.winh)
5124 gotoghyll y
5126 | @g | @home | @kphome ->
5127 addnav ();
5128 gotoghyll 0
5129 | @G | @jend | @kpend ->
5130 addnav ();
5131 gotoghyll (clamp state.maxy)
5133 | @right | @kpright when Wsi.withalt mask ->
5134 gotoghyll (getnav 1)
5135 | @left | @kpleft when Wsi.withalt mask ->
5136 gotoghyll (getnav ~-1)
5138 | @r ->
5139 reload ()
5141 | @v when conf.debug ->
5142 state.rects <- [];
5143 List.iter (fun l ->
5144 match getopaque l.pageno with
5145 | None -> ()
5146 | Some opaque ->
5147 let x0, y0, x1, y1 = pagebbox opaque in
5148 let a,b = float x0, float y0 in
5149 let c,d = float x1, float y0 in
5150 let e,f = float x1, float y1 in
5151 let h,j = float x0, float y1 in
5152 let rect = (a,b,c,d,e,f,h,j) in
5153 debugrect rect;
5154 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
5155 state.rects <- (l.pageno, color, rect) :: state.rects;
5156 ) state.layout;
5157 G.postRedisplay "v";
5159 | @pipe ->
5160 let mode = state.mode in
5161 let cmd = ref E.s in
5162 let onleave = function
5163 | Cancel -> state.mode <- mode
5164 | Confirm ->
5165 List.iter (fun l ->
5166 match getopaque l.pageno with
5167 | Some opaque -> pipesel opaque !cmd
5168 | None -> ()) state.layout;
5169 state.mode <- mode
5171 let ondone s =
5172 cbput state.hists.sel s;
5173 cmd := s
5175 let te =
5176 "| ", !cmd, Some (onhist state.hists.sel), textentry, ondone, true
5178 G.postRedisplay "|";
5179 state.mode <- Textentry (te, onleave);
5181 | _ ->
5182 vlog "huh? %s" (Wsi.keyname key)
5185 let linknavkeyboard key mask linknav =
5186 let getpage pageno =
5187 let rec loop = function
5188 | [] -> None
5189 | l :: _ when l.pageno = pageno -> Some l
5190 | _ :: rest -> loop rest
5191 in loop state.layout
5193 let doexact (pageno, n) =
5194 match getopaque pageno, getpage pageno with
5195 | Some opaque, Some l ->
5196 if key = @enter || key = @kpenter
5197 then
5198 let under = getlink opaque n in
5199 G.postRedisplay "link gotounder";
5200 gotounder under;
5201 state.mode <- View;
5202 else
5203 let opt, dir =
5204 match key with
5205 | @home ->
5206 Some (findlink opaque LDfirst), -1
5208 | @jend ->
5209 Some (findlink opaque LDlast), 1
5211 | @left ->
5212 Some (findlink opaque (LDleft n)), -1
5214 | @right ->
5215 Some (findlink opaque (LDright n)), 1
5217 | @up ->
5218 Some (findlink opaque (LDup n)), -1
5220 | @down ->
5221 Some (findlink opaque (LDdown n)), 1
5223 | _ -> None, 0
5225 let pwl l dir =
5226 begin match findpwl l.pageno dir with
5227 | Pwlnotfound -> ()
5228 | Pwl pageno ->
5229 let notfound dir =
5230 state.mode <- LinkNav (Ltgendir dir);
5231 let y, h = getpageyh pageno in
5232 let y =
5233 if dir < 0
5234 then y + h - state.winh
5235 else y
5237 gotoy y
5239 begin match getopaque pageno, getpage pageno with
5240 | Some opaque, Some _ ->
5241 let link =
5242 let ld = if dir > 0 then LDfirst else LDlast in
5243 findlink opaque ld
5245 begin match link with
5246 | Lfound m ->
5247 showlinktype (getlink opaque m);
5248 state.mode <- LinkNav (Ltexact (pageno, m));
5249 G.postRedisplay "linknav jpage";
5250 | Lnotfound -> notfound dir
5251 end;
5252 | _ -> notfound dir
5253 end;
5254 end;
5256 begin match opt with
5257 | Some Lnotfound -> pwl l dir;
5258 | Some (Lfound m) ->
5259 if m = n
5260 then pwl l dir
5261 else (
5262 let _, y0, _, y1 = getlinkrect opaque m in
5263 if y0 < l.pagey
5264 then gotopage1 l.pageno y0
5265 else (
5266 let d = fstate.fontsize + 1 in
5267 if y1 - l.pagey > l.pagevh - d
5268 then gotopage1 l.pageno (y1 - state.winh - hscrollh () + d)
5269 else G.postRedisplay "linknav";
5271 showlinktype (getlink opaque m);
5272 state.mode <- LinkNav (Ltexact (l.pageno, m));
5275 | None -> viewkeyboard key mask
5276 end;
5277 | _ -> viewkeyboard key mask
5279 if key = @insert
5280 then (
5281 state.mode <- View;
5282 G.postRedisplay "leave linknav"
5284 else
5285 match linknav with
5286 | Ltgendir _ | Ltnotready _ -> viewkeyboard key mask
5287 | Ltexact exact -> doexact exact
5290 let keyboard key mask =
5291 if (key = @g && Wsi.withctrl mask) && not (istextentry state.mode)
5292 then wcmd "interrupt"
5293 else state.uioh <- state.uioh#key key mask
5296 let birdseyekeyboard key mask
5297 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5298 let incr =
5299 match conf.columns with
5300 | Csingle _ -> 1
5301 | Cmulti ((c, _, _), _) -> c
5302 | Csplit _ -> failwith "bird's eye split mode"
5304 let pgh layout = List.fold_left
5305 (fun m l -> max l.pageh m) state.winh layout in
5306 match key with
5307 | @l when Wsi.withctrl mask ->
5308 let y, h = getpageyh pageno in
5309 let top = (state.winh - h) / 2 in
5310 gotoy (max 0 (y - top))
5311 | @enter | @kpenter -> leavebirdseye beye false
5312 | @escape -> leavebirdseye beye true
5313 | @up -> upbirdseye incr beye
5314 | @down -> downbirdseye incr beye
5315 | @left -> upbirdseye 1 beye
5316 | @right -> downbirdseye 1 beye
5318 | @prior ->
5319 begin match state.layout with
5320 | l :: _ ->
5321 if l.pagey != 0
5322 then (
5323 state.mode <- Birdseye (
5324 oconf, leftx, l.pageno, hooverpageno, anchor
5326 gotopage1 l.pageno 0;
5328 else (
5329 let layout = layout state.x (state.y-state.winh)
5330 state.winw
5331 (pgh state.layout) in
5332 match layout with
5333 | [] -> gotoy (clamp (-state.winh))
5334 | l :: _ ->
5335 state.mode <- Birdseye (
5336 oconf, leftx, l.pageno, hooverpageno, anchor
5338 gotopage1 l.pageno 0
5341 | [] -> gotoy (clamp (-state.winh))
5342 end;
5344 | @next ->
5345 begin match List.rev state.layout with
5346 | l :: _ ->
5347 let layout = layout state.x
5348 (state.y + (pgh state.layout))
5349 state.winw state.winh in
5350 begin match layout with
5351 | [] ->
5352 let incr = l.pageh - l.pagevh in
5353 if incr = 0
5354 then (
5355 state.mode <-
5356 Birdseye (
5357 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5359 G.postRedisplay "birdseye pagedown";
5361 else gotoy (clamp (incr + conf.interpagespace*2));
5363 | l :: _ ->
5364 state.mode <-
5365 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5366 gotopage1 l.pageno 0;
5369 | [] -> gotoy (clamp state.winh)
5370 end;
5372 | @home ->
5373 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5374 gotopage1 0 0
5376 | @jend ->
5377 let pageno = state.pagecount - 1 in
5378 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5379 if not (pagevisible state.layout pageno)
5380 then
5381 let h =
5382 match List.rev state.pdims with
5383 | [] -> state.winh
5384 | (_, _, h, _) :: _ -> h
5386 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5387 else G.postRedisplay "birdseye end";
5389 | _ -> viewkeyboard key mask
5392 let drawpage l =
5393 let color =
5394 match state.mode with
5395 | Textentry _ -> scalecolor 0.4
5396 | LinkNav _
5397 | View -> scalecolor 1.0
5398 | Birdseye (_, _, pageno, hooverpageno, _) ->
5399 if l.pageno = hooverpageno
5400 then scalecolor 0.9
5401 else (
5402 if l.pageno = pageno
5403 then (
5404 let c = scalecolor 1.0 in
5405 GlDraw.color c;
5406 GlDraw.line_width 3.0;
5407 let dispx = xadjsb () + l.pagedispx in
5408 linerect
5409 (float (dispx-1)) (float (l.pagedispy-1))
5410 (float (dispx+l.pagevw+1))
5411 (float (l.pagedispy+l.pagevh+1))
5413 GlDraw.line_width 1.0;
5416 else scalecolor 0.8
5419 drawtiles l color;
5422 let postdrawpage l linkindexbase =
5423 match getopaque l.pageno with
5424 | Some opaque ->
5425 if tileready l l.pagex l.pagey
5426 then
5427 let x = l.pagedispx - l.pagex + xadjsb ()
5428 and y = l.pagedispy - l.pagey in
5429 let hlmask =
5430 match conf.columns with
5431 | Csingle _ | Cmulti _ ->
5432 (if conf.hlinks then 1 else 0)
5433 + (if state.glinks
5434 && not (isbirdseye state.mode) then 2 else 0)
5435 | Csplit _ -> 0
5437 let s =
5438 match state.mode with
5439 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5440 | Textentry _
5441 | Birdseye _
5442 | View
5443 | LinkNav _ -> E.s
5445 Hashtbl.find_all state.prects l.pageno |>
5446 List.iter (fun vals -> drawprect opaque x y vals);
5447 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5448 else 0
5449 | _ -> 0
5452 let scrollindicator () =
5453 let sbw, ph, sh = state.uioh#scrollph in
5454 let sbh, pw, sw = state.uioh#scrollpw in
5456 let x0,x1,hx0 =
5457 if conf.leftscroll
5458 then (0, sbw, sbw)
5459 else ((state.winw - sbw), state.winw, 0)
5462 GlDraw.color (0.64, 0.64, 0.64);
5463 filledrect (float x0) 0. (float x1) (float state.winh);
5464 filledrect
5465 (float hx0) (float (state.winh - sbh))
5466 (float (hx0 + wadjsb () + state.winw)) (float state.winh)
5468 GlDraw.color (0.0, 0.0, 0.0);
5470 filledrect (float x0) ph (float x1) (ph +. sh);
5471 let pw = pw +. float hx0 in
5472 filledrect pw (float (state.winh - sbh)) (pw +. sw) (float state.winh);
5475 let showsel () =
5476 match state.mstate with
5477 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5480 | Msel ((x0, y0), (x1, y1)) ->
5481 let identify opaque l px py = Some (opaque, l.pageno, px, py) in
5482 let o0,n0,px0,py0 = onppundermouse identify x0 y0 (~< E.s, -1, 0, 0) in
5483 let _o1,n1,px1,py1 = onppundermouse identify x1 y1 (~< E.s, -1, 0, 0) in
5484 if n0 != -1 && n0 = n1 then seltext o0 (px0, py0, px1, py1);
5487 let showrects = function [] -> () | rects ->
5488 Gl.enable `blend;
5489 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5490 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5491 List.iter
5492 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5493 List.iter (fun l ->
5494 if l.pageno = pageno
5495 then (
5496 let dx = float (l.pagedispx - l.pagex) in
5497 let dy = float (l.pagedispy - l.pagey) in
5498 let r, g, b, alpha = c in
5499 GlDraw.color (r, g, b) ~alpha;
5500 Raw.sets_float state.vraw ~pos:0
5501 [| x0+.dx; y0+.dy;
5502 x1+.dx; y1+.dy;
5503 x3+.dx; y3+.dy;
5504 x2+.dx; y2+.dy |];
5505 GlArray.vertex `two state.vraw;
5506 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
5508 ) state.layout
5509 ) rects
5511 Gl.disable `blend;
5514 let display () =
5515 GlClear.color (scalecolor2 conf.bgcolor);
5516 GlClear.clear [`color];
5517 List.iter drawpage state.layout;
5518 let rects =
5519 match state.mode with
5520 | LinkNav (Ltexact (pageno, linkno)) ->
5521 begin match getopaque pageno with
5522 | Some opaque ->
5523 let dx = xadjsb () in
5524 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5525 let x0 = x0 + dx and x1 = x1 + dx in
5526 let color = (0.0, 0.0, 0.5, 0.5) in
5527 (pageno, color, (
5528 float x0, float y0,
5529 float x1, float y0,
5530 float x1, float y1,
5531 float x0, float y1)
5532 ) :: state.rects
5533 | None -> state.rects
5535 | LinkNav (Ltgendir _) | LinkNav (Ltnotready _)
5536 | Birdseye _
5537 | Textentry _
5538 | View -> state.rects
5540 showrects rects;
5541 let rec postloop linkindexbase = function
5542 | l :: rest ->
5543 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5544 postloop linkindexbase rest
5545 | [] -> ()
5547 showsel ();
5548 postloop 0 state.layout;
5549 state.uioh#display;
5550 begin match state.mstate with
5551 | Mzoomrect ((x0, y0), (x1, y1)) ->
5552 Gl.enable `blend;
5553 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5554 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5555 filledrect (float x0) (float y0) (float x1) (float y1);
5556 Gl.disable `blend;
5557 | Msel _
5558 | Mpan _
5559 | Mscrolly | Mscrollx
5560 | Mzoom _
5561 | Mnone -> ()
5562 end;
5563 enttext ();
5564 scrollindicator ();
5565 Wsi.swapb ();
5568 let zoomrect x y x1 y1 =
5569 let x0 = min x x1
5570 and x1 = max x x1
5571 and y0 = min y y1 in
5572 gotoy (state.y + y0);
5573 state.anchor <- getanchor ();
5574 let zoom = (float state.w) /. float (x1 - x0) in
5575 let margin =
5576 let simple () =
5577 let adjw = wadjsb () + state.winw in
5578 if state.w < adjw
5579 then (adjw - state.w) / 2
5580 else 0
5582 match conf.fitmodel with
5583 | FitWidth | FitProportional -> simple ()
5584 | FitPage ->
5585 match conf.columns with
5586 | Csplit _ ->
5587 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
5588 | Cmulti _ | Csingle _ -> simple ()
5590 state.x <- (state.x + margin) - x0;
5591 setzoom zoom;
5592 resetmstate ();
5595 let annot inline x y =
5596 match unproject x y with
5597 | Some (opaque, n, ux, uy) ->
5598 let add text =
5599 addannot opaque ux uy text;
5600 wcmd "freepage %s" (~> opaque);
5601 Hashtbl.remove state.pagemap (n, state.gen);
5602 flushtiles ();
5603 gotoy state.y
5605 if inline
5606 then
5607 let ondone s = add s in
5608 let mode = state.mode in
5609 state.mode <- Textentry (
5610 ("annotation: ", E.s, None, textentry, ondone, true),
5611 fun _ -> state.mode <- mode);
5612 state.text <- E.s;
5613 enttext ();
5614 G.postRedisplay "annot"
5615 else
5616 add @@ getusertext E.s
5617 | _ -> ()
5620 let zoomblock x y =
5621 let g opaque l px py =
5622 match rectofblock opaque px py with
5623 | Some a ->
5624 let x0 = a.(0) -. 20. in
5625 let x1 = a.(1) +. 20. in
5626 let y0 = a.(2) -. 20. in
5627 let zoom = (float state.w) /. (x1 -. x0) in
5628 let pagey = getpagey l.pageno in
5629 gotoy_and_clear_text (pagey + truncate y0);
5630 state.anchor <- getanchor ();
5631 let margin = (state.w - l.pagew)/2 in
5632 state.x <- -truncate x0 - margin;
5633 setzoom zoom;
5634 None
5635 | None -> None
5637 match conf.columns with
5638 | Csplit _ ->
5639 impmsg "block zooming does not work properly in split columns mode"
5640 | Cmulti _ | Csingle _ -> onppundermouse g x y ()
5643 let scrollx x =
5644 let winw = wadjsb () + state.winw - 1 in
5645 let s = float x /. float winw in
5646 let destx = truncate (float (state.w + winw) *. s) in
5647 state.x <- winw - destx;
5648 gotoy_and_clear_text state.y;
5649 state.mstate <- Mscrollx;
5652 let scrolly y =
5653 let s = float y /. float state.winh in
5654 let desty = truncate (float (state.maxy - state.winh) *. s) in
5655 gotoy_and_clear_text desty;
5656 state.mstate <- Mscrolly;
5659 let viewmulticlick clicks x y mask =
5660 let g opaque l px py =
5661 let mark =
5662 match clicks with
5663 | 2 -> Mark_word
5664 | 3 -> Mark_line
5665 | 4 -> Mark_block
5666 | _ -> Mark_page
5668 if markunder opaque px py mark
5669 then (
5670 Some (fun () ->
5671 let dopipe cmd =
5672 match getopaque l.pageno with
5673 | None -> ()
5674 | Some opaque -> pipesel opaque cmd
5676 state.roam <- (fun () -> dopipe conf.paxcmd);
5677 if not (Wsi.withctrl mask) then dopipe conf.selcmd;
5680 else None
5682 G.postRedisplay "viewmulticlick";
5683 onppundermouse g x y (fun () -> impmsg "nothing to select") ();
5686 let canselect () =
5687 match conf.columns with
5688 | Csplit _ -> false
5689 | Csingle _ | Cmulti _ -> conf.angle mod 360 = 0
5692 let viewmouse button down x y mask =
5693 match button with
5694 | n when (n == 4 || n == 5) && not down ->
5695 if Wsi.withctrl mask
5696 then (
5697 match state.mstate with
5698 | Mzoom (oldn, i) ->
5699 if oldn = n
5700 then (
5701 if i = 2
5702 then
5703 let incr =
5704 match n with
5705 | 5 ->
5706 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5707 | _ ->
5708 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5710 let zoom = conf.zoom -. incr in
5711 setzoom zoom;
5712 state.mstate <- Mzoom (n, 0);
5713 else
5714 state.mstate <- Mzoom (n, i+1);
5716 else state.mstate <- Mzoom (n, 0)
5718 | Msel _
5719 | Mpan _
5720 | Mscrolly | Mscrollx
5721 | Mzoomrect _
5722 | Mnone -> state.mstate <- Mzoom (n, 0)
5724 else (
5725 match state.autoscroll with
5726 | Some step -> setautoscrollspeed step (n=4)
5727 | None ->
5728 if conf.wheelbypage || conf.presentation
5729 then (
5730 if n = 4
5731 then prevpage ()
5732 else nextpage ()
5734 else
5735 let incr =
5736 if n = 4
5737 then -conf.scrollstep
5738 else conf.scrollstep
5740 let incr = incr * 2 in
5741 let y = clamp incr in
5742 gotoy_and_clear_text y
5745 | n when (n = 6 || n = 7) && not down && canpan () ->
5746 state.x <-
5747 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
5748 gotoy_and_clear_text state.y
5750 | 1 when Wsi.withshift mask ->
5751 state.mstate <- Mnone;
5752 if not down
5753 then (
5754 match unproject x y with
5755 | None -> ()
5756 | Some (_, pageno, ux, uy) ->
5757 let cmd = Printf.sprintf
5758 "%s %s %d %d %d"
5759 conf.stcmd state.path pageno ux uy
5761 match spawn cmd [] with
5762 | (exception exn) ->
5763 impmsg "execution of synctex command(%S) failed: %S"
5764 conf.stcmd @@ exntos exn
5765 | _pid -> ()
5768 | 1 when Wsi.withctrl mask ->
5769 if down
5770 then (
5771 Wsi.setcursor Wsi.CURSOR_FLEUR;
5772 state.mstate <- Mpan (x, y)
5774 else
5775 state.mstate <- Mnone
5777 | 3 ->
5778 if down
5779 then (
5780 if Wsi.withshift mask
5781 then (
5782 annot conf.annotinline x y;
5783 G.postRedisplay "addannot"
5785 else
5786 let p = (x, y) in
5787 Wsi.setcursor Wsi.CURSOR_CYCLE;
5788 state.mstate <- Mzoomrect (p, p)
5790 else (
5791 match state.mstate with
5792 | Mzoomrect ((x0, y0), _) ->
5793 if abs (x-x0) > 10 && abs (y - y0) > 10
5794 then zoomrect x0 y0 x y
5795 else (
5796 resetmstate ();
5797 G.postRedisplay "kill accidental zoom rect";
5799 | Msel _
5800 | Mpan _
5801 | Mscrolly | Mscrollx
5802 | Mzoom _
5803 | Mnone ->
5804 resetmstate ()
5807 | 1 when vscrollhit x ->
5808 if down
5809 then
5810 let _, position, sh = state.uioh#scrollph in
5811 if y > truncate position && y < truncate (position +. sh)
5812 then state.mstate <- Mscrolly
5813 else scrolly y
5814 else
5815 state.mstate <- Mnone
5817 | 1 when y > state.winh - hscrollh () ->
5818 if down
5819 then
5820 let _, position, sw = state.uioh#scrollpw in
5821 if x > truncate position && x < truncate (position +. sw)
5822 then state.mstate <- Mscrollx
5823 else scrollx x
5824 else
5825 state.mstate <- Mnone
5827 | 1 when state.bzoom -> if not down then zoomblock x y
5829 | 1 ->
5830 let dest = if down then getunder x y else Unone in
5831 begin match dest with
5832 | Ulinkgoto _
5833 | Ulinkuri _
5834 | Uremote _ | Uremotedest _
5835 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5836 gotounder dest
5838 | Unone when down ->
5839 Wsi.setcursor Wsi.CURSOR_FLEUR;
5840 state.mstate <- Mpan (x, y);
5842 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
5844 | Unone | Utext _ ->
5845 if down
5846 then (
5847 if canselect ()
5848 then (
5849 state.mstate <- Msel ((x, y), (x, y));
5850 G.postRedisplay "mouse select";
5853 else (
5854 match state.mstate with
5855 | Mnone -> ()
5857 | Mzoom _ | Mscrollx | Mscrolly ->
5858 state.mstate <- Mnone
5860 | Mzoomrect ((x0, y0), _) ->
5861 zoomrect x0 y0 x y
5863 | Mpan _ ->
5864 Wsi.setcursor Wsi.CURSOR_INHERIT;
5865 state.mstate <- Mnone
5867 | Msel ((x0, y0), (x1, y1)) ->
5868 let rec loop = function
5869 | [] -> ()
5870 | l :: rest ->
5871 let inside =
5872 let a0 = l.pagedispy in
5873 let a1 = a0 + l.pagevh in
5874 let b0 = l.pagedispx in
5875 let b1 = b0 + l.pagevw in
5876 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5877 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5879 if inside
5880 then
5881 match getopaque l.pageno with
5882 | Some opaque ->
5883 let dosel cmd () =
5884 match Unix.pipe () with
5885 | (exception exn) ->
5886 impmsg "cannot create sel pipe: %s" @@
5887 exntos exn;
5888 | (r, w) ->
5889 let clo what fd =
5890 Ne.clo fd (fun msg ->
5891 dolog "%s close failed: %s" what msg)
5893 let pid =
5894 try spawn cmd [r, 0; w, -1]
5895 with exn ->
5896 dolog "cannot execute %S: %s"
5897 cmd @@ exntos exn;
5900 if pid > 0
5901 then (
5902 copysel w opaque;
5903 G.postRedisplay "copysel";
5905 else clo "Msel pipe/w" w;
5906 clo "Msel pipe/r" r;
5908 dosel conf.selcmd ();
5909 state.roam <- dosel conf.paxcmd;
5910 | None -> ()
5911 else loop rest
5913 loop state.layout;
5914 resetmstate ();
5918 | _ -> ()
5921 let birdseyemouse button down x y mask
5922 (conf, leftx, _, hooverpageno, anchor) =
5923 match button with
5924 | 1 when down ->
5925 let rec loop = function
5926 | [] -> ()
5927 | l :: rest ->
5928 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5929 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5930 then (
5931 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5933 else loop rest
5935 loop state.layout
5936 | 3 -> ()
5937 | _ -> viewmouse button down x y mask
5940 let uioh = object
5941 method display = ()
5943 method key key mask =
5944 begin match state.mode with
5945 | Textentry textentry -> textentrykeyboard key mask textentry
5946 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5947 | View -> viewkeyboard key mask
5948 | LinkNav linknav -> linknavkeyboard key mask linknav
5949 end;
5950 state.uioh
5952 method button button bstate x y mask =
5953 begin match state.mode with
5954 | LinkNav _
5955 | View -> viewmouse button bstate x y mask
5956 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5957 | Textentry _ -> ()
5958 end;
5959 state.uioh
5961 method multiclick clicks x y mask =
5962 begin match state.mode with
5963 | LinkNav _
5964 | View -> viewmulticlick clicks x y mask
5965 | Birdseye _
5966 | Textentry _ -> ()
5967 end;
5968 state.uioh
5970 method motion x y =
5971 begin match state.mode with
5972 | Textentry _ -> ()
5973 | View | Birdseye _ | LinkNav _ ->
5974 match state.mstate with
5975 | Mzoom _ | Mnone -> ()
5977 | Mpan (x0, y0) ->
5978 let dx = x - x0
5979 and dy = y0 - y in
5980 state.mstate <- Mpan (x, y);
5981 if canpan ()
5982 then state.x <- panbound (state.x + dx);
5983 let y = clamp dy in
5984 gotoy_and_clear_text y
5986 | Msel (a, _) ->
5987 state.mstate <- Msel (a, (x, y));
5988 G.postRedisplay "motion select";
5990 | Mscrolly ->
5991 let y = min state.winh (max 0 y) in
5992 scrolly y
5994 | Mscrollx ->
5995 let x = min state.winw (max 0 x) in
5996 scrollx x
5998 | Mzoomrect (p0, _) ->
5999 state.mstate <- Mzoomrect (p0, (x, y));
6000 G.postRedisplay "motion zoomrect";
6001 end;
6002 state.uioh
6004 method pmotion x y =
6005 begin match state.mode with
6006 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6007 let rec loop = function
6008 | [] ->
6009 if hooverpageno != -1
6010 then (
6011 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6012 G.postRedisplay "pmotion birdseye no hoover";
6014 | l :: rest ->
6015 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6016 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6017 then (
6018 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6019 G.postRedisplay "pmotion birdseye hoover";
6021 else loop rest
6023 loop state.layout
6025 | Textentry _ -> ()
6027 | LinkNav _
6028 | View ->
6029 match state.mstate with
6030 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ -> ()
6031 | Mnone ->
6032 updateunder x y;
6033 if canselect ()
6034 then
6035 match conf.pax with
6036 | None -> ()
6037 | Some r ->
6038 let past, _, _ = !r in
6039 let now = now () in
6040 let delta = now -. past in
6041 if delta > 0.01
6042 then paxunder x y
6043 else r := (now, x, y)
6044 end;
6045 state.uioh
6047 method infochanged _ = ()
6049 method scrollph =
6050 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6051 let p, h =
6052 if maxy = 0
6053 then 0.0, float state.winh
6054 else scrollph state.y maxy
6056 vscrollw (), p, h
6058 method scrollpw =
6059 let winw = wadjsb () + state.winw in
6060 let fwinw = float winw in
6061 let sw =
6062 let sw = fwinw /. float state.w in
6063 let sw = fwinw *. sw in
6064 max sw (float conf.scrollh)
6066 let position =
6067 let maxx = state.w + winw in
6068 let x = winw - state.x in
6069 let percent = float x /. float maxx in
6070 (fwinw -. sw) *. percent
6072 hscrollh (), position, sw
6074 method modehash =
6075 let modename =
6076 match state.mode with
6077 | LinkNav _ -> "links"
6078 | Textentry _ -> "textentry"
6079 | Birdseye _ -> "birdseye"
6080 | View -> "view"
6082 findkeyhash conf modename
6084 method eformsgs = true
6085 method alwaysscrolly = false
6086 end;;
6088 let adderrmsg src msg =
6089 Buffer.add_string state.errmsgs msg;
6090 state.newerrmsgs <- true;
6091 G.postRedisplay src
6094 let adderrfmt src fmt =
6095 Format.ksprintf (fun s -> adderrmsg src s) fmt;
6098 let addrect pageno r g b a x0 y0 x1 y1 =
6099 Hashtbl.add state.prects pageno [|r; g; b; a; x0; y0; x1; y1|];
6102 let ract cmds =
6103 let cl = splitatspace cmds in
6104 let scan s fmt f =
6105 try Scanf.sscanf s fmt f
6106 with exn ->
6107 adderrfmt "remote exec"
6108 "error processing '%S': %s\n" cmds @@ exntos exn
6110 let rectx s pageno (r, g, b, a) x0 y0 x1 y1 =
6111 vlog "%s page %d color (%f %f %f %f) x0,y0,x1,y1 = %f %f %f %f"
6112 s pageno r g b a x0 y0 x1 y1;
6113 onpagerect
6114 pageno
6115 (fun w h ->
6116 let _,w1,h1,_ = getpagedim pageno in
6117 let sw = float w1 /. float w
6118 and sh = float h1 /. float h in
6119 let x0s = x0 *. sw
6120 and x1s = x1 *. sw
6121 and y0s = y0 *. sh
6122 and y1s = y1 *. sh in
6123 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
6124 let color = (r, g, b, a) in
6125 if conf.verbose then debugrect rect;
6126 state.rects <- (pageno, color, rect) :: state.rects;
6127 G.postRedisplay s;
6130 match cl with
6131 | "reload" :: [] -> reload ()
6132 | "goto" :: args :: [] ->
6133 scan args "%u %f %f"
6134 (fun pageno x y ->
6135 let cmd, _ = state.geomcmds in
6136 if emptystr cmd
6137 then gotopagexy pageno x y
6138 else
6139 let f prevf () =
6140 gotopagexy pageno x y;
6141 prevf ()
6143 state.reprf <- f state.reprf
6145 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
6146 | "gotor" :: args :: [] ->
6147 scan args "%S %u"
6148 (fun filename pageno -> gotounder (Uremote (filename, pageno)))
6149 | "gotord" :: args :: [] ->
6150 scan args "%S %S"
6151 (fun filename dest -> gotounder (Uremotedest (filename, dest)))
6152 | "rect" :: args :: [] ->
6153 scan args "%u %u %f %f %f %f"
6154 (fun pageno c x0 y0 x1 y1 ->
6155 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
6156 rectx "rect" pageno color x0 y0 x1 y1;
6158 | "prect" :: args :: [] ->
6159 scan args "%u %f %f %f %f %f %f %f %f"
6160 (fun pageno r g b alpha x0 y0 x1 y1 ->
6161 addrect pageno r g b alpha x0 y0 x1 y1;
6162 G.postRedisplay "prect"
6164 | "pgoto" :: args :: [] ->
6165 scan args "%u %f %f"
6166 (fun pageno x y ->
6167 let optopaque =
6168 match getopaque pageno with
6169 | Some opaque -> opaque
6170 | None -> ~< ""
6172 pgoto optopaque pageno x y;
6173 let rec fixx = function
6174 | [] -> ()
6175 | l :: rest ->
6176 if l.pageno = pageno
6177 then (
6178 state.x <- state.x - l.pagedispx;
6179 gotoy state.y;
6181 else fixx rest
6183 let layout =
6184 let mult =
6185 match conf.columns with
6186 | Csingle _ | Csplit _ -> 1
6187 | Cmulti ((n, _, _), _) -> n
6189 layout 0 state.y (state.winw * mult) state.winh
6191 fixx layout
6193 | "activatewin" :: [] -> Wsi.activatewin ()
6194 | "quit" :: [] -> raise Quit
6195 | "clearrects" :: [] ->
6196 Hashtbl.clear state.prects;
6197 G.postRedisplay "clearrects"
6198 | _ ->
6199 adderrfmt "remote command"
6200 "error processing remote command: %S\n" cmds;
6203 let remote =
6204 let scratch = Bytes.create 80 in
6205 let buf = Buffer.create 80 in
6206 fun fd ->
6207 match tempfailureretry (Unix.read fd scratch 0) 80 with
6208 | (exception Unix.Unix_error (Unix.EAGAIN, _, _)) -> None
6209 | 0 ->
6210 Unix.close fd;
6211 if Buffer.length buf > 0
6212 then (
6213 let s = Buffer.contents buf in
6214 Buffer.clear buf;
6215 ract s;
6217 None
6218 | n ->
6219 let rec eat ppos =
6220 let nlpos =
6221 match Bytes.index_from scratch ppos '\n' with
6222 | pos -> if pos >= n then -1 else pos
6223 | (exception Not_found) -> -1
6225 if nlpos >= 0
6226 then (
6227 Buffer.add_subbytes buf scratch ppos (nlpos-ppos);
6228 let s = Buffer.contents buf in
6229 Buffer.clear buf;
6230 ract s;
6231 eat (nlpos+1);
6233 else (
6234 Buffer.add_subbytes buf scratch ppos (n-ppos);
6235 Some fd
6237 in eat 0
6240 let remoteopen path =
6241 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
6242 with exn ->
6243 adderrfmt "remoteopen" "error opening %S: %s" path @@ exntos exn;
6244 None
6247 let () =
6248 let gcconfig = ref E.s in
6249 let trimcachepath = ref E.s in
6250 let rcmdpath = ref E.s in
6251 let pageno = ref None in
6252 let rootwid = ref 0 in
6253 let openlast = ref false in
6254 let nofc = ref false in
6255 let doreap = ref false in
6256 selfexec := Sys.executable_name;
6257 Arg.parse
6258 (Arg.align
6259 [("-p", Arg.String (fun s -> state.password <- s),
6260 "<password> Set password");
6262 ("-f", Arg.String
6263 (fun s ->
6264 Config.fontpath := s;
6265 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
6267 "<path> Set path to the user interface font");
6269 ("-c", Arg.String
6270 (fun s ->
6271 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
6272 Config.confpath := s),
6273 "<path> Set path to the configuration file");
6275 ("-last", Arg.Set openlast, " Open last document");
6277 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
6278 "<page-number> Jump to page");
6280 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6281 "<path> Set path to the trim cache file");
6283 ("-dest", Arg.String (fun s -> state.nameddest <- s),
6284 "<named-destination> Set named destination");
6286 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
6287 ("-cxack", Arg.Set cxack, " Cut corners");
6289 ("-remote", Arg.String (fun s -> rcmdpath := s),
6290 "<path> Set path to the remote commands source");
6292 ("-origin", Arg.String (fun s -> state.origin <- s),
6293 "<original-path> Set original path");
6295 ("-gc", Arg.Set_string gcconfig,
6296 "<script-path> Collect garbage with the help of a script");
6298 ("-nofc", Arg.Set nofc, " Do not use fontconfig");
6300 ("-v", Arg.Unit (fun () ->
6301 Printf.printf
6302 "%s\nconfiguration path: %s\n"
6303 (version ())
6304 Config.defconfpath
6306 exit 0), " Print version and exit");
6308 ("-embed", Arg.Set_int rootwid,
6309 "<window-id> Embed into window")
6312 (fun s -> state.path <- s)
6313 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6315 if !wtmode
6316 then selfexec := !selfexec ^ " -wtmode";
6318 let histmode = emptystr state.path && not !openlast in
6320 if not (Config.load !openlast)
6321 then dolog "failed to load configuration";
6322 begin match !pageno with
6323 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
6324 | None -> ()
6325 end;
6327 if nonemptystr !gcconfig
6328 then (
6329 let (c, s) =
6330 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6331 | (exception exn) -> error "socketpair for gc failed: %s" @@ exntos exn
6332 | fds -> fds
6334 match spawn !gcconfig [(c, 0); (c, 1); (s, -1)] with
6335 | (exception exn) -> error "failed to execute gc script: %s" @@ exntos exn
6336 | _pid ->
6337 Ne.clo c @@ (fun s -> error "failed to close gc fd %s" s);
6338 Config.gc s;
6339 exit 0
6342 let wsfd, winw, winh = Wsi.init (object (self)
6343 val mutable m_clicks = 0
6344 val mutable m_click_x = 0
6345 val mutable m_click_y = 0
6346 val mutable m_lastclicktime = infinity
6348 method private cleanup =
6349 state.roam <- noroam;
6350 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap
6351 method expose = G.postRedisplay"expose"
6352 method visible v =
6353 let name =
6354 match v with
6355 | Wsi.Unobscured -> "unobscured"
6356 | Wsi.PartiallyObscured -> "partiallyobscured"
6357 | Wsi.FullyObscured -> "fullyobscured"
6359 vlog "visibility change %s" name
6360 method display = display ()
6361 method map mapped = vlog "mappped %b" mapped
6362 method reshape w h =
6363 self#cleanup;
6364 reshape w h
6365 method mouse b d x y m =
6366 if d && canselect ()
6367 then (
6368 (* http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx *)
6369 m_click_x <- x;
6370 m_click_y <- y;
6371 if b = 1
6372 then (
6373 let t = now () in
6374 if abs x - m_click_x > 10
6375 || abs y - m_click_y > 10
6376 || abs_float (t -. m_lastclicktime) > 0.3
6377 then m_clicks <- 0;
6378 m_clicks <- m_clicks + 1;
6379 m_lastclicktime <- t;
6380 if m_clicks = 1
6381 then (
6382 self#cleanup;
6383 G.postRedisplay "cleanup";
6384 state.uioh <- state.uioh#button b d x y m;
6386 else state.uioh <- state.uioh#multiclick m_clicks x y m
6388 else (
6389 self#cleanup;
6390 m_clicks <- 0;
6391 m_lastclicktime <- infinity;
6392 state.uioh <- state.uioh#button b d x y m
6395 else (
6396 state.uioh <- state.uioh#button b d x y m
6398 method motion x y =
6399 state.mpos <- (x, y);
6400 state.uioh <- state.uioh#motion x y
6401 method pmotion x y =
6402 state.mpos <- (x, y);
6403 state.uioh <- state.uioh#pmotion x y
6404 method key k m =
6405 let mascm = m land (
6406 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6407 ) in
6408 let keyboard k m =
6409 let x = state.x and y = state.y in
6410 keyboard k m;
6411 if x != state.x || y != state.y then self#cleanup
6413 match state.keystate with
6414 | KSnone ->
6415 let km = k, mascm in
6416 begin
6417 match
6418 let modehash = state.uioh#modehash in
6419 try Hashtbl.find modehash km
6420 with Not_found ->
6421 try Hashtbl.find (findkeyhash conf "global") km
6422 with Not_found -> KMinsrt (k, m)
6423 with
6424 | KMinsrt (k, m) -> keyboard k m
6425 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6426 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6428 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6429 List.iter (fun (k, m) -> keyboard k m) insrt;
6430 state.keystate <- KSnone
6431 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6432 state.keystate <- KSinto (keys, insrt)
6433 | KSinto _ -> state.keystate <- KSnone
6435 method enter x y =
6436 state.mpos <- (x, y);
6437 state.uioh <- state.uioh#pmotion x y
6438 method leave = state.mpos <- (-1, -1)
6439 method winstate wsl = state.winstate <- wsl
6440 method quit = raise Quit
6441 end) !rootwid conf.cwinw conf.cwinh platform in
6443 state.wsfd <- wsfd;
6445 if not (
6446 List.exists GlMisc.check_extension
6447 [ "GL_ARB_texture_rectangle"
6448 ; "GL_EXT_texture_recangle"
6449 ; "GL_NV_texture_rectangle" ]
6451 then (dolog "OpenGL does not suppport rectangular textures"; exit 1);
6453 if (
6454 let r = GlMisc.get_string `renderer in
6455 let p = "Mesa DRI Intel(" in
6456 let l = String.length p in
6457 String.length r > l && String.sub r 0 l = p
6459 then (
6460 defconf.sliceheight <- 1024;
6461 defconf.texcount <- 32;
6462 defconf.usepbo <- true;
6465 let cs, ss =
6466 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6467 | (exception exn) ->
6468 dolog "socketpair failed: %s" @@ exntos exn;
6469 exit 1
6470 | (r, w) ->
6471 cloexec r;
6472 cloexec w;
6473 r, w
6476 setcheckers conf.checkers;
6478 init cs (
6479 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
6480 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6481 !Config.fontpath, !trimcachepath,
6482 GlMisc.check_extension "GL_ARB_pixel_buffer_object",
6483 not !nofc
6485 List.iter GlArray.enable [`texture_coord; `vertex];
6486 state.ss <- ss;
6487 reshape ~firsttime:true winw winh;
6488 state.uioh <- uioh;
6489 if histmode
6490 then (
6491 Wsi.settitle "llpp (history)";
6492 enterhistmode ();
6494 else (
6495 state.text <- "Opening " ^ (mbtoutf8 state.path);
6496 opendoc state.path state.password;
6498 display ();
6499 Wsi.mapwin ();
6500 Wsi.setcursor Wsi.CURSOR_INHERIT;
6501 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
6503 let rec reap () =
6504 match Unix.waitpid [Unix.WNOHANG] ~-1 with
6505 | (exception (Unix.Unix_error (Unix.ECHILD, _, _))) -> ()
6506 | (exception exn) -> dolog "Unix.waitpid: %s" @@ exntos exn
6507 | 0, _ -> ()
6508 | _pid, _status -> reap ()
6510 Sys.set_signal Sys.sigchld (Sys.Signal_handle (fun _ -> doreap := true));
6512 let optrfd =
6513 ref (
6514 if nonemptystr !rcmdpath
6515 then remoteopen !rcmdpath
6516 else None
6520 let rec loop deadline =
6521 if !doreap
6522 then (
6523 doreap := false;
6524 reap ()
6526 let r = [state.ss; state.wsfd] in
6527 let r =
6528 match !optrfd with
6529 | None -> r
6530 | Some fd -> fd :: r
6532 if state.redisplay
6533 then (
6534 state.redisplay <- false;
6535 display ();
6537 let timeout =
6538 let now = now () in
6539 if deadline > now
6540 then (
6541 if deadline = infinity
6542 then ~-.1.0
6543 else max 0.0 (deadline -. now)
6545 else 0.0
6547 let r, _, _ =
6548 try Unix.select r [] [] timeout
6549 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6551 begin match r with
6552 | [] ->
6553 state.ghyll None;
6554 let newdeadline =
6555 if state.ghyll == noghyll
6556 then
6557 match state.autoscroll with
6558 | Some step when step != 0 ->
6559 let y = state.y + step in
6560 let y =
6561 if y < 0
6562 then state.maxy
6563 else if y >= state.maxy then 0 else y
6565 if state.mode = View
6566 then gotoy_and_clear_text y
6567 else gotoy y;
6568 deadline +. 0.01
6569 | _ -> infinity
6570 else deadline +. 0.01
6572 loop newdeadline
6574 | l ->
6575 let rec checkfds = function
6576 | [] -> ()
6577 | fd :: rest when fd = state.ss ->
6578 let cmd = readcmd state.ss in
6579 act cmd;
6580 checkfds rest
6582 | fd :: rest when fd = state.wsfd ->
6583 Wsi.readresp fd;
6584 checkfds rest
6586 | fd :: rest when Some fd = !optrfd ->
6587 begin match remote fd with
6588 | None -> optrfd := remoteopen !rcmdpath;
6589 | opt -> optrfd := opt
6590 end;
6591 checkfds rest
6593 | _ :: rest ->
6594 dolog "select returned unknown descriptor";
6595 checkfds rest
6597 checkfds l;
6598 let newdeadline =
6599 let deadline1 =
6600 if deadline = infinity
6601 then now () +. 0.01
6602 else deadline
6604 match state.autoscroll with
6605 | Some step when step != 0 -> deadline1
6606 | _ -> if state.ghyll == noghyll then infinity else deadline1
6608 loop newdeadline
6609 end;
6612 loop infinity;
6613 with Quit ->
6614 Config.save leavebirdseye;
6615 if hasunsavedchanges ()
6616 then save ();