From b371ad32f7f51a393885534a6acc1e991fef412c Mon Sep 17 00:00:00 2001 From: Markus Gans Date: Mon, 6 Jul 2015 23:15:34 +0200 Subject: [PATCH] Class FSwitch: Hold down the mouse button displays the switch in a different color. --- src/fswitch.cpp | 39 +++++++++++++++++++++++++++++++++++---- src/fswitch.h | 6 +++++- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/src/fswitch.cpp b/src/fswitch.cpp index d3c8c690..fb7802b7 100644 --- a/src/fswitch.cpp +++ b/src/fswitch.cpp @@ -12,7 +12,7 @@ //---------------------------------------------------------------------- FSwitch::FSwitch(FWidget* parent) : FToggleButton(parent) { - button_width = 11; + init(); } //---------------------------------------------------------------------- @@ -20,7 +20,7 @@ FSwitch::FSwitch ( const FString& txt, FWidget* parent ) : FToggleButton(txt, parent) { switch_offset_pos = int(txt.getLength()) + 1; - button_width = 11; + init(); } //---------------------------------------------------------------------- @@ -30,6 +30,13 @@ FSwitch::~FSwitch() // destructor // private methods of FSwitch +//---------------------------------------------------------------------- +void FSwitch::init() +{ + button_width = 11; + button_pressed = false; +} + //---------------------------------------------------------------------- void FSwitch::draw() { @@ -53,7 +60,7 @@ void FSwitch::drawCheckButton() if ( checked ) { - if ( hasFocus() ) + if ( hasFocus() && ! button_pressed ) setColor (wc.button_hotkey_fg, wc.button_active_focus_bg); else setColor (wc.button_hotkey_fg, wc.button_active_bg); @@ -67,7 +74,7 @@ void FSwitch::drawCheckButton() { setColor (wc.button_inactive_fg, wc.button_inactive_bg); print (" On "); - if ( hasFocus() ) + if ( hasFocus() && ! button_pressed ) setColor (wc.button_hotkey_fg, wc.button_active_focus_bg); else setColor (wc.button_hotkey_fg, wc.button_active_bg); @@ -108,3 +115,27 @@ void FSwitch::onKeyPress (FKeyEvent* event) else FToggleButton::onKeyPress(event); } + +//---------------------------------------------------------------------- +void FSwitch::onMouseDown (FMouseEvent* event) +{ + FToggleButton::onMouseDown(event); + + if ( event->getButton() != LeftButton ) + return; + + button_pressed = true; + draw(); +} + +//---------------------------------------------------------------------- +void FSwitch::onMouseUp (FMouseEvent* event) +{ + FToggleButton::onMouseUp(event); + + if ( event->getButton() != LeftButton ) + return; + + button_pressed = false; + draw(); +} diff --git a/src/fswitch.h b/src/fswitch.h index a1223e39..ab13e994 100644 --- a/src/fswitch.h +++ b/src/fswitch.h @@ -17,11 +17,13 @@ class FSwitch : public FToggleButton { private: - int switch_offset_pos; + int switch_offset_pos; + bool button_pressed; private: FSwitch (const FSwitch&); FSwitch& operator = (const FSwitch&); + void init(); void draw(); void drawCheckButton(); @@ -31,6 +33,8 @@ class FSwitch : public FToggleButton virtual ~FSwitch(); // destructor const char* getClassName() const; void onKeyPress (FKeyEvent*); + void onMouseDown (FMouseEvent*); + void onMouseUp (FMouseEvent*); void setText (FString); }; #pragma pack(pop)