1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files)
// DO NOT EDIT

use crate::CssSection;
use crate::StyleProvider;
use glib::object::Cast;
use glib::object::IsA;
use glib::signal::connect_raw;
use glib::signal::SignalHandlerId;
use glib::translate::*;
use std::boxed::Box as Box_;
use std::fmt;
use std::mem::transmute;
use std::ptr;

glib::wrapper! {
    /// GtkCssProvider is an object implementing the [`StyleProvider`][crate::StyleProvider] interface.
    /// It is able to parse [CSS-like][css-overview] input in order to style widgets.
    ///
    /// An application can make GTK+ parse a specific CSS style sheet by calling
    /// [`CssProviderExt::load_from_file()`][crate::prelude::CssProviderExt::load_from_file()] or [`CssProviderExt::load_from_resource()`][crate::prelude::CssProviderExt::load_from_resource()]
    /// and adding the provider with [`StyleContextExt::add_provider()`][crate::prelude::StyleContextExt::add_provider()] or
    /// [`StyleContext::add_provider_for_screen()`][crate::StyleContext::add_provider_for_screen()].
    ///
    /// In addition, certain files will be read when GTK+ is initialized. First, the
    /// file `$XDG_CONFIG_HOME/gtk-3.0/gtk.css` is loaded if it exists. Then, GTK+
    /// loads the first existing file among
    /// `XDG_DATA_HOME/themes/THEME/gtk-VERSION/gtk.css`,
    /// `$HOME/.themes/THEME/gtk-VERSION/gtk.css`,
    /// `$XDG_DATA_DIRS/themes/THEME/gtk-VERSION/gtk.css` and
    /// `DATADIR/share/themes/THEME/gtk-VERSION/gtk.css`, where `THEME` is the name of
    /// the current theme (see the `property::Settings::gtk-theme-name` setting), `DATADIR`
    /// is the prefix configured when GTK+ was compiled (unless overridden by the
    /// `GTK_DATA_PREFIX` environment variable), and `VERSION` is the GTK+ version number.
    /// If no file is found for the current version, GTK+ tries older versions all the
    /// way back to 3.0.
    ///
    /// In the same way, GTK+ tries to load a gtk-keys.css file for the current
    /// key theme, as defined by `property::Settings::gtk-key-theme-name`.
    ///
    /// # Implements
    ///
    /// [`CssProviderExt`][trait@crate::prelude::CssProviderExt], [`trait@glib::ObjectExt`], [`StyleProviderExt`][trait@crate::prelude::StyleProviderExt]
    #[doc(alias = "GtkCssProvider")]
    pub struct CssProvider(Object<ffi::GtkCssProvider, ffi::GtkCssProviderClass>) @implements StyleProvider;

    match fn {
        type_ => || ffi::gtk_css_provider_get_type(),
    }
}

impl CssProvider {
    pub const NONE: Option<&'static CssProvider> = None;

    /// Returns a newly created [`CssProvider`][crate::CssProvider].
    ///
    /// # Returns
    ///
    /// A new [`CssProvider`][crate::CssProvider]
    #[doc(alias = "gtk_css_provider_new")]
    pub fn new() -> CssProvider {
        assert_initialized_main_thread!();
        unsafe { from_glib_full(ffi::gtk_css_provider_new()) }
    }

    /// Returns the provider containing the style settings used as a
    /// fallback for all widgets.
    ///
    /// # Deprecated since 3.24
    ///
    /// Use [`new()`][Self::new()] instead.
    ///
    /// # Returns
    ///
    /// The provider used for fallback styling.
    ///  This memory is owned by GTK+, and you must not free it.
    #[cfg_attr(feature = "v3_24", deprecated = "Since 3.24")]
    #[doc(alias = "gtk_css_provider_get_default")]
    #[doc(alias = "get_default")]
    pub fn default() -> Option<CssProvider> {
        assert_initialized_main_thread!();
        unsafe { from_glib_none(ffi::gtk_css_provider_get_default()) }
    }

    /// Loads a theme from the usual theme paths
    /// ## `name`
    /// A theme name
    /// ## `variant`
    /// variant to load, for example, "dark", or
    ///  [`None`] for the default
    ///
    /// # Returns
    ///
    /// a [`CssProvider`][crate::CssProvider] with the theme loaded.
    ///  This memory is owned by GTK+, and you must not free it.
    #[doc(alias = "gtk_css_provider_get_named")]
    #[doc(alias = "get_named")]
    pub fn named(name: &str, variant: Option<&str>) -> Option<CssProvider> {
        assert_initialized_main_thread!();
        unsafe {
            from_glib_none(ffi::gtk_css_provider_get_named(
                name.to_glib_none().0,
                variant.to_glib_none().0,
            ))
        }
    }
}

impl Default for CssProvider {
    fn default() -> Self {
        Self::new()
    }
}

impl fmt::Display for CssProvider {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str(&CssProviderExt::to_str(self))
    }
}

/// Trait containing all [`struct@CssProvider`] methods.
///
/// # Implementors
///
/// [`CssProvider`][struct@crate::CssProvider]
pub trait CssProviderExt: 'static {
    /// Loads `data` into `self`, and by doing so clears any previously loaded
    /// information.
    /// ## `data`
    /// CSS data loaded in memory
    ///
    /// # Returns
    ///
    /// [`true`]. The return value is deprecated and [`false`] will only be
    ///  returned for backwards compatibility reasons if an `error` is not
    ///  [`None`] and a loading error occurred. To track errors while loading
    ///  CSS, connect to the `signal::CssProvider::parsing-error` signal.
    #[doc(alias = "gtk_css_provider_load_from_data")]
    fn load_from_data(&self, data: &[u8]) -> Result<(), glib::Error>;

    /// Loads the data contained in `file` into `self`, making it
    /// clear any previously loaded information.
    /// ## `file`
    /// [`gio::File`][crate::gio::File] pointing to a file to load
    ///
    /// # Returns
    ///
    /// [`true`]. The return value is deprecated and [`false`] will only be
    ///  returned for backwards compatibility reasons if an `error` is not
    ///  [`None`] and a loading error occurred. To track errors while loading
    ///  CSS, connect to the `signal::CssProvider::parsing-error` signal.
    #[doc(alias = "gtk_css_provider_load_from_file")]
    fn load_from_file(&self, file: &impl IsA<gio::File>) -> Result<(), glib::Error>;

    /// Loads the data contained in `path` into `self`, making it clear
    /// any previously loaded information.
    /// ## `path`
    /// the path of a filename to load, in the GLib filename encoding
    ///
    /// # Returns
    ///
    /// [`true`]. The return value is deprecated and [`false`] will only be
    ///  returned for backwards compatibility reasons if an `error` is not
    ///  [`None`] and a loading error occurred. To track errors while loading
    ///  CSS, connect to the `signal::CssProvider::parsing-error` signal.
    #[doc(alias = "gtk_css_provider_load_from_path")]
    fn load_from_path(&self, path: &str) -> Result<(), glib::Error>;

    /// Loads the data contained in the resource at `resource_path` into
    /// the [`CssProvider`][crate::CssProvider], clearing any previously loaded information.
    ///
    /// To track errors while loading CSS, connect to the
    /// `signal::CssProvider::parsing-error` signal.
    /// ## `resource_path`
    /// a `GResource` resource path
    #[doc(alias = "gtk_css_provider_load_from_resource")]
    fn load_from_resource(&self, resource_path: &str);

    /// Converts the `self` into a string representation in CSS
    /// format.
    ///
    /// Using [`load_from_data()`][Self::load_from_data()] with the return value
    /// from this function on a new provider created with
    /// [`CssProvider::new()`][crate::CssProvider::new()] will basically create a duplicate of
    /// this `self`.
    ///
    /// # Returns
    ///
    /// a new string representing the `self`.
    #[doc(alias = "gtk_css_provider_to_string")]
    #[doc(alias = "to_string")]
    fn to_str(&self) -> glib::GString;

    /// Signals that a parsing error occurred. the `path`, `line` and `position`
    /// describe the actual location of the error as accurately as possible.
    ///
    /// Parsing errors are never fatal, so the parsing will resume after
    /// the error. Errors may however cause parts of the given
    /// data or even all of it to not be parsed at all. So it is a useful idea
    /// to check that the parsing succeeds by connecting to this signal.
    ///
    /// Note that this signal may be emitted at any time as the css provider
    /// may opt to defer parsing parts or all of the input to a later time
    /// than when a loading function was called.
    /// ## `section`
    /// section the error happened in
    /// ## `error`
    /// The parsing error
    #[doc(alias = "parsing-error")]
    fn connect_parsing_error<F: Fn(&Self, &CssSection, &glib::Error) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId;
}

impl<O: IsA<CssProvider>> CssProviderExt for O {
    fn load_from_data(&self, data: &[u8]) -> Result<(), glib::Error> {
        let length = data.len() as isize;
        unsafe {
            let mut error = ptr::null_mut();
            let is_ok = ffi::gtk_css_provider_load_from_data(
                self.as_ref().to_glib_none().0,
                data.to_glib_none().0,
                length,
                &mut error,
            );
            assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
            if error.is_null() {
                Ok(())
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    fn load_from_file(&self, file: &impl IsA<gio::File>) -> Result<(), glib::Error> {
        unsafe {
            let mut error = ptr::null_mut();
            let is_ok = ffi::gtk_css_provider_load_from_file(
                self.as_ref().to_glib_none().0,
                file.as_ref().to_glib_none().0,
                &mut error,
            );
            assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
            if error.is_null() {
                Ok(())
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    fn load_from_path(&self, path: &str) -> Result<(), glib::Error> {
        unsafe {
            let mut error = ptr::null_mut();
            let is_ok = ffi::gtk_css_provider_load_from_path(
                self.as_ref().to_glib_none().0,
                path.to_glib_none().0,
                &mut error,
            );
            assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
            if error.is_null() {
                Ok(())
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    fn load_from_resource(&self, resource_path: &str) {
        unsafe {
            ffi::gtk_css_provider_load_from_resource(
                self.as_ref().to_glib_none().0,
                resource_path.to_glib_none().0,
            );
        }
    }

    fn to_str(&self) -> glib::GString {
        unsafe {
            from_glib_full(ffi::gtk_css_provider_to_string(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn connect_parsing_error<F: Fn(&Self, &CssSection, &glib::Error) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn parsing_error_trampoline<
            P: IsA<CssProvider>,
            F: Fn(&P, &CssSection, &glib::Error) + 'static,
        >(
            this: *mut ffi::GtkCssProvider,
            section: *mut ffi::GtkCssSection,
            error: *mut glib::ffi::GError,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(
                CssProvider::from_glib_borrow(this).unsafe_cast_ref(),
                &from_glib_borrow(section),
                &from_glib_borrow(error),
            )
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"parsing-error\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    parsing_error_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }
}