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.CheckMenuItem;
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.MenuItem;
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 #GtkCheckMenuItem is a menu item that maintains the state of a boolean
40  * value in addition to a #GtkMenuItem usual role in activating application
41  * code.
42  * 
43  * A check box indicating the state of the boolean value is displayed
44  * at the left side of the #GtkMenuItem.  Activating the #GtkMenuItem
45  * toggles the value.
46  * 
47  * # CSS nodes
48  * 
49  * |[<!-- language="plain" -->
50  * menuitem
51  * ├── check.left
52  * ╰── <child>
53  * ]|
54  * 
55  * GtkCheckMenuItem has a main CSS node with name menuitem, and a subnode
56  * with name check, which gets the .left or .right style class.
57  */
58 public class CheckMenuItem : MenuItem
59 {
60 	/** the main Gtk struct */
61 	protected GtkCheckMenuItem* gtkCheckMenuItem;
62 
63 	/** Get the main Gtk struct */
64 	public GtkCheckMenuItem* getCheckMenuItemStruct(bool transferOwnership = false)
65 	{
66 		if (transferOwnership)
67 			ownedRef = false;
68 		return gtkCheckMenuItem;
69 	}
70 
71 	/** the main Gtk struct as a void* */
72 	protected override void* getStruct()
73 	{
74 		return cast(void*)gtkCheckMenuItem;
75 	}
76 
77 	protected override void setStruct(GObject* obj)
78 	{
79 		gtkCheckMenuItem = cast(GtkCheckMenuItem*)obj;
80 		super.setStruct(obj);
81 	}
82 
83 	/**
84 	 * Sets our main struct and passes it to the parent class.
85 	 */
86 	public this (GtkCheckMenuItem* gtkCheckMenuItem, bool ownedRef = false)
87 	{
88 		this.gtkCheckMenuItem = gtkCheckMenuItem;
89 		super(cast(GtkMenuItem*)gtkCheckMenuItem, ownedRef);
90 	}
91 
92 	/**
93 	 * Creates a new GtkCheckMenuItem with a label.
94 	 * Params:
95 	 *  label = the string to use for the label.
96 	 *  mnemonic = if true the label
97 	 *  will be created using gtk_label_new_with_mnemonic(), so underscores
98 	 *  in label indicate the mnemonic for the menu item.
99 	 * Throws: ConstructionException GTK+ fails to create the object.
100 	 */
101 	public this (string label, bool mnemonic=true)
102 	{
103 		GtkCheckMenuItem* p;
104 		
105 		if ( mnemonic )
106 		{
107 			// GtkWidget* gtk_check_menu_item_new_with_mnemonic  (const gchar *label);
108 			p = cast(GtkCheckMenuItem*)gtk_check_menu_item_new_with_mnemonic(Str.toStringz(label));
109 		}
110 		else
111 		{
112 			// GtkWidget* gtk_check_menu_item_new_with_label  (const gchar *label);
113 			p = cast(GtkCheckMenuItem*)gtk_check_menu_item_new_with_label(Str.toStringz(label));
114 		}
115 		
116 		if(p is null)
117 		{
118 			throw new ConstructionException("null returned by gtk_check_menu_item_new_with_");
119 		}
120 		
121 		this(p);
122 	}
123 
124 	/**
125 	 */
126 
127 	/** */
128 	public static GType getType()
129 	{
130 		return gtk_check_menu_item_get_type();
131 	}
132 
133 	/**
134 	 * Creates a new #GtkCheckMenuItem.
135 	 *
136 	 * Returns: a new #GtkCheckMenuItem.
137 	 *
138 	 * Throws: ConstructionException GTK+ fails to create the object.
139 	 */
140 	public this()
141 	{
142 		auto p = gtk_check_menu_item_new();
143 		
144 		if(p is null)
145 		{
146 			throw new ConstructionException("null returned by new");
147 		}
148 		
149 		this(cast(GtkCheckMenuItem*) p);
150 	}
151 
152 	/**
153 	 * Returns whether the check menu item is active. See
154 	 * gtk_check_menu_item_set_active ().
155 	 *
156 	 * Returns: %TRUE if the menu item is checked.
157 	 */
158 	public bool getActive()
159 	{
160 		return gtk_check_menu_item_get_active(gtkCheckMenuItem) != 0;
161 	}
162 
163 	/**
164 	 * Returns whether @check_menu_item looks like a #GtkRadioMenuItem
165 	 *
166 	 * Returns: Whether @check_menu_item looks like a #GtkRadioMenuItem
167 	 *
168 	 * Since: 2.4
169 	 */
170 	public bool getDrawAsRadio()
171 	{
172 		return gtk_check_menu_item_get_draw_as_radio(gtkCheckMenuItem) != 0;
173 	}
174 
175 	/**
176 	 * Retrieves the value set by gtk_check_menu_item_set_inconsistent().
177 	 *
178 	 * Returns: %TRUE if inconsistent
179 	 */
180 	public bool getInconsistent()
181 	{
182 		return gtk_check_menu_item_get_inconsistent(gtkCheckMenuItem) != 0;
183 	}
184 
185 	/**
186 	 * Sets the active state of the menu item’s check box.
187 	 *
188 	 * Params:
189 	 *     isActive = boolean value indicating whether the check box is active.
190 	 */
191 	public void setActive(bool isActive)
192 	{
193 		gtk_check_menu_item_set_active(gtkCheckMenuItem, isActive);
194 	}
195 
196 	/**
197 	 * Sets whether @check_menu_item is drawn like a #GtkRadioMenuItem
198 	 *
199 	 * Params:
200 	 *     drawAsRadio = whether @check_menu_item is drawn like a #GtkRadioMenuItem
201 	 *
202 	 * Since: 2.4
203 	 */
204 	public void setDrawAsRadio(bool drawAsRadio)
205 	{
206 		gtk_check_menu_item_set_draw_as_radio(gtkCheckMenuItem, drawAsRadio);
207 	}
208 
209 	/**
210 	 * If the user has selected a range of elements (such as some text or
211 	 * spreadsheet cells) that are affected by a boolean setting, and the
212 	 * current values in that range are inconsistent, you may want to
213 	 * display the check in an “in between” state. This function turns on
214 	 * “in between” display.  Normally you would turn off the inconsistent
215 	 * state again if the user explicitly selects a setting. This has to be
216 	 * done manually, gtk_check_menu_item_set_inconsistent() only affects
217 	 * visual appearance, it doesn’t affect the semantics of the widget.
218 	 *
219 	 * Params:
220 	 *     setting = %TRUE to display an “inconsistent” third state check
221 	 */
222 	public void setInconsistent(bool setting)
223 	{
224 		gtk_check_menu_item_set_inconsistent(gtkCheckMenuItem, setting);
225 	}
226 
227 	/**
228 	 * Emits the #GtkCheckMenuItem::toggled signal.
229 	 */
230 	public void toggled()
231 	{
232 		gtk_check_menu_item_toggled(gtkCheckMenuItem);
233 	}
234 
235 	protected class OnToggledDelegateWrapper
236 	{
237 		static OnToggledDelegateWrapper[] listeners;
238 		void delegate(CheckMenuItem) dlg;
239 		gulong handlerId;
240 		
241 		this(void delegate(CheckMenuItem) dlg)
242 		{
243 			this.dlg = dlg;
244 			this.listeners ~= this;
245 		}
246 		
247 		void remove(OnToggledDelegateWrapper source)
248 		{
249 			foreach(index, wrapper; listeners)
250 			{
251 				if (wrapper.handlerId == source.handlerId)
252 				{
253 					listeners[index] = null;
254 					listeners = std.algorithm.remove(listeners, index);
255 					break;
256 				}
257 			}
258 		}
259 	}
260 
261 	/**
262 	 * This signal is emitted when the state of the check box is changed.
263 	 *
264 	 * A signal handler can use gtk_check_menu_item_get_active()
265 	 * to discover the new state.
266 	 */
267 	gulong addOnToggled(void delegate(CheckMenuItem) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
268 	{
269 		auto wrapper = new OnToggledDelegateWrapper(dlg);
270 		wrapper.handlerId = Signals.connectData(
271 			this,
272 			"toggled",
273 			cast(GCallback)&callBackToggled,
274 			cast(void*)wrapper,
275 			cast(GClosureNotify)&callBackToggledDestroy,
276 			connectFlags);
277 		return wrapper.handlerId;
278 	}
279 	
280 	extern(C) static void callBackToggled(GtkCheckMenuItem* checkmenuitemStruct, OnToggledDelegateWrapper wrapper)
281 	{
282 		wrapper.dlg(wrapper.outer);
283 	}
284 	
285 	extern(C) static void callBackToggledDestroy(OnToggledDelegateWrapper wrapper, GClosure* closure)
286 	{
287 		wrapper.remove(wrapper);
288 	}
289 }