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
use std::ffi::CString;
use crate::{
draw_2d::{Font, Image},
util::macros::callback,
};
use nappgui_sys::{
button_OnClick, button_check, button_check3, button_flat, button_flatgle, button_font,
button_get_height, button_get_state, button_get_tag, button_image, button_image_alt,
button_push, button_radio, button_state, button_tag, button_text, button_text_alt,
button_tooltip, button_vpadding,
};
use crate::prelude::*;
/// The buttons are another classic element in graphic interfaces, where we distinguish four types:
/// the push button, checkbox, radiobutton and flat button typical of toolbars
pub struct Button {
pub(crate) inner: *mut nappgui_sys::Button,
}
impl Button {
pub(crate) fn new(ptr: *mut nappgui_sys::Button) -> Self {
if ptr.is_null() {
panic!("ptr is null");
}
Self { inner: ptr }
}
/// Create a push button, the typical \[Accept\], \[Cancel\], etc.
pub fn push() -> Self {
let button = unsafe { button_push() };
Self::new(button)
}
/// Create a checkbox.
pub fn check() -> Self {
let button = unsafe { button_check() };
Self::new(button)
}
/// Create a checkbox with three states.
pub fn check3() -> Self {
let button = unsafe { button_check3() };
Self::new(button)
}
/// Create a radio button.
pub fn radio() -> Self {
let button = unsafe { button_radio() };
Self::new(button)
}
/// Create a flat button, to which an image can be assigned. It is the typical toolbar button.
pub fn flat() -> Self {
let button = unsafe { button_flat() };
Self::new(button)
}
/// Create a flat button with status. The button will alternate between pressed/released each time you click on it.
pub fn flatgle() -> Self {
let button = unsafe { button_flatgle() };
Self::new(button)
}
callback! {
/// Set a function for pressing the button.
pub on_click(Button) => button_OnClick;
}
/// Set the text that the button will display.
///
/// # Remarks
/// In flat buttons, the text will be displayed as tooltip.
pub fn text(&self, text: &str) {
let text = CString::new(text).unwrap();
unsafe { button_text(self.inner, text.as_ptr()) };
}
/// Set an alternative text.
///
/// # Remarks
/// Only applicable on flat buttons with status button_flatgle. It will be displayed when the button is in ekGUI_ON status.
pub fn text_alt(&self, text: &str) {
let text = CString::new(text).unwrap();
unsafe { button_text_alt(self.inner, text.as_ptr()) }
}
/// Set a tooltip for the button. It is a small explanatory text that will appear when the mouse is over the control.
pub fn tooltip(&self, text: &str) {
let text = CString::new(text).unwrap();
unsafe { button_tooltip(self.inner, text.as_ptr()) }
}
/// Set the button font.
pub fn font(&self, font: &Font) {
unsafe { button_font(self.inner, font.inner) }
}
/// Set the icon that will show the button.
///
/// # Remarks
/// Not applicable in checkbox or radiobutton. In flat buttons, the size of the control will be adjusted to the image.
pub fn image(&self, image: &Image) {
unsafe { button_image(self.inner, image.inner) }
}
/// Set an alternative image for the button.
///
/// # Remarks
/// Only applicable on flat buttons with status button_flatgle. It will be displayed when the button is in ekGUI_ON status.
pub fn image_alt(&self, image: &Image) {
unsafe { button_image_alt(self.inner, image.inner) }
}
/// Set the button status.
///
/// # Remarks
/// Not applicable on push buttons button_push.
pub fn state(&self, state: GuiState) {
unsafe { button_state(self.inner, state) }
}
/// Sets a numeric tag for the button.
pub fn tag(&self, tag: u32) {
unsafe { button_tag(self.inner, tag) }
}
/// Sets the inner vertical margin.
pub fn vpadding(&self, padding: f32) {
unsafe { button_vpadding(self.inner, padding) }
}
/// Get button status.
///
/// # Remarks
/// Not applicable on push buttons button_push.
pub fn get_state(&self) -> GuiState {
unsafe { button_get_state(self.inner) }
}
/// Gets the button's tag.
pub fn get_tag(&self) -> u32 {
unsafe { button_get_tag(self.inner) }
}
/// Gets the current height of the control.
pub fn get_height(&self) -> f32 {
unsafe { button_get_height(self.inner) }
}
}