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-name/gtk-VERSION/gtk.css`,
56  * `$HOME/.themes/theme-name/gtk-VERSION/gtk.css`,
57  * `$XDG_DATA_DIRS/themes/theme-name/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 	protected override void setStruct(GObject* obj)
88 	{
89 		gtkCssProvider = cast(GtkCssProvider*)obj;
90 		super.setStruct(obj);
91 	}
92 
93 	/**
94 	 * Sets our main struct and passes it to the parent class.
95 	 */
96 	public this (GtkCssProvider* gtkCssProvider, bool ownedRef = false)
97 	{
98 		this.gtkCssProvider = gtkCssProvider;
99 		super(cast(GObject*)gtkCssProvider, ownedRef);
100 	}
101 
102 	// add the StyleProvider capabilities
103 	mixin StyleProviderT!(GtkCssProvider);
104 
105 
106 	/** */
107 	public static GType getType()
108 	{
109 		return gtk_css_provider_get_type();
110 	}
111 
112 	/**
113 	 * Returns a newly created #GtkCssProvider.
114 	 *
115 	 * Returns: A new #GtkCssProvider
116 	 *
117 	 * Throws: ConstructionException GTK+ fails to create the object.
118 	 */
119 	public this()
120 	{
121 		auto p = gtk_css_provider_new();
122 
123 		if(p is null)
124 		{
125 			throw new ConstructionException("null returned by new");
126 		}
127 
128 		this(cast(GtkCssProvider*) p, true);
129 	}
130 
131 	/**
132 	 * Returns the provider containing the style settings used as a
133 	 * fallback for all widgets.
134 	 *
135 	 * Returns: The provider used for fallback styling.
136 	 *     This memory is owned by GTK+, and you must not free it.
137 	 */
138 	public static CssProvider getDefault()
139 	{
140 		auto p = gtk_css_provider_get_default();
141 
142 		if(p is null)
143 		{
144 			return null;
145 		}
146 
147 		return ObjectG.getDObject!(CssProvider)(cast(GtkCssProvider*) p);
148 	}
149 
150 	/**
151 	 * Loads a theme from the usual theme paths
152 	 *
153 	 * Params:
154 	 *     name = A theme name
155 	 *     variant = variant to load, for example, "dark", or
156 	 *         %NULL for the default
157 	 *
158 	 * Returns: a #GtkCssProvider with the theme loaded.
159 	 *     This memory is owned by GTK+, and you must not free it.
160 	 */
161 	public static CssProvider getNamed(string name, string variant)
162 	{
163 		auto p = gtk_css_provider_get_named(Str.toStringz(name), Str.toStringz(variant));
164 
165 		if(p is null)
166 		{
167 			return null;
168 		}
169 
170 		return ObjectG.getDObject!(CssProvider)(cast(GtkCssProvider*) p);
171 	}
172 
173 	/**
174 	 * Loads @data into @css_provider, and by doing so clears any previously loaded
175 	 * information.
176 	 *
177 	 * Params:
178 	 *     data = CSS data loaded in memory
179 	 *
180 	 * Returns: %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 	 * Returns: %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 	 * Returns: %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 	 * Returns: 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 	protected class OnParsingErrorDelegateWrapper
296 	{
297 		void delegate(CssSection, ErrorG, CssProvider) dlg;
298 		gulong handlerId;
299 
300 		this(void delegate(CssSection, ErrorG, CssProvider) dlg)
301 		{
302 			this.dlg = dlg;
303 			onParsingErrorListeners ~= this;
304 		}
305 
306 		void remove(OnParsingErrorDelegateWrapper source)
307 		{
308 			foreach(index, wrapper; onParsingErrorListeners)
309 			{
310 				if (wrapper.handlerId == source.handlerId)
311 				{
312 					onParsingErrorListeners[index] = null;
313 					onParsingErrorListeners = std.algorithm.remove(onParsingErrorListeners, index);
314 					break;
315 				}
316 			}
317 		}
318 	}
319 	OnParsingErrorDelegateWrapper[] onParsingErrorListeners;
320 
321 	/**
322 	 * Signals that a parsing error occurred. the @path, @line and @position
323 	 * describe the actual location of the error as accurately as possible.
324 	 *
325 	 * Parsing errors are never fatal, so the parsing will resume after
326 	 * the error. Errors may however cause parts of the given
327 	 * data or even all of it to not be parsed at all. So it is a useful idea
328 	 * to check that the parsing succeeds by connecting to this signal.
329 	 *
330 	 * Note that this signal may be emitted at any time as the css provider
331 	 * may opt to defer parsing parts or all of the input to a later time
332 	 * than when a loading function was called.
333 	 *
334 	 * Params:
335 	 *     section = section the error happened in
336 	 *     error = The parsing error
337 	 */
338 	gulong addOnParsingError(void delegate(CssSection, ErrorG, CssProvider) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
339 	{
340 		auto wrapper = new OnParsingErrorDelegateWrapper(dlg);
341 		wrapper.handlerId = Signals.connectData(
342 			this,
343 			"parsing-error",
344 			cast(GCallback)&callBackParsingError,
345 			cast(void*)wrapper,
346 			cast(GClosureNotify)&callBackParsingErrorDestroy,
347 			connectFlags);
348 		return wrapper.handlerId;
349 	}
350 
351 	extern(C) static void callBackParsingError(GtkCssProvider* cssproviderStruct, GtkCssSection* section, GError* error, OnParsingErrorDelegateWrapper wrapper)
352 	{
353 		wrapper.dlg(ObjectG.getDObject!(CssSection)(section), new ErrorG(error), wrapper.outer);
354 	}
355 
356 	extern(C) static void callBackParsingErrorDestroy(OnParsingErrorDelegateWrapper wrapper, GClosure* closure)
357 	{
358 		wrapper.remove(wrapper);
359 	}
360 }