Skip to content

Commit

Permalink
Advancing HiDPI support for the WIN32 platform - still incomplete.
Browse files Browse the repository at this point in the history
It's still necessary to compile with -DFLTK_HIDPI_SUPPORT
to activate the new HiDPI support.
Default builds get the same HiDPI support as in FLTK 1.3

git-svn-id: file:///fltk/svn/fltk/branches/branch-1.4@12265 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
  • Loading branch information
Manolo Gouy authored and Manolo Gouy committed Jun 18, 2017
1 parent 2cda5a4 commit 701fa00
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 75 deletions.
103 changes: 68 additions & 35 deletions src/Fl_win32.cxx
Expand Up @@ -505,10 +505,8 @@ void Fl_WinAPI_Screen_Driver::open_display_platform() {
HMODULE hMod = LoadLibrary("Shcore.DLL");
if (hMod) {
SetProcessDpiAwareness_type fl_SetProcessDpiAwareness = (SetProcessDpiAwareness_type)GetProcAddress(hMod, "SetProcessDpiAwareness");
HRESULT r = 0;
const int PROCESS_PER_MONITOR_DPI_AWARE = 2;
if (fl_SetProcessDpiAwareness) r = fl_SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE);
//fprintf(LOG,"SetProcessDpiAwareness=%p result=%d\n",fl_SetProcessDpiAwareness,r);fflush(LOG);
if (fl_SetProcessDpiAwareness) fl_SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE);
}
#endif // FLTK_HIDPI_SUPPORT
OleInitialize(0L);
Expand Down Expand Up @@ -583,12 +581,19 @@ void Fl_WinAPI_Screen_Driver::disable_im() {

////////////////////////////////////////////////////////////////

int Fl_WinAPI_Screen_Driver::get_mouse(int &x, int &y) {
int Fl_WinAPI_Screen_Driver::get_mouse_unscaled(int &mx, int &my) {
POINT p;
GetCursorPos(&p);
x = p.x;
y = p.y;
return screen_num(x, y);
mx = p.x; my = p.y;
return screen_num_unscaled(mx, my);
}

int Fl_WinAPI_Screen_Driver::get_mouse(int &x, int &y) {
int n = get_mouse_unscaled(x, y);
float s = scale(n);
x = x/s;
y = y/s;
return n;
}

////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -801,7 +806,7 @@ 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::desktop_scaling_factor();
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
height = int(height * scaling / factor);
RECT rect = {0, 0, width, height};
Expand Down Expand Up @@ -1153,7 +1158,7 @@ static LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lPar
}

// convert i->region in FLTK units to R2 in drawing units
R2 = Fl_GDI_Graphics_Driver::scale_region(i->region, scale, false);
R2 = Fl_GDI_Graphics_Driver::scale_region(i->region, scale, NULL, false);

if (R2) {
// Also tell WIN32 that we are drawing someplace else as well...
Expand All @@ -1168,7 +1173,7 @@ static LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lPar
}

// convert R2 in drawing units to i->region in FLTK units
i->region = Fl_GDI_Graphics_Driver::scale_region(R2, 1/scale, false, true);
i->region = Fl_GDI_Graphics_Driver::scale_region(R2, 1/scale, NULL, false /*, true*/);

window->clear_damage((uchar)(window->damage()|FL_DAMAGE_EXPOSE));
// These next two statements should not be here, so that all update
Expand Down Expand Up @@ -1524,8 +1529,20 @@ static int fake_X_wm_style(const Fl_Window* w,int &X,int &Y, int &bt,int &bx, in
int ret = bx = by = bt = 0;

int fallback = 1;
float s = Fl::screen_driver()->scale(((Fl_Window*)w)->driver()->screen_num());
if (!w->parent()) {
if (fl_xid(w) || style) {
if (fl_xid(w)) {
Fl_WinAPI_Window_Driver *dr = (Fl_WinAPI_Window_Driver*)w->driver();
dr->border_width_title_bar_height(bx, by, bt);
xoff = bx;
yoff = by + bt;
dx = 2*bx;
dy = 2*by + bt;
X = w->x()*s - bx;
Y = w->y()*s - bt - by;
W = w->w()*s + dx;
H = w->h()*s + dy;
} else if (fl_xid(w) || style) {
// The block below calculates the window borders by requesting the
// required decorated window rectangle for a desired client rectangle.
// If any part of the function above fails, we will drop to a
Expand All @@ -1539,24 +1556,24 @@ static int fake_X_wm_style(const Fl_Window* w,int &X,int &Y, int &bt,int &bx, in
}

RECT r;
r.left = w->x();
r.top = w->y();
r.right = w->x()+w->w();
r.bottom = w->y()+w->h();
r.left = w->x() * s;
r.top = w->y() * s;
r.right = (w->x()+w->w())*s;
r.bottom = (w->y()+w->h())*s;
// get the decoration rectangle for the desired client rectangle
BOOL ok = AdjustWindowRectEx(&r, style, FALSE, styleEx);
if (ok) {
X = r.left;
Y = r.top;
W = r.right - r.left;
H = r.bottom - r.top;
bx = w->x() - r.left;
by = r.bottom - w->y() - w->h(); // height of the bottom frame
bt = w->y() - r.top - by; // height of top caption bar
bx = w->x() *s - r.left;
by = r.bottom - (w->y() + w->h())*s; // height of the bottom frame
bt = w->y() * s - r.top - by; // height of top caption bar
xoff = bx;
yoff = by + bt;
dx = W - w->w();
dy = H - w->h();
dx = W - w->w() * s;
dy = H - w->h() * s;
if (w_size_range_set && (w_maxw != w_minw || w_maxh != w_minh))
ret = 2;
else
Expand Down Expand Up @@ -1598,16 +1615,17 @@ static int fake_X_wm_style(const Fl_Window* w,int &X,int &Y, int &bt,int &bx, in
//Find screen that contains most of the window
//FIXME: this ought to be the "work area" instead of the entire screen !
int scr_x = 0, scr_y = 0, scr_w = 0, scr_h = 0;
Fl::screen_xywh(scr_x, scr_y, scr_w, scr_h, X, Y, W, H);
Fl::screen_xywh(scr_x, scr_y, scr_w, scr_h, X/s, Y/s, W/s, H/s);
scr_x *= s; scr_y *= s; scr_w *= s; scr_h *= s;
//Make border's lower right corner visible
if (scr_x+scr_w < X+W) X = scr_x+scr_w - W;
if (scr_y+scr_h < Y+H) Y = scr_y+scr_h - H;
//Make border's upper left corner visible
if (X<scr_x) X = scr_x;
if (Y<scr_y) Y = scr_y;
//Make client area's lower right corner visible
if (scr_x+scr_w < X+dx+ w->w()) X = scr_x+scr_w - w->w() - dx;
if (scr_y+scr_h < Y+dy+ w->h()) Y = scr_y+scr_h - w->h() - dy;
if (scr_x+scr_w < X+dx+ w->w()) X = scr_x+scr_w - w->w()*s - dx;
if (scr_y+scr_h < Y+dy+ w->h()) Y = scr_y+scr_h - w->h()*s - dy;
//Make client area's upper left corner visible
if (X+xoff < scr_x) X = scr_x-xoff;
if (Y+yoff < scr_y) Y = scr_y-yoff;
Expand Down Expand Up @@ -1659,6 +1677,8 @@ void Fl_WinAPI_Window_Driver::resize(int X,int Y,int W,int H) {
if (!pWindow->resizable()) pWindow->size_range(w(), h(), w(), h());
int dummy_x, dummy_y, bt, bx, by;
//Ignore window managing when resizing, so that windows (and more
float s = Fl::screen_driver()->scale(screen_num());
X *= s; Y *= s; W *= s; H *= s;
//specifically menus) can be moved offscreen.
if (fake_X_wm(dummy_x, dummy_y, bt, bx, by)) {
X -= bx;
Expand All @@ -1670,8 +1690,8 @@ void Fl_WinAPI_Window_Driver::resize(int X,int Y,int W,int H) {
// will cause continouly new redraw events.
if (W<=0) W = 1;
if (H<=0) H = 1;
float s = Fl::screen_driver()->scale(0);
SetWindowPos(fl_xid(pWindow), 0, X*s, Y*s, W*s, H*s, flags);
SetWindowPos(fl_xid(pWindow), 0, X, Y, W, H, flags);
//fprintf(LOG,"parent=%p bt=%d X=%d W=%d H=%d s=%f\n",pWindow->parent(),bt,int(X),int(W),int(H),s);fflush(LOG);
}
}

Expand Down Expand Up @@ -1782,7 +1802,7 @@ Fl_X* Fl_WinAPI_Window_Driver::makeWindow() {
DWORD styleEx = WS_EX_LEFT;

float s = Fl::screen_driver()->scale(0);
int xp = w->x() * s;
int xp = w->x() * s; // these are in graphical units
int yp = w->y() * s;
int wp = w->w() * s;
int hp = w->h() * s;
Expand Down Expand Up @@ -1834,7 +1854,7 @@ Fl_X* Fl_WinAPI_Window_Driver::makeWindow() {
break;
}

int xwm = xp , ywm = yp , bt, bx, by;
int xwm = xp , ywm = yp , bt, bx, by; // these are in graphical units
fake_X_wm_style(w, xwm, ywm, bt, bx, by, style, styleEx, maxw(), minw(), maxh(), minh(), size_range_set());
if (by+bt) {
wp += 2*bx;
Expand Down Expand Up @@ -2456,29 +2476,42 @@ void Fl_WinAPI_Window_Driver::capture_titlebar_and_borders(Fl_Shared_Image*& top
top = left = bottom = right = NULL;
if (!shown() || parent() || !border() || !visible()) return;
int wsides, hbottom, bt;
float scaling = 1;
RECT r = border_width_title_bar_height(wsides, hbottom, bt, &scaling);
float scaling = Fl::screen_driver()->scale(screen_num());
RECT r = border_width_title_bar_height(wsides, hbottom, bt);
int htop = bt + hbottom;
Window save_win = fl_window;
Fl_Surface_Device::push_current( Fl_Display_Device::display_device() );
pWindow->show();
Fl::check();
void* save_gc = fl_graphics_driver->gc();
fl_graphics_driver->gc(GetDC(NULL));
int ww = w() + 2 * wsides;
int ww = w()*scaling + 2 * wsides;
wsides /= scaling; if (wsides < 1) wsides = 1;
ww /= scaling; if (wsides <= 1) ww = w() + 2*wsides;
#ifdef FLTK_HIDPI_SUPPORT
float DWMscaling = scaling;
#else
float DWMscaling = ((Fl_WinAPI_Screen_Driver*)Fl::screen_driver())->DWM_scaling_factor(0);
#endif
// capture the 4 window sides from screen
Fl_WinAPI_Screen_Driver *dr = (Fl_WinAPI_Screen_Driver*)Fl::screen_driver();
if (htop) {
r_top = Fl::screen_driver()->read_win_rectangle(NULL, r.left, r.top, r.right - r.left + 1, htop, 0);
r_top = dr->read_win_rectangle_unscaled(NULL, r.left, r.top, r.right - r.left, htop, 0);
top = Fl_Shared_Image::get(r_top);
if (scaling >= 1.1) top->scale(ww, htop/scaling, 0);
if (DWMscaling >= 1.1) top->scale(ww, htop/DWMscaling, 0, 1);
}
if (wsides) {
r_left = Fl::screen_driver()->read_win_rectangle(NULL, r.left, r.top + htop, wsides, h(), 0);
r_left = dr->read_win_rectangle_unscaled(NULL, r.left, r.top + htop, wsides, h()*scaling, 0);
left = Fl_Shared_Image::get(r_left);
r_right = Fl::screen_driver()->read_win_rectangle(NULL, r.right - wsides, r.top + htop, wsides, h(), 0);
r_right = dr->read_win_rectangle_unscaled(NULL, r.right - wsides, r.top + htop, wsides, h()*scaling, 0);
right = Fl_Shared_Image::get(r_right);
r_bottom = Fl::screen_driver()->read_win_rectangle(NULL, r.left, r.bottom-hbottom, ww, hbottom, 0);
r_bottom = dr->read_win_rectangle_unscaled(NULL, r.left, r.bottom-hbottom, ww, hbottom, 0);
bottom = Fl_Shared_Image::get(r_bottom);
if (scaling >= 1.1) {
left->scale(wsides, h(), 0, 1);
right->scale(wsides, h(), 0, 1);
bottom->scale(ww, hbottom, 0, 1);
}
}
ReleaseDC(NULL, (HDC)fl_graphics_driver->gc());
fl_window = save_win;
Expand Down
9 changes: 7 additions & 2 deletions src/drivers/GDI/Fl_GDI_Copy_Surface_Driver.cxx
Expand Up @@ -60,8 +60,13 @@ Fl_GDI_Copy_Surface_Driver::Fl_GDI_Copy_Surface_Driver(int w, int h) : Fl_Copy_S
float factorw = (100.f * hmm) / hdots;
float factorh = (100.f * vmm) / vdots;
// Global display scaling factor: 1, 1.25, 1.5, 1.75, etc...
float scaling = Fl_WinAPI_Screen_Driver::desktop_scaling_factor();

#ifdef FLTK_HIDPI_SUPPORT
float scaling = Fl_Graphics_Driver::default_driver().scale();
factorw *= scaling;
factorh *= scaling;
#else
float scaling = ((Fl_WinAPI_Screen_Driver*)Fl::screen_driver())->DWM_scaling_factor(0);
#endif
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
2 changes: 1 addition & 1 deletion src/drivers/GDI/Fl_GDI_Graphics_Driver.H
Expand Up @@ -89,7 +89,7 @@ public:
void XDestroyRegion(Fl_Region r);
void translate_all(int x, int y);
void untranslate_all(void);
static HRGN scale_region(HRGN r, float f, bool keep, bool inflate=false);
static HRGN scale_region(HRGN r, float f, Fl_GDI_Graphics_Driver *dr, bool keep/*, bool inflate=false*/);
virtual void scale(float f);
virtual float scale();
protected:
Expand Down
32 changes: 26 additions & 6 deletions src/drivers/GDI/Fl_GDI_Graphics_Driver.cxx
Expand Up @@ -146,7 +146,7 @@ void Fl_GDI_Graphics_Driver::translate_all(int x, int y) {
depth = stack_height - 1;
}
GetWindowOrgEx((HDC)gc(), origins+depth);
SetWindowOrgEx((HDC)gc(), origins[depth].x - x, origins[depth].y - y, NULL);
SetWindowOrgEx((HDC)gc(), origins[depth].x - x*scale_, origins[depth].y - y*scale_, NULL);
depth++;
}

Expand Down Expand Up @@ -243,32 +243,52 @@ float Fl_GDI_Graphics_Driver::scale() {

/* Rescale region r with factor f and returns the scaled region.
The input region is deleted if keep is false.
The input region is inflated by 1 unit before rescaling if inflate is true.
//The input region is inflated by 1 unit before rescaling if inflate is true.
Region r is returned unchanged if r is null or f is 1.
*/
HRGN Fl_GDI_Graphics_Driver::scale_region(HRGN r, float f, bool keep, bool inflate) {
HRGN Fl_GDI_Graphics_Driver::scale_region(HRGN r, float f, Fl_GDI_Graphics_Driver *dr, bool keep/*, bool inflate*/) {
if (r && f != 1) {
DWORD size = GetRegionData(r, 0, NULL);
RGNDATA *pdata = (RGNDATA*)malloc(size);
GetRegionData(r, size, pdata);
if (!keep) DeleteObject(r);
if (inflate) {
/*if (inflate) { // seems no longer useful
RECT *rects = (RECT*)&(pdata->Buffer);
for (DWORD i = 0; i < pdata->rdh.nCount; i++) {
InflateRect(rects+i, 1, 1);
}
}*/
POINT pt = {0, 0};
if (dr && dr->depth >= 1) { // account for both scaling and translation
GetWindowOrgEx((HDC)dr->gc(), &pt);
pt.x *= (f - 1);
pt.y *= (f - 1);
}
XFORM xform = {f, 0, 0, f, 0, 0};
XFORM xform = {f, 0, 0, f, (FLOAT)pt.x , (FLOAT)pt.y};
r = ExtCreateRegion(&xform, size, pdata);
free(pdata);

if (dr && int(f) != f && f > 1) { // needed for clean checkers demo
DWORD size = GetRegionData(r, 0, NULL);
RGNDATA *pdata = (RGNDATA*)malloc(size);
GetRegionData(r, size, pdata);
DeleteObject(r);
RECT *rects = (RECT*)&(pdata->Buffer);
for (DWORD i = 0; i < pdata->rdh.nCount; i++) {
InflateRect(rects+i, 1, 1);
}
r = ExtCreateRegion(NULL, size, pdata);
free(pdata);
}

}
return r;
}


Fl_Region Fl_GDI_Graphics_Driver::scale_clip(float f) {
HRGN r = rstack[rstackptr];
HRGN r2 = scale_region(r, f, true);
HRGN r2 = scale_region(r, f, this, true);
return (r == r2 ? NULL : (rstack[rstackptr] = r2, r));
}

Expand Down
10 changes: 5 additions & 5 deletions src/drivers/GDI/Fl_GDI_Image_Surface_Driver.cxx
Expand Up @@ -21,6 +21,7 @@

#ifdef FL_CFG_GFX_GDI
#include "Fl_GDI_Graphics_Driver.H"
#include "../WinAPI/Fl_WinAPI_Screen_Driver.H"
#include <FL/Fl_Image_Surface.H>
#include <FL/fl_draw.H>
#include <FL/x.H>
Expand Down Expand Up @@ -55,7 +56,9 @@ Fl_GDI_Image_Surface_Driver::Fl_GDI_Image_Surface_Driver(int w, int h, int high_
w = int(w*d);
h = int(h*d);
}
offscreen = off ? off : CreateCompatibleBitmap( (fl_graphics_driver->gc() ? (HDC)fl_graphics_driver->gc() : fl_GetDC(0) ) , w, h);
HDC gc = (HDC)Fl_Graphics_Driver::default_driver().gc();
offscreen = off ? off : CreateCompatibleBitmap( (gc ? gc : fl_GetDC(0) ) , w, h);
if (!offscreen) offscreen = CreateCompatibleBitmap(fl_GetDC(0), w, h);
driver(new Fl_GDI_Graphics_Driver);
if (d != 1 && high_res) driver()->scale(d);
_sgc = NULL;
Expand Down Expand Up @@ -93,11 +96,8 @@ void Fl_GDI_Image_Surface_Driver::untranslate() {

Fl_RGB_Image* Fl_GDI_Image_Surface_Driver::image()
{
unsigned char *data;
data = fl_read_image(NULL, 0, 0, width, height, 0);
Fl_RGB_Image *image = Fl::screen_driver()->read_win_rectangle(NULL, 0, 0, width, height, 0);
previous->driver()->gc(_sgc);
Fl_RGB_Image *image = new Fl_RGB_Image(data, width, height);
image->alloc_array = 1;
return image;
}

Expand Down
5 changes: 4 additions & 1 deletion src/drivers/WinAPI/Fl_WinAPI_Screen_Driver.H
Expand Up @@ -40,6 +40,8 @@ protected:

static BOOL CALLBACK screen_cb(HMONITOR mon, HDC, LPRECT r, LPARAM);
BOOL screen_cb(HMONITOR mon, HDC, LPRECT r);
int screen_num_unscaled(int x, int y);
int get_mouse_unscaled(int &mx, int &my);

public:
Fl_WinAPI_Screen_Driver() : Fl_Screen_Driver() { scale_ = 1; }
Expand All @@ -53,7 +55,7 @@ public:
virtual int h();
virtual void screen_xywh(int &X, int &Y, int &W, int &H, int n);
virtual void screen_dpi(float &h, float &v, int n=0);
static float desktop_scaling_factor();
float DWM_scaling_factor(int screen_num);
virtual void screen_work_area(int &X, int &Y, int &W, int &H, int n);
// --- audible output
virtual void beep(int type);
Expand All @@ -74,6 +76,7 @@ public:
virtual int dnd(int unused);
virtual int compose(int &del);
virtual Fl_RGB_Image *read_win_rectangle(uchar *p, int X, int Y, int w, int h, int alpha);
Fl_RGB_Image *read_win_rectangle_unscaled(uchar *p, int X, int Y, int w, int h, int alpha);
virtual int get_mouse(int &x, int &y);
virtual void enable_im();
virtual void disable_im();
Expand Down

0 comments on commit 701fa00

Please sign in to comment.