2001-06-11 16:03:00 -04:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: erase.cpp
|
2004-05-25 07:20:37 -04:00
|
|
|
// Purpose: Erase wxWidgets sample
|
2001-06-11 16:03:00 -04:00
|
|
|
// Author: Robert Roebling
|
|
|
|
// Modified by:
|
|
|
|
// Created: 04/01/98
|
|
|
|
// RCS-ID: $Id$
|
|
|
|
// Copyright: (c) Robert Roebling
|
|
|
|
// Licence: wxWindows licence
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
// declarations
|
|
|
|
// ============================================================================
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// headers
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// For compilers that support precompilation, includes "wx/wx.h".
|
|
|
|
#include "wx/wxprec.h"
|
|
|
|
|
|
|
|
#ifdef __BORLANDC__
|
|
|
|
#pragma hdrstop
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// for all others, include the necessary headers (this file is usually all you
|
2004-05-25 07:20:37 -04:00
|
|
|
// need because it includes almost all "standard" wxWidgets headers)
|
2001-06-11 16:03:00 -04:00
|
|
|
#ifndef WX_PRECOMP
|
|
|
|
#include "wx/wx.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// resources
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// the application icon
|
2002-02-16 12:54:35 -05:00
|
|
|
#if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__)
|
2001-06-11 16:03:00 -04:00
|
|
|
#include "mondrian.xpm"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// private classes
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class MyApp : public wxApp
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual bool OnInit();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2005-02-12 20:14:36 -05:00
|
|
|
class MyCanvas : public wxScrolledWindow
|
2001-06-11 16:03:00 -04:00
|
|
|
{
|
|
|
|
public:
|
2005-02-12 20:14:36 -05:00
|
|
|
MyCanvas( wxFrame *parent );
|
2001-06-11 16:03:00 -04:00
|
|
|
|
2005-02-12 20:14:36 -05:00
|
|
|
void UseBuffer(bool useBuffer) { m_useBuffer = useBuffer; Refresh(); }
|
2005-02-13 10:42:31 -05:00
|
|
|
void EraseBg(bool eraseBg) { m_eraseBg = eraseBg; Refresh(); }
|
2001-06-11 16:03:00 -04:00
|
|
|
|
|
|
|
private:
|
|
|
|
void OnPaint( wxPaintEvent &event );
|
2002-12-04 09:11:26 -05:00
|
|
|
void OnChar( wxKeyEvent &event );
|
2001-06-11 16:03:00 -04:00
|
|
|
void OnEraseBackground( wxEraseEvent &event );
|
2002-05-05 17:31:08 -04:00
|
|
|
|
2005-02-12 20:14:36 -05:00
|
|
|
void DoPaint(wxDC& dc);
|
|
|
|
|
|
|
|
|
2002-03-29 13:01:50 -05:00
|
|
|
wxBitmap m_bitmap;
|
2002-12-04 09:11:26 -05:00
|
|
|
wxString m_text;
|
2001-06-11 16:03:00 -04:00
|
|
|
|
2005-02-12 20:14:36 -05:00
|
|
|
// use wxMemoryDC in OnPaint()?
|
|
|
|
bool m_useBuffer;
|
|
|
|
|
2005-02-13 10:42:31 -05:00
|
|
|
// paint custom background in OnEraseBackground()?
|
|
|
|
bool m_eraseBg;
|
|
|
|
|
|
|
|
|
2005-02-12 20:14:36 -05:00
|
|
|
DECLARE_EVENT_TABLE()
|
|
|
|
};
|
|
|
|
|
|
|
|
class MyFrame : public wxFrame
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MyFrame();
|
|
|
|
|
|
|
|
void OnUseBuffer(wxCommandEvent& event);
|
2005-02-13 10:42:31 -05:00
|
|
|
void OnEraseBg(wxCommandEvent& event);
|
2005-02-12 20:14:36 -05:00
|
|
|
void OnQuit(wxCommandEvent& event);
|
|
|
|
void OnAbout(wxCommandEvent& event);
|
|
|
|
|
2001-06-11 16:03:00 -04:00
|
|
|
private:
|
2005-02-12 20:14:36 -05:00
|
|
|
MyCanvas *m_canvas;
|
|
|
|
|
2001-06-11 16:03:00 -04:00
|
|
|
DECLARE_EVENT_TABLE()
|
|
|
|
};
|
|
|
|
|
2005-02-12 20:14:36 -05:00
|
|
|
|
2001-06-11 16:03:00 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// constants
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
// menu items
|
2005-02-12 20:14:36 -05:00
|
|
|
Erase_Menu_UseBuffer = 100,
|
2005-02-13 10:42:31 -05:00
|
|
|
Erase_Menu_EraseBg,
|
2005-02-12 20:14:36 -05:00
|
|
|
Erase_Menu_Exit = wxID_EXIT,
|
|
|
|
Erase_Menu_About = wxID_ABOUT
|
2001-06-11 16:03:00 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// the application class
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
IMPLEMENT_APP(MyApp)
|
|
|
|
|
|
|
|
bool MyApp::OnInit()
|
|
|
|
{
|
2007-02-03 19:34:18 -05:00
|
|
|
if ( !wxApp::OnInit() )
|
|
|
|
return false;
|
|
|
|
|
2005-02-12 20:14:36 -05:00
|
|
|
MyFrame *frame = new MyFrame;
|
2001-06-11 16:03:00 -04:00
|
|
|
|
2004-05-28 06:43:39 -04:00
|
|
|
frame->Show(true);
|
2002-05-05 17:31:08 -04:00
|
|
|
|
2004-05-28 06:43:39 -04:00
|
|
|
return true;
|
2001-06-11 16:03:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// main frame
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
|
2005-02-12 20:14:36 -05:00
|
|
|
EVT_MENU(Erase_Menu_UseBuffer, MyFrame::OnUseBuffer)
|
2005-02-13 10:42:31 -05:00
|
|
|
EVT_MENU(Erase_Menu_EraseBg, MyFrame::OnEraseBg)
|
2005-02-12 20:14:36 -05:00
|
|
|
EVT_MENU(Erase_Menu_Exit, MyFrame::OnQuit)
|
|
|
|
EVT_MENU(Erase_Menu_About, MyFrame::OnAbout)
|
2001-06-11 16:03:00 -04:00
|
|
|
END_EVENT_TABLE()
|
|
|
|
|
|
|
|
// frame constructor
|
2005-02-12 20:14:36 -05:00
|
|
|
MyFrame::MyFrame()
|
|
|
|
: wxFrame(NULL, wxID_ANY, _T("Erase sample"),
|
|
|
|
wxPoint(50, 50), wxSize(450, 340))
|
2001-06-11 16:03:00 -04:00
|
|
|
{
|
|
|
|
SetIcon(wxICON(mondrian));
|
|
|
|
|
2002-12-14 13:13:27 -05:00
|
|
|
wxMenu *menuFile = new wxMenu(_T(""), wxMENU_TEAROFF);
|
2005-02-12 20:14:36 -05:00
|
|
|
menuFile->AppendCheckItem(Erase_Menu_UseBuffer, _T("&Use memory DC\tCtrl-M"));
|
2005-02-13 10:42:31 -05:00
|
|
|
menuFile->AppendCheckItem(Erase_Menu_EraseBg, _T("Custom &background\tCtrl-B"));
|
2005-02-12 20:14:36 -05:00
|
|
|
menuFile->AppendSeparator();
|
|
|
|
menuFile->Append(Erase_Menu_Exit, _T("E&xit\tAlt-X"), _T("Quit this program"));
|
2001-06-11 16:03:00 -04:00
|
|
|
|
|
|
|
|
2005-02-12 20:14:36 -05:00
|
|
|
wxMenu *helpMenu = new wxMenu;
|
|
|
|
helpMenu->Append(Erase_Menu_About, _T("&About...\tCtrl-A"), _T("Show about dialog"));
|
2001-06-11 16:03:00 -04:00
|
|
|
|
|
|
|
wxMenuBar *menuBar = new wxMenuBar();
|
2002-05-05 17:31:08 -04:00
|
|
|
menuBar->Append(menuFile, _T("&File"));
|
|
|
|
menuBar->Append(helpMenu, _T("&Help"));
|
2001-06-11 16:03:00 -04:00
|
|
|
|
|
|
|
SetMenuBar(menuBar);
|
|
|
|
|
|
|
|
#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 erase sample!"));
|
2001-06-11 16:03:00 -04:00
|
|
|
#endif // wxUSE_STATUSBAR
|
|
|
|
|
2005-02-12 20:14:36 -05:00
|
|
|
m_canvas = new MyCanvas( this );
|
2001-06-11 16:03:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-02-12 20:14:36 -05:00
|
|
|
void MyFrame::OnUseBuffer(wxCommandEvent& event)
|
|
|
|
{
|
|
|
|
m_canvas->UseBuffer(event.IsChecked());
|
|
|
|
}
|
|
|
|
|
2005-02-13 10:42:31 -05:00
|
|
|
void MyFrame::OnEraseBg(wxCommandEvent& event)
|
|
|
|
{
|
|
|
|
m_canvas->EraseBg(event.IsChecked());
|
|
|
|
}
|
|
|
|
|
2001-06-11 16:03:00 -04:00
|
|
|
void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
|
|
|
|
{
|
2004-05-28 06:43:39 -04:00
|
|
|
Close(true);
|
2001-06-11 16:03:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
|
|
|
|
{
|
2002-05-05 17:31:08 -04:00
|
|
|
wxMessageBox(_T("This sample shows how you can draw custom background."),
|
|
|
|
_T("About Erase Sample"), wxOK | wxICON_INFORMATION, this);
|
2001-06-11 16:03:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
|
|
|
|
EVT_PAINT( MyCanvas::OnPaint)
|
2002-12-04 09:11:26 -05:00
|
|
|
EVT_CHAR( MyCanvas::OnChar)
|
2001-06-11 16:03:00 -04:00
|
|
|
EVT_ERASE_BACKGROUND( MyCanvas::OnEraseBackground)
|
|
|
|
END_EVENT_TABLE()
|
|
|
|
|
2005-02-12 20:14:36 -05:00
|
|
|
MyCanvas::MyCanvas( wxFrame *parent )
|
2004-05-28 06:43:39 -04:00
|
|
|
: wxScrolledWindow( parent, wxID_ANY, wxDefaultPosition, wxDefaultSize,
|
2005-02-12 20:14:36 -05:00
|
|
|
wxScrolledWindowStyle | wxSUNKEN_BORDER )
|
2001-06-11 16:03:00 -04:00
|
|
|
{
|
2005-02-13 10:42:31 -05:00
|
|
|
m_eraseBg =
|
2005-02-12 20:14:36 -05:00
|
|
|
m_useBuffer = false;
|
|
|
|
|
2001-06-11 16:03:00 -04:00
|
|
|
SetScrollbars( 10, 10, 40, 100, 0, 0 );
|
2002-05-05 17:31:08 -04:00
|
|
|
|
|
|
|
m_bitmap = wxBitmap( wxICON(mondrian) );
|
|
|
|
|
2004-05-28 06:43:39 -04:00
|
|
|
new wxStaticBitmap( this, wxID_ANY, m_bitmap, wxPoint(80,20) );
|
2005-03-30 14:10:46 -05:00
|
|
|
|
2005-02-13 09:36:15 -05:00
|
|
|
SetFocusIgnoringChildren();
|
2001-06-11 16:03:00 -04:00
|
|
|
}
|
|
|
|
|
2002-12-04 09:11:26 -05:00
|
|
|
void MyCanvas::OnChar( wxKeyEvent &event )
|
|
|
|
{
|
|
|
|
#if wxUSE_UNICODE
|
|
|
|
if (event.m_uniChar)
|
|
|
|
{
|
|
|
|
m_text += event.m_uniChar;
|
|
|
|
Refresh();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// some test cases
|
|
|
|
switch (event.m_keyCode)
|
|
|
|
{
|
|
|
|
case WXK_UP: m_text += wxT( "<UP>" ); break;
|
|
|
|
case WXK_LEFT: m_text += wxT( "<LEFT>" ); break;
|
|
|
|
case WXK_RIGHT: m_text += wxT( "<RIGHT>" ); break;
|
|
|
|
case WXK_DOWN: m_text += wxT( "<DOWN>" ); break;
|
|
|
|
case WXK_RETURN: m_text += wxT( "<ENTER>" ); break;
|
2004-10-06 16:54:57 -04:00
|
|
|
default: m_text += (wxChar)event.m_keyCode; break;
|
2002-12-04 09:11:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-02-12 20:14:36 -05:00
|
|
|
void MyCanvas::DoPaint(wxDC& dc)
|
2001-06-11 16:03:00 -04:00
|
|
|
{
|
2002-03-29 13:01:50 -05:00
|
|
|
dc.SetBrush( *wxBLACK_BRUSH );
|
2005-02-13 09:36:15 -05:00
|
|
|
dc.DrawRectangle( 10,10,200,50 );
|
2002-05-05 17:31:08 -04:00
|
|
|
|
2004-05-28 06:43:39 -04:00
|
|
|
dc.DrawBitmap( m_bitmap, 10, 20, true );
|
2002-05-05 17:31:08 -04:00
|
|
|
|
|
|
|
dc.SetTextForeground(*wxBLUE);
|
|
|
|
dc.DrawText(_T("This text is drawn from OnPaint"), 65, 65);
|
2004-10-06 16:54:57 -04:00
|
|
|
|
2002-12-04 09:11:26 -05:00
|
|
|
wxString tmp;
|
|
|
|
tmp.Printf( _T("Hit any key to display more text: %s"), m_text.c_str() );
|
|
|
|
int w,h;
|
|
|
|
dc.GetTextExtent( tmp, &w, &h );
|
|
|
|
dc.SetBrush( *wxWHITE_BRUSH );
|
|
|
|
dc.DrawRectangle( 65, 85, w, h );
|
|
|
|
dc.DrawText( tmp, 65, 85 );
|
2002-05-05 17:31:08 -04:00
|
|
|
|
|
|
|
#if 0
|
2002-02-24 06:06:55 -05:00
|
|
|
wxRegionIterator upd( GetUpdateRegion() );
|
|
|
|
while (upd)
|
|
|
|
{
|
2002-12-14 13:13:27 -05:00
|
|
|
wxLogDebug( _T("Paint: %d %d %d %d"), upd.GetX(), upd.GetY(), upd.GetWidth(), upd.GetHeight() );
|
2002-02-24 06:06:55 -05:00
|
|
|
upd ++;
|
|
|
|
}
|
2002-02-24 11:23:43 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
wxSize size = GetSize();
|
|
|
|
wxSize client_size = GetClientSize();
|
2002-12-14 13:13:27 -05:00
|
|
|
wxLogDebug( _T("size %d %d client_size %d %d"), size.x, size.y, client_size.x, client_size.y );
|
2002-02-24 11:23:43 -05:00
|
|
|
#endif
|
2002-03-17 09:16:03 -05:00
|
|
|
|
2002-05-05 17:31:08 -04:00
|
|
|
#if 0
|
2002-03-17 09:16:03 -05:00
|
|
|
int i;
|
2002-02-24 06:06:55 -05:00
|
|
|
dc.SetPen( *wxWHITE_PEN );
|
2002-03-17 09:16:03 -05:00
|
|
|
for (i = 0; i < 20; i += 2)
|
2002-02-24 06:06:55 -05:00
|
|
|
dc.DrawLine( i,i, i+100,i );
|
2002-05-05 17:31:08 -04:00
|
|
|
|
2002-02-24 11:23:43 -05:00
|
|
|
dc.SetPen( *wxWHITE_PEN );
|
2002-03-17 09:16:03 -05:00
|
|
|
for (i = 200; i < 220; i += 2)
|
2002-02-24 11:23:43 -05:00
|
|
|
dc.DrawLine( i-200,i, i-100,i );
|
2002-05-05 17:31:08 -04:00
|
|
|
|
2002-02-16 12:54:35 -05:00
|
|
|
wxRegion region( 110, 110, 80, 80 );
|
|
|
|
wxRegion hole( 130, 130, 40, 1 );
|
|
|
|
region.Intersect( hole );
|
|
|
|
dc.SetClippingRegion( region );
|
2002-05-05 17:31:08 -04:00
|
|
|
|
2001-06-11 16:03:00 -04:00
|
|
|
dc.SetBrush( *wxRED_BRUSH );
|
2002-02-16 12:54:35 -05:00
|
|
|
dc.DrawRectangle( 100, 100, 200, 200 );
|
2002-05-05 17:31:08 -04:00
|
|
|
|
2002-02-24 06:06:55 -05:00
|
|
|
dc.DestroyClippingRegion();
|
2002-02-18 17:13:23 -05:00
|
|
|
|
|
|
|
dc.SetPen( *wxTRANSPARENT_PEN );
|
2002-05-05 17:31:08 -04:00
|
|
|
|
2002-02-18 17:13:23 -05:00
|
|
|
wxRegion strip( 110, 200, 30, 1 );
|
|
|
|
wxRegionIterator it( strip );
|
|
|
|
while (it)
|
|
|
|
{
|
|
|
|
dc.DrawRectangle( it.GetX(), it.GetY(), it.GetWidth(), it.GetHeight() );
|
|
|
|
it ++;
|
|
|
|
}
|
2002-05-05 17:31:08 -04:00
|
|
|
#endif // 0
|
2001-06-11 16:03:00 -04:00
|
|
|
}
|
|
|
|
|
2005-02-12 20:14:36 -05:00
|
|
|
void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
|
|
|
|
{
|
|
|
|
wxPaintDC dcWin(this);
|
|
|
|
PrepareDC( dcWin );
|
|
|
|
|
|
|
|
if ( m_useBuffer )
|
|
|
|
{
|
|
|
|
const wxSize size = GetClientSize();
|
|
|
|
wxMemoryDC dc;
|
|
|
|
wxBitmap bmp(size.x, size.y);
|
|
|
|
dc.SelectObject(bmp);
|
|
|
|
dc.Blit(0, 0, size.x, size.y, &dcWin, 0, 0);
|
|
|
|
dc.DrawText(_T("(copy of background)"), 5, 120 );
|
|
|
|
|
|
|
|
DoPaint(dc);
|
|
|
|
|
|
|
|
dcWin.Blit(0, 0, size.x, size.y, &dc, 0, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DoPaint(dcWin);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-05-05 17:31:08 -04:00
|
|
|
void MyCanvas::OnEraseBackground( wxEraseEvent& event )
|
2001-06-11 16:03:00 -04:00
|
|
|
{
|
2005-02-13 10:42:31 -05:00
|
|
|
if ( !m_eraseBg )
|
|
|
|
{
|
|
|
|
event.Skip();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-05-05 17:31:08 -04:00
|
|
|
wxDC& dc = *event.GetDC();
|
|
|
|
dc.SetPen(*wxGREEN_PEN);
|
|
|
|
|
2005-02-13 09:36:15 -05:00
|
|
|
PrepareDC( dc );
|
2005-03-30 14:10:46 -05:00
|
|
|
|
2005-02-12 20:14:36 -05:00
|
|
|
// clear any junk currently displayed
|
2002-05-05 17:31:08 -04:00
|
|
|
dc.Clear();
|
|
|
|
|
2005-02-12 20:14:36 -05:00
|
|
|
const wxSize size = GetClientSize();
|
2005-02-13 09:36:15 -05:00
|
|
|
for ( int x = 0; x < size.x; x += 15 )
|
2002-05-05 17:31:08 -04:00
|
|
|
{
|
|
|
|
dc.DrawLine(x, 0, x, size.y);
|
|
|
|
}
|
|
|
|
|
2005-02-13 09:36:15 -05:00
|
|
|
for ( int y = 0; y < size.y; y += 15 )
|
2002-05-05 17:31:08 -04:00
|
|
|
{
|
|
|
|
dc.DrawLine(0, y, size.x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
dc.SetTextForeground(*wxRED);
|
2005-02-13 09:36:15 -05:00
|
|
|
dc.DrawText(_T("This text is drawn from OnEraseBackground"), 60, 160);
|
2001-06-11 16:03:00 -04:00
|
|
|
}
|
|
|
|
|