aboutsummaryrefslogtreecommitdiff
path: root/extensions/gui/ix/pd_objects.tk
blob: a6574050be95cf1f03079ff1b9b4bcd17b07ad8f (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
variable obj
set obj {

    rect {
	tags {
	    box img
	}
	attributes {
	    x 1 y 1
	    color {123 232 4}
	    zero_x 0
	    zero_y 1
	    zs_x 0.5
	    zs_y 0.5
	    img ""
	}
	hints {
	    color rgb
	    zero_x bool
	    zero_y bool
	    zs_x float 0.1 10
	    zs_y float 0.1 10
	}
	init {
	    $_ create rect $x $y $xx $yy -tags [concat $tags box]
	    if {$img ne ""} {
		package require Img
		image create photo photo$id
		$_ create image 0 0 -tags [concat $tags img]
	    }
	}
	redraw {
	    if {[expr $zero_x == 1]} {set zs_x [tr $_ x id $zs_x]; set xx [expr $x + $zs_x]; set x [expr $x - $zs_x]}
	    if {[expr $zero_y == 1]} {set zs_y [tr $_ y id $zs_y]; set yy [expr $y + $zs_y]; set y [expr $y - $zs_y]}
	    $_ coords $box $x $y $xx $yy
	    $_ coords $img $x $y
	    if {$img ne ""} {
		photo$id configure -file $img
		set photo photo$id
	    } {set photo ""}
	    $_ itemconfigure $img -image $photo
	    $_ itemconfigure $box -fill $color -outline [color [lighten $rgb 0.3]] -width [expr $selected + 1]
	}
    }

    colors {
	attributes {ins 1 out 1 x 1 y 3}
	tags {box subwin}
	init {
	    $_ create rect 0 0 0 0 -tags [concat $tags box]
	    set p [frame $_.$id]
	    $_ create window 0 0 -tags [concat $tags subwin] -window $_.$id -anchor nw
	    set picker {iVBORw0KGgoAAAANSUhEUgAAADIAAAEACAIAAAB+mLL0AAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH1QkNDBMQBB81AgAAELhJREFUeNqtnVty47wOhMGL/5nZ29n/ZkYiz0MsBQTQTciZFMulyE7SISEQpPAJ5X+//vwq5Xcpv0R+lfKrlF9S3mfWk+8z7w/I9VPfJ0VE/vyWP3+u9vz49/u4i5S1VXcm30TkJfIS6SJdHbTrtYnU67Vef666XyJdna0JifzMLesWZDR5ZV5TQbLIAX9XyzLKjLiKO+zdW0W9Hb5uP3C/ish/So1pXlMFNvPuLfPb/UHyPO8tPYJkEEWk9PW3k+PMW8a2Xq63qustYwN2EMNG3qpUVqcmH16PgcmT1nKy2g8GMXYQhepoT94Ke4ubfCEmX9xfMgfJ86KsKuMgYu9wm3w4Iua14bfCQbxNPuNOa3IQ2+4gPCngMkSTjxlK8X7LKPMiMmfCySe0emRbonurAF/QHrakrL2j7wkRPX0yM4hZ26q0n3p0jE7KqsmYfM9pKsjkvQ0ZQf5Vywp7y49gI+a17a0GBIXfEtvKmJdok/futOGu0jr8MZKVMfZqTB4NorGwUIpp4gzLdGfo5X80iHd75WT13Wwd9hk0+XAQSXs5WX1n8j4GrGiJUaPJ2BtWKOgVyQobCdfYILbdZWg0mVfBV8a2w77DB2TyFbuuUJmXhXxbo0HzxuRD22pg+JAsNBm0TDhv1ol5w3+5BbSXxceu5YNmHiCgK/Gr/Sci+JdUoK/wdaKPzb2aBgR9abp7KxNL7oPmgoNmYmGvSJyAYB8tCB4ENuYXdXAZGkFeVnLVVEjQXBIzD3GkxraeNhg0Z+bp5nzEKxr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Si4XWgkLVTaIOBn4raeBCmQdS3aTy9C0SQZA178RlRDgIVVWhmUqS3UKTLyuQgazbZ6oLrbhSHAw7IezTvy8Hg2JkKhp4WZkyIqFVDR9BmAInvrpJwfWE7l/ad7BfxZCIK47Sv7P4x8qrnLiTJCptMunYabqnrVMwHESfWHrm4CxfRiTUpKMjQ4hMZPIjwatwNaSMiIFEzohdiR3EjJCaJDM2qayKIZG2oci6xUOIE/eZ6qaSiFADPylZYIPmuSZbI0IkrHEyVs5LsCZCiESlHjosbeI9gsdDmgNXw13rAxMig0w+BdfrEIyHNFeWQvD+3bmK47WPvoNmZNeC8ZDhjmW3obi1LTuIxC9k7tecFFzpbgTPZGATdlXBpU0KKJOr99ANHnImAKTFnRo8RKIyRxVDIkjW37WYSE+CWuhKJIQIv6skUSGYYzeIk5i8L23imYyxu58kyiMcuAhForpJ5OU9ITLWYJfcRjL3ro6IEDn39dH6stw2f2ZGKDu/h6TvE/kaIueucs4SNIvdjNvfzhIqy/M0B74GB4+3kne0yAfE3YHJ1IKJXPf/ARXN6/ssS+vjAAAAAElFTkSuQmCC}
	    image create photo colorpicker$id
	    colorpicker$id configure -data $picker
	    label $p.colors -bd 0
	    entry $p.rgb -bd 0 -width 10 -font {{bitstream vera sans mono} 8}
	    bind $p.colors <B1-Motion> "::pd::colors:pick $_ $id %x %y"
	    bind $p.colors <1> "::pd::colors:pick $_ $id %x %y"
	    grid $p.colors -sticky nsew
	    grid $p.rgb -stick nsew
	    $p.colors configure -image colorpicker$id
	}
	
	redraw {
	    $_ coords $box [expr $x - 10] $y $xx $yy
	    $_ itemconfigure $box -fill $color
	    $_ coords $subwin $x $y
	    set w [expr $xx - $x]
	    set h [expr $yy - $y]
	    if {[expr abs($w - [$_.$id cget -width]) >= 1 || abs($h - [$_.$id cget -height]) >= 1]} {
		$_.$id configure -width $w -height $h
	    }
	}

	destroy {
	    destroy $_.$id
	}
	methods {
	    proc colors:pick {_ id x y} {
		if {![expr $y <= 255 && $y >= 0 && $x >= 0 && $x <= 50]} {return}
		set p $_.$id
		set rgb [hsvToRgb [expr (255 - $y) / 255.] [expr $x <= 25 ? 1. : ($x - 50) / -25.] [expr $x <= 25 ? $x / 25. : 1.]]
		$p.rgb delete 0 end
		$p.rgb insert 0 $rgb
		$p.rgb configure -bg [color $rgb]
	    }

	}
    }

    canvas {
	attributes {
	    color {222 222 222}
	    sc orange
	    mode edit
	    sel {}
	    xa 0 xb 100 ya 0 yb 100 xao 0 xbo 100 yao 0 ybo 100
	}
	init {
	    canvas $_
	    place $_ -relwidth 1 -relheight 1
	    #	    bind $_ <Configure> "::pd::redraw $_ all"
	    bind $_ <Enter> "focus $_"
	    bind $_ <Key> "::pd::key $_ %k 1"
	    bind $_ <KeyRelease> "::pd::key $_ %k 0"
	    bind $_ <Motion> "::pd::hover $_ %x %y"
	    bind $_ <4> "::pd::viewpoint $_ {action scroll units 1 axis x}"
	    bind $_ <5> "::pd::viewpoint $_ {action scroll units -1 axis x}"
	    bind $_ <Control-4> "::pd::viewpoint $_ {action zoom dir in axe x x %x y %y}"
	    bind $_ <Control-5> "::pd::viewpoint $_ {action zoom dir out axe x x %x y %y}"
	    bind $_ <Alt-4> "::pd::viewpoint $_ {action zoom dir in axe {x y} x %x y %y}"
	    bind $_ <Alt-5> "::pd::viewpoint $_ {action zoom dir out axe {x  y} x %x y %y}"
	    bind $_ <Control-Shift-4> "::pd::viewpoint $_ {action zoom dir in axe y x %x y %y}"
	    bind $_ <Control-Shift-5> "::pd::viewpoint $_ {action zoom dir out axe y x %x y %y}"
	    bind $_ <Shift-4> "::pd::viewpoint $_ {action scroll units -1 axis y}"
	    bind $_ <Shift-5> "::pd::viewpoint $_ {action scroll units 1 axis y}"
	    foreach m {"Control-" "" "Shift-" "Double-"} {
		foreach bn {1 2 3} {
		    set b [list [concat $bn first] [concat B${bn}-Motion motion] [concat ButtonRelease-$bn release]]
		    foreach ba {0 1 2} {
			bind $_ <$m[lindex [lindex $b $ba] 0]> "::pd::click [list [string tolower [string trimright $m -1]]] $bn [lindex [lindex $b $ba] 1] $_ %x %y %X %Y"}}}
	    if {$::has_tkdnd == 1} {dnd bindtarget $_ text/plain <Drop> "::pd::drop $_ %D %x %y"}
	}
	redraw {
	    set bd [expr {[$_ cget -bd] * 2}]
	    $_ configure -bg $color -width [expr {[winfo width $_] + $bd}] -height [expr {[winfo height $_] + $bd}]
	}
	methods {
	    proc item_pos {_ item r x y xx yy} {
		update $_ $item $r [dict create x $x y $y xx $xx yy $yy] 1}

	    proc pencil {_ a x y} {
		variable ""
		switch $a {
		    motion {
			item_pos $_ $($_:ci) abs [tr $_ x i $x] [tr $_ y i $y] [tr $_ x i $($_:fx)] [tr $_ y i $y]
		    }
		    first {set ($_:ci) [item_new $_ [dict create type rect id -]]}}}

	    proc resize_canvas {_ a x y} {viewpoint $_ [dict create action resize x $x y $y]}

	    proc item_v {_ a x y} {
		variable ""
		variable obj
		foreach id [getsel $_] {
		    set type [dict get $($_) $id type]
		    if {[dict exists $obj $type defaults]} {
			foreach local [dict keys [dict get $obj $type defaults]] {
			    set $local [dict get $obj $type defaults $local]}}
		    if {[dict exists $obj $type control Button-1]} {eval [dict get $obj $type control Button-1]}
		}
	    }

	    proc move_canvas {_ a x y} {viewpoint $_ [dict create action move x $x y $y]}

	    proc move_object {_ a x y} {
		variable ""
		set mx [tr $_ x d [expr {$x - $($_:cx)}]]
		set my [tr $_ y d [expr {$y - $($_:cy)}]]
		foreach item [getsel $_] {item_pos $_ $item rel $mx $my $mx $my}
	    }

	    proc resize_left {_ a x y} {item_resize $_ x $x $y}
	    proc resize_right {_ a x y} {item_resize $_ xx $x $y}    
	    proc resize_top {_ a x y} {item_resize $_ y $x $y}
	    proc resize_bottom {_ a x y} {item_resize $_ yy $x $y}  
	    proc resize_tl {_ a x y} {item_resize $_ x $x $y; item_resize $_ y $x $y}
	    proc resize_tr {_ a x y} {item_resize $_ x $x $y; item_resize $_ yy $x $y}
	    proc resize_bl {_ a x y} {item_resize $_ xx $x $y; item_resize $_ y $x $y}
	    proc resize_br {_ a x y} {item_resize $_ xx $x $y; item_resize $_ yy $x $y}

	    proc item_resize {_ e x y} {
		variable ""
		array set ax {x x xx x y y yy y}
		set m [tr $_ $ax($e) d [expr $$ax($e) - $($_:c$ax($e))]]
		item_mua $_ rel [dict create $e $m] 1
	    }


	    proc item_scale {_ a x y} {


	    }

	    proc click {m button action _ x y X Y} {
		variable ""
		set clicked [cleansel $_ [$_ find overlapping $x $y $x $y]]
		set a $($_:submode)
		switch $action {
		    first {
			foreach xy {x y}  {set ($_:f$xy) [set $xy]}
			switch $button {
			    1 {if {$m eq "control"} {
				mode $_ pencil; pencil $_ $action $x $y
			    } elseif {$m eq "double" || $m eq "shift"} {
				if {[llength $clicked] > 0} {
				    togglesel $_ $clicked 
				} else {
				    item_new $_ [dict create type sel id sel xa $x ya $y xb $x yb $y]
				}
			    } elseif {[$_ find withtag sel] ne ""} {
				item_delete $_ sel
			    } elseif {[llength [getsel $_]] > 1 && [llength $clicked] > 0 && [lsearch [getsel $_] $clicked] == -1} {
				updatesel $_ $clicked} elseif {$a eq "cabledraw"} {eval $a $_ $action $x $y}}
			    2 {if {$clicked ne ""} {mode $_ item_scale} {mode $_ resize_canvas}}
			    3 {if {$clicked ne ""} {mode $_ item_v} {rmenu $_ $x $y $X $Y}}}}
		    motion {
#			puts "$a $_ $action $x $y"
			eval $a $_ $action $x $y
#			catch {eval $a $_ $action $x $y}
		    }
		    release {
			switch $button {
			    1 {if {$a eq "draw" || $a eq "cabledraw"} {eval $a $_ $action $x $y}}
			    2 {mode $_ move_canvas}
			    3 {mode $_ move_canvas}
			}
		    }
		}
		if {$clicked ne ""} {inspect $_ [lindex $clicked end]}
		foreach xy {x y}  {set ($_:c$xy) [set $xy]}
	    }

	    proc hover {_ x y} {
		variable ""
		if {[$_ find withtag sel] ne ""} {
		    update $_ sel abs [dict create xa $x ya $y] 1
		} else {
		    set clicked [$_ find overlapping [expr $x - 2]  [expr $y - 2] [expr $x + 2] [expr $y + 2]]
		    set out 0
		    foreach id $clicked {
			if {[lindex [$_ itemcget $id -tags] 3] eq "out"} {
			    set out 1
			    set ($_:cable) [$_ itemcget $id -tags]
			    break
			}
		    }
		    if {$out == 1} {mode $_ cabledraw} else {
			set clicked [cleansel $_ $clicked]
			if {$clicked ne ""} {
			    set c [lindex $clicked end]
			    resize_modes $_ $x $y $c
			    if {[llength [getsel $_]] <= 1} {updatesel $_ $c}
			    inspect $_ $c
			} else {
			    mode $_ move_canvas
			}
		    }
		}
	    }
	    
	    proc resize_modes {_ x y id} {
		variable ""
		set d 3
		set bbox [$_ bbox [$_ find withtag "i$id && box"]]
		if {$bbox eq ""} {set bbox {0 0 0 0}}
		lassign $bbox bx by bxx byy
		set dx [expr {abs($bx - $x)}]
		set dxx [expr {abs($bxx - $x)}]
		set dy [expr {abs($by - $y)}]
		set dyy [expr {abs($byy - $y)}]
		if {$dx < $d && $dy < $d} {
		    mode $_ resize_tl
		} elseif {$dxx < $d && $dy < $d} {
		    mode $_ resize_tr
		} elseif {$dx < $d && $dyy < $d} {
		    mode $_ resize_bl
		} elseif {$dxx < $d && $dyy < $d} {
		    mode $_ resize_br
		} elseif {$dx < 5} {
		    mode $_ resize_left
		} elseif {$dxx < 5} {
		    mode $_ resize_right
		} elseif {$dy < 1} {
		    mode $_ resize_top
		} elseif {$dyy < 1} {
		    mode $_ resize_bottom
		} else {
		    mode $_ move_object
		}
	    }

	    proc mode {_ m} {
		variable ""
		array set cursor {pencil pencil move_canvas fleur move_object dotbox item_v box_spiral resize_canvas bogosity sel cross_reverse item_scale sizing resize_left left_side cabledraw circle resize_right right_side resize_top top_side resize_bottom bottom_side resize_tl top_left_corner resize_tr top_right_corner resize_bl bottom_left_corner resize_br bottom_right_corner}
		set ($_:submode) $m
		$_ configure -cursor $cursor($m)
	    }
	    proc mode_flip {_} {
		variable ""
		l $_ canvas mode
		if {$mode eq "edit"} {set mode forward} {set mode edit}
		up $_ canvas mode
		redraw $_ {canvas grid}
	    }

	    proc drop {_ dropped x y} {
		foreach d [split $dropped "\n"] {
		    set x [tr $_ x i $x] 
		    set y [tr $_ y i $y] 
		    item_new $_ [dict create type sound id - g 1 v 1 x $x y $y xx $x yy $y filename [regsub -- {^file:[/]+} $d "/"]]
		}
	    }


	    proc key {_ k b} {
		#	puts $k
		switch $b {
		    1 {
			switch $k {
			    9 {mode_flip $_}
			    22 {item_delete $_}
			    38 {clip $_ selecta}
			    53 {clip $_ cut}
			    54 {clip $_ copy}
			    55 {clip $_ paste}
			    97 {viewpoint $_ {action reset}}
			    98 {viewpoint $_ {action scroll units -1 axis y}}
			    100 {viewpoint $_ {action scroll units -1 axis x}}
			    102 {viewpoint $_ {action scroll units 1 axis x}}
			    104 {viewpoint $_ {action scroll units 1 axis y}}
			    107 {item_delete $_}
			}
		    }
		}
	    }
	}
    }

    sel {
	attributes {
	    class selrect
	    outline {255 255 0}
	    fill {255 128 0}
	    width 0
	    stipple gray12
	}
	init {
	    $_ create rect $xa $xb $ya $yb -tags $tags
	    mode $_ sel
	}
	redraw {
	    updatesel $_ [cleansel $_ [$_ find overlapping $xa $ya $xb $yb]]
	    $_ coords $item $xa $ya $xb $yb
	    $_ itemconfigure $item -fill [color $fill] -stipple $stipple -outline [color $outline] -width $width
	}
	methods {
	    proc sel {_ a x y} {
		update $_ sel abs [dict create xb $x yb $y] 1
	    }

	    proc getsel {_} {
		return [set [l $_ canvas sel]]
	    }

	    proc cleansel {_ sel} {
		set clean {}
		foreach item $sel {
		    set class [lindex [$_ itemcget $item -tags] 0]
		    if {$class eq "item" || $class eq "cable"} {lappend clean [lindex [$_ itemcget $item -tags] 2]}}
		set clean [lsort -unique $clean]
		return $clean
	    }

	    proc togglesel {_ d} {
		variable ""
		if {[lsearch [getsel $_] $d] < 0} {
		    set sel [concat [getsel $_] $d]
		} else {
		    set sel [lsearch -inline -not -all [getsel $_] $d]
		}
		up $_ canvas sel
		redraw $_ $d
	    }

	    proc updatesel {_ sel} {
		variable ""
		set os [getsel $_]
		up $_ canvas sel
		redraw $_  [lsort -unique [concat $os $sel]]
	    }

	}
    }

    gridlines {
	attributes {
	    class gridlines
	    stipple gray50
	    xq 10 yq 10 xm 15 ym 15
	}
	redraw {
	    foreach items $item {$_ delete $items}
	    array set ta {x n y w}
	    array set tj {x center y left}
	    array set igx {y width x height}
	    l $_ canvas xa xb ya yb
	    foreach xy {x y} {
		set range [expr abs($${xy}b - $${xy}a)]
		set nSlices [expr $range / $${xy}q]
		if {$nSlices > [set ${xy}m]} {set factor [expr int($nSlices / ($${xy}m + 0.0) + 1)]} else {
		    set factor [expr 1. / (int(1./($nSlices / ($${xy}m + 0.0) + 0.0)) + 0.0)]
		}
		set increment [expr $${xy}q * $factor]
		for {set x [expr int($${xy}a / ($increment + 0.0) + 1)*($increment + 0.0)]} {[expr $${xy}a > $${xy}b ? $x >=  $${xy}b : $x <=  $${xy}b]} {set x [expr $${xy}a > $${xy}b ? $x - $increment : $x + $increment]} {
		    set og [tr $_ $xy t $x]
		    set invgeo [winfo $igx($xy) $_]
		    switch $xy {
			y {set coords [concat 0 $og $invgeo $og]}
			x {set coords [concat $og 0 $og $invgeo]}}
		    $_ lower [$_ create text [lrange $coords 0 1] -font {{Bitstream Vera Sans} 8} -fill [rc] -anchor $ta($xy) -text [string range $x 0 7] -justify $tj($xy) -tags $atags]
		    $_ lower [$_ create line $coords -fill $color -stipple $stipple -tags $atags]
		}
	    }
	}
    }
    subwin {
	tags {
	    box subwin
	}
	attributes {
	    x 32 y 24
	    ins 1
	    outs 1
	}
	init {
	    $_ create rect 0 0 0 0 -tags [concat $tags box]
	    new $_.$id
	    $_ create window 0 0 -tags [concat $tags subwin] -window $_.$id -anchor nw
	}
	redraw {
	    $_ coords $box [expr $x - 10] $y $xx $yy
	    $_ coords $subwin $x $y
	    set w [expr $xx - $x]
	    set h [expr $yy - $y]
	    if {[expr abs($w - [$_.$id cget -width]) >= 1 || abs($h - [$_.$id cget -height]) >= 1]} {
		$_.$id configure -width $w -height $h
	    }
	    $_ itemconfigure $box -fill $color
	}
    }
    kbd {
	attributes {
	    ins 1 outs 1
	    x 60
	    y 12
	    octaves 5
	    color {0 0 0}
	    color_bg {255 255 255}
	    lp -1
	}
	tags {box subwin}
	init {
	    $_ create rect 0 0 0 0 -tags [concat $tags box]
	    set path [frame $_.$id]
	    $_ create window 0 0 -tags [concat $tags subwin] -window $_.$id -anchor nw
	    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}
	    set keys [dict create 1 [dict create fg $color_bg bg $rgb rw 0.1 rh 0.6 an "-anchor n"] 0 [dict create fg $rgb bg $color_bg rw [expr 1 / 7.] rh 1. an "; lower \$wk"]]
	    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 [color [dict get $keys $n bg]] -fg [color [dict get $keys $n bg]] -bd 1 -relief raised;place $wk -relx [expr [lindex $npl $on] / 7.] -y 0 -relwidth [dict get $keys $n rw] -relheight [dict get $keys $n rh] [dict get $keys $n an]"
		    bind $wk <1> "::pd::kbd_play $_ $id 0 1 %X %Y"; bind $wk <B1-Motion> [bind $wk <1>];bind $wk <ButtonRelease-1> "::pd::kbd_play $_ $id 0 0 %X %Y"; bind $wk <Enter> "::pd::kbd_play $_ $id 1 0 %X %Y"; bind $wk <3> "::pd::kbd_play $_ $id 1 1 %X %Y"; bind $wk <B3-Motion> [bind $wk <3>]; bind $wk <2> "::pd::kbd_off $_ $id $octaves"
		}
		place $w -y 0 -relheight 1.0 -relx [expr $o / $octaves.0] -relwidth [expr 1 / $octaves.0]	  
	    }
	}
	redraw {
	    $_ coords $box [expr $x - 10] $y $xx $yy
	    $_ itemconfigure $box -fill $color
	    $_ coords $subwin $x $y
	    set w [expr $xx - $x]
	    set h [expr $yy - $y]
	    if {[expr abs($w - [$_.$id cget -width]) >= 1 || abs($h - [$_.$id cget -height]) >= 1]} {
		$_.$id configure -width $w -height $h
	    }
	}
	methods {
	    proc kbd_play {_ id m b x y} {
		l $_ $id lp
		foreach a {{rs {0 sunken 1 raised}} {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]
		if {$m != 1 && $lp != -1 && $lp ne $w} {kbd_action $_ $id $lp 0}
		if {[$w cget -relief] eq $rs($b)} {kbd_action $_ $id $w [eval expr $v($b)]}
		set lp $w
		up $_ $id lp
	    }

	    proc kbd_action {_ id w v} {
		if {$v == 0} {set relief raised} {set relief sunken}
		$w config -relief $relief
#		pd [concat $t.rp _cb [winfo name $w] $v \;]
	    }

	    proc kbd_off {_ id octaves} {
		for {set o 0} {$o < $octaves} {incr o} {	
		    for {set on 0} {$on < 12} {incr on} {
			set w $_.$id.f$o.[expr $o * 12 + $on]
			if {[$w cget -relief] eq "sunken"} {kbd_action $_ $id $w 0}}}}
	}
    }
    
    xy {
	tags {
	    box loc
	}
	attributes {
	    x 10 y 10
	    ins 1
	    outs 1
	}
	init {
	    $_ create rect 0 0 0 0 -tags [concat $tags box] -fill gray90
	    $_ create rect 0 0 0 0 -tags [concat $tags loc] -fill red
	}
	redraw {
	    $_ coords $box $x $y $xx $yy
	    $_ coords $loc $x $y [expr $x + ($xx - $x) / 12.] [expr $y + ($yy - $y)/12.]
	    $_ itemconfigure $box -outline $color
	}
    }

    cable {
	attributes {
	    class cable
	    x 0 y 0
	    from ""
	    to ""
	    arrow last
	    width 1
	}
	init {
	    $_ create line 0 0 0 0 -tags $tags -fill green -arrow $arrow -width $width
	}
	redraw {
	    if {[llength $from] == 2 } {
		lassign [ioloc $_ [lindex $from 0] [lindex $from 1] outs] x y
	    }
	    if {[llength $to] == 2 } {
		lassign [ioloc $_ [lindex $to 0] [lindex $to 1] ins] xx yy
	    }
	    $_ coords $item $x $y $xx $yy
	}
	control {
	    
	}
	methods {
	    proc cabledraw {_ a x y} {
		variable ""
		lassign $($_:cable) class iid id io ioid ionum cur
		switch $a {
		    first {
			set ($_:cable:id) [item_new $_ [dict create type cable id - from [list $id $ionum]]]
		    }
		    motion {
			update $_ $($_:cable:id) abs [dict create xx [tr $_ x i $x] yy [tr $_ y i $y]] 1
		    }
		    release {}
		}
	    }
	}
    }

    io {
	attributes {
	    class io
	    width 1
	    height 0.3
	    incolor {255 0 0}
	    outcolor {0 0 255}
	}
	init {
	    l $_ $id ins outs
	    foreach io {in out} {
		for {set i 0} {$i < [set ${io}s]} {incr i} {
		    set port [$_ create rect $x $y $x $y -tags [concat $tags $io $io$i $i] -width 0 -fill [color [dict get $obj io attributes ${io}color]]]
#		    $_ bind $port <1> "puts lolport"
		}
	    }
	}
	redraw {
	    set w [tr $_ x id [dict get $obj io attributes width]]
	    set h [tr $_ x id [dict get $obj io attributes height]]
	    foreach io {in out} {
		for {set i 0} {$i < [set ${io}s]} {incr i} {
		    lassign [ioloc $_ $id $i ${io}s] x y
		    $_ coords [$_ find withtag "$tags && $io$i"]  [expr $x - $w] [expr $y - $h] [expr $x + $w] [expr $y + $h]
		}
	    }
	}
	methods {
	    proc ioloc {_ id n io} {
		variable ""
		foreach c {x xx y yy} {
		    set $c [tr $_ [string range $c 0 0] t [dict get $($_) $id $c]]}
		if {$io eq "ins"} {set py $y} {set py $yy}
		set px [expr ($n / ([dict get $($_) $id $io] + 0.0)) * ($xx - $x + 0.0) + $x]
		return [list $px $py]
	    }
	}
    }

    button {
	tags {box button}
	attributes {x 3 y 3 ins 1 outs 1}
	init {
	    $_ create rect 0 0 0 0 -tags [concat $tags box] -width 0
	    $_ create oval 0 0 0 0 -tags [concat $tags button]
	}
	redraw {
	    $_ itemconfigure $box -fill $color -width $selected -outline [color $rgb]
	    $_ coords $box $x $y $xx $yy
	    $_ coords $button $x $y $xx $yy
	}
	methods {
	    proc button:flash {} {
		
	    }
	}
    }

     msg {
	tags {
	    box txt
	}
	attributes {
	    x 2 y 2
	    ins 1
	    outs 1
	    msg msg
	}
	init {
	    $_ create polygon 0 0 0 0 -tags [concat $tags box]
	    $_ create text 0 0 -tags [concat $tags txt] -fill white -anchor nw -justify left
	}
	redraw {
	    $_ coords $txt $x $y
	    $_ itemconfigure $box -fill $color	  
	    $_ itemconfigure $txt -font [list {bitstream vera sans} [expr int($sy)]] -text $msg
	    lassign [$_ bbox $txt] x y xx yy
	    set flare [expr $xx + ($xx - $x) / 12.]
	    $_ coords $box $x $y $flare $y $xx [expr $y + ($yy - $y)/2.] $flare $yy $x $yy 
#	    foreach z {x xx y yy} {set $z [tr $_ [string range $z 0 0] i [set $z]]}
#	    up $_ $id x y xx yy
	}
	 methods {
	     proc msg_updatetext {_} {
		 
	     }
	 }
    }

    slider {
	tags {
	    pos box
	}
	attributes {
	    x 2 y 10
	    min 1
	    v 48
	    max 69
	    ins 1
	    outs 1
	}
	init {
	    $_ create rect 0 0 0 0 -tags [concat $tags box] -fill $color
	    $_ create rect 0 0 0 0 -tags [concat $tags pos] -fill green -width 0
	}
	redraw {
	    $_ coords $box $x $y $xx $yy
	    if {[expr ($xx - $x) > ($yy - $y)]} {set orient h} {set orient v}
	    set v [expr ($v - $min + 0.) / ($max - $min + 0.)]
	    switch $orient {
		v {
		    set loc [expr $y + ($yy - $y) * $v]
		    set coords [concat $x $loc $xx $loc]
		}
		h {
		    set loc [expr $x + ($xx - $x) * $v]
		    set coords [concat $loc $y $loc $yy]
		}
	    }
	    $_ coords $pos $coords
	}
	control {
	    Button-1 {
	        update $_ $id rel [dict create v [expr {($x - $($_:cx)) / 100.0 * ($max - $min + 0.)}]] 1
	    }
	}
    }

    sound {
	tags {
	    tl tlr tf tfr r w
	}
	attributes {
	    ins 0
	    outs 0
	}
	init {
	    snack::sound s$id
	    set filename [dict get $($_) $id filename]
	    s$id read $filename
	    $_ create path [::tkpath::coords rect 0 0 0 0 -rx 3 -ry 3] -tags [concat $tags r] -strokewidth 0 -fill $color -fillopacity 0.08
	    $_ create waveform 0 0 -tags [concat $tags w] -sound s$id -fill white
	    set length [expr [s$id length] / ($($_:samplerate) + 0.0) * 1000]
	    update $_ $id abs [dict create xx [expr [dict get $($_) $id x] + $length]] 0
	    $_ create text 0 0 -tags [concat $tags tl] -font {{bitstream vera sans} 8} -fill HotPink -anchor nw -justify left -text "[string range $length 0 10] s"
	    $_ create rectangle 0 0 0 0 -tags [concat $tags tlr] -fill LightGreen -width 0
	    $_ raise $tl
	    $_ create text 0 0 -tags [concat $tags tf] -font {{bitstream vera sans} 8} -fill NavyBlue -anchor ne -justify right -text $filename
	    $_ create rectangle 0 0 0 0 -tags [concat $tags tfr] -fill gray90 -width 0
	    $_ raise $tf
	}
	redraw {
	    set ro 6
	    $_ coords $r [::tkpath::coords rect $x $y $sx $sy -rx $ro -ry $ro]
	    $_ itemconfigure $r -fillopacity [dict get $($_) $id v] -fill $color
	    $_ coords $w $x $y
	    $_ itemconfigure $w -width $sx -height [expr int($sy)]
	    $_ coords $tl [expr $x + 4] [expr $y + 4]
	    $_ coords $tlr [$_ bbox $tl]
	    $_ coords $tf [expr $x + $sx] [expr $y + 4]
	    $_ coords $tfr [$_ bbox $tf]
	}
    }

}