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