-
Notifications
You must be signed in to change notification settings - Fork 35
/
image_list.h
139 lines (119 loc) · 4.17 KB
/
image_list.h
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
/**
* Part of WinLamb - Win32 API Lambda Library
* https://github.com/rodrigocfd/winlamb
* Copyright 2017-present Rodrigo Cesar de Freitas Dias
* This library is released under the MIT License
*/
#pragma once
#include "icon.h"
namespace wl {
// Wrapper to image list object from Common Controls library.
class image_list final {
private:
HIMAGELIST _hImgList = nullptr;
public:
~image_list() {
// Image lists are destroyed automatically in some cases,
// but we'll destroy them always:
// http://www.catch22.net/tuts/sysimgq
// http://www.autohotkey.com/docs/commands/ListView.htm
this->destroy();
}
image_list() = default;
image_list(image_list&& other) noexcept : _hImgList{other._hImgList} { other._hImgList = nullptr; }
// Returns the handle to the image list.
HIMAGELIST himagelist() const noexcept {
return this->_hImgList;
}
image_list& operator=(image_list&& other) noexcept {
this->destroy();
std::swap(this->_hImgList, other._hImgList);
return *this;
}
image_list& destroy() noexcept {
if (this->_hImgList) {
ImageList_Destroy(this->_hImgList);
this->_hImgList = nullptr;
}
return *this;
}
// Creates the image list; if was already created, it's destroyed first.
image_list& create(SIZE resolution, UINT flags = ILC_COLOR32,
WORD szInitial = 1, WORD szGrow = 1)
{
this->destroy();
this->_hImgList = ImageList_Create(resolution.cx, resolution.cy, flags,
static_cast<int>(szInitial), static_cast<int>(szGrow));
if (!this->_hImgList) {
throw std::system_error(GetLastError(), std::system_category(),
"ImageList_Create failed");
}
return *this;
}
// Loads an icon into the image list.
image_list& load(HICON hIcon) {
if (!this->_hImgList) {
throw std::logic_error("Can't add icon before create image list.");
}
ImageList_AddIcon(this->_hImgList, hIcon);
return *this;
}
// Loads an icon into the image list.
image_list& load(const icon& ico) {
return this->load(ico.hicon());
}
// Loads an icon from resource into the image list.
image_list& load_from_resource(int iconId, HINSTANCE hInst = nullptr) {
icon tmpIco;
tmpIco.load_from_resource(iconId, this->resolution(), hInst);
return this->load(tmpIco);
}
// Loads an icon from resource into the image list.
image_list& load_from_resource(std::initializer_list<int> iconIds, HINSTANCE hInst = nullptr) {
for (const int iconId : iconIds) {
this->load_from_resource(iconId, hInst);
}
return *this;
}
// Loads an icon from resource into the image list.
image_list& load_from_resource(int iconId, HWND hParent) {
return this->load_from_resource(iconId,
reinterpret_cast<HINSTANCE>(GetWindowLongPtrW(hParent, GWLP_HINSTANCE)));
}
// Loads an icon from resource into the image list.
image_list& load_from_resource(std::initializer_list<int> iconIds, HWND hParent) {
return this->load_from_resource(iconIds,
reinterpret_cast<HINSTANCE>(GetWindowLongPtrW(hParent, GWLP_HINSTANCE)));
}
// Loads the icon used by Windows Explorer to represent the given file type.
image_list& load_from_shell(const wchar_t* fileExtension) {
icon::res iRes = icon::util::resolve_resolution_type(this->resolution());
if (iRes == icon::res::OTHER) {
throw std::logic_error("Trying to load icon from shell with unsupported resolution.");
}
icon tmpIco;
tmpIco.load_from_shell(fileExtension, iRes);
return this->load(tmpIco);
}
// Loads the icon used by Windows Explorer to represent the given file type.
image_list& load_from_shell(std::initializer_list<const wchar_t*> fileExtensions) {
for (const wchar_t* ext : fileExtensions) {
this->load_from_shell(ext);
}
return *this;
}
// Returns the icon resolution, in pixels, of this image list.
SIZE resolution() const noexcept {
SIZE buf{};
if (this->_hImgList) {
ImageList_GetIconSize(this->_hImgList,
reinterpret_cast<int*>(&buf.cx), reinterpret_cast<int*>(&buf.cy));
}
return buf;
}
// Returns how many images this image list has.
size_t size() const noexcept {
return this->_hImgList ? ImageList_GetImageCount(this->_hImgList) : 0;
}
};
}//namespace wl