6 let sh = float (maxy
+ state
.winh
) /. float state
.winh
in
7 let sh = float state
.winh
/. sh in
8 let sh = max
sh (float conf
.scrollh
) in
10 let percent = float y
/. float maxy
in
11 let position = (float state
.winh
-. sh) *. percent in
14 if position +. sh > float state
.winh
15 then float state
.winh
-. sh
21 let isbirdseye = function
23 | Textentry _
| View
| LinkNav _
-> false
26 let istextentry = function
28 | Birdseye _
| View
| LinkNav _
-> false
32 if state
.uioh#alwaysscrolly
|| ((conf
.scrollb
land scrollbvv
!= 0)
33 && (state
.maxy
> state
.winh
))
41 else x
> state
.winw
- vscrollw ()
44 let firstof first active
=
45 if first
> active
|| abs
(first
- active
) > fstate
.maxrows
- 1
46 then max
0 (active
- (fstate
.maxrows
/2))
50 let calcfirst first active
=
53 let rows = active
- first
in
54 if rows > fstate
.maxrows
then active
- fstate
.maxrows
else first
59 let len = String.length state
.text
in
60 let x0 = if conf
.leftscroll
then vscrollw () else 0 in
64 | Textentry _
| View
| LinkNav _
->
65 let h, _
, _
= state
.uioh#scrollpw
in
71 x
(float (state
.winh
- (fstate
.fontsize
+ 4) - hscrollh))
72 (x
+.w
) (float (state
.winh
- hscrollh))
75 let w = float (state
.winw
- 1 - vscrollw ()) in
76 if state
.progress
>= 0.0 && state
.progress
< 1.0
78 GlDraw.color
(0.3, 0.3, 0.3);
79 let w1 = w *. state
.progress
in
81 GlDraw.color
(0.0, 0.0, 0.0);
82 rect (float x0+.w1) (float x0+.w-.w1)
85 GlDraw.color
(0.0, 0.0, 0.0);
89 GlDraw.color
(1.0, 1.0, 1.0);
92 (if conf
.leftscroll
then x0 + 2 else x0 + if len > 0 then 8 else 2)
93 (state
.winh
- hscrollh - 5) s
;
97 | Textentry
((prefix
, text
, _
, _
, _
, _
), _
) ->
100 then Printf.sprintf
"%s%s_ [%s]" prefix text state
.text
101 else Printf.sprintf
"%s%s_" prefix text
105 | Birdseye _
| View
| LinkNav _
-> state
.text
110 if not
(istextentry state
.mode
) && state
.uioh#eformsgs
112 let s1 = "(press 'e' to review error messasges)" in
113 if nonemptystr
s then s ^
" " ^
s1 else s1
122 let textentrykeyboard
123 key mask
((c
, text
, opthist
, onkey
, ondone
, cancelonempty
), onleave
) =
126 state
.mode
<- Textentry
(te
, onleave
);
128 postRedisplay
"textentrykeyboard enttext";
133 | Some
(action
, _
) ->
134 state
.mode
<- Textentry
(
135 (c
, action cmd
, opthist
,
136 onkey
, ondone
, cancelonempty
),
138 postRedisplay
"textentry histaction"
141 let kt = Wsi.kc2kt key
in
142 match [@warning
"-4"] kt with
144 if emptystr text
&& cancelonempty
147 postRedisplay
"textentrykeyboard after cancel";
150 let s = withoutlastutf8 text
in
151 enttext (c
, s, opthist
, onkey
, ondone
, cancelonempty
)
156 postRedisplay
"textentrykeyboard after confirm"
158 | Up
-> histaction HCprev
159 | Down
-> histaction HCnext
160 | Home
-> histaction HCfirst
161 | End
-> histaction HClast
166 begin match opthist
with
168 | Some
(_
, onhistcancel
) -> onhistcancel
()
172 postRedisplay
"textentrykeyboard after cancel2"
174 else enttext (c
, E.s, opthist
, onkey
, ondone
, cancelonempty
)
178 | Insert
when Wsi.withshift mask
->
179 let s = getcmdoutput
(fun s ->
180 prerr_endline
("error pasting: " ^
s)) conf
.pastecmd
in
181 enttext (c
, s, opthist
, onkey
, ondone
, cancelonempty
)
183 | Code _
| Ascii _
->
184 begin match onkey text
kt with
188 postRedisplay
"textentrykeyboard after confirm2";
191 enttext (c
, text
, opthist
, onkey
, ondone
, cancelonempty
);
195 postRedisplay
"textentrykeyboard after cancel3";
198 state
.mode
<- Textentry
(te
, onleave
);
199 postRedisplay
"textentrykeyboard switch";
201 | _
-> vlog
"unhandled key"
204 class type lvsource
=
206 method getitemcount
: int
207 method getitem
: int -> (string * int)
208 method hasaction
: int -> bool
209 method exit
: uioh
:uioh
->
215 method getactive
: int
216 method getfirst
: int
218 method getminfo
: (int * int) array
221 class virtual lvsourcebase
=
223 val mutable m_active
= 0
224 val mutable m_first
= 0
225 val mutable m_pan
= 0
226 method getactive
= m_active
227 method getfirst
= m_first
228 method getpan
= m_pan
229 method getminfo
: (int * int) array
= E.a
232 let coe s = (s :> uioh
);;
234 class listview ~zebra ~helpmode ~
(source
:lvsource
) ~trusted ~modehash
=
236 val m_pan
= source#getpan
237 val m_first
= source#getfirst
238 val m_active
= source#getactive
240 val m_prev_uioh
= state
.uioh
242 method private elemunder y
=
246 let n = y
/ (fstate
.fontsize
+1) in
247 if m_first
+ n < source#getitemcount
249 if source#hasaction
(m_first
+ n)
250 then Some
(m_first
+ n)
257 GlFunc.blend_func ~src
:`src_alpha ~dst
:`one_minus_src_alpha
;
258 GlDraw.color
(0., 0., 0.) ~alpha
:0.85;
259 filledrect
0. 0. (float state
.winw
) (float state
.winh
);
260 GlDraw.color
(1., 1., 1.);
261 Gl.enable `texture_2d
;
262 let fs = fstate
.fontsize
in
264 let hw = state
.winw
/3 in
265 let ww = fstate
.wwidth
in
266 let tabw = 17.0*.ww in
267 let itemcount = source#getitemcount
in
268 let minfo = source#getminfo
in
272 GlMat.translate ~x
:(float conf
.scrollbw
) ();
274 let x0 = 0.0 and x1
= float (state
.winw
- conf
.scrollbw
- 1) in
276 if not
((row
- m_first
) > fstate
.maxrows
)
278 if row
>= 0 && row
< itemcount
280 let (s, level
) = source#getitem row
in
281 let y = (row
- m_first
) * nfs in
282 let x = 5.0 +. (float (level
+ m_pan
)) *. ww in
285 (let c = if row
land 1 = 0 then 1.0 else 0.92 in (c,c,c));
289 Gl.disable `texture_2d
;
290 let alpha = if source#hasaction row
then 0.9 else 0.3 in
291 GlDraw.color
(1., 1., 1.) ~
alpha;
292 linerect
(x0 +. 1.) (float (y + 1)) (x1
) (float (y + fs + 3));
293 Gl.enable `texture_2d
;
296 if zebra
&& row
land 1 = 1
300 GlDraw.color
(c,c,c);
301 let drawtabularstring s =
303 let x'
= truncate
(x0 +. x) in
304 let s1, s2
= splitatchar
s '
\000'
in
306 then drawstring1
fs x'
(y+nfs) s
312 let s'
= withoutlastutf8
s in
313 let s = s' ^
Utf8syms.ellipsis
in
314 let w = measurestr
fs s in
315 if float x'
+. w +. ww < float (hw + x'
)
320 if float x'
+. ww +. measurestr
fs s1 > float (hw + x'
)
324 ignore
(drawstring1
fs x'
(y+nfs) s1);
325 drawstring1
fs (hw + x'
) (y+nfs) s2
329 let x = if helpmode
&& row
> 0 then x +. ww else x in
330 let s1, s2
= splitatchar
s '
\t'
in
333 let nx = drawstr x s1 in
335 let x = x +. (max
tabw sw) in
338 let len = String.length
s - 2 in
339 if len > 0 && s.[0] = '
\xc2'
&& s.[1] = '
\xb7'
341 let s = String.sub
s 2 len in
342 let x = if not helpmode
then x +. ww else x in
343 GlDraw.color
(1.2, 1.2, 1.2);
344 let vinc = drawstring1
(fs+fs/4)
345 (truncate
(x -. ww)) (y+nfs) s in
346 GlDraw.color
(1., 1., 1.);
347 vinc +. (float fs *. 0.8)
351 ignore
(drawtabularstring s);
357 GlDraw.color
(1.0, 1.0, 1.0) ~
alpha:0.5;
360 if (row
- m_first
) <= fstate
.maxrows
362 if row
>= 0 && row
< itemcount
364 let (s, level
) = source#getitem row
in
365 let pos0 = Ne.index
s '
\000'
in
366 let y = (row
- m_first
) * nfs in
367 let x = float (level
+ m_pan
) *. ww in
368 let (first
, last
) = minfo.(row
) in
370 if pos0 > 0 && first
> pos0
371 then String.sub
s (pos0+1) (first
-pos0-1)
372 else String.sub
s 0 first
374 let suffix = String.sub
s first
(last
- first
) in
375 let w1 = measurestr fstate
.fontsize
prefix in
376 let w2 = measurestr fstate
.fontsize
suffix in
377 let x = x +. if conf
.leftscroll
then xadj else 5.0 in
378 let x = if pos0 > 0 && first
> pos0 then x +. float hw else x in
380 and y0
= float (y+2) in
382 and y1
= float (y+fs+3) in
383 filledrect
x0 y0
x1 y1
;
386 Gl.disable `texture_2d
;
387 if Array.length
minfo > 0 then loop m_first
;
392 method nextcurlevel incr
=
393 let len = source#getitemcount
in
395 if m_active
>= 0 && m_active
< len
396 then snd
(source#getitem m_active
)
406 let _, l
= source#getitem i
in
407 if l
<= curlevel then i
else flow (i
+incr
)
410 let active = flow (m_active
+incr
) in
411 let first = calcfirst m_first
active in
412 postRedisplay
"outline nextcurlevel";
413 {< m_active
= active; m_first
= first >}
415 method updownlevel incr
=
416 let len = source#getitemcount
in
418 if m_active
>= 0 && m_active
< len
419 then snd
(source#getitem m_active
)
426 if i
= -1 then 0 else
427 let _, l
= source#getitem i
in
428 if l
!= curlevel then i
else flow (i
+incr
)
431 let active = flow m_active
in
432 let first = calcfirst m_first
active in
433 postRedisplay
"outline updownlevel";
434 {< m_active
= active; m_first
= first >}
436 method private key1 key mask
=
437 let set1 active first qsearch
=
438 coe {< m_active
= active; m_first
= first; m_qsearch
= qsearch
>}
440 let search active pattern incr
=
441 let active = if active = -1 then m_first
else active in
444 if n >= 0 && n < source#getitemcount
446 let s, _ = source#getitem
n in
447 match Str.search_forward re
s 0 with
448 | exception Not_found
-> loop (n + incr
)
455 let qpat = Str.quote pattern
in
456 match Str.regexp_case_fold
qpat with
459 dolog
"regexp_case_fold for `%S' failed: %S\n" qpat @@
460 Printexc.to_string exn
;
463 let itemcount = source#getitemcount
in
464 let find start incr
=
466 if i
= -1 || i
= itemcount
469 if source#hasaction i
476 let set active first =
477 let first = bound
first 0 (itemcount - fstate
.maxrows
) in
479 coe {< m_active
= active; m_first
= first; m_qsearch
= E.s >}
482 let isvisible first n = n >= first && n - first <= fstate
.maxrows
in
484 let incr1 = if incr
> 0 then 1 else -1 in
485 if isvisible m_first m_active
488 let next = m_active
+ incr
in
490 if next < 0 || next >= itemcount
494 if abs
(m_active
- next) > fstate
.maxrows
500 let first = m_first
+ incr
in
501 let first = bound
first 0 (itemcount - fstate
.maxrows
) in
503 let next = m_active
+ incr
in
504 let next = bound
next 0 (itemcount - 1) in
511 if isvisible first next
518 let first = min
next m_first
in
520 if abs
(next - first) > fstate
.maxrows
526 let first = m_first
+ incr
in
527 let first = bound
first 0 (itemcount - 1) in
529 let next = m_active
+ incr
in
530 let next = bound
next 0 (itemcount - 1) in
531 let next = find next incr1 in
533 if next = -1 || abs
(m_active
- first) > fstate
.maxrows
535 let active = if m_active
= -1 then next else m_active
in
540 if isvisible first active
546 postRedisplay
"listview navigate";
550 let kt = Wsi.kc2kt key
in
551 match [@warning
"-4"] kt with
552 | Ascii
(('r'
|'
s'
) as c) when Wsi.withctrl mask
->
553 let incr = if c = 'r'
then -1 else 1 in
555 match search (m_active
+ incr) m_qsearch
incr with
557 state
.text
<- m_qsearch ^
" [not found]";
560 state
.text
<- m_qsearch
;
561 active, firstof m_first
active
563 postRedisplay
"listview ctrl-r/s";
564 set1 active first m_qsearch
;
566 | Insert
when Wsi.withctrl mask
->
567 if m_active
>= 0 && m_active
< source#getitemcount
569 let s, _ = source#getitem m_active
in
570 selstring conf
.selcmd
s;
575 if emptystr m_qsearch
578 let qsearch = withoutlastutf8 m_qsearch
in
582 postRedisplay
"listview empty qsearch";
583 set1 m_active m_first
E.s;
587 match search m_active
qsearch ~
-1 with
589 state
.text
<- qsearch ^
" [not found]";
592 state
.text
<- qsearch;
593 active, firstof m_first
active
595 postRedisplay
"listview backspace qsearch";
596 set1 active first qsearch
599 | Ascii
_ | Code
_ ->
601 match [@warning
"-8"] kt with
602 | Ascii
c -> String.make
1 c
603 | Code code
-> toutf8 code
605 let pattern = m_qsearch ^
utf8 in
607 match search m_active
pattern 1 with
609 state
.text
<- pattern ^
" [not found]";
612 state
.text
<- pattern;
613 active, firstof m_first
active
615 postRedisplay
"listview qsearch add";
616 set1 active first pattern;
620 if emptystr m_qsearch
622 postRedisplay
"list view escape";
624 let mx, my = state.mpos in
627 getoptdef m_prev_uioh
@@
628 source#exit ~uioh
:(coe self
) ~cancel
:true ~
active:m_active
629 ~
first:m_first ~pan
:m_pan
632 postRedisplay
"list view kill qsearch";
633 coe {< m_qsearch
= E.s >}
638 let self = {< m_qsearch
= E.s >} in
640 postRedisplay
"listview enter";
641 let cancel = not
(m_active
>= 0 && m_active
< source#getitemcount
) in
642 source#exit ~uioh
:(coe self) ~
cancel
643 ~
active:m_active ~
first:m_first ~pan
:m_pan
;
645 getoptdef m_prev_uioh
opt
650 | Prior
-> navigate ~
-(fstate
.maxrows
)
651 | Next
-> navigate fstate
.maxrows
655 postRedisplay
"listview right";
656 coe {< m_pan
= m_pan
- 1 >}
660 postRedisplay
"listview left";
661 coe {< m_pan
= m_pan
+ 1 >}
664 let active = find 0 1 in
665 postRedisplay
"listview home";
669 let first = max
0 (itemcount - fstate
.maxrows
) in
670 let active = find (itemcount - 1) ~
-1 in
671 postRedisplay
"listview end";
676 method key key mask
=
677 match state
.mode
with
679 textentrykeyboard key mask te
;
681 | Birdseye
_ | View
| LinkNav
_ -> self#key1 key mask
683 method button button down
x y _ =
686 | 1 when vscrollhit x ->
687 postRedisplay
"listview scroll";
690 let _, position, sh = self#
scrollph in
691 if y > truncate
position && y < truncate
(position +. sh)
693 state
.mstate
<- Mscrolly
;
697 let s = float (max
0 (y - conf
.scrollh
)) /. float state
.winh
in
698 let first = truncate
(s *. float source#getitemcount
) in
699 let first = min source#getitemcount
first in
700 Some
(coe {< m_first
= first; m_active
= first >})
702 state
.mstate
<- Mnone
;
706 begin match self#elemunder
y with
708 postRedisplay
"listview click";
709 source#exit ~uioh
:(coe {< m_active
= n >})
710 ~
cancel:false ~
active:n ~
first:m_first ~pan
:m_pan
714 | n when (n == 4 || n == 5) && not down
->
715 let len = source#getitemcount
in
717 if n = 5 && m_first
+ fstate
.maxrows
>= len
720 let first = m_first
+ (if n == 4 then -1 else 1) in
721 bound
first 0 (len - 1)
723 postRedisplay
"listview wheel";
724 Some
(coe {< m_first
= first >})
725 | n when (n = 6 || n = 7) && not down
->
726 let inc = if n = 7 then -1 else 1 in
727 postRedisplay
"listview hwheel";
728 Some
(coe {< m_pan
= m_pan
+ inc >})
729 | _ -> Some
(coe self)
731 getoptdef m_prev_uioh
opt
733 method multiclick
_ x y = self#button
1 true x y
736 match state
.mstate
with
738 let s = float (max
0 (y - conf
.scrollh
)) /. float state
.winh
in
739 let first = truncate
(s *. float source#getitemcount
) in
740 let first = min source#getitemcount
first in
741 postRedisplay
"listview motion";
742 coe {< m_first
= first; m_active
= first >}
751 if x < state
.winw
- conf
.scrollbw
754 match self#elemunder
y with
755 | None
-> Wsi.setcursor
Wsi.CURSOR_INHERIT
; m_active
756 | Some
n -> Wsi.setcursor
Wsi.CURSOR_INFO
; n
760 then (postRedisplay
"listview pmotion"; {< m_active
= n >})
765 Wsi.setcursor
Wsi.CURSOR_INHERIT
;
769 method infochanged
_ = ()
771 method scrollpw
= (0, 0.0, 0.0)
773 let nfs = fstate
.fontsize
+ 1 in
774 let y = m_first
* nfs in
775 let itemcount = source#getitemcount
in
776 let maxi = max
0 (itemcount - fstate
.maxrows
) in
777 let maxy = maxi * nfs in
778 let p, h = scrollph y maxy in
781 method modehash
= modehash
782 method eformsgs
= false
783 method alwaysscrolly
= true
786 if dy
!= 0 then begin
787 let len = source#getitemcount
in
789 if dy
> 0 && m_first
+ fstate
.maxrows
>= len
792 let first = m_first
+ dy
/ 10 in
793 bound
first 0 (len - 1)
795 postRedisplay
"listview wheel";
796 {< m_first
= first >}
802 method zoom
_ _ _ = ()