wxMac, PaintEvent
Nusret Taşçı
nusi at sofha.de
Sun Sep 24 10:44:36 PDT 2006
> wxPanels are windows, not just drawable areas, so yes, you will have to
> connect the events in another way, or call back upon the parent from the
> paint events of the chilrdren.
I'm sorry that I answer so late, but I couldn't continue fixing the
remaining problem until today.
I've tracked down the issue to one specific point, let me give an
example so that we both have something to argue about:
#include "wx/wxprec.h"
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
// Define a new application type, each program should derive a class
from wxApp
class MyApp : public wxApp
{
public:
virtual bool OnInit();
};
// Define a new frame type: this is going to be our main frame
class MyFrame : public wxFrame
{
public:
MyFrame(const wxString& title);
private:
void ButtonClicked(wxCommandEvent &event);
void UpperPaint(wxPaintEvent &event);
void LowerPaint(wxPaintEvent &event);
bool m_bDrawUpperRed;
wxButton *pButton;
wxPanel *pUpperPanel;
wxPanel *pLowerPanel;
DECLARE_EVENT_TABLE()
};
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
END_EVENT_TABLE()
IMPLEMENT_APP(MyApp)
bool MyApp::OnInit()
{
if ( !wxApp::OnInit() )
return false;
MyFrame *frame = new MyFrame(_T("Minimal wxWidgets App"));
frame->Show(true);
return true;
}
MyFrame::MyFrame(const wxString& title)
: wxFrame(NULL, wxID_ANY, title)
{
m_bDrawUpperRed = false;
pButton = new wxButton(this, wxID_ANY, wxT("Change Color"));
pUpperPanel = new wxPanel(this, wxID_ANY, wxPoint(100, 0),
wxSize(100, 100));
pLowerPanel = new wxPanel(this, wxID_ANY, wxPoint(100, 100),
wxSize(100, 100));
pButton->Connect(wxEVT_COMMAND_BUTTON_CLICKED,
wxCommandEventHandler(MyFrame::ButtonClicked), NULL, this);
// POI
pUpperPanel->Connect(wxEVT_PAINT,
wxPaintEventHandler(MyFrame::UpperPaint), NULL, this);
pLowerPanel->Connect(wxEVT_PAINT,
wxPaintEventHandler(MyFrame::LowerPaint), NULL, this);
}
void MyFrame::ButtonClicked(wxCommandEvent &)
{
m_bDrawUpperRed = !m_bDrawUpperRed;
#if 1 // POINT OF INTEREST
// case 1
pUpperPanel->Refresh();
pLowerPanel->Refresh();
#else
//case 2
Refresh();
#endif
}
void MyFrame::UpperPaint(wxPaintEvent &event)
{
wxPaintDC dc((wxWindow*)event.GetEventObject());
if (m_bDrawUpperRed)
dc.SetBackground(*wxRED);
else
dc.SetBackground(*wxGREEN);
dc.Clear();
}
void MyFrame::LowerPaint(wxPaintEvent &event)
{
wxPaintDC dc((wxWindow*)event.GetEventObject());
if (m_bDrawUpperRed)
dc.SetBackground(*wxGREEN);
else
dc.SetBackground(*wxRED);
dc.Clear();
}
This sample works.
If you change the connects to somethink like following:
// POI
pUpperPanel->Connect(pUpperPanel->GetId(), wxEVT_PAINT,
wxPaintEventHandler(MyFrame::UpperPaint), NULL, this);
pLowerPanel->Connect(pLowerPanel->GetId(), wxEVT_PAINT,
wxPaintEventHandler(MyFrame::LowerPaint), NULL, this);
than it will not work anymore. The difference is "->GetId()". When
connecting to a painthandler, this seems not to work for wxMac. However,
Connecting with GetId() for other handlers seem to work, though I
haven't tested all of them of course.
Nusi
More information about the wx-users
mailing list