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.ToggleButton;
26 
27 private import glib.ConstructionException;
28 private import glib.Str;
29 private import gobject.ObjectG;
30 private import gobject.Signals;
31 private import gtk.Button;
32 private import gtk.Widget;
33 private import gtk.c.functions;
34 public  import gtk.c.types;
35 public  import gtkc.gtktypes;
36 private import std.algorithm;
37 
38 
39 /**
40  * A #GtkToggleButton is a #GtkButton which will remain “pressed-in” when
41  * clicked. Clicking again will cause the toggle button to return to its
42  * normal state.
43  * 
44  * A toggle button is created by calling either gtk_toggle_button_new() or
45  * gtk_toggle_button_new_with_label(). If using the former, it is advisable to
46  * pack a widget, (such as a #GtkLabel and/or a #GtkImage), into the toggle
47  * button’s container. (See #GtkButton for more information).
48  * 
49  * The state of a #GtkToggleButton can be set specifically using
50  * gtk_toggle_button_set_active(), and retrieved using
51  * gtk_toggle_button_get_active().
52  * 
53  * To simply switch the state of a toggle button, use gtk_toggle_button_toggled().
54  * 
55  * # CSS nodes
56  * 
57  * GtkToggleButton has a single CSS node with name button. To differentiate
58  * it from a plain #GtkButton, it gets the .toggle style class.
59  * 
60  * ## Creating two #GtkToggleButton widgets.
61  * 
62  * |[<!-- language="C" -->
63  * static void output_state (GtkToggleButton *source, gpointer user_data) {
64  * printf ("Active: %d\n", gtk_toggle_button_get_active (source));
65  * }
66  * 
67  * void make_toggles (void) {
68  * GtkWidget *window, *toggle1, *toggle2;
69  * GtkWidget *box;
70  * const char *text;
71  * 
72  * window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
73  * box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
74  * 
75  * text = "Hi, I’m a toggle button.";
76  * toggle1 = gtk_toggle_button_new_with_label (text);
77  * 
78  * // Makes this toggle button invisible
79  * gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (toggle1),
80  * TRUE);
81  * 
82  * g_signal_connect (toggle1, "toggled",
83  * G_CALLBACK (output_state),
84  * NULL);
85  * gtk_container_add (GTK_CONTAINER (box), toggle1);
86  * 
87  * text = "Hi, I’m a toggle button.";
88  * toggle2 = gtk_toggle_button_new_with_label (text);
89  * gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (toggle2),
90  * FALSE);
91  * g_signal_connect (toggle2, "toggled",
92  * G_CALLBACK (output_state),
93  * NULL);
94  * gtk_container_add (GTK_CONTAINER (box), toggle2);
95  * 
96  * gtk_container_add (GTK_CONTAINER (window), box);
97  * gtk_widget_show_all (window);
98  * }
99  * ]|
100  */
101 public class ToggleButton : Button
102 {
103 	/** the main Gtk struct */
104 	protected GtkToggleButton* gtkToggleButton;
105 
106 	/** Get the main Gtk struct */
107 	public GtkToggleButton* getToggleButtonStruct(bool transferOwnership = false)
108 	{
109 		if (transferOwnership)
110 			ownedRef = false;
111 		return gtkToggleButton;
112 	}
113 
114 	/** the main Gtk struct as a void* */
115 	protected override void* getStruct()
116 	{
117 		return cast(void*)gtkToggleButton;
118 	}
119 
120 	protected override void setStruct(GObject* obj)
121 	{
122 		gtkToggleButton = cast(GtkToggleButton*)obj;
123 		super.setStruct(obj);
124 	}
125 
126 	/**
127 	 * Sets our main struct and passes it to the parent class.
128 	 */
129 	public this (GtkToggleButton* gtkToggleButton, bool ownedRef = false)
130 	{
131 		this.gtkToggleButton = gtkToggleButton;
132 		super(cast(GtkButton*)gtkToggleButton, ownedRef);
133 	}
134 
135 	/**
136 	 * Creates a new toggle button with a text label.
137 	 * Params:
138 	 *  label = a string containing the message to be placed in the toggle button.
139 	 *  mnemonic =  if true the label
140 	 *  will be created using gtk_label_new_with_mnemonic(), so underscores
141 	 *  in label indicate the mnemonic for the button.
142 	 * Throws: ConstructionException GTK+ fails to create the object.
143 	 */
144 	public this (string label, bool mnemonic=true)
145 	{
146 		GtkToggleButton* p;
147 
148 		if ( mnemonic )
149 		{
150 			// GtkWidget* gtk_toggle_button_new_with_mnemonic  (const gchar *label);
151 			p = cast(GtkToggleButton*)gtk_toggle_button_new_with_mnemonic(Str.toStringz(label));
152 		}
153 		else
154 		{
155 			// GtkWidget* gtk_toggle_button_new_with_label  (const gchar *label);
156 			p = cast(GtkToggleButton*)gtk_toggle_button_new_with_label(Str.toStringz(label));
157 		}
158 
159 		if(p is null)
160 		{
161 			throw new ConstructionException("null returned by gtk_toggle_button_new_");
162 		}
163 
164 		this(p);
165 	}
166 
167 	/**
168 	 */
169 
170 	/** */
171 	public static GType getType()
172 	{
173 		return gtk_toggle_button_get_type();
174 	}
175 
176 	/**
177 	 * Creates a new toggle button. A widget should be packed into the button, as in gtk_button_new().
178 	 *
179 	 * Returns: a new toggle button.
180 	 *
181 	 * Throws: ConstructionException GTK+ fails to create the object.
182 	 */
183 	public this()
184 	{
185 		auto p = gtk_toggle_button_new();
186 
187 		if(p is null)
188 		{
189 			throw new ConstructionException("null returned by new");
190 		}
191 
192 		this(cast(GtkToggleButton*) p);
193 	}
194 
195 	/**
196 	 * Queries a #GtkToggleButton and returns its current state. Returns %TRUE if
197 	 * the toggle button is pressed in and %FALSE if it is raised.
198 	 *
199 	 * Returns: a #gboolean value.
200 	 */
201 	public bool getActive()
202 	{
203 		return gtk_toggle_button_get_active(gtkToggleButton) != 0;
204 	}
205 
206 	/**
207 	 * Gets the value set by gtk_toggle_button_set_inconsistent().
208 	 *
209 	 * Returns: %TRUE if the button is displayed as inconsistent, %FALSE otherwise
210 	 */
211 	public bool getInconsistent()
212 	{
213 		return gtk_toggle_button_get_inconsistent(gtkToggleButton) != 0;
214 	}
215 
216 	/**
217 	 * Retrieves whether the button is displayed as a separate indicator
218 	 * and label. See gtk_toggle_button_set_mode().
219 	 *
220 	 * Returns: %TRUE if the togglebutton is drawn as a separate indicator
221 	 *     and label.
222 	 */
223 	public bool getMode()
224 	{
225 		return gtk_toggle_button_get_mode(gtkToggleButton) != 0;
226 	}
227 
228 	/**
229 	 * Sets the status of the toggle button. Set to %TRUE if you want the
230 	 * GtkToggleButton to be “pressed in”, and %FALSE to raise it.
231 	 * This action causes the #GtkToggleButton::toggled signal and the
232 	 * #GtkButton::clicked signal to be emitted.
233 	 *
234 	 * Params:
235 	 *     isActive = %TRUE or %FALSE.
236 	 */
237 	public void setActive(bool isActive)
238 	{
239 		gtk_toggle_button_set_active(gtkToggleButton, isActive);
240 	}
241 
242 	/**
243 	 * If the user has selected a range of elements (such as some text or
244 	 * spreadsheet cells) that are affected by a toggle button, and the
245 	 * current values in that range are inconsistent, you may want to
246 	 * display the toggle in an “in between” state. This function turns on
247 	 * “in between” display.  Normally you would turn off the inconsistent
248 	 * state again if the user toggles the toggle button. This has to be
249 	 * done manually, gtk_toggle_button_set_inconsistent() only affects
250 	 * visual appearance, it doesn’t affect the semantics of the button.
251 	 *
252 	 * Params:
253 	 *     setting = %TRUE if state is inconsistent
254 	 */
255 	public void setInconsistent(bool setting)
256 	{
257 		gtk_toggle_button_set_inconsistent(gtkToggleButton, setting);
258 	}
259 
260 	/**
261 	 * Sets whether the button is displayed as a separate indicator and label.
262 	 * You can call this function on a checkbutton or a radiobutton with
263 	 * @draw_indicator = %FALSE to make the button look like a normal button.
264 	 *
265 	 * This can be used to create linked strip of buttons that work like
266 	 * a #GtkStackSwitcher.
267 	 *
268 	 * This function only affects instances of classes like #GtkCheckButton
269 	 * and #GtkRadioButton that derive from #GtkToggleButton,
270 	 * not instances of #GtkToggleButton itself.
271 	 *
272 	 * Params:
273 	 *     drawIndicator = if %TRUE, draw the button as a separate indicator
274 	 *         and label; if %FALSE, draw the button like a normal button
275 	 */
276 	public void setMode(bool drawIndicator)
277 	{
278 		gtk_toggle_button_set_mode(gtkToggleButton, drawIndicator);
279 	}
280 
281 	/**
282 	 * Emits the #GtkToggleButton::toggled signal on the
283 	 * #GtkToggleButton. There is no good reason for an
284 	 * application ever to call this function.
285 	 */
286 	public void toggled()
287 	{
288 		gtk_toggle_button_toggled(gtkToggleButton);
289 	}
290 
291 	protected class OnToggledDelegateWrapper
292 	{
293 		void delegate(ToggleButton) dlg;
294 		gulong handlerId;
295 
296 		this(void delegate(ToggleButton) dlg)
297 		{
298 			this.dlg = dlg;
299 			onToggledListeners ~= this;
300 		}
301 
302 		void remove(OnToggledDelegateWrapper source)
303 		{
304 			foreach(index, wrapper; onToggledListeners)
305 			{
306 				if (wrapper.handlerId == source.handlerId)
307 				{
308 					onToggledListeners[index] = null;
309 					onToggledListeners = std.algorithm.remove(onToggledListeners, index);
310 					break;
311 				}
312 			}
313 		}
314 	}
315 	OnToggledDelegateWrapper[] onToggledListeners;
316 
317 	/**
318 	 * Should be connected if you wish to perform an action whenever the
319 	 * #GtkToggleButton's state is changed.
320 	 */
321 	gulong addOnToggled(void delegate(ToggleButton) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
322 	{
323 		auto wrapper = new OnToggledDelegateWrapper(dlg);
324 		wrapper.handlerId = Signals.connectData(
325 			this,
326 			"toggled",
327 			cast(GCallback)&callBackToggled,
328 			cast(void*)wrapper,
329 			cast(GClosureNotify)&callBackToggledDestroy,
330 			connectFlags);
331 		return wrapper.handlerId;
332 	}
333 
334 	extern(C) static void callBackToggled(GtkToggleButton* togglebuttonStruct, OnToggledDelegateWrapper wrapper)
335 	{
336 		wrapper.dlg(wrapper.outer);
337 	}
338 
339 	extern(C) static void callBackToggledDestroy(OnToggledDelegateWrapper wrapper, GClosure* closure)
340 	{
341 		wrapper.remove(wrapper);
342 	}
343 }