-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSizeDialog.cpp
More file actions
468 lines (409 loc) · 10.6 KB
/
Copy pathSizeDialog.cpp
File metadata and controls
468 lines (409 loc) · 10.6 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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#include <crtdefs.h>
#include <crtdbg.h>
#include <string.h>
#include <malloc.h>
#include <tchar.h>
#include <Windows.h>
#include <CommCtrl.h>
#include "misc.h"
#include "resource.h"
#include "SizeDialog.h"
SizeDialog::SizeDialog(HWND hwnd)
{
Debug(TEXT("SizeDialog: Initialize"));
p_ref = 1;
p_dialog = hwnd;
p_parent = (HWND)GetWindowLongPtr(p_dialog, GWLP_HWNDPARENT);
}
SizeDialog::~SizeDialog()
{
Debug(TEXT("SizeDialog: Destroy"));
_ASSERT(p_ref == 0);
if(p_caption_text)
{
free(p_caption_text);
}
}
intptr_t SizeDialog::AddRef()
{
return InterlockedIncrement(&p_ref);
}
intptr_t SizeDialog::Release()
{
InterlockedDecrement(&p_ref);
if(p_ref == 0)
{
delete this;
}
return p_ref;
}
INT_PTR CALLBACK SizeDialog::DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
SizeDialog * _this;
INT_PTR r;
#if defined(ZEROFILL_LOCAL_VARS)
_this = NULL;
r = 0;
#endif
switch(uMsg)
{
case WM_DESTROY:
_this = (SizeDialog *)GetProp(hwndDlg, TEXT("DialogClassPtr"));
_ASSERT(_this != NULL);
_this->do_destroy();
r = TRUE;
break;
case WM_NOTIFY:
_this = (SizeDialog *)GetProp(hwndDlg, TEXT("DialogClassPtr"));
_ASSERT(_this != NULL);
_this->do_notify((NMHDR *)lParam);
r = TRUE;
break;
case WM_INITDIALOG:
// プロパティシートとして作成した場合、LPARAMはPROPSHEETPAGE構造体となる。
_this = new SizeDialog(hwndDlg);
_ASSERT(_this != NULL);
_this->do_initdialog(((const PROPSHEETPAGE *)lParam)->lParam);
r = TRUE;
break;
case WM_COMMAND:
_this = (SizeDialog *)GetProp(hwndDlg, TEXT("DialogClassPtr"));
if(_this)
{
_this->do_command(LOWORD(wParam), HIWORD(wParam), (HWND)lParam);
r = TRUE;
}
else
{
r = FALSE;
}
break;
default:
r = FALSE;
break;
}
return r;
}
void SizeDialog::do_destroy()
{
Release();
RemoveProp(p_dialog, TEXT("DialogClassPtr"));
}
void SizeDialog::do_initdialog(LPARAM l)
{
LRESULT item;
int ival;
ENUMMONITORPARAM mp;
RECT r;
bool client_metrics;
#if defined(ZEROFILL_LOCAL_VARS)
item = 0;
ival = 0;
memset(&mp, 0, sizeof(ENUMMONITORPARAM));
memset(&r, 0, sizeof(RECT));
client_metrics = false;
#endif
SetProp(p_dialog,TEXT("DialogClassPtr"), (HANDLE)this);
p_do_apply = false;
p_target = ((const SIZEDIALOGPARAM *)l)->target;
p_caption = GetDlgItem(p_dialog, IDC_CAPTION);
p_xpos = GetDlgItem(p_dialog, IDC_SPIN_XPOS);
p_ypos = GetDlgItem(p_dialog, IDC_SPIN_YPOS);
p_width = GetDlgItem(p_dialog, IDC_SPIN_WIDTH);
p_height = GetDlgItem(p_dialog, IDC_SPIN_HEIGHT);
p_monitor = GetDlgItem(p_dialog, IDC_DISPLAY);
p_callback = ((const SIZEDIALOGPARAM *)l)->callback;
p_callback_param = ((const SIZEDIALOGPARAM *)l)->callback_param;
SendMessage(p_caption, EM_LIMITTEXT, 1023, 0);
SendDlgItemMessage(p_dialog, IDC_XPOS,EM_LIMITTEXT, 15, 0);
SendDlgItemMessage(p_dialog, IDC_YPOS,EM_LIMITTEXT, 15, 0);
SendDlgItemMessage(p_dialog, IDC_WIDTH,EM_LIMITTEXT, 15, 0);
SendDlgItemMessage(p_dialog, IDC_HEIGHT,EM_LIMITTEXT, 15, 0);
mp.combobox = p_monitor;
item = SendMessage(p_monitor, CB_ADDSTRING, 0, (LPARAM)TEXT("すべてのモニタ"));
SendMessage(p_monitor, CB_SETITEMDATA, item, 0);
mp.current_monitor = MonitorFromWindow(p_target, MONITOR_DEFAULTTONEAREST);
EnumDisplayMonitors(NULL, NULL, enum_monitors, (LPARAM)&mp);
/* 仮想画面の幅と左端の座標 */
ival = GetSystemMetrics(SM_CXVIRTUALSCREEN);
SendMessage(p_xpos, UDM_SETRANGE32, (WPARAM)(intptr_t)-32768, 32767);
SendMessage(p_width, UDM_SETRANGE32, 0, ival);
/* 仮想画面の高さと上端の座標 */
ival = GetSystemMetrics(SM_CYVIRTUALSCREEN);
SendMessage(p_ypos, UDM_SETRANGE32, (WPARAM)(intptr_t)-32768, 32767);
SendMessage(p_height, UDM_SETRANGE32, 0, ival);
client_metrics = ((0x8000 & GetAsyncKeyState(VK_SHIFT)) == 0x8000);
if(client_metrics)
{
GetClientRectAsScreenPos(p_target, &r);
}
else
{
GetWindowRect(p_target, &r);
}
p_pos.x = r.left;
p_pos.y = r.top;
p_size.cx = r.right - r.left;
p_size.cy = r.bottom - r.top;
SendMessage(p_xpos, UDM_SETPOS32, 0, p_pos.x);
SendMessage(p_ypos, UDM_SETPOS32, 0, p_pos.y);
SendMessage(p_width, UDM_SETPOS32, 0, p_size.cx);
SendMessage(p_height, UDM_SETPOS32, 0, p_size.cy);
if(client_metrics)
{
SendDlgItemMessage(p_dialog, IDC_POS_CLIENT, BM_SETCHECK, BST_CHECKED, 0);
SendDlgItemMessage(p_dialog, IDC_SIZE_CLIENT, BM_SETCHECK, BST_CHECKED, 0);
}
p_caption_text = (TCHAR *)calloc(1024, sizeof(TCHAR));
_ASSERT(p_caption_text);
if(p_caption_text)
{
GetWindowText(p_target, p_caption_text, 1024);
SetWindowText(p_caption, p_caption_text);
}
}
void SizeDialog::do_notify(NMHDR * notify_header)
{
POINT pos;
SIZE size;
ULONG flags;
TCHAR * new_caption;
int n;
#if defined(ZEROFILL_LOCAL_VARS)
memset(&pos, 0, sizeof(POINT));
memset(&size, 0, sizeof(SIZE));
//flags = 0;
new_caption = NULL;
n = 0;
#endif
flags = 0;
if(notify_header->code == PSN_APPLY)
{
if(p_do_apply)
{
pos.x = (int)SendMessage(p_xpos, UDM_GETPOS32, 0, 0);
pos.y = (int)SendMessage(p_ypos, UDM_GETPOS32, 0, 0);
size.cx = (int)SendMessage(p_width, UDM_GETPOS32, 0, 0);
size.cy = (int)SendMessage(p_height, UDM_GETPOS32, 0, 0);
if(IsDlgButtonChecked(p_dialog, IDC_POS_CLIENT) == BST_CHECKED)
{
flags |= SDAF_CLIENT_POS;
}
if(IsDlgButtonChecked(p_dialog, IDC_SIZE_CLIENT) == BST_CHECKED)
{
flags |= SDAF_CLIENT_SIZE;
}
n = GetWindowTextLength(p_caption);
if(n > 0)
{
new_caption = (TCHAR *)calloc(++n, sizeof(TCHAR));;
_ASSERT(new_caption);
if(new_caption)
{
GetWindowText(p_caption, new_caption, n);
if(_tcscmp(new_caption, p_caption_text))
{
flags |= SDAF_SETCAPTION;
}
}
}
else
new_caption = nullptr;
if(p_callback)
{
(*p_callback)(p_callback_param, flags, &pos, &size, new_caption);
}
p_pos = pos;
p_size = size;
if(new_caption)
{
_tcsncpy_s(p_caption_text, 1024, new_caption, _TRUNCATE);
free(new_caption);
}
p_do_apply = false;
}
}
}
void SizeDialog::do_command(WORD id, WORD code, HWND window_from)
{
LRESULT item;
HMONITOR mon;
RECT r;
ULONG flags;
TCHAR * buffer;
TCHAR minibuffer[16];
int val;
int val_o;
#if defined(ZEROFILL_LOCAL_VARS)
item = 0;
HMONITOR = NULL;
memset(&r, 0, sizeof(RECT));
//flags = 0;
buffer = NULL;
memset(minibuffer, 0, sizeof(TCHAR) * 16);
val = 0;
val_o = 0;
#endif
flags = 0;
if(code == EN_CHANGE)
{
switch(id)
{
case IDC_CAPTION:
buffer = (TCHAR *)calloc(1024, sizeof(TCHAR));
if(buffer)
{
GetWindowText(window_from, buffer, 1024);
p_do_apply = _tcscmp(buffer, p_caption_text) != 0;
free(buffer);
}
break;
case IDC_XPOS:
val_o = p_pos.x;
goto getval;
case IDC_YPOS:
val_o = p_pos.y;
goto getval;
case IDC_WIDTH:
val_o = p_size.cx;
goto getval;
case IDC_HEIGHT:
val_o = p_size.cy;
goto getval;
getval:
GetWindowText(window_from, minibuffer, 16);
val = _ttoi(minibuffer);
p_do_apply = (val != val_o);
break;
}
if(p_do_apply)
{
SendMessage(p_parent, PSM_CHANGED, (WPARAM)p_dialog, 0);
}
else
{
SendMessage(p_parent, PSM_UNCHANGED, (WPARAM)p_dialog, 0);
}
return;
}
switch(id)
{
case IDC_UPLEFT:
flags = CWPF_UP | CWPF_LEFT;
break;
case IDC_UP:
flags = CWPF_UP;
break;
case IDC_UPRIGHT:
flags = CWPF_RIGHT | CWPF_UP;
break;
case IDC_LEFT:
flags = CWPF_LEFT;
break;
case IDC_CENTER:
flags = CWPF_MIDDLE | CWPF_CENTER;
break;
case IDC_RIGHT:
flags = CWPF_RIGHT;
break;
case IDC_DOWNLEFT:
flags = CWPF_DOWN | CWPF_LEFT;
break;
case IDC_DOWN:
flags = CWPF_DOWN;
break;
case IDC_DOWNRIGHT:
flags = CWPF_DOWN | CWPF_RIGHT;
break;
case IDC_FULLWIDTH:
flags = CWPF_FULLWIDTH;
break;
case IDC_FULLHEIGHT:
flags = CWPF_FULLHEIGHT;
break;
}
r.left = (LONG)SendMessage(p_xpos, UDM_GETPOS32, 0, 0);
r.top = (LONG)SendMessage(p_ypos, UDM_GETPOS32, 0, 0);
r.right = (LONG)SendMessage(p_width, UDM_GETPOS32, 0, 0);
r.bottom = (LONG)SendMessage(p_height, UDM_GETPOS32, 0, 0);
item = SendMessage(p_monitor, CB_GETCURSEL, 0, 0);
mon = (HMONITOR)SendMessage(p_monitor, CB_GETITEMDATA, item, 0);
calc_window_position(flags, mon, &r);
SendMessage(p_xpos, UDM_SETPOS32, 0, r.left);
SendMessage(p_ypos, UDM_SETPOS32, 0, r.top);
SendMessage(p_width, UDM_SETPOS32, 0, r.right);
SendMessage(p_height, UDM_SETPOS32, 0, r.bottom);
}
void SizeDialog::calc_window_position(ULONG flags, HMONITOR monitor, RECT * r)
{
MONITORINFO mi;
#if defined(ZEROFILL_LOCAL_VARS)
mi = NULL;
#endif
if(monitor)
{
mi.cbSize = sizeof(MONITORINFO);
GetMonitorInfo(monitor, &mi);
}
else
{
mi.rcWork.left = GetSystemMetrics(SM_XVIRTUALSCREEN);
mi.rcWork.top = GetSystemMetrics(SM_YVIRTUALSCREEN);
mi.rcWork.right = GetSystemMetrics(SM_CXVIRTUALSCREEN) - mi.rcWork.left;
mi.rcWork.bottom = GetSystemMetrics(SM_CYVIRTUALSCREEN) - mi.rcWork.top;
}
if(flags & CWPF_FULLWIDTH)
{
r->left = mi.rcWork.left;
r->right = mi.rcWork.right;
}
if(flags & CWPF_FULLHEIGHT)
{
r->top = mi.rcWork.top;
r->bottom = mi.rcWork.bottom;
}
switch(flags & CWPF_YPOS_MASK)
{
case CWPF_UP:
r->top = mi.rcWork.top;
break;
case CWPF_MIDDLE:
r->top = ((mi.rcWork.bottom - mi.rcWork.top) - r->bottom) / 2;
break;
case CWPF_DOWN:
r->top = mi.rcWork.bottom - r->bottom;
break;
}
switch(flags & CWPF_XPOS_MASK)
{
case CWPF_LEFT:
r->left = mi.rcWork.left;
break;
case CWPF_CENTER:
r->left = ((mi.rcWork.right - mi.rcWork.left) - r->right) / 2;
break;
case CWPF_RIGHT:
r->left = mi.rcWork.right - r->right;
break;
}
}
BOOL CALLBACK SizeDialog::enum_monitors(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
{
UNREFERENCED_PARAMETER(hdcMonitor);
UNREFERENCED_PARAMETER(lprcMonitor);
LRESULT item;
MONITORINFOEX mi;
#if defined(ZEROFILL_LOCAL_VARS)
item = 0;
memset(&mi, 0, sizeof(MONITORINFOEX));
#endif
mi.cbSize = sizeof(MONITORINFOEX);
GetMonitorInfo(hMonitor, (LPMONITORINFO)&mi);
item = SendMessage(((const ENUMMONITORPARAM *)dwData)->combobox, CB_ADDSTRING, 0, (LPARAM)mi.szDevice);
SendMessage(((const ENUMMONITORPARAM *)dwData)->combobox, CB_SETITEMDATA, item, (LPARAM)hMonitor);
if(hMonitor == ((const ENUMMONITORPARAM *)dwData)->current_monitor)
{
SendMessage(((const ENUMMONITORPARAM *)dwData)->combobox, CB_SETCURSEL, item, 0);
}
return TRUE;
}