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"
40 #include "wx/hashmap.h"
43 #include "wx/thread.h"
47 #pragma message disable nosimpint
51 #include <X11/Xutil.h>
52 #include <X11/Xresource.h>
53 #include <X11/Xatom.h>
55 #pragma message enable nosimpint
58 #include "wx/motif/private.h"
62 WX_DECLARE_VOIDPTR_HASH_MAP( wxXVisualInfo
*, wxXVisualInfoMap
);
64 extern wxList wxPendingDelete
;
65 extern bool wxAddIdleCallback();
67 wxApp
*wxTheApp
= NULL
;
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 long wxApp::sm_lastMessageTime
= 0;
91 bool wxApp::Initialize()
93 wxClassInfo::InitializeClasses();
95 // GL: I'm annoyed ... I don't know where to put this and I don't want to
96 // create a module for that as it's part of the core.
98 wxPendingEventsLocker
= new wxCriticalSection();
101 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
102 wxTheColourDatabase
->Initialize();
104 wxInitializeStockLists();
105 wxInitializeStockObjects();
107 wxWidgetHashTable
= new wxHashTable(wxKEY_INTEGER
);
109 wxModule::RegisterModules();
110 if (!wxModule::InitializeModules()) return FALSE
;
115 void wxApp::CleanUp()
117 delete wxWidgetHashTable
;
118 wxWidgetHashTable
= NULL
;
120 wxModule::CleanUpModules();
122 wxDeleteStockObjects() ;
124 // Destroy all GDI lists, etc.
126 wxDeleteStockLists();
128 delete wxTheColourDatabase
;
129 wxTheColourDatabase
= NULL
;
131 wxClassInfo::CleanUpClasses();
136 // GL: I'm annoyed ... I don't know where to put this and I don't want to
137 // create a module for that as it's part of the core.
139 delete wxPendingEvents
;
140 delete wxPendingEventsLocker
;
143 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
144 // At this point we want to check if there are any memory
145 // blocks that aren't part of the wxDebugContext itself,
146 // as a special case. Then when dumping we need to ignore
147 // wxDebugContext, too.
148 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
150 wxLogDebug("There were memory leaks.\n");
151 wxDebugContext::Dump();
152 wxDebugContext::PrintStatistics();
156 // do it as the very last thing because everything else can log messages
157 wxLog::DontCreateOnDemand();
158 // do it as the very last thing because everything else can log messages
159 delete wxLog::SetActiveTarget(NULL
);
162 int wxEntry( int argc
, char *argv
[] )
164 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
165 // This seems to be necessary since there are 'rogue'
166 // objects present at this point (perhaps global objects?)
167 // Setting a checkpoint will ignore them as far as the
168 // memory checking facility is concerned.
169 // Of course you may argue that memory allocated in globals should be
170 // checked, but this is a reasonable compromise.
171 wxDebugContext::SetCheckpoint();
174 if (!wxApp::Initialize())
179 if (!wxApp::GetInitializerFunction())
181 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
185 wxTheApp
= (wxApp
*) (* wxApp::GetInitializerFunction()) ();
190 printf( "wxWindows error: wxTheApp == NULL\n" );
194 wxTheApp
->SetClassName(wxFileNameFromPath(argv
[0]));
195 wxTheApp
->SetAppName(wxFileNameFromPath(argv
[0]));
197 wxTheApp
->argc
= argc
;
198 wxTheApp
->argv
= argv
;
200 // GUI-specific initialization, such as creating an app context.
201 wxTheApp
->OnInitGui();
203 // Here frames insert themselves automatically into wxTopLevelWindows by
204 // getting created in OnInit().
207 if (wxTheApp
->OnInit())
209 if (wxTheApp
->Initialized()) retValue
= wxTheApp
->OnRun();
212 // flush the logged messages if any
213 wxLog
*pLog
= wxLog::GetActiveTarget();
214 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
217 // So dialog boxes aren't used for further messages
218 delete wxLog::SetActiveTarget(new wxLogStderr
);
220 if (wxTheApp
->GetTopWindow())
222 delete wxTheApp
->GetTopWindow();
223 wxTheApp
->SetTopWindow(NULL
);
226 wxTheApp
->DeletePendingObjects();
235 // Static member initialization
236 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
243 m_eventLoop
= new wxEventLoop
;
244 m_mainColormap
= (WXColormap
) NULL
;
245 m_appContext
= (WXAppContext
) NULL
;
246 m_topLevelWidget
= (WXWidget
) NULL
;
247 m_maxRequestSize
= 0;
248 m_initialDisplay
= (WXDisplay
*) 0;
249 m_visualInfoMap
= new wxXVisualInfoMap
;
256 for( wxXVisualInfoMap::iterator it
= m_visualInfoMap
->begin(),
257 end
= m_visualInfoMap
->end();
263 delete m_visualInfoMap
;
266 bool wxApp::Initialized()
274 int wxApp::MainLoop()
277 * Sit around forever waiting to process X-events. Property Change
278 * event are handled special, because they have to refer to
279 * the root window rather than to a widget. therefore we can't
280 * use an Xt-eventhandler.
283 XSelectInput(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()),
284 XDefaultRootWindow(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget())),
292 // Processes an idle event.
293 // Returns TRUE if more time is needed.
294 bool wxApp::ProcessIdle()
298 return ProcessEvent(event
) && event
.MoreRequested();
301 void wxApp::ExitMainLoop()
306 // Is a message/event pending?
307 bool wxApp::Pending()
310 return XtAppPending( (XtAppContext
) GetAppContext() ) != 0;
312 return m_eventLoop
->Pending();
314 XFlush(XtDisplay( (Widget
) wxTheApp
->GetTopLevelWidget() ));
316 // Fix by Doug from STI, to prevent a stall if non-X event
318 return ((XtAppPending( (XtAppContext
) GetAppContext() ) & XtIMXEvent
) != 0) ;
323 // Dispatch a message.
324 void wxApp::Dispatch()
326 m_eventLoop
->Dispatch();
329 // This should be redefined in a derived class for
330 // handling property change events for XAtom IPC.
331 void wxApp::HandlePropertyChange(WXEvent
*event
)
333 // by default do nothing special
334 XtDispatchEvent((XEvent
*) event
); /* let Motif do the work */
337 void wxApp::OnIdle(wxIdleEvent
& event
)
339 static bool inOnIdle
= FALSE
;
341 // Avoid recursion (via ProcessEvent default case)
347 // If there are pending events, we must process them: pending events
348 // are either events to the threads other than main or events posted
349 // with wxPostEvent() functions
350 // GRG: I have moved this here so that all pending events are processed
351 // before starting to delete any objects. This behaves better (in
352 // particular, wrt wxPostEvent) and is coherent with wxGTK's current
353 // behaviour. Also removed the '#if wxUSE_THREADS' around it.
354 // Changed Mar/2000 before 2.1.14
356 // Flush pending events.
357 ProcessPendingEvents();
359 // 'Garbage' collection of windows deleted with Close().
360 DeletePendingObjects();
362 // flush the logged messages if any
363 wxLog
*pLog
= wxLog::GetActiveTarget();
364 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
367 // Send OnIdle events to all windows
368 bool needMore
= SendIdleEvents();
371 event
.RequestMore(TRUE
);
376 // Send idle event to all top-level windows
377 bool wxApp::SendIdleEvents()
379 bool needMore
= FALSE
;
381 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
384 wxWindow
* win
= node
->GetData();
385 if (SendIdleEvents(win
))
387 node
= node
->GetNext();
393 // Send idle event to window and all subwindows
394 bool wxApp::SendIdleEvents(wxWindow
* win
)
396 bool needMore
= FALSE
;
399 event
.SetEventObject(win
);
400 win
->ProcessEvent(event
);
402 if (event
.MoreRequested())
405 wxWindowList::Node
* node
= win
->GetChildren().GetFirst();
408 wxWindow
* win
= node
->GetData();
409 if (SendIdleEvents(win
))
412 node
= node
->GetNext();
417 void wxApp::DeletePendingObjects()
419 wxList::Node
*node
= wxPendingDelete
.GetFirst();
422 wxObject
*obj
= node
->GetData();
426 if (wxPendingDelete
.Member(obj
))
429 // Deleting one object may have deleted other pending
430 // objects, so start from beginning of list again.
431 node
= wxPendingDelete
.GetFirst();
435 static char *fallbackResources
[] = {
436 "*menuBar.marginHeight: 0",
437 "*menuBar.shadowThickness: 1",
438 "*background: #c0c0c0",
439 "*foreground: black",
443 // Create an application context
444 bool wxApp::OnInitGui()
446 if( !wxAppBase::OnInitGui() )
449 XtToolkitInitialize() ;
450 wxTheApp
->m_appContext
= (WXAppContext
) XtCreateApplicationContext();
451 XtAppSetFallbackResources((XtAppContext
) wxTheApp
->m_appContext
, fallbackResources
);
453 Display
*dpy
= XtOpenDisplay((XtAppContext
) wxTheApp
->m_appContext
,(String
)NULL
,NULL
,
454 wxTheApp
->GetClassName().c_str(), NULL
, 0,
455 # if XtSpecificationRelease < 5
463 // if you don't log to stderr, nothing will be shown...
464 delete wxLog::SetActiveTarget(new wxLogStderr
);
465 wxString
className(wxTheApp
->GetClassName());
466 wxLogError(_("wxWindows could not open display for '%s': exiting."),
470 m_initialDisplay
= (WXDisplay
*) dpy
;
473 // install the X error handler
474 gs_pfnXErrorHandler
= XSetErrorHandler(wxXErrorHandler
);
475 #endif // __WXDEBUG__
477 wxTheApp
->m_topLevelWidget
=
478 (WXWidget
) XtAppCreateShell((String
)NULL
,
479 wxTheApp
->GetClassName().c_str(),
480 applicationShellWidgetClass
,dpy
,
483 // Add general resize proc
485 rec
.string
= "resize";
486 rec
.proc
= (XtActionProc
)wxWidgetResizeProc
;
487 XtAppAddActions((XtAppContext
) wxTheApp
->m_appContext
, &rec
, 1);
489 GetMainColormap(dpy
);
490 m_maxRequestSize
= XMaxRequestSize((Display
*) dpy
);
497 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
499 if (!display
) /* Must be called first with non-NULL display */
500 return m_mainColormap
;
502 int defaultScreen
= DefaultScreen((Display
*) display
);
503 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
505 Colormap c
= DefaultColormapOfScreen(screen
);
508 m_mainColormap
= (WXColormap
) c
;
510 return (WXColormap
) c
;
513 wxXVisualInfo
* wxApp::GetVisualInfo( WXDisplay
* display
)
515 wxXVisualInfoMap::iterator it
= m_visualInfoMap
->find( display
);
517 if( it
!= m_visualInfoMap
->end() ) return it
->second
;
519 wxXVisualInfo
* vi
= new wxXVisualInfo
;
520 wxFillXVisualInfo( vi
, (Display
*)display
);
522 (*m_visualInfoMap
)[display
] = vi
;
531 retValue
= wxTheApp
->OnExit();
535 * Exit in some platform-specific way.
536 * Not recommended that the app calls this:
537 * only for emergencies.
542 // Yield to other processes
544 bool wxApp::Yield(bool onlyIfNeeded
)
546 bool s_inYield
= FALSE
;
552 wxFAIL_MSG( wxT("wxYield called recursively" ) );
560 while (wxTheApp
&& wxTheApp
->Pending())
561 wxTheApp
->Dispatch();
568 // ----------------------------------------------------------------------------
569 // accessors for C modules
570 // ----------------------------------------------------------------------------
572 extern "C" XtAppContext
wxGetAppContext()
574 return (XtAppContext
)wxTheApp
->GetAppContext();