How to catch wxToolBar button down and button up

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
tt3
Earned a small fee
Earned a small fee
Posts: 10
Joined: Tue Jan 11, 2011 10:19 pm

How to catch wxToolBar button down and button up

Post by tt3 » Fri Dec 02, 2011 5:57 pm

I want to catch the moments when a toolbar button is getting pressed down and released up. The toolbar button is created by calling wxToolBar function AddTool().
The macro EVT_TOOL only gets the moment of releasing up. I have tried the wxMouseEvent macros EVT_LEFT_DOWN and EVT_LEFT_UP in the wxFrame derived class, but no success. Then I tried creating a derived class of wxToolBar and using EVT_LEFT_DOWN and EVT_LEFT_UP within it, no success either.
Does anyone know how to achieve this?
(Ubuntu 10.04, wx2.8.12, libgtk2.0)

DavidHart
Site Admin
Site Admin
Posts: 3906
Joined: Thu Jan 12, 2006 6:23 pm
Location: IoW, UK

Re: How to catch wxToolBar button down and button up

Post by DavidHart » Fri Dec 02, 2011 6:17 pm

Hi,
I want to catch the moments when a toolbar button is getting pressed down and released up.
Out of interest, why?
I have tried the wxMouseEvent macros EVT_LEFT_DOWN and EVT_LEFT_UP in the wxFrame derived class, but no success.
because wxMouseEvent doesn't propagate to parent controls.
Then I tried creating a derived class of wxToolBar and using EVT_LEFT_DOWN and EVT_LEFT_UP within it, no success either.
That was a better idea, but I don't think wxToolBar was designed with subclassing in mind.

If it's possible at all, you'll need to use wxEvtHandler::Connect to catch the event. However you may have problems working out which tool is underneath the mouse as (at least several years ago when I needed to know) this information isn't readily available.

You may be better off using (the undocumented in wx2.8 ) wxAuiToolbar, which has wxAuiToolBar::FindToolByPosition; or faking tools/toolbar using wxBitmapButtons on a wxPanel.

Regards,

David

catalin
Moderator
Moderator
Posts: 1526
Joined: Wed Nov 12, 2008 7:23 am
Location: Romania

Re: How to catch wxToolBar button down and button up

Post by catalin » Fri Dec 02, 2011 8:05 pm

wxToolBar::AddControl() may also help you to add a button instead of a tool, but "why" is a good question indeed.

tt3
Earned a small fee
Earned a small fee
Posts: 10
Joined: Tue Jan 11, 2011 10:19 pm

Re: How to catch wxToolBar button down and button up

Post by tt3 » Fri Dec 02, 2011 10:23 pm

Thank you both for your reply. I was able to finally make it work, by a wxBitmapButton derived class that handles EVT_LEFT_DOWN/UP and using AddControl() to add to the toolbar. Downside is, when the toolbar style changes (between bitmap only, text only, or both), need to update the bitmap of the button accordingly so that this button would look the same as the rest ones. I guess this would be easier with wxAnyButton in 2.9.
As to the "why", this button is for "Jog" command to a motion controller. When the user presses down the jog button, motion starts. When the user releases it, motion stops. I have suggested with two regular buttons as jog-start and jog-stop, but the idea got turned down. So here I came.
Cheers!

DerKleineNik
Knows some wx things
Knows some wx things
Posts: 29
Joined: Fri Sep 09, 2011 9:59 am

Re: How to catch wxToolBar button down and button up

Post by DerKleineNik » Tue Dec 06, 2011 6:25 am

Looking at your "why" it seems to me a wxToggleButton would suit your needs quite well. With that controlyou can easily switch your motion on and off. Or do you want the user to be forced to hold down the button all the time while the motion runs?

tt3
Earned a small fee
Earned a small fee
Posts: 10
Joined: Tue Jan 11, 2011 10:19 pm

Re: How to catch wxToolBar button down and button up

Post by tt3 » Wed Dec 07, 2011 3:03 am

DerKleineNik wrote:Or do you want the user to be forced to hold down the button all the time while the motion runs?
That's exactly what I want (actually, that's what my boss wants). Is there a built-in button that could achieve this?

catalin
Moderator
Moderator
Posts: 1526
Joined: Wed Nov 12, 2008 7:23 am
Location: Romania

Re: How to catch wxToolBar button down and button up

Post by catalin » Wed Dec 07, 2011 6:08 am

wxToolBar::AddCheckTool() ?

_Sam_
In need of some credit
In need of some credit
Posts: 1
Joined: Mon May 28, 2012 2:43 pm

Re: How to catch wxToolBar button down and button up

Post by _Sam_ » Mon May 28, 2012 3:08 pm

Code: Select all

m_auiToolBar->Connect( wxEVT_COMMAND_AUITOOLBAR_TOOL_DROPDOWN, wxAuiToolBarEventHandler( AuiFrame::OnToolBarDropDown ), NULL, this );

Code: Select all

void AuiFrame::OnToolBarDropDown(wxAuiToolBarEvent& event){
    switch(event.GetId()){
        case ID1:
        case ID2:
        .....
    }
}

zakays
In need of some credit
In need of some credit
Posts: 1
Joined: Tue Jun 05, 2012 6:38 am
Contact:

Re: How to catch wxToolBar button down and button up

Post by zakays » Tue Jun 05, 2012 6:39 am

I was wondering if there can be a mouse button down event that isn't connected to any actor? Meaning can I just click anywhere on the screen to have an event happen? Or do I have to go through each actor and add the code?

DavidHart
Site Admin
Site Admin
Posts: 3906
Joined: Thu Jan 12, 2006 6:23 pm
Location: IoW, UK

Re: How to catch wxToolBar button down and button up

Post by DavidHart » Tue Jun 05, 2012 7:17 am

Hi,

See this wiki page.

Regards,

David

Radek
Super wx Problem Solver
Super wx Problem Solver
Posts: 286
Joined: Sun Sep 11, 2011 7:17 am

Re: How to catch wxToolBar button down and button up

Post by Radek » Wed Jun 06, 2012 6:53 am

I suggest using AddControl() instead of AddTool(). I think that AddTool() calls AddControl() in the end, too, it is a "simplified way" of adding toolbar controls. AddControl() adds a wxControl, which can be Connect-ed to events before placing on a toolbar, for example, to mouse events, so that you can monitor when the control is pressed or released.

With AddTool() you are losing access to the control itself. Perhaps, FindWindowById() after adding the tool should provide a pointer to the tool so that the tool could be Connect-ed as well.

Post Reply