Christoph von Wittich: fix for bug 823 ("Resource leak in regedit").
[reactos.git] / reactos / subsys / system / regedit / main.c
1 /*
2 * Regedit main function
3 *
4 * Copyright (C) 2002 Robert Dickenson <robd@reactos.org>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #define WIN32_LEAN_AND_MEAN /* Exclude rarely-used stuff from Windows headers */
22 #include <windows.h>
23 #include <commctrl.h>
24 #include <accctrl.h>
25 #include <unknwn.h>
26 #include <stdlib.h>
27 #include <tchar.h>
28 #include <process.h>
29 #include <stdio.h>
30 #include <fcntl.h>
31 #include <aclui.h>
32 #include <cguid.h>
33
34 #include "main.h"
35 #include "hexedit.h"
36 #include "security.h"
37
38 BOOL ProcessCmdLine(LPSTR lpCmdLine);
39
40
41 /*******************************************************************************
42 * Global Variables:
43 */
44
45 HINSTANCE hInst;
46 HWND hFrameWnd;
47 HWND hStatusBar;
48 HMENU hMenuFrame;
49 HMENU hPopupMenus = 0;
50 UINT nClipboardFormat;
51 LPCTSTR strClipboardFormat = _T("TODO: SET CORRECT FORMAT");
52
53
54 #define MAX_LOADSTRING 100
55 TCHAR szTitle[MAX_LOADSTRING];
56 TCHAR szFrameClass[MAX_LOADSTRING];
57 TCHAR szChildClass[MAX_LOADSTRING];
58
59
60 /*******************************************************************************
61 *
62 *
63 * FUNCTION: InitInstance(HANDLE, int)
64 *
65 * PURPOSE: Saves instance handle and creates main window
66 *
67 * COMMENTS:
68 *
69 * In this function, we save the instance handle in a global variable and
70 * create and display the main program window.
71 */
72
73 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
74 {
75 BOOL AclUiAvailable;
76
77 WNDCLASSEX wcFrame = {
78 sizeof(WNDCLASSEX),
79 CS_HREDRAW | CS_VREDRAW/*style*/,
80 FrameWndProc,
81 0/*cbClsExtra*/,
82 0/*cbWndExtra*/,
83 hInstance,
84 LoadIcon(hInstance, MAKEINTRESOURCE(IDI_REGEDIT)),
85 LoadCursor(0, IDC_ARROW),
86 0/*hbrBackground*/,
87 0/*lpszMenuName*/,
88 szFrameClass,
89 (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_REGEDIT), IMAGE_ICON,
90 GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED)
91 };
92 ATOM hFrameWndClass = RegisterClassEx(&wcFrame); /* register frame window class */
93
94 WNDCLASSEX wcChild = {
95 sizeof(WNDCLASSEX),
96 CS_HREDRAW | CS_VREDRAW/*style*/,
97 ChildWndProc,
98 0/*cbClsExtra*/,
99 sizeof(HANDLE)/*cbWndExtra*/,
100 hInstance,
101 LoadIcon(hInstance, MAKEINTRESOURCE(IDI_REGEDIT)),
102 LoadCursor(0, IDC_ARROW),
103 0/*hbrBackground*/,
104 0/*lpszMenuName*/,
105 szChildClass,
106 (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_REGEDIT), IMAGE_ICON,
107 GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED)
108
109 };
110 ATOM hChildWndClass = RegisterClassEx(&wcChild); /* register child windows class */
111 hChildWndClass = hChildWndClass; /* warning eater */
112
113 RegisterHexEditorClass(hInstance);
114
115 hMenuFrame = LoadMenu(hInstance, MAKEINTRESOURCE(IDR_REGEDIT_MENU));
116 hPopupMenus = LoadMenu(hInstance, MAKEINTRESOURCE(IDR_POPUP_MENUS));
117
118 /* Initialize the Windows Common Controls DLL */
119 InitCommonControls();
120
121 AclUiAvailable = InitializeAclUiDll();
122 if(!AclUiAvailable)
123 {
124 HMENU hEditMenu;
125 /* hide the Edit/Permissions... menu entry */
126 hEditMenu = GetSubMenu(hMenuFrame, 1);
127 if(hEditMenu != NULL)
128 {
129 RemoveMenu(hEditMenu, ID_EDIT_PERMISSIONS, MF_BYCOMMAND);
130 /* remove the separator after the menu item */
131 RemoveMenu(hEditMenu, 4, MF_BYPOSITION);
132 }
133 }
134
135 nClipboardFormat = RegisterClipboardFormat(strClipboardFormat);
136 /* if (nClipboardFormat == 0) {
137 DWORD dwError = GetLastError();
138 } */
139
140 hFrameWnd = CreateWindowEx(WS_EX_WINDOWEDGE, (LPCTSTR)(int)hFrameWndClass, szTitle,
141 WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
142 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
143 NULL, hMenuFrame, hInstance, NULL/*lpParam*/);
144
145 if (!hFrameWnd) {
146 return FALSE;
147 }
148
149 /* Create the status bar */
150 hStatusBar = CreateStatusWindow(WS_VISIBLE|WS_CHILD|WS_CLIPSIBLINGS|SBT_NOBORDERS,
151 _T(""), hFrameWnd, STATUS_WINDOW);
152 if (hStatusBar) {
153 /* Create the status bar panes */
154 SetupStatusBar(hFrameWnd, FALSE);
155 CheckMenuItem(GetSubMenu(hMenuFrame, ID_VIEW_MENU), ID_VIEW_STATUSBAR, MF_BYCOMMAND|MF_CHECKED);
156 }
157 ShowWindow(hFrameWnd, nCmdShow);
158 UpdateWindow(hFrameWnd);
159 return TRUE;
160 }
161
162 /******************************************************************************/
163
164 void ExitInstance(HINSTANCE hInstance)
165 {
166 UnregisterHexEditorClass(hInstance);
167 DestroyMenu(hMenuFrame);
168 DestroyMenu(hPopupMenus);
169 UnloadAclUiDll();
170 }
171
172 BOOL TranslateChildTabMessage(MSG *msg)
173 {
174 if (msg->message != WM_KEYDOWN) return FALSE;
175 if (msg->wParam != VK_TAB) return FALSE;
176 if (GetParent(msg->hwnd) != g_pChildWnd->hWnd) return FALSE;
177 PostMessage(g_pChildWnd->hWnd, WM_COMMAND, ID_SWITCH_PANELS, 0);
178 return TRUE;
179 }
180
181 int APIENTRY WinMain(HINSTANCE hInstance,
182 HINSTANCE hPrevInstance,
183 LPSTR lpCmdLine,
184 int nCmdShow)
185 {
186 MSG msg;
187 HACCEL hAccel;
188 /*
189 int hCrt;
190 FILE *hf;
191 AllocConsole();
192 hCrt = _open_osfhandle((long)GetStdHandle(STD_OUTPUT_HANDLE), _O_TEXT);
193 hf = _fdopen(hCrt, "w");
194 *stdout = *hf;
195 setvbuf(stdout, NULL, _IONBF, 0);
196
197 wprintf(L"command line exit, hInstance = %d\n", hInstance);
198 getch();
199 FreeConsole();
200 return 0;
201 */
202
203 if (ProcessCmdLine(lpCmdLine)) {
204 return 0;
205 }
206
207 /* Initialize global strings */
208 LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
209 LoadString(hInstance, IDC_REGEDIT_FRAME, szFrameClass, MAX_LOADSTRING);
210 LoadString(hInstance, IDC_REGEDIT, szChildClass, MAX_LOADSTRING);
211
212 /* Store instance handle in our global variable */
213 hInst = hInstance;
214
215 /* Perform application initialization */
216 if (!InitInstance(hInstance, nCmdShow)) {
217 return FALSE;
218 }
219 hAccel = LoadAccelerators(hInstance, (LPCTSTR)IDC_REGEDIT);
220
221 /* Main message loop */
222 while (GetMessage(&msg, (HWND)NULL, 0, 0)) {
223 if (!TranslateAccelerator(msg.hwnd, hAccel, &msg)
224 && !TranslateChildTabMessage(&msg)) {
225 TranslateMessage(&msg);
226 DispatchMessage(&msg);
227 }
228 }
229 ExitInstance(hInstance);
230 return msg.wParam;
231 }