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