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