aboutsummaryrefslogtreecommitdiff
path: root/externals/grill/flext/source/flattr_ed.cpp
blob: 6e13e71ccccae4e13bae36ce976ac9ec3496e4cb (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
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
/* 

flext - C++ layer for Max/MSP and pd (pure data) externals

Copyright (c) 2001-2005 Thomas Grill (gr@grrrr.org)
For information on usage and redistribution, and for a DISCLAIMER OF ALL
WARRANTIES, see the file, "license.txt," in this distribution.  

*/

/*! \file flattr_ed.cpp
    \brief Attribute editor (property dialog) for PD
*/

#include "flext.h"

#if FLEXT_SYS == FLEXT_SYS_PD 

#ifdef _MSC_VER
#pragma warning( disable : 4091 ) 
#endif

/*
#ifdef PD_DEVEL_VERSION
#define FLEXT_CLONEWIDGET
#endif
*/

#ifndef FLEXT_CLONEWIDGET
// This is problematic... non-public headers!
// compilation is specific for a compiled version!!
#include <m_imp.h>
#endif

#include <g_canvas.h>

#include <string.h>
#include <stdio.h>

static t_widgetbehavior widgetbehavior; 

void flext_base::SetGfx(t_classid c)
{
    // widgetbehavior struct MUST be resident... (static is just ok here)

#ifndef FLEXT_CLONEWIDGET
    widgetbehavior.w_visfn =        c->c_wb->w_visfn; 
    widgetbehavior.w_selectfn =     c->c_wb->w_selectfn; 
    widgetbehavior.w_getrectfn =    c->c_wb->w_getrectfn; 
    widgetbehavior.w_displacefn =   c->c_wb->w_displacefn; 
    widgetbehavior.w_activatefn =   c->c_wb->w_activatefn; 
    widgetbehavior.w_deletefn =     c->c_wb->w_deletefn; 
    widgetbehavior.w_selectfn =     c->c_wb->w_selectfn;
#else
    widgetbehavior.w_visfn =        text_widgetbehavior.w_visfn; 
    widgetbehavior.w_selectfn =     text_widgetbehavior.w_selectfn; 
    widgetbehavior.w_getrectfn =    text_widgetbehavior.w_getrectfn; 
    widgetbehavior.w_displacefn =   text_widgetbehavior.w_displacefn; 
    widgetbehavior.w_activatefn =   text_widgetbehavior.w_activatefn; 
    widgetbehavior.w_deletefn =     text_widgetbehavior.w_deletefn; 
    widgetbehavior.w_selectfn =     text_widgetbehavior.w_selectfn;
#endif

#ifndef FLEXT_NOATTREDIT
    attrsetup(c);
#endif // FLEXT_NOATTREDIT

    widgetbehavior.w_clickfn =      cb_click;
    class_setwidget(c, &widgetbehavior);
}


#ifndef FLEXT_NOATTREDIT

#ifndef FLEXT_CLONEWIDGET
static void (*ori_vis)(t_gobj *c, t_glist *, int vis) = NULL;
static void (*ori_select)(t_gobj *c, t_glist *, int state) = NULL;
#endif

//! generate the script for the property dialog
static void tclscript()
{
    static bool havecode = false;
    if(havecode) return;
    else havecode = true;

    sys_vgui(
        "proc flext_escatoms {lst} {\n"
            "set tmp {}\n"
            "foreach a $lst {\n"
                "set a [regsub {\\$} $a \\\\$]\n"  // replace $ with \$
                "set a [regsub {,} $a \\\\,]\n"  // replace , with \,
                "set a [regsub {;} $a \\\\\\;]\n"  // replace ; with \;
                "lappend tmp $a\n"
            "}\n"
            "return $tmp\n"
        "}\n"
    );
    sys_vgui(
        "proc flext_makevalue {id ix} {\n"
            // strip "." from the TK id to make a variable name suffix
            "set vid [string trimleft $id .]\n"

            "set var_attr_name [concat [concat var_name_$ix]_$vid ]\n"
            "set var_attr_init [concat [concat var_init_$ix]_$vid ]\n"
            "set var_attr_val [concat [concat var_val_$ix]_$vid ]\n"
            "set var_attr_save [concat [concat var_save_$ix]_$vid ]\n"
            "set var_attr_type [concat [concat var_type_$ix]_$vid ]\n"

            "global $var_attr_name $var_attr_init $var_attr_val $var_attr_save $var_attr_type\n"

            "set lst {}\n"

            "if { [expr $$var_attr_type] != 0 } {\n"
                // attribute is puttable

                "lappend lst [eval concat $$var_attr_name]\n" 

                // process current value
                "set tmp [flext_escatoms [eval concat $$var_attr_val]]\n"
                "set lst [concat $lst [llength $tmp] $tmp]\n" 

                // process init value
                "set tmp [flext_escatoms [eval concat $$var_attr_init]]\n"
                "set lst [concat $lst [llength $tmp] $tmp]\n" 

                "lappend lst [eval concat $$var_attr_save]\n" 
            "}\n"

            // return list
            "return $lst\n" 
        "}\n"
    );
    sys_vgui(
        "proc flext_apply {id ix} {\n"
            "set lst [flext_makevalue $id $ix]\n"
            "set lst [eval concat $lst]\n" // remove curly braces from character escaping
            "pd [concat $id attributedialog $lst \\;]\n"
        "}\n"

        "proc flext_applyall {id alen} {\n"
            // make a list of the attribute values (including save flags)

            "set lst {}\n"
            "for {set ix 1} {$ix <= $alen} {incr ix} {\n"
                "set lst [concat $lst [flext_makevalue $id $ix]]\n" 
            "}\n"
            "set lst [eval concat $lst]\n" // remove curly braces from character escaping

            "pd [concat $id attributedialog $lst \\;]\n"
        "}\n"

        "proc flext_cancel {id} {\n"
            "pd [concat $id cancel \\;]\n"
        "}\n"

        "proc flext_ok {id alen} {\n"
            "flext_applyall $id $alen\n"
            "flext_cancel $id\n"
        "}\n"
    );
    sys_vgui(
        "proc flext_help {id} {\n"
            "toplevel $id.hw\n"
            "wm title $id.hw \"Flext attribute editor help\"\n"

            "frame $id.hw.buttons\n"
            "pack $id.hw.buttons -side bottom -fill x -pady 2m\n"

            "text $id.hw.text -relief sunken -bd 2 -yscrollcommand \"$id.hw.scroll set\" -setgrid 1 -width 80 -height 10 -wrap word\n"
            "scrollbar $id.hw.scroll -command \"$id.hw.text yview\"\n"
            "pack $id.hw.scroll -side right -fill y\n"
            "pack $id.hw.text -expand yes -fill both\n"

            "button $id.hw.buttons.ok -text OK -command \"destroy $id.hw\"\n"
            "pack $id.hw.buttons.ok -side left -expand 1\n"
            "bind $id.hw {<KeyPress-Escape>} \"destroy $id.hw\"\n"

            "$id.hw.text tag configure big -font {Arial 10 bold}\n"
            "$id.hw.text configure -font {Arial 8 bold}\n"
            "$id.hw.text insert end \""
                "The flext attribute editor lets you query or change attribute values exposed by an external object. \" big \"\n\n"
                "Local variable names ($-values) will only be saved as such for init values. "
                "Alternatively, # can be used instead of $.\n"
                "Ctrl-Button on a text field will open an editor window where text can be entered more comfortably.\n"
            "\"\n"
            "$id.hw.text configure -state disabled\n"
        "}\n"
    );
    sys_vgui(
        "proc flext_copyval {dst src} {\n"
            "global $src $dst\n"
            "set $dst [expr $$src]\n"
        "}\n"

        "proc flext_textcopy {id idtxt var} {\n"
            "global $var\n"
            "set txt [eval $idtxt get 0.0 end]\n"
            // strip newline characters
            "set tmp {}\n"
            "foreach t $txt { lappend tmp [string trim $t] }\n"
            "set $var $tmp\n"
            "destroy $id\n"
        "}\n"
    );
    sys_vgui(
        "proc flext_textzoom {id var title attr edit} {\n"
            "global $var\n"
            "toplevel $id.w\n"
            "wm title $id.w [concat $title \" @\" $attr]\n"
//            "wm iconname $w \"text\"\n"
//            "positionWindow $id.w\n"

            "frame $id.w.buttons\n"
            "pack $id.w.buttons -side bottom -fill x -pady 2m\n"

            "text $id.w.text -relief sunken -bd 2 -yscrollcommand \"$id.w.scroll set\" -setgrid 1 -width 80 -height 20\n"
            "scrollbar $id.w.scroll -command \"$id.w.text yview\"\n"
            "pack $id.w.scroll -side right -fill y\n"
            "pack $id.w.text -expand yes -fill both\n"

            // insert text with newlines
            "set txt [split [expr $$var] ,]\n"
            "set lines [llength $txt]\n"
            "for {set ix 0} {$ix < ($lines-1)} {incr ix} {\n"
                "$id.w.text insert end [string trim [lindex $txt $ix] ]\n"
                "$id.w.text insert end \" ,\\n\"\n"
            "}\n"
            "$id.w.text insert end [string trim [lindex $txt end] ]\n"

            "$id.w.text mark set insert 0.0\n"

            "if { $edit != 0 } then {\n"
                "button $id.w.buttons.ok -text OK -command \"flext_textcopy $id.w $id.w.text $var\"\n"
                "pack $id.w.buttons.ok -side left -expand 1\n"
//              "bind $id.w {<Shift-KeyPress-Return>} \"flext_textcopy $id.w $id.w.text $var\"\n"
            "} "
            "else { $id.w.text configure -state disabled }\n"

            "button $id.w.buttons.cancel -text Cancel -command \"destroy $id.w\"\n"
            "pack $id.w.buttons.cancel -side left -expand 1\n"
            "bind $id.w {<KeyPress-Escape>} \"destroy $id.w\"\n"
        "}\n"
    );
    sys_vgui(
        "proc pdtk_flext_dialog {id title attrlist} {\n"
                "set vid [string trimleft $id .]\n"
                "set alen [expr [llength $attrlist] / 6 ]\n"

                "toplevel $id\n"
                "wm title $id $title\n" 
                "wm protocol $id WM_DELETE_WINDOW [concat flext_cancel $id]\n"

                "set row 0\n"

                // set grow parameters
                "grid columnconfigure $id 0 -weight 1\n"  // label
                "grid columnconfigure $id {1 4} -weight 3\n" // value entry
                "grid columnconfigure $id {2 3} -weight 0\n"  // copy buttons
                "grid columnconfigure $id 5 -weight 1\n"  // apply button
                "grid columnconfigure $id {6 7 8} -weight 0\n" // radio buttons

//                "grid rowconfigure $id {0 1 2} -weight 0\n"

                // set column labels
                "label $id.label -text {attribute} -height 2 -font {Helvetica 9 bold}\n"
                "label $id.init  -text {initial value} -height 2 -font {Helvetica 9 bold}\n"
                "label $id.copy  -text {copy} -height 2 -font {Helvetica 9 bold}\n"
                "label $id.val   -text {current value} -height 2 -font {Helvetica 9 bold}\n"
                "label $id.apply -text {} -height 2 -font {Helvetica 9 bold}\n" // why must this be empty?
                "foreach {i txt} {0 {don't\rsave} 1 {do\rinit} 2 {always\rsave} } {\n"
                    "label $id.b$i -text $txt -height 2 -font {Helvetica 9 bold}\n"
                "}\n"
//              "label $id.options -text {options} -height 2\n"

                "grid config $id.label -column 0 -row $row \n"
                "grid config $id.init  -column 1 -row $row \n"
                "grid config $id.copy  -column 2 -columnspan 2 -row $row \n"
                "grid config $id.val   -column 4 -row $row \n"
                "grid config $id.apply  -column 5 -row $row \n"
                "foreach i {0 1 2} { grid config $id.b$i -column [expr $i + 6] -row $row }\n"
//              "grid config $id.options -column 3 -row 0 \n"
                "incr row\n"

                // Separator
                "frame $id.sep -relief ridge -bd 1 -height 2\n"
                "grid config $id.sep -column 0 -columnspan 9 -row $row -pady 2 -sticky {snew}\n"
                "incr row\n"
    );
    sys_vgui(
                "set ix 1\n"
                "foreach {an av ai atp asv afl} $attrlist {\n"
                    "grid rowconfigure $id $row -weight 0\n"

                    // get attribute name
                    "set var_attr_name [concat [concat var_name_$ix]_$vid ]\n"
                    "global $var_attr_name\n"
                    "set $var_attr_name $an\n"

                    // get attribute init value (list)
                    "set var_attr_init [concat [concat var_init_$ix]_$vid ]\n"
                    "global $var_attr_init\n"
                    "set $var_attr_init $ai\n"

                    // get attribute value (list)
                    "set var_attr_val [concat [concat var_val_$ix]_$vid ]\n"
                    "global $var_attr_val\n"
                    "set $var_attr_val $av\n"

                    // get save flag
                    "set var_attr_save [concat [concat var_save_$ix]_$vid ]\n"
                    "global $var_attr_save\n"
                    "set $var_attr_save $asv\n"

                    // get type flag
                    "set var_attr_type [concat [concat var_type_$ix]_$vid ]\n"
                    "global $var_attr_type\n"
                    "set $var_attr_type $afl\n"

                    // add dialog elements to window

                    // attribute label
                    "label $id.label-$ix -text \"$an :\" -font {Helvetica 8 bold}\n"
                    "grid config $id.label-$ix -column 0 -row $row -padx 5 -sticky {e}\n"
    );
    sys_vgui(
                    "if { $afl != 0 } {\n"
                        // attribute is puttable

                        // entry field for initial value
                        // entry field for current value

                        // choose entry field type
                        "switch $atp {\n"
                            "0 - 1 {\n"  // int or float
                                "entry $id.init-$ix -textvariable $var_attr_init\n"
                                "entry $id.val-$ix -textvariable $var_attr_val\n"
                            "}\n"
                            "2 {\n"  // boolean
                                "checkbutton $id.init-$ix -variable $var_attr_init\n"
                                "checkbutton $id.val-$ix -variable $var_attr_val\n"
                            "}\n"
                            "3 {\n"  // symbol
                                "entry $id.init-$ix -textvariable $var_attr_init\n"
                                "entry $id.val-$ix -textvariable $var_attr_val\n"
                            "}\n"
                            "4 - 5 {\n"  // list or unknown
                                "entry $id.init-$ix -textvariable $var_attr_init\n"
                                "bind $id.init-$ix {<Control-Button-1>} \" flext_textzoom $id.init-$ix $var_attr_init { $title } $an 1\"\n"
                                "entry $id.val-$ix -textvariable $var_attr_val\n"
                                "bind $id.val-$ix {<Control-Button-1>} \" flext_textzoom $id.val-$ix $var_attr_val { $title } $an 1\"\n"
                            "}\n"
                        "}\n"

                        "grid config $id.init-$ix  -column 1 -row $row -padx 5 -sticky {ew}\n"
                        "grid config $id.val-$ix   -column 4 -row $row -padx 5 -sticky {ew}\n"

                        // copy buttons
                        "button $id.b2i-$ix -text {<-} -height 1 -command \" flext_copyval $var_attr_init $var_attr_val \"\n"
                        "grid config $id.b2i-$ix  -column 2 -row $row  -sticky {ew}\n"
                        "button $id.b2c-$ix -text {->} -height 1 -command \" flext_copyval $var_attr_val $var_attr_init \"\n"
                        "grid config $id.b2c-$ix  -column 3 -row $row  -sticky {ew}\n"

                        // apply button
                        "button $id.apply-$ix -text {Apply} -height 1 -command \" flext_apply $id $ix \"\n"
                        "grid config $id.apply-$ix  -column 5 -row $row  -sticky {ew}\n"

    //                  "tk_optionMenu $id.opt-$ix $var_attr_save {don't save} {initialize} {always save}\n"
    //                  "grid config $id.opt-$ix -column 5 -row $ix \n"

                        // radiobuttons
                        "foreach {i c} {0 black 1 blue 2 red} {\n"
                            "radiobutton $id.b$i-$ix -value $i -foreground $c -variable $var_attr_save \n"
                            "grid config $id.b$i-$ix -column [expr $i + 6] -row $row  \n"
                        "}\n"
    );
    sys_vgui(
                    "} else {\n"
                        // attribute is gettable only

                        // entry field for current value (read-only)

                        // choose display field type
                        "switch $atp {\n"
                            "0 - 1 {\n"  // int or float
                                "entry $id.val-$ix -textvariable $var_attr_val -state disabled\n"
                            "}\n"
                            "2 {\n"  // boolean
                                "checkbutton $id.val-$ix -variable $var_attr_val -state disabled\n"
                            "}\n"
                            "3 {\n"  // symbol
                                "entry $id.val-$ix -textvariable $var_attr_val -state disabled\n"
                            "}\n"
                            "4 - 5 {\n"  // list or unknown
                                "entry $id.val-$ix -textvariable $var_attr_val -state disabled\n"
                                "bind $id.val-$ix {<Control-Button-1>} \" flext_textzoom $id.val-$ix $var_attr_val { $title } $an 0\"\n"
                            "}\n"
                        "}\n"

//                      "entry $id.val-$ix -textvariable $var_attr_val -state disabled\n"
                        "grid config $id.val-$ix -column 4 -row $row -padx 5 -sticky {ew}\n"

                        "label $id.readonly-$ix -text \"read-only\"\n"
                        "grid config $id.readonly-$ix -column 6 -columnspan 3 -row $row -padx 5 -sticky {ew}\n"
                    "}\n"

                    // increase counter
                    "incr ix\n"
                    "incr row\n"
                "}\n"
    );
    sys_vgui(
                // Separator
                "frame $id.sep2 -relief ridge -bd 1 -height 2\n"
//                "grid rowconfigure $id $row -weight 0\n"
                "grid config $id.sep2 -column 0 -columnspan 9 -row $row -pady 5 -sticky {snew}\n"
                "incr row\n"

                // Buttons
                "frame $id.buttonframe\n"
                "pack $id.buttonframe -side bottom -fill x\n"

                "button $id.buttonframe.cancel -text {Leave} -width 20 -command \" flext_cancel $id \"\n"
                "button $id.buttonframe.apply -text {Apply all} -width 20 -command \" flext_applyall $id $alen \"\n"
                "button $id.buttonframe.ok -text {Apply & Leave} -width 20 -command \" flext_ok $id $alen \"\n"
                "button $id.buttonframe.help -text {Help} -width 10 -command \" flext_help $id \"\n"

                "pack $id.buttonframe.cancel -side left -expand 1\n"
                "pack $id.buttonframe.apply -side left -expand 1\n"
                "pack $id.buttonframe.ok -side left -expand 1\n"
                "pack $id.buttonframe.help -side left -expand 1\n"

//                "grid rowconfigure $id $row -weight 0\n"
                "grid config $id.buttonframe -column 0 -columnspan 9 -row $row -pady 5 -sticky {ew}\n"

                // Key bindings
                "bind $id {<KeyPress-Escape>} \" flext_cancel $id \"\n"
                "bind $id {<KeyPress-Return>} \" flext_ok $id $alen \"\n"
                "bind $id {<Shift-KeyPress-Return>} \" flext_applyall $id $alen \"\n"
        "}\n"
    );
}

void flext_base::attrsetup(t_classid c)
{
#ifndef FLEXT_CLONEWIDGET
    ori_vis = c->c_wb->w_visfn; 
    ori_select = c->c_wb->w_selectfn; 
#endif

    widgetbehavior.w_visfn =        cb_GfxVis;
    widgetbehavior.w_selectfn =     cb_GfxSelect; 

#if PD_MINOR_VERSION >= 37
    class_setpropertiesfn(c,cb_GfxProperties);
    class_setsavefn(c,cb_GfxSave);
#else
    widgetbehavior.w_propertiesfn = cb_GfxProperties;
    widgetbehavior.w_savefn =       cb_GfxSave;
#endif

    tclscript();
}

static size_t escapeit(char *dst,size_t maxlen,const char *src)
{
    char *d;
    for(d = dst; *src && (d-dst) < (int)maxlen; ++src) {
        if(*src == '%')
            *(d++) = '%',*(d++) = '%';
        else
            *(d++) = *src;
    }
    *d = 0;
    return d-dst;
}

void flext_base::cb_GfxProperties(t_gobj *c, t_glist *)
{
    flext_base *th = thisObject(c);
    char buf[1000];

     // beginning of proc
    sys_vgui("proc pdtk_flext_dialog_%p {title} {\n",th);

    sys_vgui("pdtk_flext_dialog $title {\n");

    // add title
    t_text *x = (t_text *)c;
    FLEXT_ASSERT(x->te_binbuf);

    int argc = binbuf_getnatom(x->te_binbuf);
    t_atom *argv = binbuf_getvec(x->te_binbuf);

    PrintList(argc,argv,buf,sizeof(buf));
    sys_vgui("%s } {\n",buf);

    AtomList la;
    th->ListAttrib(la);
    int cnt = la.Count();

    for(int i = 0; i < cnt; ++i) {
        const t_symbol *sym = GetSymbol(la[i]); 

        // get attribute
        AttrItem *gattr = th->FindAttrib(sym,true);
        // get puttable attribute
        AttrItem *pattr = gattr?gattr->Counterpart():th->FindAttrib(sym,false);

        // get flags
        int sv;
        const AtomList *initdata;
        AttrDataCont::iterator it = th->attrdata->find(sym);
        if(it == th->attrdata->end())
            sv = 0,initdata = NULL;
        else {
            const AttrData &a = *it.data();
            if(a.IsSaved())
                sv = 2;
            else if(a.IsInit())
                sv = 1;
            else 
                sv = 0;
            initdata = a.IsInitValue()?&a.GetInitValue():NULL;
        }

        // get attribute type
        int tp;
        bool list;
        switch((gattr?gattr:pattr)->argtp) {
            case a_int: tp = 0; list = false; break;
            case a_float: tp = 1; list = false; break;
            case a_bool: tp = 2; list = false; break;
            case a_symbol: tp = 3; list = true; break;
            case a_list: 
            case a_LIST: tp = 4; list = true; break;
            default: 
                tp = 5; list = true; 
                FLEXT_ASSERT(false);
        }

        sys_vgui(const_cast<char *>(list?"%s {\n":"%s\n"),GetString(sym));

        AtomList lv;
        if(gattr) { // gettable attribute is present
            // Retrieve attribute value
            th->GetAttrib(sym,gattr,lv);

            char *b = buf; *b = 0;
            for(int i = 0; i < lv.Count(); ++i) {
                char tmp[100];
                PrintAtom(lv[i],tmp,sizeof tmp);
                b += escapeit(b,sizeof(buf)+buf-b,tmp);
                if(i < lv.Count()-1) { *(b++) = ' '; *b = 0; }
            }
            sys_vgui("%s\n",buf);
        }
        else
            sys_vgui("{}\n");

        sys_vgui(const_cast<char *>(list?"} {\n":" \n"));

        if(pattr) {
            // if there is initialization data take this, otherwise take the current data
            const AtomList &lp = initdata?*initdata:lv;

            char *b = buf; *b = 0;
            for(int i = 0; i < lp.Count(); ++i) {
                char tmp[256];
                PrintAtom(lp[i],tmp,sizeof(tmp)); 
                b += escapeit(b,sizeof(buf)+buf-b,tmp);
                if(i < lp.Count()-1) { *(b++) = ' '; *b = 0; }
            }
            sys_vgui("%s\n",buf);
        }
        else
            sys_vgui("{}\n");


        sys_vgui(const_cast<char *>(list?"} %i %i %i \n":" %i %i %i \n"),tp,sv,pattr?(pattr->BothExist()?2:1):0);
    }

    sys_vgui(" } }\n"); // end of proc

    STD::sprintf(buf,"pdtk_flext_dialog_%p %%s\n",th);
    gfxstub_new((t_pd *)th->thisHdr(), th->thisHdr(),buf);

    //! \todo delete proc in TCL space
}

//! Strip the attributes off the object command line
void flext_base::cb_GfxVis(t_gobj *c, t_glist *gl, int vis)
{
    // show object if it's not a GOP
    if(!gl->gl_isgraph || gl->gl_havewindow) {
        flext_base *th = thisObject(c);
        t_text *x = (t_text *)c;
        FLEXT_ASSERT(x->te_binbuf);

        t_binbuf *b = binbuf_new();
        th->BinbufArgs(b,x->te_binbuf,true,false);

        // delete old object box text
        binbuf_free(x->te_binbuf);
        // set new one
        x->te_binbuf = b;

        t_rtext *rt = glist_findrtext(gl,x);
        rtext_retext(rt);

        // now display the changed text with the normal drawing function
    #ifdef FLEXT_CLONEWIDGET
        text_widgetbehavior.w_visfn(c,gl,vis);
    #else
        ori_vis(c,gl,vis);
    #endif
   }
}

void flext_base::cb_GfxSelect(t_gobj *c,t_glist *gl,int state)
{
    // show object if it's not a GOP
    if(!gl->gl_isgraph || gl->gl_havewindow) {
        if(state || !gl->gl_editor->e_textdirty) {
            // change text only on selection
            // OR if text has _not_ been changed 
            // ->  since object will not be recreated we have to get rid
            //     of the attribute text

            flext_base *th = thisObject(c);
            t_text *x = (t_text *)c;
            FLEXT_ASSERT(x->te_binbuf);

            t_binbuf *b = binbuf_new();
            th->BinbufArgs(b,x->te_binbuf,true,false);
            if(state) th->BinbufAttr(b,false);

            // delete old object box text
            binbuf_free(x->te_binbuf);
            // set new one
            x->te_binbuf = b;

            t_rtext *rt = glist_findrtext(gl,x);
            rtext_retext(rt);

            // fix lines
            canvas_fixlinesfor(gl,x);
        }

        // call original function
        #ifdef FLEXT_CLONEWIDGET
            text_widgetbehavior.w_selectfn(c,gl,state);
        #else
            ori_select(c,gl,state);
        #endif
    }
}

void flext_base::cb_GfxSave(t_gobj *c, t_binbuf *b)
{
    flext_base *th = thisObject(c);
    t_text *t = (t_text *)c;
    binbuf_addv(b, "ssiis", gensym("#X"),gensym("obj"), t->te_xpix, t->te_ypix,MakeSymbol(th->thisName()));

    // process the object arguments
    th->BinbufArgs(b,t->te_binbuf,false,true);
    // process the attributes
    th->BinbufAttr(b,true);
    // add end sign
    binbuf_addv(b, ";");
}

bool flext_base::cb_AttrDialog(flext_base *th,int argc,const t_atom *argv)
{
    for(int i = 0; i < argc; ) {
        FLEXT_ASSERT(IsSymbol(argv[i]));

        // get name
        const t_symbol *aname = GetSymbol(argv[i]);
        i++;

        // get current value
        FLEXT_ASSERT(CanbeInt(argv[i]));
        int ccnt,coffs;
        ccnt = GetAInt(argv[i]);
        coffs = ++i;
        i += ccnt;

        // get init value
        FLEXT_ASSERT(CanbeInt(argv[i]));
        int icnt,ioffs;
        icnt = GetAInt(argv[i]);
        ioffs = ++i;
        i += icnt;

        FLEXT_ASSERT(i < argc);
        int sv = GetAInt(argv[i]);
        ++i;

        // find puttable attribute
        AttrItem *attr = th->FindAttrib(aname,false);
        if(attr) {
            bool ret = th->SetAttrib(aname,attr,ccnt,argv+coffs);
            FLEXT_ASSERT(ret);

            AttrDataCont::iterator it = th->attrdata->find(aname);

            if(sv >= 1) {
                // if data not present create it
                if(it == th->attrdata->end()) {
                    AttrDataCont::pair pair; 
                    pair.key() = aname;
                    pair.data() = new AttrData;
                    it = th->attrdata->insert(th->attrdata->begin(),pair);
                }

                AttrData &a = *it.data();
                a.SetSave(sv == 2);
                a.SetInit(true);
                a.SetInitValue(icnt,argv+ioffs);
            }
            else {
                if(it != th->attrdata->end()) {
                    AttrData &a = *it.data();
                    // if data is present reset flags
                    a.SetSave(false);
                    a.SetInit(false);

                    // let init data as is
                }
            }
        }
        else {
            post("%s - Attribute %s can't be set",th->thisName(),GetString(aname));
        }
    }
    return true;
}


static void BinbufAdd(t_binbuf *b,const t_atom &at,bool transdoll)
{
    if(transdoll && at.a_type == A_DOLLAR) {
        char tbuf[MAXPDSTRING];
        sprintf(tbuf, "$%d", at.a_w.w_index);
        binbuf_addv(b,"s",flext::MakeSymbol(tbuf));
    }
    else if(transdoll && at.a_type == A_DOLLSYM) {
        char tbuf[MAXPDSTRING];
        sprintf(tbuf, "$%s", at.a_w.w_symbol->s_name);
        binbuf_addv(b,"s",flext::MakeSymbol(tbuf));
    }
    else
        binbuf_add(b,1,const_cast<t_atom *>(&at));
}

void flext_base::BinbufArgs(t_binbuf *b,t_binbuf *args,bool withname,bool transdoll)
{
    int argc = binbuf_getnatom(args);
    t_atom *argv = binbuf_getvec(args);
    int i,cnt = CheckAttrib(argc,argv);
    // process the creation arguments
    for(i = withname?0:1; i < cnt; ++i) BinbufAdd(b,argv[i],transdoll);
}

void flext_base::BinbufAttr(t_binbuf *b,bool transdoll)
{
    // process the attributes
    AtomList la;
    ListAttrib(la);
    int i,cnt = la.Count();

    for(i = 0; i < cnt; ++i) {
        const t_symbol *sym = GetSymbol(la[i]);
        AtomList lv;
        const AtomList *lref = NULL;
        AttrDataCont::iterator it = attrdata->find(sym);

        if(it != attrdata->end()) {
            const AttrData &a = *it.data();
            if(a.IsInit() && a.IsInitValue()) {
                lref = &a.GetInitValue();
/*
                // check for $-parameters
                lv = lref->Count();
                for(int j = 0; j < lref->Count(); ++j) {
                    const char *s = IsSymbol((*lref)[j])?GetString((*lref)[j]):NULL;
                    if(s && s[0] == '$') { // TODO: More refined checking?
                        // prepend a "\"
                        char tmp[256]; *tmp = '\\';
                        strcpy(tmp+1,s);
                        SetString(lv[j],tmp);
                    }
                    else
                        lv[i] = (*lref)[j];
                }

                lref = &lv;
*/
            }
            else if(a.IsSaved()) {
                AttrItem *attr = FindAttrib(sym,true);

                // attribute must be gettable (so that the data can be retrieved) and puttable (so that the data can be inited)
                if(attr && attr->BothExist()) {
                    GetAttrib(sym,attr,lv); 
                    lref = &lv;
                }
            }
        }

        if(lref) {
            char attrname[256]; *attrname= '@';
            // store name
            strcpy(attrname+1,GetString(sym));
            binbuf_addv(b,"s",MakeSymbol(attrname));

            // store value
            for(int j = 0; j < lref->Count(); ++j) BinbufAdd(b,(*lref)[j],transdoll);
        }
    }
}

#endif // FLEXT_NOATTREDIT

#endif // FLEXT_SYS_PD