Page 1 of 1

How to catch a left click on a wxSlider thumb

Posted: Sat Oct 27, 2018 9:24 am
by bertolino
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

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

Posted: Sat Oct 27, 2018 10:14 am
by doublemax
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.

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

Posted: Sat Oct 27, 2018 10:42 am
by Kvaz1r
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

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

Posted: Sat Oct 27, 2018 10:52 am
by bertolino
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

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

Posted: Sat Oct 27, 2018 11:00 am
by Kvaz1r
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);

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

Posted: Sat Oct 27, 2018 11:08 am
by bertolino
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

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

Posted: Sat Oct 27, 2018 12:31 pm
by bertolino
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

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

Posted: Sat Oct 27, 2018 1:19 pm
by doublemax
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.

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

Posted: Sun Oct 28, 2018 8:12 am
by bertolino
Thanks for the explanation doublemax.