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