diff options
| author | Matthias Melcher <github@matthiasm.com> | 2023-07-22 15:30:09 +0200 |
|---|---|---|
| committer | Matthias Melcher <github@matthiasm.com> | 2023-07-22 15:30:17 +0200 |
| commit | f0375d6213d67d585c9b8e5adc443d8c8adc280c (patch) | |
| tree | d1835f88b834dc44829c6a6879355cc83206614f /src/Fl_Shortcut_Button.cxx | |
| parent | fa0aa95443689fc0d5fae2abdf16b2920faf55fa (diff) | |
Adds default shortcut to Fl_Shortcut_Button.
Diffstat (limited to 'src/Fl_Shortcut_Button.cxx')
| -rw-r--r-- | src/Fl_Shortcut_Button.cxx | 73 |
1 files changed, 70 insertions, 3 deletions
diff --git a/src/Fl_Shortcut_Button.cxx b/src/Fl_Shortcut_Button.cxx index a411184f2..f6d38c6b0 100644 --- a/src/Fl_Shortcut_Button.cxx +++ b/src/Fl_Shortcut_Button.cxx @@ -46,8 +46,11 @@ Fl_Shortcut_Button::Fl_Shortcut_Button(int X,int Y,int W,int H, const char* l) : Fl_Button(X,Y,W,H,l), hot_(false), pre_hot_(false), + default_set_(false), + handle_default_button_(false), pre_esc_(0), - shortcut_value(0) + shortcut_value(0), + default_shortcut_(0) { box(FL_DOWN_BOX); selection_color(FL_SELECTION_COLOR); @@ -73,6 +76,34 @@ Fl_Shortcut Fl_Shortcut_Button::value() { } /** + Set the default shortcut. + If set, and additional 'reverse' button apears that the user can click to + reset the shortcut to some default value (including 0). + \param[in] shortcut encoded as key and modifier + */ +void Fl_Shortcut_Button::default_value(Fl_Shortcut shortcut) { + default_shortcut_ = shortcut; + default_set_ = true; + redraw(); +} + +/** + Return the default shortcut. + \return shortcut encoded as key and modifier + */ +Fl_Shortcut Fl_Shortcut_Button::default_value() { + return default_shortcut_; +} + +/** + No longer show the button to reverse to a default shortcut. + */ +void Fl_Shortcut_Button::default_clear() { + default_set_ = false; + redraw(); +} + +/** Draw the textual representation of the shortcut button. When the button can receive shortcut key events, it's "hot". A hot button @@ -103,7 +134,12 @@ void Fl_Shortcut_Button::draw() { const char *text = label(); if (shortcut_value) text = fl_shortcut_label(shortcut_value); - fl_draw(text, X, Y, W, H, align() | FL_ALIGN_INSIDE); + if (default_set_) { + fl_draw(text, X, Y, W-H, H, align() | FL_ALIGN_INSIDE); + fl_draw_symbol("@-29undo", X+W-H, Y, H, H, textcol); + } else { + fl_draw(text, X, Y, W, H, align() | FL_ALIGN_INSIDE); + } if (Fl::focus() == this) draw_focus(); } @@ -120,6 +156,35 @@ void Fl_Shortcut_Button::do_end_hot_callback() { Handle keystrokes to catch the user's shortcut. */ int Fl_Shortcut_Button::handle(int e) { + bool inside_default_button = false; + if (default_set_ && ( (e == FL_PUSH) || (e == FL_DRAG) || (e == FL_RELEASE) ) ) { + int X = x() + Fl::box_dx(box()); + int Y = y() + Fl::box_dy(box()); + int W = w() - Fl::box_dw(box()); + int H = h() - Fl::box_dh(box()); + if (Fl::event_inside(this) && (Fl::event_x() > X+W-H)) + inside_default_button = true; + } + if ((e == FL_PUSH) && default_set_ && inside_default_button) { + if (Fl::visible_focus() && handle(FL_FOCUS)) Fl::focus(this); + handle_default_button_ = true; + return 1; + } + if (handle_default_button_) { + if (e == FL_DRAG) + return 1; + if (e == FL_RELEASE) { + if (inside_default_button && (shortcut_value != default_shortcut_)) { + shortcut_value = default_shortcut_; + set_changed(); + redraw(); + if (when() & FL_WHEN_CHANGED) do_callback(FL_REASON_CHANGED); + clear_changed(); + } + handle_default_button_ = false; + return 1; + } + } switch (e) { case FL_PUSH: if (Fl::visible_focus() && handle(FL_FOCUS)) Fl::focus(this); @@ -135,17 +200,19 @@ int Fl_Shortcut_Button::handle(int e) { if ((e == FL_RELEASE) && pre_hot_ && !hot_) do_end_hot_callback(); redraw(); + handle_default_button_ = false; return 1; case FL_UNFOCUS: if (hot_) do_end_hot_callback(); hot_ = false; + handle_default_button_ = false; /* FALLTHROUGH */ case FL_FOCUS: redraw(); return 1; case FL_KEYBOARD: if (hot_) { - // Note: we can't really hanlde non-Latin shortcuts in the Fl_Shortcut + // Note: we can't really handle non-Latin shortcuts in the Fl_Shortcut // type, so we don't handle them here either int v = fl_utf8decode(Fl::event_text(), 0, 0); if ( (v > 32 && v < 0x7f) || (v > 0xa0 && v <= 0xff) ) { |
