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