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;;
132 { mutable scrollbw
: int
133 ; mutable scrollh
: int
134 ; mutable icase
: bool
135 ; mutable preload
: bool
136 ; mutable pagebias
: int
137 ; mutable verbose
: bool
138 ; mutable scrollstep
: int
139 ; mutable maxhfit
: bool
140 ; mutable crophack
: bool
141 ; mutable autoscrollstep
: int
142 ; mutable showall
: bool
143 ; mutable hlinks
: bool
144 ; mutable underinfo
: bool
145 ; mutable interpagespace
: interpagespace
146 ; mutable zoom
: float
147 ; mutable presentation
: bool
148 ; mutable angle
: angle
151 ; mutable savebmarks
: bool
152 ; mutable proportional
: proportional
153 ; mutable memlimit
: int
154 ; mutable texcount
: texcount
155 ; mutable sliceheight
: sliceheight
156 ; mutable blockwidth
: blockwidth
157 ; mutable thumbw
: width
158 ; mutable jumpback
: bool
159 ; mutable bgcolor
: float * float * float
160 ; mutable bedefault
: bool
161 ; mutable scrollbarinpm
: bool
165 type anchor
= pageno
* top
;;
167 type outline
= string * int * anchor
169 | Oarray
of outline array
170 | Olist
of outline list
171 | Onarrow
of string * outline array
* outline array
174 type rect
= float * float * float * float * float * float * float * float;;
176 type pagemapkey
= pageno
* width
* angle
* proportional
* gen
;;
178 let emptyanchor = (0, 0.0);;
181 | Birdseye
of (conf
* leftx
* pageno
* pageno
* anchor
)
182 | Outline
of (bool * int * int * outline array
* string * int * mode
)
183 | Items
of (int * int * item array
* string * int * mode
)
184 | Textentry
of (textentry
* onleave
)
186 and onleave
= leavetextentrystatus
-> unit
187 and leavetextentrystatus
= | Cancel
| Confirm
188 and item
= string * int * action
191 | Action
of (int -> int -> string -> int -> mode
)
194 let isbirdseye = function Birdseye _
-> true | _
-> false;;
195 let istextentry = function Textentry _
-> true | _
-> false;;
198 { mutable csock
: Unix.file_descr
199 ; mutable ssock
: Unix.file_descr
203 ; mutable scrollw
: int
204 ; mutable anchor
: anchor
206 ; mutable layout
: layout list
207 ; pagemap
: (pagemapkey
, (opaque
* pixmapsize
)) Hashtbl.t
208 ; pagelru
: opaque
Queue.t
209 ; mutable pdims
: (pageno
* width
* height
* leftx
) list
210 ; mutable pagecount
: int
211 ; mutable rendering
: bool
212 ; mutable mstate
: mstate
213 ; mutable searchpattern
: string
214 ; mutable rects
: (pageno
* recttype
* rect
) list
215 ; mutable rects1
: (pageno
* recttype
* rect
) list
216 ; mutable text
: string
217 ; mutable fullscreen
: (width
* height
) option
218 ; mutable mode
: mode
219 ; mutable outlines
: outlines
220 ; mutable bookmarks
: outline list
221 ; mutable path
: string
222 ; mutable password
: string
223 ; mutable invalidated
: int
224 ; mutable colorscale
: float
225 ; mutable memused
: int
227 ; mutable throttle
: layout list
option
228 ; mutable autoscroll
:int option
229 ; mutable help
: item array
230 ; mutable docinfo
: (int * string) list
231 ; mutable deadline
: float
235 { pat
: string circbuf
236 ; pag
: string circbuf
237 ; nav
: anchor circbuf
257 ; presentation
= false
262 ; proportional
= true
263 ; memlimit
= 32*1024*1024
269 ; bgcolor
= (0.5, 0.5, 0.5)
271 ; scrollbarinpm
= true
275 let conf = { defconf with angle
= defconf.angle
};;
278 let strings = ("llpp version " ^
Help.version
) :: "" :: Help.keys
in
279 Array.of_list
(List.map
(fun s
-> s
, 0, Noaction
) strings);
289 ; anchor
= emptyanchor
292 ; pagelru
= Queue.create
()
293 ; pagemap
= Hashtbl.create
10
304 ; outlines
= Olist
[]
310 { nav
= cbnew 100 (0, 0.0)
328 Printf.kprintf prerr_endline fmt
330 Printf.kprintf ignore fmt
334 let len = String.length s
in
336 let b = Buffer.create
n in
337 Buffer.add_char
b (Char.chr
((len lsr 24) land 0xff));
338 Buffer.add_char
b (Char.chr
((len lsr 16) land 0xff));
339 Buffer.add_char
b (Char.chr
((len lsr 8) land 0xff));
340 Buffer.add_char
b (Char.chr
((len lsr 0) land 0xff));
341 Buffer.add_string
b s
;
342 let s'
= Buffer.contents
b in
343 let n'
= Unix.write fd
s'
0 n in
344 if n'
!= n then failwith
"write failed";
349 let n = Unix.read fd
s 0 4 in
350 if n != 4 then failwith
"incomplete read(len)";
352 lor (Char.code
s.[0] lsl 24)
353 lor (Char.code
s.[1] lsl 16)
354 lor (Char.code
s.[2] lsl 8)
355 lor (Char.code
s.[3] lsl 0)
357 let s = String.create
len in
358 let n = Unix.read fd
s 0 len in
359 if n != len then failwith
"incomplete read(data)";
364 let b = Buffer.create
10 in
365 Buffer.add_string
b s;
366 let rec combine = function
369 Buffer.add_char
b ' '
;
372 | `
b b -> if b then "1" else "0"
374 | `i i
-> string_of_int i
375 | `f f
-> string_of_float f
376 | `I f
-> string_of_int
(truncate f
)
378 Buffer.add_string
b s;
385 let cmd = Buffer.contents
(makecmd s l
) in
386 writecmd state.csock
cmd;
392 let d = conf.winh
- h
in
399 let rec f pn ph pi fh l
=
401 | (n, _
, h
, _
) :: rest
->
402 let ips = calcips h
in
407 if isbirdseye state.mode
&& pn
= 0
412 let fh = fh + ((n - pn
) * (ph
+ pi
)) in
417 if conf.presentation
|| (isbirdseye state.mode
&& pn
= 0)
421 let fh = fh + ((state.pagecount
- pn
) * (ph
+ pi
)) + inc in
424 let fh = f 0 0 0 0 state.pdims
in
428 let getpageyh pageno
=
429 let rec f pn ph pi y l
=
431 | (n, _
, h
, _
) :: rest
->
432 let ips = calcips h
in
435 let h = if n = pageno
then h else ph
in
436 if conf.presentation
&& n = pageno
438 y
+ (pageno
- pn
) * (ph
+ pi
) + pi
, h
440 y
+ (pageno
- pn
) * (ph
+ pi
), h
442 let y = y + (if conf.presentation
then pi
else 0) in
443 let y = y + (n - pn
) * (ph
+ pi
) in
447 y + (pageno
- pn
) * (ph
+ pi
), ph
449 f 0 0 0 0 state.pdims
452 let getpagey pageno
= fst
(getpageyh pageno
);;
455 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~accu
=
456 let ((w
, h, ips, x
) as curr
), rest
, pdimno
, yinc
=
458 | (pageno'
, w
, h, x
) :: rest
when pageno'
= pageno
->
459 let ips = calcips h in
461 if conf.presentation
|| (isbirdseye state.mode
&& pageno
= 0)
465 (w
, h, ips, x
), rest
, pdimno
+ 1, yinc
467 prev
, pdims
, pdimno
, 0
469 let dy = dy + yinc in
470 let py = py + yinc in
471 if pageno
= state.pagecount
|| dy >= sh
476 if py + h <= vy - yinc
478 let py = py + h + ips in
479 let dy = max
0 (py - y) in
488 let pagey = vy - py in
489 let pagevh = h - pagey in
490 let pagevh = min
(sh
- dy) pagevh in
491 let off = if yinc > 0 then py - vy else 0 in
492 let py = py + h + ips in
498 ; pagedispy
= dy + off
499 ; pagey = pagey + off
500 ; pagevh = pagevh - off
504 let accu = e :: accu in
513 if state.invalidated
= 0
532 let y = state.y + incr
in
534 let y = min
y (state.maxy
- (if conf.maxhfit
then conf.winh
else 0)) in
538 let getopaque pageno
=
539 try Some
(Hashtbl.find
state.pagemap
540 (pageno
, state.w
, conf.angle
, conf.proportional
, state.gen
))
541 with Not_found
-> None
544 let cache pageno opaque
=
545 Hashtbl.replace
state.pagemap
546 (pageno
, state.w
, conf.angle
, conf.proportional
, state.gen
) opaque
549 let validopaque opaque
= String.length opaque
> 0;;
552 match getopaque l
.pageno
with
553 | None
when not
state.rendering
->
554 state.rendering
<- true;
555 cache l
.pageno
("", -1);
556 wcmd "render" [`i
(l
.pageno
+ 1)
563 let loadlayout layout =
564 let rec f all
= function
566 begin match getopaque l
.pageno
with
567 | None
-> render l
; f false ls
568 | Some
(opaque
, _
) -> f (all
&& validopaque opaque
) ls
572 f (layout <> []) layout;
575 let findpageforopaque opaque
=
577 (fun k
(v
, s) a
-> if v
= opaque
then Some
(k
, s) else a
)
581 let pagevisible layout n = List.exists
(fun l
-> l
.pageno
= n) layout;;
585 if conf.preload && not
state.rendering
586 then conf.memlimit
> state.memused
591 let presentation = conf.presentation in
592 let interpagespace = conf.interpagespace in
593 let maxy = state.maxy in
594 conf.presentation <- false;
595 conf.interpagespace <- 0;
596 state.maxy <- calcheight ();
598 match state.layout with
600 | l
:: _
-> getpagey l
.pageno
+ l
.pagey
602 let y = if y < conf.winh
then 0 else y - conf.winh
in
603 let h = state.y - y + conf.winh
*3 in
604 let pages = layout y h in
605 List.iter
render pages;
606 conf.presentation <- presentation;
607 conf.interpagespace <- interpagespace;
613 let y = min
state.maxy y in
614 let pages = layout y conf.winh
in
615 let ready = loadlayout pages in
621 state.layout <- pages;
622 state.throttle
<- None
;
623 Glut.postRedisplay
();
626 state.throttle
<- Some
pages;
631 state.layout <- pages;
632 state.throttle
<- None
;
633 Glut.postRedisplay
();
635 begin match state.mode
with
636 | Birdseye
(conf, leftx
, pageno
, hooverpageno
, anchor
) ->
637 if not
(pagevisible pages pageno
)
639 match state.layout with
642 state.mode
<- Birdseye
(conf, leftx
, l
.pageno
, hooverpageno
, anchor
)
649 let gotoy_and_clear_text y =
651 if not
conf.verbose
then state.text
<- "";
655 match state.layout with
657 | l
:: _
-> (l
.pageno
, float l
.pagey /. float l
.pageh
)
660 let getanchory (n, top
) =
661 let y, h = getpageyh n in
662 y + (truncate
(top
*. float h));
665 let gotoanchor anchor
=
666 gotoy (getanchory anchor
);
670 cbput state.hists
.nav
(getanchor ());
674 let anchor = cbgetc state.hists
.nav dir
in
679 let y, h = getpageyh n in
680 gotoy_and_clear_text (y + (truncate
(top
*. float h)));
683 let gotopage1 n top
=
684 let y = getpagey n in
685 gotoy_and_clear_text (y + top
);
693 state.invalidated
<- state.invalidated
+ 1;
697 let c = c *. state.colorscale
in
701 let scalecolor2 (r
, g
, b) =
702 (r
*. state.colorscale
, g
*. state.colorscale
, b *. state.colorscale
);
706 state.maxy <- calcheight ();
707 match state.mode
with
708 | Birdseye
(_
, _
, pageno
, _
, _
) ->
709 let y, h = getpageyh pageno
in
710 let top = (conf.winh
- h) / 2 in
711 gotoy (max
0 (y - top))
712 | _
-> gotoanchor state.anchor
716 GlMat.mode `projection
;
717 GlMat.load_identity
();
718 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
719 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
720 GlMat.scale3
(2.0 /. float state.w
, 2.0 /. float conf.winh
, 1.0);
723 GlMat.translate ~x
:(float state.x
) ();
728 GlMat.mode `projection
;
729 GlMat.load_identity
();
730 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
731 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
732 GlMat.scale3
(2.0 /. float conf.winw
, 2.0 /. float conf.winh
, 1.0);
736 let firsttime = ref true in
738 if state.invalidated
= 0 && not
!firsttime
739 then state.anchor <- getanchor ();
743 let w = truncate
(float w *. conf.zoom
) - state.scrollw
in
747 GlMat.mode `modelview
;
748 GlMat.load_identity
();
749 GlClear.color
(scalecolor 1.0);
750 GlClear.clear
[`color
];
753 wcmd "geometry" [`i
w; `i
h];
756 let drawstring size x
y s =
758 Gl.enable `texture_2d
;
759 ignore
(drawstr size x
y s);
761 Gl.disable `texture_2d
;
764 let drawstring1 size x
y s =
769 let len = String.length
state.text
in
771 GlDraw.color
(0.0, 0.0, 0.0);
773 (0.0, float (conf.winh
- 18))
774 (float (conf.winw
- state.scrollw
- 1), float conf.winh
)
776 GlDraw.color
(1.0, 1.0, 1.0);
777 drawstring 14 (if len > 0 then 8 else 2) (conf.winh
- 5) s;
779 match state.mode
with
780 | Textentry
((prefix
, text
, _
, _
, _
), _
) ->
784 Printf.sprintf
"%s%s_ [%s]" prefix text
state.text
786 Printf.sprintf
"%s%s_" prefix text
791 if len > 0 then drawstring state.text
795 state.text
<- Printf.sprintf
"%c%s" c s;
796 Glut.postRedisplay
();
805 state.rects
<- state.rects1
;
806 Glut.postRedisplay
()
809 let n = Scanf.sscanf
cmd "C %u" (fun n -> n) in
810 state.pagecount
<- n;
811 state.invalidated
<- state.invalidated
- 1;
812 if state.invalidated
= 0
816 let s = Scanf.sscanf
cmd "t %n"
817 (fun n -> String.sub
cmd n (String.length
cmd - n))
819 Glut.setWindowTitle
s
822 let s = Scanf.sscanf
cmd "T %n"
823 (fun n -> String.sub
cmd n (String.length
cmd - n))
825 if istextentry state.mode
832 Glut.postRedisplay
();
838 let s = Scanf.sscanf
cmd "V %n"
839 (fun n -> String.sub
cmd n (String.length
cmd - n))
845 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
846 Scanf.sscanf
cmd "F %u %d %f %f %f %f %f %f %f %f"
847 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
848 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
850 let y = (getpagey pageno) + truncate y0
in
853 state.rects1
<- [pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)]
856 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
857 Scanf.sscanf
cmd "R %u %d %f %f %f %f %f %f %f %f"
858 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
859 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
862 (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) :: state.rects1
865 let n, w, _h
, r
, l
, s, p
=
866 Scanf.sscanf
cmd "r %u %u %u %d %d %u %s"
867 (fun n w h r l
s p
->
868 (n-1, w, h, r
, l
!= 0, s, p
))
871 state.memused
<- state.memused
+ s;
874 match state.throttle
with
875 | None
-> state.layout
876 | Some
layout -> layout
880 if state.memused
<= conf.memlimit
|| Queue.is_empty
state.pagelru
883 let opaque = Queue.peek
state.pagelru
in
884 match findpageforopaque opaque with
885 | None
-> failwith
"bug in gc"
886 | Some
(pagekey
, size
) ->
887 let n, w, a
, p
, g
= pagekey
in
890 || p
!= conf.proportional
892 || not
(pagevisible layout n)
894 ignore
(Queue.pop
state.pagelru
);
895 wcmd "free" [`
s opaque];
896 Hashtbl.remove
state.pagemap pagekey
;
897 state.memused
<- state.memused
- size
;
903 Hashtbl.replace
state.pagemap
(n, w, r
, l
, state.gen
) (p
, s);
904 Queue.push p
state.pagelru
;
905 state.rendering
<- false;
907 begin match state.throttle
with
909 if pagevisible state.layout n
910 then Glut.postRedisplay
();
911 let allvisible = loadlayout state.layout in
912 if allvisible then preload ()
918 let y = getpagey l
.pageno + l
.pagey in
924 Scanf.sscanf
cmd "l %u %u %u %u" (fun n w h x
-> n, w, h, x
)
926 state.pdims
<- pdim :: state.pdims
929 let (l
, n, t
, h, pos
) =
930 Scanf.sscanf
cmd "o %u %u %d %u %n" (fun l
n t
h pos
-> l
, n, t
, h, pos
)
932 let s = String.sub
cmd pos
(String.length
cmd - pos
) in
933 let outline = (s, l
, (n, float t
/. float h)) in
935 match state.outlines with
936 | Olist
outlines -> Olist
(outline :: outlines)
937 | Oarray _
-> Olist
[outline]
938 | Onarrow _
-> Olist
[outline]
940 state.outlines <- outlines
944 if String.length
cmd > 1 && cmd.[1] = '
e'
946 state.docinfo
<- List.rev
state.docinfo
948 let s = Scanf.sscanf
cmd "i %n"
949 (fun n -> String.sub
cmd n (String.length
cmd - n))
951 state.docinfo
<- (1, s) :: state.docinfo
954 dolog "unknown cmd `%S'" cmd
957 let now = Unix.gettimeofday
;;
960 if state.deadline
== nan
then state.deadline
<- now ();
964 then max
0.0 (state.deadline
-. now ())
967 let r, _
, _
= Unix.select
[state.csock
] [] [] timeout in
970 begin match state.autoscroll
with
971 | Some step
when step
!= 0 ->
972 let y = state.y + step
in
976 else if y >= state.maxy then 0 else y
980 then state.text
<- "";
981 state.deadline
<- state.deadline
+. 0.005;
984 state.deadline
<- state.deadline
+. delay
;
988 let cmd = readcmd state.csock
in
997 let action = function
998 | HCprev
-> cbget cb ~
-1
999 | HCnext
-> cbget cb
1
1000 | HCfirst
-> cbget cb ~
-(cb
.rc)
1001 | HClast
-> cbget cb
(cb
.len - 1 - cb
.rc)
1002 and cancel
() = cb
.rc <- rc
1006 let search pattern forward
=
1007 if String.length pattern
> 0
1010 match state.layout with
1013 l
.pageno, (l
.pagey + if forward
then 0 else 0*l
.pagevh)
1016 let b = makecmd "search"
1017 [`
b conf.icase
; `i
pn; `i
py; `i
(if forward
then 1 else 0)]
1019 Buffer.add_char
b '
,'
;
1020 Buffer.add_string
b pattern
;
1021 Buffer.add_char
b '
\000'
;
1024 writecmd state.csock
cmd;
1027 let intentry text key
=
1028 let c = Char.unsafe_chr key
in
1031 let s = "x" in s.[0] <- c;
1032 let text = text ^
s in
1036 state.text <- Printf.sprintf
"invalid char (%d, `%c')" key
c;
1041 let b = Buffer.create
(String.length
s + 1) in
1042 Buffer.add_string
b s;
1043 Buffer.add_char
b c;
1047 let textentry text key
=
1048 let c = Char.unsafe_chr key
in
1050 | _
when key
>= 32 && key
< 127 ->
1051 let text = addchar text c in
1055 dolog "unhandled key %d char `%c'" key
(Char.unsafe_chr key
);
1059 let reinit angle proportional
=
1060 state.anchor <- getanchor ();
1061 conf.angle
<- angle
;
1062 conf.proportional
<- proportional
;
1064 wcmd "reinit" [`i angle
; `
b proportional
];
1068 let zoom = max
0.01 zoom in
1069 if zoom <> conf.zoom
1074 reshape conf.winw
conf.winh
;
1075 state.text <- Printf.sprintf
"zoom is now %-5.1f" (zoom *. 100.0);
1079 let enterbirdseye () =
1080 let zoom = float conf.thumbw
/. float conf.winw
in
1081 let birdseyepageno =
1082 let cy = conf.winh
/ 2 in
1086 let rec fold best
= function
1089 let d = cy - (l
.pagedispy
+ l
.pagevh/2)
1090 and dbest
= cy - (best
.pagedispy
+ best
.pagevh/2) in
1091 if abs
d < abs dbest
1098 state.mode
<- Birdseye
(
1099 { conf with zoom = conf.zoom }, state.x
, birdseyepageno, -1, getanchor ()
1102 conf.presentation <- false;
1103 conf.interpagespace <- 10;
1104 conf.hlinks
<- false;
1106 state.mstate
<- Mnone
;
1107 conf.showall
<- false;
1108 Glut.setCursor
Glut.CURSOR_INHERIT
;
1111 state.text <- Printf.sprintf
"birds eye mode on (zoom %3.1f%%)"
1116 reshape conf.winw
conf.winh
;
1119 let leavebirdseye (c, leftx
, pageno, _
, anchor) goback
=
1121 conf.zoom <- c.zoom;
1122 conf.presentation <- c.presentation;
1123 conf.interpagespace <- c.interpagespace;
1124 conf.showall
<- c.showall
;
1125 conf.hlinks
<- c.hlinks
;
1129 state.text <- Printf.sprintf
"birds eye mode off (zoom %3.1f%%)"
1132 reshape conf.winw
conf.winh
;
1133 state.anchor <- if goback
then anchor else (pageno, 0.0);
1136 let togglebirdseye () =
1137 match state.mode
with
1138 | Birdseye vals
-> leavebirdseye vals
true
1139 | View
| Outline _
-> enterbirdseye ()
1143 let upbirdseye (conf, leftx
, pageno, hooverpageno
, anchor) =
1144 let pageno = max
0 (pageno - 1) in
1145 let rec loop = function
1146 | [] -> gotopage1 pageno 0
1147 | l
:: _
when l
.pageno = pageno ->
1148 if l
.pagedispy
>= 0 && l
.pagey = 0
1149 then Glut.postRedisplay
()
1150 else gotopage1 pageno 0
1151 | _
:: rest
-> loop rest
1154 state.mode
<- Birdseye
(conf, leftx
, pageno, hooverpageno
, anchor)
1157 let downbirdseye (conf, leftx
, pageno, hooverpageno
, anchor) =
1158 let pageno = min
(state.pagecount
- 1) (pageno + 1) in
1159 state.mode
<- Birdseye
(conf, leftx
, pageno, hooverpageno
, anchor);
1160 let rec loop = function
1162 let y, h = getpageyh pageno in
1163 let dy = (y - state.y) - (conf.winh
- h - conf.interpagespace) in
1165 | l
:: _
when l
.pageno = pageno ->
1166 if l
.pagevh != l
.pageh
1167 then gotoy (clamp (l
.pageh
- l
.pagevh + conf.interpagespace))
1168 else Glut.postRedisplay
()
1169 | _
:: rest
-> loop rest
1174 let optentry mode _ key
=
1175 let btos b = if b then "on" else "off" in
1176 let c = Char.unsafe_chr key
in
1180 try conf.scrollstep
<- int_of_string
s with exc
->
1181 state.text <- Printf.sprintf
"bad integer `%s': %s"
1182 s (Printexc.to_string exc
)
1184 TEswitch
("scroll step: ", "", None
, intentry, ondone)
1189 conf.autoscrollstep
<- int_of_string
s;
1190 if state.autoscroll
<> None
1191 then state.autoscroll
<- Some
conf.autoscrollstep
1193 state.text <- Printf.sprintf
"bad integer `%s': %s"
1194 s (Printexc.to_string exc
)
1196 TEswitch
("auto scroll step: ", "", None
, intentry, ondone)
1201 let zoom = float (int_of_string
s) /. 100.0 in
1204 state.text <- Printf.sprintf
"bad integer `%s': %s"
1205 s (Printexc.to_string exc
)
1207 TEswitch
("zoom: ", "", None
, intentry, ondone)
1212 conf.thumbw
<- max
2 (min
1920 (int_of_string
s));
1214 Printf.sprintf
"thumbnail width is set to %d" conf.thumbw
;
1215 begin match mode
with
1217 leavebirdseye beye
false;
1222 state.text <- Printf.sprintf
"bad integer `%s': %s"
1223 s (Printexc.to_string exc
)
1225 TEswitch
("thumbnail width: ", "", None
, intentry, ondone)
1230 Some
(int_of_string
s)
1232 state.text <- Printf.sprintf
"bad integer `%s': %s"
1233 s (Printexc.to_string exc
);
1236 | Some angle
-> reinit angle
conf.proportional
1239 TEswitch
("rotation: ", "", None
, intentry, ondone)
1242 conf.icase
<- not
conf.icase
;
1243 TEdone
("case insensitive search " ^
(btos conf.icase
))
1246 conf.preload <- not
conf.preload;
1248 TEdone
("preload " ^
(btos conf.preload))
1251 conf.verbose
<- not
conf.verbose
;
1252 TEdone
("verbose " ^
(btos conf.verbose
))
1255 conf.maxhfit
<- not
conf.maxhfit
;
1256 state.maxy <- state.maxy + (if conf.maxhfit
then -conf.winh
else conf.winh
);
1257 TEdone
("maxhfit " ^
(btos conf.maxhfit
))
1260 conf.crophack
<- not
conf.crophack
;
1261 TEdone
("crophack " ^
btos conf.crophack
)
1264 conf.showall
<- not
conf.showall
;
1265 TEdone
("throttle " ^
btos conf.showall
)
1268 conf.underinfo
<- not
conf.underinfo
;
1269 TEdone
("underinfo " ^
btos conf.underinfo
)
1272 conf.savebmarks
<- not
conf.savebmarks
;
1273 TEdone
("persistent bookmarks " ^
btos conf.savebmarks
)
1279 match state.layout with
1284 conf.interpagespace <- int_of_string
s;
1285 state.maxy <- calcheight ();
1286 let y = getpagey pageno in
1289 state.text <- Printf.sprintf
"bad integer `%s': %s"
1290 s (Printexc.to_string exc
)
1292 TEswitch
("vertical margin: ", "", None
, intentry, ondone)
1295 reinit conf.angle
(not
conf.proportional
);
1296 TEdone
("proprortional display " ^
btos conf.proportional
)
1299 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
1303 let maxoutlinerows () = (conf.winh
- 31) / 16;;
1305 let enterselector allowdel
outlines errmsg msg
=
1306 if Array.length
outlines = 0
1308 showtext ' ' errmsg
;
1312 Glut.setCursor
Glut.CURSOR_INHERIT
;
1314 match state.layout with
1316 | {pageno=pageno} :: _
-> pageno
1320 if n = Array.length
outlines
1323 let (_
, _
, (outlinepageno
, _
)) = outlines.(n) in
1324 if outlinepageno
>= pageno then n else loop (n+1)
1328 state.mode
<- Outline
1329 (allowdel
, active, max
0 (active - maxoutlinerows () / 2), outlines, "", 0,
1331 Glut.postRedisplay
();
1335 let enteroutlinemode () =
1337 match state.outlines with
1340 let a = Array.of_list
(List.rev l
) in
1341 state.outlines <- Oarray
a;
1343 | Onarrow
(pat
, a, _
) ->
1344 a, "Outline was narrowed to `" ^ pat ^
"' (Ctrl-u to restore)"
1346 enterselector false outlines "Document has no outline" msg
;
1349 let enterbookmarkmode () =
1350 let bookmarks = Array.of_list
state.bookmarks in
1351 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1354 let mode_to_string mode
=
1355 let b = Buffer.create
10 in
1356 let rec f = function
1357 | Textentry
(_
, _
) -> Buffer.add_string
b "Textentry ";
1358 | View
-> Buffer.add_string
b "View"
1359 | Birdseye _
-> Buffer.add_string
b "Birdseye"
1360 | Items _
-> Buffer.add_string
b "Items"
1361 | Outline _
-> Buffer.add_string
b "Outline"
1367 let color_of_string s =
1368 Scanf.sscanf
s "%d/%d/%d" (fun r g
b ->
1369 (float r /. 256.0, float g
/. 256.0, float b /. 256.0)
1373 let color_to_string (r, g
, b) =
1374 let r = truncate
(r *. 256.0)
1375 and g
= truncate
(g
*. 256.0)
1376 and b = truncate
(b *. 256.0) in
1377 Printf.sprintf
"%d/%d/%d" r g
b
1380 let enterinfomode () =
1381 let btos = function true -> "on" | _
-> "off" in
1382 let mode = state.mode in
1383 let rec makeitems () =
1384 let intp name get set
=
1385 Printf.sprintf
"%s\t%d" name
(get
()), 1, Action
(
1386 fun active first _ pan
->
1391 state.text <- Printf.sprintf
"bad integer `%s': %s"
1392 s (Printexc.to_string exn
);
1395 if n != max_int
then set
n;
1397 let te = name ^
": ", "", None
, intentry, ondone in
1402 state.mode <- Items
(active, first
, makeitems (), "", pan
, mode)
1405 and boolp name get set
=
1406 Printf.sprintf
"%s\t%s" name
(btos (get
())), 1, Action
(
1407 fun active first qsearch pan
->
1410 Items
(active, first
, makeitems (), qsearch
, pan
, mode);
1412 and colorp name get set
=
1413 Printf.sprintf
"%s\t%s" name
(color_to_string (get
())), 1, Action
(
1414 fun active first _ pan
->
1415 let invalid = (nan
, nan
, nan
) in
1418 try color_of_string s
1420 state.text <- Printf.sprintf
"bad color `%s': %s"
1421 s (Printexc.to_string exn
);
1427 let te = name ^
": ", "", None
, textentry, ondone in
1432 state.mode <- Items
(active, first
, makeitems (), "", pan
, mode)
1437 let birdseye = isbirdseye mode in
1439 (if birdseye then "Setup bird's eye" else "Setup"), 0, Noaction
;
1441 boolp
"presentation"
1442 (fun () -> conf.presentation)
1444 conf.presentation <- v;
1445 state.anchor <- getanchor ();
1448 boolp
"ignore case in searches"
1449 (fun () -> conf.icase
)
1450 (fun v -> conf.icase
<- v);
1453 (fun () -> conf.preload)
1454 (fun v -> conf.preload <- v);
1457 (fun () -> conf.verbose
)
1458 (fun v -> conf.verbose
<- v);
1461 (fun () -> conf.maxhfit
)
1462 (fun v -> conf.maxhfit
<- v);
1465 (fun () -> conf.crophack
)
1466 (fun v -> conf.crophack
<- v);
1469 (fun () -> conf.showall
)
1470 (fun v -> conf.showall
<- v);
1472 boolp
"highlight links"
1473 (fun () -> conf.hlinks
)
1474 (fun v -> conf.hlinks
<- v);
1477 (fun () -> conf.underinfo
)
1478 (fun v -> conf.underinfo
<- v);
1479 boolp
"persistent bookmarks"
1480 (fun () -> conf.savebmarks
)
1481 (fun v -> conf.savebmarks
<- v);
1483 boolp
"proportional display"
1484 (fun () -> conf.proportional
)
1485 (fun v -> reinit conf.angle
v);
1487 boolp
"persistent location"
1488 (fun () -> conf.jumpback
)
1489 (fun v -> conf.jumpback
<- v);
1493 intp "vertical margin"
1494 (fun () -> conf.interpagespace)
1496 conf.interpagespace <- n;
1498 match state.layout with
1503 state.maxy <- calcheight ();
1504 let y = getpagey pageno in
1509 (fun () -> conf.pagebias
)
1510 (fun v -> conf.pagebias
<- v);
1513 (fun () -> conf.scrollstep
)
1514 (fun n -> conf.scrollstep
<- n);
1516 intp "auto scroll step"
1518 match state.autoscroll
with
1520 | _
-> conf.autoscrollstep
)
1522 if state.autoscroll
<> None
1523 then state.autoscroll
<- Some
n;
1524 conf.autoscrollstep
<- n);
1527 (fun () -> truncate
(conf.zoom *. 100.))
1528 (fun v -> setzoom ((float v) /. 100.));
1531 (fun () -> conf.angle
)
1532 (fun v -> reinit v conf.proportional
);
1534 intp "scroll bar width"
1535 (fun () -> state.scrollw
)
1539 reshape conf.winw
conf.winh
;
1542 intp "scroll handle height"
1543 (fun () -> conf.scrollh
)
1544 (fun v -> conf.scrollh
<- v;);
1546 intp "thumbnail width"
1547 (fun () -> conf.thumbw
)
1549 conf.thumbw
<- min
1920 v;
1552 leavebirdseye beye
false;
1557 colorp
"background color"
1558 (fun () -> conf.bgcolor
)
1559 (fun v -> conf.bgcolor
<- v);
1562 "Presentation mode", 0, Noaction
;
1564 boolp
"scrollbar visible"
1565 (fun () -> conf.scrollbarinpm
)
1567 if v != conf.scrollbarinpm
1569 conf.scrollbarinpm
<- v;
1570 if conf.presentation
1572 state.scrollw
<- if v then conf.scrollbw
else 0;
1573 reshape conf.winw
conf.winh
;
1579 "Pixmap Cache", 0, Noaction
;
1581 intp "size (advisory)"
1582 (fun () -> conf.memlimit
)
1583 (fun v -> conf.memlimit
<- v);
1584 Printf.sprintf
"%s\t%d" "used" state.memused
, 1, Noaction
;
1591 :: ("Document", 0, Noaction
)
1592 :: List.map
(fun (_
, s) -> (s, 1, Noaction
)) state.docinfo
1598 :: (Printf.sprintf
"Save these parameters as defaults at exit (%s)"
1599 (btos conf.bedefault
),
1602 fun active first qsearch pan
->
1603 conf.bedefault
<- not
conf.bedefault
;
1604 Items
(active, first
, makeitems (), qsearch
, pan
, mode);
1609 Array.of_list
(items @ tailer)
1612 state.mode <- Items
(1, 0, makeitems (), "", 0, mode);
1613 Glut.postRedisplay
();
1616 let enterhelpmode () =
1617 state.mode <- Items
(-1, 0, state.help
, "", 0, state.mode);
1618 Glut.postRedisplay
();
1621 let quickbookmark ?title
() =
1622 match state.layout with
1628 let sec = Unix.gettimeofday
() in
1629 let tm = Unix.localtime
sec in
1630 Printf.sprintf
"Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1634 (tm.Unix.tm_year
+ 1900)
1637 | Some
title -> title
1640 (title, 0, (l
.pageno, float l
.pagey /. float l
.pageh
))
1645 state.fullscreen
<- None
;
1646 Glut.reshapeWindow
w h;
1649 let writeopen path password
=
1650 writecmd state.csock
("open " ^ path ^
"\000" ^ password ^
"\000");
1653 let opendoc path password
=
1656 state.password
<- password
;
1657 state.gen
<- state.gen
+ 1;
1658 state.docinfo
<- [];
1660 writeopen path password
;
1661 Glut.setWindowTitle
("llpp " ^
Filename.basename path
);
1662 wcmd "geometry" [`i
state.w; `i
conf.winh
];
1665 let viewkeyboard key
=
1667 let mode = state.mode in
1668 state.mode <- Textentry
(te, fun _
-> state.mode <- mode);
1671 Glut.postRedisplay
()
1673 let c = Char.chr key
in
1675 | '
\027'
| 'q'
-> (* escape *)
1678 | '
\008'
-> (* backspace *)
1679 let y = getnav ~
-1 in
1680 gotoy_and_clear_text y
1688 Glut.postRedisplay
()
1691 let ondone isforw
s =
1692 cbput state.hists
.pat
s;
1693 state.searchpattern
<- s;
1696 let s = String.create
1 in
1698 enttext (s, "", Some
(onhist state.hists
.pat
),
1699 textentry, ondone (c ='
/'
))
1701 | '
+'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1702 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1703 setzoom (conf.zoom +. incr)
1708 try int_of_string
s with exc
->
1709 state.text <- Printf.sprintf
"bad integer `%s': %s"
1710 s (Printexc.to_string exc
);
1716 state.text <- "page bias is now " ^ string_of_int
n;
1719 enttext ("page bias: ", "", None
, intentry, ondone)
1721 | '
-'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1722 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1723 setzoom (max
0.01 (conf.zoom -. decr))
1726 let ondone msg
= state.text <- msg
in
1728 "option [acfhilpstvAPRSZ]: ", "", None
,
1729 optentry state.mode, ondone
1732 | '
0'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1735 | '
1'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1736 let zoom = zoomforh
conf.winw
conf.winh
state.scrollw
in
1740 | '
9'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1746 try int_of_string
s with exc
->
1747 state.text <- Printf.sprintf
"bad integer `%s': %s"
1748 s (Printexc.to_string exc
);
1754 cbput state.hists
.pag
(string_of_int
n);
1755 gotoy_and_clear_text (getpagey (n + conf.pagebias
- 1))
1758 let pageentry text key
=
1759 match Char.unsafe_chr key
with
1760 | 'g'
-> TEdone
text
1761 | _
-> intentry text key
1763 let text = "x" in text.[0] <- c;
1764 enttext (":", text, Some
(onhist state.hists
.pag
), pageentry, ondone)
1767 state.scrollw
<- if state.scrollw
> 0 then 0 else conf.scrollbw
;
1768 reshape conf.winw
conf.winh
;
1771 conf.hlinks
<- not
conf.hlinks
;
1772 state.text <- "highlightlinks " ^
if conf.hlinks
then "on" else "off";
1773 Glut.postRedisplay
()
1776 begin match state.autoscroll
with
1778 conf.autoscrollstep
<- step
;
1779 state.autoscroll
<- None
1781 if conf.autoscrollstep
= 0
1782 then state.autoscroll
<- Some
1
1783 else state.autoscroll
<- Some
conf.autoscrollstep
1787 conf.presentation <- not
conf.presentation;
1788 if conf.presentation
1790 if not
conf.scrollbarinpm
1791 then state.scrollw
<- 0;
1794 state.scrollw
<- conf.scrollbw
;
1796 showtext ' '
("presentation mode " ^
1797 if conf.presentation then "on" else "off");
1798 state.anchor <- getanchor ();
1802 begin match state.fullscreen
with
1804 state.fullscreen
<- Some
(conf.winw
, conf.winh
);
1807 state.fullscreen
<- None
;
1812 gotoy_and_clear_text 0
1815 gotopage1 (state.pagecount
- 1) 0
1818 search state.searchpattern
true
1821 search state.searchpattern
false
1824 begin match state.layout with
1827 gotoy_and_clear_text (getpagey l
.pageno)
1831 begin match List.rev
state.layout with
1834 let pageno = min
(l
.pageno+1) (state.pagecount
-1) in
1835 gotoy_and_clear_text (getpagey pageno)
1838 | '
\127'
-> (* del *)
1839 begin match state.layout with
1842 let pageno = max
0 (l
.pageno-1) in
1843 gotoy_and_clear_text (getpagey pageno)
1848 if fn
= -1 then l
.pageno, l
.pageno else fn
, l
.pageno
1850 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
1852 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
1856 else (100. *. (float state.y /. float maxy)) in
1859 Printf.sprintf
"Page %d of %d %.2f%%"
1860 (fn+1) state.pagecount
percent
1863 "Pages %d-%d of %d %.2f%%"
1864 (fn+1) (ln
+1) state.pagecount
percent
1869 begin match state.layout with
1872 doreshape (l
.pagew
+ state.scrollw
) l
.pageh
;
1873 Glut.postRedisplay
();
1877 enterbookmarkmode ()
1887 match state.layout with
1890 (s, 0, (l
.pageno, float l
.pagey /. float l
.pageh
))
1894 enttext ("bookmark: ", "", None
, textentry, ondone)
1898 showtext ' '
"Quick bookmark added";
1901 begin match state.layout with
1903 let rect = getpdimrect l
.pagedimno
in
1907 (truncate
(1.8 *. (rect.(1) -. rect.(0))),
1908 truncate
(1.2 *. (rect.(3) -. rect.(0))))
1910 (truncate
(rect.(1) -. rect.(0)),
1911 truncate
(rect.(3) -. rect.(0)))
1915 state.anchor <- getanchor ();
1916 doreshape (w + state.scrollw
) (h + conf.interpagespace)
1918 Glut.postRedisplay
();
1923 | '
\000'
-> (* ctrl-2 *)
1924 let maxw = getmaxw
() in
1926 then setzoom (maxw /. float conf.winw
)
1929 reinit (conf.angle
+ (if c = '
>'
then 30 else -30)) conf.proportional
1934 (min
(state.colorscale
+. (if c = '
]'
then 0.1 else -0.1)) 1.0);
1935 Glut.postRedisplay
()
1938 begin match state.mode with
1939 | Birdseye beye
-> upbirdseye beye
1940 | _
-> gotoy (clamp (-conf.scrollstep
))
1944 begin match state.mode with
1945 | Birdseye beye
-> downbirdseye beye
1946 | _
-> gotoy (clamp conf.scrollstep
)
1950 state.anchor <- getanchor ();
1951 opendoc state.path
state.password
1954 vlog "huh? %d %c" key
(Char.chr key
);
1957 let textentrykeyboard key
((c, text, opthist
, onkey
, ondone), onleave
) =
1959 state.mode <- Textentry
(te, onleave
);
1962 Glut.postRedisplay
()
1964 match Char.unsafe_chr key
with
1965 | '
\008'
-> (* backspace *)
1966 let len = String.length
text in
1970 Glut.postRedisplay
();
1973 let s = String.sub
text 0 (len - 1) in
1974 enttext (c, s, opthist
, onkey
, ondone)
1980 Glut.postRedisplay
()
1982 | '
\007'
(* ctrl-g *)
1983 | '
\027'
-> (* escape *)
1984 begin match opthist
with
1986 | Some
(_
, onhistcancel
) -> onhistcancel
()
1989 Glut.postRedisplay
()
1992 begin match onkey
text key
with
1996 Glut.postRedisplay
()
1999 enttext (c, text, opthist
, onkey
, ondone);
2003 Glut.postRedisplay
()
2006 state.mode <- Textentry
(te, onleave
);
2007 Glut.postRedisplay
()
2011 let birdseyekeyboard key
((_
, _
, pageno, _
, _
) as beye
) =
2013 | 27 -> (* escape *)
2014 leavebirdseye beye
true
2016 | 12 -> (* ctrl-l *)
2017 let y, h = getpageyh pageno in
2018 let top = (conf.winh
- h) / 2 in
2019 gotoy (max
0 (y - top))
2022 leavebirdseye beye
false
2028 let itemskeyboard key
(active, first
, items, qsearch
, pan
, oldmode
) =
2029 let set active first qsearch
=
2030 state.mode <- Items
(active, first
, items, qsearch
, pan
, oldmode
)
2032 let search active pattern
incr =
2035 if n >= Array.length
items || n < 0
2038 let (s, _
, _
) = items.(n) in
2040 (try ignore
(Str.search_forward re
s 0); true
2041 with Not_found
-> false)
2043 else loop (n + incr)
2048 let re = Str.regexp_case_fold pattern
in
2054 let firstof active = max
0 (active - maxoutlinerows () / 2) in
2056 | 18 | 19 -> (* ctrl-r/ctlr-s *)
2057 let incr = if key
= 18 then -1 else 1 in
2059 match search (active + incr) qsearch
incr with
2061 state.text <- qsearch ^
" [not found]";
2064 state.text <- qsearch
;
2065 active, firstof active
2067 set active first qsearch
;
2068 Glut.postRedisplay
();
2070 | 8 -> (* backspace *)
2071 let len = String.length qsearch
in
2078 set active first
"";
2081 let qsearch = String.sub
qsearch 0 (len - 1) in
2083 match search active qsearch ~
-1 with
2085 state.text <- qsearch ^
" [not found]";
2088 state.text <- qsearch;
2089 active, firstof active
2091 set active first
qsearch
2093 Glut.postRedisplay
()
2095 | _
when key
>= 32 && key
< 127 ->
2096 let pattern = addchar qsearch (Char.chr key
) in
2098 match search active pattern 1 with
2100 state.text <- pattern ^
" [not found]";
2103 state.text <- pattern;
2104 active, firstof active
2106 set active first
pattern;
2107 Glut.postRedisplay
()
2109 | 27 -> (* escape *)
2111 if String.length
qsearch = 0
2113 state.mode <- oldmode
;
2116 set active first
"";
2118 Glut.postRedisplay
()
2121 if active >= 0 && active < Array.length
items
2123 match items.(active) with
2125 state.mode <- f active first
qsearch pan
2129 state.mode <- oldmode
2133 state.mode <- oldmode
2135 Glut.postRedisplay
();
2137 | _
-> dolog "unknown key %d" key
2140 let outlinekeyboard key
2141 (allowdel
, active, first
, outlines, qsearch, pan
, oldmode
) =
2142 let narrow outlines pattern =
2143 let reopt = try Some
(Str.regexp_case_fold
pattern) with _
-> None
in
2147 let rec fold accu n =
2151 let (s, _
, _
) as o
= outlines.(n) in
2153 if (try ignore
(Str.search_forward
re s 0); true
2154 with Not_found
-> false)
2160 let matched = fold [] (Array.length
outlines - 1) in
2161 if matched = [] then None
else Some
(Array.of_list
matched)
2163 let search active pattern incr =
2166 if n = Array.length
outlines || n = -1
2169 let (s, _
, _
) = outlines.(n) in
2171 (try ignore
(Str.search_forward
re s 0); true
2172 with Not_found
-> false)
2174 else loop (n + incr)
2179 let re = Str.regexp_case_fold
pattern in
2185 let firstof active = max
0 (active - maxoutlinerows () / 2) in
2187 | 27 -> (* escape *)
2189 if String.length
qsearch = 0
2191 state.mode <- oldmode
;
2194 state.mode <- Outline
(
2195 allowdel
, active, first
, outlines, "", pan
, oldmode
2198 Glut.postRedisplay
();
2200 | 18 | 19 -> (* ctrl-r/ctrl-s *)
2201 let incr = if key
= 18 then -1 else 1 in
2203 match search (active + incr) qsearch incr with
2205 state.text <- qsearch ^
" [not found]";
2208 state.text <- qsearch;
2209 active, firstof active
2211 state.mode <- Outline
(
2212 allowdel
, active, first
, outlines, qsearch, pan
, oldmode
2214 Glut.postRedisplay
();
2216 | 8 -> (* backspace *)
2217 let len = String.length
qsearch in
2224 state.mode <- Outline
(
2225 allowdel
, active, first
, outlines, "", pan
, oldmode
2229 let qsearch = String.sub
qsearch 0 (len - 1) in
2231 match search active qsearch ~
-1 with
2233 state.text <- qsearch ^
" [not found]";
2236 state.text <- qsearch;
2237 active, firstof active
2239 state.mode <- Outline
(
2240 allowdel
, active, first
, outlines, qsearch, pan
, oldmode
2243 Glut.postRedisplay
()
2246 if active < Array.length
outlines
2248 let (_
, _
, anchor) = outlines.(active) in
2253 if allowdel
then state.bookmarks <- Array.to_list
outlines;
2254 state.mode <- oldmode
;
2255 Glut.postRedisplay
();
2257 | _
when key
>= 32 && key
< 127 ->
2258 let pattern = addchar qsearch (Char.chr key
) in
2260 match search active pattern 1 with
2262 state.text <- pattern ^
" [not found]";
2265 state.text <- pattern;
2266 active, firstof active
2268 state.mode <- Outline
(
2269 allowdel
, active, first
, outlines, pattern, pan
, oldmode
2271 Glut.postRedisplay
()
2273 | 14 when not allowdel
-> (* ctrl-n *)
2274 if String.length
qsearch > 0
2276 let optoutlines = narrow outlines qsearch in
2277 begin match optoutlines with
2278 | None
-> state.text <- "can't narrow"
2280 state.mode <- Outline
(
2281 allowdel
, 0, 0, outlines, qsearch, pan
, oldmode
2283 match state.outlines with
2286 state.outlines <- Onarrow
(qsearch, outlines, a)
2287 | Onarrow
(_
, _
, b) ->
2288 state.outlines <- Onarrow
(qsearch, outlines, b)
2291 Glut.postRedisplay
()
2293 | 21 when not allowdel
-> (* ctrl-u *)
2295 match state.outlines with
2298 let a = Array.of_list
(List.rev l
) in
2299 state.outlines <- Oarray
a;
2301 | Onarrow
(_
, _
, b) ->
2302 state.outlines <- Oarray
b;
2306 state.mode <- Outline
(allowdel
, 0, 0, outline, qsearch, pan
, oldmode
);
2307 Glut.postRedisplay
()
2309 | 12 -> (* ctrl-l *)
2310 state.mode <- Outline
2311 (allowdel
, active, firstof active, outlines, qsearch, pan
, oldmode
);
2312 Glut.postRedisplay
()
2314 | 127 when allowdel
-> (* delete *)
2315 let len = Array.length
outlines - 1 in
2319 state.bookmarks <- [];
2322 let bookmarks = Array.init
len
2324 let i = if i >= active then i + 1 else i in
2338 Glut.postRedisplay
()
2340 | _
-> dolog "unknown key %d" key
2343 let keyboard ~key ~x ~
y =
2346 if key
= 7 && not
(istextentry state.mode) (* ctrl-g *)
2350 match state.mode with
2351 | Outline
outline -> outlinekeyboard key
outline
2352 | Textentry
textentry -> textentrykeyboard key
textentry
2353 | Birdseye
birdseye -> birdseyekeyboard key
birdseye
2354 | View
-> viewkeyboard key
2355 | Items
items -> itemskeyboard key
items
2358 let birdseyespecial key
((conf, leftx
, _
, hooverpageno
, anchor) as beye
) =
2360 | Glut.KEY_UP
-> upbirdseye beye
2361 | Glut.KEY_DOWN
-> downbirdseye beye
2363 | Glut.KEY_PAGE_UP
->
2364 begin match state.layout with
2368 state.mode <- Birdseye
(
2369 conf, leftx
, l
.pageno, hooverpageno
, anchor
2371 gotopage1 l
.pageno 0;
2374 let layout = layout (state.y-conf.winh
) conf.winh
in
2376 | [] -> gotoy (clamp (-conf.winh
))
2378 state.mode <- Birdseye
(
2379 conf, leftx
, l
.pageno, hooverpageno
, anchor
2381 gotopage1 l
.pageno 0
2384 | [] -> gotoy (clamp (-conf.winh
))
2387 | Glut.KEY_PAGE_DOWN
->
2388 begin match List.rev
state.layout with
2390 let layout = layout (state.y + conf.winh
) conf.winh
in
2391 begin match layout with
2393 let incr = l
.pageh
- l
.pagevh in
2398 conf, leftx
, state.pagecount
- 1, hooverpageno
, anchor
2400 Glut.postRedisplay
();
2402 else gotoy (clamp (incr + conf.interpagespace*2));
2406 Birdseye
(conf, leftx
, l
.pageno, hooverpageno
, anchor);
2407 gotopage1 l
.pageno 0;
2410 | [] -> gotoy (clamp conf.winh
)
2414 state.mode <- Birdseye
(conf, leftx
, 0, hooverpageno
, anchor);
2418 let pageno = state.pagecount
- 1 in
2419 state.mode <- Birdseye
(conf, leftx
, pageno, hooverpageno
, anchor);
2420 if not
(pagevisible state.layout pageno)
2423 match List.rev
state.pdims
with
2425 | (_
, _
, h, _
) :: _
-> h
2427 gotoy (max
0 (getpagey pageno - (conf.winh
- h - conf.interpagespace)))
2428 else Glut.postRedisplay
();
2432 let setautoscrollspeed step goingdown
=
2433 let incr = max
1 ((abs step
) / 2) in
2434 let incr = if goingdown
then incr else -incr in
2435 let astep = step
+ incr in
2436 state.autoscroll
<- Some
astep;
2439 let special ~key ~x ~
y =
2442 match state.mode with
2443 | View
| (Birdseye _
) when key
= Glut.KEY_F9
->
2447 birdseyespecial key vals
2449 | View
when key
= Glut.KEY_F1
->
2453 begin match state.autoscroll
with
2454 | Some step
when key
= Glut.KEY_DOWN
|| key
= Glut.KEY_UP
->
2455 setautoscrollspeed step
(key
= Glut.KEY_DOWN
)
2460 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
2461 | Glut.KEY_UP
-> clamp (-conf.scrollstep
)
2462 | Glut.KEY_DOWN
-> clamp conf.scrollstep
2463 | Glut.KEY_PAGE_UP
->
2464 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2466 match state.layout with
2468 | l
:: _
-> state.y - l
.pagey
2471 | Glut.KEY_PAGE_DOWN
->
2472 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2474 match List.rev
state.layout with
2476 | l
:: _
-> getpagey l
.pageno
2479 | Glut.KEY_HOME
-> addnav (); 0
2482 state.maxy - (if conf.maxhfit
then conf.winh
else 0)
2484 | (Glut.KEY_RIGHT
| Glut.KEY_LEFT
) when
2485 Glut.getModifiers
() land Glut.active_alt
!= 0 ->
2486 getnav (if key
= Glut.KEY_LEFT
then 1 else -1)
2488 | Glut.KEY_RIGHT
when conf.zoom > 1.0 ->
2489 state.x
<- state.x
- 10;
2491 | Glut.KEY_LEFT
when conf.zoom > 1.0 ->
2492 state.x
<- state.x
+ 10;
2497 gotoy_and_clear_text y
2501 ((c, _
, (Some
(action, _
) as onhist), onkey
, ondone), mode) ->
2504 | Glut.KEY_UP
-> action HCprev
2505 | Glut.KEY_DOWN
-> action HCnext
2506 | Glut.KEY_HOME
-> action HCfirst
2507 | Glut.KEY_END
-> action HClast
2510 state.mode <- Textentry
((c, s, onhist, onkey
, ondone), mode);
2511 Glut.postRedisplay
()
2515 | Items
(active, first
, items, qsearch, pan
, oldmode
) ->
2516 let maxrows = maxoutlinerows () in
2517 let itemcount = Array.length
items in
2518 let hasaction = function
2519 | (_
, _
, Noaction
) -> false
2522 let find start
incr =
2524 if i = -1 || i = itemcount
2527 if hasaction items.(i)
2529 else find (i + incr)
2534 let set active first
=
2535 let first = max
0 (min
first (itemcount - maxrows)) in
2536 state.mode <- Items
(active, first, items, qsearch, pan
, oldmode
)
2539 let isvisible first n = n >= first && n - first <= maxrows in
2541 let incr1 = if incr > 0 then 1 else -1 in
2542 if isvisible first active
2545 let next = active + incr in
2547 if next < 0 || next >= itemcount
2549 else find next incr1
2551 if next = -1 || abs
(active - next) > maxrows
2557 let first = first + incr in
2558 let first = max
0 (min
first (itemcount - 1)) in
2560 let next = active + incr in
2561 let next = max
0 (min
next (itemcount - 1)) in
2564 let active = if next = -1 then active else next in
2567 let first = min
next first in
2570 let first = first + incr in
2571 let first = max
0 (min
first (itemcount - 1)) in
2573 let next = active + incr in
2574 let next = max
0 (min
next (itemcount - 1)) in
2575 let next = find next incr1 in
2576 if next = -1 || abs
(active - first) > maxrows
2583 Glut.postRedisplay
()
2585 begin match key
with
2586 | Glut.KEY_UP
-> navigate ~
-1
2587 | Glut.KEY_DOWN
-> navigate 1
2588 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
2589 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
2592 state.mode <- Items
(
2593 active, first, items, qsearch, min
0 (pan
- 1), oldmode
2595 Glut.postRedisplay
()
2598 state.mode <- Items
(
2599 active, first, items, qsearch, min
0 (pan
+ 1), oldmode
2601 Glut.postRedisplay
()
2604 let active = find 0 1 in
2606 Glut.postRedisplay
()
2609 let first = max
0 (itemcount - maxrows) in
2610 let active = find (itemcount - 1) ~
-1 in
2612 Glut.postRedisplay
()
2617 | Outline
(allowdel
, active, first, outlines, qsearch, pan
, oldmode
) ->
2618 let maxrows = maxoutlinerows () in
2619 let calcfirst first active =
2622 let rows = active - first in
2623 if rows > maxrows then active - maxrows else first
2627 let active = active + incr in
2628 let active = max
0 (min
active (Array.length
outlines - 1)) in
2629 let first = calcfirst first active in
2630 state.mode <- Outline
(
2631 allowdel
, active, first, outlines, qsearch, pan
, oldmode
2633 Glut.postRedisplay
()
2635 let updownlevel incr =
2636 let len = Array.length
outlines in
2637 let (_
, curlevel
, _
) = outlines.(active) in
2639 if i = len then i-1 else if i = -1 then 0 else
2640 let (_
, l
, _
) = outlines.(i) in
2641 if l
!= curlevel
then i else flow (i+incr)
2643 let active = flow active in
2644 let first = calcfirst first active in
2645 state.mode <- Outline
(
2646 allowdel
, active, first, outlines, qsearch, pan
, oldmode
2648 Glut.postRedisplay
()
2651 | Glut.KEY_UP
-> navigate ~
-1
2652 | Glut.KEY_DOWN
-> navigate 1
2653 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
2654 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
2657 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2659 state.mode <- Outline
(
2660 allowdel
, active, first, outlines,
2661 qsearch, min
0 (pan
+ 1), oldmode
2663 Glut.postRedisplay
();
2671 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2673 state.mode <- Outline
(
2674 allowdel
, active, first, outlines, qsearch, pan
- 1, oldmode
2676 Glut.postRedisplay
();
2680 then updownlevel ~
-1
2684 state.mode <- Outline
(
2685 allowdel
, 0, 0, outlines, qsearch, pan
, oldmode
2687 Glut.postRedisplay
()
2690 let active = Array.length
outlines - 1 in
2691 let first = max
0 (active - maxrows) in
2692 state.mode <- Outline
(
2693 allowdel
, active, first, outlines, qsearch, pan
, oldmode
2695 Glut.postRedisplay
()
2700 let drawplaceholder l
=
2701 GlDraw.color
(1.0, 1.0, 1.0);
2702 let margin = state.x
+ (conf.winw
- (state.w + state.scrollw
)) / 2 in
2704 (float l
.pagex
, float l
.pagedispy
)
2705 (float (l
.pagew
+ l
.pagex
), float (l
.pagedispy
+ l
.pagevh))
2707 let x = if margin < 0 then -margin else l
.pagex
2708 and y = l
.pagedispy
+ 13 in
2709 GlDraw.color
(0.0, 0.0, 0.0);
2710 drawstring 13 x y ("Loading " ^ string_of_int
(l
.pageno + 1))
2713 let now () = Unix.gettimeofday
();;
2717 match state.mode with
2718 | Textentry _
-> scalecolor 0.4
2719 | View
| Outline _
| Items _
-> scalecolor 1.0
2720 | Birdseye
(_
, _
, pageno, hooverpageno
, _
) ->
2721 if l
.pageno = hooverpageno
2724 if l
.pageno = pageno
2730 begin match getopaque l
.pageno with
2731 | Some
(opaque, _
) when validopaque opaque ->
2733 draw
(l
.pagedispy
, l
.pagevh, l
.pagey, conf.hlinks
) opaque;
2736 vlog "draw %d %f sec" l
.pageno d;
2744 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
2745 let sh = (float (maxy + conf.winh
) /. float conf.winh
) in
2746 let sh = float conf.winh
/. sh in
2747 let sh = max
sh (float conf.scrollh
) in
2750 if state.y = state.maxy
2752 else float y /. float maxy
2754 let position = (float conf.winh
-. sh) *. percent in
2757 if position +. sh > float conf.winh
2758 then float conf.winh
-. sh
2764 let scrollindicator () =
2765 GlDraw.color (0.64 , 0.64, 0.64);
2767 (float (conf.winw
- state.scrollw
), 0.)
2768 (float conf.winw
, float conf.winh
)
2770 GlDraw.color (0.0, 0.0, 0.0);
2772 let position, sh = scrollph state.y in
2774 (float (conf.winw
- state.scrollw
), position)
2775 (float conf.winw
, position +. sh)
2779 let showsel margin =
2780 match state.mstate
with
2781 | Mnone
| Mscroll _
| Mpan _
| Mzoom _
->
2784 | Msel
((x0
, y0
), (x1
, y1
)) ->
2785 let rec loop = function
2787 if (y0
>= l
.pagedispy
&& y0
<= (l
.pagedispy
+ l
.pagevh))
2788 || ((y1
>= l
.pagedispy
&& y1
<= (l
.pagedispy
+ l
.pagevh)))
2790 match getopaque l
.pageno with
2791 | Some
(opaque, _
) when validopaque opaque ->
2792 let oy = -l
.pagey + l
.pagedispy
in
2794 (x0
- margin - state.x, y0
,
2795 x1
- margin - state.x, y1
) oy;
2805 let panx = float state.x in
2807 GlDraw.color (0.0, 0.0, 1.0) ~alpha
:0.5;
2808 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2810 (fun (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) ->
2812 if l
.pageno = pageno
2814 let d = float (l
.pagedispy
- l
.pagey) in
2815 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
2816 GlDraw.begins `quads
;
2818 GlDraw.vertex2
(x0
+.panx, y0
+.d);
2819 GlDraw.vertex2
(x1
+.panx, y1
+.d);
2820 GlDraw.vertex2
(x2
+.panx, y2
+.d);
2821 GlDraw.vertex2
(x3
+.panx, y3
+.d);
2831 let showstrings trusted
active first pan
strings =
2833 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2834 GlDraw.color (0., 0., 0.) ~alpha
:0.85;
2835 GlDraw.rect (0., 0.) (float conf.winw
, float conf.winh
);
2836 GlDraw.color (1., 1., 1.);
2837 Gl.enable `texture_2d
;
2839 let wx = measurestr
15 "w" in
2840 let tabx = 30.0*.wx +. float (pan
*15) in
2842 if row
= Array.length
strings || (row
- first) * 16 > conf.winh
2845 let (s, level
, _
) = strings.(row
) in
2846 let y = (row
- first) * 16 in
2847 let x = 5 + 15*(max
0 (level
+pan
)) in
2850 Gl.disable `texture_2d
;
2851 GlDraw.polygon_mode `both `line
;
2852 GlDraw.color (1., 1., 1.) ~alpha
:0.9;
2853 GlDraw.rect (1., float (y + 1))
2854 (float (conf.winw
- 1), float (y + 18));
2855 GlDraw.polygon_mode `both `fill
;
2856 GlDraw.color (1., 1., 1.);
2857 Gl.enable `texture_2d
;
2860 let drawtabularstring x s =
2864 let tabpos = try String.index
s '
\t'
with Not_found
-> -1 in
2867 let len = String.length
s - tabpos - 1 in
2868 let s1 = String.sub
s 0 tabpos
2869 and s2
= String.sub
s (tabpos + 1) len in
2870 let xx = wx +. drawstring1 14 x (y + 16) s1 in
2871 let x = truncate
(max
xx tabx) in
2872 drawstring1 15 x (y + 16) s2
2874 drawstring1 15 x (y + 16) s
2876 drawstring1 15 x (y + 16) s
2880 drawtabularstring (x + pan
*15) s;
2886 Gl.disable `texture_2d
;
2889 let showoutline (_, active, first, outlines, _, pan
, _) =
2890 showstrings false active first pan
outlines;
2893 let showitems (active, first, items, _, pan
, _) =
2894 showstrings true active first pan
items;
2898 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
2899 GlDraw.viewport
margin 0 state.w conf.winh
;
2901 GlClear.color (scalecolor2 conf.bgcolor
);
2902 GlClear.clear
[`
color];
2905 Gl.enable `scissor_test
;
2906 GlMisc.scissor
0 0 (conf.winw
- state.scrollw
) conf.winh
;
2908 List.iter
drawpage state.layout;
2911 Gl.disable `scissor_test
2915 let x = -.float state.x in
2916 GlMat.translate ~
x ();
2920 GlDraw.viewport
0 0 conf.winw
conf.winh
;
2923 begin match state.mode with
2924 | Items
items -> showitems items
2925 | Outline
outline -> showoutline outline
2929 Glut.swapBuffers
();
2933 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
2934 let x = x - margin - state.x in
2935 let rec f = function
2937 begin match getopaque l
.pageno with
2938 | Some
(opaque, _) when validopaque opaque ->
2939 let y = y - l
.pagedispy
in
2942 let y = l
.pagey + y in
2943 let x = x - l
.pagex
in
2944 match whatsunder
opaque x y with
2957 let viewmouse button bstate
x y =
2959 | Glut.OTHER_BUTTON
n when (n == 3 || n == 4) && bstate
= Glut.UP
->
2960 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2962 match state.mstate
with
2963 | Mzoom
(oldn
, i) ->
2971 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
2973 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
2975 let zoom = conf.zoom +. incr in
2977 state.mstate
<- Mzoom
(n, 0);
2979 state.mstate
<- Mzoom
(n, i+1);
2981 else state.mstate
<- Mzoom
(n, 0)
2983 | _ -> state.mstate
<- Mzoom
(n, 0)
2986 match state.autoscroll
with
2987 | Some step
-> setautoscrollspeed step
(n=4)
2991 then -conf.scrollstep
2992 else conf.scrollstep
2994 let incr = incr * 2 in
2995 let y = clamp incr in
2996 gotoy_and_clear_text y
2999 | Glut.LEFT_BUTTON
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
3000 if bstate
= Glut.DOWN
3002 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
3003 state.mstate
<- Mpan
(x, y)
3006 state.mstate
<- Mnone
3008 | Glut.LEFT_BUTTON
when x > conf.winw
- state.scrollw
->
3009 if bstate
= Glut.DOWN
3011 let position, sh = scrollph state.y in
3012 if y > truncate
position && y < truncate
(position +. sh)
3014 state.mstate
<- Mscroll
3016 let percent = float y /. float conf.winh
in
3017 let desty = truncate
(float (state.maxy - conf.winh
) *. percent) in
3019 state.mstate
<- Mscroll
3021 state.mstate
<- Mnone
3023 | Glut.LEFT_BUTTON
->
3024 let dest = if bstate
= Glut.DOWN
then getunder x y else Unone
in
3025 begin match dest with
3026 | Ulinkgoto
(pageno, top) ->
3030 gotopage1 pageno top;
3036 | Unone
when bstate
= Glut.DOWN
->
3037 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
3038 state.mstate
<- Mpan
(x, y);
3040 | Unone
| Utext
_ ->
3041 if bstate
= Glut.DOWN
3043 if conf.angle
mod 360 = 0
3045 state.mstate
<- Msel
((x, y), (x, y));
3046 Glut.postRedisplay
()
3050 match state.mstate
with
3053 | Mzoom
_ | Mscroll
->
3054 state.mstate
<- Mnone
3057 Glut.setCursor
Glut.CURSOR_INHERIT
;
3058 state.mstate
<- Mnone
3060 | Msel
((_, y0
), (_, y1
)) ->
3062 if (y0
>= l
.pagedispy
&& y0
<= (l
.pagedispy
+ l
.pagevh))
3063 || ((y1
>= l
.pagedispy
&& y1
<= (l
.pagedispy
+ l
.pagevh)))
3065 match getopaque l
.pageno with
3066 | Some
(opaque, _) when validopaque opaque ->
3070 List.iter
f state.layout;
3071 copysel
""; (* ugly *)
3072 Glut.setCursor
Glut.CURSOR_INHERIT
;
3073 state.mstate
<- Mnone
;
3080 let birdseyemouse button bstate
x y
3081 (conf, leftx
, _, hooverpageno
, anchor) =
3083 | Glut.LEFT_BUTTON
when bstate
= Glut.UP
->
3084 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
3085 let rec loop = function
3088 if y > l
.pagedispy
&& y < l
.pagedispy
+ l
.pagevh
3089 && x > margin && x < margin + l
.pagew
3091 leavebirdseye (conf, leftx
, l
.pageno, hooverpageno
, anchor) false;
3096 | Glut.OTHER_BUTTON
_ -> viewmouse button bstate
x y
3100 let mouse bstate button
x y =
3101 match state.mode with
3102 | View
-> viewmouse button bstate
x y
3103 | Birdseye beye
-> birdseyemouse button bstate
x y beye
3104 | Textentry
_ | Outline
_ | Items
_ -> ()
3107 let mouse ~button ~
state ~
x ~
y = mouse state button
x y;;
3110 match state.mode with
3113 match state.mstate
with
3114 | Mzoom
_ | Mnone
-> ()
3119 state.mstate
<- Mpan
(x, y);
3120 if conf.zoom > 1.0 then state.x <- state.x + dx;
3122 gotoy_and_clear_text y
3125 state.mstate
<- Msel
(a, (x, y));
3126 Glut.postRedisplay
()
3129 let y = min
conf.winh
(max
0 y) in
3130 let percent = float y /. float conf.winh
in
3131 let y = truncate
(float (state.maxy - conf.winh
) *. percent) in
3132 gotoy_and_clear_text y
3136 match state.mode with
3137 | Birdseye
(conf, leftx
, pageno, hooverpageno
, anchor) ->
3138 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
3139 let rec loop = function
3141 if hooverpageno
!= -1
3143 state.mode <- Birdseye
(conf, leftx
, pageno, -1, anchor);
3144 Glut.postRedisplay
();
3147 if y > l
.pagedispy
&& y < l
.pagedispy
+ l
.pagevh
3148 && x > margin && x < margin + l
.pagew
3150 state.mode <- Birdseye
(conf, leftx
, pageno, l
.pageno, anchor);
3151 Glut.postRedisplay
();
3157 | Outline
_ | Items
_ | Textentry
_ -> ()
3159 match state.mstate
with
3161 begin match getunder x y with
3162 | Unone
-> Glut.setCursor
Glut.CURSOR_INHERIT
3164 if conf.underinfo
then showtext 'u'
("ri: " ^ uri
);
3165 Glut.setCursor
Glut.CURSOR_INFO
3166 | Ulinkgoto
(page
, _) ->
3168 then showtext 'p'
("age: " ^ string_of_int page
);
3169 Glut.setCursor
Glut.CURSOR_INFO
3171 if conf.underinfo
then showtext '
f'
("ont: " ^
s);
3172 Glut.setCursor
Glut.CURSOR_TEXT
3175 | Mpan
_ | Msel
_ | Mzoom
_ | Mscroll
->
3183 let fontpath = ref "";;
3187 match Sys.os_type
with
3188 | "Win32" -> Sys.getenv
"HOMEPATH"
3189 | _ -> Sys.getenv
"HOME"
3192 ("Can not determine home directory location: " ^
3193 Printexc.to_string exn
);
3197 let config_of c attrs
=
3201 | "scroll-bar-width" -> { c with scrollbw
= max
0 (int_of_string
v) }
3202 | "scroll-handle-height" -> { c with scrollh
= max
0 (int_of_string
v) }
3203 | "case-insensitive-search" -> { c with icase
= bool_of_string
v }
3204 | "preload" -> { c with preload = bool_of_string
v }
3205 | "page-bias" -> { c with pagebias
= int_of_string
v }
3206 | "scroll-step" -> { c with scrollstep
= max
1 (int_of_string
v) }
3207 | "auto-scroll-step" ->
3208 { c with autoscrollstep
= max
0 (int_of_string
v) }
3209 | "max-height-fit" -> { c with maxhfit
= bool_of_string
v }
3210 | "crop-hack" -> { c with crophack
= bool_of_string
v }
3211 | "throttle" -> { c with showall
= bool_of_string
v }
3212 | "highlight-links" -> { c with hlinks
= bool_of_string
v }
3213 | "under-cursor-info" -> { c with underinfo
= bool_of_string
v }
3214 | "vertical-margin" ->
3215 { c with interpagespace = max
0 (int_of_string
v) }
3217 let zoom = float_of_string
v /. 100. in
3218 let zoom = max
0.01 zoom in
3219 { c with zoom = zoom }
3220 | "presentation" -> { c with presentation = bool_of_string
v }
3221 | "rotation-angle" -> { c with angle
= int_of_string
v }
3222 | "width" -> { c with winw
= max
20 (int_of_string
v) }
3223 | "height" -> { c with winh
= max
20 (int_of_string
v) }
3224 | "persistent-bookmarks" -> { c with savebmarks
= bool_of_string
v }
3225 | "proportional-display" -> { c with proportional
= bool_of_string
v }
3226 | "pixmap-cache-size" -> { c with memlimit
= max
2 (int_of_string
v) }
3227 | "tex-count" -> { c with texcount
= max
1 (int_of_string
v) }
3228 | "slice-height" -> { c with sliceheight
= max
2 (int_of_string
v) }
3229 | "block-width" -> { c with blockwidth
= max
2 (int_of_string
v) }
3230 | "thumbnail-width" -> { c with thumbw
= max
2 (int_of_string
v) }
3231 | "persistent-location" -> { c with jumpback
= bool_of_string
v }
3232 | "background-color" -> { c with bgcolor
= color_of_string v }
3233 | "scrollbar-in-presentation" ->
3234 { c with scrollbarinpm
= bool_of_string
v }
3237 prerr_endline
("Error processing attribute (`" ^
3238 k ^
"'=`" ^
v ^
"'): " ^
Printexc.to_string exn
);
3241 let rec fold c = function
3244 let c = apply c k
v in
3250 let fromstring f pos
n v d =
3253 dolog "Error processing attribute (%S=%S) at %d\n%s"
3254 n v pos
(Printexc.to_string exn
)
3259 let bookmark_of attrs
=
3260 let rec fold title page rely
= function
3261 | ("title", v) :: rest
-> fold v page rely rest
3262 | ("page", v) :: rest
-> fold title v rely rest
3263 | ("rely", v) :: rest
-> fold title page
v rest
3264 | _ :: rest
-> fold title page rely rest
3265 | [] -> title, page
, rely
3267 fold "invalid" "0" "0" attrs
3271 let rec fold path page rely pan
= function
3272 | ("path", v) :: rest
-> fold v page rely pan rest
3273 | ("page", v) :: rest
-> fold path
v rely pan rest
3274 | ("rely", v) :: rest
-> fold path page
v pan rest
3275 | ("pan", v) :: rest
-> fold path page rely
v rest
3276 | _ :: rest
-> fold path page rely pan rest
3277 | [] -> path
, page
, rely
, pan
3279 fold "" "0" "0" "0" attrs
3282 let setconf dst src
=
3283 dst
.scrollbw
<- src
.scrollbw
;
3284 dst
.scrollh
<- src
.scrollh
;
3285 dst
.icase
<- src
.icase
;
3286 dst
.preload <- src
.preload;
3287 dst
.pagebias
<- src
.pagebias
;
3288 dst
.verbose
<- src
.verbose
;
3289 dst
.scrollstep
<- src
.scrollstep
;
3290 dst
.maxhfit
<- src
.maxhfit
;
3291 dst
.crophack
<- src
.crophack
;
3292 dst
.autoscrollstep
<- src
.autoscrollstep
;
3293 dst
.showall
<- src
.showall
;
3294 dst
.hlinks
<- src
.hlinks
;
3295 dst
.underinfo
<- src
.underinfo
;
3296 dst
.interpagespace <- src
.interpagespace;
3297 dst
.zoom <- src
.zoom;
3298 dst
.presentation <- src
.presentation;
3299 dst
.angle
<- src
.angle
;
3300 dst
.winw
<- src
.winw
;
3301 dst
.winh
<- src
.winh
;
3302 dst
.savebmarks
<- src
.savebmarks
;
3303 dst
.memlimit
<- src
.memlimit
;
3304 dst
.proportional
<- src
.proportional
;
3305 dst
.texcount
<- src
.texcount
;
3306 dst
.sliceheight
<- src
.sliceheight
;
3307 dst
.blockwidth
<- src
.blockwidth
;
3308 dst
.thumbw
<- src
.thumbw
;
3309 dst
.jumpback
<- src
.jumpback
;
3310 dst
.bgcolor
<- src
.bgcolor
;
3311 dst
.scrollbarinpm
<- src
.scrollbarinpm
;
3315 let l = String.length
s in
3316 let b = Buffer.create
l in
3322 let h = Hashtbl.create
10 in
3323 let dc = { defconf with angle
= defconf.angle
} in
3324 let rec toplevel v t spos
_ =
3326 | Vdata
| Vcdata
| Vend
-> v
3327 | Vopen
("llppconfig", _, closed
) ->
3330 else { v with f = llppconfig
}
3332 error
"unexpected subelement at top level" s spos
3333 | Vclose
_ -> error
"unexpected close at top level" s spos
3335 and llppconfig
v t spos
_ =
3337 | Vdata
| Vcdata
-> v
3338 | Vend
-> error
"unexpected end of input in llppconfig" s spos
3339 | Vopen
("defaults", attrs
, closed
) ->
3340 let c = config_of dc attrs
in
3344 else { v with f = skip
"defaults" (fun () -> v) }
3346 | Vopen
("ui-font", _, closed
) ->
3349 else { v with f = uifont
(Buffer.create
10) }
3351 | Vopen
("doc", attrs
, closed
) ->
3352 let pathent, spage
, srely
, span
= doc_of attrs
in
3353 let path = unent pathent
3354 and pageno = fromstring int_of_string spos
"page" spage
0
3355 and rely
= fromstring float_of_string spos
"rely" srely
0.0
3356 and pan
= fromstring int_of_string spos
"pan" span
0 in
3357 let c = config_of dc attrs
in
3358 let anchor = (pageno, rely
) in
3360 then (Hashtbl.add
h path (c, [], pan
, anchor); v)
3361 else { v with f = doc
path pan
anchor c [] }
3364 error
"unexpected subelement in llppconfig" s spos
3366 | Vclose
"llppconfig" -> { v with f = toplevel }
3367 | Vclose
_ -> error
"unexpected close in llppconfig" s spos
3369 and uifont
b v t spos epos
=
3372 Buffer.add_substring
b s spos
(epos
- spos
);
3374 | Vopen
(_, _, _) ->
3375 error
"unexpected subelement in ui-font" s spos
3376 | Vclose
"ui-font" ->
3377 if String.length
!fontpath = 0
3378 then fontpath := Buffer.contents
b;
3379 { v with f = llppconfig
}
3380 | Vclose
_ -> error
"unexpected close in ui-font" s spos
3381 | Vend
-> error
"unexpected end of input in ui-font" s spos
3383 and doc
path pan
anchor c bookmarks v t spos
_ =
3385 | Vdata
| Vcdata
-> v
3386 | Vend
-> error
"unexpected end of input in doc" s spos
3387 | Vopen
("bookmarks", _, closed
) ->
3390 else { v with f = pbookmarks
path pan
anchor c bookmarks }
3392 | Vopen
(_, _, _) ->
3393 error
"unexpected subelement in doc" s spos
3396 Hashtbl.add
h path (c, List.rev
bookmarks, pan
, anchor);
3397 { v with f = llppconfig
}
3399 | Vclose
_ -> error
"unexpected close in doc" s spos
3401 and pbookmarks
path pan
anchor c bookmarks v t spos
_ =
3403 | Vdata
| Vcdata
-> v
3404 | Vend
-> error
"unexpected end of input in bookmarks" s spos
3405 | Vopen
("item", attrs
, closed
) ->
3406 let titleent, spage
, srely
= bookmark_of attrs
in
3407 let page = fromstring int_of_string spos
"page" spage
0
3408 and rely
= fromstring float_of_string spos
"rely" srely
0.0 in
3409 let bookmarks = (unent titleent, 0, (page, rely
)) :: bookmarks in
3411 then { v with f = pbookmarks
path pan
anchor c bookmarks }
3414 { v with f = skip
"item" f }
3417 error
"unexpected subelement in bookmarks" s spos
3419 | Vclose
"bookmarks" ->
3420 { v with f = doc
path pan
anchor c bookmarks }
3422 | Vclose
_ -> error
"unexpected close in bookmarks" s spos
3424 and skip tag
f v t spos
_ =
3426 | Vdata
| Vcdata
-> v
3428 error
("unexpected end of input in skipped " ^ tag
) s spos
3429 | Vopen
(tag'
, _, closed
) ->
3433 let f'
() = { v with f = skip tag
f } in
3434 { v with f = skip tag'
f'
}
3438 else error
("unexpected close in skipped " ^ tag
) s spos
3441 parse
{ f = toplevel; accu = () } s;
3447 let len = in_channel_length ic
in
3448 let s = String.create
len in
3449 really_input ic
s 0 len;
3452 | Parse_error
(msg
, s, pos
) ->
3453 let subs = subs s pos
in
3454 let s = Printf.sprintf
"%s: at %d [..%s..]" msg pos
subs in
3455 failwith
("parse error: " ^
s)
3458 failwith
("config load error: " ^
Printexc.to_string exn
)
3464 let dir = Filename.concat
home ".config" in
3465 if Sys.is_directory
dir then dir else home
3468 Filename.concat
dir "llpp.conf"
3471 let confpath = ref defconfpath;;
3474 if Sys.file_exists
!confpath
3477 (try Some
(open_in_bin
!confpath)
3480 ("Error opening configuation file `" ^
!confpath ^
"': " ^
3481 Printexc.to_string exn
);
3490 ("Error loading configuation from `" ^
!confpath ^
"': " ^
3491 Printexc.to_string exn
);
3497 f (Hashtbl.create
0, defconf)
3502 let pc, pb
, px
, pa
=
3504 Hashtbl.find h (Filename.basename
state.path)
3505 with Not_found
-> dc, [], 0, (0, 0.0)
3509 state.bookmarks <- pb
;
3511 state.scrollw
<- conf.scrollbw
;
3513 then state.anchor <- pa
;
3514 cbput state.hists
.nav pa
;
3519 let add_attrs bb always
dc c =
3522 then Printf.bprintf bb
"\n %s='%b'" s a
3525 then Printf.bprintf bb
"\n %s='%d'" s a
3528 then Printf.bprintf bb
"\n %s='%d'" s (truncate
(a*.100.))
3532 Printf.bprintf bb
"\n %s='%s'" s (color_to_string a)
3536 then dc.winw
, dc.winh
3538 match state.fullscreen
with
3540 | None
-> c.winw
, c.winh
3542 let zoom, presentation, interpagespace, showall
=
3544 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
3546 match state.mode with
3547 | Birdseye
(bc
, _, _, _, _) ->
3548 bc
.zoom, bc
.presentation, bc
.interpagespace, bc
.showall
3549 | _ -> c.zoom, c.presentation, c.interpagespace, c.showall
3551 oi
"width" w dc.winw
;
3552 oi
"height" h dc.winh
;
3553 oi
"scroll-bar-width" c.scrollbw
dc.scrollbw
;
3554 oi
"scroll-handle-height" c.scrollh
dc.scrollh
;
3555 ob "case-insensitive-search" c.icase
dc.icase
;
3556 ob "preload" c.preload dc.preload;
3557 oi
"page-bias" c.pagebias
dc.pagebias
;
3558 oi
"scroll-step" c.scrollstep
dc.scrollstep
;
3559 oi
"auto-scroll-step" c.autoscrollstep
dc.autoscrollstep
;
3560 ob "max-height-fit" c.maxhfit
dc.maxhfit
;
3561 ob "crop-hack" c.crophack
dc.crophack
;
3562 ob "throttle" showall
dc.showall
;
3563 ob "highlight-links" c.hlinks
dc.hlinks
;
3564 ob "under-cursor-info" c.underinfo
dc.underinfo
;
3565 oi
"vertical-margin" interpagespace dc.interpagespace;
3566 oz
"zoom" zoom dc.zoom;
3567 ob "presentation" presentation dc.presentation;
3568 oi
"rotation-angle" c.angle
dc.angle
;
3569 ob "persistent-bookmarks" c.savebmarks
dc.savebmarks
;
3570 ob "proportional-display" c.proportional
dc.proportional
;
3571 oi
"pixmap-cache-size" c.memlimit
dc.memlimit
;
3572 oi
"tex-count" c.texcount
dc.texcount
;
3573 oi
"slice-height" c.sliceheight
dc.sliceheight
;
3574 oi
"block-width" c.blockwidth
dc.blockwidth
;
3575 oi
"thumbnail-width" c.thumbw
dc.thumbw
;
3576 ob "persistent-location" c.jumpback
dc.jumpback
;
3577 oc
"background-color" c.bgcolor
dc.bgcolor
;
3578 ob "scrollbar-in-presentation" c.scrollbarinpm
dc.scrollbarinpm
;
3582 let bb = Buffer.create
32768 in
3584 let dc = if conf.bedefault
then conf else dc in
3585 Buffer.add_string
bb "<llppconfig>\n";
3587 if String.length
!fontpath > 0
3588 then Printf.bprintf
bb "<ui-font><![CDATA[%s]]></ui-font>\n" !fontpath;
3590 Buffer.add_string
bb "<defaults ";
3591 add_attrs bb true dc dc;
3592 Buffer.add_string
bb "/>\n";
3594 let adddoc path pan
anchor c bookmarks =
3595 if bookmarks == [] && c = dc && anchor = emptyanchor
3598 Printf.bprintf
bb "<doc path='%s'"
3599 (enent
path 0 (String.length
path));
3601 if anchor <> emptyanchor
3603 let n, y = anchor in
3604 Printf.bprintf
bb " page='%d'" n;
3607 Printf.bprintf
bb " rely='%f'" y
3612 then Printf.bprintf
bb " pan='%d'" pan
;
3614 add_attrs bb false dc c;
3616 begin match bookmarks with
3617 | [] -> Buffer.add_string
bb "/>\n"
3619 Buffer.add_string
bb ">\n<bookmarks>\n";
3620 List.iter
(fun (title, _level
, (page, rely
)) ->
3622 "<item title='%s' page='%d'"
3623 (enent
title 0 (String.length
title))
3628 Printf.bprintf
bb " rely='%f'" rely
3630 Buffer.add_string
bb "/>\n";
3632 Buffer.add_string
bb "</bookmarks>\n</doc>\n";
3638 match state.mode with
3639 | Birdseye
(_, pan, _, _, _) -> pan
3642 let basename = Filename.basename state.path in
3643 adddoc basename pan (getanchor ())
3646 match state.autoscroll
with
3648 | None
-> conf.autoscrollstep
}
3649 (if conf.savebmarks
then state.bookmarks else []);
3651 Hashtbl.iter
(fun path (c, bookmarks, x, y) ->
3653 then adddoc path x y c bookmarks
3655 Buffer.add_string
bb "</llppconfig>";
3658 if Buffer.length
bb > 0
3661 let tmp = !confpath ^
".tmp" in
3662 let oc = open_out_bin
tmp in
3663 Buffer.output_buffer
oc bb;
3665 Sys.rename
tmp !confpath;
3668 ("error while saving configuration: " ^
Printexc.to_string exn
)
3675 [("-p", Arg.String
(fun s -> state.password
<- s) ,
3676 "<password> Set password");
3678 ("-f", Arg.String
(fun s -> State.fontpath := s),
3679 "<path> Set path to the user interface font");
3681 ("-c", Arg.String
(fun s -> State.confpath := s),
3682 "<path> Set path to the configuration file");
3684 ("-v", Arg.Unit
(fun () ->
3686 "%s\nconfiguration path: %s\n"
3690 exit
0), " Print version and exit");
3693 (fun s -> state.path <- s)
3694 ("Usage: " ^
Sys.argv
.(0) ^
" [options] some.pdf\nOptions:")
3696 if String.length
state.path = 0
3697 then (prerr_endline
"file name missing"; exit
1);
3701 let _ = Glut.init
Sys.argv
in
3702 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
3703 let () = Glut.initWindowSize
conf.winw
conf.winh
in
3704 let _ = Glut.createWindow
("llpp " ^
Filename.basename state.path) in
3707 if Sys.os_type
= "Unix"
3709 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
3711 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
3712 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
3713 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
3714 Unix.bind
sock addr;
3716 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
3717 Unix.connect
csock addr;
3718 let ssock, _ = Unix.accept
sock in
3721 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
3722 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
3729 let () = Glut.displayFunc
display in
3730 let () = Glut.reshapeFunc
reshape in
3731 let () = Glut.keyboardFunc
keyboard in
3732 let () = Glut.specialFunc
special in
3733 let () = Glut.idleFunc
(Some
idle) in
3734 let () = Glut.mouseFunc
mouse in
3735 let () = Glut.motionFunc
motion in
3736 let () = Glut.passiveMotionFunc
pmotion in
3739 conf.angle
, conf.proportional
, conf.texcount
,
3740 conf.sliceheight
, conf.blockwidth
, !State.fontpath
3742 state.csock <- csock;
3743 state.ssock <- ssock;
3744 state.text <- "Opening " ^
state.path;
3745 writeopen state.path state.password
;
3751 | Glut.BadEnum
"key in special_of_int" ->
3752 showtext '
!'
" LablGlut bug: special key not recognized";
3755 if Sys.os_type
<> "Unix"
3756 then Unix.shutdown
ssock Unix.SHUTDOWN_ALL
;