Regedit: Implemented accelerator keys
[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 #include "regproc.h"
38
39 BOOL ProcessCmdLine(LPSTR lpCmdLine);
40
41
42 /*******************************************************************************
43 * Global Variables:
44 */
45
46 HINSTANCE hInst;
47 HWND hFrameWnd;
48 HWND hStatusBar;
49 HMENU hMenuFrame;
50 HMENU hPopupMenus = 0;
51 UINT nClipboardFormat;
52 LPCTSTR strClipboardFormat = _T("TODO: SET CORRECT FORMAT");
53 const TCHAR g_szGeneralRegKey[] = _T("Software\\Microsoft\\Windows\\CurrentVersion\\Applets\\Regedit");
54
55
56 #define MAX_LOADSTRING 100
57 TCHAR szTitle[MAX_LOADSTRING];
58 TCHAR szFrameClass[MAX_LOADSTRING];
59 TCHAR szChildClass[MAX_LOADSTRING];
60
61
62 /*******************************************************************************
63 *
64 *
65 * FUNCTION: InitInstance(HANDLE, int)
66 *
67 * PURPOSE: Saves instance handle and creates main window
68 *
69 * COMMENTS:
70 *
71 * In this function, we save the instance handle in a global variable and
72 * create and display the main program window.
73 */
74
75 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
76 {
77 BOOL AclUiAvailable;
78 HMENU hEditMenu;
79 TCHAR szBuffer[256];
80
81 WNDCLASSEX wcFrame = {
82 sizeof(WNDCLASSEX),
83 CS_HREDRAW | CS_VREDRAW/*style*/,
84 FrameWndProc,
85 0/*cbClsExtra*/,
86 0/*cbWndExtra*/,
87 hInstance,
88 LoadIcon(hInstance, MAKEINTRESOURCE(IDI_REGEDIT)),
89 LoadCursor(0, IDC_ARROW),
90 0/*hbrBackground*/,
91 0/*lpszMenuName*/,
92 szFrameClass,
93 (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_REGEDIT), IMAGE_ICON,
94 GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED)
95 };
96 ATOM hFrameWndClass = RegisterClassEx(&wcFrame); /* register frame window class */
97
98 WNDCLASSEX wcChild = {
99 sizeof(WNDCLASSEX),
100 CS_HREDRAW | CS_VREDRAW/*style*/,
101 ChildWndProc,
102 0/*cbClsExtra*/,
103 sizeof(HANDLE)/*cbWndExtra*/,
104 hInstance,
105 LoadIcon(hInstance, MAKEINTRESOURCE(IDI_REGEDIT)),
106 LoadCursor(0, IDC_ARROW),
107 0/*hbrBackground*/,
108 0/*lpszMenuName*/,
109 szChildClass,
110 (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_REGEDIT), IMAGE_ICON,
111 GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED)
112
113 };
114 ATOM hChildWndClass = RegisterClassEx(&wcChild); /* register child windows class */
115 hChildWndClass = hChildWndClass; /* warning eater */
116
117 RegisterHexEditorClass(hInstance);
118
119 hMenuFrame = LoadMenu(hInstance, MAKEINTRESOURCE(IDR_REGEDIT_MENU));
120 hPopupMenus = LoadMenu(hInstance, MAKEINTRESOURCE(IDR_POPUP_MENUS));
121
122 /* Initialize the Windows Common Controls DLL */
123 InitCommonControls();
124
125 hEditMenu = GetSubMenu(hMenuFrame, 1);
126
127 AclUiAvailable = InitializeAclUiDll();
128 if(!AclUiAvailable)
129 {
130 /* hide the Edit/Permissions... menu entry */
131 if(hEditMenu != NULL)
132 {
133 RemoveMenu(hEditMenu, ID_EDIT_PERMISSIONS, MF_BYCOMMAND);
134 /* remove the separator after the menu item */
135 RemoveMenu(hEditMenu, 4, MF_BYPOSITION);
136 }
137 }
138
139 if(hEditMenu != NULL)
140 SetMenuDefaultItem(hEditMenu, ID_EDIT_MODIFY, MF_BYCOMMAND);
141
142 nClipboardFormat = RegisterClipboardFormat(strClipboardFormat);
143 /* if (nClipboardFormat == 0) {
144 DWORD dwError = GetLastError();
145 } */
146
147 hFrameWnd = CreateWindowEx(WS_EX_WINDOWEDGE, (LPCTSTR)(int)hFrameWndClass, szTitle,
148 WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
149 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
150 NULL, hMenuFrame, hInstance, NULL/*lpParam*/);
151
152 if (!hFrameWnd) {
153 return FALSE;
154 }
155
156 /* Create the status bar */
157 hStatusBar = CreateStatusWindow(WS_VISIBLE|WS_CHILD|WS_CLIPSIBLINGS|SBT_NOBORDERS,
158 _T(""), hFrameWnd, STATUS_WINDOW);
159 if (hStatusBar) {
160 /* Create the status bar panes */
161 SetupStatusBar(hFrameWnd, FALSE);
162 CheckMenuItem(GetSubMenu(hMenuFrame, ID_VIEW_MENU), ID_VIEW_STATUSBAR, MF_BYCOMMAND|MF_CHECKED);
163 }
164
165 /* Restore position */
166 if (RegQueryStringValue(HKEY_CURRENT_USER, g_szGeneralRegKey,
167 _T("LastKey"),
168 szBuffer, sizeof(szBuffer) / sizeof(szBuffer[0])) == ERROR_SUCCESS)
169 {
170 SelectNode(g_pChildWnd->hTreeWnd, szBuffer);
171 }
172
173 ShowWindow(hFrameWnd, nCmdShow);
174 UpdateWindow(hFrameWnd);
175 return TRUE;
176 }
177
178 /******************************************************************************/
179
180 /* we need to destroy the main menu before destroying the main window
181 to avoid a memory leak */
182
183 void DestroyMainMenu() {
184 DestroyMenu(hMenuFrame);
185 }
186
187 /******************************************************************************/
188
189 void ExitInstance(HINSTANCE hInstance)
190 {
191 UnregisterHexEditorClass(hInstance);
192
193 DestroyMenu(hPopupMenus);
194 UnloadAclUiDll();
195 }
196
197 BOOL TranslateChildTabMessage(MSG *msg)
198 {
199 if (msg->message != WM_KEYDOWN) return FALSE;
200 if (msg->wParam != VK_TAB) return FALSE;
201 if (GetParent(msg->hwnd) != g_pChildWnd->hWnd) return FALSE;
202 PostMessage(g_pChildWnd->hWnd, WM_COMMAND, ID_SWITCH_PANELS, 0);
203 return TRUE;
204 }
205
206 int APIENTRY WinMain(HINSTANCE hInstance,
207 HINSTANCE hPrevInstance,
208 LPSTR lpCmdLine,
209 int nCmdShow)
210 {
211 MSG msg;
212 HACCEL hAccel;
213
214 /*
215 int hCrt;
216 FILE *hf;
217 AllocConsole();
218 hCrt = _open_osfhandle((long)GetStdHandle(STD_OUTPUT_HANDLE), _O_TEXT);
219 hf = _fdopen(hCrt, "w");
220 *stdout = *hf;
221 setvbuf(stdout, NULL, _IONBF, 0);
222
223 wprintf(L"command line exit, hInstance = %d\n", hInstance);
224 getch();
225 FreeConsole();
226 return 0;
227 */
228
229 if (ProcessCmdLine(lpCmdLine)) {
230 return 0;
231 }
232
233 /* Initialize global strings */
234 LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
235 LoadString(hInstance, IDC_REGEDIT_FRAME, szFrameClass, MAX_LOADSTRING);
236 LoadString(hInstance, IDC_REGEDIT, szChildClass, MAX_LOADSTRING);
237
238 /* Store instance handle in our global variable */
239 hInst = hInstance;
240
241 /* Perform application initialization */
242 if (!InitInstance(hInstance, nCmdShow)) {
243 return FALSE;
244 }
245 hAccel = LoadAccelerators(hInstance, MAKEINTRESOURCE(ID_ACCEL));
246
247 /* Main message loop */
248 while (GetMessage(&msg, (HWND)NULL, 0, 0)) {
249 if (!TranslateAccelerator(hFrameWnd, hAccel, &msg)
250 && !TranslateChildTabMessage(&msg)) {
251 TranslateMessage(&msg);
252 DispatchMessage(&msg);
253 }
254 }
255
256 ExitInstance(hInstance);
257 return msg.wParam;
258 }