Fixed compilation for when not including <windows.h>
[wxWidgets.git] / samples / mfc / mfctest.cpp
1 // hello.cpp : Defines the class behaviors for the application.
2 // Hello is a simple program which consists of a main window
3 // and an "About" dialog which can be invoked by a menu choice.
4 // It is intended to serve as a starting-point for new
5 // applications.
6 //
7 // This is a part of the Microsoft Foundation Classes C++ library.
8 // Copyright (C) 1992 Microsoft Corporation
9 // All rights reserved.
10 //
11 // This source code is only intended as a supplement to the
12 // Microsoft Foundation Classes Reference and Microsoft
13 // WinHelp documentation provided with the library.
14 // See these sources for detailed information regarding the
15 // Microsoft Foundation Classes product.
16
17 // *** MODIFIED BY JULIAN SMART TO DEMONSTRATE CO-EXISTANCE WITH wxWINDOWS ***
18 //
19 // This sample pops up an initial wxWindows frame, with a menu item
20 // that allows a new MFC window to be created. Note that CDummyWindow
21 // is a class that allows a wxWindows window to be seen as a CWnd
22 // for the purposes of specifying a valid main window to the
23 // MFC initialisation.
24 //
25 // You can easily modify this code so that an MFC window pops up
26 // initially as the main frame, and allows wxWindows frames to be
27 // created subsequently:
28 //
29 // (1) Make MyApp::OnInit return NULL, not create a window.
30 // (2) Restore the MFC code to create a window in InitInstance, and remove
31 // creation of CDummyWindow.
32 //
33 // IMPORTANT NOTE: to compile this sample, you must first edit
34 // wx/src/msw/wx_main.cc, set NOWINMAIN to 1, and remake wxWindows
35 // (it only needs to recompile wx_main.cc).
36 // This eliminates the duplicate WinMain function which MFC implements.
37
38 // For compilers that support precompilation, includes "wx/wx.h".
39 #include "wx/wxprec.h"
40
41 #ifdef __BORLANDC__
42 #pragma hdrstop
43 #endif
44
45 #include "wx/wx.h"
46
47 #ifdef new
48 #undef new
49 #endif
50
51 #include "stdafx.h"
52
53 #ifdef DrawText
54 #undef DrawText
55 #endif
56
57 #include "resource.h"
58
59 #include "mfctest.h"
60
61 /////////////////////////////////////////////////////////////////////////////
62
63 // theApp:
64 // Just creating this application object runs the whole application.
65 //
66 CTheApp theApp;
67
68 // wxWindows elements
69
70 // Define a new application type
71 class MyApp: public wxApp
72 { public:
73 bool OnInit(void);
74 wxFrame *CreateFrame(void);
75 };
76
77 DECLARE_APP(MyApp)
78
79 class MyCanvas: public wxScrolledWindow
80 {
81 public:
82 MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size);
83 void OnPaint(wxPaintEvent& event);
84 void OnMouseEvent(wxMouseEvent& event);
85 DECLARE_EVENT_TABLE()
86 };
87
88 class MyChild: public wxFrame
89 {
90 public:
91 MyCanvas *canvas;
92 MyChild(wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& size, const long style);
93 ~MyChild(void);
94 bool OnClose(void);
95
96 void OnQuit(wxCommandEvent& event);
97 void OnNew(wxCommandEvent& event);
98 void OnActivate(wxActivateEvent& event);
99
100 DECLARE_EVENT_TABLE()
101 };
102
103 // For drawing lines in a canvas
104 long xpos = -1;
105 long ypos = -1;
106
107 // Initialise this in OnInit, not statically
108 wxPen *red_pen;
109 wxFont *small_font;
110
111 // ID for the menu quit command
112 #define HELLO_QUIT 1
113 #define HELLO_NEW 2
114
115 DECLARE_APP(MyApp)
116 IMPLEMENT_APP(MyApp)
117
118 /////////////////////////////////////////////////////////////////////////////
119
120 // CMainWindow constructor:
121 // Create the window with the appropriate style, size, menu, etc.
122 //
123 CMainWindow::CMainWindow()
124 {
125 LoadAccelTable( "MainAccelTable" );
126 Create( NULL, "Hello Foundation Application",
127 WS_OVERLAPPEDWINDOW, rectDefault, NULL, "MainMenu" );
128 }
129
130 // OnPaint:
131 // This routine draws the string "Hello, Windows!" in the center of the
132 // client area. It is called whenever Windows sends a WM_PAINT message.
133 // Note that creating a CPaintDC automatically does a BeginPaint and
134 // an EndPaint call is done when it is destroyed at the end of this
135 // function. CPaintDC's constructor needs the window (this).
136 //
137 void CMainWindow::OnPaint()
138 {
139 CString s = "Hello, Windows!";
140 CPaintDC dc( this );
141 CRect rect;
142
143 GetClientRect( rect );
144 dc.SetTextAlign( TA_BASELINE | TA_CENTER );
145 dc.SetTextColor( ::GetSysColor( COLOR_WINDOWTEXT ) );
146 dc.SetBkMode(TRANSPARENT);
147 dc.TextOut( ( rect.right / 2 ), ( rect.bottom / 2 ),
148 s, s.GetLength() );
149 }
150
151 // OnAbout:
152 // This member function is called when a WM_COMMAND message with an
153 // IDM_ABOUT code is received by the CMainWindow class object. The
154 // message map below is responsible for this routing.
155 //
156 // We create a ClDialog object using the "AboutBox" resource (see
157 // hello.rc), and invoke it.
158 //
159 void CMainWindow::OnAbout()
160 {
161 CDialog about( "AboutBox", this );
162 about.DoModal();
163 }
164
165 void CMainWindow::OnTest()
166 {
167 wxMessageBox("This is a wxWindows message box.\nWe're about to create a new wxWindows frame.", "wxWindows", wxOK);
168 wxGetApp().CreateFrame();
169 }
170
171 // CMainWindow message map:
172 // Associate messages with member functions.
173 //
174 // It is implied that the ON_WM_PAINT macro expects a member function
175 // "void OnPaint()".
176 //
177 // It is implied that members connected with the ON_COMMAND macro
178 // receive no arguments and are void of return type, e.g., "void OnAbout()".
179 //
180 BEGIN_MESSAGE_MAP( CMainWindow, CFrameWnd )
181 //{{AFX_MSG_MAP( CMainWindow )
182 ON_WM_PAINT()
183 ON_COMMAND( IDM_ABOUT, OnAbout )
184 ON_COMMAND( IDM_TEST, OnTest )
185 //}}AFX_MSG_MAP
186 END_MESSAGE_MAP()
187
188 /////////////////////////////////////////////////////////////////////////////
189 // CTheApp
190
191 // InitInstance:
192 // When any CTheApp object is created, this member function is automatically
193 // called. Any data may be set up at this point.
194 //
195 // Also, the main window of the application should be created and shown here.
196 // Return TRUE if the initialization is successful.
197 //
198 BOOL CTheApp::InitInstance()
199 {
200 TRACE( "HELLO WORLD\n" );
201
202 SetDialogBkColor(); // hook gray dialogs (was default in MFC V1)
203
204 wxEntry((WXHINSTANCE) m_hInstance, (WXHINSTANCE) m_hPrevInstance, m_lpCmdLine, m_nCmdShow, FALSE);
205
206 /*
207 m_pMainWnd = new CMainWindow();
208 m_pMainWnd->ShowWindow( m_nCmdShow );
209 m_pMainWnd->UpdateWindow();
210 */
211
212 if (wxTheApp && wxTheApp->GetTopWindow())
213 {
214 m_pMainWnd = new CDummyWindow((HWND) wxTheApp->GetTopWindow()->GetHWND());
215 }
216
217 return TRUE;
218 }
219
220 int CTheApp::ExitInstance()
221 {
222 wxApp::CleanUp();
223
224 return CWinApp::ExitInstance();
225 }
226
227 // Override this to provide wxWindows message loop
228 // compatibility
229
230 BOOL CTheApp::PreTranslateMessage(MSG *msg)
231 {
232 if (wxTheApp && wxTheApp->ProcessMessage((WXMSG*) msg))
233 return TRUE;
234 else
235 return CWinApp::PreTranslateMessage(msg);
236 }
237
238 BOOL CTheApp::OnIdle(LONG lCount)
239 {
240 if (wxTheApp)
241 return wxTheApp->ProcessIdle();
242 else
243 return FALSE;
244 }
245
246 /*********************************************************************
247 * wxWindows elements
248 ********************************************************************/
249
250 bool MyApp::OnInit(void)
251 {
252 // Don't exit app when the top level frame is deleted
253 // SetExitOnFrameDelete(FALSE);
254
255 // Create a red pen
256 red_pen = new wxPen("RED", 3, wxSOLID);
257
258 // Create a small font
259 small_font = new wxFont(10, wxSWISS, wxNORMAL, wxNORMAL);
260
261 wxFrame* frame = CreateFrame();
262 return TRUE;
263 }
264
265 wxFrame *MyApp::CreateFrame(void)
266 {
267 MyChild *subframe = new MyChild(NULL, "Canvas Frame", wxPoint(10, 10), wxSize(300, 300),
268 wxDEFAULT_FRAME_STYLE);
269
270 subframe->SetTitle("wxWindows canvas frame");
271
272 // Give it a status line
273 subframe->CreateStatusBar();
274
275 // Make a menubar
276 wxMenu *file_menu = new wxMenu;
277
278 file_menu->Append(HELLO_NEW, "&New MFC Window");
279 file_menu->Append(HELLO_QUIT, "&Close");
280
281 wxMenuBar *menu_bar = new wxMenuBar;
282
283 menu_bar->Append(file_menu, "&File");
284
285 // Associate the menu bar with the frame
286 subframe->SetMenuBar(menu_bar);
287
288 int width, height;
289 subframe->GetClientSize(&width, &height);
290
291 MyCanvas *canvas = new MyCanvas(subframe, wxPoint(0, 0), wxSize(width, height));
292 wxCursor *cursor = new wxCursor(wxCURSOR_PENCIL);
293 canvas->SetCursor(*cursor);
294 subframe->canvas = canvas;
295
296 // Give it scrollbars
297 // canvas->SetScrollbars(20, 20, 50, 50, 4, 4);
298
299 subframe->Show(TRUE);
300 // Return the main frame window
301 return subframe;
302 }
303
304 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
305 EVT_PAINT(MyCanvas::OnPaint)
306 EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent)
307 END_EVENT_TABLE()
308
309 // Define a constructor for my canvas
310 MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size):
311 wxScrolledWindow(parent, -1, pos, size)
312 {
313 }
314
315 // Define the repainting behaviour
316 void MyCanvas::OnPaint(wxPaintEvent& event)
317 {
318 wxPaintDC dc(this);
319
320 dc.SetFont(* small_font);
321 dc.SetPen(* wxGREEN_PEN);
322 dc.DrawLine(0, 0, 200, 200);
323 dc.DrawLine(200, 0, 0, 200);
324
325 dc.SetBrush(* wxCYAN_BRUSH);
326 dc.SetPen(* wxRED_PEN);
327 dc.DrawRectangle(100, 100, 100, 50);
328 dc.DrawRoundedRectangle(150, 150, 100, 50, 20);
329
330 dc.DrawEllipse(250, 250, 100, 50);
331 dc.DrawSpline(50, 200, 50, 100, 200, 10);
332 dc.DrawLine(50, 230, 200, 230);
333 dc.DrawText("This is a test string", 50, 230);
334 }
335
336 // This implements a tiny doodling program! Drag the mouse using
337 // the left button.
338 void MyCanvas::OnMouseEvent(wxMouseEvent& event)
339 {
340 wxClientDC dc(this);
341 dc.SetPen(* wxBLACK_PEN);
342 long x, y;
343 event.Position(&x, &y);
344 if (xpos > -1 && ypos > -1 && event.Dragging())
345 {
346 dc.DrawLine(xpos, ypos, x, y);
347 }
348 xpos = x;
349 ypos = y;
350 }
351
352 BEGIN_EVENT_TABLE(MyChild, wxFrame)
353 EVT_MENU(HELLO_QUIT, MyChild::OnQuit)
354 EVT_MENU(HELLO_NEW, MyChild::OnNew)
355 EVT_ACTIVATE(MyChild::OnActivate)
356 END_EVENT_TABLE()
357
358 MyChild::MyChild(wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& size, const long style):
359 wxFrame(frame, -1, title, pos, size, style)
360 {
361 canvas = NULL;
362 }
363
364 MyChild::~MyChild(void)
365 {
366 }
367
368 void MyChild::OnQuit(wxCommandEvent& event)
369 {
370 Close(TRUE);
371 }
372
373 void MyChild::OnNew(wxCommandEvent& event)
374 {
375 CMainWindow *mainWin = new CMainWindow();
376 mainWin->ShowWindow( TRUE );
377 mainWin->UpdateWindow();
378 }
379
380 void MyChild::OnActivate(wxActivateEvent& event)
381 {
382 if (event.GetActive() && canvas)
383 canvas->SetFocus();
384 }
385
386 bool MyChild::OnClose(void)
387 {
388 return TRUE;
389 }
390
391
392 // Dummy MFC window for specifying a valid main window to MFC, using
393 // a wxWindows HWND.
394 CDummyWindow::CDummyWindow(HWND hWnd):CWnd()
395 {
396 Attach(hWnd);
397 }
398
399 // Don't let the CWnd destructor delete the HWND
400 CDummyWindow::~CDummyWindow(void)
401 {
402 Detach();
403 }
404