]> git.saurik.com Git - wxWidgets.git/blob - samples/mfc/mfctest.cpp
use wxDC::GetMultiLineTextExtent() instead of duplicating its code in wxButton::DoGet...
[wxWidgets.git] / samples / mfc / mfctest.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: mfctest.cpp
3 // Purpose: Sample to demonstrate mixing MFC and wxWidgets code
4 // Author: Julian Smart
5 // Id: $Id$
6 // Copyright: (c) Julian Smart
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // This sample pops up an initial wxWidgets frame, with a menu item
11 // that allows a new MFC window to be created. Note that CDummyWindow
12 // is a class that allows a wxWidgets window to be seen as a CWnd
13 // for the purposes of specifying a valid main window to the
14 // MFC initialisation.
15 //
16 // You can easily modify this code so that an MFC window pops up
17 // initially as the main frame, and allows wxWidgets frames to be
18 // created subsequently.
19 //
20 // (1) Make MyApp::OnInit not create a main window.
21 // (2) Make MFC's InitInstance create a main window, and remove
22 // creation of CDummyWindow.
23 //
24 // This can be accomplished by setting START_WITH_MFC_WINDOW to 1 below.
25
26 #define START_WITH_MFC_WINDOW 0
27
28 //
29 // IMPORTANT NOTES:
30 //
31 // (1) You may need to set wxUSE_MFC to 1 in include/wx/msw/setup.h but
32 // normally this shouldn't be needed any longer, i.e. it works without
33 // it for me (VZ)
34 //
35 // (2) You should link with MFC DLL, not static libraries
36
37 // suppress warning about WINVER not being defined from MFC
38 #ifndef WINVER
39 #define WINVER 0x7000
40 #endif
41
42 #include "stdafx.h"
43
44 // For compilers that support precompilation, includes "wx/wx.h".
45 #include "wx/wxprec.h"
46
47 #ifdef __BORLANDC__
48 #pragma hdrstop
49 #endif
50
51 #ifndef WX_PRECOMP
52 #include "wx/wx.h"
53 #endif
54
55 #include "wx/evtloop.h"
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 // wxWidgets elements
69
70 // Define a new application type
71 class MyApp: public wxApp
72 {
73 public:
74 virtual bool OnInit();
75
76 wxFrame *CreateFrame();
77 };
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 MyChild(wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& size, const long style);
92 virtual ~MyChild();
93
94 void OnQuit(wxCommandEvent& event);
95 void OnNew(wxCommandEvent& event);
96 void OnActivate(wxActivateEvent& event);
97
98 MyCanvas *canvas;
99
100 DECLARE_EVENT_TABLE()
101 };
102
103 // ID for the menu quit command
104 #define HELLO_QUIT 1
105 #define HELLO_NEW 2
106
107 DECLARE_APP(MyApp)
108
109 // notice use of IMPLEMENT_APP_NO_MAIN() instead of the usual IMPLEMENT_APP!
110 IMPLEMENT_APP_NO_MAIN(MyApp)
111
112 CMainWindow::CMainWindow()
113 {
114 LoadAccelTable( "MainAccelTable" );
115 Create( NULL, "Hello Foundation Application",
116 WS_OVERLAPPEDWINDOW, rectDefault, NULL, "MainMenu" );
117 }
118
119 void CMainWindow::OnPaint()
120 {
121 CString s = "Hello, Windows!";
122 CPaintDC dc( this );
123 CRect rect;
124
125 GetClientRect( rect );
126 dc.SetTextAlign( TA_BASELINE | TA_CENTER );
127 dc.SetTextColor( ::GetSysColor( COLOR_WINDOWTEXT ) );
128 dc.SetBkMode(TRANSPARENT);
129 dc.TextOut( ( rect.right / 2 ), ( rect.bottom / 2 ),
130 s, s.GetLength() );
131 }
132
133 void CMainWindow::OnAbout()
134 {
135 CDialog about( "AboutBox", this );
136 about.DoModal();
137 }
138
139 void CMainWindow::OnTest()
140 {
141 wxMessageBox("This is a wxWidgets message box.\nWe're about to create a new wxWidgets frame.", "wxWidgets", wxOK);
142 wxGetApp().CreateFrame();
143 }
144
145 // CMainWindow message map:
146 // Associate messages with member functions.
147 //
148 // It is implied that the ON_WM_PAINT macro expects a member function
149 // "void OnPaint()".
150 //
151 // It is implied that members connected with the ON_COMMAND macro
152 // receive no arguments and are void of return type, e.g., "void OnAbout()".
153 //
154 BEGIN_MESSAGE_MAP( CMainWindow, CFrameWnd )
155 //{{AFX_MSG_MAP( CMainWindow )
156 ON_WM_PAINT()
157 ON_COMMAND( IDM_ABOUT, OnAbout )
158 ON_COMMAND( IDM_TEST, OnTest )
159 //}}AFX_MSG_MAP
160 END_MESSAGE_MAP()
161
162 BOOL CTheApp::InitInstance()
163 {
164 if ( !CWinApp::InitInstance() )
165 return FALSE;
166
167 // TODO: cmd line parsing
168 WXDLLIMPEXP_BASE void wxSetInstance(HINSTANCE hInst);
169 wxSetInstance(m_hInstance);
170 wxApp::m_nCmdShow = m_nCmdShow;
171 int argc = 0;
172 char **argv = NULL;
173 wxEntryStart(argc, argv);
174 if ( !wxTheApp || !wxTheApp->CallOnInit() )
175 return FALSE;
176
177 #if START_WITH_MFC_WINDOW
178 // Demonstrate creation of an initial MFC main window.
179 m_pMainWnd = new CMainWindow();
180 m_pMainWnd->ShowWindow( m_nCmdShow );
181 m_pMainWnd->UpdateWindow();
182 #else
183 // Demonstrate creation of an initial wxWidgets main window.
184 // Wrap wxWidgets window in a dummy MFC window and
185 // make the main window.
186 if (wxTheApp && wxTheApp->GetTopWindow())
187 {
188 m_pMainWnd = new CDummyWindow((HWND) wxTheApp->GetTopWindow()->GetHWND());
189 }
190 #endif
191
192 return TRUE;
193 }
194
195 int CTheApp::ExitInstance()
196 {
197 #if !START_WITH_MFC_WINDOW
198 delete m_pMainWnd;
199 #endif
200
201 if ( wxTheApp )
202 wxTheApp->OnExit();
203 wxEntryCleanup();
204
205 return CWinApp::ExitInstance();
206 }
207
208 // Override this to provide wxWidgets message loop compatibility
209 BOOL CTheApp::PreTranslateMessage(MSG *msg)
210 {
211 wxEventLoop *evtLoop = wxEventLoop::GetActive();
212 if ( evtLoop && evtLoop->PreProcessMessage(msg) )
213 return TRUE;
214
215 return CWinApp::PreTranslateMessage(msg);
216 }
217
218 BOOL CTheApp::OnIdle(LONG WXUNUSED(lCount))
219 {
220 return wxTheApp && wxTheApp->ProcessIdle();
221 }
222
223 /*********************************************************************
224 * wxWidgets elements
225 ********************************************************************/
226
227 bool MyApp::OnInit()
228 {
229 #if !START_WITH_MFC_WINDOW
230
231 // Exit app when the top level frame is deleted
232 SetExitOnFrameDelete(TRUE);
233
234 (void) CreateFrame();
235 #endif
236
237 return TRUE;
238 }
239
240 wxFrame *MyApp::CreateFrame()
241 {
242 MyChild *subframe = new MyChild(NULL, "Canvas Frame", wxPoint(10, 10), wxSize(300, 300),
243 wxDEFAULT_FRAME_STYLE);
244
245 subframe->SetTitle("wxWidgets canvas frame");
246
247 // Give it a status line
248 subframe->CreateStatusBar();
249
250 // Make a menubar
251 wxMenu *file_menu = new wxMenu;
252
253 file_menu->Append(HELLO_NEW, "&New MFC Window");
254 file_menu->Append(HELLO_QUIT, "&Close");
255
256 wxMenuBar *menu_bar = new wxMenuBar;
257
258 menu_bar->Append(file_menu, "&File");
259
260 // Associate the menu bar with the frame
261 subframe->SetMenuBar(menu_bar);
262
263 int width, height;
264 subframe->GetClientSize(&width, &height);
265
266 MyCanvas *canvas = new MyCanvas(subframe, wxPoint(0, 0), wxSize(width, height));
267 canvas->SetCursor(wxCursor(wxCURSOR_PENCIL));
268 subframe->canvas = canvas;
269 subframe->Show(TRUE);
270
271 // Return the main frame window
272 return subframe;
273 }
274
275 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
276 EVT_PAINT(MyCanvas::OnPaint)
277 EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent)
278 END_EVENT_TABLE()
279
280 // Define a constructor for my canvas
281 MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size)
282 : wxScrolledWindow(parent, -1, pos, size)
283 {
284 }
285
286 // Define the repainting behaviour
287 void MyCanvas::OnPaint(wxPaintEvent& WXUNUSED(event))
288 {
289 wxPaintDC dc(this);
290
291 dc.SetFont(* wxSWISS_FONT);
292 dc.SetPen(* wxGREEN_PEN);
293 dc.DrawLine(0, 0, 200, 200);
294 dc.DrawLine(200, 0, 0, 200);
295
296 dc.SetBrush(* wxCYAN_BRUSH);
297 dc.SetPen(* wxRED_PEN);
298 dc.DrawRectangle(100, 100, 100, 50);
299 dc.DrawRoundedRectangle(150, 150, 100, 50, 20);
300
301 dc.DrawEllipse(250, 250, 100, 50);
302 dc.DrawLine(50, 230, 200, 230);
303 dc.DrawText("This is a test string", 50, 230);
304 }
305
306 // This implements a tiny doodling program! Drag the mouse using
307 // the left button.
308 void MyCanvas::OnMouseEvent(wxMouseEvent& event)
309 {
310 static long s_xpos = -1;
311 static long s_ypos = -1;
312
313 wxClientDC dc(this);
314 dc.SetPen(* wxBLACK_PEN);
315 wxPoint pos = event.GetPosition();
316 if (s_xpos > -1 && s_ypos > -1 && event.Dragging())
317 {
318 dc.DrawLine(s_xpos, s_ypos, pos.x, pos.y);
319 }
320
321 s_xpos = pos.x;
322 s_ypos = pos.y;
323 }
324
325 BEGIN_EVENT_TABLE(MyChild, wxFrame)
326 EVT_MENU(HELLO_QUIT, MyChild::OnQuit)
327 EVT_MENU(HELLO_NEW, MyChild::OnNew)
328 EVT_ACTIVATE(MyChild::OnActivate)
329 END_EVENT_TABLE()
330
331 MyChild::MyChild(wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& size, const long style)
332 : wxFrame(frame, -1, title, pos, size, style)
333 {
334 canvas = NULL;
335 }
336
337 MyChild::~MyChild()
338 {
339 if ( IsLastBeforeExit() )
340 PostQuitMessage(0);
341 }
342
343 void MyChild::OnQuit(wxCommandEvent& WXUNUSED(event))
344 {
345 Close(TRUE);
346 }
347
348 void MyChild::OnNew(wxCommandEvent& WXUNUSED(event))
349 {
350 CMainWindow *mainWin = new CMainWindow();
351 mainWin->ShowWindow( TRUE );
352 mainWin->UpdateWindow();
353 }
354
355 void MyChild::OnActivate(wxActivateEvent& event)
356 {
357 if (event.GetActive() && canvas)
358 canvas->SetFocus();
359 }
360
361 // Dummy MFC window for specifying a valid main window to MFC, using
362 // a wxWidgets HWND.
363 CDummyWindow::CDummyWindow(HWND hWnd)
364 {
365 Attach(hWnd);
366 }
367
368 // Don't let the CWnd destructor delete the HWND
369 CDummyWindow::~CDummyWindow()
370 {
371 Detach();
372 }
373