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() )
213 XtSetLanguageProc(NULL
, NULL
, NULL
);
214 XtToolkitInitialize() ;
215 wxTheApp
->m_appContext
= (WXAppContext
) XtCreateApplicationContext();
216 XtAppSetFallbackResources((XtAppContext
) wxTheApp
->m_appContext
, fallbackResources
);
218 Display
*dpy
= XtOpenDisplay((XtAppContext
) wxTheApp
->m_appContext
,(String
)NULL
,NULL
,
219 wxTheApp
->GetClassName().c_str(), NULL
, 0,
220 # if XtSpecificationRelease < 5
228 // if you don't log to stderr, nothing will be shown...
229 delete wxLog::SetActiveTarget(new wxLogStderr
);
230 wxString
className(wxTheApp
->GetClassName());
231 wxLogError(_("wxWidgets could not open display for '%s': exiting."),
235 m_initialDisplay
= (WXDisplay
*) dpy
;
238 // install the X error handler
239 gs_pfnXErrorHandler
= XSetErrorHandler(wxXErrorHandler
);
240 #endif // __WXDEBUG__
242 // Add general resize proc
244 rec
.string
= wxMOTIF_STR("resize");
245 rec
.proc
= (XtActionProc
)wxWidgetResizeProc
;
246 XtAppAddActions((XtAppContext
) wxTheApp
->m_appContext
, &rec
, 1);
248 GetMainColormap(dpy
);
255 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
257 if (!display
) /* Must be called first with non-NULL display */
258 return m_mainColormap
;
260 int defaultScreen
= DefaultScreen((Display
*) display
);
261 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
263 Colormap c
= DefaultColormapOfScreen(screen
);
266 m_mainColormap
= (WXColormap
) c
;
268 return (WXColormap
) c
;
271 static inline wxPerDisplayData
& GetOrCreatePerDisplayData
272 ( wxPerDisplayDataMap
& m
, WXDisplay
* display
)
274 wxPerDisplayDataMap::iterator it
= m
.find( display
);
275 if( it
!= m
.end() && it
->second
!= NULL
)
276 return *(it
->second
);
278 wxPerDisplayData
* nData
= new wxPerDisplayData();
284 wxXVisualInfo
* wxApp::GetVisualInfo( WXDisplay
* display
)
286 wxPerDisplayData
& data
= GetOrCreatePerDisplayData( *m_perDisplayData
,
288 if( data
.m_visualInfo
)
289 return data
.m_visualInfo
;
291 wxXVisualInfo
* vi
= new wxXVisualInfo
;
292 wxFillXVisualInfo( vi
, (Display
*)display
);
294 data
.m_visualInfo
= vi
;
299 static void wxTLWidgetDestroyCallback(Widget w
, XtPointer
WXUNUSED(clientData
),
300 XtPointer
WXUNUSED(ptr
))
304 wxTheApp
->SetTopLevelWidget( (WXDisplay
*)XtDisplay(w
),
306 wxTheApp
->SetTopLevelRealizedWidget( (WXDisplay
*)XtDisplay(w
),
311 WXWidget
wxCreateTopLevelWidget( WXDisplay
* display
)
313 Widget tlw
= XtAppCreateShell( (String
)NULL
,
314 wxTheApp
->GetClassName().c_str(),
315 applicationShellWidgetClass
,
319 XmNoverrideRedirect
, True
,
322 XtAddCallback( tlw
, XmNdestroyCallback
,
323 (XtCallbackProc
)wxTLWidgetDestroyCallback
,
326 return (WXWidget
)tlw
;
329 WXWidget
wxCreateTopLevelRealizedWidget( WXDisplay
* WXUNUSED(display
) )
331 Widget rTlw
= XtVaCreateWidget( "dummy_widget", topLevelShellWidgetClass
,
332 (Widget
)wxTheApp
->GetTopLevelWidget(),
334 XtSetMappedWhenManaged( rTlw
, False
);
335 XtRealizeWidget( rTlw
);
337 return (WXWidget
)rTlw
;
340 WXWidget
wxApp::GetTopLevelWidget()
342 WXDisplay
* display
= wxGetDisplay();
343 wxPerDisplayData
& data
= GetOrCreatePerDisplayData( *m_perDisplayData
,
345 if( data
.m_topLevelWidget
)
346 return (WXWidget
)data
.m_topLevelWidget
;
348 WXWidget tlw
= wxCreateTopLevelWidget( display
);
349 SetTopLevelWidget( display
, tlw
);
354 WXWidget
wxApp::GetTopLevelRealizedWidget()
356 WXDisplay
* display
= wxGetDisplay();
357 wxPerDisplayDataMap::iterator it
= m_perDisplayData
->find( display
);
359 if( it
!= m_perDisplayData
->end() && it
->second
->m_topLevelRealizedWidget
)
360 return (WXWidget
)it
->second
->m_topLevelRealizedWidget
;
362 WXWidget rTlw
= wxCreateTopLevelRealizedWidget( display
);
363 SetTopLevelRealizedWidget( display
, rTlw
);
368 void wxApp::SetTopLevelWidget(WXDisplay
* display
, WXWidget widget
)
370 GetOrCreatePerDisplayData( *m_perDisplayData
, display
)
371 .m_topLevelWidget
= (Widget
)widget
;
374 void wxApp::SetTopLevelRealizedWidget(WXDisplay
* display
, WXWidget widget
)
376 GetOrCreatePerDisplayData( *m_perDisplayData
, display
)
377 .m_topLevelRealizedWidget
= (Widget
)widget
;
380 // Yield to other processes
382 bool wxApp::Yield(bool onlyIfNeeded
)
384 static bool s_inYield
= false;
390 wxFAIL_MSG( wxT("wxYield called recursively" ) );
398 while (wxTheApp
&& wxTheApp
->Pending())
399 wxTheApp
->Dispatch();
406 // ----------------------------------------------------------------------------
407 // accessors for C modules
408 // ----------------------------------------------------------------------------
410 extern "C" XtAppContext
wxGetAppContext()
412 return (XtAppContext
)wxTheApp
->GetAppContext();