/
tSubInputList.c
209 lines (168 loc) · 5.07 KB
/
tSubInputList.c
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include "tSubGet.h"
static int getExtentPx(HWND hwndList, wchar_t *text, int textLength){
HANDLE hdc = GetDC(hwndList);
HANDLE hFont = (HANDLE) SendMessage(hwndList, WM_GETFONT, 0, 0);
HANDLE hOld = SelectObject(hdc, hFont);
SIZE sz;
if (hOld == HGDI_ERROR || !hOld || !hFont)
return 0;
GetTextExtentPoint32(hdc, text, textLength, &sz);
SelectObject(hdc, hOld);
ReleaseDC(hwndList, hdc);
return sz.cx + 10; //Add 10px for eye-candy
}
int listAdd(HWND hwndList, wchar_t *fullPath){
wchar_t *ptr, *buf;
int index, newExtent, len = wcslen(fullPath)+1;
if (!(ptr = wcsrchr(fullPath, L'\\')))
ptr = fullPath;
else ptr++;
newExtent = getExtentPx(hwndList, ptr, wcslen(ptr));
if (newExtent > SendMessage(hwndList, LB_GETHORIZONTALEXTENT, 0, 0))
SendMessage(hwndList, LB_SETHORIZONTALEXTENT, newExtent, 0);
index = SendMessage(hwndList, LB_ADDSTRING, 0, (LPARAM) ptr);
if (index < 0)
return FALSE;
buf = malloc(sizeof(wchar_t)*len);
if (!buf)
return FALSE;
wcscpy_s(buf, len, fullPath);
if (SendMessage(hwndList, LB_SETITEMDATA, index, (LPARAM) buf) == LB_ERR){
free(buf);
return FALSE;
}
return TRUE;
}
static int delItem(HWND hwndList, int index){
int ret;
void *buf = (void *)SendMessage(hwndList, LB_GETITEMDATA, index, 0);
if ((int) buf == LB_ERR)
return FALSE;
free(buf);
ret = SendMessage(hwndList, LB_DELETESTRING, index, 0) != LB_ERR;
if (!SendMessage(hwndList, LB_GETCOUNT, 0, 0))
SendMessage(hwndList, LB_SETHORIZONTALEXTENT, 0, 0);
return ret;
}
int listDeleteSelected(HWND hwndList){
int nSelected = SendMessage(hwndList, LB_GETSELCOUNT, 0, 0);
int *buf, nReceived, i, ret = TRUE;
if (nSelected < 0) return FALSE;
buf = malloc(sizeof(int) * nSelected);
if (!buf) return FALSE;
nReceived = SendMessage(hwndList, LB_GETSELITEMS, nSelected, (LPARAM) buf);
if (nReceived != nSelected)
ret = FALSE;
else for (i = nReceived - 1; i >= 0; i--){
if (!delItem(hwndList, buf[i])){
ret = FALSE;
break;
}
}
free (buf);
return ret;
}
int listClear(HWND hwndList){
int i, count = SendMessage(hwndList, LB_GETCOUNT, 0, 0);
if (count >= 0){
for (i = 0; i < count; i++){
if (!delItem(hwndList, 0))
return FALSE;
}
return TRUE;
}
return FALSE;
}
static int moveItem(HWND hwndList, int from, int to){
LPARAM data = SendMessage(hwndList, LB_GETITEMDATA, from, 0);
int len = SendMessage(hwndList, LB_GETTEXTLEN, from, 0);
int ret = TRUE;
wchar_t *name;
if (data == LB_ERR || len == LB_ERR)
return FALSE;
name = malloc(sizeof(wchar_t)*(len + 1));
if (!name)
return FALSE;
if (SendMessage(hwndList, LB_GETTEXT, from, (LPARAM) name) == LB_ERR ||
SendMessage(hwndList, LB_DELETESTRING, from, 0) == LB_ERR){
ret = FALSE;
} else if (SendMessage(hwndList, LB_INSERTSTRING, to, (LPARAM) name) == LB_ERR ||
SendMessage(hwndList, LB_SETITEMDATA, to, data) == LB_ERR){
free((void*) data);
ret = FALSE;
}
SendMessage(hwndList, LB_SETSEL, TRUE, to);
free(name);
return ret;
}
int listMoveSelected(HWND hwndList, int direction){
int nSelected = SendMessage(hwndList, LB_GETSELCOUNT, 0, 0);
int total = SendMessage(hwndList, LB_GETCOUNT, 0, 0);
int *buf, nReceived, to, i, ret = TRUE;
if (nSelected < 0 || total == LB_ERR)
return FALSE;
buf = malloc(sizeof(int) * nSelected);
if (!buf) return FALSE;
nReceived = SendMessage(hwndList, LB_GETSELITEMS, nSelected, (LPARAM) buf);
if (nReceived != nSelected)
ret = FALSE;
else for (i = 0; i < nReceived; i++){
int j;
if (direction == UP){
j = i; //Ensure indexes of ther selected items are unaffected on move
to = buf[j] - 1;
} else{
j = nReceived - i - 1;
to = buf[j] + 1;
}
if (to < 0 || to >= total) break;
else if (!moveItem(hwndList, buf[j], to)){
ret = FALSE;
break;
}
}
free(buf);
return ret;
}
int listGetCount(HWND hwndList){
return SendMessage(hwndList, LB_GETCOUNT, 0, 0);
}
int listGetItem(HWND hwndList, int index, wchar_t **disp, wchar_t *full, size_t fBufSize){
wchar_t *data = (wchar_t*) SendMessage(hwndList, LB_GETITEMDATA, index, 0);
int dispLen = SendMessage(hwndList, LB_GETTEXTLEN, index, 0);
if ((int)data == LB_ERR || dispLen == LB_ERR)
return FALSE;
else if (!(*disp = malloc(sizeof(wchar_t)*(dispLen + 1))))
return FALSE;
if (SendMessage(hwndList, LB_GETTEXT, index, (LPARAM) *disp) == LB_ERR ||
wcsncpy_s(full, fBufSize, data, _TRUNCATE)){
free(*disp);
return FALSE;
}
return TRUE;
}
int listLocateFirstSelected(HWND hwndList){
int nSelected = SendMessage(hwndList, LB_GETSELCOUNT, 0, 0);
int *buf, nReceived, ret = TRUE;
if (nSelected <= 0) return FALSE;
buf = malloc(sizeof(int)*nSelected);
if (!buf) return FALSE;
nReceived = SendMessage(hwndList, LB_GETSELITEMS, nSelected, (LPARAM) buf);
if (nReceived != nSelected)
ret = FALSE;
else{
wchar_t *data = (wchar_t*) SendMessage(hwndList, LB_GETITEMDATA, buf[0], 0);
if ((int)data == LB_ERR)
ret = FALSE;
else {
ITEMIDLIST __unaligned *iilFPath = {0};
CoInitialize(NULL);
iilFPath = ILCreateFromPath(data);
SHOpenFolderAndSelectItems(iilFPath,0,NULL,0);
ILFree(iilFPath);
CoUninitialize();
}
}
free(buf);
return ret;
}