X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/e306597309a120f2ae91385c731a5cb2722c52aa..f1d5aa4eca541a2f90044a63f555985d6bd4adf3:/samples/mfc/mfctest.cpp diff --git a/samples/mfc/mfctest.cpp b/samples/mfc/mfctest.cpp index 91ed30dbdc..245feb580c 100644 --- a/samples/mfc/mfctest.cpp +++ b/samples/mfc/mfctest.cpp @@ -1,39 +1,60 @@ -// hello.cpp : Defines the class behaviors for the application. -// Hello is a simple program which consists of a main window -// and an "About" dialog which can be invoked by a menu choice. -// It is intended to serve as a starting-point for new -// applications. -// -// This is a part of the Microsoft Foundation Classes C++ library. -// Copyright (C) 1992 Microsoft Corporation -// All rights reserved. -// -// This source code is only intended as a supplement to the -// Microsoft Foundation Classes Reference and Microsoft -// WinHelp documentation provided with the library. -// See these sources for detailed information regarding the -// Microsoft Foundation Classes product. +///////////////////////////////////////////////////////////////////////////// +// Name: mfctest.cpp +// Purpose: Sample to demonstrate mixing MFC and wxWidgets code +// Author: Julian Smart +// Id: $Id$ +// Copyright: (c) Julian Smart +// Licence: wxWindows licence +///////////////////////////////////////////////////////////////////////////// -// *** MODIFIED BY JULIAN SMART TO DEMONSTRATE CO-EXISTANCE WITH wxWINDOWS *** -// -// This sample pops up an initial wxWindows frame, with a menu item +// This sample pops up an initial wxWidgets frame, with a menu item // that allows a new MFC window to be created. Note that CDummyWindow -// is a class that allows a wxWindows window to be seen as a CWnd +// is a class that allows a wxWidgets window to be seen as a CWnd // for the purposes of specifying a valid main window to the // MFC initialisation. // // You can easily modify this code so that an MFC window pops up -// initially as the main frame, and allows wxWindows frames to be -// created subsequently: +// initially as the main frame, and allows wxWidgets frames to be +// created subsequently. // -// (1) Make MyApp::OnInit return NULL, not create a window. -// (2) Restore the MFC code to create a window in InitInstance, and remove +// (1) Make MyApp::OnInit not create a main window. +// (2) Make MFC's InitInstance create a main window, and remove // creation of CDummyWindow. // -// IMPORTANT NOTE: to compile this sample, you must first edit -// wx/src/msw/wx_main.cc, set NOWINMAIN to 1, and remake wxWindows -// (it only needs to recompile wx_main.cc). -// This eliminates the duplicate WinMain function which MFC implements. +// This can be accomplished by setting START_WITH_MFC_WINDOW to 1 below. + +#define START_WITH_MFC_WINDOW 0 + +// +// IMPORTANT NOTES: +// +// (1) You may need to set wxUSE_MFC to 1 in include/wx/msw/setup.h but +// normally this shouldn't be needed any longer, i.e. it works without +// it for me (VZ) +// +// (2) You should link with MFC DLL, not static libraries: or, to use static +// run-time libraries, use this command for both building wxWidgets and +// the sample: +// +// nmake -f makefile.vc BUILD=debug SHARED=0 DEBUG_RUNTIME_LIBS=0 RUNTIME_LIBS=static all +// +// Unless the run-time library settings match for wxWidgets and MFC, you +// will get link errors for symbols such as __mbctype, __argc, and __argv +// +// (3) If you see bogus memory leaks within the MSVC IDE on exit, in this +// sample or in your own project, you must be using __WXDEBUG__ + +// WXUSINGDLL + _AFXDLL +// Unfortunately this confuses the MSVC/MFC leak detector. To do away with +// these bogus memory leaks, add this to the list of link objects, make it +// first: mfc[version][u]d.lib +// - [version] -> 42 or 70 or 80 etc +// - u if using Unicode +// +// (4) Unicode builds may produce the linker error "unresolved external symbol _WinMain@16". +// MFC requires you to manually add the Unicode entry point to the linker settings, +// Entry point symbol -> wWinMainCRTStartup + +#include "stdafx.h" // For compilers that support precompilation, includes "wx/wx.h". #include "wx/wxprec.h" @@ -42,21 +63,11 @@ #pragma hdrstop #endif -#include "wx/wx.h" - -#ifdef _WINDOWS_ -#error Sorry, you need to edit include/wx/wxprec.h, comment out the windows.h inclusion, and recompile. -#endif - -#ifdef new -#undef new +#ifndef WX_PRECOMP + #include "wx/wx.h" #endif -#include "stdafx.h" - -#ifdef DrawText -#undef DrawText -#endif +#include "wx/evtloop.h" #include "resource.h" @@ -69,100 +80,85 @@ // CTheApp theApp; -// wxWindows elements +// wxWidgets elements // Define a new application type class MyApp: public wxApp -{ public: - bool OnInit(void); - wxFrame *CreateFrame(void); - }; +{ +public: + virtual bool OnInit(); + + // we need to override this as the default behaviour only works when we're + // running wxWidgets main loop, not MFC one + virtual void ExitMainLoop(); + + wxFrame *CreateFrame(); +}; class MyCanvas: public wxScrolledWindow { - public: +public: MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size); void OnPaint(wxPaintEvent& event); void OnMouseEvent(wxMouseEvent& event); -DECLARE_EVENT_TABLE() + DECLARE_EVENT_TABLE() }; class MyChild: public wxFrame { - public: - MyCanvas *canvas; +public: MyChild(wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& size, const long style); - ~MyChild(void); + virtual ~MyChild(); void OnQuit(wxCommandEvent& event); void OnNew(wxCommandEvent& event); void OnActivate(wxActivateEvent& event); -DECLARE_EVENT_TABLE() -}; + MyCanvas *canvas; -// For drawing lines in a canvas -long xpos = -1; -long ypos = -1; + DECLARE_EVENT_TABLE() +}; // ID for the menu quit command #define HELLO_QUIT 1 #define HELLO_NEW 2 DECLARE_APP(MyApp) -IMPLEMENT_APP(MyApp) -///////////////////////////////////////////////////////////////////////////// +// notice use of IMPLEMENT_APP_NO_MAIN() instead of the usual IMPLEMENT_APP! +IMPLEMENT_APP_NO_MAIN(MyApp) -// CMainWindow constructor: -// Create the window with the appropriate style, size, menu, etc. -// CMainWindow::CMainWindow() { - LoadAccelTable( "MainAccelTable" ); - Create( NULL, "Hello Foundation Application", - WS_OVERLAPPEDWINDOW, rectDefault, NULL, "MainMenu" ); + LoadAccelTable( _T("MainAccelTable") ); + Create( NULL, _T("Hello Foundation Application"), + WS_OVERLAPPEDWINDOW, rectDefault, NULL, _T("MainMenu") ); } -// OnPaint: -// This routine draws the string "Hello, Windows!" in the center of the -// client area. It is called whenever Windows sends a WM_PAINT message. -// Note that creating a CPaintDC automatically does a BeginPaint and -// an EndPaint call is done when it is destroyed at the end of this -// function. CPaintDC's constructor needs the window (this). -// void CMainWindow::OnPaint() { - CString s = "Hello, Windows!"; - CPaintDC dc( this ); - CRect rect; - - GetClientRect( rect ); - dc.SetTextAlign( TA_BASELINE | TA_CENTER ); - dc.SetTextColor( ::GetSysColor( COLOR_WINDOWTEXT ) ); - dc.SetBkMode(TRANSPARENT); - dc.TextOut( ( rect.right / 2 ), ( rect.bottom / 2 ), - s, s.GetLength() ); + CString s = _T("Hello, Windows!"); + CPaintDC dc( this ); + CRect rect; + + GetClientRect( rect ); + dc.SetTextAlign( TA_BASELINE | TA_CENTER ); + dc.SetTextColor( ::GetSysColor( COLOR_WINDOWTEXT ) ); + dc.SetBkMode(TRANSPARENT); + dc.TextOut( ( rect.right / 2 ), ( rect.bottom / 2 ), + s, s.GetLength() ); } -// OnAbout: -// This member function is called when a WM_COMMAND message with an -// IDM_ABOUT code is received by the CMainWindow class object. The -// message map below is responsible for this routing. -// -// We create a ClDialog object using the "AboutBox" resource (see -// hello.rc), and invoke it. -// void CMainWindow::OnAbout() { - CDialog about( "AboutBox", this ); - about.DoModal(); + CDialog about( _T("AboutBox"), this ); + about.DoModal(); } void CMainWindow::OnTest() { - wxMessageBox("This is a wxWindows message box.\nWe're about to create a new wxWindows frame.", "wxWindows", wxOK); - wxGetApp().CreateFrame(); + wxMessageBox(_T("This is a wxWidgets message box.\nWe're about to create a new wxWidgets frame."), _T("wxWidgets"), wxOK); + wxGetApp().CreateFrame(); } // CMainWindow message map: @@ -175,122 +171,134 @@ void CMainWindow::OnTest() // receive no arguments and are void of return type, e.g., "void OnAbout()". // BEGIN_MESSAGE_MAP( CMainWindow, CFrameWnd ) - //{{AFX_MSG_MAP( CMainWindow ) - ON_WM_PAINT() - ON_COMMAND( IDM_ABOUT, OnAbout ) - ON_COMMAND( IDM_TEST, OnTest ) - //}}AFX_MSG_MAP +//{{AFX_MSG_MAP( CMainWindow ) +ON_WM_PAINT() +ON_COMMAND( IDM_ABOUT, OnAbout ) +ON_COMMAND( IDM_TEST, OnTest ) +//}}AFX_MSG_MAP END_MESSAGE_MAP() -///////////////////////////////////////////////////////////////////////////// -// CTheApp - -// InitInstance: -// When any CTheApp object is created, this member function is automatically -// called. Any data may be set up at this point. -// -// Also, the main window of the application should be created and shown here. -// Return TRUE if the initialization is successful. -// BOOL CTheApp::InitInstance() { - TRACE( "HELLO WORLD\n" ); - - SetDialogBkColor(); // hook gray dialogs (was default in MFC V1) - - wxEntry((WXHINSTANCE) m_hInstance, (WXHINSTANCE) m_hPrevInstance, m_lpCmdLine, m_nCmdShow, FALSE); + if ( !CWinApp::InitInstance() ) + return FALSE; -/* - m_pMainWnd = new CMainWindow(); - m_pMainWnd->ShowWindow( m_nCmdShow ); - m_pMainWnd->UpdateWindow(); -*/ + // TODO: cmd line parsing + WXDLLIMPEXP_BASE void wxSetInstance(HINSTANCE hInst); + wxSetInstance(m_hInstance); + wxApp::m_nCmdShow = m_nCmdShow; + int argc = 0; + wxChar **argv = NULL; + wxEntryStart(argc, argv); + if ( !wxTheApp || !wxTheApp->CallOnInit() ) + return FALSE; +#if START_WITH_MFC_WINDOW + // Demonstrate creation of an initial MFC main window. + m_pMainWnd = new CMainWindow(); + m_pMainWnd->ShowWindow( m_nCmdShow ); + m_pMainWnd->UpdateWindow(); +#else + // Demonstrate creation of an initial wxWidgets main window. + // Wrap wxWidgets window in a dummy MFC window and + // make the main window. if (wxTheApp && wxTheApp->GetTopWindow()) { m_pMainWnd = new CDummyWindow((HWND) wxTheApp->GetTopWindow()->GetHWND()); } +#endif - return TRUE; + return TRUE; } int CTheApp::ExitInstance() { - // OnExit isn't called by CleanUp so must be called explicitly. - wxTheApp->OnExit(); - wxApp::CleanUp(); +#if !START_WITH_MFC_WINDOW + delete m_pMainWnd; +#endif - return CWinApp::ExitInstance(); -} + if ( wxTheApp ) + wxTheApp->OnExit(); + wxEntryCleanup(); -// Override this to provide wxWindows message loop -// compatibility + return CWinApp::ExitInstance(); +} +// Override this to provide wxWidgets message loop compatibility BOOL CTheApp::PreTranslateMessage(MSG *msg) { - if (wxTheApp && wxTheApp->ProcessMessage((WXMSG*) msg)) - return TRUE; - else + wxEventLoop * const + evtLoop = wx_static_cast(wxEventLoop *, wxEventLoop::GetActive()); + if ( evtLoop && evtLoop->PreProcessMessage(msg) ) + return TRUE; + return CWinApp::PreTranslateMessage(msg); } -BOOL CTheApp::OnIdle(LONG lCount) +BOOL CTheApp::OnIdle(LONG WXUNUSED(lCount)) { - if (wxTheApp) - return wxTheApp->ProcessIdle(); - else - return FALSE; + return wxTheApp && wxTheApp->ProcessIdle(); } /********************************************************************* - * wxWindows elements - ********************************************************************/ - -bool MyApp::OnInit(void) +* wxWidgets elements +********************************************************************/ + +bool MyApp::OnInit() { - // Don't exit app when the top level frame is deleted -// SetExitOnFrameDelete(FALSE); - - wxFrame* frame = CreateFrame(); - return TRUE; + if ( !wxApp::OnInit() ) + return false; + +#if !START_WITH_MFC_WINDOW + // as we're not inside wxWidgets main loop, the default logic doesn't work + // in our case and we need to do this explicitly + SetExitOnFrameDelete(true); + + (void) CreateFrame(); +#endif + + return true; } -wxFrame *MyApp::CreateFrame(void) +void MyApp::ExitMainLoop() { - MyChild *subframe = new MyChild(NULL, "Canvas Frame", wxPoint(10, 10), wxSize(300, 300), - wxDEFAULT_FRAME_STYLE); + // instead of existing wxWidgets main loop, terminate the MFC one + ::PostQuitMessage(0); +} - subframe->SetTitle("wxWindows canvas frame"); +wxFrame *MyApp::CreateFrame() +{ + MyChild *subframe = new MyChild(NULL, _T("Canvas Frame"), wxPoint(10, 10), wxSize(300, 300), + wxDEFAULT_FRAME_STYLE); - // Give it a status line - subframe->CreateStatusBar(); + subframe->SetTitle(_T("wxWidgets canvas frame")); - // Make a menubar - wxMenu *file_menu = new wxMenu; + // Give it a status line + subframe->CreateStatusBar(); - file_menu->Append(HELLO_NEW, "&New MFC Window"); - file_menu->Append(HELLO_QUIT, "&Close"); + // Make a menubar + wxMenu *file_menu = new wxMenu; - wxMenuBar *menu_bar = new wxMenuBar; + file_menu->Append(HELLO_NEW, _T("&New MFC Window")); + file_menu->Append(HELLO_QUIT, _T("&Close")); - menu_bar->Append(file_menu, "&File"); + wxMenuBar *menu_bar = new wxMenuBar; - // Associate the menu bar with the frame - subframe->SetMenuBar(menu_bar); + menu_bar->Append(file_menu, _T("&File")); - int width, height; - subframe->GetClientSize(&width, &height); + // Associate the menu bar with the frame + subframe->SetMenuBar(menu_bar); - MyCanvas *canvas = new MyCanvas(subframe, wxPoint(0, 0), wxSize(width, height)); - canvas->SetCursor(wxCursor(wxCURSOR_PENCIL)); - subframe->canvas = canvas; + int width, height; + subframe->GetClientSize(&width, &height); - // Give it scrollbars -// canvas->SetScrollbars(20, 20, 50, 50, 4, 4); + MyCanvas *canvas = new MyCanvas(subframe, wxPoint(0, 0), wxSize(width, height)); + canvas->SetCursor(wxCursor(wxCURSOR_PENCIL)); + subframe->canvas = canvas; + subframe->Show(true); - subframe->Show(TRUE); - // Return the main frame window - return subframe; + // Return the main frame window + return subframe; } BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow) @@ -299,13 +307,13 @@ BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow) END_EVENT_TABLE() // Define a constructor for my canvas -MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size): - wxScrolledWindow(parent, -1, pos, size) +MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size) + : wxScrolledWindow(parent, -1, pos, size) { } // Define the repainting behaviour -void MyCanvas::OnPaint(wxPaintEvent& event) +void MyCanvas::OnPaint(wxPaintEvent& WXUNUSED(event)) { wxPaintDC dc(this); @@ -320,25 +328,27 @@ void MyCanvas::OnPaint(wxPaintEvent& event) dc.DrawRoundedRectangle(150, 150, 100, 50, 20); dc.DrawEllipse(250, 250, 100, 50); - dc.DrawSpline(50, 200, 50, 100, 200, 10); dc.DrawLine(50, 230, 200, 230); - dc.DrawText("This is a test string", 50, 230); + dc.DrawText(_T("This is a test string"), 50, 230); } // This implements a tiny doodling program! Drag the mouse using // the left button. void MyCanvas::OnMouseEvent(wxMouseEvent& event) { + static long s_xpos = -1; + static long s_ypos = -1; + wxClientDC dc(this); dc.SetPen(* wxBLACK_PEN); - long x, y; - event.Position(&x, &y); - if (xpos > -1 && ypos > -1 && event.Dragging()) + wxPoint pos = event.GetPosition(); + if (s_xpos > -1 && s_ypos > -1 && event.Dragging()) { - dc.DrawLine(xpos, ypos, x, y); + dc.DrawLine(s_xpos, s_ypos, pos.x, pos.y); } - xpos = x; - ypos = y; + + s_xpos = pos.x; + s_ypos = pos.y; } BEGIN_EVENT_TABLE(MyChild, wxFrame) @@ -347,44 +357,46 @@ BEGIN_EVENT_TABLE(MyChild, wxFrame) EVT_ACTIVATE(MyChild::OnActivate) END_EVENT_TABLE() -MyChild::MyChild(wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& size, const long style): - wxFrame(frame, -1, title, pos, size, style) +MyChild::MyChild(wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& size, const long style) + : wxFrame(frame, -1, title, pos, size, style) { - canvas = NULL; + canvas = NULL; } -MyChild::~MyChild(void) +MyChild::~MyChild() { + if ( IsLastBeforeExit() ) + PostQuitMessage(0); } -void MyChild::OnQuit(wxCommandEvent& event) +void MyChild::OnQuit(wxCommandEvent& WXUNUSED(event)) { - Close(TRUE); + Close(true); } -void MyChild::OnNew(wxCommandEvent& event) +void MyChild::OnNew(wxCommandEvent& WXUNUSED(event)) { CMainWindow *mainWin = new CMainWindow(); mainWin->ShowWindow( TRUE ); mainWin->UpdateWindow(); } - + void MyChild::OnActivate(wxActivateEvent& event) { - if (event.GetActive() && canvas) - canvas->SetFocus(); + if (event.GetActive() && canvas) + canvas->SetFocus(); } // Dummy MFC window for specifying a valid main window to MFC, using -// a wxWindows HWND. -CDummyWindow::CDummyWindow(HWND hWnd):CWnd() +// a wxWidgets HWND. +CDummyWindow::CDummyWindow(HWND hWnd) { - Attach(hWnd); + Attach(hWnd); } // Don't let the CWnd destructor delete the HWND -CDummyWindow::~CDummyWindow(void) +CDummyWindow::~CDummyWindow() { - Detach(); + Detach(); }