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