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.RadioButton;
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.CheckButton;
33 private import gtk.Widget;
34 public  import gtkc.gdktypes;
35 private import gtkc.gtk;
36 public  import gtkc.gtktypes;
37 
38 
39 /**
40  * A single radio button performs the same basic function as a #GtkCheckButton,
41  * as its position in the object hierarchy reflects. It is only when multiple
42  * radio buttons are grouped together that they become a different user
43  * interface component in their own right.
44  * 
45  * Every radio button is a member of some group of radio buttons. When one is
46  * selected, all other radio buttons in the same group are deselected. A
47  * #GtkRadioButton is one way of giving the user a choice from many options.
48  * 
49  * Radio button widgets are created with gtk_radio_button_new(), passing %NULL
50  * as the argument if this is the first radio button in a group. In subsequent
51  * calls, the group you wish to add this button to should be passed as an
52  * argument. Optionally, gtk_radio_button_new_with_label() can be used if you
53  * want a text label on the radio button.
54  * 
55  * Alternatively, when adding widgets to an existing group of radio buttons,
56  * use gtk_radio_button_new_from_widget() with a #GtkRadioButton that already
57  * has a group assigned to it. The convenience function
58  * gtk_radio_button_new_with_label_from_widget() is also provided.
59  * 
60  * To retrieve the group a #GtkRadioButton is assigned to, use
61  * gtk_radio_button_get_group().
62  * 
63  * To remove a #GtkRadioButton from one group and make it part of a new one,
64  * use gtk_radio_button_set_group().
65  * 
66  * The group list does not need to be freed, as each #GtkRadioButton will remove
67  * itself and its list item when it is destroyed.
68  * 
69  * ## How to create a group of two radio buttons.
70  * 
71  * |[<!-- language="C" -->
72  * void create_radio_buttons (void) {
73  * 
74  * GtkWidget *window, *radio1, *radio2, *box, *entry;
75  * window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
76  * box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 2);
77  * gtk_box_set_homogeneous (GTK_BOX (box), TRUE);
78  * 
79  * // Create a radio button with a GtkEntry widget
80  * radio1 = gtk_radio_button_new (NULL);
81  * entry = gtk_entry_new ();
82  * gtk_container_add (GTK_CONTAINER (radio1), entry);
83  * 
84  * 
85  * // Create a radio button with a label
86  * radio2 = gtk_radio_button_new_with_label_from_widget (GTK_RADIO_BUTTON (radio1),
87  * "I’m the second radio button.");
88  * 
89  * // Pack them into a box, then show all the widgets
90  * gtk_box_pack_start (GTK_BOX (box), radio1, TRUE, TRUE, 2);
91  * gtk_box_pack_start (GTK_BOX (box), radio2, TRUE, TRUE, 2);
92  * gtk_container_add (GTK_CONTAINER (window), box);
93  * gtk_widget_show_all (window);
94  * return;
95  * }
96  * ]|
97  * 
98  * When an unselected button in the group is clicked the clicked button
99  * receives the #GtkToggleButton::toggled signal, as does the previously
100  * selected button.
101  * Inside the #GtkToggleButton::toggled handler, gtk_toggle_button_get_active()
102  * can be used to determine if the button has been selected or deselected.
103  */
104 public class RadioButton : CheckButton
105 {
106 	/** the main Gtk struct */
107 	protected GtkRadioButton* gtkRadioButton;
108 
109 	/** Get the main Gtk struct */
110 	public GtkRadioButton* getRadioButtonStruct()
111 	{
112 		return gtkRadioButton;
113 	}
114 
115 	/** the main Gtk struct as a void* */
116 	protected override void* getStruct()
117 	{
118 		return cast(void*)gtkRadioButton;
119 	}
120 
121 	protected override void setStruct(GObject* obj)
122 	{
123 		gtkRadioButton = cast(GtkRadioButton*)obj;
124 		super.setStruct(obj);
125 	}
126 
127 	/**
128 	 * Sets our main struct and passes it to the parent class.
129 	 */
130 	public this (GtkRadioButton* gtkRadioButton, bool ownedRef = false)
131 	{
132 		this.gtkRadioButton = gtkRadioButton;
133 		super(cast(GtkCheckButton*)gtkRadioButton, ownedRef);
134 	}
135 
136 	/**
137 	 * Creates a new RadioButton with a text label.
138 	 * Params:
139 	 *  group = an existing radio button group.
140 	 *  label = the text label to display next to the radio button.
141 	 *  mnemonic = if true the label will be created using
142 	 *  gtk_label_new_with_mnemonic(), so underscores in label indicate the
143 	 *  mnemonic for the button.
144 	 * Throws: ConstructionException GTK+ fails to create the object.
145 	 */
146 	public this (ListSG group, string label, bool mnemonic=true)
147 	{
148 		GtkRadioButton* p;
149 		
150 		if ( mnemonic )
151 		{
152 			// GtkWidget* gtk_radio_button_new_with_mnemonic  (GSList *group,  const gchar *label);
153 			p = cast(GtkRadioButton*)gtk_radio_button_new_with_mnemonic(
154 				group is null ? null : group.getListSGStruct(),
155 			Str.toStringz(label));
156 		}
157 		else
158 		{
159 			// GtkWidget* gtk_radio_button_new_with_label (GSList *group,  const gchar *label);
160 			p = cast(GtkRadioButton*)gtk_radio_button_new_with_label(
161 				group is null ? null : group.getListSGStruct(),
162 			Str.toStringz(label));
163 		}
164 		
165 		if(p is null)
166 		{
167 			throw new ConstructionException("null returned by gtk_radio_button_new_");
168 		}
169 		
170 		this(p);
171 	}
172 	
173 	/**
174 	 * Creates a new RadioButton with a text label, adding it to the same group
175 	 * as group.
176 	 * Params:
177 	 *  radioButton = an existing RadioButton.
178 	 *  label = a text string to display next to the radio button.
179 	 *  mnemonic = if true the label
180 	 *  will be created using gtk_label_new_with_mnemonic(), so underscores
181 	 *  in label indicate the mnemonic for the button.
182 	 * Throws: ConstructionException GTK+ fails to create the object.
183 	 */
184 	public this (RadioButton radioButton, string label, bool mnemonic=true)
185 	{
186 		GtkRadioButton* p;
187 		
188 		if ( mnemonic )
189 		{
190 			// GtkWidget* gtk_radio_button_new_with_mnemonic_from_widget  (GtkRadioButton *group,  const gchar *label);
191 			p = cast(GtkRadioButton*)gtk_radio_button_new_with_mnemonic_from_widget(
192 				radioButton.getRadioButtonStruct(),
193 			Str.toStringz(label));
194 		}
195 		else
196 		{
197 			// GtkWidget* gtk_radio_button_new_with_label_from_widget  (GtkRadioButton *group,  const gchar *label);
198 			p = cast(GtkRadioButton*)gtk_radio_button_new_with_label_from_widget(
199 				radioButton.getRadioButtonStruct(),
200 			Str.toStringz(label));
201 		}
202 		
203 		if(p is null)
204 		{
205 			throw new ConstructionException("null returned by gtk_radio_button_new_");
206 		}
207 		
208 		this(p);
209 	}
210 	
211 	/**
212 	 * Creates a new RadioButton with a text label,
213 	 * and creates a new group.
214 	 * Params:
215 	 *  label = a text string to display next to the radio button.
216 	 *  mnemonic = if true the label
217 	 *  will be created using gtk_label_new_with_mnemonic(), so underscores
218 	 *  in label indicate the mnemonic for the button.
219 	 * Throws: ConstructionException GTK+ fails to create the object.
220 	 */
221 	public this (string label, bool mnemonic=true)
222 	{
223 		this(cast(ListSG)null, label, mnemonic);
224 	}
225 
226 	/**
227 	 */
228 
229 	public static GType getType()
230 	{
231 		return gtk_radio_button_get_type();
232 	}
233 
234 	/**
235 	 * Creates a new #GtkRadioButton. To be of any practical value, a widget should
236 	 * then be packed into the radio button.
237 	 *
238 	 * Params:
239 	 *     group = an existing
240 	 *         radio button group, or %NULL if you are creating a new group.
241 	 *
242 	 * Return: a new radio button
243 	 *
244 	 * Throws: ConstructionException GTK+ fails to create the object.
245 	 */
246 	public this(ListSG group)
247 	{
248 		auto p = gtk_radio_button_new((group is null) ? null : group.getListSGStruct());
249 		
250 		if(p is null)
251 		{
252 			throw new ConstructionException("null returned by new");
253 		}
254 		
255 		this(cast(GtkRadioButton*) p);
256 	}
257 
258 	/**
259 	 * Creates a new #GtkRadioButton, adding it to the same group as
260 	 * @radio_group_member. As with gtk_radio_button_new(), a widget
261 	 * should be packed into the radio button.
262 	 *
263 	 * Params:
264 	 *     radioGroupMember = an existing #GtkRadioButton.
265 	 *
266 	 * Return: a new radio button.
267 	 *
268 	 * Throws: ConstructionException GTK+ fails to create the object.
269 	 */
270 	public this(RadioButton radioGroupMember)
271 	{
272 		auto p = gtk_radio_button_new_from_widget((radioGroupMember is null) ? null : radioGroupMember.getRadioButtonStruct());
273 		
274 		if(p is null)
275 		{
276 			throw new ConstructionException("null returned by new_from_widget");
277 		}
278 		
279 		this(cast(GtkRadioButton*) p);
280 	}
281 
282 	/**
283 	 * Retrieves the group assigned to a radio button.
284 	 *
285 	 * Return: a linked list
286 	 *     containing all the radio buttons in the same group
287 	 *     as @radio_button. The returned list is owned by the radio button
288 	 *     and must not be modified or freed.
289 	 */
290 	public ListSG getGroup()
291 	{
292 		auto p = gtk_radio_button_get_group(gtkRadioButton);
293 		
294 		if(p is null)
295 		{
296 			return null;
297 		}
298 		
299 		return new ListSG(cast(GSList*) p);
300 	}
301 
302 	/**
303 	 * Joins a #GtkRadioButton object to the group of another #GtkRadioButton object
304 	 *
305 	 * Use this in language bindings instead of the gtk_radio_button_get_group()
306 	 * and gtk_radio_button_set_group() methods
307 	 *
308 	 * A common way to set up a group of radio buttons is the following:
309 	 * |[<!-- language="C" -->
310 	 * GtkRadioButton *radio_button;
311 	 * GtkRadioButton *last_button;
312 	 *
313 	 * while ( ...more buttons to add... )
314 	 * {
315 	 * radio_button = gtk_radio_button_new (...);
316 	 *
317 	 * gtk_radio_button_join_group (radio_button, last_button);
318 	 * last_button = radio_button;
319 	 * }
320 	 * ]|
321 	 *
322 	 * Params:
323 	 *     groupSource = a radio button object whos group we are
324 	 *         joining, or %NULL to remove the radio button from its group
325 	 *
326 	 * Since: 3.0
327 	 */
328 	public void joinGroup(RadioButton groupSource)
329 	{
330 		gtk_radio_button_join_group(gtkRadioButton, (groupSource is null) ? null : groupSource.getRadioButtonStruct());
331 	}
332 
333 	/**
334 	 * Sets a #GtkRadioButton’s group. It should be noted that this does not change
335 	 * the layout of your interface in any way, so if you are changing the group,
336 	 * it is likely you will need to re-arrange the user interface to reflect these
337 	 * changes.
338 	 *
339 	 * Params:
340 	 *     group = an existing radio
341 	 *         button group, such as one returned from gtk_radio_button_get_group(), or %NULL.
342 	 */
343 	public void setGroup(ListSG group)
344 	{
345 		gtk_radio_button_set_group(gtkRadioButton, (group is null) ? null : group.getListSGStruct());
346 	}
347 
348 	int[string] connectedSignals;
349 
350 	void delegate(RadioButton)[] onGroupChangedListeners;
351 	/**
352 	 * Emitted when the group of radio buttons that a radio button belongs
353 	 * to changes. This is emitted when a radio button switches from
354 	 * being alone to being part of a group of 2 or more buttons, or
355 	 * vice-versa, and when a button is moved from one group of 2 or
356 	 * more buttons to a different one, but not when the composition
357 	 * of the group that a button belongs to changes.
358 	 *
359 	 * Since: 2.4
360 	 */
361 	void addOnGroupChanged(void delegate(RadioButton) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
362 	{
363 		if ( "group-changed" !in connectedSignals )
364 		{
365 			Signals.connectData(
366 				this,
367 				"group-changed",
368 				cast(GCallback)&callBackGroupChanged,
369 				cast(void*)this,
370 				null,
371 				connectFlags);
372 			connectedSignals["group-changed"] = 1;
373 		}
374 		onGroupChangedListeners ~= dlg;
375 	}
376 	extern(C) static void callBackGroupChanged(GtkRadioButton* radiobuttonStruct, RadioButton _radiobutton)
377 	{
378 		foreach ( void delegate(RadioButton) dlg; _radiobutton.onGroupChangedListeners )
379 		{
380 			dlg(_radiobutton);
381 		}
382 	}
383 }