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