1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
use crate::image::Image;
pub use crate::prelude::*;
use fltk_sys::button::*;
use std::{
    ffi::{CStr, CString},
    mem,
    os::raw,
};

/// Creates a normal button
#[derive(WidgetExt, ButtonExt, Debug)]
pub struct Button {
    _inner: *mut Fl_Button,
    _tracker: *mut fltk_sys::fl::Fl_Widget_Tracker,
}

/// Defines the button type, which can be changed dynamically using the set_type function().
#[repr(i32)]
#[derive(WidgetType, Debug, Copy, Clone, PartialEq)]
pub enum ButtonType {
    Normal = 0,
    Toggle = 1,
    Radio = 102,
    Hidden = 3,
}

/// Creates a radio button
#[derive(WidgetExt, ButtonExt, Debug)]
pub struct RadioButton {
    _inner: *mut Fl_Radio_Button,
    _tracker: *mut fltk_sys::fl::Fl_Widget_Tracker,
}

impl RadioButton {
    /// Check whether a RadioButton is toggled
    pub fn is_toggled(&self) -> bool {
        unsafe {
            assert!(!self.was_deleted());
            match Fl_Radio_Button_is_toggled(self._inner) {
                0 => false,
                _ => true,
            }
        }
    }

    /// Sets whether the RadioButton is toggled or not
    pub fn toggle(&mut self, val: bool) {
        assert!(!self.was_deleted());
        unsafe { Fl_Radio_Button_toggle(self._inner, val as i32) }
    }
}

/// Creates a radio round button
#[derive(WidgetExt, ButtonExt, Debug)]
pub struct RadioRoundButton {
    _inner: *mut Fl_Radio_Round_Button,
    _tracker: *mut fltk_sys::fl::Fl_Widget_Tracker,
}

impl RadioRoundButton {
    /// Check whether a RadioRoundButton is toggled
    pub fn is_toggled(&self) -> bool {
        unsafe {
            assert!(!self.was_deleted());
            match Fl_Radio_Round_Button_is_toggled(self._inner) {
                0 => false,
                _ => true,
            }
        }
    }

    /// Sets whether the RadioRoundButton is toggled or not
    pub fn toggle(&mut self, val: bool) {
        assert!(!self.was_deleted());
        unsafe { Fl_Radio_Round_Button_toggle(self._inner, val as i32) }
    }
}

/// Creates a radio light button
#[derive(WidgetExt, ButtonExt, Debug)]
pub struct RadioLightButton {
    _inner: *mut Fl_Radio_Light_Button,
    _tracker: *mut fltk_sys::fl::Fl_Widget_Tracker,
}

impl RadioLightButton {
    /// Check whether a RadioLightButton is toggled
    pub fn is_toggled(&self) -> bool {
        unsafe {
            assert!(!self.was_deleted());
            match Fl_Radio_Light_Button_is_toggled(self._inner) {
                0 => false,
                _ => true,
            }
        }
    }

    /// Sets whether the RadioLightButton is toggled or not
    pub fn toggle(&mut self, val: bool) {
        assert!(!self.was_deleted());
        unsafe { Fl_Radio_Light_Button_toggle(self._inner, val as i32) }
    }
}

/// Creates a round button
#[derive(WidgetExt, ButtonExt, Debug)]
pub struct RoundButton {
    _inner: *mut Fl_Round_Button,
    _tracker: *mut fltk_sys::fl::Fl_Widget_Tracker,
}

impl RoundButton {
    /// Check whether a RoundButton is toggled
    pub fn is_toggled(&self) -> bool {
        unsafe {
            assert!(!self.was_deleted());
            match Fl_Round_Button_is_toggled(self._inner) {
                0 => false,
                _ => true,
            }
        }
    }

    /// Sets whether the RoundButton is toggled or not
    pub fn toggle(&mut self, val: bool) {
        assert!(!self.was_deleted());
        unsafe { Fl_Round_Button_toggle(self._inner, val as i32) }
    }
}

/// Creates a check button
#[derive(WidgetExt, ButtonExt, Debug)]
pub struct CheckButton {
    _inner: *mut Fl_Check_Button,
    _tracker: *mut fltk_sys::fl::Fl_Widget_Tracker,
}

impl CheckButton {
    /// Check whether a CheckButton is checked
    pub fn is_checked(&self) -> bool {
        unsafe {
            assert!(!self.was_deleted());
            match Fl_Check_Button_is_checked(self._inner) {
                0 => false,
                _ => true,
            }
        }
    }

    /// Set whether CheckButton is checked or not
    pub fn set_checked(&self, checked: bool) {
        unsafe {
            assert!(!self.was_deleted());
            Fl_Check_Button_set_checked(self._inner, checked as i32);
        }
    }
}

/// Creates a toggle button
#[derive(WidgetExt, ButtonExt, Debug)]
pub struct ToggleButton {
    _inner: *mut Fl_Toggle_Button,
    _tracker: *mut fltk_sys::fl::Fl_Widget_Tracker,
}

impl ToggleButton {
    /// Check whether a ToggleButton is toggled
    pub fn is_toggled(&self) -> bool {
        unsafe {
            assert!(!self.was_deleted());
            match Fl_Toggle_Button_is_toggled(self._inner) {
                0 => false,
                _ => true,
            }
        }
    }

    /// Sets whether the ToggleButton is toggled or not
    pub fn toggle(&mut self, val: bool) {
        assert!(!self.was_deleted());
        unsafe { Fl_Toggle_Button_toggle(self._inner, val as i32) }
    }
}

/// Creates a light button
#[derive(WidgetExt, ButtonExt, Debug)]
pub struct LightButton {
    _inner: *mut Fl_Light_Button,
    _tracker: *mut fltk_sys::fl::Fl_Widget_Tracker,
}

impl LightButton {
    /// Check whether a LightButton is on
    pub fn is_on(&self) -> bool {
        unsafe {
            assert!(!self.was_deleted());
            match Fl_Light_Button_is_on(self._inner) {
                0 => false,
                _ => true,
            }
        }
    }

    /// Sets whether the LightButton is on or not
    pub fn turn_on(&mut self, on: bool) {
        assert!(!self.was_deleted());
        unsafe { Fl_Light_Button_turn_on(self._inner, on as i32) }
    }
}

/// Creates a repeat button
#[derive(WidgetExt, ButtonExt, Debug)]
pub struct RepeatButton {
    _inner: *mut Fl_Repeat_Button,
    _tracker: *mut fltk_sys::fl::Fl_Widget_Tracker,
}

/// Creates a return button
#[derive(WidgetExt, ButtonExt, Debug)]
pub struct ReturnButton {
    _inner: *mut Fl_Return_Button,
    _tracker: *mut fltk_sys::fl::Fl_Widget_Tracker,
}

#[cfg(test)]
mod button {
    use super::*;
    #[test]
    fn tooltip() {
        let mut but = Button::new(0, 0, 0, 0, "hello");
        but.set_tooltip("tooltip");
        assert!(but.tooltip().unwrap() == "tooltip");
    }
}