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
)
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
);
141 wxAppConsole::Exit();
144 // ============================================================================
146 // ============================================================================
153 m_mainLoop
= new wxEventLoop
;
154 m_mainColormap
= (WXColormap
) NULL
;
155 m_appContext
= (WXAppContext
) NULL
;
156 m_initialDisplay
= (WXDisplay
*) 0;
157 m_perDisplayData
= new wxPerDisplayDataMap
;
162 delete m_perDisplayData
;
165 int wxApp::MainLoop()
168 * Sit around forever waiting to process X-events. Property Change
169 * event are handled special, because they have to refer to
170 * the root window rather than to a widget. therefore we can't
171 * use an Xt-eventhandler.
174 XSelectInput(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()),
175 XDefaultRootWindow(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget())),
183 // This should be redefined in a derived class for
184 // handling property change events for XAtom IPC.
185 void wxApp::HandlePropertyChange(WXEvent
*event
)
187 // by default do nothing special
188 XtDispatchEvent((XEvent
*) event
); /* let Motif do the work */
191 static char *fallbackResources
[] = {
192 // better defaults for CDE under Irix
194 // TODO: do something similar for the other systems, the hardcoded defaults
197 wxMOTIF_STR("*sgiMode: True"),
198 wxMOTIF_STR("*useSchemes: all"),
200 wxMOTIF_STR("*menuBar.marginHeight: 0"),
201 wxMOTIF_STR("*menuBar.shadowThickness: 1"),
202 wxMOTIF_STR("*background: #c0c0c0"),
203 wxMOTIF_STR("*foreground: black"),
204 #endif // __SGI__/!__SGI__
208 // Create an application context
209 bool wxApp::OnInitGui()
211 if( !wxAppBase::OnInitGui() )
215 // under HP-UX creating XmFontSet fails when the system locale is C and
216 // we're using a remote DISPLAY, presumably because HP-UX uses its own
217 // names for C and ISO locales (roman8 and iso8859n respectively) and so
218 // its Motif libraries have troubles with non-HP X server
220 // whatever the reason, the fact is that without this hack any wxMotif
221 // program crashes on startup because it can't create any font (HP programs
222 // still work but they do spit out messages about failing to create font
223 // sets and failing back on "fixed" font too)
225 // notice that calling setlocale() here is not enough because X(m) init
226 // functions call setlocale() later so we really have to change environment
227 bool fixAll
= false; // tweak LC_ALL (or just LC_CTYPE)?
228 const char *loc
= getenv("LC_CTYPE");
231 loc
= getenv("LC_ALL");
237 (loc
[0] == 'C' && loc
[1] == '\0') ||
238 strcmp(loc
, "POSIX") == 0 )
240 // we're using C locale, "fix" it
241 wxLogDebug(_T("HP-UX fontset hack: forcing locale to en_US.iso88591"));
242 putenv(fixAll
? "LC_ALL=en_US.iso88591" : "LC_CTYPE=en_US.iso88591");
246 XtSetLanguageProc(NULL
, NULL
, NULL
);
247 XtToolkitInitialize() ;
248 wxTheApp
->m_appContext
= (WXAppContext
) XtCreateApplicationContext();
249 XtAppSetFallbackResources((XtAppContext
) wxTheApp
->m_appContext
, fallbackResources
);
251 // we shouldn't pass empty application/class name as it results in
252 // immediate crash inside XOpenIM() (if XIM is used) under IRIX
253 wxString appname
= wxTheApp
->GetAppName();
254 if ( appname
.empty() )
255 appname
= _T("wxapp");
256 wxString clsname
= wxTheApp
->GetClassName();
257 if ( clsname
.empty() )
260 Display
*dpy
= XtOpenDisplay((XtAppContext
) wxTheApp
->m_appContext
,
264 NULL
, 0, // no options
265 # if XtSpecificationRelease < 5
273 // if you don't log to stderr, nothing will be shown...
274 delete wxLog::SetActiveTarget(new wxLogStderr
);
275 wxString
className(wxTheApp
->GetClassName());
276 wxLogError(_("wxWidgets could not open display for '%s': exiting."),
280 m_initialDisplay
= (WXDisplay
*) dpy
;
283 // install the X error handler
284 gs_pfnXErrorHandler
= XSetErrorHandler(wxXErrorHandler
);
285 #endif // __WXDEBUG__
287 // Add general resize proc
289 rec
.string
= wxMOTIF_STR("resize");
290 rec
.proc
= (XtActionProc
)wxWidgetResizeProc
;
291 XtAppAddActions((XtAppContext
) wxTheApp
->m_appContext
, &rec
, 1);
293 GetMainColormap(dpy
);
300 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
302 if (!display
) /* Must be called first with non-NULL display */
303 return m_mainColormap
;
305 int defaultScreen
= DefaultScreen((Display
*) display
);
306 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
308 Colormap c
= DefaultColormapOfScreen(screen
);
311 m_mainColormap
= (WXColormap
) c
;
313 return (WXColormap
) c
;
316 static inline wxPerDisplayData
& GetOrCreatePerDisplayData
317 ( wxPerDisplayDataMap
& m
, WXDisplay
* display
)
319 wxPerDisplayDataMap::iterator it
= m
.find( display
);
320 if( it
!= m
.end() && it
->second
!= NULL
)
321 return *(it
->second
);
323 wxPerDisplayData
* nData
= new wxPerDisplayData();
329 wxXVisualInfo
* wxApp::GetVisualInfo( WXDisplay
* display
)
331 wxPerDisplayData
& data
= GetOrCreatePerDisplayData( *m_perDisplayData
,
333 if( data
.m_visualInfo
)
334 return data
.m_visualInfo
;
336 wxXVisualInfo
* vi
= new wxXVisualInfo
;
337 wxFillXVisualInfo( vi
, (Display
*)display
);
339 data
.m_visualInfo
= vi
;
344 static void wxTLWidgetDestroyCallback(Widget w
, XtPointer
WXUNUSED(clientData
),
345 XtPointer
WXUNUSED(ptr
))
349 wxTheApp
->SetTopLevelWidget( (WXDisplay
*)XtDisplay(w
),
351 wxTheApp
->SetTopLevelRealizedWidget( (WXDisplay
*)XtDisplay(w
),
356 WXWidget
wxCreateTopLevelWidget( WXDisplay
* display
)
358 Widget tlw
= XtAppCreateShell( (String
)NULL
,
359 wxTheApp
->GetClassName().c_str(),
360 applicationShellWidgetClass
,
364 XmNoverrideRedirect
, True
,
367 XtAddCallback( tlw
, XmNdestroyCallback
,
368 (XtCallbackProc
)wxTLWidgetDestroyCallback
,
371 return (WXWidget
)tlw
;
374 WXWidget
wxCreateTopLevelRealizedWidget( WXDisplay
* WXUNUSED(display
) )
376 Widget rTlw
= XtVaCreateWidget( "dummy_widget", topLevelShellWidgetClass
,
377 (Widget
)wxTheApp
->GetTopLevelWidget(),
379 XtSetMappedWhenManaged( rTlw
, False
);
380 XtRealizeWidget( rTlw
);
382 return (WXWidget
)rTlw
;
385 WXWidget
wxApp::GetTopLevelWidget()
387 WXDisplay
* display
= wxGetDisplay();
388 wxPerDisplayData
& data
= GetOrCreatePerDisplayData( *m_perDisplayData
,
390 if( data
.m_topLevelWidget
)
391 return (WXWidget
)data
.m_topLevelWidget
;
393 WXWidget tlw
= wxCreateTopLevelWidget( display
);
394 SetTopLevelWidget( display
, tlw
);
399 WXWidget
wxApp::GetTopLevelRealizedWidget()
401 WXDisplay
* display
= wxGetDisplay();
402 wxPerDisplayDataMap::iterator it
= m_perDisplayData
->find( display
);
404 if( it
!= m_perDisplayData
->end() && it
->second
->m_topLevelRealizedWidget
)
405 return (WXWidget
)it
->second
->m_topLevelRealizedWidget
;
407 WXWidget rTlw
= wxCreateTopLevelRealizedWidget( display
);
408 SetTopLevelRealizedWidget( display
, rTlw
);
413 void wxApp::SetTopLevelWidget(WXDisplay
* display
, WXWidget widget
)
415 GetOrCreatePerDisplayData( *m_perDisplayData
, display
)
416 .m_topLevelWidget
= (Widget
)widget
;
419 void wxApp::SetTopLevelRealizedWidget(WXDisplay
* display
, WXWidget widget
)
421 GetOrCreatePerDisplayData( *m_perDisplayData
, display
)
422 .m_topLevelRealizedWidget
= (Widget
)widget
;
425 // Yield to other processes
427 bool wxApp::Yield(bool onlyIfNeeded
)
429 static bool s_inYield
= false;
435 wxFAIL_MSG( wxT("wxYield called recursively" ) );
443 while (wxTheApp
&& wxTheApp
->Pending())
444 wxTheApp
->Dispatch();
451 // ----------------------------------------------------------------------------
452 // accessors for C modules
453 // ----------------------------------------------------------------------------
455 extern "C" XtAppContext
wxGetAppContext()
457 return (XtAppContext
)wxTheApp
->GetAppContext();