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::{translate::*, Quark};
6
7use crate::{ffi, prelude::*, CssParserWarning, Ordering};
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/// Controls how a widget deals with extra space in a single dimension.
59///
60/// Alignment only matters if the widget receives a “too large” allocation,
61/// for example if you packed the widget with the [`hexpand`][struct@crate::Widget#hexpand]
62/// property inside a [`Box`][crate::Box], then the widget might get extra space.
63/// If you have for example a 16x16 icon inside a 32x32 space, the icon
64/// could be scaled and stretched, it could be centered, or it could be
65/// positioned to one side of the space.
66///
67/// Note that in horizontal context `GTK_ALIGN_START` and `GTK_ALIGN_END`
68/// are interpreted relative to text direction.
69///
70/// Baseline support is optional for containers and widgets, and is only available
71/// for vertical alignment. `GTK_ALIGN_BASELINE_CENTER` and `GTK_ALIGN_BASELINE_FILL`
72/// are treated similar to `GTK_ALIGN_CENTER` and `GTK_ALIGN_FILL`, except that it
73/// positions the widget to line up the baselines, where that is supported.
74#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Clone, Copy)]
75#[non_exhaustive]
76#[doc(alias = "GtkAlign")]
77pub enum Align {
78    /// stretch to fill all space if possible, center if
79    ///   no meaningful way to stretch
80    #[doc(alias = "GTK_ALIGN_FILL")]
81    Fill,
82    /// snap to left or top side, leaving space on right or bottom
83    #[doc(alias = "GTK_ALIGN_START")]
84    Start,
85    /// snap to right or bottom side, leaving space on left or top
86    #[doc(alias = "GTK_ALIGN_END")]
87    End,
88    /// center natural width of widget inside the allocation
89    #[doc(alias = "GTK_ALIGN_CENTER")]
90    Center,
91    /// align the widget according to the baseline.
92    /// Use `GTK_ALIGN_BASELINE_FILL` instead
93    #[doc(alias = "GTK_ALIGN_BASELINE")]
94    Baseline,
95    /// a different name for `GTK_ALIGN_BASELINE`.
96    #[cfg(feature = "v4_12")]
97    #[cfg_attr(docsrs, doc(cfg(feature = "v4_12")))]
98    #[doc(alias = "GTK_ALIGN_BASELINE_FILL")]
99    BaselineFill,
100    /// stretch to fill all space, but align the baseline.
101    #[cfg(feature = "v4_12")]
102    #[cfg_attr(docsrs, doc(cfg(feature = "v4_12")))]
103    #[doc(alias = "GTK_ALIGN_BASELINE_CENTER")]
104    BaselineCenter,
105    #[doc(hidden)]
106    __Unknown(i32),
107}
108
109impl std::fmt::Display for Align {
110    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
111        write!(
112            f,
113            "Align::{}",
114            match *self {
115                Self::Fill => "Fill",
116                Self::Start => "Start",
117                Self::End => "End",
118                Self::Center => "Center",
119                Self::Baseline => "Baseline",
120                #[cfg(feature = "v4_12")]
121                Self::BaselineFill => "BaselineFill",
122                #[cfg(feature = "v4_12")]
123                Self::BaselineCenter => "BaselineCenter",
124                _ => "Unknown",
125            }
126        )
127    }
128}
129
130#[doc(hidden)]
131impl IntoGlib for Align {
132    type GlibType = ffi::GtkAlign;
133
134    #[inline]
135    fn into_glib(self) -> ffi::GtkAlign {
136        match self {
137            Self::Fill => ffi::GTK_ALIGN_FILL,
138            Self::Start => ffi::GTK_ALIGN_START,
139            Self::End => ffi::GTK_ALIGN_END,
140            Self::Center => ffi::GTK_ALIGN_CENTER,
141            #[cfg(not(feature = "v4_12"))]
142            Self::Baseline => ffi::GTK_ALIGN_BASELINE,
143            #[cfg(feature = "v4_12")]
144            Self::BaselineFill | Self::Baseline => ffi::GTK_ALIGN_BASELINE_FILL,
145            #[cfg(feature = "v4_12")]
146            Self::BaselineCenter => ffi::GTK_ALIGN_BASELINE_CENTER,
147            Self::__Unknown(value) => value,
148        }
149    }
150}
151
152#[doc(hidden)]
153impl FromGlib<ffi::GtkAlign> for Align {
154    #[inline]
155    unsafe fn from_glib(value: ffi::GtkAlign) -> Self {
156        skip_assert_initialized!();
157
158        match value {
159            ffi::GTK_ALIGN_FILL => Self::Fill,
160            ffi::GTK_ALIGN_START => Self::Start,
161            ffi::GTK_ALIGN_END => Self::End,
162            ffi::GTK_ALIGN_CENTER => Self::Center,
163            #[cfg(not(feature = "v4_12"))]
164            ffi::GTK_ALIGN_BASELINE => Self::Baseline,
165            #[cfg(feature = "v4_12")]
166            ffi::GTK_ALIGN_BASELINE_FILL => Self::BaselineFill,
167            #[cfg(feature = "v4_12")]
168            ffi::GTK_ALIGN_BASELINE_CENTER => Self::BaselineCenter,
169            value => Self::__Unknown(value),
170        }
171    }
172}
173
174impl StaticType for Align {
175    #[inline]
176    #[doc(alias = "gtk_align_get_type")]
177    fn static_type() -> glib::Type {
178        unsafe { from_glib(ffi::gtk_align_get_type()) }
179    }
180}
181
182impl glib::HasParamSpec for Align {
183    type ParamSpec = glib::ParamSpecEnum;
184    type SetValue = Self;
185    type BuilderFn = fn(&str, Self) -> glib::ParamSpecEnumBuilder<Self>;
186
187    fn param_spec_builder() -> Self::BuilderFn {
188        Self::ParamSpec::builder_with_default
189    }
190}
191
192impl glib::value::ValueType for Align {
193    type Type = Self;
194}
195
196unsafe impl<'a> glib::value::FromValue<'a> for Align {
197    type Checker = glib::value::GenericValueTypeChecker<Self>;
198
199    #[inline]
200    unsafe fn from_value(value: &'a glib::Value) -> Self {
201        skip_assert_initialized!();
202        from_glib(glib::gobject_ffi::g_value_get_enum(value.to_glib_none().0))
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}