aboutsummaryrefslogtreecommitdiff
path: root/extensions/gui/ix/kbd.wid
blob: ab5f6a7f99935b50aefbf46e75aed1efa529191b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
namespace eval ::ix {
    proc kbd_play {t m b x y} {
	variable _
	foreach a {{rs {0 sunken 1 raised}} {sr {0 raised 1 sunken}} {v {0 0 1 {($y - [winfo rooty $w]) / ([winfo height $w] + 0.0)}}}} {array set [lindex $a 0] [lindex $a 1]}
	set w [winfo containing $x $y]; set tp $t.rp
	if {$m != 1 && $_($t:lp) != -1 && $_($t:lp) ne $w} {
	    $_($t:lp) config -relief raised;pd "$tp _cb [winfo name $_($t:lp)] 0 \;"}
	if {[$w cget -relief] eq $rs($b)} {
	    eval $w config -relief $sr($b); pd "$tp _cb [winfo name $w] [eval expr $v($b)] \;"}
	set _($t:lp) $w}

    proc kbd_off {p t octaves} {
	for {set o 0} {$o < $octaves} {incr o} {	
	    for {set on 0} {$on < 12} {incr on} {
		set no [expr $o * 12 + $on]
	        if {[$p.f$o.$no cget -relief] eq "sunken"} {
		    $p.f$o.$no config -relief raised
		    pd "$t.rp _cb $no 0 \;"}}}}

    proc kbd {path t octaves b f} {
	if {![winfo exists $path.f0]} {
	    variable _
	    set _($t:lp) -1
	    $path config -width [expr $octaves * 66]
	    set bw {0 1 0 1 0 0 1 0 1 0 1 0} ; set npl {0 1 1 2 2 3 4 4 5 5 6 6}
	    foreach a {{bg "0 $b 1 $f"} {fg "1 $b 0 $f"} {an {0 ";lower $wk" 1 "-anchor n"}} {rw {0 {[expr 1 / 7.]} 1 {[expr 1 / 10.]}}} {rh {0 {[expr 1.]} 1 {[expr 6 / 10.]}}}} {array set [lindex $a 0] [lindex $a 1]}
	    for {set o 0} {$o < $octaves} {incr o} {
		set w $path.f$o ; frame $w -bd 0 -height 66 -width 66
		for {set on 0} {$on < 12} {incr on} {
		    set wk $w.[expr $o * 12 + $on] ; set n [lindex $bw $on]
		    eval "label $wk -bg $bg($n) -fg $fg($n) -bd 1 -relief raised;place $wk -relx [expr [lindex $npl $on] / 7.] -y 0 -relwidth $rw($n) -relheight $rh($n) $an($n)"
		    bind $wk <1> "::ix::kbd_play $t 0 1 %X %Y"; bind $wk <B1-Motion> [bind $wk <1>];bind $wk <ButtonRelease-1> "::ix::kbd_play $t 0 0 %X %Y"; bind $wk <Enter> "::ix::kbd_play $t 1 0 %X %Y"; bind $wk <3> "::ix::kbd_play $t 1 1 %X %Y"; bind $wk <B3-Motion> [bind $wk <3>]; bind $wk <2> "::ix::kbd_off $path $t $octaves"}
		place $w -y 0 -relheight 1.0 -relx [expr $o / $octaves.0] -relwidth [expr 1 / $octaves.0]}}}}

#> kbd frame
#. -height 100 -width 60
#. #octaves 6 #bg black #fg white

::ix::kbd .- .| .#octaves .#bg .#fg