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"
41 #include "wx/thread.h"
44 #if wxUSE_WX_RESOURCES
45 #include "wx/resource.h"
49 #pragma message disable nosimpint
53 #include <X11/Xutil.h>
54 #include <X11/Xresource.h>
55 #include <X11/Xatom.h>
57 #pragma message enable nosimpint
60 #include "wx/motif/private.h"
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 #if wxUSE_WX_RESOURCES
108 wxInitializeResourceSystem();
111 // For PostScript printing
113 /* Done using wxModule now
114 wxInitializePrintSetupData();
115 wxThePrintPaperDatabase = new wxPrintPaperDatabase;
116 wxThePrintPaperDatabase->CreateDatabase();
120 wxBitmap::InitStandardHandlers();
122 wxWidgetHashTable
= new wxHashTable(wxKEY_INTEGER
);
124 wxModule::RegisterModules();
125 if (!wxModule::InitializeModules()) return FALSE
;
130 void wxApp::CleanUp()
132 delete wxWidgetHashTable
;
133 wxWidgetHashTable
= NULL
;
135 wxModule::CleanUpModules();
137 #if wxUSE_WX_RESOURCES
138 wxCleanUpResourceSystem();
141 wxDeleteStockObjects() ;
143 // Destroy all GDI lists, etc.
145 wxDeleteStockLists();
147 delete wxTheColourDatabase
;
148 wxTheColourDatabase
= NULL
;
151 /* Done using wxModule now
152 wxInitializePrintSetupData(FALSE);
153 delete wxThePrintPaperDatabase;
154 wxThePrintPaperDatabase = NULL;
158 wxBitmap::CleanUpHandlers();
160 wxClassInfo::CleanUpClasses();
165 // GL: I'm annoyed ... I don't know where to put this and I don't want to
166 // create a module for that as it's part of the core.
168 delete wxPendingEvents
;
169 delete wxPendingEventsLocker
;
172 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
173 // At this point we want to check if there are any memory
174 // blocks that aren't part of the wxDebugContext itself,
175 // as a special case. Then when dumping we need to ignore
176 // wxDebugContext, too.
177 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
179 wxLogDebug("There were memory leaks.\n");
180 wxDebugContext::Dump();
181 wxDebugContext::PrintStatistics();
185 // do it as the very last thing because everything else can log messages
186 wxLog::DontCreateOnDemand();
187 // do it as the very last thing because everything else can log messages
188 delete wxLog::SetActiveTarget(NULL
);
191 int wxEntry( int argc
, char *argv
[] )
193 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
194 // This seems to be necessary since there are 'rogue'
195 // objects present at this point (perhaps global objects?)
196 // Setting a checkpoint will ignore them as far as the
197 // memory checking facility is concerned.
198 // Of course you may argue that memory allocated in globals should be
199 // checked, but this is a reasonable compromise.
200 wxDebugContext::SetCheckpoint();
203 if (!wxApp::Initialize())
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 wxTheApp
->OnInitGui();
232 // Here frames insert themselves automatically into wxTopLevelWindows by
233 // getting created in OnInit().
236 if (wxTheApp
->OnInit())
238 if (wxTheApp
->Initialized()) retValue
= wxTheApp
->OnRun();
241 // flush the logged messages if any
242 wxLog
*pLog
= wxLog::GetActiveTarget();
243 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
246 delete wxLog::SetActiveTarget(new wxLogStderr
); // So dialog boxes aren't used
247 // for further messages
249 if (wxTheApp
->GetTopWindow())
251 delete wxTheApp
->GetTopWindow();
252 wxTheApp
->SetTopWindow(NULL
);
255 wxTheApp
->DeletePendingObjects();
264 // Static member initialization
265 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
272 m_eventLoop
= new wxEventLoop
;
273 m_mainColormap
= (WXColormap
) NULL
;
274 m_appContext
= (WXAppContext
) NULL
;
275 m_topLevelWidget
= (WXWidget
) NULL
;
276 m_maxRequestSize
= 0;
277 m_initialDisplay
= (WXDisplay
*) 0;
285 bool wxApp::Initialized()
293 int wxApp::MainLoop()
296 * Sit around forever waiting to process X-events. Property Change
297 * event are handled special, because they have to refer to
298 * the root window rather than to a widget. therefore we can't
299 * use an Xt-eventhandler.
302 XSelectInput(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()),
303 XDefaultRootWindow(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget())),
311 // Processes an idle event.
312 // Returns TRUE if more time is needed.
313 bool wxApp::ProcessIdle()
317 return ProcessEvent(event
) && event
.MoreRequested();
320 void wxApp::ExitMainLoop()
325 // Is a message/event pending?
326 bool wxApp::Pending()
328 XFlush(XtDisplay( (Widget
) wxTheApp
->GetTopLevelWidget() ));
330 // Fix by Doug from STI, to prevent a stall if non-X event
332 return ((XtAppPending( (XtAppContext
) GetAppContext() ) & XtIMXEvent
) != 0) ;
335 // Dispatch a message.
336 void wxApp::Dispatch()
338 m_eventLoop
->Dispatch();
341 // This should be redefined in a derived class for
342 // handling property change events for XAtom IPC.
343 void wxApp::HandlePropertyChange(WXEvent
*event
)
345 // by default do nothing special
346 XtDispatchEvent((XEvent
*) event
); /* let Motif do the work */
349 void wxApp::OnIdle(wxIdleEvent
& event
)
351 static bool inOnIdle
= FALSE
;
353 // Avoid recursion (via ProcessEvent default case)
359 // If there are pending events, we must process them: pending events
360 // are either events to the threads other than main or events posted
361 // with wxPostEvent() functions
362 // GRG: I have moved this here so that all pending events are processed
363 // before starting to delete any objects. This behaves better (in
364 // particular, wrt wxPostEvent) and is coherent with wxGTK's current
365 // behaviour. Also removed the '#if wxUSE_THREADS' around it.
366 // Changed Mar/2000 before 2.1.14
368 // Flush pending events.
369 ProcessPendingEvents();
371 // 'Garbage' collection of windows deleted with Close().
372 DeletePendingObjects();
374 // flush the logged messages if any
375 wxLog
*pLog
= wxLog::GetActiveTarget();
376 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
379 // Send OnIdle events to all windows
380 bool needMore
= SendIdleEvents();
383 event
.RequestMore(TRUE
);
388 // Send idle event to all top-level windows
389 bool wxApp::SendIdleEvents()
391 bool needMore
= FALSE
;
393 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
396 wxWindow
* win
= node
->GetData();
397 if (SendIdleEvents(win
))
399 node
= node
->GetNext();
405 // Send idle event to window and all subwindows
406 bool wxApp::SendIdleEvents(wxWindow
* win
)
408 bool needMore
= FALSE
;
411 event
.SetEventObject(win
);
412 win
->ProcessEvent(event
);
414 if (event
.MoreRequested())
417 wxWindowList::Node
* node
= win
->GetChildren().GetFirst();
420 wxWindow
* win
= node
->GetData();
421 if (SendIdleEvents(win
))
424 node
= node
->GetNext();
429 void wxApp::DeletePendingObjects()
431 wxList::Node
*node
= wxPendingDelete
.GetFirst();
434 wxObject
*obj
= node
->GetData();
438 if (wxPendingDelete
.Member(obj
))
441 // Deleting one object may have deleted other pending
442 // objects, so start from beginning of list again.
443 node
= wxPendingDelete
.GetFirst();
447 static char *fallbackResources
[] = {
448 "*menuBar.marginHeight: 0",
449 "*menuBar.shadowThickness: 1",
450 "*background: #c0c0c0",
451 "*foreground: black",
455 // Create an application context
456 bool wxApp::OnInitGui()
458 if( !wxAppBase::OnInitGui() )
461 XtToolkitInitialize() ;
462 wxTheApp
->m_appContext
= (WXAppContext
) XtCreateApplicationContext();
463 XtAppSetFallbackResources((XtAppContext
) wxTheApp
->m_appContext
, fallbackResources
);
465 Display
*dpy
= XtOpenDisplay((XtAppContext
) wxTheApp
->m_appContext
,(String
)NULL
,NULL
,
466 (const char*) wxTheApp
->GetClassName(), NULL
, 0,
467 # if XtSpecificationRelease < 5
475 // if you don't log to stderr, nothing will be shown...
476 delete wxLog::SetActiveTarget(new wxLogStderr
);
477 wxString
className(wxTheApp
->GetClassName());
478 wxLogError(_("wxWindows could not open display for '%s': exiting."),
479 (const char*) className
);
482 m_initialDisplay
= (WXDisplay
*) dpy
;
485 // install the X error handler
486 gs_pfnXErrorHandler
= XSetErrorHandler(wxXErrorHandler
);
487 #endif // __WXDEBUG__
489 wxTheApp
->m_topLevelWidget
= (WXWidget
) XtAppCreateShell((String
)NULL
, (const char*) wxTheApp
->GetClassName(),
490 applicationShellWidgetClass
,dpy
,
493 // Add general resize proc
495 rec
.string
= "resize";
496 rec
.proc
= (XtActionProc
)wxWidgetResizeProc
;
497 XtAppAddActions((XtAppContext
) wxTheApp
->m_appContext
, &rec
, 1);
499 GetMainColormap(dpy
);
500 m_maxRequestSize
= XMaxRequestSize((Display
*) dpy
);
507 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
509 if (!display
) /* Must be called first with non-NULL display */
510 return m_mainColormap
;
512 int defaultScreen
= DefaultScreen((Display
*) display
);
513 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
515 Colormap c
= DefaultColormapOfScreen(screen
);
518 m_mainColormap
= (WXColormap
) c
;
520 return (WXColormap
) c
;
527 retValue
= wxTheApp
->OnExit();
531 * Exit in some platform-specific way. Not recommended that the app calls this:
532 * only for emergencies.
537 // Yield to other processes
539 bool wxApp::Yield(bool onlyIfNeeded
)
541 bool s_inYield
= FALSE
;
547 wxFAIL_MSG( wxT("wxYield called recursively" ) );
555 while (wxTheApp
&& wxTheApp
->Pending())
556 wxTheApp
->Dispatch();
563 // ----------------------------------------------------------------------------
564 // accessors for C modules
565 // ----------------------------------------------------------------------------
567 extern "C" XtAppContext
wxGetAppContext()
569 return (XtAppContext
)wxTheApp
->GetAppContext();