Skip to content

Commit 4ff7f75

Browse files
authored
Merge branch 'master' into 35-enable-disable-controls
2 parents f835bfb + b43042c commit 4ff7f75

File tree

2 files changed

+58
-6
lines changed

2 files changed

+58
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
1010

1111
- `ProgressBar` control for tracking the completion of a task
1212
- `enable()` and `disable()` methods on all controls
13+
- `RadioButtons` control for groups of radio buttons
14+
- `Combobox::selected()` method to retrieve the currently selected index of the combobox
1315

1416
### Changed
1517

iui/src/controls/entry.rs

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
//! User input mechanisms: numbers, colors, and text in various forms.
22
33
use super::Control;
4-
use std::os::raw::c_void;
54
use std::ffi::{CStr, CString};
65
use std::i32;
76
use std::mem;
7+
use std::os::raw::c_void;
88
use ui::UI;
99
use ui_sys::{
10-
self, uiCheckbox, uiCombobox, uiControl, uiEntry, uiMultilineEntry, uiSlider, uiSpinbox,
10+
self, uiCheckbox, uiCombobox, uiControl, uiEntry, uiMultilineEntry, uiRadioButtons, uiSlider,
11+
uiSpinbox,
1112
};
1213

1314
pub trait NumericEntry {
@@ -22,15 +23,15 @@ pub trait TextEntry {
2223
fn on_changed<'ctx, F: FnMut(String) + 'ctx>(&mut self, ctx: &'ctx UI, callback: F);
2324
}
2425

25-
define_control!{
26+
define_control! {
2627
/// Numerical entry control which allows users to set any value in a range by typing or incrementing/decrementing.
27-
rust_type: Spinbox,
28+
rust_type: Spinbox,
2829
sys_type: uiSpinbox
2930
}
3031

31-
define_control!{
32+
define_control! {
3233
/// Numerical entry which allows users to select a value by picking a location along a line.
33-
rust_type: Slider,
34+
rust_type: Slider,
3435
sys_type: uiSlider
3536
}
3637

@@ -227,6 +228,11 @@ impl Combobox {
227228
}
228229
}
229230

231+
/// Returns the index of the currently selected option.
232+
pub fn selected(&self, _ctx: &UI) -> i32 {
233+
unsafe { ui_sys::uiComboboxSelected(self.uiCombobox) }
234+
}
235+
230236
pub fn set_selected(&mut self, _ctx: &UI, value: i32) {
231237
unsafe { ui_sys::uiComboboxSetSelected(self.uiCombobox, value) }
232238
}
@@ -291,3 +297,47 @@ impl Checkbox {
291297
}
292298
}
293299
}
300+
301+
define_control! {
302+
/// A set of toggles; only one can be selected at a time.
303+
rust_type: RadioButtons,
304+
sys_type: uiRadioButtons
305+
}
306+
307+
impl RadioButtons {
308+
pub fn new(_ctx: &UI) -> Self {
309+
unsafe { RadioButtons::from_raw(ui_sys::uiNewRadioButtons()) }
310+
}
311+
312+
pub fn append(&self, _ctx: &UI, name: &str) {
313+
let c_string = CString::new(name.as_bytes().to_vec()).unwrap();
314+
unsafe { ui_sys::uiRadioButtonsAppend(self.uiRadioButtons, c_string.as_ptr()); }
315+
}
316+
317+
pub fn selected(&self, _ctx: &UI) -> i32 {
318+
unsafe { ui_sys::uiRadioButtonsSelected(self.uiRadioButtons) }
319+
}
320+
321+
pub fn set_selected(&mut self, _ctx: &UI, idx: i32) {
322+
unsafe { ui_sys::uiRadioButtonsSetSelected(self.uiRadioButtons, idx); }
323+
}
324+
325+
pub fn on_selected<'ctx, F: FnMut(i32) + 'ctx>(&self, _ctx: &'ctx UI, callback: F) {
326+
unsafe {
327+
let mut data: Box<Box<FnMut(i32)>> = Box::new(Box::new(callback));
328+
ui_sys::uiRadioButtonsOnSelected(
329+
self.uiRadioButtons,
330+
Some(c_callback),
331+
&mut *data as *mut Box<FnMut(i32)> as *mut c_void,
332+
);
333+
mem::forget(data);
334+
}
335+
336+
extern "C" fn c_callback(radio_buttons: *mut uiRadioButtons, data: *mut c_void) {
337+
unsafe {
338+
let val = ui_sys::uiRadioButtonsSelected(radio_buttons);
339+
mem::transmute::<*mut c_void, &mut Box<FnMut(i32)>>(data)(val);
340+
}
341+
}
342+
}
343+
}

0 commit comments

Comments
 (0)