1 /* 2 * This file is part of gtkD. 3 * 4 * gtkD is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU Lesser General Public License 6 * as published by the Free Software Foundation; either version 3 7 * of the License, or (at your option) any later version, with 8 * some exceptions, please read the COPYING file. 9 * 10 * gtkD is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public License 16 * along with gtkD; if not, write to the Free Software 17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA 18 */ 19 20 // generated automatically - do not change 21 // find conversion definition on APILookup.txt 22 // implement new conversion functionalities on the wrap.utils pakage 23 24 25 module gtk.Button; 26 27 private import gdk.Window; 28 private import glib.ConstructionException; 29 private import glib.Str; 30 private import gobject.ObjectG; 31 private import gobject.Signals; 32 private import gtk.ActionableIF; 33 private import gtk.ActionableT; 34 private import gtk.ActivatableIF; 35 private import gtk.ActivatableT; 36 private import gtk.Bin; 37 private import gtk.Image; 38 private import gtk.Widget; 39 public import gtkc.gdktypes; 40 private import gtkc.gtk; 41 public import gtkc.gtktypes; 42 43 44 /** 45 * The #GtkButton widget is generally used to trigger a callback function that is 46 * called when the button is pressed. The various signals and how to use them 47 * are outlined below. 48 * 49 * The #GtkButton widget can hold any valid child widget. That is, it can hold 50 * almost any other standard #GtkWidget. The most commonly used child is the 51 * #GtkLabel. 52 * 53 * # CSS nodes 54 * 55 * GtkButton has a single CSS node with name button. The node will get the 56 * style classes .image-button or .text-button, if the content is just an 57 * image or label, respectively. It may also receive the .flat style class. 58 * 59 * Other style classes that are commonly used with GtkButton include 60 * .suggested-action and .destructive-action. 61 * 62 * Button-like widgets like #GtkToggleButton, #GtkMenuButton, #GtkVolumeButton, 63 * #GtkLockButton, #GtkColorButton, #GtkFontButton or #GtkFileChooserButton use 64 * style classes such as .toggle, .popup, .scale, .lock, .color, .font, .file 65 * to differentiate themselves from a plain GtkButton. 66 */ 67 public class Button : Bin, ActionableIF, ActivatableIF 68 { 69 /** the main Gtk struct */ 70 protected GtkButton* gtkButton; 71 72 /** Get the main Gtk struct */ 73 public GtkButton* getButtonStruct() 74 { 75 return gtkButton; 76 } 77 78 /** the main Gtk struct as a void* */ 79 protected override void* getStruct() 80 { 81 return cast(void*)gtkButton; 82 } 83 84 protected override void setStruct(GObject* obj) 85 { 86 gtkButton = cast(GtkButton*)obj; 87 super.setStruct(obj); 88 } 89 90 /** 91 * Sets our main struct and passes it to the parent class. 92 */ 93 public this (GtkButton* gtkButton, bool ownedRef = false) 94 { 95 this.gtkButton = gtkButton; 96 super(cast(GtkBin*)gtkButton, ownedRef); 97 } 98 99 // add the Actionable capabilities 100 mixin ActionableT!(GtkButton); 101 102 // add the Activatable capabilities 103 mixin ActivatableT!(GtkButton); 104 105 private static IconSize currentIconSize = IconSize.BUTTON; 106 107 /** */ 108 public static void setIconSize(IconSize iconSize) 109 { 110 currentIconSize = iconSize; 111 } 112 113 /** */ 114 public static IconSize getIconSize() 115 { 116 return currentIconSize; 117 } 118 119 /** 120 * Creates a new GtkButton containing a label. 121 * If characters in label are preceded by an underscore, they are underlined. 122 * If you need a literal underscore character in a label, use '__' (two 123 * underscores). The first underlined character represents a keyboard 124 * accelerator called a mnemonic. 125 * Pressing Alt and that key activates the button. 126 * Params: 127 * label = The text of the button, with an underscore in front of the 128 * mnemonic character 129 * mnemonic = true if the button has an mnemnonic 130 * Returns: 131 * a new GtkButton 132 * Throws: ConstructionException GTK+ fails to create the object. 133 */ 134 public this (string label, bool mnemonic=true) 135 { 136 GtkButton* p; 137 138 if ( mnemonic ) 139 { 140 // GtkWidget* gtk_button_new_with_mnemonic (const gchar *label); 141 p = cast(GtkButton*)gtk_button_new_with_mnemonic(Str.toStringz(label)); 142 } 143 else 144 { 145 // GtkWidget* gtk_button_new_with_label (const gchar *label); 146 p = cast(GtkButton*)gtk_button_new_with_label(Str.toStringz(label)); 147 } 148 149 if(p is null) 150 { 151 throw new ConstructionException("null returned by gtk_button_new_with_label"); 152 } 153 154 this(p); 155 } 156 157 /** 158 * Creates a new GtkButton containing the image and text from a stock item. 159 * Some stock ids have preprocessor macros like GTK_STOCK_OK and 160 * GTK_STOCK_APPLY. 161 * If stock_id is unknown, then it will be treated as a mnemonic 162 * label (as for gtk_button_new_with_mnemonic()). 163 * Params: 164 * StockID = the name of the stock item 165 * Throws: ConstructionException GTK+ fails to create the object. 166 */ 167 public this (StockID stockID, bool hideLabel=false) 168 { 169 // GtkWidget* gtk_button_new_from_stock (const gchar *stock_id); 170 if ( hideLabel ) 171 { 172 this(); 173 Image image = new Image(stockID,currentIconSize); 174 add(image); 175 } 176 else 177 { 178 auto p = gtk_button_new_from_stock(Str.toStringz(stockID)); 179 180 if(p is null) 181 { 182 throw new ConstructionException("null returned by gtk_button_new_from_stock"); 183 } 184 185 this(cast(GtkButton*) p); 186 } 187 } 188 189 /** */ 190 public this(StockID stockID, void delegate(Button) dlg, bool hideLabel=false) 191 { 192 this(stockID, hideLabel); 193 addOnClicked(dlg); 194 } 195 196 /** */ 197 public this(string label, void delegate(Button) dlg, bool mnemonic=true) 198 { 199 this(label, mnemonic); 200 addOnClicked(dlg); 201 } 202 203 /** */ 204 public this(string label, void delegate(Button) dlg, string action) 205 { 206 this(label); 207 setActionName(action); 208 addOnClicked(dlg); 209 } 210 211 /** 212 */ 213 214 /** */ 215 public static GType getType() 216 { 217 return gtk_button_get_type(); 218 } 219 220 /** 221 * Creates a new #GtkButton widget. To add a child widget to the button, 222 * use gtk_container_add(). 223 * 224 * Return: The newly created #GtkButton widget. 225 * 226 * Throws: ConstructionException GTK+ fails to create the object. 227 */ 228 public this() 229 { 230 auto p = gtk_button_new(); 231 232 if(p is null) 233 { 234 throw new ConstructionException("null returned by new"); 235 } 236 237 this(cast(GtkButton*) p); 238 } 239 240 /** 241 * Creates a new button containing an icon from the current icon theme. 242 * 243 * If the icon name isn’t known, a “broken image” icon will be 244 * displayed instead. If the current icon theme is changed, the icon 245 * will be updated appropriately. 246 * 247 * This function is a convenience wrapper around gtk_button_new() and 248 * gtk_button_set_image(). 249 * 250 * Params: 251 * iconName = an icon name 252 * size = an icon size (#GtkIconSize) 253 * 254 * Return: a new #GtkButton displaying the themed icon 255 * 256 * Since: 3.10 257 * 258 * Throws: ConstructionException GTK+ fails to create the object. 259 */ 260 public this(string iconName, GtkIconSize size) 261 { 262 auto p = gtk_button_new_from_icon_name(Str.toStringz(iconName), size); 263 264 if(p is null) 265 { 266 throw new ConstructionException("null returned by new_from_icon_name"); 267 } 268 269 this(cast(GtkButton*) p); 270 } 271 272 /** 273 * Emits a #GtkButton::clicked signal to the given #GtkButton. 274 */ 275 public void clicked() 276 { 277 gtk_button_clicked(gtkButton); 278 } 279 280 /** 281 * Emits a #GtkButton::enter signal to the given #GtkButton. 282 * 283 * Deprecated: Use the #GtkWidget::enter-notify-event signal. 284 */ 285 public void enter() 286 { 287 gtk_button_enter(gtkButton); 288 } 289 290 /** 291 * Gets the alignment of the child in the button. 292 * 293 * Deprecated: Access the child widget directly if you need to control 294 * its alignment. 295 * 296 * Params: 297 * xalign = return location for horizontal alignment 298 * yalign = return location for vertical alignment 299 * 300 * Since: 2.4 301 */ 302 public void getAlignment(out float xalign, out float yalign) 303 { 304 gtk_button_get_alignment(gtkButton, &xalign, &yalign); 305 } 306 307 /** 308 * Returns whether the button will ignore the #GtkSettings:gtk-button-images 309 * setting and always show the image, if available. 310 * 311 * Return: %TRUE if the button will always show the image 312 * 313 * Since: 3.6 314 */ 315 public bool getAlwaysShowImage() 316 { 317 return gtk_button_get_always_show_image(gtkButton) != 0; 318 } 319 320 /** 321 * Returns the button’s event window if it is realized, %NULL otherwise. 322 * This function should be rarely needed. 323 * 324 * Return: @button’s event window. 325 * 326 * Since: 2.22 327 */ 328 public Window getEventWindow() 329 { 330 auto p = gtk_button_get_event_window(gtkButton); 331 332 if(p is null) 333 { 334 return null; 335 } 336 337 return ObjectG.getDObject!(Window)(cast(GdkWindow*) p); 338 } 339 340 /** 341 * Returns whether the button grabs focus when it is clicked with the mouse. 342 * See gtk_button_set_focus_on_click(). 343 * 344 * Return: %TRUE if the button grabs focus when it is clicked with 345 * the mouse. 346 * 347 * Since: 2.4 348 */ 349 public override bool getFocusOnClick() 350 { 351 return gtk_button_get_focus_on_click(gtkButton) != 0; 352 } 353 354 /** 355 * Gets the widget that is currenty set as the image of @button. 356 * This may have been explicitly set by gtk_button_set_image() 357 * or constructed by gtk_button_new_from_stock(). 358 * 359 * Return: a #GtkWidget or %NULL in case 360 * there is no image 361 * 362 * Since: 2.6 363 */ 364 public Widget getImage() 365 { 366 auto p = gtk_button_get_image(gtkButton); 367 368 if(p is null) 369 { 370 return null; 371 } 372 373 return ObjectG.getDObject!(Widget)(cast(GtkWidget*) p); 374 } 375 376 /** 377 * Gets the position of the image relative to the text 378 * inside the button. 379 * 380 * Return: the position 381 * 382 * Since: 2.10 383 */ 384 public GtkPositionType getImagePosition() 385 { 386 return gtk_button_get_image_position(gtkButton); 387 } 388 389 /** 390 * Fetches the text from the label of the button, as set by 391 * gtk_button_set_label(). If the label text has not 392 * been set the return value will be %NULL. This will be the 393 * case if you create an empty button with gtk_button_new() to 394 * use as a container. 395 * 396 * Return: The text of the label widget. This string is owned 397 * by the widget and must not be modified or freed. 398 */ 399 public string getLabel() 400 { 401 return Str.toString(gtk_button_get_label(gtkButton)); 402 } 403 404 /** 405 * Returns the current relief style of the given #GtkButton. 406 * 407 * Return: The current #GtkReliefStyle 408 */ 409 public GtkReliefStyle getRelief() 410 { 411 return gtk_button_get_relief(gtkButton); 412 } 413 414 /** 415 * Returns whether the button label is a stock item. 416 * 417 * Return: %TRUE if the button label is used to 418 * select a stock item instead of being 419 * used directly as the label text. 420 */ 421 public bool getUseStock() 422 { 423 return gtk_button_get_use_stock(gtkButton) != 0; 424 } 425 426 /** 427 * Returns whether an embedded underline in the button label indicates a 428 * mnemonic. See gtk_button_set_use_underline (). 429 * 430 * Return: %TRUE if an embedded underline in the button label 431 * indicates the mnemonic accelerator keys. 432 */ 433 public bool getUseUnderline() 434 { 435 return gtk_button_get_use_underline(gtkButton) != 0; 436 } 437 438 /** 439 * Emits a #GtkButton::leave signal to the given #GtkButton. 440 * 441 * Deprecated: Use the #GtkWidget::leave-notify-event signal. 442 */ 443 public void leave() 444 { 445 gtk_button_leave(gtkButton); 446 } 447 448 /** 449 * Emits a #GtkButton::pressed signal to the given #GtkButton. 450 * 451 * Deprecated: Use the #GtkWidget::button-press-event signal. 452 */ 453 public void pressed() 454 { 455 gtk_button_pressed(gtkButton); 456 } 457 458 /** 459 * Emits a #GtkButton::released signal to the given #GtkButton. 460 * 461 * Deprecated: Use the #GtkWidget::button-release-event signal. 462 */ 463 public void released() 464 { 465 gtk_button_released(gtkButton); 466 } 467 468 /** 469 * Sets the alignment of the child. This property has no effect unless 470 * the child is a #GtkMisc or a #GtkAlignment. 471 * 472 * Deprecated: Access the child widget directly if you need to control 473 * its alignment. 474 * 475 * Params: 476 * xalign = the horizontal position of the child, 0.0 is left aligned, 477 * 1.0 is right aligned 478 * yalign = the vertical position of the child, 0.0 is top aligned, 479 * 1.0 is bottom aligned 480 * 481 * Since: 2.4 482 */ 483 public void setAlignment(float xalign, float yalign) 484 { 485 gtk_button_set_alignment(gtkButton, xalign, yalign); 486 } 487 488 /** 489 * If %TRUE, the button will ignore the #GtkSettings:gtk-button-images 490 * setting and always show the image, if available. 491 * 492 * Use this property if the button would be useless or hard to use 493 * without the image. 494 * 495 * Params: 496 * alwaysShow = %TRUE if the menuitem should always show the image 497 * 498 * Since: 3.6 499 */ 500 public void setAlwaysShowImage(bool alwaysShow) 501 { 502 gtk_button_set_always_show_image(gtkButton, alwaysShow); 503 } 504 505 /** 506 * Sets whether the button will grab focus when it is clicked with the mouse. 507 * Making mouse clicks not grab focus is useful in places like toolbars where 508 * you don’t want the keyboard focus removed from the main area of the 509 * application. 510 * 511 * Params: 512 * focusOnClick = whether the button grabs focus when clicked with the mouse 513 * 514 * Since: 2.4 515 */ 516 public override void setFocusOnClick(bool focusOnClick) 517 { 518 gtk_button_set_focus_on_click(gtkButton, focusOnClick); 519 } 520 521 /** 522 * Set the image of @button to the given widget. The image will be 523 * displayed if the label text is %NULL or if 524 * #GtkButton:always-show-image is %TRUE. You don’t have to call 525 * gtk_widget_show() on @image yourself. 526 * 527 * Params: 528 * image = a widget to set as the image for the button 529 * 530 * Since: 2.6 531 */ 532 public void setImage(Widget image) 533 { 534 gtk_button_set_image(gtkButton, (image is null) ? null : image.getWidgetStruct()); 535 } 536 537 /** 538 * Sets the position of the image relative to the text 539 * inside the button. 540 * 541 * Params: 542 * position = the position 543 * 544 * Since: 2.10 545 */ 546 public void setImagePosition(GtkPositionType position) 547 { 548 gtk_button_set_image_position(gtkButton, position); 549 } 550 551 /** 552 * Sets the text of the label of the button to @str. This text is 553 * also used to select the stock item if gtk_button_set_use_stock() 554 * is used. 555 * 556 * This will also clear any previously set labels. 557 * 558 * Params: 559 * label = a string 560 */ 561 public void setLabel(string label) 562 { 563 gtk_button_set_label(gtkButton, Str.toStringz(label)); 564 } 565 566 /** 567 * Sets the relief style of the edges of the given #GtkButton widget. 568 * Two styles exist, %GTK_RELIEF_NORMAL and %GTK_RELIEF_NONE. 569 * The default style is, as one can guess, %GTK_RELIEF_NORMAL. 570 * The deprecated value %GTK_RELIEF_HALF behaves the same as 571 * %GTK_RELIEF_NORMAL. 572 * 573 * Params: 574 * relief = The GtkReliefStyle as described above 575 */ 576 public void setRelief(GtkReliefStyle relief) 577 { 578 gtk_button_set_relief(gtkButton, relief); 579 } 580 581 /** 582 * If %TRUE, the label set on the button is used as a 583 * stock id to select the stock item for the button. 584 * 585 * Params: 586 * useStock = %TRUE if the button should use a stock item 587 */ 588 public void setUseStock(bool useStock) 589 { 590 gtk_button_set_use_stock(gtkButton, useStock); 591 } 592 593 /** 594 * If true, an underline in the text of the button label indicates 595 * the next character should be used for the mnemonic accelerator key. 596 * 597 * Params: 598 * useUnderline = %TRUE if underlines in the text indicate mnemonics 599 */ 600 public void setUseUnderline(bool useUnderline) 601 { 602 gtk_button_set_use_underline(gtkButton, useUnderline); 603 } 604 605 int[string] connectedSignals; 606 607 void delegate(Button)[] onActivateListeners; 608 /** 609 * The ::activate signal on GtkButton is an action signal and 610 * emitting it causes the button to animate press then release. 611 * Applications should never connect to this signal, but use the 612 * #GtkButton::clicked signal. 613 */ 614 void addOnActivate(void delegate(Button) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 615 { 616 if ( "activate" !in connectedSignals ) 617 { 618 Signals.connectData( 619 this, 620 "activate", 621 cast(GCallback)&callBackActivate, 622 cast(void*)this, 623 null, 624 connectFlags); 625 connectedSignals["activate"] = 1; 626 } 627 onActivateListeners ~= dlg; 628 } 629 extern(C) static void callBackActivate(GtkButton* buttonStruct, Button _button) 630 { 631 foreach ( void delegate(Button) dlg; _button.onActivateListeners ) 632 { 633 dlg(_button); 634 } 635 } 636 637 void delegate(Button)[] onClickedListeners; 638 /** 639 * Emitted when the button has been activated (pressed and released). 640 */ 641 void addOnClicked(void delegate(Button) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 642 { 643 if ( "clicked" !in connectedSignals ) 644 { 645 Signals.connectData( 646 this, 647 "clicked", 648 cast(GCallback)&callBackClicked, 649 cast(void*)this, 650 null, 651 connectFlags); 652 connectedSignals["clicked"] = 1; 653 } 654 onClickedListeners ~= dlg; 655 } 656 extern(C) static void callBackClicked(GtkButton* buttonStruct, Button _button) 657 { 658 foreach ( void delegate(Button) dlg; _button.onClickedListeners ) 659 { 660 dlg(_button); 661 } 662 } 663 664 void delegate(Button)[] onEnterListeners; 665 /** 666 * Emitted when the pointer enters the button. 667 * 668 * Deprecated: Use the #GtkWidget::enter-notify-event signal. 669 */ 670 void addOnEnter(void delegate(Button) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 671 { 672 if ( "enter" !in connectedSignals ) 673 { 674 Signals.connectData( 675 this, 676 "enter", 677 cast(GCallback)&callBackEnter, 678 cast(void*)this, 679 null, 680 connectFlags); 681 connectedSignals["enter"] = 1; 682 } 683 onEnterListeners ~= dlg; 684 } 685 extern(C) static void callBackEnter(GtkButton* buttonStruct, Button _button) 686 { 687 foreach ( void delegate(Button) dlg; _button.onEnterListeners ) 688 { 689 dlg(_button); 690 } 691 } 692 693 void delegate(Button)[] onLeaveListeners; 694 /** 695 * Emitted when the pointer leaves the button. 696 * 697 * Deprecated: Use the #GtkWidget::leave-notify-event signal. 698 */ 699 void addOnLeave(void delegate(Button) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 700 { 701 if ( "leave" !in connectedSignals ) 702 { 703 Signals.connectData( 704 this, 705 "leave", 706 cast(GCallback)&callBackLeave, 707 cast(void*)this, 708 null, 709 connectFlags); 710 connectedSignals["leave"] = 1; 711 } 712 onLeaveListeners ~= dlg; 713 } 714 extern(C) static void callBackLeave(GtkButton* buttonStruct, Button _button) 715 { 716 foreach ( void delegate(Button) dlg; _button.onLeaveListeners ) 717 { 718 dlg(_button); 719 } 720 } 721 722 void delegate(Button)[] onPressedListeners; 723 /** 724 * Emitted when the button is pressed. 725 * 726 * Deprecated: Use the #GtkWidget::button-press-event signal. 727 */ 728 void addOnPressed(void delegate(Button) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 729 { 730 if ( "pressed" !in connectedSignals ) 731 { 732 Signals.connectData( 733 this, 734 "pressed", 735 cast(GCallback)&callBackPressed, 736 cast(void*)this, 737 null, 738 connectFlags); 739 connectedSignals["pressed"] = 1; 740 } 741 onPressedListeners ~= dlg; 742 } 743 extern(C) static void callBackPressed(GtkButton* buttonStruct, Button _button) 744 { 745 foreach ( void delegate(Button) dlg; _button.onPressedListeners ) 746 { 747 dlg(_button); 748 } 749 } 750 751 void delegate(Button)[] onReleasedListeners; 752 /** 753 * Emitted when the button is released. 754 * 755 * Deprecated: Use the #GtkWidget::button-release-event signal. 756 */ 757 void addOnReleased(void delegate(Button) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 758 { 759 if ( "released" !in connectedSignals ) 760 { 761 Signals.connectData( 762 this, 763 "released", 764 cast(GCallback)&callBackReleased, 765 cast(void*)this, 766 null, 767 connectFlags); 768 connectedSignals["released"] = 1; 769 } 770 onReleasedListeners ~= dlg; 771 } 772 extern(C) static void callBackReleased(GtkButton* buttonStruct, Button _button) 773 { 774 foreach ( void delegate(Button) dlg; _button.onReleasedListeners ) 775 { 776 dlg(_button); 777 } 778 } 779 }