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