]> git.saurik.com Git - wxWidgets.git/commitdiff
Refactor wxDocParentFrame and wxDocMDIParentFrame to share common base class.
authorVadim Zeitlin <vadim@wxwidgets.org>
Wed, 12 May 2010 14:34:18 +0000 (14:34 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Wed, 12 May 2010 14:34:18 +0000 (14:34 +0000)
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
include/wx/docview.h
src/common/docmdi.cpp
src/common/docview.cpp

index 3d4e437fbe9d75f19e18dc9e7e6c7bd542974653..d0e0a19968ccb5475ce4d5b7c945ffb438925814 100644 (file)
@@ -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
 /////////////////////////////////////////////////////////////////////////////
 
 #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<wxMDIParentFrame> 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<wxMDIChildFrame, wxMDIParentFrame> wxDocMDIChildFrameBase;
 
 class WXDLLIMPEXP_CORE wxDocMDIChildFrame : public wxDocMDIChildFrameBase
 {
 public:
+    wxDocMDIChildFrame() { }
+
     wxDocMDIChildFrame(wxDocument *doc,
                        wxView *view,
                        wxMDIParentFrame *parent,
index f8b7ea2cb72b31dd75b7e5118cbfc8c1421e1013..824e432fdc13e3cd3ec0d9082828ddcd2046f9b4 100644 (file)
@@ -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 BaseFrame>
+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<wxFrame> 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
 // ----------------------------------------------------------------------------
index 0767a621044d97ce42bb0dde1b8006db84c69f08..612b01995a7e68f6ffa957ba918a1dd1b75c959e 100644 (file)
@@ -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
 /////////////////////////////////////////////////////////////////////////////
 
 
 #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
index 335265e02c972d9fddc9d4a94ce9bd09bf594f0b..04b6bb98187776fc31807a2023699003ef297b60 100644 (file)
@@ -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)