views:

1994

answers:

2

How to add a click event listener to my custom control made with wxWidgets? The custom control uses wxWindow as the base. On the event list I see

wxEVT_LEFT_DOWN 
wxEVT_LEFT_UP 
wxEVT_LEFT_DCLICK
wxEVT_MIDDLE_DOWN
wxEVT_MIDDLE_UP 
wxEVT_MIDDLE_DCLICK
wxEVT_RIGHT_DOWN 
wxEVT_RIGHT_UP
wxEVT_RIGHT_DCLICK
wxEVT_MOTION
wxEVT_ENTER_WINDOW
wxEVT_LEAVE_WINDOW
wxEVT_MOUSEWHEEL

But there is no wxEVT_LEFT_CLICK or similar.

+2  A: 

Typically, there is no "click" event (and in the case of wxWidgets - there isn't ). The action of clicking is broken into its two parts: Mouse Down and Mouse Up. Typically what you think of as a "left click" event is actually handled in a "left up" event.

Try it out:

  • Hover over a button (such as the "Add Comment" button this page)
  • Click the left-mouse button down and hold
  • Move the mouse off of the button while holding down
  • Release the left-mouse button
  • Nothing happens!

This time:

  • Hover over the same button
  • Click the left-mouse button down and hold
  • Release the left-mouse button
  • The "click" action you expect is triggered by the up event!
Burly
Yes, that is the problem of using left-down instead of click. Usually user can cancel the click after holding down the mouse button by moving the pointer outside the object. Here I need to detect the click accurately by detecting left-down and left-up? What a hassle :(
yuku
I think if you are writing a control you need to be ready to manage all the details of the user's mouse interaction. From that perspective the wxWidget family of mouse events make sense.
Bill Forster
+2  A: 

In the first instance I recommend inheriting from wxControl not wxWindow, wxControl is designed for that exact purpose and you are less likely to find yourself fighting the system. When I look at a control I am building in my own wxWidgets app, I see that my click handler is attached to wxEVT_LEFT_DOWN. Looking in my copy of Cross Platform GUI Programming with wxWidgets I can see a list of all wxMouseEvents, and there is no wxEVT_LEFT_CLICK. I would suggest wxEVT_LEFT_DOWN is the event to use.

Now after posting I've read Burly's answer and I agree with him, wxWidgets offers the lowest level events and that gives you the maximum amount of control of the user interface you construct for your users.

Bill Forster
Thanks for your suggestion of using wxControl. I am new to wxWidgets so I don't know which control to inherit. The left-down event is different than click, user cannot cancel the click by letting the pointer be outside the control before releasing the mouse button.
yuku