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