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