fltk/
button.rs

1use crate::prelude::*;
2use crate::utils::FlString;
3use fltk_sys::button::*;
4use std::ffi::{CStr, CString};
5
6/// Creates a normal button
7#[derive(Debug)]
8pub struct Button {
9    inner: crate::widget::WidgetTracker,
10    is_derived: bool,
11}
12
13crate::macros::widget::impl_widget_ext!(Button, Fl_Button);
14crate::macros::widget::impl_widget_base!(Button, Fl_Button);
15crate::macros::widget::impl_widget_default!(Button, Fl_Button);
16crate::macros::button::impl_button_ext!(Button, Fl_Button);
17
18impl Button {
19    /// Set whether a button is compact
20    pub fn set_compact(&mut self, flag: bool) {
21        unsafe { Fl_Button_set_compact(self.inner.widget() as _, flag.into()) }
22    }
23
24    /// Get whether a button is compact
25    pub fn compact(&self) -> bool {
26        unsafe { Fl_Button_compact(self.inner.widget() as _) != 0 }
27    }
28}
29
30/// Defines the button type, which can be changed dynamically using the `set_type()`.
31#[repr(i32)]
32#[derive(Debug, Copy, Clone, PartialEq, Eq)]
33pub enum ButtonType {
34    /// Normal button
35    Normal = 0,
36    /// Toggle button
37    Toggle = 1,
38    /// Radio button
39    Radio = 102,
40    /// Hidden button
41    Hidden = 3,
42}
43
44crate::macros::widget::impl_widget_type!(ButtonType);
45
46/// Creates a radio button.
47/// Radio meaning only one can be toggled in the same group
48#[derive(Debug)]
49pub struct RadioButton {
50    inner: crate::widget::WidgetTracker,
51    is_derived: bool,
52}
53
54crate::macros::widget::impl_widget_ext!(RadioButton, Fl_Radio_Button);
55crate::macros::widget::impl_widget_base!(RadioButton, Fl_Radio_Button);
56crate::macros::widget::impl_widget_default!(RadioButton, Fl_Radio_Button);
57crate::macros::button::impl_button_ext!(RadioButton, Fl_Radio_Button);
58
59impl RadioButton {
60    /// Check whether a `RadioButton` is toggled
61    pub fn is_toggled(&self) -> bool {
62        unsafe { Fl_Radio_Button_is_toggled(self.inner.widget() as _) != 0 }
63    }
64
65    /// Sets whether the `RadioButton` is toggled or not
66    pub fn toggle(&mut self, val: bool) {
67        unsafe { Fl_Radio_Button_toggle(self.inner.widget() as _, i32::from(val)) }
68    }
69}
70
71/// Creates a radio round button.
72/// Radio meaning only one can be toggled in the same group
73#[derive(Debug)]
74pub struct RadioRoundButton {
75    inner: crate::widget::WidgetTracker,
76    is_derived: bool,
77}
78
79crate::macros::widget::impl_widget_ext!(RadioRoundButton, Fl_Radio_Round_Button);
80crate::macros::widget::impl_widget_base!(RadioRoundButton, Fl_Radio_Round_Button);
81crate::macros::widget::impl_widget_default!(RadioRoundButton, Fl_Radio_Round_Button);
82crate::macros::button::impl_button_ext!(RadioRoundButton, Fl_Radio_Round_Button);
83
84impl RadioRoundButton {
85    /// Check whether a `RadioRoundButton` is toggled
86    pub fn is_toggled(&self) -> bool {
87        unsafe { Fl_Radio_Round_Button_is_toggled(self.inner.widget() as _) != 0 }
88    }
89
90    /// Sets whether the `RadioRoundButton` is toggled or not
91    pub fn toggle(&mut self, val: bool) {
92        unsafe { Fl_Radio_Round_Button_toggle(self.inner.widget() as _, i32::from(val)) }
93    }
94}
95
96/// Creates a radio light button.
97/// Radio meaning only one can be toggled in the same group
98#[derive(Debug)]
99pub struct RadioLightButton {
100    inner: crate::widget::WidgetTracker,
101    is_derived: bool,
102}
103
104crate::macros::widget::impl_widget_ext!(RadioLightButton, Fl_Radio_Light_Button);
105crate::macros::widget::impl_widget_base!(RadioLightButton, Fl_Radio_Light_Button);
106crate::macros::widget::impl_widget_default!(RadioLightButton, Fl_Radio_Light_Button);
107crate::macros::button::impl_button_ext!(RadioLightButton, Fl_Radio_Light_Button);
108
109impl RadioLightButton {
110    /// Check whether a `RadioLightButton` is toggled
111    pub fn is_toggled(&self) -> bool {
112        unsafe { Fl_Radio_Light_Button_is_toggled(self.inner.widget() as _) != 0 }
113    }
114
115    /// Sets whether the `RadioLightButton` is toggled or not
116    pub fn toggle(&mut self, val: bool) {
117        unsafe { Fl_Radio_Light_Button_toggle(self.inner.widget() as _, i32::from(val)) }
118    }
119}
120
121/// Creates a round button
122#[derive(Debug)]
123pub struct RoundButton {
124    inner: crate::widget::WidgetTracker,
125    is_derived: bool,
126}
127
128crate::macros::widget::impl_widget_ext!(RoundButton, Fl_Round_Button);
129crate::macros::widget::impl_widget_base!(RoundButton, Fl_Round_Button);
130crate::macros::widget::impl_widget_default!(RoundButton, Fl_Round_Button);
131crate::macros::button::impl_button_ext!(RoundButton, Fl_Round_Button);
132
133impl RoundButton {
134    /// Check whether a `RoundButton` is toggled
135    pub fn is_toggled(&self) -> bool {
136        unsafe { Fl_Round_Button_is_toggled(self.inner.widget() as _) != 0 }
137    }
138
139    /// Sets whether the `RoundButton` is toggled or not
140    pub fn toggle(&mut self, val: bool) {
141        unsafe { Fl_Round_Button_toggle(self.inner.widget() as _, i32::from(val)) }
142    }
143}
144
145/// Creates a check button
146#[derive(Debug)]
147pub struct CheckButton {
148    inner: crate::widget::WidgetTracker,
149    is_derived: bool,
150}
151
152crate::macros::widget::impl_widget_ext!(CheckButton, Fl_Check_Button);
153crate::macros::widget::impl_widget_base!(CheckButton, Fl_Check_Button);
154crate::macros::widget::impl_widget_default!(CheckButton, Fl_Check_Button);
155crate::macros::button::impl_button_ext!(CheckButton, Fl_Check_Button);
156
157impl CheckButton {
158    /// Check whether a `CheckButton` is checked
159    pub fn is_checked(&self) -> bool {
160        unsafe { Fl_Check_Button_is_checked(self.inner.widget() as _) != 0 }
161    }
162
163    /// Set whether `CheckButton` is checked or not
164    pub fn set_checked(&self, checked: bool) {
165        unsafe {
166            Fl_Check_Button_set_checked(self.inner.widget() as _, i32::from(checked));
167        }
168    }
169}
170
171/// Creates a toggle button
172#[derive(Debug)]
173pub struct ToggleButton {
174    inner: crate::widget::WidgetTracker,
175    is_derived: bool,
176}
177
178crate::macros::widget::impl_widget_ext!(ToggleButton, Fl_Toggle_Button);
179crate::macros::widget::impl_widget_base!(ToggleButton, Fl_Toggle_Button);
180crate::macros::widget::impl_widget_default!(ToggleButton, Fl_Toggle_Button);
181crate::macros::button::impl_button_ext!(ToggleButton, Fl_Toggle_Button);
182
183impl ToggleButton {
184    /// Check whether a `ToggleButton` is toggled
185    pub fn is_toggled(&self) -> bool {
186        unsafe { Fl_Toggle_Button_is_toggled(self.inner.widget() as _) != 0 }
187    }
188
189    /// Sets whether the `ToggleButton` is toggled or not
190    pub fn toggle(&mut self, val: bool) {
191        unsafe { Fl_Toggle_Button_toggle(self.inner.widget() as _, i32::from(val)) }
192    }
193}
194
195/// Creates a light button
196#[derive(Debug)]
197pub struct LightButton {
198    inner: crate::widget::WidgetTracker,
199    is_derived: bool,
200}
201
202crate::macros::widget::impl_widget_ext!(LightButton, Fl_Light_Button);
203crate::macros::widget::impl_widget_base!(LightButton, Fl_Light_Button);
204crate::macros::widget::impl_widget_default!(LightButton, Fl_Light_Button);
205crate::macros::button::impl_button_ext!(LightButton, Fl_Light_Button);
206
207impl LightButton {
208    /// Check whether a `LightButton` is on
209    pub fn is_on(&self) -> bool {
210        unsafe { Fl_Light_Button_is_on(self.inner.widget() as _) != 0 }
211    }
212
213    /// Sets whether the `LightButton` is on or not
214    pub fn turn_on(&mut self, on: bool) {
215        unsafe { Fl_Light_Button_turn_on(self.inner.widget() as _, i32::from(on)) }
216    }
217}
218
219/// Creates a repeat button
220#[derive(Debug)]
221pub struct RepeatButton {
222    inner: crate::widget::WidgetTracker,
223    is_derived: bool,
224}
225
226crate::macros::widget::impl_widget_ext!(RepeatButton, Fl_Repeat_Button);
227crate::macros::widget::impl_widget_base!(RepeatButton, Fl_Repeat_Button);
228crate::macros::widget::impl_widget_default!(RepeatButton, Fl_Repeat_Button);
229crate::macros::button::impl_button_ext!(RepeatButton, Fl_Repeat_Button);
230
231/// Creates a return button
232#[derive(Debug)]
233pub struct ReturnButton {
234    inner: crate::widget::WidgetTracker,
235    is_derived: bool,
236}
237
238crate::macros::widget::impl_widget_ext!(ReturnButton, Fl_Return_Button);
239crate::macros::widget::impl_widget_base!(ReturnButton, Fl_Return_Button);
240crate::macros::widget::impl_widget_default!(ReturnButton, Fl_Return_Button);
241crate::macros::button::impl_button_ext!(ReturnButton, Fl_Return_Button);
242
243/// Creates a Shortcut button
244#[derive(Debug)]
245pub struct ShortcutButton {
246    inner: crate::widget::WidgetTracker,
247    is_derived: bool,
248}
249
250crate::macros::widget::impl_widget_ext!(ShortcutButton, Fl_Shortcut_Button);
251crate::macros::widget::impl_widget_base!(ShortcutButton, Fl_Shortcut_Button);
252crate::macros::widget::impl_widget_default!(ShortcutButton, Fl_Shortcut_Button);
253crate::macros::button::impl_button_ext!(ShortcutButton, Fl_Shortcut_Button);
254
255impl ShortcutButton {
256    /// Gets the Shortcut button value
257    pub fn value(&self) -> crate::enums::Shortcut {
258        unsafe { std::mem::transmute(Fl_Shortcut_Button_value(self.inner.widget() as _)) }
259    }
260
261    /// Sets the Shortcut button value
262    pub fn set_value(&mut self, val: crate::enums::Shortcut) {
263        unsafe { Fl_Shortcut_Button_set_value(self.inner.widget() as _, val.bits()) }
264    }
265
266    /// Gets the Shortcut button default value
267    pub fn default_value(&self) -> crate::enums::Shortcut {
268        unsafe { std::mem::transmute(Fl_Shortcut_Button_default_value(self.inner.widget() as _)) }
269    }
270
271    /// Sets the Shortcut button default value
272    pub fn set_default_value(&mut self, val: crate::enums::Shortcut) {
273        unsafe { Fl_Shortcut_Button_set_default_value(self.inner.widget() as _, val.bits()) }
274    }
275
276    /// Clears the default shortcut
277    pub fn default_clear(&mut self) {
278        unsafe { Fl_Shortcut_Button_default_clear(self.inner.widget() as _) }
279    }
280}