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.ToggleAction;
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.Action;
32 public  import gtkc.gdktypes;
33 private import gtkc.gtk;
34 public  import gtkc.gtktypes;
35 private import std.algorithm;
36 
37 
38 /**
39  * A #GtkToggleAction corresponds roughly to a #GtkCheckMenuItem. It has an
40  * “active” state specifying whether the action has been checked or not.
41  */
42 public class ToggleAction : Action
43 {
44 	/** the main Gtk struct */
45 	protected GtkToggleAction* gtkToggleAction;
46 
47 	/** Get the main Gtk struct */
48 	public GtkToggleAction* getToggleActionStruct()
49 	{
50 		return gtkToggleAction;
51 	}
52 
53 	/** the main Gtk struct as a void* */
54 	protected override void* getStruct()
55 	{
56 		return cast(void*)gtkToggleAction;
57 	}
58 
59 	protected override void setStruct(GObject* obj)
60 	{
61 		gtkToggleAction = cast(GtkToggleAction*)obj;
62 		super.setStruct(obj);
63 	}
64 
65 	/**
66 	 * Sets our main struct and passes it to the parent class.
67 	 */
68 	public this (GtkToggleAction* gtkToggleAction, bool ownedRef = false)
69 	{
70 		this.gtkToggleAction = gtkToggleAction;
71 		super(cast(GtkAction*)gtkToggleAction, ownedRef);
72 	}
73 
74 	/**
75 	 * Creates a new ToggleAction object. To add the action to
76 	 * a ActionGroup and set the accelerator for the action,
77 	 * call gtk.ActionGroup.ActionGroup.addActionWithAccel().
78 	 *
79 	 * Since: 2.4
80 	 *
81 	 * Params:
82 	 *     name    = A unique name for the action
83 	 *     label   = The label displayed in menu items and on buttons, or null
84 	 *     tooltip = A tooltip for the action, or null
85 	 *     stockId = The stock icon to display in widgets representing the
86 	 *               action, or null
87 	 * Throws: ConstructionException GTK+ fails to create the object.
88 	 */
89 	public this (string name, string label, string tooltip, StockID stockId)
90 	{
91 		this(name, label, tooltip, cast(string)stockId);
92 	}
93 
94 	/**
95 	 */
96 
97 	/** */
98 	public static GType getType()
99 	{
100 		return gtk_toggle_action_get_type();
101 	}
102 
103 	/**
104 	 * Creates a new #GtkToggleAction object. To add the action to
105 	 * a #GtkActionGroup and set the accelerator for the action,
106 	 * call gtk_action_group_add_action_with_accel().
107 	 *
108 	 * Params:
109 	 *     name = A unique name for the action
110 	 *     label = The label displayed in menu items and on buttons,
111 	 *         or %NULL
112 	 *     tooltip = A tooltip for the action, or %NULL
113 	 *     stockId = The stock icon to display in widgets representing
114 	 *         the action, or %NULL
115 	 *
116 	 * Return: a new #GtkToggleAction
117 	 *
118 	 * Since: 2.4
119 	 *
120 	 * Throws: ConstructionException GTK+ fails to create the object.
121 	 */
122 	public this(string name, string label, string tooltip, string stockId)
123 	{
124 		auto p = gtk_toggle_action_new(Str.toStringz(name), Str.toStringz(label), Str.toStringz(tooltip), Str.toStringz(stockId));
125 		
126 		if(p is null)
127 		{
128 			throw new ConstructionException("null returned by new");
129 		}
130 		
131 		this(cast(GtkToggleAction*) p, true);
132 	}
133 
134 	/**
135 	 * Returns the checked state of the toggle action.
136 	 *
137 	 * Return: the checked state of the toggle action
138 	 *
139 	 * Since: 2.4
140 	 */
141 	public bool getActive()
142 	{
143 		return gtk_toggle_action_get_active(gtkToggleAction) != 0;
144 	}
145 
146 	/**
147 	 * Returns whether the action should have proxies like a radio action.
148 	 *
149 	 * Return: whether the action should have proxies like a radio action.
150 	 *
151 	 * Since: 2.4
152 	 */
153 	public bool getDrawAsRadio()
154 	{
155 		return gtk_toggle_action_get_draw_as_radio(gtkToggleAction) != 0;
156 	}
157 
158 	/**
159 	 * Sets the checked state on the toggle action.
160 	 *
161 	 * Params:
162 	 *     isActive = whether the action should be checked or not
163 	 *
164 	 * Since: 2.4
165 	 */
166 	public void setActive(bool isActive)
167 	{
168 		gtk_toggle_action_set_active(gtkToggleAction, isActive);
169 	}
170 
171 	/**
172 	 * Sets whether the action should have proxies like a radio action.
173 	 *
174 	 * Params:
175 	 *     drawAsRadio = whether the action should have proxies like a radio
176 	 *         action
177 	 *
178 	 * Since: 2.4
179 	 */
180 	public void setDrawAsRadio(bool drawAsRadio)
181 	{
182 		gtk_toggle_action_set_draw_as_radio(gtkToggleAction, drawAsRadio);
183 	}
184 
185 	/**
186 	 * Emits the “toggled” signal on the toggle action.
187 	 *
188 	 * Since: 2.4
189 	 */
190 	public void toggled()
191 	{
192 		gtk_toggle_action_toggled(gtkToggleAction);
193 	}
194 
195 	protected class OnToggledDelegateWrapper
196 	{
197 		void delegate(ToggleAction) dlg;
198 		gulong handlerId;
199 		ConnectFlags flags;
200 		this(void delegate(ToggleAction) dlg, gulong handlerId, ConnectFlags flags)
201 		{
202 			this.dlg = dlg;
203 			this.handlerId = handlerId;
204 			this.flags = flags;
205 		}
206 	}
207 	protected OnToggledDelegateWrapper[] onToggledListeners;
208 
209 	/**
210 	 * Should be connected if you wish to perform an action
211 	 * whenever the #GtkToggleAction state is changed.
212 	 */
213 	gulong addOnToggled(void delegate(ToggleAction) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
214 	{
215 		onToggledListeners ~= new OnToggledDelegateWrapper(dlg, 0, connectFlags);
216 		onToggledListeners[onToggledListeners.length - 1].handlerId = Signals.connectData(
217 			this,
218 			"toggled",
219 			cast(GCallback)&callBackToggled,
220 			cast(void*)onToggledListeners[onToggledListeners.length - 1],
221 			cast(GClosureNotify)&callBackToggledDestroy,
222 			connectFlags);
223 		return onToggledListeners[onToggledListeners.length - 1].handlerId;
224 	}
225 	
226 	extern(C) static void callBackToggled(GtkToggleAction* toggleactionStruct,OnToggledDelegateWrapper wrapper)
227 	{
228 		wrapper.dlg(wrapper.outer);
229 	}
230 	
231 	extern(C) static void callBackToggledDestroy(OnToggledDelegateWrapper wrapper, GClosure* closure)
232 	{
233 		wrapper.outer.internalRemoveOnToggled(wrapper);
234 	}
235 
236 	protected void internalRemoveOnToggled(OnToggledDelegateWrapper source)
237 	{
238 		foreach(index, wrapper; onToggledListeners)
239 		{
240 			if (wrapper.dlg == source.dlg && wrapper.flags == source.flags && wrapper.handlerId == source.handlerId)
241 			{
242 				onToggledListeners[index] = null;
243 				onToggledListeners = std.algorithm.remove(onToggledListeners, index);
244 				break;
245 			}
246 		}
247 	}
248 	
249 }