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