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 <vector> |
9 #include "base/gtest_prod_util.h" | 9 |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/memory/scoped_ptr.h" | |
12 #include "ui/gfx/display_change_notifier.h" | 11 #include "ui/gfx/display_change_notifier.h" |
13 #include "ui/gfx/gfx_export.h" | 12 #include "ui/gfx/gfx_export.h" |
14 #include "ui/gfx/screen.h" | 13 #include "ui/gfx/screen.h" |
15 #include "ui/gfx/win/singleton_hwnd_observer.h" | 14 #include "ui/gfx/win/display_manager_observer.h" |
16 | 15 |
17 namespace gfx { | 16 namespace gfx { |
18 | 17 |
19 class GFX_EXPORT ScreenWin : public Screen { | 18 class Point; |
| 19 class Rect; |
| 20 class Size; |
| 21 |
| 22 namespace win { |
| 23 |
| 24 class DisplayInfo; |
| 25 class DisplayManager; |
| 26 class ScreenWinDisplay; |
| 27 |
| 28 } // namespace win |
| 29 |
| 30 // Windows historically has had a hard time handling displays of DPIs higher |
| 31 // than 96. Handling multiple DPI displays means we have to deal with Windows' |
| 32 // monitor physical coordinates and map into Chrome's DIP coordinates. |
| 33 // |
| 34 // To do this, ScreenWin reasons over monitors as a tree using the primary |
| 35 // monitor as the root. Any monitor touching this root is considered a child. |
| 36 // Subsequent generations are formed similarly, having preferring parents that |
| 37 // are discovered earlier. As a result, if there is a monitor that touches two |
| 38 // other monitors, the earliest discovered monitor is the parent. |
| 39 // |
| 40 // This also presumes that all monitors are connected components. Windows, by UI |
| 41 // construction restricts the layout of monitors to connected components. |
| 42 // |
| 43 // All scaling transformations are performed with respect to this physical-DIP |
| 44 // mapping. |
| 45 // |
| 46 // Note that this does not handle cases where a scaled display may have |
| 47 // insufficent room to lay out its children. In these cases, a DIP point could |
| 48 // map to multiple screen points due to overlap. The first discovered screen |
| 49 // will take precedence. |
| 50 class GFX_EXPORT ScreenWin : public Screen, |
| 51 public gfx::win::DisplayManagerObserver { |
20 public: | 52 public: |
21 ScreenWin(); | 53 ScreenWin(); |
22 ~ScreenWin() override; | 54 virtual ~ScreenWin() override; |
| 55 |
| 56 // Converts a screen pixel point to a DIP point. |
| 57 // The DPI scale is performed relative to the display containing the screen |
| 58 // pixel point maintaining a constant origin on both Screen and DIP (same as |
| 59 // Windows). |
| 60 static Point ScreenToDIPPoint(const Point& pixel_point); |
| 61 |
| 62 // Converts a DIP point to a screen pixel point. |
| 63 // The DPI scale is performed relative to the display containing the DIP point |
| 64 // maintaining a constant origin on both Screen and DIP (same as Windows). |
| 65 static Point DIPToScreenPoint(const Point& dip_point); |
| 66 |
| 67 // Converts a client pixel point relative to |hwnd| to a DIP point. |
| 68 // The DPI scale is performed relative to |hwnd| using an origin of (0, 0). |
| 69 static Point ClientToDIPPoint(HWND hwnd, const Point& client_point); |
| 70 |
| 71 // Converts a client DIP point relative to |hwnd| to a client pixel point. |
| 72 // The DPI scale is performed relative to |hwnd| using an origin of (0, 0). |
| 73 static Point DIPToClientPoint(HWND hwnd, const Point& dip_point); |
| 74 |
| 75 // WARNING: there is no right way to scale sizes and rects. The implementation |
| 76 // of these strives to maintain a constant size by scaling the size |
| 77 // independent of the origin. An alternative is to get the enclosing rect, |
| 78 // which is the right way for some situations. Understand which you need |
| 79 // before blindly assuming this is the right way. |
| 80 |
| 81 // Converts a screen rect to a DIP rect. |
| 82 // The DPI scale is performed relative to the display nearest to |hwnd| |
| 83 // maintaining a constant origin on both Screen and DIP (same as Windows). |
| 84 // If |hwnd| is null, scaling will be performed to the display nearest to |
| 85 // |pixel_bounds|. |
| 86 static Rect ScreenToDIPRect(HWND hwnd, const Rect& pixel_bounds); |
| 87 |
| 88 // Converts a DIP rect to a screen rect. |
| 89 // The DPI scale is performed relative to the display nearest to |hwnd| |
| 90 // maintaining a constant origin on both Screen and DIP (same as Windows). |
| 91 // If |hwnd| is null, scaling will be performed to the display nearest to |
| 92 // |dip_bounds|. |
| 93 static Rect DIPToScreenRect(HWND hwnd, const Rect& dip_bounds); |
| 94 |
| 95 // Converts a client rect to a DIP rect. |
| 96 // The DPI scale is performed relative to |hwnd| using an origin of (0, 0). |
| 97 static Rect ClientToDIPRect(HWND hwnd, const Rect& pixel_bounds); |
| 98 |
| 99 // Converts a DIP rect to a client rect. |
| 100 // The DPI scale is performed relative to |hwnd| using an origin of (0, 0). |
| 101 static Rect DIPToClientRect(HWND hwnd, const Rect& dip_bounds); |
| 102 |
| 103 // Converts a screen size to a DIP size. |
| 104 // The DPI scale is performed relative to the display nearest to |hwnd|. |
| 105 static Size ScreenToDIPSize(HWND hwnd, const Size& size_in_pixels); |
| 106 |
| 107 // Converts a DIP size to a screen size. |
| 108 // The DPI scale is performed relative to the display nearest to |hwnd|. |
| 109 static Size DIPToScreenSize(HWND hwnd, const Size& dip_size); |
23 | 110 |
24 // Returns the HWND associated with the NativeView. | 111 // Returns the HWND associated with the NativeView. |
25 virtual HWND GetHWNDFromNativeView(NativeView window) const; | 112 virtual HWND GetHWNDFromNativeView(NativeView window) const; |
26 | 113 |
27 // Returns the NativeView associated with the HWND. | 114 // Returns the NativeView associated with the HWND. |
28 virtual NativeWindow GetNativeWindowFromHWND(HWND hwnd) const; | 115 virtual NativeWindow GetNativeWindowFromHWND(HWND hwnd) const; |
29 | 116 |
30 protected: | 117 protected: |
31 // Overridden from gfx::Screen: | 118 // Overridden from gfx::Screen: |
32 gfx::Point GetCursorScreenPoint() override; | 119 gfx::Point GetCursorScreenPoint() override; |
33 gfx::NativeWindow GetWindowUnderCursor() override; | 120 gfx::NativeWindow GetWindowUnderCursor() override; |
34 gfx::NativeWindow GetWindowAtScreenPoint(const gfx::Point& point) override; | 121 gfx::NativeWindow GetWindowAtScreenPoint(const gfx::Point& point) override; |
35 int GetNumDisplays() const override; | 122 int GetNumDisplays() const override; |
36 std::vector<gfx::Display> GetAllDisplays() const override; | 123 std::vector<gfx::Display> GetAllDisplays() const override; |
37 gfx::Display GetDisplayNearestWindow(gfx::NativeView window) const override; | 124 gfx::Display GetDisplayNearestWindow(gfx::NativeView window) const override; |
38 gfx::Display GetDisplayNearestPoint(const gfx::Point& point) const override; | 125 gfx::Display GetDisplayNearestPoint(const gfx::Point& point) const override; |
39 gfx::Display GetDisplayMatching(const gfx::Rect& match_rect) const override; | 126 gfx::Display GetDisplayMatching(const gfx::Rect& match_rect) const override; |
40 gfx::Display GetPrimaryDisplay() const override; | 127 gfx::Display GetPrimaryDisplay() const override; |
41 void AddObserver(DisplayObserver* observer) override; | 128 void AddObserver(DisplayObserver* observer) override; |
42 void RemoveObserver(DisplayObserver* observer) override; | 129 void RemoveObserver(DisplayObserver* observer) override; |
| 130 gfx::Rect ScreenToDIPRectInWindow( |
| 131 NativeView view, const gfx::Rect& screen_rect) const override; |
| 132 gfx::Rect DIPToScreenRectInWindow( |
| 133 NativeView view, const gfx::Rect& dip_rect) const override; |
| 134 |
| 135 // Overridden from gfx::win::DisplayManagerObserver: |
| 136 void OnDisplaysChanged( |
| 137 const std::vector<gfx::win::ScreenWinDisplay> old_screen_win_displays, |
| 138 const std::vector<gfx::win::ScreenWinDisplay> new_screen_win_displays) |
| 139 override; |
43 | 140 |
44 private: | 141 private: |
45 FRIEND_TEST_ALL_PREFIXES(ScreenWinTest, SingleDisplay1x); | |
46 FRIEND_TEST_ALL_PREFIXES(ScreenWinTest, SingleDisplay2x); | |
47 | |
48 void OnWndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam); | |
49 | |
50 static std::vector<gfx::Display> GetDisplaysForMonitorInfos( | |
51 const std::vector<MONITORINFOEX>& monitor_infos); | |
52 | |
53 // Helper implementing the DisplayObserver handling. | 142 // Helper implementing the DisplayObserver handling. |
54 gfx::DisplayChangeNotifier change_notifier_; | 143 gfx::DisplayChangeNotifier change_notifier_; |
55 | 144 |
56 scoped_ptr<SingletonHwndObserver> singleton_hwnd_observer_; | 145 gfx::win::DisplayManager* display_manager_; |
57 | |
58 // Current list of displays. | |
59 std::vector<gfx::Display> displays_; | |
60 | 146 |
61 DISALLOW_COPY_AND_ASSIGN(ScreenWin); | 147 DISALLOW_COPY_AND_ASSIGN(ScreenWin); |
62 }; | 148 }; |
63 | 149 |
64 } // namespace gfx | 150 } // namespace gfx |
65 | 151 |
66 #endif // UI_GFX_SCREEN_WIN_H_ | 152 #endif // UI_GFX_SCREEN_WIN_H_ |
OLD | NEW |