summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--FL/Fl_Shortcut_Button.H38
-rw-r--r--fluid/CMakeLists.txt4
-rw-r--r--fluid/Fl_Menu_Type.cxx47
-rw-r--r--fluid/Fl_Widget_Type.cxx5
-rw-r--r--fluid/Makefile2
-rw-r--r--fluid/custom_widgets.cxx (renamed from fluid/Shortcut_Button.cxx)57
-rw-r--r--fluid/custom_widgets.h (renamed from fluid/Shortcut_Button.h)10
-rw-r--r--fluid/function_panel.cxx2
-rw-r--r--fluid/function_panel.fl4
-rw-r--r--fluid/widget_panel.cxx22
-rw-r--r--fluid/widget_panel.fl23
-rw-r--r--fluid/widget_panel.h6
-rw-r--r--src/CMakeLists.txt1
-rw-r--r--src/Fl_Shortcut_Button.cxx191
-rw-r--r--src/Makefile1
15 files changed, 298 insertions, 115 deletions
diff --git a/FL/Fl_Shortcut_Button.H b/FL/Fl_Shortcut_Button.H
new file mode 100644
index 000000000..f928944f5
--- /dev/null
+++ b/FL/Fl_Shortcut_Button.H
@@ -0,0 +1,38 @@
+//
+// Shortcut Button header file for the Fast Light Tool Kit (FLTK).
+//
+// Copyright 1998-2023 by Bill Spitzak and others.
+//
+// This library is free software. Distribution and use rights are outlined in
+// the file "COPYING" which should have been included with this file. If this
+// file is missing or damaged, see the license at:
+//
+// https://www.fltk.org/COPYING.php
+//
+// Please see the following page on how to report bugs and issues:
+//
+// https://www.fltk.org/bugs.php
+//
+
+#ifndef Fl_Shortcut_Button_H
+#define Fl_Shortcut_Button_H
+
+#include <FL/Fl_Button.H>
+
+class Fl_Shortcut_Button : public Fl_Button {
+private:
+ bool hot_, pre_hot_;
+ Fl_Shortcut pre_esc_;
+protected:
+ Fl_Shortcut shortcut_value;
+ void do_end_hot_callback();
+ int handle(int) FL_OVERRIDE;
+ void draw() FL_OVERRIDE;
+public:
+ Fl_Shortcut_Button(int X,int Y,int W,int H, const char* l = 0);
+ void value(Fl_Shortcut shortcut);
+ Fl_Shortcut value();
+};
+
+#endif // Fl_Shortcut_Button_H
+
diff --git a/fluid/CMakeLists.txt b/fluid/CMakeLists.txt
index a7a0d08df..a9aac0019 100644
--- a/fluid/CMakeLists.txt
+++ b/fluid/CMakeLists.txt
@@ -24,11 +24,11 @@ set (CPPFILES
Fl_Widget_Type.cxx
Fl_Window_Type.cxx
Fluid_Image.cxx
- Shortcut_Button.cxx
about_panel.cxx
align_widget.cxx
alignment_panel.cxx
code.cxx
+ custom_widgets.cxx
factory.cxx
file.cxx
fluid.cxx
@@ -52,13 +52,13 @@ set (HEADERFILES
Fl_Widget_Type.h
Fl_Window_Type.h
Fluid_Image.h
- Shortcut_Button.h
StyleParse.h
about_panel.h
align_widget.h
alignment_panel.h
code.h
comments.h
+ custom_widgets.h
factory.h
file.h
fluid.h
diff --git a/fluid/Fl_Menu_Type.cxx b/fluid/Fl_Menu_Type.cxx
index 371f7ab29..9d88f56c8 100644
--- a/fluid/Fl_Menu_Type.cxx
+++ b/fluid/Fl_Menu_Type.cxx
@@ -28,7 +28,7 @@
#include "file.h"
#include "code.h"
#include "Fluid_Image.h"
-#include "Shortcut_Button.h"
+#include "custom_widgets.h"
#include <FL/Fl.H>
#include <FL/fl_message.H>
@@ -37,6 +37,7 @@
#include <FL/Fl_Value_Input.H>
#include <FL/Fl_Text_Display.H>
#include <FL/Fl_Menu_Button.H>
+#include <FL/Fl_Shortcut_Button.H>
#include <FL/Fl_Output.H>
#include <FL/fl_draw.H>
#include <FL/Fl_Multi_Label.H>
@@ -392,14 +393,18 @@ void Fl_Menu_Item_Type::write_item(Fd_Code_Writer& f) {
else
f.write_c("\"\"");
if (((Fl_Button*)o)->shortcut()) {
- int s = ((Fl_Button*)o)->shortcut();
- if (g_project.use_FL_COMMAND && (s & (FL_CTRL|FL_META))) {
- f.write_c(", FL_COMMAND|0x%x, ", s & ~(FL_CTRL|FL_META));
- } else {
- f.write_c(", 0x%x, ", s);
- }
- } else
+ int s = ((Fl_Button*)o)->shortcut();
+ if (g_project.use_FL_COMMAND && (s & (FL_CTRL|FL_META))) {
+ f.write_c(", ");
+ if (s & FL_COMMAND) f.write_c("FL_COMMAND|");
+ if (s & FL_CONTROL) f.write_c("FL_CONTROL|");
+ f.write_c("0x%x, ", s & ~(FL_CTRL|FL_META));
+ } else {
+ f.write_c(", 0x%x, ", s);
+ }
+ } else {
f.write_c(", 0, ");
+ }
if (callback()) {
const char* k = is_name(callback()) ? 0 : class_name(1);
if (k) {
@@ -689,16 +694,16 @@ Fl_Menu_Bar_Type Fl_Menu_Bar_type;
// Shortcut entry item in panel:
-void shortcut_in_cb(Shortcut_Button* i, void* v) {
+void shortcut_in_cb(Fl_Shortcut_Button* i, void* v) {
if (v == LOAD) {
if (current_widget->is_button())
- i->svalue = ((Fl_Button*)(current_widget->o))->shortcut();
+ i->value( ((Fl_Button*)(current_widget->o))->shortcut() );
else if (current_widget->is_input())
- i->svalue = ((Fl_Input_*)(current_widget->o))->shortcut();
+ i->value( ((Fl_Input_*)(current_widget->o))->shortcut() );
else if (current_widget->is_value_input())
- i->svalue = ((Fl_Value_Input*)(current_widget->o))->shortcut();
+ i->value( ((Fl_Value_Input*)(current_widget->o))->shortcut() );
else if (current_widget->is_text_display())
- i->svalue = ((Fl_Text_Display*)(current_widget->o))->shortcut();
+ i->value( ((Fl_Text_Display*)(current_widget->o))->shortcut() );
else {
i->hide();
return;
@@ -710,21 +715,21 @@ void shortcut_in_cb(Shortcut_Button* i, void* v) {
for (Fl_Type *o = Fl_Type::first; o; o = o->next)
if (o->selected && o->is_button()) {
Fl_Button* b = (Fl_Button*)(((Fl_Widget_Type*)o)->o);
- if (b->shortcut()!=i->svalue) mod = 1;
- b->shortcut(i->svalue);
+ if (b->shortcut() != (int)i->value()) mod = 1;
+ b->shortcut(i->value());
if (o->is_menu_item()) ((Fl_Widget_Type*)o)->redraw();
} else if (o->selected && o->is_input()) {
Fl_Input_* b = (Fl_Input_*)(((Fl_Widget_Type*)o)->o);
- if (b->shortcut()!=i->svalue) mod = 1;
- b->shortcut(i->svalue);
+ if (b->shortcut() != (int)i->value()) mod = 1;
+ b->shortcut(i->value());
} else if (o->selected && o->is_value_input()) {
Fl_Value_Input* b = (Fl_Value_Input*)(((Fl_Widget_Type*)o)->o);
- if (b->shortcut()!=i->svalue) mod = 1;
- b->shortcut(i->svalue);
+ if (b->shortcut() != (int)i->value()) mod = 1;
+ b->shortcut(i->value());
} else if (o->selected && o->is_text_display()) {
Fl_Text_Display* b = (Fl_Text_Display*)(((Fl_Widget_Type*)o)->o);
- if (b->shortcut()!=i->svalue) mod = 1;
- b->shortcut(i->svalue);
+ if (b->shortcut() != (int)i->value()) mod = 1;
+ b->shortcut(i->value());
}
if (mod) set_modflag(1);
}
diff --git a/fluid/Fl_Widget_Type.cxx b/fluid/Fl_Widget_Type.cxx
index 76cc31b11..afdb61823 100644
--- a/fluid/Fl_Widget_Type.cxx
+++ b/fluid/Fl_Widget_Type.cxx
@@ -3019,7 +3019,10 @@ void Fl_Widget_Type::write_widget_code(Fd_Code_Writer& f) {
else if (is_text_display()) shortcut = ((Fl_Text_Display*)o)->shortcut();
if (shortcut) {
if (g_project.use_FL_COMMAND && (shortcut & (FL_CTRL|FL_META))) {
- f.write_c("%s%s->shortcut(FL_COMMAND|0x%x);\n", f.indent(), var, shortcut & ~(FL_CTRL|FL_META));
+ f.write_c("%s%s->shortcut(", f.indent(), var);
+ if (shortcut & FL_COMMAND) f.write_c("FL_COMMAND|");
+ if (shortcut & FL_CONTROL) f.write_c("FL_CONTROL|");
+ f.write_c("0x%x);\n", shortcut & ~(FL_CTRL|FL_META));
} else {
f.write_c("%s%s->shortcut(0x%x);\n", f.indent(), var, shortcut);
}
diff --git a/fluid/Makefile b/fluid/Makefile
index aa85f8d14..3511b683e 100644
--- a/fluid/Makefile
+++ b/fluid/Makefile
@@ -26,11 +26,11 @@ CPPFILES = \
Fl_Widget_Type.cxx \
Fl_Window_Type.cxx \
Fluid_Image.cxx \
- Shortcut_Button.cxx \
about_panel.cxx \
align_widget.cxx \
alignment_panel.cxx \
code.cxx \
+ custom_widgets.cxx \
factory.cxx \
file.cxx \
fluid.cxx \
diff --git a/fluid/Shortcut_Button.cxx b/fluid/custom_widgets.cxx
index bbeb6305f..f1a12a474 100644
--- a/fluid/Shortcut_Button.cxx
+++ b/fluid/custom_widgets.cxx
@@ -14,7 +14,7 @@
// https://www.fltk.org/bugs.php
//
-#include "Shortcut_Button.h"
+#include "custom_widgets.h"
#include "fluid.h"
#include "Fl_Window_Type.h"
@@ -30,61 +30,6 @@
#include <FL/fl_string_functions.h>
#include "../src/flstring.h"
-/** \class Shortcut_Button
- A button that allows the user to type a key combination to create shortcuts.
- After clicked once, the button catches the following keyboard events and
- records the pressed keys and all modifiers. It draws a text representation of
- the shortcut. The backspace key deletes the current shortcut.
- */
-
-/**
- Draw the textual representation of the shortcut.
- */
-void Shortcut_Button::draw() {
- if (value()) draw_box(FL_DOWN_BOX, (Fl_Color)9);
- else draw_box(FL_UP_BOX, FL_WHITE);
- fl_font(FL_HELVETICA,14); fl_color(FL_FOREGROUND_COLOR);
- if (g_project.use_FL_COMMAND && (svalue & (FL_CTRL|FL_META))) {
- char buf[1024];
- fl_snprintf(buf, 1023, "Command+%s", fl_shortcut_label(svalue&~(FL_CTRL|FL_META)));
- fl_draw(buf,x()+6,y(),w(),h(),FL_ALIGN_LEFT);
- } else {
- fl_draw(fl_shortcut_label(svalue),x()+6,y(),w(),h(),FL_ALIGN_LEFT);
- }
-}
-
-/**
- Handle keystrokes to catch the user's shortcut.
- */
-int Shortcut_Button::handle(int e) {
- when(0); type(FL_TOGGLE_BUTTON);
- if (e == FL_KEYBOARD) {
- if (!value()) return 0;
- int v = Fl::event_text()[0];
- if ( (v > 32 && v < 0x7f) || (v > 0xa0 && v <= 0xff) ) {
- if (isupper(v)) {
- v = tolower(v);
- v |= FL_SHIFT;
- }
- v = v | (Fl::event_state()&(FL_META|FL_ALT|FL_CTRL));
- } else {
- v = (Fl::event_state()&(FL_META|FL_ALT|FL_CTRL|FL_SHIFT)) | Fl::event_key();
- if (v == FL_BackSpace && svalue) v = 0;
- }
- if (v != svalue) {svalue = v; set_changed(); redraw(); do_callback(); }
- return 1;
- } else if (e == FL_UNFOCUS) {
- int c = changed(); value(0); if (c) set_changed();
- return 1;
- } else if (e == FL_FOCUS) {
- return value();
- } else {
- int r = Fl_Button::handle(e);
- if (e == FL_RELEASE && value() && Fl::focus() != this) take_focus();
- return r;
- }
-}
-
/** \class Widget_Bin_Button
The Widget_Bin_Button button is a button that can be used in the widget bin to
allow the user to drag and drop widgets into a window or group. This feature
diff --git a/fluid/Shortcut_Button.h b/fluid/custom_widgets.h
index 7f1755ef0..8a464ae82 100644
--- a/fluid/Shortcut_Button.h
+++ b/fluid/custom_widgets.h
@@ -20,16 +20,6 @@
#include <FL/Fl_Button.H>
#include <FL/Fl_Input.H>
-// Button will catch and display keyboard shortcuts when activated.
-class Shortcut_Button : public Fl_Button {
-public:
- int svalue;
- int handle(int) FL_OVERRIDE;
- void draw() FL_OVERRIDE;
- Shortcut_Button(int X,int Y,int W,int H, const char* l = 0) :
- Fl_Button(X,Y,W,H,l) {svalue = 0;}
-};
-
// Adding drag and drop for dragging widgets into windows.
class Widget_Bin_Button : public Fl_Button {
public:
diff --git a/fluid/function_panel.cxx b/fluid/function_panel.cxx
index 4c55c2691..c9a43b2fb 100644
--- a/fluid/function_panel.cxx
+++ b/fluid/function_panel.cxx
@@ -18,7 +18,7 @@
#include "function_panel.h"
#include "fluid.h"
-#include "Shortcut_Button.h"
+#include "custom_widgets.h"
#include "pixmaps.h"
#include "factory.h"
#include "Fl_Type.h"
diff --git a/fluid/function_panel.fl b/fluid/function_panel.fl
index cbe505ccc..0607f6d82 100644
--- a/fluid/function_panel.fl
+++ b/fluid/function_panel.fl
@@ -23,7 +23,7 @@ comment {//
decl {\#include "fluid.h"} {private local
}
-decl {\#include "Shortcut_Button.h"} {private global
+decl {\#include "custom_widgets.h"} {selected private global
}
decl {\#include "pixmaps.h"} {private local
@@ -496,7 +496,7 @@ Function {make_comment_panel()} {open
xywh {370 250 80 20} labelsize 11 hotspot
}
Fl_Button comment_panel_cancel {
- label Cancel selected
+ label Cancel
xywh {460 250 80 20} shortcut 0xff1b labelsize 11
}
Fl_Box {} {
diff --git a/fluid/widget_panel.cxx b/fluid/widget_panel.cxx
index 1c8c97696..a871486bb 100644
--- a/fluid/widget_panel.cxx
+++ b/fluid/widget_panel.cxx
@@ -43,7 +43,7 @@ Fl_Menu_Item menu_1[] = {
{"right", 0, 0, (void*)((fl_intptr_t)FL_ALIGN_RIGHT), 0, (uchar)FL_NORMAL_LABEL, 0, 11, 0},
{"bottom left", 0, 0, (void*)((fl_intptr_t)FL_ALIGN_BOTTOM_LEFT), 0, (uchar)FL_NORMAL_LABEL, 0, 11, 0},
{"bottom", 0, 0, (void*)((fl_intptr_t)FL_ALIGN_BOTTOM), 0, (uchar)FL_NORMAL_LABEL, 0, 11, 0},
- {"bottom right", 0, 0, (void*)((fl_intptr_t)FL_ALIGN_BOTTOM_RIGHT), 128, (uchar)FL_NORMAL_LABEL, 0, 11, 0},
+ {"bottom right", 0, 0, (void*)((fl_intptr_t)FL_ALIGN_BOTTOM_RIGHT), 0, (uchar)FL_NORMAL_LABEL, 0, 11, 0},
{" Outside Alignment ", 0, 0, (void*)((fl_intptr_t)0xFFFFFFFF), 1, (uchar)FL_NORMAL_LABEL, 2, 11, 0},
{"left top", 0, 0, (void*)((fl_intptr_t)FL_ALIGN_LEFT_TOP), 0, (uchar)FL_NORMAL_LABEL, 0, 11, 0},
{"right top", 0, 0, (void*)((fl_intptr_t)FL_ALIGN_RIGHT_TOP), 0, (uchar)FL_NORMAL_LABEL, 0, 11, 0},
@@ -562,20 +562,26 @@ sized to fit the container.");
} // Fl_Box* o
o->end();
} // Fl_Group* o
+ { Fl_Group* o = new Fl_Group(95, 210, 0, 20, "Shortcut:");
+ o->labelfont(1);
+ o->labelsize(11);
+ o->align(Fl_Align(FL_ALIGN_LEFT));
+ o->end();
+ } // Fl_Group* o
{ // This is a special button that grabs keystrokes directly
- Shortcut_Button* o = new Shortcut_Button(95, 210, 310, 20, "Shortcut:");
+ Fl_Shortcut_Button* o = new Fl_Shortcut_Button(95, 210, 310, 20);
o->tooltip("The shortcut key for the widget.\nUse \'Backspace\' key to clear.");
o->box(FL_DOWN_BOX);
o->color(FL_BACKGROUND2_COLOR);
- o->selection_color(FL_BACKGROUND2_COLOR);
+ o->selection_color((Fl_Color)12);
o->labeltype(FL_NORMAL_LABEL);
- o->labelfont(1);
+ o->labelfont(0);
o->labelsize(11);
o->labelcolor(FL_FOREGROUND_COLOR);
o->callback((Fl_Callback*)shortcut_in_cb);
- o->align(Fl_Align(FL_ALIGN_LEFT));
- o->when(FL_WHEN_RELEASE);
- } // Shortcut_Button* o
+ o->align(Fl_Align(FL_ALIGN_CENTER));
+ o->when(FL_WHEN_CHANGED);
+ } // Fl_Shortcut_Button* o
{ Fl_Group* o = new Fl_Group(95, 235, 300, 20, "X Class:");
o->labelfont(1);
o->labelsize(11);
@@ -623,6 +629,7 @@ sized to fit the container.");
} // Fl_Light_Button* o
{ Fl_Light_Button* o = new Fl_Light_Button(160, 260, 60, 20, "Active");
o->tooltip("Activate the widget.");
+ o->shortcut(0x400061);
o->selection_color((Fl_Color)1);
o->labelsize(11);
o->callback((Fl_Callback*)active_cb);
@@ -656,7 +663,6 @@ sized to fit the container.");
} // Fl_Input* o
{ Fl_Box* o = new Fl_Box(95, 305, 300, 5);
o->labelsize(11);
- Fl_Group::current()->resizable(o);
} // Fl_Box* o
o->end();
Fl_Group::current()->resizable(o);
diff --git a/fluid/widget_panel.fl b/fluid/widget_panel.fl
index 317b16518..afd6bb62a 100644
--- a/fluid/widget_panel.fl
+++ b/fluid/widget_panel.fl
@@ -23,7 +23,7 @@ comment {//
decl {\#include "Fl_Widget_Type.h"} {private global
}
-decl {\#include "Shortcut_Button.h"} {public global
+decl {\#include "custom_widgets.h"} {public global
}
Function {make_widget_panel()} {
@@ -49,7 +49,7 @@ Function {make_widget_panel()} {
xywh {95 40 309 20} labelfont 1 labelsize 11 align 4
} {
Fl_Input {} {
- callback label_cb selected
+ callback label_cb
tooltip {The label text for the widget.
Use Ctrl-J for newlines.} xywh {95 40 190 20} labelfont 1 labelsize 11 when 15 textsize 11 resizable
}
@@ -244,7 +244,7 @@ or compressed in the original file format} xywh {364 90 20 20} type Toggle
MenuItem {} {
label {bottom right}
user_data {(fl_intptr_t)FL_ALIGN_BOTTOM_RIGHT}
- xywh {115 115 100 20} labelsize 11 divider
+ xywh {115 115 100 20} labelsize 11
}
MenuItem {} {
label { Outside Alignment }
@@ -465,14 +465,17 @@ h, ph, sh, ch, and i} xywh {275 150 55 20} labelsize 11 align 5 textsize 11
xywh {395 185 0 20} resizable
}
}
+ Fl_Group {} {
+ label {Shortcut:} open
+ xywh {95 210 0 20} labelfont 1 labelsize 11 align 4
+ } {}
Fl_Button {} {
- label {Shortcut:}
callback shortcut_in_cb
- comment {This is a special button that grabs keystrokes directly}
+ comment {This is a special button that grabs keystrokes directly} selected
tooltip {The shortcut key for the widget.
-Use 'Backspace' key to clear.} xywh {95 210 310 20} box DOWN_BOX color 7 selection_color 7 labelfont 1 labelsize 11 align 4
- code0 {\#include "Shortcut_Button.h"}
- class Shortcut_Button
+Use 'Backspace' key to clear.} xywh {95 210 310 20} box DOWN_BOX color 7 selection_color 12 labelsize 11 when 1
+ code0 {\#include <FL/Fl_Shortcut_Button.H>}
+ class Fl_Shortcut_Button
}
Fl_Group {} {
label {X Class:}
@@ -513,7 +516,7 @@ Use 'Backspace' key to clear.} xywh {95 210 310 20} box DOWN_BOX color 7 selecti
Fl_Light_Button {} {
label Active
callback active_cb
- tooltip {Activate the widget.} xywh {160 260 60 20} selection_color 1 labelsize 11
+ tooltip {Activate the widget.} xywh {160 260 60 20} shortcut 0x400061 selection_color 1 labelsize 11
}
Fl_Light_Button {} {
label Resizable
@@ -536,7 +539,7 @@ Use 'Backspace' key to clear.} xywh {95 210 310 20} box DOWN_BOX color 7 selecti
Use Ctrl-J for newlines.} xywh {95 285 310 20} labelfont 1 labelsize 11 textsize 11
}
Fl_Box {} {
- xywh {95 305 300 5} labelsize 11 resizable
+ xywh {95 305 300 5} labelsize 11
}
}
Fl_Group {} {
diff --git a/fluid/widget_panel.h b/fluid/widget_panel.h
index 6d6ddfe95..ddeed0b79 100644
--- a/fluid/widget_panel.h
+++ b/fluid/widget_panel.h
@@ -19,7 +19,7 @@
#ifndef widget_panel_h
#define widget_panel_h
#include <FL/Fl.H>
-#include "Shortcut_Button.h"
+#include "custom_widgets.h"
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Tabs.H>
#include <FL/Fl_Group.H>
@@ -79,8 +79,8 @@ extern void set_min_size_cb(Fl_Button*, void*);
extern void max_w_cb(Fl_Value_Input*, void*);
extern void max_h_cb(Fl_Value_Input*, void*);
extern void set_max_size_cb(Fl_Button*, void*);
-#include "Shortcut_Button.h"
-extern void shortcut_in_cb(Shortcut_Button*, void*);
+#include <FL/Fl_Shortcut_Button.H>
+extern void shortcut_in_cb(Fl_Shortcut_Button*, void*);
extern void xclass_cb(Fl_Input*, void*);
#include <FL/Fl_Light_Button.H>
extern void border_cb(Fl_Light_Button*, void*);
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 56d3467d6..40671d392 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -78,6 +78,7 @@ set (CPPFILES
Fl_Scroll.cxx
Fl_Scrollbar.cxx
Fl_Shared_Image.cxx
+ Fl_Shortcut_Button.cxx
Fl_Simple_Terminal.cxx
Fl_Single_Window.cxx
Fl_Slider.cxx
diff --git a/src/Fl_Shortcut_Button.cxx b/src/Fl_Shortcut_Button.cxx
new file mode 100644
index 000000000..a411184f2
--- /dev/null
+++ b/src/Fl_Shortcut_Button.cxx
@@ -0,0 +1,191 @@
+//
+// Shortcut Button code for the Fast Light Tool Kit (FLTK).
+//
+// Copyright 1998-2023 by Bill Spitzak and others.
+//
+// This library is free software. Distribution and use rights are outlined in
+// the file "COPYING" which should have been included with this file. If this
+// file is missing or damaged, see the license at:
+//
+// https://www.fltk.org/COPYING.php
+//
+// Please see the following page on how to report bugs and issues:
+//
+// https://www.fltk.org/bugs.php
+//
+
+#include <FL/Fl_Shortcut_Button.H>
+
+#include <FL/Fl.H>
+#include <FL/fl_draw.H>
+#include <FL/fl_utf8.h>
+#include "flstring.h"
+
+#include <ctype.h>
+
+
+/** \class Fl_Shortcut_Button
+ A button that allows the user to type a key combination to create shortcuts.
+ After clicked once, the button catches the following keyboard events and
+ records the pressed keys and all modifiers. It draws a text representation of
+ the shortcut.
+
+ The backspace key deletes the current shortcut. A second click on the button
+ or moving focus makes the last shortcut permanent.
+
+ The Shortcut button calls the user callback after every change if
+ FL_WHEN_CHANGED is set, and when the button is no longer recording
+ shortcuts if FL_WHEN_RELEASE is set.
+ */
+
+/** Construct a shortcut button.
+ \param X, Y, W, H position and size of the button
+ \param l label text when no shortcut is set
+ */
+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),
+ pre_esc_(0),
+ shortcut_value(0)
+{
+ box(FL_DOWN_BOX);
+ selection_color(FL_SELECTION_COLOR);
+ type(FL_TOGGLE_BUTTON);
+}
+
+/**
+ Set the displayed shortcut.
+ \param[in] shortcut encoded as key and modifier
+ */
+void Fl_Shortcut_Button::value(Fl_Shortcut shortcut) {
+ shortcut_value = shortcut;
+ clear_changed();
+ redraw();
+}
+
+/**
+ Return the user selected shortcut.
+ \return shortcut encoded as key and modifier
+ */
+Fl_Shortcut Fl_Shortcut_Button::value() {
+ return shortcut_value;
+}
+
+/**
+ Draw the textual representation of the shortcut button.
+
+ When the button can receive shortcut key events, it's "hot". A hot button
+ is drawn in selection color. A cold button is drawn as a regular text box
+ containing a human readable version of the shortcut key.
+ */
+void Fl_Shortcut_Button::draw() {
+ Fl_Color col = hot_ ? selection_color() : color();
+ Fl_Boxtype b = box();
+ if (hot_) {
+ if (down_box())
+ b = down_box();
+ else if ((b > FL_FLAT_BOX) && (b < FL_BORDER_BOX))
+ b = Fl_Boxtype(((int)b) ^ 1);
+ }
+ draw_box(b, col);
+ draw_backdrop();
+
+ 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());
+ Fl_Color textcol = fl_contrast(labelcolor(), col);
+ if (!active_r())
+ textcol = fl_inactive(textcol);
+ fl_color(textcol);
+ fl_font(labelfont(), labelsize());
+ 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 (Fl::focus() == this) draw_focus();
+}
+
+/**
+ Call the callback if the user is interested.
+ */
+void Fl_Shortcut_Button::do_end_hot_callback() {
+ if (when() & FL_WHEN_RELEASE) {
+ do_callback(FL_REASON_RELEASED);
+ }
+}
+
+/**
+ Handle keystrokes to catch the user's shortcut.
+ */
+int Fl_Shortcut_Button::handle(int e) {
+ switch (e) {
+ case FL_PUSH:
+ if (Fl::visible_focus() && handle(FL_FOCUS)) Fl::focus(this);
+ pre_hot_ = hot_;
+ /* FALLTHROUGH */
+ case FL_DRAG:
+ case FL_RELEASE:
+ if (Fl::event_inside(this)) {
+ hot_ = !pre_hot_;
+ } else {
+ hot_ = pre_hot_;
+ }
+ if ((e == FL_RELEASE) && pre_hot_ && !hot_)
+ do_end_hot_callback();
+ redraw();
+ return 1;
+ case FL_UNFOCUS:
+ if (hot_) do_end_hot_callback();
+ hot_ = 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
+ // 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) ) {
+ if (isupper(v)) {
+ v = tolower(v);
+ v |= FL_SHIFT;
+ }
+ v = v | (Fl::event_state()&(FL_META|FL_ALT|FL_CTRL));
+ } else {
+ v = (Fl::event_state() & (FL_META|FL_ALT|FL_CTRL|FL_SHIFT)) | Fl::event_key();
+ if (v == FL_Escape) {
+ if (shortcut_value == FL_Escape) {
+ v = pre_esc_;
+ do_end_hot_callback();
+ hot_ = false;
+ } else {
+ pre_esc_ = shortcut_value;
+ }
+ }
+ if ((v == FL_BackSpace) && shortcut_value) {
+ v = 0;
+ }
+ }
+ if (v != (int)shortcut_value) {
+ shortcut_value = v;
+ set_changed();
+ redraw();
+ if (when() & FL_WHEN_CHANGED) do_callback(FL_REASON_CHANGED);
+ clear_changed();
+ }
+ return 1;
+ } else {
+ if ((Fl::event_key() == FL_Enter) || (strcmp(Fl::event_text(), " ") == 0)) {
+ hot_ = true;
+ redraw();
+ return 1;
+ }
+ }
+ break;
+ }
+ return Fl_Button::handle(e);
+}
+
diff --git a/src/Makefile b/src/Makefile
index cbf9d51e4..4e20b3e56 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -81,6 +81,7 @@ CPPFILES = \
Fl_Scroll.cxx \
Fl_Scrollbar.cxx \
Fl_Shared_Image.cxx \
+ Fl_Shortcut_Button.cxx \
Fl_Simple_Terminal.cxx \
Fl_Single_Window.cxx \
Fl_Slider.cxx \