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
)
84 typedef int (*XErrorHandlerFunc
)(Display
*, XErrorEvent
*);
87 XErrorHandlerFunc gs_pfnXErrorHandler
= 0;
92 static int wxXErrorHandler(Display
*dpy
, XErrorEvent
*xevent
)
94 // just forward to the default handler for now
95 return gs_pfnXErrorHandler(dpy
, xevent
);
101 bool wxApp
::Initialize(int& argcOrig
, wxChar
**argvOrig
)
103 if ( !wxAppBase
::Initialize(argcOrig
, argvOrig
) )
106 wxWidgetHashTable
= new wxHashTable(wxKEY_INTEGER
);
109 wxFont
::SetDefaultEncoding(wxLocale
::GetSystemEncoding());
115 void wxApp
::CleanUp()
117 wxAppBase
::CleanUp();
119 delete wxWidgetHashTable
;
120 wxWidgetHashTable
= NULL
;
124 for( wxPerDisplayDataMap
::iterator it
= m_perDisplayData
->begin(),
125 end
= m_perDisplayData
->end();
128 delete it
->second
->m_visualInfo
;
129 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 // some compilers (e.g. Sun CC) give warnings when treating string literals as
191 // (non const) "char *" so use this macro to suppress them when we know it's ok
192 #define STR(x) wx_const_cast(char *, x)
194 static char *fallbackResources
[] = {
195 // better defaults for CDE under Irix
197 // TODO: do something similar for the other systems, the hardcoded defaults
200 STR("*sgiMode: True"),
201 STR("*useSchemes: all"),
203 STR("*menuBar.marginHeight: 0"),
204 STR("*menuBar.shadowThickness: 1"),
205 STR("*background: #c0c0c0"),
206 STR("*foreground: black"),
207 #endif // __SGI__/!__SGI__
211 // Create an application context
212 bool wxApp
::OnInitGui()
214 if( !wxAppBase
::OnInitGui() )
217 XtSetLanguageProc(NULL
, NULL
, NULL
);
218 XtToolkitInitialize() ;
219 wxTheApp
->m_appContext
= (WXAppContext
) XtCreateApplicationContext();
220 XtAppSetFallbackResources((XtAppContext
) wxTheApp
->m_appContext
, fallbackResources
);
222 Display
*dpy
= XtOpenDisplay((XtAppContext
) wxTheApp
->m_appContext
,(String
)NULL
,NULL
,
223 wxTheApp
->GetClassName().c_str(), NULL
, 0,
224 # if XtSpecificationRelease < 5
232 // if you don't log to stderr, nothing will be shown...
233 delete wxLog
::SetActiveTarget(new wxLogStderr
);
234 wxString
className(wxTheApp
->GetClassName());
235 wxLogError(_("wxWidgets could not open display for '%s': exiting."),
239 m_initialDisplay
= (WXDisplay
*) dpy
;
242 // install the X error handler
243 gs_pfnXErrorHandler
= XSetErrorHandler(wxXErrorHandler
);
244 #endif // __WXDEBUG__
246 // Add general resize proc
248 rec
.string
= STR("resize");
249 rec
.proc
= (XtActionProc
)wxWidgetResizeProc
;
250 XtAppAddActions((XtAppContext
) wxTheApp
->m_appContext
, &rec
, 1);
252 GetMainColormap(dpy
);
259 WXColormap wxApp
::GetMainColormap(WXDisplay
* display
)
261 if (!display
) /* Must be called first with non-NULL display */
262 return m_mainColormap
;
264 int defaultScreen
= DefaultScreen((Display
*) display
);
265 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
267 Colormap c
= DefaultColormapOfScreen(screen
);
270 m_mainColormap
= (WXColormap
) c
;
272 return (WXColormap
) c
;
275 static inline wxPerDisplayData
& GetOrCreatePerDisplayData
276 ( wxPerDisplayDataMap
& m
, WXDisplay
* display
)
278 wxPerDisplayDataMap
::iterator it
= m
.find( display
);
279 if( it
!= m
.end() && it
->second
!= NULL
)
280 return *(it
->second
);
282 wxPerDisplayData
* nData
= new wxPerDisplayData();
288 wxXVisualInfo
* wxApp
::GetVisualInfo( WXDisplay
* display
)
290 wxPerDisplayData
& data
= GetOrCreatePerDisplayData( *m_perDisplayData
,
292 if( data
.m_visualInfo
)
293 return data
.m_visualInfo
;
295 wxXVisualInfo
* vi
= new wxXVisualInfo
;
296 wxFillXVisualInfo( vi
, (Display
*)display
);
298 data
.m_visualInfo
= vi
;
303 static void wxTLWidgetDestroyCallback(Widget w
, XtPointer clientData
,
308 wxTheApp
->SetTopLevelWidget( (WXDisplay
*)XtDisplay(w
),
310 wxTheApp
->SetTopLevelRealizedWidget( (WXDisplay
*)XtDisplay(w
),
315 WXWidget
wxCreateTopLevelWidget( WXDisplay
* display
)
317 Widget tlw
= XtAppCreateShell( (String
)NULL
,
318 wxTheApp
->GetClassName().c_str(),
319 applicationShellWidgetClass
,
323 XmNoverrideRedirect
, True
,
326 XtAddCallback( tlw
, XmNdestroyCallback
,
327 (XtCallbackProc
)wxTLWidgetDestroyCallback
,
330 return (WXWidget
)tlw
;
333 WXWidget
wxCreateTopLevelRealizedWidget( WXDisplay
* display
)
335 Widget rTlw
= XtVaCreateWidget( "dummy_widget", topLevelShellWidgetClass
,
336 (Widget
)wxTheApp
->GetTopLevelWidget(),
338 XtSetMappedWhenManaged( rTlw
, False
);
339 XtRealizeWidget( rTlw
);
341 return (WXWidget
)rTlw
;
344 WXWidget wxApp
::GetTopLevelWidget()
346 WXDisplay
* display
= wxGetDisplay();
347 wxPerDisplayData
& data
= GetOrCreatePerDisplayData( *m_perDisplayData
,
349 if( data
.m_topLevelWidget
)
350 return (WXWidget
)data
.m_topLevelWidget
;
352 WXWidget tlw
= wxCreateTopLevelWidget( display
);
353 SetTopLevelWidget( display
, tlw
);
358 WXWidget wxApp
::GetTopLevelRealizedWidget()
360 WXDisplay
* display
= wxGetDisplay();
361 wxPerDisplayDataMap
::iterator it
= m_perDisplayData
->find( display
);
363 if( it
!= m_perDisplayData
->end() && it
->second
->m_topLevelRealizedWidget
)
364 return (WXWidget
)it
->second
->m_topLevelRealizedWidget
;
366 WXWidget rTlw
= wxCreateTopLevelRealizedWidget( display
);
367 SetTopLevelRealizedWidget( display
, rTlw
);
372 void wxApp
::SetTopLevelWidget(WXDisplay
* display
, WXWidget widget
)
374 GetOrCreatePerDisplayData( *m_perDisplayData
, display
)
375 .m_topLevelWidget
= (Widget
)widget
;
378 void wxApp
::SetTopLevelRealizedWidget(WXDisplay
* display
, WXWidget widget
)
380 GetOrCreatePerDisplayData( *m_perDisplayData
, display
)
381 .m_topLevelRealizedWidget
= (Widget
)widget
;
384 // Yield to other processes
386 bool wxApp
::Yield(bool onlyIfNeeded
)
388 static bool s_inYield
= false;
394 wxFAIL_MSG( wxT("wxYield called recursively" ) );
402 while (wxTheApp
&& wxTheApp
->Pending())
403 wxTheApp
->Dispatch();
410 // ----------------------------------------------------------------------------
411 // accessors for C modules
412 // ----------------------------------------------------------------------------
414 extern "C" XtAppContext
wxGetAppContext()
416 return (XtAppContext
)wxTheApp
->GetAppContext();