1 /////////////////////////////////////////////////////////////////////////////
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 XtDestroyWidget( it
->second
->m_topLevelWidget
);
129 delete m_perDisplayData
;
136 wxAppConsole::Exit();
139 // ============================================================================
141 // ============================================================================
148 m_mainLoop
= new wxEventLoop
;
149 m_mainColormap
= (WXColormap
) NULL
;
150 m_appContext
= (WXAppContext
) NULL
;
151 m_initialDisplay
= (WXDisplay
*) 0;
152 m_perDisplayData
= new wxPerDisplayDataMap
;
157 wxApp::SetInstance(NULL
);
160 int wxApp::MainLoop()
163 * Sit around forever waiting to process X-events. Property Change
164 * event are handled special, because they have to refer to
165 * the root window rather than to a widget. therefore we can't
166 * use an Xt-eventhandler.
169 XSelectInput(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()),
170 XDefaultRootWindow(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget())),
178 // This should be redefined in a derived class for
179 // handling property change events for XAtom IPC.
180 void wxApp::HandlePropertyChange(WXEvent
*event
)
182 // by default do nothing special
183 XtDispatchEvent((XEvent
*) event
); /* let Motif do the work */
186 static char *fallbackResources
[] = {
187 // better defaults for CDE under Irix
189 // TODO: do something similar for the other systems, the hardcoded defaults
192 wxMOTIF_STR("*sgiMode: True"),
193 wxMOTIF_STR("*useSchemes: all"),
195 wxMOTIF_STR("*menuBar.marginHeight: 0"),
196 wxMOTIF_STR("*menuBar.shadowThickness: 1"),
197 wxMOTIF_STR("*background: #c0c0c0"),
198 wxMOTIF_STR("*foreground: black"),
199 #endif // __SGI__/!__SGI__
203 // Create an application context
204 bool wxApp::OnInitGui()
206 if( !wxAppBase::OnInitGui() )
209 XtSetLanguageProc(NULL
, NULL
, NULL
);
210 XtToolkitInitialize() ;
211 wxTheApp
->m_appContext
= (WXAppContext
) XtCreateApplicationContext();
212 XtAppSetFallbackResources((XtAppContext
) wxTheApp
->m_appContext
, fallbackResources
);
214 Display
*dpy
= XtOpenDisplay((XtAppContext
) wxTheApp
->m_appContext
,(String
)NULL
,NULL
,
215 wxTheApp
->GetClassName().c_str(), NULL
, 0,
216 # if XtSpecificationRelease < 5
224 // if you don't log to stderr, nothing will be shown...
225 delete wxLog::SetActiveTarget(new wxLogStderr
);
226 wxString
className(wxTheApp
->GetClassName());
227 wxLogError(_("wxWidgets could not open display for '%s': exiting."),
231 m_initialDisplay
= (WXDisplay
*) dpy
;
234 // install the X error handler
235 gs_pfnXErrorHandler
= XSetErrorHandler(wxXErrorHandler
);
236 #endif // __WXDEBUG__
238 // Add general resize proc
240 rec
.string
= wxMOTIF_STR("resize");
241 rec
.proc
= (XtActionProc
)wxWidgetResizeProc
;
242 XtAppAddActions((XtAppContext
) wxTheApp
->m_appContext
, &rec
, 1);
244 GetMainColormap(dpy
);
251 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
253 if (!display
) /* Must be called first with non-NULL display */
254 return m_mainColormap
;
256 int defaultScreen
= DefaultScreen((Display
*) display
);
257 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
259 Colormap c
= DefaultColormapOfScreen(screen
);
262 m_mainColormap
= (WXColormap
) c
;
264 return (WXColormap
) c
;
267 static inline wxPerDisplayData
& GetOrCreatePerDisplayData
268 ( wxPerDisplayDataMap
& m
, WXDisplay
* display
)
270 wxPerDisplayDataMap::iterator it
= m
.find( display
);
271 if( it
!= m
.end() && it
->second
!= NULL
)
272 return *(it
->second
);
274 wxPerDisplayData
* nData
= new wxPerDisplayData();
280 wxXVisualInfo
* wxApp::GetVisualInfo( WXDisplay
* display
)
282 wxPerDisplayData
& data
= GetOrCreatePerDisplayData( *m_perDisplayData
,
284 if( data
.m_visualInfo
)
285 return data
.m_visualInfo
;
287 wxXVisualInfo
* vi
= new wxXVisualInfo
;
288 wxFillXVisualInfo( vi
, (Display
*)display
);
290 data
.m_visualInfo
= vi
;
295 static void wxTLWidgetDestroyCallback(Widget w
, XtPointer clientData
,
300 wxTheApp
->SetTopLevelWidget( (WXDisplay
*)XtDisplay(w
),
302 wxTheApp
->SetTopLevelRealizedWidget( (WXDisplay
*)XtDisplay(w
),
307 WXWidget
wxCreateTopLevelWidget( WXDisplay
* display
)
309 Widget tlw
= XtAppCreateShell( (String
)NULL
,
310 wxTheApp
->GetClassName().c_str(),
311 applicationShellWidgetClass
,
315 XmNoverrideRedirect
, True
,
318 XtAddCallback( tlw
, XmNdestroyCallback
,
319 (XtCallbackProc
)wxTLWidgetDestroyCallback
,
322 return (WXWidget
)tlw
;
325 WXWidget
wxCreateTopLevelRealizedWidget( WXDisplay
* display
)
327 Widget rTlw
= XtVaCreateWidget( "dummy_widget", topLevelShellWidgetClass
,
328 (Widget
)wxTheApp
->GetTopLevelWidget(),
330 XtSetMappedWhenManaged( rTlw
, False
);
331 XtRealizeWidget( rTlw
);
333 return (WXWidget
)rTlw
;
336 WXWidget
wxApp::GetTopLevelWidget()
338 WXDisplay
* display
= wxGetDisplay();
339 wxPerDisplayData
& data
= GetOrCreatePerDisplayData( *m_perDisplayData
,
341 if( data
.m_topLevelWidget
)
342 return (WXWidget
)data
.m_topLevelWidget
;
344 WXWidget tlw
= wxCreateTopLevelWidget( display
);
345 SetTopLevelWidget( display
, tlw
);
350 WXWidget
wxApp::GetTopLevelRealizedWidget()
352 WXDisplay
* display
= wxGetDisplay();
353 wxPerDisplayDataMap::iterator it
= m_perDisplayData
->find( display
);
355 if( it
!= m_perDisplayData
->end() && it
->second
->m_topLevelRealizedWidget
)
356 return (WXWidget
)it
->second
->m_topLevelRealizedWidget
;
358 WXWidget rTlw
= wxCreateTopLevelRealizedWidget( display
);
359 SetTopLevelRealizedWidget( display
, rTlw
);
364 void wxApp::SetTopLevelWidget(WXDisplay
* display
, WXWidget widget
)
366 GetOrCreatePerDisplayData( *m_perDisplayData
, display
)
367 .m_topLevelWidget
= (Widget
)widget
;
370 void wxApp::SetTopLevelRealizedWidget(WXDisplay
* display
, WXWidget widget
)
372 GetOrCreatePerDisplayData( *m_perDisplayData
, display
)
373 .m_topLevelRealizedWidget
= (Widget
)widget
;
376 // Yield to other processes
378 bool wxApp::Yield(bool onlyIfNeeded
)
380 static bool s_inYield
= false;
386 wxFAIL_MSG( wxT("wxYield called recursively" ) );
394 while (wxTheApp
&& wxTheApp
->Pending())
395 wxTheApp
->Dispatch();
402 // ----------------------------------------------------------------------------
403 // accessors for C modules
404 // ----------------------------------------------------------------------------
406 extern "C" XtAppContext
wxGetAppContext()
408 return (XtAppContext
)wxTheApp
->GetAppContext();