From f0375d6213d67d585c9b8e5adc443d8c8adc280c Mon Sep 17 00:00:00 2001 From: Matthias Melcher Date: Sat, 22 Jul 2023 15:30:09 +0200 Subject: [PATCH] Adds default shortcut to Fl_Shortcut_Button. --- FL/Fl_Shortcut_Button.H | 6 +++- fluid/Fl_Menu_Type.cxx | 1 + src/Fl_Shortcut_Button.cxx | 73 ++++++++++++++++++++++++++++++++++++-- 3 files changed, 76 insertions(+), 4 deletions(-) diff --git a/FL/Fl_Shortcut_Button.H b/FL/Fl_Shortcut_Button.H index 5fef2ccb5..f07c05283 100644 --- a/FL/Fl_Shortcut_Button.H +++ b/FL/Fl_Shortcut_Button.H @@ -21,8 +21,9 @@ class FL_EXPORT Fl_Shortcut_Button : public Fl_Button { private: - bool hot_, pre_hot_; + bool hot_, pre_hot_, default_set_, handle_default_button_; Fl_Shortcut pre_esc_; + Fl_Shortcut default_shortcut_; protected: Fl_Shortcut shortcut_value; void do_end_hot_callback(); @@ -32,6 +33,9 @@ public: Fl_Shortcut_Button(int X,int Y,int W,int H, const char* l = 0); void value(Fl_Shortcut shortcut); Fl_Shortcut value(); + void default_value(Fl_Shortcut shortcut); + Fl_Shortcut default_value(); + void default_clear(); }; #endif // Fl_Shortcut_Button_H diff --git a/fluid/Fl_Menu_Type.cxx b/fluid/Fl_Menu_Type.cxx index 28f77175f..75679e835 100644 --- a/fluid/Fl_Menu_Type.cxx +++ b/fluid/Fl_Menu_Type.cxx @@ -708,6 +708,7 @@ void shortcut_in_cb(Fl_Shortcut_Button* i, void* v) { i->parent()->hide(); return; } + i->default_value( i->value() ); // enable the "undo" capability of the shortcut button i->show(); i->parent()->show(); i->redraw(); 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); @@ -72,6 +75,34 @@ Fl_Shortcut Fl_Shortcut_Button::value() { return shortcut_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. @@ -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) ) {