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