1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
#pragma once
#include <wx/bmpbuttn.h>
#include <wx/timer.h>
namespace wxutil
{
namespace
{
// The delay between the first "click" and the second "click" event
const int DELAY_INITIAL = 200;
// The delay between all following "click" events
const int DELAY_PERIODIC = 20;
}
/**
* A button containing a single icon that keeps periodically emitting the
* "clicked" event as long as the user keeps the mouse button pressed.
* Used for Surface Inspector controls, for example.
*/
class ControlButton :
public wxBitmapButton
{
private:
wxTimer _timer;
public:
ControlButton(wxWindow* parent, const wxBitmap& bitmap) :
wxBitmapButton(parent, wxID_ANY, bitmap),
_timer(this)
{
// Connect the pressed/released signals
Connect(wxEVT_LEFT_DOWN, wxMouseEventHandler(ControlButton::onPress), NULL, this);
Connect(wxEVT_LEFT_UP, wxMouseEventHandler(ControlButton::onRelease), NULL, this);
Connect(wxEVT_TIMER, wxTimerEventHandler(ControlButton::onIntervalReached), NULL, this);
_timer.Stop();
}
void onIntervalReached(wxTimerEvent& ev)
{
// Safety check to see whether the mouse is still pointing on our control
wxPoint mousePos = wxGetMousePosition();
wxWindow* windowAtPoint = wxFindWindowAtPointer(mousePos);
if (windowAtPoint != this)
{
// Disconnect the timing event
_timer.Stop();
return;
}
// Fire the "clicked" signal
wxCommandEvent event(wxEVT_BUTTON, GetId());
event.SetEventObject(this);
ProcessEvent(event);
// Set the interval to a smaller value
_timer.Stop();
_timer.Start(DELAY_PERIODIC);
}
void onPress(wxMouseEvent& ev)
{
// Trigger a first click
wxCommandEvent event(wxEVT_BUTTON, GetId());
event.SetEventObject(this);
ProcessEvent(event);
// Start the timer using the initial value
_timer.Start(DELAY_INITIAL);
}
void onRelease(wxMouseEvent& ev)
{
// Disconnect the timing event
_timer.Stop();
}
};
} // namespace
|