added sample GUI module with UIP
[k8lst.git] / modules / guiModules / gui.st
blob2ae40a98d586c9407d5d1ab19aeb0471b48aa44f
1 Package [
2   GUI
6 class: GuiWidget [
7   | id ablock |
11 class: GuiSingleton  | widgetlist quitflag | [
12   ^idToString: elid [
13     <#IUPDispatcher 250 elid>.
14     ^nil
15   ]
17   ^message: msg title: title [
18     <#IUPDispatcher 1 title msg>
19   ]
21   ^message: msg  [
22     ^self message: msg title: 'LittleSmalltalk Message'
23   ]
25   ^canvas: action [
26     <#IUPDispatcher 2 0 action>.
27     self primitiveFailed
28   ]
30   ^button: label action: action [
31     <#IUPDispatcher 2 1 action label>.
32     self primitiveFailed
33   ]
35   ^toggle: label action: action [
36     <#IUPDispatcher 2 2 action label>.
37     self primitiveFailed
38   ]
40   ^edit: action [
41     <#IUPDispatcher 2 3 action>.
42     self primitiveFailed
43   ]
45   ^multiEdit: action [
46     <#IUPDispatcher 2 4 action>.
47     self primitiveFailed
48   ]
50   ^list: action [
51     <#IUPDispatcher 2 5 action>.
52     self primitiveFailed
53   ]
55   ^label: label [
56     <#IUPDispatcher 2 6 label>.
57     self primitiveFailed
58   ]
60   ^progressBar [
61     <#IUPDispatcher 2 7>.
62     self primitiveFailed
63   ]
65   ^spin [
66     <#IUPDispatcher 2 8>.
67     self primitiveFailed
68   ]
70   ^tree [
71     <#IUPDispatcher 2 9>.
72     self primitiveFailed
73   ]
75   ^frame: elid [
76     <#IUPDispatcher 2 10 elid>.
77     self primitiveFailed
78   ]
80   ^dialog: elid [
81     <#IUPDispatcher 2 11 elid>.
82     self primitiveFailed
83   ]
85   ^fill [
86     <#IUPDispatcher 3 0>.
87     self primitiveFailed
88   ]
90   ^hbox [
91     <#IUPDispatcher 3 1>.
92     self primitiveFailed
93   ]
95   ^vbox [
96     <#IUPDispatcher 3 2>.
97     self primitiveFailed
98   ]
100   ^zbox [
101     <#IUPDispatcher 3 3>.
102     self primitiveFailed
103   ]
105   ^radiobox: elid [
106     <#IUPDispatcher 3 4 elid>.
107     self primitiveFailed
108   ]
110   ^cbox [
111     <#IUPDispatcher 3 5>.
112     self primitiveFailed
113   ]
115   ^sbox [
116     <#IUPDispatcher 3 6>.
117     self primitiveFailed
118   ]
120   ^normalizer [
121     <#IUPDispatcher 3 7>.
122     self primitiveFailed
123   ]
125   ^split: elid0 and: elid1 [
126     <#IUPDispatcher 3 8 elid0 elid1>.
127     self primitiveFailed
128   ]
130   ^split [
131     <#IUPDispatcher 3 8>.
132     self primitiveFailed
133   ]
135   ^attachTo: boxid element: elid [
136     <#IUPDispatcher 4 boxid elid>.
137     self primitiveFailed
138   ]
140   ^detach: elid [
141     <#IUPDispatcher 5 elid>.
142     self primitiveFailed
143   ]
145   ^destroy: elid [
146     <#IUPDispatcher 6 elid>.
147     self primitiveFailed
148   ]
150   ^show: elid [
151     <#IUPDispatcher 7 elid>.
152     self primitiveFailed
153   ]
155   ^registerEvent: aName [
156     <#IUPDispatcher 8 aName>.
157     self primitiveFailed
158   ]
160   ^unregisterEvent: aName [
161     <#IUPDispatcher 8 aName false>.
162     self primitiveFailed
163   ]
165   ^registerListEvent: aName [
166     <#IUPDispatcher 9 aName>.
167     self primitiveFailed
168   ]
170   ^unregisterListEvent: aName [
171     <#IUPDispatcher 9 aName false>.
172     self primitiveFailed
173   ]
175   ^getEvent [
176     <#IUPDispatcher 10>.
177     self primitiveFailed
178   ]
180   ^setAttribute: elid name: aName value: value [
181     <#IUPDispatcher 11 elid aName value>.
182     self primitiveFailed
183   ]
185   ^getAttribute: elid name: aName [
186     <#IUPDispatcher 12 elid aName>.
187     self primitiveFailed
188   ]
190   ^deleteAttribute: elid name: aName [
191     <#IUPDispatcher 13 elid aName>.
192     self primitiveFailed
193   ]
195   ^popup: elid [
196     <#IUPDispatcher 14 elid>.
197     self primitiveFailed
198   ]
200   ^show: elid [
201     <#IUPDispatcher 15 elid>.
202     self primitiveFailed
203   ]
205   ^hide: elid [
206     <#IUPDispatcher 15 elid true>.
207     self primitiveFailed
208   ]
210   ^addWidget: aWC [
211     widgetlist ifNil: [ widgetlist := List new ].
212     widgetlist << aWC.
213   ]
215   ^removeWidget: aWC [
216     widgetlist ifNotNil: [ widgetlist remove: aWC ifAbsent: [ nil ]]
217   ]
219   ^findWidgetById: aId [
220     widgetlist ifNotNil: [ widgetlist do: [:w | w idToString = aId ifTrue: [ ^w ]]].
221     ^nil
222   ]
224   ^setQuitFlag [
225     quitflag := true.
226   ]
228   ^passEvent: aId [
229     widgetlist ifNotNil: [ widgetlist do: [:w | (w passEvent: aId) ifTrue: [ ^true ]]].
230     ^false
231   ]
233   ^mainLoop [
234     quitflag := false.
235     [ quitflag ] whileFalse: [
236       System eventWaitFor: 1100.
237       self passEvent: (self getEvent).
238     ].
239   ]
243 GuiWidget extend [
244   ^new [
245     self error: 'GuiWidget instances must be created with special methods'.
246   ]
248   ^newWithId: aId [
249     | obj |
250     obj := self basicNew.
251     self in: obj at: 1 put: aId.
252     ^obj
253   ]
255   idToString [
256     ^GuiSingleton idToString: id
257   ]
259   id [
260     ^id
261   ]
263   setAttribute: aName value: value [
264     GuiSingleton setAttribute: id name: aName value: value
265   ]
267   attribute: aName [
268     ^GuiSingleton getAttribute: id name: aName
269   ]
271   removeAttribute: aName [
272     GuiSingleton deleteAttribute: id name: aName
273   ]
275   popup [
276     GuiSingleton popup: id
277   ]
279   show [
280     GuiSingleton show: id
281   ]
283   passEvent: aId [
284     "returns true if event handler found"
285     self idToString = aId ifTrue: [ self onEvent. ^true ].
286     ^false
287   ]
289   detach [
290     GuiSingleton detach: id
291   ]
293   attachTo: aWid [
294     GuiSingleton attachTo: aWid id element: id
295   ]
299 GuiWidget subclass: GuiButton [
300   ^new: aText actionBlock: aBlock [
301     | obj |
302     obj := self newWithId: (GuiSingleton button: aText action: true).
303     self in: obj var: #ablock put: aBlock.
304     ^obj
305   ]
307   onEvent [
308     ablock ifNotNil: [ ablock value ].
309   ]
313 GuiWidget subclass: GuiRestrictedGroup [
314   | widgetlist |
316   containsWidget: aWC [
317     widgetlist ifNotNil: [ ^widgetlist includes: aWC ].
318     ^false
319   ]
321   findWidgetById: aId [
322     widgetlist ifNotNil: [ widgetlist do: [:w | w idToString = aId ifTrue: [ ^w ]]].
323     ^nil
324   ]
326   passEvent: aId [
327     "returns true if event handler found"
328     | w |
329     (super passEvent: aId) ifTrue: [ ^true ].
330     (w := self findWidgetById: aId) ifNotNil: [ w onEvent. ^true ].
331     widgetlist ifNotNil: [ widgetlist do: [:w | (w passEvent: aId) ifTrue: [ ^true ]]].
332     ^false
333   ]
337 GuiRestrictedGroup subclass: GuiGroup [
338   addWidget: aWC [
339     widgetlist ifNil: [ widgetlist := List new ].
340     widgetlist << aWC.
341   ]
343   removeWidget: aWC [
344     widgetlist ifNotNil: [ widgetlist remove: aWC ifAbsent: [ nil ]]
345   ]
349 GuiGroup subclass: GuiBox [
350   addWidget: aWid [
351     aWid; detach; attachTo: self.
352     ^super addWidget: aWid
353   ]
355   removeWidget: aWid [
356     (self containsWidget: aWid) ifTrue: [
357       aWid detach.
358       super removeWidget: aWid.
359     ]
360   ]
363 GuiBox subclass: GuiHBox [
364   ^new [
365     ^self newWithId: (GuiSingleton hbox).
366   ]
369 GuiBox subclass: GuiVBox [
370   ^new [
371     ^self newWithId: (GuiSingleton vbox).
372   ]
375 GuiBox subclass: GuiCBox [
376   ^new [
377     ^self newWithId: (GuiSingleton cbox).
378   ]
381 GuiBox subclass: GuiSBox [
382   ^new [
383     ^self newWithId: (GuiSingleton sbox).
384   ]
387 GuiBox subclass: GuiNormBox [
388   ^new [
389     ^self newWithId: (GuiSingleton normalizer).
390   ]
394 GuiRestrictedGroup subclass: GuiSplit [
395   ^new: aWid0 and: aWid1 [
396     | obj |
397     obj := self newWithId: (GuiSingleton split: aWid0 and aWid1).
398     self in: obj var: #widgetlist put: (List with: aWid0 with: aWid1).
399     ^obj
400   ]
402   left [
403     ^widgetlist first
404   ]
406   right [
407     ^widgetlist at: 1
408   ]
413 GuiRestrictedGroup subclass: GuiDialog [
414   ^new: aTitle widget: aWidget [
415     | obj |
416     obj := self newWithId: (GuiSingleton dialog: aWidget id).
417     self in: obj var: #widgetlist put: (List with: aWidget).
418     obj setAttribute: 'TITLE' value: aTitle asString.
419     ^obj
420   ]
422   mainWidget [
423     ^widgetlist first
424   ]