Merge pull request #506 from andrewcsmith/patch-2
[supercollider.git] / SCClassLibrary / Common / GUI / Base / EZText.sc
blob444cfec522a86d5d12741d2af3ebdcb091ccbcb8
1         //      value can be any object,
2         //      display is asCompileString
3 EZText : EZGui {
4         var <textField;
6         *new { arg parent, bounds, label, action, initVal,
7                         initAction=false, labelWidth=60, textWidth,
8                         labelHeight=20,  layout=\horz, gap, margin;
10                 ^super.new.init(parent, bounds, label, action,
11                         initVal, initAction, labelWidth, textWidth,
12                                 labelHeight, layout, gap, margin)
13         }
15         init { arg parentView, bounds, label, argAction, initVal,
16                         initAction, labelWidth, argTextWidth,
17                         labelHeight, argLayout, argGap, argMargin;
19                 var labelBounds, textBounds, textSize;
20                 var textWidth = argTextWidth;
22                 // Set Margin and Gap
23                 this.prMakeMarginGap(parentView, argMargin, argGap);
26                 layout=argLayout;
28                 bounds.isNil.if {bounds= 200@20};
30                 // if no parent, then pop up window
31                 # view,bounds = this.prMakeView( parentView,bounds);
33                 textWidth.isNil.if{textWidth=140}{
34                         labelWidth=bounds.width-textWidth; //override the labelWidth
35                 //      if (layout==\line2){unitWidth=bounds.width-textWidth}; //override the unitWidth
36                 };
37                 labelSize=labelWidth@labelHeight;
38                 textSize = textWidth@labelHeight;
40                 // calculate bounds
41                 # labelBounds,textBounds
42                                 = this.prSubViewBounds(innerBounds, label.notNil);
44                 // insert the views
46                 label.notNil.if{ //only add a label if desired
47                                 labelView = GUI.staticText.new(view, labelBounds);
48                         if (layout==\line2)
49                                 {labelView.align = \left;}
50                                 {labelView.align = \right;};
51                         labelView.string = label;
52                 };
54                 // set view parameters and actions
55                 initVal = initVal ? "";
56                 action = argAction;
58                 textField = TextField(view, textBounds).resize_(2);
60                 textField.action = {
61                         var newstr = textField.string;
62                         var newval = try { newstr.interpret };
63                         if (newval.notNil or: { newstr == "" }) {
64                                 this.valueAction_(newval);
65                         } {
66                         //      "EZText compile failed - reset to prev value.".postln;
67                                 textField.string = this.value.asCompileString;
68                         }
69                 };
71                 if (initAction) {
72                         this.valueAction = initVal;
73                 }{
74                         this.value = initVal;
75                 };
77                 this.prSetViewParams;
78         }
80         value_ { |inval|
81                 value = inval;
82                 textField.string = value.asCompileString;
83         }
85         font_{ arg font;
86                 labelView.notNil.if{labelView.font=font};
87                 textField.font=font;
88         }
90         setColors { arg stringBackground, stringColor, textBackground, textStringColor,
91                          textNormalColor, textTypingColor, background ;
93                 stringBackground.notNil.if{
94                         labelView.notNil.if{labelView.background_(stringBackground)};
95                         };
96                 stringColor.notNil.if{
97                         labelView.notNil.if{labelView.stringColor_(stringColor)};
98                         };
99                 textBackground.notNil.if{
100                         textField.background_(textBackground);  };
101                 textNormalColor.notNil.if{
102                         textField.normalColor_(textNormalColor);};
104                 textTypingColor.notNil.if{
105                         textField.typingColor_(textTypingColor);};
106                 textStringColor.notNil.if{
107                         textField.stringColor_(textStringColor);};
108                 background.notNil.if{
109                         view.background=background;};
110         }
112         prSetViewParams { // sets resize and alignment for different layouts
113         switch (layout,
114         \vert, {
115             labelView.notNil.if{labelView.resize_(2).align_(\left)};
116             textField.resize_(5);
117         },
118         \horz, {
119             labelView.notNil.if{
120                 labelView.resize_(4).align_(\right);
121                 textField.resize_(5);
122             }{
123                 textField.resize_(5);
124             };
125         });
126             popUp.if{ view.resize_(2) };
127     }