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