How to catch a left click on a wxSlider thumb

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
bertolino
Knows some wx things
Knows some wx things
Posts: 45
Joined: Wed Aug 14, 2013 8:07 am
Location: France
Contact:

How to catch a left click on a wxSlider thumb

Post by bertolino » Sat Oct 27, 2018 9:24 am

Hi all,

I'd like to detect a left mouse click on a wxSlider thumb (hence the slider value is not changed).
But there is no counterpart to wxEVT_SCROLL_THUMBRELEASE and mouse events are not available
for a wxSlider.
Any help will be welcome.

Regards,
Pascal

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

Re: How to catch a left click on a wxSlider thumb

Post by doublemax » Sat Oct 27, 2018 10:14 am

mouse events are not available for a wxSlider.
Are you sure, have you tried? If these don't work, there probably is no way to do it.
Use the source, Luke!

Kvaz1r
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 207
Joined: Tue Jun 07, 2016 1:07 pm

Re: How to catch a left click on a wxSlider thumb

Post by Kvaz1r » Sat Oct 27, 2018 10:42 am

bertolino wrote: But there is no counterpart to wxEVT_SCROLL_THUMBRELEASE and mouse events are not available for a wxSlider.
They are. Because wxSlider inherit they from wxWindow.
About click events see this SO question - wxWidgets: Detecting click event on custom controls

bertolino
Knows some wx things
Knows some wx things
Posts: 45
Joined: Wed Aug 14, 2013 8:07 am
Location: France
Contact:

Re: How to catch a left click on a wxSlider thumb

Post by bertolino » Sat Oct 27, 2018 10:52 am

I have double-ckecked and still have the same issue. To be more precise:

I have several sliders, so I use the range macro (this could be the problem):
EVT_COMMAND_RANGE(ID_SLIDER, ID_SLIDER + MAX_PARAM_COUNT, wxEVT_LEFT_DOWN, Effects::OnSlider)

I've tried the 2 following OnSlider implementations:

(1) void Effects::OnSlider(wxCommandEvent& event)
The method is not called.

(2) void Effects::OnSliderClick(wxMouseEvent& event)
It doesn't compile, invalid type conversion.

Maybe, I should try not to use the macro to be able to cast and make the conversion work.
I'm also going to make the tests not with a range, just with one wxSlider.

Pascal

Kvaz1r
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 207
Joined: Tue Jun 07, 2016 1:07 pm

Re: How to catch a left click on a wxSlider thumb

Post by Kvaz1r » Sat Oct 27, 2018 11:00 am

bertolino wrote: I've tried the 2 following OnSlider implementations:
Code below works for me, try change it for your event:

Code: Select all

#include <wx/wx.h>

class MyFrame : public wxFrame
	{
	public:
		MyFrame() : wxFrame(NULL, wxID_ANY, "Test", wxDefaultPosition, wxSize(600, 400))
			{                                       
			wxBoxSizer* bSizer1 = new wxBoxSizer( wxVERTICAL );
			m_logCtrl = new wxTextCtrl(this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE | wxTE_READONLY | wxTE_RICH2);                
			wxLog::SetActiveTarget(new wxLogTextCtrl(m_logCtrl));

			m_slider = new wxSlider( this, wxID_ANY, 50, 0, 100, wxDefaultPosition, wxDefaultSize, wxSL_HORIZONTAL );
			bSizer1->Add(m_slider, 0, wxALL, 5 );
			bSizer1->Add(m_logCtrl, 1, wxALL, 5 );

			this->SetSizer( bSizer1 );
			this->Layout();

			this->Centre( wxBOTH );
			m_slider->Bind(wxEVT_LEFT_UP,[](wxMouseEvent& event)
				{
				wxLogMessage("wxEVT_LEFT_UP");
				event.Skip();
				});
			m_slider->Bind(wxEVT_SLIDER,[](wxCommandEvent& event)
				{
				wxLogMessage("wxEVT_SLIDER");
				event.Skip();
				});
			}
	protected:
		wxSlider* m_slider;
		wxTextCtrl* m_logCtrl;
	};

class MyApp : public wxApp
	{
	public:   
		bool OnInit()
			{
			(new MyFrame)->Show();
			return true;
			}
	}; wxIMPLEMENT_APP(MyApp);

bertolino
Knows some wx things
Knows some wx things
Posts: 45
Joined: Wed Aug 14, 2013 8:07 am
Location: France
Contact:

Re: How to catch a left click on a wxSlider thumb

Post by bertolino » Sat Oct 27, 2018 11:08 am

Hello Kvaz1r,

Many thanks for your valuable help and piece of code.
I'm going to replace the macro with a call to Bind.
Then I 'll just have to adapt the code to manage my slider range.
Many thanks again for your time,

Pascal

bertolino
Knows some wx things
Knows some wx things
Posts: 45
Joined: Wed Aug 14, 2013 8:07 am
Location: France
Contact:

Re: How to catch a left click on a wxSlider thumb

Post by bertolino » Sat Oct 27, 2018 12:31 pm

Just to confirm that as Kvaz1r stated, the solution with Bind works well.
Of course, two methods are necessary to catch both
wxCommandEvent and wxMouseEvent.
Moral of the story: don't use event tables ;-)
Many thanks for the helps,

Pascal

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

Re: How to catch a left click on a wxSlider thumb

Post by doublemax » Sat Oct 27, 2018 1:19 pm

Moral of the story: don't use event tables
Just for completeness sake: If you tried to catch mouse events for the slider using the event table for the frame, that only didn't work because unlike wxCommandEvents, wxMouseEvents don't propagate upwards in the window hierarchy. You can only catch them where they originate.
Use the source, Luke!

bertolino
Knows some wx things
Knows some wx things
Posts: 45
Joined: Wed Aug 14, 2013 8:07 am
Location: France
Contact:

Re: How to catch a left click on a wxSlider thumb

Post by bertolino » Sun Oct 28, 2018 8:12 am

Thanks for the explanation doublemax.

Post Reply