1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "app.h"
19 #include "wx/gdicmn.h"
22 #include "wx/cursor.h"
24 #include "wx/dialog.h"
25 #include "wx/msgdlg.h"
27 #include "wx/module.h"
28 #include "wx/memory.h"
31 #include "wx/evtloop.h"
34 #include "wx/thread.h"
37 #if wxUSE_WX_RESOURCES
38 #include "wx/resource.h"
42 #pragma message disable nosimpint
45 #include <X11/Xutil.h>
46 #include <X11/Xresource.h>
47 #include <X11/Xatom.h>
49 #pragma message enable nosimpint
52 #include "wx/x11/private.h"
56 extern char *wxBuffer
;
57 extern wxList wxPendingDelete
;
59 wxApp
*wxTheApp
= NULL
;
61 wxHashTable
*wxWidgetHashTable
= NULL
;
63 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
65 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
66 EVT_IDLE(wxApp::OnIdle
)
70 typedef int (*XErrorHandlerFunc
)(Display
*, XErrorEvent
*);
72 XErrorHandlerFunc gs_pfnXErrorHandler
= 0;
74 static int wxXErrorHandler(Display
*dpy
, XErrorEvent
*xevent
)
76 // just forward to the default handler for now
77 return gs_pfnXErrorHandler(dpy
, xevent
);
81 long wxApp::sm_lastMessageTime
= 0;
82 WXDisplay
*wxApp::ms_display
= NULL
;
84 bool wxApp::Initialize()
86 wxBuffer
= new char[BUFSIZ
+ 512];
88 wxClassInfo::InitializeClasses();
90 // GL: I'm annoyed ... I don't know where to put this and I don't want to
91 // create a module for that as it's part of the core.
93 wxPendingEventsLocker
= new wxCriticalSection();
96 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
97 wxTheColourDatabase
->Initialize();
99 wxInitializeStockLists();
100 wxInitializeStockObjects();
102 #if wxUSE_WX_RESOURCES
103 wxInitializeResourceSystem();
106 wxWidgetHashTable
= new wxHashTable(wxKEY_INTEGER
);
108 wxModule::RegisterModules();
109 if (!wxModule::InitializeModules()) return FALSE
;
114 void wxApp::CleanUp()
116 delete wxWidgetHashTable
;
117 wxWidgetHashTable
= NULL
;
119 wxModule::CleanUpModules();
121 #if wxUSE_WX_RESOURCES
122 wxCleanUpResourceSystem();
125 delete wxTheColourDatabase
;
126 wxTheColourDatabase
= NULL
;
128 wxDeleteStockObjects();
130 wxDeleteStockLists();
138 wxClassInfo::CleanUpClasses();
141 delete wxPendingEvents
;
142 delete wxPendingEventsLocker
;
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 // NB: argc and argv may be changed here, pass by reference!
165 int wxEntryStart( int& argc
, char *argv
[] )
168 // install the X error handler
169 gs_pfnXErrorHandler
= XSetErrorHandler( wxXErrorHandler
);
170 #endif // __WXDEBUG__
172 Display
* xdisplay
= XOpenDisplay(NULL
);
176 wxLogError( _("wxWindows could not open display. Exiting.") );
180 wxApp::ms_display
= (WXDisplay
*) xdisplay
;
182 XSelectInput( xdisplay
, XDefaultRootWindow(xdisplay
), PropertyChangeMask
);
184 // wxSetDetectableAutoRepeat( TRUE );
186 if (!wxApp::Initialize())
197 if ( !wxTheApp
->OnInitGui() )
204 int wxEntry( int argc
, char *argv
[] )
206 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
207 // This seems to be necessary since there are 'rogue'
208 // objects present at this point (perhaps global objects?)
209 // Setting a checkpoint will ignore them as far as the
210 // memory checking facility is concerned.
211 // Of course you may argue that memory allocated in globals should be
212 // checked, but this is a reasonable compromise.
213 wxDebugContext::SetCheckpoint();
215 int err
= wxEntryStart(argc
, argv
);
221 if (!wxApp::GetInitializerFunction())
223 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
227 wxTheApp
= (wxApp
*) (* wxApp::GetInitializerFunction()) ();
232 printf( "wxWindows error: wxTheApp == NULL\n" );
236 wxTheApp
->SetClassName(wxFileNameFromPath(argv
[0]));
237 wxTheApp
->SetAppName(wxFileNameFromPath(argv
[0]));
239 wxTheApp
->argc
= argc
;
240 wxTheApp
->argv
= argv
;
243 retValue
= wxEntryInitGui();
245 // Here frames insert themselves automatically into wxTopLevelWindows by
246 // getting created in OnInit().
249 if ( !wxTheApp
->OnInit() )
255 if (wxTheApp
->Initialized()) retValue
= wxTheApp
->OnRun();
258 // flush the logged messages if any
259 wxLog
*pLog
= wxLog::GetActiveTarget();
260 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
263 delete wxLog::SetActiveTarget(new wxLogStderr
); // So dialog boxes aren't used
264 // for further messages
266 if (wxTheApp
->GetTopWindow())
268 delete wxTheApp
->GetTopWindow();
269 wxTheApp
->SetTopWindow(NULL
);
272 wxTheApp
->DeletePendingObjects();
281 // Static member initialization
282 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
289 m_wantDebugOutput
= TRUE
;
293 m_exitOnFrameDelete
= TRUE
;
294 m_mainColormap
= (WXColormap
) NULL
;
295 m_topLevelWidget
= (WXWindow
) NULL
;
296 m_maxRequestSize
= 0;
300 bool wxApp::Initialized()
308 int wxApp::MainLoop()
311 m_mainLoop
= new wxEventLoop
;
313 rt
= m_mainLoop
->Run();
320 // Processes an X event.
321 void wxApp::ProcessXEvent(WXEvent
* _event
)
323 XEvent
* event
= (XEvent
*) _event
;
325 wxWindow
* win
= NULL
;
326 Window window
= event
->xany
.window
;
327 Window actualWindow
= window
;
329 // Find the first wxWindow that corresponds to this event window
330 // TODO: may need to translate coordinates from actualWindow
331 // to window, if the receiving window != wxWindow window
332 // while (window && !(win = wxGetWindowFromTable(window)))
333 // window = wxGetWindowParent(window);
335 // Because we're receiving events after a window
336 // has been destroyed, assume a 1:1 match between
337 // Window and wxWindow, so if it's not in the table,
338 // it must have been destroyed.
340 win
= wxGetWindowFromTable(window
);
348 if (win
&& !win
->IsEnabled())
354 wxKeyEvent
keyEvent(wxEVT_KEY_DOWN
);
355 wxTranslateKeyEvent(keyEvent
, win
, window
, event
);
357 // We didn't process wxEVT_KEY_DOWN, so send
359 if (!win
->GetEventHandler()->ProcessEvent( keyEvent
))
361 keyEvent
.SetEventType(wxEVT_CHAR
);
362 win
->GetEventHandler()->ProcessEvent( keyEvent
);
365 // We intercepted and processed the key down event
373 if (win
&& !win
->IsEnabled())
378 wxKeyEvent
keyEvent(wxEVT_KEY_UP
);
379 wxTranslateKeyEvent(keyEvent
, win
, window
, event
);
381 win
->GetEventHandler()->ProcessEvent( keyEvent
);
387 HandlePropertyChange(_event
);
392 Atom wm_delete_window
= XInternAtom(wxGlobalDisplay(), "WM_DELETE_WINDOW", True
);;
393 Atom wm_protocols
= XInternAtom(wxGlobalDisplay(), "WM_PROTOCOLS", True
);;
395 if (event
->xclient
.message_type
== wm_protocols
)
397 if ((Atom
) (event
->xclient
.data
.l
[0]) == wm_delete_window
)
407 case ConfigureNotify
:
411 wxSizeEvent
sizeEvent( wxSize(event
->xconfigure
.width
,event
->xconfigure
.height
), win
->GetId() );
412 sizeEvent
.SetEventObject( win
);
414 win
->GetEventHandler()->ProcessEvent( sizeEvent
);
419 /* Terry Gitnick <terryg@scientech.com> - 1/21/98
420 * If resize event, don't resize until the last resize event for this
421 * window is recieved. Prevents flicker as windows are resized.
424 Display
*disp
= (Display
*) wxGetDisplay();
429 while( XCheckTypedWindowEvent (disp
, actualWindow
, ResizeRequest
, &report
));
431 // TODO: when implementing refresh optimization, we can use
432 // XtAddExposureToRegion to expand the window's paint region.
436 wxSize sz
= win
->GetSize();
437 wxSizeEvent
sizeEvent(sz
, win
->GetId());
438 sizeEvent
.SetEventObject(win
);
440 win
->GetEventHandler()->ProcessEvent( sizeEvent
);
449 // Schedule update for later
450 win
->GetUpdateRegion().Union( event
->xexpose
.x
, event
->xexpose
.y
,
451 event
->xexpose
.width
, event
->xexpose
.height
);
462 if (win
&& !win
->IsEnabled())
467 wxMouseEvent wxevent
;
468 wxTranslateMouseEvent(wxevent
, win
, window
, event
);
469 win
->GetEventHandler()->ProcessEvent( wxevent
);
475 if (win
&& event
->xfocus
.detail
!= NotifyPointer
)
477 wxFocusEvent
focusEvent(wxEVT_SET_FOCUS
, win
->GetId());
478 focusEvent
.SetEventObject(win
);
479 win
->GetEventHandler()->ProcessEvent(focusEvent
);
485 if (win
&& event
->xfocus
.detail
!= NotifyPointer
)
487 wxFocusEvent
focusEvent(wxEVT_KILL_FOCUS
, win
->GetId());
488 focusEvent
.SetEventObject(win
);
489 win
->GetEventHandler()->ProcessEvent(focusEvent
);
495 // Do we want to process this (for top-level windows)?
496 // But we want to be able to veto closes, anyway
506 // Returns TRUE if more time is needed.
507 // Note that this duplicates wxEventLoopImpl::SendIdleEvent
508 // but ProcessIdle may be needed by apps, so is kept.
509 bool wxApp::ProcessIdle()
512 event
.SetEventObject(this);
515 return event
.MoreRequested();
518 void wxApp::ExitMainLoop()
524 // Is a message/event pending?
525 bool wxApp::Pending()
527 return wxEventLoop::GetActive()->Pending();
530 // Dispatch a message.
531 void wxApp::Dispatch()
533 wxEventLoop::GetActive()->Dispatch();
536 // This should be redefined in a derived class for
537 // handling property change events for XAtom IPC.
538 void wxApp::HandlePropertyChange(WXEvent
*event
)
540 // by default do nothing special
541 // TODO: what to do for X11
542 // XtDispatchEvent((XEvent*) event); /* let Motif do the work */
545 void wxApp::OnIdle(wxIdleEvent
& event
)
547 static bool s_inOnIdle
= FALSE
;
549 // Avoid recursion (via ProcessEvent default case)
555 // Resend in the main thread events which have been prepared in other
557 ProcessPendingEvents();
559 // 'Garbage' collection of windows deleted with Close()
560 DeletePendingObjects();
562 // Send OnIdle events to all windows
563 bool needMore
= SendIdleEvents();
566 event
.RequestMore(TRUE
);
573 // **** please implement me! ****
574 // Wake up the idle handler processor, even if it is in another thread...
578 // Send idle event to all top-level windows
579 bool wxApp::SendIdleEvents()
581 bool needMore
= FALSE
;
583 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
586 wxWindow
* win
= node
->GetData();
587 if (SendIdleEvents(win
))
589 node
= node
->GetNext();
595 // Send idle event to window and all subwindows
596 bool wxApp::SendIdleEvents(wxWindow
* win
)
598 bool needMore
= FALSE
;
601 event
.SetEventObject(win
);
603 win
->GetEventHandler()->ProcessEvent(event
);
605 win
->OnInternalIdle();
607 if (event
.MoreRequested())
610 wxNode
* node
= win
->GetChildren().First();
613 wxWindow
* win
= (wxWindow
*) node
->Data();
614 if (SendIdleEvents(win
))
623 void wxApp::DeletePendingObjects()
625 wxNode
*node
= wxPendingDelete
.First();
628 wxObject
*obj
= (wxObject
*)node
->Data();
632 if (wxPendingDelete
.Member(obj
))
635 // Deleting one object may have deleted other pending
636 // objects, so start from beginning of list again.
637 node
= wxPendingDelete
.First();
641 // Create an application context
642 bool wxApp::OnInitGui()
644 // Eventually this line will be removed, but for
645 // now we don't want to try popping up a dialog
646 // for error messages.
647 delete wxLog::SetActiveTarget(new wxLogStderr
);
649 if (!wxAppBase::OnInitGui())
653 GetMainColormap( wxApp::GetDisplay() );
654 m_maxRequestSize
= XMaxRequestSize( (Display
*) wxApp::GetDisplay() );
659 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
661 if (!display
) /* Must be called first with non-NULL display */
662 return m_mainColormap
;
664 int defaultScreen
= DefaultScreen((Display
*) display
);
665 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
667 Colormap c
= DefaultColormapOfScreen(screen
);
670 m_mainColormap
= (WXColormap
) c
;
672 return (WXColormap
) c
;
675 Window
wxGetWindowParent(Window window
)
677 Window parent
, root
= 0;
678 unsigned int noChildren
= 0;
679 Window
* children
= NULL
;
680 int res
= XQueryTree((Display
*) wxGetDisplay(), window
, & root
, & parent
,
681 & children
, & noChildren
);
694 retValue
= wxTheApp
->OnExit();
698 * Exit in some platform-specific way. Not recommended that the app calls this:
699 * only for emergencies.
704 // Yield to other processes
706 bool wxApp::Yield(bool onlyIfNeeded
)
708 bool s_inYield
= FALSE
;
714 wxFAIL_MSG( wxT("wxYield called recursively" ) );
722 while (wxTheApp
&& wxTheApp
->Pending())
723 wxTheApp
->Dispatch();
730 // TODO use XmGetPixmap (?) to get the really standard icons!
732 // XPM hack: make the arrays const
733 #define static static const
735 #include "wx/generic/info.xpm"
736 #include "wx/generic/error.xpm"
737 #include "wx/generic/question.xpm"
738 #include "wx/generic/warning.xpm"
743 wxApp::GetStdIcon(int which
) const
747 case wxICON_INFORMATION
:
748 return wxIcon(info_xpm
);
750 case wxICON_QUESTION
:
751 return wxIcon(question_xpm
);
753 case wxICON_EXCLAMATION
:
754 return wxIcon(warning_xpm
);
757 wxFAIL_MSG("requested non existent standard icon");
758 // still fall through
761 return wxIcon(error_xpm
);
765 void wxApp::OnAssert(const wxChar
*file
, int line
, const wxChar
*msg
)
767 // While the GUI isn't working that well, just print out the
770 wxAppBase::OnAssert(file
, line
, msg
);
773 msg2
.Printf("At file %s:%d: %s", file
, line
, msg
);
778 // ----------------------------------------------------------------------------
779 // accessors for C modules
780 // ----------------------------------------------------------------------------
783 extern "C" XtAppContext
wxGetAppContext()
785 return (XtAppContext
)wxTheApp
->GetAppContext();