MRPT  2.0.4
CBaseGUIWindow.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
12 #include <mrpt/gui/gui_frwds.h>
13 #include <mrpt/gui/keycodes.h>
14 #include <mrpt/img/TPixelCoord.h>
17 #include <mrpt/system/mrptEvent.h>
18 #include <atomic>
19 #include <future>
20 #include <mutex>
21 
22 namespace mrpt::gui
23 {
24 /** The base class for GUI window classes based on wxWidgets.
25  *
26  * This class can be observed (see mrpt::system::CObserver) for the following
27  * events (see mrpt::system::mrptEvent):
28  * - mrpt::gui::mrptEventWindowChar
29  * - mrpt::gui::mrptEventWindowResize
30  * - mrpt::gui::mrptEventMouseDown
31  * - mrpt::gui::mrptEventWindowClosed
32  *
33  * See derived classes to check if they emit other additional events.
34  *
35  * IMPORTANTE NOTICE: Event handlers in your observer class will be invoked
36  * from the wxWidgets internal MRPT thread, so all your code in the handler
37  * must be thread safe.
38  * \ingroup mrpt_gui_grp
39  */
41 {
42  friend class CWindowDialog;
43  friend class C3DWindowDialog;
44  friend class CWindowDialogPlots;
45 
46  private:
47  /** can be 200,300,400... See WxSubsystem */
48  const int m_CMD_CREATE_WIN;
49  /** can be 299,399,499... See WxSubsystem */
50  const int m_CMD_DESTROY_WIN;
52 
53  protected:
54  /** This semaphore will be signaled when the wx window is built and ready.
55  */
56  mutable std::promise<void> m_threadReady;
57  /** This semaphore will be signaled when the wx window is destroyed. */
58  mutable std::promise<void> m_windowDestroyed;
59  /** The caption of the window */
60  std::string m_caption;
61  /** The window handle */
63 
64  /* Auxiliary */
65  std::atomic_bool m_keyPushed = false;
66  std::atomic_int m_keyPushedCode = 0;
67  std::atomic<mrptKeyModifier> m_keyPushedModifier;
68 
69  /** Must be called by child classes just within the constructor. */
70  void createWxWindow(unsigned int initialWidth, unsigned int initialHeight);
71  /** Must be called by child classes in their destructors. The code cannot be
72  * put into this class' destructor. */
73  void destroyWxWindow();
74 
75  public:
76  /** Read-only access to the wxDialog object. */
77  void* getWxObject() { return m_hwnd.get(); }
78  /** Called by wx main thread to set m_hwnd to NULL. */
80  /** Called by wx main thread to signal the semaphore that the wx window is
81  * built and ready. */
82  void notifySemThreadReady();
83 
84  public:
85  /** CMD_DESTROY_WIN can be 299,399,499... See WxSubsystem */
86 
88  void* winobj_voidptr, int CMD_CREATE_WIN, int CMD_DESTROY_WIN,
89  const std::string& initial_caption = std::string());
90  ~CBaseGUIWindow() override;
91 
92  /** Returns false if the user has already closed the window.
93  */
94  bool isOpen();
95 
96  /** Resizes the window, stretching the image to fit into the display area.
97  */
98  virtual void resize(unsigned int width, unsigned int height) = 0;
99 
100  /** Changes the position of the window on the screen.
101  */
102  virtual void setPos(int x, int y) = 0;
103 
104  /** Changes the window title text.
105  */
106  virtual void setWindowTitle(const std::string& str) = 0;
107 
108  /** Gets the last x,y pixel coordinates of the mouse. \return False if the
109  * window is closed. */
110  virtual bool getLastMousePosition(int& x, int& y) const = 0;
111 
112  /** Set cursor style to default (cursorIsCross=false) or to a cross
113  * (cursorIsCross=true) */
114  virtual void setCursorCross(bool cursorIsCross) = 0;
115 
116  /** Waits for any key to be pushed on the image or the console, and returns
117  * the key code.
118  * This method remove key strokes previous to its call, so it will always
119  * wait. To get
120  * the latest pushed key, see
121  *
122  * \param ignoreControlKeys If set to false, any push of shift, cmd,
123  * control, etc... will make this method to return.
124  * \param out_pushModifier If set to !=nullptr, the modifiers of the key
125  * stroke will be saved here.
126  * \return The virtual key code, as defined in mrptKeyCode (a replication
127  * of wxWidgets key codes).
128  *
129  * \sa getPushedKey, Key codes in the enum mrptKeyCode
130  */
131  int waitForKey(
132  bool ignoreControlKeys = true,
133  mrptKeyModifier* out_pushModifier = nullptr);
134 
135  /** Returns true if a key has been pushed, without blocking waiting for a
136  * new key being pushed.
137  * \sa waitForKey, clearKeyHitFlag
138  */
139  bool keyHit() const { return m_keyPushed; }
140  /** Assure that "keyHit" will return false until the next pushed key.
141  * \sa keyHit, waitForKey
142  */
143  void clearKeyHitFlag() { m_keyPushed = false; }
144  /** Returns the latest pushed key, or 0 if there is no new key stroke.
145  * \param out_pushModifier If set to !=nullptr, the modifiers of the key
146  * stroke will be saved here.
147  * \return The virtual key code, as defined in <mrpt/gui/keycodes.h> (a
148  * replication of wxWidgets key codes).
149  *
150  * \sa keyHit, waitForKey
151  */
152  int getPushedKey(mrptKeyModifier* out_pushModifier = nullptr);
153 
154 }; // End of class def.
155 
156 /** @name Events common to all GUI windows:
157  @{ */
158 
159 /** An event sent by a window upon a char pressed by the user.
160  *
161  * IMPORTANTE NOTICE: Event handlers in your observer class will be invoked
162  * from the wxWidgets internal MRPT thread,
163  * so all your code in the handler must be thread safe.
164  */
166 {
167  protected:
168  /** Just to allow this class to be polymorphic */
169  void do_nothing() override {}
170 
171  public:
173  CBaseGUIWindow* obj, int _char_code, mrptKeyModifier _key_mod)
174  : source_object(obj), char_code(_char_code), key_modifiers(_key_mod)
175  {
176  }
177 
179  /** The virtual key code, as defined in <mrpt/gui/keycodes.h> (a replication
180  * of wxWidgets key codes). */
182  /** Modifiers (Shift, Control, etc...) */
184 }; // End of class def.
185 
186 /** An event sent by a window upon resize.
187  *
188  * IMPORTANTE NOTICE: Event handlers in your observer class will be invoked
189  * from the wxWidgets internal MRPT thread,
190  * so all your code in the handler must be thread safe.
191  */
193 {
194  protected:
195  /** Just to allow this class to be polymorphic */
196  void do_nothing() override {}
197 
198  public:
200  CBaseGUIWindow* obj, size_t _new_width, size_t _new_height)
201  : source_object(obj), new_width(_new_width), new_height(_new_height)
202  {
203  }
204 
207 }; // End of class def.
208 
209 /** An event sent by a window upon a mouse click, giving the (x,y) pixel
210  * coordinates.
211  *
212  * IMPORTANTE NOTICE: Event handlers in your observer class will be invoked
213  * from the wxWidgets internal MRPT thread,
214  * so all your code in the handler must be thread safe.
215  *
216  * \sa mrptEventMouseMove
217  */
219 {
220  protected:
221  /** Just to allow this class to be polymorphic */
222  void do_nothing() override {}
223 
224  public:
226  CBaseGUIWindow* obj, mrpt::img::TPixelCoord _coords, bool _leftButton,
227  bool _rightButton)
228  : source_object(obj),
229  coords(_coords),
230  leftButton(_leftButton),
231  rightButton(_rightButton)
232  {
233  }
234 
239 }; // End of class def.
240 
241 /** An event sent by a window when the mouse is moved over it.
242  * IMPORTANTE NOTICE: Event handlers in your observer class will be invoked
243  * from the wxWidgets internal MRPT thread,
244  * so all your code in the handler must be thread safe.
245  * \sa mrptEventMouseDown
246  */
248 {
249  protected:
250  /** Just to allow this class to be polymorphic */
251  void do_nothing() override {}
252 
253  public:
255  CBaseGUIWindow* obj, mrpt::img::TPixelCoord _coords, bool _leftButton,
256  bool _rightButton)
257  : source_object(obj),
258  coords(_coords),
259  leftButton(_leftButton),
260  rightButton(_rightButton)
261  {
262  }
263 
268 }; // End of class def.
269 
270 /** An event sent by a window upon when it's about to be closed, either
271  * manually by the user or programmatically.
272  * The event field member \a allow_close is default by default, but can be
273  * set to false in the event callback
274  * to forbid the window to be closed by the user. If the event corresponds to
275  * a programatic close, this field is ignored.
276  *
277  * IMPORTANTE NOTICE: Event handlers in your observer class will be invoked
278  * from the wxWidgets internal MRPT thread,
279  * so all your code in the handler must be thread safe.
280  *
281  * \sa CBaseGUIWindow
282  */
284 {
285  protected:
286  /** Just to allow this class to be polymorphic */
287  void do_nothing() override {}
288 
289  public:
290  inline mrptEventWindowClosed(CBaseGUIWindow* obj, bool _allow_close = true)
291  : source_object(obj), allow_close(_allow_close)
292  {
293  }
296 }; // End of class def.
297 
298 /** @} */
299 
300 } // namespace mrpt::gui
An event sent by a window upon resize.
const int m_CMD_CREATE_WIN
can be 200,300,400...
An event sent by a window upon a mouse click, giving the (x,y) pixel coordinates. ...
std::atomic_int m_keyPushedCode
int getPushedKey(mrptKeyModifier *out_pushModifier=nullptr)
Returns the latest pushed key, or 0 if there is no new key stroke.
mrptEventMouseMove(CBaseGUIWindow *obj, mrpt::img::TPixelCoord _coords, bool _leftButton, bool _rightButton)
mrpt::void_ptr_noncopy m_hwnd
The window handle.
std::string m_caption
The caption of the window.
virtual void setWindowTitle(const std::string &str)=0
Changes the window title text.
The basic event type for the observer-observable pattern in MRPT.
Definition: mrptEvent.h:31
int waitForKey(bool ignoreControlKeys=true, mrptKeyModifier *out_pushModifier=nullptr)
Waits for any key to be pushed on the image or the console, and returns the key code.
mrptEventWindowResize(CBaseGUIWindow *obj, size_t _new_width, size_t _new_height)
CBaseGUIWindow(void *winobj_voidptr, int CMD_CREATE_WIN, int CMD_DESTROY_WIN, const std::string &initial_caption=std::string())
CMD_DESTROY_WIN can be 299,399,499...
The wx dialog for gui::CDisplayWindowPlots.
Definition: WxSubsystem.h:413
mrptKeyModifier
Definition: keycodes.h:156
virtual void setCursorCross(bool cursorIsCross)=0
Set cursor style to default (cursorIsCross=false) or to a cross (cursorIsCross=true) ...
An event sent by a window upon when it&#39;s about to be closed, either manually by the user or programma...
An event sent by a window when the mouse is moved over it.
void notifyChildWindowDestruction()
Called by wx main thread to set m_hwnd to NULL.
virtual void setPos(int x, int y)=0
Changes the position of the window on the screen.
void do_nothing() override
Just to allow this class to be polymorphic.
void * getWxObject()
Read-only access to the wxDialog object.
bool isOpen()
Returns false if the user has already closed the window.
std::atomic< mrptKeyModifier > m_keyPushedModifier
bool keyHit() const
Returns true if a key has been pushed, without blocking waiting for a new key being pushed...
A pair (x,y) of pixel coordinates (integer resolution).
Definition: TPixelCoord.h:40
The wx dialog for gui::CDisplayWindow.
Definition: WxSubsystem.h:315
const int m_CMD_DESTROY_WIN
can be 299,399,499...
An event sent by a window upon a char pressed by the user.
Inherit from this class for those objects capable of being observed by a CObserver class...
Definition: CObservable.h:31
std::promise< void > m_threadReady
This semaphore will be signaled when the wx window is built and ready.
mrptEventMouseDown(CBaseGUIWindow *obj, mrpt::img::TPixelCoord _coords, bool _leftButton, bool _rightButton)
mrptEventWindowClosed(CBaseGUIWindow *obj, bool _allow_close=true)
mrpt::img::TPixelCoord coords
std::atomic_bool m_keyPushed
mrptKeyModifier key_modifiers
Modifiers (Shift, Control, etc...)
virtual void resize(unsigned int width, unsigned int height)=0
Resizes the window, stretching the image to fit into the display area.
mrptEventWindowChar(CBaseGUIWindow *obj, int _char_code, mrptKeyModifier _key_mod)
virtual bool getLastMousePosition(int &x, int &y) const =0
Gets the last x,y pixel coordinates of the mouse.
void destroyWxWindow()
Must be called by child classes in their destructors.
Classes for creating GUI windows for 2D and 3D visualization.
Definition: about_box.h:14
void createWxWindow(unsigned int initialWidth, unsigned int initialHeight)
Must be called by child classes just within the constructor.
void do_nothing() override
Just to allow this class to be polymorphic.
mrpt::img::TPixelCoord coords
void notifySemThreadReady()
Called by wx main thread to signal the semaphore that the wx window is built and ready.
std::promise< void > m_windowDestroyed
This semaphore will be signaled when the wx window is destroyed.
int char_code
The virtual key code, as defined in <mrpt/gui/keycodes.h> (a replication of wxWidgets key codes)...
The base class for GUI window classes based on wxWidgets.
void do_nothing() override
Just to allow this class to be polymorphic.
void do_nothing() override
Just to allow this class to be polymorphic.
void clearKeyHitFlag()
Assure that "keyHit" will return false until the next pushed key.
void do_nothing() override
Just to allow this class to be polymorphic.



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 3e8ecd670 Mon May 18 14:23:19 2020 +0200 at lun may 18 14:30:11 CEST 2020