- Get autochk, calc, cmd, devmgr, expand, format, gettype, hostname, lsass, msconfig...
[reactos.git] / reactos / subsys / system / 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #include <regedit.h>
22
23 BOOL ProcessCmdLine(LPSTR 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 LPCTSTR strClipboardFormat = _T("TODO: SET CORRECT FORMAT");
37 const TCHAR g_szGeneralRegKey[] = _T("Software\\Microsoft\\Windows\\CurrentVersion\\Applets\\Regedit");
38
39
40 #define MAX_LOADSTRING 100
41 TCHAR szTitle[MAX_LOADSTRING];
42 TCHAR szFrameClass[MAX_LOADSTRING];
43 TCHAR szChildClass[MAX_LOADSTRING];
44
45
46 /*******************************************************************************
47 *
48 *
49 * FUNCTION: InitInstance(HANDLE, int)
50 *
51 * PURPOSE: Saves instance handle and creates main window
52 *
53 * COMMENTS:
54 *
55 * In this function, we save the instance handle in a global variable and
56 * create and display the main program window.
57 */
58
59 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
60 {
61 BOOL AclUiAvailable;
62 HMENU hEditMenu;
63 TCHAR szBuffer[256];
64
65 WNDCLASSEX wcFrame = {
66 sizeof(WNDCLASSEX),
67 CS_HREDRAW | CS_VREDRAW/*style*/,
68 FrameWndProc,
69 0/*cbClsExtra*/,
70 0/*cbWndExtra*/,
71 hInstance,
72 LoadIcon(hInstance, MAKEINTRESOURCE(IDI_REGEDIT)),
73 LoadCursor(0, IDC_ARROW),
74 0/*hbrBackground*/,
75 0/*lpszMenuName*/,
76 szFrameClass,
77 (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_REGEDIT), IMAGE_ICON,
78 GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED)
79 };
80 ATOM hFrameWndClass = RegisterClassEx(&wcFrame); /* register frame window class */
81
82 WNDCLASSEX wcChild = {
83 sizeof(WNDCLASSEX),
84 CS_HREDRAW | CS_VREDRAW/*style*/,
85 ChildWndProc,
86 0/*cbClsExtra*/,
87 sizeof(HANDLE)/*cbWndExtra*/,
88 hInstance,
89 LoadIcon(hInstance, MAKEINTRESOURCE(IDI_REGEDIT)),
90 LoadCursor(0, IDC_ARROW),
91 0/*hbrBackground*/,
92 0/*lpszMenuName*/,
93 szChildClass,
94 (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_REGEDIT), IMAGE_ICON,
95 GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED)
96
97 };
98 ATOM hChildWndClass = RegisterClassEx(&wcChild); /* register child windows class */
99 hChildWndClass = hChildWndClass; /* warning eater */
100
101 RegisterHexEditorClass(hInstance);
102
103 hMenuFrame = LoadMenu(hInstance, MAKEINTRESOURCE(IDR_REGEDIT_MENU));
104 hPopupMenus = LoadMenu(hInstance, MAKEINTRESOURCE(IDR_POPUP_MENUS));
105
106 /* Initialize the Windows Common Controls DLL */
107 InitCommonControls();
108
109 hEditMenu = GetSubMenu(hMenuFrame, 1);
110
111 AclUiAvailable = InitializeAclUiDll();
112 if(!AclUiAvailable)
113 {
114 /* hide the Edit/Permissions... menu entry */
115 if(hEditMenu != NULL)
116 {
117 RemoveMenu(hEditMenu, ID_EDIT_PERMISSIONS, MF_BYCOMMAND);
118 /* remove the separator after the menu item */
119 RemoveMenu(hEditMenu, 4, MF_BYPOSITION);
120 }
121 }
122
123 if(hEditMenu != NULL)
124 SetMenuDefaultItem(hEditMenu, ID_EDIT_MODIFY, MF_BYCOMMAND);
125
126 nClipboardFormat = RegisterClipboardFormat(strClipboardFormat);
127 /* if (nClipboardFormat == 0) {
128 DWORD dwError = GetLastError();
129 } */
130
131 hFrameWnd = CreateWindowEx(WS_EX_WINDOWEDGE, (LPCTSTR)(UlongToPtr(hFrameWndClass)), szTitle,
132 WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
133 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
134 NULL, hMenuFrame, hInstance, NULL/*lpParam*/);
135
136 if (!hFrameWnd) {
137 return FALSE;
138 }
139
140 /* Create the status bar */
141 hStatusBar = CreateStatusWindow(WS_VISIBLE|WS_CHILD|WS_CLIPSIBLINGS|SBT_NOBORDERS,
142 _T(""), hFrameWnd, STATUS_WINDOW);
143 if (hStatusBar) {
144 /* Create the status bar panes */
145 SetupStatusBar(hFrameWnd, FALSE);
146 CheckMenuItem(GetSubMenu(hMenuFrame, ID_VIEW_MENU), ID_VIEW_STATUSBAR, MF_BYCOMMAND|MF_CHECKED);
147 }
148
149 /* Restore position */
150 if (RegQueryStringValue(HKEY_CURRENT_USER, g_szGeneralRegKey,
151 _T("LastKey"),
152 szBuffer, sizeof(szBuffer) / sizeof(szBuffer[0])) == ERROR_SUCCESS)
153 {
154 SelectNode(g_pChildWnd->hTreeWnd, szBuffer);
155 }
156
157 ShowWindow(hFrameWnd, nCmdShow);
158 UpdateWindow(hFrameWnd);
159 return TRUE;
160 }
161
162 /******************************************************************************/
163
164 /* we need to destroy the main menu before destroying the main window
165 to avoid a memory leak */
166
167 void DestroyMainMenu() {
168 DestroyMenu(hMenuFrame);
169 }
170
171 /******************************************************************************/
172
173 void ExitInstance(HINSTANCE hInstance)
174 {
175 UnregisterHexEditorClass(hInstance);
176
177 DestroyMenu(hPopupMenus);
178 UnloadAclUiDll();
179 }
180
181 BOOL TranslateChildTabMessage(MSG *msg)
182 {
183 if (msg->message != WM_KEYDOWN) return FALSE;
184 if (msg->wParam != VK_TAB) return FALSE;
185 if (GetParent(msg->hwnd) != g_pChildWnd->hWnd) return FALSE;
186 PostMessage(g_pChildWnd->hWnd, WM_COMMAND, ID_SWITCH_PANELS, 0);
187 return TRUE;
188 }
189
190 int APIENTRY WinMain(HINSTANCE hInstance,
191 HINSTANCE hPrevInstance,
192 LPSTR lpCmdLine,
193 int nCmdShow)
194 {
195 MSG msg;
196 HACCEL hAccel;
197
198 /*
199 int hCrt;
200 FILE *hf;
201 AllocConsole();
202 hCrt = _open_osfhandle((long)GetStdHandle(STD_OUTPUT_HANDLE), _O_TEXT);
203 hf = _fdopen(hCrt, "w");
204 *stdout = *hf;
205 setvbuf(stdout, NULL, _IONBF, 0);
206
207 wprintf(L"command line exit, hInstance = %d\n", hInstance);
208 getch();
209 FreeConsole();
210 return 0;
211 */
212
213 if (ProcessCmdLine(lpCmdLine)) {
214 return 0;
215 }
216
217 /* Initialize global strings */
218 LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
219 LoadString(hInstance, IDC_REGEDIT_FRAME, szFrameClass, MAX_LOADSTRING);
220 LoadString(hInstance, IDC_REGEDIT, szChildClass, MAX_LOADSTRING);
221
222 /* Store instance handle in our global variable */
223 hInst = hInstance;
224
225 /* Perform application initialization */
226 if (!InitInstance(hInstance, nCmdShow)) {
227 return FALSE;
228 }
229 hAccel = LoadAccelerators(hInstance, MAKEINTRESOURCE(ID_ACCEL));
230
231 /* Main message loop */
232 while (GetMessage(&msg, (HWND)NULL, 0, 0)) {
233 if (!TranslateAccelerator(hFrameWnd, hAccel, &msg)
234 && !TranslateChildTabMessage(&msg)) {
235 TranslateMessage(&msg);
236 DispatchMessage(&msg);
237 }
238 }
239
240 ExitInstance(hInstance);
241 return msg.wParam;
242 }