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
1585 conf.scrollbarinpm
<- v;
1586 if conf.presentation
1588 state.scrollw
<- if v then conf.scrollbw
else 0;
1589 reshape conf.winw
conf.winh
;
1595 "Pixmap Cache", 0, Noaction
;
1597 intp "size (advisory)"
1598 (fun () -> conf.memlimit
)
1599 (fun v -> conf.memlimit
<- v);
1600 Printf.sprintf
"%-24s %d" "used" state.memused
, 1, Noaction
;
1607 :: ("Document", 0, Noaction
)
1608 :: List.map
(fun (_
, s) -> (s, 1, Noaction
)) state.docinfo
1614 :: (Printf.sprintf
"Save these parameters as defaults at exit (%s)"
1615 (btos conf.bedefault
),
1618 fun active first qsearch pan
->
1619 conf.bedefault
<- not
conf.bedefault
;
1620 Items
(active, first
, makeitems (), qsearch
, pan
, mode);
1625 Array.of_list
(items @ tailer)
1628 state.mode <- Items
(1, 0, makeitems (), "", 0, mode);
1629 Glut.postRedisplay
();
1632 let enterhelpmode () =
1633 state.mode <- Items
(-1, 0, state.help
, "", 0, state.mode);
1634 Glut.postRedisplay
();
1637 let quickbookmark ?title
() =
1638 match state.layout with
1644 let sec = Unix.gettimeofday
() in
1645 let tm = Unix.localtime
sec in
1646 Printf.sprintf
"Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1650 (tm.Unix.tm_year
+ 1900)
1653 | Some
title -> title
1656 (title, 0, (l.pageno, float l.pagey /. float l.pageh
))
1661 state.fullscreen
<- None
;
1662 Glut.reshapeWindow
w h;
1665 let writeopen path password
=
1666 writecmd state.csock
("open " ^ path ^
"\000" ^ password ^
"\000");
1667 writecmd state.csock
"info";
1670 let opendoc path password
=
1673 state.password
<- password
;
1674 state.gen
<- state.gen
+ 1;
1676 writeopen path password
;
1677 Glut.setWindowTitle
("llpp " ^
Filename.basename path
);
1678 wcmd "geometry" [`i
state.w; `i
conf.winh
];
1681 let viewkeyboard key
=
1683 let mode = state.mode in
1684 state.mode <- Textentry
(te, fun _
-> state.mode <- mode);
1687 Glut.postRedisplay
()
1689 let c = Char.chr key
in
1691 | '
\027'
| 'q'
-> (* escape *)
1694 | '
\008'
-> (* backspace *)
1695 let y = getnav ~
-1 in
1696 gotoy_and_clear_text y
1704 Glut.postRedisplay
()
1707 let ondone isforw
s =
1708 cbput state.hists
.pat
s;
1709 state.searchpattern
<- s;
1712 let s = String.create
1 in
1714 enttext (s, "", Some
(onhist state.hists
.pat
),
1715 textentry, ondone (c ='
/'
))
1717 | '
+'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1718 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1719 setzoom (min
2.2 (conf.zoom +. incr))
1724 try int_of_string
s with exc
->
1725 state.text <- Printf.sprintf
"bad integer `%s': %s"
1726 s (Printexc.to_string exc
);
1732 state.text <- "page bias is now " ^ string_of_int
n;
1735 enttext ("page bias", "", None
, intentry, ondone)
1737 | '
-'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1738 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1739 setzoom (max
0.01 (conf.zoom -. decr))
1742 let ondone msg
= state.text <- msg
in
1744 "option [acfhilpstvAPRSZ]", "", None
,
1745 optentry state.mode, ondone
1748 | '
0'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1751 | '
1'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1752 let zoom = zoomforh
conf.winw
conf.winh
state.scrollw
in
1756 | '
9'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1762 try int_of_string
s with exc
->
1763 state.text <- Printf.sprintf
"bad integer `%s': %s"
1764 s (Printexc.to_string exc
);
1770 cbput state.hists
.pag
(string_of_int
n);
1771 gotoy_and_clear_text (getpagey (n + conf.pagebias
- 1))
1774 let pageentry text key
=
1775 match Char.unsafe_chr key
with
1776 | 'g'
-> TEdone
text
1777 | _
-> intentry text key
1779 let text = "x" in text.[0] <- c;
1780 enttext (":", text, Some
(onhist state.hists
.pag
), pageentry, ondone)
1783 state.scrollw
<- if state.scrollw
> 0 then 0 else conf.scrollbw
;
1784 reshape conf.winw
conf.winh
;
1787 conf.hlinks
<- not
conf.hlinks
;
1788 state.text <- "highlightlinks " ^
if conf.hlinks
then "on" else "off";
1789 Glut.postRedisplay
()
1792 if state.ascrollstep
= 0
1793 then state.ascrollstep
<- conf.autoscrollstep
1795 conf.autoscrollstep
<- state.ascrollstep
;
1796 state.ascrollstep
<- 0;
1800 conf.presentation <- not
conf.presentation;
1801 if conf.presentation
1803 if not
conf.scrollbarinpm
1804 then state.scrollw
<- 0;
1807 state.scrollw
<- conf.scrollbw
;
1809 showtext ' '
("presentation mode " ^
1810 if conf.presentation then "on" else "off");
1811 state.anchor <- getanchor ();
1815 begin match state.fullscreen
with
1817 state.fullscreen
<- Some
(conf.winw
, conf.winh
);
1820 state.fullscreen
<- None
;
1825 gotoy_and_clear_text 0
1828 search state.searchpattern
true
1831 search state.searchpattern
false
1834 begin match state.layout with
1837 gotoy_and_clear_text (getpagey l.pageno)
1841 begin match List.rev
state.layout with
1844 let pageno = min
(l.pageno+1) (state.pagecount
-1) in
1845 gotoy_and_clear_text (getpagey pageno)
1848 | '
\127'
-> (* delte *)
1849 begin match state.layout with
1852 let pageno = max
0 (l.pageno-1) in
1853 gotoy_and_clear_text (getpagey pageno)
1858 if fn
= -1 then l.pageno, l.pageno else fn
, l.pageno
1860 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
1862 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
1866 else (100. *. (float state.y /. float maxy)) in
1869 Printf.sprintf
"Page %d of %d %.2f%%"
1870 (fn+1) state.pagecount
percent
1873 "Pages %d-%d of %d %.2f%%"
1874 (fn+1) (ln
+1) state.pagecount
percent
1879 begin match state.layout with
1882 doreshape (l.pagew
+ state.scrollw
) l.pageh
;
1883 Glut.postRedisplay
();
1887 enterbookmarkmode ()
1897 match state.layout with
1900 (s, 0, (l.pageno, float l.pagey /. float l.pageh
))
1904 enttext ("bookmark", "", None
, textentry, ondone)
1908 showtext ' '
"Quick bookmark added";
1911 begin match state.layout with
1913 let rect = getpdimrect
l.pagedimno
in
1917 (truncate
(1.8 *. (rect.(1) -. rect.(0))),
1918 truncate
(1.2 *. (rect.(3) -. rect.(0))))
1920 (truncate
(rect.(1) -. rect.(0)),
1921 truncate
(rect.(3) -. rect.(0)))
1925 doreshape (w + state.scrollw
) (h + conf.interpagespace)
1927 Glut.postRedisplay
();
1933 reinit (conf.angle
+ (if c = '
>'
then 30 else -30)) conf.proportional
1938 (min
(state.colorscale
+. (if c = '
]'
then 0.1 else -0.1)) 1.0);
1939 Glut.postRedisplay
()
1942 begin match state.mode with
1943 | Birdseye beye
-> upbirdseye beye
1944 | _
-> gotoy (clamp (-conf.scrollstep
))
1948 begin match state.mode with
1949 | Birdseye beye
-> downbirdseye beye
1950 | _
-> gotoy (clamp conf.scrollstep
)
1954 state.anchor <- getanchor ();
1955 opendoc state.path
state.password
1958 vlog "huh? %d %c" key
(Char.chr key
);
1961 let textentrykeyboard key
((c, text, opthist
, onkey
, ondone), onleave
) =
1963 state.mode <- Textentry
(te, onleave
);
1966 Glut.postRedisplay
()
1968 match Char.unsafe_chr key
with
1969 | '
\008'
-> (* backspace *)
1970 let len = String.length
text in
1974 Glut.postRedisplay
();
1977 let s = String.sub
text 0 (len - 1) in
1978 enttext (c, s, opthist
, onkey
, ondone)
1984 Glut.postRedisplay
()
1986 | '
\027'
-> (* escape *)
1987 begin match opthist
with
1989 | Some
(_
, onhistcancel
) -> onhistcancel
()
1992 Glut.postRedisplay
()
1995 begin match onkey
text key
with
1999 Glut.postRedisplay
()
2002 enttext (c, text, opthist
, onkey
, ondone);
2006 Glut.postRedisplay
()
2009 state.mode <- Textentry
(te, onleave
);
2010 Glut.postRedisplay
()
2014 let birdseyekeyboard key
((_
, _
, pageno, _
, _
) as beye
) =
2016 | 27 -> (* escape *)
2017 leavebirdseye beye
true
2019 | 12 -> (* ctrl-l *)
2020 let y, h = getpageyh pageno in
2021 let top = (conf.winh
- h) / 2 in
2022 gotoy (max
0 (y - top))
2025 leavebirdseye beye
false
2031 let itemskeyboard key
(active, first
, items, qsearch
, pan
, oldmode
) =
2032 let set active first qsearch
=
2033 state.mode <- Items
(active, first
, items, qsearch
, pan
, oldmode
)
2035 let search active pattern
incr =
2038 if n = Array.length
items || n = -1
2041 let (s, _
, _
) = items.(n) in
2043 (try ignore
(Str.search_forward re
s 0); true
2044 with Not_found
-> false)
2046 else loop (n + incr)
2051 let re = Str.regexp_case_fold pattern
in
2057 let firstof active = max
0 (active - maxoutlinerows () / 2) in
2059 | 18 | 19 -> (* ctrl-r/ctlr-s *)
2060 let incr = if key
= 18 then -1 else 1 in
2062 match search (active + incr) qsearch
incr with
2064 state.text <- qsearch ^
" [not found]";
2067 state.text <- qsearch
;
2068 active, firstof active
2070 set active first qsearch
;
2071 Glut.postRedisplay
();
2073 | 8 -> (* backspace *)
2074 let len = String.length qsearch
in
2081 set active first
"";
2084 let qsearch = String.sub
qsearch 0 (len - 1) in
2086 match search active qsearch ~
-1 with
2088 state.text <- qsearch ^
" [not found]";
2091 state.text <- qsearch;
2092 active, firstof active
2094 set active first
qsearch
2096 Glut.postRedisplay
()
2098 | _
when key
>= 32 && key
< 127 ->
2099 let pattern = addchar qsearch (Char.chr key
) in
2101 match search active pattern 1 with
2103 state.text <- pattern ^
" [not found]";
2106 state.text <- pattern;
2107 active, firstof active
2109 set active first
pattern;
2110 Glut.postRedisplay
()
2112 | 27 -> (* escape *)
2114 state.mode <- oldmode
;
2115 Glut.postRedisplay
();
2118 if active < Array.length
items
2120 match items.(active) with
2122 state.mode <- f active first
qsearch pan
2126 state.mode <- oldmode
2128 Glut.postRedisplay
();
2130 | _
-> dolog "unknown key %d" key
2133 let outlinekeyboard key
2134 (allowdel
, active, first
, outlines, qsearch, pan
, oldmode
) =
2135 let narrow outlines pattern =
2136 let reopt = try Some
(Str.regexp_case_fold
pattern) with _
-> None
in
2140 let rec fold accu n =
2144 let (s, _
, _
) as o
= outlines.(n) in
2146 if (try ignore
(Str.search_forward
re s 0); true
2147 with Not_found
-> false)
2153 let matched = fold [] (Array.length
outlines - 1) in
2154 if matched = [] then None
else Some
(Array.of_list
matched)
2156 let search active pattern incr =
2159 if n = Array.length
outlines || n = -1
2162 let (s, _
, _
) = outlines.(n) in
2164 (try ignore
(Str.search_forward
re s 0); true
2165 with Not_found
-> false)
2167 else loop (n + incr)
2172 let re = Str.regexp_case_fold
pattern in
2178 let firstof active = max
0 (active - maxoutlinerows () / 2) in
2180 | 27 -> (* escape *)
2181 if String.length
qsearch = 0
2184 state.mode <- oldmode
;
2185 Glut.postRedisplay
();
2189 state.mode <- Outline
(
2190 allowdel
, active, first
, outlines, "", pan
, oldmode
2192 Glut.postRedisplay
();
2195 | 18 | 19 -> (* ctrl-r/ctrl-s *)
2196 let incr = if key
= 18 then -1 else 1 in
2198 match search (active + incr) qsearch incr with
2200 state.text <- qsearch ^
" [not found]";
2203 state.text <- qsearch;
2204 active, firstof active
2206 state.mode <- Outline
(
2207 allowdel
, active, first
, outlines, qsearch, pan
, oldmode
2209 Glut.postRedisplay
();
2211 | 8 -> (* backspace *)
2212 let len = String.length
qsearch in
2219 state.mode <- Outline
(
2220 allowdel
, active, first
, outlines, "", pan
, oldmode
2224 let qsearch = String.sub
qsearch 0 (len - 1) in
2226 match search active qsearch ~
-1 with
2228 state.text <- qsearch ^
" [not found]";
2231 state.text <- qsearch;
2232 active, firstof active
2234 state.mode <- Outline
(
2235 allowdel
, active, first
, outlines, qsearch, pan
, oldmode
2238 Glut.postRedisplay
()
2241 if active < Array.length
outlines
2243 let (_
, _
, anchor) = outlines.(active) in
2248 if allowdel
then state.bookmarks <- Array.to_list
outlines;
2249 state.mode <- oldmode
;
2250 Glut.postRedisplay
();
2252 | _
when key
>= 32 && key
< 127 ->
2253 let pattern = addchar qsearch (Char.chr key
) in
2255 match search active pattern 1 with
2257 state.text <- pattern ^
" [not found]";
2260 state.text <- pattern;
2261 active, firstof active
2263 state.mode <- Outline
(
2264 allowdel
, active, first
, outlines, pattern, pan
, oldmode
2266 Glut.postRedisplay
()
2268 | 14 when not allowdel
-> (* ctrl-n *)
2269 if String.length
qsearch > 0
2271 let optoutlines = narrow outlines qsearch in
2272 begin match optoutlines with
2273 | None
-> state.text <- "can't narrow"
2275 state.mode <- Outline
(
2276 allowdel
, 0, 0, outlines, qsearch, pan
, oldmode
2278 match state.outlines with
2281 state.outlines <- Onarrow
(qsearch, outlines, a)
2282 | Onarrow
(_
, _
, b) ->
2283 state.outlines <- Onarrow
(qsearch, outlines, b)
2286 Glut.postRedisplay
()
2288 | 21 when not allowdel
-> (* ctrl-u *)
2290 match state.outlines with
2293 let a = Array.of_list
(List.rev
l) in
2294 state.outlines <- Oarray
a;
2296 | Onarrow
(_
, _
, b) ->
2297 state.outlines <- Oarray
b;
2301 state.mode <- Outline
(allowdel
, 0, 0, outline, qsearch, pan
, oldmode
);
2302 Glut.postRedisplay
()
2304 | 12 -> (* ctrl-l *)
2305 state.mode <- Outline
2306 (allowdel
, active, firstof active, outlines, qsearch, pan
, oldmode
);
2307 Glut.postRedisplay
()
2309 | 127 when allowdel
-> (* delete *)
2310 let len = Array.length
outlines - 1 in
2314 state.bookmarks <- [];
2317 let bookmarks = Array.init
len
2319 let i = if i >= active then i + 1 else i in
2333 Glut.postRedisplay
()
2335 | _
-> dolog "unknown key %d" key
2338 let keyboard ~key ~x ~
y =
2341 if key
= 7 (* ctrl-g *)
2345 match state.mode with
2346 | Outline
outline -> outlinekeyboard key
outline
2347 | Textentry
textentry -> textentrykeyboard key
textentry
2348 | Birdseye
birdseye -> birdseyekeyboard key
birdseye
2349 | View
-> viewkeyboard key
2350 | Items
items -> itemskeyboard key
items
2353 let birdseyespecial key
((conf, leftx
, _
, hooverpageno
, anchor) as beye
) =
2355 | Glut.KEY_UP
-> upbirdseye beye
2356 | Glut.KEY_DOWN
-> downbirdseye beye
2358 | Glut.KEY_PAGE_UP
->
2359 begin match state.layout with
2363 state.mode <- Birdseye
(
2364 conf, leftx
, l.pageno, hooverpageno
, anchor
2366 gotopage1 l.pageno 0;
2369 let layout = layout (state.y-conf.winh
) conf.winh
in
2371 | [] -> gotoy (clamp (-conf.winh
))
2373 state.mode <- Birdseye
(
2374 conf, leftx
, l.pageno, hooverpageno
, anchor
2376 gotopage1 l.pageno 0
2379 | [] -> gotoy (clamp (-conf.winh
))
2382 | Glut.KEY_PAGE_DOWN
->
2383 begin match List.rev
state.layout with
2385 let layout = layout (state.y + conf.winh
) conf.winh
in
2386 begin match layout with
2388 let incr = l.pageh
- l.pagevh in
2393 conf, leftx
, state.pagecount
- 1, hooverpageno
, anchor
2395 Glut.postRedisplay
();
2397 else gotoy (clamp (incr + conf.interpagespace*2));
2401 Birdseye
(conf, leftx
, l.pageno, hooverpageno
, anchor);
2402 gotopage1 l.pageno 0;
2405 | [] -> gotoy (clamp conf.winh
)
2409 state.mode <- Birdseye
(conf, leftx
, 0, hooverpageno
, anchor);
2413 let pageno = state.pagecount
- 1 in
2414 state.mode <- Birdseye
(conf, leftx
, pageno, hooverpageno
, anchor);
2415 if not
(pagevisible state.layout pageno)
2418 match List.rev
state.pdims
with
2420 | (_
, _
, h, _
) :: _
-> h
2422 gotoy (max
0 (getpagey pageno - (conf.winh
- h - conf.interpagespace)))
2423 else Glut.postRedisplay
();
2427 let setautoscrollspeed goingdown
=
2428 let incr = max
1 (state.ascrollstep
/ 2) in
2429 let astep = max
1 (state.ascrollstep
+ (if goingdown
then incr else -incr)) in
2430 state.ascrollstep
<- astep;
2433 let special ~key ~x ~
y =
2436 match state.mode with
2437 | View
| (Birdseye _
) when key
= Glut.KEY_F9
->
2441 birdseyespecial key vals
2443 | View
when key
= Glut.KEY_F1
->
2447 if state.ascrollstep
> 0 && (key
= Glut.KEY_DOWN
|| key
= Glut.KEY_UP
)
2448 then setautoscrollspeed (key
= Glut.KEY_DOWN
)
2452 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
2453 | Glut.KEY_UP
-> clamp (-conf.scrollstep
)
2454 | Glut.KEY_DOWN
-> clamp conf.scrollstep
2455 | Glut.KEY_PAGE_UP
->
2456 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2458 match state.layout with
2460 | l :: _
-> state.y - l.pagey
2463 | Glut.KEY_PAGE_DOWN
->
2464 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2466 match List.rev
state.layout with
2468 | l :: _
-> getpagey l.pageno
2471 | Glut.KEY_HOME
-> addnav (); 0
2474 state.maxy - (if conf.maxhfit
then conf.winh
else 0)
2476 | (Glut.KEY_RIGHT
| Glut.KEY_LEFT
) when
2477 Glut.getModifiers
() land Glut.active_alt
!= 0 ->
2478 getnav (if key
= Glut.KEY_LEFT
then 1 else -1)
2480 | Glut.KEY_RIGHT
when conf.zoom > 1.0 ->
2481 state.x
<- state.x
- 10;
2483 | Glut.KEY_LEFT
when conf.zoom > 1.0 ->
2484 state.x
<- state.x
+ 10;
2489 gotoy_and_clear_text y
2492 ((c, _
, (Some
(action, _
) as onhist), onkey
, ondone), mode) ->
2495 | Glut.KEY_UP
-> action HCprev
2496 | Glut.KEY_DOWN
-> action HCnext
2497 | Glut.KEY_HOME
-> action HCfirst
2498 | Glut.KEY_END
-> action HClast
2501 state.mode <- Textentry
((c, s, onhist, onkey
, ondone), mode);
2502 Glut.postRedisplay
()
2506 | Items
(active, first
, items, qsearch, pan
, oldmode
) ->
2507 let maxrows = maxoutlinerows () in
2508 let itemcount = Array.length
items in
2509 let hasaction = function
2510 | (_
, _
, Noaction
) -> false
2513 let find start
incr =
2515 if i = -1 || i = itemcount
2518 if hasaction items.(i)
2520 else find (i + incr)
2525 let set active first
=
2526 let first = max
0 (min
first (itemcount - maxrows)) in
2527 state.mode <- Items
(active, first, items, qsearch, pan
, oldmode
)
2530 let isvisible first n = n >= first && n - first <= maxrows in
2532 let incr1 = if incr > 0 then 1 else -1 in
2533 if isvisible first active
2536 let next = active + incr in
2538 if next < 0 || next >= itemcount
2540 else find next incr1
2542 if next = -1 || abs
(active - next) > maxrows
2548 let first = first + incr in
2549 let first = max
0 (min
first (itemcount - 1)) in
2551 let next = active + incr in
2552 let next = max
0 (min
next (itemcount - 1)) in
2555 let active = if next = -1 then active else next in
2558 let first = min
next first in
2561 let first = first + incr in
2562 let first = max
0 (min
first (itemcount - 1)) in
2564 let next = active + incr in
2565 let next = max
0 (min
next (itemcount - 1)) in
2566 let next = find next incr1 in
2567 if next = -1 || abs
(active - first) > maxrows
2574 Glut.postRedisplay
()
2576 begin match key
with
2577 | Glut.KEY_UP
-> navigate ~
-1
2578 | Glut.KEY_DOWN
-> navigate 1
2579 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
2580 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
2583 state.mode <- Items
(
2584 active, first, items, qsearch, min
0 (pan
- 1), oldmode
2586 Glut.postRedisplay
()
2589 state.mode <- Items
(
2590 active, first, items, qsearch, min
0 (pan
+ 1), oldmode
2592 Glut.postRedisplay
()
2595 let active = find 0 1 in
2597 Glut.postRedisplay
()
2600 let first = max
0 (itemcount - maxrows) in
2601 let active = find (itemcount - 1) ~
-1 in
2603 Glut.postRedisplay
()
2608 | Outline
(allowdel
, active, first, outlines, qsearch, pan
, oldmode
) ->
2609 let maxrows = maxoutlinerows () in
2610 let calcfirst first active =
2613 let rows = active - first in
2614 if rows > maxrows then active - maxrows else first
2618 let active = active + incr in
2619 let active = max
0 (min
active (Array.length
outlines - 1)) in
2620 let first = calcfirst first active in
2621 state.mode <- Outline
(
2622 allowdel
, active, first, outlines, qsearch, pan
, oldmode
2624 Glut.postRedisplay
()
2626 let updownlevel incr =
2627 let len = Array.length
outlines in
2628 let (_
, curlevel
, _
) = outlines.(active) in
2630 if i = len then i-1 else if i = -1 then 0 else
2631 let (_
, l, _
) = outlines.(i) in
2632 if l != curlevel
then i else flow (i+incr)
2634 let active = flow active in
2635 let first = calcfirst first active in
2636 state.mode <- Outline
(
2637 allowdel
, active, first, outlines, qsearch, pan
, oldmode
2639 Glut.postRedisplay
()
2642 | Glut.KEY_UP
-> navigate ~
-1
2643 | Glut.KEY_DOWN
-> navigate 1
2644 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
2645 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
2648 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2650 state.mode <- Outline
(
2651 allowdel
, active, first, outlines,
2652 qsearch, min
0 (pan
+ 1), oldmode
2654 Glut.postRedisplay
();
2662 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2664 state.mode <- Outline
(
2665 allowdel
, active, first, outlines, qsearch, pan
- 1, oldmode
2667 Glut.postRedisplay
();
2671 then updownlevel ~
-1
2675 state.mode <- Outline
(
2676 allowdel
, 0, 0, outlines, qsearch, pan
, oldmode
2678 Glut.postRedisplay
()
2681 let active = Array.length
outlines - 1 in
2682 let first = max
0 (active - maxrows) in
2683 state.mode <- Outline
(
2684 allowdel
, active, first, outlines, qsearch, pan
, oldmode
2686 Glut.postRedisplay
()
2691 let drawplaceholder l =
2692 let margin = state.x
+ (conf.winw
- (state.w + state.scrollw
)) / 2 in
2694 (float l.pagex
, float l.pagedispy
)
2695 (float (l.pagew
+ l.pagex
), float (l.pagedispy
+ l.pagevh))
2697 let x = float (if margin < 0 then -margin else l.pagex
)
2698 and y = float (l.pagedispy
+ 13) in
2699 let font = Glut.BITMAP_8_BY_13
in
2700 GlDraw.color
(0.0, 0.0, 0.0);
2701 GlPix.raster_pos ~
x ~
y ();
2702 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c))
2703 ("Loading " ^ string_of_int
(l.pageno + 1));
2706 let now () = Unix.gettimeofday
();;
2710 match state.mode with
2711 | Textentry _
-> scalecolor 0.4
2712 | View
| Outline _
| Items _
-> scalecolor 1.0
2713 | Birdseye
(_
, _
, pageno, hooverpageno
, _
) ->
2714 if l.pageno = hooverpageno
2717 if l.pageno = pageno
2723 begin match getopaque l.pageno with
2724 | Some
(opaque, _
) when validopaque opaque ->
2726 draw
(l.pagedispy
, l.pagew
, l.pagevh, l.pagey, conf.hlinks
)
2730 vlog "draw %d %f sec" l.pageno d;
2738 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
2739 let sh = (float (maxy + conf.winh
) /. float conf.winh
) in
2740 let sh = float conf.winh
/. sh in
2741 let sh = max
sh (float conf.scrollh
) in
2744 if state.y = state.maxy
2746 else float y /. float maxy
2748 let position = (float conf.winh
-. sh) *. percent in
2751 if position +. sh > float conf.winh
2752 then float conf.winh
-. sh
2758 let scrollindicator () =
2759 GlDraw.color (0.64 , 0.64, 0.64);
2761 (float (conf.winw
- state.scrollw
), 0.)
2762 (float conf.winw
, float conf.winh
)
2764 GlDraw.color (0.0, 0.0, 0.0);
2766 let position, sh = scrollph state.y in
2768 (float (conf.winw
- state.scrollw
), position)
2769 (float conf.winw
, position +. sh)
2773 let showsel margin =
2774 match state.mstate
with
2775 | Mnone
| Mscroll _
| Mpan _
| Mzoom _
->
2778 | Msel
((x0
, y0
), (x1
, y1
)) ->
2779 let rec loop = function
2781 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh))
2782 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh)))
2784 match getopaque l.pageno with
2785 | Some
(opaque, _
) when validopaque opaque ->
2786 let oy = -l.pagey + l.pagedispy
in
2788 (x0
- margin - state.x, y0
,
2789 x1
- margin - state.x, y1
) oy;
2799 let panx = float state.x in
2801 GlDraw.color (0.0, 0.0, 1.0) ~alpha
:0.5;
2802 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2804 (fun (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) ->
2806 if l.pageno = pageno
2808 let d = float (l.pagedispy
- l.pagey) in
2809 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
2810 GlDraw.begins `quads
;
2812 GlDraw.vertex2
(x0
+.panx, y0
+.d);
2813 GlDraw.vertex2
(x1
+.panx, y1
+.d);
2814 GlDraw.vertex2
(x2
+.panx, y2
+.d);
2815 GlDraw.vertex2
(x3
+.panx, y3
+.d);
2825 let showstrings active first pan
strings =
2827 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2828 GlDraw.color (0., 0., 0.) ~alpha
:0.85;
2829 GlDraw.rect (0., 0.) (float conf.winw
, float conf.winh
);
2832 GlDraw.color (1., 1., 1.);
2833 let font = Glut.BITMAP_9_BY_15
in
2834 let draw_string x y s =
2835 GlPix.raster_pos ~
x ~
y ();
2836 String.iter
(fun c -> Glut.bitmapCharacter ~
font ~
c:(Char.code
c)) s
2839 if row
= Array.length
strings || (row
- first) * 16 > conf.winh
2842 let (s, level
, _
) = strings.(row
) in
2843 let y = (row
- first) * 16 in
2844 let x = 5 + 15*(max
0 (level
+pan
)) in
2848 GlDraw.polygon_mode `both `
line;
2849 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2850 GlDraw.color (1., 1., 1.) ~alpha
:0.9;
2851 GlDraw.rect (0., float (y + 1))
2852 (float (conf.winw
- 1), float (y + 18));
2853 GlDraw.polygon_mode `both `fill
;
2855 GlDraw.color (1., 1., 1.);
2858 let l = String.length
s in
2861 let pan = pan * 2 in
2862 let pos = pan + level
in
2863 let left = l + pos in
2869 else String.sub
s (-pos) left
2871 draw_string (float x) (float (y + 16)) s
2874 draw_string (float (x + pan*15)) (float (y + 16)) s
2883 let showoutline (_
, active, first, outlines, _
, pan, _
) =
2884 showstrings active first pan outlines;
2887 let showitems (active, first, items, _
, pan, _
) =
2888 showstrings active first pan items;
2892 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
2893 GlDraw.viewport
margin 0 state.w conf.winh
;
2895 GlClear.color (scalecolor2 conf.bgcolor
);
2896 GlClear.clear
[`
color];
2899 Gl.enable `scissor_test
;
2900 GlMisc.scissor
0 0 (conf.winw
- state.scrollw
) conf.winh
;
2902 List.iter
drawpage state.layout;
2905 Gl.disable `scissor_test
2909 let x = -.float state.x in
2910 GlMat.translate ~
x ();
2914 GlDraw.viewport
0 0 conf.winw
conf.winh
;
2917 begin match state.mode with
2918 | Items
items -> showitems items
2919 | Outline
outline -> showoutline outline
2923 Glut.swapBuffers
();
2927 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
2928 let x = x - margin - state.x in
2929 let rec f = function
2931 begin match getopaque l.pageno with
2932 | Some
(opaque, _
) when validopaque opaque ->
2933 let y = y - l.pagedispy
in
2936 let y = l.pagey + y in
2937 let x = x - l.pagex
in
2938 match whatsunder
opaque x y with
2951 let viewmouse button bstate
x y =
2953 | Glut.OTHER_BUTTON
n when (n == 3 || n == 4) && bstate
= Glut.UP
->
2954 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2956 match state.mstate
with
2957 | Mzoom
(oldn
, i) ->
2965 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
2967 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
2969 let zoom = conf.zoom +. incr in
2971 state.mstate
<- Mzoom
(n, 0);
2973 state.mstate
<- Mzoom
(n, i+1);
2975 else state.mstate
<- Mzoom
(n, 0)
2977 | _
-> state.mstate
<- Mzoom
(n, 0)
2980 if state.ascrollstep
> 0
2982 setautoscrollspeed (n=4)
2986 then -conf.scrollstep
2987 else conf.scrollstep
2989 let incr = incr * 2 in
2990 let y = clamp incr in
2991 gotoy_and_clear_text y
2994 | Glut.LEFT_BUTTON
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
2995 if bstate
= Glut.DOWN
2997 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
2998 state.mstate
<- Mpan
(x, y)
3001 state.mstate
<- Mnone
3003 | Glut.LEFT_BUTTON
when x > conf.winw
- state.scrollw
->
3004 if bstate
= Glut.DOWN
3006 let position, sh = scrollph state.y in
3007 if y > truncate
position && y < truncate
(position +. sh)
3009 state.mstate
<- Mscroll
3011 let percent = float y /. float conf.winh
in
3012 let desty = truncate
(float (state.maxy - conf.winh
) *. percent) in
3014 state.mstate
<- Mscroll
3016 state.mstate
<- Mnone
3018 | Glut.LEFT_BUTTON
->
3019 let dest = if bstate
= Glut.DOWN
then getunder x y else Unone
in
3020 begin match dest with
3021 | Ulinkgoto
(pageno, top) ->
3025 gotopage1 pageno top;
3031 | Unone
when bstate
= Glut.DOWN
->
3032 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
3033 state.mstate
<- Mpan
(x, y);
3035 | Unone
| Utext _
->
3036 if bstate
= Glut.DOWN
3038 if conf.angle
mod 360 = 0
3040 state.mstate
<- Msel
((x, y), (x, y));
3041 Glut.postRedisplay
()
3045 match state.mstate
with
3048 | Mzoom _
| Mscroll
->
3049 state.mstate
<- Mnone
3052 Glut.setCursor
Glut.CURSOR_INHERIT
;
3053 state.mstate
<- Mnone
3055 | Msel
((_
, y0
), (_
, y1
)) ->
3057 if (y0
>= l.pagedispy
&& y0
<= (l.pagedispy
+ l.pagevh))
3058 || ((y1
>= l.pagedispy
&& y1
<= (l.pagedispy
+ l.pagevh)))
3060 match getopaque l.pageno with
3061 | Some
(opaque, _
) when validopaque opaque ->
3065 List.iter
f state.layout;
3066 copysel
""; (* ugly *)
3067 Glut.setCursor
Glut.CURSOR_INHERIT
;
3068 state.mstate
<- Mnone
;
3075 let birdseyemouse button bstate
x y
3076 (conf, leftx
, _
, hooverpageno
, anchor) =
3078 | Glut.LEFT_BUTTON
when bstate
= Glut.UP
->
3079 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
3080 let rec loop = function
3083 if y > l.pagedispy
&& y < l.pagedispy
+ l.pagevh
3084 && x > margin && x < margin + l.pagew
3086 leavebirdseye (conf, leftx
, l.pageno, hooverpageno
, anchor) false;
3091 | Glut.OTHER_BUTTON _
-> viewmouse button bstate
x y
3095 let mouse bstate button
x y =
3096 match state.mode with
3097 | View
-> viewmouse button bstate
x y
3098 | Birdseye beye
-> birdseyemouse button bstate
x y beye
3099 | Textentry _
| Outline _
| Items _
-> ()
3102 let mouse ~button ~
state ~
x ~
y = mouse state button
x y;;
3105 match state.mode with
3108 match state.mstate
with
3109 | Mzoom _
| Mnone
-> ()
3114 state.mstate
<- Mpan
(x, y);
3115 if conf.zoom > 1.0 then state.x <- state.x + dx;
3117 gotoy_and_clear_text y
3120 state.mstate
<- Msel
(a, (x, y));
3121 Glut.postRedisplay
()
3124 let y = min
conf.winh
(max
0 y) in
3125 let percent = float y /. float conf.winh
in
3126 let y = truncate
(float (state.maxy - conf.winh
) *. percent) in
3127 gotoy_and_clear_text y
3131 match state.mode with
3132 | Birdseye
(conf, leftx
, pageno, hooverpageno
, anchor) ->
3133 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
3134 let rec loop = function
3136 if hooverpageno
!= -1
3138 state.mode <- Birdseye
(conf, leftx
, pageno, -1, anchor);
3139 Glut.postRedisplay
();
3142 if y > l.pagedispy
&& y < l.pagedispy
+ l.pagevh
3143 && x > margin && x < margin + l.pagew
3145 state.mode <- Birdseye
(conf, leftx
, pageno, l.pageno, anchor);
3146 Glut.postRedisplay
();
3152 | Outline _
| Items _
| Textentry _
-> ()
3154 match state.mstate
with
3156 begin match getunder x y with
3157 | Unone
-> Glut.setCursor
Glut.CURSOR_INHERIT
3159 if conf.underinfo
then showtext 'u'
("ri: " ^ uri
);
3160 Glut.setCursor
Glut.CURSOR_INFO
3161 | Ulinkgoto
(page
, _
) ->
3163 then showtext 'p'
("age: " ^ string_of_int page
);
3164 Glut.setCursor
Glut.CURSOR_INFO
3166 if conf.underinfo
then showtext '
f'
("ont: " ^
s);
3167 Glut.setCursor
Glut.CURSOR_TEXT
3170 | Mpan _
| Msel _
| Mzoom _
| Mscroll
->
3181 match Sys.os_type
with
3182 | "Win32" -> Sys.getenv
"HOMEPATH"
3183 | _
-> Sys.getenv
"HOME"
3186 ("Can not determine home directory location: " ^
3187 Printexc.to_string exn
);
3191 let config_of c attrs
=
3195 | "scroll-bar-width" -> { c with scrollbw
= max
0 (int_of_string
v) }
3196 | "scroll-handle-height" -> { c with scrollh
= max
0 (int_of_string
v) }
3197 | "case-insensitive-search" -> { c with icase
= bool_of_string
v }
3198 | "preload" -> { c with preload = bool_of_string
v }
3199 | "page-bias" -> { c with pagebias
= int_of_string
v }
3200 | "scroll-step" -> { c with scrollstep
= max
1 (int_of_string
v) }
3201 | "auto-scroll-step" ->
3202 { c with autoscrollstep
= max
0 (int_of_string
v) }
3203 | "max-height-fit" -> { c with maxhfit
= bool_of_string
v }
3204 | "crop-hack" -> { c with crophack
= bool_of_string
v }
3205 | "throttle" -> { c with showall
= bool_of_string
v }
3206 | "highlight-links" -> { c with hlinks
= bool_of_string
v }
3207 | "under-cursor-info" -> { c with underinfo
= bool_of_string
v }
3208 | "vertical-margin" ->
3209 { c with interpagespace = max
0 (int_of_string
v) }
3211 let zoom = float_of_string
v /. 100. in
3212 let zoom = max
0.01 (min
2.2 zoom) in
3213 { c with zoom = zoom }
3214 | "presentation" -> { c with presentation = bool_of_string
v }
3215 | "rotation-angle" -> { c with angle
= int_of_string
v }
3216 | "width" -> { c with winw
= max
20 (int_of_string
v) }
3217 | "height" -> { c with winh
= max
20 (int_of_string
v) }
3218 | "persistent-bookmarks" -> { c with savebmarks
= bool_of_string
v }
3219 | "proportional-display" -> { c with proportional
= bool_of_string
v }
3220 | "pixmap-cache-size" -> { c with memlimit
= max
2 (int_of_string
v) }
3221 | "tex-count" -> { c with texcount
= max
1 (int_of_string
v) }
3222 | "slice-height" -> { c with sliceheight
= max
2 (int_of_string
v) }
3223 | "thumbnail-width" -> { c with thumbw
= max
2 (int_of_string
v) }
3224 | "persistent-location" -> { c with jumpback
= bool_of_string
v }
3225 | "background-color" -> { c with bgcolor
= color_of_string v }
3226 | "scrollbar-in-presentation" ->
3227 { c with scrollbarinpm
= bool_of_string
v }
3230 prerr_endline
("Error processing attribute (`" ^
3231 k ^
"'=`" ^
v ^
"'): " ^
Printexc.to_string exn
);
3234 let rec fold c = function
3237 let c = apply c k
v in
3243 let fromstring f pos n v d =
3246 dolog "Error processing attribute (%S=%S) at %d\n%s"
3247 n v pos (Printexc.to_string exn
)
3252 let bookmark_of attrs
=
3253 let rec fold title page rely
= function
3254 | ("title", v) :: rest
-> fold v page rely rest
3255 | ("page", v) :: rest
-> fold title v rely rest
3256 | ("rely", v) :: rest
-> fold title page
v rest
3257 | _
:: rest
-> fold title page rely rest
3258 | [] -> title, page
, rely
3260 fold "invalid" "0" "0" attrs
3264 let rec fold path page rely
pan = function
3265 | ("path", v) :: rest
-> fold v page rely
pan rest
3266 | ("page", v) :: rest
-> fold path
v rely
pan rest
3267 | ("rely", v) :: rest
-> fold path page
v pan rest
3268 | ("pan", v) :: rest
-> fold path page rely
v rest
3269 | _
:: rest
-> fold path page rely
pan rest
3270 | [] -> path
, page
, rely
, pan
3272 fold "" "0" "0" "0" attrs
3275 let setconf dst src
=
3276 dst
.scrollbw
<- src
.scrollbw
;
3277 dst
.scrollh
<- src
.scrollh
;
3278 dst
.icase
<- src
.icase
;
3279 dst
.preload <- src
.preload;
3280 dst
.pagebias
<- src
.pagebias
;
3281 dst
.verbose
<- src
.verbose
;
3282 dst
.scrollstep
<- src
.scrollstep
;
3283 dst
.maxhfit
<- src
.maxhfit
;
3284 dst
.crophack
<- src
.crophack
;
3285 dst
.autoscrollstep
<- src
.autoscrollstep
;
3286 dst
.showall
<- src
.showall
;
3287 dst
.hlinks
<- src
.hlinks
;
3288 dst
.underinfo
<- src
.underinfo
;
3289 dst
.interpagespace <- src
.interpagespace;
3290 dst
.zoom <- src
.zoom;
3291 dst
.presentation <- src
.presentation;
3292 dst
.angle
<- src
.angle
;
3293 dst
.winw
<- src
.winw
;
3294 dst
.winh
<- src
.winh
;
3295 dst
.savebmarks
<- src
.savebmarks
;
3296 dst
.memlimit
<- src
.memlimit
;
3297 dst
.proportional
<- src
.proportional
;
3298 dst
.texcount
<- src
.texcount
;
3299 dst
.sliceheight
<- src
.sliceheight
;
3300 dst
.thumbw
<- src
.thumbw
;
3301 dst
.jumpback
<- src
.jumpback
;
3302 dst
.bgcolor
<- src
.bgcolor
;
3303 dst
.scrollbarinpm
<- src
.scrollbarinpm
;
3307 let l = String.length
s in
3308 let b = Buffer.create
l in
3314 let h = Hashtbl.create
10 in
3315 let dc = { defconf with angle
= defconf.angle
} in
3316 let rec toplevel v t spos _
=
3318 | Vdata
| Vcdata
| Vend
-> v
3319 | Vopen
("llppconfig", _
, closed
) ->
3322 else { v with f = llppconfig
}
3324 error
"unexpected subelement at top level" s spos
3325 | Vclose _
-> error
"unexpected close at top level" s spos
3327 and llppconfig
v t spos _
=
3329 | Vdata
| Vcdata
| Vend
-> v
3330 | Vopen
("defaults", attrs
, closed
) ->
3331 let c = config_of dc attrs
in
3335 else { v with f = skip
"defaults" (fun () -> v) }
3337 | Vopen
("doc", attrs
, closed
) ->
3338 let pathent, spage
, srely
, span
= doc_of attrs
in
3339 let path = unent pathent
3340 and pageno = fromstring int_of_string spos
"page" spage
0
3341 and rely
= fromstring float_of_string spos
"rely" srely
0.0
3342 and pan = fromstring int_of_string spos
"pan" span
0 in
3343 let c = config_of dc attrs
in
3344 let anchor = (pageno, rely
) in
3346 then (Hashtbl.add
h path (c, [], pan, anchor); v)
3347 else { v with f = doc
path pan anchor c [] }
3350 error
"unexpected subelement in llppconfig" s spos
3352 | Vclose
"llppconfig" -> { v with f = toplevel }
3353 | Vclose _
-> error
"unexpected close in llppconfig" s spos
3355 and doc
path pan anchor c bookmarks v t spos _
=
3357 | Vdata
| Vcdata
-> v
3358 | Vend
-> error
"unexpected end of input in doc" s spos
3359 | Vopen
("bookmarks", _
, closed
) ->
3362 else { v with f = pbookmarks
path pan anchor c bookmarks }
3364 | Vopen
(_
, _
, _
) ->
3365 error
"unexpected subelement in doc" s spos
3368 Hashtbl.add
h path (c, List.rev
bookmarks, pan, anchor);
3369 { v with f = llppconfig
}
3371 | Vclose _
-> error
"unexpected close in doc" s spos
3373 and pbookmarks
path pan anchor c bookmarks v t spos _
=
3375 | Vdata
| Vcdata
-> v
3376 | Vend
-> error
"unexpected end of input in bookmarks" s spos
3377 | Vopen
("item", attrs
, closed
) ->
3378 let titleent, spage
, srely
= bookmark_of attrs
in
3379 let page = fromstring int_of_string spos
"page" spage
0
3380 and rely
= fromstring float_of_string spos
"rely" srely
0.0 in
3381 let bookmarks = (unent titleent, 0, (page, rely
)) :: bookmarks in
3383 then { v with f = pbookmarks
path pan anchor c bookmarks }
3386 { v with f = skip
"item" f }
3389 error
"unexpected subelement in bookmarks" s spos
3391 | Vclose
"bookmarks" ->
3392 { v with f = doc
path pan anchor c bookmarks }
3394 | Vclose _
-> error
"unexpected close in bookmarks" s spos
3396 and skip tag
f v t spos _
=
3398 | Vdata
| Vcdata
-> v
3400 error
("unexpected end of input in skipped " ^ tag
) s spos
3401 | Vopen
(tag'
, _
, closed
) ->
3405 let f'
() = { v with f = skip tag
f } in
3406 { v with f = skip tag'
f'
}
3410 else error
("unexpected close in skipped " ^ tag
) s spos
3413 parse
{ f = toplevel; accu = () } s;
3419 let len = in_channel_length ic
in
3420 let s = String.create
len in
3421 really_input ic
s 0 len;
3424 | Parse_error
(msg
, s, pos) ->
3425 let subs = subs s pos in
3426 let s = Printf.sprintf
"%s: at %d [..%s..]" msg
pos subs in
3427 failwith
("parse error: " ^
s)
3430 failwith
("config load error: " ^
Printexc.to_string exn
)
3436 let dir = Filename.concat
home ".config" in
3437 if Sys.is_directory
dir then dir else home
3440 Filename.concat
dir "llpp.conf"
3444 if Sys.file_exists
path
3447 (try Some
(open_in_bin
path)
3450 ("Error opening configuation file `" ^
path ^
"': " ^
3451 Printexc.to_string exn
);
3460 ("Error loading configuation from `" ^
path ^
"': " ^
3461 Printexc.to_string exn
);
3467 f (Hashtbl.create
0, defconf)
3472 let pc, pb
, px
, pa
=
3474 Hashtbl.find h (Filename.basename
state.path)
3475 with Not_found
-> dc, [], 0, (0, 0.0)
3479 state.bookmarks <- pb
;
3481 state.scrollw
<- conf.scrollbw
;
3483 then state.anchor <- pa
;
3484 cbput state.hists
.nav pa
;
3489 let add_attrs bb always
dc c =
3492 then Printf.bprintf bb
"\n %s='%b'" s a
3495 then Printf.bprintf bb
"\n %s='%d'" s a
3498 then Printf.bprintf bb
"\n %s='%f'" s (a*.100.)
3502 Printf.bprintf bb
"\n %s='%s'" s (color_to_string a)
3506 then dc.winw
, dc.winh
3508 match state.fullscreen
with
3510 | None
-> c.winw
, c.winh
3512 let zoom, presentation, interpagespace, showall
=
3514 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
3516 match state.mode with
3517 | Birdseye
(bc
, _
, _
, _
, _
) ->
3518 bc
.zoom, bc
.presentation, bc
.interpagespace, bc
.showall
3519 | _
-> c.zoom, c.presentation, c.interpagespace, c.showall
3521 oi
"width" w dc.winw
;
3522 oi
"height" h dc.winh
;
3523 oi
"scroll-bar-width" c.scrollbw
dc.scrollbw
;
3524 oi
"scroll-handle-height" c.scrollh
dc.scrollh
;
3525 ob "case-insensitive-search" c.icase
dc.icase
;
3526 ob "preload" c.preload dc.preload;
3527 oi
"page-bias" c.pagebias
dc.pagebias
;
3528 oi
"scroll-step" c.scrollstep
dc.scrollstep
;
3529 oi
"auto-scroll-step" c.autoscrollstep
dc.autoscrollstep
;
3530 ob "max-height-fit" c.maxhfit
dc.maxhfit
;
3531 ob "crop-hack" c.crophack
dc.crophack
;
3532 ob "throttle" showall
dc.showall
;
3533 ob "highlight-links" c.hlinks
dc.hlinks
;
3534 ob "under-cursor-info" c.underinfo
dc.underinfo
;
3535 oi
"vertical-margin" interpagespace dc.interpagespace;
3536 oz
"zoom" zoom dc.zoom;
3537 ob "presentation" presentation dc.presentation;
3538 oi
"rotation-angle" c.angle
dc.angle
;
3539 ob "persistent-bookmarks" c.savebmarks
dc.savebmarks
;
3540 ob "proportional-display" c.proportional
dc.proportional
;
3541 oi
"pixmap-cache-size" c.memlimit
dc.memlimit
;
3542 oi
"texcount" c.texcount
dc.texcount
;
3543 oi
"slice-height" c.sliceheight
dc.sliceheight
;
3544 oi
"thumbnail-width" c.thumbw
dc.thumbw
;
3545 ob "persistent-location" c.jumpback
dc.jumpback
;
3546 oc
"background-color" c.bgcolor
dc.bgcolor
;
3547 ob "scrollbar-in-presentation" c.scrollbarinpm
dc.scrollbarinpm
;
3551 let bb = Buffer.create
32768 in
3553 let dc = if conf.bedefault
then conf else dc in
3554 Buffer.add_string
bb "<llppconfig>\n<defaults ";
3555 add_attrs bb true dc dc;
3556 Buffer.add_string
bb "/>\n";
3558 let adddoc path pan anchor c bookmarks =
3559 if bookmarks == [] && c = dc && anchor = emptyanchor
3562 Printf.bprintf
bb "<doc path='%s'"
3563 (enent
path 0 (String.length
path));
3565 if anchor <> emptyanchor
3567 let n, y = anchor in
3568 Printf.bprintf
bb " page='%d'" n;
3571 Printf.bprintf
bb " rely='%f'" y
3576 then Printf.bprintf
bb " pan='%d'" pan;
3578 add_attrs bb false dc c;
3580 begin match bookmarks with
3581 | [] -> Buffer.add_string
bb "/>\n"
3583 Buffer.add_string
bb ">\n<bookmarks>\n";
3584 List.iter
(fun (title, _level
, (page, rely
)) ->
3586 "<item title='%s' page='%d'"
3587 (enent
title 0 (String.length
title))
3592 Printf.bprintf
bb " rely='%f'" rely
3594 Buffer.add_string
bb "/>\n";
3596 Buffer.add_string
bb "</bookmarks>\n</doc>\n";
3602 match state.mode with
3603 | Birdseye
(_
, pan, _
, _
, _
) -> pan
3606 let basename = Filename.basename state.path in
3607 adddoc basename pan (getanchor ())
3610 if state.ascrollstep
> 0
3611 then state.ascrollstep
3612 else conf.autoscrollstep
}
3613 (if conf.savebmarks
then state.bookmarks else []);
3615 Hashtbl.iter
(fun path (c, bookmarks, x, y) ->
3617 then adddoc path x y c bookmarks
3619 Buffer.add_string
bb "</llppconfig>";
3622 if Buffer.length
bb > 0
3625 let tmp = path ^
".tmp" in
3626 let oc = open_out_bin
tmp in
3627 Buffer.output_buffer
oc bb;
3629 Sys.rename
tmp path;
3632 ("error while saving configuration: " ^
Printexc.to_string exn
)
3639 [("-p", Arg.String
(fun s -> state.password
<- s) , " Set password")
3640 ;("-v", Arg.Unit
(fun () -> print_endline
Help.version
; exit
0),
3641 " Print version and exit")]
3643 (fun s -> state.path <- s)
3644 ("Usage: " ^
Sys.argv
.(0) ^
" [options] some.pdf\nOptions:")
3646 if String.length
state.path = 0
3647 then (prerr_endline
"file name missing"; exit
1);
3651 let _ = Glut.init
Sys.argv
in
3652 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
3653 let () = Glut.initWindowSize
conf.winw
conf.winh
in
3654 let _ = Glut.createWindow
("llpp " ^
Filename.basename state.path) in
3657 if Sys.os_type
= "Unix"
3659 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
3661 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
3662 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
3663 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
3664 Unix.bind
sock addr;
3666 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
3667 Unix.connect
csock addr;
3668 let ssock, _ = Unix.accept
sock in
3671 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
3672 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
3676 at_exit
(fun () -> Unix.shutdown
ssock Unix.SHUTDOWN_ALL
);
3680 let () = Glut.displayFunc
display in
3681 let () = Glut.reshapeFunc
reshape in
3682 let () = Glut.keyboardFunc
keyboard in
3683 let () = Glut.specialFunc
special in
3684 let () = Glut.idleFunc
(Some
idle) in
3685 let () = Glut.mouseFunc
mouse in
3686 let () = Glut.motionFunc
motion in
3687 let () = Glut.passiveMotionFunc
pmotion in
3689 init
ssock (conf.angle
, conf.proportional
, conf.texcount
, conf.sliceheight
);
3690 state.csock <- csock;
3691 state.ssock <- ssock;
3692 state.text <- "Opening " ^
state.path;
3693 writeopen state.path state.password
;
3697 let rec handlelablglutbug () =
3700 with Glut.BadEnum
"key in special_of_int" ->
3701 showtext '
!'
" LablGlut bug: special key not recognized";
3702 handlelablglutbug ()
3704 handlelablglutbug ();