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