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