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