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 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 bool wxApp::Initialize()
91 wxClassInfo::InitializeClasses();
93 // GL: I'm annoyed ... I don't know where to put this and I don't want to
94 // create a module for that as it's part of the core.
96 wxPendingEventsLocker
= new wxCriticalSection();
99 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
100 wxTheColourDatabase
->Initialize();
102 wxInitializeStockLists();
103 wxInitializeStockObjects();
105 wxWidgetHashTable
= new wxHashTable(wxKEY_INTEGER
);
107 wxModule::RegisterModules();
108 if (!wxModule::InitializeModules()) return FALSE
;
113 void wxApp::CleanUp()
115 wxModule::CleanUpModules();
117 wxDeleteStockObjects() ;
119 // Destroy all GDI lists, etc.
121 wxDeleteStockLists();
123 delete wxTheColourDatabase
;
124 wxTheColourDatabase
= NULL
;
126 wxClassInfo::CleanUpClasses();
131 delete wxWidgetHashTable
;
132 wxWidgetHashTable
= NULL
;
134 // GL: I'm annoyed ... I don't know where to put this and I don't want to
135 // create a module for that as it's part of the core.
137 delete wxPendingEvents
;
138 wxPendingEvents
= NULL
;
139 delete wxPendingEventsLocker
;
140 wxPendingEventsLocker
= NULL
;
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
);
166 wxAppConsole::Exit();
169 // ============================================================================
171 // ============================================================================
173 int wxEntryStart( int argc
, char* argv
[] )
175 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
176 // This seems to be necessary since there are 'rogue'
177 // objects present at this point (perhaps global objects?)
178 // Setting a checkpoint will ignore them as far as the
179 // memory checking facility is concerned.
180 // Of course you may argue that memory allocated in globals should be
181 // checked, but this is a reasonable compromise.
182 wxDebugContext::SetCheckpoint();
185 if (!wxApp::Initialize())
195 // GUI-specific initialization, such as creating an app context.
196 if (!wxTheApp
->OnInitGui())
202 void wxEntryCleanup()
204 // So dialog boxes aren't used for further messages
205 delete wxLog::SetActiveTarget(new wxLogStderr
);
207 // flush the logged messages if any
208 wxLog
*pLog
= wxLog::GetActiveTarget();
209 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
215 int wxEntry( int argc
, char *argv
[] )
219 retValue
= wxEntryStart( argc
, argv
);
220 if (retValue
) return retValue
;
224 if (!wxApp::GetInitializerFunction())
226 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
230 wxTheApp
= (wxApp
*) (* wxApp::GetInitializerFunction()) ();
235 printf( "wxWindows error: wxTheApp == NULL\n" );
239 wxTheApp
->SetClassName(wxFileNameFromPath(argv
[0]));
240 wxTheApp
->SetAppName(wxFileNameFromPath(argv
[0]));
242 wxTheApp
->argc
= argc
;
243 wxTheApp
->argv
= argv
;
245 // GUI-specific initialization, such as creating an app context.
246 retValue
= wxEntryInitGui();
247 if (retValue
) return retValue
;
249 // Here frames insert themselves automatically into wxTopLevelWindows by
250 // getting created in OnInit().
252 if (wxTheApp
->OnInit())
254 if (wxTheApp
->Initialized())
258 if (wxTheApp
->GetTopWindow())
260 delete wxTheApp
->GetTopWindow();
261 wxTheApp
->SetTopWindow(NULL
);
264 wxTheApp
->DeletePendingObjects();
266 retValue
= wxTheApp
->OnExit();
278 m_eventLoop
= new wxEventLoop
;
279 m_mainColormap
= (WXColormap
) NULL
;
280 m_appContext
= (WXAppContext
) NULL
;
281 m_initialDisplay
= (WXDisplay
*) 0;
282 m_perDisplayData
= new wxPerDisplayDataMap
;
289 for( wxPerDisplayDataMap::iterator it
= m_perDisplayData
->begin(),
290 end
= m_perDisplayData
->end();
293 delete it
->second
.m_visualInfo
;
294 XtDestroyWidget( it
->second
.m_topLevelWidget
);
297 delete m_perDisplayData
;
302 bool wxApp::Initialized()
310 int wxApp::MainLoop()
313 * Sit around forever waiting to process X-events. Property Change
314 * event are handled special, because they have to refer to
315 * the root window rather than to a widget. therefore we can't
316 * use an Xt-eventhandler.
319 XSelectInput(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()),
320 XDefaultRootWindow(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget())),
328 // Processes an idle event.
329 // Returns TRUE if more time is needed.
330 bool wxApp::ProcessIdle()
334 return ProcessEvent(event
) && event
.MoreRequested();
337 void wxApp::ExitMainLoop()
339 if( m_eventLoop
->IsRunning() )
343 // Is a message/event pending?
344 bool wxApp::Pending()
346 return m_eventLoop
->Pending();
348 XFlush(XtDisplay( (Widget
) wxTheApp
->GetTopLevelWidget() ));
350 // Fix by Doug from STI, to prevent a stall if non-X event
352 return ((XtAppPending( (XtAppContext
) GetAppContext() ) & XtIMXEvent
) != 0) ;
356 // Dispatch a message.
357 void wxApp::Dispatch()
359 m_eventLoop
->Dispatch();
362 // This should be redefined in a derived class for
363 // handling property change events for XAtom IPC.
364 void wxApp::HandlePropertyChange(WXEvent
*event
)
366 // by default do nothing special
367 XtDispatchEvent((XEvent
*) event
); /* let Motif do the work */
370 void wxApp::OnIdle(wxIdleEvent
& event
)
372 static bool inOnIdle
= FALSE
;
374 // Avoid recursion (via ProcessEvent default case)
380 // If there are pending events, we must process them: pending events
381 // are either events to the threads other than main or events posted
382 // with wxPostEvent() functions
383 // GRG: I have moved this here so that all pending events are processed
384 // before starting to delete any objects. This behaves better (in
385 // particular, wrt wxPostEvent) and is coherent with wxGTK's current
386 // behaviour. Also removed the '#if wxUSE_THREADS' around it.
387 // Changed Mar/2000 before 2.1.14
389 // Flush pending events.
390 ProcessPendingEvents();
392 // 'Garbage' collection of windows deleted with Close().
393 DeletePendingObjects();
395 // flush the logged messages if any
396 wxLog
*pLog
= wxLog::GetActiveTarget();
397 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
400 // Send OnIdle events to all windows
401 bool needMore
= SendIdleEvents();
404 event
.RequestMore(TRUE
);
409 // Send idle event to all top-level windows
410 bool wxApp::SendIdleEvents()
412 bool needMore
= FALSE
;
414 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
417 wxWindow
* win
= node
->GetData();
418 if (SendIdleEvents(win
))
420 node
= node
->GetNext();
426 // Send idle event to window and all subwindows
427 bool wxApp::SendIdleEvents(wxWindow
* win
)
429 bool needMore
= FALSE
;
432 event
.SetEventObject(win
);
433 win
->GetEventHandler()->ProcessEvent(event
);
435 if (event
.MoreRequested())
438 wxWindowList::Node
* node
= win
->GetChildren().GetFirst();
441 wxWindow
* win
= node
->GetData();
442 if (SendIdleEvents(win
))
445 node
= node
->GetNext();
450 void wxApp::DeletePendingObjects()
452 wxList::Node
*node
= wxPendingDelete
.GetFirst();
455 wxObject
*obj
= node
->GetData();
459 if (wxPendingDelete
.Member(obj
))
462 // Deleting one object may have deleted other pending
463 // objects, so start from beginning of list again.
464 node
= wxPendingDelete
.GetFirst();
468 static char *fallbackResources
[] = {
469 "*menuBar.marginHeight: 0",
470 "*menuBar.shadowThickness: 1",
471 "*background: #c0c0c0",
472 "*foreground: black",
476 // Create an application context
477 bool wxApp::OnInitGui()
479 if( !wxAppBase::OnInitGui() )
482 XtToolkitInitialize() ;
483 wxTheApp
->m_appContext
= (WXAppContext
) XtCreateApplicationContext();
484 XtAppSetFallbackResources((XtAppContext
) wxTheApp
->m_appContext
, fallbackResources
);
486 Display
*dpy
= XtOpenDisplay((XtAppContext
) wxTheApp
->m_appContext
,(String
)NULL
,NULL
,
487 wxTheApp
->GetClassName().c_str(), NULL
, 0,
488 # if XtSpecificationRelease < 5
496 // if you don't log to stderr, nothing will be shown...
497 delete wxLog::SetActiveTarget(new wxLogStderr
);
498 wxString
className(wxTheApp
->GetClassName());
499 wxLogError(_("wxWindows could not open display for '%s': exiting."),
503 m_initialDisplay
= (WXDisplay
*) dpy
;
506 // install the X error handler
507 gs_pfnXErrorHandler
= XSetErrorHandler(wxXErrorHandler
);
508 #endif // __WXDEBUG__
510 // Add general resize proc
512 rec
.string
= "resize";
513 rec
.proc
= (XtActionProc
)wxWidgetResizeProc
;
514 XtAppAddActions((XtAppContext
) wxTheApp
->m_appContext
, &rec
, 1);
516 GetMainColormap(dpy
);
523 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
525 if (!display
) /* Must be called first with non-NULL display */
526 return m_mainColormap
;
528 int defaultScreen
= DefaultScreen((Display
*) display
);
529 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
531 Colormap c
= DefaultColormapOfScreen(screen
);
534 m_mainColormap
= (WXColormap
) c
;
536 return (WXColormap
) c
;
539 wxXVisualInfo
* wxApp::GetVisualInfo( WXDisplay
* display
)
541 wxPerDisplayDataMap::iterator it
= m_perDisplayData
->find( display
);
543 if( it
!= m_perDisplayData
->end() && it
->second
.m_visualInfo
)
544 return it
->second
.m_visualInfo
;
546 wxXVisualInfo
* vi
= new wxXVisualInfo
;
547 wxFillXVisualInfo( vi
, (Display
*)display
);
549 (*m_perDisplayData
)[display
].m_visualInfo
= vi
;
554 static void wxTLWidgetDestroyCallback(Widget w
, XtPointer clientData
,
558 wxTheApp
->SetTopLevelWidget( (WXDisplay
*)XtDisplay(w
),
562 WXWidget
wxCreateTopLevelWidget( WXDisplay
* display
)
564 Widget tlw
= XtAppCreateShell( (String
)NULL
,
565 wxTheApp
->GetClassName().c_str(),
566 applicationShellWidgetClass
,
569 XtSetMappedWhenManaged( tlw
, False
);
570 XtRealizeWidget( tlw
);
572 XtAddCallback( tlw
, XmNdestroyCallback
,
573 (XtCallbackProc
)wxTLWidgetDestroyCallback
,
576 return (WXWidget
)tlw
;
579 WXWidget
wxApp::GetTopLevelWidget()
581 WXDisplay
* display
= wxGetDisplay();
582 wxPerDisplayDataMap::iterator it
= m_perDisplayData
->find( display
);
584 if( it
!= m_perDisplayData
->end() && it
->second
.m_topLevelWidget
)
585 return (WXWidget
)it
->second
.m_topLevelWidget
;
587 WXWidget tlw
= wxCreateTopLevelWidget( display
);
588 SetTopLevelWidget( display
, tlw
);
593 void wxApp::SetTopLevelWidget(WXDisplay
* display
, WXWidget widget
)
595 (*m_perDisplayData
)[display
].m_topLevelWidget
= (Widget
)widget
;
598 // Yield to other processes
600 bool wxApp::Yield(bool onlyIfNeeded
)
602 bool s_inYield
= FALSE
;
608 wxFAIL_MSG( wxT("wxYield called recursively" ) );
616 while (wxTheApp
&& wxTheApp
->Pending())
617 wxTheApp
->Dispatch();
624 // ----------------------------------------------------------------------------
625 // accessors for C modules
626 // ----------------------------------------------------------------------------
628 extern "C" XtAppContext
wxGetAppContext()
630 return (XtAppContext
)wxTheApp
->GetAppContext();