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