I'll copy all the code at the end of the post.
I'm at the beginning of it and i already have problems:
The upper is my component, the lower is a panel: they're built in the same manned but the label in my component is'nt centered.
What am i doing wrong?
2nd problem: if i "connect" function to the left mouse down event (not in the code), my component cease to react to it. I guess the new function "steals" it. How should i ensure that the wxActiveArea ALWAYS get the mousedown before, and then eventually it is passed to user functions?
Tips:
1) How can my wxActiveArea get the mouse click even if it contains other components? if in the example i click on the label the ActiveArea does'nt react to the mousedown, while i want it ALWAYS get the event no matter what's the child component under the mouse.
2) is the event table still a thing for components? should i use "connect" or "bind" instead?
3) Is it ok to use a wxTimer to make animations inside my component?
Thanks.
wxActiveArea.h
Code: Select all
#ifndef WXACTIVEAREA_H
#define WXACTIVEAREA_H
#include <wx/panel.h>
#include <wx/dcbuffer.h>
// forward declarations
class wxActiveArea;
#define ID_WXACTIVEAREA 10101
class wxActiveArea: public wxPanel //wxWindow //wxControl
{
typedef wxPanel inherited;
DECLARE_DYNAMIC_CLASS( wxActiveArea )
DECLARE_EVENT_TABLE()
public:
wxActiveArea(){};
wxActiveArea(
wxWindow *parent,
wxWindowID winid = ID_WXACTIVEAREA,
const wxPoint& pos = wxDefaultPosition,
const wxSize& size = wxDefaultSize,
long style = wxTAB_TRAVERSAL | wxSIMPLE_BORDER,
const wxString& name = wxPanelNameStr
);
virtual ~wxActiveArea();
wxActiveArea(const wxActiveArea& other);
protected:
// Pseudo ctor
bool Create(wxWindow *parent,
wxWindowID winid = ID_WXACTIVEAREA,
const wxPoint& pos = wxDefaultPosition,
const wxSize& size = wxDefaultSize,
long style = wxTAB_TRAVERSAL | wxSIMPLE_BORDER,
const wxString& name = wxPanelNameStr);
void doDraw(wxBufferedPaintDC& dc);
virtual bool AcceptsFocus() const {return false;};
private:
virtual void OnSize( wxSizeEvent& event );
virtual void OnPaint( wxPaintEvent& event );
virtual void OnLeftDown( wxMouseEvent& event );
virtual void OnLeftUp( wxMouseEvent& event );
};
#endif // WXACTIVEAREA_H
Code: Select all
#include "wxActiveArea.h"
#include <iostream>
#include <wx/graphics.h>
IMPLEMENT_DYNAMIC_CLASS( wxActiveArea, inherited )
BEGIN_EVENT_TABLE( wxActiveArea, inherited )
EVT_SIZE( wxActiveArea::OnSize )
EVT_PAINT( wxActiveArea::OnPaint )
EVT_LEFT_DOWN( wxActiveArea::OnLeftDown )
EVT_LEFT_UP( wxActiveArea::OnLeftUp )
END_EVENT_TABLE()
wxActiveArea::~wxActiveArea()
{
}
//---------------------------------------------------------------------------------------------------
wxActiveArea::wxActiveArea(
wxWindow *parent ,
wxWindowID winid ,
const wxPoint& pos ,
const wxSize& size ,
long style ,
const wxString& name
)
{
m_parent = parent;
if (m_parent) {
Create(
m_parent ,
winid ,
pos ,
size ,
style ,
name
);
}
}
bool wxActiveArea::Create(
wxWindow *parent ,
wxWindowID winid ,
const wxPoint& pos ,
const wxSize& size ,
long style ,
const wxString& name
)
{
inherited::Create(parent, winid, pos, size, style, name);
if (m_parent!=nullptr) {
this->SetBackgroundColour(m_parent->GetBackgroundColour());
this->SetForegroundColour(m_parent->GetForegroundColour());
this->SetFont(m_parent->GetFont());
}else{
this->SetBackgroundColour(wxColour(0, 0, 64));
this->SetForegroundColour(wxColour(0, 200, 255));
this->SetFont(wxFont(8, wxFONTFAMILY_SWISS, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD, false, wxT("Tahoma")));
}
return true;
}
//---------------------------------------------------------------------------------------------------
void wxActiveArea::OnPaint( wxPaintEvent& event )
{
wxBufferedPaintDC dc(this);
doDraw(dc);
}
//---------------------------------------------------------------------------------------------------
void wxActiveArea::doDraw(wxBufferedPaintDC& dc)
{
bool Cliccato = (GetCapture() == this) && this->IsEnabled();
wxPen bPen(GetForegroundColour());
const double rr = 10;
const double bb = 10;
wxGraphicsContext *gc;
wxRect clientRect = GetClientRect();
wxRect in = clientRect;
in.Deflate(bb);
dc.SetPen (wxPen (GetBackgroundColour()) );
dc.SetBrush (wxBrush (GetBackgroundColour()) );
dc.DrawRectangle (clientRect);
if (Cliccato) {
gc = wxGraphicsContext::Create( dc );
if(gc) {
bPen.SetColour( wxColor(255,255,0,128) );
bPen.SetWidth(2);
gc->SetPen(bPen);
gc->SetBrush(*wxTRANSPARENT_BRUSH);
gc->DrawRoundedRectangle(in.GetLeft(), in.GetTop(), in.GetWidth(),in.GetHeight(),rr);
delete gc;
} // if gc
}else{
bPen.SetWidth(2);
bPen.SetColour(*wxWHITE);
dc.SetPen(bPen);
dc.SetBrush(*wxTRANSPARENT_BRUSH);
dc.DrawRoundedRectangle(in,rr);
}
}
//---------------------------------------------------------------------------------------------------
void wxActiveArea::OnLeftDown( wxMouseEvent& event )
{
if(GetCapture() != this)
{
CaptureMouse();
Refresh();
}
}
void wxActiveArea::OnLeftUp( wxMouseEvent& event )
{
if(GetCapture() == this)
{
ReleaseMouse();
if(GetClientRect().Contains(event.GetPosition()))
{
wxCommandEvent evt(wxEVT_COMMAND_BUTTON_CLICKED, GetId());
evt.SetEventObject(this);
GetEventHandler()->AddPendingEvent(evt);
}
Refresh();
}
}
void wxActiveArea::OnSize( wxSizeEvent& event )
{
Refresh();
}
Code: Select all
#include "wx/wx.h"
class TEST_wxAA_App : public wxApp
{
public:
virtual bool OnInit() wxOVERRIDE;
};
// Define a new frame type: this is going to be our main frame
class MyFrame : public wxFrame
{
public:
MyFrame(const wxString& title);
//void OnQuit(wxCommandEvent& event);
private:
//wxDECLARE_EVENT_TABLE();
};
//==============================================================
wxIMPLEMENT_APP(TEST_wxAA_App);
//==============================================================
bool TEST_wxAA_App::OnInit()
{
if ( !wxApp::OnInit() )
return false;
MyFrame *frame = new MyFrame("wxActiveArea TEST");
frame->Show(true);
//SetTopWindow(frame);
return true;
}
//==============================================================
#include "wxActiveArea.h"
//==============================================================
/*
wxActiveArea* m_ActiveArea;
wxStaticText* m_st_label;
*/
//==============================================================
MyFrame::MyFrame(const wxString& title)
: wxFrame(NULL, wxID_ANY, title)
{
this->SetBackgroundColour(wxColour(wxT("rgb(255,128,0)")));
wxBoxSizer* szr_main = new wxBoxSizer(wxVERTICAL);
this->SetSizer(szr_main);
wxActiveArea* m_ActiveArea = new wxActiveArea(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL);
m_ActiveArea->SetBackgroundColour(wxColour(wxT("rgb(0,0,128)")));
m_ActiveArea->SetForegroundColour(wxColour(wxT("rgb(255,255,255)")));
szr_main->Add(m_ActiveArea, 1, wxALL|wxEXPAND, 15);
wxBoxSizer* szr_AA_V = new wxBoxSizer(wxVERTICAL);
m_ActiveArea->SetSizer(szr_AA_V);
wxBoxSizer* szr_AA_H = new wxBoxSizer(wxHORIZONTAL);
szr_AA_V->Add(szr_AA_H, 1, wxALIGN_CENTER_HORIZONTAL, 5);
wxStaticText* m_st_label = new wxStaticText(m_ActiveArea, wxID_ANY, _("This should be a centered label"), wxDefaultPosition, wxDefaultSize, 0);
m_st_label->SetBackgroundColour(wxColour(wxT("rgb(0,0,255)")));
szr_AA_H->Add(m_st_label, 0, wxALIGN_CENTER_VERTICAL, 5);
wxPanel* m_ComparePanel = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL);
m_ComparePanel->SetBackgroundColour(wxColour(wxT("rgb(0,0,128)")));
m_ComparePanel->SetForegroundColour(wxColour(wxT("rgb(255,255,255)")));
szr_main->Add(m_ComparePanel, 1, wxALL|wxEXPAND, 15);
wxBoxSizer* szr_CP_V = new wxBoxSizer(wxVERTICAL);
m_ComparePanel->SetSizer(szr_CP_V);
wxBoxSizer* szr_CP_H = new wxBoxSizer(wxHORIZONTAL);
szr_CP_V->Add(szr_CP_H, 1, wxALIGN_CENTER_HORIZONTAL, 5);
wxStaticText* m_st_label_cp = new wxStaticText(m_ComparePanel, wxID_ANY, _("This is a centered label"), wxDefaultPosition, wxDefaultSize, 0);
m_st_label_cp->SetBackgroundColour(wxColour(wxT("rgb(0,0,255)")));
szr_CP_H->Add(m_st_label_cp, 0, wxALIGN_CENTER_VERTICAL, 5);
SetBackgroundColour(wxColour(wxT("rgb(255,128,0)")));
CentreOnScreen(wxBOTH);
}