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