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
);
101 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
107 void wxApp::CleanUp()
109 wxAppBase::CleanUp();
111 delete wxWidgetHashTable
;
112 wxWidgetHashTable
= NULL
;
116 for( wxPerDisplayDataMap::iterator it
= m_perDisplayData
->begin(),
117 end
= m_perDisplayData
->end();
120 delete it
->second
->m_visualInfo
;
121 XtDestroyWidget( it
->second
->m_topLevelWidget
);
125 delete m_perDisplayData
;
132 wxAppConsole::Exit();
135 // ============================================================================
137 // ============================================================================
144 m_mainLoop
= new wxEventLoop
;
145 m_mainColormap
= (WXColormap
) NULL
;
146 m_appContext
= (WXAppContext
) NULL
;
147 m_initialDisplay
= (WXDisplay
*) 0;
148 m_perDisplayData
= new wxPerDisplayDataMap
;
153 wxApp::SetInstance(NULL
);
156 int wxApp::MainLoop()
159 * Sit around forever waiting to process X-events. Property Change
160 * event are handled special, because they have to refer to
161 * the root window rather than to a widget. therefore we can't
162 * use an Xt-eventhandler.
165 XSelectInput(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()),
166 XDefaultRootWindow(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget())),
174 // This should be redefined in a derived class for
175 // handling property change events for XAtom IPC.
176 void wxApp::HandlePropertyChange(WXEvent
*event
)
178 // by default do nothing special
179 XtDispatchEvent((XEvent
*) event
); /* let Motif do the work */
182 static char *fallbackResources
[] = {
183 "*menuBar.marginHeight: 0",
184 "*menuBar.shadowThickness: 1",
185 "*background: #c0c0c0",
186 "*foreground: black",
190 // Create an application context
191 bool wxApp::OnInitGui()
193 if( !wxAppBase::OnInitGui() )
196 XtSetLanguageProc(NULL
, NULL
, NULL
);
197 XtToolkitInitialize() ;
198 wxTheApp
->m_appContext
= (WXAppContext
) XtCreateApplicationContext();
199 XtAppSetFallbackResources((XtAppContext
) wxTheApp
->m_appContext
, fallbackResources
);
201 Display
*dpy
= XtOpenDisplay((XtAppContext
) wxTheApp
->m_appContext
,(String
)NULL
,NULL
,
202 wxTheApp
->GetClassName().c_str(), NULL
, 0,
203 # if XtSpecificationRelease < 5
211 // if you don't log to stderr, nothing will be shown...
212 delete wxLog::SetActiveTarget(new wxLogStderr
);
213 wxString
className(wxTheApp
->GetClassName());
214 wxLogError(_("wxWidgets could not open display for '%s': exiting."),
218 m_initialDisplay
= (WXDisplay
*) dpy
;
221 // install the X error handler
222 gs_pfnXErrorHandler
= XSetErrorHandler(wxXErrorHandler
);
223 #endif // __WXDEBUG__
225 // Add general resize proc
227 rec
.string
= "resize";
228 rec
.proc
= (XtActionProc
)wxWidgetResizeProc
;
229 XtAppAddActions((XtAppContext
) wxTheApp
->m_appContext
, &rec
, 1);
231 GetMainColormap(dpy
);
238 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
240 if (!display
) /* Must be called first with non-NULL display */
241 return m_mainColormap
;
243 int defaultScreen
= DefaultScreen((Display
*) display
);
244 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
246 Colormap c
= DefaultColormapOfScreen(screen
);
249 m_mainColormap
= (WXColormap
) c
;
251 return (WXColormap
) c
;
254 static inline wxPerDisplayData
& GetOrCreatePerDisplayData
255 ( wxPerDisplayDataMap
& m
, WXDisplay
* display
)
257 wxPerDisplayDataMap::iterator it
= m
.find( display
);
258 if( it
!= m
.end() && it
->second
!= NULL
)
259 return *(it
->second
);
261 wxPerDisplayData
* nData
= new wxPerDisplayData();
267 wxXVisualInfo
* wxApp::GetVisualInfo( WXDisplay
* display
)
269 wxPerDisplayData
& data
= GetOrCreatePerDisplayData( *m_perDisplayData
,
271 if( data
.m_visualInfo
)
272 return data
.m_visualInfo
;
274 wxXVisualInfo
* vi
= new wxXVisualInfo
;
275 wxFillXVisualInfo( vi
, (Display
*)display
);
277 data
.m_visualInfo
= vi
;
282 static void wxTLWidgetDestroyCallback(Widget w
, XtPointer clientData
,
287 wxTheApp
->SetTopLevelWidget( (WXDisplay
*)XtDisplay(w
),
289 wxTheApp
->SetTopLevelRealizedWidget( (WXDisplay
*)XtDisplay(w
),
294 WXWidget
wxCreateTopLevelWidget( WXDisplay
* display
)
296 Widget tlw
= XtAppCreateShell( (String
)NULL
,
297 wxTheApp
->GetClassName().c_str(),
298 applicationShellWidgetClass
,
302 XmNoverrideRedirect
, True
,
305 XtAddCallback( tlw
, XmNdestroyCallback
,
306 (XtCallbackProc
)wxTLWidgetDestroyCallback
,
309 return (WXWidget
)tlw
;
312 WXWidget
wxCreateTopLevelRealizedWidget( WXDisplay
* display
)
314 Widget rTlw
= XtVaCreateWidget( "dummy_widget", topLevelShellWidgetClass
,
315 (Widget
)wxTheApp
->GetTopLevelWidget(),
317 XtSetMappedWhenManaged( rTlw
, False
);
318 XtRealizeWidget( rTlw
);
320 return (WXWidget
)rTlw
;
323 WXWidget
wxApp::GetTopLevelWidget()
325 WXDisplay
* display
= wxGetDisplay();
326 wxPerDisplayData
& data
= GetOrCreatePerDisplayData( *m_perDisplayData
,
328 if( data
.m_topLevelWidget
)
329 return (WXWidget
)data
.m_topLevelWidget
;
331 WXWidget tlw
= wxCreateTopLevelWidget( display
);
332 SetTopLevelWidget( display
, tlw
);
337 WXWidget
wxApp::GetTopLevelRealizedWidget()
339 WXDisplay
* display
= wxGetDisplay();
340 wxPerDisplayDataMap::iterator it
= m_perDisplayData
->find( display
);
342 if( it
!= m_perDisplayData
->end() && it
->second
->m_topLevelRealizedWidget
)
343 return (WXWidget
)it
->second
->m_topLevelRealizedWidget
;
345 WXWidget rTlw
= wxCreateTopLevelRealizedWidget( display
);
346 SetTopLevelRealizedWidget( display
, rTlw
);
351 void wxApp::SetTopLevelWidget(WXDisplay
* display
, WXWidget widget
)
353 GetOrCreatePerDisplayData( *m_perDisplayData
, display
)
354 .m_topLevelWidget
= (Widget
)widget
;
357 void wxApp::SetTopLevelRealizedWidget(WXDisplay
* display
, WXWidget widget
)
359 GetOrCreatePerDisplayData( *m_perDisplayData
, display
)
360 .m_topLevelRealizedWidget
= (Widget
)widget
;
363 // Yield to other processes
365 bool wxApp::Yield(bool onlyIfNeeded
)
367 static bool s_inYield
= false;
373 wxFAIL_MSG( wxT("wxYield called recursively" ) );
381 while (wxTheApp
&& wxTheApp
->Pending())
382 wxTheApp
->Dispatch();
389 // ----------------------------------------------------------------------------
390 // accessors for C modules
391 // ----------------------------------------------------------------------------
393 extern "C" XtAppContext
wxGetAppContext()
395 return (XtAppContext
)wxTheApp
->GetAppContext();