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
|
44
|
144 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) {
|
|
166 GtkBorder* border = new GtkBorder ();
|
|
167 GtkBorder* buffer;
|
|
168 OS.gtk_widget_style_get1 (handle, OS.default_border.ptr, cast(int*)&buffer );
|
|
169 if (buffer !is null) {
|
|
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
|
|
182 void createHandle (int index) {
|
|
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
|
44
|
250 if ((style & SWT.ARROW) !is 0) return;
|
37
|
251 _setAlignment (style & (SWT.LEFT | SWT.CENTER | SWT.RIGHT));
|
|
252 }
|
|
253
|
|
254 void createWidget (int index) {
|
|
255 super.createWidget (index);
|
|
256 text = "";
|
|
257 }
|
|
258
|
|
259 void deregister () {
|
|
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
|
44
|
319 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
|
|
413 void hookEvents () {
|
|
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
|
44
|
421 bool isDescribedByLabel () {
|
37
|
422 return false;
|
|
423 }
|
|
424
|
44
|
425 alias Control.mnemonicHit mnemonicHit;
|
|
426 bool mnemonicHit (char key) {
|
|
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;
|
|
434 bool mnemonicMatch (char key) {
|
|
435 if (labelHandle is null) return false;
|
37
|
436 return mnemonicMatch (labelHandle, key);
|
|
437 }
|
|
438
|
|
439 void register () {
|
|
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
|
|
447 void releaseHandle () {
|
|
448 super.releaseHandle ();
|
44
|
449 boxHandle = imageHandle = labelHandle = arrowHandle = null;
|
37
|
450 }
|
|
451
|
|
452 void releaseWidget () {
|
|
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
|
|
487 void resizeHandle (int width, int height) {
|
|
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;
|
|
558 }
|
44
|
559 OS.gtk_arrow_set (cast(GtkArrow*)arrowHandle, arrow_type, OS.GTK_SHADOW_OUT);
|
37
|
560 return;
|
|
561 }
|
44
|
562 if ((alignment & (SWT.LEFT | SWT.RIGHT | SWT.CENTER)) is 0) return;
|
37
|
563 style &= ~(SWT.LEFT | SWT.RIGHT | SWT.CENTER);
|
|
564 style |= alignment & (SWT.LEFT | SWT.RIGHT | SWT.CENTER);
|
|
565 /* Alignment not honoured when image and text are visible */
|
44
|
566 bool bothVisible = OS.GTK_WIDGET_VISIBLE (labelHandle) && OS.GTK_WIDGET_VISIBLE (imageHandle);
|
37
|
567 if (bothVisible) {
|
44
|
568 if ((style & (SWT.RADIO | SWT.CHECK)) !is 0) alignment = SWT.LEFT;
|
|
569 if ((style & (SWT.PUSH | SWT.TOGGLE)) !is 0) alignment = SWT.CENTER;
|
37
|
570 }
|
44
|
571 if ((alignment & SWT.LEFT) !is 0) {
|
37
|
572 if (bothVisible) {
|
44
|
573 OS.gtk_box_set_child_packing (cast(GtkBox*)boxHandle, labelHandle, false, false, 0, OS.GTK_PACK_START);
|
|
574 OS.gtk_box_set_child_packing (cast(GtkBox*)boxHandle, imageHandle, false, false, 0, OS.GTK_PACK_START);
|
37
|
575 }
|
44
|
576 OS.gtk_misc_set_alignment (cast(GtkMisc*)labelHandle, 0.0f, 0.5f);
|
|
577 OS.gtk_label_set_justify (cast(GtkLabel*)labelHandle, OS.GTK_JUSTIFY_LEFT);
|
|
578 OS.gtk_misc_set_alignment (cast(GtkMisc*)imageHandle, 0.0f, 0.5f);
|
37
|
579 return;
|
|
580 }
|
44
|
581 if ((alignment & SWT.CENTER) !is 0) {
|
37
|
582 if (bothVisible) {
|
44
|
583 OS.gtk_box_set_child_packing (cast(GtkBox*)boxHandle, labelHandle, true, true, 0, OS.GTK_PACK_END);
|
|
584 OS.gtk_box_set_child_packing (cast(GtkBox*)boxHandle, imageHandle, true, true, 0, OS.GTK_PACK_START);
|
|
585 OS.gtk_misc_set_alignment (cast(GtkMisc*)labelHandle, 0f, 0.5f);
|
|
586 OS.gtk_misc_set_alignment (cast(GtkMisc*)imageHandle, 1f, 0.5f);
|
37
|
587 } else {
|
44
|
588 OS.gtk_misc_set_alignment (cast(GtkMisc*)labelHandle, 0.5f, 0.5f);
|
|
589 OS.gtk_label_set_justify (cast(GtkLabel*)labelHandle, OS.GTK_JUSTIFY_CENTER);
|
|
590 OS.gtk_misc_set_alignment (cast(GtkMisc*)imageHandle, 0.5f, 0.5f);
|
37
|
591 }
|
|
592 return;
|
|
593 }
|
44
|
594 if ((alignment & SWT.RIGHT) !is 0) {
|
37
|
595 if (bothVisible) {
|
44
|
596 OS.gtk_box_set_child_packing (cast(GtkBox*)boxHandle, labelHandle, false, false, 0, OS.GTK_PACK_END);
|
|
597 OS.gtk_box_set_child_packing (cast(GtkBox*)boxHandle, imageHandle, false, false, 0, OS.GTK_PACK_END);
|
37
|
598 }
|
44
|
599 OS.gtk_misc_set_alignment (cast(GtkMisc*)labelHandle, 1.0f, 0.5f);
|
|
600 OS.gtk_label_set_justify (cast(GtkLabel*)labelHandle, OS.GTK_JUSTIFY_RIGHT);
|
|
601 OS.gtk_misc_set_alignment (cast(GtkMisc*)imageHandle, 1.0f, 0.5f);
|
37
|
602 return;
|
|
603 }
|
|
604 }
|
|
605
|
44
|
606 alias Control.setBackgroundColor setBackgroundColor;
|
|
607 override void setBackgroundColor (GdkColor* color) {
|
37
|
608 super.setBackgroundColor (color);
|
|
609 setBackgroundColor(fixedHandle, color);
|
44
|
610 if (labelHandle !is null) setBackgroundColor(labelHandle, color);
|
|
611 if (imageHandle !is null) setBackgroundColor(imageHandle, color);
|
37
|
612 }
|
|
613
|
44
|
614 void setFontDescription (PangoFontDescription* font) {
|
37
|
615 super.setFontDescription (font);
|
44
|
616 if (labelHandle !is null) OS.gtk_widget_modify_font (labelHandle, font);
|
|
617 if (imageHandle !is null) OS.gtk_widget_modify_font (imageHandle, font);
|
37
|
618 }
|
|
619
|
44
|
620 bool setRadioSelection (bool value) {
|
|
621 if ((style & SWT.RADIO) is 0) return false;
|
|
622 if (getSelection () !is value) {
|
37
|
623 setSelection (value);
|
|
624 postEvent (SWT.Selection);
|
|
625 }
|
|
626 return true;
|
|
627 }
|
|
628
|
44
|
629 alias Control.setForegroundColor setForegroundColor;
|
|
630 override void setForegroundColor (GdkColor* color) {
|
37
|
631 super.setForegroundColor (color);
|
|
632 setForegroundColor (fixedHandle, color);
|
44
|
633 if (labelHandle !is null) setForegroundColor (labelHandle, color);
|
|
634 if (imageHandle !is null) setForegroundColor (imageHandle, color);
|
37
|
635 }
|
|
636
|
|
637 /**
|
|
638 * Sets the receiver's image to the argument, which may be
|
|
639 * <code>null</code> indicating that no image should be displayed.
|
|
640 * <p>
|
|
641 * Note that a Button can display an image and text simultaneously
|
|
642 * on Windows (starting with XP), GTK+ and OSX. On other platforms,
|
|
643 * a Button that has an image and text set into it will display the
|
|
644 * image or text that was set most recently.
|
|
645 * </p>
|
|
646 * @param image the image to display on the receiver (may be <code>null</code>)
|
|
647 *
|
|
648 * @exception IllegalArgumentException <ul>
|
|
649 * <li>ERROR_INVALID_ARGUMENT - if the image has been disposed</li>
|
|
650 * </ul>
|
|
651 * @exception SWTException <ul>
|
|
652 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
653 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
654 * </ul>
|
|
655 */
|
|
656 public void setImage (Image image) {
|
|
657 checkWidget ();
|
44
|
658 if ((style & SWT.ARROW) !is 0) return;
|
|
659 if (imageList !is null) imageList.dispose ();
|
37
|
660 imageList = null;
|
44
|
661 if (image !is null) {
|
37
|
662 if (image.isDisposed()) error (SWT.ERROR_INVALID_ARGUMENT);
|
|
663 imageList = new ImageList ();
|
|
664 int imageIndex = imageList.add (image);
|
44
|
665 auto pixbuf = imageList.getPixbuf (imageIndex);
|
|
666 OS.gtk_image_set_from_pixbuf (cast(GtkImage*)imageHandle, pixbuf);
|
|
667 if (text.length is 0) OS.gtk_widget_hide (labelHandle);
|
37
|
668 OS.gtk_widget_show (imageHandle);
|
|
669 } else {
|
44
|
670 OS.gtk_image_set_from_pixbuf (cast(GtkImage*)imageHandle, null);
|
37
|
671 OS.gtk_widget_show (labelHandle);
|
|
672 OS.gtk_widget_hide (imageHandle);
|
|
673 }
|
|
674 this.image = image;
|
|
675 _setAlignment (style);
|
|
676 }
|
|
677
|
|
678 void setOrientation () {
|
|
679 super.setOrientation ();
|
44
|
680 if ((style & SWT.RIGHT_TO_LEFT) !is 0) {
|
|
681 if (labelHandle !is null) OS.gtk_widget_set_direction (labelHandle, OS.GTK_TEXT_DIR_RTL);
|
|
682 if (imageHandle !is null) OS.gtk_widget_set_direction (imageHandle, OS.GTK_TEXT_DIR_RTL);
|
|
683 if (arrowHandle !is null) {
|
37
|
684 switch (style & (SWT.LEFT | SWT.RIGHT)) {
|
44
|
685 case SWT.LEFT: OS.gtk_arrow_set (cast(GtkArrow*)arrowHandle, OS.GTK_ARROW_RIGHT, OS.GTK_SHADOW_OUT); break;
|
|
686 case SWT.RIGHT: OS.gtk_arrow_set (cast(GtkArrow*)arrowHandle, OS.GTK_ARROW_LEFT, OS.GTK_SHADOW_OUT); break;
|
37
|
687 }
|
|
688 }
|
|
689 }
|
|
690 }
|
|
691
|
|
692 /**
|
|
693 * Sets the selection state of the receiver, if it is of type <code>CHECK</code>,
|
|
694 * <code>RADIO</code>, or <code>TOGGLE</code>.
|
|
695 *
|
|
696 * <p>
|
|
697 * When the receiver is of type <code>CHECK</code> or <code>RADIO</code>,
|
|
698 * it is selected when it is checked. When it is of type <code>TOGGLE</code>,
|
|
699 * it is selected when it is pushed in.
|
|
700 *
|
|
701 * @param selected the new selection state
|
|
702 *
|
|
703 * @exception SWTException <ul>
|
|
704 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
705 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
706 * </ul>
|
|
707 */
|
44
|
708 public void setSelection (bool selected) {
|
37
|
709 checkWidget();
|
44
|
710 if ((style & (SWT.CHECK | SWT.RADIO | SWT.TOGGLE)) is 0) return;
|
|
711 OS.g_signal_handlers_block_matched (handle, OS.G_SIGNAL_MATCH_DATA, 0, 0, null, null, cast(void*)CLICKED);
|
|
712 OS.gtk_toggle_button_set_active (cast(GtkToggleButton*)handle, selected);
|
|
713 if ((style & SWT.RADIO) !is 0) OS.gtk_toggle_button_set_active (cast(GtkToggleButton*)groupHandle, !selected);
|
|
714 OS.g_signal_handlers_unblock_matched (handle, OS.G_SIGNAL_MATCH_DATA, 0, 0, null, null, cast(void*)CLICKED);
|
37
|
715 }
|
|
716
|
|
717 /**
|
|
718 * Sets the receiver's text.
|
|
719 * <p>
|
|
720 * This method sets the button label. The label may include
|
|
721 * the mnemonic character but must not contain line delimiters.
|
|
722 * </p>
|
|
723 * <p>
|
|
724 * Mnemonics are indicated by an '&' that causes the next
|
|
725 * character to be the mnemonic. When the user presses a
|
|
726 * key sequence that matches the mnemonic, a selection
|
|
727 * event occurs. On most platforms, the mnemonic appears
|
|
728 * underlined but may be emphasized in a platform specific
|
|
729 * manner. The mnemonic indicator character '&' can be
|
|
730 * escaped by doubling it in the string, causing a single
|
|
731 * '&' to be displayed.
|
|
732 * </p><p>
|
|
733 * Note that a Button can display an image and text simultaneously
|
|
734 * on Windows (starting with XP), GTK+ and OSX. On other platforms,
|
|
735 * a Button that has an image and text set into it will display the
|
|
736 * image or text that was set most recently.
|
|
737 * </p>
|
|
738 * @param string the new text
|
|
739 *
|
|
740 * @exception IllegalArgumentException <ul>
|
|
741 * <li>ERROR_NULL_ARGUMENT - if the text is null</li>
|
|
742 * </ul>
|
|
743 * @exception SWTException <ul>
|
|
744 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
745 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
746 * </ul>
|
|
747 */
|
44
|
748 public void setText (char[] string) {
|
37
|
749 checkWidget ();
|
44
|
750 if (string is null) error (SWT.ERROR_NULL_ARGUMENT);
|
|
751 if ((style & SWT.ARROW) !is 0) return;
|
37
|
752 text = string;
|
|
753 char [] chars = fixMnemonic (string);
|
44
|
754 OS.gtk_label_set_text_with_mnemonic (cast(GtkLabel*)labelHandle, toStringz(chars));
|
|
755 if (image is null) OS.gtk_widget_hide (imageHandle);
|
37
|
756 OS.gtk_widget_show (labelHandle);
|
|
757 _setAlignment (style);
|
|
758 }
|
|
759
|
|
760 void showWidget () {
|
|
761 super.showWidget ();
|
44
|
762 if (boxHandle !is null) OS.gtk_widget_show (boxHandle);
|
|
763 if (labelHandle !is null) OS.gtk_widget_show (labelHandle);
|
|
764 if (arrowHandle !is null) OS.gtk_widget_show (arrowHandle);
|
37
|
765 }
|
|
766
|
44
|
767 alias Control.traversalCode traversalCode;
|
|
768 override int traversalCode (int key, GdkEventKey* event) {
|
37
|
769 int code = super.traversalCode (key, event);
|
44
|
770 if ((style & SWT.RADIO) !is 0) code |= SWT.TRAVERSE_ARROW_NEXT | SWT.TRAVERSE_ARROW_PREVIOUS;
|
37
|
771 return code;
|
|
772 }
|
|
773
|
|
774 }
|