]> git.saurik.com Git - wxWidgets.git/blob - samples/mfc/mfctest.cpp
fix for the X fatal error when closing frames
[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 FALSE, not creating a window.
30 // (2) Restore the MFC code to create a window in InitInstance, and remove
31 // creation of CDummyWindow.
32 //
33 // IMPORTANT NOTES:
34 //
35 // (1) You need to set wxUSE_MFC to 1 in include/wx/msw/setup.h, which switches
36 // off some debugging features and also removes the windows.h inclusion
37 // in wxprec.h (MFC headers don't like this to have been included previously).
38 // Then recompile wxWindows and this sample.
39 //
40 // (2) 10/3/2000, wxWindows 2.1.14: unfortunately there is an assert when
41 // the sample tries to create an MFC window. Any suggestions welcome. It may be
42 // a problem with conflicting project settings. Ignoring the assert (several times)
43 // allows the sample to continue. In release mode the asserts don't happen.
44 //
45 // (3) I can't get the sample to link using a static MFC library, only the DLL
46 // version. Perhaps someone else is a wizard at working out the required settings
47 // in the wxWin library and the sample; then debugging the assert problem may be
48 // easier.
49 //
50 // (4) Compiling wxWindows in DLL mode currently includes windows.h, so you must only
51 // try linking wxWindows statically.
52
53 // For compilers that support precompilation, includes "wx/wx.h".
54 #include "wx/wxprec.h"
55
56 #ifdef __BORLANDC__
57 #pragma hdrstop
58 #endif
59
60 #include "wx/wx.h"
61
62 #if defined(_WINDOWS_) || !wxUSE_MFC
63 #error Sorry, you need to edit include/wx/msw/setup.h, set wxUSE_MFC to 1, and recompile.
64 #endif
65
66 #ifdef new
67 #undef new
68 #endif
69
70 #include "stdafx.h"
71
72 #ifdef DrawText
73 #undef DrawText
74 #endif
75
76 #include "resource.h"
77
78 #include "mfctest.h"
79
80 /////////////////////////////////////////////////////////////////////////////
81
82 // theApp:
83 // Just creating this application object runs the whole application.
84 //
85 CTheApp theApp;
86
87 // wxWindows elements
88
89 // Define a new application type
90 class MyApp: public wxApp
91 { public:
92 bool OnInit(void);
93 wxFrame *CreateFrame(void);
94 };
95
96 class MyCanvas: public wxScrolledWindow
97 {
98 public:
99 MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size);
100 void OnPaint(wxPaintEvent& event);
101 void OnMouseEvent(wxMouseEvent& event);
102 DECLARE_EVENT_TABLE()
103 };
104
105 class MyChild: public wxFrame
106 {
107 public:
108 MyCanvas *canvas;
109 MyChild(wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& size, const long style);
110 ~MyChild(void);
111
112 void OnQuit(wxCommandEvent& event);
113 void OnNew(wxCommandEvent& event);
114 void OnActivate(wxActivateEvent& event);
115
116 DECLARE_EVENT_TABLE()
117 };
118
119 // For drawing lines in a canvas
120 long xpos = -1;
121 long ypos = -1;
122
123 // ID for the menu quit command
124 #define HELLO_QUIT 1
125 #define HELLO_NEW 2
126
127 DECLARE_APP(MyApp)
128 IMPLEMENT_APP(MyApp)
129
130 /////////////////////////////////////////////////////////////////////////////
131
132 // CMainWindow constructor:
133 // Create the window with the appropriate style, size, menu, etc.
134 //
135 CMainWindow::CMainWindow()
136 {
137 LoadAccelTable( "MainAccelTable" );
138 Create( NULL, "Hello Foundation Application",
139 WS_OVERLAPPEDWINDOW, rectDefault, NULL, "MainMenu" );
140 }
141
142 // OnPaint:
143 // This routine draws the string "Hello, Windows!" in the center of the
144 // client area. It is called whenever Windows sends a WM_PAINT message.
145 // Note that creating a CPaintDC automatically does a BeginPaint and
146 // an EndPaint call is done when it is destroyed at the end of this
147 // function. CPaintDC's constructor needs the window (this).
148 //
149 void CMainWindow::OnPaint()
150 {
151 CString s = "Hello, Windows!";
152 CPaintDC dc( this );
153 CRect rect;
154
155 GetClientRect( rect );
156 dc.SetTextAlign( TA_BASELINE | TA_CENTER );
157 dc.SetTextColor( ::GetSysColor( COLOR_WINDOWTEXT ) );
158 dc.SetBkMode(TRANSPARENT);
159 dc.TextOut( ( rect.right / 2 ), ( rect.bottom / 2 ),
160 s, s.GetLength() );
161 }
162
163 // OnAbout:
164 // This member function is called when a WM_COMMAND message with an
165 // IDM_ABOUT code is received by the CMainWindow class object. The
166 // message map below is responsible for this routing.
167 //
168 // We create a ClDialog object using the "AboutBox" resource (see
169 // hello.rc), and invoke it.
170 //
171 void CMainWindow::OnAbout()
172 {
173 CDialog about( "AboutBox", this );
174 about.DoModal();
175 }
176
177 void CMainWindow::OnTest()
178 {
179 wxMessageBox("This is a wxWindows message box.\nWe're about to create a new wxWindows frame.", "wxWindows", wxOK);
180 wxGetApp().CreateFrame();
181 }
182
183 // CMainWindow message map:
184 // Associate messages with member functions.
185 //
186 // It is implied that the ON_WM_PAINT macro expects a member function
187 // "void OnPaint()".
188 //
189 // It is implied that members connected with the ON_COMMAND macro
190 // receive no arguments and are void of return type, e.g., "void OnAbout()".
191 //
192 BEGIN_MESSAGE_MAP( CMainWindow, CFrameWnd )
193 //{{AFX_MSG_MAP( CMainWindow )
194 ON_WM_PAINT()
195 ON_COMMAND( IDM_ABOUT, OnAbout )
196 ON_COMMAND( IDM_TEST, OnTest )
197 //}}AFX_MSG_MAP
198 END_MESSAGE_MAP()
199
200 /////////////////////////////////////////////////////////////////////////////
201 // CTheApp
202
203 // InitInstance:
204 // When any CTheApp object is created, this member function is automatically
205 // called. Any data may be set up at this point.
206 //
207 // Also, the main window of the application should be created and shown here.
208 // Return TRUE if the initialization is successful.
209 //
210 BOOL CTheApp::InitInstance()
211 {
212 TRACE( "HELLO WORLD\n" );
213
214 SetDialogBkColor(); // hook gray dialogs (was default in MFC V1)
215
216 wxEntry((WXHINSTANCE) m_hInstance, (WXHINSTANCE) m_hPrevInstance, m_lpCmdLine, m_nCmdShow, FALSE);
217
218 /*
219 m_pMainWnd = new CMainWindow();
220 m_pMainWnd->ShowWindow( m_nCmdShow );
221 m_pMainWnd->UpdateWindow();
222 */
223
224 if (wxTheApp && wxTheApp->GetTopWindow())
225 {
226 m_pMainWnd = new CDummyWindow((HWND) wxTheApp->GetTopWindow()->GetHWND());
227 }
228
229 return TRUE;
230 }
231
232 int CTheApp::ExitInstance()
233 {
234 // OnExit isn't called by CleanUp so must be called explicitly.
235 wxTheApp->OnExit();
236 wxApp::CleanUp();
237
238 return CWinApp::ExitInstance();
239 }
240
241 // Override this to provide wxWindows message loop
242 // compatibility
243
244 BOOL CTheApp::PreTranslateMessage(MSG *msg)
245 {
246 if (wxTheApp && wxTheApp->ProcessMessage((WXMSG*) msg))
247 return TRUE;
248 else
249 return CWinApp::PreTranslateMessage(msg);
250 }
251
252 BOOL CTheApp::OnIdle(LONG lCount)
253 {
254 if (wxTheApp)
255 return wxTheApp->ProcessIdle();
256 else
257 return FALSE;
258 }
259
260 /*********************************************************************
261 * wxWindows elements
262 ********************************************************************/
263
264 bool MyApp::OnInit(void)
265 {
266 // Don't exit app when the top level frame is deleted
267 // SetExitOnFrameDelete(FALSE);
268
269 wxFrame* frame = CreateFrame();
270 return TRUE;
271 }
272
273 wxFrame *MyApp::CreateFrame(void)
274 {
275 MyChild *subframe = new MyChild(NULL, "Canvas Frame", wxPoint(10, 10), wxSize(300, 300),
276 wxDEFAULT_FRAME_STYLE);
277
278 subframe->SetTitle("wxWindows canvas frame");
279
280 // Give it a status line
281 subframe->CreateStatusBar();
282
283 // Make a menubar
284 wxMenu *file_menu = new wxMenu;
285
286 file_menu->Append(HELLO_NEW, "&New MFC Window");
287 file_menu->Append(HELLO_QUIT, "&Close");
288
289 wxMenuBar *menu_bar = new wxMenuBar;
290
291 menu_bar->Append(file_menu, "&File");
292
293 // Associate the menu bar with the frame
294 subframe->SetMenuBar(menu_bar);
295
296 int width, height;
297 subframe->GetClientSize(&width, &height);
298
299 MyCanvas *canvas = new MyCanvas(subframe, wxPoint(0, 0), wxSize(width, height));
300 canvas->SetCursor(wxCursor(wxCURSOR_PENCIL));
301 subframe->canvas = canvas;
302
303 // Give it scrollbars
304 // canvas->SetScrollbars(20, 20, 50, 50, 4, 4);
305
306 subframe->Show(TRUE);
307 // Return the main frame window
308 return subframe;
309 }
310
311 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
312 EVT_PAINT(MyCanvas::OnPaint)
313 EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent)
314 END_EVENT_TABLE()
315
316 // Define a constructor for my canvas
317 MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size):
318 wxScrolledWindow(parent, -1, pos, size)
319 {
320 }
321
322 // Define the repainting behaviour
323 void MyCanvas::OnPaint(wxPaintEvent& event)
324 {
325 wxPaintDC dc(this);
326
327 dc.SetFont(* wxSWISS_FONT);
328 dc.SetPen(* wxGREEN_PEN);
329 dc.DrawLine(0, 0, 200, 200);
330 dc.DrawLine(200, 0, 0, 200);
331
332 dc.SetBrush(* wxCYAN_BRUSH);
333 dc.SetPen(* wxRED_PEN);
334 dc.DrawRectangle(100, 100, 100, 50);
335 dc.DrawRoundedRectangle(150, 150, 100, 50, 20);
336
337 dc.DrawEllipse(250, 250, 100, 50);
338 dc.DrawSpline(50, 200, 50, 100, 200, 10);
339 dc.DrawLine(50, 230, 200, 230);
340 dc.DrawText("This is a test string", 50, 230);
341 }
342
343 // This implements a tiny doodling program! Drag the mouse using
344 // the left button.
345 void MyCanvas::OnMouseEvent(wxMouseEvent& event)
346 {
347 wxClientDC dc(this);
348 dc.SetPen(* wxBLACK_PEN);
349 wxPoint pos = event.GetPosition();
350 if (xpos > -1 && ypos > -1 && event.Dragging())
351 {
352 dc.DrawLine(xpos, ypos, pos.x, pos.y);
353 }
354 xpos = pos.x;
355 ypos = pos.y;
356 }
357
358 BEGIN_EVENT_TABLE(MyChild, wxFrame)
359 EVT_MENU(HELLO_QUIT, MyChild::OnQuit)
360 EVT_MENU(HELLO_NEW, MyChild::OnNew)
361 EVT_ACTIVATE(MyChild::OnActivate)
362 END_EVENT_TABLE()
363
364 MyChild::MyChild(wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& size, const long style):
365 wxFrame(frame, -1, title, pos, size, style)
366 {
367 canvas = NULL;
368 }
369
370 MyChild::~MyChild(void)
371 {
372 }
373
374 void MyChild::OnQuit(wxCommandEvent& event)
375 {
376 Close(TRUE);
377 }
378
379 void MyChild::OnNew(wxCommandEvent& event)
380 {
381 CMainWindow *mainWin = new CMainWindow();
382 mainWin->ShowWindow( TRUE );
383 mainWin->UpdateWindow();
384 }
385
386 void MyChild::OnActivate(wxActivateEvent& event)
387 {
388 if (event.GetActive() && canvas)
389 canvas->SetFocus();
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