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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
use std::hash::Hash;
use std::any::TypeId;
use std::fmt::Display;
use std::mem;
use user32::SendMessageW;
use winapi::{HWND, HFONT, WPARAM};
use ui::Ui;
use controls::{Control, ControlT, ControlType, AnyHandle};
use error::Error;
use events::Event;
use low::other_helper::{to_utf16, from_utf16};
#[derive(Clone)]
pub struct ListBoxT<D: Clone+Display+'static, ID: Hash+Clone> {
pub collection: Vec<D>,
pub position: (i32, i32),
pub size: (u32, u32),
pub visible: bool,
pub disabled: bool,
pub readonly: bool,
pub multi_select: bool,
pub parent: ID,
pub font: Option<ID>,
}
impl<D: Clone+Display+'static, ID: Hash+Clone> ControlT<ID> for ListBoxT<D, ID> {
fn type_id(&self) -> TypeId { TypeId::of::<ListBox<D>>() }
fn events(&self) -> Vec<Event> {
vec![Event::Destroyed, Event::SelectionChanged, 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 low::defs::{LB_ADDSTRING, LBS_HASSTRINGS, LBS_MULTIPLESEL, LBS_NOSEL, LBS_NOTIFY};
use winapi::{DWORD, WS_VISIBLE, WS_DISABLED, WS_CHILD, WS_BORDER, WS_VSCROLL, WS_HSCROLL};
use user32::SendMessageW;
let flags: DWORD = WS_CHILD | WS_BORDER | LBS_HASSTRINGS | WS_VSCROLL | WS_HSCROLL | LBS_NOTIFY |
if self.visible { WS_VISIBLE } else { 0 } |
if self.disabled { WS_DISABLED } else { 0 } |
if self.multi_select { LBS_MULTIPLESEL } else { 0 } |
if self.readonly { LBS_NOSEL } else { 0 };
let parent = match handle_of_window(ui, &self.parent, "The parent of a listbox 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 listbox must be a font resource.") {
Ok(h) => Some(h),
Err(e) => { return Err(e); }
},
None => None
};
let params = WindowParams {
title: "",
class_name: "LISTBOX",
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);
let collection: Vec<D> = self.collection.iter().map(
|s|{
let text = to_utf16(format!("{}", s).as_str());
SendMessageW(h, LB_ADDSTRING, 0, mem::transmute(text.as_ptr()));
s.clone()
}
).collect();
Ok( Box::new(ListBox{handle: h, collection: collection}) )
}
},
Err(e) => Err(Error::System(e))
}
}
}
pub struct ListBox<D: Clone+Display> {
handle: HWND,
collection: Vec<D>
}
impl<D: Clone+Display> ListBox<D> {
pub fn len(&self) -> usize { self.collection.len() }
pub fn collection(&self) -> &Vec<D> { &self.collection }
pub fn collection_mut(&mut self) -> &mut Vec<D> { &mut self.collection }
pub fn sync(&self) {
use low::defs::{LB_RESETCONTENT, LB_ADDSTRING};
unsafe{ SendMessageW(self.handle, LB_RESETCONTENT, 0, 0); }
for i in self.collection.iter() {
let text = to_utf16(format!("{}", i).as_str());
unsafe{ SendMessageW(self.handle, LB_ADDSTRING, 0, mem::transmute(text.as_ptr())); }
}
}
pub fn push(&mut self, item: D) {
use low::defs::LB_ADDSTRING;
let text = to_utf16(format!("{}", item).as_str());
unsafe{ SendMessageW(self.handle, LB_ADDSTRING, 0, mem::transmute(text.as_ptr())); }
self.collection.push(item);
}
pub fn remove(&mut self, index: usize) -> D {
use low::defs::LB_DELETESTRING;
unsafe{ SendMessageW(self.handle, LB_DELETESTRING, index as WPARAM, 0); }
self.collection.remove(index)
}
pub fn insert(&mut self, index: usize, item: D) {
use low::defs::LB_INSERTSTRING;
let text = to_utf16(format!("{}", item).as_str());
unsafe{ SendMessageW(self.handle, LB_INSERTSTRING, index as WPARAM, mem::transmute(text.as_ptr())); }
self.collection.insert(index, item);
}
pub fn get_selected_index(&self) -> Option<usize> {
use low::defs::LB_GETCURSEL;
let index = unsafe{ SendMessageW(self.handle, LB_GETCURSEL, 0, 0) };
if index == -1 { None }
else { Some(index as usize) }
}
pub fn get_selected_indexes(&self) -> Vec<usize> {
use low::defs::{LB_GETSELCOUNT, LB_GETSELITEMS};
let selected_count = unsafe{ SendMessageW(self.handle, LB_GETSELCOUNT, 0, 0) };
if selected_count == 0 || selected_count == -1 {
return Vec::new();
}
unsafe{
let mut buffer: Vec<u32> = Vec::with_capacity(selected_count as usize);
buffer.set_len(selected_count as usize);
SendMessageW(self.handle, LB_GETSELITEMS, selected_count as WPARAM, mem::transmute(buffer.as_mut_ptr()) );
buffer.into_iter().map(|i| i as usize).collect()
}
}
pub fn index_selected(&self, index: usize) -> bool {
use low::defs::LB_GETSEL;
unsafe{ SendMessageW(self.handle, LB_GETSEL, index as WPARAM, 0) > 0 }
}
pub fn set_selected_index(&self, index: usize) {
use low::defs::LB_SETCURSEL;
unsafe{ SendMessageW(self.handle, LB_SETCURSEL, index as WPARAM, 0); }
}
pub fn set_index_selected(&self, index: usize, selected: bool) {
use low::defs::LB_SETSEL;
use winapi::LPARAM;
let selected: WPARAM = (selected == true) as WPARAM;
unsafe { SendMessageW(self.handle, LB_SETSEL, selected, index as LPARAM); }
}
pub fn set_range_selected(&self, index_min: usize, index_max: usize, selected: bool) {
use low::defs::LB_SELITEMRANGEEX;
use winapi::LPARAM;
let (min, max) = if selected {
(index_min as WPARAM, index_max as LPARAM)
} else {
(index_max as WPARAM, index_min as LPARAM)
};
unsafe{ SendMessageW(self.handle, LB_SELITEMRANGEEX, min, max); }
}
pub fn len_selected(&self) -> usize {
use low::defs::LB_GETSELCOUNT;
let index = unsafe{ SendMessageW(self.handle, LB_GETSELCOUNT, 0, 0) };
if index == -1 {
1
} else {
index as usize
}
}
pub fn clear(&mut self) {
use low::defs::LB_RESETCONTENT;
unsafe{ SendMessageW(self.handle, LB_RESETCONTENT, 0, 0) };
self.collection.clear();
}
pub fn find_string<'a>(&self, text: &'a str, full_match: bool) -> Option<usize> {
use low::defs::{LB_FINDSTRING, LB_FINDSTRINGEXACT};
let text = to_utf16(text);
let msg = if full_match { LB_FINDSTRINGEXACT } else { LB_FINDSTRING };
let index = unsafe{ SendMessageW(self.handle, msg, -1isize as WPARAM, mem::transmute(text.as_ptr()) ) };
if index == -1 {
None
} else {
Some(index as usize)
}
}
pub fn get_string(&self, index: usize) -> Option<String> {
use low::defs::{LB_GETTEXT, LB_GETTEXTLEN};
let length = unsafe{ SendMessageW(self.handle, LB_GETTEXTLEN, index as WPARAM, 0) };
if length == -1 { return None; }
let length = (length+1) as usize;
let mut buffer: Vec<u16> = Vec::with_capacity(length);
unsafe {
buffer.set_len(length);
let err = SendMessageW(self.handle, LB_GETTEXT, index as WPARAM, mem::transmute( buffer.as_mut_ptr() ));
if err == -1 { return None; }
}
Some( from_utf16(&buffer[..]) )
}
pub fn get_readonly(&self) -> bool {
use low::window_helper::get_window_long;
use low::defs::LBS_NOSEL;
use winapi::GWL_STYLE;
let style = get_window_long(self.handle, GWL_STYLE) as u32;
(style & LBS_NOSEL) == LBS_NOSEL
}
pub fn set_readonly(&self, readonly: bool) {
use low::window_helper::{set_window_long, get_window_long};
use low::defs::LBS_NOSEL;
use winapi::GWL_STYLE;
let old_style = get_window_long(self.handle, GWL_STYLE) as usize;
if readonly {
set_window_long(self.handle, GWL_STYLE, old_style|(LBS_NOSEL as usize));
} else {
set_window_long(self.handle, GWL_STYLE, old_style&(!LBS_NOSEL as usize) );
}
}
pub fn get_multi_select(&self) -> bool {
use low::window_helper::get_window_long;
use low::defs::LBS_MULTIPLESEL;
use winapi::GWL_STYLE;
let style = get_window_long(self.handle, GWL_STYLE) as u32;
(style & LBS_MULTIPLESEL) == LBS_MULTIPLESEL
}
pub fn set_multi_select(&self, multi: bool) {
use low::window_helper::{set_window_long, get_window_long};
use low::defs::LBS_MULTIPLESEL;
use winapi::GWL_STYLE;
let old_style = get_window_long(self.handle, GWL_STYLE) as usize;
if multi {
set_window_long(self.handle, GWL_STYLE, old_style|(LBS_MULTIPLESEL as usize));
} else {
set_window_long(self.handle, GWL_STYLE, old_style&(!LBS_MULTIPLESEL as usize) );
}
}
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, true); } }
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<D: Clone+Display> Control for ListBox<D> {
fn handle(&self) -> AnyHandle {
AnyHandle::HWND(self.handle)
}
fn control_type(&self) -> ControlType {
ControlType::ListBox
}
fn free(&mut self) {
use user32::DestroyWindow;
unsafe{ DestroyWindow(self.handle) };
}
}