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