OSDN Git Service

Add menu of "New". Create new window.
[ckw/ckw.git] / misc.cpp
1 /*-----------------------------------------------------------------------------
2  * File: misc.cpp
3  *-----------------------------------------------------------------------------
4  * Copyright (c) 2004-2005  Kazuo Ishii <k-ishii@wb4.so-net.ne.jp>
5  *                              - original version
6  *
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.
11  *
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.
16  *
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  *---------------------------------------------------------------------------*/
21 #include "ckw.h"
22 #include "rsrc.h"
23 #include "option.h"
24
25 BOOL init_options(ckOpt& opt);
26
27 static void __write_console_input(LPCWSTR str, DWORD length)
28 {
29         if(!str || !length) return;
30
31         INPUT_RECORD *p, *buf;
32         DWORD   i = 0;
33         p = buf = new INPUT_RECORD[ length ];
34
35         for( ; i < length ; i++, p++) {
36                 p->EventType = KEY_EVENT;
37                 p->Event.KeyEvent.bKeyDown = TRUE;
38                 p->Event.KeyEvent.wRepeatCount = 1;
39                 p->Event.KeyEvent.wVirtualKeyCode = 0;
40                 p->Event.KeyEvent.wVirtualScanCode = 0;
41                 p->Event.KeyEvent.uChar.UnicodeChar = 0;
42                 p->Event.KeyEvent.dwControlKeyState = 0;
43                 if(*str == '\r') {
44                         str++;
45                         length--;
46                 }
47                 if(*str == '\n') {
48                         p->Event.KeyEvent.wVirtualKeyCode = VK_RETURN;
49                         str++;
50                 } else {
51                         p->Event.KeyEvent.uChar.UnicodeChar = *str++;
52                 }
53         }
54
55         WriteConsoleInput(gStdIn, buf, length, &length);
56         delete [] buf;
57 }
58
59 /*----------*/
60 void    onPasteFromClipboard(HWND hWnd)
61 {
62         bool    result = true;
63         HANDLE  hMem;
64         wchar_t *ptr;
65
66         if(! IsClipboardFormatAvailable(CF_UNICODETEXT))
67                 return;
68         if(!OpenClipboard(hWnd)) {
69                 Sleep(10);
70                 if(!OpenClipboard(hWnd))
71                         return;
72         }
73         hMem = GetClipboardData(CF_UNICODETEXT);
74         if(!hMem)
75                 result = false;
76         if(result && !(ptr = (wchar_t*)GlobalLock(hMem)))
77                 result = false;
78         if(result) {
79                 __write_console_input(ptr, (DWORD)wcslen(ptr));
80                 GlobalUnlock(hMem);
81         }
82         CloseClipboard();
83 }
84
85 /*----------*/
86 void    onDropFile(HDROP hDrop)
87 {
88         DWORD   i, nb, len;
89         wchar_t wbuf[MAX_PATH+32];
90         wchar_t* wp;
91
92         nb = DragQueryFile(hDrop, (DWORD)-1, NULL, 0);
93         for(i = 0 ; i < nb ; i++) {
94                 len = DragQueryFile(hDrop, i, NULL, 0);
95                 if(len < 1 || len > MAX_PATH)
96                         continue;
97                 wp = wbuf + 1;
98                 if(! DragQueryFile(hDrop, i, wp, MAX_PATH))
99                         continue;
100                 wp[len] = 0;
101                 while(*wp > 0x20) wp++;
102                 if(*wp) {
103                         wp = wbuf;
104                         len++;
105                         wp[0] = wp[len++] = L'\"';
106                 }
107                 else {
108                         wp = wbuf + 1;
109                 }
110                 wp[len++] = L' ';
111
112                 __write_console_input(wp, len);
113         }
114         DragFinish(hDrop);
115 }
116
117 /*----------*/
118 BOOL CALLBACK AboutDlgProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
119 {
120         switch(msg) {
121         case WM_INITDIALOG:
122                 {
123                         HWND hEdit = GetDlgItem(hWnd, IDC_EDIT1);
124                         SetWindowText(hEdit,
125 L"This program is free software; you can redistribute it and/or\r\n"
126 L"modify it under the terms of the GNU General Public License\r\n"
127 L"as published by the Free Software Foundation; either version 2\r\n"
128 L"of the License, or (at your option) any later version.\r\n"
129 L"\r\n"
130 L"This program is distributed in the hope that it will be useful,\r\n"
131 L"but WITHOUT ANY WARRANTY; without even the implied warranty of\r\n"
132 L"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\r\n"
133 L"See the GNU General Public License for more details.\r\n"
134 L"\r\n"
135 L"You should have received a copy of the GNU General Public License\r\n"
136 L"along with this program; if not, write to the Free Software Foundation, Inc.,\r\n"
137 L" 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA."
138                         );
139                 }
140                 return(TRUE);
141         case WM_COMMAND:
142                 switch(LOWORD(wp)) {
143                 case IDOK:
144                 case IDCANCEL:
145                         EndDialog(hWnd, 0);
146                         return(TRUE);
147                 }
148         }
149         return(FALSE);
150 }
151
152 /*----------*/
153 void    sysmenu_init_subconfig(HWND hWnd, HMENU hMenu);
154
155 void    sysmenu_init(HWND hWnd)
156 {
157         MENUITEMINFO mii;
158         HMENU hMenu = GetSystemMenu(hWnd, FALSE);
159
160         memset(&mii, 0, sizeof(mii));
161         mii.cbSize = sizeof(mii);
162         mii.fMask = MIIM_TYPE | MIIM_ID;
163
164         mii.fType = MFT_STRING;
165         mii.wID = IDM_NEW;
166         mii.dwTypeData = L"New (&N)";
167         mii.cch = (UINT) wcslen(mii.dwTypeData);
168         InsertMenuItem(hMenu, SC_CLOSE, FALSE, &mii);
169
170     sysmenu_init_subconfig(hWnd, hMenu);
171
172         mii.fType = MFT_SEPARATOR;
173         mii.wID = 0;
174         mii.dwTypeData = 0;
175         mii.cch = 0;
176         InsertMenuItem(hMenu, SC_CLOSE, FALSE, &mii);
177
178         mii.fType = MFT_STRING;
179         mii.wID = IDM_ABOUT;
180         mii.dwTypeData = L"About (&A)";
181         mii.cch = (UINT) wcslen(mii.dwTypeData);
182         InsertMenuItem(hMenu, SC_CLOSE, FALSE, &mii);
183
184         mii.fType = MFT_SEPARATOR;
185         mii.wID = 0;
186         mii.dwTypeData = 0;
187         mii.cch = 0;
188         InsertMenuItem(hMenu, SC_CLOSE, FALSE, &mii);
189 }
190
191 void    get_directory_path(wchar_t *path)
192 {
193         wchar_t *c;
194         GetModuleFileName(NULL, path, MAX_PATH);
195         c = wcsrchr(path, L'\\');
196         if(c) *c = 0;
197 }
198
199 void    sysmenu_init_subconfig(HWND hWnd, HMENU hMenu)
200 {
201         MENUITEMINFO mii;
202     HMENU hSubMenu = CreatePopupMenu();
203
204         memset(&mii, 0, sizeof(mii));
205         mii.cbSize = sizeof(mii);
206         mii.fMask = MIIM_TYPE | MIIM_ID;
207
208         wchar_t path[MAX_PATH+1];
209     get_directory_path(path);
210     wcscat_s(path, L"\\*.cfg");
211
212     WIN32_FIND_DATA fd;
213         memset(&fd, 0, sizeof(fd));
214     HANDLE hFile = FindFirstFile(path, &fd);
215     //MessageBox(hWnd, path, L"", MB_OK);
216
217     if (hFile != INVALID_HANDLE_VALUE)
218     {
219         for(int i = 0;;i++)
220         {
221             mii.fType = MFT_STRING;
222             mii.wID = IDM_CONFIG_SELECT_1 + i;
223             mii.dwTypeData = fd.cFileName;
224             mii.cch = (UINT) wcslen(mii.dwTypeData);
225             InsertMenuItem(hSubMenu, 0, TRUE, &mii);
226             if(FindNextFile(hFile, &fd) == 0) { break; }
227         }
228         FindClose(hFile);
229     }
230
231         mii.fMask |= MIIM_SUBMENU;
232         mii.fType = MFT_STRING;
233         mii.wID = IDM_CONFIG_SELECT;
234         mii.hSubMenu = hSubMenu;
235         mii.dwTypeData = L"Config (&O)";
236         mii.cch = (UINT) wcslen(mii.dwTypeData);
237         InsertMenuItem(hMenu, SC_CLOSE, FALSE, &mii);
238 }
239
240 void reloadConfig(wchar_t *path)
241 {
242         char filepath[MAX_PATH+1];
243         wcstombs(filepath, path, MAX_PATH);
244
245         ckOpt opt;
246         opt.setFile(filepath);
247     init_options(opt);
248 }
249
250 BOOL    onConfigMenuCommand(HWND hWnd, DWORD id)
251 {
252         wchar_t path[MAX_PATH+1];
253     get_directory_path(path);
254
255         MENUITEMINFO mii;
256         memset(&mii, 0, sizeof(mii));
257         mii.cbSize = sizeof(mii);
258         mii.fMask = MIIM_TYPE | MIIM_ID;
259     mii.fType = MFT_STRING;
260
261         HMENU hMenu = GetSystemMenu(hWnd, FALSE);
262     if (GetMenuItemInfo(hMenu, id, 0, &mii))
263     {
264         wchar_t sz[MAX_PATH+1], filepath[MAX_PATH+1];
265         mii.dwTypeData = sz;
266         mii.cch++;
267         GetMenuItemInfo(hMenu, id, 0, &mii);
268         wsprintf(filepath, L"%s\\%s", path, sz);
269         /* need to open file and update config here */
270         MessageBox(hWnd, filepath, L"", MB_OK);
271                 reloadConfig(filepath);
272     }
273
274     return(TRUE);
275 }
276
277 /*----------*/
278 BOOL    onSysCommand(HWND hWnd, DWORD id)
279 {
280         switch(id) {
281         case IDM_ABOUT:
282                 DialogBox(GetModuleHandle(NULL),
283                           MAKEINTRESOURCE(IDD_DIALOG1),
284                           hWnd,
285                           AboutDlgProc);
286                 return(TRUE);
287         case IDM_NEW:
288                 makeNewWindow();
289                 return(TRUE);
290         }
291     if(IDM_CONFIG_SELECT < id && id <= IDM_CONFIG_SELECT_MAX) {
292         return onConfigMenuCommand(hWnd, id);
293     }
294         return(FALSE);
295 }
296
297 /* EOF */