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.ColorChooserT;
26 
27 public  import gdk.RGBA;
28 public  import gobject.ObjectG;
29 public  import gobject.Signals;
30 public  import gtkc.gtk;
31 public  import gtkc.gtktypes;
32 public  import std.algorithm;
33 
34 
35 /**
36  * #GtkColorChooser is an interface that is implemented by widgets
37  * for choosing colors. Depending on the situation, colors may be
38  * allowed to have alpha (translucency).
39  * 
40  * In GTK+, the main widgets that implement this interface are
41  * #GtkColorChooserWidget, #GtkColorChooserDialog and #GtkColorButton.
42  */
43 public template ColorChooserT(TStruct)
44 {
45 	/** Get the main Gtk struct */
46 	public GtkColorChooser* getColorChooserStruct(bool transferOwnership = false)
47 	{
48 		if (transferOwnership)
49 			ownedRef = false;
50 		return cast(GtkColorChooser*)getStruct();
51 	}
52 
53 
54 	/**
55 	 * Adds a palette to the color chooser. If @orientation is horizontal,
56 	 * the colors are grouped in rows, with @colors_per_line colors
57 	 * in each row. If @horizontal is %FALSE, the colors are grouped
58 	 * in columns instead.
59 	 *
60 	 * The default color palette of #GtkColorChooserWidget has
61 	 * 27 colors, organized in columns of 3 colors. The default gray
62 	 * palette has 9 grays in a single row.
63 	 *
64 	 * The layout of the color chooser widget works best when the
65 	 * palettes have 9-10 columns.
66 	 *
67 	 * Calling this function for the first time has the
68 	 * side effect of removing the default color and gray palettes
69 	 * from the color chooser.
70 	 *
71 	 * If @colors is %NULL, removes all previously added palettes.
72 	 *
73 	 * Params:
74 	 *     orientation = %GTK_ORIENTATION_HORIZONTAL if the palette should
75 	 *         be displayed in rows, %GTK_ORIENTATION_VERTICAL for columns
76 	 *     colorsPerLine = the number of colors to show in each row/column
77 	 *     nColors = the total number of elements in @colors
78 	 *     colors = the colors of the palette, or %NULL
79 	 *
80 	 * Since: 3.4
81 	 */
82 	public void addPalette(GtkOrientation orientation, int colorsPerLine, RGBA[] colors)
83 	{
84 		GdkRGBA[] colorsArray = new GdkRGBA[colors.length];
85 		for ( int i = 0; i < colors.length; i++ )
86 		{
87 			colorsArray[i] = *(colors[i].getRGBAStruct());
88 		}
89 		
90 		gtk_color_chooser_add_palette(getColorChooserStruct(), orientation, colorsPerLine, cast(int)colors.length, colorsArray.ptr);
91 	}
92 
93 	/**
94 	 * Gets the currently-selected color.
95 	 *
96 	 * Params:
97 	 *     color = a #GdkRGBA to fill in with the current color
98 	 *
99 	 * Since: 3.4
100 	 */
101 	public void getRgba(out RGBA color)
102 	{
103 		GdkRGBA* outcolor = gMalloc!GdkRGBA();
104 		
105 		gtk_color_chooser_get_rgba(getColorChooserStruct(), outcolor);
106 		
107 		color = ObjectG.getDObject!(RGBA)(outcolor, true);
108 	}
109 
110 	/**
111 	 * Returns whether the color chooser shows the alpha channel.
112 	 *
113 	 * Returns: %TRUE if the color chooser uses the alpha channel,
114 	 *     %FALSE if not
115 	 *
116 	 * Since: 3.4
117 	 */
118 	public bool getUseAlpha()
119 	{
120 		return gtk_color_chooser_get_use_alpha(getColorChooserStruct()) != 0;
121 	}
122 
123 	/**
124 	 * Sets the color.
125 	 *
126 	 * Params:
127 	 *     color = the new color
128 	 *
129 	 * Since: 3.4
130 	 */
131 	public void setRgba(RGBA color)
132 	{
133 		gtk_color_chooser_set_rgba(getColorChooserStruct(), (color is null) ? null : color.getRGBAStruct());
134 	}
135 
136 	/**
137 	 * Sets whether or not the color chooser should use the alpha channel.
138 	 *
139 	 * Params:
140 	 *     useAlpha = %TRUE if color chooser should use alpha channel, %FALSE if not
141 	 *
142 	 * Since: 3.4
143 	 */
144 	public void setUseAlpha(bool useAlpha)
145 	{
146 		gtk_color_chooser_set_use_alpha(getColorChooserStruct(), useAlpha);
147 	}
148 
149 	protected class OnColorActivatedDelegateWrapper
150 	{
151 		static OnColorActivatedDelegateWrapper[] listeners;
152 		void delegate(RGBA, ColorChooserIF) dlg;
153 		gulong handlerId;
154 		
155 		this(void delegate(RGBA, ColorChooserIF) dlg)
156 		{
157 			this.dlg = dlg;
158 			this.listeners ~= this;
159 		}
160 		
161 		void remove(OnColorActivatedDelegateWrapper source)
162 		{
163 			foreach(index, wrapper; listeners)
164 			{
165 				if (wrapper.handlerId == source.handlerId)
166 				{
167 					listeners[index] = null;
168 					listeners = std.algorithm.remove(listeners, index);
169 					break;
170 				}
171 			}
172 		}
173 	}
174 
175 	/**
176 	 * Emitted when a color is activated from the color chooser.
177 	 * This usually happens when the user clicks a color swatch,
178 	 * or a color is selected and the user presses one of the keys
179 	 * Space, Shift+Space, Return or Enter.
180 	 *
181 	 * Params:
182 	 *     color = the color
183 	 *
184 	 * Since: 3.4
185 	 */
186 	gulong addOnColorActivated(void delegate(RGBA, ColorChooserIF) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
187 	{
188 		auto wrapper = new OnColorActivatedDelegateWrapper(dlg);
189 		wrapper.handlerId = Signals.connectData(
190 			this,
191 			"color-activated",
192 			cast(GCallback)&callBackColorActivated,
193 			cast(void*)wrapper,
194 			cast(GClosureNotify)&callBackColorActivatedDestroy,
195 			connectFlags);
196 		return wrapper.handlerId;
197 	}
198 	
199 	extern(C) static void callBackColorActivated(GtkColorChooser* colorchooserStruct, GdkRGBA* color, OnColorActivatedDelegateWrapper wrapper)
200 	{
201 		wrapper.dlg(ObjectG.getDObject!(RGBA)(color), wrapper.outer);
202 	}
203 	
204 	extern(C) static void callBackColorActivatedDestroy(OnColorActivatedDelegateWrapper wrapper, GClosure* closure)
205 	{
206 		wrapper.remove(wrapper);
207 	}
208 }