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(int argc
, wxChar
**argv
)
91 if ( !wxAppBase::Initialize(argc
, argv
) )
94 wxWidgetHashTable
= new wxHashTable(wxKEY_INTEGER
);
101 delete wxWidgetHashTable
;
102 wxWidgetHashTable
= NULL
;
104 wxAppBase::CleanUp();
111 wxAppConsole::Exit();
114 // ============================================================================
116 // ============================================================================
118 int wxEntryStart( int argc
, char* argv
[] )
120 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
121 // This seems to be necessary since there are 'rogue'
122 // objects present at this point (perhaps global objects?)
123 // Setting a checkpoint will ignore them as far as the
124 // memory checking facility is concerned.
125 // Of course you may argue that memory allocated in globals should be
126 // checked, but this is a reasonable compromise.
127 wxDebugContext::SetCheckpoint();
130 if (!wxApp::Initialize())
140 // GUI-specific initialization, such as creating an app context.
141 if (!wxTheApp
->OnInitGui())
147 void wxEntryCleanup()
149 // So dialog boxes aren't used for further messages
150 delete wxLog::SetActiveTarget(new wxLogStderr
);
152 // flush the logged messages if any
153 wxLog
*pLog
= wxLog::GetActiveTarget();
154 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
160 int wxEntry( int argc
, char *argv
[] )
164 retValue
= wxEntryStart( argc
, argv
);
165 if (retValue
) return retValue
;
169 if (!wxApp::GetInitializerFunction())
171 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
175 wxTheApp
= (wxApp
*) (* wxApp::GetInitializerFunction()) ();
180 printf( "wxWindows error: wxTheApp == NULL\n" );
184 wxTheApp
->SetClassName(wxFileNameFromPath(argv
[0]));
185 wxTheApp
->SetAppName(wxFileNameFromPath(argv
[0]));
187 wxTheApp
->argc
= argc
;
188 wxTheApp
->argv
= argv
;
190 // GUI-specific initialization, such as creating an app context.
191 retValue
= wxEntryInitGui();
192 if (retValue
) return retValue
;
194 // Here frames insert themselves automatically into wxTopLevelWindows by
195 // getting created in OnInit().
197 if (wxTheApp
->OnInit())
199 if (wxTheApp
->Initialized())
203 if (wxTheApp
->GetTopWindow())
205 delete wxTheApp
->GetTopWindow();
206 wxTheApp
->SetTopWindow(NULL
);
209 wxTheApp
->DeletePendingObjects();
211 retValue
= wxTheApp
->OnExit();
223 m_eventLoop
= new wxEventLoop
;
224 m_mainColormap
= (WXColormap
) NULL
;
225 m_appContext
= (WXAppContext
) NULL
;
226 m_initialDisplay
= (WXDisplay
*) 0;
227 m_perDisplayData
= new wxPerDisplayDataMap
;
234 for( wxPerDisplayDataMap::iterator it
= m_perDisplayData
->begin(),
235 end
= m_perDisplayData
->end();
238 delete it
->second
.m_visualInfo
;
239 XtDestroyWidget( it
->second
.m_topLevelWidget
);
242 delete m_perDisplayData
;
247 bool wxApp::Initialized()
255 int wxApp::MainLoop()
258 * Sit around forever waiting to process X-events. Property Change
259 * event are handled special, because they have to refer to
260 * the root window rather than to a widget. therefore we can't
261 * use an Xt-eventhandler.
264 XSelectInput(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()),
265 XDefaultRootWindow(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget())),
273 // Processes an idle event.
274 // Returns TRUE if more time is needed.
275 bool wxApp::ProcessIdle()
279 return ProcessEvent(event
) && event
.MoreRequested();
282 void wxApp::ExitMainLoop()
284 if( m_eventLoop
->IsRunning() )
288 // Is a message/event pending?
289 bool wxApp::Pending()
291 return m_eventLoop
->Pending();
293 XFlush(XtDisplay( (Widget
) wxTheApp
->GetTopLevelWidget() ));
295 // Fix by Doug from STI, to prevent a stall if non-X event
297 return ((XtAppPending( (XtAppContext
) GetAppContext() ) & XtIMXEvent
) != 0) ;
301 // Dispatch a message.
302 void wxApp::Dispatch()
304 m_eventLoop
->Dispatch();
307 // This should be redefined in a derived class for
308 // handling property change events for XAtom IPC.
309 void wxApp::HandlePropertyChange(WXEvent
*event
)
311 // by default do nothing special
312 XtDispatchEvent((XEvent
*) event
); /* let Motif do the work */
315 void wxApp::OnIdle(wxIdleEvent
& event
)
317 static bool inOnIdle
= FALSE
;
319 // Avoid recursion (via ProcessEvent default case)
325 // If there are pending events, we must process them: pending events
326 // are either events to the threads other than main or events posted
327 // with wxPostEvent() functions
328 // GRG: I have moved this here so that all pending events are processed
329 // before starting to delete any objects. This behaves better (in
330 // particular, wrt wxPostEvent) and is coherent with wxGTK's current
331 // behaviour. Also removed the '#if wxUSE_THREADS' around it.
332 // Changed Mar/2000 before 2.1.14
334 // Flush pending events.
335 ProcessPendingEvents();
337 // 'Garbage' collection of windows deleted with Close().
338 DeletePendingObjects();
340 // flush the logged messages if any
341 wxLog
*pLog
= wxLog::GetActiveTarget();
342 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
345 // Send OnIdle events to all windows
346 bool needMore
= SendIdleEvents();
349 event
.RequestMore(TRUE
);
354 // Send idle event to all top-level windows
355 bool wxApp::SendIdleEvents()
357 bool needMore
= FALSE
;
359 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
362 wxWindow
* win
= node
->GetData();
363 if (SendIdleEvents(win
))
365 node
= node
->GetNext();
371 // Send idle event to window and all subwindows
372 bool wxApp::SendIdleEvents(wxWindow
* win
)
374 bool needMore
= FALSE
;
377 event
.SetEventObject(win
);
378 win
->GetEventHandler()->ProcessEvent(event
);
380 if (event
.MoreRequested())
383 wxWindowList::Node
* node
= win
->GetChildren().GetFirst();
386 wxWindow
* win
= node
->GetData();
387 if (SendIdleEvents(win
))
390 node
= node
->GetNext();
395 static char *fallbackResources
[] = {
396 "*menuBar.marginHeight: 0",
397 "*menuBar.shadowThickness: 1",
398 "*background: #c0c0c0",
399 "*foreground: black",
403 // Create an application context
404 bool wxApp::OnInitGui()
406 if( !wxAppBase::OnInitGui() )
409 XtToolkitInitialize() ;
410 wxTheApp
->m_appContext
= (WXAppContext
) XtCreateApplicationContext();
411 XtAppSetFallbackResources((XtAppContext
) wxTheApp
->m_appContext
, fallbackResources
);
413 Display
*dpy
= XtOpenDisplay((XtAppContext
) wxTheApp
->m_appContext
,(String
)NULL
,NULL
,
414 wxTheApp
->GetClassName().c_str(), NULL
, 0,
415 # if XtSpecificationRelease < 5
423 // if you don't log to stderr, nothing will be shown...
424 delete wxLog::SetActiveTarget(new wxLogStderr
);
425 wxString
className(wxTheApp
->GetClassName());
426 wxLogError(_("wxWindows could not open display for '%s': exiting."),
430 m_initialDisplay
= (WXDisplay
*) dpy
;
433 // install the X error handler
434 gs_pfnXErrorHandler
= XSetErrorHandler(wxXErrorHandler
);
435 #endif // __WXDEBUG__
437 // Add general resize proc
439 rec
.string
= "resize";
440 rec
.proc
= (XtActionProc
)wxWidgetResizeProc
;
441 XtAppAddActions((XtAppContext
) wxTheApp
->m_appContext
, &rec
, 1);
443 GetMainColormap(dpy
);
450 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
452 if (!display
) /* Must be called first with non-NULL display */
453 return m_mainColormap
;
455 int defaultScreen
= DefaultScreen((Display
*) display
);
456 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
458 Colormap c
= DefaultColormapOfScreen(screen
);
461 m_mainColormap
= (WXColormap
) c
;
463 return (WXColormap
) c
;
466 wxXVisualInfo
* wxApp::GetVisualInfo( WXDisplay
* display
)
468 wxPerDisplayDataMap::iterator it
= m_perDisplayData
->find( display
);
470 if( it
!= m_perDisplayData
->end() && it
->second
.m_visualInfo
)
471 return it
->second
.m_visualInfo
;
473 wxXVisualInfo
* vi
= new wxXVisualInfo
;
474 wxFillXVisualInfo( vi
, (Display
*)display
);
476 (*m_perDisplayData
)[display
].m_visualInfo
= vi
;
481 static void wxTLWidgetDestroyCallback(Widget w
, XtPointer clientData
,
485 wxTheApp
->SetTopLevelWidget( (WXDisplay
*)XtDisplay(w
),
489 WXWidget
wxCreateTopLevelWidget( WXDisplay
* display
)
491 Widget tlw
= XtAppCreateShell( (String
)NULL
,
492 wxTheApp
->GetClassName().c_str(),
493 applicationShellWidgetClass
,
496 XtSetMappedWhenManaged( tlw
, False
);
497 XtRealizeWidget( tlw
);
499 XtAddCallback( tlw
, XmNdestroyCallback
,
500 (XtCallbackProc
)wxTLWidgetDestroyCallback
,
503 return (WXWidget
)tlw
;
506 WXWidget
wxApp::GetTopLevelWidget()
508 WXDisplay
* display
= wxGetDisplay();
509 wxPerDisplayDataMap::iterator it
= m_perDisplayData
->find( display
);
511 if( it
!= m_perDisplayData
->end() && it
->second
.m_topLevelWidget
)
512 return (WXWidget
)it
->second
.m_topLevelWidget
;
514 WXWidget tlw
= wxCreateTopLevelWidget( display
);
515 SetTopLevelWidget( display
, tlw
);
520 void wxApp::SetTopLevelWidget(WXDisplay
* display
, WXWidget widget
)
522 (*m_perDisplayData
)[display
].m_topLevelWidget
= (Widget
)widget
;
525 // Yield to other processes
527 bool wxApp::Yield(bool onlyIfNeeded
)
529 bool s_inYield
= FALSE
;
535 wxFAIL_MSG( wxT("wxYield called recursively" ) );
543 while (wxTheApp
&& wxTheApp
->Pending())
544 wxTheApp
->Dispatch();
551 // ----------------------------------------------------------------------------
552 // accessors for C modules
553 // ----------------------------------------------------------------------------
555 extern "C" XtAppContext
wxGetAppContext()
557 return (XtAppContext
)wxTheApp
->GetAppContext();