Skip to content

Commit

Permalink
Added visual indication for buttons activated by a keyboard shortcut …
Browse files Browse the repository at this point in the history
…(STR 2372

git-svn-id: file:///fltk/svn/fltk/branches/branch-1.3@7826 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
  • Loading branch information
Matthias Melcher committed Nov 14, 2010
1 parent e729fcc commit e7c9305
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES
@@ -1,5 +1,6 @@
CHANGES IN FLTK 1.3.0

- Added visual feedback for button shortcuts (STR #2372)
- Fixed internationalisation of menus using FLuid (STR #2246)
- Fixed blinking of selection when the mouse was dragged
outside of the Fl_Text_* widget
Expand Down
6 changes: 6 additions & 0 deletions FL/Fl_Button.H
Expand Up @@ -46,6 +46,8 @@

extern FL_EXPORT Fl_Shortcut fl_old_shortcut(const char*);

class Fl_Widget_Tracker;

/**
\class Fl_Button
\brief Buttons generate callbacks when they are clicked by the user.
Expand Down Expand Up @@ -88,6 +90,10 @@ class FL_EXPORT Fl_Button : public Fl_Widget {

protected:

static Fl_Widget_Tracker *key_release_tracker;
static void key_release_timeout(void*);
void simulate_key_action();

virtual void draw();

public:
Expand Down
33 changes: 33 additions & 0 deletions src/Fl_Button.cxx
Expand Up @@ -30,6 +30,10 @@
#include <FL/Fl_Group.H>
#include <FL/Fl_Window.H>


Fl_Widget_Tracker *Fl_Button::key_release_tracker = 0;


// There are a lot of subclasses, named Fl_*_Button. Some of
// them are implemented by setting the type() value and testing it
// here. This includes Fl_Radio_Button and Fl_Toggle_Button
Expand Down Expand Up @@ -156,6 +160,8 @@ int Fl_Button::handle(int event) {
} else if (type() == FL_TOGGLE_BUTTON) {
value(!value());
if (when() & FL_WHEN_CHANGED) do_callback();
} else {
simulate_key_action();
}
if (wp.deleted()) return 1;
if (when() & FL_WHEN_RELEASE) do_callback();
Expand All @@ -166,6 +172,33 @@ int Fl_Button::handle(int event) {
}
}

void Fl_Button::simulate_key_action()
{
if (key_release_tracker) {
Fl::remove_timeout(key_release_timeout, key_release_tracker);
key_release_timeout(key_release_tracker);
}
value(1);
redraw();
key_release_tracker = new Fl_Widget_Tracker(this);
Fl::add_timeout(0.15, key_release_timeout, key_release_tracker);
}

void Fl_Button::key_release_timeout(void *d)
{
Fl_Widget_Tracker *wt = (Fl_Widget_Tracker*)d;
if (!wt)
return;
if (wt==key_release_tracker)
key_release_tracker = 0L;
Fl_Button *btn = (Fl_Button*)wt->widget();
if (btn) {
btn->value(0);
btn->redraw();
}
delete wt;
}

/**
The constructor creates the button using the given position, size and label.
\param[in] X, Y, W, H position and size of the widget
Expand Down
1 change: 1 addition & 0 deletions src/Fl_Return_Button.cxx
Expand Up @@ -61,6 +61,7 @@ void Fl_Return_Button::draw() {
int Fl_Return_Button::handle(int event) {
if (event == FL_SHORTCUT &&
(Fl::event_key() == FL_Enter || Fl::event_key() == FL_KP_Enter)) {
simulate_key_action();
do_callback();
return 1;
} else
Expand Down
3 changes: 2 additions & 1 deletion test/buttons.cxx
Expand Up @@ -39,7 +39,8 @@

int main(int argc, char ** argv) {
Fl_Window *window = new Fl_Window(320,130);
(new Fl_Button(10, 10, 130, 30, "Fl_Button"))->tooltip("This is a Tooltip.");
Fl_Button *b = new Fl_Button(10, 10, 130, 30, "Fl_Button");
b->tooltip("This is a Tooltip.");
new Fl_Return_Button(150, 10, 160, 30, "Fl_Return_Button");
new Fl_Repeat_Button(10,50,130,30,"Fl_Repeat_Button");
new Fl_Light_Button(10,90,130,30,"Fl_Light_Button");
Expand Down

0 comments on commit e7c9305

Please sign in to comment.