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