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.Str; 31 private import glib.c.functions; 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 private import std.algorithm; 40 41 42 /** 43 * `GtkCssProvider` is an object implementing the `GtkStyleProvider` interface 44 * for CSS. 45 * 46 * It is able to parse CSS-like input in order to style widgets. 47 * 48 * An application can make GTK parse a specific CSS style sheet by calling 49 * [method@Gtk.CssProvider.load_from_file] or 50 * [method@Gtk.CssProvider.load_from_resource] 51 * and adding the provider with [method@Gtk.StyleContext.add_provider] or 52 * [func@Gtk.StyleContext.add_provider_for_display]. 53 * 54 * In addition, certain files will be read when GTK is initialized. 55 * First, the file `$XDG_CONFIG_HOME/gtk-4.0/gtk.css` is loaded if it 56 * exists. Then, GTK loads the first existing file among 57 * `XDG_DATA_HOME/themes/THEME/gtk-VERSION/gtk-VARIANT.css`, 58 * `$HOME/.themes/THEME/gtk-VERSION/gtk-VARIANT.css`, 59 * `$XDG_DATA_DIRS/themes/THEME/gtk-VERSION/gtk-VARIANT.css` and 60 * `DATADIR/share/themes/THEME/gtk-VERSION/gtk-VARIANT.css`, 61 * where `THEME` is the name of the current theme (see the 62 * [property@Gtk.Settings:gtk-theme-name] setting), `VARIANT` is the 63 * variant to load (see the 64 * [property@Gtk.Settings:gtk-application-prefer-dark-theme] setting), 65 * `DATADIR` is the prefix configured when GTK was compiled (unless 66 * overridden by the `GTK_DATA_PREFIX` environment variable), and 67 * `VERSION` is the GTK version number. If no file is found for the 68 * current version, GTK tries older versions all the way back to 4.0. 69 * 70 * To track errors while loading CSS, connect to the 71 * [signal@Gtk.CssProvider::parsing-error] signal. 72 */ 73 public class CssProvider : ObjectG, StyleProviderIF 74 { 75 /** the main Gtk struct */ 76 protected GtkCssProvider* gtkCssProvider; 77 78 /** Get the main Gtk struct */ 79 public GtkCssProvider* getCssProviderStruct(bool transferOwnership = false) 80 { 81 if (transferOwnership) 82 ownedRef = false; 83 return gtkCssProvider; 84 } 85 86 /** the main Gtk struct as a void* */ 87 protected override void* getStruct() 88 { 89 return cast(void*)gtkCssProvider; 90 } 91 92 /** 93 * Sets our main struct and passes it to the parent class. 94 */ 95 public this (GtkCssProvider* gtkCssProvider, bool ownedRef = false) 96 { 97 this.gtkCssProvider = gtkCssProvider; 98 super(cast(GObject*)gtkCssProvider, ownedRef); 99 } 100 101 // add the StyleProvider capabilities 102 mixin StyleProviderT!(GtkCssProvider); 103 104 105 /** */ 106 public static GType getType() 107 { 108 return gtk_css_provider_get_type(); 109 } 110 111 /** 112 * Returns a newly created `GtkCssProvider`. 113 * 114 * Returns: A new `GtkCssProvider` 115 * 116 * Throws: ConstructionException GTK+ fails to create the object. 117 */ 118 public this() 119 { 120 auto __p = gtk_css_provider_new(); 121 122 if(__p is null) 123 { 124 throw new ConstructionException("null returned by new"); 125 } 126 127 this(cast(GtkCssProvider*) __p, true); 128 } 129 130 /** 131 * Loads @data into @css_provider. 132 * 133 * This clears any previously loaded information. 134 * 135 * Params: 136 * data = CSS data loaded in memory 137 */ 138 public void loadFromData(string data) 139 { 140 gtk_css_provider_load_from_data(gtkCssProvider, Str.toStringz(data), cast(ptrdiff_t)data.length); 141 } 142 143 /** 144 * Loads the data contained in @file into @css_provider. 145 * 146 * This clears any previously loaded information. 147 * 148 * Params: 149 * file = `GFile` pointing to a file to load 150 */ 151 public void loadFromFile(FileIF file) 152 { 153 gtk_css_provider_load_from_file(gtkCssProvider, (file is null) ? null : file.getFileStruct()); 154 } 155 156 /** 157 * Loads the data contained in @path into @css_provider. 158 * 159 * This clears any previously loaded information. 160 * 161 * Params: 162 * path = the path of a filename to load, in the GLib filename encoding 163 */ 164 public void loadFromPath(string path) 165 { 166 gtk_css_provider_load_from_path(gtkCssProvider, Str.toStringz(path)); 167 } 168 169 /** 170 * Loads the data contained in the resource at @resource_path into 171 * the @css_provider. 172 * 173 * This clears any previously loaded information. 174 * 175 * Params: 176 * resourcePath = a `GResource` resource path 177 */ 178 public void loadFromResource(string resourcePath) 179 { 180 gtk_css_provider_load_from_resource(gtkCssProvider, Str.toStringz(resourcePath)); 181 } 182 183 /** 184 * Loads a theme from the usual theme paths. 185 * 186 * The actual process of finding the theme might change between 187 * releases, but it is guaranteed that this function uses the same 188 * mechanism to load the theme that GTK uses for loading its own theme. 189 * 190 * Params: 191 * name = A theme name 192 * variant = variant to load, for example, "dark", or 193 * %NULL for the default 194 */ 195 public void loadNamed(string name, string variant) 196 { 197 gtk_css_provider_load_named(gtkCssProvider, Str.toStringz(name), Str.toStringz(variant)); 198 } 199 200 /** 201 * Converts the @provider into a string representation in CSS 202 * format. 203 * 204 * Using [method@Gtk.CssProvider.load_from_data] with the return 205 * value from this function on a new provider created with 206 * [ctor@Gtk.CssProvider.new] will basically create a duplicate 207 * of this @provider. 208 * 209 * Returns: a new string representing the @provider. 210 */ 211 public override string toString() 212 { 213 auto retStr = gtk_css_provider_to_string(gtkCssProvider); 214 215 scope(exit) Str.freeString(retStr); 216 return Str.toString(retStr); 217 } 218 219 /** 220 * Signals that a parsing error occurred. 221 * 222 * The @path, @line and @position describe the actual location of 223 * the error as accurately as possible. 224 * 225 * Parsing errors are never fatal, so the parsing will resume after 226 * the error. Errors may however cause parts of the given data or 227 * even all of it to not be parsed at all. So it is a useful idea 228 * to check that the parsing succeeds by connecting to this signal. 229 * 230 * Note that this signal may be emitted at any time as the css provider 231 * may opt to defer parsing parts or all of the input to a later time 232 * than when a loading function was called. 233 * 234 * Params: 235 * section = section the error happened in 236 * error = The parsing error 237 */ 238 gulong addOnParsingError(void delegate(CssSection, ErrorG, CssProvider) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 239 { 240 return Signals.connect(this, "parsing-error", dlg, connectFlags ^ ConnectFlags.SWAPPED); 241 } 242 }