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