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