IMPLEMENT_DYNAMIC_CLASS(wxDocPrintout, wxPrintout)
#endif
-IMPLEMENT_DYNAMIC_CLASS(wxFileHistory, wxObject)
-
// ============================================================================
// implementation
// ============================================================================
return ext.MakeLower();
}
-// return the string used for the MRU list items in the menu
-//
-// NB: the index n is 0-based, as usual, but the strings start from 1
-wxString GetMRUEntryLabel(int n, const wxString& path)
-{
- // we need to quote '&' characters which are used for mnemonics
- wxString pathInMenu(path);
- pathInMenu.Replace("&", "&&");
-
- return wxString::Format("&%d %s", n + 1, pathInMenu);
-}
-
} // anonymous namespace
// ----------------------------------------------------------------------------
wxDocument::wxDocument(wxDocument *parent)
{
m_documentModified = false;
- m_documentParent = parent;
m_documentTemplate = NULL;
+
+ m_documentParent = parent;
+ if ( parent )
+ parent->m_childDocuments.push_back(this);
+
m_commandProcessor = NULL;
m_savedYet = false;
}
wxDocument::~wxDocument()
{
- DeleteContents();
-
delete m_commandProcessor;
if (GetDocumentManager())
GetDocumentManager()->RemoveDocument(this);
+ if ( m_documentParent )
+ m_documentParent->m_childDocuments.remove(this);
+
// Not safe to do here, since it'll invoke virtual view functions
// expecting to see valid derived objects: and by the time we get here,
// we've called destructors higher up.
if ( !OnSaveModified() )
return false;
+ // When the parent document closes, its children must be closed as well as
+ // they can't exist without the parent.
+
+ // As usual, first check if all children can be closed.
+ DocsList::const_iterator it = m_childDocuments.begin();
+ for ( DocsList::const_iterator end = m_childDocuments.end(); it != end; ++it )
+ {
+ if ( !(*it)->OnSaveModified() )
+ {
+ // Leave the parent document opened if a child can't close.
+ return false;
+ }
+ }
+
+ // Now that they all did, do close them: as m_childDocuments is modified as
+ // we iterate over it, don't use the usual for-style iteration here.
+ while ( !m_childDocuments.empty() )
+ {
+ wxDocument * const childDoc = m_childDocuments.front();
+
+ // This will call OnSaveModified() once again but it shouldn't do
+ // anything as the document was just saved or marked as not needing to
+ // be saved by the call to OnSaveModified() that returned true above.
+ if ( !childDoc->Close() )
+ {
+ wxFAIL_MSG( "Closing the child document unexpectedly failed "
+ "after its OnSaveModified() returned true" );
+ }
+
+ // Delete the child document by deleting all its views.
+ childDoc->DeleteAllViews();
+ }
+
+
return OnCloseDocument();
}
wxDocManager *wxDocument::GetDocumentManager() const
{
+ // For child documents we use the same document manager as the parent, even
+ // though we don't have our own template (as children are not opened/saved
+ // directly).
+ if ( m_documentParent )
+ return m_documentParent->GetDocumentManager();
+
return m_documentTemplate ? m_documentTemplate->GetDocumentManager() : NULL;
}
#if WXWIN_COMPATIBILITY_2_8
bool wxDocument::GetPrintableName(wxString& buf) const
{
- // this function can not only be overridden by the user code but also
+ // this function cannot only be overridden by the user code but also
// called by it so we need to ensure that we return the same thing as
// GetUserReadableName() but we can't call it because this would result in
// an infinite recursion, hence we use the helper DoGetUserReadableName()
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),
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)
EVT_UPDATE_UI(wxID_REVERT, wxDocManager::OnUpdateFileRevert)
EVT_UPDATE_UI(wxID_NEW, wxDocManager::OnUpdateFileNew)
EVT_UPDATE_UI(wxID_SAVE, wxDocManager::OnUpdateFileSave)
- EVT_UPDATE_UI(wxID_SAVEAS, wxDocManager::OnUpdateDisableIfNoDoc)
+ EVT_UPDATE_UI(wxID_SAVEAS, wxDocManager::OnUpdateFileSaveAs)
EVT_UPDATE_UI(wxID_UNDO, wxDocManager::OnUpdateUndo)
EVT_UPDATE_UI(wxID_REDO, wxDocManager::OnUpdateRedo)
#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;
void wxDocManager::OnFileClose(wxCommandEvent& WXUNUSED(event))
{
wxDocument *doc = GetCurrentDocument();
- if (!doc)
- return;
- if (doc->Close())
- {
- doc->DeleteAllViews();
- if (m_docs.Member(doc))
- delete doc;
- }
+ if (doc)
+ CloseDocument(doc);
}
void wxDocManager::OnFileCloseAll(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 + static_cast<int>(m_fileHistory->GetCount()) )
+ {
+ 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;
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)
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());
- if ( !preview->Ok() )
+ preview = new wxPrintPreview(printout,
+ view->OnCreatePrintout(),
+ &printDialogData);
+ if ( !preview->IsOk() )
{
delete preview;
wxLogError(_("Print preview creation failed."));
frame->Initialize();
frame->Show(true);
}
-#endif // wxUSE_PRINTING_ARCHITECTURE
}
+#endif // wxUSE_PRINTING_ARCHITECTURE
void wxDocManager::OnUndo(wxCommandEvent& event)
{
void wxDocManager::OnUpdateFileSave(wxUpdateUIEvent& event)
{
wxDocument * const doc = GetCurrentDocument();
- event.Enable( doc && !doc->AlreadySaved() );
+ event.Enable( doc && !doc->IsChildDocument() && !doc->AlreadySaved() );
+}
+
+void wxDocManager::OnUpdateFileSaveAs(wxUpdateUIEvent& event)
+{
+ wxDocument * const doc = GetCurrentDocument();
+ event.Enable( doc && !doc->IsChildDocument() );
}
void wxDocManager::OnUpdateUndo(wxUpdateUIEvent& event)
bool wxDocManager::TryBefore(wxEvent& event)
{
wxView * const view = GetActiveView();
- return view && view->ProcessEventHere(event);
+ return view && view->ProcessEventLocally(event);
}
namespace
} // 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
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);
+ return doc;
}
}
}
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;
}
m_templates.DeleteObject(temp);
}
+wxDocTemplate* wxDocManager::FindTemplate(const wxClassInfo* classinfo)
+{
+ for ( wxList::compatibility_iterator node = m_templates.GetFirst();
+ node;
+ node = node->GetNext() )
+ {
+ wxDocTemplate* t = wxStaticCast(node->GetData(), wxDocTemplate);
+ if ( t->GetDocClassInfo() == classinfo )
+ return t;
+ }
+
+ return NULL;
+}
+
// Add and remove a document from the manager's list
void wxDocManager::AddDocument(wxDocument *doc)
{
// deleted directly not by us as indicated by its doc child frame
// pointer still being set
m_childView->SetDocChildFrame(NULL);
- delete m_childView;
- m_childView = NULL;
+ wxDELETE(m_childView);
}
m_childDocument = NULL;
}
// ----------------------------------------------------------------------------
-// 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);
-}
+#if wxUSE_PRINTING_ARCHITECTURE
-void wxDocParentFrame::OnExit(wxCommandEvent& WXUNUSED(event))
+namespace
{
- Close();
-}
-void wxDocParentFrame::OnMRUFile(wxCommandEvent& event)
+wxString GetAppropriateTitle(const wxView *view, const wxString& titleGiven)
{
- 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
+ wxString title(titleGiven);
+ if ( title.empty() )
{
- errMsg = _("The file '%s' doesn't exist and couldn't be opened.");
+ if ( view && view->GetDocument() )
+ title = view->GetDocument()->GetUserReadableName();
+ else
+ title = _("Printout");
}
-
- 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();
+ return title;
}
-#if wxUSE_PRINTING_ARCHITECTURE
+} // anonymous namespace
wxDocPrintout::wxDocPrintout(wxView *view, const wxString& title)
- : wxPrintout(title)
+ : wxPrintout(GetAppropriateTitle(view, title))
{
m_printoutView = view;
}
#endif // wxUSE_PRINTING_ARCHITECTURE
-// ----------------------------------------------------------------------------
-// File history (a.k.a. MRU, most recently used, files list)
-// ----------------------------------------------------------------------------
-
-wxFileHistory::wxFileHistory(size_t maxFiles, wxWindowID idBase)
-{
- m_fileMaxFiles = maxFiles;
- m_idBase = idBase;
-}
-
-void wxFileHistory::AddFileToHistory(const wxString& file)
-{
- // check if we don't already have this file
- const wxFileName fnNew(file);
- size_t i,
- numFiles = m_fileHistory.size();
- for ( i = 0; i < numFiles; i++ )
- {
- if ( fnNew == m_fileHistory[i] )
- {
- // we do have it, move it to the top of the history
- RemoveFileFromHistory(i);
- numFiles--;
- break;
- }
- }
-
- // if we already have a full history, delete the one at the end
- if ( numFiles == m_fileMaxFiles )
- {
- RemoveFileFromHistory(--numFiles);
- }
-
- // add a new menu item to all file menus (they will be updated below)
- for ( wxList::compatibility_iterator node = m_fileMenus.GetFirst();
- node;
- node = node->GetNext() )
- {
- wxMenu * const menu = (wxMenu *)node->GetData();
-
- if ( !numFiles && menu->GetMenuItemCount() )
- menu->AppendSeparator();
-
- // label doesn't matter, it will be set below anyhow, but it can't
- // be empty (this is supposed to indicate a stock item)
- menu->Append(m_idBase + numFiles, " ");
- }
-
- // insert the new file in the beginning of the file history
- m_fileHistory.insert(m_fileHistory.begin(), file);
- numFiles++;
-
- // update the labels in all menus
- for ( i = 0; i < numFiles; i++ )
- {
- // if in same directory just show the filename; otherwise the full path
- const wxFileName fnOld(m_fileHistory[i]);
-
- wxString pathInMenu;
- if ( fnOld.GetPath() == fnNew.GetPath() )
- {
- pathInMenu = fnOld.GetFullName();
- }
- else // file in different directory
- {
- // absolute path; could also set relative path
- pathInMenu = m_fileHistory[i];
- }
-
- for ( wxList::compatibility_iterator node = m_fileMenus.GetFirst();
- node;
- node = node->GetNext() )
- {
- wxMenu * const menu = (wxMenu *)node->GetData();
-
- menu->SetLabel(m_idBase + i, GetMRUEntryLabel(i, pathInMenu));
- }
- }
-}
-
-void wxFileHistory::RemoveFileFromHistory(size_t i)
-{
- size_t numFiles = m_fileHistory.size();
- wxCHECK_RET( i < numFiles,
- wxT("invalid index in wxFileHistory::RemoveFileFromHistory") );
-
- // delete the element from the array
- m_fileHistory.RemoveAt(i);
- numFiles--;
-
- for ( wxList::compatibility_iterator node = m_fileMenus.GetFirst();
- node;
- node = node->GetNext() )
- {
- wxMenu * const menu = (wxMenu *) node->GetData();
-
- // shift filenames up
- for ( size_t j = i; j < numFiles; j++ )
- {
- menu->SetLabel(m_idBase + j, GetMRUEntryLabel(j, m_fileHistory[j]));
- }
-
- // delete the last menu item which is unused now
- const wxWindowID lastItemId = m_idBase + numFiles;
- if ( menu->FindItem(lastItemId) )
- menu->Delete(lastItemId);
-
- // delete the last separator too if no more files are left
- if ( m_fileHistory.empty() )
- {
- const wxMenuItemList::compatibility_iterator
- nodeLast = menu->GetMenuItems().GetLast();
- if ( nodeLast )
- {
- wxMenuItem * const lastMenuItem = nodeLast->GetData();
- if ( lastMenuItem->IsSeparator() )
- menu->Delete(lastMenuItem);
- }
- //else: menu is empty somehow
- }
- }
-}
-
-void wxFileHistory::UseMenu(wxMenu *menu)
-{
- if ( !m_fileMenus.Member(menu) )
- m_fileMenus.Append(menu);
-}
-
-void wxFileHistory::RemoveMenu(wxMenu *menu)
-{
- m_fileMenus.DeleteObject(menu);
-}
-
-#if wxUSE_CONFIG
-void wxFileHistory::Load(const wxConfigBase& config)
-{
- m_fileHistory.Clear();
-
- wxString buf;
- buf.Printf(wxT("file%d"), 1);
-
- wxString historyFile;
- while ((m_fileHistory.GetCount() < m_fileMaxFiles) &&
- config.Read(buf, &historyFile) && !historyFile.empty())
- {
- m_fileHistory.Add(historyFile);
-
- buf.Printf(wxT("file%d"), (int)m_fileHistory.GetCount()+1);
- historyFile = wxEmptyString;
- }
-
- AddFilesToMenu();
-}
-
-void wxFileHistory::Save(wxConfigBase& config)
-{
- size_t i;
- for (i = 0; i < m_fileMaxFiles; i++)
- {
- wxString buf;
- buf.Printf(wxT("file%d"), (int)i+1);
- if (i < m_fileHistory.GetCount())
- config.Write(buf, wxString(m_fileHistory[i]));
- else
- config.Write(buf, wxEmptyString);
- }
-}
-#endif // wxUSE_CONFIG
-
-void wxFileHistory::AddFilesToMenu()
-{
- if ( m_fileHistory.empty() )
- return;
-
- for ( wxList::compatibility_iterator node = m_fileMenus.GetFirst();
- node;
- node = node->GetNext() )
- {
- AddFilesToMenu((wxMenu *) node->GetData());
- }
-}
-
-void wxFileHistory::AddFilesToMenu(wxMenu* menu)
-{
- if ( m_fileHistory.empty() )
- return;
-
- if ( menu->GetMenuItemCount() )
- menu->AppendSeparator();
-
- for ( size_t i = 0; i < m_fileHistory.GetCount(); i++ )
- {
- menu->Append(m_idBase + i, GetMRUEntryLabel(i, m_fileHistory[i]));
- }
-}
-
// ----------------------------------------------------------------------------
// Permits compatibility with existing file formats and functions that
// manipulate files directly