Skip to main content

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