build.sh: respect $CFLAGS
[llpp.git] / main.ml
blob25800b7cfa647c53b8746fe337b46ca47a5b3c22
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 getmaxw : unit -> float = "ml_getmaxw";;
16 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
17 external measurestr : int -> string -> float = "ml_measure_string";;
18 external postprocess :
19 opaque -> int -> int -> int -> (int * string * int) -> int
20 = "ml_postprocess";;
21 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
22 external setaalevel : int -> unit = "ml_setaalevel";;
23 external realloctexts : int -> bool = "ml_realloctexts";;
24 external findlink : opaque -> linkdir -> link = "ml_findlink";;
25 external getlink : opaque -> int -> under = "ml_getlink";;
26 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
27 external getlinkcount : opaque -> int = "ml_getlinkcount";;
28 external findpwl : int -> int -> pagewithlinks = "ml_find_page_with_links";;
29 external getpbo : width -> height -> colorspace -> opaque = "ml_getpbo";;
30 external freepbo : opaque -> unit = "ml_freepbo";;
31 external unmappbo : opaque -> unit = "ml_unmappbo";;
32 external bousable : unit -> bool = "ml_bo_usable";;
33 external unproject : opaque -> int -> int -> (int * int) option
34 = "ml_unproject";;
35 external project : opaque -> int -> int -> float -> float -> (float * float)
36 = "ml_project";;
37 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
38 external rectofblock : opaque -> int -> int -> float array option
39 = "ml_rectofblock";;
40 external begintiles : unit -> unit = "ml_begintiles";;
41 external endtiles : unit -> unit = "ml_endtiles";;
42 external addannot : opaque -> int -> int -> string -> unit = "ml_addannot";;
43 external modannot : opaque -> slinkindex -> string -> unit = "ml_modannot";;
44 external delannot : opaque -> slinkindex -> unit = "ml_delannot";;
45 external hasunsavedchanges : unit -> bool = "ml_hasunsavedchanges";;
46 external savedoc : string -> unit = "ml_savedoc";;
47 external getannotcontents : opaque -> slinkindex -> string
48 = "ml_getannotcontents";;
49 external drawprect : opaque -> int -> int -> float array -> unit =
50 "ml_drawprect";;
52 let selfexec = ref E.s;;
53 let opengl_has_pbo = ref false;;
55 let drawstring size x y s =
56 Gl.enable `blend;
57 Gl.enable `texture_2d;
58 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
59 ignore (drawstr size x y s);
60 Gl.disable `blend;
61 Gl.disable `texture_2d;
64 let drawstring1 size x y s =
65 drawstr size x y s;
68 let drawstring2 size x y fmt =
69 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
72 let _debugl l =
73 dolog "l %d dim=%d {" l.pageno l.pagedimno;
74 dolog " WxH %dx%d" l.pagew l.pageh;
75 dolog " vWxH %dx%d" l.pagevw l.pagevh;
76 dolog " pagex,y %d,%d" l.pagex l.pagey;
77 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
78 dolog " column %d" l.pagecol;
79 dolog "}";
82 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
83 dolog "rect {";
84 dolog " x0,y0=(% f, % f)" x0 y0;
85 dolog " x1,y1=(% f, % f)" x1 y1;
86 dolog " x2,y2=(% f, % f)" x2 y2;
87 dolog " x3,y3=(% f, % f)" x3 y3;
88 dolog "}";
91 let isbirdseye = function
92 | Birdseye _ -> true
93 | Textentry _
94 | View
95 | LinkNav _ -> false
98 let istextentry = function
99 | Textentry _ -> true
100 | Birdseye _
101 | View
102 | LinkNav _ -> false
105 let wtmode = ref false;;
106 let cxack = ref false;;
108 let pgscale h = truncate (float h *. conf.pgscale);;
110 let hscrollh () =
111 if not state.uioh#alwaysscrolly && (conf.scrollb land scrollbhv = 0)
112 || (state.x = 0 && state.w <= state.winw - conf.scrollbw)
113 then 0
114 else conf.scrollbw
117 let vscrollw () =
118 if not state.uioh#alwaysscrolly && (conf.scrollb land scrollbvv = 0)
119 then 0
120 else conf.scrollbw
123 let vscrollhit x =
124 if conf.leftscroll
125 then x < vscrollw ()
126 else x > state.winw - vscrollw ()
129 let wadjsb () = -vscrollw ();;
130 let xadjsb () = if conf.leftscroll then vscrollw () else 0;;
132 let setfontsize n =
133 fstate.fontsize <- n;
134 fstate.wwidth <- measurestr fstate.fontsize "w";
135 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
138 let vlog fmt =
139 if conf.verbose
140 then dolog fmt
141 else Printf.kprintf ignore fmt
144 let launchpath () =
145 if emptystr conf.pathlauncher
146 then dolog "%s" state.path
147 else (
148 let command = Str.global_replace percentsre state.path conf.pathlauncher in
149 match spawn command [] with
150 | _pid -> ()
151 | (exception exn) ->
152 dolog "failed to execute `%s': %s" command @@ exntos exn
156 module G =
157 struct
158 let postRedisplay who =
159 vlog "redisplay for [%S]" who;
160 state.redisplay <- true;
162 end;;
164 let getopaque pageno =
165 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
166 with Not_found -> None
169 let pagetranslatepoint l x y =
170 let dy = y - l.pagedispy in
171 let y = dy + l.pagey in
172 let dx = x - l.pagedispx in
173 let x = dx + l.pagex in
174 (x, y);
177 let onppundermouse g x y d =
178 let rec f = function
179 | l :: rest ->
180 begin match getopaque l.pageno with
181 | Some opaque ->
182 let x0 = l.pagedispx in
183 let x1 = x0 + l.pagevw in
184 let y0 = l.pagedispy in
185 let y1 = y0 + l.pagevh in
186 if y >= y0 && y <= y1 && x >= x0 && x <= x1
187 then
188 let px, py = pagetranslatepoint l x y in
189 match g opaque l px py with
190 | Some res -> res
191 | None -> f rest
192 else f rest
193 | _ ->
194 f rest
196 | [] -> d
198 f state.layout
201 let getunder x y =
202 let g opaque l px py =
203 if state.bzoom
204 then (
205 match rectofblock opaque px py with
206 | Some [|x0;x1;y0;y1|] ->
207 let ox = xadjsb () |> float in
208 let rect = (x0+.ox, y0, x1+.ox, y0, x1+.ox, y1, x0+.ox, y1) in
209 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
210 state.rects <- [l.pageno, color, rect];
211 G.postRedisplay "getunder";
212 | _otherwise -> ()
214 let under = whatsunder opaque px py in
215 if under = Unone then None else Some under
217 onppundermouse g x y Unone
220 let unproject x y =
221 let g opaque l x y =
222 match unproject opaque x y with
223 | Some (x, y) -> Some (Some (opaque, l.pageno, x, y))
224 | None -> None
226 onppundermouse g x y None;
229 let showtext c s =
230 state.text <- Printf.sprintf "%c%s" c s;
231 G.postRedisplay "showtext";
234 let impmsg fmt =
235 Format.ksprintf (fun s -> showtext '!' s) fmt;
238 let pipesel opaque cmd =
239 if hassel opaque
240 then
241 match Unix.pipe () with
242 | (exception exn) -> dolog "pipesel cannot create pipe: %S" @@ exntos exn;
243 | (r, w) ->
244 let doclose what fd =
245 Ne.clo fd (fun msg -> dolog "%s close failed: %s" what msg)
247 let pid =
248 try spawn cmd [r, 0; w, -1]
249 with exn ->
250 dolog "cannot execute %S: %s" cmd @@ exntos exn;
253 if pid > 0
254 then (
255 copysel w opaque;
256 G.postRedisplay "pipesel";
258 else doclose "pipesel pipe/w" w;
259 doclose "pipesel pipe/r" r;
262 let paxunder x y =
263 let g opaque l px py =
264 if markunder opaque px py conf.paxmark
265 then (
266 Some (fun () ->
267 match getopaque l.pageno with
268 | None -> ()
269 | Some opaque -> pipesel opaque conf.paxcmd
272 else None
274 G.postRedisplay "paxunder";
275 if conf.paxmark = Mark_page
276 then
277 List.iter (fun l ->
278 match getopaque l.pageno with
279 | None -> ()
280 | Some opaque -> clearmark opaque) state.layout;
281 state.roam <- onppundermouse g x y (fun () -> impmsg "whoopsie daisy");
284 let selstring s =
285 match Unix.pipe () with
286 | (exception exn) -> impmsg "pipe failed: %s" @@ exntos exn
287 | (r, w) ->
288 let clo cap fd =
289 Ne.clo fd (fun msg -> impmsg "failed to close %s: %s" cap msg)
291 let pid =
292 try spawn conf.selcmd [r, 0; w, -1]
293 with exn ->
294 impmsg "failed to execute %s: %s" conf.selcmd @@ exntos exn;
297 if pid > 0
298 then (
300 let l = String.length s in
301 let bytes = Bytes.unsafe_of_string s in
302 let n = tempfailureretry (Unix.write w bytes 0) l in
303 if n != l
304 then impmsg "failed to write %d characters to sel pipe, wrote %d"
306 with exn ->
307 impmsg "failed to write to sel pipe: %s" @@ exntos exn
309 else dolog "%s" s;
310 clo "selstring pipe/r" r;
311 clo "selstring pipe/w" w;
314 let undertext ?(nopath=false) = function
315 | Unone -> "none"
316 | Ulinkuri s -> s
317 | Ulinkgoto (pageno, _) ->
318 if nopath
319 then "page " ^ string_of_int (pageno+1)
320 else Printf.sprintf "%s: page %d" state.path (pageno+1)
321 | Utext s -> "font: " ^ s
322 | Uunexpected s -> "unexpected: " ^ s
323 | Ulaunch s -> "launch: " ^ s
324 | Unamed s -> "named: " ^ s
325 | Uremote (filename, pageno) ->
326 Printf.sprintf "%s: page %d" filename (pageno+1)
327 | Uremotedest (filename, destname) ->
328 Printf.sprintf "%s: destination %S" filename destname
329 | Uannotation (opaque, slinkindex) ->
330 "annotation: " ^ getannotcontents opaque slinkindex
333 let updateunder x y =
334 match getunder x y with
335 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
336 | Ulinkuri uri ->
337 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
338 Wsi.setcursor Wsi.CURSOR_INFO
339 | Ulinkgoto (pageno, _) ->
340 if conf.underinfo
341 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
342 Wsi.setcursor Wsi.CURSOR_INFO
343 | Utext s ->
344 if conf.underinfo then showtext 'f' ("ont: " ^ s);
345 Wsi.setcursor Wsi.CURSOR_TEXT
346 | Uunexpected s ->
347 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
348 Wsi.setcursor Wsi.CURSOR_INHERIT
349 | Ulaunch s ->
350 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
351 Wsi.setcursor Wsi.CURSOR_INHERIT
352 | Unamed s ->
353 if conf.underinfo then showtext 'n' ("amed: " ^ s);
354 Wsi.setcursor Wsi.CURSOR_INHERIT
355 | Uremote (filename, pageno) ->
356 if conf.underinfo then showtext 'r'
357 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
358 Wsi.setcursor Wsi.CURSOR_INFO
359 | Uremotedest (filename, destname) ->
360 if conf.underinfo then showtext 'r'
361 (Printf.sprintf "emote destination: %s (%S)" filename destname);
362 Wsi.setcursor Wsi.CURSOR_INFO
363 | Uannotation _ ->
364 if conf.underinfo then showtext 'a' "nnotation";
365 Wsi.setcursor Wsi.CURSOR_INFO
368 let showlinktype under =
369 if conf.underinfo && under != Unone
370 then showtext ' ' @@ undertext under
373 let intentry_with_suffix text key =
374 let c =
375 if key >= 32 && key < 127
376 then Char.chr key
377 else '\000'
379 match Char.lowercase c with
380 | '0' .. '9' ->
381 let text = addchar text c in
382 TEcont text
384 | 'k' | 'm' | 'g' ->
385 let text = addchar text c in
386 TEcont text
388 | _ ->
389 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
390 TEcont text
393 let readcmd fd =
394 let s = Bytes.create 4 in
395 let n = tempfailureretry (Unix.read fd s 0) 4 in
396 if n != 4 then error "incomplete read(len) = %d" n;
397 let len = (Char.code (Bytes.get s 0) lsl 24)
398 lor (Char.code (Bytes.get s 1) lsl 16)
399 lor (Char.code (Bytes.get s 2) lsl 8)
400 lor (Char.code (Bytes.get s 3))
402 let s = Bytes.create len in
403 let n = tempfailureretry (Unix.read fd s 0) len in
404 if n != len then error "incomplete read(data) %d vs %d" n len;
405 Bytes.to_string s
408 let wcmd fmt =
409 let b = Buffer.create 16 in
410 Buffer.add_string b "llll";
411 Printf.kbprintf
412 (fun b ->
413 let s = Buffer.to_bytes b in
414 let n = Bytes.length s in
415 let len = n - 4 in
416 (* dolog "wcmd %S" (String.sub s 4 len); *)
417 Bytes.set s 0 (Char.chr ((len lsr 24) land 0xff));
418 Bytes.set s 1 (Char.chr ((len lsr 16) land 0xff));
419 Bytes.set s 2 (Char.chr ((len lsr 8) land 0xff));
420 Bytes.set s 3 (Char.chr (len land 0xff));
421 let n' = tempfailureretry (Unix.write state.ss s 0) n in
422 if n' != n then error "write failed %d vs %d" n' n;
423 ) b fmt;
426 let nogeomcmds cmds =
427 match cmds with
428 | s, [] -> emptystr s
429 | _ -> false
432 let layoutN ((columns, coverA, coverB), b) x y sw sh =
433 let sh = sh - (hscrollh ()) in
434 let wadj = wadjsb () in
435 let rec fold accu n =
436 if n = Array.length b
437 then accu
438 else
439 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
440 if (vy - y) > sh &&
441 (n = coverA - 1
442 || n = state.pagecount - coverB
443 || (n - coverA) mod columns = columns - 1)
444 then accu
445 else
446 let accu =
447 if vy + h > y
448 then
449 let pagey = max 0 (y - vy) in
450 let pagedispy = if pagey > 0 then 0 else vy - y in
451 let pagedispx, pagex =
452 let pdx =
453 if n = coverA - 1 || n = state.pagecount - coverB
454 then x + (wadj + sw - w) / 2
455 else dx + xoff + x
457 if pdx < 0
458 then 0, -pdx
459 else pdx, 0
461 let pagevw =
462 let vw = wadj + sw - pagedispx in
463 let pw = w - pagex in
464 min vw pw
466 let pagevh = min (h - pagey) (sh - pagedispy) in
467 if pagevw > 0 && pagevh > 0
468 then
469 let e =
470 { pageno = n
471 ; pagedimno = pdimno
472 ; pagew = w
473 ; pageh = h
474 ; pagex = pagex
475 ; pagey = pagey
476 ; pagevw = pagevw
477 ; pagevh = pagevh
478 ; pagedispx = pagedispx
479 ; pagedispy = pagedispy
480 ; pagecol = 0
483 e :: accu
484 else
485 accu
486 else
487 accu
489 fold accu (n+1)
491 if Array.length b = 0
492 then []
493 else List.rev (fold [] (page_of_y y))
496 let layoutS (columns, b) x y sw sh =
497 let sh = sh - hscrollh () in
498 let wadj = wadjsb () in
499 let rec fold accu n =
500 if n = Array.length b
501 then accu
502 else
503 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
504 if (vy - y) > sh
505 then accu
506 else
507 let accu =
508 if vy + pageh > y
509 then
510 let x = xoff + x in
511 let pagey = max 0 (y - vy) in
512 let pagedispy = if pagey > 0 then 0 else vy - y in
513 let pagedispx, pagex =
514 if px = 0
515 then (
516 if x < 0
517 then 0, -x
518 else x, 0
520 else (
521 let px = px - x in
522 if px < 0
523 then -px, 0
524 else 0, px
527 let pagecolw = pagew/columns in
528 let pagedispx =
529 if pagecolw < sw
530 then pagedispx + ((wadj + sw - pagecolw) / 2)
531 else pagedispx
533 let pagevw =
534 let vw = wadj + sw - pagedispx in
535 let pw = pagew - pagex in
536 min vw pw
538 let pagevw = min pagevw pagecolw in
539 let pagevh = min (pageh - pagey) (sh - pagedispy) in
540 if pagevw > 0 && pagevh > 0
541 then
542 let e =
543 { pageno = n/columns
544 ; pagedimno = pdimno
545 ; pagew = pagew
546 ; pageh = pageh
547 ; pagex = pagex
548 ; pagey = pagey
549 ; pagevw = pagevw
550 ; pagevh = pagevh
551 ; pagedispx = pagedispx
552 ; pagedispy = pagedispy
553 ; pagecol = n mod columns
556 e :: accu
557 else
558 accu
559 else
560 accu
562 fold accu (n+1)
564 List.rev (fold [] 0)
567 let layout x y sw sh =
568 if nogeomcmds state.geomcmds
569 then
570 match conf.columns with
571 | Csingle b -> layoutN ((1, 0, 0), b) x y sw sh
572 | Cmulti c -> layoutN c x y sw sh
573 | Csplit s -> layoutS s x y sw sh
574 else []
577 let clamp incr =
578 let y = state.y + incr in
579 let y = max 0 y in
580 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
584 let itertiles l f =
585 let tilex = l.pagex mod conf.tilew in
586 let tiley = l.pagey mod conf.tileh in
588 let col = l.pagex / conf.tilew in
589 let row = l.pagey / conf.tileh in
591 let xadj = xadjsb () in
592 let rec rowloop row y0 dispy h =
593 if h = 0
594 then ()
595 else (
596 let dh = conf.tileh - y0 in
597 let dh = min h dh in
598 let rec colloop col x0 dispx w =
599 if w = 0
600 then ()
601 else (
602 let dw = conf.tilew - x0 in
603 let dw = min w dw in
604 let dispx' = xadj + dispx in
605 f col row dispx' dispy x0 y0 dw dh;
606 colloop (col+1) 0 (dispx+dw) (w-dw)
609 colloop col tilex l.pagedispx l.pagevw;
610 rowloop (row+1) 0 (dispy+dh) (h-dh)
613 if l.pagevw > 0 && l.pagevh > 0
614 then rowloop row tiley l.pagedispy l.pagevh;
617 let gettileopaque l col row =
618 let key =
619 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
621 try Some (Hashtbl.find state.tilemap key)
622 with Not_found -> None
625 let puttileopaque l col row gen colorspace angle opaque size elapsed =
626 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
627 Hashtbl.add state.tilemap key (opaque, size, elapsed)
630 let filledrect1 x0 y0 x1 y1 =
631 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
632 GlArray.vertex `two state.vraw;
633 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
636 let filledrect x0 y0 x1 y1 =
637 GlArray.disable `texture_coord;
638 filledrect1 x0 y0 x1 y1;
639 GlArray.enable `texture_coord;
642 let linerect x0 y0 x1 y1 =
643 GlArray.disable `texture_coord;
644 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y1; x1; y0 |];
645 GlArray.vertex `two state.vraw;
646 GlArray.draw_arrays `line_loop ~first:0 ~count:4;
647 GlArray.enable `texture_coord;
650 let drawtiles l color =
651 GlDraw.color color;
652 let wadj = wadjsb () in
653 begintiles ();
654 let f col row x y tilex tiley w h =
655 match gettileopaque l col row with
656 | Some (opaque, _, t) ->
657 let params = x, y, w, h, tilex, tiley in
658 if conf.invert
659 then GlTex.env (`mode `blend);
660 drawtile params opaque;
661 if conf.invert
662 then GlTex.env (`mode `modulate);
663 if conf.debug
664 then (
665 endtiles ();
666 let s = Printf.sprintf
667 "%d[%d,%d] %f sec"
668 l.pageno col row t
670 let w = measurestr fstate.fontsize s in
671 GlDraw.color (0.0, 0.0, 0.0);
672 filledrect (float (x-2))
673 (float (y-2))
674 (float (x+2) +. w)
675 (float (y + fstate.fontsize + 2));
676 GlDraw.color color;
677 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
678 begintiles ();
681 | None ->
682 endtiles ();
683 let w =
684 if conf.leftscroll
685 then w
686 else
687 let lw = wadj + state.winw - x in
688 min lw w
689 and h =
690 let lh = state.winh - y in
691 min lh h
693 if conf.invert
694 then GlTex.env (`mode `blend);
695 begin match state.checkerstexid with
696 | Some id ->
697 Gl.enable `texture_2d;
698 GlTex.bind_texture ~target:`texture_2d id;
699 let x0 = float x
700 and y0 = float y
701 and x1 = float (x+w)
702 and y1 = float (y+h) in
704 let tw = float w /. 16.0
705 and th = float h /. 16.0 in
706 let tx0 = float tilex /. 16.0
707 and ty0 = float tiley /. 16.0 in
708 let tx1 = tx0 +. tw
709 and ty1 = ty0 +. th in
710 Raw.sets_float state.vraw ~pos:0
711 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
712 Raw.sets_float state.traw ~pos:0
713 [| tx0; ty0; tx0; ty1; tx1; ty0; tx1; ty1 |];
714 GlArray.vertex `two state.vraw;
715 GlArray.tex_coord `two state.traw;
716 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
717 Gl.disable `texture_2d;
719 | None ->
720 GlDraw.color (1.0, 1.0, 1.0);
721 filledrect (float x) (float y) (float (x+w)) (float (y+h));
722 end;
723 if conf.invert
724 then GlTex.env (`mode `modulate);
725 if w > 128 && h > fstate.fontsize + 10
726 then (
727 let c = if conf.invert then 1.0 else 0.0 in
728 GlDraw.color (c, c, c);
729 let c, r =
730 if conf.verbose
731 then (col*conf.tilew, row*conf.tileh)
732 else col, row
734 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
736 GlDraw.color color;
737 begintiles ();
739 itertiles l f;
740 endtiles ();
743 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
745 let tilevisible1 l x y =
746 let ax0 = l.pagex
747 and ax1 = l.pagex + l.pagevw
748 and ay0 = l.pagey
749 and ay1 = l.pagey + l.pagevh in
751 let bx0 = x
752 and by0 = y in
753 let bx1 = min (bx0 + conf.tilew) l.pagew
754 and by1 = min (by0 + conf.tileh) l.pageh in
756 let rx0 = max ax0 bx0
757 and ry0 = max ay0 by0
758 and rx1 = min ax1 bx1
759 and ry1 = min ay1 by1 in
761 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
762 nonemptyintersection
765 let tilevisible layout n x y =
766 let rec findpageinlayout m = function
767 | l :: rest when l.pageno = n ->
768 tilevisible1 l x y || (
769 match conf.columns with
770 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
771 | Csplit _
772 | Csingle _
773 | Cmulti _ -> false
775 | _ :: rest -> findpageinlayout 0 rest
776 | [] -> false
778 findpageinlayout 0 layout;
781 let tileready l x y =
782 tilevisible1 l x y &&
783 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
786 let tilepage n p layout =
787 let rec loop = function
788 | l :: rest ->
789 if l.pageno = n
790 then
791 let f col row _ _ _ _ _ _ =
792 if state.currently = Idle
793 then
794 match gettileopaque l col row with
795 | Some _ -> ()
796 | None ->
797 let x = col*conf.tilew
798 and y = row*conf.tileh in
799 let w =
800 let w = l.pagew - x in
801 min w conf.tilew
803 let h =
804 let h = l.pageh - y in
805 min h conf.tileh
807 let pbo =
808 if conf.usepbo
809 then getpbo w h conf.colorspace
810 else ~< "0"
812 wcmd "tile %s %d %d %d %d %s"
813 (~> p) x y w h (~> pbo);
814 state.currently <-
815 Tiling (
816 l, p, conf.colorspace, conf.angle,
817 state.gen, col, row, conf.tilew, conf.tileh
820 itertiles l f;
821 else
822 loop rest
824 | [] -> ()
826 if nogeomcmds state.geomcmds
827 then loop layout;
830 let preloadlayout x y sw sh =
831 let y = if y < sh then 0 else y - sh in
832 let x = min 0 (x + sw) in
833 let h = sh*3 in
834 let w = sw*3 in
835 layout x y w h;
838 let load pages =
839 let rec loop pages =
840 if state.currently != Idle
841 then ()
842 else
843 match pages with
844 | l :: rest ->
845 begin match getopaque l.pageno with
846 | None ->
847 wcmd "page %d %d" l.pageno l.pagedimno;
848 state.currently <- Loading (l, state.gen);
849 | Some opaque ->
850 tilepage l.pageno opaque pages;
851 loop rest
852 end;
853 | _ -> ()
855 if nogeomcmds state.geomcmds
856 then loop pages
859 let preload pages =
860 load pages;
861 if conf.preload && state.currently = Idle
862 then load (preloadlayout state.x state.y state.winw state.winh);
865 let layoutready layout =
866 let rec fold all ls =
867 all && match ls with
868 | l :: rest ->
869 let seen = ref false in
870 let allvisible = ref true in
871 let foo col row _ _ _ _ _ _ =
872 seen := true;
873 allvisible := !allvisible &&
874 begin match gettileopaque l col row with
875 | Some _ -> true
876 | None -> false
879 itertiles l foo;
880 fold (!seen && !allvisible) rest
881 | [] -> true
883 let alltilesvisible = fold true layout in
884 alltilesvisible;
887 let gotoy y =
888 let y = bound y 0 state.maxy in
889 let y, layout, proceed =
890 match conf.maxwait with
891 | Some time when state.ghyll == noghyll ->
892 begin match state.throttle with
893 | None ->
894 let layout = layout state.x y state.winw state.winh in
895 let ready = layoutready layout in
896 if not ready
897 then (
898 load layout;
899 state.throttle <- Some (layout, y, now ());
901 else G.postRedisplay "gotoy showall (None)";
902 y, layout, ready
903 | Some (_, _, started) ->
904 let dt = now () -. started in
905 if dt > time
906 then (
907 state.throttle <- None;
908 let layout = layout state.x y state.winw state.winh in
909 load layout;
910 G.postRedisplay "maxwait";
911 y, layout, true
913 else -1, [], false
916 | _ ->
917 let layout = layout state.x y state.winw state.winh in
918 if not !wtmode || layoutready layout
919 then G.postRedisplay "gotoy ready";
920 y, layout, true
922 if proceed
923 then (
924 state.y <- y;
925 state.layout <- layout;
926 begin match state.mode with
927 | LinkNav ln ->
928 begin match ln with
929 | Ltexact (pageno, linkno) ->
930 let rec loop = function
931 | [] ->
932 state.mode <- LinkNav (Ltgendir 0)
933 | l :: _ when l.pageno = pageno ->
934 begin match getopaque pageno with
935 | None -> state.mode <- LinkNav (Ltnotready (pageno, 0))
936 | Some opaque ->
937 let x0, y0, x1, y1 = getlinkrect opaque linkno in
938 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
939 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
940 then state.mode <- LinkNav (Ltgendir 0)
942 | _ :: rest -> loop rest
944 loop layout
945 | Ltnotready _ | Ltgendir _ -> ()
947 | Birdseye _
948 | Textentry _
949 | View -> ()
950 end;
951 begin match state.mode with
952 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
953 if not (pagevisible layout pageno)
954 then (
955 match state.layout with
956 | [] -> ()
957 | l :: _ ->
958 state.mode <- Birdseye (
959 conf, leftx, l.pageno, hooverpageno, anchor
962 | LinkNav lt ->
963 begin match lt with
964 | Ltnotready (_, dir)
965 | Ltgendir dir ->
966 let linknav =
967 let rec loop = function
968 | [] -> lt
969 | l :: rest ->
970 match getopaque l.pageno with
971 | None -> Ltnotready (l.pageno, dir)
972 | Some opaque ->
973 let link =
974 let ld =
975 if dir = 0
976 then LDfirstvisible (l.pagex, l.pagey, dir)
977 else (
978 if dir > 0 then LDfirst else LDlast
981 findlink opaque ld
983 match link with
984 | Lnotfound -> loop rest
985 | Lfound n ->
986 showlinktype (getlink opaque n);
987 Ltexact (l.pageno, n)
989 loop state.layout
991 state.mode <- LinkNav linknav
992 | Ltexact _ -> ()
994 | Textentry _
995 | View -> ()
996 end;
997 preload layout;
999 state.ghyll <- noghyll;
1000 if conf.updatecurs
1001 then (
1002 let mx, my = state.mpos in
1003 updateunder mx my;
1007 let conttiling pageno opaque =
1008 tilepage pageno opaque
1009 (if conf.preload
1010 then preloadlayout state.x state.y state.winw state.winh
1011 else state.layout)
1014 let gotoy_and_clear_text y =
1015 if not conf.verbose then state.text <- E.s;
1016 gotoy y;
1019 let getanchory (n, top, dtop) =
1020 let y, h = getpageyh n in
1021 if conf.presentation
1022 then
1023 let ips = calcips h in
1024 y + truncate (top*.float h -. dtop*.float ips) + ips;
1025 else
1026 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1029 let gotoanchor anchor =
1030 gotoy (getanchory anchor);
1033 let addnav () =
1034 cbput state.hists.nav (getanchor ());
1037 let getnav dir =
1038 let anchor = cbgetc state.hists.nav dir in
1039 getanchory anchor;
1042 let gotoghyll1 single y =
1043 let scroll f n a b =
1044 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1045 let snake f a b =
1046 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1047 if f < a
1048 then s (float f /. float a)
1049 else (
1050 if f > b
1051 then 1.0 -. s ((float (f-b) /. float (n-b)))
1052 else 1.0
1055 snake f a b
1056 and summa n a b =
1057 let ins = float a *. 0.5
1058 and outs = float (n-b) *. 0.5 in
1059 let ones = b - a in
1060 ins +. outs +. float ones
1062 let rec set nab y sy =
1063 let (_N, _A, _B), y =
1064 if single
1065 then
1066 let scl = if y > sy then 2 else -2 in
1067 let _N, _, _ = nab in
1068 (_N,0,_N), y+conf.scrollstep*scl
1069 else nab,y in
1070 let sum = summa _N _A _B in
1071 let dy = float (y - sy) in
1072 state.ghyll <- (
1073 let rec gf n y1 o =
1074 if n >= _N
1075 then state.ghyll <- noghyll
1076 else
1077 let go n =
1078 let s = scroll n _N _A _B in
1079 let y1 = y1 +. ((s *. dy) /. sum) in
1080 gotoy_and_clear_text (truncate y1);
1081 state.ghyll <- gf (n+1) y1;
1083 match o with
1084 | None -> go n
1085 | Some y' when single -> set nab y' state.y
1086 | Some y' -> set (_N/2, 1, 1) y' state.y
1088 gf 0 (float state.y)
1091 match conf.ghyllscroll with
1092 | Some nab when not conf.presentation ->
1093 if state.ghyll == noghyll
1094 then set nab y state.y
1095 else state.ghyll (Some y)
1096 | _ ->
1097 gotoy_and_clear_text y
1100 let gotoghyll = gotoghyll1 false;;
1102 let gotopage n top =
1103 let y, h = getpageyh n in
1104 let y = y + (truncate (top *. float h)) in
1105 gotoghyll y
1108 let gotopage1 n top =
1109 let y = getpagey n in
1110 let y = y + top in
1111 gotoghyll y
1114 let invalidate s f =
1115 state.layout <- [];
1116 state.pdims <- [];
1117 state.rects <- [];
1118 state.rects1 <- [];
1119 match state.geomcmds with
1120 | ps, [] when emptystr ps ->
1121 f ();
1122 state.geomcmds <- s, [];
1124 | ps, [] ->
1125 state.geomcmds <- ps, [s, f];
1127 | ps, (s', _) :: rest when s' = s ->
1128 state.geomcmds <- ps, ((s, f) :: rest);
1130 | ps, cmds ->
1131 state.geomcmds <- ps, ((s, f) :: cmds);
1134 let flushpages () =
1135 Hashtbl.iter (fun _ opaque ->
1136 wcmd "freepage %s" (~> opaque);
1137 ) state.pagemap;
1138 Hashtbl.clear state.pagemap;
1141 let flushtiles () =
1142 if not (Queue.is_empty state.tilelru)
1143 then (
1144 Queue.iter (fun (k, p, s) ->
1145 wcmd "freetile %s" (~> p);
1146 state.memused <- state.memused - s;
1147 Hashtbl.remove state.tilemap k;
1148 ) state.tilelru;
1149 state.uioh#infochanged Memused;
1150 Queue.clear state.tilelru;
1152 load state.layout;
1155 let stateh h =
1156 let h = truncate (float h*.conf.zoom) in
1157 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
1158 h - d
1161 let opendoc path password =
1162 state.path <- path;
1163 state.password <- password;
1164 state.gen <- state.gen + 1;
1165 state.docinfo <- [];
1166 state.outlines <- [||];
1168 flushpages ();
1169 setaalevel conf.aalevel;
1170 let titlepath =
1171 if emptystr state.origin
1172 then path
1173 else state.origin
1175 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
1176 wcmd "open %d %d %s\000%s\000" (btod !wtmode) (btod !cxack) path password;
1177 invalidate "reqlayout"
1178 (fun () ->
1179 wcmd "reqlayout %d %d %d %s\000"
1180 conf.angle (FMTE.to_int conf.fitmodel)
1181 (stateh state.winh) state.nameddest
1185 let reload () =
1186 state.anchor <- getanchor ();
1187 opendoc state.path state.password;
1190 let scalecolor c =
1191 let c = c *. conf.colorscale in
1192 (c, c, c);
1195 let scalecolor2 (r, g, b) =
1196 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1199 let docolumns columns =
1200 let wadj = wadjsb () in
1201 match columns with
1202 | Csingle _ ->
1203 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1204 let wadj = wadjsb () in
1205 let rec loop pageno pdimno pdim y ph pdims =
1206 if pageno = state.pagecount
1207 then ()
1208 else
1209 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1210 match pdims with
1211 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1212 pdimno+1, pdim, rest
1213 | _ ->
1214 pdimno, pdim, pdims
1216 let x = max 0 (((wadj + state.winw - w) / 2) - xoff) in
1217 let y = y +
1218 (if conf.presentation
1219 then (if pageno = 0 then calcips h else calcips ph + calcips h)
1220 else (if pageno = 0 then 0 else conf.interpagespace)
1223 a.(pageno) <- (pdimno, x, y, pdim);
1224 loop (pageno+1) pdimno pdim (y + h) h pdims
1226 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
1227 conf.columns <- Csingle a;
1229 | Cmulti ((columns, coverA, coverB), _) ->
1230 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1231 let rec loop pageno pdimno pdim x y rowh pdims =
1232 let rec fixrow m = if m = pageno then () else
1233 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1234 if h < rowh
1235 then (
1236 let y = y + (rowh - h) / 2 in
1237 a.(m) <- (pdimno, x, y, pdim);
1239 fixrow (m+1)
1241 if pageno = state.pagecount
1242 then fixrow (((pageno - 1) / columns) * columns)
1243 else
1244 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1245 match pdims with
1246 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1247 pdimno+1, pdim, rest
1248 | _ ->
1249 pdimno, pdim, pdims
1251 let x, y, rowh' =
1252 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1253 then (
1254 let x = (wadj + state.winw - w) / 2 in
1255 let ips =
1256 if conf.presentation then calcips h else conf.interpagespace in
1257 x, y + ips + rowh, h
1259 else (
1260 if (pageno - coverA) mod columns = 0
1261 then (
1262 let x = max 0 (wadj + state.winw - state.w) / 2 in
1263 let y =
1264 if conf.presentation
1265 then
1266 let ips = calcips h in
1267 y + (if pageno = 0 then 0 else calcips rowh + ips)
1268 else
1269 y + (if pageno = 0 then 0 else conf.interpagespace)
1271 x, y + rowh, h
1273 else x, y, max rowh h
1276 let y =
1277 if pageno > 1 && (pageno - coverA) mod columns = 0
1278 then (
1279 let y =
1280 if pageno = columns && conf.presentation
1281 then (
1282 let ips = calcips rowh in
1283 for i = 0 to pred columns
1285 let (pdimno, x, y, pdim) = a.(i) in
1286 a.(i) <- (pdimno, x, y+ips, pdim)
1287 done;
1288 y+ips;
1290 else y
1292 fixrow (pageno - columns);
1295 else y
1297 a.(pageno) <- (pdimno, x, y, pdim);
1298 let x = x + w + xoff*2 + conf.interpagespace in
1299 loop (pageno+1) pdimno pdim x y rowh' pdims
1301 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1302 conf.columns <- Cmulti ((columns, coverA, coverB), a);
1304 | Csplit (c, _) ->
1305 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
1306 let rec loop pageno pdimno pdim y pdims =
1307 if pageno = state.pagecount
1308 then ()
1309 else
1310 let pdimno, ((_, w, h, _) as pdim), pdims =
1311 match pdims with
1312 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1313 pdimno+1, pdim, rest
1314 | _ ->
1315 pdimno, pdim, pdims
1317 let cw = w / c in
1318 let rec loop1 n x y =
1319 if n = c then y else (
1320 a.(pageno*c + n) <- (pdimno, x, y, pdim);
1321 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
1324 let y = loop1 0 0 y in
1325 loop (pageno+1) pdimno pdim y pdims
1327 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
1328 conf.columns <- Csplit (c, a);
1331 let represent () =
1332 docolumns conf.columns;
1333 state.maxy <- calcheight ();
1334 if state.reprf == noreprf
1335 then (
1336 match state.mode with
1337 | Birdseye (_, _, pageno, _, _) ->
1338 let y, h = getpageyh pageno in
1339 let top = (state.winh - h) / 2 in
1340 gotoy (max 0 (y - top))
1341 | Textentry _
1342 | View
1343 | LinkNav _ ->
1344 let y = getanchory state.anchor in
1345 let y = min y (state.maxy - state.winw - hscrollh ()) in
1346 gotoy y;
1348 else (
1349 state.reprf ();
1350 state.reprf <- noreprf;
1354 let reshape ?(firsttime=false) w h =
1355 GlDraw.viewport ~x:0 ~y:0 ~w:w ~h:h;
1356 if not firsttime && nogeomcmds state.geomcmds
1357 then state.anchor <- getanchor ();
1359 state.winw <- w;
1360 let w = wadjsb () + (truncate (float w *. conf.zoom)) in
1361 let w = max w 2 in
1362 state.winh <- h;
1363 setfontsize fstate.fontsize;
1364 GlMat.mode `modelview;
1365 GlMat.load_identity ();
1367 GlMat.mode `projection;
1368 GlMat.load_identity ();
1369 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1370 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1371 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
1373 let relx =
1374 if conf.zoom <= 1.0
1375 then 0.0
1376 else float state.x /. float state.w
1378 invalidate "geometry"
1379 (fun () ->
1380 state.w <- w;
1381 if not firsttime
1382 then state.x <- truncate (relx *. float w);
1383 let w =
1384 match conf.columns with
1385 | Csingle _ -> w
1386 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1387 | Csplit (c, _) -> w * c
1389 wcmd "geometry %d %d %d"
1390 w (stateh h) (FMTE.to_int conf.fitmodel)
1394 let enttext () =
1395 let len = String.length state.text in
1396 let x0 = xadjsb () in
1397 let drawstring s =
1398 let hscrollh =
1399 match state.mode with
1400 | Textentry _ | View | LinkNav _ ->
1401 let h, _, _ = state.uioh#scrollpw in
1403 | Birdseye _ -> 0
1405 let rect x w =
1406 filledrect x (float (state.winh - (fstate.fontsize + 4) - hscrollh))
1407 (x+.w) (float (state.winh - hscrollh))
1410 let w = float (wadjsb () + state.winw - 1) in
1411 if state.progress >= 0.0 && state.progress < 1.0
1412 then (
1413 GlDraw.color (0.3, 0.3, 0.3);
1414 let w1 = w *. state.progress in
1415 rect (float x0) w1;
1416 GlDraw.color (0.0, 0.0, 0.0);
1417 rect (float x0+.w1) (float x0+.w-.w1)
1419 else (
1420 GlDraw.color (0.0, 0.0, 0.0);
1421 rect (float x0) w;
1424 GlDraw.color (1.0, 1.0, 1.0);
1425 drawstring fstate.fontsize
1426 (if conf.leftscroll then x0 + 2 else x0 + if len > 0 then 8 else 2)
1427 (state.winh - hscrollh - 5) s;
1429 let s =
1430 match state.mode with
1431 | Textentry ((prefix, text, _, _, _, _), _) ->
1432 let s =
1433 if len > 0
1434 then
1435 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1436 else
1437 Printf.sprintf "%s%s_" prefix text
1441 | Birdseye _
1442 | View
1443 | LinkNav _ -> state.text
1445 let s =
1446 if state.newerrmsgs
1447 then (
1448 if not (istextentry state.mode) && state.uioh#eformsgs
1449 then
1450 let s1 = "(press 'e' to review error messasges)" in
1451 if nonemptystr s then s ^ " " ^ s1 else s1
1452 else s
1454 else s
1456 if nonemptystr s
1457 then drawstring s
1460 let gctiles () =
1461 let len = Queue.length state.tilelru in
1462 let layout = lazy (
1463 match state.throttle with
1464 | None ->
1465 if conf.preload
1466 then preloadlayout state.x state.y state.winw state.winh
1467 else state.layout
1468 | Some (layout, _, _) ->
1469 layout
1470 ) in
1471 let rec loop qpos =
1472 if state.memused <= conf.memlimit
1473 then ()
1474 else (
1475 if qpos < len
1476 then
1477 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1478 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1479 let (_, pw, ph, _) = getpagedim n in
1481 gen = state.gen
1482 && colorspace = conf.colorspace
1483 && angle = conf.angle
1484 && pagew = pw
1485 && pageh = ph
1486 && (
1487 let x = col*conf.tilew
1488 and y = row*conf.tileh in
1489 tilevisible (Lazy.force_val layout) n x y
1491 then Queue.push lruitem state.tilelru
1492 else (
1493 freepbo p;
1494 wcmd "freetile %s" (~> p);
1495 state.memused <- state.memused - s;
1496 state.uioh#infochanged Memused;
1497 Hashtbl.remove state.tilemap k;
1499 loop (qpos+1)
1502 loop 0
1505 let onpagerect pageno f =
1506 let b =
1507 match conf.columns with
1508 | Cmulti (_, b) -> b
1509 | Csingle b -> b
1510 | Csplit (_, b) -> b
1512 if pageno >= 0 && pageno < Array.length b
1513 then
1514 let (_, _, _, (_, w, h, _)) = b.(pageno) in
1515 f w h
1518 let gotopagexy1 wtmode pageno x y =
1519 let _,w1,h1,leftx = getpagedim pageno in
1520 let top = y /. (float h1) in
1521 let left = x /. (float w1) in
1522 let py, w, h = getpageywh pageno in
1523 let wh = state.winh - hscrollh () in
1524 let x = left *. (float w) in
1525 let x = leftx + state.x + truncate x in
1526 let wadj = wadjsb () in
1527 let sx =
1528 if x < 0 || x >= wadj + state.winw
1529 then state.x - x
1530 else state.x
1532 let pdy = truncate (top *. float h) in
1533 let y' = py + pdy in
1534 let dy = y' - state.y in
1535 let sy =
1536 if x != state.x || not (dy > 0 && dy < wh)
1537 then (
1538 if conf.presentation
1539 then
1540 if abs (py - y') > wh
1541 then y'
1542 else py
1543 else y';
1545 else state.y
1547 if state.x != sx || state.y != sy
1548 then (
1549 let x, y =
1550 if wtmode
1551 then (
1552 let ww = wadj + state.winw in
1553 let qx = sx / ww
1554 and qy = pdy / wh in
1555 let x = qx * ww
1556 and y = py + qy * wh in
1557 let x = if -x + ww > w1 then -(w1-ww) else x
1558 and y' = if y + wh > state.maxy then state.maxy - wh else y in
1559 let y =
1560 if conf.presentation
1561 then
1562 if abs (py - y') > wh
1563 then y'
1564 else py
1565 else y';
1567 (x, y)
1569 else (sx, sy)
1571 state.x <- x;
1572 gotoy_and_clear_text y;
1574 else gotoy_and_clear_text state.y;
1577 let gotopagexy wtmode pageno x y =
1578 match state.mode with
1579 | Birdseye _ -> gotopage pageno 0.0
1580 | Textentry _
1581 | View
1582 | LinkNav _ -> gotopagexy1 wtmode pageno x y
1585 let getpassword () =
1586 let passcmd = getenvwithdef "LLPP_ASKPASS" conf.passcmd in
1587 if emptystr passcmd
1588 then E.s
1589 else getcmdoutput
1590 (fun s ->
1591 impmsg "error getting password: %s" s;
1592 dolog "%s" s) passcmd;
1595 let pgoto opaque pageno x y =
1596 let pdimno = getpdimno pageno in
1597 let x, y = project opaque pageno pdimno x y in
1598 gotopagexy false pageno x y;
1601 let act cmds =
1602 (* dolog "%S" cmds; *)
1603 let cl = splitatspace cmds in
1604 let scan s fmt f =
1605 try Scanf.sscanf s fmt f
1606 with exn ->
1607 dolog "error processing '%S': %s" cmds @@ exntos exn;
1608 exit 1
1610 let addoutline outline =
1611 match state.currently with
1612 | Outlining outlines ->
1613 state.currently <- Outlining (outline :: outlines)
1614 | Idle -> state.currently <- Outlining [outline]
1615 | Loading _
1616 | Tiling _ ->
1617 dolog "invalid outlining state";
1618 logcurrently state.currently
1620 match cl with
1621 | "clear" :: [] ->
1622 state.uioh#infochanged Pdim;
1623 state.pdims <- [];
1625 | "clearrects" :: [] ->
1626 state.rects <- state.rects1;
1627 G.postRedisplay "clearrects";
1629 | "continue" :: args :: [] ->
1630 let n = scan args "%u" (fun n -> n) in
1631 state.pagecount <- n;
1632 begin match state.currently with
1633 | Outlining l ->
1634 state.currently <- Idle;
1635 state.outlines <- Array.of_list (List.rev l)
1636 | Idle
1637 | Loading _
1638 | Tiling _ -> ()
1639 end;
1641 let cur, cmds = state.geomcmds in
1642 if emptystr cur
1643 then failwith "umpossible";
1645 begin match List.rev cmds with
1646 | [] ->
1647 state.geomcmds <- E.s, [];
1648 state.throttle <- None;
1649 represent ();
1650 | (s, f) :: rest ->
1651 f ();
1652 state.geomcmds <- s, List.rev rest;
1653 end;
1654 if conf.maxwait = None && not !wtmode
1655 then G.postRedisplay "continue";
1657 | "msg" :: args :: [] ->
1658 showtext ' ' args
1660 | "vmsg" :: args :: [] ->
1661 if conf.verbose
1662 then showtext ' ' args
1664 | "emsg" :: args :: [] ->
1665 Buffer.add_string state.errmsgs args;
1666 state.newerrmsgs <- true;
1667 G.postRedisplay "error message"
1669 | "progress" :: args :: [] ->
1670 let progress, text =
1671 scan args "%f %n"
1672 (fun f pos ->
1673 f, String.sub args pos (String.length args - pos))
1675 state.text <- text;
1676 state.progress <- progress;
1677 G.postRedisplay "progress"
1679 | "firstmatch" :: args :: [] ->
1680 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1681 scan args "%u %d %f %f %f %f %f %f %f %f"
1682 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1683 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1685 let xoff = float (xadjsb ()) in
1686 let x0 = x0 +. xoff
1687 and x1 = x1 +. xoff
1688 and x2 = x2 +. xoff
1689 and x3 = x3 +. xoff in
1690 let y = (getpagey pageno) + truncate y0 in
1691 if conf.zoom > 1.0
1692 then state.x <- truncate (xoff -. x0) + state.winw/2;
1693 addnav ();
1694 gotoy y;
1695 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1696 state.rects1 <- [pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)]
1698 | "match" :: args :: [] ->
1699 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
1700 scan args "%u %d %f %f %f %f %f %f %f %f"
1701 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
1702 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
1704 let xoff = float (xadjsb ()) in
1705 let x0 = x0 +. xoff
1706 and x1 = x1 +. xoff
1707 and x2 = x2 +. xoff
1708 and x3 = x3 +. xoff in
1709 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
1710 state.rects1 <-
1711 (pageno, color, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
1713 | "page" :: args :: [] ->
1714 let pageopaques, t = scan args "%s %f" (fun p t -> p, t) in
1715 let pageopaque = ~< pageopaques in
1716 begin match state.currently with
1717 | Loading (l, gen) ->
1718 vlog "page %d took %f sec" l.pageno t;
1719 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
1720 begin match state.throttle with
1721 | None ->
1722 let preloadedpages =
1723 if conf.preload
1724 then preloadlayout state.x state.y state.winw state.winh
1725 else state.layout
1727 let evict () =
1728 let set =
1729 List.fold_left (fun s l -> IntSet.add l.pageno s)
1730 IntSet.empty preloadedpages
1732 let evictedpages =
1733 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
1734 if not (IntSet.mem pageno set)
1735 then (
1736 wcmd "freepage %s" (~> opaque);
1737 key :: accu
1739 else accu
1740 ) state.pagemap []
1742 List.iter (Hashtbl.remove state.pagemap) evictedpages;
1744 evict ();
1745 state.currently <- Idle;
1746 if gen = state.gen
1747 then (
1748 tilepage l.pageno pageopaque state.layout;
1749 load state.layout;
1750 load preloadedpages;
1751 let visible = pagevisible state.layout l.pageno in
1752 if visible
1753 then (
1754 match state.mode with
1755 | LinkNav (Ltnotready (pageno, dir)) ->
1756 if pageno = l.pageno
1757 then (
1758 let link =
1759 let ld =
1760 if dir = 0
1761 then LDfirstvisible (l.pagex, l.pagey, dir)
1762 else (
1763 if dir > 0 then LDfirst else LDlast
1766 findlink pageopaque ld
1768 match link with
1769 | Lnotfound -> ()
1770 | Lfound n ->
1771 showlinktype (getlink pageopaque n);
1772 state.mode <- LinkNav (Ltexact (l.pageno, n))
1774 | LinkNav (Ltgendir _)
1775 | LinkNav (Ltexact _)
1776 | View
1777 | Birdseye _
1778 | Textentry _ -> ()
1781 if visible && layoutready state.layout
1782 then (
1783 G.postRedisplay "page";
1787 | Some (layout, _, _) ->
1788 state.currently <- Idle;
1789 tilepage l.pageno pageopaque layout;
1790 load state.layout
1791 end;
1793 | Idle
1794 | Tiling _
1795 | Outlining _ ->
1796 dolog "Inconsistent loading state";
1797 logcurrently state.currently;
1798 exit 1
1801 | "tile" :: args :: [] ->
1802 let (x, y, opaques, size, t) =
1803 scan args "%u %u %s %u %f"
1804 (fun x y p size t -> (x, y, p, size, t))
1806 let opaque = ~< opaques in
1807 begin match state.currently with
1808 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
1809 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
1811 unmappbo opaque;
1812 if tilew != conf.tilew || tileh != conf.tileh
1813 then (
1814 wcmd "freetile %s" (~> opaque);
1815 state.currently <- Idle;
1816 load state.layout;
1818 else (
1819 puttileopaque l col row gen cs angle opaque size t;
1820 state.memused <- state.memused + size;
1821 state.uioh#infochanged Memused;
1822 gctiles ();
1823 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
1824 opaque, size) state.tilelru;
1826 let layout =
1827 match state.throttle with
1828 | None -> state.layout
1829 | Some (layout, _, _) -> layout
1832 state.currently <- Idle;
1833 if gen = state.gen
1834 && conf.colorspace = cs
1835 && conf.angle = angle
1836 && tilevisible layout l.pageno x y
1837 then conttiling l.pageno pageopaque;
1839 begin match state.throttle with
1840 | None ->
1841 preload state.layout;
1842 if gen = state.gen
1843 && conf.colorspace = cs
1844 && conf.angle = angle
1845 && tilevisible state.layout l.pageno x y
1846 && (not !wtmode || layoutready state.layout)
1847 then G.postRedisplay "tile nothrottle";
1849 | Some (layout, y, _) ->
1850 let ready = layoutready layout in
1851 if ready
1852 then (
1853 state.y <- y;
1854 state.layout <- layout;
1855 state.throttle <- None;
1856 G.postRedisplay "throttle";
1858 else load layout;
1859 end;
1862 | Idle
1863 | Loading _
1864 | Outlining _ ->
1865 dolog "Inconsistent tiling state";
1866 logcurrently state.currently;
1867 exit 1
1870 | "pdim" :: args :: [] ->
1871 let (n, w, h, _) as pdim =
1872 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
1874 let pdim =
1875 match conf.fitmodel with
1876 | FitWidth -> pdim
1877 | FitPage | FitProportional ->
1878 match conf.columns with
1879 | Csplit _ -> (n, w, h, 0)
1880 | Csingle _ | Cmulti _ -> pdim
1882 state.uioh#infochanged Pdim;
1883 state.pdims <- pdim :: state.pdims
1885 | "o" :: args :: [] ->
1886 let (l, n, t, h, pos) =
1887 scan args "%u %u %d %u %n"
1888 (fun l n t h pos -> l, n, t, h, pos)
1890 let s = String.sub args pos (String.length args - pos) in
1891 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
1893 | "ou" :: args :: [] ->
1894 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
1895 let s = String.sub args pos len in
1896 let pos2 = pos + len + 1 in
1897 let uri = String.sub args pos2 (String.length args - pos2) in
1898 addoutline (s, l, Ouri uri)
1900 | "on" :: args :: [] ->
1901 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
1902 let s = String.sub args pos (String.length args - pos) in
1903 addoutline (s, l, Onone)
1905 | "a" :: args :: [] ->
1906 let (n, l, t) =
1907 scan args "%u %d %d" (fun n l t -> n, l, t)
1909 state.reprf <- (fun () -> gotopagexy !wtmode n (float l) (float t))
1911 | "info" :: args :: [] ->
1912 let pos = nindex args '\t' in
1913 if pos >= 0 && String.sub args 0 pos = "Title"
1914 then (
1915 let s = String.sub args (pos+1) @@ String.length args - pos - 1 in
1916 conf.title <- s;
1917 Wsi.settitle s;
1919 state.docinfo <- (1, args) :: state.docinfo
1921 | "infoend" :: [] ->
1922 state.uioh#infochanged Docinfo;
1923 state.docinfo <- List.rev state.docinfo
1925 | "pass" :: l ->
1926 if l = "fail" :: []
1927 then Wsi.settitle "Wrong password";
1928 let password = getpassword () in
1929 if emptystr password
1930 then error "document is password protected"
1931 else opendoc state.path password
1932 | _ ->
1933 error "unknown cmd `%S'" cmds
1936 let onhist cb =
1937 let rc = cb.rc in
1938 let action = function
1939 | HCprev -> cbget cb ~-1
1940 | HCnext -> cbget cb 1
1941 | HCfirst -> cbget cb ~-(cb.rc)
1942 | HClast -> cbget cb (cb.len - 1 - cb.rc)
1943 and cancel () = cb.rc <- rc
1944 in (action, cancel)
1947 let search pattern forward =
1948 match conf.columns with
1949 | Csplit _ -> impmsg "searching does not work properly in split columns mode"
1950 | Csingle _
1951 | Cmulti _ ->
1952 if nonemptystr pattern
1953 then
1954 let pn, py =
1955 match state.layout with
1956 | [] -> 0, 0
1957 | l :: _ ->
1958 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
1960 wcmd "search %d %d %d %d,%s\000"
1961 (btod conf.icase) pn py (btod forward) pattern;
1964 let intentry text key =
1965 let c =
1966 if key >= 32 && key < 127
1967 then Char.chr key
1968 else '\000'
1970 match c with
1971 | '0' .. '9' ->
1972 let text = addchar text c in
1973 TEcont text
1975 | _ ->
1976 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1977 TEcont text
1980 let linknact f s =
1981 if nonemptystr s
1982 then (
1983 let n =
1984 let l = String.length s in
1985 let rec loop pos n = if pos = l then n else
1986 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
1987 loop (pos+1) (n*26 + m)
1988 in loop 0 0
1990 let rec loop n = function
1991 | [] -> ()
1992 | l :: rest ->
1993 match getopaque l.pageno with
1994 | None -> loop n rest
1995 | Some opaque ->
1996 let m = getlinkcount opaque in
1997 if n < m
1998 then (
1999 let under = getlink opaque n in
2000 f under
2002 else loop (n-m) rest
2004 loop n state.layout;
2008 let linknentry text key =
2009 let c =
2010 if key >= 32 && key < 127
2011 then Char.chr key
2012 else '\000'
2014 match c with
2015 | 'a' .. 'z' ->
2016 let text = addchar text c in
2017 linknact (fun under -> state.text <- undertext ~nopath:true under) text;
2018 TEcont text
2020 | _ ->
2021 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2022 TEcont text
2025 let textentry text key =
2026 if key land 0xff00 = 0xff00
2027 then TEcont text
2028 else TEcont (text ^ toutf8 key)
2031 let reqlayout angle fitmodel =
2032 match state.throttle with
2033 | None ->
2034 if nogeomcmds state.geomcmds
2035 then state.anchor <- getanchor ();
2036 conf.angle <- angle mod 360;
2037 if conf.angle != 0
2038 then (
2039 match state.mode with
2040 | LinkNav _ -> state.mode <- View
2041 | Birdseye _
2042 | Textentry _
2043 | View -> ()
2045 conf.fitmodel <- fitmodel;
2046 invalidate "reqlayout"
2047 (fun () ->
2048 wcmd "reqlayout %d %d %d"
2049 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
2051 | _ -> ()
2054 let settrim trimmargins trimfuzz =
2055 if nogeomcmds state.geomcmds
2056 then state.anchor <- getanchor ();
2057 conf.trimmargins <- trimmargins;
2058 conf.trimfuzz <- trimfuzz;
2059 let x0, y0, x1, y1 = trimfuzz in
2060 invalidate "settrim"
2061 (fun () ->
2062 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2063 flushpages ();
2066 let setzoom zoom =
2067 match state.throttle with
2068 | None ->
2069 let zoom = max 0.0001 zoom in
2070 if zoom <> conf.zoom
2071 then (
2072 state.prevzoom <- (conf.zoom, state.x);
2073 conf.zoom <- zoom;
2074 reshape state.winw state.winh;
2075 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2078 | Some (layout, y, started) ->
2079 let time =
2080 match conf.maxwait with
2081 | None -> 0.0
2082 | Some t -> t
2084 let dt = now () -. started in
2085 if dt > time
2086 then (
2087 state.y <- y;
2088 load layout;
2092 let setcolumns mode columns coverA coverB =
2093 state.prevcolumns <- Some (conf.columns, conf.zoom);
2094 if columns < 0
2095 then (
2096 if isbirdseye mode
2097 then impmsg "split mode doesn't work in bird's eye"
2098 else (
2099 conf.columns <- Csplit (-columns, E.a);
2100 state.x <- 0;
2101 conf.zoom <- 1.0;
2104 else (
2105 if columns < 2
2106 then (
2107 conf.columns <- Csingle E.a;
2108 state.x <- 0;
2109 setzoom 1.0;
2111 else (
2112 conf.columns <- Cmulti ((columns, coverA, coverB), E.a);
2113 conf.zoom <- 1.0;
2116 reshape state.winw state.winh;
2119 let resetmstate () =
2120 state.mstate <- Mnone;
2121 Wsi.setcursor Wsi.CURSOR_INHERIT;
2124 let enterbirdseye () =
2125 let zoom = float conf.thumbw /. float state.winw in
2126 let birdseyepageno =
2127 let cy = state.winh / 2 in
2128 let fold = function
2129 | [] -> 0
2130 | l :: rest ->
2131 let rec fold best = function
2132 | [] -> best.pageno
2133 | l :: rest ->
2134 let d = cy - (l.pagedispy + l.pagevh/2)
2135 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2136 if abs d < abs dbest
2137 then fold l rest
2138 else best.pageno
2139 in fold l rest
2141 fold state.layout
2143 state.mode <- Birdseye (
2144 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2146 resetmstate ();
2147 conf.zoom <- zoom;
2148 conf.presentation <- false;
2149 conf.interpagespace <- 10;
2150 conf.hlinks <- false;
2151 conf.fitmodel <- FitPage;
2152 state.x <- 0;
2153 conf.maxwait <- None;
2154 conf.columns <- (
2155 match conf.beyecolumns with
2156 | Some c ->
2157 conf.zoom <- 1.0;
2158 Cmulti ((c, 0, 0), E.a)
2159 | None -> Csingle E.a
2161 if conf.verbose
2162 then
2163 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2164 (100.0*.zoom)
2165 else
2166 state.text <- E.s
2168 reshape state.winw state.winh;
2171 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2172 state.mode <- View;
2173 conf.zoom <- c.zoom;
2174 conf.presentation <- c.presentation;
2175 conf.interpagespace <- c.interpagespace;
2176 conf.maxwait <- c.maxwait;
2177 conf.hlinks <- c.hlinks;
2178 conf.fitmodel <- c.fitmodel;
2179 conf.beyecolumns <- (
2180 match conf.columns with
2181 | Cmulti ((c, _, _), _) -> Some c
2182 | Csingle _ -> None
2183 | Csplit _ -> failwith "leaving bird's eye split mode"
2185 conf.columns <- (
2186 match c.columns with
2187 | Cmulti (c, _) -> Cmulti (c, E.a)
2188 | Csingle _ -> Csingle E.a
2189 | Csplit (c, _) -> Csplit (c, E.a)
2191 if conf.verbose
2192 then
2193 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2194 (100.0*.conf.zoom)
2196 reshape state.winw state.winh;
2197 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
2198 state.x <- leftx;
2201 let togglebirdseye () =
2202 match state.mode with
2203 | Birdseye vals -> leavebirdseye vals true
2204 | View -> enterbirdseye ()
2205 | Textentry _
2206 | LinkNav _ -> ()
2209 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2210 let pageno = max 0 (pageno - incr) in
2211 let rec loop = function
2212 | [] -> gotopage1 pageno 0
2213 | l :: _ when l.pageno = pageno ->
2214 if l.pagedispy >= 0 && l.pagey = 0
2215 then G.postRedisplay "upbirdseye"
2216 else gotopage1 pageno 0
2217 | _ :: rest -> loop rest
2219 loop state.layout;
2220 state.text <- E.s;
2221 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2224 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2225 let pageno = min (state.pagecount - 1) (pageno + incr) in
2226 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2227 let rec loop = function
2228 | [] ->
2229 let y, h = getpageyh pageno in
2230 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
2231 gotoy (clamp dy)
2232 | l :: _ when l.pageno = pageno ->
2233 if l.pagevh != l.pageh
2234 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2235 else G.postRedisplay "downbirdseye"
2236 | _ :: rest -> loop rest
2238 loop state.layout;
2239 state.text <- E.s;
2242 let optentry mode _ key =
2243 let btos b = if b then "on" else "off" in
2244 if key >= 32 && key < 127
2245 then
2246 let c = Char.chr key in
2247 match c with
2248 | 's' ->
2249 let ondone s =
2250 try conf.scrollstep <- int_of_string s with exc ->
2251 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exc
2253 TEswitch ("scroll step: ", E.s, None, intentry, ondone, true)
2255 | 'A' ->
2256 let ondone s =
2258 conf.autoscrollstep <- boundastep state.winh (int_of_string s);
2259 if state.autoscroll <> None
2260 then state.autoscroll <- Some conf.autoscrollstep
2261 with exc ->
2262 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exc
2264 TEswitch ("auto scroll step: ", E.s, None, intentry, ondone, true)
2266 | 'C' ->
2267 let ondone s =
2269 let n, a, b = multicolumns_of_string s in
2270 setcolumns mode n a b;
2271 with exc ->
2272 state.text <- Printf.sprintf "bad columns `%s': %s" s @@ exntos exc
2274 TEswitch ("columns: ", E.s, None, textentry, ondone, true)
2276 | 'Z' ->
2277 let ondone s =
2279 let zoom = float (int_of_string s) /. 100.0 in
2280 setzoom zoom
2281 with exc ->
2282 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exc
2284 TEswitch ("zoom: ", E.s, None, intentry, ondone, true)
2286 | 't' ->
2287 let ondone s =
2289 conf.thumbw <- bound (int_of_string s) 2 4096;
2290 state.text <-
2291 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2292 begin match mode with
2293 | Birdseye beye ->
2294 leavebirdseye beye false;
2295 enterbirdseye ();
2296 | Textentry _
2297 | View
2298 | LinkNav _ -> ();
2300 with exc ->
2301 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exc
2303 TEswitch ("thumbnail width: ", E.s, None, intentry, ondone, true)
2305 | 'R' ->
2306 let ondone s =
2307 match try
2308 Some (int_of_string s)
2309 with exc ->
2310 state.text <-
2311 Printf.sprintf "bad integer `%s': %s" s @@ exntos exc;
2312 None
2313 with
2314 | Some angle -> reqlayout angle conf.fitmodel
2315 | None -> ()
2317 TEswitch ("rotation: ", E.s, None, intentry, ondone, true)
2319 | 'i' ->
2320 conf.icase <- not conf.icase;
2321 TEdone ("case insensitive search " ^ (btos conf.icase))
2323 | 'p' ->
2324 conf.preload <- not conf.preload;
2325 gotoy state.y;
2326 TEdone ("preload " ^ (btos conf.preload))
2328 | 'v' ->
2329 conf.verbose <- not conf.verbose;
2330 TEdone ("verbose " ^ (btos conf.verbose))
2332 | 'd' ->
2333 conf.debug <- not conf.debug;
2334 TEdone ("debug " ^ (btos conf.debug))
2336 | 'h' ->
2337 conf.maxhfit <- not conf.maxhfit;
2338 state.maxy <- calcheight ();
2339 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2341 | 'c' ->
2342 conf.crophack <- not conf.crophack;
2343 TEdone ("crophack " ^ btos conf.crophack)
2345 | 'a' ->
2346 let s =
2347 match conf.maxwait with
2348 | None ->
2349 conf.maxwait <- Some infinity;
2350 "always wait for page to complete"
2351 | Some _ ->
2352 conf.maxwait <- None;
2353 "show placeholder if page is not ready"
2355 TEdone s
2357 | 'f' ->
2358 conf.underinfo <- not conf.underinfo;
2359 TEdone ("underinfo " ^ btos conf.underinfo)
2361 | 'P' ->
2362 conf.savebmarks <- not conf.savebmarks;
2363 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2365 | 'S' ->
2366 let ondone s =
2368 let pageno, py =
2369 match state.layout with
2370 | [] -> 0, 0
2371 | l :: _ ->
2372 l.pageno, l.pagey
2374 conf.interpagespace <- int_of_string s;
2375 docolumns conf.columns;
2376 state.maxy <- calcheight ();
2377 let y = getpagey pageno in
2378 gotoy (y + py)
2379 with exc ->
2380 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exc
2382 TEswitch ("vertical margin: ", E.s, None, intentry, ondone, true)
2384 | 'l' ->
2385 let fm =
2386 match conf.fitmodel with
2387 | FitProportional -> FitWidth
2388 | FitWidth | FitPage -> FitProportional
2390 reqlayout conf.angle fm;
2391 TEdone ("proportional display " ^ btos (fm == FitProportional))
2393 | 'T' ->
2394 settrim (not conf.trimmargins) conf.trimfuzz;
2395 TEdone ("trim margins " ^ btos conf.trimmargins)
2397 | 'I' ->
2398 conf.invert <- not conf.invert;
2399 TEdone ("invert colors " ^ btos conf.invert)
2401 | 'x' ->
2402 let ondone s =
2403 cbput state.hists.sel s;
2404 conf.selcmd <- s;
2406 TEswitch ("selection command: ", E.s, Some (onhist state.hists.sel),
2407 textentry, ondone, true)
2409 | 'M' ->
2410 if conf.pax == None
2411 then conf.pax <- Some (ref (0.0, 0, 0))
2412 else conf.pax <- None;
2413 TEdone ("PAX " ^ btos (conf.pax != None))
2415 | _ ->
2416 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2417 TEstop
2418 else
2419 TEcont state.text
2422 class type lvsource = object
2423 method getitemcount : int
2424 method getitem : int -> (string * int)
2425 method hasaction : int -> bool
2426 method exit :
2427 uioh:uioh ->
2428 cancel:bool ->
2429 active:int ->
2430 first:int ->
2431 pan:int ->
2432 uioh option
2433 method getactive : int
2434 method getfirst : int
2435 method getpan : int
2436 method getminfo : (int * int) array
2437 end;;
2439 class virtual lvsourcebase = object
2440 val mutable m_active = 0
2441 val mutable m_first = 0
2442 val mutable m_pan = 0
2443 method getactive = m_active
2444 method getfirst = m_first
2445 method getpan = m_pan
2446 method getminfo : (int * int) array = E.a
2447 end;;
2449 let textentrykeyboard
2450 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
2451 state.text <- E.s;
2452 let key =
2453 if key >= 0xffb0 && key <= 0xffb9
2454 then key - 0xffb0 + 48 else key
2456 let enttext te =
2457 state.mode <- Textentry (te, onleave);
2458 enttext ();
2459 G.postRedisplay "textentrykeyboard enttext";
2461 let histaction cmd =
2462 match opthist with
2463 | None -> ()
2464 | Some (action, _) ->
2465 state.mode <- Textentry (
2466 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
2468 G.postRedisplay "textentry histaction"
2470 match key with
2471 | @backspace ->
2472 if emptystr text && cancelonempty
2473 then (
2474 onleave Cancel;
2475 G.postRedisplay "textentrykeyboard after cancel";
2477 else
2478 let s = withoutlastutf8 text in
2479 enttext (c, s, opthist, onkey, ondone, cancelonempty)
2481 | @enter | @kpenter ->
2482 ondone text;
2483 onleave Confirm;
2484 G.postRedisplay "textentrykeyboard after confirm"
2486 | @up | @kpup -> histaction HCprev
2487 | @down | @kpdown -> histaction HCnext
2488 | @home | @kphome -> histaction HCfirst
2489 | @jend | @kpend -> histaction HClast
2491 | @escape ->
2492 if emptystr text
2493 then (
2494 begin match opthist with
2495 | None -> ()
2496 | Some (_, onhistcancel) -> onhistcancel ()
2497 end;
2498 onleave Cancel;
2499 state.text <- E.s;
2500 G.postRedisplay "textentrykeyboard after cancel2"
2502 else (
2503 enttext (c, E.s, opthist, onkey, ondone, cancelonempty)
2506 | @delete | @kpdelete -> ()
2508 | _ when key != 0
2509 && key land 0xff00 != 0xff00 (* keyboard *)
2510 && key land 0xfe00 != 0xfe00 (* xkb *)
2511 && key land 0xfd00 != 0xfd00 (* 3270 *)
2513 begin match onkey text key with
2514 | TEdone text ->
2515 ondone text;
2516 onleave Confirm;
2517 G.postRedisplay "textentrykeyboard after confirm2";
2519 | TEcont text ->
2520 enttext (c, text, opthist, onkey, ondone, cancelonempty);
2522 | TEstop ->
2523 onleave Cancel;
2524 G.postRedisplay "textentrykeyboard after cancel3"
2526 | TEswitch te ->
2527 state.mode <- Textentry (te, onleave);
2528 G.postRedisplay "textentrykeyboard switch";
2529 end;
2531 | _ ->
2532 vlog "unhandled key %s" (Wsi.keyname key)
2535 let firstof first active =
2536 if first > active || abs (first - active) > fstate.maxrows - 1
2537 then max 0 (active - (fstate.maxrows/2))
2538 else first
2541 let calcfirst first active =
2542 if active > first
2543 then
2544 let rows = active - first in
2545 if rows > fstate.maxrows then active - fstate.maxrows else first
2546 else active
2549 let scrollph y maxy =
2550 let sh = float (maxy + state.winh) /. float state.winh in
2551 let sh = float state.winh /. sh in
2552 let sh = max sh (float conf.scrollh) in
2554 let percent = float y /. float maxy in
2555 let position = (float state.winh -. sh) *. percent in
2557 let position =
2558 if position +. sh > float state.winh
2559 then float state.winh -. sh
2560 else position
2562 position, sh;
2565 let coe s = (s :> uioh);;
2567 class listview ~zebra ~helpmode ~(source:lvsource) ~trusted ~modehash =
2568 object (self)
2569 val m_pan = source#getpan
2570 val m_first = source#getfirst
2571 val m_active = source#getactive
2572 val m_qsearch = E.s
2573 val m_prev_uioh = state.uioh
2575 method private elemunder y =
2576 if y < 0
2577 then None
2578 else
2579 let n = y / (fstate.fontsize+1) in
2580 if m_first + n < source#getitemcount
2581 then (
2582 if source#hasaction (m_first + n)
2583 then Some (m_first + n)
2584 else None
2586 else None
2588 method display =
2589 Gl.enable `blend;
2590 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
2591 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2592 filledrect 0. 0. (float state.winw) (float state.winh);
2593 GlDraw.color (1., 1., 1.);
2594 Gl.enable `texture_2d;
2595 let fs = fstate.fontsize in
2596 let nfs = fs + 1 in
2597 let hw = (wadjsb () + xadjsb () + state.winw)/3 in
2598 let ww = fstate.wwidth in
2599 let tabw = 17.0*.ww in
2600 let itemcount = source#getitemcount in
2601 let minfo = source#getminfo in
2602 let x0, x1 =
2603 if conf.leftscroll
2604 then float (xadjsb ()), float (state.winw - 1)
2605 else 0.0, float (state.winw - conf.scrollbw - 1)
2607 let xadj = xadjsb () in
2608 let rec loop row =
2609 if (row - m_first) > fstate.maxrows
2610 then ()
2611 else (
2612 if row >= 0 && row < itemcount
2613 then (
2614 let (s, level) = source#getitem row in
2615 let y = (row - m_first) * nfs in
2616 let x =
2617 (if conf.leftscroll then float xadj else 5.0)
2618 +. (float (level + m_pan)) *. ww in
2619 if helpmode
2620 then GlDraw.color
2621 (let c = if row land 1 = 0 then 1.0 else 0.92 in (c,c,c));
2623 if row = m_active
2624 then (
2625 Gl.disable `texture_2d;
2626 let alpha = if source#hasaction row then 0.9 else 0.3 in
2627 GlDraw.color (1., 1., 1.) ~alpha;
2628 linerect (x0 +. 1.) (float (y + 1)) (x1) (float (y + fs + 3));
2629 Gl.enable `texture_2d;
2631 let c =
2632 if zebra && row land 1 = 1
2633 then 0.8
2634 else 1.0
2636 GlDraw.color (c,c,c);
2637 let drawtabularstring s =
2638 let drawstr x s =
2639 let x' = truncate (x0 +. x) in
2640 let pos = nindex s '\000' in
2641 if pos = -1
2642 then drawstring1 fs x' (y+nfs) s
2643 else
2644 let s1 = String.sub s 0 pos
2645 and s2 = String.sub s (pos+1) (String.length s - pos - 1) in
2646 let rec e s =
2647 if emptystr s
2648 then s
2649 else
2650 let s' = withoutlastutf8 s in
2651 let s = s' ^ "@Uellipsis" in
2652 let w = measurestr fs s in
2653 if float x' +. w +. ww < float (hw + x')
2654 then s
2655 else e s'
2657 let s1 =
2658 if float x' +. ww +. measurestr fs s1 > float (hw + x')
2659 then e s1
2660 else s1
2662 ignore (drawstring1 fs x' (y+nfs) s1);
2663 drawstring1 fs (hw + x') (y+nfs) s2
2665 if trusted
2666 then
2667 let x = if helpmode && row > 0 then x +. ww else x in
2668 let tabpos = nindex s '\t' in
2669 if tabpos > 0
2670 then
2671 let len = String.length s - tabpos - 1 in
2672 let s1 = String.sub s 0 tabpos
2673 and s2 = String.sub s (tabpos + 1) len in
2674 let nx = drawstr x s1 in
2675 let sw = nx -. x in
2676 let x = x +. (max tabw sw) in
2677 drawstr x s2
2678 else
2679 let len = String.length s - 2 in
2680 if len > 0 && s.[0] = '\xc2' && s.[1] = '\xb7'
2681 then
2682 let s = String.sub s 2 len in
2683 let x = if not helpmode then x +. ww else x in
2684 GlDraw.color (1.2, 1.2, 1.2);
2685 let vinc = drawstring1 (fs+fs/4)
2686 (truncate (x -. ww)) (y+nfs) s in
2687 GlDraw.color (1., 1., 1.);
2688 vinc +. (float fs *. 0.8)
2689 else
2690 drawstr x s
2691 else
2692 drawstr x s
2694 ignore (drawtabularstring s);
2695 loop (row+1)
2699 loop m_first;
2700 GlDraw.color (1.0, 1.0, 1.0) ~alpha:0.5;
2701 let xadj = float (xadjsb () + 5) in
2702 let rec loop row =
2703 if (row - m_first) > fstate.maxrows
2704 then ()
2705 else (
2706 if row >= 0 && row < itemcount
2707 then (
2708 let (s, level) = source#getitem row in
2709 let pos0 = nindex s '\000' in
2710 let y = (row - m_first) * nfs in
2711 let x = float (level + m_pan) *. ww in
2712 let (first, last) = minfo.(row) in
2713 let prefix =
2714 if pos0 > 0 && first > pos0
2715 then String.sub s (pos0+1) (first-pos0-1)
2716 else String.sub s 0 first
2718 let suffix = String.sub s first (last - first) in
2719 let w1 = measurestr fstate.fontsize prefix in
2720 let w2 = measurestr fstate.fontsize suffix in
2721 let x = x +. if conf.leftscroll then xadj else 5.0 in
2722 let x = if pos0 > 0 && first > pos0 then x +. float hw else x in
2723 let x0 = x +. w1
2724 and y0 = float (y+2) in
2725 let x1 = x0 +. w2
2726 and y1 = float (y+fs+3) in
2727 filledrect x0 y0 x1 y1;
2728 loop (row+1)
2732 Gl.disable `texture_2d;
2733 if Array.length minfo > 0 then loop m_first;
2734 Gl.disable `blend;
2736 method updownlevel incr =
2737 let len = source#getitemcount in
2738 let curlevel =
2739 if m_active >= 0 && m_active < len
2740 then snd (source#getitem m_active)
2741 else -1
2743 let rec flow i =
2744 if i = len then i-1 else if i = -1 then 0 else
2745 let _, l = source#getitem i in
2746 if l != curlevel then i else flow (i+incr)
2748 let active = flow m_active in
2749 let first = calcfirst m_first active in
2750 G.postRedisplay "outline updownlevel";
2751 {< m_active = active; m_first = first >}
2753 method private key1 key mask =
2754 let set1 active first qsearch =
2755 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2757 let search active pattern incr =
2758 let active = if active = -1 then m_first else active in
2759 let dosearch re =
2760 let rec loop n =
2761 if n >= 0 && n < source#getitemcount
2762 then (
2763 let s, _ = source#getitem n in
2764 match Str.search_forward re s 0 with
2765 | (exception Not_found) -> loop (n + incr)
2766 | _ -> Some n
2768 else None
2770 loop active
2772 Str.regexp_case_fold pattern |> dosearch
2774 let itemcount = source#getitemcount in
2775 let find start incr =
2776 let rec find i =
2777 if i = -1 || i = itemcount
2778 then -1
2779 else (
2780 if source#hasaction i
2781 then i
2782 else find (i + incr)
2785 find start
2787 let set active first =
2788 let first = bound first 0 (itemcount - fstate.maxrows) in
2789 state.text <- E.s;
2790 coe {< m_active = active; m_first = first; m_qsearch = E.s >}
2792 let navigate incr =
2793 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2794 let active, first =
2795 let incr1 = if incr > 0 then 1 else -1 in
2796 if isvisible m_first m_active
2797 then
2798 let next =
2799 let next = m_active + incr in
2800 let next =
2801 if next < 0 || next >= itemcount
2802 then -1
2803 else find next incr1
2805 if abs (m_active - next) > fstate.maxrows
2806 then -1
2807 else next
2809 if next = -1
2810 then
2811 let first = m_first + incr in
2812 let first = bound first 0 (itemcount - fstate.maxrows) in
2813 let next =
2814 let next = m_active + incr in
2815 let next = bound next 0 (itemcount - 1) in
2816 find next ~-incr1
2818 let active =
2819 if next = -1
2820 then m_active
2821 else (
2822 if isvisible first next
2823 then next
2824 else m_active
2827 active, first
2828 else
2829 let first = min next m_first in
2830 let first =
2831 if abs (next - first) > fstate.maxrows
2832 then first + incr
2833 else first
2835 next, first
2836 else
2837 let first = m_first + incr in
2838 let first = bound first 0 (itemcount - 1) in
2839 let active =
2840 let next = m_active + incr in
2841 let next = bound next 0 (itemcount - 1) in
2842 let next = find next incr1 in
2843 let active =
2844 if next = -1 || abs (m_active - first) > fstate.maxrows
2845 then (
2846 let active = if m_active = -1 then next else m_active in
2847 active
2849 else next
2851 if isvisible first active
2852 then active
2853 else -1
2855 active, first
2857 G.postRedisplay "listview navigate";
2858 set active first;
2860 match key with
2861 | (@r|@s) when Wsi.withctrl mask ->
2862 let incr = if key = @r then -1 else 1 in
2863 let active, first =
2864 match search (m_active + incr) m_qsearch incr with
2865 | None ->
2866 state.text <- m_qsearch ^ " [not found]";
2867 m_active, m_first
2868 | Some active ->
2869 state.text <- m_qsearch;
2870 active, firstof m_first active
2872 G.postRedisplay "listview ctrl-r/s";
2873 set1 active first m_qsearch;
2875 | @insert when Wsi.withctrl mask ->
2876 if m_active >= 0 && m_active < source#getitemcount
2877 then (
2878 let s, _ = source#getitem m_active in
2879 selstring s;
2881 coe self
2883 | @backspace ->
2884 if emptystr m_qsearch
2885 then coe self
2886 else (
2887 let qsearch = withoutlastutf8 m_qsearch in
2888 if emptystr qsearch
2889 then (
2890 state.text <- E.s;
2891 G.postRedisplay "listview empty qsearch";
2892 set1 m_active m_first E.s;
2894 else
2895 let active, first =
2896 match search m_active qsearch ~-1 with
2897 | None ->
2898 state.text <- qsearch ^ " [not found]";
2899 m_active, m_first
2900 | Some active ->
2901 state.text <- qsearch;
2902 active, firstof m_first active
2904 G.postRedisplay "listview backspace qsearch";
2905 set1 active first qsearch
2908 | key when (key != 0 && key land 0xff00 != 0xff00) ->
2909 let pattern = m_qsearch ^ toutf8 key in
2910 let active, first =
2911 match search m_active pattern 1 with
2912 | None ->
2913 state.text <- pattern ^ " [not found]";
2914 m_active, m_first
2915 | Some active ->
2916 state.text <- pattern;
2917 active, firstof m_first active
2919 G.postRedisplay "listview qsearch add";
2920 set1 active first pattern;
2922 | @escape ->
2923 state.text <- E.s;
2924 if emptystr m_qsearch
2925 then (
2926 G.postRedisplay "list view escape";
2927 let mx, my = state.mpos in
2928 updateunder mx my;
2929 begin
2930 match
2931 source#exit ~uioh:(coe self)
2932 ~cancel:true ~active:m_active ~first:m_first ~pan:m_pan
2933 with
2934 | None -> m_prev_uioh
2935 | Some uioh -> uioh
2938 else (
2939 G.postRedisplay "list view kill qsearch";
2940 coe {< m_qsearch = E.s >}
2943 | @enter | @kpenter ->
2944 state.text <- E.s;
2945 let self = {< m_qsearch = E.s >} in
2946 let opt =
2947 G.postRedisplay "listview enter";
2948 if m_active >= 0 && m_active < source#getitemcount
2949 then (
2950 source#exit ~uioh:(coe self) ~cancel:false
2951 ~active:m_active ~first:m_first ~pan:m_pan;
2953 else (
2954 source#exit ~uioh:(coe self) ~cancel:true
2955 ~active:m_active ~first:m_first ~pan:m_pan;
2958 begin match opt with
2959 | None -> m_prev_uioh
2960 | Some uioh -> uioh
2963 | @delete | @kpdelete ->
2964 coe self
2966 | @up | @kpup -> navigate ~-1
2967 | @down | @kpdown -> navigate 1
2968 | @prior | @kpprior -> navigate ~-(fstate.maxrows)
2969 | @next | @kpnext -> navigate fstate.maxrows
2971 | @right | @kpright ->
2972 state.text <- E.s;
2973 G.postRedisplay "listview right";
2974 coe {< m_pan = m_pan - 1 >}
2976 | @left | @kpleft ->
2977 state.text <- E.s;
2978 G.postRedisplay "listview left";
2979 coe {< m_pan = m_pan + 1 >}
2981 | @home | @kphome ->
2982 let active = find 0 1 in
2983 G.postRedisplay "listview home";
2984 set active 0;
2986 | @jend | @kpend ->
2987 let first = max 0 (itemcount - fstate.maxrows) in
2988 let active = find (itemcount - 1) ~-1 in
2989 G.postRedisplay "listview end";
2990 set active first;
2992 | key when (key = 0 || key land 0xff00 = 0xff00) ->
2993 coe self
2995 | _ ->
2996 dolog "listview unknown key %#x" key; coe self
2998 method key key mask =
2999 match state.mode with
3000 | Textentry te -> textentrykeyboard key mask te; coe self
3001 | Birdseye _
3002 | View
3003 | LinkNav _ -> self#key1 key mask
3005 method button button down x y _ =
3006 let opt =
3007 match button with
3008 | 1 when vscrollhit x ->
3009 G.postRedisplay "listview scroll";
3010 if down
3011 then
3012 let _, position, sh = self#scrollph in
3013 if y > truncate position && y < truncate (position +. sh)
3014 then (
3015 state.mstate <- Mscrolly;
3016 Some (coe self)
3018 else
3019 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3020 let first = truncate (s *. float source#getitemcount) in
3021 let first = min source#getitemcount first in
3022 Some (coe {< m_first = first; m_active = first >})
3023 else (
3024 state.mstate <- Mnone;
3025 Some (coe self);
3027 | 1 when down ->
3028 begin match self#elemunder y with
3029 | Some n ->
3030 G.postRedisplay "listview click";
3031 source#exit ~uioh:(coe {< m_active = n >})
3032 ~cancel:false ~active:n ~first:m_first ~pan:m_pan
3033 | _ ->
3034 Some (coe self)
3036 | n when (n == 4 || n == 5) && not down ->
3037 let len = source#getitemcount in
3038 let first =
3039 if n = 5 && m_first + fstate.maxrows >= len
3040 then
3041 m_first
3042 else
3043 let first = m_first + (if n == 4 then -1 else 1) in
3044 bound first 0 (len - 1)
3046 G.postRedisplay "listview wheel";
3047 Some (coe {< m_first = first >})
3048 | n when (n = 6 || n = 7) && not down ->
3049 let inc = if n = 7 then -1 else 1 in
3050 G.postRedisplay "listview hwheel";
3051 Some (coe {< m_pan = m_pan + inc >})
3052 | _ ->
3053 Some (coe self)
3055 match opt with
3056 | None -> m_prev_uioh
3057 | Some uioh -> uioh
3059 method multiclick _ x y = self#button 1 true x y
3061 method motion _ y =
3062 match state.mstate with
3063 | Mscrolly ->
3064 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3065 let first = truncate (s *. float source#getitemcount) in
3066 let first = min source#getitemcount first in
3067 G.postRedisplay "listview motion";
3068 coe {< m_first = first; m_active = first >}
3069 | Msel _
3070 | Mpan _
3071 | Mscrollx
3072 | Mzoom _
3073 | Mzoomrect _
3074 | Mnone -> coe self
3076 method pmotion x y =
3077 if x < state.winw - conf.scrollbw
3078 then
3079 let n =
3080 match self#elemunder y with
3081 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3082 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3084 let o =
3085 if n != m_active
3086 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3087 else self
3089 coe o
3090 else (
3091 Wsi.setcursor Wsi.CURSOR_INHERIT;
3092 coe self
3095 method infochanged _ = ()
3097 method scrollpw = (0, 0.0, 0.0)
3098 method scrollph =
3099 let nfs = fstate.fontsize + 1 in
3100 let y = m_first * nfs in
3101 let itemcount = source#getitemcount in
3102 let maxi = max 0 (itemcount - fstate.maxrows) in
3103 let maxy = maxi * nfs in
3104 let p, h = scrollph y maxy in
3105 conf.scrollbw, p, h
3107 method modehash = modehash
3108 method eformsgs = false
3109 method alwaysscrolly = true
3110 end;;
3112 class outlinelistview ~zebra ~source =
3113 let settext autonarrow s =
3114 if autonarrow
3115 then
3116 let ss = source#statestr in
3117 state.text <-
3118 if emptystr ss
3119 then "[" ^ s ^ "]"
3120 else "{" ^ ss ^ "} [" ^ s ^ "]"
3121 else state.text <- s
3123 object (self)
3124 inherit listview
3125 ~zebra
3126 ~helpmode:false
3127 ~source:(source :> lvsource)
3128 ~trusted:false
3129 ~modehash:(findkeyhash conf "outline")
3130 as super
3132 val m_autonarrow = false
3134 method! key key mask =
3135 let maxrows =
3136 if emptystr state.text
3137 then fstate.maxrows
3138 else fstate.maxrows - 2
3140 let calcfirst first active =
3141 if active > first
3142 then
3143 let rows = active - first in
3144 if rows > maxrows then active - maxrows else first
3145 else active
3147 let navigate incr =
3148 let active = m_active + incr in
3149 let active = bound active 0 (source#getitemcount - 1) in
3150 let first = calcfirst m_first active in
3151 G.postRedisplay "outline navigate";
3152 coe {< m_active = active; m_first = first >}
3154 let navscroll first =
3155 let active =
3156 let dist = m_active - first in
3157 if dist < 0
3158 then first
3159 else (
3160 if dist < maxrows
3161 then m_active
3162 else first + maxrows
3165 G.postRedisplay "outline navscroll";
3166 coe {< m_first = first; m_active = active >}
3168 let ctrl = Wsi.withctrl mask in
3169 match key with
3170 | @a when ctrl ->
3171 let text =
3172 if m_autonarrow
3173 then (source#denarrow; E.s)
3174 else (
3175 let pattern = source#renarrow in
3176 if nonemptystr m_qsearch
3177 then (source#narrow m_qsearch; m_qsearch)
3178 else pattern
3181 settext (not m_autonarrow) text;
3182 G.postRedisplay "toggle auto narrowing";
3183 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
3185 | @slash when emptystr m_qsearch && not m_autonarrow ->
3186 settext true E.s;
3187 G.postRedisplay "toggle auto narrowing";
3188 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
3190 | @n when ctrl ->
3191 source#narrow m_qsearch;
3192 if not m_autonarrow
3193 then source#add_narrow_pattern m_qsearch;
3194 G.postRedisplay "outline ctrl-n";
3195 coe {< m_first = 0; m_active = 0 >}
3197 | @S when ctrl ->
3198 let active = source#calcactive (getanchor ()) in
3199 let first = firstof m_first active in
3200 G.postRedisplay "outline ctrl-s";
3201 coe {< m_first = first; m_active = active >}
3203 | @u when ctrl ->
3204 G.postRedisplay "outline ctrl-u";
3205 if m_autonarrow && nonemptystr m_qsearch
3206 then (
3207 ignore (source#renarrow);
3208 settext m_autonarrow E.s;
3209 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3211 else (
3212 source#del_narrow_pattern;
3213 let pattern = source#renarrow in
3214 let text =
3215 if emptystr pattern then E.s else "Narrowed to " ^ pattern
3217 settext m_autonarrow text;
3218 coe {< m_first = 0; m_active = 0; m_qsearch = E.s >}
3221 | @l when ctrl ->
3222 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3223 G.postRedisplay "outline ctrl-l";
3224 coe {< m_first = first >}
3226 | @tab when m_autonarrow ->
3227 if nonemptystr m_qsearch
3228 then (
3229 G.postRedisplay "outline list view tab";
3230 source#add_narrow_pattern m_qsearch;
3231 settext true E.s;
3232 coe {< m_qsearch = E.s >}
3234 else coe self
3236 | @escape when m_autonarrow ->
3237 if nonemptystr m_qsearch
3238 then source#add_narrow_pattern m_qsearch;
3239 super#key key mask
3241 | @enter | @kpenter when m_autonarrow ->
3242 if nonemptystr m_qsearch
3243 then source#add_narrow_pattern m_qsearch;
3244 super#key key mask
3246 | key when m_autonarrow && (key != 0 && key land 0xff00 != 0xff00) ->
3247 let pattern = m_qsearch ^ toutf8 key in
3248 G.postRedisplay "outlinelistview autonarrow add";
3249 source#narrow pattern;
3250 settext true pattern;
3251 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3253 | key when m_autonarrow && key = @backspace ->
3254 if emptystr m_qsearch
3255 then coe self
3256 else
3257 let pattern = withoutlastutf8 m_qsearch in
3258 G.postRedisplay "outlinelistview autonarrow backspace";
3259 ignore (source#renarrow);
3260 source#narrow pattern;
3261 settext true pattern;
3262 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3264 | @up | @kpup when ctrl ->
3265 navscroll (max 0 (m_first - 1))
3267 | @down | @kpdown when ctrl ->
3268 navscroll (min (source#getitemcount - 1) (m_first + 1))
3270 | @up | @kpup -> navigate ~-1
3271 | @down | @kpdown -> navigate 1
3272 | @prior | @kpprior -> navigate ~-(fstate.maxrows)
3273 | @next | @kpnext -> navigate fstate.maxrows
3275 | @right | @kpright ->
3276 let o =
3277 if ctrl
3278 then (
3279 G.postRedisplay "outline ctrl right";
3280 {< m_pan = m_pan + 1 >}
3282 else self#updownlevel 1
3284 coe o
3286 | @left | @kpleft ->
3287 let o =
3288 if ctrl
3289 then (
3290 G.postRedisplay "outline ctrl left";
3291 {< m_pan = m_pan - 1 >}
3293 else self#updownlevel ~-1
3295 coe o
3297 | @home | @kphome ->
3298 G.postRedisplay "outline home";
3299 coe {< m_first = 0; m_active = 0 >}
3301 | @jend | @kpend ->
3302 let active = source#getitemcount - 1 in
3303 let first = max 0 (active - fstate.maxrows) in
3304 G.postRedisplay "outline end";
3305 coe {< m_active = active; m_first = first >}
3307 | _ -> super#key key mask
3308 end;;
3310 let genhistoutlines () =
3311 Config.gethist ()
3312 |> List.sort (fun (_, c1, _, _, _, _) (_, c2, _, _, _, _) ->
3313 compare c2.lastvisit c1.lastvisit)
3314 |> List.map
3315 (fun ((path, c, _, _, _, origin) as hist) ->
3316 let path = if nonemptystr origin then origin else path in
3317 let base = mbtoutf8 @@ Filename.basename path in
3318 (base ^ "\000" ^ c.title, 1, Ohistory hist)
3320 |> Array.of_list
3323 let gotohist (path, c, bookmarks, x, anchor, origin) =
3324 Config.save leavebirdseye;
3325 state.anchor <- anchor;
3326 state.bookmarks <- bookmarks;
3327 state.origin <- origin;
3328 state.x <- x;
3329 setconf conf c;
3330 let x0, y0, x1, y1 = conf.trimfuzz in
3331 wcmd "trimset %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1;
3332 reshape ~firsttime:true state.winw state.winh;
3333 opendoc path origin;
3334 setzoom c.zoom;
3337 let makecheckers () =
3338 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3339 following to say:
3340 converted by Issac Trotts. July 25, 2002 *)
3341 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
3342 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
3343 let id = GlTex.gen_texture () in
3344 GlTex.bind_texture ~target:`texture_2d id;
3345 GlPix.store (`unpack_alignment 1);
3346 GlTex.image2d image;
3347 List.iter (GlTex.parameter ~target:`texture_2d)
3348 [ `mag_filter `nearest; `min_filter `nearest ];
3352 let setcheckers enabled =
3353 match state.checkerstexid with
3354 | None ->
3355 if enabled then state.checkerstexid <- Some (makecheckers ())
3357 | Some checkerstexid ->
3358 if not enabled
3359 then (
3360 GlTex.delete_texture checkerstexid;
3361 state.checkerstexid <- None;
3365 let describe_location () =
3366 let fn = page_of_y state.y in
3367 let ln = page_of_y (state.y + state.winh - hscrollh () - 1) in
3368 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
3369 let percent =
3370 if maxy <= 0
3371 then 100.
3372 else (100. *. (float state.y /. float maxy))
3374 if fn = ln
3375 then
3376 Printf.sprintf "page %d of %d [%.2f%%]"
3377 (fn+1) state.pagecount percent
3378 else
3379 Printf.sprintf
3380 "pages %d-%d of %d [%.2f%%]"
3381 (fn+1) (ln+1) state.pagecount percent
3384 let setpresentationmode v =
3385 let n = page_of_y state.y in
3386 state.anchor <- (n, 0.0, 1.0);
3387 conf.presentation <- v;
3388 if conf.fitmodel = FitPage
3389 then reqlayout conf.angle conf.fitmodel;
3390 represent ();
3393 let setbgcol (r, g, b) =
3394 let col =
3395 let r = r *. 255.0 |> truncate
3396 and g = g *. 255.0 |> truncate
3397 and b = b *. 255.0 |> truncate in
3398 r lsl 16 |> (lor) (g lsl 8) |> (lor) b
3400 Wsi.setwinbgcol col;
3403 let enterinfomode =
3404 let btos b = if b then "@Uradical" else E.s in
3405 let showextended = ref false in
3406 let leave mode _ = state.mode <- mode in
3407 let src =
3408 (object
3409 val mutable m_l = []
3410 val mutable m_a = E.a
3411 val mutable m_prev_uioh = nouioh
3412 val mutable m_prev_mode = View
3414 inherit lvsourcebase
3416 method reset prev_mode prev_uioh =
3417 m_a <- Array.of_list (List.rev m_l);
3418 m_l <- [];
3419 m_prev_mode <- prev_mode;
3420 m_prev_uioh <- prev_uioh;
3422 method int name get set =
3423 m_l <-
3424 (name, `int get, 1, Action (
3425 fun u ->
3426 let ondone s =
3427 try set (int_of_string s)
3428 with exn ->
3429 state.text <- Printf.sprintf "bad integer `%s': %s"
3430 s @@ exntos exn
3432 state.text <- E.s;
3433 let te = name ^ ": ", E.s, None, intentry, ondone, true in
3434 state.mode <- Textentry (te, leave m_prev_mode);
3436 )) :: m_l
3438 method int_with_suffix name get set =
3439 m_l <-
3440 (name, `intws get, 1, Action (
3441 fun u ->
3442 let ondone s =
3443 try set (int_of_string_with_suffix s)
3444 with exn ->
3445 state.text <- Printf.sprintf "bad integer `%s': %s"
3446 s @@ exntos exn
3448 state.text <- E.s;
3449 let te =
3450 name ^ ": ", E.s, None, intentry_with_suffix, ondone, true
3452 state.mode <- Textentry (te, leave m_prev_mode);
3454 )) :: m_l
3456 method bool ?(offset=1) ?(btos=btos) name get set =
3457 m_l <-
3458 (name, `bool (btos, get), offset, Action (
3459 fun u ->
3460 let v = get () in
3461 set (not v);
3463 )) :: m_l
3465 method color name get set =
3466 m_l <-
3467 (name, `color get, 1, Action (
3468 fun u ->
3469 let invalid = (nan, nan, nan) in
3470 let ondone s =
3471 let c =
3472 try color_of_string s
3473 with exn ->
3474 state.text <- Printf.sprintf "bad color `%s': %s"
3475 s @@ exntos exn;
3476 invalid
3478 if c <> invalid
3479 then set c;
3481 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3482 state.text <- color_to_string (get ());
3483 state.mode <- Textentry (te, leave m_prev_mode);
3485 )) :: m_l
3487 method string name get set =
3488 m_l <-
3489 (name, `string get, 1, Action (
3490 fun u ->
3491 let ondone s = set s in
3492 let te = name ^ ": ", E.s, None, textentry, ondone, true in
3493 state.mode <- Textentry (te, leave m_prev_mode);
3495 )) :: m_l
3497 method colorspace name get set =
3498 m_l <-
3499 (name, `string get, 1, Action (
3500 fun _ ->
3501 let source =
3502 (object
3503 inherit lvsourcebase
3505 initializer
3506 m_active <- CSTE.to_int conf.colorspace;
3507 m_first <- 0;
3509 method getitemcount =
3510 Array.length CSTE.names
3511 method getitem n =
3512 (CSTE.names.(n), 0)
3513 method exit ~uioh ~cancel ~active ~first ~pan =
3514 ignore (uioh, first, pan);
3515 if not cancel then set active;
3516 None
3517 method hasaction _ = true
3518 end)
3520 state.text <- E.s;
3521 let modehash = findkeyhash conf "info" in
3522 coe (new listview ~zebra:false ~helpmode:false
3523 ~source ~trusted:true ~modehash)
3524 )) :: m_l
3526 method paxmark name get set =
3527 m_l <-
3528 (name, `string get, 1, Action (
3529 fun _ ->
3530 let source =
3531 (object
3532 inherit lvsourcebase
3534 initializer
3535 m_active <- MTE.to_int conf.paxmark;
3536 m_first <- 0;
3538 method getitemcount = Array.length MTE.names
3539 method getitem n = (MTE.names.(n), 0)
3540 method exit ~uioh ~cancel ~active ~first ~pan =
3541 ignore (uioh, first, pan);
3542 if not cancel then set active;
3543 None
3544 method hasaction _ = true
3545 end)
3547 state.text <- E.s;
3548 let modehash = findkeyhash conf "info" in
3549 coe (new listview ~zebra:false ~helpmode:false
3550 ~source ~trusted:true ~modehash)
3551 )) :: m_l
3553 method fitmodel name get set =
3554 m_l <-
3555 (name, `string get, 1, Action (
3556 fun _ ->
3557 let source =
3558 (object
3559 inherit lvsourcebase
3561 initializer
3562 m_active <- FMTE.to_int conf.fitmodel;
3563 m_first <- 0;
3565 method getitemcount = Array.length FMTE.names
3566 method getitem n = (FMTE.names.(n), 0)
3567 method exit ~uioh ~cancel ~active ~first ~pan =
3568 ignore (uioh, first, pan);
3569 if not cancel then set active;
3570 None
3571 method hasaction _ = true
3572 end)
3574 state.text <- E.s;
3575 let modehash = findkeyhash conf "info" in
3576 coe (new listview ~zebra:false ~helpmode:false
3577 ~source ~trusted:true ~modehash)
3578 )) :: m_l
3580 method caption s offset =
3581 m_l <- (s, `empty, offset, Noaction) :: m_l
3583 method caption2 s f offset =
3584 m_l <- (s, `string f, offset, Noaction) :: m_l
3586 method getitemcount = Array.length m_a
3588 method getitem n =
3589 let tostr = function
3590 | `int f -> string_of_int (f ())
3591 | `intws f -> string_with_suffix_of_int (f ())
3592 | `string f -> f ()
3593 | `color f -> color_to_string (f ())
3594 | `bool (btos, f) -> btos (f ())
3595 | `empty -> E.s
3597 let name, t, offset, _ = m_a.(n) in
3598 ((let s = tostr t in
3599 if nonemptystr s
3600 then Printf.sprintf "%s\t%s" name s
3601 else name),
3602 offset)
3604 method exit ~uioh ~cancel ~active ~first ~pan =
3605 let uiohopt =
3606 if not cancel
3607 then (
3608 let uioh =
3609 match m_a.(active) with
3610 | _, _, _, Action f -> f uioh
3611 | _, _, _, Noaction -> uioh
3613 Some uioh
3615 else None
3617 m_active <- active;
3618 m_first <- first;
3619 m_pan <- pan;
3620 uiohopt
3622 method hasaction n =
3623 match m_a.(n) with
3624 | _, _, _, Action _ -> true
3625 | _, _, _, Noaction -> false
3627 initializer m_active <- 1
3628 end)
3630 let rec fillsrc prevmode prevuioh =
3631 let sep () = src#caption E.s 0 in
3632 let colorp name get set =
3633 src#string name
3634 (fun () -> color_to_string (get ()))
3635 (fun v ->
3637 let c = color_of_string v in
3638 set c
3639 with exn ->
3640 state.text <- Printf.sprintf "bad color `%s': %s" v @@ exntos exn
3643 let oldmode = state.mode in
3644 let birdseye = isbirdseye state.mode in
3646 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3648 src#bool "presentation mode"
3649 (fun () -> conf.presentation)
3650 (fun v -> setpresentationmode v);
3652 src#bool "ignore case in searches"
3653 (fun () -> conf.icase)
3654 (fun v -> conf.icase <- v);
3656 src#bool "preload"
3657 (fun () -> conf.preload)
3658 (fun v -> conf.preload <- v);
3660 src#bool "highlight links"
3661 (fun () -> conf.hlinks)
3662 (fun v -> conf.hlinks <- v);
3664 src#bool "under info"
3665 (fun () -> conf.underinfo)
3666 (fun v -> conf.underinfo <- v);
3668 src#bool "persistent bookmarks"
3669 (fun () -> conf.savebmarks)
3670 (fun v -> conf.savebmarks <- v);
3672 src#fitmodel "fit model"
3673 (fun () -> FMTE.to_string conf.fitmodel)
3674 (fun v -> reqlayout conf.angle (FMTE.of_int v));
3676 src#bool "trim margins"
3677 (fun () -> conf.trimmargins)
3678 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3680 src#bool "persistent location"
3681 (fun () -> conf.jumpback)
3682 (fun v -> conf.jumpback <- v);
3684 sep ();
3685 src#int "inter-page space"
3686 (fun () -> conf.interpagespace)
3687 (fun n ->
3688 conf.interpagespace <- n;
3689 docolumns conf.columns;
3690 let pageno, py =
3691 match state.layout with
3692 | [] -> 0, 0
3693 | l :: _ ->
3694 l.pageno, l.pagey
3696 state.maxy <- calcheight ();
3697 let y = getpagey pageno in
3698 gotoy (y + py)
3701 src#int "page bias"
3702 (fun () -> conf.pagebias)
3703 (fun v -> conf.pagebias <- v);
3705 src#int "scroll step"
3706 (fun () -> conf.scrollstep)
3707 (fun n -> conf.scrollstep <- n);
3709 src#int "horizontal scroll step"
3710 (fun () -> conf.hscrollstep)
3711 (fun v -> conf.hscrollstep <- v);
3713 src#int "auto scroll step"
3714 (fun () ->
3715 match state.autoscroll with
3716 | Some step -> step
3717 | _ -> conf.autoscrollstep)
3718 (fun n ->
3719 let n = boundastep state.winh n in
3720 if state.autoscroll <> None
3721 then state.autoscroll <- Some n;
3722 conf.autoscrollstep <- n);
3724 src#int "zoom"
3725 (fun () -> truncate (conf.zoom *. 100.))
3726 (fun v -> setzoom ((float v) /. 100.));
3728 src#int "rotation"
3729 (fun () -> conf.angle)
3730 (fun v -> reqlayout v conf.fitmodel);
3732 src#int "scroll bar width"
3733 (fun () -> conf.scrollbw)
3734 (fun v ->
3735 conf.scrollbw <- v;
3736 reshape state.winw state.winh;
3739 src#int "scroll handle height"
3740 (fun () -> conf.scrollh)
3741 (fun v -> conf.scrollh <- v;);
3743 src#int "thumbnail width"
3744 (fun () -> conf.thumbw)
3745 (fun v ->
3746 conf.thumbw <- min 4096 v;
3747 match oldmode with
3748 | Birdseye beye ->
3749 leavebirdseye beye false;
3750 enterbirdseye ()
3751 | Textentry _
3752 | View
3753 | LinkNav _ -> ()
3756 let mode = state.mode in
3757 src#string "columns"
3758 (fun () ->
3759 match conf.columns with
3760 | Csingle _ -> "1"
3761 | Cmulti (multi, _) -> multicolumns_to_string multi
3762 | Csplit (count, _) -> "-" ^ string_of_int count
3764 (fun v ->
3765 let n, a, b = multicolumns_of_string v in
3766 setcolumns mode n a b);
3768 sep ();
3769 src#caption "Pixmap cache" 0;
3770 src#int_with_suffix "size (advisory)"
3771 (fun () -> conf.memlimit)
3772 (fun v -> conf.memlimit <- v);
3774 src#caption2 "used"
3775 (fun () -> Printf.sprintf "%s bytes, %d tiles"
3776 (string_with_suffix_of_int state.memused)
3777 (Hashtbl.length state.tilemap)) 1;
3779 sep ();
3780 src#caption "Layout" 0;
3781 src#caption2 "Dimension"
3782 (fun () ->
3783 Printf.sprintf "%dx%d (virtual %dx%d)"
3784 state.winw state.winh
3785 state.w state.maxy)
3787 if conf.debug
3788 then
3789 src#caption2 "Position" (fun () ->
3790 Printf.sprintf "%dx%d" state.x state.y
3792 else
3793 src#caption2 "Position" (fun () -> describe_location ()) 1
3796 sep ();
3797 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3798 "Save these parameters as global defaults at exit"
3799 (fun () -> conf.bedefault)
3800 (fun v -> conf.bedefault <- v)
3803 sep ();
3804 let btos b = if b then "@Ulguillemet" else "@Urguillemet" in
3805 src#bool ~offset:0 ~btos "Extended parameters"
3806 (fun () -> !showextended)
3807 (fun v -> showextended := v; fillsrc prevmode prevuioh);
3808 if !showextended
3809 then (
3810 src#bool "checkers"
3811 (fun () -> conf.checkers)
3812 (fun v -> conf.checkers <- v; setcheckers v);
3813 src#bool "update cursor"
3814 (fun () -> conf.updatecurs)
3815 (fun v -> conf.updatecurs <- v);
3816 src#bool "scroll-bar on the left"
3817 (fun () -> conf.leftscroll)
3818 (fun v -> conf.leftscroll <- v);
3819 src#bool "verbose"
3820 (fun () -> conf.verbose)
3821 (fun v -> conf.verbose <- v);
3822 src#bool "invert colors"
3823 (fun () -> conf.invert)
3824 (fun v -> conf.invert <- v);
3825 src#bool "max fit"
3826 (fun () -> conf.maxhfit)
3827 (fun v -> conf.maxhfit <- v);
3828 src#bool "pax mode"
3829 (fun () -> conf.pax != None)
3830 (fun v ->
3831 if v
3832 then conf.pax <- Some (ref (now (), 0, 0))
3833 else conf.pax <- None);
3834 src#string "uri launcher"
3835 (fun () -> conf.urilauncher)
3836 (fun v -> conf.urilauncher <- v);
3837 src#string "path launcher"
3838 (fun () -> conf.pathlauncher)
3839 (fun v -> conf.pathlauncher <- v);
3840 src#string "tile size"
3841 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
3842 (fun v ->
3844 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
3845 conf.tilew <- max 64 w;
3846 conf.tileh <- max 64 h;
3847 flushtiles ();
3848 with exn ->
3849 state.text <- Printf.sprintf "bad tile size `%s': %s"
3850 v @@ exntos exn
3852 src#int "texture count"
3853 (fun () -> conf.texcount)
3854 (fun v ->
3855 if realloctexts v
3856 then conf.texcount <- v
3857 else impmsg "failed to set texture count please retry later"
3859 src#int "slice height"
3860 (fun () -> conf.sliceheight)
3861 (fun v ->
3862 conf.sliceheight <- v;
3863 wcmd "sliceh %d" conf.sliceheight;
3865 src#int "anti-aliasing level"
3866 (fun () -> conf.aalevel)
3867 (fun v ->
3868 conf.aalevel <- bound v 0 8;
3869 state.anchor <- getanchor ();
3870 opendoc state.path state.password;
3872 src#string "page scroll scaling factor"
3873 (fun () -> string_of_float conf.pgscale)
3874 (fun v ->
3876 let s = float_of_string v in
3877 conf.pgscale <- s
3878 with exn ->
3879 state.text <- Printf.sprintf
3880 "bad page scroll scaling factor `%s': %s" v @@ exntos exn
3883 src#int "ui font size"
3884 (fun () -> fstate.fontsize)
3885 (fun v -> setfontsize (bound v 5 100));
3886 src#int "hint font size"
3887 (fun () -> conf.hfsize)
3888 (fun v -> conf.hfsize <- bound v 5 100);
3889 colorp "background color"
3890 (fun () -> conf.bgcolor)
3891 (fun v -> conf.bgcolor <- v; setbgcol v);
3892 src#bool "crop hack"
3893 (fun () -> conf.crophack)
3894 (fun v -> conf.crophack <- v);
3895 src#string "trim fuzz"
3896 (fun () -> irect_to_string conf.trimfuzz)
3897 (fun v ->
3899 conf.trimfuzz <- irect_of_string v;
3900 if conf.trimmargins
3901 then settrim true conf.trimfuzz;
3902 with exn ->
3903 state.text <- Printf.sprintf "bad irect `%s': %s" v @@ exntos exn
3905 src#string "throttle"
3906 (fun () ->
3907 match conf.maxwait with
3908 | None -> "show place holder if page is not ready"
3909 | Some time ->
3910 if time = infinity
3911 then "wait for page to fully render"
3912 else
3913 "wait " ^ string_of_float time
3914 ^ " seconds before showing placeholder"
3916 (fun v ->
3918 let f = float_of_string v in
3919 if f <= 0.0
3920 then conf.maxwait <- None
3921 else conf.maxwait <- Some f
3922 with exn ->
3923 state.text <- Printf.sprintf "bad time `%s': %s" v @@ exntos exn
3925 src#string "ghyll scroll"
3926 (fun () ->
3927 match conf.ghyllscroll with
3928 | None -> E.s
3929 | Some nab -> ghyllscroll_to_string nab
3931 (fun v ->
3932 try conf.ghyllscroll <- ghyllscroll_of_string v
3933 with
3934 | Failure msg ->
3935 state.text <- Printf.sprintf "bad ghyll `%s': %s" v msg
3936 | exn ->
3937 state.text <- Printf.sprintf "bad ghyll `%s': %s" v @@ exntos exn
3939 src#string "selection command"
3940 (fun () -> conf.selcmd)
3941 (fun v -> conf.selcmd <- v);
3942 src#string "synctex command"
3943 (fun () -> conf.stcmd)
3944 (fun v -> conf.stcmd <- v);
3945 src#string "pax command"
3946 (fun () -> conf.paxcmd)
3947 (fun v -> conf.paxcmd <- v);
3948 src#string "ask password command"
3949 (fun () -> conf.passcmd)
3950 (fun v -> conf.passcmd <- v);
3951 src#string "save path command"
3952 (fun () -> conf.savecmd)
3953 (fun v -> conf.savecmd <- v);
3954 src#colorspace "color space"
3955 (fun () -> CSTE.to_string conf.colorspace)
3956 (fun v ->
3957 conf.colorspace <- CSTE.of_int v;
3958 wcmd "cs %d" v;
3959 load state.layout;
3961 src#paxmark "pax mark method"
3962 (fun () -> MTE.to_string conf.paxmark)
3963 (fun v -> conf.paxmark <- MTE.of_int v);
3964 if bousable () && !opengl_has_pbo
3965 then
3966 src#bool "use PBO"
3967 (fun () -> conf.usepbo)
3968 (fun v -> conf.usepbo <- v);
3969 src#bool "mouse wheel scrolls pages"
3970 (fun () -> conf.wheelbypage)
3971 (fun v -> conf.wheelbypage <- v);
3972 src#bool "open remote links in a new instance"
3973 (fun () -> conf.riani)
3974 (fun v -> conf.riani <- v);
3975 src#bool "edit annotations inline"
3976 (fun () -> conf.annotinline)
3977 (fun v -> conf.annotinline <- v);
3980 sep ();
3981 src#caption "Document" 0;
3982 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
3983 src#caption2 "Pages"
3984 (fun () -> string_of_int state.pagecount) 1;
3985 src#caption2 "Dimensions"
3986 (fun () -> string_of_int (List.length state.pdims)) 1;
3987 if conf.trimmargins
3988 then (
3989 sep ();
3990 src#caption "Trimmed margins" 0;
3991 src#caption2 "Dimensions"
3992 (fun () -> string_of_int (List.length state.pdims)) 1;
3995 sep ();
3996 src#caption "OpenGL" 0;
3997 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
3998 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4000 sep ();
4001 src#caption "Location" 0;
4002 if nonemptystr state.origin
4003 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
4004 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
4006 src#reset prevmode prevuioh;
4008 fun () ->
4009 state.text <- E.s;
4010 resetmstate ();
4011 let prevmode = state.mode
4012 and prevuioh = state.uioh in
4013 fillsrc prevmode prevuioh;
4014 let source = (src :> lvsource) in
4015 let modehash = findkeyhash conf "info" in
4016 state.uioh <- coe (object (self)
4017 inherit listview ~zebra:false ~helpmode:false
4018 ~source ~trusted:true ~modehash as super
4019 val mutable m_prevmemused = 0
4020 method! infochanged = function
4021 | Memused ->
4022 if m_prevmemused != state.memused
4023 then (
4024 m_prevmemused <- state.memused;
4025 G.postRedisplay "memusedchanged";
4027 | Pdim -> G.postRedisplay "pdimchanged"
4028 | Docinfo -> fillsrc prevmode prevuioh
4030 method! key key mask =
4031 if not (Wsi.withctrl mask)
4032 then
4033 match key with
4034 | @left | @kpleft -> coe (self#updownlevel ~-1)
4035 | @right | @kpright -> coe (self#updownlevel 1)
4036 | _ -> super#key key mask
4037 else super#key key mask
4038 end);
4039 G.postRedisplay "info";
4042 let enterhelpmode =
4043 let source =
4044 (object
4045 inherit lvsourcebase
4046 method getitemcount = Array.length state.help
4047 method getitem n =
4048 let s, l, _ = state.help.(n) in
4049 (s, l)
4051 method exit ~uioh ~cancel ~active ~first ~pan =
4052 let optuioh =
4053 if not cancel
4054 then (
4055 match state.help.(active) with
4056 | _, _, Action f -> Some (f uioh)
4057 | _, _, Noaction -> Some uioh
4059 else None
4061 m_active <- active;
4062 m_first <- first;
4063 m_pan <- pan;
4064 optuioh
4066 method hasaction n =
4067 match state.help.(n) with
4068 | _, _, Action _ -> true
4069 | _, _, Noaction -> false
4071 initializer
4072 m_active <- -1
4073 end)
4074 in fun () ->
4075 let modehash = findkeyhash conf "help" in
4076 resetmstate ();
4077 state.uioh <- coe (new listview
4078 ~zebra:false ~helpmode:true
4079 ~source ~trusted:true ~modehash);
4080 G.postRedisplay "help";
4083 let entermsgsmode =
4084 let msgsource =
4085 (object
4086 inherit lvsourcebase
4087 val mutable m_items = E.a
4089 method getitemcount = 1 + Array.length m_items
4091 method getitem n =
4092 if n = 0
4093 then "[Clear]", 0
4094 else m_items.(n-1), 0
4096 method exit ~uioh ~cancel ~active ~first ~pan =
4097 ignore uioh;
4098 if not cancel
4099 then (
4100 if active = 0
4101 then Buffer.clear state.errmsgs;
4103 m_active <- active;
4104 m_first <- first;
4105 m_pan <- pan;
4106 None
4108 method hasaction n =
4109 n = 0
4111 method reset =
4112 state.newerrmsgs <- false;
4113 let l = Str.split newlinere (Buffer.contents state.errmsgs) in
4114 m_items <- Array.of_list l
4116 initializer
4117 m_active <- 0
4118 end)
4119 in fun () ->
4120 state.text <- E.s;
4121 resetmstate ();
4122 msgsource#reset;
4123 let source = (msgsource :> lvsource) in
4124 let modehash = findkeyhash conf "listview" in
4125 state.uioh <- coe (object
4126 inherit listview ~zebra:false ~helpmode:false
4127 ~source ~trusted:false ~modehash as super
4128 method! display =
4129 if state.newerrmsgs
4130 then msgsource#reset;
4131 super#display
4132 end);
4133 G.postRedisplay "msgs";
4136 let getusertext s =
4137 let editor = getenvwithdef "EDITOR" E.s in
4138 if emptystr editor
4139 then E.s
4140 else
4141 let tmppath = Filename.temp_file "llpp" "note" in
4142 if nonemptystr s
4143 then (
4144 let oc = open_out tmppath in
4145 output_string oc s;
4146 close_out oc;
4148 let execstr = editor ^ " " ^ tmppath in
4149 let s =
4150 match spawn execstr [] with
4151 | (exception exn) ->
4152 impmsg "spawn(%S) failed: %s" execstr @@ exntos exn;
4154 | pid ->
4155 match Unix.waitpid [] pid with
4156 | (exception exn) ->
4157 impmsg "waitpid(%d) failed: %s" pid @@ exntos exn;
4159 | (_pid, status) ->
4160 match status with
4161 | Unix.WEXITED 0 -> filecontents tmppath
4162 | Unix.WEXITED n ->
4163 impmsg "editor process(%s) exited abnormally: %d" execstr n;
4165 | Unix.WSIGNALED n ->
4166 impmsg "editor process(%s) was killed by signal %d" execstr n;
4168 | Unix.WSTOPPED n ->
4169 impmsg "editor(%s) process was stopped by signal %d" execstr n;
4172 match Unix.unlink tmppath with
4173 | (exception exn) ->
4174 impmsg "failed to ulink %S: %s" tmppath @@ exntos exn;
4176 | () -> s
4179 let enterannotmode opaque slinkindex =
4180 let msgsource =
4181 (object
4182 inherit lvsourcebase
4183 val mutable m_text = E.s
4184 val mutable m_items = E.a
4186 method getitemcount = Array.length m_items
4188 method getitem n =
4189 let label, _func = m_items.(n) in
4190 label, 0
4192 method exit ~uioh ~cancel ~active ~first ~pan =
4193 ignore (uioh, first, pan);
4194 if not cancel
4195 then (
4196 let _label, func = m_items.(active) in
4197 func ()
4199 None
4201 method hasaction n = nonemptystr @@ fst m_items.(n)
4203 method reset s =
4204 let rec split accu b i =
4205 let p = b+i in
4206 if p = String.length s
4207 then (String.sub s b (p-b), unit) :: accu
4208 else
4209 if (i > 70 && s.[p] = ' ') || s.[p] = '\r' || s.[p] = '\n'
4210 then
4211 let ss = if i = 0 then E.s else String.sub s b i in
4212 split ((ss, unit)::accu) (p+1) 0
4213 else
4214 split accu b (i+1)
4216 let cleanup () =
4217 wcmd "freepage %s" (~> opaque);
4218 let keys =
4219 Hashtbl.fold (fun key opaque' accu ->
4220 if opaque' = opaque'
4221 then key :: accu else accu) state.pagemap []
4223 List.iter (Hashtbl.remove state.pagemap) keys;
4224 flushtiles ();
4225 gotoy state.y
4227 let dele () =
4228 delannot opaque slinkindex;
4229 cleanup ();
4231 let edit inline () =
4232 let update s =
4233 if emptystr s
4234 then dele ()
4235 else (
4236 modannot opaque slinkindex s;
4237 cleanup ();
4240 if inline
4241 then
4242 let mode = state.mode in
4243 state.mode <-
4244 Textentry (
4245 ("annotation: ", m_text, None, textentry, update, true),
4246 fun _ -> state.mode <- mode);
4247 state.text <- E.s;
4248 enttext ();
4249 else
4250 let s = getusertext m_text in
4251 update s
4253 m_text <- s;
4254 m_items <-
4255 ( "[Copy]", fun () -> selstring m_text)
4256 :: ("[Delete]", dele)
4257 :: ("[Edit]", edit conf.annotinline)
4258 :: (E.s, unit)
4259 :: split [] 0 0 |> List.rev |> Array.of_list
4261 initializer
4262 m_active <- 0
4263 end)
4265 state.text <- E.s;
4266 let s = getannotcontents opaque slinkindex in
4267 resetmstate ();
4268 msgsource#reset s;
4269 let source = (msgsource :> lvsource) in
4270 let modehash = findkeyhash conf "listview" in
4271 state.uioh <- coe (object
4272 inherit listview ~zebra:false ~helpmode:false
4273 ~source ~trusted:false ~modehash
4274 end);
4275 G.postRedisplay "enterannotmode";
4278 let gotounder under =
4279 let getpath filename =
4280 let path =
4281 if nonemptystr filename
4282 then
4283 if Filename.is_relative filename
4284 then
4285 let dir = Filename.dirname state.path in
4286 let dir =
4287 if Filename.is_implicit dir
4288 then Filename.concat (Sys.getcwd ()) dir
4289 else dir
4291 Filename.concat dir filename
4292 else filename
4293 else E.s
4295 if Sys.file_exists path
4296 then path
4297 else E.s
4299 match under with
4300 | Ulinkgoto (pageno, top) ->
4301 if pageno >= 0
4302 then (
4303 addnav ();
4304 gotopage1 pageno top;
4307 | Ulinkuri s -> gotouri s
4309 | Uremote (filename, pageno) ->
4310 let path = getpath filename in
4311 if nonemptystr path
4312 then (
4313 if conf.riani
4314 then
4315 let command = Printf.sprintf "%s -page %d %S" !selfexec pageno path in
4316 match spawn command [] with
4317 | _pid -> ()
4318 | (exception exn) ->
4319 dolog "failed to execute `%s': %s" command @@ exntos exn
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.anchor <- (pageno, 0.0, 0.0);
4325 state.ranchors <- ranchor :: state.ranchors;
4326 opendoc path E.s;
4328 else impmsg "cannot find %s" filename
4330 | Uremotedest (filename, destname) ->
4331 let path = getpath filename in
4332 if nonemptystr path
4333 then (
4334 if conf.riani
4335 then
4336 let command = !selfexec ^ " " ^ path ^ " -dest " ^ destname in
4337 match spawn command [] with
4338 | (exception exn) ->
4339 dolog "failed to execute `%s': %s" command @@ exntos exn
4340 | _pid -> ()
4341 else
4342 let anchor = getanchor () in
4343 let ranchor = state.path, state.password, anchor, state.origin in
4344 state.origin <- E.s;
4345 state.nameddest <- destname;
4346 state.ranchors <- ranchor :: state.ranchors;
4347 opendoc path E.s;
4349 else impmsg "cannot find %s" filename
4351 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4352 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
4355 let gotooutline (_, _, kind) =
4356 match kind with
4357 | Onone -> ()
4358 | Oanchor anchor ->
4359 let (pageno, y, _) = anchor in
4360 let y = getanchory
4361 (if conf.presentation then (pageno, y, 1.0) else anchor)
4363 addnav ();
4364 gotoghyll y
4365 | Ouri uri -> gotounder (Ulinkuri uri)
4366 | Olaunch cmd -> gotounder (Ulaunch cmd)
4367 | Oremote remote -> gotounder (Uremote remote)
4368 | Ohistory hist -> gotohist hist
4369 | Oremotedest remotedest -> gotounder (Uremotedest remotedest)
4372 class outlinesoucebase fetchoutlines = object (self)
4373 inherit lvsourcebase
4374 val mutable m_items = E.a
4375 val mutable m_minfo = E.a
4376 val mutable m_orig_items = E.a
4377 val mutable m_orig_minfo = E.a
4378 val mutable m_narrow_patterns = []
4379 val mutable m_gen = -1
4381 method getitemcount = Array.length m_items
4383 method getitem n =
4384 let s, n, _ = m_items.(n) in
4385 (s, n+0)
4387 method exit ~(uioh:uioh) ~cancel ~active ~(first:int) ~pan :
4388 uioh option =
4389 ignore (uioh, first);
4390 let items, minfo =
4391 if m_narrow_patterns = []
4392 then m_orig_items, m_orig_minfo
4393 else m_items, m_minfo
4395 m_pan <- pan;
4396 if not cancel
4397 then (
4398 m_items <- items;
4399 m_minfo <- minfo;
4400 gotooutline m_items.(active);
4402 else (
4403 m_items <- items;
4404 m_minfo <- minfo;
4406 None
4408 method hasaction (_:int) = true
4410 method greetmsg =
4411 if Array.length m_items != Array.length m_orig_items
4412 then
4413 let s =
4414 match m_narrow_patterns with
4415 | one :: [] -> one
4416 | many -> String.concat "@Uellipsis" (List.rev many)
4418 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
4419 else E.s
4421 method statestr =
4422 match m_narrow_patterns with
4423 | [] -> E.s
4424 | one :: [] -> one
4425 | head :: _ -> "@Uellipsis" ^ head
4427 method narrow pattern =
4428 match Str.regexp_case_fold pattern with
4429 | (exception _) -> ()
4430 | re ->
4431 let rec loop accu minfo n =
4432 if n = -1
4433 then (
4434 m_items <- Array.of_list accu;
4435 m_minfo <- Array.of_list minfo;
4437 else
4438 let (s, _, _) as o = m_items.(n) in
4439 let accu, minfo =
4440 match Str.search_forward re s 0 with
4441 | (exception Not_found) -> accu, minfo
4442 | first -> o :: accu, (first, Str.match_end ()) :: minfo
4444 loop accu minfo (n-1)
4446 loop [] [] (Array.length m_items - 1)
4448 method! getminfo = m_minfo
4450 method denarrow =
4451 m_orig_items <- fetchoutlines ();
4452 m_minfo <- m_orig_minfo;
4453 m_items <- m_orig_items
4455 method add_narrow_pattern pattern =
4456 m_narrow_patterns <- pattern :: m_narrow_patterns
4458 method del_narrow_pattern =
4459 match m_narrow_patterns with
4460 | _ :: rest -> m_narrow_patterns <- rest
4461 | [] -> ()
4463 method renarrow =
4464 self#denarrow;
4465 match m_narrow_patterns with
4466 | pattern :: [] -> self#narrow pattern; pattern
4467 | list ->
4468 List.fold_left (fun accu pattern ->
4469 self#narrow pattern;
4470 pattern ^ "@Uellipsis" ^ accu) E.s list
4472 method calcactive (_:anchor) = 0
4474 method reset anchor items =
4475 if state.gen != m_gen
4476 then (
4477 m_orig_items <- items;
4478 m_items <- items;
4479 m_narrow_patterns <- [];
4480 m_minfo <- E.a;
4481 m_orig_minfo <- E.a;
4482 m_gen <- state.gen;
4484 else (
4485 if items != m_orig_items
4486 then (
4487 m_orig_items <- items;
4488 if m_narrow_patterns == []
4489 then m_items <- items;
4492 let active = self#calcactive anchor in
4493 m_active <- active;
4494 m_first <- firstof m_first active
4498 let outlinesource fetchoutlines =
4499 (object
4500 inherit outlinesoucebase fetchoutlines
4501 method! calcactive anchor =
4502 let rely = getanchory anchor in
4503 let rec loop n best bestd =
4504 if n = Array.length m_items
4505 then best
4506 else
4507 let _, _, kind = m_items.(n) in
4508 match kind with
4509 | Oanchor anchor ->
4510 let orely = getanchory anchor in
4511 let d = abs (orely - rely) in
4512 if d < bestd
4513 then loop (n+1) n d
4514 else loop (n+1) best bestd
4515 | Onone | Oremote _ | Olaunch _
4516 | Oremotedest _ | Ouri _ | Ohistory _ ->
4517 loop (n+1) best bestd
4519 loop 0 ~-1 max_int
4520 end)
4523 let enteroutlinemode, enterbookmarkmode, enterhistmode =
4524 let mkselector sourcetype =
4525 let fetchoutlines () =
4526 match sourcetype with
4527 | `bookmarks -> Array.of_list state.bookmarks
4528 | `outlines -> state.outlines
4529 | `history -> genhistoutlines ()
4531 let source =
4532 if sourcetype = `history
4533 then new outlinesoucebase fetchoutlines
4534 else outlinesource fetchoutlines
4536 fun errmsg ->
4537 let outlines = fetchoutlines () in
4538 if Array.length outlines = 0
4539 then (
4540 showtext ' ' errmsg;
4542 else (
4543 resetmstate ();
4544 Wsi.setcursor Wsi.CURSOR_INHERIT;
4545 let anchor = getanchor () in
4546 source#reset anchor outlines;
4547 state.text <- source#greetmsg;
4548 state.uioh <-
4549 coe (new outlinelistview ~zebra:(sourcetype=`history) ~source);
4550 G.postRedisplay "enter selector";
4553 let mkenter sourcetype errmsg =
4554 let enter = mkselector sourcetype in
4555 fun () -> enter errmsg
4557 (**)mkenter `outlines "document has no outline"
4558 , mkenter `bookmarks "document has no bookmarks (yet)"
4559 , mkenter `history "history is empty"
4562 let quickbookmark ?title () =
4563 match state.layout with
4564 | [] -> ()
4565 | l :: _ ->
4566 let title =
4567 match title with
4568 | None ->
4569 let tm = Unix.localtime (now ()) in
4570 Printf.sprintf
4571 "Quick (page %d) (bookmarked at %02d/%02d/%d %02d:%02d)"
4572 (l.pageno+1)
4573 tm.Unix.tm_mday
4574 (tm.Unix.tm_mon+1)
4575 (tm.Unix.tm_year + 1900)
4576 tm.Unix.tm_hour
4577 tm.Unix.tm_min
4578 | Some title -> title
4580 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
4583 let setautoscrollspeed step goingdown =
4584 let incr = max 1 ((abs step) / 2) in
4585 let incr = if goingdown then incr else -incr in
4586 let astep = boundastep state.winh (step + incr) in
4587 state.autoscroll <- Some astep;
4590 let canpan () =
4591 match conf.columns with
4592 | Csplit _ -> true
4593 | Csingle _ | Cmulti _ -> state.x != 0 || conf.zoom > 1.0
4596 let panbound x = bound x (-state.w) (wadjsb () + state.winw);;
4598 let existsinrow pageno (columns, coverA, coverB) p =
4599 let last = ((pageno - coverA) mod columns) + columns in
4600 let rec any = function
4601 | [] -> false
4602 | l :: rest ->
4603 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
4604 then p l
4605 else (
4606 if not (p l)
4607 then (if l.pageno = last then false else any rest)
4608 else true
4611 any state.layout
4614 let nextpage () =
4615 match state.layout with
4616 | [] ->
4617 let pageno = page_of_y state.y in
4618 gotoghyll (getpagey (pageno+1))
4619 | l :: rest ->
4620 match conf.columns with
4621 | Csingle _ ->
4622 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
4623 then
4624 let y = clamp (pgscale state.winh) in
4625 gotoghyll y
4626 else
4627 let pageno = min (l.pageno+1) (state.pagecount-1) in
4628 gotoghyll (getpagey pageno)
4629 | Cmulti ((c, _, _) as cl, _) ->
4630 if conf.presentation
4631 && (existsinrow l.pageno cl
4632 (fun l -> l.pageh > l.pagey + l.pagevh))
4633 then
4634 let y = clamp (pgscale state.winh) in
4635 gotoghyll y
4636 else
4637 let pageno = min (l.pageno+c) (state.pagecount-1) in
4638 gotoghyll (getpagey pageno)
4639 | Csplit (n, _) ->
4640 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
4641 then
4642 let pagey, pageh = getpageyh l.pageno in
4643 let pagey = pagey + pageh * l.pagecol in
4644 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
4645 gotoghyll (pagey + pageh + ips)
4648 let prevpage () =
4649 match state.layout with
4650 | [] ->
4651 let pageno = page_of_y state.y in
4652 gotoghyll (getpagey (pageno-1))
4653 | l :: _ ->
4654 match conf.columns with
4655 | Csingle _ ->
4656 if conf.presentation && l.pagey != 0
4657 then
4658 gotoghyll (clamp (pgscale ~-(state.winh)))
4659 else
4660 let pageno = max 0 (l.pageno-1) in
4661 gotoghyll (getpagey pageno)
4662 | Cmulti ((c, _, coverB) as cl, _) ->
4663 if conf.presentation &&
4664 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
4665 then
4666 gotoghyll (clamp (pgscale ~-(state.winh)))
4667 else
4668 let decr =
4669 if l.pageno = state.pagecount - coverB
4670 then 1
4671 else c
4673 let pageno = max 0 (l.pageno-decr) in
4674 gotoghyll (getpagey pageno)
4675 | Csplit (n, _) ->
4676 let y =
4677 if l.pagecol = 0
4678 then
4679 if l.pageno = 0
4680 then l.pagey
4681 else
4682 let pageno = max 0 (l.pageno-1) in
4683 let pagey, pageh = getpageyh pageno in
4684 pagey + (n-1)*pageh
4685 else
4686 let pagey, pageh = getpageyh l.pageno in
4687 pagey + pageh * (l.pagecol-1) - conf.interpagespace
4689 gotoghyll y
4692 let save () =
4693 if emptystr conf.savecmd
4694 then error "don't know where to save modified document"
4695 else
4696 let savecmd = Str.global_replace percentsre state.path conf.savecmd in
4697 let path =
4698 getcmdoutput
4699 (fun s -> error "failed to obtain path to the saved copy: %s" s)
4700 savecmd
4702 if nonemptystr path
4703 then
4704 let tmp = path ^ ".tmp" in
4705 savedoc tmp;
4706 Unix.rename tmp path;
4709 let viewkeyboard key mask =
4710 let enttext te =
4711 let mode = state.mode in
4712 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4713 state.text <- E.s;
4714 enttext ();
4715 G.postRedisplay "view:enttext"
4717 let ctrl = Wsi.withctrl mask in
4718 let key =
4719 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
4721 match key with
4722 | @Q -> exit 0
4724 | @W ->
4725 if hasunsavedchanges ()
4726 then save ()
4728 | @insert ->
4729 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
4730 then (
4731 state.mode <- LinkNav (Ltgendir 0);
4732 gotoy state.y;
4734 else impmsg "keyboard link navigation does not work under rotation"
4736 | @escape | @q ->
4737 begin match state.mstate with
4738 | Mzoomrect _ ->
4739 resetmstate ();
4740 G.postRedisplay "kill rect";
4741 | Msel _
4742 | Mpan _
4743 | Mscrolly | Mscrollx
4744 | Mzoom _
4745 | Mnone ->
4746 begin match state.mode with
4747 | LinkNav _ ->
4748 state.mode <- View;
4749 G.postRedisplay "esc leave linknav"
4750 | Birdseye _
4751 | Textentry _
4752 | View ->
4753 match state.ranchors with
4754 | [] -> raise Quit
4755 | (path, password, anchor, origin) :: rest ->
4756 state.ranchors <- rest;
4757 state.anchor <- anchor;
4758 state.origin <- origin;
4759 state.nameddest <- E.s;
4760 opendoc path password
4761 end;
4762 end;
4764 | @backspace ->
4765 gotoghyll (getnav ~-1)
4767 | @o ->
4768 enteroutlinemode ()
4770 | @H ->
4771 enterhistmode ()
4773 | @u ->
4774 state.rects <- [];
4775 state.text <- E.s;
4776 Hashtbl.iter (fun _ opaque ->
4777 clearmark opaque;
4778 Hashtbl.clear state.prects) state.pagemap;
4779 G.postRedisplay "dehighlight";
4781 | @slash | @question ->
4782 let ondone isforw s =
4783 cbput state.hists.pat s;
4784 state.searchpattern <- s;
4785 search s isforw
4787 let s = String.make 1 (Char.chr key) in
4788 enttext (s, E.s, Some (onhist state.hists.pat),
4789 textentry, ondone (key = @slash), true)
4791 | @plus | @kpplus | @equals when ctrl ->
4792 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4793 setzoom (conf.zoom +. incr)
4795 | @plus | @kpplus ->
4796 let ondone s =
4797 let n =
4798 try int_of_string s with exc ->
4799 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exc;
4800 max_int
4802 if n != max_int
4803 then (
4804 conf.pagebias <- n;
4805 state.text <- "page bias is now " ^ string_of_int n;
4808 enttext ("page bias: ", E.s, None, intentry, ondone, true)
4810 | @minus | @kpminus when ctrl ->
4811 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4812 setzoom (max 0.01 (conf.zoom -. decr))
4814 | @minus | @kpminus ->
4815 let ondone msg = state.text <- msg in
4816 enttext (
4817 "option [acfhilpstvxACFPRSZTISM]: ", E.s, None,
4818 optentry state.mode, ondone, true
4821 | @0 when ctrl ->
4822 if conf.zoom = 1.0
4823 then (
4824 state.x <- 0;
4825 gotoy state.y
4827 else setzoom 1.0
4829 | (@1 | @2) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
4830 let cols =
4831 match conf.columns with
4832 | Csingle _ | Cmulti _ -> 1
4833 | Csplit (n, _) -> n
4835 let h = state.winh -
4836 conf.interpagespace lsl (if conf.presentation then 1 else 0)
4838 let zoom = zoomforh state.winw h (vscrollw ()) cols in
4839 if zoom > 0.0 && (key = @2 || zoom < 1.0)
4840 then setzoom zoom
4842 | @3 when ctrl ->
4843 let fm =
4844 match conf.fitmodel with
4845 | FitWidth -> FitProportional
4846 | FitProportional -> FitPage
4847 | FitPage -> FitWidth
4849 state.text <- "fit model: " ^ FMTE.to_string fm;
4850 reqlayout conf.angle fm
4852 | @4 when ctrl -> (* ctrl-4 *)
4853 let zoom = getmaxw () /. float state.winw in
4854 if zoom > 0.0 then setzoom zoom
4856 | @F9 ->
4857 togglebirdseye ()
4859 | @9 when ctrl ->
4860 togglebirdseye ()
4862 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4863 when not ctrl -> (* 0..9 *)
4864 let ondone s =
4865 let n =
4866 try int_of_string s with exc ->
4867 state.text <- Printf.sprintf "bad integer `%s': %s" s @@ exntos exc;
4870 if n >= 0
4871 then (
4872 addnav ();
4873 cbput state.hists.pag (string_of_int n);
4874 gotopage1 (n + conf.pagebias - 1) 0;
4877 let pageentry text key =
4878 match Char.unsafe_chr key with
4879 | 'g' -> TEdone text
4880 | _ -> intentry text key
4882 let text = String.make 1 (Char.chr key) in
4883 enttext (":", text, Some (onhist state.hists.pag),
4884 pageentry, ondone, true)
4886 | @b ->
4887 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
4888 reshape state.winw state.winh;
4890 | @B ->
4891 state.bzoom <- not state.bzoom;
4892 state.rects <- [];
4893 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
4895 | @l ->
4896 conf.hlinks <- not conf.hlinks;
4897 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4898 G.postRedisplay "toggle highlightlinks";
4900 | @F ->
4901 if conf.angle mod 360 = 0
4902 then (
4903 state.glinks <- true;
4904 let mode = state.mode in
4905 state.mode <-
4906 Textentry (
4907 (":", E.s, None, linknentry, linknact gotounder, false),
4908 (fun _ ->
4909 state.glinks <- false;
4910 state.mode <- mode)
4912 state.text <- E.s;
4913 G.postRedisplay "view:linkent(F)"
4915 else impmsg "hint mode does not work under rotation"
4917 | @y ->
4918 state.glinks <- true;
4919 let mode = state.mode in
4920 state.mode <- Textentry (
4922 ":", E.s, None, linknentry, linknact (fun under ->
4923 selstring (undertext under);
4924 ), false
4926 fun _ ->
4927 state.glinks <- false;
4928 state.mode <- mode
4930 state.text <- E.s;
4931 G.postRedisplay "view:linkent"
4933 | @a ->
4934 begin match state.autoscroll with
4935 | Some step ->
4936 conf.autoscrollstep <- step;
4937 state.autoscroll <- None
4938 | None ->
4939 if conf.autoscrollstep = 0
4940 then state.autoscroll <- Some 1
4941 else state.autoscroll <- Some conf.autoscrollstep
4944 | @p when ctrl ->
4945 launchpath () (* XXX where do error messages go? *)
4947 | @P ->
4948 setpresentationmode (not conf.presentation);
4949 showtext ' ' ("presentation mode " ^
4950 if conf.presentation then "on" else "off");
4952 | @f ->
4953 if List.mem Wsi.Fullscreen state.winstate
4954 then Wsi.reshape conf.cwinw conf.cwinh
4955 else Wsi.fullscreen ()
4957 | @p | @N ->
4958 search state.searchpattern false
4960 | @n | @F3 ->
4961 search state.searchpattern true
4963 | @t ->
4964 begin match state.layout with
4965 | [] -> ()
4966 | l :: _ ->
4967 gotoghyll (getpagey l.pageno)
4970 | @space ->
4971 nextpage ()
4973 | @delete | @kpdelete -> (* delete *)
4974 prevpage ()
4976 | @equals ->
4977 showtext ' ' (describe_location ());
4979 | @w ->
4980 begin match state.layout with
4981 | [] -> ()
4982 | l :: _ ->
4983 Wsi.reshape (l.pagew + vscrollw ()) l.pageh;
4984 G.postRedisplay "w"
4987 | @apos ->
4988 enterbookmarkmode ()
4990 | @h | @F1 ->
4991 enterhelpmode ()
4993 | @i ->
4994 enterinfomode ()
4996 | @e when Buffer.length state.errmsgs > 0 ->
4997 entermsgsmode ()
4999 | @m ->
5000 let ondone s =
5001 match state.layout with
5002 | l :: _ ->
5003 if nonemptystr s
5004 then
5005 state.bookmarks <-
5006 (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
5007 | _ -> ()
5009 enttext ("bookmark: ", E.s, None, textentry, ondone, true)
5011 | @tilde ->
5012 quickbookmark ();
5013 showtext ' ' "Quick bookmark added";
5015 | @z ->
5016 begin match state.layout with
5017 | l :: _ ->
5018 let rect = getpdimrect l.pagedimno in
5019 let w, h =
5020 if conf.crophack
5021 then
5022 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5023 truncate (1.2 *. (rect.(3) -. rect.(0))))
5024 else
5025 (truncate (rect.(1) -. rect.(0)),
5026 truncate (rect.(3) -. rect.(0)))
5028 let w = truncate ((float w)*.conf.zoom)
5029 and h = truncate ((float h)*.conf.zoom) in
5030 if w != 0 && h != 0
5031 then (
5032 state.anchor <- getanchor ();
5033 Wsi.reshape (w + vscrollw ()) (h + conf.interpagespace)
5035 G.postRedisplay "z";
5037 | [] -> ()
5040 | @x -> state.roam ()
5042 | @Lt | @Gt ->
5043 reqlayout (conf.angle +
5044 (if key = @Gt then 30 else -30)) conf.fitmodel
5046 | @Lb | @Rb ->
5047 conf.colorscale <-
5048 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5050 G.postRedisplay "brightness";
5052 | @c when state.mode = View ->
5053 if Wsi.withalt mask
5054 then (
5055 if conf.zoom > 1.0
5056 then
5057 let m = (wadjsb () + state.winw - state.w) / 2 in
5058 state.x <- m;
5059 gotoy_and_clear_text state.y
5061 else
5062 let (c, a, b), z =
5063 match state.prevcolumns with
5064 | None -> (1, 0, 0), 1.0
5065 | Some (columns, z) ->
5066 let cab =
5067 match columns with
5068 | Csplit (c, _) -> -c, 0, 0
5069 | Cmulti ((c, a, b), _) -> c, a, b
5070 | Csingle _ -> 1, 0, 0
5072 cab, z
5074 setcolumns View c a b;
5075 setzoom z
5077 | @down | @up when ctrl && Wsi.withshift mask ->
5078 let zoom, x = state.prevzoom in
5079 setzoom zoom;
5080 state.x <- x;
5082 | @k | @up | @kpup ->
5083 begin match state.autoscroll with
5084 | None ->
5085 begin match state.mode with
5086 | Birdseye beye -> upbirdseye 1 beye
5087 | Textentry _
5088 | View
5089 | LinkNav _ ->
5090 if ctrl
5091 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5092 else (
5093 if not (Wsi.withshift mask) && conf.presentation
5094 then prevpage ()
5095 else gotoghyll1 true (clamp (-conf.scrollstep))
5098 | Some n ->
5099 setautoscrollspeed n false
5102 | @j | @down | @kpdown ->
5103 begin match state.autoscroll with
5104 | None ->
5105 begin match state.mode with
5106 | Birdseye beye -> downbirdseye 1 beye
5107 | Textentry _
5108 | View
5109 | LinkNav _ ->
5110 if ctrl
5111 then gotoy_and_clear_text (clamp (state.winh/2))
5112 else (
5113 if not (Wsi.withshift mask) && conf.presentation
5114 then nextpage ()
5115 else gotoghyll1 true (clamp (conf.scrollstep))
5118 | Some n ->
5119 setautoscrollspeed n true
5122 | @left | @right | @kpleft | @kpright when not (Wsi.withalt mask) ->
5123 if canpan ()
5124 then
5125 let dx =
5126 if ctrl
5127 then state.winw / 2
5128 else conf.hscrollstep
5130 let dx = if key = @left || key = @kpleft then dx else -dx in
5131 state.x <- panbound (state.x + dx);
5132 gotoy_and_clear_text state.y
5133 else (
5134 state.text <- E.s;
5135 G.postRedisplay "left/right"
5138 | @prior | @kpprior ->
5139 let y =
5140 if ctrl
5141 then
5142 match state.layout with
5143 | [] -> state.y
5144 | l :: _ -> state.y - l.pagey
5145 else
5146 clamp (pgscale (-state.winh))
5148 gotoghyll y
5150 | @next | @kpnext ->
5151 let y =
5152 if ctrl
5153 then
5154 match List.rev state.layout with
5155 | [] -> state.y
5156 | l :: _ -> getpagey l.pageno
5157 else
5158 clamp (pgscale state.winh)
5160 gotoghyll y
5162 | @g | @home | @kphome ->
5163 addnav ();
5164 gotoghyll 0
5165 | @G | @jend | @kpend ->
5166 addnav ();
5167 gotoghyll (clamp state.maxy)
5169 | @right | @kpright when Wsi.withalt mask ->
5170 gotoghyll (getnav 1)
5171 | @left | @kpleft when Wsi.withalt mask ->
5172 gotoghyll (getnav ~-1)
5174 | @r ->
5175 reload ()
5177 | @v when conf.debug ->
5178 state.rects <- [];
5179 List.iter (fun l ->
5180 match getopaque l.pageno with
5181 | None -> ()
5182 | Some opaque ->
5183 let x0, y0, x1, y1 = pagebbox opaque in
5184 let a,b = float x0, float y0 in
5185 let c,d = float x1, float y0 in
5186 let e,f = float x1, float y1 in
5187 let h,j = float x0, float y1 in
5188 let rect = (a,b,c,d,e,f,h,j) in
5189 debugrect rect;
5190 let color = (0.0, 0.0, 1.0 /. (l.pageno mod 3 |> float), 0.5) in
5191 state.rects <- (l.pageno, color, rect) :: state.rects;
5192 ) state.layout;
5193 G.postRedisplay "v";
5195 | @pipe ->
5196 let mode = state.mode in
5197 let cmd = ref E.s in
5198 let onleave = function
5199 | Cancel -> state.mode <- mode
5200 | Confirm ->
5201 List.iter (fun l ->
5202 match getopaque l.pageno with
5203 | Some opaque -> pipesel opaque !cmd
5204 | None -> ()) state.layout;
5205 state.mode <- mode
5207 let ondone s =
5208 cbput state.hists.sel s;
5209 cmd := s
5211 let te =
5212 "| ", !cmd, Some (onhist state.hists.sel), textentry, ondone, true
5214 G.postRedisplay "|";
5215 state.mode <- Textentry (te, onleave);
5217 | _ ->
5218 vlog "huh? %s" (Wsi.keyname key)
5221 let linknavkeyboard key mask linknav =
5222 let getpage pageno =
5223 let rec loop = function
5224 | [] -> None
5225 | l :: _ when l.pageno = pageno -> Some l
5226 | _ :: rest -> loop rest
5227 in loop state.layout
5229 let doexact (pageno, n) =
5230 match getopaque pageno, getpage pageno with
5231 | Some opaque, Some l ->
5232 if key = @enter || key = @kpenter
5233 then
5234 let under = getlink opaque n in
5235 G.postRedisplay "link gotounder";
5236 gotounder under;
5237 state.mode <- View;
5238 else
5239 let opt, dir =
5240 match key with
5241 | @home ->
5242 Some (findlink opaque LDfirst), -1
5244 | @jend ->
5245 Some (findlink opaque LDlast), 1
5247 | @left ->
5248 Some (findlink opaque (LDleft n)), -1
5250 | @right ->
5251 Some (findlink opaque (LDright n)), 1
5253 | @up ->
5254 Some (findlink opaque (LDup n)), -1
5256 | @down ->
5257 Some (findlink opaque (LDdown n)), 1
5259 | _ -> None, 0
5261 let pwl l dir =
5262 begin match findpwl l.pageno dir with
5263 | Pwlnotfound -> ()
5264 | Pwl pageno ->
5265 let notfound dir =
5266 state.mode <- LinkNav (Ltgendir dir);
5267 let y, h = getpageyh pageno in
5268 let y =
5269 if dir < 0
5270 then y + h - state.winh
5271 else y
5273 gotoy y
5275 begin match getopaque pageno, getpage pageno with
5276 | Some opaque, Some _ ->
5277 let link =
5278 let ld = if dir > 0 then LDfirst else LDlast in
5279 findlink opaque ld
5281 begin match link with
5282 | Lfound m ->
5283 showlinktype (getlink opaque m);
5284 state.mode <- LinkNav (Ltexact (pageno, m));
5285 G.postRedisplay "linknav jpage";
5286 | Lnotfound -> notfound dir
5287 end;
5288 | _ -> notfound dir
5289 end;
5290 end;
5292 begin match opt with
5293 | Some Lnotfound -> pwl l dir;
5294 | Some (Lfound m) ->
5295 if m = n
5296 then pwl l dir
5297 else (
5298 let _, y0, _, y1 = getlinkrect opaque m in
5299 if y0 < l.pagey
5300 then gotopage1 l.pageno y0
5301 else (
5302 let d = fstate.fontsize + 1 in
5303 if y1 - l.pagey > l.pagevh - d
5304 then gotopage1 l.pageno (y1 - state.winh - hscrollh () + d)
5305 else G.postRedisplay "linknav";
5307 showlinktype (getlink opaque m);
5308 state.mode <- LinkNav (Ltexact (l.pageno, m));
5311 | None -> viewkeyboard key mask
5312 end;
5313 | _ -> viewkeyboard key mask
5315 if key = @insert
5316 then (
5317 state.mode <- View;
5318 G.postRedisplay "leave linknav"
5320 else
5321 match linknav with
5322 | Ltgendir _ | Ltnotready _ -> viewkeyboard key mask
5323 | Ltexact exact -> doexact exact
5326 let keyboard key mask =
5327 if (key = @g && Wsi.withctrl mask) && not (istextentry state.mode)
5328 then wcmd "interrupt"
5329 else state.uioh <- state.uioh#key key mask
5332 let birdseyekeyboard key mask
5333 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5334 let incr =
5335 match conf.columns with
5336 | Csingle _ -> 1
5337 | Cmulti ((c, _, _), _) -> c
5338 | Csplit _ -> failwith "bird's eye split mode"
5340 let pgh layout = List.fold_left
5341 (fun m l -> max l.pageh m) state.winh layout in
5342 match key with
5343 | @l when Wsi.withctrl mask ->
5344 let y, h = getpageyh pageno in
5345 let top = (state.winh - h) / 2 in
5346 gotoy (max 0 (y - top))
5347 | @enter | @kpenter -> leavebirdseye beye false
5348 | @escape -> leavebirdseye beye true
5349 | @up -> upbirdseye incr beye
5350 | @down -> downbirdseye incr beye
5351 | @left -> upbirdseye 1 beye
5352 | @right -> downbirdseye 1 beye
5354 | @prior ->
5355 begin match state.layout with
5356 | l :: _ ->
5357 if l.pagey != 0
5358 then (
5359 state.mode <- Birdseye (
5360 oconf, leftx, l.pageno, hooverpageno, anchor
5362 gotopage1 l.pageno 0;
5364 else (
5365 let layout = layout state.x (state.y-state.winh)
5366 state.winw
5367 (pgh state.layout) in
5368 match layout with
5369 | [] -> gotoy (clamp (-state.winh))
5370 | l :: _ ->
5371 state.mode <- Birdseye (
5372 oconf, leftx, l.pageno, hooverpageno, anchor
5374 gotopage1 l.pageno 0
5377 | [] -> gotoy (clamp (-state.winh))
5378 end;
5380 | @next ->
5381 begin match List.rev state.layout with
5382 | l :: _ ->
5383 let layout = layout state.x
5384 (state.y + (pgh state.layout))
5385 state.winw state.winh in
5386 begin match layout with
5387 | [] ->
5388 let incr = l.pageh - l.pagevh in
5389 if incr = 0
5390 then (
5391 state.mode <-
5392 Birdseye (
5393 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5395 G.postRedisplay "birdseye pagedown";
5397 else gotoy (clamp (incr + conf.interpagespace*2));
5399 | l :: _ ->
5400 state.mode <-
5401 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5402 gotopage1 l.pageno 0;
5405 | [] -> gotoy (clamp state.winh)
5406 end;
5408 | @home ->
5409 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5410 gotopage1 0 0
5412 | @jend ->
5413 let pageno = state.pagecount - 1 in
5414 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5415 if not (pagevisible state.layout pageno)
5416 then
5417 let h =
5418 match List.rev state.pdims with
5419 | [] -> state.winh
5420 | (_, _, h, _) :: _ -> h
5422 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5423 else G.postRedisplay "birdseye end";
5425 | _ -> viewkeyboard key mask
5428 let drawpage l =
5429 let color =
5430 match state.mode with
5431 | Textentry _ -> scalecolor 0.4
5432 | LinkNav _
5433 | View -> scalecolor 1.0
5434 | Birdseye (_, _, pageno, hooverpageno, _) ->
5435 if l.pageno = hooverpageno
5436 then scalecolor 0.9
5437 else (
5438 if l.pageno = pageno
5439 then (
5440 let c = scalecolor 1.0 in
5441 GlDraw.color c;
5442 GlDraw.line_width 3.0;
5443 let dispx = xadjsb () + l.pagedispx in
5444 linerect
5445 (float (dispx-1)) (float (l.pagedispy-1))
5446 (float (dispx+l.pagevw+1))
5447 (float (l.pagedispy+l.pagevh+1))
5449 GlDraw.line_width 1.0;
5452 else scalecolor 0.8
5455 drawtiles l color;
5458 let postdrawpage l linkindexbase =
5459 match getopaque l.pageno with
5460 | Some opaque ->
5461 if tileready l l.pagex l.pagey
5462 then
5463 let x = l.pagedispx - l.pagex + xadjsb ()
5464 and y = l.pagedispy - l.pagey in
5465 let hlmask =
5466 match conf.columns with
5467 | Csingle _ | Cmulti _ ->
5468 (if conf.hlinks then 1 else 0)
5469 + (if state.glinks
5470 && not (isbirdseye state.mode) then 2 else 0)
5471 | Csplit _ -> 0
5473 let s =
5474 match state.mode with
5475 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5476 | Textentry _
5477 | Birdseye _
5478 | View
5479 | LinkNav _ -> E.s
5481 Hashtbl.find_all state.prects l.pageno |>
5482 List.iter (fun vals -> drawprect opaque x y vals);
5483 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
5484 else 0
5485 | _ -> 0
5488 let scrollindicator () =
5489 let sbw, ph, sh = state.uioh#scrollph in
5490 let sbh, pw, sw = state.uioh#scrollpw in
5492 let x0,x1,hx0 =
5493 if conf.leftscroll
5494 then (0, sbw, sbw)
5495 else ((state.winw - sbw), state.winw, 0)
5498 GlDraw.color (0.64, 0.64, 0.64);
5499 filledrect (float x0) 0. (float x1) (float state.winh);
5500 filledrect
5501 (float hx0) (float (state.winh - sbh))
5502 (float (hx0 + wadjsb () + state.winw)) (float state.winh)
5504 GlDraw.color (0.0, 0.0, 0.0);
5506 filledrect (float x0) ph (float x1) (ph +. sh);
5507 let pw = pw +. float hx0 in
5508 filledrect pw (float (state.winh - sbh)) (pw +. sw) (float state.winh);
5511 let showsel () =
5512 match state.mstate with
5513 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5516 | Msel ((x0, y0), (x1, y1)) ->
5517 let identify opaque l px py = Some (opaque, l.pageno, px, py) in
5518 let o0,n0,px0,py0 = onppundermouse identify x0 y0 (~< E.s, -1, 0, 0) in
5519 let _o1,n1,px1,py1 = onppundermouse identify x1 y1 (~< E.s, -1, 0, 0) in
5520 if n0 != -1 && n0 = n1 then seltext o0 (px0, py0, px1, py1);
5523 let showrects = function [] -> () | rects ->
5524 Gl.enable `blend;
5525 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5526 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5527 List.iter
5528 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5529 List.iter (fun l ->
5530 if l.pageno = pageno
5531 then (
5532 let dx = float (l.pagedispx - l.pagex) in
5533 let dy = float (l.pagedispy - l.pagey) in
5534 let r, g, b, alpha = c in
5535 GlDraw.color (r, g, b) ~alpha;
5536 Raw.sets_float state.vraw ~pos:0
5537 [| x0+.dx; y0+.dy;
5538 x1+.dx; y1+.dy;
5539 x3+.dx; y3+.dy;
5540 x2+.dx; y2+.dy |];
5541 GlArray.vertex `two state.vraw;
5542 GlArray.draw_arrays `triangle_strip ~first:0 ~count:4;
5544 ) state.layout
5545 ) rects
5547 Gl.disable `blend;
5550 let display () =
5551 begin match conf.columns, state.layout with
5552 | Csingle _, _ :: _ ->
5553 GlDraw.color (scalecolor2 conf.bgcolor);
5554 let y =
5555 List.fold_left (fun y l ->
5556 let x0 = 0 in
5557 let y0 = y in
5558 let x1 = l.pagedispx + xadjsb () in
5559 let y1 = (l.pagedispy + l.pagevh) in
5560 filledrect (float x0) (float y0) (float x1) (float y1);
5561 let x0 = x1 + l.pagevw in
5562 let x1 = state.winw in
5563 filledrect1 (float x0) (float y0) (float x1) (float y1);
5564 if y != l.pagedispy
5565 then (
5566 let x0 = 0
5567 and x1 = state.winw in
5568 let y0 = y
5569 and y1 = l.pagedispy in
5570 filledrect1 (float x0) (float y0) (float x1) (float y1);
5572 l.pagedispy + l.pagevh) 0 state.layout
5574 let x0 = 0
5575 and x1 = state.winw in
5576 let y0 = y
5577 and y1 = state.winh in
5578 filledrect1 (float x0) (float y0) (float x1) (float y1)
5579 | (Cmulti _ | Csplit _), _ | Csingle _, [] ->
5580 GlClear.color (scalecolor2 conf.bgcolor);
5581 GlClear.clear [`color];
5582 end;
5583 List.iter drawpage state.layout;
5584 let rects =
5585 match state.mode with
5586 | LinkNav (Ltexact (pageno, linkno)) ->
5587 begin match getopaque pageno with
5588 | Some opaque ->
5589 let dx = xadjsb () in
5590 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5591 let x0 = x0 + dx and x1 = x1 + dx in
5592 let color = (0.0, 0.0, 0.5, 0.5) in
5593 (pageno, color, (
5594 float x0, float y0,
5595 float x1, float y0,
5596 float x1, float y1,
5597 float x0, float y1)
5598 ) :: state.rects
5599 | None -> state.rects
5601 | LinkNav (Ltgendir _) | LinkNav (Ltnotready _)
5602 | Birdseye _
5603 | Textentry _
5604 | View -> state.rects
5606 showrects rects;
5607 let rec postloop linkindexbase = function
5608 | l :: rest ->
5609 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
5610 postloop linkindexbase rest
5611 | [] -> ()
5613 showsel ();
5614 postloop 0 state.layout;
5615 state.uioh#display;
5616 begin match state.mstate with
5617 | Mzoomrect ((x0, y0), (x1, y1)) ->
5618 Gl.enable `blend;
5619 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5620 GlFunc.blend_func ~src:`src_alpha ~dst:`one_minus_src_alpha;
5621 filledrect (float x0) (float y0) (float x1) (float y1);
5622 Gl.disable `blend;
5623 | Msel _
5624 | Mpan _
5625 | Mscrolly | Mscrollx
5626 | Mzoom _
5627 | Mnone -> ()
5628 end;
5629 enttext ();
5630 scrollindicator ();
5631 Wsi.swapb ();
5634 let zoomrect x y x1 y1 =
5635 let x0 = min x x1
5636 and x1 = max x x1
5637 and y0 = min y y1 in
5638 gotoy (state.y + y0);
5639 state.anchor <- getanchor ();
5640 let zoom = (float state.w) /. float (x1 - x0) in
5641 let margin =
5642 let simple () =
5643 let adjw = wadjsb () + state.winw in
5644 if state.w < adjw
5645 then (adjw - state.w) / 2
5646 else 0
5648 match conf.fitmodel with
5649 | FitWidth | FitProportional -> simple ()
5650 | FitPage ->
5651 match conf.columns with
5652 | Csplit _ ->
5653 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
5654 | Cmulti _ | Csingle _ -> simple ()
5656 state.x <- (state.x + margin) - x0;
5657 setzoom zoom;
5658 resetmstate ();
5661 let annot inline x y =
5662 match unproject x y with
5663 | Some (opaque, n, ux, uy) ->
5664 let add text =
5665 addannot opaque ux uy text;
5666 wcmd "freepage %s" (~> opaque);
5667 Hashtbl.remove state.pagemap (n, state.gen);
5668 flushtiles ();
5669 gotoy state.y
5671 if inline
5672 then
5673 let ondone s = add s in
5674 let mode = state.mode in
5675 state.mode <- Textentry (
5676 ("annotation: ", E.s, None, textentry, ondone, true),
5677 fun _ -> state.mode <- mode);
5678 state.text <- E.s;
5679 enttext ();
5680 G.postRedisplay "annot"
5681 else
5682 add @@ getusertext E.s
5683 | _ -> ()
5686 let zoomblock x y =
5687 let g opaque l px py =
5688 match rectofblock opaque px py with
5689 | Some a ->
5690 let x0 = a.(0) -. 20. in
5691 let x1 = a.(1) +. 20. in
5692 let y0 = a.(2) -. 20. in
5693 let zoom = (float state.w) /. (x1 -. x0) in
5694 let pagey = getpagey l.pageno in
5695 gotoy_and_clear_text (pagey + truncate y0);
5696 state.anchor <- getanchor ();
5697 let margin = (state.w - l.pagew)/2 in
5698 state.x <- -truncate x0 - margin;
5699 setzoom zoom;
5700 None
5701 | None -> None
5703 match conf.columns with
5704 | Csplit _ ->
5705 impmsg "block zooming does not work properly in split columns mode"
5706 | Cmulti _ | Csingle _ -> onppundermouse g x y ()
5709 let scrollx x =
5710 let winw = wadjsb () + state.winw - 1 in
5711 let s = float x /. float winw in
5712 let destx = truncate (float (state.w + winw) *. s) in
5713 state.x <- winw - destx;
5714 gotoy_and_clear_text state.y;
5715 state.mstate <- Mscrollx;
5718 let scrolly y =
5719 let s = float y /. float state.winh in
5720 let desty = truncate (float (state.maxy - state.winh) *. s) in
5721 gotoy_and_clear_text desty;
5722 state.mstate <- Mscrolly;
5725 let viewmulticlick clicks x y mask =
5726 let g opaque l px py =
5727 let mark =
5728 match clicks with
5729 | 2 -> Mark_word
5730 | 3 -> Mark_line
5731 | 4 -> Mark_block
5732 | _ -> Mark_page
5734 if markunder opaque px py mark
5735 then (
5736 Some (fun () ->
5737 let dopipe cmd =
5738 match getopaque l.pageno with
5739 | None -> ()
5740 | Some opaque -> pipesel opaque cmd
5742 state.roam <- (fun () -> dopipe conf.paxcmd);
5743 if not (Wsi.withctrl mask) then dopipe conf.selcmd;
5746 else None
5748 G.postRedisplay "viewmulticlick";
5749 onppundermouse g x y (fun () -> impmsg "nothing to select") ();
5752 let canselect () =
5753 match conf.columns with
5754 | Csplit _ -> false
5755 | Csingle _ | Cmulti _ -> conf.angle mod 360 = 0
5758 let viewmouse button down x y mask =
5759 match button with
5760 | n when (n == 4 || n == 5) && not down ->
5761 if Wsi.withctrl mask
5762 then (
5763 match state.mstate with
5764 | Mzoom (oldn, i) ->
5765 if oldn = n
5766 then (
5767 if i = 2
5768 then
5769 let incr =
5770 match n with
5771 | 5 ->
5772 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5773 | _ ->
5774 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5776 let zoom = conf.zoom -. incr in
5777 setzoom zoom;
5778 state.mstate <- Mzoom (n, 0);
5779 else
5780 state.mstate <- Mzoom (n, i+1);
5782 else state.mstate <- Mzoom (n, 0)
5784 | Msel _
5785 | Mpan _
5786 | Mscrolly | Mscrollx
5787 | Mzoomrect _
5788 | Mnone -> state.mstate <- Mzoom (n, 0)
5790 else (
5791 match state.autoscroll with
5792 | Some step -> setautoscrollspeed step (n=4)
5793 | None ->
5794 if conf.wheelbypage || conf.presentation
5795 then (
5796 if n = 4
5797 then prevpage ()
5798 else nextpage ()
5800 else
5801 let incr =
5802 if n = 4
5803 then -conf.scrollstep
5804 else conf.scrollstep
5806 let incr = incr * 2 in
5807 let y = clamp incr in
5808 gotoy_and_clear_text y
5811 | n when (n = 6 || n = 7) && not down && canpan () ->
5812 state.x <-
5813 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
5814 gotoy_and_clear_text state.y
5816 | 1 when Wsi.withshift mask ->
5817 state.mstate <- Mnone;
5818 if not down
5819 then (
5820 match unproject x y with
5821 | None -> ()
5822 | Some (_, pageno, ux, uy) ->
5823 let cmd = Printf.sprintf
5824 "%s %s %d %d %d"
5825 conf.stcmd state.path pageno ux uy
5827 match spawn cmd [] with
5828 | (exception exn) ->
5829 impmsg "execution of synctex command(%S) failed: %S"
5830 conf.stcmd @@ exntos exn
5831 | _pid -> ()
5834 | 1 when Wsi.withctrl mask ->
5835 if down
5836 then (
5837 Wsi.setcursor Wsi.CURSOR_FLEUR;
5838 state.mstate <- Mpan (x, y)
5840 else
5841 state.mstate <- Mnone
5843 | 3 ->
5844 if down
5845 then (
5846 if Wsi.withshift mask
5847 then (
5848 annot conf.annotinline x y;
5849 G.postRedisplay "addannot"
5851 else
5852 let p = (x, y) in
5853 Wsi.setcursor Wsi.CURSOR_CYCLE;
5854 state.mstate <- Mzoomrect (p, p)
5856 else (
5857 match state.mstate with
5858 | Mzoomrect ((x0, y0), _) ->
5859 if abs (x-x0) > 10 && abs (y - y0) > 10
5860 then zoomrect x0 y0 x y
5861 else (
5862 resetmstate ();
5863 G.postRedisplay "kill accidental zoom rect";
5865 | Msel _
5866 | Mpan _
5867 | Mscrolly | Mscrollx
5868 | Mzoom _
5869 | Mnone ->
5870 resetmstate ()
5873 | 1 when vscrollhit x ->
5874 if down
5875 then
5876 let _, position, sh = state.uioh#scrollph in
5877 if y > truncate position && y < truncate (position +. sh)
5878 then state.mstate <- Mscrolly
5879 else scrolly y
5880 else
5881 state.mstate <- Mnone
5883 | 1 when y > state.winh - hscrollh () ->
5884 if down
5885 then
5886 let _, position, sw = state.uioh#scrollpw in
5887 if x > truncate position && x < truncate (position +. sw)
5888 then state.mstate <- Mscrollx
5889 else scrollx x
5890 else
5891 state.mstate <- Mnone
5893 | 1 when state.bzoom -> if not down then zoomblock x y
5895 | 1 ->
5896 let dest = if down then getunder x y else Unone in
5897 begin match dest with
5898 | Ulinkgoto _
5899 | Ulinkuri _
5900 | Uremote _ | Uremotedest _
5901 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5902 gotounder dest
5904 | Unone when down ->
5905 Wsi.setcursor Wsi.CURSOR_FLEUR;
5906 state.mstate <- Mpan (x, y);
5908 | Uannotation (opaque, slinkindex) -> enterannotmode opaque slinkindex
5910 | Unone | Utext _ ->
5911 if down
5912 then (
5913 if canselect ()
5914 then (
5915 state.mstate <- Msel ((x, y), (x, y));
5916 G.postRedisplay "mouse select";
5919 else (
5920 match state.mstate with
5921 | Mnone -> ()
5923 | Mzoom _ | Mscrollx | Mscrolly ->
5924 state.mstate <- Mnone
5926 | Mzoomrect ((x0, y0), _) ->
5927 zoomrect x0 y0 x y
5929 | Mpan _ ->
5930 Wsi.setcursor Wsi.CURSOR_INHERIT;
5931 state.mstate <- Mnone
5933 | Msel ((x0, y0), (x1, y1)) ->
5934 let rec loop = function
5935 | [] -> ()
5936 | l :: rest ->
5937 let inside =
5938 let a0 = l.pagedispy in
5939 let a1 = a0 + l.pagevh in
5940 let b0 = l.pagedispx in
5941 let b1 = b0 + l.pagevw in
5942 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
5943 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
5945 if inside
5946 then
5947 match getopaque l.pageno with
5948 | Some opaque ->
5949 let dosel cmd () =
5950 match Unix.pipe () with
5951 | (exception exn) ->
5952 impmsg "cannot create sel pipe: %s" @@
5953 exntos exn;
5954 | (r, w) ->
5955 let clo what fd =
5956 Ne.clo fd (fun msg ->
5957 dolog "%s close failed: %s" what msg)
5959 let pid =
5960 try spawn cmd [r, 0; w, -1]
5961 with exn ->
5962 dolog "cannot execute %S: %s"
5963 cmd @@ exntos exn;
5966 if pid > 0
5967 then (
5968 copysel w opaque;
5969 G.postRedisplay "copysel";
5971 else clo "Msel pipe/w" w;
5972 clo "Msel pipe/r" r;
5974 dosel conf.selcmd ();
5975 state.roam <- dosel conf.paxcmd;
5976 | None -> ()
5977 else loop rest
5979 loop state.layout;
5980 resetmstate ();
5984 | _ -> ()
5987 let birdseyemouse button down x y mask
5988 (conf, leftx, _, hooverpageno, anchor) =
5989 match button with
5990 | 1 when down ->
5991 let rec loop = function
5992 | [] -> ()
5993 | l :: rest ->
5994 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5995 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5996 then (
5997 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5999 else loop rest
6001 loop state.layout
6002 | 3 -> ()
6003 | _ -> viewmouse button down x y mask
6006 let uioh = object
6007 method display = ()
6009 method key key mask =
6010 begin match state.mode with
6011 | Textentry textentry -> textentrykeyboard key mask textentry
6012 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6013 | View -> viewkeyboard key mask
6014 | LinkNav linknav -> linknavkeyboard key mask linknav
6015 end;
6016 state.uioh
6018 method button button bstate x y mask =
6019 begin match state.mode with
6020 | LinkNav _
6021 | View -> viewmouse button bstate x y mask
6022 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6023 | Textentry _ -> ()
6024 end;
6025 state.uioh
6027 method multiclick clicks x y mask =
6028 begin match state.mode with
6029 | LinkNav _
6030 | View -> viewmulticlick clicks x y mask
6031 | Birdseye _
6032 | Textentry _ -> ()
6033 end;
6034 state.uioh
6036 method motion x y =
6037 begin match state.mode with
6038 | Textentry _ -> ()
6039 | View | Birdseye _ | LinkNav _ ->
6040 match state.mstate with
6041 | Mzoom _ | Mnone -> ()
6043 | Mpan (x0, y0) ->
6044 let dx = x - x0
6045 and dy = y0 - y in
6046 state.mstate <- Mpan (x, y);
6047 if canpan ()
6048 then state.x <- panbound (state.x + dx);
6049 let y = clamp dy in
6050 gotoy_and_clear_text y
6052 | Msel (a, _) ->
6053 state.mstate <- Msel (a, (x, y));
6054 G.postRedisplay "motion select";
6056 | Mscrolly ->
6057 let y = min state.winh (max 0 y) in
6058 scrolly y
6060 | Mscrollx ->
6061 let x = min state.winw (max 0 x) in
6062 scrollx x
6064 | Mzoomrect (p0, _) ->
6065 state.mstate <- Mzoomrect (p0, (x, y));
6066 G.postRedisplay "motion zoomrect";
6067 end;
6068 state.uioh
6070 method pmotion x y =
6071 begin match state.mode with
6072 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6073 let rec loop = function
6074 | [] ->
6075 if hooverpageno != -1
6076 then (
6077 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6078 G.postRedisplay "pmotion birdseye no hoover";
6080 | l :: rest ->
6081 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6082 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6083 then (
6084 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6085 G.postRedisplay "pmotion birdseye hoover";
6087 else loop rest
6089 loop state.layout
6091 | Textentry _ -> ()
6093 | LinkNav _
6094 | View ->
6095 match state.mstate with
6096 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ -> ()
6097 | Mnone ->
6098 updateunder x y;
6099 if canselect ()
6100 then
6101 match conf.pax with
6102 | None -> ()
6103 | Some r ->
6104 let past, _, _ = !r in
6105 let now = now () in
6106 let delta = now -. past in
6107 if delta > 0.01
6108 then paxunder x y
6109 else r := (now, x, y)
6110 end;
6111 state.uioh
6113 method infochanged _ = ()
6115 method scrollph =
6116 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6117 let p, h =
6118 if maxy = 0
6119 then 0.0, float state.winh
6120 else scrollph state.y maxy
6122 vscrollw (), p, h
6124 method scrollpw =
6125 let winw = wadjsb () + state.winw in
6126 let fwinw = float winw in
6127 let sw =
6128 let sw = fwinw /. float state.w in
6129 let sw = fwinw *. sw in
6130 max sw (float conf.scrollh)
6132 let position =
6133 let maxx = state.w + winw in
6134 let x = winw - state.x in
6135 let percent = float x /. float maxx in
6136 (fwinw -. sw) *. percent
6138 hscrollh (), position, sw
6140 method modehash =
6141 let modename =
6142 match state.mode with
6143 | LinkNav _ -> "links"
6144 | Textentry _ -> "textentry"
6145 | Birdseye _ -> "birdseye"
6146 | View -> "view"
6148 findkeyhash conf modename
6150 method eformsgs = true
6151 method alwaysscrolly = false
6152 end;;
6154 let adderrmsg src msg =
6155 Buffer.add_string state.errmsgs msg;
6156 state.newerrmsgs <- true;
6157 G.postRedisplay src
6160 let adderrfmt src fmt =
6161 Format.ksprintf (fun s -> adderrmsg src s) fmt;
6164 let addrect pageno r g b a x0 y0 x1 y1 =
6165 Hashtbl.add state.prects pageno [|r; g; b; a; x0; y0; x1; y1|];
6168 let ract cmds =
6169 let cl = splitatspace cmds in
6170 let scan s fmt f =
6171 try Scanf.sscanf s fmt f
6172 with exn ->
6173 adderrfmt "remote exec"
6174 "error processing '%S': %s\n" cmds @@ exntos exn
6176 let rectx s pageno (r, g, b, a) x0 y0 x1 y1 =
6177 vlog "%s page %d color (%f %f %f %f) x0,y0,x1,y1 = %f %f %f %f"
6178 s pageno r g b a x0 y0 x1 y1;
6179 onpagerect
6180 pageno
6181 (fun w h ->
6182 let _,w1,h1,_ = getpagedim pageno in
6183 let sw = float w1 /. float w
6184 and sh = float h1 /. float h in
6185 let x0s = x0 *. sw
6186 and x1s = x1 *. sw
6187 and y0s = y0 *. sh
6188 and y1s = y1 *. sh in
6189 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
6190 let color = (r, g, b, a) in
6191 if conf.verbose then debugrect rect;
6192 state.rects <- (pageno, color, rect) :: state.rects;
6193 G.postRedisplay s;
6196 match cl with
6197 | "reload" :: [] -> reload ()
6198 | "goto" :: args :: [] ->
6199 scan args "%u %f %f"
6200 (fun pageno x y ->
6201 let cmd, _ = state.geomcmds in
6202 if emptystr cmd
6203 then gotopagexy !wtmode pageno x y
6204 else
6205 let f prevf () =
6206 gotopagexy !wtmode pageno x y;
6207 prevf ()
6209 state.reprf <- f state.reprf
6211 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
6212 | "gotor" :: args :: [] ->
6213 scan args "%S %u"
6214 (fun filename pageno -> gotounder (Uremote (filename, pageno)))
6215 | "gotord" :: args :: [] ->
6216 scan args "%S %S"
6217 (fun filename dest -> gotounder (Uremotedest (filename, dest)))
6218 | "rect" :: args :: [] ->
6219 scan args "%u %u %f %f %f %f"
6220 (fun pageno c x0 y0 x1 y1 ->
6221 let color = (0.0, 0.0, 1.0 /. float c, 0.5) in
6222 rectx "rect" pageno color x0 y0 x1 y1;
6224 | "prect" :: args :: [] ->
6225 scan args "%u %f %f %f %f %f %f %f %f"
6226 (fun pageno r g b alpha x0 y0 x1 y1 ->
6227 addrect pageno r g b alpha x0 y0 x1 y1;
6228 G.postRedisplay "prect"
6230 | "pgoto" :: args :: [] ->
6231 scan args "%u %f %f"
6232 (fun pageno x y ->
6233 let optopaque =
6234 match getopaque pageno with
6235 | Some opaque -> opaque
6236 | None -> ~< E.s
6238 pgoto optopaque pageno x y;
6239 let rec fixx = function
6240 | [] -> ()
6241 | l :: rest ->
6242 if l.pageno = pageno
6243 then (
6244 state.x <- state.x - l.pagedispx;
6245 gotoy state.y;
6247 else fixx rest
6249 let layout =
6250 let mult =
6251 match conf.columns with
6252 | Csingle _ | Csplit _ -> 1
6253 | Cmulti ((n, _, _), _) -> n
6255 layout 0 state.y (state.winw * mult) state.winh
6257 fixx layout
6259 | "activatewin" :: [] -> Wsi.activatewin ()
6260 | "quit" :: [] -> raise Quit
6261 | "clearrects" :: [] ->
6262 Hashtbl.clear state.prects;
6263 G.postRedisplay "clearrects"
6264 | _ ->
6265 adderrfmt "remote command"
6266 "error processing remote command: %S\n" cmds;
6269 let remote =
6270 let scratch = Bytes.create 80 in
6271 let buf = Buffer.create 80 in
6272 fun fd ->
6273 match tempfailureretry (Unix.read fd scratch 0) 80 with
6274 | (exception Unix.Unix_error (Unix.EAGAIN, _, _)) -> None
6275 | 0 ->
6276 Unix.close fd;
6277 if Buffer.length buf > 0
6278 then (
6279 let s = Buffer.contents buf in
6280 Buffer.clear buf;
6281 ract s;
6283 None
6284 | n ->
6285 let rec eat ppos =
6286 let nlpos =
6287 match Bytes.index_from scratch ppos '\n' with
6288 | pos -> if pos >= n then -1 else pos
6289 | (exception Not_found) -> -1
6291 if nlpos >= 0
6292 then (
6293 Buffer.add_subbytes buf scratch ppos (nlpos-ppos);
6294 let s = Buffer.contents buf in
6295 Buffer.clear buf;
6296 ract s;
6297 eat (nlpos+1);
6299 else (
6300 Buffer.add_subbytes buf scratch ppos (n-ppos);
6301 Some fd
6303 in eat 0
6306 let remoteopen path =
6307 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
6308 with exn ->
6309 adderrfmt "remoteopen" "error opening %S: %s" path @@ exntos exn;
6310 None
6313 let () =
6314 let gcconfig = ref E.s in
6315 let trimcachepath = ref E.s in
6316 let rcmdpath = ref E.s in
6317 let pageno = ref None in
6318 let rootwid = ref 0 in
6319 let openlast = ref false in
6320 let nofc = ref false in
6321 let doreap = ref false in
6322 selfexec := Sys.executable_name;
6323 Arg.parse
6324 (Arg.align
6325 [("-p", Arg.String (fun s -> state.password <- s),
6326 "<password> Set password");
6328 ("-f", Arg.String
6329 (fun s ->
6330 Config.fontpath := s;
6331 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
6333 "<path> Set path to the user interface font");
6335 ("-c", Arg.String
6336 (fun s ->
6337 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
6338 Config.confpath := s),
6339 "<path> Set path to the configuration file");
6341 ("-last", Arg.Set openlast, " Open last document");
6343 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
6344 "<page-number> Jump to page");
6346 ("-tcf", Arg.String (fun s -> trimcachepath := s),
6347 "<path> Set path to the trim cache file");
6349 ("-dest", Arg.String (fun s -> state.nameddest <- s),
6350 "<named-destination> Set named destination");
6352 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
6353 ("-cxack", Arg.Set cxack, " Cut corners");
6355 ("-remote", Arg.String (fun s -> rcmdpath := s),
6356 "<path> Set path to the remote commands source");
6358 ("-origin", Arg.String (fun s -> state.origin <- s),
6359 "<original-path> Set original path");
6361 ("-gc", Arg.Set_string gcconfig,
6362 "<script-path> Collect garbage with the help of a script");
6364 ("-nofc", Arg.Set nofc, " Do not use fontconfig");
6366 ("-v", Arg.Unit (fun () ->
6367 Printf.printf
6368 "%s\nconfiguration path: %s\n"
6369 (version ())
6370 Config.defconfpath
6372 exit 0), " Print version and exit");
6374 ("-embed", Arg.Set_int rootwid,
6375 "<window-id> Embed into window")
6378 (fun s -> state.path <- s)
6379 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6381 if !wtmode
6382 then selfexec := !selfexec ^ " -wtmode";
6384 let histmode = emptystr state.path && not !openlast in
6386 if not (Config.load !openlast)
6387 then dolog "failed to load configuration";
6388 begin match !pageno with
6389 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
6390 | None -> ()
6391 end;
6393 if nonemptystr !gcconfig
6394 then (
6395 let (c, s) =
6396 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6397 | (exception exn) -> error "socketpair for gc failed: %s" @@ exntos exn
6398 | fds -> fds
6400 match spawn !gcconfig [(c, 0); (c, 1); (s, -1)] with
6401 | (exception exn) -> error "failed to execute gc script: %s" @@ exntos exn
6402 | _pid ->
6403 Ne.clo c @@ (fun s -> error "failed to close gc fd %s" s);
6404 Config.gc s;
6405 exit 0
6408 let wsfd, winw, winh = Wsi.init (object (self)
6409 val mutable m_clicks = 0
6410 val mutable m_click_x = 0
6411 val mutable m_click_y = 0
6412 val mutable m_lastclicktime = infinity
6414 method private cleanup =
6415 state.roam <- noroam;
6416 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap
6417 method expose = G.postRedisplay "expose"
6418 method visible v =
6419 let name =
6420 match v with
6421 | Wsi.Unobscured -> "unobscured"
6422 | Wsi.PartiallyObscured -> "partiallyobscured"
6423 | Wsi.FullyObscured -> "fullyobscured"
6425 vlog "visibility change %s" name
6426 method display = display ()
6427 method map mapped = vlog "mapped %b" mapped
6428 method reshape w h =
6429 self#cleanup;
6430 reshape w h
6431 method mouse b d x y m =
6432 if d && canselect ()
6433 then (
6434 (* http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx *)
6435 m_click_x <- x;
6436 m_click_y <- y;
6437 if b = 1
6438 then (
6439 let t = now () in
6440 if abs x - m_click_x > 10
6441 || abs y - m_click_y > 10
6442 || abs_float (t -. m_lastclicktime) > 0.3
6443 then m_clicks <- 0;
6444 m_clicks <- m_clicks + 1;
6445 m_lastclicktime <- t;
6446 if m_clicks = 1
6447 then (
6448 self#cleanup;
6449 G.postRedisplay "cleanup";
6450 state.uioh <- state.uioh#button b d x y m;
6452 else state.uioh <- state.uioh#multiclick m_clicks x y m
6454 else (
6455 self#cleanup;
6456 m_clicks <- 0;
6457 m_lastclicktime <- infinity;
6458 state.uioh <- state.uioh#button b d x y m
6461 else (
6462 state.uioh <- state.uioh#button b d x y m
6464 method motion x y =
6465 state.mpos <- (x, y);
6466 state.uioh <- state.uioh#motion x y
6467 method pmotion x y =
6468 state.mpos <- (x, y);
6469 state.uioh <- state.uioh#pmotion x y
6470 method key k m =
6471 let mascm = m land (
6472 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6473 ) in
6474 let keyboard k m =
6475 let x = state.x and y = state.y in
6476 keyboard k m;
6477 if x != state.x || y != state.y then self#cleanup
6479 match state.keystate with
6480 | KSnone ->
6481 let km = k, mascm in
6482 begin
6483 match
6484 let modehash = state.uioh#modehash in
6485 try Hashtbl.find modehash km
6486 with Not_found ->
6487 try Hashtbl.find (findkeyhash conf "global") km
6488 with Not_found -> KMinsrt (k, m)
6489 with
6490 | KMinsrt (k, m) -> keyboard k m
6491 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6492 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6494 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6495 List.iter (fun (k, m) -> keyboard k m) insrt;
6496 state.keystate <- KSnone
6497 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6498 state.keystate <- KSinto (keys, insrt)
6499 | KSinto _ -> state.keystate <- KSnone
6501 method enter x y =
6502 state.mpos <- (x, y);
6503 state.uioh <- state.uioh#pmotion x y
6504 method leave = state.mpos <- (-1, -1)
6505 method winstate wsl = state.winstate <- wsl
6506 method quit = raise Quit
6507 end) !rootwid conf.cwinw conf.cwinh platform in
6509 setbgcol conf.bgcolor;
6510 state.wsfd <- wsfd;
6512 if not (
6513 List.exists GlMisc.check_extension
6514 [ "GL_ARB_texture_rectangle"
6515 ; "GL_EXT_texture_recangle"
6516 ; "GL_NV_texture_rectangle" ]
6518 then (dolog "OpenGL does not suppport rectangular textures"; exit 1);
6520 if (
6521 let r = GlMisc.get_string `renderer in
6522 let p = "Mesa DRI Intel(" in
6523 let l = String.length p in
6524 String.length r > l && String.sub r 0 l = p
6526 then (
6527 defconf.sliceheight <- 1024;
6528 defconf.texcount <- 32;
6529 defconf.usepbo <- true;
6532 let cs, ss =
6533 match Unix.socketpair Unix.PF_UNIX Unix.SOCK_STREAM 0 with
6534 | (exception exn) ->
6535 dolog "socketpair failed: %s" @@ exntos exn;
6536 exit 1
6537 | (r, w) ->
6538 cloexec r;
6539 cloexec w;
6540 r, w
6543 setcheckers conf.checkers;
6545 opengl_has_pbo := GlMisc.check_extension "GL_ARB_pixel_buffer_object";
6547 init cs (
6548 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
6549 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6550 !Config.fontpath, !trimcachepath,
6551 !opengl_has_pbo,
6552 not !nofc
6554 List.iter GlArray.enable [`texture_coord; `vertex];
6555 state.ss <- ss;
6556 reshape ~firsttime:true winw winh;
6557 state.uioh <- uioh;
6558 if histmode
6559 then (
6560 Wsi.settitle "llpp (history)";
6561 enterhistmode ();
6563 else (
6564 state.text <- "Opening " ^ (mbtoutf8 state.path);
6565 opendoc state.path state.password;
6567 display ();
6568 Wsi.mapwin ();
6569 Wsi.setcursor Wsi.CURSOR_INHERIT;
6570 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
6572 let rec reap () =
6573 match Unix.waitpid [Unix.WNOHANG] ~-1 with
6574 | (exception (Unix.Unix_error (Unix.ECHILD, _, _))) -> ()
6575 | (exception exn) -> dolog "Unix.waitpid: %s" @@ exntos exn
6576 | 0, _ -> ()
6577 | _pid, _status -> reap ()
6579 Sys.set_signal Sys.sigchld (Sys.Signal_handle (fun _ -> doreap := true));
6581 let optrfd =
6582 ref (
6583 if nonemptystr !rcmdpath
6584 then remoteopen !rcmdpath
6585 else None
6589 let rec loop deadline =
6590 if !doreap
6591 then (
6592 doreap := false;
6593 reap ()
6595 let r = [state.ss; state.wsfd] in
6596 let r =
6597 match !optrfd with
6598 | None -> r
6599 | Some fd -> fd :: r
6601 if state.redisplay
6602 then (
6603 state.redisplay <- false;
6604 display ();
6606 let timeout =
6607 let now = now () in
6608 if deadline > now
6609 then (
6610 if deadline = infinity
6611 then ~-.1.0
6612 else max 0.0 (deadline -. now)
6614 else 0.0
6616 let r, _, _ =
6617 try Unix.select r [] [] timeout
6618 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6620 begin match r with
6621 | [] ->
6622 state.ghyll None;
6623 let newdeadline =
6624 if state.ghyll == noghyll
6625 then
6626 match state.autoscroll with
6627 | Some step when step != 0 ->
6628 let y = state.y + step in
6629 let y =
6630 if y < 0
6631 then state.maxy
6632 else if y >= state.maxy then 0 else y
6634 if state.mode = View
6635 then gotoy_and_clear_text y
6636 else gotoy y;
6637 deadline +. 0.01
6638 | _ -> infinity
6639 else deadline +. 0.01
6641 loop newdeadline
6643 | l ->
6644 let rec checkfds = function
6645 | [] -> ()
6646 | fd :: rest when fd = state.ss ->
6647 let cmd = readcmd state.ss in
6648 act cmd;
6649 checkfds rest
6651 | fd :: rest when fd = state.wsfd ->
6652 Wsi.readresp fd;
6653 checkfds rest
6655 | fd :: rest when Some fd = !optrfd ->
6656 begin match remote fd with
6657 | None -> optrfd := remoteopen !rcmdpath;
6658 | opt -> optrfd := opt
6659 end;
6660 checkfds rest
6662 | _ :: rest ->
6663 dolog "select returned unknown descriptor";
6664 checkfds rest
6666 checkfds l;
6667 let newdeadline =
6668 let deadline1 =
6669 if deadline = infinity
6670 then now () +. 0.01
6671 else deadline
6673 match state.autoscroll with
6674 | Some step when step != 0 -> deadline1
6675 | _ -> if state.ghyll == noghyll then infinity else deadline1
6677 loop newdeadline
6678 end;
6681 loop infinity;
6682 with Quit ->
6683 Config.save leavebirdseye;
6684 if hasunsavedchanges ()
6685 then save ();