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 // Processes an idle event.
174 // Returns TRUE if more time is needed.
175 bool wxApp::ProcessIdle()
179 return ProcessEvent(event
) && event
.MoreRequested();
182 void wxApp::ExitMainLoop()
184 if( m_eventLoop
->IsRunning() )
188 // Is a message/event pending?
189 bool wxApp::Pending()
191 return m_eventLoop
->Pending();
193 XFlush(XtDisplay( (Widget
) wxTheApp
->GetTopLevelWidget() ));
195 // Fix by Doug from STI, to prevent a stall if non-X event
197 return ((XtAppPending( (XtAppContext
) GetAppContext() ) & XtIMXEvent
) != 0) ;
201 // Dispatch a message.
202 void wxApp::Dispatch()
204 m_eventLoop
->Dispatch();
207 // This should be redefined in a derived class for
208 // handling property change events for XAtom IPC.
209 void wxApp::HandlePropertyChange(WXEvent
*event
)
211 // by default do nothing special
212 XtDispatchEvent((XEvent
*) event
); /* let Motif do the work */
215 void wxApp::OnIdle(wxIdleEvent
& event
)
217 static bool inOnIdle
= FALSE
;
219 // Avoid recursion (via ProcessEvent default case)
225 // If there are pending events, we must process them: pending events
226 // are either events to the threads other than main or events posted
227 // with wxPostEvent() functions
228 // GRG: I have moved this here so that all pending events are processed
229 // before starting to delete any objects. This behaves better (in
230 // particular, wrt wxPostEvent) and is coherent with wxGTK's current
231 // behaviour. Also removed the '#if wxUSE_THREADS' around it.
232 // Changed Mar/2000 before 2.1.14
234 // Flush pending events.
235 ProcessPendingEvents();
237 // 'Garbage' collection of windows deleted with Close().
238 DeletePendingObjects();
240 // flush the logged messages if any
241 wxLog
*pLog
= wxLog::GetActiveTarget();
242 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
245 // Send OnIdle events to all windows
246 bool needMore
= SendIdleEvents();
249 event
.RequestMore(TRUE
);
254 // Send idle event to all top-level windows
255 bool wxApp::SendIdleEvents()
257 bool needMore
= FALSE
;
259 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
262 wxWindow
* win
= node
->GetData();
263 if (SendIdleEvents(win
))
265 node
= node
->GetNext();
271 // Send idle event to window and all subwindows
272 bool wxApp::SendIdleEvents(wxWindow
* win
)
274 bool needMore
= FALSE
;
277 event
.SetEventObject(win
);
278 win
->GetEventHandler()->ProcessEvent(event
);
280 if (event
.MoreRequested())
283 wxWindowList::Node
* node
= win
->GetChildren().GetFirst();
286 wxWindow
* win
= node
->GetData();
287 if (SendIdleEvents(win
))
290 node
= node
->GetNext();
295 static char *fallbackResources
[] = {
296 "*menuBar.marginHeight: 0",
297 "*menuBar.shadowThickness: 1",
298 "*background: #c0c0c0",
299 "*foreground: black",
303 // Create an application context
304 bool wxApp::OnInitGui()
306 if( !wxAppBase::OnInitGui() )
309 XtToolkitInitialize() ;
310 wxTheApp
->m_appContext
= (WXAppContext
) XtCreateApplicationContext();
311 XtAppSetFallbackResources((XtAppContext
) wxTheApp
->m_appContext
, fallbackResources
);
313 Display
*dpy
= XtOpenDisplay((XtAppContext
) wxTheApp
->m_appContext
,(String
)NULL
,NULL
,
314 wxTheApp
->GetClassName().c_str(), NULL
, 0,
315 # if XtSpecificationRelease < 5
323 // if you don't log to stderr, nothing will be shown...
324 delete wxLog::SetActiveTarget(new wxLogStderr
);
325 wxString
className(wxTheApp
->GetClassName());
326 wxLogError(_("wxWindows could not open display for '%s': exiting."),
330 m_initialDisplay
= (WXDisplay
*) dpy
;
333 // install the X error handler
334 gs_pfnXErrorHandler
= XSetErrorHandler(wxXErrorHandler
);
335 #endif // __WXDEBUG__
337 // Add general resize proc
339 rec
.string
= "resize";
340 rec
.proc
= (XtActionProc
)wxWidgetResizeProc
;
341 XtAppAddActions((XtAppContext
) wxTheApp
->m_appContext
, &rec
, 1);
343 GetMainColormap(dpy
);
350 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
352 if (!display
) /* Must be called first with non-NULL display */
353 return m_mainColormap
;
355 int defaultScreen
= DefaultScreen((Display
*) display
);
356 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
358 Colormap c
= DefaultColormapOfScreen(screen
);
361 m_mainColormap
= (WXColormap
) c
;
363 return (WXColormap
) c
;
366 wxXVisualInfo
* wxApp::GetVisualInfo( WXDisplay
* display
)
368 wxPerDisplayDataMap::iterator it
= m_perDisplayData
->find( display
);
370 if( it
!= m_perDisplayData
->end() && it
->second
.m_visualInfo
)
371 return it
->second
.m_visualInfo
;
373 wxXVisualInfo
* vi
= new wxXVisualInfo
;
374 wxFillXVisualInfo( vi
, (Display
*)display
);
376 (*m_perDisplayData
)[display
].m_visualInfo
= vi
;
381 static void wxTLWidgetDestroyCallback(Widget w
, XtPointer clientData
,
385 wxTheApp
->SetTopLevelWidget( (WXDisplay
*)XtDisplay(w
),
389 WXWidget
wxCreateTopLevelWidget( WXDisplay
* display
)
391 Widget tlw
= XtAppCreateShell( (String
)NULL
,
392 wxTheApp
->GetClassName().c_str(),
393 applicationShellWidgetClass
,
396 XtSetMappedWhenManaged( tlw
, False
);
397 XtRealizeWidget( tlw
);
399 XtAddCallback( tlw
, XmNdestroyCallback
,
400 (XtCallbackProc
)wxTLWidgetDestroyCallback
,
403 return (WXWidget
)tlw
;
406 WXWidget
wxApp::GetTopLevelWidget()
408 WXDisplay
* display
= wxGetDisplay();
409 wxPerDisplayDataMap::iterator it
= m_perDisplayData
->find( display
);
411 if( it
!= m_perDisplayData
->end() && it
->second
.m_topLevelWidget
)
412 return (WXWidget
)it
->second
.m_topLevelWidget
;
414 WXWidget tlw
= wxCreateTopLevelWidget( display
);
415 SetTopLevelWidget( display
, tlw
);
420 void wxApp::SetTopLevelWidget(WXDisplay
* display
, WXWidget widget
)
422 (*m_perDisplayData
)[display
].m_topLevelWidget
= (Widget
)widget
;
425 // Yield to other processes
427 bool wxApp::Yield(bool onlyIfNeeded
)
429 bool s_inYield
= FALSE
;
435 wxFAIL_MSG( wxT("wxYield called recursively" ) );
443 while (wxTheApp
&& wxTheApp
->Pending())
444 wxTheApp
->Dispatch();
451 // ----------------------------------------------------------------------------
452 // accessors for C modules
453 // ----------------------------------------------------------------------------
455 extern "C" XtAppContext
wxGetAppContext()
457 return (XtAppContext
)wxTheApp
->GetAppContext();