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