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