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"
28 #include "wx/module.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
)
76 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
77 EVT_IDLE(wxAppBase::OnIdle
)
83 typedef int (*XErrorHandlerFunc
)(Display
*, XErrorEvent
*);
86 XErrorHandlerFunc gs_pfnXErrorHandler
= 0;
91 static int wxXErrorHandler(Display
*dpy
, XErrorEvent
*xevent
)
93 // just forward to the default handler for now
94 return gs_pfnXErrorHandler(dpy
, xevent
);
100 bool wxApp::Initialize(int& argcOrig
, wxChar
**argvOrig
)
102 if ( !wxAppBase::Initialize(argcOrig
, argvOrig
) )
105 wxWidgetHashTable
= new wxHashTable(wxKEY_INTEGER
);
108 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
114 void wxApp::CleanUp()
116 wxAppBase::CleanUp();
118 delete wxWidgetHashTable
;
119 wxWidgetHashTable
= NULL
;
123 for( wxPerDisplayDataMap::iterator it
= m_perDisplayData
->begin(),
124 end
= m_perDisplayData
->end();
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"
131 XtDestroyWidget( it
->second
->m_topLevelWidget
);
136 delete m_perDisplayData
;
143 wxAppConsole::Exit();
146 // ============================================================================
148 // ============================================================================
155 m_mainLoop
= new wxEventLoop
;
156 m_mainColormap
= (WXColormap
) NULL
;
157 m_appContext
= (WXAppContext
) NULL
;
158 m_initialDisplay
= (WXDisplay
*) 0;
159 m_perDisplayData
= new wxPerDisplayDataMap
;
164 wxApp::SetInstance(NULL
);
167 int wxApp::MainLoop()
170 * Sit around forever waiting to process X-events. Property Change
171 * event are handled special, because they have to refer to
172 * the root window rather than to a widget. therefore we can't
173 * use an Xt-eventhandler.
176 XSelectInput(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()),
177 XDefaultRootWindow(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget())),
185 // This should be redefined in a derived class for
186 // handling property change events for XAtom IPC.
187 void wxApp::HandlePropertyChange(WXEvent
*event
)
189 // by default do nothing special
190 XtDispatchEvent((XEvent
*) event
); /* let Motif do the work */
193 static char *fallbackResources
[] = {
194 // better defaults for CDE under Irix
196 // TODO: do something similar for the other systems, the hardcoded defaults
199 wxMOTIF_STR("*sgiMode: True"),
200 wxMOTIF_STR("*useSchemes: all"),
202 wxMOTIF_STR("*menuBar.marginHeight: 0"),
203 wxMOTIF_STR("*menuBar.shadowThickness: 1"),
204 wxMOTIF_STR("*background: #c0c0c0"),
205 wxMOTIF_STR("*foreground: black"),
206 #endif // __SGI__/!__SGI__
210 // Create an application context
211 bool wxApp::OnInitGui()
213 if( !wxAppBase::OnInitGui() )
217 // under HP-UX creating XmFontSet fails when the system locale is C and
218 // we're using a remote DISPLAY, presumably because HP-UX uses its own
219 // names for C and ISO locales (roman8 and iso8859n respectively) and so
220 // its Motif libraries have troubles with non-HP X server
222 // whatever the reason, the fact is that without this hack any wxMotif
223 // program crashes on startup because it can't create any font (HP programs
224 // still work but they do spit out messages about failing to create font
225 // sets and failing back on "fixed" font too)
227 // notice that calling setlocale() here is not enough because X(m) init
228 // functions call setlocale() later so we really have to change environment
229 bool fixAll
= false; // tweak LC_ALL (or just LC_CTYPE)?
230 const char *loc
= getenv("LC_CTYPE");
233 loc
= getenv("LC_ALL");
239 (loc
[0] == 'C' && loc
[1] == '\0') ||
240 strcmp(loc
, "POSIX") == 0 )
242 // we're using C locale, "fix" it
243 wxLogDebug(_T("HP-UX fontset hack: forcing locale to en_US.iso88591"));
244 putenv(fixAll
? "LC_ALL=en_US.iso88591" : "LC_CTYPE=en_US.iso88591");
248 XtSetLanguageProc(NULL
, NULL
, NULL
);
249 XtToolkitInitialize() ;
250 wxTheApp
->m_appContext
= (WXAppContext
) XtCreateApplicationContext();
251 XtAppSetFallbackResources((XtAppContext
) wxTheApp
->m_appContext
, fallbackResources
);
253 // we shouldn't pass empty application/class name as it results in
254 // immediate crash inside XOpenIM() (if XIM is used) under IRIX
255 wxString appname
= wxTheApp
->GetAppName();
256 if ( appname
.empty() )
257 appname
= _T("wxapp");
258 wxString clsname
= wxTheApp
->GetClassName();
259 if ( clsname
.empty() )
262 Display
*dpy
= XtOpenDisplay((XtAppContext
) wxTheApp
->m_appContext
,
266 NULL
, 0, // no options
267 # if XtSpecificationRelease < 5
275 // if you don't log to stderr, nothing will be shown...
276 delete wxLog::SetActiveTarget(new wxLogStderr
);
277 wxString
className(wxTheApp
->GetClassName());
278 wxLogError(_("wxWidgets could not open display for '%s': exiting."),
282 m_initialDisplay
= (WXDisplay
*) dpy
;
285 // install the X error handler
286 gs_pfnXErrorHandler
= XSetErrorHandler(wxXErrorHandler
);
287 #endif // __WXDEBUG__
289 // Add general resize proc
291 rec
.string
= wxMOTIF_STR("resize");
292 rec
.proc
= (XtActionProc
)wxWidgetResizeProc
;
293 XtAppAddActions((XtAppContext
) wxTheApp
->m_appContext
, &rec
, 1);
295 GetMainColormap(dpy
);
302 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
304 if (!display
) /* Must be called first with non-NULL display */
305 return m_mainColormap
;
307 int defaultScreen
= DefaultScreen((Display
*) display
);
308 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
310 Colormap c
= DefaultColormapOfScreen(screen
);
313 m_mainColormap
= (WXColormap
) c
;
315 return (WXColormap
) c
;
318 static inline wxPerDisplayData
& GetOrCreatePerDisplayData
319 ( wxPerDisplayDataMap
& m
, WXDisplay
* display
)
321 wxPerDisplayDataMap::iterator it
= m
.find( display
);
322 if( it
!= m
.end() && it
->second
!= NULL
)
323 return *(it
->second
);
325 wxPerDisplayData
* nData
= new wxPerDisplayData();
331 wxXVisualInfo
* wxApp::GetVisualInfo( WXDisplay
* display
)
333 wxPerDisplayData
& data
= GetOrCreatePerDisplayData( *m_perDisplayData
,
335 if( data
.m_visualInfo
)
336 return data
.m_visualInfo
;
338 wxXVisualInfo
* vi
= new wxXVisualInfo
;
339 wxFillXVisualInfo( vi
, (Display
*)display
);
341 data
.m_visualInfo
= vi
;
346 static void wxTLWidgetDestroyCallback(Widget w
, XtPointer
WXUNUSED(clientData
),
347 XtPointer
WXUNUSED(ptr
))
351 wxTheApp
->SetTopLevelWidget( (WXDisplay
*)XtDisplay(w
),
353 wxTheApp
->SetTopLevelRealizedWidget( (WXDisplay
*)XtDisplay(w
),
358 WXWidget
wxCreateTopLevelWidget( WXDisplay
* display
)
360 Widget tlw
= XtAppCreateShell( (String
)NULL
,
361 wxTheApp
->GetClassName().c_str(),
362 applicationShellWidgetClass
,
366 XmNoverrideRedirect
, True
,
369 XtAddCallback( tlw
, XmNdestroyCallback
,
370 (XtCallbackProc
)wxTLWidgetDestroyCallback
,
373 return (WXWidget
)tlw
;
376 WXWidget
wxCreateTopLevelRealizedWidget( WXDisplay
* WXUNUSED(display
) )
378 Widget rTlw
= XtVaCreateWidget( "dummy_widget", topLevelShellWidgetClass
,
379 (Widget
)wxTheApp
->GetTopLevelWidget(),
381 XtSetMappedWhenManaged( rTlw
, False
);
382 XtRealizeWidget( rTlw
);
384 return (WXWidget
)rTlw
;
387 WXWidget
wxApp::GetTopLevelWidget()
389 WXDisplay
* display
= wxGetDisplay();
390 wxPerDisplayData
& data
= GetOrCreatePerDisplayData( *m_perDisplayData
,
392 if( data
.m_topLevelWidget
)
393 return (WXWidget
)data
.m_topLevelWidget
;
395 WXWidget tlw
= wxCreateTopLevelWidget( display
);
396 SetTopLevelWidget( display
, tlw
);
401 WXWidget
wxApp::GetTopLevelRealizedWidget()
403 WXDisplay
* display
= wxGetDisplay();
404 wxPerDisplayDataMap::iterator it
= m_perDisplayData
->find( display
);
406 if( it
!= m_perDisplayData
->end() && it
->second
->m_topLevelRealizedWidget
)
407 return (WXWidget
)it
->second
->m_topLevelRealizedWidget
;
409 WXWidget rTlw
= wxCreateTopLevelRealizedWidget( display
);
410 SetTopLevelRealizedWidget( display
, rTlw
);
415 void wxApp::SetTopLevelWidget(WXDisplay
* display
, WXWidget widget
)
417 GetOrCreatePerDisplayData( *m_perDisplayData
, display
)
418 .m_topLevelWidget
= (Widget
)widget
;
421 void wxApp::SetTopLevelRealizedWidget(WXDisplay
* display
, WXWidget widget
)
423 GetOrCreatePerDisplayData( *m_perDisplayData
, display
)
424 .m_topLevelRealizedWidget
= (Widget
)widget
;
427 // Yield to other processes
429 bool wxApp::Yield(bool onlyIfNeeded
)
431 static bool s_inYield
= false;
437 wxFAIL_MSG( wxT("wxYield called recursively" ) );
445 while (wxTheApp
&& wxTheApp
->Pending())
446 wxTheApp
->Dispatch();
453 // ----------------------------------------------------------------------------
454 // accessors for C modules
455 // ----------------------------------------------------------------------------
457 extern "C" XtAppContext
wxGetAppContext()
459 return (XtAppContext
)wxTheApp
->GetAppContext();