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
use std::hash::Hash;
use std::any::TypeId;
use winapi::{HWND, HFONT};
use user32::SendMessageW;
use ui::Ui;
use controls::{Control, ControlT, ControlType, AnyHandle};
use error::Error;
use events::Event;
use defs::CheckState;
#[derive(Clone)]
pub struct RadioButtonT<S: Clone+Into<String>, ID: Hash+Clone> {
pub text: S,
pub position: (i32, i32),
pub size: (u32, u32),
pub visible: bool,
pub disabled: bool,
pub parent: ID,
pub checkstate: CheckState,
pub font: Option<ID>,
}
impl<S: Clone+Into<String>, ID: Hash+Clone> ControlT<ID> for RadioButtonT<S, ID> {
fn type_id(&self) -> TypeId { TypeId::of::<RadioButton>() }
fn events(&self) -> Vec<Event> {
vec![Event::Destroyed, Event::Click, Event::DoubleClick, Event::Focus, Event::Moved, Event::Resized, Event::Raw]
}
fn build(&self, ui: &Ui<ID>) -> Result<Box<Control>, Error> {
use low::window_helper::{WindowParams, build_window, set_window_font, handle_of_window, handle_of_font};
use winapi::{DWORD, WS_VISIBLE, WS_DISABLED, WS_CHILD, BS_NOTIFY, BS_AUTORADIOBUTTON, BS_TEXT};
let flags: DWORD = WS_CHILD | BS_NOTIFY | BS_TEXT | BS_AUTORADIOBUTTON |
if self.visible { WS_VISIBLE } else { 0 } |
if self.disabled { WS_DISABLED } else { 0 } ;
let parent = match handle_of_window(ui, &self.parent, "The parent of a checkbox must be a window-like control.") {
Ok(h) => h,
Err(e) => { return Err(e); }
};
let font_handle: Option<HFONT> = match self.font.as_ref() {
Some(font_id) =>
match handle_of_font(ui, &font_id, "The font of a checkbox must be a font resource.") {
Ok(h) => Some(h),
Err(e) => { return Err(e); }
},
None => None
};
let params = WindowParams {
title: self.text.clone().into(),
class_name: "BUTTON",
position: self.position.clone(),
size: self.size.clone(),
flags: flags,
ex_flags: Some(0),
parent: parent
};
match unsafe{ build_window(params) } {
Ok(h) => {
unsafe{
set_window_font(h, font_handle, true);
set_checkstate(h, &self.checkstate);
}
Ok( Box::new(RadioButton{handle: h}) )
},
Err(e) => Err(Error::System(e))
}
}
}
pub struct RadioButton {
handle: HWND
}
impl RadioButton {
pub fn get_checkstate(&self) -> CheckState {
use low::defs::{BM_GETCHECK, BST_CHECKED};
match unsafe{ SendMessageW(self.handle, BM_GETCHECK, 0 , 0) as u32 } {
BST_CHECKED => CheckState::Checked,
_ => CheckState::Unchecked
}
}
pub fn set_checkstate(&self, check: CheckState) {
let check = if check == CheckState::Indeterminate { CheckState::Checked } else { check };
unsafe{ set_checkstate(self.handle, &check); }
}
pub fn get_text(&self) -> String { unsafe{ ::low::window_helper::get_window_text(self.handle) } }
pub fn set_text<'a>(&self, text: &'a str) { unsafe{ ::low::window_helper::set_window_text(self.handle, text); } }
pub fn get_visibility(&self) -> bool { unsafe{ ::low::window_helper::get_window_visibility(self.handle) } }
pub fn set_visibility(&self, visible: bool) { unsafe{ ::low::window_helper::set_window_visibility(self.handle, visible); }}
pub fn get_position(&self) -> (i32, i32) { unsafe{ ::low::window_helper::get_window_position(self.handle) } }
pub fn set_position(&self, x: i32, y: i32) { unsafe{ ::low::window_helper::set_window_position(self.handle, x, y); }}
pub fn get_size(&self) -> (u32, u32) { unsafe{ ::low::window_helper::get_window_size(self.handle) } }
pub fn set_size(&self, w: u32, h: u32) { unsafe{ ::low::window_helper::set_window_size(self.handle, w, h, false); } }
pub fn get_enabled(&self) -> bool { unsafe{ ::low::window_helper::get_window_enabled(self.handle) } }
pub fn set_enabled(&self, e:bool) { unsafe{ ::low::window_helper::set_window_enabled(self.handle, e); } }
}
impl Control for RadioButton {
fn handle(&self) -> AnyHandle {
AnyHandle::HWND(self.handle)
}
fn control_type(&self) -> ControlType {
ControlType::RadioButton
}
fn free(&mut self) {
use user32::DestroyWindow;
unsafe{ DestroyWindow(self.handle) };
}
}
#[inline(always)]
unsafe fn set_checkstate(handle: HWND, check: &CheckState) {
use low::defs::{BM_SETCHECK, BST_CHECKED, BST_INDETERMINATE, BST_UNCHECKED};
use user32::SendMessageW;
use winapi::WPARAM;
let check_state = match check {
&CheckState::Checked => BST_CHECKED,
&CheckState::Indeterminate => BST_INDETERMINATE,
&CheckState::Unchecked => BST_UNCHECKED
};
SendMessageW(handle, BM_SETCHECK, check_state as WPARAM, 0);
}