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.RadioAction;
26 
27 private import glib.ConstructionException;
28 private import glib.ListSG;
29 private import glib.Str;
30 private import gobject.ObjectG;
31 private import gobject.Signals;
32 private import gtk.ToggleAction;
33 private import gtk.c.functions;
34 public  import gtk.c.types;
35 public  import gtkc.gtktypes;
36 private import std.algorithm;
37 
38 
39 /**
40  * A #GtkRadioAction is similar to #GtkRadioMenuItem. A number of radio
41  * actions can be linked together so that only one may be active at any
42  * one time.
43  */
44 public class RadioAction : ToggleAction
45 {
46 	/** the main Gtk struct */
47 	protected GtkRadioAction* gtkRadioAction;
48 
49 	/** Get the main Gtk struct */
50 	public GtkRadioAction* getRadioActionStruct(bool transferOwnership = false)
51 	{
52 		if (transferOwnership)
53 			ownedRef = false;
54 		return gtkRadioAction;
55 	}
56 
57 	/** the main Gtk struct as a void* */
58 	protected override void* getStruct()
59 	{
60 		return cast(void*)gtkRadioAction;
61 	}
62 
63 	protected override void setStruct(GObject* obj)
64 	{
65 		gtkRadioAction = cast(GtkRadioAction*)obj;
66 		super.setStruct(obj);
67 	}
68 
69 	/**
70 	 * Sets our main struct and passes it to the parent class.
71 	 */
72 	public this (GtkRadioAction* gtkRadioAction, bool ownedRef = false)
73 	{
74 		this.gtkRadioAction = gtkRadioAction;
75 		super(cast(GtkToggleAction*)gtkRadioAction, ownedRef);
76 	}
77 
78 	/**
79 	 * Creates a new RadioAction object. To add the action to
80 	 * a ActionGroup and set the accelerator for the action,
81 	 * call gtk.ActionGroup.ActionGroup.addActionWithAccel().
82 	 *
83 	 * Since: 2.4
84 	 *
85 	 * Params:
86 	 *     name    = A unique name for the action
87 	 *     label   = The label displayed in menu items and on buttons, or null
88 	 *     tooltip = A tooltip for this action, or null
89 	 *     stockId = The stock icon to display in widgets representing this
90 	 *               action, or null
91 	 *     value   = The value which getCurrentValue() should
92 	 *               return if this action is selected.
93 	 * Throws: ConstructionException GTK+ fails to create the object.
94 	 */
95 	public this (string name, string label, string tooltip, StockID stockId, int value)
96 	{
97 		this(name, label, tooltip, cast(string)stockId, value);
98 	}
99 
100 	/**
101 	 */
102 
103 	/** */
104 	public static GType getType()
105 	{
106 		return gtk_radio_action_get_type();
107 	}
108 
109 	/**
110 	 * Creates a new #GtkRadioAction object. To add the action to
111 	 * a #GtkActionGroup and set the accelerator for the action,
112 	 * call gtk_action_group_add_action_with_accel().
113 	 *
114 	 * Params:
115 	 *     name = A unique name for the action
116 	 *     label = The label displayed in menu items and on buttons,
117 	 *         or %NULL
118 	 *     tooltip = A tooltip for this action, or %NULL
119 	 *     stockId = The stock icon to display in widgets representing
120 	 *         this action, or %NULL
121 	 *     value = The value which gtk_radio_action_get_current_value() should
122 	 *         return if this action is selected.
123 	 *
124 	 * Returns: a new #GtkRadioAction
125 	 *
126 	 * Since: 2.4
127 	 *
128 	 * Throws: ConstructionException GTK+ fails to create the object.
129 	 */
130 	public this(string name, string label, string tooltip, string stockId, int value)
131 	{
132 		auto p = gtk_radio_action_new(Str.toStringz(name), Str.toStringz(label), Str.toStringz(tooltip), Str.toStringz(stockId), value);
133 
134 		if(p is null)
135 		{
136 			throw new ConstructionException("null returned by new");
137 		}
138 
139 		this(cast(GtkRadioAction*) p, true);
140 	}
141 
142 	/**
143 	 * Obtains the value property of the currently active member of
144 	 * the group to which @action belongs.
145 	 *
146 	 * Returns: The value of the currently active group member
147 	 *
148 	 * Since: 2.4
149 	 */
150 	public int getCurrentValue()
151 	{
152 		return gtk_radio_action_get_current_value(gtkRadioAction);
153 	}
154 
155 	/**
156 	 * Returns the list representing the radio group for this object.
157 	 * Note that the returned list is only valid until the next change
158 	 * to the group.
159 	 *
160 	 * A common way to set up a group of radio group is the following:
161 	 * |[<!-- language="C" -->
162 	 * GSList *group = NULL;
163 	 * GtkRadioAction *action;
164 	 *
165 	 * while ( ...more actions to add... /)
166 	 * {
167 	 * action = gtk_radio_action_new (...);
168 	 *
169 	 * gtk_radio_action_set_group (action, group);
170 	 * group = gtk_radio_action_get_group (action);
171 	 * }
172 	 * ]|
173 	 *
174 	 * Returns: the list representing the radio group for this object
175 	 *
176 	 * Since: 2.4
177 	 */
178 	public ListSG getGroup()
179 	{
180 		auto p = gtk_radio_action_get_group(gtkRadioAction);
181 
182 		if(p is null)
183 		{
184 			return null;
185 		}
186 
187 		return new ListSG(cast(GSList*) p);
188 	}
189 
190 	/**
191 	 * Joins a radio action object to the group of another radio action object.
192 	 *
193 	 * Use this in language bindings instead of the gtk_radio_action_get_group()
194 	 * and gtk_radio_action_set_group() methods
195 	 *
196 	 * A common way to set up a group of radio actions is the following:
197 	 * |[<!-- language="C" -->
198 	 * GtkRadioAction *action;
199 	 * GtkRadioAction *last_action;
200 	 *
201 	 * while ( ...more actions to add... /)
202 	 * {
203 	 * action = gtk_radio_action_new (...);
204 	 *
205 	 * gtk_radio_action_join_group (action, last_action);
206 	 * last_action = action;
207 	 * }
208 	 * ]|
209 	 *
210 	 * Params:
211 	 *     groupSource = a radio action object whos group we are
212 	 *         joining, or %NULL to remove the radio action from its group
213 	 *
214 	 * Since: 3.0
215 	 */
216 	public void joinGroup(RadioAction groupSource)
217 	{
218 		gtk_radio_action_join_group(gtkRadioAction, (groupSource is null) ? null : groupSource.getRadioActionStruct());
219 	}
220 
221 	/**
222 	 * Sets the currently active group member to the member with value
223 	 * property @current_value.
224 	 *
225 	 * Params:
226 	 *     currentValue = the new value
227 	 *
228 	 * Since: 2.10
229 	 */
230 	public void setCurrentValue(int currentValue)
231 	{
232 		gtk_radio_action_set_current_value(gtkRadioAction, currentValue);
233 	}
234 
235 	/**
236 	 * Sets the radio group for the radio action object.
237 	 *
238 	 * Params:
239 	 *     group = a list representing a radio group, or %NULL
240 	 *
241 	 * Since: 2.4
242 	 */
243 	public void setGroup(ListSG group)
244 	{
245 		gtk_radio_action_set_group(gtkRadioAction, (group is null) ? null : group.getListSGStruct());
246 	}
247 
248 	protected class OnChangedDelegateWrapper
249 	{
250 		void delegate(RadioAction, RadioAction) dlg;
251 		gulong handlerId;
252 
253 		this(void delegate(RadioAction, RadioAction) dlg)
254 		{
255 			this.dlg = dlg;
256 			onChangedListeners ~= this;
257 		}
258 
259 		void remove(OnChangedDelegateWrapper source)
260 		{
261 			foreach(index, wrapper; onChangedListeners)
262 			{
263 				if (wrapper.handlerId == source.handlerId)
264 				{
265 					onChangedListeners[index] = null;
266 					onChangedListeners = std.algorithm.remove(onChangedListeners, index);
267 					break;
268 				}
269 			}
270 		}
271 	}
272 	OnChangedDelegateWrapper[] onChangedListeners;
273 
274 	/**
275 	 * The ::changed signal is emitted on every member of a radio group when the
276 	 * active member is changed. The signal gets emitted after the ::activate signals
277 	 * for the previous and current active members.
278 	 *
279 	 * Params:
280 	 *     current = the member of @action's group which has just been activated
281 	 *
282 	 * Since: 2.4
283 	 */
284 	gulong addOnChanged(void delegate(RadioAction, RadioAction) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
285 	{
286 		auto wrapper = new OnChangedDelegateWrapper(dlg);
287 		wrapper.handlerId = Signals.connectData(
288 			this,
289 			"changed",
290 			cast(GCallback)&callBackChanged,
291 			cast(void*)wrapper,
292 			cast(GClosureNotify)&callBackChangedDestroy,
293 			connectFlags);
294 		return wrapper.handlerId;
295 	}
296 
297 	extern(C) static void callBackChanged(GtkRadioAction* radioactionStruct, GtkRadioAction* current, OnChangedDelegateWrapper wrapper)
298 	{
299 		wrapper.dlg(ObjectG.getDObject!(RadioAction)(current), wrapper.outer);
300 	}
301 
302 	extern(C) static void callBackChangedDestroy(OnChangedDelegateWrapper wrapper, GClosure* closure)
303 	{
304 		wrapper.remove(wrapper);
305 	}
306 }