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.ColorButton;
26 
27 private import gdk.Color;
28 private import gdk.RGBA;
29 private import glib.ConstructionException;
30 private import glib.Str;
31 private import gobject.ObjectG;
32 private import gobject.Signals;
33 private import gtk.Button;
34 private import gtk.ColorChooserIF;
35 private import gtk.ColorChooserT;
36 private import gtk.Widget;
37 private import gtk.c.functions;
38 public  import gtk.c.types;
39 public  import gtkc.gtktypes;
40 private import std.algorithm;
41 
42 
43 /**
44  * The #GtkColorButton is a button which displays the currently selected
45  * color and allows to open a color selection dialog to change the color.
46  * It is suitable widget for selecting a color in a preference dialog.
47  * 
48  * # CSS nodes
49  * 
50  * GtkColorButton has a single CSS node with name button. To differentiate
51  * it from a plain #GtkButton, it gets the .color style class.
52  */
53 public class ColorButton : Button, ColorChooserIF
54 {
55 	/** the main Gtk struct */
56 	protected GtkColorButton* gtkColorButton;
57 
58 	/** Get the main Gtk struct */
59 	public GtkColorButton* getColorButtonStruct(bool transferOwnership = false)
60 	{
61 		if (transferOwnership)
62 			ownedRef = false;
63 		return gtkColorButton;
64 	}
65 
66 	/** the main Gtk struct as a void* */
67 	protected override void* getStruct()
68 	{
69 		return cast(void*)gtkColorButton;
70 	}
71 
72 	protected override void setStruct(GObject* obj)
73 	{
74 		gtkColorButton = cast(GtkColorButton*)obj;
75 		super.setStruct(obj);
76 	}
77 
78 	/**
79 	 * Sets our main struct and passes it to the parent class.
80 	 */
81 	public this (GtkColorButton* gtkColorButton, bool ownedRef = false)
82 	{
83 		this.gtkColorButton = gtkColorButton;
84 		super(cast(GtkButton*)gtkColorButton, ownedRef);
85 	}
86 
87 	// add the ColorChooser capabilities
88 	mixin ColorChooserT!(GtkColorButton);
89 
90 
91 	/** */
92 	public static GType getType()
93 	{
94 		return gtk_color_button_get_type();
95 	}
96 
97 	/**
98 	 * Creates a new color button.
99 	 *
100 	 * This returns a widget in the form of a small button containing
101 	 * a swatch representing the current selected color. When the button
102 	 * is clicked, a color-selection dialog will open, allowing the user
103 	 * to select a color. The swatch will be updated to reflect the new
104 	 * color when the user finishes.
105 	 *
106 	 * Returns: a new color button
107 	 *
108 	 * Since: 2.4
109 	 *
110 	 * Throws: ConstructionException GTK+ fails to create the object.
111 	 */
112 	public this()
113 	{
114 		auto p = gtk_color_button_new();
115 
116 		if(p is null)
117 		{
118 			throw new ConstructionException("null returned by new");
119 		}
120 
121 		this(cast(GtkColorButton*) p);
122 	}
123 
124 	/**
125 	 * Creates a new color button.
126 	 *
127 	 * Deprecated: Use gtk_color_button_new_with_rgba() instead.
128 	 *
129 	 * Params:
130 	 *     color = A #GdkColor to set the current color with
131 	 *
132 	 * Returns: a new color button
133 	 *
134 	 * Since: 2.4
135 	 *
136 	 * Throws: ConstructionException GTK+ fails to create the object.
137 	 */
138 	public this(Color color)
139 	{
140 		auto p = gtk_color_button_new_with_color((color is null) ? null : color.getColorStruct());
141 
142 		if(p is null)
143 		{
144 			throw new ConstructionException("null returned by new_with_color");
145 		}
146 
147 		this(cast(GtkColorButton*) p);
148 	}
149 
150 	/**
151 	 * Creates a new color button.
152 	 *
153 	 * Params:
154 	 *     rgba = A #GdkRGBA to set the current color with
155 	 *
156 	 * Returns: a new color button
157 	 *
158 	 * Since: 3.0
159 	 *
160 	 * Throws: ConstructionException GTK+ fails to create the object.
161 	 */
162 	public this(RGBA rgba)
163 	{
164 		auto p = gtk_color_button_new_with_rgba((rgba is null) ? null : rgba.getRGBAStruct());
165 
166 		if(p is null)
167 		{
168 			throw new ConstructionException("null returned by new_with_rgba");
169 		}
170 
171 		this(cast(GtkColorButton*) p);
172 	}
173 
174 	/**
175 	 * Returns the current alpha value.
176 	 *
177 	 * Deprecated: Use gtk_color_chooser_get_rgba() instead.
178 	 *
179 	 * Returns: an integer between 0 and 65535
180 	 *
181 	 * Since: 2.4
182 	 */
183 	public ushort getAlpha()
184 	{
185 		return gtk_color_button_get_alpha(gtkColorButton);
186 	}
187 
188 	/**
189 	 * Sets @color to be the current color in the #GtkColorButton widget.
190 	 *
191 	 * Deprecated: Use gtk_color_chooser_get_rgba() instead.
192 	 *
193 	 * Params:
194 	 *     color = a #GdkColor to fill in with the current color
195 	 *
196 	 * Since: 2.4
197 	 */
198 	public void getColor(out Color color)
199 	{
200 		GdkColor* outcolor = gMalloc!GdkColor();
201 
202 		gtk_color_button_get_color(gtkColorButton, outcolor);
203 
204 		color = ObjectG.getDObject!(Color)(outcolor, true);
205 	}
206 
207 	/**
208 	 * Gets the title of the color selection dialog.
209 	 *
210 	 * Returns: An internal string, do not free the return value
211 	 *
212 	 * Since: 2.4
213 	 */
214 	public string getTitle()
215 	{
216 		return Str.toString(gtk_color_button_get_title(gtkColorButton));
217 	}
218 
219 	/**
220 	 * Sets the current opacity to be @alpha.
221 	 *
222 	 * Deprecated: Use gtk_color_chooser_set_rgba() instead.
223 	 *
224 	 * Params:
225 	 *     alpha = an integer between 0 and 65535
226 	 *
227 	 * Since: 2.4
228 	 */
229 	public void setAlpha(ushort alpha)
230 	{
231 		gtk_color_button_set_alpha(gtkColorButton, alpha);
232 	}
233 
234 	/**
235 	 * Sets the current color to be @color.
236 	 *
237 	 * Deprecated: Use gtk_color_chooser_set_rgba() instead.
238 	 *
239 	 * Params:
240 	 *     color = A #GdkColor to set the current color with
241 	 *
242 	 * Since: 2.4
243 	 */
244 	public void setColor(Color color)
245 	{
246 		gtk_color_button_set_color(gtkColorButton, (color is null) ? null : color.getColorStruct());
247 	}
248 
249 	/**
250 	 * Sets the title for the color selection dialog.
251 	 *
252 	 * Params:
253 	 *     title = String containing new window title
254 	 *
255 	 * Since: 2.4
256 	 */
257 	public void setTitle(string title)
258 	{
259 		gtk_color_button_set_title(gtkColorButton, Str.toStringz(title));
260 	}
261 
262 	protected class OnColorSetDelegateWrapper
263 	{
264 		void delegate(ColorButton) dlg;
265 		gulong handlerId;
266 
267 		this(void delegate(ColorButton) dlg)
268 		{
269 			this.dlg = dlg;
270 			onColorSetListeners ~= this;
271 		}
272 
273 		void remove(OnColorSetDelegateWrapper source)
274 		{
275 			foreach(index, wrapper; onColorSetListeners)
276 			{
277 				if (wrapper.handlerId == source.handlerId)
278 				{
279 					onColorSetListeners[index] = null;
280 					onColorSetListeners = std.algorithm.remove(onColorSetListeners, index);
281 					break;
282 				}
283 			}
284 		}
285 	}
286 	OnColorSetDelegateWrapper[] onColorSetListeners;
287 
288 	/**
289 	 * The ::color-set signal is emitted when the user selects a color.
290 	 * When handling this signal, use gtk_color_button_get_rgba() to
291 	 * find out which color was just selected.
292 	 *
293 	 * Note that this signal is only emitted when the user
294 	 * changes the color. If you need to react to programmatic color changes
295 	 * as well, use the notify::color signal.
296 	 *
297 	 * Since: 2.4
298 	 */
299 	gulong addOnColorSet(void delegate(ColorButton) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
300 	{
301 		auto wrapper = new OnColorSetDelegateWrapper(dlg);
302 		wrapper.handlerId = Signals.connectData(
303 			this,
304 			"color-set",
305 			cast(GCallback)&callBackColorSet,
306 			cast(void*)wrapper,
307 			cast(GClosureNotify)&callBackColorSetDestroy,
308 			connectFlags);
309 		return wrapper.handlerId;
310 	}
311 
312 	extern(C) static void callBackColorSet(GtkColorButton* colorbuttonStruct, OnColorSetDelegateWrapper wrapper)
313 	{
314 		wrapper.dlg(wrapper.outer);
315 	}
316 
317 	extern(C) static void callBackColorSetDestroy(OnColorSetDelegateWrapper wrapper, GClosure* closure)
318 	{
319 		wrapper.remove(wrapper);
320 	}
321 }