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
19 and proportional
= bool
20 and interpagespace
= int
27 external init
: Unix.file_descr
-> params
-> unit = "ml_init";;
28 external draw
: (int * int * int * int * bool) -> string -> unit = "ml_draw";;
29 external seltext
: string -> (int * int * int * int) -> int -> unit =
31 external copysel
: string -> unit = "ml_copysel";;
32 external getpdimrect
: int -> float array
= "ml_getpdimrect";;
33 external whatsunder
: string -> int -> int -> under
= "ml_whatsunder";;
34 external zoomforh
: int -> int -> int -> float = "ml_zoom_for_height";;
38 | Msel
of (mpos
* mpos
)
41 | Mzoom
of (int * int)
45 type textentry
= string * string * onhist
option * onkey
* ondone
46 and onkey
= string -> int -> te
47 and ondone
= string -> unit
48 and histcancel
= unit -> unit
49 and onhist
= ((histcmd
-> string) * histcancel
)
50 and histcmd
= HCnext
| HCprev
| HCfirst
| HClast
55 | TEswitch
of textentry
67 { store
= Array.create n v
74 let cbcap b
= Array.length b
.store
;;
79 b
.wc
<- (b
.wc
+ 1) mod cap;
81 b
.len
<- min
(b
.len
+ 1) cap;
84 let cbempty b
= b
.len
= 0;;
86 let cbgetg b circular dir
=
90 let rc = b
.rc + dir
in
102 else max
0 (min
rc (b
.len
-1))
108 let cbget b
= cbgetg b
false;;
109 let cbgetc b
= cbgetg b
true;;
112 let rc = b
.wc
- b
.len
in
113 let rc = if rc < 0 then cbcap b
+ rc else rc in
117 let cbdecr b
= b
.len
<- b
.len
- 1;;
132 { mutable scrollbw
: int
133 ; mutable scrollh
: int
134 ; mutable icase
: bool
135 ; mutable preload
: bool
136 ; mutable pagebias
: int
137 ; mutable verbose
: bool
138 ; mutable scrollstep
: int
139 ; mutable maxhfit
: bool
140 ; mutable crophack
: bool
141 ; mutable autoscrollstep
: int
142 ; mutable showall
: bool
143 ; mutable hlinks
: bool
144 ; mutable underinfo
: bool
145 ; mutable interpagespace
: interpagespace
146 ; mutable zoom
: float
147 ; mutable presentation
: bool
148 ; mutable angle
: angle
151 ; mutable savebmarks
: bool
152 ; mutable proportional
: proportional
153 ; mutable memlimit
: int
154 ; mutable texcount
: texcount
155 ; mutable sliceheight
: sliceheight
156 ; mutable thumbw
: width
157 ; mutable jumpback
: bool
158 ; mutable bgcolor
: float * float * float
159 ; mutable bedefault
: bool
160 ; mutable scrollbarinpm
: bool
164 type anchor
= pageno
* top
;;
166 type outline
= string * int * anchor
168 | Oarray
of outline array
169 | Olist
of outline list
170 | Onarrow
of string * outline array
* outline array
173 type rect
= float * float * float * float * float * float * float * float;;
175 type pagemapkey
= pageno
* width
* angle
* proportional
* gen
;;
177 let emptyanchor = (0, 0.0);;
178 let initialanchor = (-1, nan
);;
181 | Birdseye
of (conf
* leftx
* pageno
* pageno
* anchor
)
182 | Outline
of (bool * int * int * outline array
* string * int * mode
)
183 | Items
of (int * int * item array
* string * int * mode
)
184 | Textentry
of (textentry
* onleave
)
186 and onleave
= leavetextentrystatus
-> unit
187 and leavetextentrystatus
= | Cancel
| Confirm
188 and item
= string * int * action
191 | Action
of (int -> int -> string -> int -> mode
)
194 let isbirdseye = function Birdseye _
-> true | _
-> false;;
195 let istextentry = function Textentry _
-> true | _
-> false;;
198 { mutable csock
: Unix.file_descr
199 ; mutable ssock
: Unix.file_descr
203 ; mutable scrollw
: int
204 ; mutable anchor
: anchor
206 ; mutable layout
: layout list
207 ; pagemap
: (pagemapkey
, (opaque
* pixmapsize
)) Hashtbl.t
208 ; mutable pdims
: (pageno
* width
* height
* leftx
) list
209 ; mutable pagecount
: int
210 ; pagecache
: string circbuf
211 ; mutable rendering
: bool
212 ; mutable mstate
: mstate
213 ; mutable searchpattern
: string
214 ; mutable rects
: (pageno
* recttype
* rect
) list
215 ; mutable rects1
: (pageno
* recttype
* rect
) list
216 ; mutable text
: string
217 ; mutable fullscreen
: (width
* height
) option
218 ; mutable mode
: mode
219 ; mutable outlines
: outlines
220 ; mutable bookmarks
: outline list
221 ; mutable path
: string
222 ; mutable password
: string
223 ; mutable invalidated
: int
224 ; mutable colorscale
: float
225 ; mutable memused
: int
227 ; mutable throttle
: layout list
option
228 ; mutable ascrollstep
: int
229 ; mutable help
: item array
230 ; mutable docinfo
: (int * string) list
234 { pat
: string circbuf
235 ; pag
: string circbuf
236 ; nav
: anchor circbuf
250 ; autoscrollstep
= 24
256 ; presentation
= false
261 ; proportional
= true
262 ; memlimit
= 32*1024*1024
267 ; bgcolor
= (0.5, 0.5, 0.5)
269 ; scrollbarinpm
= true
273 let conf = { defconf with angle
= defconf.angle
};;
276 let strings = ("llpp version " ^
Help.version
) :: "" :: Help.keys
in
277 Array.of_list
(List.map
(fun s
-> s
, 0, Noaction
) strings);
287 ; anchor
= initialanchor
290 ; pagemap
= Hashtbl.create
10
291 ; pagecache
= cbnew 100 ""
302 ; outlines
= Olist
[]
308 { nav
= cbnew 100 (0, 0.0)
325 Printf.kprintf prerr_endline fmt
327 Printf.kprintf ignore fmt
331 let len = String.length s
in
333 let b = Buffer.create
n in
334 Buffer.add_char
b (Char.chr
((len lsr 24) land 0xff));
335 Buffer.add_char
b (Char.chr
((len lsr 16) land 0xff));
336 Buffer.add_char
b (Char.chr
((len lsr 8) land 0xff));
337 Buffer.add_char
b (Char.chr
((len lsr 0) land 0xff));
338 Buffer.add_string
b s
;
339 let s'
= Buffer.contents
b in
340 let n'
= Unix.write fd
s'
0 n in
341 if n'
!= n then failwith
"write failed";
346 let n = Unix.read fd
s 0 4 in
347 if n != 4 then failwith
"incomplete read(len)";
349 lor (Char.code
s.[0] lsl 24)
350 lor (Char.code
s.[1] lsl 16)
351 lor (Char.code
s.[2] lsl 8)
352 lor (Char.code
s.[3] lsl 0)
354 let s = String.create
len in
355 let n = Unix.read fd
s 0 len in
356 if n != len then failwith
"incomplete read(data)";
361 let b = Buffer.create
10 in
362 Buffer.add_string
b s;
363 let rec combine = function
366 Buffer.add_char
b ' '
;
369 | `
b b -> if b then "1" else "0"
371 | `i i
-> string_of_int i
372 | `f f
-> string_of_float f
373 | `I f
-> string_of_int
(truncate f
)
375 Buffer.add_string
b s;
382 let cmd = Buffer.contents
(makecmd s l
) in
383 writecmd state.csock
cmd;
389 let d = conf.winh
- h
in
396 let rec f pn ph pi fh l
=
398 | (n, _
, h
, _
) :: rest
->
399 let ips = calcips h
in
404 if isbirdseye state.mode
&& pn
= 0
409 let fh = fh + ((n - pn
) * (ph
+ pi
)) in
414 if conf.presentation
|| (isbirdseye state.mode
&& pn
= 0)
418 let fh = fh + ((state.pagecount
- pn
) * (ph
+ pi
)) + inc in
421 let fh = f 0 0 0 0 state.pdims
in
425 let getpageyh pageno
=
426 let rec f pn ph pi y l
=
428 | (n, _
, h
, _
) :: rest
->
429 let ips = calcips h
in
432 let h = if n = pageno
then h else ph
in
433 if conf.presentation
&& n = pageno
435 y
+ (pageno
- pn
) * (ph
+ pi
) + pi
, h
437 y
+ (pageno
- pn
) * (ph
+ pi
), h
439 let y = y + (if conf.presentation
then pi
else 0) in
440 let y = y + (n - pn
) * (ph
+ pi
) in
444 y + (pageno
- pn
) * (ph
+ pi
), ph
446 f 0 0 0 0 state.pdims
449 let getpagey pageno
= fst
(getpageyh pageno
);;
452 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu
=
453 let ((w
, h, ips, x
) as curr
), rest
, pdimno
, yinc
=
455 | (pageno'
, w
, h, x
) :: rest
when pageno'
= pageno
->
456 let ips = calcips h in
458 if conf.presentation
|| (isbirdseye state.mode
&& pageno
= 0)
462 (w
, h, ips, x
), rest
, pdimno
+ 1, yinc
464 prev
, pdims
, pdimno
, 0
466 let dy = dy + yinc in
467 let py = py + yinc in
468 if pageno
= state.pagecount
|| cacheleft
= 0 || dy >= sh
473 if py + h <= vy - yinc
475 let py = py + h + ips in
476 let dy = max
0 (py - y) in
486 let pagey = vy - py in
487 let pagevh = h - pagey in
488 let pagevh = min
(sh
- dy) pagevh in
489 let off = if yinc > 0 then py - vy else 0 in
490 let py = py + h + ips in
496 ; pagedispy
= dy + off
497 ; pagey = pagey + off
498 ; pagevh = pagevh - off
502 let accu = e :: accu in
509 ~cacheleft
:(cacheleft
-1)
512 if state.invalidated
= 0
522 ~cacheleft
:(cbcap state.pagecache
)
532 let y = state.y + incr
in
534 let y = min
y (state.maxy
- (if conf.maxhfit
then conf.winh
else 0)) in
538 let getopaque pageno
=
539 try Some
(Hashtbl.find
state.pagemap
540 (pageno
, state.w
, conf.angle
, conf.proportional
, state.gen
))
541 with Not_found
-> None
544 let cache pageno opaque
=
545 Hashtbl.replace
state.pagemap
546 (pageno
, state.w
, conf.angle
, conf.proportional
, state.gen
) opaque
549 let validopaque opaque
= String.length opaque
> 0;;
552 match getopaque l
.pageno
with
553 | None
when not
state.rendering
->
554 state.rendering
<- true;
555 cache l
.pageno
("", -1);
556 wcmd "render" [`i
(l
.pageno
+ 1)
563 let loadlayout layout =
564 let rec f all
= function
566 begin match getopaque l
.pageno
with
567 | None
-> render l
; f false ls
568 | Some
(opaque
, _
) -> f (all
&& validopaque opaque
) ls
572 f (layout <> []) layout;
575 let findpageforopaque opaque
=
577 (fun k
(v
, s) a
-> if v
= opaque
then Some
(k
, s) else a
)
581 let pagevisible layout n = List.exists
(fun l
-> l
.pageno
= n) layout;;
587 let memleft = conf.memlimit
- state.memused
in
590 let opaque = cbpeek state.pagecache
in
591 match findpageforopaque opaque with
592 | Some
((n, _
, _
, _
, _
), size
) ->
593 memleft + size
>= 0 && not
(pagevisible state.layout n)
600 let presentation = conf.presentation in
601 let interpagespace = conf.interpagespace in
602 let maxy = state.maxy in
603 conf.presentation <- false;
604 conf.interpagespace <- 0;
605 state.maxy <- calcheight ();
607 match state.layout with
609 | l
:: _
-> getpagey l
.pageno
+ l
.pagey
611 let y = if y < conf.winh
then 0 else y - conf.winh
in
612 let pages = layout y (conf.winh
*3) in
613 List.iter
render pages;
614 conf.presentation <- presentation;
615 conf.interpagespace <- interpagespace;
621 let y = min
state.maxy y in
622 let pages = layout y conf.winh
in
623 let ready = loadlayout pages in
629 state.layout <- pages;
630 state.throttle
<- None
;
631 Glut.postRedisplay
();
634 state.throttle
<- Some
pages;
639 state.layout <- pages;
640 state.throttle
<- None
;
641 Glut.postRedisplay
();
643 begin match state.mode
with
644 | Birdseye
(conf, leftx
, pageno
, hooverpageno
, anchor
) ->
645 if not
(pagevisible pages pageno
)
647 match state.layout with
650 state.mode
<- Birdseye
(conf, leftx
, l
.pageno
, hooverpageno
, anchor
)
657 let gotoy_and_clear_text y =
659 if not
conf.verbose
then state.text
<- "";
663 match state.layout with
665 | l
:: _
-> (l
.pageno
, float l
.pagey /. float l
.pageh
)
668 let getanchory (n, top
) =
669 let y, h = getpageyh n in
670 y + (truncate
(top
*. float h));
673 let gotoanchor anchor
=
674 gotoy (getanchory anchor
);
678 cbput state.hists
.nav
(getanchor ());
682 let anchor = cbgetc state.hists
.nav dir
in
687 let y, h = getpageyh n in
688 gotoy_and_clear_text (y + (truncate
(top
*. float h)));
691 let gotopage1 n top
=
692 let y = getpagey n in
693 gotoy_and_clear_text (y + top
);
701 state.invalidated
<- state.invalidated
+ 1;
705 let c = c *. state.colorscale
in
709 let scalecolor2 (r
, g
, b) =
710 (r
*. state.colorscale
, g
*. state.colorscale
, b *. state.colorscale
);
714 state.maxy <- calcheight ();
715 match state.mode
with
716 | Birdseye
(_
, _
, pageno
, _
, _
) ->
717 let y, h = getpageyh pageno
in
718 let top = (conf.winh
- h) / 2 in
719 gotoy (max
0 (y - top))
720 | _
-> gotoanchor state.anchor
724 GlMat.mode `projection
;
725 GlMat.load_identity
();
726 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
727 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
728 GlMat.scale3
(2.0 /. float state.w
, 2.0 /. float conf.winh
, 1.0);
731 GlMat.translate ~x
:(float state.x
) ();
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 conf.winw
, 2.0 /. float conf.winh
, 1.0);
744 if state.invalidated
= 0 && state.anchor == initialanchor
745 then state.anchor <- getanchor ();
748 let w = truncate
(float w *. conf.zoom
) - state.scrollw
in
752 GlMat.mode `modelview
;
753 GlMat.load_identity
();
754 GlClear.color
(scalecolor 1.0);
755 GlClear.clear
[`color
];
758 wcmd "geometry" [`i
w; `i
h];
762 GlDraw.color
(0.0, 0.0, 0.0);
764 (0.0, float (conf.winh
- 18))
765 (float (conf.winw
- state.scrollw
- 1), float conf.winh
)
767 let font = Glut.BITMAP_8_BY_13
in
768 GlDraw.color
(1.0, 1.0, 1.0);
769 GlPix.raster_pos ~x
:0.0 ~
y:(float (conf.winh
- 5)) ();
770 Glut.bitmapCharacter ~
font ~
c:(Char.code ' '
);
771 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s;
775 let len = String.length
state.text
in
776 match state.mode
with
777 | Textentry
((prefix
, text
, _
, _
, _
), _
) ->
779 match String.length prefix
with
783 Printf.sprintf
"%s%s^ [%s]" prefix text
state.text
785 Printf.sprintf
"%s%s^" prefix text
790 Printf.sprintf
"%s: %s^ [%s]" prefix text
state.text
792 Printf.sprintf
"%s: %s^" prefix text
797 if len > 0 then showtext state.text
801 state.text
<- Printf.sprintf
"%c%s" c s;
802 Glut.postRedisplay
();
811 state.rects
<- state.rects1
;
812 Glut.postRedisplay
()
815 let n = Scanf.sscanf
cmd "C %u" (fun n -> n) in
816 state.pagecount
<- n;
817 state.invalidated
<- state.invalidated
- 1;
818 if state.invalidated
= 0
822 let s = Scanf.sscanf
cmd "t %n"
823 (fun n -> String.sub
cmd n (String.length
cmd - n))
825 Glut.setWindowTitle
s
828 let s = Scanf.sscanf
cmd "T %n"
829 (fun n -> String.sub
cmd n (String.length
cmd - n))
831 if istextentry state.mode
838 Glut.postRedisplay
();
844 let s = Scanf.sscanf
cmd "V %n"
845 (fun n -> String.sub
cmd n (String.length
cmd - n))
851 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
852 Scanf.sscanf
cmd "F %u %d %f %f %f %f %f %f %f %f"
853 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
854 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
856 let y = (getpagey pageno) + truncate y0
in
859 state.rects1
<- [pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)]
862 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
863 Scanf.sscanf
cmd "R %u %d %f %f %f %f %f %f %f %f"
864 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
865 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
868 (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) :: state.rects1
871 let n, w, _h
, r
, l
, s, p
=
872 Scanf.sscanf
cmd "r %u %u %u %d %d %u %s"
873 (fun n w h r l
s p
->
874 (n-1, w, h, r
, l
!= 0, s, p
))
877 Hashtbl.replace
state.pagemap
(n, w, r
, l
, state.gen
) (p
, s);
878 state.memused
<- state.memused
+ s;
881 match state.throttle
with
882 | None
-> state.layout
883 | Some
layout -> layout
887 if (state.memused
<= conf.memlimit
) || cbempty state.pagecache
890 let evictedopaque = cbpeek state.pagecache
in
891 match findpageforopaque evictedopaque with
892 | None
-> failwith
"bug in gc"
893 | Some
((evictedn
, _
, _
, _
, gen
) as k
, evictedsize
) ->
894 if state.gen
!= gen
|| not
(pagevisible layout evictedn
)
896 wcmd "free" [`
s evictedopaque];
897 state.memused
<- state.memused
- evictedsize
;
898 Hashtbl.remove
state.pagemap k
;
899 cbdecr state.pagecache
;
906 cbput state.pagecache p
;
907 state.rendering
<- false;
909 begin match state.throttle
with
911 if pagevisible state.layout n
914 let allvisible = loadlayout state.layout in
915 if allvisible then preload ();
922 let y = getpagey l
.pageno + l
.pagey in
928 Scanf.sscanf
cmd "l %u %u %u %u" (fun n w h x
-> n, w, h, x
)
930 state.pdims
<- pdim :: state.pdims
933 let (l
, n, t
, h, pos
) =
934 Scanf.sscanf
cmd "o %u %u %d %u %n" (fun l
n t
h pos
-> l
, n, t
, h, pos
)
936 let s = String.sub
cmd pos
(String.length
cmd - pos
) in
938 let l = String.length
s in
939 let b = Buffer.create
(String.length
s) in
946 | '
\xa0'
when pc2 -> Buffer.add_char
b ' '
; false
949 let c = if Char.code
c land 0x80 = 0 then c else '?'
in
958 let outline = (s, l, (n, float t
/. float h)) in
960 match state.outlines with
961 | Olist
outlines -> Olist
(outline :: outlines)
962 | Oarray _
-> Olist
[outline]
963 | Onarrow _
-> Olist
[outline]
965 state.outlines <- outlines
969 let s = Scanf.sscanf
cmd "i %n"
970 (fun n -> String.sub
cmd n (String.length
cmd - n))
972 let len = String.length
s in
973 let rec fold accu pos
=
975 try String.index_from
s pos '
\n'
with Not_found
-> len
980 let line = String.sub
s pos
(eolpos - pos
) in
981 fold ((1, line)::accu) (eolpos+1)
983 state.docinfo
<- fold state.docinfo
0
986 dolog "unknown cmd `%S'" cmd
989 let now = Unix.gettimeofday
;;
993 let r, _
, _
= Unix.select
[state.csock
] [] [] delay
in
996 if state.ascrollstep
> 0
998 let y = state.y + state.ascrollstep
in
999 let y = if y >= state.maxy then 0 else y in
1005 let cmd = readcmd state.csock
in
1014 let action = function
1015 | HCprev
-> cbget cb ~
-1
1016 | HCnext
-> cbget cb
1
1017 | HCfirst
-> cbget cb ~
-(cb
.rc)
1018 | HClast
-> cbget cb
(cb
.len - 1 - cb
.rc)
1019 and cancel
() = cb
.rc <- rc
1023 let search pattern forward
=
1024 if String.length pattern
> 0
1027 match state.layout with
1030 l.pageno, (l.pagey + if forward
then 0 else 0*l.pagevh)
1033 let b = makecmd "search"
1034 [`
b conf.icase
; `i
pn; `i
py; `i
(if forward
then 1 else 0)]
1036 Buffer.add_char
b '
,'
;
1037 Buffer.add_string
b pattern
;
1038 Buffer.add_char
b '
\000'
;
1041 writecmd state.csock
cmd;
1044 let intentry text key
=
1045 let c = Char.unsafe_chr key
in
1048 let s = "x" in s.[0] <- c;
1049 let text = text ^
s in
1053 state.text <- Printf.sprintf
"invalid char (%d, `%c')" key
c;
1058 let b = Buffer.create
(String.length
s + 1) in
1059 Buffer.add_string
b s;
1060 Buffer.add_char
b c;
1064 let textentry text key
=
1065 let c = Char.unsafe_chr key
in
1067 | _
when key
>= 32 && key
< 127 ->
1068 let text = addchar text c in
1072 dolog "unhandled key %d char `%c'" key
(Char.unsafe_chr key
);
1076 let reinit angle proportional
=
1077 conf.angle
<- angle
;
1078 conf.proportional
<- proportional
;
1080 wcmd "reinit" [`i angle
; `
b proportional
];
1084 let zoom = max
0.01 (min
2.2 zoom) in
1085 if zoom <> conf.zoom
1090 state.anchor <- getanchor ();
1091 reshape conf.winw
conf.winh
;
1092 state.text <- Printf.sprintf
"zoom is now %-5.1f" (zoom *. 100.0);
1096 let enterbirdseye () =
1097 let zoom = float conf.thumbw
/. float conf.winw
in
1098 let birdseyepageno =
1099 let cy = conf.winh
/ 2 in
1103 let rec fold best
= function
1106 let d = cy - (l.pagedispy
+ l.pagevh/2)
1107 and dbest
= cy - (best
.pagedispy
+ best
.pagevh/2) in
1108 if abs
d < abs dbest
1115 state.mode
<- Birdseye
(
1116 { conf with zoom = conf.zoom }, state.x
, birdseyepageno, -1, getanchor ()
1119 conf.presentation <- false;
1120 conf.interpagespace <- 10;
1121 conf.hlinks
<- false;
1123 state.mstate
<- Mnone
;
1124 conf.showall
<- false;
1125 Glut.setCursor
Glut.CURSOR_INHERIT
;
1128 state.text <- Printf.sprintf
"birds eye mode on (zoom %3.1f%%)"
1133 reshape conf.winw
conf.winh
;
1136 let leavebirdseye (c, leftx
, pageno, _
, anchor) goback
=
1138 conf.zoom <- c.zoom;
1139 conf.presentation <- c.presentation;
1140 conf.interpagespace <- c.interpagespace;
1141 conf.showall
<- c.showall
;
1142 conf.hlinks
<- c.hlinks
;
1146 state.text <- Printf.sprintf
"birds eye mode off (zoom %3.1f%%)"
1149 reshape conf.winw
conf.winh
;
1150 state.anchor <- if goback
then anchor else (pageno, 0.0);
1153 let togglebirdseye () =
1154 match state.mode
with
1155 | Birdseye vals
-> leavebirdseye vals
true
1156 | View
| Outline _
-> enterbirdseye ()
1160 let upbirdseye (conf, leftx
, pageno, hooverpageno
, anchor) =
1161 let pageno = max
0 (pageno - 1) in
1162 let rec loop = function
1163 | [] -> gotopage1 pageno 0
1164 | l :: _
when l.pageno = pageno ->
1165 if l.pagedispy
>= 0 && l.pagey = 0
1166 then Glut.postRedisplay
()
1167 else gotopage1 pageno 0
1168 | _
:: rest
-> loop rest
1171 state.mode
<- Birdseye
(conf, leftx
, pageno, hooverpageno
, anchor)
1174 let downbirdseye (conf, leftx
, pageno, hooverpageno
, anchor) =
1175 let pageno = min
(state.pagecount
- 1) (pageno + 1) in
1176 state.mode
<- Birdseye
(conf, leftx
, pageno, hooverpageno
, anchor);
1177 let rec loop = function
1179 let y, h = getpageyh pageno in
1180 let dy = (y - state.y) - (conf.winh
- h - conf.interpagespace) in
1182 | l :: _
when l.pageno = pageno ->
1183 if l.pagevh != l.pageh
1184 then gotoy (clamp (l.pageh
- l.pagevh + conf.interpagespace))
1185 else Glut.postRedisplay
()
1186 | _
:: rest
-> loop rest
1191 let optentry mode _ key
=
1192 let btos b = if b then "on" else "off" in
1193 let c = Char.unsafe_chr key
in
1197 try conf.scrollstep
<- int_of_string
s with exc
->
1198 state.text <- Printf.sprintf
"bad integer `%s': %s"
1199 s (Printexc.to_string exc
)
1201 TEswitch
("scroll step", "", None
, intentry, ondone)
1206 conf.autoscrollstep
<- int_of_string
s;
1207 if state.ascrollstep
> 0
1208 then state.ascrollstep
<- conf.autoscrollstep
;
1210 state.text <- Printf.sprintf
"bad integer `%s': %s"
1211 s (Printexc.to_string exc
)
1213 TEswitch
("auto scroll step", "", None
, intentry, ondone)
1218 let zoom = float (int_of_string
s) /. 100.0 in
1221 state.text <- Printf.sprintf
"bad integer `%s': %s"
1222 s (Printexc.to_string exc
)
1224 TEswitch
("zoom", "", None
, intentry, ondone)
1229 conf.thumbw
<- max
2 (min
1920 (int_of_string
s));
1231 Printf.sprintf
"thumbnail width is set to %d" conf.thumbw
;
1232 begin match mode
with
1234 leavebirdseye beye
false;
1239 state.text <- Printf.sprintf
"bad integer `%s': %s"
1240 s (Printexc.to_string exc
)
1242 TEswitch
("thumbnail width", "", None
, intentry, ondone)
1247 Some
(int_of_string
s)
1249 state.text <- Printf.sprintf
"bad integer `%s': %s"
1250 s (Printexc.to_string exc
);
1253 | Some angle
-> reinit angle
conf.proportional
1256 TEswitch
("rotation", "", None
, intentry, ondone)
1259 conf.icase
<- not
conf.icase
;
1260 TEdone
("case insensitive search " ^
(btos conf.icase
))
1263 conf.preload <- not
conf.preload;
1265 TEdone
("preload " ^
(btos conf.preload))
1268 conf.verbose
<- not
conf.verbose
;
1269 TEdone
("verbose " ^
(btos conf.verbose
))
1272 conf.maxhfit
<- not
conf.maxhfit
;
1273 state.maxy <- state.maxy + (if conf.maxhfit
then -conf.winh
else conf.winh
);
1274 TEdone
("maxhfit " ^
(btos conf.maxhfit
))
1277 conf.crophack
<- not
conf.crophack
;
1278 TEdone
("crophack " ^
btos conf.crophack
)
1281 conf.showall
<- not
conf.showall
;
1282 TEdone
("throttle " ^
btos conf.showall
)
1285 conf.underinfo
<- not
conf.underinfo
;
1286 TEdone
("underinfo " ^
btos conf.underinfo
)
1289 conf.savebmarks
<- not
conf.savebmarks
;
1290 TEdone
("persistent bookmarks " ^
btos conf.savebmarks
)
1296 match state.layout with
1301 conf.interpagespace <- int_of_string
s;
1302 state.maxy <- calcheight ();
1303 let y = getpagey pageno in
1306 state.text <- Printf.sprintf
"bad integer `%s': %s"
1307 s (Printexc.to_string exc
)
1309 TEswitch
("vertical margin", "", None
, intentry, ondone)
1312 reinit conf.angle
(not
conf.proportional
);
1313 TEdone
("proprortional display " ^
btos conf.proportional
)
1316 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
1320 let maxoutlinerows () = (conf.winh
- 31) / 16;;
1322 let enterselector allowdel
outlines errmsg msg
=
1323 if Array.length
outlines = 0
1325 showtext ' ' errmsg
;
1329 Glut.setCursor
Glut.CURSOR_INHERIT
;
1331 match state.layout with
1333 | {pageno=pageno} :: _
-> pageno
1337 if n = Array.length
outlines
1340 let (_
, _
, (outlinepageno
, _
)) = outlines.(n) in
1341 if outlinepageno
>= pageno then n else loop (n+1)
1345 state.mode
<- Outline
1346 (allowdel
, active, max
0 (active - maxoutlinerows () / 2), outlines, "", 0,
1348 Glut.postRedisplay
();
1352 let enteroutlinemode () =
1354 match state.outlines with
1357 let a = Array.of_list
(List.rev
l) in
1358 state.outlines <- Oarray
a;
1360 | Onarrow
(pat
, a, _
) ->
1361 a, "Outline was narrowed to `" ^ pat ^
"' (Ctrl-u to restore)"
1363 enterselector false outlines "Document has no outline" msg
;
1366 let enterbookmarkmode () =
1367 let bookmarks = Array.of_list
state.bookmarks in
1368 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1371 let mode_to_string mode
=
1372 let b = Buffer.create
10 in
1373 let rec f = function
1374 | Textentry
(_
, _
) -> Buffer.add_string
b "Textentry ";
1375 | View
-> Buffer.add_string
b "View"
1376 | Birdseye _
-> Buffer.add_string
b "Birdseye"
1377 | Items _
-> Buffer.add_string
b "Items"
1378 | Outline _
-> Buffer.add_string
b "Outline"
1384 let color_of_string s =
1385 Scanf.sscanf
s "%d/%d/%d" (fun r g
b ->
1386 (float r /. 256.0, float g
/. 256.0, float b /. 256.0)
1390 let color_to_string (r, g
, b) =
1391 let r = truncate
(r *. 256.0)
1392 and g
= truncate
(g
*. 256.0)
1393 and b = truncate
(b *. 256.0) in
1394 Printf.sprintf
"%d/%d/%d" r g
b
1397 let enterinfomode () =
1398 let btos = function true -> "on" | _
-> "off" in
1399 let mode = state.mode in
1400 let rec makeitems () =
1401 let intp name get set
=
1402 Printf.sprintf
"%-24s %d" name
(get
()), 1, Action
(
1403 fun active first _ pan
->
1408 state.text <- Printf.sprintf
"bad integer `%s': %s"
1409 s (Printexc.to_string exn
);
1412 if n != max_int
then set
n;
1414 let te = name
, "", None
, intentry, ondone in
1419 state.mode <- Items
(active, first
, makeitems (), "", pan
, mode)
1422 and boolp name get set
=
1423 Printf.sprintf
"%-24s %s" name
(btos (get
())), 1, Action
(
1424 fun active first qsearch pan
->
1427 Items
(active, first
, makeitems (), qsearch
, pan
, mode);
1429 and colorp name get set
=
1430 Printf.sprintf
"%-24s %s" name
(color_to_string (get
())), 1, Action
(
1431 fun active first _ pan
->
1432 let invalid = (nan
, nan
, nan
) in
1435 try color_of_string s
1437 state.text <- Printf.sprintf
"bad color `%s': %s"
1438 s (Printexc.to_string exn
);
1444 let te = name
, "", None
, textentry, ondone in
1449 state.mode <- Items
(active, first
, makeitems (), "", pan
, mode)
1454 let birdseye = isbirdseye mode in
1456 (if birdseye then "Setup bird's eye" else "Setup"), 0, Noaction
;
1458 boolp
"presentation"
1459 (fun () -> conf.presentation)
1461 conf.presentation <- v;
1462 state.anchor <- getanchor ();
1465 boolp
"ignore case in searches"
1466 (fun () -> conf.icase
)
1467 (fun v -> conf.icase
<- v);
1470 (fun () -> conf.preload)
1471 (fun v -> conf.preload <- v);
1474 (fun () -> conf.verbose
)
1475 (fun v -> conf.verbose
<- v);
1478 (fun () -> conf.maxhfit
)
1479 (fun v -> conf.maxhfit
<- v);
1482 (fun () -> conf.crophack
)
1483 (fun v -> conf.crophack
<- v);
1486 (fun () -> conf.showall
)
1487 (fun v -> conf.showall
<- v);
1489 boolp
"highlight links"
1490 (fun () -> conf.hlinks
)
1491 (fun v -> conf.hlinks
<- v);
1494 (fun () -> conf.underinfo
)
1495 (fun v -> conf.underinfo
<- v);
1496 boolp
"persistent bookmarks"
1497 (fun () -> conf.savebmarks
)
1498 (fun v -> conf.savebmarks
<- v);
1500 boolp
"proportional display"
1501 (fun () -> conf.proportional
)
1502 (fun v -> reinit conf.angle
v);
1504 boolp
"persistent location"
1505 (fun () -> conf.jumpback
)
1506 (fun v -> conf.jumpback
<- v);
1510 intp "vertical margin"
1511 (fun () -> conf.interpagespace)
1513 conf.interpagespace <- n;
1515 match state.layout with
1520 state.maxy <- calcheight ();
1521 let y = getpagey pageno in
1526 (fun () -> conf.pagebias
)
1527 (fun v -> conf.pagebias
<- v);
1530 (fun () -> conf.scrollstep
)
1531 (fun n -> conf.scrollstep
<- n);
1533 intp "auto scroll step"
1535 if state.ascrollstep
> 0
1536 then state.ascrollstep
1537 else conf.autoscrollstep
)
1539 if state.ascrollstep
> 0
1540 then state.ascrollstep
<- n
1541 else conf.autoscrollstep
<- n);
1544 (fun () -> truncate
(conf.zoom *. 100.))
1545 (fun v -> setzoom ((float v) /. 100.));
1548 (fun () -> conf.angle
)
1549 (fun v -> reinit v conf.proportional
);
1551 intp "scroll bar width"
1552 (fun () -> state.scrollw
)
1555 reshape conf.winw
conf.winh
;
1558 intp "scroll handle height"
1559 (fun () -> conf.scrollh
)
1560 (fun v -> conf.scrollh
<- v;);
1562 intp "thumbnail width"
1563 (fun () -> conf.thumbw
)
1565 conf.thumbw
<- min
1920 v;
1568 leavebirdseye beye
false;
1573 colorp
"background color"
1574 (fun () -> conf.bgcolor
)
1575 (fun v -> conf.bgcolor
<- v);
1578 "Presentation mode", 0, Noaction
;
1580 boolp
"scrollbar visible"
1581 (fun () -> conf.scrollbarinpm
)
1583 if v != conf.scrollbarinpm
then (
1584 conf.scrollbarinpm
<- v;
1585 if conf.presentation
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
"%-24s %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");
1666 writecmd state.csock
"info";
1669 let opendoc path password
=
1672 state.password
<- password
;
1673 state.gen
<- state.gen
+ 1;
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'
| 'q'
-> (* escape *)
1693 | '
\008'
-> (* backspace *)
1694 let y = getnav ~
-1 in
1695 gotoy_and_clear_text y
1703 Glut.postRedisplay
()
1706 let ondone isforw
s =
1707 cbput state.hists
.pat
s;
1708 state.searchpattern
<- s;
1711 let s = String.create
1 in
1713 enttext (s, "", Some
(onhist state.hists
.pat
),
1714 textentry, ondone (c ='
/'
))
1716 | '
+'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1717 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1718 setzoom (min
2.2 (conf.zoom +. incr))
1723 try int_of_string
s with exc
->
1724 state.text <- Printf.sprintf
"bad integer `%s': %s"
1725 s (Printexc.to_string exc
);
1731 state.text <- "page bias is now " ^ string_of_int
n;
1734 enttext ("page bias", "", None
, intentry, ondone)
1736 | '
-'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1737 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1738 setzoom (max
0.01 (conf.zoom -. decr))
1741 let ondone msg
= state.text <- msg
in
1743 "option [acfhilpstvAPRSZ]", "", None
,
1744 optentry state.mode, ondone
1747 | '
0'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1750 | '
1'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1751 let zoom = zoomforh
conf.winw
conf.winh
state.scrollw
in
1755 | '
9'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1761 try int_of_string
s with exc
->
1762 state.text <- Printf.sprintf
"bad integer `%s': %s"
1763 s (Printexc.to_string exc
);
1769 cbput state.hists
.pag
(string_of_int
n);
1770 gotoy_and_clear_text (getpagey (n + conf.pagebias
- 1))
1773 let pageentry text key
=
1774 match Char.unsafe_chr key
with
1775 | 'g'
-> TEdone
text
1776 | _
-> intentry text key
1778 let text = "x" in text.[0] <- c;
1779 enttext (":", text, Some
(onhist state.hists
.pag
), pageentry, ondone)
1782 state.scrollw
<- if state.scrollw
> 0 then 0 else conf.scrollbw
;
1783 reshape conf.winw
conf.winh
;
1786 conf.hlinks
<- not
conf.hlinks
;
1787 state.text <- "highlightlinks " ^
if conf.hlinks
then "on" else "off";
1788 Glut.postRedisplay
()
1791 if state.ascrollstep
= 0
1792 then state.ascrollstep
<- conf.autoscrollstep
1794 conf.autoscrollstep
<- state.ascrollstep
;
1795 state.ascrollstep
<- 0;
1799 conf.presentation <- not
conf.presentation;
1800 if conf.presentation
1802 if not
conf.scrollbarinpm
1803 then state.scrollw
<- 0;
1806 state.scrollw
<- conf.scrollbw
;
1808 showtext ' '
("presentation mode " ^
1809 if conf.presentation then "on" else "off");
1810 state.anchor <- getanchor ();
1814 begin match state.fullscreen
with
1816 state.fullscreen
<- Some
(conf.winw
, conf.winh
);
1819 state.fullscreen
<- None
;
1824 gotoy_and_clear_text 0
1827 search state.searchpattern
true
1830 search state.searchpattern
false
1833 begin match state.layout with
1836 gotoy_and_clear_text (getpagey l.pageno)
1840 begin match List.rev
state.layout with
1843 let pageno = min
(l.pageno+1) (state.pagecount
-1) in
1844 gotoy_and_clear_text (getpagey pageno)
1847 | '
\127'
-> (* delte *)
1848 begin match state.layout with
1851 let pageno = max
0 (l.pageno-1) in
1852 gotoy_and_clear_text (getpagey pageno)
1857 if fn
= -1 then l.pageno, l.pageno else fn
, l.pageno
1859 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
1861 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
1865 else (100. *. (float state.y /. float maxy)) in
1868 Printf.sprintf
"Page %d of %d %.2f%%"
1869 (fn+1) state.pagecount
percent
1872 "Pages %d-%d of %d %.2f%%"
1873 (fn+1) (ln
+1) state.pagecount
percent
1878 begin match state.layout with
1881 doreshape (l.pagew
+ state.scrollw
) l.pageh
;
1882 Glut.postRedisplay
();
1886 enterbookmarkmode ()
1896 match state.layout with
1899 (s, 0, (l.pageno, float l.pagey /. float l.pageh
))
1903 enttext ("bookmark", "", None
, textentry, ondone)
1907 showtext ' '
"Quick bookmark added";
1910 begin match state.layout with
1912 let rect = getpdimrect
l.pagedimno
in
1916 (truncate
(1.8 *. (rect.(1) -. rect.(0))),
1917 truncate
(1.2 *. (rect.(3) -. rect.(0))))
1919 (truncate
(rect.(1) -. rect.(0)),
1920 truncate
(rect.(3) -. rect.(0)))
1924 doreshape (w + state.scrollw
) (h + conf.interpagespace)
1926 Glut.postRedisplay
();
1932 state.text <- "zoom is reset to 100";
1933 Glut.reshapeWindow
(state.w + state.scrollw
)
1934 (truncate
(float conf.winh
*. conf.zoom));
1936 Glut.postRedisplay
()
1939 reinit (conf.angle
+ (if c = '
>'
then 30 else -30)) conf.proportional
1944 (min
(state.colorscale
+. (if c = '
]'
then 0.1 else -0.1)) 1.0);
1945 Glut.postRedisplay
()
1948 begin match state.mode with
1949 | Birdseye beye
-> upbirdseye beye
1950 | _
-> gotoy (clamp (-conf.scrollstep
))
1954 begin match state.mode with
1955 | Birdseye beye
-> downbirdseye beye
1956 | _
-> gotoy (clamp conf.scrollstep
)
1960 state.anchor <- getanchor ();
1961 opendoc state.path
state.password
1964 vlog "huh? %d %c" key
(Char.chr key
);
1967 let textentrykeyboard key
((c, text, opthist
, onkey
, ondone), onleave
) =
1969 state.mode <- Textentry
(te, onleave
);
1972 Glut.postRedisplay
()
1974 match Char.unsafe_chr key
with
1975 | '
\008'
-> (* backspace *)
1976 let len = String.length
text in
1980 Glut.postRedisplay
();
1983 let s = String.sub
text 0 (len - 1) in
1984 enttext (c, s, opthist
, onkey
, ondone)
1990 Glut.postRedisplay
()
1992 | '
\027'
-> (* escape *)
1993 begin match opthist
with
1995 | Some
(_
, onhistcancel
) -> onhistcancel
()
1998 Glut.postRedisplay
()
2001 begin match onkey
text key
with
2005 Glut.postRedisplay
()
2008 enttext (c, text, opthist
, onkey
, ondone);
2012 Glut.postRedisplay
()
2015 state.mode <- Textentry
(te, onleave
);
2016 Glut.postRedisplay
()
2020 let birdseyekeyboard key
((_
, _
, pageno, _
, _
) as beye
) =
2022 | 27 -> (* escape *)
2023 leavebirdseye beye
true
2025 | 12 -> (* ctrl-l *)
2026 let y, h = getpageyh pageno in
2027 let top = (conf.winh
- h) / 2 in
2028 gotoy (max
0 (y - top))
2031 leavebirdseye beye
false
2037 let itemskeyboard key
(active, first
, items, qsearch
, pan
, oldmode
) =
2038 let set active first qsearch
=
2039 state.mode <- Items
(active, first
, items, qsearch
, pan
, oldmode
)
2041 let search active pattern
incr =
2044 if n = Array.length
items || n = -1
2047 let (s, _
, _
) = items.(n) in
2049 (try ignore
(Str.search_forward re
s 0); true
2050 with Not_found
-> false)
2052 else loop (n + incr)
2057 let re = Str.regexp_case_fold pattern
in
2063 let firstof active = max
0 (active - maxoutlinerows () / 2) in
2065 | 18 | 19 -> (* ctrl-r/ctlr-s *)
2066 let incr = if key
= 18 then -1 else 1 in
2068 match search (active + incr) qsearch
incr with
2070 state.text <- qsearch ^
" [not found]";
2073 state.text <- qsearch
;
2074 active, firstof active
2076 set active first qsearch
;
2077 Glut.postRedisplay
();
2079 | 8 -> (* backspace *)
2080 let len = String.length qsearch
in
2087 set active first
"";
2090 let qsearch = String.sub
qsearch 0 (len - 1) in
2092 match search active qsearch ~
-1 with
2094 state.text <- qsearch ^
" [not found]";
2097 state.text <- qsearch;
2098 active, firstof active
2100 set active first
qsearch
2102 Glut.postRedisplay
()
2104 | _
when key
>= 32 && key
< 127 ->
2105 let pattern = addchar qsearch (Char.chr key
) in
2107 match search active pattern 1 with
2109 state.text <- pattern ^
" [not found]";
2112 state.text <- pattern;
2113 active, firstof active
2115 set active first
pattern;
2116 Glut.postRedisplay
()
2118 | 27 -> (* escape *)
2120 state.mode <- oldmode
;
2121 Glut.postRedisplay
();
2124 if active < Array.length
items
2126 match items.(active) with
2128 state.mode <- f active first
qsearch pan
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 *)
2187 if String.length
qsearch = 0
2190 state.mode <- oldmode
;
2191 Glut.postRedisplay
();
2195 state.mode <- Outline
(
2196 allowdel
, active, first
, outlines, "", pan
, oldmode
2198 Glut.postRedisplay
();
2201 | 18 | 19 -> (* ctrl-r/ctrl-s *)
2202 let incr = if key
= 18 then -1 else 1 in
2204 match search (active + incr) qsearch incr with
2206 state.text <- qsearch ^
" [not found]";
2209 state.text <- qsearch;
2210 active, firstof active
2212 state.mode <- Outline
(
2213 allowdel
, active, first
, outlines, qsearch, pan
, oldmode
2215 Glut.postRedisplay
();
2217 | 8 -> (* backspace *)
2218 let len = String.length
qsearch in
2225 state.mode <- Outline
(
2226 allowdel
, active, first
, outlines, "", pan
, oldmode
2230 let qsearch = String.sub
qsearch 0 (len - 1) in
2232 match search active qsearch ~
-1 with
2234 state.text <- qsearch ^
" [not found]";
2237 state.text <- qsearch;
2238 active, firstof active
2240 state.mode <- Outline
(
2241 allowdel
, active, first
, outlines, qsearch, pan
, oldmode
2244 Glut.postRedisplay
()
2247 if active < Array.length
outlines
2249 let (_
, _
, anchor) = outlines.(active) in
2254 if allowdel
then state.bookmarks <- Array.to_list
outlines;
2255 state.mode <- oldmode
;
2256 Glut.postRedisplay
();
2258 | _
when key
>= 32 && key
< 127 ->
2259 let pattern = addchar qsearch (Char.chr key
) in
2261 match search active pattern 1 with
2263 state.text <- pattern ^
" [not found]";
2266 state.text <- pattern;
2267 active, firstof active
2269 state.mode <- Outline
(
2270 allowdel
, active, first
, outlines, pattern, pan
, oldmode
2272 Glut.postRedisplay
()
2274 | 14 when not allowdel
-> (* ctrl-n *)
2275 if String.length
qsearch > 0
2277 let optoutlines = narrow outlines qsearch in
2278 begin match optoutlines with
2279 | None
-> state.text <- "can't narrow"
2281 state.mode <- Outline
(
2282 allowdel
, 0, 0, outlines, qsearch, pan
, oldmode
2284 match state.outlines with
2287 state.outlines <- Onarrow
(qsearch, outlines, a)
2288 | Onarrow
(_
, _
, b) ->
2289 state.outlines <- Onarrow
(qsearch, outlines, b)
2292 Glut.postRedisplay
()
2294 | 21 when not allowdel
-> (* ctrl-u *)
2296 match state.outlines with
2299 let a = Array.of_list
(List.rev
l) in
2300 state.outlines <- Oarray
a;
2302 | Onarrow
(_
, _
, b) ->
2303 state.outlines <- Oarray
b;
2307 state.mode <- Outline
(allowdel
, 0, 0, outline, qsearch, pan
, oldmode
);
2308 Glut.postRedisplay
()
2310 | 12 -> (* ctrl-l *)
2311 state.mode <- Outline
2312 (allowdel
, active, firstof active, outlines, qsearch, pan
, oldmode
);
2313 Glut.postRedisplay
()
2315 | 127 when allowdel
-> (* delete *)
2316 let len = Array.length
outlines - 1 in
2320 state.bookmarks <- [];
2323 let bookmarks = Array.init
len
2325 let i = if i >= active then i + 1 else i in
2339 Glut.postRedisplay
()
2341 | _
-> dolog "unknown key %d" key
2344 let keyboard ~key ~x ~
y =
2347 if key
= 7 (* ctrl-g *)
2351 match state.mode with
2352 | Outline
outline -> outlinekeyboard key
outline
2353 | Textentry
textentry -> textentrykeyboard key
textentry
2354 | Birdseye
birdseye -> birdseyekeyboard key
birdseye
2355 | View
-> viewkeyboard key
2356 | Items
items -> itemskeyboard key
items
2359 let birdseyespecial key
((conf, leftx
, _
, hooverpageno
, anchor) as beye
) =
2361 | Glut.KEY_UP
-> upbirdseye beye
2362 | Glut.KEY_DOWN
-> downbirdseye beye
2364 | Glut.KEY_PAGE_UP
->
2365 begin match state.layout with
2369 state.mode <- Birdseye
(
2370 conf, leftx
, l.pageno, hooverpageno
, anchor
2372 gotopage1 l.pageno 0;
2375 let layout = layout (state.y-conf.winh
) conf.winh
in
2377 | [] -> gotoy (clamp (-conf.winh
))
2379 state.mode <- Birdseye
(
2380 conf, leftx
, l.pageno, hooverpageno
, anchor
2382 gotopage1 l.pageno 0
2385 | [] -> gotoy (clamp (-conf.winh
))
2388 | Glut.KEY_PAGE_DOWN
->
2389 begin match List.rev
state.layout with
2391 let layout = layout (state.y + conf.winh
) conf.winh
in
2392 begin match layout with
2394 let incr = l.pageh
- l.pagevh in
2399 conf, leftx
, state.pagecount
- 1, hooverpageno
, anchor
2401 Glut.postRedisplay
();
2403 else gotoy (clamp (incr + conf.interpagespace*2));
2407 Birdseye
(conf, leftx
, l.pageno, hooverpageno
, anchor);
2408 gotopage1 l.pageno 0;
2411 | [] -> gotoy (clamp conf.winh
)
2415 state.mode <- Birdseye
(conf, leftx
, 0, hooverpageno
, anchor);
2419 let pageno = state.pagecount
- 1 in
2420 state.mode <- Birdseye
(conf, leftx
, pageno, hooverpageno
, anchor);
2421 if not
(pagevisible state.layout pageno)
2424 match List.rev
state.pdims
with
2426 | (_
, _
, h, _
) :: _
-> h
2428 gotoy (max
0 (getpagey pageno - (conf.winh
- h - conf.interpagespace)))
2429 else Glut.postRedisplay
();
2433 let setautoscrollspeed goingdown
=
2434 let incr = max
1 (state.ascrollstep
/ 2) in
2435 let astep = max
1 (state.ascrollstep
+ (if goingdown
then incr else -incr)) in
2436 state.ascrollstep
<- astep;
2439 let special ~key ~x ~
y =
2442 match state.mode with
2443 | View
| (Birdseye _
) when key
= Glut.KEY_F9
->
2447 birdseyespecial key vals
2449 | View
when key
= Glut.KEY_F1
->
2453 if state.ascrollstep
> 0 && (key
= Glut.KEY_DOWN
|| key
= Glut.KEY_UP
)
2454 then setautoscrollspeed (key
= Glut.KEY_DOWN
)
2458 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
2459 | Glut.KEY_UP
-> clamp (-conf.scrollstep
)
2460 | Glut.KEY_DOWN
-> clamp conf.scrollstep
2461 | Glut.KEY_PAGE_UP
->
2462 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2464 match state.layout with
2466 | l :: _
-> state.y - l.pagey
2469 | Glut.KEY_PAGE_DOWN
->
2470 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2472 match List.rev
state.layout with
2474 | l :: _
-> getpagey l.pageno
2477 | Glut.KEY_HOME
-> addnav (); 0
2480 state.maxy - (if conf.maxhfit
then conf.winh
else 0)
2482 | (Glut.KEY_RIGHT
| Glut.KEY_LEFT
) when
2483 Glut.getModifiers
() land Glut.active_alt
!= 0 ->
2484 getnav (if key
= Glut.KEY_LEFT
then 1 else -1)
2486 | Glut.KEY_RIGHT
when conf.zoom > 1.0 ->
2487 state.x
<- state.x
- 10;
2489 | Glut.KEY_LEFT
when conf.zoom > 1.0 ->
2490 state.x
<- state.x
+ 10;
2495 gotoy_and_clear_text y
2498 ((c, _
, (Some
(action, _
) as onhist), onkey
, ondone), mode) ->
2501 | Glut.KEY_UP
-> action HCprev
2502 | Glut.KEY_DOWN
-> action HCnext
2503 | Glut.KEY_HOME
-> action HCfirst
2504 | Glut.KEY_END
-> action HClast
2507 state.mode <- Textentry
((c, s, onhist, onkey
, ondone), mode);
2508 Glut.postRedisplay
()
2512 | Items
(active, first
, items, qsearch, pan
, oldmode
) ->
2513 let maxrows = maxoutlinerows () in
2514 let itemcount = Array.length
items in
2515 let hasaction = function
2516 | (_
, _
, Noaction
) -> false
2519 let find start
incr =
2521 if i = -1 || i = itemcount
2524 if hasaction items.(i)
2526 else find (i + incr)
2531 let set active first
=
2532 let first = max
0 (min
first (itemcount - maxrows)) in
2533 state.mode <- Items
(active, first, items, qsearch, pan
, oldmode
)
2536 let isvisible first n = n >= first && n - first <= maxrows in
2538 let incr1 = if incr > 0 then 1 else -1 in
2539 if isvisible first active
2542 let next = active + incr in
2544 if next < 0 || next >= itemcount
2546 else find next incr1
2548 if next = -1 || abs
(active - next) > maxrows
2554 let first = first + incr in
2555 let first = max
0 (min
first (itemcount - 1)) in
2557 let next = active + incr in
2558 let next = max
0 (min
next (itemcount - 1)) in
2561 let active = if next = -1 then active else next in
2564 let first = min
next first in
2567 let first = first + incr in
2568 let first = max
0 (min
first (itemcount - 1)) in
2570 let next = active + incr in
2571 let next = max
0 (min
next (itemcount - 1)) in
2572 let next = find next incr1 in
2573 if next = -1 || abs
(active - first) > maxrows
2580 Glut.postRedisplay
()
2582 begin match key
with
2583 | Glut.KEY_UP
-> navigate ~
-1
2584 | Glut.KEY_DOWN
-> navigate 1
2585 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
2586 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
2589 state.mode <- Items
(
2590 active, first, items, qsearch, min
0 (pan
- 1), oldmode
2592 Glut.postRedisplay
()
2595 state.mode <- Items
(
2596 active, first, items, qsearch, min
0 (pan
+ 1), oldmode
2598 Glut.postRedisplay
()
2601 let active = find 0 1 in
2603 Glut.postRedisplay
()
2606 let first = max
0 (itemcount - maxrows) in
2607 let active = find (itemcount - 1) ~
-1 in
2609 Glut.postRedisplay
()
2614 | Outline
(allowdel
, active, first, outlines, qsearch, pan
, oldmode
) ->
2615 let maxrows = maxoutlinerows () in
2616 let calcfirst first active =
2619 let rows = active - first in
2620 if rows > maxrows then active - maxrows else first
2624 let active = active + incr in
2625 let active = max
0 (min
active (Array.length
outlines - 1)) in
2626 let first = calcfirst first active in
2627 state.mode <- Outline
(
2628 allowdel
, active, first, outlines, qsearch, pan
, oldmode
2630 Glut.postRedisplay
()
2632 let updownlevel incr =
2633 let len = Array.length
outlines in
2634 let (_
, curlevel
, _
) = outlines.(active) in
2636 if i = len then i-1 else if i = -1 then 0 else
2637 let (_
, l, _
) = outlines.(i) in
2638 if l != curlevel
then i else flow (i+incr)
2640 let active = flow active in
2641 let first = calcfirst first active in
2642 state.mode <- Outline
(
2643 allowdel
, active, first, outlines, qsearch, pan
, oldmode
2645 Glut.postRedisplay
()
2648 | Glut.KEY_UP
-> navigate ~
-1
2649 | Glut.KEY_DOWN
-> navigate 1
2650 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
2651 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
2654 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2656 state.mode <- Outline
(
2657 allowdel
, active, first, outlines,
2658 qsearch, min
0 (pan
+ 1), oldmode
2660 Glut.postRedisplay
();
2668 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2670 state.mode <- Outline
(
2671 allowdel
, active, first, outlines, qsearch, pan
- 1, oldmode
2673 Glut.postRedisplay
();
2677 then updownlevel ~
-1
2681 state.mode <- Outline
(
2682 allowdel
, 0, 0, outlines, qsearch, pan
, oldmode
2684 Glut.postRedisplay
()
2687 let active = Array.length
outlines - 1 in
2688 let first = max
0 (active - maxrows) in
2689 state.mode <- Outline
(
2690 allowdel
, active, first, outlines, qsearch, pan
, oldmode
2692 Glut.postRedisplay
()
2697 let drawplaceholder l =
2698 let margin = state.x
+ (conf.winw
- (state.w + state.scrollw
)) / 2 in
2700 (float l.pagex
, float l.pagedispy
)
2701 (float (l.pagew
+ l.pagex
), float (l.pagedispy
+ l.pagevh))
2703 let x = float (if margin < 0 then -margin else l.pagex
)
2704 and y = float (l.pagedispy
+ 13) in
2705 let font = Glut.BITMAP_8_BY_13
in
2706 GlDraw.color
(0.0, 0.0, 0.0);
2707 GlPix.raster_pos ~
x ~
y ();
2708 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c))
2709 ("Loading " ^ string_of_int
(l.pageno + 1));
2712 let now () = Unix.gettimeofday
();;
2716 match state.mode with
2717 | Textentry _
-> scalecolor 0.4
2718 | View
| Outline _
| Items _
-> scalecolor 1.0
2719 | Birdseye
(_
, _
, pageno, hooverpageno
, _
) ->
2720 if l.pageno = hooverpageno
2723 if l.pageno = pageno
2729 begin match getopaque l.pageno with
2730 | Some
(opaque, _
) when validopaque opaque ->
2732 draw
(l.pagedispy
, l.pagew
, l.pagevh, l.pagey, conf.hlinks
)
2736 vlog "draw %d %f sec" l.pageno d;
2744 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
2745 let sh = (float (maxy + conf.winh
) /. float conf.winh
) in
2746 let sh = float conf.winh
/. sh in
2747 let sh = max
sh (float conf.scrollh
) in
2750 if state.y = state.maxy
2752 else float y /. float maxy
2754 let position = (float conf.winh
-. sh) *. percent in
2757 if position +. sh > float conf.winh
2758 then float conf.winh
-. sh
2764 let scrollindicator () =
2765 GlDraw.color (0.64 , 0.64, 0.64);
2767 (float (conf.winw
- state.scrollw
), 0.)
2768 (float conf.winw
, float conf.winh
)
2770 GlDraw.color (0.0, 0.0, 0.0);
2772 let position, sh = scrollph state.y in
2774 (float (conf.winw
- state.scrollw
), position)
2775 (float conf.winw
, position +. sh)
2779 let showsel margin =
2780 match state.mstate
with
2781 | Mnone
| Mscroll _
| Mpan _
| Mzoom _
->
2784 | Msel
((x0
, y0
), (x1
, y1
)) ->
2785 let rec loop = function
2787 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh))
2788 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh)))
2790 match getopaque l.pageno with
2791 | Some
(opaque, _
) when validopaque opaque ->
2792 let oy = -l.pagey + l.pagedispy
in
2794 (x0
- margin - state.x, y0
,
2795 x1
- margin - state.x, y1
) oy;
2805 let panx = float state.x in
2807 GlDraw.color (0.0, 0.0, 1.0) ~alpha
:0.5;
2808 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2810 (fun (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) ->
2812 if l.pageno = pageno
2814 let d = float (l.pagedispy
- l.pagey) in
2815 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
2816 GlDraw.begins `quads
;
2818 GlDraw.vertex2
(x0
+.panx, y0
+.d);
2819 GlDraw.vertex2
(x1
+.panx, y1
+.d);
2820 GlDraw.vertex2
(x2
+.panx, y2
+.d);
2821 GlDraw.vertex2
(x3
+.panx, y3
+.d);
2831 let showstrings active first pan
strings =
2833 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2834 GlDraw.color (0., 0., 0.) ~alpha
:0.85;
2835 GlDraw.rect (0., 0.) (float conf.winw
, float conf.winh
);
2838 GlDraw.color (1., 1., 1.);
2839 let font = Glut.BITMAP_9_BY_15
in
2840 let draw_string x y s =
2841 GlPix.raster_pos ~
x ~
y ();
2842 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s
2845 if row
= Array.length
strings || (row
- first) * 16 > conf.winh
2848 let (s, level
, _
) = strings.(row
) in
2849 let y = (row
- first) * 16 in
2850 let x = 5 + 15*(max
0 (level
+pan
)) in
2854 GlDraw.polygon_mode `both `
line;
2855 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2856 GlDraw.color (1., 1., 1.) ~alpha
:0.9;
2857 GlDraw.rect (0., float (y + 1))
2858 (float (conf.winw
- 1), float (y + 18));
2859 GlDraw.polygon_mode `both `fill
;
2861 GlDraw.color (1., 1., 1.);
2864 let l = String.length
s in
2867 let pan = pan * 2 in
2868 let pos = pan + level
in
2869 let left = l + pos in
2875 else String.sub
s (-pos) left
2877 draw_string (float x) (float (y + 16)) s
2880 draw_string (float (x + pan*15)) (float (y + 16)) s
2889 let showoutline (_
, active, first, outlines, _
, pan, _
) =
2890 showstrings active first pan outlines;
2893 let showitems (active, first, items, _
, pan, _
) =
2894 showstrings active first pan items;
2898 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
2899 GlDraw.viewport
margin 0 state.w conf.winh
;
2901 GlClear.color (scalecolor2 conf.bgcolor
);
2902 GlClear.clear
[`
color];
2905 Gl.enable `scissor_test
;
2906 GlMisc.scissor
0 0 (conf.winw
- state.scrollw
) conf.winh
;
2908 List.iter
drawpage state.layout;
2911 Gl.disable `scissor_test
2915 let x = -.float state.x in
2916 GlMat.translate ~
x ();
2920 GlDraw.viewport
0 0 conf.winw
conf.winh
;
2923 begin match state.mode with
2924 | Items
items -> showitems items
2925 | Outline
outline -> showoutline outline
2929 Glut.swapBuffers
();
2933 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
2934 let x = x - margin - state.x in
2935 let rec f = function
2937 begin match getopaque l.pageno with
2938 | Some
(opaque, _
) when validopaque opaque ->
2939 let y = y - l.pagedispy
in
2942 let y = l.pagey + y in
2943 let x = x - l.pagex
in
2944 match whatsunder
opaque x y with
2957 let viewmouse button bstate
x y =
2959 | Glut.OTHER_BUTTON
n when (n == 3 || n == 4) && bstate
= Glut.UP
->
2960 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2962 match state.mstate
with
2963 | Mzoom
(oldn
, i) ->
2971 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
2973 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
2975 let zoom = conf.zoom +. incr in
2977 state.mstate
<- Mzoom
(n, 0);
2979 state.mstate
<- Mzoom
(n, i+1);
2981 else state.mstate
<- Mzoom
(n, 0)
2983 | _
-> state.mstate
<- Mzoom
(n, 0)
2986 if state.ascrollstep
> 0
2988 setautoscrollspeed (n=4)
2992 then -conf.scrollstep
2993 else conf.scrollstep
2995 let incr = incr * 2 in
2996 let y = clamp incr in
2997 gotoy_and_clear_text y
3000 | Glut.LEFT_BUTTON
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
3001 if bstate
= Glut.DOWN
3003 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
3004 state.mstate
<- Mpan
(x, y)
3007 state.mstate
<- Mnone
3009 | Glut.LEFT_BUTTON
when x > conf.winw
- state.scrollw
->
3010 if bstate
= Glut.DOWN
3012 let position, sh = scrollph state.y in
3013 if y > truncate
position && y < truncate
(position +. sh)
3015 state.mstate
<- Mscroll
3017 let percent = float y /. float conf.winh
in
3018 let desty = truncate
(float (state.maxy - conf.winh
) *. percent) in
3020 state.mstate
<- Mscroll
3022 state.mstate
<- Mnone
3024 | Glut.LEFT_BUTTON
->
3025 let dest = if bstate
= Glut.DOWN
then getunder x y else Unone
in
3026 begin match dest with
3027 | Ulinkgoto
(pageno, top) ->
3031 gotopage1 pageno top;
3037 | Unone
when bstate
= Glut.DOWN
->
3038 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
3039 state.mstate
<- Mpan
(x, y);
3041 | Unone
| Utext _
->
3042 if bstate
= Glut.DOWN
3044 if conf.angle
mod 360 = 0
3046 state.mstate
<- Msel
((x, y), (x, y));
3047 Glut.postRedisplay
()
3051 match state.mstate
with
3054 | Mzoom _
| Mscroll
->
3055 state.mstate
<- Mnone
3058 Glut.setCursor
Glut.CURSOR_INHERIT
;
3059 state.mstate
<- Mnone
3061 | Msel
((_
, y0
), (_
, y1
)) ->
3063 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh))
3064 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh)))
3066 match getopaque l.pageno with
3067 | Some
(opaque, _
) when validopaque opaque ->
3071 List.iter
f state.layout;
3072 copysel
""; (* ugly *)
3073 Glut.setCursor
Glut.CURSOR_INHERIT
;
3074 state.mstate
<- Mnone
;
3081 let birdseyemouse button bstate
x y
3082 (conf, leftx
, _
, hooverpageno
, anchor) =
3084 | Glut.LEFT_BUTTON
when bstate
= Glut.UP
->
3085 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
3086 let rec loop = function
3089 if y > l.pagedispy
&& y < l.pagedispy
+ l.pagevh
3090 && x > margin && x < margin + l.pagew
3092 leavebirdseye (conf, leftx
, l.pageno, hooverpageno
, anchor) false;
3097 | Glut.OTHER_BUTTON _
-> viewmouse button bstate
x y
3101 let mouse bstate button
x y =
3102 match state.mode with
3103 | View
-> viewmouse button bstate
x y
3104 | Birdseye beye
-> birdseyemouse button bstate
x y beye
3105 | Textentry _
| Outline _
| Items _
-> ()
3108 let mouse ~button ~
state ~
x ~
y = mouse state button
x y;;
3111 match state.mode with
3114 match state.mstate
with
3115 | Mzoom _
| Mnone
-> ()
3120 state.mstate
<- Mpan
(x, y);
3121 if conf.zoom > 1.0 then state.x <- state.x + dx;
3123 gotoy_and_clear_text y
3126 state.mstate
<- Msel
(a, (x, y));
3127 Glut.postRedisplay
()
3130 let y = min
conf.winh
(max
0 y) in
3131 let percent = float y /. float conf.winh
in
3132 let y = truncate
(float (state.maxy - conf.winh
) *. percent) in
3133 gotoy_and_clear_text y
3137 match state.mode with
3138 | Birdseye
(conf, leftx
, pageno, hooverpageno
, anchor) ->
3139 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
3140 let rec loop = function
3142 if hooverpageno
!= -1
3144 state.mode <- Birdseye
(conf, leftx
, pageno, -1, anchor);
3145 Glut.postRedisplay
();
3148 if y > l.pagedispy
&& y < l.pagedispy
+ l.pagevh
3149 && x > margin && x < margin + l.pagew
3151 state.mode <- Birdseye
(conf, leftx
, pageno, l.pageno, anchor);
3152 Glut.postRedisplay
();
3158 | Outline _
| Items _
| Textentry _
-> ()
3160 match state.mstate
with
3162 begin match getunder x y with
3163 | Unone
-> Glut.setCursor
Glut.CURSOR_INHERIT
3165 if conf.underinfo
then showtext 'u'
("ri: " ^ uri
);
3166 Glut.setCursor
Glut.CURSOR_INFO
3167 | Ulinkgoto
(page
, _
) ->
3169 then showtext 'p'
("age: " ^ string_of_int page
);
3170 Glut.setCursor
Glut.CURSOR_INFO
3172 if conf.underinfo
then showtext '
f'
("ont: " ^
s);
3173 Glut.setCursor
Glut.CURSOR_TEXT
3176 | Mpan _
| Msel _
| Mzoom _
| Mscroll
->
3187 match Sys.os_type
with
3188 | "Win32" -> Sys.getenv
"HOMEPATH"
3189 | _
-> Sys.getenv
"HOME"
3192 ("Can not determine home directory location: " ^
3193 Printexc.to_string exn
);
3197 let config_of c attrs
=
3201 | "scroll-bar-width" -> { c with scrollbw
= max
0 (int_of_string
v) }
3202 | "scroll-handle-height" -> { c with scrollh
= max
0 (int_of_string
v) }
3203 | "case-insensitive-search" -> { c with icase
= bool_of_string
v }
3204 | "preload" -> { c with preload = bool_of_string
v }
3205 | "page-bias" -> { c with pagebias
= int_of_string
v }
3206 | "scroll-step" -> { c with scrollstep
= max
1 (int_of_string
v) }
3207 | "auto-scroll-step" ->
3208 { c with autoscrollstep
= max
0 (int_of_string
v) }
3209 | "max-height-fit" -> { c with maxhfit
= bool_of_string
v }
3210 | "crop-hack" -> { c with crophack
= bool_of_string
v }
3211 | "throttle" -> { c with showall
= bool_of_string
v }
3212 | "highlight-links" -> { c with hlinks
= bool_of_string
v }
3213 | "under-cursor-info" -> { c with underinfo
= bool_of_string
v }
3214 | "vertical-margin" ->
3215 { c with interpagespace = max
0 (int_of_string
v) }
3217 let zoom = float_of_string
v /. 100. in
3218 let zoom = max
0.01 (min
2.2 zoom) in
3219 { c with zoom = zoom }
3220 | "presentation" -> { c with presentation = bool_of_string
v }
3221 | "rotation-angle" -> { c with angle
= int_of_string
v }
3222 | "width" -> { c with winw
= max
20 (int_of_string
v) }
3223 | "height" -> { c with winh
= max
20 (int_of_string
v) }
3224 | "persistent-bookmarks" -> { c with savebmarks
= bool_of_string
v }
3225 | "proportional-display" -> { c with proportional
= bool_of_string
v }
3226 | "pixmap-cache-size" -> { c with memlimit
= max
2 (int_of_string
v) }
3227 | "tex-count" -> { c with texcount
= max
1 (int_of_string
v) }
3228 | "slice-height" -> { c with sliceheight
= max
2 (int_of_string
v) }
3229 | "thumbnail-width" -> { c with thumbw
= max
2 (int_of_string
v) }
3230 | "persistent-location" -> { c with jumpback
= bool_of_string
v }
3231 | "background-color" -> { c with bgcolor
= color_of_string v }
3232 | "scrollbar-in-presentation" ->
3233 { c with scrollbarinpm
= bool_of_string
v }
3236 prerr_endline
("Error processing attribute (`" ^
3237 k ^
"'=`" ^
v ^
"'): " ^
Printexc.to_string exn
);
3240 let rec fold c = function
3243 let c = apply c k
v in
3249 let fromstring f pos n v d =
3252 dolog "Error processing attribute (%S=%S) at %d\n%s"
3253 n v pos (Printexc.to_string exn
)
3258 let bookmark_of attrs
=
3259 let rec fold title page rely
= function
3260 | ("title", v) :: rest
-> fold v page rely rest
3261 | ("page", v) :: rest
-> fold title v rely rest
3262 | ("rely", v) :: rest
-> fold title page
v rest
3263 | _
:: rest
-> fold title page rely rest
3264 | [] -> title, page
, rely
3266 fold "invalid" "0" "0" attrs
3270 let rec fold path page rely
pan = function
3271 | ("path", v) :: rest
-> fold v page rely
pan rest
3272 | ("page", v) :: rest
-> fold path
v rely
pan rest
3273 | ("rely", v) :: rest
-> fold path page
v pan rest
3274 | ("pan", v) :: rest
-> fold path page rely
v rest
3275 | _
:: rest
-> fold path page rely
pan rest
3276 | [] -> path
, page
, rely
, pan
3278 fold "" "0" "0" "0" attrs
3281 let setconf dst src
=
3282 dst
.scrollbw
<- src
.scrollbw
;
3283 dst
.scrollh
<- src
.scrollh
;
3284 dst
.icase
<- src
.icase
;
3285 dst
.preload <- src
.preload;
3286 dst
.pagebias
<- src
.pagebias
;
3287 dst
.verbose
<- src
.verbose
;
3288 dst
.scrollstep
<- src
.scrollstep
;
3289 dst
.maxhfit
<- src
.maxhfit
;
3290 dst
.crophack
<- src
.crophack
;
3291 dst
.autoscrollstep
<- src
.autoscrollstep
;
3292 dst
.showall
<- src
.showall
;
3293 dst
.hlinks
<- src
.hlinks
;
3294 dst
.underinfo
<- src
.underinfo
;
3295 dst
.interpagespace <- src
.interpagespace;
3296 dst
.zoom <- src
.zoom;
3297 dst
.presentation <- src
.presentation;
3298 dst
.angle
<- src
.angle
;
3299 dst
.winw
<- src
.winw
;
3300 dst
.winh
<- src
.winh
;
3301 dst
.savebmarks
<- src
.savebmarks
;
3302 dst
.memlimit
<- src
.memlimit
;
3303 dst
.proportional
<- src
.proportional
;
3304 dst
.texcount
<- src
.texcount
;
3305 dst
.sliceheight
<- src
.sliceheight
;
3306 dst
.thumbw
<- src
.thumbw
;
3307 dst
.jumpback
<- src
.jumpback
;
3308 dst
.bgcolor
<- src
.bgcolor
;
3309 dst
.scrollbarinpm
<- src
.scrollbarinpm
;
3313 let l = String.length
s in
3314 let b = Buffer.create
l in
3320 let h = Hashtbl.create
10 in
3321 let dc = { defconf with angle
= defconf.angle
} in
3322 let rec toplevel v t spos _
=
3324 | Vdata
| Vcdata
| Vend
-> v
3325 | Vopen
("llppconfig", _
, closed
) ->
3328 else { v with f = llppconfig
}
3330 error
"unexpected subelement at top level" s spos
3331 | Vclose _
-> error
"unexpected close at top level" s spos
3333 and llppconfig
v t spos _
=
3335 | Vdata
| Vcdata
| Vend
-> v
3336 | Vopen
("defaults", attrs
, closed
) ->
3337 let c = config_of dc attrs
in
3341 else { v with f = skip
"defaults" (fun () -> v) }
3343 | Vopen
("doc", attrs
, closed
) ->
3344 let pathent, spage
, srely
, span
= doc_of attrs
in
3345 let path = unent pathent
3346 and pageno = fromstring int_of_string spos
"page" spage
0
3347 and rely
= fromstring float_of_string spos
"rely" srely
0.0
3348 and pan = fromstring int_of_string spos
"pan" span
0 in
3349 let c = config_of dc attrs
in
3350 let anchor = (pageno, rely
) in
3352 then (Hashtbl.add
h path (c, [], pan, anchor); v)
3353 else { v with f = doc
path pan anchor c [] }
3356 error
"unexpected subelement in llppconfig" s spos
3358 | Vclose
"llppconfig" -> { v with f = toplevel }
3359 | Vclose _
-> error
"unexpected close in llppconfig" s spos
3361 and doc
path pan anchor c bookmarks v t spos _
=
3363 | Vdata
| Vcdata
-> v
3364 | Vend
-> error
"unexpected end of input in doc" s spos
3365 | Vopen
("bookmarks", _
, closed
) ->
3368 else { v with f = pbookmarks
path pan anchor c bookmarks }
3370 | Vopen
(_
, _
, _
) ->
3371 error
"unexpected subelement in doc" s spos
3374 Hashtbl.add
h path (c, List.rev
bookmarks, pan, anchor);
3375 { v with f = llppconfig
}
3377 | Vclose _
-> error
"unexpected close in doc" s spos
3379 and pbookmarks
path pan anchor c bookmarks v t spos _
=
3381 | Vdata
| Vcdata
-> v
3382 | Vend
-> error
"unexpected end of input in bookmarks" s spos
3383 | Vopen
("item", attrs
, closed
) ->
3384 let titleent, spage
, srely
= bookmark_of attrs
in
3385 let page = fromstring int_of_string spos
"page" spage
0
3386 and rely
= fromstring float_of_string spos
"rely" srely
0.0 in
3387 let bookmarks = (unent titleent, 0, (page, rely
)) :: bookmarks in
3389 then { v with f = pbookmarks
path pan anchor c bookmarks }
3392 { v with f = skip
"item" f }
3395 error
"unexpected subelement in bookmarks" s spos
3397 | Vclose
"bookmarks" ->
3398 { v with f = doc
path pan anchor c bookmarks }
3400 | Vclose _
-> error
"unexpected close in bookmarks" s spos
3402 and skip tag
f v t spos _
=
3404 | Vdata
| Vcdata
-> v
3406 error
("unexpected end of input in skipped " ^ tag
) s spos
3407 | Vopen
(tag'
, _
, closed
) ->
3411 let f'
() = { v with f = skip tag
f } in
3412 { v with f = skip tag'
f'
}
3416 else error
("unexpected close in skipped " ^ tag
) s spos
3419 parse
{ f = toplevel; accu = () } s;
3425 let len = in_channel_length ic
in
3426 let s = String.create
len in
3427 really_input ic
s 0 len;
3430 | Parse_error
(msg
, s, pos) ->
3431 let subs = subs s pos in
3432 let s = Printf.sprintf
"%s: at %d [..%s..]" msg
pos subs in
3433 failwith
("parse error: " ^
s)
3436 failwith
("config load error: " ^
Printexc.to_string exn
)
3442 let dir = Filename.concat
home ".config" in
3443 if Sys.is_directory
dir then dir else home
3446 Filename.concat
dir "llpp.conf"
3450 if Sys.file_exists
path
3453 (try Some
(open_in_bin
path)
3456 ("Error opening configuation file `" ^
path ^
"': " ^
3457 Printexc.to_string exn
);
3466 ("Error loading configuation from `" ^
path ^
"': " ^
3467 Printexc.to_string exn
);
3473 f (Hashtbl.create
0, defconf)
3478 let pc, pb
, px
, pa
=
3480 Hashtbl.find h (Filename.basename
state.path)
3481 with Not_found
-> dc, [], 0, (0, 0.0)
3485 state.bookmarks <- pb
;
3487 state.scrollw
<- conf.scrollbw
;
3489 then state.anchor <- pa
;
3490 cbput state.hists
.nav pa
;
3495 let add_attrs bb always
dc c =
3498 then Printf.bprintf bb
"\n %s='%b'" s a
3501 then Printf.bprintf bb
"\n %s='%d'" s a
3504 then Printf.bprintf bb
"\n %s='%f'" s (a*.100.)
3508 Printf.bprintf bb
"\n %s='%s'" s (color_to_string a)
3512 then dc.winw
, dc.winh
3514 match state.fullscreen
with
3516 | None
-> c.winw
, c.winh
3518 let zoom, presentation, interpagespace, showall
=
3520 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
3522 match state.mode with
3523 | Birdseye
(bc
, _
, _
, _
, _
) ->
3524 bc
.zoom, bc
.presentation, bc
.interpagespace, bc
.showall
3525 | _
-> c.zoom, c.presentation, c.interpagespace, c.showall
3527 oi
"width" w dc.winw
;
3528 oi
"height" h dc.winh
;
3529 oi
"scroll-bar-width" c.scrollbw
dc.scrollbw
;
3530 oi
"scroll-handle-height" c.scrollh
dc.scrollh
;
3531 ob "case-insensitive-search" c.icase
dc.icase
;
3532 ob "preload" c.preload dc.preload;
3533 oi
"page-bias" c.pagebias
dc.pagebias
;
3534 oi
"scroll-step" c.scrollstep
dc.scrollstep
;
3535 oi
"auto-scroll-step" c.autoscrollstep
dc.autoscrollstep
;
3536 ob "max-height-fit" c.maxhfit
dc.maxhfit
;
3537 ob "crop-hack" c.crophack
dc.crophack
;
3538 ob "throttle" showall
dc.showall
;
3539 ob "highlight-links" c.hlinks
dc.hlinks
;
3540 ob "under-cursor-info" c.underinfo
dc.underinfo
;
3541 oi
"vertical-margin" interpagespace dc.interpagespace;
3542 oz
"zoom" zoom dc.zoom;
3543 ob "presentation" presentation dc.presentation;
3544 oi
"rotation-angle" c.angle
dc.angle
;
3545 ob "persistent-bookmarks" c.savebmarks
dc.savebmarks
;
3546 ob "proportional-display" c.proportional
dc.proportional
;
3547 oi
"pixmap-cache-size" c.memlimit
dc.memlimit
;
3548 oi
"texcount" c.texcount
dc.texcount
;
3549 oi
"slice-height" c.sliceheight
dc.sliceheight
;
3550 oi
"thumbnail-width" c.thumbw
dc.thumbw
;
3551 ob "persistent-location" c.jumpback
dc.jumpback
;
3552 oc
"background-color" c.bgcolor
dc.bgcolor
;
3553 ob "scrollbar-in-presentation" c.scrollbarinpm
dc.scrollbarinpm
;
3557 let bb = Buffer.create
32768 in
3559 let dc = if conf.bedefault
then conf else dc in
3560 Buffer.add_string
bb "<llppconfig>\n<defaults ";
3561 add_attrs bb true dc dc;
3562 Buffer.add_string
bb "/>\n";
3564 let adddoc path pan anchor c bookmarks =
3565 if bookmarks == [] && c = dc && anchor = emptyanchor
3568 Printf.bprintf
bb "<doc path='%s'"
3569 (enent
path 0 (String.length
path));
3571 if anchor <> emptyanchor
3573 let n, y = anchor in
3574 Printf.bprintf
bb " page='%d'" n;
3577 Printf.bprintf
bb " rely='%f'" y
3582 then Printf.bprintf
bb " pan='%d'" pan;
3584 add_attrs bb false dc c;
3586 begin match bookmarks with
3587 | [] -> Buffer.add_string
bb "/>\n"
3589 Buffer.add_string
bb ">\n<bookmarks>\n";
3590 List.iter
(fun (title, _level
, (page, rely
)) ->
3592 "<item title='%s' page='%d'"
3593 (enent
title 0 (String.length
title))
3598 Printf.bprintf
bb " rely='%f'" rely
3600 Buffer.add_string
bb "/>\n";
3602 Buffer.add_string
bb "</bookmarks>\n</doc>\n";
3608 match state.mode with
3609 | Birdseye
(_
, pan, _
, _
, _
) -> pan
3612 let basename = Filename.basename state.path in
3613 adddoc basename pan (getanchor ())
3616 if state.ascrollstep
> 0
3617 then state.ascrollstep
3618 else conf.autoscrollstep
}
3619 (if conf.savebmarks
then state.bookmarks else []);
3621 Hashtbl.iter
(fun path (c, bookmarks, x, y) ->
3623 then adddoc path x y c bookmarks
3625 Buffer.add_string
bb "</llppconfig>";
3628 if Buffer.length
bb > 0
3631 let tmp = path ^
".tmp" in
3632 let oc = open_out_bin
tmp in
3633 Buffer.output_buffer
oc bb;
3635 Sys.rename
tmp path;
3638 ("error while saving configuration: " ^
Printexc.to_string exn
)
3645 [("-p", Arg.String
(fun s -> state.password
<- s) , " Set password")
3646 ;("-v", Arg.Unit
(fun () -> print_endline
Help.version
; exit
0),
3647 " Print version and exit")]
3649 (fun s -> state.path <- s)
3650 ("Usage: " ^
Sys.argv
.(0) ^
" [options] some.pdf\nOptions:")
3652 if String.length
state.path = 0
3653 then (prerr_endline
"file name missing"; exit
1);
3657 let _ = Glut.init
Sys.argv
in
3658 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
3659 let () = Glut.initWindowSize
conf.winw
conf.winh
in
3660 let _ = Glut.createWindow
("llpp " ^
Filename.basename state.path) in
3663 if Sys.os_type
= "Unix"
3665 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
3667 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
3668 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
3669 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
3670 Unix.bind
sock addr;
3672 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
3673 Unix.connect
csock addr;
3674 let ssock, _ = Unix.accept
sock in
3677 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
3678 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
3682 at_exit
(fun () -> Unix.shutdown
ssock Unix.SHUTDOWN_ALL
);
3686 let () = Glut.displayFunc
display in
3687 let () = Glut.reshapeFunc
reshape in
3688 let () = Glut.keyboardFunc
keyboard in
3689 let () = Glut.specialFunc
special in
3690 let () = Glut.idleFunc
(Some
idle) in
3691 let () = Glut.mouseFunc
mouse in
3692 let () = Glut.motionFunc
motion in
3693 let () = Glut.passiveMotionFunc
pmotion in
3695 init
ssock (conf.angle
, conf.proportional
, conf.texcount
, conf.sliceheight
);
3696 state.csock <- csock;
3697 state.ssock <- ssock;
3698 state.text <- "Opening " ^
state.path;
3699 writeopen state.path state.password
;
3703 let rec handlelablglutbug () =
3706 with Glut.BadEnum
"key in special_of_int" ->
3707 showtext '
!'
" LablGlut bug: special key not recognized";
3708 handlelablglutbug ()
3710 handlelablglutbug ();