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