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 gio.SimpleAction;
26 
27 private import gio.ActionIF;
28 private import gio.ActionT;
29 private import gio.c.functions;
30 public  import gio.c.types;
31 private import glib.ConstructionException;
32 private import glib.Str;
33 private import glib.Variant;
34 private import glib.VariantType;
35 private import gobject.ObjectG;
36 private import gobject.Signals;
37 public  import gtkc.giotypes;
38 private import std.algorithm;
39 
40 
41 /**
42  * A #GSimpleAction is the obvious simple implementation of the #GAction
43  * interface. This is the easiest way to create an action for purposes of
44  * adding it to a #GSimpleActionGroup.
45  * 
46  * See also #GtkAction.
47  */
48 public class SimpleAction : ObjectG, ActionIF
49 {
50 	/** the main Gtk struct */
51 	protected GSimpleAction* gSimpleAction;
52 
53 	/** Get the main Gtk struct */
54 	public GSimpleAction* getSimpleActionStruct(bool transferOwnership = false)
55 	{
56 		if (transferOwnership)
57 			ownedRef = false;
58 		return gSimpleAction;
59 	}
60 
61 	/** the main Gtk struct as a void* */
62 	protected override void* getStruct()
63 	{
64 		return cast(void*)gSimpleAction;
65 	}
66 
67 	protected override void setStruct(GObject* obj)
68 	{
69 		gSimpleAction = cast(GSimpleAction*)obj;
70 		super.setStruct(obj);
71 	}
72 
73 	/**
74 	 * Sets our main struct and passes it to the parent class.
75 	 */
76 	public this (GSimpleAction* gSimpleAction, bool ownedRef = false)
77 	{
78 		this.gSimpleAction = gSimpleAction;
79 		super(cast(GObject*)gSimpleAction, ownedRef);
80 	}
81 
82 	// add the Action capabilities
83 	mixin ActionT!(GSimpleAction);
84 
85 
86 	/** */
87 	public static GType getType()
88 	{
89 		return g_simple_action_get_type();
90 	}
91 
92 	/**
93 	 * Creates a new action.
94 	 *
95 	 * The created action is stateless.  See g_simple_action_new_stateful().
96 	 *
97 	 * Params:
98 	 *     name = the name of the action
99 	 *     parameterType = the type of parameter to the activate function
100 	 *
101 	 * Returns: a new #GSimpleAction
102 	 *
103 	 * Since: 2.28
104 	 *
105 	 * Throws: ConstructionException GTK+ fails to create the object.
106 	 */
107 	public this(string name, VariantType parameterType)
108 	{
109 		auto p = g_simple_action_new(Str.toStringz(name), (parameterType is null) ? null : parameterType.getVariantTypeStruct());
110 
111 		if(p is null)
112 		{
113 			throw new ConstructionException("null returned by new");
114 		}
115 
116 		this(cast(GSimpleAction*) p, true);
117 	}
118 
119 	/**
120 	 * Creates a new stateful action.
121 	 *
122 	 * @state is the initial state of the action.  All future state values
123 	 * must have the same #GVariantType as the initial state.
124 	 *
125 	 * If the @state GVariant is floating, it is consumed.
126 	 *
127 	 * Params:
128 	 *     name = the name of the action
129 	 *     parameterType = the type of the parameter to the activate function
130 	 *     state = the initial state of the action
131 	 *
132 	 * Returns: a new #GSimpleAction
133 	 *
134 	 * Since: 2.28
135 	 *
136 	 * Throws: ConstructionException GTK+ fails to create the object.
137 	 */
138 	public this(string name, VariantType parameterType, Variant state)
139 	{
140 		auto p = g_simple_action_new_stateful(Str.toStringz(name), (parameterType is null) ? null : parameterType.getVariantTypeStruct(), (state is null) ? null : state.getVariantStruct());
141 
142 		if(p is null)
143 		{
144 			throw new ConstructionException("null returned by new_stateful");
145 		}
146 
147 		this(cast(GSimpleAction*) p, true);
148 	}
149 
150 	/**
151 	 * Sets the action as enabled or not.
152 	 *
153 	 * An action must be enabled in order to be activated or in order to
154 	 * have its state changed from outside callers.
155 	 *
156 	 * This should only be called by the implementor of the action.  Users
157 	 * of the action should not attempt to modify its enabled flag.
158 	 *
159 	 * Params:
160 	 *     enabled = whether the action is enabled
161 	 *
162 	 * Since: 2.28
163 	 */
164 	public void setEnabled(bool enabled)
165 	{
166 		g_simple_action_set_enabled(gSimpleAction, enabled);
167 	}
168 
169 	/**
170 	 * Sets the state of the action.
171 	 *
172 	 * This directly updates the 'state' property to the given value.
173 	 *
174 	 * This should only be called by the implementor of the action.  Users
175 	 * of the action should not attempt to directly modify the 'state'
176 	 * property.  Instead, they should call g_action_change_state() to
177 	 * request the change.
178 	 *
179 	 * If the @value GVariant is floating, it is consumed.
180 	 *
181 	 * Params:
182 	 *     value = the new #GVariant for the state
183 	 *
184 	 * Since: 2.30
185 	 */
186 	public void setState(Variant value)
187 	{
188 		g_simple_action_set_state(gSimpleAction, (value is null) ? null : value.getVariantStruct());
189 	}
190 
191 	/**
192 	 * Sets the state hint for the action.
193 	 *
194 	 * See g_action_get_state_hint() for more information about
195 	 * action state hints.
196 	 *
197 	 * Params:
198 	 *     stateHint = a #GVariant representing the state hint
199 	 *
200 	 * Since: 2.44
201 	 */
202 	public void setStateHint(Variant stateHint)
203 	{
204 		g_simple_action_set_state_hint(gSimpleAction, (stateHint is null) ? null : stateHint.getVariantStruct());
205 	}
206 
207 	protected class OnActivateDelegateWrapper
208 	{
209 		void delegate(Variant, SimpleAction) dlg;
210 		gulong handlerId;
211 
212 		this(void delegate(Variant, SimpleAction) dlg)
213 		{
214 			this.dlg = dlg;
215 			onActivateListeners ~= this;
216 		}
217 
218 		void remove(OnActivateDelegateWrapper source)
219 		{
220 			foreach(index, wrapper; onActivateListeners)
221 			{
222 				if (wrapper.handlerId == source.handlerId)
223 				{
224 					onActivateListeners[index] = null;
225 					onActivateListeners = std.algorithm.remove(onActivateListeners, index);
226 					break;
227 				}
228 			}
229 		}
230 	}
231 	OnActivateDelegateWrapper[] onActivateListeners;
232 
233 	/**
234 	 * Indicates that the action was just activated.
235 	 *
236 	 * @parameter will always be of the expected type.  In the event that
237 	 * an incorrect type was given, no signal will be emitted.
238 	 *
239 	 * Since GLib 2.40, if no handler is connected to this signal then the
240 	 * default behaviour for boolean-stated actions with a %NULL parameter
241 	 * type is to toggle them via the #GSimpleAction::change-state signal.
242 	 * For stateful actions where the state type is equal to the parameter
243 	 * type, the default is to forward them directly to
244 	 * #GSimpleAction::change-state.  This should allow almost all users
245 	 * of #GSimpleAction to connect only one handler or the other.
246 	 *
247 	 * Params:
248 	 *     parameter = the parameter to the activation
249 	 *
250 	 * Since: 2.28
251 	 */
252 	gulong addOnActivate(void delegate(Variant, SimpleAction) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
253 	{
254 		auto wrapper = new OnActivateDelegateWrapper(dlg);
255 		wrapper.handlerId = Signals.connectData(
256 			this,
257 			"activate",
258 			cast(GCallback)&callBackActivate,
259 			cast(void*)wrapper,
260 			cast(GClosureNotify)&callBackActivateDestroy,
261 			connectFlags);
262 		return wrapper.handlerId;
263 	}
264 
265 	extern(C) static void callBackActivate(GSimpleAction* simpleactionStruct, GVariant* parameter, OnActivateDelegateWrapper wrapper)
266 	{
267 		wrapper.dlg(new Variant(parameter), wrapper.outer);
268 	}
269 
270 	extern(C) static void callBackActivateDestroy(OnActivateDelegateWrapper wrapper, GClosure* closure)
271 	{
272 		wrapper.remove(wrapper);
273 	}
274 
275 	protected class OnChangeStateDelegateWrapper
276 	{
277 		void delegate(Variant, SimpleAction) dlg;
278 		gulong handlerId;
279 
280 		this(void delegate(Variant, SimpleAction) dlg)
281 		{
282 			this.dlg = dlg;
283 			onChangeStateListeners ~= this;
284 		}
285 
286 		void remove(OnChangeStateDelegateWrapper source)
287 		{
288 			foreach(index, wrapper; onChangeStateListeners)
289 			{
290 				if (wrapper.handlerId == source.handlerId)
291 				{
292 					onChangeStateListeners[index] = null;
293 					onChangeStateListeners = std.algorithm.remove(onChangeStateListeners, index);
294 					break;
295 				}
296 			}
297 		}
298 	}
299 	OnChangeStateDelegateWrapper[] onChangeStateListeners;
300 
301 	/**
302 	 * Indicates that the action just received a request to change its
303 	 * state.
304 	 *
305 	 * @value will always be of the correct state type.  In the event that
306 	 * an incorrect type was given, no signal will be emitted.
307 	 *
308 	 * If no handler is connected to this signal then the default
309 	 * behaviour is to call g_simple_action_set_state() to set the state
310 	 * to the requested value. If you connect a signal handler then no
311 	 * default action is taken. If the state should change then you must
312 	 * call g_simple_action_set_state() from the handler.
313 	 *
314 	 * An example of a 'change-state' handler:
315 	 * |[<!-- language="C" -->
316 	 * static void
317 	 * change_volume_state (GSimpleAction *action,
318 	 * GVariant      *value,
319 	 * gpointer       user_data)
320 	 * {
321 	 * gint requested;
322 	 *
323 	 * requested = g_variant_get_int32 (value);
324 	 *
325 	 * // Volume only goes from 0 to 10
326 	 * if (0 <= requested && requested <= 10)
327 	 * g_simple_action_set_state (action, value);
328 	 * }
329 	 * ]|
330 	 *
331 	 * The handler need not set the state to the requested value.
332 	 * It could set it to any value at all, or take some other action.
333 	 *
334 	 * Params:
335 	 *     value = the requested value for the state
336 	 *
337 	 * Since: 2.30
338 	 */
339 	gulong addOnChangeState(void delegate(Variant, SimpleAction) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
340 	{
341 		auto wrapper = new OnChangeStateDelegateWrapper(dlg);
342 		wrapper.handlerId = Signals.connectData(
343 			this,
344 			"change-state",
345 			cast(GCallback)&callBackChangeState,
346 			cast(void*)wrapper,
347 			cast(GClosureNotify)&callBackChangeStateDestroy,
348 			connectFlags);
349 		return wrapper.handlerId;
350 	}
351 
352 	extern(C) static void callBackChangeState(GSimpleAction* simpleactionStruct, GVariant* value, OnChangeStateDelegateWrapper wrapper)
353 	{
354 		wrapper.dlg(new Variant(value), wrapper.outer);
355 	}
356 
357 	extern(C) static void callBackChangeStateDestroy(OnChangeStateDelegateWrapper wrapper, GClosure* closure)
358 	{
359 		wrapper.remove(wrapper);
360 	}
361 }