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