Add "Save PNG" feature to clipboard viewer demo

This commit is contained in:
Albrecht Schlosser 2021-11-18 14:15:42 +01:00
parent 0bc06e8f1a
commit ca89f48c0b
2 changed files with 102 additions and 28 deletions

View File

@ -17,13 +17,17 @@
#include <FL/Fl.H> #include <FL/Fl.H>
#include <FL/Fl_Window.H> #include <FL/Fl_Window.H>
#include <FL/Fl_Box.H> #include <FL/Fl_Box.H>
#include <FL/Fl_Image.H> #include <FL/Fl_PNG_Image.H>
#include <FL/Fl_Shared_Image.H> #include <FL/Fl_Shared_Image.H>
#include <FL/Fl_Text_Buffer.H>
#include <FL/Fl_Text_Display.H> #include <FL/Fl_Text_Display.H>
#include <FL/Fl_Tabs.H> #include <FL/Fl_Tabs.H>
#include <FL/Fl_Button.H> #include <FL/Fl_Button.H>
#include <FL/Fl_Native_File_Chooser.H>
#include <FL/fl_draw.H> #include <FL/fl_draw.H>
#include <FL/fl_ask.H>
#include <stdio.h>
#ifdef _WIN32 #ifdef _WIN32
#include <windows.h> #include <windows.h>
#endif // _WIN32 #endif // _WIN32
@ -34,6 +38,7 @@
Fl_Box *image_box; Fl_Box *image_box;
Fl_Box *image_size; Fl_Box *image_size;
Fl_Text_Display *display; Fl_Text_Display *display;
Fl_RGB_Image *cl_img = 0; // image from clipboard
inline int fl_min(int a, int b) { inline int fl_min(int a, int b) {
return (a < b ? a : b); return (a < b ? a : b);
@ -48,9 +53,12 @@ public:
} }
void draw() { void draw() {
draw_box(); draw_box();
Fl_Image *im = image(); Fl_Image *img = image();
if (im) { // draw the chess pattern below the box centered image if (img) { // draw the chess pattern below the box centered image
int X = x() + (w() - im->w()) / 2, Y = y() + (h() - im->h()) / 2, W = im->w(), H = im->h(); int X = x() + (w() - img->w()) / 2;
int Y = y() + (h() - img->h()) / 2;
int W = img->w();
int H = img->h();
fl_push_clip(X, Y, W, H); fl_push_clip(X, Y, W, H);
fl_push_clip(x(), y(), w(), h()); fl_push_clip(x(), y(), w(), h());
fl_color(FL_WHITE); fl_color(FL_WHITE);
@ -80,11 +88,11 @@ public:
if (event != FL_PASTE) if (event != FL_PASTE)
return Fl_Tabs::handle(event); return Fl_Tabs::handle(event);
if (strcmp(Fl::event_clipboard_type(), Fl::clipboard_image) == 0) { // an image is being pasted if (strcmp(Fl::event_clipboard_type(), Fl::clipboard_image) == 0) { // an image is being pasted
Fl_RGB_Image *im = (Fl_RGB_Image *)Fl::event_clipboard(); // get it as an Fl_RGB_Image object cl_img = (Fl_RGB_Image *)Fl::event_clipboard(); // get it as an Fl_RGB_Image object
if (!im) if (!cl_img)
return 1; return 1;
char title[300]; char title[300];
sprintf(title, "%dx%d", im->w(), im->h()); // display the image original size sprintf(title, "%dx%d", cl_img->w(), cl_img->h()); // display the image original size
#ifdef _WIN32 #ifdef _WIN32
OpenClipboard(NULL); // display extra technical info about clipboard content OpenClipboard(NULL); // display extra technical info about clipboard content
char *p = title + strlen(title); char *p = title + strlen(title);
@ -103,16 +111,18 @@ public:
HANDLE h; HANDLE h;
if ((h = GetClipboardData(CF_DIB))) { if ((h = GetClipboardData(CF_DIB))) {
LPBITMAPINFO lpBI = (LPBITMAPINFO)GlobalLock(h); LPBITMAPINFO lpBI = (LPBITMAPINFO)GlobalLock(h);
sprintf(p, " biBitCount=%d biCompression=%d biClrUsed=%d", lpBI->bmiHeader.biBitCount, sprintf(p, " biBitCount=%d biCompression=%d biClrUsed=%d",
(int)lpBI->bmiHeader.biCompression, (int)lpBI->bmiHeader.biClrUsed); lpBI->bmiHeader.biBitCount,
(int)lpBI->bmiHeader.biCompression,
(int)lpBI->bmiHeader.biClrUsed);
} }
CloseClipboard(); CloseClipboard();
#endif #endif
Fl_Image *oldimg = image_box->image(); Fl_Image *oldimg = image_box->image();
delete oldimg; delete oldimg;
if (im->w() > image_box->w() || im->h() > image_box->h()) if (cl_img->w() > image_box->w() || cl_img->h() > image_box->h())
im->scale(image_box->w(), image_box->h()); cl_img->scale(image_box->w(), image_box->h());
image_box->image(im); // show the scaled image image_box->image(cl_img); // show the scaled image
image_size->copy_label(title); image_size->copy_label(title);
value(image_box->parent()); value(image_box->parent());
window()->redraw(); window()->redraw();
@ -125,12 +135,9 @@ public:
} }
}; };
// global variables
// chess *chess_obj;
clipboard_viewer *tabs; clipboard_viewer *tabs;
// clipboard viewer callback
void cb(Fl_Widget *wid, clipboard_viewer *tabs) { void cb(Fl_Widget *wid, clipboard_viewer *tabs) {
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
@ -140,16 +147,35 @@ void cb(Fl_Widget *wid, clipboard_viewer *tabs) {
Fl::paste(*tabs, 1, Fl::clipboard_plain_text); // also try to find text Fl::paste(*tabs, 1, Fl::clipboard_plain_text); // also try to find text
} }
void clip_callback(int source, void *data) { // called after clipboard was changed or at application activation // "Save PNG" callback
void save_cb(Fl_Widget *wid, clipboard_viewer *tabs) {
if (cl_img && !cl_img->fail()) {
Fl_Native_File_Chooser fnfc;
fnfc.title("Please select a .png file");
fnfc.type(Fl_Native_File_Chooser::BROWSE_SAVE_FILE);
fnfc.filter("PNG\t*.png\n");
fnfc.options(Fl_Native_File_Chooser::SAVEAS_CONFIRM | Fl_Native_File_Chooser::USE_FILTER_EXT);
if (fnfc.show())
return;
const char *filename = fnfc.filename();
if (filename)
fl_write_png(filename, cl_img);
} else {
fl_message("%s", "No image available");
}
}
// called after clipboard was changed or at application activation
void clip_callback(int source, void *data) {
if (source == 1) if (source == 1)
cb(NULL, (clipboard_viewer *)data); cb(NULL, (clipboard_viewer *)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, "clipboard viewer"); Fl_Window *win = new Fl_Window(500, 550, "FLTK Clipboard Viewer");
tabs = new clipboard_viewer(0, 0, 500, 500); tabs = new clipboard_viewer(0, 0, 500, 500);
Fl_Group *g = new Fl_Group(5, 30, 490, 460, Fl::clipboard_image); // g 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, 450);
image_size = new Fl_Box(FL_NO_BOX, 5, 485, 490, 10, 0); image_size = new Fl_Box(FL_NO_BOX, 5, 485, 490, 10, 0);
@ -157,24 +183,31 @@ int main(int argc, char **argv) {
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); // display will display the text form display = new Fl_Text_Display(5, 30, 490, 460, 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);
tabs->end(); tabs->end();
tabs->resizable(display); tabs->resizable(display);
Fl_Group *g2 = new Fl_Group(10, 510, 200, 25); Fl_Group *g2 = new Fl_Group(10, 510, 330, 25);
Fl_Button *refresh = new Fl_Button(10, 510, 200, 25, "Refresh from clipboard"); Fl_Button *refresh = new Fl_Button(10, 510, 200, 25, "Refresh from clipboard");
refresh->callback((Fl_Callback *)cb, tabs); refresh->callback((Fl_Callback *)cb, tabs);
Fl_Button *save = new Fl_Button(220, 510, 100, 25, "Save PNG");
save->callback((Fl_Callback *)save_cb, tabs);
g2->end(); g2->end();
g2->resizable(NULL); g2->resizable(NULL);
win->end(); win->end();
win->resizable(tabs); win->resizable(tabs);
win->show(argc, argv); win->show(argc, argv);
clip_callback(1, tabs); // use clipboard content at start // TEST: set another default background color
Fl::add_clipboard_notify(clip_callback, #if (0)
tabs); // will update with new clipboard content immediately or at application activation if (argc < 2) {
Fl::set_color(FL_BACKGROUND_COLOR, 0xff, 0xee, 0xdd);
Fl::set_color(FL_BACKGROUND2_COLOR, 0xdd, 0xee, 0xff);
}
#endif
clip_callback(1, tabs); // use clipboard content at start
Fl::add_clipboard_notify(clip_callback, 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();
} }

View File

@ -250,10 +250,29 @@ buttons.o: ../FL/platform_types.h
cairo_test.o: ../config.h cairo_test.o: ../config.h
cairo_test.o: ../FL/abi-version.h cairo_test.o: ../FL/abi-version.h
cairo_test.o: ../FL/Enumerations.H cairo_test.o: ../FL/Enumerations.H
cairo_test.o: ../FL/fl_ask.H cairo_test.o: ../FL/Fl.H
cairo_test.o: ../FL/fl_attr.h cairo_test.o: ../FL/Fl_Bitmap.H
cairo_test.o: ../FL/Fl_Box.H
cairo_test.o: ../FL/Fl_Cairo_Window.H
cairo_test.o: ../FL/fl_casts.H
cairo_test.o: ../FL/Fl_Device.H
cairo_test.o: ../FL/Fl_Double_Window.H
cairo_test.o: ../FL/fl_draw.H
cairo_test.o: ../FL/Fl_Export.H cairo_test.o: ../FL/Fl_Export.H
cairo_test.o: ../FL/Fl_Graphics_Driver.H
cairo_test.o: ../FL/Fl_Group.H
cairo_test.o: ../FL/Fl_Image.H
cairo_test.o: ../FL/Fl_Pixmap.H
cairo_test.o: ../FL/Fl_Plugin.H
cairo_test.o: ../FL/Fl_Preferences.H
cairo_test.o: ../FL/Fl_Rect.H
cairo_test.o: ../FL/Fl_RGB_Image.H
cairo_test.o: ../FL/fl_types.h cairo_test.o: ../FL/fl_types.h
cairo_test.o: ../FL/fl_utf8.h
cairo_test.o: ../FL/Fl_Widget.H
cairo_test.o: ../FL/Fl_Window.H
cairo_test.o: ../FL/math.h
cairo_test.o: ../FL/platform.H
cairo_test.o: ../FL/platform_types.h cairo_test.o: ../FL/platform_types.h
checkers.o: ../FL/abi-version.h checkers.o: ../FL/abi-version.h
checkers.o: ../FL/Enumerations.H checkers.o: ../FL/Enumerations.H
@ -288,21 +307,42 @@ checkers.o: ../FL/platform_types.h
checkers.o: checkers_pieces.h checkers.o: checkers_pieces.h
clipboard.o: ../FL/abi-version.h clipboard.o: ../FL/abi-version.h
clipboard.o: ../FL/Enumerations.H clipboard.o: ../FL/Enumerations.H
clipboard.o: ../FL/filename.H
clipboard.o: ../FL/Fl.H clipboard.o: ../FL/Fl.H
clipboard.o: ../FL/fl_ask.H
clipboard.o: ../FL/fl_attr.h
clipboard.o: ../FL/Fl_Bitmap.H clipboard.o: ../FL/Fl_Bitmap.H
clipboard.o: ../FL/Fl_Box.H clipboard.o: ../FL/Fl_Box.H
clipboard.o: ../FL/Fl_Browser.H
clipboard.o: ../FL/Fl_Browser_.H
clipboard.o: ../FL/Fl_Button.H clipboard.o: ../FL/Fl_Button.H
clipboard.o: ../FL/fl_casts.H clipboard.o: ../FL/fl_casts.H
clipboard.o: ../FL/Fl_Check_Button.H
clipboard.o: ../FL/Fl_Choice.H
clipboard.o: ../FL/Fl_Device.H clipboard.o: ../FL/Fl_Device.H
clipboard.o: ../FL/Fl_Double_Window.H
clipboard.o: ../FL/fl_draw.H clipboard.o: ../FL/fl_draw.H
clipboard.o: ../FL/Fl_Export.H clipboard.o: ../FL/Fl_Export.H
clipboard.o: ../FL/Fl_File_Browser.H
clipboard.o: ../FL/Fl_File_Chooser.H
clipboard.o: ../FL/Fl_File_Icon.H
clipboard.o: ../FL/Fl_File_Input.H
clipboard.o: ../FL/Fl_Graphics_Driver.H clipboard.o: ../FL/Fl_Graphics_Driver.H
clipboard.o: ../FL/Fl_Group.H clipboard.o: ../FL/Fl_Group.H
clipboard.o: ../FL/Fl_Image.H clipboard.o: ../FL/Fl_Image.H
clipboard.o: ../FL/Fl_Input.H
clipboard.o: ../FL/Fl_Input_.H
clipboard.o: ../FL/Fl_Light_Button.H
clipboard.o: ../FL/Fl_Menu_.H
clipboard.o: ../FL/Fl_Menu_Button.H
clipboard.o: ../FL/Fl_Menu_Item.H
clipboard.o: ../FL/Fl_Native_File_Chooser.H
clipboard.o: ../FL/Fl_Pixmap.H clipboard.o: ../FL/Fl_Pixmap.H
clipboard.o: ../FL/Fl_Plugin.H clipboard.o: ../FL/Fl_Plugin.H
clipboard.o: ../FL/Fl_PNG_Image.H
clipboard.o: ../FL/Fl_Preferences.H clipboard.o: ../FL/Fl_Preferences.H
clipboard.o: ../FL/Fl_Rect.H clipboard.o: ../FL/Fl_Rect.H
clipboard.o: ../FL/Fl_Return_Button.H
clipboard.o: ../FL/Fl_RGB_Image.H clipboard.o: ../FL/Fl_RGB_Image.H
clipboard.o: ../FL/Fl_Scrollbar.H clipboard.o: ../FL/Fl_Scrollbar.H
clipboard.o: ../FL/Fl_Shared_Image.H clipboard.o: ../FL/Fl_Shared_Image.H
@ -310,6 +350,7 @@ clipboard.o: ../FL/Fl_Slider.H
clipboard.o: ../FL/Fl_Tabs.H clipboard.o: ../FL/Fl_Tabs.H
clipboard.o: ../FL/Fl_Text_Buffer.H clipboard.o: ../FL/Fl_Text_Buffer.H
clipboard.o: ../FL/Fl_Text_Display.H clipboard.o: ../FL/Fl_Text_Display.H
clipboard.o: ../FL/Fl_Tile.H
clipboard.o: ../FL/fl_types.h clipboard.o: ../FL/fl_types.h
clipboard.o: ../FL/fl_utf8.h clipboard.o: ../FL/fl_utf8.h
clipboard.o: ../FL/Fl_Valuator.H clipboard.o: ../FL/Fl_Valuator.H