4 | Ulinkgoto
of (int * int)
6 and facename
= string;;
8 let dolog fmt
= Printf.kprintf prerr_endline fmt
;;
13 angle
* proportional
* texcount
* sliceheight
* blockwidth
* fontpath
22 and proportional
= bool
23 and interpagespace
= int
32 external init
: Unix.file_descr
-> params
-> unit = "ml_init";;
33 external draw
: (int * int * int * bool) -> string -> unit = "ml_draw";;
34 external seltext
: string -> (int * int * int * int) -> int -> unit =
36 external copysel
: string -> unit = "ml_copysel";;
37 external getpdimrect
: int -> float array
= "ml_getpdimrect";;
38 external whatsunder
: string -> int -> int -> under
= "ml_whatsunder";;
39 external zoomforh
: int -> int -> int -> float = "ml_zoom_for_height";;
40 external drawstr
: int -> int -> int -> string -> float = "ml_draw_string";;
41 external measurestr
: int -> string -> float = "ml_measure_string";;
42 external getmaxw
: unit -> float = "ml_getmaxw";;
46 | Msel
of (mpos
* mpos
)
49 | Mzoom
of (int * int)
53 type textentry
= string * string * onhist
option * onkey
* ondone
54 and onkey
= string -> int -> te
55 and ondone
= string -> unit
56 and histcancel
= unit -> unit
57 and onhist
= ((histcmd
-> string) * histcancel
)
58 and histcmd
= HCnext
| HCprev
| HCfirst
| HClast
63 | TEswitch
of textentry
75 { store
= Array.create n v
82 let cbcap b
= Array.length b
.store
;;
87 b
.wc
<- (b
.wc
+ 1) mod cap;
89 b
.len
<- min
(b
.len
+ 1) cap;
92 let cbempty b
= b
.len
= 0;;
94 let cbgetg b circular dir
=
98 let rc = b
.rc + dir
in
110 else max
0 (min
rc (b
.len
-1))
116 let cbget b
= cbgetg b
false;;
117 let cbgetc b
= cbgetg b
true;;
120 let rc = b
.wc
- b
.len
in
121 let rc = if rc < 0 then cbcap b
+ rc else rc in
125 let cbdecr b
= b
.len
<- b
.len
- 1;;
140 { mutable scrollbw
: int
141 ; mutable scrollh
: int
142 ; mutable icase
: bool
143 ; mutable preload
: bool
144 ; mutable pagebias
: int
145 ; mutable verbose
: bool
146 ; mutable scrollstep
: int
147 ; mutable maxhfit
: bool
148 ; mutable crophack
: bool
149 ; mutable autoscrollstep
: int
150 ; mutable showall
: bool
151 ; mutable hlinks
: bool
152 ; mutable underinfo
: bool
153 ; mutable interpagespace
: interpagespace
154 ; mutable zoom
: float
155 ; mutable presentation
: bool
156 ; mutable angle
: angle
159 ; mutable savebmarks
: bool
160 ; mutable proportional
: proportional
161 ; mutable memlimit
: int
162 ; mutable texcount
: texcount
163 ; mutable sliceheight
: sliceheight
164 ; mutable blockwidth
: blockwidth
165 ; mutable thumbw
: width
166 ; mutable jumpback
: bool
167 ; mutable bgcolor
: float * float * float
168 ; mutable bedefault
: bool
169 ; mutable scrollbarinpm
: bool
173 type anchor
= pageno
* top
;;
175 type outline
= string * int * anchor
177 | Oarray
of outline array
178 | Olist
of outline list
179 | Onarrow
of string * outline array
* outline array
182 type rect
= float * float * float * float * float * float * float * float;;
184 type pagemapkey
= pageno
* width
* angle
* proportional
* gen
;;
186 let emptyanchor = (0, 0.0);;
189 | Birdseye
of (conf
* leftx
* pageno
* pageno
* anchor
)
190 | Outline
of (bool * int * int * outline array
* string * int * mode
)
191 | Items
of (int * int * item array
* string * int * mode
)
192 | Textentry
of (textentry
* onleave
)
194 and onleave
= leavetextentrystatus
-> unit
195 and leavetextentrystatus
= | Cancel
| Confirm
196 and item
= string * int * action
199 | Action
of (int -> int -> string -> int -> mode
)
202 let isbirdseye = function Birdseye _
-> true | _
-> false;;
203 let istextentry = function Textentry _
-> true | _
-> false;;
206 { mutable csock
: Unix.file_descr
207 ; mutable ssock
: Unix.file_descr
211 ; mutable scrollw
: int
212 ; mutable anchor
: anchor
214 ; mutable layout
: layout list
215 ; pagemap
: (pagemapkey
, (opaque
* pixmapsize
)) Hashtbl.t
216 ; mutable pdims
: (pageno
* width
* height
* leftx
) list
217 ; mutable pagecount
: int
218 ; pagecache
: string circbuf
219 ; mutable rendering
: bool
220 ; mutable mstate
: mstate
221 ; mutable searchpattern
: string
222 ; mutable rects
: (pageno
* recttype
* rect
) list
223 ; mutable rects1
: (pageno
* recttype
* rect
) list
224 ; mutable text
: string
225 ; mutable fullscreen
: (width
* height
) option
226 ; mutable mode
: mode
227 ; mutable outlines
: outlines
228 ; mutable bookmarks
: outline list
229 ; mutable path
: string
230 ; mutable password
: string
231 ; mutable invalidated
: int
232 ; mutable colorscale
: float
233 ; mutable memused
: int
235 ; mutable throttle
: layout list
option
236 ; mutable autoscroll
:int option
237 ; mutable help
: item array
238 ; mutable docinfo
: (int * string) list
239 ; mutable deadline
: float
243 { pat
: string circbuf
244 ; pag
: string circbuf
245 ; nav
: anchor circbuf
265 ; presentation
= false
270 ; proportional
= true
271 ; memlimit
= 32*1024*1024
277 ; bgcolor
= (0.5, 0.5, 0.5)
279 ; scrollbarinpm
= true
283 let conf = { defconf with angle
= defconf.angle
};;
286 let strings = ("llpp version " ^
Help.version
) :: "" :: Help.keys
in
287 Array.of_list
(List.map
(fun s
-> s
, 0, Noaction
) strings);
297 ; anchor
= emptyanchor
300 ; pagemap
= Hashtbl.create
10
301 ; pagecache
= cbnew 100 ""
312 ; outlines
= Olist
[]
318 { nav
= cbnew 100 (0, 0.0)
336 Printf.kprintf prerr_endline fmt
338 Printf.kprintf ignore fmt
342 let len = String.length s
in
344 let b = Buffer.create
n in
345 Buffer.add_char
b (Char.chr
((len lsr 24) land 0xff));
346 Buffer.add_char
b (Char.chr
((len lsr 16) land 0xff));
347 Buffer.add_char
b (Char.chr
((len lsr 8) land 0xff));
348 Buffer.add_char
b (Char.chr
((len lsr 0) land 0xff));
349 Buffer.add_string
b s
;
350 let s'
= Buffer.contents
b in
351 let n'
= Unix.write fd
s'
0 n in
352 if n'
!= n then failwith
"write failed";
357 let n = Unix.read fd
s 0 4 in
358 if n != 4 then failwith
"incomplete read(len)";
360 lor (Char.code
s.[0] lsl 24)
361 lor (Char.code
s.[1] lsl 16)
362 lor (Char.code
s.[2] lsl 8)
363 lor (Char.code
s.[3] lsl 0)
365 let s = String.create
len in
366 let n = Unix.read fd
s 0 len in
367 if n != len then failwith
"incomplete read(data)";
372 let b = Buffer.create
10 in
373 Buffer.add_string
b s;
374 let rec combine = function
377 Buffer.add_char
b ' '
;
380 | `
b b -> if b then "1" else "0"
382 | `i i
-> string_of_int i
383 | `f f
-> string_of_float f
384 | `I f
-> string_of_int
(truncate f
)
386 Buffer.add_string
b s;
393 let cmd = Buffer.contents
(makecmd s l
) in
394 writecmd state.csock
cmd;
400 let d = conf.winh
- h
in
407 let rec f pn ph pi fh l
=
409 | (n, _
, h
, _
) :: rest
->
410 let ips = calcips h
in
415 if isbirdseye state.mode
&& pn
= 0
420 let fh = fh + ((n - pn
) * (ph
+ pi
)) in
425 if conf.presentation
|| (isbirdseye state.mode
&& pn
= 0)
429 let fh = fh + ((state.pagecount
- pn
) * (ph
+ pi
)) + inc in
432 let fh = f 0 0 0 0 state.pdims
in
436 let getpageyh pageno
=
437 let rec f pn ph pi y l
=
439 | (n, _
, h
, _
) :: rest
->
440 let ips = calcips h
in
443 let h = if n = pageno
then h else ph
in
444 if conf.presentation
&& n = pageno
446 y
+ (pageno
- pn
) * (ph
+ pi
) + pi
, h
448 y
+ (pageno
- pn
) * (ph
+ pi
), h
450 let y = y + (if conf.presentation
then pi
else 0) in
451 let y = y + (n - pn
) * (ph
+ pi
) in
455 y + (pageno
- pn
) * (ph
+ pi
), ph
457 f 0 0 0 0 state.pdims
460 let getpagey pageno
= fst
(getpageyh pageno
);;
463 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu
=
464 let ((w
, h, ips, x
) as curr
), rest
, pdimno
, yinc
=
466 | (pageno'
, w
, h, x
) :: rest
when pageno'
= pageno
->
467 let ips = calcips h in
469 if conf.presentation
|| (isbirdseye state.mode
&& pageno
= 0)
473 (w
, h, ips, x
), rest
, pdimno
+ 1, yinc
475 prev
, pdims
, pdimno
, 0
477 let dy = dy + yinc in
478 let py = py + yinc in
479 if pageno
= state.pagecount
|| cacheleft
= 0 || dy >= sh
484 if py + h <= vy - yinc
486 let py = py + h + ips in
487 let dy = max
0 (py - y) in
497 let pagey = vy - py in
498 let pagevh = h - pagey in
499 let pagevh = min
(sh
- dy) pagevh in
500 let off = if yinc > 0 then py - vy else 0 in
501 let py = py + h + ips in
507 ; pagedispy
= dy + off
508 ; pagey = pagey + off
509 ; pagevh = pagevh - off
513 let accu = e :: accu in
520 ~cacheleft
:(cacheleft
-1)
523 if state.invalidated
= 0
533 ~cacheleft
:(cbcap state.pagecache
)
543 let y = state.y + incr
in
545 let y = min
y (state.maxy
- (if conf.maxhfit
then conf.winh
else 0)) in
549 let getopaque pageno
=
550 try Some
(Hashtbl.find
state.pagemap
551 (pageno
, state.w
, conf.angle
, conf.proportional
, state.gen
))
552 with Not_found
-> None
555 let cache pageno opaque
=
556 Hashtbl.replace
state.pagemap
557 (pageno
, state.w
, conf.angle
, conf.proportional
, state.gen
) opaque
560 let validopaque opaque
= String.length opaque
> 0;;
563 match getopaque l
.pageno
with
564 | None
when not
state.rendering
->
565 state.rendering
<- true;
566 cache l
.pageno
("", -1);
567 wcmd "render" [`i
(l
.pageno
+ 1)
574 let loadlayout layout =
575 let rec f all
= function
577 begin match getopaque l
.pageno
with
578 | None
-> render l
; f false ls
579 | Some
(opaque
, _
) -> f (all
&& validopaque opaque
) ls
583 f (layout <> []) layout;
586 let findpageforopaque opaque
=
588 (fun k
(v
, s) a
-> if v
= opaque
then Some
(k
, s) else a
)
592 let pagevisible layout n = List.exists
(fun l
-> l
.pageno
= n) layout;;
596 if conf.preload && not
state.rendering
598 let memleft = conf.memlimit
- state.memused
in
601 let cb = { state.pagecache
with len = state.pagecache
.len } in
602 let rec simgc memleft =
606 let opaque = cbpeek cb in
607 match findpageforopaque opaque with
608 | Some
((n, _
, _
, _
, _
), size
) ->
609 let memleft = memleft + size
in
611 then not
(pagevisible state.layout n)
612 else (cbdecr cb; simgc memleft)
621 let presentation = conf.presentation in
622 let interpagespace = conf.interpagespace in
623 let maxy = state.maxy in
624 conf.presentation <- false;
625 conf.interpagespace <- 0;
626 state.maxy <- calcheight ();
628 match state.layout with
630 | l
:: _
-> getpagey l
.pageno
+ l
.pagey
632 let y = if y < conf.winh
then 0 else y - conf.winh
in
633 let h = state.y - y + conf.winh
*3 in
634 let pages = layout y h in
635 List.iter
render pages;
636 conf.presentation <- presentation;
637 conf.interpagespace <- interpagespace;
643 let y = min
state.maxy y in
644 let pages = layout y conf.winh
in
645 let ready = loadlayout pages in
651 state.layout <- pages;
652 state.throttle
<- None
;
653 Glut.postRedisplay
();
656 state.throttle
<- Some
pages;
661 state.layout <- pages;
662 state.throttle
<- None
;
663 Glut.postRedisplay
();
665 begin match state.mode
with
666 | Birdseye
(conf, leftx
, pageno
, hooverpageno
, anchor
) ->
667 if not
(pagevisible pages pageno
)
669 match state.layout with
672 state.mode
<- Birdseye
(conf, leftx
, l
.pageno
, hooverpageno
, anchor
)
679 let gotoy_and_clear_text y =
681 if not
conf.verbose
then state.text
<- "";
685 match state.layout with
687 | l
:: _
-> (l
.pageno
, float l
.pagey /. float l
.pageh
)
690 let getanchory (n, top
) =
691 let y, h = getpageyh n in
692 y + (truncate
(top
*. float h));
695 let gotoanchor anchor
=
696 gotoy (getanchory anchor
);
700 cbput state.hists
.nav
(getanchor ());
704 let anchor = cbgetc state.hists
.nav dir
in
709 let y, h = getpageyh n in
710 gotoy_and_clear_text (y + (truncate
(top
*. float h)));
713 let gotopage1 n top
=
714 let y = getpagey n in
715 gotoy_and_clear_text (y + top
);
723 state.invalidated
<- state.invalidated
+ 1;
727 let c = c *. state.colorscale
in
731 let scalecolor2 (r
, g
, b) =
732 (r
*. state.colorscale
, g
*. state.colorscale
, b *. state.colorscale
);
736 state.maxy <- calcheight ();
737 match state.mode
with
738 | Birdseye
(_
, _
, pageno
, _
, _
) ->
739 let y, h = getpageyh pageno
in
740 let top = (conf.winh
- h) / 2 in
741 gotoy (max
0 (y - top))
742 | _
-> gotoanchor state.anchor
746 GlMat.mode `projection
;
747 GlMat.load_identity
();
748 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
749 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
750 GlMat.scale3
(2.0 /. float state.w
, 2.0 /. float conf.winh
, 1.0);
753 GlMat.translate ~x
:(float state.x
) ();
758 GlMat.mode `projection
;
759 GlMat.load_identity
();
760 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
761 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
762 GlMat.scale3
(2.0 /. float conf.winw
, 2.0 /. float conf.winh
, 1.0);
766 let firsttime = ref true in
768 if state.invalidated
= 0 && not
!firsttime
769 then state.anchor <- getanchor ();
773 let w = truncate
(float w *. conf.zoom
) - state.scrollw
in
777 GlMat.mode `modelview
;
778 GlMat.load_identity
();
779 GlClear.color
(scalecolor 1.0);
780 GlClear.clear
[`color
];
783 wcmd "geometry" [`i
w; `i
h];
786 let drawstring size x
y s =
788 Gl.enable `texture_2d
;
789 ignore
(drawstr size x
y s);
791 Gl.disable `texture_2d
;
794 let drawstring1 size x
y s =
799 let len = String.length
state.text
in
801 GlDraw.color
(0.0, 0.0, 0.0);
803 (0.0, float (conf.winh
- 18))
804 (float (conf.winw
- state.scrollw
- 1), float conf.winh
)
806 GlDraw.color
(1.0, 1.0, 1.0);
807 drawstring 14 (if len > 0 then 8 else 2) (conf.winh
- 5) s;
809 match state.mode
with
810 | Textentry
((prefix
, text
, _
, _
, _
), _
) ->
814 Printf.sprintf
"%s%s_ [%s]" prefix text
state.text
816 Printf.sprintf
"%s%s_" prefix text
821 if len > 0 then drawstring state.text
825 state.text
<- Printf.sprintf
"%c%s" c s;
826 Glut.postRedisplay
();
835 state.rects
<- state.rects1
;
836 Glut.postRedisplay
()
839 let n = Scanf.sscanf
cmd "C %u" (fun n -> n) in
840 state.pagecount
<- n;
841 state.invalidated
<- state.invalidated
- 1;
842 if state.invalidated
= 0
846 let s = Scanf.sscanf
cmd "t %n"
847 (fun n -> String.sub
cmd n (String.length
cmd - n))
849 Glut.setWindowTitle
s
852 let s = Scanf.sscanf
cmd "T %n"
853 (fun n -> String.sub
cmd n (String.length
cmd - n))
855 if istextentry state.mode
862 Glut.postRedisplay
();
868 let s = Scanf.sscanf
cmd "V %n"
869 (fun n -> String.sub
cmd n (String.length
cmd - n))
875 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
876 Scanf.sscanf
cmd "F %u %d %f %f %f %f %f %f %f %f"
877 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
878 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
880 let y = (getpagey pageno) + truncate y0
in
883 state.rects1
<- [pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)]
886 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
887 Scanf.sscanf
cmd "R %u %d %f %f %f %f %f %f %f %f"
888 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
889 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
892 (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) :: state.rects1
895 let n, w, _h
, r
, l
, s, p
=
896 Scanf.sscanf
cmd "r %u %u %u %d %d %u %s"
897 (fun n w h r l
s p
->
898 (n-1, w, h, r
, l
!= 0, s, p
))
901 Hashtbl.replace
state.pagemap
(n, w, r
, l
, state.gen
) (p
, s);
902 state.memused
<- state.memused
+ s;
905 match state.throttle
with
906 | None
-> state.layout
907 | Some
layout -> layout
911 if (state.memused
<= conf.memlimit
) || cbempty state.pagecache
914 let evictedopaque = cbpeek state.pagecache
in
915 match findpageforopaque evictedopaque with
916 | None
-> failwith
"bug in gc"
917 | Some
((evictedn
, _
, _
, _
, gen
) as k
, evictedsize
) ->
918 if state.gen
!= gen
|| not
(pagevisible layout evictedn
)
920 wcmd "free" [`
s evictedopaque];
921 state.memused
<- state.memused
- evictedsize
;
922 Hashtbl.remove
state.pagemap k
;
923 cbdecr state.pagecache
;
930 cbput state.pagecache p
;
931 state.rendering
<- false;
933 begin match state.throttle
with
935 if pagevisible state.layout n
938 let allvisible = loadlayout state.layout in
939 if allvisible then preload ();
946 let y = getpagey l
.pageno + l
.pagey in
952 Scanf.sscanf
cmd "l %u %u %u %u" (fun n w h x
-> n, w, h, x
)
954 state.pdims
<- pdim :: state.pdims
957 let (l
, n, t
, h, pos
) =
958 Scanf.sscanf
cmd "o %u %u %d %u %n" (fun l
n t
h pos
-> l
, n, t
, h, pos
)
960 let s = String.sub
cmd pos
(String.length
cmd - pos
) in
961 let outline = (s, l
, (n, float t
/. float h)) in
963 match state.outlines with
964 | Olist
outlines -> Olist
(outline :: outlines)
965 | Oarray _
-> Olist
[outline]
966 | Onarrow _
-> Olist
[outline]
968 state.outlines <- outlines
972 if String.length
cmd > 1 && cmd.[1] = '
e'
974 state.docinfo
<- List.rev
state.docinfo
976 let s = Scanf.sscanf
cmd "i %n"
977 (fun n -> String.sub
cmd n (String.length
cmd - n))
979 state.docinfo
<- (1, s) :: state.docinfo
982 dolog "unknown cmd `%S'" cmd
985 let now = Unix.gettimeofday
;;
988 if state.deadline
== nan
then state.deadline
<- now ();
992 then max
0.0 (state.deadline
-. now ())
995 let r, _
, _
= Unix.select
[state.csock
] [] [] timeout in
996 state.deadline
<- state.deadline
+. delay
;
999 begin match state.autoscroll
with
1000 | Some step
when step
!= 0 ->
1001 let y = state.y + step
in
1005 else if y >= state.maxy then 0 else y
1008 if state.mode
= View
1009 then state.text
<- "";
1014 let cmd = readcmd state.csock
in
1023 let action = function
1024 | HCprev
-> cbget cb ~
-1
1025 | HCnext
-> cbget cb 1
1026 | HCfirst
-> cbget cb ~
-(cb.rc)
1027 | HClast
-> cbget cb (cb.len - 1 - cb.rc)
1028 and cancel
() = cb.rc <- rc
1032 let search pattern forward
=
1033 if String.length pattern
> 0
1036 match state.layout with
1039 l
.pageno, (l
.pagey + if forward
then 0 else 0*l
.pagevh)
1042 let b = makecmd "search"
1043 [`
b conf.icase
; `i
pn; `i
py; `i
(if forward
then 1 else 0)]
1045 Buffer.add_char
b '
,'
;
1046 Buffer.add_string
b pattern
;
1047 Buffer.add_char
b '
\000'
;
1050 writecmd state.csock
cmd;
1053 let intentry text key
=
1054 let c = Char.unsafe_chr key
in
1057 let s = "x" in s.[0] <- c;
1058 let text = text ^
s in
1062 state.text <- Printf.sprintf
"invalid char (%d, `%c')" key
c;
1067 let b = Buffer.create
(String.length
s + 1) in
1068 Buffer.add_string
b s;
1069 Buffer.add_char
b c;
1073 let textentry text key
=
1074 let c = Char.unsafe_chr key
in
1076 | _
when key
>= 32 && key
< 127 ->
1077 let text = addchar text c in
1081 dolog "unhandled key %d char `%c'" key
(Char.unsafe_chr key
);
1085 let reinit angle proportional
=
1086 state.anchor <- getanchor ();
1087 conf.angle
<- angle
;
1088 conf.proportional
<- proportional
;
1090 wcmd "reinit" [`i angle
; `
b proportional
];
1094 let zoom = max
0.01 zoom in
1095 if zoom <> conf.zoom
1100 reshape conf.winw
conf.winh
;
1101 state.text <- Printf.sprintf
"zoom is now %-5.1f" (zoom *. 100.0);
1105 let enterbirdseye () =
1106 let zoom = float conf.thumbw
/. float conf.winw
in
1107 let birdseyepageno =
1108 let cy = conf.winh
/ 2 in
1112 let rec fold best
= function
1115 let d = cy - (l
.pagedispy
+ l
.pagevh/2)
1116 and dbest
= cy - (best
.pagedispy
+ best
.pagevh/2) in
1117 if abs
d < abs dbest
1124 state.mode
<- Birdseye
(
1125 { conf with zoom = conf.zoom }, state.x
, birdseyepageno, -1, getanchor ()
1128 conf.presentation <- false;
1129 conf.interpagespace <- 10;
1130 conf.hlinks
<- false;
1132 state.mstate
<- Mnone
;
1133 conf.showall
<- false;
1134 Glut.setCursor
Glut.CURSOR_INHERIT
;
1137 state.text <- Printf.sprintf
"birds eye mode on (zoom %3.1f%%)"
1142 reshape conf.winw
conf.winh
;
1145 let leavebirdseye (c, leftx
, pageno, _
, anchor) goback
=
1147 conf.zoom <- c.zoom;
1148 conf.presentation <- c.presentation;
1149 conf.interpagespace <- c.interpagespace;
1150 conf.showall
<- c.showall
;
1151 conf.hlinks
<- c.hlinks
;
1155 state.text <- Printf.sprintf
"birds eye mode off (zoom %3.1f%%)"
1158 reshape conf.winw
conf.winh
;
1159 state.anchor <- if goback
then anchor else (pageno, 0.0);
1162 let togglebirdseye () =
1163 match state.mode
with
1164 | Birdseye vals
-> leavebirdseye vals
true
1165 | View
| Outline _
-> enterbirdseye ()
1169 let upbirdseye (conf, leftx
, pageno, hooverpageno
, anchor) =
1170 let pageno = max
0 (pageno - 1) in
1171 let rec loop = function
1172 | [] -> gotopage1 pageno 0
1173 | l
:: _
when l
.pageno = pageno ->
1174 if l
.pagedispy
>= 0 && l
.pagey = 0
1175 then Glut.postRedisplay
()
1176 else gotopage1 pageno 0
1177 | _
:: rest
-> loop rest
1180 state.mode
<- Birdseye
(conf, leftx
, pageno, hooverpageno
, anchor)
1183 let downbirdseye (conf, leftx
, pageno, hooverpageno
, anchor) =
1184 let pageno = min
(state.pagecount
- 1) (pageno + 1) in
1185 state.mode
<- Birdseye
(conf, leftx
, pageno, hooverpageno
, anchor);
1186 let rec loop = function
1188 let y, h = getpageyh pageno in
1189 let dy = (y - state.y) - (conf.winh
- h - conf.interpagespace) in
1191 | l
:: _
when l
.pageno = pageno ->
1192 if l
.pagevh != l
.pageh
1193 then gotoy (clamp (l
.pageh
- l
.pagevh + conf.interpagespace))
1194 else Glut.postRedisplay
()
1195 | _
:: rest
-> loop rest
1200 let optentry mode _ key
=
1201 let btos b = if b then "on" else "off" in
1202 let c = Char.unsafe_chr key
in
1206 try conf.scrollstep
<- int_of_string
s with exc
->
1207 state.text <- Printf.sprintf
"bad integer `%s': %s"
1208 s (Printexc.to_string exc
)
1210 TEswitch
("scroll step: ", "", None
, intentry, ondone)
1215 conf.autoscrollstep
<- int_of_string
s;
1216 if state.autoscroll
<> None
1217 then state.autoscroll
<- Some
conf.autoscrollstep
1219 state.text <- Printf.sprintf
"bad integer `%s': %s"
1220 s (Printexc.to_string exc
)
1222 TEswitch
("auto scroll step: ", "", None
, intentry, ondone)
1227 let zoom = float (int_of_string
s) /. 100.0 in
1230 state.text <- Printf.sprintf
"bad integer `%s': %s"
1231 s (Printexc.to_string exc
)
1233 TEswitch
("zoom: ", "", None
, intentry, ondone)
1238 conf.thumbw
<- max
2 (min
1920 (int_of_string
s));
1240 Printf.sprintf
"thumbnail width is set to %d" conf.thumbw
;
1241 begin match mode
with
1243 leavebirdseye beye
false;
1248 state.text <- Printf.sprintf
"bad integer `%s': %s"
1249 s (Printexc.to_string exc
)
1251 TEswitch
("thumbnail width: ", "", None
, intentry, ondone)
1256 Some
(int_of_string
s)
1258 state.text <- Printf.sprintf
"bad integer `%s': %s"
1259 s (Printexc.to_string exc
);
1262 | Some angle
-> reinit angle
conf.proportional
1265 TEswitch
("rotation: ", "", None
, intentry, ondone)
1268 conf.icase
<- not
conf.icase
;
1269 TEdone
("case insensitive search " ^
(btos conf.icase
))
1272 conf.preload <- not
conf.preload;
1274 TEdone
("preload " ^
(btos conf.preload))
1277 conf.verbose
<- not
conf.verbose
;
1278 TEdone
("verbose " ^
(btos conf.verbose
))
1281 conf.maxhfit
<- not
conf.maxhfit
;
1282 state.maxy <- state.maxy + (if conf.maxhfit
then -conf.winh
else conf.winh
);
1283 TEdone
("maxhfit " ^
(btos conf.maxhfit
))
1286 conf.crophack
<- not
conf.crophack
;
1287 TEdone
("crophack " ^
btos conf.crophack
)
1290 conf.showall
<- not
conf.showall
;
1291 TEdone
("throttle " ^
btos conf.showall
)
1294 conf.underinfo
<- not
conf.underinfo
;
1295 TEdone
("underinfo " ^
btos conf.underinfo
)
1298 conf.savebmarks
<- not
conf.savebmarks
;
1299 TEdone
("persistent bookmarks " ^
btos conf.savebmarks
)
1305 match state.layout with
1310 conf.interpagespace <- int_of_string
s;
1311 state.maxy <- calcheight ();
1312 let y = getpagey pageno in
1315 state.text <- Printf.sprintf
"bad integer `%s': %s"
1316 s (Printexc.to_string exc
)
1318 TEswitch
("vertical margin: ", "", None
, intentry, ondone)
1321 reinit conf.angle
(not
conf.proportional
);
1322 TEdone
("proprortional display " ^
btos conf.proportional
)
1325 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
1329 let maxoutlinerows () = (conf.winh
- 31) / 16;;
1331 let enterselector allowdel
outlines errmsg msg
=
1332 if Array.length
outlines = 0
1334 showtext ' ' errmsg
;
1338 Glut.setCursor
Glut.CURSOR_INHERIT
;
1340 match state.layout with
1342 | {pageno=pageno} :: _
-> pageno
1346 if n = Array.length
outlines
1349 let (_
, _
, (outlinepageno
, _
)) = outlines.(n) in
1350 if outlinepageno
>= pageno then n else loop (n+1)
1354 state.mode
<- Outline
1355 (allowdel
, active, max
0 (active - maxoutlinerows () / 2), outlines, "", 0,
1357 Glut.postRedisplay
();
1361 let enteroutlinemode () =
1363 match state.outlines with
1366 let a = Array.of_list
(List.rev l
) in
1367 state.outlines <- Oarray
a;
1369 | Onarrow
(pat
, a, _
) ->
1370 a, "Outline was narrowed to `" ^ pat ^
"' (Ctrl-u to restore)"
1372 enterselector false outlines "Document has no outline" msg
;
1375 let enterbookmarkmode () =
1376 let bookmarks = Array.of_list
state.bookmarks in
1377 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1380 let mode_to_string mode
=
1381 let b = Buffer.create
10 in
1382 let rec f = function
1383 | Textentry
(_
, _
) -> Buffer.add_string
b "Textentry ";
1384 | View
-> Buffer.add_string
b "View"
1385 | Birdseye _
-> Buffer.add_string
b "Birdseye"
1386 | Items _
-> Buffer.add_string
b "Items"
1387 | Outline _
-> Buffer.add_string
b "Outline"
1393 let color_of_string s =
1394 Scanf.sscanf
s "%d/%d/%d" (fun r g
b ->
1395 (float r /. 256.0, float g
/. 256.0, float b /. 256.0)
1399 let color_to_string (r, g
, b) =
1400 let r = truncate
(r *. 256.0)
1401 and g
= truncate
(g
*. 256.0)
1402 and b = truncate
(b *. 256.0) in
1403 Printf.sprintf
"%d/%d/%d" r g
b
1406 let enterinfomode () =
1407 let btos = function true -> "on" | _
-> "off" in
1408 let mode = state.mode in
1409 let rec makeitems () =
1410 let intp name get set
=
1411 Printf.sprintf
"%s\t%d" name
(get
()), 1, Action
(
1412 fun active first _ pan
->
1417 state.text <- Printf.sprintf
"bad integer `%s': %s"
1418 s (Printexc.to_string exn
);
1421 if n != max_int
then set
n;
1423 let te = name ^
": ", "", None
, intentry, ondone in
1428 state.mode <- Items
(active, first
, makeitems (), "", pan
, mode)
1431 and boolp name get set
=
1432 Printf.sprintf
"%s\t%s" name
(btos (get
())), 1, Action
(
1433 fun active first qsearch pan
->
1436 Items
(active, first
, makeitems (), qsearch
, pan
, mode);
1438 and colorp name get set
=
1439 Printf.sprintf
"%s\t%s" name
(color_to_string (get
())), 1, Action
(
1440 fun active first _ pan
->
1441 let invalid = (nan
, nan
, nan
) in
1444 try color_of_string s
1446 state.text <- Printf.sprintf
"bad color `%s': %s"
1447 s (Printexc.to_string exn
);
1453 let te = name ^
": ", "", None
, textentry, ondone in
1458 state.mode <- Items
(active, first
, makeitems (), "", pan
, mode)
1463 let birdseye = isbirdseye mode in
1465 (if birdseye then "Setup bird's eye" else "Setup"), 0, Noaction
;
1467 boolp
"presentation"
1468 (fun () -> conf.presentation)
1470 conf.presentation <- v;
1471 state.anchor <- getanchor ();
1474 boolp
"ignore case in searches"
1475 (fun () -> conf.icase
)
1476 (fun v -> conf.icase
<- v);
1479 (fun () -> conf.preload)
1480 (fun v -> conf.preload <- v);
1483 (fun () -> conf.verbose
)
1484 (fun v -> conf.verbose
<- v);
1487 (fun () -> conf.maxhfit
)
1488 (fun v -> conf.maxhfit
<- v);
1491 (fun () -> conf.crophack
)
1492 (fun v -> conf.crophack
<- v);
1495 (fun () -> conf.showall
)
1496 (fun v -> conf.showall
<- v);
1498 boolp
"highlight links"
1499 (fun () -> conf.hlinks
)
1500 (fun v -> conf.hlinks
<- v);
1503 (fun () -> conf.underinfo
)
1504 (fun v -> conf.underinfo
<- v);
1505 boolp
"persistent bookmarks"
1506 (fun () -> conf.savebmarks
)
1507 (fun v -> conf.savebmarks
<- v);
1509 boolp
"proportional display"
1510 (fun () -> conf.proportional
)
1511 (fun v -> reinit conf.angle
v);
1513 boolp
"persistent location"
1514 (fun () -> conf.jumpback
)
1515 (fun v -> conf.jumpback
<- v);
1519 intp "vertical margin"
1520 (fun () -> conf.interpagespace)
1522 conf.interpagespace <- n;
1524 match state.layout with
1529 state.maxy <- calcheight ();
1530 let y = getpagey pageno in
1535 (fun () -> conf.pagebias
)
1536 (fun v -> conf.pagebias
<- v);
1539 (fun () -> conf.scrollstep
)
1540 (fun n -> conf.scrollstep
<- n);
1542 intp "auto scroll step"
1544 match state.autoscroll
with
1546 | _
-> conf.autoscrollstep
)
1548 if state.autoscroll
<> None
1549 then state.autoscroll
<- Some
n;
1550 conf.autoscrollstep
<- n);
1553 (fun () -> truncate
(conf.zoom *. 100.))
1554 (fun v -> setzoom ((float v) /. 100.));
1557 (fun () -> conf.angle
)
1558 (fun v -> reinit v conf.proportional
);
1560 intp "scroll bar width"
1561 (fun () -> state.scrollw
)
1565 reshape conf.winw
conf.winh
;
1568 intp "scroll handle height"
1569 (fun () -> conf.scrollh
)
1570 (fun v -> conf.scrollh
<- v;);
1572 intp "thumbnail width"
1573 (fun () -> conf.thumbw
)
1575 conf.thumbw
<- min
1920 v;
1578 leavebirdseye beye
false;
1583 colorp
"background color"
1584 (fun () -> conf.bgcolor
)
1585 (fun v -> conf.bgcolor
<- v);
1588 "Presentation mode", 0, Noaction
;
1590 boolp
"scrollbar visible"
1591 (fun () -> conf.scrollbarinpm
)
1593 if v != conf.scrollbarinpm
1595 conf.scrollbarinpm
<- v;
1596 if conf.presentation
1598 state.scrollw
<- if v then conf.scrollbw
else 0;
1599 reshape conf.winw
conf.winh
;
1605 "Pixmap Cache", 0, Noaction
;
1607 intp "size (advisory)"
1608 (fun () -> conf.memlimit
)
1609 (fun v -> conf.memlimit
<- v);
1610 Printf.sprintf
"%s\t%d" "used" state.memused
, 1, Noaction
;
1617 :: ("Document", 0, Noaction
)
1618 :: List.map
(fun (_
, s) -> (s, 1, Noaction
)) state.docinfo
1624 :: (Printf.sprintf
"Save these parameters as defaults at exit (%s)"
1625 (btos conf.bedefault
),
1628 fun active first qsearch pan
->
1629 conf.bedefault
<- not
conf.bedefault
;
1630 Items
(active, first
, makeitems (), qsearch
, pan
, mode);
1635 Array.of_list
(items @ tailer)
1638 state.mode <- Items
(1, 0, makeitems (), "", 0, mode);
1639 Glut.postRedisplay
();
1642 let enterhelpmode () =
1643 state.mode <- Items
(-1, 0, state.help
, "", 0, state.mode);
1644 Glut.postRedisplay
();
1647 let quickbookmark ?title
() =
1648 match state.layout with
1654 let sec = Unix.gettimeofday
() in
1655 let tm = Unix.localtime
sec in
1656 Printf.sprintf
"Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1660 (tm.Unix.tm_year
+ 1900)
1663 | Some
title -> title
1666 (title, 0, (l
.pageno, float l
.pagey /. float l
.pageh
))
1671 state.fullscreen
<- None
;
1672 Glut.reshapeWindow
w h;
1675 let writeopen path password
=
1676 writecmd state.csock
("open " ^ path ^
"\000" ^ password ^
"\000");
1679 let opendoc path password
=
1682 state.password
<- password
;
1683 state.gen
<- state.gen
+ 1;
1684 state.docinfo
<- [];
1686 writeopen path password
;
1687 Glut.setWindowTitle
("llpp " ^
Filename.basename path
);
1688 wcmd "geometry" [`i
state.w; `i
conf.winh
];
1691 let viewkeyboard key
=
1693 let mode = state.mode in
1694 state.mode <- Textentry
(te, fun _
-> state.mode <- mode);
1697 Glut.postRedisplay
()
1699 let c = Char.chr key
in
1701 | '
\027'
| 'q'
-> (* escape *)
1704 | '
\008'
-> (* backspace *)
1705 let y = getnav ~
-1 in
1706 gotoy_and_clear_text y
1714 Glut.postRedisplay
()
1717 let ondone isforw
s =
1718 cbput state.hists
.pat
s;
1719 state.searchpattern
<- s;
1722 let s = String.create
1 in
1724 enttext (s, "", Some
(onhist state.hists
.pat
),
1725 textentry, ondone (c ='
/'
))
1727 | '
+'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1728 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1729 setzoom (conf.zoom +. incr)
1734 try int_of_string
s with exc
->
1735 state.text <- Printf.sprintf
"bad integer `%s': %s"
1736 s (Printexc.to_string exc
);
1742 state.text <- "page bias is now " ^ string_of_int
n;
1745 enttext ("page bias: ", "", None
, intentry, ondone)
1747 | '
-'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1748 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1749 setzoom (max
0.01 (conf.zoom -. decr))
1752 let ondone msg
= state.text <- msg
in
1754 "option [acfhilpstvAPRSZ]: ", "", None
,
1755 optentry state.mode, ondone
1758 | '
0'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1761 | '
1'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1762 let zoom = zoomforh
conf.winw
conf.winh
state.scrollw
in
1766 | '
9'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1772 try int_of_string
s with exc
->
1773 state.text <- Printf.sprintf
"bad integer `%s': %s"
1774 s (Printexc.to_string exc
);
1780 cbput state.hists
.pag
(string_of_int
n);
1781 gotoy_and_clear_text (getpagey (n + conf.pagebias
- 1))
1784 let pageentry text key
=
1785 match Char.unsafe_chr key
with
1786 | 'g'
-> TEdone
text
1787 | _
-> intentry text key
1789 let text = "x" in text.[0] <- c;
1790 enttext (":", text, Some
(onhist state.hists
.pag
), pageentry, ondone)
1793 state.scrollw
<- if state.scrollw
> 0 then 0 else conf.scrollbw
;
1794 reshape conf.winw
conf.winh
;
1797 conf.hlinks
<- not
conf.hlinks
;
1798 state.text <- "highlightlinks " ^
if conf.hlinks
then "on" else "off";
1799 Glut.postRedisplay
()
1802 begin match state.autoscroll
with
1804 conf.autoscrollstep
<- step
;
1805 state.autoscroll
<- None
1807 if conf.autoscrollstep
= 0
1808 then state.autoscroll
<- Some
1
1809 else state.autoscroll
<- Some
conf.autoscrollstep
1813 conf.presentation <- not
conf.presentation;
1814 if conf.presentation
1816 if not
conf.scrollbarinpm
1817 then state.scrollw
<- 0;
1820 state.scrollw
<- conf.scrollbw
;
1822 showtext ' '
("presentation mode " ^
1823 if conf.presentation then "on" else "off");
1824 state.anchor <- getanchor ();
1828 begin match state.fullscreen
with
1830 state.fullscreen
<- Some
(conf.winw
, conf.winh
);
1833 state.fullscreen
<- None
;
1838 gotoy_and_clear_text 0
1841 gotopage1 (state.pagecount
- 1) 0
1844 search state.searchpattern
true
1847 search state.searchpattern
false
1850 begin match state.layout with
1853 gotoy_and_clear_text (getpagey l
.pageno)
1857 begin match List.rev
state.layout with
1860 let pageno = min
(l
.pageno+1) (state.pagecount
-1) in
1861 gotoy_and_clear_text (getpagey pageno)
1864 | '
\127'
-> (* del *)
1865 begin match state.layout with
1868 let pageno = max
0 (l
.pageno-1) in
1869 gotoy_and_clear_text (getpagey pageno)
1874 if fn
= -1 then l
.pageno, l
.pageno else fn
, l
.pageno
1876 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
1878 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
1882 else (100. *. (float state.y /. float maxy)) in
1885 Printf.sprintf
"Page %d of %d %.2f%%"
1886 (fn+1) state.pagecount
percent
1889 "Pages %d-%d of %d %.2f%%"
1890 (fn+1) (ln
+1) state.pagecount
percent
1895 begin match state.layout with
1898 doreshape (l
.pagew
+ state.scrollw
) l
.pageh
;
1899 Glut.postRedisplay
();
1903 enterbookmarkmode ()
1913 match state.layout with
1916 (s, 0, (l
.pageno, float l
.pagey /. float l
.pageh
))
1920 enttext ("bookmark: ", "", None
, textentry, ondone)
1924 showtext ' '
"Quick bookmark added";
1927 begin match state.layout with
1929 let rect = getpdimrect l
.pagedimno
in
1933 (truncate
(1.8 *. (rect.(1) -. rect.(0))),
1934 truncate
(1.2 *. (rect.(3) -. rect.(0))))
1936 (truncate
(rect.(1) -. rect.(0)),
1937 truncate
(rect.(3) -. rect.(0)))
1941 doreshape (w + state.scrollw
) (h + conf.interpagespace)
1943 Glut.postRedisplay
();
1948 | '
\000'
-> (* ctrl-2 *)
1949 let maxw = getmaxw
() in
1951 then setzoom (maxw /. float conf.winw
)
1954 reinit (conf.angle
+ (if c = '
>'
then 30 else -30)) conf.proportional
1959 (min
(state.colorscale
+. (if c = '
]'
then 0.1 else -0.1)) 1.0);
1960 Glut.postRedisplay
()
1963 begin match state.mode with
1964 | Birdseye beye
-> upbirdseye beye
1965 | _
-> gotoy (clamp (-conf.scrollstep
))
1969 begin match state.mode with
1970 | Birdseye beye
-> downbirdseye beye
1971 | _
-> gotoy (clamp conf.scrollstep
)
1975 state.anchor <- getanchor ();
1976 opendoc state.path
state.password
1979 vlog "huh? %d %c" key
(Char.chr key
);
1982 let textentrykeyboard key
((c, text, opthist
, onkey
, ondone), onleave
) =
1984 state.mode <- Textentry
(te, onleave
);
1987 Glut.postRedisplay
()
1989 match Char.unsafe_chr key
with
1990 | '
\008'
-> (* backspace *)
1991 let len = String.length
text in
1995 Glut.postRedisplay
();
1998 let s = String.sub
text 0 (len - 1) in
1999 enttext (c, s, opthist
, onkey
, ondone)
2005 Glut.postRedisplay
()
2007 | '
\007'
(* ctrl-g *)
2008 | '
\027'
-> (* escape *)
2009 begin match opthist
with
2011 | Some
(_
, onhistcancel
) -> onhistcancel
()
2014 Glut.postRedisplay
()
2017 begin match onkey
text key
with
2021 Glut.postRedisplay
()
2024 enttext (c, text, opthist
, onkey
, ondone);
2028 Glut.postRedisplay
()
2031 state.mode <- Textentry
(te, onleave
);
2032 Glut.postRedisplay
()
2036 let birdseyekeyboard key
((_
, _
, pageno, _
, _
) as beye
) =
2038 | 27 -> (* escape *)
2039 leavebirdseye beye
true
2041 | 12 -> (* ctrl-l *)
2042 let y, h = getpageyh pageno in
2043 let top = (conf.winh
- h) / 2 in
2044 gotoy (max
0 (y - top))
2047 leavebirdseye beye
false
2053 let itemskeyboard key
(active, first
, items, qsearch
, pan
, oldmode
) =
2054 let set active first qsearch
=
2055 state.mode <- Items
(active, first
, items, qsearch
, pan
, oldmode
)
2057 let search active pattern
incr =
2060 if n >= Array.length
items || n < 0
2063 let (s, _
, _
) = items.(n) in
2065 (try ignore
(Str.search_forward re
s 0); true
2066 with Not_found
-> false)
2068 else loop (n + incr)
2073 let re = Str.regexp_case_fold pattern
in
2079 let firstof active = max
0 (active - maxoutlinerows () / 2) in
2081 | 18 | 19 -> (* ctrl-r/ctlr-s *)
2082 let incr = if key
= 18 then -1 else 1 in
2084 match search (active + incr) qsearch
incr with
2086 state.text <- qsearch ^
" [not found]";
2089 state.text <- qsearch
;
2090 active, firstof active
2092 set active first qsearch
;
2093 Glut.postRedisplay
();
2095 | 8 -> (* backspace *)
2096 let len = String.length qsearch
in
2103 set active first
"";
2106 let qsearch = String.sub
qsearch 0 (len - 1) in
2108 match search active qsearch ~
-1 with
2110 state.text <- qsearch ^
" [not found]";
2113 state.text <- qsearch;
2114 active, firstof active
2116 set active first
qsearch
2118 Glut.postRedisplay
()
2120 | _
when key
>= 32 && key
< 127 ->
2121 let pattern = addchar qsearch (Char.chr key
) in
2123 match search active pattern 1 with
2125 state.text <- pattern ^
" [not found]";
2128 state.text <- pattern;
2129 active, firstof active
2131 set active first
pattern;
2132 Glut.postRedisplay
()
2134 | 27 -> (* escape *)
2136 if String.length
qsearch = 0
2138 state.mode <- oldmode
;
2141 set active first
"";
2143 Glut.postRedisplay
()
2146 if active >= 0 && active < Array.length
items
2148 match items.(active) with
2150 state.mode <- f active first
qsearch pan
2154 state.mode <- oldmode
2158 state.mode <- oldmode
2160 Glut.postRedisplay
();
2162 | _
-> dolog "unknown key %d" key
2165 let outlinekeyboard key
2166 (allowdel
, active, first
, outlines, qsearch, pan
, oldmode
) =
2167 let narrow outlines pattern =
2168 let reopt = try Some
(Str.regexp_case_fold
pattern) with _
-> None
in
2172 let rec fold accu n =
2176 let (s, _
, _
) as o
= outlines.(n) in
2178 if (try ignore
(Str.search_forward
re s 0); true
2179 with Not_found
-> false)
2185 let matched = fold [] (Array.length
outlines - 1) in
2186 if matched = [] then None
else Some
(Array.of_list
matched)
2188 let search active pattern incr =
2191 if n = Array.length
outlines || n = -1
2194 let (s, _
, _
) = outlines.(n) in
2196 (try ignore
(Str.search_forward
re s 0); true
2197 with Not_found
-> false)
2199 else loop (n + incr)
2204 let re = Str.regexp_case_fold
pattern in
2210 let firstof active = max
0 (active - maxoutlinerows () / 2) in
2212 | 27 -> (* escape *)
2214 if String.length
qsearch = 0
2216 state.mode <- oldmode
;
2219 state.mode <- Outline
(
2220 allowdel
, active, first
, outlines, "", pan
, oldmode
2223 Glut.postRedisplay
();
2225 | 18 | 19 -> (* ctrl-r/ctrl-s *)
2226 let incr = if key
= 18 then -1 else 1 in
2228 match search (active + incr) qsearch incr with
2230 state.text <- qsearch ^
" [not found]";
2233 state.text <- qsearch;
2234 active, firstof active
2236 state.mode <- Outline
(
2237 allowdel
, active, first
, outlines, qsearch, pan
, oldmode
2239 Glut.postRedisplay
();
2241 | 8 -> (* backspace *)
2242 let len = String.length
qsearch in
2249 state.mode <- Outline
(
2250 allowdel
, active, first
, outlines, "", pan
, oldmode
2254 let qsearch = String.sub
qsearch 0 (len - 1) in
2256 match search active qsearch ~
-1 with
2258 state.text <- qsearch ^
" [not found]";
2261 state.text <- qsearch;
2262 active, firstof active
2264 state.mode <- Outline
(
2265 allowdel
, active, first
, outlines, qsearch, pan
, oldmode
2268 Glut.postRedisplay
()
2271 if active < Array.length
outlines
2273 let (_
, _
, anchor) = outlines.(active) in
2278 if allowdel
then state.bookmarks <- Array.to_list
outlines;
2279 state.mode <- oldmode
;
2280 Glut.postRedisplay
();
2282 | _
when key
>= 32 && key
< 127 ->
2283 let pattern = addchar qsearch (Char.chr key
) in
2285 match search active pattern 1 with
2287 state.text <- pattern ^
" [not found]";
2290 state.text <- pattern;
2291 active, firstof active
2293 state.mode <- Outline
(
2294 allowdel
, active, first
, outlines, pattern, pan
, oldmode
2296 Glut.postRedisplay
()
2298 | 14 when not allowdel
-> (* ctrl-n *)
2299 if String.length
qsearch > 0
2301 let optoutlines = narrow outlines qsearch in
2302 begin match optoutlines with
2303 | None
-> state.text <- "can't narrow"
2305 state.mode <- Outline
(
2306 allowdel
, 0, 0, outlines, qsearch, pan
, oldmode
2308 match state.outlines with
2311 state.outlines <- Onarrow
(qsearch, outlines, a)
2312 | Onarrow
(_
, _
, b) ->
2313 state.outlines <- Onarrow
(qsearch, outlines, b)
2316 Glut.postRedisplay
()
2318 | 21 when not allowdel
-> (* ctrl-u *)
2320 match state.outlines with
2323 let a = Array.of_list
(List.rev l
) in
2324 state.outlines <- Oarray
a;
2326 | Onarrow
(_
, _
, b) ->
2327 state.outlines <- Oarray
b;
2331 state.mode <- Outline
(allowdel
, 0, 0, outline, qsearch, pan
, oldmode
);
2332 Glut.postRedisplay
()
2334 | 12 -> (* ctrl-l *)
2335 state.mode <- Outline
2336 (allowdel
, active, firstof active, outlines, qsearch, pan
, oldmode
);
2337 Glut.postRedisplay
()
2339 | 127 when allowdel
-> (* delete *)
2340 let len = Array.length
outlines - 1 in
2344 state.bookmarks <- [];
2347 let bookmarks = Array.init
len
2349 let i = if i >= active then i + 1 else i in
2363 Glut.postRedisplay
()
2365 | _
-> dolog "unknown key %d" key
2368 let keyboard ~key ~x ~
y =
2371 if key
= 7 && not
(istextentry state.mode) (* ctrl-g *)
2375 match state.mode with
2376 | Outline
outline -> outlinekeyboard key
outline
2377 | Textentry
textentry -> textentrykeyboard key
textentry
2378 | Birdseye
birdseye -> birdseyekeyboard key
birdseye
2379 | View
-> viewkeyboard key
2380 | Items
items -> itemskeyboard key
items
2383 let birdseyespecial key
((conf, leftx
, _
, hooverpageno
, anchor) as beye
) =
2385 | Glut.KEY_UP
-> upbirdseye beye
2386 | Glut.KEY_DOWN
-> downbirdseye beye
2388 | Glut.KEY_PAGE_UP
->
2389 begin match state.layout with
2393 state.mode <- Birdseye
(
2394 conf, leftx
, l
.pageno, hooverpageno
, anchor
2396 gotopage1 l
.pageno 0;
2399 let layout = layout (state.y-conf.winh
) conf.winh
in
2401 | [] -> gotoy (clamp (-conf.winh
))
2403 state.mode <- Birdseye
(
2404 conf, leftx
, l
.pageno, hooverpageno
, anchor
2406 gotopage1 l
.pageno 0
2409 | [] -> gotoy (clamp (-conf.winh
))
2412 | Glut.KEY_PAGE_DOWN
->
2413 begin match List.rev
state.layout with
2415 let layout = layout (state.y + conf.winh
) conf.winh
in
2416 begin match layout with
2418 let incr = l
.pageh
- l
.pagevh in
2423 conf, leftx
, state.pagecount
- 1, hooverpageno
, anchor
2425 Glut.postRedisplay
();
2427 else gotoy (clamp (incr + conf.interpagespace*2));
2431 Birdseye
(conf, leftx
, l
.pageno, hooverpageno
, anchor);
2432 gotopage1 l
.pageno 0;
2435 | [] -> gotoy (clamp conf.winh
)
2439 state.mode <- Birdseye
(conf, leftx
, 0, hooverpageno
, anchor);
2443 let pageno = state.pagecount
- 1 in
2444 state.mode <- Birdseye
(conf, leftx
, pageno, hooverpageno
, anchor);
2445 if not
(pagevisible state.layout pageno)
2448 match List.rev
state.pdims
with
2450 | (_
, _
, h, _
) :: _
-> h
2452 gotoy (max
0 (getpagey pageno - (conf.winh
- h - conf.interpagespace)))
2453 else Glut.postRedisplay
();
2457 let setautoscrollspeed step goingdown
=
2458 let incr = max
1 ((abs step
) / 2) in
2459 let incr = if goingdown
then incr else -incr in
2460 let astep = step
+ incr in
2461 state.autoscroll
<- Some
astep;
2464 let special ~key ~x ~
y =
2467 match state.mode with
2468 | View
| (Birdseye _
) when key
= Glut.KEY_F9
->
2472 birdseyespecial key vals
2474 | View
when key
= Glut.KEY_F1
->
2478 begin match state.autoscroll
with
2479 | Some step
when key
= Glut.KEY_DOWN
|| key
= Glut.KEY_UP
->
2480 setautoscrollspeed step
(key
= Glut.KEY_DOWN
)
2485 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
2486 | Glut.KEY_UP
-> clamp (-conf.scrollstep
)
2487 | Glut.KEY_DOWN
-> clamp conf.scrollstep
2488 | Glut.KEY_PAGE_UP
->
2489 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2491 match state.layout with
2493 | l
:: _
-> state.y - l
.pagey
2496 | Glut.KEY_PAGE_DOWN
->
2497 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2499 match List.rev
state.layout with
2501 | l
:: _
-> getpagey l
.pageno
2504 | Glut.KEY_HOME
-> addnav (); 0
2507 state.maxy - (if conf.maxhfit
then conf.winh
else 0)
2509 | (Glut.KEY_RIGHT
| Glut.KEY_LEFT
) when
2510 Glut.getModifiers
() land Glut.active_alt
!= 0 ->
2511 getnav (if key
= Glut.KEY_LEFT
then 1 else -1)
2513 | Glut.KEY_RIGHT
when conf.zoom > 1.0 ->
2514 state.x
<- state.x
- 10;
2516 | Glut.KEY_LEFT
when conf.zoom > 1.0 ->
2517 state.x
<- state.x
+ 10;
2522 gotoy_and_clear_text y
2526 ((c, _
, (Some
(action, _
) as onhist), onkey
, ondone), mode) ->
2529 | Glut.KEY_UP
-> action HCprev
2530 | Glut.KEY_DOWN
-> action HCnext
2531 | Glut.KEY_HOME
-> action HCfirst
2532 | Glut.KEY_END
-> action HClast
2535 state.mode <- Textentry
((c, s, onhist, onkey
, ondone), mode);
2536 Glut.postRedisplay
()
2540 | Items
(active, first
, items, qsearch, pan
, oldmode
) ->
2541 let maxrows = maxoutlinerows () in
2542 let itemcount = Array.length
items in
2543 let hasaction = function
2544 | (_
, _
, Noaction
) -> false
2547 let find start
incr =
2549 if i = -1 || i = itemcount
2552 if hasaction items.(i)
2554 else find (i + incr)
2559 let set active first
=
2560 let first = max
0 (min
first (itemcount - maxrows)) in
2561 state.mode <- Items
(active, first, items, qsearch, pan
, oldmode
)
2564 let isvisible first n = n >= first && n - first <= maxrows in
2566 let incr1 = if incr > 0 then 1 else -1 in
2567 if isvisible first active
2570 let next = active + incr in
2572 if next < 0 || next >= itemcount
2574 else find next incr1
2576 if next = -1 || abs
(active - next) > maxrows
2582 let first = first + incr in
2583 let first = max
0 (min
first (itemcount - 1)) in
2585 let next = active + incr in
2586 let next = max
0 (min
next (itemcount - 1)) in
2589 let active = if next = -1 then active else next in
2592 let first = min
next first in
2595 let first = first + incr in
2596 let first = max
0 (min
first (itemcount - 1)) in
2598 let next = active + incr in
2599 let next = max
0 (min
next (itemcount - 1)) in
2600 let next = find next incr1 in
2601 if next = -1 || abs
(active - first) > maxrows
2608 Glut.postRedisplay
()
2610 begin match key
with
2611 | Glut.KEY_UP
-> navigate ~
-1
2612 | Glut.KEY_DOWN
-> navigate 1
2613 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
2614 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
2617 state.mode <- Items
(
2618 active, first, items, qsearch, min
0 (pan
- 1), oldmode
2620 Glut.postRedisplay
()
2623 state.mode <- Items
(
2624 active, first, items, qsearch, min
0 (pan
+ 1), oldmode
2626 Glut.postRedisplay
()
2629 let active = find 0 1 in
2631 Glut.postRedisplay
()
2634 let first = max
0 (itemcount - maxrows) in
2635 let active = find (itemcount - 1) ~
-1 in
2637 Glut.postRedisplay
()
2642 | Outline
(allowdel
, active, first, outlines, qsearch, pan
, oldmode
) ->
2643 let maxrows = maxoutlinerows () in
2644 let calcfirst first active =
2647 let rows = active - first in
2648 if rows > maxrows then active - maxrows else first
2652 let active = active + incr in
2653 let active = max
0 (min
active (Array.length
outlines - 1)) in
2654 let first = calcfirst first active in
2655 state.mode <- Outline
(
2656 allowdel
, active, first, outlines, qsearch, pan
, oldmode
2658 Glut.postRedisplay
()
2660 let updownlevel incr =
2661 let len = Array.length
outlines in
2662 let (_
, curlevel
, _
) = outlines.(active) in
2664 if i = len then i-1 else if i = -1 then 0 else
2665 let (_
, l
, _
) = outlines.(i) in
2666 if l
!= curlevel
then i else flow (i+incr)
2668 let active = flow active in
2669 let first = calcfirst first active in
2670 state.mode <- Outline
(
2671 allowdel
, active, first, outlines, qsearch, pan
, oldmode
2673 Glut.postRedisplay
()
2676 | Glut.KEY_UP
-> navigate ~
-1
2677 | Glut.KEY_DOWN
-> navigate 1
2678 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
2679 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
2682 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2684 state.mode <- Outline
(
2685 allowdel
, active, first, outlines,
2686 qsearch, min
0 (pan
+ 1), oldmode
2688 Glut.postRedisplay
();
2696 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2698 state.mode <- Outline
(
2699 allowdel
, active, first, outlines, qsearch, pan
- 1, oldmode
2701 Glut.postRedisplay
();
2705 then updownlevel ~
-1
2709 state.mode <- Outline
(
2710 allowdel
, 0, 0, outlines, qsearch, pan
, oldmode
2712 Glut.postRedisplay
()
2715 let active = Array.length
outlines - 1 in
2716 let first = max
0 (active - maxrows) in
2717 state.mode <- Outline
(
2718 allowdel
, active, first, outlines, qsearch, pan
, oldmode
2720 Glut.postRedisplay
()
2725 let drawplaceholder l
=
2726 let margin = state.x
+ (conf.winw
- (state.w + state.scrollw
)) / 2 in
2728 (float l
.pagex
, float l
.pagedispy
)
2729 (float (l
.pagew
+ l
.pagex
), float (l
.pagedispy
+ l
.pagevh))
2731 let x = if margin < 0 then -margin else l
.pagex
2732 and y = l
.pagedispy
+ 13 in
2733 GlDraw.color
(0.0, 0.0, 0.0);
2734 drawstring 13 x y ("Loading " ^ string_of_int
(l
.pageno + 1))
2737 let now () = Unix.gettimeofday
();;
2741 match state.mode with
2742 | Textentry _
-> scalecolor 0.4
2743 | View
| Outline _
| Items _
-> scalecolor 1.0
2744 | Birdseye
(_
, _
, pageno, hooverpageno
, _
) ->
2745 if l
.pageno = hooverpageno
2748 if l
.pageno = pageno
2754 begin match getopaque l
.pageno with
2755 | Some
(opaque, _
) when validopaque opaque ->
2757 draw
(l
.pagedispy
, l
.pagevh, l
.pagey, conf.hlinks
) opaque;
2760 vlog "draw %d %f sec" l
.pageno d;
2768 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
2769 let sh = (float (maxy + conf.winh
) /. float conf.winh
) in
2770 let sh = float conf.winh
/. sh in
2771 let sh = max
sh (float conf.scrollh
) in
2774 if state.y = state.maxy
2776 else float y /. float maxy
2778 let position = (float conf.winh
-. sh) *. percent in
2781 if position +. sh > float conf.winh
2782 then float conf.winh
-. sh
2788 let scrollindicator () =
2789 GlDraw.color (0.64 , 0.64, 0.64);
2791 (float (conf.winw
- state.scrollw
), 0.)
2792 (float conf.winw
, float conf.winh
)
2794 GlDraw.color (0.0, 0.0, 0.0);
2796 let position, sh = scrollph state.y in
2798 (float (conf.winw
- state.scrollw
), position)
2799 (float conf.winw
, position +. sh)
2803 let showsel margin =
2804 match state.mstate
with
2805 | Mnone
| Mscroll _
| Mpan _
| Mzoom _
->
2808 | Msel
((x0
, y0
), (x1
, y1
)) ->
2809 let rec loop = function
2811 if (y0
>= l
.pagedispy
&& y0
<= (l
.pagedispy
+ l
.pagevh))
2812 || ((y1
>= l
.pagedispy
&& y1
<= (l
.pagedispy
+ l
.pagevh)))
2814 match getopaque l
.pageno with
2815 | Some
(opaque, _
) when validopaque opaque ->
2816 let oy = -l
.pagey + l
.pagedispy
in
2818 (x0
- margin - state.x, y0
,
2819 x1
- margin - state.x, y1
) oy;
2829 let panx = float state.x in
2831 GlDraw.color (0.0, 0.0, 1.0) ~alpha
:0.5;
2832 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2834 (fun (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) ->
2836 if l
.pageno = pageno
2838 let d = float (l
.pagedispy
- l
.pagey) in
2839 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
2840 GlDraw.begins `quads
;
2842 GlDraw.vertex2
(x0
+.panx, y0
+.d);
2843 GlDraw.vertex2
(x1
+.panx, y1
+.d);
2844 GlDraw.vertex2
(x2
+.panx, y2
+.d);
2845 GlDraw.vertex2
(x3
+.panx, y3
+.d);
2855 let showstrings trusted
active first pan
strings =
2857 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2858 GlDraw.color (0., 0., 0.) ~alpha
:0.85;
2859 GlDraw.rect (0., 0.) (float conf.winw
, float conf.winh
);
2860 GlDraw.color (1., 1., 1.);
2861 Gl.enable `texture_2d
;
2863 let wx = measurestr
15 "w" in
2864 let tabx = 30.0*.wx +. float (pan
*15) in
2866 if row
= Array.length
strings || (row
- first) * 16 > conf.winh
2869 let (s, level
, _
) = strings.(row
) in
2870 let y = (row
- first) * 16 in
2871 let x = 5 + 15*(max
0 (level
+pan
)) in
2874 Gl.disable `texture_2d
;
2875 GlDraw.polygon_mode `both `line
;
2876 GlDraw.color (1., 1., 1.) ~alpha
:0.9;
2877 GlDraw.rect (1., float (y + 1))
2878 (float (conf.winw
- 1), float (y + 18));
2879 GlDraw.polygon_mode `both `fill
;
2880 GlDraw.color (1., 1., 1.);
2881 Gl.enable `texture_2d
;
2884 let drawtabularstring x s =
2888 let tabpos = try String.index
s '
\t'
with Not_found
-> -1 in
2891 let len = String.length
s - tabpos - 1 in
2892 let s1 = String.sub
s 0 tabpos
2893 and s2
= String.sub
s (tabpos + 1) len in
2894 let xx = wx +. drawstring1 14 x (y + 16) s1 in
2895 let x = truncate
(max
xx tabx) in
2896 drawstring1 15 x (y + 16) s2
2898 drawstring1 15 x (y + 16) s
2900 drawstring1 15 x (y + 16) s
2904 drawtabularstring (x + pan
*15) s;
2910 Gl.disable `texture_2d
;
2913 let showoutline (_, active, first, outlines, _, pan
, _) =
2914 showstrings false active first pan
outlines;
2917 let showitems (active, first, items, _, pan
, _) =
2918 showstrings true active first pan
items;
2922 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
2923 GlDraw.viewport
margin 0 state.w conf.winh
;
2925 GlClear.color (scalecolor2 conf.bgcolor
);
2926 GlClear.clear
[`
color];
2929 Gl.enable `scissor_test
;
2930 GlMisc.scissor
0 0 (conf.winw
- state.scrollw
) conf.winh
;
2932 List.iter
drawpage state.layout;
2935 Gl.disable `scissor_test
2939 let x = -.float state.x in
2940 GlMat.translate ~
x ();
2944 GlDraw.viewport
0 0 conf.winw
conf.winh
;
2947 begin match state.mode with
2948 | Items
items -> showitems items
2949 | Outline
outline -> showoutline outline
2953 Glut.swapBuffers
();
2957 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
2958 let x = x - margin - state.x in
2959 let rec f = function
2961 begin match getopaque l
.pageno with
2962 | Some
(opaque, _) when validopaque opaque ->
2963 let y = y - l
.pagedispy
in
2966 let y = l
.pagey + y in
2967 let x = x - l
.pagex
in
2968 match whatsunder
opaque x y with
2981 let viewmouse button bstate
x y =
2983 | Glut.OTHER_BUTTON
n when (n == 3 || n == 4) && bstate
= Glut.UP
->
2984 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2986 match state.mstate
with
2987 | Mzoom
(oldn
, i) ->
2995 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
2997 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
2999 let zoom = conf.zoom +. incr in
3001 state.mstate
<- Mzoom
(n, 0);
3003 state.mstate
<- Mzoom
(n, i+1);
3005 else state.mstate
<- Mzoom
(n, 0)
3007 | _ -> state.mstate
<- Mzoom
(n, 0)
3010 match state.autoscroll
with
3011 | Some step
-> setautoscrollspeed step
(n=4)
3015 then -conf.scrollstep
3016 else conf.scrollstep
3018 let incr = incr * 2 in
3019 let y = clamp incr in
3020 gotoy_and_clear_text y
3023 | Glut.LEFT_BUTTON
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
3024 if bstate
= Glut.DOWN
3026 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
3027 state.mstate
<- Mpan
(x, y)
3030 state.mstate
<- Mnone
3032 | Glut.LEFT_BUTTON
when x > conf.winw
- state.scrollw
->
3033 if bstate
= Glut.DOWN
3035 let position, sh = scrollph state.y in
3036 if y > truncate
position && y < truncate
(position +. sh)
3038 state.mstate
<- Mscroll
3040 let percent = float y /. float conf.winh
in
3041 let desty = truncate
(float (state.maxy - conf.winh
) *. percent) in
3043 state.mstate
<- Mscroll
3045 state.mstate
<- Mnone
3047 | Glut.LEFT_BUTTON
->
3048 let dest = if bstate
= Glut.DOWN
then getunder x y else Unone
in
3049 begin match dest with
3050 | Ulinkgoto
(pageno, top) ->
3054 gotopage1 pageno top;
3060 | Unone
when bstate
= Glut.DOWN
->
3061 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
3062 state.mstate
<- Mpan
(x, y);
3064 | Unone
| Utext
_ ->
3065 if bstate
= Glut.DOWN
3067 if conf.angle
mod 360 = 0
3069 state.mstate
<- Msel
((x, y), (x, y));
3070 Glut.postRedisplay
()
3074 match state.mstate
with
3077 | Mzoom
_ | Mscroll
->
3078 state.mstate
<- Mnone
3081 Glut.setCursor
Glut.CURSOR_INHERIT
;
3082 state.mstate
<- Mnone
3084 | Msel
((_, y0
), (_, y1
)) ->
3086 if (y0
>= l
.pagedispy
&& y0
<= (l
.pagedispy
+ l
.pagevh))
3087 || ((y1
>= l
.pagedispy
&& y1
<= (l
.pagedispy
+ l
.pagevh)))
3089 match getopaque l
.pageno with
3090 | Some
(opaque, _) when validopaque opaque ->
3094 List.iter
f state.layout;
3095 copysel
""; (* ugly *)
3096 Glut.setCursor
Glut.CURSOR_INHERIT
;
3097 state.mstate
<- Mnone
;
3104 let birdseyemouse button bstate
x y
3105 (conf, leftx
, _, hooverpageno
, anchor) =
3107 | Glut.LEFT_BUTTON
when bstate
= Glut.UP
->
3108 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
3109 let rec loop = function
3112 if y > l
.pagedispy
&& y < l
.pagedispy
+ l
.pagevh
3113 && x > margin && x < margin + l
.pagew
3115 leavebirdseye (conf, leftx
, l
.pageno, hooverpageno
, anchor) false;
3120 | Glut.OTHER_BUTTON
_ -> viewmouse button bstate
x y
3124 let mouse bstate button
x y =
3125 match state.mode with
3126 | View
-> viewmouse button bstate
x y
3127 | Birdseye beye
-> birdseyemouse button bstate
x y beye
3128 | Textentry
_ | Outline
_ | Items
_ -> ()
3131 let mouse ~button ~
state ~
x ~
y = mouse state button
x y;;
3134 match state.mode with
3137 match state.mstate
with
3138 | Mzoom
_ | Mnone
-> ()
3143 state.mstate
<- Mpan
(x, y);
3144 if conf.zoom > 1.0 then state.x <- state.x + dx;
3146 gotoy_and_clear_text y
3149 state.mstate
<- Msel
(a, (x, y));
3150 Glut.postRedisplay
()
3153 let y = min
conf.winh
(max
0 y) in
3154 let percent = float y /. float conf.winh
in
3155 let y = truncate
(float (state.maxy - conf.winh
) *. percent) in
3156 gotoy_and_clear_text y
3160 match state.mode with
3161 | Birdseye
(conf, leftx
, pageno, hooverpageno
, anchor) ->
3162 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
3163 let rec loop = function
3165 if hooverpageno
!= -1
3167 state.mode <- Birdseye
(conf, leftx
, pageno, -1, anchor);
3168 Glut.postRedisplay
();
3171 if y > l
.pagedispy
&& y < l
.pagedispy
+ l
.pagevh
3172 && x > margin && x < margin + l
.pagew
3174 state.mode <- Birdseye
(conf, leftx
, pageno, l
.pageno, anchor);
3175 Glut.postRedisplay
();
3181 | Outline
_ | Items
_ | Textentry
_ -> ()
3183 match state.mstate
with
3185 begin match getunder x y with
3186 | Unone
-> Glut.setCursor
Glut.CURSOR_INHERIT
3188 if conf.underinfo
then showtext 'u'
("ri: " ^ uri
);
3189 Glut.setCursor
Glut.CURSOR_INFO
3190 | Ulinkgoto
(page
, _) ->
3192 then showtext 'p'
("age: " ^ string_of_int page
);
3193 Glut.setCursor
Glut.CURSOR_INFO
3195 if conf.underinfo
then showtext '
f'
("ont: " ^
s);
3196 Glut.setCursor
Glut.CURSOR_TEXT
3199 | Mpan
_ | Msel
_ | Mzoom
_ | Mscroll
->
3207 let fontpath = ref "";;
3211 match Sys.os_type
with
3212 | "Win32" -> Sys.getenv
"HOMEPATH"
3213 | _ -> Sys.getenv
"HOME"
3216 ("Can not determine home directory location: " ^
3217 Printexc.to_string exn
);
3221 let config_of c attrs
=
3225 | "scroll-bar-width" -> { c with scrollbw
= max
0 (int_of_string
v) }
3226 | "scroll-handle-height" -> { c with scrollh
= max
0 (int_of_string
v) }
3227 | "case-insensitive-search" -> { c with icase
= bool_of_string
v }
3228 | "preload" -> { c with preload = bool_of_string
v }
3229 | "page-bias" -> { c with pagebias
= int_of_string
v }
3230 | "scroll-step" -> { c with scrollstep
= max
1 (int_of_string
v) }
3231 | "auto-scroll-step" ->
3232 { c with autoscrollstep
= max
0 (int_of_string
v) }
3233 | "max-height-fit" -> { c with maxhfit
= bool_of_string
v }
3234 | "crop-hack" -> { c with crophack
= bool_of_string
v }
3235 | "throttle" -> { c with showall
= bool_of_string
v }
3236 | "highlight-links" -> { c with hlinks
= bool_of_string
v }
3237 | "under-cursor-info" -> { c with underinfo
= bool_of_string
v }
3238 | "vertical-margin" ->
3239 { c with interpagespace = max
0 (int_of_string
v) }
3241 let zoom = float_of_string
v /. 100. in
3242 let zoom = max
0.01 zoom in
3243 { c with zoom = zoom }
3244 | "presentation" -> { c with presentation = bool_of_string
v }
3245 | "rotation-angle" -> { c with angle
= int_of_string
v }
3246 | "width" -> { c with winw
= max
20 (int_of_string
v) }
3247 | "height" -> { c with winh
= max
20 (int_of_string
v) }
3248 | "persistent-bookmarks" -> { c with savebmarks
= bool_of_string
v }
3249 | "proportional-display" -> { c with proportional
= bool_of_string
v }
3250 | "pixmap-cache-size" -> { c with memlimit
= max
2 (int_of_string
v) }
3251 | "tex-count" -> { c with texcount
= max
1 (int_of_string
v) }
3252 | "slice-height" -> { c with sliceheight
= max
2 (int_of_string
v) }
3253 | "block-width" -> { c with blockwidth
= max
2 (int_of_string
v) }
3254 | "thumbnail-width" -> { c with thumbw
= max
2 (int_of_string
v) }
3255 | "persistent-location" -> { c with jumpback
= bool_of_string
v }
3256 | "background-color" -> { c with bgcolor
= color_of_string v }
3257 | "scrollbar-in-presentation" ->
3258 { c with scrollbarinpm
= bool_of_string
v }
3261 prerr_endline
("Error processing attribute (`" ^
3262 k ^
"'=`" ^
v ^
"'): " ^
Printexc.to_string exn
);
3265 let rec fold c = function
3268 let c = apply c k
v in
3274 let fromstring f pos
n v d =
3277 dolog "Error processing attribute (%S=%S) at %d\n%s"
3278 n v pos
(Printexc.to_string exn
)
3283 let bookmark_of attrs
=
3284 let rec fold title page rely
= function
3285 | ("title", v) :: rest
-> fold v page rely rest
3286 | ("page", v) :: rest
-> fold title v rely rest
3287 | ("rely", v) :: rest
-> fold title page
v rest
3288 | _ :: rest
-> fold title page rely rest
3289 | [] -> title, page
, rely
3291 fold "invalid" "0" "0" attrs
3295 let rec fold path page rely pan
= function
3296 | ("path", v) :: rest
-> fold v page rely pan rest
3297 | ("page", v) :: rest
-> fold path
v rely pan rest
3298 | ("rely", v) :: rest
-> fold path page
v pan rest
3299 | ("pan", v) :: rest
-> fold path page rely
v rest
3300 | _ :: rest
-> fold path page rely pan rest
3301 | [] -> path
, page
, rely
, pan
3303 fold "" "0" "0" "0" attrs
3306 let setconf dst src
=
3307 dst
.scrollbw
<- src
.scrollbw
;
3308 dst
.scrollh
<- src
.scrollh
;
3309 dst
.icase
<- src
.icase
;
3310 dst
.preload <- src
.preload;
3311 dst
.pagebias
<- src
.pagebias
;
3312 dst
.verbose
<- src
.verbose
;
3313 dst
.scrollstep
<- src
.scrollstep
;
3314 dst
.maxhfit
<- src
.maxhfit
;
3315 dst
.crophack
<- src
.crophack
;
3316 dst
.autoscrollstep
<- src
.autoscrollstep
;
3317 dst
.showall
<- src
.showall
;
3318 dst
.hlinks
<- src
.hlinks
;
3319 dst
.underinfo
<- src
.underinfo
;
3320 dst
.interpagespace <- src
.interpagespace;
3321 dst
.zoom <- src
.zoom;
3322 dst
.presentation <- src
.presentation;
3323 dst
.angle
<- src
.angle
;
3324 dst
.winw
<- src
.winw
;
3325 dst
.winh
<- src
.winh
;
3326 dst
.savebmarks
<- src
.savebmarks
;
3327 dst
.memlimit
<- src
.memlimit
;
3328 dst
.proportional
<- src
.proportional
;
3329 dst
.texcount
<- src
.texcount
;
3330 dst
.sliceheight
<- src
.sliceheight
;
3331 dst
.blockwidth
<- src
.blockwidth
;
3332 dst
.thumbw
<- src
.thumbw
;
3333 dst
.jumpback
<- src
.jumpback
;
3334 dst
.bgcolor
<- src
.bgcolor
;
3335 dst
.scrollbarinpm
<- src
.scrollbarinpm
;
3339 let l = String.length
s in
3340 let b = Buffer.create
l in
3346 let h = Hashtbl.create
10 in
3347 let dc = { defconf with angle
= defconf.angle
} in
3348 let rec toplevel v t spos
_ =
3350 | Vdata
| Vcdata
| Vend
-> v
3351 | Vopen
("llppconfig", _, closed
) ->
3354 else { v with f = llppconfig
}
3356 error
"unexpected subelement at top level" s spos
3357 | Vclose
_ -> error
"unexpected close at top level" s spos
3359 and llppconfig
v t spos
_ =
3361 | Vdata
| Vcdata
-> v
3362 | Vend
-> error
"unexpected end of input in llppconfig" s spos
3363 | Vopen
("defaults", attrs
, closed
) ->
3364 let c = config_of dc attrs
in
3368 else { v with f = skip
"defaults" (fun () -> v) }
3370 | Vopen
("ui-font", _, closed
) ->
3373 else { v with f = uifont
(Buffer.create
10) }
3375 | Vopen
("doc", attrs
, closed
) ->
3376 let pathent, spage
, srely
, span
= doc_of attrs
in
3377 let path = unent pathent
3378 and pageno = fromstring int_of_string spos
"page" spage
0
3379 and rely
= fromstring float_of_string spos
"rely" srely
0.0
3380 and pan
= fromstring int_of_string spos
"pan" span
0 in
3381 let c = config_of dc attrs
in
3382 let anchor = (pageno, rely
) in
3384 then (Hashtbl.add
h path (c, [], pan
, anchor); v)
3385 else { v with f = doc
path pan
anchor c [] }
3388 error
"unexpected subelement in llppconfig" s spos
3390 | Vclose
"llppconfig" -> { v with f = toplevel }
3391 | Vclose
_ -> error
"unexpected close in llppconfig" s spos
3393 and uifont
b v t spos epos
=
3396 Buffer.add_substring
b s spos
(epos
- spos
);
3398 | Vopen
(_, _, _) ->
3399 error
"unexpected subelement in ui-font" s spos
3400 | Vclose
"ui-font" ->
3401 if String.length
!fontpath = 0
3402 then fontpath := Buffer.contents
b;
3403 { v with f = llppconfig
}
3404 | Vclose
_ -> error
"unexpected close in ui-font" s spos
3405 | Vend
-> error
"unexpected end of input in ui-font" s spos
3407 and doc
path pan
anchor c bookmarks v t spos
_ =
3409 | Vdata
| Vcdata
-> v
3410 | Vend
-> error
"unexpected end of input in doc" s spos
3411 | Vopen
("bookmarks", _, closed
) ->
3414 else { v with f = pbookmarks
path pan
anchor c bookmarks }
3416 | Vopen
(_, _, _) ->
3417 error
"unexpected subelement in doc" s spos
3420 Hashtbl.add
h path (c, List.rev
bookmarks, pan
, anchor);
3421 { v with f = llppconfig
}
3423 | Vclose
_ -> error
"unexpected close in doc" s spos
3425 and pbookmarks
path pan
anchor c bookmarks v t spos
_ =
3427 | Vdata
| Vcdata
-> v
3428 | Vend
-> error
"unexpected end of input in bookmarks" s spos
3429 | Vopen
("item", attrs
, closed
) ->
3430 let titleent, spage
, srely
= bookmark_of attrs
in
3431 let page = fromstring int_of_string spos
"page" spage
0
3432 and rely
= fromstring float_of_string spos
"rely" srely
0.0 in
3433 let bookmarks = (unent titleent, 0, (page, rely
)) :: bookmarks in
3435 then { v with f = pbookmarks
path pan
anchor c bookmarks }
3438 { v with f = skip
"item" f }
3441 error
"unexpected subelement in bookmarks" s spos
3443 | Vclose
"bookmarks" ->
3444 { v with f = doc
path pan
anchor c bookmarks }
3446 | Vclose
_ -> error
"unexpected close in bookmarks" s spos
3448 and skip tag
f v t spos
_ =
3450 | Vdata
| Vcdata
-> v
3452 error
("unexpected end of input in skipped " ^ tag
) s spos
3453 | Vopen
(tag'
, _, closed
) ->
3457 let f'
() = { v with f = skip tag
f } in
3458 { v with f = skip tag'
f'
}
3462 else error
("unexpected close in skipped " ^ tag
) s spos
3465 parse
{ f = toplevel; accu = () } s;
3471 let len = in_channel_length ic
in
3472 let s = String.create
len in
3473 really_input ic
s 0 len;
3476 | Parse_error
(msg
, s, pos
) ->
3477 let subs = subs s pos
in
3478 let s = Printf.sprintf
"%s: at %d [..%s..]" msg pos
subs in
3479 failwith
("parse error: " ^
s)
3482 failwith
("config load error: " ^
Printexc.to_string exn
)
3488 let dir = Filename.concat
home ".config" in
3489 if Sys.is_directory
dir then dir else home
3492 Filename.concat
dir "llpp.conf"
3495 let confpath = ref defconfpath;;
3498 if Sys.file_exists
!confpath
3501 (try Some
(open_in_bin
!confpath)
3504 ("Error opening configuation file `" ^
!confpath ^
"': " ^
3505 Printexc.to_string exn
);
3514 ("Error loading configuation from `" ^
!confpath ^
"': " ^
3515 Printexc.to_string exn
);
3521 f (Hashtbl.create
0, defconf)
3526 let pc, pb
, px
, pa
=
3528 Hashtbl.find h (Filename.basename
state.path)
3529 with Not_found
-> dc, [], 0, (0, 0.0)
3533 state.bookmarks <- pb
;
3535 state.scrollw
<- conf.scrollbw
;
3537 then state.anchor <- pa
;
3538 cbput state.hists
.nav pa
;
3543 let add_attrs bb always
dc c =
3546 then Printf.bprintf bb
"\n %s='%b'" s a
3549 then Printf.bprintf bb
"\n %s='%d'" s a
3552 then Printf.bprintf bb
"\n %s='%d'" s (truncate
(a*.100.))
3556 Printf.bprintf bb
"\n %s='%s'" s (color_to_string a)
3560 then dc.winw
, dc.winh
3562 match state.fullscreen
with
3564 | None
-> c.winw
, c.winh
3566 let zoom, presentation, interpagespace, showall
=
3568 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
3570 match state.mode with
3571 | Birdseye
(bc
, _, _, _, _) ->
3572 bc
.zoom, bc
.presentation, bc
.interpagespace, bc
.showall
3573 | _ -> c.zoom, c.presentation, c.interpagespace, c.showall
3575 oi
"width" w dc.winw
;
3576 oi
"height" h dc.winh
;
3577 oi
"scroll-bar-width" c.scrollbw
dc.scrollbw
;
3578 oi
"scroll-handle-height" c.scrollh
dc.scrollh
;
3579 ob "case-insensitive-search" c.icase
dc.icase
;
3580 ob "preload" c.preload dc.preload;
3581 oi
"page-bias" c.pagebias
dc.pagebias
;
3582 oi
"scroll-step" c.scrollstep
dc.scrollstep
;
3583 oi
"auto-scroll-step" c.autoscrollstep
dc.autoscrollstep
;
3584 ob "max-height-fit" c.maxhfit
dc.maxhfit
;
3585 ob "crop-hack" c.crophack
dc.crophack
;
3586 ob "throttle" showall
dc.showall
;
3587 ob "highlight-links" c.hlinks
dc.hlinks
;
3588 ob "under-cursor-info" c.underinfo
dc.underinfo
;
3589 oi
"vertical-margin" interpagespace dc.interpagespace;
3590 oz
"zoom" zoom dc.zoom;
3591 ob "presentation" presentation dc.presentation;
3592 oi
"rotation-angle" c.angle
dc.angle
;
3593 ob "persistent-bookmarks" c.savebmarks
dc.savebmarks
;
3594 ob "proportional-display" c.proportional
dc.proportional
;
3595 oi
"pixmap-cache-size" c.memlimit
dc.memlimit
;
3596 oi
"tex-count" c.texcount
dc.texcount
;
3597 oi
"slice-height" c.sliceheight
dc.sliceheight
;
3598 oi
"block-width" c.blockwidth
dc.blockwidth
;
3599 oi
"thumbnail-width" c.thumbw
dc.thumbw
;
3600 ob "persistent-location" c.jumpback
dc.jumpback
;
3601 oc
"background-color" c.bgcolor
dc.bgcolor
;
3602 ob "scrollbar-in-presentation" c.scrollbarinpm
dc.scrollbarinpm
;
3606 let bb = Buffer.create
32768 in
3608 let dc = if conf.bedefault
then conf else dc in
3609 Buffer.add_string
bb "<llppconfig>\n";
3611 if String.length
!fontpath > 0
3612 then Printf.bprintf
bb "<ui-font><![CDATA[%s]]></ui-font>\n" !fontpath;
3614 Buffer.add_string
bb "<defaults ";
3615 add_attrs bb true dc dc;
3616 Buffer.add_string
bb "/>\n";
3618 let adddoc path pan
anchor c bookmarks =
3619 if bookmarks == [] && c = dc && anchor = emptyanchor
3622 Printf.bprintf
bb "<doc path='%s'"
3623 (enent
path 0 (String.length
path));
3625 if anchor <> emptyanchor
3627 let n, y = anchor in
3628 Printf.bprintf
bb " page='%d'" n;
3631 Printf.bprintf
bb " rely='%f'" y
3636 then Printf.bprintf
bb " pan='%d'" pan
;
3638 add_attrs bb false dc c;
3640 begin match bookmarks with
3641 | [] -> Buffer.add_string
bb "/>\n"
3643 Buffer.add_string
bb ">\n<bookmarks>\n";
3644 List.iter
(fun (title, _level
, (page, rely
)) ->
3646 "<item title='%s' page='%d'"
3647 (enent
title 0 (String.length
title))
3652 Printf.bprintf
bb " rely='%f'" rely
3654 Buffer.add_string
bb "/>\n";
3656 Buffer.add_string
bb "</bookmarks>\n</doc>\n";
3662 match state.mode with
3663 | Birdseye
(_, pan, _, _, _) -> pan
3666 let basename = Filename.basename state.path in
3667 adddoc basename pan (getanchor ())
3670 match state.autoscroll
with
3672 | None
-> conf.autoscrollstep
}
3673 (if conf.savebmarks
then state.bookmarks else []);
3675 Hashtbl.iter
(fun path (c, bookmarks, x, y) ->
3677 then adddoc path x y c bookmarks
3679 Buffer.add_string
bb "</llppconfig>";
3682 if Buffer.length
bb > 0
3685 let tmp = !confpath ^
".tmp" in
3686 let oc = open_out_bin
tmp in
3687 Buffer.output_buffer
oc bb;
3689 Sys.rename
tmp !confpath;
3692 ("error while saving configuration: " ^
Printexc.to_string exn
)
3699 [("-p", Arg.String
(fun s -> state.password
<- s) ,
3700 "<password> Set password");
3702 ("-f", Arg.String
(fun s -> State.fontpath := s),
3703 "<path> Set path to the user interface font");
3705 ("-c", Arg.String
(fun s -> State.confpath := s),
3706 "<path> Set path to the configuration file");
3708 ("-v", Arg.Unit
(fun () ->
3710 "%s\nconfiguration path: %s\n"
3714 exit
0), " Print version and exit");
3717 (fun s -> state.path <- s)
3718 ("Usage: " ^
Sys.argv
.(0) ^
" [options] some.pdf\nOptions:")
3720 if String.length
state.path = 0
3721 then (prerr_endline
"file name missing"; exit
1);
3725 let _ = Glut.init
Sys.argv
in
3726 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
3727 let () = Glut.initWindowSize
conf.winw
conf.winh
in
3728 let _ = Glut.createWindow
("llpp " ^
Filename.basename state.path) in
3731 if Sys.os_type
= "Unix"
3733 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
3735 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
3736 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
3737 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
3738 Unix.bind
sock addr;
3740 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
3741 Unix.connect
csock addr;
3742 let ssock, _ = Unix.accept
sock in
3745 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
3746 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
3753 let () = Glut.displayFunc
display in
3754 let () = Glut.reshapeFunc
reshape in
3755 let () = Glut.keyboardFunc
keyboard in
3756 let () = Glut.specialFunc
special in
3757 let () = Glut.idleFunc
(Some
idle) in
3758 let () = Glut.mouseFunc
mouse in
3759 let () = Glut.motionFunc
motion in
3760 let () = Glut.passiveMotionFunc
pmotion in
3763 conf.angle
, conf.proportional
, conf.texcount
,
3764 conf.sliceheight
, conf.blockwidth
, !State.fontpath
3766 state.csock <- csock;
3767 state.ssock <- ssock;
3768 state.text <- "Opening " ^
state.path;
3769 writeopen state.path state.password
;
3775 | Glut.BadEnum
"key in special_of_int" ->
3776 showtext '
!'
" LablGlut bug: special key not recognized";
3779 if Sys.os_type
<> "Unix"
3780 then Unix.shutdown
ssock Unix.SHUTDOWN_ALL
;