1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
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(wxApp::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_eventLoop
= 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
;
147 bool wxApp::Initialized()
155 int wxApp::MainLoop()
158 * Sit around forever waiting to process X-events. Property Change
159 * event are handled special, because they have to refer to
160 * the root window rather than to a widget. therefore we can't
161 * use an Xt-eventhandler.
164 XSelectInput(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()),
165 XDefaultRootWindow(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget())),
173 void wxApp::ExitMainLoop()
175 if( m_eventLoop
->IsRunning() )
179 // Is a message/event pending?
180 bool wxApp::Pending()
182 return m_eventLoop
->Pending();
184 XFlush(XtDisplay( (Widget
) wxTheApp
->GetTopLevelWidget() ));
186 // Fix by Doug from STI, to prevent a stall if non-X event
188 return ((XtAppPending( (XtAppContext
) GetAppContext() ) & XtIMXEvent
) != 0) ;
192 // Dispatch a message.
193 void wxApp::Dispatch()
195 m_eventLoop
->Dispatch();
198 // This should be redefined in a derived class for
199 // handling property change events for XAtom IPC.
200 void wxApp::HandlePropertyChange(WXEvent
*event
)
202 // by default do nothing special
203 XtDispatchEvent((XEvent
*) event
); /* let Motif do the work */
206 void wxApp::OnIdle(wxIdleEvent
& event
)
208 static bool inOnIdle
= FALSE
;
210 // Avoid recursion (via ProcessEvent default case)
216 // If there are pending events, we must process them: pending events
217 // are either events to the threads other than main or events posted
218 // with wxPostEvent() functions
219 // GRG: I have moved this here so that all pending events are processed
220 // before starting to delete any objects. This behaves better (in
221 // particular, wrt wxPostEvent) and is coherent with wxGTK's current
222 // behaviour. Also removed the '#if wxUSE_THREADS' around it.
223 // Changed Mar/2000 before 2.1.14
225 // Flush pending events.
226 ProcessPendingEvents();
228 // 'Garbage' collection of windows deleted with Close().
229 DeletePendingObjects();
231 // flush the logged messages if any
232 wxLog
*pLog
= wxLog::GetActiveTarget();
233 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
236 // Send OnIdle events to all windows
237 bool needMore
= SendIdleEvents();
240 event
.RequestMore(TRUE
);
245 static char *fallbackResources
[] = {
246 "*menuBar.marginHeight: 0",
247 "*menuBar.shadowThickness: 1",
248 "*background: #c0c0c0",
249 "*foreground: black",
253 // Create an application context
254 bool wxApp::OnInitGui()
256 if( !wxAppBase::OnInitGui() )
259 XtToolkitInitialize() ;
260 wxTheApp
->m_appContext
= (WXAppContext
) XtCreateApplicationContext();
261 XtAppSetFallbackResources((XtAppContext
) wxTheApp
->m_appContext
, fallbackResources
);
263 Display
*dpy
= XtOpenDisplay((XtAppContext
) wxTheApp
->m_appContext
,(String
)NULL
,NULL
,
264 wxTheApp
->GetClassName().c_str(), NULL
, 0,
265 # if XtSpecificationRelease < 5
273 // if you don't log to stderr, nothing will be shown...
274 delete wxLog::SetActiveTarget(new wxLogStderr
);
275 wxString
className(wxTheApp
->GetClassName());
276 wxLogError(_("wxWindows could not open display for '%s': exiting."),
280 m_initialDisplay
= (WXDisplay
*) dpy
;
283 // install the X error handler
284 gs_pfnXErrorHandler
= XSetErrorHandler(wxXErrorHandler
);
285 #endif // __WXDEBUG__
287 // Add general resize proc
289 rec
.string
= "resize";
290 rec
.proc
= (XtActionProc
)wxWidgetResizeProc
;
291 XtAppAddActions((XtAppContext
) wxTheApp
->m_appContext
, &rec
, 1);
293 GetMainColormap(dpy
);
300 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
302 if (!display
) /* Must be called first with non-NULL display */
303 return m_mainColormap
;
305 int defaultScreen
= DefaultScreen((Display
*) display
);
306 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
308 Colormap c
= DefaultColormapOfScreen(screen
);
311 m_mainColormap
= (WXColormap
) c
;
313 return (WXColormap
) c
;
316 wxXVisualInfo
* wxApp::GetVisualInfo( WXDisplay
* display
)
318 wxPerDisplayDataMap::iterator it
= m_perDisplayData
->find( display
);
320 if( it
!= m_perDisplayData
->end() && it
->second
.m_visualInfo
)
321 return it
->second
.m_visualInfo
;
323 wxXVisualInfo
* vi
= new wxXVisualInfo
;
324 wxFillXVisualInfo( vi
, (Display
*)display
);
326 (*m_perDisplayData
)[display
].m_visualInfo
= vi
;
331 static void wxTLWidgetDestroyCallback(Widget w
, XtPointer clientData
,
335 wxTheApp
->SetTopLevelWidget( (WXDisplay
*)XtDisplay(w
),
339 WXWidget
wxCreateTopLevelWidget( WXDisplay
* display
)
341 Widget tlw
= XtAppCreateShell( (String
)NULL
,
342 wxTheApp
->GetClassName().c_str(),
343 applicationShellWidgetClass
,
346 XtSetMappedWhenManaged( tlw
, False
);
347 XtRealizeWidget( tlw
);
349 XtAddCallback( tlw
, XmNdestroyCallback
,
350 (XtCallbackProc
)wxTLWidgetDestroyCallback
,
353 return (WXWidget
)tlw
;
356 WXWidget
wxApp::GetTopLevelWidget()
358 WXDisplay
* display
= wxGetDisplay();
359 wxPerDisplayDataMap::iterator it
= m_perDisplayData
->find( display
);
361 if( it
!= m_perDisplayData
->end() && it
->second
.m_topLevelWidget
)
362 return (WXWidget
)it
->second
.m_topLevelWidget
;
364 WXWidget tlw
= wxCreateTopLevelWidget( display
);
365 SetTopLevelWidget( display
, tlw
);
370 void wxApp::SetTopLevelWidget(WXDisplay
* display
, WXWidget widget
)
372 (*m_perDisplayData
)[display
].m_topLevelWidget
= (Widget
)widget
;
375 // Yield to other processes
377 bool wxApp::Yield(bool onlyIfNeeded
)
379 bool s_inYield
= FALSE
;
385 wxFAIL_MSG( wxT("wxYield called recursively" ) );
393 while (wxTheApp
&& wxTheApp
->Pending())
394 wxTheApp
->Dispatch();
401 // ----------------------------------------------------------------------------
402 // accessors for C modules
403 // ----------------------------------------------------------------------------
405 extern "C" XtAppContext
wxGetAppContext()
407 return (XtAppContext
)wxTheApp
->GetAppContext();