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" | |
11 #include "ui/gfx/display.h" | 12 #include "ui/gfx/display.h" |
12 #include "ui/gfx/point_conversions.h" | 13 #include "ui/gfx/point_conversions.h" |
13 #include "ui/gfx/rect_conversions.h" | 14 #include "ui/gfx/rect_conversions.h" |
14 #include "ui/gfx/size_conversions.h" | 15 #include "ui/gfx/size_conversions.h" |
15 | 16 |
16 namespace { | 17 namespace { |
17 | 18 |
18 int kDefaultDPIX = 96; | 19 int kDefaultDPIX = 96; |
19 int kDefaultDPIY = 96; | 20 int kDefaultDPIY = 96; |
20 | 21 |
21 float GetDeviceScaleFactorImpl() { | 22 float GetDeviceScaleFactorImpl() { |
22 #if defined(ENABLE_HIDPI) | 23 #if defined(ENABLE_HIDPI) |
23 float scale = gfx::Display::HasForceDeviceScaleFactor() ? | 24 float scale = gfx::Display::HasForceDeviceScaleFactor() ? |
24 gfx::Display::GetForcedDeviceScaleFactor() : ui::GetDPIScale(); | 25 gfx::Display::GetForcedDeviceScaleFactor() : ui::GetDPIScale(); |
25 // Quantize to nearest supported scale factor. | 26 // Quantize to nearest supported scale factor. |
26 scale = ui::GetScaleFactorScale(ui::GetScaleFactorFromScale(scale)); | 27 scale = ui::GetScaleFactorScale(ui::GetScaleFactorFromScale(scale)); |
27 return scale; | 28 return scale; |
28 #else | 29 #else |
29 return 1.0f; | 30 return 1.0f; |
30 #endif | 31 #endif |
31 } | 32 } |
32 | 33 |
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 | |
33 } // namespace | 52 } // namespace |
34 | 53 |
35 namespace ui { | 54 namespace ui { |
36 | 55 |
37 gfx::Size GetDPI() { | 56 gfx::Size GetDPI() { |
38 static int dpi_x = 0; | 57 static int dpi_x = 0; |
39 static int dpi_y = 0; | 58 static int dpi_y = 0; |
40 static bool should_initialize = true; | 59 static bool should_initialize = true; |
41 | 60 |
42 if (should_initialize) { | 61 if (should_initialize) { |
(...skipping 15 matching lines...) Expand all Loading... | |
58 | 77 |
59 bool IsInHighDPIMode() { | 78 bool IsInHighDPIMode() { |
60 gfx::Size dpi(GetDPI()); | 79 gfx::Size dpi(GetDPI()); |
61 return dpi.width() > kDefaultDPIX || dpi.height() > kDefaultDPIY; | 80 return dpi.width() > kDefaultDPIX || dpi.height() > kDefaultDPIY; |
62 } | 81 } |
63 | 82 |
64 void EnableHighDPISupport() { | 83 void EnableHighDPISupport() { |
65 typedef BOOL(WINAPI *SetProcessDPIAwarePtr)(VOID); | 84 typedef BOOL(WINAPI *SetProcessDPIAwarePtr)(VOID); |
66 SetProcessDPIAwarePtr set_process_dpi_aware_func = | 85 SetProcessDPIAwarePtr set_process_dpi_aware_func = |
67 reinterpret_cast<SetProcessDPIAwarePtr>( | 86 reinterpret_cast<SetProcessDPIAwarePtr>( |
68 GetProcAddress(GetModuleHandleA("user32.dll"), "SetProcessDPIAware")); | 87 GetProcAddressWrapper("user32.dll", "SetProcessDPIAware")); |
69 if (set_process_dpi_aware_func) | 88 if (set_process_dpi_aware_func) |
70 set_process_dpi_aware_func(); | 89 set_process_dpi_aware_func(); |
71 } | 90 } |
72 | 91 |
73 namespace win { | 92 namespace win { |
74 | 93 |
75 float GetDeviceScaleFactor() { | 94 float GetDeviceScaleFactor() { |
76 static const float device_scale_factor = GetDeviceScaleFactorImpl(); | 95 static const float device_scale_factor = GetDeviceScaleFactorImpl(); |
77 return device_scale_factor; | 96 return device_scale_factor; |
78 } | 97 } |
(...skipping 17 matching lines...) Expand all Loading... | |
96 | 115 |
97 gfx::Size ScreenToDIPSize(const gfx::Size& size_in_pixels) { | 116 gfx::Size ScreenToDIPSize(const gfx::Size& size_in_pixels) { |
98 return gfx::ToFlooredSize( | 117 return gfx::ToFlooredSize( |
99 gfx::ScaleSize(size_in_pixels, 1.0f / GetDeviceScaleFactor())); | 118 gfx::ScaleSize(size_in_pixels, 1.0f / GetDeviceScaleFactor())); |
100 } | 119 } |
101 | 120 |
102 gfx::Size DIPToScreenSize(const gfx::Size& dip_size) { | 121 gfx::Size DIPToScreenSize(const gfx::Size& dip_size) { |
103 return gfx::ToFlooredSize(gfx::ScaleSize(dip_size, GetDeviceScaleFactor())); | 122 return gfx::ToFlooredSize(gfx::ScaleSize(dip_size, GetDeviceScaleFactor())); |
104 } | 123 } |
105 | 124 |
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 | |
Ryan Sleevi
2013/03/05 18:32:56
nit: space between //
| |
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; | |
Ryan Sleevi
2013/03/05 18:32:56
style: Don't do C-casts
| |
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 | |
106 } // namespace win | 152 } // namespace win |
107 | 153 |
108 } // namespace ui | 154 } // namespace ui |
OLD | NEW |