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
56 { m_visualInfo
= NULL
; m_topLevelWidget
= NULL
; }
58 wxXVisualInfo
* m_visualInfo
;
59 Widget m_topLevelWidget
;
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
)
78 typedef int (*XErrorHandlerFunc
)(Display
*, XErrorEvent
*);
80 XErrorHandlerFunc gs_pfnXErrorHandler
= 0;
82 static int wxXErrorHandler(Display
*dpy
, XErrorEvent
*xevent
)
84 // just forward to the default handler for now
85 return gs_pfnXErrorHandler(dpy
, xevent
);
89 bool wxApp::Initialize(int& argc
, wxChar
**argv
)
91 if ( !wxAppBase::Initialize(argc
, argv
) )
94 wxWidgetHashTable
= new wxHashTable(wxKEY_INTEGER
);
101 delete wxWidgetHashTable
;
102 wxWidgetHashTable
= NULL
;
104 wxAppBase::CleanUp();
111 wxAppConsole::Exit();
114 // ============================================================================
116 // ============================================================================
123 m_mainLoop
= new wxEventLoop
;
124 m_mainColormap
= (WXColormap
) NULL
;
125 m_appContext
= (WXAppContext
) NULL
;
126 m_initialDisplay
= (WXDisplay
*) 0;
127 m_perDisplayData
= new wxPerDisplayDataMap
;
134 for( wxPerDisplayDataMap::iterator it
= m_perDisplayData
->begin(),
135 end
= m_perDisplayData
->end();
138 delete it
->second
->m_visualInfo
;
139 XtDestroyWidget( it
->second
->m_topLevelWidget
);
143 delete m_perDisplayData
;
145 wxApp::SetInstance(NULL
);
148 int wxApp::MainLoop()
151 * Sit around forever waiting to process X-events. Property Change
152 * event are handled special, because they have to refer to
153 * the root window rather than to a widget. therefore we can't
154 * use an Xt-eventhandler.
157 XSelectInput(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()),
158 XDefaultRootWindow(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget())),
166 // This should be redefined in a derived class for
167 // handling property change events for XAtom IPC.
168 void wxApp::HandlePropertyChange(WXEvent
*event
)
170 // by default do nothing special
171 XtDispatchEvent((XEvent
*) event
); /* let Motif do the work */
174 static char *fallbackResources
[] = {
175 "*menuBar.marginHeight: 0",
176 "*menuBar.shadowThickness: 1",
177 "*background: #c0c0c0",
178 "*foreground: black",
182 // Create an application context
183 bool wxApp::OnInitGui()
185 if( !wxAppBase::OnInitGui() )
188 XtToolkitInitialize() ;
189 wxTheApp
->m_appContext
= (WXAppContext
) XtCreateApplicationContext();
190 XtAppSetFallbackResources((XtAppContext
) wxTheApp
->m_appContext
, fallbackResources
);
192 Display
*dpy
= XtOpenDisplay((XtAppContext
) wxTheApp
->m_appContext
,(String
)NULL
,NULL
,
193 wxTheApp
->GetClassName().c_str(), NULL
, 0,
194 # if XtSpecificationRelease < 5
202 // if you don't log to stderr, nothing will be shown...
203 delete wxLog::SetActiveTarget(new wxLogStderr
);
204 wxString
className(wxTheApp
->GetClassName());
205 wxLogError(_("wxWindows could not open display for '%s': exiting."),
209 m_initialDisplay
= (WXDisplay
*) dpy
;
212 // install the X error handler
213 gs_pfnXErrorHandler
= XSetErrorHandler(wxXErrorHandler
);
214 #endif // __WXDEBUG__
216 // Add general resize proc
218 rec
.string
= "resize";
219 rec
.proc
= (XtActionProc
)wxWidgetResizeProc
;
220 XtAppAddActions((XtAppContext
) wxTheApp
->m_appContext
, &rec
, 1);
222 GetMainColormap(dpy
);
229 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
231 if (!display
) /* Must be called first with non-NULL display */
232 return m_mainColormap
;
234 int defaultScreen
= DefaultScreen((Display
*) display
);
235 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
237 Colormap c
= DefaultColormapOfScreen(screen
);
240 m_mainColormap
= (WXColormap
) c
;
242 return (WXColormap
) c
;
245 static inline wxPerDisplayData
& GetOrCreatePerDisplayData
246 ( wxPerDisplayDataMap
& m
, WXDisplay
* display
)
248 wxPerDisplayDataMap::iterator it
= m
.find( display
);
249 if( it
!= m
.end() && it
->second
!= NULL
)
250 return *(it
->second
);
252 wxPerDisplayData
* nData
= new wxPerDisplayData();
258 wxXVisualInfo
* wxApp::GetVisualInfo( WXDisplay
* display
)
260 wxPerDisplayData
& data
= GetOrCreatePerDisplayData( *m_perDisplayData
,
262 if( data
.m_visualInfo
)
263 return data
.m_visualInfo
;
265 wxXVisualInfo
* vi
= new wxXVisualInfo
;
266 wxFillXVisualInfo( vi
, (Display
*)display
);
268 data
.m_visualInfo
= vi
;
273 static void wxTLWidgetDestroyCallback(Widget w
, XtPointer clientData
,
277 wxTheApp
->SetTopLevelWidget( (WXDisplay
*)XtDisplay(w
),
281 WXWidget
wxCreateTopLevelWidget( WXDisplay
* display
)
283 Widget tlw
= XtAppCreateShell( (String
)NULL
,
284 wxTheApp
->GetClassName().c_str(),
285 applicationShellWidgetClass
,
288 XtSetMappedWhenManaged( tlw
, False
);
289 XtRealizeWidget( tlw
);
291 XtAddCallback( tlw
, XmNdestroyCallback
,
292 (XtCallbackProc
)wxTLWidgetDestroyCallback
,
295 return (WXWidget
)tlw
;
298 WXWidget
wxApp::GetTopLevelWidget()
300 WXDisplay
* display
= wxGetDisplay();
301 wxPerDisplayData
& data
= GetOrCreatePerDisplayData( *m_perDisplayData
,
303 if( data
.m_topLevelWidget
)
304 return (WXWidget
)data
.m_topLevelWidget
;
306 WXWidget tlw
= wxCreateTopLevelWidget( display
);
307 SetTopLevelWidget( display
, tlw
);
312 void wxApp::SetTopLevelWidget(WXDisplay
* display
, WXWidget widget
)
314 (*m_perDisplayData
)[display
]->m_topLevelWidget
= (Widget
)widget
;
317 // Yield to other processes
319 bool wxApp::Yield(bool onlyIfNeeded
)
321 static bool s_inYield
= FALSE
;
327 wxFAIL_MSG( wxT("wxYield called recursively" ) );
335 while (wxTheApp
&& wxTheApp
->Pending())
336 wxTheApp
->Dispatch();
343 // ----------------------------------------------------------------------------
344 // accessors for C modules
345 // ----------------------------------------------------------------------------
347 extern "C" XtAppContext
wxGetAppContext()
349 return (XtAppContext
)wxTheApp
->GetAppContext();