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