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
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
)
82 typedef int (*XErrorHandlerFunc
)(Display
*, XErrorEvent
*);
84 XErrorHandlerFunc gs_pfnXErrorHandler
= 0;
86 static int wxXErrorHandler(Display
*dpy
, XErrorEvent
*xevent
)
88 // just forward to the default handler for now
89 return gs_pfnXErrorHandler(dpy
, xevent
);
93 bool wxApp::Initialize(int& argc
, wxChar
**argv
)
95 if ( !wxAppBase::Initialize(argc
, argv
) )
98 wxWidgetHashTable
= new wxHashTable(wxKEY_INTEGER
);
103 void wxApp::CleanUp()
105 delete wxWidgetHashTable
;
106 wxWidgetHashTable
= NULL
;
108 wxAppBase::CleanUp();
115 wxAppConsole::Exit();
118 // ============================================================================
120 // ============================================================================
127 m_mainLoop
= new wxEventLoop
;
128 m_mainColormap
= (WXColormap
) NULL
;
129 m_appContext
= (WXAppContext
) NULL
;
130 m_initialDisplay
= (WXDisplay
*) 0;
131 m_perDisplayData
= new wxPerDisplayDataMap
;
138 for( wxPerDisplayDataMap::iterator it
= m_perDisplayData
->begin(),
139 end
= m_perDisplayData
->end();
142 delete it
->second
->m_visualInfo
;
143 XtDestroyWidget( it
->second
->m_topLevelWidget
);
147 delete m_perDisplayData
;
149 wxApp::SetInstance(NULL
);
152 int wxApp::MainLoop()
155 * Sit around forever waiting to process X-events. Property Change
156 * event are handled special, because they have to refer to
157 * the root window rather than to a widget. therefore we can't
158 * use an Xt-eventhandler.
161 XSelectInput(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()),
162 XDefaultRootWindow(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget())),
170 // This should be redefined in a derived class for
171 // handling property change events for XAtom IPC.
172 void wxApp::HandlePropertyChange(WXEvent
*event
)
174 // by default do nothing special
175 XtDispatchEvent((XEvent
*) event
); /* let Motif do the work */
178 static char *fallbackResources
[] = {
179 "*menuBar.marginHeight: 0",
180 "*menuBar.shadowThickness: 1",
181 "*background: #c0c0c0",
182 "*foreground: black",
186 // Create an application context
187 bool wxApp::OnInitGui()
189 if( !wxAppBase::OnInitGui() )
192 XtToolkitInitialize() ;
193 wxTheApp
->m_appContext
= (WXAppContext
) XtCreateApplicationContext();
194 XtAppSetFallbackResources((XtAppContext
) wxTheApp
->m_appContext
, fallbackResources
);
196 Display
*dpy
= XtOpenDisplay((XtAppContext
) wxTheApp
->m_appContext
,(String
)NULL
,NULL
,
197 wxTheApp
->GetClassName().c_str(), NULL
, 0,
198 # if XtSpecificationRelease < 5
206 // if you don't log to stderr, nothing will be shown...
207 delete wxLog::SetActiveTarget(new wxLogStderr
);
208 wxString
className(wxTheApp
->GetClassName());
209 wxLogError(_("wxWidgets could not open display for '%s': exiting."),
213 m_initialDisplay
= (WXDisplay
*) dpy
;
216 // install the X error handler
217 gs_pfnXErrorHandler
= XSetErrorHandler(wxXErrorHandler
);
218 #endif // __WXDEBUG__
220 // Add general resize proc
222 rec
.string
= "resize";
223 rec
.proc
= (XtActionProc
)wxWidgetResizeProc
;
224 XtAppAddActions((XtAppContext
) wxTheApp
->m_appContext
, &rec
, 1);
226 GetMainColormap(dpy
);
233 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
235 if (!display
) /* Must be called first with non-NULL display */
236 return m_mainColormap
;
238 int defaultScreen
= DefaultScreen((Display
*) display
);
239 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
241 Colormap c
= DefaultColormapOfScreen(screen
);
244 m_mainColormap
= (WXColormap
) c
;
246 return (WXColormap
) c
;
249 static inline wxPerDisplayData
& GetOrCreatePerDisplayData
250 ( wxPerDisplayDataMap
& m
, WXDisplay
* display
)
252 wxPerDisplayDataMap::iterator it
= m
.find( display
);
253 if( it
!= m
.end() && it
->second
!= NULL
)
254 return *(it
->second
);
256 wxPerDisplayData
* nData
= new wxPerDisplayData();
262 wxXVisualInfo
* wxApp::GetVisualInfo( WXDisplay
* display
)
264 wxPerDisplayData
& data
= GetOrCreatePerDisplayData( *m_perDisplayData
,
266 if( data
.m_visualInfo
)
267 return data
.m_visualInfo
;
269 wxXVisualInfo
* vi
= new wxXVisualInfo
;
270 wxFillXVisualInfo( vi
, (Display
*)display
);
272 data
.m_visualInfo
= vi
;
277 static void wxTLWidgetDestroyCallback(Widget w
, XtPointer clientData
,
282 wxTheApp
->SetTopLevelWidget( (WXDisplay
*)XtDisplay(w
),
284 wxTheApp
->SetTopLevelRealizedWidget( (WXDisplay
*)XtDisplay(w
),
289 WXWidget
wxCreateTopLevelWidget( WXDisplay
* display
)
291 Widget tlw
= XtAppCreateShell( (String
)NULL
,
292 wxTheApp
->GetClassName().c_str(),
293 applicationShellWidgetClass
,
297 XmNoverrideRedirect
, True
,
300 XtAddCallback( tlw
, XmNdestroyCallback
,
301 (XtCallbackProc
)wxTLWidgetDestroyCallback
,
304 return (WXWidget
)tlw
;
307 WXWidget
wxCreateTopLevelRealizedWidget( WXDisplay
* display
)
309 Widget rTlw
= XtVaCreateWidget( "dummy_widget", topLevelShellWidgetClass
,
310 (Widget
)wxTheApp
->GetTopLevelWidget(),
312 XtSetMappedWhenManaged( rTlw
, False
);
313 XtRealizeWidget( rTlw
);
315 return (WXWidget
)rTlw
;
318 WXWidget
wxApp::GetTopLevelWidget()
320 WXDisplay
* display
= wxGetDisplay();
321 wxPerDisplayData
& data
= GetOrCreatePerDisplayData( *m_perDisplayData
,
323 if( data
.m_topLevelWidget
)
324 return (WXWidget
)data
.m_topLevelWidget
;
326 WXWidget tlw
= wxCreateTopLevelWidget( display
);
327 SetTopLevelWidget( display
, tlw
);
332 WXWidget
wxApp::GetTopLevelRealizedWidget()
334 WXDisplay
* display
= wxGetDisplay();
335 wxPerDisplayDataMap::iterator it
= m_perDisplayData
->find( display
);
337 if( it
!= m_perDisplayData
->end() && it
->second
->m_topLevelRealizedWidget
)
338 return (WXWidget
)it
->second
->m_topLevelRealizedWidget
;
340 WXWidget rTlw
= wxCreateTopLevelRealizedWidget( display
);
341 SetTopLevelRealizedWidget( display
, rTlw
);
346 void wxApp::SetTopLevelWidget(WXDisplay
* display
, WXWidget widget
)
348 GetOrCreatePerDisplayData( *m_perDisplayData
, display
)
349 .m_topLevelWidget
= (Widget
)widget
;
352 void wxApp::SetTopLevelRealizedWidget(WXDisplay
* display
, WXWidget widget
)
354 GetOrCreatePerDisplayData( *m_perDisplayData
, display
)
355 .m_topLevelRealizedWidget
= (Widget
)widget
;
358 // Yield to other processes
360 bool wxApp::Yield(bool onlyIfNeeded
)
362 static bool s_inYield
= false;
368 wxFAIL_MSG( wxT("wxYield called recursively" ) );
376 while (wxTheApp
&& wxTheApp
->Pending())
377 wxTheApp
->Dispatch();
384 // ----------------------------------------------------------------------------
385 // accessors for C modules
386 // ----------------------------------------------------------------------------
388 extern "C" XtAppContext
wxGetAppContext()
390 return (XtAppContext
)wxTheApp
->GetAppContext();