| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/base/win/dpi.h" | 5 #include "ui/base/win/dpi.h" |
| 6 | 6 |
| 7 #include <windows.h> | 7 #include <windows.h> |
| 8 | 8 |
| 9 #include "base/win/scoped_hdc.h" | 9 #include "base/win/scoped_hdc.h" |
| 10 #include "ui/base/layout.h" | 10 #include "ui/base/layout.h" |
| 11 #include "base/win/registry.h" | |
| 12 #include "ui/gfx/display.h" | 11 #include "ui/gfx/display.h" |
| 13 #include "ui/gfx/point_conversions.h" | 12 #include "ui/gfx/point_conversions.h" |
| 14 #include "ui/gfx/rect_conversions.h" | 13 #include "ui/gfx/rect_conversions.h" |
| 15 #include "ui/gfx/size_conversions.h" | 14 #include "ui/gfx/size_conversions.h" |
| 16 | 15 |
| 17 namespace { | 16 namespace { |
| 18 | 17 |
| 19 int kDefaultDPIX = 96; | 18 int kDefaultDPIX = 96; |
| 20 int kDefaultDPIY = 96; | 19 int kDefaultDPIY = 96; |
| 21 | 20 |
| 22 float GetDeviceScaleFactorImpl() { | 21 float GetDeviceScaleFactorImpl() { |
| 23 #if defined(ENABLE_HIDPI) | 22 #if defined(ENABLE_HIDPI) |
| 24 float scale = gfx::Display::HasForceDeviceScaleFactor() ? | 23 float scale = gfx::Display::HasForceDeviceScaleFactor() ? |
| 25 gfx::Display::GetForcedDeviceScaleFactor() : ui::GetDPIScale(); | 24 gfx::Display::GetForcedDeviceScaleFactor() : ui::GetDPIScale(); |
| 26 // Quantize to nearest supported scale factor. | 25 // Quantize to nearest supported scale factor. |
| 27 scale = ui::GetScaleFactorScale(ui::GetScaleFactorFromScale(scale)); | 26 scale = ui::GetScaleFactorScale(ui::GetScaleFactorFromScale(scale)); |
| 28 return scale; | 27 return scale; |
| 29 #else | 28 #else |
| 30 return 1.0f; | 29 return 1.0f; |
| 31 #endif | 30 #endif |
| 32 } | 31 } |
| 33 | 32 |
| 34 FARPROC GetProcAddressWrapper(LPCSTR module_name, LPCSTR proc_name) { | |
| 35 HMODULE module = ::GetModuleHandleA(module_name); | |
| 36 if (module) { | |
| 37 return ::GetProcAddress(module, proc_name); | |
| 38 } | |
| 39 return NULL; | |
| 40 } | |
| 41 | |
| 42 BOOL IsProcessDPIAwareWrapper() { | |
| 43 typedef BOOL(WINAPI *IsProcessDPIAwarePtr)(VOID); | |
| 44 IsProcessDPIAwarePtr is_process_dpi_aware_func = | |
| 45 reinterpret_cast<IsProcessDPIAwarePtr>( | |
| 46 GetProcAddressWrapper("user32.dll", "IsProcessDPIAware")); | |
| 47 if (is_process_dpi_aware_func) | |
| 48 return is_process_dpi_aware_func(); | |
| 49 return FALSE; | |
| 50 } | |
| 51 | |
| 52 } // namespace | 33 } // namespace |
| 53 | 34 |
| 54 namespace ui { | 35 namespace ui { |
| 55 | 36 |
| 56 gfx::Size GetDPI() { | 37 gfx::Size GetDPI() { |
| 57 static int dpi_x = 0; | 38 static int dpi_x = 0; |
| 58 static int dpi_y = 0; | 39 static int dpi_y = 0; |
| 59 static bool should_initialize = true; | 40 static bool should_initialize = true; |
| 60 | 41 |
| 61 if (should_initialize) { | 42 if (should_initialize) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 77 | 58 |
| 78 bool IsInHighDPIMode() { | 59 bool IsInHighDPIMode() { |
| 79 gfx::Size dpi(GetDPI()); | 60 gfx::Size dpi(GetDPI()); |
| 80 return dpi.width() > kDefaultDPIX || dpi.height() > kDefaultDPIY; | 61 return dpi.width() > kDefaultDPIX || dpi.height() > kDefaultDPIY; |
| 81 } | 62 } |
| 82 | 63 |
| 83 void EnableHighDPISupport() { | 64 void EnableHighDPISupport() { |
| 84 typedef BOOL(WINAPI *SetProcessDPIAwarePtr)(VOID); | 65 typedef BOOL(WINAPI *SetProcessDPIAwarePtr)(VOID); |
| 85 SetProcessDPIAwarePtr set_process_dpi_aware_func = | 66 SetProcessDPIAwarePtr set_process_dpi_aware_func = |
| 86 reinterpret_cast<SetProcessDPIAwarePtr>( | 67 reinterpret_cast<SetProcessDPIAwarePtr>( |
| 87 GetProcAddressWrapper("user32.dll", "SetProcessDPIAware")); | 68 GetProcAddress(GetModuleHandleA("user32.dll"), "SetProcessDPIAware")); |
| 88 if (set_process_dpi_aware_func) | 69 if (set_process_dpi_aware_func) |
| 89 set_process_dpi_aware_func(); | 70 set_process_dpi_aware_func(); |
| 90 } | 71 } |
| 91 | 72 |
| 92 namespace win { | 73 namespace win { |
| 93 | 74 |
| 94 float GetDeviceScaleFactor() { | 75 float GetDeviceScaleFactor() { |
| 95 static const float device_scale_factor = GetDeviceScaleFactorImpl(); | 76 static const float device_scale_factor = GetDeviceScaleFactorImpl(); |
| 96 return device_scale_factor; | 77 return device_scale_factor; |
| 97 } | 78 } |
| (...skipping 17 matching lines...) Expand all Loading... |
| 115 | 96 |
| 116 gfx::Size ScreenToDIPSize(const gfx::Size& size_in_pixels) { | 97 gfx::Size ScreenToDIPSize(const gfx::Size& size_in_pixels) { |
| 117 return gfx::ToFlooredSize( | 98 return gfx::ToFlooredSize( |
| 118 gfx::ScaleSize(size_in_pixels, 1.0f / GetDeviceScaleFactor())); | 99 gfx::ScaleSize(size_in_pixels, 1.0f / GetDeviceScaleFactor())); |
| 119 } | 100 } |
| 120 | 101 |
| 121 gfx::Size DIPToScreenSize(const gfx::Size& dip_size) { | 102 gfx::Size DIPToScreenSize(const gfx::Size& dip_size) { |
| 122 return gfx::ToFlooredSize(gfx::ScaleSize(dip_size, GetDeviceScaleFactor())); | 103 return gfx::ToFlooredSize(gfx::ScaleSize(dip_size, GetDeviceScaleFactor())); |
| 123 } | 104 } |
| 124 | 105 |
| 125 double GetDPIScaleFromRegistry() { | |
| 126 static double scale = -1.0; | |
| 127 if (scale == -1.0) { | |
| 128 double result = 1.0; | |
| 129 if (!IsProcessDPIAwareWrapper()) { | |
| 130 //HKEY_CURRENT_USER\Control Panel\Desktop\WindowMetrics\AppliedDPI | |
| 131 base::win::RegKey key(HKEY_CURRENT_USER, | |
| 132 L"Control Panel\\Desktop\\WindowMetrics", | |
| 133 KEY_QUERY_VALUE); | |
| 134 | |
| 135 if (key.Valid()) { | |
| 136 DWORD value = 0; | |
| 137 if (key.ReadValueDW(L"AppliedDPI", &value) == ERROR_SUCCESS) { | |
| 138 result = ((double)value) / kDefaultDPIX; | |
| 139 } | |
| 140 } | |
| 141 } | |
| 142 scale = result; | |
| 143 } | |
| 144 | |
| 145 // Safety test to ignore invalid settings. | |
| 146 if (scale <= 0.0) | |
| 147 scale = 1.0; | |
| 148 | |
| 149 return scale; | |
| 150 } | |
| 151 | |
| 152 } // namespace win | 106 } // namespace win |
| 153 | 107 |
| 154 } // namespace ui | 108 } // namespace ui |
| OLD | NEW |