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