local_menu.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #include "main.h"
  2. #include "./local_menu.h"
  3. unsigned int
  4. Menu_InsertDeviceItems(HMENU menu, int position, unsigned int baseId,
  5. ifc_device *device, DeviceCommandContext context)
  6. {
  7. unsigned int count, separator;
  8. MENUITEMINFO itemInfo = {0};
  9. wchar_t itemName[512] = {0};
  10. ifc_devicecommand *commandInfo;
  11. ifc_devicesupportedcommandenum *enumerator;
  12. ifc_devicesupportedcommand *command;
  13. DeviceCommandFlags commandFlags;
  14. if (NULL == device || NULL == menu)
  15. return 0;
  16. if (FAILED(device->EnumerateCommands(&enumerator, context)))
  17. return 0;
  18. itemInfo.cbSize = sizeof(itemInfo);
  19. itemInfo.fMask = MIIM_DATA | MIIM_ID | MIIM_FTYPE | MIIM_STATE | MIIM_STRING;
  20. count = 0;
  21. separator = 0;
  22. while(S_OK == enumerator->Next(&command, 1, NULL))
  23. {
  24. if(SUCCEEDED(command->GetFlags(&commandFlags)))
  25. {
  26. if (0 != (DeviceCommandFlag_Group & commandFlags) &&
  27. separator != count)
  28. {
  29. itemInfo.fType = MFT_SEPARATOR;
  30. itemInfo.fState = MFS_ENABLED;
  31. itemInfo.wID = 0xFFFE;
  32. itemInfo.dwItemData = NULL;
  33. if (0 != InsertMenuItem(menu, position + count, TRUE, &itemInfo))
  34. {
  35. count++;
  36. separator = count;
  37. }
  38. }
  39. if (0 == (DeviceCommandFlag_Hidden & commandFlags))
  40. {
  41. if (S_OK == WASABI_API_DEVICES->CommandFind(command->GetName(), &commandInfo))
  42. {
  43. if (SUCCEEDED(commandInfo->GetDisplayName(itemName, ARRAYSIZE(itemName))))
  44. {
  45. itemInfo.dwItemData = (ULONG_PTR)AnsiString_Duplicate(command->GetName());
  46. if (NULL != itemInfo.dwItemData)
  47. {
  48. itemInfo.fType = MFT_STRING;
  49. itemInfo.dwTypeData = itemName;
  50. itemInfo.fState = 0;
  51. itemInfo.wID = baseId + count;
  52. if (0 == (DeviceCommandFlag_Disabled & commandFlags))
  53. itemInfo.fState |= MFS_ENABLED;
  54. else
  55. itemInfo.fState |= (MFS_DISABLED | MFS_GRAYED);
  56. if (0 != (DeviceCommandFlag_Primary & commandFlags))
  57. itemInfo.fState |= MFS_DEFAULT;
  58. if (0 != InsertMenuItem(menu, position + count, TRUE, &itemInfo))
  59. count++;
  60. else
  61. AnsiString_Free((char*)itemInfo.dwItemData);
  62. }
  63. }
  64. commandInfo->Release();
  65. }
  66. }
  67. }
  68. command->Release();
  69. }
  70. enumerator->Release();
  71. return count;
  72. }
  73. unsigned int
  74. Menu_FreeItemData(HMENU menu, unsigned int start, int count)
  75. {
  76. unsigned int processed;
  77. MENUITEMINFO itemInfo;
  78. if (NULL == menu)
  79. return 0;
  80. if (count < 0 )
  81. count = GetMenuItemCount(menu);
  82. if (start > (unsigned int)count)
  83. return 0;
  84. count -= start;
  85. processed = 0;
  86. itemInfo.cbSize = sizeof(itemInfo);
  87. itemInfo.fMask = MIIM_DATA;
  88. while(count-- &&
  89. FALSE != GetMenuItemInfo(menu, start + processed, TRUE, &itemInfo))
  90. {
  91. AnsiString_Free((char*)itemInfo.dwItemData);
  92. processed++;
  93. }
  94. return processed;
  95. }
  96. ULONG_PTR
  97. Menu_GetItemData(HMENU menu, unsigned int item, BOOL byPosition)
  98. {
  99. MENUITEMINFO itemInfo;
  100. if (NULL == menu)
  101. return 0;
  102. itemInfo.cbSize = sizeof(itemInfo);
  103. itemInfo.fMask = MIIM_DATA;
  104. if (FALSE != GetMenuItemInfo(menu, item, byPosition, &itemInfo))
  105. return itemInfo.dwItemData;
  106. return 0;
  107. }
  108. unsigned int
  109. Menu_FindItemByData(HMENU menu, Menu_FindItemByDataCb callback, void *user)
  110. {
  111. int index, count;
  112. MENUITEMINFO itemInfo;
  113. if (NULL == menu || NULL == callback)
  114. return -1;
  115. count = GetMenuItemCount(menu);
  116. if (0 == count)
  117. return -1;
  118. itemInfo.cbSize = sizeof(itemInfo);
  119. itemInfo.fMask = MIIM_DATA | MIIM_ID;
  120. for (index = 0; index < count; index++)
  121. {
  122. if (FALSE != GetMenuItemInfo(menu, index, TRUE, &itemInfo) &&
  123. FALSE != callback(itemInfo.dwItemData, user))
  124. {
  125. return itemInfo.wID;
  126. }
  127. }
  128. return -1;
  129. }
  130. static BOOL
  131. Menu_FindItemByAnsiStringDataCb(ULONG_PTR param, void *user)
  132. {
  133. const char *string1, *string2;
  134. string1 = (const char*)param;
  135. string2 = (const char*)user;
  136. if (NULL == string1 || NULL == string2)
  137. return (string1 == string2);
  138. return (CSTR_EQUAL == CompareStringA(CSTR_INVARIANT, 0, string1, -1, string2, -1));
  139. }
  140. unsigned int
  141. Menu_FindItemByAnsiStringData(HMENU menu, const char *string)
  142. {
  143. return Menu_FindItemByData(menu, Menu_FindItemByAnsiStringDataCb, (void*)string);
  144. }