Main MRPT website > C++ reference
MRPT logo
CBaseGUIWindow.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | The Mobile Robot Programming Toolkit (MRPT) C++ library |
3  | |
4  | http://www.mrpt.org/ |
5  | |
6  | Copyright (C) 2005-2012 University of Malaga |
7  | |
8  | This software was written by the Machine Perception and Intelligent |
9  | Robotics Lab, University of Malaga (Spain). |
10  | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> |
11  | |
12  | This file is part of the MRPT project. |
13  | |
14  | MRPT is free software: you can redistribute it and/or modify |
15  | it under the terms of the GNU General Public License as published by |
16  | the Free Software Foundation, either version 3 of the License, or |
17  | (at your option) any later version. |
18  | |
19  | MRPT is distributed in the hope that it will be useful, |
20  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
21  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22  | GNU General Public License for more details. |
23  | |
24  | You should have received a copy of the GNU General Public License |
25  | along with MRPT. If not, see <http://www.gnu.org/licenses/>. |
26  | |
27  +---------------------------------------------------------------------------+ */
28 #ifndef CBaseGUIWindow_H
29 #define CBaseGUIWindow_H
30 
31 #include <mrpt/synch.h>
33 #include <mrpt/utils/CObservable.h>
35 #include <mrpt/utils/TPixelCoord.h>
36 #include <mrpt/gui/keycodes.h>
37 
38 #include <mrpt/gui/link_pragmas.h>
39 
40 
41 namespace mrpt
42 {
43  namespace gui
44  {
45  using namespace mrpt::utils;
46 
47  class CWindowDialog;
48  class CWindowDialogPlots;
49  class C3DWindowDialog;
50 
52 
53  /** The base class for GUI window classes.
54  *
55  * This class can be observed (see mrpt::utils::CObserver) for the following events (see mrpt::utils::mrptEvent):
56  * - mrpt::gui::mrptEventWindowChar
57  * - mrpt::gui::mrptEventWindowResize
58  * - mrpt::gui::mrptEventMouseDown
59  * - mrpt::gui::mrptEventWindowClosed
60  *
61  * See derived classes to check if they emit other additional events.
62  *
63  * IMPORTANTE NOTICE: Event handlers in your observer class will be invoked from the wxWidgets internal MRPT thread,
64  * so all your code in the handler must be thread safe.
65  * \ingroup mrpt_gui_grp
66  */
68  public mrpt::utils::CObject,
69  public mrpt::utils::CObservable
70  {
71  // This must be added to any CSerializable derived class:
73 
74  friend class CWindowDialog;
75  friend class C3DWindowDialog;
76  friend class CWindowDialogPlots;
77 
78  private:
79  const int m_CMD_CREATE_WIN; //!< can be 200,300,400... See WxSubsystem
80  const int m_CMD_DESTROY_WIN; //!< can be 299,399,499... See WxSubsystem
81  void* m_winobj_voidptr;
82 
83  protected:
84  synch::CSemaphore m_semThreadReady; //!< This semaphore will be signaled when the wx window is built and ready.
85  synch::CSemaphore m_semWindowDestroyed; //!< This semaphore will be signaled when the wx window is destroyed.
86  std::string m_caption; //!< The caption of the window
87  void_ptr_noncopy m_hwnd; //!< The window handle
88 
89  /* Auxiliary */
90  volatile bool m_keyPushed;
91  volatile int m_keyPushedCode;
92  volatile mrptKeyModifier m_keyPushedModifier;
93 
94  void createWxWindow(unsigned int initialWidth, unsigned int initialHeight); //!< Must be called by child classes just within the constructor.
95  void destroyWxWindow(); //!< Must be called by child classes in their destructors. The code cannot be put into this class' destructor.
96 
97  public:
98  void * getWxObject() { return m_hwnd.get(); } //!< Read-only access to the wxDialog object.
99  void notifyChildWindowDestruction(); //!< Called by wx main thread to set m_hwnd to NULL.
100  void notifySemThreadReady(); //!< Called by wx main thread to signal the semaphore that the wx window is built and ready.
101 
102  public:
103  /** CMD_DESTROY_WIN can be 299,399,499... See WxSubsystem */
104 
105  CBaseGUIWindow(void* winobj_voidptr, int CMD_CREATE_WIN, int CMD_DESTROY_WIN, const std::string &initial_caption = std::string() );
106  virtual ~CBaseGUIWindow();
107 
108  /** Returns false if the user has already closed the window.
109  */
110  bool isOpen();
111 
112  /** Resizes the window, stretching the image to fit into the display area.
113  */
114  virtual void resize( unsigned int width, unsigned int height ) = 0;
115 
116  /** Changes the position of the window on the screen.
117  */
118  virtual void setPos( int x, int y ) = 0;
119 
120  /** Changes the window title text.
121  */
122  virtual void setWindowTitle( const std::string &str )=0;
123 
124  /** Gets the last x,y pixel coordinates of the mouse. \return False if the window is closed. */
125  virtual bool getLastMousePosition(int &x, int &y) const = 0;
126 
127  /** Set cursor style to default (cursorIsCross=false) or to a cross (cursorIsCross=true) */
128  virtual void setCursorCross(bool cursorIsCross) = 0;
129 
130  /** Waits for any key to be pushed on the image or the console, and returns the key code.
131  * This method remove key strokes previous to its call, so it will always wait. To get
132  * the latest pushed key, see
133  *
134  * \param ignoreControlKeys If set to false, any push of shift, cmd, control, etc... will make this method to return.
135  * \param out_pushModifier If set to !=NULL, the modifiers of the key stroke will be saved here.
136  * \return The virtual key code, as defined in mrptKeyCode (a replication of wxWidgets key codes).
137  *
138  * \sa getPushedKey, Key codes in the enum mrptKeyCode
139  */
140  int waitForKey(bool ignoreControlKeys = true, mrptKeyModifier *out_pushModifier=NULL);
141 
142  /** Returns true if a key has been pushed, without blocking waiting for a new key being pushed.
143  * \sa waitForKey, clearKeyHitFlag
144  */
145  bool keyHit() const { return m_keyPushed; }
146 
147  /** Assure that "keyHit" will return false until the next pushed key.
148  * \sa keyHit, waitForKey
149  */
150  void clearKeyHitFlag() { m_keyPushed = false; }
151 
152  /** Returns the latest pushed key, or 0 if there is no new key stroke.
153  * \param out_pushModifier If set to !=NULL, the modifiers of the key stroke will be saved here.
154  * \return The virtual key code, as defined in <mrpt/gui/keycodes.h> (a replication of wxWidgets key codes).
155  *
156  * \sa keyHit, waitForKey
157  */
158  int getPushedKey(mrptKeyModifier *out_pushModifier=NULL);
159 
160 
161  }; // End of class def.
162 
163 
164  /** @name Events common to all GUI windows:
165  @{ */
166 
167  /** An event sent by a window upon a char pressed by the user.
168  *
169  * IMPORTANTE NOTICE: Event handlers in your observer class will be invoked from the wxWidgets internal MRPT thread,
170  * so all your code in the handler must be thread safe.
171  */
173  {
174  protected:
175  virtual void do_nothing() { } //!< Just to allow this class to be polymorphic
176  public:
178  CBaseGUIWindow *obj,
179  int _char_code,
180  mrptKeyModifier _key_mod
181  ) : source_object(obj), char_code(_char_code), key_modifiers(_key_mod) { }
182 
184  int char_code; //!< The virtual key code, as defined in <mrpt/gui/keycodes.h> (a replication of wxWidgets key codes).
185  mrptKeyModifier key_modifiers; //!< Modifiers (Shift, Control, etc...)
186  }; // End of class def.
187 
188  /** An event sent by a window upon resize.
189  *
190  * IMPORTANTE NOTICE: Event handlers in your observer class will be invoked from the wxWidgets internal MRPT thread,
191  * so all your code in the handler must be thread safe.
192  */
194  {
195  protected:
196  virtual void do_nothing() { } //!< Just to allow this class to be polymorphic
197  public:
199  CBaseGUIWindow *obj,
200  size_t _new_width,
201  size_t _new_height) : source_object(obj), new_width(_new_width), new_height(_new_width) { }
202 
204  size_t new_width, new_height;
205  }; // End of class def.
206 
207  /** An event sent by a window upon a mouse click, giving the (x,y) pixel coordinates.
208  *
209  * IMPORTANTE NOTICE: Event handlers in your observer class will be invoked from the wxWidgets internal MRPT thread,
210  * so all your code in the handler must be thread safe.
211  *
212  * \sa mrptEventMouseDown
213  */
215  {
216  protected:
217  virtual void do_nothing() { } //!< Just to allow this class to be polymorphic
218  public:
220  CBaseGUIWindow *obj,
221  mrpt::utils::TPixelCoord _coords,
222  bool _leftButton,
223  bool _rightButton
224  ) : source_object(obj), coords(_coords), leftButton(_leftButton), rightButton(_rightButton)
225  { }
226 
231  }; // End of class def.
232 
233  /** An event sent by a window upon when it's about to be closed, either manually by the user or programatically.
234  * The event field member \a allow_close is default by default, but can be set to false in the event callback
235  * to forbid the window to be closed by the user. If the event corresponds to a programatic close, this field is ignored.
236  *
237  * IMPORTANTE NOTICE: Event handlers in your observer class will be invoked from the wxWidgets internal MRPT thread,
238  * so all your code in the handler must be thread safe.
239  *
240  * \sa CBaseGUIWindow
241  */
243  {
244  protected:
245  virtual void do_nothing() { } //!< Just to allow this class to be polymorphic
246  public:
248  CBaseGUIWindow *obj,
249  bool _allow_close = true )
250  : source_object(obj), allow_close(_allow_close)
251  { }
254  }; // End of class def.
255 
256  /** @} */
257 
258  } // End of namespace
259 
260 } // End of namespace
261 
262 #endif



Page generated by Doxygen 1.8.3 for MRPT 0.9.6 SVN: at Fri Feb 15 22:05:02 EST 2013