Cleanup after extracting regedit code.
[reactos.git] / rosapps / regedt32 / regedt32.cpp
1 /*
2 * ReactOS regedt32
3 *
4 * regedt32.cpp
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 "resource.h"
38 #include <shellapi.h>
39 //#include <winspool.h>
40
41 #define STATUS_WINDOW 2001
42 #define MAX_LOADSTRING 100
43
44 // Global Variables:
45 HINSTANCE hInst; // current instance
46
47 HWND hMainWnd; // Main Window
48 HWND hStatusWnd; // Status Bar Window
49 HWND hTabWnd; // Tab Control Window
50
51 int nMinimumWidth; // Minimum width of the dialog (OnSize()'s cx)
52 int nMinimumHeight; // Minimum height of the dialog (OnSize()'s cy)
53
54 int nOldWidth; // Holds the previous client area width
55 int nOldHeight; // Holds the previous client area height
56
57 BOOL bInMenuLoop = FALSE; // Tells us if we are in the menu loop
58
59 TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
60 TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text
61 TCHAR szFrameClass[MAX_LOADSTRING]; // The title bar text
62
63 // Foward declarations of functions included in this code module:
64 ATOM MyRegisterClass(HINSTANCE hInstance);
65 ATOM MyRegisterClass2(HINSTANCE hInstance);
66 BOOL InitInstance(HINSTANCE, int);
67 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
68 LRESULT CALLBACK FrameWndProc(HWND, UINT, WPARAM, LPARAM);
69
70
71 int APIENTRY WinMain(HINSTANCE hInstance,
72 HINSTANCE hPrevInstance,
73 LPSTR lpCmdLine,
74 int nCmdShow)
75 {
76 MSG msg;
77 HACCEL hAccelTable;
78
79 // Initialize global strings
80 LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
81 LoadString(hInstance, IDC_REGEDT32, szWindowClass, MAX_LOADSTRING);
82 LoadString(hInstance, IDC_REGEDT32_FRAME, szFrameClass, MAX_LOADSTRING);
83
84 MyRegisterClass(hInstance);
85 MyRegisterClass2(hInstance);
86
87 // Perform application initialization:
88 if (!InitInstance(hInstance, nCmdShow)) {
89 return FALSE;
90 }
91 hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_REGEDT32);
92
93 // Main message loop:
94 while (GetMessage(&msg, NULL, 0, 0)) {
95 if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) {
96 TranslateMessage(&msg);
97 DispatchMessage(&msg);
98 }
99 }
100 return msg.wParam;
101 }
102
103
104
105 //
106 // FUNCTION: MyRegisterClass()
107 //
108 // PURPOSE: Registers the window class.
109 //
110 // COMMENTS:
111 //
112 // This function and its usage is only necessary if you want this code
113 // to be compatible with Win32 systems prior to the 'RegisterClassEx'
114 // function that was added to Windows 95. It is important to call this function
115 // so that the application will get 'well formed' small icons associated
116 // with it.
117 //
118 ATOM MyRegisterClass(HINSTANCE hInstance)
119 {
120 WNDCLASSEX wcex;
121
122 wcex.cbSize = sizeof(WNDCLASSEX);
123 wcex.style = CS_HREDRAW | CS_VREDRAW;
124 wcex.lpfnWndProc = (WNDPROC)WndProc;
125 wcex.cbClsExtra = 0;
126 wcex.cbWndExtra = 0;
127 wcex.hInstance = hInstance;
128 wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_REGEDT32);
129 wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
130 wcex.hbrBackground = (HBRUSH)SS_BLACKRECT/*(COLOR_WINDOW+1)*/;
131 // wcex.lpszMenuName = (LPCSTR)IDC_REGEDT32;
132 wcex.lpszMenuName = (LPCSTR)IDR_REGEDT32_MENU;
133 wcex.lpszClassName = szWindowClass;
134 wcex.hIconSm = LoadIcon((HINSTANCE)wcex.hInstance, (LPCTSTR)IDI_SMALL);
135 return RegisterClassEx(&wcex);
136 }
137
138 ATOM MyRegisterClass2(HINSTANCE hInstance)
139 {
140 WNDCLASSEX wcex;
141
142 wcex.cbSize = sizeof(WNDCLASSEX);
143 wcex.style = CS_HREDRAW | CS_VREDRAW;
144 wcex.lpfnWndProc = (WNDPROC)FrameWndProc;
145 wcex.cbClsExtra = 0;
146 wcex.cbWndExtra = 0;
147 wcex.hInstance = hInstance;
148 wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_REGEDT32);
149 wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
150 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
151 wcex.lpszMenuName = (LPCSTR)IDR_REGEDT32_MENU;
152 wcex.lpszClassName = szFrameClass;
153 wcex.hIconSm = LoadIcon((HINSTANCE)wcex.hInstance, (LPCTSTR)IDI_SMALL);
154 return RegisterClassEx(&wcex);
155 }
156
157 //
158 //
159 // FUNCTION: InitInstance(HANDLE, int)
160 //
161 // PURPOSE: Saves instance handle and creates main window
162 //
163 // COMMENTS:
164 //
165 // In this function, we save the instance handle in a global variable and
166 // create and display the main program window.
167 //
168 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
169 {
170 int nParts[3];
171
172 // Initialize the Windows Common Controls DLL
173 InitCommonControls();
174
175 hInst = hInstance; // Store instance handle in our global variable
176 hMainWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
177 CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
178 if (!hMainWnd) {
179 return FALSE;
180 }
181
182 // Get the minimum window sizes
183 // GetWindowRect(hMainWnd, &rc);
184 // nMinimumWidth = (rc.right - rc.left);
185 // nMinimumHeight = (rc.bottom - rc.top);
186
187 // Create the status bar
188 hStatusWnd = CreateStatusWindow(WS_VISIBLE|WS_CHILD|WS_CLIPSIBLINGS|SBT_NOBORDERS,
189 "", hMainWnd, STATUS_WINDOW);
190 if (!hStatusWnd)
191 return FALSE;
192
193 // Create the status bar panes
194 nParts[0] = 100;
195 nParts[1] = 210;
196 nParts[2] = 400;
197 SendMessage(hStatusWnd, SB_SETPARTS, 3, (long)nParts);
198
199
200 ShowWindow(hMainWnd, nCmdShow);
201 UpdateWindow(hMainWnd);
202 return TRUE;
203 }
204
205
206 // OnSize()
207 // This function handles all the sizing events for the application
208 // It re-sizes every window, and child window that needs re-sizing
209 void OnSize(UINT nType, int cx, int cy)
210 {
211 int nParts[3];
212 int nXDifference;
213 int nYDifference;
214 RECT rc;
215
216 if (nType == SIZE_MINIMIZED)
217 return;
218
219 nXDifference = cx - nOldWidth;
220 nYDifference = cy - nOldHeight;
221 nOldWidth = cx;
222 nOldHeight = cy;
223
224 // Update the status bar size
225 GetWindowRect(hStatusWnd, &rc);
226 SendMessage(hStatusWnd, WM_SIZE, nType, MAKELPARAM(cx, cy + (rc.bottom - rc.top)));
227
228 // Update the status bar pane sizes
229 nParts[0] = bInMenuLoop ? -1 : 100;
230 nParts[1] = 210;
231 nParts[2] = cx;
232 SendMessage(hStatusWnd, SB_SETPARTS, bInMenuLoop ? 1 : 3, (long)nParts);
233 }
234
235 void OnEnterMenuLoop(HWND hWnd)
236 {
237 int nParts;
238
239 // Update the status bar pane sizes
240 nParts = -1;
241 SendMessage(hStatusWnd, SB_SETPARTS, 1, (long)&nParts);
242 bInMenuLoop = TRUE;
243 SendMessage(hStatusWnd, SB_SETTEXT, (WPARAM)0, (LPARAM)_T(""));
244 }
245
246 void OnExitMenuLoop(HWND hWnd)
247 {
248 RECT rc;
249 int nParts[3];
250 // TCHAR text[260];
251
252 bInMenuLoop = FALSE;
253 // Update the status bar pane sizes
254 GetClientRect(hWnd, &rc);
255 nParts[0] = 100;
256 nParts[1] = 210;
257 nParts[2] = rc.right;
258 SendMessage(hStatusWnd, SB_SETPARTS, 3, (long)nParts);
259 SendMessage(hStatusWnd, SB_SETTEXT, 0, (LPARAM)_T(""));
260 // wsprintf(text, _T("CPU Usage: %3d%%"), PerfDataGetProcessorUsage());
261 // SendMessage(hStatusWnd, SB_SETTEXT, 1, (LPARAM)text);
262 // wsprintf(text, _T("Processes: %d"), PerfDataGetProcessCount());
263 // SendMessage(hStatusWnd, SB_SETTEXT, 0, (LPARAM)text);
264 }
265
266 void OnMenuSelect(HWND hWnd, UINT nItemID, UINT nFlags, HMENU hSysMenu)
267 {
268 TCHAR str[100];
269
270 strcpy(str, TEXT(""));
271 if (LoadString(hInst, nItemID, str, 100)) {
272 // load appropriate string
273 LPTSTR lpsz = str;
274 // first newline terminates actual string
275 lpsz = _tcschr(lpsz, '\n');
276 if (lpsz != NULL)
277 *lpsz = '\0';
278 }
279 SendMessage(hStatusWnd, SB_SETTEXT, 0, (LPARAM)str);
280 }
281
282
283 LRESULT CALLBACK FrameWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
284 {
285 int wmId, wmEvent;
286 PAINTSTRUCT ps;
287 HDC hdc;
288 TCHAR szHello[MAX_LOADSTRING];
289 LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
290
291 switch (message) {
292 case WM_COMMAND:
293 wmId = LOWORD(wParam);
294 wmEvent = HIWORD(wParam);
295 // Parse the menu selections:
296 switch (wmId) {
297 case IDM_ABOUT:
298 // ShowAboutBox(hWnd);
299 {
300 HICON hIcon = LoadIcon(hInst, (LPCTSTR)IDI_REGEDT32);
301 ShellAbout(hWnd, szTitle, "FrameWndProc", hIcon);
302 //if (hIcon) DestroyIcon(hIcon); // NOT REQUIRED
303 }
304 break;
305 case IDM_EXIT:
306 DestroyWindow(hWnd);
307 break;
308 default:
309 return DefWindowProc(hWnd, message, wParam, lParam);
310 }
311 break;
312 case WM_PAINT:
313 hdc = BeginPaint(hWnd, &ps);
314 // TODO: Add any drawing code here...
315 RECT rt;
316 GetClientRect(hWnd, &rt);
317 DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
318 EndPaint(hWnd, &ps);
319 break;
320 case WM_DESTROY:
321 PostQuitMessage(0);
322 break;
323 default:
324 return DefWindowProc(hWnd, message, wParam, lParam);
325 }
326 return 0;
327 }
328
329
330 //
331 // FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
332 //
333 // PURPOSE: Processes messages for the main window.
334 //
335 // WM_COMMAND - process the application menu
336 // WM_PAINT - Paint the main window
337 // WM_DESTROY - post a quit message and return
338 //
339 //
340 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
341 {
342 int wmId, wmEvent;
343 PAINTSTRUCT ps;
344 HDC hdc;
345 //TCHAR szHello[MAX_LOADSTRING];
346 //LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
347
348 switch (message) {
349 case WM_COMMAND:
350 wmId = LOWORD(wParam);
351 wmEvent = HIWORD(wParam);
352 // Parse the menu selections:
353 switch (wmId) {
354 case ID_REGISTRY_PRINTERSETUP:
355 //PRINTDLG pd;
356 //PrintDlg(&pd);
357 //PAGESETUPDLG psd;
358 //PageSetupDlg(&psd);
359 break;
360 case ID_REGISTRY_OPENLOCAL:
361 {
362 HWND hChildWnd;
363 // hChildWnd = CreateWindow(szFrameClass, szTitle, WS_OVERLAPPEDWINDOW | WS_CHILD,
364 // CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, hWnd, NULL, hInst, NULL);
365 hChildWnd = CreateWindow(szFrameClass, szTitle, WS_OVERLAPPEDWINDOW | WS_CHILD,
366 0, 0, 150, 170, hWnd, NULL, hInst, NULL);
367 if (hChildWnd) {
368 ShowWindow(hChildWnd, 1);
369 UpdateWindow(hChildWnd);
370 }
371 }
372 break;
373 case IDM_ABOUT:
374 // ShowAboutBox(hWnd);
375 {
376 HICON hIcon = LoadIcon(hInst, (LPCTSTR)IDI_REGEDT32);
377 ShellAbout(hWnd, szTitle, "", hIcon);
378 //if (hIcon) DestroyIcon(hIcon); // NOT REQUIRED
379 }
380 break;
381 case IDM_EXIT:
382 DestroyWindow(hWnd);
383 break;
384 default:
385 return DefWindowProc(hWnd, message, wParam, lParam);
386 }
387 break;
388 case WM_SIZE:
389 // Handle the window sizing in it's own function
390 OnSize(wParam, LOWORD(lParam), HIWORD(lParam));
391 break;
392 case WM_PAINT:
393 hdc = BeginPaint(hWnd, &ps);
394 // TODO: Add any drawing code here...
395 RECT rt;
396 GetClientRect(hWnd, &rt);
397 //DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
398 EndPaint(hWnd, &ps);
399 break;
400 case WM_DESTROY:
401 PostQuitMessage(0);
402 break;
403 case WM_TIMER:
404 break;
405
406 case WM_ENTERMENULOOP:
407 OnEnterMenuLoop(hWnd);
408 break;
409 case WM_EXITMENULOOP:
410 OnExitMenuLoop(hWnd);
411 break;
412 case WM_MENUSELECT:
413 OnMenuSelect(hWnd, LOWORD(wParam), HIWORD(wParam), (HMENU)lParam);
414 break;
415 default:
416 return DefWindowProc(hWnd, message, wParam, lParam);
417 }
418 return 0;
419 }