ratingMenu.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #include "main.h"
  2. #include "./ratingMenu.h"
  3. #include "./resource.h"
  4. #include "../nu/menuHelpers.h"
  5. #include "./ifc_skinhelper.h"
  6. #include "./ifc_skinnedrating.h"
  7. #include "./ifc_menucustomizer.h"
  8. #define RATING_MARKER MAKELONG(MAKEWORD('R','A'),MAKEWORD('T','E'))
  9. static BOOL RatingMenu_IsStar(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 HMENU RatingMenu_FindMenuRecur(HMENU hMenu, MENUINFO *pmi, MENUITEMINFO *pmii)
  25. {
  26. if (GetMenuInfo(hMenu, pmi) && RATING_MARKER == pmi->dwMenuData)
  27. return hMenu;
  28. INT count = GetMenuItemCount(hMenu);
  29. for(INT i = 0; i < count; i++)
  30. {
  31. if (GetMenuItemInfo(hMenu, i, TRUE, pmii) && NULL != pmii->hSubMenu)
  32. {
  33. HMENU hRating = RatingMenu_FindMenuRecur(pmii->hSubMenu, pmi, pmii);
  34. if (NULL != hRating)
  35. return hRating;
  36. }
  37. }
  38. return NULL;
  39. }
  40. HMENU RatingMenu_FindMenu(HMENU hMenu)
  41. {
  42. if (NULL == hMenu)
  43. return NULL;
  44. MENUITEMINFO mii = {0};
  45. mii.cbSize = sizeof(MENUITEMINFO);
  46. mii.fMask = MIIM_SUBMENU;
  47. MENUINFO mi = {0};
  48. mi.cbSize = sizeof(MENUINFO);
  49. mi.fMask = MIM_MENUDATA;
  50. return RatingMenu_FindMenuRecur(hMenu, &mi, &mii);
  51. }
  52. BOOL RatingMenu_InitializeMenu(HMENU ratingMenu, INT ratingValue)
  53. {
  54. if (NULL == ratingMenu) return FALSE;
  55. MENUINFO mi = {0};
  56. mi.cbSize = sizeof(MENUINFO);
  57. mi.fMask = MIM_MENUDATA;
  58. mi.dwMenuData = RATING_MARKER;
  59. if (FALSE == SetMenuInfo(ratingMenu, &mi))
  60. return FALSE;
  61. return RatingMenu_SetValue(ratingMenu, ratingValue);
  62. }
  63. BOOL RatingMenu_SetValue(HMENU ratingMenu, INT ratingValue)
  64. {
  65. if (NULL == ratingMenu) return FALSE;
  66. INT ratingList[] = { ID_RATING_VALUE_1, ID_RATING_VALUE_2, ID_RATING_VALUE_3,
  67. ID_RATING_VALUE_4, ID_RATING_VALUE_5};
  68. ratingValue--;
  69. MENUITEMINFO mii = {0};
  70. mii.cbSize = sizeof(MENUITEMINFO);
  71. UINT type = 0, state = 0;
  72. for (INT i = 0; i < ARRAYSIZE(ratingList); i++)
  73. {
  74. mii.fMask = MIIM_STATE | MIIM_FTYPE;
  75. if (GetMenuItemInfo(ratingMenu, ratingList[i], FALSE, &mii))
  76. {
  77. if (ratingValue == i)
  78. {
  79. type = mii.fType | MFT_RADIOCHECK;
  80. state = mii.fState | MFS_CHECKED;
  81. }
  82. else
  83. {
  84. type = mii.fType & ~MFT_RADIOCHECK;
  85. state = mii.fState & ~MFS_CHECKED;
  86. }
  87. mii.fMask = 0;
  88. if (type != mii.fType)
  89. {
  90. mii.fType = type;
  91. mii.fMask |= MIIM_FTYPE;
  92. }
  93. if (state != mii.fState)
  94. {
  95. mii.fState = state;
  96. mii.fMask |= MIIM_STATE;
  97. }
  98. if (0 != mii.fMask)
  99. SetMenuItemInfo(ratingMenu, ratingList[i], FALSE, &mii);
  100. }
  101. }
  102. return TRUE;
  103. }
  104. HRESULT RatingMenu_GetCustomizer(HMENU hMenu, ifc_menucustomizer **customizer)
  105. {
  106. if (NULL == customizer) return E_POINTER;
  107. *customizer = NULL;
  108. HMENU ratingMenu = RatingMenu_FindMenu(hMenu);
  109. if (NULL == ratingMenu)
  110. return S_OK;
  111. ifc_skinhelper *skinHelper = NULL;
  112. HRESULT hr = Plugin_GetSkinHelper(&skinHelper);
  113. if (FAILED(hr) || skinHelper == NULL) return hr;
  114. ifc_skinnedrating *skinnedRating = NULL;
  115. hr = skinHelper->QueryInterface(IFC_SkinnedRating, (void**)&skinnedRating);
  116. if (SUCCEEDED(hr) && skinnedRating != NULL)
  117. {
  118. hr = skinnedRating->CreateMenuCustomizer(ratingMenu, customizer);
  119. skinnedRating->Release();
  120. }
  121. skinHelper->Release();
  122. return hr;
  123. }