1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "app.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
20 #define XtParent XTPARENT
21 #define XtDisplay XTDISPLAY
26 #include "wx/module.h"
27 #include "wx/memory.h"
30 #include "wx/evtloop.h"
34 #include "wx/thread.h"
38 #pragma message disable nosimpint
43 #include <X11/Xutil.h>
44 #include <X11/Xresource.h>
45 #include <X11/Xatom.h>
47 #pragma message enable nosimpint
50 #include "wx/motif/private.h"
54 struct wxPerDisplayData
59 m_topLevelWidget
= NULL
;
60 m_topLevelRealizedWidget
= NULL
;
63 wxXVisualInfo
* m_visualInfo
;
64 Widget m_topLevelWidget
, m_topLevelRealizedWidget
;
67 static void wxTLWidgetDestroyCallback(Widget w
, XtPointer clientData
,
69 static WXWidget
wxCreateTopLevelWidget( WXDisplay
* display
);
71 extern wxList wxPendingDelete
;
72 extern bool wxAddIdleCallback();
74 wxHashTable
*wxWidgetHashTable
= NULL
;
76 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
78 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
79 EVT_IDLE(wxAppBase::OnIdle
)
83 typedef int (*XErrorHandlerFunc
)(Display
*, XErrorEvent
*);
85 XErrorHandlerFunc gs_pfnXErrorHandler
= 0;
87 static int wxXErrorHandler(Display
*dpy
, XErrorEvent
*xevent
)
89 // just forward to the default handler for now
90 return gs_pfnXErrorHandler(dpy
, xevent
);
94 bool wxApp::Initialize(int& argc
, wxChar
**argv
)
96 if ( !wxAppBase::Initialize(argc
, argv
) )
99 wxWidgetHashTable
= new wxHashTable(wxKEY_INTEGER
);
104 void wxApp::CleanUp()
106 delete wxWidgetHashTable
;
107 wxWidgetHashTable
= NULL
;
109 wxAppBase::CleanUp();
116 wxAppConsole::Exit();
119 // ============================================================================
121 // ============================================================================
128 m_mainLoop
= new wxEventLoop
;
129 m_mainColormap
= (WXColormap
) NULL
;
130 m_appContext
= (WXAppContext
) NULL
;
131 m_initialDisplay
= (WXDisplay
*) 0;
132 m_perDisplayData
= new wxPerDisplayDataMap
;
139 for( wxPerDisplayDataMap::iterator it
= m_perDisplayData
->begin(),
140 end
= m_perDisplayData
->end();
143 delete it
->second
->m_visualInfo
;
144 XtDestroyWidget( it
->second
->m_topLevelWidget
);
148 delete m_perDisplayData
;
150 wxApp::SetInstance(NULL
);
153 int wxApp::MainLoop()
156 * Sit around forever waiting to process X-events. Property Change
157 * event are handled special, because they have to refer to
158 * the root window rather than to a widget. therefore we can't
159 * use an Xt-eventhandler.
162 XSelectInput(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()),
163 XDefaultRootWindow(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget())),
171 // This should be redefined in a derived class for
172 // handling property change events for XAtom IPC.
173 void wxApp::HandlePropertyChange(WXEvent
*event
)
175 // by default do nothing special
176 XtDispatchEvent((XEvent
*) event
); /* let Motif do the work */
179 static char *fallbackResources
[] = {
180 "*menuBar.marginHeight: 0",
181 "*menuBar.shadowThickness: 1",
182 "*background: #c0c0c0",
183 "*foreground: black",
187 // Create an application context
188 bool wxApp::OnInitGui()
190 if( !wxAppBase::OnInitGui() )
193 XtToolkitInitialize() ;
194 wxTheApp
->m_appContext
= (WXAppContext
) XtCreateApplicationContext();
195 XtAppSetFallbackResources((XtAppContext
) wxTheApp
->m_appContext
, fallbackResources
);
197 Display
*dpy
= XtOpenDisplay((XtAppContext
) wxTheApp
->m_appContext
,(String
)NULL
,NULL
,
198 wxTheApp
->GetClassName().c_str(), NULL
, 0,
199 # if XtSpecificationRelease < 5
207 // if you don't log to stderr, nothing will be shown...
208 delete wxLog::SetActiveTarget(new wxLogStderr
);
209 wxString
className(wxTheApp
->GetClassName());
210 wxLogError(_("wxWidgets could not open display for '%s': exiting."),
214 m_initialDisplay
= (WXDisplay
*) dpy
;
217 // install the X error handler
218 gs_pfnXErrorHandler
= XSetErrorHandler(wxXErrorHandler
);
219 #endif // __WXDEBUG__
221 // Add general resize proc
223 rec
.string
= "resize";
224 rec
.proc
= (XtActionProc
)wxWidgetResizeProc
;
225 XtAppAddActions((XtAppContext
) wxTheApp
->m_appContext
, &rec
, 1);
227 GetMainColormap(dpy
);
234 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
236 if (!display
) /* Must be called first with non-NULL display */
237 return m_mainColormap
;
239 int defaultScreen
= DefaultScreen((Display
*) display
);
240 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
242 Colormap c
= DefaultColormapOfScreen(screen
);
245 m_mainColormap
= (WXColormap
) c
;
247 return (WXColormap
) c
;
250 static inline wxPerDisplayData
& GetOrCreatePerDisplayData
251 ( wxPerDisplayDataMap
& m
, WXDisplay
* display
)
253 wxPerDisplayDataMap::iterator it
= m
.find( display
);
254 if( it
!= m
.end() && it
->second
!= NULL
)
255 return *(it
->second
);
257 wxPerDisplayData
* nData
= new wxPerDisplayData();
263 wxXVisualInfo
* wxApp::GetVisualInfo( WXDisplay
* display
)
265 wxPerDisplayData
& data
= GetOrCreatePerDisplayData( *m_perDisplayData
,
267 if( data
.m_visualInfo
)
268 return data
.m_visualInfo
;
270 wxXVisualInfo
* vi
= new wxXVisualInfo
;
271 wxFillXVisualInfo( vi
, (Display
*)display
);
273 data
.m_visualInfo
= vi
;
278 static void wxTLWidgetDestroyCallback(Widget w
, XtPointer clientData
,
283 wxTheApp
->SetTopLevelWidget( (WXDisplay
*)XtDisplay(w
),
285 wxTheApp
->SetTopLevelRealizedWidget( (WXDisplay
*)XtDisplay(w
),
290 WXWidget
wxCreateTopLevelWidget( WXDisplay
* display
)
292 Widget tlw
= XtAppCreateShell( (String
)NULL
,
293 wxTheApp
->GetClassName().c_str(),
294 applicationShellWidgetClass
,
298 XmNoverrideRedirect
, True
,
301 XtAddCallback( tlw
, XmNdestroyCallback
,
302 (XtCallbackProc
)wxTLWidgetDestroyCallback
,
305 return (WXWidget
)tlw
;
308 WXWidget
wxCreateTopLevelRealizedWidget( WXDisplay
* display
)
310 Widget rTlw
= XtVaCreateWidget( "dummy_widget", xmLabelWidgetClass
,
311 (Widget
)wxTheApp
->GetTopLevelWidget(),
313 XtSetMappedWhenManaged( rTlw
, False
);
314 XtRealizeWidget( rTlw
);
316 return (WXWidget
)rTlw
;
319 WXWidget
wxApp::GetTopLevelWidget()
321 WXDisplay
* display
= wxGetDisplay();
322 wxPerDisplayData
& data
= GetOrCreatePerDisplayData( *m_perDisplayData
,
324 if( data
.m_topLevelWidget
)
325 return (WXWidget
)data
.m_topLevelWidget
;
327 WXWidget tlw
= wxCreateTopLevelWidget( display
);
328 SetTopLevelWidget( display
, tlw
);
333 WXWidget
wxApp::GetTopLevelRealizedWidget()
335 WXDisplay
* display
= wxGetDisplay();
336 wxPerDisplayDataMap::iterator it
= m_perDisplayData
->find( display
);
338 if( it
!= m_perDisplayData
->end() && it
->second
->m_topLevelRealizedWidget
)
339 return (WXWidget
)it
->second
->m_topLevelRealizedWidget
;
341 WXWidget rTlw
= wxCreateTopLevelRealizedWidget( display
);
342 SetTopLevelRealizedWidget( display
, rTlw
);
347 void wxApp::SetTopLevelWidget(WXDisplay
* display
, WXWidget widget
)
349 GetOrCreatePerDisplayData( *m_perDisplayData
, display
)
350 .m_topLevelWidget
= (Widget
)widget
;
353 void wxApp::SetTopLevelRealizedWidget(WXDisplay
* display
, WXWidget widget
)
355 GetOrCreatePerDisplayData( *m_perDisplayData
, display
)
356 .m_topLevelRealizedWidget
= (Widget
)widget
;
359 // Yield to other processes
361 bool wxApp::Yield(bool onlyIfNeeded
)
363 static bool s_inYield
= false;
369 wxFAIL_MSG( wxT("wxYield called recursively" ) );
377 while (wxTheApp
&& wxTheApp
->Pending())
378 wxTheApp
->Dispatch();
385 // ----------------------------------------------------------------------------
386 // accessors for C modules
387 // ----------------------------------------------------------------------------
389 extern "C" XtAppContext
wxGetAppContext()
391 return (XtAppContext
)wxTheApp
->GetAppContext();