subclass panel : custom draw and controls

Are you writing your own components and need help with how to set them up or have questions about the components you are deriving from ? Ask them here.
Post Reply
murena
In need of some credit
In need of some credit
Posts: 4
Joined: Fri Sep 15, 2017 12:52 pm

subclass panel : custom draw and controls

Post by murena »

Hello to everyone,
I have a problem :
I want to subclass wxPanel as my template for custom panel. In this derived wxPanel I can add normal controls as wxButton etc but I don't know how to add my custom draws (e.g. a border). In summary, I would mix normal wxControl and custom drawing in a wxPanel.

Can you help me?

Thanks

Stefano
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: subclass panel : custom draw and controls

Post by doublemax »

Basically, you need to catch the wxEVT_PAINT event and do your drawing in the event handler.

https://wiki.wxwidgets.org/Drawing_on_a_panel_with_a_DC
Use the source, Luke!
murena
In need of some credit
In need of some credit
Posts: 4
Joined: Fri Sep 15, 2017 12:52 pm

Re: subclass panel : custom draw and controls

Post by murena »

thank you for your reply.
i will try to explain better.
For a single panel, how to draw in the render context it is ok.
The problem is fired when I try to detect EV_PAINT in a super class. I Have a set of panel, each of them extends a super class custompanel.
I would write a piece of generic code for all subclasses in only one place (the superclass) e.g. to draw a common border.

More in details I have ( only a portion of code ):
//SUPERCLASS
class CustomPanel : public wxPanel, public Observer {

public:
void OnPaint(wxPaintEvent& event);

DECLARE_EVENT_TABLE()
};
// Event table for CustomPanel
BEGIN_EVENT_TABLE(CustomPanel, wxPanel)
EVT_PAINT(CustomPanel::OnPaint)
END_EVENT_TABLE()

//SPECIALIZED AND CONCRETE CLASS
class SystemDateTimePanel : public CustomPanel
{
}

//END OF CODE

In this real example, hence the my problem, the evt_paint of superclass is never fired.

thank you

Stefano
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7458
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: subclass panel : custom draw and controls

Post by ONEEYEMAN »

Hi,
Are you catching EVT_PAINT in the derived class?

Thank you.
murena
In need of some credit
In need of some credit
Posts: 4
Joined: Fri Sep 15, 2017 12:52 pm

Re: subclass panel : custom draw and controls

Post by murena »

Hi,
no I am not.
So I have to catch the event in the derived class, and then? Could I catch the event in the derived class and then call prior the superclass method (common to all derived class) ? I will try this.
thank you.
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: subclass panel : custom draw and controls

Post by doublemax »

I would suggest to catch the paint event only in the base class.

Then add a virtual method that does the actual painting. That can be overriden by the subclasses.

Code: Select all

void CustomPanel::OnPaint( wxPaintEvent &evt )
{
   wxPaintDC dc(this);
   DoDraw( dc );
}

virtual void CustomPanel::DoDraw( wxDC &dc )
{
  // do actual drawing here.
}
Use the source, Luke!
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7458
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: subclass panel : custom draw and controls

Post by ONEEYEMAN »

Hi,
Also, make sure you call Refresh()/Update() to send the paint event.

Thank you.
Post Reply