4 | Ulinkgoto
of (int * int)
6 and facename
= string;;
8 let dolog fmt
= Printf.kprintf prerr_endline fmt
;;
10 type params
= angle
* proportional
* texcount
* sliceheight
* fontpath
19 and proportional
= bool
20 and interpagespace
= int
28 external init
: Unix.file_descr
-> params
-> unit = "ml_init";;
29 external draw
: (int * int * int * int * bool) -> string -> unit = "ml_draw";;
30 external seltext
: string -> (int * int * int * int) -> int -> unit =
32 external copysel
: string -> unit = "ml_copysel";;
33 external getpdimrect
: int -> float array
= "ml_getpdimrect";;
34 external whatsunder
: string -> int -> int -> under
= "ml_whatsunder";;
35 external zoomforh
: int -> int -> int -> float = "ml_zoom_for_height";;
36 external drawstr
: int -> int -> int -> string -> float = "ml_draw_string";;
37 external measurestr
: int -> string -> float = "ml_measure_string";;
41 | Msel
of (mpos
* mpos
)
44 | Mzoom
of (int * int)
48 type textentry
= string * string * onhist
option * onkey
* ondone
49 and onkey
= string -> int -> te
50 and ondone
= string -> unit
51 and histcancel
= unit -> unit
52 and onhist
= ((histcmd
-> string) * histcancel
)
53 and histcmd
= HCnext
| HCprev
| HCfirst
| HClast
58 | TEswitch
of textentry
70 { store
= Array.create n v
77 let cbcap b
= Array.length b
.store
;;
82 b
.wc
<- (b
.wc
+ 1) mod cap;
84 b
.len
<- min
(b
.len
+ 1) cap;
87 let cbempty b
= b
.len
= 0;;
89 let cbgetg b circular dir
=
93 let rc = b
.rc + dir
in
105 else max
0 (min
rc (b
.len
-1))
111 let cbget b
= cbgetg b
false;;
112 let cbgetc b
= cbgetg b
true;;
115 let rc = b
.wc
- b
.len
in
116 let rc = if rc < 0 then cbcap b
+ rc else rc in
120 let cbdecr b
= b
.len
<- b
.len
- 1;;
135 { mutable scrollbw
: int
136 ; mutable scrollh
: int
137 ; mutable icase
: bool
138 ; mutable preload
: bool
139 ; mutable pagebias
: int
140 ; mutable verbose
: bool
141 ; mutable scrollstep
: int
142 ; mutable maxhfit
: bool
143 ; mutable crophack
: bool
144 ; mutable autoscrollstep
: int
145 ; mutable showall
: bool
146 ; mutable hlinks
: bool
147 ; mutable underinfo
: bool
148 ; mutable interpagespace
: interpagespace
149 ; mutable zoom
: float
150 ; mutable presentation
: bool
151 ; mutable angle
: angle
154 ; mutable savebmarks
: bool
155 ; mutable proportional
: proportional
156 ; mutable memlimit
: int
157 ; mutable texcount
: texcount
158 ; mutable sliceheight
: sliceheight
159 ; mutable thumbw
: width
160 ; mutable jumpback
: bool
161 ; mutable bgcolor
: float * float * float
162 ; mutable bedefault
: bool
163 ; mutable scrollbarinpm
: bool
164 ; mutable uifont
: string
168 type anchor
= pageno
* top
;;
170 type outline
= string * int * anchor
172 | Oarray
of outline array
173 | Olist
of outline list
174 | Onarrow
of string * outline array
* outline array
177 type rect
= float * float * float * float * float * float * float * float;;
179 type pagemapkey
= pageno
* width
* angle
* proportional
* gen
;;
181 let emptyanchor = (0, 0.0);;
184 | Birdseye
of (conf
* leftx
* pageno
* pageno
* anchor
)
185 | Outline
of (bool * int * int * outline array
* string * int * mode
)
186 | Items
of (int * int * item array
* string * int * mode
)
187 | Textentry
of (textentry
* onleave
)
189 and onleave
= leavetextentrystatus
-> unit
190 and leavetextentrystatus
= | Cancel
| Confirm
191 and item
= string * int * action
194 | Action
of (int -> int -> string -> int -> mode
)
197 let isbirdseye = function Birdseye _
-> true | _
-> false;;
198 let istextentry = function Textentry _
-> true | _
-> false;;
201 { mutable csock
: Unix.file_descr
202 ; mutable ssock
: Unix.file_descr
206 ; mutable scrollw
: int
207 ; mutable anchor
: anchor
209 ; mutable layout
: layout list
210 ; pagemap
: (pagemapkey
, (opaque
* pixmapsize
)) Hashtbl.t
211 ; mutable pdims
: (pageno
* width
* height
* leftx
) list
212 ; mutable pagecount
: int
213 ; pagecache
: string circbuf
214 ; mutable rendering
: bool
215 ; mutable mstate
: mstate
216 ; mutable searchpattern
: string
217 ; mutable rects
: (pageno
* recttype
* rect
) list
218 ; mutable rects1
: (pageno
* recttype
* rect
) list
219 ; mutable text
: string
220 ; mutable fullscreen
: (width
* height
) option
221 ; mutable mode
: mode
222 ; mutable outlines
: outlines
223 ; mutable bookmarks
: outline list
224 ; mutable path
: string
225 ; mutable password
: string
226 ; mutable invalidated
: int
227 ; mutable colorscale
: float
228 ; mutable memused
: int
230 ; mutable throttle
: layout list
option
231 ; mutable autoscroll
:int option
232 ; mutable help
: item array
233 ; mutable docinfo
: (int * string) list
234 ; mutable deadline
: float
238 { pat
: string circbuf
239 ; pag
: string circbuf
240 ; nav
: anchor circbuf
254 ; autoscrollstep
= 24
260 ; presentation
= false
265 ; proportional
= true
266 ; memlimit
= 32*1024*1024
271 ; bgcolor
= (0.5, 0.5, 0.5)
273 ; scrollbarinpm
= true
278 let conf = { defconf with angle
= defconf.angle
};;
281 let strings = ("llpp version " ^
Help.version
) :: "" :: Help.keys
in
282 Array.of_list
(List.map
(fun s
-> s
, 0, Noaction
) strings);
292 ; anchor
= emptyanchor
295 ; pagemap
= Hashtbl.create
10
296 ; pagecache
= cbnew 100 ""
307 ; outlines
= Olist
[]
313 { nav
= cbnew 100 (0, 0.0)
331 Printf.kprintf prerr_endline fmt
333 Printf.kprintf ignore fmt
337 let len = String.length s
in
339 let b = Buffer.create
n in
340 Buffer.add_char
b (Char.chr
((len lsr 24) land 0xff));
341 Buffer.add_char
b (Char.chr
((len lsr 16) land 0xff));
342 Buffer.add_char
b (Char.chr
((len lsr 8) land 0xff));
343 Buffer.add_char
b (Char.chr
((len lsr 0) land 0xff));
344 Buffer.add_string
b s
;
345 let s'
= Buffer.contents
b in
346 let n'
= Unix.write fd
s'
0 n in
347 if n'
!= n then failwith
"write failed";
352 let n = Unix.read fd
s 0 4 in
353 if n != 4 then failwith
"incomplete read(len)";
355 lor (Char.code
s.[0] lsl 24)
356 lor (Char.code
s.[1] lsl 16)
357 lor (Char.code
s.[2] lsl 8)
358 lor (Char.code
s.[3] lsl 0)
360 let s = String.create
len in
361 let n = Unix.read fd
s 0 len in
362 if n != len then failwith
"incomplete read(data)";
367 let b = Buffer.create
10 in
368 Buffer.add_string
b s;
369 let rec combine = function
372 Buffer.add_char
b ' '
;
375 | `
b b -> if b then "1" else "0"
377 | `i i
-> string_of_int i
378 | `f f
-> string_of_float f
379 | `I f
-> string_of_int
(truncate f
)
381 Buffer.add_string
b s;
388 let cmd = Buffer.contents
(makecmd s l
) in
389 writecmd state.csock
cmd;
395 let d = conf.winh
- h
in
402 let rec f pn ph pi fh l
=
404 | (n, _
, h
, _
) :: rest
->
405 let ips = calcips h
in
410 if isbirdseye state.mode
&& pn
= 0
415 let fh = fh + ((n - pn
) * (ph
+ pi
)) in
420 if conf.presentation
|| (isbirdseye state.mode
&& pn
= 0)
424 let fh = fh + ((state.pagecount
- pn
) * (ph
+ pi
)) + inc in
427 let fh = f 0 0 0 0 state.pdims
in
431 let getpageyh pageno
=
432 let rec f pn ph pi y l
=
434 | (n, _
, h
, _
) :: rest
->
435 let ips = calcips h
in
438 let h = if n = pageno
then h else ph
in
439 if conf.presentation
&& n = pageno
441 y
+ (pageno
- pn
) * (ph
+ pi
) + pi
, h
443 y
+ (pageno
- pn
) * (ph
+ pi
), h
445 let y = y + (if conf.presentation
then pi
else 0) in
446 let y = y + (n - pn
) * (ph
+ pi
) in
450 y + (pageno
- pn
) * (ph
+ pi
), ph
452 f 0 0 0 0 state.pdims
455 let getpagey pageno
= fst
(getpageyh pageno
);;
458 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu
=
459 let ((w
, h, ips, x
) as curr
), rest
, pdimno
, yinc
=
461 | (pageno'
, w
, h, x
) :: rest
when pageno'
= pageno
->
462 let ips = calcips h in
464 if conf.presentation
|| (isbirdseye state.mode
&& pageno
= 0)
468 (w
, h, ips, x
), rest
, pdimno
+ 1, yinc
470 prev
, pdims
, pdimno
, 0
472 let dy = dy + yinc in
473 let py = py + yinc in
474 if pageno
= state.pagecount
|| cacheleft
= 0 || dy >= sh
479 if py + h <= vy - yinc
481 let py = py + h + ips in
482 let dy = max
0 (py - y) in
492 let pagey = vy - py in
493 let pagevh = h - pagey in
494 let pagevh = min
(sh
- dy) pagevh in
495 let off = if yinc > 0 then py - vy else 0 in
496 let py = py + h + ips in
502 ; pagedispy
= dy + off
503 ; pagey = pagey + off
504 ; pagevh = pagevh - off
508 let accu = e :: accu in
515 ~cacheleft
:(cacheleft
-1)
518 if state.invalidated
= 0
528 ~cacheleft
:(cbcap state.pagecache
)
538 let y = state.y + incr
in
540 let y = min
y (state.maxy
- (if conf.maxhfit
then conf.winh
else 0)) in
544 let getopaque pageno
=
545 try Some
(Hashtbl.find
state.pagemap
546 (pageno
, state.w
, conf.angle
, conf.proportional
, state.gen
))
547 with Not_found
-> None
550 let cache pageno opaque
=
551 Hashtbl.replace
state.pagemap
552 (pageno
, state.w
, conf.angle
, conf.proportional
, state.gen
) opaque
555 let validopaque opaque
= String.length opaque
> 0;;
558 match getopaque l
.pageno
with
559 | None
when not
state.rendering
->
560 state.rendering
<- true;
561 cache l
.pageno
("", -1);
562 wcmd "render" [`i
(l
.pageno
+ 1)
569 let loadlayout layout =
570 let rec f all
= function
572 begin match getopaque l
.pageno
with
573 | None
-> render l
; f false ls
574 | Some
(opaque
, _
) -> f (all
&& validopaque opaque
) ls
578 f (layout <> []) layout;
581 let findpageforopaque opaque
=
583 (fun k
(v
, s) a
-> if v
= opaque
then Some
(k
, s) else a
)
587 let pagevisible layout n = List.exists
(fun l
-> l
.pageno
= n) layout;;
593 let memleft = conf.memlimit
- state.memused
in
596 let opaque = cbpeek state.pagecache
in
597 match findpageforopaque opaque with
598 | Some
((n, _
, _
, _
, _
), size
) ->
599 memleft + size
>= 0 && not
(pagevisible state.layout n)
606 let presentation = conf.presentation in
607 let interpagespace = conf.interpagespace in
608 let maxy = state.maxy in
609 conf.presentation <- false;
610 conf.interpagespace <- 0;
611 state.maxy <- calcheight ();
613 match state.layout with
615 | l
:: _
-> getpagey l
.pageno
+ l
.pagey
617 let y = if y < conf.winh
then 0 else y - conf.winh
in
618 let pages = layout y (conf.winh
*3) in
619 List.iter
render pages;
620 conf.presentation <- presentation;
621 conf.interpagespace <- interpagespace;
627 let y = min
state.maxy y in
628 let pages = layout y conf.winh
in
629 let ready = loadlayout pages in
635 state.layout <- pages;
636 state.throttle
<- None
;
637 Glut.postRedisplay
();
640 state.throttle
<- Some
pages;
645 state.layout <- pages;
646 state.throttle
<- None
;
647 Glut.postRedisplay
();
649 begin match state.mode
with
650 | Birdseye
(conf, leftx
, pageno
, hooverpageno
, anchor
) ->
651 if not
(pagevisible pages pageno
)
653 match state.layout with
656 state.mode
<- Birdseye
(conf, leftx
, l
.pageno
, hooverpageno
, anchor
)
663 let gotoy_and_clear_text y =
665 if not
conf.verbose
then state.text
<- "";
669 match state.layout with
671 | l
:: _
-> (l
.pageno
, float l
.pagey /. float l
.pageh
)
674 let getanchory (n, top
) =
675 let y, h = getpageyh n in
676 y + (truncate
(top
*. float h));
679 let gotoanchor anchor
=
680 gotoy (getanchory anchor
);
684 cbput state.hists
.nav
(getanchor ());
688 let anchor = cbgetc state.hists
.nav dir
in
693 let y, h = getpageyh n in
694 gotoy_and_clear_text (y + (truncate
(top
*. float h)));
697 let gotopage1 n top
=
698 let y = getpagey n in
699 gotoy_and_clear_text (y + top
);
707 state.invalidated
<- state.invalidated
+ 1;
711 let c = c *. state.colorscale
in
715 let scalecolor2 (r
, g
, b) =
716 (r
*. state.colorscale
, g
*. state.colorscale
, b *. state.colorscale
);
720 state.maxy <- calcheight ();
721 match state.mode
with
722 | Birdseye
(_
, _
, pageno
, _
, _
) ->
723 let y, h = getpageyh pageno
in
724 let top = (conf.winh
- h) / 2 in
725 gotoy (max
0 (y - top))
726 | _
-> gotoanchor state.anchor
730 GlMat.mode `projection
;
731 GlMat.load_identity
();
732 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
733 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
734 GlMat.scale3
(2.0 /. float state.w
, 2.0 /. float conf.winh
, 1.0);
737 GlMat.translate ~x
:(float state.x
) ();
742 GlMat.mode `projection
;
743 GlMat.load_identity
();
744 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
745 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
746 GlMat.scale3
(2.0 /. float conf.winw
, 2.0 /. float conf.winh
, 1.0);
750 let firsttime = ref true in
752 if state.invalidated
= 0 && not
!firsttime
753 then state.anchor <- getanchor ();
757 let w = truncate
(float w *. conf.zoom
) - state.scrollw
in
761 GlMat.mode `modelview
;
762 GlMat.load_identity
();
763 GlClear.color
(scalecolor 1.0);
764 GlClear.clear
[`color
];
767 wcmd "geometry" [`i
w; `i
h];
770 let drawstring size x
y s =
772 Gl.enable `texture_2d
;
773 ignore
(drawstr size x
y s);
775 Gl.disable `texture_2d
;
778 let drawstring1 size x
y s =
783 let len = String.length
state.text
in
785 GlDraw.color
(0.0, 0.0, 0.0);
787 (0.0, float (conf.winh
- 18))
788 (float (conf.winw
- state.scrollw
- 1), float conf.winh
)
790 GlDraw.color
(1.0, 1.0, 1.0);
791 drawstring 14 (if len > 0 then 8 else 2) (conf.winh
- 5) s;
793 match state.mode
with
794 | Textentry
((prefix
, text
, _
, _
, _
), _
) ->
798 Printf.sprintf
"%s%s_ [%s]" prefix text
state.text
800 Printf.sprintf
"%s%s_" prefix text
805 if len > 0 then drawstring state.text
809 state.text
<- Printf.sprintf
"%c%s" c s;
810 Glut.postRedisplay
();
819 state.rects
<- state.rects1
;
820 Glut.postRedisplay
()
823 let n = Scanf.sscanf
cmd "C %u" (fun n -> n) in
824 state.pagecount
<- n;
825 state.invalidated
<- state.invalidated
- 1;
826 if state.invalidated
= 0
830 let s = Scanf.sscanf
cmd "t %n"
831 (fun n -> String.sub
cmd n (String.length
cmd - n))
833 Glut.setWindowTitle
s
836 let s = Scanf.sscanf
cmd "T %n"
837 (fun n -> String.sub
cmd n (String.length
cmd - n))
839 if istextentry state.mode
846 Glut.postRedisplay
();
852 let s = Scanf.sscanf
cmd "V %n"
853 (fun n -> String.sub
cmd n (String.length
cmd - n))
859 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
860 Scanf.sscanf
cmd "F %u %d %f %f %f %f %f %f %f %f"
861 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
862 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
864 let y = (getpagey pageno) + truncate y0
in
867 state.rects1
<- [pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)]
870 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
871 Scanf.sscanf
cmd "R %u %d %f %f %f %f %f %f %f %f"
872 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
873 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
876 (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) :: state.rects1
879 let n, w, _h
, r
, l
, s, p
=
880 Scanf.sscanf
cmd "r %u %u %u %d %d %u %s"
881 (fun n w h r l
s p
->
882 (n-1, w, h, r
, l
!= 0, s, p
))
885 Hashtbl.replace
state.pagemap
(n, w, r
, l
, state.gen
) (p
, s);
886 state.memused
<- state.memused
+ s;
889 match state.throttle
with
890 | None
-> state.layout
891 | Some
layout -> layout
895 if (state.memused
<= conf.memlimit
) || cbempty state.pagecache
898 let evictedopaque = cbpeek state.pagecache
in
899 match findpageforopaque evictedopaque with
900 | None
-> failwith
"bug in gc"
901 | Some
((evictedn
, _
, _
, _
, gen
) as k
, evictedsize
) ->
902 if state.gen
!= gen
|| not
(pagevisible layout evictedn
)
904 wcmd "free" [`
s evictedopaque];
905 state.memused
<- state.memused
- evictedsize
;
906 Hashtbl.remove
state.pagemap k
;
907 cbdecr state.pagecache
;
914 cbput state.pagecache p
;
915 state.rendering
<- false;
917 begin match state.throttle
with
919 if pagevisible state.layout n
922 let allvisible = loadlayout state.layout in
923 if allvisible then preload ();
930 let y = getpagey l
.pageno + l
.pagey in
936 Scanf.sscanf
cmd "l %u %u %u %u" (fun n w h x
-> n, w, h, x
)
938 state.pdims
<- pdim :: state.pdims
941 let (l
, n, t
, h, pos
) =
942 Scanf.sscanf
cmd "o %u %u %d %u %n" (fun l
n t
h pos
-> l
, n, t
, h, pos
)
944 let s = String.sub
cmd pos
(String.length
cmd - pos
) in
945 let outline = (s, l
, (n, float t
/. float h)) in
947 match state.outlines with
948 | Olist
outlines -> Olist
(outline :: outlines)
949 | Oarray _
-> Olist
[outline]
950 | Onarrow _
-> Olist
[outline]
952 state.outlines <- outlines
956 if String.length
cmd > 1 && cmd.[1] = '
e'
958 state.docinfo
<- List.rev
state.docinfo
960 let s = Scanf.sscanf
cmd "i %n"
961 (fun n -> String.sub
cmd n (String.length
cmd - n))
963 state.docinfo
<- (1, s) :: state.docinfo
966 dolog "unknown cmd `%S'" cmd
969 let now = Unix.gettimeofday
;;
972 if state.deadline
== nan
then state.deadline
<- now ();
976 then max
0.0 (state.deadline
-. now ())
979 let r, _
, _
= Unix.select
[state.csock
] [] [] timeout in
980 state.deadline
<- state.deadline
+. delay
;
983 begin match state.autoscroll
with
984 | Some step
when step
!= 0 ->
985 let y = state.y + step
in
989 else if y >= state.maxy then 0 else y
993 then state.text
<- "";
998 let cmd = readcmd state.csock
in
1007 let action = function
1008 | HCprev
-> cbget cb ~
-1
1009 | HCnext
-> cbget cb
1
1010 | HCfirst
-> cbget cb ~
-(cb
.rc)
1011 | HClast
-> cbget cb
(cb
.len - 1 - cb
.rc)
1012 and cancel
() = cb
.rc <- rc
1016 let search pattern forward
=
1017 if String.length pattern
> 0
1020 match state.layout with
1023 l
.pageno, (l
.pagey + if forward
then 0 else 0*l
.pagevh)
1026 let b = makecmd "search"
1027 [`
b conf.icase
; `i
pn; `i
py; `i
(if forward
then 1 else 0)]
1029 Buffer.add_char
b '
,'
;
1030 Buffer.add_string
b pattern
;
1031 Buffer.add_char
b '
\000'
;
1034 writecmd state.csock
cmd;
1037 let intentry text key
=
1038 let c = Char.unsafe_chr key
in
1041 let s = "x" in s.[0] <- c;
1042 let text = text ^
s in
1046 state.text <- Printf.sprintf
"invalid char (%d, `%c')" key
c;
1051 let b = Buffer.create
(String.length
s + 1) in
1052 Buffer.add_string
b s;
1053 Buffer.add_char
b c;
1057 let textentry text key
=
1058 let c = Char.unsafe_chr key
in
1060 | _
when key
>= 32 && key
< 127 ->
1061 let text = addchar text c in
1065 dolog "unhandled key %d char `%c'" key
(Char.unsafe_chr key
);
1069 let reinit angle proportional
=
1070 conf.angle
<- angle
;
1071 conf.proportional
<- proportional
;
1073 wcmd "reinit" [`i angle
; `
b proportional
];
1077 let zoom = max
0.01 (min
2.2 zoom) in
1078 if zoom <> conf.zoom
1083 reshape conf.winw
conf.winh
;
1084 state.text <- Printf.sprintf
"zoom is now %-5.1f" (zoom *. 100.0);
1088 let enterbirdseye () =
1089 let zoom = float conf.thumbw
/. float conf.winw
in
1090 let birdseyepageno =
1091 let cy = conf.winh
/ 2 in
1095 let rec fold best
= function
1098 let d = cy - (l
.pagedispy
+ l
.pagevh/2)
1099 and dbest
= cy - (best
.pagedispy
+ best
.pagevh/2) in
1100 if abs
d < abs dbest
1107 state.mode
<- Birdseye
(
1108 { conf with zoom = conf.zoom }, state.x
, birdseyepageno, -1, getanchor ()
1111 conf.presentation <- false;
1112 conf.interpagespace <- 10;
1113 conf.hlinks
<- false;
1115 state.mstate
<- Mnone
;
1116 conf.showall
<- false;
1117 Glut.setCursor
Glut.CURSOR_INHERIT
;
1120 state.text <- Printf.sprintf
"birds eye mode on (zoom %3.1f%%)"
1125 reshape conf.winw
conf.winh
;
1128 let leavebirdseye (c, leftx
, pageno, _
, anchor) goback
=
1130 conf.zoom <- c.zoom;
1131 conf.presentation <- c.presentation;
1132 conf.interpagespace <- c.interpagespace;
1133 conf.showall
<- c.showall
;
1134 conf.hlinks
<- c.hlinks
;
1138 state.text <- Printf.sprintf
"birds eye mode off (zoom %3.1f%%)"
1141 reshape conf.winw
conf.winh
;
1142 state.anchor <- if goback
then anchor else (pageno, 0.0);
1145 let togglebirdseye () =
1146 match state.mode
with
1147 | Birdseye vals
-> leavebirdseye vals
true
1148 | View
| Outline _
-> enterbirdseye ()
1152 let upbirdseye (conf, leftx
, pageno, hooverpageno
, anchor) =
1153 let pageno = max
0 (pageno - 1) in
1154 let rec loop = function
1155 | [] -> gotopage1 pageno 0
1156 | l
:: _
when l
.pageno = pageno ->
1157 if l
.pagedispy
>= 0 && l
.pagey = 0
1158 then Glut.postRedisplay
()
1159 else gotopage1 pageno 0
1160 | _
:: rest
-> loop rest
1163 state.mode
<- Birdseye
(conf, leftx
, pageno, hooverpageno
, anchor)
1166 let downbirdseye (conf, leftx
, pageno, hooverpageno
, anchor) =
1167 let pageno = min
(state.pagecount
- 1) (pageno + 1) in
1168 state.mode
<- Birdseye
(conf, leftx
, pageno, hooverpageno
, anchor);
1169 let rec loop = function
1171 let y, h = getpageyh pageno in
1172 let dy = (y - state.y) - (conf.winh
- h - conf.interpagespace) in
1174 | l
:: _
when l
.pageno = pageno ->
1175 if l
.pagevh != l
.pageh
1176 then gotoy (clamp (l
.pageh
- l
.pagevh + conf.interpagespace))
1177 else Glut.postRedisplay
()
1178 | _
:: rest
-> loop rest
1183 let optentry mode _ key
=
1184 let btos b = if b then "on" else "off" in
1185 let c = Char.unsafe_chr key
in
1189 try conf.scrollstep
<- int_of_string
s with exc
->
1190 state.text <- Printf.sprintf
"bad integer `%s': %s"
1191 s (Printexc.to_string exc
)
1193 TEswitch
("scroll step: ", "", None
, intentry, ondone)
1198 conf.autoscrollstep
<- int_of_string
s;
1199 if state.autoscroll
<> None
1200 then state.autoscroll
<- Some
conf.autoscrollstep
1202 state.text <- Printf.sprintf
"bad integer `%s': %s"
1203 s (Printexc.to_string exc
)
1205 TEswitch
("auto scroll step: ", "", None
, intentry, ondone)
1210 let zoom = float (int_of_string
s) /. 100.0 in
1213 state.text <- Printf.sprintf
"bad integer `%s': %s"
1214 s (Printexc.to_string exc
)
1216 TEswitch
("zoom: ", "", None
, intentry, ondone)
1221 conf.thumbw
<- max
2 (min
1920 (int_of_string
s));
1223 Printf.sprintf
"thumbnail width is set to %d" conf.thumbw
;
1224 begin match mode
with
1226 leavebirdseye beye
false;
1231 state.text <- Printf.sprintf
"bad integer `%s': %s"
1232 s (Printexc.to_string exc
)
1234 TEswitch
("thumbnail width: ", "", None
, intentry, ondone)
1239 Some
(int_of_string
s)
1241 state.text <- Printf.sprintf
"bad integer `%s': %s"
1242 s (Printexc.to_string exc
);
1245 | Some angle
-> reinit angle
conf.proportional
1248 TEswitch
("rotation: ", "", None
, intentry, ondone)
1251 conf.icase
<- not
conf.icase
;
1252 TEdone
("case insensitive search " ^
(btos conf.icase
))
1255 conf.preload <- not
conf.preload;
1257 TEdone
("preload " ^
(btos conf.preload))
1260 conf.verbose
<- not
conf.verbose
;
1261 TEdone
("verbose " ^
(btos conf.verbose
))
1264 conf.maxhfit
<- not
conf.maxhfit
;
1265 state.maxy <- state.maxy + (if conf.maxhfit
then -conf.winh
else conf.winh
);
1266 TEdone
("maxhfit " ^
(btos conf.maxhfit
))
1269 conf.crophack
<- not
conf.crophack
;
1270 TEdone
("crophack " ^
btos conf.crophack
)
1273 conf.showall
<- not
conf.showall
;
1274 TEdone
("throttle " ^
btos conf.showall
)
1277 conf.underinfo
<- not
conf.underinfo
;
1278 TEdone
("underinfo " ^
btos conf.underinfo
)
1281 conf.savebmarks
<- not
conf.savebmarks
;
1282 TEdone
("persistent bookmarks " ^
btos conf.savebmarks
)
1288 match state.layout with
1293 conf.interpagespace <- int_of_string
s;
1294 state.maxy <- calcheight ();
1295 let y = getpagey pageno in
1298 state.text <- Printf.sprintf
"bad integer `%s': %s"
1299 s (Printexc.to_string exc
)
1301 TEswitch
("vertical margin: ", "", None
, intentry, ondone)
1304 reinit conf.angle
(not
conf.proportional
);
1305 TEdone
("proprortional display " ^
btos conf.proportional
)
1308 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
1312 let maxoutlinerows () = (conf.winh
- 31) / 16;;
1314 let enterselector allowdel
outlines errmsg msg
=
1315 if Array.length
outlines = 0
1317 showtext ' ' errmsg
;
1321 Glut.setCursor
Glut.CURSOR_INHERIT
;
1323 match state.layout with
1325 | {pageno=pageno} :: _
-> pageno
1329 if n = Array.length
outlines
1332 let (_
, _
, (outlinepageno
, _
)) = outlines.(n) in
1333 if outlinepageno
>= pageno then n else loop (n+1)
1337 state.mode
<- Outline
1338 (allowdel
, active, max
0 (active - maxoutlinerows () / 2), outlines, "", 0,
1340 Glut.postRedisplay
();
1344 let enteroutlinemode () =
1346 match state.outlines with
1349 let a = Array.of_list
(List.rev l
) in
1350 state.outlines <- Oarray
a;
1352 | Onarrow
(pat
, a, _
) ->
1353 a, "Outline was narrowed to `" ^ pat ^
"' (Ctrl-u to restore)"
1355 enterselector false outlines "Document has no outline" msg
;
1358 let enterbookmarkmode () =
1359 let bookmarks = Array.of_list
state.bookmarks in
1360 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1363 let mode_to_string mode
=
1364 let b = Buffer.create
10 in
1365 let rec f = function
1366 | Textentry
(_
, _
) -> Buffer.add_string
b "Textentry ";
1367 | View
-> Buffer.add_string
b "View"
1368 | Birdseye _
-> Buffer.add_string
b "Birdseye"
1369 | Items _
-> Buffer.add_string
b "Items"
1370 | Outline _
-> Buffer.add_string
b "Outline"
1376 let color_of_string s =
1377 Scanf.sscanf
s "%d/%d/%d" (fun r g
b ->
1378 (float r /. 256.0, float g
/. 256.0, float b /. 256.0)
1382 let color_to_string (r, g
, b) =
1383 let r = truncate
(r *. 256.0)
1384 and g
= truncate
(g
*. 256.0)
1385 and b = truncate
(b *. 256.0) in
1386 Printf.sprintf
"%d/%d/%d" r g
b
1389 let enterinfomode () =
1390 let btos = function true -> "on" | _
-> "off" in
1391 let mode = state.mode in
1392 let rec makeitems () =
1393 let intp name get set
=
1394 Printf.sprintf
"%s\t%d" name
(get
()), 1, Action
(
1395 fun active first _ pan
->
1400 state.text <- Printf.sprintf
"bad integer `%s': %s"
1401 s (Printexc.to_string exn
);
1404 if n != max_int
then set
n;
1406 let te = name ^
": ", "", None
, intentry, ondone in
1411 state.mode <- Items
(active, first
, makeitems (), "", pan
, mode)
1414 and boolp name get set
=
1415 Printf.sprintf
"%s\t%s" name
(btos (get
())), 1, Action
(
1416 fun active first qsearch pan
->
1419 Items
(active, first
, makeitems (), qsearch
, pan
, mode);
1421 and colorp name get set
=
1422 Printf.sprintf
"%s\t%s" name
(color_to_string (get
())), 1, Action
(
1423 fun active first _ pan
->
1424 let invalid = (nan
, nan
, nan
) in
1427 try color_of_string s
1429 state.text <- Printf.sprintf
"bad color `%s': %s"
1430 s (Printexc.to_string exn
);
1436 let te = name ^
": ", "", None
, textentry, ondone in
1441 state.mode <- Items
(active, first
, makeitems (), "", pan
, mode)
1446 let birdseye = isbirdseye mode in
1448 (if birdseye then "Setup bird's eye" else "Setup"), 0, Noaction
;
1450 boolp
"presentation"
1451 (fun () -> conf.presentation)
1453 conf.presentation <- v;
1454 state.anchor <- getanchor ();
1457 boolp
"ignore case in searches"
1458 (fun () -> conf.icase
)
1459 (fun v -> conf.icase
<- v);
1462 (fun () -> conf.preload)
1463 (fun v -> conf.preload <- v);
1466 (fun () -> conf.verbose
)
1467 (fun v -> conf.verbose
<- v);
1470 (fun () -> conf.maxhfit
)
1471 (fun v -> conf.maxhfit
<- v);
1474 (fun () -> conf.crophack
)
1475 (fun v -> conf.crophack
<- v);
1478 (fun () -> conf.showall
)
1479 (fun v -> conf.showall
<- v);
1481 boolp
"highlight links"
1482 (fun () -> conf.hlinks
)
1483 (fun v -> conf.hlinks
<- v);
1486 (fun () -> conf.underinfo
)
1487 (fun v -> conf.underinfo
<- v);
1488 boolp
"persistent bookmarks"
1489 (fun () -> conf.savebmarks
)
1490 (fun v -> conf.savebmarks
<- v);
1492 boolp
"proportional display"
1493 (fun () -> conf.proportional
)
1494 (fun v -> reinit conf.angle
v);
1496 boolp
"persistent location"
1497 (fun () -> conf.jumpback
)
1498 (fun v -> conf.jumpback
<- v);
1502 intp "vertical margin"
1503 (fun () -> conf.interpagespace)
1505 conf.interpagespace <- n;
1507 match state.layout with
1512 state.maxy <- calcheight ();
1513 let y = getpagey pageno in
1518 (fun () -> conf.pagebias
)
1519 (fun v -> conf.pagebias
<- v);
1522 (fun () -> conf.scrollstep
)
1523 (fun n -> conf.scrollstep
<- n);
1525 intp "auto scroll step"
1527 match state.autoscroll
with
1529 | _
-> conf.autoscrollstep
)
1531 if state.autoscroll
<> None
1532 then state.autoscroll
<- Some
n;
1533 conf.autoscrollstep
<- n);
1536 (fun () -> truncate
(conf.zoom *. 100.))
1537 (fun v -> setzoom ((float v) /. 100.));
1540 (fun () -> conf.angle
)
1541 (fun v -> reinit v conf.proportional
);
1543 intp "scroll bar width"
1544 (fun () -> state.scrollw
)
1548 reshape conf.winw
conf.winh
;
1551 intp "scroll handle height"
1552 (fun () -> conf.scrollh
)
1553 (fun v -> conf.scrollh
<- v;);
1555 intp "thumbnail width"
1556 (fun () -> conf.thumbw
)
1558 conf.thumbw
<- min
1920 v;
1561 leavebirdseye beye
false;
1566 colorp
"background color"
1567 (fun () -> conf.bgcolor
)
1568 (fun v -> conf.bgcolor
<- v);
1571 "Presentation mode", 0, Noaction
;
1573 boolp
"scrollbar visible"
1574 (fun () -> conf.scrollbarinpm
)
1576 if v != conf.scrollbarinpm
1578 conf.scrollbarinpm
<- v;
1579 if conf.presentation
1581 state.scrollw
<- if v then conf.scrollbw
else 0;
1582 reshape conf.winw
conf.winh
;
1588 "Pixmap Cache", 0, Noaction
;
1590 intp "size (advisory)"
1591 (fun () -> conf.memlimit
)
1592 (fun v -> conf.memlimit
<- v);
1593 Printf.sprintf
"%s\t%d" "used" state.memused
, 1, Noaction
;
1600 :: ("Document", 0, Noaction
)
1601 :: List.map
(fun (_
, s) -> (s, 1, Noaction
)) state.docinfo
1607 :: (Printf.sprintf
"Save these parameters as defaults at exit (%s)"
1608 (btos conf.bedefault
),
1611 fun active first qsearch pan
->
1612 conf.bedefault
<- not
conf.bedefault
;
1613 Items
(active, first
, makeitems (), qsearch
, pan
, mode);
1618 Array.of_list
(items @ tailer)
1621 state.mode <- Items
(1, 0, makeitems (), "", 0, mode);
1622 Glut.postRedisplay
();
1625 let enterhelpmode () =
1626 state.mode <- Items
(-1, 0, state.help
, "", 0, state.mode);
1627 Glut.postRedisplay
();
1630 let quickbookmark ?title
() =
1631 match state.layout with
1637 let sec = Unix.gettimeofday
() in
1638 let tm = Unix.localtime
sec in
1639 Printf.sprintf
"Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1643 (tm.Unix.tm_year
+ 1900)
1646 | Some
title -> title
1649 (title, 0, (l
.pageno, float l
.pagey /. float l
.pageh
))
1654 state.fullscreen
<- None
;
1655 Glut.reshapeWindow
w h;
1658 let writeopen path password
=
1659 writecmd state.csock
("open " ^ path ^
"\000" ^ password ^
"\000");
1662 let opendoc path password
=
1665 state.password
<- password
;
1666 state.gen
<- state.gen
+ 1;
1667 state.docinfo
<- [];
1669 writeopen path password
;
1670 Glut.setWindowTitle
("llpp " ^
Filename.basename path
);
1671 wcmd "geometry" [`i
state.w; `i
conf.winh
];
1674 let viewkeyboard key
=
1676 let mode = state.mode in
1677 state.mode <- Textentry
(te, fun _
-> state.mode <- mode);
1680 Glut.postRedisplay
()
1682 let c = Char.chr key
in
1684 | '
\027'
| 'q'
-> (* escape *)
1687 | '
\008'
-> (* backspace *)
1688 let y = getnav ~
-1 in
1689 gotoy_and_clear_text y
1697 Glut.postRedisplay
()
1700 let ondone isforw
s =
1701 cbput state.hists
.pat
s;
1702 state.searchpattern
<- s;
1705 let s = String.create
1 in
1707 enttext (s, "", Some
(onhist state.hists
.pat
),
1708 textentry, ondone (c ='
/'
))
1710 | '
+'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1711 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1712 setzoom (min
2.2 (conf.zoom +. incr))
1717 try int_of_string
s with exc
->
1718 state.text <- Printf.sprintf
"bad integer `%s': %s"
1719 s (Printexc.to_string exc
);
1725 state.text <- "page bias is now " ^ string_of_int
n;
1728 enttext ("page bias: ", "", None
, intentry, ondone)
1730 | '
-'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1731 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1732 setzoom (max
0.01 (conf.zoom -. decr))
1735 let ondone msg
= state.text <- msg
in
1737 "option [acfhilpstvAPRSZ]: ", "", None
,
1738 optentry state.mode, ondone
1741 | '
0'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1744 | '
1'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1745 let zoom = zoomforh
conf.winw
conf.winh
state.scrollw
in
1749 | '
9'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1755 try int_of_string
s with exc
->
1756 state.text <- Printf.sprintf
"bad integer `%s': %s"
1757 s (Printexc.to_string exc
);
1763 cbput state.hists
.pag
(string_of_int
n);
1764 gotoy_and_clear_text (getpagey (n + conf.pagebias
- 1))
1767 let pageentry text key
=
1768 match Char.unsafe_chr key
with
1769 | 'g'
-> TEdone
text
1770 | _
-> intentry text key
1772 let text = "x" in text.[0] <- c;
1773 enttext (":", text, Some
(onhist state.hists
.pag
), pageentry, ondone)
1776 state.scrollw
<- if state.scrollw
> 0 then 0 else conf.scrollbw
;
1777 reshape conf.winw
conf.winh
;
1780 conf.hlinks
<- not
conf.hlinks
;
1781 state.text <- "highlightlinks " ^
if conf.hlinks
then "on" else "off";
1782 Glut.postRedisplay
()
1785 begin match state.autoscroll
with
1787 conf.autoscrollstep
<- step
;
1788 state.autoscroll
<- None
1790 if conf.autoscrollstep
= 0
1791 then state.autoscroll
<- Some
1
1792 else state.autoscroll
<- Some
conf.autoscrollstep
1796 conf.presentation <- not
conf.presentation;
1797 if conf.presentation
1799 if not
conf.scrollbarinpm
1800 then state.scrollw
<- 0;
1803 state.scrollw
<- conf.scrollbw
;
1805 showtext ' '
("presentation mode " ^
1806 if conf.presentation then "on" else "off");
1807 state.anchor <- getanchor ();
1811 begin match state.fullscreen
with
1813 state.fullscreen
<- Some
(conf.winw
, conf.winh
);
1816 state.fullscreen
<- None
;
1821 gotoy_and_clear_text 0
1824 search state.searchpattern
true
1827 search state.searchpattern
false
1830 begin match state.layout with
1833 gotoy_and_clear_text (getpagey l
.pageno)
1837 begin match List.rev
state.layout with
1840 let pageno = min
(l
.pageno+1) (state.pagecount
-1) in
1841 gotoy_and_clear_text (getpagey pageno)
1844 | '
\127'
-> (* del *)
1845 begin match state.layout with
1848 let pageno = max
0 (l
.pageno-1) in
1849 gotoy_and_clear_text (getpagey pageno)
1854 if fn
= -1 then l
.pageno, l
.pageno else fn
, l
.pageno
1856 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
1858 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
1862 else (100. *. (float state.y /. float maxy)) in
1865 Printf.sprintf
"Page %d of %d %.2f%%"
1866 (fn+1) state.pagecount
percent
1869 "Pages %d-%d of %d %.2f%%"
1870 (fn+1) (ln
+1) state.pagecount
percent
1875 begin match state.layout with
1878 doreshape (l
.pagew
+ state.scrollw
) l
.pageh
;
1879 Glut.postRedisplay
();
1883 enterbookmarkmode ()
1893 match state.layout with
1896 (s, 0, (l
.pageno, float l
.pagey /. float l
.pageh
))
1900 enttext ("bookmark: ", "", None
, textentry, ondone)
1904 showtext ' '
"Quick bookmark added";
1907 begin match state.layout with
1909 let rect = getpdimrect l
.pagedimno
in
1913 (truncate
(1.8 *. (rect.(1) -. rect.(0))),
1914 truncate
(1.2 *. (rect.(3) -. rect.(0))))
1916 (truncate
(rect.(1) -. rect.(0)),
1917 truncate
(rect.(3) -. rect.(0)))
1921 doreshape (w + state.scrollw
) (h + conf.interpagespace)
1923 Glut.postRedisplay
();
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 | '
\027'
-> (* escape *)
1983 begin match opthist
with
1985 | Some
(_
, onhistcancel
) -> onhistcancel
()
1988 Glut.postRedisplay
()
1991 begin match onkey
text key
with
1995 Glut.postRedisplay
()
1998 enttext (c, text, opthist
, onkey
, ondone);
2002 Glut.postRedisplay
()
2005 state.mode <- Textentry
(te, onleave
);
2006 Glut.postRedisplay
()
2010 let birdseyekeyboard key
((_
, _
, pageno, _
, _
) as beye
) =
2012 | 27 -> (* escape *)
2013 leavebirdseye beye
true
2015 | 12 -> (* ctrl-l *)
2016 let y, h = getpageyh pageno in
2017 let top = (conf.winh
- h) / 2 in
2018 gotoy (max
0 (y - top))
2021 leavebirdseye beye
false
2027 let itemskeyboard key
(active, first
, items, qsearch
, pan
, oldmode
) =
2028 let set active first qsearch
=
2029 state.mode <- Items
(active, first
, items, qsearch
, pan
, oldmode
)
2031 let search active pattern
incr =
2034 if n >= Array.length
items || n < 0
2037 let (s, _
, _
) = items.(n) in
2039 (try ignore
(Str.search_forward re
s 0); true
2040 with Not_found
-> false)
2042 else loop (n + incr)
2047 let re = Str.regexp_case_fold pattern
in
2053 let firstof active = max
0 (active - maxoutlinerows () / 2) in
2055 | 18 | 19 -> (* ctrl-r/ctlr-s *)
2056 let incr = if key
= 18 then -1 else 1 in
2058 match search (active + incr) qsearch
incr with
2060 state.text <- qsearch ^
" [not found]";
2063 state.text <- qsearch
;
2064 active, firstof active
2066 set active first qsearch
;
2067 Glut.postRedisplay
();
2069 | 8 -> (* backspace *)
2070 let len = String.length qsearch
in
2077 set active first
"";
2080 let qsearch = String.sub
qsearch 0 (len - 1) in
2082 match search active qsearch ~
-1 with
2084 state.text <- qsearch ^
" [not found]";
2087 state.text <- qsearch;
2088 active, firstof active
2090 set active first
qsearch
2092 Glut.postRedisplay
()
2094 | _
when key
>= 32 && key
< 127 ->
2095 let pattern = addchar qsearch (Char.chr key
) in
2097 match search active pattern 1 with
2099 state.text <- pattern ^
" [not found]";
2102 state.text <- pattern;
2103 active, firstof active
2105 set active first
pattern;
2106 Glut.postRedisplay
()
2108 | 27 -> (* escape *)
2110 if String.length
qsearch = 0
2112 state.mode <- oldmode
;
2115 set active first
"";
2117 Glut.postRedisplay
()
2120 if active >= 0 && active < Array.length
items
2122 match items.(active) with
2124 state.mode <- f active first
qsearch pan
2128 state.mode <- oldmode
2132 state.mode <- oldmode
2134 Glut.postRedisplay
();
2136 | _
-> dolog "unknown key %d" key
2139 let outlinekeyboard key
2140 (allowdel
, active, first
, outlines, qsearch, pan
, oldmode
) =
2141 let narrow outlines pattern =
2142 let reopt = try Some
(Str.regexp_case_fold
pattern) with _
-> None
in
2146 let rec fold accu n =
2150 let (s, _
, _
) as o
= outlines.(n) in
2152 if (try ignore
(Str.search_forward
re s 0); true
2153 with Not_found
-> false)
2159 let matched = fold [] (Array.length
outlines - 1) in
2160 if matched = [] then None
else Some
(Array.of_list
matched)
2162 let search active pattern incr =
2165 if n = Array.length
outlines || n = -1
2168 let (s, _
, _
) = outlines.(n) in
2170 (try ignore
(Str.search_forward
re s 0); true
2171 with Not_found
-> false)
2173 else loop (n + incr)
2178 let re = Str.regexp_case_fold
pattern in
2184 let firstof active = max
0 (active - maxoutlinerows () / 2) in
2186 | 27 -> (* escape *)
2188 if String.length
qsearch = 0
2190 state.mode <- oldmode
;
2193 state.mode <- Outline
(
2194 allowdel
, active, first
, outlines, "", pan
, oldmode
2197 Glut.postRedisplay
();
2199 | 18 | 19 -> (* ctrl-r/ctrl-s *)
2200 let incr = if key
= 18 then -1 else 1 in
2202 match search (active + incr) qsearch incr with
2204 state.text <- qsearch ^
" [not found]";
2207 state.text <- qsearch;
2208 active, firstof active
2210 state.mode <- Outline
(
2211 allowdel
, active, first
, outlines, qsearch, pan
, oldmode
2213 Glut.postRedisplay
();
2215 | 8 -> (* backspace *)
2216 let len = String.length
qsearch in
2223 state.mode <- Outline
(
2224 allowdel
, active, first
, outlines, "", pan
, oldmode
2228 let qsearch = String.sub
qsearch 0 (len - 1) in
2230 match search active qsearch ~
-1 with
2232 state.text <- qsearch ^
" [not found]";
2235 state.text <- qsearch;
2236 active, firstof active
2238 state.mode <- Outline
(
2239 allowdel
, active, first
, outlines, qsearch, pan
, oldmode
2242 Glut.postRedisplay
()
2245 if active < Array.length
outlines
2247 let (_
, _
, anchor) = outlines.(active) in
2252 if allowdel
then state.bookmarks <- Array.to_list
outlines;
2253 state.mode <- oldmode
;
2254 Glut.postRedisplay
();
2256 | _
when key
>= 32 && key
< 127 ->
2257 let pattern = addchar qsearch (Char.chr key
) in
2259 match search active pattern 1 with
2261 state.text <- pattern ^
" [not found]";
2264 state.text <- pattern;
2265 active, firstof active
2267 state.mode <- Outline
(
2268 allowdel
, active, first
, outlines, pattern, pan
, oldmode
2270 Glut.postRedisplay
()
2272 | 14 when not allowdel
-> (* ctrl-n *)
2273 if String.length
qsearch > 0
2275 let optoutlines = narrow outlines qsearch in
2276 begin match optoutlines with
2277 | None
-> state.text <- "can't narrow"
2279 state.mode <- Outline
(
2280 allowdel
, 0, 0, outlines, qsearch, pan
, oldmode
2282 match state.outlines with
2285 state.outlines <- Onarrow
(qsearch, outlines, a)
2286 | Onarrow
(_
, _
, b) ->
2287 state.outlines <- Onarrow
(qsearch, outlines, b)
2290 Glut.postRedisplay
()
2292 | 21 when not allowdel
-> (* ctrl-u *)
2294 match state.outlines with
2297 let a = Array.of_list
(List.rev l
) in
2298 state.outlines <- Oarray
a;
2300 | Onarrow
(_
, _
, b) ->
2301 state.outlines <- Oarray
b;
2305 state.mode <- Outline
(allowdel
, 0, 0, outline, qsearch, pan
, oldmode
);
2306 Glut.postRedisplay
()
2308 | 12 -> (* ctrl-l *)
2309 state.mode <- Outline
2310 (allowdel
, active, firstof active, outlines, qsearch, pan
, oldmode
);
2311 Glut.postRedisplay
()
2313 | 127 when allowdel
-> (* delete *)
2314 let len = Array.length
outlines - 1 in
2318 state.bookmarks <- [];
2321 let bookmarks = Array.init
len
2323 let i = if i >= active then i + 1 else i in
2337 Glut.postRedisplay
()
2339 | _
-> dolog "unknown key %d" key
2342 let keyboard ~key ~x ~
y =
2345 if key
= 7 (* ctrl-g *)
2349 match state.mode with
2350 | Outline
outline -> outlinekeyboard key
outline
2351 | Textentry
textentry -> textentrykeyboard key
textentry
2352 | Birdseye
birdseye -> birdseyekeyboard key
birdseye
2353 | View
-> viewkeyboard key
2354 | Items
items -> itemskeyboard key
items
2357 let birdseyespecial key
((conf, leftx
, _
, hooverpageno
, anchor) as beye
) =
2359 | Glut.KEY_UP
-> upbirdseye beye
2360 | Glut.KEY_DOWN
-> downbirdseye beye
2362 | Glut.KEY_PAGE_UP
->
2363 begin match state.layout with
2367 state.mode <- Birdseye
(
2368 conf, leftx
, l
.pageno, hooverpageno
, anchor
2370 gotopage1 l
.pageno 0;
2373 let layout = layout (state.y-conf.winh
) conf.winh
in
2375 | [] -> gotoy (clamp (-conf.winh
))
2377 state.mode <- Birdseye
(
2378 conf, leftx
, l
.pageno, hooverpageno
, anchor
2380 gotopage1 l
.pageno 0
2383 | [] -> gotoy (clamp (-conf.winh
))
2386 | Glut.KEY_PAGE_DOWN
->
2387 begin match List.rev
state.layout with
2389 let layout = layout (state.y + conf.winh
) conf.winh
in
2390 begin match layout with
2392 let incr = l
.pageh
- l
.pagevh in
2397 conf, leftx
, state.pagecount
- 1, hooverpageno
, anchor
2399 Glut.postRedisplay
();
2401 else gotoy (clamp (incr + conf.interpagespace*2));
2405 Birdseye
(conf, leftx
, l
.pageno, hooverpageno
, anchor);
2406 gotopage1 l
.pageno 0;
2409 | [] -> gotoy (clamp conf.winh
)
2413 state.mode <- Birdseye
(conf, leftx
, 0, hooverpageno
, anchor);
2417 let pageno = state.pagecount
- 1 in
2418 state.mode <- Birdseye
(conf, leftx
, pageno, hooverpageno
, anchor);
2419 if not
(pagevisible state.layout pageno)
2422 match List.rev
state.pdims
with
2424 | (_
, _
, h, _
) :: _
-> h
2426 gotoy (max
0 (getpagey pageno - (conf.winh
- h - conf.interpagespace)))
2427 else Glut.postRedisplay
();
2431 let setautoscrollspeed step goingdown
=
2432 let incr = max
1 ((abs step
) / 2) in
2433 let incr = if goingdown
then incr else -incr in
2434 let astep = step
+ incr in
2435 state.autoscroll
<- Some
astep;
2438 let special ~key ~x ~
y =
2441 match state.mode with
2442 | View
| (Birdseye _
) when key
= Glut.KEY_F9
->
2446 birdseyespecial key vals
2448 | View
when key
= Glut.KEY_F1
->
2452 begin match state.autoscroll
with
2453 | Some step
when key
= Glut.KEY_DOWN
|| key
= Glut.KEY_UP
->
2454 setautoscrollspeed step
(key
= Glut.KEY_DOWN
)
2459 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
2460 | Glut.KEY_UP
-> clamp (-conf.scrollstep
)
2461 | Glut.KEY_DOWN
-> clamp conf.scrollstep
2462 | Glut.KEY_PAGE_UP
->
2463 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2465 match state.layout with
2467 | l
:: _
-> state.y - l
.pagey
2470 | Glut.KEY_PAGE_DOWN
->
2471 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2473 match List.rev
state.layout with
2475 | l
:: _
-> getpagey l
.pageno
2478 | Glut.KEY_HOME
-> addnav (); 0
2481 state.maxy - (if conf.maxhfit
then conf.winh
else 0)
2483 | (Glut.KEY_RIGHT
| Glut.KEY_LEFT
) when
2484 Glut.getModifiers
() land Glut.active_alt
!= 0 ->
2485 getnav (if key
= Glut.KEY_LEFT
then 1 else -1)
2487 | Glut.KEY_RIGHT
when conf.zoom > 1.0 ->
2488 state.x
<- state.x
- 10;
2490 | Glut.KEY_LEFT
when conf.zoom > 1.0 ->
2491 state.x
<- state.x
+ 10;
2496 gotoy_and_clear_text y
2500 ((c, _
, (Some
(action, _
) as onhist), onkey
, ondone), mode) ->
2503 | Glut.KEY_UP
-> action HCprev
2504 | Glut.KEY_DOWN
-> action HCnext
2505 | Glut.KEY_HOME
-> action HCfirst
2506 | Glut.KEY_END
-> action HClast
2509 state.mode <- Textentry
((c, s, onhist, onkey
, ondone), mode);
2510 Glut.postRedisplay
()
2514 | Items
(active, first
, items, qsearch, pan
, oldmode
) ->
2515 let maxrows = maxoutlinerows () in
2516 let itemcount = Array.length
items in
2517 let hasaction = function
2518 | (_
, _
, Noaction
) -> false
2521 let find start
incr =
2523 if i = -1 || i = itemcount
2526 if hasaction items.(i)
2528 else find (i + incr)
2533 let set active first
=
2534 let first = max
0 (min
first (itemcount - maxrows)) in
2535 state.mode <- Items
(active, first, items, qsearch, pan
, oldmode
)
2538 let isvisible first n = n >= first && n - first <= maxrows in
2540 let incr1 = if incr > 0 then 1 else -1 in
2541 if isvisible first active
2544 let next = active + incr in
2546 if next < 0 || next >= itemcount
2548 else find next incr1
2550 if next = -1 || abs
(active - next) > maxrows
2556 let first = first + incr in
2557 let first = max
0 (min
first (itemcount - 1)) in
2559 let next = active + incr in
2560 let next = max
0 (min
next (itemcount - 1)) in
2563 let active = if next = -1 then active else next in
2566 let first = min
next first in
2569 let first = first + incr in
2570 let first = max
0 (min
first (itemcount - 1)) in
2572 let next = active + incr in
2573 let next = max
0 (min
next (itemcount - 1)) in
2574 let next = find next incr1 in
2575 if next = -1 || abs
(active - first) > maxrows
2582 Glut.postRedisplay
()
2584 begin match key
with
2585 | Glut.KEY_UP
-> navigate ~
-1
2586 | Glut.KEY_DOWN
-> navigate 1
2587 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
2588 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
2591 state.mode <- Items
(
2592 active, first, items, qsearch, min
0 (pan
- 1), oldmode
2594 Glut.postRedisplay
()
2597 state.mode <- Items
(
2598 active, first, items, qsearch, min
0 (pan
+ 1), oldmode
2600 Glut.postRedisplay
()
2603 let active = find 0 1 in
2605 Glut.postRedisplay
()
2608 let first = max
0 (itemcount - maxrows) in
2609 let active = find (itemcount - 1) ~
-1 in
2611 Glut.postRedisplay
()
2616 | Outline
(allowdel
, active, first, outlines, qsearch, pan
, oldmode
) ->
2617 let maxrows = maxoutlinerows () in
2618 let calcfirst first active =
2621 let rows = active - first in
2622 if rows > maxrows then active - maxrows else first
2626 let active = active + incr in
2627 let active = max
0 (min
active (Array.length
outlines - 1)) in
2628 let first = calcfirst first active in
2629 state.mode <- Outline
(
2630 allowdel
, active, first, outlines, qsearch, pan
, oldmode
2632 Glut.postRedisplay
()
2634 let updownlevel incr =
2635 let len = Array.length
outlines in
2636 let (_
, curlevel
, _
) = outlines.(active) in
2638 if i = len then i-1 else if i = -1 then 0 else
2639 let (_
, l
, _
) = outlines.(i) in
2640 if l
!= curlevel
then i else flow (i+incr)
2642 let active = flow active in
2643 let first = calcfirst first active in
2644 state.mode <- Outline
(
2645 allowdel
, active, first, outlines, qsearch, pan
, oldmode
2647 Glut.postRedisplay
()
2650 | Glut.KEY_UP
-> navigate ~
-1
2651 | Glut.KEY_DOWN
-> navigate 1
2652 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
2653 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
2656 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2658 state.mode <- Outline
(
2659 allowdel
, active, first, outlines,
2660 qsearch, min
0 (pan
+ 1), oldmode
2662 Glut.postRedisplay
();
2670 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2672 state.mode <- Outline
(
2673 allowdel
, active, first, outlines, qsearch, pan
- 1, oldmode
2675 Glut.postRedisplay
();
2679 then updownlevel ~
-1
2683 state.mode <- Outline
(
2684 allowdel
, 0, 0, outlines, qsearch, pan
, oldmode
2686 Glut.postRedisplay
()
2689 let active = Array.length
outlines - 1 in
2690 let first = max
0 (active - maxrows) in
2691 state.mode <- Outline
(
2692 allowdel
, active, first, outlines, qsearch, pan
, oldmode
2694 Glut.postRedisplay
()
2699 let drawplaceholder l
=
2700 let margin = state.x
+ (conf.winw
- (state.w + state.scrollw
)) / 2 in
2702 (float l
.pagex
, float l
.pagedispy
)
2703 (float (l
.pagew
+ l
.pagex
), float (l
.pagedispy
+ l
.pagevh))
2705 let x = if margin < 0 then -margin else l
.pagex
2706 and y = l
.pagedispy
+ 13 in
2707 GlDraw.color
(0.0, 0.0, 0.0);
2708 drawstring 13 x y ("Loading " ^ string_of_int
(l
.pageno + 1))
2711 let now () = Unix.gettimeofday
();;
2715 match state.mode with
2716 | Textentry _
-> scalecolor 0.4
2717 | View
| Outline _
| Items _
-> scalecolor 1.0
2718 | Birdseye
(_
, _
, pageno, hooverpageno
, _
) ->
2719 if l
.pageno = hooverpageno
2722 if l
.pageno = pageno
2728 begin match getopaque l
.pageno with
2729 | Some
(opaque, _
) when validopaque opaque ->
2731 draw
(l
.pagedispy
, l
.pagew
, l
.pagevh, l
.pagey, conf.hlinks
)
2735 vlog "draw %d %f sec" l
.pageno d;
2743 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
2744 let sh = (float (maxy + conf.winh
) /. float conf.winh
) in
2745 let sh = float conf.winh
/. sh in
2746 let sh = max
sh (float conf.scrollh
) in
2749 if state.y = state.maxy
2751 else float y /. float maxy
2753 let position = (float conf.winh
-. sh) *. percent in
2756 if position +. sh > float conf.winh
2757 then float conf.winh
-. sh
2763 let scrollindicator () =
2764 GlDraw.color (0.64 , 0.64, 0.64);
2766 (float (conf.winw
- state.scrollw
), 0.)
2767 (float conf.winw
, float conf.winh
)
2769 GlDraw.color (0.0, 0.0, 0.0);
2771 let position, sh = scrollph state.y in
2773 (float (conf.winw
- state.scrollw
), position)
2774 (float conf.winw
, position +. sh)
2778 let showsel margin =
2779 match state.mstate
with
2780 | Mnone
| Mscroll _
| Mpan _
| Mzoom _
->
2783 | Msel
((x0
, y0
), (x1
, y1
)) ->
2784 let rec loop = function
2786 if (y0
>= l
.pagedispy
&& y0
<= (l
.pagedispy
+ l
.pagevh))
2787 || ((y1
>= l
.pagedispy
&& y1
<= (l
.pagedispy
+ l
.pagevh)))
2789 match getopaque l
.pageno with
2790 | Some
(opaque, _
) when validopaque opaque ->
2791 let oy = -l
.pagey + l
.pagedispy
in
2793 (x0
- margin - state.x, y0
,
2794 x1
- margin - state.x, y1
) oy;
2804 let panx = float state.x in
2806 GlDraw.color (0.0, 0.0, 1.0) ~alpha
:0.5;
2807 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2809 (fun (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) ->
2811 if l
.pageno = pageno
2813 let d = float (l
.pagedispy
- l
.pagey) in
2814 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
2815 GlDraw.begins `quads
;
2817 GlDraw.vertex2
(x0
+.panx, y0
+.d);
2818 GlDraw.vertex2
(x1
+.panx, y1
+.d);
2819 GlDraw.vertex2
(x2
+.panx, y2
+.d);
2820 GlDraw.vertex2
(x3
+.panx, y3
+.d);
2830 let showstrings trusted
active first pan
strings =
2832 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2833 GlDraw.color (0., 0., 0.) ~alpha
:0.85;
2834 GlDraw.rect (0., 0.) (float conf.winw
, float conf.winh
);
2835 GlDraw.color (1., 1., 1.);
2836 Gl.enable `texture_2d
;
2838 let wx = measurestr
15 "w" in
2839 let tabx = 30.0*.wx +. float (pan
*15) in
2841 if row
= Array.length
strings || (row
- first) * 16 > conf.winh
2844 let (s, level
, _
) = strings.(row
) in
2845 let y = (row
- first) * 16 in
2846 let x = 5 + 15*(max
0 (level
+pan
)) in
2849 Gl.disable `texture_2d
;
2850 GlDraw.polygon_mode `both `line
;
2851 GlDraw.color (1., 1., 1.) ~alpha
:0.9;
2852 GlDraw.rect (0., float (y + 1))
2853 (float (conf.winw
- 1), float (y + 18));
2854 GlDraw.polygon_mode `both `fill
;
2855 GlDraw.color (1., 1., 1.);
2856 Gl.enable `texture_2d
;
2859 let drawtabularstring x s =
2863 let tabpos = try String.index
s '
\t'
with Not_found
-> -1 in
2866 let len = String.length
s - tabpos - 1 in
2867 let s1 = String.sub
s 0 tabpos
2868 and s2
= String.sub
s (tabpos + 1) len in
2869 let xx = wx +. drawstring1 14 x (y + 16) s1 in
2870 let x = truncate
(max
xx tabx) in
2871 drawstring1 15 x (y + 16) s2
2873 drawstring1 15 x (y + 16) s
2875 drawstring1 15 x (y + 16) s
2879 drawtabularstring (x + pan
*15) s;
2885 Gl.disable `texture_2d
;
2888 let showoutline (_, active, first, outlines, _, pan
, _) =
2889 showstrings false active first pan
outlines;
2892 let showitems (active, first, items, _, pan
, _) =
2893 showstrings true active first pan
items;
2897 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
2898 GlDraw.viewport
margin 0 state.w conf.winh
;
2900 GlClear.color (scalecolor2 conf.bgcolor
);
2901 GlClear.clear
[`
color];
2904 Gl.enable `scissor_test
;
2905 GlMisc.scissor
0 0 (conf.winw
- state.scrollw
) conf.winh
;
2907 List.iter
drawpage state.layout;
2910 Gl.disable `scissor_test
2914 let x = -.float state.x in
2915 GlMat.translate ~
x ();
2919 GlDraw.viewport
0 0 conf.winw
conf.winh
;
2922 begin match state.mode with
2923 | Items
items -> showitems items
2924 | Outline
outline -> showoutline outline
2928 Glut.swapBuffers
();
2932 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
2933 let x = x - margin - state.x in
2934 let rec f = function
2936 begin match getopaque l
.pageno with
2937 | Some
(opaque, _) when validopaque opaque ->
2938 let y = y - l
.pagedispy
in
2941 let y = l
.pagey + y in
2942 let x = x - l
.pagex
in
2943 match whatsunder
opaque x y with
2956 let viewmouse button bstate
x y =
2958 | Glut.OTHER_BUTTON
n when (n == 3 || n == 4) && bstate
= Glut.UP
->
2959 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2961 match state.mstate
with
2962 | Mzoom
(oldn
, i) ->
2970 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
2972 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
2974 let zoom = conf.zoom +. incr in
2976 state.mstate
<- Mzoom
(n, 0);
2978 state.mstate
<- Mzoom
(n, i+1);
2980 else state.mstate
<- Mzoom
(n, 0)
2982 | _ -> state.mstate
<- Mzoom
(n, 0)
2985 match state.autoscroll
with
2986 | Some step
-> setautoscrollspeed step
(n=4)
2990 then -conf.scrollstep
2991 else conf.scrollstep
2993 let incr = incr * 2 in
2994 let y = clamp incr in
2995 gotoy_and_clear_text y
2998 | Glut.LEFT_BUTTON
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
2999 if bstate
= Glut.DOWN
3001 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
3002 state.mstate
<- Mpan
(x, y)
3005 state.mstate
<- Mnone
3007 | Glut.LEFT_BUTTON
when x > conf.winw
- state.scrollw
->
3008 if bstate
= Glut.DOWN
3010 let position, sh = scrollph state.y in
3011 if y > truncate
position && y < truncate
(position +. sh)
3013 state.mstate
<- Mscroll
3015 let percent = float y /. float conf.winh
in
3016 let desty = truncate
(float (state.maxy - conf.winh
) *. percent) in
3018 state.mstate
<- Mscroll
3020 state.mstate
<- Mnone
3022 | Glut.LEFT_BUTTON
->
3023 let dest = if bstate
= Glut.DOWN
then getunder x y else Unone
in
3024 begin match dest with
3025 | Ulinkgoto
(pageno, top) ->
3029 gotopage1 pageno top;
3035 | Unone
when bstate
= Glut.DOWN
->
3036 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
3037 state.mstate
<- Mpan
(x, y);
3039 | Unone
| Utext
_ ->
3040 if bstate
= Glut.DOWN
3042 if conf.angle
mod 360 = 0
3044 state.mstate
<- Msel
((x, y), (x, y));
3045 Glut.postRedisplay
()
3049 match state.mstate
with
3052 | Mzoom
_ | Mscroll
->
3053 state.mstate
<- Mnone
3056 Glut.setCursor
Glut.CURSOR_INHERIT
;
3057 state.mstate
<- Mnone
3059 | Msel
((_, y0
), (_, y1
)) ->
3061 if (y0
>= l
.pagedispy
&& y0
<= (l
.pagedispy
+ l
.pagevh))
3062 || ((y1
>= l
.pagedispy
&& y1
<= (l
.pagedispy
+ l
.pagevh)))
3064 match getopaque l
.pageno with
3065 | Some
(opaque, _) when validopaque opaque ->
3069 List.iter
f state.layout;
3070 copysel
""; (* ugly *)
3071 Glut.setCursor
Glut.CURSOR_INHERIT
;
3072 state.mstate
<- Mnone
;
3079 let birdseyemouse button bstate
x y
3080 (conf, leftx
, _, hooverpageno
, anchor) =
3082 | Glut.LEFT_BUTTON
when bstate
= Glut.UP
->
3083 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
3084 let rec loop = function
3087 if y > l
.pagedispy
&& y < l
.pagedispy
+ l
.pagevh
3088 && x > margin && x < margin + l
.pagew
3090 leavebirdseye (conf, leftx
, l
.pageno, hooverpageno
, anchor) false;
3095 | Glut.OTHER_BUTTON
_ -> viewmouse button bstate
x y
3099 let mouse bstate button
x y =
3100 match state.mode with
3101 | View
-> viewmouse button bstate
x y
3102 | Birdseye beye
-> birdseyemouse button bstate
x y beye
3103 | Textentry
_ | Outline
_ | Items
_ -> ()
3106 let mouse ~button ~
state ~
x ~
y = mouse state button
x y;;
3109 match state.mode with
3112 match state.mstate
with
3113 | Mzoom
_ | Mnone
-> ()
3118 state.mstate
<- Mpan
(x, y);
3119 if conf.zoom > 1.0 then state.x <- state.x + dx;
3121 gotoy_and_clear_text y
3124 state.mstate
<- Msel
(a, (x, y));
3125 Glut.postRedisplay
()
3128 let y = min
conf.winh
(max
0 y) in
3129 let percent = float y /. float conf.winh
in
3130 let y = truncate
(float (state.maxy - conf.winh
) *. percent) in
3131 gotoy_and_clear_text y
3135 match state.mode with
3136 | Birdseye
(conf, leftx
, pageno, hooverpageno
, anchor) ->
3137 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
3138 let rec loop = function
3140 if hooverpageno
!= -1
3142 state.mode <- Birdseye
(conf, leftx
, pageno, -1, anchor);
3143 Glut.postRedisplay
();
3146 if y > l
.pagedispy
&& y < l
.pagedispy
+ l
.pagevh
3147 && x > margin && x < margin + l
.pagew
3149 state.mode <- Birdseye
(conf, leftx
, pageno, l
.pageno, anchor);
3150 Glut.postRedisplay
();
3156 | Outline
_ | Items
_ | Textentry
_ -> ()
3158 match state.mstate
with
3160 begin match getunder x y with
3161 | Unone
-> Glut.setCursor
Glut.CURSOR_INHERIT
3163 if conf.underinfo
then showtext 'u'
("ri: " ^ uri
);
3164 Glut.setCursor
Glut.CURSOR_INFO
3165 | Ulinkgoto
(page
, _) ->
3167 then showtext 'p'
("age: " ^ string_of_int page
);
3168 Glut.setCursor
Glut.CURSOR_INFO
3170 if conf.underinfo
then showtext '
f'
("ont: " ^
s);
3171 Glut.setCursor
Glut.CURSOR_TEXT
3174 | Mpan
_ | Msel
_ | Mzoom
_ | Mscroll
->
3185 match Sys.os_type
with
3186 | "Win32" -> Sys.getenv
"HOMEPATH"
3187 | _ -> Sys.getenv
"HOME"
3190 ("Can not determine home directory location: " ^
3191 Printexc.to_string exn
);
3195 let config_of c attrs
=
3199 | "scroll-bar-width" -> { c with scrollbw
= max
0 (int_of_string
v) }
3200 | "scroll-handle-height" -> { c with scrollh
= max
0 (int_of_string
v) }
3201 | "case-insensitive-search" -> { c with icase
= bool_of_string
v }
3202 | "preload" -> { c with preload = bool_of_string
v }
3203 | "page-bias" -> { c with pagebias
= int_of_string
v }
3204 | "scroll-step" -> { c with scrollstep
= max
1 (int_of_string
v) }
3205 | "auto-scroll-step" ->
3206 { c with autoscrollstep
= max
0 (int_of_string
v) }
3207 | "max-height-fit" -> { c with maxhfit
= bool_of_string
v }
3208 | "crop-hack" -> { c with crophack
= bool_of_string
v }
3209 | "throttle" -> { c with showall
= bool_of_string
v }
3210 | "highlight-links" -> { c with hlinks
= bool_of_string
v }
3211 | "under-cursor-info" -> { c with underinfo
= bool_of_string
v }
3212 | "vertical-margin" ->
3213 { c with interpagespace = max
0 (int_of_string
v) }
3215 let zoom = float_of_string
v /. 100. in
3216 let zoom = max
0.01 (min
2.2 zoom) in
3217 { c with zoom = zoom }
3218 | "presentation" -> { c with presentation = bool_of_string
v }
3219 | "rotation-angle" -> { c with angle
= int_of_string
v }
3220 | "width" -> { c with winw
= max
20 (int_of_string
v) }
3221 | "height" -> { c with winh
= max
20 (int_of_string
v) }
3222 | "persistent-bookmarks" -> { c with savebmarks
= bool_of_string
v }
3223 | "proportional-display" -> { c with proportional
= bool_of_string
v }
3224 | "pixmap-cache-size" -> { c with memlimit
= max
2 (int_of_string
v) }
3225 | "tex-count" -> { c with texcount
= max
1 (int_of_string
v) }
3226 | "slice-height" -> { c with sliceheight
= max
2 (int_of_string
v) }
3227 | "thumbnail-width" -> { c with thumbw
= max
2 (int_of_string
v) }
3228 | "persistent-location" -> { c with jumpback
= bool_of_string
v }
3229 | "background-color" -> { c with bgcolor
= color_of_string v }
3230 | "scrollbar-in-presentation" ->
3231 { c with scrollbarinpm
= bool_of_string
v }
3232 | "ui-font" -> { c with uifont
= v }
3235 prerr_endline
("Error processing attribute (`" ^
3236 k ^
"'=`" ^
v ^
"'): " ^
Printexc.to_string exn
);
3239 let rec fold c = function
3242 let c = apply c k
v in
3248 let fromstring f pos
n v d =
3251 dolog "Error processing attribute (%S=%S) at %d\n%s"
3252 n v pos
(Printexc.to_string exn
)
3257 let bookmark_of attrs
=
3258 let rec fold title page rely
= function
3259 | ("title", v) :: rest
-> fold v page rely rest
3260 | ("page", v) :: rest
-> fold title v rely rest
3261 | ("rely", v) :: rest
-> fold title page
v rest
3262 | _ :: rest
-> fold title page rely rest
3263 | [] -> title, page
, rely
3265 fold "invalid" "0" "0" attrs
3269 let rec fold path page rely pan
= function
3270 | ("path", v) :: rest
-> fold v page rely pan rest
3271 | ("page", v) :: rest
-> fold path
v rely pan rest
3272 | ("rely", v) :: rest
-> fold path page
v pan rest
3273 | ("pan", v) :: rest
-> fold path page rely
v rest
3274 | _ :: rest
-> fold path page rely pan rest
3275 | [] -> path
, page
, rely
, pan
3277 fold "" "0" "0" "0" attrs
3280 let setconf dst src
=
3281 dst
.scrollbw
<- src
.scrollbw
;
3282 dst
.scrollh
<- src
.scrollh
;
3283 dst
.icase
<- src
.icase
;
3284 dst
.preload <- src
.preload;
3285 dst
.pagebias
<- src
.pagebias
;
3286 dst
.verbose
<- src
.verbose
;
3287 dst
.scrollstep
<- src
.scrollstep
;
3288 dst
.maxhfit
<- src
.maxhfit
;
3289 dst
.crophack
<- src
.crophack
;
3290 dst
.autoscrollstep
<- src
.autoscrollstep
;
3291 dst
.showall
<- src
.showall
;
3292 dst
.hlinks
<- src
.hlinks
;
3293 dst
.underinfo
<- src
.underinfo
;
3294 dst
.interpagespace <- src
.interpagespace;
3295 dst
.zoom <- src
.zoom;
3296 dst
.presentation <- src
.presentation;
3297 dst
.angle
<- src
.angle
;
3298 dst
.winw
<- src
.winw
;
3299 dst
.winh
<- src
.winh
;
3300 dst
.savebmarks
<- src
.savebmarks
;
3301 dst
.memlimit
<- src
.memlimit
;
3302 dst
.proportional
<- src
.proportional
;
3303 dst
.texcount
<- src
.texcount
;
3304 dst
.sliceheight
<- src
.sliceheight
;
3305 dst
.thumbw
<- src
.thumbw
;
3306 dst
.jumpback
<- src
.jumpback
;
3307 dst
.bgcolor
<- src
.bgcolor
;
3308 dst
.scrollbarinpm
<- src
.scrollbarinpm
;
3309 dst
.uifont
<- src
.uifont
;
3313 let l = String.length
s in
3314 let b = Buffer.create
l in
3320 let h = Hashtbl.create
10 in
3321 let dc = { defconf with angle
= defconf.angle
} in
3322 let rec toplevel v t spos
_ =
3324 | Vdata
| Vcdata
| Vend
-> v
3325 | Vopen
("llppconfig", _, closed
) ->
3328 else { v with f = llppconfig
}
3330 error
"unexpected subelement at top level" s spos
3331 | Vclose
_ -> error
"unexpected close at top level" s spos
3333 and llppconfig
v t spos
_ =
3335 | Vdata
| Vcdata
| Vend
-> v
3336 | Vopen
("defaults", attrs
, closed
) ->
3337 let c = config_of dc attrs
in
3341 else { v with f = skip
"defaults" (fun () -> v) }
3343 | Vopen
("doc", attrs
, closed
) ->
3344 let pathent, spage
, srely
, span
= doc_of attrs
in
3345 let path = unent pathent
3346 and pageno = fromstring int_of_string spos
"page" spage
0
3347 and rely
= fromstring float_of_string spos
"rely" srely
0.0
3348 and pan
= fromstring int_of_string spos
"pan" span
0 in
3349 let c = config_of dc attrs
in
3350 let anchor = (pageno, rely
) in
3352 then (Hashtbl.add
h path (c, [], pan
, anchor); v)
3353 else { v with f = doc
path pan
anchor c [] }
3356 error
"unexpected subelement in llppconfig" s spos
3358 | Vclose
"llppconfig" -> { v with f = toplevel }
3359 | Vclose
_ -> error
"unexpected close in llppconfig" s spos
3361 and doc
path pan
anchor c bookmarks v t spos
_ =
3363 | Vdata
| Vcdata
-> v
3364 | Vend
-> error
"unexpected end of input in doc" s spos
3365 | Vopen
("bookmarks", _, closed
) ->
3368 else { v with f = pbookmarks
path pan
anchor c bookmarks }
3370 | Vopen
(_, _, _) ->
3371 error
"unexpected subelement in doc" s spos
3374 Hashtbl.add
h path (c, List.rev
bookmarks, pan
, anchor);
3375 { v with f = llppconfig
}
3377 | Vclose
_ -> error
"unexpected close in doc" s spos
3379 and pbookmarks
path pan
anchor c bookmarks v t spos
_ =
3381 | Vdata
| Vcdata
-> v
3382 | Vend
-> error
"unexpected end of input in bookmarks" s spos
3383 | Vopen
("item", attrs
, closed
) ->
3384 let titleent, spage
, srely
= bookmark_of attrs
in
3385 let page = fromstring int_of_string spos
"page" spage
0
3386 and rely
= fromstring float_of_string spos
"rely" srely
0.0 in
3387 let bookmarks = (unent titleent, 0, (page, rely
)) :: bookmarks in
3389 then { v with f = pbookmarks
path pan
anchor c bookmarks }
3392 { v with f = skip
"item" f }
3395 error
"unexpected subelement in bookmarks" s spos
3397 | Vclose
"bookmarks" ->
3398 { v with f = doc
path pan
anchor c bookmarks }
3400 | Vclose
_ -> error
"unexpected close in bookmarks" s spos
3402 and skip tag
f v t spos
_ =
3404 | Vdata
| Vcdata
-> v
3406 error
("unexpected end of input in skipped " ^ tag
) s spos
3407 | Vopen
(tag'
, _, closed
) ->
3411 let f'
() = { v with f = skip tag
f } in
3412 { v with f = skip tag'
f'
}
3416 else error
("unexpected close in skipped " ^ tag
) s spos
3419 parse
{ f = toplevel; accu = () } s;
3425 let len = in_channel_length ic
in
3426 let s = String.create
len in
3427 really_input ic
s 0 len;
3430 | Parse_error
(msg
, s, pos
) ->
3431 let subs = subs s pos
in
3432 let s = Printf.sprintf
"%s: at %d [..%s..]" msg pos
subs in
3433 failwith
("parse error: " ^
s)
3436 failwith
("config load error: " ^
Printexc.to_string exn
)
3442 let dir = Filename.concat
home ".config" in
3443 if Sys.is_directory
dir then dir else home
3446 Filename.concat
dir "llpp.conf"
3450 if Sys.file_exists
path
3453 (try Some
(open_in_bin
path)
3456 ("Error opening configuation file `" ^
path ^
"': " ^
3457 Printexc.to_string exn
);
3466 ("Error loading configuation from `" ^
path ^
"': " ^
3467 Printexc.to_string exn
);
3473 f (Hashtbl.create
0, defconf)
3478 let pc, pb
, px
, pa
=
3480 Hashtbl.find h (Filename.basename
state.path)
3481 with Not_found
-> dc, [], 0, (0, 0.0)
3485 state.bookmarks <- pb
;
3487 state.scrollw
<- conf.scrollbw
;
3489 then state.anchor <- pa
;
3490 cbput state.hists
.nav pa
;
3495 let add_attrs bb always
dc c =
3498 then Printf.bprintf bb
"\n %s='%b'" s a
3501 then Printf.bprintf bb
"\n %s='%d'" s a
3504 then Printf.bprintf bb
"\n %s='%f'" s (a*.100.)
3508 Printf.bprintf bb
"\n %s='%s'" s (color_to_string a)
3512 Printf.bprintf bb
"\n %s='%s'" s a
3516 then dc.winw
, dc.winh
3518 match state.fullscreen
with
3520 | None
-> c.winw
, c.winh
3522 let zoom, presentation, interpagespace, showall
=
3524 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
3526 match state.mode with
3527 | Birdseye
(bc
, _, _, _, _) ->
3528 bc
.zoom, bc
.presentation, bc
.interpagespace, bc
.showall
3529 | _ -> c.zoom, c.presentation, c.interpagespace, c.showall
3531 oi
"width" w dc.winw
;
3532 oi
"height" h dc.winh
;
3533 oi
"scroll-bar-width" c.scrollbw
dc.scrollbw
;
3534 oi
"scroll-handle-height" c.scrollh
dc.scrollh
;
3535 ob "case-insensitive-search" c.icase
dc.icase
;
3536 ob "preload" c.preload dc.preload;
3537 oi
"page-bias" c.pagebias
dc.pagebias
;
3538 oi
"scroll-step" c.scrollstep
dc.scrollstep
;
3539 oi
"auto-scroll-step" c.autoscrollstep
dc.autoscrollstep
;
3540 ob "max-height-fit" c.maxhfit
dc.maxhfit
;
3541 ob "crop-hack" c.crophack
dc.crophack
;
3542 ob "throttle" showall
dc.showall
;
3543 ob "highlight-links" c.hlinks
dc.hlinks
;
3544 ob "under-cursor-info" c.underinfo
dc.underinfo
;
3545 oi
"vertical-margin" interpagespace dc.interpagespace;
3546 oz
"zoom" zoom dc.zoom;
3547 ob "presentation" presentation dc.presentation;
3548 oi
"rotation-angle" c.angle
dc.angle
;
3549 ob "persistent-bookmarks" c.savebmarks
dc.savebmarks
;
3550 ob "proportional-display" c.proportional
dc.proportional
;
3551 oi
"pixmap-cache-size" c.memlimit
dc.memlimit
;
3552 oi
"texcount" c.texcount
dc.texcount
;
3553 oi
"slice-height" c.sliceheight
dc.sliceheight
;
3554 oi
"thumbnail-width" c.thumbw
dc.thumbw
;
3555 ob "persistent-location" c.jumpback
dc.jumpback
;
3556 oc
"background-color" c.bgcolor
dc.bgcolor
;
3557 ob "scrollbar-in-presentation" c.scrollbarinpm
dc.scrollbarinpm
;
3558 os
"ui-font" c.uifont
dc.uifont
;
3562 let bb = Buffer.create
32768 in
3564 let dc = if conf.bedefault
then conf else dc in
3565 Buffer.add_string
bb "<llppconfig>\n<defaults ";
3566 add_attrs bb true dc dc;
3567 Buffer.add_string
bb "/>\n";
3569 let adddoc path pan
anchor c bookmarks =
3570 if bookmarks == [] && c = dc && anchor = emptyanchor
3573 Printf.bprintf
bb "<doc path='%s'"
3574 (enent
path 0 (String.length
path));
3576 if anchor <> emptyanchor
3578 let n, y = anchor in
3579 Printf.bprintf
bb " page='%d'" n;
3582 Printf.bprintf
bb " rely='%f'" y
3587 then Printf.bprintf
bb " pan='%d'" pan
;
3589 add_attrs bb false dc c;
3591 begin match bookmarks with
3592 | [] -> Buffer.add_string
bb "/>\n"
3594 Buffer.add_string
bb ">\n<bookmarks>\n";
3595 List.iter
(fun (title, _level
, (page, rely
)) ->
3597 "<item title='%s' page='%d'"
3598 (enent
title 0 (String.length
title))
3603 Printf.bprintf
bb " rely='%f'" rely
3605 Buffer.add_string
bb "/>\n";
3607 Buffer.add_string
bb "</bookmarks>\n</doc>\n";
3613 match state.mode with
3614 | Birdseye
(_, pan, _, _, _) -> pan
3617 let basename = Filename.basename state.path in
3618 adddoc basename pan (getanchor ())
3621 match state.autoscroll
with
3623 | None
-> conf.autoscrollstep
}
3624 (if conf.savebmarks
then state.bookmarks else []);
3626 Hashtbl.iter
(fun path (c, bookmarks, x, y) ->
3628 then adddoc path x y c bookmarks
3630 Buffer.add_string
bb "</llppconfig>";
3633 if Buffer.length
bb > 0
3636 let tmp = path ^
".tmp" in
3637 let oc = open_out_bin
tmp in
3638 Buffer.output_buffer
oc bb;
3640 Sys.rename
tmp path;
3643 ("error while saving configuration: " ^
Printexc.to_string exn
)
3650 [("-p", Arg.String
(fun s -> state.password
<- s) , " Set password")
3651 ;("-v", Arg.Unit
(fun () -> print_endline
Help.version
; exit
0),
3652 " Print version and exit")]
3654 (fun s -> state.path <- s)
3655 ("Usage: " ^
Sys.argv
.(0) ^
" [options] some.pdf\nOptions:")
3657 if String.length
state.path = 0
3658 then (prerr_endline
"file name missing"; exit
1);
3662 let _ = Glut.init
Sys.argv
in
3663 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
3664 let () = Glut.initWindowSize
conf.winw
conf.winh
in
3665 let _ = Glut.createWindow
("llpp " ^
Filename.basename state.path) in
3668 if Sys.os_type
= "Unix"
3670 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
3672 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
3673 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
3674 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
3675 Unix.bind
sock addr;
3677 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
3678 Unix.connect
csock addr;
3679 let ssock, _ = Unix.accept
sock in
3682 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
3683 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
3687 at_exit
(fun () -> Unix.shutdown
ssock Unix.SHUTDOWN_ALL
);
3691 let () = Glut.displayFunc
display in
3692 let () = Glut.reshapeFunc
reshape in
3693 let () = Glut.keyboardFunc
keyboard in
3694 let () = Glut.specialFunc
special in
3695 let () = Glut.idleFunc
(Some
idle) in
3696 let () = Glut.mouseFunc
mouse in
3697 let () = Glut.motionFunc
motion in
3698 let () = Glut.passiveMotionFunc
pmotion in
3701 conf.angle
, conf.proportional
, conf.texcount
,
3702 conf.sliceheight
, conf.uifont
3704 state.csock <- csock;
3705 state.ssock <- ssock;
3706 state.text <- "Opening " ^
state.path;
3707 writeopen state.path state.password
;
3711 let rec handlelablglutbug () =
3714 with Glut.BadEnum
"key in special_of_int" ->
3715 showtext '
!'
" LablGlut bug: special key not recognized";
3716 handlelablglutbug ()
3718 handlelablglutbug ();