listWidgetCommand.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. #include "main.h"
  2. #include "./listWidgetInternal.h"
  3. #define REQUEST_STRING ((wchar_t*)1)
  4. #define REQUEST_IMAGE ((DeviceImage*)1)
  5. typedef struct ListWidgetCommand
  6. {
  7. char *name;
  8. ListWidgetCommandState state;
  9. wchar_t *title;
  10. wchar_t *description;
  11. DeviceImage *imageLarge;
  12. DeviceImage *imageSmall;
  13. RECT rect;
  14. } ListWidgetCommand;
  15. ListWidgetCommand *
  16. ListWidget_CreateCommand(const char *name, BOOL primary, BOOL disabled)
  17. {
  18. ListWidgetCommand *self;
  19. if (NULL == name)
  20. return NULL;
  21. self = (ListWidgetCommand*)malloc(sizeof(ListWidgetCommand));
  22. if (NULL == self)
  23. return NULL;
  24. ZeroMemory(self, sizeof(ListWidgetCommand));
  25. self->name = AnsiString_Duplicate(name);
  26. self->state = ListWidgetCommandState_Normal;
  27. if (NULL != self->name)
  28. {
  29. if (FALSE != primary)
  30. self->state |= ListWidgetCommandState_Primary;
  31. if (FALSE != disabled)
  32. self->state |= ListWidgetCommandState_Disabled;
  33. }
  34. else
  35. {
  36. ListWidget_DestroyCommand(self);
  37. return NULL;
  38. }
  39. self->title = REQUEST_STRING;
  40. self->description = REQUEST_STRING;
  41. self->imageLarge = REQUEST_IMAGE;
  42. self->imageSmall = REQUEST_IMAGE;
  43. return self;
  44. }
  45. void
  46. ListWidget_DestroyCommand(ListWidgetCommand *command)
  47. {
  48. if (NULL == command)
  49. return;
  50. AnsiString_Free(command->name);
  51. if (NULL != command->imageLarge && REQUEST_IMAGE != command->imageLarge)
  52. DeviceImage_Release(command->imageLarge);
  53. if (NULL != command->imageSmall && REQUEST_IMAGE != command->imageSmall)
  54. DeviceImage_Release(command->imageSmall);
  55. if (REQUEST_STRING != command->title)
  56. String_Free(command->title);
  57. if (REQUEST_STRING != command->description)
  58. String_Free(command->description);
  59. free(command);
  60. }
  61. size_t
  62. ListWigdet_GetDeviceCommands(ListWidgetCommand **buffer, size_t bufferMax, ifc_device *device)
  63. {
  64. size_t count;
  65. ifc_devicesupportedcommandenum *enumerator;
  66. ifc_devicesupportedcommand *command;
  67. DeviceCommandFlags flags;
  68. ListWidgetCommand *widgetCommand;
  69. BOOL primaryFound;
  70. if (NULL == buffer || bufferMax < 1 || NULL == device)
  71. return 0;
  72. if (FAILED(device->EnumerateCommands(&enumerator, DeviceCommandContext_View)))
  73. return 0;
  74. count = 0;
  75. primaryFound = FALSE;
  76. while(S_OK == enumerator->Next(&command, 1, NULL))
  77. {
  78. if(FAILED(command->GetFlags(&flags)))
  79. flags = DeviceCommandFlag_None;
  80. if (0 == (DeviceCommandFlag_Hidden & flags))
  81. {
  82. widgetCommand = ListWidget_CreateCommand(command->GetName(),
  83. (0 != (DeviceCommandFlag_Primary & flags)),
  84. (0 != (DeviceCommandFlag_Disabled & flags)));
  85. if (NULL != widgetCommand)
  86. {
  87. if (0 != (DeviceCommandFlag_Primary & flags))
  88. {
  89. if (count == bufferMax)
  90. {
  91. ListWidget_DestroyCommand(buffer[count-1]);
  92. count--;
  93. }
  94. if (count > 0)
  95. MoveMemory(&buffer[1], buffer, sizeof(ListWidgetCommand*) * count);
  96. buffer[0] = widgetCommand;
  97. primaryFound = TRUE;
  98. count++;
  99. }
  100. else
  101. {
  102. if (count < bufferMax)
  103. {
  104. buffer[count] = widgetCommand;
  105. count++;
  106. }
  107. }
  108. }
  109. }
  110. command->Release();
  111. if (count == bufferMax && FALSE != primaryFound)
  112. break;
  113. }
  114. enumerator->Release();
  115. return count;
  116. }
  117. void
  118. ListWidget_DestroyAllCommands(ListWidgetCommand **buffer, size_t bufferMax)
  119. {
  120. if (NULL == buffer)
  121. return;
  122. while(bufferMax--)
  123. ListWidget_DestroyCommand(buffer[bufferMax]);
  124. }
  125. const char *
  126. ListWidget_GetCommandName(ListWidgetCommand *command)
  127. {
  128. return (NULL != command) ? command->name : NULL;
  129. }
  130. const wchar_t *
  131. ListWidget_GetCommandTitle(ListWidgetCommand *command)
  132. {
  133. if (NULL == command || NULL == command->title)
  134. return NULL;
  135. if (REQUEST_STRING == command->title)
  136. {
  137. ifc_devicecommand *info;
  138. command->title = NULL;
  139. if (NULL != WASABI_API_DEVICES &&
  140. S_OK == WASABI_API_DEVICES->CommandFind(command->name, &info))
  141. {
  142. wchar_t buffer[512] = {0};
  143. if (SUCCEEDED(info->GetDisplayName(buffer, ARRAYSIZE(buffer))))
  144. command->title = String_Duplicate(buffer);
  145. info->Release();
  146. }
  147. }
  148. return command->title;
  149. }
  150. const wchar_t *
  151. ListWidget_GetCommandDescription(ListWidgetCommand *command)
  152. {
  153. if (NULL == command || NULL == command->description)
  154. return NULL;
  155. if (REQUEST_STRING == command->description)
  156. {
  157. ifc_devicecommand *info;
  158. command->description = NULL;
  159. if (NULL != WASABI_API_DEVICES &&
  160. S_OK == WASABI_API_DEVICES->CommandFind(command->name, &info))
  161. {
  162. wchar_t buffer[1024] = {0};
  163. if (SUCCEEDED(info->GetDescription(buffer, ARRAYSIZE(buffer))))
  164. command->description = String_Duplicate(buffer);
  165. info->Release();
  166. }
  167. }
  168. return command->description;
  169. }
  170. HBITMAP
  171. ListWidget_GetCommandLargeBitmap(WidgetStyle *style, ListWidgetCommand *command, int width, int height)
  172. {
  173. if (NULL == style || NULL == command || NULL == command->imageLarge)
  174. return NULL;
  175. if (REQUEST_IMAGE == command->imageLarge)
  176. {
  177. ifc_devicecommand *info;
  178. command->imageLarge = NULL;
  179. if (NULL != WASABI_API_DEVICES &&
  180. S_OK == WASABI_API_DEVICES->CommandFind(command->name, &info))
  181. {
  182. wchar_t buffer[MAX_PATH * 2] = {0};
  183. if (FAILED(info->GetIcon(buffer, ARRAYSIZE(buffer), width, height)))
  184. buffer[0] = L'\0';
  185. info->Release();
  186. if (L'\0' != buffer[0])
  187. {
  188. command->imageLarge = DeviceImageCache_GetImage(Plugin_GetImageCache(), buffer,
  189. width, height, NULL, NULL);
  190. }
  191. }
  192. }
  193. return DeviceImage_GetBitmap(command->imageLarge, DeviceImage_Normal);
  194. }
  195. HBITMAP
  196. ListWidget_GetCommandSmallBitmap(WidgetStyle *style, ListWidgetCommand *command, int width, int height)
  197. {
  198. if (NULL == style || NULL == command || NULL == command->imageSmall)
  199. return NULL;
  200. if (REQUEST_IMAGE == command->imageSmall)
  201. {
  202. ifc_devicecommand *info;
  203. command->imageSmall = NULL;
  204. if (NULL != WASABI_API_DEVICES &&
  205. S_OK == WASABI_API_DEVICES->CommandFind(command->name, &info))
  206. {
  207. wchar_t buffer[MAX_PATH * 2] = {0};
  208. if (FAILED(info->GetIcon(buffer, ARRAYSIZE(buffer), width, height)))
  209. buffer[0] = L'\0';
  210. info->Release();
  211. if (L'\0' != buffer[0])
  212. {
  213. command->imageSmall = DeviceImageCache_GetImage(Plugin_GetImageCache(), buffer,
  214. width, height, NULL, NULL);
  215. }
  216. }
  217. }
  218. return DeviceImage_GetBitmap(command->imageSmall, DeviceImage_Normal);
  219. }
  220. BOOL
  221. ListWidget_ResetCommandImages(ListWidgetCommand *command)
  222. {
  223. if (NULL == command)
  224. return FALSE;
  225. if (REQUEST_IMAGE != command->imageLarge)
  226. {
  227. if (NULL != command->imageLarge)
  228. DeviceImage_Release(command->imageLarge);
  229. command->imageLarge = REQUEST_IMAGE;
  230. }
  231. if (REQUEST_IMAGE != command->imageSmall)
  232. {
  233. if (NULL != command->imageSmall)
  234. DeviceImage_Release(command->imageSmall);
  235. command->imageSmall = REQUEST_IMAGE;
  236. }
  237. return TRUE;
  238. }
  239. BOOL
  240. ListWidget_GetCommandRect(ListWidgetCommand *command, RECT *rect)
  241. {
  242. if (NULL == command || NULL == rect)
  243. return FALSE;
  244. return CopyRect(rect, &command->rect);
  245. }
  246. BOOL
  247. ListWidget_SetCommandRect(ListWidgetCommand *command, const RECT *rect)
  248. {
  249. if (NULL == command || NULL == rect)
  250. return FALSE;
  251. return CopyRect(&command->rect, rect);
  252. }
  253. BOOL
  254. ListWidget_GetCommandRectEqual(ListWidgetCommand *command, const RECT *rect)
  255. {
  256. if (NULL == command || NULL == rect)
  257. return FALSE;
  258. return EqualRect(&command->rect, rect);
  259. }
  260. BOOL
  261. ListWidget_GetCommandPrimary(ListWidgetCommand *command)
  262. {
  263. return (NULL == command ||
  264. (0 != (ListWidgetCommandState_Primary & command->state)));
  265. }
  266. BOOL
  267. ListWidget_GetCommandDisabled(ListWidgetCommand *command)
  268. {
  269. return (NULL == command ||
  270. (0 != (ListWidgetCommandState_Disabled & command->state)));
  271. }
  272. BOOL
  273. ListWidget_GetCommandPressed(ListWidgetCommand *command)
  274. {
  275. return (NULL == command ||
  276. (0 != (ListWidgetCommandState_Pressed & command->state)));
  277. }
  278. BOOL
  279. ListWidget_EnableCommand(ListWidgetCommand *command, BOOL enable)
  280. {
  281. if (NULL == command)
  282. return FALSE;
  283. if ((FALSE == enable) == (0 != (ListWidgetCommandState_Disabled & command->state)))
  284. return FALSE;
  285. if (FALSE == enable)
  286. command->state |= ListWidgetCommandState_Disabled;
  287. else
  288. command->state &= ~ListWidgetCommandState_Disabled;
  289. return TRUE;
  290. }
  291. BOOL
  292. ListWidget_SetCommandPressed(ListWidgetCommand *command, BOOL pressed)
  293. {
  294. if (NULL == command)
  295. return FALSE;
  296. if ((FALSE == pressed) == (0 == (ListWidgetCommandState_Pressed & command->state)))
  297. return FALSE;
  298. if (FALSE == pressed)
  299. command->state &= ~ListWidgetCommandState_Pressed;
  300. else
  301. command->state |= ListWidgetCommandState_Pressed;
  302. return TRUE;
  303. }