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 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
190 wxView
*view
= (wxView
*)node
->GetData();
194 wxList::compatibility_iterator 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 wxList::compatibility_iterator 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 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
531 wxView
*view
= (wxView
*)node
->GetData();
533 view
->OnUpdate(sender
, hint
);
534 node
= node
->GetNext();
538 void wxDocument::NotifyClosing()
540 wxList::compatibility_iterator 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 wxList::compatibility_iterator 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 wxList::compatibility_iterator node
= m_docs
.GetFirst();
812 wxDocument
*doc
= (wxDocument
*)node
->GetData();
813 wxList::compatibility_iterator 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 wxList::compatibility_iterator node
= m_templates
.GetFirst();
834 wxDocTemplate
*templ
= (wxDocTemplate
*) node
->GetData();
835 wxList::compatibility_iterator 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
;
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 strings
.Sort(wxStringSortAscending
);
1566 // Yes, this will be slow, but template lists
1567 // are typically short.
1569 n
= strings
.Count();
1570 for (i
= 0; i
< n
; i
++)
1572 for (j
= 0; j
< noTemplates
; j
++)
1574 if (strings
[i
] == templates
[j
]->m_description
)
1575 data
[i
] = templates
[j
];
1580 wxDocTemplate
*theTemplate
;
1585 // no visible templates, hence nothing to choose from
1590 // don't propose the user to choose if he heas no choice
1591 theTemplate
= data
[0];
1595 // propose the user to choose one of several
1596 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1598 _("Select a document template"),
1602 wxFindSuitableParent()
1611 wxDocTemplate
*wxDocManager::SelectViewType(wxDocTemplate
**templates
,
1612 int noTemplates
, bool sort
)
1614 wxArrayString strings
;
1615 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1619 for (i
= 0; i
< noTemplates
; i
++)
1621 wxDocTemplate
*templ
= templates
[i
];
1622 if ( templ
->IsVisible() && !templ
->GetViewName().empty() )
1626 for (j
= 0; j
< n
; j
++)
1628 //filter out NOT unique views
1629 if ( templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
)
1635 strings
.Add(templ
->m_viewTypeName
);
1644 strings
.Sort(wxStringSortAscending
);
1645 // Yes, this will be slow, but template lists
1646 // are typically short.
1648 n
= strings
.Count();
1649 for (i
= 0; i
< n
; i
++)
1651 for (j
= 0; j
< noTemplates
; j
++)
1653 if (strings
[i
] == templates
[j
]->m_viewTypeName
)
1654 data
[i
] = templates
[j
];
1659 wxDocTemplate
*theTemplate
;
1661 // the same logic as above
1665 theTemplate
= (wxDocTemplate
*)NULL
;
1669 theTemplate
= data
[0];
1673 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1675 _("Select a document view"),
1679 wxFindSuitableParent()
1688 void wxDocManager::AssociateTemplate(wxDocTemplate
*temp
)
1690 if (!m_templates
.Member(temp
))
1691 m_templates
.Append(temp
);
1694 void wxDocManager::DisassociateTemplate(wxDocTemplate
*temp
)
1696 m_templates
.DeleteObject(temp
);
1699 // Add and remove a document from the manager's list
1700 void wxDocManager::AddDocument(wxDocument
*doc
)
1702 if (!m_docs
.Member(doc
))
1706 void wxDocManager::RemoveDocument(wxDocument
*doc
)
1708 m_docs
.DeleteObject(doc
);
1711 // Views or windows should inform the document manager
1712 // when a view is going in or out of focus
1713 void wxDocManager::ActivateView(wxView
*view
, bool activate
)
1717 m_currentView
= view
;
1721 if ( m_currentView
== view
)
1723 // don't keep stale pointer
1724 m_currentView
= (wxView
*) NULL
;
1729 // ----------------------------------------------------------------------------
1730 // Default document child frame
1731 // ----------------------------------------------------------------------------
1733 BEGIN_EVENT_TABLE(wxDocChildFrame
, wxFrame
)
1734 EVT_ACTIVATE(wxDocChildFrame::OnActivate
)
1735 EVT_CLOSE(wxDocChildFrame::OnCloseWindow
)
1738 wxDocChildFrame::wxDocChildFrame(wxDocument
*doc
,
1742 const wxString
& title
,
1746 const wxString
& name
)
1747 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1749 m_childDocument
= doc
;
1752 view
->SetFrame(this);
1755 wxDocChildFrame::~wxDocChildFrame()
1759 // Extend event processing to search the view's event table
1760 bool wxDocChildFrame::ProcessEvent(wxEvent
& event
)
1763 m_childView
->Activate(TRUE
);
1765 if ( !m_childView
|| ! m_childView
->ProcessEvent(event
) )
1767 // Only hand up to the parent if it's a menu command
1768 if (!event
.IsKindOf(CLASSINFO(wxCommandEvent
)) || !GetParent() || !GetParent()->ProcessEvent(event
))
1769 return wxEvtHandler::ProcessEvent(event
);
1777 void wxDocChildFrame::OnActivate(wxActivateEvent
& event
)
1779 wxFrame::OnActivate(event
);
1782 m_childView
->Activate(event
.GetActive());
1785 void wxDocChildFrame::OnCloseWindow(wxCloseEvent
& event
)
1790 if (!event
.CanVeto())
1791 ans
= TRUE
; // Must delete.
1793 ans
= m_childView
->Close(FALSE
); // FALSE means don't delete associated window
1797 m_childView
->Activate(FALSE
);
1799 m_childView
= (wxView
*) NULL
;
1800 m_childDocument
= (wxDocument
*) NULL
;
1811 // ----------------------------------------------------------------------------
1812 // Default parent frame
1813 // ----------------------------------------------------------------------------
1815 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1816 EVT_MENU(wxID_EXIT
, wxDocParentFrame::OnExit
)
1817 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE9
, wxDocParentFrame::OnMRUFile
)
1818 EVT_CLOSE(wxDocParentFrame::OnCloseWindow
)
1821 wxDocParentFrame::wxDocParentFrame(wxDocManager
*manager
,
1824 const wxString
& title
,
1828 const wxString
& name
)
1829 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1831 m_docManager
= manager
;
1834 void wxDocParentFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
1839 void wxDocParentFrame::OnMRUFile(wxCommandEvent
& event
)
1841 int n
= event
.GetId() - wxID_FILE1
; // the index in MRU list
1842 wxString
filename(m_docManager
->GetHistoryFile(n
));
1843 if ( !filename
.IsEmpty() )
1845 // verify that the file exists before doing anything else
1846 if ( wxFile::Exists(filename
) )
1849 if (!m_docManager
->CreateDocument(filename
, wxDOC_SILENT
))
1851 // remove the file from the MRU list. The user should already be notified.
1852 m_docManager
->RemoveFileFromHistory(n
);
1854 wxLogError(_("The file '%s' couldn't be opened.\nIt has been removed from the most recently used files list."),
1860 // remove the bogus filename from the MRU list and notify the user
1862 m_docManager
->RemoveFileFromHistory(n
);
1864 wxLogError(_("The file '%s' doesn't exist and couldn't be opened.\nIt has been removed from the most recently used files list."),
1870 // Extend event processing to search the view's event table
1871 bool wxDocParentFrame::ProcessEvent(wxEvent
& event
)
1873 // Try the document manager, then do default processing
1874 if (!m_docManager
|| !m_docManager
->ProcessEvent(event
))
1875 return wxEvtHandler::ProcessEvent(event
);
1880 // Define the behaviour for the frame closing
1881 // - must delete all frames except for the main one.
1882 void wxDocParentFrame::OnCloseWindow(wxCloseEvent
& event
)
1884 if (m_docManager
->Clear(!event
.CanVeto()))
1892 #if wxUSE_PRINTING_ARCHITECTURE
1894 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
)
1897 m_printoutView
= view
;
1900 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
1904 // Get the logical pixels per inch of screen and printer
1905 int ppiScreenX
, ppiScreenY
;
1906 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
1907 int ppiPrinterX
, ppiPrinterY
;
1908 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
1910 // This scales the DC so that the printout roughly represents the
1911 // the screen scaling. The text point size _should_ be the right size
1912 // but in fact is too small for some reason. This is a detail that will
1913 // need to be addressed at some point but can be fudged for the
1915 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
1917 // Now we have to check in case our real page size is reduced
1918 // (e.g. because we're drawing to a print preview memory DC)
1919 int pageWidth
, pageHeight
;
1921 dc
->GetSize(&w
, &h
);
1922 GetPageSizePixels(&pageWidth
, &pageHeight
);
1924 // If printer pageWidth == current DC width, then this doesn't
1925 // change. But w might be the preview bitmap width, so scale down.
1926 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
1927 dc
->SetUserScale(overallScale
, overallScale
);
1931 m_printoutView
->OnDraw(dc
);
1936 bool wxDocPrintout::HasPage(int pageNum
)
1938 return (pageNum
== 1);
1941 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
1943 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
1949 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
1957 #endif // wxUSE_PRINTING_ARCHITECTURE
1959 // ----------------------------------------------------------------------------
1960 // File history processor
1961 // ----------------------------------------------------------------------------
1963 static inline wxChar
* MYcopystring(const wxString
& s
)
1965 wxChar
* copy
= new wxChar
[s
.length() + 1];
1966 return wxStrcpy(copy
, s
.c_str());
1969 static inline wxChar
* MYcopystring(const wxChar
* s
)
1971 wxChar
* copy
= new wxChar
[wxStrlen(s
) + 1];
1972 return wxStrcpy(copy
, s
);
1975 wxFileHistory::wxFileHistory(size_t maxFiles
, wxWindowID idBase
)
1977 m_fileMaxFiles
= maxFiles
;
1980 m_fileHistory
= new wxChar
*[m_fileMaxFiles
];
1983 wxFileHistory::~wxFileHistory()
1986 for (i
= 0; i
< m_fileHistoryN
; i
++)
1987 delete[] m_fileHistory
[i
];
1988 delete[] m_fileHistory
;
1991 // File history management
1992 void wxFileHistory::AddFileToHistory(const wxString
& file
)
1996 // Check we don't already have this file
1997 for (i
= 0; i
< m_fileHistoryN
; i
++)
1999 #if defined( __WXMSW__ ) // Add any other OSes with case insensitive file names
2000 wxString testString
;
2001 if ( m_fileHistory
[i
] )
2002 testString
= m_fileHistory
[i
];
2003 if ( m_fileHistory
[i
] && ( file
.Lower() == testString
.Lower() ) )
2005 if ( m_fileHistory
[i
] && ( file
== m_fileHistory
[i
] ) )
2008 // we do have it, move it to the top of the history
2009 RemoveFileFromHistory (i
);
2010 AddFileToHistory (file
);
2015 // if we already have a full history, delete the one at the end
2016 if ( m_fileMaxFiles
== m_fileHistoryN
)
2018 RemoveFileFromHistory (m_fileHistoryN
- 1);
2019 AddFileToHistory (file
);
2023 // Add to the project file history:
2024 // Move existing files (if any) down so we can insert file at beginning.
2025 if (m_fileHistoryN
< m_fileMaxFiles
)
2027 wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2030 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2031 if ( m_fileHistoryN
== 0 && menu
->GetMenuItemCount() )
2033 menu
->AppendSeparator();
2035 menu
->Append(m_idBase
+m_fileHistoryN
, _("[EMPTY]"));
2036 node
= node
->GetNext();
2040 // Shuffle filenames down
2041 for (i
= (m_fileHistoryN
-1); i
> 0; i
--)
2043 m_fileHistory
[i
] = m_fileHistory
[i
-1];
2045 m_fileHistory
[0] = MYcopystring(file
);
2047 // this is the directory of the last opened file
2048 wxString pathCurrent
;
2049 wxSplitPath( m_fileHistory
[0], &pathCurrent
, NULL
, NULL
);
2050 for (i
= 0; i
< m_fileHistoryN
; i
++)
2052 if ( m_fileHistory
[i
] )
2054 // if in same directory just show the filename; otherwise the full
2056 wxString pathInMenu
, path
, filename
, ext
;
2057 wxSplitPath( m_fileHistory
[i
], &path
, &filename
, &ext
);
2058 if ( path
== pathCurrent
)
2060 pathInMenu
= filename
;
2062 pathInMenu
= pathInMenu
+ wxFILE_SEP_EXT
+ ext
;
2066 // absolute path; could also set relative path
2067 pathInMenu
= m_fileHistory
[i
];
2071 buf
.Printf(s_MRUEntryFormat
, i
+ 1, pathInMenu
.c_str());
2072 wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2075 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2076 menu
->SetLabel(m_idBase
+ i
, buf
);
2077 node
= node
->GetNext();
2083 void wxFileHistory::RemoveFileFromHistory(size_t i
)
2085 wxCHECK_RET( i
< m_fileHistoryN
,
2086 wxT("invalid index in wxFileHistory::RemoveFileFromHistory") );
2088 // delete the element from the array (could use memmove() too...)
2089 delete [] m_fileHistory
[i
];
2092 for ( j
= i
; j
< m_fileHistoryN
- 1; j
++ )
2094 m_fileHistory
[j
] = m_fileHistory
[j
+ 1];
2097 wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2100 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2102 // shuffle filenames up
2104 for ( j
= i
; j
< m_fileHistoryN
- 1; j
++ )
2106 buf
.Printf(s_MRUEntryFormat
, j
+ 1, m_fileHistory
[j
]);
2107 menu
->SetLabel(m_idBase
+ j
, buf
);
2110 node
= node
->GetNext();
2112 // delete the last menu item which is unused now
2113 wxWindowID lastItemId
= m_idBase
+ m_fileHistoryN
- 1;
2114 if (menu
->FindItem(lastItemId
))
2116 menu
->Delete(lastItemId
);
2119 // delete the last separator too if no more files are left
2120 if ( m_fileHistoryN
== 1 )
2122 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetLast();
2125 wxMenuItem
*menuItem
= node
->GetData();
2126 if ( menuItem
->IsSeparator() )
2128 menu
->Delete(menuItem
);
2130 //else: should we search backwards for the last separator?
2132 //else: menu is empty somehow
2139 wxString
wxFileHistory::GetHistoryFile(size_t i
) const
2142 if ( i
< m_fileHistoryN
)
2144 s
= m_fileHistory
[i
];
2148 wxFAIL_MSG( wxT("bad index in wxFileHistory::GetHistoryFile") );
2154 void wxFileHistory::UseMenu(wxMenu
*menu
)
2156 if (!m_fileMenus
.Member(menu
))
2157 m_fileMenus
.Append(menu
);
2160 void wxFileHistory::RemoveMenu(wxMenu
*menu
)
2162 m_fileMenus
.DeleteObject(menu
);
2166 void wxFileHistory::Load(wxConfigBase
& config
)
2170 buf
.Printf(wxT("file%d"), (int)m_fileHistoryN
+1);
2171 wxString historyFile
;
2172 while ((m_fileHistoryN
< m_fileMaxFiles
) && config
.Read(buf
, &historyFile
) && (historyFile
!= wxT("")))
2174 m_fileHistory
[m_fileHistoryN
] = MYcopystring((const wxChar
*) historyFile
);
2176 buf
.Printf(wxT("file%d"), (int)m_fileHistoryN
+1);
2177 historyFile
= wxT("");
2182 void wxFileHistory::Save(wxConfigBase
& config
)
2185 for (i
= 0; i
< m_fileMaxFiles
; i
++)
2188 buf
.Printf(wxT("file%d"), (int)i
+1);
2189 if (i
< m_fileHistoryN
)
2190 config
.Write(buf
, wxString(m_fileHistory
[i
]));
2192 config
.Write(buf
, wxEmptyString
);
2195 #endif // wxUSE_CONFIG
2197 void wxFileHistory::AddFilesToMenu()
2199 if (m_fileHistoryN
> 0)
2201 wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2204 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2205 if (menu
->GetMenuItemCount())
2207 menu
->AppendSeparator();
2211 for (i
= 0; i
< m_fileHistoryN
; i
++)
2213 if (m_fileHistory
[i
])
2216 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
2217 menu
->Append(m_idBase
+i
, buf
);
2220 node
= node
->GetNext();
2225 void wxFileHistory::AddFilesToMenu(wxMenu
* menu
)
2227 if (m_fileHistoryN
> 0)
2229 if (menu
->GetMenuItemCount())
2231 menu
->AppendSeparator();
2235 for (i
= 0; i
< m_fileHistoryN
; i
++)
2237 if (m_fileHistory
[i
])
2240 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
2241 menu
->Append(m_idBase
+i
, buf
);
2247 // ----------------------------------------------------------------------------
2248 // Permits compatibility with existing file formats and functions that
2249 // manipulate files directly
2250 // ----------------------------------------------------------------------------
2252 #if wxUSE_STD_IOSTREAM
2254 bool wxTransferFileToStream(const wxString
& filename
, wxSTD ostream
& stream
)
2256 wxFFile
file(filename
, _T("rb"));
2257 if ( !file
.IsOpened() )
2265 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2269 stream
.write(buf
, nRead
);
2273 while ( !file
.Eof() );
2278 bool wxTransferStreamToFile(wxSTD istream
& stream
, const wxString
& filename
)
2280 wxFFile
file(filename
, _T("wb"));
2281 if ( !file
.IsOpened() )
2287 stream
.read(buf
, WXSIZEOF(buf
));
2288 if ( !stream
.bad() ) // fail may be set on EOF, don't use operator!()
2290 if ( !file
.Write(buf
, stream
.gcount()) )
2294 while ( !stream
.eof() );
2299 #else // !wxUSE_STD_IOSTREAM
2301 bool wxTransferFileToStream(const wxString
& filename
, wxOutputStream
& stream
)
2303 wxFFile
file(filename
, _T("rb"));
2304 if ( !file
.IsOpened() )
2312 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2316 stream
.Write(buf
, nRead
);
2320 while ( !file
.Eof() );
2325 bool wxTransferStreamToFile(wxInputStream
& stream
, const wxString
& filename
)
2327 wxFFile
file(filename
, _T("wb"));
2328 if ( !file
.IsOpened() )
2334 stream
.Read(buf
, WXSIZEOF(buf
));
2336 const size_t nRead
= stream
.LastRead();
2337 if ( !nRead
|| !file
.Write(buf
, nRead
) )
2340 while ( !stream
.Eof() );
2345 #endif // wxUSE_STD_IOSTREAM/!wxUSE_STD_IOSTREAM
2347 #endif // wxUSE_DOC_VIEW_ARCHITECTURE