Skip to main content

gtk4/
enums.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::cmp;
4
5use glib::{Quark, translate::*};
6
7use crate::{CssParserWarning, Ordering, ffi, prelude::*};
8
9impl From<cmp::Ordering> for Ordering {
10    #[inline]
11    fn from(o: cmp::Ordering) -> Self {
12        skip_assert_initialized!();
13        match o {
14            cmp::Ordering::Equal => Self::Equal,
15            cmp::Ordering::Greater => Self::Larger,
16            cmp::Ordering::Less => Self::Smaller,
17        }
18    }
19}
20
21impl From<Ordering> for cmp::Ordering {
22    #[inline]
23    fn from(o: Ordering) -> Self {
24        skip_assert_initialized!();
25        match o {
26            Ordering::Equal => Self::Equal,
27            Ordering::Larger => Self::Greater,
28            Ordering::Smaller => Self::Less,
29            Ordering::__Unknown(_) => unreachable!(),
30        }
31    }
32}
33
34impl ErrorDomain for CssParserWarning {
35    #[inline]
36    fn domain() -> Quark {
37        skip_assert_initialized!();
38        unsafe { from_glib(ffi::gtk_css_parser_warning_quark()) }
39    }
40
41    #[inline]
42    fn code(self) -> i32 {
43        self.into_glib()
44    }
45
46    #[inline]
47    fn from(code: i32) -> Option<Self> {
48        skip_assert_initialized!();
49        match code {
50            ffi::GTK_CSS_PARSER_WARNING_DEPRECATED => Some(Self::Deprecated),
51            ffi::GTK_CSS_PARSER_WARNING_SYNTAX => Some(Self::Syntax),
52            ffi::GTK_CSS_PARSER_WARNING_UNIMPLEMENTED => Some(Self::Unimplemented),
53            value => Some(Self::__Unknown(value)),
54        }
55    }
56}
57
58///  allocation,
59/// for example if you packed the widget with the [`hexpand`][struct@crate::Widget#hexpand]
60/// property inside a [`Box`][crate::Box], then the widget might get extra space.
61/// If you have for example a 16x16 icon inside a 32x32 space, the icon
62/// could be scaled and stretched, it could be centered, or it could be
63/// positioned to one side of the space.
64///
65/// Note that in horizontal context `GTK_ALIGN_START` and `GTK_ALIGN_END`
66/// are interpreted relative to text direction.
67///
68/// Baseline support is optional for containers and widgets, and is only available
69/// for vertical alignment. `GTK_ALIGN_BASELINE_CENTER` and `GTK_ALIGN_BASELINE_FILL`
70/// are treated similar to `GTK_ALIGN_CENTER` and `GTK_ALIGN_FILL`, except that it
71/// positions the widget to line up the baselines, where that is supported.
72#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Clone, Copy)]
73#[non_exhaustive]
74#[doc(alias = "GtkAlign")]
75pub enum Align {
76    /// stretch to fill all space if possible, center if
77    ///   no meaningful way to stretch
78    #[doc(alias = "GTK_ALIGN_FILL")]
79    Fill,
80    /// snap to left or top side, leaving space on right or bottom
81    #[doc(alias = "GTK_ALIGN_START")]
82    Start,
83    /// snap to right or bottom side, leaving space on left or top
84    #[doc(alias = "GTK_ALIGN_END")]
85    End,
86    /// center natural width of widget inside the allocation
87    #[doc(alias = "GTK_ALIGN_CENTER")]
88    Center,
89    /// align the widget according to the baseline.
90    /// Use `GTK_ALIGN_BASELINE_FILL` instead
91    #[doc(alias = "GTK_ALIGN_BASELINE")]
92    Baseline,
93    /// stretch to fill all space, but align the baseline.
94    #[cfg(feature = "v4_12")]
95    #[cfg_attr(docsrs, doc(cfg(feature = "v4_12")))]
96    #[doc(alias = "GTK_ALIGN_BASELINE_FILL")]
97    BaselineFill,
98    /// align the baseline.
99    #[cfg(feature = "v4_12")]
100    #[cfg_attr(docsrs, doc(cfg(feature = "v4_12")))]
101    #[doc(alias = "GTK_ALIGN_BASELINE_CENTER")]
102    BaselineCenter,
103    #[doc(hidden)]
104    __Unknown(i32),
105}
106
107impl std::fmt::Display for Align {
108    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
109        write!(
110            f,
111            "Align::{}",
112            match *self {
113                Self::Fill => "Fill",
114                Self::Start => "Start",
115                Self::End => "End",
116                Self::Center => "Center",
117                Self::Baseline => "Baseline",
118                #[cfg(feature = "v4_12")]
119                Self::BaselineFill => "BaselineFill",
120                #[cfg(feature = "v4_12")]
121                Self::BaselineCenter => "BaselineCenter",
122                _ => "Unknown",
123            }
124        )
125    }
126}
127
128#[doc(hidden)]
129impl IntoGlib for Align {
130    type GlibType = ffi::GtkAlign;
131
132    #[inline]
133    fn into_glib(self) -> ffi::GtkAlign {
134        match self {
135            Self::Fill => ffi::GTK_ALIGN_FILL,
136            Self::Start => ffi::GTK_ALIGN_START,
137            Self::End => ffi::GTK_ALIGN_END,
138            Self::Center => ffi::GTK_ALIGN_CENTER,
139            #[cfg(not(feature = "v4_12"))]
140            Self::Baseline => ffi::GTK_ALIGN_BASELINE,
141            #[cfg(feature = "v4_12")]
142            Self::BaselineFill | Self::Baseline => ffi::GTK_ALIGN_BASELINE_FILL,
143            #[cfg(feature = "v4_12")]
144            Self::BaselineCenter => ffi::GTK_ALIGN_BASELINE_CENTER,
145            Self::__Unknown(value) => value,
146        }
147    }
148}
149
150#[doc(hidden)]
151impl FromGlib<ffi::GtkAlign> for Align {
152    #[inline]
153    unsafe fn from_glib(value: ffi::GtkAlign) -> Self {
154        skip_assert_initialized!();
155
156        match value {
157            ffi::GTK_ALIGN_FILL => Self::Fill,
158            ffi::GTK_ALIGN_START => Self::Start,
159            ffi::GTK_ALIGN_END => Self::End,
160            ffi::GTK_ALIGN_CENTER => Self::Center,
161            #[cfg(not(feature = "v4_12"))]
162            ffi::GTK_ALIGN_BASELINE => Self::Baseline,
163            #[cfg(feature = "v4_12")]
164            ffi::GTK_ALIGN_BASELINE_FILL => Self::BaselineFill,
165            #[cfg(feature = "v4_12")]
166            ffi::GTK_ALIGN_BASELINE_CENTER => Self::BaselineCenter,
167            value => Self::__Unknown(value),
168        }
169    }
170}
171
172impl StaticType for Align {
173    #[inline]
174    #[doc(alias = "gtk_align_get_type")]
175    fn static_type() -> glib::Type {
176        unsafe { from_glib(ffi::gtk_align_get_type()) }
177    }
178}
179
180impl glib::HasParamSpec for Align {
181    type ParamSpec = glib::ParamSpecEnum;
182    type SetValue = Self;
183    type BuilderFn = fn(&str, Self) -> glib::ParamSpecEnumBuilder<Self>;
184
185    fn param_spec_builder() -> Self::BuilderFn {
186        Self::ParamSpec::builder_with_default
187    }
188}
189
190impl glib::value::ValueType for Align {
191    type Type = Self;
192}
193
194unsafe impl<'a> glib::value::FromValue<'a> for Align {
195    type Checker = glib::value::GenericValueTypeChecker<Self>;
196
197    #[inline]
198    unsafe fn from_value(value: &'a glib::Value) -> Self {
199        unsafe {
200            skip_assert_initialized!();
201            from_glib(glib::gobject_ffi::g_value_get_enum(value.to_glib_none().0))
202        }
203    }
204}
205
206impl glib::value::ToValue for Align {
207    #[inline]
208    fn to_value(&self) -> glib::Value {
209        let mut value = glib::Value::for_value_type::<Self>();
210        unsafe {
211            glib::gobject_ffi::g_value_set_enum(value.to_glib_none_mut().0, self.into_glib());
212        }
213        value
214    }
215
216    #[inline]
217    fn value_type(&self) -> glib::Type {
218        Self::static_type()
219    }
220}
221
222impl From<Align> for glib::Value {
223    #[inline]
224    fn from(v: Align) -> Self {
225        skip_assert_initialized!();
226        glib::value::ToValue::to_value(&v)
227    }
228}