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