-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwbits.cpp
More file actions
405 lines (342 loc) · 9.89 KB
/
Copy pathwbits.cpp
File metadata and controls
405 lines (342 loc) · 9.89 KB
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#if defined(_M_IX86)
#error 32bit OSはさっさと滅べ
#endif
#define OEMRESOURCE
#include "wbits.h"
#include <tchar.h>
#include <commctrl.h>
#include <objbase.h>
#if defined(THREADED)
#include <process.h>
#endif
#include "resource.h"
#include "myedbox_sc.h"
#include "misc.h"
MainWindow::MainWindow(HINSTANCE instance, int show_cmd)
{
this->window = nullptr;
#if defined(THREADED)
this->thread = nullptr;
#endif
this->instance = instance;
this->show_window = show_cmd;
}
MainWindow::~MainWindow()
{
if(this->IsValidInstance())
{
::SendMessage(this->window, WM_CLOSE, 0, 0);
}
#if defined(THREADED)
this->WaitForClose();
#endif
}
void MainWindow::Start()
{
#if defined(THREADED)
unsigned int tid;
this->thread = reinterpret_cast<HANDLE>(::_beginthreadex(nullptr, 0u, MainWindow::thread_proc, this, 0, &tid));
#else
MainWindow::thread_proc(this);
#endif
}
int MainWindow::WaitForClose()
{
#if defined(THREADED)
DWORD quit_code = 0xFFFFFFFFul;
if(this->thread)
{
::WaitForSingleObject(this->thread, INFINITE);
::GetExitCodeThread(this->thread, &quit_code);
::CloseHandle(this->thread);
this->thread = nullptr;
}
return quit_code;
#else
return 0;
#endif
}
bool MainWindow::create(const CREATESTRUCT * create_param)
{
::SetWindowLongPtr(this->window, 0, reinterpret_cast<LONG_PTR>(this));
{
RECT rect;
::GetClientRect(this->window, &rect);
try
{
this->job_list = new JobList(this, 1, &rect);
}
catch(...)
{
return false;
}
try
{
this->job_status = new JobStatus(this, 2);
}
catch(...)
{
return false;
}
}
this->job_list->UpdateList();
this->menu = ::GetMenu(this->window);
::SetTimer(this->window, 37564, 1000, nullptr);
return true;
}
void MainWindow::destroy()
{
::KillTimer(this->window, 37564);
delete this->job_list;
delete this->job_status;
::PostQuitMessage(0);
}
void MainWindow::size(WORD width, WORD height)
{
this->job_status->SendResizeMessage(width, height);
this->current_size.cx = width;
this->current_size.cy = height - this->job_status->GetHeight();
this->job_list->Resize(&this->current_size);
}
void MainWindow::set_focus()
{
::SetFocus(this->job_list->GetWindow());
}
void MainWindow::system_color_change()
{
this->job_list->UpdateWindowStyle();
}
void MainWindow::get_min_max_info(MINMAXINFO * min_max_info)
{
// ウィンドウが生成されるタイミングではthisはnullptrになっている。
if(!this)
{
return;
}
min_max_info->ptMinTrackSize.x = 800l;
min_max_info->ptMinTrackSize.y = 192l;
}
void MainWindow::draw_item(const DRAWITEMSTRUCT * draw_item)
{
if((draw_item->CtlType == ODT_LISTVIEW) && (draw_item->CtlID == 1))
{
this->job_list->DrawListItem(this->job_list, draw_item);
}
}
void MainWindow::notify(const NMHDR * notification_header)
{
auto ctlid = notification_header->idFrom;
if(ctlid == 0)
{
if((notification_header->code <= HDN_FIRST) && (notification_header->code >= HDN_LAST))
{
auto parent_window = ::GetParent(notification_header->hwndFrom);
ctlid = ::GetDlgCtrlID(parent_window);
}
}
if(ctlid == 1)
{
this->job_list->Notify(const_cast<NMHDR*>(notification_header));
}
}
void MainWindow::command(WORD id, WORD type, HWND invoke_from)
{
if(!type && !invoke_from)
{
switch(id)
{
case IDC_JOBPROPERTY:
this->job_list->ShowProperty();
break;
case IDC_COMPLETEJOB:
if(::MessageBox(window, TEXT("完了させるとダウンロード済みの一時ファイルが指定したファイル名に変更され使用可能になります。ジョブを完了しますか?"), TEXT("確認"), MB_ICONQUESTION | MB_YESNO) == IDYES)
{
this->job_list->CompleteSelectedJobs();
}
break;
case IDC_ABORTJOB:
if(::MessageBox(window, TEXT("中止するとダウンロード済みのファイルが削除されます。ジョブを中止しますか?"), TEXT("確認"), MB_ICONWARNING | MB_YESNO | MB_DEFBUTTON2) == IDYES)
{
this->job_list->AbortSelectedJobs();
}
break;
case IDC_PAUSEJOB:
this->job_list->PauseSelectedJobs();
break;
case IDC_RESUMEJOB:
this->job_list->ResumeSelectedJobs();
break;
}
}
}
void MainWindow::timer(UINT_PTR id)
{
if(static_cast<unsigned int>(id) == 37564u)
{
this->job_list->UpdateList();
}
}
void MainWindow::enter_menu_loop()
{
auto selected = this->job_list->GetSelectedCount();
auto state = MF_BYCOMMAND | (selected ? MF_ENABLED : MF_GRAYED);
::EnableMenuItem(this->menu, IDC_COMPLETEJOB, state);
::EnableMenuItem(this->menu, IDC_ABORTJOB, state);
::EnableMenuItem(this->menu, IDC_PAUSEJOB, state);
::EnableMenuItem(this->menu, IDC_RESUMEJOB, state);
//EnableMenuItem(this->menu, IDC_JOBPROPERTY, MF_BYCOMMAND | ((selected == 1) ? MF_ENABLED : MF_GRAYED));
}
void MainWindow::theme_changed()
{
this->job_list->UpdateWindowStyle();
}
LRESULT CALLBACK MainWindow::main_window_proc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
auto _this = static_cast<MainWindow*>(nullptr);
auto r = (LRESULT)0;
if(uMsg != WM_CREATE)
{
_this = reinterpret_cast<MainWindow*>(::GetWindowLongPtr(hwnd, 0));
}
switch(uMsg)
{
case WM_CREATE:
{
auto create_struct = reinterpret_cast<const CREATESTRUCT*>(lParam);
_this = reinterpret_cast<MainWindow*>(create_struct->lpCreateParams);
_this->window = hwnd;
r = _this->create(create_struct) ? 0 : -1;
}
break;
case WM_DESTROY:
_this->destroy();
break;
case WM_SIZE:
_this->size(LOWORD(lParam), HIWORD(lParam));
break;
case WM_SETFOCUS:
_this->set_focus();
break;
//case WM_SYSCOLORCHANGE:
// _this->system_color_change();
// break;
case WM_GETMINMAXINFO:
_this->get_min_max_info(reinterpret_cast<MINMAXINFO*>(lParam));
break;
case WM_DRAWITEM:
_this->draw_item(reinterpret_cast<const DRAWITEMSTRUCT*>(lParam));
break;
//case WM_MEASUREITEM:
// if((((const MEASUREITEMSTRUCT*)l)->CtlType == ODT_LISTVIEW) && (((const MEASUREITEMSTRUCT*)l)->CtlID == 1))
// xthis->JobListWindow->MeasureListItem((MEASUREITEMSTRUCT*)l);
// break;
case WM_NOTIFY:
_this->notify(reinterpret_cast<const NMHDR*>(lParam));
break;
case WM_COMMAND:
_this->command(LOWORD(wParam), HIWORD(wParam), reinterpret_cast<HWND>(lParam));
break;
case WM_TIMER:
_this->timer(static_cast<UINT_PTR>(lParam));
break;
case WM_ENTERMENULOOP:
_this->enter_menu_loop();
break;
case WM_THEMECHANGED:
_this->theme_changed();
break;
default:
r = ::DefWindowProc(hwnd, uMsg, wParam, lParam);
break;
}
return r;
}
unsigned int __stdcall MainWindow::thread_proc(void * argument)
{
::CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED); //COINIT_MULTITHREADED
auto _this = static_cast<MainWindow*>(argument);
_this->window = ::CreateWindow(MAINWINDOWCLASS, MAINWINDOWTITLE, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, nullptr, nullptr, _this->instance, _this);
if(!_this->window)
{
#if defined(THREADED)
_this->thread = nullptr;
#endif
::CoUninitialize();
return 1;
}
::ShowWindow(_this->window, _this->show_window);
auto quit_code = MainWindow::do_message_loop();
_this->window = nullptr;
::CoUninitialize();
return static_cast<unsigned int>(quit_code);
}
int MainWindow::do_message_loop()
{
MSG msg;
// BOOLは符号付き整数なのでこのような比較でも問題はない
while(::GetMessage(&msg, nullptr, 0, 0) > 0)
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
return static_cast<int>(msg.wParam);
}
const WNDCLASSEX MainWindow::main_window_class_t = {
/* cbSize */ sizeof(WNDCLASSEX),
/* style */ 0,
/* lpfnWndProc */ MainWindow::main_window_proc,
/* cbClsExtra */ 0,
/* cbWndExtra */ sizeof(void*), // ポインタを格納する領域を確保する
/* hInstance */ nullptr,
/* hIcon */ nullptr,
/* hCursor */ nullptr,
/* hbrBackground */ (HBRUSH)(1 + COLOR_3DFACE),
/* lpszMenuName */ MAKEINTRESOURCE(IDM_MAIN),
/* lpszClassName */ MAINWINDOWCLASS,
/* hIconSm */ 0 };
extern "C" unsigned __stdcall main_window_thread(void * arg)
{
auto window = new MainWindow(static_cast<const MAINWINDOWSTRUCT*>(arg)->Instance, static_cast<const MAINWINDOWSTRUCT*>(arg)->ShowWindow);
window->Start();
auto exit_code = window->WaitForClose();
delete window;
return exit_code;
}
extern "C" void register_main_window_class(HINSTANCE instance, const HICON *icons)
{
auto main_window_class = MainWindow::main_window_class_t;
main_window_class.hInstance = instance;
main_window_class.hIcon = icons[0];
main_window_class.hCursor = static_cast<HCURSOR>(::LoadImage(nullptr, MAKEINTRESOURCE(OCR_NORMAL), IMAGE_CURSOR, ::GetSystemMetrics(SM_CXCURSOR), ::GetSystemMetrics(SM_CYCURSOR), LR_SHARED));
main_window_class.hIconSm = icons[1];
::RegisterClassEx(&main_window_class);
}
extern "C" int WINAPI _tWinMain(HINSTANCE instance,HINSTANCE x,PTSTR command,int show)
{
static const INITCOMMONCONTROLSEX common_control_descriptor = {
/* dwSize */ sizeof(INITCOMMONCONTROLSEX),
/* dwICC */ ICC_LISTVIEW_CLASSES};
// アイコンハンドル(0=大きいアイコン、1=小さいアイコン)
HICON icons[2];
MAINWINDOWSTRUCT main_st;
#if defined(_M_IX86)
*((char*)nullptr) = 0;
#endif
::InitCommonControlsEx(&common_control_descriptor);
::prepare_editbox(instance);
// アイコンをリソースから読み込む
// Windows95、WindowsNT4以降では小さいアイコンも読み込む必要がある
icons[0] = static_cast<HICON>(::LoadImage(instance, MAKEINTRESOURCE(IDI_MAIN), IMAGE_ICON, ::GetSystemMetrics(SM_CXICON), ::GetSystemMetrics(SM_CYICON), 0));
icons[1] = static_cast<HICON>(::LoadImage(instance, MAKEINTRESOURCE(IDI_MAIN), IMAGE_ICON, ::GetSystemMetrics(SM_CXSMICON), ::GetSystemMetrics(SM_CYSMICON), 0));
::register_main_window_class(instance, icons);
main_st.Instance = instance;
main_st.ShowWindow = show;
auto quit_code = static_cast<int>(::main_window_thread(&main_st));
// 後始末
::UnregisterClass(MAINWINDOWCLASS, instance);
// アイコンの削除
::DestroyIcon(icons[1]);
::DestroyIcon(icons[0]);
::CoUninitialize();
return quit_code;
}