Weekly spelling fixes.
[wine] / programs / 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 #define REGEDIT_DECLARE_FUNCTIONS
31 #include "main.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 UINT nClipboardFormat;
46 LPCTSTR strClipboardFormat = _T("TODO: SET CORRECT FORMAT");
47
48
49 TCHAR szTitle[MAX_LOADSTRING];
50 TCHAR szFrameClass[MAX_LOADSTRING];
51 TCHAR szChildClass[MAX_LOADSTRING];
52
53
54 /*******************************************************************************
55  *
56  *   FUNCTION: DynamicBind( void )
57  *
58  *   PURPOSE: Binds all functions dependent on user32.dll
59  */
60 static BOOL DynamicBind( void )
61 {
62     HMODULE dll;
63
64 #define d(x)                                                             \
65     p##x = (typeof (x) ) GetProcAddress( dll, #x );                      \
66     if( ! p##x )                                                         \
67     {                                                                    \
68         fprintf(stderr,"failed to bind function at line %d\n",__LINE__); \
69         return FALSE;                                                    \
70     }                                                                    \
71
72
73     dll = LoadLibrary("user32");
74     if( !dll )
75         return FALSE;
76
77     d(BeginDeferWindowPos)
78     d(BeginPaint)
79     d(CallWindowProcA)
80     d(CheckMenuItem)
81     d(CloseClipboard)
82     d(CreateWindowExA)
83     d(DefWindowProcA)
84     d(DeferWindowPos)
85     d(DestroyMenu)
86     d(DestroyWindow)
87     d(DialogBoxParamA)
88     d(DispatchMessageA)
89     d(EmptyClipboard)
90     d(EndDeferWindowPos)
91     d(EndDialog)
92     d(EndPaint)
93     d(FillRect)
94     d(GetCapture)
95     d(GetClientRect)
96     d(GetCursorPos)
97     d(GetDC)
98     d(GetDlgItem)
99     d(GetMenu)
100     d(GetMessageA)
101     d(GetSubMenu)
102     d(GetSystemMetrics)
103     d(InvertRect)
104     d(IsWindowVisible)
105     d(LoadAcceleratorsA)
106     d(LoadBitmapA)
107     d(LoadCursorA)
108     d(LoadIconA)
109     d(LoadImageA)
110     d(LoadMenuA)
111     d(LoadStringA)
112     d(MessageBeep)
113     d(MoveWindow)
114     d(OpenClipboard)
115     d(PostQuitMessage)
116     d(RegisterClassExA)
117     d(RegisterClipboardFormatA)
118     d(ReleaseCapture)
119     d(ReleaseDC)
120     d(ScreenToClient)
121     d(SendMessageA)
122     d(SetCapture)
123     d(SetCursor)
124     d(SetFocus)
125     d(SetWindowLongA)
126     d(SetWindowTextA)
127     d(ShowWindow)
128     d(TranslateAccelerator)
129     d(TranslateMessage)
130     d(UpdateWindow)
131     d(WinHelpA)
132     d(wsprintfA)
133
134     dll = LoadLibrary("gdi32");
135     if( !dll )
136         return FALSE;
137
138     d(DeleteDC)
139     d(DeleteObject)
140     d(GetStockObject)
141
142     dll = LoadLibrary("comctl32");
143     if( !dll )
144         return FALSE;
145
146     d(CreateStatusWindowA)
147     d(ImageList_Add)
148     d(ImageList_Create)
149     d(ImageList_GetImageCount)
150     d(InitCommonControls)
151
152     dll = LoadLibrary("comdlg32");
153     if( !dll )
154         return FALSE;
155
156     d(CommDlgExtendedError)
157     d(GetOpenFileNameA)
158     d(GetSaveFileNameA)
159     d(PrintDlgA)
160
161     return TRUE;
162 }
163
164 /*******************************************************************************
165  *
166  *
167  *   FUNCTION: InitInstance(HANDLE, int)
168  *
169  *   PURPOSE: Saves instance handle and creates main window
170  *
171  *   COMMENTS:
172  *
173  *        In this function, we save the instance handle in a global variable and
174  *        create and display the main program window.
175  */
176
177 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
178 {
179     WNDCLASSEX wcFrame = {
180         sizeof(WNDCLASSEX),
181         CS_HREDRAW | CS_VREDRAW/*style*/,
182         FrameWndProc,
183         0/*cbClsExtra*/,
184         0/*cbWndExtra*/,
185         hInstance,
186         LoadIcon(hInstance, MAKEINTRESOURCE(IDI_REGEDIT)),
187         LoadCursor(0, IDC_ARROW),
188         0/*hbrBackground*/,
189         0/*lpszMenuName*/,
190         szFrameClass,
191         (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_REGEDIT), IMAGE_ICON,
192             GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED)
193     };
194     ATOM hFrameWndClass = RegisterClassEx(&wcFrame); /* register frame window class */
195
196     WNDCLASSEX wcChild = {
197         sizeof(WNDCLASSEX),
198         CS_HREDRAW | CS_VREDRAW/*style*/,
199         ChildWndProc,
200         0/*cbClsExtra*/,
201         sizeof(HANDLE)/*cbWndExtra*/,
202         hInstance,
203         LoadIcon(hInstance, MAKEINTRESOURCE(IDI_REGEDIT)),
204         LoadCursor(0, IDC_ARROW),
205         0/*hbrBackground*/,
206         0/*lpszMenuName*/,
207         szChildClass,
208         (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_REGEDIT), IMAGE_ICON,
209             GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED)
210
211     };
212     ATOM hChildWndClass = RegisterClassEx(&wcChild); /* register child windows class */
213     hChildWndClass = hChildWndClass; /* warning eater */
214
215         hMenuFrame = LoadMenu(hInstance, MAKEINTRESOURCE(IDR_REGEDIT_MENU));
216
217     /* Initialize the Windows Common Controls DLL */
218     InitCommonControls();
219
220     nClipboardFormat = RegisterClipboardFormat(strClipboardFormat);
221     /* if (nClipboardFormat == 0) {
222         DWORD dwError = GetLastError();
223     } */
224
225     hFrameWnd = CreateWindowEx(0, (LPCTSTR)(int)hFrameWndClass, szTitle,
226                     WS_OVERLAPPEDWINDOW | WS_EX_CLIENTEDGE,
227                     CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
228                     NULL, hMenuFrame, hInstance, NULL/*lpParam*/);
229
230     if (!hFrameWnd) {
231         return FALSE;
232     }
233
234     /* Create the status bar */
235     hStatusBar = CreateStatusWindow(WS_VISIBLE|WS_CHILD|WS_CLIPSIBLINGS|SBT_NOBORDERS,
236                                     _T(""), hFrameWnd, STATUS_WINDOW);
237     if (hStatusBar) {
238         /* Create the status bar panes */
239         SetupStatusBar(hFrameWnd, FALSE);
240         CheckMenuItem(GetSubMenu(hMenuFrame, ID_VIEW_MENU), ID_VIEW_STATUSBAR, MF_BYCOMMAND|MF_CHECKED);
241     }
242     ShowWindow(hFrameWnd, nCmdShow);
243     UpdateWindow(hFrameWnd);
244     return TRUE;
245 }
246
247 /******************************************************************************/
248
249 void ExitInstance(void)
250 {
251     DestroyMenu(hMenuFrame);
252 }
253
254 int APIENTRY WinMain(HINSTANCE hInstance,
255                      HINSTANCE hPrevInstance,
256                      LPSTR     lpCmdLine,
257                      int       nCmdShow)
258 {
259     MSG msg;
260     HACCEL hAccel;
261 /*
262     int hCrt;
263     FILE *hf;
264     AllocConsole();
265     hCrt = _open_osfhandle((long)GetStdHandle(STD_OUTPUT_HANDLE), _O_TEXT);
266     hf = _fdopen(hCrt, "w");
267     *stdout = *hf;
268     setvbuf(stdout, NULL, _IONBF, 0);
269
270         wprintf(L"command line exit, hInstance = %d\n", hInstance);
271         getch();
272         FreeConsole();
273     return 0;
274  */
275
276     if (ProcessCmdLine(lpCmdLine)) {
277         return 0;
278     }
279
280     if (!DynamicBind()) {
281         return 0;
282     }
283
284     /* Initialize global strings */
285     LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
286     LoadString(hInstance, IDC_REGEDIT_FRAME, szFrameClass, MAX_LOADSTRING);
287     LoadString(hInstance, IDC_REGEDIT, szChildClass, MAX_LOADSTRING);
288
289     /* Store instance handle in our global variable */
290     hInst = hInstance;
291
292     /* Perform application initialization */
293     if (!InitInstance(hInstance, nCmdShow)) {
294         return FALSE;
295     }
296     hAccel = LoadAccelerators(hInstance, (LPCTSTR)IDC_REGEDIT);
297
298     /* Main message loop */
299     while (GetMessage(&msg, (HWND)NULL, 0, 0)) {
300         if (!TranslateAccelerator(msg.hwnd, hAccel, &msg)) {
301             TranslateMessage(&msg);
302             DispatchMessage(&msg);
303         }
304     }
305     ExitInstance();
306     return msg.wParam;
307 }