im trying to build a custom button so I can get rounded buttons, so I derived from wxwindow and build a button, but Im not sure why this happened but the tab traversal is not working anymore, I tried to get focus events but it seems the focus is for the Panel/Frame not the childs on it (is also tried ChildFocusEvent with similar results).
I captured KeyEvents and sent the "wxEVT_COMMAND_BUTTON_CLICKED" , but it will only send the event for the first button in the wxpanel, hitting tab or arrow keys wont change that, im not sure where should I look for examples or anything related to that, can anyone help me?
this is the current code for the custombutton
Code: Select all
#ifndef CUSTOMCONTROLS_H
#define CUSTOMCONTROLS_H
#include "wx/wx.h"
#include "wx/graphics.h"
#include "../include/utils.h"
class wxCustomButton : public wxWindow
{
public:
wxCustomButton(wxPanel *parent, wxWindowID id, wxString text, const wxSize &size,
wxColour textcolour, wxColour bucolour, wxColour bacolour,
wxColour pressedcolour, bool rounded) :
wxWindow(parent, id, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL)
{
SetMinSize(size);
this->text = text;
this->rounded = rounded;
this->bucolour = bucolour;
this->bacolour = bacolour;
this->textcolour = textcolour;
this->pressedcolour = pressedcolour;
pressedDown = false;
rect.SetSize(size);
Bind(wxEVT_LEFT_DOWN, &wxCustomButton::OnMouseDown, this);
Bind(wxEVT_LEFT_UP, &wxCustomButton::OnMouseUp, this);
Bind(wxEVT_ENTER_WINDOW, &wxCustomButton::OnMouseOver, this);
Bind(wxEVT_LEAVE_WINDOW, &wxCustomButton::OnMouseOut, this);
Bind(wxEVT_PAINT, &wxCustomButton::OnPaint, this);
Bind(wxEVT_SET_FOCUS, &wxCustomButton::OnFocus, this);
Bind(wxEVT_CHILD_FOCUS, &wxCustomButton::OnChildFocus, this);
Bind(wxEVT_KEY_DOWN, &wxCustomButton::keyPressed, this);
Bind(wxEVT_KEY_UP, &wxCustomButton::keyReleased, this);
}
inline void SetText(const wxString &newtext);
protected:
bool pressedDown;
bool rounded;
wxRect rect;
wxString text;
wxColour bucolour;
wxColour bacolour;
wxColour textcolour;
wxColour pressedcolour;
inline void OnPaint(wxPaintEvent &evt);
inline void render();
inline void paintNow();
inline void OnFocus(wxFocusEvent& evt);
inline void OnChildFocus(wxChildFocusEvent& evt);
inline void OnMouseDown(wxMouseEvent& event);
inline void OnMouseUp(wxMouseEvent &event);
inline void OnMouseOver(wxMouseEvent &event);
inline void OnMouseOut(wxMouseEvent &event);
inline void keyPressed(wxKeyEvent &event);
inline void keyReleased(wxKeyEvent &event);
};
void wxCustomButton::keyPressed(wxKeyEvent &event)
{
TRACE_FUNC_CALL();
if (event.GetKeyCode() == WXK_RETURN || event.GetKeyCode() == WXK_SPACE)
{
TRACE_LF("sending event");
wxCommandEvent evt(wxEVT_COMMAND_BUTTON_CLICKED, GetId());
evt.SetEventObject(this);
HandleWindowEvent(evt);
event.Skip();
}
TRACE_LF("key code = %d", (int)event.GetKeyCode());
}
void wxCustomButton::keyReleased(wxKeyEvent &event)
{
TRACE_FUNC_CALL();
}
void wxCustomButton::OnFocus(wxFocusEvent &evt)
{
TRACE_FUNC_CALL();
}
void wxCustomButton::OnChildFocus(wxChildFocusEvent &evt)
{
TRACE_FUNC_CALL();
}
void wxCustomButton::SetText(const wxString &newtext)
{
TRACE_FUNC_CALL();
this->text = newtext;
paintNow();
this->RefreshRect(rect);
}
void wxCustomButton::OnPaint(wxPaintEvent &evt)
{
paintNow();
}
void wxCustomButton::paintNow()
{
wxPaintDC dc(this);
dc.SetBackground(bacolour);
dc.Clear();
wxGraphicsContext *gc = wxGraphicsContext::Create(dc);
if (pressedDown)
{
gc->SetPen(*wxTRANSPARENT_PEN);
gc->SetBrush(pressedcolour);
}
else
{
gc->SetPen(*wxTRANSPARENT_PEN);
gc->SetBrush(bucolour);
}
wxGraphicsPath path = gc->CreatePath();
if (rounded)
{
gc->DrawRoundedRectangle(rect.x, rect.y, rect.width, rect.height, 15);
path.AddRoundedRectangle(rect.x, rect.y, rect.width, rect.height, 15);
}
else
{
gc->DrawRectangle(rect.x, rect.y, rect.width, rect.height);
path.AddRectangle(rect.x, rect.y, rect.width, rect.height);
}
gc->FillPath(path);
gc->StrokePath(path);
delete gc;
dc.SetTextForeground(textcolour);
dc.DrawLabel(text, rect, wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL);
}
void wxCustomButton::OnMouseDown(wxMouseEvent& event)
{
pressedDown = true;
paintNow();
this->RefreshRect(rect);
}
void wxCustomButton::OnMouseUp(wxMouseEvent& event)
{
TRACE_FUNC_CALL();
pressedDown = false;
paintNow();
this->RefreshRect(rect);
wxCommandEvent evt(wxEVT_COMMAND_BUTTON_CLICKED, GetId());
evt.SetEventObject(this);
HandleWindowEvent(evt);
}
void wxCustomButton::OnMouseOut(wxMouseEvent& event)
{
if (pressedDown)
{
pressedDown = false;
paintNow();
}
}
void wxCustomButton::OnMouseOver(wxMouseEvent& event)
{
}
#endif