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
use winapi::um::winnt::HANDLE;
use winapi::um::winuser::IMAGE_ICON;
use crate::win32::resources_helper as rh;
use crate::{OemImage, OemIcon, NwgError};
use std::ptr;
#[cfg(feature = "embed-resource")]
use super::EmbedResource;
#[allow(unused)]
pub struct Icon {
pub handle: HANDLE,
pub(crate) owned: bool
}
impl Icon {
pub fn builder<'a>() -> IconBuilder<'a> {
IconBuilder {
source_text: None,
source_bin: None,
source_system: None,
#[cfg(feature = "embed-resource")]
source_embed: None,
#[cfg(feature = "embed-resource")]
source_embed_id: 0,
#[cfg(feature = "embed-resource")]
source_embed_str: None,
size: None,
strict: false
}
}
}
pub struct IconBuilder<'a> {
source_text: Option<&'a str>,
source_bin: Option<&'a [u8]>,
source_system: Option<OemIcon>,
#[cfg(feature = "embed-resource")]
source_embed: Option<&'a EmbedResource>,
#[cfg(feature = "embed-resource")]
source_embed_id: usize,
#[cfg(feature = "embed-resource")]
source_embed_str: Option<&'a str>,
size: Option<(u32, u32)>,
strict: bool,
}
impl<'a> IconBuilder<'a> {
pub fn source_file(mut self, t: Option<&'a str>) -> IconBuilder<'a> {
self.source_text = t;
self
}
pub fn source_bin(mut self, t: Option<&'a [u8]>) -> IconBuilder<'a> {
self.source_bin = t;
self
}
pub fn source_system(mut self, t: Option<OemIcon>) -> IconBuilder<'a> {
self.source_system = t;
self
}
#[cfg(feature = "embed-resource")]
pub fn source_embed(mut self, em: Option<&'a EmbedResource>) -> IconBuilder<'a> {
self.source_embed = em;
self
}
#[cfg(feature = "embed-resource")]
pub fn source_embed_id(mut self, id: usize) -> IconBuilder<'a> {
self.source_embed_id = id;
self
}
#[cfg(feature = "embed-resource")]
pub fn source_embed_str(mut self, id: Option<&'a str>) -> IconBuilder<'a> {
self.source_embed_str = id;
self
}
pub fn size(mut self, s: Option<(u32, u32)>) -> IconBuilder<'a> {
self.size = s;
self
}
pub fn strict(mut self, s: bool) -> IconBuilder<'a> {
self.strict = s;
self
}
pub fn build(self, b: &mut Icon) -> Result<(), NwgError> {
if let Some(src) = self.source_text {
let handle = unsafe { rh::build_image(src, self.size, self.strict, IMAGE_ICON)? };
*b = Icon { handle, owned: true };
} else if let Some(src) = self.source_system {
let handle = unsafe { rh::build_oem_image(OemImage::Icon(src), self.size)? };
*b = Icon { handle, owned: true };
} else {
#[cfg(feature = "embed-resource")]
fn build_embed(builder: IconBuilder) -> Result<Icon, NwgError> {
match builder.source_embed {
Some(embed) => {
match builder.source_embed_str {
Some(src) => embed.icon_str(src)
.ok_or_else(|| NwgError::resource_create(format!("No icon in embed resource identified by {}", src))),
None => embed.icon(builder.source_embed_id)
.ok_or_else(|| NwgError::resource_create(format!("No icon in embed resource identified by {}", builder.source_embed_id)))
}
},
None => Err(NwgError::resource_create("No source provided for Icon"))
}
}
#[cfg(not(feature = "embed-resource"))]
fn build_embed(_builder: IconBuilder) -> Result<Icon, NwgError> {
Err(NwgError::resource_create("No source provided for Icon"))
}
*b = build_embed(self)?;
}
Ok(())
}
}
impl Default for Icon {
fn default() -> Icon {
Icon {
handle: ptr::null_mut(),
owned: false
}
}
}
impl PartialEq for Icon {
fn eq(&self, other: &Self) -> bool {
self.handle == other.handle
}
}