annotate src/windows/native/sun/windows/awt_Win32GraphicsEnv.cpp @ 2859:c595c2730226

6989721: awt native code compiler warnings Reviewed-by: yan, uta
author art
date Thu, 14 Oct 2010 14:07:50 +0400
parents 00cd9dc3c2b5
children a06412e13bf7
rev   line source
duke@0 1 /*
ohair@2362 2 * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved.
duke@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@0 4 *
duke@0 5 * This code is free software; you can redistribute it and/or modify it
duke@0 6 * under the terms of the GNU General Public License version 2 only, as
ohair@2362 7 * published by the Free Software Foundation. Oracle designates this
duke@0 8 * particular file as subject to the "Classpath" exception as provided
ohair@2362 9 * by Oracle in the LICENSE file that accompanied this code.
duke@0 10 *
duke@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@0 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@0 15 * accompanied this code).
duke@0 16 *
duke@0 17 * You should have received a copy of the GNU General Public License version
duke@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@0 20 *
ohair@2362 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@2362 22 * or visit www.oracle.com if you need additional information or have any
ohair@2362 23 * questions.
duke@0 24 */
duke@0 25
duke@0 26 #include <awt.h>
duke@0 27 #include <sun_awt_Win32GraphicsEnvironment.h>
rkennke@1686 28 #include <sun_awt_Win32FontManager.h>
duke@0 29 #include "awt_Canvas.h"
duke@0 30 #include "awt_Win32GraphicsDevice.h"
duke@0 31 #include "Devices.h"
duke@0 32 #include "WindowsFlags.h"
anthony@1066 33 #include "DllUtil.h"
tdv@430 34
tdv@430 35 BOOL DWMIsCompositionEnabled();
duke@0 36
duke@0 37 void initScreens(JNIEnv *env) {
duke@0 38
duke@0 39 if (!Devices::UpdateInstance(env)) {
duke@0 40 JNU_ThrowInternalError(env, "Could not update the devices array.");
duke@0 41 return;
duke@0 42 }
duke@0 43 }
duke@0 44
duke@0 45 /**
duke@0 46 * This function attempts to make a Win32 API call to
duke@0 47 * BOOL SetProcessDPIAware(VOID);
duke@0 48 * which is only present on Windows Vista, and which instructs the
duke@0 49 * Vista Windows Display Manager that this application is High DPI Aware
duke@0 50 * and does not need to be scaled by the WDM and lied about the
duke@0 51 * actual system dpi.
duke@0 52 */
duke@0 53 static void
duke@0 54 SetProcessDPIAwareProperty()
duke@0 55 {
tdv@430 56 typedef BOOL (WINAPI SetProcessDPIAwareFunc)(void);
duke@0 57 static BOOL bAlreadySet = FALSE;
duke@0 58
duke@0 59 // setHighDPIAware is set in WindowsFlags.cpp
duke@0 60 if (!setHighDPIAware || bAlreadySet) {
duke@0 61 return;
duke@0 62 }
duke@0 63
duke@0 64 bAlreadySet = TRUE;
duke@0 65
tdv@430 66 HMODULE hLibUser32Dll = ::LoadLibrary(TEXT("user32.dll"));
duke@0 67
duke@0 68 if (hLibUser32Dll != NULL) {
duke@0 69 SetProcessDPIAwareFunc *lpSetProcessDPIAware =
duke@0 70 (SetProcessDPIAwareFunc*)GetProcAddress(hLibUser32Dll,
duke@0 71 "SetProcessDPIAware");
duke@0 72 if (lpSetProcessDPIAware != NULL) {
duke@0 73 lpSetProcessDPIAware();
duke@0 74 }
duke@0 75 ::FreeLibrary(hLibUser32Dll);
duke@0 76 }
duke@0 77 }
duke@0 78
tdv@430 79 #define DWM_COMP_UNDEFINED (~(TRUE|FALSE))
tdv@430 80 static int dwmIsCompositionEnabled = DWM_COMP_UNDEFINED;
tdv@430 81
tdv@430 82 /**
tdv@430 83 * This function is called from toolkit event handling code when
tdv@430 84 * WM_DWMCOMPOSITIONCHANGED event is received
tdv@430 85 */
tdv@430 86 void DWMResetCompositionEnabled() {
tdv@430 87 dwmIsCompositionEnabled = DWM_COMP_UNDEFINED;
tdv@430 88 (void)DWMIsCompositionEnabled();
tdv@430 89 }
tdv@430 90
tdv@430 91 /**
tdv@430 92 * Returns true if dwm composition is enabled, false if it is not applicable
tdv@430 93 * (if the OS is not Vista) or dwm composition is disabled.
tdv@430 94 */
tdv@430 95 BOOL DWMIsCompositionEnabled() {
tdv@430 96 // cheaper to check than whether it's vista or not
tdv@430 97 if (dwmIsCompositionEnabled != DWM_COMP_UNDEFINED) {
tdv@430 98 return (BOOL)dwmIsCompositionEnabled;
tdv@430 99 }
tdv@430 100
tdv@430 101 if (!IS_WINVISTA) {
tdv@430 102 dwmIsCompositionEnabled = FALSE;
tdv@430 103 return FALSE;
tdv@430 104 }
tdv@430 105
tdv@430 106 BOOL bRes = FALSE;
tdv@430 107
anthony@1066 108 try {
anthony@1066 109 BOOL bEnabled;
anthony@1066 110 HRESULT res = DwmAPI::DwmIsCompositionEnabled(&bEnabled);
anthony@1066 111 if (SUCCEEDED(res)) {
anthony@1066 112 bRes = bEnabled;
anthony@1066 113 J2dTraceLn1(J2D_TRACE_VERBOSE, " composition enabled: %d",bRes);
tdv@430 114 } else {
anthony@1066 115 J2dTraceLn1(J2D_TRACE_ERROR,
anthony@1066 116 "IsDWMCompositionEnabled: error %x when detecting"\
anthony@1066 117 "if composition is enabled", res);
tdv@430 118 }
anthony@1066 119 } catch (const DllUtil::Exception &) {
tdv@430 120 J2dTraceLn(J2D_TRACE_ERROR,
anthony@1066 121 "IsDWMCompositionEnabled: no DwmIsCompositionEnabled() "\
anthony@1066 122 "in dwmapi.dll or dwmapi.dll cannot be loaded");
tdv@430 123 }
tdv@430 124
tdv@430 125 dwmIsCompositionEnabled = bRes;
tdv@430 126
tdv@430 127 JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
tdv@430 128 JNU_CallStaticMethodByName(env, NULL,
tdv@430 129 "sun/awt/Win32GraphicsEnvironment",
tdv@430 130 "dwmCompositionChanged", "(Z)V", (jboolean)bRes);
tdv@430 131 return bRes;
tdv@430 132 }
tdv@430 133
duke@0 134 /*
duke@0 135 * Class: sun_awt_Win32GraphicsEnvironment
duke@0 136 * Method: initDisplay
duke@0 137 * Signature: ()V
duke@0 138 */
duke@0 139 JNIEXPORT void JNICALL
duke@0 140 Java_sun_awt_Win32GraphicsEnvironment_initDisplay(JNIEnv *env,
duke@0 141 jclass thisClass)
duke@0 142 {
duke@0 143 // This method needs to be called prior to any display-related activity
duke@0 144 SetProcessDPIAwareProperty();
duke@0 145
tdv@430 146 DWMIsCompositionEnabled();
tdv@430 147
duke@0 148 initScreens(env);
duke@0 149 }
duke@0 150
duke@0 151 /*
duke@0 152 * Class: sun_awt_Win32GraphicsEnvironment
duke@0 153 * Method: getNumScreens
duke@0 154 * Signature: ()I
duke@0 155 */
duke@0 156 JNIEXPORT jint JNICALL
duke@0 157 Java_sun_awt_Win32GraphicsEnvironment_getNumScreens(JNIEnv *env,
duke@0 158 jobject thisobj)
duke@0 159 {
duke@0 160 Devices::InstanceAccess devices;
duke@0 161 return devices->GetNumDevices();
duke@0 162 }
duke@0 163
duke@0 164 /*
duke@0 165 * Class: sun_awt_Win32GraphicsEnvironment
duke@0 166 * Method: getDefaultScreen
duke@0 167 * Signature: ()I
duke@0 168 */
duke@0 169 JNIEXPORT jint JNICALL
duke@0 170 Java_sun_awt_Win32GraphicsEnvironment_getDefaultScreen(JNIEnv *env,
duke@0 171 jobject thisobj)
duke@0 172 {
duke@0 173 return AwtWin32GraphicsDevice::GetDefaultDeviceIndex();
duke@0 174 }
duke@0 175
duke@0 176 /*
rkennke@1686 177 * Class: sun_awt_Win32FontManager
duke@0 178 * Method: registerFontWithPlatform
duke@0 179 * Signature: (Ljava/lang/String;)V
duke@0 180 */
duke@0 181 JNIEXPORT void JNICALL
rkennke@1686 182 Java_sun_awt_Win32FontManager_registerFontWithPlatform(JNIEnv *env,
rkennke@1686 183 jclass cl,
rkennke@1686 184 jstring fontName)
duke@0 185 {
art@862 186 LPTSTR file = (LPTSTR)JNU_GetStringPlatformChars(env, fontName, JNI_FALSE);
art@862 187 if (file) {
art@862 188 ::AddFontResourceEx(file, FR_PRIVATE, NULL);
art@862 189 JNU_ReleaseStringPlatformChars(env, fontName, file);
duke@0 190 }
duke@0 191 }
duke@0 192
duke@0 193
duke@0 194 /*
rkennke@1686 195 * Class: sun_awt_Win32FontManagerEnvironment
duke@0 196 * Method: deRegisterFontWithPlatform
duke@0 197 * Signature: (Ljava/lang/String;)V
duke@0 198 *
duke@0 199 * This method intended for future use.
duke@0 200 */
duke@0 201 JNIEXPORT void JNICALL
rkennke@1686 202 Java_sun_awt_Win32FontManager_deRegisterFontWithPlatform(JNIEnv *env,
rkennke@1686 203 jclass cl,
rkennke@1686 204 jstring fontName)
duke@0 205 {
art@862 206 LPTSTR file = (LPTSTR)JNU_GetStringPlatformChars(env, fontName, JNI_FALSE);
art@862 207 if (file) {
art@862 208 ::RemoveFontResourceEx(file, FR_PRIVATE, NULL);
art@862 209 JNU_ReleaseStringPlatformChars(env, fontName, file);
duke@0 210 }
duke@0 211 }
duke@0 212
duke@0 213 #define EUDCKEY_JA_JP L"EUDC\\932"
duke@0 214 #define EUDCKEY_ZH_CN L"EUDC\\936"
duke@0 215 #define EUDCKEY_ZH_TW L"EUDC\\950"
duke@0 216 #define EUDCKEY_KO_KR L"EUDC\\949"
duke@0 217 #define LANGID_JA_JP 0x411
duke@0 218 #define LANGID_ZH_CN 0x0804
duke@0 219 #define LANGID_ZH_SG 0x1004
duke@0 220 #define LANGID_ZH_TW 0x0404
duke@0 221 #define LANGID_ZH_HK 0x0c04
duke@0 222 #define LANGID_ZH_MO 0x1404
duke@0 223 #define LANGID_KO_KR 0x0412
duke@0 224
duke@0 225
duke@0 226 JNIEXPORT jstring JNICALL
rkennke@1686 227 Java_sun_awt_Win32FontManager_getEUDCFontFile(JNIEnv *env, jclass cl) {
duke@0 228 int rc;
duke@0 229 HKEY key;
duke@0 230 DWORD type;
duke@0 231 WCHAR fontPathBuf[MAX_PATH + 1];
duke@0 232 unsigned long fontPathLen = MAX_PATH + 1;
duke@0 233 WCHAR tmpPath[MAX_PATH + 1];
duke@0 234 LPWSTR fontPath = fontPathBuf;
duke@0 235 LPWSTR eudcKey = NULL;
duke@0 236
duke@0 237 LANGID langID = GetSystemDefaultLangID();
duke@0 238 //lookup for encoding ID, EUDC only supported in
duke@0 239 //codepage 932, 936, 949, 950 (and unicode)
duke@0 240 if (langID == LANGID_JA_JP) {
duke@0 241 eudcKey = EUDCKEY_JA_JP;
duke@0 242 } else if (langID == LANGID_ZH_CN || langID == LANGID_ZH_SG) {
duke@0 243 eudcKey = EUDCKEY_ZH_CN;
duke@0 244 } else if (langID == LANGID_ZH_HK || langID == LANGID_ZH_TW ||
duke@0 245 langID == LANGID_ZH_MO) {
duke@0 246 eudcKey = EUDCKEY_ZH_TW;
duke@0 247 } else if (langID == LANGID_KO_KR) {
duke@0 248 eudcKey = EUDCKEY_KO_KR;
duke@0 249 } else {
duke@0 250 return NULL;
duke@0 251 }
duke@0 252
duke@0 253 rc = RegOpenKeyEx(HKEY_CURRENT_USER, eudcKey, 0, KEY_READ, &key);
duke@0 254 if (rc != ERROR_SUCCESS) {
duke@0 255 return NULL;
duke@0 256 }
duke@0 257 rc = RegQueryValueEx(key,
duke@0 258 L"SystemDefaultEUDCFont",
duke@0 259 0,
duke@0 260 &type,
duke@0 261 (LPBYTE)fontPath,
duke@0 262 &fontPathLen);
duke@0 263 RegCloseKey(key);
duke@0 264 if (rc != ERROR_SUCCESS || type != REG_SZ) {
duke@0 265 return NULL;
duke@0 266 }
duke@0 267 fontPath[fontPathLen] = L'\0';
duke@0 268 if (wcsstr(fontPath, L"%SystemRoot%")) {
duke@0 269 //if the fontPath includes %SystemRoot%
duke@0 270 LPWSTR systemRoot = _wgetenv(L"SystemRoot");
duke@0 271 if (systemRoot != NULL
art@2859 272 && swprintf(tmpPath, MAX_PATH, L"%s%s", systemRoot, fontPath + 12) != -1) {
duke@0 273 fontPath = tmpPath;
duke@0 274 }
duke@0 275 else {
duke@0 276 return NULL;
duke@0 277 }
duke@0 278 } else if (wcscmp(fontPath, L"EUDC.TTE") == 0) {
duke@0 279 //else to see if it only inludes "EUDC.TTE"
duke@0 280 WCHAR systemRoot[MAX_PATH + 1];
duke@0 281 if (GetWindowsDirectory(systemRoot, MAX_PATH + 1) != 0) {
art@2859 282 swprintf(tmpPath, MAX_PATH, L"%s\\FONTS\\EUDC.TTE", systemRoot);
duke@0 283 fontPath = tmpPath;
duke@0 284 }
duke@0 285 else {
duke@0 286 return NULL;
duke@0 287 }
duke@0 288 }
duke@0 289 return JNU_NewStringPlatform(env, fontPath);
duke@0 290 }
duke@0 291
duke@0 292 /*
duke@0 293 * Class: sun_awt_Win32GraphicsEnvironment
duke@0 294 * Method: getXResolution
duke@0 295 * Signature: ()I
duke@0 296 */
duke@0 297 JNIEXPORT jint JNICALL
duke@0 298 Java_sun_awt_Win32GraphicsEnvironment_getXResolution(JNIEnv *env, jobject wge)
duke@0 299 {
duke@0 300 TRY;
duke@0 301
duke@0 302 HWND hWnd = ::GetDesktopWindow();
duke@0 303 HDC hDC = ::GetDC(hWnd);
duke@0 304 jint result = ::GetDeviceCaps(hDC, LOGPIXELSX);
duke@0 305 ::ReleaseDC(hWnd, hDC);
duke@0 306 return result;
duke@0 307
duke@0 308 CATCH_BAD_ALLOC_RET(0);
duke@0 309 }
duke@0 310
duke@0 311 /*
duke@0 312 * Class: sun_awt_Win32GraphicsEnvironment
duke@0 313 * Method: getYResolution
duke@0 314 * Signature: ()I
duke@0 315 */
duke@0 316 JNIEXPORT jint JNICALL
duke@0 317 Java_sun_awt_Win32GraphicsEnvironment_getYResolution(JNIEnv *env, jobject wge)
duke@0 318 {
duke@0 319 TRY;
duke@0 320
duke@0 321 HWND hWnd = ::GetDesktopWindow();
duke@0 322 HDC hDC = ::GetDC(hWnd);
duke@0 323 jint result = ::GetDeviceCaps(hDC, LOGPIXELSY);
duke@0 324 ::ReleaseDC(hWnd, hDC);
duke@0 325 return result;
duke@0 326
duke@0 327 CATCH_BAD_ALLOC_RET(0);
duke@0 328 }
tdv@430 329
tdv@430 330 /*
tdv@430 331 * Class: sun_awt_Win32GraphicsEnvironment
tdv@430 332 * Method: isVistaOS
tdv@430 333 * Signature: ()Z
tdv@430 334 */
tdv@430 335 JNIEXPORT jboolean JNICALL Java_sun_awt_Win32GraphicsEnvironment_isVistaOS
tdv@430 336 (JNIEnv *env, jclass wgeclass)
tdv@430 337 {
tdv@430 338 return IS_WINVISTA;
tdv@430 339 }