From b7189192e2e31ce5ca1f2eaac2a303f9b8216ded Mon Sep 17 00:00:00 2001 From: Matthias Melcher Date: Sat, 29 Mar 2025 22:40:13 +0100 Subject: Adds a new event FL_TOOLTIP_EVENT... ... and Fl_Tootip::override_text() to allow users to dynamically generate tooltips. --- test/color_chooser.cxx | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/color_chooser.cxx b/test/color_chooser.cxx index 61fbc4caf..a9a7a5ca1 100644 --- a/test/color_chooser.cxx +++ b/test/color_chooser.cxx @@ -21,6 +21,7 @@ #include #include #include +#include #include #include @@ -84,6 +85,22 @@ void cb2(Fl_Widget *, void *v) { bx->parent()->redraw(); } +class Image_Box: public Fl_Box { +public: + Image_Box(int x, int y, int w, int h, const char *label = nullptr) + : Fl_Box(x, y, w, h, label) { } + int handle(int event) { + if (event == FL_TOOLTIP_EVENT) { + const char *color_name_lut[] = { "blue", "green", "black", "red" }; + int quadrant = (Fl::event_x() < x()+w()/2) + 2*(Fl::event_y() < y()+h()/2); + char buf[80]; + ::snprintf(buf, 79, "Color %s at x=%d, y=%d", color_name_lut[quadrant], Fl::event_x(), Fl::event_y()); + return Fl_Tooltip::override_text(buf); + } + return Fl_Box::handle(event); + } +}; + int main(int argc, char ** argv) { Fl::set_color(fullcolor_cell,145,159,170); Fl_Window window(400,400); @@ -98,7 +115,8 @@ int main(int argc, char ** argv) { b1.callback(cb1,&box); Fl_Button b2(120,120,180,30,"fl_color_chooser()"); b2.callback(cb2,&box); - Fl_Box image_box(160,190,width,height,0); + Image_Box image_box(160,190,width,height,0); + image_box.tooltip("Image Box"); make_image(); (new Fl_RGB_Image(image, width, height))->label(&image_box); Fl_Box b(160,310,120,30,"Example of fl_draw_image()"); -- cgit v1.2.3 From 5dd1062df53c747339b875c8cb0c13df576ad4b8 Mon Sep 17 00:00:00 2001 From: Matthias Melcher Date: Sat, 29 Mar 2025 23:36:09 +0100 Subject: Adding `FL_BEFORE_MENU` event to classes derived from `Fl_Menu_` --- test/color_chooser.cxx | 2 +- test/menubar.cxx | 31 +++++++++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 3 deletions(-) (limited to 'test') diff --git a/test/color_chooser.cxx b/test/color_chooser.cxx index a9a7a5ca1..f5c9827cc 100644 --- a/test/color_chooser.cxx +++ b/test/color_chooser.cxx @@ -90,7 +90,7 @@ public: Image_Box(int x, int y, int w, int h, const char *label = nullptr) : Fl_Box(x, y, w, h, label) { } int handle(int event) { - if (event == FL_TOOLTIP_EVENT) { + if (event == FL_BEFORE_TOOLTIP) { const char *color_name_lut[] = { "blue", "green", "black", "red" }; int quadrant = (Fl::event_x() < x()+w()/2) + 2*(Fl::event_y() < y()+h()/2); char buf[80]; diff --git a/test/menubar.cxx b/test/menubar.cxx index a5ccfc234..2aeec991c 100644 --- a/test/menubar.cxx +++ b/test/menubar.cxx @@ -210,7 +210,7 @@ void menu_location_cb(Fl_Widget* w, void* data) if (((Fl_Choice*)w)->value() == 1) { // switch to system menu bar menubar->hide(); const Fl_Menu_Item *menu = menubar->menu(); - smenubar = new Fl_Sys_Menu_Bar(0,0,0,30); + smenubar = new Fl_Sys_Menu_Bar(0,0,0,30); smenubar->menu(menu); smenubar->callback(test_cb); } @@ -236,6 +236,30 @@ void about_cb(Fl_Widget*, void*) { fl_message("The menubar test app."); } +class Dynamic_Choice: public Fl_Choice { +public: + Dynamic_Choice(int x, int y, int w, int h, const char *label=nullptr) + : Fl_Choice(x, y, w, h, label) { } + int handle(int event) { + static int flip_flop = 0; + if (event == FL_BEFORE_MENU) { + // The following line is legal because we used `copy()` to create a + // writable copy of the menu array when creating this Choice. + Fl_Menu_Item *mi = const_cast(menu()); + if (flip_flop == 1) { + mi[7].flags |= FL_MENU_INACTIVE; + mi[8].flags &= ~FL_MENU_INACTIVE; + flip_flop = 0; + } else { + mi[7].flags &= ~FL_MENU_INACTIVE; + mi[8].flags |= FL_MENU_INACTIVE; + flip_flop = 1; + } + } + return Fl_Choice::handle(event); + } +}; + int main(int argc, char **argv) { for (int i=0; i<99; i++) { char buf[100]; @@ -256,7 +280,10 @@ int main(int argc, char **argv) { mb1.tooltip("this is a menu button"); mb1.callback(test_cb); menus[1] = &mb1; - Fl_Choice ch(300,100,80,25,"&choice:"); ch.menu(pulldown); + Dynamic_Choice ch(300,100,80,25,"&choice:"); + ch.copy(pulldown); + ch.add("Flip"); + ch.add("Flop"); ch.tooltip("this is a choice menu"); ch.callback(test_cb); menus[2] = &ch; -- cgit v1.2.3 From 61c2b798becff0c5b479f399f48fb0387ada4703 Mon Sep 17 00:00:00 2001 From: Matthias Melcher Date: Sat, 29 Mar 2025 23:52:29 +0100 Subject: Adding missing `override`s --- test/color_chooser.cxx | 2 +- test/menubar.cxx | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'test') diff --git a/test/color_chooser.cxx b/test/color_chooser.cxx index f5c9827cc..3b62fa059 100644 --- a/test/color_chooser.cxx +++ b/test/color_chooser.cxx @@ -89,7 +89,7 @@ class Image_Box: public Fl_Box { public: Image_Box(int x, int y, int w, int h, const char *label = nullptr) : Fl_Box(x, y, w, h, label) { } - int handle(int event) { + int handle(int event) override { if (event == FL_BEFORE_TOOLTIP) { const char *color_name_lut[] = { "blue", "green", "black", "red" }; int quadrant = (Fl::event_x() < x()+w()/2) + 2*(Fl::event_y() < y()+h()/2); diff --git a/test/menubar.cxx b/test/menubar.cxx index 2aeec991c..6cf22458a 100644 --- a/test/menubar.cxx +++ b/test/menubar.cxx @@ -240,7 +240,7 @@ class Dynamic_Choice: public Fl_Choice { public: Dynamic_Choice(int x, int y, int w, int h, const char *label=nullptr) : Fl_Choice(x, y, w, h, label) { } - int handle(int event) { + int handle(int event) override { static int flip_flop = 0; if (event == FL_BEFORE_MENU) { // The following line is legal because we used `copy()` to create a -- cgit v1.2.3 From b2b5e47ede75f0b3df06ed28537b459d79b9f867 Mon Sep 17 00:00:00 2001 From: Matthias Melcher Date: Sun, 30 Mar 2025 00:25:18 +0100 Subject: Somewhat better example for dynamic tooltip. --- test/color_chooser.cxx | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) (limited to 'test') diff --git a/test/color_chooser.cxx b/test/color_chooser.cxx index 3b62fa059..263cad28a 100644 --- a/test/color_chooser.cxx +++ b/test/color_chooser.cxx @@ -85,16 +85,24 @@ void cb2(Fl_Widget *, void *v) { bx->parent()->redraw(); } -class Image_Box: public Fl_Box { +class Sample_Box: public Fl_Box { public: - Image_Box(int x, int y, int w, int h, const char *label = nullptr) + Sample_Box(int x, int y, int w, int h, const char *label = nullptr) : Fl_Box(x, y, w, h, label) { } int handle(int event) override { if (event == FL_BEFORE_TOOLTIP) { - const char *color_name_lut[] = { "blue", "green", "black", "red" }; - int quadrant = (Fl::event_x() < x()+w()/2) + 2*(Fl::event_y() < y()+h()/2); - char buf[80]; - ::snprintf(buf, 79, "Color %s at x=%d, y=%d", color_name_lut[quadrant], Fl::event_x(), Fl::event_y()); + char buf[128]; + uchar r, g, b; + Fl::get_color(color(), r, g, b); + if ((color()&255) && (color()!=16)) { + ::snprintf(buf, 127, + "Background color is:\n" + "palette no. %d = r:%d, g:%d, b:%d", color(), r, g, b); + } else { + ::snprintf(buf, 127, + "Background color is:\n" + "r:%d, g:%d, b:%d", r, g, b); + } return Fl_Tooltip::override_text(buf); } return Fl_Box::handle(event); @@ -104,7 +112,8 @@ public: int main(int argc, char ** argv) { Fl::set_color(fullcolor_cell,145,159,170); Fl_Window window(400,400); - Fl_Box box(30,30,340,340); + Sample_Box box(30,30,340,340); + box.tooltip("Show RGB values"); box.box(FL_THIN_DOWN_BOX); c = fullcolor_cell; box.color(c); @@ -115,8 +124,7 @@ int main(int argc, char ** argv) { b1.callback(cb1,&box); Fl_Button b2(120,120,180,30,"fl_color_chooser()"); b2.callback(cb2,&box); - Image_Box image_box(160,190,width,height,0); - image_box.tooltip("Image Box"); + Fl_Box image_box(160,190,width,height,0); make_image(); (new Fl_RGB_Image(image, width, height))->label(&image_box); Fl_Box b(160,310,120,30,"Example of fl_draw_image()"); -- cgit v1.2.3 From 0eb6bb8e6d34b39399cda8b768851a3218eb7e2c Mon Sep 17 00:00:00 2001 From: Matthias Melcher Date: Sun, 30 Mar 2025 00:51:34 +0100 Subject: Fixing a few more compiler warnings. --- test/fractals.cxx | 2 +- test/terminal.fl | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) (limited to 'test') diff --git a/test/fractals.cxx b/test/fractals.cxx index d9cf9905f..5e3839d03 100644 --- a/test/fractals.cxx +++ b/test/fractals.cxx @@ -70,7 +70,7 @@ int main(int, char**) { # define srand48(x) (srand((x))) #elif defined __APPLE__ # define drand48() (((float) rand())/((float) RAND_MAX)) -# define srand48(x) (srand((x))) +# define srand48(x) (srand((int)(x))) #endif typedef enum { NOTALLOWED, MOUNTAIN, TREE, ISLAND, BIGMTN, STEM, LEAF, diff --git a/test/terminal.fl b/test/terminal.fl index b2c0d4d1f..3c9186b68 100644 --- a/test/terminal.fl +++ b/test/terminal.fl @@ -452,8 +452,7 @@ const char *test[] = { }; if (reset) { index = 0; return 0; } -return show_test(test, index);} {selected - } +return show_test(test, index);} {} } Function {test_esc_rgbcolors(bool reset)} { comment {--- 0020: Test RGB Colors} return_type {static int} @@ -1373,7 +1372,7 @@ for ( int t=0; t 0) G_tty->append(s, bytes); // write block to terminal, handles utf8 + else if (bytes > 0) G_tty->append(s, (int)bytes); // write block to terminal, handles utf8 else break; // pipe closed } PCLOSE(fp); G_tty->append_ascii("\\033[33;2m<>\\033[0m\\n"); -G_tty->redraw();} {} +G_tty->redraw();} {selected + } } decl {////// GUI LAYOUT //////} {private local } -- cgit v1.2.3 From f4978a014997656b4592c2b3b866865f76d390ea Mon Sep 17 00:00:00 2001 From: Matthias Melcher Date: Tue, 1 Apr 2025 17:00:51 +0200 Subject: Adding the FL_MENU_CHATTY flag to Fl_Menu_Item. If set, menu items will also call the callback when highlighting changes. The reason is given with Fl::callback_reason(). #941 --- test/menubar.cxx | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'test') diff --git a/test/menubar.cxx b/test/menubar.cxx index 6cf22458a..355921eba 100644 --- a/test/menubar.cxx +++ b/test/menubar.cxx @@ -61,7 +61,19 @@ void test_cb(Fl_Widget* w, void*) { G_tty->printf("%s\n", m->label()); } -void quit_cb(Fl_Widget*, void*) {exit(0);} +void quit_cb(Fl_Widget*, void*) { + switch (Fl::callback_reason()) { + case FL_REASON_SELECTED: + exit(0); + case FL_REASON_GOT_FOCUS: + G_tty->printf("Selecting this menu item will quit this application!\n"); + break; + case FL_REASON_LOST_FOCUS: + G_tty->printf("Risk of quitting averted.\n"); + break; + default: break; + } +} Fl_Menu_Item hugemenu[100]; @@ -70,7 +82,7 @@ Fl_Menu_Item menutable[] = { {"&File",0,0,0,FL_SUBMENU}, {"&Open", FL_ALT+'o', 0, 0, FL_MENU_INACTIVE}, {"&Close", 0, 0}, - {"&Quit", FL_ALT+'q', quit_cb, 0, FL_MENU_DIVIDER}, + {"&Quit", FL_ALT+'q', quit_cb, 0, FL_MENU_DIVIDER|FL_MENU_CHATTY}, #if (OVERRIDE_SCALING_SHORTCUTS) {"CTRL/0", FL_COMMAND+'0', 0}, -- cgit v1.2.3