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
use std::marker::PhantomPinned;
use std::ops::Index;
use std::pin::Pin;
use std::sync::Arc;
use crate::decl::*;
use crate::gui::{privs::*, *};
use crate::prelude::*;
struct RadioGroupObj {
radios: Vec<RadioButton>,
events: BaseCtrlEvents,
_pin: PhantomPinned,
}
/// A group of native [`RadioButton`](crate::gui::RadioButton) controls.
#[derive(Clone)]
pub struct RadioGroup(Pin<Arc<RadioGroupObj>>);
unsafe impl Send for RadioGroup {}
impl Index<usize> for RadioGroup {
type Output = RadioButton;
fn index(&self, i: usize) -> &Self::Output {
&self.0.radios[i]
}
}
impl RadioGroup {
/// Instantiates a new `RadioGroup` object, each `RadioButton` to be created
/// on the parent window with
/// [`HWND::CreateWindowEx`](crate::HWND::CreateWindowEx).
///
/// # Panics
///
/// Panics if `opts` is empty.
///
/// Panics if the parent window was already created – that is, you cannot
/// dynamically create a `RadioGroup` in an event closure.
#[must_use]
pub fn new(parent: &(impl GuiParent + 'static), opts: &[RadioButtonOpts]) -> Self {
if opts.is_empty() {
panic!("RadioGroup needs at least one RadioButton.");
}
let (ctrl_ids, radios): (Vec<_>, Vec<_>) = opts
.iter()
.enumerate()
.map(|(idx, opt)| {
let radio = RadioButton::new(parent, opt.clone(), idx == 0);
(radio.ctrl_id(), radio) // the constructor may set the ID
})
.unzip();
Self(Arc::pin(RadioGroupObj {
radios,
events: BaseCtrlEvents::new_many(parent, ctrl_ids),
_pin: PhantomPinned,
}))
}
/// Instantiates a new `RadioGroup` object, to be loaded from a dialog
/// resource with [`HWND::GetDlgItem`](crate::HWND::GetDlgItem).
///
/// # Panics
///
/// Panics if `ctrls` is empty.
///
/// Panics if the parent dialog was already created – that is, you cannot
/// dynamically create a `RadioGroup` in an event closure.
#[must_use]
pub fn new_dlg(parent: &(impl GuiParent + 'static), ctrls: &[(u16, Horz, Vert)]) -> Self {
if ctrls.is_empty() {
panic!("RadioGroup needs at least one RadioButton.");
}
let (ctrl_ids, radios): (Vec<_>, Vec<_>) = ctrls
.iter()
.enumerate()
.map(|(idx, (ctrl_id, horz, vert))| {
(*ctrl_id, RadioButton::new_dlg(parent, *ctrl_id, idx == 0, (*horz, *vert)))
})
.unzip();
Self(Arc::pin(RadioGroupObj {
radios,
events: BaseCtrlEvents::new_many(parent, ctrl_ids),
_pin: PhantomPinned,
}))
}
/// Exposes the specific control events.
///
/// # Panics
///
/// Panics if the controls are already created. Events must be set before
/// control creation.
#[must_use]
pub fn on(&self) -> &impl GuiEventsRadioGroup {
if *self.0.radios[0].hwnd() != HWND::NULL {
panic!("Cannot add events after control creation.");
}
&self.0.events
}
/// Returns the number of [`RadioButton`](crate::gui::RadioButton) controls
/// in this group.
#[must_use]
pub fn count(&self) -> usize {
self.0.radios.len()
}
/// Returns an iterator over the internal
/// [`RadioButton`](crate::gui::RadioButton) objects.
///
/// # Example
///
/// Changing the text of all radio buttons to `"One"`:
///
/// ```no_run
/// use winsafe::{self as w, prelude::*, gui};
///
/// let radio_group: gui::RadioGroup; // initialized somewhere
/// # let wnd = gui::WindowMain::new(gui::WindowMainOpts::default());
/// # let radio_group = gui::RadioGroup::new(&wnd, &[]);
///
/// for radio in radio_group.iter() {
/// radio.hwnd().SetWindowText("One");
/// }
/// # w::SysResult::Ok(())
/// ```
#[must_use]
pub fn iter(&self) -> impl DoubleEndedIterator<Item = &RadioButton> {
self.0.radios.iter()
}
/// Returns the currently checked [`RadioButton`](crate::gui::RadioButton)
/// of this group, if any.
#[must_use]
pub fn selected(&self) -> Option<&RadioButton> {
self.selected_index().map(|idx| &self.0.radios[idx])
}
/// Returns the index of the currently selected
/// [`RadioButton`](crate::gui::RadioButton) of this group, if any.
#[must_use]
pub fn selected_index(&self) -> Option<usize> {
self.0.radios.iter().position(|radio| radio.is_selected())
}
}