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