Ocean
win/Screen.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_PLATFORM_WIN_SCREEN_H
9 #define META_OCEAN_PLATFORM_WIN_SCREEN_H
10 
11 #include "ocean/platform/win/Win.h"
12 
14 
15 namespace Ocean
16 {
17 
18 namespace Platform
19 {
20 
21 namespace Win
22 {
23 
24 /**
25  * This class provides screen functionalities.
26  * @ingroup platformwin
27  */
28 class OCEAN_PLATFORM_WIN_EXPORT Screen
29 {
30  public:
31 
32  /**
33  * Returns the width of the primary display in pixel.
34  * @return Primary display width in pixel
35  */
36  static int primaryDisplayWidth();
37 
38  /**
39  * Returns the height of the primary display in pixel.
40  * @return Primary display height in pixel
41  */
42  static int primaryDisplayHeight();
43 
44  /**
45  * Returns the width of the virtual display in pixel.
46  * The virtual display is bounded by all screens.
47  * @return Virtual display width in pixel
48  */
49  static int virtualDisplayWidth();
50 
51  /**
52  * Returns the height of the virtual display in pixel.
53  * The virtual display is bounded by all screens.
54  * @return Virtual display height in pixel
55  */
56  static int virtualDisplayHeight();
57 
58  /**
59  * Returns the bounding box of the virtual display with pixel resolution.
60  * @return The bounding box of the virtual display
61  */
63 
64  /**
65  * Returns the most suitable top left position of a window that has to be placed on the (virtual) screen.
66  * The final position ensures that the window lies inside the bounding box of the (virtual) screen.
67  * @param windowWidth The width of the window for which the position will be determined in pixel, with range (0, infinity)
68  * @param windowHeight The height of the window for which the pose will be determined in pixel, with range (0, infinity)
69  * @param left Optional proposed left position of the window, which will be adjusted to fit into the virtual screen
70  * @param top Optional proposed top position of the window, which will be adjusted to fit into the virtual screen
71  * @param parent Optional handle of a parent (or associated) window to which the resulting position should match if no proposed position is provided
72  * @return The resulting top left position of the window
73  */
74  static CV::PixelPositionI suitableWindowPosition(const unsigned int windowWidth, const unsigned int windowHeight, const int left = NumericT<int>::minValue(), const int top = NumericT<int>::minValue(), const HWND parent = nullptr);
75 
76  /**
77  * Returns the number of installed screens.
78  * @return Screen number
79  */
80  static unsigned int screens();
81 
82  /**
83  * Returns the position and dimension of the screen most suitable for a given cursor point.
84  * @param positionX Horizontal cursor position to receive the screen for
85  * @param positionY Vertical cursor position to receive the screen for
86  * @param left Left screen position, pixel in virtual coordinate space
87  * @param top Top screen position, pixel in virtual coordinate space
88  * @param width Screen width, in pixel
89  * @param height Screen height, in pixel
90  * @return True, if succeeded
91  */
92  static bool screen(const unsigned int positionX, const unsigned int positionY, unsigned int& left, unsigned int& top, unsigned int& width, unsigned int& height);
93 
94  /**
95  * Returns the position and dimension of the screen most suitable for a given window handle.
96  * @param window Window handle to receive the screen for
97  * @param left Left screen position, pixel in virtual coordinate space
98  * @param top Top screen position, pixel in virtual coordinate space
99  * @param width Screen width, in pixel
100  * @param height Screen height, in pixel
101  * @return True, if succeeded
102  */
103  static bool screen(const HWND window, unsigned int& left, unsigned int& top, unsigned int& width, unsigned int& height);
104 
105  /**
106  * Returns the current DPI value of a given device context.
107  * If no context is given, the desktop DC is used instead.
108  * A DPI value of 96 complies with a 100 percent scaling, 192 DPI complies with 200 percent scaling
109  * @param dc The device context, nullptr to determine the scale factor of the default screen
110  * @return The resulting DPI value, with range (0, infinity)
111  */
112  static int dpi(const HDC dc);
113 
114  /**
115  * Returns the scaling factor transforming the number of (backing) pixels defined in the (virtual) display coordinate system to the number of pixels defined in the (native/physical) screen coordinate system for a specified device context.
116  * @param dc The device context, nullptr to determine the scale factor of the default screen
117  * @return The resulting scale factor with range (0, infinity)
118  */
119  static double scaleFactor(const HDC dc);
120 
121  /**
122  * Converts a given pixel value defined in the (virtual) display coordinate system to the pixel value defined in the (native/physical) screen coordinate system.
123  * @param pixel Pixel value to convert, with range (-infinity, infinity)
124  * @param dc The device context, nullptr to determine the scale factor of the default screen
125  * @return The converted pixel value for the (native/pyhsical) screen coordinate system, with range (-infinity, infinity)
126  */
127  static inline int scalePixel(const int pixel, const HDC dc);
128 
129  /**
130  * Converts a given pixel value by a given scale factor.
131  * @param pixel Pixel value to convert, with range (-infinity, infinity)
132  * @param scaleFactor The scale factor to be applied, with range (-infinity, infinity)
133  * @return The resulting converted pixel value (-infinity, infinity)
134  */
135  static inline int scalePixelByFactor(const int pixel, const double scaleFactor);
136 };
137 
138 inline int Screen::scalePixel(const int pixel, const HDC dc)
139 {
140  return scalePixelByFactor(pixel, scaleFactor(dc));
141 }
142 
143 inline int Screen::scalePixelByFactor(const int pixel, const double scaleFactor)
144 {
145  return NumericD::round32(double(pixel) * scaleFactor);
146 }
147 
148 }
149 
150 }
151 
152 }
153 
154 #endif // META_OCEAN_PLATFORM_WIN_SCREEN_H
This class implements a 2D bounding box with pixel precision.
Definition: PixelBoundingBox.h:57
This class implements a 2D pixel position with pixel precision.
Definition: PixelPosition.h:65
This class provides basic numeric functionalities.
Definition: Numeric.h:57
static constexpr int32_t round32(const T value)
Returns the rounded 32 bit integer value of a given value.
Definition: Numeric.h:2064
This class provides screen functionalities.
Definition: win/Screen.h:29
static int virtualDisplayHeight()
Returns the height of the virtual display in pixel.
static unsigned int screens()
Returns the number of installed screens.
static int primaryDisplayWidth()
Returns the width of the primary display in pixel.
static bool screen(const unsigned int positionX, const unsigned int positionY, unsigned int &left, unsigned int &top, unsigned int &width, unsigned int &height)
Returns the position and dimension of the screen most suitable for a given cursor point.
static int scalePixel(const int pixel, const HDC dc)
Converts a given pixel value defined in the (virtual) display coordinate system to the pixel value de...
Definition: win/Screen.h:138
static int virtualDisplayWidth()
Returns the width of the virtual display in pixel.
static int dpi(const HDC dc)
Returns the current DPI value of a given device context.
static int scalePixelByFactor(const int pixel, const double scaleFactor)
Converts a given pixel value by a given scale factor.
Definition: win/Screen.h:143
static double scaleFactor(const HDC dc)
Returns the scaling factor transforming the number of (backing) pixels defined in the (virtual) displ...
static CV::PixelBoundingBoxI virtualDisplayBoundingBox()
Returns the bounding box of the virtual display with pixel resolution.
static bool screen(const HWND window, unsigned int &left, unsigned int &top, unsigned int &width, unsigned int &height)
Returns the position and dimension of the screen most suitable for a given window handle.
static int primaryDisplayHeight()
Returns the height of the primary display in pixel.
static CV::PixelPositionI suitableWindowPosition(const unsigned int windowWidth, const unsigned int windowHeight, const int left=NumericT< int >::minValue(), const int top=NumericT< int >::minValue(), const HWND parent=nullptr)
Returns the most suitable top left position of a window that has to be placed on the (virtual) screen...
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15