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"
17 #define XtParent XTPARENT
18 #define XtDisplay XTDISPLAY
23 #include "wx/module.h"
24 #include "wx/memory.h"
27 #include "wx/evtloop.h"
29 #include "wx/hashmap.h"
32 #include "wx/thread.h"
36 #pragma message disable nosimpint
40 #include <X11/Xutil.h>
41 #include <X11/Xresource.h>
42 #include <X11/Xatom.h>
44 #pragma message enable nosimpint
47 #include "wx/motif/private.h"
51 struct wxPerDisplayData
54 { m_visualInfo
= NULL
; m_topLevelWidget
= NULL
; }
56 wxXVisualInfo
* m_visualInfo
;
57 Widget m_topLevelWidget
;
60 WX_DECLARE_VOIDPTR_HASH_MAP( wxPerDisplayData
, wxPerDisplayDataMap
);
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
);
142 delete m_perDisplayData
;
144 wxApp::SetInstance(NULL
);
147 int wxApp::MainLoop()
150 * Sit around forever waiting to process X-events. Property Change
151 * event are handled special, because they have to refer to
152 * the root window rather than to a widget. therefore we can't
153 * use an Xt-eventhandler.
156 XSelectInput(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()),
157 XDefaultRootWindow(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget())),
165 // This should be redefined in a derived class for
166 // handling property change events for XAtom IPC.
167 void wxApp::HandlePropertyChange(WXEvent
*event
)
169 // by default do nothing special
170 XtDispatchEvent((XEvent
*) event
); /* let Motif do the work */
173 static char *fallbackResources
[] = {
174 "*menuBar.marginHeight: 0",
175 "*menuBar.shadowThickness: 1",
176 "*background: #c0c0c0",
177 "*foreground: black",
181 // Create an application context
182 bool wxApp::OnInitGui()
184 if( !wxAppBase::OnInitGui() )
187 XtToolkitInitialize() ;
188 wxTheApp
->m_appContext
= (WXAppContext
) XtCreateApplicationContext();
189 XtAppSetFallbackResources((XtAppContext
) wxTheApp
->m_appContext
, fallbackResources
);
191 Display
*dpy
= XtOpenDisplay((XtAppContext
) wxTheApp
->m_appContext
,(String
)NULL
,NULL
,
192 wxTheApp
->GetClassName().c_str(), NULL
, 0,
193 # if XtSpecificationRelease < 5
201 // if you don't log to stderr, nothing will be shown...
202 delete wxLog::SetActiveTarget(new wxLogStderr
);
203 wxString
className(wxTheApp
->GetClassName());
204 wxLogError(_("wxWindows could not open display for '%s': exiting."),
208 m_initialDisplay
= (WXDisplay
*) dpy
;
211 // install the X error handler
212 gs_pfnXErrorHandler
= XSetErrorHandler(wxXErrorHandler
);
213 #endif // __WXDEBUG__
215 // Add general resize proc
217 rec
.string
= "resize";
218 rec
.proc
= (XtActionProc
)wxWidgetResizeProc
;
219 XtAppAddActions((XtAppContext
) wxTheApp
->m_appContext
, &rec
, 1);
221 GetMainColormap(dpy
);
228 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
230 if (!display
) /* Must be called first with non-NULL display */
231 return m_mainColormap
;
233 int defaultScreen
= DefaultScreen((Display
*) display
);
234 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
236 Colormap c
= DefaultColormapOfScreen(screen
);
239 m_mainColormap
= (WXColormap
) c
;
241 return (WXColormap
) c
;
244 wxXVisualInfo
* wxApp::GetVisualInfo( WXDisplay
* display
)
246 wxPerDisplayDataMap::iterator it
= m_perDisplayData
->find( display
);
248 if( it
!= m_perDisplayData
->end() && it
->second
.m_visualInfo
)
249 return it
->second
.m_visualInfo
;
251 wxXVisualInfo
* vi
= new wxXVisualInfo
;
252 wxFillXVisualInfo( vi
, (Display
*)display
);
254 (*m_perDisplayData
)[display
].m_visualInfo
= vi
;
259 static void wxTLWidgetDestroyCallback(Widget w
, XtPointer clientData
,
263 wxTheApp
->SetTopLevelWidget( (WXDisplay
*)XtDisplay(w
),
267 WXWidget
wxCreateTopLevelWidget( WXDisplay
* display
)
269 Widget tlw
= XtAppCreateShell( (String
)NULL
,
270 wxTheApp
->GetClassName().c_str(),
271 applicationShellWidgetClass
,
274 XtSetMappedWhenManaged( tlw
, False
);
275 XtRealizeWidget( tlw
);
277 XtAddCallback( tlw
, XmNdestroyCallback
,
278 (XtCallbackProc
)wxTLWidgetDestroyCallback
,
281 return (WXWidget
)tlw
;
284 WXWidget
wxApp::GetTopLevelWidget()
286 WXDisplay
* display
= wxGetDisplay();
287 wxPerDisplayDataMap::iterator it
= m_perDisplayData
->find( display
);
289 if( it
!= m_perDisplayData
->end() && it
->second
.m_topLevelWidget
)
290 return (WXWidget
)it
->second
.m_topLevelWidget
;
292 WXWidget tlw
= wxCreateTopLevelWidget( display
);
293 SetTopLevelWidget( display
, tlw
);
298 void wxApp::SetTopLevelWidget(WXDisplay
* display
, WXWidget widget
)
300 (*m_perDisplayData
)[display
].m_topLevelWidget
= (Widget
)widget
;
303 // Yield to other processes
305 bool wxApp::Yield(bool onlyIfNeeded
)
307 bool s_inYield
= FALSE
;
313 wxFAIL_MSG( wxT("wxYield called recursively" ) );
321 while (wxTheApp
&& wxTheApp
->Pending())
322 wxTheApp
->Dispatch();
329 // ----------------------------------------------------------------------------
330 // accessors for C modules
331 // ----------------------------------------------------------------------------
333 extern "C" XtAppContext
wxGetAppContext()
335 return (XtAppContext
)wxTheApp
->GetAppContext();