Applied fix for wxCreateTopLevelRealizedWidget crash.
[wxWidgets.git] / src / motif / app.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: app.cpp
3 // Purpose: wxApp
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 17/09/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "app.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __VMS
20 #define XtParent XTPARENT
21 #define XtDisplay XTDISPLAY
22 #endif
23
24 #include "wx/app.h"
25 #include "wx/utils.h"
26 #include "wx/module.h"
27 #include "wx/memory.h"
28 #include "wx/log.h"
29 #include "wx/intl.h"
30 #include "wx/evtloop.h"
31 #include "wx/hash.h"
32
33 #if wxUSE_THREADS
34 #include "wx/thread.h"
35 #endif
36
37 #ifdef __VMS__
38 #pragma message disable nosimpint
39 #endif
40 #include <Xm/Xm.h>
41 #include <X11/Xlib.h>
42 #include <X11/Xutil.h>
43 #include <X11/Xresource.h>
44 #include <X11/Xatom.h>
45 #ifdef __VMS__
46 #pragma message enable nosimpint
47 #endif
48
49 #include "wx/motif/private.h"
50
51 #include <string.h>
52
53 struct wxPerDisplayData
54 {
55 wxPerDisplayData()
56 {
57 m_visualInfo = NULL;
58 m_topLevelWidget = NULL;
59 m_topLevelRealizedWidget = NULL;
60 }
61
62 wxXVisualInfo* m_visualInfo;
63 Widget m_topLevelWidget, m_topLevelRealizedWidget;
64 };
65
66 static void wxTLWidgetDestroyCallback(Widget w, XtPointer clientData,
67 XtPointer ptr);
68 static WXWidget wxCreateTopLevelWidget( WXDisplay* display );
69
70 extern wxList wxPendingDelete;
71 extern bool wxAddIdleCallback();
72
73 wxHashTable *wxWidgetHashTable = NULL;
74
75 IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
76
77 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
78 EVT_IDLE(wxAppBase::OnIdle)
79 END_EVENT_TABLE()
80
81 #ifdef __WXDEBUG__
82 typedef int (*XErrorHandlerFunc)(Display *, XErrorEvent *);
83
84 XErrorHandlerFunc gs_pfnXErrorHandler = 0;
85
86 static int wxXErrorHandler(Display *dpy, XErrorEvent *xevent)
87 {
88 // just forward to the default handler for now
89 return gs_pfnXErrorHandler(dpy, xevent);
90 }
91 #endif // __WXDEBUG__
92
93 bool wxApp::Initialize(int& argc, wxChar **argv)
94 {
95 if ( !wxAppBase::Initialize(argc, argv) )
96 return false;
97
98 wxWidgetHashTable = new wxHashTable(wxKEY_INTEGER);
99
100 return true;
101 }
102
103 void wxApp::CleanUp()
104 {
105 delete wxWidgetHashTable;
106 wxWidgetHashTable = NULL;
107
108 wxAppBase::CleanUp();
109 }
110
111 void wxApp::Exit()
112 {
113 wxApp::CleanUp();
114
115 wxAppConsole::Exit();
116 }
117
118 // ============================================================================
119 // wxApp
120 // ============================================================================
121
122 wxApp::wxApp()
123 {
124 argc = 0;
125 argv = NULL;
126
127 m_mainLoop = new wxEventLoop;
128 m_mainColormap = (WXColormap) NULL;
129 m_appContext = (WXAppContext) NULL;
130 m_initialDisplay = (WXDisplay*) 0;
131 m_perDisplayData = new wxPerDisplayDataMap;
132 }
133
134 wxApp::~wxApp()
135 {
136 delete m_mainLoop;
137
138 for( wxPerDisplayDataMap::iterator it = m_perDisplayData->begin(),
139 end = m_perDisplayData->end();
140 it != end; ++it )
141 {
142 delete it->second->m_visualInfo;
143 XtDestroyWidget( it->second->m_topLevelWidget );
144 delete it->second;
145 }
146
147 delete m_perDisplayData;
148
149 wxApp::SetInstance(NULL);
150 }
151
152 int wxApp::MainLoop()
153 {
154 /*
155 * Sit around forever waiting to process X-events. Property Change
156 * event are handled special, because they have to refer to
157 * the root window rather than to a widget. therefore we can't
158 * use an Xt-eventhandler.
159 */
160
161 XSelectInput(XtDisplay((Widget) wxTheApp->GetTopLevelWidget()),
162 XDefaultRootWindow(XtDisplay((Widget) wxTheApp->GetTopLevelWidget())),
163 PropertyChangeMask);
164
165 m_mainLoop->Run();
166
167 return 0;
168 }
169
170 // This should be redefined in a derived class for
171 // handling property change events for XAtom IPC.
172 void wxApp::HandlePropertyChange(WXEvent *event)
173 {
174 // by default do nothing special
175 XtDispatchEvent((XEvent*) event); /* let Motif do the work */
176 }
177
178 static char *fallbackResources[] = {
179 "*menuBar.marginHeight: 0",
180 "*menuBar.shadowThickness: 1",
181 "*background: #c0c0c0",
182 "*foreground: black",
183 NULL
184 };
185
186 // Create an application context
187 bool wxApp::OnInitGui()
188 {
189 if( !wxAppBase::OnInitGui() )
190 return false;
191
192 XtToolkitInitialize() ;
193 wxTheApp->m_appContext = (WXAppContext) XtCreateApplicationContext();
194 XtAppSetFallbackResources((XtAppContext) wxTheApp->m_appContext, fallbackResources);
195
196 Display *dpy = XtOpenDisplay((XtAppContext) wxTheApp->m_appContext,(String)NULL,NULL,
197 wxTheApp->GetClassName().c_str(), NULL, 0,
198 # if XtSpecificationRelease < 5
199 (Cardinal*) &argc,
200 # else
201 &argc,
202 # endif
203 argv);
204
205 if (!dpy) {
206 // if you don't log to stderr, nothing will be shown...
207 delete wxLog::SetActiveTarget(new wxLogStderr);
208 wxString className(wxTheApp->GetClassName());
209 wxLogError(_("wxWidgets could not open display for '%s': exiting."),
210 className.c_str());
211 exit(-1);
212 }
213 m_initialDisplay = (WXDisplay*) dpy;
214
215 #ifdef __WXDEBUG__
216 // install the X error handler
217 gs_pfnXErrorHandler = XSetErrorHandler(wxXErrorHandler);
218 #endif // __WXDEBUG__
219
220 // Add general resize proc
221 XtActionsRec rec;
222 rec.string = "resize";
223 rec.proc = (XtActionProc)wxWidgetResizeProc;
224 XtAppAddActions((XtAppContext) wxTheApp->m_appContext, &rec, 1);
225
226 GetMainColormap(dpy);
227
228 wxAddIdleCallback();
229
230 return true;
231 }
232
233 WXColormap wxApp::GetMainColormap(WXDisplay* display)
234 {
235 if (!display) /* Must be called first with non-NULL display */
236 return m_mainColormap;
237
238 int defaultScreen = DefaultScreen((Display*) display);
239 Screen* screen = XScreenOfDisplay((Display*) display, defaultScreen);
240
241 Colormap c = DefaultColormapOfScreen(screen);
242
243 if (!m_mainColormap)
244 m_mainColormap = (WXColormap) c;
245
246 return (WXColormap) c;
247 }
248
249 static inline wxPerDisplayData& GetOrCreatePerDisplayData
250 ( wxPerDisplayDataMap& m, WXDisplay* display )
251 {
252 wxPerDisplayDataMap::iterator it = m.find( display );
253 if( it != m.end() && it->second != NULL )
254 return *(it->second);
255
256 wxPerDisplayData* nData = new wxPerDisplayData();
257 m[display] = nData;
258
259 return *nData;
260 }
261
262 wxXVisualInfo* wxApp::GetVisualInfo( WXDisplay* display )
263 {
264 wxPerDisplayData& data = GetOrCreatePerDisplayData( *m_perDisplayData,
265 display );
266 if( data.m_visualInfo )
267 return data.m_visualInfo;
268
269 wxXVisualInfo* vi = new wxXVisualInfo;
270 wxFillXVisualInfo( vi, (Display*)display );
271
272 data.m_visualInfo = vi;
273
274 return vi;
275 }
276
277 static void wxTLWidgetDestroyCallback(Widget w, XtPointer clientData,
278 XtPointer ptr)
279 {
280 if( wxTheApp )
281 {
282 wxTheApp->SetTopLevelWidget( (WXDisplay*)XtDisplay(w),
283 (WXWidget)NULL );
284 wxTheApp->SetTopLevelRealizedWidget( (WXDisplay*)XtDisplay(w),
285 (WXWidget)NULL );
286 }
287 }
288
289 WXWidget wxCreateTopLevelWidget( WXDisplay* display )
290 {
291 Widget tlw = XtAppCreateShell( (String)NULL,
292 wxTheApp->GetClassName().c_str(),
293 applicationShellWidgetClass,
294 (Display*)display,
295 NULL, 0 );
296 XtVaSetValues( tlw,
297 XmNoverrideRedirect, True,
298 NULL );
299
300 XtAddCallback( tlw, XmNdestroyCallback,
301 (XtCallbackProc)wxTLWidgetDestroyCallback,
302 (XtPointer)NULL );
303
304 return (WXWidget)tlw;
305 }
306
307 WXWidget wxCreateTopLevelRealizedWidget( WXDisplay* display )
308 {
309 Widget rTlw = XtVaCreateWidget( "dummy_widget", topLevelShellWidgetClass,
310 (Widget)wxTheApp->GetTopLevelWidget(),
311 NULL, 0 );
312 XtSetMappedWhenManaged( rTlw, False );
313 XtRealizeWidget( rTlw );
314
315 return (WXWidget)rTlw;
316 }
317
318 WXWidget wxApp::GetTopLevelWidget()
319 {
320 WXDisplay* display = wxGetDisplay();
321 wxPerDisplayData& data = GetOrCreatePerDisplayData( *m_perDisplayData,
322 display );
323 if( data.m_topLevelWidget )
324 return (WXWidget)data.m_topLevelWidget;
325
326 WXWidget tlw = wxCreateTopLevelWidget( display );
327 SetTopLevelWidget( display, tlw );
328
329 return tlw;
330 }
331
332 WXWidget wxApp::GetTopLevelRealizedWidget()
333 {
334 WXDisplay* display = wxGetDisplay();
335 wxPerDisplayDataMap::iterator it = m_perDisplayData->find( display );
336
337 if( it != m_perDisplayData->end() && it->second->m_topLevelRealizedWidget )
338 return (WXWidget)it->second->m_topLevelRealizedWidget;
339
340 WXWidget rTlw = wxCreateTopLevelRealizedWidget( display );
341 SetTopLevelRealizedWidget( display, rTlw );
342
343 return rTlw;
344 }
345
346 void wxApp::SetTopLevelWidget(WXDisplay* display, WXWidget widget)
347 {
348 GetOrCreatePerDisplayData( *m_perDisplayData, display )
349 .m_topLevelWidget = (Widget)widget;
350 }
351
352 void wxApp::SetTopLevelRealizedWidget(WXDisplay* display, WXWidget widget)
353 {
354 GetOrCreatePerDisplayData( *m_perDisplayData, display )
355 .m_topLevelRealizedWidget = (Widget)widget;
356 }
357
358 // Yield to other processes
359
360 bool wxApp::Yield(bool onlyIfNeeded)
361 {
362 static bool s_inYield = false;
363
364 if ( s_inYield )
365 {
366 if ( !onlyIfNeeded )
367 {
368 wxFAIL_MSG( wxT("wxYield called recursively" ) );
369 }
370
371 return false;
372 }
373
374 s_inYield = true;
375
376 while (wxTheApp && wxTheApp->Pending())
377 wxTheApp->Dispatch();
378
379 s_inYield = false;
380
381 return true;
382 }
383
384 // ----------------------------------------------------------------------------
385 // accessors for C modules
386 // ----------------------------------------------------------------------------
387
388 extern "C" XtAppContext wxGetAppContext()
389 {
390 return (XtAppContext)wxTheApp->GetAppContext();
391 }