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_resouce()
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+ tries
53  * to load `$HOME/.themes/theme-name/gtk-3.0/gtk.css`, falling back to
54  * `DATADIR/share/themes/THEME/gtk-VERSION/gtk.css`, where THEME is the name of
55  * the current theme (see the #GtkSettings:gtk-theme-name setting), DATADIR
56  * is the prefix configured when GTK+ was compiled, unless overridden by the
57  * `GTK_DATA_PREFIX` environment variable, and VERSION is the GTK+ version number.
58  * If no file is found for the current version, GTK+ tries older versions all the
59  * way back to 3.0.
60  * 
61  * In the same way, GTK+ tries to load a gtk-keys.css file for the current
62  * key theme, as defined by #GtkSettings:gtk-key-theme-name.
63  */
64 public class CssProvider : ObjectG, StyleProviderIF
65 {
66 	/** the main Gtk struct */
67 	protected GtkCssProvider* gtkCssProvider;
68 
69 	/** Get the main Gtk struct */
70 	public GtkCssProvider* getCssProviderStruct()
71 	{
72 		return gtkCssProvider;
73 	}
74 
75 	/** the main Gtk struct as a void* */
76 	protected override void* getStruct()
77 	{
78 		return cast(void*)gtkCssProvider;
79 	}
80 
81 	protected override void setStruct(GObject* obj)
82 	{
83 		gtkCssProvider = cast(GtkCssProvider*)obj;
84 		super.setStruct(obj);
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 	 * Return: 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 	 * Return: 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 	 * Return: 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 	 *     length = the length of @data in bytes, or -1 for NUL terminated strings. If
174 	 *         @length is not -1, the code will assume it is not NUL terminated and will
175 	 *         potentially do a copy.
176 	 *
177 	 * Return: %TRUE. The return value is deprecated and %FALSE will only be
178 	 *     returned for backwards compatibility reasons if an @error is not
179 	 *     %NULL and a loading error occured. To track errors while loading
180 	 *     CSS, connect to the #GtkCssProvider::parsing-error signal.
181 	 *
182 	 * Throws: GException on failure.
183 	 */
184 	public bool loadFromData(string data)
185 	{
186 		GError* err = null;
187 		
188 		auto p = gtk_css_provider_load_from_data(gtkCssProvider, Str.toStringz(data), cast(ptrdiff_t)data.length, &err) != 0;
189 		
190 		if (err !is null)
191 		{
192 			throw new GException( new ErrorG(err) );
193 		}
194 		
195 		return p;
196 	}
197 
198 	/**
199 	 * Loads the data contained in @file into @css_provider, making it
200 	 * clear any previously loaded information.
201 	 *
202 	 * Params:
203 	 *     file = #GFile pointing to a file to load
204 	 *
205 	 * Return: %TRUE. The return value is deprecated and %FALSE will only be
206 	 *     returned for backwards compatibility reasons if an @error is not
207 	 *     %NULL and a loading error occured. To track errors while loading
208 	 *     CSS, connect to the #GtkCssProvider::parsing-error signal.
209 	 *
210 	 * Throws: GException on failure.
211 	 */
212 	public bool loadFromFile(FileIF file)
213 	{
214 		GError* err = null;
215 		
216 		auto p = gtk_css_provider_load_from_file(gtkCssProvider, (file is null) ? null : file.getFileStruct(), &err) != 0;
217 		
218 		if (err !is null)
219 		{
220 			throw new GException( new ErrorG(err) );
221 		}
222 		
223 		return p;
224 	}
225 
226 	/**
227 	 * Loads the data contained in @path into @css_provider, making it clear
228 	 * any previously loaded information.
229 	 *
230 	 * Params:
231 	 *     path = the path of a filename to load, in the GLib filename encoding
232 	 *
233 	 * Return: %TRUE. The return value is deprecated and %FALSE will only be
234 	 *     returned for backwards compatibility reasons if an @error is not
235 	 *     %NULL and a loading error occured. To track errors while loading
236 	 *     CSS, connect to the #GtkCssProvider::parsing-error signal.
237 	 *
238 	 * Throws: GException on failure.
239 	 */
240 	public bool loadFromPath(string path)
241 	{
242 		GError* err = null;
243 		
244 		auto p = gtk_css_provider_load_from_path(gtkCssProvider, Str.toStringz(path), &err) != 0;
245 		
246 		if (err !is null)
247 		{
248 			throw new GException( new ErrorG(err) );
249 		}
250 		
251 		return p;
252 	}
253 
254 	/**
255 	 * Loads the data contained in the resource at @resource_path into
256 	 * the #GtkCssProvider, clearing any previously loaded information.
257 	 *
258 	 * To track errors while loading CSS, connect to the
259 	 * #GtkCssProvider::parsing-error signal.
260 	 *
261 	 * Params:
262 	 *     resourcePath = a #GResource resource path
263 	 *
264 	 * Since: 3.16
265 	 */
266 	public void loadFromResource(string resourcePath)
267 	{
268 		gtk_css_provider_load_from_resource(gtkCssProvider, Str.toStringz(resourcePath));
269 	}
270 
271 	/**
272 	 * Converts the @provider into a string representation in CSS
273 	 * format.
274 	 *
275 	 * Using gtk_css_provider_load_from_data() with the return value
276 	 * from this function on a new provider created with
277 	 * gtk_css_provider_new() will basically create a duplicate of
278 	 * this @provider.
279 	 *
280 	 * Return: a new string representing the @provider.
281 	 *
282 	 * Since: 3.2
283 	 */
284 	public override string toString()
285 	{
286 		auto retStr = gtk_css_provider_to_string(gtkCssProvider);
287 		
288 		scope(exit) Str.freeString(retStr);
289 		return Str.toString(retStr);
290 	}
291 
292 	int[string] connectedSignals;
293 
294 	void delegate(CssSection, ErrorG, CssProvider)[] onParsingErrorListeners;
295 	/**
296 	 * Signals that a parsing error occured. the @path, @line and @position
297 	 * describe the actual location of the error as accurately as possible.
298 	 *
299 	 * Parsing errors are never fatal, so the parsing will resume after
300 	 * the error. Errors may however cause parts of the given
301 	 * data or even all of it to not be parsed at all. So it is a useful idea
302 	 * to check that the parsing succeeds by connecting to this signal.
303 	 *
304 	 * Note that this signal may be emitted at any time as the css provider
305 	 * may opt to defer parsing parts or all of the input to a later time
306 	 * than when a loading function was called.
307 	 *
308 	 * Params:
309 	 *     section = section the error happened in
310 	 *     error = The parsing error
311 	 */
312 	void addOnParsingError(void delegate(CssSection, ErrorG, CssProvider) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
313 	{
314 		if ( "parsing-error" !in connectedSignals )
315 		{
316 			Signals.connectData(
317 				this,
318 				"parsing-error",
319 				cast(GCallback)&callBackParsingError,
320 				cast(void*)this,
321 				null,
322 				connectFlags);
323 			connectedSignals["parsing-error"] = 1;
324 		}
325 		onParsingErrorListeners ~= dlg;
326 	}
327 	extern(C) static void callBackParsingError(GtkCssProvider* cssproviderStruct, GtkCssSection* section, GError* error, CssProvider _cssprovider)
328 	{
329 		foreach ( void delegate(CssSection, ErrorG, CssProvider) dlg; _cssprovider.onParsingErrorListeners )
330 		{
331 			dlg(ObjectG.getDObject!(CssSection)(section), new ErrorG(error), _cssprovider);
332 		}
333 	}
334 }