]> git.saurik.com Git - wxWidgets.git/blame - src/motif/app.cpp
use unsgined int instead of int for 1 bit bitfields (SGI CC warning fix)
[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
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;
129 XtDestroyWidget( it->second->m_topLevelWidget );
130 delete it->second;
131 }
132
133 delete m_perDisplayData;
4bb6408c
JS
134}
135
e2478fde
VZ
136void wxApp::Exit()
137{
138 wxApp::CleanUp();
139
140 wxAppConsole::Exit();
141}
142
e6be3e7c 143// ============================================================================
05e2b077 144// wxApp
e6be3e7c
MB
145// ============================================================================
146
4bb6408c
JS
147wxApp::wxApp()
148{
4bb6408c
JS
149 argc = 0;
150 argv = NULL;
afb74891 151
f15b55ce 152 m_mainLoop = new wxEventLoop;
4bb6408c
JS
153 m_mainColormap = (WXColormap) NULL;
154 m_appContext = (WXAppContext) NULL;
47bc1060 155 m_initialDisplay = (WXDisplay*) 0;
eb6fa4b4 156 m_perDisplayData = new wxPerDisplayDataMap;
4bb6408c
JS
157}
158
7e1bcfa8
MB
159wxApp::~wxApp()
160{
57bfe3be 161 wxApp::SetInstance(NULL);
7e1bcfa8
MB
162}
163
4bb6408c
JS
164int wxApp::MainLoop()
165{
2d120f83 166 /*
4bb6408c
JS
167 * Sit around forever waiting to process X-events. Property Change
168 * event are handled special, because they have to refer to
169 * the root window rather than to a widget. therefore we can't
170 * use an Xt-eventhandler.
171 */
afb74891 172
4bb6408c 173 XSelectInput(XtDisplay((Widget) wxTheApp->GetTopLevelWidget()),
2d120f83
JS
174 XDefaultRootWindow(XtDisplay((Widget) wxTheApp->GetTopLevelWidget())),
175 PropertyChangeMask);
afb74891 176
f15b55ce 177 m_mainLoop->Run();
afb74891 178
8aa04e8b
JS
179 return 0;
180}
181
4bb6408c
JS
182// This should be redefined in a derived class for
183// handling property change events for XAtom IPC.
184void wxApp::HandlePropertyChange(WXEvent *event)
185{
186 // by default do nothing special
187 XtDispatchEvent((XEvent*) event); /* let Motif do the work */
188}
189
edc1cd8b 190static char *fallbackResources[] = {
0ab82ca9
VZ
191 // better defaults for CDE under Irix
192 //
193 // TODO: do something similar for the other systems, the hardcoded defaults
194 // below are ugly
195#ifdef __SGI__
f1db433a
VZ
196 wxMOTIF_STR("*sgiMode: True"),
197 wxMOTIF_STR("*useSchemes: all"),
0ab82ca9 198#else // !__SGI__
f1db433a
VZ
199 wxMOTIF_STR("*menuBar.marginHeight: 0"),
200 wxMOTIF_STR("*menuBar.shadowThickness: 1"),
201 wxMOTIF_STR("*background: #c0c0c0"),
202 wxMOTIF_STR("*foreground: black"),
0ab82ca9 203#endif // __SGI__/!__SGI__
edc1cd8b
JS
204 NULL
205};
206
4bb6408c
JS
207// Create an application context
208bool wxApp::OnInitGui()
209{
fd304d98 210 if( !wxAppBase::OnInitGui() )
96be256b 211 return false;
fd304d98 212
9d02485e 213 XtSetLanguageProc(NULL, NULL, NULL);
4bb6408c 214 XtToolkitInitialize() ;
edc1cd8b
JS
215 wxTheApp->m_appContext = (WXAppContext) XtCreateApplicationContext();
216 XtAppSetFallbackResources((XtAppContext) wxTheApp->m_appContext, fallbackResources);
217
4bb6408c 218 Display *dpy = XtOpenDisplay((XtAppContext) wxTheApp->m_appContext,(String)NULL,NULL,
d3a80c92 219 wxTheApp->GetClassName().c_str(), NULL, 0,
4bb6408c 220# if XtSpecificationRelease < 5
9c6e1335 221 (Cardinal*) &argc,
4bb6408c 222# else
9c6e1335 223 &argc,
4bb6408c 224# endif
9c6e1335
VZ
225 argv);
226
4bb6408c 227 if (!dpy) {
c88b736c
MB
228 // if you don't log to stderr, nothing will be shown...
229 delete wxLog::SetActiveTarget(new wxLogStderr);
7bcb11d3 230 wxString className(wxTheApp->GetClassName());
77ffb593 231 wxLogError(_("wxWidgets could not open display for '%s': exiting."),
d3a80c92 232 className.c_str());
2d120f83 233 exit(-1);
4bb6408c 234 }
47bc1060 235 m_initialDisplay = (WXDisplay*) dpy;
afb74891 236
e838cc14
VZ
237#ifdef __WXDEBUG__
238 // install the X error handler
239 gs_pfnXErrorHandler = XSetErrorHandler(wxXErrorHandler);
240#endif // __WXDEBUG__
241
4bb6408c
JS
242 // Add general resize proc
243 XtActionsRec rec;
f1db433a 244 rec.string = wxMOTIF_STR("resize");
4bb6408c
JS
245 rec.proc = (XtActionProc)wxWidgetResizeProc;
246 XtAppAddActions((XtAppContext) wxTheApp->m_appContext, &rec, 1);
afb74891 247
4bb6408c 248 GetMainColormap(dpy);
afb74891 249
7e1bcfa8
MB
250 wxAddIdleCallback();
251
96be256b 252 return true;
4bb6408c
JS
253}
254
255WXColormap wxApp::GetMainColormap(WXDisplay* display)
256{
257 if (!display) /* Must be called first with non-NULL display */
2d120f83 258 return m_mainColormap;
a91b47e8
JS
259
260 int defaultScreen = DefaultScreen((Display*) display);
261 Screen* screen = XScreenOfDisplay((Display*) display, defaultScreen);
afb74891 262
a91b47e8 263 Colormap c = DefaultColormapOfScreen(screen);
afb74891 264
4bb6408c 265 if (!m_mainColormap)
2d120f83 266 m_mainColormap = (WXColormap) c;
afb74891 267
4bb6408c
JS
268 return (WXColormap) c;
269}
270
bdcade0a
MB
271static inline wxPerDisplayData& GetOrCreatePerDisplayData
272 ( wxPerDisplayDataMap& m, WXDisplay* display )
9ce8d6a2 273{
bdcade0a
MB
274 wxPerDisplayDataMap::iterator it = m.find( display );
275 if( it != m.end() && it->second != NULL )
276 return *(it->second);
277
278 wxPerDisplayData* nData = new wxPerDisplayData();
279 m[display] = nData;
280
281 return *nData;
282}
9ce8d6a2 283
bdcade0a
MB
284wxXVisualInfo* wxApp::GetVisualInfo( WXDisplay* display )
285{
286 wxPerDisplayData& data = GetOrCreatePerDisplayData( *m_perDisplayData,
287 display );
288 if( data.m_visualInfo )
289 return data.m_visualInfo;
9ce8d6a2
MB
290
291 wxXVisualInfo* vi = new wxXVisualInfo;
292 wxFillXVisualInfo( vi, (Display*)display );
293
bdcade0a 294 data.m_visualInfo = vi;
9ce8d6a2
MB
295
296 return vi;
297}
298
eb6fa4b4
MB
299static void wxTLWidgetDestroyCallback(Widget w, XtPointer clientData,
300 XtPointer ptr)
301{
302 if( wxTheApp )
5a2e3d8c 303 {
eb6fa4b4
MB
304 wxTheApp->SetTopLevelWidget( (WXDisplay*)XtDisplay(w),
305 (WXWidget)NULL );
5a2e3d8c
MB
306 wxTheApp->SetTopLevelRealizedWidget( (WXDisplay*)XtDisplay(w),
307 (WXWidget)NULL );
308 }
eb6fa4b4
MB
309}
310
311WXWidget wxCreateTopLevelWidget( WXDisplay* display )
312{
313 Widget tlw = XtAppCreateShell( (String)NULL,
314 wxTheApp->GetClassName().c_str(),
315 applicationShellWidgetClass,
316 (Display*)display,
317 NULL, 0 );
5a2e3d8c
MB
318 XtVaSetValues( tlw,
319 XmNoverrideRedirect, True,
320 NULL );
eb6fa4b4
MB
321
322 XtAddCallback( tlw, XmNdestroyCallback,
323 (XtCallbackProc)wxTLWidgetDestroyCallback,
324 (XtPointer)NULL );
325
326 return (WXWidget)tlw;
327}
328
5a2e3d8c
MB
329WXWidget wxCreateTopLevelRealizedWidget( WXDisplay* display )
330{
cb81f1a6 331 Widget rTlw = XtVaCreateWidget( "dummy_widget", topLevelShellWidgetClass,
5a2e3d8c 332 (Widget)wxTheApp->GetTopLevelWidget(),
cb81f1a6 333 NULL, 0 );
5a2e3d8c
MB
334 XtSetMappedWhenManaged( rTlw, False );
335 XtRealizeWidget( rTlw );
336
337 return (WXWidget)rTlw;
338}
339
eb6fa4b4
MB
340WXWidget wxApp::GetTopLevelWidget()
341{
342 WXDisplay* display = wxGetDisplay();
bdcade0a
MB
343 wxPerDisplayData& data = GetOrCreatePerDisplayData( *m_perDisplayData,
344 display );
345 if( data.m_topLevelWidget )
346 return (WXWidget)data.m_topLevelWidget;
eb6fa4b4
MB
347
348 WXWidget tlw = wxCreateTopLevelWidget( display );
349 SetTopLevelWidget( display, tlw );
350
351 return tlw;
352}
353
5a2e3d8c
MB
354WXWidget wxApp::GetTopLevelRealizedWidget()
355{
356 WXDisplay* display = wxGetDisplay();
357 wxPerDisplayDataMap::iterator it = m_perDisplayData->find( display );
358
359 if( it != m_perDisplayData->end() && it->second->m_topLevelRealizedWidget )
360 return (WXWidget)it->second->m_topLevelRealizedWidget;
361
362 WXWidget rTlw = wxCreateTopLevelRealizedWidget( display );
363 SetTopLevelRealizedWidget( display, rTlw );
364
365 return rTlw;
366}
367
eb6fa4b4
MB
368void wxApp::SetTopLevelWidget(WXDisplay* display, WXWidget widget)
369{
5a2e3d8c
MB
370 GetOrCreatePerDisplayData( *m_perDisplayData, display )
371 .m_topLevelWidget = (Widget)widget;
372}
373
374void wxApp::SetTopLevelRealizedWidget(WXDisplay* display, WXWidget widget)
375{
376 GetOrCreatePerDisplayData( *m_perDisplayData, display )
377 .m_topLevelRealizedWidget = (Widget)widget;
eb6fa4b4
MB
378}
379
4bb6408c 380// Yield to other processes
cb2713bf 381
8461e4c2 382bool wxApp::Yield(bool onlyIfNeeded)
4bb6408c 383{
96be256b 384 static bool s_inYield = false;
8461e4c2
VZ
385
386 if ( s_inYield )
387 {
388 if ( !onlyIfNeeded )
389 {
390 wxFAIL_MSG( wxT("wxYield called recursively" ) );
391 }
392
96be256b 393 return false;
8461e4c2
VZ
394 }
395
96be256b 396 s_inYield = true;
cb2713bf 397
4bb6408c 398 while (wxTheApp && wxTheApp->Pending())
2d120f83 399 wxTheApp->Dispatch();
518b5d2f 400
96be256b 401 s_inYield = false;
cb2713bf 402
96be256b 403 return true;
4bb6408c
JS
404}
405
d391a345
VZ
406// ----------------------------------------------------------------------------
407// accessors for C modules
408// ----------------------------------------------------------------------------
409
410extern "C" XtAppContext wxGetAppContext()
411{
412 return (XtAppContext)wxTheApp->GetAppContext();
413}