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
use winapi::um::commctrl::{HIMAGELIST, ImageList_AddMasked};
use winapi::shared::windef::{HICON, HBITMAP};
use crate::{Bitmap, Icon, NwgError};
use std::ptr;
const NOT_BOUND: &'static str = "ImageList is not yet bound to a winapi object";
pub struct ImageList {
pub handle: HIMAGELIST,
pub owned: bool
}
impl ImageList {
pub fn builder() -> ImageListBuilder {
ImageListBuilder {
size: (32, 32),
initial: 5,
grow: 5
}
}
pub fn size(&self) -> (i32, i32) {
use winapi::um::commctrl::ImageList_GetIconSize;
if self.handle.is_null() { panic!("{}", NOT_BOUND); }
let mut size = (0, 0);
unsafe { ImageList_GetIconSize(self.handle, &mut size.0, &mut size.1); }
size
}
pub fn set_size(&self, size: (i32, i32)) {
use winapi::um::commctrl::ImageList_SetIconSize;
if self.handle.is_null() { panic!("{}", NOT_BOUND); }
let (w, h) = size;
unsafe { ImageList_SetIconSize(self.handle, w, h); }
}
pub fn len(&self) -> u32 {
use winapi::um::commctrl::ImageList_GetImageCount;
if self.handle.is_null() { panic!("{}", NOT_BOUND); }
unsafe { ImageList_GetImageCount(self.handle) as u32 }
}
pub fn add_bitmap(&self, bitmap: &Bitmap) -> i32 {
if self.handle.is_null() { panic!("{}", NOT_BOUND); }
if bitmap.handle.is_null() { panic!("Bitmap was not initialized"); }
unsafe { ImageList_AddMasked(self.handle, bitmap.handle as HBITMAP, 0) }
}
pub fn add_bitmap_from_filename(&self, filename: &str) -> Result<i32, NwgError> {
if self.handle.is_null() { panic!("{}", NOT_BOUND); }
let (w, h) = self.size();
let mut bitmap = Bitmap::default();
Bitmap::builder()
.source_file(Some(filename))
.size(Some((w as u32, h as u32)))
.strict(true)
.build(&mut bitmap)?;
unsafe { Ok(ImageList_AddMasked(self.handle, bitmap.handle as HBITMAP, 0)) }
}
pub fn add_icon(&self, icon: &Icon) -> i32 {
use winapi::um::winuser::{GetIconInfo, ICONINFO};
use winapi::um::wingdi::DeleteObject;
if self.handle.is_null() { panic!("{}", NOT_BOUND); }
if icon.handle.is_null() { panic!("Icon was not initialized"); }
unsafe {
let mut info: ICONINFO = ::std::mem::zeroed();
GetIconInfo(icon.handle as _, &mut info);
let i = ImageList_AddMasked(self.handle, info.hbmColor, 0);
DeleteObject(info.hbmMask as _);
DeleteObject(info.hbmColor as _);
i
}
}
pub fn add_icon_from_filename(&self, filename: &str) -> Result<i32, NwgError> {
if self.handle.is_null() { panic!("{}", NOT_BOUND); }
let (w, h) = self.size();
let mut icon = Icon::default();
Icon::builder()
.source_file(Some(filename))
.size(Some((w as u32, h as u32)))
.strict(true)
.build(&mut icon)?;
Ok(self.add_icon(&icon))
}
pub fn remove(&self, index: i32) {
use winapi::um::commctrl::ImageList_Remove;
if self.handle.is_null() { panic!("{}", NOT_BOUND); }
unsafe { ImageList_Remove(self.handle, index); }
}
pub fn replace_bitmap(&self, index: i32, bitmap: &Bitmap) {
use winapi::um::commctrl::ImageList_Replace;
if self.handle.is_null() { panic!("{}", NOT_BOUND); }
if bitmap.handle.is_null() { panic!("Bitmap was not initialized"); }
unsafe { ImageList_Replace(self.handle, index, bitmap.handle as HBITMAP, ptr::null_mut()); }
}
pub fn replace_icon(&self, index: i32, icon: &Icon) {
use winapi::um::commctrl::ImageList_ReplaceIcon;
if self.handle.is_null() { panic!("{}", NOT_BOUND); }
if icon.handle.is_null() { panic!("Icon was not initialized"); }
unsafe { ImageList_ReplaceIcon(self.handle, index, icon.handle as HICON); }
}
}
impl Drop for ImageList {
fn drop(&mut self) {
use winapi::um::commctrl::ImageList_Destroy;
unsafe {
if self.owned && !self.handle.is_null() {
ImageList_Destroy(self.handle);
}
}
}
}
impl Default for ImageList {
fn default() -> ImageList {
ImageList {
handle: ptr::null_mut(),
owned: false
}
}
}
impl PartialEq for ImageList {
fn eq(&self, other: &Self) -> bool {
self.handle == other.handle
}
}
pub struct ImageListBuilder {
size: (i32, i32),
initial: i32,
grow: i32,
}
impl ImageListBuilder {
pub fn size(mut self, size: (i32, i32)) -> ImageListBuilder {
self.size = size;
self
}
pub fn initial(mut self, initial: i32) -> ImageListBuilder {
self.initial = initial;
self
}
pub fn grow(mut self, grow: i32) -> ImageListBuilder {
self.grow = grow;
self
}
pub fn build(self, list: &mut ImageList) -> Result<(), NwgError> {
use winapi::um::commctrl::{ImageList_Create, ILC_COLOR32, ILC_MASK};
unsafe {
let (w, h) = self.size;
let handle = ImageList_Create(w, h, ILC_COLOR32 | ILC_MASK, self.initial, self.grow);
if handle.is_null() {
return Err(NwgError::resource_create("Failed to create image list"));
}
list.handle = handle;
list.owned = true;
}
Ok(())
}
}