Improve and simplify test/clipboard.cxx

- replace FL_OVERRIDE and NULL with 'override' and 'nullptr', resp.
- remove useless global variable (declare it locally in main)
- standardize callbacks using standard callback signatures
- rename some callbacks with more appropriate names
- fix layout of image display and image size info
- improve alignment of text display
- set window size_range (minimal window size)
This commit is contained in:
Albrecht Schlosser
2025-11-29 20:05:13 +01:00
parent 8f55e0634c
commit 6aa95584e0
+22 -19
View File
@@ -59,7 +59,7 @@ public:
: Fl_Box(FL_FLAT_BOX, x, y, w, h, 0) { : Fl_Box(FL_FLAT_BOX, x, y, w, h, 0) {
align(FL_ALIGN_CENTER | FL_ALIGN_CLIP); align(FL_ALIGN_CENTER | FL_ALIGN_CLIP);
} }
void draw() FL_OVERRIDE { void draw() override {
draw_box(); draw_box();
Fl_Image *img = image(); Fl_Image *img = image();
if (img) { // draw the chess pattern below the box centered image if (img) { // draw the chess pattern below the box centered image
@@ -104,7 +104,7 @@ public:
flex->layout(); flex->layout();
} }
int handle(int event) FL_OVERRIDE { int handle(int event) override {
if (event != FL_PASTE) { if (event != FL_PASTE) {
auto val = value(); auto val = value();
int ret = Fl_Tabs::handle(event); int ret = Fl_Tabs::handle(event);
@@ -124,7 +124,7 @@ public:
#if defined(_WIN32) && defined(DEBUG_CLIPBOARD_DATA) #if defined(_WIN32) && defined(DEBUG_CLIPBOARD_DATA)
OpenClipboard(NULL); // OpenClipboard(nullptr); //
char *p = title + strlen(title); char *p = title + strlen(title);
int format = EnumClipboardFormats(0); int format = EnumClipboardFormats(0);
if (format && format < CF_MAX) { if (format && format < CF_MAX) {
@@ -168,10 +168,10 @@ public:
} }
}; };
clipboard_viewer *tabs; // clipboard viewer refresh callback:
// 2nd argument must be `clipboard_viewer *`
// clipboard viewer callback void refresh_cb(Fl_Widget *, void *v) {
void cb(Fl_Widget *wid, clipboard_viewer *tabs) { auto tabs = (clipboard_viewer *)v;
if (Fl::clipboard_contains(Fl::clipboard_image)) { if (Fl::clipboard_contains(Fl::clipboard_image)) {
Fl::paste(*tabs, 1, Fl::clipboard_image); // try to find image in the clipboard Fl::paste(*tabs, 1, Fl::clipboard_image); // try to find image in the clipboard
return; return;
@@ -181,7 +181,7 @@ void cb(Fl_Widget *wid, clipboard_viewer *tabs) {
} }
// "Save PNG" callback // "Save PNG" callback
void save_cb(Fl_Widget *wid, clipboard_viewer *tabs) { void save_cb(Fl_Widget *wid, void *) {
if (cl_img && !cl_img->fail()) { if (cl_img && !cl_img->fail()) {
Fl_Native_File_Chooser fnfc; Fl_Native_File_Chooser fnfc;
fnfc.title("Please select a .png file"); fnfc.title("Please select a .png file");
@@ -211,24 +211,26 @@ void wrap_cb(Fl_Widget *w, void *d) {
} }
// called after clipboard was changed or at application activation // called after clipboard was changed or at application activation
void clip_callback(int source, void *data) { void clipboard_cb(int source, void *data) {
if (source == 1) if (source == 1)
cb(NULL, (clipboard_viewer *)data); refresh_cb(nullptr, data);
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {
fl_register_images(); // required for the X11 platform to allow pasting of images fl_register_images(); // required for the X11 platform to allow pasting of images
Fl_Window *win = new Fl_Window(500, 550, "FLTK Clipboard Viewer"); Fl_Window *win = new Fl_Window(500, 550, "FLTK Clipboard Viewer");
tabs = new clipboard_viewer(0, 0, 500, 500); auto tabs = new clipboard_viewer(0, 0, 500, 500);
Fl_Group *g = new Fl_Group(5, 30, 490, 460, Fl::clipboard_image); // will display the image form Fl_Group *g = new Fl_Group(5, 30, 490, 460, Fl::clipboard_image); // will display the image form
g->box(FL_FLAT_BOX); g->box(FL_FLAT_BOX);
image_box = new chess(5, 30, 490, 450); image_box = new chess(5, 30, 490, 440);
image_size = new Fl_Box(FL_NO_BOX, 5, 485, 490, 10, 0); image_size = new Fl_Box(FL_NO_BOX, 5, 472, 490, 16, 0);
image_size->align(FL_ALIGN_CENTER | FL_ALIGN_CLIP);
g->end(); g->end();
g->resizable(image_box);
g->selection_color(TAB_COLOR); g->selection_color(TAB_COLOR);
Fl_Text_Buffer *buffer = new Fl_Text_Buffer(); Fl_Text_Buffer *buffer = new Fl_Text_Buffer();
display = new Fl_Text_Display(5, 30, 490, 460, Fl::clipboard_plain_text); // will display the text form display = new Fl_Text_Display(5, 40, 490, 455, Fl::clipboard_plain_text); // will display the text form
display->buffer(buffer); display->buffer(buffer);
display->selection_color(TAB_COLOR); display->selection_color(TAB_COLOR);
display->textfont(FL_COURIER); // use fixed font for text display display->textfont(FL_COURIER); // use fixed font for text display
@@ -242,15 +244,15 @@ int main(int argc, char **argv) {
auto refresh = new Fl_Button(0, 0, 0, 0, "Refresh from clipboard"); auto refresh = new Fl_Button(0, 0, 0, 0, "Refresh from clipboard");
flex->fixed(refresh, 200); flex->fixed(refresh, 200);
refresh->callback((Fl_Callback *)cb, tabs); refresh->callback(refresh_cb, (void *)tabs);
save = new Fl_Button(0, 0, 0, 0 , "Save PNG"); save = new Fl_Button(0, 0, 0, 0 , "Save PNG");
flex->fixed(save, 120); flex->fixed(save, 120);
save->callback((Fl_Callback *)save_cb, tabs); save->callback(save_cb);
wrap = new Fl_Check_Button(0, 0, 0, 0 , "wrap mode"); wrap = new Fl_Check_Button(0, 0, 0, 0 , "wrap mode");
flex->fixed(wrap, 120); flex->fixed(wrap, 120);
wrap->callback((Fl_Callback *)wrap_cb, display); wrap->callback(wrap_cb, display);
wrap->box(FL_UP_BOX); wrap->box(FL_UP_BOX);
wrap->visible_focus(0); wrap->visible_focus(0);
wrap->value(0); wrap->value(0);
@@ -258,6 +260,7 @@ int main(int argc, char **argv) {
flex->end(); flex->end();
win->end(); win->end();
win->resizable(tabs); win->resizable(tabs);
win->size_range(380, 300);
win->show(argc, argv); win->show(argc, argv);
// TEST: set another default background color // TEST: set another default background color
#if (0) #if (0)
@@ -266,8 +269,8 @@ int main(int argc, char **argv) {
Fl::set_color(FL_BACKGROUND2_COLOR, 0xdd, 0xee, 0xff); Fl::set_color(FL_BACKGROUND2_COLOR, 0xdd, 0xee, 0xff);
} }
#endif #endif
clip_callback(1, tabs); // use clipboard content at start clipboard_cb(1, tabs); // use clipboard content at start
Fl::add_clipboard_notify(clip_callback, tabs); // will update with new clipboard content Fl::add_clipboard_notify(clipboard_cb, tabs); // will update with new clipboard content
Fl_Image::RGB_scaling(FL_RGB_SCALING_BILINEAR); // set bilinear image scaling method Fl_Image::RGB_scaling(FL_RGB_SCALING_BILINEAR); // set bilinear image scaling method
return Fl::run(); return Fl::run();
} }