xref: /reactos/modules/rosapps/templates/mdi/main.c (revision 53221834)
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 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
63 {
64     WNDCLASSEX wcFrame = {
65         sizeof(WNDCLASSEX),
66         CS_HREDRAW | CS_VREDRAW/*style*/,
67         FrameWndProc,
68         0/*cbClsExtra*/,
69         0/*cbWndExtra*/,
70         hInstance,
71         LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MDI_APP)),
72         LoadCursor(0, IDC_ARROW),
73         0/*hbrBackground*/,
74         0/*lpszMenuName*/,
75         szFrameClass,
76         (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_MDI_APP), IMAGE_ICON,
77             GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED)
78     };
79     ATOM hFrameWndClass = RegisterClassEx(&wcFrame); // register frame window class
80 #if 0
81     WNDCLASS wcChild = {
82         CS_CLASSDC|CS_DBLCLKS|CS_VREDRAW,
83         ChildWndProc,
84         0/*cbClsExtra*/,
85         0/*cbWndExtra*/,
86         hInstance,
87         0/*hIcon*/,
88         LoadCursor(0, IDC_ARROW),
89         0/*hbrBackground*/,
90         0/*lpszMenuName*/,
91         szChildClass
92     };
93     ATOM hChildWndClass = RegisterClass(&wcChild); // register child windows class
94 #else
95     // WNDCLASSEX wcChild = {
96     //     sizeof(WNDCLASSEX),
97     //     CS_HREDRAW | CS_VREDRAW/*style*/,
98     //     ChildWndProc,
99     //     0/*cbClsExtra*/,
100     //     sizeof(HANDLE)/*cbWndExtra*/,
101     //     hInstance,
102     //     LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MDI_APP)),
103     //     LoadCursor(0, IDC_ARROW),
104     //     0/*hbrBackground*/,
105     //     0/*lpszMenuName*/,
106     //     szChildClass,
107     //     (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDC_MDI_APP), IMAGE_ICON,
108     //         GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED)
109     // };
110     // ATOM hChildWndClass = RegisterClassEx(&wcChild); // register child windows class
111 #endif
112 
113     HMENU hMenu = LoadMenu(hInstance, MAKEINTRESOURCE(IDC_MDI_APP));
114     HMENU hMenuOptions = GetSubMenu(hMenu, ID_OPTIONS_MENU);
115     // HMENU hChildMenu = LoadMenu(hInstance, MAKEINTRESOURCE(IDC_MDI_APP_CHILD));
116 
117     INITCOMMONCONTROLSEX icc = {
118         sizeof(INITCOMMONCONTROLSEX),
119         ICC_BAR_CLASSES
120     };
121 
122     HDC hdc = GetDC(0);
123 
124     hMenuFrame = hMenu;
125 //  hMenuView = GetSubMenu(hMenuFrame, ID_VIEW_MENU);
126     hAccel = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MDI_APP));
127     hFont = CreateFont(-MulDiv(8,GetDeviceCaps(hdc,LOGPIXELSY),72), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, _T("MS Sans Serif"));
128     ReleaseDC(0, hdc);
129 
130     hFrameWnd = CreateWindowEx(0, (LPCTSTR)(ULONG_PTR)hFrameWndClass, szTitle,
131 //  hFrameWnd = CreateWindow(szFrameClass, szTitle,
132                     WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
133                     CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
134                     NULL/*hWndParent*/, hMenuFrame, hInstance, NULL/*lpParam*/);
135     if (!hFrameWnd) {
136         return FALSE;
137     }
138 
139     if (InitCommonControlsEx(&icc))
140     {
141         int nParts[3];
142         TBBUTTON toolbarBtns[] = {
143             {0, 0, 0, TBSTYLE_SEP},
144             {0, ID_WINDOW_NEW_WINDOW, TBSTATE_ENABLED, TBSTYLE_BUTTON},
145             {1, ID_WINDOW_CASCADE, TBSTATE_ENABLED, TBSTYLE_BUTTON},
146             {2, ID_WINDOW_TILE_HORZ, TBSTATE_ENABLED, TBSTYLE_BUTTON},
147             {3, ID_WINDOW_TILE_VERT, TBSTATE_ENABLED, TBSTYLE_BUTTON},
148             {4, 2/*TODO: ID_...*/, TBSTATE_ENABLED, TBSTYLE_BUTTON},
149             {5, 2/*TODO: ID_...*/, TBSTATE_ENABLED, TBSTYLE_BUTTON},
150         };
151 
152         hToolBar = CreateToolbarEx(hFrameWnd, WS_CHILD|WS_VISIBLE,
153             IDC_TOOLBAR, 2, hInstance, IDB_TOOLBAR, toolbarBtns,
154             sizeof(toolbarBtns)/sizeof(TBBUTTON), 16, 15, 16, 15, sizeof(TBBUTTON));
155         CheckMenuItem(hMenuOptions, ID_OPTIONS_TOOLBAR, MF_BYCOMMAND|MF_CHECKED);
156 
157         // Create the status bar
158         hStatusBar = CreateStatusWindow(WS_VISIBLE|WS_CHILD|WS_CLIPSIBLINGS|SBT_NOBORDERS,
159                                         "", hFrameWnd, IDC_STATUSBAR);
160         if (!hStatusBar)
161             return FALSE;
162         CheckMenuItem(hMenuOptions, ID_OPTIONS_STATUSBAR, MF_BYCOMMAND|MF_CHECKED);
163 
164         // Create the status bar panes
165         nParts[0] = 100;
166         nParts[1] = 210;
167         nParts[2] = 400;
168         SendMessage(hStatusBar, SB_SETPARTS, 3, (LPARAM)nParts);
169     } else {
170         CheckMenuItem(hMenuOptions, ID_OPTIONS_TOOLBAR, MF_BYCOMMAND|MF_GRAYED);
171         CheckMenuItem(hMenuOptions, ID_OPTIONS_STATUSBAR, MF_BYCOMMAND|MF_GRAYED);
172 	}
173 
174     ShowWindow(hFrameWnd, nCmdShow);
175     UpdateWindow(hFrameWnd);
176     UpdateStatusBar();
177     return TRUE;
178 }
179 
180 ////////////////////////////////////////////////////////////////////////////////
181 
182 void UpdateStatusBar(void)
183 {
184     TCHAR text[260];
185     DWORD size;
186 
187     size = sizeof(text)/sizeof(TCHAR);
188     GetUserName(text, &size);
189     SendMessage(hStatusBar, SB_SETTEXT, 1, (LPARAM)text);
190     size = sizeof(text)/sizeof(TCHAR);
191     GetComputerName(text, &size);
192     SendMessage(hStatusBar, SB_SETTEXT, 0, (LPARAM)text);
193 }
194 
195 
196 static int g_foundPrevInstance = 0;
197 
198 // search for already running win[e]files
199 static BOOL CALLBACK EnumWndProc(HWND hWnd, LPARAM lParam)
200 {
201     TCHAR cls[128];
202 
203     GetClassName(hWnd, cls, 128);
204     if (!lstrcmp(cls, (LPCTSTR)lParam)) {
205         g_foundPrevInstance++;
206         return FALSE;
207     }
208     return TRUE;
209 }
210 
211 ////////////////////////////////////////////////////////////////////////////////
212 
213 void ExitInstance(void)
214 {
215     DestroyMenu(hMenuFrame);
216 }
217 
218 
219 int APIENTRY WinMain(HINSTANCE hInstance,
220                      HINSTANCE hPrevInstance,
221                      LPSTR     lpCmdLine,
222                      int       nCmdShow)
223 {
224     MSG msg;
225 //    HACCEL hAccel;
226     HWND hMDIClient;
227 
228     // Initialize global strings
229     LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
230     LoadString(hInstance, IDC_MDI_APP, szFrameClass, MAX_LOADSTRING);
231     LoadString(hInstance, IDC_MDI_APP_CHILD, szChildClass, MAX_LOADSTRING);
232 
233     // Allow only one running instance
234     EnumWindows(EnumWndProc, (LPARAM)szFrameClass);
235     if (g_foundPrevInstance)
236         return 1;
237 
238     // Store instance handle in our global variable
239     hInst = hInstance;
240 
241     // Perform application initialization:
242     if (!InitInstance(hInstance, nCmdShow)) {
243         return FALSE;
244     }
245 //    hAccel = LoadAccelerators(hInstance, (LPCTSTR)IDC_MDI_APP);
246     hMDIClient = GetWindow(hFrameWnd, GW_CHILD);
247 
248     // Main message loop:
249     while (GetMessage(&msg, (HWND)NULL, 0, 0)) {
250         if (!TranslateMDISysAccel(hMDIClient, &msg) &&
251             !TranslateAccelerator(hFrameWnd, hAccel, &msg)) {
252             TranslateMessage(&msg);
253             DispatchMessage(&msg);
254         }
255     }
256     ExitInstance();
257     return msg.wParam;
258 }
259