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