4f99a70fe7864b52ad315e63cdb9b38a9cbf28f6
[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
54
55 #define MAX_LOADSTRING 100
56 TCHAR szTitle[MAX_LOADSTRING];
57 TCHAR szFrameClass[MAX_LOADSTRING];
58 TCHAR szChildClass[MAX_LOADSTRING];
59
60
61 /*******************************************************************************
62 *
63 *
64 * FUNCTION: InitInstance(HANDLE, int)
65 *
66 * PURPOSE: Saves instance handle and creates main window
67 *
68 * COMMENTS:
69 *
70 * In this function, we save the instance handle in a global variable and
71 * create and display the main program window.
72 */
73
74 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
75 {
76 BOOL AclUiAvailable;
77 HMENU hEditMenu;
78 TCHAR szBuffer[256];
79 LPCTSTR s;
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,
167 _T("Software\\Microsoft\\Windows\\CurrentVersion\\Applets\\Regedit"),
168 _T("LastKey"),
169 szBuffer, sizeof(szBuffer) / sizeof(szBuffer[0])) == ERROR_SUCCESS)
170 {
171 s = szBuffer;
172 if (!_tcsncmp(s, _T("My Computer\\"), 12))
173 s += 12;
174 SelectNode(g_pChildWnd->hTreeWnd, s);
175 }
176
177 ShowWindow(hFrameWnd, nCmdShow);
178 UpdateWindow(hFrameWnd);
179 return TRUE;
180 }
181
182 /******************************************************************************/
183
184 void ExitInstance(HINSTANCE hInstance)
185 {
186 UnregisterHexEditorClass(hInstance);
187 DestroyMenu(hMenuFrame);
188 DestroyMenu(hPopupMenus);
189 UnloadAclUiDll();
190 }
191
192 BOOL TranslateChildTabMessage(MSG *msg)
193 {
194 if (msg->message != WM_KEYDOWN) return FALSE;
195 if (msg->wParam != VK_TAB) return FALSE;
196 if (GetParent(msg->hwnd) != g_pChildWnd->hWnd) return FALSE;
197 PostMessage(g_pChildWnd->hWnd, WM_COMMAND, ID_SWITCH_PANELS, 0);
198 return TRUE;
199 }
200
201 int APIENTRY WinMain(HINSTANCE hInstance,
202 HINSTANCE hPrevInstance,
203 LPSTR lpCmdLine,
204 int nCmdShow)
205 {
206 MSG msg;
207 HACCEL hAccel;
208
209 /*
210 int hCrt;
211 FILE *hf;
212 AllocConsole();
213 hCrt = _open_osfhandle((long)GetStdHandle(STD_OUTPUT_HANDLE), _O_TEXT);
214 hf = _fdopen(hCrt, "w");
215 *stdout = *hf;
216 setvbuf(stdout, NULL, _IONBF, 0);
217
218 wprintf(L"command line exit, hInstance = %d\n", hInstance);
219 getch();
220 FreeConsole();
221 return 0;
222 */
223
224 if (ProcessCmdLine(lpCmdLine)) {
225 return 0;
226 }
227
228 /* Initialize global strings */
229 LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
230 LoadString(hInstance, IDC_REGEDIT_FRAME, szFrameClass, MAX_LOADSTRING);
231 LoadString(hInstance, IDC_REGEDIT, szChildClass, MAX_LOADSTRING);
232
233 /* Store instance handle in our global variable */
234 hInst = hInstance;
235
236 /* Perform application initialization */
237 if (!InitInstance(hInstance, nCmdShow)) {
238 return FALSE;
239 }
240 hAccel = LoadAccelerators(hInstance, (LPCTSTR)IDC_REGEDIT);
241
242 /* Main message loop */
243 while (GetMessage(&msg, (HWND)NULL, 0, 0)) {
244 if (!TranslateAccelerator(msg.hwnd, hAccel, &msg)
245 && !TranslateChildTabMessage(&msg)) {
246 TranslateMessage(&msg);
247 DispatchMessage(&msg);
248 }
249 }
250
251 ExitInstance(hInstance);
252 return msg.wParam;
253 }