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_ [%s]" prefix text
state.text
798 Printf.sprintf
"%s%s_" prefix text
803 Printf.sprintf
"%s: %s_ [%s]" prefix text
state.text
805 Printf.sprintf
"%s: %s_" 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
"%s\t%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
"%s\t%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
"%s\t%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
"%s\t%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 < 0
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 trusted
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
;
2834 let wx = measurestr
14 "w" in
2835 let tabx = 30.0*.wx +. float (pan
*15) in
2837 if row
= Array.length
strings || (row
- first) * 16 > conf.winh
2840 let (s, level
, _
) = strings.(row
) in
2841 let y = (row
- first) * 16 in
2842 let x = 5 + 15*(max
0 (level
+pan
)) in
2845 Gl.disable `texture_2d
;
2846 GlDraw.polygon_mode `both `
line;
2847 GlDraw.color (1., 1., 1.) ~alpha
:0.9;
2848 GlDraw.rect (0., float (y + 1))
2849 (float (conf.winw
- 1), float (y + 18));
2850 GlDraw.polygon_mode `both `fill
;
2851 GlDraw.color (1., 1., 1.);
2852 Gl.enable `texture_2d
;
2855 let drawtabularstring x s =
2859 let tabpos = try String.index
s '
\t'
with Not_found
-> -1 in
2862 let len = String.length
s - tabpos - 1 in
2863 let s1 = String.sub
s 0 tabpos
2864 and s2
= String.sub
s (tabpos + 1) len in
2865 let xx = wx +. drawstring1 14 x (y + 16) s1 in
2866 let x = truncate
(max
xx tabx) in
2867 drawstring1 14 x (y + 16) s2
2869 drawstring1 14 x (y + 16) s
2871 drawstring1 14 x (y + 16) s
2877 let l = String.length
s in
2880 let pan = pan * 2 in
2881 let pos = pan + level
in
2882 let left = l + pos in
2888 else String.sub
s (-pos) left
2890 drawtabularstring x s
2891 (* ignore (drawstring1 14 x (y + 16) s) *)
2894 drawtabularstring (x + pan*15) s
2895 (* ignore (drawstring1 14 (x + pan*15) (y + 16) s) *)
2903 Gl.disable `texture_2d
;
2906 let showoutline (_, active, first, outlines, _, pan, _) =
2907 showstrings false active first pan outlines;
2910 let showitems (active, first, items, _, pan, _) =
2911 showstrings true active first pan items;
2915 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
2916 GlDraw.viewport
margin 0 state.w conf.winh
;
2918 GlClear.color (scalecolor2 conf.bgcolor
);
2919 GlClear.clear
[`
color];
2922 Gl.enable `scissor_test
;
2923 GlMisc.scissor
0 0 (conf.winw
- state.scrollw
) conf.winh
;
2925 List.iter
drawpage state.layout;
2928 Gl.disable `scissor_test
2932 let x = -.float state.x in
2933 GlMat.translate ~
x ();
2937 GlDraw.viewport
0 0 conf.winw
conf.winh
;
2940 begin match state.mode with
2941 | Items
items -> showitems items
2942 | Outline
outline -> showoutline outline
2946 Glut.swapBuffers
();
2950 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
2951 let x = x - margin - state.x in
2952 let rec f = function
2954 begin match getopaque l.pageno with
2955 | Some
(opaque, _) when validopaque opaque ->
2956 let y = y - l.pagedispy
in
2959 let y = l.pagey + y in
2960 let x = x - l.pagex
in
2961 match whatsunder
opaque x y with
2974 let viewmouse button bstate
x y =
2976 | Glut.OTHER_BUTTON
n when (n == 3 || n == 4) && bstate
= Glut.UP
->
2977 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2979 match state.mstate
with
2980 | Mzoom
(oldn
, i) ->
2988 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
2990 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
2992 let zoom = conf.zoom +. incr in
2994 state.mstate
<- Mzoom
(n, 0);
2996 state.mstate
<- Mzoom
(n, i+1);
2998 else state.mstate
<- Mzoom
(n, 0)
3000 | _ -> state.mstate
<- Mzoom
(n, 0)
3003 if state.ascrollstep
> 0
3005 setautoscrollspeed (n=4)
3009 then -conf.scrollstep
3010 else conf.scrollstep
3012 let incr = incr * 2 in
3013 let y = clamp incr in
3014 gotoy_and_clear_text y
3017 | Glut.LEFT_BUTTON
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
3018 if bstate
= Glut.DOWN
3020 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
3021 state.mstate
<- Mpan
(x, y)
3024 state.mstate
<- Mnone
3026 | Glut.LEFT_BUTTON
when x > conf.winw
- state.scrollw
->
3027 if bstate
= Glut.DOWN
3029 let position, sh = scrollph state.y in
3030 if y > truncate
position && y < truncate
(position +. sh)
3032 state.mstate
<- Mscroll
3034 let percent = float y /. float conf.winh
in
3035 let desty = truncate
(float (state.maxy - conf.winh
) *. percent) in
3037 state.mstate
<- Mscroll
3039 state.mstate
<- Mnone
3041 | Glut.LEFT_BUTTON
->
3042 let dest = if bstate
= Glut.DOWN
then getunder x y else Unone
in
3043 begin match dest with
3044 | Ulinkgoto
(pageno, top) ->
3048 gotopage1 pageno top;
3054 | Unone
when bstate
= Glut.DOWN
->
3055 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
3056 state.mstate
<- Mpan
(x, y);
3058 | Unone
| Utext
_ ->
3059 if bstate
= Glut.DOWN
3061 if conf.angle
mod 360 = 0
3063 state.mstate
<- Msel
((x, y), (x, y));
3064 Glut.postRedisplay
()
3068 match state.mstate
with
3071 | Mzoom
_ | Mscroll
->
3072 state.mstate
<- Mnone
3075 Glut.setCursor
Glut.CURSOR_INHERIT
;
3076 state.mstate
<- Mnone
3078 | Msel
((_, y0
), (_, y1
)) ->
3080 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh))
3081 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh)))
3083 match getopaque l.pageno with
3084 | Some
(opaque, _) when validopaque opaque ->
3088 List.iter
f state.layout;
3089 copysel
""; (* ugly *)
3090 Glut.setCursor
Glut.CURSOR_INHERIT
;
3091 state.mstate
<- Mnone
;
3098 let birdseyemouse button bstate
x y
3099 (conf, leftx
, _, hooverpageno
, anchor) =
3101 | Glut.LEFT_BUTTON
when bstate
= Glut.UP
->
3102 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
3103 let rec loop = function
3106 if y > l.pagedispy
&& y < l.pagedispy
+ l.pagevh
3107 && x > margin && x < margin + l.pagew
3109 leavebirdseye (conf, leftx
, l.pageno, hooverpageno
, anchor) false;
3114 | Glut.OTHER_BUTTON
_ -> viewmouse button bstate
x y
3118 let mouse bstate button
x y =
3119 match state.mode with
3120 | View
-> viewmouse button bstate
x y
3121 | Birdseye beye
-> birdseyemouse button bstate
x y beye
3122 | Textentry
_ | Outline
_ | Items
_ -> ()
3125 let mouse ~button ~
state ~
x ~
y = mouse state button
x y;;
3128 match state.mode with
3131 match state.mstate
with
3132 | Mzoom
_ | Mnone
-> ()
3137 state.mstate
<- Mpan
(x, y);
3138 if conf.zoom > 1.0 then state.x <- state.x + dx;
3140 gotoy_and_clear_text y
3143 state.mstate
<- Msel
(a, (x, y));
3144 Glut.postRedisplay
()
3147 let y = min
conf.winh
(max
0 y) in
3148 let percent = float y /. float conf.winh
in
3149 let y = truncate
(float (state.maxy - conf.winh
) *. percent) in
3150 gotoy_and_clear_text y
3154 match state.mode with
3155 | Birdseye
(conf, leftx
, pageno, hooverpageno
, anchor) ->
3156 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
3157 let rec loop = function
3159 if hooverpageno
!= -1
3161 state.mode <- Birdseye
(conf, leftx
, pageno, -1, anchor);
3162 Glut.postRedisplay
();
3165 if y > l.pagedispy
&& y < l.pagedispy
+ l.pagevh
3166 && x > margin && x < margin + l.pagew
3168 state.mode <- Birdseye
(conf, leftx
, pageno, l.pageno, anchor);
3169 Glut.postRedisplay
();
3175 | Outline
_ | Items
_ | Textentry
_ -> ()
3177 match state.mstate
with
3179 begin match getunder x y with
3180 | Unone
-> Glut.setCursor
Glut.CURSOR_INHERIT
3182 if conf.underinfo
then showtext 'u'
("ri: " ^ uri
);
3183 Glut.setCursor
Glut.CURSOR_INFO
3184 | Ulinkgoto
(page
, _) ->
3186 then showtext 'p'
("age: " ^ string_of_int page
);
3187 Glut.setCursor
Glut.CURSOR_INFO
3189 if conf.underinfo
then showtext '
f'
("ont: " ^
s);
3190 Glut.setCursor
Glut.CURSOR_TEXT
3193 | Mpan
_ | Msel
_ | Mzoom
_ | Mscroll
->
3204 match Sys.os_type
with
3205 | "Win32" -> Sys.getenv
"HOMEPATH"
3206 | _ -> Sys.getenv
"HOME"
3209 ("Can not determine home directory location: " ^
3210 Printexc.to_string exn
);
3214 let config_of c attrs
=
3218 | "scroll-bar-width" -> { c with scrollbw
= max
0 (int_of_string
v) }
3219 | "scroll-handle-height" -> { c with scrollh
= max
0 (int_of_string
v) }
3220 | "case-insensitive-search" -> { c with icase
= bool_of_string
v }
3221 | "preload" -> { c with preload = bool_of_string
v }
3222 | "page-bias" -> { c with pagebias
= int_of_string
v }
3223 | "scroll-step" -> { c with scrollstep
= max
1 (int_of_string
v) }
3224 | "auto-scroll-step" ->
3225 { c with autoscrollstep
= max
0 (int_of_string
v) }
3226 | "max-height-fit" -> { c with maxhfit
= bool_of_string
v }
3227 | "crop-hack" -> { c with crophack
= bool_of_string
v }
3228 | "throttle" -> { c with showall
= bool_of_string
v }
3229 | "highlight-links" -> { c with hlinks
= bool_of_string
v }
3230 | "under-cursor-info" -> { c with underinfo
= bool_of_string
v }
3231 | "vertical-margin" ->
3232 { c with interpagespace = max
0 (int_of_string
v) }
3234 let zoom = float_of_string
v /. 100. in
3235 let zoom = max
0.01 (min
2.2 zoom) in
3236 { c with zoom = zoom }
3237 | "presentation" -> { c with presentation = bool_of_string
v }
3238 | "rotation-angle" -> { c with angle
= int_of_string
v }
3239 | "width" -> { c with winw
= max
20 (int_of_string
v) }
3240 | "height" -> { c with winh
= max
20 (int_of_string
v) }
3241 | "persistent-bookmarks" -> { c with savebmarks
= bool_of_string
v }
3242 | "proportional-display" -> { c with proportional
= bool_of_string
v }
3243 | "pixmap-cache-size" -> { c with memlimit
= max
2 (int_of_string
v) }
3244 | "tex-count" -> { c with texcount
= max
1 (int_of_string
v) }
3245 | "slice-height" -> { c with sliceheight
= max
2 (int_of_string
v) }
3246 | "thumbnail-width" -> { c with thumbw
= max
2 (int_of_string
v) }
3247 | "persistent-location" -> { c with jumpback
= bool_of_string
v }
3248 | "background-color" -> { c with bgcolor
= color_of_string v }
3249 | "scrollbar-in-presentation" ->
3250 { c with scrollbarinpm
= bool_of_string
v }
3251 | "ui-font" -> { c with uifont
= v }
3254 prerr_endline
("Error processing attribute (`" ^
3255 k ^
"'=`" ^
v ^
"'): " ^
Printexc.to_string exn
);
3258 let rec fold c = function
3261 let c = apply c k
v in
3267 let fromstring f pos n v d =
3270 dolog "Error processing attribute (%S=%S) at %d\n%s"
3271 n v pos (Printexc.to_string exn
)
3276 let bookmark_of attrs
=
3277 let rec fold title page rely
= function
3278 | ("title", v) :: rest
-> fold v page rely rest
3279 | ("page", v) :: rest
-> fold title v rely rest
3280 | ("rely", v) :: rest
-> fold title page
v rest
3281 | _ :: rest
-> fold title page rely rest
3282 | [] -> title, page
, rely
3284 fold "invalid" "0" "0" attrs
3288 let rec fold path page rely
pan = function
3289 | ("path", v) :: rest
-> fold v page rely
pan rest
3290 | ("page", v) :: rest
-> fold path
v rely
pan rest
3291 | ("rely", v) :: rest
-> fold path page
v pan rest
3292 | ("pan", v) :: rest
-> fold path page rely
v rest
3293 | _ :: rest
-> fold path page rely
pan rest
3294 | [] -> path
, page
, rely
, pan
3296 fold "" "0" "0" "0" attrs
3299 let setconf dst src
=
3300 dst
.scrollbw
<- src
.scrollbw
;
3301 dst
.scrollh
<- src
.scrollh
;
3302 dst
.icase
<- src
.icase
;
3303 dst
.preload <- src
.preload;
3304 dst
.pagebias
<- src
.pagebias
;
3305 dst
.verbose
<- src
.verbose
;
3306 dst
.scrollstep
<- src
.scrollstep
;
3307 dst
.maxhfit
<- src
.maxhfit
;
3308 dst
.crophack
<- src
.crophack
;
3309 dst
.autoscrollstep
<- src
.autoscrollstep
;
3310 dst
.showall
<- src
.showall
;
3311 dst
.hlinks
<- src
.hlinks
;
3312 dst
.underinfo
<- src
.underinfo
;
3313 dst
.interpagespace <- src
.interpagespace;
3314 dst
.zoom <- src
.zoom;
3315 dst
.presentation <- src
.presentation;
3316 dst
.angle
<- src
.angle
;
3317 dst
.winw
<- src
.winw
;
3318 dst
.winh
<- src
.winh
;
3319 dst
.savebmarks
<- src
.savebmarks
;
3320 dst
.memlimit
<- src
.memlimit
;
3321 dst
.proportional
<- src
.proportional
;
3322 dst
.texcount
<- src
.texcount
;
3323 dst
.sliceheight
<- src
.sliceheight
;
3324 dst
.thumbw
<- src
.thumbw
;
3325 dst
.jumpback
<- src
.jumpback
;
3326 dst
.bgcolor
<- src
.bgcolor
;
3327 dst
.scrollbarinpm
<- src
.scrollbarinpm
;
3328 dst
.uifont
<- src
.uifont
;
3332 let l = String.length
s in
3333 let b = Buffer.create
l in
3339 let h = Hashtbl.create
10 in
3340 let dc = { defconf with angle
= defconf.angle
} in
3341 let rec toplevel v t spos
_ =
3343 | Vdata
| Vcdata
| Vend
-> v
3344 | Vopen
("llppconfig", _, closed
) ->
3347 else { v with f = llppconfig
}
3349 error
"unexpected subelement at top level" s spos
3350 | Vclose
_ -> error
"unexpected close at top level" s spos
3352 and llppconfig
v t spos
_ =
3354 | Vdata
| Vcdata
| Vend
-> v
3355 | Vopen
("defaults", attrs
, closed
) ->
3356 let c = config_of dc attrs
in
3360 else { v with f = skip
"defaults" (fun () -> v) }
3362 | Vopen
("doc", attrs
, closed
) ->
3363 let pathent, spage
, srely
, span
= doc_of attrs
in
3364 let path = unent pathent
3365 and pageno = fromstring int_of_string spos
"page" spage
0
3366 and rely
= fromstring float_of_string spos
"rely" srely
0.0
3367 and pan = fromstring int_of_string spos
"pan" span
0 in
3368 let c = config_of dc attrs
in
3369 let anchor = (pageno, rely
) in
3371 then (Hashtbl.add
h path (c, [], pan, anchor); v)
3372 else { v with f = doc
path pan anchor c [] }
3375 error
"unexpected subelement in llppconfig" s spos
3377 | Vclose
"llppconfig" -> { v with f = toplevel }
3378 | Vclose
_ -> error
"unexpected close in llppconfig" s spos
3380 and doc
path pan anchor c bookmarks v t spos
_ =
3382 | Vdata
| Vcdata
-> v
3383 | Vend
-> error
"unexpected end of input in doc" s spos
3384 | Vopen
("bookmarks", _, closed
) ->
3387 else { v with f = pbookmarks
path pan anchor c bookmarks }
3389 | Vopen
(_, _, _) ->
3390 error
"unexpected subelement in doc" s spos
3393 Hashtbl.add
h path (c, List.rev
bookmarks, pan, anchor);
3394 { v with f = llppconfig
}
3396 | Vclose
_ -> error
"unexpected close in doc" s spos
3398 and pbookmarks
path pan anchor c bookmarks v t spos
_ =
3400 | Vdata
| Vcdata
-> v
3401 | Vend
-> error
"unexpected end of input in bookmarks" s spos
3402 | Vopen
("item", attrs
, closed
) ->
3403 let titleent, spage
, srely
= bookmark_of attrs
in
3404 let page = fromstring int_of_string spos
"page" spage
0
3405 and rely
= fromstring float_of_string spos
"rely" srely
0.0 in
3406 let bookmarks = (unent titleent, 0, (page, rely
)) :: bookmarks in
3408 then { v with f = pbookmarks
path pan anchor c bookmarks }
3411 { v with f = skip
"item" f }
3414 error
"unexpected subelement in bookmarks" s spos
3416 | Vclose
"bookmarks" ->
3417 { v with f = doc
path pan anchor c bookmarks }
3419 | Vclose
_ -> error
"unexpected close in bookmarks" s spos
3421 and skip tag
f v t spos
_ =
3423 | Vdata
| Vcdata
-> v
3425 error
("unexpected end of input in skipped " ^ tag
) s spos
3426 | Vopen
(tag'
, _, closed
) ->
3430 let f'
() = { v with f = skip tag
f } in
3431 { v with f = skip tag'
f'
}
3435 else error
("unexpected close in skipped " ^ tag
) s spos
3438 parse
{ f = toplevel; accu = () } s;
3444 let len = in_channel_length ic
in
3445 let s = String.create
len in
3446 really_input ic
s 0 len;
3449 | Parse_error
(msg
, s, pos) ->
3450 let subs = subs s pos in
3451 let s = Printf.sprintf
"%s: at %d [..%s..]" msg
pos subs in
3452 failwith
("parse error: " ^
s)
3455 failwith
("config load error: " ^
Printexc.to_string exn
)
3461 let dir = Filename.concat
home ".config" in
3462 if Sys.is_directory
dir then dir else home
3465 Filename.concat
dir "llpp.conf"
3469 if Sys.file_exists
path
3472 (try Some
(open_in_bin
path)
3475 ("Error opening configuation file `" ^
path ^
"': " ^
3476 Printexc.to_string exn
);
3485 ("Error loading configuation from `" ^
path ^
"': " ^
3486 Printexc.to_string exn
);
3492 f (Hashtbl.create
0, defconf)
3497 let pc, pb
, px
, pa
=
3499 Hashtbl.find h (Filename.basename
state.path)
3500 with Not_found
-> dc, [], 0, (0, 0.0)
3504 state.bookmarks <- pb
;
3506 state.scrollw
<- conf.scrollbw
;
3508 then state.anchor <- pa
;
3509 cbput state.hists
.nav pa
;
3514 let add_attrs bb always
dc c =
3517 then Printf.bprintf bb
"\n %s='%b'" s a
3520 then Printf.bprintf bb
"\n %s='%d'" s a
3523 then Printf.bprintf bb
"\n %s='%f'" s (a*.100.)
3527 Printf.bprintf bb
"\n %s='%s'" s (color_to_string a)
3531 Printf.bprintf bb
"\n %s='%s'" s a
3535 then dc.winw
, dc.winh
3537 match state.fullscreen
with
3539 | None
-> c.winw
, c.winh
3541 let zoom, presentation, interpagespace, showall
=
3543 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
3545 match state.mode with
3546 | Birdseye
(bc
, _, _, _, _) ->
3547 bc
.zoom, bc
.presentation, bc
.interpagespace, bc
.showall
3548 | _ -> c.zoom, c.presentation, c.interpagespace, c.showall
3550 oi
"width" w dc.winw
;
3551 oi
"height" h dc.winh
;
3552 oi
"scroll-bar-width" c.scrollbw
dc.scrollbw
;
3553 oi
"scroll-handle-height" c.scrollh
dc.scrollh
;
3554 ob "case-insensitive-search" c.icase
dc.icase
;
3555 ob "preload" c.preload dc.preload;
3556 oi
"page-bias" c.pagebias
dc.pagebias
;
3557 oi
"scroll-step" c.scrollstep
dc.scrollstep
;
3558 oi
"auto-scroll-step" c.autoscrollstep
dc.autoscrollstep
;
3559 ob "max-height-fit" c.maxhfit
dc.maxhfit
;
3560 ob "crop-hack" c.crophack
dc.crophack
;
3561 ob "throttle" showall
dc.showall
;
3562 ob "highlight-links" c.hlinks
dc.hlinks
;
3563 ob "under-cursor-info" c.underinfo
dc.underinfo
;
3564 oi
"vertical-margin" interpagespace dc.interpagespace;
3565 oz
"zoom" zoom dc.zoom;
3566 ob "presentation" presentation dc.presentation;
3567 oi
"rotation-angle" c.angle
dc.angle
;
3568 ob "persistent-bookmarks" c.savebmarks
dc.savebmarks
;
3569 ob "proportional-display" c.proportional
dc.proportional
;
3570 oi
"pixmap-cache-size" c.memlimit
dc.memlimit
;
3571 oi
"texcount" c.texcount
dc.texcount
;
3572 oi
"slice-height" c.sliceheight
dc.sliceheight
;
3573 oi
"thumbnail-width" c.thumbw
dc.thumbw
;
3574 ob "persistent-location" c.jumpback
dc.jumpback
;
3575 oc
"background-color" c.bgcolor
dc.bgcolor
;
3576 ob "scrollbar-in-presentation" c.scrollbarinpm
dc.scrollbarinpm
;
3577 os
"ui-font" c.uifont
dc.uifont
;
3581 let bb = Buffer.create
32768 in
3583 let dc = if conf.bedefault
then conf else dc in
3584 Buffer.add_string
bb "<llppconfig>\n<defaults ";
3585 add_attrs bb true dc dc;
3586 Buffer.add_string
bb "/>\n";
3588 let adddoc path pan anchor c bookmarks =
3589 if bookmarks == [] && c = dc && anchor = emptyanchor
3592 Printf.bprintf
bb "<doc path='%s'"
3593 (enent
path 0 (String.length
path));
3595 if anchor <> emptyanchor
3597 let n, y = anchor in
3598 Printf.bprintf
bb " page='%d'" n;
3601 Printf.bprintf
bb " rely='%f'" y
3606 then Printf.bprintf
bb " pan='%d'" pan;
3608 add_attrs bb false dc c;
3610 begin match bookmarks with
3611 | [] -> Buffer.add_string
bb "/>\n"
3613 Buffer.add_string
bb ">\n<bookmarks>\n";
3614 List.iter
(fun (title, _level
, (page, rely
)) ->
3616 "<item title='%s' page='%d'"
3617 (enent
title 0 (String.length
title))
3622 Printf.bprintf
bb " rely='%f'" rely
3624 Buffer.add_string
bb "/>\n";
3626 Buffer.add_string
bb "</bookmarks>\n</doc>\n";
3632 match state.mode with
3633 | Birdseye
(_, pan, _, _, _) -> pan
3636 let basename = Filename.basename state.path in
3637 adddoc basename pan (getanchor ())
3640 if state.ascrollstep
> 0
3641 then state.ascrollstep
3642 else conf.autoscrollstep
}
3643 (if conf.savebmarks
then state.bookmarks else []);
3645 Hashtbl.iter
(fun path (c, bookmarks, x, y) ->
3647 then adddoc path x y c bookmarks
3649 Buffer.add_string
bb "</llppconfig>";
3652 if Buffer.length
bb > 0
3655 let tmp = path ^
".tmp" in
3656 let oc = open_out_bin
tmp in
3657 Buffer.output_buffer
oc bb;
3659 Sys.rename
tmp path;
3662 ("error while saving configuration: " ^
Printexc.to_string exn
)
3669 [("-p", Arg.String
(fun s -> state.password
<- s) , " Set password")
3670 ;("-v", Arg.Unit
(fun () -> print_endline
Help.version
; exit
0),
3671 " Print version and exit")]
3673 (fun s -> state.path <- s)
3674 ("Usage: " ^
Sys.argv
.(0) ^
" [options] some.pdf\nOptions:")
3676 if String.length
state.path = 0
3677 then (prerr_endline
"file name missing"; exit
1);
3681 let _ = Glut.init
Sys.argv
in
3682 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
3683 let () = Glut.initWindowSize
conf.winw
conf.winh
in
3684 let _ = Glut.createWindow
("llpp " ^
Filename.basename state.path) in
3687 if Sys.os_type
= "Unix"
3689 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
3691 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
3692 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
3693 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
3694 Unix.bind
sock addr;
3696 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
3697 Unix.connect
csock addr;
3698 let ssock, _ = Unix.accept
sock in
3701 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
3702 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
3706 at_exit
(fun () -> Unix.shutdown
ssock Unix.SHUTDOWN_ALL
);
3710 let () = Glut.displayFunc
display in
3711 let () = Glut.reshapeFunc
reshape in
3712 let () = Glut.keyboardFunc
keyboard in
3713 let () = Glut.specialFunc
special in
3714 let () = Glut.idleFunc
(Some
idle) in
3715 let () = Glut.mouseFunc
mouse in
3716 let () = Glut.motionFunc
motion in
3717 let () = Glut.passiveMotionFunc
pmotion in
3720 conf.angle
, conf.proportional
, conf.texcount
,
3721 conf.sliceheight
, conf.uifont
3723 state.csock <- csock;
3724 state.ssock <- ssock;
3725 state.text <- "Opening " ^
state.path;
3726 writeopen state.path state.password
;
3730 let rec handlelablglutbug () =
3733 with Glut.BadEnum
"key in special_of_int" ->
3734 showtext '
!'
" LablGlut bug: special key not recognized";
3735 handlelablglutbug ()
3737 handlelablglutbug ();