Updates with progress on calling standard dialogs, FileOpen, SelectFont and more...
[reactos.git] / rosapps / regedt32 / framewnd.c
1 /*
2 * ReactOS regedt32
3 *
4 * framewnd.c
5 *
6 * Copyright (C) 2002 Robert Dickenson <robd@reactos.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #ifdef _MSC_VER
24 #include "stdafx.h"
25 #else
26 //#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
27 #include <windows.h>
28 #include <commctrl.h>
29 #include <stdlib.h>
30 #include <malloc.h>
31 #include <memory.h>
32 #include <tchar.h>
33 #include <process.h>
34 #include <stdio.h>
35 #endif
36
37 #include <shellapi.h>
38
39 #include "main.h"
40 #include "framewnd.h"
41
42
43 ////////////////////////////////////////////////////////////////////////////////
44 // Globals and Variables:
45 //
46
47 BOOL bInMenuLoop = FALSE; // Tells us if we are in the menu loop
48
49 static HHOOK hcbthook;
50 static ChildWnd* newchild = NULL;
51
52 ////////////////////////////////////////////////////////////////////////////////
53 // Local module support methods
54 //
55
56 static LRESULT CALLBACK CBTProc(int code, WPARAM wParam, LPARAM lParam)
57 {
58 if (code == HCBT_CREATEWND && newchild) {
59 ChildWnd* pChildWnd = newchild;
60 newchild = NULL;
61 pChildWnd->hWnd = (HWND)wParam;
62 SetWindowLong(pChildWnd->hWnd, GWL_USERDATA, (LPARAM)pChildWnd);
63 }
64 return CallNextHookEx(hcbthook, code, wParam, lParam);
65 }
66
67 static HWND InitChildWindow(LPTSTR param)
68 {
69 //TCHAR drv[_MAX_DRIVE];
70 TCHAR path[MAX_PATH];
71 ChildWnd* pChildWnd = NULL;
72 pChildWnd = (ChildWnd*)malloc(sizeof(ChildWnd));
73 if (pChildWnd != NULL) {
74 MDICREATESTRUCT mcs = {
75 szChildClass, path, hInst,
76 CW_USEDEFAULT, CW_USEDEFAULT,
77 CW_USEDEFAULT, CW_USEDEFAULT,
78 0/*style*/, 0/*lParam*/
79 };
80 memset(pChildWnd, 0, sizeof(ChildWnd));
81 lstrcpy(pChildWnd->szPath, path);
82 pChildWnd->pos.length = sizeof(WINDOWPLACEMENT);
83 pChildWnd->pos.flags = 0;
84 pChildWnd->pos.showCmd = SW_SHOWNORMAL;
85 pChildWnd->pos.rcNormalPosition.left = CW_USEDEFAULT;
86 pChildWnd->pos.rcNormalPosition.top = CW_USEDEFAULT;
87 pChildWnd->pos.rcNormalPosition.right = CW_USEDEFAULT;
88 pChildWnd->pos.rcNormalPosition.bottom = CW_USEDEFAULT;
89 pChildWnd->nFocusPanel = 0;
90 pChildWnd->nSplitPos = 200;
91 hcbthook = SetWindowsHookEx(WH_CBT, CBTProc, 0, GetCurrentThreadId());
92 newchild = pChildWnd;
93 pChildWnd->hWnd = (HWND)SendMessage(hMDIClient, WM_MDICREATE, 0, (LPARAM)&mcs);
94 UnhookWindowsHookEx(hcbthook);
95 if (pChildWnd->hWnd == NULL) {
96 free(pChildWnd);
97 newchild = pChildWnd = NULL;
98 }
99 return pChildWnd->hWnd;
100 }
101 return 0;
102 }
103
104 static BOOL CALLBACK CloseEnumProc(HWND hWnd, LPARAM lParam)
105 {
106 if (!GetWindow(hWnd, GW_OWNER)) {
107 SendMessage(GetParent(hWnd), WM_MDIRESTORE, (WPARAM)hWnd, 0);
108 if (SendMessage(hWnd, WM_QUERYENDSESSION, 0, 0)) {
109 SendMessage(GetParent(hWnd), WM_MDIDESTROY, (WPARAM)hWnd, 0);
110 }
111 }
112 return 1;
113 }
114 /*
115 UINT_PTR CALLBACK CFHookProc(
116 HWND hdlg, // handle to dialog box
117 UINT uiMsg, // message identifier
118 WPARAM wParam, // message parameter
119 LPARAM lParam // message parameter
120 );
121
122 typedef UINT_PTR (CALLBACK *LPCFHOOKPROC)(HWND, UINT, WPARAM, LPARAM);
123
124 typedef struct {
125 DWORD lStructSize;
126 HWND hwndOwner;
127 HDC hDC;
128 LPLOGFONT lpLogFont;
129 INT iPointSize;
130 DWORD Flags;
131 COLORREF rgbColors;
132 LPARAM lCustData;
133 LPCFHOOKPROC lpfnHook;
134 LPCTSTR lpTemplateName;
135 HINSTANCE hInstance;
136 LPTSTR lpszStyle;
137 WORD nFontType;
138 WORD ___MISSING_ALIGNMENT__;
139 INT nSizeMin;
140 INT nSizeMax;
141 } CHOOSEFONT, *LPCHOOSEFONT;
142 */
143 void CmdOptionsFont(HWND hWnd)
144 {
145 // LOGFONT LogFont;
146 CHOOSEFONT cf = { sizeof(CHOOSEFONT), hWnd, NULL,
147 // &LogFont, // lpLogFont
148 NULL, // lpLogFont
149 0, // iPointSize
150 // CF_INITTOLOGFONTSTRUCT, // Flags
151 CF_SCREENFONTS, // Flags
152 0, // rgbColors;
153 0L, // lCustData;
154 NULL, // lpfnHook;
155 NULL, // lpTemplateName;
156 hInst, // hInstance;
157 NULL, // lpszStyle;
158 0, // nFontType;
159 0, // ___MISSING_ALIGNMENT__;
160 0, // nSizeMin;
161 0 // nSizeMax
162 };
163
164 if (ChooseFont(&cf)) {
165
166
167 } else {
168 TCHAR* errStr = NULL;
169 DWORD error = CommDlgExtendedError();
170 switch (error) {
171 case CDERR_DIALOGFAILURE: errStr = _T("The dialog box could not be created. The common dialog box function's call to the DialogBox function failed. For example, this error occurs if the common dialog box call specifies an invalid window handle."); break;
172 case CDERR_FINDRESFAILURE: errStr = _T("The common dialog box function failed to find a specified resource."); break;
173 case CDERR_INITIALIZATION: errStr = _T("The common dialog box function failed during initialization. This error often occurs when sufficient memory is not available."); break;
174 case CDERR_LOADRESFAILURE: errStr = _T("The common dialog box function failed to load a specified resource."); break;
175 case CDERR_LOADSTRFAILURE: errStr = _T("The common dialog box function failed to load a specified string."); break;
176 case CDERR_LOCKRESFAILURE: errStr = _T("The common dialog box function failed to lock a specified resource."); break;
177 case CDERR_MEMALLOCFAILURE: errStr = _T("The common dialog box function was unable to allocate memory for internal structures."); break;
178 case CDERR_MEMLOCKFAILURE: errStr = _T("The common dialog box function was unable to lock the memory associated with a handle."); break;
179 case CDERR_NOHINSTANCE: errStr = _T("The ENABLETEMPLATE flag was set in the Flags member of the initialization structure for the corresponding common dialog box, but you failed to provide a corresponding instance handle."); break;
180 case CDERR_NOHOOK: errStr = _T("The ENABLEHOOK flag was set in the Flags member of the initialization structure for the corresponding common dialog box, but you failed to provide a pointer to a corresponding hook procedure."); break;
181 case CDERR_NOTEMPLATE: errStr = _T("The ENABLETEMPLATE flag was set in the Flags member of the initialization structure for the corresponding common dialog box, but you failed to provide a corresponding template."); break;
182 case CDERR_REGISTERMSGFAIL: errStr = _T("The RegisterWindowMessage function returned an error code when it was called by the common dialog box function."); break;
183 case CDERR_STRUCTSIZE: errStr = _T("The lStructSize member of the initialization structure for the corresponding common dialog box is invalid."); break;
184 case CFERR_MAXLESSTHANMIN:
185 break;
186 case CFERR_NOFONTS:
187 break;
188 }
189 if (errStr) {
190 MessageBox(hWnd, errStr, szTitle, MB_ICONERROR | MB_OK);
191 }
192 }
193 }
194
195
196 void CmdRegistryPrint(HWND hWnd, int cmd)
197 {
198 PRINTDLG pd = { sizeof(PRINTDLG), hWnd,
199 0, // hDevMode;
200 0, // hDevNames;
201 NULL, // hDC;
202 0L, // Flags;
203 0, // nFromPage;
204 0, // nToPage;
205 0, // nMinPage;
206 0, // nMaxPage;
207 0, // nCopies;
208 NULL, // hInstance;
209 0, // lCustData;
210 NULL, // lpfnPrintHook;
211 NULL, // lpfnSetupHook;
212 NULL, // lpPrintTemplateName;
213 NULL, // lpSetupTemplateName;
214 0, // hPrintTemplate;
215 0 // hSetupTemplate;
216 };
217
218 switch (cmd) {
219 case ID_REGISTRY_PRINTSUBTREE:
220 PrintDlg(&pd);
221 break;
222 case ID_REGISTRY_PRINTERSETUP:
223 PrintDlg(&pd);
224 break;
225 }
226 //PAGESETUPDLG psd;
227 //PageSetupDlg(&psd);
228 }
229 /*
230 typedef struct tagOFN {
231 DWORD lStructSize;
232 HWND hwndOwner;
233 HINSTANCE hInstance;
234 LPCTSTR lpstrFilter;
235 LPTSTR lpstrCustomFilter;
236 DWORD nMaxCustFilter;
237 DWORD nFilterIndex;
238 LPTSTR lpstrFile;
239 DWORD nMaxFile;
240 LPTSTR lpstrFileTitle;
241 DWORD nMaxFileTitle;
242 LPCTSTR lpstrInitialDir;
243 LPCTSTR lpstrTitle;
244 DWORD Flags;
245 WORD nFileOffset;
246 WORD nFileExtension;
247 LPCTSTR lpstrDefExt;
248 LPARAM lCustData;
249 LPOFNHOOKPROC lpfnHook;
250 LPCTSTR lpTemplateName;
251 #if (_WIN32_WINNT >= 0x0500)
252 void * pvReserved;
253 DWORD dwReserved;
254 DWORD FlagsEx;
255 #endif // (_WIN32_WINNT >= 0x0500)
256 } OPENFILENAME, *LPOPENFILENAME;
257 */
258 //GetOpenFileName(...);
259 //GetSaveFileName(...);
260 void CmdRegistrySaveSubTreeAs(HWND hWnd)
261 {
262 OPENFILENAME ofn;// = { };
263
264 memset(&ofn, 0, sizeof(OPENFILENAME));
265
266 ofn.lStructSize = sizeof(OPENFILENAME);
267 ofn.hwndOwner = hWnd;
268 if (GetSaveFileName(&ofn)) {
269 } else {
270 }
271 }
272
273
274 static LRESULT _CmdWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
275 {
276 HWND hChildWnd;
277 switch (LOWORD(wParam)) {
278 case ID_WINDOW_CLOSEALL:
279 EnumChildWindows(hMDIClient, &CloseEnumProc, 0);
280 break;
281 case ID_WINDOW_CLOSE:
282 hChildWnd = (HWND) SendMessage(hMDIClient, WM_MDIGETACTIVE, 0, 0);
283 if (!SendMessage(hChildWnd, WM_QUERYENDSESSION, 0, 0))
284 SendMessage(hMDIClient, WM_MDIDESTROY, (WPARAM)hChildWnd, 0);
285 break;
286 // case ID_FILE_EXIT:
287 // SendMessage(hWnd, WM_CLOSE, 0, 0);
288 // break;
289 // case IDM_EXIT:
290 // DestroyWindow(hWnd);
291 // break;
292 // case ID_FILE_OPEN:
293 case ID_REGISTRY_PRINTSUBTREE:
294 case ID_REGISTRY_PRINTERSETUP:
295 CmdRegistryPrint(hWnd, LOWORD(wParam));
296 break;
297 case ID_REGISTRY_SAVESUBTREEAS:
298 CmdRegistrySaveSubTreeAs(hWnd);
299 break;
300 case ID_OPTIONS_FONT:
301 CmdOptionsFont(hWnd);
302 break;
303 case ID_REGISTRY_OPENLOCAL:
304 case ID_WINDOW_NEW_WINDOW:
305 InitChildWindow("Child Window");
306 return 0;
307 case ID_WINDOW_CASCADE:
308 SendMessage(hMDIClient, WM_MDICASCADE, 0, 0);
309 break;
310 case ID_WINDOW_TILE_HORZ:
311 SendMessage(hMDIClient, WM_MDITILE, MDITILE_HORIZONTAL, 0);
312 break;
313 case ID_WINDOW_TILE_VERT:
314 SendMessage(hMDIClient, WM_MDITILE, MDITILE_VERTICAL, 0);
315 break;
316 case ID_WINDOW_ARRANGE_ICONS:
317 SendMessage(hMDIClient, WM_MDIICONARRANGE, 0, 0);
318 break;
319 case ID_HELP_ABOUT:
320 // ShowAboutBox(hWnd);
321 {
322 HICON hIcon = LoadIcon(hInst, (LPCTSTR)IDI_REGEDT32);
323 ShellAbout(hWnd, szTitle, "FrameWndProc", hIcon);
324 //if (hIcon) DestroyIcon(hIcon); // NOT REQUIRED
325 }
326 break;
327 default:
328 hChildWnd = (HWND)SendMessage(hMDIClient, WM_MDIGETACTIVE, 0, 0);
329 if (IsWindow(hChildWnd))
330 SendMessage(hChildWnd, WM_COMMAND, wParam, lParam);
331 else
332 return DefFrameProc(hWnd, hMDIClient, message, wParam, lParam);
333 }
334 return 0;
335 }
336
337 ////////////////////////////////////////////////////////////////////////////////
338 //
339 // FUNCTION: FrameWndProc(HWND, unsigned, WORD, LONG)
340 //
341 // PURPOSE: Processes messages for the main frame window.
342 //
343 // WM_COMMAND - process the application menu
344 // WM_DESTROY - post a quit message and return
345 //
346 //
347
348 LRESULT CALLBACK FrameWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
349 {
350 switch (message) {
351 case WM_CREATE:
352 {
353 HMENU hMenuWindow = GetSubMenu(hMenuFrame, GetMenuItemCount(hMenuFrame)-2);
354 CLIENTCREATESTRUCT ccs = { hMenuWindow, IDW_FIRST_CHILD };
355 hMDIClient = CreateWindowEx(0, _T("MDICLIENT"), NULL,
356 WS_CHILD|WS_CLIPCHILDREN|WS_VSCROLL|WS_HSCROLL|WS_VISIBLE|WS_BORDER,
357 0, 0, 0, 0,
358 hWnd, (HMENU)0, hInst, &ccs);
359 }
360 break;
361 case WM_COMMAND:
362 return _CmdWndProc(hWnd, message, wParam, lParam);
363 break;
364 case WM_DESTROY:
365 PostQuitMessage(0);
366 break;
367 case WM_QUERYENDSESSION:
368 case WM_CLOSE:
369 SendMessage(hWnd, WM_COMMAND, ID_WINDOW_CLOSEALL, 0);
370 if (GetWindow(hMDIClient, GW_CHILD) != NULL)
371 return 0;
372 // else fall thru...
373 default:
374 return DefFrameProc(hWnd, hMDIClient, message, wParam, lParam);
375 }
376 return 0;
377 }
378
379