1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/docview.cpp
3 // Purpose: Document/view classes
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "docview.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
31 #if wxUSE_DOC_VIEW_ARCHITECTURE
34 #include "wx/string.h"
38 #include "wx/dialog.h"
41 #include "wx/filedlg.h"
49 #include "wx/filename.h"
56 #if wxUSE_PRINTING_ARCHITECTURE
57 #include "wx/prntbase.h"
58 #include "wx/printdlg.h"
61 #include "wx/msgdlg.h"
62 #include "wx/choicdlg.h"
63 #include "wx/docview.h"
64 #include "wx/confbase.h"
66 #include "wx/cmdproc.h"
71 #if wxUSE_STD_IOSTREAM
72 #include "wx/ioswrap.h"
79 #include "wx/wfstream.h"
82 // ----------------------------------------------------------------------------
84 // ----------------------------------------------------------------------------
86 IMPLEMENT_ABSTRACT_CLASS(wxDocument
, wxEvtHandler
)
87 IMPLEMENT_ABSTRACT_CLASS(wxView
, wxEvtHandler
)
88 IMPLEMENT_ABSTRACT_CLASS(wxDocTemplate
, wxObject
)
89 IMPLEMENT_DYNAMIC_CLASS(wxDocManager
, wxEvtHandler
)
90 IMPLEMENT_CLASS(wxDocChildFrame
, wxFrame
)
91 IMPLEMENT_CLASS(wxDocParentFrame
, wxFrame
)
93 #if wxUSE_PRINTING_ARCHITECTURE
94 IMPLEMENT_DYNAMIC_CLASS(wxDocPrintout
, wxPrintout
)
97 IMPLEMENT_DYNAMIC_CLASS(wxFileHistory
, wxObject
)
99 // ----------------------------------------------------------------------------
100 // function prototypes
101 // ----------------------------------------------------------------------------
103 static inline wxString
FindExtension(const wxChar
*path
);
104 static wxWindow
* wxFindSuitableParent(void);
106 // ----------------------------------------------------------------------------
108 // ----------------------------------------------------------------------------
110 static const wxChar
*s_MRUEntryFormat
= wxT("&%d %s");
112 // ============================================================================
114 // ============================================================================
116 // ----------------------------------------------------------------------------
118 // ----------------------------------------------------------------------------
120 static wxString
FindExtension(const wxChar
*path
)
123 wxSplitPath(path
, NULL
, NULL
, &ext
);
125 // VZ: extensions are considered not case sensitive - is this really a good
127 return ext
.MakeLower();
130 // ----------------------------------------------------------------------------
131 // Definition of wxDocument
132 // ----------------------------------------------------------------------------
134 wxDocument::wxDocument(wxDocument
*parent
)
136 m_documentModified
= FALSE
;
137 m_documentParent
= parent
;
138 m_documentTemplate
= (wxDocTemplate
*) NULL
;
139 m_commandProcessor
= (wxCommandProcessor
*) NULL
;
143 bool wxDocument::DeleteContents()
148 wxDocument::~wxDocument()
152 if (m_commandProcessor
)
153 delete m_commandProcessor
;
155 if (GetDocumentManager())
156 GetDocumentManager()->RemoveDocument(this);
158 // Not safe to do here, since it'll invoke virtual view functions
159 // expecting to see valid derived objects: and by the time we get here,
160 // we've called destructors higher up.
164 bool wxDocument::Close()
166 if (OnSaveModified())
167 return OnCloseDocument();
172 bool wxDocument::OnCloseDocument()
174 // Tell all views that we're about to close
181 // Note that this implicitly deletes the document when the last view is
183 bool wxDocument::DeleteAllViews()
185 wxDocManager
* manager
= GetDocumentManager();
187 wxNode
*node
= m_documentViews
.GetFirst();
190 wxView
*view
= (wxView
*)node
->GetData();
194 wxNode
*next
= node
->GetNext();
196 delete view
; // Deletes node implicitly
199 // If we haven't yet deleted the document (for example
200 // if there were no views) then delete it.
201 if (manager
&& manager
->GetDocuments().Member(this))
207 wxView
*wxDocument::GetFirstView() const
209 if (m_documentViews
.GetCount() == 0)
210 return (wxView
*) NULL
;
211 return (wxView
*)m_documentViews
.GetFirst()->GetData();
214 wxDocManager
*wxDocument::GetDocumentManager() const
216 return (m_documentTemplate
? m_documentTemplate
->GetDocumentManager() : (wxDocManager
*) NULL
);
219 bool wxDocument::OnNewDocument()
221 if (!OnSaveModified())
224 if (OnCloseDocument()==FALSE
) return FALSE
;
227 SetDocumentSaved(FALSE
);
230 GetDocumentManager()->MakeDefaultName(name
);
232 SetFilename(name
, TRUE
);
237 bool wxDocument::Save()
239 if (!IsModified() && m_savedYet
)
242 if ( m_documentFile
.empty() || !m_savedYet
)
245 return OnSaveDocument(m_documentFile
);
248 bool wxDocument::SaveAs()
250 wxDocTemplate
*docTemplate
= GetDocumentTemplate();
254 wxString tmp
= wxFileSelector(_("Save as"),
255 docTemplate
->GetDirectory(),
256 wxFileNameFromPath(GetFilename()),
257 docTemplate
->GetDefaultExtension(),
258 docTemplate
->GetFileFilter(),
259 wxSAVE
| wxOVERWRITE_PROMPT
,
260 GetDocumentWindow());
265 wxString
fileName(tmp
);
266 wxString path
, name
, ext
;
267 wxSplitPath(fileName
, & path
, & name
, & ext
);
269 if (ext
.IsEmpty() || ext
== wxT(""))
271 fileName
+= wxT(".");
272 fileName
+= docTemplate
->GetDefaultExtension();
275 SetFilename(fileName
);
276 SetTitle(wxFileNameFromPath(fileName
));
278 // Notify the views that the filename has changed
279 wxNode
*node
= m_documentViews
.GetFirst();
282 wxView
*view
= (wxView
*)node
->GetData();
283 view
->OnChangeFilename();
284 node
= node
->GetNext();
287 // Files that were not saved correctly are not added to the FileHistory.
288 if (!OnSaveDocument(m_documentFile
))
291 // A file that doesn't use the default extension of its document template cannot be opened
292 // via the FileHistory, so we do not add it.
293 if (docTemplate
->FileMatchesTemplate(fileName
))
295 GetDocumentManager()->AddFileToHistory(fileName
);
299 // The user will probably not be able to open the file again, so
300 // we could warn about the wrong file-extension here.
305 bool wxDocument::OnSaveDocument(const wxString
& file
)
311 if (wxTheApp
->GetAppName() != wxT(""))
312 msgTitle
= wxTheApp
->GetAppName();
314 msgTitle
= wxString(_("File error"));
316 #if wxUSE_STD_IOSTREAM
317 wxSTD ofstream
store(file
.mb_str());
318 if (store
.fail() || store
.bad())
320 wxFileOutputStream
store(file
);
321 if (store
.GetLastError() != wxSTREAM_NO_ERROR
)
324 (void)wxMessageBox(_("Sorry, could not open this file for saving."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
325 GetDocumentWindow());
329 if (!SaveObject(store
))
331 (void)wxMessageBox(_("Sorry, could not save this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
332 GetDocumentWindow());
338 SetDocumentSaved(TRUE
);
340 wxFileName
fn(file
) ;
341 fn
.MacSetDefaultTypeAndCreator() ;
346 bool wxDocument::OnOpenDocument(const wxString
& file
)
348 if (!OnSaveModified())
352 if (wxTheApp
->GetAppName() != wxT(""))
353 msgTitle
= wxTheApp
->GetAppName();
355 msgTitle
= wxString(_("File error"));
357 #if wxUSE_STD_IOSTREAM
358 wxSTD ifstream
store(file
.mb_str());
359 if (store
.fail() || store
.bad())
361 wxFileInputStream
store(file
);
362 if (store
.GetLastError() != wxSTREAM_NO_ERROR
)
365 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
|wxICON_EXCLAMATION
,
366 GetDocumentWindow());
369 #if wxUSE_STD_IOSTREAM
371 if ( !store
&& !store
.eof() )
373 int res
= LoadObject(store
).GetLastError();
374 if ((res
!= wxSTREAM_NO_ERROR
) &&
375 (res
!= wxSTREAM_EOF
))
378 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
|wxICON_EXCLAMATION
,
379 GetDocumentWindow());
382 SetFilename(file
, TRUE
);
391 #if wxUSE_STD_IOSTREAM
392 wxSTD istream
& wxDocument::LoadObject(wxSTD istream
& stream
)
394 wxInputStream
& wxDocument::LoadObject(wxInputStream
& stream
)
400 #if wxUSE_STD_IOSTREAM
401 wxSTD ostream
& wxDocument::SaveObject(wxSTD ostream
& stream
)
403 wxOutputStream
& wxDocument::SaveObject(wxOutputStream
& stream
)
409 bool wxDocument::Revert()
415 // Get title, or filename if no title, else unnamed
416 bool wxDocument::GetPrintableName(wxString
& buf
) const
418 if (m_documentTitle
!= wxT(""))
420 buf
= m_documentTitle
;
423 else if (m_documentFile
!= wxT(""))
425 buf
= wxFileNameFromPath(m_documentFile
);
435 wxWindow
*wxDocument::GetDocumentWindow() const
437 wxView
*view
= GetFirstView();
439 return view
->GetFrame();
441 return wxTheApp
->GetTopWindow();
444 wxCommandProcessor
*wxDocument::OnCreateCommandProcessor()
446 return new wxCommandProcessor
;
449 // TRUE if safe to close
450 bool wxDocument::OnSaveModified()
455 GetPrintableName(title
);
458 if (wxTheApp
->GetAppName() != wxT(""))
459 msgTitle
= wxTheApp
->GetAppName();
461 msgTitle
= wxString(_("Warning"));
464 prompt
.Printf(_("Do you want to save changes to document %s?"),
465 (const wxChar
*)title
);
466 int res
= wxMessageBox(prompt
, msgTitle
,
467 wxYES_NO
|wxCANCEL
|wxICON_QUESTION
,
468 GetDocumentWindow());
474 else if (res
== wxYES
)
476 else if (res
== wxCANCEL
)
482 bool wxDocument::Draw(wxDC
& WXUNUSED(context
))
487 bool wxDocument::AddView(wxView
*view
)
489 if (!m_documentViews
.Member(view
))
491 m_documentViews
.Append(view
);
497 bool wxDocument::RemoveView(wxView
*view
)
499 (void)m_documentViews
.DeleteObject(view
);
504 bool wxDocument::OnCreate(const wxString
& WXUNUSED(path
), long flags
)
506 if (GetDocumentTemplate()->CreateView(this, flags
))
512 // Called after a view is added or removed.
513 // The default implementation deletes the document if
514 // there are no more views.
515 void wxDocument::OnChangedViewList()
517 if (m_documentViews
.GetCount() == 0)
519 if (OnSaveModified())
526 void wxDocument::UpdateAllViews(wxView
*sender
, wxObject
*hint
)
528 wxNode
*node
= m_documentViews
.GetFirst();
531 wxView
*view
= (wxView
*)node
->GetData();
533 view
->OnUpdate(sender
, hint
);
534 node
= node
->GetNext();
538 void wxDocument::NotifyClosing()
540 wxNode
*node
= m_documentViews
.GetFirst();
543 wxView
*view
= (wxView
*)node
->GetData();
544 view
->OnClosingDocument();
545 node
= node
->GetNext();
549 void wxDocument::SetFilename(const wxString
& filename
, bool notifyViews
)
551 m_documentFile
= filename
;
554 // Notify the views that the filename has changed
555 wxNode
*node
= m_documentViews
.GetFirst();
558 wxView
*view
= (wxView
*)node
->GetData();
559 view
->OnChangeFilename();
560 node
= node
->GetNext();
565 // ----------------------------------------------------------------------------
567 // ----------------------------------------------------------------------------
572 m_viewDocument
= (wxDocument
*) NULL
;
574 m_viewTypeName
= wxT("");
575 m_viewFrame
= (wxFrame
*) NULL
;
580 // GetDocumentManager()->ActivateView(this, FALSE, TRUE);
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 wxNode
*node
= m_docs
.GetFirst();
814 wxDocument
*doc
= (wxDocument
*)node
->GetData();
815 wxNode
*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 wxNode
*node
= m_templates
.GetFirst();
836 wxDocTemplate
*templ
= (wxDocTemplate
*) node
->GetData();
837 wxNode
* 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
= (wxPrintPreviewBase
*) NULL
;
956 preview
= new wxPrintPreview(printout
, view
->OnCreatePrintout());
958 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, (wxFrame
*)wxTheApp
->GetTopWindow(), _("Print Preview"),
959 wxPoint(100, 100), wxSize(600, 650));
960 frame
->Centre(wxBOTH
);
964 #endif // wxUSE_PRINTING_ARCHITECTURE
967 void wxDocManager::OnUndo(wxCommandEvent
& WXUNUSED(event
))
969 wxDocument
*doc
= GetCurrentDocument();
972 if (doc
->GetCommandProcessor())
973 doc
->GetCommandProcessor()->Undo();
976 void wxDocManager::OnRedo(wxCommandEvent
& WXUNUSED(event
))
978 wxDocument
*doc
= GetCurrentDocument();
981 if (doc
->GetCommandProcessor())
982 doc
->GetCommandProcessor()->Redo();
985 // Handlers for UI update commands
987 void wxDocManager::OnUpdateFileOpen(wxUpdateUIEvent
& event
)
989 event
.Enable( TRUE
);
992 void wxDocManager::OnUpdateFileClose(wxUpdateUIEvent
& event
)
994 wxDocument
*doc
= GetCurrentDocument();
995 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
998 void wxDocManager::OnUpdateFileRevert(wxUpdateUIEvent
& event
)
1000 wxDocument
*doc
= GetCurrentDocument();
1001 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1004 void wxDocManager::OnUpdateFileNew(wxUpdateUIEvent
& event
)
1006 event
.Enable( TRUE
);
1009 void wxDocManager::OnUpdateFileSave(wxUpdateUIEvent
& event
)
1011 wxDocument
*doc
= GetCurrentDocument();
1012 event
.Enable( doc
&& doc
->IsModified() );
1015 void wxDocManager::OnUpdateFileSaveAs(wxUpdateUIEvent
& event
)
1017 wxDocument
*doc
= GetCurrentDocument();
1018 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1021 void wxDocManager::OnUpdateUndo(wxUpdateUIEvent
& event
)
1023 wxDocument
*doc
= GetCurrentDocument();
1024 event
.Enable( (doc
&& doc
->GetCommandProcessor() && doc
->GetCommandProcessor()->CanUndo()) );
1025 if (doc
&& doc
->GetCommandProcessor())
1026 doc
->GetCommandProcessor()->SetMenuStrings();
1029 void wxDocManager::OnUpdateRedo(wxUpdateUIEvent
& event
)
1031 wxDocument
*doc
= GetCurrentDocument();
1032 event
.Enable( (doc
&& doc
->GetCommandProcessor() && doc
->GetCommandProcessor()->CanRedo()) );
1033 if (doc
&& doc
->GetCommandProcessor())
1034 doc
->GetCommandProcessor()->SetMenuStrings();
1037 void wxDocManager::OnUpdatePrint(wxUpdateUIEvent
& event
)
1039 wxDocument
*doc
= GetCurrentDocument();
1040 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1043 void wxDocManager::OnUpdatePrintSetup(wxUpdateUIEvent
& event
)
1045 event
.Enable( TRUE
);
1048 void wxDocManager::OnUpdatePreview(wxUpdateUIEvent
& event
)
1050 wxDocument
*doc
= GetCurrentDocument();
1051 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1054 wxView
*wxDocManager::GetCurrentView() const
1057 return m_currentView
;
1058 if (m_docs
.GetCount() == 1)
1060 wxDocument
* doc
= (wxDocument
*) m_docs
.GetFirst()->GetData();
1061 return doc
->GetFirstView();
1063 return (wxView
*) NULL
;
1066 // Extend event processing to search the view's event table
1067 bool wxDocManager::ProcessEvent(wxEvent
& event
)
1069 wxView
* view
= GetCurrentView();
1072 if (view
->ProcessEvent(event
))
1075 return wxEvtHandler::ProcessEvent(event
);
1078 wxDocument
*wxDocManager::CreateDocument(const wxString
& path
, long flags
)
1080 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.GetCount()];
1083 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1085 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Item(i
)->GetData());
1086 if (temp
->IsVisible())
1088 templates
[n
] = temp
;
1095 return (wxDocument
*) NULL
;
1098 wxDocument
* docToClose
= NULL
;
1100 // If we've reached the max number of docs, close the
1102 if ( (int)GetDocuments().GetCount() >= m_maxDocsOpen
)
1104 wxDocument
*doc
= (wxDocument
*)GetDocuments().GetFirst()->GetData();
1108 // New document: user chooses a template, unless there's only one.
1109 if (flags
& wxDOC_NEW
)
1115 if (!CloseDocument(docToClose
, FALSE
))
1122 wxDocTemplate
*temp
= templates
[0];
1124 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
1128 newDoc
->SetDocumentName(temp
->GetDocumentName());
1129 newDoc
->SetDocumentTemplate(temp
);
1130 newDoc
->OnNewDocument();
1135 wxDocTemplate
*temp
= SelectDocumentType(templates
, n
);
1141 if (!CloseDocument(docToClose
, FALSE
))
1147 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
1151 newDoc
->SetDocumentName(temp
->GetDocumentName());
1152 newDoc
->SetDocumentTemplate(temp
);
1153 newDoc
->OnNewDocument();
1158 return (wxDocument
*) NULL
;
1161 // Existing document
1162 wxDocTemplate
*temp
= (wxDocTemplate
*) NULL
;
1164 wxString
path2(wxT(""));
1165 if (path
!= wxT(""))
1168 if (flags
& wxDOC_SILENT
)
1170 temp
= FindTemplateForPath(path2
);
1173 // Since we do not add files with non-default extensions to the FileHistory this
1174 // can only happen if the application changes the allowed templates in runtime.
1175 (void)wxMessageBox(_("Sorry, the format for this file is unknown."),
1177 wxOK
| wxICON_EXCLAMATION
, wxFindSuitableParent());
1181 temp
= SelectDocumentPath(templates
, n
, path2
, flags
);
1189 if (!CloseDocument(docToClose
, FALSE
))
1195 wxDocument
*newDoc
= temp
->CreateDocument(path2
, flags
);
1198 newDoc
->SetDocumentName(temp
->GetDocumentName());
1199 newDoc
->SetDocumentTemplate(temp
);
1200 if (!newDoc
->OnOpenDocument(path2
))
1202 newDoc
->DeleteAllViews();
1203 // delete newDoc; // Implicitly deleted by DeleteAllViews
1204 return (wxDocument
*) NULL
;
1206 // A file that doesn't use the default extension of its document
1207 // template cannot be opened via the FileHistory, so we do not
1209 if (temp
->FileMatchesTemplate(path2
))
1210 AddFileToHistory(path2
);
1215 return (wxDocument
*) NULL
;
1218 wxView
*wxDocManager::CreateView(wxDocument
*doc
, long flags
)
1220 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.GetCount()];
1223 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1225 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Item(i
)->GetData());
1226 if (temp
->IsVisible())
1228 if (temp
->GetDocumentName() == doc
->GetDocumentName())
1230 templates
[n
] = temp
;
1238 return (wxView
*) NULL
;
1242 wxDocTemplate
*temp
= templates
[0];
1244 wxView
*view
= temp
->CreateView(doc
, flags
);
1246 view
->SetViewName(temp
->GetViewName());
1250 wxDocTemplate
*temp
= SelectViewType(templates
, n
);
1254 wxView
*view
= temp
->CreateView(doc
, flags
);
1256 view
->SetViewName(temp
->GetViewName());
1260 return (wxView
*) NULL
;
1263 // Not yet implemented
1264 void wxDocManager::DeleteTemplate(wxDocTemplate
*WXUNUSED(temp
), long WXUNUSED(flags
))
1268 // Not yet implemented
1269 bool wxDocManager::FlushDoc(wxDocument
*WXUNUSED(doc
))
1274 wxDocument
*wxDocManager::GetCurrentDocument() const
1276 wxView
*view
= GetCurrentView();
1278 return view
->GetDocument();
1280 return (wxDocument
*) NULL
;
1283 // Make a default document name
1284 bool wxDocManager::MakeDefaultName(wxString
& name
)
1286 name
.Printf(_("unnamed%d"), m_defaultDocumentNameCounter
);
1287 m_defaultDocumentNameCounter
++;
1292 // Make a frame title (override this to do something different)
1293 // If docName is empty, a document is not currently active.
1294 wxString
wxDocManager::MakeFrameTitle(wxDocument
* doc
)
1296 wxString appName
= wxTheApp
->GetAppName();
1303 doc
->GetPrintableName(docName
);
1304 title
= docName
+ wxString(_(" - ")) + appName
;
1310 // Not yet implemented
1311 wxDocTemplate
*wxDocManager::MatchTemplate(const wxString
& WXUNUSED(path
))
1313 return (wxDocTemplate
*) NULL
;
1316 // File history management
1317 void wxDocManager::AddFileToHistory(const wxString
& file
)
1320 m_fileHistory
->AddFileToHistory(file
);
1323 void wxDocManager::RemoveFileFromHistory(size_t i
)
1326 m_fileHistory
->RemoveFileFromHistory(i
);
1329 wxString
wxDocManager::GetHistoryFile(size_t i
) const
1334 histFile
= m_fileHistory
->GetHistoryFile(i
);
1339 void wxDocManager::FileHistoryUseMenu(wxMenu
*menu
)
1342 m_fileHistory
->UseMenu(menu
);
1345 void wxDocManager::FileHistoryRemoveMenu(wxMenu
*menu
)
1348 m_fileHistory
->RemoveMenu(menu
);
1352 void wxDocManager::FileHistoryLoad(wxConfigBase
& config
)
1355 m_fileHistory
->Load(config
);
1358 void wxDocManager::FileHistorySave(wxConfigBase
& config
)
1361 m_fileHistory
->Save(config
);
1365 void wxDocManager::FileHistoryAddFilesToMenu(wxMenu
* menu
)
1368 m_fileHistory
->AddFilesToMenu(menu
);
1371 void wxDocManager::FileHistoryAddFilesToMenu()
1374 m_fileHistory
->AddFilesToMenu();
1377 size_t wxDocManager::GetHistoryFilesCount() const
1379 return m_fileHistory
? m_fileHistory
->GetCount() : 0;
1383 // Find out the document template via matching in the document file format
1384 // against that of the template
1385 wxDocTemplate
*wxDocManager::FindTemplateForPath(const wxString
& path
)
1387 wxDocTemplate
*theTemplate
= (wxDocTemplate
*) NULL
;
1389 // Find the template which this extension corresponds to
1390 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1392 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Item(i
)->GetData();
1393 if ( temp
->FileMatchesTemplate(path
) )
1402 // Try to get a more suitable parent frame than the top window,
1403 // for selection dialogs. Otherwise you may get an unexpected
1404 // window being activated when a dialog is shown.
1405 static wxWindow
* wxFindSuitableParent()
1407 wxWindow
* parent
= wxTheApp
->GetTopWindow();
1409 wxWindow
* focusWindow
= wxWindow::FindFocus();
1412 while (focusWindow
&&
1413 !focusWindow
->IsKindOf(CLASSINFO(wxDialog
)) &&
1414 !focusWindow
->IsKindOf(CLASSINFO(wxFrame
)))
1416 focusWindow
= focusWindow
->GetParent();
1419 parent
= focusWindow
;
1424 // Prompts user to open a file, using file specs in templates.
1425 // Must extend the file selector dialog or implement own; OR
1426 // match the extension to the template extension.
1428 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**templates
,
1429 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
1432 int WXUNUSED(noTemplates
),
1435 long WXUNUSED(flags
),
1436 bool WXUNUSED(save
))
1438 // We can only have multiple filters in Windows and GTK
1439 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
1443 for (i
= 0; i
< noTemplates
; i
++)
1445 if (templates
[i
]->IsVisible())
1447 // add a '|' to separate this filter from the previous one
1448 if ( !descrBuf
.IsEmpty() )
1449 descrBuf
<< wxT('|');
1451 descrBuf
<< templates
[i
]->GetDescription()
1452 << wxT(" (") << templates
[i
]->GetFileFilter() << wxT(") |")
1453 << templates
[i
]->GetFileFilter();
1457 wxString descrBuf
= wxT("*.*");
1460 int FilterIndex
= -1;
1462 wxWindow
* parent
= wxFindSuitableParent();
1464 wxString pathTmp
= wxFileSelectorEx(_("Select a file"),
1472 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)NULL
;
1473 if (!pathTmp
.IsEmpty())
1475 if (!wxFileExists(pathTmp
))
1478 if (!wxTheApp
->GetAppName().IsEmpty())
1479 msgTitle
= wxTheApp
->GetAppName();
1481 msgTitle
= wxString(_("File error"));
1483 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
1487 return (wxDocTemplate
*) NULL
;
1489 m_lastDirectory
= wxPathOnly(pathTmp
);
1493 // first choose the template using the extension, if this fails (i.e.
1494 // wxFileSelectorEx() didn't fill it), then use the path
1495 if ( FilterIndex
!= -1 )
1496 theTemplate
= templates
[FilterIndex
];
1498 theTemplate
= FindTemplateForPath(path
);
1501 // Since we do not add files with non-default extensions to the FileHistory this
1502 // can only happen if the application changes the allowed templates in runtime.
1503 (void)wxMessageBox(_("Sorry, the format for this file is unknown."),
1505 wxOK
| wxICON_EXCLAMATION
, wxFindSuitableParent());
1516 // In all other windowing systems, until we have more advanced
1517 // file selectors, we must select the document type (template) first, and
1518 // _then_ pop up the file selector.
1519 wxDocTemplate
*temp
= SelectDocumentType(templates
, noTemplates
);
1521 return (wxDocTemplate
*) NULL
;
1523 wxChar
*pathTmp
= wxFileSelector(_("Select a file"), wxT(""), wxT(""),
1524 temp
->GetDefaultExtension(),
1525 temp
->GetFileFilter(),
1526 0, wxTheApp
->GetTopWindow());
1534 return (wxDocTemplate
*) NULL
;
1538 wxDocTemplate
*wxDocManager::SelectDocumentType(wxDocTemplate
**templates
,
1539 int noTemplates
, bool sort
)
1541 wxArrayString
strings(sort
);
1542 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1546 for (i
= 0; i
< noTemplates
; i
++)
1548 if (templates
[i
]->IsVisible())
1552 for (j
= 0; j
< n
; j
++)
1554 //filter out NOT unique documents + view combinations
1555 if ( templates
[i
]->m_docTypeName
== data
[j
]->m_docTypeName
&&
1556 templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
1563 strings
.Add(templates
[i
]->m_description
);
1565 data
[n
] = templates
[i
];
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(sort
);
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 // Yes, this will be slow, but template lists
1652 // are typically short.
1654 n
= strings
.Count();
1655 for (i
= 0; i
< n
; i
++)
1657 for (j
= 0; j
< noTemplates
; j
++)
1659 if (strings
[i
] == templates
[j
]->m_viewTypeName
)
1660 data
[i
] = templates
[j
];
1665 wxDocTemplate
*theTemplate
;
1667 // the same logic as above
1671 theTemplate
= (wxDocTemplate
*)NULL
;
1675 theTemplate
= data
[0];
1679 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1681 _("Select a document view"),
1685 wxFindSuitableParent()
1694 void wxDocManager::AssociateTemplate(wxDocTemplate
*temp
)
1696 if (!m_templates
.Member(temp
))
1697 m_templates
.Append(temp
);
1700 void wxDocManager::DisassociateTemplate(wxDocTemplate
*temp
)
1702 m_templates
.DeleteObject(temp
);
1705 // Add and remove a document from the manager's list
1706 void wxDocManager::AddDocument(wxDocument
*doc
)
1708 if (!m_docs
.Member(doc
))
1712 void wxDocManager::RemoveDocument(wxDocument
*doc
)
1714 m_docs
.DeleteObject(doc
);
1717 // Views or windows should inform the document manager
1718 // when a view is going in or out of focus
1719 void wxDocManager::ActivateView(wxView
*view
, bool activate
, bool WXUNUSED(deleting
))
1721 // If we're deactiving, and if we're not actually deleting the view, then
1722 // don't reset the current view because we may be going to
1723 // a window without a view.
1724 // WHAT DID I MEAN BY THAT EXACTLY?
1728 if (m_currentView == view)
1729 m_currentView = NULL;
1735 m_currentView
= view
;
1737 m_currentView
= (wxView
*) NULL
;
1741 // ----------------------------------------------------------------------------
1742 // Default document child frame
1743 // ----------------------------------------------------------------------------
1745 BEGIN_EVENT_TABLE(wxDocChildFrame
, wxFrame
)
1746 EVT_ACTIVATE(wxDocChildFrame::OnActivate
)
1747 EVT_CLOSE(wxDocChildFrame::OnCloseWindow
)
1750 wxDocChildFrame::wxDocChildFrame(wxDocument
*doc
,
1754 const wxString
& title
,
1758 const wxString
& name
)
1759 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1761 m_childDocument
= doc
;
1764 view
->SetFrame(this);
1767 wxDocChildFrame::~wxDocChildFrame()
1771 // Extend event processing to search the view's event table
1772 bool wxDocChildFrame::ProcessEvent(wxEvent
& event
)
1775 m_childView
->Activate(TRUE
);
1777 if ( !m_childView
|| ! m_childView
->ProcessEvent(event
) )
1779 // Only hand up to the parent if it's a menu command
1780 if (!event
.IsKindOf(CLASSINFO(wxCommandEvent
)) || !GetParent() || !GetParent()->ProcessEvent(event
))
1781 return wxEvtHandler::ProcessEvent(event
);
1789 void wxDocChildFrame::OnActivate(wxActivateEvent
& event
)
1791 wxFrame::OnActivate(event
);
1794 m_childView
->Activate(event
.GetActive());
1797 void wxDocChildFrame::OnCloseWindow(wxCloseEvent
& event
)
1802 if (!event
.CanVeto())
1803 ans
= TRUE
; // Must delete.
1805 ans
= m_childView
->Close(FALSE
); // FALSE means don't delete associated window
1809 m_childView
->Activate(FALSE
);
1811 m_childView
= (wxView
*) NULL
;
1812 m_childDocument
= (wxDocument
*) NULL
;
1823 // ----------------------------------------------------------------------------
1824 // Default parent frame
1825 // ----------------------------------------------------------------------------
1827 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1828 EVT_MENU(wxID_EXIT
, wxDocParentFrame::OnExit
)
1829 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE9
, wxDocParentFrame::OnMRUFile
)
1830 EVT_CLOSE(wxDocParentFrame::OnCloseWindow
)
1833 wxDocParentFrame::wxDocParentFrame(wxDocManager
*manager
,
1836 const wxString
& title
,
1840 const wxString
& name
)
1841 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1843 m_docManager
= manager
;
1846 void wxDocParentFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
1851 void wxDocParentFrame::OnMRUFile(wxCommandEvent
& event
)
1853 int n
= event
.GetId() - wxID_FILE1
; // the index in MRU list
1854 wxString
filename(m_docManager
->GetHistoryFile(n
));
1855 if ( !filename
.IsEmpty() )
1857 // verify that the file exists before doing anything else
1858 if ( wxFile::Exists(filename
) )
1861 if (!m_docManager
->CreateDocument(filename
, wxDOC_SILENT
))
1863 // remove the file from the MRU list. The user should already be notified.
1864 m_docManager
->RemoveFileFromHistory(n
);
1866 wxLogError(_("The file '%s' couldn't be opened.\nIt has been removed from the most recently used files list."),
1872 // remove the bogus filename from the MRU list and notify the user
1874 m_docManager
->RemoveFileFromHistory(n
);
1876 wxLogError(_("The file '%s' doesn't exist and couldn't be opened.\nIt has been removed from the most recently used files list."),
1882 // Extend event processing to search the view's event table
1883 bool wxDocParentFrame::ProcessEvent(wxEvent
& event
)
1885 // Try the document manager, then do default processing
1886 if (!m_docManager
|| !m_docManager
->ProcessEvent(event
))
1887 return wxEvtHandler::ProcessEvent(event
);
1892 // Define the behaviour for the frame closing
1893 // - must delete all frames except for the main one.
1894 void wxDocParentFrame::OnCloseWindow(wxCloseEvent
& event
)
1896 if (m_docManager
->Clear(!event
.CanVeto()))
1904 #if wxUSE_PRINTING_ARCHITECTURE
1906 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
)
1909 m_printoutView
= view
;
1912 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
1916 // Get the logical pixels per inch of screen and printer
1917 int ppiScreenX
, ppiScreenY
;
1918 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
1919 int ppiPrinterX
, ppiPrinterY
;
1920 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
1922 // This scales the DC so that the printout roughly represents the
1923 // the screen scaling. The text point size _should_ be the right size
1924 // but in fact is too small for some reason. This is a detail that will
1925 // need to be addressed at some point but can be fudged for the
1927 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
1929 // Now we have to check in case our real page size is reduced
1930 // (e.g. because we're drawing to a print preview memory DC)
1931 int pageWidth
, pageHeight
;
1933 dc
->GetSize(&w
, &h
);
1934 GetPageSizePixels(&pageWidth
, &pageHeight
);
1936 // If printer pageWidth == current DC width, then this doesn't
1937 // change. But w might be the preview bitmap width, so scale down.
1938 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
1939 dc
->SetUserScale(overallScale
, overallScale
);
1943 m_printoutView
->OnDraw(dc
);
1948 bool wxDocPrintout::HasPage(int pageNum
)
1950 return (pageNum
== 1);
1953 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
1955 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
1961 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
1969 #endif // wxUSE_PRINTING_ARCHITECTURE
1971 // ----------------------------------------------------------------------------
1972 // File history processor
1973 // ----------------------------------------------------------------------------
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 wxNode
* 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] = copystring(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 wxNode
* 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 wxNode
* 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::Node
*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
] = copystring((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 wxNode
* 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