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