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
24 #include "wx/gdicmn.h"
27 #include "wx/cursor.h"
29 #include "wx/palette.h"
31 #include "wx/dialog.h"
32 #include "wx/msgdlg.h"
34 #include "wx/module.h"
35 #include "wx/memory.h"
38 #include "wx/evtloop.h"
42 #include "wx/thread.h"
46 #pragma message disable nosimpint
50 #include <X11/Xutil.h>
51 #include <X11/Xresource.h>
52 #include <X11/Xatom.h>
54 #pragma message enable nosimpint
57 #include "wx/motif/private.h"
61 extern wxList wxPendingDelete
;
62 extern bool wxAddIdleCallback();
64 wxApp
*wxTheApp
= NULL
;
66 wxHashTable
*wxWidgetHashTable
= NULL
;
68 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
70 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
71 EVT_IDLE(wxApp::OnIdle
)
75 typedef int (*XErrorHandlerFunc
)(Display
*, XErrorEvent
*);
77 XErrorHandlerFunc gs_pfnXErrorHandler
= 0;
79 static int wxXErrorHandler(Display
*dpy
, XErrorEvent
*xevent
)
81 // just forward to the default handler for now
82 return gs_pfnXErrorHandler(dpy
, xevent
);
86 long wxApp::sm_lastMessageTime
= 0;
88 bool wxApp::Initialize()
90 wxClassInfo::InitializeClasses();
92 // GL: I'm annoyed ... I don't know where to put this and I don't want to
93 // create a module for that as it's part of the core.
95 wxPendingEventsLocker
= new wxCriticalSection();
98 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
99 wxTheColourDatabase
->Initialize();
101 wxInitializeStockLists();
102 wxInitializeStockObjects();
104 wxWidgetHashTable
= new wxHashTable(wxKEY_INTEGER
);
106 wxModule::RegisterModules();
107 if (!wxModule::InitializeModules()) return FALSE
;
112 void wxApp::CleanUp()
114 delete wxWidgetHashTable
;
115 wxWidgetHashTable
= NULL
;
117 wxModule::CleanUpModules();
119 wxDeleteStockObjects() ;
121 // Destroy all GDI lists, etc.
123 wxDeleteStockLists();
125 delete wxTheColourDatabase
;
126 wxTheColourDatabase
= NULL
;
128 wxClassInfo::CleanUpClasses();
133 // GL: I'm annoyed ... I don't know where to put this and I don't want to
134 // create a module for that as it's part of the core.
136 delete wxPendingEvents
;
137 delete wxPendingEventsLocker
;
140 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
141 // At this point we want to check if there are any memory
142 // blocks that aren't part of the wxDebugContext itself,
143 // as a special case. Then when dumping we need to ignore
144 // wxDebugContext, too.
145 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
147 wxLogDebug("There were memory leaks.\n");
148 wxDebugContext::Dump();
149 wxDebugContext::PrintStatistics();
153 // do it as the very last thing because everything else can log messages
154 wxLog::DontCreateOnDemand();
155 // do it as the very last thing because everything else can log messages
156 delete wxLog::SetActiveTarget(NULL
);
159 int wxEntry( int argc
, char *argv
[] )
161 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
162 // This seems to be necessary since there are 'rogue'
163 // objects present at this point (perhaps global objects?)
164 // Setting a checkpoint will ignore them as far as the
165 // memory checking facility is concerned.
166 // Of course you may argue that memory allocated in globals should be
167 // checked, but this is a reasonable compromise.
168 wxDebugContext::SetCheckpoint();
171 if (!wxApp::Initialize())
176 if (!wxApp::GetInitializerFunction())
178 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
182 wxTheApp
= (wxApp
*) (* wxApp::GetInitializerFunction()) ();
187 printf( "wxWindows error: wxTheApp == NULL\n" );
191 wxTheApp
->SetClassName(wxFileNameFromPath(argv
[0]));
192 wxTheApp
->SetAppName(wxFileNameFromPath(argv
[0]));
194 wxTheApp
->argc
= argc
;
195 wxTheApp
->argv
= argv
;
197 // GUI-specific initialization, such as creating an app context.
198 wxTheApp
->OnInitGui();
200 // Here frames insert themselves automatically into wxTopLevelWindows by
201 // getting created in OnInit().
204 if (wxTheApp
->OnInit())
206 if (wxTheApp
->Initialized()) retValue
= wxTheApp
->OnRun();
209 // flush the logged messages if any
210 wxLog
*pLog
= wxLog::GetActiveTarget();
211 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
214 // So dialog boxes aren't used for further messages
215 delete wxLog::SetActiveTarget(new wxLogStderr
);
217 if (wxTheApp
->GetTopWindow())
219 delete wxTheApp
->GetTopWindow();
220 wxTheApp
->SetTopWindow(NULL
);
223 wxTheApp
->DeletePendingObjects();
232 // Static member initialization
233 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
240 m_eventLoop
= new wxEventLoop
;
241 m_mainColormap
= (WXColormap
) NULL
;
242 m_appContext
= (WXAppContext
) NULL
;
243 m_topLevelWidget
= (WXWidget
) NULL
;
244 m_maxRequestSize
= 0;
245 m_initialDisplay
= (WXDisplay
*) 0;
253 bool wxApp::Initialized()
261 int wxApp::MainLoop()
264 * Sit around forever waiting to process X-events. Property Change
265 * event are handled special, because they have to refer to
266 * the root window rather than to a widget. therefore we can't
267 * use an Xt-eventhandler.
270 XSelectInput(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()),
271 XDefaultRootWindow(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget())),
279 // Processes an idle event.
280 // Returns TRUE if more time is needed.
281 bool wxApp::ProcessIdle()
285 return ProcessEvent(event
) && event
.MoreRequested();
288 void wxApp::ExitMainLoop()
293 // Is a message/event pending?
294 bool wxApp::Pending()
296 return m_eventLoop
->Pending();
298 XFlush(XtDisplay( (Widget
) wxTheApp
->GetTopLevelWidget() ));
300 // Fix by Doug from STI, to prevent a stall if non-X event
302 return ((XtAppPending( (XtAppContext
) GetAppContext() ) & XtIMXEvent
) != 0) ;
306 // Dispatch a message.
307 void wxApp::Dispatch()
309 m_eventLoop
->Dispatch();
312 // This should be redefined in a derived class for
313 // handling property change events for XAtom IPC.
314 void wxApp::HandlePropertyChange(WXEvent
*event
)
316 // by default do nothing special
317 XtDispatchEvent((XEvent
*) event
); /* let Motif do the work */
320 void wxApp::OnIdle(wxIdleEvent
& event
)
322 static bool inOnIdle
= FALSE
;
324 // Avoid recursion (via ProcessEvent default case)
330 // If there are pending events, we must process them: pending events
331 // are either events to the threads other than main or events posted
332 // with wxPostEvent() functions
333 // GRG: I have moved this here so that all pending events are processed
334 // before starting to delete any objects. This behaves better (in
335 // particular, wrt wxPostEvent) and is coherent with wxGTK's current
336 // behaviour. Also removed the '#if wxUSE_THREADS' around it.
337 // Changed Mar/2000 before 2.1.14
339 // Flush pending events.
340 ProcessPendingEvents();
342 // 'Garbage' collection of windows deleted with Close().
343 DeletePendingObjects();
345 // flush the logged messages if any
346 wxLog
*pLog
= wxLog::GetActiveTarget();
347 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
350 // Send OnIdle events to all windows
351 bool needMore
= SendIdleEvents();
354 event
.RequestMore(TRUE
);
359 // Send idle event to all top-level windows
360 bool wxApp::SendIdleEvents()
362 bool needMore
= FALSE
;
364 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
367 wxWindow
* win
= node
->GetData();
368 if (SendIdleEvents(win
))
370 node
= node
->GetNext();
376 // Send idle event to window and all subwindows
377 bool wxApp::SendIdleEvents(wxWindow
* win
)
379 bool needMore
= FALSE
;
382 event
.SetEventObject(win
);
383 win
->ProcessEvent(event
);
385 if (event
.MoreRequested())
388 wxWindowList::Node
* node
= win
->GetChildren().GetFirst();
391 wxWindow
* win
= node
->GetData();
392 if (SendIdleEvents(win
))
395 node
= node
->GetNext();
400 void wxApp::DeletePendingObjects()
402 wxList::Node
*node
= wxPendingDelete
.GetFirst();
405 wxObject
*obj
= node
->GetData();
409 if (wxPendingDelete
.Member(obj
))
412 // Deleting one object may have deleted other pending
413 // objects, so start from beginning of list again.
414 node
= wxPendingDelete
.GetFirst();
418 static char *fallbackResources
[] = {
419 "*menuBar.marginHeight: 0",
420 "*menuBar.shadowThickness: 1",
421 "*background: #c0c0c0",
422 "*foreground: black",
426 // Create an application context
427 bool wxApp::OnInitGui()
429 if( !wxAppBase::OnInitGui() )
432 XtToolkitInitialize() ;
433 wxTheApp
->m_appContext
= (WXAppContext
) XtCreateApplicationContext();
434 XtAppSetFallbackResources((XtAppContext
) wxTheApp
->m_appContext
, fallbackResources
);
436 Display
*dpy
= XtOpenDisplay((XtAppContext
) wxTheApp
->m_appContext
,(String
)NULL
,NULL
,
437 wxTheApp
->GetClassName().c_str(), NULL
, 0,
438 # if XtSpecificationRelease < 5
446 // if you don't log to stderr, nothing will be shown...
447 delete wxLog::SetActiveTarget(new wxLogStderr
);
448 wxString
className(wxTheApp
->GetClassName());
449 wxLogError(_("wxWindows could not open display for '%s': exiting."),
453 m_initialDisplay
= (WXDisplay
*) dpy
;
456 // install the X error handler
457 gs_pfnXErrorHandler
= XSetErrorHandler(wxXErrorHandler
);
458 #endif // __WXDEBUG__
460 wxTheApp
->m_topLevelWidget
=
461 (WXWidget
) XtAppCreateShell((String
)NULL
,
462 wxTheApp
->GetClassName().c_str(),
463 applicationShellWidgetClass
,dpy
,
466 // Add general resize proc
468 rec
.string
= "resize";
469 rec
.proc
= (XtActionProc
)wxWidgetResizeProc
;
470 XtAppAddActions((XtAppContext
) wxTheApp
->m_appContext
, &rec
, 1);
472 GetMainColormap(dpy
);
473 m_maxRequestSize
= XMaxRequestSize((Display
*) dpy
);
480 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
482 if (!display
) /* Must be called first with non-NULL display */
483 return m_mainColormap
;
485 int defaultScreen
= DefaultScreen((Display
*) display
);
486 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
488 Colormap c
= DefaultColormapOfScreen(screen
);
491 m_mainColormap
= (WXColormap
) c
;
493 return (WXColormap
) c
;
500 retValue
= wxTheApp
->OnExit();
504 * Exit in some platform-specific way.
505 * Not recommended that the app calls this:
506 * only for emergencies.
511 // Yield to other processes
513 bool wxApp::Yield(bool onlyIfNeeded
)
515 bool s_inYield
= FALSE
;
521 wxFAIL_MSG( wxT("wxYield called recursively" ) );
529 while (wxTheApp
&& wxTheApp
->Pending())
530 wxTheApp
->Dispatch();
537 // ----------------------------------------------------------------------------
538 // accessors for C modules
539 // ----------------------------------------------------------------------------
541 extern "C" XtAppContext
wxGetAppContext()
543 return (XtAppContext
)wxTheApp
->GetAppContext();