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"
32 #include "wx/cmdline.h"
35 #include "wx/thread.h"
38 #if wxUSE_WX_RESOURCES
39 #include "wx/resource.h"
43 #pragma message disable nosimpint
46 #include <X11/Xutil.h>
47 #include <X11/Xresource.h>
48 #include <X11/Xatom.h>
50 #pragma message enable nosimpint
53 #include "wx/x11/private.h"
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 wxClassInfo::InitializeClasses();
88 // GL: I'm annoyed ... I don't know where to put this and I don't want to
89 // create a module for that as it's part of the core.
91 wxPendingEventsLocker
= new wxCriticalSection();
94 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
95 wxTheColourDatabase
->Initialize();
97 wxInitializeStockLists();
98 wxInitializeStockObjects();
100 #if wxUSE_WX_RESOURCES
101 wxInitializeResourceSystem();
104 wxWidgetHashTable
= new wxHashTable(wxKEY_INTEGER
);
106 wxModule::RegisterModules();
107 if (!wxModule::InitializeModules()) return FALSE
;
112 void wxApp::CleanUp()
114 delete wxWidgetHashTable
;
115 wxWidgetHashTable
= NULL
;
117 wxModule::CleanUpModules();
119 #if wxUSE_WX_RESOURCES
120 wxCleanUpResourceSystem();
123 delete wxTheColourDatabase
;
124 wxTheColourDatabase
= NULL
;
126 wxDeleteStockObjects();
128 wxDeleteStockLists();
133 wxClassInfo::CleanUpClasses();
136 delete wxPendingEvents
;
137 delete wxPendingEventsLocker
;
140 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
141 // At this point we want to check if there are any memory
142 // blocks that aren't part of the wxDebugContext itself,
143 // as a special case. Then when dumping we need to ignore
144 // wxDebugContext, too.
145 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
147 wxLogDebug("There were memory leaks.\n");
148 wxDebugContext::Dump();
149 wxDebugContext::PrintStatistics();
153 // do it as the very last thing because everything else can log messages
154 wxLog::DontCreateOnDemand();
155 // do it as the very last thing because everything else can log messages
156 delete wxLog::SetActiveTarget(NULL
);
159 // This is set within wxEntryStart -- too early on
160 // to put these in wxTheApp
161 static int g_newArgc
= 0;
162 static wxChar
** g_newArgv
= 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__
174 // Parse the arguments.
177 Display
* xdisplay
= XOpenDisplay(NULL
);
181 wxLogError( _("wxWindows could not open display. Exiting.") );
185 wxApp::ms_display
= (WXDisplay
*) xdisplay
;
187 XSelectInput( xdisplay
, XDefaultRootWindow(xdisplay
), PropertyChangeMask
);
189 // wxSetDetectableAutoRepeat( TRUE );
191 if (!wxApp::Initialize())
202 if ( !wxTheApp
->OnInitGui() )
209 int wxEntry( int argc
, char *argv
[] )
211 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
212 // This seems to be necessary since there are 'rogue'
213 // objects present at this point (perhaps global objects?)
214 // Setting a checkpoint will ignore them as far as the
215 // memory checking facility is concerned.
216 // Of course you may argue that memory allocated in globals should be
217 // checked, but this is a reasonable compromise.
218 wxDebugContext::SetCheckpoint();
220 int err
= wxEntryStart(argc
, argv
);
226 if (!wxApp::GetInitializerFunction())
228 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
232 wxTheApp
= (wxApp
*) (* wxApp::GetInitializerFunction()) ();
237 printf( "wxWindows error: wxTheApp == NULL\n" );
241 wxTheApp
->SetClassName(wxFileNameFromPath(argv
[0]));
242 wxTheApp
->SetAppName(wxFileNameFromPath(argv
[0]));
244 // The command line may have been changed
245 // by stripping out -display etc.
248 wxTheApp
->argc
= g_newArgc
;
249 wxTheApp
->argv
= g_newArgv
;
253 wxTheApp
->argc
= argc
;
254 wxTheApp
->argv
= argv
;
258 retValue
= wxEntryInitGui();
260 // Here frames insert themselves automatically into wxTopLevelWindows by
261 // getting created in OnInit().
264 if ( !wxTheApp
->OnInit() )
270 if (wxTheApp
->Initialized()) retValue
= wxTheApp
->OnRun();
273 // flush the logged messages if any
274 wxLog
*pLog
= wxLog::GetActiveTarget();
275 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
278 delete wxLog::SetActiveTarget(new wxLogStderr
); // So dialog boxes aren't used
279 // for further messages
281 if (wxTheApp
->GetTopWindow())
283 delete wxTheApp
->GetTopWindow();
284 wxTheApp
->SetTopWindow(NULL
);
287 wxTheApp
->DeletePendingObjects();
296 // Static member initialization
297 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
304 m_wantDebugOutput
= TRUE
;
308 m_exitOnFrameDelete
= TRUE
;
309 m_mainColormap
= (WXColormap
) NULL
;
310 m_topLevelWidget
= (WXWindow
) NULL
;
311 m_maxRequestSize
= 0;
315 bool wxApp::Initialized()
323 int wxApp::MainLoop()
326 m_mainLoop
= new wxEventLoop
;
328 rt
= m_mainLoop
->Run();
335 // Processes an X event.
336 void wxApp::ProcessXEvent(WXEvent
* _event
)
338 XEvent
* event
= (XEvent
*) _event
;
340 wxWindow
* win
= NULL
;
341 Window window
= event
->xany
.window
;
342 Window actualWindow
= window
;
344 // Find the first wxWindow that corresponds to this event window
345 // TODO: may need to translate coordinates from actualWindow
346 // to window, if the receiving window != wxWindow window
347 // while (window && !(win = wxGetWindowFromTable(window)))
348 // window = wxGetWindowParent(window);
350 // Because we're receiving events after a window
351 // has been destroyed, assume a 1:1 match between
352 // Window and wxWindow, so if it's not in the table,
353 // it must have been destroyed.
355 win
= wxGetWindowFromTable(window
);
363 if (win
&& !win
->IsEnabled())
369 wxKeyEvent
keyEvent(wxEVT_KEY_DOWN
);
370 wxTranslateKeyEvent(keyEvent
, win
, window
, event
);
372 // We didn't process wxEVT_KEY_DOWN, so send
374 if (!win
->GetEventHandler()->ProcessEvent( keyEvent
))
376 keyEvent
.SetEventType(wxEVT_CHAR
);
377 win
->GetEventHandler()->ProcessEvent( keyEvent
);
380 // We intercepted and processed the key down event
388 if (win
&& !win
->IsEnabled())
393 wxKeyEvent
keyEvent(wxEVT_KEY_UP
);
394 wxTranslateKeyEvent(keyEvent
, win
, window
, event
);
396 win
->GetEventHandler()->ProcessEvent( keyEvent
);
402 HandlePropertyChange(_event
);
407 if (win
&& !win
->IsEnabled())
410 Atom wm_delete_window
= XInternAtom(wxGlobalDisplay(), "WM_DELETE_WINDOW", True
);;
411 Atom wm_protocols
= XInternAtom(wxGlobalDisplay(), "WM_PROTOCOLS", True
);;
413 if (event
->xclient
.message_type
== wm_protocols
)
415 if ((Atom
) (event
->xclient
.data
.l
[0]) == wm_delete_window
)
425 case ConfigureNotify
:
429 wxSizeEvent
sizeEvent( wxSize(event
->xconfigure
.width
,event
->xconfigure
.height
), win
->GetId() );
430 sizeEvent
.SetEventObject( win
);
432 win
->GetEventHandler()->ProcessEvent( sizeEvent
);
437 /* Terry Gitnick <terryg@scientech.com> - 1/21/98
438 * If resize event, don't resize until the last resize event for this
439 * window is recieved. Prevents flicker as windows are resized.
442 Display
*disp
= (Display
*) wxGetDisplay();
447 while( XCheckTypedWindowEvent (disp
, actualWindow
, ResizeRequest
, &report
));
449 // TODO: when implementing refresh optimization, we can use
450 // XtAddExposureToRegion to expand the window's paint region.
454 wxSize sz
= win
->GetSize();
455 wxSizeEvent
sizeEvent(sz
, win
->GetId());
456 sizeEvent
.SetEventObject(win
);
458 win
->GetEventHandler()->ProcessEvent( sizeEvent
);
467 // Schedule update for later
468 win
->GetUpdateRegion().Union( event
->xexpose
.x
, event
->xexpose
.y
,
469 event
->xexpose
.width
, event
->xexpose
.height
);
480 if (win
&& !win
->IsEnabled())
485 wxMouseEvent wxevent
;
486 wxTranslateMouseEvent(wxevent
, win
, window
, event
);
487 win
->GetEventHandler()->ProcessEvent( wxevent
);
493 if (win
&& event
->xfocus
.detail
!= NotifyPointer
)
495 wxFocusEvent
focusEvent(wxEVT_SET_FOCUS
, win
->GetId());
496 focusEvent
.SetEventObject(win
);
497 win
->GetEventHandler()->ProcessEvent(focusEvent
);
503 if (win
&& event
->xfocus
.detail
!= NotifyPointer
)
505 wxFocusEvent
focusEvent(wxEVT_KILL_FOCUS
, win
->GetId());
506 focusEvent
.SetEventObject(win
);
507 win
->GetEventHandler()->ProcessEvent(focusEvent
);
513 // Do we want to process this (for top-level windows)?
514 // But we want to be able to veto closes, anyway
520 //wxString eventName = wxGetXEventName(XEvent& event);
521 //wxLogDebug(wxT("Event %s not handled"), eventName.c_str());
528 // Returns TRUE if more time is needed.
529 // Note that this duplicates wxEventLoopImpl::SendIdleEvent
530 // but ProcessIdle may be needed by apps, so is kept.
531 bool wxApp::ProcessIdle()
534 event
.SetEventObject(this);
537 return event
.MoreRequested();
540 void wxApp::ExitMainLoop()
546 // Is a message/event pending?
547 bool wxApp::Pending()
549 return wxEventLoop::GetActive()->Pending();
552 // Dispatch a message.
553 void wxApp::Dispatch()
555 wxEventLoop::GetActive()->Dispatch();
558 // This should be redefined in a derived class for
559 // handling property change events for XAtom IPC.
560 void wxApp::HandlePropertyChange(WXEvent
*event
)
562 // by default do nothing special
563 // TODO: what to do for X11
564 // XtDispatchEvent((XEvent*) event); /* let Motif do the work */
567 void wxApp::OnIdle(wxIdleEvent
& event
)
569 static bool s_inOnIdle
= FALSE
;
571 // Avoid recursion (via ProcessEvent default case)
577 // Resend in the main thread events which have been prepared in other
579 ProcessPendingEvents();
581 // 'Garbage' collection of windows deleted with Close()
582 DeletePendingObjects();
584 // Send OnIdle events to all windows
585 bool needMore
= SendIdleEvents();
588 event
.RequestMore(TRUE
);
595 // **** please implement me! ****
596 // Wake up the idle handler processor, even if it is in another thread...
600 // Send idle event to all top-level windows
601 bool wxApp::SendIdleEvents()
603 bool needMore
= FALSE
;
605 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
608 wxWindow
* win
= node
->GetData();
609 if (SendIdleEvents(win
))
611 node
= node
->GetNext();
617 // Send idle event to window and all subwindows
618 bool wxApp::SendIdleEvents(wxWindow
* win
)
620 bool needMore
= FALSE
;
623 event
.SetEventObject(win
);
625 win
->GetEventHandler()->ProcessEvent(event
);
627 win
->OnInternalIdle();
629 if (event
.MoreRequested())
632 wxNode
* node
= win
->GetChildren().First();
635 wxWindow
* win
= (wxWindow
*) node
->Data();
636 if (SendIdleEvents(win
))
645 void wxApp::DeletePendingObjects()
647 wxNode
*node
= wxPendingDelete
.First();
650 wxObject
*obj
= (wxObject
*)node
->Data();
654 if (wxPendingDelete
.Member(obj
))
657 // Deleting one object may have deleted other pending
658 // objects, so start from beginning of list again.
659 node
= wxPendingDelete
.First();
663 // Create an application context
664 bool wxApp::OnInitGui()
666 // Eventually this line will be removed, but for
667 // now we don't want to try popping up a dialog
668 // for error messages.
669 delete wxLog::SetActiveTarget(new wxLogStderr
);
671 if (!wxAppBase::OnInitGui())
675 GetMainColormap( wxApp::GetDisplay() );
676 m_maxRequestSize
= XMaxRequestSize( (Display
*) wxApp::GetDisplay() );
681 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
683 if (!display
) /* Must be called first with non-NULL display */
684 return m_mainColormap
;
686 int defaultScreen
= DefaultScreen((Display
*) display
);
687 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
689 Colormap c
= DefaultColormapOfScreen(screen
);
692 m_mainColormap
= (WXColormap
) c
;
694 return (WXColormap
) c
;
697 Window
wxGetWindowParent(Window window
)
699 wxASSERT_MSG( window
, "invalid window" );
703 Window parent
, root
= 0;
704 unsigned int noChildren
= 0;
705 Window
* children
= NULL
;
706 int res
= XQueryTree((Display
*) wxGetDisplay(), window
, & root
, & parent
,
707 & children
, & noChildren
);
720 retValue
= wxTheApp
->OnExit();
724 * Exit in some platform-specific way. Not recommended that the app calls this:
725 * only for emergencies.
730 // Yield to other processes
732 bool wxApp::Yield(bool onlyIfNeeded
)
734 bool s_inYield
= FALSE
;
740 wxFAIL_MSG( wxT("wxYield called recursively" ) );
748 while (wxTheApp
&& wxTheApp
->Pending())
749 wxTheApp
->Dispatch();
756 // TODO use XmGetPixmap (?) to get the really standard icons!
758 // XPM hack: make the arrays const
759 #define static static const
761 #include "wx/generic/info.xpm"
762 #include "wx/generic/error.xpm"
763 #include "wx/generic/question.xpm"
764 #include "wx/generic/warning.xpm"
769 wxApp::GetStdIcon(int which
) const
773 case wxICON_INFORMATION
:
774 return wxIcon(info_xpm
);
776 case wxICON_QUESTION
:
777 return wxIcon(question_xpm
);
779 case wxICON_EXCLAMATION
:
780 return wxIcon(warning_xpm
);
783 wxFAIL_MSG("requested non existent standard icon");
784 // still fall through
787 return wxIcon(error_xpm
);
791 void wxApp::OnAssert(const wxChar
*file
, int line
, const wxChar
*msg
)
793 // While the GUI isn't working that well, just print out the
796 wxAppBase::OnAssert(file
, line
, msg
);
799 msg2
.Printf("At file %s:%d: %s", file
, line
, msg
);