From c48bdb0c28dd146b89bcbfeda43db1b48d4a343b Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 12 May 2010 14:34:18 +0000 Subject: [PATCH] Refactor wxDocParentFrame and wxDocMDIParentFrame to share common base class. Use the same approach as for the child frames: add a base template class which allows wxDocParentFrame to inherit from wxFrame and wxDocMDIParentFrame from wxMDIParentFrame while still allowing to reuse the common code. This reduces code duplication and should make implementing parent AUI document frame easier as well, see #8945. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@64295 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/docmdi.h | 77 ++++++++++---------- include/wx/docview.h | 156 ++++++++++++++++++++++++++++++++++------- src/common/docmdi.cpp | 74 ++----------------- src/common/docview.cpp | 68 +----------------- 4 files changed, 176 insertions(+), 199 deletions(-) diff --git a/include/wx/docmdi.h b/include/wx/docmdi.h index 3d4e437fbe..d0e0a19968 100644 --- a/include/wx/docmdi.h +++ b/include/wx/docmdi.h @@ -1,11 +1,11 @@ ///////////////////////////////////////////////////////////////////////////// -// Name: docmdi.h +// Name: wx/docmdi.h // Purpose: Frame classes for MDI document/view applications // Author: Julian Smart -// Modified by: // Created: 01/02/97 // RCS-ID: $Id$ -// Copyright: (c) Julian Smart +// Copyright: (c) 1997 Julian Smart +// (c) 2010 Vadim Zeitlin // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// @@ -19,61 +19,60 @@ #include "wx/docview.h" #include "wx/mdi.h" -/* - * Use this instead of wxMDIParentFrame - */ - -class WXDLLIMPEXP_CORE wxDocMDIParentFrame: public wxMDIParentFrame -{ -public: - wxDocMDIParentFrame(); - wxDocMDIParentFrame(wxDocManager *manager, wxFrame *parent, wxWindowID id, - const wxString& title, const wxPoint& pos = wxDefaultPosition, - const wxSize& size = wxDefaultSize, long style = wxDEFAULT_FRAME_STYLE, const wxString& name = wxFrameNameStr); - - bool Create(wxDocManager *manager, wxFrame *parent, wxWindowID id, - const wxString& title, const wxPoint& pos = wxDefaultPosition, - const wxSize& size = wxDefaultSize, long style = wxDEFAULT_FRAME_STYLE, const wxString& name = wxFrameNameStr); - - wxDocManager *GetDocumentManager(void) const { return m_docManager; } +#ifdef __VISUALC6__ + // "non dll-interface class 'wxDocXXXFrameAny<>' used as base interface for + // dll-interface class 'wxDocMDIXXXFrame'" -- this is bogus as the template + // will be DLL-exported but only once it is used as base class here! + #pragma warning (push) + #pragma warning (disable:4275) +#endif - void OnExit(wxCommandEvent& event); - void OnMRUFile(wxCommandEvent& event); - void OnCloseWindow(wxCloseEvent& event); +// Define MDI versions of the doc-view frame classes. Note that we need to +// define them as classes for wxRTTI, otherwise we could simply define them as +// typedefs. -protected: - void Init(); +// ---------------------------------------------------------------------------- +// An MDI document parent frame +// ---------------------------------------------------------------------------- - virtual bool TryBefore(wxEvent& event); +typedef + wxDocParentFrameAny wxDocMDIParentFrameBase; - wxDocManager *m_docManager; +class WXDLLIMPEXP_CORE wxDocMDIParentFrame : public wxDocMDIParentFrameBase +{ +public: + wxDocMDIParentFrame() : wxDocMDIParentFrameBase() { } + + wxDocMDIParentFrame(wxDocManager *manager, + wxFrame *parent, + wxWindowID id, + const wxString& title, + const wxPoint& pos = wxDefaultPosition, + const wxSize& size = wxDefaultSize, + long style = wxDEFAULT_FRAME_STYLE, + const wxString& name = wxFrameNameStr) + : wxDocMDIParentFrameBase(manager, + parent, id, title, pos, size, style, name) + { + } private: DECLARE_CLASS(wxDocMDIParentFrame) - DECLARE_EVENT_TABLE() wxDECLARE_NO_COPY_CLASS(wxDocMDIParentFrame); }; // ---------------------------------------------------------------------------- -// An MDI document child frame: we need to define it as a class just for wxRTTI, -// otherwise we could simply typedef it +// An MDI document child frame // ---------------------------------------------------------------------------- -#ifdef __VISUALC6__ - // "non dll-interface class 'wxDocChildFrameAny<>' used as base interface - // for dll-interface class 'wxDocMDIChildFrame'" -- this is bogus as the - // template will be DLL-exported but only once it is used as base class - // here! - #pragma warning (push) - #pragma warning (disable:4275) -#endif - typedef wxDocChildFrameAny wxDocMDIChildFrameBase; class WXDLLIMPEXP_CORE wxDocMDIChildFrame : public wxDocMDIChildFrameBase { public: + wxDocMDIChildFrame() { } + wxDocMDIChildFrame(wxDocument *doc, wxView *view, wxMDIParentFrame *parent, diff --git a/include/wx/docview.h b/include/wx/docview.h index f8b7ea2cb7..824e432fdc 100644 --- a/include/wx/docview.h +++ b/include/wx/docview.h @@ -751,26 +751,55 @@ private: wxDECLARE_NO_COPY_CLASS(wxDocChildFrame); }; -#ifdef __VISUALC6__ - #pragma warning (pop) -#endif - // ---------------------------------------------------------------------------- -// A default parent frame +// wxDocParentFrame and related classes. +// +// As with wxDocChildFrame we define a template base class used by both normal +// and MDI versions // ---------------------------------------------------------------------------- -class WXDLLIMPEXP_CORE wxDocParentFrame : public wxFrame +// Base class containing type-independent code of wxDocParentFrameAny +// +// Similarly to wxDocChildFrameAnyBase, this class is a mix-in and doesn't +// derive from wxWindow. +class WXDLLIMPEXP_CORE wxDocParentFrameAnyBase { public: - wxDocParentFrame(); - wxDocParentFrame(wxDocManager *manager, - wxFrame *frame, - wxWindowID id, - const wxString& title, - const wxPoint& pos = wxDefaultPosition, - const wxSize& size = wxDefaultSize, - long style = wxDEFAULT_FRAME_STYLE, - const wxString& name = wxFrameNameStr); + wxDocParentFrameAnyBase() { m_docManager = NULL; } + + wxDocManager *GetDocumentManager() const { return m_docManager; } + +protected: + // Open the MRU file with the given index in our associated file history. + // + // This is called from the derived class event handler for the MRU menu + // items. + void DoOpenMRUFile(unsigned n); + + wxDocManager *m_docManager; + + wxDECLARE_NO_COPY_CLASS(wxDocParentFrameAnyBase); +}; + +// This is similar to wxDocChildFrameAny and is used to provide common +// implementation for both wxDocParentFrame and wxDocMDIParentFrame +template +class WXDLLIMPEXP_CORE wxDocParentFrameAny : public BaseFrame, + public wxDocParentFrameAnyBase +{ +public: + wxDocParentFrameAny() { } + wxDocParentFrameAny(wxDocManager *manager, + wxFrame *frame, + wxWindowID id, + const wxString& title, + const wxPoint& pos = wxDefaultPosition, + const wxSize& size = wxDefaultSize, + long style = wxDEFAULT_FRAME_STYLE, + const wxString& name = wxFrameNameStr) + { + Create(manager, frame, id, title, pos, size, style, name); + } bool Create(wxDocManager *manager, wxFrame *frame, @@ -779,27 +808,106 @@ public: const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = wxDEFAULT_FRAME_STYLE, - const wxString& name = wxFrameNameStr); + const wxString& name = wxFrameNameStr) + { + m_docManager = manager; - wxDocManager *GetDocumentManager() const { return m_docManager; } + if ( !BaseFrame::Create(frame, id, title, pos, size, style, name) ) + return false; + + this->Connect(wxID_EXIT, wxEVT_COMMAND_MENU_SELECTED, + wxCommandEventHandler(wxDocParentFrameAny::OnExit)); + this->Connect(wxID_FILE1, wxID_FILE9, wxEVT_COMMAND_MENU_SELECTED, + wxCommandEventHandler(wxDocParentFrameAny::OnMRUFile)); + this->Connect(wxEVT_CLOSE_WINDOW, + wxCloseEventHandler(wxDocParentFrameAny::OnCloseWindow)); - void OnExit(wxCommandEvent& event); - void OnMRUFile(wxCommandEvent& event); - void OnCloseWindow(wxCloseEvent& event); + return true; + } protected: // hook the document manager into event handling chain here - virtual bool TryBefore(wxEvent& event); + virtual bool TryBefore(wxEvent& event) + { + if ( m_docManager && m_docManager->ProcessEventLocally(event) ) + return true; - wxDocManager *m_docManager; + return BaseFrame::TryBefore(event); + } + +private: + void OnExit(wxCommandEvent& WXUNUSED(event)) + { + this->Close(); + } + + void OnMRUFile(wxCommandEvent& event) + { + DoOpenMRUFile(event.GetId() - wxID_FILE1); + } + + void OnCloseWindow(wxCloseEvent& event) + { + if ( m_docManager && !m_docManager->Clear(!event.CanVeto()) ) + { + // The user decided not to close finally, abort. + event.Veto(); + } + else + { + // Just skip the event, base class handler will destroy the window. + event.Skip(); + } + } + + + wxDECLARE_NO_COPY_CLASS(wxDocParentFrameAny); +}; + +typedef wxDocParentFrameAny wxDocParentFrameBase; + +class WXDLLIMPEXP_CORE wxDocParentFrame : public wxDocParentFrameBase +{ +public: + wxDocParentFrame() : wxDocParentFrameBase() { } + + wxDocParentFrame(wxDocManager *manager, + wxFrame *parent, + wxWindowID id, + const wxString& title, + const wxPoint& pos = wxDefaultPosition, + const wxSize& size = wxDefaultSize, + long style = wxDEFAULT_FRAME_STYLE, + const wxString& name = wxFrameNameStr) + : wxDocParentFrameBase(manager, + parent, id, title, pos, size, style, name) + { + } + + bool Create(wxDocManager *manager, + wxFrame *parent, + wxWindowID id, + const wxString& title, + const wxPoint& pos = wxDefaultPosition, + const wxSize& size = wxDefaultSize, + long style = wxDEFAULT_FRAME_STYLE, + const wxString& name = wxFrameNameStr) + { + return wxDocParentFrameBase::Create(manager, + parent, id, title, + pos, size, style, name); + } private: - typedef wxFrame base_type; DECLARE_CLASS(wxDocParentFrame) - DECLARE_EVENT_TABLE() wxDECLARE_NO_COPY_CLASS(wxDocParentFrame); }; +#ifdef __VISUALC6__ + // reenable warning 4275 + #pragma warning (pop) +#endif + // ---------------------------------------------------------------------------- // Provide simple default printing facilities // ---------------------------------------------------------------------------- diff --git a/src/common/docmdi.cpp b/src/common/docmdi.cpp index 0767a62104..612b01995a 100644 --- a/src/common/docmdi.cpp +++ b/src/common/docmdi.cpp @@ -1,11 +1,11 @@ ///////////////////////////////////////////////////////////////////////////// -// Name: docmdi.cpp +// Name: src/common/docmdi.cpp // Purpose: Frame classes for MDI document/view applications -// Author: Julian Smart -// Modified by: +// Author: Julian Smart, Vadim Zeitlin // Created: 01/02/97 // RCS-ID: $Id$ -// Copyright: (c) Julian Smart +// Copyright: (c) 1997 Julian Smart +// (c) 2010 Vadim Zeitlin // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// @@ -20,73 +20,7 @@ #include "wx/docmdi.h" -/* - * Docview MDI parent frame - */ - IMPLEMENT_CLASS(wxDocMDIParentFrame, wxMDIParentFrame) - -BEGIN_EVENT_TABLE(wxDocMDIParentFrame, wxMDIParentFrame) - EVT_MENU(wxID_EXIT, wxDocMDIParentFrame::OnExit) - EVT_MENU_RANGE(wxID_FILE1, wxID_FILE9, wxDocMDIParentFrame::OnMRUFile) - EVT_CLOSE(wxDocMDIParentFrame::OnCloseWindow) -END_EVENT_TABLE() - -wxDocMDIParentFrame::wxDocMDIParentFrame() -{ - Init(); -} - -wxDocMDIParentFrame::wxDocMDIParentFrame(wxDocManager *manager, wxFrame *frame, wxWindowID id, const wxString& title, - const wxPoint& pos, const wxSize& size, long style, const wxString& name) -{ - Init(); - Create(manager, frame, id, title, pos, size, style, name); -} - -bool wxDocMDIParentFrame::Create(wxDocManager *manager, wxFrame *frame, wxWindowID id, const wxString& title, - const wxPoint& pos, const wxSize& size, long style, const wxString& name) -{ - m_docManager = manager; - return wxMDIParentFrame::Create(frame, id, title, pos, size, style, name); -} - -void wxDocMDIParentFrame::OnExit(wxCommandEvent& WXUNUSED(event)) -{ - Close(); -} - -void wxDocMDIParentFrame::Init() -{ - m_docManager = NULL; -} - -void wxDocMDIParentFrame::OnMRUFile(wxCommandEvent& event) -{ - wxString f(m_docManager->GetHistoryFile(event.GetId() - wxID_FILE1)); - if (!f.empty()) - (void)m_docManager->CreateDocument(f, wxDOC_SILENT); -} - -bool wxDocMDIParentFrame::TryBefore(wxEvent& event) -{ - if ( m_docManager && m_docManager->ProcessEventLocally(event) ) - return true; - - return wxMDIParentFrame::TryBefore(event); -} - -void wxDocMDIParentFrame::OnCloseWindow(wxCloseEvent& event) -{ - if (m_docManager->Clear(!event.CanVeto())) - { - this->Destroy(); - } - else - event.Veto(); -} - - IMPLEMENT_CLASS(wxDocMDIChildFrame, wxMDIChildFrame) #endif // wxUSE_DOC_VIEW_ARCHITECTURE diff --git a/src/common/docview.cpp b/src/common/docview.cpp index 335265e02c..04b6bb9818 100644 --- a/src/common/docview.cpp +++ b/src/common/docview.cpp @@ -1889,54 +1889,11 @@ bool wxDocChildFrameAnyBase::CloseView(wxCloseEvent& event) } // ---------------------------------------------------------------------------- -// Default parent frame +// wxDocParentFrameAnyBase // ---------------------------------------------------------------------------- -BEGIN_EVENT_TABLE(wxDocParentFrame, wxFrame) - EVT_MENU(wxID_EXIT, wxDocParentFrame::OnExit) - EVT_MENU_RANGE(wxID_FILE1, wxID_FILE9, wxDocParentFrame::OnMRUFile) - EVT_CLOSE(wxDocParentFrame::OnCloseWindow) -END_EVENT_TABLE() - -wxDocParentFrame::wxDocParentFrame() +void wxDocParentFrameAnyBase::DoOpenMRUFile(unsigned n) { - m_docManager = NULL; -} - -wxDocParentFrame::wxDocParentFrame(wxDocManager *manager, - wxFrame *frame, - wxWindowID id, - const wxString& title, - const wxPoint& pos, - const wxSize& size, - long style, - const wxString& name) - : wxFrame(frame, id, title, pos, size, style, name) -{ - m_docManager = manager; -} - -bool wxDocParentFrame::Create(wxDocManager *manager, - wxFrame *frame, - wxWindowID id, - const wxString& title, - const wxPoint& pos, - const wxSize& size, - long style, - const wxString& name) -{ - m_docManager = manager; - return base_type::Create(frame, id, title, pos, size, style, name); -} - -void wxDocParentFrame::OnExit(wxCommandEvent& WXUNUSED(event)) -{ - Close(); -} - -void wxDocParentFrame::OnMRUFile(wxCommandEvent& event) -{ - int n = event.GetId() - wxID_FILE1; // the index in MRU list wxString filename(m_docManager->GetHistoryFile(n)); if ( filename.empty() ) return; @@ -1967,27 +1924,6 @@ void wxDocParentFrame::OnMRUFile(wxCommandEvent& event) filename); } -// Extend event processing to search the view's event table -bool wxDocParentFrame::TryBefore(wxEvent& event) -{ - if ( m_docManager && m_docManager->ProcessEventLocally(event) ) - return true; - - return wxFrame::TryBefore(event); -} - -// Define the behaviour for the frame closing -// - must delete all frames except for the main one. -void wxDocParentFrame::OnCloseWindow(wxCloseEvent& event) -{ - if (m_docManager->Clear(!event.CanVeto())) - { - Destroy(); - } - else - event.Veto(); -} - #if wxUSE_PRINTING_ARCHITECTURE wxDocPrintout::wxDocPrintout(wxView *view, const wxString& title) -- 2.45.2