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