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