]> git.saurik.com Git - wxWidgets.git/blame - samples/mfc/mfctest.cpp
Fixed wrong cursor problem in wxGTK
[wxWidgets.git] / samples / mfc / mfctest.cpp
CommitLineData
bbf1f0e5
KB
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//
3f8e5072 29// (1) Make MyApp::OnInit return FALSE, not creating a window.
bbf1f0e5
KB
30// (2) Restore the MFC code to create a window in InitInstance, and remove
31// creation of CDummyWindow.
32//
3f8e5072
JS
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.
bbf1f0e5
KB
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
bbf1f0e5 57#include "wx/wx.h"
bbf1f0e5 58
818e52c2
JS
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
bbf1f0e5
KB
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
bbf1f0e5
KB
77/////////////////////////////////////////////////////////////////////////////
78
79// theApp:
80// Just creating this application object runs the whole application.
81//
82CTheApp theApp;
83
84// wxWindows elements
85
86// Define a new application type
87class MyApp: public wxApp
88{ public:
89 bool OnInit(void);
90 wxFrame *CreateFrame(void);
91 };
92
bbf1f0e5
KB
93class 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);
99DECLARE_EVENT_TABLE()
100};
101
102class 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);
bbf1f0e5
KB
108
109 void OnQuit(wxCommandEvent& event);
110 void OnNew(wxCommandEvent& event);
111 void OnActivate(wxActivateEvent& event);
112
113DECLARE_EVENT_TABLE()
114};
115
116// For drawing lines in a canvas
117long xpos = -1;
118long ypos = -1;
119
bbf1f0e5
KB
120// ID for the menu quit command
121#define HELLO_QUIT 1
122#define HELLO_NEW 2
123
124DECLARE_APP(MyApp)
125IMPLEMENT_APP(MyApp)
126
127/////////////////////////////////////////////////////////////////////////////
128
129// CMainWindow constructor:
130// Create the window with the appropriate style, size, menu, etc.
131//
132CMainWindow::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//
146void 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//
168void CMainWindow::OnAbout()
169{
170 CDialog about( "AboutBox", this );
171 about.DoModal();
172}
173
174void 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//
189BEGIN_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
195END_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//
207BOOL 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
229int CTheApp::ExitInstance()
230{
f5e5bd66
JS
231 // OnExit isn't called by CleanUp so must be called explicitly.
232 wxTheApp->OnExit();
bbf1f0e5
KB
233 wxApp::CleanUp();
234
235 return CWinApp::ExitInstance();
236}
237
238// Override this to provide wxWindows message loop
239// compatibility
240
241BOOL CTheApp::PreTranslateMessage(MSG *msg)
242{
243 if (wxTheApp && wxTheApp->ProcessMessage((WXMSG*) msg))
244 return TRUE;
245 else
246 return CWinApp::PreTranslateMessage(msg);
247}
248
249BOOL 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
261bool MyApp::OnInit(void)
262{
263 // Don't exit app when the top level frame is deleted
264// SetExitOnFrameDelete(FALSE);
265
bbf1f0e5
KB
266 wxFrame* frame = CreateFrame();
267 return TRUE;
268}
269
270wxFrame *MyApp::CreateFrame(void)
271{
272 MyChild *subframe = new MyChild(NULL, "Canvas Frame", wxPoint(10, 10), wxSize(300, 300),
17b74d79 273 wxDEFAULT_FRAME_STYLE);
bbf1f0e5
KB
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));
f5e5bd66 297 canvas->SetCursor(wxCursor(wxCURSOR_PENCIL));
bbf1f0e5
KB
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
308BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
309 EVT_PAINT(MyCanvas::OnPaint)
310 EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent)
311END_EVENT_TABLE()
312
313// Define a constructor for my canvas
314MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size):
315 wxScrolledWindow(parent, -1, pos, size)
316{
317}
318
319// Define the repainting behaviour
320void MyCanvas::OnPaint(wxPaintEvent& event)
321{
322 wxPaintDC dc(this);
323
f5e5bd66 324 dc.SetFont(* wxSWISS_FONT);
17b74d79 325 dc.SetPen(* wxGREEN_PEN);
bbf1f0e5
KB
326 dc.DrawLine(0, 0, 200, 200);
327 dc.DrawLine(200, 0, 0, 200);
328
17b74d79
JS
329 dc.SetBrush(* wxCYAN_BRUSH);
330 dc.SetPen(* wxRED_PEN);
bbf1f0e5
KB
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.
342void MyCanvas::OnMouseEvent(wxMouseEvent& event)
343{
344 wxClientDC dc(this);
17b74d79 345 dc.SetPen(* wxBLACK_PEN);
3f8e5072 346 wxPoint pos = event.GetPosition();
bbf1f0e5
KB
347 if (xpos > -1 && ypos > -1 && event.Dragging())
348 {
3f8e5072 349 dc.DrawLine(xpos, ypos, pos.x, pos.y);
bbf1f0e5 350 }
3f8e5072
JS
351 xpos = pos.x;
352 ypos = pos.y;
bbf1f0e5
KB
353}
354
355BEGIN_EVENT_TABLE(MyChild, wxFrame)
356 EVT_MENU(HELLO_QUIT, MyChild::OnQuit)
357 EVT_MENU(HELLO_NEW, MyChild::OnNew)
358 EVT_ACTIVATE(MyChild::OnActivate)
359END_EVENT_TABLE()
360
361MyChild::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
367MyChild::~MyChild(void)
368{
369}
370
371void MyChild::OnQuit(wxCommandEvent& event)
372{
373 Close(TRUE);
374}
375
376void MyChild::OnNew(wxCommandEvent& event)
377{
378 CMainWindow *mainWin = new CMainWindow();
379 mainWin->ShowWindow( TRUE );
380 mainWin->UpdateWindow();
381}
382
383void MyChild::OnActivate(wxActivateEvent& event)
384{
385 if (event.GetActive() && canvas)
386 canvas->SetFocus();
387}
388
bbf1f0e5
KB
389// Dummy MFC window for specifying a valid main window to MFC, using
390// a wxWindows HWND.
391CDummyWindow::CDummyWindow(HWND hWnd):CWnd()
392{
393 Attach(hWnd);
394}
395
396// Don't let the CWnd destructor delete the HWND
397CDummyWindow::~CDummyWindow(void)
398{
399 Detach();
400}
401