aboutsummaryrefslogtreecommitdiff
path: root/Src/Plugins/Library/ml_devices/local_menu.cpp
blob: 446552f1fdc5fb2471d8fee0161113c21439e5d1 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include "main.h"
#include "./local_menu.h"

unsigned int
Menu_InsertDeviceItems(HMENU menu, int position, unsigned int baseId, 
					   ifc_device *device, DeviceCommandContext context)
{
	unsigned int count, separator;
	MENUITEMINFO itemInfo = {0};
	wchar_t itemName[512] = {0};

	ifc_devicecommand *commandInfo;
	ifc_devicesupportedcommandenum *enumerator;
	ifc_devicesupportedcommand *command;
	DeviceCommandFlags commandFlags;

	if (NULL == device || NULL == menu)
		return 0;

	if (FAILED(device->EnumerateCommands(&enumerator, context)))
		return 0;

	itemInfo.cbSize = sizeof(itemInfo);
	itemInfo.fMask = MIIM_DATA | MIIM_ID | MIIM_FTYPE | MIIM_STATE | MIIM_STRING;
		
	count = 0;
	separator = 0;

	while(S_OK == enumerator->Next(&command, 1, NULL))
	{
		if(SUCCEEDED(command->GetFlags(&commandFlags)))
		{
			if (0 != (DeviceCommandFlag_Group & commandFlags) && 
				separator != count)
			{
				itemInfo.fType = MFT_SEPARATOR;
				itemInfo.fState = MFS_ENABLED;
				itemInfo.wID = 0xFFFE;
				itemInfo.dwItemData = NULL;
				if (0 != InsertMenuItem(menu, position + count, TRUE, &itemInfo))
				{
					count++;
					separator = count;
				}
			}
			if (0 == (DeviceCommandFlag_Hidden & commandFlags))
			{
				if (S_OK == WASABI_API_DEVICES->CommandFind(command->GetName(), &commandInfo))
				{
					if (SUCCEEDED(commandInfo->GetDisplayName(itemName, ARRAYSIZE(itemName))))
					{						
						itemInfo.dwItemData = (ULONG_PTR)AnsiString_Duplicate(command->GetName());
						if (NULL != itemInfo.dwItemData)
						{
							itemInfo.fType = MFT_STRING;
							itemInfo.dwTypeData = itemName;
							itemInfo.fState = 0;
							itemInfo.wID = baseId + count;

							if (0 == (DeviceCommandFlag_Disabled & commandFlags))
								itemInfo.fState |= MFS_ENABLED;
							else
								itemInfo.fState |= (MFS_DISABLED | MFS_GRAYED);
														
							if (0 != (DeviceCommandFlag_Primary & commandFlags))
								itemInfo.fState |= MFS_DEFAULT;

							if (0 != InsertMenuItem(menu, position + count, TRUE, &itemInfo))
								count++;
							else
								AnsiString_Free((char*)itemInfo.dwItemData);
						}

					}
					commandInfo->Release();
				}
			}
		}
		command->Release();
	}
	

	enumerator->Release();
	
	return count;
}

unsigned int 
Menu_FreeItemData(HMENU menu, unsigned int start, int count)
{
	unsigned int processed;
	MENUITEMINFO itemInfo;

	if (NULL == menu)
		return 0;
	
	if (count < 0 )
		count = GetMenuItemCount(menu);

	if (start > (unsigned int)count)
		return 0;
	
	count -= start;
	processed = 0;

	itemInfo.cbSize = sizeof(itemInfo);
	itemInfo.fMask = MIIM_DATA;

	while(count-- && 
		  FALSE != GetMenuItemInfo(menu, start + processed, TRUE, &itemInfo))
	{
		AnsiString_Free((char*)itemInfo.dwItemData);
		processed++;
	}
	
	return processed;
}

ULONG_PTR
Menu_GetItemData(HMENU menu, unsigned int item, BOOL byPosition)
{
	MENUITEMINFO itemInfo;

	if (NULL == menu)
		return 0;

	itemInfo.cbSize = sizeof(itemInfo);
	itemInfo.fMask = MIIM_DATA;

	if (FALSE != GetMenuItemInfo(menu, item, byPosition, &itemInfo))
		return itemInfo.dwItemData;
	
	return 0;
}

unsigned int
Menu_FindItemByData(HMENU menu, Menu_FindItemByDataCb callback, void *user)
{
	int index, count;
	MENUITEMINFO itemInfo;

	if (NULL == menu || NULL == callback)
		return -1;

	count = GetMenuItemCount(menu);
	if (0 == count)
		return -1;

	itemInfo.cbSize = sizeof(itemInfo);
	itemInfo.fMask = MIIM_DATA | MIIM_ID;

	for (index = 0; index < count; index++)
	{
		if (FALSE != GetMenuItemInfo(menu, index, TRUE, &itemInfo) &&
			FALSE != callback(itemInfo.dwItemData, user))
		{
			return itemInfo.wID;
		}
	}
	
	return -1;

}

static BOOL
Menu_FindItemByAnsiStringDataCb(ULONG_PTR param, void *user)
{
	const char *string1, *string2;
	
	string1 = (const char*)param;
	string2 = (const char*)user;
	
	if (NULL == string1 || NULL == string2)
		return (string1 == string2);

	return (CSTR_EQUAL == CompareStringA(CSTR_INVARIANT, 0, string1, -1, string2, -1));
}

unsigned int
Menu_FindItemByAnsiStringData(HMENU menu, const char *string)
{
	return Menu_FindItemByData(menu, Menu_FindItemByAnsiStringDataCb, (void*)string);
}