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