Further changes to make mfctest work.
[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 class MyCanvas: public wxScrolledWindow
78 {
79 public:
80 MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size);
81 void OnPaint(wxPaintEvent& event);
82 void OnMouseEvent(wxMouseEvent& event);
83 DECLARE_EVENT_TABLE()
84 };
85
86 class MyChild: public wxFrame
87 {
88 public:
89 MyCanvas *canvas;
90 MyChild(wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& size, const long style);
91 ~MyChild(void);
92 bool OnClose(void);
93
94 void OnQuit(wxCommandEvent& event);
95 void OnNew(wxCommandEvent& event);
96 void OnActivate(wxActivateEvent& event);
97
98 DECLARE_EVENT_TABLE()
99 };
100
101 // For drawing lines in a canvas
102 long xpos = -1;
103 long ypos = -1;
104
105 // ID for the menu quit command
106 #define HELLO_QUIT 1
107 #define HELLO_NEW 2
108
109 DECLARE_APP(MyApp)
110 IMPLEMENT_APP(MyApp)
111
112 /////////////////////////////////////////////////////////////////////////////
113
114 // CMainWindow constructor:
115 // Create the window with the appropriate style, size, menu, etc.
116 //
117 CMainWindow::CMainWindow()
118 {
119 LoadAccelTable( "MainAccelTable" );
120 Create( NULL, "Hello Foundation Application",
121 WS_OVERLAPPEDWINDOW, rectDefault, NULL, "MainMenu" );
122 }
123
124 // OnPaint:
125 // This routine draws the string "Hello, Windows!" in the center of the
126 // client area. It is called whenever Windows sends a WM_PAINT message.
127 // Note that creating a CPaintDC automatically does a BeginPaint and
128 // an EndPaint call is done when it is destroyed at the end of this
129 // function. CPaintDC's constructor needs the window (this).
130 //
131 void CMainWindow::OnPaint()
132 {
133 CString s = "Hello, Windows!";
134 CPaintDC dc( this );
135 CRect rect;
136
137 GetClientRect( rect );
138 dc.SetTextAlign( TA_BASELINE | TA_CENTER );
139 dc.SetTextColor( ::GetSysColor( COLOR_WINDOWTEXT ) );
140 dc.SetBkMode(TRANSPARENT);
141 dc.TextOut( ( rect.right / 2 ), ( rect.bottom / 2 ),
142 s, s.GetLength() );
143 }
144
145 // OnAbout:
146 // This member function is called when a WM_COMMAND message with an
147 // IDM_ABOUT code is received by the CMainWindow class object. The
148 // message map below is responsible for this routing.
149 //
150 // We create a ClDialog object using the "AboutBox" resource (see
151 // hello.rc), and invoke it.
152 //
153 void CMainWindow::OnAbout()
154 {
155 CDialog about( "AboutBox", this );
156 about.DoModal();
157 }
158
159 void CMainWindow::OnTest()
160 {
161 wxMessageBox("This is a wxWindows message box.\nWe're about to create a new wxWindows frame.", "wxWindows", wxOK);
162 wxGetApp().CreateFrame();
163 }
164
165 // CMainWindow message map:
166 // Associate messages with member functions.
167 //
168 // It is implied that the ON_WM_PAINT macro expects a member function
169 // "void OnPaint()".
170 //
171 // It is implied that members connected with the ON_COMMAND macro
172 // receive no arguments and are void of return type, e.g., "void OnAbout()".
173 //
174 BEGIN_MESSAGE_MAP( CMainWindow, CFrameWnd )
175 //{{AFX_MSG_MAP( CMainWindow )
176 ON_WM_PAINT()
177 ON_COMMAND( IDM_ABOUT, OnAbout )
178 ON_COMMAND( IDM_TEST, OnTest )
179 //}}AFX_MSG_MAP
180 END_MESSAGE_MAP()
181
182 /////////////////////////////////////////////////////////////////////////////
183 // CTheApp
184
185 // InitInstance:
186 // When any CTheApp object is created, this member function is automatically
187 // called. Any data may be set up at this point.
188 //
189 // Also, the main window of the application should be created and shown here.
190 // Return TRUE if the initialization is successful.
191 //
192 BOOL CTheApp::InitInstance()
193 {
194 TRACE( "HELLO WORLD\n" );
195
196 SetDialogBkColor(); // hook gray dialogs (was default in MFC V1)
197
198 wxEntry((WXHINSTANCE) m_hInstance, (WXHINSTANCE) m_hPrevInstance, m_lpCmdLine, m_nCmdShow, FALSE);
199
200 /*
201 m_pMainWnd = new CMainWindow();
202 m_pMainWnd->ShowWindow( m_nCmdShow );
203 m_pMainWnd->UpdateWindow();
204 */
205
206 if (wxTheApp && wxTheApp->GetTopWindow())
207 {
208 m_pMainWnd = new CDummyWindow((HWND) wxTheApp->GetTopWindow()->GetHWND());
209 }
210
211 return TRUE;
212 }
213
214 int CTheApp::ExitInstance()
215 {
216 // OnExit isn't called by CleanUp so must be called explicitly.
217 wxTheApp->OnExit();
218 wxApp::CleanUp();
219
220 return CWinApp::ExitInstance();
221 }
222
223 // Override this to provide wxWindows message loop
224 // compatibility
225
226 BOOL CTheApp::PreTranslateMessage(MSG *msg)
227 {
228 if (wxTheApp && wxTheApp->ProcessMessage((WXMSG*) msg))
229 return TRUE;
230 else
231 return CWinApp::PreTranslateMessage(msg);
232 }
233
234 BOOL CTheApp::OnIdle(LONG lCount)
235 {
236 if (wxTheApp)
237 return wxTheApp->ProcessIdle();
238 else
239 return FALSE;
240 }
241
242 /*********************************************************************
243 * wxWindows elements
244 ********************************************************************/
245
246 bool MyApp::OnInit(void)
247 {
248 // Don't exit app when the top level frame is deleted
249 // SetExitOnFrameDelete(FALSE);
250
251 wxFrame* frame = CreateFrame();
252 return TRUE;
253 }
254
255 wxFrame *MyApp::CreateFrame(void)
256 {
257 MyChild *subframe = new MyChild(NULL, "Canvas Frame", wxPoint(10, 10), wxSize(300, 300),
258 wxDEFAULT_FRAME_STYLE);
259
260 subframe->SetTitle("wxWindows canvas frame");
261
262 // Give it a status line
263 subframe->CreateStatusBar();
264
265 // Make a menubar
266 wxMenu *file_menu = new wxMenu;
267
268 file_menu->Append(HELLO_NEW, "&New MFC Window");
269 file_menu->Append(HELLO_QUIT, "&Close");
270
271 wxMenuBar *menu_bar = new wxMenuBar;
272
273 menu_bar->Append(file_menu, "&File");
274
275 // Associate the menu bar with the frame
276 subframe->SetMenuBar(menu_bar);
277
278 int width, height;
279 subframe->GetClientSize(&width, &height);
280
281 MyCanvas *canvas = new MyCanvas(subframe, wxPoint(0, 0), wxSize(width, height));
282 canvas->SetCursor(wxCursor(wxCURSOR_PENCIL));
283 subframe->canvas = canvas;
284
285 // Give it scrollbars
286 // canvas->SetScrollbars(20, 20, 50, 50, 4, 4);
287
288 subframe->Show(TRUE);
289 // Return the main frame window
290 return subframe;
291 }
292
293 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
294 EVT_PAINT(MyCanvas::OnPaint)
295 EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent)
296 END_EVENT_TABLE()
297
298 // Define a constructor for my canvas
299 MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size):
300 wxScrolledWindow(parent, -1, pos, size)
301 {
302 }
303
304 // Define the repainting behaviour
305 void MyCanvas::OnPaint(wxPaintEvent& event)
306 {
307 wxPaintDC dc(this);
308
309 dc.SetFont(* wxSWISS_FONT);
310 dc.SetPen(* wxGREEN_PEN);
311 dc.DrawLine(0, 0, 200, 200);
312 dc.DrawLine(200, 0, 0, 200);
313
314 dc.SetBrush(* wxCYAN_BRUSH);
315 dc.SetPen(* wxRED_PEN);
316 dc.DrawRectangle(100, 100, 100, 50);
317 dc.DrawRoundedRectangle(150, 150, 100, 50, 20);
318
319 dc.DrawEllipse(250, 250, 100, 50);
320 dc.DrawSpline(50, 200, 50, 100, 200, 10);
321 dc.DrawLine(50, 230, 200, 230);
322 dc.DrawText("This is a test string", 50, 230);
323 }
324
325 // This implements a tiny doodling program! Drag the mouse using
326 // the left button.
327 void MyCanvas::OnMouseEvent(wxMouseEvent& event)
328 {
329 wxClientDC dc(this);
330 dc.SetPen(* wxBLACK_PEN);
331 long x, y;
332 event.Position(&x, &y);
333 if (xpos > -1 && ypos > -1 && event.Dragging())
334 {
335 dc.DrawLine(xpos, ypos, x, y);
336 }
337 xpos = x;
338 ypos = y;
339 }
340
341 BEGIN_EVENT_TABLE(MyChild, wxFrame)
342 EVT_MENU(HELLO_QUIT, MyChild::OnQuit)
343 EVT_MENU(HELLO_NEW, MyChild::OnNew)
344 EVT_ACTIVATE(MyChild::OnActivate)
345 END_EVENT_TABLE()
346
347 MyChild::MyChild(wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& size, const long style):
348 wxFrame(frame, -1, title, pos, size, style)
349 {
350 canvas = NULL;
351 }
352
353 MyChild::~MyChild(void)
354 {
355 }
356
357 void MyChild::OnQuit(wxCommandEvent& event)
358 {
359 Close(TRUE);
360 }
361
362 void MyChild::OnNew(wxCommandEvent& event)
363 {
364 CMainWindow *mainWin = new CMainWindow();
365 mainWin->ShowWindow( TRUE );
366 mainWin->UpdateWindow();
367 }
368
369 void MyChild::OnActivate(wxActivateEvent& event)
370 {
371 if (event.GetActive() && canvas)
372 canvas->SetFocus();
373 }
374
375 bool MyChild::OnClose(void)
376 {
377 return TRUE;
378 }
379
380
381 // Dummy MFC window for specifying a valid main window to MFC, using
382 // a wxWindows HWND.
383 CDummyWindow::CDummyWindow(HWND hWnd):CWnd()
384 {
385 Attach(hWnd);
386 }
387
388 // Don't let the CWnd destructor delete the HWND
389 CDummyWindow::~CDummyWindow(void)
390 {
391 Detach();
392 }
393