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.ActionGroupT;
26 
27 public  import glib.Str;
28 public  import glib.Variant;
29 public  import glib.VariantType;
30 public  import gobject.Signals;
31 public  import gtkc.gio;
32 public  import gtkc.giotypes;
33 public  import std.algorithm;
34 
35 
36 /**
37  * #GActionGroup represents a group of actions. Actions can be used to
38  * expose functionality in a structured way, either from one part of a
39  * program to another, or to the outside world. Action groups are often
40  * used together with a #GMenuModel that provides additional
41  * representation data for displaying the actions to the user, e.g. in
42  * a menu.
43  * 
44  * The main way to interact with the actions in a GActionGroup is to
45  * activate them with g_action_group_activate_action(). Activating an
46  * action may require a #GVariant parameter. The required type of the
47  * parameter can be inquired with g_action_group_get_action_parameter_type().
48  * Actions may be disabled, see g_action_group_get_action_enabled().
49  * Activating a disabled action has no effect.
50  * 
51  * Actions may optionally have a state in the form of a #GVariant. The
52  * current state of an action can be inquired with
53  * g_action_group_get_action_state(). Activating a stateful action may
54  * change its state, but it is also possible to set the state by calling
55  * g_action_group_change_action_state().
56  * 
57  * As typical example, consider a text editing application which has an
58  * option to change the current font to 'bold'. A good way to represent
59  * this would be a stateful action, with a boolean state. Activating the
60  * action would toggle the state.
61  * 
62  * Each action in the group has a unique name (which is a string).  All
63  * method calls, except g_action_group_list_actions() take the name of
64  * an action as an argument.
65  * 
66  * The #GActionGroup API is meant to be the 'public' API to the action
67  * group.  The calls here are exactly the interaction that 'external
68  * forces' (eg: UI, incoming D-Bus messages, etc.) are supposed to have
69  * with actions.  'Internal' APIs (ie: ones meant only to be accessed by
70  * the action group implementation) are found on subclasses.  This is
71  * why you will find - for example - g_action_group_get_action_enabled()
72  * but not an equivalent set() call.
73  * 
74  * Signals are emitted on the action group in response to state changes
75  * on individual actions.
76  * 
77  * Implementations of #GActionGroup should provide implementations for
78  * the virtual functions g_action_group_list_actions() and
79  * g_action_group_query_action().  The other virtual functions should
80  * not be implemented - their "wrappers" are actually implemented with
81  * calls to g_action_group_query_action().
82  */
83 public template ActionGroupT(TStruct)
84 {
85 	/** Get the main Gtk struct */
86 	public GActionGroup* getActionGroupStruct(bool transferOwnership = false)
87 	{
88 		if (transferOwnership)
89 			ownedRef = false;
90 		return cast(GActionGroup*)getStruct();
91 	}
92 
93 
94 	/**
95 	 * Emits the #GActionGroup::action-added signal on @action_group.
96 	 *
97 	 * This function should only be called by #GActionGroup implementations.
98 	 *
99 	 * Params:
100 	 *     actionName = the name of an action in the group
101 	 *
102 	 * Since: 2.28
103 	 */
104 	public void actionAdded(string actionName)
105 	{
106 		g_action_group_action_added(getActionGroupStruct(), Str.toStringz(actionName));
107 	}
108 
109 	/**
110 	 * Emits the #GActionGroup::action-enabled-changed signal on @action_group.
111 	 *
112 	 * This function should only be called by #GActionGroup implementations.
113 	 *
114 	 * Params:
115 	 *     actionName = the name of an action in the group
116 	 *     enabled = whether or not the action is now enabled
117 	 *
118 	 * Since: 2.28
119 	 */
120 	public void actionEnabledChanged(string actionName, bool enabled)
121 	{
122 		g_action_group_action_enabled_changed(getActionGroupStruct(), Str.toStringz(actionName), enabled);
123 	}
124 
125 	/**
126 	 * Emits the #GActionGroup::action-removed signal on @action_group.
127 	 *
128 	 * This function should only be called by #GActionGroup implementations.
129 	 *
130 	 * Params:
131 	 *     actionName = the name of an action in the group
132 	 *
133 	 * Since: 2.28
134 	 */
135 	public void actionRemoved(string actionName)
136 	{
137 		g_action_group_action_removed(getActionGroupStruct(), Str.toStringz(actionName));
138 	}
139 
140 	/**
141 	 * Emits the #GActionGroup::action-state-changed signal on @action_group.
142 	 *
143 	 * This function should only be called by #GActionGroup implementations.
144 	 *
145 	 * Params:
146 	 *     actionName = the name of an action in the group
147 	 *     state = the new state of the named action
148 	 *
149 	 * Since: 2.28
150 	 */
151 	public void actionStateChanged(string actionName, Variant state)
152 	{
153 		g_action_group_action_state_changed(getActionGroupStruct(), Str.toStringz(actionName), (state is null) ? null : state.getVariantStruct());
154 	}
155 
156 	/**
157 	 * Activate the named action within @action_group.
158 	 *
159 	 * If the action is expecting a parameter, then the correct type of
160 	 * parameter must be given as @parameter.  If the action is expecting no
161 	 * parameters then @parameter must be %NULL.  See
162 	 * g_action_group_get_action_parameter_type().
163 	 *
164 	 * Params:
165 	 *     actionName = the name of the action to activate
166 	 *     parameter = parameters to the activation
167 	 *
168 	 * Since: 2.28
169 	 */
170 	public void activateAction(string actionName, Variant parameter)
171 	{
172 		g_action_group_activate_action(getActionGroupStruct(), Str.toStringz(actionName), (parameter is null) ? null : parameter.getVariantStruct());
173 	}
174 
175 	/**
176 	 * Request for the state of the named action within @action_group to be
177 	 * changed to @value.
178 	 *
179 	 * The action must be stateful and @value must be of the correct type.
180 	 * See g_action_group_get_action_state_type().
181 	 *
182 	 * This call merely requests a change.  The action may refuse to change
183 	 * its state or may change its state to something other than @value.
184 	 * See g_action_group_get_action_state_hint().
185 	 *
186 	 * If the @value GVariant is floating, it is consumed.
187 	 *
188 	 * Params:
189 	 *     actionName = the name of the action to request the change on
190 	 *     value = the new state
191 	 *
192 	 * Since: 2.28
193 	 */
194 	public void changeActionState(string actionName, Variant value)
195 	{
196 		g_action_group_change_action_state(getActionGroupStruct(), Str.toStringz(actionName), (value is null) ? null : value.getVariantStruct());
197 	}
198 
199 	/**
200 	 * Checks if the named action within @action_group is currently enabled.
201 	 *
202 	 * An action must be enabled in order to be activated or in order to
203 	 * have its state changed from outside callers.
204 	 *
205 	 * Params:
206 	 *     actionName = the name of the action to query
207 	 *
208 	 * Returns: whether or not the action is currently enabled
209 	 *
210 	 * Since: 2.28
211 	 */
212 	public bool getActionEnabled(string actionName)
213 	{
214 		return g_action_group_get_action_enabled(getActionGroupStruct(), Str.toStringz(actionName)) != 0;
215 	}
216 
217 	/**
218 	 * Queries the type of the parameter that must be given when activating
219 	 * the named action within @action_group.
220 	 *
221 	 * When activating the action using g_action_group_activate_action(),
222 	 * the #GVariant given to that function must be of the type returned
223 	 * by this function.
224 	 *
225 	 * In the case that this function returns %NULL, you must not give any
226 	 * #GVariant, but %NULL instead.
227 	 *
228 	 * The parameter type of a particular action will never change but it is
229 	 * possible for an action to be removed and for a new action to be added
230 	 * with the same name but a different parameter type.
231 	 *
232 	 * Params:
233 	 *     actionName = the name of the action to query
234 	 *
235 	 * Returns: the parameter type
236 	 *
237 	 * Since: 2.28
238 	 */
239 	public VariantType getActionParameterType(string actionName)
240 	{
241 		auto p = g_action_group_get_action_parameter_type(getActionGroupStruct(), Str.toStringz(actionName));
242 		
243 		if(p is null)
244 		{
245 			return null;
246 		}
247 		
248 		return new VariantType(cast(GVariantType*) p);
249 	}
250 
251 	/**
252 	 * Queries the current state of the named action within @action_group.
253 	 *
254 	 * If the action is not stateful then %NULL will be returned.  If the
255 	 * action is stateful then the type of the return value is the type
256 	 * given by g_action_group_get_action_state_type().
257 	 *
258 	 * The return value (if non-%NULL) should be freed with
259 	 * g_variant_unref() when it is no longer required.
260 	 *
261 	 * Params:
262 	 *     actionName = the name of the action to query
263 	 *
264 	 * Returns: the current state of the action
265 	 *
266 	 * Since: 2.28
267 	 */
268 	public Variant getActionState(string actionName)
269 	{
270 		auto p = g_action_group_get_action_state(getActionGroupStruct(), Str.toStringz(actionName));
271 		
272 		if(p is null)
273 		{
274 			return null;
275 		}
276 		
277 		return new Variant(cast(GVariant*) p, true);
278 	}
279 
280 	/**
281 	 * Requests a hint about the valid range of values for the state of the
282 	 * named action within @action_group.
283 	 *
284 	 * If %NULL is returned it either means that the action is not stateful
285 	 * or that there is no hint about the valid range of values for the
286 	 * state of the action.
287 	 *
288 	 * If a #GVariant array is returned then each item in the array is a
289 	 * possible value for the state.  If a #GVariant pair (ie: two-tuple) is
290 	 * returned then the tuple specifies the inclusive lower and upper bound
291 	 * of valid values for the state.
292 	 *
293 	 * In any case, the information is merely a hint.  It may be possible to
294 	 * have a state value outside of the hinted range and setting a value
295 	 * within the range may fail.
296 	 *
297 	 * The return value (if non-%NULL) should be freed with
298 	 * g_variant_unref() when it is no longer required.
299 	 *
300 	 * Params:
301 	 *     actionName = the name of the action to query
302 	 *
303 	 * Returns: the state range hint
304 	 *
305 	 * Since: 2.28
306 	 */
307 	public Variant getActionStateHint(string actionName)
308 	{
309 		auto p = g_action_group_get_action_state_hint(getActionGroupStruct(), Str.toStringz(actionName));
310 		
311 		if(p is null)
312 		{
313 			return null;
314 		}
315 		
316 		return new Variant(cast(GVariant*) p, true);
317 	}
318 
319 	/**
320 	 * Queries the type of the state of the named action within
321 	 * @action_group.
322 	 *
323 	 * If the action is stateful then this function returns the
324 	 * #GVariantType of the state.  All calls to
325 	 * g_action_group_change_action_state() must give a #GVariant of this
326 	 * type and g_action_group_get_action_state() will return a #GVariant
327 	 * of the same type.
328 	 *
329 	 * If the action is not stateful then this function will return %NULL.
330 	 * In that case, g_action_group_get_action_state() will return %NULL
331 	 * and you must not call g_action_group_change_action_state().
332 	 *
333 	 * The state type of a particular action will never change but it is
334 	 * possible for an action to be removed and for a new action to be added
335 	 * with the same name but a different state type.
336 	 *
337 	 * Params:
338 	 *     actionName = the name of the action to query
339 	 *
340 	 * Returns: the state type, if the action is stateful
341 	 *
342 	 * Since: 2.28
343 	 */
344 	public VariantType getActionStateType(string actionName)
345 	{
346 		auto p = g_action_group_get_action_state_type(getActionGroupStruct(), Str.toStringz(actionName));
347 		
348 		if(p is null)
349 		{
350 			return null;
351 		}
352 		
353 		return new VariantType(cast(GVariantType*) p);
354 	}
355 
356 	/**
357 	 * Checks if the named action exists within @action_group.
358 	 *
359 	 * Params:
360 	 *     actionName = the name of the action to check for
361 	 *
362 	 * Returns: whether the named action exists
363 	 *
364 	 * Since: 2.28
365 	 */
366 	public bool hasAction(string actionName)
367 	{
368 		return g_action_group_has_action(getActionGroupStruct(), Str.toStringz(actionName)) != 0;
369 	}
370 
371 	/**
372 	 * Lists the actions contained within @action_group.
373 	 *
374 	 * The caller is responsible for freeing the list with g_strfreev() when
375 	 * it is no longer required.
376 	 *
377 	 * Returns: a %NULL-terminated array of the names of the
378 	 *     actions in the group
379 	 *
380 	 * Since: 2.28
381 	 */
382 	public string[] listActions()
383 	{
384 		auto retStr = g_action_group_list_actions(getActionGroupStruct());
385 		
386 		scope(exit) Str.freeStringArray(retStr);
387 		return Str.toStringArray(retStr);
388 	}
389 
390 	/**
391 	 * Queries all aspects of the named action within an @action_group.
392 	 *
393 	 * This function acquires the information available from
394 	 * g_action_group_has_action(), g_action_group_get_action_enabled(),
395 	 * g_action_group_get_action_parameter_type(),
396 	 * g_action_group_get_action_state_type(),
397 	 * g_action_group_get_action_state_hint() and
398 	 * g_action_group_get_action_state() with a single function call.
399 	 *
400 	 * This provides two main benefits.
401 	 *
402 	 * The first is the improvement in efficiency that comes with not having
403 	 * to perform repeated lookups of the action in order to discover
404 	 * different things about it.  The second is that implementing
405 	 * #GActionGroup can now be done by only overriding this one virtual
406 	 * function.
407 	 *
408 	 * The interface provides a default implementation of this function that
409 	 * calls the individual functions, as required, to fetch the
410 	 * information.  The interface also provides default implementations of
411 	 * those functions that call this function.  All implementations,
412 	 * therefore, must override either this function or all of the others.
413 	 *
414 	 * If the action exists, %TRUE is returned and any of the requested
415 	 * fields (as indicated by having a non-%NULL reference passed in) are
416 	 * filled.  If the action doesn't exist, %FALSE is returned and the
417 	 * fields may or may not have been modified.
418 	 *
419 	 * Params:
420 	 *     actionName = the name of an action in the group
421 	 *     enabled = if the action is presently enabled
422 	 *     parameterType = the parameter type, or %NULL if none needed
423 	 *     stateType = the state type, or %NULL if stateless
424 	 *     stateHint = the state hint, or %NULL if none
425 	 *     state = the current state, or %NULL if stateless
426 	 *
427 	 * Returns: %TRUE if the action exists, else %FALSE
428 	 *
429 	 * Since: 2.32
430 	 */
431 	public bool queryAction(string actionName, out bool enabled, out VariantType parameterType, out VariantType stateType, out Variant stateHint, out Variant state)
432 	{
433 		int outenabled;
434 		GVariantType* outparameterType = null;
435 		GVariantType* outstateType = null;
436 		GVariant* outstateHint = null;
437 		GVariant* outstate = null;
438 		
439 		auto p = g_action_group_query_action(getActionGroupStruct(), Str.toStringz(actionName), &outenabled, &outparameterType, &outstateType, &outstateHint, &outstate) != 0;
440 		
441 		enabled = (outenabled == 1);
442 		parameterType = new VariantType(outparameterType);
443 		stateType = new VariantType(outstateType);
444 		stateHint = new Variant(outstateHint);
445 		state = new Variant(outstate);
446 		
447 		return p;
448 	}
449 
450 	protected class OnActionAddedDelegateWrapper
451 	{
452 		static OnActionAddedDelegateWrapper[] listeners;
453 		void delegate(string, ActionGroupIF) dlg;
454 		gulong handlerId;
455 		
456 		this(void delegate(string, ActionGroupIF) dlg)
457 		{
458 			this.dlg = dlg;
459 			this.listeners ~= this;
460 		}
461 		
462 		void remove(OnActionAddedDelegateWrapper source)
463 		{
464 			foreach(index, wrapper; listeners)
465 			{
466 				if (wrapper.handlerId == source.handlerId)
467 				{
468 					listeners[index] = null;
469 					listeners = std.algorithm.remove(listeners, index);
470 					break;
471 				}
472 			}
473 		}
474 	}
475 
476 	/**
477 	 * Signals that a new action was just added to the group.
478 	 * This signal is emitted after the action has been added
479 	 * and is now visible.
480 	 *
481 	 * Params:
482 	 *     actionName = the name of the action in @action_group
483 	 *
484 	 * Since: 2.28
485 	 */
486 	gulong addOnActionAdded(void delegate(string, ActionGroupIF) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
487 	{
488 		auto wrapper = new OnActionAddedDelegateWrapper(dlg);
489 		wrapper.handlerId = Signals.connectData(
490 			this,
491 			"action-added",
492 			cast(GCallback)&callBackActionAdded,
493 			cast(void*)wrapper,
494 			cast(GClosureNotify)&callBackActionAddedDestroy,
495 			connectFlags);
496 		return wrapper.handlerId;
497 	}
498 	
499 	extern(C) static void callBackActionAdded(GActionGroup* actiongroupStruct, char* actionName, OnActionAddedDelegateWrapper wrapper)
500 	{
501 		wrapper.dlg(Str.toString(actionName), wrapper.outer);
502 	}
503 	
504 	extern(C) static void callBackActionAddedDestroy(OnActionAddedDelegateWrapper wrapper, GClosure* closure)
505 	{
506 		wrapper.remove(wrapper);
507 	}
508 
509 	protected class OnActionEnabledChangedDelegateWrapper
510 	{
511 		static OnActionEnabledChangedDelegateWrapper[] listeners;
512 		void delegate(string, bool, ActionGroupIF) dlg;
513 		gulong handlerId;
514 		
515 		this(void delegate(string, bool, ActionGroupIF) dlg)
516 		{
517 			this.dlg = dlg;
518 			this.listeners ~= this;
519 		}
520 		
521 		void remove(OnActionEnabledChangedDelegateWrapper source)
522 		{
523 			foreach(index, wrapper; listeners)
524 			{
525 				if (wrapper.handlerId == source.handlerId)
526 				{
527 					listeners[index] = null;
528 					listeners = std.algorithm.remove(listeners, index);
529 					break;
530 				}
531 			}
532 		}
533 	}
534 
535 	/**
536 	 * Signals that the enabled status of the named action has changed.
537 	 *
538 	 * Params:
539 	 *     actionName = the name of the action in @action_group
540 	 *     enabled = whether the action is enabled or not
541 	 *
542 	 * Since: 2.28
543 	 */
544 	gulong addOnActionEnabledChanged(void delegate(string, bool, ActionGroupIF) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
545 	{
546 		auto wrapper = new OnActionEnabledChangedDelegateWrapper(dlg);
547 		wrapper.handlerId = Signals.connectData(
548 			this,
549 			"action-enabled-changed",
550 			cast(GCallback)&callBackActionEnabledChanged,
551 			cast(void*)wrapper,
552 			cast(GClosureNotify)&callBackActionEnabledChangedDestroy,
553 			connectFlags);
554 		return wrapper.handlerId;
555 	}
556 	
557 	extern(C) static void callBackActionEnabledChanged(GActionGroup* actiongroupStruct, char* actionName, bool enabled, OnActionEnabledChangedDelegateWrapper wrapper)
558 	{
559 		wrapper.dlg(Str.toString(actionName), enabled, wrapper.outer);
560 	}
561 	
562 	extern(C) static void callBackActionEnabledChangedDestroy(OnActionEnabledChangedDelegateWrapper wrapper, GClosure* closure)
563 	{
564 		wrapper.remove(wrapper);
565 	}
566 
567 	protected class OnActionRemovedDelegateWrapper
568 	{
569 		static OnActionRemovedDelegateWrapper[] listeners;
570 		void delegate(string, ActionGroupIF) dlg;
571 		gulong handlerId;
572 		
573 		this(void delegate(string, ActionGroupIF) dlg)
574 		{
575 			this.dlg = dlg;
576 			this.listeners ~= this;
577 		}
578 		
579 		void remove(OnActionRemovedDelegateWrapper source)
580 		{
581 			foreach(index, wrapper; listeners)
582 			{
583 				if (wrapper.handlerId == source.handlerId)
584 				{
585 					listeners[index] = null;
586 					listeners = std.algorithm.remove(listeners, index);
587 					break;
588 				}
589 			}
590 		}
591 	}
592 
593 	/**
594 	 * Signals that an action is just about to be removed from the group.
595 	 * This signal is emitted before the action is removed, so the action
596 	 * is still visible and can be queried from the signal handler.
597 	 *
598 	 * Params:
599 	 *     actionName = the name of the action in @action_group
600 	 *
601 	 * Since: 2.28
602 	 */
603 	gulong addOnActionRemoved(void delegate(string, ActionGroupIF) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
604 	{
605 		auto wrapper = new OnActionRemovedDelegateWrapper(dlg);
606 		wrapper.handlerId = Signals.connectData(
607 			this,
608 			"action-removed",
609 			cast(GCallback)&callBackActionRemoved,
610 			cast(void*)wrapper,
611 			cast(GClosureNotify)&callBackActionRemovedDestroy,
612 			connectFlags);
613 		return wrapper.handlerId;
614 	}
615 	
616 	extern(C) static void callBackActionRemoved(GActionGroup* actiongroupStruct, char* actionName, OnActionRemovedDelegateWrapper wrapper)
617 	{
618 		wrapper.dlg(Str.toString(actionName), wrapper.outer);
619 	}
620 	
621 	extern(C) static void callBackActionRemovedDestroy(OnActionRemovedDelegateWrapper wrapper, GClosure* closure)
622 	{
623 		wrapper.remove(wrapper);
624 	}
625 
626 	protected class OnActionStateChangedDelegateWrapper
627 	{
628 		static OnActionStateChangedDelegateWrapper[] listeners;
629 		void delegate(string, Variant, ActionGroupIF) dlg;
630 		gulong handlerId;
631 		
632 		this(void delegate(string, Variant, ActionGroupIF) dlg)
633 		{
634 			this.dlg = dlg;
635 			this.listeners ~= this;
636 		}
637 		
638 		void remove(OnActionStateChangedDelegateWrapper source)
639 		{
640 			foreach(index, wrapper; listeners)
641 			{
642 				if (wrapper.handlerId == source.handlerId)
643 				{
644 					listeners[index] = null;
645 					listeners = std.algorithm.remove(listeners, index);
646 					break;
647 				}
648 			}
649 		}
650 	}
651 
652 	/**
653 	 * Signals that the state of the named action has changed.
654 	 *
655 	 * Params:
656 	 *     actionName = the name of the action in @action_group
657 	 *     value = the new value of the state
658 	 *
659 	 * Since: 2.28
660 	 */
661 	gulong addOnActionStateChanged(void delegate(string, Variant, ActionGroupIF) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
662 	{
663 		auto wrapper = new OnActionStateChangedDelegateWrapper(dlg);
664 		wrapper.handlerId = Signals.connectData(
665 			this,
666 			"action-state-changed",
667 			cast(GCallback)&callBackActionStateChanged,
668 			cast(void*)wrapper,
669 			cast(GClosureNotify)&callBackActionStateChangedDestroy,
670 			connectFlags);
671 		return wrapper.handlerId;
672 	}
673 	
674 	extern(C) static void callBackActionStateChanged(GActionGroup* actiongroupStruct, char* actionName, GVariant* value, OnActionStateChangedDelegateWrapper wrapper)
675 	{
676 		wrapper.dlg(Str.toString(actionName), new Variant(value), wrapper.outer);
677 	}
678 	
679 	extern(C) static void callBackActionStateChangedDestroy(OnActionStateChangedDelegateWrapper wrapper, GClosure* closure)
680 	{
681 		wrapper.remove(wrapper);
682 	}
683 }