4 | Ulinkgoto
of (int * int)
6 and facename
= string;;
8 let dolog fmt
= Printf.kprintf prerr_endline fmt
;;
11 angle
* proportional
* texcount
* sliceheight
* blockwidth
* fontpath
20 and proportional
= bool
21 and interpagespace
= int
30 external init
: Unix.file_descr
-> params
-> unit = "ml_init";;
31 external draw
: (int * int * int * bool) -> string -> unit = "ml_draw";;
32 external seltext
: string -> (int * int * int * int) -> int -> unit =
34 external copysel
: string -> unit = "ml_copysel";;
35 external getpdimrect
: int -> float array
= "ml_getpdimrect";;
36 external whatsunder
: string -> int -> int -> under
= "ml_whatsunder";;
37 external zoomforh
: int -> int -> int -> float = "ml_zoom_for_height";;
38 external drawstr
: int -> int -> int -> string -> float = "ml_draw_string";;
39 external measurestr
: int -> string -> float = "ml_measure_string";;
43 | Msel
of (mpos
* mpos
)
46 | Mzoom
of (int * int)
50 type textentry
= string * string * onhist
option * onkey
* ondone
51 and onkey
= string -> int -> te
52 and ondone
= string -> unit
53 and histcancel
= unit -> unit
54 and onhist
= ((histcmd
-> string) * histcancel
)
55 and histcmd
= HCnext
| HCprev
| HCfirst
| HClast
60 | TEswitch
of textentry
72 { store
= Array.create n v
79 let cbcap b
= Array.length b
.store
;;
84 b
.wc
<- (b
.wc
+ 1) mod cap;
86 b
.len
<- min
(b
.len
+ 1) cap;
89 let cbempty b
= b
.len
= 0;;
91 let cbgetg b circular dir
=
95 let rc = b
.rc + dir
in
107 else max
0 (min
rc (b
.len
-1))
113 let cbget b
= cbgetg b
false;;
114 let cbgetc b
= cbgetg b
true;;
117 let rc = b
.wc
- b
.len
in
118 let rc = if rc < 0 then cbcap b
+ rc else rc in
122 let cbdecr b
= b
.len
<- b
.len
- 1;;
137 { mutable scrollbw
: int
138 ; mutable scrollh
: int
139 ; mutable icase
: bool
140 ; mutable preload
: bool
141 ; mutable pagebias
: int
142 ; mutable verbose
: bool
143 ; mutable scrollstep
: int
144 ; mutable maxhfit
: bool
145 ; mutable crophack
: bool
146 ; mutable autoscrollstep
: int
147 ; mutable showall
: bool
148 ; mutable hlinks
: bool
149 ; mutable underinfo
: bool
150 ; mutable interpagespace
: interpagespace
151 ; mutable zoom
: float
152 ; mutable presentation
: bool
153 ; mutable angle
: angle
156 ; mutable savebmarks
: bool
157 ; mutable proportional
: proportional
158 ; mutable memlimit
: int
159 ; mutable texcount
: texcount
160 ; mutable sliceheight
: sliceheight
161 ; mutable blockwidth
: blockwidth
162 ; mutable thumbw
: width
163 ; mutable jumpback
: bool
164 ; mutable bgcolor
: float * float * float
165 ; mutable bedefault
: bool
166 ; mutable scrollbarinpm
: bool
167 ; mutable uifont
: string
171 type anchor
= pageno
* top
;;
173 type outline
= string * int * anchor
175 | Oarray
of outline array
176 | Olist
of outline list
177 | Onarrow
of string * outline array
* outline array
180 type rect
= float * float * float * float * float * float * float * float;;
182 type pagemapkey
= pageno
* width
* angle
* proportional
* gen
;;
184 let emptyanchor = (0, 0.0);;
187 | Birdseye
of (conf
* leftx
* pageno
* pageno
* anchor
)
188 | Outline
of (bool * int * int * outline array
* string * int * mode
)
189 | Items
of (int * int * item array
* string * int * mode
)
190 | Textentry
of (textentry
* onleave
)
192 and onleave
= leavetextentrystatus
-> unit
193 and leavetextentrystatus
= | Cancel
| Confirm
194 and item
= string * int * action
197 | Action
of (int -> int -> string -> int -> mode
)
200 let isbirdseye = function Birdseye _
-> true | _
-> false;;
201 let istextentry = function Textentry _
-> true | _
-> false;;
204 { mutable csock
: Unix.file_descr
205 ; mutable ssock
: Unix.file_descr
209 ; mutable scrollw
: int
210 ; mutable anchor
: anchor
212 ; mutable layout
: layout list
213 ; pagemap
: (pagemapkey
, (opaque
* pixmapsize
)) Hashtbl.t
214 ; mutable pdims
: (pageno
* width
* height
* leftx
) list
215 ; mutable pagecount
: int
216 ; pagecache
: string circbuf
217 ; mutable rendering
: bool
218 ; mutable mstate
: mstate
219 ; mutable searchpattern
: string
220 ; mutable rects
: (pageno
* recttype
* rect
) list
221 ; mutable rects1
: (pageno
* recttype
* rect
) list
222 ; mutable text
: string
223 ; mutable fullscreen
: (width
* height
) option
224 ; mutable mode
: mode
225 ; mutable outlines
: outlines
226 ; mutable bookmarks
: outline list
227 ; mutable path
: string
228 ; mutable password
: string
229 ; mutable invalidated
: int
230 ; mutable colorscale
: float
231 ; mutable memused
: int
233 ; mutable throttle
: layout list
option
234 ; mutable autoscroll
:int option
235 ; mutable help
: item array
236 ; mutable docinfo
: (int * string) list
237 ; mutable deadline
: float
241 { pat
: string circbuf
242 ; pag
: string circbuf
243 ; nav
: anchor circbuf
257 ; autoscrollstep
= 24
263 ; presentation
= false
268 ; proportional
= true
269 ; memlimit
= 32*1024*1024
275 ; bgcolor
= (0.5, 0.5, 0.5)
277 ; scrollbarinpm
= true
282 let conf = { defconf with angle
= defconf.angle
};;
285 let strings = ("llpp version " ^
Help.version
) :: "" :: Help.keys
in
286 Array.of_list
(List.map
(fun s
-> s
, 0, Noaction
) strings);
296 ; anchor
= emptyanchor
299 ; pagemap
= Hashtbl.create
10
300 ; pagecache
= cbnew 100 ""
311 ; outlines
= Olist
[]
317 { nav
= cbnew 100 (0, 0.0)
335 Printf.kprintf prerr_endline fmt
337 Printf.kprintf ignore fmt
341 let len = String.length s
in
343 let b = Buffer.create
n in
344 Buffer.add_char
b (Char.chr
((len lsr 24) land 0xff));
345 Buffer.add_char
b (Char.chr
((len lsr 16) land 0xff));
346 Buffer.add_char
b (Char.chr
((len lsr 8) land 0xff));
347 Buffer.add_char
b (Char.chr
((len lsr 0) land 0xff));
348 Buffer.add_string
b s
;
349 let s'
= Buffer.contents
b in
350 let n'
= Unix.write fd
s'
0 n in
351 if n'
!= n then failwith
"write failed";
356 let n = Unix.read fd
s 0 4 in
357 if n != 4 then failwith
"incomplete read(len)";
359 lor (Char.code
s.[0] lsl 24)
360 lor (Char.code
s.[1] lsl 16)
361 lor (Char.code
s.[2] lsl 8)
362 lor (Char.code
s.[3] lsl 0)
364 let s = String.create
len in
365 let n = Unix.read fd
s 0 len in
366 if n != len then failwith
"incomplete read(data)";
371 let b = Buffer.create
10 in
372 Buffer.add_string
b s;
373 let rec combine = function
376 Buffer.add_char
b ' '
;
379 | `
b b -> if b then "1" else "0"
381 | `i i
-> string_of_int i
382 | `f f
-> string_of_float f
383 | `I f
-> string_of_int
(truncate f
)
385 Buffer.add_string
b s;
392 let cmd = Buffer.contents
(makecmd s l
) in
393 writecmd state.csock
cmd;
399 let d = conf.winh
- h
in
406 let rec f pn ph pi fh l
=
408 | (n, _
, h
, _
) :: rest
->
409 let ips = calcips h
in
414 if isbirdseye state.mode
&& pn
= 0
419 let fh = fh + ((n - pn
) * (ph
+ pi
)) in
424 if conf.presentation
|| (isbirdseye state.mode
&& pn
= 0)
428 let fh = fh + ((state.pagecount
- pn
) * (ph
+ pi
)) + inc in
431 let fh = f 0 0 0 0 state.pdims
in
435 let getpageyh pageno
=
436 let rec f pn ph pi y l
=
438 | (n, _
, h
, _
) :: rest
->
439 let ips = calcips h
in
442 let h = if n = pageno
then h else ph
in
443 if conf.presentation
&& n = pageno
445 y
+ (pageno
- pn
) * (ph
+ pi
) + pi
, h
447 y
+ (pageno
- pn
) * (ph
+ pi
), h
449 let y = y + (if conf.presentation
then pi
else 0) in
450 let y = y + (n - pn
) * (ph
+ pi
) in
454 y + (pageno
- pn
) * (ph
+ pi
), ph
456 f 0 0 0 0 state.pdims
459 let getpagey pageno
= fst
(getpageyh pageno
);;
462 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu
=
463 let ((w
, h, ips, x
) as curr
), rest
, pdimno
, yinc
=
465 | (pageno'
, w
, h, x
) :: rest
when pageno'
= pageno
->
466 let ips = calcips h in
468 if conf.presentation
|| (isbirdseye state.mode
&& pageno
= 0)
472 (w
, h, ips, x
), rest
, pdimno
+ 1, yinc
474 prev
, pdims
, pdimno
, 0
476 let dy = dy + yinc in
477 let py = py + yinc in
478 if pageno
= state.pagecount
|| cacheleft
= 0 || dy >= sh
483 if py + h <= vy - yinc
485 let py = py + h + ips in
486 let dy = max
0 (py - y) in
496 let pagey = vy - py in
497 let pagevh = h - pagey in
498 let pagevh = min
(sh
- dy) pagevh in
499 let off = if yinc > 0 then py - vy else 0 in
500 let py = py + h + ips in
506 ; pagedispy
= dy + off
507 ; pagey = pagey + off
508 ; pagevh = pagevh - off
512 let accu = e :: accu in
519 ~cacheleft
:(cacheleft
-1)
522 if state.invalidated
= 0
532 ~cacheleft
:(cbcap state.pagecache
)
542 let y = state.y + incr
in
544 let y = min
y (state.maxy
- (if conf.maxhfit
then conf.winh
else 0)) in
548 let getopaque pageno
=
549 try Some
(Hashtbl.find
state.pagemap
550 (pageno
, state.w
, conf.angle
, conf.proportional
, state.gen
))
551 with Not_found
-> None
554 let cache pageno opaque
=
555 Hashtbl.replace
state.pagemap
556 (pageno
, state.w
, conf.angle
, conf.proportional
, state.gen
) opaque
559 let validopaque opaque
= String.length opaque
> 0;;
562 match getopaque l
.pageno
with
563 | None
when not
state.rendering
->
564 state.rendering
<- true;
565 cache l
.pageno
("", -1);
566 wcmd "render" [`i
(l
.pageno
+ 1)
573 let loadlayout layout =
574 let rec f all
= function
576 begin match getopaque l
.pageno
with
577 | None
-> render l
; f false ls
578 | Some
(opaque
, _
) -> f (all
&& validopaque opaque
) ls
582 f (layout <> []) layout;
585 let findpageforopaque opaque
=
587 (fun k
(v
, s) a
-> if v
= opaque
then Some
(k
, s) else a
)
591 let pagevisible layout n = List.exists
(fun l
-> l
.pageno
= n) layout;;
597 let memleft = conf.memlimit
- state.memused
in
600 let opaque = cbpeek state.pagecache
in
601 match findpageforopaque opaque with
602 | Some
((n, _
, _
, _
, _
), size
) ->
603 memleft + size
>= 0 && not
(pagevisible state.layout n)
610 let presentation = conf.presentation in
611 let interpagespace = conf.interpagespace in
612 let maxy = state.maxy in
613 conf.presentation <- false;
614 conf.interpagespace <- 0;
615 state.maxy <- calcheight ();
617 match state.layout with
619 | l
:: _
-> getpagey l
.pageno
+ l
.pagey
621 let y = if y < conf.winh
then 0 else y - conf.winh
in
622 let pages = layout y (conf.winh
*3) in
623 List.iter
render pages;
624 conf.presentation <- presentation;
625 conf.interpagespace <- interpagespace;
631 let y = min
state.maxy y in
632 let pages = layout y conf.winh
in
633 let ready = loadlayout pages in
639 state.layout <- pages;
640 state.throttle
<- None
;
641 Glut.postRedisplay
();
644 state.throttle
<- Some
pages;
649 state.layout <- pages;
650 state.throttle
<- None
;
651 Glut.postRedisplay
();
653 begin match state.mode
with
654 | Birdseye
(conf, leftx
, pageno
, hooverpageno
, anchor
) ->
655 if not
(pagevisible pages pageno
)
657 match state.layout with
660 state.mode
<- Birdseye
(conf, leftx
, l
.pageno
, hooverpageno
, anchor
)
667 let gotoy_and_clear_text y =
669 if not
conf.verbose
then state.text
<- "";
673 match state.layout with
675 | l
:: _
-> (l
.pageno
, float l
.pagey /. float l
.pageh
)
678 let getanchory (n, top
) =
679 let y, h = getpageyh n in
680 y + (truncate
(top
*. float h));
683 let gotoanchor anchor
=
684 gotoy (getanchory anchor
);
688 cbput state.hists
.nav
(getanchor ());
692 let anchor = cbgetc state.hists
.nav dir
in
697 let y, h = getpageyh n in
698 gotoy_and_clear_text (y + (truncate
(top
*. float h)));
701 let gotopage1 n top
=
702 let y = getpagey n in
703 gotoy_and_clear_text (y + top
);
711 state.invalidated
<- state.invalidated
+ 1;
715 let c = c *. state.colorscale
in
719 let scalecolor2 (r
, g
, b) =
720 (r
*. state.colorscale
, g
*. state.colorscale
, b *. state.colorscale
);
724 state.maxy <- calcheight ();
725 match state.mode
with
726 | Birdseye
(_
, _
, pageno
, _
, _
) ->
727 let y, h = getpageyh pageno
in
728 let top = (conf.winh
- h) / 2 in
729 gotoy (max
0 (y - top))
730 | _
-> gotoanchor state.anchor
734 GlMat.mode `projection
;
735 GlMat.load_identity
();
736 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
737 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
738 GlMat.scale3
(2.0 /. float state.w
, 2.0 /. float conf.winh
, 1.0);
741 GlMat.translate ~x
:(float state.x
) ();
746 GlMat.mode `projection
;
747 GlMat.load_identity
();
748 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
749 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
750 GlMat.scale3
(2.0 /. float conf.winw
, 2.0 /. float conf.winh
, 1.0);
754 let firsttime = ref true in
756 if state.invalidated
= 0 && not
!firsttime
757 then state.anchor <- getanchor ();
761 let w = truncate
(float w *. conf.zoom
) - state.scrollw
in
765 GlMat.mode `modelview
;
766 GlMat.load_identity
();
767 GlClear.color
(scalecolor 1.0);
768 GlClear.clear
[`color
];
771 wcmd "geometry" [`i
w; `i
h];
774 let drawstring size x
y s =
776 Gl.enable `texture_2d
;
777 ignore
(drawstr size x
y s);
779 Gl.disable `texture_2d
;
782 let drawstring1 size x
y s =
787 let len = String.length
state.text
in
789 GlDraw.color
(0.0, 0.0, 0.0);
791 (0.0, float (conf.winh
- 18))
792 (float (conf.winw
- state.scrollw
- 1), float conf.winh
)
794 GlDraw.color
(1.0, 1.0, 1.0);
795 drawstring 14 (if len > 0 then 8 else 2) (conf.winh
- 5) s;
797 match state.mode
with
798 | Textentry
((prefix
, text
, _
, _
, _
), _
) ->
802 Printf.sprintf
"%s%s_ [%s]" prefix text
state.text
804 Printf.sprintf
"%s%s_" prefix text
809 if len > 0 then drawstring state.text
813 state.text
<- Printf.sprintf
"%c%s" c s;
814 Glut.postRedisplay
();
823 state.rects
<- state.rects1
;
824 Glut.postRedisplay
()
827 let n = Scanf.sscanf
cmd "C %u" (fun n -> n) in
828 state.pagecount
<- n;
829 state.invalidated
<- state.invalidated
- 1;
830 if state.invalidated
= 0
834 let s = Scanf.sscanf
cmd "t %n"
835 (fun n -> String.sub
cmd n (String.length
cmd - n))
837 Glut.setWindowTitle
s
840 let s = Scanf.sscanf
cmd "T %n"
841 (fun n -> String.sub
cmd n (String.length
cmd - n))
843 if istextentry state.mode
850 Glut.postRedisplay
();
856 let s = Scanf.sscanf
cmd "V %n"
857 (fun n -> String.sub
cmd n (String.length
cmd - n))
863 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
864 Scanf.sscanf
cmd "F %u %d %f %f %f %f %f %f %f %f"
865 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
866 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
868 let y = (getpagey pageno) + truncate y0
in
871 state.rects1
<- [pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)]
874 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
875 Scanf.sscanf
cmd "R %u %d %f %f %f %f %f %f %f %f"
876 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
877 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
880 (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) :: state.rects1
883 let n, w, _h
, r
, l
, s, p
=
884 Scanf.sscanf
cmd "r %u %u %u %d %d %u %s"
885 (fun n w h r l
s p
->
886 (n-1, w, h, r
, l
!= 0, s, p
))
889 Hashtbl.replace
state.pagemap
(n, w, r
, l
, state.gen
) (p
, s);
890 state.memused
<- state.memused
+ s;
893 match state.throttle
with
894 | None
-> state.layout
895 | Some
layout -> layout
899 if (state.memused
<= conf.memlimit
) || cbempty state.pagecache
902 let evictedopaque = cbpeek state.pagecache
in
903 match findpageforopaque evictedopaque with
904 | None
-> failwith
"bug in gc"
905 | Some
((evictedn
, _
, _
, _
, gen
) as k
, evictedsize
) ->
906 if state.gen
!= gen
|| not
(pagevisible layout evictedn
)
908 wcmd "free" [`
s evictedopaque];
909 state.memused
<- state.memused
- evictedsize
;
910 Hashtbl.remove
state.pagemap k
;
911 cbdecr state.pagecache
;
918 cbput state.pagecache p
;
919 state.rendering
<- false;
921 begin match state.throttle
with
923 if pagevisible state.layout n
926 let allvisible = loadlayout state.layout in
927 if allvisible then preload ();
934 let y = getpagey l
.pageno + l
.pagey in
940 Scanf.sscanf
cmd "l %u %u %u %u" (fun n w h x
-> n, w, h, x
)
942 state.pdims
<- pdim :: state.pdims
945 let (l
, n, t
, h, pos
) =
946 Scanf.sscanf
cmd "o %u %u %d %u %n" (fun l
n t
h pos
-> l
, n, t
, h, pos
)
948 let s = String.sub
cmd pos
(String.length
cmd - pos
) in
949 let outline = (s, l
, (n, float t
/. float h)) in
951 match state.outlines with
952 | Olist
outlines -> Olist
(outline :: outlines)
953 | Oarray _
-> Olist
[outline]
954 | Onarrow _
-> Olist
[outline]
956 state.outlines <- outlines
960 if String.length
cmd > 1 && cmd.[1] = '
e'
962 state.docinfo
<- List.rev
state.docinfo
964 let s = Scanf.sscanf
cmd "i %n"
965 (fun n -> String.sub
cmd n (String.length
cmd - n))
967 state.docinfo
<- (1, s) :: state.docinfo
970 dolog "unknown cmd `%S'" cmd
973 let now = Unix.gettimeofday
;;
976 if state.deadline
== nan
then state.deadline
<- now ();
980 then max
0.0 (state.deadline
-. now ())
983 let r, _
, _
= Unix.select
[state.csock
] [] [] timeout in
984 state.deadline
<- state.deadline
+. delay
;
987 begin match state.autoscroll
with
988 | Some step
when step
!= 0 ->
989 let y = state.y + step
in
993 else if y >= state.maxy then 0 else y
997 then state.text
<- "";
1002 let cmd = readcmd state.csock
in
1011 let action = function
1012 | HCprev
-> cbget cb ~
-1
1013 | HCnext
-> cbget cb
1
1014 | HCfirst
-> cbget cb ~
-(cb
.rc)
1015 | HClast
-> cbget cb
(cb
.len - 1 - cb
.rc)
1016 and cancel
() = cb
.rc <- rc
1020 let search pattern forward
=
1021 if String.length pattern
> 0
1024 match state.layout with
1027 l
.pageno, (l
.pagey + if forward
then 0 else 0*l
.pagevh)
1030 let b = makecmd "search"
1031 [`
b conf.icase
; `i
pn; `i
py; `i
(if forward
then 1 else 0)]
1033 Buffer.add_char
b '
,'
;
1034 Buffer.add_string
b pattern
;
1035 Buffer.add_char
b '
\000'
;
1038 writecmd state.csock
cmd;
1041 let intentry text key
=
1042 let c = Char.unsafe_chr key
in
1045 let s = "x" in s.[0] <- c;
1046 let text = text ^
s in
1050 state.text <- Printf.sprintf
"invalid char (%d, `%c')" key
c;
1055 let b = Buffer.create
(String.length
s + 1) in
1056 Buffer.add_string
b s;
1057 Buffer.add_char
b c;
1061 let textentry text key
=
1062 let c = Char.unsafe_chr key
in
1064 | _
when key
>= 32 && key
< 127 ->
1065 let text = addchar text c in
1069 dolog "unhandled key %d char `%c'" key
(Char.unsafe_chr key
);
1073 let reinit angle proportional
=
1074 conf.angle
<- angle
;
1075 conf.proportional
<- proportional
;
1077 wcmd "reinit" [`i angle
; `
b proportional
];
1081 let zoom = max
0.01 zoom in
1082 if zoom <> conf.zoom
1087 reshape conf.winw
conf.winh
;
1088 state.text <- Printf.sprintf
"zoom is now %-5.1f" (zoom *. 100.0);
1092 let enterbirdseye () =
1093 let zoom = float conf.thumbw
/. float conf.winw
in
1094 let birdseyepageno =
1095 let cy = conf.winh
/ 2 in
1099 let rec fold best
= function
1102 let d = cy - (l
.pagedispy
+ l
.pagevh/2)
1103 and dbest
= cy - (best
.pagedispy
+ best
.pagevh/2) in
1104 if abs
d < abs dbest
1111 state.mode
<- Birdseye
(
1112 { conf with zoom = conf.zoom }, state.x
, birdseyepageno, -1, getanchor ()
1115 conf.presentation <- false;
1116 conf.interpagespace <- 10;
1117 conf.hlinks
<- false;
1119 state.mstate
<- Mnone
;
1120 conf.showall
<- false;
1121 Glut.setCursor
Glut.CURSOR_INHERIT
;
1124 state.text <- Printf.sprintf
"birds eye mode on (zoom %3.1f%%)"
1129 reshape conf.winw
conf.winh
;
1132 let leavebirdseye (c, leftx
, pageno, _
, anchor) goback
=
1134 conf.zoom <- c.zoom;
1135 conf.presentation <- c.presentation;
1136 conf.interpagespace <- c.interpagespace;
1137 conf.showall
<- c.showall
;
1138 conf.hlinks
<- c.hlinks
;
1142 state.text <- Printf.sprintf
"birds eye mode off (zoom %3.1f%%)"
1145 reshape conf.winw
conf.winh
;
1146 state.anchor <- if goback
then anchor else (pageno, 0.0);
1149 let togglebirdseye () =
1150 match state.mode
with
1151 | Birdseye vals
-> leavebirdseye vals
true
1152 | View
| Outline _
-> enterbirdseye ()
1156 let upbirdseye (conf, leftx
, pageno, hooverpageno
, anchor) =
1157 let pageno = max
0 (pageno - 1) in
1158 let rec loop = function
1159 | [] -> gotopage1 pageno 0
1160 | l
:: _
when l
.pageno = pageno ->
1161 if l
.pagedispy
>= 0 && l
.pagey = 0
1162 then Glut.postRedisplay
()
1163 else gotopage1 pageno 0
1164 | _
:: rest
-> loop rest
1167 state.mode
<- Birdseye
(conf, leftx
, pageno, hooverpageno
, anchor)
1170 let downbirdseye (conf, leftx
, pageno, hooverpageno
, anchor) =
1171 let pageno = min
(state.pagecount
- 1) (pageno + 1) in
1172 state.mode
<- Birdseye
(conf, leftx
, pageno, hooverpageno
, anchor);
1173 let rec loop = function
1175 let y, h = getpageyh pageno in
1176 let dy = (y - state.y) - (conf.winh
- h - conf.interpagespace) in
1178 | l
:: _
when l
.pageno = pageno ->
1179 if l
.pagevh != l
.pageh
1180 then gotoy (clamp (l
.pageh
- l
.pagevh + conf.interpagespace))
1181 else Glut.postRedisplay
()
1182 | _
:: rest
-> loop rest
1187 let optentry mode _ key
=
1188 let btos b = if b then "on" else "off" in
1189 let c = Char.unsafe_chr key
in
1193 try conf.scrollstep
<- int_of_string
s with exc
->
1194 state.text <- Printf.sprintf
"bad integer `%s': %s"
1195 s (Printexc.to_string exc
)
1197 TEswitch
("scroll step: ", "", None
, intentry, ondone)
1202 conf.autoscrollstep
<- int_of_string
s;
1203 if state.autoscroll
<> None
1204 then state.autoscroll
<- Some
conf.autoscrollstep
1206 state.text <- Printf.sprintf
"bad integer `%s': %s"
1207 s (Printexc.to_string exc
)
1209 TEswitch
("auto scroll step: ", "", None
, intentry, ondone)
1214 let zoom = float (int_of_string
s) /. 100.0 in
1217 state.text <- Printf.sprintf
"bad integer `%s': %s"
1218 s (Printexc.to_string exc
)
1220 TEswitch
("zoom: ", "", None
, intentry, ondone)
1225 conf.thumbw
<- max
2 (min
1920 (int_of_string
s));
1227 Printf.sprintf
"thumbnail width is set to %d" conf.thumbw
;
1228 begin match mode
with
1230 leavebirdseye beye
false;
1235 state.text <- Printf.sprintf
"bad integer `%s': %s"
1236 s (Printexc.to_string exc
)
1238 TEswitch
("thumbnail width: ", "", None
, intentry, ondone)
1243 Some
(int_of_string
s)
1245 state.text <- Printf.sprintf
"bad integer `%s': %s"
1246 s (Printexc.to_string exc
);
1249 | Some angle
-> reinit angle
conf.proportional
1252 TEswitch
("rotation: ", "", None
, intentry, ondone)
1255 conf.icase
<- not
conf.icase
;
1256 TEdone
("case insensitive search " ^
(btos conf.icase
))
1259 conf.preload <- not
conf.preload;
1261 TEdone
("preload " ^
(btos conf.preload))
1264 conf.verbose
<- not
conf.verbose
;
1265 TEdone
("verbose " ^
(btos conf.verbose
))
1268 conf.maxhfit
<- not
conf.maxhfit
;
1269 state.maxy <- state.maxy + (if conf.maxhfit
then -conf.winh
else conf.winh
);
1270 TEdone
("maxhfit " ^
(btos conf.maxhfit
))
1273 conf.crophack
<- not
conf.crophack
;
1274 TEdone
("crophack " ^
btos conf.crophack
)
1277 conf.showall
<- not
conf.showall
;
1278 TEdone
("throttle " ^
btos conf.showall
)
1281 conf.underinfo
<- not
conf.underinfo
;
1282 TEdone
("underinfo " ^
btos conf.underinfo
)
1285 conf.savebmarks
<- not
conf.savebmarks
;
1286 TEdone
("persistent bookmarks " ^
btos conf.savebmarks
)
1292 match state.layout with
1297 conf.interpagespace <- int_of_string
s;
1298 state.maxy <- calcheight ();
1299 let y = getpagey pageno in
1302 state.text <- Printf.sprintf
"bad integer `%s': %s"
1303 s (Printexc.to_string exc
)
1305 TEswitch
("vertical margin: ", "", None
, intentry, ondone)
1308 reinit conf.angle
(not
conf.proportional
);
1309 TEdone
("proprortional display " ^
btos conf.proportional
)
1312 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
1316 let maxoutlinerows () = (conf.winh
- 31) / 16;;
1318 let enterselector allowdel
outlines errmsg msg
=
1319 if Array.length
outlines = 0
1321 showtext ' ' errmsg
;
1325 Glut.setCursor
Glut.CURSOR_INHERIT
;
1327 match state.layout with
1329 | {pageno=pageno} :: _
-> pageno
1333 if n = Array.length
outlines
1336 let (_
, _
, (outlinepageno
, _
)) = outlines.(n) in
1337 if outlinepageno
>= pageno then n else loop (n+1)
1341 state.mode
<- Outline
1342 (allowdel
, active, max
0 (active - maxoutlinerows () / 2), outlines, "", 0,
1344 Glut.postRedisplay
();
1348 let enteroutlinemode () =
1350 match state.outlines with
1353 let a = Array.of_list
(List.rev l
) in
1354 state.outlines <- Oarray
a;
1356 | Onarrow
(pat
, a, _
) ->
1357 a, "Outline was narrowed to `" ^ pat ^
"' (Ctrl-u to restore)"
1359 enterselector false outlines "Document has no outline" msg
;
1362 let enterbookmarkmode () =
1363 let bookmarks = Array.of_list
state.bookmarks in
1364 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1367 let mode_to_string mode
=
1368 let b = Buffer.create
10 in
1369 let rec f = function
1370 | Textentry
(_
, _
) -> Buffer.add_string
b "Textentry ";
1371 | View
-> Buffer.add_string
b "View"
1372 | Birdseye _
-> Buffer.add_string
b "Birdseye"
1373 | Items _
-> Buffer.add_string
b "Items"
1374 | Outline _
-> Buffer.add_string
b "Outline"
1380 let color_of_string s =
1381 Scanf.sscanf
s "%d/%d/%d" (fun r g
b ->
1382 (float r /. 256.0, float g
/. 256.0, float b /. 256.0)
1386 let color_to_string (r, g
, b) =
1387 let r = truncate
(r *. 256.0)
1388 and g
= truncate
(g
*. 256.0)
1389 and b = truncate
(b *. 256.0) in
1390 Printf.sprintf
"%d/%d/%d" r g
b
1393 let enterinfomode () =
1394 let btos = function true -> "on" | _
-> "off" in
1395 let mode = state.mode in
1396 let rec makeitems () =
1397 let intp name get set
=
1398 Printf.sprintf
"%s\t%d" name
(get
()), 1, Action
(
1399 fun active first _ pan
->
1404 state.text <- Printf.sprintf
"bad integer `%s': %s"
1405 s (Printexc.to_string exn
);
1408 if n != max_int
then set
n;
1410 let te = name ^
": ", "", None
, intentry, ondone in
1415 state.mode <- Items
(active, first
, makeitems (), "", pan
, mode)
1418 and boolp name get set
=
1419 Printf.sprintf
"%s\t%s" name
(btos (get
())), 1, Action
(
1420 fun active first qsearch pan
->
1423 Items
(active, first
, makeitems (), qsearch
, pan
, mode);
1425 and colorp name get set
=
1426 Printf.sprintf
"%s\t%s" name
(color_to_string (get
())), 1, Action
(
1427 fun active first _ pan
->
1428 let invalid = (nan
, nan
, nan
) in
1431 try color_of_string s
1433 state.text <- Printf.sprintf
"bad color `%s': %s"
1434 s (Printexc.to_string exn
);
1440 let te = name ^
": ", "", None
, textentry, ondone in
1445 state.mode <- Items
(active, first
, makeitems (), "", pan
, mode)
1450 let birdseye = isbirdseye mode in
1452 (if birdseye then "Setup bird's eye" else "Setup"), 0, Noaction
;
1454 boolp
"presentation"
1455 (fun () -> conf.presentation)
1457 conf.presentation <- v;
1458 state.anchor <- getanchor ();
1461 boolp
"ignore case in searches"
1462 (fun () -> conf.icase
)
1463 (fun v -> conf.icase
<- v);
1466 (fun () -> conf.preload)
1467 (fun v -> conf.preload <- v);
1470 (fun () -> conf.verbose
)
1471 (fun v -> conf.verbose
<- v);
1474 (fun () -> conf.maxhfit
)
1475 (fun v -> conf.maxhfit
<- v);
1478 (fun () -> conf.crophack
)
1479 (fun v -> conf.crophack
<- v);
1482 (fun () -> conf.showall
)
1483 (fun v -> conf.showall
<- v);
1485 boolp
"highlight links"
1486 (fun () -> conf.hlinks
)
1487 (fun v -> conf.hlinks
<- v);
1490 (fun () -> conf.underinfo
)
1491 (fun v -> conf.underinfo
<- v);
1492 boolp
"persistent bookmarks"
1493 (fun () -> conf.savebmarks
)
1494 (fun v -> conf.savebmarks
<- v);
1496 boolp
"proportional display"
1497 (fun () -> conf.proportional
)
1498 (fun v -> reinit conf.angle
v);
1500 boolp
"persistent location"
1501 (fun () -> conf.jumpback
)
1502 (fun v -> conf.jumpback
<- v);
1506 intp "vertical margin"
1507 (fun () -> conf.interpagespace)
1509 conf.interpagespace <- n;
1511 match state.layout with
1516 state.maxy <- calcheight ();
1517 let y = getpagey pageno in
1522 (fun () -> conf.pagebias
)
1523 (fun v -> conf.pagebias
<- v);
1526 (fun () -> conf.scrollstep
)
1527 (fun n -> conf.scrollstep
<- n);
1529 intp "auto scroll step"
1531 match state.autoscroll
with
1533 | _
-> conf.autoscrollstep
)
1535 if state.autoscroll
<> None
1536 then state.autoscroll
<- Some
n;
1537 conf.autoscrollstep
<- n);
1540 (fun () -> truncate
(conf.zoom *. 100.))
1541 (fun v -> setzoom ((float v) /. 100.));
1544 (fun () -> conf.angle
)
1545 (fun v -> reinit v conf.proportional
);
1547 intp "scroll bar width"
1548 (fun () -> state.scrollw
)
1552 reshape conf.winw
conf.winh
;
1555 intp "scroll handle height"
1556 (fun () -> conf.scrollh
)
1557 (fun v -> conf.scrollh
<- v;);
1559 intp "thumbnail width"
1560 (fun () -> conf.thumbw
)
1562 conf.thumbw
<- min
1920 v;
1565 leavebirdseye beye
false;
1570 colorp
"background color"
1571 (fun () -> conf.bgcolor
)
1572 (fun v -> conf.bgcolor
<- v);
1575 "Presentation mode", 0, Noaction
;
1577 boolp
"scrollbar visible"
1578 (fun () -> conf.scrollbarinpm
)
1580 if v != conf.scrollbarinpm
1582 conf.scrollbarinpm
<- v;
1583 if conf.presentation
1585 state.scrollw
<- if v then conf.scrollbw
else 0;
1586 reshape conf.winw
conf.winh
;
1592 "Pixmap Cache", 0, Noaction
;
1594 intp "size (advisory)"
1595 (fun () -> conf.memlimit
)
1596 (fun v -> conf.memlimit
<- v);
1597 Printf.sprintf
"%s\t%d" "used" state.memused
, 1, Noaction
;
1604 :: ("Document", 0, Noaction
)
1605 :: List.map
(fun (_
, s) -> (s, 1, Noaction
)) state.docinfo
1611 :: (Printf.sprintf
"Save these parameters as defaults at exit (%s)"
1612 (btos conf.bedefault
),
1615 fun active first qsearch pan
->
1616 conf.bedefault
<- not
conf.bedefault
;
1617 Items
(active, first
, makeitems (), qsearch
, pan
, mode);
1622 Array.of_list
(items @ tailer)
1625 state.mode <- Items
(1, 0, makeitems (), "", 0, mode);
1626 Glut.postRedisplay
();
1629 let enterhelpmode () =
1630 state.mode <- Items
(-1, 0, state.help
, "", 0, state.mode);
1631 Glut.postRedisplay
();
1634 let quickbookmark ?title
() =
1635 match state.layout with
1641 let sec = Unix.gettimeofday
() in
1642 let tm = Unix.localtime
sec in
1643 Printf.sprintf
"Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1647 (tm.Unix.tm_year
+ 1900)
1650 | Some
title -> title
1653 (title, 0, (l
.pageno, float l
.pagey /. float l
.pageh
))
1658 state.fullscreen
<- None
;
1659 Glut.reshapeWindow
w h;
1662 let writeopen path password
=
1663 writecmd state.csock
("open " ^ path ^
"\000" ^ password ^
"\000");
1666 let opendoc path password
=
1669 state.password
<- password
;
1670 state.gen
<- state.gen
+ 1;
1671 state.docinfo
<- [];
1673 writeopen path password
;
1674 Glut.setWindowTitle
("llpp " ^
Filename.basename path
);
1675 wcmd "geometry" [`i
state.w; `i
conf.winh
];
1678 let viewkeyboard key
=
1680 let mode = state.mode in
1681 state.mode <- Textentry
(te, fun _
-> state.mode <- mode);
1684 Glut.postRedisplay
()
1686 let c = Char.chr key
in
1688 | '
\027'
-> (* escape *)
1689 if String.length
state.text > 0
1692 Glut.postRedisplay
();
1700 | '
\008'
-> (* backspace *)
1701 let y = getnav ~
-1 in
1702 gotoy_and_clear_text y
1710 Glut.postRedisplay
()
1713 let ondone isforw
s =
1714 cbput state.hists
.pat
s;
1715 state.searchpattern
<- s;
1718 let s = String.create
1 in
1720 enttext (s, "", Some
(onhist state.hists
.pat
),
1721 textentry, ondone (c ='
/'
))
1723 | '
+'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1724 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1725 setzoom (conf.zoom +. incr)
1730 try int_of_string
s with exc
->
1731 state.text <- Printf.sprintf
"bad integer `%s': %s"
1732 s (Printexc.to_string exc
);
1738 state.text <- "page bias is now " ^ string_of_int
n;
1741 enttext ("page bias: ", "", None
, intentry, ondone)
1743 | '
-'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1744 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1745 setzoom (max
0.01 (conf.zoom -. decr))
1748 let ondone msg
= state.text <- msg
in
1750 "option [acfhilpstvAPRSZ]: ", "", None
,
1751 optentry state.mode, ondone
1754 | '
0'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1757 | '
1'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1758 let zoom = zoomforh
conf.winw
conf.winh
state.scrollw
in
1762 | '
9'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1768 try int_of_string
s with exc
->
1769 state.text <- Printf.sprintf
"bad integer `%s': %s"
1770 s (Printexc.to_string exc
);
1776 cbput state.hists
.pag
(string_of_int
n);
1777 gotoy_and_clear_text (getpagey (n + conf.pagebias
- 1))
1780 let pageentry text key
=
1781 match Char.unsafe_chr key
with
1782 | 'g'
-> TEdone
text
1783 | _
-> intentry text key
1785 let text = "x" in text.[0] <- c;
1786 enttext (":", text, Some
(onhist state.hists
.pag
), pageentry, ondone)
1789 state.scrollw
<- if state.scrollw
> 0 then 0 else conf.scrollbw
;
1790 reshape conf.winw
conf.winh
;
1793 conf.hlinks
<- not
conf.hlinks
;
1794 state.text <- "highlightlinks " ^
if conf.hlinks
then "on" else "off";
1795 Glut.postRedisplay
()
1798 begin match state.autoscroll
with
1800 conf.autoscrollstep
<- step
;
1801 state.autoscroll
<- None
1803 if conf.autoscrollstep
= 0
1804 then state.autoscroll
<- Some
1
1805 else state.autoscroll
<- Some
conf.autoscrollstep
1809 conf.presentation <- not
conf.presentation;
1810 if conf.presentation
1812 if not
conf.scrollbarinpm
1813 then state.scrollw
<- 0;
1816 state.scrollw
<- conf.scrollbw
;
1818 showtext ' '
("presentation mode " ^
1819 if conf.presentation then "on" else "off");
1820 state.anchor <- getanchor ();
1824 begin match state.fullscreen
with
1826 state.fullscreen
<- Some
(conf.winw
, conf.winh
);
1829 state.fullscreen
<- None
;
1834 gotoy_and_clear_text 0
1837 search state.searchpattern
true
1840 search state.searchpattern
false
1843 begin match state.layout with
1846 gotoy_and_clear_text (getpagey l
.pageno)
1850 begin match List.rev
state.layout with
1853 let pageno = min
(l
.pageno+1) (state.pagecount
-1) in
1854 gotoy_and_clear_text (getpagey pageno)
1857 | '
\127'
-> (* del *)
1858 begin match state.layout with
1861 let pageno = max
0 (l
.pageno-1) in
1862 gotoy_and_clear_text (getpagey pageno)
1867 if fn
= -1 then l
.pageno, l
.pageno else fn
, l
.pageno
1869 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
1871 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
1875 else (100. *. (float state.y /. float maxy)) in
1878 Printf.sprintf
"Page %d of %d %.2f%%"
1879 (fn+1) state.pagecount
percent
1882 "Pages %d-%d of %d %.2f%%"
1883 (fn+1) (ln
+1) state.pagecount
percent
1888 begin match state.layout with
1891 doreshape (l
.pagew
+ state.scrollw
) l
.pageh
;
1892 Glut.postRedisplay
();
1896 enterbookmarkmode ()
1906 match state.layout with
1909 (s, 0, (l
.pageno, float l
.pagey /. float l
.pageh
))
1913 enttext ("bookmark: ", "", None
, textentry, ondone)
1917 showtext ' '
"Quick bookmark added";
1920 begin match state.layout with
1922 let rect = getpdimrect l
.pagedimno
in
1926 (truncate
(1.8 *. (rect.(1) -. rect.(0))),
1927 truncate
(1.2 *. (rect.(3) -. rect.(0))))
1929 (truncate
(rect.(1) -. rect.(0)),
1930 truncate
(rect.(3) -. rect.(0)))
1934 doreshape (w + state.scrollw
) (h + conf.interpagespace)
1936 Glut.postRedisplay
();
1942 reinit (conf.angle
+ (if c = '
>'
then 30 else -30)) conf.proportional
1947 (min
(state.colorscale
+. (if c = '
]'
then 0.1 else -0.1)) 1.0);
1948 Glut.postRedisplay
()
1951 begin match state.mode with
1952 | Birdseye beye
-> upbirdseye beye
1953 | _
-> gotoy (clamp (-conf.scrollstep
))
1957 begin match state.mode with
1958 | Birdseye beye
-> downbirdseye beye
1959 | _
-> gotoy (clamp conf.scrollstep
)
1963 state.anchor <- getanchor ();
1964 opendoc state.path
state.password
1967 vlog "huh? %d %c" key
(Char.chr key
);
1970 let textentrykeyboard key
((c, text, opthist
, onkey
, ondone), onleave
) =
1972 state.mode <- Textentry
(te, onleave
);
1975 Glut.postRedisplay
()
1977 match Char.unsafe_chr key
with
1978 | '
\008'
-> (* backspace *)
1979 let len = String.length
text in
1983 Glut.postRedisplay
();
1986 let s = String.sub
text 0 (len - 1) in
1987 enttext (c, s, opthist
, onkey
, ondone)
1993 Glut.postRedisplay
()
1995 | '
\027'
-> (* escape *)
1996 begin match opthist
with
1998 | Some
(_
, onhistcancel
) -> onhistcancel
()
2001 Glut.postRedisplay
()
2004 begin match onkey
text key
with
2008 Glut.postRedisplay
()
2011 enttext (c, text, opthist
, onkey
, ondone);
2015 Glut.postRedisplay
()
2018 state.mode <- Textentry
(te, onleave
);
2019 Glut.postRedisplay
()
2023 let birdseyekeyboard key
((_
, _
, pageno, _
, _
) as beye
) =
2025 | 27 -> (* escape *)
2026 leavebirdseye beye
true
2028 | 12 -> (* ctrl-l *)
2029 let y, h = getpageyh pageno in
2030 let top = (conf.winh
- h) / 2 in
2031 gotoy (max
0 (y - top))
2034 leavebirdseye beye
false
2040 let itemskeyboard key
(active, first
, items, qsearch
, pan
, oldmode
) =
2041 let set active first qsearch
=
2042 state.mode <- Items
(active, first
, items, qsearch
, pan
, oldmode
)
2044 let search active pattern
incr =
2047 if n >= Array.length
items || n < 0
2050 let (s, _
, _
) = items.(n) in
2052 (try ignore
(Str.search_forward re
s 0); true
2053 with Not_found
-> false)
2055 else loop (n + incr)
2060 let re = Str.regexp_case_fold pattern
in
2066 let firstof active = max
0 (active - maxoutlinerows () / 2) in
2068 | 18 | 19 -> (* ctrl-r/ctlr-s *)
2069 let incr = if key
= 18 then -1 else 1 in
2071 match search (active + incr) qsearch
incr with
2073 state.text <- qsearch ^
" [not found]";
2076 state.text <- qsearch
;
2077 active, firstof active
2079 set active first qsearch
;
2080 Glut.postRedisplay
();
2082 | 8 -> (* backspace *)
2083 let len = String.length qsearch
in
2090 set active first
"";
2093 let qsearch = String.sub
qsearch 0 (len - 1) in
2095 match search active qsearch ~
-1 with
2097 state.text <- qsearch ^
" [not found]";
2100 state.text <- qsearch;
2101 active, firstof active
2103 set active first
qsearch
2105 Glut.postRedisplay
()
2107 | _
when key
>= 32 && key
< 127 ->
2108 let pattern = addchar qsearch (Char.chr key
) in
2110 match search active pattern 1 with
2112 state.text <- pattern ^
" [not found]";
2115 state.text <- pattern;
2116 active, firstof active
2118 set active first
pattern;
2119 Glut.postRedisplay
()
2121 | 27 -> (* escape *)
2123 if String.length
qsearch = 0
2125 state.mode <- oldmode
;
2128 set active first
"";
2130 Glut.postRedisplay
()
2133 if active >= 0 && active < Array.length
items
2135 match items.(active) with
2137 state.mode <- f active first
qsearch pan
2141 state.mode <- oldmode
2145 state.mode <- oldmode
2147 Glut.postRedisplay
();
2149 | _
-> dolog "unknown key %d" key
2152 let outlinekeyboard key
2153 (allowdel
, active, first
, outlines, qsearch, pan
, oldmode
) =
2154 let narrow outlines pattern =
2155 let reopt = try Some
(Str.regexp_case_fold
pattern) with _
-> None
in
2159 let rec fold accu n =
2163 let (s, _
, _
) as o
= outlines.(n) in
2165 if (try ignore
(Str.search_forward
re s 0); true
2166 with Not_found
-> false)
2172 let matched = fold [] (Array.length
outlines - 1) in
2173 if matched = [] then None
else Some
(Array.of_list
matched)
2175 let search active pattern incr =
2178 if n = Array.length
outlines || n = -1
2181 let (s, _
, _
) = outlines.(n) in
2183 (try ignore
(Str.search_forward
re s 0); true
2184 with Not_found
-> false)
2186 else loop (n + incr)
2191 let re = Str.regexp_case_fold
pattern in
2197 let firstof active = max
0 (active - maxoutlinerows () / 2) in
2199 | 27 -> (* escape *)
2201 if String.length
qsearch = 0
2203 state.mode <- oldmode
;
2206 state.mode <- Outline
(
2207 allowdel
, active, first
, outlines, "", pan
, oldmode
2210 Glut.postRedisplay
();
2212 | 18 | 19 -> (* ctrl-r/ctrl-s *)
2213 let incr = if key
= 18 then -1 else 1 in
2215 match search (active + incr) qsearch incr with
2217 state.text <- qsearch ^
" [not found]";
2220 state.text <- qsearch;
2221 active, firstof active
2223 state.mode <- Outline
(
2224 allowdel
, active, first
, outlines, qsearch, pan
, oldmode
2226 Glut.postRedisplay
();
2228 | 8 -> (* backspace *)
2229 let len = String.length
qsearch in
2236 state.mode <- Outline
(
2237 allowdel
, active, first
, outlines, "", pan
, oldmode
2241 let qsearch = String.sub
qsearch 0 (len - 1) in
2243 match search active qsearch ~
-1 with
2245 state.text <- qsearch ^
" [not found]";
2248 state.text <- qsearch;
2249 active, firstof active
2251 state.mode <- Outline
(
2252 allowdel
, active, first
, outlines, qsearch, pan
, oldmode
2255 Glut.postRedisplay
()
2258 if active < Array.length
outlines
2260 let (_
, _
, anchor) = outlines.(active) in
2265 if allowdel
then state.bookmarks <- Array.to_list
outlines;
2266 state.mode <- oldmode
;
2267 Glut.postRedisplay
();
2269 | _
when key
>= 32 && key
< 127 ->
2270 let pattern = addchar qsearch (Char.chr key
) in
2272 match search active pattern 1 with
2274 state.text <- pattern ^
" [not found]";
2277 state.text <- pattern;
2278 active, firstof active
2280 state.mode <- Outline
(
2281 allowdel
, active, first
, outlines, pattern, pan
, oldmode
2283 Glut.postRedisplay
()
2285 | 14 when not allowdel
-> (* ctrl-n *)
2286 if String.length
qsearch > 0
2288 let optoutlines = narrow outlines qsearch in
2289 begin match optoutlines with
2290 | None
-> state.text <- "can't narrow"
2292 state.mode <- Outline
(
2293 allowdel
, 0, 0, outlines, qsearch, pan
, oldmode
2295 match state.outlines with
2298 state.outlines <- Onarrow
(qsearch, outlines, a)
2299 | Onarrow
(_
, _
, b) ->
2300 state.outlines <- Onarrow
(qsearch, outlines, b)
2303 Glut.postRedisplay
()
2305 | 21 when not allowdel
-> (* ctrl-u *)
2307 match state.outlines with
2310 let a = Array.of_list
(List.rev l
) in
2311 state.outlines <- Oarray
a;
2313 | Onarrow
(_
, _
, b) ->
2314 state.outlines <- Oarray
b;
2318 state.mode <- Outline
(allowdel
, 0, 0, outline, qsearch, pan
, oldmode
);
2319 Glut.postRedisplay
()
2321 | 12 -> (* ctrl-l *)
2322 state.mode <- Outline
2323 (allowdel
, active, firstof active, outlines, qsearch, pan
, oldmode
);
2324 Glut.postRedisplay
()
2326 | 127 when allowdel
-> (* delete *)
2327 let len = Array.length
outlines - 1 in
2331 state.bookmarks <- [];
2334 let bookmarks = Array.init
len
2336 let i = if i >= active then i + 1 else i in
2350 Glut.postRedisplay
()
2352 | _
-> dolog "unknown key %d" key
2355 let keyboard ~key ~x ~
y =
2358 if key
= 7 (* ctrl-g *)
2362 match state.mode with
2363 | Outline
outline -> outlinekeyboard key
outline
2364 | Textentry
textentry -> textentrykeyboard key
textentry
2365 | Birdseye
birdseye -> birdseyekeyboard key
birdseye
2366 | View
-> viewkeyboard key
2367 | Items
items -> itemskeyboard key
items
2370 let birdseyespecial key
((conf, leftx
, _
, hooverpageno
, anchor) as beye
) =
2372 | Glut.KEY_UP
-> upbirdseye beye
2373 | Glut.KEY_DOWN
-> downbirdseye beye
2375 | Glut.KEY_PAGE_UP
->
2376 begin match state.layout with
2380 state.mode <- Birdseye
(
2381 conf, leftx
, l
.pageno, hooverpageno
, anchor
2383 gotopage1 l
.pageno 0;
2386 let layout = layout (state.y-conf.winh
) conf.winh
in
2388 | [] -> gotoy (clamp (-conf.winh
))
2390 state.mode <- Birdseye
(
2391 conf, leftx
, l
.pageno, hooverpageno
, anchor
2393 gotopage1 l
.pageno 0
2396 | [] -> gotoy (clamp (-conf.winh
))
2399 | Glut.KEY_PAGE_DOWN
->
2400 begin match List.rev
state.layout with
2402 let layout = layout (state.y + conf.winh
) conf.winh
in
2403 begin match layout with
2405 let incr = l
.pageh
- l
.pagevh in
2410 conf, leftx
, state.pagecount
- 1, hooverpageno
, anchor
2412 Glut.postRedisplay
();
2414 else gotoy (clamp (incr + conf.interpagespace*2));
2418 Birdseye
(conf, leftx
, l
.pageno, hooverpageno
, anchor);
2419 gotopage1 l
.pageno 0;
2422 | [] -> gotoy (clamp conf.winh
)
2426 state.mode <- Birdseye
(conf, leftx
, 0, hooverpageno
, anchor);
2430 let pageno = state.pagecount
- 1 in
2431 state.mode <- Birdseye
(conf, leftx
, pageno, hooverpageno
, anchor);
2432 if not
(pagevisible state.layout pageno)
2435 match List.rev
state.pdims
with
2437 | (_
, _
, h, _
) :: _
-> h
2439 gotoy (max
0 (getpagey pageno - (conf.winh
- h - conf.interpagespace)))
2440 else Glut.postRedisplay
();
2444 let setautoscrollspeed step goingdown
=
2445 let incr = max
1 ((abs step
) / 2) in
2446 let incr = if goingdown
then incr else -incr in
2447 let astep = step
+ incr in
2448 state.autoscroll
<- Some
astep;
2451 let special ~key ~x ~
y =
2454 match state.mode with
2455 | View
| (Birdseye _
) when key
= Glut.KEY_F9
->
2459 birdseyespecial key vals
2461 | View
when key
= Glut.KEY_F1
->
2465 begin match state.autoscroll
with
2466 | Some step
when key
= Glut.KEY_DOWN
|| key
= Glut.KEY_UP
->
2467 setautoscrollspeed step
(key
= Glut.KEY_DOWN
)
2472 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
2473 | Glut.KEY_UP
-> clamp (-conf.scrollstep
)
2474 | Glut.KEY_DOWN
-> clamp conf.scrollstep
2475 | Glut.KEY_PAGE_UP
->
2476 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2478 match state.layout with
2480 | l
:: _
-> state.y - l
.pagey
2483 | Glut.KEY_PAGE_DOWN
->
2484 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2486 match List.rev
state.layout with
2488 | l
:: _
-> getpagey l
.pageno
2491 | Glut.KEY_HOME
-> addnav (); 0
2494 state.maxy - (if conf.maxhfit
then conf.winh
else 0)
2496 | (Glut.KEY_RIGHT
| Glut.KEY_LEFT
) when
2497 Glut.getModifiers
() land Glut.active_alt
!= 0 ->
2498 getnav (if key
= Glut.KEY_LEFT
then 1 else -1)
2500 | Glut.KEY_RIGHT
when conf.zoom > 1.0 ->
2501 state.x
<- state.x
- 10;
2503 | Glut.KEY_LEFT
when conf.zoom > 1.0 ->
2504 state.x
<- state.x
+ 10;
2509 gotoy_and_clear_text y
2513 ((c, _
, (Some
(action, _
) as onhist), onkey
, ondone), mode) ->
2516 | Glut.KEY_UP
-> action HCprev
2517 | Glut.KEY_DOWN
-> action HCnext
2518 | Glut.KEY_HOME
-> action HCfirst
2519 | Glut.KEY_END
-> action HClast
2522 state.mode <- Textentry
((c, s, onhist, onkey
, ondone), mode);
2523 Glut.postRedisplay
()
2527 | Items
(active, first
, items, qsearch, pan
, oldmode
) ->
2528 let maxrows = maxoutlinerows () in
2529 let itemcount = Array.length
items in
2530 let hasaction = function
2531 | (_
, _
, Noaction
) -> false
2534 let find start
incr =
2536 if i = -1 || i = itemcount
2539 if hasaction items.(i)
2541 else find (i + incr)
2546 let set active first
=
2547 let first = max
0 (min
first (itemcount - maxrows)) in
2548 state.mode <- Items
(active, first, items, qsearch, pan
, oldmode
)
2551 let isvisible first n = n >= first && n - first <= maxrows in
2553 let incr1 = if incr > 0 then 1 else -1 in
2554 if isvisible first active
2557 let next = active + incr in
2559 if next < 0 || next >= itemcount
2561 else find next incr1
2563 if next = -1 || abs
(active - next) > maxrows
2569 let first = first + incr in
2570 let first = max
0 (min
first (itemcount - 1)) in
2572 let next = active + incr in
2573 let next = max
0 (min
next (itemcount - 1)) in
2576 let active = if next = -1 then active else next in
2579 let first = min
next first in
2582 let first = first + incr in
2583 let first = max
0 (min
first (itemcount - 1)) in
2585 let next = active + incr in
2586 let next = max
0 (min
next (itemcount - 1)) in
2587 let next = find next incr1 in
2588 if next = -1 || abs
(active - first) > maxrows
2595 Glut.postRedisplay
()
2597 begin match key
with
2598 | Glut.KEY_UP
-> navigate ~
-1
2599 | Glut.KEY_DOWN
-> navigate 1
2600 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
2601 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
2604 state.mode <- Items
(
2605 active, first, items, qsearch, min
0 (pan
- 1), oldmode
2607 Glut.postRedisplay
()
2610 state.mode <- Items
(
2611 active, first, items, qsearch, min
0 (pan
+ 1), oldmode
2613 Glut.postRedisplay
()
2616 let active = find 0 1 in
2618 Glut.postRedisplay
()
2621 let first = max
0 (itemcount - maxrows) in
2622 let active = find (itemcount - 1) ~
-1 in
2624 Glut.postRedisplay
()
2629 | Outline
(allowdel
, active, first, outlines, qsearch, pan
, oldmode
) ->
2630 let maxrows = maxoutlinerows () in
2631 let calcfirst first active =
2634 let rows = active - first in
2635 if rows > maxrows then active - maxrows else first
2639 let active = active + incr in
2640 let active = max
0 (min
active (Array.length
outlines - 1)) in
2641 let first = calcfirst first active in
2642 state.mode <- Outline
(
2643 allowdel
, active, first, outlines, qsearch, pan
, oldmode
2645 Glut.postRedisplay
()
2647 let updownlevel incr =
2648 let len = Array.length
outlines in
2649 let (_
, curlevel
, _
) = outlines.(active) in
2651 if i = len then i-1 else if i = -1 then 0 else
2652 let (_
, l
, _
) = outlines.(i) in
2653 if l
!= curlevel
then i else flow (i+incr)
2655 let active = flow active in
2656 let first = calcfirst first active in
2657 state.mode <- Outline
(
2658 allowdel
, active, first, outlines, qsearch, pan
, oldmode
2660 Glut.postRedisplay
()
2663 | Glut.KEY_UP
-> navigate ~
-1
2664 | Glut.KEY_DOWN
-> navigate 1
2665 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
2666 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
2669 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2671 state.mode <- Outline
(
2672 allowdel
, active, first, outlines,
2673 qsearch, min
0 (pan
+ 1), oldmode
2675 Glut.postRedisplay
();
2683 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2685 state.mode <- Outline
(
2686 allowdel
, active, first, outlines, qsearch, pan
- 1, oldmode
2688 Glut.postRedisplay
();
2692 then updownlevel ~
-1
2696 state.mode <- Outline
(
2697 allowdel
, 0, 0, outlines, qsearch, pan
, oldmode
2699 Glut.postRedisplay
()
2702 let active = Array.length
outlines - 1 in
2703 let first = max
0 (active - maxrows) in
2704 state.mode <- Outline
(
2705 allowdel
, active, first, outlines, qsearch, pan
, oldmode
2707 Glut.postRedisplay
()
2712 let drawplaceholder l
=
2713 let margin = state.x
+ (conf.winw
- (state.w + state.scrollw
)) / 2 in
2715 (float l
.pagex
, float l
.pagedispy
)
2716 (float (l
.pagew
+ l
.pagex
), float (l
.pagedispy
+ l
.pagevh))
2718 let x = if margin < 0 then -margin else l
.pagex
2719 and y = l
.pagedispy
+ 13 in
2720 GlDraw.color
(0.0, 0.0, 0.0);
2721 drawstring 13 x y ("Loading " ^ string_of_int
(l
.pageno + 1))
2724 let now () = Unix.gettimeofday
();;
2728 match state.mode with
2729 | Textentry _
-> scalecolor 0.4
2730 | View
| Outline _
| Items _
-> scalecolor 1.0
2731 | Birdseye
(_
, _
, pageno, hooverpageno
, _
) ->
2732 if l
.pageno = hooverpageno
2735 if l
.pageno = pageno
2741 begin match getopaque l
.pageno with
2742 | Some
(opaque, _
) when validopaque opaque ->
2744 draw
(l
.pagedispy
, l
.pagevh, l
.pagey, conf.hlinks
) opaque;
2747 vlog "draw %d %f sec" l
.pageno d;
2755 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
2756 let sh = (float (maxy + conf.winh
) /. float conf.winh
) in
2757 let sh = float conf.winh
/. sh in
2758 let sh = max
sh (float conf.scrollh
) in
2761 if state.y = state.maxy
2763 else float y /. float maxy
2765 let position = (float conf.winh
-. sh) *. percent in
2768 if position +. sh > float conf.winh
2769 then float conf.winh
-. sh
2775 let scrollindicator () =
2776 GlDraw.color (0.64 , 0.64, 0.64);
2778 (float (conf.winw
- state.scrollw
), 0.)
2779 (float conf.winw
, float conf.winh
)
2781 GlDraw.color (0.0, 0.0, 0.0);
2783 let position, sh = scrollph state.y in
2785 (float (conf.winw
- state.scrollw
), position)
2786 (float conf.winw
, position +. sh)
2790 let showsel margin =
2791 match state.mstate
with
2792 | Mnone
| Mscroll _
| Mpan _
| Mzoom _
->
2795 | Msel
((x0
, y0
), (x1
, y1
)) ->
2796 let rec loop = function
2798 if (y0
>= l
.pagedispy
&& y0
<= (l
.pagedispy
+ l
.pagevh))
2799 || ((y1
>= l
.pagedispy
&& y1
<= (l
.pagedispy
+ l
.pagevh)))
2801 match getopaque l
.pageno with
2802 | Some
(opaque, _
) when validopaque opaque ->
2803 let oy = -l
.pagey + l
.pagedispy
in
2805 (x0
- margin - state.x, y0
,
2806 x1
- margin - state.x, y1
) oy;
2816 let panx = float state.x in
2818 GlDraw.color (0.0, 0.0, 1.0) ~alpha
:0.5;
2819 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2821 (fun (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) ->
2823 if l
.pageno = pageno
2825 let d = float (l
.pagedispy
- l
.pagey) in
2826 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
2827 GlDraw.begins `quads
;
2829 GlDraw.vertex2
(x0
+.panx, y0
+.d);
2830 GlDraw.vertex2
(x1
+.panx, y1
+.d);
2831 GlDraw.vertex2
(x2
+.panx, y2
+.d);
2832 GlDraw.vertex2
(x3
+.panx, y3
+.d);
2842 let showstrings trusted
active first pan
strings =
2844 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2845 GlDraw.color (0., 0., 0.) ~alpha
:0.85;
2846 GlDraw.rect (0., 0.) (float conf.winw
, float conf.winh
);
2847 GlDraw.color (1., 1., 1.);
2848 Gl.enable `texture_2d
;
2850 let wx = measurestr
15 "w" in
2851 let tabx = 30.0*.wx +. float (pan
*15) in
2853 if row
= Array.length
strings || (row
- first) * 16 > conf.winh
2856 let (s, level
, _
) = strings.(row
) in
2857 let y = (row
- first) * 16 in
2858 let x = 5 + 15*(max
0 (level
+pan
)) in
2861 Gl.disable `texture_2d
;
2862 GlDraw.polygon_mode `both `line
;
2863 GlDraw.color (1., 1., 1.) ~alpha
:0.9;
2864 GlDraw.rect (0., float (y + 1))
2865 (float (conf.winw
- 1), float (y + 18));
2866 GlDraw.polygon_mode `both `fill
;
2867 GlDraw.color (1., 1., 1.);
2868 Gl.enable `texture_2d
;
2871 let drawtabularstring x s =
2875 let tabpos = try String.index
s '
\t'
with Not_found
-> -1 in
2878 let len = String.length
s - tabpos - 1 in
2879 let s1 = String.sub
s 0 tabpos
2880 and s2
= String.sub
s (tabpos + 1) len in
2881 let xx = wx +. drawstring1 14 x (y + 16) s1 in
2882 let x = truncate
(max
xx tabx) in
2883 drawstring1 15 x (y + 16) s2
2885 drawstring1 15 x (y + 16) s
2887 drawstring1 15 x (y + 16) s
2891 drawtabularstring (x + pan
*15) s;
2897 Gl.disable `texture_2d
;
2900 let showoutline (_, active, first, outlines, _, pan
, _) =
2901 showstrings false active first pan
outlines;
2904 let showitems (active, first, items, _, pan
, _) =
2905 showstrings true active first pan
items;
2909 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
2910 GlDraw.viewport
margin 0 state.w conf.winh
;
2912 GlClear.color (scalecolor2 conf.bgcolor
);
2913 GlClear.clear
[`
color];
2916 Gl.enable `scissor_test
;
2917 GlMisc.scissor
0 0 (conf.winw
- state.scrollw
) conf.winh
;
2919 List.iter
drawpage state.layout;
2922 Gl.disable `scissor_test
2926 let x = -.float state.x in
2927 GlMat.translate ~
x ();
2931 GlDraw.viewport
0 0 conf.winw
conf.winh
;
2934 begin match state.mode with
2935 | Items
items -> showitems items
2936 | Outline
outline -> showoutline outline
2940 Glut.swapBuffers
();
2944 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
2945 let x = x - margin - state.x in
2946 let rec f = function
2948 begin match getopaque l
.pageno with
2949 | Some
(opaque, _) when validopaque opaque ->
2950 let y = y - l
.pagedispy
in
2953 let y = l
.pagey + y in
2954 let x = x - l
.pagex
in
2955 match whatsunder
opaque x y with
2968 let viewmouse button bstate
x y =
2970 | Glut.OTHER_BUTTON
n when (n == 3 || n == 4) && bstate
= Glut.UP
->
2971 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2973 match state.mstate
with
2974 | Mzoom
(oldn
, i) ->
2982 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
2984 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
2986 let zoom = conf.zoom +. incr in
2988 state.mstate
<- Mzoom
(n, 0);
2990 state.mstate
<- Mzoom
(n, i+1);
2992 else state.mstate
<- Mzoom
(n, 0)
2994 | _ -> state.mstate
<- Mzoom
(n, 0)
2997 match state.autoscroll
with
2998 | Some step
-> setautoscrollspeed step
(n=4)
3002 then -conf.scrollstep
3003 else conf.scrollstep
3005 let incr = incr * 2 in
3006 let y = clamp incr in
3007 gotoy_and_clear_text y
3010 | Glut.LEFT_BUTTON
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
3011 if bstate
= Glut.DOWN
3013 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
3014 state.mstate
<- Mpan
(x, y)
3017 state.mstate
<- Mnone
3019 | Glut.LEFT_BUTTON
when x > conf.winw
- state.scrollw
->
3020 if bstate
= Glut.DOWN
3022 let position, sh = scrollph state.y in
3023 if y > truncate
position && y < truncate
(position +. sh)
3025 state.mstate
<- Mscroll
3027 let percent = float y /. float conf.winh
in
3028 let desty = truncate
(float (state.maxy - conf.winh
) *. percent) in
3030 state.mstate
<- Mscroll
3032 state.mstate
<- Mnone
3034 | Glut.LEFT_BUTTON
->
3035 let dest = if bstate
= Glut.DOWN
then getunder x y else Unone
in
3036 begin match dest with
3037 | Ulinkgoto
(pageno, top) ->
3041 gotopage1 pageno top;
3047 | Unone
when bstate
= Glut.DOWN
->
3048 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
3049 state.mstate
<- Mpan
(x, y);
3051 | Unone
| Utext
_ ->
3052 if bstate
= Glut.DOWN
3054 if conf.angle
mod 360 = 0
3056 state.mstate
<- Msel
((x, y), (x, y));
3057 Glut.postRedisplay
()
3061 match state.mstate
with
3064 | Mzoom
_ | Mscroll
->
3065 state.mstate
<- Mnone
3068 Glut.setCursor
Glut.CURSOR_INHERIT
;
3069 state.mstate
<- Mnone
3071 | Msel
((_, y0
), (_, y1
)) ->
3073 if (y0
>= l
.pagedispy
&& y0
<= (l
.pagedispy
+ l
.pagevh))
3074 || ((y1
>= l
.pagedispy
&& y1
<= (l
.pagedispy
+ l
.pagevh)))
3076 match getopaque l
.pageno with
3077 | Some
(opaque, _) when validopaque opaque ->
3081 List.iter
f state.layout;
3082 copysel
""; (* ugly *)
3083 Glut.setCursor
Glut.CURSOR_INHERIT
;
3084 state.mstate
<- Mnone
;
3091 let birdseyemouse button bstate
x y
3092 (conf, leftx
, _, hooverpageno
, anchor) =
3094 | Glut.LEFT_BUTTON
when bstate
= Glut.UP
->
3095 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
3096 let rec loop = function
3099 if y > l
.pagedispy
&& y < l
.pagedispy
+ l
.pagevh
3100 && x > margin && x < margin + l
.pagew
3102 leavebirdseye (conf, leftx
, l
.pageno, hooverpageno
, anchor) false;
3107 | Glut.OTHER_BUTTON
_ -> viewmouse button bstate
x y
3111 let mouse bstate button
x y =
3112 match state.mode with
3113 | View
-> viewmouse button bstate
x y
3114 | Birdseye beye
-> birdseyemouse button bstate
x y beye
3115 | Textentry
_ | Outline
_ | Items
_ -> ()
3118 let mouse ~button ~
state ~
x ~
y = mouse state button
x y;;
3121 match state.mode with
3124 match state.mstate
with
3125 | Mzoom
_ | Mnone
-> ()
3130 state.mstate
<- Mpan
(x, y);
3131 if conf.zoom > 1.0 then state.x <- state.x + dx;
3133 gotoy_and_clear_text y
3136 state.mstate
<- Msel
(a, (x, y));
3137 Glut.postRedisplay
()
3140 let y = min
conf.winh
(max
0 y) in
3141 let percent = float y /. float conf.winh
in
3142 let y = truncate
(float (state.maxy - conf.winh
) *. percent) in
3143 gotoy_and_clear_text y
3147 match state.mode with
3148 | Birdseye
(conf, leftx
, pageno, hooverpageno
, anchor) ->
3149 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
3150 let rec loop = function
3152 if hooverpageno
!= -1
3154 state.mode <- Birdseye
(conf, leftx
, pageno, -1, anchor);
3155 Glut.postRedisplay
();
3158 if y > l
.pagedispy
&& y < l
.pagedispy
+ l
.pagevh
3159 && x > margin && x < margin + l
.pagew
3161 state.mode <- Birdseye
(conf, leftx
, pageno, l
.pageno, anchor);
3162 Glut.postRedisplay
();
3168 | Outline
_ | Items
_ | Textentry
_ -> ()
3170 match state.mstate
with
3172 begin match getunder x y with
3173 | Unone
-> Glut.setCursor
Glut.CURSOR_INHERIT
3175 if conf.underinfo
then showtext 'u'
("ri: " ^ uri
);
3176 Glut.setCursor
Glut.CURSOR_INFO
3177 | Ulinkgoto
(page
, _) ->
3179 then showtext 'p'
("age: " ^ string_of_int page
);
3180 Glut.setCursor
Glut.CURSOR_INFO
3182 if conf.underinfo
then showtext '
f'
("ont: " ^
s);
3183 Glut.setCursor
Glut.CURSOR_TEXT
3186 | Mpan
_ | Msel
_ | Mzoom
_ | Mscroll
->
3197 match Sys.os_type
with
3198 | "Win32" -> Sys.getenv
"HOMEPATH"
3199 | _ -> Sys.getenv
"HOME"
3202 ("Can not determine home directory location: " ^
3203 Printexc.to_string exn
);
3207 let config_of c attrs
=
3211 | "scroll-bar-width" -> { c with scrollbw
= max
0 (int_of_string
v) }
3212 | "scroll-handle-height" -> { c with scrollh
= max
0 (int_of_string
v) }
3213 | "case-insensitive-search" -> { c with icase
= bool_of_string
v }
3214 | "preload" -> { c with preload = bool_of_string
v }
3215 | "page-bias" -> { c with pagebias
= int_of_string
v }
3216 | "scroll-step" -> { c with scrollstep
= max
1 (int_of_string
v) }
3217 | "auto-scroll-step" ->
3218 { c with autoscrollstep
= max
0 (int_of_string
v) }
3219 | "max-height-fit" -> { c with maxhfit
= bool_of_string
v }
3220 | "crop-hack" -> { c with crophack
= bool_of_string
v }
3221 | "throttle" -> { c with showall
= bool_of_string
v }
3222 | "highlight-links" -> { c with hlinks
= bool_of_string
v }
3223 | "under-cursor-info" -> { c with underinfo
= bool_of_string
v }
3224 | "vertical-margin" ->
3225 { c with interpagespace = max
0 (int_of_string
v) }
3227 let zoom = float_of_string
v /. 100. in
3228 let zoom = max
0.01 zoom in
3229 { c with zoom = zoom }
3230 | "presentation" -> { c with presentation = bool_of_string
v }
3231 | "rotation-angle" -> { c with angle
= int_of_string
v }
3232 | "width" -> { c with winw
= max
20 (int_of_string
v) }
3233 | "height" -> { c with winh
= max
20 (int_of_string
v) }
3234 | "persistent-bookmarks" -> { c with savebmarks
= bool_of_string
v }
3235 | "proportional-display" -> { c with proportional
= bool_of_string
v }
3236 | "pixmap-cache-size" -> { c with memlimit
= max
2 (int_of_string
v) }
3237 | "tex-count" -> { c with texcount
= max
1 (int_of_string
v) }
3238 | "slice-height" -> { c with sliceheight
= max
2 (int_of_string
v) }
3239 | "max-tex-width" -> { c with blockwidth
= max
2 (int_of_string
v) }
3240 | "thumbnail-width" -> { c with thumbw
= max
2 (int_of_string
v) }
3241 | "persistent-location" -> { c with jumpback
= bool_of_string
v }
3242 | "background-color" -> { c with bgcolor
= color_of_string v }
3243 | "scrollbar-in-presentation" ->
3244 { c with scrollbarinpm
= bool_of_string
v }
3245 | "ui-font" -> { c with uifont
= v }
3248 prerr_endline
("Error processing attribute (`" ^
3249 k ^
"'=`" ^
v ^
"'): " ^
Printexc.to_string exn
);
3252 let rec fold c = function
3255 let c = apply c k
v in
3261 let fromstring f pos
n v d =
3264 dolog "Error processing attribute (%S=%S) at %d\n%s"
3265 n v pos
(Printexc.to_string exn
)
3270 let bookmark_of attrs
=
3271 let rec fold title page rely
= function
3272 | ("title", v) :: rest
-> fold v page rely rest
3273 | ("page", v) :: rest
-> fold title v rely rest
3274 | ("rely", v) :: rest
-> fold title page
v rest
3275 | _ :: rest
-> fold title page rely rest
3276 | [] -> title, page
, rely
3278 fold "invalid" "0" "0" attrs
3282 let rec fold path page rely pan
= function
3283 | ("path", v) :: rest
-> fold v page rely pan rest
3284 | ("page", v) :: rest
-> fold path
v rely pan rest
3285 | ("rely", v) :: rest
-> fold path page
v pan rest
3286 | ("pan", v) :: rest
-> fold path page rely
v rest
3287 | _ :: rest
-> fold path page rely pan rest
3288 | [] -> path
, page
, rely
, pan
3290 fold "" "0" "0" "0" attrs
3293 let setconf dst src
=
3294 dst
.scrollbw
<- src
.scrollbw
;
3295 dst
.scrollh
<- src
.scrollh
;
3296 dst
.icase
<- src
.icase
;
3297 dst
.preload <- src
.preload;
3298 dst
.pagebias
<- src
.pagebias
;
3299 dst
.verbose
<- src
.verbose
;
3300 dst
.scrollstep
<- src
.scrollstep
;
3301 dst
.maxhfit
<- src
.maxhfit
;
3302 dst
.crophack
<- src
.crophack
;
3303 dst
.autoscrollstep
<- src
.autoscrollstep
;
3304 dst
.showall
<- src
.showall
;
3305 dst
.hlinks
<- src
.hlinks
;
3306 dst
.underinfo
<- src
.underinfo
;
3307 dst
.interpagespace <- src
.interpagespace;
3308 dst
.zoom <- src
.zoom;
3309 dst
.presentation <- src
.presentation;
3310 dst
.angle
<- src
.angle
;
3311 dst
.winw
<- src
.winw
;
3312 dst
.winh
<- src
.winh
;
3313 dst
.savebmarks
<- src
.savebmarks
;
3314 dst
.memlimit
<- src
.memlimit
;
3315 dst
.proportional
<- src
.proportional
;
3316 dst
.texcount
<- src
.texcount
;
3317 dst
.sliceheight
<- src
.sliceheight
;
3318 dst
.blockwidth
<- src
.blockwidth
;
3319 dst
.thumbw
<- src
.thumbw
;
3320 dst
.jumpback
<- src
.jumpback
;
3321 dst
.bgcolor
<- src
.bgcolor
;
3322 dst
.scrollbarinpm
<- src
.scrollbarinpm
;
3323 dst
.uifont
<- src
.uifont
;
3327 let l = String.length
s in
3328 let b = Buffer.create
l in
3334 let h = Hashtbl.create
10 in
3335 let dc = { defconf with angle
= defconf.angle
} in
3336 let rec toplevel v t spos
_ =
3338 | Vdata
| Vcdata
| Vend
-> v
3339 | Vopen
("llppconfig", _, closed
) ->
3342 else { v with f = llppconfig
}
3344 error
"unexpected subelement at top level" s spos
3345 | Vclose
_ -> error
"unexpected close at top level" s spos
3347 and llppconfig
v t spos
_ =
3349 | Vdata
| Vcdata
| Vend
-> v
3350 | Vopen
("defaults", attrs
, closed
) ->
3351 let c = config_of dc attrs
in
3355 else { v with f = skip
"defaults" (fun () -> v) }
3357 | Vopen
("doc", attrs
, closed
) ->
3358 let pathent, spage
, srely
, span
= doc_of attrs
in
3359 let path = unent pathent
3360 and pageno = fromstring int_of_string spos
"page" spage
0
3361 and rely
= fromstring float_of_string spos
"rely" srely
0.0
3362 and pan
= fromstring int_of_string spos
"pan" span
0 in
3363 let c = config_of dc attrs
in
3364 let anchor = (pageno, rely
) in
3366 then (Hashtbl.add
h path (c, [], pan
, anchor); v)
3367 else { v with f = doc
path pan
anchor c [] }
3370 error
"unexpected subelement in llppconfig" s spos
3372 | Vclose
"llppconfig" -> { v with f = toplevel }
3373 | Vclose
_ -> error
"unexpected close in llppconfig" s spos
3375 and doc
path pan
anchor c bookmarks v t spos
_ =
3377 | Vdata
| Vcdata
-> v
3378 | Vend
-> error
"unexpected end of input in doc" s spos
3379 | Vopen
("bookmarks", _, closed
) ->
3382 else { v with f = pbookmarks
path pan
anchor c bookmarks }
3384 | Vopen
(_, _, _) ->
3385 error
"unexpected subelement in doc" s spos
3388 Hashtbl.add
h path (c, List.rev
bookmarks, pan
, anchor);
3389 { v with f = llppconfig
}
3391 | Vclose
_ -> error
"unexpected close in doc" s spos
3393 and pbookmarks
path pan
anchor c bookmarks v t spos
_ =
3395 | Vdata
| Vcdata
-> v
3396 | Vend
-> error
"unexpected end of input in bookmarks" s spos
3397 | Vopen
("item", attrs
, closed
) ->
3398 let titleent, spage
, srely
= bookmark_of attrs
in
3399 let page = fromstring int_of_string spos
"page" spage
0
3400 and rely
= fromstring float_of_string spos
"rely" srely
0.0 in
3401 let bookmarks = (unent titleent, 0, (page, rely
)) :: bookmarks in
3403 then { v with f = pbookmarks
path pan
anchor c bookmarks }
3406 { v with f = skip
"item" f }
3409 error
"unexpected subelement in bookmarks" s spos
3411 | Vclose
"bookmarks" ->
3412 { v with f = doc
path pan
anchor c bookmarks }
3414 | Vclose
_ -> error
"unexpected close in bookmarks" s spos
3416 and skip tag
f v t spos
_ =
3418 | Vdata
| Vcdata
-> v
3420 error
("unexpected end of input in skipped " ^ tag
) s spos
3421 | Vopen
(tag'
, _, closed
) ->
3425 let f'
() = { v with f = skip tag
f } in
3426 { v with f = skip tag'
f'
}
3430 else error
("unexpected close in skipped " ^ tag
) s spos
3433 parse
{ f = toplevel; accu = () } s;
3439 let len = in_channel_length ic
in
3440 let s = String.create
len in
3441 really_input ic
s 0 len;
3444 | Parse_error
(msg
, s, pos
) ->
3445 let subs = subs s pos
in
3446 let s = Printf.sprintf
"%s: at %d [..%s..]" msg pos
subs in
3447 failwith
("parse error: " ^
s)
3450 failwith
("config load error: " ^
Printexc.to_string exn
)
3456 let dir = Filename.concat
home ".config" in
3457 if Sys.is_directory
dir then dir else home
3460 Filename.concat
dir "llpp.conf"
3464 if Sys.file_exists
path
3467 (try Some
(open_in_bin
path)
3470 ("Error opening configuation file `" ^
path ^
"': " ^
3471 Printexc.to_string exn
);
3480 ("Error loading configuation from `" ^
path ^
"': " ^
3481 Printexc.to_string exn
);
3487 f (Hashtbl.create
0, defconf)
3492 let pc, pb
, px
, pa
=
3494 Hashtbl.find h (Filename.basename
state.path)
3495 with Not_found
-> dc, [], 0, (0, 0.0)
3499 state.bookmarks <- pb
;
3501 state.scrollw
<- conf.scrollbw
;
3503 then state.anchor <- pa
;
3504 cbput state.hists
.nav pa
;
3509 let add_attrs bb always
dc c =
3512 then Printf.bprintf bb
"\n %s='%b'" s a
3515 then Printf.bprintf bb
"\n %s='%d'" s a
3518 then Printf.bprintf bb
"\n %s='%f'" s (a*.100.)
3522 Printf.bprintf bb
"\n %s='%s'" s (color_to_string a)
3526 Printf.bprintf bb
"\n %s='%s'" s a
3530 then dc.winw
, dc.winh
3532 match state.fullscreen
with
3534 | None
-> c.winw
, c.winh
3536 let zoom, presentation, interpagespace, showall
=
3538 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
3540 match state.mode with
3541 | Birdseye
(bc
, _, _, _, _) ->
3542 bc
.zoom, bc
.presentation, bc
.interpagespace, bc
.showall
3543 | _ -> c.zoom, c.presentation, c.interpagespace, c.showall
3545 oi
"width" w dc.winw
;
3546 oi
"height" h dc.winh
;
3547 oi
"scroll-bar-width" c.scrollbw
dc.scrollbw
;
3548 oi
"scroll-handle-height" c.scrollh
dc.scrollh
;
3549 ob "case-insensitive-search" c.icase
dc.icase
;
3550 ob "preload" c.preload dc.preload;
3551 oi
"page-bias" c.pagebias
dc.pagebias
;
3552 oi
"scroll-step" c.scrollstep
dc.scrollstep
;
3553 oi
"auto-scroll-step" c.autoscrollstep
dc.autoscrollstep
;
3554 ob "max-height-fit" c.maxhfit
dc.maxhfit
;
3555 ob "crop-hack" c.crophack
dc.crophack
;
3556 ob "throttle" showall
dc.showall
;
3557 ob "highlight-links" c.hlinks
dc.hlinks
;
3558 ob "under-cursor-info" c.underinfo
dc.underinfo
;
3559 oi
"vertical-margin" interpagespace dc.interpagespace;
3560 oz
"zoom" zoom dc.zoom;
3561 ob "presentation" presentation dc.presentation;
3562 oi
"rotation-angle" c.angle
dc.angle
;
3563 ob "persistent-bookmarks" c.savebmarks
dc.savebmarks
;
3564 ob "proportional-display" c.proportional
dc.proportional
;
3565 oi
"pixmap-cache-size" c.memlimit
dc.memlimit
;
3566 oi
"tex-count" c.texcount
dc.texcount
;
3567 oi
"slice-height" c.sliceheight
dc.sliceheight
;
3568 oi
"max-tex-width" c.blockwidth
dc.blockwidth
;
3569 oi
"thumbnail-width" c.thumbw
dc.thumbw
;
3570 ob "persistent-location" c.jumpback
dc.jumpback
;
3571 oc
"background-color" c.bgcolor
dc.bgcolor
;
3572 ob "scrollbar-in-presentation" c.scrollbarinpm
dc.scrollbarinpm
;
3573 os
"ui-font" c.uifont
dc.uifont
;
3577 let bb = Buffer.create
32768 in
3579 let dc = if conf.bedefault
then conf else dc in
3580 Buffer.add_string
bb "<llppconfig>\n<defaults ";
3581 add_attrs bb true dc dc;
3582 Buffer.add_string
bb "/>\n";
3584 let adddoc path pan
anchor c bookmarks =
3585 if bookmarks == [] && c = dc && anchor = emptyanchor
3588 Printf.bprintf
bb "<doc path='%s'"
3589 (enent
path 0 (String.length
path));
3591 if anchor <> emptyanchor
3593 let n, y = anchor in
3594 Printf.bprintf
bb " page='%d'" n;
3597 Printf.bprintf
bb " rely='%f'" y
3602 then Printf.bprintf
bb " pan='%d'" pan
;
3604 add_attrs bb false dc c;
3606 begin match bookmarks with
3607 | [] -> Buffer.add_string
bb "/>\n"
3609 Buffer.add_string
bb ">\n<bookmarks>\n";
3610 List.iter
(fun (title, _level
, (page, rely
)) ->
3612 "<item title='%s' page='%d'"
3613 (enent
title 0 (String.length
title))
3618 Printf.bprintf
bb " rely='%f'" rely
3620 Buffer.add_string
bb "/>\n";
3622 Buffer.add_string
bb "</bookmarks>\n</doc>\n";
3628 match state.mode with
3629 | Birdseye
(_, pan, _, _, _) -> pan
3632 let basename = Filename.basename state.path in
3633 adddoc basename pan (getanchor ())
3636 match state.autoscroll
with
3638 | None
-> conf.autoscrollstep
}
3639 (if conf.savebmarks
then state.bookmarks else []);
3641 Hashtbl.iter
(fun path (c, bookmarks, x, y) ->
3643 then adddoc path x y c bookmarks
3645 Buffer.add_string
bb "</llppconfig>";
3648 if Buffer.length
bb > 0
3651 let tmp = path ^
".tmp" in
3652 let oc = open_out_bin
tmp in
3653 Buffer.output_buffer
oc bb;
3655 Sys.rename
tmp path;
3658 ("error while saving configuration: " ^
Printexc.to_string exn
)
3665 [("-p", Arg.String
(fun s -> state.password
<- s) , " Set password")
3666 ;("-v", Arg.Unit
(fun () -> print_endline
Help.version
; exit
0),
3667 " Print version and exit")]
3669 (fun s -> state.path <- s)
3670 ("Usage: " ^
Sys.argv
.(0) ^
" [options] some.pdf\nOptions:")
3672 if String.length
state.path = 0
3673 then (prerr_endline
"file name missing"; exit
1);
3677 let _ = Glut.init
Sys.argv
in
3678 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
3679 let () = Glut.initWindowSize
conf.winw
conf.winh
in
3680 let _ = Glut.createWindow
("llpp " ^
Filename.basename state.path) in
3683 if Sys.os_type
= "Unix"
3685 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
3687 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
3688 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
3689 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
3690 Unix.bind
sock addr;
3692 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
3693 Unix.connect
csock addr;
3694 let ssock, _ = Unix.accept
sock in
3697 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
3698 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
3702 at_exit
(fun () -> Unix.shutdown
ssock Unix.SHUTDOWN_ALL
);
3706 let () = Glut.displayFunc
display in
3707 let () = Glut.reshapeFunc
reshape in
3708 let () = Glut.keyboardFunc
keyboard in
3709 let () = Glut.specialFunc
special in
3710 let () = Glut.idleFunc
(Some
idle) in
3711 let () = Glut.mouseFunc
mouse in
3712 let () = Glut.motionFunc
motion in
3713 let () = Glut.passiveMotionFunc
pmotion in
3716 conf.angle
, conf.proportional
, conf.texcount
,
3717 conf.sliceheight
, conf.blockwidth
, conf.uifont
3719 state.csock <- csock;
3720 state.ssock <- ssock;
3721 state.text <- "Opening " ^
state.path;
3722 writeopen state.path state.password
;
3726 let rec handlelablglutbug () =
3729 with Glut.BadEnum
"key in special_of_int" ->
3730 showtext '
!'
" LablGlut bug: special key not recognized";
3731 handlelablglutbug ()
3733 handlelablglutbug ();