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";

/**
An image list is a collection of images of the same size, each of which can be referred to by its index.
Image lists are used in controls such as tabs container and tree view in order to add icon next to the items.

There are two kinds of image list in Winapi: masked. This is a wrapper over the masked type.

Image list and the method that use them in controls are behind the "image-list" feature. 

**Builder parameters:**
  * `size`:    The size size of the images in the image list. Default `(32, 32)`
  * `initial`: The initial size (in images) of the image list. Default `5`
  * `grow`:    The number of images by which the image list can grow when the system needs to make room for new images. Default `5`

```rust
use native_windows_gui as nwg;
fn build_image_list(list: &mut nwg::ImageList) {
    nwg::ImageList::builder()
        .size((64, 64))
        .initial(10)
        .grow(1)
        .build(list);
}
```

*/
pub struct ImageList {
    pub handle: HIMAGELIST,
    pub owned: bool
}

impl ImageList {

    pub fn builder() -> ImageListBuilder {
        ImageListBuilder {
            size: (32, 32),
            initial: 5,
            grow: 5
        }
    }

    /// Returns the size of the images in the image list
    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
    }

    /// Sets the size of the image list. This clears all current image data.
    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); }
    }

    /// Returns the number of images in the image list
    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 }
    }

    /// Adds a new bitmap to the image list. Returns the index to the image. Panics if the bitmap was not initialized
    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) }
    }

    /**
        Adds a bitmap directly from a filename. The image is resized to the image list size.
        Returns the index to the image or an error if the image could not be loaded
    */
    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)) }
    }

    /// Adds a new icon to the image list. Returns the index to the image. Panics if the icon was not initialized
    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"); }

        // Extract the bitmap from the icon
        // Can't use `ImageList_AddIcon` because it doesn't always guess the mask
        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
        }
    }

    /**
        Adds a icon directly from a filename. The image is resized to the image list size.
        Returns the index to the image or an error if the image could not be loaded
    */
    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))
    }

    /**
        Removes the image at the specified index

        When an image is removed, the indexes of the remaining images are adjusted so that the image indexes
        always range from zero to one less than the number of images in the image list. For example, if you
        remove the image at index 0, then image 1 becomes image 0, image 2 becomes image 1, and so on.
    */
    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); }
    }

    /// Replaces an image in the image list. Panics if the bitmap was not initialized
    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()); }
    }

    /// Replaces an image in the image list by an icon. Panics if the icon was not initialized
    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(())
    }

}