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