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