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