Ocean
Loading...
Searching...
No Matches
platform/win/Utilities.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_UTILITIES_H
9#define META_OCEAN_PLATFORM_WIN_UTILITIES_H
10
13
14#include "ocean/base/Frame.h"
15
17
18#ifdef OCEAN_COMPILER_MSC
19 #pragma managed(push, off)
20#endif
21
22#include "ocean/base/String.h"
23
24#ifdef OCEAN_COMPILER_MSC
25 #pragma managed(pop)
26#endif
27
28namespace Ocean
29{
30
31namespace Platform
32{
33
34namespace Win
35{
36
37/**
38 * This class implements utilities and helper functions.
39 * @ingroup platformwin
40 */
41class OCEAN_PLATFORM_WIN_EXPORT Utilities
42{
43 public:
44
45 /**
46 * Definition of anchor positions for text rendering.
47 */
48 enum AnchorPosition : uint32_t
49 {
50 /// Text is anchored at the top-left corner
52 /// Text is anchored at the top-right corner
54 /// Text is anchored at the bottom-left corner
56 /// Text is anchored at the bottom-right corner
58 /// Text is centered
59 AP_CENTER
60 };
61
62 public:
63
64 /**
65 * Prints a text on the given device context.
66 * @param dc The device context on which the text will be drawn
67 * @param x Horizontal output position
68 * @param y Vertical output position
69 * @param text Text to be printed
70 */
71 static void textOutput(HDC dc, const int x, const int y, const std::string& text);
72
73 /**
74 * Draws styled text on a device context with customizable font, anchor position, and colors.
75 * The text can be positioned using anchor points relative to the window dimensions, with optional shadow for better visibility.
76 * @param dc The device context on which the text will be drawn
77 * @param text The text to be drawn
78 * @param font The name of the font to use (e.g., "Arial")
79 * @param fontSize The height of the font in pixels
80 * @param bold True to use bold font weight; False for normal weight
81 * @param anchorPosition The anchor position for the text
82 * @param windowWidth The width of the window, used for anchor calculations
83 * @param windowHeight The height of the window, used for anchor calculations
84 * @param foregroundColor The color of the text (RGB value)
85 * @param backgroundColor The background color of the text, or -1 for transparent background
86 * @param shadowColor The color of the shadow, or -1 to disable shadow
87 * @param shadowOffsetX The horizontal offset of the shadow in pixels, with range [0, infinity)
88 * @param shadowOffsetY The vertical offset of the shadow in pixels, with range [0, infinity)
89 * @param marginX The horizontal margin from the window edge in pixels, with range [0, infinity)
90 * @param marginY The vertical margin from the window edge in pixels, with range [0, infinity)
91 */
92 static void textOutput(HDC deviceContext, const std::wstring& text, const std::wstring& font, const unsigned int fontSize, const bool bold, const AnchorPosition anchorPosition, const unsigned int windowWidth, const unsigned int windowHeight, const int32_t foregroundColor, const int32_t backgroundColor = -1, const int32_t shadowColor = -1, const unsigned int shadowOffsetX = 2u, const unsigned int shadowOffsetY = 2u, const unsigned int marginX = 20u, const unsigned int marginY = 20u);
93
94 /**
95 * Prints a text on the desktop.
96 * @param x Horizontal output position
97 * @param y Vertical output position
98 * @param text Text to be printed
99 */
100 static void desktopTextOutput(const int x, const int y, const std::string& text);
101
102 /**
103 * Prints a frame on the given device context.
104 * @param dc Device context receiving the frame
105 * @param x Horizontal output position
106 * @param y Vertical output position
107 * @param frame The frame to be printed
108 */
109 static void frameOutput(HDC dc, const int x, const int y, const Frame& frame);
110
111 /**
112 * Prints a frame on the given device context.
113 * @param dc Device context receiving the text
114 * @param x Horizontal output position
115 * @param y Vertical output position
116 * @param width Output width in pixel
117 * @param height Output height in pixel
118 * @param frame The frame to be printed
119 */
120 static void frameOutput(HDC dc, const int x, const int y, const unsigned int width, const unsigned int height, const Frame& frame);
121
122 /**
123 * Prints a given frame on the Windows' main desktop at a specified location.
124 * You do not need to have a GUI application to use this function.<br>
125 * Call this function from any application and from any thread.<br>
126 * The painted frame will disappear immediately when Windows receives any repaint event.<br>
127 * This function is intended for debugging purposes only.
128 * @param x Horizontal output position, with range (-infinity, infinity)
129 * @param y Vertical output position, with range (-infinity, infinity)
130 * @param frame The frame to be printed
131 */
132 static void desktopFrameOutput(const int x, const int y, const Frame& frame);
133
134 /**
135 * Prints a given frame on the Windows' main desktop at a specified location.
136 * You do not need to have a GUI application to use this function.<br>
137 * Call this function from any application and from any thread.<br>
138 * The painted frame will disappear immediately when Windows receives any repaint event.<br>
139 * This function is intended for debugging purposes only.
140 * @param x Horizontal output position, with range (-infinity, infinity)
141 * @param y Vertical output position, with range (-infinity, infinity)
142 * @param scale The scale that is applied to the frame, a factor of 2 will increase the painted frame by two, with range [1, infinity)
143 * @param frame The frame to be printed
144 */
145 static inline void desktopFrameOutput(const int x, const int y, const unsigned int scale, const Frame& frame);
146
147 /**
148 * Prints a given frame on the Windows' main desktop at a specified location.
149 * You do not need to have a GUI application to use this function.<br>
150 * Call this function from any application and from any thread.<br>
151 * The painted frame will disappear immediately when Windows receives any repaint event.<br>
152 * This function is intended for debugging purposes only.
153 * @param x Horizontal output position, with range (-infinity, infinity)
154 * @param y Vertical output position, with range (-infinity, infinity)
155 * @param width The width of the painted frame in the domain of the desktop, in pixel, with range [1, infinity)
156 * @param height The height of the painted frame in the domain of the desktop, in pixel, with range [1, infinity)
157 * @param frame The frame to be printed
158 */
159 static void desktopFrameOutput(const int x, const int y, const unsigned int width, const unsigned int height, const Frame& frame);
160
161 /**
162 * Prints a bitmap on the given device context.
163 * @param dc Device context receiving the bitmap
164 * @param x Horizontal output position
165 * @param y Vertical output position
166 * @param bitmap bitmap to be printed
167 */
168 static void bitmapOutput(HDC dc, const int x, const int y, const Bitmap& bitmap);
169
170 /**
171 * Prints a bitmap on the desktop.
172 * @param x Horizontal output position
173 * @param y Vertical output position
174 * @param scale Scale of the output bitmap
175 * @param bitmap Bitmap to be printed
176 */
177 static inline void desktopBitmapOutput(const int x, const int y, const unsigned int scale, const Bitmap& bitmap);
178
179 /**
180 * Prints a bitmap on the given device context.
181 * @param dc Device context receiving the text
182 * @param x Horizontal output position
183 * @param y Vertical output position
184 * @param width Output width in pixel
185 * @param height Output height in pixel
186 * @param bitmap bitmap to be printed
187 */
188 static void bitmapOutput(HDC dc, const int x, const int y, const unsigned int width, const unsigned int height, const Bitmap& bitmap);
189
190 /**
191 * Prints a bitmap on the desktop.
192 * @param x Horizontal output position
193 * @param y Vertical output position
194 * @param bitmap bitmap to be printed
195 */
196 static void desktopBitmapOutput(const int x, const int y, const Bitmap& bitmap);
197
198 /**
199 * Prints a bitmap on the desktop.
200 * @param x Horizontal output position
201 * @param y Vertical output position
202 * @param width Output width in pixel
203 * @param height Output height in pixel
204 * @param bitmap bitmap to be printed
205 */
206 static void desktopBitmapOutput(const int x, const int y, const unsigned int width, const unsigned int height, const Bitmap& bitmap);
207
208 /**
209 * Determines the bounding box of a given string with specified font and font size.
210 * @param value The string for which the bounding box will be determined
211 * @param font The name of the font which will be applied
212 * @param size The size of the font
213 * @return The bounding box of the given string
214 */
215 static CV::PixelBoundingBox textBoundingBox(const std::string& value, const std::string& font = std::string(), const unsigned int size = 0u);
216
217 /**
218 * Determines the bounding box of a given string with specified font and font size.
219 * @param value The string for which the bounding box will be determined
220 * @param font The name of the font which will be applied
221 * @param size The size of the font
222 * @return The bounding box of the given string
223 */
224 static CV::PixelBoundingBox textBoundingBox(const std::wstring& value, const std::wstring& font = std::wstring(), const unsigned int size = 0u);
225};
226
227/**
228 * This class implements a nested scoped object which disables a window object until the scope of all nested elements ends (or until all nested object are released explicitly).
229 * The creation of the first nested object disables the window.<br>
230 * Following nested objects only increase the internal counter while do not have any further consequence.<br>
231 * @ingroup platformwin
232 */
234{
235 protected:
236
237 /**
238 * This class implements a simple counter for nested disable window objects.
239 */
240 class DisableWindowCounter : public Singleton<DisableWindowCounter>
241 {
242 friend class Singleton<DisableWindowCounter>;
244
245 protected:
246
247 /**
248 * Definition of a map mapping window handles to counters.
249 */
250 using CounterMap = std::unordered_map<HWND, unsigned int>;
251
252 protected:
253
254 /**
255 * Creates a new DisableWindowCounter object.
256 */
257 inline DisableWindowCounter();
258
259 /**
260 * Destructs a DisableWindowCounter object.
261 */
262 inline ~DisableWindowCounter();
263
264 /**
265 * Increases the counter of this object.
266 * @return True, if the counter was zero before the counter has been increased
267 */
268 inline bool increase(const HWND windowHandle);
269
270 /**
271 * Decreases the counter of this object.
272 * @return True, if the counter is zero after it has been decreased
273 */
274 inline bool decrease(const HWND windowHandle);
275
276 /**
277 * Returns whether the counter is zero.
278 * @return True, if so
279 */
280 inline bool isZero(const HWND windowHandle);
281
282 /**
283 * Returns the lock object.
284 * @return Lock object
285 */
286 inline Lock& lock();
287
288 protected:
289
290 /// Disable window counter.
292
293 /// Disable window lock.
295 };
296
297 public:
298
299 /**
300 * Creates a new scoped object and disables the associated window.
301 * @param windowHandle Handle of the window to disable, this window must not be disposed before this scoped object is disposed
302 */
303 explicit ScopedDisableWindow(const HWND windowHandle);
304
305 /**
306 * Destructs this scoped object and enables the associated window.
307 */
308 inline ~ScopedDisableWindow();
309
310 /**
311 * Explicitly releases the scoped object and enables the associated window (already before the scope ends).
312 */
313 void release();
314
315 protected:
316
317 /**
318 * Disabled copy constructor.
319 * @param window The window that would be copied
320 */
322
323 /**
324 * Disabled assign operator.
325 * @param window The window that would be assigned
326 */
328
329 protected:
330
331 /// The associated window, nullptr if the object has been released already.
332 HWND handle_ = nullptr;
333};
334
335#if defined(OCEAN_COMPILER_MSC) && defined(_MANAGED)
336
337inline System::String^ toString(const std::string& value)
338{
339 return gcnew System::String(value.c_str());
340}
341
342inline System::String^ toString(const char* value)
343{
344 return gcnew System::String(value);
345}
346
347inline System::String^ toString(const std::wstring& value)
348{
349 return gcnew System::String(value.c_str());
350}
351
352inline System::String^ toString(const wchar_t* value)
353{
354 return gcnew System::String(value);
355}
356
357inline std::string toAString(System::String^ value)
358{
359 char* str = (char*)(void*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(value);
360 std::string result(str);
361
362 System::Runtime::InteropServices::Marshal::FreeHGlobal(System::IntPtr(str));
363
364 return result;
365}
366
367inline std::wstring toWString(System::String^ value)
368{
369 char* str = (char*)(void*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(value);
370 std::string result(str);
371
372 System::Runtime::InteropServices::Marshal::FreeHGlobal(System::IntPtr(str));
373
374 return Ocean::String::toWString(result);
375}
376
377#endif // OCEAN_COMPILER_MSC && _MANAGED
378
379inline void Utilities::desktopFrameOutput(const int x, const int y, const unsigned int scale, const Frame& frame)
380{
381 desktopFrameOutput(x, y, frame.width() * scale, frame.height() * scale, frame);
382}
383
384inline void Utilities::desktopBitmapOutput(const int x, const int y, const unsigned int scale, const Bitmap& bitmap)
385{
386 desktopBitmapOutput(x, y, bitmap.width() * scale, bitmap.height() * scale, bitmap);
387}
388
390{
391 // nothing to do here
392}
393
395{
396#ifdef OCEAN_DEBUG
397
398 for (CounterMap::value_type& counterPair : counterMap_)
399 {
400 ocean_assert(counterPair.second == 0u);
401 }
402
403#endif
404}
405
406inline bool ScopedDisableWindow::DisableWindowCounter::increase(const HWND windowHandle)
407{
408 counterMap_[windowHandle]++;
409
410 return counterMap_[windowHandle] == 1u;
411}
412
413inline bool ScopedDisableWindow::DisableWindowCounter::decrease(const HWND windowHandle)
414{
415 ocean_assert(counterMap_.find(windowHandle) != counterMap_.end());
416 ocean_assert(counterMap_[windowHandle] >= 1u);
417
418 counterMap_[windowHandle]--;
419
420 return counterMap_[windowHandle] == 0u;
421}
422
423inline bool ScopedDisableWindow::DisableWindowCounter::isZero(const HWND windowHandle)
424{
425 return counterMap_[windowHandle] == 0u;
426}
427
429{
430 return windowLock_;
431}
432
437
438}
439
440}
441
442}
443
444#endif // META_OCEAN_PLATFORM_WIN_UTILITIES_H
This class implements Ocean's image class.
Definition Frame.h:1879
unsigned int width() const
Returns the width of the frame format in pixel.
Definition Frame.h:3241
unsigned int height() const
Returns the height of the frame in pixel.
Definition Frame.h:3246
This class implements a recursive lock object.
Definition Lock.h:31
This class implements a Windows device independent bitmap.
Definition win/Bitmap.h:30
unsigned int width() const
Returns the width of the bitmap.
Definition win/Bitmap.h:267
unsigned int height() const
Returns the height of the bitmap.
Definition win/Bitmap.h:272
This class implements a simple counter for nested disable window objects.
Definition platform/win/Utilities.h:241
CounterMap counterMap_
Disable window counter.
Definition platform/win/Utilities.h:291
~DisableWindowCounter()
Destructs a DisableWindowCounter object.
Definition platform/win/Utilities.h:394
Lock windowLock_
Disable window lock.
Definition platform/win/Utilities.h:294
bool isZero(const HWND windowHandle)
Returns whether the counter is zero.
Definition platform/win/Utilities.h:423
DisableWindowCounter()
Creates a new DisableWindowCounter object.
Definition platform/win/Utilities.h:389
std::unordered_map< HWND, unsigned int > CounterMap
Definition of a map mapping window handles to counters.
Definition platform/win/Utilities.h:250
Lock & lock()
Returns the lock object.
Definition platform/win/Utilities.h:428
bool increase(const HWND windowHandle)
Increases the counter of this object.
Definition platform/win/Utilities.h:406
bool decrease(const HWND windowHandle)
Decreases the counter of this object.
Definition platform/win/Utilities.h:413
This class implements a nested scoped object which disables a window object until the scope of all ne...
Definition platform/win/Utilities.h:234
ScopedDisableWindow(const ScopedDisableWindow &window)=delete
Disabled copy constructor.
HWND handle_
The associated window, nullptr if the object has been released already.
Definition platform/win/Utilities.h:332
~ScopedDisableWindow()
Destructs this scoped object and enables the associated window.
Definition platform/win/Utilities.h:433
ScopedDisableWindow(const HWND windowHandle)
Creates a new scoped object and disables the associated window.
ScopedDisableWindow & operator=(const ScopedDisableWindow &window)=delete
Disabled assign operator.
void release()
Explicitly releases the scoped object and enables the associated window (already before the scope end...
This class implements utilities and helper functions.
Definition platform/win/Utilities.h:42
static void bitmapOutput(HDC dc, const int x, const int y, const unsigned int width, const unsigned int height, const Bitmap &bitmap)
Prints a bitmap on the given device context.
static void frameOutput(HDC dc, const int x, const int y, const Frame &frame)
Prints a frame on the given device context.
static void frameOutput(HDC dc, const int x, const int y, const unsigned int width, const unsigned int height, const Frame &frame)
Prints a frame on the given device context.
static void desktopFrameOutput(const int x, const int y, const Frame &frame)
Prints a given frame on the Windows' main desktop at a specified location.
static void textOutput(HDC dc, const int x, const int y, const std::string &text)
Prints a text on the given device context.
static CV::PixelBoundingBox textBoundingBox(const std::string &value, const std::string &font=std::string(), const unsigned int size=0u)
Determines the bounding box of a given string with specified font and font size.
static void desktopTextOutput(const int x, const int y, const std::string &text)
Prints a text on the desktop.
static void textOutput(HDC deviceContext, const std::wstring &text, const std::wstring &font, const unsigned int fontSize, const bool bold, const AnchorPosition anchorPosition, const unsigned int windowWidth, const unsigned int windowHeight, const int32_t foregroundColor, const int32_t backgroundColor=-1, const int32_t shadowColor=-1, const unsigned int shadowOffsetX=2u, const unsigned int shadowOffsetY=2u, const unsigned int marginX=20u, const unsigned int marginY=20u)
Draws styled text on a device context with customizable font, anchor position, and colors.
static CV::PixelBoundingBox textBoundingBox(const std::wstring &value, const std::wstring &font=std::wstring(), const unsigned int size=0u)
Determines the bounding box of a given string with specified font and font size.
AnchorPosition
Definition of anchor positions for text rendering.
Definition platform/win/Utilities.h:49
@ AP_BOTTOM_RIGHT
Text is anchored at the bottom-right corner.
Definition platform/win/Utilities.h:57
@ AP_BOTTOM_LEFT
Text is anchored at the bottom-left corner.
Definition platform/win/Utilities.h:55
@ AP_TOP_RIGHT
Text is anchored at the top-right corner.
Definition platform/win/Utilities.h:53
@ AP_TOP_LEFT
Text is anchored at the top-left corner.
Definition platform/win/Utilities.h:51
static void desktopBitmapOutput(const int x, const int y, const unsigned int width, const unsigned int height, const Bitmap &bitmap)
Prints a bitmap on the desktop.
static void bitmapOutput(HDC dc, const int x, const int y, const Bitmap &bitmap)
Prints a bitmap on the given device context.
static void desktopFrameOutput(const int x, const int y, const unsigned int width, const unsigned int height, const Frame &frame)
Prints a given frame on the Windows' main desktop at a specified location.
static void desktopBitmapOutput(const int x, const int y, const Bitmap &bitmap)
Prints a bitmap on the desktop.
static void desktopBitmapOutput(const int x, const int y, const unsigned int scale, const Bitmap &bitmap)
Prints a bitmap on the desktop.
Definition platform/win/Utilities.h:384
This template class is the base class for all singleton objects.
Definition Singleton.h:71
static std::wstring toWString(const char value)
Converts a value to a wstring.
std::string toAString(System::String^ value)
Definition platform/win/Utilities.h:357
std::wstring toWString(System::String^ value)
Definition platform/win/Utilities.h:367
System::String toString(const std::string &value)
Definition platform/win/Utilities.h:337
The namespace covering the entire Ocean framework.
Definition Accessor.h:15