deprecate SCViewHolder-layRight
[supercollider.git] / SCClassLibrary / Common / GUI / Base / EZNumber.sc
blob95713b6d8a07e798e2bf142094444eba55a16896
1 EZNumber : EZGui{
2         var <numberView, <unitView, <>controlSpec,
3                  numSize,numberWidth,unitWidth, gap, gap2;
4         var <>round = 0.001;
6         var scaler=1;  //for swing compatibility
7         *new { arg parent, bounds, label, controlSpec, action, initVal,
8                         initAction=false, labelWidth=60, numberWidth,
9                         unitWidth=0, labelHeight=20,  layout=\horz, gap, margin;
11                 ^super.new.init(parent, bounds, label, controlSpec, action,
12                         initVal, initAction, labelWidth, numberWidth,
13                                 unitWidth, labelHeight, layout, gap, margin)
14         }
16         init { arg parentView, bounds, label, argControlSpec, argAction, initVal,
17                         initAction, labelWidth, argNumberWidth,argUnitWidth,
18                         labelHeight, argLayout, argGap, argMargin;
20                 var labelBounds, numBounds, unitBounds;
21                 var numberStep;
23                 // Set Margin and Gap
24                 this.prMakeMarginGap(parentView, argMargin, argGap);
27                 unitWidth = argUnitWidth;
28                 numberWidth = argNumberWidth;
29                 layout=argLayout;
31                 bounds.isNil.if {bounds= 160@20};
33                 // if no parent, then pop up window
34                 # view,bounds = this.prMakeView( parentView,bounds);
36                 numberWidth.isNil.if{numberWidth=45}{
37                         labelWidth=bounds.width-unitWidth-numberWidth; //override the labelWidth
38                         if (layout==\line2){unitWidth=bounds.width-numberWidth}; //override the unitWidth
39                 };
40                 labelSize=labelWidth@labelHeight;
41                 numSize = numberWidth@labelHeight;
43                 // calcualate bounds
44                 # labelBounds,numBounds, unitBounds
45                                 = this.prSubViewBounds(innerBounds, label.notNil, unitWidth>0);
47                 // insert the views
49                 label.notNil.if{ //only add a label if desired
50                                 labelView = GUI.staticText.new(view, labelBounds);
51                         if (layout==\line2)
52                                 {labelView.align = \left;}
53                                 {labelView.align = \right;};
54                         labelView.string = label;
55                 };
57                 (unitWidth>0).if{ //only add a unitLabel if desired
58                         unitView = GUI.staticText.new(view, unitBounds);
59                 };
61                 // set view parameters and actions
62                 controlSpec = argControlSpec.asSpec;    // let default to nil.asSpec!
63                 (unitWidth>0).if{ unitView.string = " "++controlSpec.units.asString};
64                 initVal = initVal ? controlSpec.default;
65                 action = argAction;
67                 numberView = GUI.numberBox.new(view, numBounds).resize_(2);
69                 numberStep = controlSpec.step;
70                 if (numberStep == 0) {
71                         numberStep = controlSpec.guessNumberStep;
72                 }{
73                         // controlSpec wants a step, so zooming in with alt is disabled.
74                         numberView.alt_scale = 1.0
75                 };
77                 numberView.step = numberStep;
78                 numberView.scroll_step = numberStep;
79                 numberView.scroll=true;
81                 numberView.action = {
82                         this.valueAction_(numberView.value);
83                 };
85                 if (initAction) {
86                         this.valueAction = initVal;
87                 }{
88                         this.value = initVal;
89                 };
91                 this.prSetViewParams;
92         }
94         value_{ arg val;
95                 value = controlSpec.constrain(val);
96                 numberView.value = value.round(round);
97         }
98         valueAction_ { arg val;
99                 this.value_(val);
100                 this.doAction;
101         }
102         doAction { action.value(this) }
104         set { arg label, spec, argAction, initVal, initAction=false;
105                 labelView.notNil.if { labelView.string = label.asString };
106                 spec.notNil.if { controlSpec = spec.asSpec };
107                 argAction.notNil.if { action = argAction };
108                 initVal = initVal ? value ? controlSpec.default;
109                 if (initAction) {
110                         this.valueAction_(initVal);
111                 }{
112                         this.value_(initVal);
113                 };
114         }
116         setColors{ arg stringBackground, stringColor,numBackground,numStringColor,
117                          numNormalColor, numTypingColor, background ;
119                 stringBackground.notNil.if{
120                         labelView.notNil.if{labelView.background_(stringBackground)};
121                         unitView.notNil.if{unitView.background_(stringBackground)};};
122                 stringColor.notNil.if{
123                         labelView.notNil.if{labelView.stringColor_(stringColor)};
124                         unitView.notNil.if{unitView.stringColor_(stringColor)};};
125                 numBackground.notNil.if{
126                         numberView.background_(numBackground);  };
127                 numNormalColor.notNil.if{
128                         numberView.normalColor_(numNormalColor);};
130                 numTypingColor.notNil.if{
131                         numberView.typingColor_(numTypingColor);};
132                 numStringColor.notNil.if{
133                         numberView.stringColor_(numStringColor);};
134                 background.notNil.if{
135                         view.background=background;};
136         }
139         font_{ arg font;
141                         labelView.notNil.if{labelView.font=font};
142                         unitView.notNil.if{unitView.font=font};
143                         numberView.font=font;
144         }
146         /////// Private methods /////////
148         prSetViewParams{ // sets resize and alignment for different layouts
150                 switch (layout,
151                 \line2, {
152                         labelView.notNil.if{labelView.resize_(2)};
153                         unitView.notNil.if{unitView.resize_(6)};
154                         numberView.resize_(5);
155                 },
156                 \horz, {
157                         labelView.notNil.if{
158                                 labelView.resize_(4).align_(\right);
159                                 numberView.resize_(5);
160                                 unitView.notNil.if{unitView.resize_(6)};
161                         }{
162                                 unitView.notNil.if{     unitView.resize_(6)};
163                                 numberView.resize_(5);
164                         };
165                 });
166                         popUp.if{view.resize_(2)};
167         }
169         prSubViewBounds{arg rect, hasLabel, hasUnit;
170                 var numBounds,labelBounds,sliderBounds;
171                 var unitBounds, gap1, gap2, numY;
172                 gap1 = gap.copy;
173                 gap2 = gap.copy;
174                 hasLabel.not.if{ gap1 = 0@0; labelSize=0@0};
175                 hasUnit.not.if{gap2 = 0@0};
177                 switch (layout,
178                         \line2, {
180                                 labelBounds = Rect( // fill the line
181                                                 0,
182                                                 0,
183                                                 rect.width,
184                                                 labelSize.y;
185                                                 );
187                                 numSize.y=numSize.y-gap1.y;
188                                 numY=labelBounds.height+gap1.y;
189                                 unitBounds = Rect( rect.width - unitWidth, // //adjust to fit
190                                         numY, unitWidth, rect.height-labelSize.y-gap1.y);
191                                 numBounds = Rect(0, numY,
192                                         rect.width-unitWidth-gap2.x, rect.height-labelSize.y-gap1.y); // to right
194                                 },
196                          \horz, {
197                                 labelSize.y=rect.height;
198                                 labelBounds = (labelSize.x@labelSize.y).asRect; // to left
199                                 unitBounds = (unitWidth@labelSize.y).asRect.left_(rect.width-unitWidth); // to right
200                                 numBounds  =  Rect( //adjust to fit
201                                         labelBounds.width+gap1.x,
202                                         0,
203                                         rect.width - labelBounds.width - unitBounds.width - gap1.x - gap2.x ,
204                                         labelBounds.height
205                                         );
206                 });
209                 ^[labelBounds, numBounds, unitBounds].collect{arg v; v.moveBy(margin.x,margin.y)}
210         }