* Sync with recent trunk (r52637).
[reactos.git] / 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <regedit.h>
22
23 BOOL ProcessCmdLine(LPWSTR 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
69 ZeroMemory(&wcFrame, sizeof(WNDCLASSEX));
70 wcFrame.cbSize = sizeof(WNDCLASSEX);
71 wcFrame.lpfnWndProc = FrameWndProc;
72 wcFrame.hInstance = hInstance;
73 wcFrame.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_REGEDIT));
74 wcFrame.hIconSm = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_REGEDIT),
75 IMAGE_ICON, GetSystemMetrics(SM_CXSMICON),
76 GetSystemMetrics(SM_CYSMICON), LR_SHARED);
77 wcFrame.hCursor = LoadCursor(0, IDC_ARROW);
78 wcFrame.lpszClassName = szFrameClass;
79
80 hFrameWndClass = RegisterClassEx(&wcFrame); /* register frame window class */
81
82 ZeroMemory(&wcChild, sizeof(WNDCLASSEX));
83 wcChild.cbSize = sizeof(WNDCLASSEX);
84 wcChild.lpfnWndProc = ChildWndProc;
85 wcChild.cbWndExtra = sizeof(HANDLE);
86 wcChild.hInstance = hInstance;
87 wcChild.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_REGEDIT));
88 wcChild.hCursor = LoadCursor(0, IDC_ARROW),
89 wcChild.lpszClassName = szChildClass,
90 wcChild.hIconSm = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_REGEDIT), IMAGE_ICON,
91 GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED);
92
93 RegisterClassEx(&wcChild); /* register child windows class */
94
95 RegisterHexEditorClass(hInstance);
96
97 hMenuFrame = LoadMenu(hInstance, MAKEINTRESOURCE(IDR_REGEDIT_MENU));
98 hPopupMenus = LoadMenu(hInstance, MAKEINTRESOURCE(IDR_POPUP_MENUS));
99
100 /* Initialize the Windows Common Controls DLL */
101 InitCommonControls();
102
103 hEditMenu = GetSubMenu(hMenuFrame, 1);
104
105 AclUiAvailable = InitializeAclUiDll();
106 if(!AclUiAvailable)
107 {
108 /* hide the Edit/Permissions... menu entry */
109 if(hEditMenu != NULL)
110 {
111 RemoveMenu(hEditMenu, ID_EDIT_PERMISSIONS, MF_BYCOMMAND);
112 /* remove the separator after the menu item */
113 RemoveMenu(hEditMenu, 4, MF_BYPOSITION);
114 }
115 }
116
117 if(hEditMenu != NULL)
118 SetMenuDefaultItem(hEditMenu, ID_EDIT_MODIFY, MF_BYCOMMAND);
119
120 nClipboardFormat = RegisterClipboardFormat(strClipboardFormat);
121 /* if (nClipboardFormat == 0) {
122 DWORD dwError = GetLastError();
123 } */
124
125 hFrameWnd = CreateWindowEx(WS_EX_WINDOWEDGE, (LPCTSTR)(UlongToPtr(hFrameWndClass)), szTitle,
126 WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
127 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
128 NULL, hMenuFrame, hInstance, NULL/*lpParam*/);
129
130 if (!hFrameWnd)
131 {
132 return FALSE;
133 }
134
135 /* Create the status bar */
136 hStatusBar = CreateStatusWindow(WS_VISIBLE|WS_CHILD|WS_CLIPSIBLINGS|WS_CLIPCHILDREN|SBT_NOBORDERS,
137 _T(""), hFrameWnd, STATUS_WINDOW);
138 if (hStatusBar)
139 {
140 /* Create the status bar panes */
141 SetupStatusBar(hFrameWnd, FALSE);
142 CheckMenuItem(GetSubMenu(hMenuFrame, ID_VIEW_MENU), ID_VIEW_STATUSBAR, MF_BYCOMMAND|MF_CHECKED);
143 }
144
145 /* Restore position */
146 if (QueryStringValue(HKEY_CURRENT_USER, g_szGeneralRegKey, _T("LastKey"),
147 szBuffer, COUNT_OF(szBuffer)) == ERROR_SUCCESS)
148 {
149 SelectNode(g_pChildWnd->hTreeWnd, szBuffer);
150 }
151
152 ShowWindow(hFrameWnd, nCmdShow);
153 UpdateWindow(hFrameWnd);
154 return TRUE;
155 }
156
157 /******************************************************************************/
158
159 /* we need to destroy the main menu before destroying the main window
160 to avoid a memory leak */
161
162 void DestroyMainMenu()
163 {
164 DestroyMenu(hMenuFrame);
165 }
166
167 /******************************************************************************/
168
169 void ExitInstance(HINSTANCE hInstance)
170 {
171 UnregisterHexEditorClass(hInstance);
172
173 DestroyMenu(hPopupMenus);
174 UnloadAclUiDll();
175 }
176
177 BOOL TranslateChildTabMessage(MSG *msg)
178 {
179 if (msg->message != WM_KEYDOWN) return FALSE;
180 if (msg->wParam != VK_TAB) return FALSE;
181 if (GetParent(msg->hwnd) != g_pChildWnd->hWnd) return FALSE;
182 PostMessage(g_pChildWnd->hWnd, WM_COMMAND, ID_SWITCH_PANELS, 0);
183 return TRUE;
184 }
185
186 int APIENTRY wWinMain(HINSTANCE hInstance,
187 HINSTANCE hPrevInstance,
188 LPWSTR lpCmdLine,
189 int nCmdShow)
190 {
191 MSG msg;
192 HACCEL hAccel;
193
194 UNREFERENCED_PARAMETER(hPrevInstance);
195
196 if (ProcessCmdLine(lpCmdLine))
197 {
198 return 0;
199 }
200
201 /* Initialize global strings */
202 LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
203 LoadString(hInstance, IDC_REGEDIT_FRAME, szFrameClass, MAX_LOADSTRING);
204 LoadString(hInstance, IDC_REGEDIT, szChildClass, MAX_LOADSTRING);
205
206 /* Store instance handle in our global variable */
207 hInst = hInstance;
208
209 /* Perform application initialization */
210 if (!InitInstance(hInstance, nCmdShow))
211 {
212 return FALSE;
213 }
214 hAccel = LoadAccelerators(hInstance, MAKEINTRESOURCE(ID_ACCEL));
215
216 /* Main message loop */
217 while (GetMessage(&msg, (HWND)NULL, 0, 0))
218 {
219 if (!TranslateAccelerator(hFrameWnd, hAccel, &msg)
220 && !TranslateChildTabMessage(&msg))
221 {
222 TranslateMessage(&msg);
223 DispatchMessage(&msg);
224 }
225 }
226
227 ExitInstance(hInstance);
228 return (int) msg.wParam;
229 }