1
0

IconStore.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #include "IconStore.h"
  2. #include "resource1.h"
  3. #include "..\..\General\gen_ml/ml.h"
  4. extern winampMediaLibraryPlugin plugin;
  5. IconStore icon_store;
  6. static int IconStore_RegisterBitmap(HMLIMGLST imageList, int index, HINSTANCE module, const wchar_t *iconName)
  7. {
  8. MLIMAGESOURCE imageSource;
  9. MLIMAGELISTITEM listItem;
  10. imageSource.cbSize = sizeof(imageSource);
  11. imageSource.hInst = module;
  12. imageSource.lpszName = iconName;
  13. imageSource.type = SRC_TYPE_PNG;
  14. imageSource.bpp = 32;
  15. imageSource.flags = ISF_FORCE_BPP;
  16. if (NULL == module && FALSE == IS_INTRESOURCE(iconName))
  17. imageSource.flags |= ISF_LOADFROMFILE;
  18. listItem.cbSize = sizeof(listItem);
  19. listItem.hmlil = imageList;
  20. listItem.filterUID = MLIF_FILTER3_UID;
  21. listItem.pmlImgSource = &imageSource;
  22. listItem.mlilIndex = index;
  23. if (listItem.mlilIndex >= 0)
  24. {
  25. if (FALSE == MLImageList_Replace(plugin.hwndLibraryParent, &listItem))
  26. return -1;
  27. return listItem.mlilIndex;
  28. }
  29. return MLImageList_Add(plugin.hwndLibraryParent, &listItem);
  30. }
  31. static BOOL IconStore_IsResourceNameEqual(const wchar_t *name1, const wchar_t *name2)
  32. {
  33. if (FALSE != IS_INTRESOURCE(name1) || FALSE != IS_INTRESOURCE(name2))
  34. {
  35. return (name1 == name2);
  36. }
  37. return (CSTR_EQUAL == CompareString(MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT),
  38. NORM_IGNORECASE, name1, -1, name2, -1));
  39. }
  40. static void IconStore_FreeResourceName(wchar_t *name)
  41. {
  42. if (FALSE == IS_INTRESOURCE(name))
  43. free(name);
  44. }
  45. IconStore::IconStore()
  46. {
  47. playlist_icon_index = -1;
  48. video_icon_index = -1;
  49. device_icon_index = -1;
  50. for (int i = 0; i < 4; i++)
  51. {
  52. active_queue_icon[i] = queue_icon_index[i] = 0;
  53. }
  54. }
  55. IconStore::~IconStore()
  56. {
  57. size_t index;
  58. index = iconList.size();
  59. while(index--)
  60. {
  61. IconStore_FreeResourceName(iconList[index].name);
  62. }
  63. }
  64. int IconStore::GetPlaylistIcon()
  65. {
  66. if (-1 == playlist_icon_index)
  67. {
  68. playlist_icon_index = IconStore_RegisterBitmap(MLNavCtrl_GetImageList(plugin.hwndLibraryParent), -1,
  69. plugin.hDllInstance, MAKEINTRESOURCE(IDR_PLAYLIST_ICON));
  70. }
  71. return playlist_icon_index;
  72. }
  73. int IconStore::GetVideoIcon()
  74. {
  75. if (-1 == video_icon_index)
  76. {
  77. MLIMAGELISTTAG imageTag;
  78. imageTag.hmlil = MLNavCtrl_GetImageList(plugin.hwndLibraryParent);
  79. imageTag.nTag = 102; // video node image tag registered by ml_local
  80. if (FALSE != MLImageList_GetIndexFromTag(plugin.hwndLibraryParent, &imageTag))
  81. video_icon_index = imageTag.mlilIndex;
  82. else
  83. video_icon_index = IconStore_RegisterBitmap(imageTag.hmlil, -1,
  84. plugin.hDllInstance, MAKEINTRESOURCE(IDR_VIDEO_ICON));
  85. }
  86. return video_icon_index;
  87. }
  88. int IconStore::GetDeviceIcon()
  89. {
  90. if (-1 == device_icon_index)
  91. {
  92. HMLIMGLST imageList;
  93. imageList = MLNavCtrl_GetImageList(plugin.hwndLibraryParent);
  94. if (NULL != imageList)
  95. {
  96. device_icon_index = IconStore_RegisterBitmap(imageList, -1,
  97. plugin.hDllInstance, MAKEINTRESOURCE(IDR_DEVICE_ICON));
  98. }
  99. }
  100. return device_icon_index;
  101. }
  102. int IconStore::GetQueueIcon(int iconIndex)
  103. {
  104. if (!queue_icon_index[iconIndex])
  105. {
  106. HMLIMGLST imageList;
  107. imageList = MLNavCtrl_GetImageList(plugin.hwndLibraryParent);
  108. if (NULL != imageList)
  109. {
  110. queue_icon_index[iconIndex] = IconStore_RegisterBitmap(imageList, -1,
  111. plugin.hDllInstance, MAKEINTRESOURCE(IDB_XFER_QUEUE_16 + iconIndex));
  112. }
  113. }
  114. return queue_icon_index[iconIndex];
  115. }
  116. void IconStore::ReleaseResourceIcon(int iconIndex)
  117. {
  118. if (-1 == iconIndex)
  119. return;
  120. size_t index = iconList.size();
  121. while(index--)
  122. {
  123. ResourceIcon *icon = &iconList[index];
  124. if (icon->index == iconIndex)
  125. {
  126. if (0 != icon->ref)
  127. {
  128. icon->ref--;
  129. if (0 == icon->ref)
  130. {
  131. IconStore_FreeResourceName(icon->name);
  132. icon->name = NULL;
  133. icon->module = NULL;
  134. }
  135. }
  136. break;
  137. }
  138. }
  139. }
  140. int IconStore::GetResourceIcon(HINSTANCE module, const wchar_t *name)
  141. {
  142. ResourceIcon *icon;
  143. size_t index;
  144. if (module == plugin.hDllInstance &&
  145. FALSE != IS_INTRESOURCE(name) &&
  146. name == MAKEINTRESOURCE(IDR_DEVICE_ICON))
  147. {
  148. return GetDeviceIcon();
  149. }
  150. index = iconList.size();
  151. while(index--)
  152. {
  153. icon = &iconList[index];
  154. if (icon->module == module &&
  155. FALSE != IconStore_IsResourceNameEqual(icon->name, name))
  156. {
  157. icon->ref++;
  158. return icon->index;
  159. }
  160. }
  161. return RegisterResourceIcon(module, name);
  162. }
  163. int IconStore::RegisterResourceIcon(HINSTANCE module, const wchar_t *name)
  164. {
  165. ResourceIcon *icon, iconData;
  166. HMLIMGLST imageList;
  167. size_t index;
  168. imageList = MLNavCtrl_GetImageList(plugin.hwndLibraryParent);
  169. if (NULL == imageList)
  170. return -1;
  171. index = iconList.size();
  172. while(index--)
  173. {
  174. icon = &iconList[index];
  175. if (0 == icon->ref)
  176. {
  177. break;
  178. }
  179. }
  180. if ((size_t)-1 == index)
  181. {
  182. icon = &iconData;
  183. icon->index = -1;
  184. }
  185. if (FALSE != IS_INTRESOURCE(name))
  186. icon->name = (wchar_t*)name;
  187. else
  188. {
  189. icon->name = _wcsdup(name);
  190. if (NULL == icon->name)
  191. return -1;
  192. }
  193. icon->ref = 1;
  194. icon->module = module;
  195. icon->index = IconStore_RegisterBitmap(imageList, icon->index, icon->module, icon->name);
  196. if (-1 != icon->index && &iconData == icon)
  197. iconList.push_back(iconData);
  198. return icon->index;
  199. }