]> git.saurik.com Git - wxWidgets.git/blame - src/motif/app.cpp
Commit patch [ 1559950 ] fix mem leaks in wx{Dir|File}Dialog
[wxWidgets.git] / src / motif / app.cpp
CommitLineData
4bb6408c 1/////////////////////////////////////////////////////////////////////////////
55034339 2// Name: src/motif/app.cpp
4bb6408c
JS
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
1248b41f
MB
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
bcd055ae
JJ
15#ifdef __VMS
16#define XtParent XTPARENT
17#define XtDisplay XTDISPLAY
18#endif
19
88a7a4e1
WS
20#include "wx/app.h"
21
32d4c30a
WS
22#ifndef WX_PRECOMP
23 #include "wx/hash.h"
88a7a4e1 24 #include "wx/intl.h"
e4db172a 25 #include "wx/log.h"
de6185e2 26 #include "wx/utils.h"
5b56bffb 27 #include "wx/memory.h"
73216ce6 28 #include "wx/font.h"
32d4c30a
WS
29#endif
30
7e1bcfa8 31#include "wx/evtloop.h"
b412f9be 32
7bcb11d3 33#if wxUSE_THREADS
dfe1eee3 34 #include "wx/thread.h"
7bcb11d3 35#endif
4bb6408c 36
338dd992 37#ifdef __VMS__
02761f6c 38 #pragma message disable nosimpint
338dd992 39#endif
4bb6408c
JS
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>
338dd992 45#ifdef __VMS__
02761f6c 46 #pragma message enable nosimpint
338dd992 47#endif
4bb6408c
JS
48
49#include "wx/motif/private.h"
50
51#include <string.h>
52
eb6fa4b4
MB
53struct wxPerDisplayData
54{
55 wxPerDisplayData()
5a2e3d8c
MB
56 {
57 m_visualInfo = NULL;
58 m_topLevelWidget = NULL;
59 m_topLevelRealizedWidget = NULL;
60 }
eb6fa4b4
MB
61
62 wxXVisualInfo* m_visualInfo;
5a2e3d8c 63 Widget m_topLevelWidget, m_topLevelRealizedWidget;
eb6fa4b4
MB
64};
65
eb6fa4b4
MB
66static void wxTLWidgetDestroyCallback(Widget w, XtPointer clientData,
67 XtPointer ptr);
68static WXWidget wxCreateTopLevelWidget( WXDisplay* display );
9ce8d6a2 69
7e1bcfa8 70extern bool wxAddIdleCallback();
7491d644 71
4bb6408c
JS
72wxHashTable *wxWidgetHashTable = NULL;
73
4bb6408c 74IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
9c6e1335 75
4bb6408c 76BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
955a9197 77 EVT_IDLE(wxAppBase::OnIdle)
4bb6408c 78END_EVENT_TABLE()
4bb6408c 79
e838cc14 80#ifdef __WXDEBUG__
86ac4a22
VZ
81extern "C"
82{
e838cc14 83 typedef int (*XErrorHandlerFunc)(Display *, XErrorEvent *);
86ac4a22 84}
e838cc14 85
86ac4a22 86XErrorHandlerFunc gs_pfnXErrorHandler = 0;
e838cc14 87
86ac4a22
VZ
88extern "C"
89{
90
91static int wxXErrorHandler(Display *dpy, XErrorEvent *xevent)
92{
93 // just forward to the default handler for now
94 return gs_pfnXErrorHandler(dpy, xevent);
95}
96
97}
e838cc14
VZ
98#endif // __WXDEBUG__
99
86ac4a22 100bool wxApp::Initialize(int& argcOrig, wxChar **argvOrig)
4bb6408c 101{
86ac4a22 102 if ( !wxAppBase::Initialize(argcOrig, argvOrig) )
94826170 103 return false;
afb74891 104
4bb6408c 105 wxWidgetHashTable = new wxHashTable(wxKEY_INTEGER);
afb74891 106
9d02485e
MB
107#if wxUSE_INTL
108 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
109#endif
110
94826170 111 return true;
4bb6408c
JS
112}
113
114void wxApp::CleanUp()
115{
8d1bf3c7
MB
116 wxAppBase::CleanUp();
117
e6be3e7c
MB
118 delete wxWidgetHashTable;
119 wxWidgetHashTable = NULL;
120
8d1bf3c7
MB
121 delete m_mainLoop;
122
123 for( wxPerDisplayDataMap::iterator it = m_perDisplayData->begin(),
124 end = m_perDisplayData->end();
125 it != end; ++it )
126 {
127 delete it->second->m_visualInfo;
df7e062c
MW
128 // On Solaris 10 calling XtDestroyWidget on the top level widget
129 // dumps core if the locale is set to something other than "C"
130#ifndef __SUN__
8d1bf3c7 131 XtDestroyWidget( it->second->m_topLevelWidget );
df7e062c 132#endif
8d1bf3c7
MB
133 delete it->second;
134 }
135
136 delete m_perDisplayData;
4bb6408c
JS
137}
138
e2478fde
VZ
139void wxApp::Exit()
140{
141 wxApp::CleanUp();
142
143 wxAppConsole::Exit();
144}
145
e6be3e7c 146// ============================================================================
05e2b077 147// wxApp
e6be3e7c
MB
148// ============================================================================
149
4bb6408c
JS
150wxApp::wxApp()
151{
4bb6408c
JS
152 argc = 0;
153 argv = NULL;
afb74891 154
f15b55ce 155 m_mainLoop = new wxEventLoop;
4bb6408c
JS
156 m_mainColormap = (WXColormap) NULL;
157 m_appContext = (WXAppContext) NULL;
47bc1060 158 m_initialDisplay = (WXDisplay*) 0;
eb6fa4b4 159 m_perDisplayData = new wxPerDisplayDataMap;
4bb6408c
JS
160}
161
7e1bcfa8
MB
162wxApp::~wxApp()
163{
57bfe3be 164 wxApp::SetInstance(NULL);
7e1bcfa8
MB
165}
166
4bb6408c
JS
167int wxApp::MainLoop()
168{
2d120f83 169 /*
4bb6408c
JS
170 * Sit around forever waiting to process X-events. Property Change
171 * event are handled special, because they have to refer to
172 * the root window rather than to a widget. therefore we can't
173 * use an Xt-eventhandler.
174 */
afb74891 175
4bb6408c 176 XSelectInput(XtDisplay((Widget) wxTheApp->GetTopLevelWidget()),
2d120f83
JS
177 XDefaultRootWindow(XtDisplay((Widget) wxTheApp->GetTopLevelWidget())),
178 PropertyChangeMask);
afb74891 179
f15b55ce 180 m_mainLoop->Run();
afb74891 181
8aa04e8b
JS
182 return 0;
183}
184
4bb6408c
JS
185// This should be redefined in a derived class for
186// handling property change events for XAtom IPC.
187void wxApp::HandlePropertyChange(WXEvent *event)
188{
189 // by default do nothing special
190 XtDispatchEvent((XEvent*) event); /* let Motif do the work */
191}
192
edc1cd8b 193static char *fallbackResources[] = {
0ab82ca9
VZ
194 // better defaults for CDE under Irix
195 //
196 // TODO: do something similar for the other systems, the hardcoded defaults
197 // below are ugly
198#ifdef __SGI__
f1db433a
VZ
199 wxMOTIF_STR("*sgiMode: True"),
200 wxMOTIF_STR("*useSchemes: all"),
0ab82ca9 201#else // !__SGI__
f1db433a
VZ
202 wxMOTIF_STR("*menuBar.marginHeight: 0"),
203 wxMOTIF_STR("*menuBar.shadowThickness: 1"),
204 wxMOTIF_STR("*background: #c0c0c0"),
205 wxMOTIF_STR("*foreground: black"),
0ab82ca9 206#endif // __SGI__/!__SGI__
edc1cd8b
JS
207 NULL
208};
209
4bb6408c
JS
210// Create an application context
211bool wxApp::OnInitGui()
212{
fd304d98 213 if( !wxAppBase::OnInitGui() )
96be256b 214 return false;
fd304d98 215
0fad0dfd
VZ
216#ifdef __HPUX__
217 // under HP-UX creating XmFontSet fails when the system locale is C and
218 // we're using a remote DISPLAY, presumably because HP-UX uses its own
219 // names for C and ISO locales (roman8 and iso8859n respectively) and so
220 // its Motif libraries have troubles with non-HP X server
221 //
222 // whatever the reason, the fact is that without this hack any wxMotif
223 // program crashes on startup because it can't create any font (HP programs
224 // still work but they do spit out messages about failing to create font
225 // sets and failing back on "fixed" font too)
226 //
227 // notice that calling setlocale() here is not enough because X(m) init
228 // functions call setlocale() later so we really have to change environment
229 bool fixAll = false; // tweak LC_ALL (or just LC_CTYPE)?
230 const char *loc = getenv("LC_CTYPE");
231 if ( !loc )
232 {
233 loc = getenv("LC_ALL");
234 if ( loc )
235 fixAll = true;
236 }
237
238 if ( !loc ||
239 (loc[0] == 'C' && loc[1] == '\0') ||
240 strcmp(loc, "POSIX") == 0 )
241 {
242 // we're using C locale, "fix" it
243 wxLogDebug(_T("HP-UX fontset hack: forcing locale to en_US.iso88591"));
244 putenv(fixAll ? "LC_ALL=en_US.iso88591" : "LC_CTYPE=en_US.iso88591");
245 }
246#endif // __HPUX__
247
9d02485e 248 XtSetLanguageProc(NULL, NULL, NULL);
4bb6408c 249 XtToolkitInitialize() ;
edc1cd8b
JS
250 wxTheApp->m_appContext = (WXAppContext) XtCreateApplicationContext();
251 XtAppSetFallbackResources((XtAppContext) wxTheApp->m_appContext, fallbackResources);
252
0b6d76bf
VZ
253 // we shouldn't pass empty application/class name as it results in
254 // immediate crash inside XOpenIM() (if XIM is used) under IRIX
255 wxString appname = wxTheApp->GetAppName();
256 if ( appname.empty() )
257 appname = _T("wxapp");
258 wxString clsname = wxTheApp->GetClassName();
259 if ( clsname.empty() )
260 clsname = _T("wx");
261
262 Display *dpy = XtOpenDisplay((XtAppContext) wxTheApp->m_appContext,
263 (String)NULL,
264 appname.c_str(),
265 clsname.c_str(),
266 NULL, 0, // no options
4bb6408c 267# if XtSpecificationRelease < 5
9c6e1335 268 (Cardinal*) &argc,
4bb6408c 269# else
9c6e1335 270 &argc,
4bb6408c 271# endif
9c6e1335
VZ
272 argv);
273
4bb6408c 274 if (!dpy) {
c88b736c
MB
275 // if you don't log to stderr, nothing will be shown...
276 delete wxLog::SetActiveTarget(new wxLogStderr);
7bcb11d3 277 wxString className(wxTheApp->GetClassName());
77ffb593 278 wxLogError(_("wxWidgets could not open display for '%s': exiting."),
d3a80c92 279 className.c_str());
2d120f83 280 exit(-1);
4bb6408c 281 }
47bc1060 282 m_initialDisplay = (WXDisplay*) dpy;
afb74891 283
e838cc14
VZ
284#ifdef __WXDEBUG__
285 // install the X error handler
286 gs_pfnXErrorHandler = XSetErrorHandler(wxXErrorHandler);
287#endif // __WXDEBUG__
288
4bb6408c
JS
289 // Add general resize proc
290 XtActionsRec rec;
f1db433a 291 rec.string = wxMOTIF_STR("resize");
4bb6408c
JS
292 rec.proc = (XtActionProc)wxWidgetResizeProc;
293 XtAppAddActions((XtAppContext) wxTheApp->m_appContext, &rec, 1);
afb74891 294
4bb6408c 295 GetMainColormap(dpy);
afb74891 296
7e1bcfa8
MB
297 wxAddIdleCallback();
298
96be256b 299 return true;
4bb6408c
JS
300}
301
302WXColormap wxApp::GetMainColormap(WXDisplay* display)
303{
304 if (!display) /* Must be called first with non-NULL display */
2d120f83 305 return m_mainColormap;
a91b47e8
JS
306
307 int defaultScreen = DefaultScreen((Display*) display);
308 Screen* screen = XScreenOfDisplay((Display*) display, defaultScreen);
afb74891 309
a91b47e8 310 Colormap c = DefaultColormapOfScreen(screen);
afb74891 311
4bb6408c 312 if (!m_mainColormap)
2d120f83 313 m_mainColormap = (WXColormap) c;
afb74891 314
4bb6408c
JS
315 return (WXColormap) c;
316}
317
bdcade0a
MB
318static inline wxPerDisplayData& GetOrCreatePerDisplayData
319 ( wxPerDisplayDataMap& m, WXDisplay* display )
9ce8d6a2 320{
bdcade0a
MB
321 wxPerDisplayDataMap::iterator it = m.find( display );
322 if( it != m.end() && it->second != NULL )
323 return *(it->second);
324
325 wxPerDisplayData* nData = new wxPerDisplayData();
326 m[display] = nData;
327
328 return *nData;
329}
9ce8d6a2 330
bdcade0a
MB
331wxXVisualInfo* wxApp::GetVisualInfo( WXDisplay* display )
332{
333 wxPerDisplayData& data = GetOrCreatePerDisplayData( *m_perDisplayData,
334 display );
335 if( data.m_visualInfo )
336 return data.m_visualInfo;
9ce8d6a2
MB
337
338 wxXVisualInfo* vi = new wxXVisualInfo;
339 wxFillXVisualInfo( vi, (Display*)display );
340
bdcade0a 341 data.m_visualInfo = vi;
9ce8d6a2
MB
342
343 return vi;
344}
345
55034339
WS
346static void wxTLWidgetDestroyCallback(Widget w, XtPointer WXUNUSED(clientData),
347 XtPointer WXUNUSED(ptr))
eb6fa4b4
MB
348{
349 if( wxTheApp )
5a2e3d8c 350 {
eb6fa4b4
MB
351 wxTheApp->SetTopLevelWidget( (WXDisplay*)XtDisplay(w),
352 (WXWidget)NULL );
5a2e3d8c
MB
353 wxTheApp->SetTopLevelRealizedWidget( (WXDisplay*)XtDisplay(w),
354 (WXWidget)NULL );
355 }
eb6fa4b4
MB
356}
357
358WXWidget wxCreateTopLevelWidget( WXDisplay* display )
359{
360 Widget tlw = XtAppCreateShell( (String)NULL,
361 wxTheApp->GetClassName().c_str(),
362 applicationShellWidgetClass,
363 (Display*)display,
364 NULL, 0 );
5a2e3d8c
MB
365 XtVaSetValues( tlw,
366 XmNoverrideRedirect, True,
367 NULL );
eb6fa4b4
MB
368
369 XtAddCallback( tlw, XmNdestroyCallback,
370 (XtCallbackProc)wxTLWidgetDestroyCallback,
371 (XtPointer)NULL );
372
373 return (WXWidget)tlw;
374}
375
55034339 376WXWidget wxCreateTopLevelRealizedWidget( WXDisplay* WXUNUSED(display) )
5a2e3d8c 377{
cb81f1a6 378 Widget rTlw = XtVaCreateWidget( "dummy_widget", topLevelShellWidgetClass,
5a2e3d8c 379 (Widget)wxTheApp->GetTopLevelWidget(),
b09a59f5 380 NULL );
5a2e3d8c
MB
381 XtSetMappedWhenManaged( rTlw, False );
382 XtRealizeWidget( rTlw );
383
384 return (WXWidget)rTlw;
385}
386
eb6fa4b4
MB
387WXWidget wxApp::GetTopLevelWidget()
388{
389 WXDisplay* display = wxGetDisplay();
bdcade0a
MB
390 wxPerDisplayData& data = GetOrCreatePerDisplayData( *m_perDisplayData,
391 display );
392 if( data.m_topLevelWidget )
393 return (WXWidget)data.m_topLevelWidget;
eb6fa4b4
MB
394
395 WXWidget tlw = wxCreateTopLevelWidget( display );
396 SetTopLevelWidget( display, tlw );
397
398 return tlw;
399}
400
5a2e3d8c
MB
401WXWidget wxApp::GetTopLevelRealizedWidget()
402{
403 WXDisplay* display = wxGetDisplay();
404 wxPerDisplayDataMap::iterator it = m_perDisplayData->find( display );
405
406 if( it != m_perDisplayData->end() && it->second->m_topLevelRealizedWidget )
407 return (WXWidget)it->second->m_topLevelRealizedWidget;
408
409 WXWidget rTlw = wxCreateTopLevelRealizedWidget( display );
410 SetTopLevelRealizedWidget( display, rTlw );
411
412 return rTlw;
413}
414
eb6fa4b4
MB
415void wxApp::SetTopLevelWidget(WXDisplay* display, WXWidget widget)
416{
5a2e3d8c
MB
417 GetOrCreatePerDisplayData( *m_perDisplayData, display )
418 .m_topLevelWidget = (Widget)widget;
419}
420
421void wxApp::SetTopLevelRealizedWidget(WXDisplay* display, WXWidget widget)
422{
423 GetOrCreatePerDisplayData( *m_perDisplayData, display )
424 .m_topLevelRealizedWidget = (Widget)widget;
eb6fa4b4
MB
425}
426
4bb6408c 427// Yield to other processes
cb2713bf 428
8461e4c2 429bool wxApp::Yield(bool onlyIfNeeded)
4bb6408c 430{
96be256b 431 static bool s_inYield = false;
8461e4c2
VZ
432
433 if ( s_inYield )
434 {
435 if ( !onlyIfNeeded )
436 {
437 wxFAIL_MSG( wxT("wxYield called recursively" ) );
438 }
439
96be256b 440 return false;
8461e4c2
VZ
441 }
442
96be256b 443 s_inYield = true;
cb2713bf 444
4bb6408c 445 while (wxTheApp && wxTheApp->Pending())
2d120f83 446 wxTheApp->Dispatch();
518b5d2f 447
96be256b 448 s_inYield = false;
cb2713bf 449
96be256b 450 return true;
4bb6408c
JS
451}
452
d391a345
VZ
453// ----------------------------------------------------------------------------
454// accessors for C modules
455// ----------------------------------------------------------------------------
456
457extern "C" XtAppContext wxGetAppContext()
458{
459 return (XtAppContext)wxTheApp->GetAppContext();
460}