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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
|
#define is_fdir(wfd) wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY // For wfds
void search_dir(char * dir, char * input, file_list * list, HWND * hwnd_in, bool * cont);
void __cdecl search_thread(void * pparam);
void init_wclass(WNDCLASS * wclass);
void sdraw_border(HDC hdc);
void sdraw_items(HDC hdc, file_list * list, int x, int * y);
void sdraw_item(HDC hdc, int x, int * y, file_list * list, unsigned int counter);
void sdraw_title(HDC hdc, file_list * list, char * dir, char * input, int * y);
void sdraw_status(HDC, int x, int * y, bool is_done);
void sproc_mouse(UINT msg, WPARAM wParam, LPARAM lParam, file_list * list);
void sproc_mouse_click(UINT msg, WPARAM wParam, LPARAM lParam, file_list * list);
bool flicker = 0;
typedef struct {
file_list * list;
char dir[MAX_PATH+1];
char input[MAX_SEARCH_INPUT];
bool is_done;
bool cont;
} PARAMS;
void sproc_drawing(UINT msg, WPARAM wParam, LPARAM lParam, PARAMS params);
/*
This is a recursive function, it takes a pointer to a file list which
it will populate with the files that mach the search criteria `input`
it also expects a parent directory to start searching from. Note that
the file list needs not be empty, in the case that it isn't, the files
found will be appended to the existing files harmlessly.
If hwnd_in is not NULL, search_dir will send a UWM_SEARCH_UPDATE msg
to hwnd_in everytime it updates `list` with a new file. `cont` will
cause the thread to terminate if it is set to 1 while the function is
executing.
*/
void search_dir(char * dir, char * input, file_list * list, HWND * hwnd_in, bool * cont){
HANDLE search_handle;
WIN32_FIND_DATA wfd;
// First preform the search criteria in the dir given
slash_path(dir, dir);
SetCurrentDirectory(dir);
bool is_done = 0;
search_handle = FindFirstFile(input, &wfd);
while(!is_done && search_handle != INVALID_HANDLE_VALUE){
if(! *cont){
FindClose(search_handle);
return ; // Bail out son
}
file_list_append(list, dir, &wfd); // This is multi-thread safe
if(hwnd_in != NULL){
SendMessage(*hwnd_in, UWM_SEARCH_UPDATE, 0, 0);
}
if(!FindNextFile(search_handle, &wfd) ){
if(GetLastError() != ERROR_NO_MORE_FILES){
error("from:FindNextFile");
}
is_done = 1;
}
}
FindClose(search_handle);
/*
Now that we've searched the current directory, let iterate through all
the sub dirs in this directory and recursively call this function on them.
*/
char path[MAX_PATH+3]; // space for the \\*
get_astrick_path(dir, path); // Path should now be dir\*
// Disregard ./ (this directory)
search_handle = FindFirstFile(path, &wfd);
// Now disregard ../ (parent dir)
FindNextFile(search_handle, &wfd);
// Check the rest
while(FindNextFile(search_handle, &wfd)){
if(! *cont){
FindClose(search_handle);
return ; // Bail out son
}
if(is_fdir(wfd)){ // Is this file a dir?
strcpy(path, dir);
strcat(path, wfd.cFileName);
// Path should now be current_dir\this_dir
search_dir(path, input, list, hwnd_in, cont);
}
}
FindClose(search_handle);
}
// Search window: Display search results
void __cdecl search_thread(void * pparam){
PARAMS * p = (PARAMS*) pparam;
p->is_done = 0;
search_dir(p->dir, p->input, p->list, NULL, &(p->cont));
p->is_done = 1;
}
LRESULT APIENTRY s_wndproc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){
static PARAMS params ;
static HDC hdc;
static file_list * search_list;
switch(msg){
case WM_CREATE:
search_list = create_empty_list();
params.list = search_list;
params.cont = 1;
// Make local copies of the given args since they are likely to change
// Once we create the thread.
strcpy(params.dir, main_list->dir);
strcpy(params.input, fsearch_input);
SetTimer(hwnd, SEARCH_TIMER, SEARCH_TIMER_UPDATE, NULL);
_beginthread(search_thread, 0, ¶ms);
break;
case WM_PAINT:
sproc_drawing(msg , wParam, lParam, params);
break;
case(WM_LBUTTONDOWN):
case(WM_RBUTTONDOWN):
case(WM_LBUTTONUP):
case(WM_RBUTTONUP):
case(WM_LBUTTONDBLCLK):
sproc_mouse_click(msg, wParam, lParam, search_list);
break;
case WM_MOUSEMOVE:
sproc_mouse(msg, wParam, lParam, search_list);
break;
case UWM_MAIN_WINDOW_MOVE:
MoveWindow(hwnd,
user_config.search_pos_x,
user_config.search_pos_y ,
user_config.search_width,
user_config.search_height,
1);
break;
case WM_TIMER:
if(wParam == SEARCH_TIMER && !params.is_done){
InvalidateRect(hwnd, NULL, 0);
}
if(params.is_done){
KillTimer(hwnd, SEARCH_TIMER);
InvalidateRect(hwnd, NULL, 0);
}
break;
case WM_CLOSE:
params.cont = 0;
destroy_list(search_list);
DestroyWindow(hwnd);
break;
case WM_DESTROY:
return 0;
}
return DefWindowProc(hwnd, msg, wParam, lParam) ;
}
// Init search and create a window for it
void create_search(char * dir_in, char * input_in){
WNDCLASS wclass;
init_wclass(&wclass);
RegisterClass(&wclass);
if(f_search_window_alive){
SendMessage(shwnd, WM_CLOSE, 0, 0);
}
f_search_window_alive = 1;
shwnd = CreateWindow("SearchWindowClass", NULL, WS_CHILDWINDOW | WS_BORDER | WS_VISIBLE,
user_config.search_pos_x, user_config.search_pos_y ,
user_config.search_width, user_config.search_height, GetDesktopWindow(),
NULL, Instance, NULL);
set_transparency(shwnd, user_config.transparency);
desktop_stick(shwnd);
}
void init_wclass(WNDCLASS * wclass){
wclass->style = CS_HREDRAW | CS_VREDRAW ;
wclass->cbClsExtra = 0 ;
wclass->cbWndExtra = 0 ;
wclass->hInstance = Instance ;
wclass->hIcon = NULL ;
wclass->hCursor = LoadCursor (NULL, IDC_ARROW) ;
wclass->hbrBackground = CreateSolidBrush(user_config.cbackground) ;
wclass->lpszMenuName = NULL ;
wclass->lpfnWndProc = s_wndproc;
wclass->lpszClassName = "SearchWindowClass";
}
void sproc_drawing(UINT msg, WPARAM wParam, LPARAM lParam, PARAMS params){
static HDC hdc ;
hdc = GetDC(shwnd);
SelectObject(hdc, hfont);
int x = 30;
int y = 10;
SetBkMode(hdc, TRANSPARENT);
sdraw_border(hdc);
sdraw_title(hdc, params.list, params.dir, params.input, &y);
sdraw_items(hdc, params.list, x, &y);
sdraw_status(hdc, x, &y, params.is_done);
ReleaseDC(hwnd, hdc);
}
void sdraw_border(HDC hdc){
SelectObject(hdc, hbkbrush);
SelectObject(hdc, border_pen);
Rectangle(hdc, 0, 0, user_config.search_width-2, user_config.search_height-2);
}
void sdraw_title(HDC hdc, file_list * list, char * dir, char * input, int * y){
char out[MAX_PATH+256];
sprintf(out, "In %s", dir);
SetTextColor(hdc, user_config.csearch);
TextOut(hdc, 10, *y, out, strlen(out));
sprintf(out, "%d Results found for (%s)", list->last_item, input);
*y += tm.tmHeight + tm.tmExternalLeading;
TextOut(hdc, 10, *y, out, strlen(out));
*y += 15;
SelectObject(hdc, border_pen);
MoveToEx (hdc, 10, *y, NULL);
LineTo (hdc, user_config.search_width - 10, *y);
*y += 5;
}
void sdraw_status(HDC hdc, int x, int * y, bool is_done){
SelectObject(hdc, border_pen);
MoveToEx (hdc, 10, *y, NULL);
LineTo(hdc, user_config.search_width - 10, *y);
static char out[32];
SetTextColor(hdc, user_config.cnormal);
strcpy(out, is_done ? "Search completed" : "Searching in progress");
TextOut(hdc, x, *y + tm.tmHeight + tm.tmExternalLeading, out, strlen(out));
static SIZE ts;
GetTextExtentPoint32(hdc, out, strlen(out), &ts);
SetTextColor(hdc, user_config.cflickers[flicker]);
TextOut(hdc, x + ts.cx, *y + tm.tmHeight + tm.tmExternalLeading, "...", 3);
}
void sdraw_items(HDC hdc, file_list * list, int x, int * y){
EnterCriticalSection(&(list->cs)); // for multithreading protection
unsigned int counter = 0;
while(counter != list->last_item && *y < user_config.search_height - 40 ){
sdraw_item(hdc, x, y, list, counter);
counter++;
}
LeaveCriticalSection(&(list->cs));
}
void sdraw_item(HDC hdc, int x, int * y, file_list * list, unsigned int id){
file * file_in = list->index[id];
WIN32_FIND_DATA wfd = file_in->wfd;
SIZE curr_size;
GetTextExtentPoint32(hdc, wfd.cFileName, strlen(wfd.cFileName), &curr_size);
file_in->area.left = x;
file_in->area.top = *y;
file_in->area.right = x+curr_size.cx;
file_in->area.bottom = *y+curr_size.cy;
static char item_out[(FILENAME_MAX * 2) + 2];
if(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY){
draw_directory(hdc, x, *y, id, item_out, &wfd);
}
else if(id == list->selected) {
SetTextColor(hdc, f_got_focus ? user_config.cselected : user_config.cselected_nofocus);
TextOut(hdc, x, *y, wfd.cFileName, strlen(wfd.cFileName));
}
else{
draw_normal_item(hdc, &wfd, x, y);
}
draw_item_icon(hdc, list, x, *y, id);
*y += tm.tmHeight + tm.tmExternalLeading;
}
void sproc_mouse(UINT message, WPARAM wParam, LPARAM lParam, file_list * list){
if(message == WM_MOUSEMOVE){
int xpos = LOWORD(lParam);
int ypos = HIWORD(lParam);
if(xpos > tm.tmHeight + tm.tmExternalLeading + 15){
POINT in_point = {xpos, ypos};
unsigned int counter = list->start_id;
file * curr_file;
while(counter != list->last_item){
curr_file = list->index[counter];
if(PtInRect(&curr_file->area, in_point)){
HCURSOR hCur = LoadCursor(NULL, IDC_HAND);
SetCursor(hCur);
list->selected = counter;
break;
}
list->selected = -1;
counter++;
}
}
InvalidateRect(shwnd, NULL, 0);
}
}
void sproc_mouse_click(UINT msg, WPARAM wParam, LPARAM lParam, file_list * list){
if(msg == WM_LBUTTONDOWN && list->selected != -1){
change_list_dir(main_list, list->index[list->selected]->dir);
InvalidateRect(hwnd, NULL, 1);
}
}
|