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 drawstring
: int -> int -> int -> string -> unit = "ml_draw_string";;
40 | Msel
of (mpos
* mpos
)
43 | Mzoom
of (int * int)
47 type textentry
= string * string * onhist
option * onkey
* ondone
48 and onkey
= string -> int -> te
49 and ondone
= string -> unit
50 and histcancel
= unit -> unit
51 and onhist
= ((histcmd
-> string) * histcancel
)
52 and histcmd
= HCnext
| HCprev
| HCfirst
| HClast
57 | TEswitch
of textentry
69 { store
= Array.create n v
76 let cbcap b
= Array.length b
.store
;;
81 b
.wc
<- (b
.wc
+ 1) mod cap;
83 b
.len
<- min
(b
.len
+ 1) cap;
86 let cbempty b
= b
.len
= 0;;
88 let cbgetg b circular dir
=
92 let rc = b
.rc + dir
in
104 else max
0 (min
rc (b
.len
-1))
110 let cbget b
= cbgetg b
false;;
111 let cbgetc b
= cbgetg b
true;;
114 let rc = b
.wc
- b
.len
in
115 let rc = if rc < 0 then cbcap b
+ rc else rc in
119 let cbdecr b
= b
.len
<- b
.len
- 1;;
134 { mutable scrollbw
: int
135 ; mutable scrollh
: int
136 ; mutable icase
: bool
137 ; mutable preload
: bool
138 ; mutable pagebias
: int
139 ; mutable verbose
: bool
140 ; mutable scrollstep
: int
141 ; mutable maxhfit
: bool
142 ; mutable crophack
: bool
143 ; mutable autoscrollstep
: int
144 ; mutable showall
: bool
145 ; mutable hlinks
: bool
146 ; mutable underinfo
: bool
147 ; mutable interpagespace
: interpagespace
148 ; mutable zoom
: float
149 ; mutable presentation
: bool
150 ; mutable angle
: angle
153 ; mutable savebmarks
: bool
154 ; mutable proportional
: proportional
155 ; mutable memlimit
: int
156 ; mutable texcount
: texcount
157 ; mutable sliceheight
: sliceheight
158 ; mutable thumbw
: width
159 ; mutable jumpback
: bool
160 ; mutable bgcolor
: float * float * float
161 ; mutable bedefault
: bool
162 ; mutable scrollbarinpm
: bool
163 ; mutable uifont
: string
167 type anchor
= pageno
* top
;;
169 type outline
= string * int * anchor
171 | Oarray
of outline array
172 | Olist
of outline list
173 | Onarrow
of string * outline array
* outline array
176 type rect
= float * float * float * float * float * float * float * float;;
178 type pagemapkey
= pageno
* width
* angle
* proportional
* gen
;;
180 let emptyanchor = (0, 0.0);;
181 let initialanchor = (-1, nan
);;
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 ascrollstep
: int
232 ; mutable help
: item array
233 ; mutable docinfo
: (int * string) list
237 { pat
: string circbuf
238 ; pag
: string circbuf
239 ; nav
: anchor circbuf
253 ; autoscrollstep
= 24
259 ; presentation
= false
264 ; proportional
= true
265 ; memlimit
= 32*1024*1024
270 ; bgcolor
= (0.5, 0.5, 0.5)
272 ; scrollbarinpm
= true
277 let conf = { defconf with angle
= defconf.angle
};;
280 let strings = ("llpp version " ^
Help.version
) :: "" :: Help.keys
in
281 Array.of_list
(List.map
(fun s
-> s
, 0, Noaction
) strings);
291 ; anchor
= initialanchor
294 ; pagemap
= Hashtbl.create
10
295 ; pagecache
= cbnew 100 ""
306 ; outlines
= Olist
[]
312 { nav
= cbnew 100 (0, 0.0)
329 Printf.kprintf prerr_endline fmt
331 Printf.kprintf ignore fmt
335 let len = String.length s
in
337 let b = Buffer.create
n in
338 Buffer.add_char
b (Char.chr
((len lsr 24) land 0xff));
339 Buffer.add_char
b (Char.chr
((len lsr 16) land 0xff));
340 Buffer.add_char
b (Char.chr
((len lsr 8) land 0xff));
341 Buffer.add_char
b (Char.chr
((len lsr 0) land 0xff));
342 Buffer.add_string
b s
;
343 let s'
= Buffer.contents
b in
344 let n'
= Unix.write fd
s'
0 n in
345 if n'
!= n then failwith
"write failed";
350 let n = Unix.read fd
s 0 4 in
351 if n != 4 then failwith
"incomplete read(len)";
353 lor (Char.code
s.[0] lsl 24)
354 lor (Char.code
s.[1] lsl 16)
355 lor (Char.code
s.[2] lsl 8)
356 lor (Char.code
s.[3] lsl 0)
358 let s = String.create
len in
359 let n = Unix.read fd
s 0 len in
360 if n != len then failwith
"incomplete read(data)";
365 let b = Buffer.create
10 in
366 Buffer.add_string
b s;
367 let rec combine = function
370 Buffer.add_char
b ' '
;
373 | `
b b -> if b then "1" else "0"
375 | `i i
-> string_of_int i
376 | `f f
-> string_of_float f
377 | `I f
-> string_of_int
(truncate f
)
379 Buffer.add_string
b s;
386 let cmd = Buffer.contents
(makecmd s l
) in
387 writecmd state.csock
cmd;
393 let d = conf.winh
- h
in
400 let rec f pn ph pi fh l
=
402 | (n, _
, h
, _
) :: rest
->
403 let ips = calcips h
in
408 if isbirdseye state.mode
&& pn
= 0
413 let fh = fh + ((n - pn
) * (ph
+ pi
)) in
418 if conf.presentation
|| (isbirdseye state.mode
&& pn
= 0)
422 let fh = fh + ((state.pagecount
- pn
) * (ph
+ pi
)) + inc in
425 let fh = f 0 0 0 0 state.pdims
in
429 let getpageyh pageno
=
430 let rec f pn ph pi y l
=
432 | (n, _
, h
, _
) :: rest
->
433 let ips = calcips h
in
436 let h = if n = pageno
then h else ph
in
437 if conf.presentation
&& n = pageno
439 y
+ (pageno
- pn
) * (ph
+ pi
) + pi
, h
441 y
+ (pageno
- pn
) * (ph
+ pi
), h
443 let y = y + (if conf.presentation
then pi
else 0) in
444 let y = y + (n - pn
) * (ph
+ pi
) in
448 y + (pageno
- pn
) * (ph
+ pi
), ph
450 f 0 0 0 0 state.pdims
453 let getpagey pageno
= fst
(getpageyh pageno
);;
456 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu
=
457 let ((w
, h, ips, x
) as curr
), rest
, pdimno
, yinc
=
459 | (pageno'
, w
, h, x
) :: rest
when pageno'
= pageno
->
460 let ips = calcips h in
462 if conf.presentation
|| (isbirdseye state.mode
&& pageno
= 0)
466 (w
, h, ips, x
), rest
, pdimno
+ 1, yinc
468 prev
, pdims
, pdimno
, 0
470 let dy = dy + yinc in
471 let py = py + yinc in
472 if pageno
= state.pagecount
|| cacheleft
= 0 || dy >= sh
477 if py + h <= vy - yinc
479 let py = py + h + ips in
480 let dy = max
0 (py - y) in
490 let pagey = vy - py in
491 let pagevh = h - pagey in
492 let pagevh = min
(sh
- dy) pagevh in
493 let off = if yinc > 0 then py - vy else 0 in
494 let py = py + h + ips in
500 ; pagedispy
= dy + off
501 ; pagey = pagey + off
502 ; pagevh = pagevh - off
506 let accu = e :: accu in
513 ~cacheleft
:(cacheleft
-1)
516 if state.invalidated
= 0
526 ~cacheleft
:(cbcap state.pagecache
)
536 let y = state.y + incr
in
538 let y = min
y (state.maxy
- (if conf.maxhfit
then conf.winh
else 0)) in
542 let getopaque pageno
=
543 try Some
(Hashtbl.find
state.pagemap
544 (pageno
, state.w
, conf.angle
, conf.proportional
, state.gen
))
545 with Not_found
-> None
548 let cache pageno opaque
=
549 Hashtbl.replace
state.pagemap
550 (pageno
, state.w
, conf.angle
, conf.proportional
, state.gen
) opaque
553 let validopaque opaque
= String.length opaque
> 0;;
556 match getopaque l
.pageno
with
557 | None
when not
state.rendering
->
558 state.rendering
<- true;
559 cache l
.pageno
("", -1);
560 wcmd "render" [`i
(l
.pageno
+ 1)
567 let loadlayout layout =
568 let rec f all
= function
570 begin match getopaque l
.pageno
with
571 | None
-> render l
; f false ls
572 | Some
(opaque
, _
) -> f (all
&& validopaque opaque
) ls
576 f (layout <> []) layout;
579 let findpageforopaque opaque
=
581 (fun k
(v
, s) a
-> if v
= opaque
then Some
(k
, s) else a
)
585 let pagevisible layout n = List.exists
(fun l
-> l
.pageno
= n) layout;;
591 let memleft = conf.memlimit
- state.memused
in
594 let opaque = cbpeek state.pagecache
in
595 match findpageforopaque opaque with
596 | Some
((n, _
, _
, _
, _
), size
) ->
597 memleft + size
>= 0 && not
(pagevisible state.layout n)
604 let presentation = conf.presentation in
605 let interpagespace = conf.interpagespace in
606 let maxy = state.maxy in
607 conf.presentation <- false;
608 conf.interpagespace <- 0;
609 state.maxy <- calcheight ();
611 match state.layout with
613 | l
:: _
-> getpagey l
.pageno
+ l
.pagey
615 let y = if y < conf.winh
then 0 else y - conf.winh
in
616 let pages = layout y (conf.winh
*3) in
617 List.iter
render pages;
618 conf.presentation <- presentation;
619 conf.interpagespace <- interpagespace;
625 let y = min
state.maxy y in
626 let pages = layout y conf.winh
in
627 let ready = loadlayout pages in
633 state.layout <- pages;
634 state.throttle
<- None
;
635 Glut.postRedisplay
();
638 state.throttle
<- Some
pages;
643 state.layout <- pages;
644 state.throttle
<- None
;
645 Glut.postRedisplay
();
647 begin match state.mode
with
648 | Birdseye
(conf, leftx
, pageno
, hooverpageno
, anchor
) ->
649 if not
(pagevisible pages pageno
)
651 match state.layout with
654 state.mode
<- Birdseye
(conf, leftx
, l
.pageno
, hooverpageno
, anchor
)
661 let gotoy_and_clear_text y =
663 if not
conf.verbose
then state.text
<- "";
667 match state.layout with
669 | l
:: _
-> (l
.pageno
, float l
.pagey /. float l
.pageh
)
672 let getanchory (n, top
) =
673 let y, h = getpageyh n in
674 y + (truncate
(top
*. float h));
677 let gotoanchor anchor
=
678 gotoy (getanchory anchor
);
682 cbput state.hists
.nav
(getanchor ());
686 let anchor = cbgetc state.hists
.nav dir
in
691 let y, h = getpageyh n in
692 gotoy_and_clear_text (y + (truncate
(top
*. float h)));
695 let gotopage1 n top
=
696 let y = getpagey n in
697 gotoy_and_clear_text (y + top
);
705 state.invalidated
<- state.invalidated
+ 1;
709 let c = c *. state.colorscale
in
713 let scalecolor2 (r
, g
, b) =
714 (r
*. state.colorscale
, g
*. state.colorscale
, b *. state.colorscale
);
718 state.maxy <- calcheight ();
719 match state.mode
with
720 | Birdseye
(_
, _
, pageno
, _
, _
) ->
721 let y, h = getpageyh pageno
in
722 let top = (conf.winh
- h) / 2 in
723 gotoy (max
0 (y - top))
724 | _
-> gotoanchor state.anchor
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 state.w
, 2.0 /. float conf.winh
, 1.0);
735 GlMat.translate ~x
:(float state.x
) ();
740 GlMat.mode `projection
;
741 GlMat.load_identity
();
742 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
743 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
744 GlMat.scale3
(2.0 /. float conf.winw
, 2.0 /. float conf.winh
, 1.0);
748 if state.invalidated
= 0 && state.anchor == initialanchor
749 then state.anchor <- getanchor ();
752 let w = truncate
(float w *. conf.zoom
) - state.scrollw
in
756 GlMat.mode `modelview
;
757 GlMat.load_identity
();
758 GlClear.color
(scalecolor 1.0);
759 GlClear.clear
[`color
];
762 wcmd "geometry" [`i
w; `i
h];
767 GlDraw.color
(0.0, 0.0, 0.0);
769 (0.0, float (conf.winh
- 18))
770 (float (conf.winw
- state.scrollw
- 1), float conf.winh
)
772 GlDraw.color
(1.0, 1.0, 1.0);
773 drawstring 14 8 (conf.winh
- 5) s;
775 let len = String.length
state.text
in
776 match state.mode
with
777 | Textentry
((prefix
, text
, _
, _
, _
), _
) ->
779 match String.length prefix
with
783 Printf.sprintf
"%s%s\226–� [%s]" prefix text
state.text
785 Printf.sprintf
"%s%s\226–�" prefix text
790 Printf.sprintf
"%s: %s\226–� [%s]" prefix text
state.text
792 Printf.sprintf
"%s: %s\226–�" prefix text
797 if len > 0 then drawstring state.text
801 state.text
<- Printf.sprintf
"%c%s" c s;
802 Glut.postRedisplay
();
811 state.rects
<- state.rects1
;
812 Glut.postRedisplay
()
815 let n = Scanf.sscanf
cmd "C %u" (fun n -> n) in
816 state.pagecount
<- n;
817 state.invalidated
<- state.invalidated
- 1;
818 if state.invalidated
= 0
822 let s = Scanf.sscanf
cmd "t %n"
823 (fun n -> String.sub
cmd n (String.length
cmd - n))
825 Glut.setWindowTitle
s
828 let s = Scanf.sscanf
cmd "T %n"
829 (fun n -> String.sub
cmd n (String.length
cmd - n))
831 if istextentry state.mode
838 Glut.postRedisplay
();
844 let s = Scanf.sscanf
cmd "V %n"
845 (fun n -> String.sub
cmd n (String.length
cmd - n))
851 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
852 Scanf.sscanf
cmd "F %u %d %f %f %f %f %f %f %f %f"
853 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
854 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
856 let y = (getpagey pageno) + truncate y0
in
859 state.rects1
<- [pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)]
862 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
863 Scanf.sscanf
cmd "R %u %d %f %f %f %f %f %f %f %f"
864 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
865 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
868 (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) :: state.rects1
871 let n, w, _h
, r
, l
, s, p
=
872 Scanf.sscanf
cmd "r %u %u %u %d %d %u %s"
873 (fun n w h r l
s p
->
874 (n-1, w, h, r
, l
!= 0, s, p
))
877 Hashtbl.replace
state.pagemap
(n, w, r
, l
, state.gen
) (p
, s);
878 state.memused
<- state.memused
+ s;
881 match state.throttle
with
882 | None
-> state.layout
883 | Some
layout -> layout
887 if (state.memused
<= conf.memlimit
) || cbempty state.pagecache
890 let evictedopaque = cbpeek state.pagecache
in
891 match findpageforopaque evictedopaque with
892 | None
-> failwith
"bug in gc"
893 | Some
((evictedn
, _
, _
, _
, gen
) as k
, evictedsize
) ->
894 if state.gen
!= gen
|| not
(pagevisible layout evictedn
)
896 wcmd "free" [`
s evictedopaque];
897 state.memused
<- state.memused
- evictedsize
;
898 Hashtbl.remove
state.pagemap k
;
899 cbdecr state.pagecache
;
906 cbput state.pagecache p
;
907 state.rendering
<- false;
909 begin match state.throttle
with
911 if pagevisible state.layout n
914 let allvisible = loadlayout state.layout in
915 if allvisible then preload ();
922 let y = getpagey l
.pageno + l
.pagey in
928 Scanf.sscanf
cmd "l %u %u %u %u" (fun n w h x
-> n, w, h, x
)
930 state.pdims
<- pdim :: state.pdims
933 let (l
, n, t
, h, pos
) =
934 Scanf.sscanf
cmd "o %u %u %d %u %n" (fun l
n t
h pos
-> l
, n, t
, h, pos
)
936 let s = String.sub
cmd pos
(String.length
cmd - pos
) in
937 let outline = (s, l
, (n, float t
/. float h)) in
939 match state.outlines with
940 | Olist
outlines -> Olist
(outline :: outlines)
941 | Oarray _
-> Olist
[outline]
942 | Onarrow _
-> Olist
[outline]
944 state.outlines <- outlines
948 let s = Scanf.sscanf
cmd "i %n"
949 (fun n -> String.sub
cmd n (String.length
cmd - n))
951 let len = String.length
s in
952 let rec fold accu pos
=
954 try String.index_from
s pos '
\n'
with Not_found
-> len
959 let line = String.sub
s pos
(eolpos - pos
) in
960 fold ((1, line)::accu) (eolpos+1)
962 state.docinfo
<- fold state.docinfo
0
965 dolog "unknown cmd `%S'" cmd
968 let now = Unix.gettimeofday
;;
972 let r, _
, _
= Unix.select
[state.csock
] [] [] delay
in
975 if state.ascrollstep
> 0
977 let y = state.y + state.ascrollstep
in
978 let y = if y >= state.maxy then 0 else y in
984 let cmd = readcmd state.csock
in
993 let action = function
994 | HCprev
-> cbget cb ~
-1
995 | HCnext
-> cbget cb
1
996 | HCfirst
-> cbget cb ~
-(cb
.rc)
997 | HClast
-> cbget cb
(cb
.len - 1 - cb
.rc)
998 and cancel
() = cb
.rc <- rc
1002 let search pattern forward
=
1003 if String.length pattern
> 0
1006 match state.layout with
1009 l
.pageno, (l
.pagey + if forward
then 0 else 0*l
.pagevh)
1012 let b = makecmd "search"
1013 [`
b conf.icase
; `i
pn; `i
py; `i
(if forward
then 1 else 0)]
1015 Buffer.add_char
b '
,'
;
1016 Buffer.add_string
b pattern
;
1017 Buffer.add_char
b '
\000'
;
1020 writecmd state.csock
cmd;
1023 let intentry text key
=
1024 let c = Char.unsafe_chr key
in
1027 let s = "x" in s.[0] <- c;
1028 let text = text ^
s in
1032 state.text <- Printf.sprintf
"invalid char (%d, `%c')" key
c;
1037 let b = Buffer.create
(String.length
s + 1) in
1038 Buffer.add_string
b s;
1039 Buffer.add_char
b c;
1043 let textentry text key
=
1044 let c = Char.unsafe_chr key
in
1046 | _
when key
>= 32 && key
< 127 ->
1047 let text = addchar text c in
1051 dolog "unhandled key %d char `%c'" key
(Char.unsafe_chr key
);
1055 let reinit angle proportional
=
1056 conf.angle
<- angle
;
1057 conf.proportional
<- proportional
;
1059 wcmd "reinit" [`i angle
; `
b proportional
];
1063 let zoom = max
0.01 (min
2.2 zoom) in
1064 if zoom <> conf.zoom
1069 if state.invalidated
= 0
1071 state.anchor <- getanchor ()
1073 reshape conf.winw
conf.winh
;
1074 state.text <- Printf.sprintf
"zoom is now %-5.1f" (zoom *. 100.0);
1078 let enterbirdseye () =
1079 let zoom = float conf.thumbw
/. float conf.winw
in
1080 let birdseyepageno =
1081 let cy = conf.winh
/ 2 in
1085 let rec fold best
= function
1088 let d = cy - (l
.pagedispy
+ l
.pagevh/2)
1089 and dbest
= cy - (best
.pagedispy
+ best
.pagevh/2) in
1090 if abs
d < abs dbest
1097 state.mode
<- Birdseye
(
1098 { conf with zoom = conf.zoom }, state.x
, birdseyepageno, -1, getanchor ()
1101 conf.presentation <- false;
1102 conf.interpagespace <- 10;
1103 conf.hlinks
<- false;
1105 state.mstate
<- Mnone
;
1106 conf.showall
<- false;
1107 Glut.setCursor
Glut.CURSOR_INHERIT
;
1110 state.text <- Printf.sprintf
"birds eye mode on (zoom %3.1f%%)"
1115 reshape conf.winw
conf.winh
;
1118 let leavebirdseye (c, leftx
, pageno, _
, anchor) goback
=
1120 conf.zoom <- c.zoom;
1121 conf.presentation <- c.presentation;
1122 conf.interpagespace <- c.interpagespace;
1123 conf.showall
<- c.showall
;
1124 conf.hlinks
<- c.hlinks
;
1128 state.text <- Printf.sprintf
"birds eye mode off (zoom %3.1f%%)"
1131 reshape conf.winw
conf.winh
;
1132 state.anchor <- if goback
then anchor else (pageno, 0.0);
1135 let togglebirdseye () =
1136 match state.mode
with
1137 | Birdseye vals
-> leavebirdseye vals
true
1138 | View
| Outline _
-> enterbirdseye ()
1142 let upbirdseye (conf, leftx
, pageno, hooverpageno
, anchor) =
1143 let pageno = max
0 (pageno - 1) in
1144 let rec loop = function
1145 | [] -> gotopage1 pageno 0
1146 | l
:: _
when l
.pageno = pageno ->
1147 if l
.pagedispy
>= 0 && l
.pagey = 0
1148 then Glut.postRedisplay
()
1149 else gotopage1 pageno 0
1150 | _
:: rest
-> loop rest
1153 state.mode
<- Birdseye
(conf, leftx
, pageno, hooverpageno
, anchor)
1156 let downbirdseye (conf, leftx
, pageno, hooverpageno
, anchor) =
1157 let pageno = min
(state.pagecount
- 1) (pageno + 1) in
1158 state.mode
<- Birdseye
(conf, leftx
, pageno, hooverpageno
, anchor);
1159 let rec loop = function
1161 let y, h = getpageyh pageno in
1162 let dy = (y - state.y) - (conf.winh
- h - conf.interpagespace) in
1164 | l
:: _
when l
.pageno = pageno ->
1165 if l
.pagevh != l
.pageh
1166 then gotoy (clamp (l
.pageh
- l
.pagevh + conf.interpagespace))
1167 else Glut.postRedisplay
()
1168 | _
:: rest
-> loop rest
1173 let optentry mode _ key
=
1174 let btos b = if b then "on" else "off" in
1175 let c = Char.unsafe_chr key
in
1179 try conf.scrollstep
<- int_of_string
s with exc
->
1180 state.text <- Printf.sprintf
"bad integer `%s': %s"
1181 s (Printexc.to_string exc
)
1183 TEswitch
("scroll step", "", None
, intentry, ondone)
1188 conf.autoscrollstep
<- int_of_string
s;
1189 if state.ascrollstep
> 0
1190 then state.ascrollstep
<- conf.autoscrollstep
;
1192 state.text <- Printf.sprintf
"bad integer `%s': %s"
1193 s (Printexc.to_string exc
)
1195 TEswitch
("auto scroll step", "", None
, intentry, ondone)
1200 let zoom = float (int_of_string
s) /. 100.0 in
1203 state.text <- Printf.sprintf
"bad integer `%s': %s"
1204 s (Printexc.to_string exc
)
1206 TEswitch
("zoom", "", None
, intentry, ondone)
1211 conf.thumbw
<- max
2 (min
1920 (int_of_string
s));
1213 Printf.sprintf
"thumbnail width is set to %d" conf.thumbw
;
1214 begin match mode
with
1216 leavebirdseye beye
false;
1221 state.text <- Printf.sprintf
"bad integer `%s': %s"
1222 s (Printexc.to_string exc
)
1224 TEswitch
("thumbnail width", "", None
, intentry, ondone)
1229 Some
(int_of_string
s)
1231 state.text <- Printf.sprintf
"bad integer `%s': %s"
1232 s (Printexc.to_string exc
);
1235 | Some angle
-> reinit angle
conf.proportional
1238 TEswitch
("rotation", "", None
, intentry, ondone)
1241 conf.icase
<- not
conf.icase
;
1242 TEdone
("case insensitive search " ^
(btos conf.icase
))
1245 conf.preload <- not
conf.preload;
1247 TEdone
("preload " ^
(btos conf.preload))
1250 conf.verbose
<- not
conf.verbose
;
1251 TEdone
("verbose " ^
(btos conf.verbose
))
1254 conf.maxhfit
<- not
conf.maxhfit
;
1255 state.maxy <- state.maxy + (if conf.maxhfit
then -conf.winh
else conf.winh
);
1256 TEdone
("maxhfit " ^
(btos conf.maxhfit
))
1259 conf.crophack
<- not
conf.crophack
;
1260 TEdone
("crophack " ^
btos conf.crophack
)
1263 conf.showall
<- not
conf.showall
;
1264 TEdone
("throttle " ^
btos conf.showall
)
1267 conf.underinfo
<- not
conf.underinfo
;
1268 TEdone
("underinfo " ^
btos conf.underinfo
)
1271 conf.savebmarks
<- not
conf.savebmarks
;
1272 TEdone
("persistent bookmarks " ^
btos conf.savebmarks
)
1278 match state.layout with
1283 conf.interpagespace <- int_of_string
s;
1284 state.maxy <- calcheight ();
1285 let y = getpagey pageno in
1288 state.text <- Printf.sprintf
"bad integer `%s': %s"
1289 s (Printexc.to_string exc
)
1291 TEswitch
("vertical margin", "", None
, intentry, ondone)
1294 reinit conf.angle
(not
conf.proportional
);
1295 TEdone
("proprortional display " ^
btos conf.proportional
)
1298 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
1302 let maxoutlinerows () = (conf.winh
- 31) / 16;;
1304 let enterselector allowdel
outlines errmsg msg
=
1305 if Array.length
outlines = 0
1307 showtext ' ' errmsg
;
1311 Glut.setCursor
Glut.CURSOR_INHERIT
;
1313 match state.layout with
1315 | {pageno=pageno} :: _
-> pageno
1319 if n = Array.length
outlines
1322 let (_
, _
, (outlinepageno
, _
)) = outlines.(n) in
1323 if outlinepageno
>= pageno then n else loop (n+1)
1327 state.mode
<- Outline
1328 (allowdel
, active, max
0 (active - maxoutlinerows () / 2), outlines, "", 0,
1330 Glut.postRedisplay
();
1334 let enteroutlinemode () =
1336 match state.outlines with
1339 let a = Array.of_list
(List.rev l
) in
1340 state.outlines <- Oarray
a;
1342 | Onarrow
(pat
, a, _
) ->
1343 a, "Outline was narrowed to `" ^ pat ^
"' (Ctrl-u to restore)"
1345 enterselector false outlines "Document has no outline" msg
;
1348 let enterbookmarkmode () =
1349 let bookmarks = Array.of_list
state.bookmarks in
1350 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1353 let mode_to_string mode
=
1354 let b = Buffer.create
10 in
1355 let rec f = function
1356 | Textentry
(_
, _
) -> Buffer.add_string
b "Textentry ";
1357 | View
-> Buffer.add_string
b "View"
1358 | Birdseye _
-> Buffer.add_string
b "Birdseye"
1359 | Items _
-> Buffer.add_string
b "Items"
1360 | Outline _
-> Buffer.add_string
b "Outline"
1366 let color_of_string s =
1367 Scanf.sscanf
s "%d/%d/%d" (fun r g
b ->
1368 (float r /. 256.0, float g
/. 256.0, float b /. 256.0)
1372 let color_to_string (r, g
, b) =
1373 let r = truncate
(r *. 256.0)
1374 and g
= truncate
(g
*. 256.0)
1375 and b = truncate
(b *. 256.0) in
1376 Printf.sprintf
"%d/%d/%d" r g
b
1379 let enterinfomode () =
1380 let btos = function true -> "on" | _
-> "off" in
1381 let mode = state.mode in
1382 let rec makeitems () =
1383 let intp name get set
=
1384 Printf.sprintf
"%-24s %d" name
(get
()), 1, Action
(
1385 fun active first _ pan
->
1390 state.text <- Printf.sprintf
"bad integer `%s': %s"
1391 s (Printexc.to_string exn
);
1394 if n != max_int
then set
n;
1396 let te = name
, "", None
, intentry, ondone in
1401 state.mode <- Items
(active, first
, makeitems (), "", pan
, mode)
1404 and boolp name get set
=
1405 Printf.sprintf
"%-24s %s" name
(btos (get
())), 1, Action
(
1406 fun active first qsearch pan
->
1409 Items
(active, first
, makeitems (), qsearch
, pan
, mode);
1411 and colorp name get set
=
1412 Printf.sprintf
"%-24s %s" name
(color_to_string (get
())), 1, Action
(
1413 fun active first _ pan
->
1414 let invalid = (nan
, nan
, nan
) in
1417 try color_of_string s
1419 state.text <- Printf.sprintf
"bad color `%s': %s"
1420 s (Printexc.to_string exn
);
1426 let te = name
, "", None
, textentry, ondone in
1431 state.mode <- Items
(active, first
, makeitems (), "", pan
, mode)
1436 let birdseye = isbirdseye mode in
1438 (if birdseye then "Setup bird's eye" else "Setup"), 0, Noaction
;
1440 boolp
"presentation"
1441 (fun () -> conf.presentation)
1443 conf.presentation <- v;
1444 state.anchor <- getanchor ();
1447 boolp
"ignore case in searches"
1448 (fun () -> conf.icase
)
1449 (fun v -> conf.icase
<- v);
1452 (fun () -> conf.preload)
1453 (fun v -> conf.preload <- v);
1456 (fun () -> conf.verbose
)
1457 (fun v -> conf.verbose
<- v);
1460 (fun () -> conf.maxhfit
)
1461 (fun v -> conf.maxhfit
<- v);
1464 (fun () -> conf.crophack
)
1465 (fun v -> conf.crophack
<- v);
1468 (fun () -> conf.showall
)
1469 (fun v -> conf.showall
<- v);
1471 boolp
"highlight links"
1472 (fun () -> conf.hlinks
)
1473 (fun v -> conf.hlinks
<- v);
1476 (fun () -> conf.underinfo
)
1477 (fun v -> conf.underinfo
<- v);
1478 boolp
"persistent bookmarks"
1479 (fun () -> conf.savebmarks
)
1480 (fun v -> conf.savebmarks
<- v);
1482 boolp
"proportional display"
1483 (fun () -> conf.proportional
)
1484 (fun v -> reinit conf.angle
v);
1486 boolp
"persistent location"
1487 (fun () -> conf.jumpback
)
1488 (fun v -> conf.jumpback
<- v);
1492 intp "vertical margin"
1493 (fun () -> conf.interpagespace)
1495 conf.interpagespace <- n;
1497 match state.layout with
1502 state.maxy <- calcheight ();
1503 let y = getpagey pageno in
1508 (fun () -> conf.pagebias
)
1509 (fun v -> conf.pagebias
<- v);
1512 (fun () -> conf.scrollstep
)
1513 (fun n -> conf.scrollstep
<- n);
1515 intp "auto scroll step"
1517 if state.ascrollstep
> 0
1518 then state.ascrollstep
1519 else conf.autoscrollstep
)
1521 if state.ascrollstep
> 0
1522 then state.ascrollstep
<- n
1523 else conf.autoscrollstep
<- n);
1526 (fun () -> truncate
(conf.zoom *. 100.))
1527 (fun v -> setzoom ((float v) /. 100.));
1530 (fun () -> conf.angle
)
1531 (fun v -> reinit v conf.proportional
);
1533 intp "scroll bar width"
1534 (fun () -> state.scrollw
)
1538 reshape conf.winw
conf.winh
;
1541 intp "scroll handle height"
1542 (fun () -> conf.scrollh
)
1543 (fun v -> conf.scrollh
<- v;);
1545 intp "thumbnail width"
1546 (fun () -> conf.thumbw
)
1548 conf.thumbw
<- min
1920 v;
1551 leavebirdseye beye
false;
1556 colorp
"background color"
1557 (fun () -> conf.bgcolor
)
1558 (fun v -> conf.bgcolor
<- v);
1561 "Presentation mode", 0, Noaction
;
1563 boolp
"scrollbar visible"
1564 (fun () -> conf.scrollbarinpm
)
1566 if v != conf.scrollbarinpm
1568 conf.scrollbarinpm
<- v;
1569 if conf.presentation
1571 state.scrollw
<- if v then conf.scrollbw
else 0;
1572 reshape conf.winw
conf.winh
;
1578 "Pixmap Cache", 0, Noaction
;
1580 intp "size (advisory)"
1581 (fun () -> conf.memlimit
)
1582 (fun v -> conf.memlimit
<- v);
1583 Printf.sprintf
"%-24s %d" "used" state.memused
, 1, Noaction
;
1590 :: ("Document", 0, Noaction
)
1591 :: List.map
(fun (_
, s) -> (s, 1, Noaction
)) state.docinfo
1597 :: (Printf.sprintf
"Save these parameters as defaults at exit (%s)"
1598 (btos conf.bedefault
),
1601 fun active first qsearch pan
->
1602 conf.bedefault
<- not
conf.bedefault
;
1603 Items
(active, first
, makeitems (), qsearch
, pan
, mode);
1608 Array.of_list
(items @ tailer)
1611 state.mode <- Items
(1, 0, makeitems (), "", 0, mode);
1612 Glut.postRedisplay
();
1615 let enterhelpmode () =
1616 state.mode <- Items
(-1, 0, state.help
, "", 0, state.mode);
1617 Glut.postRedisplay
();
1620 let quickbookmark ?title
() =
1621 match state.layout with
1627 let sec = Unix.gettimeofday
() in
1628 let tm = Unix.localtime
sec in
1629 Printf.sprintf
"Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1633 (tm.Unix.tm_year
+ 1900)
1636 | Some
title -> title
1639 (title, 0, (l
.pageno, float l
.pagey /. float l
.pageh
))
1644 state.fullscreen
<- None
;
1645 Glut.reshapeWindow
w h;
1648 let writeopen path password
=
1649 writecmd state.csock
("open " ^ path ^
"\000" ^ password ^
"\000");
1650 writecmd state.csock
"info";
1653 let opendoc path password
=
1656 state.password
<- password
;
1657 state.gen
<- state.gen
+ 1;
1659 writeopen path password
;
1660 Glut.setWindowTitle
("llpp " ^
Filename.basename path
);
1661 wcmd "geometry" [`i
state.w; `i
conf.winh
];
1664 let viewkeyboard key
=
1666 let mode = state.mode in
1667 state.mode <- Textentry
(te, fun _
-> state.mode <- mode);
1670 Glut.postRedisplay
()
1672 let c = Char.chr key
in
1674 | '
\027'
| 'q'
-> (* escape *)
1677 | '
\008'
-> (* backspace *)
1678 let y = getnav ~
-1 in
1679 gotoy_and_clear_text y
1687 Glut.postRedisplay
()
1690 let ondone isforw
s =
1691 cbput state.hists
.pat
s;
1692 state.searchpattern
<- s;
1695 let s = String.create
1 in
1697 enttext (s, "", Some
(onhist state.hists
.pat
),
1698 textentry, ondone (c ='
/'
))
1700 | '
+'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1701 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1702 setzoom (min
2.2 (conf.zoom +. incr))
1707 try int_of_string
s with exc
->
1708 state.text <- Printf.sprintf
"bad integer `%s': %s"
1709 s (Printexc.to_string exc
);
1715 state.text <- "page bias is now " ^ string_of_int
n;
1718 enttext ("page bias", "", None
, intentry, ondone)
1720 | '
-'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1721 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1722 setzoom (max
0.01 (conf.zoom -. decr))
1725 let ondone msg
= state.text <- msg
in
1727 "option [acfhilpstvAPRSZ]", "", None
,
1728 optentry state.mode, ondone
1731 | '
0'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1734 | '
1'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1735 let zoom = zoomforh
conf.winw
conf.winh
state.scrollw
in
1739 | '
9'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1745 try int_of_string
s with exc
->
1746 state.text <- Printf.sprintf
"bad integer `%s': %s"
1747 s (Printexc.to_string exc
);
1753 cbput state.hists
.pag
(string_of_int
n);
1754 gotoy_and_clear_text (getpagey (n + conf.pagebias
- 1))
1757 let pageentry text key
=
1758 match Char.unsafe_chr key
with
1759 | 'g'
-> TEdone
text
1760 | _
-> intentry text key
1762 let text = "x" in text.[0] <- c;
1763 enttext (":", text, Some
(onhist state.hists
.pag
), pageentry, ondone)
1766 state.scrollw
<- if state.scrollw
> 0 then 0 else conf.scrollbw
;
1767 reshape conf.winw
conf.winh
;
1770 conf.hlinks
<- not
conf.hlinks
;
1771 state.text <- "highlightlinks " ^
if conf.hlinks
then "on" else "off";
1772 Glut.postRedisplay
()
1775 if state.ascrollstep
= 0
1776 then state.ascrollstep
<- conf.autoscrollstep
1778 conf.autoscrollstep
<- state.ascrollstep
;
1779 state.ascrollstep
<- 0;
1783 conf.presentation <- not
conf.presentation;
1784 if conf.presentation
1786 if not
conf.scrollbarinpm
1787 then state.scrollw
<- 0;
1790 state.scrollw
<- conf.scrollbw
;
1792 showtext ' '
("presentation mode " ^
1793 if conf.presentation then "on" else "off");
1794 state.anchor <- getanchor ();
1798 begin match state.fullscreen
with
1800 state.fullscreen
<- Some
(conf.winw
, conf.winh
);
1803 state.fullscreen
<- None
;
1808 gotoy_and_clear_text 0
1811 search state.searchpattern
true
1814 search state.searchpattern
false
1817 begin match state.layout with
1820 gotoy_and_clear_text (getpagey l
.pageno)
1824 begin match List.rev
state.layout with
1827 let pageno = min
(l
.pageno+1) (state.pagecount
-1) in
1828 gotoy_and_clear_text (getpagey pageno)
1831 | '
\127'
-> (* delte *)
1832 begin match state.layout with
1835 let pageno = max
0 (l
.pageno-1) in
1836 gotoy_and_clear_text (getpagey pageno)
1841 if fn
= -1 then l
.pageno, l
.pageno else fn
, l
.pageno
1843 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
1845 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
1849 else (100. *. (float state.y /. float maxy)) in
1852 Printf.sprintf
"Page %d of %d %.2f%%"
1853 (fn+1) state.pagecount
percent
1856 "Pages %d-%d of %d %.2f%%"
1857 (fn+1) (ln
+1) state.pagecount
percent
1862 begin match state.layout with
1865 doreshape (l
.pagew
+ state.scrollw
) l
.pageh
;
1866 Glut.postRedisplay
();
1870 enterbookmarkmode ()
1880 match state.layout with
1883 (s, 0, (l
.pageno, float l
.pagey /. float l
.pageh
))
1887 enttext ("bookmark", "", None
, textentry, ondone)
1891 showtext ' '
"Quick bookmark added";
1894 begin match state.layout with
1896 let rect = getpdimrect l
.pagedimno
in
1900 (truncate
(1.8 *. (rect.(1) -. rect.(0))),
1901 truncate
(1.2 *. (rect.(3) -. rect.(0))))
1903 (truncate
(rect.(1) -. rect.(0)),
1904 truncate
(rect.(3) -. rect.(0)))
1908 doreshape (w + state.scrollw
) (h + conf.interpagespace)
1910 Glut.postRedisplay
();
1916 reinit (conf.angle
+ (if c = '
>'
then 30 else -30)) conf.proportional
1921 (min
(state.colorscale
+. (if c = '
]'
then 0.1 else -0.1)) 1.0);
1922 Glut.postRedisplay
()
1925 begin match state.mode with
1926 | Birdseye beye
-> upbirdseye beye
1927 | _
-> gotoy (clamp (-conf.scrollstep
))
1931 begin match state.mode with
1932 | Birdseye beye
-> downbirdseye beye
1933 | _
-> gotoy (clamp conf.scrollstep
)
1937 state.anchor <- getanchor ();
1938 opendoc state.path
state.password
1941 vlog "huh? %d %c" key
(Char.chr key
);
1944 let textentrykeyboard key
((c, text, opthist
, onkey
, ondone), onleave
) =
1946 state.mode <- Textentry
(te, onleave
);
1949 Glut.postRedisplay
()
1951 match Char.unsafe_chr key
with
1952 | '
\008'
-> (* backspace *)
1953 let len = String.length
text in
1957 Glut.postRedisplay
();
1960 let s = String.sub
text 0 (len - 1) in
1961 enttext (c, s, opthist
, onkey
, ondone)
1967 Glut.postRedisplay
()
1969 | '
\027'
-> (* escape *)
1970 begin match opthist
with
1972 | Some
(_
, onhistcancel
) -> onhistcancel
()
1975 Glut.postRedisplay
()
1978 begin match onkey
text key
with
1982 Glut.postRedisplay
()
1985 enttext (c, text, opthist
, onkey
, ondone);
1989 Glut.postRedisplay
()
1992 state.mode <- Textentry
(te, onleave
);
1993 Glut.postRedisplay
()
1997 let birdseyekeyboard key
((_
, _
, pageno, _
, _
) as beye
) =
1999 | 27 -> (* escape *)
2000 leavebirdseye beye
true
2002 | 12 -> (* ctrl-l *)
2003 let y, h = getpageyh pageno in
2004 let top = (conf.winh
- h) / 2 in
2005 gotoy (max
0 (y - top))
2008 leavebirdseye beye
false
2014 let itemskeyboard key
(active, first
, items, qsearch
, pan
, oldmode
) =
2015 let set active first qsearch
=
2016 state.mode <- Items
(active, first
, items, qsearch
, pan
, oldmode
)
2018 let search active pattern
incr =
2021 if n = Array.length
items || n = -1
2024 let (s, _
, _
) = items.(n) in
2026 (try ignore
(Str.search_forward re
s 0); true
2027 with Not_found
-> false)
2029 else loop (n + incr)
2034 let re = Str.regexp_case_fold pattern
in
2040 let firstof active = max
0 (active - maxoutlinerows () / 2) in
2042 | 18 | 19 -> (* ctrl-r/ctlr-s *)
2043 let incr = if key
= 18 then -1 else 1 in
2045 match search (active + incr) qsearch
incr with
2047 state.text <- qsearch ^
" [not found]";
2050 state.text <- qsearch
;
2051 active, firstof active
2053 set active first qsearch
;
2054 Glut.postRedisplay
();
2056 | 8 -> (* backspace *)
2057 let len = String.length qsearch
in
2064 set active first
"";
2067 let qsearch = String.sub
qsearch 0 (len - 1) in
2069 match search active qsearch ~
-1 with
2071 state.text <- qsearch ^
" [not found]";
2074 state.text <- qsearch;
2075 active, firstof active
2077 set active first
qsearch
2079 Glut.postRedisplay
()
2081 | _
when key
>= 32 && key
< 127 ->
2082 let pattern = addchar qsearch (Char.chr key
) in
2084 match search active pattern 1 with
2086 state.text <- pattern ^
" [not found]";
2089 state.text <- pattern;
2090 active, firstof active
2092 set active first
pattern;
2093 Glut.postRedisplay
()
2095 | 27 -> (* escape *)
2097 state.mode <- oldmode
;
2098 Glut.postRedisplay
();
2101 if active >= 0 && active < Array.length
items
2103 match items.(active) with
2105 state.mode <- f active first
qsearch pan
2109 state.mode <- oldmode
2113 state.mode <- oldmode
2115 Glut.postRedisplay
();
2117 | _
-> dolog "unknown key %d" key
2120 let outlinekeyboard key
2121 (allowdel
, active, first
, outlines, qsearch, pan
, oldmode
) =
2122 let narrow outlines pattern =
2123 let reopt = try Some
(Str.regexp_case_fold
pattern) with _
-> None
in
2127 let rec fold accu n =
2131 let (s, _
, _
) as o
= outlines.(n) in
2133 if (try ignore
(Str.search_forward
re s 0); true
2134 with Not_found
-> false)
2140 let matched = fold [] (Array.length
outlines - 1) in
2141 if matched = [] then None
else Some
(Array.of_list
matched)
2143 let search active pattern incr =
2146 if n = Array.length
outlines || n = -1
2149 let (s, _
, _
) = outlines.(n) in
2151 (try ignore
(Str.search_forward
re s 0); true
2152 with Not_found
-> false)
2154 else loop (n + incr)
2159 let re = Str.regexp_case_fold
pattern in
2165 let firstof active = max
0 (active - maxoutlinerows () / 2) in
2167 | 27 -> (* escape *)
2168 if String.length
qsearch = 0
2171 state.mode <- oldmode
;
2172 Glut.postRedisplay
();
2176 state.mode <- Outline
(
2177 allowdel
, active, first
, outlines, "", pan
, oldmode
2179 Glut.postRedisplay
();
2182 | 18 | 19 -> (* ctrl-r/ctrl-s *)
2183 let incr = if key
= 18 then -1 else 1 in
2185 match search (active + incr) qsearch incr with
2187 state.text <- qsearch ^
" [not found]";
2190 state.text <- qsearch;
2191 active, firstof active
2193 state.mode <- Outline
(
2194 allowdel
, active, first
, outlines, qsearch, pan
, oldmode
2196 Glut.postRedisplay
();
2198 | 8 -> (* backspace *)
2199 let len = String.length
qsearch in
2206 state.mode <- Outline
(
2207 allowdel
, active, first
, outlines, "", pan
, oldmode
2211 let qsearch = String.sub
qsearch 0 (len - 1) in
2213 match search active qsearch ~
-1 with
2215 state.text <- qsearch ^
" [not found]";
2218 state.text <- qsearch;
2219 active, firstof active
2221 state.mode <- Outline
(
2222 allowdel
, active, first
, outlines, qsearch, pan
, oldmode
2225 Glut.postRedisplay
()
2228 if active < Array.length
outlines
2230 let (_
, _
, anchor) = outlines.(active) in
2235 if allowdel
then state.bookmarks <- Array.to_list
outlines;
2236 state.mode <- oldmode
;
2237 Glut.postRedisplay
();
2239 | _
when key
>= 32 && key
< 127 ->
2240 let pattern = addchar qsearch (Char.chr key
) in
2242 match search active pattern 1 with
2244 state.text <- pattern ^
" [not found]";
2247 state.text <- pattern;
2248 active, firstof active
2250 state.mode <- Outline
(
2251 allowdel
, active, first
, outlines, pattern, pan
, oldmode
2253 Glut.postRedisplay
()
2255 | 14 when not allowdel
-> (* ctrl-n *)
2256 if String.length
qsearch > 0
2258 let optoutlines = narrow outlines qsearch in
2259 begin match optoutlines with
2260 | None
-> state.text <- "can't narrow"
2262 state.mode <- Outline
(
2263 allowdel
, 0, 0, outlines, qsearch, pan
, oldmode
2265 match state.outlines with
2268 state.outlines <- Onarrow
(qsearch, outlines, a)
2269 | Onarrow
(_
, _
, b) ->
2270 state.outlines <- Onarrow
(qsearch, outlines, b)
2273 Glut.postRedisplay
()
2275 | 21 when not allowdel
-> (* ctrl-u *)
2277 match state.outlines with
2280 let a = Array.of_list
(List.rev l
) in
2281 state.outlines <- Oarray
a;
2283 | Onarrow
(_
, _
, b) ->
2284 state.outlines <- Oarray
b;
2288 state.mode <- Outline
(allowdel
, 0, 0, outline, qsearch, pan
, oldmode
);
2289 Glut.postRedisplay
()
2291 | 12 -> (* ctrl-l *)
2292 state.mode <- Outline
2293 (allowdel
, active, firstof active, outlines, qsearch, pan
, oldmode
);
2294 Glut.postRedisplay
()
2296 | 127 when allowdel
-> (* delete *)
2297 let len = Array.length
outlines - 1 in
2301 state.bookmarks <- [];
2304 let bookmarks = Array.init
len
2306 let i = if i >= active then i + 1 else i in
2320 Glut.postRedisplay
()
2322 | _
-> dolog "unknown key %d" key
2325 let keyboard ~key ~x ~
y =
2328 if key
= 7 (* ctrl-g *)
2332 match state.mode with
2333 | Outline
outline -> outlinekeyboard key
outline
2334 | Textentry
textentry -> textentrykeyboard key
textentry
2335 | Birdseye
birdseye -> birdseyekeyboard key
birdseye
2336 | View
-> viewkeyboard key
2337 | Items
items -> itemskeyboard key
items
2340 let birdseyespecial key
((conf, leftx
, _
, hooverpageno
, anchor) as beye
) =
2342 | Glut.KEY_UP
-> upbirdseye beye
2343 | Glut.KEY_DOWN
-> downbirdseye beye
2345 | Glut.KEY_PAGE_UP
->
2346 begin match state.layout with
2350 state.mode <- Birdseye
(
2351 conf, leftx
, l
.pageno, hooverpageno
, anchor
2353 gotopage1 l
.pageno 0;
2356 let layout = layout (state.y-conf.winh
) conf.winh
in
2358 | [] -> gotoy (clamp (-conf.winh
))
2360 state.mode <- Birdseye
(
2361 conf, leftx
, l
.pageno, hooverpageno
, anchor
2363 gotopage1 l
.pageno 0
2366 | [] -> gotoy (clamp (-conf.winh
))
2369 | Glut.KEY_PAGE_DOWN
->
2370 begin match List.rev
state.layout with
2372 let layout = layout (state.y + conf.winh
) conf.winh
in
2373 begin match layout with
2375 let incr = l
.pageh
- l
.pagevh in
2380 conf, leftx
, state.pagecount
- 1, hooverpageno
, anchor
2382 Glut.postRedisplay
();
2384 else gotoy (clamp (incr + conf.interpagespace*2));
2388 Birdseye
(conf, leftx
, l
.pageno, hooverpageno
, anchor);
2389 gotopage1 l
.pageno 0;
2392 | [] -> gotoy (clamp conf.winh
)
2396 state.mode <- Birdseye
(conf, leftx
, 0, hooverpageno
, anchor);
2400 let pageno = state.pagecount
- 1 in
2401 state.mode <- Birdseye
(conf, leftx
, pageno, hooverpageno
, anchor);
2402 if not
(pagevisible state.layout pageno)
2405 match List.rev
state.pdims
with
2407 | (_
, _
, h, _
) :: _
-> h
2409 gotoy (max
0 (getpagey pageno - (conf.winh
- h - conf.interpagespace)))
2410 else Glut.postRedisplay
();
2414 let setautoscrollspeed goingdown
=
2415 let incr = max
1 (state.ascrollstep
/ 2) in
2416 let astep = max
1 (state.ascrollstep
+ (if goingdown
then incr else -incr)) in
2417 state.ascrollstep
<- astep;
2420 let special ~key ~x ~
y =
2423 match state.mode with
2424 | View
| (Birdseye _
) when key
= Glut.KEY_F9
->
2428 birdseyespecial key vals
2430 | View
when key
= Glut.KEY_F1
->
2434 if state.ascrollstep
> 0 && (key
= Glut.KEY_DOWN
|| key
= Glut.KEY_UP
)
2435 then setautoscrollspeed (key
= Glut.KEY_DOWN
)
2439 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
2440 | Glut.KEY_UP
-> clamp (-conf.scrollstep
)
2441 | Glut.KEY_DOWN
-> clamp conf.scrollstep
2442 | Glut.KEY_PAGE_UP
->
2443 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2445 match state.layout with
2447 | l
:: _
-> state.y - l
.pagey
2450 | Glut.KEY_PAGE_DOWN
->
2451 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2453 match List.rev
state.layout with
2455 | l
:: _
-> getpagey l
.pageno
2458 | Glut.KEY_HOME
-> addnav (); 0
2461 state.maxy - (if conf.maxhfit
then conf.winh
else 0)
2463 | (Glut.KEY_RIGHT
| Glut.KEY_LEFT
) when
2464 Glut.getModifiers
() land Glut.active_alt
!= 0 ->
2465 getnav (if key
= Glut.KEY_LEFT
then 1 else -1)
2467 | Glut.KEY_RIGHT
when conf.zoom > 1.0 ->
2468 state.x
<- state.x
- 10;
2470 | Glut.KEY_LEFT
when conf.zoom > 1.0 ->
2471 state.x
<- state.x
+ 10;
2476 gotoy_and_clear_text y
2479 ((c, _
, (Some
(action, _
) as onhist), onkey
, ondone), mode) ->
2482 | Glut.KEY_UP
-> action HCprev
2483 | Glut.KEY_DOWN
-> action HCnext
2484 | Glut.KEY_HOME
-> action HCfirst
2485 | Glut.KEY_END
-> action HClast
2488 state.mode <- Textentry
((c, s, onhist, onkey
, ondone), mode);
2489 Glut.postRedisplay
()
2493 | Items
(active, first
, items, qsearch, pan
, oldmode
) ->
2494 let maxrows = maxoutlinerows () in
2495 let itemcount = Array.length
items in
2496 let hasaction = function
2497 | (_
, _
, Noaction
) -> false
2500 let find start
incr =
2502 if i = -1 || i = itemcount
2505 if hasaction items.(i)
2507 else find (i + incr)
2512 let set active first
=
2513 let first = max
0 (min
first (itemcount - maxrows)) in
2514 state.mode <- Items
(active, first, items, qsearch, pan
, oldmode
)
2517 let isvisible first n = n >= first && n - first <= maxrows in
2519 let incr1 = if incr > 0 then 1 else -1 in
2520 if isvisible first active
2523 let next = active + incr in
2525 if next < 0 || next >= itemcount
2527 else find next incr1
2529 if next = -1 || abs
(active - next) > maxrows
2535 let first = first + incr in
2536 let first = max
0 (min
first (itemcount - 1)) in
2538 let next = active + incr in
2539 let next = max
0 (min
next (itemcount - 1)) in
2542 let active = if next = -1 then active else next in
2545 let first = min
next first in
2548 let first = first + incr in
2549 let first = max
0 (min
first (itemcount - 1)) in
2551 let next = active + incr in
2552 let next = max
0 (min
next (itemcount - 1)) in
2553 let next = find next incr1 in
2554 if next = -1 || abs
(active - first) > maxrows
2561 Glut.postRedisplay
()
2563 begin match key
with
2564 | Glut.KEY_UP
-> navigate ~
-1
2565 | Glut.KEY_DOWN
-> navigate 1
2566 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
2567 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
2570 state.mode <- Items
(
2571 active, first, items, qsearch, min
0 (pan
- 1), oldmode
2573 Glut.postRedisplay
()
2576 state.mode <- Items
(
2577 active, first, items, qsearch, min
0 (pan
+ 1), oldmode
2579 Glut.postRedisplay
()
2582 let active = find 0 1 in
2584 Glut.postRedisplay
()
2587 let first = max
0 (itemcount - maxrows) in
2588 let active = find (itemcount - 1) ~
-1 in
2590 Glut.postRedisplay
()
2595 | Outline
(allowdel
, active, first, outlines, qsearch, pan
, oldmode
) ->
2596 let maxrows = maxoutlinerows () in
2597 let calcfirst first active =
2600 let rows = active - first in
2601 if rows > maxrows then active - maxrows else first
2605 let active = active + incr in
2606 let active = max
0 (min
active (Array.length
outlines - 1)) in
2607 let first = calcfirst first active in
2608 state.mode <- Outline
(
2609 allowdel
, active, first, outlines, qsearch, pan
, oldmode
2611 Glut.postRedisplay
()
2613 let updownlevel incr =
2614 let len = Array.length
outlines in
2615 let (_
, curlevel
, _
) = outlines.(active) in
2617 if i = len then i-1 else if i = -1 then 0 else
2618 let (_
, l
, _
) = outlines.(i) in
2619 if l
!= curlevel
then i else flow (i+incr)
2621 let active = flow active in
2622 let first = calcfirst first active in
2623 state.mode <- Outline
(
2624 allowdel
, active, first, outlines, qsearch, pan
, oldmode
2626 Glut.postRedisplay
()
2629 | Glut.KEY_UP
-> navigate ~
-1
2630 | Glut.KEY_DOWN
-> navigate 1
2631 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
2632 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
2635 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2637 state.mode <- Outline
(
2638 allowdel
, active, first, outlines,
2639 qsearch, min
0 (pan
+ 1), oldmode
2641 Glut.postRedisplay
();
2649 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2651 state.mode <- Outline
(
2652 allowdel
, active, first, outlines, qsearch, pan
- 1, oldmode
2654 Glut.postRedisplay
();
2658 then updownlevel ~
-1
2662 state.mode <- Outline
(
2663 allowdel
, 0, 0, outlines, qsearch, pan
, oldmode
2665 Glut.postRedisplay
()
2668 let active = Array.length
outlines - 1 in
2669 let first = max
0 (active - maxrows) in
2670 state.mode <- Outline
(
2671 allowdel
, active, first, outlines, qsearch, pan
, oldmode
2673 Glut.postRedisplay
()
2678 let drawplaceholder l
=
2679 let margin = state.x
+ (conf.winw
- (state.w + state.scrollw
)) / 2 in
2681 (float l
.pagex
, float l
.pagedispy
)
2682 (float (l
.pagew
+ l
.pagex
), float (l
.pagedispy
+ l
.pagevh))
2684 let x = if margin < 0 then -margin else l
.pagex
2685 and y = l
.pagedispy
+ 13 in
2686 GlDraw.color
(0.0, 0.0, 0.0);
2687 drawstring 13 x y ("Loading " ^ string_of_int
(l
.pageno + 1))
2690 let now () = Unix.gettimeofday
();;
2694 match state.mode with
2695 | Textentry _
-> scalecolor 0.4
2696 | View
| Outline _
| Items _
-> scalecolor 1.0
2697 | Birdseye
(_
, _
, pageno, hooverpageno
, _
) ->
2698 if l
.pageno = hooverpageno
2701 if l
.pageno = pageno
2707 begin match getopaque l
.pageno with
2708 | Some
(opaque, _
) when validopaque opaque ->
2710 draw
(l
.pagedispy
, l
.pagew
, l
.pagevh, l
.pagey, conf.hlinks
)
2714 vlog "draw %d %f sec" l
.pageno d;
2722 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
2723 let sh = (float (maxy + conf.winh
) /. float conf.winh
) in
2724 let sh = float conf.winh
/. sh in
2725 let sh = max
sh (float conf.scrollh
) in
2728 if state.y = state.maxy
2730 else float y /. float maxy
2732 let position = (float conf.winh
-. sh) *. percent in
2735 if position +. sh > float conf.winh
2736 then float conf.winh
-. sh
2742 let scrollindicator () =
2743 GlDraw.color (0.64 , 0.64, 0.64);
2745 (float (conf.winw
- state.scrollw
), 0.)
2746 (float conf.winw
, float conf.winh
)
2748 GlDraw.color (0.0, 0.0, 0.0);
2750 let position, sh = scrollph state.y in
2752 (float (conf.winw
- state.scrollw
), position)
2753 (float conf.winw
, position +. sh)
2757 let showsel margin =
2758 match state.mstate
with
2759 | Mnone
| Mscroll _
| Mpan _
| Mzoom _
->
2762 | Msel
((x0
, y0
), (x1
, y1
)) ->
2763 let rec loop = function
2765 if (y0
>= l
.pagedispy
&& y0
<= (l
.pagedispy
+ l
.pagevh))
2766 || ((y1
>= l
.pagedispy
&& y1
<= (l
.pagedispy
+ l
.pagevh)))
2768 match getopaque l
.pageno with
2769 | Some
(opaque, _
) when validopaque opaque ->
2770 let oy = -l
.pagey + l
.pagedispy
in
2772 (x0
- margin - state.x, y0
,
2773 x1
- margin - state.x, y1
) oy;
2783 let panx = float state.x in
2785 GlDraw.color (0.0, 0.0, 1.0) ~alpha
:0.5;
2786 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2788 (fun (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) ->
2790 if l
.pageno = pageno
2792 let d = float (l
.pagedispy
- l
.pagey) in
2793 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
2794 GlDraw.begins `quads
;
2796 GlDraw.vertex2
(x0
+.panx, y0
+.d);
2797 GlDraw.vertex2
(x1
+.panx, y1
+.d);
2798 GlDraw.vertex2
(x2
+.panx, y2
+.d);
2799 GlDraw.vertex2
(x3
+.panx, y3
+.d);
2809 let showstrings active first pan
strings =
2811 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2812 GlDraw.color (0., 0., 0.) ~alpha
:0.85;
2813 GlDraw.rect (0., 0.) (float conf.winw
, float conf.winh
);
2816 GlDraw.color (1., 1., 1.);
2818 if row
= Array.length
strings || (row
- first) * 16 > conf.winh
2821 let (s, level
, _
) = strings.(row
) in
2822 let y = (row
- first) * 16 in
2823 let x = 5 + 15*(max
0 (level
+pan
)) in
2827 GlDraw.polygon_mode `both `
line;
2828 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2829 GlDraw.color (1., 1., 1.) ~alpha
:0.9;
2830 GlDraw.rect (0., float (y + 1))
2831 (float (conf.winw
- 1), float (y + 18));
2832 GlDraw.polygon_mode `both `fill
;
2834 GlDraw.color (1., 1., 1.);
2837 let l = String.length
s in
2840 let pan = pan * 2 in
2841 let pos = pan + level
in
2842 let left = l + pos in
2848 else String.sub
s (-pos) left
2850 drawstring 14 x (y + 16) s
2853 drawstring 14 (x + pan*15) (y + 16) s
2862 let showoutline (_
, active, first, outlines, _
, pan, _
) =
2863 showstrings active first pan outlines;
2866 let showitems (active, first, items, _
, pan, _
) =
2867 showstrings active first pan items;
2871 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
2872 GlDraw.viewport
margin 0 state.w conf.winh
;
2874 GlClear.color (scalecolor2 conf.bgcolor
);
2875 GlClear.clear
[`
color];
2878 Gl.enable `scissor_test
;
2879 GlMisc.scissor
0 0 (conf.winw
- state.scrollw
) conf.winh
;
2881 List.iter
drawpage state.layout;
2884 Gl.disable `scissor_test
2888 let x = -.float state.x in
2889 GlMat.translate ~
x ();
2893 GlDraw.viewport
0 0 conf.winw
conf.winh
;
2896 begin match state.mode with
2897 | Items
items -> showitems items
2898 | Outline
outline -> showoutline outline
2902 Glut.swapBuffers
();
2906 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
2907 let x = x - margin - state.x in
2908 let rec f = function
2910 begin match getopaque l.pageno with
2911 | Some
(opaque, _
) when validopaque opaque ->
2912 let y = y - l.pagedispy
in
2915 let y = l.pagey + y in
2916 let x = x - l.pagex
in
2917 match whatsunder
opaque x y with
2930 let viewmouse button bstate
x y =
2932 | Glut.OTHER_BUTTON
n when (n == 3 || n == 4) && bstate
= Glut.UP
->
2933 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2935 match state.mstate
with
2936 | Mzoom
(oldn
, i) ->
2944 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
2946 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
2948 let zoom = conf.zoom +. incr in
2950 state.mstate
<- Mzoom
(n, 0);
2952 state.mstate
<- Mzoom
(n, i+1);
2954 else state.mstate
<- Mzoom
(n, 0)
2956 | _
-> state.mstate
<- Mzoom
(n, 0)
2959 if state.ascrollstep
> 0
2961 setautoscrollspeed (n=4)
2965 then -conf.scrollstep
2966 else conf.scrollstep
2968 let incr = incr * 2 in
2969 let y = clamp incr in
2970 gotoy_and_clear_text y
2973 | Glut.LEFT_BUTTON
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
2974 if bstate
= Glut.DOWN
2976 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
2977 state.mstate
<- Mpan
(x, y)
2980 state.mstate
<- Mnone
2982 | Glut.LEFT_BUTTON
when x > conf.winw
- state.scrollw
->
2983 if bstate
= Glut.DOWN
2985 let position, sh = scrollph state.y in
2986 if y > truncate
position && y < truncate
(position +. sh)
2988 state.mstate
<- Mscroll
2990 let percent = float y /. float conf.winh
in
2991 let desty = truncate
(float (state.maxy - conf.winh
) *. percent) in
2993 state.mstate
<- Mscroll
2995 state.mstate
<- Mnone
2997 | Glut.LEFT_BUTTON
->
2998 let dest = if bstate
= Glut.DOWN
then getunder x y else Unone
in
2999 begin match dest with
3000 | Ulinkgoto
(pageno, top) ->
3004 gotopage1 pageno top;
3010 | Unone
when bstate
= Glut.DOWN
->
3011 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
3012 state.mstate
<- Mpan
(x, y);
3014 | Unone
| Utext _
->
3015 if bstate
= Glut.DOWN
3017 if conf.angle
mod 360 = 0
3019 state.mstate
<- Msel
((x, y), (x, y));
3020 Glut.postRedisplay
()
3024 match state.mstate
with
3027 | Mzoom _
| Mscroll
->
3028 state.mstate
<- Mnone
3031 Glut.setCursor
Glut.CURSOR_INHERIT
;
3032 state.mstate
<- Mnone
3034 | Msel
((_
, y0
), (_
, y1
)) ->
3036 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh))
3037 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh)))
3039 match getopaque l.pageno with
3040 | Some
(opaque, _
) when validopaque opaque ->
3044 List.iter
f state.layout;
3045 copysel
""; (* ugly *)
3046 Glut.setCursor
Glut.CURSOR_INHERIT
;
3047 state.mstate
<- Mnone
;
3054 let birdseyemouse button bstate
x y
3055 (conf, leftx
, _
, hooverpageno
, anchor) =
3057 | Glut.LEFT_BUTTON
when bstate
= Glut.UP
->
3058 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
3059 let rec loop = function
3062 if y > l.pagedispy
&& y < l.pagedispy
+ l.pagevh
3063 && x > margin && x < margin + l.pagew
3065 leavebirdseye (conf, leftx
, l.pageno, hooverpageno
, anchor) false;
3070 | Glut.OTHER_BUTTON _
-> viewmouse button bstate
x y
3074 let mouse bstate button
x y =
3075 match state.mode with
3076 | View
-> viewmouse button bstate
x y
3077 | Birdseye beye
-> birdseyemouse button bstate
x y beye
3078 | Textentry _
| Outline _
| Items _
-> ()
3081 let mouse ~button ~
state ~
x ~
y = mouse state button
x y;;
3084 match state.mode with
3087 match state.mstate
with
3088 | Mzoom _
| Mnone
-> ()
3093 state.mstate
<- Mpan
(x, y);
3094 if conf.zoom > 1.0 then state.x <- state.x + dx;
3096 gotoy_and_clear_text y
3099 state.mstate
<- Msel
(a, (x, y));
3100 Glut.postRedisplay
()
3103 let y = min
conf.winh
(max
0 y) in
3104 let percent = float y /. float conf.winh
in
3105 let y = truncate
(float (state.maxy - conf.winh
) *. percent) in
3106 gotoy_and_clear_text y
3110 match state.mode with
3111 | Birdseye
(conf, leftx
, pageno, hooverpageno
, anchor) ->
3112 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
3113 let rec loop = function
3115 if hooverpageno
!= -1
3117 state.mode <- Birdseye
(conf, leftx
, pageno, -1, anchor);
3118 Glut.postRedisplay
();
3121 if y > l.pagedispy
&& y < l.pagedispy
+ l.pagevh
3122 && x > margin && x < margin + l.pagew
3124 state.mode <- Birdseye
(conf, leftx
, pageno, l.pageno, anchor);
3125 Glut.postRedisplay
();
3131 | Outline _
| Items _
| Textentry _
-> ()
3133 match state.mstate
with
3135 begin match getunder x y with
3136 | Unone
-> Glut.setCursor
Glut.CURSOR_INHERIT
3138 if conf.underinfo
then showtext 'u'
("ri: " ^ uri
);
3139 Glut.setCursor
Glut.CURSOR_INFO
3140 | Ulinkgoto
(page
, _
) ->
3142 then showtext 'p'
("age: " ^ string_of_int page
);
3143 Glut.setCursor
Glut.CURSOR_INFO
3145 if conf.underinfo
then showtext '
f'
("ont: " ^
s);
3146 Glut.setCursor
Glut.CURSOR_TEXT
3149 | Mpan _
| Msel _
| Mzoom _
| Mscroll
->
3160 match Sys.os_type
with
3161 | "Win32" -> Sys.getenv
"HOMEPATH"
3162 | _
-> Sys.getenv
"HOME"
3165 ("Can not determine home directory location: " ^
3166 Printexc.to_string exn
);
3170 let config_of c attrs
=
3174 | "scroll-bar-width" -> { c with scrollbw
= max
0 (int_of_string
v) }
3175 | "scroll-handle-height" -> { c with scrollh
= max
0 (int_of_string
v) }
3176 | "case-insensitive-search" -> { c with icase
= bool_of_string
v }
3177 | "preload" -> { c with preload = bool_of_string
v }
3178 | "page-bias" -> { c with pagebias
= int_of_string
v }
3179 | "scroll-step" -> { c with scrollstep
= max
1 (int_of_string
v) }
3180 | "auto-scroll-step" ->
3181 { c with autoscrollstep
= max
0 (int_of_string
v) }
3182 | "max-height-fit" -> { c with maxhfit
= bool_of_string
v }
3183 | "crop-hack" -> { c with crophack
= bool_of_string
v }
3184 | "throttle" -> { c with showall
= bool_of_string
v }
3185 | "highlight-links" -> { c with hlinks
= bool_of_string
v }
3186 | "under-cursor-info" -> { c with underinfo
= bool_of_string
v }
3187 | "vertical-margin" ->
3188 { c with interpagespace = max
0 (int_of_string
v) }
3190 let zoom = float_of_string
v /. 100. in
3191 let zoom = max
0.01 (min
2.2 zoom) in
3192 { c with zoom = zoom }
3193 | "presentation" -> { c with presentation = bool_of_string
v }
3194 | "rotation-angle" -> { c with angle
= int_of_string
v }
3195 | "width" -> { c with winw
= max
20 (int_of_string
v) }
3196 | "height" -> { c with winh
= max
20 (int_of_string
v) }
3197 | "persistent-bookmarks" -> { c with savebmarks
= bool_of_string
v }
3198 | "proportional-display" -> { c with proportional
= bool_of_string
v }
3199 | "pixmap-cache-size" -> { c with memlimit
= max
2 (int_of_string
v) }
3200 | "tex-count" -> { c with texcount
= max
1 (int_of_string
v) }
3201 | "slice-height" -> { c with sliceheight
= max
2 (int_of_string
v) }
3202 | "thumbnail-width" -> { c with thumbw
= max
2 (int_of_string
v) }
3203 | "persistent-location" -> { c with jumpback
= bool_of_string
v }
3204 | "background-color" -> { c with bgcolor
= color_of_string v }
3205 | "scrollbar-in-presentation" ->
3206 { c with scrollbarinpm
= bool_of_string
v }
3207 | "ui-font" -> { c with uifont
= v }
3210 prerr_endline
("Error processing attribute (`" ^
3211 k ^
"'=`" ^
v ^
"'): " ^
Printexc.to_string exn
);
3214 let rec fold c = function
3217 let c = apply c k
v in
3223 let fromstring f pos n v d =
3226 dolog "Error processing attribute (%S=%S) at %d\n%s"
3227 n v pos (Printexc.to_string exn
)
3232 let bookmark_of attrs
=
3233 let rec fold title page rely
= function
3234 | ("title", v) :: rest
-> fold v page rely rest
3235 | ("page", v) :: rest
-> fold title v rely rest
3236 | ("rely", v) :: rest
-> fold title page
v rest
3237 | _
:: rest
-> fold title page rely rest
3238 | [] -> title, page
, rely
3240 fold "invalid" "0" "0" attrs
3244 let rec fold path page rely
pan = function
3245 | ("path", v) :: rest
-> fold v page rely
pan rest
3246 | ("page", v) :: rest
-> fold path
v rely
pan rest
3247 | ("rely", v) :: rest
-> fold path page
v pan rest
3248 | ("pan", v) :: rest
-> fold path page rely
v rest
3249 | _
:: rest
-> fold path page rely
pan rest
3250 | [] -> path
, page
, rely
, pan
3252 fold "" "0" "0" "0" attrs
3255 let setconf dst src
=
3256 dst
.scrollbw
<- src
.scrollbw
;
3257 dst
.scrollh
<- src
.scrollh
;
3258 dst
.icase
<- src
.icase
;
3259 dst
.preload <- src
.preload;
3260 dst
.pagebias
<- src
.pagebias
;
3261 dst
.verbose
<- src
.verbose
;
3262 dst
.scrollstep
<- src
.scrollstep
;
3263 dst
.maxhfit
<- src
.maxhfit
;
3264 dst
.crophack
<- src
.crophack
;
3265 dst
.autoscrollstep
<- src
.autoscrollstep
;
3266 dst
.showall
<- src
.showall
;
3267 dst
.hlinks
<- src
.hlinks
;
3268 dst
.underinfo
<- src
.underinfo
;
3269 dst
.interpagespace <- src
.interpagespace;
3270 dst
.zoom <- src
.zoom;
3271 dst
.presentation <- src
.presentation;
3272 dst
.angle
<- src
.angle
;
3273 dst
.winw
<- src
.winw
;
3274 dst
.winh
<- src
.winh
;
3275 dst
.savebmarks
<- src
.savebmarks
;
3276 dst
.memlimit
<- src
.memlimit
;
3277 dst
.proportional
<- src
.proportional
;
3278 dst
.texcount
<- src
.texcount
;
3279 dst
.sliceheight
<- src
.sliceheight
;
3280 dst
.thumbw
<- src
.thumbw
;
3281 dst
.jumpback
<- src
.jumpback
;
3282 dst
.bgcolor
<- src
.bgcolor
;
3283 dst
.scrollbarinpm
<- src
.scrollbarinpm
;
3284 dst
.uifont
<- src
.uifont
;
3288 let l = String.length
s in
3289 let b = Buffer.create
l in
3295 let h = Hashtbl.create
10 in
3296 let dc = { defconf with angle
= defconf.angle
} in
3297 let rec toplevel v t spos _
=
3299 | Vdata
| Vcdata
| Vend
-> v
3300 | Vopen
("llppconfig", _
, closed
) ->
3303 else { v with f = llppconfig
}
3305 error
"unexpected subelement at top level" s spos
3306 | Vclose _
-> error
"unexpected close at top level" s spos
3308 and llppconfig
v t spos _
=
3310 | Vdata
| Vcdata
| Vend
-> v
3311 | Vopen
("defaults", attrs
, closed
) ->
3312 let c = config_of dc attrs
in
3316 else { v with f = skip
"defaults" (fun () -> v) }
3318 | Vopen
("doc", attrs
, closed
) ->
3319 let pathent, spage
, srely
, span
= doc_of attrs
in
3320 let path = unent pathent
3321 and pageno = fromstring int_of_string spos
"page" spage
0
3322 and rely
= fromstring float_of_string spos
"rely" srely
0.0
3323 and pan = fromstring int_of_string spos
"pan" span
0 in
3324 let c = config_of dc attrs
in
3325 let anchor = (pageno, rely
) in
3327 then (Hashtbl.add
h path (c, [], pan, anchor); v)
3328 else { v with f = doc
path pan anchor c [] }
3331 error
"unexpected subelement in llppconfig" s spos
3333 | Vclose
"llppconfig" -> { v with f = toplevel }
3334 | Vclose _
-> error
"unexpected close in llppconfig" s spos
3336 and doc
path pan anchor c bookmarks v t spos _
=
3338 | Vdata
| Vcdata
-> v
3339 | Vend
-> error
"unexpected end of input in doc" s spos
3340 | Vopen
("bookmarks", _
, closed
) ->
3343 else { v with f = pbookmarks
path pan anchor c bookmarks }
3345 | Vopen
(_
, _
, _
) ->
3346 error
"unexpected subelement in doc" s spos
3349 Hashtbl.add
h path (c, List.rev
bookmarks, pan, anchor);
3350 { v with f = llppconfig
}
3352 | Vclose _
-> error
"unexpected close in doc" s spos
3354 and pbookmarks
path pan anchor c bookmarks v t spos _
=
3356 | Vdata
| Vcdata
-> v
3357 | Vend
-> error
"unexpected end of input in bookmarks" s spos
3358 | Vopen
("item", attrs
, closed
) ->
3359 let titleent, spage
, srely
= bookmark_of attrs
in
3360 let page = fromstring int_of_string spos
"page" spage
0
3361 and rely
= fromstring float_of_string spos
"rely" srely
0.0 in
3362 let bookmarks = (unent titleent, 0, (page, rely
)) :: bookmarks in
3364 then { v with f = pbookmarks
path pan anchor c bookmarks }
3367 { v with f = skip
"item" f }
3370 error
"unexpected subelement in bookmarks" s spos
3372 | Vclose
"bookmarks" ->
3373 { v with f = doc
path pan anchor c bookmarks }
3375 | Vclose _
-> error
"unexpected close in bookmarks" s spos
3377 and skip tag
f v t spos _
=
3379 | Vdata
| Vcdata
-> v
3381 error
("unexpected end of input in skipped " ^ tag
) s spos
3382 | Vopen
(tag'
, _
, closed
) ->
3386 let f'
() = { v with f = skip tag
f } in
3387 { v with f = skip tag'
f'
}
3391 else error
("unexpected close in skipped " ^ tag
) s spos
3394 parse
{ f = toplevel; accu = () } s;
3400 let len = in_channel_length ic
in
3401 let s = String.create
len in
3402 really_input ic
s 0 len;
3405 | Parse_error
(msg
, s, pos) ->
3406 let subs = subs s pos in
3407 let s = Printf.sprintf
"%s: at %d [..%s..]" msg
pos subs in
3408 failwith
("parse error: " ^
s)
3411 failwith
("config load error: " ^
Printexc.to_string exn
)
3417 let dir = Filename.concat
home ".config" in
3418 if Sys.is_directory
dir then dir else home
3421 Filename.concat
dir "llpp.conf"
3425 if Sys.file_exists
path
3428 (try Some
(open_in_bin
path)
3431 ("Error opening configuation file `" ^
path ^
"': " ^
3432 Printexc.to_string exn
);
3441 ("Error loading configuation from `" ^
path ^
"': " ^
3442 Printexc.to_string exn
);
3448 f (Hashtbl.create
0, defconf)
3453 let pc, pb
, px
, pa
=
3455 Hashtbl.find h (Filename.basename
state.path)
3456 with Not_found
-> dc, [], 0, (0, 0.0)
3460 state.bookmarks <- pb
;
3462 state.scrollw
<- conf.scrollbw
;
3464 then state.anchor <- pa
;
3465 cbput state.hists
.nav pa
;
3470 let add_attrs bb always
dc c =
3473 then Printf.bprintf bb
"\n %s='%b'" s a
3476 then Printf.bprintf bb
"\n %s='%d'" s a
3479 then Printf.bprintf bb
"\n %s='%f'" s (a*.100.)
3483 Printf.bprintf bb
"\n %s='%s'" s (color_to_string a)
3487 Printf.bprintf bb
"\n %s='%s'" s a
3491 then dc.winw
, dc.winh
3493 match state.fullscreen
with
3495 | None
-> c.winw
, c.winh
3497 let zoom, presentation, interpagespace, showall
=
3499 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
3501 match state.mode with
3502 | Birdseye
(bc
, _
, _
, _
, _
) ->
3503 bc
.zoom, bc
.presentation, bc
.interpagespace, bc
.showall
3504 | _
-> c.zoom, c.presentation, c.interpagespace, c.showall
3506 oi
"width" w dc.winw
;
3507 oi
"height" h dc.winh
;
3508 oi
"scroll-bar-width" c.scrollbw
dc.scrollbw
;
3509 oi
"scroll-handle-height" c.scrollh
dc.scrollh
;
3510 ob "case-insensitive-search" c.icase
dc.icase
;
3511 ob "preload" c.preload dc.preload;
3512 oi
"page-bias" c.pagebias
dc.pagebias
;
3513 oi
"scroll-step" c.scrollstep
dc.scrollstep
;
3514 oi
"auto-scroll-step" c.autoscrollstep
dc.autoscrollstep
;
3515 ob "max-height-fit" c.maxhfit
dc.maxhfit
;
3516 ob "crop-hack" c.crophack
dc.crophack
;
3517 ob "throttle" showall
dc.showall
;
3518 ob "highlight-links" c.hlinks
dc.hlinks
;
3519 ob "under-cursor-info" c.underinfo
dc.underinfo
;
3520 oi
"vertical-margin" interpagespace dc.interpagespace;
3521 oz
"zoom" zoom dc.zoom;
3522 ob "presentation" presentation dc.presentation;
3523 oi
"rotation-angle" c.angle
dc.angle
;
3524 ob "persistent-bookmarks" c.savebmarks
dc.savebmarks
;
3525 ob "proportional-display" c.proportional
dc.proportional
;
3526 oi
"pixmap-cache-size" c.memlimit
dc.memlimit
;
3527 oi
"texcount" c.texcount
dc.texcount
;
3528 oi
"slice-height" c.sliceheight
dc.sliceheight
;
3529 oi
"thumbnail-width" c.thumbw
dc.thumbw
;
3530 ob "persistent-location" c.jumpback
dc.jumpback
;
3531 oc
"background-color" c.bgcolor
dc.bgcolor
;
3532 ob "scrollbar-in-presentation" c.scrollbarinpm
dc.scrollbarinpm
;
3533 os
"ui-font" c.uifont
dc.uifont
;
3537 let bb = Buffer.create
32768 in
3539 let dc = if conf.bedefault
then conf else dc in
3540 Buffer.add_string
bb "<llppconfig>\n<defaults ";
3541 add_attrs bb true dc dc;
3542 Buffer.add_string
bb "/>\n";
3544 let adddoc path pan anchor c bookmarks =
3545 if bookmarks == [] && c = dc && anchor = emptyanchor
3548 Printf.bprintf
bb "<doc path='%s'"
3549 (enent
path 0 (String.length
path));
3551 if anchor <> emptyanchor
3553 let n, y = anchor in
3554 Printf.bprintf
bb " page='%d'" n;
3557 Printf.bprintf
bb " rely='%f'" y
3562 then Printf.bprintf
bb " pan='%d'" pan;
3564 add_attrs bb false dc c;
3566 begin match bookmarks with
3567 | [] -> Buffer.add_string
bb "/>\n"
3569 Buffer.add_string
bb ">\n<bookmarks>\n";
3570 List.iter
(fun (title, _level
, (page, rely
)) ->
3572 "<item title='%s' page='%d'"
3573 (enent
title 0 (String.length
title))
3578 Printf.bprintf
bb " rely='%f'" rely
3580 Buffer.add_string
bb "/>\n";
3582 Buffer.add_string
bb "</bookmarks>\n</doc>\n";
3588 match state.mode with
3589 | Birdseye
(_
, pan, _
, _
, _
) -> pan
3592 let basename = Filename.basename state.path in
3593 adddoc basename pan (getanchor ())
3596 if state.ascrollstep
> 0
3597 then state.ascrollstep
3598 else conf.autoscrollstep
}
3599 (if conf.savebmarks
then state.bookmarks else []);
3601 Hashtbl.iter
(fun path (c, bookmarks, x, y) ->
3603 then adddoc path x y c bookmarks
3605 Buffer.add_string
bb "</llppconfig>";
3608 if Buffer.length
bb > 0
3611 let tmp = path ^
".tmp" in
3612 let oc = open_out_bin
tmp in
3613 Buffer.output_buffer
oc bb;
3615 Sys.rename
tmp path;
3618 ("error while saving configuration: " ^
Printexc.to_string exn
)
3625 [("-p", Arg.String
(fun s -> state.password
<- s) , " Set password")
3626 ;("-v", Arg.Unit
(fun () -> print_endline
Help.version
; exit
0),
3627 " Print version and exit")]
3629 (fun s -> state.path <- s)
3630 ("Usage: " ^
Sys.argv
.(0) ^
" [options] some.pdf\nOptions:")
3632 if String.length
state.path = 0
3633 then (prerr_endline
"file name missing"; exit
1);
3637 let _ = Glut.init
Sys.argv
in
3638 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
3639 let () = Glut.initWindowSize
conf.winw
conf.winh
in
3640 let _ = Glut.createWindow
("llpp " ^
Filename.basename state.path) in
3643 if Sys.os_type
= "Unix"
3645 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
3647 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
3648 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
3649 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
3650 Unix.bind
sock addr;
3652 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
3653 Unix.connect
csock addr;
3654 let ssock, _ = Unix.accept
sock in
3657 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
3658 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
3662 at_exit
(fun () -> Unix.shutdown
ssock Unix.SHUTDOWN_ALL
);
3666 let () = Glut.displayFunc
display in
3667 let () = Glut.reshapeFunc
reshape in
3668 let () = Glut.keyboardFunc
keyboard in
3669 let () = Glut.specialFunc
special in
3670 let () = Glut.idleFunc
(Some
idle) in
3671 let () = Glut.mouseFunc
mouse in
3672 let () = Glut.motionFunc
motion in
3673 let () = Glut.passiveMotionFunc
pmotion in
3676 conf.angle
, conf.proportional
, conf.texcount
,
3677 conf.sliceheight
, conf.uifont
3679 state.csock <- csock;
3680 state.ssock <- ssock;
3681 state.text <- "Opening " ^
state.path;
3682 writeopen state.path state.password
;
3686 let rec handlelablglutbug () =
3689 with Glut.BadEnum
"key in special_of_int" ->
3690 showtext '
!'
" LablGlut bug: special key not recognized";
3691 handlelablglutbug ()
3693 handlelablglutbug ();