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 #ifndef UI_GFX_SCREEN_WIN_H_ | 5 #ifndef UI_GFX_SCREEN_WIN_H_ |
6 #define UI_GFX_SCREEN_WIN_H_ | 6 #define UI_GFX_SCREEN_WIN_H_ |
7 | 7 |
8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
9 #include "base/gtest_prod_util.h" | 9 #include "base/gtest_prod_util.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "ui/gfx/display_change_notifier.h" | 12 #include "ui/gfx/display_change_notifier.h" |
13 #include "ui/gfx/gfx_export.h" | 13 #include "ui/gfx/gfx_export.h" |
14 #include "ui/gfx/screen.h" | 14 #include "ui/gfx/screen.h" |
15 #include "ui/gfx/win/singleton_hwnd_observer.h" | 15 #include "ui/gfx/win/singleton_hwnd_observer.h" |
16 | 16 |
17 namespace gfx { | 17 namespace gfx { |
18 | 18 |
| 19 class Point; |
| 20 class Rect; |
| 21 class Size; |
| 22 |
| 23 // Windows historically has had a hard time handling displays of DPIs higher |
| 24 // than 96. Handling multiple DPI displays means we have to deal with Windows' |
| 25 // monitor physical coordinates and map into Chrome's DIP coordinates. |
| 26 // |
| 27 // To do this, ScreenWin reasons over monitors as a tree using the primary |
| 28 // monitor as the root. Any monitor touching this root is considered a child. |
| 29 // Subsequent generations are formed similarly, having preferring parents that |
| 30 // are discovered earlier. As a result, if there is a monitor that touches two |
| 31 // other monitors, the earliest discovered monitor is the parent. |
| 32 // |
| 33 // This also presumes that all monitors are connected components. Windows, by UI |
| 34 // construction restricts the layout of monitors to connected components. |
| 35 // |
| 36 // All scaling transformations are performed with respect to this physical-DIP |
| 37 // mapping. |
| 38 // |
| 39 // Note that this does not handle cases where a scaled display may have |
| 40 // insufficent room to lay out its children. In these cases, a DIP point could |
| 41 // map to multiple screen points due to overlap. The first discovered screen |
| 42 // will take precedence. |
19 class GFX_EXPORT ScreenWin : public Screen { | 43 class GFX_EXPORT ScreenWin : public Screen { |
20 public: | 44 public: |
21 ScreenWin(); | 45 ScreenWin(); |
22 ~ScreenWin() override; | 46 virtual ~ScreenWin() override; |
| 47 |
| 48 // Converts a screen pixel point to a DIP point. |
| 49 // The DPI scale is performed relative to the display containing the screen |
| 50 // pixel point maintaining a constant origin on both Screen and DIP (same as |
| 51 // Windows). |
| 52 static Point ScreenToDIPPoint(const Point& pixel_point); |
| 53 |
| 54 // Converts a DIP point to a screen pixel point. |
| 55 // The DPI scale is performed relative to the display containing the DIP point |
| 56 // maintaining a constant origin on both Screen and DIP (same as Windows). |
| 57 static Point DIPToScreenPoint(const Point& dip_point); |
| 58 |
| 59 // Converts a client pixel point relative to |hwnd| to a DIP point. |
| 60 // The DPI scale is performed relative to |hwnd| using an origin of (0, 0). |
| 61 static Point ClientToDIPPoint(HWND hwnd, const Point& client_point); |
| 62 |
| 63 // Converts a client DIP point relative to |hwnd| to a client pixel point. |
| 64 // The DPI scale is performed relative to |hwnd| using an origin of (0, 0). |
| 65 static Point DIPToClientPoint(HWND hwnd, const Point& dip_point); |
| 66 |
| 67 // WARNING: there is no right way to scale sizes and rects. The implementation |
| 68 // of these strives to maintain a constant size by scaling the size |
| 69 // independent of the origin. An alternative is to get the enclosing rect, |
| 70 // which is the right way for some situations. Understand which you need |
| 71 // before blindly assuming this is the right way. |
| 72 |
| 73 // Converts a screen rect to a DIP rect. |
| 74 // The DPI scale is performed relative to the display nearest to |hwnd| |
| 75 // maintaining a constant origin on both Screen and DIP (same as Windows). |
| 76 // If |hwnd| is null, scaling will be performed to the display nearest to |
| 77 // |pixel_bounds|. |
| 78 static Rect ScreenToDIPRect(HWND hwnd, const Rect& pixel_bounds); |
| 79 |
| 80 // Converts a DIP rect to a screen rect. |
| 81 // The DPI scale is performed relative to the display nearest to |hwnd| |
| 82 // maintaining a constant origin on both Screen and DIP (same as Windows). |
| 83 // If |hwnd| is null, scaling will be performed to the display nearest to |
| 84 // |dip_bounds|. |
| 85 static Rect DIPToScreenRect(HWND hwnd, const Rect& dip_bounds); |
| 86 |
| 87 // Converts a client rect to a DIP rect. |
| 88 // The DPI scale is performed relative to |hwnd| using an origin of (0, 0). |
| 89 static Rect ClientToDIPRect(HWND hwnd, const Rect& pixel_bounds); |
| 90 |
| 91 // Converts a DIP rect to a client rect. |
| 92 // The DPI scale is performed relative to |hwnd| using an origin of (0, 0). |
| 93 static Rect DIPToClientRect(HWND hwnd, const Rect& dip_bounds); |
| 94 |
| 95 // Converts a screen size to a DIP size. |
| 96 // The DPI scale is performed relative to the display nearest to |hwnd|. |
| 97 static Size ScreenToDIPSize(HWND hwnd, const Size& size_in_pixels); |
| 98 |
| 99 // Converts a DIP size to a screen size. |
| 100 // The DPI scale is performed relative to the display nearest to |hwnd|. |
| 101 static Size DIPToScreenSize(HWND hwnd, const Size& dip_size); |
23 | 102 |
24 // Returns the HWND associated with the NativeView. | 103 // Returns the HWND associated with the NativeView. |
25 virtual HWND GetHWNDFromNativeView(NativeView window) const; | 104 virtual HWND GetHWNDFromNativeView(NativeView window) const; |
26 | 105 |
27 // Returns the NativeView associated with the HWND. | 106 // Returns the NativeView associated with the HWND. |
28 virtual NativeWindow GetNativeWindowFromHWND(HWND hwnd) const; | 107 virtual NativeWindow GetNativeWindowFromHWND(HWND hwnd) const; |
29 | 108 |
30 protected: | 109 protected: |
31 // Overridden from gfx::Screen: | 110 // Holds the parameters used to create a gfx::DisplayWin. |
| 111 class GFX_EXPORT DisplayInfo { |
| 112 public: |
| 113 DisplayInfo(HMONITOR monitor, float device_scale_factor); |
| 114 DisplayInfo(const MONITORINFOEX& monitor_info, |
| 115 gfx::Display::Rotation rotation, |
| 116 float device_scale_factor); |
| 117 |
| 118 int64_t id() const { return id_; } |
| 119 gfx::Display::Rotation rotation() const { return rotation_; } |
| 120 const gfx::Rect& screen_rect() const { return screen_rect_; } |
| 121 const gfx::Rect& screen_work_rect() const { return screen_work_rect_; } |
| 122 float device_scale_factor() const { return device_scale_factor_; } |
| 123 |
| 124 private: |
| 125 void InitializeFromMonitorInfo(const MONITORINFOEX& monitor_info); |
| 126 |
| 127 int64_t id_; |
| 128 gfx::Display::Rotation rotation_; |
| 129 gfx::Rect screen_rect_; |
| 130 gfx::Rect screen_work_rect_; |
| 131 float device_scale_factor_; |
| 132 }; |
| 133 |
| 134 // For Unit Tests. |
| 135 explicit ScreenWin(const std::vector<DisplayInfo>& display_infos); |
| 136 |
| 137 static int64_t HashDeviceName(const wchar_t* device_name); |
| 138 |
| 139 // Overridden from gfx::Screen. |
32 gfx::Point GetCursorScreenPoint() override; | 140 gfx::Point GetCursorScreenPoint() override; |
33 gfx::NativeWindow GetWindowUnderCursor() override; | 141 gfx::NativeWindow GetWindowUnderCursor() override; |
34 gfx::NativeWindow GetWindowAtScreenPoint(const gfx::Point& point) override; | 142 gfx::NativeWindow GetWindowAtScreenPoint(const gfx::Point& point) override; |
35 int GetNumDisplays() const override; | 143 int GetNumDisplays() const override; |
36 std::vector<gfx::Display> GetAllDisplays() const override; | 144 std::vector<gfx::Display> GetAllDisplays() const override; |
37 gfx::Display GetDisplayNearestWindow(gfx::NativeView window) const override; | 145 gfx::Display GetDisplayNearestWindow(gfx::NativeView window) const override; |
38 gfx::Display GetDisplayNearestPoint(const gfx::Point& point) const override; | 146 gfx::Display GetDisplayNearestPoint(const gfx::Point& point) const override; |
39 gfx::Display GetDisplayMatching(const gfx::Rect& match_rect) const override; | 147 gfx::Display GetDisplayMatching(const gfx::Rect& match_rect) const override; |
40 gfx::Display GetPrimaryDisplay() const override; | 148 gfx::Display GetPrimaryDisplay() const override; |
41 void AddObserver(DisplayObserver* observer) override; | 149 void AddObserver(DisplayObserver* observer) override; |
42 void RemoveObserver(DisplayObserver* observer) override; | 150 void RemoveObserver(DisplayObserver* observer) override; |
| 151 gfx::Rect ScreenToDIPRectInWindow( |
| 152 NativeView view, const gfx::Rect& screen_rect) const override; |
| 153 gfx::Rect DIPToScreenRectInWindow( |
| 154 NativeView view, const gfx::Rect& dip_rect) const override; |
43 | 155 |
44 private: | 156 private: |
45 FRIEND_TEST_ALL_PREFIXES(ScreenWinTest, SingleDisplay1x); | 157 friend class TestScreenWinInitializer; |
46 FRIEND_TEST_ALL_PREFIXES(ScreenWinTest, SingleDisplay2x); | 158 |
| 159 // A gfx::Display that knows about its physical bounds. |
| 160 class DisplayWin; |
| 161 |
| 162 static DisplayWin GetDisplayNearestHWND(HWND hwnd); |
| 163 static DisplayWin GetDisplayNearestScreenRect(const Rect& screen_rect); |
| 164 static DisplayWin GetDisplayNearestScreenPoint(const Point& screen_point); |
| 165 static DisplayWin GetDisplayNearestDIPRect(const Rect& rect); |
| 166 static DisplayWin GetDisplayNearestDIPPoint(const Point& dip_point); |
| 167 static float GetScaleFactorForHWND(HWND hwnd); |
| 168 static float GetScaleFactorForScreenPoint(const Point& screen_point); |
| 169 static std::vector<DisplayInfo>::const_iterator FindTouchingDisplayInfo( |
| 170 const DisplayWin& display, const std::vector<DisplayInfo>& display_infos); |
| 171 static std::vector<DisplayWin> GetDisplaysFromDisplayInfos( |
| 172 const std::vector<DisplayInfo>& display_infos); |
| 173 static std::vector<DisplayWin> GetDisplayWins(); |
| 174 static BOOL CALLBACK EnumMonitorCallback(HMONITOR monitor, |
| 175 HDC hdc, |
| 176 LPRECT rect, |
| 177 LPARAM data); |
47 | 178 |
48 void OnWndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam); | 179 void OnWndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam); |
49 | 180 |
50 static std::vector<gfx::Display> GetDisplaysForMonitorInfos( | 181 // For Unit Tests. |
51 const std::vector<MONITORINFOEX>& monitor_infos); | 182 virtual MONITORINFOEX MonitorInfoFromScreenPoint( |
| 183 const gfx::Point& screen_point) const; |
| 184 virtual MONITORINFOEX MonitorInfoFromScreenRect(const gfx::Rect& screen_rect) |
| 185 const; |
| 186 virtual MONITORINFOEX MonitorInfoFromWindow(HWND hwnd) const; |
| 187 virtual HWND GetRootWindow(HWND hwnd) const; |
| 188 |
| 189 DisplayWin GetDisplayWin(const MONITORINFOEX& monitor_info) const; |
52 | 190 |
53 // Helper implementing the DisplayObserver handling. | 191 // Helper implementing the DisplayObserver handling. |
54 gfx::DisplayChangeNotifier change_notifier_; | 192 gfx::DisplayChangeNotifier change_notifier_; |
55 | 193 |
56 scoped_ptr<SingletonHwndObserver> singleton_hwnd_observer_; | 194 scoped_ptr<SingletonHwndObserver> singleton_hwnd_observer_; |
57 | 195 |
58 // Current list of displays. | 196 // Current list of DisplayWins. |
59 std::vector<gfx::Display> displays_; | 197 std::vector<DisplayWin> display_wins_; |
60 | 198 |
61 DISALLOW_COPY_AND_ASSIGN(ScreenWin); | 199 DISALLOW_COPY_AND_ASSIGN(ScreenWin); |
62 }; | 200 }; |
63 | 201 |
64 } // namespace gfx | 202 } // namespace gfx |
65 | 203 |
66 #endif // UI_GFX_SCREEN_WIN_H_ | 204 #endif // UI_GFX_SCREEN_WIN_H_ |
OLD | NEW |