Skip to content

Commit

Permalink
8321151: JDK-8294427 breaks Windows L&F on all older Windows versions
Browse files Browse the repository at this point in the history
Backport-of: f695ca588453265d6ad791c6a396197e8a53ba39
  • Loading branch information
mrserb committed Feb 26, 2024
1 parent 93c5f7c commit a60a5c4
Showing 1 changed file with 52 additions and 11 deletions.
63 changes: 52 additions & 11 deletions src/java.desktop/windows/native/libawt/windows/ThemeReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ typedef HRESULT(__stdcall *PFNCLOSETHEMEDATA)(HTHEME hTheme);
typedef HRESULT(__stdcall *PFNDRAWTHEMEBACKGROUND)(HTHEME hTheme, HDC hdc,
int iPartId, int iStateId, const RECT *pRect, const RECT *pClipRect);

typedef HTHEME(__stdcall *PFNOPENTHEMEDATA)(HWND hwnd, LPCWSTR pszClassList);
typedef HTHEME(__stdcall *PFNOPENTHEMEDATAFORDPI)(HWND hwnd, LPCWSTR pszClassList, UINT dpi);

typedef HRESULT (__stdcall *PFNDRAWTHEMETEXT)(HTHEME hTheme, HDC hdc,
Expand Down Expand Up @@ -134,6 +135,7 @@ typedef HRESULT (__stdcall *PFNGETTHEMETRANSITIONDURATION)
(HTHEME hTheme, int iPartId, int iStateIdFrom, int iStateIdTo,
int iPropId, DWORD *pdwDuration);

static PFNOPENTHEMEDATA OpenThemeDataFunc = NULL;
static PFNOPENTHEMEDATAFORDPI OpenThemeDataForDpiFunc = NULL;
static PFNDRAWTHEMEBACKGROUND DrawThemeBackground = NULL;
static PFNCLOSETHEMEDATA CloseThemeData = NULL;
Expand All @@ -154,13 +156,17 @@ static PFNISTHEMEBACKGROUNDPARTIALLYTRANSPARENT
//this function might not exist on Windows XP
static PFNGETTHEMETRANSITIONDURATION GetThemeTransitionDuration = NULL;

constexpr unsigned int defaultDPI = 96;

BOOL InitThemes() {

static BOOL InitThemes() {
static HMODULE hModThemes = NULL;
hModThemes = JDK_LoadSystemLibrary("UXTHEME.DLL");
DTRACE_PRINTLN1("InitThemes hModThemes = %x\n", hModThemes);
if(hModThemes) {
DTRACE_PRINTLN("Loaded UxTheme.dll\n");
OpenThemeDataFunc = (PFNOPENTHEMEDATA)GetProcAddress(hModThemes,
"OpenThemeData");
OpenThemeDataForDpiFunc = (PFNOPENTHEMEDATAFORDPI)GetProcAddress(
hModThemes, "OpenThemeDataForDpi");
DrawThemeBackground = (PFNDRAWTHEMEBACKGROUND)GetProcAddress(
Expand Down Expand Up @@ -198,7 +204,7 @@ BOOL InitThemes() {
(PFNGETTHEMETRANSITIONDURATION)GetProcAddress(hModThemes,
"GetThemeTransitionDuration");

if(OpenThemeDataForDpiFunc
if((OpenThemeDataForDpiFunc || OpenThemeDataFunc)
&& DrawThemeBackground
&& CloseThemeData
&& DrawThemeText
Expand All @@ -218,10 +224,12 @@ BOOL InitThemes() {
DTRACE_PRINTLN("Loaded function pointers.\n");
// We need to make sure we can load the Theme.
// Use the default DPI value of 96 on windows.
constexpr unsigned int defaultDPI = 96;
HTHEME hTheme = OpenThemeDataForDpiFunc (
AwtToolkit::GetInstance().GetHWnd(),
L"Button", defaultDPI);
HTHEME hTheme = OpenThemeDataForDpiFunc
? OpenThemeDataForDpiFunc(AwtToolkit::GetInstance().GetHWnd(),
L"Button", defaultDPI)
: OpenThemeDataFunc(AwtToolkit::GetInstance().GetHWnd(),
L"Button");

if(hTheme) {
DTRACE_PRINTLN("Loaded Theme data.\n");
CloseThemeData(hTheme);
Expand Down Expand Up @@ -285,11 +293,13 @@ JNIEXPORT jlong JNICALL Java_sun_awt_windows_ThemeReader_openTheme
JNU_ThrowOutOfMemoryError(env, 0);
return 0;
}

// We need to open the Theme on a Window that will stick around.
// The best one for that purpose is the Toolkit window.
HTHEME htheme = OpenThemeDataForDpiFunc(
AwtToolkit::GetInstance().GetHWnd(),
str, dpi);
HTHEME htheme = OpenThemeDataForDpiFunc
? OpenThemeDataForDpiFunc(AwtToolkit::GetInstance().GetHWnd(), str, dpi)
: OpenThemeDataFunc(AwtToolkit::GetInstance().GetHWnd(), str);

JNU_ReleaseStringPlatformChars(env, widget, str);
return (jlong) htheme;
}
Expand Down Expand Up @@ -469,9 +479,14 @@ JNIEXPORT void JNICALL Java_sun_awt_windows_ThemeReader_paintBackground

rect.left = 0;
rect.top = 0;
rect.bottom = rectBottom;
rect.right = rectRight;

if (OpenThemeDataForDpiFunc) {
rect.bottom = rectBottom;
rect.right = rectRight;
} else {
rect.bottom = h;
rect.right = w;
}
ZeroMemory(pSrcBits,(BITS_PER_PIXEL>>3)*w*h);

HRESULT hres = DrawThemeBackground(hTheme, memDC, part, state, &rect, NULL);
Expand All @@ -494,6 +509,28 @@ JNIEXPORT void JNICALL Java_sun_awt_windows_ThemeReader_paintBackground
ReleaseDC(NULL,defaultDC);
}

static void rescale(SIZE *size) {
static int dpiX = -1;
static int dpiY = -1;

if (dpiX == -1 || dpiY == -1) {
HWND hWnd = ::GetDesktopWindow();
HDC hDC = ::GetDC(hWnd);
dpiX = ::GetDeviceCaps(hDC, LOGPIXELSX);
dpiY = ::GetDeviceCaps(hDC, LOGPIXELSY);
::ReleaseDC(hWnd, hDC);
}

if (dpiX !=0 && dpiX != defaultDPI) {
float invScaleX = (float) defaultDPI / dpiX;
size->cx = (int) round(size->cx * invScaleX);
}
if (dpiY != 0 && dpiY != defaultDPI) {
float invScaleY = (float) defaultDPI / dpiY;
size->cy = (int) round(size->cy * invScaleY);
}
}

jobject newInsets(JNIEnv *env, jint top, jint left, jint bottom, jint right) {
if (env->EnsureLocalCapacity(2) < 0) {
return NULL;
Expand Down Expand Up @@ -785,6 +822,10 @@ JNIEXPORT jobject JNICALL Java_sun_awt_windows_ThemeReader_getPartSize
CHECK_NULL_RETURN(dimMID, NULL);
}

if (!OpenThemeDataForDpiFunc) {
rescale(&size);
}

jobject dimObj = env->NewObject(dimClassID, dimMID, size.cx, size.cy);
if (safe_ExceptionOccurred(env)) {
env->ExceptionDescribe();
Expand Down

1 comment on commit a60a5c4

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.