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
22 #include "wx/module.h"
23 #include "wx/memory.h"
26 #include "wx/evtloop.h"
30 #include "wx/thread.h"
34 #pragma message disable nosimpint
38 #include <X11/Xutil.h>
39 #include <X11/Xresource.h>
40 #include <X11/Xatom.h>
42 #pragma message enable nosimpint
45 #include "wx/motif/private.h"
49 struct wxPerDisplayData
54 m_topLevelWidget
= NULL
;
55 m_topLevelRealizedWidget
= NULL
;
58 wxXVisualInfo
* m_visualInfo
;
59 Widget m_topLevelWidget
, m_topLevelRealizedWidget
;
62 static void wxTLWidgetDestroyCallback(Widget w
, XtPointer clientData
,
64 static WXWidget
wxCreateTopLevelWidget( WXDisplay
* display
);
66 extern wxList wxPendingDelete
;
67 extern bool wxAddIdleCallback();
69 wxHashTable
*wxWidgetHashTable
= NULL
;
71 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
73 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
74 EVT_IDLE(wxAppBase::OnIdle
)
80 typedef int (*XErrorHandlerFunc
)(Display
*, XErrorEvent
*);
83 XErrorHandlerFunc gs_pfnXErrorHandler
= 0;
88 static int wxXErrorHandler(Display
*dpy
, XErrorEvent
*xevent
)
90 // just forward to the default handler for now
91 return gs_pfnXErrorHandler(dpy
, xevent
);
97 bool wxApp::Initialize(int& argcOrig
, wxChar
**argvOrig
)
99 if ( !wxAppBase::Initialize(argcOrig
, argvOrig
) )
102 wxWidgetHashTable
= new wxHashTable(wxKEY_INTEGER
);
105 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
111 void wxApp::CleanUp()
113 wxAppBase::CleanUp();
115 delete wxWidgetHashTable
;
116 wxWidgetHashTable
= NULL
;
120 for( wxPerDisplayDataMap::iterator it
= m_perDisplayData
->begin(),
121 end
= m_perDisplayData
->end();
124 delete it
->second
->m_visualInfo
;
125 // On Solaris 10 calling XtDestroyWidget on the top level widget
126 // dumps core if the locale is set to something other than "C"
128 XtDestroyWidget( it
->second
->m_topLevelWidget
);
133 delete m_perDisplayData
;
140 wxAppConsole::Exit();
143 // ============================================================================
145 // ============================================================================
152 m_mainLoop
= new wxEventLoop
;
153 m_mainColormap
= (WXColormap
) NULL
;
154 m_appContext
= (WXAppContext
) NULL
;
155 m_initialDisplay
= (WXDisplay
*) 0;
156 m_perDisplayData
= new wxPerDisplayDataMap
;
161 wxApp::SetInstance(NULL
);
164 int wxApp::MainLoop()
167 * Sit around forever waiting to process X-events. Property Change
168 * event are handled special, because they have to refer to
169 * the root window rather than to a widget. therefore we can't
170 * use an Xt-eventhandler.
173 XSelectInput(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()),
174 XDefaultRootWindow(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget())),
182 // This should be redefined in a derived class for
183 // handling property change events for XAtom IPC.
184 void wxApp::HandlePropertyChange(WXEvent
*event
)
186 // by default do nothing special
187 XtDispatchEvent((XEvent
*) event
); /* let Motif do the work */
190 static char *fallbackResources
[] = {
191 // better defaults for CDE under Irix
193 // TODO: do something similar for the other systems, the hardcoded defaults
196 wxMOTIF_STR("*sgiMode: True"),
197 wxMOTIF_STR("*useSchemes: all"),
199 wxMOTIF_STR("*menuBar.marginHeight: 0"),
200 wxMOTIF_STR("*menuBar.shadowThickness: 1"),
201 wxMOTIF_STR("*background: #c0c0c0"),
202 wxMOTIF_STR("*foreground: black"),
203 #endif // __SGI__/!__SGI__
207 // Create an application context
208 bool wxApp::OnInitGui()
210 if( !wxAppBase::OnInitGui() )
214 // under HP-UX creating XmFontSet fails when the system locale is C and
215 // we're using a remote DISPLAY, presumably because HP-UX uses its own
216 // names for C and ISO locales (roman8 and iso8859n respectively) and so
217 // its Motif libraries have troubles with non-HP X server
219 // whatever the reason, the fact is that without this hack any wxMotif
220 // program crashes on startup because it can't create any font (HP programs
221 // still work but they do spit out messages about failing to create font
222 // sets and failing back on "fixed" font too)
224 // notice that calling setlocale() here is not enough because X(m) init
225 // functions call setlocale() later so we really have to change environment
226 bool fixAll
= false; // tweak LC_ALL (or just LC_CTYPE)?
227 const char *loc
= getenv("LC_CTYPE");
230 loc
= getenv("LC_ALL");
236 (loc
[0] == 'C' && loc
[1] == '\0') ||
237 strcmp(loc
, "POSIX") == 0 )
239 // we're using C locale, "fix" it
240 wxLogDebug(_T("HP-UX fontset hack: forcing locale to en_US.iso88591"));
241 putenv(fixAll
? "LC_ALL=en_US.iso88591" : "LC_CTYPE=en_US.iso88591");
245 XtSetLanguageProc(NULL
, NULL
, NULL
);
246 XtToolkitInitialize() ;
247 wxTheApp
->m_appContext
= (WXAppContext
) XtCreateApplicationContext();
248 XtAppSetFallbackResources((XtAppContext
) wxTheApp
->m_appContext
, fallbackResources
);
250 Display
*dpy
= XtOpenDisplay((XtAppContext
) wxTheApp
->m_appContext
,(String
)NULL
,NULL
,
251 wxTheApp
->GetClassName().c_str(), NULL
, 0,
252 # if XtSpecificationRelease < 5
260 // if you don't log to stderr, nothing will be shown...
261 delete wxLog::SetActiveTarget(new wxLogStderr
);
262 wxString
className(wxTheApp
->GetClassName());
263 wxLogError(_("wxWidgets could not open display for '%s': exiting."),
267 m_initialDisplay
= (WXDisplay
*) dpy
;
270 // install the X error handler
271 gs_pfnXErrorHandler
= XSetErrorHandler(wxXErrorHandler
);
272 #endif // __WXDEBUG__
274 // Add general resize proc
276 rec
.string
= wxMOTIF_STR("resize");
277 rec
.proc
= (XtActionProc
)wxWidgetResizeProc
;
278 XtAppAddActions((XtAppContext
) wxTheApp
->m_appContext
, &rec
, 1);
280 GetMainColormap(dpy
);
287 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
289 if (!display
) /* Must be called first with non-NULL display */
290 return m_mainColormap
;
292 int defaultScreen
= DefaultScreen((Display
*) display
);
293 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
295 Colormap c
= DefaultColormapOfScreen(screen
);
298 m_mainColormap
= (WXColormap
) c
;
300 return (WXColormap
) c
;
303 static inline wxPerDisplayData
& GetOrCreatePerDisplayData
304 ( wxPerDisplayDataMap
& m
, WXDisplay
* display
)
306 wxPerDisplayDataMap::iterator it
= m
.find( display
);
307 if( it
!= m
.end() && it
->second
!= NULL
)
308 return *(it
->second
);
310 wxPerDisplayData
* nData
= new wxPerDisplayData();
316 wxXVisualInfo
* wxApp::GetVisualInfo( WXDisplay
* display
)
318 wxPerDisplayData
& data
= GetOrCreatePerDisplayData( *m_perDisplayData
,
320 if( data
.m_visualInfo
)
321 return data
.m_visualInfo
;
323 wxXVisualInfo
* vi
= new wxXVisualInfo
;
324 wxFillXVisualInfo( vi
, (Display
*)display
);
326 data
.m_visualInfo
= vi
;
331 static void wxTLWidgetDestroyCallback(Widget w
, XtPointer
WXUNUSED(clientData
),
332 XtPointer
WXUNUSED(ptr
))
336 wxTheApp
->SetTopLevelWidget( (WXDisplay
*)XtDisplay(w
),
338 wxTheApp
->SetTopLevelRealizedWidget( (WXDisplay
*)XtDisplay(w
),
343 WXWidget
wxCreateTopLevelWidget( WXDisplay
* display
)
345 Widget tlw
= XtAppCreateShell( (String
)NULL
,
346 wxTheApp
->GetClassName().c_str(),
347 applicationShellWidgetClass
,
351 XmNoverrideRedirect
, True
,
354 XtAddCallback( tlw
, XmNdestroyCallback
,
355 (XtCallbackProc
)wxTLWidgetDestroyCallback
,
358 return (WXWidget
)tlw
;
361 WXWidget
wxCreateTopLevelRealizedWidget( WXDisplay
* WXUNUSED(display
) )
363 Widget rTlw
= XtVaCreateWidget( "dummy_widget", topLevelShellWidgetClass
,
364 (Widget
)wxTheApp
->GetTopLevelWidget(),
366 XtSetMappedWhenManaged( rTlw
, False
);
367 XtRealizeWidget( rTlw
);
369 return (WXWidget
)rTlw
;
372 WXWidget
wxApp::GetTopLevelWidget()
374 WXDisplay
* display
= wxGetDisplay();
375 wxPerDisplayData
& data
= GetOrCreatePerDisplayData( *m_perDisplayData
,
377 if( data
.m_topLevelWidget
)
378 return (WXWidget
)data
.m_topLevelWidget
;
380 WXWidget tlw
= wxCreateTopLevelWidget( display
);
381 SetTopLevelWidget( display
, tlw
);
386 WXWidget
wxApp::GetTopLevelRealizedWidget()
388 WXDisplay
* display
= wxGetDisplay();
389 wxPerDisplayDataMap::iterator it
= m_perDisplayData
->find( display
);
391 if( it
!= m_perDisplayData
->end() && it
->second
->m_topLevelRealizedWidget
)
392 return (WXWidget
)it
->second
->m_topLevelRealizedWidget
;
394 WXWidget rTlw
= wxCreateTopLevelRealizedWidget( display
);
395 SetTopLevelRealizedWidget( display
, rTlw
);
400 void wxApp::SetTopLevelWidget(WXDisplay
* display
, WXWidget widget
)
402 GetOrCreatePerDisplayData( *m_perDisplayData
, display
)
403 .m_topLevelWidget
= (Widget
)widget
;
406 void wxApp::SetTopLevelRealizedWidget(WXDisplay
* display
, WXWidget widget
)
408 GetOrCreatePerDisplayData( *m_perDisplayData
, display
)
409 .m_topLevelRealizedWidget
= (Widget
)widget
;
412 // Yield to other processes
414 bool wxApp::Yield(bool onlyIfNeeded
)
416 static bool s_inYield
= false;
422 wxFAIL_MSG( wxT("wxYield called recursively" ) );
430 while (wxTheApp
&& wxTheApp
->Pending())
431 wxTheApp
->Dispatch();
438 // ----------------------------------------------------------------------------
439 // accessors for C modules
440 // ----------------------------------------------------------------------------
442 extern "C" XtAppContext
wxGetAppContext()
444 return (XtAppContext
)wxTheApp
->GetAppContext();