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
// 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
#![allow(deprecated)]

use crate::{CssSection, StyleProvider};
use glib::{
    prelude::*,
    signal::{connect_raw, SignalHandlerId},
    translate::*,
};
use std::{boxed::Box as Box_, fmt, mem::transmute};

glib::wrapper! {
    /// [`CssProvider`][crate::CssProvider] is an object implementing the [`StyleProvider`][crate::StyleProvider] interface
    /// for CSS.
    ///
    /// It is able to parse CSS-like input in order to style widgets.
    ///
    /// An application can make GTK parse a specific CSS style sheet by calling
    /// [`load_from_file()`][Self::load_from_file()] or
    /// [`load_from_resource()`][Self::load_from_resource()]
    /// and adding the provider with [`StyleContextExt::add_provider()`][crate::prelude::StyleContextExt::add_provider()] or
    /// [`StyleContext::add_provider_for_display()`][crate::StyleContext::add_provider_for_display()].
    ///
    /// In addition, certain files will be read when GTK is initialized.
    /// First, the file `$XDG_CONFIG_HOME/gtk-4.0/gtk.css` is loaded if it
    /// exists. Then, GTK loads the first existing file among
    /// `XDG_DATA_HOME/themes/THEME/gtk-VERSION/gtk-VARIANT.css`,
    /// `$HOME/.themes/THEME/gtk-VERSION/gtk-VARIANT.css`,
    /// `$XDG_DATA_DIRS/themes/THEME/gtk-VERSION/gtk-VARIANT.css` and
    /// `DATADIR/share/themes/THEME/gtk-VERSION/gtk-VARIANT.css`,
    /// where `THEME` is the name of the current theme (see the
    /// [`gtk-theme-name`][struct@crate::Settings#gtk-theme-name] setting), `VARIANT` is the
    /// variant to load (see the
    /// [`gtk-application-prefer-dark-theme`][struct@crate::Settings#gtk-application-prefer-dark-theme] 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 4.0.
    ///
    /// To track errors while loading CSS, connect to the
    /// [`parsing-error`][struct@crate::CssProvider#parsing-error] signal.
    ///
    /// ## Signals
    ///
    ///
    /// #### `parsing-error`
    ///  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.
    ///
    ///
    /// <details><summary><h4>StyleProvider</h4></summary>
    ///
    ///
    /// #### `gtk-private-changed`
    ///
    /// </details>
    ///
    /// # Implements
    ///
    /// [`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 {
    /// 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()) }
    }

    /// Loads @data into @self.
    ///
    /// This clears any previously loaded information.
    /// ## `data`
    /// `GBytes` containing the data to load
    #[cfg(feature = "v4_12")]
    #[cfg_attr(docsrs, doc(cfg(feature = "v4_12")))]
    #[doc(alias = "gtk_css_provider_load_from_bytes")]
    pub fn load_from_bytes(&self, data: &glib::Bytes) {
        unsafe {
            ffi::gtk_css_provider_load_from_bytes(self.to_glib_none().0, data.to_glib_none().0);
        }
    }

    /// Loads @data into @self.
    ///
    /// This clears any previously loaded information.
    ///
    /// # Deprecated since 4.12
    ///
    /// Use [`load_from_string()`][Self::load_from_string()]
    ///   or [`load_from_bytes()`][Self::load_from_bytes()] instead
    /// ## `data`
    /// CSS data to be parsed
    /// ## `length`
    /// the length of @data in bytes, or -1 for NUL terminated strings
    #[cfg_attr(feature = "v4_12", deprecated = "Since 4.12")]
    #[allow(deprecated)]
    #[doc(alias = "gtk_css_provider_load_from_data")]
    pub fn load_from_data(&self, data: &str) {
        let length = data.len() as _;
        unsafe {
            ffi::gtk_css_provider_load_from_data(
                self.to_glib_none().0,
                data.to_glib_none().0,
                length,
            );
        }
    }

    /// Loads the data contained in @file into @self.
    ///
    /// This clears any previously loaded information.
    /// ## `file`
    /// `GFile` pointing to a file to load
    #[doc(alias = "gtk_css_provider_load_from_file")]
    pub fn load_from_file(&self, file: &impl IsA<gio::File>) {
        unsafe {
            ffi::gtk_css_provider_load_from_file(
                self.to_glib_none().0,
                file.as_ref().to_glib_none().0,
            );
        }
    }

    /// Loads the data contained in @path into @self.
    ///
    /// This clears any previously loaded information.
    /// ## `path`
    /// the path of a filename to load, in the GLib filename encoding
    #[doc(alias = "gtk_css_provider_load_from_path")]
    pub fn load_from_path(&self, path: impl AsRef<std::path::Path>) {
        unsafe {
            ffi::gtk_css_provider_load_from_path(
                self.to_glib_none().0,
                path.as_ref().to_glib_none().0,
            );
        }
    }

    /// Loads the data contained in the resource at @resource_path into
    /// the @self.
    ///
    /// This clears any previously loaded information.
    /// ## `resource_path`
    /// a `GResource` resource path
    #[doc(alias = "gtk_css_provider_load_from_resource")]
    pub fn load_from_resource(&self, resource_path: &str) {
        unsafe {
            ffi::gtk_css_provider_load_from_resource(
                self.to_glib_none().0,
                resource_path.to_glib_none().0,
            );
        }
    }

    /// Loads @string into @self.
    ///
    /// This clears any previously loaded information.
    /// ## `string`
    /// the CSS to load
    #[cfg(feature = "v4_12")]
    #[cfg_attr(docsrs, doc(cfg(feature = "v4_12")))]
    #[doc(alias = "gtk_css_provider_load_from_string")]
    pub fn load_from_string(&self, string: &str) {
        unsafe {
            ffi::gtk_css_provider_load_from_string(self.to_glib_none().0, string.to_glib_none().0);
        }
    }

    /// Loads a theme from the usual theme paths.
    ///
    /// The actual process of finding the theme might change between
    /// releases, but it is guaranteed that this function uses the same
    /// mechanism to load the theme that GTK uses for loading its own theme.
    /// ## `name`
    /// A theme name
    /// ## `variant`
    /// variant to load, for example, "dark", or
    ///   [`None`] for the default
    #[doc(alias = "gtk_css_provider_load_named")]
    pub fn load_named(&self, name: &str, variant: Option<&str>) {
        unsafe {
            ffi::gtk_css_provider_load_named(
                self.to_glib_none().0,
                name.to_glib_none().0,
                variant.to_glib_none().0,
            );
        }
    }

    /// 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
    /// [`new()`][Self::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")]
    pub fn to_str(&self) -> glib::GString {
        unsafe { from_glib_full(ffi::gtk_css_provider_to_string(self.to_glib_none().0)) }
    }

    /// 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")]
    pub fn connect_parsing_error<F: Fn(&Self, &CssSection, &glib::Error) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn parsing_error_trampoline<
            F: Fn(&CssProvider, &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(
                &from_glib_borrow(this),
                &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::<F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }
}

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(&self.to_str())
    }
}