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 >= 0 && active < Array.length
items
2099 match items.(active) with
2101 state.mode <- f active first
qsearch pan
2105 state.mode <- oldmode
2109 state.mode <- oldmode
2111 Glut.postRedisplay
();
2113 | _
-> dolog "unknown key %d" key
2116 let outlinekeyboard key
2117 (allowdel
, active, first
, outlines, qsearch, pan
, oldmode
) =
2118 let narrow outlines pattern =
2119 let reopt = try Some
(Str.regexp_case_fold
pattern) with _
-> None
in
2123 let rec fold accu n =
2127 let (s, _
, _
) as o
= outlines.(n) in
2129 if (try ignore
(Str.search_forward
re s 0); true
2130 with Not_found
-> false)
2136 let matched = fold [] (Array.length
outlines - 1) in
2137 if matched = [] then None
else Some
(Array.of_list
matched)
2139 let search active pattern incr =
2142 if n = Array.length
outlines || n = -1
2145 let (s, _
, _
) = outlines.(n) in
2147 (try ignore
(Str.search_forward
re s 0); true
2148 with Not_found
-> false)
2150 else loop (n + incr)
2155 let re = Str.regexp_case_fold
pattern in
2161 let firstof active = max
0 (active - maxoutlinerows () / 2) in
2163 | 27 -> (* escape *)
2164 if String.length
qsearch = 0
2167 state.mode <- oldmode
;
2168 Glut.postRedisplay
();
2172 state.mode <- Outline
(
2173 allowdel
, active, first
, outlines, "", pan
, oldmode
2175 Glut.postRedisplay
();
2178 | 18 | 19 -> (* ctrl-r/ctrl-s *)
2179 let incr = if key
= 18 then -1 else 1 in
2181 match search (active + incr) qsearch incr with
2183 state.text <- qsearch ^
" [not found]";
2186 state.text <- qsearch;
2187 active, firstof active
2189 state.mode <- Outline
(
2190 allowdel
, active, first
, outlines, qsearch, pan
, oldmode
2192 Glut.postRedisplay
();
2194 | 8 -> (* backspace *)
2195 let len = String.length
qsearch in
2202 state.mode <- Outline
(
2203 allowdel
, active, first
, outlines, "", pan
, oldmode
2207 let qsearch = String.sub
qsearch 0 (len - 1) in
2209 match search active qsearch ~
-1 with
2211 state.text <- qsearch ^
" [not found]";
2214 state.text <- qsearch;
2215 active, firstof active
2217 state.mode <- Outline
(
2218 allowdel
, active, first
, outlines, qsearch, pan
, oldmode
2221 Glut.postRedisplay
()
2224 if active < Array.length
outlines
2226 let (_
, _
, anchor) = outlines.(active) in
2231 if allowdel
then state.bookmarks <- Array.to_list
outlines;
2232 state.mode <- oldmode
;
2233 Glut.postRedisplay
();
2235 | _
when key
>= 32 && key
< 127 ->
2236 let pattern = addchar qsearch (Char.chr key
) in
2238 match search active pattern 1 with
2240 state.text <- pattern ^
" [not found]";
2243 state.text <- pattern;
2244 active, firstof active
2246 state.mode <- Outline
(
2247 allowdel
, active, first
, outlines, pattern, pan
, oldmode
2249 Glut.postRedisplay
()
2251 | 14 when not allowdel
-> (* ctrl-n *)
2252 if String.length
qsearch > 0
2254 let optoutlines = narrow outlines qsearch in
2255 begin match optoutlines with
2256 | None
-> state.text <- "can't narrow"
2258 state.mode <- Outline
(
2259 allowdel
, 0, 0, outlines, qsearch, pan
, oldmode
2261 match state.outlines with
2264 state.outlines <- Onarrow
(qsearch, outlines, a)
2265 | Onarrow
(_
, _
, b) ->
2266 state.outlines <- Onarrow
(qsearch, outlines, b)
2269 Glut.postRedisplay
()
2271 | 21 when not allowdel
-> (* ctrl-u *)
2273 match state.outlines with
2276 let a = Array.of_list
(List.rev l
) in
2277 state.outlines <- Oarray
a;
2279 | Onarrow
(_
, _
, b) ->
2280 state.outlines <- Oarray
b;
2284 state.mode <- Outline
(allowdel
, 0, 0, outline, qsearch, pan
, oldmode
);
2285 Glut.postRedisplay
()
2287 | 12 -> (* ctrl-l *)
2288 state.mode <- Outline
2289 (allowdel
, active, firstof active, outlines, qsearch, pan
, oldmode
);
2290 Glut.postRedisplay
()
2292 | 127 when allowdel
-> (* delete *)
2293 let len = Array.length
outlines - 1 in
2297 state.bookmarks <- [];
2300 let bookmarks = Array.init
len
2302 let i = if i >= active then i + 1 else i in
2316 Glut.postRedisplay
()
2318 | _
-> dolog "unknown key %d" key
2321 let keyboard ~key ~x ~
y =
2324 if key
= 7 (* ctrl-g *)
2328 match state.mode with
2329 | Outline
outline -> outlinekeyboard key
outline
2330 | Textentry
textentry -> textentrykeyboard key
textentry
2331 | Birdseye
birdseye -> birdseyekeyboard key
birdseye
2332 | View
-> viewkeyboard key
2333 | Items
items -> itemskeyboard key
items
2336 let birdseyespecial key
((conf, leftx
, _
, hooverpageno
, anchor) as beye
) =
2338 | Glut.KEY_UP
-> upbirdseye beye
2339 | Glut.KEY_DOWN
-> downbirdseye beye
2341 | Glut.KEY_PAGE_UP
->
2342 begin match state.layout with
2346 state.mode <- Birdseye
(
2347 conf, leftx
, l
.pageno, hooverpageno
, anchor
2349 gotopage1 l
.pageno 0;
2352 let layout = layout (state.y-conf.winh
) conf.winh
in
2354 | [] -> gotoy (clamp (-conf.winh
))
2356 state.mode <- Birdseye
(
2357 conf, leftx
, l
.pageno, hooverpageno
, anchor
2359 gotopage1 l
.pageno 0
2362 | [] -> gotoy (clamp (-conf.winh
))
2365 | Glut.KEY_PAGE_DOWN
->
2366 begin match List.rev
state.layout with
2368 let layout = layout (state.y + conf.winh
) conf.winh
in
2369 begin match layout with
2371 let incr = l
.pageh
- l
.pagevh in
2376 conf, leftx
, state.pagecount
- 1, hooverpageno
, anchor
2378 Glut.postRedisplay
();
2380 else gotoy (clamp (incr + conf.interpagespace*2));
2384 Birdseye
(conf, leftx
, l
.pageno, hooverpageno
, anchor);
2385 gotopage1 l
.pageno 0;
2388 | [] -> gotoy (clamp conf.winh
)
2392 state.mode <- Birdseye
(conf, leftx
, 0, hooverpageno
, anchor);
2396 let pageno = state.pagecount
- 1 in
2397 state.mode <- Birdseye
(conf, leftx
, pageno, hooverpageno
, anchor);
2398 if not
(pagevisible state.layout pageno)
2401 match List.rev
state.pdims
with
2403 | (_
, _
, h, _
) :: _
-> h
2405 gotoy (max
0 (getpagey pageno - (conf.winh
- h - conf.interpagespace)))
2406 else Glut.postRedisplay
();
2410 let setautoscrollspeed goingdown
=
2411 let incr = max
1 (state.ascrollstep
/ 2) in
2412 let astep = max
1 (state.ascrollstep
+ (if goingdown
then incr else -incr)) in
2413 state.ascrollstep
<- astep;
2416 let special ~key ~x ~
y =
2419 match state.mode with
2420 | View
| (Birdseye _
) when key
= Glut.KEY_F9
->
2424 birdseyespecial key vals
2426 | View
when key
= Glut.KEY_F1
->
2430 if state.ascrollstep
> 0 && (key
= Glut.KEY_DOWN
|| key
= Glut.KEY_UP
)
2431 then setautoscrollspeed (key
= Glut.KEY_DOWN
)
2435 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
2436 | Glut.KEY_UP
-> clamp (-conf.scrollstep
)
2437 | Glut.KEY_DOWN
-> clamp conf.scrollstep
2438 | Glut.KEY_PAGE_UP
->
2439 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2441 match state.layout with
2443 | l
:: _
-> state.y - l
.pagey
2446 | Glut.KEY_PAGE_DOWN
->
2447 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2449 match List.rev
state.layout with
2451 | l
:: _
-> getpagey l
.pageno
2454 | Glut.KEY_HOME
-> addnav (); 0
2457 state.maxy - (if conf.maxhfit
then conf.winh
else 0)
2459 | (Glut.KEY_RIGHT
| Glut.KEY_LEFT
) when
2460 Glut.getModifiers
() land Glut.active_alt
!= 0 ->
2461 getnav (if key
= Glut.KEY_LEFT
then 1 else -1)
2463 | Glut.KEY_RIGHT
when conf.zoom > 1.0 ->
2464 state.x
<- state.x
- 10;
2466 | Glut.KEY_LEFT
when conf.zoom > 1.0 ->
2467 state.x
<- state.x
+ 10;
2472 gotoy_and_clear_text y
2475 ((c, _
, (Some
(action, _
) as onhist), onkey
, ondone), mode) ->
2478 | Glut.KEY_UP
-> action HCprev
2479 | Glut.KEY_DOWN
-> action HCnext
2480 | Glut.KEY_HOME
-> action HCfirst
2481 | Glut.KEY_END
-> action HClast
2484 state.mode <- Textentry
((c, s, onhist, onkey
, ondone), mode);
2485 Glut.postRedisplay
()
2489 | Items
(active, first
, items, qsearch, pan
, oldmode
) ->
2490 let maxrows = maxoutlinerows () in
2491 let itemcount = Array.length
items in
2492 let hasaction = function
2493 | (_
, _
, Noaction
) -> false
2496 let find start
incr =
2498 if i = -1 || i = itemcount
2501 if hasaction items.(i)
2503 else find (i + incr)
2508 let set active first
=
2509 let first = max
0 (min
first (itemcount - maxrows)) in
2510 state.mode <- Items
(active, first, items, qsearch, pan
, oldmode
)
2513 let isvisible first n = n >= first && n - first <= maxrows in
2515 let incr1 = if incr > 0 then 1 else -1 in
2516 if isvisible first active
2519 let next = active + incr in
2521 if next < 0 || next >= itemcount
2523 else find next incr1
2525 if next = -1 || abs
(active - next) > maxrows
2531 let first = first + incr in
2532 let first = max
0 (min
first (itemcount - 1)) in
2534 let next = active + incr in
2535 let next = max
0 (min
next (itemcount - 1)) in
2538 let active = if next = -1 then active else next in
2541 let first = min
next first in
2544 let first = first + incr in
2545 let first = max
0 (min
first (itemcount - 1)) in
2547 let next = active + incr in
2548 let next = max
0 (min
next (itemcount - 1)) in
2549 let next = find next incr1 in
2550 if next = -1 || abs
(active - first) > maxrows
2557 Glut.postRedisplay
()
2559 begin match key
with
2560 | Glut.KEY_UP
-> navigate ~
-1
2561 | Glut.KEY_DOWN
-> navigate 1
2562 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
2563 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
2566 state.mode <- Items
(
2567 active, first, items, qsearch, min
0 (pan
- 1), oldmode
2569 Glut.postRedisplay
()
2572 state.mode <- Items
(
2573 active, first, items, qsearch, min
0 (pan
+ 1), oldmode
2575 Glut.postRedisplay
()
2578 let active = find 0 1 in
2580 Glut.postRedisplay
()
2583 let first = max
0 (itemcount - maxrows) in
2584 let active = find (itemcount - 1) ~
-1 in
2586 Glut.postRedisplay
()
2591 | Outline
(allowdel
, active, first, outlines, qsearch, pan
, oldmode
) ->
2592 let maxrows = maxoutlinerows () in
2593 let calcfirst first active =
2596 let rows = active - first in
2597 if rows > maxrows then active - maxrows else first
2601 let active = active + incr in
2602 let active = max
0 (min
active (Array.length
outlines - 1)) in
2603 let first = calcfirst first active in
2604 state.mode <- Outline
(
2605 allowdel
, active, first, outlines, qsearch, pan
, oldmode
2607 Glut.postRedisplay
()
2609 let updownlevel incr =
2610 let len = Array.length
outlines in
2611 let (_
, curlevel
, _
) = outlines.(active) in
2613 if i = len then i-1 else if i = -1 then 0 else
2614 let (_
, l
, _
) = outlines.(i) in
2615 if l
!= curlevel
then i else flow (i+incr)
2617 let active = flow active in
2618 let first = calcfirst first active in
2619 state.mode <- Outline
(
2620 allowdel
, active, first, outlines, qsearch, pan
, oldmode
2622 Glut.postRedisplay
()
2625 | Glut.KEY_UP
-> navigate ~
-1
2626 | Glut.KEY_DOWN
-> navigate 1
2627 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
2628 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
2631 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2633 state.mode <- Outline
(
2634 allowdel
, active, first, outlines,
2635 qsearch, min
0 (pan
+ 1), oldmode
2637 Glut.postRedisplay
();
2645 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2647 state.mode <- Outline
(
2648 allowdel
, active, first, outlines, qsearch, pan
- 1, oldmode
2650 Glut.postRedisplay
();
2654 then updownlevel ~
-1
2658 state.mode <- Outline
(
2659 allowdel
, 0, 0, outlines, qsearch, pan
, oldmode
2661 Glut.postRedisplay
()
2664 let active = Array.length
outlines - 1 in
2665 let first = max
0 (active - maxrows) in
2666 state.mode <- Outline
(
2667 allowdel
, active, first, outlines, qsearch, pan
, oldmode
2669 Glut.postRedisplay
()
2674 let drawplaceholder l
=
2675 let margin = state.x
+ (conf.winw
- (state.w + state.scrollw
)) / 2 in
2677 (float l
.pagex
, float l
.pagedispy
)
2678 (float (l
.pagew
+ l
.pagex
), float (l
.pagedispy
+ l
.pagevh))
2680 let x = if margin < 0 then -margin else l
.pagex
2681 and y = l
.pagedispy
+ 13 in
2682 GlDraw.color
(0.0, 0.0, 0.0);
2683 drawstring 13 x y ("Loading " ^ string_of_int
(l
.pageno + 1))
2686 let now () = Unix.gettimeofday
();;
2690 match state.mode with
2691 | Textentry _
-> scalecolor 0.4
2692 | View
| Outline _
| Items _
-> scalecolor 1.0
2693 | Birdseye
(_
, _
, pageno, hooverpageno
, _
) ->
2694 if l
.pageno = hooverpageno
2697 if l
.pageno = pageno
2703 begin match getopaque l
.pageno with
2704 | Some
(opaque, _
) when validopaque opaque ->
2706 draw
(l
.pagedispy
, l
.pagew
, l
.pagevh, l
.pagey, conf.hlinks
)
2710 vlog "draw %d %f sec" l
.pageno d;
2718 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
2719 let sh = (float (maxy + conf.winh
) /. float conf.winh
) in
2720 let sh = float conf.winh
/. sh in
2721 let sh = max
sh (float conf.scrollh
) in
2724 if state.y = state.maxy
2726 else float y /. float maxy
2728 let position = (float conf.winh
-. sh) *. percent in
2731 if position +. sh > float conf.winh
2732 then float conf.winh
-. sh
2738 let scrollindicator () =
2739 GlDraw.color (0.64 , 0.64, 0.64);
2741 (float (conf.winw
- state.scrollw
), 0.)
2742 (float conf.winw
, float conf.winh
)
2744 GlDraw.color (0.0, 0.0, 0.0);
2746 let position, sh = scrollph state.y in
2748 (float (conf.winw
- state.scrollw
), position)
2749 (float conf.winw
, position +. sh)
2753 let showsel margin =
2754 match state.mstate
with
2755 | Mnone
| Mscroll _
| Mpan _
| Mzoom _
->
2758 | Msel
((x0
, y0
), (x1
, y1
)) ->
2759 let rec loop = function
2761 if (y0
>= l
.pagedispy
&& y0
<= (l
.pagedispy
+ l
.pagevh))
2762 || ((y1
>= l
.pagedispy
&& y1
<= (l
.pagedispy
+ l
.pagevh)))
2764 match getopaque l
.pageno with
2765 | Some
(opaque, _
) when validopaque opaque ->
2766 let oy = -l
.pagey + l
.pagedispy
in
2768 (x0
- margin - state.x, y0
,
2769 x1
- margin - state.x, y1
) oy;
2779 let panx = float state.x in
2781 GlDraw.color (0.0, 0.0, 1.0) ~alpha
:0.5;
2782 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2784 (fun (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) ->
2786 if l
.pageno = pageno
2788 let d = float (l
.pagedispy
- l
.pagey) in
2789 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
2790 GlDraw.begins `quads
;
2792 GlDraw.vertex2
(x0
+.panx, y0
+.d);
2793 GlDraw.vertex2
(x1
+.panx, y1
+.d);
2794 GlDraw.vertex2
(x2
+.panx, y2
+.d);
2795 GlDraw.vertex2
(x3
+.panx, y3
+.d);
2805 let showstrings active first pan
strings =
2807 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2808 GlDraw.color (0., 0., 0.) ~alpha
:0.85;
2809 GlDraw.rect (0., 0.) (float conf.winw
, float conf.winh
);
2812 GlDraw.color (1., 1., 1.);
2814 if row
= Array.length
strings || (row
- first) * 16 > conf.winh
2817 let (s, level
, _
) = strings.(row
) in
2818 let y = (row
- first) * 16 in
2819 let x = 5 + 15*(max
0 (level
+pan
)) in
2823 GlDraw.polygon_mode `both `
line;
2824 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2825 GlDraw.color (1., 1., 1.) ~alpha
:0.9;
2826 GlDraw.rect (0., float (y + 1))
2827 (float (conf.winw
- 1), float (y + 18));
2828 GlDraw.polygon_mode `both `fill
;
2830 GlDraw.color (1., 1., 1.);
2833 let l = String.length
s in
2836 let pan = pan * 2 in
2837 let pos = pan + level
in
2838 let left = l + pos in
2844 else String.sub
s (-pos) left
2846 drawstring 14 x (y + 16) s
2849 drawstring 14 (x + pan*15) (y + 16) s
2858 let showoutline (_
, active, first, outlines, _
, pan, _
) =
2859 showstrings active first pan outlines;
2862 let showitems (active, first, items, _
, pan, _
) =
2863 showstrings active first pan items;
2867 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
2868 GlDraw.viewport
margin 0 state.w conf.winh
;
2870 GlClear.color (scalecolor2 conf.bgcolor
);
2871 GlClear.clear
[`
color];
2874 Gl.enable `scissor_test
;
2875 GlMisc.scissor
0 0 (conf.winw
- state.scrollw
) conf.winh
;
2877 List.iter
drawpage state.layout;
2880 Gl.disable `scissor_test
2884 let x = -.float state.x in
2885 GlMat.translate ~
x ();
2889 GlDraw.viewport
0 0 conf.winw
conf.winh
;
2892 begin match state.mode with
2893 | Items
items -> showitems items
2894 | Outline
outline -> showoutline outline
2898 Glut.swapBuffers
();
2902 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
2903 let x = x - margin - state.x in
2904 let rec f = function
2906 begin match getopaque l.pageno with
2907 | Some
(opaque, _
) when validopaque opaque ->
2908 let y = y - l.pagedispy
in
2911 let y = l.pagey + y in
2912 let x = x - l.pagex
in
2913 match whatsunder
opaque x y with
2926 let viewmouse button bstate
x y =
2928 | Glut.OTHER_BUTTON
n when (n == 3 || n == 4) && bstate
= Glut.UP
->
2929 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2931 match state.mstate
with
2932 | Mzoom
(oldn
, i) ->
2940 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
2942 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
2944 let zoom = conf.zoom +. incr in
2946 state.mstate
<- Mzoom
(n, 0);
2948 state.mstate
<- Mzoom
(n, i+1);
2950 else state.mstate
<- Mzoom
(n, 0)
2952 | _
-> state.mstate
<- Mzoom
(n, 0)
2955 if state.ascrollstep
> 0
2957 setautoscrollspeed (n=4)
2961 then -conf.scrollstep
2962 else conf.scrollstep
2964 let incr = incr * 2 in
2965 let y = clamp incr in
2966 gotoy_and_clear_text y
2969 | Glut.LEFT_BUTTON
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
2970 if bstate
= Glut.DOWN
2972 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
2973 state.mstate
<- Mpan
(x, y)
2976 state.mstate
<- Mnone
2978 | Glut.LEFT_BUTTON
when x > conf.winw
- state.scrollw
->
2979 if bstate
= Glut.DOWN
2981 let position, sh = scrollph state.y in
2982 if y > truncate
position && y < truncate
(position +. sh)
2984 state.mstate
<- Mscroll
2986 let percent = float y /. float conf.winh
in
2987 let desty = truncate
(float (state.maxy - conf.winh
) *. percent) in
2989 state.mstate
<- Mscroll
2991 state.mstate
<- Mnone
2993 | Glut.LEFT_BUTTON
->
2994 let dest = if bstate
= Glut.DOWN
then getunder x y else Unone
in
2995 begin match dest with
2996 | Ulinkgoto
(pageno, top) ->
3000 gotopage1 pageno top;
3006 | Unone
when bstate
= Glut.DOWN
->
3007 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
3008 state.mstate
<- Mpan
(x, y);
3010 | Unone
| Utext _
->
3011 if bstate
= Glut.DOWN
3013 if conf.angle
mod 360 = 0
3015 state.mstate
<- Msel
((x, y), (x, y));
3016 Glut.postRedisplay
()
3020 match state.mstate
with
3023 | Mzoom _
| Mscroll
->
3024 state.mstate
<- Mnone
3027 Glut.setCursor
Glut.CURSOR_INHERIT
;
3028 state.mstate
<- Mnone
3030 | Msel
((_
, y0
), (_
, y1
)) ->
3032 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh))
3033 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh)))
3035 match getopaque l.pageno with
3036 | Some
(opaque, _
) when validopaque opaque ->
3040 List.iter
f state.layout;
3041 copysel
""; (* ugly *)
3042 Glut.setCursor
Glut.CURSOR_INHERIT
;
3043 state.mstate
<- Mnone
;
3050 let birdseyemouse button bstate
x y
3051 (conf, leftx
, _
, hooverpageno
, anchor) =
3053 | Glut.LEFT_BUTTON
when bstate
= Glut.UP
->
3054 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
3055 let rec loop = function
3058 if y > l.pagedispy
&& y < l.pagedispy
+ l.pagevh
3059 && x > margin && x < margin + l.pagew
3061 leavebirdseye (conf, leftx
, l.pageno, hooverpageno
, anchor) false;
3066 | Glut.OTHER_BUTTON _
-> viewmouse button bstate
x y
3070 let mouse bstate button
x y =
3071 match state.mode with
3072 | View
-> viewmouse button bstate
x y
3073 | Birdseye beye
-> birdseyemouse button bstate
x y beye
3074 | Textentry _
| Outline _
| Items _
-> ()
3077 let mouse ~button ~
state ~
x ~
y = mouse state button
x y;;
3080 match state.mode with
3083 match state.mstate
with
3084 | Mzoom _
| Mnone
-> ()
3089 state.mstate
<- Mpan
(x, y);
3090 if conf.zoom > 1.0 then state.x <- state.x + dx;
3092 gotoy_and_clear_text y
3095 state.mstate
<- Msel
(a, (x, y));
3096 Glut.postRedisplay
()
3099 let y = min
conf.winh
(max
0 y) in
3100 let percent = float y /. float conf.winh
in
3101 let y = truncate
(float (state.maxy - conf.winh
) *. percent) in
3102 gotoy_and_clear_text y
3106 match state.mode with
3107 | Birdseye
(conf, leftx
, pageno, hooverpageno
, anchor) ->
3108 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
3109 let rec loop = function
3111 if hooverpageno
!= -1
3113 state.mode <- Birdseye
(conf, leftx
, pageno, -1, anchor);
3114 Glut.postRedisplay
();
3117 if y > l.pagedispy
&& y < l.pagedispy
+ l.pagevh
3118 && x > margin && x < margin + l.pagew
3120 state.mode <- Birdseye
(conf, leftx
, pageno, l.pageno, anchor);
3121 Glut.postRedisplay
();
3127 | Outline _
| Items _
| Textentry _
-> ()
3129 match state.mstate
with
3131 begin match getunder x y with
3132 | Unone
-> Glut.setCursor
Glut.CURSOR_INHERIT
3134 if conf.underinfo
then showtext 'u'
("ri: " ^ uri
);
3135 Glut.setCursor
Glut.CURSOR_INFO
3136 | Ulinkgoto
(page
, _
) ->
3138 then showtext 'p'
("age: " ^ string_of_int page
);
3139 Glut.setCursor
Glut.CURSOR_INFO
3141 if conf.underinfo
then showtext '
f'
("ont: " ^
s);
3142 Glut.setCursor
Glut.CURSOR_TEXT
3145 | Mpan _
| Msel _
| Mzoom _
| Mscroll
->
3156 match Sys.os_type
with
3157 | "Win32" -> Sys.getenv
"HOMEPATH"
3158 | _
-> Sys.getenv
"HOME"
3161 ("Can not determine home directory location: " ^
3162 Printexc.to_string exn
);
3166 let config_of c attrs
=
3170 | "scroll-bar-width" -> { c with scrollbw
= max
0 (int_of_string
v) }
3171 | "scroll-handle-height" -> { c with scrollh
= max
0 (int_of_string
v) }
3172 | "case-insensitive-search" -> { c with icase
= bool_of_string
v }
3173 | "preload" -> { c with preload = bool_of_string
v }
3174 | "page-bias" -> { c with pagebias
= int_of_string
v }
3175 | "scroll-step" -> { c with scrollstep
= max
1 (int_of_string
v) }
3176 | "auto-scroll-step" ->
3177 { c with autoscrollstep
= max
0 (int_of_string
v) }
3178 | "max-height-fit" -> { c with maxhfit
= bool_of_string
v }
3179 | "crop-hack" -> { c with crophack
= bool_of_string
v }
3180 | "throttle" -> { c with showall
= bool_of_string
v }
3181 | "highlight-links" -> { c with hlinks
= bool_of_string
v }
3182 | "under-cursor-info" -> { c with underinfo
= bool_of_string
v }
3183 | "vertical-margin" ->
3184 { c with interpagespace = max
0 (int_of_string
v) }
3186 let zoom = float_of_string
v /. 100. in
3187 let zoom = max
0.01 (min
2.2 zoom) in
3188 { c with zoom = zoom }
3189 | "presentation" -> { c with presentation = bool_of_string
v }
3190 | "rotation-angle" -> { c with angle
= int_of_string
v }
3191 | "width" -> { c with winw
= max
20 (int_of_string
v) }
3192 | "height" -> { c with winh
= max
20 (int_of_string
v) }
3193 | "persistent-bookmarks" -> { c with savebmarks
= bool_of_string
v }
3194 | "proportional-display" -> { c with proportional
= bool_of_string
v }
3195 | "pixmap-cache-size" -> { c with memlimit
= max
2 (int_of_string
v) }
3196 | "tex-count" -> { c with texcount
= max
1 (int_of_string
v) }
3197 | "slice-height" -> { c with sliceheight
= max
2 (int_of_string
v) }
3198 | "thumbnail-width" -> { c with thumbw
= max
2 (int_of_string
v) }
3199 | "persistent-location" -> { c with jumpback
= bool_of_string
v }
3200 | "background-color" -> { c with bgcolor
= color_of_string v }
3201 | "scrollbar-in-presentation" ->
3202 { c with scrollbarinpm
= bool_of_string
v }
3203 | "ui-font" -> { c with uifont
= v }
3206 prerr_endline
("Error processing attribute (`" ^
3207 k ^
"'=`" ^
v ^
"'): " ^
Printexc.to_string exn
);
3210 let rec fold c = function
3213 let c = apply c k
v in
3219 let fromstring f pos n v d =
3222 dolog "Error processing attribute (%S=%S) at %d\n%s"
3223 n v pos (Printexc.to_string exn
)
3228 let bookmark_of attrs
=
3229 let rec fold title page rely
= function
3230 | ("title", v) :: rest
-> fold v page rely rest
3231 | ("page", v) :: rest
-> fold title v rely rest
3232 | ("rely", v) :: rest
-> fold title page
v rest
3233 | _
:: rest
-> fold title page rely rest
3234 | [] -> title, page
, rely
3236 fold "invalid" "0" "0" attrs
3240 let rec fold path page rely
pan = function
3241 | ("path", v) :: rest
-> fold v page rely
pan rest
3242 | ("page", v) :: rest
-> fold path
v rely
pan rest
3243 | ("rely", v) :: rest
-> fold path page
v pan rest
3244 | ("pan", v) :: rest
-> fold path page rely
v rest
3245 | _
:: rest
-> fold path page rely
pan rest
3246 | [] -> path
, page
, rely
, pan
3248 fold "" "0" "0" "0" attrs
3251 let setconf dst src
=
3252 dst
.scrollbw
<- src
.scrollbw
;
3253 dst
.scrollh
<- src
.scrollh
;
3254 dst
.icase
<- src
.icase
;
3255 dst
.preload <- src
.preload;
3256 dst
.pagebias
<- src
.pagebias
;
3257 dst
.verbose
<- src
.verbose
;
3258 dst
.scrollstep
<- src
.scrollstep
;
3259 dst
.maxhfit
<- src
.maxhfit
;
3260 dst
.crophack
<- src
.crophack
;
3261 dst
.autoscrollstep
<- src
.autoscrollstep
;
3262 dst
.showall
<- src
.showall
;
3263 dst
.hlinks
<- src
.hlinks
;
3264 dst
.underinfo
<- src
.underinfo
;
3265 dst
.interpagespace <- src
.interpagespace;
3266 dst
.zoom <- src
.zoom;
3267 dst
.presentation <- src
.presentation;
3268 dst
.angle
<- src
.angle
;
3269 dst
.winw
<- src
.winw
;
3270 dst
.winh
<- src
.winh
;
3271 dst
.savebmarks
<- src
.savebmarks
;
3272 dst
.memlimit
<- src
.memlimit
;
3273 dst
.proportional
<- src
.proportional
;
3274 dst
.texcount
<- src
.texcount
;
3275 dst
.sliceheight
<- src
.sliceheight
;
3276 dst
.thumbw
<- src
.thumbw
;
3277 dst
.jumpback
<- src
.jumpback
;
3278 dst
.bgcolor
<- src
.bgcolor
;
3279 dst
.scrollbarinpm
<- src
.scrollbarinpm
;
3280 dst
.uifont
<- src
.uifont
;
3284 let l = String.length
s in
3285 let b = Buffer.create
l in
3291 let h = Hashtbl.create
10 in
3292 let dc = { defconf with angle
= defconf.angle
} in
3293 let rec toplevel v t spos _
=
3295 | Vdata
| Vcdata
| Vend
-> v
3296 | Vopen
("llppconfig", _
, closed
) ->
3299 else { v with f = llppconfig
}
3301 error
"unexpected subelement at top level" s spos
3302 | Vclose _
-> error
"unexpected close at top level" s spos
3304 and llppconfig
v t spos _
=
3306 | Vdata
| Vcdata
| Vend
-> v
3307 | Vopen
("defaults", attrs
, closed
) ->
3308 let c = config_of dc attrs
in
3312 else { v with f = skip
"defaults" (fun () -> v) }
3314 | Vopen
("doc", attrs
, closed
) ->
3315 let pathent, spage
, srely
, span
= doc_of attrs
in
3316 let path = unent pathent
3317 and pageno = fromstring int_of_string spos
"page" spage
0
3318 and rely
= fromstring float_of_string spos
"rely" srely
0.0
3319 and pan = fromstring int_of_string spos
"pan" span
0 in
3320 let c = config_of dc attrs
in
3321 let anchor = (pageno, rely
) in
3323 then (Hashtbl.add
h path (c, [], pan, anchor); v)
3324 else { v with f = doc
path pan anchor c [] }
3327 error
"unexpected subelement in llppconfig" s spos
3329 | Vclose
"llppconfig" -> { v with f = toplevel }
3330 | Vclose _
-> error
"unexpected close in llppconfig" s spos
3332 and doc
path pan anchor c bookmarks v t spos _
=
3334 | Vdata
| Vcdata
-> v
3335 | Vend
-> error
"unexpected end of input in doc" s spos
3336 | Vopen
("bookmarks", _
, closed
) ->
3339 else { v with f = pbookmarks
path pan anchor c bookmarks }
3341 | Vopen
(_
, _
, _
) ->
3342 error
"unexpected subelement in doc" s spos
3345 Hashtbl.add
h path (c, List.rev
bookmarks, pan, anchor);
3346 { v with f = llppconfig
}
3348 | Vclose _
-> error
"unexpected close in doc" s spos
3350 and pbookmarks
path pan anchor c bookmarks v t spos _
=
3352 | Vdata
| Vcdata
-> v
3353 | Vend
-> error
"unexpected end of input in bookmarks" s spos
3354 | Vopen
("item", attrs
, closed
) ->
3355 let titleent, spage
, srely
= bookmark_of attrs
in
3356 let page = fromstring int_of_string spos
"page" spage
0
3357 and rely
= fromstring float_of_string spos
"rely" srely
0.0 in
3358 let bookmarks = (unent titleent, 0, (page, rely
)) :: bookmarks in
3360 then { v with f = pbookmarks
path pan anchor c bookmarks }
3363 { v with f = skip
"item" f }
3366 error
"unexpected subelement in bookmarks" s spos
3368 | Vclose
"bookmarks" ->
3369 { v with f = doc
path pan anchor c bookmarks }
3371 | Vclose _
-> error
"unexpected close in bookmarks" s spos
3373 and skip tag
f v t spos _
=
3375 | Vdata
| Vcdata
-> v
3377 error
("unexpected end of input in skipped " ^ tag
) s spos
3378 | Vopen
(tag'
, _
, closed
) ->
3382 let f'
() = { v with f = skip tag
f } in
3383 { v with f = skip tag'
f'
}
3387 else error
("unexpected close in skipped " ^ tag
) s spos
3390 parse
{ f = toplevel; accu = () } s;
3396 let len = in_channel_length ic
in
3397 let s = String.create
len in
3398 really_input ic
s 0 len;
3401 | Parse_error
(msg
, s, pos) ->
3402 let subs = subs s pos in
3403 let s = Printf.sprintf
"%s: at %d [..%s..]" msg
pos subs in
3404 failwith
("parse error: " ^
s)
3407 failwith
("config load error: " ^
Printexc.to_string exn
)
3413 let dir = Filename.concat
home ".config" in
3414 if Sys.is_directory
dir then dir else home
3417 Filename.concat
dir "llpp.conf"
3421 if Sys.file_exists
path
3424 (try Some
(open_in_bin
path)
3427 ("Error opening configuation file `" ^
path ^
"': " ^
3428 Printexc.to_string exn
);
3437 ("Error loading configuation from `" ^
path ^
"': " ^
3438 Printexc.to_string exn
);
3444 f (Hashtbl.create
0, defconf)
3449 let pc, pb
, px
, pa
=
3451 Hashtbl.find h (Filename.basename
state.path)
3452 with Not_found
-> dc, [], 0, (0, 0.0)
3456 state.bookmarks <- pb
;
3458 state.scrollw
<- conf.scrollbw
;
3460 then state.anchor <- pa
;
3461 cbput state.hists
.nav pa
;
3466 let add_attrs bb always
dc c =
3469 then Printf.bprintf bb
"\n %s='%b'" s a
3472 then Printf.bprintf bb
"\n %s='%d'" s a
3475 then Printf.bprintf bb
"\n %s='%f'" s (a*.100.)
3479 Printf.bprintf bb
"\n %s='%s'" s (color_to_string a)
3483 Printf.bprintf bb
"\n %s='%s'" s a
3487 then dc.winw
, dc.winh
3489 match state.fullscreen
with
3491 | None
-> c.winw
, c.winh
3493 let zoom, presentation, interpagespace, showall
=
3495 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
3497 match state.mode with
3498 | Birdseye
(bc
, _
, _
, _
, _
) ->
3499 bc
.zoom, bc
.presentation, bc
.interpagespace, bc
.showall
3500 | _
-> c.zoom, c.presentation, c.interpagespace, c.showall
3502 oi
"width" w dc.winw
;
3503 oi
"height" h dc.winh
;
3504 oi
"scroll-bar-width" c.scrollbw
dc.scrollbw
;
3505 oi
"scroll-handle-height" c.scrollh
dc.scrollh
;
3506 ob "case-insensitive-search" c.icase
dc.icase
;
3507 ob "preload" c.preload dc.preload;
3508 oi
"page-bias" c.pagebias
dc.pagebias
;
3509 oi
"scroll-step" c.scrollstep
dc.scrollstep
;
3510 oi
"auto-scroll-step" c.autoscrollstep
dc.autoscrollstep
;
3511 ob "max-height-fit" c.maxhfit
dc.maxhfit
;
3512 ob "crop-hack" c.crophack
dc.crophack
;
3513 ob "throttle" showall
dc.showall
;
3514 ob "highlight-links" c.hlinks
dc.hlinks
;
3515 ob "under-cursor-info" c.underinfo
dc.underinfo
;
3516 oi
"vertical-margin" interpagespace dc.interpagespace;
3517 oz
"zoom" zoom dc.zoom;
3518 ob "presentation" presentation dc.presentation;
3519 oi
"rotation-angle" c.angle
dc.angle
;
3520 ob "persistent-bookmarks" c.savebmarks
dc.savebmarks
;
3521 ob "proportional-display" c.proportional
dc.proportional
;
3522 oi
"pixmap-cache-size" c.memlimit
dc.memlimit
;
3523 oi
"texcount" c.texcount
dc.texcount
;
3524 oi
"slice-height" c.sliceheight
dc.sliceheight
;
3525 oi
"thumbnail-width" c.thumbw
dc.thumbw
;
3526 ob "persistent-location" c.jumpback
dc.jumpback
;
3527 oc
"background-color" c.bgcolor
dc.bgcolor
;
3528 ob "scrollbar-in-presentation" c.scrollbarinpm
dc.scrollbarinpm
;
3529 os
"ui-font" c.uifont
dc.uifont
;
3533 let bb = Buffer.create
32768 in
3535 let dc = if conf.bedefault
then conf else dc in
3536 Buffer.add_string
bb "<llppconfig>\n<defaults ";
3537 add_attrs bb true dc dc;
3538 Buffer.add_string
bb "/>\n";
3540 let adddoc path pan anchor c bookmarks =
3541 if bookmarks == [] && c = dc && anchor = emptyanchor
3544 Printf.bprintf
bb "<doc path='%s'"
3545 (enent
path 0 (String.length
path));
3547 if anchor <> emptyanchor
3549 let n, y = anchor in
3550 Printf.bprintf
bb " page='%d'" n;
3553 Printf.bprintf
bb " rely='%f'" y
3558 then Printf.bprintf
bb " pan='%d'" pan;
3560 add_attrs bb false dc c;
3562 begin match bookmarks with
3563 | [] -> Buffer.add_string
bb "/>\n"
3565 Buffer.add_string
bb ">\n<bookmarks>\n";
3566 List.iter
(fun (title, _level
, (page, rely
)) ->
3568 "<item title='%s' page='%d'"
3569 (enent
title 0 (String.length
title))
3574 Printf.bprintf
bb " rely='%f'" rely
3576 Buffer.add_string
bb "/>\n";
3578 Buffer.add_string
bb "</bookmarks>\n</doc>\n";
3584 match state.mode with
3585 | Birdseye
(_
, pan, _
, _
, _
) -> pan
3588 let basename = Filename.basename state.path in
3589 adddoc basename pan (getanchor ())
3592 if state.ascrollstep
> 0
3593 then state.ascrollstep
3594 else conf.autoscrollstep
}
3595 (if conf.savebmarks
then state.bookmarks else []);
3597 Hashtbl.iter
(fun path (c, bookmarks, x, y) ->
3599 then adddoc path x y c bookmarks
3601 Buffer.add_string
bb "</llppconfig>";
3604 if Buffer.length
bb > 0
3607 let tmp = path ^
".tmp" in
3608 let oc = open_out_bin
tmp in
3609 Buffer.output_buffer
oc bb;
3611 Sys.rename
tmp path;
3614 ("error while saving configuration: " ^
Printexc.to_string exn
)
3621 [("-p", Arg.String
(fun s -> state.password
<- s) , " Set password")
3622 ;("-v", Arg.Unit
(fun () -> print_endline
Help.version
; exit
0),
3623 " Print version and exit")]
3625 (fun s -> state.path <- s)
3626 ("Usage: " ^
Sys.argv
.(0) ^
" [options] some.pdf\nOptions:")
3628 if String.length
state.path = 0
3629 then (prerr_endline
"file name missing"; exit
1);
3633 let _ = Glut.init
Sys.argv
in
3634 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
3635 let () = Glut.initWindowSize
conf.winw
conf.winh
in
3636 let _ = Glut.createWindow
("llpp " ^
Filename.basename state.path) in
3639 if Sys.os_type
= "Unix"
3641 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
3643 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
3644 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
3645 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
3646 Unix.bind
sock addr;
3648 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
3649 Unix.connect
csock addr;
3650 let ssock, _ = Unix.accept
sock in
3653 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
3654 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
3658 at_exit
(fun () -> Unix.shutdown
ssock Unix.SHUTDOWN_ALL
);
3662 let () = Glut.displayFunc
display in
3663 let () = Glut.reshapeFunc
reshape in
3664 let () = Glut.keyboardFunc
keyboard in
3665 let () = Glut.specialFunc
special in
3666 let () = Glut.idleFunc
(Some
idle) in
3667 let () = Glut.mouseFunc
mouse in
3668 let () = Glut.motionFunc
motion in
3669 let () = Glut.passiveMotionFunc
pmotion in
3672 conf.angle
, conf.proportional
, conf.texcount
,
3673 conf.sliceheight
, conf.uifont
3675 state.csock <- csock;
3676 state.ssock <- ssock;
3677 state.text <- "Opening " ^
state.path;
3678 writeopen state.path state.password
;
3682 let rec handlelablglutbug () =
3685 with Glut.BadEnum
"key in special_of_int" ->
3686 showtext '
!'
" LablGlut bug: special key not recognized";
3687 handlelablglutbug ()
3689 handlelablglutbug ();