aboutsummaryrefslogtreecommitdiff
path: root/Src/Plugins/Library/ml_pmp/IconStore.cpp
blob: c5ef3eea1e0f2ca48a06b443a79390b2355ad290 (plain) (blame)
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
#include "IconStore.h"
#include "resource1.h"
#include "..\..\General\gen_ml/ml.h"

extern winampMediaLibraryPlugin plugin;
IconStore icon_store;

static int IconStore_RegisterBitmap(HMLIMGLST imageList, int index, HINSTANCE module, const wchar_t *iconName)
{
	MLIMAGESOURCE imageSource;
	MLIMAGELISTITEM listItem;

	imageSource.cbSize = sizeof(imageSource);
	imageSource.hInst = module;
	imageSource.lpszName = iconName;
	imageSource.type = SRC_TYPE_PNG;
	imageSource.bpp = 32;
	imageSource.flags = ISF_FORCE_BPP;

	if (NULL == module && FALSE == IS_INTRESOURCE(iconName))
		imageSource.flags |= ISF_LOADFROMFILE;

	listItem.cbSize = sizeof(listItem);
	listItem.hmlil = imageList;
	listItem.filterUID = MLIF_FILTER3_UID;
	listItem.pmlImgSource = &imageSource;
	listItem.mlilIndex = index;

	if (listItem.mlilIndex >= 0)
	{
		if (FALSE == MLImageList_Replace(plugin.hwndLibraryParent, &listItem))
			return -1;
		return listItem.mlilIndex;
	}

	return MLImageList_Add(plugin.hwndLibraryParent, &listItem);
}

static BOOL IconStore_IsResourceNameEqual(const wchar_t *name1, const wchar_t *name2)
{
	if (FALSE != IS_INTRESOURCE(name1) || FALSE != IS_INTRESOURCE(name2))
	{
		return (name1 == name2);
	}

	return (CSTR_EQUAL == CompareString(MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT), 
										NORM_IGNORECASE, name1, -1, name2, -1));
}

static void IconStore_FreeResourceName(wchar_t *name)
{
	if (FALSE == IS_INTRESOURCE(name))
		free(name);
}

IconStore::IconStore()
{
	playlist_icon_index = -1;
	video_icon_index = -1;
	device_icon_index = -1;
	for (int i = 0; i < 4; i++)
	{
		active_queue_icon[i] = queue_icon_index[i] = 0;
	}
}

IconStore::~IconStore()
{
	size_t index;
	
	index = iconList.size();
	while(index--)
	{
		IconStore_FreeResourceName(iconList[index].name);
	}
}

int IconStore::GetPlaylistIcon()
{
	if (-1 == playlist_icon_index)
	{
		playlist_icon_index = IconStore_RegisterBitmap(MLNavCtrl_GetImageList(plugin.hwndLibraryParent), -1,
													   plugin.hDllInstance, MAKEINTRESOURCE(IDR_PLAYLIST_ICON));
	}

	return playlist_icon_index;
}

int IconStore::GetVideoIcon()
{
	if (-1 == video_icon_index)
	{
		MLIMAGELISTTAG imageTag;

		imageTag.hmlil =  MLNavCtrl_GetImageList(plugin.hwndLibraryParent);
		imageTag.nTag = 102; // video node image tag registered by ml_local

		if (FALSE != MLImageList_GetIndexFromTag(plugin.hwndLibraryParent, &imageTag))
			video_icon_index = imageTag.mlilIndex;
		else
			video_icon_index = IconStore_RegisterBitmap(imageTag.hmlil, -1, 
									plugin.hDllInstance, MAKEINTRESOURCE(IDR_VIDEO_ICON));
	}

	return video_icon_index;
}

int IconStore::GetDeviceIcon()
{
	if (-1 == device_icon_index)
	{
		HMLIMGLST imageList;

		imageList  = MLNavCtrl_GetImageList(plugin.hwndLibraryParent);
		if (NULL != imageList)
		{
			device_icon_index = IconStore_RegisterBitmap(imageList, -1,
									plugin.hDllInstance, MAKEINTRESOURCE(IDR_DEVICE_ICON));
		}
	}

	return device_icon_index;
}

int IconStore::GetQueueIcon(int iconIndex)
{
	if (!queue_icon_index[iconIndex])
	{
		HMLIMGLST imageList;

		imageList = MLNavCtrl_GetImageList(plugin.hwndLibraryParent);
		if (NULL != imageList)
		{
			queue_icon_index[iconIndex] = IconStore_RegisterBitmap(imageList, -1,
												plugin.hDllInstance, MAKEINTRESOURCE(IDB_XFER_QUEUE_16 + iconIndex));
		}
	}

	return queue_icon_index[iconIndex];
}

void IconStore::ReleaseResourceIcon(int iconIndex)
{
	if (-1 == iconIndex)
		return;

	size_t index = iconList.size();
	while(index--)
	{
		ResourceIcon *icon = &iconList[index];
		if (icon->index == iconIndex)
		{
			if (0 != icon->ref)
			{
				icon->ref--;
				if (0 == icon->ref)
				{
					IconStore_FreeResourceName(icon->name);
					icon->name = NULL;
					icon->module = NULL;
				}
			}
			break;
		}
	}
}

int IconStore::GetResourceIcon(HINSTANCE module, const wchar_t *name)
{
	ResourceIcon *icon;
	size_t index;

	if (module == plugin.hDllInstance &&
		FALSE != IS_INTRESOURCE(name) &&
		name == MAKEINTRESOURCE(IDR_DEVICE_ICON))
	{
		return GetDeviceIcon();
	}

	index = iconList.size();
	while(index--)
	{
		icon = &iconList[index];
		if (icon->module == module &&
			FALSE != IconStore_IsResourceNameEqual(icon->name, name))
		{
			icon->ref++;
			return icon->index;
		}
	}

	return RegisterResourceIcon(module, name);
}

int IconStore::RegisterResourceIcon(HINSTANCE module, const wchar_t *name)
{
	ResourceIcon *icon, iconData;
	HMLIMGLST imageList;
	size_t index;

	imageList = MLNavCtrl_GetImageList(plugin.hwndLibraryParent);
	if (NULL == imageList)
		return -1;

	index = iconList.size();
	while(index--)
	{
		icon = &iconList[index];
		if (0 == icon->ref)
		{
			break;
		}
	}
	if ((size_t)-1 == index)
	{
		icon = &iconData;
		icon->index = -1;
	}

	if (FALSE != IS_INTRESOURCE(name))
		icon->name = (wchar_t*)name;
	else
	{
		icon->name = _wcsdup(name);
		if (NULL == icon->name)
			return -1;
	}

	icon->ref = 1;
	icon->module = module;
	icon->index = IconStore_RegisterBitmap(imageList, icon->index, icon->module, icon->name);

	if (-1 != icon->index && &iconData == icon)
		iconList.push_back(iconData);

	return icon->index;
}