< prev index next >

src/java.desktop/windows/native/libawt/windows/awt_DesktopProperties.cpp

Print this page

        

@@ -33,10 +33,12 @@
 #include "awtmsg.h"
 #include "zmouse.h"
 #include <shellapi.h>
 #include <shlobj.h>
 
+#include "math.h"
+
 // WDesktopProperties fields
 jfieldID AwtDesktopProperties::pDataID = 0;
 jmethodID AwtDesktopProperties::setBooleanPropertyID = 0;
 jmethodID AwtDesktopProperties::setIntegerPropertyID = 0;
 jmethodID AwtDesktopProperties::setStringPropertyID = 0;

@@ -77,19 +79,31 @@
     if (IS_WINXP) {
         GetXPStyleProperties();
     }
 }
 
+float getScale() {
+    HWND hWnd = ::GetDesktopWindow();
+    HDC hDC = ::GetDC(hWnd);
+    float dpi = ::GetDeviceCaps(hDC, LOGPIXELSX);
+    ::ReleaseDC(hWnd, hDC);
+    return dpi == 0.0f ? 1.0f : dpi / 96;
+}
+
+int rescale(int x, float scale){
+    return scale == 1.0f ? x : (int)ceil(x / scale);
+}
+
 void AwtDesktopProperties::GetSystemProperties() {
     HDC dc = CreateDC(TEXT("DISPLAY"), NULL, NULL, NULL);
 
     if (dc != NULL) {
         try {
             SetFontProperty(dc, ANSI_FIXED_FONT, TEXT("win.ansiFixed.font"));
             SetFontProperty(dc, ANSI_VAR_FONT, TEXT("win.ansiVar.font"));
             SetFontProperty(dc, DEVICE_DEFAULT_FONT, TEXT("win.deviceDefault.font"));
-            SetFontProperty(dc, DEFAULT_GUI_FONT, TEXT("win.defaultGUI.font"));
+            SetFontProperty(dc, DEFAULT_GUI_FONT, TEXT("win.defaultGUI.font"), getScale());
             SetFontProperty(dc, OEM_FIXED_FONT, TEXT("win.oemFixed.font"));
             SetFontProperty(dc, SYSTEM_FONT, TEXT("win.system.font"));
             SetFontProperty(dc, SYSTEM_FIXED_FONT, TEXT("win.systemFixed.font"));
         }
         catch (std::bad_alloc&) {

@@ -264,35 +278,37 @@
     } else {
         ncmetrics.cbSize = sizeof(ncmetrics);
     }
     VERIFY( SystemParametersInfo(SPI_GETNONCLIENTMETRICS, ncmetrics.cbSize, &ncmetrics, FALSE) );
 
-    SetFontProperty( TEXT("win.frame.captionFont"), ncmetrics.lfCaptionFont );
-    SetIntegerProperty( TEXT("win.frame.captionHeight"), ncmetrics.iCaptionHeight );
-    SetIntegerProperty( TEXT("win.frame.captionButtonWidth"), ncmetrics.iCaptionWidth );
-    SetIntegerProperty( TEXT("win.frame.captionButtonHeight"), ncmetrics.iCaptionHeight );
-    SetFontProperty( TEXT("win.frame.smallCaptionFont"), ncmetrics.lfSmCaptionFont );
-    SetIntegerProperty( TEXT("win.frame.smallCaptionHeight"), ncmetrics.iSmCaptionHeight );
-    SetIntegerProperty( TEXT("win.frame.smallCaptionButtonWidth"), ncmetrics.iSmCaptionWidth );
-    SetIntegerProperty( TEXT("win.frame.smallCaptionButtonHeight"), ncmetrics.iSmCaptionHeight );
-    SetIntegerProperty( TEXT("win.frame.sizingBorderWidth"), ncmetrics.iBorderWidth );
+    float scale = getScale();
+
+    SetFontProperty(TEXT("win.frame.captionFont"), ncmetrics.lfCaptionFont, scale);
+    SetIntegerProperty(TEXT("win.frame.captionHeight"), rescale(ncmetrics.iCaptionHeight, scale));
+    SetIntegerProperty(TEXT("win.frame.captionButtonWidth"), rescale(ncmetrics.iCaptionWidth, scale));
+    SetIntegerProperty(TEXT("win.frame.captionButtonHeight"), rescale(ncmetrics.iCaptionHeight, scale));
+    SetFontProperty(TEXT("win.frame.smallCaptionFont"), ncmetrics.lfSmCaptionFont, scale);
+    SetIntegerProperty(TEXT("win.frame.smallCaptionHeight"), rescale(ncmetrics.iSmCaptionHeight, scale));
+    SetIntegerProperty(TEXT("win.frame.smallCaptionButtonWidth"), rescale(ncmetrics.iSmCaptionWidth, scale));
+    SetIntegerProperty(TEXT("win.frame.smallCaptionButtonHeight"), rescale(ncmetrics.iSmCaptionHeight, scale));
+    SetIntegerProperty(TEXT("win.frame.sizingBorderWidth"), rescale(ncmetrics.iBorderWidth, 1.0f / scale));
 
     // menu properties
-    SetFontProperty( TEXT("win.menu.font"), ncmetrics.lfMenuFont );
-    SetIntegerProperty( TEXT("win.menu.height"), ncmetrics.iMenuHeight );
-    SetIntegerProperty( TEXT("win.menu.buttonWidth"), ncmetrics.iMenuWidth );
+    SetFontProperty(TEXT("win.menu.font"), ncmetrics.lfMenuFont, scale);
+    SetIntegerProperty(TEXT("win.menu.height"), rescale(ncmetrics.iMenuHeight, scale));
+    SetIntegerProperty(TEXT("win.menu.buttonWidth"), rescale(ncmetrics.iMenuWidth, scale));
 
     // scrollbar properties
-    SetIntegerProperty( TEXT("win.scrollbar.width"), ncmetrics.iScrollWidth );
-    SetIntegerProperty( TEXT("win.scrollbar.height"), ncmetrics.iScrollHeight );
+    SetIntegerProperty(TEXT("win.scrollbar.width"), rescale(ncmetrics.iScrollWidth, scale));
+    SetIntegerProperty(TEXT("win.scrollbar.height"), rescale(ncmetrics.iScrollHeight, scale));
 
     // status bar and tooltip properties
-    SetFontProperty( TEXT("win.status.font"), ncmetrics.lfStatusFont );
-    SetFontProperty( TEXT("win.tooltip.font"), ncmetrics.lfStatusFont );
+    SetFontProperty(TEXT("win.status.font"), ncmetrics.lfStatusFont, scale);
+    SetFontProperty(TEXT("win.tooltip.font"), ncmetrics.lfStatusFont, scale);
 
     // message box properties
-    SetFontProperty( TEXT("win.messagebox.font"), ncmetrics.lfMessageFont );
+    SetFontProperty(TEXT("win.messagebox.font"), ncmetrics.lfMessageFont, scale);
 }
 
 void AwtDesktopProperties::GetIconParameters() {
     //
     // icon properties

@@ -300,14 +316,15 @@
     ICONMETRICS iconmetrics;
 
     iconmetrics.cbSize = sizeof(iconmetrics);
     VERIFY( SystemParametersInfo(SPI_GETICONMETRICS, iconmetrics.cbSize, &iconmetrics, FALSE) );
 
-    SetIntegerProperty(TEXT("win.icon.hspacing"), iconmetrics.iHorzSpacing);
-    SetIntegerProperty(TEXT("win.icon.vspacing"), iconmetrics.iVertSpacing);
+    float scale = getScale();
+    SetIntegerProperty(TEXT("win.icon.hspacing"), rescale(iconmetrics.iHorzSpacing, scale));
+    SetIntegerProperty(TEXT("win.icon.vspacing"), rescale(iconmetrics.iVertSpacing, scale));
     SetBooleanProperty(TEXT("win.icon.titleWrappingOn"), iconmetrics.iTitleWrap != 0);
-    SetFontProperty(TEXT("win.icon.font"), iconmetrics.lfFont);
+    SetFontProperty(TEXT("win.icon.font"), iconmetrics.lfFont, scale);
 }
 /*
  Windows settings for these are also in the registry
  They exist as system wide HKLM: HKEY_LOCAL_MACHINE and
  HKCU: HKEY_CURRENT_USER.

@@ -716,10 +733,11 @@
     GetEnv()->DeleteLocalRef(jValue);
     GetEnv()->DeleteLocalRef(key);
 }
 
 void AwtDesktopProperties::SetIntegerProperty(LPCTSTR propName, int value) {
+
     jstring key = JNU_NewStringPlatform(GetEnv(), propName);
     if (key == NULL) {
         throw std::bad_alloc();
     }
     GetEnv()->CallVoidMethod(self,

@@ -751,10 +769,15 @@
     GetEnv()->DeleteLocalRef(key);
 }
 
 void AwtDesktopProperties::SetFontProperty(HDC dc, int fontID,
                                            LPCTSTR propName) {
+    AwtDesktopProperties::SetFontProperty(dc, fontID, propName, 1.0f);
+}
+
+void AwtDesktopProperties::SetFontProperty(HDC dc, int fontID,
+        LPCTSTR propName, float scale) {
     HGDIOBJ font = GetStockObject(fontID);
     if (font != NULL && SelectObject(dc, font) != NULL) {
         int length = GetTextFace(dc, 0, NULL);
 
         if (length > 0) {

@@ -787,12 +810,12 @@
                     if (fontName == NULL) {
                         delete[] face;
                         throw std::bad_alloc();
                     }
 
-                    jint pointSize = metrics.tmHeight -
-                                     metrics.tmInternalLeading;
+                    jint pointSize = rescale(metrics.tmHeight -
+                                     metrics.tmInternalLeading, scale);
                     jint style = java_awt_Font_PLAIN;
 
                     if (metrics.tmWeight >= FW_BOLD) {
                         style =  java_awt_Font_BOLD;
                     }

@@ -817,10 +840,15 @@
         }
     }
 }
 
 void AwtDesktopProperties::SetFontProperty(LPCTSTR propName, const LOGFONT & font) {
+    AwtDesktopProperties::SetFontProperty(propName, font, 1.0f);
+}
+
+void AwtDesktopProperties::SetFontProperty(LPCTSTR propName, const LOGFONT & font,
+    float scale) {
     jstring fontName;
     jint pointSize;
     jint style;
 
     fontName = JNU_NewStringPlatform(GetEnv(), font.lfFaceName);

@@ -834,11 +862,11 @@
     hdc = GetDC(NULL);
     pointSize = (-font.lfHeight)*72/pixelsPerInch;
     ReleaseDC(NULL, hdc);
 #endif
     // Java uses point sizes, but assumes 1 pixel = 1 point
-    pointSize = -font.lfHeight;
+    pointSize = rescale(-font.lfHeight, scale);
 
     // convert Windows font style to Java style
     style = java_awt_Font_PLAIN;
     DTRACE_PRINTLN1("weight=%d", font.lfWeight);
     if ( font.lfWeight >= FW_BOLD ) {
< prev index next >