Added new template multiple document interface application.
[reactos.git] / rosapps / templates / mdi / main.c
1 /*
2 * ReactOS Application
3 *
4 * main.c
5 *
6 * Copyright (C) 2002 Robert Dickenson <robd@reactos.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #ifdef _MSC_VER
24 #include "stdafx.h"
25 #else
26 #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
27 #include <windows.h>
28 #include <commctrl.h>
29 #include <stdlib.h>
30 #include <malloc.h>
31 #include <memory.h>
32 #include <tchar.h>
33 #include <process.h>
34 #include <stdio.h>
35 #endif
36
37 #include "main.h"
38 #include "framewnd.h"
39 #include "childwnd.h"
40
41
42 ////////////////////////////////////////////////////////////////////////////////
43 // Global Variables:
44 //
45
46 HINSTANCE hInst;
47 HACCEL hAccel;
48 HWND hFrameWnd;
49 HWND hMDIClient;
50 HMENU hMenuFrame;
51 HWND hStatusBar;
52 HWND hToolBar;
53 HFONT hFont;
54
55 TCHAR szTitle[MAX_LOADSTRING];
56 TCHAR szFrameClass[MAX_LOADSTRING];
57 TCHAR szChildClass[MAX_LOADSTRING];
58
59
60 ////////////////////////////////////////////////////////////////////////////////
61 //
62 //
63 // FUNCTION: InitInstance(HANDLE, int)
64 //
65 // PURPOSE: Saves instance handle and creates main window
66 //
67 // COMMENTS:
68 //
69 // In this function, we save the instance handle in a global variable and
70 // create and display the main program window.
71 //
72 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
73 {
74 // ChildWnd* pChildWnd;
75
76 WNDCLASSEX wcFrame = {
77 sizeof(WNDCLASSEX),
78 CS_HREDRAW | CS_VREDRAW/*style*/,
79 FrameWndProc,
80 0/*cbClsExtra*/,
81 0/*cbWndExtra*/,
82 hInstance,
83 LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MDI_APP)),
84 LoadCursor(0, IDC_ARROW),
85 0/*hbrBackground*/,
86 0/*lpszMenuName*/,
87 szFrameClass,
88 (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_MDI_APP), IMAGE_ICON,
89 GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED)
90 };
91 ATOM hFrameWndClass = RegisterClassEx(&wcFrame); // register frame window class
92 #if 0
93 WNDCLASS wcChild = {
94 CS_CLASSDC|CS_DBLCLKS|CS_VREDRAW,
95 ChildWndProc,
96 0/*cbClsExtra*/,
97 0/*cbWndExtra*/,
98 hInstance,
99 0/*hIcon*/,
100 LoadCursor(0, IDC_ARROW),
101 0/*hbrBackground*/,
102 0/*lpszMenuName*/,
103 szChildClass
104 };
105 ATOM hChildWndClass = RegisterClass(&wcChild); // register child windows class
106 #else
107 WNDCLASSEX wcChild = {
108 sizeof(WNDCLASSEX),
109 CS_HREDRAW | CS_VREDRAW/*style*/,
110 ChildWndProc,
111 0/*cbClsExtra*/,
112 sizeof(HANDLE)/*cbWndExtra*/,
113 hInstance,
114 LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MDI_APP)),
115 LoadCursor(0, IDC_ARROW),
116 0/*hbrBackground*/,
117 0/*lpszMenuName*/,
118 szChildClass,
119 (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDC_MDI_APP), IMAGE_ICON,
120 GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED)
121
122 };
123 ATOM hChildWndClass = RegisterClassEx(&wcChild); // register child windows class
124 #endif
125
126 HMENU hMenu = LoadMenu(hInstance, MAKEINTRESOURCE(IDC_MDI_APP));
127 HMENU hMenuOptions = GetSubMenu(hMenu, ID_OPTIONS_MENU);
128 HMENU hChildMenu = LoadMenu(hInstance, MAKEINTRESOURCE(IDC_MDI_APP_CHILD));
129
130 INITCOMMONCONTROLSEX icc = {
131 sizeof(INITCOMMONCONTROLSEX),
132 ICC_BAR_CLASSES
133 };
134
135 HDC hdc = GetDC(0);
136
137 hMenuFrame = hMenu;
138 // hMenuView = GetSubMenu(hMenuFrame, ID_VIEW_MENU);
139 hAccel = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MDI_APP));
140 hFont = CreateFont(-MulDiv(8,GetDeviceCaps(hdc,LOGPIXELSY),72), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, _T("MS Sans Serif"));
141 ReleaseDC(0, hdc);
142
143 hFrameWnd = CreateWindowEx(0, (LPCTSTR)(int)hFrameWndClass, szTitle,
144 // hFrameWnd = CreateWindow(szFrameClass, szTitle,
145 WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
146 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
147 NULL/*hWndParent*/, hMenuFrame, hInstance, NULL/*lpParam*/);
148 if (!hFrameWnd) {
149 return FALSE;
150 }
151
152 if (InitCommonControlsEx(&icc))
153 {
154 int nParts[3];
155 TBBUTTON toolbarBtns[] = {
156 {0, 0, 0, TBSTYLE_SEP},
157 {0, ID_WINDOW_NEW_WINDOW, TBSTATE_ENABLED, TBSTYLE_BUTTON},
158 {1, ID_WINDOW_CASCADE, TBSTATE_ENABLED, TBSTYLE_BUTTON},
159 {2, ID_WINDOW_TILE_HORZ, TBSTATE_ENABLED, TBSTYLE_BUTTON},
160 {3, ID_WINDOW_TILE_VERT, TBSTATE_ENABLED, TBSTYLE_BUTTON},
161 {4, 2/*TODO: ID_...*/, TBSTATE_ENABLED, TBSTYLE_BUTTON},
162 {5, 2/*TODO: ID_...*/, TBSTATE_ENABLED, TBSTYLE_BUTTON},
163 };
164
165 hToolBar = CreateToolbarEx(hFrameWnd, WS_CHILD|WS_VISIBLE,
166 IDC_TOOLBAR, 2, hInstance, IDB_TOOLBAR, toolbarBtns,
167 sizeof(toolbarBtns)/sizeof(TBBUTTON), 16, 15, 16, 15, sizeof(TBBUTTON));
168 CheckMenuItem(hMenuOptions, ID_OPTIONS_TOOLBAR, MF_BYCOMMAND|MF_CHECKED);
169
170 // Create the status bar
171 hStatusBar = CreateStatusWindow(WS_VISIBLE|WS_CHILD|WS_CLIPSIBLINGS|SBT_NOBORDERS,
172 "", hFrameWnd, IDC_STATUSBAR);
173 if (!hStatusBar)
174 return FALSE;
175 CheckMenuItem(hMenuOptions, ID_OPTIONS_STATUSBAR, MF_BYCOMMAND|MF_CHECKED);
176
177 // Create the status bar panes
178 nParts[0] = 100;
179 nParts[1] = 210;
180 nParts[2] = 400;
181 SendMessage(hStatusBar, SB_SETPARTS, 3, (long)nParts);
182 } else {
183 CheckMenuItem(hMenuOptions, ID_OPTIONS_TOOLBAR, MF_BYCOMMAND|MF_GRAYED);
184 CheckMenuItem(hMenuOptions, ID_OPTIONS_STATUSBAR, MF_BYCOMMAND|MF_GRAYED);
185 }
186
187 ShowWindow(hFrameWnd, nCmdShow);
188 UpdateWindow(hFrameWnd);
189 UpdateStatusBar();
190 return TRUE;
191 }
192
193 void UpdateStatusBar(void)
194 {
195 TCHAR text[260];
196 DWORD size;
197
198 size = sizeof(text)/sizeof(TCHAR);
199 GetUserName(text, &size);
200 SendMessage(hStatusBar, SB_SETTEXT, 1, (LPARAM)text);
201 size = sizeof(text)/sizeof(TCHAR);
202 GetComputerName(text, &size);
203 SendMessage(hStatusBar, SB_SETTEXT, 0, (LPARAM)text);
204 }
205
206
207 static int g_foundPrevInstance = 0;
208
209 // search for already running win[e]files
210 static BOOL CALLBACK EnumWndProc(HWND hWnd, LPARAM lParam)
211 {
212 TCHAR cls[128];
213
214 GetClassName(hWnd, cls, 128);
215 if (!lstrcmp(cls, (LPCTSTR)lParam)) {
216 g_foundPrevInstance++;
217 return FALSE;
218 }
219 return TRUE;
220 }
221
222
223 void ExitInstance(void)
224 {
225 DestroyMenu(hMenuFrame);
226 }
227
228
229 int APIENTRY WinMain(HINSTANCE hInstance,
230 HINSTANCE hPrevInstance,
231 LPSTR lpCmdLine,
232 int nCmdShow)
233 {
234 MSG msg;
235 HACCEL hAccel;
236 HWND hMDIClient;
237
238 // Initialize global strings
239 LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
240 LoadString(hInstance, IDC_MDI_APP, szFrameClass, MAX_LOADSTRING);
241 LoadString(hInstance, IDC_MDI_APP_CHILD, szChildClass, MAX_LOADSTRING);
242
243 // Allow only one running instance
244 EnumWindows(EnumWndProc, (LPARAM)szFrameClass);
245 if (g_foundPrevInstance)
246 return 1;
247
248 // Store instance handle in our global variable
249 hInst = hInstance;
250
251 // Perform application initialization:
252 if (!InitInstance(hInstance, nCmdShow)) {
253 return FALSE;
254 }
255 hAccel = LoadAccelerators(hInstance, (LPCTSTR)IDC_MDI_APP);
256 hMDIClient = GetWindow(hFrameWnd, GW_CHILD);
257
258 // Main message loop:
259 while (GetMessage(&msg, (HWND)NULL, 0, 0)) {
260 if (!TranslateMDISysAccel(hMDIClient, &msg) &&
261 !TranslateAccelerator(hFrameWnd/*hwndFrame*/, hAccel, &msg)) {
262 TranslateMessage(&msg);
263 DispatchMessage(&msg);
264 }
265 }
266 ExitInstance();
267 return msg.wParam;
268 }