xref: /reactos/base/applications/regedit/main.c (revision 58aee30e)
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.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
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.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
87     wcChild.lpszClassName = szChildClass;
88     wcChild.hIconSm = (HICON)LoadImageW(hInstance, MAKEINTRESOURCEW(IDI_REGEDIT),
89                                         IMAGE_ICON, GetSystemMetrics(SM_CXSMICON),
90                                         GetSystemMetrics(SM_CYSMICON), LR_SHARED);
91 
92     RegisterClassExW(&wcChild); /* register child windows class */
93 
94     RegisterHexEditorClass(hInstance);
95 
96     hMenuFrame = LoadMenuW(hInstance, MAKEINTRESOURCEW(IDR_REGEDIT_MENU));
97     hPopupMenus = LoadMenuW(hInstance, MAKEINTRESOURCEW(IDR_POPUP_MENUS));
98 
99     /* Initialize the Windows Common Controls DLL */
100     // TODO: Replace this call by InitCommonControlsEx(_something_)
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 = RegisterClipboardFormatW(strClipboardFormat);
121     /* if (nClipboardFormat == 0) {
122         DWORD dwError = GetLastError();
123     } */
124 
125     hFrameWnd = CreateWindowExW(WS_EX_WINDOWEDGE, (LPCWSTR)(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 = CreateStatusWindowW(WS_VISIBLE | WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | SBT_NOBORDERS,
137                                      L"", 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     LoadSettings();
146     UpdateWindow(hFrameWnd);
147     return TRUE;
148 }
149 
150 /******************************************************************************/
151 
152 /*
153  * We need to destroy the main menu before destroying the main window
154  * to avoid a memory leak.
155  */
156 
157 void DestroyMainMenu()
158 {
159     DestroyMenu(hMenuFrame);
160 }
161 
162 /******************************************************************************/
163 
164 void ExitInstance(HINSTANCE hInstance)
165 {
166     UnregisterHexEditorClass(hInstance);
167 
168     DestroyMenu(hPopupMenus);
169     UnloadAclUiDll();
170 }
171 
172 BOOL TranslateChildTabMessage(PMSG msg)
173 {
174     if (msg->message != WM_KEYDOWN) return FALSE;
175 
176     /* Allow Ctrl+A on address bar */
177     if ((msg->hwnd == g_pChildWnd->hAddressBarWnd) &&
178         (msg->message == WM_KEYDOWN) &&
179         (msg->wParam == L'A') && (GetKeyState(VK_CONTROL) < 0))
180     {
181         SendMessageW(msg->hwnd, EM_SETSEL, 0, -1);
182         return TRUE;
183     }
184 
185     if (msg->wParam != VK_TAB) return FALSE;
186     if (GetParent(msg->hwnd) != g_pChildWnd->hWnd) return FALSE;
187     PostMessageW(g_pChildWnd->hWnd, WM_COMMAND, ID_SWITCH_PANELS, 0);
188     return TRUE;
189 }
190 
191 int APIENTRY wWinMain(HINSTANCE hInstance,
192                       HINSTANCE hPrevInstance,
193                       LPWSTR    lpCmdLine,
194                       int       nCmdShow)
195 {
196     MSG msg;
197     HACCEL hAccel;
198 
199     UNREFERENCED_PARAMETER(hPrevInstance);
200 
201     /* Initialize global strings */
202     LoadStringW(hInstance, IDS_APP_TITLE, szTitle, COUNT_OF(szTitle));
203     LoadStringW(hInstance, IDC_REGEDIT_FRAME, szFrameClass, COUNT_OF(szFrameClass));
204     LoadStringW(hInstance, IDC_REGEDIT, szChildClass, COUNT_OF(szChildClass));
205 
206     if (ProcessCmdLine(lpCmdLine))
207     {
208         return 0;
209     }
210 
211     switch (GetUserDefaultUILanguage())
212     {
213         case MAKELANGID(LANG_HEBREW, SUBLANG_DEFAULT):
214             SetProcessDefaultLayout(LAYOUT_RTL);
215             break;
216 
217         default:
218             break;
219     }
220     /* Store instance handle in our global variable */
221     hInst = hInstance;
222 
223     /* Perform application initialization */
224     if (!InitInstance(hInstance, nCmdShow))
225     {
226         return 0;
227     }
228     hAccel = LoadAcceleratorsW(hInstance, MAKEINTRESOURCEW(ID_ACCEL));
229 
230     /* Main message loop */
231     while (GetMessageW(&msg, NULL, 0, 0))
232     {
233         if (!TranslateAcceleratorW(hFrameWnd, hAccel, &msg) &&
234             !TranslateChildTabMessage(&msg))
235         {
236             TranslateMessage(&msg);
237             DispatchMessageW(&msg);
238         }
239     }
240 
241     ExitInstance(hInstance);
242     return (int)msg.wParam;
243 }
244 
245 /* EOF */
246