2003-08-06 08:32:14 -04:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: render.cpp
|
2004-05-25 07:20:37 -04:00
|
|
|
// Purpose: Render wxWidgets sample
|
2003-08-06 08:32:14 -04:00
|
|
|
// Author: Vadim Zeitlin
|
|
|
|
// Modified by:
|
|
|
|
// Created: 04.08.03
|
|
|
|
// RCS-ID: $Id$
|
2004-05-25 07:20:37 -04:00
|
|
|
// Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwidgets.org>
|
2003-08-06 08:32:14 -04:00
|
|
|
// Licence: wxWindows licence
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
// declarations
|
|
|
|
// ============================================================================
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// headers
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// For compilers that support precompilation, includes "wx/wx.h".
|
|
|
|
#include "wx/wxprec.h"
|
|
|
|
|
|
|
|
#ifdef __BORLANDC__
|
|
|
|
#pragma hdrstop
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef WX_PRECOMP
|
|
|
|
#include "wx/app.h"
|
|
|
|
#include "wx/frame.h"
|
2003-08-06 11:37:32 -04:00
|
|
|
#include "wx/dc.h"
|
|
|
|
#include "wx/dcclient.h"
|
|
|
|
#include "wx/panel.h"
|
|
|
|
#include "wx/menu.h"
|
|
|
|
#include "wx/textdlg.h"
|
|
|
|
#include "wx/log.h"
|
|
|
|
#include "wx/msgdlg.h"
|
2006-08-29 15:45:34 -04:00
|
|
|
#include "wx/icon.h"
|
2003-08-06 08:32:14 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "wx/apptrait.h"
|
|
|
|
#include "wx/renderer.h"
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// resources
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// the application icon (under Windows and OS/2 it is in resources)
|
|
|
|
#if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__)
|
|
|
|
#include "../sample.xpm"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// private classes
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// A renderer class draws the header buttons in a "special" way
|
|
|
|
class MyRenderer : public wxDelegateRendererNative
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MyRenderer() : wxDelegateRendererNative(wxRendererNative::GetDefault()) { }
|
|
|
|
|
2006-12-01 11:07:30 -05:00
|
|
|
virtual int DrawHeaderButton(wxWindow *WXUNUSED(win),
|
2003-08-06 08:32:14 -04:00
|
|
|
wxDC& dc,
|
|
|
|
const wxRect& rect,
|
2006-10-10 08:07:17 -04:00
|
|
|
int WXUNUSED(flags) = 0,
|
|
|
|
wxHeaderSortIconType WXUNUSED(sortArrow) = wxHDR_SORT_ICON_NONE,
|
|
|
|
wxHeaderButtonParams* WXUNUSED(params) = NULL)
|
2003-08-06 08:32:14 -04:00
|
|
|
{
|
|
|
|
dc.SetBrush(*wxBLUE_BRUSH);
|
|
|
|
dc.SetTextForeground(*wxWHITE);
|
|
|
|
dc.DrawRoundedRectangle(rect, 5);
|
|
|
|
dc.DrawLabel(_T("MyRenderer"), wxNullBitmap, rect, wxALIGN_CENTER);
|
2006-12-01 11:07:30 -05:00
|
|
|
return rect.width;
|
2003-08-06 08:32:14 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// To use a different renderer from the very beginning we must override
|
|
|
|
// wxAppTraits method creating the renderer (another, simpler, alternative is
|
|
|
|
// to call wxRendererNative::Set() a.s.a.p. which should work in 99% of the
|
|
|
|
// cases, but we show this here just for completeness)
|
|
|
|
class MyTraits : public wxGUIAppTraits
|
|
|
|
{
|
|
|
|
virtual wxRendererNative *CreateRenderer()
|
|
|
|
{
|
2004-05-25 07:20:37 -04:00
|
|
|
// it will be deleted on program shutdown by wxWidgets itself
|
2003-08-06 08:32:14 -04:00
|
|
|
return new MyRenderer;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Define a new application type, each program should derive a class from wxApp
|
|
|
|
class MyApp : public wxApp
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual bool OnInit();
|
|
|
|
|
|
|
|
// if we want MyTraits to be used we must override CreateTraits()
|
|
|
|
virtual wxAppTraits *CreateTraits() { return new MyTraits; }
|
|
|
|
};
|
|
|
|
|
|
|
|
// Define a new frame type: this is going to be our main frame
|
|
|
|
class MyFrame : public wxFrame
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// ctor(s)
|
|
|
|
MyFrame();
|
|
|
|
virtual ~MyFrame();
|
|
|
|
|
|
|
|
// event handlers (these functions should _not_ be virtual)
|
2006-11-05 19:59:50 -05:00
|
|
|
#if wxUSE_DYNLIB_CLASS
|
2003-08-06 08:32:14 -04:00
|
|
|
void OnLoad(wxCommandEvent& event);
|
|
|
|
void OnUnload(wxCommandEvent& event);
|
2006-11-05 19:59:50 -05:00
|
|
|
#endif // wxUSE_DYNLIB_CLASS
|
2003-08-06 08:32:14 -04:00
|
|
|
void OnQuit(wxCommandEvent& event);
|
|
|
|
void OnAbout(wxCommandEvent& event);
|
|
|
|
|
|
|
|
private:
|
|
|
|
wxPanel *m_panel;
|
|
|
|
|
2004-05-25 07:20:37 -04:00
|
|
|
// any class wishing to process wxWidgets events must use this macro
|
2003-08-06 08:32:14 -04:00
|
|
|
DECLARE_EVENT_TABLE()
|
|
|
|
};
|
|
|
|
|
|
|
|
// a very simple class just to have something to draw on
|
|
|
|
class MyPanel : public wxPanel
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MyPanel(wxWindow *parent) : wxPanel(parent) { }
|
|
|
|
|
|
|
|
void OnPaint(wxPaintEvent&)
|
|
|
|
{
|
|
|
|
wxPaintDC dc(this);
|
|
|
|
|
|
|
|
dc.DrawText(_T("Below is the standard header button drawn"), 10, 10);
|
|
|
|
dc.DrawText(_T("using the current renderer:"), 10, 40);
|
|
|
|
|
2007-09-03 17:50:28 -04:00
|
|
|
wxRendererNative& renderer = wxRendererNative::Get();
|
|
|
|
const wxCoord height = renderer.GetHeaderButtonHeight(this);
|
|
|
|
|
|
|
|
renderer.DrawHeaderButton(this, dc, wxRect(20, 70, 100, height));
|
2003-08-06 08:32:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
DECLARE_EVENT_TABLE()
|
|
|
|
};
|
|
|
|
|
|
|
|
BEGIN_EVENT_TABLE(MyPanel, wxPanel)
|
|
|
|
EVT_PAINT(MyPanel::OnPaint)
|
|
|
|
END_EVENT_TABLE()
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// constants
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// IDs for the controls and the menu commands
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
// our menu items
|
2006-11-05 19:59:50 -05:00
|
|
|
#if wxUSE_DYNLIB_CLASS
|
2003-08-06 08:32:14 -04:00
|
|
|
Render_Load = 100,
|
|
|
|
Render_Unload,
|
2006-11-05 19:59:50 -05:00
|
|
|
#endif // wxUSE_DYNLIB_CLASS
|
2003-08-06 08:32:14 -04:00
|
|
|
|
|
|
|
// standard menu items
|
|
|
|
Render_Quit = wxID_EXIT,
|
|
|
|
|
|
|
|
// it is important for the id corresponding to the "About" command to have
|
|
|
|
// this standard value as otherwise it won't be handled properly under Mac
|
|
|
|
// (where it is special and put into the "Apple" menu)
|
|
|
|
Render_About = wxID_ABOUT
|
|
|
|
};
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2004-05-25 07:20:37 -04:00
|
|
|
// event tables and other macros for wxWidgets
|
2003-08-06 08:32:14 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2004-05-25 07:20:37 -04:00
|
|
|
// the event tables connect the wxWidgets events with the functions (event
|
2003-08-06 08:32:14 -04:00
|
|
|
// handlers) which process them. It can be also done at run-time, but for the
|
|
|
|
// simple menu events like this the static method is much simpler.
|
|
|
|
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
|
2006-11-05 19:59:50 -05:00
|
|
|
#if wxUSE_DYNLIB_CLASS
|
2003-08-06 08:32:14 -04:00
|
|
|
EVT_MENU(Render_Load, MyFrame::OnLoad)
|
|
|
|
EVT_MENU(Render_Unload,MyFrame::OnUnload)
|
2006-11-05 19:59:50 -05:00
|
|
|
#endif // wxUSE_DYNLIB_CLASS
|
2003-08-06 08:32:14 -04:00
|
|
|
EVT_MENU(Render_Quit, MyFrame::OnQuit)
|
|
|
|
|
|
|
|
EVT_MENU(Render_About, MyFrame::OnAbout)
|
|
|
|
END_EVENT_TABLE()
|
|
|
|
|
2004-05-25 07:20:37 -04:00
|
|
|
// Create a new application object: this macro will allow wxWidgets to create
|
2003-08-06 08:32:14 -04:00
|
|
|
// the application object during program execution (it's better than using a
|
|
|
|
// static object for many reasons) and also implements the accessor function
|
|
|
|
// wxGetApp() which will return the reference of the right type (i.e. MyApp and
|
|
|
|
// not wxApp)
|
|
|
|
IMPLEMENT_APP(MyApp)
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
// implementation
|
|
|
|
// ============================================================================
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// the application class
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// 'Main program' equivalent: the program execution "starts" here
|
|
|
|
bool MyApp::OnInit()
|
|
|
|
{
|
2007-02-03 19:34:18 -05:00
|
|
|
if ( !wxApp::OnInit() )
|
|
|
|
return false;
|
|
|
|
|
2003-08-06 08:32:14 -04:00
|
|
|
// create the main application window
|
|
|
|
new MyFrame;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// main frame
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// frame constructor
|
|
|
|
MyFrame::MyFrame()
|
|
|
|
: wxFrame(NULL,
|
2006-10-10 08:07:17 -04:00
|
|
|
wxID_ANY,
|
2004-05-25 07:20:37 -04:00
|
|
|
_T("Render wxWidgets Sample"),
|
2003-08-06 08:32:14 -04:00
|
|
|
wxPoint(50, 50),
|
|
|
|
wxSize(450, 340))
|
|
|
|
{
|
|
|
|
// set the frame icon
|
|
|
|
SetIcon(wxICON(sample));
|
|
|
|
|
|
|
|
#if wxUSE_MENUS
|
|
|
|
// create a menu bar
|
|
|
|
wxMenu *menuFile = new wxMenu;
|
2006-11-05 19:59:50 -05:00
|
|
|
#if wxUSE_DYNLIB_CLASS
|
2003-08-06 08:32:14 -04:00
|
|
|
menuFile->Append(Render_Load, _T("&Load renderer...\tCtrl-L"));
|
|
|
|
menuFile->Append(Render_Unload, _T("&Unload renderer\tCtrl-U"));
|
2006-11-05 19:59:50 -05:00
|
|
|
#endif // wxUSE_DYNLIB_CLASS
|
2003-08-06 08:32:14 -04:00
|
|
|
menuFile->Append(Render_Quit, _T("E&xit\tCtrl-Q"), _T("Quit this program"));
|
|
|
|
|
|
|
|
// the "About" item should be in the help menu
|
|
|
|
wxMenu *helpMenu = new wxMenu;
|
|
|
|
helpMenu->Append(Render_About, _T("&About...\tF1"), _T("Show about dialog"));
|
|
|
|
|
|
|
|
// now append the freshly created menu to the menu bar...
|
|
|
|
wxMenuBar *menuBar = new wxMenuBar();
|
|
|
|
menuBar->Append(menuFile, _T("&File"));
|
|
|
|
menuBar->Append(helpMenu, _T("&Help"));
|
|
|
|
|
|
|
|
// ... and attach this menu bar to the frame
|
|
|
|
SetMenuBar(menuBar);
|
|
|
|
#endif // wxUSE_MENUS
|
|
|
|
|
|
|
|
m_panel = new MyPanel(this);
|
|
|
|
|
|
|
|
#if wxUSE_STATUSBAR
|
|
|
|
// create a status bar just for fun (by default with 1 pane only)
|
|
|
|
CreateStatusBar(2);
|
2004-05-25 07:20:37 -04:00
|
|
|
SetStatusText(_T("Welcome to wxWidgets!"));
|
2003-08-06 08:32:14 -04:00
|
|
|
#endif // wxUSE_STATUSBAR
|
|
|
|
|
|
|
|
Show();
|
|
|
|
}
|
|
|
|
|
|
|
|
MyFrame::~MyFrame()
|
|
|
|
{
|
|
|
|
delete wxRendererNative::Set(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// event handlers
|
|
|
|
|
2006-11-05 19:59:50 -05:00
|
|
|
#if wxUSE_DYNLIB_CLASS
|
|
|
|
|
2003-08-06 08:32:14 -04:00
|
|
|
void MyFrame::OnLoad(wxCommandEvent& WXUNUSED(event))
|
|
|
|
{
|
|
|
|
static wxString s_name = _T("renddll");
|
|
|
|
|
|
|
|
wxString name = wxGetTextFromUser
|
|
|
|
(
|
|
|
|
_T("Name of the renderer to load:"),
|
2004-05-25 07:20:37 -04:00
|
|
|
_T("Render wxWidgets Sample"),
|
2003-08-06 08:32:14 -04:00
|
|
|
s_name,
|
|
|
|
this
|
|
|
|
);
|
|
|
|
if ( name.empty() )
|
|
|
|
{
|
|
|
|
// cancelled
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
s_name = name;
|
|
|
|
|
|
|
|
wxRendererNative *renderer = wxRendererNative::Load(name);
|
|
|
|
if ( !renderer )
|
|
|
|
{
|
|
|
|
wxLogError(_T("Failed to load renderer \"%s\"."), name.c_str());
|
|
|
|
}
|
|
|
|
else // loaded ok
|
|
|
|
{
|
|
|
|
delete wxRendererNative::Set(renderer);
|
|
|
|
|
|
|
|
m_panel->Refresh();
|
|
|
|
|
|
|
|
wxLogStatus(this, _T("Successfully loaded the renderer \"%s\"."),
|
|
|
|
name.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyFrame::OnUnload(wxCommandEvent& WXUNUSED(event))
|
|
|
|
{
|
|
|
|
wxRendererNative *renderer = wxRendererNative::Set(NULL);
|
|
|
|
if ( renderer )
|
|
|
|
{
|
|
|
|
delete renderer;
|
|
|
|
|
|
|
|
m_panel->Refresh();
|
|
|
|
|
|
|
|
wxLogStatus(this, _T("Unloaded the previously loaded renderer."));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wxLogWarning(_T("No renderer to unload."));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-11-05 19:59:50 -05:00
|
|
|
#endif // wxUSE_DYNLIB_CLASS
|
|
|
|
|
2003-08-06 08:32:14 -04:00
|
|
|
void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
|
|
|
|
{
|
|
|
|
// true is to force the frame to close
|
|
|
|
Close(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
|
|
|
|
{
|
|
|
|
wxMessageBox(_T("Render sample shows how to use custom renderers.\n")
|
|
|
|
_T("\n")
|
2004-10-02 08:36:02 -04:00
|
|
|
_T("(c) 2003 Vadim Zeitlin"),
|
2004-05-25 07:20:37 -04:00
|
|
|
_T("About Render wxWidgets Sample"),
|
2003-08-06 08:32:14 -04:00
|
|
|
wxOK | wxICON_INFORMATION, this);
|
|
|
|
}
|
|
|
|
|