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.RadioMenuItem;
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.CheckMenuItem;
33 private import gtk.Widget;
34 public  import gtkc.gdktypes;
35 private import gtkc.gtk;
36 public  import gtkc.gtktypes;
37 private import std.algorithm;
38 
39 
40 /**
41  * A radio menu item is a check menu item that belongs to a group. At each
42  * instant exactly one of the radio menu items from a group is selected.
43  * 
44  * The group list does not need to be freed, as each #GtkRadioMenuItem will
45  * remove itself and its list item when it is destroyed.
46  * 
47  * The correct way to create a group of radio menu items is approximatively
48  * this:
49  * 
50  * ## How to create a group of radio menu items.
51  * 
52  * |[<!-- language="C" -->
53  * GSList *group = NULL;
54  * GtkWidget *item;
55  * gint i;
56  * 
57  * for (i = 0; i < 5; i++)
58  * {
59  * item = gtk_radio_menu_item_new_with_label (group, "This is an example");
60  * group = gtk_radio_menu_item_get_group (GTK_RADIO_MENU_ITEM (item));
61  * if (i == 1)
62  * gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (item), TRUE);
63  * }
64  * ]|
65  * 
66  * # CSS nodes
67  * 
68  * |[<!-- language="plain" -->
69  * menuitem
70  * ├── radio.left
71  * ╰── <child>
72  * ]|
73  * 
74  * GtkRadioMenuItem has a main CSS node with name menuitem, and a subnode
75  * with name radio, which gets the .left or .right style class.
76  */
77 public class RadioMenuItem : CheckMenuItem
78 {
79 	/** the main Gtk struct */
80 	protected GtkRadioMenuItem* gtkRadioMenuItem;
81 
82 	/** Get the main Gtk struct */
83 	public GtkRadioMenuItem* getRadioMenuItemStruct()
84 	{
85 		return gtkRadioMenuItem;
86 	}
87 
88 	/** the main Gtk struct as a void* */
89 	protected override void* getStruct()
90 	{
91 		return cast(void*)gtkRadioMenuItem;
92 	}
93 
94 	protected override void setStruct(GObject* obj)
95 	{
96 		gtkRadioMenuItem = cast(GtkRadioMenuItem*)obj;
97 		super.setStruct(obj);
98 	}
99 
100 	/**
101 	 * Sets our main struct and passes it to the parent class.
102 	 */
103 	public this (GtkRadioMenuItem* gtkRadioMenuItem, bool ownedRef = false)
104 	{
105 		this.gtkRadioMenuItem = gtkRadioMenuItem;
106 		super(cast(GtkCheckMenuItem*)gtkRadioMenuItem, ownedRef);
107 	}
108 
109 	/**
110 	 * Creates a new GtkRadioMenuItem whose child is a simple GtkLabel.
111 	 * The new GtkRadioMenuItem is added to the same group as group.
112 	 * If mnemonic is true the label will be
113 	 * created using gtk_label_new_with_mnemonic(), so underscores in label
114 	 * indicate the mnemonic for the menu item.
115 	 * Since 2.4
116 	 * Params:
117 	 *  group = an existing GtkRadioMenuItem
118 	 *  label = the text for the label
119 	 * Throws: ConstructionException GTK+ fails to create the object.
120 	 */
121 	public this (RadioMenuItem radioMenuItem, string label, bool mnemonic=true)
122 	{
123 		GtkRadioMenuItem* p;
124 		
125 		if ( mnemonic )
126 		{
127 			// GtkWidget* gtk_radio_menu_item_new_with_mnemonic_from_widget  (GtkRadioMenuItem *group,  const gchar *label);
128 			p = cast(GtkRadioMenuItem*)gtk_radio_menu_item_new_with_mnemonic_from_widget(
129 			radioMenuItem.getRadioMenuItemStruct(), Str.toStringz(label));
130 		}
131 		else
132 		{
133 			// GtkWidget* gtk_radio_menu_item_new_with_label_from_widget  (GtkRadioMenuItem *group,  const gchar *label);
134 			p = cast(GtkRadioMenuItem*)gtk_radio_menu_item_new_with_label_from_widget(
135 			radioMenuItem.getRadioMenuItemStruct(), Str.toStringz(label));
136 		}
137 		
138 		if(p is null)
139 		{
140 			throw new ConstructionException("null returned by gtk_radio_menu_item_new_with_");
141 		}
142 		
143 		this(p);
144 	}
145 	
146 	/**
147 	 * Creates a new GtkRadioMenuItem whose child is a simple GtkLabel.
148 	 * Params:
149 	 *  group = the group to which the radio menu item is to be attached
150 	 *  label = the text for the label
151 	 *  mnemonic = if true the label
152 	 *  will be created using gtk_label_new_with_mnemonic(), so underscores
153 	 *  in label indicate the mnemonic for the menu item.
154 	 * Throws: ConstructionException GTK+ fails to create the object.
155 	 */
156 	public this (ListSG group, string label, bool mnemonic=true)
157 	{
158 		GtkRadioMenuItem* p;
159 		
160 		if ( mnemonic )
161 		{
162 			// GtkWidget* gtk_radio_menu_item_new_with_mnemonic  (GSList *group,  const gchar *label);
163 			p = cast(GtkRadioMenuItem*)gtk_radio_menu_item_new_with_mnemonic(
164 			group is null ? null : group.getListSGStruct(), Str.toStringz(label));
165 		}
166 		else
167 		{
168 			// GtkWidget* gtk_radio_menu_item_new_with_label  (GSList *group,  const gchar *label);
169 			p = cast(GtkRadioMenuItem*)gtk_radio_menu_item_new_with_label(
170 			group is null ? null : group.getListSGStruct(), Str.toStringz(label));
171 		}
172 		
173 		if(p is null)
174 		{
175 			throw new ConstructionException("null returned by gtk_radio_menu_item_new_with_");
176 		}
177 		
178 		this(p);
179 	}
180 
181 	/**
182 	 */
183 
184 	/** */
185 	public static GType getType()
186 	{
187 		return gtk_radio_menu_item_get_type();
188 	}
189 
190 	/**
191 	 * Creates a new #GtkRadioMenuItem.
192 	 *
193 	 * Params:
194 	 *     group = the group to which the
195 	 *         radio menu item is to be attached, or %NULL
196 	 *
197 	 * Return: a new #GtkRadioMenuItem
198 	 *
199 	 * Throws: ConstructionException GTK+ fails to create the object.
200 	 */
201 	public this(ListSG group)
202 	{
203 		auto p = gtk_radio_menu_item_new((group is null) ? null : group.getListSGStruct());
204 		
205 		if(p is null)
206 		{
207 			throw new ConstructionException("null returned by new");
208 		}
209 		
210 		this(cast(GtkRadioMenuItem*) p);
211 	}
212 
213 	/**
214 	 * Creates a new #GtkRadioMenuItem adding it to the same group as @group.
215 	 *
216 	 * Params:
217 	 *     group = An existing #GtkRadioMenuItem
218 	 *
219 	 * Return: The new #GtkRadioMenuItem
220 	 *
221 	 * Since: 2.4
222 	 *
223 	 * Throws: ConstructionException GTK+ fails to create the object.
224 	 */
225 	public this(RadioMenuItem group)
226 	{
227 		auto p = gtk_radio_menu_item_new_from_widget((group is null) ? null : group.getRadioMenuItemStruct());
228 		
229 		if(p is null)
230 		{
231 			throw new ConstructionException("null returned by new_from_widget");
232 		}
233 		
234 		this(cast(GtkRadioMenuItem*) p);
235 	}
236 
237 	/**
238 	 * Returns the group to which the radio menu item belongs, as a #GList of
239 	 * #GtkRadioMenuItem. The list belongs to GTK+ and should not be freed.
240 	 *
241 	 * Return: the group
242 	 *     of @radio_menu_item
243 	 */
244 	public ListSG getGroup()
245 	{
246 		auto p = gtk_radio_menu_item_get_group(gtkRadioMenuItem);
247 		
248 		if(p is null)
249 		{
250 			return null;
251 		}
252 		
253 		return new ListSG(cast(GSList*) p);
254 	}
255 
256 	/**
257 	 * Joins a #GtkRadioMenuItem object to the group of another #GtkRadioMenuItem
258 	 * object.
259 	 *
260 	 * This function should be used by language bindings to avoid the memory
261 	 * manangement of the opaque #GSList of gtk_radio_menu_item_get_group()
262 	 * and gtk_radio_menu_item_set_group().
263 	 *
264 	 * A common way to set up a group of #GtkRadioMenuItem instances is:
265 	 *
266 	 * |[
267 	 * GtkRadioMenuItem *last_item = NULL;
268 	 *
269 	 * while ( ...more items to add... )
270 	 * {
271 	 * GtkRadioMenuItem *radio_item;
272 	 *
273 	 * radio_item = gtk_radio_menu_item_new (...);
274 	 *
275 	 * gtk_radio_menu_item_join_group (radio_item, last_item);
276 	 * last_item = radio_item;
277 	 * }
278 	 * ]|
279 	 *
280 	 * Params:
281 	 *     groupSource = a #GtkRadioMenuItem whose group we are
282 	 *         joining, or %NULL to remove the @radio_menu_item from its current
283 	 *         group
284 	 *
285 	 * Since: 3.18
286 	 */
287 	public void joinGroup(RadioMenuItem groupSource)
288 	{
289 		gtk_radio_menu_item_join_group(gtkRadioMenuItem, (groupSource is null) ? null : groupSource.getRadioMenuItemStruct());
290 	}
291 
292 	/**
293 	 * Sets the group of a radio menu item, or changes it.
294 	 *
295 	 * Params:
296 	 *     group = the new group, or %NULL.
297 	 */
298 	public void setGroup(ListSG group)
299 	{
300 		gtk_radio_menu_item_set_group(gtkRadioMenuItem, (group is null) ? null : group.getListSGStruct());
301 	}
302 
303 	protected class OnGroupChangedDelegateWrapper
304 	{
305 		void delegate(RadioMenuItem) dlg;
306 		gulong handlerId;
307 		ConnectFlags flags;
308 		this(void delegate(RadioMenuItem) dlg, gulong handlerId, ConnectFlags flags)
309 		{
310 			this.dlg = dlg;
311 			this.handlerId = handlerId;
312 			this.flags = flags;
313 		}
314 	}
315 	protected OnGroupChangedDelegateWrapper[] onGroupChangedListeners;
316 
317 	/** */
318 	gulong addOnGroupChanged(void delegate(RadioMenuItem) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
319 	{
320 		onGroupChangedListeners ~= new OnGroupChangedDelegateWrapper(dlg, 0, connectFlags);
321 		onGroupChangedListeners[onGroupChangedListeners.length - 1].handlerId = Signals.connectData(
322 			this,
323 			"group-changed",
324 			cast(GCallback)&callBackGroupChanged,
325 			cast(void*)onGroupChangedListeners[onGroupChangedListeners.length - 1],
326 			cast(GClosureNotify)&callBackGroupChangedDestroy,
327 			connectFlags);
328 		return onGroupChangedListeners[onGroupChangedListeners.length - 1].handlerId;
329 	}
330 	
331 	extern(C) static void callBackGroupChanged(GtkRadioMenuItem* radiomenuitemStruct,OnGroupChangedDelegateWrapper wrapper)
332 	{
333 		wrapper.dlg(wrapper.outer);
334 	}
335 	
336 	extern(C) static void callBackGroupChangedDestroy(OnGroupChangedDelegateWrapper wrapper, GClosure* closure)
337 	{
338 		wrapper.outer.internalRemoveOnGroupChanged(wrapper);
339 	}
340 
341 	protected void internalRemoveOnGroupChanged(OnGroupChangedDelegateWrapper source)
342 	{
343 		foreach(index, wrapper; onGroupChangedListeners)
344 		{
345 			if (wrapper.dlg == source.dlg && wrapper.flags == source.flags && wrapper.handlerId == source.handlerId)
346 			{
347 				onGroupChangedListeners[index] = null;
348 				onGroupChangedListeners = std.algorithm.remove(onGroupChangedListeners, index);
349 				break;
350 			}
351 		}
352 	}
353 	
354 }