wxSlider(): Right click to reset

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Ishtar
Experienced Solver
Experienced Solver
Posts: 78
Joined: Mon May 20, 2013 6:33 pm

wxSlider(): Right click to reset

Post by Ishtar »

Dear All,

My project has a significant number of sliders which I would like to have a "right-click" function which will reset the slider back to its default values. I have looked through the wxSlider() class documentation but didn't see a "right-click" event. I have therefore subclassed the wxSlider() class and added a EVT_RIGHT_DOWN event. My code example is as follows and does actually work as designed. I would be grateful if somebody would look at this code and let me know if this is the correct way to subclass a wxWidgets control.

Many thanks
Amanda

Code: Select all

#ifndef UI__ASTROSLIDER_H
#define UI__ASTROSLIDER_H

// WX Headers
#include <wx/slider.h>
#include <wx/frame.h>
#include <wx/panel.h>

// Class Definition
class sub_wxSlider : public wxSlider
{
public:
    sub_wxSlider(wxPanel *parent, int id, int value, int min, int max, wxFrame *f);
    ~sub_wxSlider();
    
    void set_Control_Default(int default_value);

private:
    int m_ID;
    int m_Default_Value;
    
    wxFrame *m_Event_Path;

    DECLARE_EVENT_TABLE()
            
    void on_click(wxMouseEvent& event);

};

#endif /* UI__ASTROSLIDER_H */

Code: Select all

#include "sub_wxSlider.h"

// Event table
BEGIN_EVENT_TABLE(sub_wxSlider, wxSlider)

EVT_RIGHT_DOWN(sub_wxSlider::on_click)

END_EVENT_TABLE()

// Constructor
sub_wxSlider::sub_wxSlider(wxPanel *parent, int id, int value, int min, int max, wxFrame *f)
            : wxSlider(parent, id, value, min, max, wxDefaultPosition, wxDefaultSize, wxSL_HORIZONTAL) 
{
    m_Event_Path = f;
    m_ID = id;
}

// Destructor
sub_wxSlider::~sub_wxSlider() { }


// Set default control values
void sub_wxSlider::set_Control_Default(int default_value)
{
    m_Default_Value = default_value;
}

// Event Management
void sub_wxSlider::on_click(wxMouseEvent& WXUNUSED(event))
{
    this->SetValue(m_Default_Value);
    
    // Send event to the main class
     wxCommandEvent e(wxEVT_COMMAND_TEXT_UPDATED, AstroDMx::ID_EVENT_RESET_CONTROL);
     e.SetInt(m_ID);
     m_Event_Path->GetEventHandler()->AddPendingEvent(e);
}

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

Re: wxSlider(): Right click to reset

Post by doublemax »

In general that looks ok, but there are two things i don't like about the solution:

a) passing the wxFrame pointer in order to use it as m_Event_Path.
This is unneccesary, just send the event to the slider itself or its parent. As it's a wxCommandEvent it will travel upwards the window hierarchy automatically and will reach its destination.

b) Sending an event to a particular control (ID_EVENT_RESET_CONTROL). The subclassed control should not know anything about its parents or even rely on them. Also, resetting a slider should not be different than just dragging the slider to a new position. So i would just send a standard wxEVT_SLIDER event to the parent. As you handle that somewhere in your code anyway, everything should work as expected without special treatment. This would also make the control re-usable.

Also, when creating the event, you should set the ID and the event object.

So, i think it should look more like this (untested):

Code: Select all

wxCommandEvent e( wxEVT_SLIDER, GetId() );
e.SetInt( m_Default_Value );
e.SetEventObject( this );
GetEventHandler()->AddPendingEvent(e);
Use the source, Luke!
Ishtar
Experienced Solver
Experienced Solver
Posts: 78
Joined: Mon May 20, 2013 6:33 pm

Re: wxSlider(): Right click to reset

Post by Ishtar »

Dear Doublemax,

Thanks for your advice. I have removed the wxFrame pointer which works fine.

Currently, my wxSliders are using EVT_COMMAND_SCROLL (wxScrollEvent) so using:

Code: Select all

wxCommandEvent e( wxEVT_SLIDER, GetId() ); 


...the event does not to get through. Instead of using wxEVT_SLIDER I tried:

Code: Select all

wxScrollEvent e(wxEVT_SCROLL_CHANGED, GetId()); 
Despite the documentation saying that wxEVT_SCROLL_CHANGED only works on MSW (I'm using Linux) the event seems to get received by the slider event handler. Do you think this will be OK? Or should I really be using EVT_SLIDER (wxCommandEvent)?


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

Re: wxSlider(): Right click to reset

Post by doublemax »

Despite the documentation saying that wxEVT_SCROLL_CHANGED only works on MSW (I'm using Linux) the event seems to get received by the slider event handler. Do you think this will be OK? Or should I really be using EVT_SLIDER (wxCommandEvent)?
Doing a full-text-search on the wx sources reveals that his event is indeed generated under MSW and GTK, but not under OS X. So if you don't need an OS X version in the future, you should be fine.
Use the source, Luke!
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxSlider(): Right click to reset

Post by ONEEYEMAN »

doublemax,
Would you be able to submit a ticket/PR with the documentation fix?
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxSlider(): Right click to reset

Post by doublemax »

Would you be able to submit a ticket/PR with the documentation fix?
I certainly could, but honestly i don't care enough to do it. If i had written down every little mistake i found in the documentation, it'll probably be 50-100.
Use the source, Luke!
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxSlider(): Right click to reset

Post by ONEEYEMAN »

Let's put it together and submit as one single huge request.
Ishtar
Experienced Solver
Experienced Solver
Posts: 78
Joined: Mon May 20, 2013 6:33 pm

Re: wxSlider(): Right click to reset

Post by Ishtar »

Thank you very much Doublemax. Your help, as aways, is appreciated.

Regards,
Amanda
Post Reply