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