1 /*-----------------------------------------------------------------------------
3 *-----------------------------------------------------------------------------
4 * Copyright (c) 2004-2005 Kazuo Ishii <k-ishii@wb4.so-net.ne.jp>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 *---------------------------------------------------------------------------*/
28 BOOL init_options(ckOpt& opt);
30 static void __write_console_input(LPCWSTR str, DWORD length)
32 if(!str || !length) return;
34 INPUT_RECORD *p, *buf;
36 p = buf = new INPUT_RECORD[ length ];
38 for( ; i < length ; i++, p++) {
39 p->EventType = KEY_EVENT;
40 p->Event.KeyEvent.bKeyDown = TRUE;
41 p->Event.KeyEvent.wRepeatCount = 1;
42 p->Event.KeyEvent.wVirtualKeyCode = 0;
43 p->Event.KeyEvent.wVirtualScanCode = 0;
44 p->Event.KeyEvent.uChar.UnicodeChar = 0;
45 p->Event.KeyEvent.dwControlKeyState = 0;
51 p->Event.KeyEvent.wVirtualKeyCode = VK_RETURN;
54 p->Event.KeyEvent.uChar.UnicodeChar = *str++;
58 WriteConsoleInput(gStdIn, buf, length, &length);
63 void copyChar(wchar_t*& p, CHAR_INFO* src, SHORT start, SHORT end, bool ret)
65 CHAR_INFO* pend = src + end;
66 CHAR_INFO* test = src + start;
67 CHAR_INFO* last = test-1;
69 /* search last char */
70 for( ; test <= pend ; test++) {
71 if(test->Char.UnicodeChar > 0x20)
75 for(test = src+start ; test <= last ; test++) {
76 if(!(test->Attributes & COMMON_LVB_TRAILING_BYTE))
77 *p++ = test->Char.UnicodeChar;
79 if(ret && last < pend) {
87 void onPasteFromClipboard(HWND hWnd)
93 if(! IsClipboardFormatAvailable(CF_UNICODETEXT))
95 if(!OpenClipboard(hWnd)) {
97 if(!OpenClipboard(hWnd))
100 hMem = GetClipboardData(CF_UNICODETEXT);
103 if(result && !(ptr = (wchar_t*)GlobalLock(hMem)))
106 __write_console_input(ptr, (DWORD)wcslen(ptr));
114 void copyStringToClipboard( HWND hWnd, const wchar_t * str )
116 size_t length = wcslen(str) +1;
121 hMem = GlobalAlloc(GMEM_MOVEABLE, sizeof(wchar_t) * length);
122 if(!hMem) result = false;
124 if(result && !(ptr = (wchar_t*) GlobalLock(hMem))) {
128 memcpy(ptr, str, sizeof(wchar_t) * length);
131 if(result && !OpenClipboard(hWnd)) {
133 if(!OpenClipboard(hWnd))
137 if(!EmptyClipboard() ||
138 !SetClipboardData(CF_UNICODETEXT, hMem))
142 if(!result && hMem) {
149 wchar_t * getAllString()
153 nb = gCSI->dwSize.X * gCSI->dwSize.Y;
155 COORD size = { gCSI->dwSize.X, 1 };
156 CHAR_INFO* work = new CHAR_INFO[ gCSI->dwSize.X ];
157 wchar_t* buffer = new wchar_t[ nb ];
158 wchar_t* wp = buffer;
160 SMALL_RECT sr = { 0, 0, gCSI->dwSize.X-1, 0 };
164 for( int y=0 ; y<gCSI->dwSize.Y ; ++y )
166 sr.Top = sr.Bottom = y;
167 ReadConsoleOutput_Unicode(gStdOut, work, size, pos, &sr);
168 copyChar( wp, work, 0, gCSI->dwSize.X-1 );
178 void copyAllStringToClipboard(HWND hWnd)
180 wchar_t* str = getAllString();
183 std::wstring s = str;
186 size_t begin = s.find_first_not_of(L"\r\n");
187 size_t end = s.find_last_not_of(L"\r\n");
188 if(begin!=s.npos && end!=s.npos)
190 s = s.substr( begin, end+1-begin );
193 copyStringToClipboard( hWnd, s.c_str() );
199 void onDropFile(HDROP hDrop)
202 wchar_t wbuf[MAX_PATH+32];
205 nb = DragQueryFile(hDrop, (DWORD)-1, NULL, 0);
206 for(i = 0 ; i < nb ; i++) {
207 len = DragQueryFile(hDrop, i, NULL, 0);
208 if(len < 1 || len > MAX_PATH)
211 if(! DragQueryFile(hDrop, i, wp, MAX_PATH))
214 while(*wp > 0x20) wp++;
218 wp[0] = wp[len++] = L'\"';
225 __write_console_input(wp, len);
231 INT_PTR CALLBACK AboutDlgProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
236 HWND hEdit = GetDlgItem(hWnd, IDC_EDIT1);
238 L"This program is free software; you can redistribute it and/or\r\n"
239 L"modify it under the terms of the GNU General Public License\r\n"
240 L"as published by the Free Software Foundation; either version 2\r\n"
241 L"of the License, or (at your option) any later version.\r\n"
243 L"This program is distributed in the hope that it will be useful,\r\n"
244 L"but WITHOUT ANY WARRANTY; without even the implied warranty of\r\n"
245 L"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\r\n"
246 L"See the GNU General Public License for more details.\r\n"
248 L"You should have received a copy of the GNU General Public License\r\n"
249 L"along with this program; if not, write to the Free Software Foundation, Inc.,\r\n"
250 L" 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA."
266 void sysmenu_init_topmost(HWND hWnd, HMENU hMenu);
267 void sysmenu_init_subconfig(HWND hWnd, HMENU hMenu);
268 void changeStateTopMostMenu(HWND hWnd, HMENU hMenu);
270 void sysmenu_init(HWND hWnd)
273 HMENU hMenu = GetSystemMenu(hWnd, FALSE);
275 memset(&mii, 0, sizeof(mii));
276 mii.cbSize = sizeof(mii);
277 mii.fMask = MIIM_TYPE | MIIM_ID;
279 mii.fType = MFT_STRING;
280 mii.wID = IDM_COPYALL;
281 mii.dwTypeData = L"Copy All(&C)";
282 mii.cch = (UINT) wcslen(mii.dwTypeData);
283 InsertMenuItem(hMenu, SC_CLOSE, FALSE, &mii);
285 mii.fType = MFT_STRING;
287 mii.dwTypeData = L"New (&N)";
288 mii.cch = (UINT) wcslen(mii.dwTypeData);
289 InsertMenuItem(hMenu, SC_CLOSE, FALSE, &mii);
291 sysmenu_init_topmost(hWnd, hMenu);
293 // sysmenu_init_subconfig(hWnd, hMenu);
295 mii.fType = MFT_SEPARATOR;
299 InsertMenuItem(hMenu, SC_CLOSE, FALSE, &mii);
301 mii.fType = MFT_STRING;
303 mii.dwTypeData = L"About (&A)";
304 mii.cch = (UINT) wcslen(mii.dwTypeData);
305 InsertMenuItem(hMenu, SC_CLOSE, FALSE, &mii);
307 mii.fType = MFT_SEPARATOR;
311 InsertMenuItem(hMenu, SC_CLOSE, FALSE, &mii);
314 void get_directory_path(wchar_t *path)
317 GetModuleFileName(NULL, path, MAX_PATH);
318 c = wcsrchr(path, L'\\');
322 void sysmenu_init_topmost(HWND hWnd, HMENU hMenu)
326 memset(&mii, 0, sizeof(mii));
327 mii.cbSize = sizeof(mii);
328 mii.fMask = MIIM_TYPE | MIIM_ID | MIIM_CHECKMARKS;
330 mii.fType = MFT_STRING;
331 mii.wID = IDM_TOPMOST;
332 mii.dwTypeData = L"TopMost (&T)";
333 mii.cch = (UINT) wcslen(mii.dwTypeData);
335 InsertMenuItem(hMenu, SC_CLOSE, FALSE, &mii);
337 changeStateTopMostMenu(hWnd,hMenu);
340 void sysmenu_init_subconfig(HWND hWnd, HMENU hMenu)
343 HMENU hSubMenu = CreatePopupMenu();
345 memset(&mii, 0, sizeof(mii));
346 mii.cbSize = sizeof(mii);
347 mii.fMask = MIIM_TYPE | MIIM_ID;
349 wchar_t path[MAX_PATH+1];
350 get_directory_path(path);
351 wcscat_s(path, L"\\*.cfg");
354 memset(&fd, 0, sizeof(fd));
355 HANDLE hFile = FindFirstFile(path, &fd);
356 //MessageBox(hWnd, path, L"", MB_OK);
358 if (hFile != INVALID_HANDLE_VALUE)
362 mii.fType = MFT_STRING;
363 mii.wID = IDM_CONFIG_SELECT_1 + i;
364 mii.dwTypeData = fd.cFileName;
365 mii.cch = (UINT) wcslen(mii.dwTypeData);
366 InsertMenuItem(hSubMenu, 0, TRUE, &mii);
367 if(FindNextFile(hFile, &fd) == 0) { break; }
372 mii.fMask |= MIIM_SUBMENU;
373 mii.fType = MFT_STRING;
374 mii.wID = IDM_CONFIG_SELECT;
375 mii.hSubMenu = hSubMenu;
376 mii.dwTypeData = L"Config (&O)";
377 mii.cch = (UINT) wcslen(mii.dwTypeData);
378 InsertMenuItem(hMenu, SC_CLOSE, FALSE, &mii);
381 void reloadConfig(wchar_t *path)
383 char filepath[MAX_PATH+1];
384 wcstombs(filepath, path, MAX_PATH);
387 opt.setFile(filepath);
391 BOOL onConfigMenuCommand(HWND hWnd, DWORD id)
393 wchar_t path[MAX_PATH+1];
394 get_directory_path(path);
397 memset(&mii, 0, sizeof(mii));
398 mii.cbSize = sizeof(mii);
399 mii.fMask = MIIM_TYPE | MIIM_ID;
400 mii.fType = MFT_STRING;
402 HMENU hMenu = GetSystemMenu(hWnd, FALSE);
403 if (GetMenuItemInfo(hMenu, id, 0, &mii))
405 wchar_t sz[MAX_PATH+1], filepath[MAX_PATH+1];
408 GetMenuItemInfo(hMenu, id, 0, &mii);
409 wsprintf(filepath, L"%s\\%s", path, sz);
410 /* need to open file and update config here */
411 MessageBox(hWnd, filepath, L"", MB_OK);
412 reloadConfig(filepath);
418 BOOL onTopMostMenuCommand(HWND hWnd)
420 HMENU hMenu = GetSystemMenu(hWnd, FALSE);
422 UINT uState = GetMenuState( hMenu, IDM_TOPMOST, MF_BYCOMMAND);
423 DWORD dwExStyle = GetWindowLong(hWnd,GWL_EXSTYLE);
424 if( uState & MFS_CHECKED )
426 SetWindowPos(hWnd, HWND_NOTOPMOST,NULL,NULL,NULL,NULL,SWP_NOMOVE | SWP_NOSIZE);
428 SetWindowPos(hWnd, HWND_TOPMOST,NULL,NULL,NULL,NULL,SWP_NOMOVE | SWP_NOSIZE);
431 changeStateTopMostMenu(hWnd, hMenu);
436 void changeStateTopMostMenu(HWND hWnd,HMENU hMenu)
438 DWORD dwExStyle = GetWindowLong(hWnd,GWL_EXSTYLE);
440 if ((dwExStyle & WS_EX_TOPMOST) == 0) {
441 CheckMenuItem(hMenu, IDM_TOPMOST, MF_BYCOMMAND | MFS_UNCHECKED);
443 CheckMenuItem(hMenu, IDM_TOPMOST, MF_BYCOMMAND | MFS_CHECKED);
448 BOOL onSysCommand(HWND hWnd, DWORD id)
452 copyAllStringToClipboard(hWnd);
455 DialogBox(GetModuleHandle(NULL),
456 MAKEINTRESOURCE(IDD_DIALOG1),
464 return onTopMostMenuCommand(hWnd);
466 if(IDM_CONFIG_SELECT < id && id <= IDM_CONFIG_SELECT_MAX) {
467 return onConfigMenuCommand(hWnd, id);