| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "ui/gfx/win/dpi.h" | |
| 6 | |
| 7 #include <windows.h> | |
| 8 #include "base/win/scoped_hdc.h" | |
| 9 #include "ui/gfx/display.h" | |
| 10 | |
| 11 namespace { | |
| 12 | |
| 13 const float kDefaultDPI = 96.f; | |
| 14 | |
| 15 float g_device_scale_factor = 0.f; | |
| 16 | |
| 17 float GetUnforcedDeviceScaleFactor() { | |
| 18 return g_device_scale_factor ? | |
| 19 g_device_scale_factor : | |
| 20 static_cast<float>(gfx::GetDPI().width()) / kDefaultDPI; | |
| 21 } | |
| 22 | |
| 23 } // namespace | |
| 24 | |
| 25 namespace gfx { | |
| 26 | |
| 27 void SetDefaultDeviceScaleFactor(float scale) { | |
| 28 DCHECK_NE(0.f, scale); | |
| 29 g_device_scale_factor = scale; | |
| 30 } | |
| 31 | |
| 32 Size GetDPI() { | |
| 33 static int dpi_x = 0; | |
| 34 static int dpi_y = 0; | |
| 35 static bool should_initialize = true; | |
| 36 | |
| 37 if (should_initialize) { | |
| 38 should_initialize = false; | |
| 39 base::win::ScopedGetDC screen_dc(NULL); | |
| 40 // This value is safe to cache for the life time of the app since the | |
| 41 // user must logout to change the DPI setting. This value also applies | |
| 42 // to all screens. | |
| 43 dpi_x = GetDeviceCaps(screen_dc, LOGPIXELSX); | |
| 44 dpi_y = GetDeviceCaps(screen_dc, LOGPIXELSY); | |
| 45 } | |
| 46 return Size(dpi_x, dpi_y); | |
| 47 } | |
| 48 | |
| 49 float GetDPIScale() { | |
| 50 if (gfx::Display::HasForceDeviceScaleFactor()) | |
| 51 return gfx::Display::GetForcedDeviceScaleFactor(); | |
| 52 float dpi_scale = GetUnforcedDeviceScaleFactor(); | |
| 53 return (dpi_scale <= 1.25f) ? 1.f : dpi_scale; | |
| 54 } | |
| 55 | |
| 56 namespace win { | |
| 57 | |
| 58 int GetSystemMetricsInDIP(int metric) { | |
| 59 // The system metrics always reflect the system DPI, not whatever scale we've | |
| 60 // forced or decided to use. | |
| 61 return static_cast<int>( | |
| 62 std::round(GetSystemMetrics(metric) / GetUnforcedDeviceScaleFactor())); | |
| 63 } | |
| 64 | |
| 65 } // namespace win | |
| 66 } // namespace gfx | |
| OLD | NEW |