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