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"
32 #include "wx/hashmap.h"
35 #include "wx/thread.h"
39 #pragma message disable nosimpint
43 #include <X11/Xutil.h>
44 #include <X11/Xresource.h>
45 #include <X11/Xatom.h>
47 #pragma message enable nosimpint
50 #include "wx/motif/private.h"
54 struct wxPerDisplayData
57 { m_visualInfo
= NULL
; m_topLevelWidget
= NULL
; }
59 wxXVisualInfo
* m_visualInfo
;
60 Widget m_topLevelWidget
;
63 WX_DECLARE_VOIDPTR_HASH_MAP( wxPerDisplayData
, wxPerDisplayDataMap
);
65 static void wxTLWidgetDestroyCallback(Widget w
, XtPointer clientData
,
67 static WXWidget
wxCreateTopLevelWidget( WXDisplay
* display
);
69 extern wxList wxPendingDelete
;
70 extern bool wxAddIdleCallback();
72 wxHashTable
*wxWidgetHashTable
= NULL
;
74 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
76 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
77 EVT_IDLE(wxAppBase::OnIdle
)
81 typedef int (*XErrorHandlerFunc
)(Display
*, XErrorEvent
*);
83 XErrorHandlerFunc gs_pfnXErrorHandler
= 0;
85 static int wxXErrorHandler(Display
*dpy
, XErrorEvent
*xevent
)
87 // just forward to the default handler for now
88 return gs_pfnXErrorHandler(dpy
, xevent
);
92 bool wxApp::Initialize(int& argc
, wxChar
**argv
)
94 if ( !wxAppBase::Initialize(argc
, argv
) )
97 wxWidgetHashTable
= new wxHashTable(wxKEY_INTEGER
);
102 void wxApp::CleanUp()
104 delete wxWidgetHashTable
;
105 wxWidgetHashTable
= NULL
;
107 wxAppBase::CleanUp();
114 wxAppConsole::Exit();
117 // ============================================================================
119 // ============================================================================
126 m_mainLoop
= new wxEventLoop
;
127 m_mainColormap
= (WXColormap
) NULL
;
128 m_appContext
= (WXAppContext
) NULL
;
129 m_initialDisplay
= (WXDisplay
*) 0;
130 m_perDisplayData
= new wxPerDisplayDataMap
;
137 for( wxPerDisplayDataMap::iterator it
= m_perDisplayData
->begin(),
138 end
= m_perDisplayData
->end();
141 delete it
->second
.m_visualInfo
;
142 XtDestroyWidget( it
->second
.m_topLevelWidget
);
145 delete m_perDisplayData
;
147 wxApp::SetInstance(NULL
);
150 int wxApp::MainLoop()
153 * Sit around forever waiting to process X-events. Property Change
154 * event are handled special, because they have to refer to
155 * the root window rather than to a widget. therefore we can't
156 * use an Xt-eventhandler.
159 XSelectInput(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()),
160 XDefaultRootWindow(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget())),
168 // This should be redefined in a derived class for
169 // handling property change events for XAtom IPC.
170 void wxApp::HandlePropertyChange(WXEvent
*event
)
172 // by default do nothing special
173 XtDispatchEvent((XEvent
*) event
); /* let Motif do the work */
176 static char *fallbackResources
[] = {
177 "*menuBar.marginHeight: 0",
178 "*menuBar.shadowThickness: 1",
179 "*background: #c0c0c0",
180 "*foreground: black",
184 // Create an application context
185 bool wxApp::OnInitGui()
187 if( !wxAppBase::OnInitGui() )
190 XtToolkitInitialize() ;
191 wxTheApp
->m_appContext
= (WXAppContext
) XtCreateApplicationContext();
192 XtAppSetFallbackResources((XtAppContext
) wxTheApp
->m_appContext
, fallbackResources
);
194 Display
*dpy
= XtOpenDisplay((XtAppContext
) wxTheApp
->m_appContext
,(String
)NULL
,NULL
,
195 wxTheApp
->GetClassName().c_str(), NULL
, 0,
196 # if XtSpecificationRelease < 5
204 // if you don't log to stderr, nothing will be shown...
205 delete wxLog::SetActiveTarget(new wxLogStderr
);
206 wxString
className(wxTheApp
->GetClassName());
207 wxLogError(_("wxWindows could not open display for '%s': exiting."),
211 m_initialDisplay
= (WXDisplay
*) dpy
;
214 // install the X error handler
215 gs_pfnXErrorHandler
= XSetErrorHandler(wxXErrorHandler
);
216 #endif // __WXDEBUG__
218 // Add general resize proc
220 rec
.string
= "resize";
221 rec
.proc
= (XtActionProc
)wxWidgetResizeProc
;
222 XtAppAddActions((XtAppContext
) wxTheApp
->m_appContext
, &rec
, 1);
224 GetMainColormap(dpy
);
231 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
233 if (!display
) /* Must be called first with non-NULL display */
234 return m_mainColormap
;
236 int defaultScreen
= DefaultScreen((Display
*) display
);
237 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
239 Colormap c
= DefaultColormapOfScreen(screen
);
242 m_mainColormap
= (WXColormap
) c
;
244 return (WXColormap
) c
;
247 wxXVisualInfo
* wxApp::GetVisualInfo( WXDisplay
* display
)
249 wxPerDisplayDataMap::iterator it
= m_perDisplayData
->find( display
);
251 if( it
!= m_perDisplayData
->end() && it
->second
.m_visualInfo
)
252 return it
->second
.m_visualInfo
;
254 wxXVisualInfo
* vi
= new wxXVisualInfo
;
255 wxFillXVisualInfo( vi
, (Display
*)display
);
257 (*m_perDisplayData
)[display
].m_visualInfo
= vi
;
262 static void wxTLWidgetDestroyCallback(Widget w
, XtPointer clientData
,
266 wxTheApp
->SetTopLevelWidget( (WXDisplay
*)XtDisplay(w
),
270 WXWidget
wxCreateTopLevelWidget( WXDisplay
* display
)
272 Widget tlw
= XtAppCreateShell( (String
)NULL
,
273 wxTheApp
->GetClassName().c_str(),
274 applicationShellWidgetClass
,
277 XtSetMappedWhenManaged( tlw
, False
);
278 XtRealizeWidget( tlw
);
280 XtAddCallback( tlw
, XmNdestroyCallback
,
281 (XtCallbackProc
)wxTLWidgetDestroyCallback
,
284 return (WXWidget
)tlw
;
287 WXWidget
wxApp::GetTopLevelWidget()
289 WXDisplay
* display
= wxGetDisplay();
290 wxPerDisplayDataMap::iterator it
= m_perDisplayData
->find( display
);
292 if( it
!= m_perDisplayData
->end() && it
->second
.m_topLevelWidget
)
293 return (WXWidget
)it
->second
.m_topLevelWidget
;
295 WXWidget tlw
= wxCreateTopLevelWidget( display
);
296 SetTopLevelWidget( display
, tlw
);
301 void wxApp::SetTopLevelWidget(WXDisplay
* display
, WXWidget widget
)
303 (*m_perDisplayData
)[display
].m_topLevelWidget
= (Widget
)widget
;
306 // Yield to other processes
308 bool wxApp::Yield(bool onlyIfNeeded
)
310 static bool s_inYield
= FALSE
;
316 wxFAIL_MSG( wxT("wxYield called recursively" ) );
324 while (wxTheApp
&& wxTheApp
->Pending())
325 wxTheApp
->Dispatch();
332 // ----------------------------------------------------------------------------
333 // accessors for C modules
334 // ----------------------------------------------------------------------------
336 extern "C" XtAppContext
wxGetAppContext()
338 return (XtAppContext
)wxTheApp
->GetAppContext();