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