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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
// 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::{Border, StateFlags, StyleContextPrintFlags, StyleProvider};
use glib::{
    prelude::*,
    signal::{connect_raw, SignalHandlerId},
    translate::*,
};
use std::boxed::Box as Box_;

glib::wrapper! {
    /// The relevant API has been moved to [`Widget`][crate::Widget]
    ///   where applicable; otherwise, there is no replacement for querying the
    ///   style machinery. Stylable UI elements should use widgets.
    /// [`StyleContext`][crate::StyleContext] stores styling information affecting a widget.
    ///
    /// In order to construct the final style information, [`StyleContext`][crate::StyleContext]
    /// queries information from all attached `GtkStyleProviders`. Style
    /// providers can be either attached explicitly to the context through
    /// [`StyleContextExt::add_provider()`][crate::prelude::StyleContextExt::add_provider()], or to the display through
    /// [`add_provider_for_display()`][Self::add_provider_for_display()]. The resulting
    /// style is a combination of all providers’ information in priority order.
    ///
    /// For GTK widgets, any [`StyleContext`][crate::StyleContext] returned by
    /// [`WidgetExt::style_context()`][crate::prelude::WidgetExt::style_context()] will already have a [`gdk::Display`][crate::gdk::Display]
    /// and RTL/LTR information set. The style context will also be updated
    /// automatically if any of these settings change on the widget.
    ///
    /// ## Style Classes
    ///
    /// Widgets can add style classes to their context, which can be used to associate
    /// different styles by class. The documentation for individual widgets lists
    /// which style classes it uses itself, and which style classes may be added by
    /// applications to affect their appearance.
    ///
    /// # Custom styling in UI libraries and applications
    ///
    /// If you are developing a library with custom widgets that render differently
    /// than standard components, you may need to add a [`StyleProvider`][crate::StyleProvider] yourself
    /// with the `GTK_STYLE_PROVIDER_PRIORITY_FALLBACK` priority, either a
    /// [`CssProvider`][crate::CssProvider] or a custom object implementing the [`StyleProvider`][crate::StyleProvider]
    /// interface. This way themes may still attempt to style your UI elements in
    /// a different way if needed so.
    ///
    /// If you are using custom styling on an applications, you probably want then
    /// to make your style information prevail to the theme’s, so you must use
    /// a [`StyleProvider`][crate::StyleProvider] with the `GTK_STYLE_PROVIDER_PRIORITY_APPLICATION`
    /// priority, keep in mind that the user settings in
    /// `XDG_CONFIG_HOME/gtk-4.0/gtk.css` will
    /// still take precedence over your changes, as it uses the
    /// `GTK_STYLE_PROVIDER_PRIORITY_USER` priority.
    ///
    /// ## Properties
    ///
    ///
    /// #### `display`
    ///  Readable | Writeable
    ///
    /// # Implements
    ///
    /// [`StyleContextExt`][trait@crate::prelude::StyleContextExt], [`trait@glib::ObjectExt`]
    #[doc(alias = "GtkStyleContext")]
    pub struct StyleContext(Object<ffi::GtkStyleContext, ffi::GtkStyleContextClass>);

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

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

mod sealed {
    pub trait Sealed {}
    impl<T: super::IsA<super::StyleContext>> Sealed for T {}
}

/// Trait containing all [`struct@StyleContext`] methods.
///
/// # Implementors
///
/// [`StyleContext`][struct@crate::StyleContext]
pub trait StyleContextExt: IsA<StyleContext> + sealed::Sealed + 'static {
    /// Adds a style class to @self, so later uses of the
    /// style context will make use of this new class for styling.
    ///
    /// In the CSS file format, a [`Entry`][crate::Entry] defining a “search”
    /// class, would be matched by:
    ///
    /// ```css
    /// entry.search { ... }
    /// ```
    ///
    /// While any widget defining a “search” class would be
    /// matched by:
    /// ```css
    /// .search { ... }
    /// ```
    ///
    /// # Deprecated since 4.10
    ///
    /// Use [`WidgetExt::add_css_class()`][crate::prelude::WidgetExt::add_css_class()] instead
    /// ## `class_name`
    /// class name to use in styling
    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
    #[allow(deprecated)]
    #[doc(alias = "gtk_style_context_add_class")]
    fn add_class(&self, class_name: &str) {
        unsafe {
            ffi::gtk_style_context_add_class(
                self.as_ref().to_glib_none().0,
                class_name.to_glib_none().0,
            );
        }
    }

    /// Adds a style provider to @self, to be used in style construction.
    ///
    /// Note that a style provider added by this function only affects
    /// the style of the widget to which @self belongs. If you want
    /// to affect the style of all widgets, use
    /// [`StyleContext::add_provider_for_display()`][crate::StyleContext::add_provider_for_display()].
    ///
    /// Note: If both priorities are the same, a [`StyleProvider`][crate::StyleProvider]
    /// added through this function takes precedence over another added
    /// through [`StyleContext::add_provider_for_display()`][crate::StyleContext::add_provider_for_display()].
    ///
    /// # Deprecated since 4.10
    ///
    /// Use style classes instead
    /// ## `provider`
    /// a [`StyleProvider`][crate::StyleProvider]
    /// ## `priority`
    /// the priority of the style provider. The lower
    ///   it is, the earlier it will be used in the style construction.
    ///   Typically this will be in the range between
    ///   `GTK_STYLE_PROVIDER_PRIORITY_FALLBACK` and
    ///   `GTK_STYLE_PROVIDER_PRIORITY_USER`
    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
    #[allow(deprecated)]
    #[doc(alias = "gtk_style_context_add_provider")]
    fn add_provider(&self, provider: &impl IsA<StyleProvider>, priority: u32) {
        unsafe {
            ffi::gtk_style_context_add_provider(
                self.as_ref().to_glib_none().0,
                provider.as_ref().to_glib_none().0,
                priority,
            );
        }
    }

    /// Gets the border for a given state as a [`Border`][crate::Border].
    ///
    /// # Deprecated since 4.10
    ///
    /// This api will be removed in GTK 5
    ///
    /// # Returns
    ///
    ///
    /// ## `border`
    /// return value for the border settings
    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
    #[allow(deprecated)]
    #[doc(alias = "gtk_style_context_get_border")]
    #[doc(alias = "get_border")]
    fn border(&self) -> Border {
        unsafe {
            let mut border = Border::uninitialized();
            ffi::gtk_style_context_get_border(
                self.as_ref().to_glib_none().0,
                border.to_glib_none_mut().0,
            );
            border
        }
    }

    /// Gets the foreground color for a given state.
    ///
    /// # Deprecated since 4.10
    ///
    /// Use [`WidgetExt::color()`][crate::prelude::WidgetExt::color()] instead
    ///
    /// # Returns
    ///
    ///
    /// ## `color`
    /// return value for the foreground color
    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
    #[allow(deprecated)]
    #[doc(alias = "gtk_style_context_get_color")]
    #[doc(alias = "get_color")]
    fn color(&self) -> gdk::RGBA {
        unsafe {
            let mut color = gdk::RGBA::uninitialized();
            ffi::gtk_style_context_get_color(
                self.as_ref().to_glib_none().0,
                color.to_glib_none_mut().0,
            );
            color
        }
    }

    /// Returns the [`gdk::Display`][crate::gdk::Display] to which @self is attached.
    ///
    /// # Deprecated since 4.10
    ///
    /// Use [`WidgetExt::display()`][crate::prelude::WidgetExt::display()] instead
    ///
    /// # Returns
    ///
    /// a [`gdk::Display`][crate::gdk::Display].
    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
    #[allow(deprecated)]
    #[doc(alias = "gtk_style_context_get_display")]
    #[doc(alias = "get_display")]
    fn display(&self) -> gdk::Display {
        unsafe {
            from_glib_none(ffi::gtk_style_context_get_display(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    /// Gets the margin for a given state as a [`Border`][crate::Border].
    ///
    /// # Deprecated since 4.10
    ///
    /// This api will be removed in GTK 5
    ///
    /// # Returns
    ///
    ///
    /// ## `margin`
    /// return value for the margin settings
    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
    #[allow(deprecated)]
    #[doc(alias = "gtk_style_context_get_margin")]
    #[doc(alias = "get_margin")]
    fn margin(&self) -> Border {
        unsafe {
            let mut margin = Border::uninitialized();
            ffi::gtk_style_context_get_margin(
                self.as_ref().to_glib_none().0,
                margin.to_glib_none_mut().0,
            );
            margin
        }
    }

    /// Gets the padding for a given state as a [`Border`][crate::Border].
    ///
    /// # Deprecated since 4.10
    ///
    /// This api will be removed in GTK 5
    ///
    /// # Returns
    ///
    ///
    /// ## `padding`
    /// return value for the padding settings
    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
    #[allow(deprecated)]
    #[doc(alias = "gtk_style_context_get_padding")]
    #[doc(alias = "get_padding")]
    fn padding(&self) -> Border {
        unsafe {
            let mut padding = Border::uninitialized();
            ffi::gtk_style_context_get_padding(
                self.as_ref().to_glib_none().0,
                padding.to_glib_none_mut().0,
            );
            padding
        }
    }

    /// Returns the scale used for assets.
    ///
    /// # Deprecated since 4.10
    ///
    /// Use [`WidgetExt::scale_factor()`][crate::prelude::WidgetExt::scale_factor()] instead
    ///
    /// # Returns
    ///
    /// the scale
    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
    #[allow(deprecated)]
    #[doc(alias = "gtk_style_context_get_scale")]
    #[doc(alias = "get_scale")]
    fn scale(&self) -> i32 {
        unsafe { ffi::gtk_style_context_get_scale(self.as_ref().to_glib_none().0) }
    }

    /// Returns the state used for style matching.
    ///
    /// This method should only be used to retrieve the [`StateFlags`][crate::StateFlags]
    /// to pass to [`StyleContext`][crate::StyleContext] methods, like
    /// [`padding()`][Self::padding()].
    /// If you need to retrieve the current state of a [`Widget`][crate::Widget], use
    /// [`WidgetExt::state_flags()`][crate::prelude::WidgetExt::state_flags()].
    ///
    /// # Deprecated since 4.10
    ///
    /// Use [`WidgetExt::state_flags()`][crate::prelude::WidgetExt::state_flags()] instead
    ///
    /// # Returns
    ///
    /// the state flags
    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
    #[allow(deprecated)]
    #[doc(alias = "gtk_style_context_get_state")]
    #[doc(alias = "get_state")]
    fn state(&self) -> StateFlags {
        unsafe {
            from_glib(ffi::gtk_style_context_get_state(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    /// Returns [`true`] if @self currently has defined the
    /// given class name.
    ///
    /// # Deprecated since 4.10
    ///
    /// Use [`WidgetExt::has_css_class()`][crate::prelude::WidgetExt::has_css_class()] instead
    /// ## `class_name`
    /// a class name
    ///
    /// # Returns
    ///
    /// [`true`] if @self has @class_name defined
    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
    #[allow(deprecated)]
    #[doc(alias = "gtk_style_context_has_class")]
    fn has_class(&self, class_name: &str) -> bool {
        unsafe {
            from_glib(ffi::gtk_style_context_has_class(
                self.as_ref().to_glib_none().0,
                class_name.to_glib_none().0,
            ))
        }
    }

    /// Looks up and resolves a color name in the @self color map.
    ///
    /// # Deprecated since 4.10
    ///
    /// This api will be removed in GTK 5
    /// ## `color_name`
    /// color name to lookup
    ///
    /// # Returns
    ///
    /// [`true`] if @color_name was found and resolved, [`false`] otherwise
    ///
    /// ## `color`
    /// Return location for the looked up color
    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
    #[allow(deprecated)]
    #[doc(alias = "gtk_style_context_lookup_color")]
    fn lookup_color(&self, color_name: &str) -> Option<gdk::RGBA> {
        unsafe {
            let mut color = gdk::RGBA::uninitialized();
            let ret = from_glib(ffi::gtk_style_context_lookup_color(
                self.as_ref().to_glib_none().0,
                color_name.to_glib_none().0,
                color.to_glib_none_mut().0,
            ));
            if ret {
                Some(color)
            } else {
                None
            }
        }
    }

    /// Removes @class_name from @self.
    ///
    /// # Deprecated since 4.10
    ///
    /// Use [`WidgetExt::remove_css_class()`][crate::prelude::WidgetExt::remove_css_class()] instead
    /// ## `class_name`
    /// class name to remove
    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
    #[allow(deprecated)]
    #[doc(alias = "gtk_style_context_remove_class")]
    fn remove_class(&self, class_name: &str) {
        unsafe {
            ffi::gtk_style_context_remove_class(
                self.as_ref().to_glib_none().0,
                class_name.to_glib_none().0,
            );
        }
    }

    /// Removes @provider from the style providers list in @self.
    ///
    /// # Deprecated since 4.10
    ///
    /// ## `provider`
    /// a [`StyleProvider`][crate::StyleProvider]
    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
    #[allow(deprecated)]
    #[doc(alias = "gtk_style_context_remove_provider")]
    fn remove_provider(&self, provider: &impl IsA<StyleProvider>) {
        unsafe {
            ffi::gtk_style_context_remove_provider(
                self.as_ref().to_glib_none().0,
                provider.as_ref().to_glib_none().0,
            );
        }
    }

    /// Restores @self state to a previous stage.
    ///
    /// See [`save()`][Self::save()].
    ///
    /// # Deprecated since 4.10
    ///
    /// This API will be removed in GTK 5
    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
    #[allow(deprecated)]
    #[doc(alias = "gtk_style_context_restore")]
    fn restore(&self) {
        unsafe {
            ffi::gtk_style_context_restore(self.as_ref().to_glib_none().0);
        }
    }

    /// Saves the @self state.
    ///
    /// This allows temporary modifications done through
    /// [`add_class()`][Self::add_class()],
    /// [`remove_class()`][Self::remove_class()],
    /// [`set_state()`][Self::set_state()] to be quickly
    /// reverted in one go through [`restore()`][Self::restore()].
    ///
    /// The matching call to [`restore()`][Self::restore()]
    /// must be done before GTK returns to the main loop.
    ///
    /// # Deprecated since 4.10
    ///
    /// This API will be removed in GTK 5
    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
    #[allow(deprecated)]
    #[doc(alias = "gtk_style_context_save")]
    fn save(&self) {
        unsafe {
            ffi::gtk_style_context_save(self.as_ref().to_glib_none().0);
        }
    }

    /// Attaches @self to the given display.
    ///
    /// The display is used to add style information from “global”
    /// style providers, such as the display's [`Settings`][crate::Settings] instance.
    ///
    /// If you are using a [`StyleContext`][crate::StyleContext] returned from
    /// [`WidgetExt::style_context()`][crate::prelude::WidgetExt::style_context()], you do not need to
    /// call this yourself.
    ///
    /// # Deprecated since 4.10
    ///
    /// You should not use this api
    /// ## `display`
    /// a [`gdk::Display`][crate::gdk::Display]
    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
    #[allow(deprecated)]
    #[doc(alias = "gtk_style_context_set_display")]
    fn set_display(&self, display: &impl IsA<gdk::Display>) {
        unsafe {
            ffi::gtk_style_context_set_display(
                self.as_ref().to_glib_none().0,
                display.as_ref().to_glib_none().0,
            );
        }
    }

    /// Sets the scale to use when getting image assets for the style.
    ///
    /// # Deprecated since 4.10
    ///
    /// You should not use this api
    /// ## `scale`
    /// scale
    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
    #[allow(deprecated)]
    #[doc(alias = "gtk_style_context_set_scale")]
    fn set_scale(&self, scale: i32) {
        unsafe {
            ffi::gtk_style_context_set_scale(self.as_ref().to_glib_none().0, scale);
        }
    }

    /// Sets the state to be used for style matching.
    ///
    /// # Deprecated since 4.10
    ///
    /// You should not use this api
    /// ## `flags`
    /// state to represent
    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
    #[allow(deprecated)]
    #[doc(alias = "gtk_style_context_set_state")]
    fn set_state(&self, flags: StateFlags) {
        unsafe {
            ffi::gtk_style_context_set_state(self.as_ref().to_glib_none().0, flags.into_glib());
        }
    }

    /// Converts the style context into a string representation.
    ///
    /// The string representation always includes information about
    /// the name, state, id, visibility and style classes of the CSS
    /// node that is backing @self. Depending on the flags, more
    /// information may be included.
    ///
    /// This function is intended for testing and debugging of the
    /// CSS implementation in GTK. There are no guarantees about
    /// the format of the returned string, it may change.
    ///
    /// # Deprecated since 4.10
    ///
    /// This api will be removed in GTK 5
    /// ## `flags`
    /// Flags that determine what to print
    ///
    /// # Returns
    ///
    /// a newly allocated string representing @self
    #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
    #[allow(deprecated)]
    #[doc(alias = "gtk_style_context_to_string")]
    fn to_string(&self, flags: StyleContextPrintFlags) -> glib::GString {
        unsafe {
            from_glib_full(ffi::gtk_style_context_to_string(
                self.as_ref().to_glib_none().0,
                flags.into_glib(),
            ))
        }
    }

    #[doc(alias = "display")]
    fn connect_display_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_display_trampoline<
            P: IsA<StyleContext>,
            F: Fn(&P) + 'static,
        >(
            this: *mut ffi::GtkStyleContext,
            _param_spec: glib::ffi::gpointer,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(StyleContext::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::display\0".as_ptr() as *const _,
                Some(std::mem::transmute::<_, unsafe extern "C" fn()>(
                    notify_display_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }
}

impl<O: IsA<StyleContext>> StyleContextExt for O {}