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.ColorSelection;
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.Box;
34 private import gtk.Widget;
35 public  import gtkc.gdktypes;
36 private import gtkc.gtk;
37 public  import gtkc.gtktypes;
38 private import std.algorithm;
39 
40 
41 /** */
42 public class ColorSelection : Box
43 {
44 	/** the main Gtk struct */
45 	protected GtkColorSelection* gtkColorSelection;
46 
47 	/** Get the main Gtk struct */
48 	public GtkColorSelection* getColorSelectionStruct()
49 	{
50 		return gtkColorSelection;
51 	}
52 
53 	/** the main Gtk struct as a void* */
54 	protected override void* getStruct()
55 	{
56 		return cast(void*)gtkColorSelection;
57 	}
58 
59 	protected override void setStruct(GObject* obj)
60 	{
61 		gtkColorSelection = cast(GtkColorSelection*)obj;
62 		super.setStruct(obj);
63 	}
64 
65 	/**
66 	 * Sets our main struct and passes it to the parent class.
67 	 */
68 	public this (GtkColorSelection* gtkColorSelection, bool ownedRef = false)
69 	{
70 		this.gtkColorSelection = gtkColorSelection;
71 		super(cast(GtkBox*)gtkColorSelection, ownedRef);
72 	}
73 
74 
75 	/** */
76 	public static GType getType()
77 	{
78 		return gtk_color_selection_get_type();
79 	}
80 
81 	/**
82 	 * Creates a new GtkColorSelection.
83 	 *
84 	 * Return: a new #GtkColorSelection
85 	 *
86 	 * Throws: ConstructionException GTK+ fails to create the object.
87 	 */
88 	public this()
89 	{
90 		auto p = gtk_color_selection_new();
91 		
92 		if(p is null)
93 		{
94 			throw new ConstructionException("null returned by new");
95 		}
96 		
97 		this(cast(GtkColorSelection*) p);
98 	}
99 
100 	/**
101 	 * Parses a color palette string; the string is a colon-separated
102 	 * list of color names readable by gdk_color_parse().
103 	 *
104 	 * Params:
105 	 *     str = a string encoding a color palette
106 	 *     colors = return location for
107 	 *         allocated array of #GdkColor
108 	 *     nColors = return location for length of array
109 	 *
110 	 * Return: %TRUE if a palette was successfully parsed
111 	 */
112 	public static bool paletteFromString(string str, out Color[] colors)
113 	{
114 		GdkColor* outcolors = null;
115 		int nColors;
116 		
117 		auto p = gtk_color_selection_palette_from_string(Str.toStringz(str), &outcolors, &nColors) != 0;
118 		
119 		colors = new Color[nColors];
120 		for(size_t i = 0; i < nColors; i++)
121 		{
122 			colors[i] = ObjectG.getDObject!(Color)(cast(GdkColor*) &outcolors[i]);
123 		}
124 		
125 		return p;
126 	}
127 
128 	/**
129 	 * Encodes a palette as a string, useful for persistent storage.
130 	 *
131 	 * Params:
132 	 *     colors = an array of colors
133 	 *     nColors = length of the array
134 	 *
135 	 * Return: allocated string encoding the palette
136 	 */
137 	public static string paletteToString(Color[] colors)
138 	{
139 		GdkColor[] colorsArray = new GdkColor[colors.length];
140 		for ( int i = 0; i < colors.length; i++ )
141 		{
142 			colorsArray[i] = *(colors[i].getColorStruct());
143 		}
144 		
145 		auto retStr = gtk_color_selection_palette_to_string(colorsArray.ptr, cast(int)colors.length);
146 		
147 		scope(exit) Str.freeString(retStr);
148 		return Str.toString(retStr);
149 	}
150 
151 	/**
152 	 * Installs a global function to be called whenever the user
153 	 * tries to modify the palette in a color selection.
154 	 *
155 	 * This function should save the new palette contents, and update
156 	 * the #GtkSettings:gtk-color-palette GtkSettings property so all
157 	 * GtkColorSelection widgets will be modified.
158 	 *
159 	 * Params:
160 	 *     func = a function to call when the custom palette needs saving
161 	 *
162 	 * Return: the previous change palette hook (that was replaced)
163 	 *
164 	 * Since: 2.2
165 	 */
166 	public static GtkColorSelectionChangePaletteWithScreenFunc setChangePaletteWithScreenHook(GtkColorSelectionChangePaletteWithScreenFunc func)
167 	{
168 		return gtk_color_selection_set_change_palette_with_screen_hook(func);
169 	}
170 
171 	/**
172 	 * Returns the current alpha value.
173 	 *
174 	 * Return: an integer between 0 and 65535
175 	 */
176 	public ushort getCurrentAlpha()
177 	{
178 		return gtk_color_selection_get_current_alpha(gtkColorSelection);
179 	}
180 
181 	/**
182 	 * Sets @color to be the current color in the GtkColorSelection widget.
183 	 *
184 	 * Deprecated: Use gtk_color_selection_get_current_rgba() instead.
185 	 *
186 	 * Params:
187 	 *     color = a #GdkColor to fill in with the current color
188 	 */
189 	public void getCurrentColor(out Color color)
190 	{
191 		GdkColor* outcolor = gMalloc!GdkColor();
192 		
193 		gtk_color_selection_get_current_color(gtkColorSelection, outcolor);
194 		
195 		color = ObjectG.getDObject!(Color)(outcolor, true);
196 	}
197 
198 	/**
199 	 * Sets @rgba to be the current color in the GtkColorSelection widget.
200 	 *
201 	 * Params:
202 	 *     rgba = a #GdkRGBA to fill in with the current color
203 	 *
204 	 * Since: 3.0
205 	 */
206 	public void getCurrentRgba(out RGBA rgba)
207 	{
208 		GdkRGBA* outrgba = gMalloc!GdkRGBA();
209 		
210 		gtk_color_selection_get_current_rgba(gtkColorSelection, outrgba);
211 		
212 		rgba = ObjectG.getDObject!(RGBA)(outrgba, true);
213 	}
214 
215 	/**
216 	 * Determines whether the colorsel has an opacity control.
217 	 *
218 	 * Return: %TRUE if the @colorsel has an opacity control,
219 	 *     %FALSE if it does't
220 	 */
221 	public bool getHasOpacityControl()
222 	{
223 		return gtk_color_selection_get_has_opacity_control(gtkColorSelection) != 0;
224 	}
225 
226 	/**
227 	 * Determines whether the color selector has a color palette.
228 	 *
229 	 * Return: %TRUE if the selector has a palette, %FALSE if it hasn't
230 	 */
231 	public bool getHasPalette()
232 	{
233 		return gtk_color_selection_get_has_palette(gtkColorSelection) != 0;
234 	}
235 
236 	/**
237 	 * Returns the previous alpha value.
238 	 *
239 	 * Return: an integer between 0 and 65535
240 	 */
241 	public ushort getPreviousAlpha()
242 	{
243 		return gtk_color_selection_get_previous_alpha(gtkColorSelection);
244 	}
245 
246 	/**
247 	 * Fills @color in with the original color value.
248 	 *
249 	 * Deprecated: Use gtk_color_selection_get_previous_rgba() instead.
250 	 *
251 	 * Params:
252 	 *     color = a #GdkColor to fill in with the original color value
253 	 */
254 	public void getPreviousColor(out Color color)
255 	{
256 		GdkColor* outcolor = gMalloc!GdkColor();
257 		
258 		gtk_color_selection_get_previous_color(gtkColorSelection, outcolor);
259 		
260 		color = ObjectG.getDObject!(Color)(outcolor, true);
261 	}
262 
263 	/**
264 	 * Fills @rgba in with the original color value.
265 	 *
266 	 * Params:
267 	 *     rgba = a #GdkRGBA to fill in with the original color value
268 	 *
269 	 * Since: 3.0
270 	 */
271 	public void getPreviousRgba(out RGBA rgba)
272 	{
273 		GdkRGBA* outrgba = gMalloc!GdkRGBA();
274 		
275 		gtk_color_selection_get_previous_rgba(gtkColorSelection, outrgba);
276 		
277 		rgba = ObjectG.getDObject!(RGBA)(outrgba, true);
278 	}
279 
280 	/**
281 	 * Gets the current state of the @colorsel.
282 	 *
283 	 * Return: %TRUE if the user is currently dragging
284 	 *     a color around, and %FALSE if the selection has stopped
285 	 */
286 	public bool isAdjusting()
287 	{
288 		return gtk_color_selection_is_adjusting(gtkColorSelection) != 0;
289 	}
290 
291 	/**
292 	 * Sets the current opacity to be @alpha.
293 	 *
294 	 * The first time this is called, it will also set
295 	 * the original opacity to be @alpha too.
296 	 *
297 	 * Params:
298 	 *     alpha = an integer between 0 and 65535
299 	 */
300 	public void setCurrentAlpha(ushort alpha)
301 	{
302 		gtk_color_selection_set_current_alpha(gtkColorSelection, alpha);
303 	}
304 
305 	/**
306 	 * Sets the current color to be @color.
307 	 *
308 	 * The first time this is called, it will also set
309 	 * the original color to be @color too.
310 	 *
311 	 * Deprecated: Use gtk_color_selection_set_current_rgba() instead.
312 	 *
313 	 * Params:
314 	 *     color = a #GdkColor to set the current color with
315 	 */
316 	public void setCurrentColor(Color color)
317 	{
318 		gtk_color_selection_set_current_color(gtkColorSelection, (color is null) ? null : color.getColorStruct());
319 	}
320 
321 	/**
322 	 * Sets the current color to be @rgba.
323 	 *
324 	 * The first time this is called, it will also set
325 	 * the original color to be @rgba too.
326 	 *
327 	 * Params:
328 	 *     rgba = A #GdkRGBA to set the current color with
329 	 *
330 	 * Since: 3.0
331 	 */
332 	public void setCurrentRgba(RGBA rgba)
333 	{
334 		gtk_color_selection_set_current_rgba(gtkColorSelection, (rgba is null) ? null : rgba.getRGBAStruct());
335 	}
336 
337 	/**
338 	 * Sets the @colorsel to use or not use opacity.
339 	 *
340 	 * Params:
341 	 *     hasOpacity = %TRUE if @colorsel can set the opacity, %FALSE otherwise
342 	 */
343 	public void setHasOpacityControl(bool hasOpacity)
344 	{
345 		gtk_color_selection_set_has_opacity_control(gtkColorSelection, hasOpacity);
346 	}
347 
348 	/**
349 	 * Shows and hides the palette based upon the value of @has_palette.
350 	 *
351 	 * Params:
352 	 *     hasPalette = %TRUE if palette is to be visible, %FALSE otherwise
353 	 */
354 	public void setHasPalette(bool hasPalette)
355 	{
356 		gtk_color_selection_set_has_palette(gtkColorSelection, hasPalette);
357 	}
358 
359 	/**
360 	 * Sets the “previous” alpha to be @alpha.
361 	 *
362 	 * This function should be called with some hesitations,
363 	 * as it might seem confusing to have that alpha change.
364 	 *
365 	 * Params:
366 	 *     alpha = an integer between 0 and 65535
367 	 */
368 	public void setPreviousAlpha(ushort alpha)
369 	{
370 		gtk_color_selection_set_previous_alpha(gtkColorSelection, alpha);
371 	}
372 
373 	/**
374 	 * Sets the “previous” color to be @color.
375 	 *
376 	 * This function should be called with some hesitations,
377 	 * as it might seem confusing to have that color change.
378 	 * Calling gtk_color_selection_set_current_color() will also
379 	 * set this color the first time it is called.
380 	 *
381 	 * Deprecated: Use gtk_color_selection_set_previous_rgba() instead.
382 	 *
383 	 * Params:
384 	 *     color = a #GdkColor to set the previous color with
385 	 */
386 	public void setPreviousColor(Color color)
387 	{
388 		gtk_color_selection_set_previous_color(gtkColorSelection, (color is null) ? null : color.getColorStruct());
389 	}
390 
391 	/**
392 	 * Sets the “previous” color to be @rgba.
393 	 *
394 	 * This function should be called with some hesitations,
395 	 * as it might seem confusing to have that color change.
396 	 * Calling gtk_color_selection_set_current_rgba() will also
397 	 * set this color the first time it is called.
398 	 *
399 	 * Params:
400 	 *     rgba = a #GdkRGBA to set the previous color with
401 	 *
402 	 * Since: 3.0
403 	 */
404 	public void setPreviousRgba(RGBA rgba)
405 	{
406 		gtk_color_selection_set_previous_rgba(gtkColorSelection, (rgba is null) ? null : rgba.getRGBAStruct());
407 	}
408 
409 	protected class OnColorChangedDelegateWrapper
410 	{
411 		void delegate(ColorSelection) dlg;
412 		gulong handlerId;
413 		ConnectFlags flags;
414 		this(void delegate(ColorSelection) dlg, gulong handlerId, ConnectFlags flags)
415 		{
416 			this.dlg = dlg;
417 			this.handlerId = handlerId;
418 			this.flags = flags;
419 		}
420 	}
421 	protected OnColorChangedDelegateWrapper[] onColorChangedListeners;
422 
423 	/**
424 	 * This signal is emitted when the color changes in the #GtkColorSelection
425 	 * according to its update policy.
426 	 */
427 	gulong addOnColorChanged(void delegate(ColorSelection) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
428 	{
429 		onColorChangedListeners ~= new OnColorChangedDelegateWrapper(dlg, 0, connectFlags);
430 		onColorChangedListeners[onColorChangedListeners.length - 1].handlerId = Signals.connectData(
431 			this,
432 			"color-changed",
433 			cast(GCallback)&callBackColorChanged,
434 			cast(void*)onColorChangedListeners[onColorChangedListeners.length - 1],
435 			cast(GClosureNotify)&callBackColorChangedDestroy,
436 			connectFlags);
437 		return onColorChangedListeners[onColorChangedListeners.length - 1].handlerId;
438 	}
439 	
440 	extern(C) static void callBackColorChanged(GtkColorSelection* colorselectionStruct,OnColorChangedDelegateWrapper wrapper)
441 	{
442 		wrapper.dlg(wrapper.outer);
443 	}
444 	
445 	extern(C) static void callBackColorChangedDestroy(OnColorChangedDelegateWrapper wrapper, GClosure* closure)
446 	{
447 		wrapper.outer.internalRemoveOnColorChanged(wrapper);
448 	}
449 
450 	protected void internalRemoveOnColorChanged(OnColorChangedDelegateWrapper source)
451 	{
452 		foreach(index, wrapper; onColorChangedListeners)
453 		{
454 			if (wrapper.dlg == source.dlg && wrapper.flags == source.flags && wrapper.handlerId == source.handlerId)
455 			{
456 				onColorChangedListeners[index] = null;
457 				onColorChangedListeners = std.algorithm.remove(onColorChangedListeners, index);
458 				break;
459 			}
460 		}
461 	}
462 	
463 }