1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/docview.cpp
3 // Purpose: Document/view classes
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "docview.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
31 #if wxUSE_DOC_VIEW_ARCHITECTURE
34 #include "wx/string.h"
38 #include "wx/dialog.h"
41 #include "wx/filedlg.h"
49 #include "wx/filename.h"
56 #if wxUSE_PRINTING_ARCHITECTURE
57 #include "wx/prntbase.h"
58 #include "wx/printdlg.h"
61 #include "wx/msgdlg.h"
62 #include "wx/choicdlg.h"
63 #include "wx/docview.h"
64 #include "wx/confbase.h"
66 #include "wx/cmdproc.h"
71 #if wxUSE_STD_IOSTREAM
72 #include "wx/ioswrap.h"
79 #include "wx/wfstream.h"
82 // ----------------------------------------------------------------------------
84 // ----------------------------------------------------------------------------
86 IMPLEMENT_ABSTRACT_CLASS(wxDocument
, wxEvtHandler
)
87 IMPLEMENT_ABSTRACT_CLASS(wxView
, wxEvtHandler
)
88 IMPLEMENT_ABSTRACT_CLASS(wxDocTemplate
, wxObject
)
89 IMPLEMENT_DYNAMIC_CLASS(wxDocManager
, wxEvtHandler
)
90 IMPLEMENT_CLASS(wxDocChildFrame
, wxFrame
)
91 IMPLEMENT_CLASS(wxDocParentFrame
, wxFrame
)
93 #if wxUSE_PRINTING_ARCHITECTURE
94 IMPLEMENT_DYNAMIC_CLASS(wxDocPrintout
, wxPrintout
)
97 IMPLEMENT_DYNAMIC_CLASS(wxFileHistory
, wxObject
)
99 // ----------------------------------------------------------------------------
100 // function prototypes
101 // ----------------------------------------------------------------------------
103 static inline wxString
FindExtension(const wxChar
*path
);
104 static wxWindow
* wxFindSuitableParent(void);
106 // ----------------------------------------------------------------------------
108 // ----------------------------------------------------------------------------
110 static const wxChar
*s_MRUEntryFormat
= wxT("&%d %s");
112 // ============================================================================
114 // ============================================================================
116 // ----------------------------------------------------------------------------
118 // ----------------------------------------------------------------------------
120 static wxString
FindExtension(const wxChar
*path
)
123 wxSplitPath(path
, NULL
, NULL
, &ext
);
125 // VZ: extensions are considered not case sensitive - is this really a good
127 return ext
.MakeLower();
130 // ----------------------------------------------------------------------------
131 // Definition of wxDocument
132 // ----------------------------------------------------------------------------
134 wxDocument::wxDocument(wxDocument
*parent
)
136 m_documentModified
= FALSE
;
137 m_documentParent
= parent
;
138 m_documentTemplate
= (wxDocTemplate
*) NULL
;
139 m_commandProcessor
= (wxCommandProcessor
*) NULL
;
143 bool wxDocument::DeleteContents()
148 wxDocument::~wxDocument()
152 if (m_commandProcessor
)
153 delete m_commandProcessor
;
155 if (GetDocumentManager())
156 GetDocumentManager()->RemoveDocument(this);
158 // Not safe to do here, since it'll invoke virtual view functions
159 // expecting to see valid derived objects: and by the time we get here,
160 // we've called destructors higher up.
164 bool wxDocument::Close()
166 if (OnSaveModified())
167 return OnCloseDocument();
172 bool wxDocument::OnCloseDocument()
174 // Tell all views that we're about to close
181 // Note that this implicitly deletes the document when the last view is
183 bool wxDocument::DeleteAllViews()
185 wxDocManager
* manager
= GetDocumentManager();
186 wxList::iterator it
, en
;
188 for ( it
= m_documentViews
.begin(), en
= m_documentViews
.end();
192 wxView
*view
= (wxView
*)*it
;
196 wxList::iterator next
= it
; ++next
;
198 delete view
; // Deletes node implicitly
201 // If we haven't yet deleted the document (for example
202 // if there were no views) then delete it.
203 if (manager
&& manager
->GetDocuments().Member(this))
209 wxView
*wxDocument::GetFirstView() const
211 if (m_documentViews
.GetCount() == 0)
212 return (wxView
*) NULL
;
213 return (wxView
*)m_documentViews
.GetFirst()->GetData();
216 wxDocManager
*wxDocument::GetDocumentManager() const
218 return (m_documentTemplate
? m_documentTemplate
->GetDocumentManager() : (wxDocManager
*) NULL
);
221 bool wxDocument::OnNewDocument()
223 if (!OnSaveModified())
226 if (OnCloseDocument()==FALSE
) return FALSE
;
229 SetDocumentSaved(FALSE
);
232 GetDocumentManager()->MakeDefaultName(name
);
234 SetFilename(name
, TRUE
);
239 bool wxDocument::Save()
241 if (!IsModified() && m_savedYet
)
244 if ( m_documentFile
.empty() || !m_savedYet
)
247 return OnSaveDocument(m_documentFile
);
250 bool wxDocument::SaveAs()
252 wxDocTemplate
*docTemplate
= GetDocumentTemplate();
256 wxString tmp
= wxFileSelector(_("Save as"),
257 docTemplate
->GetDirectory(),
258 wxFileNameFromPath(GetFilename()),
259 docTemplate
->GetDefaultExtension(),
260 docTemplate
->GetFileFilter(),
261 wxSAVE
| wxOVERWRITE_PROMPT
,
262 GetDocumentWindow());
267 wxString
fileName(tmp
);
268 wxString path
, name
, ext
;
269 wxSplitPath(fileName
, & path
, & name
, & ext
);
271 if (ext
.IsEmpty() || ext
== wxT(""))
273 fileName
+= wxT(".");
274 fileName
+= docTemplate
->GetDefaultExtension();
277 SetFilename(fileName
);
278 SetTitle(wxFileNameFromPath(fileName
));
280 // Notify the views that the filename has changed
281 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
284 wxView
*view
= (wxView
*)node
->GetData();
285 view
->OnChangeFilename();
286 node
= node
->GetNext();
289 // Files that were not saved correctly are not added to the FileHistory.
290 if (!OnSaveDocument(m_documentFile
))
293 // A file that doesn't use the default extension of its document template cannot be opened
294 // via the FileHistory, so we do not add it.
295 if (docTemplate
->FileMatchesTemplate(fileName
))
297 GetDocumentManager()->AddFileToHistory(fileName
);
301 // The user will probably not be able to open the file again, so
302 // we could warn about the wrong file-extension here.
307 bool wxDocument::OnSaveDocument(const wxString
& file
)
313 if (wxTheApp
->GetAppName() != wxT(""))
314 msgTitle
= wxTheApp
->GetAppName();
316 msgTitle
= wxString(_("File error"));
318 #if wxUSE_STD_IOSTREAM
319 wxSTD ofstream
store(file
.mb_str());
320 if (store
.fail() || store
.bad())
322 wxFileOutputStream
store(file
);
323 if (store
.GetLastError() != wxSTREAM_NO_ERROR
)
326 (void)wxMessageBox(_("Sorry, could not open this file for saving."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
327 GetDocumentWindow());
331 if (!SaveObject(store
))
333 (void)wxMessageBox(_("Sorry, could not save this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
334 GetDocumentWindow());
340 SetDocumentSaved(TRUE
);
342 wxFileName
fn(file
) ;
343 fn
.MacSetDefaultTypeAndCreator() ;
348 bool wxDocument::OnOpenDocument(const wxString
& file
)
350 if (!OnSaveModified())
354 if (wxTheApp
->GetAppName() != wxT(""))
355 msgTitle
= wxTheApp
->GetAppName();
357 msgTitle
= wxString(_("File error"));
359 #if wxUSE_STD_IOSTREAM
360 wxSTD ifstream
store(file
.mb_str());
361 if (store
.fail() || store
.bad())
363 wxFileInputStream
store(file
);
364 if (store
.GetLastError() != wxSTREAM_NO_ERROR
)
367 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
|wxICON_EXCLAMATION
,
368 GetDocumentWindow());
371 #if wxUSE_STD_IOSTREAM
373 if ( !store
&& !store
.eof() )
375 int res
= LoadObject(store
).GetLastError();
376 if ((res
!= wxSTREAM_NO_ERROR
) &&
377 (res
!= wxSTREAM_EOF
))
380 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
|wxICON_EXCLAMATION
,
381 GetDocumentWindow());
384 SetFilename(file
, TRUE
);
393 #if wxUSE_STD_IOSTREAM
394 wxSTD istream
& wxDocument::LoadObject(wxSTD istream
& stream
)
396 wxInputStream
& wxDocument::LoadObject(wxInputStream
& stream
)
402 #if wxUSE_STD_IOSTREAM
403 wxSTD ostream
& wxDocument::SaveObject(wxSTD ostream
& stream
)
405 wxOutputStream
& wxDocument::SaveObject(wxOutputStream
& stream
)
411 bool wxDocument::Revert()
417 // Get title, or filename if no title, else unnamed
418 bool wxDocument::GetPrintableName(wxString
& buf
) const
420 if (m_documentTitle
!= wxT(""))
422 buf
= m_documentTitle
;
425 else if (m_documentFile
!= wxT(""))
427 buf
= wxFileNameFromPath(m_documentFile
);
437 wxWindow
*wxDocument::GetDocumentWindow() const
439 wxView
*view
= GetFirstView();
441 return view
->GetFrame();
443 return wxTheApp
->GetTopWindow();
446 wxCommandProcessor
*wxDocument::OnCreateCommandProcessor()
448 return new wxCommandProcessor
;
451 // TRUE if safe to close
452 bool wxDocument::OnSaveModified()
457 GetPrintableName(title
);
460 if (wxTheApp
->GetAppName() != wxT(""))
461 msgTitle
= wxTheApp
->GetAppName();
463 msgTitle
= wxString(_("Warning"));
466 prompt
.Printf(_("Do you want to save changes to document %s?"),
467 (const wxChar
*)title
);
468 int res
= wxMessageBox(prompt
, msgTitle
,
469 wxYES_NO
|wxCANCEL
|wxICON_QUESTION
,
470 GetDocumentWindow());
476 else if (res
== wxYES
)
478 else if (res
== wxCANCEL
)
484 bool wxDocument::Draw(wxDC
& WXUNUSED(context
))
489 bool wxDocument::AddView(wxView
*view
)
491 if (!m_documentViews
.Member(view
))
493 m_documentViews
.Append(view
);
499 bool wxDocument::RemoveView(wxView
*view
)
501 (void)m_documentViews
.DeleteObject(view
);
506 bool wxDocument::OnCreate(const wxString
& WXUNUSED(path
), long flags
)
508 if (GetDocumentTemplate()->CreateView(this, flags
))
514 // Called after a view is added or removed.
515 // The default implementation deletes the document if
516 // there are no more views.
517 void wxDocument::OnChangedViewList()
519 if (m_documentViews
.GetCount() == 0)
521 if (OnSaveModified())
528 void wxDocument::UpdateAllViews(wxView
*sender
, wxObject
*hint
)
530 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
533 wxView
*view
= (wxView
*)node
->GetData();
535 view
->OnUpdate(sender
, hint
);
536 node
= node
->GetNext();
540 void wxDocument::NotifyClosing()
542 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
545 wxView
*view
= (wxView
*)node
->GetData();
546 view
->OnClosingDocument();
547 node
= node
->GetNext();
551 void wxDocument::SetFilename(const wxString
& filename
, bool notifyViews
)
553 m_documentFile
= filename
;
556 // Notify the views that the filename has changed
557 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
560 wxView
*view
= (wxView
*)node
->GetData();
561 view
->OnChangeFilename();
562 node
= node
->GetNext();
567 // ----------------------------------------------------------------------------
569 // ----------------------------------------------------------------------------
573 m_viewDocument
= (wxDocument
*) NULL
;
575 m_viewFrame
= (wxFrame
*) NULL
;
580 GetDocumentManager()->ActivateView(this, FALSE
);
581 m_viewDocument
->RemoveView(this);
584 // Extend event processing to search the document's event table
585 bool wxView::ProcessEvent(wxEvent
& event
)
587 if ( !GetDocument() || !GetDocument()->ProcessEvent(event
) )
588 return wxEvtHandler::ProcessEvent(event
);
593 void wxView::OnActivateView(bool WXUNUSED(activate
), wxView
*WXUNUSED(activeView
), wxView
*WXUNUSED(deactiveView
))
597 void wxView::OnPrint(wxDC
*dc
, wxObject
*WXUNUSED(info
))
602 void wxView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
))
606 void wxView::OnChangeFilename()
608 if (GetFrame() && GetDocument())
612 GetDocument()->GetPrintableName(title
);
614 GetFrame()->SetTitle(title
);
618 void wxView::SetDocument(wxDocument
*doc
)
620 m_viewDocument
= doc
;
625 bool wxView::Close(bool deleteWindow
)
627 if (OnClose(deleteWindow
))
633 void wxView::Activate(bool activate
)
635 if (GetDocument() && GetDocumentManager())
637 OnActivateView(activate
, this, GetDocumentManager()->GetCurrentView());
638 GetDocumentManager()->ActivateView(this, activate
);
642 bool wxView::OnClose(bool WXUNUSED(deleteWindow
))
644 return GetDocument() ? GetDocument()->Close() : TRUE
;
647 #if wxUSE_PRINTING_ARCHITECTURE
648 wxPrintout
*wxView::OnCreatePrintout()
650 return new wxDocPrintout(this);
652 #endif // wxUSE_PRINTING_ARCHITECTURE
654 // ----------------------------------------------------------------------------
656 // ----------------------------------------------------------------------------
658 wxDocTemplate::wxDocTemplate(wxDocManager
*manager
,
659 const wxString
& descr
,
660 const wxString
& filter
,
663 const wxString
& docTypeName
,
664 const wxString
& viewTypeName
,
665 wxClassInfo
*docClassInfo
,
666 wxClassInfo
*viewClassInfo
,
669 m_documentManager
= manager
;
670 m_description
= descr
;
673 m_fileFilter
= filter
;
675 m_docTypeName
= docTypeName
;
676 m_viewTypeName
= viewTypeName
;
677 m_documentManager
->AssociateTemplate(this);
679 m_docClassInfo
= docClassInfo
;
680 m_viewClassInfo
= viewClassInfo
;
683 wxDocTemplate::~wxDocTemplate()
685 m_documentManager
->DisassociateTemplate(this);
688 // Tries to dynamically construct an object of the right class.
689 wxDocument
*wxDocTemplate::CreateDocument(const wxString
& path
, long flags
)
692 return (wxDocument
*) NULL
;
693 wxDocument
*doc
= (wxDocument
*)m_docClassInfo
->CreateObject();
694 doc
->SetFilename(path
);
695 doc
->SetDocumentTemplate(this);
696 GetDocumentManager()->AddDocument(doc
);
697 doc
->SetCommandProcessor(doc
->OnCreateCommandProcessor());
699 if (doc
->OnCreate(path
, flags
))
703 if (GetDocumentManager()->GetDocuments().Member(doc
))
704 doc
->DeleteAllViews();
705 return (wxDocument
*) NULL
;
709 wxView
*wxDocTemplate::CreateView(wxDocument
*doc
, long flags
)
711 if (!m_viewClassInfo
)
712 return (wxView
*) NULL
;
713 wxView
*view
= (wxView
*)m_viewClassInfo
->CreateObject();
714 view
->SetDocument(doc
);
715 if (view
->OnCreate(doc
, flags
))
722 return (wxView
*) NULL
;
726 // The default (very primitive) format detection: check is the extension is
727 // that of the template
728 bool wxDocTemplate::FileMatchesTemplate(const wxString
& path
)
730 return GetDefaultExtension().IsSameAs(FindExtension(path
));
733 // ----------------------------------------------------------------------------
735 // ----------------------------------------------------------------------------
737 BEGIN_EVENT_TABLE(wxDocManager
, wxEvtHandler
)
738 EVT_MENU(wxID_OPEN
, wxDocManager::OnFileOpen
)
739 EVT_MENU(wxID_CLOSE
, wxDocManager::OnFileClose
)
740 EVT_MENU(wxID_CLOSE_ALL
, wxDocManager::OnFileCloseAll
)
741 EVT_MENU(wxID_REVERT
, wxDocManager::OnFileRevert
)
742 EVT_MENU(wxID_NEW
, wxDocManager::OnFileNew
)
743 EVT_MENU(wxID_SAVE
, wxDocManager::OnFileSave
)
744 EVT_MENU(wxID_SAVEAS
, wxDocManager::OnFileSaveAs
)
745 EVT_MENU(wxID_UNDO
, wxDocManager::OnUndo
)
746 EVT_MENU(wxID_REDO
, wxDocManager::OnRedo
)
748 EVT_UPDATE_UI(wxID_OPEN
, wxDocManager::OnUpdateFileOpen
)
749 EVT_UPDATE_UI(wxID_CLOSE
, wxDocManager::OnUpdateFileClose
)
750 EVT_UPDATE_UI(wxID_CLOSE_ALL
, wxDocManager::OnUpdateFileClose
)
751 EVT_UPDATE_UI(wxID_REVERT
, wxDocManager::OnUpdateFileRevert
)
752 EVT_UPDATE_UI(wxID_NEW
, wxDocManager::OnUpdateFileNew
)
753 EVT_UPDATE_UI(wxID_SAVE
, wxDocManager::OnUpdateFileSave
)
754 EVT_UPDATE_UI(wxID_SAVEAS
, wxDocManager::OnUpdateFileSaveAs
)
755 EVT_UPDATE_UI(wxID_UNDO
, wxDocManager::OnUpdateUndo
)
756 EVT_UPDATE_UI(wxID_REDO
, wxDocManager::OnUpdateRedo
)
758 #if wxUSE_PRINTING_ARCHITECTURE
759 EVT_MENU(wxID_PRINT
, wxDocManager::OnPrint
)
760 EVT_MENU(wxID_PRINT_SETUP
, wxDocManager::OnPrintSetup
)
761 EVT_MENU(wxID_PREVIEW
, wxDocManager::OnPreview
)
763 EVT_UPDATE_UI(wxID_PRINT
, wxDocManager::OnUpdatePrint
)
764 EVT_UPDATE_UI(wxID_PRINT_SETUP
, wxDocManager::OnUpdatePrintSetup
)
765 EVT_UPDATE_UI(wxID_PREVIEW
, wxDocManager::OnUpdatePreview
)
769 wxDocManager
* wxDocManager::sm_docManager
= (wxDocManager
*) NULL
;
771 wxDocManager::wxDocManager(long flags
, bool initialize
)
773 m_defaultDocumentNameCounter
= 1;
775 m_currentView
= (wxView
*) NULL
;
776 m_maxDocsOpen
= 10000;
777 m_fileHistory
= (wxFileHistory
*) NULL
;
780 sm_docManager
= this;
783 wxDocManager::~wxDocManager()
787 delete m_fileHistory
;
788 sm_docManager
= (wxDocManager
*) NULL
;
791 // closes the specified document
792 bool wxDocManager::CloseDocument(wxDocument
* doc
, bool force
)
794 if (doc
->Close() || force
)
796 // Implicitly deletes the document when
797 // the last view is deleted
798 doc
->DeleteAllViews();
800 // Check we're really deleted
801 if (m_docs
.Member(doc
))
809 bool wxDocManager::CloseDocuments(bool force
)
811 wxList::compatibility_iterator node
= m_docs
.GetFirst();
814 wxDocument
*doc
= (wxDocument
*)node
->GetData();
815 wxList::compatibility_iterator next
= node
->GetNext();
817 if (!CloseDocument(doc
, force
))
820 // This assumes that documents are not connected in
821 // any way, i.e. deleting one document does NOT
828 bool wxDocManager::Clear(bool force
)
830 if (!CloseDocuments(force
))
833 wxList::compatibility_iterator node
= m_templates
.GetFirst();
836 wxDocTemplate
*templ
= (wxDocTemplate
*) node
->GetData();
837 wxList::compatibility_iterator next
= node
->GetNext();
844 bool wxDocManager::Initialize()
846 m_fileHistory
= OnCreateFileHistory();
850 wxFileHistory
*wxDocManager::OnCreateFileHistory()
852 return new wxFileHistory
;
855 void wxDocManager::OnFileClose(wxCommandEvent
& WXUNUSED(event
))
857 wxDocument
*doc
= GetCurrentDocument();
862 doc
->DeleteAllViews();
863 if (m_docs
.Member(doc
))
868 void wxDocManager::OnFileCloseAll(wxCommandEvent
& WXUNUSED(event
))
870 CloseDocuments(FALSE
);
873 void wxDocManager::OnFileNew(wxCommandEvent
& WXUNUSED(event
))
875 CreateDocument( wxT(""), wxDOC_NEW
);
878 void wxDocManager::OnFileOpen(wxCommandEvent
& WXUNUSED(event
))
880 if ( !CreateDocument( wxT(""), 0) )
886 void wxDocManager::OnFileRevert(wxCommandEvent
& WXUNUSED(event
))
888 wxDocument
*doc
= GetCurrentDocument();
894 void wxDocManager::OnFileSave(wxCommandEvent
& WXUNUSED(event
))
896 wxDocument
*doc
= GetCurrentDocument();
902 void wxDocManager::OnFileSaveAs(wxCommandEvent
& WXUNUSED(event
))
904 wxDocument
*doc
= GetCurrentDocument();
910 void wxDocManager::OnPrint(wxCommandEvent
& WXUNUSED(event
))
912 #if wxUSE_PRINTING_ARCHITECTURE
913 wxView
*view
= GetCurrentView();
917 wxPrintout
*printout
= view
->OnCreatePrintout();
921 printer
.Print(view
->GetFrame(), printout
, TRUE
);
925 #endif // wxUSE_PRINTING_ARCHITECTURE
928 void wxDocManager::OnPrintSetup(wxCommandEvent
& WXUNUSED(event
))
930 #if wxUSE_PRINTING_ARCHITECTURE
931 wxWindow
*parentWin
= wxTheApp
->GetTopWindow();
932 wxView
*view
= GetCurrentView();
934 parentWin
= view
->GetFrame();
936 wxPrintDialogData data
;
938 wxPrintDialog
printerDialog(parentWin
, &data
);
939 printerDialog
.GetPrintDialogData().SetSetupDialog(TRUE
);
940 printerDialog
.ShowModal();
941 #endif // wxUSE_PRINTING_ARCHITECTURE
944 void wxDocManager::OnPreview(wxCommandEvent
& WXUNUSED(event
))
946 #if wxUSE_PRINTING_ARCHITECTURE
947 wxView
*view
= GetCurrentView();
951 wxPrintout
*printout
= view
->OnCreatePrintout();
954 // Pass two printout objects: for preview, and possible printing.
955 wxPrintPreviewBase
*preview
= new wxPrintPreview(printout
, view
->OnCreatePrintout());
956 if ( !preview
->Ok() )
959 wxMessageBox( _("Sorry, print preview needs a printer to be installed.") );
963 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, (wxFrame
*)wxTheApp
->GetTopWindow(), _("Print Preview"),
964 wxPoint(100, 100), wxSize(600, 650));
965 frame
->Centre(wxBOTH
);
969 #endif // wxUSE_PRINTING_ARCHITECTURE
972 void wxDocManager::OnUndo(wxCommandEvent
& event
)
974 wxDocument
*doc
= GetCurrentDocument();
977 if (doc
->GetCommandProcessor())
978 doc
->GetCommandProcessor()->Undo();
983 void wxDocManager::OnRedo(wxCommandEvent
& event
)
985 wxDocument
*doc
= GetCurrentDocument();
988 if (doc
->GetCommandProcessor())
989 doc
->GetCommandProcessor()->Redo();
994 // Handlers for UI update commands
996 void wxDocManager::OnUpdateFileOpen(wxUpdateUIEvent
& event
)
998 event
.Enable( TRUE
);
1001 void wxDocManager::OnUpdateFileClose(wxUpdateUIEvent
& event
)
1003 wxDocument
*doc
= GetCurrentDocument();
1004 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1007 void wxDocManager::OnUpdateFileRevert(wxUpdateUIEvent
& event
)
1009 wxDocument
*doc
= GetCurrentDocument();
1010 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1013 void wxDocManager::OnUpdateFileNew(wxUpdateUIEvent
& event
)
1015 event
.Enable( TRUE
);
1018 void wxDocManager::OnUpdateFileSave(wxUpdateUIEvent
& event
)
1020 wxDocument
*doc
= GetCurrentDocument();
1021 event
.Enable( doc
&& doc
->IsModified() );
1024 void wxDocManager::OnUpdateFileSaveAs(wxUpdateUIEvent
& event
)
1026 wxDocument
*doc
= GetCurrentDocument();
1027 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1030 void wxDocManager::OnUpdateUndo(wxUpdateUIEvent
& event
)
1032 wxDocument
*doc
= GetCurrentDocument();
1034 event
.Enable(FALSE
);
1035 else if (!doc
->GetCommandProcessor())
1039 event
.Enable( doc
->GetCommandProcessor()->CanUndo() );
1040 doc
->GetCommandProcessor()->SetMenuStrings();
1044 void wxDocManager::OnUpdateRedo(wxUpdateUIEvent
& event
)
1046 wxDocument
*doc
= GetCurrentDocument();
1048 event
.Enable(FALSE
);
1049 else if (!doc
->GetCommandProcessor())
1053 event
.Enable( doc
->GetCommandProcessor()->CanRedo() );
1054 doc
->GetCommandProcessor()->SetMenuStrings();
1058 void wxDocManager::OnUpdatePrint(wxUpdateUIEvent
& event
)
1060 wxDocument
*doc
= GetCurrentDocument();
1061 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1064 void wxDocManager::OnUpdatePrintSetup(wxUpdateUIEvent
& event
)
1066 event
.Enable( TRUE
);
1069 void wxDocManager::OnUpdatePreview(wxUpdateUIEvent
& event
)
1071 wxDocument
*doc
= GetCurrentDocument();
1072 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1075 wxView
*wxDocManager::GetCurrentView() const
1078 return m_currentView
;
1079 if (m_docs
.GetCount() == 1)
1081 wxDocument
* doc
= (wxDocument
*) m_docs
.GetFirst()->GetData();
1082 return doc
->GetFirstView();
1084 return (wxView
*) NULL
;
1087 // Extend event processing to search the view's event table
1088 bool wxDocManager::ProcessEvent(wxEvent
& event
)
1090 wxView
* view
= GetCurrentView();
1093 if (view
->ProcessEvent(event
))
1096 return wxEvtHandler::ProcessEvent(event
);
1099 wxDocument
*wxDocManager::CreateDocument(const wxString
& path
, long flags
)
1101 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.GetCount()];
1104 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1106 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Item(i
)->GetData());
1107 if (temp
->IsVisible())
1109 templates
[n
] = temp
;
1116 return (wxDocument
*) NULL
;
1119 wxDocument
* docToClose
= NULL
;
1121 // If we've reached the max number of docs, close the
1123 if ( (int)GetDocuments().GetCount() >= m_maxDocsOpen
)
1125 wxDocument
*doc
= (wxDocument
*)GetDocuments().GetFirst()->GetData();
1129 // New document: user chooses a template, unless there's only one.
1130 if (flags
& wxDOC_NEW
)
1136 if (!CloseDocument(docToClose
, FALSE
))
1143 wxDocTemplate
*temp
= templates
[0];
1145 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
1149 newDoc
->SetDocumentName(temp
->GetDocumentName());
1150 newDoc
->SetDocumentTemplate(temp
);
1151 newDoc
->OnNewDocument();
1156 wxDocTemplate
*temp
= SelectDocumentType(templates
, n
);
1162 if (!CloseDocument(docToClose
, FALSE
))
1168 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
1172 newDoc
->SetDocumentName(temp
->GetDocumentName());
1173 newDoc
->SetDocumentTemplate(temp
);
1174 newDoc
->OnNewDocument();
1179 return (wxDocument
*) NULL
;
1182 // Existing document
1183 wxDocTemplate
*temp
;
1185 wxString
path2(wxT(""));
1186 if (path
!= wxT(""))
1189 if (flags
& wxDOC_SILENT
)
1191 temp
= FindTemplateForPath(path2
);
1194 // Since we do not add files with non-default extensions to the FileHistory this
1195 // can only happen if the application changes the allowed templates in runtime.
1196 (void)wxMessageBox(_("Sorry, the format for this file is unknown."),
1198 wxOK
| wxICON_EXCLAMATION
, wxFindSuitableParent());
1202 temp
= SelectDocumentPath(templates
, n
, path2
, flags
);
1210 if (!CloseDocument(docToClose
, FALSE
))
1216 wxDocument
*newDoc
= temp
->CreateDocument(path2
, flags
);
1219 newDoc
->SetDocumentName(temp
->GetDocumentName());
1220 newDoc
->SetDocumentTemplate(temp
);
1221 if (!newDoc
->OnOpenDocument(path2
))
1223 newDoc
->DeleteAllViews();
1224 // delete newDoc; // Implicitly deleted by DeleteAllViews
1225 return (wxDocument
*) NULL
;
1227 // A file that doesn't use the default extension of its document
1228 // template cannot be opened via the FileHistory, so we do not
1230 if (temp
->FileMatchesTemplate(path2
))
1231 AddFileToHistory(path2
);
1236 return (wxDocument
*) NULL
;
1239 wxView
*wxDocManager::CreateView(wxDocument
*doc
, long flags
)
1241 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.GetCount()];
1244 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1246 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Item(i
)->GetData());
1247 if (temp
->IsVisible())
1249 if (temp
->GetDocumentName() == doc
->GetDocumentName())
1251 templates
[n
] = temp
;
1259 return (wxView
*) NULL
;
1263 wxDocTemplate
*temp
= templates
[0];
1265 wxView
*view
= temp
->CreateView(doc
, flags
);
1267 view
->SetViewName(temp
->GetViewName());
1271 wxDocTemplate
*temp
= SelectViewType(templates
, n
);
1275 wxView
*view
= temp
->CreateView(doc
, flags
);
1277 view
->SetViewName(temp
->GetViewName());
1281 return (wxView
*) NULL
;
1284 // Not yet implemented
1285 void wxDocManager::DeleteTemplate(wxDocTemplate
*WXUNUSED(temp
), long WXUNUSED(flags
))
1289 // Not yet implemented
1290 bool wxDocManager::FlushDoc(wxDocument
*WXUNUSED(doc
))
1295 wxDocument
*wxDocManager::GetCurrentDocument() const
1297 wxView
*view
= GetCurrentView();
1299 return view
->GetDocument();
1301 return (wxDocument
*) NULL
;
1304 // Make a default document name
1305 bool wxDocManager::MakeDefaultName(wxString
& name
)
1307 name
.Printf(_("unnamed%d"), m_defaultDocumentNameCounter
);
1308 m_defaultDocumentNameCounter
++;
1313 // Make a frame title (override this to do something different)
1314 // If docName is empty, a document is not currently active.
1315 wxString
wxDocManager::MakeFrameTitle(wxDocument
* doc
)
1317 wxString appName
= wxTheApp
->GetAppName();
1324 doc
->GetPrintableName(docName
);
1325 title
= docName
+ wxString(_(" - ")) + appName
;
1331 // Not yet implemented
1332 wxDocTemplate
*wxDocManager::MatchTemplate(const wxString
& WXUNUSED(path
))
1334 return (wxDocTemplate
*) NULL
;
1337 // File history management
1338 void wxDocManager::AddFileToHistory(const wxString
& file
)
1341 m_fileHistory
->AddFileToHistory(file
);
1344 void wxDocManager::RemoveFileFromHistory(size_t i
)
1347 m_fileHistory
->RemoveFileFromHistory(i
);
1350 wxString
wxDocManager::GetHistoryFile(size_t i
) const
1355 histFile
= m_fileHistory
->GetHistoryFile(i
);
1360 void wxDocManager::FileHistoryUseMenu(wxMenu
*menu
)
1363 m_fileHistory
->UseMenu(menu
);
1366 void wxDocManager::FileHistoryRemoveMenu(wxMenu
*menu
)
1369 m_fileHistory
->RemoveMenu(menu
);
1373 void wxDocManager::FileHistoryLoad(wxConfigBase
& config
)
1376 m_fileHistory
->Load(config
);
1379 void wxDocManager::FileHistorySave(wxConfigBase
& config
)
1382 m_fileHistory
->Save(config
);
1386 void wxDocManager::FileHistoryAddFilesToMenu(wxMenu
* menu
)
1389 m_fileHistory
->AddFilesToMenu(menu
);
1392 void wxDocManager::FileHistoryAddFilesToMenu()
1395 m_fileHistory
->AddFilesToMenu();
1398 size_t wxDocManager::GetHistoryFilesCount() const
1400 return m_fileHistory
? m_fileHistory
->GetCount() : 0;
1404 // Find out the document template via matching in the document file format
1405 // against that of the template
1406 wxDocTemplate
*wxDocManager::FindTemplateForPath(const wxString
& path
)
1408 wxDocTemplate
*theTemplate
= (wxDocTemplate
*) NULL
;
1410 // Find the template which this extension corresponds to
1411 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1413 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Item(i
)->GetData();
1414 if ( temp
->FileMatchesTemplate(path
) )
1423 // Try to get a more suitable parent frame than the top window,
1424 // for selection dialogs. Otherwise you may get an unexpected
1425 // window being activated when a dialog is shown.
1426 static wxWindow
* wxFindSuitableParent()
1428 wxWindow
* parent
= wxTheApp
->GetTopWindow();
1430 wxWindow
* focusWindow
= wxWindow::FindFocus();
1433 while (focusWindow
&&
1434 !focusWindow
->IsKindOf(CLASSINFO(wxDialog
)) &&
1435 !focusWindow
->IsKindOf(CLASSINFO(wxFrame
)))
1437 focusWindow
= focusWindow
->GetParent();
1440 parent
= focusWindow
;
1445 // Prompts user to open a file, using file specs in templates.
1446 // Must extend the file selector dialog or implement own; OR
1447 // match the extension to the template extension.
1449 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**templates
,
1450 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
1453 int WXUNUSED(noTemplates
),
1456 long WXUNUSED(flags
),
1457 bool WXUNUSED(save
))
1459 // We can only have multiple filters in Windows and GTK
1460 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
1464 for (i
= 0; i
< noTemplates
; i
++)
1466 if (templates
[i
]->IsVisible())
1468 // add a '|' to separate this filter from the previous one
1469 if ( !descrBuf
.IsEmpty() )
1470 descrBuf
<< wxT('|');
1472 descrBuf
<< templates
[i
]->GetDescription()
1473 << wxT(" (") << templates
[i
]->GetFileFilter() << wxT(") |")
1474 << templates
[i
]->GetFileFilter();
1478 wxString descrBuf
= wxT("*.*");
1481 int FilterIndex
= -1;
1483 wxWindow
* parent
= wxFindSuitableParent();
1485 wxString pathTmp
= wxFileSelectorEx(_("Select a file"),
1493 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)NULL
;
1494 if (!pathTmp
.IsEmpty())
1496 if (!wxFileExists(pathTmp
))
1499 if (!wxTheApp
->GetAppName().IsEmpty())
1500 msgTitle
= wxTheApp
->GetAppName();
1502 msgTitle
= wxString(_("File error"));
1504 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
1508 return (wxDocTemplate
*) NULL
;
1510 m_lastDirectory
= wxPathOnly(pathTmp
);
1514 // first choose the template using the extension, if this fails (i.e.
1515 // wxFileSelectorEx() didn't fill it), then use the path
1516 if ( FilterIndex
!= -1 )
1517 theTemplate
= templates
[FilterIndex
];
1519 theTemplate
= FindTemplateForPath(path
);
1522 // Since we do not add files with non-default extensions to the FileHistory this
1523 // can only happen if the application changes the allowed templates in runtime.
1524 (void)wxMessageBox(_("Sorry, the format for this file is unknown."),
1526 wxOK
| wxICON_EXCLAMATION
, wxFindSuitableParent());
1537 wxDocTemplate
*wxDocManager::SelectDocumentType(wxDocTemplate
**templates
,
1538 int noTemplates
, bool sort
)
1540 wxArrayString strings
;
1541 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1545 for (i
= 0; i
< noTemplates
; i
++)
1547 if (templates
[i
]->IsVisible())
1551 for (j
= 0; j
< n
; j
++)
1553 //filter out NOT unique documents + view combinations
1554 if ( templates
[i
]->m_docTypeName
== data
[j
]->m_docTypeName
&&
1555 templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
1562 strings
.Add(templates
[i
]->m_description
);
1564 data
[n
] = templates
[i
];
1572 strings
.Sort(wxStringSortAscending
);
1573 // Yes, this will be slow, but template lists
1574 // are typically short.
1576 n
= strings
.Count();
1577 for (i
= 0; i
< n
; i
++)
1579 for (j
= 0; j
< noTemplates
; j
++)
1581 if (strings
[i
] == templates
[j
]->m_description
)
1582 data
[i
] = templates
[j
];
1587 wxDocTemplate
*theTemplate
;
1592 // no visible templates, hence nothing to choose from
1597 // don't propose the user to choose if he heas no choice
1598 theTemplate
= data
[0];
1602 // propose the user to choose one of several
1603 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1605 _("Select a document template"),
1609 wxFindSuitableParent()
1618 wxDocTemplate
*wxDocManager::SelectViewType(wxDocTemplate
**templates
,
1619 int noTemplates
, bool sort
)
1621 wxArrayString strings
;
1622 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1626 for (i
= 0; i
< noTemplates
; i
++)
1628 wxDocTemplate
*templ
= templates
[i
];
1629 if ( templ
->IsVisible() && !templ
->GetViewName().empty() )
1633 for (j
= 0; j
< n
; j
++)
1635 //filter out NOT unique views
1636 if ( templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
)
1642 strings
.Add(templ
->m_viewTypeName
);
1651 strings
.Sort(wxStringSortAscending
);
1652 // Yes, this will be slow, but template lists
1653 // are typically short.
1655 n
= strings
.Count();
1656 for (i
= 0; i
< n
; i
++)
1658 for (j
= 0; j
< noTemplates
; j
++)
1660 if (strings
[i
] == templates
[j
]->m_viewTypeName
)
1661 data
[i
] = templates
[j
];
1666 wxDocTemplate
*theTemplate
;
1668 // the same logic as above
1672 theTemplate
= (wxDocTemplate
*)NULL
;
1676 theTemplate
= data
[0];
1680 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1682 _("Select a document view"),
1686 wxFindSuitableParent()
1695 void wxDocManager::AssociateTemplate(wxDocTemplate
*temp
)
1697 if (!m_templates
.Member(temp
))
1698 m_templates
.Append(temp
);
1701 void wxDocManager::DisassociateTemplate(wxDocTemplate
*temp
)
1703 m_templates
.DeleteObject(temp
);
1706 // Add and remove a document from the manager's list
1707 void wxDocManager::AddDocument(wxDocument
*doc
)
1709 if (!m_docs
.Member(doc
))
1713 void wxDocManager::RemoveDocument(wxDocument
*doc
)
1715 m_docs
.DeleteObject(doc
);
1718 // Views or windows should inform the document manager
1719 // when a view is going in or out of focus
1720 void wxDocManager::ActivateView(wxView
*view
, bool activate
)
1724 m_currentView
= view
;
1728 if ( m_currentView
== view
)
1730 // don't keep stale pointer
1731 m_currentView
= (wxView
*) NULL
;
1736 // ----------------------------------------------------------------------------
1737 // Default document child frame
1738 // ----------------------------------------------------------------------------
1740 BEGIN_EVENT_TABLE(wxDocChildFrame
, wxFrame
)
1741 EVT_ACTIVATE(wxDocChildFrame::OnActivate
)
1742 EVT_CLOSE(wxDocChildFrame::OnCloseWindow
)
1745 wxDocChildFrame::wxDocChildFrame(wxDocument
*doc
,
1749 const wxString
& title
,
1753 const wxString
& name
)
1754 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1756 m_childDocument
= doc
;
1759 view
->SetFrame(this);
1762 wxDocChildFrame::~wxDocChildFrame()
1766 // Extend event processing to search the view's event table
1767 bool wxDocChildFrame::ProcessEvent(wxEvent
& event
)
1770 m_childView
->Activate(TRUE
);
1772 if ( !m_childView
|| ! m_childView
->ProcessEvent(event
) )
1774 // Only hand up to the parent if it's a menu command
1775 if (!event
.IsKindOf(CLASSINFO(wxCommandEvent
)) || !GetParent() || !GetParent()->ProcessEvent(event
))
1776 return wxEvtHandler::ProcessEvent(event
);
1784 void wxDocChildFrame::OnActivate(wxActivateEvent
& event
)
1786 wxFrame::OnActivate(event
);
1789 m_childView
->Activate(event
.GetActive());
1792 void wxDocChildFrame::OnCloseWindow(wxCloseEvent
& event
)
1796 bool ans
= event
.CanVeto()
1797 ? m_childView
->Close(FALSE
) // FALSE means don't delete associated window
1798 : TRUE
; // Must delete.
1802 m_childView
->Activate(FALSE
);
1804 m_childView
= (wxView
*) NULL
;
1805 m_childDocument
= (wxDocument
*) NULL
;
1816 // ----------------------------------------------------------------------------
1817 // Default parent frame
1818 // ----------------------------------------------------------------------------
1820 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1821 EVT_MENU(wxID_EXIT
, wxDocParentFrame::OnExit
)
1822 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE9
, wxDocParentFrame::OnMRUFile
)
1823 EVT_CLOSE(wxDocParentFrame::OnCloseWindow
)
1826 wxDocParentFrame::wxDocParentFrame(wxDocManager
*manager
,
1829 const wxString
& title
,
1833 const wxString
& name
)
1834 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1836 m_docManager
= manager
;
1839 void wxDocParentFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
1844 void wxDocParentFrame::OnMRUFile(wxCommandEvent
& event
)
1846 int n
= event
.GetId() - wxID_FILE1
; // the index in MRU list
1847 wxString
filename(m_docManager
->GetHistoryFile(n
));
1848 if ( !filename
.IsEmpty() )
1850 // verify that the file exists before doing anything else
1851 if ( wxFile::Exists(filename
) )
1854 if (!m_docManager
->CreateDocument(filename
, wxDOC_SILENT
))
1856 // remove the file from the MRU list. The user should already be notified.
1857 m_docManager
->RemoveFileFromHistory(n
);
1859 wxLogError(_("The file '%s' couldn't be opened.\nIt has been removed from the most recently used files list."),
1865 // remove the bogus filename from the MRU list and notify the user
1867 m_docManager
->RemoveFileFromHistory(n
);
1869 wxLogError(_("The file '%s' doesn't exist and couldn't be opened.\nIt has been removed from the most recently used files list."),
1875 // Extend event processing to search the view's event table
1876 bool wxDocParentFrame::ProcessEvent(wxEvent
& event
)
1878 // Try the document manager, then do default processing
1879 if (!m_docManager
|| !m_docManager
->ProcessEvent(event
))
1880 return wxEvtHandler::ProcessEvent(event
);
1885 // Define the behaviour for the frame closing
1886 // - must delete all frames except for the main one.
1887 void wxDocParentFrame::OnCloseWindow(wxCloseEvent
& event
)
1889 if (m_docManager
->Clear(!event
.CanVeto()))
1897 #if wxUSE_PRINTING_ARCHITECTURE
1899 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
)
1902 m_printoutView
= view
;
1905 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
1909 // Get the logical pixels per inch of screen and printer
1910 int ppiScreenX
, ppiScreenY
;
1911 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
1912 wxUnusedVar(ppiScreenY
);
1913 int ppiPrinterX
, ppiPrinterY
;
1914 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
1915 wxUnusedVar(ppiPrinterY
);
1917 // This scales the DC so that the printout roughly represents the
1918 // the screen scaling. The text point size _should_ be the right size
1919 // but in fact is too small for some reason. This is a detail that will
1920 // need to be addressed at some point but can be fudged for the
1922 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
1924 // Now we have to check in case our real page size is reduced
1925 // (e.g. because we're drawing to a print preview memory DC)
1926 int pageWidth
, pageHeight
;
1928 dc
->GetSize(&w
, &h
);
1929 GetPageSizePixels(&pageWidth
, &pageHeight
);
1930 wxUnusedVar(pageHeight
);
1932 // If printer pageWidth == current DC width, then this doesn't
1933 // change. But w might be the preview bitmap width, so scale down.
1934 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
1935 dc
->SetUserScale(overallScale
, overallScale
);
1939 m_printoutView
->OnDraw(dc
);
1944 bool wxDocPrintout::HasPage(int pageNum
)
1946 return (pageNum
== 1);
1949 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
1951 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
1957 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
1965 #endif // wxUSE_PRINTING_ARCHITECTURE
1967 // ----------------------------------------------------------------------------
1968 // File history processor
1969 // ----------------------------------------------------------------------------
1971 static inline wxChar
* MYcopystring(const wxString
& s
)
1973 wxChar
* copy
= new wxChar
[s
.length() + 1];
1974 return wxStrcpy(copy
, s
.c_str());
1977 static inline wxChar
* MYcopystring(const wxChar
* s
)
1979 wxChar
* copy
= new wxChar
[wxStrlen(s
) + 1];
1980 return wxStrcpy(copy
, s
);
1983 wxFileHistory::wxFileHistory(size_t maxFiles
, wxWindowID idBase
)
1985 m_fileMaxFiles
= maxFiles
;
1988 m_fileHistory
= new wxChar
*[m_fileMaxFiles
];
1991 wxFileHistory::~wxFileHistory()
1994 for (i
= 0; i
< m_fileHistoryN
; i
++)
1995 delete[] m_fileHistory
[i
];
1996 delete[] m_fileHistory
;
1999 // File history management
2000 void wxFileHistory::AddFileToHistory(const wxString
& file
)
2004 // Check we don't already have this file
2005 for (i
= 0; i
< m_fileHistoryN
; i
++)
2007 #if defined( __WXMSW__ ) // Add any other OSes with case insensitive file names
2008 wxString testString
;
2009 if ( m_fileHistory
[i
] )
2010 testString
= m_fileHistory
[i
];
2011 if ( m_fileHistory
[i
] && ( file
.Lower() == testString
.Lower() ) )
2013 if ( m_fileHistory
[i
] && ( file
== m_fileHistory
[i
] ) )
2016 // we do have it, move it to the top of the history
2017 RemoveFileFromHistory (i
);
2018 AddFileToHistory (file
);
2023 // if we already have a full history, delete the one at the end
2024 if ( m_fileMaxFiles
== m_fileHistoryN
)
2026 RemoveFileFromHistory (m_fileHistoryN
- 1);
2027 AddFileToHistory (file
);
2031 // Add to the project file history:
2032 // Move existing files (if any) down so we can insert file at beginning.
2033 if (m_fileHistoryN
< m_fileMaxFiles
)
2035 wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2038 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2039 if ( m_fileHistoryN
== 0 && menu
->GetMenuItemCount() )
2041 menu
->AppendSeparator();
2043 menu
->Append(m_idBase
+m_fileHistoryN
, _("[EMPTY]"));
2044 node
= node
->GetNext();
2048 // Shuffle filenames down
2049 for (i
= (m_fileHistoryN
-1); i
> 0; i
--)
2051 m_fileHistory
[i
] = m_fileHistory
[i
-1];
2053 m_fileHistory
[0] = MYcopystring(file
);
2055 // this is the directory of the last opened file
2056 wxString pathCurrent
;
2057 wxSplitPath( m_fileHistory
[0], &pathCurrent
, NULL
, NULL
);
2058 for (i
= 0; i
< m_fileHistoryN
; i
++)
2060 if ( m_fileHistory
[i
] )
2062 // if in same directory just show the filename; otherwise the full
2064 wxString pathInMenu
, path
, filename
, ext
;
2065 wxSplitPath( m_fileHistory
[i
], &path
, &filename
, &ext
);
2066 if ( path
== pathCurrent
)
2068 pathInMenu
= filename
;
2070 pathInMenu
= pathInMenu
+ wxFILE_SEP_EXT
+ ext
;
2074 // absolute path; could also set relative path
2075 pathInMenu
= m_fileHistory
[i
];
2079 buf
.Printf(s_MRUEntryFormat
, i
+ 1, pathInMenu
.c_str());
2080 wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2083 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2084 menu
->SetLabel(m_idBase
+ i
, buf
);
2085 node
= node
->GetNext();
2091 void wxFileHistory::RemoveFileFromHistory(size_t i
)
2093 wxCHECK_RET( i
< m_fileHistoryN
,
2094 wxT("invalid index in wxFileHistory::RemoveFileFromHistory") );
2096 // delete the element from the array (could use memmove() too...)
2097 delete [] m_fileHistory
[i
];
2100 for ( j
= i
; j
< m_fileHistoryN
- 1; j
++ )
2102 m_fileHistory
[j
] = m_fileHistory
[j
+ 1];
2105 wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2108 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2110 // shuffle filenames up
2112 for ( j
= i
; j
< m_fileHistoryN
- 1; j
++ )
2114 buf
.Printf(s_MRUEntryFormat
, j
+ 1, m_fileHistory
[j
]);
2115 menu
->SetLabel(m_idBase
+ j
, buf
);
2118 node
= node
->GetNext();
2120 // delete the last menu item which is unused now
2121 wxWindowID lastItemId
= m_idBase
+ m_fileHistoryN
- 1;
2122 if (menu
->FindItem(lastItemId
))
2124 menu
->Delete(lastItemId
);
2127 // delete the last separator too if no more files are left
2128 if ( m_fileHistoryN
== 1 )
2130 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetLast();
2133 wxMenuItem
*menuItem
= node
->GetData();
2134 if ( menuItem
->IsSeparator() )
2136 menu
->Delete(menuItem
);
2138 //else: should we search backwards for the last separator?
2140 //else: menu is empty somehow
2147 wxString
wxFileHistory::GetHistoryFile(size_t i
) const
2150 if ( i
< m_fileHistoryN
)
2152 s
= m_fileHistory
[i
];
2156 wxFAIL_MSG( wxT("bad index in wxFileHistory::GetHistoryFile") );
2162 void wxFileHistory::UseMenu(wxMenu
*menu
)
2164 if (!m_fileMenus
.Member(menu
))
2165 m_fileMenus
.Append(menu
);
2168 void wxFileHistory::RemoveMenu(wxMenu
*menu
)
2170 m_fileMenus
.DeleteObject(menu
);
2174 void wxFileHistory::Load(wxConfigBase
& config
)
2178 buf
.Printf(wxT("file%d"), (int)m_fileHistoryN
+1);
2179 wxString historyFile
;
2180 while ((m_fileHistoryN
< m_fileMaxFiles
) && config
.Read(buf
, &historyFile
) && (historyFile
!= wxT("")))
2182 m_fileHistory
[m_fileHistoryN
] = MYcopystring((const wxChar
*) historyFile
);
2184 buf
.Printf(wxT("file%d"), (int)m_fileHistoryN
+1);
2185 historyFile
= wxT("");
2190 void wxFileHistory::Save(wxConfigBase
& config
)
2193 for (i
= 0; i
< m_fileMaxFiles
; i
++)
2196 buf
.Printf(wxT("file%d"), (int)i
+1);
2197 if (i
< m_fileHistoryN
)
2198 config
.Write(buf
, wxString(m_fileHistory
[i
]));
2200 config
.Write(buf
, wxEmptyString
);
2203 #endif // wxUSE_CONFIG
2205 void wxFileHistory::AddFilesToMenu()
2207 if (m_fileHistoryN
> 0)
2209 wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2212 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2213 if (menu
->GetMenuItemCount())
2215 menu
->AppendSeparator();
2219 for (i
= 0; i
< m_fileHistoryN
; i
++)
2221 if (m_fileHistory
[i
])
2224 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
2225 menu
->Append(m_idBase
+i
, buf
);
2228 node
= node
->GetNext();
2233 void wxFileHistory::AddFilesToMenu(wxMenu
* menu
)
2235 if (m_fileHistoryN
> 0)
2237 if (menu
->GetMenuItemCount())
2239 menu
->AppendSeparator();
2243 for (i
= 0; i
< m_fileHistoryN
; i
++)
2245 if (m_fileHistory
[i
])
2248 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
2249 menu
->Append(m_idBase
+i
, buf
);
2255 // ----------------------------------------------------------------------------
2256 // Permits compatibility with existing file formats and functions that
2257 // manipulate files directly
2258 // ----------------------------------------------------------------------------
2260 #if wxUSE_STD_IOSTREAM
2262 bool wxTransferFileToStream(const wxString
& filename
, wxSTD ostream
& stream
)
2264 wxFFile
file(filename
, _T("rb"));
2265 if ( !file
.IsOpened() )
2273 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2277 stream
.write(buf
, nRead
);
2281 while ( !file
.Eof() );
2286 bool wxTransferStreamToFile(wxSTD istream
& stream
, const wxString
& filename
)
2288 wxFFile
file(filename
, _T("wb"));
2289 if ( !file
.IsOpened() )
2295 stream
.read(buf
, WXSIZEOF(buf
));
2296 if ( !stream
.bad() ) // fail may be set on EOF, don't use operator!()
2298 if ( !file
.Write(buf
, stream
.gcount()) )
2302 while ( !stream
.eof() );
2307 #else // !wxUSE_STD_IOSTREAM
2309 bool wxTransferFileToStream(const wxString
& filename
, wxOutputStream
& stream
)
2311 wxFFile
file(filename
, _T("rb"));
2312 if ( !file
.IsOpened() )
2320 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2324 stream
.Write(buf
, nRead
);
2328 while ( !file
.Eof() );
2333 bool wxTransferStreamToFile(wxInputStream
& stream
, const wxString
& filename
)
2335 wxFFile
file(filename
, _T("wb"));
2336 if ( !file
.IsOpened() )
2342 stream
.Read(buf
, WXSIZEOF(buf
));
2344 const size_t nRead
= stream
.LastRead();
2345 if ( !nRead
|| !file
.Write(buf
, nRead
) )
2348 while ( !stream
.Eof() );
2353 #endif // wxUSE_STD_IOSTREAM/!wxUSE_STD_IOSTREAM
2355 #endif // wxUSE_DOC_VIEW_ARCHITECTURE