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