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 WX_DECLARE_VOIDPTR_HASH_MAP( wxXVisualInfo
*, wxXVisualInfoMap
);
53 extern wxList wxPendingDelete
;
54 extern bool wxAddIdleCallback();
56 wxApp
*wxTheApp
= NULL
;
58 wxHashTable
*wxWidgetHashTable
= NULL
;
60 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
62 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
63 EVT_IDLE(wxApp::OnIdle
)
67 typedef int (*XErrorHandlerFunc
)(Display
*, XErrorEvent
*);
69 XErrorHandlerFunc gs_pfnXErrorHandler
= 0;
71 static int wxXErrorHandler(Display
*dpy
, XErrorEvent
*xevent
)
73 // just forward to the default handler for now
74 return gs_pfnXErrorHandler(dpy
, xevent
);
78 long wxApp::sm_lastMessageTime
= 0;
80 bool wxApp::Initialize()
82 wxClassInfo::InitializeClasses();
84 // GL: I'm annoyed ... I don't know where to put this and I don't want to
85 // create a module for that as it's part of the core.
87 wxPendingEventsLocker
= new wxCriticalSection();
90 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
91 wxTheColourDatabase
->Initialize();
93 wxInitializeStockLists();
94 wxInitializeStockObjects();
96 wxWidgetHashTable
= new wxHashTable(wxKEY_INTEGER
);
98 wxModule::RegisterModules();
99 if (!wxModule::InitializeModules()) return FALSE
;
104 void wxApp::CleanUp()
106 wxModule::CleanUpModules();
108 wxDeleteStockObjects() ;
110 // Destroy all GDI lists, etc.
112 wxDeleteStockLists();
114 delete wxTheColourDatabase
;
115 wxTheColourDatabase
= NULL
;
117 wxClassInfo::CleanUpClasses();
122 delete wxWidgetHashTable
;
123 wxWidgetHashTable
= NULL
;
125 // GL: I'm annoyed ... I don't know where to put this and I don't want to
126 // create a module for that as it's part of the core.
128 delete wxPendingEvents
;
129 wxPendingEvents
= NULL
;
130 delete wxPendingEventsLocker
;
131 wxPendingEventsLocker
= NULL
;
134 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
135 // At this point we want to check if there are any memory
136 // blocks that aren't part of the wxDebugContext itself,
137 // as a special case. Then when dumping we need to ignore
138 // wxDebugContext, too.
139 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
141 wxLogDebug("There were memory leaks.\n");
142 wxDebugContext::Dump();
143 wxDebugContext::PrintStatistics();
147 // do it as the very last thing because everything else can log messages
148 wxLog::DontCreateOnDemand();
149 // do it as the very last thing because everything else can log messages
150 delete wxLog::SetActiveTarget(NULL
);
153 // ============================================================================
155 // ============================================================================
157 int wxEntryStart( int argc
, char* argv
[] )
159 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
160 // This seems to be necessary since there are 'rogue'
161 // objects present at this point (perhaps global objects?)
162 // Setting a checkpoint will ignore them as far as the
163 // memory checking facility is concerned.
164 // Of course you may argue that memory allocated in globals should be
165 // checked, but this is a reasonable compromise.
166 wxDebugContext::SetCheckpoint();
169 if (!wxApp::Initialize())
179 // GUI-specific initialization, such as creating an app context.
180 if (!wxTheApp
->OnInitGui())
186 void wxEntryCleanup()
188 // So dialog boxes aren't used for further messages
189 delete wxLog::SetActiveTarget(new wxLogStderr
);
191 // flush the logged messages if any
192 wxLog
*pLog
= wxLog::GetActiveTarget();
193 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
199 int wxEntry( int argc
, char *argv
[] )
203 retValue
= wxEntryStart( argc
, argv
);
204 if (retValue
) return retValue
;
208 if (!wxApp::GetInitializerFunction())
210 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
214 wxTheApp
= (wxApp
*) (* wxApp::GetInitializerFunction()) ();
219 printf( "wxWindows error: wxTheApp == NULL\n" );
223 wxTheApp
->SetClassName(wxFileNameFromPath(argv
[0]));
224 wxTheApp
->SetAppName(wxFileNameFromPath(argv
[0]));
226 wxTheApp
->argc
= argc
;
227 wxTheApp
->argv
= argv
;
229 // GUI-specific initialization, such as creating an app context.
230 retValue
= wxEntryInitGui();
231 if (retValue
) return retValue
;
233 // Here frames insert themselves automatically into wxTopLevelWindows by
234 // getting created in OnInit().
236 if (wxTheApp
->OnInit())
238 if (wxTheApp
->Initialized())
242 if (wxTheApp
->GetTopWindow())
244 delete wxTheApp
->GetTopWindow();
245 wxTheApp
->SetTopWindow(NULL
);
248 wxTheApp
->DeletePendingObjects();
250 retValue
= wxTheApp
->OnExit();
257 // Static member initialization
258 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
265 m_eventLoop
= new wxEventLoop
;
266 m_mainColormap
= (WXColormap
) NULL
;
267 m_appContext
= (WXAppContext
) NULL
;
268 m_topLevelWidget
= (WXWidget
) NULL
;
269 m_maxRequestSize
= 0;
270 m_initialDisplay
= (WXDisplay
*) 0;
271 m_visualInfoMap
= new wxXVisualInfoMap
;
278 for( wxXVisualInfoMap::iterator it
= m_visualInfoMap
->begin(),
279 end
= m_visualInfoMap
->end();
285 delete m_visualInfoMap
;
288 bool wxApp::Initialized()
296 int wxApp::MainLoop()
299 * Sit around forever waiting to process X-events. Property Change
300 * event are handled special, because they have to refer to
301 * the root window rather than to a widget. therefore we can't
302 * use an Xt-eventhandler.
305 XSelectInput(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()),
306 XDefaultRootWindow(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget())),
314 // Processes an idle event.
315 // Returns TRUE if more time is needed.
316 bool wxApp::ProcessIdle()
320 return ProcessEvent(event
) && event
.MoreRequested();
323 void wxApp::ExitMainLoop()
325 if( m_eventLoop
->IsRunning() )
329 // Is a message/event pending?
330 bool wxApp::Pending()
332 return m_eventLoop
->Pending();
334 XFlush(XtDisplay( (Widget
) wxTheApp
->GetTopLevelWidget() ));
336 // Fix by Doug from STI, to prevent a stall if non-X event
338 return ((XtAppPending( (XtAppContext
) GetAppContext() ) & XtIMXEvent
) != 0) ;
342 // Dispatch a message.
343 void wxApp::Dispatch()
345 m_eventLoop
->Dispatch();
348 // This should be redefined in a derived class for
349 // handling property change events for XAtom IPC.
350 void wxApp::HandlePropertyChange(WXEvent
*event
)
352 // by default do nothing special
353 XtDispatchEvent((XEvent
*) event
); /* let Motif do the work */
356 void wxApp::OnIdle(wxIdleEvent
& event
)
358 static bool inOnIdle
= FALSE
;
360 // Avoid recursion (via ProcessEvent default case)
366 // If there are pending events, we must process them: pending events
367 // are either events to the threads other than main or events posted
368 // with wxPostEvent() functions
369 // GRG: I have moved this here so that all pending events are processed
370 // before starting to delete any objects. This behaves better (in
371 // particular, wrt wxPostEvent) and is coherent with wxGTK's current
372 // behaviour. Also removed the '#if wxUSE_THREADS' around it.
373 // Changed Mar/2000 before 2.1.14
375 // Flush pending events.
376 ProcessPendingEvents();
378 // 'Garbage' collection of windows deleted with Close().
379 DeletePendingObjects();
381 // flush the logged messages if any
382 wxLog
*pLog
= wxLog::GetActiveTarget();
383 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
386 // Send OnIdle events to all windows
387 bool needMore
= SendIdleEvents();
390 event
.RequestMore(TRUE
);
395 // Send idle event to all top-level windows
396 bool wxApp::SendIdleEvents()
398 bool needMore
= FALSE
;
400 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
403 wxWindow
* win
= node
->GetData();
404 if (SendIdleEvents(win
))
406 node
= node
->GetNext();
412 // Send idle event to window and all subwindows
413 bool wxApp::SendIdleEvents(wxWindow
* win
)
415 bool needMore
= FALSE
;
418 event
.SetEventObject(win
);
419 win
->GetEventHandler()->ProcessEvent(event
);
421 if (event
.MoreRequested())
424 wxWindowList::Node
* node
= win
->GetChildren().GetFirst();
427 wxWindow
* win
= node
->GetData();
428 if (SendIdleEvents(win
))
431 node
= node
->GetNext();
436 void wxApp::DeletePendingObjects()
438 wxList::Node
*node
= wxPendingDelete
.GetFirst();
441 wxObject
*obj
= node
->GetData();
445 if (wxPendingDelete
.Member(obj
))
448 // Deleting one object may have deleted other pending
449 // objects, so start from beginning of list again.
450 node
= wxPendingDelete
.GetFirst();
454 static char *fallbackResources
[] = {
455 "*menuBar.marginHeight: 0",
456 "*menuBar.shadowThickness: 1",
457 "*background: #c0c0c0",
458 "*foreground: black",
462 // Create an application context
463 bool wxApp::OnInitGui()
465 if( !wxAppBase::OnInitGui() )
468 XtToolkitInitialize() ;
469 wxTheApp
->m_appContext
= (WXAppContext
) XtCreateApplicationContext();
470 XtAppSetFallbackResources((XtAppContext
) wxTheApp
->m_appContext
, fallbackResources
);
472 Display
*dpy
= XtOpenDisplay((XtAppContext
) wxTheApp
->m_appContext
,(String
)NULL
,NULL
,
473 wxTheApp
->GetClassName().c_str(), NULL
, 0,
474 # if XtSpecificationRelease < 5
482 // if you don't log to stderr, nothing will be shown...
483 delete wxLog::SetActiveTarget(new wxLogStderr
);
484 wxString
className(wxTheApp
->GetClassName());
485 wxLogError(_("wxWindows could not open display for '%s': exiting."),
489 m_initialDisplay
= (WXDisplay
*) dpy
;
492 // install the X error handler
493 gs_pfnXErrorHandler
= XSetErrorHandler(wxXErrorHandler
);
494 #endif // __WXDEBUG__
496 wxTheApp
->m_topLevelWidget
=
497 (WXWidget
) XtAppCreateShell((String
)NULL
,
498 wxTheApp
->GetClassName().c_str(),
499 applicationShellWidgetClass
,dpy
,
502 // Add general resize proc
504 rec
.string
= "resize";
505 rec
.proc
= (XtActionProc
)wxWidgetResizeProc
;
506 XtAppAddActions((XtAppContext
) wxTheApp
->m_appContext
, &rec
, 1);
508 GetMainColormap(dpy
);
509 m_maxRequestSize
= XMaxRequestSize((Display
*) dpy
);
516 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
518 if (!display
) /* Must be called first with non-NULL display */
519 return m_mainColormap
;
521 int defaultScreen
= DefaultScreen((Display
*) display
);
522 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
524 Colormap c
= DefaultColormapOfScreen(screen
);
527 m_mainColormap
= (WXColormap
) c
;
529 return (WXColormap
) c
;
532 wxXVisualInfo
* wxApp::GetVisualInfo( WXDisplay
* display
)
534 wxXVisualInfoMap::iterator it
= m_visualInfoMap
->find( display
);
536 if( it
!= m_visualInfoMap
->end() ) return it
->second
;
538 wxXVisualInfo
* vi
= new wxXVisualInfo
;
539 wxFillXVisualInfo( vi
, (Display
*)display
);
541 (*m_visualInfoMap
)[display
] = vi
;
550 retValue
= wxTheApp
->OnExit();
554 * Exit in some platform-specific way.
555 * Not recommended that the app calls this:
556 * only for emergencies.
561 // Yield to other processes
563 bool wxApp::Yield(bool onlyIfNeeded
)
565 bool s_inYield
= FALSE
;
571 wxFAIL_MSG( wxT("wxYield called recursively" ) );
579 while (wxTheApp
&& wxTheApp
->Pending())
580 wxTheApp
->Dispatch();
587 // ----------------------------------------------------------------------------
588 // accessors for C modules
589 // ----------------------------------------------------------------------------
591 extern "C" XtAppContext
wxGetAppContext()
593 return (XtAppContext
)wxTheApp
->GetAppContext();