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