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
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files.git)
// DO NOT EDIT

use crate::CssSection;
use crate::StyleProvider;
use glib::object::IsA;
use glib::object::ObjectType as ObjectType_;
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;

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
    /// `property::Settings::gtk-theme-name` setting), `VARIANT` is the
    /// variant to load (see the
    /// `property::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
    /// `signal::CssProvider::parsing-error` signal.
    ///
    /// # 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`
    /// CSS data loaded in memory
    #[doc(alias = "gtk_css_provider_load_from_data")]
    pub fn load_from_data(&self, data: &[u8]) {
        let length = data.len() as isize;
        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<P: IsA<gio::File>>(&self, file: &P) {
        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: &str) {
        unsafe {
            ffi::gtk_css_provider_load_from_path(self.to_glib_none().0, path.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 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,
            );
        }
    }

    #[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())
    }
}