aboutsummaryrefslogtreecommitdiff
path: root/Src/Plugins/Library/ml_devices/listWidgetGroup.cpp
blob: 7ac29e9247338933a3055d60a4ce0002ae4a590a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "main.h"
#include "./listWidgetInternal.h"
#include <algorithm>

ListWidgetGroup *
ListWidget_CreateGroup(const char *name)
{
	ifc_devicetype *deviceType;
	wchar_t buffer[1024] = {0};

	if (NULL == name)
		return NULL;

	if (NULL != WASABI_API_DEVICES &&
		S_OK == WASABI_API_DEVICES->TypeFind(name, &deviceType))
	{
		if (FAILED(deviceType->GetDisplayName(buffer, ARRAYSIZE(buffer))))
			buffer[0] = L'\0';

		deviceType->Release();
	}
	else
		buffer[0] = L'\0';

	return ListWidget_CreateGroupEx(name, buffer);
}

ListWidgetGroup *
ListWidget_CreateGroupEx(const char *name, const wchar_t *title)
{
	ListWidgetGroup *group;

	if (NULL == name)
		return NULL;

	group = new ListWidgetGroup();
	if (NULL == group)
		return NULL;

	group->name = AnsiString_Duplicate(name);
	group->title = String_Duplicate(title);
			
	return group;
}


void 
ListWidget_DestroyGroup(ListWidgetGroup *group)
{
	size_t index;
	if (NULL == group)
		return;

	index = group->items.size();
	while(index--)
	{
		ListWidget_DestroyItem(group->items[index]);
	}

	AnsiString_Free(group->name);
	String_Free(group->title);

	delete group;
}

BOOL
ListWidget_AddGroup( ListWidgetCategory *category, ListWidgetGroup *group)
{
	if (NULL == category || NULL == group)
		return FALSE;

	category->groups.push_back(group);
	return TRUE;
}

ListWidgetGroup *
ListWidget_FindGroupEx(ListWidgetCategory *category, const char *name, size_t max)
{
	size_t index, count;

	if (NULL == category || NULL == name)
		return NULL;

	count = category->groups.size();
	if (max < count) 
		count = max;

	for(index = 0; index < count; index++)
	{
		ListWidgetGroup *group = category->groups[index];
		if (CSTR_EQUAL == CompareStringA(CSTR_INVARIANT, 0, name, -1, group->name, -1))
			return group;
	}
	
	return NULL;
}

ListWidgetGroup *
ListWidget_FindGroup(ListWidgetCategory *category, const char *name)
{
	return ListWidget_FindGroupEx(category, name, -1);
}

static int
ListWidget_ItemSortCb(const void *element1, const void *element2)
{
	ListWidgetItem *item1;
	ListWidgetItem *item2;
	int result;

	item1 = (ListWidgetItem*)element1;
	item2 = (ListWidgetItem*)element2;

	result = CompareString(LOCALE_USER_DEFAULT, 0, item1->title, -1, item2->title, -1);
	if (CSTR_EQUAL == result || 0 == result)
		result = CompareStringA(CSTR_INVARIANT, 0, item1->name, -1, item2->name, -1);
		
	return (result - 2);

}
static bool
ListWidget_ItemSortCb_V2(const void* element1, const void* element2)
{
	return ListWidget_ItemSortCb(element1, element2) < 0;
}

void 
ListWidget_SortGroup(ListWidgetGroup *group)
{
	if (group->items.size())
	{
		//qsort(group->items.first(), group->items.size(), sizeof(ListWidgetItem**), ListWidget_ItemSortCb);
		std::sort(group->items.begin(), group->items.end(), ListWidget_ItemSortCb_V2);
	}
}