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