From 551f281b794a8f9b04526bd3055b080ec406dff5 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 20 Aug 2006 10:30:26 +0000 Subject: [PATCH] handle the buttons with ids specified in SetAffirmative/CancelId() as Ok/Cancel git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@40700 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/dialog.h | 20 +++++++--- src/common/dlgcmn.cpp | 89 +++++++++++++++++++++++++++++++++++++++---- 2 files changed, 95 insertions(+), 14 deletions(-) diff --git a/include/wx/dialog.h b/include/wx/dialog.h index ed6b5e4e8c..74c6ecfcb6 100644 --- a/include/wx/dialog.h +++ b/include/wx/dialog.h @@ -52,12 +52,16 @@ public: void SetReturnCode(int returnCode) { m_returnCode = returnCode; } int GetReturnCode() const { return m_returnCode; } - // The identifier for the affirmative button - void SetAffirmativeId(int affirmativeId) { m_affirmativeId = affirmativeId; } + // Set the identifier for the affirmative button: this button will close + // the dialog after validating data and calling TransferDataFromWindow() + void SetAffirmativeId(int affirmativeId); int GetAffirmativeId() const { return m_affirmativeId; } - // Identifier for Esc key translation - void SetEscapeId(int escapeId) { m_escapeId = escapeId; } + // Set identifier for Esc key translation: the button with this id will + // close the dialog without doing anything else; special value wxID_NONE + // means to not handle Esc at all while wxID_ANY means to map Esc to + // wxID_CANCEL if present and GetAffirmativeId() otherwise + void SetEscapeId(int escapeId); int GetEscapeId() const { return m_escapeId; } #if wxUSE_STATTEXT // && wxUSE_TEXTCTRL @@ -92,6 +96,10 @@ protected: // the dialog return code void EndDialog(int rc); + // call Validate() and TransferDataFromWindow() and close dialog with + // wxID_OK return code + void AcceptAndClose(); + // The return code from modal dialog int m_returnCode; @@ -113,9 +121,9 @@ private: void OnCloseWindow(wxCloseEvent& event); // handle the standard buttons - void OnOK(wxCommandEvent& event); + void OnAffirmativeButton(wxCommandEvent& event); void OnApply(wxCommandEvent& event); - void OnCancel(wxCommandEvent& event); + void OnCancelButton(wxCommandEvent& event); // update the background colour void OnSysColourChanged(wxSysColourChangedEvent& event); diff --git a/src/common/dlgcmn.cpp b/src/common/dlgcmn.cpp index ed592fb8cf..7fad04b640 100644 --- a/src/common/dlgcmn.cpp +++ b/src/common/dlgcmn.cpp @@ -99,9 +99,9 @@ private: // ---------------------------------------------------------------------------- BEGIN_EVENT_TABLE(wxDialogBase, wxTopLevelWindow) - EVT_BUTTON(wxID_OK, wxDialogBase::OnOK) + EVT_BUTTON(wxID_OK, wxDialogBase::OnAffirmativeButton) EVT_BUTTON(wxID_APPLY, wxDialogBase::OnApply) - EVT_BUTTON(wxID_CANCEL, wxDialogBase::OnCancel) + EVT_BUTTON(wxID_CANCEL, wxDialogBase::OnCancelButton) EVT_CLOSE(wxDialogBase::OnCloseWindow) @@ -426,6 +426,82 @@ wxStdDialogButtonSizer *wxDialogBase::CreateStdDialogButtonSizer( long flags ) #endif // wxUSE_BUTTON +// ---------------------------------------------------------------------------- +// escape/affirmatives button handling +// ---------------------------------------------------------------------------- + +void wxDialogBase::AcceptAndClose() +{ + if ( Validate() && TransferDataFromWindow() ) + { + EndDialog(wxID_OK); + } +} + +void wxDialogBase::SetAffirmativeId(int affirmativeId) +{ + if ( affirmativeId == m_affirmativeId ) + return; + + // disconnect the handler for the old affirmative button + if ( m_affirmativeId != wxID_NONE && m_affirmativeId != wxID_OK ) + { + if ( !Disconnect + ( + m_affirmativeId, + wxEVT_COMMAND_BUTTON_CLICKED, + wxCommandEventHandler(wxDialogBase::OnAffirmativeButton) + ) ) + { + wxFAIL_MSG( _T("failed to disconnect old ok handler") ); + } + } + //else: wxID_OK is always handled + + // connect the handler to the new button + if ( affirmativeId != wxID_NONE ) + { + Connect(m_affirmativeId, + wxEVT_COMMAND_BUTTON_CLICKED, + wxCommandEventHandler(wxDialogBase::OnAffirmativeButton)); + } + //else: no affirmative button + + m_affirmativeId = affirmativeId; +} + +void wxDialogBase::SetEscapeId(int escapeId) +{ + if ( escapeId == m_escapeId ) + return; + + if ( m_escapeId != wxID_ANY && + m_escapeId != wxID_CANCEL && + m_escapeId != wxID_ANY ) + { + if ( !Disconnect + ( + m_escapeId, + wxEVT_COMMAND_BUTTON_CLICKED, + wxCommandEventHandler(wxDialogBase::OnCancelButton) + ) ) + { + wxFAIL_MSG( _T("failed to disconnect old cancel handler") ); + } + } + //else: wxID_CANCEL is always handled + + // connect the handler to the new button + if ( escapeId != wxID_NONE ) + { + Connect(m_escapeId, + wxEVT_COMMAND_BUTTON_CLICKED, + wxCommandEventHandler(wxDialogBase::OnCancelButton)); + } + + m_escapeId = escapeId; +} + // ---------------------------------------------------------------------------- // event handling stuff // ---------------------------------------------------------------------------- @@ -488,12 +564,9 @@ void wxDialogBase::OnCharHook(wxKeyEvent& event) event.Skip(); } -void wxDialogBase::OnOK(wxCommandEvent& WXUNUSED(event)) +void wxDialogBase::OnAffirmativeButton(wxCommandEvent& WXUNUSED(event)) { - if ( Validate() && TransferDataFromWindow() ) - { - EndDialog(wxID_OK); - } + AcceptAndClose(); } void wxDialogBase::OnApply(wxCommandEvent& WXUNUSED(event)) @@ -504,7 +577,7 @@ void wxDialogBase::OnApply(wxCommandEvent& WXUNUSED(event)) // TODO probably need to disable the Apply button until things change again } -void wxDialogBase::OnCancel(wxCommandEvent& WXUNUSED(event)) +void wxDialogBase::OnCancelButton(wxCommandEvent& WXUNUSED(event)) { EndDialog(wxID_CANCEL); } -- 2.47.2