2014-05-23 20:47:21 +04:00
|
|
|
//
|
|
|
|
// Clipboard display test application for the Fast Light Tool Kit (FLTK).
|
|
|
|
//
|
2021-11-18 15:44:41 +03:00
|
|
|
// Copyright 1998-2021 by Bill Spitzak and others.
|
2014-05-23 20:47:21 +04:00
|
|
|
//
|
|
|
|
// 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:
|
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// https://www.fltk.org/COPYING.php
|
2014-05-23 20:47:21 +04:00
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// Please see the following page on how to report bugs and issues:
|
2014-05-23 20:47:21 +04:00
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// https://www.fltk.org/bugs.php
|
2014-05-23 20:47:21 +04:00
|
|
|
//
|
|
|
|
|
2021-11-18 15:44:41 +03:00
|
|
|
#include <FL/Fl.H>
|
2014-05-23 20:47:21 +04:00
|
|
|
#include <FL/Fl_Window.H>
|
|
|
|
#include <FL/Fl_Box.H>
|
2021-11-18 16:15:42 +03:00
|
|
|
#include <FL/Fl_PNG_Image.H>
|
2016-05-03 17:28:45 +03:00
|
|
|
#include <FL/Fl_Shared_Image.H>
|
2014-05-23 20:47:21 +04:00
|
|
|
#include <FL/Fl_Text_Display.H>
|
|
|
|
#include <FL/Fl_Tabs.H>
|
|
|
|
#include <FL/Fl_Button.H>
|
2021-11-18 16:15:42 +03:00
|
|
|
#include <FL/Fl_Native_File_Chooser.H>
|
2014-05-23 20:47:21 +04:00
|
|
|
#include <FL/fl_draw.H>
|
2021-11-18 16:15:42 +03:00
|
|
|
#include <FL/fl_ask.H>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2021-11-19 20:29:34 +03:00
|
|
|
// optional: display extra technical info about clipboard content if defined
|
|
|
|
// #define DEBUG_CLIPBOARD_DATA
|
|
|
|
|
|
|
|
#if defined(_WIN32) && defined(DEBUG_CLIPBOARD_DATA)
|
2016-04-01 00:48:15 +03:00
|
|
|
#include <windows.h>
|
2021-11-19 20:29:34 +03:00
|
|
|
#endif // _WIN32 && DEBUG_CLIPBOARD_DATA
|
2014-05-23 20:47:21 +04:00
|
|
|
|
|
|
|
/* Displays and follows the content of the clipboard with either image or text data
|
|
|
|
*/
|
|
|
|
|
|
|
|
Fl_Box *image_box;
|
|
|
|
Fl_Box *image_size;
|
|
|
|
Fl_Text_Display *display;
|
2021-11-18 16:15:42 +03:00
|
|
|
Fl_RGB_Image *cl_img = 0; // image from clipboard
|
2014-05-23 20:47:21 +04:00
|
|
|
|
2021-11-18 15:44:41 +03:00
|
|
|
inline int fl_min(int a, int b) {
|
|
|
|
return (a < b ? a : b);
|
|
|
|
}
|
2017-07-07 17:31:40 +03:00
|
|
|
|
2021-11-18 15:44:41 +03:00
|
|
|
// a box with a chess-like pattern below its image
|
|
|
|
class chess : public Fl_Box {
|
2014-05-23 20:47:21 +04:00
|
|
|
public:
|
2021-11-18 15:44:41 +03:00
|
|
|
chess(int x, int y, int w, int h)
|
|
|
|
: Fl_Box(FL_FLAT_BOX, x, y, w, h, 0) {
|
2014-05-23 20:47:21 +04:00
|
|
|
align(FL_ALIGN_CENTER | FL_ALIGN_CLIP);
|
|
|
|
}
|
2022-12-30 21:14:36 +03:00
|
|
|
void draw() FL_OVERRIDE {
|
2014-05-23 20:47:21 +04:00
|
|
|
draw_box();
|
2021-11-18 16:15:42 +03:00
|
|
|
Fl_Image *img = image();
|
|
|
|
if (img) { // draw the chess pattern below the box centered image
|
|
|
|
int X = x() + (w() - img->w()) / 2;
|
|
|
|
int Y = y() + (h() - img->h()) / 2;
|
|
|
|
int W = img->w();
|
|
|
|
int H = img->h();
|
2021-11-18 15:44:41 +03:00
|
|
|
fl_push_clip(X, Y, W, H);
|
|
|
|
fl_push_clip(x(), y(), w(), h());
|
|
|
|
fl_color(FL_WHITE);
|
|
|
|
fl_rectf(X, Y, W, H);
|
2014-05-23 20:47:21 +04:00
|
|
|
fl_color(FL_LIGHT2);
|
2021-11-18 15:44:41 +03:00
|
|
|
const int side = 4, side2 = 2 * side;
|
|
|
|
for (int j = Y; j < Y + H; j += side) {
|
|
|
|
for (int i = X + (j - Y) % side2; i < X + W; i += side2) {
|
|
|
|
fl_rectf(i, j, side, side);
|
2020-07-01 19:03:10 +03:00
|
|
|
}
|
2014-05-23 20:47:21 +04:00
|
|
|
}
|
|
|
|
fl_pop_clip();
|
|
|
|
fl_pop_clip();
|
2014-09-17 15:51:56 +04:00
|
|
|
}
|
2014-05-23 20:47:21 +04:00
|
|
|
draw_label(); // draw the box image
|
|
|
|
}
|
2021-11-18 15:44:41 +03:00
|
|
|
};
|
2014-05-23 20:47:21 +04:00
|
|
|
|
2014-09-17 15:51:56 +04:00
|
|
|
#define TAB_COLOR FL_DARK3
|
|
|
|
|
2021-11-18 15:44:41 +03:00
|
|
|
// use tabs to display either the image or textual content of the clipboard
|
|
|
|
class clipboard_viewer : public Fl_Tabs {
|
2014-05-23 20:47:21 +04:00
|
|
|
public:
|
2021-11-18 15:44:41 +03:00
|
|
|
clipboard_viewer(int x, int y, int w, int h)
|
|
|
|
: Fl_Tabs(x, y, w, h) {}
|
2022-12-30 21:14:36 +03:00
|
|
|
int handle(int event) FL_OVERRIDE {
|
2021-11-18 15:44:41 +03:00
|
|
|
if (event != FL_PASTE)
|
|
|
|
return Fl_Tabs::handle(event);
|
2014-05-23 20:47:21 +04:00
|
|
|
if (strcmp(Fl::event_clipboard_type(), Fl::clipboard_image) == 0) { // an image is being pasted
|
2021-11-18 16:15:42 +03:00
|
|
|
cl_img = (Fl_RGB_Image *)Fl::event_clipboard(); // get it as an Fl_RGB_Image object
|
|
|
|
if (!cl_img)
|
2021-11-18 15:44:41 +03:00
|
|
|
return 1;
|
2014-05-23 20:47:21 +04:00
|
|
|
char title[300];
|
2022-09-26 17:12:18 +03:00
|
|
|
snprintf(title, 300, "%dx%d", cl_img->w(), cl_img->h()); // display the image original size
|
2021-11-19 20:29:34 +03:00
|
|
|
|
|
|
|
// optional: display extra technical info about clipboard content
|
|
|
|
|
|
|
|
#if defined(_WIN32) && defined(DEBUG_CLIPBOARD_DATA)
|
|
|
|
|
|
|
|
OpenClipboard(NULL); //
|
2021-11-18 15:44:41 +03:00
|
|
|
char *p = title + strlen(title);
|
2014-05-23 20:47:21 +04:00
|
|
|
int format = EnumClipboardFormats(0);
|
2021-11-18 15:44:41 +03:00
|
|
|
if (format && format < CF_MAX) {
|
2022-09-26 17:12:18 +03:00
|
|
|
snprintf(p, sizeof(title) - strlen(title), " %d", format);
|
2021-11-18 15:44:41 +03:00
|
|
|
p += strlen(p);
|
|
|
|
}
|
2014-05-23 20:47:21 +04:00
|
|
|
while (format) {
|
2018-04-29 19:29:13 +03:00
|
|
|
format = EnumClipboardFormats(format);
|
2021-11-18 15:44:41 +03:00
|
|
|
if (format && format < CF_MAX) {
|
2022-09-26 17:12:18 +03:00
|
|
|
snprintf(p, sizeof(title) - strlen(title), " %d", format);
|
2021-11-18 15:44:41 +03:00
|
|
|
p += strlen(p);
|
|
|
|
}
|
2014-05-23 20:47:21 +04:00
|
|
|
}
|
|
|
|
HANDLE h;
|
2014-09-17 15:51:56 +04:00
|
|
|
if ((h = GetClipboardData(CF_DIB))) {
|
2018-04-29 19:29:13 +03:00
|
|
|
LPBITMAPINFO lpBI = (LPBITMAPINFO)GlobalLock(h);
|
2022-09-26 17:12:18 +03:00
|
|
|
snprintf(p, sizeof(title) - strlen(title), " biBitCount=%d biCompression=%d biClrUsed=%d",
|
2021-11-18 16:15:42 +03:00
|
|
|
lpBI->bmiHeader.biBitCount,
|
|
|
|
(int)lpBI->bmiHeader.biCompression,
|
|
|
|
(int)lpBI->bmiHeader.biClrUsed);
|
2014-09-17 15:51:56 +04:00
|
|
|
}
|
2014-05-23 20:47:21 +04:00
|
|
|
CloseClipboard();
|
2021-11-19 20:29:34 +03:00
|
|
|
|
|
|
|
#endif // _WIN32 && DEBUG_CLIPBOARD_DATA
|
|
|
|
|
2018-04-29 19:29:13 +03:00
|
|
|
Fl_Image *oldimg = image_box->image();
|
|
|
|
delete oldimg;
|
2021-11-18 16:15:42 +03:00
|
|
|
if (cl_img->w() > image_box->w() || cl_img->h() > image_box->h())
|
|
|
|
cl_img->scale(image_box->w(), image_box->h());
|
|
|
|
image_box->image(cl_img); // show the scaled image
|
2014-05-23 20:47:21 +04:00
|
|
|
image_size->copy_label(title);
|
|
|
|
value(image_box->parent());
|
|
|
|
window()->redraw();
|
2021-11-18 15:44:41 +03:00
|
|
|
} else { // text is being pasted
|
2014-05-23 20:47:21 +04:00
|
|
|
display->buffer()->text(Fl::event_text());
|
|
|
|
value(display);
|
|
|
|
display->redraw();
|
|
|
|
}
|
2014-09-17 15:51:56 +04:00
|
|
|
return 1;
|
|
|
|
}
|
2021-11-18 15:44:41 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
clipboard_viewer *tabs;
|
2014-05-23 20:47:21 +04:00
|
|
|
|
2021-11-18 16:15:42 +03:00
|
|
|
// clipboard viewer callback
|
2021-11-18 15:44:41 +03:00
|
|
|
void cb(Fl_Widget *wid, clipboard_viewer *tabs) {
|
2014-05-23 20:47:21 +04:00
|
|
|
if (Fl::clipboard_contains(Fl::clipboard_image)) {
|
|
|
|
Fl::paste(*tabs, 1, Fl::clipboard_image); // try to find image in the clipboard
|
|
|
|
return;
|
2014-09-17 15:51:56 +04:00
|
|
|
}
|
2021-11-18 15:44:41 +03:00
|
|
|
if (Fl::clipboard_contains(Fl::clipboard_plain_text))
|
|
|
|
Fl::paste(*tabs, 1, Fl::clipboard_plain_text); // also try to find text
|
2014-05-23 20:47:21 +04:00
|
|
|
}
|
|
|
|
|
2021-11-18 16:15:42 +03:00
|
|
|
// "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) {
|
2021-11-18 15:44:41 +03:00
|
|
|
if (source == 1)
|
|
|
|
cb(NULL, (clipboard_viewer *)data);
|
2014-05-23 20:47:21 +04:00
|
|
|
}
|
|
|
|
|
2021-11-18 15:44:41 +03:00
|
|
|
int main(int argc, char **argv) {
|
2018-11-29 12:20:08 +03:00
|
|
|
fl_register_images(); // required for the X11 platform to allow pasting of images
|
2021-11-18 16:15:42 +03:00
|
|
|
Fl_Window *win = new Fl_Window(500, 550, "FLTK Clipboard Viewer");
|
2014-05-23 20:47:21 +04:00
|
|
|
tabs = new clipboard_viewer(0, 0, 500, 500);
|
2021-11-18 16:15:42 +03:00
|
|
|
Fl_Group *g = new Fl_Group(5, 30, 490, 460, Fl::clipboard_image); // will display the image form
|
2014-05-23 20:47:21 +04:00
|
|
|
g->box(FL_FLAT_BOX);
|
|
|
|
image_box = new chess(5, 30, 490, 450);
|
|
|
|
image_size = new Fl_Box(FL_NO_BOX, 5, 485, 490, 10, 0);
|
|
|
|
g->end();
|
2014-09-17 15:51:56 +04:00
|
|
|
g->selection_color(TAB_COLOR);
|
|
|
|
|
2014-05-23 20:47:21 +04:00
|
|
|
Fl_Text_Buffer *buffer = new Fl_Text_Buffer();
|
2021-11-18 16:15:42 +03:00
|
|
|
display = new Fl_Text_Display(5, 30, 490, 460, Fl::clipboard_plain_text); // will display the text form
|
2014-05-23 20:47:21 +04:00
|
|
|
display->buffer(buffer);
|
2014-09-17 15:51:56 +04:00
|
|
|
display->selection_color(TAB_COLOR);
|
2014-05-23 20:47:21 +04:00
|
|
|
tabs->end();
|
|
|
|
tabs->resizable(display);
|
2014-09-17 15:51:56 +04:00
|
|
|
|
2021-11-18 16:15:42 +03:00
|
|
|
Fl_Group *g2 = new Fl_Group(10, 510, 330, 25);
|
2021-11-18 15:44:41 +03:00
|
|
|
Fl_Button *refresh = new Fl_Button(10, 510, 200, 25, "Refresh from clipboard");
|
|
|
|
refresh->callback((Fl_Callback *)cb, tabs);
|
2021-11-18 16:15:42 +03:00
|
|
|
Fl_Button *save = new Fl_Button(220, 510, 100, 25, "Save PNG");
|
|
|
|
save->callback((Fl_Callback *)save_cb, tabs);
|
2014-05-23 20:47:21 +04:00
|
|
|
g2->end();
|
|
|
|
g2->resizable(NULL);
|
|
|
|
win->end();
|
|
|
|
win->resizable(tabs);
|
2021-11-18 15:44:41 +03:00
|
|
|
win->show(argc, argv);
|
2021-11-18 16:15:42 +03:00
|
|
|
// TEST: set another default background color
|
|
|
|
#if (0)
|
|
|
|
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
|
2014-10-14 15:53:51 +04:00
|
|
|
Fl_Image::RGB_scaling(FL_RGB_SCALING_BILINEAR); // set bilinear image scaling method
|
2014-05-23 20:47:21 +04:00
|
|
|
return Fl::run();
|
|
|
|
}
|