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"
46 #pragma message disable nosimpint
50 #include <X11/Xutil.h>
51 #include <X11/Xresource.h>
52 #include <X11/Xatom.h>
54 #pragma message enable nosimpint
57 #include "wx/motif/private.h"
61 extern wxList wxPendingDelete
;
62 extern bool wxAddIdleCallback();
64 wxApp
*wxTheApp
= NULL
;
66 wxHashTable
*wxWidgetHashTable
= NULL
;
68 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
70 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
71 EVT_IDLE(wxApp::OnIdle
)
75 typedef int (*XErrorHandlerFunc
)(Display
*, XErrorEvent
*);
77 XErrorHandlerFunc gs_pfnXErrorHandler
= 0;
79 static int wxXErrorHandler(Display
*dpy
, XErrorEvent
*xevent
)
81 // just forward to the default handler for now
82 return gs_pfnXErrorHandler(dpy
, xevent
);
86 long wxApp::sm_lastMessageTime
= 0;
88 bool wxApp::Initialize()
90 wxClassInfo::InitializeClasses();
92 // GL: I'm annoyed ... I don't know where to put this and I don't want to
93 // create a module for that as it's part of the core.
95 wxPendingEventsLocker
= new wxCriticalSection();
98 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
99 wxTheColourDatabase
->Initialize();
101 wxInitializeStockLists();
102 wxInitializeStockObjects();
104 // For PostScript printing
106 /* Done using wxModule now
107 wxInitializePrintSetupData();
108 wxThePrintPaperDatabase = new wxPrintPaperDatabase;
109 wxThePrintPaperDatabase->CreateDatabase();
113 wxBitmap::InitStandardHandlers();
115 wxWidgetHashTable
= new wxHashTable(wxKEY_INTEGER
);
117 wxModule::RegisterModules();
118 if (!wxModule::InitializeModules()) return FALSE
;
123 void wxApp::CleanUp()
125 delete wxWidgetHashTable
;
126 wxWidgetHashTable
= NULL
;
128 wxModule::CleanUpModules();
130 wxDeleteStockObjects() ;
132 // Destroy all GDI lists, etc.
134 wxDeleteStockLists();
136 delete wxTheColourDatabase
;
137 wxTheColourDatabase
= NULL
;
140 /* Done using wxModule now
141 wxInitializePrintSetupData(FALSE);
142 delete wxThePrintPaperDatabase;
143 wxThePrintPaperDatabase = NULL;
147 wxBitmap::CleanUpHandlers();
149 wxClassInfo::CleanUpClasses();
154 // GL: I'm annoyed ... I don't know where to put this and I don't want to
155 // create a module for that as it's part of the core.
157 delete wxPendingEvents
;
158 delete wxPendingEventsLocker
;
161 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
162 // At this point we want to check if there are any memory
163 // blocks that aren't part of the wxDebugContext itself,
164 // as a special case. Then when dumping we need to ignore
165 // wxDebugContext, too.
166 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
168 wxLogDebug("There were memory leaks.\n");
169 wxDebugContext::Dump();
170 wxDebugContext::PrintStatistics();
174 // do it as the very last thing because everything else can log messages
175 wxLog::DontCreateOnDemand();
176 // do it as the very last thing because everything else can log messages
177 delete wxLog::SetActiveTarget(NULL
);
180 int wxEntry( int argc
, char *argv
[] )
182 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
183 // This seems to be necessary since there are 'rogue'
184 // objects present at this point (perhaps global objects?)
185 // Setting a checkpoint will ignore them as far as the
186 // memory checking facility is concerned.
187 // Of course you may argue that memory allocated in globals should be
188 // checked, but this is a reasonable compromise.
189 wxDebugContext::SetCheckpoint();
192 if (!wxApp::Initialize())
197 if (!wxApp::GetInitializerFunction())
199 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
203 wxTheApp
= (wxApp
*) (* wxApp::GetInitializerFunction()) ();
208 printf( "wxWindows error: wxTheApp == NULL\n" );
212 wxTheApp
->SetClassName(wxFileNameFromPath(argv
[0]));
213 wxTheApp
->SetAppName(wxFileNameFromPath(argv
[0]));
215 wxTheApp
->argc
= argc
;
216 wxTheApp
->argv
= argv
;
218 // GUI-specific initialization, such as creating an app context.
219 wxTheApp
->OnInitGui();
221 // Here frames insert themselves automatically into wxTopLevelWindows by
222 // getting created in OnInit().
225 if (wxTheApp
->OnInit())
227 if (wxTheApp
->Initialized()) retValue
= wxTheApp
->OnRun();
230 // flush the logged messages if any
231 wxLog
*pLog
= wxLog::GetActiveTarget();
232 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
235 delete wxLog::SetActiveTarget(new wxLogStderr
); // So dialog boxes aren't used
236 // for further messages
238 if (wxTheApp
->GetTopWindow())
240 delete wxTheApp
->GetTopWindow();
241 wxTheApp
->SetTopWindow(NULL
);
244 wxTheApp
->DeletePendingObjects();
253 // Static member initialization
254 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
261 m_eventLoop
= new wxEventLoop
;
262 m_mainColormap
= (WXColormap
) NULL
;
263 m_appContext
= (WXAppContext
) NULL
;
264 m_topLevelWidget
= (WXWidget
) NULL
;
265 m_maxRequestSize
= 0;
266 m_initialDisplay
= (WXDisplay
*) 0;
274 bool wxApp::Initialized()
282 int wxApp::MainLoop()
285 * Sit around forever waiting to process X-events. Property Change
286 * event are handled special, because they have to refer to
287 * the root window rather than to a widget. therefore we can't
288 * use an Xt-eventhandler.
291 XSelectInput(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()),
292 XDefaultRootWindow(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget())),
300 // Processes an idle event.
301 // Returns TRUE if more time is needed.
302 bool wxApp::ProcessIdle()
306 return ProcessEvent(event
) && event
.MoreRequested();
309 void wxApp::ExitMainLoop()
314 // Is a message/event pending?
315 bool wxApp::Pending()
317 return m_eventLoop
->Pending();
319 XFlush(XtDisplay( (Widget
) wxTheApp
->GetTopLevelWidget() ));
321 // Fix by Doug from STI, to prevent a stall if non-X event
323 return ((XtAppPending( (XtAppContext
) GetAppContext() ) & XtIMXEvent
) != 0) ;
327 // Dispatch a message.
328 void wxApp::Dispatch()
330 m_eventLoop
->Dispatch();
333 // This should be redefined in a derived class for
334 // handling property change events for XAtom IPC.
335 void wxApp::HandlePropertyChange(WXEvent
*event
)
337 // by default do nothing special
338 XtDispatchEvent((XEvent
*) event
); /* let Motif do the work */
341 void wxApp::OnIdle(wxIdleEvent
& event
)
343 static bool inOnIdle
= FALSE
;
345 // Avoid recursion (via ProcessEvent default case)
351 // If there are pending events, we must process them: pending events
352 // are either events to the threads other than main or events posted
353 // with wxPostEvent() functions
354 // GRG: I have moved this here so that all pending events are processed
355 // before starting to delete any objects. This behaves better (in
356 // particular, wrt wxPostEvent) and is coherent with wxGTK's current
357 // behaviour. Also removed the '#if wxUSE_THREADS' around it.
358 // Changed Mar/2000 before 2.1.14
360 // Flush pending events.
361 ProcessPendingEvents();
363 // 'Garbage' collection of windows deleted with Close().
364 DeletePendingObjects();
366 // flush the logged messages if any
367 wxLog
*pLog
= wxLog::GetActiveTarget();
368 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
371 // Send OnIdle events to all windows
372 bool needMore
= SendIdleEvents();
375 event
.RequestMore(TRUE
);
380 // Send idle event to all top-level windows
381 bool wxApp::SendIdleEvents()
383 bool needMore
= FALSE
;
385 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
388 wxWindow
* win
= node
->GetData();
389 if (SendIdleEvents(win
))
391 node
= node
->GetNext();
397 // Send idle event to window and all subwindows
398 bool wxApp::SendIdleEvents(wxWindow
* win
)
400 bool needMore
= FALSE
;
403 event
.SetEventObject(win
);
404 win
->ProcessEvent(event
);
406 if (event
.MoreRequested())
409 wxWindowList::Node
* node
= win
->GetChildren().GetFirst();
412 wxWindow
* win
= node
->GetData();
413 if (SendIdleEvents(win
))
416 node
= node
->GetNext();
421 void wxApp::DeletePendingObjects()
423 wxList::Node
*node
= wxPendingDelete
.GetFirst();
426 wxObject
*obj
= node
->GetData();
430 if (wxPendingDelete
.Member(obj
))
433 // Deleting one object may have deleted other pending
434 // objects, so start from beginning of list again.
435 node
= wxPendingDelete
.GetFirst();
439 static char *fallbackResources
[] = {
440 "*menuBar.marginHeight: 0",
441 "*menuBar.shadowThickness: 1",
442 "*background: #c0c0c0",
443 "*foreground: black",
447 // Create an application context
448 bool wxApp::OnInitGui()
450 if( !wxAppBase::OnInitGui() )
453 XtToolkitInitialize() ;
454 wxTheApp
->m_appContext
= (WXAppContext
) XtCreateApplicationContext();
455 XtAppSetFallbackResources((XtAppContext
) wxTheApp
->m_appContext
, fallbackResources
);
457 Display
*dpy
= XtOpenDisplay((XtAppContext
) wxTheApp
->m_appContext
,(String
)NULL
,NULL
,
458 wxTheApp
->GetClassName().c_str(), NULL
, 0,
459 # if XtSpecificationRelease < 5
467 // if you don't log to stderr, nothing will be shown...
468 delete wxLog::SetActiveTarget(new wxLogStderr
);
469 wxString
className(wxTheApp
->GetClassName());
470 wxLogError(_("wxWindows could not open display for '%s': exiting."),
474 m_initialDisplay
= (WXDisplay
*) dpy
;
477 // install the X error handler
478 gs_pfnXErrorHandler
= XSetErrorHandler(wxXErrorHandler
);
479 #endif // __WXDEBUG__
481 wxTheApp
->m_topLevelWidget
=
482 (WXWidget
) XtAppCreateShell((String
)NULL
,
483 wxTheApp
->GetClassName().c_str(),
484 applicationShellWidgetClass
,dpy
,
487 // Add general resize proc
489 rec
.string
= "resize";
490 rec
.proc
= (XtActionProc
)wxWidgetResizeProc
;
491 XtAppAddActions((XtAppContext
) wxTheApp
->m_appContext
, &rec
, 1);
493 GetMainColormap(dpy
);
494 m_maxRequestSize
= XMaxRequestSize((Display
*) dpy
);
501 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
503 if (!display
) /* Must be called first with non-NULL display */
504 return m_mainColormap
;
506 int defaultScreen
= DefaultScreen((Display
*) display
);
507 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
509 Colormap c
= DefaultColormapOfScreen(screen
);
512 m_mainColormap
= (WXColormap
) c
;
514 return (WXColormap
) c
;
521 retValue
= wxTheApp
->OnExit();
525 * Exit in some platform-specific way. Not recommended that the app calls this:
526 * only for emergencies.
531 // Yield to other processes
533 bool wxApp::Yield(bool onlyIfNeeded
)
535 bool s_inYield
= FALSE
;
541 wxFAIL_MSG( wxT("wxYield called recursively" ) );
549 while (wxTheApp
&& wxTheApp
->Pending())
550 wxTheApp
->Dispatch();
557 // ----------------------------------------------------------------------------
558 // accessors for C modules
559 // ----------------------------------------------------------------------------
561 extern "C" XtAppContext
wxGetAppContext()
563 return (XtAppContext
)wxTheApp
->GetAppContext();