forked from ckw-mod/ckw-mod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmisc.cpp
472 lines (406 loc) · 11 KB
/
misc.cpp
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
469
470
471
472
/*-----------------------------------------------------------------------------
* File: misc.cpp
*-----------------------------------------------------------------------------
* Copyright (c) 2004-2005 Kazuo Ishii <[email protected]>
* - original version
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*---------------------------------------------------------------------------*/
#include <string>
#include "ckw.h"
#include "rsrc.h"
#include "option.h"
BOOL init_options(ckOpt& opt);
static void __write_console_input(LPCWSTR str, DWORD length)
{
if(!str || !length) return;
INPUT_RECORD *p, *buf;
DWORD i = 0;
p = buf = new INPUT_RECORD[ length ];
for( ; i < length ; i++, p++) {
p->EventType = KEY_EVENT;
p->Event.KeyEvent.bKeyDown = TRUE;
p->Event.KeyEvent.wRepeatCount = 1;
p->Event.KeyEvent.wVirtualKeyCode = 0;
p->Event.KeyEvent.wVirtualScanCode = 0;
p->Event.KeyEvent.uChar.UnicodeChar = 0;
p->Event.KeyEvent.dwControlKeyState = 0;
if(*str == '\r') {
str++;
length--;
}
if(*str == '\n') {
p->Event.KeyEvent.wVirtualKeyCode = VK_RETURN;
str++;
} else {
p->Event.KeyEvent.uChar.UnicodeChar = *str++;
}
}
WriteConsoleInput(gStdIn, buf, length, &length);
delete [] buf;
}
/*----------*/
void copyChar(wchar_t*& p, CHAR_INFO* src, SHORT start, SHORT end, bool ret)
{
CHAR_INFO* pend = src + end;
CHAR_INFO* test = src + start;
CHAR_INFO* last = test-1;
/* search last char */
for( ; test <= pend ; test++) {
if(test->Char.UnicodeChar > 0x20)
last = test;
}
/* copy */
for(test = src+start ; test <= last ; test++) {
if(!(test->Attributes & COMMON_LVB_TRAILING_BYTE))
*p++ = test->Char.UnicodeChar;
}
if(ret && last < pend) {
*p++ = L'\r';
*p++ = L'\n';
}
*p = 0;
}
/*----------*/
void onPasteFromClipboard(HWND hWnd)
{
bool result = true;
HANDLE hMem;
wchar_t *ptr;
if(! IsClipboardFormatAvailable(CF_UNICODETEXT))
return;
if(!OpenClipboard(hWnd)) {
Sleep(10);
if(!OpenClipboard(hWnd))
return;
}
hMem = GetClipboardData(CF_UNICODETEXT);
if(!hMem)
result = false;
if(result && !(ptr = (wchar_t*)GlobalLock(hMem)))
result = false;
if(result) {
__write_console_input(ptr, (DWORD)wcslen(ptr));
GlobalUnlock(hMem);
}
CloseClipboard();
}
/*----------*/
/* (craftware) */
void copyStringToClipboard( HWND hWnd, const wchar_t * str )
{
size_t length = wcslen(str) +1;
HANDLE hMem;
wchar_t* ptr;
bool result = true;
hMem = GlobalAlloc(GMEM_MOVEABLE, sizeof(wchar_t) * length);
if(!hMem) result = false;
if(result && !(ptr = (wchar_t*) GlobalLock(hMem))) {
result = false;
}
if(result) {
memcpy(ptr, str, sizeof(wchar_t) * length);
GlobalUnlock(hMem);
}
if(result && !OpenClipboard(hWnd)) {
Sleep(10);
if(!OpenClipboard(hWnd))
result = false;
}
if(result) {
if(!EmptyClipboard() ||
!SetClipboardData(CF_UNICODETEXT, hMem))
result = false;
CloseClipboard();
}
if(!result && hMem) {
GlobalFree(hMem);
}
}
/*----------*/
/* (craftware) */
wchar_t * getAllString()
{
int nb;
nb = gCSI->dwSize.X * gCSI->dwSize.Y;
COORD size = { gCSI->dwSize.X, 1 };
CHAR_INFO* work = new CHAR_INFO[ gCSI->dwSize.X ];
wchar_t* buffer = new wchar_t[ nb ];
wchar_t* wp = buffer;
COORD pos = { 0,0 };
SMALL_RECT sr = { 0, 0, gCSI->dwSize.X-1, 0 };
*wp = 0;
for( int y=0 ; y<gCSI->dwSize.Y ; ++y )
{
sr.Top = sr.Bottom = y;
ReadConsoleOutput_Unicode(gStdOut, work, size, pos, &sr);
copyChar( wp, work, 0, gCSI->dwSize.X-1 );
}
delete [] work;
return(buffer);
}
/*----------*/
/* (craftware) */
void copyAllStringToClipboard(HWND hWnd)
{
wchar_t* str = getAllString();
if(!str) return;
std::wstring s = str;
// skip empty line
size_t begin = s.find_first_not_of(L"\r\n");
size_t end = s.find_last_not_of(L"\r\n");
if(begin!=s.npos && end!=s.npos)
{
s = s.substr( begin, end+1-begin );
}
copyStringToClipboard( hWnd, s.c_str() );
delete [] str;
}
/*----------*/
void onDropFile(HDROP hDrop)
{
DWORD i, nb, len;
wchar_t wbuf[MAX_PATH+32];
wchar_t* wp;
nb = DragQueryFile(hDrop, (DWORD)-1, NULL, 0);
for(i = 0 ; i < nb ; i++) {
len = DragQueryFile(hDrop, i, NULL, 0);
if(len < 1 || len > MAX_PATH)
continue;
wp = wbuf + 1;
if(! DragQueryFile(hDrop, i, wp, MAX_PATH))
continue;
wp[len] = 0;
while(*wp > 0x20) wp++;
if(*wp) {
wp = wbuf;
len++;
wp[0] = wp[len++] = L'\"';
}
else {
wp = wbuf + 1;
}
wp[len++] = L' ';
__write_console_input(wp, len);
}
DragFinish(hDrop);
}
/*----------*/
INT_PTR CALLBACK AboutDlgProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
{
switch(msg) {
case WM_INITDIALOG:
{
HWND hEdit = GetDlgItem(hWnd, IDC_EDIT1);
SetWindowText(hEdit,
L"This program is free software; you can redistribute it and/or\r\n"
L"modify it under the terms of the GNU General Public License\r\n"
L"as published by the Free Software Foundation; either version 2\r\n"
L"of the License, or (at your option) any later version.\r\n"
L"\r\n"
L"This program is distributed in the hope that it will be useful,\r\n"
L"but WITHOUT ANY WARRANTY; without even the implied warranty of\r\n"
L"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\r\n"
L"See the GNU General Public License for more details.\r\n"
L"\r\n"
L"You should have received a copy of the GNU General Public License\r\n"
L"along with this program; if not, write to the Free Software Foundation, Inc.,\r\n"
L" 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA."
);
}
return(TRUE);
case WM_COMMAND:
switch(LOWORD(wp)) {
case IDOK:
case IDCANCEL:
EndDialog(hWnd, 0);
return(TRUE);
}
}
return(FALSE);
}
/*----------*/
void sysmenu_init_topmost(HWND hWnd, HMENU hMenu);
void sysmenu_init_subconfig(HWND hWnd, HMENU hMenu);
void changeStateTopMostMenu(HWND hWnd, HMENU hMenu);
void sysmenu_init(HWND hWnd)
{
MENUITEMINFO mii;
HMENU hMenu = GetSystemMenu(hWnd, FALSE);
memset(&mii, 0, sizeof(mii));
mii.cbSize = sizeof(mii);
mii.fMask = MIIM_TYPE | MIIM_ID;
mii.fType = MFT_STRING;
mii.wID = IDM_COPYALL;
mii.dwTypeData = L"Copy All(&C)";
mii.cch = (UINT) wcslen(mii.dwTypeData);
InsertMenuItem(hMenu, SC_CLOSE, FALSE, &mii);
mii.fType = MFT_STRING;
mii.wID = IDM_NEW;
mii.dwTypeData = L"New (&N)";
mii.cch = (UINT) wcslen(mii.dwTypeData);
InsertMenuItem(hMenu, SC_CLOSE, FALSE, &mii);
sysmenu_init_topmost(hWnd, hMenu);
// sysmenu_init_subconfig(hWnd, hMenu);
mii.fType = MFT_SEPARATOR;
mii.wID = 0;
mii.dwTypeData = 0;
mii.cch = 0;
InsertMenuItem(hMenu, SC_CLOSE, FALSE, &mii);
mii.fType = MFT_STRING;
mii.wID = IDM_ABOUT;
mii.dwTypeData = L"About (&A)";
mii.cch = (UINT) wcslen(mii.dwTypeData);
InsertMenuItem(hMenu, SC_CLOSE, FALSE, &mii);
mii.fType = MFT_SEPARATOR;
mii.wID = 0;
mii.dwTypeData = 0;
mii.cch = 0;
InsertMenuItem(hMenu, SC_CLOSE, FALSE, &mii);
}
void get_directory_path(wchar_t *path)
{
wchar_t *c;
GetModuleFileName(NULL, path, MAX_PATH);
c = wcsrchr(path, L'\\');
if(c) *c = 0;
}
void sysmenu_init_topmost(HWND hWnd, HMENU hMenu)
{
MENUITEMINFO mii;
memset(&mii, 0, sizeof(mii));
mii.cbSize = sizeof(mii);
mii.fMask = MIIM_TYPE | MIIM_ID | MIIM_CHECKMARKS;
mii.fType = MFT_STRING;
mii.wID = IDM_TOPMOST;
mii.dwTypeData = L"TopMost (&T)";
mii.cch = (UINT) wcslen(mii.dwTypeData);
InsertMenuItem(hMenu, SC_CLOSE, FALSE, &mii);
changeStateTopMostMenu(hWnd,hMenu);
}
void sysmenu_init_subconfig(HWND hWnd, HMENU hMenu)
{
MENUITEMINFO mii;
HMENU hSubMenu = CreatePopupMenu();
memset(&mii, 0, sizeof(mii));
mii.cbSize = sizeof(mii);
mii.fMask = MIIM_TYPE | MIIM_ID;
wchar_t path[MAX_PATH+1];
get_directory_path(path);
wcscat_s(path, L"\\*.cfg");
WIN32_FIND_DATA fd;
memset(&fd, 0, sizeof(fd));
HANDLE hFile = FindFirstFile(path, &fd);
//MessageBox(hWnd, path, L"", MB_OK);
if (hFile != INVALID_HANDLE_VALUE)
{
for(int i = 0;;i++)
{
mii.fType = MFT_STRING;
mii.wID = IDM_CONFIG_SELECT_1 + i;
mii.dwTypeData = fd.cFileName;
mii.cch = (UINT) wcslen(mii.dwTypeData);
InsertMenuItem(hSubMenu, 0, TRUE, &mii);
if(FindNextFile(hFile, &fd) == 0) { break; }
}
FindClose(hFile);
}
mii.fMask |= MIIM_SUBMENU;
mii.fType = MFT_STRING;
mii.wID = IDM_CONFIG_SELECT;
mii.hSubMenu = hSubMenu;
mii.dwTypeData = L"Config (&O)";
mii.cch = (UINT) wcslen(mii.dwTypeData);
InsertMenuItem(hMenu, SC_CLOSE, FALSE, &mii);
}
void reloadConfig(wchar_t *path)
{
char filepath[MAX_PATH+1];
wcstombs(filepath, path, MAX_PATH);
ckOpt opt;
opt.setFile(filepath);
init_options(opt);
}
BOOL onConfigMenuCommand(HWND hWnd, DWORD id)
{
wchar_t path[MAX_PATH+1];
get_directory_path(path);
MENUITEMINFO mii;
memset(&mii, 0, sizeof(mii));
mii.cbSize = sizeof(mii);
mii.fMask = MIIM_TYPE | MIIM_ID;
mii.fType = MFT_STRING;
HMENU hMenu = GetSystemMenu(hWnd, FALSE);
if (GetMenuItemInfo(hMenu, id, 0, &mii))
{
wchar_t sz[MAX_PATH+1], filepath[MAX_PATH+1];
mii.dwTypeData = sz;
mii.cch++;
GetMenuItemInfo(hMenu, id, 0, &mii);
wsprintf(filepath, L"%s\\%s", path, sz);
/* need to open file and update config here */
MessageBox(hWnd, filepath, L"", MB_OK);
reloadConfig(filepath);
}
return(TRUE);
}
BOOL onTopMostMenuCommand(HWND hWnd)
{
HMENU hMenu = GetSystemMenu(hWnd, FALSE);
UINT uState = GetMenuState( hMenu, IDM_TOPMOST, MF_BYCOMMAND);
DWORD dwExStyle = GetWindowLong(hWnd,GWL_EXSTYLE);
if( uState & MFS_CHECKED )
{
SetWindowPos(hWnd, HWND_NOTOPMOST,NULL,NULL,NULL,NULL,SWP_NOMOVE | SWP_NOSIZE);
}else{
SetWindowPos(hWnd, HWND_TOPMOST,NULL,NULL,NULL,NULL,SWP_NOMOVE | SWP_NOSIZE);
}
changeStateTopMostMenu(hWnd, hMenu);
return(TRUE);
}
void changeStateTopMostMenu(HWND hWnd,HMENU hMenu)
{
DWORD dwExStyle = GetWindowLong(hWnd,GWL_EXSTYLE);
if ((dwExStyle & WS_EX_TOPMOST) == 0) {
CheckMenuItem(hMenu, IDM_TOPMOST, MF_BYCOMMAND | MFS_UNCHECKED);
} else {
CheckMenuItem(hMenu, IDM_TOPMOST, MF_BYCOMMAND | MFS_CHECKED);
}
}
/*----------*/
BOOL onSysCommand(HWND hWnd, DWORD id)
{
switch(id) {
case IDM_COPYALL:
copyAllStringToClipboard(hWnd);
return(TRUE);
case IDM_ABOUT:
DialogBox(GetModuleHandle(NULL),
MAKEINTRESOURCE(IDD_DIALOG1),
hWnd,
AboutDlgProc);
return(TRUE);
case IDM_NEW:
makeNewWindow();
return(TRUE);
case IDM_TOPMOST:
return onTopMostMenuCommand(hWnd);
}
if(IDM_CONFIG_SELECT < id && id <= IDM_CONFIG_SELECT_MAX) {
return onConfigMenuCommand(hWnd, id);
}
return(FALSE);
}
/* EOF */