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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
use base::BaseWidget;
use widget_prelude::*;
#[derive(Copy, Clone)]
pub enum VAlign {
Top,
Center,
Bottom,
}
impl VAlign {
fn as_cstr(self) -> &'static str {
use self::VAlign::*;
match self {
Top => cstr!("ATOP"),
Center => cstr!("ACENTER"),
Bottom => cstr!("ABOTTOM"),
}
}
}
#[derive(Copy, Clone)]
pub enum HAlign {
Left,
Center,
Right,
}
impl HAlign {
fn as_cstr(self) -> &'static str {
use self::HAlign::*;
match self {
Left => cstr!("ALEFT"),
Center => cstr!("ACENTER"),
Right => cstr!("ARIGHT"),
}
}
}
#[derive(PartialEq, Eq, Copy, Clone)]
pub enum Orientation {
Vertical,
Horizontal,
}
impl Orientation {
#[doc(hidden)]
pub fn as_cstr(self) -> &'static str {
use self::Orientation::*;
match self {
Vertical => cstr!("VERTICAL"),
Horizontal => cstr!("HORIZONTAL"),
}
}
}
fn raw_handle_vec<B>(widgets: B) -> Vec<IUPPtr> where B: AsRef<[BaseWidget]> {
let mut raw_handles: Vec<_> = widgets.as_ref().iter().cloned().map(BaseWidget::ptr).collect();
raw_handles.push(::std::ptr::null_mut());
raw_handles
}
pub struct AbsoluteBuilder {
handles: Vec<IUPPtr>,
}
impl AbsoluteBuilder {
fn new() -> AbsoluteBuilder {
AbsoluteBuilder { handles: Vec::new() }
}
pub fn add_child_at<W: Widget>(&mut self, x: u32, y: u32, child: W) -> &mut Self {
Absolute::set_child_pos(x, y, child);
self.handles.push(child.ptr());
self
}
}
pub struct Absolute(IUPPtr);
impl Absolute {
pub fn new<F>(build_fn: F) -> Absolute where F: FnOnce(&mut AbsoluteBuilder) {
let mut builder = AbsoluteBuilder::new();
build_fn(&mut builder);
unsafe {
let ptr = ::iup_sys::IupCboxv(builder.handles.as_mut_ptr());
Self::from_ptr(ptr)
}
}
pub fn set_child_pos<W: Widget>(x: u32, y: u32, child: W) {
child.set_int_attribute(::attrs::CX, x as i32);
child.set_int_attribute(::attrs::CY, y as i32);
}
}
impl_widget! { Absolute, "cbox" }
pub struct Horizontal(IUPPtr);
impl Horizontal {
pub fn new<C>(children: C) -> Horizontal where C: AsRef<[BaseWidget]> {
let mut raw_handles = raw_handle_vec(children);
unsafe {
let ptr = ::iup_sys::IupHboxv(raw_handles.as_mut_ptr());
Self::from_ptr(ptr)
}
}
pub fn set_valign(self, valign: VAlign) -> Self {
self.set_const_str_attribute(::attrs::ALIGNMENT_VERT, valign.as_cstr());
self
}
pub fn set_elem_spacing_pixels(self, spacing: u32) -> Self {
self.set_str_attribute(::attrs::GAP, spacing.to_string());
self
}
}
impl_widget! { Horizontal, "hbox" }
pub struct Vertical(IUPPtr);
impl Vertical {
pub fn new<C>(children: C) -> Vertical where C: AsRef<[BaseWidget]> {
let mut raw_handles = raw_handle_vec(children);
unsafe {
let ptr = ::iup_sys::IupVboxv(raw_handles.as_mut_ptr());
Self::from_ptr(ptr)
}
}
pub fn set_halign(self, halign: HAlign) -> Self {
self.set_const_str_attribute(::attrs::ALIGNMENT_HORI, halign.as_cstr());
self
}
pub fn set_elem_spacing_pixels(self, spacing: u32) -> Self {
self.set_str_attribute(::attrs::GAP, spacing.to_string());
self
}
}
impl_widget! { Vertical, "vbox" }
pub struct Grid(IUPPtr);
impl Grid {
pub fn new<C>(children: C) -> Grid where C: AsRef<[BaseWidget]> {
let mut raw_handles = raw_handle_vec(children);
unsafe {
let ptr = ::iup_sys::IupGridBoxv(raw_handles.as_mut_ptr());
Self::from_ptr(ptr)
}
}
pub fn set_valign(self, valign: VAlign) -> Self {
self.set_const_str_attribute(::attrs::ALIGNMENT_VERT, valign.as_cstr());
self
}
pub fn set_halign(self, halign: HAlign) -> Self {
self.set_const_str_attribute(::attrs::ALIGNMENT_HORI, halign.as_cstr());
self
}
pub fn set_ndiv(self, ndiv: u32) -> Self {
self.set_int_attribute(::attrs::NUMDIV, ndiv as i32);
self
}
pub fn set_orientation(&mut self, orientation: Orientation) -> &mut Self {
self.set_const_str_attribute(::attrs::ORIENTATION, orientation.as_cstr());
self
}
}
impl_widget! { Grid, "matrix" }
#[macro_export]
macro_rules! children [
($($child:expr),+,) => (children![$($child),+]);
($($child:expr),+) => ({
use ::kiss_ui::widget::Widget;
vec![$($child.to_base()),+]
});
() => (vec![]);
];