gtk4/auto/
style_context.rs

1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// DO NOT EDIT
4#![allow(deprecated)]
5
6use crate::{ffi, Border, StateFlags, StyleContextPrintFlags, StyleProvider};
7use glib::{
8    prelude::*,
9    signal::{connect_raw, SignalHandlerId},
10    translate::*,
11};
12use std::boxed::Box as Box_;
13
14glib::wrapper! {
15    /// The relevant API has been moved to [`Widget`][crate::Widget]
16    ///   where applicable; otherwise, there is no replacement for querying the
17    ///   style machinery. Stylable UI elements should use widgets.
18    /// [`StyleContext`][crate::StyleContext] stores styling information affecting a widget.
19    ///
20    /// In order to construct the final style information, [`StyleContext`][crate::StyleContext]
21    /// queries information from all attached `GtkStyleProviders`. Style
22    /// providers can be either attached explicitly to the context through
23    /// [`StyleContextExt::add_provider()`][crate::prelude::StyleContextExt::add_provider()], or to the display through
24    /// [`add_provider_for_display()`][Self::add_provider_for_display()]. The resulting
25    /// style is a combination of all providers’ information in priority order.
26    ///
27    /// For GTK widgets, any [`StyleContext`][crate::StyleContext] returned by
28    /// [`WidgetExt::style_context()`][crate::prelude::WidgetExt::style_context()] will already have a [`gdk::Display`][crate::gdk::Display]
29    /// and RTL/LTR information set. The style context will also be updated
30    /// automatically if any of these settings change on the widget.
31    ///
32    /// ## Style Classes
33    ///
34    /// Widgets can add style classes to their context, which can be used to associate
35    /// different styles by class. The documentation for individual widgets lists
36    /// which style classes it uses itself, and which style classes may be added by
37    /// applications to affect their appearance.
38    ///
39    /// # Custom styling in UI libraries and applications
40    ///
41    /// If you are developing a library with custom widgets that render differently
42    /// than standard components, you may need to add a [`StyleProvider`][crate::StyleProvider] yourself
43    /// with the `GTK_STYLE_PROVIDER_PRIORITY_FALLBACK` priority, either a
44    /// [`CssProvider`][crate::CssProvider] or a custom object implementing the [`StyleProvider`][crate::StyleProvider]
45    /// interface. This way themes may still attempt to style your UI elements in
46    /// a different way if needed so.
47    ///
48    /// If you are using custom styling on an applications, you probably want then
49    /// to make your style information prevail to the theme’s, so you must use
50    /// a [`StyleProvider`][crate::StyleProvider] with the `GTK_STYLE_PROVIDER_PRIORITY_APPLICATION`
51    /// priority, keep in mind that the user settings in
52    /// `XDG_CONFIG_HOME/gtk-4.0/gtk.css` will
53    /// still take precedence over your changes, as it uses the
54    /// `GTK_STYLE_PROVIDER_PRIORITY_USER` priority.
55    ///
56    /// ## Properties
57    ///
58    ///
59    /// #### `display`
60    ///  The display of the style context.
61    ///
62    /// Readable | Writeable
63    ///
64    /// # Implements
65    ///
66    /// [`StyleContextExt`][trait@crate::prelude::StyleContextExt], [`trait@glib::ObjectExt`]
67    #[doc(alias = "GtkStyleContext")]
68    pub struct StyleContext(Object<ffi::GtkStyleContext, ffi::GtkStyleContextClass>);
69
70    match fn {
71        type_ => || ffi::gtk_style_context_get_type(),
72    }
73}
74
75impl StyleContext {
76    pub const NONE: Option<&'static StyleContext> = None;
77}
78
79mod sealed {
80    pub trait Sealed {}
81    impl<T: super::IsA<super::StyleContext>> Sealed for T {}
82}
83
84/// Trait containing all [`struct@StyleContext`] methods.
85///
86/// # Implementors
87///
88/// [`StyleContext`][struct@crate::StyleContext]
89pub trait StyleContextExt: IsA<StyleContext> + sealed::Sealed + 'static {
90    /// Adds a style class to @self, so later uses of the
91    /// style context will make use of this new class for styling.
92    ///
93    /// In the CSS file format, a [`Entry`][crate::Entry] defining a “search”
94    /// class, would be matched by:
95    ///
96    /// ```css
97    /// entry.search { ... }
98    /// ```
99    ///
100    /// While any widget defining a “search” class would be
101    /// matched by:
102    /// ```css
103    /// .search { ... }
104    /// ```
105    ///
106    /// # Deprecated since 4.10
107    ///
108    /// Use [`WidgetExt::add_css_class()`][crate::prelude::WidgetExt::add_css_class()] instead
109    /// ## `class_name`
110    /// class name to use in styling
111    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
112    #[allow(deprecated)]
113    #[doc(alias = "gtk_style_context_add_class")]
114    fn add_class(&self, class_name: &str) {
115        unsafe {
116            ffi::gtk_style_context_add_class(
117                self.as_ref().to_glib_none().0,
118                class_name.to_glib_none().0,
119            );
120        }
121    }
122
123    /// Adds a style provider to @self, to be used in style construction.
124    ///
125    /// Note that a style provider added by this function only affects
126    /// the style of the widget to which @self belongs. If you want
127    /// to affect the style of all widgets, use
128    /// [`StyleContext::add_provider_for_display()`][crate::StyleContext::add_provider_for_display()].
129    ///
130    /// Note: If both priorities are the same, a [`StyleProvider`][crate::StyleProvider]
131    /// added through this function takes precedence over another added
132    /// through [`StyleContext::add_provider_for_display()`][crate::StyleContext::add_provider_for_display()].
133    ///
134    /// # Deprecated since 4.10
135    ///
136    /// Use style classes instead
137    /// ## `provider`
138    /// a [`StyleProvider`][crate::StyleProvider]
139    /// ## `priority`
140    /// the priority of the style provider. The lower
141    ///   it is, the earlier it will be used in the style construction.
142    ///   Typically this will be in the range between
143    ///   `GTK_STYLE_PROVIDER_PRIORITY_FALLBACK` and
144    ///   `GTK_STYLE_PROVIDER_PRIORITY_USER`
145    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
146    #[allow(deprecated)]
147    #[doc(alias = "gtk_style_context_add_provider")]
148    fn add_provider(&self, provider: &impl IsA<StyleProvider>, priority: u32) {
149        unsafe {
150            ffi::gtk_style_context_add_provider(
151                self.as_ref().to_glib_none().0,
152                provider.as_ref().to_glib_none().0,
153                priority,
154            );
155        }
156    }
157
158    /// Gets the border for a given state as a [`Border`][crate::Border].
159    ///
160    /// # Deprecated since 4.10
161    ///
162    /// This api will be removed in GTK 5
163    ///
164    /// # Returns
165    ///
166    ///
167    /// ## `border`
168    /// return value for the border settings
169    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
170    #[allow(deprecated)]
171    #[doc(alias = "gtk_style_context_get_border")]
172    #[doc(alias = "get_border")]
173    fn border(&self) -> Border {
174        unsafe {
175            let mut border = Border::uninitialized();
176            ffi::gtk_style_context_get_border(
177                self.as_ref().to_glib_none().0,
178                border.to_glib_none_mut().0,
179            );
180            border
181        }
182    }
183
184    /// Gets the foreground color for a given state.
185    ///
186    /// # Deprecated since 4.10
187    ///
188    /// Use [`WidgetExt::color()`][crate::prelude::WidgetExt::color()] instead
189    ///
190    /// # Returns
191    ///
192    ///
193    /// ## `color`
194    /// return value for the foreground color
195    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
196    #[allow(deprecated)]
197    #[doc(alias = "gtk_style_context_get_color")]
198    #[doc(alias = "get_color")]
199    fn color(&self) -> gdk::RGBA {
200        unsafe {
201            let mut color = gdk::RGBA::uninitialized();
202            ffi::gtk_style_context_get_color(
203                self.as_ref().to_glib_none().0,
204                color.to_glib_none_mut().0,
205            );
206            color
207        }
208    }
209
210    /// Returns the [`gdk::Display`][crate::gdk::Display] to which @self is attached.
211    ///
212    /// # Deprecated since 4.10
213    ///
214    /// Use [`WidgetExt::display()`][crate::prelude::WidgetExt::display()] instead
215    ///
216    /// # Returns
217    ///
218    /// a [`gdk::Display`][crate::gdk::Display].
219    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
220    #[allow(deprecated)]
221    #[doc(alias = "gtk_style_context_get_display")]
222    #[doc(alias = "get_display")]
223    fn display(&self) -> gdk::Display {
224        unsafe {
225            from_glib_none(ffi::gtk_style_context_get_display(
226                self.as_ref().to_glib_none().0,
227            ))
228        }
229    }
230
231    /// Gets the margin for a given state as a [`Border`][crate::Border].
232    ///
233    /// # Deprecated since 4.10
234    ///
235    /// This api will be removed in GTK 5
236    ///
237    /// # Returns
238    ///
239    ///
240    /// ## `margin`
241    /// return value for the margin settings
242    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
243    #[allow(deprecated)]
244    #[doc(alias = "gtk_style_context_get_margin")]
245    #[doc(alias = "get_margin")]
246    fn margin(&self) -> Border {
247        unsafe {
248            let mut margin = Border::uninitialized();
249            ffi::gtk_style_context_get_margin(
250                self.as_ref().to_glib_none().0,
251                margin.to_glib_none_mut().0,
252            );
253            margin
254        }
255    }
256
257    /// Gets the padding for a given state as a [`Border`][crate::Border].
258    ///
259    /// # Deprecated since 4.10
260    ///
261    /// This api will be removed in GTK 5
262    ///
263    /// # Returns
264    ///
265    ///
266    /// ## `padding`
267    /// return value for the padding settings
268    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
269    #[allow(deprecated)]
270    #[doc(alias = "gtk_style_context_get_padding")]
271    #[doc(alias = "get_padding")]
272    fn padding(&self) -> Border {
273        unsafe {
274            let mut padding = Border::uninitialized();
275            ffi::gtk_style_context_get_padding(
276                self.as_ref().to_glib_none().0,
277                padding.to_glib_none_mut().0,
278            );
279            padding
280        }
281    }
282
283    /// Returns the scale used for assets.
284    ///
285    /// # Deprecated since 4.10
286    ///
287    /// Use [`WidgetExt::scale_factor()`][crate::prelude::WidgetExt::scale_factor()] instead
288    ///
289    /// # Returns
290    ///
291    /// the scale
292    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
293    #[allow(deprecated)]
294    #[doc(alias = "gtk_style_context_get_scale")]
295    #[doc(alias = "get_scale")]
296    fn scale(&self) -> i32 {
297        unsafe { ffi::gtk_style_context_get_scale(self.as_ref().to_glib_none().0) }
298    }
299
300    /// Returns the state used for style matching.
301    ///
302    /// This method should only be used to retrieve the [`StateFlags`][crate::StateFlags]
303    /// to pass to [`StyleContext`][crate::StyleContext] methods, like
304    /// [`padding()`][Self::padding()].
305    /// If you need to retrieve the current state of a [`Widget`][crate::Widget], use
306    /// [`WidgetExt::state_flags()`][crate::prelude::WidgetExt::state_flags()].
307    ///
308    /// # Deprecated since 4.10
309    ///
310    /// Use [`WidgetExt::state_flags()`][crate::prelude::WidgetExt::state_flags()] instead
311    ///
312    /// # Returns
313    ///
314    /// the state flags
315    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
316    #[allow(deprecated)]
317    #[doc(alias = "gtk_style_context_get_state")]
318    #[doc(alias = "get_state")]
319    fn state(&self) -> StateFlags {
320        unsafe {
321            from_glib(ffi::gtk_style_context_get_state(
322                self.as_ref().to_glib_none().0,
323            ))
324        }
325    }
326
327    /// Returns [`true`] if @self currently has defined the
328    /// given class name.
329    ///
330    /// # Deprecated since 4.10
331    ///
332    /// Use [`WidgetExt::has_css_class()`][crate::prelude::WidgetExt::has_css_class()] instead
333    /// ## `class_name`
334    /// a class name
335    ///
336    /// # Returns
337    ///
338    /// [`true`] if @self has @class_name defined
339    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
340    #[allow(deprecated)]
341    #[doc(alias = "gtk_style_context_has_class")]
342    fn has_class(&self, class_name: &str) -> bool {
343        unsafe {
344            from_glib(ffi::gtk_style_context_has_class(
345                self.as_ref().to_glib_none().0,
346                class_name.to_glib_none().0,
347            ))
348        }
349    }
350
351    /// Looks up and resolves a color name in the @self color map.
352    ///
353    /// # Deprecated since 4.10
354    ///
355    /// This api will be removed in GTK 5
356    /// ## `color_name`
357    /// color name to lookup
358    ///
359    /// # Returns
360    ///
361    /// [`true`] if @color_name was found and resolved, [`false`] otherwise
362    ///
363    /// ## `color`
364    /// Return location for the looked up color
365    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
366    #[allow(deprecated)]
367    #[doc(alias = "gtk_style_context_lookup_color")]
368    fn lookup_color(&self, color_name: &str) -> Option<gdk::RGBA> {
369        unsafe {
370            let mut color = gdk::RGBA::uninitialized();
371            let ret = from_glib(ffi::gtk_style_context_lookup_color(
372                self.as_ref().to_glib_none().0,
373                color_name.to_glib_none().0,
374                color.to_glib_none_mut().0,
375            ));
376            if ret {
377                Some(color)
378            } else {
379                None
380            }
381        }
382    }
383
384    /// Removes @class_name from @self.
385    ///
386    /// # Deprecated since 4.10
387    ///
388    /// Use [`WidgetExt::remove_css_class()`][crate::prelude::WidgetExt::remove_css_class()] instead
389    /// ## `class_name`
390    /// class name to remove
391    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
392    #[allow(deprecated)]
393    #[doc(alias = "gtk_style_context_remove_class")]
394    fn remove_class(&self, class_name: &str) {
395        unsafe {
396            ffi::gtk_style_context_remove_class(
397                self.as_ref().to_glib_none().0,
398                class_name.to_glib_none().0,
399            );
400        }
401    }
402
403    /// Removes @provider from the style providers list in @self.
404    ///
405    /// # Deprecated since 4.10
406    ///
407    /// ## `provider`
408    /// a [`StyleProvider`][crate::StyleProvider]
409    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
410    #[allow(deprecated)]
411    #[doc(alias = "gtk_style_context_remove_provider")]
412    fn remove_provider(&self, provider: &impl IsA<StyleProvider>) {
413        unsafe {
414            ffi::gtk_style_context_remove_provider(
415                self.as_ref().to_glib_none().0,
416                provider.as_ref().to_glib_none().0,
417            );
418        }
419    }
420
421    /// Restores @self state to a previous stage.
422    ///
423    /// See [`save()`][Self::save()].
424    ///
425    /// # Deprecated since 4.10
426    ///
427    /// This API will be removed in GTK 5
428    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
429    #[allow(deprecated)]
430    #[doc(alias = "gtk_style_context_restore")]
431    fn restore(&self) {
432        unsafe {
433            ffi::gtk_style_context_restore(self.as_ref().to_glib_none().0);
434        }
435    }
436
437    /// Saves the @self state.
438    ///
439    /// This allows temporary modifications done through
440    /// [`add_class()`][Self::add_class()],
441    /// [`remove_class()`][Self::remove_class()],
442    /// [`set_state()`][Self::set_state()] to be quickly
443    /// reverted in one go through [`restore()`][Self::restore()].
444    ///
445    /// The matching call to [`restore()`][Self::restore()]
446    /// must be done before GTK returns to the main loop.
447    ///
448    /// # Deprecated since 4.10
449    ///
450    /// This API will be removed in GTK 5
451    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
452    #[allow(deprecated)]
453    #[doc(alias = "gtk_style_context_save")]
454    fn save(&self) {
455        unsafe {
456            ffi::gtk_style_context_save(self.as_ref().to_glib_none().0);
457        }
458    }
459
460    /// Attaches @self to the given display.
461    ///
462    /// The display is used to add style information from “global”
463    /// style providers, such as the display's [`Settings`][crate::Settings] instance.
464    ///
465    /// If you are using a [`StyleContext`][crate::StyleContext] returned from
466    /// [`WidgetExt::style_context()`][crate::prelude::WidgetExt::style_context()], you do not need to
467    /// call this yourself.
468    ///
469    /// # Deprecated since 4.10
470    ///
471    /// You should not use this api
472    /// ## `display`
473    /// a [`gdk::Display`][crate::gdk::Display]
474    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
475    #[allow(deprecated)]
476    #[doc(alias = "gtk_style_context_set_display")]
477    #[doc(alias = "display")]
478    fn set_display(&self, display: &impl IsA<gdk::Display>) {
479        unsafe {
480            ffi::gtk_style_context_set_display(
481                self.as_ref().to_glib_none().0,
482                display.as_ref().to_glib_none().0,
483            );
484        }
485    }
486
487    /// Sets the scale to use when getting image assets for the style.
488    ///
489    /// # Deprecated since 4.10
490    ///
491    /// You should not use this api
492    /// ## `scale`
493    /// scale
494    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
495    #[allow(deprecated)]
496    #[doc(alias = "gtk_style_context_set_scale")]
497    fn set_scale(&self, scale: i32) {
498        unsafe {
499            ffi::gtk_style_context_set_scale(self.as_ref().to_glib_none().0, scale);
500        }
501    }
502
503    /// Sets the state to be used for style matching.
504    ///
505    /// # Deprecated since 4.10
506    ///
507    /// You should not use this api
508    /// ## `flags`
509    /// state to represent
510    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
511    #[allow(deprecated)]
512    #[doc(alias = "gtk_style_context_set_state")]
513    fn set_state(&self, flags: StateFlags) {
514        unsafe {
515            ffi::gtk_style_context_set_state(self.as_ref().to_glib_none().0, flags.into_glib());
516        }
517    }
518
519    /// Converts the style context into a string representation.
520    ///
521    /// The string representation always includes information about
522    /// the name, state, id, visibility and style classes of the CSS
523    /// node that is backing @self. Depending on the flags, more
524    /// information may be included.
525    ///
526    /// This function is intended for testing and debugging of the
527    /// CSS implementation in GTK. There are no guarantees about
528    /// the format of the returned string, it may change.
529    ///
530    /// # Deprecated since 4.10
531    ///
532    /// This api will be removed in GTK 5
533    /// ## `flags`
534    /// Flags that determine what to print
535    ///
536    /// # Returns
537    ///
538    /// a newly allocated string representing @self
539    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
540    #[allow(deprecated)]
541    #[doc(alias = "gtk_style_context_to_string")]
542    fn to_string(&self, flags: StyleContextPrintFlags) -> glib::GString {
543        unsafe {
544            from_glib_full(ffi::gtk_style_context_to_string(
545                self.as_ref().to_glib_none().0,
546                flags.into_glib(),
547            ))
548        }
549    }
550
551    #[doc(alias = "display")]
552    fn connect_display_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
553        unsafe extern "C" fn notify_display_trampoline<
554            P: IsA<StyleContext>,
555            F: Fn(&P) + 'static,
556        >(
557            this: *mut ffi::GtkStyleContext,
558            _param_spec: glib::ffi::gpointer,
559            f: glib::ffi::gpointer,
560        ) {
561            let f: &F = &*(f as *const F);
562            f(StyleContext::from_glib_borrow(this).unsafe_cast_ref())
563        }
564        unsafe {
565            let f: Box_<F> = Box_::new(f);
566            connect_raw(
567                self.as_ptr() as *mut _,
568                b"notify::display\0".as_ptr() as *const _,
569                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
570                    notify_display_trampoline::<Self, F> as *const (),
571                )),
572                Box_::into_raw(f),
573            )
574        }
575    }
576}
577
578impl<O: IsA<StyleContext>> StyleContextExt for O {}