listWidgetGroup.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include "main.h"
  2. #include "./listWidgetInternal.h"
  3. #include <algorithm>
  4. ListWidgetGroup *
  5. ListWidget_CreateGroup(const char *name)
  6. {
  7. ifc_devicetype *deviceType;
  8. wchar_t buffer[1024] = {0};
  9. if (NULL == name)
  10. return NULL;
  11. if (NULL != WASABI_API_DEVICES &&
  12. S_OK == WASABI_API_DEVICES->TypeFind(name, &deviceType))
  13. {
  14. if (FAILED(deviceType->GetDisplayName(buffer, ARRAYSIZE(buffer))))
  15. buffer[0] = L'\0';
  16. deviceType->Release();
  17. }
  18. else
  19. buffer[0] = L'\0';
  20. return ListWidget_CreateGroupEx(name, buffer);
  21. }
  22. ListWidgetGroup *
  23. ListWidget_CreateGroupEx(const char *name, const wchar_t *title)
  24. {
  25. ListWidgetGroup *group;
  26. if (NULL == name)
  27. return NULL;
  28. group = new ListWidgetGroup();
  29. if (NULL == group)
  30. return NULL;
  31. group->name = AnsiString_Duplicate(name);
  32. group->title = String_Duplicate(title);
  33. return group;
  34. }
  35. void
  36. ListWidget_DestroyGroup(ListWidgetGroup *group)
  37. {
  38. size_t index;
  39. if (NULL == group)
  40. return;
  41. index = group->items.size();
  42. while(index--)
  43. {
  44. ListWidget_DestroyItem(group->items[index]);
  45. }
  46. AnsiString_Free(group->name);
  47. String_Free(group->title);
  48. delete group;
  49. }
  50. BOOL
  51. ListWidget_AddGroup( ListWidgetCategory *category, ListWidgetGroup *group)
  52. {
  53. if (NULL == category || NULL == group)
  54. return FALSE;
  55. category->groups.push_back(group);
  56. return TRUE;
  57. }
  58. ListWidgetGroup *
  59. ListWidget_FindGroupEx(ListWidgetCategory *category, const char *name, size_t max)
  60. {
  61. size_t index, count;
  62. if (NULL == category || NULL == name)
  63. return NULL;
  64. count = category->groups.size();
  65. if (max < count)
  66. count = max;
  67. for(index = 0; index < count; index++)
  68. {
  69. ListWidgetGroup *group = category->groups[index];
  70. if (CSTR_EQUAL == CompareStringA(CSTR_INVARIANT, 0, name, -1, group->name, -1))
  71. return group;
  72. }
  73. return NULL;
  74. }
  75. ListWidgetGroup *
  76. ListWidget_FindGroup(ListWidgetCategory *category, const char *name)
  77. {
  78. return ListWidget_FindGroupEx(category, name, -1);
  79. }
  80. static int
  81. ListWidget_ItemSortCb(const void *element1, const void *element2)
  82. {
  83. ListWidgetItem *item1;
  84. ListWidgetItem *item2;
  85. int result;
  86. item1 = (ListWidgetItem*)element1;
  87. item2 = (ListWidgetItem*)element2;
  88. result = CompareString(LOCALE_USER_DEFAULT, 0, item1->title, -1, item2->title, -1);
  89. if (CSTR_EQUAL == result || 0 == result)
  90. result = CompareStringA(CSTR_INVARIANT, 0, item1->name, -1, item2->name, -1);
  91. return (result - 2);
  92. }
  93. static bool
  94. ListWidget_ItemSortCb_V2(const void* element1, const void* element2)
  95. {
  96. return ListWidget_ItemSortCb(element1, element2) < 0;
  97. }
  98. void
  99. ListWidget_SortGroup(ListWidgetGroup *group)
  100. {
  101. if (group->items.size())
  102. {
  103. //qsort(group->items.first(), group->items.size(), sizeof(ListWidgetItem**), ListWidget_ItemSortCb);
  104. std::sort(group->items.begin(), group->items.end(), ListWidget_ItemSortCb_V2);
  105. }
  106. }