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};
7use glib::{
8    object::ObjectType as _,
9    prelude::*,
10    signal::{connect_raw, SignalHandlerId},
11    translate::*,
12};
13use std::boxed::Box as Box_;
14
15glib::wrapper! {
16    /// A style provider for CSS.
17    ///
18    /// It is able to parse CSS-like input in order to style widgets.
19    ///
20    /// An application can make GTK parse a specific CSS style sheet by calling
21    /// [`load_from_file()`][Self::load_from_file()] or
22    /// [`load_from_resource()`][Self::load_from_resource()]
23    /// and adding the provider with [`StyleContextExt::add_provider()`][crate::prelude::StyleContextExt::add_provider()] or
24    /// [`StyleContext::add_provider_for_display()`][crate::StyleContext::add_provider_for_display()].
25    ///
26    /// In addition, certain files will be read when GTK is initialized.
27    /// First, the file `$XDG_CONFIG_HOME/gtk-4.0/gtk.css` is loaded if it
28    /// exists. Then, GTK loads the first existing file among
29    /// `XDG_DATA_HOME/themes/THEME/gtk-VERSION/gtk-VARIANT.css`,
30    /// `$HOME/.themes/THEME/gtk-VERSION/gtk-VARIANT.css`,
31    /// `$XDG_DATA_DIRS/themes/THEME/gtk-VERSION/gtk-VARIANT.css` and
32    /// `DATADIR/share/themes/THEME/gtk-VERSION/gtk-VARIANT.css`,
33    /// where `THEME` is the name of the current theme (see the
34    /// [`gtk-theme-name`][struct@crate::Settings#gtk-theme-name] setting), `VARIANT` is the
35    /// variant to load (see the
36    /// [`gtk-application-prefer-dark-theme`][struct@crate::Settings#gtk-application-prefer-dark-theme] setting),
37    /// `DATADIR` is the prefix configured when GTK was compiled (unless
38    /// overridden by the `GTK_DATA_PREFIX` environment variable), and
39    /// `VERSION` is the GTK version number. If no file is found for the
40    /// current version, GTK tries older versions all the way back to 4.0.
41    ///
42    /// To track errors while loading CSS, connect to the
43    /// [`parsing-error`][struct@crate::CssProvider#parsing-error] signal.
44    ///
45    /// ## Signals
46    ///
47    ///
48    /// #### `parsing-error`
49    ///  Signals that a parsing error occurred.
50    ///
51    /// The expected error values are in the [`CssParserError`][crate::CssParserError]
52    /// and [`CssParserWarning`][crate::CssParserWarning] enumerations.
53    ///
54    /// The @path, @line and @position describe the actual location of
55    /// the error as accurately as possible.
56    ///
57    /// Parsing errors are never fatal, so the parsing will resume after
58    /// the error. Errors may however cause parts of the given data or
59    /// even all of it to not be parsed at all. So it is a useful idea
60    /// to check that the parsing succeeds by connecting to this signal.
61    ///
62    /// Errors in the [`CssParserWarning`][crate::CssParserWarning] enumeration should not
63    /// be treated as fatal errors.
64    ///
65    /// Note that this signal may be emitted at any time as the css provider
66    /// may opt to defer parsing parts or all of the input to a later time
67    /// than when a loading function was called.
68    ///
69    ///
70    /// <details><summary><h4>StyleProvider</h4></summary>
71    ///
72    ///
73    /// #### `gtk-private-changed`
74    ///
75    /// </details>
76    ///
77    /// # Implements
78    ///
79    /// [`trait@glib::ObjectExt`], [`StyleProviderExt`][trait@crate::prelude::StyleProviderExt]
80    #[doc(alias = "GtkCssProvider")]
81    pub struct CssProvider(Object<ffi::GtkCssProvider, ffi::GtkCssProviderClass>) @implements StyleProvider;
82
83    match fn {
84        type_ => || ffi::gtk_css_provider_get_type(),
85    }
86}
87
88impl CssProvider {
89    /// Returns a newly created [`CssProvider`][crate::CssProvider].
90    ///
91    /// # Returns
92    ///
93    /// A new [`CssProvider`][crate::CssProvider]
94    #[doc(alias = "gtk_css_provider_new")]
95    pub fn new() -> CssProvider {
96        assert_initialized_main_thread!();
97        unsafe { from_glib_full(ffi::gtk_css_provider_new()) }
98    }
99
100    /// Loads @data into @self.
101    ///
102    /// This clears any previously loaded information.
103    /// ## `data`
104    /// `GBytes` containing the data to load
105    #[cfg(feature = "v4_12")]
106    #[cfg_attr(docsrs, doc(cfg(feature = "v4_12")))]
107    #[doc(alias = "gtk_css_provider_load_from_bytes")]
108    pub fn load_from_bytes(&self, data: &glib::Bytes) {
109        unsafe {
110            ffi::gtk_css_provider_load_from_bytes(self.to_glib_none().0, data.to_glib_none().0);
111        }
112    }
113
114    /// Loads @data into @self.
115    ///
116    /// This clears any previously loaded information.
117    ///
118    /// # Deprecated since 4.12
119    ///
120    /// Use [`load_from_string()`][Self::load_from_string()]
121    ///   or [`load_from_bytes()`][Self::load_from_bytes()] instead
122    /// ## `data`
123    /// CSS data to be parsed
124    /// ## `length`
125    /// the length of @data in bytes, or -1 for NUL terminated strings
126    #[cfg_attr(feature = "v4_12", deprecated = "Since 4.12")]
127    #[allow(deprecated)]
128    #[doc(alias = "gtk_css_provider_load_from_data")]
129    pub fn load_from_data(&self, data: &str) {
130        let length = data.len() as _;
131        unsafe {
132            ffi::gtk_css_provider_load_from_data(
133                self.to_glib_none().0,
134                data.to_glib_none().0,
135                length,
136            );
137        }
138    }
139
140    /// Loads the data contained in @file into @self.
141    ///
142    /// This clears any previously loaded information.
143    /// ## `file`
144    /// `GFile` pointing to a file to load
145    #[doc(alias = "gtk_css_provider_load_from_file")]
146    pub fn load_from_file(&self, file: &impl IsA<gio::File>) {
147        unsafe {
148            ffi::gtk_css_provider_load_from_file(
149                self.to_glib_none().0,
150                file.as_ref().to_glib_none().0,
151            );
152        }
153    }
154
155    /// Loads the data contained in @path into @self.
156    ///
157    /// This clears any previously loaded information.
158    /// ## `path`
159    /// the path of a filename to load, in the GLib filename encoding
160    #[doc(alias = "gtk_css_provider_load_from_path")]
161    pub fn load_from_path(&self, path: impl AsRef<std::path::Path>) {
162        unsafe {
163            ffi::gtk_css_provider_load_from_path(
164                self.to_glib_none().0,
165                path.as_ref().to_glib_none().0,
166            );
167        }
168    }
169
170    /// Loads the data contained in the resource at @resource_path into
171    /// the @self.
172    ///
173    /// This clears any previously loaded information.
174    /// ## `resource_path`
175    /// a `GResource` resource path
176    #[doc(alias = "gtk_css_provider_load_from_resource")]
177    pub fn load_from_resource(&self, resource_path: &str) {
178        unsafe {
179            ffi::gtk_css_provider_load_from_resource(
180                self.to_glib_none().0,
181                resource_path.to_glib_none().0,
182            );
183        }
184    }
185
186    /// Loads @string into @self.
187    ///
188    /// This clears any previously loaded information.
189    /// ## `string`
190    /// the CSS to load
191    #[cfg(feature = "v4_12")]
192    #[cfg_attr(docsrs, doc(cfg(feature = "v4_12")))]
193    #[doc(alias = "gtk_css_provider_load_from_string")]
194    pub fn load_from_string(&self, string: &str) {
195        unsafe {
196            ffi::gtk_css_provider_load_from_string(self.to_glib_none().0, string.to_glib_none().0);
197        }
198    }
199
200    /// Loads a theme from the usual theme paths.
201    ///
202    /// The actual process of finding the theme might change between
203    /// releases, but it is guaranteed that this function uses the same
204    /// mechanism to load the theme that GTK uses for loading its own theme.
205    /// ## `name`
206    /// A theme name
207    /// ## `variant`
208    /// variant to load, for example, "dark", or
209    ///   [`None`] for the default
210    #[doc(alias = "gtk_css_provider_load_named")]
211    pub fn load_named(&self, name: &str, variant: Option<&str>) {
212        unsafe {
213            ffi::gtk_css_provider_load_named(
214                self.to_glib_none().0,
215                name.to_glib_none().0,
216                variant.to_glib_none().0,
217            );
218        }
219    }
220
221    /// Converts the @self into a string representation in CSS
222    /// format.
223    ///
224    /// Using [`load_from_string()`][Self::load_from_string()] with the return
225    /// value from this function on a new provider created with
226    /// [`new()`][Self::new()] will basically create a duplicate
227    /// of this @self.
228    ///
229    /// # Returns
230    ///
231    /// a new string representing the @self.
232    #[doc(alias = "gtk_css_provider_to_string")]
233    #[doc(alias = "to_string")]
234    pub fn to_str(&self) -> glib::GString {
235        unsafe { from_glib_full(ffi::gtk_css_provider_to_string(self.to_glib_none().0)) }
236    }
237
238    /// Signals that a parsing error occurred.
239    ///
240    /// The expected error values are in the [`CssParserError`][crate::CssParserError]
241    /// and [`CssParserWarning`][crate::CssParserWarning] enumerations.
242    ///
243    /// The @path, @line and @position describe the actual location of
244    /// the error as accurately as possible.
245    ///
246    /// Parsing errors are never fatal, so the parsing will resume after
247    /// the error. Errors may however cause parts of the given data or
248    /// even all of it to not be parsed at all. So it is a useful idea
249    /// to check that the parsing succeeds by connecting to this signal.
250    ///
251    /// Errors in the [`CssParserWarning`][crate::CssParserWarning] enumeration should not
252    /// be treated as fatal errors.
253    ///
254    /// Note that this signal may be emitted at any time as the css provider
255    /// may opt to defer parsing parts or all of the input to a later time
256    /// than when a loading function was called.
257    /// ## `section`
258    /// section the error happened in
259    /// ## `error`
260    /// The parsing error
261    #[doc(alias = "parsing-error")]
262    pub fn connect_parsing_error<F: Fn(&Self, &CssSection, &glib::Error) + 'static>(
263        &self,
264        f: F,
265    ) -> SignalHandlerId {
266        unsafe extern "C" fn parsing_error_trampoline<
267            F: Fn(&CssProvider, &CssSection, &glib::Error) + 'static,
268        >(
269            this: *mut ffi::GtkCssProvider,
270            section: *mut ffi::GtkCssSection,
271            error: *mut glib::ffi::GError,
272            f: glib::ffi::gpointer,
273        ) {
274            let f: &F = &*(f as *const F);
275            f(
276                &from_glib_borrow(this),
277                &from_glib_borrow(section),
278                &from_glib_borrow(error),
279            )
280        }
281        unsafe {
282            let f: Box_<F> = Box_::new(f);
283            connect_raw(
284                self.as_ptr() as *mut _,
285                c"parsing-error".as_ptr() as *const _,
286                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
287                    parsing_error_trampoline::<F> as *const (),
288                )),
289                Box_::into_raw(f),
290            )
291        }
292    }
293}
294
295impl Default for CssProvider {
296    fn default() -> Self {
297        Self::new()
298    }
299}
300
301impl std::fmt::Display for CssProvider {
302    #[inline]
303    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
304        f.write_str(&self.to_str())
305    }
306}