r21325: delete children in reverse order since the array is manipulated during the...
[samba.git] / webapps / qooxdoo-0.6.3-sdk / frontend / framework / source / class / qx / ui / menu / Button.js
1 /* ************************************************************************
2
3    qooxdoo - the new era of web development
4
5    http://qooxdoo.org
6
7    Copyright:
8      2004-2006 by 1&1 Internet AG, Germany, http://www.1and1.org
9
10    License:
11      LGPL 2.1: http://www.gnu.org/licenses/lgpl.html
12
13    Authors:
14      * Sebastian Werner (wpbasti)
15      * Andreas Ecker (ecker)
16
17 ************************************************************************ */
18
19 /* ************************************************************************
20
21 #module(ui_menu)
22
23 ************************************************************************ */
24
25 qx.OO.defineClass("qx.ui.menu.Button", qx.ui.layout.HorizontalBoxLayout,
26 function(vLabel, vIcon, vCommand, vMenu)
27 {
28   qx.ui.layout.HorizontalBoxLayout.call(this);
29
30
31   // ************************************************************************
32   //   LAYOUT
33   // ************************************************************************
34
35   var io = this._iconObject = new qx.ui.basic.Image;
36   io.setWidth(16);
37   io.setAnonymous(true);
38
39   var lo = this._labelObject = new qx.ui.basic.Label;
40   lo.setAnonymous(true);
41   lo.setSelectable(false);
42
43   var so = this._shortcutObject = new qx.ui.basic.Label;
44   so.setAnonymous(true);
45   so.setSelectable(false);
46
47   var ao = this._arrowObject = new qx.ui.basic.Image("widget/arrows/next.gif");
48   ao.setAnonymous(true);
49
50
51   // ************************************************************************
52   //   INIT
53   // ************************************************************************
54
55   if (qx.util.Validation.isValidString(vLabel)) {
56     this.setLabel(vLabel);
57   }
58
59   if (qx.util.Validation.isValidString(vIcon)) {
60     this.setIcon(vIcon);
61   }
62
63   if (qx.util.Validation.isValid(vCommand)) {
64     this.setCommand(vCommand);
65   }
66
67   if (qx.util.Validation.isValid(vMenu)) {
68     this.setMenu(vMenu);
69   }
70
71
72   // ************************************************************************
73   //   EVENTS
74   // ************************************************************************
75
76   this.addEventListener("mouseup", this._onmouseup);
77 });
78
79
80
81 /*
82 ---------------------------------------------------------------------------
83   PROPERTIES
84 ---------------------------------------------------------------------------
85 */
86
87 qx.OO.changeProperty({ name : "appearance", type : "string", defaultValue : "menu-button" });
88
89 qx.OO.addProperty({ name : "icon", type : "string" });
90 qx.OO.addProperty({ name : "label", type : "string" });
91 qx.OO.addProperty({ name : "menu", type : "object" });
92
93
94
95
96
97
98 /*
99 ---------------------------------------------------------------------------
100   UTILITIES
101 ---------------------------------------------------------------------------
102 */
103
104 qx.Proto._hasIcon = false;
105 qx.Proto._hasLabel = false;
106 qx.Proto._hasShortcut = false;
107 qx.Proto._hasMenu = false;
108
109 qx.Proto.hasIcon = function() {
110   return this._hasIcon;
111 }
112
113 qx.Proto.hasLabel = function() {
114   return this._hasLabel;
115 }
116
117 qx.Proto.hasShortcut = function() {
118   return this._hasShortcut;
119 }
120
121 qx.Proto.hasMenu = function() {
122   return this._hasMenu;
123 }
124
125 qx.Proto.getIconObject = function() {
126   return this._iconObject;
127 }
128
129 qx.Proto.getLabelObject = function() {
130   return this._labelObject;
131 }
132
133 qx.Proto.getShortcutObject = function() {
134   return this._shortcutObject;
135 }
136
137 qx.Proto.getArrowObject = function() {
138   return this._arrowObject;
139 }
140
141 qx.Proto.getParentMenu = function()
142 {
143   var vParent = this.getParent();
144   if (vParent)
145   {
146     vParent = vParent.getParent();
147
148     if (vParent && vParent instanceof qx.ui.menu.Menu) {
149       return vParent;
150     }
151   }
152
153   return null;
154 }
155
156
157
158
159
160 /*
161 ---------------------------------------------------------------------------
162   INIT LAYOUT IMPL
163 ---------------------------------------------------------------------------
164 */
165
166 /*!
167   This creates an new instance of the layout impl this widget uses
168 */
169 qx.Proto._createLayoutImpl = function() {
170   return new qx.renderer.layout.MenuButtonLayoutImpl(this);
171 }
172
173
174
175
176
177 /*
178 ---------------------------------------------------------------------------
179   MODIFIERS
180 ---------------------------------------------------------------------------
181 */
182
183 qx.Proto._modifyEnabled = function(propValue, propOldValue, propData)
184 {
185   if (this._iconObject) {
186     this._iconObject.setEnabled(propValue);
187   }
188
189   if (this._labelObject) {
190     this._labelObject.setEnabled(propValue);
191   }
192
193   if (this._shortcutObject) {
194      this._shortcutObject.setEnabled(propValue);
195   }
196
197   return qx.ui.layout.HorizontalBoxLayout.prototype._modifyEnabled.call(this, propValue, propOldValue, propData);
198 }
199
200 qx.Proto._modifyIcon = function(propValue, propOldValue, propData)
201 {
202   this._iconObject.setSource(propValue);
203
204   if (qx.util.Validation.isValidString(propValue))
205   {
206     this._hasIcon = true;
207
208     if (qx.util.Validation.isInvalidString(propOldValue)) {
209       this.addAtBegin(this._iconObject);
210     }
211   }
212   else
213   {
214     this._hasIcon = false;
215     this.remove(this._iconObject);
216   }
217
218   return true;
219 }
220
221 qx.Proto._modifyLabel = function(propValue, propOldValue, propData)
222 {
223   this._labelObject.setHtml(propValue);
224
225   if (qx.util.Validation.isValidString(propValue))
226   {
227     this._hasLabel = true;
228
229     if (qx.util.Validation.isInvalidString(propOldValue)) {
230       this.addAt(this._labelObject, this.getFirstChild() == this._iconObject ? 1 : 0);
231     }
232   }
233   else
234   {
235     this._hasLabel = false;
236     this.remove(this._labelObject);
237   }
238
239   return true;
240 }
241
242 qx.Proto._modifyCommand = function(propValue, propOldValue, propData)
243 {
244   var vHtml = propValue ? propValue.getShortcut() : "";
245
246   this._shortcutObject.setHtml(vHtml);
247
248   if (qx.util.Validation.isValidString(vHtml))
249   {
250     this._hasShortcut = true;
251
252     var vOldHtml = propOldValue ? propOldValue.getShortcut() : "";
253
254     if (qx.util.Validation.isInvalidString(vOldHtml))
255     {
256       if (this.getLastChild() == this._arrowObject)
257       {
258         this.addBefore(this._shortcutObject, this._arrowObject);
259       }
260       else
261       {
262         this.addAtEnd(this._shortcutObject);
263       }
264     }
265   }
266   else
267   {
268     this._hasShortcut = false;
269     this.remove(this._shortcutObject);
270   }
271
272   return true;
273 }
274
275 qx.Proto._modifyMenu = function(propValue, propOldValue, propData)
276 {
277   if (qx.util.Validation.isValidObject(propValue))
278   {
279     this._hasMenu = true;
280
281     if (qx.util.Validation.isInvalidObject(propOldValue)) {
282       this.addAtEnd(this._arrowObject);
283     }
284   }
285   else
286   {
287     this._hasMenu = false;
288     this.remove(this._arrowObject);
289   }
290
291   return true;
292 }
293
294
295
296
297
298
299 /*
300 ---------------------------------------------------------------------------
301   EVENTS
302 ---------------------------------------------------------------------------
303 */
304
305 qx.Proto._onmouseup = function(e) {
306   this.execute();
307 }
308
309
310
311
312
313 /*
314 ---------------------------------------------------------------------------
315   DISPOSER
316 ---------------------------------------------------------------------------
317 */
318
319 qx.Proto.dispose = function()
320 {
321   if (this.getDisposed()) {
322     return;
323   }
324
325   // Dispose children
326   if (this._iconObject)
327   {
328     this._iconObject.dispose();
329     this._iconObject = null;
330   }
331
332   if (this._labelObject)
333   {
334     this._labelObject.dispose();
335     this._labelObject = null;
336   }
337
338   if (this._shortcutObject)
339   {
340     this._shortcutObject.dispose();
341     this._shortcutObject = null;
342   }
343
344   if (this._arrowObject)
345   {
346     this._arrowObject.dispose();
347     this._arrowObject = null;
348   }
349
350   // Remove event listeners
351   this.removeEventListener("mouseup", this._onmouseup);
352
353   return qx.ui.layout.HorizontalBoxLayout.prototype.dispose.call(this);
354 }