File: EZgui.sc

package info (click to toggle)
supercollider 1%3A3.13.0%2Brepack-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 80,292 kB
  • sloc: cpp: 476,363; lisp: 84,680; ansic: 77,685; sh: 25,509; python: 7,909; makefile: 3,440; perl: 1,964; javascript: 974; xml: 826; java: 677; yacc: 314; lex: 175; objc: 152; ruby: 136
file content (214 lines) | stat: -rw-r--r-- 5,612 bytes parent folder | download | duplicates (5)
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
EZGui { // an abstract class
	var <>labelView, <widget, <view, gap, popUp = false, innerBounds,
	<>action,   <layout, <value, labelSize, <alwaysOnTop = false, margin;
	var scaler = 1;  //for swing compatibility

	// general stuff for all EZ classes. override as needed
	visible { ^view.getProperty(\visible) }
	visible_ { |bool|  view.setProperty(\visible,bool)  }

	enabled {  ^view.enabled }
	enabled_ { |bool| view.enabled_(bool) }

	remove { view.remove}

	alwaysOnTop_ { |bool|
		alwaysOnTop=bool;
		popUp.if{this.window.alwaysOnTop=alwaysOnTop};

	}
	font_ { |font|
		labelView.notNil.if { labelView.font = font };
		widget.font=font;
	}

	window { ^popUp.if { view.parent.findWindow } }

	onClose { ^popUp.if { this.window.onClose } { view.onClose } }

	onClose_ { |func| popUp.if { this.window.onClose_(func) } { view.onClose_(func) } }


	//// value stuff. override as needed


	value_ { |val| value = val; widget.value_(val) }

	valueAction_ { |val| this.value_(val); this.doAction }

	doAction { this.action.value(this) }

	bounds { ^view.bounds }


	label {
		^labelView !? { labelView.string }
	}

	prSetViewParams {
		^this.subclassResponsibility
	}

	///////// private methods. You can still override these in subclasses

	prSubViewBounds { |rect, hasLabel|
		var widgetBounds, labelBounds, tmp;
		hasLabel.not.if { gap = 0@0; labelSize = 0@0 };

		if (layout == \vert) {
			widgetBounds= Rect(  // fit to full width
				0,
				labelSize.y + gap.y,
				rect.width,
				rect.height - labelSize.y - gap.y
			);
			labelBounds = Rect(0, 0, widgetBounds.width, labelSize.y); //fit to full width
		} {
			widgetBounds = Rect(   // fit to full remaining
				labelSize.x + gap.x,
				0,
				rect.width - labelSize.x - gap.x,
				rect.height
			);
			labelBounds = Rect(0, 0, labelSize.x, widgetBounds.height ) // to left
		};

		^[labelBounds, widgetBounds].collect { |v| v.moveBy(margin.x, margin.y) }
	}


	prMakeView { |parentView,bounds| // return a container, or a popUpWindow with a container
		var w, winBounds, view, extraHeight = 24;
		(GUI.id == \swing).if { extraHeight = 36 };
		parentView.isNil.if {
			popUp = true;
			// if bounds is a point then place the window on screen
			if (bounds.class == Point) {
				bounds = bounds.x@max(bounds.y, bounds.y+extraHeight);// window minimum height;
				winBounds = Rect(200,
					Window.screenBounds.height-bounds.y - 100,
					bounds.x,
					bounds.y
				)
			} { // window minimum height;
				winBounds = bounds.height_(max(bounds.height, bounds.height + extraHeight))
			};
			w = Window.new("", winBounds).alwaysOnTop_(alwaysOnTop);
			parentView = w.asView;
			w.front;
			bounds = bounds.asRect;
			// inset the bounds to make a nice margin
			bounds = Rect(4,4,bounds.width-8,bounds.height-extraHeight);
			view = CompositeView.new(parentView, bounds).resize_(2);
			w.bounds = w.bounds; // swing needs this for some reason, or bounds are too high

		} { // normal parent view
			bounds = bounds.asRect;
			view = CompositeView.new(parentView,bounds);
		};

		innerBounds = view.bounds.insetBy(margin.x, margin.y);
		^[view,bounds];
	}

	prMakeMarginGap { |parentView, argMargin, argGap|
		//try to use the parent decorator gap
		var	decorator = parentView.asView.tryPerform(\decorator);
		argGap.isNil.if {
			gap = gap ? decorator.tryPerform(\gap).copy; // use copy to protect the parent gap
			gap = gap ? (2@2)
		} {
			gap=argGap
		};

		argMargin.isNil.if { margin = 0@0 } { margin = argMargin };
	}
}


EZLists : EZGui {  // an abstract class

	var <items, <>globalAction;

	*new { |parentView, bounds, label,items, globalAction, initVal = 0,
		initAction = false, labelWidth,labelHeight = 20, layout, gap, margin|

		^super.new.init(parentView, bounds, label, items, globalAction, initVal,
			initAction, labelWidth,labelHeight,layout, gap, margin);
	}

	init { |parentView, bounds, label, argItems, argGlobalAction, initVal,
		initAction, labelWidth, labelHeight, layout,  argGap, argMargin|

		// try to use the parent decorator gap
		this.prMakeMarginGap(parentView, argMargin, argGap);

		// init the views (handled by subclasses)
		this.initViews( parentView, bounds, label, labelWidth,labelHeight,layout );

		this.items = argItems ? [];

		globalAction = argGlobalAction;

		widget.action = { |obj|
			items.at(obj.value).value.value(this);
			globalAction.value(this);
		};

		this.value_(initVal);

		items.notNil.if {
			if(initAction) {
				items.at(initVal).value.value(this); // You must do this like this
				globalAction.value(this);	// since listView's array is not accessible yet
			};
			this.value_(initVal);
		};

	}

	initViews { }  // override this for your subclass views

	value { ^widget.value }
	value_ {|val| widget.value = val }

	valueAction_ { |val| widget.value_(val); this.doAction }

	doAction { widget.doAction }

	items_{ |assocArray|
		assocArray = assocArray.collect { |it| if (it.isKindOf(Association), { it }, { it -> nil }) };
		items = assocArray;
		widget.items = assocArray.collect { |item| item.key };
	}

	item { ^items.at(this.value).key }
	itemFunc { ^items.at(this.value).value }

	addItem { |name, action|
		this.insertItem(nil, name, action);
	}

	insertItem { |index, name, action|
		var temp;
		index = index ? items.size;
		this.items = items.insert(index, name.asSymbol -> action);
	}

	removeItemAt { |index|
		var temp;
		items.removeAt(index);
		this.items_(items)

	}

	replaceItemAt { |index, name, action|
		var temp;
		name = name ? items.at(index).key;
		action = action ? items.at(index).value;
		this.removeItemAt(index);
		this.insertItem(index, name, action);

	}

}