local_menu.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #include "main.h"
  2. #include "./local_menu.h"
  3. #include "./resource.h"
  4. #include "..\..\General\gen_ml/menu.h"
  5. #include "../nu/menushortcuts.h"
  6. #define RATING_MARKER MAKELONG(MAKEWORD('R','A'),MAKEWORD('T','E'))
  7. #define RATING_MINSPACECX 16
  8. #if 0
  9. static BOOL Menu_IsRatingStar(HMENU hMenu, INT itemId, INT *valueOut)
  10. {
  11. WCHAR szBuffer[8] = {0};
  12. INT cchBuffer = GetMenuStringW(hMenu, itemId, szBuffer, ARRAYSIZE(szBuffer), MF_BYCOMMAND);
  13. if (cchBuffer < 1 || cchBuffer > 5)
  14. return FALSE;
  15. for (INT i = 1; i < cchBuffer; i++)
  16. {
  17. if (szBuffer[i -1] != szBuffer[i])
  18. return FALSE;
  19. }
  20. if (NULL != valueOut)
  21. *valueOut = cchBuffer;
  22. return TRUE;
  23. }
  24. static BOOL Menu_MeasureRating(HMENU hMenu, HDC hdc, MEASUREITEMSTRUCT *pmis)
  25. {
  26. if (NULL == hdc || !Menu_IsRatingStar(hMenu, pmis->itemID, NULL))
  27. return FALSE;
  28. RECT rect;
  29. if (!MLRating_CalcRect(plugin.hwndLibraryParent, NULL, 5, &rect))
  30. return FALSE;
  31. pmis->itemHeight = rect.bottom - rect.top + 6;
  32. TEXTMETRIC tm = {0};
  33. if (GetTextMetrics(hdc, &tm) &&
  34. (UINT)(tm.tmHeight + 2) > pmis->itemHeight)
  35. {
  36. pmis->itemHeight = tm.tmHeight + 2;
  37. }
  38. INT spaceCX = (pmis->itemHeight > RATING_MINSPACECX) ? pmis->itemHeight : RATING_MINSPACECX;
  39. pmis->itemWidth = rect.right - rect.left + (2 * spaceCX) - (GetSystemMetrics(SM_CXMENUCHECK) - 1);
  40. return TRUE;
  41. }
  42. static BOOL Menu_DrawRating(HMENU hMenu, HDC hdc, DRAWITEMSTRUCT *pdis)
  43. {
  44. INT ratingValue;
  45. if (NULL == hdc || !Menu_IsRatingStar(hMenu, pdis->itemID, &ratingValue))
  46. return FALSE;
  47. INT spaceCX = ((pdis->rcItem.bottom - pdis->rcItem.top) > RATING_MINSPACECX) ?
  48. (pdis->rcItem.bottom - pdis->rcItem.top) :
  49. RATING_MINSPACECX;
  50. RATINGDRAWPARAMS rdp = {0};
  51. rdp.cbSize = sizeof(RATINGDRAWPARAMS);
  52. rdp.hdcDst = hdc;
  53. rdp.rc = pdis->rcItem;
  54. rdp.rc.left += spaceCX + WASABI_API_APP->getScaleX(13);
  55. rdp.value = ratingValue;
  56. rdp.maxValue = 5;
  57. UINT menuState = GetMenuState(hMenu, pdis->itemID, MF_BYCOMMAND);
  58. rdp.trackingValue = (0 != (ODS_SELECTED & pdis->itemState) &&
  59. 0 == ((MF_DISABLED | MF_GRAYED) & menuState)) ?
  60. rdp.value :
  61. 0;
  62. rdp.fStyle = RDS_LEFT | RDS_VCENTER | RDS_HOT;
  63. rdp.hMLIL = NULL;
  64. rdp.index = 0;
  65. return MLRating_Draw(plugin.hwndLibraryParent, &rdp);
  66. }
  67. static BOOL CALLBACK Menu_CustomDrawProc(INT action, HMENU hMenu, HDC hdc, LPARAM param, ULONG_PTR user)
  68. {
  69. switch(action)
  70. {
  71. case MLMENU_ACTION_MEASUREITEM:
  72. if (hMenu == (HMENU)user)
  73. return Menu_MeasureRating(hMenu, hdc, (MEASUREITEMSTRUCT*)param);
  74. break;
  75. case MLMENU_ACTION_DRAWITEM:
  76. if (hMenu == (HMENU)user)
  77. return MLMENU_WANT_DRAWPART;
  78. break;
  79. case MLMENU_ACTION_DRAWBACK:
  80. break;
  81. case MLMENU_ACTION_DRAWICON:
  82. break;
  83. case MLMENU_ACTION_DRAWTEXT:
  84. if (hMenu == (HMENU)user)
  85. return Menu_DrawRating(hMenu, hdc, (DRAWITEMSTRUCT*)param);
  86. break;
  87. }
  88. return FALSE;
  89. }
  90. #endif
  91. static HMENU Menu_FindRatingMenuRecur(HMENU hMenu, MENUINFO *pmi, MENUITEMINFO *pmii)
  92. {
  93. if (GetMenuInfo(hMenu, pmi) && RATING_MARKER == pmi->dwMenuData)
  94. return hMenu;
  95. INT count = GetMenuItemCount(hMenu);
  96. for(INT i = 0; i < count; i++)
  97. {
  98. if (GetMenuItemInfo(hMenu, i, TRUE, pmii) && NULL != pmii->hSubMenu)
  99. {
  100. HMENU hRating = Menu_FindRatingMenuRecur(pmii->hSubMenu, pmi, pmii);
  101. if (NULL != hRating)
  102. return hRating;
  103. }
  104. }
  105. return NULL;
  106. }
  107. HMENU Menu_FindRatingMenu(HMENU hMenu)
  108. {
  109. if (NULL == hMenu)
  110. return NULL;
  111. MENUITEMINFO mii = {0};
  112. mii.cbSize = sizeof(MENUITEMINFO);
  113. mii.fMask = MIIM_SUBMENU;
  114. MENUINFO mi;
  115. mi.cbSize = sizeof(MENUINFO);
  116. mi.fMask = MIM_MENUDATA;
  117. return Menu_FindRatingMenuRecur(hMenu, &mi, &mii);
  118. }
  119. BOOL Menu_SetRatingValue(HMENU ratingMenu, INT ratingValue)
  120. {
  121. if (NULL == ratingMenu) return FALSE;
  122. INT ratingList[] = { ID_RATE_0, ID_RATE_1, ID_RATE_2,
  123. ID_RATE_3, ID_RATE_4, ID_RATE_5};
  124. /// set rating marker
  125. MENUINFO mi = {0};
  126. mi.cbSize = sizeof(MENUINFO);
  127. mi.fMask = MIM_MENUDATA;
  128. mi.dwMenuData = RATING_MARKER;
  129. if (!SetMenuInfo(ratingMenu, &mi))
  130. return FALSE;
  131. MENUITEMINFO mii = {0};
  132. mii.cbSize = sizeof(MENUITEMINFO);
  133. UINT type, state;
  134. for (INT i = 0; i < ARRAYSIZE(ratingList); i++)
  135. {
  136. mii.fMask = MIIM_STATE | MIIM_FTYPE;
  137. if (GetMenuItemInfo(ratingMenu, ratingList[i], FALSE, &mii))
  138. {
  139. if (ratingValue == i)
  140. {
  141. type = mii.fType | MFT_RADIOCHECK;
  142. state = mii.fState | MFS_CHECKED;
  143. }
  144. else
  145. {
  146. type = mii.fType & ~MFT_RADIOCHECK;
  147. state = mii.fState & ~MFS_CHECKED;
  148. }
  149. mii.fMask = 0;
  150. if (type != mii.fType)
  151. {
  152. mii.fType = type;
  153. mii.fMask |= MIIM_FTYPE;
  154. }
  155. if (state != mii.fState)
  156. {
  157. mii.fState = state;
  158. mii.fMask |= MIIM_STATE;
  159. }
  160. if (0 != mii.fMask)
  161. SetMenuItemInfo(ratingMenu, ratingList[i], FALSE, &mii);
  162. }
  163. }
  164. return TRUE;
  165. }
  166. void SyncMenuWithAccelerators(HWND hwndDlg, HMENU menu)
  167. {
  168. HACCEL szAccel[24] = {0};
  169. INT c = WASABI_API_APP->app_getAccelerators(hwndDlg, szAccel, sizeof(szAccel)/sizeof(szAccel[0]), FALSE);
  170. AppendMenuShortcuts(menu, szAccel, c, MSF_REPLACE);
  171. }
  172. void SwapPlayEnqueueInMenu(HMENU listMenu)
  173. {
  174. int playPos=-1, enqueuePos=-1;
  175. MENUITEMINFOW playItem={sizeof(MENUITEMINFOW), 0,}, enqueueItem={sizeof(MENUITEMINFOW), 0,};
  176. int numItems = GetMenuItemCount(listMenu);
  177. int alt = 0;
  178. for (int i=0;i<numItems;i++)
  179. {
  180. UINT id = GetMenuItemID(listMenu, i);
  181. if (id == ID_MEDIAWND_PLAYSELECTEDFILES || id == ID_AUDIOWND_PLAYSELECTION || id == ID_QUERYWND_PLAYQUERY)
  182. {
  183. playItem.fMask = MIIM_ID;
  184. playPos = i;
  185. GetMenuItemInfoW(listMenu, i, TRUE, &playItem);
  186. if (id == ID_AUDIOWND_PLAYSELECTION) alt = 1;
  187. else if (id == ID_QUERYWND_PLAYQUERY) alt = 2;
  188. }
  189. else if (id == ID_MEDIAWND_ENQUEUESELECTEDFILES || id == ID_AUDIOWND_ENQUEUESELECTION || id == ID_QUERYWND_ENQUEUEQUERY)
  190. {
  191. enqueueItem.fMask = MIIM_ID;
  192. enqueuePos = i;
  193. GetMenuItemInfoW(listMenu, i, TRUE, &enqueueItem);
  194. if (id == ID_AUDIOWND_ENQUEUESELECTION) alt = 1;
  195. else if (id == ID_QUERYWND_ENQUEUEQUERY) alt = 2;
  196. }
  197. }
  198. playItem.wID = (alt == 1 ? ID_MEDIAWND_ENQUEUESELECTEDFILES : (alt == 2 ? ID_QUERYWND_ENQUEUEQUERY : ID_AUDIOWND_ENQUEUESELECTION));
  199. enqueueItem.wID = (alt == 1 ? ID_MEDIAWND_PLAYSELECTEDFILES : (alt == 2 ? ID_QUERYWND_PLAYQUERY : ID_AUDIOWND_PLAYSELECTION));
  200. SetMenuItemInfoW(listMenu, playPos, TRUE, &playItem);
  201. SetMenuItemInfoW(listMenu, enqueuePos, TRUE, &enqueueItem);
  202. }
  203. void UpdateMenuItems(HWND hwndDlg, HMENU menu, UINT accel_id)
  204. {
  205. bool swapPlayEnqueue=false;
  206. if (g_config->ReadInt(L"enqueuedef", 0) == 1)
  207. {
  208. SwapPlayEnqueueInMenu(menu);
  209. swapPlayEnqueue=true;
  210. }
  211. if(!IsWindow(hwndDlg))
  212. {
  213. HACCEL accel = WASABI_API_LOADACCELERATORSW(accel_id);
  214. int size = CopyAcceleratorTable(accel,0,0);
  215. AppendMenuShortcuts(menu, &accel, size, MSF_REPLACE);
  216. }
  217. else
  218. {
  219. SyncMenuWithAccelerators(hwndDlg, menu);
  220. }
  221. if (swapPlayEnqueue) SwapPlayEnqueueInMenu(menu);
  222. }
  223. INT DoTrackPopup(HMENU hMenu, UINT fuFlags, INT x, INT y, HWND hwnd, LPTPMPARAMS lptpm)
  224. {
  225. if (NULL == hMenu)
  226. return NULL;
  227. return Menu_TrackPopupParam(plugin.hwndLibraryParent, hMenu, fuFlags, x, y,
  228. hwnd, lptpm, (ULONG_PTR)Menu_FindRatingMenu(hMenu));
  229. }