]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/docview.cpp
Remove identic implementations of wxDir::Exists from platform-specific files and...
[wxWidgets.git] / src / common / docview.cpp
index 64315afb6086bbeae868c9b5741ebd4f453e8eb0..71c1b15b1fbe9e6468552e3a58f5b5599a418758 100644 (file)
@@ -135,8 +135,6 @@ bool wxDocument::DeleteContents()
 
 wxDocument::~wxDocument()
 {
-    DeleteContents();
-
     delete m_commandProcessor;
 
     if (GetDocumentManager())
@@ -689,7 +687,7 @@ void wxView::SetDocChildFrame(wxDocChildFrameAnyBase *docChildFrame)
 bool wxView::TryBefore(wxEvent& event)
 {
     wxDocument * const doc = GetDocument();
-    return doc && doc->ProcessEventHere(event);
+    return doc && doc->ProcessEventLocally(event);
 }
 
 void wxView::OnActivateView(bool WXUNUSED(activate),
@@ -886,6 +884,11 @@ BEGIN_EVENT_TABLE(wxDocManager, wxEvtHandler)
     EVT_MENU(wxID_UNDO, wxDocManager::OnUndo)
     EVT_MENU(wxID_REDO, wxDocManager::OnRedo)
 
+    // We don't know in advance how many items can there be in the MRU files
+    // list so set up OnMRUFile() as a handler for all menu events and do the
+    // check for the id of the menu item clicked inside it.
+    EVT_MENU(wxID_ANY, wxDocManager::OnMRUFile)
+
     EVT_UPDATE_UI(wxID_OPEN, wxDocManager::OnUpdateFileOpen)
     EVT_UPDATE_UI(wxID_CLOSE, wxDocManager::OnUpdateDisableIfNoDoc)
     EVT_UPDATE_UI(wxID_CLOSE_ALL, wxDocManager::OnUpdateDisableIfNoDoc)
@@ -899,10 +902,13 @@ BEGIN_EVENT_TABLE(wxDocManager, wxEvtHandler)
 #if wxUSE_PRINTING_ARCHITECTURE
     EVT_MENU(wxID_PRINT, wxDocManager::OnPrint)
     EVT_MENU(wxID_PREVIEW, wxDocManager::OnPreview)
+    EVT_MENU(wxID_PRINT_SETUP, wxDocManager::OnPageSetup)
 
     EVT_UPDATE_UI(wxID_PRINT, wxDocManager::OnUpdateDisableIfNoDoc)
     EVT_UPDATE_UI(wxID_PREVIEW, wxDocManager::OnUpdateDisableIfNoDoc)
-#endif
+    // NB: we keep "Print setup" menu item always enabled as it can be used
+    //     even without an active document
+#endif // wxUSE_PRINTING_ARCHITECTURE
 END_EVENT_TABLE()
 
 wxDocManager* wxDocManager::sm_docManager = NULL;
@@ -1082,9 +1088,57 @@ void wxDocManager::OnFileSaveAs(wxCommandEvent& WXUNUSED(event))
     doc->SaveAs();
 }
 
-void wxDocManager::OnPrint(wxCommandEvent& WXUNUSED(event))
+void wxDocManager::OnMRUFile(wxCommandEvent& event)
+{
+    // Check if the id is in the range assigned to MRU list entries.
+    const int id = event.GetId();
+    if ( id >= wxID_FILE1 &&
+            id < wxID_FILE1 + m_fileHistory->GetBaseId() )
+    {
+        DoOpenMRUFile(id - wxID_FILE1);
+    }
+    else
+    {
+        event.Skip();
+    }
+}
+
+void wxDocManager::DoOpenMRUFile(unsigned n)
 {
+    wxString filename(GetHistoryFile(n));
+    if ( filename.empty() )
+        return;
+
+    wxString errMsg; // must contain exactly one "%s" if non-empty
+    if ( wxFile::Exists(filename) )
+    {
+        // try to open it
+        if ( CreateDocument(filename, wxDOC_SILENT) )
+            return;
+
+        errMsg = _("The file '%s' couldn't be opened.");
+    }
+    else // file doesn't exist
+    {
+        errMsg = _("The file '%s' doesn't exist and couldn't be opened.");
+    }
+
+
+    wxASSERT_MSG( !errMsg.empty(), "should have an error message" );
+
+    // remove the file which we can't open from the MRU list
+    RemoveFileFromHistory(n);
+
+    // and tell the user about it
+    wxLogError(errMsg + '\n' +
+               _("It has been removed from the most recently used files list."),
+               filename);
+}
+
 #if wxUSE_PRINTING_ARCHITECTURE
+
+void wxDocManager::OnPrint(wxCommandEvent& WXUNUSED(event))
+{
     wxView *view = GetActiveView();
     if (!view)
         return;
@@ -1092,26 +1146,32 @@ void wxDocManager::OnPrint(wxCommandEvent& WXUNUSED(event))
     wxPrintout *printout = view->OnCreatePrintout();
     if (printout)
     {
-        wxPrinter printer;
+        wxPrintDialogData printDialogData(m_pageSetupDialogData.GetPrintData());
+        wxPrinter printer(&printDialogData);
         printer.Print(view->GetFrame(), printout, true);
 
         delete printout;
     }
-#endif // wxUSE_PRINTING_ARCHITECTURE
 }
 
-#if wxUSE_PRINTING_ARCHITECTURE
+void wxDocManager::OnPageSetup(wxCommandEvent& WXUNUSED(event))
+{
+    wxPageSetupDialog dlg(wxTheApp->GetTopWindow(), &m_pageSetupDialogData);
+    if ( dlg.ShowModal() == wxID_OK )
+    {
+        m_pageSetupDialogData = dlg.GetPageSetupData();
+    }
+}
+
 wxPreviewFrame* wxDocManager::CreatePreviewFrame(wxPrintPreviewBase* preview,
                                                  wxWindow *parent,
                                                  const wxString& title)
 {
     return new wxPreviewFrame(preview, parent, title);
 }
-#endif // wxUSE_PRINTING_ARCHITECTURE
 
 void wxDocManager::OnPreview(wxCommandEvent& WXUNUSED(event))
 {
-#if wxUSE_PRINTING_ARCHITECTURE
     wxBusyCursor busy;
     wxView *view = GetActiveView();
     if (!view)
@@ -1120,9 +1180,13 @@ void wxDocManager::OnPreview(wxCommandEvent& WXUNUSED(event))
     wxPrintout *printout = view->OnCreatePrintout();
     if (printout)
     {
+        wxPrintDialogData printDialogData(m_pageSetupDialogData.GetPrintData());
+
         // Pass two printout objects: for preview, and possible printing.
         wxPrintPreviewBase *
-            preview = new wxPrintPreview(printout, view->OnCreatePrintout());
+            preview = new wxPrintPreview(printout,
+                                         view->OnCreatePrintout(),
+                                         &printDialogData);
         if ( !preview->Ok() )
         {
             delete preview;
@@ -1139,8 +1203,8 @@ void wxDocManager::OnPreview(wxCommandEvent& WXUNUSED(event))
         frame->Initialize();
         frame->Show(true);
     }
-#endif // wxUSE_PRINTING_ARCHITECTURE
 }
+#endif // wxUSE_PRINTING_ARCHITECTURE
 
 void wxDocManager::OnUndo(wxCommandEvent& event)
 {
@@ -1252,7 +1316,7 @@ wxView *wxDocManager::GetActiveView() const
 bool wxDocManager::TryBefore(wxEvent& event)
 {
     wxView * const view = GetActiveView();
-    return view && view->ProcessEventHere(event);
+    return view && view->ProcessEventLocally(event);
 }
 
 namespace
@@ -1284,6 +1348,17 @@ wxDocTemplates GetVisibleTemplates(const wxList& allTemplates)
 
 } // anonymous namespace
 
+void wxDocManager::ActivateDocument(wxDocument *doc)
+{
+    wxView * const view = doc->GetFirstView();
+    if ( !view )
+        return;
+
+    view->Activate(true);
+    if ( wxWindow *win = view->GetFrame() )
+        win->SetFocus();
+}
+
 wxDocument *wxDocManager::CreateDocument(const wxString& pathOrig, long flags)
 {
     // this ought to be const but SelectDocumentType/Path() are not
@@ -1338,13 +1413,7 @@ wxDocument *wxDocManager::CreateDocument(const wxString& pathOrig, long flags)
             if ( fn == doc->GetFilename() )
             {
                 // file already open, just activate it and return
-                if ( doc->GetFirstView() )
-                {
-                    ActivateView(doc->GetFirstView());
-                    if ( doc->GetDocumentWindow() )
-                        doc->GetDocumentWindow()->SetFocus();
-                    return doc;
-                }
+                ActivateDocument(doc);
             }
         }
     }
@@ -1391,6 +1460,11 @@ wxDocument *wxDocManager::CreateDocument(const wxString& pathOrig, long flags)
     if ( !(flags & wxDOC_NEW) && temp->FileMatchesTemplate(path) )
         AddFileToHistory(path);
 
+    // at least under Mac (where views are top level windows) it seems to be
+    // necessary to manually activate the new document to bring it to the
+    // forefront -- and it shouldn't hurt doing this under the other platforms
+    ActivateDocument(docNew);
+
     return docNew;
 }
 
@@ -1889,105 +1963,9 @@ 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()
-{
-    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;
-
-    wxString errMsg; // must contain exactly one "%s" if non-empty
-    if ( wxFile::Exists(filename) )
-    {
-        // try to open it
-        if ( m_docManager->CreateDocument(filename, wxDOC_SILENT) )
-            return;
-
-        errMsg = _("The file '%s' couldn't be opened.");
-    }
-    else // file doesn't exist
-    {
-        errMsg = _("The file '%s' doesn't exist and couldn't be opened.");
-    }
-
-
-    wxASSERT_MSG( !errMsg.empty(), "should have an error message" );
-
-    // remove the file which we can't open from the MRU list
-    m_docManager->RemoveFileFromHistory(n);
-
-    // and tell the user about it
-    wxLogError(errMsg + '\n' +
-               _("It has been removed from the most recently used files list."),
-               filename);
-}
-
-// Extend event processing to search the view's event table
-bool wxDocParentFrame::TryBefore(wxEvent& event)
-{
-    if ( m_docManager && m_docManager->ProcessEventHere(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)