4 | Ulinkgoto
of (int * int)
6 and facename
= string;;
8 let dolog fmt
= Printf.kprintf prerr_endline fmt
;;
10 type params
= angle
* proportional
* texcount
* sliceheight
* fontpath
19 and proportional
= bool
20 and interpagespace
= int
28 external init
: Unix.file_descr
-> params
-> unit = "ml_init";;
29 external draw
: (int * int * int * int * bool) -> string -> unit = "ml_draw";;
30 external seltext
: string -> (int * int * int * int) -> int -> unit =
32 external copysel
: string -> unit = "ml_copysel";;
33 external getpdimrect
: int -> float array
= "ml_getpdimrect";;
34 external whatsunder
: string -> int -> int -> under
= "ml_whatsunder";;
35 external zoomforh
: int -> int -> int -> float = "ml_zoom_for_height";;
36 external drawstr
: int -> int -> int -> string -> float = "ml_draw_string";;
37 external measurestr
: int -> string -> float = "ml_measure_string";;
41 | Msel
of (mpos
* mpos
)
44 | Mzoom
of (int * int)
48 type textentry
= string * string * onhist
option * onkey
* ondone
49 and onkey
= string -> int -> te
50 and ondone
= string -> unit
51 and histcancel
= unit -> unit
52 and onhist
= ((histcmd
-> string) * histcancel
)
53 and histcmd
= HCnext
| HCprev
| HCfirst
| HClast
58 | TEswitch
of textentry
70 { store
= Array.create n v
77 let cbcap b
= Array.length b
.store
;;
82 b
.wc
<- (b
.wc
+ 1) mod cap;
84 b
.len
<- min
(b
.len
+ 1) cap;
87 let cbempty b
= b
.len
= 0;;
89 let cbgetg b circular dir
=
93 let rc = b
.rc + dir
in
105 else max
0 (min
rc (b
.len
-1))
111 let cbget b
= cbgetg b
false;;
112 let cbgetc b
= cbgetg b
true;;
115 let rc = b
.wc
- b
.len
in
116 let rc = if rc < 0 then cbcap b
+ rc else rc in
120 let cbdecr b
= b
.len
<- b
.len
- 1;;
135 { mutable scrollbw
: int
136 ; mutable scrollh
: int
137 ; mutable icase
: bool
138 ; mutable preload
: bool
139 ; mutable pagebias
: int
140 ; mutable verbose
: bool
141 ; mutable scrollstep
: int
142 ; mutable maxhfit
: bool
143 ; mutable crophack
: bool
144 ; mutable autoscrollstep
: int
145 ; mutable showall
: bool
146 ; mutable hlinks
: bool
147 ; mutable underinfo
: bool
148 ; mutable interpagespace
: interpagespace
149 ; mutable zoom
: float
150 ; mutable presentation
: bool
151 ; mutable angle
: angle
154 ; mutable savebmarks
: bool
155 ; mutable proportional
: proportional
156 ; mutable memlimit
: int
157 ; mutable texcount
: texcount
158 ; mutable sliceheight
: sliceheight
159 ; mutable thumbw
: width
160 ; mutable jumpback
: bool
161 ; mutable bgcolor
: float * float * float
162 ; mutable bedefault
: bool
163 ; mutable scrollbarinpm
: bool
164 ; mutable uifont
: string
168 type anchor
= pageno
* top
;;
170 type outline
= string * int * anchor
172 | Oarray
of outline array
173 | Olist
of outline list
174 | Onarrow
of string * outline array
* outline array
177 type rect
= float * float * float * float * float * float * float * float;;
179 type pagemapkey
= pageno
* width
* angle
* proportional
* gen
;;
181 let emptyanchor = (0, 0.0);;
184 | Birdseye
of (conf
* leftx
* pageno
* pageno
* anchor
)
185 | Outline
of (bool * int * int * outline array
* string * int * mode
)
186 | Items
of (int * int * item array
* string * int * mode
)
187 | Textentry
of (textentry
* onleave
)
189 and onleave
= leavetextentrystatus
-> unit
190 and leavetextentrystatus
= | Cancel
| Confirm
191 and item
= string * int * action
194 | Action
of (int -> int -> string -> int -> mode
)
197 let isbirdseye = function Birdseye _
-> true | _
-> false;;
198 let istextentry = function Textentry _
-> true | _
-> false;;
201 { mutable csock
: Unix.file_descr
202 ; mutable ssock
: Unix.file_descr
206 ; mutable scrollw
: int
207 ; mutable anchor
: anchor
209 ; mutable layout
: layout list
210 ; pagemap
: (pagemapkey
, (opaque
* pixmapsize
)) Hashtbl.t
211 ; mutable pdims
: (pageno
* width
* height
* leftx
) list
212 ; mutable pagecount
: int
213 ; pagecache
: string circbuf
214 ; mutable rendering
: bool
215 ; mutable mstate
: mstate
216 ; mutable searchpattern
: string
217 ; mutable rects
: (pageno
* recttype
* rect
) list
218 ; mutable rects1
: (pageno
* recttype
* rect
) list
219 ; mutable text
: string
220 ; mutable fullscreen
: (width
* height
) option
221 ; mutable mode
: mode
222 ; mutable outlines
: outlines
223 ; mutable bookmarks
: outline list
224 ; mutable path
: string
225 ; mutable password
: string
226 ; mutable invalidated
: int
227 ; mutable colorscale
: float
228 ; mutable memused
: int
230 ; mutable throttle
: layout list
option
231 ; mutable ascrollstep
: int
232 ; mutable help
: item array
233 ; mutable docinfo
: (int * string) list
234 ; mutable deadline
: float
238 { pat
: string circbuf
239 ; pag
: string circbuf
240 ; nav
: anchor circbuf
254 ; autoscrollstep
= 24
260 ; presentation
= false
265 ; proportional
= true
266 ; memlimit
= 32*1024*1024
271 ; bgcolor
= (0.5, 0.5, 0.5)
273 ; scrollbarinpm
= true
278 let conf = { defconf with angle
= defconf.angle
};;
281 let strings = ("llpp version " ^
Help.version
) :: "" :: Help.keys
in
282 Array.of_list
(List.map
(fun s
-> s
, 0, Noaction
) strings);
292 ; anchor
= emptyanchor
295 ; pagemap
= Hashtbl.create
10
296 ; pagecache
= cbnew 100 ""
307 ; outlines
= Olist
[]
313 { nav
= cbnew 100 (0, 0.0)
331 Printf.kprintf prerr_endline fmt
333 Printf.kprintf ignore fmt
337 let len = String.length s
in
339 let b = Buffer.create
n in
340 Buffer.add_char
b (Char.chr
((len lsr 24) land 0xff));
341 Buffer.add_char
b (Char.chr
((len lsr 16) land 0xff));
342 Buffer.add_char
b (Char.chr
((len lsr 8) land 0xff));
343 Buffer.add_char
b (Char.chr
((len lsr 0) land 0xff));
344 Buffer.add_string
b s
;
345 let s'
= Buffer.contents
b in
346 let n'
= Unix.write fd
s'
0 n in
347 if n'
!= n then failwith
"write failed";
352 let n = Unix.read fd
s 0 4 in
353 if n != 4 then failwith
"incomplete read(len)";
355 lor (Char.code
s.[0] lsl 24)
356 lor (Char.code
s.[1] lsl 16)
357 lor (Char.code
s.[2] lsl 8)
358 lor (Char.code
s.[3] lsl 0)
360 let s = String.create
len in
361 let n = Unix.read fd
s 0 len in
362 if n != len then failwith
"incomplete read(data)";
367 let b = Buffer.create
10 in
368 Buffer.add_string
b s;
369 let rec combine = function
372 Buffer.add_char
b ' '
;
375 | `
b b -> if b then "1" else "0"
377 | `i i
-> string_of_int i
378 | `f f
-> string_of_float f
379 | `I f
-> string_of_int
(truncate f
)
381 Buffer.add_string
b s;
388 let cmd = Buffer.contents
(makecmd s l
) in
389 writecmd state.csock
cmd;
395 let d = conf.winh
- h
in
402 let rec f pn ph pi fh l
=
404 | (n, _
, h
, _
) :: rest
->
405 let ips = calcips h
in
410 if isbirdseye state.mode
&& pn
= 0
415 let fh = fh + ((n - pn
) * (ph
+ pi
)) in
420 if conf.presentation
|| (isbirdseye state.mode
&& pn
= 0)
424 let fh = fh + ((state.pagecount
- pn
) * (ph
+ pi
)) + inc in
427 let fh = f 0 0 0 0 state.pdims
in
431 let getpageyh pageno
=
432 let rec f pn ph pi y l
=
434 | (n, _
, h
, _
) :: rest
->
435 let ips = calcips h
in
438 let h = if n = pageno
then h else ph
in
439 if conf.presentation
&& n = pageno
441 y
+ (pageno
- pn
) * (ph
+ pi
) + pi
, h
443 y
+ (pageno
- pn
) * (ph
+ pi
), h
445 let y = y + (if conf.presentation
then pi
else 0) in
446 let y = y + (n - pn
) * (ph
+ pi
) in
450 y + (pageno
- pn
) * (ph
+ pi
), ph
452 f 0 0 0 0 state.pdims
455 let getpagey pageno
= fst
(getpageyh pageno
);;
458 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~cacheleft ~accu
=
459 let ((w
, h, ips, x
) as curr
), rest
, pdimno
, yinc
=
461 | (pageno'
, w
, h, x
) :: rest
when pageno'
= pageno
->
462 let ips = calcips h in
464 if conf.presentation
|| (isbirdseye state.mode
&& pageno
= 0)
468 (w
, h, ips, x
), rest
, pdimno
+ 1, yinc
470 prev
, pdims
, pdimno
, 0
472 let dy = dy + yinc in
473 let py = py + yinc in
474 if pageno
= state.pagecount
|| cacheleft
= 0 || dy >= sh
479 if py + h <= vy - yinc
481 let py = py + h + ips in
482 let dy = max
0 (py - y) in
492 let pagey = vy - py in
493 let pagevh = h - pagey in
494 let pagevh = min
(sh
- dy) pagevh in
495 let off = if yinc > 0 then py - vy else 0 in
496 let py = py + h + ips in
502 ; pagedispy
= dy + off
503 ; pagey = pagey + off
504 ; pagevh = pagevh - off
508 let accu = e :: accu in
515 ~cacheleft
:(cacheleft
-1)
518 if state.invalidated
= 0
528 ~cacheleft
:(cbcap state.pagecache
)
538 let y = state.y + incr
in
540 let y = min
y (state.maxy
- (if conf.maxhfit
then conf.winh
else 0)) in
544 let getopaque pageno
=
545 try Some
(Hashtbl.find
state.pagemap
546 (pageno
, state.w
, conf.angle
, conf.proportional
, state.gen
))
547 with Not_found
-> None
550 let cache pageno opaque
=
551 Hashtbl.replace
state.pagemap
552 (pageno
, state.w
, conf.angle
, conf.proportional
, state.gen
) opaque
555 let validopaque opaque
= String.length opaque
> 0;;
558 match getopaque l
.pageno
with
559 | None
when not
state.rendering
->
560 state.rendering
<- true;
561 cache l
.pageno
("", -1);
562 wcmd "render" [`i
(l
.pageno
+ 1)
569 let loadlayout layout =
570 let rec f all
= function
572 begin match getopaque l
.pageno
with
573 | None
-> render l
; f false ls
574 | Some
(opaque
, _
) -> f (all
&& validopaque opaque
) ls
578 f (layout <> []) layout;
581 let findpageforopaque opaque
=
583 (fun k
(v
, s) a
-> if v
= opaque
then Some
(k
, s) else a
)
587 let pagevisible layout n = List.exists
(fun l
-> l
.pageno
= n) layout;;
593 let memleft = conf.memlimit
- state.memused
in
596 let opaque = cbpeek state.pagecache
in
597 match findpageforopaque opaque with
598 | Some
((n, _
, _
, _
, _
), size
) ->
599 memleft + size
>= 0 && not
(pagevisible state.layout n)
606 let presentation = conf.presentation in
607 let interpagespace = conf.interpagespace in
608 let maxy = state.maxy in
609 conf.presentation <- false;
610 conf.interpagespace <- 0;
611 state.maxy <- calcheight ();
613 match state.layout with
615 | l
:: _
-> getpagey l
.pageno
+ l
.pagey
617 let y = if y < conf.winh
then 0 else y - conf.winh
in
618 let pages = layout y (conf.winh
*3) in
619 List.iter
render pages;
620 conf.presentation <- presentation;
621 conf.interpagespace <- interpagespace;
627 let y = min
state.maxy y in
628 let pages = layout y conf.winh
in
629 let ready = loadlayout pages in
635 state.layout <- pages;
636 state.throttle
<- None
;
637 Glut.postRedisplay
();
640 state.throttle
<- Some
pages;
645 state.layout <- pages;
646 state.throttle
<- None
;
647 Glut.postRedisplay
();
649 begin match state.mode
with
650 | Birdseye
(conf, leftx
, pageno
, hooverpageno
, anchor
) ->
651 if not
(pagevisible pages pageno
)
653 match state.layout with
656 state.mode
<- Birdseye
(conf, leftx
, l
.pageno
, hooverpageno
, anchor
)
663 let gotoy_and_clear_text y =
665 if not
conf.verbose
then state.text
<- "";
669 match state.layout with
671 | l
:: _
-> (l
.pageno
, float l
.pagey /. float l
.pageh
)
674 let getanchory (n, top
) =
675 let y, h = getpageyh n in
676 y + (truncate
(top
*. float h));
679 let gotoanchor anchor
=
680 gotoy (getanchory anchor
);
684 cbput state.hists
.nav
(getanchor ());
688 let anchor = cbgetc state.hists
.nav dir
in
693 let y, h = getpageyh n in
694 gotoy_and_clear_text (y + (truncate
(top
*. float h)));
697 let gotopage1 n top
=
698 let y = getpagey n in
699 gotoy_and_clear_text (y + top
);
707 state.invalidated
<- state.invalidated
+ 1;
711 let c = c *. state.colorscale
in
715 let scalecolor2 (r
, g
, b) =
716 (r
*. state.colorscale
, g
*. state.colorscale
, b *. state.colorscale
);
720 state.maxy <- calcheight ();
721 match state.mode
with
722 | Birdseye
(_
, _
, pageno
, _
, _
) ->
723 let y, h = getpageyh pageno
in
724 let top = (conf.winh
- h) / 2 in
725 gotoy (max
0 (y - top))
726 | _
-> gotoanchor state.anchor
730 GlMat.mode `projection
;
731 GlMat.load_identity
();
732 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
733 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
734 GlMat.scale3
(2.0 /. float state.w
, 2.0 /. float conf.winh
, 1.0);
737 GlMat.translate ~x
:(float state.x
) ();
742 GlMat.mode `projection
;
743 GlMat.load_identity
();
744 GlMat.rotate ~x
:1.0 ~angle
:180.0 ();
745 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
746 GlMat.scale3
(2.0 /. float conf.winw
, 2.0 /. float conf.winh
, 1.0);
750 let firsttime = ref true in
752 if state.invalidated
= 0 && not
!firsttime
753 then (state.anchor <- getanchor (); firsttime := false);
756 let w = truncate
(float w *. conf.zoom
) - state.scrollw
in
760 GlMat.mode `modelview
;
761 GlMat.load_identity
();
762 GlClear.color
(scalecolor 1.0);
763 GlClear.clear
[`color
];
766 wcmd "geometry" [`i
w; `i
h];
769 let drawstring size x
y s =
771 Gl.enable `texture_2d
;
772 ignore
(drawstr size x
y s);
774 Gl.disable `texture_2d
;
777 let drawstring1 size x
y s =
782 let len = String.length
state.text
in
784 GlDraw.color
(0.0, 0.0, 0.0);
786 (0.0, float (conf.winh
- 18))
787 (float (conf.winw
- state.scrollw
- 1), float conf.winh
)
789 GlDraw.color
(1.0, 1.0, 1.0);
790 drawstring 14 (if len > 0 then 8 else 2) (conf.winh
- 5) s;
792 match state.mode
with
793 | Textentry
((prefix
, text
, _
, _
, _
), _
) ->
797 Printf.sprintf
"%s%s_ [%s]" prefix text
state.text
799 Printf.sprintf
"%s%s_" prefix text
804 if len > 0 then drawstring state.text
808 state.text
<- Printf.sprintf
"%c%s" c s;
809 Glut.postRedisplay
();
818 state.rects
<- state.rects1
;
819 Glut.postRedisplay
()
822 let n = Scanf.sscanf
cmd "C %u" (fun n -> n) in
823 state.pagecount
<- n;
824 state.invalidated
<- state.invalidated
- 1;
825 if state.invalidated
= 0
829 let s = Scanf.sscanf
cmd "t %n"
830 (fun n -> String.sub
cmd n (String.length
cmd - n))
832 Glut.setWindowTitle
s
835 let s = Scanf.sscanf
cmd "T %n"
836 (fun n -> String.sub
cmd n (String.length
cmd - n))
838 if istextentry state.mode
845 Glut.postRedisplay
();
851 let s = Scanf.sscanf
cmd "V %n"
852 (fun n -> String.sub
cmd n (String.length
cmd - n))
858 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
859 Scanf.sscanf
cmd "F %u %d %f %f %f %f %f %f %f %f"
860 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
861 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
863 let y = (getpagey pageno) + truncate y0
in
866 state.rects1
<- [pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)]
869 let pageno, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
=
870 Scanf.sscanf
cmd "R %u %d %f %f %f %f %f %f %f %f"
871 (fun p
c x0 y0 x1 y1 x2 y2 x3 y3
->
872 (p
, c, x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
))
875 (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) :: state.rects1
878 let n, w, _h
, r
, l
, s, p
=
879 Scanf.sscanf
cmd "r %u %u %u %d %d %u %s"
880 (fun n w h r l
s p
->
881 (n-1, w, h, r
, l
!= 0, s, p
))
884 Hashtbl.replace
state.pagemap
(n, w, r
, l
, state.gen
) (p
, s);
885 state.memused
<- state.memused
+ s;
888 match state.throttle
with
889 | None
-> state.layout
890 | Some
layout -> layout
894 if (state.memused
<= conf.memlimit
) || cbempty state.pagecache
897 let evictedopaque = cbpeek state.pagecache
in
898 match findpageforopaque evictedopaque with
899 | None
-> failwith
"bug in gc"
900 | Some
((evictedn
, _
, _
, _
, gen
) as k
, evictedsize
) ->
901 if state.gen
!= gen
|| not
(pagevisible layout evictedn
)
903 wcmd "free" [`
s evictedopaque];
904 state.memused
<- state.memused
- evictedsize
;
905 Hashtbl.remove
state.pagemap k
;
906 cbdecr state.pagecache
;
913 cbput state.pagecache p
;
914 state.rendering
<- false;
916 begin match state.throttle
with
918 if pagevisible state.layout n
921 let allvisible = loadlayout state.layout in
922 if allvisible then preload ();
929 let y = getpagey l
.pageno + l
.pagey in
935 Scanf.sscanf
cmd "l %u %u %u %u" (fun n w h x
-> n, w, h, x
)
937 state.pdims
<- pdim :: state.pdims
940 let (l
, n, t
, h, pos
) =
941 Scanf.sscanf
cmd "o %u %u %d %u %n" (fun l
n t
h pos
-> l
, n, t
, h, pos
)
943 let s = String.sub
cmd pos
(String.length
cmd - pos
) in
944 let outline = (s, l
, (n, float t
/. float h)) in
946 match state.outlines with
947 | Olist
outlines -> Olist
(outline :: outlines)
948 | Oarray _
-> Olist
[outline]
949 | Onarrow _
-> Olist
[outline]
951 state.outlines <- outlines
955 let s = Scanf.sscanf
cmd "i %n"
956 (fun n -> String.sub
cmd n (String.length
cmd - n))
958 let len = String.length
s in
959 let rec fold accu pos
=
961 try String.index_from
s pos '
\n'
with Not_found
-> len
966 let line = String.sub
s pos
(eolpos - pos
) in
967 fold ((1, line)::accu) (eolpos+1)
969 state.docinfo
<- fold state.docinfo
0
972 dolog "unknown cmd `%S'" cmd
975 let now = Unix.gettimeofday
;;
978 if state.deadline
== nan
then state.deadline
<- now ();
982 then max
0.0 (state.deadline
-. now ())
985 let r, _
, _
= Unix.select
[state.csock
] [] [] timeout in
986 state.deadline
<- state.deadline
+. delay
;
989 if state.ascrollstep
> 0
991 let y = state.y + state.ascrollstep
in
992 let y = if y >= state.maxy then 0 else y in
998 let cmd = readcmd state.csock
in
1007 let action = function
1008 | HCprev
-> cbget cb ~
-1
1009 | HCnext
-> cbget cb
1
1010 | HCfirst
-> cbget cb ~
-(cb
.rc)
1011 | HClast
-> cbget cb
(cb
.len - 1 - cb
.rc)
1012 and cancel
() = cb
.rc <- rc
1016 let search pattern forward
=
1017 if String.length pattern
> 0
1020 match state.layout with
1023 l
.pageno, (l
.pagey + if forward
then 0 else 0*l
.pagevh)
1026 let b = makecmd "search"
1027 [`
b conf.icase
; `i
pn; `i
py; `i
(if forward
then 1 else 0)]
1029 Buffer.add_char
b '
,'
;
1030 Buffer.add_string
b pattern
;
1031 Buffer.add_char
b '
\000'
;
1034 writecmd state.csock
cmd;
1037 let intentry text key
=
1038 let c = Char.unsafe_chr key
in
1041 let s = "x" in s.[0] <- c;
1042 let text = text ^
s in
1046 state.text <- Printf.sprintf
"invalid char (%d, `%c')" key
c;
1051 let b = Buffer.create
(String.length
s + 1) in
1052 Buffer.add_string
b s;
1053 Buffer.add_char
b c;
1057 let textentry text key
=
1058 let c = Char.unsafe_chr key
in
1060 | _
when key
>= 32 && key
< 127 ->
1061 let text = addchar text c in
1065 dolog "unhandled key %d char `%c'" key
(Char.unsafe_chr key
);
1069 let reinit angle proportional
=
1070 conf.angle
<- angle
;
1071 conf.proportional
<- proportional
;
1073 wcmd "reinit" [`i angle
; `
b proportional
];
1077 let zoom = max
0.01 (min
2.2 zoom) in
1078 if zoom <> conf.zoom
1083 reshape conf.winw
conf.winh
;
1084 state.text <- Printf.sprintf
"zoom is now %-5.1f" (zoom *. 100.0);
1088 let enterbirdseye () =
1089 let zoom = float conf.thumbw
/. float conf.winw
in
1090 let birdseyepageno =
1091 let cy = conf.winh
/ 2 in
1095 let rec fold best
= function
1098 let d = cy - (l
.pagedispy
+ l
.pagevh/2)
1099 and dbest
= cy - (best
.pagedispy
+ best
.pagevh/2) in
1100 if abs
d < abs dbest
1107 state.mode
<- Birdseye
(
1108 { conf with zoom = conf.zoom }, state.x
, birdseyepageno, -1, getanchor ()
1111 conf.presentation <- false;
1112 conf.interpagespace <- 10;
1113 conf.hlinks
<- false;
1115 state.mstate
<- Mnone
;
1116 conf.showall
<- false;
1117 Glut.setCursor
Glut.CURSOR_INHERIT
;
1120 state.text <- Printf.sprintf
"birds eye mode on (zoom %3.1f%%)"
1125 reshape conf.winw
conf.winh
;
1128 let leavebirdseye (c, leftx
, pageno, _
, anchor) goback
=
1130 conf.zoom <- c.zoom;
1131 conf.presentation <- c.presentation;
1132 conf.interpagespace <- c.interpagespace;
1133 conf.showall
<- c.showall
;
1134 conf.hlinks
<- c.hlinks
;
1138 state.text <- Printf.sprintf
"birds eye mode off (zoom %3.1f%%)"
1141 reshape conf.winw
conf.winh
;
1142 state.anchor <- if goback
then anchor else (pageno, 0.0);
1145 let togglebirdseye () =
1146 match state.mode
with
1147 | Birdseye vals
-> leavebirdseye vals
true
1148 | View
| Outline _
-> enterbirdseye ()
1152 let upbirdseye (conf, leftx
, pageno, hooverpageno
, anchor) =
1153 let pageno = max
0 (pageno - 1) in
1154 let rec loop = function
1155 | [] -> gotopage1 pageno 0
1156 | l
:: _
when l
.pageno = pageno ->
1157 if l
.pagedispy
>= 0 && l
.pagey = 0
1158 then Glut.postRedisplay
()
1159 else gotopage1 pageno 0
1160 | _
:: rest
-> loop rest
1163 state.mode
<- Birdseye
(conf, leftx
, pageno, hooverpageno
, anchor)
1166 let downbirdseye (conf, leftx
, pageno, hooverpageno
, anchor) =
1167 let pageno = min
(state.pagecount
- 1) (pageno + 1) in
1168 state.mode
<- Birdseye
(conf, leftx
, pageno, hooverpageno
, anchor);
1169 let rec loop = function
1171 let y, h = getpageyh pageno in
1172 let dy = (y - state.y) - (conf.winh
- h - conf.interpagespace) in
1174 | l
:: _
when l
.pageno = pageno ->
1175 if l
.pagevh != l
.pageh
1176 then gotoy (clamp (l
.pageh
- l
.pagevh + conf.interpagespace))
1177 else Glut.postRedisplay
()
1178 | _
:: rest
-> loop rest
1183 let optentry mode _ key
=
1184 let btos b = if b then "on" else "off" in
1185 let c = Char.unsafe_chr key
in
1189 try conf.scrollstep
<- int_of_string
s with exc
->
1190 state.text <- Printf.sprintf
"bad integer `%s': %s"
1191 s (Printexc.to_string exc
)
1193 TEswitch
("scroll step", "", None
, intentry, ondone)
1198 conf.autoscrollstep
<- int_of_string
s;
1199 if state.ascrollstep
> 0
1200 then state.ascrollstep
<- conf.autoscrollstep
;
1202 state.text <- Printf.sprintf
"bad integer `%s': %s"
1203 s (Printexc.to_string exc
)
1205 TEswitch
("auto scroll step", "", None
, intentry, ondone)
1210 let zoom = float (int_of_string
s) /. 100.0 in
1213 state.text <- Printf.sprintf
"bad integer `%s': %s"
1214 s (Printexc.to_string exc
)
1216 TEswitch
("zoom", "", None
, intentry, ondone)
1221 conf.thumbw
<- max
2 (min
1920 (int_of_string
s));
1223 Printf.sprintf
"thumbnail width is set to %d" conf.thumbw
;
1224 begin match mode
with
1226 leavebirdseye beye
false;
1231 state.text <- Printf.sprintf
"bad integer `%s': %s"
1232 s (Printexc.to_string exc
)
1234 TEswitch
("thumbnail width", "", None
, intentry, ondone)
1239 Some
(int_of_string
s)
1241 state.text <- Printf.sprintf
"bad integer `%s': %s"
1242 s (Printexc.to_string exc
);
1245 | Some angle
-> reinit angle
conf.proportional
1248 TEswitch
("rotation", "", None
, intentry, ondone)
1251 conf.icase
<- not
conf.icase
;
1252 TEdone
("case insensitive search " ^
(btos conf.icase
))
1255 conf.preload <- not
conf.preload;
1257 TEdone
("preload " ^
(btos conf.preload))
1260 conf.verbose
<- not
conf.verbose
;
1261 TEdone
("verbose " ^
(btos conf.verbose
))
1264 conf.maxhfit
<- not
conf.maxhfit
;
1265 state.maxy <- state.maxy + (if conf.maxhfit
then -conf.winh
else conf.winh
);
1266 TEdone
("maxhfit " ^
(btos conf.maxhfit
))
1269 conf.crophack
<- not
conf.crophack
;
1270 TEdone
("crophack " ^
btos conf.crophack
)
1273 conf.showall
<- not
conf.showall
;
1274 TEdone
("throttle " ^
btos conf.showall
)
1277 conf.underinfo
<- not
conf.underinfo
;
1278 TEdone
("underinfo " ^
btos conf.underinfo
)
1281 conf.savebmarks
<- not
conf.savebmarks
;
1282 TEdone
("persistent bookmarks " ^
btos conf.savebmarks
)
1288 match state.layout with
1293 conf.interpagespace <- int_of_string
s;
1294 state.maxy <- calcheight ();
1295 let y = getpagey pageno in
1298 state.text <- Printf.sprintf
"bad integer `%s': %s"
1299 s (Printexc.to_string exc
)
1301 TEswitch
("vertical margin: ", "", None
, intentry, ondone)
1304 reinit conf.angle
(not
conf.proportional
);
1305 TEdone
("proprortional display: " ^
btos conf.proportional
)
1308 state.text <- Printf.sprintf
"bad option %d `%c'" key
c;
1312 let maxoutlinerows () = (conf.winh
- 31) / 16;;
1314 let enterselector allowdel
outlines errmsg msg
=
1315 if Array.length
outlines = 0
1317 showtext ' ' errmsg
;
1321 Glut.setCursor
Glut.CURSOR_INHERIT
;
1323 match state.layout with
1325 | {pageno=pageno} :: _
-> pageno
1329 if n = Array.length
outlines
1332 let (_
, _
, (outlinepageno
, _
)) = outlines.(n) in
1333 if outlinepageno
>= pageno then n else loop (n+1)
1337 state.mode
<- Outline
1338 (allowdel
, active, max
0 (active - maxoutlinerows () / 2), outlines, "", 0,
1340 Glut.postRedisplay
();
1344 let enteroutlinemode () =
1346 match state.outlines with
1349 let a = Array.of_list
(List.rev l
) in
1350 state.outlines <- Oarray
a;
1352 | Onarrow
(pat
, a, _
) ->
1353 a, "Outline was narrowed to `" ^ pat ^
"' (Ctrl-u to restore)"
1355 enterselector false outlines "Document has no outline" msg
;
1358 let enterbookmarkmode () =
1359 let bookmarks = Array.of_list
state.bookmarks in
1360 enterselector true bookmarks "Document has no bookmarks (yet)" "";
1363 let mode_to_string mode
=
1364 let b = Buffer.create
10 in
1365 let rec f = function
1366 | Textentry
(_
, _
) -> Buffer.add_string
b "Textentry ";
1367 | View
-> Buffer.add_string
b "View"
1368 | Birdseye _
-> Buffer.add_string
b "Birdseye"
1369 | Items _
-> Buffer.add_string
b "Items"
1370 | Outline _
-> Buffer.add_string
b "Outline"
1376 let color_of_string s =
1377 Scanf.sscanf
s "%d/%d/%d" (fun r g
b ->
1378 (float r /. 256.0, float g
/. 256.0, float b /. 256.0)
1382 let color_to_string (r, g
, b) =
1383 let r = truncate
(r *. 256.0)
1384 and g
= truncate
(g
*. 256.0)
1385 and b = truncate
(b *. 256.0) in
1386 Printf.sprintf
"%d/%d/%d" r g
b
1389 let enterinfomode () =
1390 let btos = function true -> "on" | _
-> "off" in
1391 let mode = state.mode in
1392 let rec makeitems () =
1393 let intp name get set
=
1394 Printf.sprintf
"%s\t%d" name
(get
()), 1, Action
(
1395 fun active first _ pan
->
1400 state.text <- Printf.sprintf
"bad integer `%s': %s"
1401 s (Printexc.to_string exn
);
1404 if n != max_int
then set
n;
1406 let te = name ^
": ", "", None
, intentry, ondone in
1411 state.mode <- Items
(active, first
, makeitems (), "", pan
, mode)
1414 and boolp name get set
=
1415 Printf.sprintf
"%s\t%s" name
(btos (get
())), 1, Action
(
1416 fun active first qsearch pan
->
1419 Items
(active, first
, makeitems (), qsearch
, pan
, mode);
1421 and colorp name get set
=
1422 Printf.sprintf
"%s\t%s" name
(color_to_string (get
())), 1, Action
(
1423 fun active first _ pan
->
1424 let invalid = (nan
, nan
, nan
) in
1427 try color_of_string s
1429 state.text <- Printf.sprintf
"bad color `%s': %s"
1430 s (Printexc.to_string exn
);
1436 let te = name ^
": ", "", None
, textentry, ondone in
1441 state.mode <- Items
(active, first
, makeitems (), "", pan
, mode)
1446 let birdseye = isbirdseye mode in
1448 (if birdseye then "Setup bird's eye" else "Setup"), 0, Noaction
;
1450 boolp
"presentation"
1451 (fun () -> conf.presentation)
1453 conf.presentation <- v;
1454 state.anchor <- getanchor ();
1457 boolp
"ignore case in searches"
1458 (fun () -> conf.icase
)
1459 (fun v -> conf.icase
<- v);
1462 (fun () -> conf.preload)
1463 (fun v -> conf.preload <- v);
1466 (fun () -> conf.verbose
)
1467 (fun v -> conf.verbose
<- v);
1470 (fun () -> conf.maxhfit
)
1471 (fun v -> conf.maxhfit
<- v);
1474 (fun () -> conf.crophack
)
1475 (fun v -> conf.crophack
<- v);
1478 (fun () -> conf.showall
)
1479 (fun v -> conf.showall
<- v);
1481 boolp
"highlight links"
1482 (fun () -> conf.hlinks
)
1483 (fun v -> conf.hlinks
<- v);
1486 (fun () -> conf.underinfo
)
1487 (fun v -> conf.underinfo
<- v);
1488 boolp
"persistent bookmarks"
1489 (fun () -> conf.savebmarks
)
1490 (fun v -> conf.savebmarks
<- v);
1492 boolp
"proportional display"
1493 (fun () -> conf.proportional
)
1494 (fun v -> reinit conf.angle
v);
1496 boolp
"persistent location"
1497 (fun () -> conf.jumpback
)
1498 (fun v -> conf.jumpback
<- v);
1502 intp "vertical margin"
1503 (fun () -> conf.interpagespace)
1505 conf.interpagespace <- n;
1507 match state.layout with
1512 state.maxy <- calcheight ();
1513 let y = getpagey pageno in
1518 (fun () -> conf.pagebias
)
1519 (fun v -> conf.pagebias
<- v);
1522 (fun () -> conf.scrollstep
)
1523 (fun n -> conf.scrollstep
<- n);
1525 intp "auto scroll step"
1527 if state.ascrollstep
> 0
1528 then state.ascrollstep
1529 else conf.autoscrollstep
)
1531 if state.ascrollstep
> 0
1532 then state.ascrollstep
<- n
1533 else conf.autoscrollstep
<- n);
1536 (fun () -> truncate
(conf.zoom *. 100.))
1537 (fun v -> setzoom ((float v) /. 100.));
1540 (fun () -> conf.angle
)
1541 (fun v -> reinit v conf.proportional
);
1543 intp "scroll bar width"
1544 (fun () -> state.scrollw
)
1548 reshape conf.winw
conf.winh
;
1551 intp "scroll handle height"
1552 (fun () -> conf.scrollh
)
1553 (fun v -> conf.scrollh
<- v;);
1555 intp "thumbnail width"
1556 (fun () -> conf.thumbw
)
1558 conf.thumbw
<- min
1920 v;
1561 leavebirdseye beye
false;
1566 colorp
"background color"
1567 (fun () -> conf.bgcolor
)
1568 (fun v -> conf.bgcolor
<- v);
1571 "Presentation mode", 0, Noaction
;
1573 boolp
"scrollbar visible"
1574 (fun () -> conf.scrollbarinpm
)
1576 if v != conf.scrollbarinpm
1578 conf.scrollbarinpm
<- v;
1579 if conf.presentation
1581 state.scrollw
<- if v then conf.scrollbw
else 0;
1582 reshape conf.winw
conf.winh
;
1588 "Pixmap Cache", 0, Noaction
;
1590 intp "size (advisory)"
1591 (fun () -> conf.memlimit
)
1592 (fun v -> conf.memlimit
<- v);
1593 Printf.sprintf
"%s\t%d" "used" state.memused
, 1, Noaction
;
1600 :: ("Document", 0, Noaction
)
1601 :: List.map
(fun (_
, s) -> (s, 1, Noaction
)) state.docinfo
1607 :: (Printf.sprintf
"Save these parameters as defaults at exit (%s)"
1608 (btos conf.bedefault
),
1611 fun active first qsearch pan
->
1612 conf.bedefault
<- not
conf.bedefault
;
1613 Items
(active, first
, makeitems (), qsearch
, pan
, mode);
1618 Array.of_list
(items @ tailer)
1621 state.mode <- Items
(1, 0, makeitems (), "", 0, mode);
1622 Glut.postRedisplay
();
1625 let enterhelpmode () =
1626 state.mode <- Items
(-1, 0, state.help
, "", 0, state.mode);
1627 Glut.postRedisplay
();
1630 let quickbookmark ?title
() =
1631 match state.layout with
1637 let sec = Unix.gettimeofday
() in
1638 let tm = Unix.localtime
sec in
1639 Printf.sprintf
"Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
1643 (tm.Unix.tm_year
+ 1900)
1646 | Some
title -> title
1649 (title, 0, (l
.pageno, float l
.pagey /. float l
.pageh
))
1654 state.fullscreen
<- None
;
1655 Glut.reshapeWindow
w h;
1658 let writeopen path password
=
1659 writecmd state.csock
("open " ^ path ^
"\000" ^ password ^
"\000");
1660 writecmd state.csock
"info";
1663 let opendoc path password
=
1666 state.password
<- password
;
1667 state.gen
<- state.gen
+ 1;
1669 writeopen path password
;
1670 Glut.setWindowTitle
("llpp " ^
Filename.basename path
);
1671 wcmd "geometry" [`i
state.w; `i
conf.winh
];
1674 let viewkeyboard key
=
1676 let mode = state.mode in
1677 state.mode <- Textentry
(te, fun _
-> state.mode <- mode);
1680 Glut.postRedisplay
()
1682 let c = Char.chr key
in
1684 | '
\027'
| 'q'
-> (* escape *)
1687 | '
\008'
-> (* backspace *)
1688 let y = getnav ~
-1 in
1689 gotoy_and_clear_text y
1697 Glut.postRedisplay
()
1700 let ondone isforw
s =
1701 cbput state.hists
.pat
s;
1702 state.searchpattern
<- s;
1705 let s = String.create
1 in
1707 enttext (s, "", Some
(onhist state.hists
.pat
),
1708 textentry, ondone (c ='
/'
))
1710 | '
+'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1711 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
1712 setzoom (min
2.2 (conf.zoom +. incr))
1717 try int_of_string
s with exc
->
1718 state.text <- Printf.sprintf
"bad integer `%s': %s"
1719 s (Printexc.to_string exc
);
1725 state.text <- "page bias is now " ^ string_of_int
n;
1728 enttext ("page bias: ", "", None
, intentry, ondone)
1730 | '
-'
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
1731 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
1732 setzoom (max
0.01 (conf.zoom -. decr))
1735 let ondone msg
= state.text <- msg
in
1737 "option [acfhilpstvAPRSZ]: ", "", None
,
1738 optentry state.mode, ondone
1741 | '
0'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1744 | '
1'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1745 let zoom = zoomforh
conf.winw
conf.winh
state.scrollw
in
1749 | '
9'
when (Glut.getModifiers
() land Glut.active_ctrl
!= 0) ->
1755 try int_of_string
s with exc
->
1756 state.text <- Printf.sprintf
"bad integer `%s': %s"
1757 s (Printexc.to_string exc
);
1763 cbput state.hists
.pag
(string_of_int
n);
1764 gotoy_and_clear_text (getpagey (n + conf.pagebias
- 1))
1767 let pageentry text key
=
1768 match Char.unsafe_chr key
with
1769 | 'g'
-> TEdone
text
1770 | _
-> intentry text key
1772 let text = "x" in text.[0] <- c;
1773 enttext (":", text, Some
(onhist state.hists
.pag
), pageentry, ondone)
1776 state.scrollw
<- if state.scrollw
> 0 then 0 else conf.scrollbw
;
1777 reshape conf.winw
conf.winh
;
1780 conf.hlinks
<- not
conf.hlinks
;
1781 state.text <- "highlightlinks " ^
if conf.hlinks
then "on" else "off";
1782 Glut.postRedisplay
()
1785 if state.ascrollstep
= 0
1786 then state.ascrollstep
<- conf.autoscrollstep
1788 conf.autoscrollstep
<- state.ascrollstep
;
1789 state.ascrollstep
<- 0;
1793 conf.presentation <- not
conf.presentation;
1794 if conf.presentation
1796 if not
conf.scrollbarinpm
1797 then state.scrollw
<- 0;
1800 state.scrollw
<- conf.scrollbw
;
1802 showtext ' '
("presentation mode " ^
1803 if conf.presentation then "on" else "off");
1804 state.anchor <- getanchor ();
1808 begin match state.fullscreen
with
1810 state.fullscreen
<- Some
(conf.winw
, conf.winh
);
1813 state.fullscreen
<- None
;
1818 gotoy_and_clear_text 0
1821 search state.searchpattern
true
1824 search state.searchpattern
false
1827 begin match state.layout with
1830 gotoy_and_clear_text (getpagey l
.pageno)
1834 begin match List.rev
state.layout with
1837 let pageno = min
(l
.pageno+1) (state.pagecount
-1) in
1838 gotoy_and_clear_text (getpagey pageno)
1841 | '
\127'
-> (* del *)
1842 begin match state.layout with
1845 let pageno = max
0 (l
.pageno-1) in
1846 gotoy_and_clear_text (getpagey pageno)
1851 if fn
= -1 then l
.pageno, l
.pageno else fn
, l
.pageno
1853 let fn, ln
= List.fold_left
f (-1, -1) state.layout in
1855 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
1859 else (100. *. (float state.y /. float maxy)) in
1862 Printf.sprintf
"Page %d of %d %.2f%%"
1863 (fn+1) state.pagecount
percent
1866 "Pages %d-%d of %d %.2f%%"
1867 (fn+1) (ln
+1) state.pagecount
percent
1872 begin match state.layout with
1875 doreshape (l
.pagew
+ state.scrollw
) l
.pageh
;
1876 Glut.postRedisplay
();
1880 enterbookmarkmode ()
1890 match state.layout with
1893 (s, 0, (l
.pageno, float l
.pagey /. float l
.pageh
))
1897 enttext ("bookmark: ", "", None
, textentry, ondone)
1901 showtext ' '
"Quick bookmark added";
1904 begin match state.layout with
1906 let rect = getpdimrect l
.pagedimno
in
1910 (truncate
(1.8 *. (rect.(1) -. rect.(0))),
1911 truncate
(1.2 *. (rect.(3) -. rect.(0))))
1913 (truncate
(rect.(1) -. rect.(0)),
1914 truncate
(rect.(3) -. rect.(0)))
1918 doreshape (w + state.scrollw
) (h + conf.interpagespace)
1920 Glut.postRedisplay
();
1926 reinit (conf.angle
+ (if c = '
>'
then 30 else -30)) conf.proportional
1931 (min
(state.colorscale
+. (if c = '
]'
then 0.1 else -0.1)) 1.0);
1932 Glut.postRedisplay
()
1935 begin match state.mode with
1936 | Birdseye beye
-> upbirdseye beye
1937 | _
-> gotoy (clamp (-conf.scrollstep
))
1941 begin match state.mode with
1942 | Birdseye beye
-> downbirdseye beye
1943 | _
-> gotoy (clamp conf.scrollstep
)
1947 state.anchor <- getanchor ();
1948 opendoc state.path
state.password
1951 vlog "huh? %d %c" key
(Char.chr key
);
1954 let textentrykeyboard key
((c, text, opthist
, onkey
, ondone), onleave
) =
1956 state.mode <- Textentry
(te, onleave
);
1959 Glut.postRedisplay
()
1961 match Char.unsafe_chr key
with
1962 | '
\008'
-> (* backspace *)
1963 let len = String.length
text in
1967 Glut.postRedisplay
();
1970 let s = String.sub
text 0 (len - 1) in
1971 enttext (c, s, opthist
, onkey
, ondone)
1977 Glut.postRedisplay
()
1979 | '
\027'
-> (* escape *)
1980 begin match opthist
with
1982 | Some
(_
, onhistcancel
) -> onhistcancel
()
1985 Glut.postRedisplay
()
1988 begin match onkey
text key
with
1992 Glut.postRedisplay
()
1995 enttext (c, text, opthist
, onkey
, ondone);
1999 Glut.postRedisplay
()
2002 state.mode <- Textentry
(te, onleave
);
2003 Glut.postRedisplay
()
2007 let birdseyekeyboard key
((_
, _
, pageno, _
, _
) as beye
) =
2009 | 27 -> (* escape *)
2010 leavebirdseye beye
true
2012 | 12 -> (* ctrl-l *)
2013 let y, h = getpageyh pageno in
2014 let top = (conf.winh
- h) / 2 in
2015 gotoy (max
0 (y - top))
2018 leavebirdseye beye
false
2024 let itemskeyboard key
(active, first
, items, qsearch
, pan
, oldmode
) =
2025 let set active first qsearch
=
2026 state.mode <- Items
(active, first
, items, qsearch
, pan
, oldmode
)
2028 let search active pattern
incr =
2031 if n >= Array.length
items || n < 0
2034 let (s, _
, _
) = items.(n) in
2036 (try ignore
(Str.search_forward re
s 0); true
2037 with Not_found
-> false)
2039 else loop (n + incr)
2044 let re = Str.regexp_case_fold pattern
in
2050 let firstof active = max
0 (active - maxoutlinerows () / 2) in
2052 | 18 | 19 -> (* ctrl-r/ctlr-s *)
2053 let incr = if key
= 18 then -1 else 1 in
2055 match search (active + incr) qsearch
incr with
2057 state.text <- qsearch ^
" [not found]";
2060 state.text <- qsearch
;
2061 active, firstof active
2063 set active first qsearch
;
2064 Glut.postRedisplay
();
2066 | 8 -> (* backspace *)
2067 let len = String.length qsearch
in
2074 set active first
"";
2077 let qsearch = String.sub
qsearch 0 (len - 1) in
2079 match search active qsearch ~
-1 with
2081 state.text <- qsearch ^
" [not found]";
2084 state.text <- qsearch;
2085 active, firstof active
2087 set active first
qsearch
2089 Glut.postRedisplay
()
2091 | _
when key
>= 32 && key
< 127 ->
2092 let pattern = addchar qsearch (Char.chr key
) in
2094 match search active pattern 1 with
2096 state.text <- pattern ^
" [not found]";
2099 state.text <- pattern;
2100 active, firstof active
2102 set active first
pattern;
2103 Glut.postRedisplay
()
2105 | 27 -> (* escape *)
2107 if String.length
qsearch = 0
2109 state.mode <- oldmode
;
2112 set active first
"";
2114 Glut.postRedisplay
()
2117 if active >= 0 && active < Array.length
items
2119 match items.(active) with
2121 state.mode <- f active first
qsearch pan
2125 state.mode <- oldmode
2129 state.mode <- oldmode
2131 Glut.postRedisplay
();
2133 | _
-> dolog "unknown key %d" key
2136 let outlinekeyboard key
2137 (allowdel
, active, first
, outlines, qsearch, pan
, oldmode
) =
2138 let narrow outlines pattern =
2139 let reopt = try Some
(Str.regexp_case_fold
pattern) with _
-> None
in
2143 let rec fold accu n =
2147 let (s, _
, _
) as o
= outlines.(n) in
2149 if (try ignore
(Str.search_forward
re s 0); true
2150 with Not_found
-> false)
2156 let matched = fold [] (Array.length
outlines - 1) in
2157 if matched = [] then None
else Some
(Array.of_list
matched)
2159 let search active pattern incr =
2162 if n = Array.length
outlines || n = -1
2165 let (s, _
, _
) = outlines.(n) in
2167 (try ignore
(Str.search_forward
re s 0); true
2168 with Not_found
-> false)
2170 else loop (n + incr)
2175 let re = Str.regexp_case_fold
pattern in
2181 let firstof active = max
0 (active - maxoutlinerows () / 2) in
2183 | 27 -> (* escape *)
2185 if String.length
qsearch = 0
2187 state.mode <- oldmode
;
2190 state.mode <- Outline
(
2191 allowdel
, active, first
, outlines, "", pan
, oldmode
2194 Glut.postRedisplay
();
2196 | 18 | 19 -> (* ctrl-r/ctrl-s *)
2197 let incr = if key
= 18 then -1 else 1 in
2199 match search (active + incr) qsearch incr with
2201 state.text <- qsearch ^
" [not found]";
2204 state.text <- qsearch;
2205 active, firstof active
2207 state.mode <- Outline
(
2208 allowdel
, active, first
, outlines, qsearch, pan
, oldmode
2210 Glut.postRedisplay
();
2212 | 8 -> (* backspace *)
2213 let len = String.length
qsearch in
2220 state.mode <- Outline
(
2221 allowdel
, active, first
, outlines, "", pan
, oldmode
2225 let qsearch = String.sub
qsearch 0 (len - 1) in
2227 match search active qsearch ~
-1 with
2229 state.text <- qsearch ^
" [not found]";
2232 state.text <- qsearch;
2233 active, firstof active
2235 state.mode <- Outline
(
2236 allowdel
, active, first
, outlines, qsearch, pan
, oldmode
2239 Glut.postRedisplay
()
2242 if active < Array.length
outlines
2244 let (_
, _
, anchor) = outlines.(active) in
2249 if allowdel
then state.bookmarks <- Array.to_list
outlines;
2250 state.mode <- oldmode
;
2251 Glut.postRedisplay
();
2253 | _
when key
>= 32 && key
< 127 ->
2254 let pattern = addchar qsearch (Char.chr key
) in
2256 match search active pattern 1 with
2258 state.text <- pattern ^
" [not found]";
2261 state.text <- pattern;
2262 active, firstof active
2264 state.mode <- Outline
(
2265 allowdel
, active, first
, outlines, pattern, pan
, oldmode
2267 Glut.postRedisplay
()
2269 | 14 when not allowdel
-> (* ctrl-n *)
2270 if String.length
qsearch > 0
2272 let optoutlines = narrow outlines qsearch in
2273 begin match optoutlines with
2274 | None
-> state.text <- "can't narrow"
2276 state.mode <- Outline
(
2277 allowdel
, 0, 0, outlines, qsearch, pan
, oldmode
2279 match state.outlines with
2282 state.outlines <- Onarrow
(qsearch, outlines, a)
2283 | Onarrow
(_
, _
, b) ->
2284 state.outlines <- Onarrow
(qsearch, outlines, b)
2287 Glut.postRedisplay
()
2289 | 21 when not allowdel
-> (* ctrl-u *)
2291 match state.outlines with
2294 let a = Array.of_list
(List.rev l
) in
2295 state.outlines <- Oarray
a;
2297 | Onarrow
(_
, _
, b) ->
2298 state.outlines <- Oarray
b;
2302 state.mode <- Outline
(allowdel
, 0, 0, outline, qsearch, pan
, oldmode
);
2303 Glut.postRedisplay
()
2305 | 12 -> (* ctrl-l *)
2306 state.mode <- Outline
2307 (allowdel
, active, firstof active, outlines, qsearch, pan
, oldmode
);
2308 Glut.postRedisplay
()
2310 | 127 when allowdel
-> (* delete *)
2311 let len = Array.length
outlines - 1 in
2315 state.bookmarks <- [];
2318 let bookmarks = Array.init
len
2320 let i = if i >= active then i + 1 else i in
2334 Glut.postRedisplay
()
2336 | _
-> dolog "unknown key %d" key
2339 let keyboard ~key ~x ~
y =
2342 if key
= 7 (* ctrl-g *)
2346 match state.mode with
2347 | Outline
outline -> outlinekeyboard key
outline
2348 | Textentry
textentry -> textentrykeyboard key
textentry
2349 | Birdseye
birdseye -> birdseyekeyboard key
birdseye
2350 | View
-> viewkeyboard key
2351 | Items
items -> itemskeyboard key
items
2354 let birdseyespecial key
((conf, leftx
, _
, hooverpageno
, anchor) as beye
) =
2356 | Glut.KEY_UP
-> upbirdseye beye
2357 | Glut.KEY_DOWN
-> downbirdseye beye
2359 | Glut.KEY_PAGE_UP
->
2360 begin match state.layout with
2364 state.mode <- Birdseye
(
2365 conf, leftx
, l
.pageno, hooverpageno
, anchor
2367 gotopage1 l
.pageno 0;
2370 let layout = layout (state.y-conf.winh
) conf.winh
in
2372 | [] -> gotoy (clamp (-conf.winh
))
2374 state.mode <- Birdseye
(
2375 conf, leftx
, l
.pageno, hooverpageno
, anchor
2377 gotopage1 l
.pageno 0
2380 | [] -> gotoy (clamp (-conf.winh
))
2383 | Glut.KEY_PAGE_DOWN
->
2384 begin match List.rev
state.layout with
2386 let layout = layout (state.y + conf.winh
) conf.winh
in
2387 begin match layout with
2389 let incr = l
.pageh
- l
.pagevh in
2394 conf, leftx
, state.pagecount
- 1, hooverpageno
, anchor
2396 Glut.postRedisplay
();
2398 else gotoy (clamp (incr + conf.interpagespace*2));
2402 Birdseye
(conf, leftx
, l
.pageno, hooverpageno
, anchor);
2403 gotopage1 l
.pageno 0;
2406 | [] -> gotoy (clamp conf.winh
)
2410 state.mode <- Birdseye
(conf, leftx
, 0, hooverpageno
, anchor);
2414 let pageno = state.pagecount
- 1 in
2415 state.mode <- Birdseye
(conf, leftx
, pageno, hooverpageno
, anchor);
2416 if not
(pagevisible state.layout pageno)
2419 match List.rev
state.pdims
with
2421 | (_
, _
, h, _
) :: _
-> h
2423 gotoy (max
0 (getpagey pageno - (conf.winh
- h - conf.interpagespace)))
2424 else Glut.postRedisplay
();
2428 let setautoscrollspeed goingdown
=
2429 let incr = max
1 (state.ascrollstep
/ 2) in
2430 let astep = max
1 (state.ascrollstep
+ (if goingdown
then incr else -incr)) in
2431 state.ascrollstep
<- astep;
2434 let special ~key ~x ~
y =
2437 match state.mode with
2438 | View
| (Birdseye _
) when key
= Glut.KEY_F9
->
2442 birdseyespecial key vals
2444 | View
when key
= Glut.KEY_F1
->
2448 if state.ascrollstep
> 0 && (key
= Glut.KEY_DOWN
|| key
= Glut.KEY_UP
)
2449 then setautoscrollspeed (key
= Glut.KEY_DOWN
)
2453 | Glut.KEY_F3
-> search state.searchpattern
true; state.y
2454 | Glut.KEY_UP
-> clamp (-conf.scrollstep
)
2455 | Glut.KEY_DOWN
-> clamp conf.scrollstep
2456 | Glut.KEY_PAGE_UP
->
2457 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2459 match state.layout with
2461 | l
:: _
-> state.y - l
.pagey
2464 | Glut.KEY_PAGE_DOWN
->
2465 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2467 match List.rev
state.layout with
2469 | l
:: _
-> getpagey l
.pageno
2472 | Glut.KEY_HOME
-> addnav (); 0
2475 state.maxy - (if conf.maxhfit
then conf.winh
else 0)
2477 | (Glut.KEY_RIGHT
| Glut.KEY_LEFT
) when
2478 Glut.getModifiers
() land Glut.active_alt
!= 0 ->
2479 getnav (if key
= Glut.KEY_LEFT
then 1 else -1)
2481 | Glut.KEY_RIGHT
when conf.zoom > 1.0 ->
2482 state.x
<- state.x
- 10;
2484 | Glut.KEY_LEFT
when conf.zoom > 1.0 ->
2485 state.x
<- state.x
+ 10;
2490 gotoy_and_clear_text y
2493 ((c, _
, (Some
(action, _
) as onhist), onkey
, ondone), mode) ->
2496 | Glut.KEY_UP
-> action HCprev
2497 | Glut.KEY_DOWN
-> action HCnext
2498 | Glut.KEY_HOME
-> action HCfirst
2499 | Glut.KEY_END
-> action HClast
2502 state.mode <- Textentry
((c, s, onhist, onkey
, ondone), mode);
2503 Glut.postRedisplay
()
2507 | Items
(active, first
, items, qsearch, pan
, oldmode
) ->
2508 let maxrows = maxoutlinerows () in
2509 let itemcount = Array.length
items in
2510 let hasaction = function
2511 | (_
, _
, Noaction
) -> false
2514 let find start
incr =
2516 if i = -1 || i = itemcount
2519 if hasaction items.(i)
2521 else find (i + incr)
2526 let set active first
=
2527 let first = max
0 (min
first (itemcount - maxrows)) in
2528 state.mode <- Items
(active, first, items, qsearch, pan
, oldmode
)
2531 let isvisible first n = n >= first && n - first <= maxrows in
2533 let incr1 = if incr > 0 then 1 else -1 in
2534 if isvisible first active
2537 let next = active + incr in
2539 if next < 0 || next >= itemcount
2541 else find next incr1
2543 if next = -1 || abs
(active - next) > maxrows
2549 let first = first + incr in
2550 let first = max
0 (min
first (itemcount - 1)) in
2552 let next = active + incr in
2553 let next = max
0 (min
next (itemcount - 1)) in
2556 let active = if next = -1 then active else next in
2559 let first = min
next first in
2562 let first = first + incr in
2563 let first = max
0 (min
first (itemcount - 1)) in
2565 let next = active + incr in
2566 let next = max
0 (min
next (itemcount - 1)) in
2567 let next = find next incr1 in
2568 if next = -1 || abs
(active - first) > maxrows
2575 Glut.postRedisplay
()
2577 begin match key
with
2578 | Glut.KEY_UP
-> navigate ~
-1
2579 | Glut.KEY_DOWN
-> navigate 1
2580 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
2581 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
2584 state.mode <- Items
(
2585 active, first, items, qsearch, min
0 (pan
- 1), oldmode
2587 Glut.postRedisplay
()
2590 state.mode <- Items
(
2591 active, first, items, qsearch, min
0 (pan
+ 1), oldmode
2593 Glut.postRedisplay
()
2596 let active = find 0 1 in
2598 Glut.postRedisplay
()
2601 let first = max
0 (itemcount - maxrows) in
2602 let active = find (itemcount - 1) ~
-1 in
2604 Glut.postRedisplay
()
2609 | Outline
(allowdel
, active, first, outlines, qsearch, pan
, oldmode
) ->
2610 let maxrows = maxoutlinerows () in
2611 let calcfirst first active =
2614 let rows = active - first in
2615 if rows > maxrows then active - maxrows else first
2619 let active = active + incr in
2620 let active = max
0 (min
active (Array.length
outlines - 1)) in
2621 let first = calcfirst first active in
2622 state.mode <- Outline
(
2623 allowdel
, active, first, outlines, qsearch, pan
, oldmode
2625 Glut.postRedisplay
()
2627 let updownlevel incr =
2628 let len = Array.length
outlines in
2629 let (_
, curlevel
, _
) = outlines.(active) in
2631 if i = len then i-1 else if i = -1 then 0 else
2632 let (_
, l
, _
) = outlines.(i) in
2633 if l
!= curlevel
then i else flow (i+incr)
2635 let active = flow active in
2636 let first = calcfirst first active in
2637 state.mode <- Outline
(
2638 allowdel
, active, first, outlines, qsearch, pan
, oldmode
2640 Glut.postRedisplay
()
2643 | Glut.KEY_UP
-> navigate ~
-1
2644 | Glut.KEY_DOWN
-> navigate 1
2645 | Glut.KEY_PAGE_UP
-> navigate ~
-maxrows
2646 | Glut.KEY_PAGE_DOWN
-> navigate maxrows
2649 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2651 state.mode <- Outline
(
2652 allowdel
, active, first, outlines,
2653 qsearch, min
0 (pan
+ 1), oldmode
2655 Glut.postRedisplay
();
2663 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2665 state.mode <- Outline
(
2666 allowdel
, active, first, outlines, qsearch, pan
- 1, oldmode
2668 Glut.postRedisplay
();
2672 then updownlevel ~
-1
2676 state.mode <- Outline
(
2677 allowdel
, 0, 0, outlines, qsearch, pan
, oldmode
2679 Glut.postRedisplay
()
2682 let active = Array.length
outlines - 1 in
2683 let first = max
0 (active - maxrows) in
2684 state.mode <- Outline
(
2685 allowdel
, active, first, outlines, qsearch, pan
, oldmode
2687 Glut.postRedisplay
()
2692 let drawplaceholder l
=
2693 let margin = state.x
+ (conf.winw
- (state.w + state.scrollw
)) / 2 in
2695 (float l
.pagex
, float l
.pagedispy
)
2696 (float (l
.pagew
+ l
.pagex
), float (l
.pagedispy
+ l
.pagevh))
2698 let x = if margin < 0 then -margin else l
.pagex
2699 and y = l
.pagedispy
+ 13 in
2700 GlDraw.color
(0.0, 0.0, 0.0);
2701 drawstring 13 x y ("Loading " ^ string_of_int
(l
.pageno + 1))
2704 let now () = Unix.gettimeofday
();;
2708 match state.mode with
2709 | Textentry _
-> scalecolor 0.4
2710 | View
| Outline _
| Items _
-> scalecolor 1.0
2711 | Birdseye
(_
, _
, pageno, hooverpageno
, _
) ->
2712 if l
.pageno = hooverpageno
2715 if l
.pageno = pageno
2721 begin match getopaque l
.pageno with
2722 | Some
(opaque, _
) when validopaque opaque ->
2724 draw
(l
.pagedispy
, l
.pagew
, l
.pagevh, l
.pagey, conf.hlinks
)
2728 vlog "draw %d %f sec" l
.pageno d;
2736 let maxy = state.maxy - (if conf.maxhfit
then conf.winh
else 0) in
2737 let sh = (float (maxy + conf.winh
) /. float conf.winh
) in
2738 let sh = float conf.winh
/. sh in
2739 let sh = max
sh (float conf.scrollh
) in
2742 if state.y = state.maxy
2744 else float y /. float maxy
2746 let position = (float conf.winh
-. sh) *. percent in
2749 if position +. sh > float conf.winh
2750 then float conf.winh
-. sh
2756 let scrollindicator () =
2757 GlDraw.color (0.64 , 0.64, 0.64);
2759 (float (conf.winw
- state.scrollw
), 0.)
2760 (float conf.winw
, float conf.winh
)
2762 GlDraw.color (0.0, 0.0, 0.0);
2764 let position, sh = scrollph state.y in
2766 (float (conf.winw
- state.scrollw
), position)
2767 (float conf.winw
, position +. sh)
2771 let showsel margin =
2772 match state.mstate
with
2773 | Mnone
| Mscroll _
| Mpan _
| Mzoom _
->
2776 | Msel
((x0
, y0
), (x1
, y1
)) ->
2777 let rec loop = function
2779 if (y0
>= l
.pagedispy
&& y0
<= (l
.pagedispy
+ l
.pagevh))
2780 || ((y1
>= l
.pagedispy
&& y1
<= (l
.pagedispy
+ l
.pagevh)))
2782 match getopaque l
.pageno with
2783 | Some
(opaque, _
) when validopaque opaque ->
2784 let oy = -l
.pagey + l
.pagedispy
in
2786 (x0
- margin - state.x, y0
,
2787 x1
- margin - state.x, y1
) oy;
2797 let panx = float state.x in
2799 GlDraw.color (0.0, 0.0, 1.0) ~alpha
:0.5;
2800 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2802 (fun (pageno, c, (x0
, y0
, x1
, y1
, x2
, y2
, x3
, y3
)) ->
2804 if l
.pageno = pageno
2806 let d = float (l
.pagedispy
- l
.pagey) in
2807 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha
:0.5;
2808 GlDraw.begins `quads
;
2810 GlDraw.vertex2
(x0
+.panx, y0
+.d);
2811 GlDraw.vertex2
(x1
+.panx, y1
+.d);
2812 GlDraw.vertex2
(x2
+.panx, y2
+.d);
2813 GlDraw.vertex2
(x3
+.panx, y3
+.d);
2823 let showstrings trusted
active first pan
strings =
2825 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
2826 GlDraw.color (0., 0., 0.) ~alpha
:0.85;
2827 GlDraw.rect (0., 0.) (float conf.winw
, float conf.winh
);
2828 GlDraw.color (1., 1., 1.);
2829 Gl.enable `texture_2d
;
2831 let wx = measurestr
15 "w" in
2832 let tabx = 30.0*.wx +. float (pan
*15) in
2834 if row
= Array.length
strings || (row
- first) * 16 > conf.winh
2837 let (s, level
, _
) = strings.(row
) in
2838 let y = (row
- first) * 16 in
2839 let x = 5 + 15*(max
0 (level
+pan
)) in
2842 Gl.disable `texture_2d
;
2843 GlDraw.polygon_mode `both `
line;
2844 GlDraw.color (1., 1., 1.) ~alpha
:0.9;
2845 GlDraw.rect (0., float (y + 1))
2846 (float (conf.winw
- 1), float (y + 18));
2847 GlDraw.polygon_mode `both `fill
;
2848 GlDraw.color (1., 1., 1.);
2849 Gl.enable `texture_2d
;
2852 let drawtabularstring x s =
2856 let tabpos = try String.index
s '
\t'
with Not_found
-> -1 in
2859 let len = String.length
s - tabpos - 1 in
2860 let s1 = String.sub
s 0 tabpos
2861 and s2
= String.sub
s (tabpos + 1) len in
2862 let xx = wx +. drawstring1 14 x (y + 16) s1 in
2863 let x = truncate
(max
xx tabx) in
2864 drawstring1 15 x (y + 16) s2
2866 drawstring1 15 x (y + 16) s
2868 drawstring1 15 x (y + 16) s
2872 drawtabularstring (x + pan
*15) s;
2878 Gl.disable `texture_2d
;
2881 let showoutline (_, active, first, outlines, _, pan
, _) =
2882 showstrings false active first pan
outlines;
2885 let showitems (active, first, items, _, pan
, _) =
2886 showstrings true active first pan
items;
2890 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
2891 GlDraw.viewport
margin 0 state.w conf.winh
;
2893 GlClear.color (scalecolor2 conf.bgcolor
);
2894 GlClear.clear
[`
color];
2897 Gl.enable `scissor_test
;
2898 GlMisc.scissor
0 0 (conf.winw
- state.scrollw
) conf.winh
;
2900 List.iter
drawpage state.layout;
2903 Gl.disable `scissor_test
2907 let x = -.float state.x in
2908 GlMat.translate ~
x ();
2912 GlDraw.viewport
0 0 conf.winw
conf.winh
;
2915 begin match state.mode with
2916 | Items
items -> showitems items
2917 | Outline
outline -> showoutline outline
2921 Glut.swapBuffers
();
2925 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
2926 let x = x - margin - state.x in
2927 let rec f = function
2929 begin match getopaque l
.pageno with
2930 | Some
(opaque, _) when validopaque opaque ->
2931 let y = y - l
.pagedispy
in
2934 let y = l
.pagey + y in
2935 let x = x - l
.pagex
in
2936 match whatsunder
opaque x y with
2949 let viewmouse button bstate
x y =
2951 | Glut.OTHER_BUTTON
n when (n == 3 || n == 4) && bstate
= Glut.UP
->
2952 if Glut.getModifiers
() land Glut.active_ctrl
!= 0
2954 match state.mstate
with
2955 | Mzoom
(oldn
, i) ->
2963 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
2965 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
2967 let zoom = conf.zoom +. incr in
2969 state.mstate
<- Mzoom
(n, 0);
2971 state.mstate
<- Mzoom
(n, i+1);
2973 else state.mstate
<- Mzoom
(n, 0)
2975 | _ -> state.mstate
<- Mzoom
(n, 0)
2978 if state.ascrollstep
> 0
2980 setautoscrollspeed (n=4)
2984 then -conf.scrollstep
2985 else conf.scrollstep
2987 let incr = incr * 2 in
2988 let y = clamp incr in
2989 gotoy_and_clear_text y
2992 | Glut.LEFT_BUTTON
when Glut.getModifiers
() land Glut.active_ctrl
!= 0 ->
2993 if bstate
= Glut.DOWN
2995 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
2996 state.mstate
<- Mpan
(x, y)
2999 state.mstate
<- Mnone
3001 | Glut.LEFT_BUTTON
when x > conf.winw
- state.scrollw
->
3002 if bstate
= Glut.DOWN
3004 let position, sh = scrollph state.y in
3005 if y > truncate
position && y < truncate
(position +. sh)
3007 state.mstate
<- Mscroll
3009 let percent = float y /. float conf.winh
in
3010 let desty = truncate
(float (state.maxy - conf.winh
) *. percent) in
3012 state.mstate
<- Mscroll
3014 state.mstate
<- Mnone
3016 | Glut.LEFT_BUTTON
->
3017 let dest = if bstate
= Glut.DOWN
then getunder x y else Unone
in
3018 begin match dest with
3019 | Ulinkgoto
(pageno, top) ->
3023 gotopage1 pageno top;
3029 | Unone
when bstate
= Glut.DOWN
->
3030 Glut.setCursor
Glut.CURSOR_CROSSHAIR
;
3031 state.mstate
<- Mpan
(x, y);
3033 | Unone
| Utext
_ ->
3034 if bstate
= Glut.DOWN
3036 if conf.angle
mod 360 = 0
3038 state.mstate
<- Msel
((x, y), (x, y));
3039 Glut.postRedisplay
()
3043 match state.mstate
with
3046 | Mzoom
_ | Mscroll
->
3047 state.mstate
<- Mnone
3050 Glut.setCursor
Glut.CURSOR_INHERIT
;
3051 state.mstate
<- Mnone
3053 | Msel
((_, y0
), (_, y1
)) ->
3055 if (y0
>= l
.pagedispy
&& y0
<= (l
.pagedispy
+ l
.pagevh))
3056 || ((y1
>= l
.pagedispy
&& y1
<= (l
.pagedispy
+ l
.pagevh)))
3058 match getopaque l
.pageno with
3059 | Some
(opaque, _) when validopaque opaque ->
3063 List.iter
f state.layout;
3064 copysel
""; (* ugly *)
3065 Glut.setCursor
Glut.CURSOR_INHERIT
;
3066 state.mstate
<- Mnone
;
3073 let birdseyemouse button bstate
x y
3074 (conf, leftx
, _, hooverpageno
, anchor) =
3076 | Glut.LEFT_BUTTON
when bstate
= Glut.UP
->
3077 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
3078 let rec loop = function
3081 if y > l
.pagedispy
&& y < l
.pagedispy
+ l
.pagevh
3082 && x > margin && x < margin + l
.pagew
3084 leavebirdseye (conf, leftx
, l
.pageno, hooverpageno
, anchor) false;
3089 | Glut.OTHER_BUTTON
_ -> viewmouse button bstate
x y
3093 let mouse bstate button
x y =
3094 match state.mode with
3095 | View
-> viewmouse button bstate
x y
3096 | Birdseye beye
-> birdseyemouse button bstate
x y beye
3097 | Textentry
_ | Outline
_ | Items
_ -> ()
3100 let mouse ~button ~
state ~
x ~
y = mouse state button
x y;;
3103 match state.mode with
3106 match state.mstate
with
3107 | Mzoom
_ | Mnone
-> ()
3112 state.mstate
<- Mpan
(x, y);
3113 if conf.zoom > 1.0 then state.x <- state.x + dx;
3115 gotoy_and_clear_text y
3118 state.mstate
<- Msel
(a, (x, y));
3119 Glut.postRedisplay
()
3122 let y = min
conf.winh
(max
0 y) in
3123 let percent = float y /. float conf.winh
in
3124 let y = truncate
(float (state.maxy - conf.winh
) *. percent) in
3125 gotoy_and_clear_text y
3129 match state.mode with
3130 | Birdseye
(conf, leftx
, pageno, hooverpageno
, anchor) ->
3131 let margin = (conf.winw
- (state.w + state.scrollw
)) / 2 in
3132 let rec loop = function
3134 if hooverpageno
!= -1
3136 state.mode <- Birdseye
(conf, leftx
, pageno, -1, anchor);
3137 Glut.postRedisplay
();
3140 if y > l
.pagedispy
&& y < l
.pagedispy
+ l
.pagevh
3141 && x > margin && x < margin + l
.pagew
3143 state.mode <- Birdseye
(conf, leftx
, pageno, l
.pageno, anchor);
3144 Glut.postRedisplay
();
3150 | Outline
_ | Items
_ | Textentry
_ -> ()
3152 match state.mstate
with
3154 begin match getunder x y with
3155 | Unone
-> Glut.setCursor
Glut.CURSOR_INHERIT
3157 if conf.underinfo
then showtext 'u'
("ri: " ^ uri
);
3158 Glut.setCursor
Glut.CURSOR_INFO
3159 | Ulinkgoto
(page
, _) ->
3161 then showtext 'p'
("age: " ^ string_of_int page
);
3162 Glut.setCursor
Glut.CURSOR_INFO
3164 if conf.underinfo
then showtext '
f'
("ont: " ^
s);
3165 Glut.setCursor
Glut.CURSOR_TEXT
3168 | Mpan
_ | Msel
_ | Mzoom
_ | Mscroll
->
3179 match Sys.os_type
with
3180 | "Win32" -> Sys.getenv
"HOMEPATH"
3181 | _ -> Sys.getenv
"HOME"
3184 ("Can not determine home directory location: " ^
3185 Printexc.to_string exn
);
3189 let config_of c attrs
=
3193 | "scroll-bar-width" -> { c with scrollbw
= max
0 (int_of_string
v) }
3194 | "scroll-handle-height" -> { c with scrollh
= max
0 (int_of_string
v) }
3195 | "case-insensitive-search" -> { c with icase
= bool_of_string
v }
3196 | "preload" -> { c with preload = bool_of_string
v }
3197 | "page-bias" -> { c with pagebias
= int_of_string
v }
3198 | "scroll-step" -> { c with scrollstep
= max
1 (int_of_string
v) }
3199 | "auto-scroll-step" ->
3200 { c with autoscrollstep
= max
0 (int_of_string
v) }
3201 | "max-height-fit" -> { c with maxhfit
= bool_of_string
v }
3202 | "crop-hack" -> { c with crophack
= bool_of_string
v }
3203 | "throttle" -> { c with showall
= bool_of_string
v }
3204 | "highlight-links" -> { c with hlinks
= bool_of_string
v }
3205 | "under-cursor-info" -> { c with underinfo
= bool_of_string
v }
3206 | "vertical-margin" ->
3207 { c with interpagespace = max
0 (int_of_string
v) }
3209 let zoom = float_of_string
v /. 100. in
3210 let zoom = max
0.01 (min
2.2 zoom) in
3211 { c with zoom = zoom }
3212 | "presentation" -> { c with presentation = bool_of_string
v }
3213 | "rotation-angle" -> { c with angle
= int_of_string
v }
3214 | "width" -> { c with winw
= max
20 (int_of_string
v) }
3215 | "height" -> { c with winh
= max
20 (int_of_string
v) }
3216 | "persistent-bookmarks" -> { c with savebmarks
= bool_of_string
v }
3217 | "proportional-display" -> { c with proportional
= bool_of_string
v }
3218 | "pixmap-cache-size" -> { c with memlimit
= max
2 (int_of_string
v) }
3219 | "tex-count" -> { c with texcount
= max
1 (int_of_string
v) }
3220 | "slice-height" -> { c with sliceheight
= max
2 (int_of_string
v) }
3221 | "thumbnail-width" -> { c with thumbw
= max
2 (int_of_string
v) }
3222 | "persistent-location" -> { c with jumpback
= bool_of_string
v }
3223 | "background-color" -> { c with bgcolor
= color_of_string v }
3224 | "scrollbar-in-presentation" ->
3225 { c with scrollbarinpm
= bool_of_string
v }
3226 | "ui-font" -> { c with uifont
= v }
3229 prerr_endline
("Error processing attribute (`" ^
3230 k ^
"'=`" ^
v ^
"'): " ^
Printexc.to_string exn
);
3233 let rec fold c = function
3236 let c = apply c k
v in
3242 let fromstring f pos
n v d =
3245 dolog "Error processing attribute (%S=%S) at %d\n%s"
3246 n v pos
(Printexc.to_string exn
)
3251 let bookmark_of attrs
=
3252 let rec fold title page rely
= function
3253 | ("title", v) :: rest
-> fold v page rely rest
3254 | ("page", v) :: rest
-> fold title v rely rest
3255 | ("rely", v) :: rest
-> fold title page
v rest
3256 | _ :: rest
-> fold title page rely rest
3257 | [] -> title, page
, rely
3259 fold "invalid" "0" "0" attrs
3263 let rec fold path page rely pan
= function
3264 | ("path", v) :: rest
-> fold v page rely pan rest
3265 | ("page", v) :: rest
-> fold path
v rely pan rest
3266 | ("rely", v) :: rest
-> fold path page
v pan rest
3267 | ("pan", v) :: rest
-> fold path page rely
v rest
3268 | _ :: rest
-> fold path page rely pan rest
3269 | [] -> path
, page
, rely
, pan
3271 fold "" "0" "0" "0" attrs
3274 let setconf dst src
=
3275 dst
.scrollbw
<- src
.scrollbw
;
3276 dst
.scrollh
<- src
.scrollh
;
3277 dst
.icase
<- src
.icase
;
3278 dst
.preload <- src
.preload;
3279 dst
.pagebias
<- src
.pagebias
;
3280 dst
.verbose
<- src
.verbose
;
3281 dst
.scrollstep
<- src
.scrollstep
;
3282 dst
.maxhfit
<- src
.maxhfit
;
3283 dst
.crophack
<- src
.crophack
;
3284 dst
.autoscrollstep
<- src
.autoscrollstep
;
3285 dst
.showall
<- src
.showall
;
3286 dst
.hlinks
<- src
.hlinks
;
3287 dst
.underinfo
<- src
.underinfo
;
3288 dst
.interpagespace <- src
.interpagespace;
3289 dst
.zoom <- src
.zoom;
3290 dst
.presentation <- src
.presentation;
3291 dst
.angle
<- src
.angle
;
3292 dst
.winw
<- src
.winw
;
3293 dst
.winh
<- src
.winh
;
3294 dst
.savebmarks
<- src
.savebmarks
;
3295 dst
.memlimit
<- src
.memlimit
;
3296 dst
.proportional
<- src
.proportional
;
3297 dst
.texcount
<- src
.texcount
;
3298 dst
.sliceheight
<- src
.sliceheight
;
3299 dst
.thumbw
<- src
.thumbw
;
3300 dst
.jumpback
<- src
.jumpback
;
3301 dst
.bgcolor
<- src
.bgcolor
;
3302 dst
.scrollbarinpm
<- src
.scrollbarinpm
;
3303 dst
.uifont
<- src
.uifont
;
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 Printf.bprintf bb
"\n %s='%s'" s a
3510 then dc.winw
, dc.winh
3512 match state.fullscreen
with
3514 | None
-> c.winw
, c.winh
3516 let zoom, presentation, interpagespace, showall
=
3518 then dc.zoom, dc.presentation, dc.interpagespace, dc.showall
3520 match state.mode with
3521 | Birdseye
(bc
, _, _, _, _) ->
3522 bc
.zoom, bc
.presentation, bc
.interpagespace, bc
.showall
3523 | _ -> c.zoom, c.presentation, c.interpagespace, c.showall
3525 oi
"width" w dc.winw
;
3526 oi
"height" h dc.winh
;
3527 oi
"scroll-bar-width" c.scrollbw
dc.scrollbw
;
3528 oi
"scroll-handle-height" c.scrollh
dc.scrollh
;
3529 ob "case-insensitive-search" c.icase
dc.icase
;
3530 ob "preload" c.preload dc.preload;
3531 oi
"page-bias" c.pagebias
dc.pagebias
;
3532 oi
"scroll-step" c.scrollstep
dc.scrollstep
;
3533 oi
"auto-scroll-step" c.autoscrollstep
dc.autoscrollstep
;
3534 ob "max-height-fit" c.maxhfit
dc.maxhfit
;
3535 ob "crop-hack" c.crophack
dc.crophack
;
3536 ob "throttle" showall
dc.showall
;
3537 ob "highlight-links" c.hlinks
dc.hlinks
;
3538 ob "under-cursor-info" c.underinfo
dc.underinfo
;
3539 oi
"vertical-margin" interpagespace dc.interpagespace;
3540 oz
"zoom" zoom dc.zoom;
3541 ob "presentation" presentation dc.presentation;
3542 oi
"rotation-angle" c.angle
dc.angle
;
3543 ob "persistent-bookmarks" c.savebmarks
dc.savebmarks
;
3544 ob "proportional-display" c.proportional
dc.proportional
;
3545 oi
"pixmap-cache-size" c.memlimit
dc.memlimit
;
3546 oi
"texcount" c.texcount
dc.texcount
;
3547 oi
"slice-height" c.sliceheight
dc.sliceheight
;
3548 oi
"thumbnail-width" c.thumbw
dc.thumbw
;
3549 ob "persistent-location" c.jumpback
dc.jumpback
;
3550 oc
"background-color" c.bgcolor
dc.bgcolor
;
3551 ob "scrollbar-in-presentation" c.scrollbarinpm
dc.scrollbarinpm
;
3552 os
"ui-font" c.uifont
dc.uifont
;
3556 let bb = Buffer.create
32768 in
3558 let dc = if conf.bedefault
then conf else dc in
3559 Buffer.add_string
bb "<llppconfig>\n<defaults ";
3560 add_attrs bb true dc dc;
3561 Buffer.add_string
bb "/>\n";
3563 let adddoc path pan
anchor c bookmarks =
3564 if bookmarks == [] && c = dc && anchor = emptyanchor
3567 Printf.bprintf
bb "<doc path='%s'"
3568 (enent
path 0 (String.length
path));
3570 if anchor <> emptyanchor
3572 let n, y = anchor in
3573 Printf.bprintf
bb " page='%d'" n;
3576 Printf.bprintf
bb " rely='%f'" y
3581 then Printf.bprintf
bb " pan='%d'" pan
;
3583 add_attrs bb false dc c;
3585 begin match bookmarks with
3586 | [] -> Buffer.add_string
bb "/>\n"
3588 Buffer.add_string
bb ">\n<bookmarks>\n";
3589 List.iter
(fun (title, _level
, (page, rely
)) ->
3591 "<item title='%s' page='%d'"
3592 (enent
title 0 (String.length
title))
3597 Printf.bprintf
bb " rely='%f'" rely
3599 Buffer.add_string
bb "/>\n";
3601 Buffer.add_string
bb "</bookmarks>\n</doc>\n";
3607 match state.mode with
3608 | Birdseye
(_, pan, _, _, _) -> pan
3611 let basename = Filename.basename state.path in
3612 adddoc basename pan (getanchor ())
3615 if state.ascrollstep
> 0
3616 then state.ascrollstep
3617 else conf.autoscrollstep
}
3618 (if conf.savebmarks
then state.bookmarks else []);
3620 Hashtbl.iter
(fun path (c, bookmarks, x, y) ->
3622 then adddoc path x y c bookmarks
3624 Buffer.add_string
bb "</llppconfig>";
3627 if Buffer.length
bb > 0
3630 let tmp = path ^
".tmp" in
3631 let oc = open_out_bin
tmp in
3632 Buffer.output_buffer
oc bb;
3634 Sys.rename
tmp path;
3637 ("error while saving configuration: " ^
Printexc.to_string exn
)
3644 [("-p", Arg.String
(fun s -> state.password
<- s) , " Set password")
3645 ;("-v", Arg.Unit
(fun () -> print_endline
Help.version
; exit
0),
3646 " Print version and exit")]
3648 (fun s -> state.path <- s)
3649 ("Usage: " ^
Sys.argv
.(0) ^
" [options] some.pdf\nOptions:")
3651 if String.length
state.path = 0
3652 then (prerr_endline
"file name missing"; exit
1);
3656 let _ = Glut.init
Sys.argv
in
3657 let () = Glut.initDisplayMode ~depth
:false ~double_buffer
:true () in
3658 let () = Glut.initWindowSize
conf.winw
conf.winh
in
3659 let _ = Glut.createWindow
("llpp " ^
Filename.basename state.path) in
3662 if Sys.os_type
= "Unix"
3664 Unix.socketpair
Unix.PF_UNIX
Unix.SOCK_STREAM
0
3666 let addr = Unix.ADDR_INET
(Unix.inet_addr_loopback
, 1337) in
3667 let sock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
3668 Unix.setsockopt
sock Unix.SO_REUSEADDR
true;
3669 Unix.bind
sock addr;
3671 let csock = Unix.socket
Unix.PF_INET
Unix.SOCK_STREAM
0 in
3672 Unix.connect
csock addr;
3673 let ssock, _ = Unix.accept
sock in
3676 Unix.setsockopt
sock Unix.TCP_NODELAY
true;
3677 Unix.setsockopt_optint
sock Unix.SO_LINGER None
;
3681 at_exit
(fun () -> Unix.shutdown
ssock Unix.SHUTDOWN_ALL
);
3685 let () = Glut.displayFunc
display in
3686 let () = Glut.reshapeFunc
reshape in
3687 let () = Glut.keyboardFunc
keyboard in
3688 let () = Glut.specialFunc
special in
3689 let () = Glut.idleFunc
(Some
idle) in
3690 let () = Glut.mouseFunc
mouse in
3691 let () = Glut.motionFunc
motion in
3692 let () = Glut.passiveMotionFunc
pmotion in
3695 conf.angle
, conf.proportional
, conf.texcount
,
3696 conf.sliceheight
, conf.uifont
3698 state.csock <- csock;
3699 state.ssock <- ssock;
3700 state.text <- "Opening " ^
state.path;
3701 writeopen state.path state.password
;
3705 let rec handlelablglutbug () =
3708 with Glut.BadEnum
"key in special_of_int" ->
3709 showtext '
!'
" LablGlut bug: special key not recognized";
3710 handlelablglutbug ()
3712 handlelablglutbug ();