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.StyleContext;
26 
27 private import gdk.Display;
28 private import gdk.RGBA;
29 private import glib.MemorySlice;
30 private import glib.Str;
31 private import glib.c.functions;
32 private import gobject.ObjectG;
33 private import gtk.Border;
34 private import gtk.StyleProviderIF;
35 private import gtk.c.functions;
36 public  import gtk.c.types;
37 
38 
39 /**
40  * `GtkStyleContext` stores styling information affecting a widget.
41  * 
42  * In order to construct the final style information, `GtkStyleContext`
43  * queries information from all attached `GtkStyleProviders`. Style
44  * providers can be either attached explicitly to the context through
45  * [method@Gtk.StyleContext.add_provider], or to the display through
46  * [func@Gtk.StyleContext.add_provider_for_display]. The resulting
47  * style is a combination of all providers’ information in priority order.
48  * 
49  * For GTK widgets, any `GtkStyleContext` returned by
50  * [method@Gtk.Widget.get_style_context] will already have a `GdkDisplay`
51  * and RTL/LTR information set. The style context will also be updated
52  * automatically if any of these settings change on the widget.
53  * 
54  * # Style Classes
55  * 
56  * Widgets can add style classes to their context, which can be used to associate
57  * different styles by class. The documentation for individual widgets lists
58  * which style classes it uses itself, and which style classes may be added by
59  * applications to affect their appearance.
60  * 
61  * # Custom styling in UI libraries and applications
62  * 
63  * If you are developing a library with custom widgets that render differently
64  * than standard components, you may need to add a `GtkStyleProvider` yourself
65  * with the %GTK_STYLE_PROVIDER_PRIORITY_FALLBACK priority, either a
66  * `GtkCssProvider` or a custom object implementing the `GtkStyleProvider`
67  * interface. This way themes may still attempt to style your UI elements in
68  * a different way if needed so.
69  * 
70  * If you are using custom styling on an applications, you probably want then
71  * to make your style information prevail to the theme’s, so you must use
72  * a `GtkStyleProvider` with the %GTK_STYLE_PROVIDER_PRIORITY_APPLICATION
73  * priority, keep in mind that the user settings in
74  * `XDG_CONFIG_HOME/gtk-4.0/gtk.css` will
75  * still take precedence over your changes, as it uses the
76  * %GTK_STYLE_PROVIDER_PRIORITY_USER priority.
77  */
78 public class StyleContext : ObjectG
79 {
80 	/** the main Gtk struct */
81 	protected GtkStyleContext* gtkStyleContext;
82 
83 	/** Get the main Gtk struct */
84 	public GtkStyleContext* getStyleContextStruct(bool transferOwnership = false)
85 	{
86 		if (transferOwnership)
87 			ownedRef = false;
88 		return gtkStyleContext;
89 	}
90 
91 	/** the main Gtk struct as a void* */
92 	protected override void* getStruct()
93 	{
94 		return cast(void*)gtkStyleContext;
95 	}
96 
97 	/**
98 	 * Sets our main struct and passes it to the parent class.
99 	 */
100 	public this (GtkStyleContext* gtkStyleContext, bool ownedRef = false)
101 	{
102 		this.gtkStyleContext = gtkStyleContext;
103 		super(cast(GObject*)gtkStyleContext, ownedRef);
104 	}
105 
106 
107 	/** */
108 	public static GType getType()
109 	{
110 		return gtk_style_context_get_type();
111 	}
112 
113 	/**
114 	 * Adds a global style provider to @display, which will be used
115 	 * in style construction for all `GtkStyleContexts` under @display.
116 	 *
117 	 * GTK uses this to make styling information from `GtkSettings`
118 	 * available.
119 	 *
120 	 * Note: If both priorities are the same, A `GtkStyleProvider`
121 	 * added through [method@Gtk.StyleContext.add_provider] takes
122 	 * precedence over another added through this function.
123 	 *
124 	 * Params:
125 	 *     display = a `GdkDisplay`
126 	 *     provider = a `GtkStyleProvider`
127 	 *     priority = the priority of the style provider. The lower
128 	 *         it is, the earlier it will be used in the style construction.
129 	 *         Typically this will be in the range between
130 	 *         %GTK_STYLE_PROVIDER_PRIORITY_FALLBACK and
131 	 *         %GTK_STYLE_PROVIDER_PRIORITY_USER
132 	 */
133 	public static void addProviderForDisplay(Display display, StyleProviderIF provider, uint priority)
134 	{
135 		gtk_style_context_add_provider_for_display((display is null) ? null : display.getDisplayStruct(), (provider is null) ? null : provider.getStyleProviderStruct(), priority);
136 	}
137 
138 	/**
139 	 * Removes @provider from the global style providers list in @display.
140 	 *
141 	 * Params:
142 	 *     display = a `GdkDisplay`
143 	 *     provider = a `GtkStyleProvider`
144 	 */
145 	public static void removeProviderForDisplay(Display display, StyleProviderIF provider)
146 	{
147 		gtk_style_context_remove_provider_for_display((display is null) ? null : display.getDisplayStruct(), (provider is null) ? null : provider.getStyleProviderStruct());
148 	}
149 
150 	/**
151 	 * Adds a style class to @context, so later uses of the
152 	 * style context will make use of this new class for styling.
153 	 *
154 	 * In the CSS file format, a `GtkEntry` defining a “search”
155 	 * class, would be matched by:
156 	 *
157 	 * ```css
158 	 * entry.search { ... }
159 	 * ```
160 	 *
161 	 * While any widget defining a “search” class would be
162 	 * matched by:
163 	 * ```css
164 	 * .search { ... }
165 	 * ```
166 	 *
167 	 * Params:
168 	 *     className = class name to use in styling
169 	 */
170 	public void addClass(string className)
171 	{
172 		gtk_style_context_add_class(gtkStyleContext, Str.toStringz(className));
173 	}
174 
175 	/**
176 	 * Adds a style provider to @context, to be used in style construction.
177 	 *
178 	 * Note that a style provider added by this function only affects
179 	 * the style of the widget to which @context belongs. If you want
180 	 * to affect the style of all widgets, use
181 	 * [func@Gtk.StyleContext.add_provider_for_display].
182 	 *
183 	 * Note: If both priorities are the same, a `GtkStyleProvider`
184 	 * added through this function takes precedence over another added
185 	 * through [func@Gtk.StyleContext.add_provider_for_display].
186 	 *
187 	 * Params:
188 	 *     provider = a `GtkStyleProvider`
189 	 *     priority = the priority of the style provider. The lower
190 	 *         it is, the earlier it will be used in the style construction.
191 	 *         Typically this will be in the range between
192 	 *         %GTK_STYLE_PROVIDER_PRIORITY_FALLBACK and
193 	 *         %GTK_STYLE_PROVIDER_PRIORITY_USER
194 	 */
195 	public void addProvider(StyleProviderIF provider, uint priority)
196 	{
197 		gtk_style_context_add_provider(gtkStyleContext, (provider is null) ? null : provider.getStyleProviderStruct(), priority);
198 	}
199 
200 	/**
201 	 * Gets the border for a given state as a `GtkBorder`.
202 	 *
203 	 * Params:
204 	 *     border = return value for the border settings
205 	 */
206 	public void getBorder(out Border border)
207 	{
208 		GtkBorder* outborder = sliceNew!GtkBorder();
209 
210 		gtk_style_context_get_border(gtkStyleContext, outborder);
211 
212 		border = ObjectG.getDObject!(Border)(outborder, true);
213 	}
214 
215 	/**
216 	 * Gets the foreground color for a given state.
217 	 *
218 	 * Params:
219 	 *     color = return value for the foreground color
220 	 */
221 	public void getColor(out RGBA color)
222 	{
223 		GdkRGBA* outcolor = sliceNew!GdkRGBA();
224 
225 		gtk_style_context_get_color(gtkStyleContext, outcolor);
226 
227 		color = ObjectG.getDObject!(RGBA)(outcolor, true);
228 	}
229 
230 	/**
231 	 * Returns the `GdkDisplay` to which @context is attached.
232 	 *
233 	 * Returns: a `GdkDisplay`.
234 	 */
235 	public Display getDisplay()
236 	{
237 		auto __p = gtk_style_context_get_display(gtkStyleContext);
238 
239 		if(__p is null)
240 		{
241 			return null;
242 		}
243 
244 		return ObjectG.getDObject!(Display)(cast(GdkDisplay*) __p);
245 	}
246 
247 	/**
248 	 * Gets the margin for a given state as a `GtkBorder`.
249 	 *
250 	 * Params:
251 	 *     margin = return value for the margin settings
252 	 */
253 	public void getMargin(out Border margin)
254 	{
255 		GtkBorder* outmargin = sliceNew!GtkBorder();
256 
257 		gtk_style_context_get_margin(gtkStyleContext, outmargin);
258 
259 		margin = ObjectG.getDObject!(Border)(outmargin, true);
260 	}
261 
262 	/**
263 	 * Gets the padding for a given state as a `GtkBorder`.
264 	 *
265 	 * Params:
266 	 *     padding = return value for the padding settings
267 	 */
268 	public void getPadding(out Border padding)
269 	{
270 		GtkBorder* outpadding = sliceNew!GtkBorder();
271 
272 		gtk_style_context_get_padding(gtkStyleContext, outpadding);
273 
274 		padding = ObjectG.getDObject!(Border)(outpadding, true);
275 	}
276 
277 	/**
278 	 * Returns the scale used for assets.
279 	 *
280 	 * Returns: the scale
281 	 */
282 	public int getScale()
283 	{
284 		return gtk_style_context_get_scale(gtkStyleContext);
285 	}
286 
287 	/**
288 	 * Returns the state used for style matching.
289 	 *
290 	 * This method should only be used to retrieve the `GtkStateFlags`
291 	 * to pass to `GtkStyleContext` methods, like
292 	 * [method@Gtk.StyleContext.get_padding].
293 	 * If you need to retrieve the current state of a `GtkWidget`, use
294 	 * [method@Gtk.Widget.get_state_flags].
295 	 *
296 	 * Returns: the state flags
297 	 */
298 	public GtkStateFlags getState()
299 	{
300 		return gtk_style_context_get_state(gtkStyleContext);
301 	}
302 
303 	/**
304 	 * Returns %TRUE if @context currently has defined the
305 	 * given class name.
306 	 *
307 	 * Params:
308 	 *     className = a class name
309 	 *
310 	 * Returns: %TRUE if @context has @class_name defined
311 	 */
312 	public bool hasClass(string className)
313 	{
314 		return gtk_style_context_has_class(gtkStyleContext, Str.toStringz(className)) != 0;
315 	}
316 
317 	/**
318 	 * Looks up and resolves a color name in the @context color map.
319 	 *
320 	 * Params:
321 	 *     colorName = color name to lookup
322 	 *     color = Return location for the looked up color
323 	 *
324 	 * Returns: %TRUE if @color_name was found and resolved, %FALSE otherwise
325 	 */
326 	public bool lookupColor(string colorName, out RGBA color)
327 	{
328 		GdkRGBA* outcolor = sliceNew!GdkRGBA();
329 
330 		auto __p = gtk_style_context_lookup_color(gtkStyleContext, Str.toStringz(colorName), outcolor) != 0;
331 
332 		color = ObjectG.getDObject!(RGBA)(outcolor, true);
333 
334 		return __p;
335 	}
336 
337 	/**
338 	 * Removes @class_name from @context.
339 	 *
340 	 * Params:
341 	 *     className = class name to remove
342 	 */
343 	public void removeClass(string className)
344 	{
345 		gtk_style_context_remove_class(gtkStyleContext, Str.toStringz(className));
346 	}
347 
348 	/**
349 	 * Removes @provider from the style providers list in @context.
350 	 *
351 	 * Params:
352 	 *     provider = a `GtkStyleProvider`
353 	 */
354 	public void removeProvider(StyleProviderIF provider)
355 	{
356 		gtk_style_context_remove_provider(gtkStyleContext, (provider is null) ? null : provider.getStyleProviderStruct());
357 	}
358 
359 	/**
360 	 * Restores @context state to a previous stage.
361 	 *
362 	 * See [method@Gtk.StyleContext.save].
363 	 */
364 	public void restore()
365 	{
366 		gtk_style_context_restore(gtkStyleContext);
367 	}
368 
369 	/**
370 	 * Saves the @context state.
371 	 *
372 	 * This allows temporary modifications done through
373 	 * [method@Gtk.StyleContext.add_class],
374 	 * [method@Gtk.StyleContext.remove_class],
375 	 * [method@Gtk.StyleContext.set_state] to be quickly
376 	 * reverted in one go through [method@Gtk.StyleContext.restore].
377 	 *
378 	 * The matching call to [method@Gtk.StyleContext.restore]
379 	 * must be done before GTK returns to the main loop.
380 	 */
381 	public void save()
382 	{
383 		gtk_style_context_save(gtkStyleContext);
384 	}
385 
386 	/**
387 	 * Attaches @context to the given display.
388 	 *
389 	 * The display is used to add style information from “global”
390 	 * style providers, such as the display's `GtkSettings` instance.
391 	 *
392 	 * If you are using a `GtkStyleContext` returned from
393 	 * [method@Gtk.Widget.get_style_context], you do not need to
394 	 * call this yourself.
395 	 *
396 	 * Params:
397 	 *     display = a `GdkDisplay`
398 	 */
399 	public void setDisplay(Display display)
400 	{
401 		gtk_style_context_set_display(gtkStyleContext, (display is null) ? null : display.getDisplayStruct());
402 	}
403 
404 	/**
405 	 * Sets the scale to use when getting image assets for the style.
406 	 *
407 	 * Params:
408 	 *     scale = scale
409 	 */
410 	public void setScale(int scale)
411 	{
412 		gtk_style_context_set_scale(gtkStyleContext, scale);
413 	}
414 
415 	/**
416 	 * Sets the state to be used for style matching.
417 	 *
418 	 * Params:
419 	 *     flags = state to represent
420 	 */
421 	public void setState(GtkStateFlags flags)
422 	{
423 		gtk_style_context_set_state(gtkStyleContext, flags);
424 	}
425 
426 	/**
427 	 * Converts the style context into a string representation.
428 	 *
429 	 * The string representation always includes information about
430 	 * the name, state, id, visibility and style classes of the CSS
431 	 * node that is backing @context. Depending on the flags, more
432 	 * information may be included.
433 	 *
434 	 * This function is intended for testing and debugging of the
435 	 * CSS implementation in GTK. There are no guarantees about
436 	 * the format of the returned string, it may change.
437 	 *
438 	 * Params:
439 	 *     flags = Flags that determine what to print
440 	 *
441 	 * Returns: a newly allocated string representing @context
442 	 */
443 	public string toString(GtkStyleContextPrintFlags flags)
444 	{
445 		auto retStr = gtk_style_context_to_string(gtkStyleContext, flags);
446 
447 		scope(exit) Str.freeString(retStr);
448 		return Str.toString(retStr);
449 	}
450 }