gtk4/auto/style_context.rs
1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// DO NOT EDIT
4#![allow(deprecated)]
5
6use crate::{Border, StateFlags, StyleContextPrintFlags, StyleProvider, ffi};
7use glib::{
8 prelude::*,
9 signal::{SignalHandlerId, connect_raw},
10 translate::*,
11};
12use std::boxed::Box as Box_;
13
14glib::wrapper! {
15 /// The relevant API has been moved to [`Widget`][crate::Widget]
16 /// where applicable; otherwise, there is no replacement for querying the
17 /// style machinery. Stylable UI elements should use widgets.
18 /// [`StyleContext`][crate::StyleContext] stores styling information affecting a widget.
19 ///
20 /// In order to construct the final style information, [`StyleContext`][crate::StyleContext]
21 /// queries information from all attached `GtkStyleProviders`. Style
22 /// providers can be either attached explicitly to the context through
23 /// [`StyleContextExt::add_provider()`][crate::prelude::StyleContextExt::add_provider()], or to the display through
24 /// [`add_provider_for_display()`][Self::add_provider_for_display()]. The resulting
25 /// style is a combination of all providers’ information in priority order.
26 ///
27 /// For GTK widgets, any [`StyleContext`][crate::StyleContext] returned by
28 /// [`WidgetExt::style_context()`][crate::prelude::WidgetExt::style_context()] will already have a [`gdk::Display`][crate::gdk::Display]
29 /// and RTL/LTR information set. The style context will also be updated
30 /// automatically if any of these settings change on the widget.
31 ///
32 /// ## Style Classes
33 ///
34 /// Widgets can add style classes to their context, which can be used to associate
35 /// different styles by class. The documentation for individual widgets lists
36 /// which style classes it uses itself, and which style classes may be added by
37 /// applications to affect their appearance.
38 ///
39 /// # Custom styling in UI libraries and applications
40 ///
41 /// If you are developing a library with custom widgets that render differently
42 /// than standard components, you may need to add a [`StyleProvider`][crate::StyleProvider] yourself
43 /// with the `GTK_STYLE_PROVIDER_PRIORITY_FALLBACK` priority, either a
44 /// [`CssProvider`][crate::CssProvider] or a custom object implementing the [`StyleProvider`][crate::StyleProvider]
45 /// interface. This way themes may still attempt to style your UI elements in
46 /// a different way if needed so.
47 ///
48 /// If you are using custom styling on an applications, you probably want then
49 /// to make your style information prevail to the theme’s, so you must use
50 /// a [`StyleProvider`][crate::StyleProvider] with the `GTK_STYLE_PROVIDER_PRIORITY_APPLICATION`
51 /// priority, keep in mind that the user settings in
52 /// `XDG_CONFIG_HOME/gtk-4.0/gtk.css` will
53 /// still take precedence over your changes, as it uses the
54 /// `GTK_STYLE_PROVIDER_PRIORITY_USER` priority.
55 ///
56 /// ## Properties
57 ///
58 ///
59 /// #### `display`
60 /// The display of the style context.
61 ///
62 /// Readable | Writeable
63 ///
64 /// # Implements
65 ///
66 /// [`StyleContextExt`][trait@crate::prelude::StyleContextExt], [`trait@glib::ObjectExt`]
67 #[doc(alias = "GtkStyleContext")]
68 pub struct StyleContext(Object<ffi::GtkStyleContext, ffi::GtkStyleContextClass>);
69
70 match fn {
71 type_ => || ffi::gtk_style_context_get_type(),
72 }
73}
74
75impl StyleContext {
76 pub const NONE: Option<&'static StyleContext> = None;
77}
78
79/// Trait containing all [`struct@StyleContext`] methods.
80///
81/// # Implementors
82///
83/// [`StyleContext`][struct@crate::StyleContext]
84pub trait StyleContextExt: IsA<StyleContext> + 'static {
85 /// Adds a style class to @self, so later uses of the
86 /// style context will make use of this new class for styling.
87 ///
88 /// In the CSS file format, a [`Entry`][crate::Entry] defining a “search”
89 /// class, would be matched by:
90 ///
91 /// ```css
92 /// entry.search { ... }
93 /// ```
94 ///
95 /// While any widget defining a “search” class would be
96 /// matched by:
97 /// ```css
98 /// .search { ... }
99 /// ```
100 ///
101 /// # Deprecated since 4.10
102 ///
103 /// Use [`WidgetExt::add_css_class()`][crate::prelude::WidgetExt::add_css_class()] instead
104 /// ## `class_name`
105 /// class name to use in styling
106 #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
107 #[allow(deprecated)]
108 #[doc(alias = "gtk_style_context_add_class")]
109 fn add_class(&self, class_name: &str) {
110 unsafe {
111 ffi::gtk_style_context_add_class(
112 self.as_ref().to_glib_none().0,
113 class_name.to_glib_none().0,
114 );
115 }
116 }
117
118 /// Adds a style provider to @self, to be used in style construction.
119 ///
120 /// Note that a style provider added by this function only affects
121 /// the style of the widget to which @self belongs. If you want
122 /// to affect the style of all widgets, use
123 /// [`StyleContext::add_provider_for_display()`][crate::StyleContext::add_provider_for_display()].
124 ///
125 /// Note: If both priorities are the same, a [`StyleProvider`][crate::StyleProvider]
126 /// added through this function takes precedence over another added
127 /// through [`StyleContext::add_provider_for_display()`][crate::StyleContext::add_provider_for_display()].
128 ///
129 /// # Deprecated since 4.10
130 ///
131 /// Use style classes instead
132 /// ## `provider`
133 /// a [`StyleProvider`][crate::StyleProvider]
134 /// ## `priority`
135 /// the priority of the style provider. The lower
136 /// it is, the earlier it will be used in the style construction.
137 /// Typically this will be in the range between
138 /// `GTK_STYLE_PROVIDER_PRIORITY_FALLBACK` and
139 /// `GTK_STYLE_PROVIDER_PRIORITY_USER`
140 #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
141 #[allow(deprecated)]
142 #[doc(alias = "gtk_style_context_add_provider")]
143 fn add_provider(&self, provider: &impl IsA<StyleProvider>, priority: u32) {
144 unsafe {
145 ffi::gtk_style_context_add_provider(
146 self.as_ref().to_glib_none().0,
147 provider.as_ref().to_glib_none().0,
148 priority,
149 );
150 }
151 }
152
153 /// Gets the border for a given state as a [`Border`][crate::Border].
154 ///
155 /// # Deprecated since 4.10
156 ///
157 /// This api will be removed in GTK 5
158 ///
159 /// # Returns
160 ///
161 ///
162 /// ## `border`
163 /// return value for the border settings
164 #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
165 #[allow(deprecated)]
166 #[doc(alias = "gtk_style_context_get_border")]
167 #[doc(alias = "get_border")]
168 fn border(&self) -> Border {
169 unsafe {
170 let mut border = Border::uninitialized();
171 ffi::gtk_style_context_get_border(
172 self.as_ref().to_glib_none().0,
173 border.to_glib_none_mut().0,
174 );
175 border
176 }
177 }
178
179 /// Gets the foreground color for a given state.
180 ///
181 /// # Deprecated since 4.10
182 ///
183 /// Use [`WidgetExt::color()`][crate::prelude::WidgetExt::color()] instead
184 ///
185 /// # Returns
186 ///
187 ///
188 /// ## `color`
189 /// return value for the foreground color
190 #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
191 #[allow(deprecated)]
192 #[doc(alias = "gtk_style_context_get_color")]
193 #[doc(alias = "get_color")]
194 fn color(&self) -> gdk::RGBA {
195 unsafe {
196 let mut color = gdk::RGBA::uninitialized();
197 ffi::gtk_style_context_get_color(
198 self.as_ref().to_glib_none().0,
199 color.to_glib_none_mut().0,
200 );
201 color
202 }
203 }
204
205 /// Returns the [`gdk::Display`][crate::gdk::Display] to which @self is attached.
206 ///
207 /// # Deprecated since 4.10
208 ///
209 /// Use [`WidgetExt::display()`][crate::prelude::WidgetExt::display()] instead
210 ///
211 /// # Returns
212 ///
213 /// a [`gdk::Display`][crate::gdk::Display].
214 #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
215 #[allow(deprecated)]
216 #[doc(alias = "gtk_style_context_get_display")]
217 #[doc(alias = "get_display")]
218 fn display(&self) -> gdk::Display {
219 unsafe {
220 from_glib_none(ffi::gtk_style_context_get_display(
221 self.as_ref().to_glib_none().0,
222 ))
223 }
224 }
225
226 /// Gets the margin for a given state as a [`Border`][crate::Border].
227 ///
228 /// # Deprecated since 4.10
229 ///
230 /// This api will be removed in GTK 5
231 ///
232 /// # Returns
233 ///
234 ///
235 /// ## `margin`
236 /// return value for the margin settings
237 #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
238 #[allow(deprecated)]
239 #[doc(alias = "gtk_style_context_get_margin")]
240 #[doc(alias = "get_margin")]
241 fn margin(&self) -> Border {
242 unsafe {
243 let mut margin = Border::uninitialized();
244 ffi::gtk_style_context_get_margin(
245 self.as_ref().to_glib_none().0,
246 margin.to_glib_none_mut().0,
247 );
248 margin
249 }
250 }
251
252 /// Gets the padding for a given state as a [`Border`][crate::Border].
253 ///
254 /// # Deprecated since 4.10
255 ///
256 /// This api will be removed in GTK 5
257 ///
258 /// # Returns
259 ///
260 ///
261 /// ## `padding`
262 /// return value for the padding settings
263 #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
264 #[allow(deprecated)]
265 #[doc(alias = "gtk_style_context_get_padding")]
266 #[doc(alias = "get_padding")]
267 fn padding(&self) -> Border {
268 unsafe {
269 let mut padding = Border::uninitialized();
270 ffi::gtk_style_context_get_padding(
271 self.as_ref().to_glib_none().0,
272 padding.to_glib_none_mut().0,
273 );
274 padding
275 }
276 }
277
278 /// Returns the scale used for assets.
279 ///
280 /// # Deprecated since 4.10
281 ///
282 /// Use [`WidgetExt::scale_factor()`][crate::prelude::WidgetExt::scale_factor()] instead
283 ///
284 /// # Returns
285 ///
286 /// the scale
287 #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
288 #[allow(deprecated)]
289 #[doc(alias = "gtk_style_context_get_scale")]
290 #[doc(alias = "get_scale")]
291 fn scale(&self) -> i32 {
292 unsafe { ffi::gtk_style_context_get_scale(self.as_ref().to_glib_none().0) }
293 }
294
295 /// Returns the state used for style matching.
296 ///
297 /// This method should only be used to retrieve the [`StateFlags`][crate::StateFlags]
298 /// to pass to [`StyleContext`][crate::StyleContext] methods, like
299 /// [`padding()`][Self::padding()].
300 /// If you need to retrieve the current state of a [`Widget`][crate::Widget], use
301 /// [`WidgetExt::state_flags()`][crate::prelude::WidgetExt::state_flags()].
302 ///
303 /// # Deprecated since 4.10
304 ///
305 /// Use [`WidgetExt::state_flags()`][crate::prelude::WidgetExt::state_flags()] instead
306 ///
307 /// # Returns
308 ///
309 /// the state flags
310 #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
311 #[allow(deprecated)]
312 #[doc(alias = "gtk_style_context_get_state")]
313 #[doc(alias = "get_state")]
314 fn state(&self) -> StateFlags {
315 unsafe {
316 from_glib(ffi::gtk_style_context_get_state(
317 self.as_ref().to_glib_none().0,
318 ))
319 }
320 }
321
322 /// Returns [`true`] if @self currently has defined the
323 /// given class name.
324 ///
325 /// # Deprecated since 4.10
326 ///
327 /// Use [`WidgetExt::has_css_class()`][crate::prelude::WidgetExt::has_css_class()] instead
328 /// ## `class_name`
329 /// a class name
330 ///
331 /// # Returns
332 ///
333 /// [`true`] if @self has @class_name defined
334 #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
335 #[allow(deprecated)]
336 #[doc(alias = "gtk_style_context_has_class")]
337 fn has_class(&self, class_name: &str) -> bool {
338 unsafe {
339 from_glib(ffi::gtk_style_context_has_class(
340 self.as_ref().to_glib_none().0,
341 class_name.to_glib_none().0,
342 ))
343 }
344 }
345
346 /// Looks up and resolves a color name in the @self color map.
347 ///
348 /// # Deprecated since 4.10
349 ///
350 /// This api will be removed in GTK 5
351 /// ## `color_name`
352 /// color name to lookup
353 ///
354 /// # Returns
355 ///
356 /// [`true`] if @color_name was found and resolved, [`false`] otherwise
357 ///
358 /// ## `color`
359 /// Return location for the looked up color
360 #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
361 #[allow(deprecated)]
362 #[doc(alias = "gtk_style_context_lookup_color")]
363 fn lookup_color(&self, color_name: &str) -> Option<gdk::RGBA> {
364 unsafe {
365 let mut color = gdk::RGBA::uninitialized();
366 let ret = from_glib(ffi::gtk_style_context_lookup_color(
367 self.as_ref().to_glib_none().0,
368 color_name.to_glib_none().0,
369 color.to_glib_none_mut().0,
370 ));
371 if ret { Some(color) } else { None }
372 }
373 }
374
375 /// Removes @class_name from @self.
376 ///
377 /// # Deprecated since 4.10
378 ///
379 /// Use [`WidgetExt::remove_css_class()`][crate::prelude::WidgetExt::remove_css_class()] instead
380 /// ## `class_name`
381 /// class name to remove
382 #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
383 #[allow(deprecated)]
384 #[doc(alias = "gtk_style_context_remove_class")]
385 fn remove_class(&self, class_name: &str) {
386 unsafe {
387 ffi::gtk_style_context_remove_class(
388 self.as_ref().to_glib_none().0,
389 class_name.to_glib_none().0,
390 );
391 }
392 }
393
394 /// Removes @provider from the style providers list in @self.
395 ///
396 /// # Deprecated since 4.10
397 ///
398 /// ## `provider`
399 /// a [`StyleProvider`][crate::StyleProvider]
400 #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
401 #[allow(deprecated)]
402 #[doc(alias = "gtk_style_context_remove_provider")]
403 fn remove_provider(&self, provider: &impl IsA<StyleProvider>) {
404 unsafe {
405 ffi::gtk_style_context_remove_provider(
406 self.as_ref().to_glib_none().0,
407 provider.as_ref().to_glib_none().0,
408 );
409 }
410 }
411
412 /// Restores @self state to a previous stage.
413 ///
414 /// See [`save()`][Self::save()].
415 ///
416 /// # Deprecated since 4.10
417 ///
418 /// This API will be removed in GTK 5
419 #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
420 #[allow(deprecated)]
421 #[doc(alias = "gtk_style_context_restore")]
422 fn restore(&self) {
423 unsafe {
424 ffi::gtk_style_context_restore(self.as_ref().to_glib_none().0);
425 }
426 }
427
428 /// Saves the @self state.
429 ///
430 /// This allows temporary modifications done through
431 /// [`add_class()`][Self::add_class()],
432 /// [`remove_class()`][Self::remove_class()],
433 /// [`set_state()`][Self::set_state()] to be quickly
434 /// reverted in one go through [`restore()`][Self::restore()].
435 ///
436 /// The matching call to [`restore()`][Self::restore()]
437 /// must be done before GTK returns to the main loop.
438 ///
439 /// # Deprecated since 4.10
440 ///
441 /// This API will be removed in GTK 5
442 #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
443 #[allow(deprecated)]
444 #[doc(alias = "gtk_style_context_save")]
445 fn save(&self) {
446 unsafe {
447 ffi::gtk_style_context_save(self.as_ref().to_glib_none().0);
448 }
449 }
450
451 /// Attaches @self to the given display.
452 ///
453 /// The display is used to add style information from “global”
454 /// style providers, such as the display's [`Settings`][crate::Settings] instance.
455 ///
456 /// If you are using a [`StyleContext`][crate::StyleContext] returned from
457 /// [`WidgetExt::style_context()`][crate::prelude::WidgetExt::style_context()], you do not need to
458 /// call this yourself.
459 ///
460 /// # Deprecated since 4.10
461 ///
462 /// You should not use this api
463 /// ## `display`
464 /// a [`gdk::Display`][crate::gdk::Display]
465 #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
466 #[allow(deprecated)]
467 #[doc(alias = "gtk_style_context_set_display")]
468 #[doc(alias = "display")]
469 fn set_display(&self, display: &impl IsA<gdk::Display>) {
470 unsafe {
471 ffi::gtk_style_context_set_display(
472 self.as_ref().to_glib_none().0,
473 display.as_ref().to_glib_none().0,
474 );
475 }
476 }
477
478 /// Sets the scale to use when getting image assets for the style.
479 ///
480 /// # Deprecated since 4.10
481 ///
482 /// You should not use this api
483 /// ## `scale`
484 /// scale
485 #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
486 #[allow(deprecated)]
487 #[doc(alias = "gtk_style_context_set_scale")]
488 fn set_scale(&self, scale: i32) {
489 unsafe {
490 ffi::gtk_style_context_set_scale(self.as_ref().to_glib_none().0, scale);
491 }
492 }
493
494 /// Sets the state to be used for style matching.
495 ///
496 /// # Deprecated since 4.10
497 ///
498 /// You should not use this api
499 /// ## `flags`
500 /// state to represent
501 #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
502 #[allow(deprecated)]
503 #[doc(alias = "gtk_style_context_set_state")]
504 fn set_state(&self, flags: StateFlags) {
505 unsafe {
506 ffi::gtk_style_context_set_state(self.as_ref().to_glib_none().0, flags.into_glib());
507 }
508 }
509
510 /// Converts the style context into a string representation.
511 ///
512 /// The string representation always includes information about
513 /// the name, state, id, visibility and style classes of the CSS
514 /// node that is backing @self. Depending on the flags, more
515 /// information may be included.
516 ///
517 /// This function is intended for testing and debugging of the
518 /// CSS implementation in GTK. There are no guarantees about
519 /// the format of the returned string, it may change.
520 ///
521 /// # Deprecated since 4.10
522 ///
523 /// This api will be removed in GTK 5
524 /// ## `flags`
525 /// Flags that determine what to print
526 ///
527 /// # Returns
528 ///
529 /// a newly allocated string representing @self
530 #[cfg_attr(feature = "v4_10", deprecated = "Since 4.10")]
531 #[allow(deprecated)]
532 #[doc(alias = "gtk_style_context_to_string")]
533 fn to_string(&self, flags: StyleContextPrintFlags) -> glib::GString {
534 unsafe {
535 from_glib_full(ffi::gtk_style_context_to_string(
536 self.as_ref().to_glib_none().0,
537 flags.into_glib(),
538 ))
539 }
540 }
541
542 #[doc(alias = "display")]
543 fn connect_display_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
544 unsafe extern "C" fn notify_display_trampoline<
545 P: IsA<StyleContext>,
546 F: Fn(&P) + 'static,
547 >(
548 this: *mut ffi::GtkStyleContext,
549 _param_spec: glib::ffi::gpointer,
550 f: glib::ffi::gpointer,
551 ) {
552 unsafe {
553 let f: &F = &*(f as *const F);
554 f(StyleContext::from_glib_borrow(this).unsafe_cast_ref())
555 }
556 }
557 unsafe {
558 let f: Box_<F> = Box_::new(f);
559 connect_raw(
560 self.as_ptr() as *mut _,
561 c"notify::display".as_ptr() as *const _,
562 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
563 notify_display_trampoline::<Self, F> as *const (),
564 )),
565 Box_::into_raw(f),
566 )
567 }
568 }
569}
570
571impl<O: IsA<StyleContext>> StyleContextExt for O {}