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 // ----------------------------------------------------------------------------
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();
187 wxNode
*node
= m_documentViews
.GetFirst();
190 wxView
*view
= (wxView
*)node
->GetData();
194 wxNode
*next
= node
->GetNext();
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 wxString tmp
= wxFileSelector(_("Save as"),
255 docTemplate
->GetDirectory(),
256 wxFileNameFromPath(GetFilename()),
257 docTemplate
->GetDefaultExtension(),
258 docTemplate
->GetFileFilter(),
259 wxSAVE
| wxOVERWRITE_PROMPT
,
260 GetDocumentWindow());
265 wxString
fileName(tmp
);
266 wxString path
, name
, ext
;
267 wxSplitPath(fileName
, & path
, & name
, & ext
);
269 if (ext
.IsEmpty() || ext
== wxT(""))
271 fileName
+= wxT(".");
272 fileName
+= docTemplate
->GetDefaultExtension();
275 SetFilename(fileName
);
276 SetTitle(wxFileNameFromPath(fileName
));
278 // Notify the views that the filename has changed
279 wxNode
*node
= m_documentViews
.GetFirst();
282 wxView
*view
= (wxView
*)node
->GetData();
283 view
->OnChangeFilename();
284 node
= node
->GetNext();
287 // Files that were not saved correctly are not added to the FileHistory.
288 if (!OnSaveDocument(m_documentFile
))
291 // A file that doesn't use the default extension of its document template cannot be opened
292 // via the FileHistory, so we do not add it.
293 if (docTemplate
->FileMatchesTemplate(fileName
))
295 GetDocumentManager()->AddFileToHistory(fileName
);
299 // The user will probably not be able to open the file again, so
300 // we could warn about the wrong file-extension here.
305 bool wxDocument::OnSaveDocument(const wxString
& file
)
311 if (wxTheApp
->GetAppName() != wxT(""))
312 msgTitle
= wxTheApp
->GetAppName();
314 msgTitle
= wxString(_("File error"));
316 #if wxUSE_STD_IOSTREAM
317 wxSTD ofstream
store(file
.mb_str());
318 if (store
.fail() || store
.bad())
320 wxFileOutputStream
store(file
);
321 if (store
.GetLastError() != wxSTREAM_NO_ERROR
)
324 (void)wxMessageBox(_("Sorry, could not open this file for saving."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
325 GetDocumentWindow());
329 if (!SaveObject(store
))
331 (void)wxMessageBox(_("Sorry, could not save this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
332 GetDocumentWindow());
338 SetDocumentSaved(TRUE
);
340 wxFileName
fn(file
) ;
341 fn
.MacSetDefaultTypeAndCreator() ;
346 bool wxDocument::OnOpenDocument(const wxString
& file
)
348 if (!OnSaveModified())
352 if (wxTheApp
->GetAppName() != wxT(""))
353 msgTitle
= wxTheApp
->GetAppName();
355 msgTitle
= wxString(_("File error"));
357 #if wxUSE_STD_IOSTREAM
358 wxSTD ifstream
store(file
.mb_str());
359 if (store
.fail() || store
.bad())
361 wxFileInputStream
store(file
);
362 if (store
.GetLastError() != wxSTREAM_NO_ERROR
)
365 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
|wxICON_EXCLAMATION
,
366 GetDocumentWindow());
369 #if wxUSE_STD_IOSTREAM
371 if ( !store
&& !store
.eof() )
373 int res
= LoadObject(store
).GetLastError();
374 if ((res
!= wxSTREAM_NO_ERROR
) &&
375 (res
!= wxSTREAM_EOF
))
378 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
|wxICON_EXCLAMATION
,
379 GetDocumentWindow());
382 SetFilename(file
, TRUE
);
391 #if wxUSE_STD_IOSTREAM
392 wxSTD istream
& wxDocument::LoadObject(wxSTD istream
& stream
)
394 wxInputStream
& wxDocument::LoadObject(wxInputStream
& stream
)
400 #if wxUSE_STD_IOSTREAM
401 wxSTD ostream
& wxDocument::SaveObject(wxSTD ostream
& stream
)
403 wxOutputStream
& wxDocument::SaveObject(wxOutputStream
& stream
)
409 bool wxDocument::Revert()
415 // Get title, or filename if no title, else unnamed
416 bool wxDocument::GetPrintableName(wxString
& buf
) const
418 if (m_documentTitle
!= wxT(""))
420 buf
= m_documentTitle
;
423 else if (m_documentFile
!= wxT(""))
425 buf
= wxFileNameFromPath(m_documentFile
);
435 wxWindow
*wxDocument::GetDocumentWindow() const
437 wxView
*view
= GetFirstView();
439 return view
->GetFrame();
441 return wxTheApp
->GetTopWindow();
444 wxCommandProcessor
*wxDocument::OnCreateCommandProcessor()
446 return new wxCommandProcessor
;
449 // TRUE if safe to close
450 bool wxDocument::OnSaveModified()
455 GetPrintableName(title
);
458 if (wxTheApp
->GetAppName() != wxT(""))
459 msgTitle
= wxTheApp
->GetAppName();
461 msgTitle
= wxString(_("Warning"));
464 prompt
.Printf(_("Do you want to save changes to document %s?"),
465 (const wxChar
*)title
);
466 int res
= wxMessageBox(prompt
, msgTitle
,
467 wxYES_NO
|wxCANCEL
|wxICON_QUESTION
,
468 GetDocumentWindow());
474 else if (res
== wxYES
)
476 else if (res
== wxCANCEL
)
482 bool wxDocument::Draw(wxDC
& WXUNUSED(context
))
487 bool wxDocument::AddView(wxView
*view
)
489 if (!m_documentViews
.Member(view
))
491 m_documentViews
.Append(view
);
497 bool wxDocument::RemoveView(wxView
*view
)
499 (void)m_documentViews
.DeleteObject(view
);
504 bool wxDocument::OnCreate(const wxString
& WXUNUSED(path
), long flags
)
506 if (GetDocumentTemplate()->CreateView(this, flags
))
512 // Called after a view is added or removed.
513 // The default implementation deletes the document if
514 // there are no more views.
515 void wxDocument::OnChangedViewList()
517 if (m_documentViews
.GetCount() == 0)
519 if (OnSaveModified())
526 void wxDocument::UpdateAllViews(wxView
*sender
, wxObject
*hint
)
528 wxNode
*node
= m_documentViews
.GetFirst();
531 wxView
*view
= (wxView
*)node
->GetData();
533 view
->OnUpdate(sender
, hint
);
534 node
= node
->GetNext();
538 void wxDocument::NotifyClosing()
540 wxNode
*node
= m_documentViews
.GetFirst();
543 wxView
*view
= (wxView
*)node
->GetData();
544 view
->OnClosingDocument();
545 node
= node
->GetNext();
549 void wxDocument::SetFilename(const wxString
& filename
, bool notifyViews
)
551 m_documentFile
= filename
;
554 // Notify the views that the filename has changed
555 wxNode
*node
= m_documentViews
.GetFirst();
558 wxView
*view
= (wxView
*)node
->GetData();
559 view
->OnChangeFilename();
560 node
= node
->GetNext();
565 // ----------------------------------------------------------------------------
567 // ----------------------------------------------------------------------------
571 m_viewDocument
= (wxDocument
*) NULL
;
573 m_viewFrame
= (wxFrame
*) NULL
;
578 GetDocumentManager()->ActivateView(this, FALSE
);
579 m_viewDocument
->RemoveView(this);
582 // Extend event processing to search the document's event table
583 bool wxView::ProcessEvent(wxEvent
& event
)
585 if ( !GetDocument() || !GetDocument()->ProcessEvent(event
) )
586 return wxEvtHandler::ProcessEvent(event
);
591 void wxView::OnActivateView(bool WXUNUSED(activate
), wxView
*WXUNUSED(activeView
), wxView
*WXUNUSED(deactiveView
))
595 void wxView::OnPrint(wxDC
*dc
, wxObject
*WXUNUSED(info
))
600 void wxView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
))
604 void wxView::OnChangeFilename()
606 if (GetFrame() && GetDocument())
610 GetDocument()->GetPrintableName(title
);
612 GetFrame()->SetTitle(title
);
616 void wxView::SetDocument(wxDocument
*doc
)
618 m_viewDocument
= doc
;
623 bool wxView::Close(bool deleteWindow
)
625 if (OnClose(deleteWindow
))
631 void wxView::Activate(bool activate
)
633 if (GetDocument() && GetDocumentManager())
635 OnActivateView(activate
, this, GetDocumentManager()->GetCurrentView());
636 GetDocumentManager()->ActivateView(this, activate
);
640 bool wxView::OnClose(bool WXUNUSED(deleteWindow
))
642 return GetDocument() ? GetDocument()->Close() : TRUE
;
645 #if wxUSE_PRINTING_ARCHITECTURE
646 wxPrintout
*wxView::OnCreatePrintout()
648 return new wxDocPrintout(this);
650 #endif // wxUSE_PRINTING_ARCHITECTURE
652 // ----------------------------------------------------------------------------
654 // ----------------------------------------------------------------------------
656 wxDocTemplate::wxDocTemplate(wxDocManager
*manager
,
657 const wxString
& descr
,
658 const wxString
& filter
,
661 const wxString
& docTypeName
,
662 const wxString
& viewTypeName
,
663 wxClassInfo
*docClassInfo
,
664 wxClassInfo
*viewClassInfo
,
667 m_documentManager
= manager
;
668 m_description
= descr
;
671 m_fileFilter
= filter
;
673 m_docTypeName
= docTypeName
;
674 m_viewTypeName
= viewTypeName
;
675 m_documentManager
->AssociateTemplate(this);
677 m_docClassInfo
= docClassInfo
;
678 m_viewClassInfo
= viewClassInfo
;
681 wxDocTemplate::~wxDocTemplate()
683 m_documentManager
->DisassociateTemplate(this);
686 // Tries to dynamically construct an object of the right class.
687 wxDocument
*wxDocTemplate::CreateDocument(const wxString
& path
, long flags
)
690 return (wxDocument
*) NULL
;
691 wxDocument
*doc
= (wxDocument
*)m_docClassInfo
->CreateObject();
692 doc
->SetFilename(path
);
693 doc
->SetDocumentTemplate(this);
694 GetDocumentManager()->AddDocument(doc
);
695 doc
->SetCommandProcessor(doc
->OnCreateCommandProcessor());
697 if (doc
->OnCreate(path
, flags
))
701 if (GetDocumentManager()->GetDocuments().Member(doc
))
702 doc
->DeleteAllViews();
703 return (wxDocument
*) NULL
;
707 wxView
*wxDocTemplate::CreateView(wxDocument
*doc
, long flags
)
709 if (!m_viewClassInfo
)
710 return (wxView
*) NULL
;
711 wxView
*view
= (wxView
*)m_viewClassInfo
->CreateObject();
712 view
->SetDocument(doc
);
713 if (view
->OnCreate(doc
, flags
))
720 return (wxView
*) NULL
;
724 // The default (very primitive) format detection: check is the extension is
725 // that of the template
726 bool wxDocTemplate::FileMatchesTemplate(const wxString
& path
)
728 return GetDefaultExtension().IsSameAs(FindExtension(path
));
731 // ----------------------------------------------------------------------------
733 // ----------------------------------------------------------------------------
735 BEGIN_EVENT_TABLE(wxDocManager
, wxEvtHandler
)
736 EVT_MENU(wxID_OPEN
, wxDocManager::OnFileOpen
)
737 EVT_MENU(wxID_CLOSE
, wxDocManager::OnFileClose
)
738 EVT_MENU(wxID_CLOSE_ALL
, wxDocManager::OnFileCloseAll
)
739 EVT_MENU(wxID_REVERT
, wxDocManager::OnFileRevert
)
740 EVT_MENU(wxID_NEW
, wxDocManager::OnFileNew
)
741 EVT_MENU(wxID_SAVE
, wxDocManager::OnFileSave
)
742 EVT_MENU(wxID_SAVEAS
, wxDocManager::OnFileSaveAs
)
743 EVT_MENU(wxID_UNDO
, wxDocManager::OnUndo
)
744 EVT_MENU(wxID_REDO
, wxDocManager::OnRedo
)
746 EVT_UPDATE_UI(wxID_OPEN
, wxDocManager::OnUpdateFileOpen
)
747 EVT_UPDATE_UI(wxID_CLOSE
, wxDocManager::OnUpdateFileClose
)
748 EVT_UPDATE_UI(wxID_CLOSE_ALL
, wxDocManager::OnUpdateFileClose
)
749 EVT_UPDATE_UI(wxID_REVERT
, wxDocManager::OnUpdateFileRevert
)
750 EVT_UPDATE_UI(wxID_NEW
, wxDocManager::OnUpdateFileNew
)
751 EVT_UPDATE_UI(wxID_SAVE
, wxDocManager::OnUpdateFileSave
)
752 EVT_UPDATE_UI(wxID_SAVEAS
, wxDocManager::OnUpdateFileSaveAs
)
753 EVT_UPDATE_UI(wxID_UNDO
, wxDocManager::OnUpdateUndo
)
754 EVT_UPDATE_UI(wxID_REDO
, wxDocManager::OnUpdateRedo
)
756 #if wxUSE_PRINTING_ARCHITECTURE
757 EVT_MENU(wxID_PRINT
, wxDocManager::OnPrint
)
758 EVT_MENU(wxID_PRINT_SETUP
, wxDocManager::OnPrintSetup
)
759 EVT_MENU(wxID_PREVIEW
, wxDocManager::OnPreview
)
761 EVT_UPDATE_UI(wxID_PRINT
, wxDocManager::OnUpdatePrint
)
762 EVT_UPDATE_UI(wxID_PRINT_SETUP
, wxDocManager::OnUpdatePrintSetup
)
763 EVT_UPDATE_UI(wxID_PREVIEW
, wxDocManager::OnUpdatePreview
)
767 wxDocManager
* wxDocManager::sm_docManager
= (wxDocManager
*) NULL
;
769 wxDocManager::wxDocManager(long flags
, bool initialize
)
771 m_defaultDocumentNameCounter
= 1;
773 m_currentView
= (wxView
*) NULL
;
774 m_maxDocsOpen
= 10000;
775 m_fileHistory
= (wxFileHistory
*) NULL
;
778 sm_docManager
= this;
781 wxDocManager::~wxDocManager()
785 delete m_fileHistory
;
786 sm_docManager
= (wxDocManager
*) NULL
;
789 // closes the specified document
790 bool wxDocManager::CloseDocument(wxDocument
* doc
, bool force
)
792 if (doc
->Close() || force
)
794 // Implicitly deletes the document when
795 // the last view is deleted
796 doc
->DeleteAllViews();
798 // Check we're really deleted
799 if (m_docs
.Member(doc
))
807 bool wxDocManager::CloseDocuments(bool force
)
809 wxNode
*node
= m_docs
.GetFirst();
812 wxDocument
*doc
= (wxDocument
*)node
->GetData();
813 wxNode
*next
= node
->GetNext();
815 if (!CloseDocument(doc
, force
))
818 // This assumes that documents are not connected in
819 // any way, i.e. deleting one document does NOT
826 bool wxDocManager::Clear(bool force
)
828 if (!CloseDocuments(force
))
831 wxNode
*node
= m_templates
.GetFirst();
834 wxDocTemplate
*templ
= (wxDocTemplate
*) node
->GetData();
835 wxNode
* next
= node
->GetNext();
842 bool wxDocManager::Initialize()
844 m_fileHistory
= OnCreateFileHistory();
848 wxFileHistory
*wxDocManager::OnCreateFileHistory()
850 return new wxFileHistory
;
853 void wxDocManager::OnFileClose(wxCommandEvent
& WXUNUSED(event
))
855 wxDocument
*doc
= GetCurrentDocument();
860 doc
->DeleteAllViews();
861 if (m_docs
.Member(doc
))
866 void wxDocManager::OnFileCloseAll(wxCommandEvent
& WXUNUSED(event
))
868 CloseDocuments(FALSE
);
871 void wxDocManager::OnFileNew(wxCommandEvent
& WXUNUSED(event
))
873 CreateDocument( wxT(""), wxDOC_NEW
);
876 void wxDocManager::OnFileOpen(wxCommandEvent
& WXUNUSED(event
))
878 if ( !CreateDocument( wxT(""), 0) )
884 void wxDocManager::OnFileRevert(wxCommandEvent
& WXUNUSED(event
))
886 wxDocument
*doc
= GetCurrentDocument();
892 void wxDocManager::OnFileSave(wxCommandEvent
& WXUNUSED(event
))
894 wxDocument
*doc
= GetCurrentDocument();
900 void wxDocManager::OnFileSaveAs(wxCommandEvent
& WXUNUSED(event
))
902 wxDocument
*doc
= GetCurrentDocument();
908 void wxDocManager::OnPrint(wxCommandEvent
& WXUNUSED(event
))
910 #if wxUSE_PRINTING_ARCHITECTURE
911 wxView
*view
= GetCurrentView();
915 wxPrintout
*printout
= view
->OnCreatePrintout();
919 printer
.Print(view
->GetFrame(), printout
, TRUE
);
923 #endif // wxUSE_PRINTING_ARCHITECTURE
926 void wxDocManager::OnPrintSetup(wxCommandEvent
& WXUNUSED(event
))
928 #if wxUSE_PRINTING_ARCHITECTURE
929 wxWindow
*parentWin
= wxTheApp
->GetTopWindow();
930 wxView
*view
= GetCurrentView();
932 parentWin
= view
->GetFrame();
934 wxPrintDialogData data
;
936 wxPrintDialog
printerDialog(parentWin
, &data
);
937 printerDialog
.GetPrintDialogData().SetSetupDialog(TRUE
);
938 printerDialog
.ShowModal();
939 #endif // wxUSE_PRINTING_ARCHITECTURE
942 void wxDocManager::OnPreview(wxCommandEvent
& WXUNUSED(event
))
944 #if wxUSE_PRINTING_ARCHITECTURE
945 wxView
*view
= GetCurrentView();
949 wxPrintout
*printout
= view
->OnCreatePrintout();
952 // Pass two printout objects: for preview, and possible printing.
953 wxPrintPreviewBase
*preview
= (wxPrintPreviewBase
*) NULL
;
954 preview
= new wxPrintPreview(printout
, view
->OnCreatePrintout());
956 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, (wxFrame
*)wxTheApp
->GetTopWindow(), _("Print Preview"),
957 wxPoint(100, 100), wxSize(600, 650));
958 frame
->Centre(wxBOTH
);
962 #endif // wxUSE_PRINTING_ARCHITECTURE
965 void wxDocManager::OnUndo(wxCommandEvent
& event
)
967 wxDocument
*doc
= GetCurrentDocument();
970 if (doc
->GetCommandProcessor())
971 doc
->GetCommandProcessor()->Undo();
976 void wxDocManager::OnRedo(wxCommandEvent
& event
)
978 wxDocument
*doc
= GetCurrentDocument();
981 if (doc
->GetCommandProcessor())
982 doc
->GetCommandProcessor()->Redo();
987 // Handlers for UI update commands
989 void wxDocManager::OnUpdateFileOpen(wxUpdateUIEvent
& event
)
991 event
.Enable( TRUE
);
994 void wxDocManager::OnUpdateFileClose(wxUpdateUIEvent
& event
)
996 wxDocument
*doc
= GetCurrentDocument();
997 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1000 void wxDocManager::OnUpdateFileRevert(wxUpdateUIEvent
& event
)
1002 wxDocument
*doc
= GetCurrentDocument();
1003 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1006 void wxDocManager::OnUpdateFileNew(wxUpdateUIEvent
& event
)
1008 event
.Enable( TRUE
);
1011 void wxDocManager::OnUpdateFileSave(wxUpdateUIEvent
& event
)
1013 wxDocument
*doc
= GetCurrentDocument();
1014 event
.Enable( doc
&& doc
->IsModified() );
1017 void wxDocManager::OnUpdateFileSaveAs(wxUpdateUIEvent
& event
)
1019 wxDocument
*doc
= GetCurrentDocument();
1020 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1023 void wxDocManager::OnUpdateUndo(wxUpdateUIEvent
& event
)
1025 wxDocument
*doc
= GetCurrentDocument();
1027 event
.Enable(FALSE
);
1028 else if (!doc
->GetCommandProcessor())
1032 event
.Enable( doc
->GetCommandProcessor()->CanUndo() );
1033 doc
->GetCommandProcessor()->SetMenuStrings();
1037 void wxDocManager::OnUpdateRedo(wxUpdateUIEvent
& event
)
1039 wxDocument
*doc
= GetCurrentDocument();
1041 event
.Enable(FALSE
);
1042 else if (!doc
->GetCommandProcessor())
1046 event
.Enable( doc
->GetCommandProcessor()->CanRedo() );
1047 doc
->GetCommandProcessor()->SetMenuStrings();
1051 void wxDocManager::OnUpdatePrint(wxUpdateUIEvent
& event
)
1053 wxDocument
*doc
= GetCurrentDocument();
1054 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1057 void wxDocManager::OnUpdatePrintSetup(wxUpdateUIEvent
& event
)
1059 event
.Enable( TRUE
);
1062 void wxDocManager::OnUpdatePreview(wxUpdateUIEvent
& event
)
1064 wxDocument
*doc
= GetCurrentDocument();
1065 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1068 wxView
*wxDocManager::GetCurrentView() const
1071 return m_currentView
;
1072 if (m_docs
.GetCount() == 1)
1074 wxDocument
* doc
= (wxDocument
*) m_docs
.GetFirst()->GetData();
1075 return doc
->GetFirstView();
1077 return (wxView
*) NULL
;
1080 // Extend event processing to search the view's event table
1081 bool wxDocManager::ProcessEvent(wxEvent
& event
)
1083 wxView
* view
= GetCurrentView();
1086 if (view
->ProcessEvent(event
))
1089 return wxEvtHandler::ProcessEvent(event
);
1092 wxDocument
*wxDocManager::CreateDocument(const wxString
& path
, long flags
)
1094 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.GetCount()];
1097 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1099 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Item(i
)->GetData());
1100 if (temp
->IsVisible())
1102 templates
[n
] = temp
;
1109 return (wxDocument
*) NULL
;
1112 wxDocument
* docToClose
= NULL
;
1114 // If we've reached the max number of docs, close the
1116 if ( (int)GetDocuments().GetCount() >= m_maxDocsOpen
)
1118 wxDocument
*doc
= (wxDocument
*)GetDocuments().GetFirst()->GetData();
1122 // New document: user chooses a template, unless there's only one.
1123 if (flags
& wxDOC_NEW
)
1129 if (!CloseDocument(docToClose
, FALSE
))
1136 wxDocTemplate
*temp
= templates
[0];
1138 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
1142 newDoc
->SetDocumentName(temp
->GetDocumentName());
1143 newDoc
->SetDocumentTemplate(temp
);
1144 newDoc
->OnNewDocument();
1149 wxDocTemplate
*temp
= SelectDocumentType(templates
, n
);
1155 if (!CloseDocument(docToClose
, FALSE
))
1161 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
1165 newDoc
->SetDocumentName(temp
->GetDocumentName());
1166 newDoc
->SetDocumentTemplate(temp
);
1167 newDoc
->OnNewDocument();
1172 return (wxDocument
*) NULL
;
1175 // Existing document
1176 wxDocTemplate
*temp
= (wxDocTemplate
*) NULL
;
1178 wxString
path2(wxT(""));
1179 if (path
!= wxT(""))
1182 if (flags
& wxDOC_SILENT
)
1184 temp
= FindTemplateForPath(path2
);
1187 // Since we do not add files with non-default extensions to the FileHistory this
1188 // can only happen if the application changes the allowed templates in runtime.
1189 (void)wxMessageBox(_("Sorry, the format for this file is unknown."),
1191 wxOK
| wxICON_EXCLAMATION
, wxFindSuitableParent());
1195 temp
= SelectDocumentPath(templates
, n
, path2
, flags
);
1203 if (!CloseDocument(docToClose
, FALSE
))
1209 wxDocument
*newDoc
= temp
->CreateDocument(path2
, flags
);
1212 newDoc
->SetDocumentName(temp
->GetDocumentName());
1213 newDoc
->SetDocumentTemplate(temp
);
1214 if (!newDoc
->OnOpenDocument(path2
))
1216 newDoc
->DeleteAllViews();
1217 // delete newDoc; // Implicitly deleted by DeleteAllViews
1218 return (wxDocument
*) NULL
;
1220 // A file that doesn't use the default extension of its document
1221 // template cannot be opened via the FileHistory, so we do not
1223 if (temp
->FileMatchesTemplate(path2
))
1224 AddFileToHistory(path2
);
1229 return (wxDocument
*) NULL
;
1232 wxView
*wxDocManager::CreateView(wxDocument
*doc
, long flags
)
1234 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.GetCount()];
1237 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1239 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Item(i
)->GetData());
1240 if (temp
->IsVisible())
1242 if (temp
->GetDocumentName() == doc
->GetDocumentName())
1244 templates
[n
] = temp
;
1252 return (wxView
*) NULL
;
1256 wxDocTemplate
*temp
= templates
[0];
1258 wxView
*view
= temp
->CreateView(doc
, flags
);
1260 view
->SetViewName(temp
->GetViewName());
1264 wxDocTemplate
*temp
= SelectViewType(templates
, n
);
1268 wxView
*view
= temp
->CreateView(doc
, flags
);
1270 view
->SetViewName(temp
->GetViewName());
1274 return (wxView
*) NULL
;
1277 // Not yet implemented
1278 void wxDocManager::DeleteTemplate(wxDocTemplate
*WXUNUSED(temp
), long WXUNUSED(flags
))
1282 // Not yet implemented
1283 bool wxDocManager::FlushDoc(wxDocument
*WXUNUSED(doc
))
1288 wxDocument
*wxDocManager::GetCurrentDocument() const
1290 wxView
*view
= GetCurrentView();
1292 return view
->GetDocument();
1294 return (wxDocument
*) NULL
;
1297 // Make a default document name
1298 bool wxDocManager::MakeDefaultName(wxString
& name
)
1300 name
.Printf(_("unnamed%d"), m_defaultDocumentNameCounter
);
1301 m_defaultDocumentNameCounter
++;
1306 // Make a frame title (override this to do something different)
1307 // If docName is empty, a document is not currently active.
1308 wxString
wxDocManager::MakeFrameTitle(wxDocument
* doc
)
1310 wxString appName
= wxTheApp
->GetAppName();
1317 doc
->GetPrintableName(docName
);
1318 title
= docName
+ wxString(_(" - ")) + appName
;
1324 // Not yet implemented
1325 wxDocTemplate
*wxDocManager::MatchTemplate(const wxString
& WXUNUSED(path
))
1327 return (wxDocTemplate
*) NULL
;
1330 // File history management
1331 void wxDocManager::AddFileToHistory(const wxString
& file
)
1334 m_fileHistory
->AddFileToHistory(file
);
1337 void wxDocManager::RemoveFileFromHistory(size_t i
)
1340 m_fileHistory
->RemoveFileFromHistory(i
);
1343 wxString
wxDocManager::GetHistoryFile(size_t i
) const
1348 histFile
= m_fileHistory
->GetHistoryFile(i
);
1353 void wxDocManager::FileHistoryUseMenu(wxMenu
*menu
)
1356 m_fileHistory
->UseMenu(menu
);
1359 void wxDocManager::FileHistoryRemoveMenu(wxMenu
*menu
)
1362 m_fileHistory
->RemoveMenu(menu
);
1366 void wxDocManager::FileHistoryLoad(wxConfigBase
& config
)
1369 m_fileHistory
->Load(config
);
1372 void wxDocManager::FileHistorySave(wxConfigBase
& config
)
1375 m_fileHistory
->Save(config
);
1379 void wxDocManager::FileHistoryAddFilesToMenu(wxMenu
* menu
)
1382 m_fileHistory
->AddFilesToMenu(menu
);
1385 void wxDocManager::FileHistoryAddFilesToMenu()
1388 m_fileHistory
->AddFilesToMenu();
1391 size_t wxDocManager::GetHistoryFilesCount() const
1393 return m_fileHistory
? m_fileHistory
->GetCount() : 0;
1397 // Find out the document template via matching in the document file format
1398 // against that of the template
1399 wxDocTemplate
*wxDocManager::FindTemplateForPath(const wxString
& path
)
1401 wxDocTemplate
*theTemplate
= (wxDocTemplate
*) NULL
;
1403 // Find the template which this extension corresponds to
1404 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1406 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Item(i
)->GetData();
1407 if ( temp
->FileMatchesTemplate(path
) )
1416 // Try to get a more suitable parent frame than the top window,
1417 // for selection dialogs. Otherwise you may get an unexpected
1418 // window being activated when a dialog is shown.
1419 static wxWindow
* wxFindSuitableParent()
1421 wxWindow
* parent
= wxTheApp
->GetTopWindow();
1423 wxWindow
* focusWindow
= wxWindow::FindFocus();
1426 while (focusWindow
&&
1427 !focusWindow
->IsKindOf(CLASSINFO(wxDialog
)) &&
1428 !focusWindow
->IsKindOf(CLASSINFO(wxFrame
)))
1430 focusWindow
= focusWindow
->GetParent();
1433 parent
= focusWindow
;
1438 // Prompts user to open a file, using file specs in templates.
1439 // Must extend the file selector dialog or implement own; OR
1440 // match the extension to the template extension.
1442 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**templates
,
1443 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
1446 int WXUNUSED(noTemplates
),
1449 long WXUNUSED(flags
),
1450 bool WXUNUSED(save
))
1452 // We can only have multiple filters in Windows and GTK
1453 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
1457 for (i
= 0; i
< noTemplates
; i
++)
1459 if (templates
[i
]->IsVisible())
1461 // add a '|' to separate this filter from the previous one
1462 if ( !descrBuf
.IsEmpty() )
1463 descrBuf
<< wxT('|');
1465 descrBuf
<< templates
[i
]->GetDescription()
1466 << wxT(" (") << templates
[i
]->GetFileFilter() << wxT(") |")
1467 << templates
[i
]->GetFileFilter();
1471 wxString descrBuf
= wxT("*.*");
1474 int FilterIndex
= -1;
1476 wxWindow
* parent
= wxFindSuitableParent();
1478 wxString pathTmp
= wxFileSelectorEx(_("Select a file"),
1486 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)NULL
;
1487 if (!pathTmp
.IsEmpty())
1489 if (!wxFileExists(pathTmp
))
1492 if (!wxTheApp
->GetAppName().IsEmpty())
1493 msgTitle
= wxTheApp
->GetAppName();
1495 msgTitle
= wxString(_("File error"));
1497 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
1501 return (wxDocTemplate
*) NULL
;
1503 m_lastDirectory
= wxPathOnly(pathTmp
);
1507 // first choose the template using the extension, if this fails (i.e.
1508 // wxFileSelectorEx() didn't fill it), then use the path
1509 if ( FilterIndex
!= -1 )
1510 theTemplate
= templates
[FilterIndex
];
1512 theTemplate
= FindTemplateForPath(path
);
1515 // Since we do not add files with non-default extensions to the FileHistory this
1516 // can only happen if the application changes the allowed templates in runtime.
1517 (void)wxMessageBox(_("Sorry, the format for this file is unknown."),
1519 wxOK
| wxICON_EXCLAMATION
, wxFindSuitableParent());
1530 wxDocTemplate
*wxDocManager::SelectDocumentType(wxDocTemplate
**templates
,
1531 int noTemplates
, bool sort
)
1533 wxArrayString
strings(sort
);
1534 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1538 for (i
= 0; i
< noTemplates
; i
++)
1540 if (templates
[i
]->IsVisible())
1544 for (j
= 0; j
< n
; j
++)
1546 //filter out NOT unique documents + view combinations
1547 if ( templates
[i
]->m_docTypeName
== data
[j
]->m_docTypeName
&&
1548 templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
1555 strings
.Add(templates
[i
]->m_description
);
1557 data
[n
] = templates
[i
];
1565 // Yes, this will be slow, but template lists
1566 // are typically short.
1568 n
= strings
.Count();
1569 for (i
= 0; i
< n
; i
++)
1571 for (j
= 0; j
< noTemplates
; j
++)
1573 if (strings
[i
] == templates
[j
]->m_description
)
1574 data
[i
] = templates
[j
];
1579 wxDocTemplate
*theTemplate
;
1584 // no visible templates, hence nothing to choose from
1589 // don't propose the user to choose if he heas no choice
1590 theTemplate
= data
[0];
1594 // propose the user to choose one of several
1595 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1597 _("Select a document template"),
1601 wxFindSuitableParent()
1610 wxDocTemplate
*wxDocManager::SelectViewType(wxDocTemplate
**templates
,
1611 int noTemplates
, bool sort
)
1613 wxArrayString
strings(sort
);
1614 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1618 for (i
= 0; i
< noTemplates
; i
++)
1620 wxDocTemplate
*templ
= templates
[i
];
1621 if ( templ
->IsVisible() && !templ
->GetViewName().empty() )
1625 for (j
= 0; j
< n
; j
++)
1627 //filter out NOT unique views
1628 if ( templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
)
1634 strings
.Add(templ
->m_viewTypeName
);
1643 // Yes, this will be slow, but template lists
1644 // are typically short.
1646 n
= strings
.Count();
1647 for (i
= 0; i
< n
; i
++)
1649 for (j
= 0; j
< noTemplates
; j
++)
1651 if (strings
[i
] == templates
[j
]->m_viewTypeName
)
1652 data
[i
] = templates
[j
];
1657 wxDocTemplate
*theTemplate
;
1659 // the same logic as above
1663 theTemplate
= (wxDocTemplate
*)NULL
;
1667 theTemplate
= data
[0];
1671 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1673 _("Select a document view"),
1677 wxFindSuitableParent()
1686 void wxDocManager::AssociateTemplate(wxDocTemplate
*temp
)
1688 if (!m_templates
.Member(temp
))
1689 m_templates
.Append(temp
);
1692 void wxDocManager::DisassociateTemplate(wxDocTemplate
*temp
)
1694 m_templates
.DeleteObject(temp
);
1697 // Add and remove a document from the manager's list
1698 void wxDocManager::AddDocument(wxDocument
*doc
)
1700 if (!m_docs
.Member(doc
))
1704 void wxDocManager::RemoveDocument(wxDocument
*doc
)
1706 m_docs
.DeleteObject(doc
);
1709 // Views or windows should inform the document manager
1710 // when a view is going in or out of focus
1711 void wxDocManager::ActivateView(wxView
*view
, bool activate
)
1715 m_currentView
= view
;
1719 if ( m_currentView
== view
)
1721 // don't keep stale pointer
1722 m_currentView
= (wxView
*) NULL
;
1727 // ----------------------------------------------------------------------------
1728 // Default document child frame
1729 // ----------------------------------------------------------------------------
1731 BEGIN_EVENT_TABLE(wxDocChildFrame
, wxFrame
)
1732 EVT_ACTIVATE(wxDocChildFrame::OnActivate
)
1733 EVT_CLOSE(wxDocChildFrame::OnCloseWindow
)
1736 wxDocChildFrame::wxDocChildFrame(wxDocument
*doc
,
1740 const wxString
& title
,
1744 const wxString
& name
)
1745 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1747 m_childDocument
= doc
;
1750 view
->SetFrame(this);
1753 wxDocChildFrame::~wxDocChildFrame()
1757 // Extend event processing to search the view's event table
1758 bool wxDocChildFrame::ProcessEvent(wxEvent
& event
)
1761 m_childView
->Activate(TRUE
);
1763 if ( !m_childView
|| ! m_childView
->ProcessEvent(event
) )
1765 // Only hand up to the parent if it's a menu command
1766 if (!event
.IsKindOf(CLASSINFO(wxCommandEvent
)) || !GetParent() || !GetParent()->ProcessEvent(event
))
1767 return wxEvtHandler::ProcessEvent(event
);
1775 void wxDocChildFrame::OnActivate(wxActivateEvent
& event
)
1777 wxFrame::OnActivate(event
);
1780 m_childView
->Activate(event
.GetActive());
1783 void wxDocChildFrame::OnCloseWindow(wxCloseEvent
& event
)
1788 if (!event
.CanVeto())
1789 ans
= TRUE
; // Must delete.
1791 ans
= m_childView
->Close(FALSE
); // FALSE means don't delete associated window
1795 m_childView
->Activate(FALSE
);
1797 m_childView
= (wxView
*) NULL
;
1798 m_childDocument
= (wxDocument
*) NULL
;
1809 // ----------------------------------------------------------------------------
1810 // Default parent frame
1811 // ----------------------------------------------------------------------------
1813 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1814 EVT_MENU(wxID_EXIT
, wxDocParentFrame::OnExit
)
1815 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE9
, wxDocParentFrame::OnMRUFile
)
1816 EVT_CLOSE(wxDocParentFrame::OnCloseWindow
)
1819 wxDocParentFrame::wxDocParentFrame(wxDocManager
*manager
,
1822 const wxString
& title
,
1826 const wxString
& name
)
1827 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1829 m_docManager
= manager
;
1832 void wxDocParentFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
1837 void wxDocParentFrame::OnMRUFile(wxCommandEvent
& event
)
1839 int n
= event
.GetId() - wxID_FILE1
; // the index in MRU list
1840 wxString
filename(m_docManager
->GetHistoryFile(n
));
1841 if ( !filename
.IsEmpty() )
1843 // verify that the file exists before doing anything else
1844 if ( wxFile::Exists(filename
) )
1847 if (!m_docManager
->CreateDocument(filename
, wxDOC_SILENT
))
1849 // remove the file from the MRU list. The user should already be notified.
1850 m_docManager
->RemoveFileFromHistory(n
);
1852 wxLogError(_("The file '%s' couldn't be opened.\nIt has been removed from the most recently used files list."),
1858 // remove the bogus filename from the MRU list and notify the user
1860 m_docManager
->RemoveFileFromHistory(n
);
1862 wxLogError(_("The file '%s' doesn't exist and couldn't be opened.\nIt has been removed from the most recently used files list."),
1868 // Extend event processing to search the view's event table
1869 bool wxDocParentFrame::ProcessEvent(wxEvent
& event
)
1871 // Try the document manager, then do default processing
1872 if (!m_docManager
|| !m_docManager
->ProcessEvent(event
))
1873 return wxEvtHandler::ProcessEvent(event
);
1878 // Define the behaviour for the frame closing
1879 // - must delete all frames except for the main one.
1880 void wxDocParentFrame::OnCloseWindow(wxCloseEvent
& event
)
1882 if (m_docManager
->Clear(!event
.CanVeto()))
1890 #if wxUSE_PRINTING_ARCHITECTURE
1892 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
)
1895 m_printoutView
= view
;
1898 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
1902 // Get the logical pixels per inch of screen and printer
1903 int ppiScreenX
, ppiScreenY
;
1904 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
1905 int ppiPrinterX
, ppiPrinterY
;
1906 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
1908 // This scales the DC so that the printout roughly represents the
1909 // the screen scaling. The text point size _should_ be the right size
1910 // but in fact is too small for some reason. This is a detail that will
1911 // need to be addressed at some point but can be fudged for the
1913 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
1915 // Now we have to check in case our real page size is reduced
1916 // (e.g. because we're drawing to a print preview memory DC)
1917 int pageWidth
, pageHeight
;
1919 dc
->GetSize(&w
, &h
);
1920 GetPageSizePixels(&pageWidth
, &pageHeight
);
1922 // If printer pageWidth == current DC width, then this doesn't
1923 // change. But w might be the preview bitmap width, so scale down.
1924 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
1925 dc
->SetUserScale(overallScale
, overallScale
);
1929 m_printoutView
->OnDraw(dc
);
1934 bool wxDocPrintout::HasPage(int pageNum
)
1936 return (pageNum
== 1);
1939 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
1941 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
1947 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
1955 #endif // wxUSE_PRINTING_ARCHITECTURE
1957 // ----------------------------------------------------------------------------
1958 // File history processor
1959 // ----------------------------------------------------------------------------
1961 static inline wxChar
* MYcopystring(const wxString
& s
)
1963 wxChar
* copy
= new wxChar
[s
.length() + 1];
1964 return wxStrcpy(copy
, s
.c_str());
1967 static inline wxChar
* MYcopystring(const wxChar
* s
)
1969 wxChar
* copy
= new wxChar
[wxStrlen(s
) + 1];
1970 return wxStrcpy(copy
, s
);
1973 wxFileHistory::wxFileHistory(size_t maxFiles
, wxWindowID idBase
)
1975 m_fileMaxFiles
= maxFiles
;
1978 m_fileHistory
= new wxChar
*[m_fileMaxFiles
];
1981 wxFileHistory::~wxFileHistory()
1984 for (i
= 0; i
< m_fileHistoryN
; i
++)
1985 delete[] m_fileHistory
[i
];
1986 delete[] m_fileHistory
;
1989 // File history management
1990 void wxFileHistory::AddFileToHistory(const wxString
& file
)
1994 // Check we don't already have this file
1995 for (i
= 0; i
< m_fileHistoryN
; i
++)
1997 #if defined( __WXMSW__ ) // Add any other OSes with case insensitive file names
1998 wxString testString
;
1999 if ( m_fileHistory
[i
] )
2000 testString
= m_fileHistory
[i
];
2001 if ( m_fileHistory
[i
] && ( file
.Lower() == testString
.Lower() ) )
2003 if ( m_fileHistory
[i
] && ( file
== m_fileHistory
[i
] ) )
2006 // we do have it, move it to the top of the history
2007 RemoveFileFromHistory (i
);
2008 AddFileToHistory (file
);
2013 // if we already have a full history, delete the one at the end
2014 if ( m_fileMaxFiles
== m_fileHistoryN
)
2016 RemoveFileFromHistory (m_fileHistoryN
- 1);
2017 AddFileToHistory (file
);
2021 // Add to the project file history:
2022 // Move existing files (if any) down so we can insert file at beginning.
2023 if (m_fileHistoryN
< m_fileMaxFiles
)
2025 wxNode
* node
= m_fileMenus
.GetFirst();
2028 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2029 if ( m_fileHistoryN
== 0 && menu
->GetMenuItemCount() )
2031 menu
->AppendSeparator();
2033 menu
->Append(m_idBase
+m_fileHistoryN
, _("[EMPTY]"));
2034 node
= node
->GetNext();
2038 // Shuffle filenames down
2039 for (i
= (m_fileHistoryN
-1); i
> 0; i
--)
2041 m_fileHistory
[i
] = m_fileHistory
[i
-1];
2043 m_fileHistory
[0] = MYcopystring(file
);
2045 // this is the directory of the last opened file
2046 wxString pathCurrent
;
2047 wxSplitPath( m_fileHistory
[0], &pathCurrent
, NULL
, NULL
);
2048 for (i
= 0; i
< m_fileHistoryN
; i
++)
2050 if ( m_fileHistory
[i
] )
2052 // if in same directory just show the filename; otherwise the full
2054 wxString pathInMenu
, path
, filename
, ext
;
2055 wxSplitPath( m_fileHistory
[i
], &path
, &filename
, &ext
);
2056 if ( path
== pathCurrent
)
2058 pathInMenu
= filename
;
2060 pathInMenu
= pathInMenu
+ wxFILE_SEP_EXT
+ ext
;
2064 // absolute path; could also set relative path
2065 pathInMenu
= m_fileHistory
[i
];
2069 buf
.Printf(s_MRUEntryFormat
, i
+ 1, pathInMenu
.c_str());
2070 wxNode
* node
= m_fileMenus
.GetFirst();
2073 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2074 menu
->SetLabel(m_idBase
+ i
, buf
);
2075 node
= node
->GetNext();
2081 void wxFileHistory::RemoveFileFromHistory(size_t i
)
2083 wxCHECK_RET( i
< m_fileHistoryN
,
2084 wxT("invalid index in wxFileHistory::RemoveFileFromHistory") );
2086 // delete the element from the array (could use memmove() too...)
2087 delete [] m_fileHistory
[i
];
2090 for ( j
= i
; j
< m_fileHistoryN
- 1; j
++ )
2092 m_fileHistory
[j
] = m_fileHistory
[j
+ 1];
2095 wxNode
* node
= m_fileMenus
.GetFirst();
2098 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2100 // shuffle filenames up
2102 for ( j
= i
; j
< m_fileHistoryN
- 1; j
++ )
2104 buf
.Printf(s_MRUEntryFormat
, j
+ 1, m_fileHistory
[j
]);
2105 menu
->SetLabel(m_idBase
+ j
, buf
);
2108 node
= node
->GetNext();
2110 // delete the last menu item which is unused now
2111 wxWindowID lastItemId
= m_idBase
+ m_fileHistoryN
- 1;
2112 if (menu
->FindItem(lastItemId
))
2114 menu
->Delete(lastItemId
);
2117 // delete the last separator too if no more files are left
2118 if ( m_fileHistoryN
== 1 )
2120 wxMenuItemList::Node
*node
= menu
->GetMenuItems().GetLast();
2123 wxMenuItem
*menuItem
= node
->GetData();
2124 if ( menuItem
->IsSeparator() )
2126 menu
->Delete(menuItem
);
2128 //else: should we search backwards for the last separator?
2130 //else: menu is empty somehow
2137 wxString
wxFileHistory::GetHistoryFile(size_t i
) const
2140 if ( i
< m_fileHistoryN
)
2142 s
= m_fileHistory
[i
];
2146 wxFAIL_MSG( wxT("bad index in wxFileHistory::GetHistoryFile") );
2152 void wxFileHistory::UseMenu(wxMenu
*menu
)
2154 if (!m_fileMenus
.Member(menu
))
2155 m_fileMenus
.Append(menu
);
2158 void wxFileHistory::RemoveMenu(wxMenu
*menu
)
2160 m_fileMenus
.DeleteObject(menu
);
2164 void wxFileHistory::Load(wxConfigBase
& config
)
2168 buf
.Printf(wxT("file%d"), (int)m_fileHistoryN
+1);
2169 wxString historyFile
;
2170 while ((m_fileHistoryN
< m_fileMaxFiles
) && config
.Read(buf
, &historyFile
) && (historyFile
!= wxT("")))
2172 m_fileHistory
[m_fileHistoryN
] = MYcopystring((const wxChar
*) historyFile
);
2174 buf
.Printf(wxT("file%d"), (int)m_fileHistoryN
+1);
2175 historyFile
= wxT("");
2180 void wxFileHistory::Save(wxConfigBase
& config
)
2183 for (i
= 0; i
< m_fileMaxFiles
; i
++)
2186 buf
.Printf(wxT("file%d"), (int)i
+1);
2187 if (i
< m_fileHistoryN
)
2188 config
.Write(buf
, wxString(m_fileHistory
[i
]));
2190 config
.Write(buf
, wxEmptyString
);
2193 #endif // wxUSE_CONFIG
2195 void wxFileHistory::AddFilesToMenu()
2197 if (m_fileHistoryN
> 0)
2199 wxNode
* node
= m_fileMenus
.GetFirst();
2202 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2203 if (menu
->GetMenuItemCount())
2205 menu
->AppendSeparator();
2209 for (i
= 0; i
< m_fileHistoryN
; i
++)
2211 if (m_fileHistory
[i
])
2214 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
2215 menu
->Append(m_idBase
+i
, buf
);
2218 node
= node
->GetNext();
2223 void wxFileHistory::AddFilesToMenu(wxMenu
* menu
)
2225 if (m_fileHistoryN
> 0)
2227 if (menu
->GetMenuItemCount())
2229 menu
->AppendSeparator();
2233 for (i
= 0; i
< m_fileHistoryN
; i
++)
2235 if (m_fileHistory
[i
])
2238 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
2239 menu
->Append(m_idBase
+i
, buf
);
2245 // ----------------------------------------------------------------------------
2246 // Permits compatibility with existing file formats and functions that
2247 // manipulate files directly
2248 // ----------------------------------------------------------------------------
2250 #if wxUSE_STD_IOSTREAM
2252 bool wxTransferFileToStream(const wxString
& filename
, wxSTD ostream
& stream
)
2254 wxFFile
file(filename
, _T("rb"));
2255 if ( !file
.IsOpened() )
2263 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2267 stream
.write(buf
, nRead
);
2271 while ( !file
.Eof() );
2276 bool wxTransferStreamToFile(wxSTD istream
& stream
, const wxString
& filename
)
2278 wxFFile
file(filename
, _T("wb"));
2279 if ( !file
.IsOpened() )
2285 stream
.read(buf
, WXSIZEOF(buf
));
2286 if ( !stream
.bad() ) // fail may be set on EOF, don't use operator!()
2288 if ( !file
.Write(buf
, stream
.gcount()) )
2292 while ( !stream
.eof() );
2297 #else // !wxUSE_STD_IOSTREAM
2299 bool wxTransferFileToStream(const wxString
& filename
, wxOutputStream
& stream
)
2301 wxFFile
file(filename
, _T("rb"));
2302 if ( !file
.IsOpened() )
2310 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2314 stream
.Write(buf
, nRead
);
2318 while ( !file
.Eof() );
2323 bool wxTransferStreamToFile(wxInputStream
& stream
, const wxString
& filename
)
2325 wxFFile
file(filename
, _T("wb"));
2326 if ( !file
.IsOpened() )
2332 stream
.Read(buf
, WXSIZEOF(buf
));
2334 const size_t nRead
= stream
.LastRead();
2335 if ( !nRead
|| !file
.Write(buf
, nRead
) )
2338 while ( !stream
.Eof() );
2343 #endif // wxUSE_STD_IOSTREAM/!wxUSE_STD_IOSTREAM
2345 #endif // wxUSE_DOC_VIEW_ARCHITECTURE