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