Ocean
Window.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_WINDOW_H
9 #define META_OCEAN_PLATFORM_WIN_WINDOW_H
10 
11 #include "ocean/platform/win/Win.h"
12 
13 #include "ocean/base/Lock.h"
14 
15 #include <map>
16 
17 namespace Ocean
18 {
19 
20 namespace Platform
21 {
22 
23 namespace Win
24 {
25 
26 /**
27  * This class is the base class for all windows.
28  * @ingroup platformwin
29  */
30 class OCEAN_PLATFORM_WIN_EXPORT Window
31 {
32  public:
33 
34  /**
35  * Definition of several mouse buttons.
36  */
38  {
39  /// No mouse button
41  /// Left mouse button
43  /// Middle mouse button
45  /// Right mouse button
46  BUTTON_RIGHT = 4
47  };
48 
49  /**
50  * Definition of a vector holding files.
51  */
52  typedef std::vector<std::wstring> Files;
53 
54  protected:
55 
56  /**
57  * Map mapping class names to an reference counter.
58  */
59  typedef std::unordered_map<std::wstring, unsigned int> ClassMap;
60 
61  public:
62 
63  /**
64  * Initializes the window.
65  * @param icon The optional handle of the icon to be used, nullptr to use default icon
66  * @param windowClass The name of the window class
67  * @return True, if succeeded
68  */
69  virtual bool initialize(const HICON icon = nullptr, const std::string& windowClass = "window");
70 
71  /**
72  * Initializes the window.
73  * @param applicationInstance The instance of the application, must be valid
74  * @param iconId The id of the icon as specified in the resources, with range [0, infinity)
75  * @param windowClass The name of the window class
76  * @return True, if succeeded
77  */
78  inline bool initialize(const HINSTANCE applicationInstance, const int iconId, const std::string& windowClass = "window");
79 
80  /**
81  * Shows the window.
82  */
83  virtual void show();
84 
85  /**
86  * Hides the window.
87  */
88  virtual void hide();
89 
90  /**
91  * Updates the window.
92  */
93  virtual void update();
94 
95  /**
96  * Repaints the window.
97  * @param eraseBackground True, to erase the background
98  */
99  virtual void repaint(const bool eraseBackground = false);
100 
101  /**
102  * Moves the window.
103  * @param x The new horizontal position
104  * @param y The new vertical position
105  */
106  bool move(const int x, const int y);
107 
108  /**
109  * Resizes the window.
110  * @param width New window width
111  * @param height New window height
112  * @return True, if succeeded
113  */
114  bool resize(const unsigned int width, const unsigned int height);
115 
116  /**
117  * Returns the application instance the window belongs to.
118  * @return Application instance
119  */
120  inline HINSTANCE applicationInstance() const;
121 
122  /**
123  * Returns the name of this window.
124  * @return Window name
125  */
126  inline const std::wstring& name() const;
127 
128  /**
129  * Returns the handle of this window.
130  * @return Window handle
131  */
132  inline HWND handle() const;
133 
134  /**
135  * Returns the handle of a possible parent window.
136  * @return Parent window handle, nullptr otherwise
137  */
138  inline HWND parentHandle() const;
139 
140  /**
141  * Returns the device context of this window.
142  * @return Device context
143  */
144  inline HDC dc() const;
145 
146  /**
147  * Returns the width of the entire window.
148  * @return Width in pixel
149  */
150  unsigned int width() const;
151 
152  /**
153  * Returns the height of the window.
154  * @return Height in pixel
155  */
156  unsigned int height() const;
157 
158  /**
159  * Returns the width of the client window.
160  * @return Width in pixel
161  */
162  unsigned int clientWidth() const;
163 
164  /**
165  * Returns the height of the client window.
166  * @return Height in pixel
167  */
168  unsigned int clientHeight() const;
169 
170  /**
171  * Sets the parent window handle.
172  * @return True, if succeeded
173  */
174  bool setParent(const HWND parent);
175 
176  /**
177  * Sets or changes the text of this windows.
178  * @param text The text to be set
179  */
180  void setText(const std::wstring& text);
181 
182  /**
183  * Enables or disables drag&drop support for files.
184  * @param state True, to enable support for drag&drop of file for this window; False, to disable drag&drop.
185  * @see onDragAndDrop().
186  */
187  void setEnableDropAndDrop(const bool state = true);
188 
189  protected:
190 
191  /**
192  * Creates a new window.
193  * Beware: If a derived window class will use a different window class as the default one you have to change the window class name in the constructor.
194  * @param applicationInstance The instance of the application, must be valid
195  * @param name The name of the application window
196  * @param parent Possible parent window making this window to a child window
197  * @param isChild True, if the window is intended to be a child window
198  */
199  Window(HINSTANCE applicationInstance, const std::wstring& name, const HWND parent = nullptr, const bool isChild = false);
200 
201  /**
202  * Destructs a window.
203  */
204  virtual ~Window();
205 
206  /**
207  * Registers a new windows class for the application window.
208  * @param icon The optional handle of the icon to be used, nullptr to use default icon
209  * @return True, if succeeded
210  */
211  virtual bool registerWindowClass(const HICON icon = nullptr);
212 
213  /**
214  * Creates the window itself using the registered window class.
215  * @return True, if succeeded
216  */
217  virtual bool createWindow();
218 
219  /**
220  * Allows the modification of the window class before registration.<br>
221  * Overload this function to change the window type.<br>
222  * However do not change the class name.<br>
223  * @param windowClass Window class to modify
224  */
225  virtual void modifyWindowClass(WNDCLASSW& windowClass);
226 
227  /**
228  * Allows the modification of the window style before creation.<br>
229  * Overload this function to change the window style.
230  * @param windowStyle Window style to modify
231  * @param windowLeft Left position of the window
232  * @param windowTop Top position of the window
233  * @param windowWidth Width of the window
234  * @param windowHeight Height of the window
235  */
236  virtual void modifyWindowStyle(DWORD& windowStyle, int& windowLeft, int& windowTop, int& windowWidth, int& windowHeight);
237 
238  /**
239  * Event function if the windows has been initialized successfully.
240  */
241  virtual void onInitialized();
242 
243  /**
244  * Event function for an activate event.
245  * @param active True, if the window is activated, false if the window is inactivated
246  */
247  virtual void onActivate(const bool active);
248 
249  /**
250  * Event function to repaint the window.
251  */
252  virtual void onPaint();
253 
254  /**
255  * Function called by the windows message loop if the process is idling.
256  */
257  virtual void onIdle();
258 
259  /**
260  * Function for a show event.
261  * @param visible True, if the window is being shown, false if the window is being hidden
262  */
263  virtual void onShow(const bool visible);
264 
265  /**
266  * Function for a destroy event.
267  */
268  virtual void onDestroy();
269 
270  /**
271  * Function for window resize event.
272  * @param clientWidth New client width
273  * @param clientHeight New client height
274  */
275  virtual void onResize(const unsigned int clientWidth, const unsigned int clientHeight);
276 
277  /**
278  * Function for keyboard button down events.
279  * @param key Specifies the key button
280  */
281  virtual void onKeyDown(const int key);
282 
283  /**
284  * Function for mouse double click events.
285  * @param button Specifies the mouse button
286  * @param x Specifies the x position of the cursor
287  * @param y Specifies the y position of the cursor
288  */
289  virtual void onMouseDoubleClick(const MouseButton button, const int x, const int y);
290 
291  /**
292  * Function for mouse button down events.
293  * @param button Specifies the mouse button
294  * @param x Specifies the x position of the cursor
295  * @param y Specifies the y position of the cursor
296  */
297  virtual void onMouseDown(const MouseButton button, const int x, const int y);
298 
299  /**
300  * Function for mouse move event.
301  * @param buttons Holds all pushed mouse buttons
302  * @param x Specifies the x position of the cursor
303  * @param y Specifies the y position of the cursor
304  */
305  virtual void onMouseMove(const MouseButton buttons, const int x, const int y);
306 
307  /**
308  * Function for keyboard button up events.
309  * @param key Specifies the key button
310  */
311  virtual void onKeyUp(const int key);
312 
313  /**
314  * Function for mouse button up events.
315  * @param button Specifies the mouse button
316  * @param x Specifies the x position of the cursor
317  * @param y Specifies the y position of the cursor
318  */
319  virtual void onMouseUp(const MouseButton button, const int x, const int y);
320 
321  /**
322  * Functions for mouse wheel events.
323  * @param buttons Holds all pushed mouse buttons
324  * @param wheel Specifies the wheel delta value
325  * @param x Specifies the x position of the cursor
326  * @param y Specifies the y position of the cursor
327  */
328  virtual void onMouseWheel(const MouseButton buttons, const int wheel, const int x, const int y);
329 
330  /**
331  * Functions for window minimize event.
332  */
333  virtual void onMinimize();
334 
335  /**
336  * Event functions for drag&drop events for files.
337  * @param files The files which have been dropped
338  * @see setEnableDropAndDrop().
339  */
340  virtual void onDragAndDrop(const Files& files);
341 
342  /**
343  * Window message / event procedure.
344  * @param hwnd Window handle
345  * @param uMsg Message id
346  * @param wParam High message parameter
347  * @param lParam Low message parameter
348  */
349  static LRESULT CALLBACK windowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
350 
351  /**
352  * Returns the class map for all windows.
353  * @return The window class map
354  */
355  static ClassMap& classMap();
356 
357  /**
358  * Returns the lock of the window class map.
359  * @return The window class map's lock
360  */
361  static Lock& classMapLock();
362 
363  protected:
364 
365  /// Window handle of a possible parent window, making this window to a child window.
366  HWND parentHandle_ = nullptr;
367 
368  /// Window class name.
369  std::wstring className_;
370 
371  /// Application instance.
372  HINSTANCE applicationInstance_ = nullptr;
373 
374  /// Name of the window.
375  std::wstring name_;
376 
377  /// Window handle.
378  HWND handle_ = nullptr;
379 
380  /// Window device context.
381  HDC dc_ = nullptr;
382 
383  /// True, if the window is a child window.
384  bool isChild_ = false;
385 };
386 
387 inline bool Window::initialize(const HINSTANCE applicationInstance, const int iconId, const std::string& windowClass)
388 {
389  ocean_assert(applicationInstance != nullptr);
390  ocean_assert(iconId >= 0);
391 
392  const HICON hIcon = LoadIcon(applicationInstance, MAKEINTRESOURCE(iconId));
393 
394  return initialize(hIcon, windowClass);
395 }
396 
397 inline HINSTANCE Window::applicationInstance() const
398 {
399  return applicationInstance_;
400 }
401 
402 inline const std::wstring& Window::name() const
403 {
404  return name_;
405 }
406 
407 inline HWND Window::handle() const
408 {
409  return handle_;
410 }
411 
412 inline HWND Window::parentHandle() const
413 {
414  return parentHandle_;
415 }
416 
417 inline HDC Window::dc() const
418 {
419  return dc_;
420 }
421 
422 }
423 
424 }
425 
426 }
427 
428 #endif // META_OCEAN_PLATFORM_WIN_WINDOW_H
This class implements a recursive lock object.
Definition: Lock.h:31
This class is the base class for all windows.
Definition: Window.h:31
virtual void onMouseDoubleClick(const MouseButton button, const int x, const int y)
Function for mouse double click events.
virtual void onIdle()
Function called by the windows message loop if the process is idling.
Window(HINSTANCE applicationInstance, const std::wstring &name, const HWND parent=nullptr, const bool isChild=false)
Creates a new window.
virtual void onMouseDown(const MouseButton button, const int x, const int y)
Function for mouse button down events.
virtual void onDestroy()
Function for a destroy event.
virtual void update()
Updates the window.
virtual void repaint(const bool eraseBackground=false)
Repaints the window.
std::wstring className_
Window class name.
Definition: Window.h:369
virtual void onActivate(const bool active)
Event function for an activate event.
HWND parentHandle_
Window handle of a possible parent window, making this window to a child window.
Definition: Window.h:366
virtual void show()
Shows the window.
virtual bool initialize(const HICON icon=nullptr, const std::string &windowClass="window")
Initializes the window.
unsigned int height() const
Returns the height of the window.
HWND handle() const
Returns the handle of this window.
Definition: Window.h:407
static Lock & classMapLock()
Returns the lock of the window class map.
HINSTANCE applicationInstance_
Application instance.
Definition: Window.h:372
virtual void onKeyDown(const int key)
Function for keyboard button down events.
HDC dc() const
Returns the device context of this window.
Definition: Window.h:417
virtual bool createWindow()
Creates the window itself using the registered window class.
virtual void hide()
Hides the window.
virtual void onPaint()
Event function to repaint the window.
virtual void onMouseWheel(const MouseButton buttons, const int wheel, const int x, const int y)
Functions for mouse wheel events.
virtual bool registerWindowClass(const HICON icon=nullptr)
Registers a new windows class for the application window.
virtual void modifyWindowClass(WNDCLASSW &windowClass)
Allows the modification of the window class before registration.
std::vector< std::wstring > Files
Definition of a vector holding files.
Definition: Window.h:52
virtual void onMinimize()
Functions for window minimize event.
void setEnableDropAndDrop(const bool state=true)
Enables or disables drag&drop support for files.
unsigned int clientHeight() const
Returns the height of the client window.
bool move(const int x, const int y)
Moves the window.
static LRESULT CALLBACK windowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
Window message / event procedure.
bool setParent(const HWND parent)
Sets the parent window handle.
virtual void onResize(const unsigned int clientWidth, const unsigned int clientHeight)
Function for window resize event.
virtual void modifyWindowStyle(DWORD &windowStyle, int &windowLeft, int &windowTop, int &windowWidth, int &windowHeight)
Allows the modification of the window style before creation.
virtual void onKeyUp(const int key)
Function for keyboard button up events.
HWND handle_
Window handle.
Definition: Window.h:378
unsigned int clientWidth() const
Returns the width of the client window.
std::unordered_map< std::wstring, unsigned int > ClassMap
Map mapping class names to an reference counter.
Definition: Window.h:59
virtual void onInitialized()
Event function if the windows has been initialized successfully.
bool resize(const unsigned int width, const unsigned int height)
Resizes the window.
void setText(const std::wstring &text)
Sets or changes the text of this windows.
HINSTANCE applicationInstance() const
Returns the application instance the window belongs to.
Definition: Window.h:397
virtual void onMouseMove(const MouseButton buttons, const int x, const int y)
Function for mouse move event.
virtual ~Window()
Destructs a window.
HWND parentHandle() const
Returns the handle of a possible parent window.
Definition: Window.h:412
virtual void onShow(const bool visible)
Function for a show event.
std::wstring name_
Name of the window.
Definition: Window.h:375
virtual void onDragAndDrop(const Files &files)
Event functions for drag&drop events for files.
HDC dc_
Window device context.
Definition: Window.h:381
virtual void onMouseUp(const MouseButton button, const int x, const int y)
Function for mouse button up events.
MouseButton
Definition of several mouse buttons.
Definition: Window.h:38
unsigned int width() const
Returns the width of the entire window.
const std::wstring & name() const
Returns the name of this window.
Definition: Window.h:402
static ClassMap & classMap()
Returns the class map for all windows.
@ BUTTON_MIDDLE
Middle button.
Definition: SceneDescription.h:82
@ BUTTON_NONE
Definition: SceneDescription.h:78
@ BUTTON_RIGHT
Right button.
Definition: SceneDescription.h:84
@ BUTTON_LEFT
Left button.
Definition: SceneDescription.h:80
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15