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.ToggleToolButton;
26 
27 private import glib.ConstructionException;
28 private import glib.Str;
29 private import gobject.ObjectG;
30 private import gobject.Signals;
31 private import gtk.ToolButton;
32 private import gtk.ToolItem;
33 private import gtk.c.functions;
34 public  import gtk.c.types;
35 public  import gtkc.gtktypes;
36 private import std.algorithm;
37 
38 
39 /**
40  * A #GtkToggleToolButton is a #GtkToolItem that contains a toggle
41  * button.
42  * 
43  * Use gtk_toggle_tool_button_new() to create a new GtkToggleToolButton.
44  * 
45  * # CSS nodes
46  * 
47  * GtkToggleToolButton has a single CSS node with name togglebutton.
48  */
49 public class ToggleToolButton : ToolButton
50 {
51 	/** the main Gtk struct */
52 	protected GtkToggleToolButton* gtkToggleToolButton;
53 
54 	/** Get the main Gtk struct */
55 	public GtkToggleToolButton* getToggleToolButtonStruct(bool transferOwnership = false)
56 	{
57 		if (transferOwnership)
58 			ownedRef = false;
59 		return gtkToggleToolButton;
60 	}
61 
62 	/** the main Gtk struct as a void* */
63 	protected override void* getStruct()
64 	{
65 		return cast(void*)gtkToggleToolButton;
66 	}
67 
68 	protected override void setStruct(GObject* obj)
69 	{
70 		gtkToggleToolButton = cast(GtkToggleToolButton*)obj;
71 		super.setStruct(obj);
72 	}
73 
74 	/**
75 	 * Sets our main struct and passes it to the parent class.
76 	 */
77 	public this (GtkToggleToolButton* gtkToggleToolButton, bool ownedRef = false)
78 	{
79 		this.gtkToggleToolButton = gtkToggleToolButton;
80 		super(cast(GtkToolButton*)gtkToggleToolButton, ownedRef);
81 	}
82 
83 	/**
84 	 * Creates a new GtkToggleToolButton containing the image and text
85 	 * from a stock item.
86 	 */
87 	public this(StockID stockId)
88 	{
89 		this(cast(string)stockId);
90 	}
91 
92 	/**
93 	 */
94 
95 	/** */
96 	public static GType getType()
97 	{
98 		return gtk_toggle_tool_button_get_type();
99 	}
100 
101 	/**
102 	 * Returns a new #GtkToggleToolButton
103 	 *
104 	 * Returns: a newly created #GtkToggleToolButton
105 	 *
106 	 * Since: 2.4
107 	 *
108 	 * Throws: ConstructionException GTK+ fails to create the object.
109 	 */
110 	public this()
111 	{
112 		auto p = gtk_toggle_tool_button_new();
113 
114 		if(p is null)
115 		{
116 			throw new ConstructionException("null returned by new");
117 		}
118 
119 		this(cast(GtkToggleToolButton*) p);
120 	}
121 
122 	/**
123 	 * Creates a new #GtkToggleToolButton containing the image and text from a
124 	 * stock item. Some stock ids have preprocessor macros like #GTK_STOCK_OK
125 	 * and #GTK_STOCK_APPLY.
126 	 *
127 	 * It is an error if @stock_id is not a name of a stock item.
128 	 *
129 	 * Deprecated: Use gtk_toggle_tool_button_new() instead.
130 	 *
131 	 * Params:
132 	 *     stockId = the name of the stock item
133 	 *
134 	 * Returns: A new #GtkToggleToolButton
135 	 *
136 	 * Since: 2.4
137 	 *
138 	 * Throws: ConstructionException GTK+ fails to create the object.
139 	 */
140 	public this(string stockId)
141 	{
142 		auto p = gtk_toggle_tool_button_new_from_stock(Str.toStringz(stockId));
143 
144 		if(p is null)
145 		{
146 			throw new ConstructionException("null returned by new_from_stock");
147 		}
148 
149 		this(cast(GtkToggleToolButton*) p);
150 	}
151 
152 	/**
153 	 * Queries a #GtkToggleToolButton and returns its current state.
154 	 * Returns %TRUE if the toggle button is pressed in and %FALSE if it is raised.
155 	 *
156 	 * Returns: %TRUE if the toggle tool button is pressed in, %FALSE if not
157 	 *
158 	 * Since: 2.4
159 	 */
160 	public bool getActive()
161 	{
162 		return gtk_toggle_tool_button_get_active(gtkToggleToolButton) != 0;
163 	}
164 
165 	/**
166 	 * Sets the status of the toggle tool button. Set to %TRUE if you
167 	 * want the GtkToggleButton to be “pressed in”, and %FALSE to raise it.
168 	 * This action causes the toggled signal to be emitted.
169 	 *
170 	 * Params:
171 	 *     isActive = whether @button should be active
172 	 *
173 	 * Since: 2.4
174 	 */
175 	public void setActive(bool isActive)
176 	{
177 		gtk_toggle_tool_button_set_active(gtkToggleToolButton, isActive);
178 	}
179 
180 	protected class OnToggledDelegateWrapper
181 	{
182 		void delegate(ToggleToolButton) dlg;
183 		gulong handlerId;
184 
185 		this(void delegate(ToggleToolButton) dlg)
186 		{
187 			this.dlg = dlg;
188 			onToggledListeners ~= this;
189 		}
190 
191 		void remove(OnToggledDelegateWrapper source)
192 		{
193 			foreach(index, wrapper; onToggledListeners)
194 			{
195 				if (wrapper.handlerId == source.handlerId)
196 				{
197 					onToggledListeners[index] = null;
198 					onToggledListeners = std.algorithm.remove(onToggledListeners, index);
199 					break;
200 				}
201 			}
202 		}
203 	}
204 	OnToggledDelegateWrapper[] onToggledListeners;
205 
206 	/**
207 	 * Emitted whenever the toggle tool button changes state.
208 	 */
209 	gulong addOnToggled(void delegate(ToggleToolButton) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
210 	{
211 		auto wrapper = new OnToggledDelegateWrapper(dlg);
212 		wrapper.handlerId = Signals.connectData(
213 			this,
214 			"toggled",
215 			cast(GCallback)&callBackToggled,
216 			cast(void*)wrapper,
217 			cast(GClosureNotify)&callBackToggledDestroy,
218 			connectFlags);
219 		return wrapper.handlerId;
220 	}
221 
222 	extern(C) static void callBackToggled(GtkToggleToolButton* toggletoolbuttonStruct, OnToggledDelegateWrapper wrapper)
223 	{
224 		wrapper.dlg(wrapper.outer);
225 	}
226 
227 	extern(C) static void callBackToggledDestroy(OnToggledDelegateWrapper wrapper, GClosure* closure)
228 	{
229 		wrapper.remove(wrapper);
230 	}
231 }