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