Ocean
platform/wxwidgets/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_WXWIDGETS_UTILITIES_H
9 #define META_OCEAN_PLATFORM_WXWIDGETS_UTILITIES_H
10 
12 
13 #include "ocean/base/Frame.h"
14 
16 
17 namespace Ocean
18 {
19 
20 namespace Platform
21 {
22 
23 namespace WxWidgets
24 {
25 
26 /**
27  * This class implemnets utility functions for WxWidgets.
28  * @ingroup platformwxw
29  */
30 class OCEAN_PLATFORM_WXWIDGETS_EXPORT Utilities
31 {
32  public:
33 
34  /**
35  * Prints a text on the given device.
36  * @param dc Device receiving the text
37  * @param x Horizontal output position
38  * @param y Vertical output position
39  * @param text Text to be printed
40  */
41  static void textOutput(wxDC& dc, const int x, const int y, const wxString& text);
42 
43  /**
44  * Prints a text on the given device.
45  * @param dc Device receiving the text
46  * @param x Horizontal output position
47  * @param y Vertical output position
48  * @param text Text to be printed
49  */
50  static void textOutput(wxDC& dc, const int x, const int y, const std::string& text);
51 
52  /**
53  * Prints a text on the desktop.
54  * @param x Horizontal output position
55  * @param y Vertical output position
56  * @param text Text to be printed
57  */
58  static void desktopTextOutput(const int x, const int y, const wxString& text);
59 
60  /**
61  * Prints a text on the desktop.
62  * @param x Horizontal output position
63  * @param y Vertical output position
64  * @param text Text to be printed
65  */
66  static void desktopTextOutput(const int x, const int y, const std::string& text);
67 
68  /**
69  * Prints a bitmap on the given device.
70  * @param dc Device receiving the bitmap
71  * @param x Horizontal output position
72  * @param y Vertical output position
73  * @param bitmap Bitmap to be printed
74  */
75  static void bitmapOutput(wxDC& dc, const int x, const int y, const wxBitmap& bitmap);
76 
77  /**
78  * Prints a frame on the given device.
79  * @param dc Device receiving the bitmap
80  * @param x Horizontal output position
81  * @param y Vertical output position
82  * @param frame The frame to be printed
83  */
84  static void frameOutput(wxDC& dc, const int x, const int y, const Frame& frame);
85 
86  /**
87  * Prints a bitmap on the desktop.
88  * @param x Horizontal output position
89  * @param y Vertical output position
90  * @param bitmap Bitmap to be printed
91  */
92  static void desktopBitmapOutput(const int x, const int y, const wxBitmap& bitmap);
93 
94  /**
95  * Prints a frame on the desktop.
96  * @param x Horizontal output position
97  * @param y Vertical output position
98  * @param frame The frame to be printed
99  */
100  static void desktopFrameOutput(const int x, const int y, const Frame& frame);
101 
102  /**
103  * Prints a bitmap on the given device.
104  * @param dc Device receiving the bitmap
105  * @param xOutput Horizontal output position
106  * @param yOutput Vertical output position
107  * @param widthOutput Width of the output in pixel
108  * @param heightOutput Height of the output in pixel
109  * @param xInput Horizontal input position
110  * @param yInput Vertical input position
111  * @param widthInput Width of the input in pixel
112  * @param heightInput Height of the input in pixe
113  * @param bitmap Bitmap to be printed
114  */
115  static void bitmapOutput(wxDC& dc, const int xOutput, const int yOutput, const int widthOutput, const int heightOutput, const int xInput, const int yInput, const int widthInput, const int heightInput, const wxBitmap& bitmap);
116 
117  /**
118  * Determines the bounding box of a given string with specified font and font size.
119  * @param value The string for which the bounding box will be determined
120  * @param font The name of the font which will be applied
121  * @param size The size of the font
122  * @return The bounding box of the given string
123  */
124  static CV::PixelBoundingBox textBoundingBox(const std::string& value, const std::string& font = std::string(), const unsigned int size = 0u);
125 
126  /**
127  * Determines the bounding box of a given string with specified font and font size.
128  * @param value The string for which the bounding box will be determined
129  * @param font The name of the font which will be applied
130  * @param size The size of the font
131  * @return The bounding box of the given string
132  */
133  static CV::PixelBoundingBox textBoundingBox(const std::wstring& value, const std::wstring& font = std::wstring(), const unsigned int size = 0u);
134 
135  /**
136  * Converts a WxWidgets string value to an 8bit string.
137  * @param value Value to convert
138  * @return Converted string
139  */
140  static inline std::string toAString(const wxString& value);
141 
142  /**
143  * Converts a WxWidgets string value to a wstring.
144  * @param value Value to convert
145  * @return Converted string
146  */
147  static inline std::wstring toWString(const wxString& value);
148 };
149 
150 /**
151  * This class implements a scoped busy cursor object.
152  * @ingroup platformwxw
153  */
155 {
156  public:
157 
158  /**
159  * Creates a new scoped cursor object.
160  */
161  inline WxScopedBusyCursor();
162 
163  /**
164  * Destructs a scoped cursor.
165  */
166  inline ~WxScopedBusyCursor();
167 
168  /**
169  * Explicitly releases the busy cursor (already before the scope ends).
170  */
171  inline void release();
172 
173  protected:
174 
175  /**
176  * Disabled copy constructor.
177  * @param cursor The cursor that would be copied
178  */
179  WxScopedBusyCursor(const WxScopedBusyCursor& cursor) = delete;
180 
181  /**
182  * Disabled assign operator.
183  * @param cursor The cursor that would be assigned
184  */
186 
187  protected:
188 
189  /// True, if the cursor is still busy.
190  bool isBusy_ = true;
191 };
192 
193 /**
194  * This class implements a scoped object which disables a window object until the scope ends (or until the object is released explicitly).
195  * @ingroup platformwxw
196  */
198 {
199  public:
200 
201  /**
202  * Creates a new scoped object and disables the associated window.
203  * @param window The window to disable, this window must not be disposed before this scoped object is disposed
204  */
205  explicit inline WxScopedDisableWindow(wxWindow& window);
206 
207  /**
208  * Destructs this scoped object and enables the associated window.
209  */
210  inline ~WxScopedDisableWindow();
211 
212  /**
213  * Explicitly releases the scoped object and enables the associated window (already before the scope ends).
214  */
215  inline void release();
216 
217  protected:
218 
219  /**
220  * Disabled copy constructor.
221  * @param window The window that would be copied
222  */
224 
225  /**
226  * Disabled assign operator.
227  * @param window The window that would be assigned
228  */
230 
231  protected:
232 
233  /// The associated window, nullptr if the object has been released already.
234  wxWindow* window_ = nullptr;
235 };
236 
237 /**
238  * This class implements a scoped object which removes a given style from a given window until the scope ends (or until the object is released explicitly).
239  * @ingroup platformwxw
240  */
242 {
243  public:
244 
245  /**
246  * Creates a new scoped object and removes the given style from the given window if the window is not nullptr.
247  * @param window The window to remove the style from, this window must not be disposed before this scoped object is disposed, may be nullptr so that nothing will be done
248  * @param style The style to remove from the window
249  */
250  inline WxScopedRemoveStyle(wxWindow* window, const long style);
251 
252  /**
253  * Destructs this scoped object and assign the old style to the associated window.
254  */
255  inline ~WxScopedRemoveStyle();
256 
257  /**
258  * Explicitly releases the scoped object and assign the old style to the associated window (already before the scope ends).
259  */
260  inline void release();
261 
262  protected:
263 
264  /**
265  * Disabled copy constructor.
266  * @param style The style object that would be copied
267  */
268  WxScopedRemoveStyle(const WxScopedRemoveStyle& style) = delete;
269 
270  /**
271  * Disabled assign operator.
272  * @param style The style object that would be assigned
273  */
275 
276  protected:
277 
278  /// The associated window, nullptr if the object has been released already
279  wxWindow* window_ = nullptr;
280 
281  /// The old style of the associated window before the removing
282  long oldStyle_ = 0;
283 };
284 
285 inline std::string Utilities::toAString(const wxString& value)
286 {
287  if (value.IsEmpty())
288  {
289  return std::string();
290  }
291 
292 #if defined(wxABI_VERSION) && wxABI_VERSION <= 20899
293  return String::toAString(value);
294 #else
295  return std::string(value.c_str().AsChar());
296 #endif
297 }
298 
299 inline std::wstring Utilities::toWString(const wxString& value)
300 {
301  if (value.IsEmpty())
302  {
303  return std::wstring();
304 }
305 
306 #if defined(wxABI_VERSION) && wxABI_VERSION <= 20899
307  return std::wstring(value);
308 #else
309  return std::wstring(value.c_str().AsWChar());
310 #endif
311 }
312 
314 {
315  wxBeginBusyCursor();
316 }
317 
319 {
320  release();
321 }
322 
324 {
325  if (isBusy_)
326  {
327  wxEndBusyCursor();
328  isBusy_ = false;
329  }
330 }
331 
333  window_(&window)
334 {
335  ocean_assert(window_->IsEnabled());
336  window_->Enable(false);
337 }
338 
340 {
341  release();
342 }
343 
345 {
346  if (window_)
347  {
348  ocean_assert(!window_->IsEnabled());
349  window_->Enable(true);
350  window_ = nullptr;
351  }
352 }
353 
354 inline WxScopedRemoveStyle::WxScopedRemoveStyle(wxWindow* window, const long style) :
355  window_(window),
356  oldStyle_(0)
357 {
358  if (window_)
359  {
360  oldStyle_ = window_->GetWindowStyleFlag();
361  window_->SetWindowStyleFlag(oldStyle_ & ~style);
362  }
363 }
364 
366 {
367  release();
368 }
369 
371 {
372  if (window_)
373  {
374  window_->SetWindowStyleFlag(oldStyle_);
375  window_ = nullptr;
376  }
377 }
378 
379 }
380 
381 }
382 
383 }
384 
385 #endif // META_OCEAN_PLATFORM_WXWIDGETS_UTILITIES_H
This class implements Ocean's image class.
Definition: Frame.h:1792
This class implemnets utility functions for WxWidgets.
Definition: platform/wxwidgets/Utilities.h:31
static void desktopTextOutput(const int x, const int y, const wxString &text)
Prints a text on the desktop.
static void frameOutput(wxDC &dc, const int x, const int y, const Frame &frame)
Prints a frame on the given device.
static void bitmapOutput(wxDC &dc, const int xOutput, const int yOutput, const int widthOutput, const int heightOutput, const int xInput, const int yInput, const int widthInput, const int heightInput, const wxBitmap &bitmap)
Prints a bitmap on the given device.
static void textOutput(wxDC &dc, const int x, const int y, const wxString &text)
Prints a text on the given device.
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.
static void desktopTextOutput(const int x, const int y, const std::string &text)
Prints a text on the desktop.
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 bitmapOutput(wxDC &dc, const int x, const int y, const wxBitmap &bitmap)
Prints a bitmap on the given device.
static std::string toAString(const wxString &value)
Converts a WxWidgets string value to an 8bit string.
Definition: platform/wxwidgets/Utilities.h:285
static void textOutput(wxDC &dc, const int x, const int y, const std::string &text)
Prints a text on the given device.
static std::wstring toWString(const wxString &value)
Converts a WxWidgets string value to a wstring.
Definition: platform/wxwidgets/Utilities.h:299
static void desktopBitmapOutput(const int x, const int y, const wxBitmap &bitmap)
Prints a bitmap on the desktop.
static void desktopFrameOutput(const int x, const int y, const Frame &frame)
Prints a frame on the desktop.
This class implements a scoped busy cursor object.
Definition: platform/wxwidgets/Utilities.h:155
WxScopedBusyCursor(const WxScopedBusyCursor &cursor)=delete
Disabled copy constructor.
~WxScopedBusyCursor()
Destructs a scoped cursor.
Definition: platform/wxwidgets/Utilities.h:318
WxScopedBusyCursor()
Creates a new scoped cursor object.
Definition: platform/wxwidgets/Utilities.h:313
WxScopedBusyCursor & operator=(const WxScopedBusyCursor &cursor)=delete
Disabled assign operator.
bool isBusy_
True, if the cursor is still busy.
Definition: platform/wxwidgets/Utilities.h:190
void release()
Explicitly releases the busy cursor (already before the scope ends).
Definition: platform/wxwidgets/Utilities.h:323
This class implements a scoped object which disables a window object until the scope ends (or until t...
Definition: platform/wxwidgets/Utilities.h:198
wxWindow * window_
The associated window, nullptr if the object has been released already.
Definition: platform/wxwidgets/Utilities.h:234
WxScopedDisableWindow(wxWindow &window)
Creates a new scoped object and disables the associated window.
Definition: platform/wxwidgets/Utilities.h:332
WxScopedDisableWindow(const WxScopedDisableWindow &window)=delete
Disabled copy constructor.
void release()
Explicitly releases the scoped object and enables the associated window (already before the scope end...
Definition: platform/wxwidgets/Utilities.h:344
WxScopedDisableWindow & operator=(const WxScopedDisableWindow &window)=delete
Disabled assign operator.
~WxScopedDisableWindow()
Destructs this scoped object and enables the associated window.
Definition: platform/wxwidgets/Utilities.h:339
This class implements a scoped object which removes a given style from a given window until the scope...
Definition: platform/wxwidgets/Utilities.h:242
~WxScopedRemoveStyle()
Destructs this scoped object and assign the old style to the associated window.
Definition: platform/wxwidgets/Utilities.h:365
WxScopedRemoveStyle(const WxScopedRemoveStyle &style)=delete
Disabled copy constructor.
long oldStyle_
The old style of the associated window before the removing.
Definition: platform/wxwidgets/Utilities.h:282
void release()
Explicitly releases the scoped object and assign the old style to the associated window (already befo...
Definition: platform/wxwidgets/Utilities.h:370
wxWindow * window_
The associated window, nullptr if the object has been released already.
Definition: platform/wxwidgets/Utilities.h:279
WxScopedRemoveStyle(wxWindow *window, const long style)
Creates a new scoped object and removes the given style from the given window if the window is not nu...
Definition: platform/wxwidgets/Utilities.h:354
WxScopedRemoveStyle & operator=(const WxScopedRemoveStyle &style)=delete
Disabled assign operator.
static std::string toAString(const char value)
Converts a value to a string with 8bit character.
std::string toAString(System::String^ value)
Definition: platform/win/Utilities.h:319
std::wstring toWString(System::String^ value)
Definition: platform/win/Utilities.h:329
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15