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