Class FSwitch: Hold down the mouse button displays the switch in a different color.

This commit is contained in:
Markus Gans 2015-07-06 23:15:34 +02:00
parent 71050832ac
commit b371ad32f7
2 changed files with 40 additions and 5 deletions

View File

@ -12,7 +12,7 @@
//---------------------------------------------------------------------- //----------------------------------------------------------------------
FSwitch::FSwitch(FWidget* parent) : FToggleButton(parent) FSwitch::FSwitch(FWidget* parent) : FToggleButton(parent)
{ {
button_width = 11; init();
} }
//---------------------------------------------------------------------- //----------------------------------------------------------------------
@ -20,7 +20,7 @@ FSwitch::FSwitch ( const FString& txt,
FWidget* parent ) : FToggleButton(txt, parent) FWidget* parent ) : FToggleButton(txt, parent)
{ {
switch_offset_pos = int(txt.getLength()) + 1; switch_offset_pos = int(txt.getLength()) + 1;
button_width = 11; init();
} }
//---------------------------------------------------------------------- //----------------------------------------------------------------------
@ -30,6 +30,13 @@ FSwitch::~FSwitch() // destructor
// private methods of FSwitch // private methods of FSwitch
//----------------------------------------------------------------------
void FSwitch::init()
{
button_width = 11;
button_pressed = false;
}
//---------------------------------------------------------------------- //----------------------------------------------------------------------
void FSwitch::draw() void FSwitch::draw()
{ {
@ -53,7 +60,7 @@ void FSwitch::drawCheckButton()
if ( checked ) if ( checked )
{ {
if ( hasFocus() ) if ( hasFocus() && ! button_pressed )
setColor (wc.button_hotkey_fg, wc.button_active_focus_bg); setColor (wc.button_hotkey_fg, wc.button_active_focus_bg);
else else
setColor (wc.button_hotkey_fg, wc.button_active_bg); 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); setColor (wc.button_inactive_fg, wc.button_inactive_bg);
print (" On "); print (" On ");
if ( hasFocus() ) if ( hasFocus() && ! button_pressed )
setColor (wc.button_hotkey_fg, wc.button_active_focus_bg); setColor (wc.button_hotkey_fg, wc.button_active_focus_bg);
else else
setColor (wc.button_hotkey_fg, wc.button_active_bg); setColor (wc.button_hotkey_fg, wc.button_active_bg);
@ -108,3 +115,27 @@ void FSwitch::onKeyPress (FKeyEvent* event)
else else
FToggleButton::onKeyPress(event); 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();
}

View File

@ -18,10 +18,12 @@ class FSwitch : public FToggleButton
{ {
private: private:
int switch_offset_pos; int switch_offset_pos;
bool button_pressed;
private: private:
FSwitch (const FSwitch&); FSwitch (const FSwitch&);
FSwitch& operator = (const FSwitch&); FSwitch& operator = (const FSwitch&);
void init();
void draw(); void draw();
void drawCheckButton(); void drawCheckButton();
@ -31,6 +33,8 @@ class FSwitch : public FToggleButton
virtual ~FSwitch(); // destructor virtual ~FSwitch(); // destructor
const char* getClassName() const; const char* getClassName() const;
void onKeyPress (FKeyEvent*); void onKeyPress (FKeyEvent*);
void onMouseDown (FMouseEvent*);
void onMouseUp (FMouseEvent*);
void setText (FString); void setText (FString);
}; };
#pragma pack(pop) #pragma pack(pop)