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
29 #include "wx/module.h"
30 #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 wxList wxPendingDelete
;
71 extern bool wxAddIdleCallback();
73 wxHashTable
*wxWidgetHashTable
= NULL
;
75 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
77 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
78 EVT_IDLE(wxAppBase::OnIdle
)
84 typedef int (*XErrorHandlerFunc
)(Display
*, XErrorEvent
*);
87 XErrorHandlerFunc gs_pfnXErrorHandler
= 0;
92 static int wxXErrorHandler(Display
*dpy
, XErrorEvent
*xevent
)
94 // just forward to the default handler for now
95 return gs_pfnXErrorHandler(dpy
, xevent
);
101 bool wxApp::Initialize(int& argcOrig
, wxChar
**argvOrig
)
103 if ( !wxAppBase::Initialize(argcOrig
, argvOrig
) )
106 wxWidgetHashTable
= new wxHashTable(wxKEY_INTEGER
);
109 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
115 void wxApp::CleanUp()
117 wxAppBase::CleanUp();
119 delete wxWidgetHashTable
;
120 wxWidgetHashTable
= NULL
;
124 for( wxPerDisplayDataMap::iterator it
= m_perDisplayData
->begin(),
125 end
= m_perDisplayData
->end();
128 delete it
->second
->m_visualInfo
;
129 // On Solaris 10 calling XtDestroyWidget on the top level widget
130 // dumps core if the locale is set to something other than "C"
132 XtDestroyWidget( it
->second
->m_topLevelWidget
);
137 delete m_perDisplayData
;
144 wxAppConsole::Exit();
147 // ============================================================================
149 // ============================================================================
156 m_mainLoop
= new wxEventLoop
;
157 m_mainColormap
= (WXColormap
) NULL
;
158 m_appContext
= (WXAppContext
) NULL
;
159 m_initialDisplay
= (WXDisplay
*) 0;
160 m_perDisplayData
= new wxPerDisplayDataMap
;
165 wxApp::SetInstance(NULL
);
168 int wxApp::MainLoop()
171 * Sit around forever waiting to process X-events. Property Change
172 * event are handled special, because they have to refer to
173 * the root window rather than to a widget. therefore we can't
174 * use an Xt-eventhandler.
177 XSelectInput(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()),
178 XDefaultRootWindow(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget())),
186 // This should be redefined in a derived class for
187 // handling property change events for XAtom IPC.
188 void wxApp::HandlePropertyChange(WXEvent
*event
)
190 // by default do nothing special
191 XtDispatchEvent((XEvent
*) event
); /* let Motif do the work */
194 static char *fallbackResources
[] = {
195 // better defaults for CDE under Irix
197 // TODO: do something similar for the other systems, the hardcoded defaults
200 wxMOTIF_STR("*sgiMode: True"),
201 wxMOTIF_STR("*useSchemes: all"),
203 wxMOTIF_STR("*menuBar.marginHeight: 0"),
204 wxMOTIF_STR("*menuBar.shadowThickness: 1"),
205 wxMOTIF_STR("*background: #c0c0c0"),
206 wxMOTIF_STR("*foreground: black"),
207 #endif // __SGI__/!__SGI__
211 // Create an application context
212 bool wxApp::OnInitGui()
214 if( !wxAppBase::OnInitGui() )
218 // under HP-UX creating XmFontSet fails when the system locale is C and
219 // we're using a remote DISPLAY, presumably because HP-UX uses its own
220 // names for C and ISO locales (roman8 and iso8859n respectively) and so
221 // its Motif libraries have troubles with non-HP X server
223 // whatever the reason, the fact is that without this hack any wxMotif
224 // program crashes on startup because it can't create any font (HP programs
225 // still work but they do spit out messages about failing to create font
226 // sets and failing back on "fixed" font too)
228 // notice that calling setlocale() here is not enough because X(m) init
229 // functions call setlocale() later so we really have to change environment
230 bool fixAll
= false; // tweak LC_ALL (or just LC_CTYPE)?
231 const char *loc
= getenv("LC_CTYPE");
234 loc
= getenv("LC_ALL");
240 (loc
[0] == 'C' && loc
[1] == '\0') ||
241 strcmp(loc
, "POSIX") == 0 )
243 // we're using C locale, "fix" it
244 wxLogDebug(_T("HP-UX fontset hack: forcing locale to en_US.iso88591"));
245 putenv(fixAll
? "LC_ALL=en_US.iso88591" : "LC_CTYPE=en_US.iso88591");
249 XtSetLanguageProc(NULL
, NULL
, NULL
);
250 XtToolkitInitialize() ;
251 wxTheApp
->m_appContext
= (WXAppContext
) XtCreateApplicationContext();
252 XtAppSetFallbackResources((XtAppContext
) wxTheApp
->m_appContext
, fallbackResources
);
254 // we shouldn't pass empty application/class name as it results in
255 // immediate crash inside XOpenIM() (if XIM is used) under IRIX
256 wxString appname
= wxTheApp
->GetAppName();
257 if ( appname
.empty() )
258 appname
= _T("wxapp");
259 wxString clsname
= wxTheApp
->GetClassName();
260 if ( clsname
.empty() )
263 Display
*dpy
= XtOpenDisplay((XtAppContext
) wxTheApp
->m_appContext
,
267 NULL
, 0, // no options
268 # if XtSpecificationRelease < 5
276 // if you don't log to stderr, nothing will be shown...
277 delete wxLog::SetActiveTarget(new wxLogStderr
);
278 wxString
className(wxTheApp
->GetClassName());
279 wxLogError(_("wxWidgets could not open display for '%s': exiting."),
283 m_initialDisplay
= (WXDisplay
*) dpy
;
286 // install the X error handler
287 gs_pfnXErrorHandler
= XSetErrorHandler(wxXErrorHandler
);
288 #endif // __WXDEBUG__
290 // Add general resize proc
292 rec
.string
= wxMOTIF_STR("resize");
293 rec
.proc
= (XtActionProc
)wxWidgetResizeProc
;
294 XtAppAddActions((XtAppContext
) wxTheApp
->m_appContext
, &rec
, 1);
296 GetMainColormap(dpy
);
303 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
305 if (!display
) /* Must be called first with non-NULL display */
306 return m_mainColormap
;
308 int defaultScreen
= DefaultScreen((Display
*) display
);
309 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
311 Colormap c
= DefaultColormapOfScreen(screen
);
314 m_mainColormap
= (WXColormap
) c
;
316 return (WXColormap
) c
;
319 static inline wxPerDisplayData
& GetOrCreatePerDisplayData
320 ( wxPerDisplayDataMap
& m
, WXDisplay
* display
)
322 wxPerDisplayDataMap::iterator it
= m
.find( display
);
323 if( it
!= m
.end() && it
->second
!= NULL
)
324 return *(it
->second
);
326 wxPerDisplayData
* nData
= new wxPerDisplayData();
332 wxXVisualInfo
* wxApp::GetVisualInfo( WXDisplay
* display
)
334 wxPerDisplayData
& data
= GetOrCreatePerDisplayData( *m_perDisplayData
,
336 if( data
.m_visualInfo
)
337 return data
.m_visualInfo
;
339 wxXVisualInfo
* vi
= new wxXVisualInfo
;
340 wxFillXVisualInfo( vi
, (Display
*)display
);
342 data
.m_visualInfo
= vi
;
347 static void wxTLWidgetDestroyCallback(Widget w
, XtPointer
WXUNUSED(clientData
),
348 XtPointer
WXUNUSED(ptr
))
352 wxTheApp
->SetTopLevelWidget( (WXDisplay
*)XtDisplay(w
),
354 wxTheApp
->SetTopLevelRealizedWidget( (WXDisplay
*)XtDisplay(w
),
359 WXWidget
wxCreateTopLevelWidget( WXDisplay
* display
)
361 Widget tlw
= XtAppCreateShell( (String
)NULL
,
362 wxTheApp
->GetClassName().c_str(),
363 applicationShellWidgetClass
,
367 XmNoverrideRedirect
, True
,
370 XtAddCallback( tlw
, XmNdestroyCallback
,
371 (XtCallbackProc
)wxTLWidgetDestroyCallback
,
374 return (WXWidget
)tlw
;
377 WXWidget
wxCreateTopLevelRealizedWidget( WXDisplay
* WXUNUSED(display
) )
379 Widget rTlw
= XtVaCreateWidget( "dummy_widget", topLevelShellWidgetClass
,
380 (Widget
)wxTheApp
->GetTopLevelWidget(),
382 XtSetMappedWhenManaged( rTlw
, False
);
383 XtRealizeWidget( rTlw
);
385 return (WXWidget
)rTlw
;
388 WXWidget
wxApp::GetTopLevelWidget()
390 WXDisplay
* display
= wxGetDisplay();
391 wxPerDisplayData
& data
= GetOrCreatePerDisplayData( *m_perDisplayData
,
393 if( data
.m_topLevelWidget
)
394 return (WXWidget
)data
.m_topLevelWidget
;
396 WXWidget tlw
= wxCreateTopLevelWidget( display
);
397 SetTopLevelWidget( display
, tlw
);
402 WXWidget
wxApp::GetTopLevelRealizedWidget()
404 WXDisplay
* display
= wxGetDisplay();
405 wxPerDisplayDataMap::iterator it
= m_perDisplayData
->find( display
);
407 if( it
!= m_perDisplayData
->end() && it
->second
->m_topLevelRealizedWidget
)
408 return (WXWidget
)it
->second
->m_topLevelRealizedWidget
;
410 WXWidget rTlw
= wxCreateTopLevelRealizedWidget( display
);
411 SetTopLevelRealizedWidget( display
, rTlw
);
416 void wxApp::SetTopLevelWidget(WXDisplay
* display
, WXWidget widget
)
418 GetOrCreatePerDisplayData( *m_perDisplayData
, display
)
419 .m_topLevelWidget
= (Widget
)widget
;
422 void wxApp::SetTopLevelRealizedWidget(WXDisplay
* display
, WXWidget widget
)
424 GetOrCreatePerDisplayData( *m_perDisplayData
, display
)
425 .m_topLevelRealizedWidget
= (Widget
)widget
;
428 // Yield to other processes
430 bool wxApp::Yield(bool onlyIfNeeded
)
432 static bool s_inYield
= false;
438 wxFAIL_MSG( wxT("wxYield called recursively" ) );
446 while (wxTheApp
&& wxTheApp
->Pending())
447 wxTheApp
->Dispatch();
454 // ----------------------------------------------------------------------------
455 // accessors for C modules
456 // ----------------------------------------------------------------------------
458 extern "C" XtAppContext
wxGetAppContext()
460 return (XtAppContext
)wxTheApp
->GetAppContext();