| 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 <windows.h> | 8 #include <windows.h> |
| 9 | 9 |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "ui/gfx/display_change_notifier.h" | 13 #include "ui/gfx/display_change_notifier.h" |
| 14 #include "ui/gfx/gfx_export.h" | 14 #include "ui/gfx/gfx_export.h" |
| 15 #include "ui/gfx/native_widget_types.h" | 15 #include "ui/gfx/native_widget_types.h" |
| 16 #include "ui/gfx/screen.h" | 16 #include "ui/gfx/screen.h" |
| 17 #include "ui/gfx/win/singleton_hwnd_observer.h" | 17 #include "ui/gfx/win/singleton_hwnd_observer.h" |
| 18 | 18 |
| 19 namespace gfx { | 19 namespace gfx { |
| 20 | 20 |
| 21 class Display; | 21 class Display; |
| 22 class Point; | 22 class Point; |
| 23 class Rect; | 23 class Rect; |
| 24 class Size; |
| 24 | 25 |
| 25 namespace win { | 26 namespace win { |
| 26 | 27 |
| 27 class DisplayInfo; | 28 class DisplayInfo; |
| 28 class ScreenWinDisplay; | 29 class ScreenWinDisplay; |
| 29 | 30 |
| 30 } // namespace win | 31 } // namespace win |
| 31 | 32 |
| 32 class GFX_EXPORT ScreenWin : public Screen { | 33 class GFX_EXPORT ScreenWin : public Screen { |
| 33 public: | 34 public: |
| 34 ScreenWin(); | 35 ScreenWin(); |
| 35 ~ScreenWin() override; | 36 ~ScreenWin() override; |
| 36 | 37 |
| 38 // Converts a screen pixel point to a DIP point. |
| 39 // The DPI scale is performed relative to the display containing the screen |
| 40 // pixel point maintaining a constant origin on both Screen and DIP (same as |
| 41 // Windows). |
| 42 static Point ScreenToDIPPoint(const Point& pixel_point); |
| 43 |
| 44 // Converts a DIP point to a screen pixel point. |
| 45 // The DPI scale is performed relative to the display containing the DIP point |
| 46 // maintaining a constant origin on both Screen and DIP (same as Windows). |
| 47 static Point DIPToScreenPoint(const Point& dip_point); |
| 48 |
| 49 // Converts a client pixel point relative to |hwnd| to a DIP point. |
| 50 // The DPI scale is performed relative to |hwnd| using an origin of (0, 0). |
| 51 static Point ClientToDIPPoint(HWND hwnd, const Point& client_point); |
| 52 |
| 53 // Converts a client DIP point relative to |hwnd| to a client pixel point. |
| 54 // The DPI scale is performed relative to |hwnd| using an origin of (0, 0). |
| 55 static Point DIPToClientPoint(HWND hwnd, const Point& dip_point); |
| 56 |
| 57 // WARNING: There is no right way to scale sizes and rects. |
| 58 // Sometimes you may need the enclosing rect (which favors transformations |
| 59 // that stretch the bounds towards integral values) or the enclosed rect |
| 60 // (transformations that shrink the bounds towards integral values). |
| 61 // This implementation favors the enclosing rect. |
| 62 // |
| 63 // Understand which you need before blindly assuming this is the right way. |
| 64 |
| 65 // Converts a screen rect to a DIP rect. |
| 66 // The DPI scale is performed relative to the display nearest to |hwnd| |
| 67 // maintaining a constant origin on both Screen and DIP (same as Windows). |
| 68 // If |hwnd| is null, scaling will be performed to the display nearest to |
| 69 // |pixel_bounds|. |
| 70 static Rect ScreenToDIPRect(HWND hwnd, const Rect& pixel_bounds); |
| 71 |
| 72 // Converts a DIP rect to a screen rect. |
| 73 // The DPI scale is performed relative to the display nearest to |hwnd| |
| 74 // maintaining a constant origin on both Screen and DIP (same as Windows). |
| 75 // If |hwnd| is null, scaling will be performed to the display nearest to |
| 76 // |dip_bounds|. |
| 77 static Rect DIPToScreenRect(HWND hwnd, const Rect& dip_bounds); |
| 78 |
| 79 // Converts a client rect to a DIP rect. |
| 80 // The DPI scale is performed relative to |hwnd| using an origin of (0, 0). |
| 81 static Rect ClientToDIPRect(HWND hwnd, const Rect& pixel_bounds); |
| 82 |
| 83 // Converts a DIP rect to a client rect. |
| 84 // The DPI scale is performed relative to |hwnd| using an origin of (0, 0). |
| 85 static Rect DIPToClientRect(HWND hwnd, const Rect& dip_bounds); |
| 86 |
| 87 // Converts a screen size to a DIP size. |
| 88 // The DPI scale is performed relative to the display nearest to |hwnd|. |
| 89 static Size ScreenToDIPSize(HWND hwnd, const Size& size_in_pixels); |
| 90 |
| 91 // Converts a DIP size to a screen size. |
| 92 // The DPI scale is performed relative to the display nearest to |hwnd|. |
| 93 static Size DIPToScreenSize(HWND hwnd, const Size& dip_size); |
| 94 |
| 37 // Returns the HWND associated with the NativeView. | 95 // Returns the HWND associated with the NativeView. |
| 38 virtual HWND GetHWNDFromNativeView(NativeView window) const; | 96 virtual HWND GetHWNDFromNativeView(NativeView window) const; |
| 39 | 97 |
| 40 // Returns the NativeView associated with the HWND. | 98 // Returns the NativeView associated with the HWND. |
| 41 virtual NativeWindow GetNativeWindowFromHWND(HWND hwnd) const; | 99 virtual NativeWindow GetNativeWindowFromHWND(HWND hwnd) const; |
| 42 | 100 |
| 43 protected: | 101 protected: |
| 44 // gfx::Screen: | 102 // gfx::Screen: |
| 45 gfx::Point GetCursorScreenPoint() override; | 103 gfx::Point GetCursorScreenPoint() override; |
| 46 gfx::NativeWindow GetWindowUnderCursor() override; | 104 gfx::NativeWindow GetWindowUnderCursor() override; |
| 47 gfx::NativeWindow GetWindowAtScreenPoint(const gfx::Point& point) override; | 105 gfx::NativeWindow GetWindowAtScreenPoint(const gfx::Point& point) override; |
| 48 int GetNumDisplays() const override; | 106 int GetNumDisplays() const override; |
| 49 std::vector<gfx::Display> GetAllDisplays() const override; | 107 std::vector<gfx::Display> GetAllDisplays() const override; |
| 50 gfx::Display GetDisplayNearestWindow(gfx::NativeView window) const override; | 108 gfx::Display GetDisplayNearestWindow(gfx::NativeView window) const override; |
| 51 gfx::Display GetDisplayNearestPoint(const gfx::Point& point) const override; | 109 gfx::Display GetDisplayNearestPoint(const gfx::Point& point) const override; |
| 52 gfx::Display GetDisplayMatching(const gfx::Rect& match_rect) const override; | 110 gfx::Display GetDisplayMatching(const gfx::Rect& match_rect) const override; |
| 53 gfx::Display GetPrimaryDisplay() const override; | 111 gfx::Display GetPrimaryDisplay() const override; |
| 54 void AddObserver(DisplayObserver* observer) override; | 112 void AddObserver(DisplayObserver* observer) override; |
| 55 void RemoveObserver(DisplayObserver* observer) override; | 113 void RemoveObserver(DisplayObserver* observer) override; |
| 114 gfx::Rect ScreenToDIPRectInWindow( |
| 115 NativeView view, const gfx::Rect& screen_rect) const override; |
| 116 gfx::Rect DIPToScreenRectInWindow( |
| 117 NativeView view, const gfx::Rect& dip_rect) const override; |
| 56 | 118 |
| 57 void UpdateFromDisplayInfos( | 119 void UpdateFromDisplayInfos( |
| 58 const std::vector<gfx::win::DisplayInfo>& display_infos); | 120 const std::vector<gfx::win::DisplayInfo>& display_infos); |
| 59 | 121 |
| 60 // Virtual to support mocking by unit tests. | 122 // Virtual to support mocking by unit tests. |
| 61 virtual void Initialize(); | 123 virtual void Initialize(); |
| 62 virtual MONITORINFOEX MonitorInfoFromScreenPoint( | 124 virtual MONITORINFOEX MonitorInfoFromScreenPoint( |
| 63 const gfx::Point& screen_point) const; | 125 const gfx::Point& screen_point) const; |
| 64 virtual MONITORINFOEX MonitorInfoFromScreenRect(const gfx::Rect& screen_rect) | 126 virtual MONITORINFOEX MonitorInfoFromScreenRect(const gfx::Rect& screen_rect) |
| 65 const; | 127 const; |
| 66 virtual MONITORINFOEX MonitorInfoFromWindow(HWND hwnd, DWORD default_options) | 128 virtual MONITORINFOEX MonitorInfoFromWindow(HWND hwnd, DWORD default_options) |
| 67 const; | 129 const; |
| 68 virtual HWND GetRootWindow(HWND hwnd) const; | 130 virtual HWND GetRootWindow(HWND hwnd) const; |
| 69 | 131 |
| 70 private: | 132 private: |
| 71 void OnWndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam); | 133 void OnWndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam); |
| 72 | 134 |
| 73 // Returns the ScreenWinDisplay closest to or enclosing |hwnd|. | 135 // Returns the ScreenWinDisplay closest to or enclosing |hwnd|. |
| 74 gfx::win::ScreenWinDisplay GetScreenWinDisplayNearestHWND(HWND hwnd) const; | 136 gfx::win::ScreenWinDisplay GetScreenWinDisplayNearestHWND(HWND hwnd) const; |
| 75 | 137 |
| 76 // Returns the ScreenWinDisplay closest to or enclosing |screen_rect|. | 138 // Returns the ScreenWinDisplay closest to or enclosing |screen_rect|. |
| 77 gfx::win::ScreenWinDisplay GetScreenWinDisplayNearestScreenRect( | 139 gfx::win::ScreenWinDisplay GetScreenWinDisplayNearestScreenRect( |
| 78 const Rect& screen_rect) const; | 140 const Rect& screen_rect) const; |
| 79 | 141 |
| 80 // Returns the ScreenWinDisplay closest to or enclosing |screen_point|. | 142 // Returns the ScreenWinDisplay closest to or enclosing |screen_point|. |
| 81 gfx::win::ScreenWinDisplay GetScreenWinDisplayNearestScreenPoint( | 143 gfx::win::ScreenWinDisplay GetScreenWinDisplayNearestScreenPoint( |
| 82 const Point& screen_point) const; | 144 const Point& screen_point) const; |
| 83 | 145 |
| 146 // Returns the ScreenWinDisplay closest to or enclosing |dip_point|. |
| 147 gfx::win::ScreenWinDisplay GetScreenWinDisplayNearestDIPPoint( |
| 148 const Point& dip_point) const; |
| 149 |
| 150 // Returns the ScreenWinDisplay closest to or enclosing |dip_rect|. |
| 151 gfx::win::ScreenWinDisplay ScreenWin::GetScreenWinDisplayNearestDIPRect( |
| 152 const Rect& dip_rect) const; |
| 153 |
| 84 // Returns the ScreenWinDisplay corresponding to the primary monitor. | 154 // Returns the ScreenWinDisplay corresponding to the primary monitor. |
| 85 gfx::win::ScreenWinDisplay GetPrimaryScreenWinDisplay() const; | 155 gfx::win::ScreenWinDisplay GetPrimaryScreenWinDisplay() const; |
| 86 | 156 |
| 87 gfx::win::ScreenWinDisplay GetScreenWinDisplay( | 157 gfx::win::ScreenWinDisplay GetScreenWinDisplay( |
| 88 const MONITORINFOEX& monitor_info) const; | 158 const MONITORINFOEX& monitor_info) const; |
| 89 | 159 |
| 160 static float GetScaleFactorForHWND(HWND hwnd); |
| 161 |
| 162 // Returns the result of calling |getter| with |value| on the global |
| 163 // ScreenWin if it exists, otherwise return the default ScreenWinDisplay. |
| 164 template <typename Getter, typename GetterType> |
| 165 static gfx::win::ScreenWinDisplay GetScreenWinDisplayVia(Getter getter, |
| 166 GetterType value); |
| 167 |
| 90 // Helper implementing the DisplayObserver handling. | 168 // Helper implementing the DisplayObserver handling. |
| 91 gfx::DisplayChangeNotifier change_notifier_; | 169 gfx::DisplayChangeNotifier change_notifier_; |
| 92 | 170 |
| 93 scoped_ptr<SingletonHwndObserver> singleton_hwnd_observer_; | 171 scoped_ptr<SingletonHwndObserver> singleton_hwnd_observer_; |
| 94 | 172 |
| 95 // Current list of ScreenWinDisplays. | 173 // Current list of ScreenWinDisplays. |
| 96 std::vector<gfx::win::ScreenWinDisplay> screen_win_displays_; | 174 std::vector<gfx::win::ScreenWinDisplay> screen_win_displays_; |
| 97 | 175 |
| 98 DISALLOW_COPY_AND_ASSIGN(ScreenWin); | 176 DISALLOW_COPY_AND_ASSIGN(ScreenWin); |
| 99 }; | 177 }; |
| 100 | 178 |
| 101 } // namespace gfx | 179 } // namespace gfx |
| 102 | 180 |
| 103 #endif // UI_GFX_SCREEN_WIN_H_ | 181 #endif // UI_GFX_SCREEN_WIN_H_ |
| OLD | NEW |