4 | Ulinkgoto
of (int * int)
6 and facename
= string;;
8 let dolog fmt
= Printf.kprintf prerr_endline fmt
;;
13 angle
* proportional
* texcount
* sliceheight
* blockwidth
* fontpath
22 and proportional
= bool
23 and interpagespace
= int
32 external init
: Unix.file_descr
-> params
-> unit = "ml_init";;
33 external draw
: (int * int * int * bool) -> string -> unit = "ml_draw";;
34 external seltext
: string -> (int * int * int * int) -> int -> unit =
36 external copysel
: string -> unit = "ml_copysel";;
37 external getpdimrect
: int -> float array
= "ml_getpdimrect";;
38 external whatsunder
: string -> int -> int -> under
= "ml_whatsunder";;
39 external zoomforh
: int -> int -> int -> float = "ml_zoom_for_height";;
40 external drawstr
: int -> int -> int -> string -> float = "ml_draw_string";;
41 external measurestr
: int -> string -> float = "ml_measure_string";;
42 external getmaxw
: unit -> float = "ml_getmaxw";;
46 | Msel
of (mpos
* mpos
)
49 | Mzoom
of (int * int)
53 type textentry
= string * string * onhist
option * onkey
* ondone
54 and onkey
= string -> int -> te
55 and ondone
= string -> unit
56 and histcancel
= unit -> unit
57 and onhist
= ((histcmd
-> string) * histcancel
)
58 and histcmd
= HCnext
| HCprev
| HCfirst
| HClast
63 | TEswitch
of textentry
75 { store
= Array.create n v
82 let cbcap b
= Array.length b
.store
;;
87 b
.wc
<- (b
.wc
+ 1) mod cap;
89 b
.len
<- min
(b
.len
+ 1) cap;
92 let cbempty b
= b
.len
= 0;;
94 let cbgetg b circular dir
=
98 let rc = b
.rc + dir
in
110 else max
0 (min
rc (b
.len
-1))
116 let cbget b
= cbgetg b
false;;
117 let cbgetc b
= cbgetg b
true;;
120 let rc = b
.wc
- b
.len
in
121 let rc = if rc < 0 then cbcap b
+ rc else rc in
125 let cbdecr b
= b
.len
<- b
.len
- 1;;
140 { mutable scrollbw
: int
141 ; mutable scrollh
: int
142 ; mutable icase
: bool
143 ; mutable preload
: bool
144 ; mutable pagebias
: int
145 ; mutable verbose
: bool
146 ; mutable scrollstep
: int
147 ; mutable maxhfit
: bool
148 ; mutable crophack
: bool
149 ; mutable autoscrollstep
: int
150 ; mutable showall
: bool
151 ; mutable hlinks
: bool
152 ; mutable underinfo
: bool
153 ; mutable interpagespace
: interpagespace
154 ; mutable zoom
: float
155 ; mutable presentation
: bool
156 ; mutable angle
: angle
159 ; mutable savebmarks
: bool
160 ; mutable proportional
: proportional
161 ; mutable memlimit
: int
162 ; mutable texcount
: texcount
163 ; mutable sliceheight
: sliceheight
164 ; mutable blockwidth
: blockwidth
165 ; mutable thumbw
: width
166 ; mutable jumpback
: bool
167 ; mutable bgcolor
: float * float * float
168 ; mutable bedefault
: bool
169 ; mutable scrollbarinpm
: bool
173 type anchor
= pageno
* top
;;
175 type outline
= string * int * anchor
177 | Oarray
of outline array
178 | Olist
of outline list
179 | Onarrow
of string * outline array
* outline array
182 type rect
= float * float * float * float * float * float * float * float;;
184 type pagemapkey
= pageno
* width
* angle
* proportional
* gen
;;
186 let emptyanchor = (0, 0.0);;
189 | Birdseye
of (conf
* leftx
* pageno
* pageno
* anchor
)
190 | Outline
of (bool * int * int * outline array
* string * int * mode
)
191 | Items
of (int * int * item array
* string * int * mode
)
192 | Textentry
of (textentry
* onleave
)
194 and onleave
= leavetextentrystatus
-> unit
195 and leavetextentrystatus
= | Cancel
| Confirm
196 and item
= string * int * action
199 | Action
of (int -> int -> string -> int -> mode
)
202 let isbirdseye = function Birdseye _
-> true | _
-> false;;
203 let istextentry = function Textentry _
-> true | _
-> false;;
206 { mutable csock
: Unix.file_descr
207 ; mutable ssock
: Unix.file_descr
211 ; mutable scrollw
: int
212 ; mutable anchor
: anchor
214 ; mutable layout
: layout list
215 ; pagemap
: (pagemapkey
, (opaque
* pixmapsize
)) Hashtbl.t
216 ; mutable pdims
: (pageno
* width
* height
* leftx
) list
217 ; mutable pagecount
: int
218 ; pagecache
: string circbuf
219 ; mutable rendering
: bool
220 ; mutable mstate
: mstate
221 ; mutable searchpattern
: string
222 ; mutable rects
: (pageno
* recttype
* rect
) list
223 ; mutable rects1
: (pageno
* recttype
* rect
) list
224 ; mutable text
: string
225 ; mutable fullscreen
: (width
* height
) option
226 ; mutable mode
: mode
227 ; mutable outlines
: outlines
228 ; mutable bookmarks
: outline list
229 ; mutable path
: string
230 ; mutable password
: string
231 ; mutable invalidated
: int
232 ; mutable colorscale
: float
233 ; mutable memused
: int
235 ; mutable throttle
: layout list
option
236 ; mutable autoscroll
:int option
237 ; mutable help
: item array
238 ; mutable docinfo
: (int * string) list
239 ; mutable deadline
: float
243 { pat
: string circbuf
244 ; pag
: string circbuf
245 ; nav
: anchor circbuf
265 ; presentation
= false
270 ; proportional
= true
271 ; memlimit
= 32*1024*1024
277 ; bgcolor
= (0.5, 0.5, 0.5)
279 ; scrollbarinpm
= true
283 let conf = { defconf with angle
= defconf.angle
};;
286 let strings = ("llpp version " ^
Help.version
) :: "" :: Help.keys
in
287 Array.of_list
(List.map
(fun s
-> s
, 0, Noaction
) strings);
297 ; anchor
= emptyanchor
300 ; pagemap
= Hashtbl.create
10
301 ; pagecache
= cbnew 100 ""
312 ; outlines
= Olist
[]
318 { nav
= cbnew 100 (0, 0.0)
336 Printf.kprintf prerr_endline fmt
338 Printf.kprintf ignore fmt
342 let len = String.length s
in
344 let b = Buffer.create
n in
345 Buffer.add_char
b (Char.chr
((len lsr 24) land 0xff));
346 Buffer.add_char
b (Char.chr
((len lsr 16) land 0xff));
347 Buffer.add_char
b (Char.chr
((len lsr 8) land 0xff));
348 Buffer.add_char
b (Char.chr
((len lsr 0) land 0xff));
349 Buffer.add_string
b s
;
350 let s'
= Buffer.contents
b in
351 let n'
= Unix.write fd
s'
0 n in
352 if n'
!= n then failwith
"write failed";
357 let n = Unix.read fd
s 0 4 in
358 if n != 4 then failwith
"incomplete read(len)";
360 lor (Char.code
s.[0] lsl 24)
361 lor (Char.code
s.[1] lsl 16)
362 lor (Char.code
s.[2] lsl 8)
363 lor (Char.code
s.[3] lsl 0)
365 let s = String.create
len in
366 let n = Unix.read fd
s 0 len in
367 if n != len then failwith
"incomplete read(data)";
372 let b = Buffer.create
10 in
373 Buffer.add_string
b s;
374 let rec combine = function
377 Buffer.add_char
b ' '
;
380 | `
b b -> if b then "1" else "0"
382 | `i i
-> string_of_int i
383 | `f f
-> string_of_float f
384 | `I f
-> string_of_int
(truncate f
)
386 Buffer.add_string
b s;
393 let cmd = Buffer.contents
(makecmd s l
) in
394 writecmd state.csock
cmd;
400 let d = conf.winh
- h
in
407 let rec f pn ph pi fh l
=
409 | (n, _
, h
, _
) :: rest
->
410 let ips = calcips h
in
415 if isbirdseye state.mode
&& pn
= 0
420 let fh = fh + ((n - pn
) * (ph
+ pi
)) in
425 if conf.presentation
|| (isbirdseye state.mode
&& pn
= 0)
429 let fh = fh + ((state.pagecount
- pn
) * (ph
+ pi
)) + inc in
432 let fh = f 0 0 0 0 state.pdims
in
436 let getpageyh pageno
=
437 let rec f pn ph pi y l
=
439 | (n, _
, h
, _
) :: rest
->
440 let ips = calcips h
in
443 let h = if n = pageno
then h else ph
in
444 if conf.presentation
&& n = pageno
446 y
+ (pageno
- pn
) * (ph
+ pi
) + pi
, h
448 y
+ (pageno
- pn
) * (ph
+ pi
), h
450 let y = y + (if conf.presentation
then pi
else 0) in
451 let y = y + (n - pn
) * (ph
+ pi
) in
455 y + (pageno
- pn
) * (ph
+ pi
), ph
457 f 0 0 0 0 state.pdims
460 let getpagey pageno
= fst
(getpageyh pageno
);;
463 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu
=
464 let ((w
, h, ips, x
) as curr
), rest
, pdimno
, yinc
=
466 | (pageno'
, w
, h, x
) :: rest
when pageno'
= pageno
->
467 let ips = calcips h in
469 if conf.presentation
|| (isbirdseye state.mode
&& pageno
= 0)
473 (w
, h, ips, x
), rest
, pdimno
+ 1, yinc
475 prev
, pdims
, pdimno
, 0
477 let dy = dy + yinc in
478 let py = py + yinc in
479 if pageno
= state.pagecount
|| cacheleft
= 0 || dy >= sh
484 if py + h <= vy - yinc
486 let py = py + h + ips in
487 let dy = max
0 (py - y) in
497 let pagey = vy - py in
498 let pagevh = h - pagey in
499 let pagevh = min
(sh
- dy) pagevh in
500 let off = if yinc > 0 then py - vy else 0 in
501 let py = py + h + ips in
507 ; pagedispy
= dy + off
508 ; pagey = pagey + off
509 ; pagevh = pagevh - off
513 let accu = e :: accu in
520 ~cacheleft
:(cacheleft
-1)
523 if state.invalidated
= 0
533 ~cacheleft
:(cbcap state.pagecache
)
543 let y = state.y + incr
in
545 let y = min
y (state.maxy
- (if conf.maxhfit
then conf.winh
else 0)) in
549 let getopaque pageno
=
550 try Some
(Hashtbl.find
state.pagemap
551 (pageno
, state.w
, conf.angle
, conf.proportional
, state.gen
))
552 with Not_found
-> None
555 let cache pageno opaque
=
556 Hashtbl.replace
state.pagemap
557 (pageno
, state.w
, conf.angle
, conf.proportional
, state.gen
) opaque
560 let validopaque opaque
= String.length opaque
> 0;;
563 match getopaque l
.pageno
with
564 | None
when not
state.rendering
->
565 state.rendering
<- true;
566 cache l
.pageno
("", -1);
567 wcmd "render" [`i
(l
.pageno
+ 1)
574 let loadlayout layout =
575 let rec f all
= function
577 begin match getopaque l
.pageno
with
578 | None
-> render l
; f false ls
579 | Some
(opaque
, _
) -> f (all
&& validopaque opaque
) ls
583 f (layout <> []) layout;
586 let findpageforopaque opaque
=
588 (fun k
(v
, s) a
-> if v
= opaque
then Some
(k
, s) else a
)
592 let pagevisible layout n = List.exists
(fun l
-> l
.pageno
= n) layout;;
598 let memleft = conf.memlimit
- state.memused
in
601 let opaque = cbpeek state.pagecache
in
602 match findpageforopaque opaque with
603 | Some
((n, _
, _
, _
, _
), size
) ->
604 memleft + size
>= 0 && not
(pagevisible state.layout n)
611 let presentation = conf.presentation in
612 let interpagespace = conf.interpagespace in
613 let maxy = state.maxy in
614 conf.presentation <- false;
615 conf.interpagespace <- 0;
616 state.maxy <- calcheight ();
618 match state.layout with
620 | l
:: _
-> getpagey l
.pageno
+ l
.pagey
622 let y = if y < conf.winh
then 0 else y - conf.winh
in
623 let h = state.y - y + conf.winh
*3 in
624 let pages = layout y h in
625 List.iter
render pages;
626 conf.presentation <- presentation;
627 conf.interpagespace <- interpagespace;
633 let y = min
state.maxy y in
634 let pages = layout y conf.winh
in
635 let ready = loadlayout pages in
641 state.layout <- pages;
642 state.throttle
<- None
;
643 Glut.postRedisplay
();
646 state.throttle
<- Some
pages;
651 state.layout <- pages;
652 state.throttle
<- None
;
653 Glut.postRedisplay
();
655 begin match state.mode
with
656 | Birdseye
(conf, leftx
, pageno
, hooverpageno
, anchor
) ->
657 if not
(pagevisible pages pageno
)
659 match state.layout with
662 state.mode
<- Birdseye
(conf, leftx
, l
.pageno
, hooverpageno
, anchor
)
669 let gotoy_and_clear_text y =
671 if not
conf.verbose
then state.text
<- "";
675 match state.layout with
677 | l
:: _
-> (l
.pageno
, float l
.pagey /. float l
.pageh
)
680 let getanchory (n, top
) =
681 let y, h = getpageyh n in
682 y + (truncate
(top
*. float h));
685 let gotoanchor anchor
=
686 gotoy (getanchory anchor
);
690 cbput state.hists
.nav
(getanchor ());
694 let anchor = cbgetc state.hists
.nav dir
in
699 let y, h = getpageyh n in
700 gotoy_and_clear_text (y + (truncate
(top
*. float h)));
703 let gotopage1 n top
=
704 let y = getpagey n in
705 gotoy_and_clear_text (y + top
);
713 state.invalidated
<- state.invalidated
+ 1;
717 let c = c *. state.colorscale
in
721 let scalecolor2 (r
, g
, b) =
722 (r
*. state.colorscale
, g
*. state.colorscale
, b *. state.colorscale
);
726 state.maxy <- calcheight ();
727 match state.mode
with
728 | Birdseye
(_
, _
, pageno
, _
, _
) ->
729 let y, h = getpageyh pageno
in
730 let top = (conf.winh
- h) / 2 in
731 gotoy (max
0 (y - top))
732 | _
-> gotoanchor state.anchor
736 GlMat.mode `projection
;
737 GlMat.load_identity
();
738 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
739 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
740 GlMat.scale3
(2.0 /. float state.w
, 2.0 /. float conf.winh
, 1.0);
743 GlMat.translate ~x
:(float state.x
) ();
748 GlMat.mode `projection
;
749 GlMat.load_identity
();
750 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
751 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
752 GlMat.scale3
(2.0 /. float conf.winw
, 2.0 /. float conf.winh
, 1.0);
756 let firsttime = ref true in
758 if state.invalidated
= 0 && not
!firsttime
759 then state.anchor <- getanchor ();
763 let w = truncate
(float w *. conf.zoom
) - state.scrollw
in
767 GlMat.mode `modelview
;
768 GlMat.load_identity
();
769 GlClear.color
(scalecolor 1.0);
770 GlClear.clear
[`color
];
773 wcmd "geometry" [`i
w; `i
h];
776 let drawstring size x
y s =
778 Gl.enable `texture_2d
;
779 ignore
(drawstr size x
y s);
781 Gl.disable `texture_2d
;
784 let drawstring1 size x
y s =
789 let len = String.length
state.text
in
791 GlDraw.color
(0.0, 0.0, 0.0);
793 (0.0, float (conf.winh
- 18))
794 (float (conf.winw
- state.scrollw
- 1), float conf.winh
)
796 GlDraw.color
(1.0, 1.0, 1.0);
797 drawstring 14 (if len > 0 then 8 else 2) (conf.winh
- 5) s;
799 match state.mode
with
800 | Textentry
((prefix
, text
, _
, _
, _
), _
) ->
804 Printf.sprintf
"%s%s_ [%s]" prefix text
state.text
806 Printf.sprintf
"%s%s_" prefix text
811 if len > 0 then drawstring state.text
815 state.text
<- Printf.sprintf
"%c%s" c s;
816 Glut.postRedisplay
();
825 state.rects
<- state.rects1
;
826 Glut.postRedisplay
()
829 let n = Scanf.sscanf
cmd "C %u" (fun n -> n) in
830 state.pagecount
<- n;
831 state.invalidated
<- state.invalidated
- 1;
832 if state.invalidated
= 0
836 let s = Scanf.sscanf
cmd "t %n"
837 (fun n -> String.sub
cmd n (String.length
cmd - n))
839 Glut.setWindowTitle
s
842 let s = Scanf.sscanf
cmd "T %n"
843 (fun n -> String.sub
cmd n (String.length
cmd - n))
845 if istextentry state.mode
852 Glut.postRedisplay
();
858 let s = Scanf.sscanf
cmd "V %n"
859 (fun n -> String.sub
cmd n (String.length
cmd - n))
865 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
866 Scanf.sscanf
cmd "F %u %d %f %f %f %f %f %f %f %f"
867 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
868 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
870 let y = (getpagey pageno) + truncate y0
in
873 state.rects1
<- [pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)]
876 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
877 Scanf.sscanf
cmd "R %u %d %f %f %f %f %f %f %f %f"
878 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
879 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
882 (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) :: state.rects1
885 let n, w, _h
, r
, l
, s, p
=
886 Scanf.sscanf
cmd "r %u %u %u %d %d %u %s"
887 (fun n w h r l
s p
->
888 (n-1, w, h, r
, l
!= 0, s, p
))
891 Hashtbl.replace
state.pagemap
(n, w, r
, l
, state.gen
) (p
, s);
892 state.memused
<- state.memused
+ s;
895 match state.throttle
with
896 | None
-> state.layout
897 | Some
layout -> layout
901 if (state.memused
<= conf.memlimit
) || cbempty state.pagecache
904 let evictedopaque = cbpeek state.pagecache
in
905 match findpageforopaque evictedopaque with
906 | None
-> failwith
"bug in gc"
907 | Some
((evictedn
, _
, _
, _
, gen
) as k
, evictedsize
) ->
908 if state.gen
!= gen
|| not
(pagevisible layout evictedn
)
910 wcmd "free" [`
s evictedopaque];
911 state.memused
<- state.memused
- evictedsize
;
912 Hashtbl.remove
state.pagemap k
;
913 cbdecr state.pagecache
;
920 cbput state.pagecache p
;
921 state.rendering
<- false;
923 begin match state.throttle
with
925 if pagevisible state.layout n
928 let allvisible = loadlayout state.layout in
929 if allvisible then preload ();
936 let y = getpagey l
.pageno + l
.pagey in
942 Scanf.sscanf
cmd "l %u %u %u %u" (fun n w h x
-> n, w, h, x
)
944 state.pdims
<- pdim :: state.pdims
947 let (l
, n, t
, h, pos
) =
948 Scanf.sscanf
cmd "o %u %u %d %u %n" (fun l
n t
h pos
-> l
, n, t
, h, pos
)
950 let s = String.sub
cmd pos
(String.length
cmd - pos
) in
951 let outline = (s, l
, (n, float t
/. float h)) in
953 match state.outlines with
954 | Olist
outlines -> Olist
(outline :: outlines)
955 | Oarray _
-> Olist
[outline]
956 | Onarrow _
-> Olist
[outline]
958 state.outlines <- outlines
962 if String.length
cmd > 1 && cmd.[1] = '
e'
964 state.docinfo
<- List.rev
state.docinfo
966 let s = Scanf.sscanf
cmd "i %n"
967 (fun n -> String.sub
cmd n (String.length
cmd - n))
969 state.docinfo
<- (1, s) :: state.docinfo
972 dolog "unknown cmd `%S'" cmd
975 let now = Unix.gettimeofday
;;
978 if state.deadline
== nan
then state.deadline
<- now ();
982 then max
0.0 (state.deadline
-. now ())
985 let r, _
, _
= Unix.select
[state.csock
] [] [] timeout in
986 state.deadline
<- state.deadline
+. delay
;
989 begin match state.autoscroll
with
990 | Some step
when step
!= 0 ->
991 let y = state.y + step
in
995 else if y >= state.maxy then 0 else y
999 then state.text
<- "";
1004 let cmd = readcmd state.csock
in
1013 let action = function
1014 | HCprev
-> cbget cb ~
-1
1015 | HCnext
-> cbget cb
1
1016 | HCfirst
-> cbget cb ~
-(cb
.rc)
1017 | HClast
-> cbget cb
(cb
.len - 1 - cb
.rc)
1018 and cancel
() = cb
.rc <- rc
1022 let search pattern forward
=
1023 if String.length pattern
> 0
1026 match state.layout with
1029 l
.pageno, (l
.pagey + if forward
then 0 else 0*l
.pagevh)
1032 let b = makecmd "search"
1033 [`
b conf.icase
; `i
pn; `i
py; `i
(if forward
then 1 else 0)]
1035 Buffer.add_char
b '
,'
;
1036 Buffer.add_string
b pattern
;
1037 Buffer.add_char
b '
\000'
;
1040 writecmd state.csock
cmd;
1043 let intentry text key
=
1044 let c = Char.unsafe_chr key
in
1047 let s = "x" in s.[0] <- c;
1048 let text = text ^
s in
1052 state.text <- Printf.sprintf
"invalid char (%d, `%c')" key
c;
1057 let b = Buffer.create
(String.length
s + 1) in
1058 Buffer.add_string
b s;
1059 Buffer.add_char
b c;
1063 let textentry text key
=
1064 let c = Char.unsafe_chr key
in
1066 | _
when key
>= 32 && key
< 127 ->
1067 let text = addchar text c in
1071 dolog "unhandled key %d char `%c'" key
(Char.unsafe_chr key
);
1075 let reinit angle proportional
=
1076 conf.angle
<- angle
;
1077 conf.proportional
<- proportional
;
1079 wcmd "reinit" [`i angle
; `
b proportional
];
1083 let zoom = max
0.01 zoom in
1084 if zoom <> conf.zoom
1089 reshape conf.winw
conf.winh
;
1090 state.text <- Printf.sprintf
"zoom is now %-5.1f" (zoom *. 100.0);
1094 let enterbirdseye () =
1095 let zoom = float conf.thumbw
/. float conf.winw
in
1096 let birdseyepageno =
1097 let cy = conf.winh
/ 2 in
1101 let rec fold best
= function
1104 let d = cy - (l
.pagedispy
+ l
.pagevh/2)
1105 and dbest
= cy - (best
.pagedispy
+ best
.pagevh/2) in
1106 if abs
d < abs dbest
1113 state.mode
<- Birdseye
(
1114 { conf with zoom = conf.zoom }, state.x
, birdseyepageno, -1, getanchor ()
1117 conf.presentation <- false;
1118 conf.interpagespace <- 10;
1119 conf.hlinks
<- false;
1121 state.mstate
<- Mnone
;
1122 conf.showall
<- false;
1123 Glut.setCursor
Glut.CURSOR_INHERIT
;
1126 state.text <- Printf.sprintf
"birds eye mode on (zoom %3.1f%%)"
1131 reshape conf.winw
conf.winh
;
1134 let leavebirdseye (c, leftx
, pageno, _
, anchor) goback
=
1136 conf.zoom <- c.zoom;
1137 conf.presentation <- c.presentation;
1138 conf.interpagespace <- c.interpagespace;
1139 conf.showall
<- c.showall
;
1140 conf.hlinks
<- c.hlinks
;
1144 state.text <- Printf.sprintf
"birds eye mode off (zoom %3.1f%%)"
1147 reshape conf.winw
conf.winh
;
1148 state.anchor <- if goback
then anchor else (pageno, 0.0);
1151 let togglebirdseye () =
1152 match state.mode
with
1153 | Birdseye vals
-> leavebirdseye vals
true
1154 | View
| Outline _
-> enterbirdseye ()
1158 let upbirdseye (conf, leftx
, pageno, hooverpageno
, anchor) =
1159 let pageno = max
0 (pageno - 1) in
1160 let rec loop = function
1161 | [] -> gotopage1 pageno 0
1162 | l
:: _
when l
.pageno = pageno ->
1163 if l
.pagedispy
>= 0 && l
.pagey = 0
1164 then Glut.postRedisplay
()
1165 else gotopage1 pageno 0
1166 | _
:: rest
-> loop rest
1169 state.mode
<- Birdseye
(conf, leftx
, pageno, hooverpageno
, anchor)
1172 let downbirdseye (conf, leftx
, pageno, hooverpageno
, anchor) =
1173 let pageno = min
(state.pagecount
- 1) (pageno + 1) in
1174 state.mode
<- Birdseye
(conf, leftx
, pageno, hooverpageno
, anchor);
1175 let rec loop = function
1177 let y, h = getpageyh pageno in
1178 let dy = (y - state.y) - (conf.winh
- h - conf.interpagespace) in
1180 | l
:: _
when l
.pageno = pageno ->
1181 if l
.pagevh != l
.pageh
1182 then gotoy (clamp (l
.pageh
- l
.pagevh + conf.interpagespace))
1183 else Glut.postRedisplay
()
1184 | _
:: rest
-> loop rest
1189 let optentry mode _ key
=
1190 let btos b = if b then "on" else "off" in
1191 let c = Char.unsafe_chr key
in
1195 try conf.scrollstep
<- int_of_string
s with exc
->
1196 state.text <- Printf.sprintf
"bad integer `%s': %s"
1197 s (Printexc.to_string exc
)
1199 TEswitch
("scroll step: ", "", None
, intentry, ondone)
1204 conf.autoscrollstep
<- int_of_string
s;
1205 if state.autoscroll
<> None
1206 then state.autoscroll
<- Some
conf.autoscrollstep
1208 state.text <- Printf.sprintf
"bad integer `%s': %s"
1209 s (Printexc.to_string exc
)
1211 TEswitch
("auto scroll step: ", "", None
, intentry, ondone)
1216 let zoom = float (int_of_string
s) /. 100.0 in
1219 state.text <- Printf.sprintf
"bad integer `%s': %s"
1220 s (Printexc.to_string exc
)
1222 TEswitch
("zoom: ", "", None
, intentry, ondone)
1227 conf.thumbw
<- max
2 (min
1920 (int_of_string
s));
1229 Printf.sprintf
"thumbnail width is set to %d" conf.thumbw
;
1230 begin match mode
with
1232 leavebirdseye beye
false;
1237 state.text <- Printf.sprintf
"bad integer `%s': %s"
1238 s (Printexc.to_string exc
)
1240 TEswitch
("thumbnail width: ", "", None
, intentry, ondone)
1245 Some
(int_of_string
s)
1247 state.text <- Printf.sprintf
"bad integer `%s': %s"
1248 s (Printexc.to_string exc
);
1251 | Some angle
-> reinit angle
conf.proportional
1254 TEswitch
("rotation: ", "", None
, intentry, ondone)
1257 conf.icase
<- not
conf.icase
;
1258 TEdone
("case insensitive search " ^
(btos conf.icase
))
1261 conf.preload <- not
conf.preload;
1263 TEdone
("preload " ^
(btos conf.preload))
1266 conf.verbose
<- not
conf.verbose
;
1267 TEdone
("verbose " ^
(btos conf.verbose
))
1270 conf.maxhfit
<- not
conf.maxhfit
;
1271 state.maxy <- state.maxy + (if conf.maxhfit
then -conf.winh
else conf.winh
);
1272 TEdone
("maxhfit " ^
(btos conf.maxhfit
))
1275 conf.crophack
<- not
conf.crophack
;
1276 TEdone
("crophack " ^
btos conf.crophack
)
1279 conf.showall
<- not
conf.showall
;
1280 TEdone
("throttle " ^
btos conf.showall
)
1283 conf.underinfo
<- not
conf.underinfo
;
1284 TEdone
("underinfo " ^
btos conf.underinfo
)
1287 conf.savebmarks
<- not
conf.savebmarks
;
1288 TEdone
("persistent bookmarks " ^
btos conf.savebmarks
)
1294 match state.layout with
1299 conf.interpagespace <- int_of_string
s;
1300 state.maxy <- calcheight ();
1301 let y = getpagey pageno in
1304 state.text <- Printf.sprintf
"bad integer `%s': %s"
1305 s (Printexc.to_string exc
)
1307 TEswitch
("vertical margin: ", "", None
, intentry, ondone)
1310 reinit conf.angle
(not
conf.proportional
);
1311 TEdone
("proprortional display " ^
btos conf.proportional
)
1314 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
1318 let maxoutlinerows () = (conf.winh
- 31) / 16;;
1320 let enterselector allowdel
outlines errmsg msg
=
1321 if Array.length
outlines = 0
1323 showtext ' ' errmsg
;
1327 Glut.setCursor
Glut.CURSOR_INHERIT
;
1329 match state.layout with
1331 | {pageno=pageno} :: _
-> pageno
1335 if n = Array.length
outlines
1338 let (_
, _
, (outlinepageno
, _
)) = outlines.(n) in
1339 if outlinepageno
>= pageno then n else loop (n+1)
1343 state.mode
<- Outline
1344 (allowdel
, active, max
0 (active - maxoutlinerows () / 2), outlines, "", 0,
1346 Glut.postRedisplay
();
1350 let enteroutlinemode () =
1352 match state.outlines with
1355 let a = Array.of_list
(List.rev l
) in
1356 state.outlines <- Oarray
a;
1358 | Onarrow
(pat
, a, _
) ->
1359 a, "Outline was narrowed to `" ^ pat ^
"' (Ctrl-u to restore)"
1361 enterselector false outlines "Document has no outline" msg
;
1364 let enterbookmarkmode () =
1365 let bookmarks = Array.of_list
state.bookmarks in
1366 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1369 let mode_to_string mode
=
1370 let b = Buffer.create
10 in
1371 let rec f = function
1372 | Textentry
(_
, _
) -> Buffer.add_string
b "Textentry ";
1373 | View
-> Buffer.add_string
b "View"
1374 | Birdseye _
-> Buffer.add_string
b "Birdseye"
1375 | Items _
-> Buffer.add_string
b "Items"
1376 | Outline _
-> Buffer.add_string
b "Outline"
1382 let color_of_string s =
1383 Scanf.sscanf
s "%d/%d/%d" (fun r g
b ->
1384 (float r /. 256.0, float g
/. 256.0, float b /. 256.0)
1388 let color_to_string (r, g
, b) =
1389 let r = truncate
(r *. 256.0)
1390 and g
= truncate
(g
*. 256.0)
1391 and b = truncate
(b *. 256.0) in
1392 Printf.sprintf
"%d/%d/%d" r g
b
1395 let enterinfomode () =
1396 let btos = function true -> "on" | _
-> "off" in
1397 let mode = state.mode in
1398 let rec makeitems () =
1399 let intp name get set
=
1400 Printf.sprintf
"%s\t%d" name
(get
()), 1, Action
(
1401 fun active first _ pan
->
1406 state.text <- Printf.sprintf
"bad integer `%s': %s"
1407 s (Printexc.to_string exn
);
1410 if n != max_int
then set
n;
1412 let te = name ^
": ", "", None
, intentry, ondone in
1417 state.mode <- Items
(active, first
, makeitems (), "", pan
, mode)
1420 and boolp name get set
=
1421 Printf.sprintf
"%s\t%s" name
(btos (get
())), 1, Action
(
1422 fun active first qsearch pan
->
1425 Items
(active, first
, makeitems (), qsearch
, pan
, mode);
1427 and colorp name get set
=
1428 Printf.sprintf
"%s\t%s" name
(color_to_string (get
())), 1, Action
(
1429 fun active first _ pan
->
1430 let invalid = (nan
, nan
, nan
) in
1433 try color_of_string s
1435 state.text <- Printf.sprintf
"bad color `%s': %s"
1436 s (Printexc.to_string exn
);
1442 let te = name ^
": ", "", None
, textentry, ondone in
1447 state.mode <- Items
(active, first
, makeitems (), "", pan
, mode)
1452 let birdseye = isbirdseye mode in
1454 (if birdseye then "Setup bird's eye" else "Setup"), 0, Noaction
;
1456 boolp
"presentation"
1457 (fun () -> conf.presentation)
1459 conf.presentation <- v;
1460 state.anchor <- getanchor ();
1463 boolp
"ignore case in searches"
1464 (fun () -> conf.icase
)
1465 (fun v -> conf.icase
<- v);
1468 (fun () -> conf.preload)
1469 (fun v -> conf.preload <- v);
1472 (fun () -> conf.verbose
)
1473 (fun v -> conf.verbose
<- v);
1476 (fun () -> conf.maxhfit
)
1477 (fun v -> conf.maxhfit
<- v);
1480 (fun () -> conf.crophack
)
1481 (fun v -> conf.crophack
<- v);
1484 (fun () -> conf.showall
)
1485 (fun v -> conf.showall
<- v);
1487 boolp
"highlight links"
1488 (fun () -> conf.hlinks
)
1489 (fun v -> conf.hlinks
<- v);
1492 (fun () -> conf.underinfo
)
1493 (fun v -> conf.underinfo
<- v);
1494 boolp
"persistent bookmarks"
1495 (fun () -> conf.savebmarks
)
1496 (fun v -> conf.savebmarks
<- v);
1498 boolp
"proportional display"
1499 (fun () -> conf.proportional
)
1500 (fun v -> reinit conf.angle
v);
1502 boolp
"persistent location"
1503 (fun () -> conf.jumpback
)
1504 (fun v -> conf.jumpback
<- v);
1508 intp "vertical margin"
1509 (fun () -> conf.interpagespace)
1511 conf.interpagespace <- n;
1513 match state.layout with
1518 state.maxy <- calcheight ();
1519 let y = getpagey pageno in
1524 (fun () -> conf.pagebias
)
1525 (fun v -> conf.pagebias
<- v);
1528 (fun () -> conf.scrollstep
)
1529 (fun n -> conf.scrollstep
<- n);
1531 intp "auto scroll step"
1533 match state.autoscroll
with
1535 | _
-> conf.autoscrollstep
)
1537 if state.autoscroll
<> None
1538 then state.autoscroll
<- Some
n;
1539 conf.autoscrollstep
<- n);
1542 (fun () -> truncate
(conf.zoom *. 100.))
1543 (fun v -> setzoom ((float v) /. 100.));
1546 (fun () -> conf.angle
)
1547 (fun v -> reinit v conf.proportional
);
1549 intp "scroll bar width"
1550 (fun () -> state.scrollw
)
1554 reshape conf.winw
conf.winh
;
1557 intp "scroll handle height"
1558 (fun () -> conf.scrollh
)
1559 (fun v -> conf.scrollh
<- v;);
1561 intp "thumbnail width"
1562 (fun () -> conf.thumbw
)
1564 conf.thumbw
<- min
1920 v;
1567 leavebirdseye beye
false;
1572 colorp
"background color"
1573 (fun () -> conf.bgcolor
)
1574 (fun v -> conf.bgcolor
<- v);
1577 "Presentation mode", 0, Noaction
;
1579 boolp
"scrollbar visible"
1580 (fun () -> conf.scrollbarinpm
)
1582 if v != conf.scrollbarinpm
1584 conf.scrollbarinpm
<- v;
1585 if conf.presentation
1587 state.scrollw
<- if v then conf.scrollbw
else 0;
1588 reshape conf.winw
conf.winh
;
1594 "Pixmap Cache", 0, Noaction
;
1596 intp "size (advisory)"
1597 (fun () -> conf.memlimit
)
1598 (fun v -> conf.memlimit
<- v);
1599 Printf.sprintf
"%s\t%d" "used" state.memused
, 1, Noaction
;
1606 :: ("Document", 0, Noaction
)
1607 :: List.map
(fun (_
, s) -> (s, 1, Noaction
)) state.docinfo
1613 :: (Printf.sprintf
"Save these parameters as defaults at exit (%s)"
1614 (btos conf.bedefault
),
1617 fun active first qsearch pan
->
1618 conf.bedefault
<- not
conf.bedefault
;
1619 Items
(active, first
, makeitems (), qsearch
, pan
, mode);
1624 Array.of_list
(items @ tailer)
1627 state.mode <- Items
(1, 0, makeitems (), "", 0, mode);
1628 Glut.postRedisplay
();
1631 let enterhelpmode () =
1632 state.mode <- Items
(-1, 0, state.help
, "", 0, state.mode);
1633 Glut.postRedisplay
();
1636 let quickbookmark ?title
() =
1637 match state.layout with
1643 let sec = Unix.gettimeofday
() in
1644 let tm = Unix.localtime
sec in
1645 Printf.sprintf
"Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1649 (tm.Unix.tm_year
+ 1900)
1652 | Some
title -> title
1655 (title, 0, (l
.pageno, float l
.pagey /. float l
.pageh
))
1660 state.fullscreen
<- None
;
1661 Glut.reshapeWindow
w h;
1664 let writeopen path password
=
1665 writecmd state.csock
("open " ^ path ^
"\000" ^ password ^
"\000");
1668 let opendoc path password
=
1671 state.password
<- password
;
1672 state.gen
<- state.gen
+ 1;
1673 state.docinfo
<- [];
1675 writeopen path password
;
1676 Glut.setWindowTitle
("llpp " ^
Filename.basename path
);
1677 wcmd "geometry" [`i
state.w; `i
conf.winh
];
1680 let viewkeyboard key
=
1682 let mode = state.mode in
1683 state.mode <- Textentry
(te, fun _
-> state.mode <- mode);
1686 Glut.postRedisplay
()
1688 let c = Char.chr key
in
1690 | '
\027'
-> (* escape *)
1691 if String.length
state.text > 0
1694 Glut.postRedisplay
();
1701 | '
\008'
-> (* backspace *)
1702 let y = getnav ~
-1 in
1703 gotoy_and_clear_text y
1711 Glut.postRedisplay
()
1714 let ondone isforw
s =
1715 cbput state.hists
.pat
s;
1716 state.searchpattern
<- s;
1719 let s = String.create
1 in
1721 enttext (s, "", Some
(onhist state.hists
.pat
),
1722 textentry, ondone (c ='
/'
))
1724 | '
+'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1725 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1726 setzoom (conf.zoom +. incr)
1731 try int_of_string
s with exc
->
1732 state.text <- Printf.sprintf
"bad integer `%s': %s"
1733 s (Printexc.to_string exc
);
1739 state.text <- "page bias is now " ^ string_of_int
n;
1742 enttext ("page bias: ", "", None
, intentry, ondone)
1744 | '
-'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1745 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1746 setzoom (max
0.01 (conf.zoom -. decr))
1749 let ondone msg
= state.text <- msg
in
1751 "option [acfhilpstvAPRSZ]: ", "", None
,
1752 optentry state.mode, ondone
1755 | '
0'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1758 | '
1'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1759 let zoom = zoomforh
conf.winw
conf.winh
state.scrollw
in
1763 | '
9'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1769 try int_of_string
s with exc
->
1770 state.text <- Printf.sprintf
"bad integer `%s': %s"
1771 s (Printexc.to_string exc
);
1777 cbput state.hists
.pag
(string_of_int
n);
1778 gotoy_and_clear_text (getpagey (n + conf.pagebias
- 1))
1781 let pageentry text key
=
1782 match Char.unsafe_chr key
with
1783 | 'g'
-> TEdone
text
1784 | _
-> intentry text key
1786 let text = "x" in text.[0] <- c;
1787 enttext (":", text, Some
(onhist state.hists
.pag
), pageentry, ondone)
1790 state.scrollw
<- if state.scrollw
> 0 then 0 else conf.scrollbw
;
1791 reshape conf.winw
conf.winh
;
1794 conf.hlinks
<- not
conf.hlinks
;
1795 state.text <- "highlightlinks " ^
if conf.hlinks
then "on" else "off";
1796 Glut.postRedisplay
()
1799 begin match state.autoscroll
with
1801 conf.autoscrollstep
<- step
;
1802 state.autoscroll
<- None
1804 if conf.autoscrollstep
= 0
1805 then state.autoscroll
<- Some
1
1806 else state.autoscroll
<- Some
conf.autoscrollstep
1810 conf.presentation <- not
conf.presentation;
1811 if conf.presentation
1813 if not
conf.scrollbarinpm
1814 then state.scrollw
<- 0;
1817 state.scrollw
<- conf.scrollbw
;
1819 showtext ' '
("presentation mode " ^
1820 if conf.presentation then "on" else "off");
1821 state.anchor <- getanchor ();
1825 begin match state.fullscreen
with
1827 state.fullscreen
<- Some
(conf.winw
, conf.winh
);
1830 state.fullscreen
<- None
;
1835 gotoy_and_clear_text 0
1838 search state.searchpattern
true
1841 search state.searchpattern
false
1844 begin match state.layout with
1847 gotoy_and_clear_text (getpagey l
.pageno)
1851 begin match List.rev
state.layout with
1854 let pageno = min
(l
.pageno+1) (state.pagecount
-1) in
1855 gotoy_and_clear_text (getpagey pageno)
1858 | '
\127'
-> (* del *)
1859 begin match state.layout with
1862 let pageno = max
0 (l
.pageno-1) in
1863 gotoy_and_clear_text (getpagey pageno)
1868 if fn
= -1 then l
.pageno, l
.pageno else fn
, l
.pageno
1870 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
1872 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
1876 else (100. *. (float state.y /. float maxy)) in
1879 Printf.sprintf
"Page %d of %d %.2f%%"
1880 (fn+1) state.pagecount
percent
1883 "Pages %d-%d of %d %.2f%%"
1884 (fn+1) (ln
+1) state.pagecount
percent
1889 begin match state.layout with
1892 doreshape (l
.pagew
+ state.scrollw
) l
.pageh
;
1893 Glut.postRedisplay
();
1897 enterbookmarkmode ()
1907 match state.layout with
1910 (s, 0, (l
.pageno, float l
.pagey /. float l
.pageh
))
1914 enttext ("bookmark: ", "", None
, textentry, ondone)
1918 showtext ' '
"Quick bookmark added";
1921 begin match state.layout with
1923 let rect = getpdimrect l
.pagedimno
in
1927 (truncate
(1.8 *. (rect.(1) -. rect.(0))),
1928 truncate
(1.2 *. (rect.(3) -. rect.(0))))
1930 (truncate
(rect.(1) -. rect.(0)),
1931 truncate
(rect.(3) -. rect.(0)))
1935 doreshape (w + state.scrollw
) (h + conf.interpagespace)
1937 Glut.postRedisplay
();
1942 | '
\000'
-> (* ctrl-2 *)
1943 let maxw = getmaxw
() in
1945 then setzoom (maxw /. float conf.winw
)
1948 reinit (conf.angle
+ (if c = '
>'
then 30 else -30)) conf.proportional
1953 (min
(state.colorscale
+. (if c = '
]'
then 0.1 else -0.1)) 1.0);
1954 Glut.postRedisplay
()
1957 begin match state.mode with
1958 | Birdseye beye
-> upbirdseye beye
1959 | _
-> gotoy (clamp (-conf.scrollstep
))
1963 begin match state.mode with
1964 | Birdseye beye
-> downbirdseye beye
1965 | _
-> gotoy (clamp conf.scrollstep
)
1969 state.anchor <- getanchor ();
1970 opendoc state.path
state.password
1973 vlog "huh? %d %c" key
(Char.chr key
);
1976 let textentrykeyboard key
((c, text, opthist
, onkey
, ondone), onleave
) =
1978 state.mode <- Textentry
(te, onleave
);
1981 Glut.postRedisplay
()
1983 match Char.unsafe_chr key
with
1984 | '
\008'
-> (* backspace *)
1985 let len = String.length
text in
1989 Glut.postRedisplay
();
1992 let s = String.sub
text 0 (len - 1) in
1993 enttext (c, s, opthist
, onkey
, ondone)
1999 Glut.postRedisplay
()
2001 | '
\027'
-> (* escape *)
2002 begin match opthist
with
2004 | Some
(_
, onhistcancel
) -> onhistcancel
()
2007 Glut.postRedisplay
()
2010 begin match onkey
text key
with
2014 Glut.postRedisplay
()
2017 enttext (c, text, opthist
, onkey
, ondone);
2021 Glut.postRedisplay
()
2024 state.mode <- Textentry
(te, onleave
);
2025 Glut.postRedisplay
()
2029 let birdseyekeyboard key
((_
, _
, pageno, _
, _
) as beye
) =
2031 | 27 -> (* escape *)
2032 leavebirdseye beye
true
2034 | 12 -> (* ctrl-l *)
2035 let y, h = getpageyh pageno in
2036 let top = (conf.winh
- h) / 2 in
2037 gotoy (max
0 (y - top))
2040 leavebirdseye beye
false
2046 let itemskeyboard key
(active, first
, items, qsearch
, pan
, oldmode
) =
2047 let set active first qsearch
=
2048 state.mode <- Items
(active, first
, items, qsearch
, pan
, oldmode
)
2050 let search active pattern
incr =
2053 if n >= Array.length
items || n < 0
2056 let (s, _
, _
) = items.(n) in
2058 (try ignore
(Str.search_forward re
s 0); true
2059 with Not_found
-> false)
2061 else loop (n + incr)
2066 let re = Str.regexp_case_fold pattern
in
2072 let firstof active = max
0 (active - maxoutlinerows () / 2) in
2074 | 18 | 19 -> (* ctrl-r/ctlr-s *)
2075 let incr = if key
= 18 then -1 else 1 in
2077 match search (active + incr) qsearch
incr with
2079 state.text <- qsearch ^
" [not found]";
2082 state.text <- qsearch
;
2083 active, firstof active
2085 set active first qsearch
;
2086 Glut.postRedisplay
();
2088 | 8 -> (* backspace *)
2089 let len = String.length qsearch
in
2096 set active first
"";
2099 let qsearch = String.sub
qsearch 0 (len - 1) in
2101 match search active qsearch ~
-1 with
2103 state.text <- qsearch ^
" [not found]";
2106 state.text <- qsearch;
2107 active, firstof active
2109 set active first
qsearch
2111 Glut.postRedisplay
()
2113 | _
when key
>= 32 && key
< 127 ->
2114 let pattern = addchar qsearch (Char.chr key
) in
2116 match search active pattern 1 with
2118 state.text <- pattern ^
" [not found]";
2121 state.text <- pattern;
2122 active, firstof active
2124 set active first
pattern;
2125 Glut.postRedisplay
()
2127 | 27 -> (* escape *)
2129 if String.length
qsearch = 0
2131 state.mode <- oldmode
;
2134 set active first
"";
2136 Glut.postRedisplay
()
2139 if active >= 0 && active < Array.length
items
2141 match items.(active) with
2143 state.mode <- f active first
qsearch pan
2147 state.mode <- oldmode
2151 state.mode <- oldmode
2153 Glut.postRedisplay
();
2155 | _
-> dolog "unknown key %d" key
2158 let outlinekeyboard key
2159 (allowdel
, active, first
, outlines, qsearch, pan
, oldmode
) =
2160 let narrow outlines pattern =
2161 let reopt = try Some
(Str.regexp_case_fold
pattern) with _
-> None
in
2165 let rec fold accu n =
2169 let (s, _
, _
) as o
= outlines.(n) in
2171 if (try ignore
(Str.search_forward
re s 0); true
2172 with Not_found
-> false)
2178 let matched = fold [] (Array.length
outlines - 1) in
2179 if matched = [] then None
else Some
(Array.of_list
matched)
2181 let search active pattern incr =
2184 if n = Array.length
outlines || n = -1
2187 let (s, _
, _
) = outlines.(n) in
2189 (try ignore
(Str.search_forward
re s 0); true
2190 with Not_found
-> false)
2192 else loop (n + incr)
2197 let re = Str.regexp_case_fold
pattern in
2203 let firstof active = max
0 (active - maxoutlinerows () / 2) in
2205 | 27 -> (* escape *)
2207 if String.length
qsearch = 0
2209 state.mode <- oldmode
;
2212 state.mode <- Outline
(
2213 allowdel
, active, first
, outlines, "", pan
, oldmode
2216 Glut.postRedisplay
();
2218 | 18 | 19 -> (* ctrl-r/ctrl-s *)
2219 let incr = if key
= 18 then -1 else 1 in
2221 match search (active + incr) qsearch incr with
2223 state.text <- qsearch ^
" [not found]";
2226 state.text <- qsearch;
2227 active, firstof active
2229 state.mode <- Outline
(
2230 allowdel
, active, first
, outlines, qsearch, pan
, oldmode
2232 Glut.postRedisplay
();
2234 | 8 -> (* backspace *)
2235 let len = String.length
qsearch in
2242 state.mode <- Outline
(
2243 allowdel
, active, first
, outlines, "", pan
, oldmode
2247 let qsearch = String.sub
qsearch 0 (len - 1) in
2249 match search active qsearch ~
-1 with
2251 state.text <- qsearch ^
" [not found]";
2254 state.text <- qsearch;
2255 active, firstof active
2257 state.mode <- Outline
(
2258 allowdel
, active, first
, outlines, qsearch, pan
, oldmode
2261 Glut.postRedisplay
()
2264 if active < Array.length
outlines
2266 let (_
, _
, anchor) = outlines.(active) in
2271 if allowdel
then state.bookmarks <- Array.to_list
outlines;
2272 state.mode <- oldmode
;
2273 Glut.postRedisplay
();
2275 | _
when key
>= 32 && key
< 127 ->
2276 let pattern = addchar qsearch (Char.chr key
) in
2278 match search active pattern 1 with
2280 state.text <- pattern ^
" [not found]";
2283 state.text <- pattern;
2284 active, firstof active
2286 state.mode <- Outline
(
2287 allowdel
, active, first
, outlines, pattern, pan
, oldmode
2289 Glut.postRedisplay
()
2291 | 14 when not allowdel
-> (* ctrl-n *)
2292 if String.length
qsearch > 0
2294 let optoutlines = narrow outlines qsearch in
2295 begin match optoutlines with
2296 | None
-> state.text <- "can't narrow"
2298 state.mode <- Outline
(
2299 allowdel
, 0, 0, outlines, qsearch, pan
, oldmode
2301 match state.outlines with
2304 state.outlines <- Onarrow
(qsearch, outlines, a)
2305 | Onarrow
(_
, _
, b) ->
2306 state.outlines <- Onarrow
(qsearch, outlines, b)
2309 Glut.postRedisplay
()
2311 | 21 when not allowdel
-> (* ctrl-u *)
2313 match state.outlines with
2316 let a = Array.of_list
(List.rev l
) in
2317 state.outlines <- Oarray
a;
2319 | Onarrow
(_
, _
, b) ->
2320 state.outlines <- Oarray
b;
2324 state.mode <- Outline
(allowdel
, 0, 0, outline, qsearch, pan
, oldmode
);
2325 Glut.postRedisplay
()
2327 | 12 -> (* ctrl-l *)
2328 state.mode <- Outline
2329 (allowdel
, active, firstof active, outlines, qsearch, pan
, oldmode
);
2330 Glut.postRedisplay
()
2332 | 127 when allowdel
-> (* delete *)
2333 let len = Array.length
outlines - 1 in
2337 state.bookmarks <- [];
2340 let bookmarks = Array.init
len
2342 let i = if i >= active then i + 1 else i in
2356 Glut.postRedisplay
()
2358 | _
-> dolog "unknown key %d" key
2361 let keyboard ~key ~x ~
y =
2364 if key
= 7 (* ctrl-g *)
2368 match state.mode with
2369 | Outline
outline -> outlinekeyboard key
outline
2370 | Textentry
textentry -> textentrykeyboard key
textentry
2371 | Birdseye
birdseye -> birdseyekeyboard key
birdseye
2372 | View
-> viewkeyboard key
2373 | Items
items -> itemskeyboard key
items
2376 let birdseyespecial key
((conf, leftx
, _
, hooverpageno
, anchor) as beye
) =
2378 | Glut.KEY_UP
-> upbirdseye beye
2379 | Glut.KEY_DOWN
-> downbirdseye beye
2381 | Glut.KEY_PAGE_UP
->
2382 begin match state.layout with
2386 state.mode <- Birdseye
(
2387 conf, leftx
, l
.pageno, hooverpageno
, anchor
2389 gotopage1 l
.pageno 0;
2392 let layout = layout (state.y-conf.winh
) conf.winh
in
2394 | [] -> gotoy (clamp (-conf.winh
))
2396 state.mode <- Birdseye
(
2397 conf, leftx
, l
.pageno, hooverpageno
, anchor
2399 gotopage1 l
.pageno 0
2402 | [] -> gotoy (clamp (-conf.winh
))
2405 | Glut.KEY_PAGE_DOWN
->
2406 begin match List.rev
state.layout with
2408 let layout = layout (state.y + conf.winh
) conf.winh
in
2409 begin match layout with
2411 let incr = l
.pageh
- l
.pagevh in
2416 conf, leftx
, state.pagecount
- 1, hooverpageno
, anchor
2418 Glut.postRedisplay
();
2420 else gotoy (clamp (incr + conf.interpagespace*2));
2424 Birdseye
(conf, leftx
, l
.pageno, hooverpageno
, anchor);
2425 gotopage1 l
.pageno 0;
2428 | [] -> gotoy (clamp conf.winh
)
2432 state.mode <- Birdseye
(conf, leftx
, 0, hooverpageno
, anchor);
2436 let pageno = state.pagecount
- 1 in
2437 state.mode <- Birdseye
(conf, leftx
, pageno, hooverpageno
, anchor);
2438 if not
(pagevisible state.layout pageno)
2441 match List.rev
state.pdims
with
2443 | (_
, _
, h, _
) :: _
-> h
2445 gotoy (max
0 (getpagey pageno - (conf.winh
- h - conf.interpagespace)))
2446 else Glut.postRedisplay
();
2450 let setautoscrollspeed step goingdown
=
2451 let incr = max
1 ((abs step
) / 2) in
2452 let incr = if goingdown
then incr else -incr in
2453 let astep = step
+ incr in
2454 state.autoscroll
<- Some
astep;
2457 let special ~key ~x ~
y =
2460 match state.mode with
2461 | View
| (Birdseye _
) when key
= Glut.KEY_F9
->
2465 birdseyespecial key vals
2467 | View
when key
= Glut.KEY_F1
->
2471 begin match state.autoscroll
with
2472 | Some step
when key
= Glut.KEY_DOWN
|| key
= Glut.KEY_UP
->
2473 setautoscrollspeed step
(key
= Glut.KEY_DOWN
)
2478 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
2479 | Glut.KEY_UP
-> clamp (-conf.scrollstep
)
2480 | Glut.KEY_DOWN
-> clamp conf.scrollstep
2481 | Glut.KEY_PAGE_UP
->
2482 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2484 match state.layout with
2486 | l
:: _
-> state.y - l
.pagey
2489 | Glut.KEY_PAGE_DOWN
->
2490 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2492 match List.rev
state.layout with
2494 | l
:: _
-> getpagey l
.pageno
2497 | Glut.KEY_HOME
-> addnav (); 0
2500 state.maxy - (if conf.maxhfit
then conf.winh
else 0)
2502 | (Glut.KEY_RIGHT
| Glut.KEY_LEFT
) when
2503 Glut.getModifiers
() land Glut.active_alt
!= 0 ->
2504 getnav (if key
= Glut.KEY_LEFT
then 1 else -1)
2506 | Glut.KEY_RIGHT
when conf.zoom > 1.0 ->
2507 state.x
<- state.x
- 10;
2509 | Glut.KEY_LEFT
when conf.zoom > 1.0 ->
2510 state.x
<- state.x
+ 10;
2515 gotoy_and_clear_text y
2519 ((c, _
, (Some
(action, _
) as onhist), onkey
, ondone), mode) ->
2522 | Glut.KEY_UP
-> action HCprev
2523 | Glut.KEY_DOWN
-> action HCnext
2524 | Glut.KEY_HOME
-> action HCfirst
2525 | Glut.KEY_END
-> action HClast
2528 state.mode <- Textentry
((c, s, onhist, onkey
, ondone), mode);
2529 Glut.postRedisplay
()
2533 | Items
(active, first
, items, qsearch, pan
, oldmode
) ->
2534 let maxrows = maxoutlinerows () in
2535 let itemcount = Array.length
items in
2536 let hasaction = function
2537 | (_
, _
, Noaction
) -> false
2540 let find start
incr =
2542 if i = -1 || i = itemcount
2545 if hasaction items.(i)
2547 else find (i + incr)
2552 let set active first
=
2553 let first = max
0 (min
first (itemcount - maxrows)) in
2554 state.mode <- Items
(active, first, items, qsearch, pan
, oldmode
)
2557 let isvisible first n = n >= first && n - first <= maxrows in
2559 let incr1 = if incr > 0 then 1 else -1 in
2560 if isvisible first active
2563 let next = active + incr in
2565 if next < 0 || next >= itemcount
2567 else find next incr1
2569 if next = -1 || abs
(active - next) > maxrows
2575 let first = first + incr in
2576 let first = max
0 (min
first (itemcount - 1)) in
2578 let next = active + incr in
2579 let next = max
0 (min
next (itemcount - 1)) in
2582 let active = if next = -1 then active else next in
2585 let first = min
next first in
2588 let first = first + incr in
2589 let first = max
0 (min
first (itemcount - 1)) in
2591 let next = active + incr in
2592 let next = max
0 (min
next (itemcount - 1)) in
2593 let next = find next incr1 in
2594 if next = -1 || abs
(active - first) > maxrows
2601 Glut.postRedisplay
()
2603 begin match key
with
2604 | Glut.KEY_UP
-> navigate ~
-1
2605 | Glut.KEY_DOWN
-> navigate 1
2606 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
2607 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
2610 state.mode <- Items
(
2611 active, first, items, qsearch, min
0 (pan
- 1), oldmode
2613 Glut.postRedisplay
()
2616 state.mode <- Items
(
2617 active, first, items, qsearch, min
0 (pan
+ 1), oldmode
2619 Glut.postRedisplay
()
2622 let active = find 0 1 in
2624 Glut.postRedisplay
()
2627 let first = max
0 (itemcount - maxrows) in
2628 let active = find (itemcount - 1) ~
-1 in
2630 Glut.postRedisplay
()
2635 | Outline
(allowdel
, active, first, outlines, qsearch, pan
, oldmode
) ->
2636 let maxrows = maxoutlinerows () in
2637 let calcfirst first active =
2640 let rows = active - first in
2641 if rows > maxrows then active - maxrows else first
2645 let active = active + incr in
2646 let active = max
0 (min
active (Array.length
outlines - 1)) in
2647 let first = calcfirst first active in
2648 state.mode <- Outline
(
2649 allowdel
, active, first, outlines, qsearch, pan
, oldmode
2651 Glut.postRedisplay
()
2653 let updownlevel incr =
2654 let len = Array.length
outlines in
2655 let (_
, curlevel
, _
) = outlines.(active) in
2657 if i = len then i-1 else if i = -1 then 0 else
2658 let (_
, l
, _
) = outlines.(i) in
2659 if l
!= curlevel
then i else flow (i+incr)
2661 let active = flow active in
2662 let first = calcfirst first active in
2663 state.mode <- Outline
(
2664 allowdel
, active, first, outlines, qsearch, pan
, oldmode
2666 Glut.postRedisplay
()
2669 | Glut.KEY_UP
-> navigate ~
-1
2670 | Glut.KEY_DOWN
-> navigate 1
2671 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
2672 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
2675 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2677 state.mode <- Outline
(
2678 allowdel
, active, first, outlines,
2679 qsearch, min
0 (pan
+ 1), oldmode
2681 Glut.postRedisplay
();
2689 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2691 state.mode <- Outline
(
2692 allowdel
, active, first, outlines, qsearch, pan
- 1, oldmode
2694 Glut.postRedisplay
();
2698 then updownlevel ~
-1
2702 state.mode <- Outline
(
2703 allowdel
, 0, 0, outlines, qsearch, pan
, oldmode
2705 Glut.postRedisplay
()
2708 let active = Array.length
outlines - 1 in
2709 let first = max
0 (active - maxrows) in
2710 state.mode <- Outline
(
2711 allowdel
, active, first, outlines, qsearch, pan
, oldmode
2713 Glut.postRedisplay
()
2718 let drawplaceholder l
=
2719 let margin = state.x
+ (conf.winw
- (state.w + state.scrollw
)) / 2 in
2721 (float l
.pagex
, float l
.pagedispy
)
2722 (float (l
.pagew
+ l
.pagex
), float (l
.pagedispy
+ l
.pagevh))
2724 let x = if margin < 0 then -margin else l
.pagex
2725 and y = l
.pagedispy
+ 13 in
2726 GlDraw.color
(0.0, 0.0, 0.0);
2727 drawstring 13 x y ("Loading " ^ string_of_int
(l
.pageno + 1))
2730 let now () = Unix.gettimeofday
();;
2734 match state.mode with
2735 | Textentry _
-> scalecolor 0.4
2736 | View
| Outline _
| Items _
-> scalecolor 1.0
2737 | Birdseye
(_
, _
, pageno, hooverpageno
, _
) ->
2738 if l
.pageno = hooverpageno
2741 if l
.pageno = pageno
2747 begin match getopaque l
.pageno with
2748 | Some
(opaque, _
) when validopaque opaque ->
2750 draw
(l
.pagedispy
, l
.pagevh, l
.pagey, conf.hlinks
) opaque;
2753 vlog "draw %d %f sec" l
.pageno d;
2761 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
2762 let sh = (float (maxy + conf.winh
) /. float conf.winh
) in
2763 let sh = float conf.winh
/. sh in
2764 let sh = max
sh (float conf.scrollh
) in
2767 if state.y = state.maxy
2769 else float y /. float maxy
2771 let position = (float conf.winh
-. sh) *. percent in
2774 if position +. sh > float conf.winh
2775 then float conf.winh
-. sh
2781 let scrollindicator () =
2782 GlDraw.color (0.64 , 0.64, 0.64);
2784 (float (conf.winw
- state.scrollw
), 0.)
2785 (float conf.winw
, float conf.winh
)
2787 GlDraw.color (0.0, 0.0, 0.0);
2789 let position, sh = scrollph state.y in
2791 (float (conf.winw
- state.scrollw
), position)
2792 (float conf.winw
, position +. sh)
2796 let showsel margin =
2797 match state.mstate
with
2798 | Mnone
| Mscroll _
| Mpan _
| Mzoom _
->
2801 | Msel
((x0
, y0
), (x1
, y1
)) ->
2802 let rec loop = function
2804 if (y0
>= l
.pagedispy
&& y0
<= (l
.pagedispy
+ l
.pagevh))
2805 || ((y1
>= l
.pagedispy
&& y1
<= (l
.pagedispy
+ l
.pagevh)))
2807 match getopaque l
.pageno with
2808 | Some
(opaque, _
) when validopaque opaque ->
2809 let oy = -l
.pagey + l
.pagedispy
in
2811 (x0
- margin - state.x, y0
,
2812 x1
- margin - state.x, y1
) oy;
2822 let panx = float state.x in
2824 GlDraw.color (0.0, 0.0, 1.0) ~alpha
:0.5;
2825 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2827 (fun (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) ->
2829 if l
.pageno = pageno
2831 let d = float (l
.pagedispy
- l
.pagey) in
2832 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
2833 GlDraw.begins `quads
;
2835 GlDraw.vertex2
(x0
+.panx, y0
+.d);
2836 GlDraw.vertex2
(x1
+.panx, y1
+.d);
2837 GlDraw.vertex2
(x2
+.panx, y2
+.d);
2838 GlDraw.vertex2
(x3
+.panx, y3
+.d);
2848 let showstrings trusted
active first pan
strings =
2850 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2851 GlDraw.color (0., 0., 0.) ~alpha
:0.85;
2852 GlDraw.rect (0., 0.) (float conf.winw
, float conf.winh
);
2853 GlDraw.color (1., 1., 1.);
2854 Gl.enable `texture_2d
;
2856 let wx = measurestr
15 "w" in
2857 let tabx = 30.0*.wx +. float (pan
*15) in
2859 if row
= Array.length
strings || (row
- first) * 16 > conf.winh
2862 let (s, level
, _
) = strings.(row
) in
2863 let y = (row
- first) * 16 in
2864 let x = 5 + 15*(max
0 (level
+pan
)) in
2867 Gl.disable `texture_2d
;
2868 GlDraw.polygon_mode `both `line
;
2869 GlDraw.color (1., 1., 1.) ~alpha
:0.9;
2870 GlDraw.rect (0., float (y + 1))
2871 (float (conf.winw
- 1), float (y + 18));
2872 GlDraw.polygon_mode `both `fill
;
2873 GlDraw.color (1., 1., 1.);
2874 Gl.enable `texture_2d
;
2877 let drawtabularstring x s =
2881 let tabpos = try String.index
s '
\t'
with Not_found
-> -1 in
2884 let len = String.length
s - tabpos - 1 in
2885 let s1 = String.sub
s 0 tabpos
2886 and s2
= String.sub
s (tabpos + 1) len in
2887 let xx = wx +. drawstring1 14 x (y + 16) s1 in
2888 let x = truncate
(max
xx tabx) in
2889 drawstring1 15 x (y + 16) s2
2891 drawstring1 15 x (y + 16) s
2893 drawstring1 15 x (y + 16) s
2897 drawtabularstring (x + pan
*15) 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 match state.autoscroll
with
3004 | Some step
-> setautoscrollspeed step
(n=4)
3008 then -conf.scrollstep
3009 else conf.scrollstep
3011 let incr = incr * 2 in
3012 let y = clamp incr in
3013 gotoy_and_clear_text y
3016 | Glut.LEFT_BUTTON
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
3017 if bstate
= Glut.DOWN
3019 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
3020 state.mstate
<- Mpan
(x, y)
3023 state.mstate
<- Mnone
3025 | Glut.LEFT_BUTTON
when x > conf.winw
- state.scrollw
->
3026 if bstate
= Glut.DOWN
3028 let position, sh = scrollph state.y in
3029 if y > truncate
position && y < truncate
(position +. sh)
3031 state.mstate
<- Mscroll
3033 let percent = float y /. float conf.winh
in
3034 let desty = truncate
(float (state.maxy - conf.winh
) *. percent) in
3036 state.mstate
<- Mscroll
3038 state.mstate
<- Mnone
3040 | Glut.LEFT_BUTTON
->
3041 let dest = if bstate
= Glut.DOWN
then getunder x y else Unone
in
3042 begin match dest with
3043 | Ulinkgoto
(pageno, top) ->
3047 gotopage1 pageno top;
3053 | Unone
when bstate
= Glut.DOWN
->
3054 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
3055 state.mstate
<- Mpan
(x, y);
3057 | Unone
| Utext
_ ->
3058 if bstate
= Glut.DOWN
3060 if conf.angle
mod 360 = 0
3062 state.mstate
<- Msel
((x, y), (x, y));
3063 Glut.postRedisplay
()
3067 match state.mstate
with
3070 | Mzoom
_ | Mscroll
->
3071 state.mstate
<- Mnone
3074 Glut.setCursor
Glut.CURSOR_INHERIT
;
3075 state.mstate
<- Mnone
3077 | Msel
((_, y0
), (_, y1
)) ->
3079 if (y0
>= l
.pagedispy
&& y0
<= (l
.pagedispy
+ l
.pagevh))
3080 || ((y1
>= l
.pagedispy
&& y1
<= (l
.pagedispy
+ l
.pagevh)))
3082 match getopaque l
.pageno with
3083 | Some
(opaque, _) when validopaque opaque ->
3087 List.iter
f state.layout;
3088 copysel
""; (* ugly *)
3089 Glut.setCursor
Glut.CURSOR_INHERIT
;
3090 state.mstate
<- Mnone
;
3097 let birdseyemouse button bstate
x y
3098 (conf, leftx
, _, hooverpageno
, anchor) =
3100 | Glut.LEFT_BUTTON
when bstate
= Glut.UP
->
3101 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
3102 let rec loop = function
3105 if y > l
.pagedispy
&& y < l
.pagedispy
+ l
.pagevh
3106 && x > margin && x < margin + l
.pagew
3108 leavebirdseye (conf, leftx
, l
.pageno, hooverpageno
, anchor) false;
3113 | Glut.OTHER_BUTTON
_ -> viewmouse button bstate
x y
3117 let mouse bstate button
x y =
3118 match state.mode with
3119 | View
-> viewmouse button bstate
x y
3120 | Birdseye beye
-> birdseyemouse button bstate
x y beye
3121 | Textentry
_ | Outline
_ | Items
_ -> ()
3124 let mouse ~button ~
state ~
x ~
y = mouse state button
x y;;
3127 match state.mode with
3130 match state.mstate
with
3131 | Mzoom
_ | Mnone
-> ()
3136 state.mstate
<- Mpan
(x, y);
3137 if conf.zoom > 1.0 then state.x <- state.x + dx;
3139 gotoy_and_clear_text y
3142 state.mstate
<- Msel
(a, (x, y));
3143 Glut.postRedisplay
()
3146 let y = min
conf.winh
(max
0 y) in
3147 let percent = float y /. float conf.winh
in
3148 let y = truncate
(float (state.maxy - conf.winh
) *. percent) in
3149 gotoy_and_clear_text y
3153 match state.mode with
3154 | Birdseye
(conf, leftx
, pageno, hooverpageno
, anchor) ->
3155 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
3156 let rec loop = function
3158 if hooverpageno
!= -1
3160 state.mode <- Birdseye
(conf, leftx
, pageno, -1, anchor);
3161 Glut.postRedisplay
();
3164 if y > l
.pagedispy
&& y < l
.pagedispy
+ l
.pagevh
3165 && x > margin && x < margin + l
.pagew
3167 state.mode <- Birdseye
(conf, leftx
, pageno, l
.pageno, anchor);
3168 Glut.postRedisplay
();
3174 | Outline
_ | Items
_ | Textentry
_ -> ()
3176 match state.mstate
with
3178 begin match getunder x y with
3179 | Unone
-> Glut.setCursor
Glut.CURSOR_INHERIT
3181 if conf.underinfo
then showtext 'u'
("ri: " ^ uri
);
3182 Glut.setCursor
Glut.CURSOR_INFO
3183 | Ulinkgoto
(page
, _) ->
3185 then showtext 'p'
("age: " ^ string_of_int page
);
3186 Glut.setCursor
Glut.CURSOR_INFO
3188 if conf.underinfo
then showtext '
f'
("ont: " ^
s);
3189 Glut.setCursor
Glut.CURSOR_TEXT
3192 | Mpan
_ | Msel
_ | Mzoom
_ | Mscroll
->
3201 let fontpath = ref "";;
3205 match Sys.os_type
with
3206 | "Win32" -> Sys.getenv
"HOMEPATH"
3207 | _ -> Sys.getenv
"HOME"
3210 ("Can not determine home directory location: " ^
3211 Printexc.to_string exn
);
3215 let config_of c attrs
=
3219 | "scroll-bar-width" -> { c with scrollbw
= max
0 (int_of_string
v) }
3220 | "scroll-handle-height" -> { c with scrollh
= max
0 (int_of_string
v) }
3221 | "case-insensitive-search" -> { c with icase
= bool_of_string
v }
3222 | "preload" -> { c with preload = bool_of_string
v }
3223 | "page-bias" -> { c with pagebias
= int_of_string
v }
3224 | "scroll-step" -> { c with scrollstep
= max
1 (int_of_string
v) }
3225 | "auto-scroll-step" ->
3226 { c with autoscrollstep
= max
0 (int_of_string
v) }
3227 | "max-height-fit" -> { c with maxhfit
= bool_of_string
v }
3228 | "crop-hack" -> { c with crophack
= bool_of_string
v }
3229 | "throttle" -> { c with showall
= bool_of_string
v }
3230 | "highlight-links" -> { c with hlinks
= bool_of_string
v }
3231 | "under-cursor-info" -> { c with underinfo
= bool_of_string
v }
3232 | "vertical-margin" ->
3233 { c with interpagespace = max
0 (int_of_string
v) }
3235 let zoom = float_of_string
v /. 100. in
3236 let zoom = max
0.01 zoom in
3237 { c with zoom = zoom }
3238 | "presentation" -> { c with presentation = bool_of_string
v }
3239 | "rotation-angle" -> { c with angle
= int_of_string
v }
3240 | "width" -> { c with winw
= max
20 (int_of_string
v) }
3241 | "height" -> { c with winh
= max
20 (int_of_string
v) }
3242 | "persistent-bookmarks" -> { c with savebmarks
= bool_of_string
v }
3243 | "proportional-display" -> { c with proportional
= bool_of_string
v }
3244 | "pixmap-cache-size" -> { c with memlimit
= max
2 (int_of_string
v) }
3245 | "tex-count" -> { c with texcount
= max
1 (int_of_string
v) }
3246 | "slice-height" -> { c with sliceheight
= max
2 (int_of_string
v) }
3247 | "block-width" -> { c with blockwidth
= max
2 (int_of_string
v) }
3248 | "thumbnail-width" -> { c with thumbw
= max
2 (int_of_string
v) }
3249 | "persistent-location" -> { c with jumpback
= bool_of_string
v }
3250 | "background-color" -> { c with bgcolor
= color_of_string v }
3251 | "scrollbar-in-presentation" ->
3252 { c with scrollbarinpm
= bool_of_string
v }
3255 prerr_endline
("Error processing attribute (`" ^
3256 k ^
"'=`" ^
v ^
"'): " ^
Printexc.to_string exn
);
3259 let rec fold c = function
3262 let c = apply c k
v in
3268 let fromstring f pos
n v d =
3271 dolog "Error processing attribute (%S=%S) at %d\n%s"
3272 n v pos
(Printexc.to_string exn
)
3277 let bookmark_of attrs
=
3278 let rec fold title page rely
= function
3279 | ("title", v) :: rest
-> fold v page rely rest
3280 | ("page", v) :: rest
-> fold title v rely rest
3281 | ("rely", v) :: rest
-> fold title page
v rest
3282 | _ :: rest
-> fold title page rely rest
3283 | [] -> title, page
, rely
3285 fold "invalid" "0" "0" attrs
3289 let rec fold path page rely pan
= function
3290 | ("path", v) :: rest
-> fold v page rely pan rest
3291 | ("page", v) :: rest
-> fold path
v rely pan rest
3292 | ("rely", v) :: rest
-> fold path page
v pan rest
3293 | ("pan", v) :: rest
-> fold path page rely
v rest
3294 | _ :: rest
-> fold path page rely pan rest
3295 | [] -> path
, page
, rely
, pan
3297 fold "" "0" "0" "0" attrs
3300 let setconf dst src
=
3301 dst
.scrollbw
<- src
.scrollbw
;
3302 dst
.scrollh
<- src
.scrollh
;
3303 dst
.icase
<- src
.icase
;
3304 dst
.preload <- src
.preload;
3305 dst
.pagebias
<- src
.pagebias
;
3306 dst
.verbose
<- src
.verbose
;
3307 dst
.scrollstep
<- src
.scrollstep
;
3308 dst
.maxhfit
<- src
.maxhfit
;
3309 dst
.crophack
<- src
.crophack
;
3310 dst
.autoscrollstep
<- src
.autoscrollstep
;
3311 dst
.showall
<- src
.showall
;
3312 dst
.hlinks
<- src
.hlinks
;
3313 dst
.underinfo
<- src
.underinfo
;
3314 dst
.interpagespace <- src
.interpagespace;
3315 dst
.zoom <- src
.zoom;
3316 dst
.presentation <- src
.presentation;
3317 dst
.angle
<- src
.angle
;
3318 dst
.winw
<- src
.winw
;
3319 dst
.winh
<- src
.winh
;
3320 dst
.savebmarks
<- src
.savebmarks
;
3321 dst
.memlimit
<- src
.memlimit
;
3322 dst
.proportional
<- src
.proportional
;
3323 dst
.texcount
<- src
.texcount
;
3324 dst
.sliceheight
<- src
.sliceheight
;
3325 dst
.blockwidth
<- src
.blockwidth
;
3326 dst
.thumbw
<- src
.thumbw
;
3327 dst
.jumpback
<- src
.jumpback
;
3328 dst
.bgcolor
<- src
.bgcolor
;
3329 dst
.scrollbarinpm
<- src
.scrollbarinpm
;
3333 let l = String.length
s in
3334 let b = Buffer.create
l in
3340 let h = Hashtbl.create
10 in
3341 let dc = { defconf with angle
= defconf.angle
} in
3342 let rec toplevel v t spos
_ =
3344 | Vdata
| Vcdata
| Vend
-> v
3345 | Vopen
("llppconfig", _, closed
) ->
3348 else { v with f = llppconfig
}
3350 error
"unexpected subelement at top level" s spos
3351 | Vclose
_ -> error
"unexpected close at top level" s spos
3353 and llppconfig
v t spos
_ =
3355 | Vdata
| Vcdata
-> v
3356 | Vend
-> error
"unexpected end of input in llppconfig" s spos
3357 | Vopen
("defaults", attrs
, closed
) ->
3358 let c = config_of dc attrs
in
3362 else { v with f = skip
"defaults" (fun () -> v) }
3364 | Vopen
("ui-font", _, closed
) ->
3367 else { v with f = uifont
(Buffer.create
10) }
3369 | Vopen
("doc", attrs
, closed
) ->
3370 let pathent, spage
, srely
, span
= doc_of attrs
in
3371 let path = unent pathent
3372 and pageno = fromstring int_of_string spos
"page" spage
0
3373 and rely
= fromstring float_of_string spos
"rely" srely
0.0
3374 and pan
= fromstring int_of_string spos
"pan" span
0 in
3375 let c = config_of dc attrs
in
3376 let anchor = (pageno, rely
) in
3378 then (Hashtbl.add
h path (c, [], pan
, anchor); v)
3379 else { v with f = doc
path pan
anchor c [] }
3382 error
"unexpected subelement in llppconfig" s spos
3384 | Vclose
"llppconfig" -> { v with f = toplevel }
3385 | Vclose
_ -> error
"unexpected close in llppconfig" s spos
3387 and uifont
b v t spos epos
=
3390 Buffer.add_substring
b s spos
(epos
- spos
);
3392 | Vopen
(_, _, _) ->
3393 error
"unexpected subelement in ui-font" s spos
3394 | Vclose
"ui-font" ->
3395 fontpath := Buffer.contents
b;
3396 { v with f = llppconfig
}
3397 | Vclose
_ -> error
"unexpected close in ui-font" s spos
3398 | Vend
-> error
"unexpected end of input in ui-font" s spos
3400 and doc
path pan
anchor c bookmarks v t spos
_ =
3402 | Vdata
| Vcdata
-> v
3403 | Vend
-> error
"unexpected end of input in doc" s spos
3404 | Vopen
("bookmarks", _, closed
) ->
3407 else { v with f = pbookmarks
path pan
anchor c bookmarks }
3409 | Vopen
(_, _, _) ->
3410 error
"unexpected subelement in doc" s spos
3413 Hashtbl.add
h path (c, List.rev
bookmarks, pan
, anchor);
3414 { v with f = llppconfig
}
3416 | Vclose
_ -> error
"unexpected close in doc" s spos
3418 and pbookmarks
path pan
anchor c bookmarks v t spos
_ =
3420 | Vdata
| Vcdata
-> v
3421 | Vend
-> error
"unexpected end of input in bookmarks" s spos
3422 | Vopen
("item", attrs
, closed
) ->
3423 let titleent, spage
, srely
= bookmark_of attrs
in
3424 let page = fromstring int_of_string spos
"page" spage
0
3425 and rely
= fromstring float_of_string spos
"rely" srely
0.0 in
3426 let bookmarks = (unent titleent, 0, (page, rely
)) :: bookmarks in
3428 then { v with f = pbookmarks
path pan
anchor c bookmarks }
3431 { v with f = skip
"item" f }
3434 error
"unexpected subelement in bookmarks" s spos
3436 | Vclose
"bookmarks" ->
3437 { v with f = doc
path pan
anchor c bookmarks }
3439 | Vclose
_ -> error
"unexpected close in bookmarks" s spos
3441 and skip tag
f v t spos
_ =
3443 | Vdata
| Vcdata
-> v
3445 error
("unexpected end of input in skipped " ^ tag
) s spos
3446 | Vopen
(tag'
, _, closed
) ->
3450 let f'
() = { v with f = skip tag
f } in
3451 { v with f = skip tag'
f'
}
3455 else error
("unexpected close in skipped " ^ tag
) s spos
3458 parse
{ f = toplevel; accu = () } s;
3464 let len = in_channel_length ic
in
3465 let s = String.create
len in
3466 really_input ic
s 0 len;
3469 | Parse_error
(msg
, s, pos
) ->
3470 let subs = subs s pos
in
3471 let s = Printf.sprintf
"%s: at %d [..%s..]" msg pos
subs in
3472 failwith
("parse error: " ^
s)
3475 failwith
("config load error: " ^
Printexc.to_string exn
)
3481 let dir = Filename.concat
home ".config" in
3482 if Sys.is_directory
dir then dir else home
3485 Filename.concat
dir "llpp.conf"
3488 let confpath = ref defconfpath;;
3491 if Sys.file_exists
!confpath
3494 (try Some
(open_in_bin
!confpath)
3497 ("Error opening configuation file `" ^
!confpath ^
"': " ^
3498 Printexc.to_string exn
);
3507 ("Error loading configuation from `" ^
!confpath ^
"': " ^
3508 Printexc.to_string exn
);
3514 f (Hashtbl.create
0, defconf)
3519 let pc, pb
, px
, pa
=
3521 Hashtbl.find h (Filename.basename
state.path)
3522 with Not_found
-> dc, [], 0, (0, 0.0)
3526 state.bookmarks <- pb
;
3528 state.scrollw
<- conf.scrollbw
;
3530 then state.anchor <- pa
;
3531 cbput state.hists
.nav pa
;
3536 let add_attrs bb always
dc c =
3539 then Printf.bprintf bb
"\n %s='%b'" s a
3542 then Printf.bprintf bb
"\n %s='%d'" s a
3545 then Printf.bprintf bb
"\n %s='%f'" s (a*.100.)
3549 Printf.bprintf bb
"\n %s='%s'" s (color_to_string a)
3553 then dc.winw
, dc.winh
3555 match state.fullscreen
with
3557 | None
-> c.winw
, c.winh
3559 let zoom, presentation, interpagespace, showall
=
3561 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
3563 match state.mode with
3564 | Birdseye
(bc
, _, _, _, _) ->
3565 bc
.zoom, bc
.presentation, bc
.interpagespace, bc
.showall
3566 | _ -> c.zoom, c.presentation, c.interpagespace, c.showall
3568 oi
"width" w dc.winw
;
3569 oi
"height" h dc.winh
;
3570 oi
"scroll-bar-width" c.scrollbw
dc.scrollbw
;
3571 oi
"scroll-handle-height" c.scrollh
dc.scrollh
;
3572 ob "case-insensitive-search" c.icase
dc.icase
;
3573 ob "preload" c.preload dc.preload;
3574 oi
"page-bias" c.pagebias
dc.pagebias
;
3575 oi
"scroll-step" c.scrollstep
dc.scrollstep
;
3576 oi
"auto-scroll-step" c.autoscrollstep
dc.autoscrollstep
;
3577 ob "max-height-fit" c.maxhfit
dc.maxhfit
;
3578 ob "crop-hack" c.crophack
dc.crophack
;
3579 ob "throttle" showall
dc.showall
;
3580 ob "highlight-links" c.hlinks
dc.hlinks
;
3581 ob "under-cursor-info" c.underinfo
dc.underinfo
;
3582 oi
"vertical-margin" interpagespace dc.interpagespace;
3583 oz
"zoom" zoom dc.zoom;
3584 ob "presentation" presentation dc.presentation;
3585 oi
"rotation-angle" c.angle
dc.angle
;
3586 ob "persistent-bookmarks" c.savebmarks
dc.savebmarks
;
3587 ob "proportional-display" c.proportional
dc.proportional
;
3588 oi
"pixmap-cache-size" c.memlimit
dc.memlimit
;
3589 oi
"tex-count" c.texcount
dc.texcount
;
3590 oi
"slice-height" c.sliceheight
dc.sliceheight
;
3591 oi
"block-width" c.blockwidth
dc.blockwidth
;
3592 oi
"thumbnail-width" c.thumbw
dc.thumbw
;
3593 ob "persistent-location" c.jumpback
dc.jumpback
;
3594 oc
"background-color" c.bgcolor
dc.bgcolor
;
3595 ob "scrollbar-in-presentation" c.scrollbarinpm
dc.scrollbarinpm
;
3599 let bb = Buffer.create
32768 in
3601 let dc = if conf.bedefault
then conf else dc in
3602 Buffer.add_string
bb "<llppconfig>\n";
3604 if String.length
!fontpath > 0
3605 then Printf.bprintf
bb "<ui-font><![CDATA[%s]]></ui-font>\n" !fontpath;
3607 Buffer.add_string
bb "<defaults ";
3608 add_attrs bb true dc dc;
3609 Buffer.add_string
bb "/>\n";
3611 let adddoc path pan
anchor c bookmarks =
3612 if bookmarks == [] && c = dc && anchor = emptyanchor
3615 Printf.bprintf
bb "<doc path='%s'"
3616 (enent
path 0 (String.length
path));
3618 if anchor <> emptyanchor
3620 let n, y = anchor in
3621 Printf.bprintf
bb " page='%d'" n;
3624 Printf.bprintf
bb " rely='%f'" y
3629 then Printf.bprintf
bb " pan='%d'" pan
;
3631 add_attrs bb false dc c;
3633 begin match bookmarks with
3634 | [] -> Buffer.add_string
bb "/>\n"
3636 Buffer.add_string
bb ">\n<bookmarks>\n";
3637 List.iter
(fun (title, _level
, (page, rely
)) ->
3639 "<item title='%s' page='%d'"
3640 (enent
title 0 (String.length
title))
3645 Printf.bprintf
bb " rely='%f'" rely
3647 Buffer.add_string
bb "/>\n";
3649 Buffer.add_string
bb "</bookmarks>\n</doc>\n";
3655 match state.mode with
3656 | Birdseye
(_, pan, _, _, _) -> pan
3659 let basename = Filename.basename state.path in
3660 adddoc basename pan (getanchor ())
3663 match state.autoscroll
with
3665 | None
-> conf.autoscrollstep
}
3666 (if conf.savebmarks
then state.bookmarks else []);
3668 Hashtbl.iter
(fun path (c, bookmarks, x, y) ->
3670 then adddoc path x y c bookmarks
3672 Buffer.add_string
bb "</llppconfig>";
3675 if Buffer.length
bb > 0
3678 let tmp = !confpath ^
".tmp" in
3679 let oc = open_out_bin
tmp in
3680 Buffer.output_buffer
oc bb;
3682 Sys.rename
tmp !confpath;
3685 ("error while saving configuration: " ^
Printexc.to_string exn
)
3692 [("-p", Arg.String
(fun s -> state.password
<- s) , " Set password")
3693 ;("-v", Arg.Unit
(fun () -> print_endline
Help.version
; exit
0),
3694 " Print version and exit")
3695 ;("-f", Arg.String
(fun s -> State.fontpath := s), "UI font path")
3696 ;("-c", Arg.String
(fun s -> State.confpath := s),
3697 "Configuration file path")
3700 (fun s -> state.path <- s)
3701 ("Usage: " ^
Sys.argv
.(0) ^
" [options] some.pdf\nOptions:")
3703 if String.length
state.path = 0
3704 then (prerr_endline
"file name missing"; exit
1);
3708 let _ = Glut.init
Sys.argv
in
3709 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
3710 let () = Glut.initWindowSize
conf.winw
conf.winh
in
3711 let _ = Glut.createWindow
("llpp " ^
Filename.basename state.path) in
3714 if Sys.os_type
= "Unix"
3716 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
3718 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
3719 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
3720 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
3721 Unix.bind
sock addr;
3723 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
3724 Unix.connect
csock addr;
3725 let ssock, _ = Unix.accept
sock in
3728 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
3729 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
3736 let () = Glut.displayFunc
display in
3737 let () = Glut.reshapeFunc
reshape in
3738 let () = Glut.keyboardFunc
keyboard in
3739 let () = Glut.specialFunc
special in
3740 let () = Glut.idleFunc
(Some
idle) in
3741 let () = Glut.mouseFunc
mouse in
3742 let () = Glut.motionFunc
motion in
3743 let () = Glut.passiveMotionFunc
pmotion in
3746 conf.angle
, conf.proportional
, conf.texcount
,
3747 conf.sliceheight
, conf.blockwidth
, !State.fontpath
3749 state.csock <- csock;
3750 state.ssock <- ssock;
3751 state.text <- "Opening " ^
state.path;
3752 writeopen state.path state.password
;
3754 let rec handlelablglutbug () =
3758 | Glut.BadEnum
"key in special_of_int" ->
3759 showtext '
!'
" LablGlut bug: special key not recognized";
3760 handlelablglutbug ()
3763 if Sys.os_type
<> "Unix"
3764 then Unix.shutdown
ssock Unix.SHUTDOWN_ALL
;
3767 handlelablglutbug ();