]> git.saurik.com Git - wxWidgets.git/blob - src/motif/app.cpp
suppress warning about converting -1 to MenuItemIndex
[wxWidgets.git] / src / motif / app.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/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
22 #ifndef WX_PRECOMP
23 #include "wx/hash.h"
24 #include "wx/intl.h"
25 #include "wx/log.h"
26 #include "wx/utils.h"
27 #include "wx/memory.h"
28 #include "wx/font.h"
29 #endif
30
31 #include "wx/evtloop.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 bool wxAddIdleCallback();
71
72 wxHashTable *wxWidgetHashTable = NULL;
73
74 IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
75
76 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
77 EVT_IDLE(wxAppBase::OnIdle)
78 END_EVENT_TABLE()
79
80 #ifdef __WXDEBUG__
81 extern "C"
82 {
83 typedef int (*XErrorHandlerFunc)(Display *, XErrorEvent *);
84 }
85
86 XErrorHandlerFunc gs_pfnXErrorHandler = 0;
87
88 extern "C"
89 {
90
91 static int wxXErrorHandler(Display *dpy, XErrorEvent *xevent)
92 {
93 // just forward to the default handler for now
94 return gs_pfnXErrorHandler(dpy, xevent);
95 }
96
97 }
98 #endif // __WXDEBUG__
99
100 bool wxApp::Initialize(int& argcOrig, wxChar **argvOrig)
101 {
102 if ( !wxAppBase::Initialize(argcOrig, argvOrig) )
103 return false;
104
105 wxWidgetHashTable = new wxHashTable(wxKEY_INTEGER);
106
107 #if wxUSE_INTL
108 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
109 #endif
110
111 return true;
112 }
113
114 void wxApp::CleanUp()
115 {
116 wxAppBase::CleanUp();
117
118 delete wxWidgetHashTable;
119 wxWidgetHashTable = NULL;
120
121 delete m_mainLoop;
122
123 for( wxPerDisplayDataMap::iterator it = m_perDisplayData->begin(),
124 end = m_perDisplayData->end();
125 it != end; ++it )
126 {
127 delete it->second->m_visualInfo;
128 // On Solaris 10 calling XtDestroyWidget on the top level widget
129 // dumps core if the locale is set to something other than "C"
130 #ifndef __SUN__
131 XtDestroyWidget( it->second->m_topLevelWidget );
132 #endif
133 delete it->second;
134 }
135 }
136
137 void wxApp::Exit()
138 {
139 wxApp::CleanUp();
140
141 wxAppConsole::Exit();
142 }
143
144 // ============================================================================
145 // wxApp
146 // ============================================================================
147
148 wxApp::wxApp()
149 {
150 argc = 0;
151 argv = NULL;
152
153 m_mainLoop = new wxEventLoop;
154 m_mainColormap = (WXColormap) NULL;
155 m_appContext = (WXAppContext) NULL;
156 m_initialDisplay = (WXDisplay*) 0;
157 m_perDisplayData = new wxPerDisplayDataMap;
158 }
159
160 wxApp::~wxApp()
161 {
162 delete m_perDisplayData;
163 }
164
165 int wxApp::MainLoop()
166 {
167 /*
168 * Sit around forever waiting to process X-events. Property Change
169 * event are handled special, because they have to refer to
170 * the root window rather than to a widget. therefore we can't
171 * use an Xt-eventhandler.
172 */
173
174 XSelectInput(XtDisplay((Widget) wxTheApp->GetTopLevelWidget()),
175 XDefaultRootWindow(XtDisplay((Widget) wxTheApp->GetTopLevelWidget())),
176 PropertyChangeMask);
177
178 m_mainLoop->Run();
179
180 return 0;
181 }
182
183 // This should be redefined in a derived class for
184 // handling property change events for XAtom IPC.
185 void wxApp::HandlePropertyChange(WXEvent *event)
186 {
187 // by default do nothing special
188 XtDispatchEvent((XEvent*) event); /* let Motif do the work */
189 }
190
191 static char *fallbackResources[] = {
192 // better defaults for CDE under Irix
193 //
194 // TODO: do something similar for the other systems, the hardcoded defaults
195 // below are ugly
196 #ifdef __SGI__
197 wxMOTIF_STR("*sgiMode: True"),
198 wxMOTIF_STR("*useSchemes: all"),
199 #else // !__SGI__
200 wxMOTIF_STR("*menuBar.marginHeight: 0"),
201 wxMOTIF_STR("*menuBar.shadowThickness: 1"),
202 wxMOTIF_STR("*background: #c0c0c0"),
203 wxMOTIF_STR("*foreground: black"),
204 #endif // __SGI__/!__SGI__
205 NULL
206 };
207
208 // Create an application context
209 bool wxApp::OnInitGui()
210 {
211 if( !wxAppBase::OnInitGui() )
212 return false;
213
214 #ifdef __HPUX__
215 // under HP-UX creating XmFontSet fails when the system locale is C and
216 // we're using a remote DISPLAY, presumably because HP-UX uses its own
217 // names for C and ISO locales (roman8 and iso8859n respectively) and so
218 // its Motif libraries have troubles with non-HP X server
219 //
220 // whatever the reason, the fact is that without this hack any wxMotif
221 // program crashes on startup because it can't create any font (HP programs
222 // still work but they do spit out messages about failing to create font
223 // sets and failing back on "fixed" font too)
224 //
225 // notice that calling setlocale() here is not enough because X(m) init
226 // functions call setlocale() later so we really have to change environment
227 bool fixAll = false; // tweak LC_ALL (or just LC_CTYPE)?
228 const char *loc = getenv("LC_CTYPE");
229 if ( !loc )
230 {
231 loc = getenv("LC_ALL");
232 if ( loc )
233 fixAll = true;
234 }
235
236 if ( !loc ||
237 (loc[0] == 'C' && loc[1] == '\0') ||
238 strcmp(loc, "POSIX") == 0 )
239 {
240 // we're using C locale, "fix" it
241 wxLogDebug(_T("HP-UX fontset hack: forcing locale to en_US.iso88591"));
242 putenv(fixAll ? "LC_ALL=en_US.iso88591" : "LC_CTYPE=en_US.iso88591");
243 }
244 #endif // __HPUX__
245
246 XtSetLanguageProc(NULL, NULL, NULL);
247 XtToolkitInitialize() ;
248 wxTheApp->m_appContext = (WXAppContext) XtCreateApplicationContext();
249 XtAppSetFallbackResources((XtAppContext) wxTheApp->m_appContext, fallbackResources);
250
251 // we shouldn't pass empty application/class name as it results in
252 // immediate crash inside XOpenIM() (if XIM is used) under IRIX
253 wxString appname = wxTheApp->GetAppName();
254 if ( appname.empty() )
255 appname = _T("wxapp");
256 wxString clsname = wxTheApp->GetClassName();
257 if ( clsname.empty() )
258 clsname = _T("wx");
259
260 Display *dpy = XtOpenDisplay((XtAppContext) wxTheApp->m_appContext,
261 (String)NULL,
262 appname.c_str(),
263 clsname.c_str(),
264 NULL, 0, // no options
265 # if XtSpecificationRelease < 5
266 (Cardinal*) &argc,
267 # else
268 &argc,
269 # endif
270 argv);
271
272 if (!dpy) {
273 // if you don't log to stderr, nothing will be shown...
274 delete wxLog::SetActiveTarget(new wxLogStderr);
275 wxString className(wxTheApp->GetClassName());
276 wxLogError(_("wxWidgets could not open display for '%s': exiting."),
277 className.c_str());
278 exit(-1);
279 }
280 m_initialDisplay = (WXDisplay*) dpy;
281
282 #ifdef __WXDEBUG__
283 // install the X error handler
284 gs_pfnXErrorHandler = XSetErrorHandler(wxXErrorHandler);
285 #endif // __WXDEBUG__
286
287 // Add general resize proc
288 XtActionsRec rec;
289 rec.string = wxMOTIF_STR("resize");
290 rec.proc = (XtActionProc)wxWidgetResizeProc;
291 XtAppAddActions((XtAppContext) wxTheApp->m_appContext, &rec, 1);
292
293 GetMainColormap(dpy);
294
295 wxAddIdleCallback();
296
297 return true;
298 }
299
300 WXColormap wxApp::GetMainColormap(WXDisplay* display)
301 {
302 if (!display) /* Must be called first with non-NULL display */
303 return m_mainColormap;
304
305 int defaultScreen = DefaultScreen((Display*) display);
306 Screen* screen = XScreenOfDisplay((Display*) display, defaultScreen);
307
308 Colormap c = DefaultColormapOfScreen(screen);
309
310 if (!m_mainColormap)
311 m_mainColormap = (WXColormap) c;
312
313 return (WXColormap) c;
314 }
315
316 static inline wxPerDisplayData& GetOrCreatePerDisplayData
317 ( wxPerDisplayDataMap& m, WXDisplay* display )
318 {
319 wxPerDisplayDataMap::iterator it = m.find( display );
320 if( it != m.end() && it->second != NULL )
321 return *(it->second);
322
323 wxPerDisplayData* nData = new wxPerDisplayData();
324 m[display] = nData;
325
326 return *nData;
327 }
328
329 wxXVisualInfo* wxApp::GetVisualInfo( WXDisplay* display )
330 {
331 wxPerDisplayData& data = GetOrCreatePerDisplayData( *m_perDisplayData,
332 display );
333 if( data.m_visualInfo )
334 return data.m_visualInfo;
335
336 wxXVisualInfo* vi = new wxXVisualInfo;
337 wxFillXVisualInfo( vi, (Display*)display );
338
339 data.m_visualInfo = vi;
340
341 return vi;
342 }
343
344 static void wxTLWidgetDestroyCallback(Widget w, XtPointer WXUNUSED(clientData),
345 XtPointer WXUNUSED(ptr))
346 {
347 if( wxTheApp )
348 {
349 wxTheApp->SetTopLevelWidget( (WXDisplay*)XtDisplay(w),
350 (WXWidget)NULL );
351 wxTheApp->SetTopLevelRealizedWidget( (WXDisplay*)XtDisplay(w),
352 (WXWidget)NULL );
353 }
354 }
355
356 WXWidget wxCreateTopLevelWidget( WXDisplay* display )
357 {
358 Widget tlw = XtAppCreateShell( (String)NULL,
359 wxTheApp->GetClassName().c_str(),
360 applicationShellWidgetClass,
361 (Display*)display,
362 NULL, 0 );
363 XtVaSetValues( tlw,
364 XmNoverrideRedirect, True,
365 NULL );
366
367 XtAddCallback( tlw, XmNdestroyCallback,
368 (XtCallbackProc)wxTLWidgetDestroyCallback,
369 (XtPointer)NULL );
370
371 return (WXWidget)tlw;
372 }
373
374 WXWidget wxCreateTopLevelRealizedWidget( WXDisplay* WXUNUSED(display) )
375 {
376 Widget rTlw = XtVaCreateWidget( "dummy_widget", topLevelShellWidgetClass,
377 (Widget)wxTheApp->GetTopLevelWidget(),
378 NULL );
379 XtSetMappedWhenManaged( rTlw, False );
380 XtRealizeWidget( rTlw );
381
382 return (WXWidget)rTlw;
383 }
384
385 WXWidget wxApp::GetTopLevelWidget()
386 {
387 WXDisplay* display = wxGetDisplay();
388 wxPerDisplayData& data = GetOrCreatePerDisplayData( *m_perDisplayData,
389 display );
390 if( data.m_topLevelWidget )
391 return (WXWidget)data.m_topLevelWidget;
392
393 WXWidget tlw = wxCreateTopLevelWidget( display );
394 SetTopLevelWidget( display, tlw );
395
396 return tlw;
397 }
398
399 WXWidget wxApp::GetTopLevelRealizedWidget()
400 {
401 WXDisplay* display = wxGetDisplay();
402 wxPerDisplayDataMap::iterator it = m_perDisplayData->find( display );
403
404 if( it != m_perDisplayData->end() && it->second->m_topLevelRealizedWidget )
405 return (WXWidget)it->second->m_topLevelRealizedWidget;
406
407 WXWidget rTlw = wxCreateTopLevelRealizedWidget( display );
408 SetTopLevelRealizedWidget( display, rTlw );
409
410 return rTlw;
411 }
412
413 void wxApp::SetTopLevelWidget(WXDisplay* display, WXWidget widget)
414 {
415 GetOrCreatePerDisplayData( *m_perDisplayData, display )
416 .m_topLevelWidget = (Widget)widget;
417 }
418
419 void wxApp::SetTopLevelRealizedWidget(WXDisplay* display, WXWidget widget)
420 {
421 GetOrCreatePerDisplayData( *m_perDisplayData, display )
422 .m_topLevelRealizedWidget = (Widget)widget;
423 }
424
425 // Yield to other processes
426
427 bool wxApp::Yield(bool onlyIfNeeded)
428 {
429 static bool s_inYield = false;
430
431 if ( s_inYield )
432 {
433 if ( !onlyIfNeeded )
434 {
435 wxFAIL_MSG( wxT("wxYield called recursively" ) );
436 }
437
438 return false;
439 }
440
441 s_inYield = true;
442
443 while (wxTheApp && wxTheApp->Pending())
444 wxTheApp->Dispatch();
445
446 s_inYield = false;
447
448 return true;
449 }
450
451 // ----------------------------------------------------------------------------
452 // accessors for C modules
453 // ----------------------------------------------------------------------------
454
455 extern "C" XtAppContext wxGetAppContext()
456 {
457 return (XtAppContext)wxTheApp->GetAppContext();
458 }