Skip to content

Commit

Permalink
WIN32 HiDPI support: fix copy and paste of image data when rescaling …
Browse files Browse the repository at this point in the history
…is applied.

git-svn-id: file:///fltk/svn/fltk/branches/branch-1.4@12297 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
  • Loading branch information
Manolo Gouy authored and Manolo Gouy committed Jul 7, 2017
1 parent 22d9007 commit 7847c2d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
8 changes: 7 additions & 1 deletion examples/clipboard.cxx
Expand Up @@ -37,6 +37,9 @@ Fl_Box *image_box;
Fl_Box *image_size;
Fl_Text_Display *display;

inline int fl_min(int a, int b) { return (a < b ? a : b); }


class chess : public Fl_Box { // a box with a chess-like pattern below its image
public:
chess(int x, int y, int w, int h) : Fl_Box(FL_FLAT_BOX,x,y,w,h,0) {
Expand Down Expand Up @@ -95,8 +98,11 @@ class clipboard_viewer : public Fl_Tabs { // use tabs to display as appropriate
#endif
Fl_Shared_Image *oldim = (Fl_Shared_Image*)image_box->image();
if (oldim) oldim->release();
float scale = fl_graphics_driver->scale();
Fl_Shared_Image *shared = Fl_Shared_Image::get(im);
shared->scale(image_box->w(), image_box->h());
int width = fl_min(image_box->w(), im->w()/scale);
int height = fl_min(image_box->h(), im->h()/scale);
shared->scale(width, height);
image_box->image(shared); // show the scaled image
image_size->copy_label(title);
value(image_box->parent());
Expand Down
33 changes: 22 additions & 11 deletions src/Fl_win32.cxx
Expand Up @@ -65,6 +65,7 @@ void fl_cleanup_dc_list(void);
#include <FL/Fl_Tooltip.H>
#include <FL/Fl_Paged_Device.H>
#include <FL/Fl_Shared_Image.H>
#include <FL/Fl_Image_Surface.H>
#include "flstring.h"
#include "drivers/GDI/Fl_Font.H"
#include <stdio.h>
Expand Down Expand Up @@ -755,6 +756,7 @@ void Fl_WinAPI_System_Driver::paste(Fl_Widget &receiver, int clipboard, const ch
}
else if (strcmp(type, Fl::clipboard_image) == 0) { // we want an image from clipboard
uchar *rgb = NULL;
Fl_RGB_Image *image = NULL;
int width = 0, height = 0, depth = 0;
if ( (h = GetClipboardData(CF_DIB)) ) { // if there's a DIB in clipboard
LPBITMAPINFO lpBI = (LPBITMAPINFO)GlobalLock(h) ;
Expand Down Expand Up @@ -806,22 +808,31 @@ void Fl_WinAPI_System_Driver::paste(Fl_Widget &receiver, int clipboard, const ch
int hdots = GetDeviceCaps(hdc, HORZRES);
ReleaseDC(NULL, hdc);
float factor = (100.f * hmm) / hdots;
float scaling = ((Fl_WinAPI_Screen_Driver*)Fl::screen_driver())->DWM_scaling_factor(receiver.top_window()->driver()->screen_num());
width = int(width * scaling / factor); // convert to screen pixel unit
#ifdef FLTK_HIDPI_SUPPORT
float scaling = Fl::screen_driver()->scale( receiver.top_window()->driver()->screen_num() );
width = int(width / (scaling * factor)); // convert to screen pixel unit
height = int(height / (scaling * factor));
#else
float scaling = ((Fl_WinAPI_Screen_Driver*)Fl::screen_driver())->DWM_scaling_factor(0);
width = int(width * scaling / factor); // convert to screen pixel unit
height = int(height * scaling / factor);
scaling = 1;
#endif
RECT rect = {0, 0, width, height};
Fl_Offscreen off = fl_create_offscreen(width, height);
fl_begin_offscreen(off);
Fl_Image_Surface *surf = new Fl_Image_Surface(width, height, 1);
Fl_Surface_Device::push_current(surf);
fl_color(FL_WHITE); fl_rectf(0,0,width, height); // draw white background
rect.right *= scaling; rect.bottom *= scaling; // apply scaling to the metafile draw operation
PlayEnhMetaFile((HDC)fl_graphics_driver->gc(), (HENHMETAFILE)h, &rect); // draw metafile to offscreen buffer
rgb = fl_read_image(NULL, 0, 0, width, height); // read pixels from offscreen buffer
depth = 3;
fl_end_offscreen();
fl_delete_offscreen(off);
image = surf->image();
Fl_Surface_Device::pop_current();
delete surf;
}
if (rgb) {
Fl_RGB_Image *image = new Fl_RGB_Image(rgb, width, height, depth); // create new image from pixel data
image->alloc_array = 1;
if (rgb || image) {
if (!image) {
Fl_RGB_Image *image = new Fl_RGB_Image(rgb, width, height, depth); // create new image from pixel data
image->alloc_array = 1;
}
Fl::e_clipboard_data = image;
Fl::e_clipboard_type = Fl::clipboard_image; // indicates that the paste event is for image data
int done = receiver.handle(FL_PASTE); // send FL_PASTE event to widget
Expand Down
4 changes: 2 additions & 2 deletions src/drivers/GDI/Fl_GDI_Copy_Surface_Driver.cxx
Expand Up @@ -62,10 +62,10 @@ Fl_GDI_Copy_Surface_Driver::Fl_GDI_Copy_Surface_Driver(int w, int h) : Fl_Copy_S
// Global display scaling factor: 1, 1.25, 1.5, 1.75, etc...
#ifdef FLTK_HIDPI_SUPPORT
float scaling = Fl_Graphics_Driver::default_driver().scale();
driver()->scale(scaling);
#else
float scaling = ((Fl_WinAPI_Screen_Driver*)Fl::screen_driver())->DWM_scaling_factor(0);
float scaling = 1/((Fl_WinAPI_Screen_Driver*)Fl::screen_driver())->DWM_scaling_factor(0);
#endif
driver()->scale(scaling);
RECT rect; rect.left = 0; rect.top = 0; rect.right = (LONG)((w*scaling) * factorw); rect.bottom = (LONG)((h*scaling) * factorh);
gc = CreateEnhMetaFile (NULL, NULL, &rect, NULL);
if (gc != NULL) {
Expand Down

0 comments on commit 7847c2d

Please sign in to comment.