1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/app.cpp
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
16 #define XtParent XTPARENT
17 #define XtDisplay XTDISPLAY
27 #include "wx/memory.h"
31 #include "wx/evtloop.h"
34 #include "wx/thread.h"
38 #pragma message disable nosimpint
42 #include <X11/Xutil.h>
43 #include <X11/Xresource.h>
44 #include <X11/Xatom.h>
46 #pragma message enable nosimpint
49 #include "wx/motif/private.h"
53 struct wxPerDisplayData
58 m_topLevelWidget
= NULL
;
59 m_topLevelRealizedWidget
= NULL
;
62 wxXVisualInfo
* m_visualInfo
;
63 Widget m_topLevelWidget
, m_topLevelRealizedWidget
;
66 static void wxTLWidgetDestroyCallback(Widget w
, XtPointer clientData
,
68 static WXWidget
wxCreateTopLevelWidget( WXDisplay
* display
);
70 extern bool wxAddIdleCallback();
72 wxHashTable
*wxWidgetHashTable
= NULL
;
74 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
79 typedef int (*XErrorHandlerFunc
)(Display
*, XErrorEvent
*);
82 XErrorHandlerFunc gs_pfnXErrorHandler
= 0;
87 static int wxXErrorHandler(Display
*dpy
, XErrorEvent
*xevent
)
89 // just forward to the default handler for now
90 return gs_pfnXErrorHandler(dpy
, xevent
);
96 bool wxApp::Initialize(int& argc_
, wxChar
**argv_
)
99 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
102 if ( !wxAppBase::Initialize(argc_
, argv_
) )
105 wxWidgetHashTable
= new wxHashTable(wxKEY_INTEGER
);
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
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)
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");
124 loc
= getenv("LC_ALL");
130 (loc
[0] == 'C' && loc
[1] == '\0') ||
131 strcmp(loc
, "POSIX") == 0 )
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");
139 XtSetLanguageProc(NULL
, NULL
, NULL
);
140 XtToolkitInitialize() ;
141 wxTheApp
->m_appContext
= (WXAppContext
) XtCreateApplicationContext();
143 static char *fallbackResources
[] = {
144 // better defaults for CDE under Irix
146 // TODO: do something similar for the other systems, the hardcoded defaults
149 wxMOTIF_STR("*sgiMode: True"),
150 wxMOTIF_STR("*useSchemes: all"),
152 #if !wxMOTIF_USE_RENDER_TABLE
153 wxMOTIF_STR("*.fontList: -*-helvetica-medium-r-normal-*-*-120-*-*-*-*-*-*"),
155 wxMOTIF_STR("*wxDefaultRendition.fontName: -*-helvetica-medium-r-normal-*-*-120-*-*-*-*-*-*"),
156 wxMOTIF_STR("*wxDefaultRendition.fontType: FONT_IS_FONTSET"),
157 wxMOTIF_STR("*.renderTable: wxDefaultRendition"),
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__
166 XtAppSetFallbackResources((XtAppContext
) wxTheApp
->m_appContext
, fallbackResources
);
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() )
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.
182 // XtOpenDisplay() wants char*, not wchar_t*, so convert
184 char **argvX11
= new char *[argc
+ 1];
185 for ( i
= 0; i
< argc
; i
++ )
187 argvX11
[i
] = strdup(wxConvLibc
.cWX2MB(argv_
[i
]));
190 argvX11
[argc
] = NULL
;
194 Display
*dpy
= XtOpenDisplay((XtAppContext
) wxTheApp
->m_appContext
,
198 NULL
, 0, // no options
199 # if XtSpecificationRelease < 5
200 (Cardinal
*) &argcX11
,
206 if ( argcX11
!= argc
)
208 // we have to drop the parameters which were consumed by X11+
209 for ( i
= 0; i
< argcX11
; i
++ )
211 while ( strcmp(wxConvLibc
.cWX2MB(argv_
[i
]), argvX11
[i
]) != 0 )
213 memmove(argv_
+ i
, argv_
+ i
+ 1, (argc
- i
)*sizeof(*argv_
));
219 // update internal arg[cv] as X11 may have removed processed options:
223 //else: XtOpenDisplay() didn't modify our parameters
226 for ( i
= 0; i
< argcX11
; i
++ )
235 Display
*dpy
= XtOpenDisplay((XtAppContext
) wxTheApp
->m_appContext
,
239 NULL
, 0, // no options
240 # if XtSpecificationRelease < 5
247 #endif // Unicode/ANSI
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."),
257 m_initialDisplay
= (WXDisplay
*) dpy
;
260 // install the X error handler
261 gs_pfnXErrorHandler
= XSetErrorHandler(wxXErrorHandler
);
262 #endif // __WXDEBUG__
264 // Add general resize proc
266 rec
.string
= wxMOTIF_STR("resize");
267 rec
.proc
= (XtActionProc
)wxWidgetResizeProc
;
268 XtAppAddActions((XtAppContext
) wxTheApp
->m_appContext
, &rec
, 1);
270 GetMainColormap(dpy
);
277 void wxApp::CleanUp()
279 wxAppBase::CleanUp();
281 delete wxWidgetHashTable
;
282 wxWidgetHashTable
= NULL
;
286 for( wxPerDisplayDataMap::iterator it
= m_perDisplayData
->begin(),
287 end
= m_perDisplayData
->end();
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"
294 XtDestroyWidget( it
->second
->m_topLevelWidget
);
304 wxAppConsole::Exit();
307 // ============================================================================
309 // ============================================================================
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
;
322 delete m_perDisplayData
;
325 int wxApp::MainLoop()
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.
334 XSelectInput(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()),
335 XDefaultRootWindow(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget())),
343 // This should be redefined in a derived class for
344 // handling property change events for XAtom IPC.
345 void wxApp::HandlePropertyChange(WXEvent
*event
)
347 // by default do nothing special
348 XtDispatchEvent((XEvent
*) event
); /* let Motif do the work */
351 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
353 if (!display
) /* Must be called first with non-NULL display */
354 return m_mainColormap
;
356 int defaultScreen
= DefaultScreen((Display
*) display
);
357 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
359 Colormap c
= DefaultColormapOfScreen(screen
);
362 m_mainColormap
= (WXColormap
) c
;
364 return (WXColormap
) c
;
367 static inline wxPerDisplayData
& GetOrCreatePerDisplayData
368 ( wxPerDisplayDataMap
& m
, WXDisplay
* display
)
370 wxPerDisplayDataMap::iterator it
= m
.find( display
);
371 if( it
!= m
.end() && it
->second
!= NULL
)
372 return *(it
->second
);
374 wxPerDisplayData
* nData
= new wxPerDisplayData();
380 wxXVisualInfo
* wxApp::GetVisualInfo( WXDisplay
* display
)
382 wxPerDisplayData
& data
= GetOrCreatePerDisplayData( *m_perDisplayData
,
384 if( data
.m_visualInfo
)
385 return data
.m_visualInfo
;
387 wxXVisualInfo
* vi
= new wxXVisualInfo
;
388 wxFillXVisualInfo( vi
, (Display
*)display
);
390 data
.m_visualInfo
= vi
;
395 static void wxTLWidgetDestroyCallback(Widget w
, XtPointer
WXUNUSED(clientData
),
396 XtPointer
WXUNUSED(ptr
))
400 wxTheApp
->SetTopLevelWidget( (WXDisplay
*)XtDisplay(w
),
402 wxTheApp
->SetTopLevelRealizedWidget( (WXDisplay
*)XtDisplay(w
),
407 WXWidget
wxCreateTopLevelWidget( WXDisplay
* display
)
409 Widget tlw
= XtAppCreateShell( (String
)NULL
,
410 wxTheApp
->GetClassName().c_str(),
411 applicationShellWidgetClass
,
415 XmNoverrideRedirect
, True
,
418 XtAddCallback( tlw
, XmNdestroyCallback
,
419 (XtCallbackProc
)wxTLWidgetDestroyCallback
,
422 return (WXWidget
)tlw
;
425 WXWidget
wxCreateTopLevelRealizedWidget( WXDisplay
* WXUNUSED(display
) )
427 Widget rTlw
= XtVaCreateWidget( "dummy_widget", topLevelShellWidgetClass
,
428 (Widget
)wxTheApp
->GetTopLevelWidget(),
430 XtSetMappedWhenManaged( rTlw
, False
);
431 XtRealizeWidget( rTlw
);
433 return (WXWidget
)rTlw
;
436 WXWidget
wxApp::GetTopLevelWidget()
438 WXDisplay
* display
= wxGetDisplay();
439 wxPerDisplayData
& data
= GetOrCreatePerDisplayData( *m_perDisplayData
,
441 if( data
.m_topLevelWidget
)
442 return (WXWidget
)data
.m_topLevelWidget
;
444 WXWidget tlw
= wxCreateTopLevelWidget( display
);
445 SetTopLevelWidget( display
, tlw
);
450 WXWidget
wxApp::GetTopLevelRealizedWidget()
452 WXDisplay
* display
= wxGetDisplay();
453 wxPerDisplayDataMap::iterator it
= m_perDisplayData
->find( display
);
455 if( it
!= m_perDisplayData
->end() && it
->second
->m_topLevelRealizedWidget
)
456 return (WXWidget
)it
->second
->m_topLevelRealizedWidget
;
458 WXWidget rTlw
= wxCreateTopLevelRealizedWidget( display
);
459 SetTopLevelRealizedWidget( display
, rTlw
);
464 void wxApp::SetTopLevelWidget(WXDisplay
* display
, WXWidget widget
)
466 GetOrCreatePerDisplayData( *m_perDisplayData
, display
)
467 .m_topLevelWidget
= (Widget
)widget
;
470 void wxApp::SetTopLevelRealizedWidget(WXDisplay
* display
, WXWidget widget
)
472 GetOrCreatePerDisplayData( *m_perDisplayData
, display
)
473 .m_topLevelRealizedWidget
= (Widget
)widget
;
476 // Yield to other processes
478 bool wxApp::Yield(bool onlyIfNeeded
)
480 static bool s_inYield
= false;
486 wxFAIL_MSG( wxT("wxYield called recursively" ) );
494 while (wxTheApp
&& wxTheApp
->Pending())
495 wxTheApp
->Dispatch();
502 // ----------------------------------------------------------------------------
503 // accessors for C modules
504 // ----------------------------------------------------------------------------
506 extern "C" XtAppContext
wxGetAppContext()
508 return (XtAppContext
)wxTheApp
->GetAppContext();