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