4 | Ulinkgoto
of (int * int)
6 and facename
= string;;
8 let dolog fmt
= Printf.kprintf prerr_endline fmt
;;
10 type params
= angle
* proportional
* texcount
* sliceheight
* fontpath
19 and proportional
= bool
20 and interpagespace
= int
28 external init
: Unix.file_descr
-> params
-> unit = "ml_init";;
29 external draw
: (int * int * int * int * bool) -> string -> unit = "ml_draw";;
30 external seltext
: string -> (int * int * int * int) -> int -> unit =
32 external copysel
: string -> unit = "ml_copysel";;
33 external getpdimrect
: int -> float array
= "ml_getpdimrect";;
34 external whatsunder
: string -> int -> int -> under
= "ml_whatsunder";;
35 external zoomforh
: int -> int -> int -> float = "ml_zoom_for_height";;
36 external drawstr
: int -> int -> int -> string -> 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];
765 let drawstring size x
y s =
767 Gl.enable `texture_2d
;
770 Gl.disable `texture_2d
;
773 let drawstring1 size x
y s =
779 GlDraw.color
(0.0, 0.0, 0.0);
781 (0.0, float (conf.winh
- 18))
782 (float (conf.winw
- state.scrollw
- 1), float conf.winh
)
784 GlDraw.color
(1.0, 1.0, 1.0);
785 drawstring 14 8 (conf.winh
- 5) s;
787 let len = String.length
state.text
in
788 match state.mode
with
789 | Textentry
((prefix
, text
, _
, _
, _
), _
) ->
791 match String.length prefix
with
795 Printf.sprintf
"%s%s\226–� [%s]" prefix text
state.text
797 Printf.sprintf
"%s%s\226–�" prefix text
802 Printf.sprintf
"%s: %s\226–� [%s]" prefix text
state.text
804 Printf.sprintf
"%s: %s\226–�" prefix text
809 if len > 0 then drawstring state.text
813 state.text
<- Printf.sprintf
"%c%s" c s;
814 Glut.postRedisplay
();
823 state.rects
<- state.rects1
;
824 Glut.postRedisplay
()
827 let n = Scanf.sscanf
cmd "C %u" (fun n -> n) in
828 state.pagecount
<- n;
829 state.invalidated
<- state.invalidated
- 1;
830 if state.invalidated
= 0
834 let s = Scanf.sscanf
cmd "t %n"
835 (fun n -> String.sub
cmd n (String.length
cmd - n))
837 Glut.setWindowTitle
s
840 let s = Scanf.sscanf
cmd "T %n"
841 (fun n -> String.sub
cmd n (String.length
cmd - n))
843 if istextentry state.mode
850 Glut.postRedisplay
();
856 let s = Scanf.sscanf
cmd "V %n"
857 (fun n -> String.sub
cmd n (String.length
cmd - n))
863 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
864 Scanf.sscanf
cmd "F %u %d %f %f %f %f %f %f %f %f"
865 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
866 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
868 let y = (getpagey pageno) + truncate y0
in
871 state.rects1
<- [pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)]
874 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
875 Scanf.sscanf
cmd "R %u %d %f %f %f %f %f %f %f %f"
876 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
877 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
880 (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) :: state.rects1
883 let n, w, _h
, r
, l
, s, p
=
884 Scanf.sscanf
cmd "r %u %u %u %d %d %u %s"
885 (fun n w h r l
s p
->
886 (n-1, w, h, r
, l
!= 0, s, p
))
889 Hashtbl.replace
state.pagemap
(n, w, r
, l
, state.gen
) (p
, s);
890 state.memused
<- state.memused
+ s;
893 match state.throttle
with
894 | None
-> state.layout
895 | Some
layout -> layout
899 if (state.memused
<= conf.memlimit
) || cbempty state.pagecache
902 let evictedopaque = cbpeek state.pagecache
in
903 match findpageforopaque evictedopaque with
904 | None
-> failwith
"bug in gc"
905 | Some
((evictedn
, _
, _
, _
, gen
) as k
, evictedsize
) ->
906 if state.gen
!= gen
|| not
(pagevisible layout evictedn
)
908 wcmd "free" [`
s evictedopaque];
909 state.memused
<- state.memused
- evictedsize
;
910 Hashtbl.remove
state.pagemap k
;
911 cbdecr state.pagecache
;
918 cbput state.pagecache p
;
919 state.rendering
<- false;
921 begin match state.throttle
with
923 if pagevisible state.layout n
926 let allvisible = loadlayout state.layout in
927 if allvisible then preload ();
934 let y = getpagey l
.pageno + l
.pagey in
940 Scanf.sscanf
cmd "l %u %u %u %u" (fun n w h x
-> n, w, h, x
)
942 state.pdims
<- pdim :: state.pdims
945 let (l
, n, t
, h, pos
) =
946 Scanf.sscanf
cmd "o %u %u %d %u %n" (fun l
n t
h pos
-> l
, n, t
, h, pos
)
948 let s = String.sub
cmd pos
(String.length
cmd - pos
) in
949 let outline = (s, l
, (n, float t
/. float h)) in
951 match state.outlines with
952 | Olist
outlines -> Olist
(outline :: outlines)
953 | Oarray _
-> Olist
[outline]
954 | Onarrow _
-> Olist
[outline]
956 state.outlines <- outlines
960 let s = Scanf.sscanf
cmd "i %n"
961 (fun n -> String.sub
cmd n (String.length
cmd - n))
963 let len = String.length
s in
964 let rec fold accu pos
=
966 try String.index_from
s pos '
\n'
with Not_found
-> len
971 let line = String.sub
s pos
(eolpos - pos
) in
972 fold ((1, line)::accu) (eolpos+1)
974 state.docinfo
<- fold state.docinfo
0
977 dolog "unknown cmd `%S'" cmd
980 let now = Unix.gettimeofday
;;
984 let r, _
, _
= Unix.select
[state.csock
] [] [] delay
in
987 if state.ascrollstep
> 0
989 let y = state.y + state.ascrollstep
in
990 let y = if y >= state.maxy then 0 else y in
996 let cmd = readcmd state.csock
in
1005 let action = function
1006 | HCprev
-> cbget cb ~
-1
1007 | HCnext
-> cbget cb
1
1008 | HCfirst
-> cbget cb ~
-(cb
.rc)
1009 | HClast
-> cbget cb
(cb
.len - 1 - cb
.rc)
1010 and cancel
() = cb
.rc <- rc
1014 let search pattern forward
=
1015 if String.length pattern
> 0
1018 match state.layout with
1021 l
.pageno, (l
.pagey + if forward
then 0 else 0*l
.pagevh)
1024 let b = makecmd "search"
1025 [`
b conf.icase
; `i
pn; `i
py; `i
(if forward
then 1 else 0)]
1027 Buffer.add_char
b '
,'
;
1028 Buffer.add_string
b pattern
;
1029 Buffer.add_char
b '
\000'
;
1032 writecmd state.csock
cmd;
1035 let intentry text key
=
1036 let c = Char.unsafe_chr key
in
1039 let s = "x" in s.[0] <- c;
1040 let text = text ^
s in
1044 state.text <- Printf.sprintf
"invalid char (%d, `%c')" key
c;
1049 let b = Buffer.create
(String.length
s + 1) in
1050 Buffer.add_string
b s;
1051 Buffer.add_char
b c;
1055 let textentry text key
=
1056 let c = Char.unsafe_chr key
in
1058 | _
when key
>= 32 && key
< 127 ->
1059 let text = addchar text c in
1063 dolog "unhandled key %d char `%c'" key
(Char.unsafe_chr key
);
1067 let reinit angle proportional
=
1068 conf.angle
<- angle
;
1069 conf.proportional
<- proportional
;
1071 wcmd "reinit" [`i angle
; `
b proportional
];
1075 let zoom = max
0.01 (min
2.2 zoom) in
1076 if zoom <> conf.zoom
1081 if state.invalidated
= 0
1083 state.anchor <- getanchor ()
1085 reshape conf.winw
conf.winh
;
1086 state.text <- Printf.sprintf
"zoom is now %-5.1f" (zoom *. 100.0);
1090 let enterbirdseye () =
1091 let zoom = float conf.thumbw
/. float conf.winw
in
1092 let birdseyepageno =
1093 let cy = conf.winh
/ 2 in
1097 let rec fold best
= function
1100 let d = cy - (l
.pagedispy
+ l
.pagevh/2)
1101 and dbest
= cy - (best
.pagedispy
+ best
.pagevh/2) in
1102 if abs
d < abs dbest
1109 state.mode
<- Birdseye
(
1110 { conf with zoom = conf.zoom }, state.x
, birdseyepageno, -1, getanchor ()
1113 conf.presentation <- false;
1114 conf.interpagespace <- 10;
1115 conf.hlinks
<- false;
1117 state.mstate
<- Mnone
;
1118 conf.showall
<- false;
1119 Glut.setCursor
Glut.CURSOR_INHERIT
;
1122 state.text <- Printf.sprintf
"birds eye mode on (zoom %3.1f%%)"
1127 reshape conf.winw
conf.winh
;
1130 let leavebirdseye (c, leftx
, pageno, _
, anchor) goback
=
1132 conf.zoom <- c.zoom;
1133 conf.presentation <- c.presentation;
1134 conf.interpagespace <- c.interpagespace;
1135 conf.showall
<- c.showall
;
1136 conf.hlinks
<- c.hlinks
;
1140 state.text <- Printf.sprintf
"birds eye mode off (zoom %3.1f%%)"
1143 reshape conf.winw
conf.winh
;
1144 state.anchor <- if goback
then anchor else (pageno, 0.0);
1147 let togglebirdseye () =
1148 match state.mode
with
1149 | Birdseye vals
-> leavebirdseye vals
true
1150 | View
| Outline _
-> enterbirdseye ()
1154 let upbirdseye (conf, leftx
, pageno, hooverpageno
, anchor) =
1155 let pageno = max
0 (pageno - 1) in
1156 let rec loop = function
1157 | [] -> gotopage1 pageno 0
1158 | l
:: _
when l
.pageno = pageno ->
1159 if l
.pagedispy
>= 0 && l
.pagey = 0
1160 then Glut.postRedisplay
()
1161 else gotopage1 pageno 0
1162 | _
:: rest
-> loop rest
1165 state.mode
<- Birdseye
(conf, leftx
, pageno, hooverpageno
, anchor)
1168 let downbirdseye (conf, leftx
, pageno, hooverpageno
, anchor) =
1169 let pageno = min
(state.pagecount
- 1) (pageno + 1) in
1170 state.mode
<- Birdseye
(conf, leftx
, pageno, hooverpageno
, anchor);
1171 let rec loop = function
1173 let y, h = getpageyh pageno in
1174 let dy = (y - state.y) - (conf.winh
- h - conf.interpagespace) in
1176 | l
:: _
when l
.pageno = pageno ->
1177 if l
.pagevh != l
.pageh
1178 then gotoy (clamp (l
.pageh
- l
.pagevh + conf.interpagespace))
1179 else Glut.postRedisplay
()
1180 | _
:: rest
-> loop rest
1185 let optentry mode _ key
=
1186 let btos b = if b then "on" else "off" in
1187 let c = Char.unsafe_chr key
in
1191 try conf.scrollstep
<- int_of_string
s with exc
->
1192 state.text <- Printf.sprintf
"bad integer `%s': %s"
1193 s (Printexc.to_string exc
)
1195 TEswitch
("scroll step", "", None
, intentry, ondone)
1200 conf.autoscrollstep
<- int_of_string
s;
1201 if state.ascrollstep
> 0
1202 then state.ascrollstep
<- conf.autoscrollstep
;
1204 state.text <- Printf.sprintf
"bad integer `%s': %s"
1205 s (Printexc.to_string exc
)
1207 TEswitch
("auto scroll step", "", None
, intentry, ondone)
1212 let zoom = float (int_of_string
s) /. 100.0 in
1215 state.text <- Printf.sprintf
"bad integer `%s': %s"
1216 s (Printexc.to_string exc
)
1218 TEswitch
("zoom", "", None
, intentry, ondone)
1223 conf.thumbw
<- max
2 (min
1920 (int_of_string
s));
1225 Printf.sprintf
"thumbnail width is set to %d" conf.thumbw
;
1226 begin match mode
with
1228 leavebirdseye beye
false;
1233 state.text <- Printf.sprintf
"bad integer `%s': %s"
1234 s (Printexc.to_string exc
)
1236 TEswitch
("thumbnail width", "", None
, intentry, ondone)
1241 Some
(int_of_string
s)
1243 state.text <- Printf.sprintf
"bad integer `%s': %s"
1244 s (Printexc.to_string exc
);
1247 | Some angle
-> reinit angle
conf.proportional
1250 TEswitch
("rotation", "", None
, intentry, ondone)
1253 conf.icase
<- not
conf.icase
;
1254 TEdone
("case insensitive search " ^
(btos conf.icase
))
1257 conf.preload <- not
conf.preload;
1259 TEdone
("preload " ^
(btos conf.preload))
1262 conf.verbose
<- not
conf.verbose
;
1263 TEdone
("verbose " ^
(btos conf.verbose
))
1266 conf.maxhfit
<- not
conf.maxhfit
;
1267 state.maxy <- state.maxy + (if conf.maxhfit
then -conf.winh
else conf.winh
);
1268 TEdone
("maxhfit " ^
(btos conf.maxhfit
))
1271 conf.crophack
<- not
conf.crophack
;
1272 TEdone
("crophack " ^
btos conf.crophack
)
1275 conf.showall
<- not
conf.showall
;
1276 TEdone
("throttle " ^
btos conf.showall
)
1279 conf.underinfo
<- not
conf.underinfo
;
1280 TEdone
("underinfo " ^
btos conf.underinfo
)
1283 conf.savebmarks
<- not
conf.savebmarks
;
1284 TEdone
("persistent bookmarks " ^
btos conf.savebmarks
)
1290 match state.layout with
1295 conf.interpagespace <- int_of_string
s;
1296 state.maxy <- calcheight ();
1297 let y = getpagey pageno in
1300 state.text <- Printf.sprintf
"bad integer `%s': %s"
1301 s (Printexc.to_string exc
)
1303 TEswitch
("vertical margin", "", None
, intentry, ondone)
1306 reinit conf.angle
(not
conf.proportional
);
1307 TEdone
("proprortional display " ^
btos conf.proportional
)
1310 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
1314 let maxoutlinerows () = (conf.winh
- 31) / 16;;
1316 let enterselector allowdel
outlines errmsg msg
=
1317 if Array.length
outlines = 0
1319 showtext ' ' errmsg
;
1323 Glut.setCursor
Glut.CURSOR_INHERIT
;
1325 match state.layout with
1327 | {pageno=pageno} :: _
-> pageno
1331 if n = Array.length
outlines
1334 let (_
, _
, (outlinepageno
, _
)) = outlines.(n) in
1335 if outlinepageno
>= pageno then n else loop (n+1)
1339 state.mode
<- Outline
1340 (allowdel
, active, max
0 (active - maxoutlinerows () / 2), outlines, "", 0,
1342 Glut.postRedisplay
();
1346 let enteroutlinemode () =
1348 match state.outlines with
1351 let a = Array.of_list
(List.rev l
) in
1352 state.outlines <- Oarray
a;
1354 | Onarrow
(pat
, a, _
) ->
1355 a, "Outline was narrowed to `" ^ pat ^
"' (Ctrl-u to restore)"
1357 enterselector false outlines "Document has no outline" msg
;
1360 let enterbookmarkmode () =
1361 let bookmarks = Array.of_list
state.bookmarks in
1362 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1365 let mode_to_string mode
=
1366 let b = Buffer.create
10 in
1367 let rec f = function
1368 | Textentry
(_
, _
) -> Buffer.add_string
b "Textentry ";
1369 | View
-> Buffer.add_string
b "View"
1370 | Birdseye _
-> Buffer.add_string
b "Birdseye"
1371 | Items _
-> Buffer.add_string
b "Items"
1372 | Outline _
-> Buffer.add_string
b "Outline"
1378 let color_of_string s =
1379 Scanf.sscanf
s "%d/%d/%d" (fun r g
b ->
1380 (float r /. 256.0, float g
/. 256.0, float b /. 256.0)
1384 let color_to_string (r, g
, b) =
1385 let r = truncate
(r *. 256.0)
1386 and g
= truncate
(g
*. 256.0)
1387 and b = truncate
(b *. 256.0) in
1388 Printf.sprintf
"%d/%d/%d" r g
b
1391 let enterinfomode () =
1392 let btos = function true -> "on" | _
-> "off" in
1393 let mode = state.mode in
1394 let rec makeitems () =
1395 let intp name get set
=
1396 Printf.sprintf
"%-24s %d" name
(get
()), 1, Action
(
1397 fun active first _ pan
->
1402 state.text <- Printf.sprintf
"bad integer `%s': %s"
1403 s (Printexc.to_string exn
);
1406 if n != max_int
then set
n;
1408 let te = name
, "", None
, intentry, ondone in
1413 state.mode <- Items
(active, first
, makeitems (), "", pan
, mode)
1416 and boolp name get set
=
1417 Printf.sprintf
"%-24s %s" name
(btos (get
())), 1, Action
(
1418 fun active first qsearch pan
->
1421 Items
(active, first
, makeitems (), qsearch
, pan
, mode);
1423 and colorp name get set
=
1424 Printf.sprintf
"%-24s %s" name
(color_to_string (get
())), 1, Action
(
1425 fun active first _ pan
->
1426 let invalid = (nan
, nan
, nan
) in
1429 try color_of_string s
1431 state.text <- Printf.sprintf
"bad color `%s': %s"
1432 s (Printexc.to_string exn
);
1438 let te = name
, "", None
, textentry, ondone in
1443 state.mode <- Items
(active, first
, makeitems (), "", pan
, mode)
1448 let birdseye = isbirdseye mode in
1450 (if birdseye then "Setup bird's eye" else "Setup"), 0, Noaction
;
1452 boolp
"presentation"
1453 (fun () -> conf.presentation)
1455 conf.presentation <- v;
1456 state.anchor <- getanchor ();
1459 boolp
"ignore case in searches"
1460 (fun () -> conf.icase
)
1461 (fun v -> conf.icase
<- v);
1464 (fun () -> conf.preload)
1465 (fun v -> conf.preload <- v);
1468 (fun () -> conf.verbose
)
1469 (fun v -> conf.verbose
<- v);
1472 (fun () -> conf.maxhfit
)
1473 (fun v -> conf.maxhfit
<- v);
1476 (fun () -> conf.crophack
)
1477 (fun v -> conf.crophack
<- v);
1480 (fun () -> conf.showall
)
1481 (fun v -> conf.showall
<- v);
1483 boolp
"highlight links"
1484 (fun () -> conf.hlinks
)
1485 (fun v -> conf.hlinks
<- v);
1488 (fun () -> conf.underinfo
)
1489 (fun v -> conf.underinfo
<- v);
1490 boolp
"persistent bookmarks"
1491 (fun () -> conf.savebmarks
)
1492 (fun v -> conf.savebmarks
<- v);
1494 boolp
"proportional display"
1495 (fun () -> conf.proportional
)
1496 (fun v -> reinit conf.angle
v);
1498 boolp
"persistent location"
1499 (fun () -> conf.jumpback
)
1500 (fun v -> conf.jumpback
<- v);
1504 intp "vertical margin"
1505 (fun () -> conf.interpagespace)
1507 conf.interpagespace <- n;
1509 match state.layout with
1514 state.maxy <- calcheight ();
1515 let y = getpagey pageno in
1520 (fun () -> conf.pagebias
)
1521 (fun v -> conf.pagebias
<- v);
1524 (fun () -> conf.scrollstep
)
1525 (fun n -> conf.scrollstep
<- n);
1527 intp "auto scroll step"
1529 if state.ascrollstep
> 0
1530 then state.ascrollstep
1531 else conf.autoscrollstep
)
1533 if state.ascrollstep
> 0
1534 then state.ascrollstep
<- n
1535 else conf.autoscrollstep
<- n);
1538 (fun () -> truncate
(conf.zoom *. 100.))
1539 (fun v -> setzoom ((float v) /. 100.));
1542 (fun () -> conf.angle
)
1543 (fun v -> reinit v conf.proportional
);
1545 intp "scroll bar width"
1546 (fun () -> state.scrollw
)
1550 reshape conf.winw
conf.winh
;
1553 intp "scroll handle height"
1554 (fun () -> conf.scrollh
)
1555 (fun v -> conf.scrollh
<- v;);
1557 intp "thumbnail width"
1558 (fun () -> conf.thumbw
)
1560 conf.thumbw
<- min
1920 v;
1563 leavebirdseye beye
false;
1568 colorp
"background color"
1569 (fun () -> conf.bgcolor
)
1570 (fun v -> conf.bgcolor
<- v);
1573 "Presentation mode", 0, Noaction
;
1575 boolp
"scrollbar visible"
1576 (fun () -> conf.scrollbarinpm
)
1578 if v != conf.scrollbarinpm
1580 conf.scrollbarinpm
<- v;
1581 if conf.presentation
1583 state.scrollw
<- if v then conf.scrollbw
else 0;
1584 reshape conf.winw
conf.winh
;
1590 "Pixmap Cache", 0, Noaction
;
1592 intp "size (advisory)"
1593 (fun () -> conf.memlimit
)
1594 (fun v -> conf.memlimit
<- v);
1595 Printf.sprintf
"%-24s %d" "used" state.memused
, 1, Noaction
;
1602 :: ("Document", 0, Noaction
)
1603 :: List.map
(fun (_
, s) -> (s, 1, Noaction
)) state.docinfo
1609 :: (Printf.sprintf
"Save these parameters as defaults at exit (%s)"
1610 (btos conf.bedefault
),
1613 fun active first qsearch pan
->
1614 conf.bedefault
<- not
conf.bedefault
;
1615 Items
(active, first
, makeitems (), qsearch
, pan
, mode);
1620 Array.of_list
(items @ tailer)
1623 state.mode <- Items
(1, 0, makeitems (), "", 0, mode);
1624 Glut.postRedisplay
();
1627 let enterhelpmode () =
1628 state.mode <- Items
(-1, 0, state.help
, "", 0, state.mode);
1629 Glut.postRedisplay
();
1632 let quickbookmark ?title
() =
1633 match state.layout with
1639 let sec = Unix.gettimeofday
() in
1640 let tm = Unix.localtime
sec in
1641 Printf.sprintf
"Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1645 (tm.Unix.tm_year
+ 1900)
1648 | Some
title -> title
1651 (title, 0, (l
.pageno, float l
.pagey /. float l
.pageh
))
1656 state.fullscreen
<- None
;
1657 Glut.reshapeWindow
w h;
1660 let writeopen path password
=
1661 writecmd state.csock
("open " ^ path ^
"\000" ^ password ^
"\000");
1662 writecmd state.csock
"info";
1665 let opendoc path password
=
1668 state.password
<- password
;
1669 state.gen
<- state.gen
+ 1;
1671 writeopen path password
;
1672 Glut.setWindowTitle
("llpp " ^
Filename.basename path
);
1673 wcmd "geometry" [`i
state.w; `i
conf.winh
];
1676 let viewkeyboard key
=
1678 let mode = state.mode in
1679 state.mode <- Textentry
(te, fun _
-> state.mode <- mode);
1682 Glut.postRedisplay
()
1684 let c = Char.chr key
in
1686 | '
\027'
| 'q'
-> (* escape *)
1689 | '
\008'
-> (* backspace *)
1690 let y = getnav ~
-1 in
1691 gotoy_and_clear_text y
1699 Glut.postRedisplay
()
1702 let ondone isforw
s =
1703 cbput state.hists
.pat
s;
1704 state.searchpattern
<- s;
1707 let s = String.create
1 in
1709 enttext (s, "", Some
(onhist state.hists
.pat
),
1710 textentry, ondone (c ='
/'
))
1712 | '
+'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1713 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1714 setzoom (min
2.2 (conf.zoom +. incr))
1719 try int_of_string
s with exc
->
1720 state.text <- Printf.sprintf
"bad integer `%s': %s"
1721 s (Printexc.to_string exc
);
1727 state.text <- "page bias is now " ^ string_of_int
n;
1730 enttext ("page bias", "", None
, intentry, ondone)
1732 | '
-'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1733 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1734 setzoom (max
0.01 (conf.zoom -. decr))
1737 let ondone msg
= state.text <- msg
in
1739 "option [acfhilpstvAPRSZ]", "", None
,
1740 optentry state.mode, ondone
1743 | '
0'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1746 | '
1'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1747 let zoom = zoomforh
conf.winw
conf.winh
state.scrollw
in
1751 | '
9'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1757 try int_of_string
s with exc
->
1758 state.text <- Printf.sprintf
"bad integer `%s': %s"
1759 s (Printexc.to_string exc
);
1765 cbput state.hists
.pag
(string_of_int
n);
1766 gotoy_and_clear_text (getpagey (n + conf.pagebias
- 1))
1769 let pageentry text key
=
1770 match Char.unsafe_chr key
with
1771 | 'g'
-> TEdone
text
1772 | _
-> intentry text key
1774 let text = "x" in text.[0] <- c;
1775 enttext (":", text, Some
(onhist state.hists
.pag
), pageentry, ondone)
1778 state.scrollw
<- if state.scrollw
> 0 then 0 else conf.scrollbw
;
1779 reshape conf.winw
conf.winh
;
1782 conf.hlinks
<- not
conf.hlinks
;
1783 state.text <- "highlightlinks " ^
if conf.hlinks
then "on" else "off";
1784 Glut.postRedisplay
()
1787 if state.ascrollstep
= 0
1788 then state.ascrollstep
<- conf.autoscrollstep
1790 conf.autoscrollstep
<- state.ascrollstep
;
1791 state.ascrollstep
<- 0;
1795 conf.presentation <- not
conf.presentation;
1796 if conf.presentation
1798 if not
conf.scrollbarinpm
1799 then state.scrollw
<- 0;
1802 state.scrollw
<- conf.scrollbw
;
1804 showtext ' '
("presentation mode " ^
1805 if conf.presentation then "on" else "off");
1806 state.anchor <- getanchor ();
1810 begin match state.fullscreen
with
1812 state.fullscreen
<- Some
(conf.winw
, conf.winh
);
1815 state.fullscreen
<- None
;
1820 gotoy_and_clear_text 0
1823 search state.searchpattern
true
1826 search state.searchpattern
false
1829 begin match state.layout with
1832 gotoy_and_clear_text (getpagey l
.pageno)
1836 begin match List.rev
state.layout with
1839 let pageno = min
(l
.pageno+1) (state.pagecount
-1) in
1840 gotoy_and_clear_text (getpagey pageno)
1843 | '
\127'
-> (* delte *)
1844 begin match state.layout with
1847 let pageno = max
0 (l
.pageno-1) in
1848 gotoy_and_clear_text (getpagey pageno)
1853 if fn
= -1 then l
.pageno, l
.pageno else fn
, l
.pageno
1855 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
1857 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
1861 else (100. *. (float state.y /. float maxy)) in
1864 Printf.sprintf
"Page %d of %d %.2f%%"
1865 (fn+1) state.pagecount
percent
1868 "Pages %d-%d of %d %.2f%%"
1869 (fn+1) (ln
+1) state.pagecount
percent
1874 begin match state.layout with
1877 doreshape (l
.pagew
+ state.scrollw
) l
.pageh
;
1878 Glut.postRedisplay
();
1882 enterbookmarkmode ()
1892 match state.layout with
1895 (s, 0, (l
.pageno, float l
.pagey /. float l
.pageh
))
1899 enttext ("bookmark", "", None
, textentry, ondone)
1903 showtext ' '
"Quick bookmark added";
1906 begin match state.layout with
1908 let rect = getpdimrect l
.pagedimno
in
1912 (truncate
(1.8 *. (rect.(1) -. rect.(0))),
1913 truncate
(1.2 *. (rect.(3) -. rect.(0))))
1915 (truncate
(rect.(1) -. rect.(0)),
1916 truncate
(rect.(3) -. rect.(0)))
1920 doreshape (w + state.scrollw
) (h + conf.interpagespace)
1922 Glut.postRedisplay
();
1928 reinit (conf.angle
+ (if c = '
>'
then 30 else -30)) conf.proportional
1933 (min
(state.colorscale
+. (if c = '
]'
then 0.1 else -0.1)) 1.0);
1934 Glut.postRedisplay
()
1937 begin match state.mode with
1938 | Birdseye beye
-> upbirdseye beye
1939 | _
-> gotoy (clamp (-conf.scrollstep
))
1943 begin match state.mode with
1944 | Birdseye beye
-> downbirdseye beye
1945 | _
-> gotoy (clamp conf.scrollstep
)
1949 state.anchor <- getanchor ();
1950 opendoc state.path
state.password
1953 vlog "huh? %d %c" key
(Char.chr key
);
1956 let textentrykeyboard key
((c, text, opthist
, onkey
, ondone), onleave
) =
1958 state.mode <- Textentry
(te, onleave
);
1961 Glut.postRedisplay
()
1963 match Char.unsafe_chr key
with
1964 | '
\008'
-> (* backspace *)
1965 let len = String.length
text in
1969 Glut.postRedisplay
();
1972 let s = String.sub
text 0 (len - 1) in
1973 enttext (c, s, opthist
, onkey
, ondone)
1979 Glut.postRedisplay
()
1981 | '
\027'
-> (* escape *)
1982 begin match opthist
with
1984 | Some
(_
, onhistcancel
) -> onhistcancel
()
1987 Glut.postRedisplay
()
1990 begin match onkey
text key
with
1994 Glut.postRedisplay
()
1997 enttext (c, text, opthist
, onkey
, ondone);
2001 Glut.postRedisplay
()
2004 state.mode <- Textentry
(te, onleave
);
2005 Glut.postRedisplay
()
2009 let birdseyekeyboard key
((_
, _
, pageno, _
, _
) as beye
) =
2011 | 27 -> (* escape *)
2012 leavebirdseye beye
true
2014 | 12 -> (* ctrl-l *)
2015 let y, h = getpageyh pageno in
2016 let top = (conf.winh
- h) / 2 in
2017 gotoy (max
0 (y - top))
2020 leavebirdseye beye
false
2026 let itemskeyboard key
(active, first
, items, qsearch
, pan
, oldmode
) =
2027 let set active first qsearch
=
2028 state.mode <- Items
(active, first
, items, qsearch
, pan
, oldmode
)
2030 let search active pattern
incr =
2033 if n = Array.length
items || n = -1
2036 let (s, _
, _
) = items.(n) in
2038 (try ignore
(Str.search_forward re
s 0); true
2039 with Not_found
-> false)
2041 else loop (n + incr)
2046 let re = Str.regexp_case_fold pattern
in
2052 let firstof active = max
0 (active - maxoutlinerows () / 2) in
2054 | 18 | 19 -> (* ctrl-r/ctlr-s *)
2055 let incr = if key
= 18 then -1 else 1 in
2057 match search (active + incr) qsearch
incr with
2059 state.text <- qsearch ^
" [not found]";
2062 state.text <- qsearch
;
2063 active, firstof active
2065 set active first qsearch
;
2066 Glut.postRedisplay
();
2068 | 8 -> (* backspace *)
2069 let len = String.length qsearch
in
2076 set active first
"";
2079 let qsearch = String.sub
qsearch 0 (len - 1) in
2081 match search active qsearch ~
-1 with
2083 state.text <- qsearch ^
" [not found]";
2086 state.text <- qsearch;
2087 active, firstof active
2089 set active first
qsearch
2091 Glut.postRedisplay
()
2093 | _
when key
>= 32 && key
< 127 ->
2094 let pattern = addchar qsearch (Char.chr key
) in
2096 match search active pattern 1 with
2098 state.text <- pattern ^
" [not found]";
2101 state.text <- pattern;
2102 active, firstof active
2104 set active first
pattern;
2105 Glut.postRedisplay
()
2107 | 27 -> (* escape *)
2109 if String.length
qsearch = 0
2111 state.mode <- oldmode
;
2114 set active first
"";
2116 Glut.postRedisplay
()
2119 if active >= 0 && active < Array.length
items
2121 match items.(active) with
2123 state.mode <- f active first
qsearch pan
2127 state.mode <- oldmode
2131 state.mode <- oldmode
2133 Glut.postRedisplay
();
2135 | _
-> dolog "unknown key %d" key
2138 let outlinekeyboard key
2139 (allowdel
, active, first
, outlines, qsearch, pan
, oldmode
) =
2140 let narrow outlines pattern =
2141 let reopt = try Some
(Str.regexp_case_fold
pattern) with _
-> None
in
2145 let rec fold accu n =
2149 let (s, _
, _
) as o
= outlines.(n) in
2151 if (try ignore
(Str.search_forward
re s 0); true
2152 with Not_found
-> false)
2158 let matched = fold [] (Array.length
outlines - 1) in
2159 if matched = [] then None
else Some
(Array.of_list
matched)
2161 let search active pattern incr =
2164 if n = Array.length
outlines || n = -1
2167 let (s, _
, _
) = outlines.(n) in
2169 (try ignore
(Str.search_forward
re s 0); true
2170 with Not_found
-> false)
2172 else loop (n + incr)
2177 let re = Str.regexp_case_fold
pattern in
2183 let firstof active = max
0 (active - maxoutlinerows () / 2) in
2185 | 27 -> (* escape *)
2187 if String.length
qsearch = 0
2189 state.mode <- oldmode
;
2192 state.mode <- Outline
(
2193 allowdel
, active, first
, outlines, "", pan
, oldmode
2196 Glut.postRedisplay
();
2198 | 18 | 19 -> (* ctrl-r/ctrl-s *)
2199 let incr = if key
= 18 then -1 else 1 in
2201 match search (active + incr) qsearch incr with
2203 state.text <- qsearch ^
" [not found]";
2206 state.text <- qsearch;
2207 active, firstof active
2209 state.mode <- Outline
(
2210 allowdel
, active, first
, outlines, qsearch, pan
, oldmode
2212 Glut.postRedisplay
();
2214 | 8 -> (* backspace *)
2215 let len = String.length
qsearch in
2222 state.mode <- Outline
(
2223 allowdel
, active, first
, outlines, "", pan
, oldmode
2227 let qsearch = String.sub
qsearch 0 (len - 1) in
2229 match search active qsearch ~
-1 with
2231 state.text <- qsearch ^
" [not found]";
2234 state.text <- qsearch;
2235 active, firstof active
2237 state.mode <- Outline
(
2238 allowdel
, active, first
, outlines, qsearch, pan
, oldmode
2241 Glut.postRedisplay
()
2244 if active < Array.length
outlines
2246 let (_
, _
, anchor) = outlines.(active) in
2251 if allowdel
then state.bookmarks <- Array.to_list
outlines;
2252 state.mode <- oldmode
;
2253 Glut.postRedisplay
();
2255 | _
when key
>= 32 && key
< 127 ->
2256 let pattern = addchar qsearch (Char.chr key
) in
2258 match search active pattern 1 with
2260 state.text <- pattern ^
" [not found]";
2263 state.text <- pattern;
2264 active, firstof active
2266 state.mode <- Outline
(
2267 allowdel
, active, first
, outlines, pattern, pan
, oldmode
2269 Glut.postRedisplay
()
2271 | 14 when not allowdel
-> (* ctrl-n *)
2272 if String.length
qsearch > 0
2274 let optoutlines = narrow outlines qsearch in
2275 begin match optoutlines with
2276 | None
-> state.text <- "can't narrow"
2278 state.mode <- Outline
(
2279 allowdel
, 0, 0, outlines, qsearch, pan
, oldmode
2281 match state.outlines with
2284 state.outlines <- Onarrow
(qsearch, outlines, a)
2285 | Onarrow
(_
, _
, b) ->
2286 state.outlines <- Onarrow
(qsearch, outlines, b)
2289 Glut.postRedisplay
()
2291 | 21 when not allowdel
-> (* ctrl-u *)
2293 match state.outlines with
2296 let a = Array.of_list
(List.rev l
) in
2297 state.outlines <- Oarray
a;
2299 | Onarrow
(_
, _
, b) ->
2300 state.outlines <- Oarray
b;
2304 state.mode <- Outline
(allowdel
, 0, 0, outline, qsearch, pan
, oldmode
);
2305 Glut.postRedisplay
()
2307 | 12 -> (* ctrl-l *)
2308 state.mode <- Outline
2309 (allowdel
, active, firstof active, outlines, qsearch, pan
, oldmode
);
2310 Glut.postRedisplay
()
2312 | 127 when allowdel
-> (* delete *)
2313 let len = Array.length
outlines - 1 in
2317 state.bookmarks <- [];
2320 let bookmarks = Array.init
len
2322 let i = if i >= active then i + 1 else i in
2336 Glut.postRedisplay
()
2338 | _
-> dolog "unknown key %d" key
2341 let keyboard ~key ~x ~
y =
2344 if key
= 7 (* ctrl-g *)
2348 match state.mode with
2349 | Outline
outline -> outlinekeyboard key
outline
2350 | Textentry
textentry -> textentrykeyboard key
textentry
2351 | Birdseye
birdseye -> birdseyekeyboard key
birdseye
2352 | View
-> viewkeyboard key
2353 | Items
items -> itemskeyboard key
items
2356 let birdseyespecial key
((conf, leftx
, _
, hooverpageno
, anchor) as beye
) =
2358 | Glut.KEY_UP
-> upbirdseye beye
2359 | Glut.KEY_DOWN
-> downbirdseye beye
2361 | Glut.KEY_PAGE_UP
->
2362 begin match state.layout with
2366 state.mode <- Birdseye
(
2367 conf, leftx
, l
.pageno, hooverpageno
, anchor
2369 gotopage1 l
.pageno 0;
2372 let layout = layout (state.y-conf.winh
) conf.winh
in
2374 | [] -> gotoy (clamp (-conf.winh
))
2376 state.mode <- Birdseye
(
2377 conf, leftx
, l
.pageno, hooverpageno
, anchor
2379 gotopage1 l
.pageno 0
2382 | [] -> gotoy (clamp (-conf.winh
))
2385 | Glut.KEY_PAGE_DOWN
->
2386 begin match List.rev
state.layout with
2388 let layout = layout (state.y + conf.winh
) conf.winh
in
2389 begin match layout with
2391 let incr = l
.pageh
- l
.pagevh in
2396 conf, leftx
, state.pagecount
- 1, hooverpageno
, anchor
2398 Glut.postRedisplay
();
2400 else gotoy (clamp (incr + conf.interpagespace*2));
2404 Birdseye
(conf, leftx
, l
.pageno, hooverpageno
, anchor);
2405 gotopage1 l
.pageno 0;
2408 | [] -> gotoy (clamp conf.winh
)
2412 state.mode <- Birdseye
(conf, leftx
, 0, hooverpageno
, anchor);
2416 let pageno = state.pagecount
- 1 in
2417 state.mode <- Birdseye
(conf, leftx
, pageno, hooverpageno
, anchor);
2418 if not
(pagevisible state.layout pageno)
2421 match List.rev
state.pdims
with
2423 | (_
, _
, h, _
) :: _
-> h
2425 gotoy (max
0 (getpagey pageno - (conf.winh
- h - conf.interpagespace)))
2426 else Glut.postRedisplay
();
2430 let setautoscrollspeed goingdown
=
2431 let incr = max
1 (state.ascrollstep
/ 2) in
2432 let astep = max
1 (state.ascrollstep
+ (if goingdown
then incr else -incr)) in
2433 state.ascrollstep
<- astep;
2436 let special ~key ~x ~
y =
2439 match state.mode with
2440 | View
| (Birdseye _
) when key
= Glut.KEY_F9
->
2444 birdseyespecial key vals
2446 | View
when key
= Glut.KEY_F1
->
2450 if state.ascrollstep
> 0 && (key
= Glut.KEY_DOWN
|| key
= Glut.KEY_UP
)
2451 then setautoscrollspeed (key
= Glut.KEY_DOWN
)
2455 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
2456 | Glut.KEY_UP
-> clamp (-conf.scrollstep
)
2457 | Glut.KEY_DOWN
-> clamp conf.scrollstep
2458 | Glut.KEY_PAGE_UP
->
2459 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2461 match state.layout with
2463 | l
:: _
-> state.y - l
.pagey
2466 | Glut.KEY_PAGE_DOWN
->
2467 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2469 match List.rev
state.layout with
2471 | l
:: _
-> getpagey l
.pageno
2474 | Glut.KEY_HOME
-> addnav (); 0
2477 state.maxy - (if conf.maxhfit
then conf.winh
else 0)
2479 | (Glut.KEY_RIGHT
| Glut.KEY_LEFT
) when
2480 Glut.getModifiers
() land Glut.active_alt
!= 0 ->
2481 getnav (if key
= Glut.KEY_LEFT
then 1 else -1)
2483 | Glut.KEY_RIGHT
when conf.zoom > 1.0 ->
2484 state.x
<- state.x
- 10;
2486 | Glut.KEY_LEFT
when conf.zoom > 1.0 ->
2487 state.x
<- state.x
+ 10;
2492 gotoy_and_clear_text y
2495 ((c, _
, (Some
(action, _
) as onhist), onkey
, ondone), mode) ->
2498 | Glut.KEY_UP
-> action HCprev
2499 | Glut.KEY_DOWN
-> action HCnext
2500 | Glut.KEY_HOME
-> action HCfirst
2501 | Glut.KEY_END
-> action HClast
2504 state.mode <- Textentry
((c, s, onhist, onkey
, ondone), mode);
2505 Glut.postRedisplay
()
2509 | Items
(active, first
, items, qsearch, pan
, oldmode
) ->
2510 let maxrows = maxoutlinerows () in
2511 let itemcount = Array.length
items in
2512 let hasaction = function
2513 | (_
, _
, Noaction
) -> false
2516 let find start
incr =
2518 if i = -1 || i = itemcount
2521 if hasaction items.(i)
2523 else find (i + incr)
2528 let set active first
=
2529 let first = max
0 (min
first (itemcount - maxrows)) in
2530 state.mode <- Items
(active, first, items, qsearch, pan
, oldmode
)
2533 let isvisible first n = n >= first && n - first <= maxrows in
2535 let incr1 = if incr > 0 then 1 else -1 in
2536 if isvisible first active
2539 let next = active + incr in
2541 if next < 0 || next >= itemcount
2543 else find next incr1
2545 if next = -1 || abs
(active - next) > maxrows
2551 let first = first + incr in
2552 let first = max
0 (min
first (itemcount - 1)) in
2554 let next = active + incr in
2555 let next = max
0 (min
next (itemcount - 1)) in
2558 let active = if next = -1 then active else next in
2561 let first = min
next first in
2564 let first = first + incr in
2565 let first = max
0 (min
first (itemcount - 1)) in
2567 let next = active + incr in
2568 let next = max
0 (min
next (itemcount - 1)) in
2569 let next = find next incr1 in
2570 if next = -1 || abs
(active - first) > maxrows
2577 Glut.postRedisplay
()
2579 begin match key
with
2580 | Glut.KEY_UP
-> navigate ~
-1
2581 | Glut.KEY_DOWN
-> navigate 1
2582 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
2583 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
2586 state.mode <- Items
(
2587 active, first, items, qsearch, min
0 (pan
- 1), oldmode
2589 Glut.postRedisplay
()
2592 state.mode <- Items
(
2593 active, first, items, qsearch, min
0 (pan
+ 1), oldmode
2595 Glut.postRedisplay
()
2598 let active = find 0 1 in
2600 Glut.postRedisplay
()
2603 let first = max
0 (itemcount - maxrows) in
2604 let active = find (itemcount - 1) ~
-1 in
2606 Glut.postRedisplay
()
2611 | Outline
(allowdel
, active, first, outlines, qsearch, pan
, oldmode
) ->
2612 let maxrows = maxoutlinerows () in
2613 let calcfirst first active =
2616 let rows = active - first in
2617 if rows > maxrows then active - maxrows else first
2621 let active = active + incr in
2622 let active = max
0 (min
active (Array.length
outlines - 1)) in
2623 let first = calcfirst first active in
2624 state.mode <- Outline
(
2625 allowdel
, active, first, outlines, qsearch, pan
, oldmode
2627 Glut.postRedisplay
()
2629 let updownlevel incr =
2630 let len = Array.length
outlines in
2631 let (_
, curlevel
, _
) = outlines.(active) in
2633 if i = len then i-1 else if i = -1 then 0 else
2634 let (_
, l
, _
) = outlines.(i) in
2635 if l
!= curlevel
then i else flow (i+incr)
2637 let active = flow active in
2638 let first = calcfirst first active in
2639 state.mode <- Outline
(
2640 allowdel
, active, first, outlines, qsearch, pan
, oldmode
2642 Glut.postRedisplay
()
2645 | Glut.KEY_UP
-> navigate ~
-1
2646 | Glut.KEY_DOWN
-> navigate 1
2647 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
2648 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
2651 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2653 state.mode <- Outline
(
2654 allowdel
, active, first, outlines,
2655 qsearch, min
0 (pan
+ 1), oldmode
2657 Glut.postRedisplay
();
2665 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2667 state.mode <- Outline
(
2668 allowdel
, active, first, outlines, qsearch, pan
- 1, oldmode
2670 Glut.postRedisplay
();
2674 then updownlevel ~
-1
2678 state.mode <- Outline
(
2679 allowdel
, 0, 0, outlines, qsearch, pan
, oldmode
2681 Glut.postRedisplay
()
2684 let active = Array.length
outlines - 1 in
2685 let first = max
0 (active - maxrows) in
2686 state.mode <- Outline
(
2687 allowdel
, active, first, outlines, qsearch, pan
, oldmode
2689 Glut.postRedisplay
()
2694 let drawplaceholder l
=
2695 let margin = state.x
+ (conf.winw
- (state.w + state.scrollw
)) / 2 in
2697 (float l
.pagex
, float l
.pagedispy
)
2698 (float (l
.pagew
+ l
.pagex
), float (l
.pagedispy
+ l
.pagevh))
2700 let x = if margin < 0 then -margin else l
.pagex
2701 and y = l
.pagedispy
+ 13 in
2702 GlDraw.color
(0.0, 0.0, 0.0);
2703 drawstring 13 x y ("Loading " ^ string_of_int
(l
.pageno + 1))
2706 let now () = Unix.gettimeofday
();;
2710 match state.mode with
2711 | Textentry _
-> scalecolor 0.4
2712 | View
| Outline _
| Items _
-> scalecolor 1.0
2713 | Birdseye
(_
, _
, pageno, hooverpageno
, _
) ->
2714 if l
.pageno = hooverpageno
2717 if l
.pageno = pageno
2723 begin match getopaque l
.pageno with
2724 | Some
(opaque, _
) when validopaque opaque ->
2726 draw
(l
.pagedispy
, l
.pagew
, l
.pagevh, l
.pagey, conf.hlinks
)
2730 vlog "draw %d %f sec" l
.pageno d;
2738 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
2739 let sh = (float (maxy + conf.winh
) /. float conf.winh
) in
2740 let sh = float conf.winh
/. sh in
2741 let sh = max
sh (float conf.scrollh
) in
2744 if state.y = state.maxy
2746 else float y /. float maxy
2748 let position = (float conf.winh
-. sh) *. percent in
2751 if position +. sh > float conf.winh
2752 then float conf.winh
-. sh
2758 let scrollindicator () =
2759 GlDraw.color (0.64 , 0.64, 0.64);
2761 (float (conf.winw
- state.scrollw
), 0.)
2762 (float conf.winw
, float conf.winh
)
2764 GlDraw.color (0.0, 0.0, 0.0);
2766 let position, sh = scrollph state.y in
2768 (float (conf.winw
- state.scrollw
), position)
2769 (float conf.winw
, position +. sh)
2773 let showsel margin =
2774 match state.mstate
with
2775 | Mnone
| Mscroll _
| Mpan _
| Mzoom _
->
2778 | Msel
((x0
, y0
), (x1
, y1
)) ->
2779 let rec loop = function
2781 if (y0
>= l
.pagedispy
&& y0
<= (l
.pagedispy
+ l
.pagevh))
2782 || ((y1
>= l
.pagedispy
&& y1
<= (l
.pagedispy
+ l
.pagevh)))
2784 match getopaque l
.pageno with
2785 | Some
(opaque, _
) when validopaque opaque ->
2786 let oy = -l
.pagey + l
.pagedispy
in
2788 (x0
- margin - state.x, y0
,
2789 x1
- margin - state.x, y1
) oy;
2799 let panx = float state.x in
2801 GlDraw.color (0.0, 0.0, 1.0) ~alpha
:0.5;
2802 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2804 (fun (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) ->
2806 if l
.pageno = pageno
2808 let d = float (l
.pagedispy
- l
.pagey) in
2809 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
2810 GlDraw.begins `quads
;
2812 GlDraw.vertex2
(x0
+.panx, y0
+.d);
2813 GlDraw.vertex2
(x1
+.panx, y1
+.d);
2814 GlDraw.vertex2
(x2
+.panx, y2
+.d);
2815 GlDraw.vertex2
(x3
+.panx, y3
+.d);
2825 let showstrings active first pan
strings =
2827 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2828 GlDraw.color (0., 0., 0.) ~alpha
:0.85;
2829 GlDraw.rect (0., 0.) (float conf.winw
, float conf.winh
);
2830 GlDraw.color (1., 1., 1.);
2831 Gl.enable `texture_2d
;
2834 if row
= Array.length
strings || (row
- first) * 16 > conf.winh
2837 let (s, level
, _
) = strings.(row
) in
2838 let y = (row
- first) * 16 in
2839 let x = 5 + 15*(max
0 (level
+pan
)) in
2842 Gl.disable `texture_2d
;
2843 GlDraw.polygon_mode `both `
line;
2844 GlDraw.color (1., 1., 1.) ~alpha
:0.9;
2845 GlDraw.rect (0., float (y + 1))
2846 (float (conf.winw
- 1), float (y + 18));
2847 GlDraw.polygon_mode `both `fill
;
2848 GlDraw.color (1., 1., 1.);
2849 Gl.enable `texture_2d
;
2852 let l = String.length
s in
2855 let pan = pan * 2 in
2856 let pos = pan + level
in
2857 let left = l + pos in
2863 else String.sub
s (-pos) left
2865 drawstring1 14 x (y + 16) s
2868 drawstring1 14 (x + pan*15) (y + 16) s
2876 Gl.disable `texture_2d
;
2879 let showoutline (_
, active, first, outlines, _
, pan, _
) =
2880 showstrings active first pan outlines;
2883 let showitems (active, first, items, _
, pan, _
) =
2884 showstrings active first pan items;
2888 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
2889 GlDraw.viewport
margin 0 state.w conf.winh
;
2891 GlClear.color (scalecolor2 conf.bgcolor
);
2892 GlClear.clear
[`
color];
2895 Gl.enable `scissor_test
;
2896 GlMisc.scissor
0 0 (conf.winw
- state.scrollw
) conf.winh
;
2898 List.iter
drawpage state.layout;
2901 Gl.disable `scissor_test
2905 let x = -.float state.x in
2906 GlMat.translate ~
x ();
2910 GlDraw.viewport
0 0 conf.winw
conf.winh
;
2913 begin match state.mode with
2914 | Items
items -> showitems items
2915 | Outline
outline -> showoutline outline
2919 Glut.swapBuffers
();
2923 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
2924 let x = x - margin - state.x in
2925 let rec f = function
2927 begin match getopaque l.pageno with
2928 | Some
(opaque, _
) when validopaque opaque ->
2929 let y = y - l.pagedispy
in
2932 let y = l.pagey + y in
2933 let x = x - l.pagex
in
2934 match whatsunder
opaque x y with
2947 let viewmouse button bstate
x y =
2949 | Glut.OTHER_BUTTON
n when (n == 3 || n == 4) && bstate
= Glut.UP
->
2950 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2952 match state.mstate
with
2953 | Mzoom
(oldn
, i) ->
2961 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
2963 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
2965 let zoom = conf.zoom +. incr in
2967 state.mstate
<- Mzoom
(n, 0);
2969 state.mstate
<- Mzoom
(n, i+1);
2971 else state.mstate
<- Mzoom
(n, 0)
2973 | _
-> state.mstate
<- Mzoom
(n, 0)
2976 if state.ascrollstep
> 0
2978 setautoscrollspeed (n=4)
2982 then -conf.scrollstep
2983 else conf.scrollstep
2985 let incr = incr * 2 in
2986 let y = clamp incr in
2987 gotoy_and_clear_text y
2990 | Glut.LEFT_BUTTON
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
2991 if bstate
= Glut.DOWN
2993 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
2994 state.mstate
<- Mpan
(x, y)
2997 state.mstate
<- Mnone
2999 | Glut.LEFT_BUTTON
when x > conf.winw
- state.scrollw
->
3000 if bstate
= Glut.DOWN
3002 let position, sh = scrollph state.y in
3003 if y > truncate
position && y < truncate
(position +. sh)
3005 state.mstate
<- Mscroll
3007 let percent = float y /. float conf.winh
in
3008 let desty = truncate
(float (state.maxy - conf.winh
) *. percent) in
3010 state.mstate
<- Mscroll
3012 state.mstate
<- Mnone
3014 | Glut.LEFT_BUTTON
->
3015 let dest = if bstate
= Glut.DOWN
then getunder x y else Unone
in
3016 begin match dest with
3017 | Ulinkgoto
(pageno, top) ->
3021 gotopage1 pageno top;
3027 | Unone
when bstate
= Glut.DOWN
->
3028 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
3029 state.mstate
<- Mpan
(x, y);
3031 | Unone
| Utext _
->
3032 if bstate
= Glut.DOWN
3034 if conf.angle
mod 360 = 0
3036 state.mstate
<- Msel
((x, y), (x, y));
3037 Glut.postRedisplay
()
3041 match state.mstate
with
3044 | Mzoom _
| Mscroll
->
3045 state.mstate
<- Mnone
3048 Glut.setCursor
Glut.CURSOR_INHERIT
;
3049 state.mstate
<- Mnone
3051 | Msel
((_
, y0
), (_
, y1
)) ->
3053 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh))
3054 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh)))
3056 match getopaque l.pageno with
3057 | Some
(opaque, _
) when validopaque opaque ->
3061 List.iter
f state.layout;
3062 copysel
""; (* ugly *)
3063 Glut.setCursor
Glut.CURSOR_INHERIT
;
3064 state.mstate
<- Mnone
;
3071 let birdseyemouse button bstate
x y
3072 (conf, leftx
, _
, hooverpageno
, anchor) =
3074 | Glut.LEFT_BUTTON
when bstate
= Glut.UP
->
3075 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
3076 let rec loop = function
3079 if y > l.pagedispy
&& y < l.pagedispy
+ l.pagevh
3080 && x > margin && x < margin + l.pagew
3082 leavebirdseye (conf, leftx
, l.pageno, hooverpageno
, anchor) false;
3087 | Glut.OTHER_BUTTON _
-> viewmouse button bstate
x y
3091 let mouse bstate button
x y =
3092 match state.mode with
3093 | View
-> viewmouse button bstate
x y
3094 | Birdseye beye
-> birdseyemouse button bstate
x y beye
3095 | Textentry _
| Outline _
| Items _
-> ()
3098 let mouse ~button ~
state ~
x ~
y = mouse state button
x y;;
3101 match state.mode with
3104 match state.mstate
with
3105 | Mzoom _
| Mnone
-> ()
3110 state.mstate
<- Mpan
(x, y);
3111 if conf.zoom > 1.0 then state.x <- state.x + dx;
3113 gotoy_and_clear_text y
3116 state.mstate
<- Msel
(a, (x, y));
3117 Glut.postRedisplay
()
3120 let y = min
conf.winh
(max
0 y) in
3121 let percent = float y /. float conf.winh
in
3122 let y = truncate
(float (state.maxy - conf.winh
) *. percent) in
3123 gotoy_and_clear_text y
3127 match state.mode with
3128 | Birdseye
(conf, leftx
, pageno, hooverpageno
, anchor) ->
3129 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
3130 let rec loop = function
3132 if hooverpageno
!= -1
3134 state.mode <- Birdseye
(conf, leftx
, pageno, -1, anchor);
3135 Glut.postRedisplay
();
3138 if y > l.pagedispy
&& y < l.pagedispy
+ l.pagevh
3139 && x > margin && x < margin + l.pagew
3141 state.mode <- Birdseye
(conf, leftx
, pageno, l.pageno, anchor);
3142 Glut.postRedisplay
();
3148 | Outline _
| Items _
| Textentry _
-> ()
3150 match state.mstate
with
3152 begin match getunder x y with
3153 | Unone
-> Glut.setCursor
Glut.CURSOR_INHERIT
3155 if conf.underinfo
then showtext 'u'
("ri: " ^ uri
);
3156 Glut.setCursor
Glut.CURSOR_INFO
3157 | Ulinkgoto
(page
, _
) ->
3159 then showtext 'p'
("age: " ^ string_of_int page
);
3160 Glut.setCursor
Glut.CURSOR_INFO
3162 if conf.underinfo
then showtext '
f'
("ont: " ^
s);
3163 Glut.setCursor
Glut.CURSOR_TEXT
3166 | Mpan _
| Msel _
| Mzoom _
| Mscroll
->
3177 match Sys.os_type
with
3178 | "Win32" -> Sys.getenv
"HOMEPATH"
3179 | _
-> Sys.getenv
"HOME"
3182 ("Can not determine home directory location: " ^
3183 Printexc.to_string exn
);
3187 let config_of c attrs
=
3191 | "scroll-bar-width" -> { c with scrollbw
= max
0 (int_of_string
v) }
3192 | "scroll-handle-height" -> { c with scrollh
= max
0 (int_of_string
v) }
3193 | "case-insensitive-search" -> { c with icase
= bool_of_string
v }
3194 | "preload" -> { c with preload = bool_of_string
v }
3195 | "page-bias" -> { c with pagebias
= int_of_string
v }
3196 | "scroll-step" -> { c with scrollstep
= max
1 (int_of_string
v) }
3197 | "auto-scroll-step" ->
3198 { c with autoscrollstep
= max
0 (int_of_string
v) }
3199 | "max-height-fit" -> { c with maxhfit
= bool_of_string
v }
3200 | "crop-hack" -> { c with crophack
= bool_of_string
v }
3201 | "throttle" -> { c with showall
= bool_of_string
v }
3202 | "highlight-links" -> { c with hlinks
= bool_of_string
v }
3203 | "under-cursor-info" -> { c with underinfo
= bool_of_string
v }
3204 | "vertical-margin" ->
3205 { c with interpagespace = max
0 (int_of_string
v) }
3207 let zoom = float_of_string
v /. 100. in
3208 let zoom = max
0.01 (min
2.2 zoom) in
3209 { c with zoom = zoom }
3210 | "presentation" -> { c with presentation = bool_of_string
v }
3211 | "rotation-angle" -> { c with angle
= int_of_string
v }
3212 | "width" -> { c with winw
= max
20 (int_of_string
v) }
3213 | "height" -> { c with winh
= max
20 (int_of_string
v) }
3214 | "persistent-bookmarks" -> { c with savebmarks
= bool_of_string
v }
3215 | "proportional-display" -> { c with proportional
= bool_of_string
v }
3216 | "pixmap-cache-size" -> { c with memlimit
= max
2 (int_of_string
v) }
3217 | "tex-count" -> { c with texcount
= max
1 (int_of_string
v) }
3218 | "slice-height" -> { c with sliceheight
= max
2 (int_of_string
v) }
3219 | "thumbnail-width" -> { c with thumbw
= max
2 (int_of_string
v) }
3220 | "persistent-location" -> { c with jumpback
= bool_of_string
v }
3221 | "background-color" -> { c with bgcolor
= color_of_string v }
3222 | "scrollbar-in-presentation" ->
3223 { c with scrollbarinpm
= bool_of_string
v }
3224 | "ui-font" -> { c with uifont
= v }
3227 prerr_endline
("Error processing attribute (`" ^
3228 k ^
"'=`" ^
v ^
"'): " ^
Printexc.to_string exn
);
3231 let rec fold c = function
3234 let c = apply c k
v in
3240 let fromstring f pos n v d =
3243 dolog "Error processing attribute (%S=%S) at %d\n%s"
3244 n v pos (Printexc.to_string exn
)
3249 let bookmark_of attrs
=
3250 let rec fold title page rely
= function
3251 | ("title", v) :: rest
-> fold v page rely rest
3252 | ("page", v) :: rest
-> fold title v rely rest
3253 | ("rely", v) :: rest
-> fold title page
v rest
3254 | _
:: rest
-> fold title page rely rest
3255 | [] -> title, page
, rely
3257 fold "invalid" "0" "0" attrs
3261 let rec fold path page rely
pan = function
3262 | ("path", v) :: rest
-> fold v page rely
pan rest
3263 | ("page", v) :: rest
-> fold path
v rely
pan rest
3264 | ("rely", v) :: rest
-> fold path page
v pan rest
3265 | ("pan", v) :: rest
-> fold path page rely
v rest
3266 | _
:: rest
-> fold path page rely
pan rest
3267 | [] -> path
, page
, rely
, pan
3269 fold "" "0" "0" "0" attrs
3272 let setconf dst src
=
3273 dst
.scrollbw
<- src
.scrollbw
;
3274 dst
.scrollh
<- src
.scrollh
;
3275 dst
.icase
<- src
.icase
;
3276 dst
.preload <- src
.preload;
3277 dst
.pagebias
<- src
.pagebias
;
3278 dst
.verbose
<- src
.verbose
;
3279 dst
.scrollstep
<- src
.scrollstep
;
3280 dst
.maxhfit
<- src
.maxhfit
;
3281 dst
.crophack
<- src
.crophack
;
3282 dst
.autoscrollstep
<- src
.autoscrollstep
;
3283 dst
.showall
<- src
.showall
;
3284 dst
.hlinks
<- src
.hlinks
;
3285 dst
.underinfo
<- src
.underinfo
;
3286 dst
.interpagespace <- src
.interpagespace;
3287 dst
.zoom <- src
.zoom;
3288 dst
.presentation <- src
.presentation;
3289 dst
.angle
<- src
.angle
;
3290 dst
.winw
<- src
.winw
;
3291 dst
.winh
<- src
.winh
;
3292 dst
.savebmarks
<- src
.savebmarks
;
3293 dst
.memlimit
<- src
.memlimit
;
3294 dst
.proportional
<- src
.proportional
;
3295 dst
.texcount
<- src
.texcount
;
3296 dst
.sliceheight
<- src
.sliceheight
;
3297 dst
.thumbw
<- src
.thumbw
;
3298 dst
.jumpback
<- src
.jumpback
;
3299 dst
.bgcolor
<- src
.bgcolor
;
3300 dst
.scrollbarinpm
<- src
.scrollbarinpm
;
3301 dst
.uifont
<- src
.uifont
;
3305 let l = String.length
s in
3306 let b = Buffer.create
l in
3312 let h = Hashtbl.create
10 in
3313 let dc = { defconf with angle
= defconf.angle
} in
3314 let rec toplevel v t spos _
=
3316 | Vdata
| Vcdata
| Vend
-> v
3317 | Vopen
("llppconfig", _
, closed
) ->
3320 else { v with f = llppconfig
}
3322 error
"unexpected subelement at top level" s spos
3323 | Vclose _
-> error
"unexpected close at top level" s spos
3325 and llppconfig
v t spos _
=
3327 | Vdata
| Vcdata
| Vend
-> v
3328 | Vopen
("defaults", attrs
, closed
) ->
3329 let c = config_of dc attrs
in
3333 else { v with f = skip
"defaults" (fun () -> v) }
3335 | Vopen
("doc", attrs
, closed
) ->
3336 let pathent, spage
, srely
, span
= doc_of attrs
in
3337 let path = unent pathent
3338 and pageno = fromstring int_of_string spos
"page" spage
0
3339 and rely
= fromstring float_of_string spos
"rely" srely
0.0
3340 and pan = fromstring int_of_string spos
"pan" span
0 in
3341 let c = config_of dc attrs
in
3342 let anchor = (pageno, rely
) in
3344 then (Hashtbl.add
h path (c, [], pan, anchor); v)
3345 else { v with f = doc
path pan anchor c [] }
3348 error
"unexpected subelement in llppconfig" s spos
3350 | Vclose
"llppconfig" -> { v with f = toplevel }
3351 | Vclose _
-> error
"unexpected close in llppconfig" s spos
3353 and doc
path pan anchor c bookmarks v t spos _
=
3355 | Vdata
| Vcdata
-> v
3356 | Vend
-> error
"unexpected end of input in doc" s spos
3357 | Vopen
("bookmarks", _
, closed
) ->
3360 else { v with f = pbookmarks
path pan anchor c bookmarks }
3362 | Vopen
(_
, _
, _
) ->
3363 error
"unexpected subelement in doc" s spos
3366 Hashtbl.add
h path (c, List.rev
bookmarks, pan, anchor);
3367 { v with f = llppconfig
}
3369 | Vclose _
-> error
"unexpected close in doc" s spos
3371 and pbookmarks
path pan anchor c bookmarks v t spos _
=
3373 | Vdata
| Vcdata
-> v
3374 | Vend
-> error
"unexpected end of input in bookmarks" s spos
3375 | Vopen
("item", attrs
, closed
) ->
3376 let titleent, spage
, srely
= bookmark_of attrs
in
3377 let page = fromstring int_of_string spos
"page" spage
0
3378 and rely
= fromstring float_of_string spos
"rely" srely
0.0 in
3379 let bookmarks = (unent titleent, 0, (page, rely
)) :: bookmarks in
3381 then { v with f = pbookmarks
path pan anchor c bookmarks }
3384 { v with f = skip
"item" f }
3387 error
"unexpected subelement in bookmarks" s spos
3389 | Vclose
"bookmarks" ->
3390 { v with f = doc
path pan anchor c bookmarks }
3392 | Vclose _
-> error
"unexpected close in bookmarks" s spos
3394 and skip tag
f v t spos _
=
3396 | Vdata
| Vcdata
-> v
3398 error
("unexpected end of input in skipped " ^ tag
) s spos
3399 | Vopen
(tag'
, _
, closed
) ->
3403 let f'
() = { v with f = skip tag
f } in
3404 { v with f = skip tag'
f'
}
3408 else error
("unexpected close in skipped " ^ tag
) s spos
3411 parse
{ f = toplevel; accu = () } s;
3417 let len = in_channel_length ic
in
3418 let s = String.create
len in
3419 really_input ic
s 0 len;
3422 | Parse_error
(msg
, s, pos) ->
3423 let subs = subs s pos in
3424 let s = Printf.sprintf
"%s: at %d [..%s..]" msg
pos subs in
3425 failwith
("parse error: " ^
s)
3428 failwith
("config load error: " ^
Printexc.to_string exn
)
3434 let dir = Filename.concat
home ".config" in
3435 if Sys.is_directory
dir then dir else home
3438 Filename.concat
dir "llpp.conf"
3442 if Sys.file_exists
path
3445 (try Some
(open_in_bin
path)
3448 ("Error opening configuation file `" ^
path ^
"': " ^
3449 Printexc.to_string exn
);
3458 ("Error loading configuation from `" ^
path ^
"': " ^
3459 Printexc.to_string exn
);
3465 f (Hashtbl.create
0, defconf)
3470 let pc, pb
, px
, pa
=
3472 Hashtbl.find h (Filename.basename
state.path)
3473 with Not_found
-> dc, [], 0, (0, 0.0)
3477 state.bookmarks <- pb
;
3479 state.scrollw
<- conf.scrollbw
;
3481 then state.anchor <- pa
;
3482 cbput state.hists
.nav pa
;
3487 let add_attrs bb always
dc c =
3490 then Printf.bprintf bb
"\n %s='%b'" s a
3493 then Printf.bprintf bb
"\n %s='%d'" s a
3496 then Printf.bprintf bb
"\n %s='%f'" s (a*.100.)
3500 Printf.bprintf bb
"\n %s='%s'" s (color_to_string a)
3504 Printf.bprintf bb
"\n %s='%s'" s a
3508 then dc.winw
, dc.winh
3510 match state.fullscreen
with
3512 | None
-> c.winw
, c.winh
3514 let zoom, presentation, interpagespace, showall
=
3516 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
3518 match state.mode with
3519 | Birdseye
(bc
, _
, _
, _
, _
) ->
3520 bc
.zoom, bc
.presentation, bc
.interpagespace, bc
.showall
3521 | _
-> c.zoom, c.presentation, c.interpagespace, c.showall
3523 oi
"width" w dc.winw
;
3524 oi
"height" h dc.winh
;
3525 oi
"scroll-bar-width" c.scrollbw
dc.scrollbw
;
3526 oi
"scroll-handle-height" c.scrollh
dc.scrollh
;
3527 ob "case-insensitive-search" c.icase
dc.icase
;
3528 ob "preload" c.preload dc.preload;
3529 oi
"page-bias" c.pagebias
dc.pagebias
;
3530 oi
"scroll-step" c.scrollstep
dc.scrollstep
;
3531 oi
"auto-scroll-step" c.autoscrollstep
dc.autoscrollstep
;
3532 ob "max-height-fit" c.maxhfit
dc.maxhfit
;
3533 ob "crop-hack" c.crophack
dc.crophack
;
3534 ob "throttle" showall
dc.showall
;
3535 ob "highlight-links" c.hlinks
dc.hlinks
;
3536 ob "under-cursor-info" c.underinfo
dc.underinfo
;
3537 oi
"vertical-margin" interpagespace dc.interpagespace;
3538 oz
"zoom" zoom dc.zoom;
3539 ob "presentation" presentation dc.presentation;
3540 oi
"rotation-angle" c.angle
dc.angle
;
3541 ob "persistent-bookmarks" c.savebmarks
dc.savebmarks
;
3542 ob "proportional-display" c.proportional
dc.proportional
;
3543 oi
"pixmap-cache-size" c.memlimit
dc.memlimit
;
3544 oi
"texcount" c.texcount
dc.texcount
;
3545 oi
"slice-height" c.sliceheight
dc.sliceheight
;
3546 oi
"thumbnail-width" c.thumbw
dc.thumbw
;
3547 ob "persistent-location" c.jumpback
dc.jumpback
;
3548 oc
"background-color" c.bgcolor
dc.bgcolor
;
3549 ob "scrollbar-in-presentation" c.scrollbarinpm
dc.scrollbarinpm
;
3550 os
"ui-font" c.uifont
dc.uifont
;
3554 let bb = Buffer.create
32768 in
3556 let dc = if conf.bedefault
then conf else dc in
3557 Buffer.add_string
bb "<llppconfig>\n<defaults ";
3558 add_attrs bb true dc dc;
3559 Buffer.add_string
bb "/>\n";
3561 let adddoc path pan anchor c bookmarks =
3562 if bookmarks == [] && c = dc && anchor = emptyanchor
3565 Printf.bprintf
bb "<doc path='%s'"
3566 (enent
path 0 (String.length
path));
3568 if anchor <> emptyanchor
3570 let n, y = anchor in
3571 Printf.bprintf
bb " page='%d'" n;
3574 Printf.bprintf
bb " rely='%f'" y
3579 then Printf.bprintf
bb " pan='%d'" pan;
3581 add_attrs bb false dc c;
3583 begin match bookmarks with
3584 | [] -> Buffer.add_string
bb "/>\n"
3586 Buffer.add_string
bb ">\n<bookmarks>\n";
3587 List.iter
(fun (title, _level
, (page, rely
)) ->
3589 "<item title='%s' page='%d'"
3590 (enent
title 0 (String.length
title))
3595 Printf.bprintf
bb " rely='%f'" rely
3597 Buffer.add_string
bb "/>\n";
3599 Buffer.add_string
bb "</bookmarks>\n</doc>\n";
3605 match state.mode with
3606 | Birdseye
(_
, pan, _
, _
, _
) -> pan
3609 let basename = Filename.basename state.path in
3610 adddoc basename pan (getanchor ())
3613 if state.ascrollstep
> 0
3614 then state.ascrollstep
3615 else conf.autoscrollstep
}
3616 (if conf.savebmarks
then state.bookmarks else []);
3618 Hashtbl.iter
(fun path (c, bookmarks, x, y) ->
3620 then adddoc path x y c bookmarks
3622 Buffer.add_string
bb "</llppconfig>";
3625 if Buffer.length
bb > 0
3628 let tmp = path ^
".tmp" in
3629 let oc = open_out_bin
tmp in
3630 Buffer.output_buffer
oc bb;
3632 Sys.rename
tmp path;
3635 ("error while saving configuration: " ^
Printexc.to_string exn
)
3642 [("-p", Arg.String
(fun s -> state.password
<- s) , " Set password")
3643 ;("-v", Arg.Unit
(fun () -> print_endline
Help.version
; exit
0),
3644 " Print version and exit")]
3646 (fun s -> state.path <- s)
3647 ("Usage: " ^
Sys.argv
.(0) ^
" [options] some.pdf\nOptions:")
3649 if String.length
state.path = 0
3650 then (prerr_endline
"file name missing"; exit
1);
3654 let _ = Glut.init
Sys.argv
in
3655 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
3656 let () = Glut.initWindowSize
conf.winw
conf.winh
in
3657 let _ = Glut.createWindow
("llpp " ^
Filename.basename state.path) in
3660 if Sys.os_type
= "Unix"
3662 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
3664 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
3665 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
3666 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
3667 Unix.bind
sock addr;
3669 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
3670 Unix.connect
csock addr;
3671 let ssock, _ = Unix.accept
sock in
3674 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
3675 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
3679 at_exit
(fun () -> Unix.shutdown
ssock Unix.SHUTDOWN_ALL
);
3683 let () = Glut.displayFunc
display in
3684 let () = Glut.reshapeFunc
reshape in
3685 let () = Glut.keyboardFunc
keyboard in
3686 let () = Glut.specialFunc
special in
3687 let () = Glut.idleFunc
(Some
idle) in
3688 let () = Glut.mouseFunc
mouse in
3689 let () = Glut.motionFunc
motion in
3690 let () = Glut.passiveMotionFunc
pmotion in
3693 conf.angle
, conf.proportional
, conf.texcount
,
3694 conf.sliceheight
, conf.uifont
3696 state.csock <- csock;
3697 state.ssock <- ssock;
3698 state.text <- "Opening " ^
state.path;
3699 writeopen state.path state.password
;
3703 let rec handlelablglutbug () =
3706 with Glut.BadEnum
"key in special_of_int" ->
3707 showtext '
!'
" LablGlut bug: special key not recognized";
3708 handlelablglutbug ()
3710 handlelablglutbug ();