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.Switch;
26 
27 private import glib.ConstructionException;
28 private import gobject.ObjectG;
29 private import gobject.Signals;
30 private import gtk.ActionableIF;
31 private import gtk.ActionableT;
32 private import gtk.ActivatableIF;
33 private import gtk.ActivatableT;
34 private import gtk.Widget;
35 public  import gtkc.gdktypes;
36 private import gtkc.gtk;
37 public  import gtkc.gtktypes;
38 private import std.algorithm;
39 
40 
41 /**
42  * #GtkSwitch is a widget that has two states: on or off. The user can control
43  * which state should be active by clicking the empty area, or by dragging the
44  * handle.
45  * 
46  * GtkSwitch can also handle situations where the underlying state changes with
47  * a delay. See #GtkSwitch::state-set for details.
48  * 
49  * # CSS nodes
50  * 
51  * |[<!-- language="plain" -->
52  * switch
53  * ╰── slider
54  * ]|
55  * 
56  * GtkSwitch has two css nodes, the main node with the name switch and a subnode
57  * named slider. Neither of them is using any style classes.
58  */
59 public class Switch : Widget, ActionableIF, ActivatableIF
60 {
61 	/** the main Gtk struct */
62 	protected GtkSwitch* gtkSwitch;
63 
64 	/** Get the main Gtk struct */
65 	public GtkSwitch* getSwitchStruct()
66 	{
67 		return gtkSwitch;
68 	}
69 
70 	/** the main Gtk struct as a void* */
71 	protected override void* getStruct()
72 	{
73 		return cast(void*)gtkSwitch;
74 	}
75 
76 	protected override void setStruct(GObject* obj)
77 	{
78 		gtkSwitch = cast(GtkSwitch*)obj;
79 		super.setStruct(obj);
80 	}
81 
82 	/**
83 	 * Sets our main struct and passes it to the parent class.
84 	 */
85 	public this (GtkSwitch* gtkSwitch, bool ownedRef = false)
86 	{
87 		this.gtkSwitch = gtkSwitch;
88 		super(cast(GtkWidget*)gtkSwitch, ownedRef);
89 	}
90 
91 	// add the Actionable capabilities
92 	mixin ActionableT!(GtkSwitch);
93 
94 	// add the Activatable capabilities
95 	mixin ActivatableT!(GtkSwitch);
96 
97 
98 	/** */
99 	public static GType getType()
100 	{
101 		return gtk_switch_get_type();
102 	}
103 
104 	/**
105 	 * Creates a new #GtkSwitch widget.
106 	 *
107 	 * Return: the newly created #GtkSwitch instance
108 	 *
109 	 * Since: 3.0
110 	 *
111 	 * Throws: ConstructionException GTK+ fails to create the object.
112 	 */
113 	public this()
114 	{
115 		auto p = gtk_switch_new();
116 		
117 		if(p is null)
118 		{
119 			throw new ConstructionException("null returned by new");
120 		}
121 		
122 		this(cast(GtkSwitch*) p);
123 	}
124 
125 	/**
126 	 * Gets whether the #GtkSwitch is in its “on” or “off” state.
127 	 *
128 	 * Return: %TRUE if the #GtkSwitch is active, and %FALSE otherwise
129 	 *
130 	 * Since: 3.0
131 	 */
132 	public bool getActive()
133 	{
134 		return gtk_switch_get_active(gtkSwitch) != 0;
135 	}
136 
137 	/**
138 	 * Gets the underlying state of the #GtkSwitch.
139 	 *
140 	 * Return: the underlying state
141 	 *
142 	 * Since: 3.14
143 	 */
144 	public bool getState()
145 	{
146 		return gtk_switch_get_state(gtkSwitch) != 0;
147 	}
148 
149 	/**
150 	 * Changes the state of @sw to the desired one.
151 	 *
152 	 * Params:
153 	 *     isActive = %TRUE if @sw should be active, and %FALSE otherwise
154 	 *
155 	 * Since: 3.0
156 	 */
157 	public void setActive(bool isActive)
158 	{
159 		gtk_switch_set_active(gtkSwitch, isActive);
160 	}
161 
162 	/**
163 	 * Sets the underlying state of the #GtkSwitch.
164 	 *
165 	 * Normally, this is the same as #GtkSwitch:active, unless the switch
166 	 * is set up for delayed state changes. This function is typically
167 	 * called from a #GtkSwitch::state-set signal handler.
168 	 *
169 	 * See #GtkSwitch::state-set for details.
170 	 *
171 	 * Params:
172 	 *     state = the new state
173 	 *
174 	 * Since: 3.14
175 	 */
176 	public void setState(bool state)
177 	{
178 		gtk_switch_set_state(gtkSwitch, state);
179 	}
180 
181 	protected class OnActivateDelegateWrapper
182 	{
183 		void delegate(Switch) dlg;
184 		gulong handlerId;
185 		ConnectFlags flags;
186 		this(void delegate(Switch) dlg, gulong handlerId, ConnectFlags flags)
187 		{
188 			this.dlg = dlg;
189 			this.handlerId = handlerId;
190 			this.flags = flags;
191 		}
192 	}
193 	protected OnActivateDelegateWrapper[] onActivateListeners;
194 
195 	/**
196 	 * The ::activate signal on GtkSwitch is an action signal and
197 	 * emitting it causes the switch to animate.
198 	 * Applications should never connect to this signal, but use the
199 	 * notify::active signal.
200 	 */
201 	gulong addOnActivate(void delegate(Switch) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
202 	{
203 		onActivateListeners ~= new OnActivateDelegateWrapper(dlg, 0, connectFlags);
204 		onActivateListeners[onActivateListeners.length - 1].handlerId = Signals.connectData(
205 			this,
206 			"activate",
207 			cast(GCallback)&callBackActivate,
208 			cast(void*)onActivateListeners[onActivateListeners.length - 1],
209 			cast(GClosureNotify)&callBackActivateDestroy,
210 			connectFlags);
211 		return onActivateListeners[onActivateListeners.length - 1].handlerId;
212 	}
213 	
214 	extern(C) static void callBackActivate(GtkSwitch* switchStruct,OnActivateDelegateWrapper wrapper)
215 	{
216 		wrapper.dlg(wrapper.outer);
217 	}
218 	
219 	extern(C) static void callBackActivateDestroy(OnActivateDelegateWrapper wrapper, GClosure* closure)
220 	{
221 		wrapper.outer.internalRemoveOnActivate(wrapper);
222 	}
223 
224 	protected void internalRemoveOnActivate(OnActivateDelegateWrapper source)
225 	{
226 		foreach(index, wrapper; onActivateListeners)
227 		{
228 			if (wrapper.dlg == source.dlg && wrapper.flags == source.flags && wrapper.handlerId == source.handlerId)
229 			{
230 				onActivateListeners[index] = null;
231 				onActivateListeners = std.algorithm.remove(onActivateListeners, index);
232 				break;
233 			}
234 		}
235 	}
236 	
237 
238 	protected class OnStateSetDelegateWrapper
239 	{
240 		bool delegate(bool, Switch) dlg;
241 		gulong handlerId;
242 		ConnectFlags flags;
243 		this(bool delegate(bool, Switch) dlg, gulong handlerId, ConnectFlags flags)
244 		{
245 			this.dlg = dlg;
246 			this.handlerId = handlerId;
247 			this.flags = flags;
248 		}
249 	}
250 	protected OnStateSetDelegateWrapper[] onStateSetListeners;
251 
252 	/**
253 	 * The ::state-set signal on GtkSwitch is emitted to change the underlying
254 	 * state. It is emitted when the user changes the switch position. The
255 	 * default handler keeps the state in sync with the #GtkSwitch:active
256 	 * property.
257 	 *
258 	 * To implement delayed state change, applications can connect to this signal,
259 	 * initiate the change of the underlying state, and call gtk_switch_set_state()
260 	 * when the underlying state change is complete. The signal handler should
261 	 * return %TRUE to prevent the default handler from running.
262 	 *
263 	 * Visually, the underlying state is represented by the trough color of
264 	 * the switch, while the #GtkSwitch:active property is represented by the
265 	 * position of the switch.
266 	 *
267 	 * Params:
268 	 *     state = the new state of the switch
269 	 *
270 	 * Return: %TRUE to stop the signal emission
271 	 *
272 	 * Since: 3.14
273 	 */
274 	gulong addOnStateSet(bool delegate(bool, Switch) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
275 	{
276 		onStateSetListeners ~= new OnStateSetDelegateWrapper(dlg, 0, connectFlags);
277 		onStateSetListeners[onStateSetListeners.length - 1].handlerId = Signals.connectData(
278 			this,
279 			"state-set",
280 			cast(GCallback)&callBackStateSet,
281 			cast(void*)onStateSetListeners[onStateSetListeners.length - 1],
282 			cast(GClosureNotify)&callBackStateSetDestroy,
283 			connectFlags);
284 		return onStateSetListeners[onStateSetListeners.length - 1].handlerId;
285 	}
286 	
287 	extern(C) static int callBackStateSet(GtkSwitch* switchStruct, bool state,OnStateSetDelegateWrapper wrapper)
288 	{
289 		return wrapper.dlg(state, wrapper.outer);
290 	}
291 	
292 	extern(C) static void callBackStateSetDestroy(OnStateSetDelegateWrapper wrapper, GClosure* closure)
293 	{
294 		wrapper.outer.internalRemoveOnStateSet(wrapper);
295 	}
296 
297 	protected void internalRemoveOnStateSet(OnStateSetDelegateWrapper source)
298 	{
299 		foreach(index, wrapper; onStateSetListeners)
300 		{
301 			if (wrapper.dlg == source.dlg && wrapper.flags == source.flags && wrapper.handlerId == source.handlerId)
302 			{
303 				onStateSetListeners[index] = null;
304 				onStateSetListeners = std.algorithm.remove(onStateSetListeners, index);
305 				break;
306 			}
307 		}
308 	}
309 	
310 }