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.CssProvider; 26 27 private import gio.FileIF; 28 private import glib.ConstructionException; 29 private import glib.ErrorG; 30 private import glib.GException; 31 private import glib.Str; 32 private import gobject.ObjectG; 33 private import gobject.Signals; 34 private import gtk.CssSection; 35 private import gtk.StyleProviderIF; 36 private import gtk.StyleProviderT; 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 * GtkCssProvider is an object implementing the #GtkStyleProvider interface. 45 * It is able to parse [CSS-like][css-overview] input in order to style widgets. 46 * 47 * An application can make GTK+ parse a specific CSS style sheet by calling 48 * gtk_css_provider_load_from_file() or gtk_css_provider_load_from_resource() 49 * and adding the provider with gtk_style_context_add_provider() or 50 * gtk_style_context_add_provider_for_screen(). 51 * 52 * In addition, certain files will be read when GTK+ is initialized. First, the 53 * file `$XDG_CONFIG_HOME/gtk-3.0/gtk.css` is loaded if it exists. Then, GTK+ 54 * loads the first existing file among 55 * `XDG_DATA_HOME/themes/THEME/gtk-VERSION/gtk.css`, 56 * `$HOME/.themes/THEME/gtk-VERSION/gtk.css`, 57 * `$XDG_DATA_DIRS/themes/THEME/gtk-VERSION/gtk.css` and 58 * `DATADIR/share/themes/THEME/gtk-VERSION/gtk.css`, where `THEME` is the name of 59 * the current theme (see the #GtkSettings:gtk-theme-name setting), `DATADIR` 60 * is the prefix configured when GTK+ was compiled (unless overridden by the 61 * `GTK_DATA_PREFIX` environment variable), and `VERSION` is the GTK+ version number. 62 * If no file is found for the current version, GTK+ tries older versions all the 63 * way back to 3.0. 64 * 65 * In the same way, GTK+ tries to load a gtk-keys.css file for the current 66 * key theme, as defined by #GtkSettings:gtk-key-theme-name. 67 */ 68 public class CssProvider : ObjectG, StyleProviderIF 69 { 70 /** the main Gtk struct */ 71 protected GtkCssProvider* gtkCssProvider; 72 73 /** Get the main Gtk struct */ 74 public GtkCssProvider* getCssProviderStruct(bool transferOwnership = false) 75 { 76 if (transferOwnership) 77 ownedRef = false; 78 return gtkCssProvider; 79 } 80 81 /** the main Gtk struct as a void* */ 82 protected override void* getStruct() 83 { 84 return cast(void*)gtkCssProvider; 85 } 86 87 /** 88 * Sets our main struct and passes it to the parent class. 89 */ 90 public this (GtkCssProvider* gtkCssProvider, bool ownedRef = false) 91 { 92 this.gtkCssProvider = gtkCssProvider; 93 super(cast(GObject*)gtkCssProvider, ownedRef); 94 } 95 96 // add the StyleProvider capabilities 97 mixin StyleProviderT!(GtkCssProvider); 98 99 100 /** */ 101 public static GType getType() 102 { 103 return gtk_css_provider_get_type(); 104 } 105 106 /** 107 * Returns a newly created #GtkCssProvider. 108 * 109 * Returns: A new #GtkCssProvider 110 * 111 * Throws: ConstructionException GTK+ fails to create the object. 112 */ 113 public this() 114 { 115 auto p = gtk_css_provider_new(); 116 117 if(p is null) 118 { 119 throw new ConstructionException("null returned by new"); 120 } 121 122 this(cast(GtkCssProvider*) p, true); 123 } 124 125 /** 126 * Returns the provider containing the style settings used as a 127 * fallback for all widgets. 128 * 129 * Deprecated: Use gtk_css_provider_new() instead. 130 * 131 * Returns: The provider used for fallback styling. 132 * This memory is owned by GTK+, and you must not free it. 133 */ 134 public static CssProvider getDefault() 135 { 136 auto p = gtk_css_provider_get_default(); 137 138 if(p is null) 139 { 140 return null; 141 } 142 143 return ObjectG.getDObject!(CssProvider)(cast(GtkCssProvider*) p); 144 } 145 146 /** 147 * Loads a theme from the usual theme paths 148 * 149 * Params: 150 * name = A theme name 151 * variant = variant to load, for example, "dark", or 152 * %NULL for the default 153 * 154 * Returns: a #GtkCssProvider with the theme loaded. 155 * This memory is owned by GTK+, and you must not free it. 156 */ 157 public static CssProvider getNamed(string name, string variant) 158 { 159 auto p = gtk_css_provider_get_named(Str.toStringz(name), Str.toStringz(variant)); 160 161 if(p is null) 162 { 163 return null; 164 } 165 166 return ObjectG.getDObject!(CssProvider)(cast(GtkCssProvider*) p); 167 } 168 169 /** 170 * Loads @data into @css_provider, and by doing so clears any previously loaded 171 * information. 172 * 173 * Params: 174 * data = CSS data loaded in memory 175 * 176 * Returns: %TRUE. The return value is deprecated and %FALSE will only be 177 * returned for backwards compatibility reasons if an @error is not 178 * %NULL and a loading error occurred. To track errors while loading 179 * CSS, connect to the #GtkCssProvider::parsing-error signal. 180 * 181 * Throws: GException on failure. 182 */ 183 public bool loadFromData(string data) 184 { 185 GError* err = null; 186 187 auto p = gtk_css_provider_load_from_data(gtkCssProvider, Str.toStringz(data), cast(ptrdiff_t)data.length, &err) != 0; 188 189 if (err !is null) 190 { 191 throw new GException( new ErrorG(err) ); 192 } 193 194 return p; 195 } 196 197 /** 198 * Loads the data contained in @file into @css_provider, making it 199 * clear any previously loaded information. 200 * 201 * Params: 202 * file = #GFile pointing to a file to load 203 * 204 * Returns: %TRUE. The return value is deprecated and %FALSE will only be 205 * returned for backwards compatibility reasons if an @error is not 206 * %NULL and a loading error occurred. To track errors while loading 207 * CSS, connect to the #GtkCssProvider::parsing-error signal. 208 * 209 * Throws: GException on failure. 210 */ 211 public bool loadFromFile(FileIF file) 212 { 213 GError* err = null; 214 215 auto p = gtk_css_provider_load_from_file(gtkCssProvider, (file is null) ? null : file.getFileStruct(), &err) != 0; 216 217 if (err !is null) 218 { 219 throw new GException( new ErrorG(err) ); 220 } 221 222 return p; 223 } 224 225 /** 226 * Loads the data contained in @path into @css_provider, making it clear 227 * any previously loaded information. 228 * 229 * Params: 230 * path = the path of a filename to load, in the GLib filename encoding 231 * 232 * Returns: %TRUE. The return value is deprecated and %FALSE will only be 233 * returned for backwards compatibility reasons if an @error is not 234 * %NULL and a loading error occurred. To track errors while loading 235 * CSS, connect to the #GtkCssProvider::parsing-error signal. 236 * 237 * Throws: GException on failure. 238 */ 239 public bool loadFromPath(string path) 240 { 241 GError* err = null; 242 243 auto p = gtk_css_provider_load_from_path(gtkCssProvider, Str.toStringz(path), &err) != 0; 244 245 if (err !is null) 246 { 247 throw new GException( new ErrorG(err) ); 248 } 249 250 return p; 251 } 252 253 /** 254 * Loads the data contained in the resource at @resource_path into 255 * the #GtkCssProvider, clearing any previously loaded information. 256 * 257 * To track errors while loading CSS, connect to the 258 * #GtkCssProvider::parsing-error signal. 259 * 260 * Params: 261 * resourcePath = a #GResource resource path 262 * 263 * Since: 3.16 264 */ 265 public void loadFromResource(string resourcePath) 266 { 267 gtk_css_provider_load_from_resource(gtkCssProvider, Str.toStringz(resourcePath)); 268 } 269 270 /** 271 * Converts the @provider into a string representation in CSS 272 * format. 273 * 274 * Using gtk_css_provider_load_from_data() with the return value 275 * from this function on a new provider created with 276 * gtk_css_provider_new() will basically create a duplicate of 277 * this @provider. 278 * 279 * Returns: a new string representing the @provider. 280 * 281 * Since: 3.2 282 */ 283 public override string toString() 284 { 285 auto retStr = gtk_css_provider_to_string(gtkCssProvider); 286 287 scope(exit) Str.freeString(retStr); 288 return Str.toString(retStr); 289 } 290 291 /** 292 * Signals that a parsing error occurred. the @path, @line and @position 293 * describe the actual location of the error as accurately as possible. 294 * 295 * Parsing errors are never fatal, so the parsing will resume after 296 * the error. Errors may however cause parts of the given 297 * data or even all of it to not be parsed at all. So it is a useful idea 298 * to check that the parsing succeeds by connecting to this signal. 299 * 300 * Note that this signal may be emitted at any time as the css provider 301 * may opt to defer parsing parts or all of the input to a later time 302 * than when a loading function was called. 303 * 304 * Params: 305 * section = section the error happened in 306 * error = The parsing error 307 */ 308 gulong addOnParsingError(void delegate(CssSection, ErrorG, CssProvider) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 309 { 310 return Signals.connect(this, "parsing-error", dlg, connectFlags ^ ConnectFlags.SWAPPED); 311 } 312 }