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 state.anchor <- getanchor ();
1070 reshape conf.winw
conf.winh
;
1071 state.text <- Printf.sprintf
"zoom is now %-5.1f" (zoom *. 100.0);
1075 let enterbirdseye () =
1076 let zoom = float conf.thumbw
/. float conf.winw
in
1077 let birdseyepageno =
1078 let cy = conf.winh
/ 2 in
1082 let rec fold best
= function
1085 let d = cy - (l
.pagedispy
+ l
.pagevh/2)
1086 and dbest
= cy - (best
.pagedispy
+ best
.pagevh/2) in
1087 if abs
d < abs dbest
1094 state.mode
<- Birdseye
(
1095 { conf with zoom = conf.zoom }, state.x
, birdseyepageno, -1, getanchor ()
1098 conf.presentation <- false;
1099 conf.interpagespace <- 10;
1100 conf.hlinks
<- false;
1102 state.mstate
<- Mnone
;
1103 conf.showall
<- false;
1104 Glut.setCursor
Glut.CURSOR_INHERIT
;
1107 state.text <- Printf.sprintf
"birds eye mode on (zoom %3.1f%%)"
1112 reshape conf.winw
conf.winh
;
1115 let leavebirdseye (c, leftx
, pageno, _
, anchor) goback
=
1117 conf.zoom <- c.zoom;
1118 conf.presentation <- c.presentation;
1119 conf.interpagespace <- c.interpagespace;
1120 conf.showall
<- c.showall
;
1121 conf.hlinks
<- c.hlinks
;
1125 state.text <- Printf.sprintf
"birds eye mode off (zoom %3.1f%%)"
1128 reshape conf.winw
conf.winh
;
1129 state.anchor <- if goback
then anchor else (pageno, 0.0);
1132 let togglebirdseye () =
1133 match state.mode
with
1134 | Birdseye vals
-> leavebirdseye vals
true
1135 | View
| Outline _
-> enterbirdseye ()
1139 let upbirdseye (conf, leftx
, pageno, hooverpageno
, anchor) =
1140 let pageno = max
0 (pageno - 1) in
1141 let rec loop = function
1142 | [] -> gotopage1 pageno 0
1143 | l
:: _
when l
.pageno = pageno ->
1144 if l
.pagedispy
>= 0 && l
.pagey = 0
1145 then Glut.postRedisplay
()
1146 else gotopage1 pageno 0
1147 | _
:: rest
-> loop rest
1150 state.mode
<- Birdseye
(conf, leftx
, pageno, hooverpageno
, anchor)
1153 let downbirdseye (conf, leftx
, pageno, hooverpageno
, anchor) =
1154 let pageno = min
(state.pagecount
- 1) (pageno + 1) in
1155 state.mode
<- Birdseye
(conf, leftx
, pageno, hooverpageno
, anchor);
1156 let rec loop = function
1158 let y, h = getpageyh pageno in
1159 let dy = (y - state.y) - (conf.winh
- h - conf.interpagespace) in
1161 | l
:: _
when l
.pageno = pageno ->
1162 if l
.pagevh != l
.pageh
1163 then gotoy (clamp (l
.pageh
- l
.pagevh + conf.interpagespace))
1164 else Glut.postRedisplay
()
1165 | _
:: rest
-> loop rest
1170 let optentry mode _ key
=
1171 let btos b = if b then "on" else "off" in
1172 let c = Char.unsafe_chr key
in
1176 try conf.scrollstep
<- int_of_string
s with exc
->
1177 state.text <- Printf.sprintf
"bad integer `%s': %s"
1178 s (Printexc.to_string exc
)
1180 TEswitch
("scroll step", "", None
, intentry, ondone)
1185 conf.autoscrollstep
<- int_of_string
s;
1186 if state.ascrollstep
> 0
1187 then state.ascrollstep
<- conf.autoscrollstep
;
1189 state.text <- Printf.sprintf
"bad integer `%s': %s"
1190 s (Printexc.to_string exc
)
1192 TEswitch
("auto scroll step", "", None
, intentry, ondone)
1197 let zoom = float (int_of_string
s) /. 100.0 in
1200 state.text <- Printf.sprintf
"bad integer `%s': %s"
1201 s (Printexc.to_string exc
)
1203 TEswitch
("zoom", "", None
, intentry, ondone)
1208 conf.thumbw
<- max
2 (min
1920 (int_of_string
s));
1210 Printf.sprintf
"thumbnail width is set to %d" conf.thumbw
;
1211 begin match mode
with
1213 leavebirdseye beye
false;
1218 state.text <- Printf.sprintf
"bad integer `%s': %s"
1219 s (Printexc.to_string exc
)
1221 TEswitch
("thumbnail width", "", None
, intentry, ondone)
1226 Some
(int_of_string
s)
1228 state.text <- Printf.sprintf
"bad integer `%s': %s"
1229 s (Printexc.to_string exc
);
1232 | Some angle
-> reinit angle
conf.proportional
1235 TEswitch
("rotation", "", None
, intentry, ondone)
1238 conf.icase
<- not
conf.icase
;
1239 TEdone
("case insensitive search " ^
(btos conf.icase
))
1242 conf.preload <- not
conf.preload;
1244 TEdone
("preload " ^
(btos conf.preload))
1247 conf.verbose
<- not
conf.verbose
;
1248 TEdone
("verbose " ^
(btos conf.verbose
))
1251 conf.maxhfit
<- not
conf.maxhfit
;
1252 state.maxy <- state.maxy + (if conf.maxhfit
then -conf.winh
else conf.winh
);
1253 TEdone
("maxhfit " ^
(btos conf.maxhfit
))
1256 conf.crophack
<- not
conf.crophack
;
1257 TEdone
("crophack " ^
btos conf.crophack
)
1260 conf.showall
<- not
conf.showall
;
1261 TEdone
("throttle " ^
btos conf.showall
)
1264 conf.underinfo
<- not
conf.underinfo
;
1265 TEdone
("underinfo " ^
btos conf.underinfo
)
1268 conf.savebmarks
<- not
conf.savebmarks
;
1269 TEdone
("persistent bookmarks " ^
btos conf.savebmarks
)
1275 match state.layout with
1280 conf.interpagespace <- int_of_string
s;
1281 state.maxy <- calcheight ();
1282 let y = getpagey pageno in
1285 state.text <- Printf.sprintf
"bad integer `%s': %s"
1286 s (Printexc.to_string exc
)
1288 TEswitch
("vertical margin", "", None
, intentry, ondone)
1291 reinit conf.angle
(not
conf.proportional
);
1292 TEdone
("proprortional display " ^
btos conf.proportional
)
1295 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
1299 let maxoutlinerows () = (conf.winh
- 31) / 16;;
1301 let enterselector allowdel
outlines errmsg msg
=
1302 if Array.length
outlines = 0
1304 showtext ' ' errmsg
;
1308 Glut.setCursor
Glut.CURSOR_INHERIT
;
1310 match state.layout with
1312 | {pageno=pageno} :: _
-> pageno
1316 if n = Array.length
outlines
1319 let (_
, _
, (outlinepageno
, _
)) = outlines.(n) in
1320 if outlinepageno
>= pageno then n else loop (n+1)
1324 state.mode
<- Outline
1325 (allowdel
, active, max
0 (active - maxoutlinerows () / 2), outlines, "", 0,
1327 Glut.postRedisplay
();
1331 let enteroutlinemode () =
1333 match state.outlines with
1336 let a = Array.of_list
(List.rev l
) in
1337 state.outlines <- Oarray
a;
1339 | Onarrow
(pat
, a, _
) ->
1340 a, "Outline was narrowed to `" ^ pat ^
"' (Ctrl-u to restore)"
1342 enterselector false outlines "Document has no outline" msg
;
1345 let enterbookmarkmode () =
1346 let bookmarks = Array.of_list
state.bookmarks in
1347 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1350 let mode_to_string mode
=
1351 let b = Buffer.create
10 in
1352 let rec f = function
1353 | Textentry
(_
, _
) -> Buffer.add_string
b "Textentry ";
1354 | View
-> Buffer.add_string
b "View"
1355 | Birdseye _
-> Buffer.add_string
b "Birdseye"
1356 | Items _
-> Buffer.add_string
b "Items"
1357 | Outline _
-> Buffer.add_string
b "Outline"
1363 let color_of_string s =
1364 Scanf.sscanf
s "%d/%d/%d" (fun r g
b ->
1365 (float r /. 256.0, float g
/. 256.0, float b /. 256.0)
1369 let color_to_string (r, g
, b) =
1370 let r = truncate
(r *. 256.0)
1371 and g
= truncate
(g
*. 256.0)
1372 and b = truncate
(b *. 256.0) in
1373 Printf.sprintf
"%d/%d/%d" r g
b
1376 let enterinfomode () =
1377 let btos = function true -> "on" | _
-> "off" in
1378 let mode = state.mode in
1379 let rec makeitems () =
1380 let intp name get set
=
1381 Printf.sprintf
"%-24s %d" name
(get
()), 1, Action
(
1382 fun active first _ pan
->
1387 state.text <- Printf.sprintf
"bad integer `%s': %s"
1388 s (Printexc.to_string exn
);
1391 if n != max_int
then set
n;
1393 let te = name
, "", None
, intentry, ondone in
1398 state.mode <- Items
(active, first
, makeitems (), "", pan
, mode)
1401 and boolp name get set
=
1402 Printf.sprintf
"%-24s %s" name
(btos (get
())), 1, Action
(
1403 fun active first qsearch pan
->
1406 Items
(active, first
, makeitems (), qsearch
, pan
, mode);
1408 and colorp name get set
=
1409 Printf.sprintf
"%-24s %s" name
(color_to_string (get
())), 1, Action
(
1410 fun active first _ pan
->
1411 let invalid = (nan
, nan
, nan
) in
1414 try color_of_string s
1416 state.text <- Printf.sprintf
"bad color `%s': %s"
1417 s (Printexc.to_string exn
);
1423 let te = name
, "", None
, textentry, ondone in
1428 state.mode <- Items
(active, first
, makeitems (), "", pan
, mode)
1433 let birdseye = isbirdseye mode in
1435 (if birdseye then "Setup bird's eye" else "Setup"), 0, Noaction
;
1437 boolp
"presentation"
1438 (fun () -> conf.presentation)
1440 conf.presentation <- v;
1441 state.anchor <- getanchor ();
1444 boolp
"ignore case in searches"
1445 (fun () -> conf.icase
)
1446 (fun v -> conf.icase
<- v);
1449 (fun () -> conf.preload)
1450 (fun v -> conf.preload <- v);
1453 (fun () -> conf.verbose
)
1454 (fun v -> conf.verbose
<- v);
1457 (fun () -> conf.maxhfit
)
1458 (fun v -> conf.maxhfit
<- v);
1461 (fun () -> conf.crophack
)
1462 (fun v -> conf.crophack
<- v);
1465 (fun () -> conf.showall
)
1466 (fun v -> conf.showall
<- v);
1468 boolp
"highlight links"
1469 (fun () -> conf.hlinks
)
1470 (fun v -> conf.hlinks
<- v);
1473 (fun () -> conf.underinfo
)
1474 (fun v -> conf.underinfo
<- v);
1475 boolp
"persistent bookmarks"
1476 (fun () -> conf.savebmarks
)
1477 (fun v -> conf.savebmarks
<- v);
1479 boolp
"proportional display"
1480 (fun () -> conf.proportional
)
1481 (fun v -> reinit conf.angle
v);
1483 boolp
"persistent location"
1484 (fun () -> conf.jumpback
)
1485 (fun v -> conf.jumpback
<- v);
1489 intp "vertical margin"
1490 (fun () -> conf.interpagespace)
1492 conf.interpagespace <- n;
1494 match state.layout with
1499 state.maxy <- calcheight ();
1500 let y = getpagey pageno in
1505 (fun () -> conf.pagebias
)
1506 (fun v -> conf.pagebias
<- v);
1509 (fun () -> conf.scrollstep
)
1510 (fun n -> conf.scrollstep
<- n);
1512 intp "auto scroll step"
1514 if state.ascrollstep
> 0
1515 then state.ascrollstep
1516 else conf.autoscrollstep
)
1518 if state.ascrollstep
> 0
1519 then state.ascrollstep
<- n
1520 else conf.autoscrollstep
<- n);
1523 (fun () -> truncate
(conf.zoom *. 100.))
1524 (fun v -> setzoom ((float v) /. 100.));
1527 (fun () -> conf.angle
)
1528 (fun v -> reinit v conf.proportional
);
1530 intp "scroll bar width"
1531 (fun () -> state.scrollw
)
1534 reshape conf.winw
conf.winh
;
1537 intp "scroll handle height"
1538 (fun () -> conf.scrollh
)
1539 (fun v -> conf.scrollh
<- v;);
1541 intp "thumbnail width"
1542 (fun () -> conf.thumbw
)
1544 conf.thumbw
<- min
1920 v;
1547 leavebirdseye beye
false;
1552 colorp
"background color"
1553 (fun () -> conf.bgcolor
)
1554 (fun v -> conf.bgcolor
<- v);
1557 "Presentation mode", 0, Noaction
;
1559 boolp
"scrollbar visible"
1560 (fun () -> conf.scrollbarinpm
)
1562 if v != conf.scrollbarinpm
1564 conf.scrollbarinpm
<- v;
1565 if conf.presentation
1567 state.scrollw
<- if v then conf.scrollbw
else 0;
1568 reshape conf.winw
conf.winh
;
1574 "Pixmap Cache", 0, Noaction
;
1576 intp "size (advisory)"
1577 (fun () -> conf.memlimit
)
1578 (fun v -> conf.memlimit
<- v);
1579 Printf.sprintf
"%-24s %d" "used" state.memused
, 1, Noaction
;
1586 :: ("Document", 0, Noaction
)
1587 :: List.map
(fun (_
, s) -> (s, 1, Noaction
)) state.docinfo
1593 :: (Printf.sprintf
"Save these parameters as defaults at exit (%s)"
1594 (btos conf.bedefault
),
1597 fun active first qsearch pan
->
1598 conf.bedefault
<- not
conf.bedefault
;
1599 Items
(active, first
, makeitems (), qsearch
, pan
, mode);
1604 Array.of_list
(items @ tailer)
1607 state.mode <- Items
(1, 0, makeitems (), "", 0, mode);
1608 Glut.postRedisplay
();
1611 let enterhelpmode () =
1612 state.mode <- Items
(-1, 0, state.help
, "", 0, state.mode);
1613 Glut.postRedisplay
();
1616 let quickbookmark ?title
() =
1617 match state.layout with
1623 let sec = Unix.gettimeofday
() in
1624 let tm = Unix.localtime
sec in
1625 Printf.sprintf
"Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1629 (tm.Unix.tm_year
+ 1900)
1632 | Some
title -> title
1635 (title, 0, (l
.pageno, float l
.pagey /. float l
.pageh
))
1640 state.fullscreen
<- None
;
1641 Glut.reshapeWindow
w h;
1644 let writeopen path password
=
1645 writecmd state.csock
("open " ^ path ^
"\000" ^ password ^
"\000");
1646 writecmd state.csock
"info";
1649 let opendoc path password
=
1652 state.password
<- password
;
1653 state.gen
<- state.gen
+ 1;
1655 writeopen path password
;
1656 Glut.setWindowTitle
("llpp " ^
Filename.basename path
);
1657 wcmd "geometry" [`i
state.w; `i
conf.winh
];
1660 let viewkeyboard key
=
1662 let mode = state.mode in
1663 state.mode <- Textentry
(te, fun _
-> state.mode <- mode);
1666 Glut.postRedisplay
()
1668 let c = Char.chr key
in
1670 | '
\027'
| 'q'
-> (* escape *)
1673 | '
\008'
-> (* backspace *)
1674 let y = getnav ~
-1 in
1675 gotoy_and_clear_text y
1683 Glut.postRedisplay
()
1686 let ondone isforw
s =
1687 cbput state.hists
.pat
s;
1688 state.searchpattern
<- s;
1691 let s = String.create
1 in
1693 enttext (s, "", Some
(onhist state.hists
.pat
),
1694 textentry, ondone (c ='
/'
))
1696 | '
+'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1697 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1698 setzoom (min
2.2 (conf.zoom +. incr))
1703 try int_of_string
s with exc
->
1704 state.text <- Printf.sprintf
"bad integer `%s': %s"
1705 s (Printexc.to_string exc
);
1711 state.text <- "page bias is now " ^ string_of_int
n;
1714 enttext ("page bias", "", None
, intentry, ondone)
1716 | '
-'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1717 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1718 setzoom (max
0.01 (conf.zoom -. decr))
1721 let ondone msg
= state.text <- msg
in
1723 "option [acfhilpstvAPRSZ]", "", None
,
1724 optentry state.mode, ondone
1727 | '
0'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1730 | '
1'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1731 let zoom = zoomforh
conf.winw
conf.winh
state.scrollw
in
1735 | '
9'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1741 try int_of_string
s with exc
->
1742 state.text <- Printf.sprintf
"bad integer `%s': %s"
1743 s (Printexc.to_string exc
);
1749 cbput state.hists
.pag
(string_of_int
n);
1750 gotoy_and_clear_text (getpagey (n + conf.pagebias
- 1))
1753 let pageentry text key
=
1754 match Char.unsafe_chr key
with
1755 | 'g'
-> TEdone
text
1756 | _
-> intentry text key
1758 let text = "x" in text.[0] <- c;
1759 enttext (":", text, Some
(onhist state.hists
.pag
), pageentry, ondone)
1762 state.scrollw
<- if state.scrollw
> 0 then 0 else conf.scrollbw
;
1763 reshape conf.winw
conf.winh
;
1766 conf.hlinks
<- not
conf.hlinks
;
1767 state.text <- "highlightlinks " ^
if conf.hlinks
then "on" else "off";
1768 Glut.postRedisplay
()
1771 if state.ascrollstep
= 0
1772 then state.ascrollstep
<- conf.autoscrollstep
1774 conf.autoscrollstep
<- state.ascrollstep
;
1775 state.ascrollstep
<- 0;
1779 conf.presentation <- not
conf.presentation;
1780 if conf.presentation
1782 if not
conf.scrollbarinpm
1783 then state.scrollw
<- 0;
1786 state.scrollw
<- conf.scrollbw
;
1788 showtext ' '
("presentation mode " ^
1789 if conf.presentation then "on" else "off");
1790 state.anchor <- getanchor ();
1794 begin match state.fullscreen
with
1796 state.fullscreen
<- Some
(conf.winw
, conf.winh
);
1799 state.fullscreen
<- None
;
1804 gotoy_and_clear_text 0
1807 search state.searchpattern
true
1810 search state.searchpattern
false
1813 begin match state.layout with
1816 gotoy_and_clear_text (getpagey l
.pageno)
1820 begin match List.rev
state.layout with
1823 let pageno = min
(l
.pageno+1) (state.pagecount
-1) in
1824 gotoy_and_clear_text (getpagey pageno)
1827 | '
\127'
-> (* delte *)
1828 begin match state.layout with
1831 let pageno = max
0 (l
.pageno-1) in
1832 gotoy_and_clear_text (getpagey pageno)
1837 if fn
= -1 then l
.pageno, l
.pageno else fn
, l
.pageno
1839 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
1841 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
1845 else (100. *. (float state.y /. float maxy)) in
1848 Printf.sprintf
"Page %d of %d %.2f%%"
1849 (fn+1) state.pagecount
percent
1852 "Pages %d-%d of %d %.2f%%"
1853 (fn+1) (ln
+1) state.pagecount
percent
1858 begin match state.layout with
1861 doreshape (l
.pagew
+ state.scrollw
) l
.pageh
;
1862 Glut.postRedisplay
();
1866 enterbookmarkmode ()
1876 match state.layout with
1879 (s, 0, (l
.pageno, float l
.pagey /. float l
.pageh
))
1883 enttext ("bookmark", "", None
, textentry, ondone)
1887 showtext ' '
"Quick bookmark added";
1890 begin match state.layout with
1892 let rect = getpdimrect l
.pagedimno
in
1896 (truncate
(1.8 *. (rect.(1) -. rect.(0))),
1897 truncate
(1.2 *. (rect.(3) -. rect.(0))))
1899 (truncate
(rect.(1) -. rect.(0)),
1900 truncate
(rect.(3) -. rect.(0)))
1904 doreshape (w + state.scrollw
) (h + conf.interpagespace)
1906 Glut.postRedisplay
();
1912 reinit (conf.angle
+ (if c = '
>'
then 30 else -30)) conf.proportional
1917 (min
(state.colorscale
+. (if c = '
]'
then 0.1 else -0.1)) 1.0);
1918 Glut.postRedisplay
()
1921 begin match state.mode with
1922 | Birdseye beye
-> upbirdseye beye
1923 | _
-> gotoy (clamp (-conf.scrollstep
))
1927 begin match state.mode with
1928 | Birdseye beye
-> downbirdseye beye
1929 | _
-> gotoy (clamp conf.scrollstep
)
1933 state.anchor <- getanchor ();
1934 opendoc state.path
state.password
1937 vlog "huh? %d %c" key
(Char.chr key
);
1940 let textentrykeyboard key
((c, text, opthist
, onkey
, ondone), onleave
) =
1942 state.mode <- Textentry
(te, onleave
);
1945 Glut.postRedisplay
()
1947 match Char.unsafe_chr key
with
1948 | '
\008'
-> (* backspace *)
1949 let len = String.length
text in
1953 Glut.postRedisplay
();
1956 let s = String.sub
text 0 (len - 1) in
1957 enttext (c, s, opthist
, onkey
, ondone)
1963 Glut.postRedisplay
()
1965 | '
\027'
-> (* escape *)
1966 begin match opthist
with
1968 | Some
(_
, onhistcancel
) -> onhistcancel
()
1971 Glut.postRedisplay
()
1974 begin match onkey
text key
with
1978 Glut.postRedisplay
()
1981 enttext (c, text, opthist
, onkey
, ondone);
1985 Glut.postRedisplay
()
1988 state.mode <- Textentry
(te, onleave
);
1989 Glut.postRedisplay
()
1993 let birdseyekeyboard key
((_
, _
, pageno, _
, _
) as beye
) =
1995 | 27 -> (* escape *)
1996 leavebirdseye beye
true
1998 | 12 -> (* ctrl-l *)
1999 let y, h = getpageyh pageno in
2000 let top = (conf.winh
- h) / 2 in
2001 gotoy (max
0 (y - top))
2004 leavebirdseye beye
false
2010 let itemskeyboard key
(active, first
, items, qsearch
, pan
, oldmode
) =
2011 let set active first qsearch
=
2012 state.mode <- Items
(active, first
, items, qsearch
, pan
, oldmode
)
2014 let search active pattern
incr =
2017 if n = Array.length
items || n = -1
2020 let (s, _
, _
) = items.(n) in
2022 (try ignore
(Str.search_forward re
s 0); true
2023 with Not_found
-> false)
2025 else loop (n + incr)
2030 let re = Str.regexp_case_fold pattern
in
2036 let firstof active = max
0 (active - maxoutlinerows () / 2) in
2038 | 18 | 19 -> (* ctrl-r/ctlr-s *)
2039 let incr = if key
= 18 then -1 else 1 in
2041 match search (active + incr) qsearch
incr with
2043 state.text <- qsearch ^
" [not found]";
2046 state.text <- qsearch
;
2047 active, firstof active
2049 set active first qsearch
;
2050 Glut.postRedisplay
();
2052 | 8 -> (* backspace *)
2053 let len = String.length qsearch
in
2060 set active first
"";
2063 let qsearch = String.sub
qsearch 0 (len - 1) in
2065 match search active qsearch ~
-1 with
2067 state.text <- qsearch ^
" [not found]";
2070 state.text <- qsearch;
2071 active, firstof active
2073 set active first
qsearch
2075 Glut.postRedisplay
()
2077 | _
when key
>= 32 && key
< 127 ->
2078 let pattern = addchar qsearch (Char.chr key
) in
2080 match search active pattern 1 with
2082 state.text <- pattern ^
" [not found]";
2085 state.text <- pattern;
2086 active, firstof active
2088 set active first
pattern;
2089 Glut.postRedisplay
()
2091 | 27 -> (* escape *)
2093 state.mode <- oldmode
;
2094 Glut.postRedisplay
();
2097 if active < Array.length
items
2099 match items.(active) with
2101 state.mode <- f active first
qsearch pan
2105 state.mode <- oldmode
2107 Glut.postRedisplay
();
2109 | _
-> dolog "unknown key %d" key
2112 let outlinekeyboard key
2113 (allowdel
, active, first
, outlines, qsearch, pan
, oldmode
) =
2114 let narrow outlines pattern =
2115 let reopt = try Some
(Str.regexp_case_fold
pattern) with _
-> None
in
2119 let rec fold accu n =
2123 let (s, _
, _
) as o
= outlines.(n) in
2125 if (try ignore
(Str.search_forward
re s 0); true
2126 with Not_found
-> false)
2132 let matched = fold [] (Array.length
outlines - 1) in
2133 if matched = [] then None
else Some
(Array.of_list
matched)
2135 let search active pattern incr =
2138 if n = Array.length
outlines || n = -1
2141 let (s, _
, _
) = outlines.(n) in
2143 (try ignore
(Str.search_forward
re s 0); true
2144 with Not_found
-> false)
2146 else loop (n + incr)
2151 let re = Str.regexp_case_fold
pattern in
2157 let firstof active = max
0 (active - maxoutlinerows () / 2) in
2159 | 27 -> (* escape *)
2160 if String.length
qsearch = 0
2163 state.mode <- oldmode
;
2164 Glut.postRedisplay
();
2168 state.mode <- Outline
(
2169 allowdel
, active, first
, outlines, "", pan
, oldmode
2171 Glut.postRedisplay
();
2174 | 18 | 19 -> (* ctrl-r/ctrl-s *)
2175 let incr = if key
= 18 then -1 else 1 in
2177 match search (active + incr) qsearch incr with
2179 state.text <- qsearch ^
" [not found]";
2182 state.text <- qsearch;
2183 active, firstof active
2185 state.mode <- Outline
(
2186 allowdel
, active, first
, outlines, qsearch, pan
, oldmode
2188 Glut.postRedisplay
();
2190 | 8 -> (* backspace *)
2191 let len = String.length
qsearch in
2198 state.mode <- Outline
(
2199 allowdel
, active, first
, outlines, "", pan
, oldmode
2203 let qsearch = String.sub
qsearch 0 (len - 1) in
2205 match search active qsearch ~
-1 with
2207 state.text <- qsearch ^
" [not found]";
2210 state.text <- qsearch;
2211 active, firstof active
2213 state.mode <- Outline
(
2214 allowdel
, active, first
, outlines, qsearch, pan
, oldmode
2217 Glut.postRedisplay
()
2220 if active < Array.length
outlines
2222 let (_
, _
, anchor) = outlines.(active) in
2227 if allowdel
then state.bookmarks <- Array.to_list
outlines;
2228 state.mode <- oldmode
;
2229 Glut.postRedisplay
();
2231 | _
when key
>= 32 && key
< 127 ->
2232 let pattern = addchar qsearch (Char.chr key
) in
2234 match search active pattern 1 with
2236 state.text <- pattern ^
" [not found]";
2239 state.text <- pattern;
2240 active, firstof active
2242 state.mode <- Outline
(
2243 allowdel
, active, first
, outlines, pattern, pan
, oldmode
2245 Glut.postRedisplay
()
2247 | 14 when not allowdel
-> (* ctrl-n *)
2248 if String.length
qsearch > 0
2250 let optoutlines = narrow outlines qsearch in
2251 begin match optoutlines with
2252 | None
-> state.text <- "can't narrow"
2254 state.mode <- Outline
(
2255 allowdel
, 0, 0, outlines, qsearch, pan
, oldmode
2257 match state.outlines with
2260 state.outlines <- Onarrow
(qsearch, outlines, a)
2261 | Onarrow
(_
, _
, b) ->
2262 state.outlines <- Onarrow
(qsearch, outlines, b)
2265 Glut.postRedisplay
()
2267 | 21 when not allowdel
-> (* ctrl-u *)
2269 match state.outlines with
2272 let a = Array.of_list
(List.rev l
) in
2273 state.outlines <- Oarray
a;
2275 | Onarrow
(_
, _
, b) ->
2276 state.outlines <- Oarray
b;
2280 state.mode <- Outline
(allowdel
, 0, 0, outline, qsearch, pan
, oldmode
);
2281 Glut.postRedisplay
()
2283 | 12 -> (* ctrl-l *)
2284 state.mode <- Outline
2285 (allowdel
, active, firstof active, outlines, qsearch, pan
, oldmode
);
2286 Glut.postRedisplay
()
2288 | 127 when allowdel
-> (* delete *)
2289 let len = Array.length
outlines - 1 in
2293 state.bookmarks <- [];
2296 let bookmarks = Array.init
len
2298 let i = if i >= active then i + 1 else i in
2312 Glut.postRedisplay
()
2314 | _
-> dolog "unknown key %d" key
2317 let keyboard ~key ~x ~
y =
2320 if key
= 7 (* ctrl-g *)
2324 match state.mode with
2325 | Outline
outline -> outlinekeyboard key
outline
2326 | Textentry
textentry -> textentrykeyboard key
textentry
2327 | Birdseye
birdseye -> birdseyekeyboard key
birdseye
2328 | View
-> viewkeyboard key
2329 | Items
items -> itemskeyboard key
items
2332 let birdseyespecial key
((conf, leftx
, _
, hooverpageno
, anchor) as beye
) =
2334 | Glut.KEY_UP
-> upbirdseye beye
2335 | Glut.KEY_DOWN
-> downbirdseye beye
2337 | Glut.KEY_PAGE_UP
->
2338 begin match state.layout with
2342 state.mode <- Birdseye
(
2343 conf, leftx
, l
.pageno, hooverpageno
, anchor
2345 gotopage1 l
.pageno 0;
2348 let layout = layout (state.y-conf.winh
) conf.winh
in
2350 | [] -> gotoy (clamp (-conf.winh
))
2352 state.mode <- Birdseye
(
2353 conf, leftx
, l
.pageno, hooverpageno
, anchor
2355 gotopage1 l
.pageno 0
2358 | [] -> gotoy (clamp (-conf.winh
))
2361 | Glut.KEY_PAGE_DOWN
->
2362 begin match List.rev
state.layout with
2364 let layout = layout (state.y + conf.winh
) conf.winh
in
2365 begin match layout with
2367 let incr = l
.pageh
- l
.pagevh in
2372 conf, leftx
, state.pagecount
- 1, hooverpageno
, anchor
2374 Glut.postRedisplay
();
2376 else gotoy (clamp (incr + conf.interpagespace*2));
2380 Birdseye
(conf, leftx
, l
.pageno, hooverpageno
, anchor);
2381 gotopage1 l
.pageno 0;
2384 | [] -> gotoy (clamp conf.winh
)
2388 state.mode <- Birdseye
(conf, leftx
, 0, hooverpageno
, anchor);
2392 let pageno = state.pagecount
- 1 in
2393 state.mode <- Birdseye
(conf, leftx
, pageno, hooverpageno
, anchor);
2394 if not
(pagevisible state.layout pageno)
2397 match List.rev
state.pdims
with
2399 | (_
, _
, h, _
) :: _
-> h
2401 gotoy (max
0 (getpagey pageno - (conf.winh
- h - conf.interpagespace)))
2402 else Glut.postRedisplay
();
2406 let setautoscrollspeed goingdown
=
2407 let incr = max
1 (state.ascrollstep
/ 2) in
2408 let astep = max
1 (state.ascrollstep
+ (if goingdown
then incr else -incr)) in
2409 state.ascrollstep
<- astep;
2412 let special ~key ~x ~
y =
2415 match state.mode with
2416 | View
| (Birdseye _
) when key
= Glut.KEY_F9
->
2420 birdseyespecial key vals
2422 | View
when key
= Glut.KEY_F1
->
2426 if state.ascrollstep
> 0 && (key
= Glut.KEY_DOWN
|| key
= Glut.KEY_UP
)
2427 then setautoscrollspeed (key
= Glut.KEY_DOWN
)
2431 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
2432 | Glut.KEY_UP
-> clamp (-conf.scrollstep
)
2433 | Glut.KEY_DOWN
-> clamp conf.scrollstep
2434 | Glut.KEY_PAGE_UP
->
2435 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2437 match state.layout with
2439 | l
:: _
-> state.y - l
.pagey
2442 | Glut.KEY_PAGE_DOWN
->
2443 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2445 match List.rev
state.layout with
2447 | l
:: _
-> getpagey l
.pageno
2450 | Glut.KEY_HOME
-> addnav (); 0
2453 state.maxy - (if conf.maxhfit
then conf.winh
else 0)
2455 | (Glut.KEY_RIGHT
| Glut.KEY_LEFT
) when
2456 Glut.getModifiers
() land Glut.active_alt
!= 0 ->
2457 getnav (if key
= Glut.KEY_LEFT
then 1 else -1)
2459 | Glut.KEY_RIGHT
when conf.zoom > 1.0 ->
2460 state.x
<- state.x
- 10;
2462 | Glut.KEY_LEFT
when conf.zoom > 1.0 ->
2463 state.x
<- state.x
+ 10;
2468 gotoy_and_clear_text y
2471 ((c, _
, (Some
(action, _
) as onhist), onkey
, ondone), mode) ->
2474 | Glut.KEY_UP
-> action HCprev
2475 | Glut.KEY_DOWN
-> action HCnext
2476 | Glut.KEY_HOME
-> action HCfirst
2477 | Glut.KEY_END
-> action HClast
2480 state.mode <- Textentry
((c, s, onhist, onkey
, ondone), mode);
2481 Glut.postRedisplay
()
2485 | Items
(active, first
, items, qsearch, pan
, oldmode
) ->
2486 let maxrows = maxoutlinerows () in
2487 let itemcount = Array.length
items in
2488 let hasaction = function
2489 | (_
, _
, Noaction
) -> false
2492 let find start
incr =
2494 if i = -1 || i = itemcount
2497 if hasaction items.(i)
2499 else find (i + incr)
2504 let set active first
=
2505 let first = max
0 (min
first (itemcount - maxrows)) in
2506 state.mode <- Items
(active, first, items, qsearch, pan
, oldmode
)
2509 let isvisible first n = n >= first && n - first <= maxrows in
2511 let incr1 = if incr > 0 then 1 else -1 in
2512 if isvisible first active
2515 let next = active + incr in
2517 if next < 0 || next >= itemcount
2519 else find next incr1
2521 if next = -1 || abs
(active - next) > maxrows
2527 let first = first + incr in
2528 let first = max
0 (min
first (itemcount - 1)) in
2530 let next = active + incr in
2531 let next = max
0 (min
next (itemcount - 1)) in
2534 let active = if next = -1 then active else next in
2537 let first = min
next first in
2540 let first = first + incr in
2541 let first = max
0 (min
first (itemcount - 1)) in
2543 let next = active + incr in
2544 let next = max
0 (min
next (itemcount - 1)) in
2545 let next = find next incr1 in
2546 if next = -1 || abs
(active - first) > maxrows
2553 Glut.postRedisplay
()
2555 begin match key
with
2556 | Glut.KEY_UP
-> navigate ~
-1
2557 | Glut.KEY_DOWN
-> navigate 1
2558 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
2559 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
2562 state.mode <- Items
(
2563 active, first, items, qsearch, min
0 (pan
- 1), oldmode
2565 Glut.postRedisplay
()
2568 state.mode <- Items
(
2569 active, first, items, qsearch, min
0 (pan
+ 1), oldmode
2571 Glut.postRedisplay
()
2574 let active = find 0 1 in
2576 Glut.postRedisplay
()
2579 let first = max
0 (itemcount - maxrows) in
2580 let active = find (itemcount - 1) ~
-1 in
2582 Glut.postRedisplay
()
2587 | Outline
(allowdel
, active, first, outlines, qsearch, pan
, oldmode
) ->
2588 let maxrows = maxoutlinerows () in
2589 let calcfirst first active =
2592 let rows = active - first in
2593 if rows > maxrows then active - maxrows else first
2597 let active = active + incr in
2598 let active = max
0 (min
active (Array.length
outlines - 1)) in
2599 let first = calcfirst first active in
2600 state.mode <- Outline
(
2601 allowdel
, active, first, outlines, qsearch, pan
, oldmode
2603 Glut.postRedisplay
()
2605 let updownlevel incr =
2606 let len = Array.length
outlines in
2607 let (_
, curlevel
, _
) = outlines.(active) in
2609 if i = len then i-1 else if i = -1 then 0 else
2610 let (_
, l
, _
) = outlines.(i) in
2611 if l
!= curlevel
then i else flow (i+incr)
2613 let active = flow active in
2614 let first = calcfirst first active in
2615 state.mode <- Outline
(
2616 allowdel
, active, first, outlines, qsearch, pan
, oldmode
2618 Glut.postRedisplay
()
2621 | Glut.KEY_UP
-> navigate ~
-1
2622 | Glut.KEY_DOWN
-> navigate 1
2623 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
2624 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
2627 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2629 state.mode <- Outline
(
2630 allowdel
, active, first, outlines,
2631 qsearch, min
0 (pan
+ 1), oldmode
2633 Glut.postRedisplay
();
2641 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2643 state.mode <- Outline
(
2644 allowdel
, active, first, outlines, qsearch, pan
- 1, oldmode
2646 Glut.postRedisplay
();
2650 then updownlevel ~
-1
2654 state.mode <- Outline
(
2655 allowdel
, 0, 0, outlines, qsearch, pan
, oldmode
2657 Glut.postRedisplay
()
2660 let active = Array.length
outlines - 1 in
2661 let first = max
0 (active - maxrows) in
2662 state.mode <- Outline
(
2663 allowdel
, active, first, outlines, qsearch, pan
, oldmode
2665 Glut.postRedisplay
()
2670 let drawplaceholder l
=
2671 let margin = state.x
+ (conf.winw
- (state.w + state.scrollw
)) / 2 in
2673 (float l
.pagex
, float l
.pagedispy
)
2674 (float (l
.pagew
+ l
.pagex
), float (l
.pagedispy
+ l
.pagevh))
2676 let x = if margin < 0 then -margin else l
.pagex
2677 and y = l
.pagedispy
+ 13 in
2678 GlDraw.color
(0.0, 0.0, 0.0);
2679 drawstring 13 x y ("Loading " ^ string_of_int
(l
.pageno + 1))
2682 let now () = Unix.gettimeofday
();;
2686 match state.mode with
2687 | Textentry _
-> scalecolor 0.4
2688 | View
| Outline _
| Items _
-> scalecolor 1.0
2689 | Birdseye
(_
, _
, pageno, hooverpageno
, _
) ->
2690 if l
.pageno = hooverpageno
2693 if l
.pageno = pageno
2699 begin match getopaque l
.pageno with
2700 | Some
(opaque, _
) when validopaque opaque ->
2702 draw
(l
.pagedispy
, l
.pagew
, l
.pagevh, l
.pagey, conf.hlinks
)
2706 vlog "draw %d %f sec" l
.pageno d;
2714 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
2715 let sh = (float (maxy + conf.winh
) /. float conf.winh
) in
2716 let sh = float conf.winh
/. sh in
2717 let sh = max
sh (float conf.scrollh
) in
2720 if state.y = state.maxy
2722 else float y /. float maxy
2724 let position = (float conf.winh
-. sh) *. percent in
2727 if position +. sh > float conf.winh
2728 then float conf.winh
-. sh
2734 let scrollindicator () =
2735 GlDraw.color (0.64 , 0.64, 0.64);
2737 (float (conf.winw
- state.scrollw
), 0.)
2738 (float conf.winw
, float conf.winh
)
2740 GlDraw.color (0.0, 0.0, 0.0);
2742 let position, sh = scrollph state.y in
2744 (float (conf.winw
- state.scrollw
), position)
2745 (float conf.winw
, position +. sh)
2749 let showsel margin =
2750 match state.mstate
with
2751 | Mnone
| Mscroll _
| Mpan _
| Mzoom _
->
2754 | Msel
((x0
, y0
), (x1
, y1
)) ->
2755 let rec loop = function
2757 if (y0
>= l
.pagedispy
&& y0
<= (l
.pagedispy
+ l
.pagevh))
2758 || ((y1
>= l
.pagedispy
&& y1
<= (l
.pagedispy
+ l
.pagevh)))
2760 match getopaque l
.pageno with
2761 | Some
(opaque, _
) when validopaque opaque ->
2762 let oy = -l
.pagey + l
.pagedispy
in
2764 (x0
- margin - state.x, y0
,
2765 x1
- margin - state.x, y1
) oy;
2775 let panx = float state.x in
2777 GlDraw.color (0.0, 0.0, 1.0) ~alpha
:0.5;
2778 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2780 (fun (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) ->
2782 if l
.pageno = pageno
2784 let d = float (l
.pagedispy
- l
.pagey) in
2785 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
2786 GlDraw.begins `quads
;
2788 GlDraw.vertex2
(x0
+.panx, y0
+.d);
2789 GlDraw.vertex2
(x1
+.panx, y1
+.d);
2790 GlDraw.vertex2
(x2
+.panx, y2
+.d);
2791 GlDraw.vertex2
(x3
+.panx, y3
+.d);
2801 let showstrings active first pan
strings =
2803 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2804 GlDraw.color (0., 0., 0.) ~alpha
:0.85;
2805 GlDraw.rect (0., 0.) (float conf.winw
, float conf.winh
);
2808 GlDraw.color (1., 1., 1.);
2810 if row
= Array.length
strings || (row
- first) * 16 > conf.winh
2813 let (s, level
, _
) = strings.(row
) in
2814 let y = (row
- first) * 16 in
2815 let x = 5 + 15*(max
0 (level
+pan
)) in
2819 GlDraw.polygon_mode `both `
line;
2820 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2821 GlDraw.color (1., 1., 1.) ~alpha
:0.9;
2822 GlDraw.rect (0., float (y + 1))
2823 (float (conf.winw
- 1), float (y + 18));
2824 GlDraw.polygon_mode `both `fill
;
2826 GlDraw.color (1., 1., 1.);
2829 let l = String.length
s in
2832 let pan = pan * 2 in
2833 let pos = pan + level
in
2834 let left = l + pos in
2840 else String.sub
s (-pos) left
2842 drawstring 14 x (y + 16) s
2845 drawstring 14 (x + pan*15) (y + 16) s
2854 let showoutline (_
, active, first, outlines, _
, pan, _
) =
2855 showstrings active first pan outlines;
2858 let showitems (active, first, items, _
, pan, _
) =
2859 showstrings active first pan items;
2863 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
2864 GlDraw.viewport
margin 0 state.w conf.winh
;
2866 GlClear.color (scalecolor2 conf.bgcolor
);
2867 GlClear.clear
[`
color];
2870 Gl.enable `scissor_test
;
2871 GlMisc.scissor
0 0 (conf.winw
- state.scrollw
) conf.winh
;
2873 List.iter
drawpage state.layout;
2876 Gl.disable `scissor_test
2880 let x = -.float state.x in
2881 GlMat.translate ~
x ();
2885 GlDraw.viewport
0 0 conf.winw
conf.winh
;
2888 begin match state.mode with
2889 | Items
items -> showitems items
2890 | Outline
outline -> showoutline outline
2894 Glut.swapBuffers
();
2898 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
2899 let x = x - margin - state.x in
2900 let rec f = function
2902 begin match getopaque l.pageno with
2903 | Some
(opaque, _
) when validopaque opaque ->
2904 let y = y - l.pagedispy
in
2907 let y = l.pagey + y in
2908 let x = x - l.pagex
in
2909 match whatsunder
opaque x y with
2922 let viewmouse button bstate
x y =
2924 | Glut.OTHER_BUTTON
n when (n == 3 || n == 4) && bstate
= Glut.UP
->
2925 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2927 match state.mstate
with
2928 | Mzoom
(oldn
, i) ->
2936 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
2938 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
2940 let zoom = conf.zoom +. incr in
2942 state.mstate
<- Mzoom
(n, 0);
2944 state.mstate
<- Mzoom
(n, i+1);
2946 else state.mstate
<- Mzoom
(n, 0)
2948 | _
-> state.mstate
<- Mzoom
(n, 0)
2951 if state.ascrollstep
> 0
2953 setautoscrollspeed (n=4)
2957 then -conf.scrollstep
2958 else conf.scrollstep
2960 let incr = incr * 2 in
2961 let y = clamp incr in
2962 gotoy_and_clear_text y
2965 | Glut.LEFT_BUTTON
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
2966 if bstate
= Glut.DOWN
2968 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
2969 state.mstate
<- Mpan
(x, y)
2972 state.mstate
<- Mnone
2974 | Glut.LEFT_BUTTON
when x > conf.winw
- state.scrollw
->
2975 if bstate
= Glut.DOWN
2977 let position, sh = scrollph state.y in
2978 if y > truncate
position && y < truncate
(position +. sh)
2980 state.mstate
<- Mscroll
2982 let percent = float y /. float conf.winh
in
2983 let desty = truncate
(float (state.maxy - conf.winh
) *. percent) in
2985 state.mstate
<- Mscroll
2987 state.mstate
<- Mnone
2989 | Glut.LEFT_BUTTON
->
2990 let dest = if bstate
= Glut.DOWN
then getunder x y else Unone
in
2991 begin match dest with
2992 | Ulinkgoto
(pageno, top) ->
2996 gotopage1 pageno top;
3002 | Unone
when bstate
= Glut.DOWN
->
3003 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
3004 state.mstate
<- Mpan
(x, y);
3006 | Unone
| Utext _
->
3007 if bstate
= Glut.DOWN
3009 if conf.angle
mod 360 = 0
3011 state.mstate
<- Msel
((x, y), (x, y));
3012 Glut.postRedisplay
()
3016 match state.mstate
with
3019 | Mzoom _
| Mscroll
->
3020 state.mstate
<- Mnone
3023 Glut.setCursor
Glut.CURSOR_INHERIT
;
3024 state.mstate
<- Mnone
3026 | Msel
((_
, y0
), (_
, y1
)) ->
3028 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh))
3029 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh)))
3031 match getopaque l.pageno with
3032 | Some
(opaque, _
) when validopaque opaque ->
3036 List.iter
f state.layout;
3037 copysel
""; (* ugly *)
3038 Glut.setCursor
Glut.CURSOR_INHERIT
;
3039 state.mstate
<- Mnone
;
3046 let birdseyemouse button bstate
x y
3047 (conf, leftx
, _
, hooverpageno
, anchor) =
3049 | Glut.LEFT_BUTTON
when bstate
= Glut.UP
->
3050 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
3051 let rec loop = function
3054 if y > l.pagedispy
&& y < l.pagedispy
+ l.pagevh
3055 && x > margin && x < margin + l.pagew
3057 leavebirdseye (conf, leftx
, l.pageno, hooverpageno
, anchor) false;
3062 | Glut.OTHER_BUTTON _
-> viewmouse button bstate
x y
3066 let mouse bstate button
x y =
3067 match state.mode with
3068 | View
-> viewmouse button bstate
x y
3069 | Birdseye beye
-> birdseyemouse button bstate
x y beye
3070 | Textentry _
| Outline _
| Items _
-> ()
3073 let mouse ~button ~
state ~
x ~
y = mouse state button
x y;;
3076 match state.mode with
3079 match state.mstate
with
3080 | Mzoom _
| Mnone
-> ()
3085 state.mstate
<- Mpan
(x, y);
3086 if conf.zoom > 1.0 then state.x <- state.x + dx;
3088 gotoy_and_clear_text y
3091 state.mstate
<- Msel
(a, (x, y));
3092 Glut.postRedisplay
()
3095 let y = min
conf.winh
(max
0 y) in
3096 let percent = float y /. float conf.winh
in
3097 let y = truncate
(float (state.maxy - conf.winh
) *. percent) in
3098 gotoy_and_clear_text y
3102 match state.mode with
3103 | Birdseye
(conf, leftx
, pageno, hooverpageno
, anchor) ->
3104 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
3105 let rec loop = function
3107 if hooverpageno
!= -1
3109 state.mode <- Birdseye
(conf, leftx
, pageno, -1, anchor);
3110 Glut.postRedisplay
();
3113 if y > l.pagedispy
&& y < l.pagedispy
+ l.pagevh
3114 && x > margin && x < margin + l.pagew
3116 state.mode <- Birdseye
(conf, leftx
, pageno, l.pageno, anchor);
3117 Glut.postRedisplay
();
3123 | Outline _
| Items _
| Textentry _
-> ()
3125 match state.mstate
with
3127 begin match getunder x y with
3128 | Unone
-> Glut.setCursor
Glut.CURSOR_INHERIT
3130 if conf.underinfo
then showtext 'u'
("ri: " ^ uri
);
3131 Glut.setCursor
Glut.CURSOR_INFO
3132 | Ulinkgoto
(page
, _
) ->
3134 then showtext 'p'
("age: " ^ string_of_int page
);
3135 Glut.setCursor
Glut.CURSOR_INFO
3137 if conf.underinfo
then showtext '
f'
("ont: " ^
s);
3138 Glut.setCursor
Glut.CURSOR_TEXT
3141 | Mpan _
| Msel _
| Mzoom _
| Mscroll
->
3152 match Sys.os_type
with
3153 | "Win32" -> Sys.getenv
"HOMEPATH"
3154 | _
-> Sys.getenv
"HOME"
3157 ("Can not determine home directory location: " ^
3158 Printexc.to_string exn
);
3162 let config_of c attrs
=
3166 | "scroll-bar-width" -> { c with scrollbw
= max
0 (int_of_string
v) }
3167 | "scroll-handle-height" -> { c with scrollh
= max
0 (int_of_string
v) }
3168 | "case-insensitive-search" -> { c with icase
= bool_of_string
v }
3169 | "preload" -> { c with preload = bool_of_string
v }
3170 | "page-bias" -> { c with pagebias
= int_of_string
v }
3171 | "scroll-step" -> { c with scrollstep
= max
1 (int_of_string
v) }
3172 | "auto-scroll-step" ->
3173 { c with autoscrollstep
= max
0 (int_of_string
v) }
3174 | "max-height-fit" -> { c with maxhfit
= bool_of_string
v }
3175 | "crop-hack" -> { c with crophack
= bool_of_string
v }
3176 | "throttle" -> { c with showall
= bool_of_string
v }
3177 | "highlight-links" -> { c with hlinks
= bool_of_string
v }
3178 | "under-cursor-info" -> { c with underinfo
= bool_of_string
v }
3179 | "vertical-margin" ->
3180 { c with interpagespace = max
0 (int_of_string
v) }
3182 let zoom = float_of_string
v /. 100. in
3183 let zoom = max
0.01 (min
2.2 zoom) in
3184 { c with zoom = zoom }
3185 | "presentation" -> { c with presentation = bool_of_string
v }
3186 | "rotation-angle" -> { c with angle
= int_of_string
v }
3187 | "width" -> { c with winw
= max
20 (int_of_string
v) }
3188 | "height" -> { c with winh
= max
20 (int_of_string
v) }
3189 | "persistent-bookmarks" -> { c with savebmarks
= bool_of_string
v }
3190 | "proportional-display" -> { c with proportional
= bool_of_string
v }
3191 | "pixmap-cache-size" -> { c with memlimit
= max
2 (int_of_string
v) }
3192 | "tex-count" -> { c with texcount
= max
1 (int_of_string
v) }
3193 | "slice-height" -> { c with sliceheight
= max
2 (int_of_string
v) }
3194 | "thumbnail-width" -> { c with thumbw
= max
2 (int_of_string
v) }
3195 | "persistent-location" -> { c with jumpback
= bool_of_string
v }
3196 | "background-color" -> { c with bgcolor
= color_of_string v }
3197 | "scrollbar-in-presentation" ->
3198 { c with scrollbarinpm
= bool_of_string
v }
3199 | "ui-font" -> { c with uifont
= v }
3202 prerr_endline
("Error processing attribute (`" ^
3203 k ^
"'=`" ^
v ^
"'): " ^
Printexc.to_string exn
);
3206 let rec fold c = function
3209 let c = apply c k
v in
3215 let fromstring f pos n v d =
3218 dolog "Error processing attribute (%S=%S) at %d\n%s"
3219 n v pos (Printexc.to_string exn
)
3224 let bookmark_of attrs
=
3225 let rec fold title page rely
= function
3226 | ("title", v) :: rest
-> fold v page rely rest
3227 | ("page", v) :: rest
-> fold title v rely rest
3228 | ("rely", v) :: rest
-> fold title page
v rest
3229 | _
:: rest
-> fold title page rely rest
3230 | [] -> title, page
, rely
3232 fold "invalid" "0" "0" attrs
3236 let rec fold path page rely
pan = function
3237 | ("path", v) :: rest
-> fold v page rely
pan rest
3238 | ("page", v) :: rest
-> fold path
v rely
pan rest
3239 | ("rely", v) :: rest
-> fold path page
v pan rest
3240 | ("pan", v) :: rest
-> fold path page rely
v rest
3241 | _
:: rest
-> fold path page rely
pan rest
3242 | [] -> path
, page
, rely
, pan
3244 fold "" "0" "0" "0" attrs
3247 let setconf dst src
=
3248 dst
.scrollbw
<- src
.scrollbw
;
3249 dst
.scrollh
<- src
.scrollh
;
3250 dst
.icase
<- src
.icase
;
3251 dst
.preload <- src
.preload;
3252 dst
.pagebias
<- src
.pagebias
;
3253 dst
.verbose
<- src
.verbose
;
3254 dst
.scrollstep
<- src
.scrollstep
;
3255 dst
.maxhfit
<- src
.maxhfit
;
3256 dst
.crophack
<- src
.crophack
;
3257 dst
.autoscrollstep
<- src
.autoscrollstep
;
3258 dst
.showall
<- src
.showall
;
3259 dst
.hlinks
<- src
.hlinks
;
3260 dst
.underinfo
<- src
.underinfo
;
3261 dst
.interpagespace <- src
.interpagespace;
3262 dst
.zoom <- src
.zoom;
3263 dst
.presentation <- src
.presentation;
3264 dst
.angle
<- src
.angle
;
3265 dst
.winw
<- src
.winw
;
3266 dst
.winh
<- src
.winh
;
3267 dst
.savebmarks
<- src
.savebmarks
;
3268 dst
.memlimit
<- src
.memlimit
;
3269 dst
.proportional
<- src
.proportional
;
3270 dst
.texcount
<- src
.texcount
;
3271 dst
.sliceheight
<- src
.sliceheight
;
3272 dst
.thumbw
<- src
.thumbw
;
3273 dst
.jumpback
<- src
.jumpback
;
3274 dst
.bgcolor
<- src
.bgcolor
;
3275 dst
.scrollbarinpm
<- src
.scrollbarinpm
;
3276 dst
.uifont
<- src
.uifont
;
3280 let l = String.length
s in
3281 let b = Buffer.create
l in
3287 let h = Hashtbl.create
10 in
3288 let dc = { defconf with angle
= defconf.angle
} in
3289 let rec toplevel v t spos _
=
3291 | Vdata
| Vcdata
| Vend
-> v
3292 | Vopen
("llppconfig", _
, closed
) ->
3295 else { v with f = llppconfig
}
3297 error
"unexpected subelement at top level" s spos
3298 | Vclose _
-> error
"unexpected close at top level" s spos
3300 and llppconfig
v t spos _
=
3302 | Vdata
| Vcdata
| Vend
-> v
3303 | Vopen
("defaults", attrs
, closed
) ->
3304 let c = config_of dc attrs
in
3308 else { v with f = skip
"defaults" (fun () -> v) }
3310 | Vopen
("doc", attrs
, closed
) ->
3311 let pathent, spage
, srely
, span
= doc_of attrs
in
3312 let path = unent pathent
3313 and pageno = fromstring int_of_string spos
"page" spage
0
3314 and rely
= fromstring float_of_string spos
"rely" srely
0.0
3315 and pan = fromstring int_of_string spos
"pan" span
0 in
3316 let c = config_of dc attrs
in
3317 let anchor = (pageno, rely
) in
3319 then (Hashtbl.add
h path (c, [], pan, anchor); v)
3320 else { v with f = doc
path pan anchor c [] }
3323 error
"unexpected subelement in llppconfig" s spos
3325 | Vclose
"llppconfig" -> { v with f = toplevel }
3326 | Vclose _
-> error
"unexpected close in llppconfig" s spos
3328 and doc
path pan anchor c bookmarks v t spos _
=
3330 | Vdata
| Vcdata
-> v
3331 | Vend
-> error
"unexpected end of input in doc" s spos
3332 | Vopen
("bookmarks", _
, closed
) ->
3335 else { v with f = pbookmarks
path pan anchor c bookmarks }
3337 | Vopen
(_
, _
, _
) ->
3338 error
"unexpected subelement in doc" s spos
3341 Hashtbl.add
h path (c, List.rev
bookmarks, pan, anchor);
3342 { v with f = llppconfig
}
3344 | Vclose _
-> error
"unexpected close in doc" s spos
3346 and pbookmarks
path pan anchor c bookmarks v t spos _
=
3348 | Vdata
| Vcdata
-> v
3349 | Vend
-> error
"unexpected end of input in bookmarks" s spos
3350 | Vopen
("item", attrs
, closed
) ->
3351 let titleent, spage
, srely
= bookmark_of attrs
in
3352 let page = fromstring int_of_string spos
"page" spage
0
3353 and rely
= fromstring float_of_string spos
"rely" srely
0.0 in
3354 let bookmarks = (unent titleent, 0, (page, rely
)) :: bookmarks in
3356 then { v with f = pbookmarks
path pan anchor c bookmarks }
3359 { v with f = skip
"item" f }
3362 error
"unexpected subelement in bookmarks" s spos
3364 | Vclose
"bookmarks" ->
3365 { v with f = doc
path pan anchor c bookmarks }
3367 | Vclose _
-> error
"unexpected close in bookmarks" s spos
3369 and skip tag
f v t spos _
=
3371 | Vdata
| Vcdata
-> v
3373 error
("unexpected end of input in skipped " ^ tag
) s spos
3374 | Vopen
(tag'
, _
, closed
) ->
3378 let f'
() = { v with f = skip tag
f } in
3379 { v with f = skip tag'
f'
}
3383 else error
("unexpected close in skipped " ^ tag
) s spos
3386 parse
{ f = toplevel; accu = () } s;
3392 let len = in_channel_length ic
in
3393 let s = String.create
len in
3394 really_input ic
s 0 len;
3397 | Parse_error
(msg
, s, pos) ->
3398 let subs = subs s pos in
3399 let s = Printf.sprintf
"%s: at %d [..%s..]" msg
pos subs in
3400 failwith
("parse error: " ^
s)
3403 failwith
("config load error: " ^
Printexc.to_string exn
)
3409 let dir = Filename.concat
home ".config" in
3410 if Sys.is_directory
dir then dir else home
3413 Filename.concat
dir "llpp.conf"
3417 if Sys.file_exists
path
3420 (try Some
(open_in_bin
path)
3423 ("Error opening configuation file `" ^
path ^
"': " ^
3424 Printexc.to_string exn
);
3433 ("Error loading configuation from `" ^
path ^
"': " ^
3434 Printexc.to_string exn
);
3440 f (Hashtbl.create
0, defconf)
3445 let pc, pb
, px
, pa
=
3447 Hashtbl.find h (Filename.basename
state.path)
3448 with Not_found
-> dc, [], 0, (0, 0.0)
3452 state.bookmarks <- pb
;
3454 state.scrollw
<- conf.scrollbw
;
3456 then state.anchor <- pa
;
3457 cbput state.hists
.nav pa
;
3462 let add_attrs bb always
dc c =
3465 then Printf.bprintf bb
"\n %s='%b'" s a
3468 then Printf.bprintf bb
"\n %s='%d'" s a
3471 then Printf.bprintf bb
"\n %s='%f'" s (a*.100.)
3475 Printf.bprintf bb
"\n %s='%s'" s (color_to_string a)
3479 Printf.bprintf bb
"\n %s='%s'" s a
3483 then dc.winw
, dc.winh
3485 match state.fullscreen
with
3487 | None
-> c.winw
, c.winh
3489 let zoom, presentation, interpagespace, showall
=
3491 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
3493 match state.mode with
3494 | Birdseye
(bc
, _
, _
, _
, _
) ->
3495 bc
.zoom, bc
.presentation, bc
.interpagespace, bc
.showall
3496 | _
-> c.zoom, c.presentation, c.interpagespace, c.showall
3498 oi
"width" w dc.winw
;
3499 oi
"height" h dc.winh
;
3500 oi
"scroll-bar-width" c.scrollbw
dc.scrollbw
;
3501 oi
"scroll-handle-height" c.scrollh
dc.scrollh
;
3502 ob "case-insensitive-search" c.icase
dc.icase
;
3503 ob "preload" c.preload dc.preload;
3504 oi
"page-bias" c.pagebias
dc.pagebias
;
3505 oi
"scroll-step" c.scrollstep
dc.scrollstep
;
3506 oi
"auto-scroll-step" c.autoscrollstep
dc.autoscrollstep
;
3507 ob "max-height-fit" c.maxhfit
dc.maxhfit
;
3508 ob "crop-hack" c.crophack
dc.crophack
;
3509 ob "throttle" showall
dc.showall
;
3510 ob "highlight-links" c.hlinks
dc.hlinks
;
3511 ob "under-cursor-info" c.underinfo
dc.underinfo
;
3512 oi
"vertical-margin" interpagespace dc.interpagespace;
3513 oz
"zoom" zoom dc.zoom;
3514 ob "presentation" presentation dc.presentation;
3515 oi
"rotation-angle" c.angle
dc.angle
;
3516 ob "persistent-bookmarks" c.savebmarks
dc.savebmarks
;
3517 ob "proportional-display" c.proportional
dc.proportional
;
3518 oi
"pixmap-cache-size" c.memlimit
dc.memlimit
;
3519 oi
"texcount" c.texcount
dc.texcount
;
3520 oi
"slice-height" c.sliceheight
dc.sliceheight
;
3521 oi
"thumbnail-width" c.thumbw
dc.thumbw
;
3522 ob "persistent-location" c.jumpback
dc.jumpback
;
3523 oc
"background-color" c.bgcolor
dc.bgcolor
;
3524 ob "scrollbar-in-presentation" c.scrollbarinpm
dc.scrollbarinpm
;
3525 os
"ui-font" c.uifont
dc.uifont
;
3529 let bb = Buffer.create
32768 in
3531 let dc = if conf.bedefault
then conf else dc in
3532 Buffer.add_string
bb "<llppconfig>\n<defaults ";
3533 add_attrs bb true dc dc;
3534 Buffer.add_string
bb "/>\n";
3536 let adddoc path pan anchor c bookmarks =
3537 if bookmarks == [] && c = dc && anchor = emptyanchor
3540 Printf.bprintf
bb "<doc path='%s'"
3541 (enent
path 0 (String.length
path));
3543 if anchor <> emptyanchor
3545 let n, y = anchor in
3546 Printf.bprintf
bb " page='%d'" n;
3549 Printf.bprintf
bb " rely='%f'" y
3554 then Printf.bprintf
bb " pan='%d'" pan;
3556 add_attrs bb false dc c;
3558 begin match bookmarks with
3559 | [] -> Buffer.add_string
bb "/>\n"
3561 Buffer.add_string
bb ">\n<bookmarks>\n";
3562 List.iter
(fun (title, _level
, (page, rely
)) ->
3564 "<item title='%s' page='%d'"
3565 (enent
title 0 (String.length
title))
3570 Printf.bprintf
bb " rely='%f'" rely
3572 Buffer.add_string
bb "/>\n";
3574 Buffer.add_string
bb "</bookmarks>\n</doc>\n";
3580 match state.mode with
3581 | Birdseye
(_
, pan, _
, _
, _
) -> pan
3584 let basename = Filename.basename state.path in
3585 adddoc basename pan (getanchor ())
3588 if state.ascrollstep
> 0
3589 then state.ascrollstep
3590 else conf.autoscrollstep
}
3591 (if conf.savebmarks
then state.bookmarks else []);
3593 Hashtbl.iter
(fun path (c, bookmarks, x, y) ->
3595 then adddoc path x y c bookmarks
3597 Buffer.add_string
bb "</llppconfig>";
3600 if Buffer.length
bb > 0
3603 let tmp = path ^
".tmp" in
3604 let oc = open_out_bin
tmp in
3605 Buffer.output_buffer
oc bb;
3607 Sys.rename
tmp path;
3610 ("error while saving configuration: " ^
Printexc.to_string exn
)
3617 [("-p", Arg.String
(fun s -> state.password
<- s) , " Set password")
3618 ;("-v", Arg.Unit
(fun () -> print_endline
Help.version
; exit
0),
3619 " Print version and exit")]
3621 (fun s -> state.path <- s)
3622 ("Usage: " ^
Sys.argv
.(0) ^
" [options] some.pdf\nOptions:")
3624 if String.length
state.path = 0
3625 then (prerr_endline
"file name missing"; exit
1);
3629 let _ = Glut.init
Sys.argv
in
3630 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
3631 let () = Glut.initWindowSize
conf.winw
conf.winh
in
3632 let _ = Glut.createWindow
("llpp " ^
Filename.basename state.path) in
3635 if Sys.os_type
= "Unix"
3637 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
3639 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
3640 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
3641 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
3642 Unix.bind
sock addr;
3644 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
3645 Unix.connect
csock addr;
3646 let ssock, _ = Unix.accept
sock in
3649 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
3650 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
3654 at_exit
(fun () -> Unix.shutdown
ssock Unix.SHUTDOWN_ALL
);
3658 let () = Glut.displayFunc
display in
3659 let () = Glut.reshapeFunc
reshape in
3660 let () = Glut.keyboardFunc
keyboard in
3661 let () = Glut.specialFunc
special in
3662 let () = Glut.idleFunc
(Some
idle) in
3663 let () = Glut.mouseFunc
mouse in
3664 let () = Glut.motionFunc
motion in
3665 let () = Glut.passiveMotionFunc
pmotion in
3668 conf.angle
, conf.proportional
, conf.texcount
,
3669 conf.sliceheight
, conf.uifont
3671 state.csock <- csock;
3672 state.ssock <- ssock;
3673 state.text <- "Opening " ^
state.path;
3674 writeopen state.path state.password
;
3678 let rec handlelablglutbug () =
3681 with Glut.BadEnum
"key in special_of_int" ->
3682 showtext '
!'
" LablGlut bug: special key not recognized";
3683 handlelablglutbug ()
3685 handlelablglutbug ();