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 * Returns: The provider used for fallback styling. 130 * This memory is owned by GTK+, and you must not free it. 131 */ 132 public static CssProvider getDefault() 133 { 134 auto p = gtk_css_provider_get_default(); 135 136 if(p is null) 137 { 138 return null; 139 } 140 141 return ObjectG.getDObject!(CssProvider)(cast(GtkCssProvider*) p); 142 } 143 144 /** 145 * Loads a theme from the usual theme paths 146 * 147 * Params: 148 * name = A theme name 149 * variant = variant to load, for example, "dark", or 150 * %NULL for the default 151 * 152 * Returns: a #GtkCssProvider with the theme loaded. 153 * This memory is owned by GTK+, and you must not free it. 154 */ 155 public static CssProvider getNamed(string name, string variant) 156 { 157 auto p = gtk_css_provider_get_named(Str.toStringz(name), Str.toStringz(variant)); 158 159 if(p is null) 160 { 161 return null; 162 } 163 164 return ObjectG.getDObject!(CssProvider)(cast(GtkCssProvider*) p); 165 } 166 167 /** 168 * Loads @data into @css_provider, and by doing so clears any previously loaded 169 * information. 170 * 171 * Params: 172 * data = CSS data loaded in memory 173 * 174 * Returns: %TRUE. The return value is deprecated and %FALSE will only be 175 * returned for backwards compatibility reasons if an @error is not 176 * %NULL and a loading error occurred. To track errors while loading 177 * CSS, connect to the #GtkCssProvider::parsing-error signal. 178 * 179 * Throws: GException on failure. 180 */ 181 public bool loadFromData(string data) 182 { 183 GError* err = null; 184 185 auto p = gtk_css_provider_load_from_data(gtkCssProvider, Str.toStringz(data), cast(ptrdiff_t)data.length, &err) != 0; 186 187 if (err !is null) 188 { 189 throw new GException( new ErrorG(err) ); 190 } 191 192 return p; 193 } 194 195 /** 196 * Loads the data contained in @file into @css_provider, making it 197 * clear any previously loaded information. 198 * 199 * Params: 200 * file = #GFile pointing to a file to load 201 * 202 * Returns: %TRUE. The return value is deprecated and %FALSE will only be 203 * returned for backwards compatibility reasons if an @error is not 204 * %NULL and a loading error occurred. To track errors while loading 205 * CSS, connect to the #GtkCssProvider::parsing-error signal. 206 * 207 * Throws: GException on failure. 208 */ 209 public bool loadFromFile(FileIF file) 210 { 211 GError* err = null; 212 213 auto p = gtk_css_provider_load_from_file(gtkCssProvider, (file is null) ? null : file.getFileStruct(), &err) != 0; 214 215 if (err !is null) 216 { 217 throw new GException( new ErrorG(err) ); 218 } 219 220 return p; 221 } 222 223 /** 224 * Loads the data contained in @path into @css_provider, making it clear 225 * any previously loaded information. 226 * 227 * Params: 228 * path = the path of a filename to load, in the GLib filename encoding 229 * 230 * Returns: %TRUE. The return value is deprecated and %FALSE will only be 231 * returned for backwards compatibility reasons if an @error is not 232 * %NULL and a loading error occurred. To track errors while loading 233 * CSS, connect to the #GtkCssProvider::parsing-error signal. 234 * 235 * Throws: GException on failure. 236 */ 237 public bool loadFromPath(string path) 238 { 239 GError* err = null; 240 241 auto p = gtk_css_provider_load_from_path(gtkCssProvider, Str.toStringz(path), &err) != 0; 242 243 if (err !is null) 244 { 245 throw new GException( new ErrorG(err) ); 246 } 247 248 return p; 249 } 250 251 /** 252 * Loads the data contained in the resource at @resource_path into 253 * the #GtkCssProvider, clearing any previously loaded information. 254 * 255 * To track errors while loading CSS, connect to the 256 * #GtkCssProvider::parsing-error signal. 257 * 258 * Params: 259 * resourcePath = a #GResource resource path 260 * 261 * Since: 3.16 262 */ 263 public void loadFromResource(string resourcePath) 264 { 265 gtk_css_provider_load_from_resource(gtkCssProvider, Str.toStringz(resourcePath)); 266 } 267 268 /** 269 * Converts the @provider into a string representation in CSS 270 * format. 271 * 272 * Using gtk_css_provider_load_from_data() with the return value 273 * from this function on a new provider created with 274 * gtk_css_provider_new() will basically create a duplicate of 275 * this @provider. 276 * 277 * Returns: a new string representing the @provider. 278 * 279 * Since: 3.2 280 */ 281 public override string toString() 282 { 283 auto retStr = gtk_css_provider_to_string(gtkCssProvider); 284 285 scope(exit) Str.freeString(retStr); 286 return Str.toString(retStr); 287 } 288 289 /** 290 * Signals that a parsing error occurred. the @path, @line and @position 291 * describe the actual location of the error as accurately as possible. 292 * 293 * Parsing errors are never fatal, so the parsing will resume after 294 * the error. Errors may however cause parts of the given 295 * data or even all of it to not be parsed at all. So it is a useful idea 296 * to check that the parsing succeeds by connecting to this signal. 297 * 298 * Note that this signal may be emitted at any time as the css provider 299 * may opt to defer parsing parts or all of the input to a later time 300 * than when a loading function was called. 301 * 302 * Params: 303 * section = section the error happened in 304 * error = The parsing error 305 */ 306 gulong addOnParsingError(void delegate(CssSection, ErrorG, CssProvider) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 307 { 308 return Signals.connect(this, "parsing-error", dlg, connectFlags ^ ConnectFlags.SWAPPED); 309 } 310 }