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