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"
45 #if wxUSE_WX_RESOURCES
46 #include "wx/resource.h"
50 #pragma message disable nosimpint
54 #include <X11/Xutil.h>
55 #include <X11/Xresource.h>
56 #include <X11/Xatom.h>
58 #pragma message enable nosimpint
61 #include "wx/motif/private.h"
65 extern wxList wxPendingDelete
;
66 extern bool wxAddIdleCallback();
68 wxApp
*wxTheApp
= NULL
;
70 wxHashTable
*wxWidgetHashTable
= NULL
;
72 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
74 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
75 EVT_IDLE(wxApp::OnIdle
)
79 typedef int (*XErrorHandlerFunc
)(Display
*, XErrorEvent
*);
81 XErrorHandlerFunc gs_pfnXErrorHandler
= 0;
83 static int wxXErrorHandler(Display
*dpy
, XErrorEvent
*xevent
)
85 // just forward to the default handler for now
86 return gs_pfnXErrorHandler(dpy
, xevent
);
90 long wxApp::sm_lastMessageTime
= 0;
92 bool wxApp::Initialize()
94 wxClassInfo::InitializeClasses();
96 // GL: I'm annoyed ... I don't know where to put this and I don't want to
97 // create a module for that as it's part of the core.
99 wxPendingEventsLocker
= new wxCriticalSection();
102 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
103 wxTheColourDatabase
->Initialize();
105 wxInitializeStockLists();
106 wxInitializeStockObjects();
108 #if wxUSE_WX_RESOURCES
109 wxInitializeResourceSystem();
112 // For PostScript printing
114 /* Done using wxModule now
115 wxInitializePrintSetupData();
116 wxThePrintPaperDatabase = new wxPrintPaperDatabase;
117 wxThePrintPaperDatabase->CreateDatabase();
121 wxBitmap::InitStandardHandlers();
123 wxWidgetHashTable
= new wxHashTable(wxKEY_INTEGER
);
125 wxModule::RegisterModules();
126 if (!wxModule::InitializeModules()) return FALSE
;
131 void wxApp::CleanUp()
133 delete wxWidgetHashTable
;
134 wxWidgetHashTable
= NULL
;
136 wxModule::CleanUpModules();
138 #if wxUSE_WX_RESOURCES
139 wxCleanUpResourceSystem();
142 wxDeleteStockObjects() ;
144 // Destroy all GDI lists, etc.
146 wxDeleteStockLists();
148 delete wxTheColourDatabase
;
149 wxTheColourDatabase
= NULL
;
152 /* Done using wxModule now
153 wxInitializePrintSetupData(FALSE);
154 delete wxThePrintPaperDatabase;
155 wxThePrintPaperDatabase = NULL;
159 wxBitmap::CleanUpHandlers();
161 wxClassInfo::CleanUpClasses();
166 // GL: I'm annoyed ... I don't know where to put this and I don't want to
167 // create a module for that as it's part of the core.
169 delete wxPendingEvents
;
170 delete wxPendingEventsLocker
;
173 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
174 // At this point we want to check if there are any memory
175 // blocks that aren't part of the wxDebugContext itself,
176 // as a special case. Then when dumping we need to ignore
177 // wxDebugContext, too.
178 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
180 wxLogDebug("There were memory leaks.\n");
181 wxDebugContext::Dump();
182 wxDebugContext::PrintStatistics();
186 // do it as the very last thing because everything else can log messages
187 wxLog::DontCreateOnDemand();
188 // do it as the very last thing because everything else can log messages
189 delete wxLog::SetActiveTarget(NULL
);
192 int wxEntry( int argc
, char *argv
[] )
194 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
195 // This seems to be necessary since there are 'rogue'
196 // objects present at this point (perhaps global objects?)
197 // Setting a checkpoint will ignore them as far as the
198 // memory checking facility is concerned.
199 // Of course you may argue that memory allocated in globals should be
200 // checked, but this is a reasonable compromise.
201 wxDebugContext::SetCheckpoint();
204 if (!wxApp::Initialize())
209 if (!wxApp::GetInitializerFunction())
211 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
215 wxTheApp
= (wxApp
*) (* wxApp::GetInitializerFunction()) ();
220 printf( "wxWindows error: wxTheApp == NULL\n" );
224 wxTheApp
->SetClassName(wxFileNameFromPath(argv
[0]));
225 wxTheApp
->SetAppName(wxFileNameFromPath(argv
[0]));
227 wxTheApp
->argc
= argc
;
228 wxTheApp
->argv
= argv
;
230 // GUI-specific initialization, such as creating an app context.
231 wxTheApp
->OnInitGui();
233 // Here frames insert themselves automatically into wxTopLevelWindows by
234 // getting created in OnInit().
237 if (wxTheApp
->OnInit())
239 if (wxTheApp
->Initialized()) retValue
= wxTheApp
->OnRun();
242 // flush the logged messages if any
243 wxLog
*pLog
= wxLog::GetActiveTarget();
244 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
247 delete wxLog::SetActiveTarget(new wxLogStderr
); // So dialog boxes aren't used
248 // for further messages
250 if (wxTheApp
->GetTopWindow())
252 delete wxTheApp
->GetTopWindow();
253 wxTheApp
->SetTopWindow(NULL
);
256 wxTheApp
->DeletePendingObjects();
265 // Static member initialization
266 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
273 m_eventLoop
= new wxEventLoop
;
274 m_mainColormap
= (WXColormap
) NULL
;
275 m_appContext
= (WXAppContext
) NULL
;
276 m_topLevelWidget
= (WXWidget
) NULL
;
277 m_maxRequestSize
= 0;
278 m_initialDisplay
= (WXDisplay
*) 0;
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()
326 // Is a message/event pending?
327 bool wxApp::Pending()
329 return m_eventLoop
->Pending();
331 XFlush(XtDisplay( (Widget
) wxTheApp
->GetTopLevelWidget() ));
333 // Fix by Doug from STI, to prevent a stall if non-X event
335 return ((XtAppPending( (XtAppContext
) GetAppContext() ) & XtIMXEvent
) != 0) ;
339 // Dispatch a message.
340 void wxApp::Dispatch()
342 m_eventLoop
->Dispatch();
345 // This should be redefined in a derived class for
346 // handling property change events for XAtom IPC.
347 void wxApp::HandlePropertyChange(WXEvent
*event
)
349 // by default do nothing special
350 XtDispatchEvent((XEvent
*) event
); /* let Motif do the work */
353 void wxApp::OnIdle(wxIdleEvent
& event
)
355 static bool inOnIdle
= FALSE
;
357 // Avoid recursion (via ProcessEvent default case)
363 // If there are pending events, we must process them: pending events
364 // are either events to the threads other than main or events posted
365 // with wxPostEvent() functions
366 // GRG: I have moved this here so that all pending events are processed
367 // before starting to delete any objects. This behaves better (in
368 // particular, wrt wxPostEvent) and is coherent with wxGTK's current
369 // behaviour. Also removed the '#if wxUSE_THREADS' around it.
370 // Changed Mar/2000 before 2.1.14
372 // Flush pending events.
373 ProcessPendingEvents();
375 // 'Garbage' collection of windows deleted with Close().
376 DeletePendingObjects();
378 // flush the logged messages if any
379 wxLog
*pLog
= wxLog::GetActiveTarget();
380 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
383 // Send OnIdle events to all windows
384 bool needMore
= SendIdleEvents();
387 event
.RequestMore(TRUE
);
392 // Send idle event to all top-level windows
393 bool wxApp::SendIdleEvents()
395 bool needMore
= FALSE
;
397 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
400 wxWindow
* win
= node
->GetData();
401 if (SendIdleEvents(win
))
403 node
= node
->GetNext();
409 // Send idle event to window and all subwindows
410 bool wxApp::SendIdleEvents(wxWindow
* win
)
412 bool needMore
= FALSE
;
415 event
.SetEventObject(win
);
416 win
->ProcessEvent(event
);
418 if (event
.MoreRequested())
421 wxWindowList::Node
* node
= win
->GetChildren().GetFirst();
424 wxWindow
* win
= node
->GetData();
425 if (SendIdleEvents(win
))
428 node
= node
->GetNext();
433 void wxApp::DeletePendingObjects()
435 wxList::Node
*node
= wxPendingDelete
.GetFirst();
438 wxObject
*obj
= node
->GetData();
442 if (wxPendingDelete
.Member(obj
))
445 // Deleting one object may have deleted other pending
446 // objects, so start from beginning of list again.
447 node
= wxPendingDelete
.GetFirst();
451 static char *fallbackResources
[] = {
452 "*menuBar.marginHeight: 0",
453 "*menuBar.shadowThickness: 1",
454 "*background: #c0c0c0",
455 "*foreground: black",
459 // Create an application context
460 bool wxApp::OnInitGui()
462 if( !wxAppBase::OnInitGui() )
465 XtToolkitInitialize() ;
466 wxTheApp
->m_appContext
= (WXAppContext
) XtCreateApplicationContext();
467 XtAppSetFallbackResources((XtAppContext
) wxTheApp
->m_appContext
, fallbackResources
);
469 Display
*dpy
= XtOpenDisplay((XtAppContext
) wxTheApp
->m_appContext
,(String
)NULL
,NULL
,
470 wxTheApp
->GetClassName().c_str(), NULL
, 0,
471 # if XtSpecificationRelease < 5
479 // if you don't log to stderr, nothing will be shown...
480 delete wxLog::SetActiveTarget(new wxLogStderr
);
481 wxString
className(wxTheApp
->GetClassName());
482 wxLogError(_("wxWindows could not open display for '%s': exiting."),
486 m_initialDisplay
= (WXDisplay
*) dpy
;
489 // install the X error handler
490 gs_pfnXErrorHandler
= XSetErrorHandler(wxXErrorHandler
);
491 #endif // __WXDEBUG__
493 wxTheApp
->m_topLevelWidget
=
494 (WXWidget
) XtAppCreateShell((String
)NULL
,
495 wxTheApp
->GetClassName().c_str(),
496 applicationShellWidgetClass
,dpy
,
499 // Add general resize proc
501 rec
.string
= "resize";
502 rec
.proc
= (XtActionProc
)wxWidgetResizeProc
;
503 XtAppAddActions((XtAppContext
) wxTheApp
->m_appContext
, &rec
, 1);
505 GetMainColormap(dpy
);
506 m_maxRequestSize
= XMaxRequestSize((Display
*) dpy
);
513 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
515 if (!display
) /* Must be called first with non-NULL display */
516 return m_mainColormap
;
518 int defaultScreen
= DefaultScreen((Display
*) display
);
519 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
521 Colormap c
= DefaultColormapOfScreen(screen
);
524 m_mainColormap
= (WXColormap
) c
;
526 return (WXColormap
) c
;
533 retValue
= wxTheApp
->OnExit();
537 * Exit in some platform-specific way. Not recommended that the app calls this:
538 * only for emergencies.
543 // Yield to other processes
545 bool wxApp::Yield(bool onlyIfNeeded
)
547 bool s_inYield
= FALSE
;
553 wxFAIL_MSG( wxT("wxYield called recursively" ) );
561 while (wxTheApp
&& wxTheApp
->Pending())
562 wxTheApp
->Dispatch();
569 // ----------------------------------------------------------------------------
570 // accessors for C modules
571 // ----------------------------------------------------------------------------
573 extern "C" XtAppContext
wxGetAppContext()
575 return (XtAppContext
)wxTheApp
->GetAppContext();