1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/docview.cpp
3 // Purpose: Document/view classes
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #if wxUSE_DOC_VIEW_ARCHITECTURE
29 #include "wx/docview.h"
33 #include "wx/string.h"
37 #include "wx/dialog.h"
39 #include "wx/filedlg.h"
42 #include "wx/msgdlg.h"
44 #include "wx/choicdlg.h"
47 #if wxUSE_PRINTING_ARCHITECTURE
48 #include "wx/prntbase.h"
49 #include "wx/printdlg.h"
52 #include "wx/confbase.h"
53 #include "wx/filename.h"
56 #include "wx/cmdproc.h"
57 #include "wx/tokenzr.h"
58 #include "wx/filename.h"
59 #include "wx/vector.h"
61 #if wxUSE_STD_IOSTREAM
62 #include "wx/ioswrap.h"
63 #include "wx/beforestd.h"
69 #include "wx/afterstd.h"
71 #include "wx/wfstream.h"
74 typedef wxVector
<wxDocTemplate
*> wxDocTemplates
;
76 // ----------------------------------------------------------------------------
78 // ----------------------------------------------------------------------------
80 IMPLEMENT_ABSTRACT_CLASS(wxDocument
, wxEvtHandler
)
81 IMPLEMENT_ABSTRACT_CLASS(wxView
, wxEvtHandler
)
82 IMPLEMENT_ABSTRACT_CLASS(wxDocTemplate
, wxObject
)
83 IMPLEMENT_DYNAMIC_CLASS(wxDocManager
, wxEvtHandler
)
84 IMPLEMENT_CLASS(wxDocChildFrame
, wxFrame
)
85 IMPLEMENT_CLASS(wxDocParentFrame
, wxFrame
)
87 #if wxUSE_PRINTING_ARCHITECTURE
88 IMPLEMENT_DYNAMIC_CLASS(wxDocPrintout
, wxPrintout
)
91 IMPLEMENT_DYNAMIC_CLASS(wxFileHistory
, wxObject
)
93 // ============================================================================
95 // ============================================================================
97 // ----------------------------------------------------------------------------
99 // ----------------------------------------------------------------------------
104 wxWindow
*wxFindSuitableParent()
106 wxWindow
* const win
= wxGetTopLevelParent(wxWindow::FindFocus());
108 return win
? win
: wxTheApp
->GetTopWindow();
111 wxString
FindExtension(const wxString
& path
)
114 wxSplitPath(path
, NULL
, NULL
, &ext
);
116 // VZ: extensions are considered not case sensitive - is this really a good
118 return ext
.MakeLower();
121 // return the string used for the MRU list items in the menu
123 // NB: the index n is 0-based, as usual, but the strings start from 1
124 wxString
GetMRUEntryLabel(int n
, const wxString
& path
)
126 // we need to quote '&' characters which are used for mnemonics
127 wxString
pathInMenu(path
);
128 pathInMenu
.Replace("&", "&&");
130 return wxString::Format("&%d %s", n
+ 1, pathInMenu
);
133 } // anonymous namespace
135 // ----------------------------------------------------------------------------
136 // Definition of wxDocument
137 // ----------------------------------------------------------------------------
139 wxDocument::wxDocument(wxDocument
*parent
)
141 m_documentModified
= false;
142 m_documentParent
= parent
;
143 m_documentTemplate
= NULL
;
144 m_commandProcessor
= NULL
;
148 bool wxDocument::DeleteContents()
153 wxDocument::~wxDocument()
157 if (m_commandProcessor
)
158 delete m_commandProcessor
;
160 if (GetDocumentManager())
161 GetDocumentManager()->RemoveDocument(this);
163 // Not safe to do here, since it'll invoke virtual view functions
164 // expecting to see valid derived objects: and by the time we get here,
165 // we've called destructors higher up.
169 bool wxDocument::Close()
171 if (OnSaveModified())
172 return OnCloseDocument();
177 bool wxDocument::OnCloseDocument()
179 // Tell all views that we're about to close
186 // Note that this implicitly deletes the document when the last view is
188 bool wxDocument::DeleteAllViews()
190 wxDocManager
* manager
= GetDocumentManager();
192 // first check if all views agree to be closed
193 const wxList::iterator end
= m_documentViews
.end();
194 for ( wxList::iterator i
= m_documentViews
.begin(); i
!= end
; ++i
)
196 wxView
*view
= (wxView
*)*i
;
197 if ( !view
->Close() )
201 // all views agreed to close, now do close them
202 if ( m_documentViews
.empty() )
204 // normally the document would be implicitly deleted when the last view
205 // is, but if don't have any views, do it here instead
206 if ( manager
&& manager
->GetDocuments().Member(this) )
211 // as we delete elements we iterate over, don't use the usual "from
212 // begin to end" loop
215 wxView
*view
= (wxView
*)*m_documentViews
.begin();
217 bool isLastOne
= m_documentViews
.size() == 1;
219 // this always deletes the node implicitly and if this is the last
220 // view also deletes this object itself (also implicitly, great),
221 // so we can't test for m_documentViews.empty() after calling this!
232 wxView
*wxDocument::GetFirstView() const
234 if (m_documentViews
.GetCount() == 0)
236 return (wxView
*)m_documentViews
.GetFirst()->GetData();
239 wxDocManager
*wxDocument::GetDocumentManager() const
241 return m_documentTemplate
? m_documentTemplate
->GetDocumentManager() : NULL
;
244 bool wxDocument::OnNewDocument()
246 if ( !OnSaveModified() )
249 if ( !OnCloseDocument() )
253 SetDocumentSaved(false);
255 const wxString name
= GetDocumentManager()->MakeNewDocumentName();
257 SetFilename(name
, true);
262 bool wxDocument::Save()
264 if (!IsModified() && m_savedYet
)
267 if ( m_documentFile
.empty() || !m_savedYet
)
270 return OnSaveDocument(m_documentFile
);
273 bool wxDocument::SaveAs()
275 wxDocTemplate
*docTemplate
= GetDocumentTemplate();
279 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
280 wxString filter
= docTemplate
->GetDescription() + wxT(" (") + docTemplate
->GetFileFilter() + wxT(")|") + docTemplate
->GetFileFilter();
282 // Now see if there are some other template with identical view and document
283 // classes, whose filters may also be used.
285 if (docTemplate
->GetViewClassInfo() && docTemplate
->GetDocClassInfo())
287 wxList::compatibility_iterator node
= docTemplate
->GetDocumentManager()->GetTemplates().GetFirst();
290 wxDocTemplate
*t
= (wxDocTemplate
*) node
->GetData();
292 if (t
->IsVisible() && t
!= docTemplate
&&
293 t
->GetViewClassInfo() == docTemplate
->GetViewClassInfo() &&
294 t
->GetDocClassInfo() == docTemplate
->GetDocClassInfo())
296 // add a '|' to separate this filter from the previous one
297 if ( !filter
.empty() )
300 filter
<< t
->GetDescription() << wxT(" (") << t
->GetFileFilter() << wxT(") |")
301 << t
->GetFileFilter();
304 node
= node
->GetNext();
308 wxString filter
= docTemplate
->GetFileFilter() ;
310 wxString defaultDir
= docTemplate
->GetDirectory();
311 if (defaultDir
.IsEmpty())
312 defaultDir
= wxPathOnly(GetFilename());
314 wxString tmp
= wxFileSelector(_("Save As"),
316 wxFileNameFromPath(GetFilename()),
317 docTemplate
->GetDefaultExtension(),
319 wxFD_SAVE
| wxFD_OVERWRITE_PROMPT
,
320 GetDocumentWindow());
325 wxString
fileName(tmp
);
326 wxString path
, name
, ext
;
327 wxSplitPath(fileName
, & path
, & name
, & ext
);
331 fileName
+= wxT(".");
332 fileName
+= docTemplate
->GetDefaultExtension();
335 SetFilename(fileName
);
336 SetTitle(wxFileNameFromPath(fileName
));
338 // Notify the views that the filename has changed
339 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
342 wxView
*view
= (wxView
*)node
->GetData();
343 view
->OnChangeFilename();
344 node
= node
->GetNext();
347 // Files that were not saved correctly are not added to the FileHistory.
348 if (!OnSaveDocument(m_documentFile
))
351 // A file that doesn't use the default extension of its document template cannot be opened
352 // via the FileHistory, so we do not add it.
353 if (docTemplate
->FileMatchesTemplate(fileName
))
355 GetDocumentManager()->AddFileToHistory(fileName
);
359 // The user will probably not be able to open the file again, so
360 // we could warn about the wrong file-extension here.
365 bool wxDocument::OnSaveDocument(const wxString
& file
)
370 if ( !DoSaveDocument(file
) )
375 SetDocumentSaved(true);
376 #if defined( __WXOSX_MAC__ ) && wxOSX_USE_CARBON
377 wxFileName
fn(file
) ;
378 fn
.MacSetDefaultTypeAndCreator() ;
383 bool wxDocument::OnOpenDocument(const wxString
& file
)
385 if ( !OnSaveModified() )
388 if ( !DoOpenDocument(file
) )
391 SetFilename(file
, true);
400 #if wxUSE_STD_IOSTREAM
401 wxSTD istream
& wxDocument::LoadObject(wxSTD istream
& stream
)
403 wxInputStream
& wxDocument::LoadObject(wxInputStream
& stream
)
409 #if wxUSE_STD_IOSTREAM
410 wxSTD ostream
& wxDocument::SaveObject(wxSTD ostream
& stream
)
412 wxOutputStream
& wxDocument::SaveObject(wxOutputStream
& stream
)
418 bool wxDocument::Revert()
424 // Get title, or filename if no title, else unnamed
425 #if WXWIN_COMPATIBILITY_2_8
426 bool wxDocument::GetPrintableName(wxString
& buf
) const
428 // this function can not only be overridden by the user code but also
429 // called by it so we need to ensure that we return the same thing as
430 // GetUserReadableName() but we can't call it because this would result in
431 // an infinite recursion, hence we use the helper DoGetUserReadableName()
432 buf
= DoGetUserReadableName();
436 #endif // WXWIN_COMPATIBILITY_2_8
438 wxString
wxDocument::GetUserReadableName() const
440 #if WXWIN_COMPATIBILITY_2_8
441 // we need to call the old virtual function to ensure that the overridden
442 // version of it is still called
444 if ( GetPrintableName(name
) )
446 #endif // WXWIN_COMPATIBILITY_2_8
448 return DoGetUserReadableName();
451 wxString
wxDocument::DoGetUserReadableName() const
453 if ( !m_documentTitle
.empty() )
454 return m_documentTitle
;
456 if ( !m_documentFile
.empty() )
457 return wxFileNameFromPath(m_documentFile
);
462 wxWindow
*wxDocument::GetDocumentWindow() const
464 wxView
*view
= GetFirstView();
466 return view
->GetFrame();
468 return wxTheApp
->GetTopWindow();
471 wxCommandProcessor
*wxDocument::OnCreateCommandProcessor()
473 return new wxCommandProcessor
;
476 // true if safe to close
477 bool wxDocument::OnSaveModified()
481 switch ( wxMessageBox
485 _("Do you want to save changes to document %s?"),
486 GetUserReadableName()
488 wxTheApp
->GetAppDisplayName(),
489 wxYES_NO
| wxCANCEL
| wxICON_QUESTION
,
508 bool wxDocument::Draw(wxDC
& WXUNUSED(context
))
513 bool wxDocument::AddView(wxView
*view
)
515 if ( !m_documentViews
.Member(view
) )
517 m_documentViews
.Append(view
);
523 bool wxDocument::RemoveView(wxView
*view
)
525 (void)m_documentViews
.DeleteObject(view
);
530 bool wxDocument::OnCreate(const wxString
& WXUNUSED(path
), long flags
)
532 return GetDocumentTemplate()->CreateView(this, flags
) != NULL
;
535 // Called after a view is added or removed.
536 // The default implementation deletes the document if
537 // there are no more views.
538 void wxDocument::OnChangedViewList()
540 if ( m_documentViews
.empty() && OnSaveModified() )
544 void wxDocument::UpdateAllViews(wxView
*sender
, wxObject
*hint
)
546 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
549 wxView
*view
= (wxView
*)node
->GetData();
551 view
->OnUpdate(sender
, hint
);
552 node
= node
->GetNext();
556 void wxDocument::NotifyClosing()
558 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
561 wxView
*view
= (wxView
*)node
->GetData();
562 view
->OnClosingDocument();
563 node
= node
->GetNext();
567 void wxDocument::SetFilename(const wxString
& filename
, bool notifyViews
)
569 m_documentFile
= filename
;
572 // Notify the views that the filename has changed
573 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
576 wxView
*view
= (wxView
*)node
->GetData();
577 view
->OnChangeFilename();
578 node
= node
->GetNext();
583 bool wxDocument::DoSaveDocument(const wxString
& file
)
586 if (!wxTheApp
->GetAppDisplayName().empty())
587 msgTitle
= wxTheApp
->GetAppDisplayName();
589 msgTitle
= wxString(_("File error"));
591 #if wxUSE_STD_IOSTREAM
592 wxSTD ofstream
store(file
.mb_str(), wxSTD
ios::binary
);
593 if (store
.fail() || store
.bad())
595 wxFileOutputStream
store(file
);
596 if (store
.GetLastError() != wxSTREAM_NO_ERROR
)
599 (void)wxMessageBox(_("Sorry, could not open this file for saving."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
600 GetDocumentWindow());
604 if (!SaveObject(store
))
606 (void)wxMessageBox(_("Sorry, could not save this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
607 GetDocumentWindow());
615 bool wxDocument::DoOpenDocument(const wxString
& file
)
617 #if wxUSE_STD_IOSTREAM
618 wxSTD ifstream
store(file
.mb_str(), wxSTD
ios::binary
);
619 if (!store
.fail() && !store
.bad())
621 wxFileInputStream
store(file
);
622 if (store
.GetLastError() == wxSTREAM_NO_ERROR
)
625 #if wxUSE_STD_IOSTREAM
627 if ( !!store
|| store
.eof() )
629 int res
= LoadObject(store
).GetLastError();
630 if ( res
== wxSTREAM_NO_ERROR
|| res
== wxSTREAM_EOF
)
635 wxLogError(_("Sorry, could not open this file."));
640 // ----------------------------------------------------------------------------
642 // ----------------------------------------------------------------------------
646 m_viewDocument
= NULL
;
653 GetDocumentManager()->ActivateView(this, false);
654 m_viewDocument
->RemoveView(this);
657 // Extend event processing to search the document's event table
658 bool wxView::ProcessEvent(wxEvent
& event
)
660 if ( !GetDocument() || !GetDocument()->ProcessEvent(event
) )
661 return wxEvtHandler::ProcessEvent(event
);
666 void wxView::OnActivateView(bool WXUNUSED(activate
), wxView
*WXUNUSED(activeView
), wxView
*WXUNUSED(deactiveView
))
670 void wxView::OnPrint(wxDC
*dc
, wxObject
*WXUNUSED(info
))
675 void wxView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
))
679 void wxView::OnChangeFilename()
681 // GetFrame can return wxWindow rather than wxTopLevelWindow due to
682 // generic MDI implementation so use SetLabel rather than SetTitle.
683 // It should cause SetTitle() for top level windows.
684 wxWindow
*win
= GetFrame();
687 wxDocument
*doc
= GetDocument();
690 win
->SetLabel(doc
->GetUserReadableName());
693 void wxView::SetDocument(wxDocument
*doc
)
695 m_viewDocument
= doc
;
700 bool wxView::Close(bool deleteWindow
)
702 return OnClose(deleteWindow
);
705 void wxView::Activate(bool activate
)
707 if (GetDocument() && GetDocumentManager())
709 OnActivateView(activate
, this, GetDocumentManager()->GetCurrentView());
710 GetDocumentManager()->ActivateView(this, activate
);
714 bool wxView::OnClose(bool WXUNUSED(deleteWindow
))
716 return GetDocument() ? GetDocument()->Close() : true;
719 #if wxUSE_PRINTING_ARCHITECTURE
720 wxPrintout
*wxView::OnCreatePrintout()
722 return new wxDocPrintout(this);
724 #endif // wxUSE_PRINTING_ARCHITECTURE
726 // ----------------------------------------------------------------------------
728 // ----------------------------------------------------------------------------
730 wxDocTemplate::wxDocTemplate(wxDocManager
*manager
,
731 const wxString
& descr
,
732 const wxString
& filter
,
735 const wxString
& docTypeName
,
736 const wxString
& viewTypeName
,
737 wxClassInfo
*docClassInfo
,
738 wxClassInfo
*viewClassInfo
,
741 m_documentManager
= manager
;
742 m_description
= descr
;
745 m_fileFilter
= filter
;
747 m_docTypeName
= docTypeName
;
748 m_viewTypeName
= viewTypeName
;
749 m_documentManager
->AssociateTemplate(this);
751 m_docClassInfo
= docClassInfo
;
752 m_viewClassInfo
= viewClassInfo
;
755 wxDocTemplate::~wxDocTemplate()
757 m_documentManager
->DisassociateTemplate(this);
760 // Tries to dynamically construct an object of the right class.
761 wxDocument
*wxDocTemplate::CreateDocument(const wxString
& path
, long flags
)
763 wxDocument
* const doc
= DoCreateDocument();
765 // VZ: this code doesn't delete doc if InitDocument() (i.e. doc->OnCreate())
766 // fails, is this intentional?
768 return doc
&& InitDocument(doc
, path
, flags
) ? doc
: NULL
;
772 wxDocTemplate::InitDocument(wxDocument
* doc
, const wxString
& path
, long flags
)
774 doc
->SetFilename(path
);
775 doc
->SetDocumentTemplate(this);
776 GetDocumentManager()->AddDocument(doc
);
777 doc
->SetCommandProcessor(doc
->OnCreateCommandProcessor());
779 if (doc
->OnCreate(path
, flags
))
783 if (GetDocumentManager()->GetDocuments().Member(doc
))
784 doc
->DeleteAllViews();
789 wxView
*wxDocTemplate::CreateView(wxDocument
*doc
, long flags
)
791 wxView
*view
= DoCreateView();
795 view
->SetDocument(doc
);
796 if (view
->OnCreate(doc
, flags
))
807 // The default (very primitive) format detection: check is the extension is
808 // that of the template
809 bool wxDocTemplate::FileMatchesTemplate(const wxString
& path
)
811 wxStringTokenizer
parser (GetFileFilter(), wxT(";"));
812 wxString anything
= wxT ("*");
813 while (parser
.HasMoreTokens())
815 wxString filter
= parser
.GetNextToken();
816 wxString filterExt
= FindExtension (filter
);
817 if ( filter
.IsSameAs (anything
) ||
818 filterExt
.IsSameAs (anything
) ||
819 filterExt
.IsSameAs (FindExtension (path
)) )
822 return GetDefaultExtension().IsSameAs(FindExtension(path
));
825 wxDocument
*wxDocTemplate::DoCreateDocument()
830 return (wxDocument
*)m_docClassInfo
->CreateObject();
833 wxView
*wxDocTemplate::DoCreateView()
835 if (!m_viewClassInfo
)
838 return (wxView
*)m_viewClassInfo
->CreateObject();
841 // ----------------------------------------------------------------------------
843 // ----------------------------------------------------------------------------
845 BEGIN_EVENT_TABLE(wxDocManager
, wxEvtHandler
)
846 EVT_MENU(wxID_OPEN
, wxDocManager::OnFileOpen
)
847 EVT_MENU(wxID_CLOSE
, wxDocManager::OnFileClose
)
848 EVT_MENU(wxID_CLOSE_ALL
, wxDocManager::OnFileCloseAll
)
849 EVT_MENU(wxID_REVERT
, wxDocManager::OnFileRevert
)
850 EVT_MENU(wxID_NEW
, wxDocManager::OnFileNew
)
851 EVT_MENU(wxID_SAVE
, wxDocManager::OnFileSave
)
852 EVT_MENU(wxID_SAVEAS
, wxDocManager::OnFileSaveAs
)
853 EVT_MENU(wxID_UNDO
, wxDocManager::OnUndo
)
854 EVT_MENU(wxID_REDO
, wxDocManager::OnRedo
)
856 EVT_UPDATE_UI(wxID_OPEN
, wxDocManager::OnUpdateFileOpen
)
857 EVT_UPDATE_UI(wxID_CLOSE
, wxDocManager::OnUpdateDisableIfNoDoc
)
858 EVT_UPDATE_UI(wxID_CLOSE_ALL
, wxDocManager::OnUpdateDisableIfNoDoc
)
859 EVT_UPDATE_UI(wxID_REVERT
, wxDocManager::OnUpdateDisableIfNoDoc
)
860 EVT_UPDATE_UI(wxID_NEW
, wxDocManager::OnUpdateFileNew
)
861 EVT_UPDATE_UI(wxID_SAVE
, wxDocManager::OnUpdateFileSave
)
862 EVT_UPDATE_UI(wxID_SAVEAS
, wxDocManager::OnUpdateDisableIfNoDoc
)
863 EVT_UPDATE_UI(wxID_UNDO
, wxDocManager::OnUpdateUndo
)
864 EVT_UPDATE_UI(wxID_REDO
, wxDocManager::OnUpdateRedo
)
866 #if wxUSE_PRINTING_ARCHITECTURE
867 EVT_MENU(wxID_PRINT
, wxDocManager::OnPrint
)
868 EVT_MENU(wxID_PREVIEW
, wxDocManager::OnPreview
)
870 EVT_UPDATE_UI(wxID_PRINT
, wxDocManager::OnUpdateDisableIfNoDoc
)
871 EVT_UPDATE_UI(wxID_PREVIEW
, wxDocManager::OnUpdateDisableIfNoDoc
)
875 wxDocManager
* wxDocManager::sm_docManager
= NULL
;
877 wxDocManager::wxDocManager(long WXUNUSED(flags
), bool initialize
)
879 wxASSERT_MSG( !sm_docManager
, "multiple wxDocManagers not allowed" );
881 sm_docManager
= this;
883 m_defaultDocumentNameCounter
= 1;
884 m_currentView
= NULL
;
885 m_maxDocsOpen
= INT_MAX
;
886 m_fileHistory
= NULL
;
891 wxDocManager::~wxDocManager()
894 delete m_fileHistory
;
895 sm_docManager
= NULL
;
898 // closes the specified document
899 bool wxDocManager::CloseDocument(wxDocument
* doc
, bool force
)
901 if (doc
->Close() || force
)
903 // Implicitly deletes the document when
904 // the last view is deleted
905 doc
->DeleteAllViews();
907 // Check we're really deleted
908 if (m_docs
.Member(doc
))
916 bool wxDocManager::CloseDocuments(bool force
)
918 wxList::compatibility_iterator node
= m_docs
.GetFirst();
921 wxDocument
*doc
= (wxDocument
*)node
->GetData();
922 wxList::compatibility_iterator next
= node
->GetNext();
924 if (!CloseDocument(doc
, force
))
927 // This assumes that documents are not connected in
928 // any way, i.e. deleting one document does NOT
935 bool wxDocManager::Clear(bool force
)
937 if (!CloseDocuments(force
))
940 m_currentView
= NULL
;
942 wxList::compatibility_iterator node
= m_templates
.GetFirst();
945 wxDocTemplate
*templ
= (wxDocTemplate
*) node
->GetData();
946 wxList::compatibility_iterator next
= node
->GetNext();
953 bool wxDocManager::Initialize()
955 m_fileHistory
= OnCreateFileHistory();
959 wxFileHistory
*wxDocManager::OnCreateFileHistory()
961 return new wxFileHistory
;
964 void wxDocManager::OnFileClose(wxCommandEvent
& WXUNUSED(event
))
966 wxDocument
*doc
= GetCurrentDocument();
971 doc
->DeleteAllViews();
972 if (m_docs
.Member(doc
))
977 void wxDocManager::OnFileCloseAll(wxCommandEvent
& WXUNUSED(event
))
979 CloseDocuments(false);
982 void wxDocManager::OnFileNew(wxCommandEvent
& WXUNUSED(event
))
984 CreateDocument( wxEmptyString
, wxDOC_NEW
);
987 void wxDocManager::OnFileOpen(wxCommandEvent
& WXUNUSED(event
))
989 if ( !CreateDocument( wxEmptyString
, 0) )
995 void wxDocManager::OnFileRevert(wxCommandEvent
& WXUNUSED(event
))
997 wxDocument
*doc
= GetCurrentDocument();
1003 void wxDocManager::OnFileSave(wxCommandEvent
& WXUNUSED(event
))
1005 wxDocument
*doc
= GetCurrentDocument();
1011 void wxDocManager::OnFileSaveAs(wxCommandEvent
& WXUNUSED(event
))
1013 wxDocument
*doc
= GetCurrentDocument();
1019 void wxDocManager::OnPrint(wxCommandEvent
& WXUNUSED(event
))
1021 #if wxUSE_PRINTING_ARCHITECTURE
1022 wxView
*view
= GetCurrentView();
1026 wxPrintout
*printout
= view
->OnCreatePrintout();
1030 printer
.Print(view
->GetFrame(), printout
, true);
1034 #endif // wxUSE_PRINTING_ARCHITECTURE
1037 void wxDocManager::OnPreview(wxCommandEvent
& WXUNUSED(event
))
1039 #if wxUSE_PRINTING_ARCHITECTURE
1040 wxView
*view
= GetCurrentView();
1044 wxPrintout
*printout
= view
->OnCreatePrintout();
1047 // Pass two printout objects: for preview, and possible printing.
1048 wxPrintPreviewBase
*preview
= new wxPrintPreview(printout
, view
->OnCreatePrintout());
1049 if ( !preview
->Ok() )
1052 wxMessageBox( _("Sorry, print preview needs a printer to be installed.") );
1056 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, (wxFrame
*)wxTheApp
->GetTopWindow(), _("Print Preview"),
1057 wxPoint(100, 100), wxSize(600, 650));
1058 frame
->Centre(wxBOTH
);
1059 frame
->Initialize();
1062 #endif // wxUSE_PRINTING_ARCHITECTURE
1065 void wxDocManager::OnUndo(wxCommandEvent
& event
)
1067 wxDocument
*doc
= GetCurrentDocument();
1070 if (doc
->GetCommandProcessor())
1071 doc
->GetCommandProcessor()->Undo();
1076 void wxDocManager::OnRedo(wxCommandEvent
& event
)
1078 wxDocument
*doc
= GetCurrentDocument();
1081 if (doc
->GetCommandProcessor())
1082 doc
->GetCommandProcessor()->Redo();
1087 // Handlers for UI update commands
1089 void wxDocManager::OnUpdateFileOpen(wxUpdateUIEvent
& event
)
1091 event
.Enable( true );
1094 void wxDocManager::OnUpdateDisableIfNoDoc(wxUpdateUIEvent
& event
)
1096 event
.Enable( GetCurrentDocument() != NULL
);
1099 void wxDocManager::OnUpdateFileNew(wxUpdateUIEvent
& event
)
1101 event
.Enable( true );
1104 void wxDocManager::OnUpdateFileSave(wxUpdateUIEvent
& event
)
1106 wxDocument
*doc
= GetCurrentDocument();
1107 event
.Enable( doc
&& doc
->IsModified() );
1110 void wxDocManager::OnUpdateUndo(wxUpdateUIEvent
& event
)
1112 wxDocument
*doc
= GetCurrentDocument();
1114 event
.Enable(false);
1115 else if (!doc
->GetCommandProcessor())
1119 event
.Enable( doc
->GetCommandProcessor()->CanUndo() );
1120 doc
->GetCommandProcessor()->SetMenuStrings();
1124 void wxDocManager::OnUpdateRedo(wxUpdateUIEvent
& event
)
1126 wxDocument
*doc
= GetCurrentDocument();
1128 event
.Enable(false);
1129 else if (!doc
->GetCommandProcessor())
1133 event
.Enable( doc
->GetCommandProcessor()->CanRedo() );
1134 doc
->GetCommandProcessor()->SetMenuStrings();
1138 wxView
*wxDocManager::GetCurrentView() const
1141 return m_currentView
;
1142 if (m_docs
.GetCount() == 1)
1144 wxDocument
* doc
= (wxDocument
*) m_docs
.GetFirst()->GetData();
1145 return doc
->GetFirstView();
1150 // Extend event processing to search the view's event table
1151 bool wxDocManager::ProcessEvent(wxEvent
& event
)
1153 wxView
* const view
= GetCurrentView();
1154 if ( view
&& view
->ProcessEvent(event
) )
1157 return wxEvtHandler::ProcessEvent(event
);
1163 // helper function: return only the visible templates
1164 wxDocTemplates
GetVisibleTemplates(const wxList
& allTemplates
)
1166 // select only the visible templates
1167 const size_t totalNumTemplates
= allTemplates
.GetCount();
1168 wxDocTemplates templates
;
1169 if ( totalNumTemplates
)
1171 templates
.reserve(totalNumTemplates
);
1173 for ( wxList::const_iterator i
= allTemplates
.begin(),
1174 end
= allTemplates
.end();
1178 wxDocTemplate
* const temp
= (wxDocTemplate
*)*i
;
1179 if ( temp
->IsVisible() )
1180 templates
.push_back(temp
);
1187 } // anonymous namespace
1189 wxDocument
*wxDocManager::CreateDocument(const wxString
& pathOrig
, long flags
)
1191 // this ought to be const but SelectDocumentType/Path() are not
1192 // const-correct and can't be changed as, being virtual, this risks
1193 // breaking user code overriding them
1194 wxDocTemplates
templates(GetVisibleTemplates(m_templates
));
1195 const size_t numTemplates
= templates
.size();
1196 if ( !numTemplates
)
1198 // no templates can be used, can't create document
1203 // normally user should select the template to use but wxDOC_SILENT flag we
1204 // choose one ourselves
1205 wxString path
= pathOrig
; // may be modified below
1206 wxDocTemplate
*temp
;
1207 if ( flags
& wxDOC_SILENT
)
1209 wxASSERT_MSG( !path
.empty(),
1210 "using empty path with wxDOC_SILENT doesn't make sense" );
1212 temp
= FindTemplateForPath(path
);
1215 wxLogWarning(_("The format of file '%s' couldn't be determined."),
1219 else // not silent, ask the user
1221 // for the new file we need just the template, for an existing one we
1222 // need the template and the path, unless it's already specified
1223 if ( (flags
& wxDOC_NEW
) || !path
.empty() )
1224 temp
= SelectDocumentType(&templates
[0], numTemplates
);
1226 temp
= SelectDocumentPath(&templates
[0], numTemplates
, path
, flags
);
1232 // check whether the document with this path is already opened
1233 if ( !path
.empty() )
1235 const wxFileName
fn(path
);
1236 for ( wxList::const_iterator i
= m_docs
.begin(); i
!= m_docs
.end(); ++i
)
1238 wxDocument
* const doc
= (wxDocument
*)*i
;
1240 if ( fn
== doc
->GetFilename() )
1242 // file already open, just activate it and return
1243 if ( doc
->GetFirstView() )
1245 ActivateView(doc
->GetFirstView());
1246 if ( doc
->GetDocumentWindow() )
1247 doc
->GetDocumentWindow()->SetFocus();
1255 // no, we need to create a new document
1258 // if we've reached the max number of docs, close the first one.
1259 if ( (int)GetDocuments().GetCount() >= m_maxDocsOpen
)
1261 if ( !CloseDocument((wxDocument
*)GetDocuments().GetFirst()->GetData()) )
1263 // can't open the new document if closing the old one failed
1269 // do create and initialize the new document finally
1270 wxDocument
* const docNew
= temp
->CreateDocument(path
, flags
);
1274 docNew
->SetDocumentName(temp
->GetDocumentName());
1275 docNew
->SetDocumentTemplate(temp
);
1277 // call the appropriate function depending on whether we're creating a new
1278 // file or opening an existing one
1279 if ( !(flags
& wxDOC_NEW
? docNew
->OnNewDocument()
1280 : docNew
->OnOpenDocument(path
)) )
1282 // Document is implicitly deleted by DeleteAllViews
1283 docNew
->DeleteAllViews();
1287 // add the successfully opened file to MRU, but only if we're going to be
1288 // able to reopen it successfully later which requires the template for
1289 // this document to be retrievable from the file extension
1290 if ( !(flags
& wxDOC_NEW
) && temp
->FileMatchesTemplate(path
) )
1291 AddFileToHistory(path
);
1296 wxView
*wxDocManager::CreateView(wxDocument
*doc
, long flags
)
1298 wxDocTemplates
templates(GetVisibleTemplates(m_templates
));
1299 const size_t numTemplates
= templates
.size();
1301 if ( numTemplates
== 0 )
1304 wxDocTemplate
* const
1305 temp
= numTemplates
== 1 ? templates
[0]
1306 : SelectViewType(&templates
[0], numTemplates
);
1311 wxView
*view
= temp
->CreateView(doc
, flags
);
1313 view
->SetViewName(temp
->GetViewName());
1317 // Not yet implemented
1319 wxDocManager::DeleteTemplate(wxDocTemplate
*WXUNUSED(temp
), long WXUNUSED(flags
))
1323 // Not yet implemented
1324 bool wxDocManager::FlushDoc(wxDocument
*WXUNUSED(doc
))
1329 wxDocument
*wxDocManager::GetCurrentDocument() const
1331 wxView
*view
= GetCurrentView();
1333 return view
->GetDocument();
1338 // Make a default name for a new document
1339 #if WXWIN_COMPATIBILITY_2_8
1340 bool wxDocManager::MakeDefaultName(wxString
& WXUNUSED(name
))
1342 // we consider that this function can only be overridden by the user code,
1343 // not called by it as it only makes sense to call it internally, so we
1344 // don't bother to return anything from here
1347 #endif // WXWIN_COMPATIBILITY_2_8
1349 wxString
wxDocManager::MakeNewDocumentName()
1353 #if WXWIN_COMPATIBILITY_2_8
1354 if ( !MakeDefaultName(name
) )
1355 #endif // WXWIN_COMPATIBILITY_2_8
1357 name
.Printf(_("unnamed%d"), m_defaultDocumentNameCounter
);
1358 m_defaultDocumentNameCounter
++;
1364 // Make a frame title (override this to do something different)
1365 // If docName is empty, a document is not currently active.
1366 wxString
wxDocManager::MakeFrameTitle(wxDocument
* doc
)
1368 wxString appName
= wxTheApp
->GetAppDisplayName();
1374 wxString docName
= doc
->GetUserReadableName();
1375 title
= docName
+ wxString(_(" - ")) + appName
;
1381 // Not yet implemented
1382 wxDocTemplate
*wxDocManager::MatchTemplate(const wxString
& WXUNUSED(path
))
1387 // File history management
1388 void wxDocManager::AddFileToHistory(const wxString
& file
)
1391 m_fileHistory
->AddFileToHistory(file
);
1394 void wxDocManager::RemoveFileFromHistory(size_t i
)
1397 m_fileHistory
->RemoveFileFromHistory(i
);
1400 wxString
wxDocManager::GetHistoryFile(size_t i
) const
1405 histFile
= m_fileHistory
->GetHistoryFile(i
);
1410 void wxDocManager::FileHistoryUseMenu(wxMenu
*menu
)
1413 m_fileHistory
->UseMenu(menu
);
1416 void wxDocManager::FileHistoryRemoveMenu(wxMenu
*menu
)
1419 m_fileHistory
->RemoveMenu(menu
);
1423 void wxDocManager::FileHistoryLoad(const wxConfigBase
& config
)
1426 m_fileHistory
->Load(config
);
1429 void wxDocManager::FileHistorySave(wxConfigBase
& config
)
1432 m_fileHistory
->Save(config
);
1436 void wxDocManager::FileHistoryAddFilesToMenu(wxMenu
* menu
)
1439 m_fileHistory
->AddFilesToMenu(menu
);
1442 void wxDocManager::FileHistoryAddFilesToMenu()
1445 m_fileHistory
->AddFilesToMenu();
1448 size_t wxDocManager::GetHistoryFilesCount() const
1450 return m_fileHistory
? m_fileHistory
->GetCount() : 0;
1454 // Find out the document template via matching in the document file format
1455 // against that of the template
1456 wxDocTemplate
*wxDocManager::FindTemplateForPath(const wxString
& path
)
1458 wxDocTemplate
*theTemplate
= NULL
;
1460 // Find the template which this extension corresponds to
1461 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1463 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Item(i
)->GetData();
1464 if ( temp
->FileMatchesTemplate(path
) )
1473 // Prompts user to open a file, using file specs in templates.
1474 // Must extend the file selector dialog or implement own; OR
1475 // match the extension to the template extension.
1477 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**templates
,
1480 long WXUNUSED(flags
),
1481 bool WXUNUSED(save
))
1483 // We can only have multiple filters in Windows and GTK
1484 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
1488 for (i
= 0; i
< noTemplates
; i
++)
1490 if (templates
[i
]->IsVisible())
1492 // add a '|' to separate this filter from the previous one
1493 if ( !descrBuf
.empty() )
1494 descrBuf
<< wxT('|');
1496 descrBuf
<< templates
[i
]->GetDescription()
1497 << wxT(" (") << templates
[i
]->GetFileFilter() << wxT(") |")
1498 << templates
[i
]->GetFileFilter();
1502 wxString descrBuf
= wxT("*.*");
1503 wxUnusedVar(noTemplates
);
1506 int FilterIndex
= -1;
1508 wxWindow
* parent
= wxFindSuitableParent();
1510 wxString pathTmp
= wxFileSelectorEx(_("Open File"),
1518 wxDocTemplate
*theTemplate
= NULL
;
1519 if (!pathTmp
.empty())
1521 if (!wxFileExists(pathTmp
))
1524 if (!wxTheApp
->GetAppDisplayName().empty())
1525 msgTitle
= wxTheApp
->GetAppDisplayName();
1527 msgTitle
= wxString(_("File error"));
1529 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
1532 path
= wxEmptyString
;
1535 m_lastDirectory
= wxPathOnly(pathTmp
);
1539 // first choose the template using the extension, if this fails (i.e.
1540 // wxFileSelectorEx() didn't fill it), then use the path
1541 if ( FilterIndex
!= -1 )
1542 theTemplate
= templates
[FilterIndex
];
1544 theTemplate
= FindTemplateForPath(path
);
1547 // Since we do not add files with non-default extensions to the FileHistory this
1548 // can only happen if the application changes the allowed templates in runtime.
1549 (void)wxMessageBox(_("Sorry, the format for this file is unknown."),
1551 wxOK
| wxICON_EXCLAMATION
, wxFindSuitableParent());
1556 path
= wxEmptyString
;
1562 wxDocTemplate
*wxDocManager::SelectDocumentType(wxDocTemplate
**templates
,
1563 int noTemplates
, bool sort
)
1565 wxArrayString strings
;
1566 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1570 for (i
= 0; i
< noTemplates
; i
++)
1572 if (templates
[i
]->IsVisible())
1576 for (j
= 0; j
< n
; j
++)
1578 //filter out NOT unique documents + view combinations
1579 if ( templates
[i
]->m_docTypeName
== data
[j
]->m_docTypeName
&&
1580 templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
1587 strings
.Add(templates
[i
]->m_description
);
1589 data
[n
] = templates
[i
];
1597 strings
.Sort(); // ascending sort
1598 // Yes, this will be slow, but template lists
1599 // are typically short.
1601 n
= strings
.Count();
1602 for (i
= 0; i
< n
; i
++)
1604 for (j
= 0; j
< noTemplates
; j
++)
1606 if (strings
[i
] == templates
[j
]->m_description
)
1607 data
[i
] = templates
[j
];
1612 wxDocTemplate
*theTemplate
;
1617 // no visible templates, hence nothing to choose from
1622 // don't propose the user to choose if he has no choice
1623 theTemplate
= data
[0];
1627 // propose the user to choose one of several
1628 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1630 _("Select a document template"),
1634 wxFindSuitableParent()
1643 wxDocTemplate
*wxDocManager::SelectViewType(wxDocTemplate
**templates
,
1644 int noTemplates
, bool sort
)
1646 wxArrayString strings
;
1647 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1651 for (i
= 0; i
< noTemplates
; i
++)
1653 wxDocTemplate
*templ
= templates
[i
];
1654 if ( templ
->IsVisible() && !templ
->GetViewName().empty() )
1658 for (j
= 0; j
< n
; j
++)
1660 //filter out NOT unique views
1661 if ( templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
)
1667 strings
.Add(templ
->m_viewTypeName
);
1676 strings
.Sort(); // ascending sort
1677 // Yes, this will be slow, but template lists
1678 // are typically short.
1680 n
= strings
.Count();
1681 for (i
= 0; i
< n
; i
++)
1683 for (j
= 0; j
< noTemplates
; j
++)
1685 if (strings
[i
] == templates
[j
]->m_viewTypeName
)
1686 data
[i
] = templates
[j
];
1691 wxDocTemplate
*theTemplate
;
1693 // the same logic as above
1701 theTemplate
= data
[0];
1705 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1707 _("Select a document view"),
1711 wxFindSuitableParent()
1720 void wxDocManager::AssociateTemplate(wxDocTemplate
*temp
)
1722 if (!m_templates
.Member(temp
))
1723 m_templates
.Append(temp
);
1726 void wxDocManager::DisassociateTemplate(wxDocTemplate
*temp
)
1728 m_templates
.DeleteObject(temp
);
1731 // Add and remove a document from the manager's list
1732 void wxDocManager::AddDocument(wxDocument
*doc
)
1734 if (!m_docs
.Member(doc
))
1738 void wxDocManager::RemoveDocument(wxDocument
*doc
)
1740 m_docs
.DeleteObject(doc
);
1743 // Views or windows should inform the document manager
1744 // when a view is going in or out of focus
1745 void wxDocManager::ActivateView(wxView
*view
, bool activate
)
1749 m_currentView
= view
;
1753 if ( m_currentView
== view
)
1755 // don't keep stale pointer
1756 m_currentView
= NULL
;
1761 // ----------------------------------------------------------------------------
1762 // Default document child frame
1763 // ----------------------------------------------------------------------------
1765 BEGIN_EVENT_TABLE(wxDocChildFrame
, wxFrame
)
1766 EVT_ACTIVATE(wxDocChildFrame::OnActivate
)
1767 EVT_CLOSE(wxDocChildFrame::OnCloseWindow
)
1770 wxDocChildFrame::wxDocChildFrame(wxDocument
*doc
,
1774 const wxString
& title
,
1778 const wxString
& name
)
1779 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1781 m_childDocument
= doc
;
1784 view
->SetFrame(this);
1787 // Extend event processing to search the view's event table
1788 bool wxDocChildFrame::ProcessEvent(wxEvent
& event
)
1791 m_childView
->Activate(true);
1793 if ( !m_childView
|| ! m_childView
->ProcessEvent(event
) )
1795 // Only hand up to the parent if it's a menu command
1796 if (!event
.IsKindOf(CLASSINFO(wxCommandEvent
)) || !GetParent() || !GetParent()->ProcessEvent(event
))
1797 return wxEvtHandler::ProcessEvent(event
);
1805 void wxDocChildFrame::OnActivate(wxActivateEvent
& event
)
1807 wxFrame::OnActivate(event
);
1810 m_childView
->Activate(event
.GetActive());
1813 void wxDocChildFrame::OnCloseWindow(wxCloseEvent
& event
)
1817 bool ans
= event
.CanVeto()
1818 ? m_childView
->Close(false) // false means don't delete associated window
1819 : true; // Must delete.
1823 m_childView
->Activate(false);
1826 m_childDocument
= NULL
;
1837 // ----------------------------------------------------------------------------
1838 // Default parent frame
1839 // ----------------------------------------------------------------------------
1841 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1842 EVT_MENU(wxID_EXIT
, wxDocParentFrame::OnExit
)
1843 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE9
, wxDocParentFrame::OnMRUFile
)
1844 EVT_CLOSE(wxDocParentFrame::OnCloseWindow
)
1847 wxDocParentFrame::wxDocParentFrame()
1849 m_docManager
= NULL
;
1852 wxDocParentFrame::wxDocParentFrame(wxDocManager
*manager
,
1855 const wxString
& title
,
1859 const wxString
& name
)
1860 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1862 m_docManager
= manager
;
1865 bool wxDocParentFrame::Create(wxDocManager
*manager
,
1868 const wxString
& title
,
1872 const wxString
& name
)
1874 m_docManager
= manager
;
1875 return base_type::Create(frame
, id
, title
, pos
, size
, style
, name
);
1878 void wxDocParentFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
1883 void wxDocParentFrame::OnMRUFile(wxCommandEvent
& event
)
1885 int n
= event
.GetId() - wxID_FILE1
; // the index in MRU list
1886 wxString
filename(m_docManager
->GetHistoryFile(n
));
1887 if ( filename
.empty() )
1890 wxString errMsg
; // must contain exactly one "%s" if non-empty
1891 if ( wxFile::Exists(filename
) )
1894 if ( m_docManager
->CreateDocument(filename
, wxDOC_SILENT
) )
1897 errMsg
= _("The file '%s' couldn't be opened.");
1899 else // file doesn't exist
1901 errMsg
= _("The file '%s' doesn't exist and couldn't be opened.");
1905 wxASSERT_MSG( !errMsg
.empty(), "should have an error message" );
1907 // remove the file which we can't open from the MRU list
1908 m_docManager
->RemoveFileFromHistory(n
);
1910 // and tell the user about it
1911 wxLogError(errMsg
+ '\n' +
1912 _("It has been removed from the most recently used files list."),
1916 // Extend event processing to search the view's event table
1917 bool wxDocParentFrame::ProcessEvent(wxEvent
& event
)
1919 // Try the document manager, then do default processing
1920 if (!m_docManager
|| !m_docManager
->ProcessEvent(event
))
1921 return wxEvtHandler::ProcessEvent(event
);
1926 // Define the behaviour for the frame closing
1927 // - must delete all frames except for the main one.
1928 void wxDocParentFrame::OnCloseWindow(wxCloseEvent
& event
)
1930 if (m_docManager
->Clear(!event
.CanVeto()))
1938 #if wxUSE_PRINTING_ARCHITECTURE
1940 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
)
1943 m_printoutView
= view
;
1946 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
1950 // Get the logical pixels per inch of screen and printer
1951 int ppiScreenX
, ppiScreenY
;
1952 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
1953 wxUnusedVar(ppiScreenY
);
1954 int ppiPrinterX
, ppiPrinterY
;
1955 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
1956 wxUnusedVar(ppiPrinterY
);
1958 // This scales the DC so that the printout roughly represents the
1959 // the screen scaling. The text point size _should_ be the right size
1960 // but in fact is too small for some reason. This is a detail that will
1961 // need to be addressed at some point but can be fudged for the
1963 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
1965 // Now we have to check in case our real page size is reduced
1966 // (e.g. because we're drawing to a print preview memory DC)
1967 int pageWidth
, pageHeight
;
1969 dc
->GetSize(&w
, &h
);
1970 GetPageSizePixels(&pageWidth
, &pageHeight
);
1971 wxUnusedVar(pageHeight
);
1973 // If printer pageWidth == current DC width, then this doesn't
1974 // change. But w might be the preview bitmap width, so scale down.
1975 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
1976 dc
->SetUserScale(overallScale
, overallScale
);
1980 m_printoutView
->OnDraw(dc
);
1985 bool wxDocPrintout::HasPage(int pageNum
)
1987 return (pageNum
== 1);
1990 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
1992 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
1998 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
2006 #endif // wxUSE_PRINTING_ARCHITECTURE
2008 // ----------------------------------------------------------------------------
2009 // File history (a.k.a. MRU, most recently used, files list)
2010 // ----------------------------------------------------------------------------
2012 wxFileHistory::wxFileHistory(size_t maxFiles
, wxWindowID idBase
)
2014 m_fileMaxFiles
= maxFiles
;
2018 void wxFileHistory::AddFileToHistory(const wxString
& file
)
2020 // check if we don't already have this file
2021 const wxFileName
fnNew(file
);
2023 numFiles
= m_fileHistory
.size();
2024 for ( i
= 0; i
< numFiles
; i
++ )
2026 if ( fnNew
== m_fileHistory
[i
] )
2028 // we do have it, move it to the top of the history
2029 RemoveFileFromHistory(i
);
2035 // if we already have a full history, delete the one at the end
2036 if ( numFiles
== m_fileMaxFiles
)
2038 RemoveFileFromHistory(--numFiles
);
2040 else // add a new menu item to all file menus (will be updated below)
2042 for ( wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2044 node
= node
->GetNext() )
2046 wxMenu
* const menu
= (wxMenu
*)node
->GetData();
2048 if ( !numFiles
&& menu
->GetMenuItemCount() )
2049 menu
->AppendSeparator();
2051 // label doesn't matter, it will be set below anyhow, but it can't
2052 // be empty (this is supposed to indicate a stock item)
2053 menu
->Append(m_idBase
+ numFiles
, " ");
2058 // insert the new file in the beginning of the file history
2059 m_fileHistory
.insert(m_fileHistory
.begin(), file
);
2062 // update the labels in all menus
2063 for ( i
= 0; i
< numFiles
; i
++ )
2065 // if in same directory just show the filename; otherwise the full path
2066 const wxFileName
fnOld(m_fileHistory
[i
]);
2068 wxString pathInMenu
;
2069 if ( fnOld
.GetPath() == fnNew
.GetPath() )
2071 pathInMenu
= fnOld
.GetFullName();
2073 else // file in different directory
2075 // absolute path; could also set relative path
2076 pathInMenu
= m_fileHistory
[i
];
2079 for ( wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2081 node
= node
->GetNext() )
2083 wxMenu
* const menu
= (wxMenu
*)node
->GetData();
2085 menu
->SetLabel(m_idBase
+ i
, GetMRUEntryLabel(i
, pathInMenu
));
2090 void wxFileHistory::RemoveFileFromHistory(size_t i
)
2092 size_t numFiles
= m_fileHistory
.size();
2093 wxCHECK_RET( i
< numFiles
,
2094 wxT("invalid index in wxFileHistory::RemoveFileFromHistory") );
2096 // delete the element from the array
2097 m_fileHistory
.RemoveAt(i
);
2100 for ( wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2102 node
= node
->GetNext() )
2104 wxMenu
* const menu
= (wxMenu
*) node
->GetData();
2106 // shift filenames up
2107 for ( size_t j
= i
; j
< numFiles
; j
++ )
2109 menu
->SetLabel(m_idBase
+ j
, GetMRUEntryLabel(j
, m_fileHistory
[j
]));
2112 // delete the last menu item which is unused now
2113 const wxWindowID lastItemId
= m_idBase
+ numFiles
;
2114 if ( menu
->FindItem(lastItemId
) )
2115 menu
->Delete(lastItemId
);
2117 // delete the last separator too if no more files are left
2118 if ( m_fileHistory
.empty() )
2120 const wxMenuItemList::compatibility_iterator
2121 nodeLast
= menu
->GetMenuItems().GetLast();
2124 wxMenuItem
* const lastMenuItem
= nodeLast
->GetData();
2125 if ( lastMenuItem
->IsSeparator() )
2126 menu
->Delete(lastMenuItem
);
2128 //else: menu is empty somehow
2133 void wxFileHistory::UseMenu(wxMenu
*menu
)
2135 if ( !m_fileMenus
.Member(menu
) )
2136 m_fileMenus
.Append(menu
);
2139 void wxFileHistory::RemoveMenu(wxMenu
*menu
)
2141 m_fileMenus
.DeleteObject(menu
);
2145 void wxFileHistory::Load(const wxConfigBase
& config
)
2147 m_fileHistory
.Clear();
2150 buf
.Printf(wxT("file%d"), 1);
2152 wxString historyFile
;
2153 while ((m_fileHistory
.GetCount() < m_fileMaxFiles
) &&
2154 config
.Read(buf
, &historyFile
) && !historyFile
.empty())
2156 m_fileHistory
.Add(historyFile
);
2158 buf
.Printf(wxT("file%d"), (int)m_fileHistory
.GetCount()+1);
2159 historyFile
= wxEmptyString
;
2165 void wxFileHistory::Save(wxConfigBase
& config
)
2168 for (i
= 0; i
< m_fileMaxFiles
; i
++)
2171 buf
.Printf(wxT("file%d"), (int)i
+1);
2172 if (i
< m_fileHistory
.GetCount())
2173 config
.Write(buf
, wxString(m_fileHistory
[i
]));
2175 config
.Write(buf
, wxEmptyString
);
2178 #endif // wxUSE_CONFIG
2180 void wxFileHistory::AddFilesToMenu()
2182 if ( m_fileHistory
.empty() )
2185 for ( wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2187 node
= node
->GetNext() )
2189 AddFilesToMenu((wxMenu
*) node
->GetData());
2193 void wxFileHistory::AddFilesToMenu(wxMenu
* menu
)
2195 if ( m_fileHistory
.empty() )
2198 if ( menu
->GetMenuItemCount() )
2199 menu
->AppendSeparator();
2201 for ( size_t i
= 0; i
< m_fileHistory
.GetCount(); i
++ )
2203 menu
->Append(m_idBase
+ i
, GetMRUEntryLabel(i
, m_fileHistory
[i
]));
2207 // ----------------------------------------------------------------------------
2208 // Permits compatibility with existing file formats and functions that
2209 // manipulate files directly
2210 // ----------------------------------------------------------------------------
2212 #if wxUSE_STD_IOSTREAM
2214 bool wxTransferFileToStream(const wxString
& filename
, wxSTD ostream
& stream
)
2216 wxFFile
file(filename
, _T("rb"));
2217 if ( !file
.IsOpened() )
2225 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2229 stream
.write(buf
, nRead
);
2233 while ( !file
.Eof() );
2238 bool wxTransferStreamToFile(wxSTD istream
& stream
, const wxString
& filename
)
2240 wxFFile
file(filename
, _T("wb"));
2241 if ( !file
.IsOpened() )
2247 stream
.read(buf
, WXSIZEOF(buf
));
2248 if ( !stream
.bad() ) // fail may be set on EOF, don't use operator!()
2250 if ( !file
.Write(buf
, stream
.gcount()) )
2254 while ( !stream
.eof() );
2259 #else // !wxUSE_STD_IOSTREAM
2261 bool wxTransferFileToStream(const wxString
& filename
, wxOutputStream
& stream
)
2263 wxFFile
file(filename
, _T("rb"));
2264 if ( !file
.IsOpened() )
2272 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2276 stream
.Write(buf
, nRead
);
2280 while ( !file
.Eof() );
2285 bool wxTransferStreamToFile(wxInputStream
& stream
, const wxString
& filename
)
2287 wxFFile
file(filename
, _T("wb"));
2288 if ( !file
.IsOpened() )
2294 stream
.Read(buf
, WXSIZEOF(buf
));
2296 const size_t nRead
= stream
.LastRead();
2305 if ( !file
.Write(buf
, nRead
) )
2312 #endif // wxUSE_STD_IOSTREAM/!wxUSE_STD_IOSTREAM
2314 #endif // wxUSE_DOC_VIEW_ARCHITECTURE