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