- Create another branch for networking fixes
[reactos.git] / base / applications / mmc / console.c
1 /*
2 * ReactOS Management Console
3 * Copyright (C) 2006 - 2007 Thomas Weidenmueller
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include "precomp.h"
21
22 static const TCHAR szMMCMainFrame[] = TEXT("MMCMainFrame");
23 static const TCHAR szMMCChildFrm[] = TEXT("MMCChildFrm");
24
25 static LONG MainFrameWndCount = 0;
26 static ULONG NewConsoleCount = 0;
27
28 static LPTSTR
29 CreateNewConsoleTitle(VOID)
30 {
31 LPTSTR lpTitle;
32
33 if (LoadAndFormatString(hAppInstance,
34 IDS_CONSOLETITLE,
35 &lpTitle,
36 ++NewConsoleCount) == 0)
37 {
38 lpTitle = NULL;
39 }
40
41 return lpTitle;
42 }
43
44 typedef struct _CONSOLE_MAINFRAME_WND
45 {
46 HWND hwnd;
47 LPCTSTR lpConsoleTitle;
48 HMENU hMenuConsoleRoot;
49 union
50 {
51 DWORD Flags;
52 struct
53 {
54 DWORD AppAuthorMode : 1;
55 };
56 };
57 } CONSOLE_MAINFRAME_WND, *PCONSOLE_MAINFRAME_WND;
58
59 static LRESULT CALLBACK
60 ConsoleMainFrameWndProc(IN HWND hwnd,
61 IN UINT uMsg,
62 IN WPARAM wParam,
63 IN LPARAM lParam)
64 {
65 PCONSOLE_MAINFRAME_WND Info;
66 LRESULT Ret = FALSE;
67
68 Info = (PCONSOLE_MAINFRAME_WND)GetWindowLongPtr(hwnd,
69 0);
70
71 if (Info != NULL || uMsg == WM_NCCREATE)
72 {
73 switch (uMsg)
74 {
75 case WM_COMMAND:
76 {
77 switch (LOWORD(wParam))
78 {
79 case ID_FILE_EXIT:
80 PostMessage(hwnd,
81 WM_CLOSE,
82 0,
83 0);
84 break;
85 }
86 break;
87 }
88
89 case WM_NCCREATE:
90 {
91 MainFrameWndCount++;
92
93 Info = HeapAlloc(hAppHeap,
94 0,
95 sizeof(*Info));
96 if (Info != NULL)
97 {
98 ZeroMemory(Info,
99 sizeof(*Info));
100
101 Info->hwnd = hwnd;
102
103 SetWindowLongPtr(hwnd,
104 0,
105 (LONG_PTR)Info);
106
107 Info->hMenuConsoleRoot = LoadMenu(hAppInstance,
108 MAKEINTRESOURCE(IDM_CONSOLEROOT));
109 Ret = TRUE;
110 }
111 break;
112 }
113
114 case WM_CREATE:
115 {
116 LPCTSTR lpFileName = (LPCTSTR)(((LPCREATESTRUCT)lParam)->lpCreateParams);
117
118 if (lpFileName != NULL)
119 {
120 /* FIXME */
121 }
122 else
123 {
124 Info->AppAuthorMode = TRUE;
125 Info->lpConsoleTitle = CreateNewConsoleTitle();
126 }
127
128 SetWindowText(Info->hwnd,
129 Info->lpConsoleTitle);
130 break;
131 }
132
133 case WM_NCDESTROY:
134 SetMenu(Info->hwnd,
135 NULL);
136
137 if (Info->hMenuConsoleRoot != NULL)
138 {
139 DestroyMenu(Info->hMenuConsoleRoot);
140 Info->hMenuConsoleRoot = NULL;
141 }
142
143 HeapFree(hAppHeap,
144 0,
145 Info);
146
147 if (--MainFrameWndCount == 0)
148 PostQuitMessage(0);
149 break;
150
151
152 case WM_CLOSE:
153 DestroyWindow(hwnd);
154 break;
155
156 default:
157 goto HandleDefaultMsg;
158 }
159 }
160 else
161 {
162 HandleDefaultMsg:
163 Ret = DefWindowProc(hwnd,
164 uMsg,
165 wParam,
166 lParam);
167 }
168
169 return Ret;
170 }
171
172 typedef struct _CONSOLE_CHILDFRM_WND
173 {
174 HWND hwnd;
175 PCONSOLE_MAINFRAME_WND MainFrame;
176 } CONSOLE_CHILDFRM_WND, *PCONSOLE_CHILDFRM_WND;
177
178 static LRESULT CALLBACK
179 ConsoleChildFrmProc(IN HWND hwnd,
180 IN UINT uMsg,
181 IN WPARAM wParam,
182 IN LPARAM lParam)
183 {
184 PCONSOLE_CHILDFRM_WND Info;
185 LRESULT Ret = FALSE;
186
187 Info = (PCONSOLE_CHILDFRM_WND)GetWindowLongPtr(hwnd,
188 0);
189
190 if (Info != NULL || uMsg == WM_NCCREATE)
191 {
192 switch (uMsg)
193 {
194 case WM_NCCREATE:
195 Info = HeapAlloc(hAppHeap,
196 0,
197 sizeof(*Info));
198 if (Info != NULL)
199 {
200 ZeroMemory(Info,
201 sizeof(*Info));
202
203 Info->hwnd = hwnd;
204
205 SetWindowLongPtr(hwnd,
206 0,
207 (LONG_PTR)Info);
208
209 Ret = TRUE;
210 }
211 break;
212
213
214 case WM_NCDESTROY:
215 HeapFree(hAppHeap,
216 0,
217 Info);
218 break;
219
220 default:
221 goto HandleDefaultMsg;
222 }
223 }
224 else
225 {
226 HandleDefaultMsg:
227 Ret = DefWindowProc(hwnd,
228 uMsg,
229 wParam,
230 lParam);
231 }
232
233 return Ret;
234
235 }
236
237 BOOL
238 RegisterMMCWndClasses(VOID)
239 {
240 WNDCLASS wc;
241 BOOL Ret;
242
243 /* Register the MMCMainFrame window class */
244 wc.style = 0;
245 wc.lpfnWndProc = ConsoleMainFrameWndProc;
246 wc.cbClsExtra = 0;
247 wc.cbWndExtra = sizeof(PCONSOLE_MAINFRAME_WND);
248 wc.hInstance = hAppInstance;
249 wc.hIcon = LoadIcon(hAppInstance,
250 MAKEINTRESOURCE(IDI_MAINAPP));
251 wc.hCursor = LoadCursor(NULL,
252 MAKEINTRESOURCE(IDC_ARROW));
253 wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
254 wc.lpszMenuName = NULL;
255 wc.lpszClassName = szMMCMainFrame;
256
257 Ret = (RegisterClass(&wc) != (ATOM)0);
258 if (Ret)
259 {
260 /* Register the MMCChildFrm window class */
261 wc.lpfnWndProc = ConsoleChildFrmProc;
262 wc.cbWndExtra = sizeof(PCONSOLE_CHILDFRM_WND);
263 wc.lpszClassName = szMMCChildFrm;
264
265 Ret = (RegisterClass(&wc) != (ATOM)0);
266 if (!Ret)
267 {
268 UnregisterClass(szMMCMainFrame,
269 hAppInstance);
270 }
271 }
272
273 return Ret;
274 }
275
276 VOID
277 UnregisterMMCWndClasses(VOID)
278 {
279 UnregisterClass(szMMCChildFrm,
280 hAppInstance);
281 UnregisterClass(szMMCMainFrame,
282 hAppInstance);
283 }
284
285 HWND
286 CreateConsoleWindow(IN LPCTSTR lpFileName OPTIONAL)
287 {
288 HWND hWndConsole;
289 LONG_PTR FileName = (LONG_PTR)lpFileName;
290
291 hWndConsole = CreateWindowEx(WS_EX_WINDOWEDGE,
292 szMMCMainFrame,
293 NULL,
294 WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS,
295 CW_USEDEFAULT,
296 CW_USEDEFAULT,
297 CW_USEDEFAULT,
298 CW_USEDEFAULT,
299 NULL,
300 NULL,
301 hAppInstance,
302 (PVOID)FileName);
303
304 if (hWndConsole != NULL)
305 {
306 ShowWindow(hWndConsole,
307 SW_SHOWDEFAULT);
308 }
309
310 return hWndConsole;
311 }