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 struct wxPerDisplayData
54 { m_visualInfo
= NULL
; m_topLevelWidget
= NULL
; }
56 wxXVisualInfo
* m_visualInfo
;
57 Widget m_topLevelWidget
;
60 WX_DECLARE_VOIDPTR_HASH_MAP( wxPerDisplayData
, wxPerDisplayDataMap
);
62 static void wxTLWidgetDestroyCallback(Widget w
, XtPointer clientData
,
64 static WXWidget
wxCreateTopLevelWidget( WXDisplay
* display
);
66 extern wxList wxPendingDelete
;
67 extern bool wxAddIdleCallback();
69 wxApp
*wxTheApp
= NULL
;
71 wxHashTable
*wxWidgetHashTable
= NULL
;
73 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
75 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
76 EVT_IDLE(wxApp::OnIdle
)
80 typedef int (*XErrorHandlerFunc
)(Display
*, XErrorEvent
*);
82 XErrorHandlerFunc gs_pfnXErrorHandler
= 0;
84 static int wxXErrorHandler(Display
*dpy
, XErrorEvent
*xevent
)
86 // just forward to the default handler for now
87 return gs_pfnXErrorHandler(dpy
, xevent
);
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 wxModule::CleanUpModules();
119 wxDeleteStockObjects() ;
121 // Destroy all GDI lists, etc.
123 wxDeleteStockLists();
125 delete wxTheColourDatabase
;
126 wxTheColourDatabase
= NULL
;
128 wxClassInfo::CleanUpClasses();
133 delete wxWidgetHashTable
;
134 wxWidgetHashTable
= NULL
;
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 wxPendingEvents
= NULL
;
141 delete wxPendingEventsLocker
;
142 wxPendingEventsLocker
= NULL
;
145 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
146 // At this point we want to check if there are any memory
147 // blocks that aren't part of the wxDebugContext itself,
148 // as a special case. Then when dumping we need to ignore
149 // wxDebugContext, too.
150 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
152 wxLogDebug("There were memory leaks.\n");
153 wxDebugContext::Dump();
154 wxDebugContext::PrintStatistics();
158 // do it as the very last thing because everything else can log messages
159 wxLog::DontCreateOnDemand();
160 // do it as the very last thing because everything else can log messages
161 delete wxLog::SetActiveTarget(NULL
);
164 // ============================================================================
166 // ============================================================================
168 int wxEntryStart( int argc
, char* argv
[] )
170 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
171 // This seems to be necessary since there are 'rogue'
172 // objects present at this point (perhaps global objects?)
173 // Setting a checkpoint will ignore them as far as the
174 // memory checking facility is concerned.
175 // Of course you may argue that memory allocated in globals should be
176 // checked, but this is a reasonable compromise.
177 wxDebugContext::SetCheckpoint();
180 if (!wxApp::Initialize())
190 // GUI-specific initialization, such as creating an app context.
191 if (!wxTheApp
->OnInitGui())
197 void wxEntryCleanup()
199 // So dialog boxes aren't used for further messages
200 delete wxLog::SetActiveTarget(new wxLogStderr
);
202 // flush the logged messages if any
203 wxLog
*pLog
= wxLog::GetActiveTarget();
204 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
210 int wxEntry( int argc
, char *argv
[] )
214 retValue
= wxEntryStart( argc
, argv
);
215 if (retValue
) return retValue
;
219 if (!wxApp::GetInitializerFunction())
221 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
225 wxTheApp
= (wxApp
*) (* wxApp::GetInitializerFunction()) ();
230 printf( "wxWindows error: wxTheApp == NULL\n" );
234 wxTheApp
->SetClassName(wxFileNameFromPath(argv
[0]));
235 wxTheApp
->SetAppName(wxFileNameFromPath(argv
[0]));
237 wxTheApp
->argc
= argc
;
238 wxTheApp
->argv
= argv
;
240 // GUI-specific initialization, such as creating an app context.
241 retValue
= wxEntryInitGui();
242 if (retValue
) return retValue
;
244 // Here frames insert themselves automatically into wxTopLevelWindows by
245 // getting created in OnInit().
247 if (wxTheApp
->OnInit())
249 if (wxTheApp
->Initialized())
253 if (wxTheApp
->GetTopWindow())
255 delete wxTheApp
->GetTopWindow();
256 wxTheApp
->SetTopWindow(NULL
);
259 wxTheApp
->DeletePendingObjects();
261 retValue
= wxTheApp
->OnExit();
268 // Static member initialization
269 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
276 m_eventLoop
= new wxEventLoop
;
277 m_mainColormap
= (WXColormap
) NULL
;
278 m_appContext
= (WXAppContext
) NULL
;
279 m_initialDisplay
= (WXDisplay
*) 0;
280 m_perDisplayData
= new wxPerDisplayDataMap
;
287 for( wxPerDisplayDataMap::iterator it
= m_perDisplayData
->begin(),
288 end
= m_perDisplayData
->end();
291 delete it
->second
.m_visualInfo
;
292 XtDestroyWidget( it
->second
.m_topLevelWidget
);
295 delete m_perDisplayData
;
300 bool wxApp::Initialized()
308 int wxApp::MainLoop()
311 * Sit around forever waiting to process X-events. Property Change
312 * event are handled special, because they have to refer to
313 * the root window rather than to a widget. therefore we can't
314 * use an Xt-eventhandler.
317 XSelectInput(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()),
318 XDefaultRootWindow(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget())),
326 // Processes an idle event.
327 // Returns TRUE if more time is needed.
328 bool wxApp::ProcessIdle()
332 return ProcessEvent(event
) && event
.MoreRequested();
335 void wxApp::ExitMainLoop()
337 if( m_eventLoop
->IsRunning() )
341 // Is a message/event pending?
342 bool wxApp::Pending()
344 return m_eventLoop
->Pending();
346 XFlush(XtDisplay( (Widget
) wxTheApp
->GetTopLevelWidget() ));
348 // Fix by Doug from STI, to prevent a stall if non-X event
350 return ((XtAppPending( (XtAppContext
) GetAppContext() ) & XtIMXEvent
) != 0) ;
354 // Dispatch a message.
355 void wxApp::Dispatch()
357 m_eventLoop
->Dispatch();
360 // This should be redefined in a derived class for
361 // handling property change events for XAtom IPC.
362 void wxApp::HandlePropertyChange(WXEvent
*event
)
364 // by default do nothing special
365 XtDispatchEvent((XEvent
*) event
); /* let Motif do the work */
368 void wxApp::OnIdle(wxIdleEvent
& event
)
370 static bool inOnIdle
= FALSE
;
372 // Avoid recursion (via ProcessEvent default case)
378 // If there are pending events, we must process them: pending events
379 // are either events to the threads other than main or events posted
380 // with wxPostEvent() functions
381 // GRG: I have moved this here so that all pending events are processed
382 // before starting to delete any objects. This behaves better (in
383 // particular, wrt wxPostEvent) and is coherent with wxGTK's current
384 // behaviour. Also removed the '#if wxUSE_THREADS' around it.
385 // Changed Mar/2000 before 2.1.14
387 // Flush pending events.
388 ProcessPendingEvents();
390 // 'Garbage' collection of windows deleted with Close().
391 DeletePendingObjects();
393 // flush the logged messages if any
394 wxLog
*pLog
= wxLog::GetActiveTarget();
395 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
398 // Send OnIdle events to all windows
399 bool needMore
= SendIdleEvents();
402 event
.RequestMore(TRUE
);
407 // Send idle event to all top-level windows
408 bool wxApp::SendIdleEvents()
410 bool needMore
= FALSE
;
412 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
415 wxWindow
* win
= node
->GetData();
416 if (SendIdleEvents(win
))
418 node
= node
->GetNext();
424 // Send idle event to window and all subwindows
425 bool wxApp::SendIdleEvents(wxWindow
* win
)
427 bool needMore
= FALSE
;
430 event
.SetEventObject(win
);
431 win
->GetEventHandler()->ProcessEvent(event
);
433 if (event
.MoreRequested())
436 wxWindowList::Node
* node
= win
->GetChildren().GetFirst();
439 wxWindow
* win
= node
->GetData();
440 if (SendIdleEvents(win
))
443 node
= node
->GetNext();
448 void wxApp::DeletePendingObjects()
450 wxList::Node
*node
= wxPendingDelete
.GetFirst();
453 wxObject
*obj
= node
->GetData();
457 if (wxPendingDelete
.Member(obj
))
460 // Deleting one object may have deleted other pending
461 // objects, so start from beginning of list again.
462 node
= wxPendingDelete
.GetFirst();
466 static char *fallbackResources
[] = {
467 "*menuBar.marginHeight: 0",
468 "*menuBar.shadowThickness: 1",
469 "*background: #c0c0c0",
470 "*foreground: black",
474 // Create an application context
475 bool wxApp::OnInitGui()
477 if( !wxAppBase::OnInitGui() )
480 XtToolkitInitialize() ;
481 wxTheApp
->m_appContext
= (WXAppContext
) XtCreateApplicationContext();
482 XtAppSetFallbackResources((XtAppContext
) wxTheApp
->m_appContext
, fallbackResources
);
484 Display
*dpy
= XtOpenDisplay((XtAppContext
) wxTheApp
->m_appContext
,(String
)NULL
,NULL
,
485 wxTheApp
->GetClassName().c_str(), NULL
, 0,
486 # if XtSpecificationRelease < 5
494 // if you don't log to stderr, nothing will be shown...
495 delete wxLog::SetActiveTarget(new wxLogStderr
);
496 wxString
className(wxTheApp
->GetClassName());
497 wxLogError(_("wxWindows could not open display for '%s': exiting."),
501 m_initialDisplay
= (WXDisplay
*) dpy
;
504 // install the X error handler
505 gs_pfnXErrorHandler
= XSetErrorHandler(wxXErrorHandler
);
506 #endif // __WXDEBUG__
508 // Add general resize proc
510 rec
.string
= "resize";
511 rec
.proc
= (XtActionProc
)wxWidgetResizeProc
;
512 XtAppAddActions((XtAppContext
) wxTheApp
->m_appContext
, &rec
, 1);
514 GetMainColormap(dpy
);
521 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
523 if (!display
) /* Must be called first with non-NULL display */
524 return m_mainColormap
;
526 int defaultScreen
= DefaultScreen((Display
*) display
);
527 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
529 Colormap c
= DefaultColormapOfScreen(screen
);
532 m_mainColormap
= (WXColormap
) c
;
534 return (WXColormap
) c
;
537 wxXVisualInfo
* wxApp::GetVisualInfo( WXDisplay
* display
)
539 wxPerDisplayDataMap::iterator it
= m_perDisplayData
->find( display
);
541 if( it
!= m_perDisplayData
->end() && it
->second
.m_visualInfo
)
542 return it
->second
.m_visualInfo
;
544 wxXVisualInfo
* vi
= new wxXVisualInfo
;
545 wxFillXVisualInfo( vi
, (Display
*)display
);
547 (*m_perDisplayData
)[display
].m_visualInfo
= vi
;
552 static void wxTLWidgetDestroyCallback(Widget w
, XtPointer clientData
,
556 wxTheApp
->SetTopLevelWidget( (WXDisplay
*)XtDisplay(w
),
560 WXWidget
wxCreateTopLevelWidget( WXDisplay
* display
)
562 Widget tlw
= XtAppCreateShell( (String
)NULL
,
563 wxTheApp
->GetClassName().c_str(),
564 applicationShellWidgetClass
,
568 XtAddCallback( tlw
, XmNdestroyCallback
,
569 (XtCallbackProc
)wxTLWidgetDestroyCallback
,
572 return (WXWidget
)tlw
;
575 WXWidget
wxApp::GetTopLevelWidget()
577 WXDisplay
* display
= wxGetDisplay();
578 wxPerDisplayDataMap::iterator it
= m_perDisplayData
->find( display
);
580 if( it
!= m_perDisplayData
->end() && it
->second
.m_topLevelWidget
)
581 return (WXWidget
)it
->second
.m_topLevelWidget
;
583 WXWidget tlw
= wxCreateTopLevelWidget( display
);
584 SetTopLevelWidget( display
, tlw
);
589 void wxApp::SetTopLevelWidget(WXDisplay
* display
, WXWidget widget
)
591 (*m_perDisplayData
)[display
].m_topLevelWidget
= (Widget
)widget
;
598 retValue
= wxTheApp
->OnExit();
602 * Exit in some platform-specific way.
603 * Not recommended that the app calls this:
604 * only for emergencies.
609 // Yield to other processes
611 bool wxApp::Yield(bool onlyIfNeeded
)
613 bool s_inYield
= FALSE
;
619 wxFAIL_MSG( wxT("wxYield called recursively" ) );
627 while (wxTheApp
&& wxTheApp
->Pending())
628 wxTheApp
->Dispatch();
635 // ----------------------------------------------------------------------------
636 // accessors for C modules
637 // ----------------------------------------------------------------------------
639 extern "C" XtAppContext
wxGetAppContext()
641 return (XtAppContext
)wxTheApp
->GetAppContext();