Mercurial > projects > dwt-linux
annotate dwt/widgets/Button.d @ 58:a5c274fa5af9
extended helloworld crashing
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sat, 12 Jan 2008 07:02:08 +0100 |
parents | 93981635e709 |
children | 8cec8f536af3 |
rev | line source |
---|---|
37 | 1 /******************************************************************************* |
2 * Copyright (c) 2000, 2007 IBM Corporation and others. | |
3 * All rights reserved. This program and the accompanying materials | |
4 * are made available under the terms of the Eclipse Public License v1.0 | |
5 * which accompanies this distribution, and is available at | |
6 * http://www.eclipse.org/legal/epl-v10.html | |
7 * | |
8 * Contributors: | |
9 * IBM Corporation - initial API and implementation | |
10 *******************************************************************************/ | |
11 module dwt.widgets.Button; | |
12 | |
13 import dwt.widgets.Control; | |
14 | |
44 | 15 import dwt.internal.gtk.OS; |
16 import dwt.SWT; | |
17 import dwt.graphics.Point; | |
18 import dwt.graphics.Image; | |
19 import dwt.widgets.ImageList; | |
20 import dwt.widgets.Composite; | |
21 import dwt.events.SelectionListener; | |
22 import dwt.widgets.TypedListener; | |
23 import dwt.widgets.Decorations; | |
37 | 24 |
44 | 25 import tango.stdc.stringz; |
37 | 26 |
27 /** | |
28 * Instances of this class represent a selectable user interface object that | |
29 * issues notification when pressed and released. | |
30 * <dl> | |
31 * <dt><b>Styles:</b></dt> | |
32 * <dd>ARROW, CHECK, PUSH, RADIO, TOGGLE, FLAT</dd> | |
33 * <dd>UP, DOWN, LEFT, RIGHT, CENTER</dd> | |
34 * <dt><b>Events:</b></dt> | |
35 * <dd>Selection</dd> | |
36 * </dl> | |
37 * <p> | |
38 * Note: Only one of the styles ARROW, CHECK, PUSH, RADIO, and TOGGLE | |
39 * may be specified. | |
40 * </p><p> | |
41 * Note: Only one of the styles LEFT, RIGHT, and CENTER may be specified. | |
42 * </p><p> | |
43 * Note: Only one of the styles UP, DOWN, LEFT, and RIGHT may be specified | |
44 * when the ARROW style is specified. | |
45 * </p><p> | |
46 * IMPORTANT: This class is intended to be subclassed <em>only</em> | |
47 * within the SWT implementation. | |
48 * </p> | |
49 */ | |
44 | 50 public class Button : Control { |
51 GtkWidget* boxHandle, labelHandle, imageHandle, arrowHandle, groupHandle; | |
52 bool selected; | |
37 | 53 ImageList imageList; |
54 Image image; | |
44 | 55 char[] text; |
37 | 56 |
57 /** | |
58 * Constructs a new instance of this class given its parent | |
59 * and a style value describing its behavior and appearance. | |
60 * <p> | |
61 * The style value is either one of the style constants defined in | |
62 * class <code>SWT</code> which is applicable to instances of this | |
63 * class, or must be built by <em>bitwise OR</em>'ing together | |
64 * (that is, using the <code>int</code> "|" operator) two or more | |
65 * of those <code>SWT</code> style constants. The class description | |
66 * lists the style constants that are applicable to the class. | |
67 * Style bits are also inherited from superclasses. | |
68 * </p> | |
69 * | |
70 * @param parent a composite control which will be the parent of the new instance (cannot be null) | |
71 * @param style the style of control to construct | |
72 * | |
73 * @exception IllegalArgumentException <ul> | |
74 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li> | |
75 * </ul> | |
76 * @exception SWTException <ul> | |
77 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li> | |
78 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li> | |
79 * </ul> | |
80 * | |
81 * @see SWT#ARROW | |
82 * @see SWT#CHECK | |
83 * @see SWT#PUSH | |
84 * @see SWT#RADIO | |
85 * @see SWT#TOGGLE | |
86 * @see SWT#FLAT | |
87 * @see SWT#LEFT | |
88 * @see SWT#RIGHT | |
89 * @see SWT#CENTER | |
90 * @see Widget#checkSubclass | |
91 * @see Widget#getStyle | |
92 */ | |
44 | 93 public this (Composite parent, int style) { |
37 | 94 super (parent, checkStyle (style)); |
95 } | |
96 | |
97 static int checkStyle (int style) { | |
98 style = checkBits (style, SWT.PUSH, SWT.ARROW, SWT.CHECK, SWT.RADIO, SWT.TOGGLE, 0); | |
44 | 99 if ((style & (SWT.PUSH | SWT.TOGGLE)) !is 0) { |
37 | 100 return checkBits (style, SWT.CENTER, SWT.LEFT, SWT.RIGHT, 0, 0, 0); |
101 } | |
44 | 102 if ((style & (SWT.CHECK | SWT.RADIO)) !is 0) { |
37 | 103 return checkBits (style, SWT.LEFT, SWT.RIGHT, SWT.CENTER, 0, 0, 0); |
104 } | |
44 | 105 if ((style & SWT.ARROW) !is 0) { |
37 | 106 style |= SWT.NO_FOCUS; |
107 return checkBits (style, SWT.UP, SWT.DOWN, SWT.LEFT, SWT.RIGHT, 0, 0); | |
108 } | |
109 return style; | |
110 } | |
111 | |
112 /** | |
113 * Adds the listener to the collection of listeners who will | |
114 * be notified when the control is selected by the user, by sending | |
115 * it one of the messages defined in the <code>SelectionListener</code> | |
116 * interface. | |
117 * <p> | |
118 * <code>widgetSelected</code> is called when the control is selected by the user. | |
119 * <code>widgetDefaultSelected</code> is not called. | |
120 * </p> | |
121 * | |
122 * @param listener the listener which should be notified | |
123 * | |
124 * @exception IllegalArgumentException <ul> | |
125 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li> | |
126 * </ul> | |
127 * @exception SWTException <ul> | |
128 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
129 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
130 * </ul> | |
131 * | |
132 * @see SelectionListener | |
133 * @see #removeSelectionListener | |
134 * @see SelectionEvent | |
135 */ | |
136 public void addSelectionListener (SelectionListener listener) { | |
137 checkWidget (); | |
44 | 138 if (listener is null) error (SWT.ERROR_NULL_ARGUMENT); |
37 | 139 TypedListener typedListener = new TypedListener (listener); |
140 addListener (SWT.Selection,typedListener); | |
141 addListener (SWT.DefaultSelection,typedListener); | |
142 } | |
143 | |
58
a5c274fa5af9
extended helloworld crashing
Frank Benoit <benoit@tionex.de>
parents:
55
diff
changeset
|
144 override public Point computeSize (int wHint, int hHint, bool changed) { |
37 | 145 checkWidget (); |
44 | 146 if (wHint !is SWT.DEFAULT && wHint < 0) wHint = 0; |
147 if (hHint !is SWT.DEFAULT && hHint < 0) hHint = 0; | |
37 | 148 /* |
149 * Feature in GTK, GtkCheckButton and GtkRadioButton allocate | |
150 * only the minimum size necessary for its child. This causes the child | |
151 * alignment to fail. The fix is to set the child size to the size | |
152 * of the button. | |
153 */ | |
154 forceResize (); | |
44 | 155 int reqWidth = -1, reqHeight = -1; |
156 if ((style & (SWT.CHECK | SWT.RADIO)) !is 0) { | |
157 OS.gtk_widget_get_size_request (boxHandle, &reqWidth, &reqHeight); | |
37 | 158 OS.gtk_widget_set_size_request (boxHandle, -1, -1); |
159 } | |
160 Point size = computeNativeSize (handle, wHint, hHint, changed); | |
44 | 161 if ((style & (SWT.CHECK | SWT.RADIO)) !is 0) { |
162 OS.gtk_widget_set_size_request (boxHandle, reqWidth, reqHeight); | |
37 | 163 } |
44 | 164 if (wHint !is SWT.DEFAULT || hHint !is SWT.DEFAULT) { |
165 if ((OS.GTK_WIDGET_FLAGS (handle) & OS.GTK_CAN_DEFAULT) !is 0) { | |
58
a5c274fa5af9
extended helloworld crashing
Frank Benoit <benoit@tionex.de>
parents:
55
diff
changeset
|
166 GtkBorder border; |
44 | 167 GtkBorder* buffer; |
168 OS.gtk_widget_style_get1 (handle, OS.default_border.ptr, cast(int*)&buffer ); | |
169 if (buffer !is null) { | |
58
a5c274fa5af9
extended helloworld crashing
Frank Benoit <benoit@tionex.de>
parents:
55
diff
changeset
|
170 border = *buffer; |
37 | 171 } else { |
172 /* Use the GTK+ default value of 1 for each. */ | |
173 border.left = border.right = border.top = border.bottom = 1; | |
174 } | |
44 | 175 if (wHint !is SWT.DEFAULT) size.x += border.left + border.right; |
176 if (hHint !is SWT.DEFAULT) size.y += border.top + border.bottom; | |
37 | 177 } |
178 } | |
179 return size; | |
180 } | |
181 | |
58
a5c274fa5af9
extended helloworld crashing
Frank Benoit <benoit@tionex.de>
parents:
55
diff
changeset
|
182 override void createHandle (int index) { |
37 | 183 state |= HANDLE; |
44 | 184 if ((style & SWT.PUSH) is 0) state |= THEME_BACKGROUND; |
37 | 185 int bits = SWT.ARROW | SWT.TOGGLE | SWT.CHECK | SWT.RADIO | SWT.PUSH; |
44 | 186 fixedHandle = cast(GtkWidget*)OS.g_object_new (display.gtk_fixed_get_type (), null); |
187 if (fixedHandle is null) error (SWT.ERROR_NO_HANDLES); | |
188 OS.gtk_fixed_set_has_window (cast(GtkFixed*)fixedHandle, true); | |
37 | 189 switch (style & bits) { |
190 case SWT.ARROW: | |
191 int arrow_type = OS.GTK_ARROW_UP; | |
44 | 192 if ((style & SWT.UP) !is 0) arrow_type = OS.GTK_ARROW_UP; |
193 if ((style & SWT.DOWN) !is 0) arrow_type = OS.GTK_ARROW_DOWN; | |
194 if ((style & SWT.LEFT) !is 0) arrow_type = OS.GTK_ARROW_LEFT; | |
195 if ((style & SWT.RIGHT) !is 0) arrow_type = OS.GTK_ARROW_RIGHT; | |
37 | 196 handle = OS.gtk_button_new (); |
44 | 197 if (handle is null) error (SWT.ERROR_NO_HANDLES); |
37 | 198 arrowHandle = OS.gtk_arrow_new (arrow_type, OS.GTK_SHADOW_OUT); |
44 | 199 if (arrowHandle is null) error (SWT.ERROR_NO_HANDLES); |
37 | 200 break; |
201 case SWT.TOGGLE: | |
202 handle = OS.gtk_toggle_button_new (); | |
44 | 203 if (handle is null) error (SWT.ERROR_NO_HANDLES); |
37 | 204 break; |
205 case SWT.CHECK: | |
206 handle = OS.gtk_check_button_new (); | |
44 | 207 if (handle is null) error (SWT.ERROR_NO_HANDLES); |
37 | 208 break; |
209 case SWT.RADIO: | |
210 /* | |
211 * Feature in GTK. In GTK, radio button must always be part of | |
212 * a radio button group. In a GTK radio group, one button is always | |
213 * selected. This means that it is not possible to have a single | |
214 * radio button that is unselected. This is necessary to allow | |
215 * applications to implement their own radio behavior or use radio | |
216 * buttons outside of radio groups. The fix is to create a hidden | |
217 * radio button for each radio button we create and add them | |
218 * to the same group. This allows the visible button to be | |
219 * unselected. | |
220 */ | |
44 | 221 groupHandle = cast(GtkWidget*)OS.gtk_radio_button_new (null); |
222 if (groupHandle is null) error (SWT.ERROR_NO_HANDLES); | |
37 | 223 OS.g_object_ref (groupHandle); |
44 | 224 OS.gtk_object_sink (cast(GtkObject*)groupHandle); |
225 handle = OS.gtk_radio_button_new ( OS.gtk_radio_button_get_group (cast(GtkRadioButton*)groupHandle)); | |
226 if (handle is null) error (SWT.ERROR_NO_HANDLES); | |
37 | 227 break; |
228 case SWT.PUSH: | |
229 default: | |
230 handle = OS.gtk_button_new (); | |
44 | 231 if (handle is null) error (SWT.ERROR_NO_HANDLES); |
37 | 232 OS.GTK_WIDGET_SET_FLAGS(handle, OS.GTK_CAN_DEFAULT); |
233 break; | |
234 } | |
44 | 235 if ((style & SWT.ARROW) !is 0) { |
236 OS.gtk_container_add (cast(GtkContainer*)handle, arrowHandle); | |
37 | 237 } else { |
238 boxHandle = OS.gtk_hbox_new (false, 4); | |
44 | 239 if (boxHandle is null) error (SWT.ERROR_NO_HANDLES); |
37 | 240 labelHandle = OS.gtk_label_new_with_mnemonic (null); |
44 | 241 if (labelHandle is null) error (SWT.ERROR_NO_HANDLES); |
37 | 242 imageHandle = OS.gtk_image_new (); |
44 | 243 if (imageHandle is null) error (SWT.ERROR_NO_HANDLES); |
244 OS.gtk_container_add (cast(GtkContainer*)handle, boxHandle); | |
245 OS.gtk_container_add (cast(GtkContainer*)boxHandle, imageHandle); | |
246 OS.gtk_container_add (cast(GtkContainer*)boxHandle, labelHandle); | |
37 | 247 } |
44 | 248 OS.gtk_container_add (cast(GtkContainer*)fixedHandle, handle); |
37 | 249 |
58
a5c274fa5af9
extended helloworld crashing
Frank Benoit <benoit@tionex.de>
parents:
55
diff
changeset
|
250 if ((style & SWT.ARROW) !is 0) return; |
37 | 251 _setAlignment (style & (SWT.LEFT | SWT.CENTER | SWT.RIGHT)); |
252 } | |
253 | |
58
a5c274fa5af9
extended helloworld crashing
Frank Benoit <benoit@tionex.de>
parents:
55
diff
changeset
|
254 override void createWidget (int index) { |
37 | 255 super.createWidget (index); |
256 text = ""; | |
257 } | |
258 | |
58
a5c274fa5af9
extended helloworld crashing
Frank Benoit <benoit@tionex.de>
parents:
55
diff
changeset
|
259 override void deregister () { |
37 | 260 super.deregister (); |
44 | 261 if (boxHandle !is null) display.removeWidget (boxHandle); |
262 if (labelHandle !is null) display.removeWidget (labelHandle); | |
263 if (imageHandle !is null) display.removeWidget (imageHandle); | |
264 if (arrowHandle !is null) display.removeWidget (arrowHandle); | |
37 | 265 } |
266 | |
44 | 267 override GtkWidget* fontHandle () { |
268 if (labelHandle !is null) return labelHandle; | |
37 | 269 return super.fontHandle (); |
270 } | |
271 | |
272 /** | |
273 * Returns a value which describes the position of the | |
274 * text or image in the receiver. The value will be one of | |
275 * <code>LEFT</code>, <code>RIGHT</code> or <code>CENTER</code> | |
276 * unless the receiver is an <code>ARROW</code> button, in | |
277 * which case, the alignment will indicate the direction of | |
278 * the arrow (one of <code>LEFT</code>, <code>RIGHT</code>, | |
279 * <code>UP</code> or <code>DOWN</code>). | |
280 * | |
281 * @return the alignment | |
282 * | |
283 * @exception SWTException <ul> | |
284 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
285 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
286 * </ul> | |
287 */ | |
288 public int getAlignment () { | |
289 checkWidget (); | |
44 | 290 if ((style & SWT.ARROW) !is 0) { |
291 if ((style & SWT.UP) !is 0) return SWT.UP; | |
292 if ((style & SWT.DOWN) !is 0) return SWT.DOWN; | |
293 if ((style & SWT.LEFT) !is 0) return SWT.LEFT; | |
294 if ((style & SWT.RIGHT) !is 0) return SWT.RIGHT; | |
37 | 295 return SWT.UP; |
296 } | |
44 | 297 if ((style & SWT.LEFT) !is 0) return SWT.LEFT; |
298 if ((style & SWT.CENTER) !is 0) return SWT.CENTER; | |
299 if ((style & SWT.RIGHT) !is 0) return SWT.RIGHT; | |
37 | 300 return SWT.LEFT; |
301 } | |
302 | |
303 /** | |
304 * Returns the receiver's image if it has one, or null | |
305 * if it does not. | |
306 * | |
307 * @return the receiver's image | |
308 * | |
309 * @exception SWTException <ul> | |
310 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
311 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
312 * </ul> | |
313 */ | |
314 public Image getImage () { | |
315 checkWidget (); | |
316 return image; | |
317 } | |
318 | |
58
a5c274fa5af9
extended helloworld crashing
Frank Benoit <benoit@tionex.de>
parents:
55
diff
changeset
|
319 override char[] getNameText () { |
37 | 320 return getText (); |
321 } | |
322 | |
323 /** | |
324 * Returns <code>true</code> if the receiver is selected, | |
325 * and false otherwise. | |
326 * <p> | |
327 * When the receiver is of type <code>CHECK</code> or <code>RADIO</code>, | |
328 * it is selected when it is checked. When it is of type <code>TOGGLE</code>, | |
329 * it is selected when it is pushed in. If the receiver is of any other type, | |
330 * this method returns false. | |
331 * | |
332 * @return the selection state | |
333 * | |
334 * @exception SWTException <ul> | |
335 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
336 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
337 * </ul> | |
338 */ | |
44 | 339 public bool getSelection () { |
37 | 340 checkWidget (); |
44 | 341 if ((style & (SWT.CHECK | SWT.RADIO | SWT.TOGGLE)) is 0) return false; |
342 return cast(bool)OS.gtk_toggle_button_get_active (cast(GtkToggleButton*)handle); | |
37 | 343 } |
344 | |
345 /** | |
346 * Returns the receiver's text, which will be an empty | |
347 * string if it has never been set or if the receiver is | |
348 * an <code>ARROW</code> button. | |
349 * | |
350 * @return the receiver's text | |
351 * | |
352 * @exception SWTException <ul> | |
353 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
354 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
355 * </ul> | |
356 */ | |
44 | 357 public char[] getText () { |
37 | 358 checkWidget(); |
44 | 359 if ((style & SWT.ARROW) !is 0) return ""; |
37 | 360 return text; |
361 } | |
362 | |
44 | 363 override int /*long*/ gtk_button_press_event (GtkWidget* widget, GdkEventButton* event) { |
364 auto result = super.gtk_button_press_event (widget, event); | |
365 if (result !is 0) return result; | |
366 if ((style & SWT.RADIO) !is 0) selected = getSelection (); | |
37 | 367 return result; |
368 } | |
369 | |
44 | 370 override int /*long*/ gtk_clicked (GtkWidget* widget) { |
371 if ((style & SWT.RADIO) !is 0) { | |
372 if ((parent.getStyle () & SWT.NO_RADIO_GROUP) !is 0) { | |
37 | 373 setSelection (!selected); |
374 } else { | |
375 selectRadio (); | |
376 } | |
377 } | |
378 postEvent (SWT.Selection); | |
379 return 0; | |
380 } | |
381 | |
44 | 382 override int /*long*/ gtk_focus_in_event (GtkWidget* widget, GdkEventFocus* event) { |
383 auto result = super.gtk_focus_in_event (widget, event); | |
37 | 384 // widget could be disposed at this point |
44 | 385 if (handle is null) return 0; |
386 if ((style & SWT.PUSH) !is 0 && OS.GTK_WIDGET_HAS_DEFAULT (handle)) { | |
37 | 387 Decorations menuShell = menuShell (); |
388 menuShell.defaultButton = this; | |
389 } | |
390 return result; | |
391 } | |
392 | |
44 | 393 override int /*long*/ gtk_focus_out_event (GtkWidget* widget, GdkEventFocus* event) { |
394 auto result = super.gtk_focus_out_event (widget, event); | |
37 | 395 // widget could be disposed at this point |
44 | 396 if (handle is null) return 0; |
397 if ((style & SWT.PUSH) !is 0 && !OS.GTK_WIDGET_HAS_DEFAULT (handle)) { | |
37 | 398 Decorations menuShell = menuShell (); |
44 | 399 if (menuShell.defaultButton is this) { |
37 | 400 menuShell.defaultButton = null; |
401 } | |
402 } | |
403 return result; | |
404 } | |
405 | |
44 | 406 override int /*long*/ gtk_key_press_event (GtkWidget* widget, GdkEventKey* event) { |
407 auto result = super.gtk_key_press_event (widget, event); | |
408 if (result !is 0) return result; | |
409 if ((style & SWT.RADIO) !is 0) selected = getSelection (); | |
37 | 410 return result; |
411 } | |
412 | |
58
a5c274fa5af9
extended helloworld crashing
Frank Benoit <benoit@tionex.de>
parents:
55
diff
changeset
|
413 override void hookEvents () { |
37 | 414 super.hookEvents(); |
44 | 415 OS.g_signal_connect_closure (handle, OS.clicked.ptr, display.closures [CLICKED], false); |
416 if (labelHandle !is null) { | |
417 OS.g_signal_connect_closure_by_id (cast(void*)labelHandle, display.signalIds [MNEMONIC_ACTIVATE], 0, display.closures [MNEMONIC_ACTIVATE], false); | |
37 | 418 } |
419 } | |
420 | |
58
a5c274fa5af9
extended helloworld crashing
Frank Benoit <benoit@tionex.de>
parents:
55
diff
changeset
|
421 override bool isDescribedByLabel () { |
37 | 422 return false; |
423 } | |
424 | |
44 | 425 alias Control.mnemonicHit mnemonicHit; |
58
a5c274fa5af9
extended helloworld crashing
Frank Benoit <benoit@tionex.de>
parents:
55
diff
changeset
|
426 override bool mnemonicHit (char key) { |
44 | 427 if (labelHandle is null) return false; |
428 bool result = super.mnemonicHit (labelHandle, key); | |
37 | 429 if (result) setFocus (); |
430 return result; | |
431 } | |
432 | |
44 | 433 alias Control.mnemonicMatch mnemonicMatch; |
58
a5c274fa5af9
extended helloworld crashing
Frank Benoit <benoit@tionex.de>
parents:
55
diff
changeset
|
434 override bool mnemonicMatch (char key) { |
44 | 435 if (labelHandle is null) return false; |
37 | 436 return mnemonicMatch (labelHandle, key); |
437 } | |
438 | |
58
a5c274fa5af9
extended helloworld crashing
Frank Benoit <benoit@tionex.de>
parents:
55
diff
changeset
|
439 override void register () { |
37 | 440 super.register (); |
44 | 441 if (boxHandle !is null) display.addWidget (boxHandle, this); |
442 if (labelHandle !is null) display.addWidget (labelHandle, this); | |
443 if (imageHandle !is null) display.addWidget (imageHandle, this); | |
444 if (arrowHandle !is null) display.addWidget (arrowHandle, this); | |
37 | 445 } |
446 | |
58
a5c274fa5af9
extended helloworld crashing
Frank Benoit <benoit@tionex.de>
parents:
55
diff
changeset
|
447 override void releaseHandle () { |
37 | 448 super.releaseHandle (); |
44 | 449 boxHandle = imageHandle = labelHandle = arrowHandle = null; |
37 | 450 } |
451 | |
58
a5c274fa5af9
extended helloworld crashing
Frank Benoit <benoit@tionex.de>
parents:
55
diff
changeset
|
452 override void releaseWidget () { |
37 | 453 super.releaseWidget (); |
44 | 454 if (groupHandle !is null) OS.g_object_unref (groupHandle); |
455 groupHandle = null; | |
456 if (imageList !is null) imageList.dispose (); | |
37 | 457 imageList = null; |
458 image = null; | |
459 text = null; | |
460 } | |
461 | |
462 /** | |
463 * Removes the listener from the collection of listeners who will | |
464 * be notified when the control is selected by the user. | |
465 * | |
466 * @param listener the listener which should no longer be notified | |
467 * | |
468 * @exception IllegalArgumentException <ul> | |
469 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li> | |
470 * </ul> | |
471 * @exception SWTException <ul> | |
472 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
473 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
474 * </ul> | |
475 * | |
476 * @see SelectionListener | |
477 * @see #addSelectionListener | |
478 */ | |
479 public void removeSelectionListener (SelectionListener listener) { | |
480 checkWidget(); | |
44 | 481 if (listener is null) error (SWT.ERROR_NULL_ARGUMENT); |
482 if (eventTable is null) return; | |
37 | 483 eventTable.unhook (SWT.Selection, listener); |
484 eventTable.unhook (SWT.DefaultSelection,listener); | |
485 } | |
486 | |
58
a5c274fa5af9
extended helloworld crashing
Frank Benoit <benoit@tionex.de>
parents:
55
diff
changeset
|
487 override void resizeHandle (int width, int height) { |
37 | 488 super.resizeHandle (width, height); |
489 /* | |
490 * Feature in GTK, GtkCheckButton and GtkRadioButton allocate | |
491 * only the minimum size necessary for its child. This causes the child | |
492 * alignment to fail. The fix is to set the child size to the size | |
493 * of the button. | |
494 */ | |
44 | 495 if ((style & (SWT.CHECK | SWT.RADIO)) !is 0) { |
37 | 496 OS.gtk_widget_set_size_request (boxHandle, width, -1); |
497 } | |
498 } | |
499 | |
500 void selectRadio () { | |
501 /* | |
502 * This code is intentionally commented. When two groups | |
503 * of radio buttons with the same parent are separated by | |
504 * another control, the correct behavior should be that | |
505 * the two groups act independently. This is consistent | |
506 * with radio tool and menu items. The commented code | |
507 * implements this behavior. | |
508 */ | |
509 // int index = 0; | |
510 // Control [] children = parent._getChildren (); | |
44 | 511 // while (index < children.length && children [index] !is this) index++; |
37 | 512 // int i = index - 1; |
513 // while (i >= 0 && children [i].setRadioSelection (false)) --i; | |
514 // int j = index + 1; | |
515 // while (j < children.length && children [j].setRadioSelection (false)) j++; | |
516 // setSelection (true); | |
517 Control [] children = parent._getChildren (); | |
518 for (int i=0; i<children.length; i++) { | |
519 Control child = children [i]; | |
44 | 520 if (this !is child) child.setRadioSelection (false); |
37 | 521 } |
522 setSelection (true); | |
523 } | |
524 | |
525 /** | |
526 * Controls how text, images and arrows will be displayed | |
527 * in the receiver. The argument should be one of | |
528 * <code>LEFT</code>, <code>RIGHT</code> or <code>CENTER</code> | |
529 * unless the receiver is an <code>ARROW</code> button, in | |
530 * which case, the argument indicates the direction of | |
531 * the arrow (one of <code>LEFT</code>, <code>RIGHT</code>, | |
532 * <code>UP</code> or <code>DOWN</code>). | |
533 * | |
534 * @param alignment the new alignment | |
535 * | |
536 * @exception SWTException <ul> | |
537 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
538 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
539 * </ul> | |
540 */ | |
541 public void setAlignment (int alignment) { | |
542 checkWidget (); | |
543 _setAlignment (alignment); | |
544 } | |
545 | |
546 void _setAlignment (int alignment) { | |
44 | 547 if ((style & SWT.ARROW) !is 0) { |
548 if ((style & (SWT.UP | SWT.DOWN | SWT.LEFT | SWT.RIGHT)) is 0) return; | |
37 | 549 style &= ~(SWT.UP | SWT.DOWN | SWT.LEFT | SWT.RIGHT); |
550 style |= alignment & (SWT.UP | SWT.DOWN | SWT.LEFT | SWT.RIGHT); | |
551 int arrow_type = OS.GTK_ARROW_UP; | |
44 | 552 bool isRTL = (style & SWT.RIGHT_TO_LEFT) !is 0; |
37 | 553 switch (alignment) { |
554 case SWT.UP: arrow_type = OS.GTK_ARROW_UP; break; | |
555 case SWT.DOWN: arrow_type = OS.GTK_ARROW_DOWN; break; | |
556 case SWT.LEFT: arrow_type = isRTL ? OS.GTK_ARROW_RIGHT : OS.GTK_ARROW_LEFT; break; | |
557 case SWT.RIGHT: arrow_type = isRTL ? OS.GTK_ARROW_LEFT : OS.GTK_ARROW_RIGHT; break; | |
55
93981635e709
Checked and inserted missing default switch cases.
Frank Benoit <benoit@tionex.de>
parents:
44
diff
changeset
|
558 default: |
37 | 559 } |
44 | 560 OS.gtk_arrow_set (cast(GtkArrow*)arrowHandle, arrow_type, OS.GTK_SHADOW_OUT); |
37 | 561 return; |
562 } | |
44 | 563 if ((alignment & (SWT.LEFT | SWT.RIGHT | SWT.CENTER)) is 0) return; |
37 | 564 style &= ~(SWT.LEFT | SWT.RIGHT | SWT.CENTER); |
565 style |= alignment & (SWT.LEFT | SWT.RIGHT | SWT.CENTER); | |
566 /* Alignment not honoured when image and text are visible */ | |
44 | 567 bool bothVisible = OS.GTK_WIDGET_VISIBLE (labelHandle) && OS.GTK_WIDGET_VISIBLE (imageHandle); |
37 | 568 if (bothVisible) { |
44 | 569 if ((style & (SWT.RADIO | SWT.CHECK)) !is 0) alignment = SWT.LEFT; |
570 if ((style & (SWT.PUSH | SWT.TOGGLE)) !is 0) alignment = SWT.CENTER; | |
37 | 571 } |
44 | 572 if ((alignment & SWT.LEFT) !is 0) { |
37 | 573 if (bothVisible) { |
44 | 574 OS.gtk_box_set_child_packing (cast(GtkBox*)boxHandle, labelHandle, false, false, 0, OS.GTK_PACK_START); |
575 OS.gtk_box_set_child_packing (cast(GtkBox*)boxHandle, imageHandle, false, false, 0, OS.GTK_PACK_START); | |
37 | 576 } |
44 | 577 OS.gtk_misc_set_alignment (cast(GtkMisc*)labelHandle, 0.0f, 0.5f); |
578 OS.gtk_label_set_justify (cast(GtkLabel*)labelHandle, OS.GTK_JUSTIFY_LEFT); | |
579 OS.gtk_misc_set_alignment (cast(GtkMisc*)imageHandle, 0.0f, 0.5f); | |
37 | 580 return; |
581 } | |
44 | 582 if ((alignment & SWT.CENTER) !is 0) { |
37 | 583 if (bothVisible) { |
44 | 584 OS.gtk_box_set_child_packing (cast(GtkBox*)boxHandle, labelHandle, true, true, 0, OS.GTK_PACK_END); |
585 OS.gtk_box_set_child_packing (cast(GtkBox*)boxHandle, imageHandle, true, true, 0, OS.GTK_PACK_START); | |
586 OS.gtk_misc_set_alignment (cast(GtkMisc*)labelHandle, 0f, 0.5f); | |
587 OS.gtk_misc_set_alignment (cast(GtkMisc*)imageHandle, 1f, 0.5f); | |
37 | 588 } else { |
44 | 589 OS.gtk_misc_set_alignment (cast(GtkMisc*)labelHandle, 0.5f, 0.5f); |
590 OS.gtk_label_set_justify (cast(GtkLabel*)labelHandle, OS.GTK_JUSTIFY_CENTER); | |
591 OS.gtk_misc_set_alignment (cast(GtkMisc*)imageHandle, 0.5f, 0.5f); | |
37 | 592 } |
593 return; | |
594 } | |
44 | 595 if ((alignment & SWT.RIGHT) !is 0) { |
37 | 596 if (bothVisible) { |
44 | 597 OS.gtk_box_set_child_packing (cast(GtkBox*)boxHandle, labelHandle, false, false, 0, OS.GTK_PACK_END); |
598 OS.gtk_box_set_child_packing (cast(GtkBox*)boxHandle, imageHandle, false, false, 0, OS.GTK_PACK_END); | |
37 | 599 } |
44 | 600 OS.gtk_misc_set_alignment (cast(GtkMisc*)labelHandle, 1.0f, 0.5f); |
601 OS.gtk_label_set_justify (cast(GtkLabel*)labelHandle, OS.GTK_JUSTIFY_RIGHT); | |
602 OS.gtk_misc_set_alignment (cast(GtkMisc*)imageHandle, 1.0f, 0.5f); | |
37 | 603 return; |
604 } | |
605 } | |
606 | |
44 | 607 alias Control.setBackgroundColor setBackgroundColor; |
608 override void setBackgroundColor (GdkColor* color) { | |
37 | 609 super.setBackgroundColor (color); |
610 setBackgroundColor(fixedHandle, color); | |
44 | 611 if (labelHandle !is null) setBackgroundColor(labelHandle, color); |
612 if (imageHandle !is null) setBackgroundColor(imageHandle, color); | |
37 | 613 } |
614 | |
58
a5c274fa5af9
extended helloworld crashing
Frank Benoit <benoit@tionex.de>
parents:
55
diff
changeset
|
615 override void setFontDescription (PangoFontDescription* font) { |
37 | 616 super.setFontDescription (font); |
44 | 617 if (labelHandle !is null) OS.gtk_widget_modify_font (labelHandle, font); |
618 if (imageHandle !is null) OS.gtk_widget_modify_font (imageHandle, font); | |
37 | 619 } |
620 | |
58
a5c274fa5af9
extended helloworld crashing
Frank Benoit <benoit@tionex.de>
parents:
55
diff
changeset
|
621 override bool setRadioSelection (bool value) { |
44 | 622 if ((style & SWT.RADIO) is 0) return false; |
623 if (getSelection () !is value) { | |
37 | 624 setSelection (value); |
625 postEvent (SWT.Selection); | |
626 } | |
627 return true; | |
628 } | |
629 | |
44 | 630 alias Control.setForegroundColor setForegroundColor; |
631 override void setForegroundColor (GdkColor* color) { | |
37 | 632 super.setForegroundColor (color); |
633 setForegroundColor (fixedHandle, color); | |
44 | 634 if (labelHandle !is null) setForegroundColor (labelHandle, color); |
635 if (imageHandle !is null) setForegroundColor (imageHandle, color); | |
37 | 636 } |
637 | |
638 /** | |
639 * Sets the receiver's image to the argument, which may be | |
640 * <code>null</code> indicating that no image should be displayed. | |
641 * <p> | |
642 * Note that a Button can display an image and text simultaneously | |
643 * on Windows (starting with XP), GTK+ and OSX. On other platforms, | |
644 * a Button that has an image and text set into it will display the | |
645 * image or text that was set most recently. | |
646 * </p> | |
647 * @param image the image to display on the receiver (may be <code>null</code>) | |
648 * | |
649 * @exception IllegalArgumentException <ul> | |
650 * <li>ERROR_INVALID_ARGUMENT - if the image has been disposed</li> | |
651 * </ul> | |
652 * @exception SWTException <ul> | |
653 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
654 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
655 * </ul> | |
656 */ | |
657 public void setImage (Image image) { | |
658 checkWidget (); | |
44 | 659 if ((style & SWT.ARROW) !is 0) return; |
660 if (imageList !is null) imageList.dispose (); | |
37 | 661 imageList = null; |
44 | 662 if (image !is null) { |
37 | 663 if (image.isDisposed()) error (SWT.ERROR_INVALID_ARGUMENT); |
664 imageList = new ImageList (); | |
665 int imageIndex = imageList.add (image); | |
44 | 666 auto pixbuf = imageList.getPixbuf (imageIndex); |
667 OS.gtk_image_set_from_pixbuf (cast(GtkImage*)imageHandle, pixbuf); | |
668 if (text.length is 0) OS.gtk_widget_hide (labelHandle); | |
37 | 669 OS.gtk_widget_show (imageHandle); |
670 } else { | |
44 | 671 OS.gtk_image_set_from_pixbuf (cast(GtkImage*)imageHandle, null); |
37 | 672 OS.gtk_widget_show (labelHandle); |
673 OS.gtk_widget_hide (imageHandle); | |
674 } | |
675 this.image = image; | |
676 _setAlignment (style); | |
677 } | |
678 | |
58
a5c274fa5af9
extended helloworld crashing
Frank Benoit <benoit@tionex.de>
parents:
55
diff
changeset
|
679 override void setOrientation () { |
37 | 680 super.setOrientation (); |
44 | 681 if ((style & SWT.RIGHT_TO_LEFT) !is 0) { |
682 if (labelHandle !is null) OS.gtk_widget_set_direction (labelHandle, OS.GTK_TEXT_DIR_RTL); | |
683 if (imageHandle !is null) OS.gtk_widget_set_direction (imageHandle, OS.GTK_TEXT_DIR_RTL); | |
684 if (arrowHandle !is null) { | |
37 | 685 switch (style & (SWT.LEFT | SWT.RIGHT)) { |
44 | 686 case SWT.LEFT: OS.gtk_arrow_set (cast(GtkArrow*)arrowHandle, OS.GTK_ARROW_RIGHT, OS.GTK_SHADOW_OUT); break; |
687 case SWT.RIGHT: OS.gtk_arrow_set (cast(GtkArrow*)arrowHandle, OS.GTK_ARROW_LEFT, OS.GTK_SHADOW_OUT); break; | |
55
93981635e709
Checked and inserted missing default switch cases.
Frank Benoit <benoit@tionex.de>
parents:
44
diff
changeset
|
688 default: |
37 | 689 } |
690 } | |
691 } | |
692 } | |
693 | |
694 /** | |
695 * Sets the selection state of the receiver, if it is of type <code>CHECK</code>, | |
696 * <code>RADIO</code>, or <code>TOGGLE</code>. | |
697 * | |
698 * <p> | |
699 * When the receiver is of type <code>CHECK</code> or <code>RADIO</code>, | |
700 * it is selected when it is checked. When it is of type <code>TOGGLE</code>, | |
701 * it is selected when it is pushed in. | |
702 * | |
703 * @param selected the new selection state | |
704 * | |
705 * @exception SWTException <ul> | |
706 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
707 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
708 * </ul> | |
709 */ | |
44 | 710 public void setSelection (bool selected) { |
37 | 711 checkWidget(); |
44 | 712 if ((style & (SWT.CHECK | SWT.RADIO | SWT.TOGGLE)) is 0) return; |
713 OS.g_signal_handlers_block_matched (handle, OS.G_SIGNAL_MATCH_DATA, 0, 0, null, null, cast(void*)CLICKED); | |
714 OS.gtk_toggle_button_set_active (cast(GtkToggleButton*)handle, selected); | |
715 if ((style & SWT.RADIO) !is 0) OS.gtk_toggle_button_set_active (cast(GtkToggleButton*)groupHandle, !selected); | |
716 OS.g_signal_handlers_unblock_matched (handle, OS.G_SIGNAL_MATCH_DATA, 0, 0, null, null, cast(void*)CLICKED); | |
37 | 717 } |
718 | |
719 /** | |
720 * Sets the receiver's text. | |
721 * <p> | |
722 * This method sets the button label. The label may include | |
723 * the mnemonic character but must not contain line delimiters. | |
724 * </p> | |
725 * <p> | |
726 * Mnemonics are indicated by an '&' that causes the next | |
727 * character to be the mnemonic. When the user presses a | |
728 * key sequence that matches the mnemonic, a selection | |
729 * event occurs. On most platforms, the mnemonic appears | |
730 * underlined but may be emphasized in a platform specific | |
731 * manner. The mnemonic indicator character '&' can be | |
732 * escaped by doubling it in the string, causing a single | |
733 * '&' to be displayed. | |
734 * </p><p> | |
735 * Note that a Button can display an image and text simultaneously | |
736 * on Windows (starting with XP), GTK+ and OSX. On other platforms, | |
737 * a Button that has an image and text set into it will display the | |
738 * image or text that was set most recently. | |
739 * </p> | |
740 * @param string the new text | |
741 * | |
742 * @exception IllegalArgumentException <ul> | |
743 * <li>ERROR_NULL_ARGUMENT - if the text is null</li> | |
744 * </ul> | |
745 * @exception SWTException <ul> | |
746 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
747 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
748 * </ul> | |
749 */ | |
44 | 750 public void setText (char[] string) { |
37 | 751 checkWidget (); |
44 | 752 if (string is null) error (SWT.ERROR_NULL_ARGUMENT); |
753 if ((style & SWT.ARROW) !is 0) return; | |
37 | 754 text = string; |
755 char [] chars = fixMnemonic (string); | |
44 | 756 OS.gtk_label_set_text_with_mnemonic (cast(GtkLabel*)labelHandle, toStringz(chars)); |
757 if (image is null) OS.gtk_widget_hide (imageHandle); | |
37 | 758 OS.gtk_widget_show (labelHandle); |
759 _setAlignment (style); | |
760 } | |
761 | |
58
a5c274fa5af9
extended helloworld crashing
Frank Benoit <benoit@tionex.de>
parents:
55
diff
changeset
|
762 override void showWidget () { |
37 | 763 super.showWidget (); |
44 | 764 if (boxHandle !is null) OS.gtk_widget_show (boxHandle); |
765 if (labelHandle !is null) OS.gtk_widget_show (labelHandle); | |
766 if (arrowHandle !is null) OS.gtk_widget_show (arrowHandle); | |
37 | 767 } |
768 | |
44 | 769 alias Control.traversalCode traversalCode; |
770 override int traversalCode (int key, GdkEventKey* event) { | |
37 | 771 int code = super.traversalCode (key, event); |
44 | 772 if ((style & SWT.RADIO) !is 0) code |= SWT.TRAVERSE_ARROW_NEXT | SWT.TRAVERSE_ARROW_PREVIOUS; |
37 | 773 return code; |
774 } | |
775 | |
776 } |