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