Main MRPT website > C++ reference for MRPT 1.5.6
CBaseGUIWindow.cpp
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +---------------------------------------------------------------------------+ */
9 
10 #include "gui-precomp.h" // Precompiled headers
11 
13 #include <mrpt/system/os.h>
14 #include <mrpt/gui/WxSubsystem.h>
15 
16 using namespace mrpt;
17 using namespace mrpt::gui;
18 using namespace mrpt::utils;
19 using namespace mrpt::system;
20 using namespace std;
21 
23 
24 
25 /*---------------------------------------------------------------
26  Ctor
27  ---------------------------------------------------------------*/
28 CBaseGUIWindow::CBaseGUIWindow(void* winobj_voidptr, int CMD_CREATE_WIN, int CMD_DESTROY_WIN, const std::string &initial_caption )
29  : m_CMD_CREATE_WIN(CMD_CREATE_WIN),
30  m_CMD_DESTROY_WIN(CMD_DESTROY_WIN),
31  m_winobj_voidptr(winobj_voidptr),
32  m_semThreadReady(0,1),
33  m_semWindowDestroyed(0,1),
34  m_caption(initial_caption),
35  m_hwnd(NULL),
36  m_keyPushed(false),
37  m_keyPushedCode(0),
38  m_keyPushedModifier(MRPTKMOD_NONE)
39 {
40 }
41 
42 /*---------------------------------------------------------------
43  Create the wx Window
44  ---------------------------------------------------------------*/
45 void CBaseGUIWindow::createWxWindow(unsigned int initialWidth, unsigned int initialHeight)
46 {
47  MRPT_UNUSED_PARAM(initialWidth); MRPT_UNUSED_PARAM(initialHeight);
49 #if MRPT_HAS_WXWIDGETS
50  // Create the main wxThread:
51  // -------------------------------
53  return; // Error!
54 
55  // Create window:
57  REQ->source2D = static_cast<gui::CDisplayWindow*>(m_winobj_voidptr);
58  REQ->source3D = static_cast<gui::CDisplayWindow3D*>(m_winobj_voidptr);
59  REQ->sourcePlots = static_cast<gui::CDisplayWindowPlots*>(m_winobj_voidptr);
60  REQ->str = m_caption;
61  REQ->OPCODE = m_CMD_CREATE_WIN;
62  REQ->voidPtr = m_hwnd.getPtrToPtr();
63  REQ->x = initialWidth ;
64  REQ->y = initialHeight ;
65 
67 
68  // Wait for the window to realize and signal it's alive:
70  {
71  mrpt::system::sleep(20); // Force at least 1-2 timer ticks for processing the event:
72  wxApp::GetInstance()->Yield(true);
73  }
74  int maxTimeout =
75 #ifdef _DEBUG
76  30000;
77 #else
78  6000;
79 #endif
80  // If we have an "MRPT_WXSUBSYS_TIMEOUT_MS" environment variable, use that timeout instead:
81  const char *envVal = getenv("MRPT_WXSUBSYS_TIMEOUT_MS");
82  if (envVal) maxTimeout = atoi(envVal);
83 
84 
85  if(!m_semThreadReady.waitForSignal(maxTimeout)) // 2 secs should be enough...
86  {
87  cerr << "[CBaseGUIWindow::ctor] Timeout waiting window creation." << endl;
88  }
89 #else
90  THROW_EXCEPTION("MRPT compiled without wxWidgets!")
91 #endif
92  MRPT_END
93 }
94 
95 /*---------------------------------------------------------------
96  Dtor
97  ---------------------------------------------------------------*/
99 {
100 }
101 
102 /*---------------------------------------------------------------
103  destroyWxWindow
104  ---------------------------------------------------------------*/
106 {
107  MRPT_START
108 #if MRPT_HAS_WXWIDGETS
109  // Send close request:
110  if (m_hwnd.get())
111  {
113  REQ->OPCODE = m_CMD_DESTROY_WIN;
114  REQ->source2D = static_cast<gui::CDisplayWindow*>(m_winobj_voidptr);
115  REQ->source3D = static_cast<gui::CDisplayWindow3D*>(m_winobj_voidptr);
116  REQ->sourcePlots = static_cast<gui::CDisplayWindowPlots*>(m_winobj_voidptr);
117 
119 
120  // Wait until the thread ends:
122  {
123  mrpt::system::sleep(20); // Force at least 1-2 timer ticks for processing the event:
124  wxApp::GetInstance()->Yield(true);
125  }
126  const int maxTimeout =
127  #ifdef _DEBUG
128  30000;
129  #else
130  6000;
131  #endif
132  if(!m_semWindowDestroyed.waitForSignal(maxTimeout)) // 2 secs should be enough...
133  {
134  cerr << "[CBaseGUIWindow::dtor] Timeout waiting window destruction." << endl;
135  }
136  }
138 #endif
139  MRPT_END
140 }
141 
142 /*---------------------------------------------------------------
143  notifyChildWindowDestruction
144  ---------------------------------------------------------------*/
146 {
147  //cout << "[CBaseGUIWindow::notifyChildWindowDestruction] Called." << endl;
148  m_hwnd = NULL;
149 }
150 
151 /*---------------------------------------------------------------
152  waitForKey
153  ---------------------------------------------------------------*/
154 int CBaseGUIWindow::waitForKey(bool ignoreControlKeys,mrptKeyModifier *out_pushModifier)
155 {
156  int k = 0;
157  if (out_pushModifier) *out_pushModifier = MRPTKMOD_NONE;
158  m_keyPushed=false;
159 
160  for (;;)
161  {
162  if (os::kbhit())
163  {
164  k=os::getch();
165  return k;
166  }
167  if (m_keyPushed)
168  {
169  k=m_keyPushedCode;
170  m_keyPushed=false;
171  if (m_keyPushedCode<256 || !ignoreControlKeys)
172  {
173  if (out_pushModifier) *out_pushModifier = m_keyPushedModifier;
174  return k;
175  }
176  // Ignore and keep waiting
177  }
179  // Are we still alive?
180  if (!isOpen())
181  return 0;
182  }
183 }
184 
185 /*---------------------------------------------------------------
186  getPushedKey
187  ---------------------------------------------------------------*/
189 {
190  int k = 0;
191  if (out_pushModifier) *out_pushModifier = MRPTKMOD_NONE;
192 
193  for (;;)
194  {
195  if (m_keyPushed)
196  {
197  k=m_keyPushedCode;
198  m_keyPushed=false;
199  if (out_pushModifier) *out_pushModifier = m_keyPushedModifier;
200  return k;
201  }
203  // Are we still alive?
204  if (!isOpen())
205  return 0;
206  }
207 }
208 
209 /*---------------------------------------------------------------
210  isOpen
211  ---------------------------------------------------------------*/
213 {
214  return m_hwnd!=NULL;
215 }
216 
217 /*---------------------------------------------------------------
218  notifySemThreadReady
219  ---------------------------------------------------------------*/
221 {
222  m_semThreadReady.release();
223 }
static void pushPendingWxRequest(TRequestToWxMainThread *data)
Thread-safe method to insert a new pending request (The memory must be dinamically allocated with "ne...
void * voidPtr
Parameters, depending on OPCODE.
Definition: WxSubsystem.h:214
int waitForKey(bool ignoreControlKeys=true, mrptKeyModifier *out_pushModifier=NULL)
Waits for any key to be pushed on the image or the console, and returns the key code.
int getPushedKey(mrptKeyModifier *out_pushModifier=NULL)
Returns the latest pushed key, or 0 if there is no new key stroke.
#define IMPLEMENTS_VIRTUAL_MRPT_OBJECT(class_name, base_class_name, NameSpace)
This must be inserted as implementation of some required members for virtual CSerializable classes: ...
Definition: CObject.h:291
mrpt::gui::CDisplayWindow3D * source3D
Only one of source* can be non-NULL, indicating the class that generated the request.
Definition: WxSubsystem.h:200
The data structure for each inter-thread request:
Definition: WxSubsystem.h:182
Create a GUI window and display plots with MATLAB-like interfaces and commands.
#define THROW_EXCEPTION(msg)
mrptKeyModifier
Definition: keycodes.h:158
int OPCODE
Valid codes are: For CDisplayWindow:
Definition: WxSubsystem.h:259
void notifyChildWindowDestruction()
Called by wx main thread to set m_hwnd to NULL.
int BASE_IMPEXP getch() MRPT_NO_THROWS
An OS-independent version of getch, which waits until a key is pushed.
Definition: os.cpp:375
#define MRPT_END
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
void BASE_IMPEXP sleep(int time_ms) MRPT_NO_THROWS
An OS-independent method for sending the current thread to "sleep" for a given period of time...
Definition: threads.cpp:57
This class creates a window as a graphical user interface (GUI) for displaying images to the user...
bool isOpen()
Returns false if the user has already closed the window.
static void waitWxShutdownsIfNoWindows()
This method must be called in the destructor of the user class FROM THE MAIN THREAD, in order to wait for the shutdown of the wx thread if this was the last open window.
#define MRPT_START
bool BASE_IMPEXP kbhit() MRPT_NO_THROWS
An OS-independent version of kbhit, which returns true if a key has been pushed.
Definition: os.cpp:396
mrpt::gui::CDisplayWindowPlots * sourcePlots
Only one of source* can be non-NULL, indicating the class that generated the request.
Definition: WxSubsystem.h:203
void destroyWxWindow()
Must be called by child classes in their destructors. The code cannot be put into this class' destruc...
void createWxWindow(unsigned int initialWidth, unsigned int initialHeight)
Must be called by child classes just within the constructor.
static volatile bool isConsoleApp
Will be set to true at runtime if it's not detected a running wxApp instance.
Definition: WxSubsystem.h:114
The virtual base class of all MRPT classes with a unified RTTI system.
Definition: CObject.h:123
mrpt::gui::CDisplayWindow * source2D
Only one of source* can be non-NULL, indicating the class that generated the request.
Definition: WxSubsystem.h:197
std::string str
Parameters, depending on OPCODE.
Definition: WxSubsystem.h:210
void notifySemThreadReady()
Called by wx main thread to signal the semaphore that the wx window is built and ready.
The base class for GUI window classes.
static bool createOneInstanceMainThread()
Thread-safe method to create one single instance of the main wxWidgets thread: it will create the thr...
A graphical user interface (GUI) for efficiently rendering 3D scenes in real-time.



Page generated by Doxygen 1.8.6 for MRPT 1.5.6 Git: 4c65e84 Tue Apr 24 08:18:17 2018 +0200 at mar abr 24 08:26:17 CEST 2018