gtk4/auto/
css_provider.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, CssSection, StyleProvider};
7#[cfg(feature = "v4_20")]
8#[cfg_attr(docsrs, doc(cfg(feature = "v4_20")))]
9use crate::{InterfaceColorScheme, InterfaceContrast};
10use glib::{
11    object::ObjectType as _,
12    prelude::*,
13    signal::{connect_raw, SignalHandlerId},
14    translate::*,
15};
16use std::boxed::Box as Box_;
17
18glib::wrapper! {
19    /// A style provider for CSS.
20    ///
21    /// It is able to parse CSS-like input in order to style widgets.
22    ///
23    /// An application can make GTK parse a specific CSS style sheet by calling
24    /// [`load_from_file()`][Self::load_from_file()] or
25    /// [`load_from_resource()`][Self::load_from_resource()]
26    /// and adding the provider with [`StyleContextExt::add_provider()`][crate::prelude::StyleContextExt::add_provider()] or
27    /// [`StyleContext::add_provider_for_display()`][crate::StyleContext::add_provider_for_display()].
28    ///
29    /// In addition, certain files will be read when GTK is initialized.
30    /// First, the file `$XDG_CONFIG_HOME/gtk-4.0/gtk.css` is loaded if it
31    /// exists. Then, GTK loads the first existing file among
32    /// `XDG_DATA_HOME/themes/THEME/gtk-VERSION/gtk-VARIANT.css`,
33    /// `$HOME/.themes/THEME/gtk-VERSION/gtk-VARIANT.css`,
34    /// `$XDG_DATA_DIRS/themes/THEME/gtk-VERSION/gtk-VARIANT.css` and
35    /// `DATADIR/share/themes/THEME/gtk-VERSION/gtk-VARIANT.css`,
36    /// where `THEME` is the name of the current theme (see the
37    /// [`gtk-theme-name`][struct@crate::Settings#gtk-theme-name] setting), `VARIANT` is the
38    /// variant to load (see the
39    /// [`gtk-application-prefer-dark-theme`][struct@crate::Settings#gtk-application-prefer-dark-theme] setting),
40    /// `DATADIR` is the prefix configured when GTK was compiled (unless
41    /// overridden by the `GTK_DATA_PREFIX` environment variable), and
42    /// `VERSION` is the GTK version number. If no file is found for the
43    /// current version, GTK tries older versions all the way back to 4.0.
44    ///
45    /// To track errors while loading CSS, connect to the
46    /// [`parsing-error`][struct@crate::CssProvider#parsing-error] signal.
47    ///
48    /// ## Properties
49    ///
50    ///
51    /// #### `prefers-color-scheme`
52    ///  Define the color scheme used for rendering the user interface.
53    ///
54    /// The UI can be set to either [enum@Gtk.InterfaceColorScheme.LIGHT],
55    /// or [enum@Gtk.InterfaceColorScheme.DARK] mode. Other values will
56    /// be interpreted the same as [enum@Gtk.InterfaceColorScheme.LIGHT].
57    ///
58    /// This setting is be available for media queries in CSS:
59    ///
60    /// ```css
61    /// @media (prefers-color-scheme: dark) {
62    ///   // some dark mode styling
63    /// }
64    /// ```
65    ///
66    /// Changing this setting will cause a re-render of the style sheet.
67    ///
68    /// Readable | Writeable
69    ///
70    ///
71    /// #### `prefers-contrast`
72    ///  Define the contrast mode to use for the user interface.
73    ///
74    /// When set to [enum@Gtk.InterfaceContrast.MORE] or
75    /// [enum@Gtk.InterfaceContrast.LESS], the UI is rendered in
76    /// high or low contrast.
77    ///
78    /// When set to [enum@Gtk.InterfaceContrast.NO_PREFERENCE] (the default),
79    /// the user interface will be rendered in default mode.
80    ///
81    /// This setting is be available for media queries in CSS:
82    ///
83    /// ```css
84    /// @media (prefers-contrast: more) {
85    ///   // some style with high contrast
86    /// }
87    /// ```
88    ///
89    /// Changing this setting will cause a re-render of the style sheet.
90    ///
91    /// Readable | Writeable
92    ///
93    /// ## Signals
94    ///
95    ///
96    /// #### `parsing-error`
97    ///  Signals that a parsing error occurred.
98    ///
99    /// The expected error values are in the [`CssParserError`][crate::CssParserError]
100    /// and [`CssParserWarning`][crate::CssParserWarning] enumerations.
101    ///
102    /// The @path, @line and @position describe the actual location of
103    /// the error as accurately as possible.
104    ///
105    /// Parsing errors are never fatal, so the parsing will resume after
106    /// the error. Errors may however cause parts of the given data or
107    /// even all of it to not be parsed at all. So it is a useful idea
108    /// to check that the parsing succeeds by connecting to this signal.
109    ///
110    /// Errors in the [`CssParserWarning`][crate::CssParserWarning] enumeration should not
111    /// be treated as fatal errors.
112    ///
113    /// Note that this signal may be emitted at any time as the css provider
114    /// may opt to defer parsing parts or all of the input to a later time
115    /// than when a loading function was called.
116    ///
117    ///
118    /// <details><summary><h4>StyleProvider</h4></summary>
119    ///
120    ///
121    /// #### `gtk-private-changed`
122    ///
123    /// </details>
124    ///
125    /// # Implements
126    ///
127    /// [`trait@glib::ObjectExt`], [`StyleProviderExt`][trait@crate::prelude::StyleProviderExt]
128    #[doc(alias = "GtkCssProvider")]
129    pub struct CssProvider(Object<ffi::GtkCssProvider, ffi::GtkCssProviderClass>) @implements StyleProvider;
130
131    match fn {
132        type_ => || ffi::gtk_css_provider_get_type(),
133    }
134}
135
136impl CssProvider {
137    /// Returns a newly created [`CssProvider`][crate::CssProvider].
138    ///
139    /// # Returns
140    ///
141    /// A new [`CssProvider`][crate::CssProvider]
142    #[doc(alias = "gtk_css_provider_new")]
143    pub fn new() -> CssProvider {
144        assert_initialized_main_thread!();
145        unsafe { from_glib_full(ffi::gtk_css_provider_new()) }
146    }
147
148    // rustdoc-stripper-ignore-next
149    /// Creates a new builder-pattern struct instance to construct [`CssProvider`] objects.
150    ///
151    /// This method returns an instance of [`CssProviderBuilder`](crate::builders::CssProviderBuilder) which can be used to create [`CssProvider`] objects.
152    pub fn builder() -> CssProviderBuilder {
153        CssProviderBuilder::new()
154    }
155
156    /// Loads @data into @self.
157    ///
158    /// This clears any previously loaded information.
159    /// ## `data`
160    /// `GBytes` containing the data to load
161    #[cfg(feature = "v4_12")]
162    #[cfg_attr(docsrs, doc(cfg(feature = "v4_12")))]
163    #[doc(alias = "gtk_css_provider_load_from_bytes")]
164    pub fn load_from_bytes(&self, data: &glib::Bytes) {
165        unsafe {
166            ffi::gtk_css_provider_load_from_bytes(self.to_glib_none().0, data.to_glib_none().0);
167        }
168    }
169
170    /// Loads @data into @self.
171    ///
172    /// This clears any previously loaded information.
173    ///
174    /// # Deprecated since 4.12
175    ///
176    /// Use [`load_from_string()`][Self::load_from_string()]
177    ///   or [`load_from_bytes()`][Self::load_from_bytes()] instead
178    /// ## `data`
179    /// CSS data to be parsed
180    /// ## `length`
181    /// the length of @data in bytes, or -1 for NUL terminated strings
182    #[cfg_attr(feature = "v4_12", deprecated = "Since 4.12")]
183    #[allow(deprecated)]
184    #[doc(alias = "gtk_css_provider_load_from_data")]
185    pub fn load_from_data(&self, data: &str) {
186        let length = data.len() as _;
187        unsafe {
188            ffi::gtk_css_provider_load_from_data(
189                self.to_glib_none().0,
190                data.to_glib_none().0,
191                length,
192            );
193        }
194    }
195
196    /// Loads the data contained in @file into @self.
197    ///
198    /// This clears any previously loaded information.
199    /// ## `file`
200    /// `GFile` pointing to a file to load
201    #[doc(alias = "gtk_css_provider_load_from_file")]
202    pub fn load_from_file(&self, file: &impl IsA<gio::File>) {
203        unsafe {
204            ffi::gtk_css_provider_load_from_file(
205                self.to_glib_none().0,
206                file.as_ref().to_glib_none().0,
207            );
208        }
209    }
210
211    /// Loads the data contained in @path into @self.
212    ///
213    /// This clears any previously loaded information.
214    /// ## `path`
215    /// the path of a filename to load, in the GLib filename encoding
216    #[doc(alias = "gtk_css_provider_load_from_path")]
217    pub fn load_from_path(&self, path: impl AsRef<std::path::Path>) {
218        unsafe {
219            ffi::gtk_css_provider_load_from_path(
220                self.to_glib_none().0,
221                path.as_ref().to_glib_none().0,
222            );
223        }
224    }
225
226    /// Loads the data contained in the resource at @resource_path into
227    /// the @self.
228    ///
229    /// This clears any previously loaded information.
230    /// ## `resource_path`
231    /// a `GResource` resource path
232    #[doc(alias = "gtk_css_provider_load_from_resource")]
233    pub fn load_from_resource(&self, resource_path: &str) {
234        unsafe {
235            ffi::gtk_css_provider_load_from_resource(
236                self.to_glib_none().0,
237                resource_path.to_glib_none().0,
238            );
239        }
240    }
241
242    /// Loads @string into @self.
243    ///
244    /// This clears any previously loaded information.
245    /// ## `string`
246    /// the CSS to load
247    #[cfg(feature = "v4_12")]
248    #[cfg_attr(docsrs, doc(cfg(feature = "v4_12")))]
249    #[doc(alias = "gtk_css_provider_load_from_string")]
250    pub fn load_from_string(&self, string: &str) {
251        unsafe {
252            ffi::gtk_css_provider_load_from_string(self.to_glib_none().0, string.to_glib_none().0);
253        }
254    }
255
256    /// Loads a theme from the usual theme paths.
257    ///
258    /// The actual process of finding the theme might change between
259    /// releases, but it is guaranteed that this function uses the same
260    /// mechanism to load the theme that GTK uses for loading its own theme.
261    ///
262    /// # Deprecated since 4.20
263    ///
264    /// Using any of the other theme loaders, combine with media queries.
265    /// ## `name`
266    /// A theme name
267    /// ## `variant`
268    /// variant to load, for example, "dark", or
269    ///   [`None`] for the default
270    #[cfg_attr(feature = "v4_20", deprecated = "Since 4.20")]
271    #[allow(deprecated)]
272    #[doc(alias = "gtk_css_provider_load_named")]
273    pub fn load_named(&self, name: &str, variant: Option<&str>) {
274        unsafe {
275            ffi::gtk_css_provider_load_named(
276                self.to_glib_none().0,
277                name.to_glib_none().0,
278                variant.to_glib_none().0,
279            );
280        }
281    }
282
283    /// Converts the @self into a string representation in CSS
284    /// format.
285    ///
286    /// Using [`load_from_string()`][Self::load_from_string()] with the return
287    /// value from this function on a new provider created with
288    /// [`new()`][Self::new()] will basically create a duplicate
289    /// of this @self.
290    ///
291    /// # Returns
292    ///
293    /// a new string representing the @self.
294    #[doc(alias = "gtk_css_provider_to_string")]
295    #[doc(alias = "to_string")]
296    pub fn to_str(&self) -> glib::GString {
297        unsafe { from_glib_full(ffi::gtk_css_provider_to_string(self.to_glib_none().0)) }
298    }
299
300    /// Define the color scheme used for rendering the user interface.
301    ///
302    /// The UI can be set to either [enum@Gtk.InterfaceColorScheme.LIGHT],
303    /// or [enum@Gtk.InterfaceColorScheme.DARK] mode. Other values will
304    /// be interpreted the same as [enum@Gtk.InterfaceColorScheme.LIGHT].
305    ///
306    /// This setting is be available for media queries in CSS:
307    ///
308    /// ```css
309    /// @media (prefers-color-scheme: dark) {
310    ///   // some dark mode styling
311    /// }
312    /// ```
313    ///
314    /// Changing this setting will cause a re-render of the style sheet.
315    #[cfg(feature = "v4_20")]
316    #[cfg_attr(docsrs, doc(cfg(feature = "v4_20")))]
317    #[doc(alias = "prefers-color-scheme")]
318    pub fn prefers_color_scheme(&self) -> InterfaceColorScheme {
319        ObjectExt::property(self, "prefers-color-scheme")
320    }
321
322    /// Define the color scheme used for rendering the user interface.
323    ///
324    /// The UI can be set to either [enum@Gtk.InterfaceColorScheme.LIGHT],
325    /// or [enum@Gtk.InterfaceColorScheme.DARK] mode. Other values will
326    /// be interpreted the same as [enum@Gtk.InterfaceColorScheme.LIGHT].
327    ///
328    /// This setting is be available for media queries in CSS:
329    ///
330    /// ```css
331    /// @media (prefers-color-scheme: dark) {
332    ///   // some dark mode styling
333    /// }
334    /// ```
335    ///
336    /// Changing this setting will cause a re-render of the style sheet.
337    #[cfg(feature = "v4_20")]
338    #[cfg_attr(docsrs, doc(cfg(feature = "v4_20")))]
339    #[doc(alias = "prefers-color-scheme")]
340    pub fn set_prefers_color_scheme(&self, prefers_color_scheme: InterfaceColorScheme) {
341        ObjectExt::set_property(self, "prefers-color-scheme", prefers_color_scheme)
342    }
343
344    /// Define the contrast mode to use for the user interface.
345    ///
346    /// When set to [enum@Gtk.InterfaceContrast.MORE] or
347    /// [enum@Gtk.InterfaceContrast.LESS], the UI is rendered in
348    /// high or low contrast.
349    ///
350    /// When set to [enum@Gtk.InterfaceContrast.NO_PREFERENCE] (the default),
351    /// the user interface will be rendered in default mode.
352    ///
353    /// This setting is be available for media queries in CSS:
354    ///
355    /// ```css
356    /// @media (prefers-contrast: more) {
357    ///   // some style with high contrast
358    /// }
359    /// ```
360    ///
361    /// Changing this setting will cause a re-render of the style sheet.
362    #[cfg(feature = "v4_20")]
363    #[cfg_attr(docsrs, doc(cfg(feature = "v4_20")))]
364    #[doc(alias = "prefers-contrast")]
365    pub fn prefers_contrast(&self) -> InterfaceContrast {
366        ObjectExt::property(self, "prefers-contrast")
367    }
368
369    /// Define the contrast mode to use for the user interface.
370    ///
371    /// When set to [enum@Gtk.InterfaceContrast.MORE] or
372    /// [enum@Gtk.InterfaceContrast.LESS], the UI is rendered in
373    /// high or low contrast.
374    ///
375    /// When set to [enum@Gtk.InterfaceContrast.NO_PREFERENCE] (the default),
376    /// the user interface will be rendered in default mode.
377    ///
378    /// This setting is be available for media queries in CSS:
379    ///
380    /// ```css
381    /// @media (prefers-contrast: more) {
382    ///   // some style with high contrast
383    /// }
384    /// ```
385    ///
386    /// Changing this setting will cause a re-render of the style sheet.
387    #[cfg(feature = "v4_20")]
388    #[cfg_attr(docsrs, doc(cfg(feature = "v4_20")))]
389    #[doc(alias = "prefers-contrast")]
390    pub fn set_prefers_contrast(&self, prefers_contrast: InterfaceContrast) {
391        ObjectExt::set_property(self, "prefers-contrast", prefers_contrast)
392    }
393
394    /// Signals that a parsing error occurred.
395    ///
396    /// The expected error values are in the [`CssParserError`][crate::CssParserError]
397    /// and [`CssParserWarning`][crate::CssParserWarning] enumerations.
398    ///
399    /// The @path, @line and @position describe the actual location of
400    /// the error as accurately as possible.
401    ///
402    /// Parsing errors are never fatal, so the parsing will resume after
403    /// the error. Errors may however cause parts of the given data or
404    /// even all of it to not be parsed at all. So it is a useful idea
405    /// to check that the parsing succeeds by connecting to this signal.
406    ///
407    /// Errors in the [`CssParserWarning`][crate::CssParserWarning] enumeration should not
408    /// be treated as fatal errors.
409    ///
410    /// Note that this signal may be emitted at any time as the css provider
411    /// may opt to defer parsing parts or all of the input to a later time
412    /// than when a loading function was called.
413    /// ## `section`
414    /// section the error happened in
415    /// ## `error`
416    /// The parsing error
417    #[doc(alias = "parsing-error")]
418    pub fn connect_parsing_error<F: Fn(&Self, &CssSection, &glib::Error) + 'static>(
419        &self,
420        f: F,
421    ) -> SignalHandlerId {
422        unsafe extern "C" fn parsing_error_trampoline<
423            F: Fn(&CssProvider, &CssSection, &glib::Error) + 'static,
424        >(
425            this: *mut ffi::GtkCssProvider,
426            section: *mut ffi::GtkCssSection,
427            error: *mut glib::ffi::GError,
428            f: glib::ffi::gpointer,
429        ) {
430            let f: &F = &*(f as *const F);
431            f(
432                &from_glib_borrow(this),
433                &from_glib_borrow(section),
434                &from_glib_borrow(error),
435            )
436        }
437        unsafe {
438            let f: Box_<F> = Box_::new(f);
439            connect_raw(
440                self.as_ptr() as *mut _,
441                c"parsing-error".as_ptr() as *const _,
442                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
443                    parsing_error_trampoline::<F> as *const (),
444                )),
445                Box_::into_raw(f),
446            )
447        }
448    }
449
450    #[cfg(feature = "v4_20")]
451    #[cfg_attr(docsrs, doc(cfg(feature = "v4_20")))]
452    #[doc(alias = "prefers-color-scheme")]
453    pub fn connect_prefers_color_scheme_notify<F: Fn(&Self) + 'static>(
454        &self,
455        f: F,
456    ) -> SignalHandlerId {
457        unsafe extern "C" fn notify_prefers_color_scheme_trampoline<
458            F: Fn(&CssProvider) + 'static,
459        >(
460            this: *mut ffi::GtkCssProvider,
461            _param_spec: glib::ffi::gpointer,
462            f: glib::ffi::gpointer,
463        ) {
464            let f: &F = &*(f as *const F);
465            f(&from_glib_borrow(this))
466        }
467        unsafe {
468            let f: Box_<F> = Box_::new(f);
469            connect_raw(
470                self.as_ptr() as *mut _,
471                c"notify::prefers-color-scheme".as_ptr() as *const _,
472                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
473                    notify_prefers_color_scheme_trampoline::<F> as *const (),
474                )),
475                Box_::into_raw(f),
476            )
477        }
478    }
479
480    #[cfg(feature = "v4_20")]
481    #[cfg_attr(docsrs, doc(cfg(feature = "v4_20")))]
482    #[doc(alias = "prefers-contrast")]
483    pub fn connect_prefers_contrast_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
484        unsafe extern "C" fn notify_prefers_contrast_trampoline<F: Fn(&CssProvider) + 'static>(
485            this: *mut ffi::GtkCssProvider,
486            _param_spec: glib::ffi::gpointer,
487            f: glib::ffi::gpointer,
488        ) {
489            let f: &F = &*(f as *const F);
490            f(&from_glib_borrow(this))
491        }
492        unsafe {
493            let f: Box_<F> = Box_::new(f);
494            connect_raw(
495                self.as_ptr() as *mut _,
496                c"notify::prefers-contrast".as_ptr() as *const _,
497                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
498                    notify_prefers_contrast_trampoline::<F> as *const (),
499                )),
500                Box_::into_raw(f),
501            )
502        }
503    }
504}
505
506impl Default for CssProvider {
507    fn default() -> Self {
508        Self::new()
509    }
510}
511
512impl std::fmt::Display for CssProvider {
513    #[inline]
514    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
515        f.write_str(&self.to_str())
516    }
517}
518
519// rustdoc-stripper-ignore-next
520/// A [builder-pattern] type to construct [`CssProvider`] objects.
521///
522/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
523#[must_use = "The builder must be built to be used"]
524pub struct CssProviderBuilder {
525    builder: glib::object::ObjectBuilder<'static, CssProvider>,
526}
527
528impl CssProviderBuilder {
529    fn new() -> Self {
530        Self {
531            builder: glib::object::Object::builder(),
532        }
533    }
534
535    /// Define the color scheme used for rendering the user interface.
536    ///
537    /// The UI can be set to either [enum@Gtk.InterfaceColorScheme.LIGHT],
538    /// or [enum@Gtk.InterfaceColorScheme.DARK] mode. Other values will
539    /// be interpreted the same as [enum@Gtk.InterfaceColorScheme.LIGHT].
540    ///
541    /// This setting is be available for media queries in CSS:
542    ///
543    /// ```css
544    /// @media (prefers-color-scheme: dark) {
545    ///   // some dark mode styling
546    /// }
547    /// ```
548    ///
549    /// Changing this setting will cause a re-render of the style sheet.
550    #[cfg(feature = "v4_20")]
551    #[cfg_attr(docsrs, doc(cfg(feature = "v4_20")))]
552    pub fn prefers_color_scheme(self, prefers_color_scheme: InterfaceColorScheme) -> Self {
553        Self {
554            builder: self
555                .builder
556                .property("prefers-color-scheme", prefers_color_scheme),
557        }
558    }
559
560    /// Define the contrast mode to use for the user interface.
561    ///
562    /// When set to [enum@Gtk.InterfaceContrast.MORE] or
563    /// [enum@Gtk.InterfaceContrast.LESS], the UI is rendered in
564    /// high or low contrast.
565    ///
566    /// When set to [enum@Gtk.InterfaceContrast.NO_PREFERENCE] (the default),
567    /// the user interface will be rendered in default mode.
568    ///
569    /// This setting is be available for media queries in CSS:
570    ///
571    /// ```css
572    /// @media (prefers-contrast: more) {
573    ///   // some style with high contrast
574    /// }
575    /// ```
576    ///
577    /// Changing this setting will cause a re-render of the style sheet.
578    #[cfg(feature = "v4_20")]
579    #[cfg_attr(docsrs, doc(cfg(feature = "v4_20")))]
580    pub fn prefers_contrast(self, prefers_contrast: InterfaceContrast) -> Self {
581        Self {
582            builder: self.builder.property("prefers-contrast", prefers_contrast),
583        }
584    }
585
586    // rustdoc-stripper-ignore-next
587    /// Build the [`CssProvider`].
588    #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
589    pub fn build(self) -> CssProvider {
590        assert_initialized_main_thread!();
591        self.builder.build()
592    }
593}