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
// 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::Border;
use crate::StateFlags;
use crate::StyleContextPrintFlags;
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;

glib::wrapper! {
    /// [`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.
    ///
    /// # 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;

    /// Adds a global style provider to @display, which will be used
    /// in style construction for all `GtkStyleContexts` under @display.
    ///
    /// GTK uses this to make styling information from [`Settings`][crate::Settings]
    /// available.
    ///
    /// Note: If both priorities are the same, A [`StyleProvider`][crate::StyleProvider]
    /// added through [`StyleContextExt::add_provider()`][crate::prelude::StyleContextExt::add_provider()] takes
    /// precedence over another added through this function.
    /// ## `display`
    /// a [`gdk::Display`][crate::gdk::Display]
    /// ## `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`
    #[doc(alias = "gtk_style_context_add_provider_for_display")]
    pub fn add_provider_for_display(
        display: &impl IsA<gdk::Display>,
        provider: &impl IsA<StyleProvider>,
        priority: u32,
    ) {
        skip_assert_initialized!();
        unsafe {
            ffi::gtk_style_context_add_provider_for_display(
                display.as_ref().to_glib_none().0,
                provider.as_ref().to_glib_none().0,
                priority,
            );
        }
    }

    /// Removes @provider from the global style providers list in @display.
    /// ## `display`
    /// a [`gdk::Display`][crate::gdk::Display]
    /// ## `provider`
    /// a [`StyleProvider`][crate::StyleProvider]
    #[doc(alias = "gtk_style_context_remove_provider_for_display")]
    pub fn remove_provider_for_display(
        display: &impl IsA<gdk::Display>,
        provider: &impl IsA<StyleProvider>,
    ) {
        skip_assert_initialized!();
        unsafe {
            ffi::gtk_style_context_remove_provider_for_display(
                display.as_ref().to_glib_none().0,
                provider.as_ref().to_glib_none().0,
            );
        }
    }
}

/// Trait containing all [`struct@StyleContext`] methods.
///
/// # Implementors
///
/// [`StyleContext`][struct@crate::StyleContext]
pub trait StyleContextExt: '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 { ... }
    /// ```
    /// ## `class_name`
    /// class name to use in styling
    #[doc(alias = "gtk_style_context_add_class")]
    fn add_class(&self, class_name: &str);

    /// 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()].
    /// ## `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`
    #[doc(alias = "gtk_style_context_add_provider")]
    fn add_provider(&self, provider: &impl IsA<StyleProvider>, priority: u32);

    /// Gets the border for a given state as a [`Border`][crate::Border].
    ///
    /// # Returns
    ///
    ///
    /// ## `border`
    /// return value for the border settings
    #[doc(alias = "gtk_style_context_get_border")]
    #[doc(alias = "get_border")]
    fn border(&self) -> Border;

    /// Gets the foreground color for a given state.
    ///
    /// # Returns
    ///
    ///
    /// ## `color`
    /// return value for the foreground color
    #[doc(alias = "gtk_style_context_get_color")]
    #[doc(alias = "get_color")]
    fn color(&self) -> gdk::RGBA;

    /// Returns the [`gdk::Display`][crate::gdk::Display] to which @self is attached.
    ///
    /// # Returns
    ///
    /// a [`gdk::Display`][crate::gdk::Display].
    #[doc(alias = "gtk_style_context_get_display")]
    #[doc(alias = "get_display")]
    fn display(&self) -> gdk::Display;

    /// Gets the margin for a given state as a [`Border`][crate::Border].
    ///
    /// # Returns
    ///
    ///
    /// ## `margin`
    /// return value for the margin settings
    #[doc(alias = "gtk_style_context_get_margin")]
    #[doc(alias = "get_margin")]
    fn margin(&self) -> Border;

    /// Gets the padding for a given state as a [`Border`][crate::Border].
    ///
    /// # Returns
    ///
    ///
    /// ## `padding`
    /// return value for the padding settings
    #[doc(alias = "gtk_style_context_get_padding")]
    #[doc(alias = "get_padding")]
    fn padding(&self) -> Border;

    /// Returns the scale used for assets.
    ///
    /// # Returns
    ///
    /// the scale
    #[doc(alias = "gtk_style_context_get_scale")]
    #[doc(alias = "get_scale")]
    fn scale(&self) -> i32;

    /// 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()].
    ///
    /// # Returns
    ///
    /// the state flags
    #[doc(alias = "gtk_style_context_get_state")]
    #[doc(alias = "get_state")]
    fn state(&self) -> StateFlags;

    /// Returns [`true`] if @self currently has defined the
    /// given class name.
    /// ## `class_name`
    /// a class name
    ///
    /// # Returns
    ///
    /// [`true`] if @self has @class_name defined
    #[doc(alias = "gtk_style_context_has_class")]
    fn has_class(&self, class_name: &str) -> bool;

    /// Looks up and resolves a color name in the @self color map.
    /// ## `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
    #[doc(alias = "gtk_style_context_lookup_color")]
    fn lookup_color(&self, color_name: &str) -> Option<gdk::RGBA>;

    /// Removes @class_name from @self.
    /// ## `class_name`
    /// class name to remove
    #[doc(alias = "gtk_style_context_remove_class")]
    fn remove_class(&self, class_name: &str);

    /// Removes @provider from the style providers list in @self.
    /// ## `provider`
    /// a [`StyleProvider`][crate::StyleProvider]
    #[doc(alias = "gtk_style_context_remove_provider")]
    fn remove_provider(&self, provider: &impl IsA<StyleProvider>);

    /// Restores @self state to a previous stage.
    ///
    /// See [`save()`][Self::save()].
    #[doc(alias = "gtk_style_context_restore")]
    fn restore(&self);

    /// 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.
    #[doc(alias = "gtk_style_context_save")]
    fn save(&self);

    /// 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.
    /// ## `display`
    /// a [`gdk::Display`][crate::gdk::Display]
    #[doc(alias = "gtk_style_context_set_display")]
    fn set_display(&self, display: &impl IsA<gdk::Display>);

    /// Sets the scale to use when getting image assets for the style.
    /// ## `scale`
    /// scale
    #[doc(alias = "gtk_style_context_set_scale")]
    fn set_scale(&self, scale: i32);

    /// Sets the state to be used for style matching.
    /// ## `flags`
    /// state to represent
    #[doc(alias = "gtk_style_context_set_state")]
    fn set_state(&self, flags: StateFlags);

    /// 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.
    /// ## `flags`
    /// Flags that determine what to print
    ///
    /// # Returns
    ///
    /// a newly allocated string representing @self
    #[doc(alias = "gtk_style_context_to_string")]
    fn to_string(&self, flags: StyleContextPrintFlags) -> glib::GString;

    #[doc(alias = "display")]
    fn connect_display_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;
}

impl<O: IsA<StyleContext>> StyleContextExt for O {
    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,
            );
        }
    }

    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,
            );
        }
    }

    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
        }
    }

    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
        }
    }

    fn display(&self) -> gdk::Display {
        unsafe {
            from_glib_none(ffi::gtk_style_context_get_display(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    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
        }
    }

    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
        }
    }

    fn scale(&self) -> i32 {
        unsafe { ffi::gtk_style_context_get_scale(self.as_ref().to_glib_none().0) }
    }

    fn state(&self) -> StateFlags {
        unsafe {
            from_glib(ffi::gtk_style_context_get_state(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    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,
            ))
        }
    }

    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
            }
        }
    }

    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,
            );
        }
    }

    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,
            );
        }
    }

    fn restore(&self) {
        unsafe {
            ffi::gtk_style_context_restore(self.as_ref().to_glib_none().0);
        }
    }

    fn save(&self) {
        unsafe {
            ffi::gtk_style_context_save(self.as_ref().to_glib_none().0);
        }
    }

    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,
            );
        }
    }

    fn set_scale(&self, scale: i32) {
        unsafe {
            ffi::gtk_style_context_set_scale(self.as_ref().to_glib_none().0, scale);
        }
    }

    fn set_state(&self, flags: StateFlags) {
        unsafe {
            ffi::gtk_style_context_set_state(self.as_ref().to_glib_none().0, flags.into_glib());
        }
    }

    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(),
            ))
        }
    }

    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(transmute::<_, unsafe extern "C" fn()>(
                    notify_display_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }
}

impl fmt::Display for StyleContext {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str("StyleContext")
    }
}