[REGEDIT]
[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., 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 LPCWSTR strClipboardFormat = L"TODO: SET CORRECT FORMAT";
37
38 #define MAX_LOADSTRING 100
39 WCHAR szTitle[MAX_LOADSTRING];
40 WCHAR szFrameClass[MAX_LOADSTRING];
41 WCHAR szChildClass[MAX_LOADSTRING];
42
43
44 /*******************************************************************************
45 *
46 *
47 * FUNCTION: InitInstance(HANDLE, int)
48 *
49 * PURPOSE: Saves instance handle and creates main window
50 *
51 * COMMENTS:
52 *
53 * In this function, we save the instance handle in a global variable and
54 * create and display the main program window.
55 */
56
57 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
58 {
59 BOOL AclUiAvailable;
60 HMENU hEditMenu;
61
62 WNDCLASSEXW wcFrame;
63 WNDCLASSEXW wcChild;
64 ATOM hFrameWndClass;
65
66 ZeroMemory(&wcFrame, sizeof(WNDCLASSEXW));
67 wcFrame.cbSize = sizeof(WNDCLASSEXW);
68 wcFrame.lpfnWndProc = FrameWndProc;
69 wcFrame.hInstance = hInstance;
70 wcFrame.hIcon = LoadIconW(hInstance, MAKEINTRESOURCEW(IDI_REGEDIT));
71 wcFrame.hIconSm = (HICON)LoadImageW(hInstance, MAKEINTRESOURCEW(IDI_REGEDIT),
72 IMAGE_ICON, GetSystemMetrics(SM_CXSMICON),
73 GetSystemMetrics(SM_CYSMICON), LR_SHARED);
74 wcFrame.hCursor = LoadCursorW(NULL, IDC_ARROW);
75 wcFrame.lpszClassName = szFrameClass;
76
77 hFrameWndClass = RegisterClassExW(&wcFrame); /* register frame window class */
78
79 ZeroMemory(&wcChild, sizeof(WNDCLASSEXW));
80 wcChild.cbSize = sizeof(WNDCLASSEXW);
81 wcChild.lpfnWndProc = ChildWndProc;
82 wcChild.cbWndExtra = sizeof(HANDLE);
83 wcChild.hInstance = hInstance;
84 wcChild.hIcon = LoadIconW(hInstance, MAKEINTRESOURCEW(IDI_REGEDIT));
85 wcChild.hCursor = LoadCursorW(NULL, IDC_ARROW);
86 wcChild.lpszClassName = szChildClass;
87 wcChild.hIconSm = (HICON)LoadImageW(hInstance, MAKEINTRESOURCEW(IDI_REGEDIT),
88 IMAGE_ICON, GetSystemMetrics(SM_CXSMICON),
89 GetSystemMetrics(SM_CYSMICON), LR_SHARED);
90
91 RegisterClassExW(&wcChild); /* register child windows class */
92
93 RegisterHexEditorClass(hInstance);
94
95 hMenuFrame = LoadMenuW(hInstance, MAKEINTRESOURCEW(IDR_REGEDIT_MENU));
96 hPopupMenus = LoadMenuW(hInstance, MAKEINTRESOURCEW(IDR_POPUP_MENUS));
97
98 /* Initialize the Windows Common Controls DLL */
99 // TODO: Replace this call by InitCommonControlsEx(_something_)
100 InitCommonControls();
101
102 hEditMenu = GetSubMenu(hMenuFrame, 1);
103
104 AclUiAvailable = InitializeAclUiDll();
105 if(!AclUiAvailable)
106 {
107 /* hide the Edit/Permissions... menu entry */
108 if(hEditMenu != NULL)
109 {
110 RemoveMenu(hEditMenu, ID_EDIT_PERMISSIONS, MF_BYCOMMAND);
111 /* remove the separator after the menu item */
112 RemoveMenu(hEditMenu, 4, MF_BYPOSITION);
113 }
114 }
115
116 if(hEditMenu != NULL)
117 SetMenuDefaultItem(hEditMenu, ID_EDIT_MODIFY, MF_BYCOMMAND);
118
119 nClipboardFormat = RegisterClipboardFormatW(strClipboardFormat);
120 /* if (nClipboardFormat == 0) {
121 DWORD dwError = GetLastError();
122 } */
123
124 hFrameWnd = CreateWindowExW(WS_EX_WINDOWEDGE, (LPCWSTR)(UlongToPtr(hFrameWndClass)), szTitle,
125 WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
126 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
127 NULL, hMenuFrame, hInstance, NULL/*lpParam*/);
128
129 if (!hFrameWnd)
130 {
131 return FALSE;
132 }
133
134 /* Create the status bar */
135 hStatusBar = CreateStatusWindowW(WS_VISIBLE | WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | SBT_NOBORDERS,
136 L"", hFrameWnd, STATUS_WINDOW);
137 if (hStatusBar)
138 {
139 /* Create the status bar panes */
140 SetupStatusBar(hFrameWnd, FALSE);
141 CheckMenuItem(GetSubMenu(hMenuFrame, ID_VIEW_MENU), ID_VIEW_STATUSBAR, MF_BYCOMMAND | MF_CHECKED);
142 }
143
144 LoadSettings();
145 UpdateWindow(hFrameWnd);
146 return TRUE;
147 }
148
149 /******************************************************************************/
150
151 /*
152 * We need to destroy the main menu before destroying the main window
153 * to avoid a memory leak.
154 */
155
156 void DestroyMainMenu()
157 {
158 DestroyMenu(hMenuFrame);
159 }
160
161 /******************************************************************************/
162
163 void ExitInstance(HINSTANCE hInstance)
164 {
165 UnregisterHexEditorClass(hInstance);
166
167 DestroyMenu(hPopupMenus);
168 UnloadAclUiDll();
169 }
170
171 BOOL TranslateChildTabMessage(PMSG msg)
172 {
173 if (msg->message != WM_KEYDOWN) return FALSE;
174
175 /* Allow Ctrl+A on address bar */
176 if ((msg->hwnd == g_pChildWnd->hAddressBarWnd) &&
177 (msg->message == WM_KEYDOWN) &&
178 (msg->wParam == L'A') && (GetKeyState(VK_CONTROL) < 0))
179 {
180 SendMessageW(msg->hwnd, EM_SETSEL, 0, -1);
181 return TRUE;
182 }
183
184 if (msg->wParam != VK_TAB) return FALSE;
185 if (GetParent(msg->hwnd) != g_pChildWnd->hWnd) return FALSE;
186 PostMessageW(g_pChildWnd->hWnd, WM_COMMAND, ID_SWITCH_PANELS, 0);
187 return TRUE;
188 }
189
190 int APIENTRY wWinMain(HINSTANCE hInstance,
191 HINSTANCE hPrevInstance,
192 LPWSTR lpCmdLine,
193 int nCmdShow)
194 {
195 MSG msg;
196 HACCEL hAccel;
197
198 UNREFERENCED_PARAMETER(hPrevInstance);
199
200 InfoMessageBox(NULL, MB_OK | MB_ICONERROR, L"Main", L"Main '%1'", lpCmdLine);
201 if (ProcessCmdLine(lpCmdLine))
202 {
203 return 0;
204 }
205
206 /* Initialize global strings */
207 LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
208 LoadStringW(hInstance, IDC_REGEDIT_FRAME, szFrameClass, MAX_LOADSTRING);
209 LoadStringW(hInstance, IDC_REGEDIT, szChildClass, MAX_LOADSTRING);
210
211 /* Store instance handle in our global variable */
212 hInst = hInstance;
213
214 /* Perform application initialization */
215 if (!InitInstance(hInstance, nCmdShow))
216 {
217 return FALSE;
218 }
219 hAccel = LoadAcceleratorsW(hInstance, MAKEINTRESOURCEW(ID_ACCEL));
220
221 /* Main message loop */
222 while (GetMessageW(&msg, NULL, 0, 0))
223 {
224 if (!TranslateAcceleratorW(hFrameWnd, hAccel, &msg) &&
225 !TranslateChildTabMessage(&msg))
226 {
227 TranslateMessage(&msg);
228 DispatchMessageW(&msg);
229 }
230 }
231
232 ExitInstance(hInstance);
233 return (int)msg.wParam;
234 }
235
236 /* EOF */