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 delete wxPendingEventsLocker
;
132 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
133 // At this point we want to check if there are any memory
134 // blocks that aren't part of the wxDebugContext itself,
135 // as a special case. Then when dumping we need to ignore
136 // wxDebugContext, too.
137 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
139 wxLogDebug("There were memory leaks.\n");
140 wxDebugContext::Dump();
141 wxDebugContext::PrintStatistics();
145 // do it as the very last thing because everything else can log messages
146 wxLog::DontCreateOnDemand();
147 // do it as the very last thing because everything else can log messages
148 delete wxLog::SetActiveTarget(NULL
);
151 // ============================================================================
153 // ============================================================================
155 int wxEntryStart( int argc
, char* argv
[] )
157 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
158 // This seems to be necessary since there are 'rogue'
159 // objects present at this point (perhaps global objects?)
160 // Setting a checkpoint will ignore them as far as the
161 // memory checking facility is concerned.
162 // Of course you may argue that memory allocated in globals should be
163 // checked, but this is a reasonable compromise.
164 wxDebugContext::SetCheckpoint();
167 if (!wxApp::Initialize())
177 // GUI-specific initialization, such as creating an app context.
178 if (!wxTheApp
->OnInitGui())
184 void wxEntryCleanup()
186 // So dialog boxes aren't used for further messages
187 delete wxLog::SetActiveTarget(new wxLogStderr
);
189 // flush the logged messages if any
190 wxLog
*pLog
= wxLog::GetActiveTarget();
191 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
197 int wxEntry( int argc
, char *argv
[] )
201 retValue
= wxEntryStart( argc
, argv
);
202 if (retValue
) return retValue
;
206 if (!wxApp::GetInitializerFunction())
208 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
212 wxTheApp
= (wxApp
*) (* wxApp::GetInitializerFunction()) ();
217 printf( "wxWindows error: wxTheApp == NULL\n" );
221 wxTheApp
->SetClassName(wxFileNameFromPath(argv
[0]));
222 wxTheApp
->SetAppName(wxFileNameFromPath(argv
[0]));
224 wxTheApp
->argc
= argc
;
225 wxTheApp
->argv
= argv
;
227 // GUI-specific initialization, such as creating an app context.
228 retValue
= wxEntryInitGui();
229 if (retValue
) return retValue
;
231 // Here frames insert themselves automatically into wxTopLevelWindows by
232 // getting created in OnInit().
234 if (wxTheApp
->OnInit())
236 if (wxTheApp
->Initialized())
240 if (wxTheApp
->GetTopWindow())
242 delete wxTheApp
->GetTopWindow();
243 wxTheApp
->SetTopWindow(NULL
);
246 wxTheApp
->DeletePendingObjects();
248 retValue
= wxTheApp
->OnExit();
255 // Static member initialization
256 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
263 m_eventLoop
= new wxEventLoop
;
264 m_mainColormap
= (WXColormap
) NULL
;
265 m_appContext
= (WXAppContext
) NULL
;
266 m_topLevelWidget
= (WXWidget
) NULL
;
267 m_maxRequestSize
= 0;
268 m_initialDisplay
= (WXDisplay
*) 0;
269 m_visualInfoMap
= new wxXVisualInfoMap
;
276 for( wxXVisualInfoMap::iterator it
= m_visualInfoMap
->begin(),
277 end
= m_visualInfoMap
->end();
283 delete m_visualInfoMap
;
286 bool wxApp::Initialized()
294 int wxApp::MainLoop()
297 * Sit around forever waiting to process X-events. Property Change
298 * event are handled special, because they have to refer to
299 * the root window rather than to a widget. therefore we can't
300 * use an Xt-eventhandler.
303 XSelectInput(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()),
304 XDefaultRootWindow(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget())),
312 // Processes an idle event.
313 // Returns TRUE if more time is needed.
314 bool wxApp::ProcessIdle()
318 return ProcessEvent(event
) && event
.MoreRequested();
321 void wxApp::ExitMainLoop()
323 if( m_eventLoop
->IsRunning() )
327 // Is a message/event pending?
328 bool wxApp::Pending()
330 return m_eventLoop
->Pending();
332 XFlush(XtDisplay( (Widget
) wxTheApp
->GetTopLevelWidget() ));
334 // Fix by Doug from STI, to prevent a stall if non-X event
336 return ((XtAppPending( (XtAppContext
) GetAppContext() ) & XtIMXEvent
) != 0) ;
340 // Dispatch a message.
341 void wxApp::Dispatch()
343 m_eventLoop
->Dispatch();
346 // This should be redefined in a derived class for
347 // handling property change events for XAtom IPC.
348 void wxApp::HandlePropertyChange(WXEvent
*event
)
350 // by default do nothing special
351 XtDispatchEvent((XEvent
*) event
); /* let Motif do the work */
354 void wxApp::OnIdle(wxIdleEvent
& event
)
356 static bool inOnIdle
= FALSE
;
358 // Avoid recursion (via ProcessEvent default case)
364 // If there are pending events, we must process them: pending events
365 // are either events to the threads other than main or events posted
366 // with wxPostEvent() functions
367 // GRG: I have moved this here so that all pending events are processed
368 // before starting to delete any objects. This behaves better (in
369 // particular, wrt wxPostEvent) and is coherent with wxGTK's current
370 // behaviour. Also removed the '#if wxUSE_THREADS' around it.
371 // Changed Mar/2000 before 2.1.14
373 // Flush pending events.
374 ProcessPendingEvents();
376 // 'Garbage' collection of windows deleted with Close().
377 DeletePendingObjects();
379 // flush the logged messages if any
380 wxLog
*pLog
= wxLog::GetActiveTarget();
381 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
384 // Send OnIdle events to all windows
385 bool needMore
= SendIdleEvents();
388 event
.RequestMore(TRUE
);
393 // Send idle event to all top-level windows
394 bool wxApp::SendIdleEvents()
396 bool needMore
= FALSE
;
398 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
401 wxWindow
* win
= node
->GetData();
402 if (SendIdleEvents(win
))
404 node
= node
->GetNext();
410 // Send idle event to window and all subwindows
411 bool wxApp::SendIdleEvents(wxWindow
* win
)
413 bool needMore
= FALSE
;
416 event
.SetEventObject(win
);
417 win
->ProcessEvent(event
);
419 if (event
.MoreRequested())
422 wxWindowList::Node
* node
= win
->GetChildren().GetFirst();
425 wxWindow
* win
= node
->GetData();
426 if (SendIdleEvents(win
))
429 node
= node
->GetNext();
434 void wxApp::DeletePendingObjects()
436 wxList::Node
*node
= wxPendingDelete
.GetFirst();
439 wxObject
*obj
= node
->GetData();
443 if (wxPendingDelete
.Member(obj
))
446 // Deleting one object may have deleted other pending
447 // objects, so start from beginning of list again.
448 node
= wxPendingDelete
.GetFirst();
452 static char *fallbackResources
[] = {
453 "*menuBar.marginHeight: 0",
454 "*menuBar.shadowThickness: 1",
455 "*background: #c0c0c0",
456 "*foreground: black",
460 // Create an application context
461 bool wxApp::OnInitGui()
463 if( !wxAppBase::OnInitGui() )
466 XtToolkitInitialize() ;
467 wxTheApp
->m_appContext
= (WXAppContext
) XtCreateApplicationContext();
468 XtAppSetFallbackResources((XtAppContext
) wxTheApp
->m_appContext
, fallbackResources
);
470 Display
*dpy
= XtOpenDisplay((XtAppContext
) wxTheApp
->m_appContext
,(String
)NULL
,NULL
,
471 wxTheApp
->GetClassName().c_str(), NULL
, 0,
472 # if XtSpecificationRelease < 5
480 // if you don't log to stderr, nothing will be shown...
481 delete wxLog::SetActiveTarget(new wxLogStderr
);
482 wxString
className(wxTheApp
->GetClassName());
483 wxLogError(_("wxWindows could not open display for '%s': exiting."),
487 m_initialDisplay
= (WXDisplay
*) dpy
;
490 // install the X error handler
491 gs_pfnXErrorHandler
= XSetErrorHandler(wxXErrorHandler
);
492 #endif // __WXDEBUG__
494 wxTheApp
->m_topLevelWidget
=
495 (WXWidget
) XtAppCreateShell((String
)NULL
,
496 wxTheApp
->GetClassName().c_str(),
497 applicationShellWidgetClass
,dpy
,
500 // Add general resize proc
502 rec
.string
= "resize";
503 rec
.proc
= (XtActionProc
)wxWidgetResizeProc
;
504 XtAppAddActions((XtAppContext
) wxTheApp
->m_appContext
, &rec
, 1);
506 GetMainColormap(dpy
);
507 m_maxRequestSize
= XMaxRequestSize((Display
*) dpy
);
514 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
516 if (!display
) /* Must be called first with non-NULL display */
517 return m_mainColormap
;
519 int defaultScreen
= DefaultScreen((Display
*) display
);
520 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
522 Colormap c
= DefaultColormapOfScreen(screen
);
525 m_mainColormap
= (WXColormap
) c
;
527 return (WXColormap
) c
;
530 wxXVisualInfo
* wxApp::GetVisualInfo( WXDisplay
* display
)
532 wxXVisualInfoMap::iterator it
= m_visualInfoMap
->find( display
);
534 if( it
!= m_visualInfoMap
->end() ) return it
->second
;
536 wxXVisualInfo
* vi
= new wxXVisualInfo
;
537 wxFillXVisualInfo( vi
, (Display
*)display
);
539 (*m_visualInfoMap
)[display
] = vi
;
548 retValue
= wxTheApp
->OnExit();
552 * Exit in some platform-specific way.
553 * Not recommended that the app calls this:
554 * only for emergencies.
559 // Yield to other processes
561 bool wxApp::Yield(bool onlyIfNeeded
)
563 bool s_inYield
= FALSE
;
569 wxFAIL_MSG( wxT("wxYield called recursively" ) );
577 while (wxTheApp
&& wxTheApp
->Pending())
578 wxTheApp
->Dispatch();
585 // ----------------------------------------------------------------------------
586 // accessors for C modules
587 // ----------------------------------------------------------------------------
589 extern "C" XtAppContext
wxGetAppContext()
591 return (XtAppContext
)wxTheApp
->GetAppContext();