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