]> git.saurik.com Git - wxWidgets.git/blob - src/motif/app.cpp
3cf1ef5e64a54a17badda39a60dd49e8f026323f
[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 <X11/Xlib.h>
42 #include <X11/Xutil.h>
43 #include <X11/Xresource.h>
44 #include <X11/Xatom.h>
45 #ifdef __VMS__
46 #pragma message enable nosimpint
47 #endif
48
49 #include "wx/motif/private.h"
50
51 #include <string.h>
52
53 struct wxPerDisplayData
54 {
55 wxPerDisplayData()
56 { m_visualInfo = NULL; m_topLevelWidget = NULL; }
57
58 wxXVisualInfo* m_visualInfo;
59 Widget m_topLevelWidget;
60 };
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_mainLoop = 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_mainLoop;
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 delete it->second;
141 }
142
143 delete m_perDisplayData;
144
145 wxApp::SetInstance(NULL);
146 }
147
148 int wxApp::MainLoop()
149 {
150 /*
151 * Sit around forever waiting to process X-events. Property Change
152 * event are handled special, because they have to refer to
153 * the root window rather than to a widget. therefore we can't
154 * use an Xt-eventhandler.
155 */
156
157 XSelectInput(XtDisplay((Widget) wxTheApp->GetTopLevelWidget()),
158 XDefaultRootWindow(XtDisplay((Widget) wxTheApp->GetTopLevelWidget())),
159 PropertyChangeMask);
160
161 m_mainLoop->Run();
162
163 return 0;
164 }
165
166 // This should be redefined in a derived class for
167 // handling property change events for XAtom IPC.
168 void wxApp::HandlePropertyChange(WXEvent *event)
169 {
170 // by default do nothing special
171 XtDispatchEvent((XEvent*) event); /* let Motif do the work */
172 }
173
174 static char *fallbackResources[] = {
175 "*menuBar.marginHeight: 0",
176 "*menuBar.shadowThickness: 1",
177 "*background: #c0c0c0",
178 "*foreground: black",
179 NULL
180 };
181
182 // Create an application context
183 bool wxApp::OnInitGui()
184 {
185 if( !wxAppBase::OnInitGui() )
186 return FALSE;
187
188 XtToolkitInitialize() ;
189 wxTheApp->m_appContext = (WXAppContext) XtCreateApplicationContext();
190 XtAppSetFallbackResources((XtAppContext) wxTheApp->m_appContext, fallbackResources);
191
192 Display *dpy = XtOpenDisplay((XtAppContext) wxTheApp->m_appContext,(String)NULL,NULL,
193 wxTheApp->GetClassName().c_str(), NULL, 0,
194 # if XtSpecificationRelease < 5
195 (Cardinal*) &argc,
196 # else
197 &argc,
198 # endif
199 argv);
200
201 if (!dpy) {
202 // if you don't log to stderr, nothing will be shown...
203 delete wxLog::SetActiveTarget(new wxLogStderr);
204 wxString className(wxTheApp->GetClassName());
205 wxLogError(_("wxWindows could not open display for '%s': exiting."),
206 className.c_str());
207 exit(-1);
208 }
209 m_initialDisplay = (WXDisplay*) dpy;
210
211 #ifdef __WXDEBUG__
212 // install the X error handler
213 gs_pfnXErrorHandler = XSetErrorHandler(wxXErrorHandler);
214 #endif // __WXDEBUG__
215
216 // Add general resize proc
217 XtActionsRec rec;
218 rec.string = "resize";
219 rec.proc = (XtActionProc)wxWidgetResizeProc;
220 XtAppAddActions((XtAppContext) wxTheApp->m_appContext, &rec, 1);
221
222 GetMainColormap(dpy);
223
224 wxAddIdleCallback();
225
226 return TRUE;
227 }
228
229 WXColormap wxApp::GetMainColormap(WXDisplay* display)
230 {
231 if (!display) /* Must be called first with non-NULL display */
232 return m_mainColormap;
233
234 int defaultScreen = DefaultScreen((Display*) display);
235 Screen* screen = XScreenOfDisplay((Display*) display, defaultScreen);
236
237 Colormap c = DefaultColormapOfScreen(screen);
238
239 if (!m_mainColormap)
240 m_mainColormap = (WXColormap) c;
241
242 return (WXColormap) c;
243 }
244
245 static inline wxPerDisplayData& GetOrCreatePerDisplayData
246 ( wxPerDisplayDataMap& m, WXDisplay* display )
247 {
248 wxPerDisplayDataMap::iterator it = m.find( display );
249 if( it != m.end() && it->second != NULL )
250 return *(it->second);
251
252 wxPerDisplayData* nData = new wxPerDisplayData();
253 m[display] = nData;
254
255 return *nData;
256 }
257
258 wxXVisualInfo* wxApp::GetVisualInfo( WXDisplay* display )
259 {
260 wxPerDisplayData& data = GetOrCreatePerDisplayData( *m_perDisplayData,
261 display );
262 if( data.m_visualInfo )
263 return data.m_visualInfo;
264
265 wxXVisualInfo* vi = new wxXVisualInfo;
266 wxFillXVisualInfo( vi, (Display*)display );
267
268 data.m_visualInfo = vi;
269
270 return vi;
271 }
272
273 static void wxTLWidgetDestroyCallback(Widget w, XtPointer clientData,
274 XtPointer ptr)
275 {
276 if( wxTheApp )
277 wxTheApp->SetTopLevelWidget( (WXDisplay*)XtDisplay(w),
278 (WXWidget)NULL );
279 }
280
281 WXWidget wxCreateTopLevelWidget( WXDisplay* display )
282 {
283 Widget tlw = XtAppCreateShell( (String)NULL,
284 wxTheApp->GetClassName().c_str(),
285 applicationShellWidgetClass,
286 (Display*)display,
287 NULL, 0 );
288 XtSetMappedWhenManaged( tlw, False );
289 XtRealizeWidget( tlw );
290
291 XtAddCallback( tlw, XmNdestroyCallback,
292 (XtCallbackProc)wxTLWidgetDestroyCallback,
293 (XtPointer)NULL );
294
295 return (WXWidget)tlw;
296 }
297
298 WXWidget wxApp::GetTopLevelWidget()
299 {
300 WXDisplay* display = wxGetDisplay();
301 wxPerDisplayData& data = GetOrCreatePerDisplayData( *m_perDisplayData,
302 display );
303 if( data.m_topLevelWidget )
304 return (WXWidget)data.m_topLevelWidget;
305
306 WXWidget tlw = wxCreateTopLevelWidget( display );
307 SetTopLevelWidget( display, tlw );
308
309 return tlw;
310 }
311
312 void wxApp::SetTopLevelWidget(WXDisplay* display, WXWidget widget)
313 {
314 (*m_perDisplayData)[display]->m_topLevelWidget = (Widget)widget;
315 }
316
317 // Yield to other processes
318
319 bool wxApp::Yield(bool onlyIfNeeded)
320 {
321 static bool s_inYield = FALSE;
322
323 if ( s_inYield )
324 {
325 if ( !onlyIfNeeded )
326 {
327 wxFAIL_MSG( wxT("wxYield called recursively" ) );
328 }
329
330 return FALSE;
331 }
332
333 s_inYield = TRUE;
334
335 while (wxTheApp && wxTheApp->Pending())
336 wxTheApp->Dispatch();
337
338 s_inYield = FALSE;
339
340 return TRUE;
341 }
342
343 // ----------------------------------------------------------------------------
344 // accessors for C modules
345 // ----------------------------------------------------------------------------
346
347 extern "C" XtAppContext wxGetAppContext()
348 {
349 return (XtAppContext)wxTheApp->GetAppContext();
350 }