4 | Ulinkgoto
of (int * int)
6 and facename
= string;;
8 let dolog fmt
= Printf.kprintf prerr_endline fmt
;;
10 type params
= angle
* proportional
* texcount
* sliceheight
* fontpath
19 and proportional
= bool
20 and interpagespace
= int
28 external init
: Unix.file_descr
-> params
-> unit = "ml_init";;
29 external draw
: (int * int * int * int * bool) -> string -> unit = "ml_draw";;
30 external seltext
: string -> (int * int * int * int) -> int -> unit =
32 external copysel
: string -> unit = "ml_copysel";;
33 external getpdimrect
: int -> float array
= "ml_getpdimrect";;
34 external whatsunder
: string -> int -> int -> under
= "ml_whatsunder";;
35 external zoomforh
: int -> int -> int -> float = "ml_zoom_for_height";;
36 external drawstr
: int -> int -> int -> string -> float = "ml_draw_string";;
37 external measurestr
: int -> string -> float = "ml_measure_string";;
41 | Msel
of (mpos
* mpos
)
44 | Mzoom
of (int * int)
48 type textentry
= string * string * onhist
option * onkey
* ondone
49 and onkey
= string -> int -> te
50 and ondone
= string -> unit
51 and histcancel
= unit -> unit
52 and onhist
= ((histcmd
-> string) * histcancel
)
53 and histcmd
= HCnext
| HCprev
| HCfirst
| HClast
58 | TEswitch
of textentry
70 { store
= Array.create n v
77 let cbcap b
= Array.length b
.store
;;
82 b
.wc
<- (b
.wc
+ 1) mod cap;
84 b
.len
<- min
(b
.len
+ 1) cap;
87 let cbempty b
= b
.len
= 0;;
89 let cbgetg b circular dir
=
93 let rc = b
.rc + dir
in
105 else max
0 (min
rc (b
.len
-1))
111 let cbget b
= cbgetg b
false;;
112 let cbgetc b
= cbgetg b
true;;
115 let rc = b
.wc
- b
.len
in
116 let rc = if rc < 0 then cbcap b
+ rc else rc in
120 let cbdecr b
= b
.len
<- b
.len
- 1;;
135 { mutable scrollbw
: int
136 ; mutable scrollh
: int
137 ; mutable icase
: bool
138 ; mutable preload
: bool
139 ; mutable pagebias
: int
140 ; mutable verbose
: bool
141 ; mutable scrollstep
: int
142 ; mutable maxhfit
: bool
143 ; mutable crophack
: bool
144 ; mutable autoscrollstep
: int
145 ; mutable showall
: bool
146 ; mutable hlinks
: bool
147 ; mutable underinfo
: bool
148 ; mutable interpagespace
: interpagespace
149 ; mutable zoom
: float
150 ; mutable presentation
: bool
151 ; mutable angle
: angle
154 ; mutable savebmarks
: bool
155 ; mutable proportional
: proportional
156 ; mutable memlimit
: int
157 ; mutable texcount
: texcount
158 ; mutable sliceheight
: sliceheight
159 ; mutable thumbw
: width
160 ; mutable jumpback
: bool
161 ; mutable bgcolor
: float * float * float
162 ; mutable bedefault
: bool
163 ; mutable scrollbarinpm
: bool
164 ; mutable uifont
: string
168 type anchor
= pageno
* top
;;
170 type outline
= string * int * anchor
172 | Oarray
of outline array
173 | Olist
of outline list
174 | Onarrow
of string * outline array
* outline array
177 type rect
= float * float * float * float * float * float * float * float;;
179 type pagemapkey
= pageno
* width
* angle
* proportional
* gen
;;
181 let emptyanchor = (0, 0.0);;
182 let initialanchor = (-1, nan
);;
185 | Birdseye
of (conf
* leftx
* pageno
* pageno
* anchor
)
186 | Outline
of (bool * int * int * outline array
* string * int * mode
)
187 | Items
of (int * int * item array
* string * int * mode
)
188 | Textentry
of (textentry
* onleave
)
190 and onleave
= leavetextentrystatus
-> unit
191 and leavetextentrystatus
= | Cancel
| Confirm
192 and item
= string * int * action
195 | Action
of (int -> int -> string -> int -> mode
)
198 let isbirdseye = function Birdseye _
-> true | _
-> false;;
199 let istextentry = function Textentry _
-> true | _
-> false;;
202 { mutable csock
: Unix.file_descr
203 ; mutable ssock
: Unix.file_descr
207 ; mutable scrollw
: int
208 ; mutable anchor
: anchor
210 ; mutable layout
: layout list
211 ; pagemap
: (pagemapkey
, (opaque
* pixmapsize
)) Hashtbl.t
212 ; mutable pdims
: (pageno
* width
* height
* leftx
) list
213 ; mutable pagecount
: int
214 ; pagecache
: string circbuf
215 ; mutable rendering
: bool
216 ; mutable mstate
: mstate
217 ; mutable searchpattern
: string
218 ; mutable rects
: (pageno
* recttype
* rect
) list
219 ; mutable rects1
: (pageno
* recttype
* rect
) list
220 ; mutable text
: string
221 ; mutable fullscreen
: (width
* height
) option
222 ; mutable mode
: mode
223 ; mutable outlines
: outlines
224 ; mutable bookmarks
: outline list
225 ; mutable path
: string
226 ; mutable password
: string
227 ; mutable invalidated
: int
228 ; mutable colorscale
: float
229 ; mutable memused
: int
231 ; mutable throttle
: layout list
option
232 ; mutable ascrollstep
: int
233 ; mutable help
: item array
234 ; mutable docinfo
: (int * string) list
238 { pat
: string circbuf
239 ; pag
: string circbuf
240 ; nav
: anchor circbuf
254 ; autoscrollstep
= 24
260 ; presentation
= false
265 ; proportional
= true
266 ; memlimit
= 32*1024*1024
271 ; bgcolor
= (0.5, 0.5, 0.5)
273 ; scrollbarinpm
= true
278 let conf = { defconf with angle
= defconf.angle
};;
281 let strings = ("llpp version " ^
Help.version
) :: "" :: Help.keys
in
282 Array.of_list
(List.map
(fun s
-> s
, 0, Noaction
) strings);
292 ; anchor
= initialanchor
295 ; pagemap
= Hashtbl.create
10
296 ; pagecache
= cbnew 100 ""
307 ; outlines
= Olist
[]
313 { nav
= cbnew 100 (0, 0.0)
330 Printf.kprintf prerr_endline fmt
332 Printf.kprintf ignore fmt
336 let len = String.length s
in
338 let b = Buffer.create
n in
339 Buffer.add_char
b (Char.chr
((len lsr 24) land 0xff));
340 Buffer.add_char
b (Char.chr
((len lsr 16) land 0xff));
341 Buffer.add_char
b (Char.chr
((len lsr 8) land 0xff));
342 Buffer.add_char
b (Char.chr
((len lsr 0) land 0xff));
343 Buffer.add_string
b s
;
344 let s'
= Buffer.contents
b in
345 let n'
= Unix.write fd
s'
0 n in
346 if n'
!= n then failwith
"write failed";
351 let n = Unix.read fd
s 0 4 in
352 if n != 4 then failwith
"incomplete read(len)";
354 lor (Char.code
s.[0] lsl 24)
355 lor (Char.code
s.[1] lsl 16)
356 lor (Char.code
s.[2] lsl 8)
357 lor (Char.code
s.[3] lsl 0)
359 let s = String.create
len in
360 let n = Unix.read fd
s 0 len in
361 if n != len then failwith
"incomplete read(data)";
366 let b = Buffer.create
10 in
367 Buffer.add_string
b s;
368 let rec combine = function
371 Buffer.add_char
b ' '
;
374 | `
b b -> if b then "1" else "0"
376 | `i i
-> string_of_int i
377 | `f f
-> string_of_float f
378 | `I f
-> string_of_int
(truncate f
)
380 Buffer.add_string
b s;
387 let cmd = Buffer.contents
(makecmd s l
) in
388 writecmd state.csock
cmd;
394 let d = conf.winh
- h
in
401 let rec f pn ph pi fh l
=
403 | (n, _
, h
, _
) :: rest
->
404 let ips = calcips h
in
409 if isbirdseye state.mode
&& pn
= 0
414 let fh = fh + ((n - pn
) * (ph
+ pi
)) in
419 if conf.presentation
|| (isbirdseye state.mode
&& pn
= 0)
423 let fh = fh + ((state.pagecount
- pn
) * (ph
+ pi
)) + inc in
426 let fh = f 0 0 0 0 state.pdims
in
430 let getpageyh pageno
=
431 let rec f pn ph pi y l
=
433 | (n, _
, h
, _
) :: rest
->
434 let ips = calcips h
in
437 let h = if n = pageno
then h else ph
in
438 if conf.presentation
&& n = pageno
440 y
+ (pageno
- pn
) * (ph
+ pi
) + pi
, h
442 y
+ (pageno
- pn
) * (ph
+ pi
), h
444 let y = y + (if conf.presentation
then pi
else 0) in
445 let y = y + (n - pn
) * (ph
+ pi
) in
449 y + (pageno
- pn
) * (ph
+ pi
), ph
451 f 0 0 0 0 state.pdims
454 let getpagey pageno
= fst
(getpageyh pageno
);;
457 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu
=
458 let ((w
, h, ips, x
) as curr
), rest
, pdimno
, yinc
=
460 | (pageno'
, w
, h, x
) :: rest
when pageno'
= pageno
->
461 let ips = calcips h in
463 if conf.presentation
|| (isbirdseye state.mode
&& pageno
= 0)
467 (w
, h, ips, x
), rest
, pdimno
+ 1, yinc
469 prev
, pdims
, pdimno
, 0
471 let dy = dy + yinc in
472 let py = py + yinc in
473 if pageno
= state.pagecount
|| cacheleft
= 0 || dy >= sh
478 if py + h <= vy - yinc
480 let py = py + h + ips in
481 let dy = max
0 (py - y) in
491 let pagey = vy - py in
492 let pagevh = h - pagey in
493 let pagevh = min
(sh
- dy) pagevh in
494 let off = if yinc > 0 then py - vy else 0 in
495 let py = py + h + ips in
501 ; pagedispy
= dy + off
502 ; pagey = pagey + off
503 ; pagevh = pagevh - off
507 let accu = e :: accu in
514 ~cacheleft
:(cacheleft
-1)
517 if state.invalidated
= 0
527 ~cacheleft
:(cbcap state.pagecache
)
537 let y = state.y + incr
in
539 let y = min
y (state.maxy
- (if conf.maxhfit
then conf.winh
else 0)) in
543 let getopaque pageno
=
544 try Some
(Hashtbl.find
state.pagemap
545 (pageno
, state.w
, conf.angle
, conf.proportional
, state.gen
))
546 with Not_found
-> None
549 let cache pageno opaque
=
550 Hashtbl.replace
state.pagemap
551 (pageno
, state.w
, conf.angle
, conf.proportional
, state.gen
) opaque
554 let validopaque opaque
= String.length opaque
> 0;;
557 match getopaque l
.pageno
with
558 | None
when not
state.rendering
->
559 state.rendering
<- true;
560 cache l
.pageno
("", -1);
561 wcmd "render" [`i
(l
.pageno
+ 1)
568 let loadlayout layout =
569 let rec f all
= function
571 begin match getopaque l
.pageno
with
572 | None
-> render l
; f false ls
573 | Some
(opaque
, _
) -> f (all
&& validopaque opaque
) ls
577 f (layout <> []) layout;
580 let findpageforopaque opaque
=
582 (fun k
(v
, s) a
-> if v
= opaque
then Some
(k
, s) else a
)
586 let pagevisible layout n = List.exists
(fun l
-> l
.pageno
= n) layout;;
592 let memleft = conf.memlimit
- state.memused
in
595 let opaque = cbpeek state.pagecache
in
596 match findpageforopaque opaque with
597 | Some
((n, _
, _
, _
, _
), size
) ->
598 memleft + size
>= 0 && not
(pagevisible state.layout n)
605 let presentation = conf.presentation in
606 let interpagespace = conf.interpagespace in
607 let maxy = state.maxy in
608 conf.presentation <- false;
609 conf.interpagespace <- 0;
610 state.maxy <- calcheight ();
612 match state.layout with
614 | l
:: _
-> getpagey l
.pageno
+ l
.pagey
616 let y = if y < conf.winh
then 0 else y - conf.winh
in
617 let pages = layout y (conf.winh
*3) in
618 List.iter
render pages;
619 conf.presentation <- presentation;
620 conf.interpagespace <- interpagespace;
626 let y = min
state.maxy y in
627 let pages = layout y conf.winh
in
628 let ready = loadlayout pages in
634 state.layout <- pages;
635 state.throttle
<- None
;
636 Glut.postRedisplay
();
639 state.throttle
<- Some
pages;
644 state.layout <- pages;
645 state.throttle
<- None
;
646 Glut.postRedisplay
();
648 begin match state.mode
with
649 | Birdseye
(conf, leftx
, pageno
, hooverpageno
, anchor
) ->
650 if not
(pagevisible pages pageno
)
652 match state.layout with
655 state.mode
<- Birdseye
(conf, leftx
, l
.pageno
, hooverpageno
, anchor
)
662 let gotoy_and_clear_text y =
664 if not
conf.verbose
then state.text
<- "";
668 match state.layout with
670 | l
:: _
-> (l
.pageno
, float l
.pagey /. float l
.pageh
)
673 let getanchory (n, top
) =
674 let y, h = getpageyh n in
675 y + (truncate
(top
*. float h));
678 let gotoanchor anchor
=
679 gotoy (getanchory anchor
);
683 cbput state.hists
.nav
(getanchor ());
687 let anchor = cbgetc state.hists
.nav dir
in
692 let y, h = getpageyh n in
693 gotoy_and_clear_text (y + (truncate
(top
*. float h)));
696 let gotopage1 n top
=
697 let y = getpagey n in
698 gotoy_and_clear_text (y + top
);
706 state.invalidated
<- state.invalidated
+ 1;
710 let c = c *. state.colorscale
in
714 let scalecolor2 (r
, g
, b) =
715 (r
*. state.colorscale
, g
*. state.colorscale
, b *. state.colorscale
);
719 state.maxy <- calcheight ();
720 match state.mode
with
721 | Birdseye
(_
, _
, pageno
, _
, _
) ->
722 let y, h = getpageyh pageno
in
723 let top = (conf.winh
- h) / 2 in
724 gotoy (max
0 (y - top))
725 | _
-> gotoanchor state.anchor
729 GlMat.mode `projection
;
730 GlMat.load_identity
();
731 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
732 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
733 GlMat.scale3
(2.0 /. float state.w
, 2.0 /. float conf.winh
, 1.0);
736 GlMat.translate ~x
:(float state.x
) ();
741 GlMat.mode `projection
;
742 GlMat.load_identity
();
743 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
744 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
745 GlMat.scale3
(2.0 /. float conf.winw
, 2.0 /. float conf.winh
, 1.0);
749 if state.invalidated
= 0 && state.anchor == initialanchor
750 then state.anchor <- getanchor ();
753 let w = truncate
(float w *. conf.zoom
) - state.scrollw
in
757 GlMat.mode `modelview
;
758 GlMat.load_identity
();
759 GlClear.color
(scalecolor 1.0);
760 GlClear.clear
[`color
];
763 wcmd "geometry" [`i
w; `i
h];
766 let drawstring size x
y s =
768 Gl.enable `texture_2d
;
769 ignore
(drawstr size x
y s);
771 Gl.disable `texture_2d
;
774 let drawstring1 size x
y s =
780 GlDraw.color
(0.0, 0.0, 0.0);
782 (0.0, float (conf.winh
- 18))
783 (float (conf.winw
- state.scrollw
- 1), float conf.winh
)
785 GlDraw.color
(1.0, 1.0, 1.0);
786 drawstring 14 8 (conf.winh
- 5) s;
788 let len = String.length
state.text
in
789 match state.mode
with
790 | Textentry
((prefix
, text
, _
, _
, _
), _
) ->
792 match String.length prefix
with
796 Printf.sprintf
"%s%s\226–� [%s]" prefix text
state.text
798 Printf.sprintf
"%s%s\226–�" prefix text
803 Printf.sprintf
"%s: %s\226–� [%s]" prefix text
state.text
805 Printf.sprintf
"%s: %s\226–�" prefix text
810 if len > 0 then drawstring state.text
814 state.text
<- Printf.sprintf
"%c%s" c s;
815 Glut.postRedisplay
();
824 state.rects
<- state.rects1
;
825 Glut.postRedisplay
()
828 let n = Scanf.sscanf
cmd "C %u" (fun n -> n) in
829 state.pagecount
<- n;
830 state.invalidated
<- state.invalidated
- 1;
831 if state.invalidated
= 0
835 let s = Scanf.sscanf
cmd "t %n"
836 (fun n -> String.sub
cmd n (String.length
cmd - n))
838 Glut.setWindowTitle
s
841 let s = Scanf.sscanf
cmd "T %n"
842 (fun n -> String.sub
cmd n (String.length
cmd - n))
844 if istextentry state.mode
851 Glut.postRedisplay
();
857 let s = Scanf.sscanf
cmd "V %n"
858 (fun n -> String.sub
cmd n (String.length
cmd - n))
864 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
865 Scanf.sscanf
cmd "F %u %d %f %f %f %f %f %f %f %f"
866 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
867 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
869 let y = (getpagey pageno) + truncate y0
in
872 state.rects1
<- [pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)]
875 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
876 Scanf.sscanf
cmd "R %u %d %f %f %f %f %f %f %f %f"
877 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
878 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
881 (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) :: state.rects1
884 let n, w, _h
, r
, l
, s, p
=
885 Scanf.sscanf
cmd "r %u %u %u %d %d %u %s"
886 (fun n w h r l
s p
->
887 (n-1, w, h, r
, l
!= 0, s, p
))
890 Hashtbl.replace
state.pagemap
(n, w, r
, l
, state.gen
) (p
, s);
891 state.memused
<- state.memused
+ s;
894 match state.throttle
with
895 | None
-> state.layout
896 | Some
layout -> layout
900 if (state.memused
<= conf.memlimit
) || cbempty state.pagecache
903 let evictedopaque = cbpeek state.pagecache
in
904 match findpageforopaque evictedopaque with
905 | None
-> failwith
"bug in gc"
906 | Some
((evictedn
, _
, _
, _
, gen
) as k
, evictedsize
) ->
907 if state.gen
!= gen
|| not
(pagevisible layout evictedn
)
909 wcmd "free" [`
s evictedopaque];
910 state.memused
<- state.memused
- evictedsize
;
911 Hashtbl.remove
state.pagemap k
;
912 cbdecr state.pagecache
;
919 cbput state.pagecache p
;
920 state.rendering
<- false;
922 begin match state.throttle
with
924 if pagevisible state.layout n
927 let allvisible = loadlayout state.layout in
928 if allvisible then preload ();
935 let y = getpagey l
.pageno + l
.pagey in
941 Scanf.sscanf
cmd "l %u %u %u %u" (fun n w h x
-> n, w, h, x
)
943 state.pdims
<- pdim :: state.pdims
946 let (l
, n, t
, h, pos
) =
947 Scanf.sscanf
cmd "o %u %u %d %u %n" (fun l
n t
h pos
-> l
, n, t
, h, pos
)
949 let s = String.sub
cmd pos
(String.length
cmd - pos
) in
950 let outline = (s, l
, (n, float t
/. float h)) in
952 match state.outlines with
953 | Olist
outlines -> Olist
(outline :: outlines)
954 | Oarray _
-> Olist
[outline]
955 | Onarrow _
-> Olist
[outline]
957 state.outlines <- outlines
961 let s = Scanf.sscanf
cmd "i %n"
962 (fun n -> String.sub
cmd n (String.length
cmd - n))
964 let len = String.length
s in
965 let rec fold accu pos
=
967 try String.index_from
s pos '
\n'
with Not_found
-> len
972 let line = String.sub
s pos
(eolpos - pos
) in
973 fold ((1, line)::accu) (eolpos+1)
975 state.docinfo
<- fold state.docinfo
0
978 dolog "unknown cmd `%S'" cmd
981 let now = Unix.gettimeofday
;;
985 let r, _
, _
= Unix.select
[state.csock
] [] [] delay
in
988 if state.ascrollstep
> 0
990 let y = state.y + state.ascrollstep
in
991 let y = if y >= state.maxy then 0 else y in
997 let cmd = readcmd state.csock
in
1006 let action = function
1007 | HCprev
-> cbget cb ~
-1
1008 | HCnext
-> cbget cb
1
1009 | HCfirst
-> cbget cb ~
-(cb
.rc)
1010 | HClast
-> cbget cb
(cb
.len - 1 - cb
.rc)
1011 and cancel
() = cb
.rc <- rc
1015 let search pattern forward
=
1016 if String.length pattern
> 0
1019 match state.layout with
1022 l
.pageno, (l
.pagey + if forward
then 0 else 0*l
.pagevh)
1025 let b = makecmd "search"
1026 [`
b conf.icase
; `i
pn; `i
py; `i
(if forward
then 1 else 0)]
1028 Buffer.add_char
b '
,'
;
1029 Buffer.add_string
b pattern
;
1030 Buffer.add_char
b '
\000'
;
1033 writecmd state.csock
cmd;
1036 let intentry text key
=
1037 let c = Char.unsafe_chr key
in
1040 let s = "x" in s.[0] <- c;
1041 let text = text ^
s in
1045 state.text <- Printf.sprintf
"invalid char (%d, `%c')" key
c;
1050 let b = Buffer.create
(String.length
s + 1) in
1051 Buffer.add_string
b s;
1052 Buffer.add_char
b c;
1056 let textentry text key
=
1057 let c = Char.unsafe_chr key
in
1059 | _
when key
>= 32 && key
< 127 ->
1060 let text = addchar text c in
1064 dolog "unhandled key %d char `%c'" key
(Char.unsafe_chr key
);
1068 let reinit angle proportional
=
1069 conf.angle
<- angle
;
1070 conf.proportional
<- proportional
;
1072 wcmd "reinit" [`i angle
; `
b proportional
];
1076 let zoom = max
0.01 (min
2.2 zoom) in
1077 if zoom <> conf.zoom
1082 if state.invalidated
= 0
1084 state.anchor <- getanchor ()
1086 reshape conf.winw
conf.winh
;
1087 state.text <- Printf.sprintf
"zoom is now %-5.1f" (zoom *. 100.0);
1091 let enterbirdseye () =
1092 let zoom = float conf.thumbw
/. float conf.winw
in
1093 let birdseyepageno =
1094 let cy = conf.winh
/ 2 in
1098 let rec fold best
= function
1101 let d = cy - (l
.pagedispy
+ l
.pagevh/2)
1102 and dbest
= cy - (best
.pagedispy
+ best
.pagevh/2) in
1103 if abs
d < abs dbest
1110 state.mode
<- Birdseye
(
1111 { conf with zoom = conf.zoom }, state.x
, birdseyepageno, -1, getanchor ()
1114 conf.presentation <- false;
1115 conf.interpagespace <- 10;
1116 conf.hlinks
<- false;
1118 state.mstate
<- Mnone
;
1119 conf.showall
<- false;
1120 Glut.setCursor
Glut.CURSOR_INHERIT
;
1123 state.text <- Printf.sprintf
"birds eye mode on (zoom %3.1f%%)"
1128 reshape conf.winw
conf.winh
;
1131 let leavebirdseye (c, leftx
, pageno, _
, anchor) goback
=
1133 conf.zoom <- c.zoom;
1134 conf.presentation <- c.presentation;
1135 conf.interpagespace <- c.interpagespace;
1136 conf.showall
<- c.showall
;
1137 conf.hlinks
<- c.hlinks
;
1141 state.text <- Printf.sprintf
"birds eye mode off (zoom %3.1f%%)"
1144 reshape conf.winw
conf.winh
;
1145 state.anchor <- if goback
then anchor else (pageno, 0.0);
1148 let togglebirdseye () =
1149 match state.mode
with
1150 | Birdseye vals
-> leavebirdseye vals
true
1151 | View
| Outline _
-> enterbirdseye ()
1155 let upbirdseye (conf, leftx
, pageno, hooverpageno
, anchor) =
1156 let pageno = max
0 (pageno - 1) in
1157 let rec loop = function
1158 | [] -> gotopage1 pageno 0
1159 | l
:: _
when l
.pageno = pageno ->
1160 if l
.pagedispy
>= 0 && l
.pagey = 0
1161 then Glut.postRedisplay
()
1162 else gotopage1 pageno 0
1163 | _
:: rest
-> loop rest
1166 state.mode
<- Birdseye
(conf, leftx
, pageno, hooverpageno
, anchor)
1169 let downbirdseye (conf, leftx
, pageno, hooverpageno
, anchor) =
1170 let pageno = min
(state.pagecount
- 1) (pageno + 1) in
1171 state.mode
<- Birdseye
(conf, leftx
, pageno, hooverpageno
, anchor);
1172 let rec loop = function
1174 let y, h = getpageyh pageno in
1175 let dy = (y - state.y) - (conf.winh
- h - conf.interpagespace) in
1177 | l
:: _
when l
.pageno = pageno ->
1178 if l
.pagevh != l
.pageh
1179 then gotoy (clamp (l
.pageh
- l
.pagevh + conf.interpagespace))
1180 else Glut.postRedisplay
()
1181 | _
:: rest
-> loop rest
1186 let optentry mode _ key
=
1187 let btos b = if b then "on" else "off" in
1188 let c = Char.unsafe_chr key
in
1192 try conf.scrollstep
<- int_of_string
s with exc
->
1193 state.text <- Printf.sprintf
"bad integer `%s': %s"
1194 s (Printexc.to_string exc
)
1196 TEswitch
("scroll step", "", None
, intentry, ondone)
1201 conf.autoscrollstep
<- int_of_string
s;
1202 if state.ascrollstep
> 0
1203 then state.ascrollstep
<- conf.autoscrollstep
;
1205 state.text <- Printf.sprintf
"bad integer `%s': %s"
1206 s (Printexc.to_string exc
)
1208 TEswitch
("auto scroll step", "", None
, intentry, ondone)
1213 let zoom = float (int_of_string
s) /. 100.0 in
1216 state.text <- Printf.sprintf
"bad integer `%s': %s"
1217 s (Printexc.to_string exc
)
1219 TEswitch
("zoom", "", None
, intentry, ondone)
1224 conf.thumbw
<- max
2 (min
1920 (int_of_string
s));
1226 Printf.sprintf
"thumbnail width is set to %d" conf.thumbw
;
1227 begin match mode
with
1229 leavebirdseye beye
false;
1234 state.text <- Printf.sprintf
"bad integer `%s': %s"
1235 s (Printexc.to_string exc
)
1237 TEswitch
("thumbnail width", "", None
, intentry, ondone)
1242 Some
(int_of_string
s)
1244 state.text <- Printf.sprintf
"bad integer `%s': %s"
1245 s (Printexc.to_string exc
);
1248 | Some angle
-> reinit angle
conf.proportional
1251 TEswitch
("rotation", "", None
, intentry, ondone)
1254 conf.icase
<- not
conf.icase
;
1255 TEdone
("case insensitive search " ^
(btos conf.icase
))
1258 conf.preload <- not
conf.preload;
1260 TEdone
("preload " ^
(btos conf.preload))
1263 conf.verbose
<- not
conf.verbose
;
1264 TEdone
("verbose " ^
(btos conf.verbose
))
1267 conf.maxhfit
<- not
conf.maxhfit
;
1268 state.maxy <- state.maxy + (if conf.maxhfit
then -conf.winh
else conf.winh
);
1269 TEdone
("maxhfit " ^
(btos conf.maxhfit
))
1272 conf.crophack
<- not
conf.crophack
;
1273 TEdone
("crophack " ^
btos conf.crophack
)
1276 conf.showall
<- not
conf.showall
;
1277 TEdone
("throttle " ^
btos conf.showall
)
1280 conf.underinfo
<- not
conf.underinfo
;
1281 TEdone
("underinfo " ^
btos conf.underinfo
)
1284 conf.savebmarks
<- not
conf.savebmarks
;
1285 TEdone
("persistent bookmarks " ^
btos conf.savebmarks
)
1291 match state.layout with
1296 conf.interpagespace <- int_of_string
s;
1297 state.maxy <- calcheight ();
1298 let y = getpagey pageno in
1301 state.text <- Printf.sprintf
"bad integer `%s': %s"
1302 s (Printexc.to_string exc
)
1304 TEswitch
("vertical margin", "", None
, intentry, ondone)
1307 reinit conf.angle
(not
conf.proportional
);
1308 TEdone
("proprortional display " ^
btos conf.proportional
)
1311 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
1315 let maxoutlinerows () = (conf.winh
- 31) / 16;;
1317 let enterselector allowdel
outlines errmsg msg
=
1318 if Array.length
outlines = 0
1320 showtext ' ' errmsg
;
1324 Glut.setCursor
Glut.CURSOR_INHERIT
;
1326 match state.layout with
1328 | {pageno=pageno} :: _
-> pageno
1332 if n = Array.length
outlines
1335 let (_
, _
, (outlinepageno
, _
)) = outlines.(n) in
1336 if outlinepageno
>= pageno then n else loop (n+1)
1340 state.mode
<- Outline
1341 (allowdel
, active, max
0 (active - maxoutlinerows () / 2), outlines, "", 0,
1343 Glut.postRedisplay
();
1347 let enteroutlinemode () =
1349 match state.outlines with
1352 let a = Array.of_list
(List.rev l
) in
1353 state.outlines <- Oarray
a;
1355 | Onarrow
(pat
, a, _
) ->
1356 a, "Outline was narrowed to `" ^ pat ^
"' (Ctrl-u to restore)"
1358 enterselector false outlines "Document has no outline" msg
;
1361 let enterbookmarkmode () =
1362 let bookmarks = Array.of_list
state.bookmarks in
1363 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1366 let mode_to_string mode
=
1367 let b = Buffer.create
10 in
1368 let rec f = function
1369 | Textentry
(_
, _
) -> Buffer.add_string
b "Textentry ";
1370 | View
-> Buffer.add_string
b "View"
1371 | Birdseye _
-> Buffer.add_string
b "Birdseye"
1372 | Items _
-> Buffer.add_string
b "Items"
1373 | Outline _
-> Buffer.add_string
b "Outline"
1379 let color_of_string s =
1380 Scanf.sscanf
s "%d/%d/%d" (fun r g
b ->
1381 (float r /. 256.0, float g
/. 256.0, float b /. 256.0)
1385 let color_to_string (r, g
, b) =
1386 let r = truncate
(r *. 256.0)
1387 and g
= truncate
(g
*. 256.0)
1388 and b = truncate
(b *. 256.0) in
1389 Printf.sprintf
"%d/%d/%d" r g
b
1392 let enterinfomode () =
1393 let btos = function true -> "on" | _
-> "off" in
1394 let mode = state.mode in
1395 let rec makeitems () =
1396 let intp name get set
=
1397 Printf.sprintf
"%-24s %d" name
(get
()), 1, Action
(
1398 fun active first _ pan
->
1403 state.text <- Printf.sprintf
"bad integer `%s': %s"
1404 s (Printexc.to_string exn
);
1407 if n != max_int
then set
n;
1409 let te = name
, "", None
, intentry, ondone in
1414 state.mode <- Items
(active, first
, makeitems (), "", pan
, mode)
1417 and boolp name get set
=
1418 Printf.sprintf
"%-24s %s" name
(btos (get
())), 1, Action
(
1419 fun active first qsearch pan
->
1422 Items
(active, first
, makeitems (), qsearch
, pan
, mode);
1424 and colorp name get set
=
1425 Printf.sprintf
"%-24s %s" name
(color_to_string (get
())), 1, Action
(
1426 fun active first _ pan
->
1427 let invalid = (nan
, nan
, nan
) in
1430 try color_of_string s
1432 state.text <- Printf.sprintf
"bad color `%s': %s"
1433 s (Printexc.to_string exn
);
1439 let te = name
, "", None
, textentry, ondone in
1444 state.mode <- Items
(active, first
, makeitems (), "", pan
, mode)
1449 let birdseye = isbirdseye mode in
1451 (if birdseye then "Setup bird's eye" else "Setup"), 0, Noaction
;
1453 boolp
"presentation"
1454 (fun () -> conf.presentation)
1456 conf.presentation <- v;
1457 state.anchor <- getanchor ();
1460 boolp
"ignore case in searches"
1461 (fun () -> conf.icase
)
1462 (fun v -> conf.icase
<- v);
1465 (fun () -> conf.preload)
1466 (fun v -> conf.preload <- v);
1469 (fun () -> conf.verbose
)
1470 (fun v -> conf.verbose
<- v);
1473 (fun () -> conf.maxhfit
)
1474 (fun v -> conf.maxhfit
<- v);
1477 (fun () -> conf.crophack
)
1478 (fun v -> conf.crophack
<- v);
1481 (fun () -> conf.showall
)
1482 (fun v -> conf.showall
<- v);
1484 boolp
"highlight links"
1485 (fun () -> conf.hlinks
)
1486 (fun v -> conf.hlinks
<- v);
1489 (fun () -> conf.underinfo
)
1490 (fun v -> conf.underinfo
<- v);
1491 boolp
"persistent bookmarks"
1492 (fun () -> conf.savebmarks
)
1493 (fun v -> conf.savebmarks
<- v);
1495 boolp
"proportional display"
1496 (fun () -> conf.proportional
)
1497 (fun v -> reinit conf.angle
v);
1499 boolp
"persistent location"
1500 (fun () -> conf.jumpback
)
1501 (fun v -> conf.jumpback
<- v);
1505 intp "vertical margin"
1506 (fun () -> conf.interpagespace)
1508 conf.interpagespace <- n;
1510 match state.layout with
1515 state.maxy <- calcheight ();
1516 let y = getpagey pageno in
1521 (fun () -> conf.pagebias
)
1522 (fun v -> conf.pagebias
<- v);
1525 (fun () -> conf.scrollstep
)
1526 (fun n -> conf.scrollstep
<- n);
1528 intp "auto scroll step"
1530 if state.ascrollstep
> 0
1531 then state.ascrollstep
1532 else conf.autoscrollstep
)
1534 if state.ascrollstep
> 0
1535 then state.ascrollstep
<- n
1536 else conf.autoscrollstep
<- n);
1539 (fun () -> truncate
(conf.zoom *. 100.))
1540 (fun v -> setzoom ((float v) /. 100.));
1543 (fun () -> conf.angle
)
1544 (fun v -> reinit v conf.proportional
);
1546 intp "scroll bar width"
1547 (fun () -> state.scrollw
)
1551 reshape conf.winw
conf.winh
;
1554 intp "scroll handle height"
1555 (fun () -> conf.scrollh
)
1556 (fun v -> conf.scrollh
<- v;);
1558 intp "thumbnail width"
1559 (fun () -> conf.thumbw
)
1561 conf.thumbw
<- min
1920 v;
1564 leavebirdseye beye
false;
1569 colorp
"background color"
1570 (fun () -> conf.bgcolor
)
1571 (fun v -> conf.bgcolor
<- v);
1574 "Presentation mode", 0, Noaction
;
1576 boolp
"scrollbar visible"
1577 (fun () -> conf.scrollbarinpm
)
1579 if v != conf.scrollbarinpm
1581 conf.scrollbarinpm
<- v;
1582 if conf.presentation
1584 state.scrollw
<- if v then conf.scrollbw
else 0;
1585 reshape conf.winw
conf.winh
;
1591 "Pixmap Cache", 0, Noaction
;
1593 intp "size (advisory)"
1594 (fun () -> conf.memlimit
)
1595 (fun v -> conf.memlimit
<- v);
1596 Printf.sprintf
"%-24s %d" "used" state.memused
, 1, Noaction
;
1603 :: ("Document", 0, Noaction
)
1604 :: List.map
(fun (_
, s) -> (s, 1, Noaction
)) state.docinfo
1610 :: (Printf.sprintf
"Save these parameters as defaults at exit (%s)"
1611 (btos conf.bedefault
),
1614 fun active first qsearch pan
->
1615 conf.bedefault
<- not
conf.bedefault
;
1616 Items
(active, first
, makeitems (), qsearch
, pan
, mode);
1621 Array.of_list
(items @ tailer)
1624 state.mode <- Items
(1, 0, makeitems (), "", 0, mode);
1625 Glut.postRedisplay
();
1628 let enterhelpmode () =
1629 state.mode <- Items
(-1, 0, state.help
, "", 0, state.mode);
1630 Glut.postRedisplay
();
1633 let quickbookmark ?title
() =
1634 match state.layout with
1640 let sec = Unix.gettimeofday
() in
1641 let tm = Unix.localtime
sec in
1642 Printf.sprintf
"Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1646 (tm.Unix.tm_year
+ 1900)
1649 | Some
title -> title
1652 (title, 0, (l
.pageno, float l
.pagey /. float l
.pageh
))
1657 state.fullscreen
<- None
;
1658 Glut.reshapeWindow
w h;
1661 let writeopen path password
=
1662 writecmd state.csock
("open " ^ path ^
"\000" ^ password ^
"\000");
1663 writecmd state.csock
"info";
1666 let opendoc path password
=
1669 state.password
<- password
;
1670 state.gen
<- state.gen
+ 1;
1672 writeopen path password
;
1673 Glut.setWindowTitle
("llpp " ^
Filename.basename path
);
1674 wcmd "geometry" [`i
state.w; `i
conf.winh
];
1677 let viewkeyboard key
=
1679 let mode = state.mode in
1680 state.mode <- Textentry
(te, fun _
-> state.mode <- mode);
1683 Glut.postRedisplay
()
1685 let c = Char.chr key
in
1687 | '
\027'
| 'q'
-> (* escape *)
1690 | '
\008'
-> (* backspace *)
1691 let y = getnav ~
-1 in
1692 gotoy_and_clear_text y
1700 Glut.postRedisplay
()
1703 let ondone isforw
s =
1704 cbput state.hists
.pat
s;
1705 state.searchpattern
<- s;
1708 let s = String.create
1 in
1710 enttext (s, "", Some
(onhist state.hists
.pat
),
1711 textentry, ondone (c ='
/'
))
1713 | '
+'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1714 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1715 setzoom (min
2.2 (conf.zoom +. incr))
1720 try int_of_string
s with exc
->
1721 state.text <- Printf.sprintf
"bad integer `%s': %s"
1722 s (Printexc.to_string exc
);
1728 state.text <- "page bias is now " ^ string_of_int
n;
1731 enttext ("page bias", "", None
, intentry, ondone)
1733 | '
-'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1734 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1735 setzoom (max
0.01 (conf.zoom -. decr))
1738 let ondone msg
= state.text <- msg
in
1740 "option [acfhilpstvAPRSZ]", "", None
,
1741 optentry state.mode, ondone
1744 | '
0'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1747 | '
1'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1748 let zoom = zoomforh
conf.winw
conf.winh
state.scrollw
in
1752 | '
9'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1758 try int_of_string
s with exc
->
1759 state.text <- Printf.sprintf
"bad integer `%s': %s"
1760 s (Printexc.to_string exc
);
1766 cbput state.hists
.pag
(string_of_int
n);
1767 gotoy_and_clear_text (getpagey (n + conf.pagebias
- 1))
1770 let pageentry text key
=
1771 match Char.unsafe_chr key
with
1772 | 'g'
-> TEdone
text
1773 | _
-> intentry text key
1775 let text = "x" in text.[0] <- c;
1776 enttext (":", text, Some
(onhist state.hists
.pag
), pageentry, ondone)
1779 state.scrollw
<- if state.scrollw
> 0 then 0 else conf.scrollbw
;
1780 reshape conf.winw
conf.winh
;
1783 conf.hlinks
<- not
conf.hlinks
;
1784 state.text <- "highlightlinks " ^
if conf.hlinks
then "on" else "off";
1785 Glut.postRedisplay
()
1788 if state.ascrollstep
= 0
1789 then state.ascrollstep
<- conf.autoscrollstep
1791 conf.autoscrollstep
<- state.ascrollstep
;
1792 state.ascrollstep
<- 0;
1796 conf.presentation <- not
conf.presentation;
1797 if conf.presentation
1799 if not
conf.scrollbarinpm
1800 then state.scrollw
<- 0;
1803 state.scrollw
<- conf.scrollbw
;
1805 showtext ' '
("presentation mode " ^
1806 if conf.presentation then "on" else "off");
1807 state.anchor <- getanchor ();
1811 begin match state.fullscreen
with
1813 state.fullscreen
<- Some
(conf.winw
, conf.winh
);
1816 state.fullscreen
<- None
;
1821 gotoy_and_clear_text 0
1824 search state.searchpattern
true
1827 search state.searchpattern
false
1830 begin match state.layout with
1833 gotoy_and_clear_text (getpagey l
.pageno)
1837 begin match List.rev
state.layout with
1840 let pageno = min
(l
.pageno+1) (state.pagecount
-1) in
1841 gotoy_and_clear_text (getpagey pageno)
1844 | '
\127'
-> (* delte *)
1845 begin match state.layout with
1848 let pageno = max
0 (l
.pageno-1) in
1849 gotoy_and_clear_text (getpagey pageno)
1854 if fn
= -1 then l
.pageno, l
.pageno else fn
, l
.pageno
1856 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
1858 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
1862 else (100. *. (float state.y /. float maxy)) in
1865 Printf.sprintf
"Page %d of %d %.2f%%"
1866 (fn+1) state.pagecount
percent
1869 "Pages %d-%d of %d %.2f%%"
1870 (fn+1) (ln
+1) state.pagecount
percent
1875 begin match state.layout with
1878 doreshape (l
.pagew
+ state.scrollw
) l
.pageh
;
1879 Glut.postRedisplay
();
1883 enterbookmarkmode ()
1893 match state.layout with
1896 (s, 0, (l
.pageno, float l
.pagey /. float l
.pageh
))
1900 enttext ("bookmark", "", None
, textentry, ondone)
1904 showtext ' '
"Quick bookmark added";
1907 begin match state.layout with
1909 let rect = getpdimrect l
.pagedimno
in
1913 (truncate
(1.8 *. (rect.(1) -. rect.(0))),
1914 truncate
(1.2 *. (rect.(3) -. rect.(0))))
1916 (truncate
(rect.(1) -. rect.(0)),
1917 truncate
(rect.(3) -. rect.(0)))
1921 doreshape (w + state.scrollw
) (h + conf.interpagespace)
1923 Glut.postRedisplay
();
1929 reinit (conf.angle
+ (if c = '
>'
then 30 else -30)) conf.proportional
1934 (min
(state.colorscale
+. (if c = '
]'
then 0.1 else -0.1)) 1.0);
1935 Glut.postRedisplay
()
1938 begin match state.mode with
1939 | Birdseye beye
-> upbirdseye beye
1940 | _
-> gotoy (clamp (-conf.scrollstep
))
1944 begin match state.mode with
1945 | Birdseye beye
-> downbirdseye beye
1946 | _
-> gotoy (clamp conf.scrollstep
)
1950 state.anchor <- getanchor ();
1951 opendoc state.path
state.password
1954 vlog "huh? %d %c" key
(Char.chr key
);
1957 let textentrykeyboard key
((c, text, opthist
, onkey
, ondone), onleave
) =
1959 state.mode <- Textentry
(te, onleave
);
1962 Glut.postRedisplay
()
1964 match Char.unsafe_chr key
with
1965 | '
\008'
-> (* backspace *)
1966 let len = String.length
text in
1970 Glut.postRedisplay
();
1973 let s = String.sub
text 0 (len - 1) in
1974 enttext (c, s, opthist
, onkey
, ondone)
1980 Glut.postRedisplay
()
1982 | '
\027'
-> (* escape *)
1983 begin match opthist
with
1985 | Some
(_
, onhistcancel
) -> onhistcancel
()
1988 Glut.postRedisplay
()
1991 begin match onkey
text key
with
1995 Glut.postRedisplay
()
1998 enttext (c, text, opthist
, onkey
, ondone);
2002 Glut.postRedisplay
()
2005 state.mode <- Textentry
(te, onleave
);
2006 Glut.postRedisplay
()
2010 let birdseyekeyboard key
((_
, _
, pageno, _
, _
) as beye
) =
2012 | 27 -> (* escape *)
2013 leavebirdseye beye
true
2015 | 12 -> (* ctrl-l *)
2016 let y, h = getpageyh pageno in
2017 let top = (conf.winh
- h) / 2 in
2018 gotoy (max
0 (y - top))
2021 leavebirdseye beye
false
2027 let itemskeyboard key
(active, first
, items, qsearch
, pan
, oldmode
) =
2028 let set active first qsearch
=
2029 state.mode <- Items
(active, first
, items, qsearch
, pan
, oldmode
)
2031 let search active pattern
incr =
2034 if n = Array.length
items || n = -1
2037 let (s, _
, _
) = items.(n) in
2039 (try ignore
(Str.search_forward re
s 0); true
2040 with Not_found
-> false)
2042 else loop (n + incr)
2047 let re = Str.regexp_case_fold pattern
in
2053 let firstof active = max
0 (active - maxoutlinerows () / 2) in
2055 | 18 | 19 -> (* ctrl-r/ctlr-s *)
2056 let incr = if key
= 18 then -1 else 1 in
2058 match search (active + incr) qsearch
incr with
2060 state.text <- qsearch ^
" [not found]";
2063 state.text <- qsearch
;
2064 active, firstof active
2066 set active first qsearch
;
2067 Glut.postRedisplay
();
2069 | 8 -> (* backspace *)
2070 let len = String.length qsearch
in
2077 set active first
"";
2080 let qsearch = String.sub
qsearch 0 (len - 1) in
2082 match search active qsearch ~
-1 with
2084 state.text <- qsearch ^
" [not found]";
2087 state.text <- qsearch;
2088 active, firstof active
2090 set active first
qsearch
2092 Glut.postRedisplay
()
2094 | _
when key
>= 32 && key
< 127 ->
2095 let pattern = addchar qsearch (Char.chr key
) in
2097 match search active pattern 1 with
2099 state.text <- pattern ^
" [not found]";
2102 state.text <- pattern;
2103 active, firstof active
2105 set active first
pattern;
2106 Glut.postRedisplay
()
2108 | 27 -> (* escape *)
2110 if String.length
qsearch = 0
2112 state.mode <- oldmode
;
2115 set active first
"";
2117 Glut.postRedisplay
()
2120 if active >= 0 && active < Array.length
items
2122 match items.(active) with
2124 state.mode <- f active first
qsearch pan
2128 state.mode <- oldmode
2132 state.mode <- oldmode
2134 Glut.postRedisplay
();
2136 | _
-> dolog "unknown key %d" key
2139 let outlinekeyboard key
2140 (allowdel
, active, first
, outlines, qsearch, pan
, oldmode
) =
2141 let narrow outlines pattern =
2142 let reopt = try Some
(Str.regexp_case_fold
pattern) with _
-> None
in
2146 let rec fold accu n =
2150 let (s, _
, _
) as o
= outlines.(n) in
2152 if (try ignore
(Str.search_forward
re s 0); true
2153 with Not_found
-> false)
2159 let matched = fold [] (Array.length
outlines - 1) in
2160 if matched = [] then None
else Some
(Array.of_list
matched)
2162 let search active pattern incr =
2165 if n = Array.length
outlines || n = -1
2168 let (s, _
, _
) = outlines.(n) in
2170 (try ignore
(Str.search_forward
re s 0); true
2171 with Not_found
-> false)
2173 else loop (n + incr)
2178 let re = Str.regexp_case_fold
pattern in
2184 let firstof active = max
0 (active - maxoutlinerows () / 2) in
2186 | 27 -> (* escape *)
2188 if String.length
qsearch = 0
2190 state.mode <- oldmode
;
2193 state.mode <- Outline
(
2194 allowdel
, active, first
, outlines, "", pan
, oldmode
2197 Glut.postRedisplay
();
2199 | 18 | 19 -> (* ctrl-r/ctrl-s *)
2200 let incr = if key
= 18 then -1 else 1 in
2202 match search (active + incr) qsearch incr with
2204 state.text <- qsearch ^
" [not found]";
2207 state.text <- qsearch;
2208 active, firstof active
2210 state.mode <- Outline
(
2211 allowdel
, active, first
, outlines, qsearch, pan
, oldmode
2213 Glut.postRedisplay
();
2215 | 8 -> (* backspace *)
2216 let len = String.length
qsearch in
2223 state.mode <- Outline
(
2224 allowdel
, active, first
, outlines, "", pan
, oldmode
2228 let qsearch = String.sub
qsearch 0 (len - 1) in
2230 match search active qsearch ~
-1 with
2232 state.text <- qsearch ^
" [not found]";
2235 state.text <- qsearch;
2236 active, firstof active
2238 state.mode <- Outline
(
2239 allowdel
, active, first
, outlines, qsearch, pan
, oldmode
2242 Glut.postRedisplay
()
2245 if active < Array.length
outlines
2247 let (_
, _
, anchor) = outlines.(active) in
2252 if allowdel
then state.bookmarks <- Array.to_list
outlines;
2253 state.mode <- oldmode
;
2254 Glut.postRedisplay
();
2256 | _
when key
>= 32 && key
< 127 ->
2257 let pattern = addchar qsearch (Char.chr key
) in
2259 match search active pattern 1 with
2261 state.text <- pattern ^
" [not found]";
2264 state.text <- pattern;
2265 active, firstof active
2267 state.mode <- Outline
(
2268 allowdel
, active, first
, outlines, pattern, pan
, oldmode
2270 Glut.postRedisplay
()
2272 | 14 when not allowdel
-> (* ctrl-n *)
2273 if String.length
qsearch > 0
2275 let optoutlines = narrow outlines qsearch in
2276 begin match optoutlines with
2277 | None
-> state.text <- "can't narrow"
2279 state.mode <- Outline
(
2280 allowdel
, 0, 0, outlines, qsearch, pan
, oldmode
2282 match state.outlines with
2285 state.outlines <- Onarrow
(qsearch, outlines, a)
2286 | Onarrow
(_
, _
, b) ->
2287 state.outlines <- Onarrow
(qsearch, outlines, b)
2290 Glut.postRedisplay
()
2292 | 21 when not allowdel
-> (* ctrl-u *)
2294 match state.outlines with
2297 let a = Array.of_list
(List.rev l
) in
2298 state.outlines <- Oarray
a;
2300 | Onarrow
(_
, _
, b) ->
2301 state.outlines <- Oarray
b;
2305 state.mode <- Outline
(allowdel
, 0, 0, outline, qsearch, pan
, oldmode
);
2306 Glut.postRedisplay
()
2308 | 12 -> (* ctrl-l *)
2309 state.mode <- Outline
2310 (allowdel
, active, firstof active, outlines, qsearch, pan
, oldmode
);
2311 Glut.postRedisplay
()
2313 | 127 when allowdel
-> (* delete *)
2314 let len = Array.length
outlines - 1 in
2318 state.bookmarks <- [];
2321 let bookmarks = Array.init
len
2323 let i = if i >= active then i + 1 else i in
2337 Glut.postRedisplay
()
2339 | _
-> dolog "unknown key %d" key
2342 let keyboard ~key ~x ~
y =
2345 if key
= 7 (* ctrl-g *)
2349 match state.mode with
2350 | Outline
outline -> outlinekeyboard key
outline
2351 | Textentry
textentry -> textentrykeyboard key
textentry
2352 | Birdseye
birdseye -> birdseyekeyboard key
birdseye
2353 | View
-> viewkeyboard key
2354 | Items
items -> itemskeyboard key
items
2357 let birdseyespecial key
((conf, leftx
, _
, hooverpageno
, anchor) as beye
) =
2359 | Glut.KEY_UP
-> upbirdseye beye
2360 | Glut.KEY_DOWN
-> downbirdseye beye
2362 | Glut.KEY_PAGE_UP
->
2363 begin match state.layout with
2367 state.mode <- Birdseye
(
2368 conf, leftx
, l
.pageno, hooverpageno
, anchor
2370 gotopage1 l
.pageno 0;
2373 let layout = layout (state.y-conf.winh
) conf.winh
in
2375 | [] -> gotoy (clamp (-conf.winh
))
2377 state.mode <- Birdseye
(
2378 conf, leftx
, l
.pageno, hooverpageno
, anchor
2380 gotopage1 l
.pageno 0
2383 | [] -> gotoy (clamp (-conf.winh
))
2386 | Glut.KEY_PAGE_DOWN
->
2387 begin match List.rev
state.layout with
2389 let layout = layout (state.y + conf.winh
) conf.winh
in
2390 begin match layout with
2392 let incr = l
.pageh
- l
.pagevh in
2397 conf, leftx
, state.pagecount
- 1, hooverpageno
, anchor
2399 Glut.postRedisplay
();
2401 else gotoy (clamp (incr + conf.interpagespace*2));
2405 Birdseye
(conf, leftx
, l
.pageno, hooverpageno
, anchor);
2406 gotopage1 l
.pageno 0;
2409 | [] -> gotoy (clamp conf.winh
)
2413 state.mode <- Birdseye
(conf, leftx
, 0, hooverpageno
, anchor);
2417 let pageno = state.pagecount
- 1 in
2418 state.mode <- Birdseye
(conf, leftx
, pageno, hooverpageno
, anchor);
2419 if not
(pagevisible state.layout pageno)
2422 match List.rev
state.pdims
with
2424 | (_
, _
, h, _
) :: _
-> h
2426 gotoy (max
0 (getpagey pageno - (conf.winh
- h - conf.interpagespace)))
2427 else Glut.postRedisplay
();
2431 let setautoscrollspeed goingdown
=
2432 let incr = max
1 (state.ascrollstep
/ 2) in
2433 let astep = max
1 (state.ascrollstep
+ (if goingdown
then incr else -incr)) in
2434 state.ascrollstep
<- astep;
2437 let special ~key ~x ~
y =
2440 match state.mode with
2441 | View
| (Birdseye _
) when key
= Glut.KEY_F9
->
2445 birdseyespecial key vals
2447 | View
when key
= Glut.KEY_F1
->
2451 if state.ascrollstep
> 0 && (key
= Glut.KEY_DOWN
|| key
= Glut.KEY_UP
)
2452 then setautoscrollspeed (key
= Glut.KEY_DOWN
)
2456 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
2457 | Glut.KEY_UP
-> clamp (-conf.scrollstep
)
2458 | Glut.KEY_DOWN
-> clamp conf.scrollstep
2459 | Glut.KEY_PAGE_UP
->
2460 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2462 match state.layout with
2464 | l
:: _
-> state.y - l
.pagey
2467 | Glut.KEY_PAGE_DOWN
->
2468 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2470 match List.rev
state.layout with
2472 | l
:: _
-> getpagey l
.pageno
2475 | Glut.KEY_HOME
-> addnav (); 0
2478 state.maxy - (if conf.maxhfit
then conf.winh
else 0)
2480 | (Glut.KEY_RIGHT
| Glut.KEY_LEFT
) when
2481 Glut.getModifiers
() land Glut.active_alt
!= 0 ->
2482 getnav (if key
= Glut.KEY_LEFT
then 1 else -1)
2484 | Glut.KEY_RIGHT
when conf.zoom > 1.0 ->
2485 state.x
<- state.x
- 10;
2487 | Glut.KEY_LEFT
when conf.zoom > 1.0 ->
2488 state.x
<- state.x
+ 10;
2493 gotoy_and_clear_text y
2496 ((c, _
, (Some
(action, _
) as onhist), onkey
, ondone), mode) ->
2499 | Glut.KEY_UP
-> action HCprev
2500 | Glut.KEY_DOWN
-> action HCnext
2501 | Glut.KEY_HOME
-> action HCfirst
2502 | Glut.KEY_END
-> action HClast
2505 state.mode <- Textentry
((c, s, onhist, onkey
, ondone), mode);
2506 Glut.postRedisplay
()
2510 | Items
(active, first
, items, qsearch, pan
, oldmode
) ->
2511 let maxrows = maxoutlinerows () in
2512 let itemcount = Array.length
items in
2513 let hasaction = function
2514 | (_
, _
, Noaction
) -> false
2517 let find start
incr =
2519 if i = -1 || i = itemcount
2522 if hasaction items.(i)
2524 else find (i + incr)
2529 let set active first
=
2530 let first = max
0 (min
first (itemcount - maxrows)) in
2531 state.mode <- Items
(active, first, items, qsearch, pan
, oldmode
)
2534 let isvisible first n = n >= first && n - first <= maxrows in
2536 let incr1 = if incr > 0 then 1 else -1 in
2537 if isvisible first active
2540 let next = active + incr in
2542 if next < 0 || next >= itemcount
2544 else find next incr1
2546 if next = -1 || abs
(active - next) > maxrows
2552 let first = first + incr in
2553 let first = max
0 (min
first (itemcount - 1)) in
2555 let next = active + incr in
2556 let next = max
0 (min
next (itemcount - 1)) in
2559 let active = if next = -1 then active else next in
2562 let first = min
next first in
2565 let first = first + incr in
2566 let first = max
0 (min
first (itemcount - 1)) in
2568 let next = active + incr in
2569 let next = max
0 (min
next (itemcount - 1)) in
2570 let next = find next incr1 in
2571 if next = -1 || abs
(active - first) > maxrows
2578 Glut.postRedisplay
()
2580 begin match key
with
2581 | Glut.KEY_UP
-> navigate ~
-1
2582 | Glut.KEY_DOWN
-> navigate 1
2583 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
2584 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
2587 state.mode <- Items
(
2588 active, first, items, qsearch, min
0 (pan
- 1), oldmode
2590 Glut.postRedisplay
()
2593 state.mode <- Items
(
2594 active, first, items, qsearch, min
0 (pan
+ 1), oldmode
2596 Glut.postRedisplay
()
2599 let active = find 0 1 in
2601 Glut.postRedisplay
()
2604 let first = max
0 (itemcount - maxrows) in
2605 let active = find (itemcount - 1) ~
-1 in
2607 Glut.postRedisplay
()
2612 | Outline
(allowdel
, active, first, outlines, qsearch, pan
, oldmode
) ->
2613 let maxrows = maxoutlinerows () in
2614 let calcfirst first active =
2617 let rows = active - first in
2618 if rows > maxrows then active - maxrows else first
2622 let active = active + incr in
2623 let active = max
0 (min
active (Array.length
outlines - 1)) in
2624 let first = calcfirst first active in
2625 state.mode <- Outline
(
2626 allowdel
, active, first, outlines, qsearch, pan
, oldmode
2628 Glut.postRedisplay
()
2630 let updownlevel incr =
2631 let len = Array.length
outlines in
2632 let (_
, curlevel
, _
) = outlines.(active) in
2634 if i = len then i-1 else if i = -1 then 0 else
2635 let (_
, l
, _
) = outlines.(i) in
2636 if l
!= curlevel
then i else flow (i+incr)
2638 let active = flow active in
2639 let first = calcfirst first active in
2640 state.mode <- Outline
(
2641 allowdel
, active, first, outlines, qsearch, pan
, oldmode
2643 Glut.postRedisplay
()
2646 | Glut.KEY_UP
-> navigate ~
-1
2647 | Glut.KEY_DOWN
-> navigate 1
2648 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
2649 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
2652 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2654 state.mode <- Outline
(
2655 allowdel
, active, first, outlines,
2656 qsearch, min
0 (pan
+ 1), oldmode
2658 Glut.postRedisplay
();
2666 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2668 state.mode <- Outline
(
2669 allowdel
, active, first, outlines, qsearch, pan
- 1, oldmode
2671 Glut.postRedisplay
();
2675 then updownlevel ~
-1
2679 state.mode <- Outline
(
2680 allowdel
, 0, 0, outlines, qsearch, pan
, oldmode
2682 Glut.postRedisplay
()
2685 let active = Array.length
outlines - 1 in
2686 let first = max
0 (active - maxrows) in
2687 state.mode <- Outline
(
2688 allowdel
, active, first, outlines, qsearch, pan
, oldmode
2690 Glut.postRedisplay
()
2695 let drawplaceholder l
=
2696 let margin = state.x
+ (conf.winw
- (state.w + state.scrollw
)) / 2 in
2698 (float l
.pagex
, float l
.pagedispy
)
2699 (float (l
.pagew
+ l
.pagex
), float (l
.pagedispy
+ l
.pagevh))
2701 let x = if margin < 0 then -margin else l
.pagex
2702 and y = l
.pagedispy
+ 13 in
2703 GlDraw.color
(0.0, 0.0, 0.0);
2704 drawstring 13 x y ("Loading " ^ string_of_int
(l
.pageno + 1))
2707 let now () = Unix.gettimeofday
();;
2711 match state.mode with
2712 | Textentry _
-> scalecolor 0.4
2713 | View
| Outline _
| Items _
-> scalecolor 1.0
2714 | Birdseye
(_
, _
, pageno, hooverpageno
, _
) ->
2715 if l
.pageno = hooverpageno
2718 if l
.pageno = pageno
2724 begin match getopaque l
.pageno with
2725 | Some
(opaque, _
) when validopaque opaque ->
2727 draw
(l
.pagedispy
, l
.pagew
, l
.pagevh, l
.pagey, conf.hlinks
)
2731 vlog "draw %d %f sec" l
.pageno d;
2739 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
2740 let sh = (float (maxy + conf.winh
) /. float conf.winh
) in
2741 let sh = float conf.winh
/. sh in
2742 let sh = max
sh (float conf.scrollh
) in
2745 if state.y = state.maxy
2747 else float y /. float maxy
2749 let position = (float conf.winh
-. sh) *. percent in
2752 if position +. sh > float conf.winh
2753 then float conf.winh
-. sh
2759 let scrollindicator () =
2760 GlDraw.color (0.64 , 0.64, 0.64);
2762 (float (conf.winw
- state.scrollw
), 0.)
2763 (float conf.winw
, float conf.winh
)
2765 GlDraw.color (0.0, 0.0, 0.0);
2767 let position, sh = scrollph state.y in
2769 (float (conf.winw
- state.scrollw
), position)
2770 (float conf.winw
, position +. sh)
2774 let showsel margin =
2775 match state.mstate
with
2776 | Mnone
| Mscroll _
| Mpan _
| Mzoom _
->
2779 | Msel
((x0
, y0
), (x1
, y1
)) ->
2780 let rec loop = function
2782 if (y0
>= l
.pagedispy
&& y0
<= (l
.pagedispy
+ l
.pagevh))
2783 || ((y1
>= l
.pagedispy
&& y1
<= (l
.pagedispy
+ l
.pagevh)))
2785 match getopaque l
.pageno with
2786 | Some
(opaque, _
) when validopaque opaque ->
2787 let oy = -l
.pagey + l
.pagedispy
in
2789 (x0
- margin - state.x, y0
,
2790 x1
- margin - state.x, y1
) oy;
2800 let panx = float state.x in
2802 GlDraw.color (0.0, 0.0, 1.0) ~alpha
:0.5;
2803 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2805 (fun (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) ->
2807 if l
.pageno = pageno
2809 let d = float (l
.pagedispy
- l
.pagey) in
2810 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
2811 GlDraw.begins `quads
;
2813 GlDraw.vertex2
(x0
+.panx, y0
+.d);
2814 GlDraw.vertex2
(x1
+.panx, y1
+.d);
2815 GlDraw.vertex2
(x2
+.panx, y2
+.d);
2816 GlDraw.vertex2
(x3
+.panx, y3
+.d);
2826 let showstrings active first pan
strings =
2828 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2829 GlDraw.color (0., 0., 0.) ~alpha
:0.85;
2830 GlDraw.rect (0., 0.) (float conf.winw
, float conf.winh
);
2831 GlDraw.color (1., 1., 1.);
2832 Gl.enable `texture_2d
;
2835 if row
= Array.length
strings || (row
- first) * 16 > conf.winh
2838 let (s, level
, _
) = strings.(row
) in
2839 let y = (row
- first) * 16 in
2840 let x = 5 + 15*(max
0 (level
+pan
)) in
2843 Gl.disable `texture_2d
;
2844 GlDraw.polygon_mode `both `
line;
2845 GlDraw.color (1., 1., 1.) ~alpha
:0.9;
2846 GlDraw.rect (0., float (y + 1))
2847 (float (conf.winw
- 1), float (y + 18));
2848 GlDraw.polygon_mode `both `fill
;
2849 GlDraw.color (1., 1., 1.);
2850 Gl.enable `texture_2d
;
2853 let l = String.length
s in
2856 let pan = pan * 2 in
2857 let pos = pan + level
in
2858 let left = l + pos in
2864 else String.sub
s (-pos) left
2866 ignore
(drawstring1 14 x (y + 16) s)
2869 ignore
(drawstring1 14 (x + pan*15) (y + 16) s)
2877 Gl.disable `texture_2d
;
2880 let showoutline (_
, active, first, outlines, _
, pan, _
) =
2881 showstrings active first pan outlines;
2884 let showitems (active, first, items, _
, pan, _
) =
2885 showstrings active first pan items;
2889 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
2890 GlDraw.viewport
margin 0 state.w conf.winh
;
2892 GlClear.color (scalecolor2 conf.bgcolor
);
2893 GlClear.clear
[`
color];
2896 Gl.enable `scissor_test
;
2897 GlMisc.scissor
0 0 (conf.winw
- state.scrollw
) conf.winh
;
2899 List.iter
drawpage state.layout;
2902 Gl.disable `scissor_test
2906 let x = -.float state.x in
2907 GlMat.translate ~
x ();
2911 GlDraw.viewport
0 0 conf.winw
conf.winh
;
2914 begin match state.mode with
2915 | Items
items -> showitems items
2916 | Outline
outline -> showoutline outline
2920 Glut.swapBuffers
();
2924 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
2925 let x = x - margin - state.x in
2926 let rec f = function
2928 begin match getopaque l.pageno with
2929 | Some
(opaque, _
) when validopaque opaque ->
2930 let y = y - l.pagedispy
in
2933 let y = l.pagey + y in
2934 let x = x - l.pagex
in
2935 match whatsunder
opaque x y with
2948 let viewmouse button bstate
x y =
2950 | Glut.OTHER_BUTTON
n when (n == 3 || n == 4) && bstate
= Glut.UP
->
2951 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2953 match state.mstate
with
2954 | Mzoom
(oldn
, i) ->
2962 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
2964 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
2966 let zoom = conf.zoom +. incr in
2968 state.mstate
<- Mzoom
(n, 0);
2970 state.mstate
<- Mzoom
(n, i+1);
2972 else state.mstate
<- Mzoom
(n, 0)
2974 | _
-> state.mstate
<- Mzoom
(n, 0)
2977 if state.ascrollstep
> 0
2979 setautoscrollspeed (n=4)
2983 then -conf.scrollstep
2984 else conf.scrollstep
2986 let incr = incr * 2 in
2987 let y = clamp incr in
2988 gotoy_and_clear_text y
2991 | Glut.LEFT_BUTTON
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
2992 if bstate
= Glut.DOWN
2994 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
2995 state.mstate
<- Mpan
(x, y)
2998 state.mstate
<- Mnone
3000 | Glut.LEFT_BUTTON
when x > conf.winw
- state.scrollw
->
3001 if bstate
= Glut.DOWN
3003 let position, sh = scrollph state.y in
3004 if y > truncate
position && y < truncate
(position +. sh)
3006 state.mstate
<- Mscroll
3008 let percent = float y /. float conf.winh
in
3009 let desty = truncate
(float (state.maxy - conf.winh
) *. percent) in
3011 state.mstate
<- Mscroll
3013 state.mstate
<- Mnone
3015 | Glut.LEFT_BUTTON
->
3016 let dest = if bstate
= Glut.DOWN
then getunder x y else Unone
in
3017 begin match dest with
3018 | Ulinkgoto
(pageno, top) ->
3022 gotopage1 pageno top;
3028 | Unone
when bstate
= Glut.DOWN
->
3029 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
3030 state.mstate
<- Mpan
(x, y);
3032 | Unone
| Utext _
->
3033 if bstate
= Glut.DOWN
3035 if conf.angle
mod 360 = 0
3037 state.mstate
<- Msel
((x, y), (x, y));
3038 Glut.postRedisplay
()
3042 match state.mstate
with
3045 | Mzoom _
| Mscroll
->
3046 state.mstate
<- Mnone
3049 Glut.setCursor
Glut.CURSOR_INHERIT
;
3050 state.mstate
<- Mnone
3052 | Msel
((_
, y0
), (_
, y1
)) ->
3054 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh))
3055 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh)))
3057 match getopaque l.pageno with
3058 | Some
(opaque, _
) when validopaque opaque ->
3062 List.iter
f state.layout;
3063 copysel
""; (* ugly *)
3064 Glut.setCursor
Glut.CURSOR_INHERIT
;
3065 state.mstate
<- Mnone
;
3072 let birdseyemouse button bstate
x y
3073 (conf, leftx
, _
, hooverpageno
, anchor) =
3075 | Glut.LEFT_BUTTON
when bstate
= Glut.UP
->
3076 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
3077 let rec loop = function
3080 if y > l.pagedispy
&& y < l.pagedispy
+ l.pagevh
3081 && x > margin && x < margin + l.pagew
3083 leavebirdseye (conf, leftx
, l.pageno, hooverpageno
, anchor) false;
3088 | Glut.OTHER_BUTTON _
-> viewmouse button bstate
x y
3092 let mouse bstate button
x y =
3093 match state.mode with
3094 | View
-> viewmouse button bstate
x y
3095 | Birdseye beye
-> birdseyemouse button bstate
x y beye
3096 | Textentry _
| Outline _
| Items _
-> ()
3099 let mouse ~button ~
state ~
x ~
y = mouse state button
x y;;
3102 match state.mode with
3105 match state.mstate
with
3106 | Mzoom _
| Mnone
-> ()
3111 state.mstate
<- Mpan
(x, y);
3112 if conf.zoom > 1.0 then state.x <- state.x + dx;
3114 gotoy_and_clear_text y
3117 state.mstate
<- Msel
(a, (x, y));
3118 Glut.postRedisplay
()
3121 let y = min
conf.winh
(max
0 y) in
3122 let percent = float y /. float conf.winh
in
3123 let y = truncate
(float (state.maxy - conf.winh
) *. percent) in
3124 gotoy_and_clear_text y
3128 match state.mode with
3129 | Birdseye
(conf, leftx
, pageno, hooverpageno
, anchor) ->
3130 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
3131 let rec loop = function
3133 if hooverpageno
!= -1
3135 state.mode <- Birdseye
(conf, leftx
, pageno, -1, anchor);
3136 Glut.postRedisplay
();
3139 if y > l.pagedispy
&& y < l.pagedispy
+ l.pagevh
3140 && x > margin && x < margin + l.pagew
3142 state.mode <- Birdseye
(conf, leftx
, pageno, l.pageno, anchor);
3143 Glut.postRedisplay
();
3149 | Outline _
| Items _
| Textentry _
-> ()
3151 match state.mstate
with
3153 begin match getunder x y with
3154 | Unone
-> Glut.setCursor
Glut.CURSOR_INHERIT
3156 if conf.underinfo
then showtext 'u'
("ri: " ^ uri
);
3157 Glut.setCursor
Glut.CURSOR_INFO
3158 | Ulinkgoto
(page
, _
) ->
3160 then showtext 'p'
("age: " ^ string_of_int page
);
3161 Glut.setCursor
Glut.CURSOR_INFO
3163 if conf.underinfo
then showtext '
f'
("ont: " ^
s);
3164 Glut.setCursor
Glut.CURSOR_TEXT
3167 | Mpan _
| Msel _
| Mzoom _
| Mscroll
->
3178 match Sys.os_type
with
3179 | "Win32" -> Sys.getenv
"HOMEPATH"
3180 | _
-> Sys.getenv
"HOME"
3183 ("Can not determine home directory location: " ^
3184 Printexc.to_string exn
);
3188 let config_of c attrs
=
3192 | "scroll-bar-width" -> { c with scrollbw
= max
0 (int_of_string
v) }
3193 | "scroll-handle-height" -> { c with scrollh
= max
0 (int_of_string
v) }
3194 | "case-insensitive-search" -> { c with icase
= bool_of_string
v }
3195 | "preload" -> { c with preload = bool_of_string
v }
3196 | "page-bias" -> { c with pagebias
= int_of_string
v }
3197 | "scroll-step" -> { c with scrollstep
= max
1 (int_of_string
v) }
3198 | "auto-scroll-step" ->
3199 { c with autoscrollstep
= max
0 (int_of_string
v) }
3200 | "max-height-fit" -> { c with maxhfit
= bool_of_string
v }
3201 | "crop-hack" -> { c with crophack
= bool_of_string
v }
3202 | "throttle" -> { c with showall
= bool_of_string
v }
3203 | "highlight-links" -> { c with hlinks
= bool_of_string
v }
3204 | "under-cursor-info" -> { c with underinfo
= bool_of_string
v }
3205 | "vertical-margin" ->
3206 { c with interpagespace = max
0 (int_of_string
v) }
3208 let zoom = float_of_string
v /. 100. in
3209 let zoom = max
0.01 (min
2.2 zoom) in
3210 { c with zoom = zoom }
3211 | "presentation" -> { c with presentation = bool_of_string
v }
3212 | "rotation-angle" -> { c with angle
= int_of_string
v }
3213 | "width" -> { c with winw
= max
20 (int_of_string
v) }
3214 | "height" -> { c with winh
= max
20 (int_of_string
v) }
3215 | "persistent-bookmarks" -> { c with savebmarks
= bool_of_string
v }
3216 | "proportional-display" -> { c with proportional
= bool_of_string
v }
3217 | "pixmap-cache-size" -> { c with memlimit
= max
2 (int_of_string
v) }
3218 | "tex-count" -> { c with texcount
= max
1 (int_of_string
v) }
3219 | "slice-height" -> { c with sliceheight
= max
2 (int_of_string
v) }
3220 | "thumbnail-width" -> { c with thumbw
= max
2 (int_of_string
v) }
3221 | "persistent-location" -> { c with jumpback
= bool_of_string
v }
3222 | "background-color" -> { c with bgcolor
= color_of_string v }
3223 | "scrollbar-in-presentation" ->
3224 { c with scrollbarinpm
= bool_of_string
v }
3225 | "ui-font" -> { c with uifont
= v }
3228 prerr_endline
("Error processing attribute (`" ^
3229 k ^
"'=`" ^
v ^
"'): " ^
Printexc.to_string exn
);
3232 let rec fold c = function
3235 let c = apply c k
v in
3241 let fromstring f pos n v d =
3244 dolog "Error processing attribute (%S=%S) at %d\n%s"
3245 n v pos (Printexc.to_string exn
)
3250 let bookmark_of attrs
=
3251 let rec fold title page rely
= function
3252 | ("title", v) :: rest
-> fold v page rely rest
3253 | ("page", v) :: rest
-> fold title v rely rest
3254 | ("rely", v) :: rest
-> fold title page
v rest
3255 | _
:: rest
-> fold title page rely rest
3256 | [] -> title, page
, rely
3258 fold "invalid" "0" "0" attrs
3262 let rec fold path page rely
pan = function
3263 | ("path", v) :: rest
-> fold v page rely
pan rest
3264 | ("page", v) :: rest
-> fold path
v rely
pan rest
3265 | ("rely", v) :: rest
-> fold path page
v pan rest
3266 | ("pan", v) :: rest
-> fold path page rely
v rest
3267 | _
:: rest
-> fold path page rely
pan rest
3268 | [] -> path
, page
, rely
, pan
3270 fold "" "0" "0" "0" attrs
3273 let setconf dst src
=
3274 dst
.scrollbw
<- src
.scrollbw
;
3275 dst
.scrollh
<- src
.scrollh
;
3276 dst
.icase
<- src
.icase
;
3277 dst
.preload <- src
.preload;
3278 dst
.pagebias
<- src
.pagebias
;
3279 dst
.verbose
<- src
.verbose
;
3280 dst
.scrollstep
<- src
.scrollstep
;
3281 dst
.maxhfit
<- src
.maxhfit
;
3282 dst
.crophack
<- src
.crophack
;
3283 dst
.autoscrollstep
<- src
.autoscrollstep
;
3284 dst
.showall
<- src
.showall
;
3285 dst
.hlinks
<- src
.hlinks
;
3286 dst
.underinfo
<- src
.underinfo
;
3287 dst
.interpagespace <- src
.interpagespace;
3288 dst
.zoom <- src
.zoom;
3289 dst
.presentation <- src
.presentation;
3290 dst
.angle
<- src
.angle
;
3291 dst
.winw
<- src
.winw
;
3292 dst
.winh
<- src
.winh
;
3293 dst
.savebmarks
<- src
.savebmarks
;
3294 dst
.memlimit
<- src
.memlimit
;
3295 dst
.proportional
<- src
.proportional
;
3296 dst
.texcount
<- src
.texcount
;
3297 dst
.sliceheight
<- src
.sliceheight
;
3298 dst
.thumbw
<- src
.thumbw
;
3299 dst
.jumpback
<- src
.jumpback
;
3300 dst
.bgcolor
<- src
.bgcolor
;
3301 dst
.scrollbarinpm
<- src
.scrollbarinpm
;
3302 dst
.uifont
<- src
.uifont
;
3306 let l = String.length
s in
3307 let b = Buffer.create
l in
3313 let h = Hashtbl.create
10 in
3314 let dc = { defconf with angle
= defconf.angle
} in
3315 let rec toplevel v t spos _
=
3317 | Vdata
| Vcdata
| Vend
-> v
3318 | Vopen
("llppconfig", _
, closed
) ->
3321 else { v with f = llppconfig
}
3323 error
"unexpected subelement at top level" s spos
3324 | Vclose _
-> error
"unexpected close at top level" s spos
3326 and llppconfig
v t spos _
=
3328 | Vdata
| Vcdata
| Vend
-> v
3329 | Vopen
("defaults", attrs
, closed
) ->
3330 let c = config_of dc attrs
in
3334 else { v with f = skip
"defaults" (fun () -> v) }
3336 | Vopen
("doc", attrs
, closed
) ->
3337 let pathent, spage
, srely
, span
= doc_of attrs
in
3338 let path = unent pathent
3339 and pageno = fromstring int_of_string spos
"page" spage
0
3340 and rely
= fromstring float_of_string spos
"rely" srely
0.0
3341 and pan = fromstring int_of_string spos
"pan" span
0 in
3342 let c = config_of dc attrs
in
3343 let anchor = (pageno, rely
) in
3345 then (Hashtbl.add
h path (c, [], pan, anchor); v)
3346 else { v with f = doc
path pan anchor c [] }
3349 error
"unexpected subelement in llppconfig" s spos
3351 | Vclose
"llppconfig" -> { v with f = toplevel }
3352 | Vclose _
-> error
"unexpected close in llppconfig" s spos
3354 and doc
path pan anchor c bookmarks v t spos _
=
3356 | Vdata
| Vcdata
-> v
3357 | Vend
-> error
"unexpected end of input in doc" s spos
3358 | Vopen
("bookmarks", _
, closed
) ->
3361 else { v with f = pbookmarks
path pan anchor c bookmarks }
3363 | Vopen
(_
, _
, _
) ->
3364 error
"unexpected subelement in doc" s spos
3367 Hashtbl.add
h path (c, List.rev
bookmarks, pan, anchor);
3368 { v with f = llppconfig
}
3370 | Vclose _
-> error
"unexpected close in doc" s spos
3372 and pbookmarks
path pan anchor c bookmarks v t spos _
=
3374 | Vdata
| Vcdata
-> v
3375 | Vend
-> error
"unexpected end of input in bookmarks" s spos
3376 | Vopen
("item", attrs
, closed
) ->
3377 let titleent, spage
, srely
= bookmark_of attrs
in
3378 let page = fromstring int_of_string spos
"page" spage
0
3379 and rely
= fromstring float_of_string spos
"rely" srely
0.0 in
3380 let bookmarks = (unent titleent, 0, (page, rely
)) :: bookmarks in
3382 then { v with f = pbookmarks
path pan anchor c bookmarks }
3385 { v with f = skip
"item" f }
3388 error
"unexpected subelement in bookmarks" s spos
3390 | Vclose
"bookmarks" ->
3391 { v with f = doc
path pan anchor c bookmarks }
3393 | Vclose _
-> error
"unexpected close in bookmarks" s spos
3395 and skip tag
f v t spos _
=
3397 | Vdata
| Vcdata
-> v
3399 error
("unexpected end of input in skipped " ^ tag
) s spos
3400 | Vopen
(tag'
, _
, closed
) ->
3404 let f'
() = { v with f = skip tag
f } in
3405 { v with f = skip tag'
f'
}
3409 else error
("unexpected close in skipped " ^ tag
) s spos
3412 parse
{ f = toplevel; accu = () } s;
3418 let len = in_channel_length ic
in
3419 let s = String.create
len in
3420 really_input ic
s 0 len;
3423 | Parse_error
(msg
, s, pos) ->
3424 let subs = subs s pos in
3425 let s = Printf.sprintf
"%s: at %d [..%s..]" msg
pos subs in
3426 failwith
("parse error: " ^
s)
3429 failwith
("config load error: " ^
Printexc.to_string exn
)
3435 let dir = Filename.concat
home ".config" in
3436 if Sys.is_directory
dir then dir else home
3439 Filename.concat
dir "llpp.conf"
3443 if Sys.file_exists
path
3446 (try Some
(open_in_bin
path)
3449 ("Error opening configuation file `" ^
path ^
"': " ^
3450 Printexc.to_string exn
);
3459 ("Error loading configuation from `" ^
path ^
"': " ^
3460 Printexc.to_string exn
);
3466 f (Hashtbl.create
0, defconf)
3471 let pc, pb
, px
, pa
=
3473 Hashtbl.find h (Filename.basename
state.path)
3474 with Not_found
-> dc, [], 0, (0, 0.0)
3478 state.bookmarks <- pb
;
3480 state.scrollw
<- conf.scrollbw
;
3482 then state.anchor <- pa
;
3483 cbput state.hists
.nav pa
;
3488 let add_attrs bb always
dc c =
3491 then Printf.bprintf bb
"\n %s='%b'" s a
3494 then Printf.bprintf bb
"\n %s='%d'" s a
3497 then Printf.bprintf bb
"\n %s='%f'" s (a*.100.)
3501 Printf.bprintf bb
"\n %s='%s'" s (color_to_string a)
3505 Printf.bprintf bb
"\n %s='%s'" s a
3509 then dc.winw
, dc.winh
3511 match state.fullscreen
with
3513 | None
-> c.winw
, c.winh
3515 let zoom, presentation, interpagespace, showall
=
3517 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
3519 match state.mode with
3520 | Birdseye
(bc
, _
, _
, _
, _
) ->
3521 bc
.zoom, bc
.presentation, bc
.interpagespace, bc
.showall
3522 | _
-> c.zoom, c.presentation, c.interpagespace, c.showall
3524 oi
"width" w dc.winw
;
3525 oi
"height" h dc.winh
;
3526 oi
"scroll-bar-width" c.scrollbw
dc.scrollbw
;
3527 oi
"scroll-handle-height" c.scrollh
dc.scrollh
;
3528 ob "case-insensitive-search" c.icase
dc.icase
;
3529 ob "preload" c.preload dc.preload;
3530 oi
"page-bias" c.pagebias
dc.pagebias
;
3531 oi
"scroll-step" c.scrollstep
dc.scrollstep
;
3532 oi
"auto-scroll-step" c.autoscrollstep
dc.autoscrollstep
;
3533 ob "max-height-fit" c.maxhfit
dc.maxhfit
;
3534 ob "crop-hack" c.crophack
dc.crophack
;
3535 ob "throttle" showall
dc.showall
;
3536 ob "highlight-links" c.hlinks
dc.hlinks
;
3537 ob "under-cursor-info" c.underinfo
dc.underinfo
;
3538 oi
"vertical-margin" interpagespace dc.interpagespace;
3539 oz
"zoom" zoom dc.zoom;
3540 ob "presentation" presentation dc.presentation;
3541 oi
"rotation-angle" c.angle
dc.angle
;
3542 ob "persistent-bookmarks" c.savebmarks
dc.savebmarks
;
3543 ob "proportional-display" c.proportional
dc.proportional
;
3544 oi
"pixmap-cache-size" c.memlimit
dc.memlimit
;
3545 oi
"texcount" c.texcount
dc.texcount
;
3546 oi
"slice-height" c.sliceheight
dc.sliceheight
;
3547 oi
"thumbnail-width" c.thumbw
dc.thumbw
;
3548 ob "persistent-location" c.jumpback
dc.jumpback
;
3549 oc
"background-color" c.bgcolor
dc.bgcolor
;
3550 ob "scrollbar-in-presentation" c.scrollbarinpm
dc.scrollbarinpm
;
3551 os
"ui-font" c.uifont
dc.uifont
;
3555 let bb = Buffer.create
32768 in
3557 let dc = if conf.bedefault
then conf else dc in
3558 Buffer.add_string
bb "<llppconfig>\n<defaults ";
3559 add_attrs bb true dc dc;
3560 Buffer.add_string
bb "/>\n";
3562 let adddoc path pan anchor c bookmarks =
3563 if bookmarks == [] && c = dc && anchor = emptyanchor
3566 Printf.bprintf
bb "<doc path='%s'"
3567 (enent
path 0 (String.length
path));
3569 if anchor <> emptyanchor
3571 let n, y = anchor in
3572 Printf.bprintf
bb " page='%d'" n;
3575 Printf.bprintf
bb " rely='%f'" y
3580 then Printf.bprintf
bb " pan='%d'" pan;
3582 add_attrs bb false dc c;
3584 begin match bookmarks with
3585 | [] -> Buffer.add_string
bb "/>\n"
3587 Buffer.add_string
bb ">\n<bookmarks>\n";
3588 List.iter
(fun (title, _level
, (page, rely
)) ->
3590 "<item title='%s' page='%d'"
3591 (enent
title 0 (String.length
title))
3596 Printf.bprintf
bb " rely='%f'" rely
3598 Buffer.add_string
bb "/>\n";
3600 Buffer.add_string
bb "</bookmarks>\n</doc>\n";
3606 match state.mode with
3607 | Birdseye
(_
, pan, _
, _
, _
) -> pan
3610 let basename = Filename.basename state.path in
3611 adddoc basename pan (getanchor ())
3614 if state.ascrollstep
> 0
3615 then state.ascrollstep
3616 else conf.autoscrollstep
}
3617 (if conf.savebmarks
then state.bookmarks else []);
3619 Hashtbl.iter
(fun path (c, bookmarks, x, y) ->
3621 then adddoc path x y c bookmarks
3623 Buffer.add_string
bb "</llppconfig>";
3626 if Buffer.length
bb > 0
3629 let tmp = path ^
".tmp" in
3630 let oc = open_out_bin
tmp in
3631 Buffer.output_buffer
oc bb;
3633 Sys.rename
tmp path;
3636 ("error while saving configuration: " ^
Printexc.to_string exn
)
3643 [("-p", Arg.String
(fun s -> state.password
<- s) , " Set password")
3644 ;("-v", Arg.Unit
(fun () -> print_endline
Help.version
; exit
0),
3645 " Print version and exit")]
3647 (fun s -> state.path <- s)
3648 ("Usage: " ^
Sys.argv
.(0) ^
" [options] some.pdf\nOptions:")
3650 if String.length
state.path = 0
3651 then (prerr_endline
"file name missing"; exit
1);
3655 let _ = Glut.init
Sys.argv
in
3656 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
3657 let () = Glut.initWindowSize
conf.winw
conf.winh
in
3658 let _ = Glut.createWindow
("llpp " ^
Filename.basename state.path) in
3661 if Sys.os_type
= "Unix"
3663 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
3665 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
3666 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
3667 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
3668 Unix.bind
sock addr;
3670 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
3671 Unix.connect
csock addr;
3672 let ssock, _ = Unix.accept
sock in
3675 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
3676 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
3680 at_exit
(fun () -> Unix.shutdown
ssock Unix.SHUTDOWN_ALL
);
3684 let () = Glut.displayFunc
display in
3685 let () = Glut.reshapeFunc
reshape in
3686 let () = Glut.keyboardFunc
keyboard in
3687 let () = Glut.specialFunc
special in
3688 let () = Glut.idleFunc
(Some
idle) in
3689 let () = Glut.mouseFunc
mouse in
3690 let () = Glut.motionFunc
motion in
3691 let () = Glut.passiveMotionFunc
pmotion in
3694 conf.angle
, conf.proportional
, conf.texcount
,
3695 conf.sliceheight
, conf.uifont
3697 state.csock <- csock;
3698 state.ssock <- ssock;
3699 state.text <- "Opening " ^
state.path;
3700 writeopen state.path state.password
;
3704 let rec handlelablglutbug () =
3707 with Glut.BadEnum
"key in special_of_int" ->
3708 showtext '
!'
" LablGlut bug: special key not recognized";
3709 handlelablglutbug ()
3711 handlelablglutbug ();