1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/docview.cpp
3 // Purpose: Document/view classes
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "docview.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
31 #if wxUSE_DOC_VIEW_ARCHITECTURE
34 #include "wx/string.h"
38 #include "wx/dialog.h"
41 #include "wx/filedlg.h"
49 #include "wx/filename.h"
56 #if wxUSE_PRINTING_ARCHITECTURE
57 #include "wx/prntbase.h"
58 #include "wx/printdlg.h"
61 #include "wx/msgdlg.h"
62 #include "wx/choicdlg.h"
63 #include "wx/docview.h"
64 #include "wx/confbase.h"
66 #include "wx/cmdproc.h"
71 #if wxUSE_STD_IOSTREAM
72 #include "wx/ioswrap.h"
79 #include "wx/wfstream.h"
82 // ----------------------------------------------------------------------------
84 // ----------------------------------------------------------------------------
86 IMPLEMENT_ABSTRACT_CLASS(wxDocument
, wxEvtHandler
)
87 IMPLEMENT_ABSTRACT_CLASS(wxView
, wxEvtHandler
)
88 IMPLEMENT_ABSTRACT_CLASS(wxDocTemplate
, wxObject
)
89 IMPLEMENT_DYNAMIC_CLASS(wxDocManager
, wxEvtHandler
)
90 IMPLEMENT_CLASS(wxDocChildFrame
, wxFrame
)
91 IMPLEMENT_CLASS(wxDocParentFrame
, wxFrame
)
93 #if wxUSE_PRINTING_ARCHITECTURE
94 IMPLEMENT_DYNAMIC_CLASS(wxDocPrintout
, wxPrintout
)
97 IMPLEMENT_DYNAMIC_CLASS(wxFileHistory
, wxObject
)
99 // ----------------------------------------------------------------------------
100 // function prototypes
101 // ----------------------------------------------------------------------------
103 static inline wxString
FindExtension(const wxChar
*path
);
104 static wxWindow
* wxFindSuitableParent(void);
106 // ----------------------------------------------------------------------------
108 // ----------------------------------------------------------------------------
110 static const wxChar
*s_MRUEntryFormat
= wxT("&%d %s");
112 // ============================================================================
114 // ============================================================================
116 // ----------------------------------------------------------------------------
118 // ----------------------------------------------------------------------------
120 static wxString
FindExtension(const wxChar
*path
)
123 wxSplitPath(path
, NULL
, NULL
, &ext
);
125 // VZ: extensions are considered not case sensitive - is this really a good
127 return ext
.MakeLower();
130 // ----------------------------------------------------------------------------
131 // Definition of wxDocument
132 // ----------------------------------------------------------------------------
134 wxDocument::wxDocument(wxDocument
*parent
)
136 m_documentModified
= false;
137 m_documentParent
= parent
;
138 m_documentTemplate
= (wxDocTemplate
*) NULL
;
139 m_commandProcessor
= (wxCommandProcessor
*) NULL
;
143 bool wxDocument::DeleteContents()
148 wxDocument::~wxDocument()
152 if (m_commandProcessor
)
153 delete m_commandProcessor
;
155 if (GetDocumentManager())
156 GetDocumentManager()->RemoveDocument(this);
158 // Not safe to do here, since it'll invoke virtual view functions
159 // expecting to see valid derived objects: and by the time we get here,
160 // we've called destructors higher up.
164 bool wxDocument::Close()
166 if (OnSaveModified())
167 return OnCloseDocument();
172 bool wxDocument::OnCloseDocument()
174 // Tell all views that we're about to close
181 // Note that this implicitly deletes the document when the last view is
183 bool wxDocument::DeleteAllViews()
185 wxDocManager
* manager
= GetDocumentManager();
186 wxList::iterator it
, en
;
188 for ( it
= m_documentViews
.begin(), en
= m_documentViews
.end();
192 wxView
*view
= (wxView
*)*it
;
196 delete view
; // Deletes node implicitly
199 // If we haven't yet deleted the document (for example
200 // if there were no views) then delete it.
201 if (manager
&& manager
->GetDocuments().Member(this))
207 wxView
*wxDocument::GetFirstView() const
209 if (m_documentViews
.GetCount() == 0)
210 return (wxView
*) NULL
;
211 return (wxView
*)m_documentViews
.GetFirst()->GetData();
214 wxDocManager
*wxDocument::GetDocumentManager() const
216 return (m_documentTemplate
? m_documentTemplate
->GetDocumentManager() : (wxDocManager
*) NULL
);
219 bool wxDocument::OnNewDocument()
221 if (!OnSaveModified())
224 if (OnCloseDocument()==false) return false;
227 SetDocumentSaved(false);
230 GetDocumentManager()->MakeDefaultName(name
);
232 SetFilename(name
, true);
237 bool wxDocument::Save()
239 if (!IsModified() && m_savedYet
)
242 if ( m_documentFile
.empty() || !m_savedYet
)
245 return OnSaveDocument(m_documentFile
);
248 bool wxDocument::SaveAs()
250 wxDocTemplate
*docTemplate
= GetDocumentTemplate();
254 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
255 wxString filter
= docTemplate
->GetDescription() + wxT(" (") + docTemplate
->GetFileFilter() + wxT(")|") + docTemplate
->GetFileFilter();
257 // Now see if there are some other template with identical view and document
258 // classes, whose filters may also be used.
260 if (docTemplate
->GetViewClassInfo() && docTemplate
->GetDocClassInfo())
262 wxList::compatibility_iterator node
= wxDocManager::GetDocumentManager()->GetTemplates().GetFirst();
265 wxDocTemplate
*t
= (wxDocTemplate
*) node
->GetData();
267 if (t
->IsVisible() && t
!= docTemplate
&&
268 t
->GetViewClassInfo() == docTemplate
->GetViewClassInfo() &&
269 t
->GetDocClassInfo() == docTemplate
->GetDocClassInfo())
271 // add a '|' to separate this filter from the previous one
272 if ( !filter
.empty() )
275 filter
<< t
->GetDescription() << wxT(" (") << t
->GetFileFilter() << wxT(") |")
276 << t
->GetFileFilter();
279 node
= node
->GetNext();
283 wxString filter
= docTemplate
->GetFileFilter() ;
285 wxString tmp
= wxFileSelector(_("Save as"),
286 docTemplate
->GetDirectory(),
287 wxFileNameFromPath(GetFilename()),
288 docTemplate
->GetDefaultExtension(),
290 wxSAVE
| wxOVERWRITE_PROMPT
,
291 GetDocumentWindow());
296 wxString
fileName(tmp
);
297 wxString path
, name
, ext
;
298 wxSplitPath(fileName
, & path
, & name
, & ext
);
302 fileName
+= wxT(".");
303 fileName
+= docTemplate
->GetDefaultExtension();
306 SetFilename(fileName
);
307 SetTitle(wxFileNameFromPath(fileName
));
309 // Notify the views that the filename has changed
310 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
313 wxView
*view
= (wxView
*)node
->GetData();
314 view
->OnChangeFilename();
315 node
= node
->GetNext();
318 // Files that were not saved correctly are not added to the FileHistory.
319 if (!OnSaveDocument(m_documentFile
))
322 // A file that doesn't use the default extension of its document template cannot be opened
323 // via the FileHistory, so we do not add it.
324 if (docTemplate
->FileMatchesTemplate(fileName
))
326 GetDocumentManager()->AddFileToHistory(fileName
);
330 // The user will probably not be able to open the file again, so
331 // we could warn about the wrong file-extension here.
336 bool wxDocument::OnSaveDocument(const wxString
& file
)
341 if ( !DoSaveDocument(file
) )
346 SetDocumentSaved(true);
348 wxFileName
fn(file
) ;
349 fn
.MacSetDefaultTypeAndCreator() ;
354 bool wxDocument::OnOpenDocument(const wxString
& file
)
356 if (!OnSaveModified())
359 if ( !DoOpenDocument(file
) )
362 SetFilename(file
, true);
371 #if wxUSE_STD_IOSTREAM
372 wxSTD istream
& wxDocument::LoadObject(wxSTD istream
& stream
)
374 wxInputStream
& wxDocument::LoadObject(wxInputStream
& stream
)
380 #if wxUSE_STD_IOSTREAM
381 wxSTD ostream
& wxDocument::SaveObject(wxSTD ostream
& stream
)
383 wxOutputStream
& wxDocument::SaveObject(wxOutputStream
& stream
)
389 bool wxDocument::Revert()
395 // Get title, or filename if no title, else unnamed
396 bool wxDocument::GetPrintableName(wxString
& buf
) const
398 if (!m_documentTitle
.empty())
400 buf
= m_documentTitle
;
403 else if (!m_documentFile
.empty())
405 buf
= wxFileNameFromPath(m_documentFile
);
415 wxWindow
*wxDocument::GetDocumentWindow() const
417 wxView
*view
= GetFirstView();
419 return view
->GetFrame();
421 return wxTheApp
->GetTopWindow();
424 wxCommandProcessor
*wxDocument::OnCreateCommandProcessor()
426 return new wxCommandProcessor
;
429 // true if safe to close
430 bool wxDocument::OnSaveModified()
435 GetPrintableName(title
);
438 if (!wxTheApp
->GetAppName().empty())
439 msgTitle
= wxTheApp
->GetAppName();
441 msgTitle
= wxString(_("Warning"));
444 prompt
.Printf(_("Do you want to save changes to document %s?"),
445 (const wxChar
*)title
);
446 int res
= wxMessageBox(prompt
, msgTitle
,
447 wxYES_NO
|wxCANCEL
|wxICON_QUESTION
,
448 GetDocumentWindow());
454 else if (res
== wxYES
)
456 else if (res
== wxCANCEL
)
462 bool wxDocument::Draw(wxDC
& WXUNUSED(context
))
467 bool wxDocument::AddView(wxView
*view
)
469 if (!m_documentViews
.Member(view
))
471 m_documentViews
.Append(view
);
477 bool wxDocument::RemoveView(wxView
*view
)
479 (void)m_documentViews
.DeleteObject(view
);
484 bool wxDocument::OnCreate(const wxString
& WXUNUSED(path
), long flags
)
486 if (GetDocumentTemplate()->CreateView(this, flags
))
492 // Called after a view is added or removed.
493 // The default implementation deletes the document if
494 // there are no more views.
495 void wxDocument::OnChangedViewList()
497 if (m_documentViews
.GetCount() == 0)
499 if (OnSaveModified())
506 void wxDocument::UpdateAllViews(wxView
*sender
, wxObject
*hint
)
508 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
511 wxView
*view
= (wxView
*)node
->GetData();
513 view
->OnUpdate(sender
, hint
);
514 node
= node
->GetNext();
518 void wxDocument::NotifyClosing()
520 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
523 wxView
*view
= (wxView
*)node
->GetData();
524 view
->OnClosingDocument();
525 node
= node
->GetNext();
529 void wxDocument::SetFilename(const wxString
& filename
, bool notifyViews
)
531 m_documentFile
= filename
;
534 // Notify the views that the filename has changed
535 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
538 wxView
*view
= (wxView
*)node
->GetData();
539 view
->OnChangeFilename();
540 node
= node
->GetNext();
545 bool wxDocument::DoSaveDocument(const wxString
& file
)
548 if (!wxTheApp
->GetAppName().empty())
549 msgTitle
= wxTheApp
->GetAppName();
551 msgTitle
= wxString(_("File error"));
553 #if wxUSE_STD_IOSTREAM
554 wxSTD ofstream
store(file
.mb_str());
555 if (store
.fail() || store
.bad())
557 wxFileOutputStream
store(file
);
558 if (store
.GetLastError() != wxSTREAM_NO_ERROR
)
561 (void)wxMessageBox(_("Sorry, could not open this file for saving."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
562 GetDocumentWindow());
566 if (!SaveObject(store
))
568 (void)wxMessageBox(_("Sorry, could not save this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
569 GetDocumentWindow());
577 bool wxDocument::DoOpenDocument(const wxString
& file
)
580 if (!wxTheApp
->GetAppName().empty())
581 msgTitle
= wxTheApp
->GetAppName();
583 msgTitle
= wxString(_("File error"));
585 #if wxUSE_STD_IOSTREAM
586 wxSTD ifstream
store(file
.mb_str());
587 if (store
.fail() || store
.bad())
589 wxFileInputStream
store(file
);
590 if (store
.GetLastError() != wxSTREAM_NO_ERROR
)
593 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
|wxICON_EXCLAMATION
,
594 GetDocumentWindow());
597 #if wxUSE_STD_IOSTREAM
599 if ( !store
&& !store
.eof() )
601 int res
= LoadObject(store
).GetLastError();
602 if ((res
!= wxSTREAM_NO_ERROR
) &&
603 (res
!= wxSTREAM_EOF
))
606 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
|wxICON_EXCLAMATION
,
607 GetDocumentWindow());
615 // ----------------------------------------------------------------------------
617 // ----------------------------------------------------------------------------
621 m_viewDocument
= (wxDocument
*) NULL
;
623 m_viewFrame
= (wxFrame
*) NULL
;
628 GetDocumentManager()->ActivateView(this, false);
629 m_viewDocument
->RemoveView(this);
632 // Extend event processing to search the document's event table
633 bool wxView::ProcessEvent(wxEvent
& event
)
635 if ( !GetDocument() || !GetDocument()->ProcessEvent(event
) )
636 return wxEvtHandler::ProcessEvent(event
);
641 void wxView::OnActivateView(bool WXUNUSED(activate
), wxView
*WXUNUSED(activeView
), wxView
*WXUNUSED(deactiveView
))
645 void wxView::OnPrint(wxDC
*dc
, wxObject
*WXUNUSED(info
))
650 void wxView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
))
654 void wxView::OnChangeFilename()
656 if (GetFrame() && GetDocument())
660 GetDocument()->GetPrintableName(title
);
662 GetFrame()->SetTitle(title
);
666 void wxView::SetDocument(wxDocument
*doc
)
668 m_viewDocument
= doc
;
673 bool wxView::Close(bool deleteWindow
)
675 if (OnClose(deleteWindow
))
681 void wxView::Activate(bool activate
)
683 if (GetDocument() && GetDocumentManager())
685 OnActivateView(activate
, this, GetDocumentManager()->GetCurrentView());
686 GetDocumentManager()->ActivateView(this, activate
);
690 bool wxView::OnClose(bool WXUNUSED(deleteWindow
))
692 return GetDocument() ? GetDocument()->Close() : true;
695 #if wxUSE_PRINTING_ARCHITECTURE
696 wxPrintout
*wxView::OnCreatePrintout()
698 return new wxDocPrintout(this);
700 #endif // wxUSE_PRINTING_ARCHITECTURE
702 // ----------------------------------------------------------------------------
704 // ----------------------------------------------------------------------------
706 wxDocTemplate::wxDocTemplate(wxDocManager
*manager
,
707 const wxString
& descr
,
708 const wxString
& filter
,
711 const wxString
& docTypeName
,
712 const wxString
& viewTypeName
,
713 wxClassInfo
*docClassInfo
,
714 wxClassInfo
*viewClassInfo
,
717 m_documentManager
= manager
;
718 m_description
= descr
;
721 m_fileFilter
= filter
;
723 m_docTypeName
= docTypeName
;
724 m_viewTypeName
= viewTypeName
;
725 m_documentManager
->AssociateTemplate(this);
727 m_docClassInfo
= docClassInfo
;
728 m_viewClassInfo
= viewClassInfo
;
731 wxDocTemplate::~wxDocTemplate()
733 m_documentManager
->DisassociateTemplate(this);
736 // Tries to dynamically construct an object of the right class.
737 wxDocument
*wxDocTemplate::CreateDocument(const wxString
& path
, long flags
)
739 wxDocument
*doc
= DoCreateDocument();
741 return (wxDocument
*) NULL
;
743 if (InitDocument(doc
, path
, flags
))
749 return (wxDocument
*) NULL
;
753 bool wxDocTemplate::InitDocument(wxDocument
* doc
, const wxString
& path
, long flags
)
755 doc
->SetFilename(path
);
756 doc
->SetDocumentTemplate(this);
757 GetDocumentManager()->AddDocument(doc
);
758 doc
->SetCommandProcessor(doc
->OnCreateCommandProcessor());
760 if (doc
->OnCreate(path
, flags
))
764 if (GetDocumentManager()->GetDocuments().Member(doc
))
765 doc
->DeleteAllViews();
770 wxView
*wxDocTemplate::CreateView(wxDocument
*doc
, long flags
)
772 wxView
*view
= DoCreateView();
774 return (wxView
*) NULL
;
776 view
->SetDocument(doc
);
777 if (view
->OnCreate(doc
, flags
))
784 return (wxView
*) NULL
;
788 // The default (very primitive) format detection: check is the extension is
789 // that of the template
790 bool wxDocTemplate::FileMatchesTemplate(const wxString
& path
)
792 return GetDefaultExtension().IsSameAs(FindExtension(path
));
795 wxDocument
*wxDocTemplate::DoCreateDocument()
798 return (wxDocument
*) NULL
;
800 return (wxDocument
*)m_docClassInfo
->CreateObject();
803 wxView
*wxDocTemplate::DoCreateView()
805 if (!m_viewClassInfo
)
806 return (wxView
*) NULL
;
808 return (wxView
*)m_viewClassInfo
->CreateObject();
811 // ----------------------------------------------------------------------------
813 // ----------------------------------------------------------------------------
815 BEGIN_EVENT_TABLE(wxDocManager
, wxEvtHandler
)
816 EVT_MENU(wxID_OPEN
, wxDocManager::OnFileOpen
)
817 EVT_MENU(wxID_CLOSE
, wxDocManager::OnFileClose
)
818 EVT_MENU(wxID_CLOSE_ALL
, wxDocManager::OnFileCloseAll
)
819 EVT_MENU(wxID_REVERT
, wxDocManager::OnFileRevert
)
820 EVT_MENU(wxID_NEW
, wxDocManager::OnFileNew
)
821 EVT_MENU(wxID_SAVE
, wxDocManager::OnFileSave
)
822 EVT_MENU(wxID_SAVEAS
, wxDocManager::OnFileSaveAs
)
823 EVT_MENU(wxID_UNDO
, wxDocManager::OnUndo
)
824 EVT_MENU(wxID_REDO
, wxDocManager::OnRedo
)
826 EVT_UPDATE_UI(wxID_OPEN
, wxDocManager::OnUpdateFileOpen
)
827 EVT_UPDATE_UI(wxID_CLOSE
, wxDocManager::OnUpdateFileClose
)
828 EVT_UPDATE_UI(wxID_CLOSE_ALL
, wxDocManager::OnUpdateFileClose
)
829 EVT_UPDATE_UI(wxID_REVERT
, wxDocManager::OnUpdateFileRevert
)
830 EVT_UPDATE_UI(wxID_NEW
, wxDocManager::OnUpdateFileNew
)
831 EVT_UPDATE_UI(wxID_SAVE
, wxDocManager::OnUpdateFileSave
)
832 EVT_UPDATE_UI(wxID_SAVEAS
, wxDocManager::OnUpdateFileSaveAs
)
833 EVT_UPDATE_UI(wxID_UNDO
, wxDocManager::OnUpdateUndo
)
834 EVT_UPDATE_UI(wxID_REDO
, wxDocManager::OnUpdateRedo
)
836 #if wxUSE_PRINTING_ARCHITECTURE
837 EVT_MENU(wxID_PRINT
, wxDocManager::OnPrint
)
838 EVT_MENU(wxID_PREVIEW
, wxDocManager::OnPreview
)
840 EVT_UPDATE_UI(wxID_PRINT
, wxDocManager::OnUpdatePrint
)
841 EVT_UPDATE_UI(wxID_PREVIEW
, wxDocManager::OnUpdatePreview
)
845 wxDocManager
* wxDocManager::sm_docManager
= (wxDocManager
*) NULL
;
847 wxDocManager::wxDocManager(long flags
, bool initialize
)
849 m_defaultDocumentNameCounter
= 1;
851 m_currentView
= (wxView
*) NULL
;
852 m_maxDocsOpen
= 10000;
853 m_fileHistory
= (wxFileHistory
*) NULL
;
856 sm_docManager
= this;
859 wxDocManager::~wxDocManager()
863 delete m_fileHistory
;
864 sm_docManager
= (wxDocManager
*) NULL
;
867 // closes the specified document
868 bool wxDocManager::CloseDocument(wxDocument
* doc
, bool force
)
870 if (doc
->Close() || force
)
872 // Implicitly deletes the document when
873 // the last view is deleted
874 doc
->DeleteAllViews();
876 // Check we're really deleted
877 if (m_docs
.Member(doc
))
885 bool wxDocManager::CloseDocuments(bool force
)
887 wxList::compatibility_iterator node
= m_docs
.GetFirst();
890 wxDocument
*doc
= (wxDocument
*)node
->GetData();
891 wxList::compatibility_iterator next
= node
->GetNext();
893 if (!CloseDocument(doc
, force
))
896 // This assumes that documents are not connected in
897 // any way, i.e. deleting one document does NOT
904 bool wxDocManager::Clear(bool force
)
906 if (!CloseDocuments(force
))
909 m_currentView
= NULL
;
911 wxList::compatibility_iterator node
= m_templates
.GetFirst();
914 wxDocTemplate
*templ
= (wxDocTemplate
*) node
->GetData();
915 wxList::compatibility_iterator next
= node
->GetNext();
922 bool wxDocManager::Initialize()
924 m_fileHistory
= OnCreateFileHistory();
928 wxFileHistory
*wxDocManager::OnCreateFileHistory()
930 return new wxFileHistory
;
933 void wxDocManager::OnFileClose(wxCommandEvent
& WXUNUSED(event
))
935 wxDocument
*doc
= GetCurrentDocument();
940 doc
->DeleteAllViews();
941 if (m_docs
.Member(doc
))
946 void wxDocManager::OnFileCloseAll(wxCommandEvent
& WXUNUSED(event
))
948 CloseDocuments(false);
951 void wxDocManager::OnFileNew(wxCommandEvent
& WXUNUSED(event
))
953 CreateDocument( wxEmptyString
, wxDOC_NEW
);
956 void wxDocManager::OnFileOpen(wxCommandEvent
& WXUNUSED(event
))
958 if ( !CreateDocument( wxEmptyString
, 0) )
964 void wxDocManager::OnFileRevert(wxCommandEvent
& WXUNUSED(event
))
966 wxDocument
*doc
= GetCurrentDocument();
972 void wxDocManager::OnFileSave(wxCommandEvent
& WXUNUSED(event
))
974 wxDocument
*doc
= GetCurrentDocument();
980 void wxDocManager::OnFileSaveAs(wxCommandEvent
& WXUNUSED(event
))
982 wxDocument
*doc
= GetCurrentDocument();
988 void wxDocManager::OnPrint(wxCommandEvent
& WXUNUSED(event
))
990 #if wxUSE_PRINTING_ARCHITECTURE
991 wxView
*view
= GetCurrentView();
995 wxPrintout
*printout
= view
->OnCreatePrintout();
999 printer
.Print(view
->GetFrame(), printout
, true);
1003 #endif // wxUSE_PRINTING_ARCHITECTURE
1006 void wxDocManager::OnPreview(wxCommandEvent
& WXUNUSED(event
))
1008 #if wxUSE_PRINTING_ARCHITECTURE
1009 wxView
*view
= GetCurrentView();
1013 wxPrintout
*printout
= view
->OnCreatePrintout();
1016 // Pass two printout objects: for preview, and possible printing.
1017 wxPrintPreviewBase
*preview
= new wxPrintPreview(printout
, view
->OnCreatePrintout());
1018 if ( !preview
->Ok() )
1021 wxMessageBox( _("Sorry, print preview needs a printer to be installed.") );
1025 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, (wxFrame
*)wxTheApp
->GetTopWindow(), _("Print Preview"),
1026 wxPoint(100, 100), wxSize(600, 650));
1027 frame
->Centre(wxBOTH
);
1028 frame
->Initialize();
1031 #endif // wxUSE_PRINTING_ARCHITECTURE
1034 void wxDocManager::OnUndo(wxCommandEvent
& event
)
1036 wxDocument
*doc
= GetCurrentDocument();
1039 if (doc
->GetCommandProcessor())
1040 doc
->GetCommandProcessor()->Undo();
1045 void wxDocManager::OnRedo(wxCommandEvent
& event
)
1047 wxDocument
*doc
= GetCurrentDocument();
1050 if (doc
->GetCommandProcessor())
1051 doc
->GetCommandProcessor()->Redo();
1056 // Handlers for UI update commands
1058 void wxDocManager::OnUpdateFileOpen(wxUpdateUIEvent
& event
)
1060 event
.Enable( true );
1063 void wxDocManager::OnUpdateFileClose(wxUpdateUIEvent
& event
)
1065 wxDocument
*doc
= GetCurrentDocument();
1066 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1069 void wxDocManager::OnUpdateFileRevert(wxUpdateUIEvent
& event
)
1071 wxDocument
*doc
= GetCurrentDocument();
1072 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1075 void wxDocManager::OnUpdateFileNew(wxUpdateUIEvent
& event
)
1077 event
.Enable( true );
1080 void wxDocManager::OnUpdateFileSave(wxUpdateUIEvent
& event
)
1082 wxDocument
*doc
= GetCurrentDocument();
1083 event
.Enable( doc
&& doc
->IsModified() );
1086 void wxDocManager::OnUpdateFileSaveAs(wxUpdateUIEvent
& event
)
1088 wxDocument
*doc
= GetCurrentDocument();
1089 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1092 void wxDocManager::OnUpdateUndo(wxUpdateUIEvent
& event
)
1094 wxDocument
*doc
= GetCurrentDocument();
1096 event
.Enable(false);
1097 else if (!doc
->GetCommandProcessor())
1101 event
.Enable( doc
->GetCommandProcessor()->CanUndo() );
1102 doc
->GetCommandProcessor()->SetMenuStrings();
1106 void wxDocManager::OnUpdateRedo(wxUpdateUIEvent
& event
)
1108 wxDocument
*doc
= GetCurrentDocument();
1110 event
.Enable(false);
1111 else if (!doc
->GetCommandProcessor())
1115 event
.Enable( doc
->GetCommandProcessor()->CanRedo() );
1116 doc
->GetCommandProcessor()->SetMenuStrings();
1120 void wxDocManager::OnUpdatePrint(wxUpdateUIEvent
& event
)
1122 wxDocument
*doc
= GetCurrentDocument();
1123 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1126 void wxDocManager::OnUpdatePreview(wxUpdateUIEvent
& event
)
1128 wxDocument
*doc
= GetCurrentDocument();
1129 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1132 wxView
*wxDocManager::GetCurrentView() const
1135 return m_currentView
;
1136 if (m_docs
.GetCount() == 1)
1138 wxDocument
* doc
= (wxDocument
*) m_docs
.GetFirst()->GetData();
1139 return doc
->GetFirstView();
1141 return (wxView
*) NULL
;
1144 // Extend event processing to search the view's event table
1145 bool wxDocManager::ProcessEvent(wxEvent
& event
)
1147 wxView
* view
= GetCurrentView();
1150 if (view
->ProcessEvent(event
))
1153 return wxEvtHandler::ProcessEvent(event
);
1156 wxDocument
*wxDocManager::CreateDocument(const wxString
& path
, long flags
)
1158 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.GetCount()];
1161 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1163 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Item(i
)->GetData());
1164 if (temp
->IsVisible())
1166 templates
[n
] = temp
;
1173 return (wxDocument
*) NULL
;
1176 wxDocument
* docToClose
= NULL
;
1178 // If we've reached the max number of docs, close the
1180 if ( (int)GetDocuments().GetCount() >= m_maxDocsOpen
)
1182 wxDocument
*doc
= (wxDocument
*)GetDocuments().GetFirst()->GetData();
1186 // New document: user chooses a template, unless there's only one.
1187 if (flags
& wxDOC_NEW
)
1193 if (!CloseDocument(docToClose
, false))
1200 wxDocTemplate
*temp
= templates
[0];
1202 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
1206 newDoc
->SetDocumentName(temp
->GetDocumentName());
1207 newDoc
->SetDocumentTemplate(temp
);
1208 newDoc
->OnNewDocument();
1213 wxDocTemplate
*temp
= SelectDocumentType(templates
, n
);
1219 if (!CloseDocument(docToClose
, false))
1225 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
1229 newDoc
->SetDocumentName(temp
->GetDocumentName());
1230 newDoc
->SetDocumentTemplate(temp
);
1231 newDoc
->OnNewDocument();
1236 return (wxDocument
*) NULL
;
1239 // Existing document
1240 wxDocTemplate
*temp
;
1242 wxString path2
= path
;
1244 if (flags
& wxDOC_SILENT
)
1246 temp
= FindTemplateForPath(path2
);
1249 // Since we do not add files with non-default extensions to the FileHistory this
1250 // can only happen if the application changes the allowed templates in runtime.
1251 (void)wxMessageBox(_("Sorry, the format for this file is unknown."),
1253 wxOK
| wxICON_EXCLAMATION
, wxFindSuitableParent());
1257 temp
= SelectDocumentPath(templates
, n
, path2
, flags
);
1265 if (!CloseDocument(docToClose
, false))
1271 wxDocument
*newDoc
= temp
->CreateDocument(path2
, flags
);
1274 newDoc
->SetDocumentName(temp
->GetDocumentName());
1275 newDoc
->SetDocumentTemplate(temp
);
1276 if (!newDoc
->OnOpenDocument(path2
))
1278 newDoc
->DeleteAllViews();
1279 // delete newDoc; // Implicitly deleted by DeleteAllViews
1280 return (wxDocument
*) NULL
;
1282 // A file that doesn't use the default extension of its document
1283 // template cannot be opened via the FileHistory, so we do not
1285 if (temp
->FileMatchesTemplate(path2
))
1286 AddFileToHistory(path2
);
1291 return (wxDocument
*) NULL
;
1294 wxView
*wxDocManager::CreateView(wxDocument
*doc
, long flags
)
1296 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.GetCount()];
1299 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1301 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Item(i
)->GetData());
1302 if (temp
->IsVisible())
1304 if (temp
->GetDocumentName() == doc
->GetDocumentName())
1306 templates
[n
] = temp
;
1314 return (wxView
*) NULL
;
1318 wxDocTemplate
*temp
= templates
[0];
1320 wxView
*view
= temp
->CreateView(doc
, flags
);
1322 view
->SetViewName(temp
->GetViewName());
1326 wxDocTemplate
*temp
= SelectViewType(templates
, n
);
1330 wxView
*view
= temp
->CreateView(doc
, flags
);
1332 view
->SetViewName(temp
->GetViewName());
1336 return (wxView
*) NULL
;
1339 // Not yet implemented
1340 void wxDocManager::DeleteTemplate(wxDocTemplate
*WXUNUSED(temp
), long WXUNUSED(flags
))
1344 // Not yet implemented
1345 bool wxDocManager::FlushDoc(wxDocument
*WXUNUSED(doc
))
1350 wxDocument
*wxDocManager::GetCurrentDocument() const
1352 wxView
*view
= GetCurrentView();
1354 return view
->GetDocument();
1356 return (wxDocument
*) NULL
;
1359 // Make a default document name
1360 bool wxDocManager::MakeDefaultName(wxString
& name
)
1362 name
.Printf(_("unnamed%d"), m_defaultDocumentNameCounter
);
1363 m_defaultDocumentNameCounter
++;
1368 // Make a frame title (override this to do something different)
1369 // If docName is empty, a document is not currently active.
1370 wxString
wxDocManager::MakeFrameTitle(wxDocument
* doc
)
1372 wxString appName
= wxTheApp
->GetAppName();
1379 doc
->GetPrintableName(docName
);
1380 title
= docName
+ wxString(_(" - ")) + appName
;
1386 // Not yet implemented
1387 wxDocTemplate
*wxDocManager::MatchTemplate(const wxString
& WXUNUSED(path
))
1389 return (wxDocTemplate
*) NULL
;
1392 // File history management
1393 void wxDocManager::AddFileToHistory(const wxString
& file
)
1396 m_fileHistory
->AddFileToHistory(file
);
1399 void wxDocManager::RemoveFileFromHistory(size_t i
)
1402 m_fileHistory
->RemoveFileFromHistory(i
);
1405 wxString
wxDocManager::GetHistoryFile(size_t i
) const
1410 histFile
= m_fileHistory
->GetHistoryFile(i
);
1415 void wxDocManager::FileHistoryUseMenu(wxMenu
*menu
)
1418 m_fileHistory
->UseMenu(menu
);
1421 void wxDocManager::FileHistoryRemoveMenu(wxMenu
*menu
)
1424 m_fileHistory
->RemoveMenu(menu
);
1428 void wxDocManager::FileHistoryLoad(wxConfigBase
& config
)
1431 m_fileHistory
->Load(config
);
1434 void wxDocManager::FileHistorySave(wxConfigBase
& config
)
1437 m_fileHistory
->Save(config
);
1441 void wxDocManager::FileHistoryAddFilesToMenu(wxMenu
* menu
)
1444 m_fileHistory
->AddFilesToMenu(menu
);
1447 void wxDocManager::FileHistoryAddFilesToMenu()
1450 m_fileHistory
->AddFilesToMenu();
1453 size_t wxDocManager::GetHistoryFilesCount() const
1455 return m_fileHistory
? m_fileHistory
->GetCount() : 0;
1459 // Find out the document template via matching in the document file format
1460 // against that of the template
1461 wxDocTemplate
*wxDocManager::FindTemplateForPath(const wxString
& path
)
1463 wxDocTemplate
*theTemplate
= (wxDocTemplate
*) NULL
;
1465 // Find the template which this extension corresponds to
1466 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1468 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Item(i
)->GetData();
1469 if ( temp
->FileMatchesTemplate(path
) )
1478 // Try to get a more suitable parent frame than the top window,
1479 // for selection dialogs. Otherwise you may get an unexpected
1480 // window being activated when a dialog is shown.
1481 static wxWindow
* wxFindSuitableParent()
1483 wxWindow
* parent
= wxTheApp
->GetTopWindow();
1485 wxWindow
* focusWindow
= wxWindow::FindFocus();
1488 while (focusWindow
&&
1489 !focusWindow
->IsKindOf(CLASSINFO(wxDialog
)) &&
1490 !focusWindow
->IsKindOf(CLASSINFO(wxFrame
)))
1492 focusWindow
= focusWindow
->GetParent();
1495 parent
= focusWindow
;
1500 // Prompts user to open a file, using file specs in templates.
1501 // Must extend the file selector dialog or implement own; OR
1502 // match the extension to the template extension.
1504 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**templates
,
1505 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
1508 int WXUNUSED(noTemplates
),
1511 long WXUNUSED(flags
),
1512 bool WXUNUSED(save
))
1514 // We can only have multiple filters in Windows and GTK
1515 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
1519 for (i
= 0; i
< noTemplates
; i
++)
1521 if (templates
[i
]->IsVisible())
1523 // add a '|' to separate this filter from the previous one
1524 if ( !descrBuf
.empty() )
1525 descrBuf
<< wxT('|');
1527 descrBuf
<< templates
[i
]->GetDescription()
1528 << wxT(" (") << templates
[i
]->GetFileFilter() << wxT(") |")
1529 << templates
[i
]->GetFileFilter();
1533 wxString descrBuf
= wxT("*.*");
1536 int FilterIndex
= -1;
1538 wxWindow
* parent
= wxFindSuitableParent();
1540 wxString pathTmp
= wxFileSelectorEx(_("Select a file"),
1548 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)NULL
;
1549 if (!pathTmp
.empty())
1551 if (!wxFileExists(pathTmp
))
1554 if (!wxTheApp
->GetAppName().empty())
1555 msgTitle
= wxTheApp
->GetAppName();
1557 msgTitle
= wxString(_("File error"));
1559 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
1562 path
= wxEmptyString
;
1563 return (wxDocTemplate
*) NULL
;
1565 m_lastDirectory
= wxPathOnly(pathTmp
);
1569 // first choose the template using the extension, if this fails (i.e.
1570 // wxFileSelectorEx() didn't fill it), then use the path
1571 if ( FilterIndex
!= -1 )
1572 theTemplate
= templates
[FilterIndex
];
1574 theTemplate
= FindTemplateForPath(path
);
1577 // Since we do not add files with non-default extensions to the FileHistory this
1578 // can only happen if the application changes the allowed templates in runtime.
1579 (void)wxMessageBox(_("Sorry, the format for this file is unknown."),
1581 wxOK
| wxICON_EXCLAMATION
, wxFindSuitableParent());
1586 path
= wxEmptyString
;
1592 wxDocTemplate
*wxDocManager::SelectDocumentType(wxDocTemplate
**templates
,
1593 int noTemplates
, bool sort
)
1595 wxArrayString strings
;
1596 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1600 for (i
= 0; i
< noTemplates
; i
++)
1602 if (templates
[i
]->IsVisible())
1606 for (j
= 0; j
< n
; j
++)
1608 //filter out NOT unique documents + view combinations
1609 if ( templates
[i
]->m_docTypeName
== data
[j
]->m_docTypeName
&&
1610 templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
1617 strings
.Add(templates
[i
]->m_description
);
1619 data
[n
] = templates
[i
];
1627 strings
.Sort(); // ascending sort
1628 // Yes, this will be slow, but template lists
1629 // are typically short.
1631 n
= strings
.Count();
1632 for (i
= 0; i
< n
; i
++)
1634 for (j
= 0; j
< noTemplates
; j
++)
1636 if (strings
[i
] == templates
[j
]->m_description
)
1637 data
[i
] = templates
[j
];
1642 wxDocTemplate
*theTemplate
;
1647 // no visible templates, hence nothing to choose from
1652 // don't propose the user to choose if he heas no choice
1653 theTemplate
= data
[0];
1657 // propose the user to choose one of several
1658 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1660 _("Select a document template"),
1664 wxFindSuitableParent()
1673 wxDocTemplate
*wxDocManager::SelectViewType(wxDocTemplate
**templates
,
1674 int noTemplates
, bool sort
)
1676 wxArrayString strings
;
1677 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1681 for (i
= 0; i
< noTemplates
; i
++)
1683 wxDocTemplate
*templ
= templates
[i
];
1684 if ( templ
->IsVisible() && !templ
->GetViewName().empty() )
1688 for (j
= 0; j
< n
; j
++)
1690 //filter out NOT unique views
1691 if ( templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
)
1697 strings
.Add(templ
->m_viewTypeName
);
1706 strings
.Sort(); // ascending sort
1707 // Yes, this will be slow, but template lists
1708 // are typically short.
1710 n
= strings
.Count();
1711 for (i
= 0; i
< n
; i
++)
1713 for (j
= 0; j
< noTemplates
; j
++)
1715 if (strings
[i
] == templates
[j
]->m_viewTypeName
)
1716 data
[i
] = templates
[j
];
1721 wxDocTemplate
*theTemplate
;
1723 // the same logic as above
1727 theTemplate
= (wxDocTemplate
*)NULL
;
1731 theTemplate
= data
[0];
1735 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1737 _("Select a document view"),
1741 wxFindSuitableParent()
1750 void wxDocManager::AssociateTemplate(wxDocTemplate
*temp
)
1752 if (!m_templates
.Member(temp
))
1753 m_templates
.Append(temp
);
1756 void wxDocManager::DisassociateTemplate(wxDocTemplate
*temp
)
1758 m_templates
.DeleteObject(temp
);
1761 // Add and remove a document from the manager's list
1762 void wxDocManager::AddDocument(wxDocument
*doc
)
1764 if (!m_docs
.Member(doc
))
1768 void wxDocManager::RemoveDocument(wxDocument
*doc
)
1770 m_docs
.DeleteObject(doc
);
1773 // Views or windows should inform the document manager
1774 // when a view is going in or out of focus
1775 void wxDocManager::ActivateView(wxView
*view
, bool activate
)
1779 m_currentView
= view
;
1783 if ( m_currentView
== view
)
1785 // don't keep stale pointer
1786 m_currentView
= (wxView
*) NULL
;
1791 // ----------------------------------------------------------------------------
1792 // Default document child frame
1793 // ----------------------------------------------------------------------------
1795 BEGIN_EVENT_TABLE(wxDocChildFrame
, wxFrame
)
1796 EVT_ACTIVATE(wxDocChildFrame::OnActivate
)
1797 EVT_CLOSE(wxDocChildFrame::OnCloseWindow
)
1800 wxDocChildFrame::wxDocChildFrame(wxDocument
*doc
,
1804 const wxString
& title
,
1808 const wxString
& name
)
1809 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1811 m_childDocument
= doc
;
1814 view
->SetFrame(this);
1817 // Extend event processing to search the view's event table
1818 bool wxDocChildFrame::ProcessEvent(wxEvent
& event
)
1821 m_childView
->Activate(true);
1823 if ( !m_childView
|| ! m_childView
->ProcessEvent(event
) )
1825 // Only hand up to the parent if it's a menu command
1826 if (!event
.IsKindOf(CLASSINFO(wxCommandEvent
)) || !GetParent() || !GetParent()->ProcessEvent(event
))
1827 return wxEvtHandler::ProcessEvent(event
);
1835 void wxDocChildFrame::OnActivate(wxActivateEvent
& event
)
1837 wxFrame::OnActivate(event
);
1840 m_childView
->Activate(event
.GetActive());
1843 void wxDocChildFrame::OnCloseWindow(wxCloseEvent
& event
)
1847 bool ans
= event
.CanVeto()
1848 ? m_childView
->Close(false) // false means don't delete associated window
1849 : true; // Must delete.
1853 m_childView
->Activate(false);
1855 m_childView
= (wxView
*) NULL
;
1856 m_childDocument
= (wxDocument
*) NULL
;
1867 // ----------------------------------------------------------------------------
1868 // Default parent frame
1869 // ----------------------------------------------------------------------------
1871 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1872 EVT_MENU(wxID_EXIT
, wxDocParentFrame::OnExit
)
1873 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE9
, wxDocParentFrame::OnMRUFile
)
1874 EVT_CLOSE(wxDocParentFrame::OnCloseWindow
)
1877 wxDocParentFrame::wxDocParentFrame(wxDocManager
*manager
,
1880 const wxString
& title
,
1884 const wxString
& name
)
1885 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1887 m_docManager
= manager
;
1890 void wxDocParentFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
1895 void wxDocParentFrame::OnMRUFile(wxCommandEvent
& event
)
1897 int n
= event
.GetId() - wxID_FILE1
; // the index in MRU list
1898 wxString
filename(m_docManager
->GetHistoryFile(n
));
1899 if ( !filename
.empty() )
1901 // verify that the file exists before doing anything else
1902 if ( wxFile::Exists(filename
) )
1905 if (!m_docManager
->CreateDocument(filename
, wxDOC_SILENT
))
1907 // remove the file from the MRU list. The user should already be notified.
1908 m_docManager
->RemoveFileFromHistory(n
);
1910 wxLogError(_("The file '%s' couldn't be opened.\nIt has been removed from the most recently used files list."),
1916 // remove the bogus filename from the MRU list and notify the user
1918 m_docManager
->RemoveFileFromHistory(n
);
1920 wxLogError(_("The file '%s' doesn't exist and couldn't be opened.\nIt has been removed from the most recently used files list."),
1926 // Extend event processing to search the view's event table
1927 bool wxDocParentFrame::ProcessEvent(wxEvent
& event
)
1929 // Try the document manager, then do default processing
1930 if (!m_docManager
|| !m_docManager
->ProcessEvent(event
))
1931 return wxEvtHandler::ProcessEvent(event
);
1936 // Define the behaviour for the frame closing
1937 // - must delete all frames except for the main one.
1938 void wxDocParentFrame::OnCloseWindow(wxCloseEvent
& event
)
1940 if (m_docManager
->Clear(!event
.CanVeto()))
1948 #if wxUSE_PRINTING_ARCHITECTURE
1950 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
)
1953 m_printoutView
= view
;
1956 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
1960 // Get the logical pixels per inch of screen and printer
1961 int ppiScreenX
, ppiScreenY
;
1962 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
1963 wxUnusedVar(ppiScreenY
);
1964 int ppiPrinterX
, ppiPrinterY
;
1965 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
1966 wxUnusedVar(ppiPrinterY
);
1968 // This scales the DC so that the printout roughly represents the
1969 // the screen scaling. The text point size _should_ be the right size
1970 // but in fact is too small for some reason. This is a detail that will
1971 // need to be addressed at some point but can be fudged for the
1973 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
1975 // Now we have to check in case our real page size is reduced
1976 // (e.g. because we're drawing to a print preview memory DC)
1977 int pageWidth
, pageHeight
;
1979 dc
->GetSize(&w
, &h
);
1980 GetPageSizePixels(&pageWidth
, &pageHeight
);
1981 wxUnusedVar(pageHeight
);
1983 // If printer pageWidth == current DC width, then this doesn't
1984 // change. But w might be the preview bitmap width, so scale down.
1985 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
1986 dc
->SetUserScale(overallScale
, overallScale
);
1990 m_printoutView
->OnDraw(dc
);
1995 bool wxDocPrintout::HasPage(int pageNum
)
1997 return (pageNum
== 1);
2000 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
2002 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
2008 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
2016 #endif // wxUSE_PRINTING_ARCHITECTURE
2018 // ----------------------------------------------------------------------------
2019 // File history processor
2020 // ----------------------------------------------------------------------------
2022 static inline wxChar
* MYcopystring(const wxString
& s
)
2024 wxChar
* copy
= new wxChar
[s
.length() + 1];
2025 return wxStrcpy(copy
, s
.c_str());
2028 static inline wxChar
* MYcopystring(const wxChar
* s
)
2030 wxChar
* copy
= new wxChar
[wxStrlen(s
) + 1];
2031 return wxStrcpy(copy
, s
);
2034 wxFileHistory::wxFileHistory(size_t maxFiles
, wxWindowID idBase
)
2036 m_fileMaxFiles
= maxFiles
;
2039 m_fileHistory
= new wxChar
*[m_fileMaxFiles
];
2042 wxFileHistory::~wxFileHistory()
2045 for (i
= 0; i
< m_fileHistoryN
; i
++)
2046 delete[] m_fileHistory
[i
];
2047 delete[] m_fileHistory
;
2050 // File history management
2051 void wxFileHistory::AddFileToHistory(const wxString
& file
)
2055 // Check we don't already have this file
2056 for (i
= 0; i
< m_fileHistoryN
; i
++)
2058 #if defined( __WXMSW__ ) // Add any other OSes with case insensitive file names
2059 wxString testString
;
2060 if ( m_fileHistory
[i
] )
2061 testString
= m_fileHistory
[i
];
2062 if ( m_fileHistory
[i
] && ( file
.Lower() == testString
.Lower() ) )
2064 if ( m_fileHistory
[i
] && ( file
== m_fileHistory
[i
] ) )
2067 // we do have it, move it to the top of the history
2068 RemoveFileFromHistory (i
);
2069 AddFileToHistory (file
);
2074 // if we already have a full history, delete the one at the end
2075 if ( m_fileMaxFiles
== m_fileHistoryN
)
2077 RemoveFileFromHistory (m_fileHistoryN
- 1);
2078 AddFileToHistory (file
);
2082 // Add to the project file history:
2083 // Move existing files (if any) down so we can insert file at beginning.
2084 if (m_fileHistoryN
< m_fileMaxFiles
)
2086 wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2089 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2090 if ( m_fileHistoryN
== 0 && menu
->GetMenuItemCount() )
2092 menu
->AppendSeparator();
2094 menu
->Append(m_idBase
+m_fileHistoryN
, _("[EMPTY]"));
2095 node
= node
->GetNext();
2099 // Shuffle filenames down
2100 for (i
= (m_fileHistoryN
-1); i
> 0; i
--)
2102 m_fileHistory
[i
] = m_fileHistory
[i
-1];
2104 m_fileHistory
[0] = MYcopystring(file
);
2106 // this is the directory of the last opened file
2107 wxString pathCurrent
;
2108 wxSplitPath( m_fileHistory
[0], &pathCurrent
, NULL
, NULL
);
2109 for (i
= 0; i
< m_fileHistoryN
; i
++)
2111 if ( m_fileHistory
[i
] )
2113 // if in same directory just show the filename; otherwise the full
2115 wxString pathInMenu
, path
, filename
, ext
;
2116 wxSplitPath( m_fileHistory
[i
], &path
, &filename
, &ext
);
2117 if ( path
== pathCurrent
)
2119 pathInMenu
= filename
;
2121 pathInMenu
= pathInMenu
+ wxFILE_SEP_EXT
+ ext
;
2125 // absolute path; could also set relative path
2126 pathInMenu
= m_fileHistory
[i
];
2130 buf
.Printf(s_MRUEntryFormat
, i
+ 1, pathInMenu
.c_str());
2131 wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2134 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2135 menu
->SetLabel(m_idBase
+ i
, buf
);
2136 node
= node
->GetNext();
2142 void wxFileHistory::RemoveFileFromHistory(size_t i
)
2144 wxCHECK_RET( i
< m_fileHistoryN
,
2145 wxT("invalid index in wxFileHistory::RemoveFileFromHistory") );
2147 // delete the element from the array (could use memmove() too...)
2148 delete [] m_fileHistory
[i
];
2151 for ( j
= i
; j
< m_fileHistoryN
- 1; j
++ )
2153 m_fileHistory
[j
] = m_fileHistory
[j
+ 1];
2156 wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2159 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2161 // shuffle filenames up
2163 for ( j
= i
; j
< m_fileHistoryN
- 1; j
++ )
2165 buf
.Printf(s_MRUEntryFormat
, j
+ 1, m_fileHistory
[j
]);
2166 menu
->SetLabel(m_idBase
+ j
, buf
);
2169 node
= node
->GetNext();
2171 // delete the last menu item which is unused now
2172 wxWindowID lastItemId
= m_idBase
+ m_fileHistoryN
- 1;
2173 if (menu
->FindItem(lastItemId
))
2175 menu
->Delete(lastItemId
);
2178 // delete the last separator too if no more files are left
2179 if ( m_fileHistoryN
== 1 )
2181 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetLast();
2184 wxMenuItem
*menuItem
= node
->GetData();
2185 if ( menuItem
->IsSeparator() )
2187 menu
->Delete(menuItem
);
2189 //else: should we search backwards for the last separator?
2191 //else: menu is empty somehow
2198 wxString
wxFileHistory::GetHistoryFile(size_t i
) const
2201 if ( i
< m_fileHistoryN
)
2203 s
= m_fileHistory
[i
];
2207 wxFAIL_MSG( wxT("bad index in wxFileHistory::GetHistoryFile") );
2213 void wxFileHistory::UseMenu(wxMenu
*menu
)
2215 if (!m_fileMenus
.Member(menu
))
2216 m_fileMenus
.Append(menu
);
2219 void wxFileHistory::RemoveMenu(wxMenu
*menu
)
2221 m_fileMenus
.DeleteObject(menu
);
2225 void wxFileHistory::Load(wxConfigBase
& config
)
2229 buf
.Printf(wxT("file%d"), (int)m_fileHistoryN
+1);
2230 wxString historyFile
;
2231 while ((m_fileHistoryN
< m_fileMaxFiles
) && config
.Read(buf
, &historyFile
) && (!historyFile
.empty()))
2233 m_fileHistory
[m_fileHistoryN
] = MYcopystring((const wxChar
*) historyFile
);
2235 buf
.Printf(wxT("file%d"), (int)m_fileHistoryN
+1);
2236 historyFile
= wxEmptyString
;
2241 void wxFileHistory::Save(wxConfigBase
& config
)
2244 for (i
= 0; i
< m_fileMaxFiles
; i
++)
2247 buf
.Printf(wxT("file%d"), (int)i
+1);
2248 if (i
< m_fileHistoryN
)
2249 config
.Write(buf
, wxString(m_fileHistory
[i
]));
2251 config
.Write(buf
, wxEmptyString
);
2254 #endif // wxUSE_CONFIG
2256 void wxFileHistory::AddFilesToMenu()
2258 if (m_fileHistoryN
> 0)
2260 wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2263 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2264 if (menu
->GetMenuItemCount())
2266 menu
->AppendSeparator();
2270 for (i
= 0; i
< m_fileHistoryN
; i
++)
2272 if (m_fileHistory
[i
])
2275 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
2276 menu
->Append(m_idBase
+i
, buf
);
2279 node
= node
->GetNext();
2284 void wxFileHistory::AddFilesToMenu(wxMenu
* menu
)
2286 if (m_fileHistoryN
> 0)
2288 if (menu
->GetMenuItemCount())
2290 menu
->AppendSeparator();
2294 for (i
= 0; i
< m_fileHistoryN
; i
++)
2296 if (m_fileHistory
[i
])
2299 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
2300 menu
->Append(m_idBase
+i
, buf
);
2306 // ----------------------------------------------------------------------------
2307 // Permits compatibility with existing file formats and functions that
2308 // manipulate files directly
2309 // ----------------------------------------------------------------------------
2311 #if wxUSE_STD_IOSTREAM
2313 bool wxTransferFileToStream(const wxString
& filename
, wxSTD ostream
& stream
)
2315 wxFFile
file(filename
, _T("rb"));
2316 if ( !file
.IsOpened() )
2324 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2328 stream
.write(buf
, nRead
);
2332 while ( !file
.Eof() );
2337 bool wxTransferStreamToFile(wxSTD istream
& stream
, const wxString
& filename
)
2339 wxFFile
file(filename
, _T("wb"));
2340 if ( !file
.IsOpened() )
2346 stream
.read(buf
, WXSIZEOF(buf
));
2347 if ( !stream
.bad() ) // fail may be set on EOF, don't use operator!()
2349 if ( !file
.Write(buf
, stream
.gcount()) )
2353 while ( !stream
.eof() );
2358 #else // !wxUSE_STD_IOSTREAM
2360 bool wxTransferFileToStream(const wxString
& filename
, wxOutputStream
& stream
)
2362 wxFFile
file(filename
, _T("rb"));
2363 if ( !file
.IsOpened() )
2371 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2375 stream
.Write(buf
, nRead
);
2379 while ( !file
.Eof() );
2384 bool wxTransferStreamToFile(wxInputStream
& stream
, const wxString
& filename
)
2386 wxFFile
file(filename
, _T("wb"));
2387 if ( !file
.IsOpened() )
2393 stream
.Read(buf
, WXSIZEOF(buf
));
2395 const size_t nRead
= stream
.LastRead();
2396 if ( !nRead
|| !file
.Write(buf
, nRead
) )
2399 while ( !stream
.Eof() );
2404 #endif // wxUSE_STD_IOSTREAM/!wxUSE_STD_IOSTREAM
2406 #endif // wxUSE_DOC_VIEW_ARCHITECTURE