From 6c20e8f816b21b911e3a8ed8197c21df6ce726d6 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 25 Jul 2006 01:31:13 +0000 Subject: [PATCH] move default button handling code from wxControlContainer to wxTLW (patch 1524441) git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@40307 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/containr.h | 60 +++--------------------------------- include/wx/toplevel.h | 34 +++++++++++++++++++- include/wx/window.h | 17 ---------- src/common/containr.cpp | 11 +------ src/common/dlgcmn.cpp | 2 +- src/common/pickerbase.cpp | 2 +- src/common/toplvcmn.cpp | 13 ++++++++ src/common/wincmn.cpp | 5 --- src/generic/panelg.cpp | 2 +- src/generic/splitter.cpp | 2 +- src/gtk/button.cpp | 6 ++-- src/gtk1/button.cpp | 6 ++-- src/mac/carbon/app.cpp | 16 ++++++---- src/mac/carbon/button.cpp | 9 +++--- src/mac/carbon/combobox.cpp | 11 +++---- src/mac/carbon/combobxc.cpp | 10 ++---- src/mac/carbon/spinctrl.cpp | 2 +- src/mac/carbon/textctrl.cpp | 11 ++----- src/mac/carbon/window.cpp | 8 ++--- src/mac/classic/app.cpp | 37 ++++++++++++---------- src/mac/classic/button.cpp | 9 +++--- src/mac/classic/combobox.cpp | 10 ++---- src/mac/classic/control.cpp | 8 ++--- src/mac/classic/listbox.cpp | 10 ++---- src/mac/classic/textctrl.cpp | 10 ++---- src/motif/button.cpp | 7 +++-- src/msw/button.cpp | 38 +++++++++++------------ src/msw/window.cpp | 15 +++++---- src/os2/button.cpp | 30 +++++++++--------- src/os2/window.cpp | 10 ++++-- 30 files changed, 181 insertions(+), 230 deletions(-) diff --git a/include/wx/containr.h b/include/wx/containr.h index 34823cfab3..1bf91cfef5 100644 --- a/include/wx/containr.h +++ b/include/wx/containr.h @@ -38,28 +38,6 @@ public: wxControlContainer(wxWindow *winParent = NULL); void SetContainerWindow(wxWindow *winParent) { m_winParent = winParent; } - // default item access: we have a permanent default item which is the one - // set by the user code but we may also have a temporary default item which - // would be chosen if the user pressed "Enter" now but the default action - // reverts to the "permanent" default as soon as this temporary default - // item lsoes focus - - // get the default item, temporary or permanent - wxWindow *GetDefaultItem() const - { return m_winTmpDefault ? m_winTmpDefault : m_winDefault; } - - // set the permanent default item, return its old value - wxWindow *SetDefaultItem(wxWindow *win) - { wxWindow *winOld = m_winDefault; m_winDefault = win; return winOld; } - - // set a temporary default item, SetTmpDefaultItem(NULL) should be called - // soon after a call to SetTmpDefaultItem(window) - void SetTmpDefaultItem(wxWindow *win) { m_winTmpDefault = win; } - - // return the temporary default item, can be NULL - wxWindow *GetTmpDefaultItem() const { return m_winTmpDefault; } - - // the methods to be called from the window event handlers void HandleOnNavigationKey(wxNavigationKeyEvent& event); void HandleOnFocus(wxFocusEvent& event); @@ -86,12 +64,6 @@ protected: // the child which had the focus last time this panel was activated wxWindow *m_winLastFocused; - // a default window (usually a button) or NULL - wxWindow *m_winDefault; - - // a temporary override of m_winDefault, use the latter if NULL - wxWindow *m_winTmpDefault; - // a guard against infinite recursion bool m_inSetFocus; @@ -115,10 +87,6 @@ public: \ virtual void SetFocus(); \ virtual void SetFocusIgnoringChildren(); \ virtual void RemoveChild(wxWindowBase *child); \ - virtual wxWindow *GetDefaultItem() const; \ - virtual wxWindow *SetDefaultItem(wxWindow *child); \ - virtual void SetTmpDefaultItem(wxWindow *win); \ - virtual wxWindow *GetTmpDefaultItem() const; \ virtual bool AcceptsFocus() const; \ \ protected: \ @@ -131,27 +99,7 @@ protected: \ EVT_NAVIGATION_KEY(classname::OnNavigationKey) // implement the methods forwarding to the wxControlContainer -#define WX_DELEGATE_TO_CONTROL_CONTAINER(classname) \ -wxWindow *classname::SetDefaultItem(wxWindow *child) \ -{ \ - return m_container.SetDefaultItem(child); \ -} \ - \ -void classname::SetTmpDefaultItem(wxWindow *child) \ -{ \ - m_container.SetTmpDefaultItem(child); \ -} \ - \ -wxWindow *classname::GetDefaultItem() const \ -{ \ - return m_container.GetDefaultItem(); \ -} \ - \ -wxWindow *classname::GetTmpDefaultItem() const \ -{ \ - return m_container.GetTmpDefaultItem(); \ -} \ - \ +#define WX_DELEGATE_TO_CONTROL_CONTAINER(classname, basename) \ void classname::OnNavigationKey( wxNavigationKeyEvent& event ) \ { \ m_container.HandleOnNavigationKey(event); \ @@ -161,18 +109,18 @@ void classname::RemoveChild(wxWindowBase *child) \ { \ m_container.HandleOnWindowDestroy(child); \ \ - wxWindow::RemoveChild(child); \ + basename::RemoveChild(child); \ } \ \ void classname::SetFocus() \ { \ if ( !m_container.DoSetFocus() ) \ - wxWindow::SetFocus(); \ + basename::SetFocus(); \ } \ \ void classname::SetFocusIgnoringChildren() \ { \ - wxWindow::SetFocus(); \ + basename::SetFocus(); \ } \ \ void classname::OnChildFocus(wxChildFocusEvent& event) \ diff --git a/include/wx/toplevel.h b/include/wx/toplevel.h index 8881631c5c..e9be5bb460 100644 --- a/include/wx/toplevel.h +++ b/include/wx/toplevel.h @@ -200,7 +200,33 @@ public: virtual bool SetTransparent(wxByte WXUNUSED(alpha)) { return false; } virtual bool CanSetTransparent() { return false; } - + + // default item access: we have a permanent default item which is the one + // set by the user code but we may also have a temporary default item which + // would be chosen if the user pressed "Enter" now but the default action + // reverts to the "permanent" default as soon as this temporary default + // item loses focus + + // used to reset default if pointing to removed child + virtual void RemoveChild(wxWindowBase *child); + + // get the default item, temporary or permanent + wxWindow *GetDefaultItem() const + { return m_winTmpDefault ? m_winTmpDefault : m_winDefault; } + + // set the permanent default item, return the old default + wxWindow *SetDefaultItem(wxWindow *win) + { wxWindow *old = GetDefaultItem(); m_winDefault = win; return old; } + + // return the temporary default item, can be NULL + wxWindow *GetTmpDefaultItem() const { return m_winTmpDefault; } + + // set a temporary default item, SetTmpDefaultItem(NULL) should be called + // soon after a call to SetTmpDefaultItem(window), return the old default + wxWindow *SetTmpDefaultItem(wxWindow *win) + { wxWindow *old = GetDefaultItem(); m_winTmpDefault = win; return old; } + + // implementation only from now on // ------------------------------- @@ -264,6 +290,12 @@ protected: // the frame icon wxIconBundle m_icons; + // a default window (usually a button) or NULL + wxWindow *m_winDefault; + + // a temporary override of m_winDefault, use the latter if NULL + wxWindow *m_winTmpDefault; + DECLARE_NO_COPY_CLASS(wxTopLevelWindowBase) DECLARE_EVENT_TABLE() }; diff --git a/include/wx/window.h b/include/wx/window.h index 9ac9397953..9f852eec40 100644 --- a/include/wx/window.h +++ b/include/wx/window.h @@ -514,23 +514,6 @@ public: // click it virtual bool AcceptsFocusFromKeyboard() const { return AcceptsFocus(); } - // NB: these methods really don't belong here but with the current - // class hierarchy there is no other place for them :-( - - // get the default child of this parent, i.e. the one which is - // activated by pressing - virtual wxWindow *GetDefaultItem() const { return NULL; } - - // set this child as default, return the old default - virtual wxWindow *SetDefaultItem(wxWindow * WXUNUSED(child)) - { return NULL; } - - // set this child as temporary default - virtual void SetTmpDefaultItem(wxWindow * WXUNUSED(win)) { } - - // return the temporary default item, can be NULL - virtual wxWindow *GetTmpDefaultItem() const { return NULL; } - // navigates in the specified direction by sending a wxNavigationKeyEvent virtual bool Navigate(int flags = wxNavigationKeyEvent::IsForward); diff --git a/src/common/containr.cpp b/src/common/containr.cpp index 020bc20ca1..0a034ba01e 100644 --- a/src/common/containr.cpp +++ b/src/common/containr.cpp @@ -43,10 +43,7 @@ wxControlContainer::wxControlContainer(wxWindow *winParent) { m_winParent = winParent; - - m_winLastFocused = - m_winTmpDefault = - m_winDefault = NULL; + m_winLastFocused = NULL; m_inSetFocus = false; } @@ -504,12 +501,6 @@ void wxControlContainer::HandleOnWindowDestroy(wxWindowBase *child) { if ( child == m_winLastFocused ) m_winLastFocused = NULL; - - if ( child == m_winDefault ) - m_winDefault = NULL; - - if ( child == m_winTmpDefault ) - m_winTmpDefault = NULL; } // ---------------------------------------------------------------------------- diff --git a/src/common/dlgcmn.cpp b/src/common/dlgcmn.cpp index 56f113c20a..c6c995cf30 100644 --- a/src/common/dlgcmn.cpp +++ b/src/common/dlgcmn.cpp @@ -104,7 +104,7 @@ BEGIN_EVENT_TABLE(wxDialogBase, wxTopLevelWindow) WX_EVENT_TABLE_CONTROL_CONTAINER(wxDialogBase) END_EVENT_TABLE() -WX_DELEGATE_TO_CONTROL_CONTAINER(wxDialogBase) +WX_DELEGATE_TO_CONTROL_CONTAINER(wxDialogBase, wxTopLevelWindow) #endif void wxDialogBase::Init() diff --git a/src/common/pickerbase.cpp b/src/common/pickerbase.cpp index c714fc13c4..e550e651c7 100644 --- a/src/common/pickerbase.cpp +++ b/src/common/pickerbase.cpp @@ -45,7 +45,7 @@ BEGIN_EVENT_TABLE(wxPickerBase, wxControl) EVT_SIZE(wxPickerBase::OnSize) WX_EVENT_TABLE_CONTROL_CONTAINER(wxPickerBase) END_EVENT_TABLE() -WX_DELEGATE_TO_CONTROL_CONTAINER(wxPickerBase) +WX_DELEGATE_TO_CONTROL_CONTAINER(wxPickerBase, wxControl) // ---------------------------------------------------------------------------- diff --git a/src/common/toplvcmn.cpp b/src/common/toplvcmn.cpp index a6c9133dbd..60376d3ebc 100644 --- a/src/common/toplvcmn.cpp +++ b/src/common/toplvcmn.cpp @@ -55,6 +55,8 @@ wxTopLevelWindowBase::wxTopLevelWindowBase() { // Unlike windows, top level windows are created hidden by default. m_isShown = false; + m_winDefault = NULL; + m_winTmpDefault = NULL; } wxTopLevelWindowBase::~wxTopLevelWindowBase() @@ -393,3 +395,14 @@ void wxTopLevelWindowBase::RequestUserAttention(int WXUNUSED(flags)) // it's probably better than do nothing, isn't it? Raise(); } + +void wxTopLevelWindowBase::RemoveChild(wxWindowBase *child) +{ + if ( child == m_winDefault ) + m_winDefault = NULL; + + if ( child == m_winTmpDefault ) + m_winTmpDefault = NULL; + + wxWindow::RemoveChild(child); +} diff --git a/src/common/wincmn.cpp b/src/common/wincmn.cpp index ea7f5ced34..7ce145ca24 100644 --- a/src/common/wincmn.cpp +++ b/src/common/wincmn.cpp @@ -291,11 +291,6 @@ wxWindowBase::~wxWindowBase() // reset the dangling pointer our parent window may keep to us if ( m_parent ) { - if ( m_parent->GetDefaultItem() == this ) - { - m_parent->SetDefaultItem(NULL); - } - m_parent->RemoveChild(this); } diff --git a/src/generic/panelg.cpp b/src/generic/panelg.cpp index e15d79bf81..34c59bb52d 100644 --- a/src/generic/panelg.cpp +++ b/src/generic/panelg.cpp @@ -97,7 +97,7 @@ END_EVENT_TABLE() // implementation // ============================================================================ -WX_DELEGATE_TO_CONTROL_CONTAINER(wxPanel) +WX_DELEGATE_TO_CONTROL_CONTAINER(wxPanel, wxWindow) // ---------------------------------------------------------------------------- // wxPanel creation diff --git a/src/generic/splitter.cpp b/src/generic/splitter.cpp index d3ed2d45c9..9e357621d6 100644 --- a/src/generic/splitter.cpp +++ b/src/generic/splitter.cpp @@ -69,7 +69,7 @@ BEGIN_EVENT_TABLE(wxSplitterWindow, wxWindow) WX_EVENT_TABLE_CONTROL_CONTAINER(wxSplitterWindow) END_EVENT_TABLE() -WX_DELEGATE_TO_CONTROL_CONTAINER(wxSplitterWindow) +WX_DELEGATE_TO_CONTROL_CONTAINER(wxSplitterWindow, wxWindow) bool wxSplitterWindow::Create(wxWindow *parent, wxWindowID id, const wxPoint& pos, diff --git a/src/gtk/button.cpp b/src/gtk/button.cpp index 74281019ca..a9631c7958 100644 --- a/src/gtk/button.cpp +++ b/src/gtk/button.cpp @@ -165,10 +165,10 @@ bool wxButton::Create( wxWindow *parent, wxWindowID id, const wxString &label, void wxButton::SetDefault() { - wxWindow *parent = GetParent(); - wxCHECK_RET( parent, _T("button without parent?") ); + wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow); + wxCHECK_RET( tlw, _T("button without top level window?") ); - parent->SetDefaultItem(this); + tlw->SetDefaultItem(this); GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT ); gtk_widget_grab_default( m_widget ); diff --git a/src/gtk1/button.cpp b/src/gtk1/button.cpp index c84debc53d..b3898b46b0 100644 --- a/src/gtk1/button.cpp +++ b/src/gtk1/button.cpp @@ -154,10 +154,10 @@ bool wxButton::Create( wxWindow *parent, wxWindowID id, const wxString &label, void wxButton::SetDefault() { - wxWindow *parent = GetParent(); - wxCHECK_RET( parent, _T("button without parent?") ); + wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow); + wxCHECK_RET( tlw, _T("button without top level window?") ); - parent->SetDefaultItem(this); + tlw->SetDefaultItem(this); GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT ); gtk_widget_grab_default( m_widget ); diff --git a/src/mac/carbon/app.cpp b/src/mac/carbon/app.cpp index 3a350c9502..154369334d 100644 --- a/src/mac/carbon/app.cpp +++ b/src/mac/carbon/app.cpp @@ -1576,14 +1576,18 @@ bool wxApp::MacSendCharEvent( wxWindow* focus , long keymessage , long modifiers { if ( keyval == WXK_RETURN ) { - wxButton *def = wxDynamicCast(focus->GetDefaultItem(), wxButton); - if ( def && def->IsEnabled() ) + wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(focus), wxTopLevelWindow); + if ( tlw && tlw->GetDefaultItem() ) { - wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() ); - event.SetEventObject(def); - def->Command(event); + wxButton *def = wxDynamicCast(tlw->GetDefaultItem(), wxButton); + if ( def && def->IsEnabled() ) + { + wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() ); + event.SetEventObject(def); + def->Command(event); - return true ; + return true ; + } } } else if (keyval == WXK_ESCAPE || (keyval == '.' && modifiers & cmdKey ) ) diff --git a/src/mac/carbon/button.cpp b/src/mac/carbon/button.cpp index c5a3f521bd..b1d1f7228f 100644 --- a/src/mac/carbon/button.cpp +++ b/src/mac/carbon/button.cpp @@ -102,13 +102,12 @@ bool wxButton::Create(wxWindow *parent, void wxButton::SetDefault() { - wxWindow *parent = GetParent(); + wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow); wxButton *btnOldDefault = NULL; - - if ( parent ) + if ( tlw ) { - btnOldDefault = wxDynamicCast(parent->GetDefaultItem(), wxButton); - parent->SetDefaultItem(this); + btnOldDefault = wxDynamicCast(tlw->GetDefaultItem(), wxButton); + tlw->SetDefaultItem(this); } if ( btnOldDefault ) diff --git a/src/mac/carbon/combobox.cpp b/src/mac/carbon/combobox.cpp index 331103395a..fc840bdec8 100644 --- a/src/mac/carbon/combobox.cpp +++ b/src/mac/carbon/combobox.cpp @@ -25,7 +25,7 @@ IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl) -WX_DELEGATE_TO_CONTROL_CONTAINER(wxComboBox) +WX_DELEGATE_TO_CONTROL_CONTAINER(wxComboBox, wxControl) BEGIN_EVENT_TABLE(wxComboBox, wxControl) WX_EVENT_TABLE_CONTROL_CONTAINER(wxComboBox) @@ -109,13 +109,10 @@ protected: // such as the clicking the default button. if (!m_cb->GetEventHandler()->ProcessEvent( event )) { - wxWindow *parent = GetParent(); - while ( parent && !parent->IsTopLevel() && parent->GetDefaultItem() == NULL ) - parent = parent->GetParent() ; - - if ( parent && parent->GetDefaultItem() ) + wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow); + if ( tlw && tlw->GetDefaultItem() ) { - wxButton *def = wxDynamicCast(parent->GetDefaultItem(), wxButton); + wxButton *def = wxDynamicCast(tlw->GetDefaultItem(), wxButton); if ( def && def->IsEnabled() ) { wxCommandEvent event( wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() ); diff --git a/src/mac/carbon/combobxc.cpp b/src/mac/carbon/combobxc.cpp index fff4f2e43a..dc03529a98 100644 --- a/src/mac/carbon/combobxc.cpp +++ b/src/mac/carbon/combobxc.cpp @@ -147,14 +147,10 @@ protected: // This will invoke the dialog default action, such // as the clicking the default button. - wxWindow *parent = GetParent(); - while( parent && !parent->IsTopLevel() && parent->GetDefaultItem() == NULL ) { - parent = parent->GetParent(); - } - if ( parent && parent->GetDefaultItem() ) + wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow); + if ( tlw && tlw->GetDefaultItem() ) { - wxButton *def = wxDynamicCast(parent->GetDefaultItem(), - wxButton); + wxButton *def = wxDynamicCast(tlw->GetDefaultItem(), wxButton); if ( def && def->IsEnabled() ) { wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() ); diff --git a/src/mac/carbon/spinctrl.cpp b/src/mac/carbon/spinctrl.cpp index a4dd944e31..8702e0bd87 100644 --- a/src/mac/carbon/spinctrl.cpp +++ b/src/mac/carbon/spinctrl.cpp @@ -146,7 +146,7 @@ BEGIN_EVENT_TABLE(wxSpinCtrl, wxControl) WX_EVENT_TABLE_CONTROL_CONTAINER(wxSpinCtrl) END_EVENT_TABLE() -WX_DELEGATE_TO_CONTROL_CONTAINER(wxSpinCtrl) +WX_DELEGATE_TO_CONTROL_CONTAINER(wxSpinCtrl, wxControl) // ============================================================================ diff --git a/src/mac/carbon/textctrl.cpp b/src/mac/carbon/textctrl.cpp index 24f9fdf83e..c84b8fa714 100644 --- a/src/mac/carbon/textctrl.cpp +++ b/src/mac/carbon/textctrl.cpp @@ -962,15 +962,10 @@ void wxTextCtrl::OnChar(wxKeyEvent& event) if ( !(m_windowStyle & wxTE_MULTILINE) ) { - wxWindow *parent = GetParent(); - while ( parent && !parent->IsTopLevel() && parent->GetDefaultItem() == NULL ) + wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow); + if ( tlw && tlw->GetDefaultItem() ) { - parent = parent->GetParent() ; - } - - if ( parent && parent->GetDefaultItem() ) - { - wxButton *def = wxDynamicCast(parent->GetDefaultItem(), wxButton); + wxButton *def = wxDynamicCast(tlw->GetDefaultItem(), wxButton); if ( def && def->IsEnabled() ) { wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() ); diff --git a/src/mac/carbon/window.cpp b/src/mac/carbon/window.cpp index 7b4445cc30..ef5d9dc053 100644 --- a/src/mac/carbon/window.cpp +++ b/src/mac/carbon/window.cpp @@ -993,11 +993,11 @@ wxWindowMac::~wxWindowMac() // wxRemoveMacControlAssociation( this ) ; // If we delete an item, we should initialize the parent panel, // because it could now be invalid. - wxWindow *parent = GetParent() ; - if ( parent ) + wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow); + if ( tlw ) { - if (parent->GetDefaultItem() == (wxButton*) this) - parent->SetDefaultItem(NULL); + if ( tlw->GetDefaultItem() == (wxButton*) this) + tlw->SetDefaultItem(NULL); } if ( m_peer && m_peer->Ok() ) diff --git a/src/mac/classic/app.cpp b/src/mac/classic/app.cpp index c2f121a0f4..16f2be2066 100644 --- a/src/mac/classic/app.cpp +++ b/src/mac/classic/app.cpp @@ -2015,31 +2015,34 @@ bool wxApp::MacSendKeyDownEvent( wxWindow* focus , long keymessage , long modifi // backdoor handler for default return and command escape if ( !handled && (!focus->IsKindOf(CLASSINFO(wxControl) ) || !focus->MacCanFocus() ) ) { - // if window is not having a focus still testing for default enter or cancel - // TODO add the UMA version for ActiveNonFloatingWindow - wxWindow* focus = wxFindWinFromMacWindow( (WXWindow) FrontWindow() ) ; - if ( focus ) - { + // if window is not having a focus still testing for default enter or cancel + // TODO add the UMA version for ActiveNonFloatingWindow + wxWindow* focus = wxFindWinFromMacWindow( (WXWindow) FrontWindow() ) ; + if ( focus ) + { if ( keyval == WXK_RETURN ) { - wxButton *def = wxDynamicCast(focus->GetDefaultItem(), - wxButton); - if ( def && def->IsEnabled() ) - { - wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() ); - event.SetEventObject(def); - def->Command(event); - return true ; + wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow); + if ( tlw && tlw->GetDefaultItem() ) + { + wxButton *def = wxDynamicCast(tlw->GetDefaultItem(), wxButton); + if ( def && def->IsEnabled() ) + { + wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() ); + event.SetEventObject(def); + def->Command(event); + return true ; + } } } /* generate wxID_CANCEL if command-. or has been pressed (typically in dialogs) */ else if (keyval == WXK_ESCAPE || (keyval == '.' && modifiers & cmdKey ) ) { - wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL); - new_event.SetEventObject( focus ); - handled = focus->GetEventHandler()->ProcessEvent( new_event ); + wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL); + new_event.SetEventObject( focus ); + handled = focus->GetEventHandler()->ProcessEvent( new_event ); } - } + } } return handled ; } diff --git a/src/mac/classic/button.cpp b/src/mac/classic/button.cpp index a12aa3a9b9..714983c2fa 100644 --- a/src/mac/classic/button.cpp +++ b/src/mac/classic/button.cpp @@ -66,13 +66,12 @@ bool wxButton::Create(wxWindow *parent, wxWindowID id, const wxString& lbl, void wxButton::SetDefault() { - wxWindow *parent = GetParent(); + wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow); wxButton *btnOldDefault = NULL; - if ( parent ) + if ( tlw ) { - btnOldDefault = wxDynamicCast(parent->GetDefaultItem(), - wxButton); - parent->SetDefaultItem(this); + btnOldDefault = wxDynamicCast(tlw->GetDefaultItem(), wxButton); + tlw->SetDefaultItem(this); } Boolean inData; diff --git a/src/mac/classic/combobox.cpp b/src/mac/classic/combobox.cpp index aba551e2f3..2879e2a3eb 100644 --- a/src/mac/classic/combobox.cpp +++ b/src/mac/classic/combobox.cpp @@ -94,14 +94,10 @@ protected: // This will invoke the dialog default action, such // as the clicking the default button. - wxWindow *parent = GetParent(); - while( parent && !parent->IsTopLevel() && parent->GetDefaultItem() == NULL ) { - parent = parent->GetParent() ; - } - if ( parent && parent->GetDefaultItem() ) + wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow); + if ( tlw && tlw->GetDefaultItem() ) { - wxButton *def = wxDynamicCast(parent->GetDefaultItem(), - wxButton); + wxButton *def = wxDynamicCast(tlw->GetDefaultItem(), wxButton); if ( def && def->IsEnabled() ) { wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() ); diff --git a/src/mac/classic/control.cpp b/src/mac/classic/control.cpp index 449a49b285..5edc253f73 100644 --- a/src/mac/classic/control.cpp +++ b/src/mac/classic/control.cpp @@ -190,11 +190,11 @@ wxControl::~wxControl() wxRemoveMacControlAssociation( this ) ; // If we delete an item, we should initialize the parent panel, // because it could now be invalid. - wxWindow *parent = GetParent() ; - if ( parent ) + wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow); + if ( tlw ) { - if (parent->GetDefaultItem() == (wxButton*) this) - parent->SetDefaultItem(NULL); + if ( tlw->GetDefaultItem() == (wxButton*) this) + tlw->SetDefaultItem(NULL); } if ( (ControlHandle) m_macControl ) { diff --git a/src/mac/classic/listbox.cpp b/src/mac/classic/listbox.cpp index 613d60912c..d0de721190 100644 --- a/src/mac/classic/listbox.cpp +++ b/src/mac/classic/listbox.cpp @@ -924,14 +924,10 @@ void wxListBox::OnChar(wxKeyEvent& event) { if ( event.GetKeyCode() == WXK_RETURN || event.GetKeyCode() == WXK_NUMPAD_ENTER) { - wxWindow* parent = GetParent() ; - while( parent && !parent->IsTopLevel() && parent->GetDefaultItem() == NULL ) - parent = parent->GetParent() ; - - if ( parent && parent->GetDefaultItem() ) + wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow); + if ( tlw && tlw->GetDefaultItem() ) { - wxButton *def = wxDynamicCast(parent->GetDefaultItem(), - wxButton); + wxButton *def = wxDynamicCast(tlw->GetDefaultItem(), wxButton); if ( def && def->IsEnabled() ) { wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() ); diff --git a/src/mac/classic/textctrl.cpp b/src/mac/classic/textctrl.cpp index cae116ab43..076e53e031 100644 --- a/src/mac/classic/textctrl.cpp +++ b/src/mac/classic/textctrl.cpp @@ -1622,14 +1622,10 @@ void wxTextCtrl::OnChar(wxKeyEvent& event) } if ( !(m_windowStyle & wxTE_MULTILINE) ) { - wxWindow *parent = GetParent(); - while( parent && !parent->IsTopLevel() && parent->GetDefaultItem() == NULL ) { - parent = parent->GetParent() ; - } - if ( parent && parent->GetDefaultItem() ) + wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow); + if ( tlw && tlw->GetDefaultItem() ) { - wxButton *def = wxDynamicCast(parent->GetDefaultItem(), - wxButton); + wxButton *def = wxDynamicCast(tlw->GetDefaultItem(), wxButton); if ( def && def->IsEnabled() ) { wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() ); diff --git a/src/motif/button.cpp b/src/motif/button.cpp index af7a37ac49..ed2ded5373 100644 --- a/src/motif/button.cpp +++ b/src/motif/button.cpp @@ -123,9 +123,9 @@ void wxButton::SetDefaultShadowThicknessAndResize() void wxButton::SetDefault() { - wxWindow *parent = GetParent(); - if ( parent ) - parent->SetDefaultItem(this); + wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow); + if ( tlw ) + tlw->SetDefaultItem(this); // We initially do not set XmNdefaultShadowThickness, to have // small buttons. Unfortunately, buttons are now mis-aligned. We @@ -134,6 +134,7 @@ void wxButton::SetDefault() // wxButton in the same row, correction is straighforward: we set // resource for all wxButton in this parent (but not sub panels) + wxWindow *parent = GetParent(); for (wxWindowList::compatibility_iterator node = parent->GetChildren().GetFirst (); node; node = node->GetNext ()) { diff --git a/src/msw/button.cpp b/src/msw/button.cpp index 08361d58b6..0729c9eea0 100644 --- a/src/msw/button.cpp +++ b/src/msw/button.cpp @@ -197,8 +197,8 @@ bool wxButton::Create(wxWindow *parent, wxButton::~wxButton() { - wxWindow *parent = GetParent(); - if ( parent && parent->GetTmpDefaultItem() == this ) + wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow); + if ( tlw && tlw->GetTmpDefaultItem() == this ) { UnsetTmpDefault(); } @@ -344,12 +344,12 @@ wxSize wxButtonBase::GetDefaultSize() // set this button as the (permanently) default one in its panel void wxButton::SetDefault() { - wxWindow *parent = GetParent(); + wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow); - wxCHECK_RET( parent, _T("button without parent?") ); + wxCHECK_RET( tlw, _T("button without top level window?") ); // set this one as the default button both for wxWidgets ... - wxWindow *winOldDefault = parent->SetDefaultItem(this); + wxWindow *winOldDefault = tlw->SetDefaultItem(this); // ... and Windows SetDefaultStyle(wxDynamicCast(winOldDefault, wxButton), false); @@ -359,12 +359,12 @@ void wxButton::SetDefault() // set this button as being currently default void wxButton::SetTmpDefault() { - wxWindow *parent = GetParent(); + wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow); - wxCHECK_RET( parent, _T("button without parent?") ); + wxCHECK_RET( tlw, _T("button without top level window?") ); - wxWindow *winOldDefault = parent->GetDefaultItem(); - parent->SetTmpDefaultItem(this); + wxWindow *winOldDefault = tlw->GetDefaultItem(); + tlw->SetTmpDefaultItem(this); SetDefaultStyle(wxDynamicCast(winOldDefault, wxButton), false); SetDefaultStyle(this, true); @@ -373,13 +373,13 @@ void wxButton::SetTmpDefault() // unset this button as currently default, it may still stay permanent default void wxButton::UnsetTmpDefault() { - wxWindow *parent = GetParent(); + wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow); - wxCHECK_RET( parent, _T("button without parent?") ); + wxCHECK_RET( tlw, _T("button without top level window?") ); - parent->SetTmpDefaultItem(NULL); + tlw->SetTmpDefaultItem(NULL); - wxWindow *winOldDefault = parent->GetDefaultItem(); + wxWindow *winOldDefault = tlw->GetDefaultItem(); SetDefaultStyle(this, false); SetDefaultStyle(wxDynamicCast(winOldDefault, wxButton), true); @@ -402,10 +402,10 @@ wxButton::SetDefaultStyle(wxButton *btn, bool on) if ( !wxTheApp->IsActive() ) return; - wxWindow * const parent = btn->GetParent(); - wxCHECK_RET( parent, _T("button without parent?") ); + wxWindow * const tlw = wxGetTopLevelParent(btn); + wxCHECK_RET( tlw, _T("button without top level window?") ); - ::SendMessage(GetHwndOf(parent), DM_SETDEFID, btn->GetId(), 0L); + ::SendMessage(GetHwndOf(tlw), DM_SETDEFID, btn->GetId(), 0L); // sending DM_SETDEFID also changes the button style to // BS_DEFPUSHBUTTON so there is nothing more to do @@ -817,10 +817,10 @@ bool wxButton::MSWOnDraw(WXDRAWITEMSTRUCT *wxdis) bool selected = (state & ODS_SELECTED) != 0; if ( !selected ) { - wxPanel *panel = wxDynamicCast(GetParent(), wxPanel); - if ( panel ) + wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow); + if ( tlw ) { - selected = panel->GetDefaultItem() == this; + selected = tlw->GetDefaultItem() == this; } } bool pushed = (SendMessage(GetHwnd(), BM_GETSTATE, 0, 0) & BST_PUSHED) != 0; diff --git a/src/msw/window.cpp b/src/msw/window.cpp index 32facca384..0515fe88be 100644 --- a/src/msw/window.cpp +++ b/src/msw/window.cpp @@ -2146,14 +2146,17 @@ bool wxWindowMSW::MSWProcessMessage(WXMSG* pMsg) else // not a button itself { #if wxUSE_BUTTON - wxButton *btn = wxDynamicCast(GetDefaultItem(), - wxButton); - if ( btn && btn->IsEnabled() ) + wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow); + if ( tlw ) { - // if we do have a default button, do press it - btn->MSWCommand(BN_CLICKED, 0 /* unused */); + wxButton *btn = wxDynamicCast(tlw->GetDefaultItem(), wxButton); + if ( btn && btn->IsEnabled() ) + { + // if we do have a default button, do press it + btn->MSWCommand(BN_CLICKED, 0 /* unused */); - return true; + return true; + } } else // no default button #endif // wxUSE_BUTTON diff --git a/src/os2/button.cpp b/src/os2/button.cpp index 2e9a10d578..7f02cfd61f 100644 --- a/src/os2/button.cpp +++ b/src/os2/button.cpp @@ -116,16 +116,16 @@ bool wxButton::Create( wxWindow* pParent, wxButton::~wxButton() { - wxPanel* pPanel = wxDynamicCast(GetParent(), wxPanel); + wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow); - if (pPanel) + if (tlw) { - if (pPanel->GetDefaultItem() == this) + if (tlw->GetDefaultItem() == this) { // // Don't leave the panel with invalid default item // - pPanel->SetDefaultItem(NULL); + tlw->SetDefaultItem(NULL); } } } // end of wxButton::~wxButton @@ -232,14 +232,14 @@ bool wxButton::SendClickEvent() void wxButton::SetDefault() { - wxWindow* pParent = GetParent(); + wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow); - wxCHECK_RET( pParent, _T("button without parent?") ); + wxCHECK_RET( tlw, _T("button without top level window?") ); // // Set this one as the default button both for wxWidgets and Windows // - wxWindow* pWinOldDefault = pParent->SetDefaultItem(this); + wxWindow* pWinOldDefault = tlw->SetDefaultItem(this); SetDefaultStyle( wxDynamicCast(pWinOldDefault, wxButton), false); SetDefaultStyle( this, true ); @@ -247,26 +247,26 @@ void wxButton::SetDefault() void wxButton::SetTmpDefault() { - wxWindow* pParent = GetParent(); + wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow); - wxCHECK_RET( pParent, _T("button without parent?") ); + wxCHECK_RET( tlw, _T("button without top level window?") ); - wxWindow* pWinOldDefault = pParent->GetDefaultItem(); + wxWindow* pWinOldDefault = tlw->GetDefaultItem(); - pParent->SetTmpDefaultItem(this); + tlw->SetTmpDefaultItem(this); SetDefaultStyle( wxDynamicCast(pWinOldDefault, wxButton), false); SetDefaultStyle( this, true ); } // end of wxButton::SetTmpDefault void wxButton::UnsetTmpDefault() { - wxWindow* pParent = GetParent(); + wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow); - wxCHECK_RET( pParent, _T("button without parent?") ); + wxCHECK_RET( tlw, _T("button without top level window?") ); - pParent->SetTmpDefaultItem(NULL); + tlw->SetTmpDefaultItem(NULL); - wxWindow* pWinOldDefault = pParent->GetDefaultItem(); + wxWindow* pWinOldDefault = tlw->GetDefaultItem(); SetDefaultStyle( this, false ); SetDefaultStyle( wxDynamicCast(pWinOldDefault, wxButton), true ); diff --git a/src/os2/window.cpp b/src/os2/window.cpp index 61dd62db31..2db19422f6 100644 --- a/src/os2/window.cpp +++ b/src/os2/window.cpp @@ -2021,9 +2021,13 @@ bool wxWindowOS2::OS2ProcessMessage( WXMSG* pMsg ) } else { - wxButton* pBtn = wxDynamicCast( GetDefaultItem() - ,wxButton - ); + wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow); + wxButton* pBtn = NULL; + + if (tlw) + { + pBtn = wxDynamicCast(tlw->GetDefaultItem(), wxButton); + } if (pBtn && pBtn->IsEnabled()) { -- 2.45.2