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::GetNoHistoryFiles() const
1380 return m_fileHistory
->GetNoHistoryFiles();
1386 // Find out the document template via matching in the document file format
1387 // against that of the template
1388 wxDocTemplate
*wxDocManager::FindTemplateForPath(const wxString
& path
)
1390 wxDocTemplate
*theTemplate
= (wxDocTemplate
*) NULL
;
1392 // Find the template which this extension corresponds to
1393 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1395 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Item(i
)->GetData();
1396 if ( temp
->FileMatchesTemplate(path
) )
1405 // Try to get a more suitable parent frame than the top window,
1406 // for selection dialogs. Otherwise you may get an unexpected
1407 // window being activated when a dialog is shown.
1408 static wxWindow
* wxFindSuitableParent()
1410 wxWindow
* parent
= wxTheApp
->GetTopWindow();
1412 wxWindow
* focusWindow
= wxWindow::FindFocus();
1415 while (focusWindow
&&
1416 !focusWindow
->IsKindOf(CLASSINFO(wxDialog
)) &&
1417 !focusWindow
->IsKindOf(CLASSINFO(wxFrame
)))
1419 focusWindow
= focusWindow
->GetParent();
1422 parent
= focusWindow
;
1427 // Prompts user to open a file, using file specs in templates.
1428 // Must extend the file selector dialog or implement own; OR
1429 // match the extension to the template extension.
1431 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**templates
,
1432 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
1435 int WXUNUSED(noTemplates
),
1438 long WXUNUSED(flags
),
1439 bool WXUNUSED(save
))
1441 // We can only have multiple filters in Windows and GTK
1442 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
1446 for (i
= 0; i
< noTemplates
; i
++)
1448 if (templates
[i
]->IsVisible())
1450 // add a '|' to separate this filter from the previous one
1451 if ( !descrBuf
.IsEmpty() )
1452 descrBuf
<< wxT('|');
1454 descrBuf
<< templates
[i
]->GetDescription()
1455 << wxT(" (") << templates
[i
]->GetFileFilter() << wxT(") |")
1456 << templates
[i
]->GetFileFilter();
1460 wxString descrBuf
= wxT("*.*");
1463 int FilterIndex
= -1;
1465 wxWindow
* parent
= wxFindSuitableParent();
1467 wxString pathTmp
= wxFileSelectorEx(_("Select a file"),
1475 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)NULL
;
1476 if (!pathTmp
.IsEmpty())
1478 if (!wxFileExists(pathTmp
))
1481 if (!wxTheApp
->GetAppName().IsEmpty())
1482 msgTitle
= wxTheApp
->GetAppName();
1484 msgTitle
= wxString(_("File error"));
1486 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
1490 return (wxDocTemplate
*) NULL
;
1492 m_lastDirectory
= wxPathOnly(pathTmp
);
1496 // first choose the template using the extension, if this fails (i.e.
1497 // wxFileSelectorEx() didn't fill it), then use the path
1498 if ( FilterIndex
!= -1 )
1499 theTemplate
= templates
[FilterIndex
];
1501 theTemplate
= FindTemplateForPath(path
);
1504 // Since we do not add files with non-default extensions to the FileHistory this
1505 // can only happen if the application changes the allowed templates in runtime.
1506 (void)wxMessageBox(_("Sorry, the format for this file is unknown."),
1508 wxOK
| wxICON_EXCLAMATION
, wxFindSuitableParent());
1519 // In all other windowing systems, until we have more advanced
1520 // file selectors, we must select the document type (template) first, and
1521 // _then_ pop up the file selector.
1522 wxDocTemplate
*temp
= SelectDocumentType(templates
, noTemplates
);
1524 return (wxDocTemplate
*) NULL
;
1526 wxChar
*pathTmp
= wxFileSelector(_("Select a file"), wxT(""), wxT(""),
1527 temp
->GetDefaultExtension(),
1528 temp
->GetFileFilter(),
1529 0, wxTheApp
->GetTopWindow());
1537 return (wxDocTemplate
*) NULL
;
1541 wxDocTemplate
*wxDocManager::SelectDocumentType(wxDocTemplate
**templates
,
1542 int noTemplates
, bool sort
)
1544 wxArrayString
strings(sort
);
1545 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1549 for (i
= 0; i
< noTemplates
; i
++)
1551 if (templates
[i
]->IsVisible())
1555 for (j
= 0; j
< n
; j
++)
1557 //filter out NOT unique documents + view combinations
1558 if ( templates
[i
]->m_docTypeName
== data
[j
]->m_docTypeName
&&
1559 templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
1566 strings
.Add(templates
[i
]->m_description
);
1568 data
[n
] = templates
[i
];
1576 // Yes, this will be slow, but template lists
1577 // are typically short.
1579 n
= strings
.Count();
1580 for (i
= 0; i
< n
; i
++)
1582 for (j
= 0; j
< noTemplates
; j
++)
1584 if (strings
[i
] == templates
[j
]->m_description
)
1585 data
[i
] = templates
[j
];
1590 wxDocTemplate
*theTemplate
;
1595 // no visible templates, hence nothing to choose from
1600 // don't propose the user to choose if he heas no choice
1601 theTemplate
= data
[0];
1605 // propose the user to choose one of several
1606 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1608 _("Select a document template"),
1612 wxFindSuitableParent()
1621 wxDocTemplate
*wxDocManager::SelectViewType(wxDocTemplate
**templates
,
1622 int noTemplates
, bool sort
)
1624 wxArrayString
strings(sort
);
1625 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1629 for (i
= 0; i
< noTemplates
; i
++)
1631 wxDocTemplate
*templ
= templates
[i
];
1632 if ( templ
->IsVisible() && !templ
->GetViewName().empty() )
1636 for (j
= 0; j
< n
; j
++)
1638 //filter out NOT unique views
1639 if ( templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
)
1645 strings
.Add(templ
->m_viewTypeName
);
1654 // Yes, this will be slow, but template lists
1655 // are typically short.
1657 n
= strings
.Count();
1658 for (i
= 0; i
< n
; i
++)
1660 for (j
= 0; j
< noTemplates
; j
++)
1662 if (strings
[i
] == templates
[j
]->m_viewTypeName
)
1663 data
[i
] = templates
[j
];
1668 wxDocTemplate
*theTemplate
;
1670 // the same logic as above
1674 theTemplate
= (wxDocTemplate
*)NULL
;
1678 theTemplate
= data
[0];
1682 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1684 _("Select a document view"),
1688 wxFindSuitableParent()
1697 void wxDocManager::AssociateTemplate(wxDocTemplate
*temp
)
1699 if (!m_templates
.Member(temp
))
1700 m_templates
.Append(temp
);
1703 void wxDocManager::DisassociateTemplate(wxDocTemplate
*temp
)
1705 m_templates
.DeleteObject(temp
);
1708 // Add and remove a document from the manager's list
1709 void wxDocManager::AddDocument(wxDocument
*doc
)
1711 if (!m_docs
.Member(doc
))
1715 void wxDocManager::RemoveDocument(wxDocument
*doc
)
1717 m_docs
.DeleteObject(doc
);
1720 // Views or windows should inform the document manager
1721 // when a view is going in or out of focus
1722 void wxDocManager::ActivateView(wxView
*view
, bool activate
, bool WXUNUSED(deleting
))
1724 // If we're deactiving, and if we're not actually deleting the view, then
1725 // don't reset the current view because we may be going to
1726 // a window without a view.
1727 // WHAT DID I MEAN BY THAT EXACTLY?
1731 if (m_currentView == view)
1732 m_currentView = NULL;
1738 m_currentView
= view
;
1740 m_currentView
= (wxView
*) NULL
;
1744 // ----------------------------------------------------------------------------
1745 // Default document child frame
1746 // ----------------------------------------------------------------------------
1748 BEGIN_EVENT_TABLE(wxDocChildFrame
, wxFrame
)
1749 EVT_ACTIVATE(wxDocChildFrame::OnActivate
)
1750 EVT_CLOSE(wxDocChildFrame::OnCloseWindow
)
1753 wxDocChildFrame::wxDocChildFrame(wxDocument
*doc
,
1757 const wxString
& title
,
1761 const wxString
& name
)
1762 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1764 m_childDocument
= doc
;
1767 view
->SetFrame(this);
1770 wxDocChildFrame::~wxDocChildFrame()
1774 // Extend event processing to search the view's event table
1775 bool wxDocChildFrame::ProcessEvent(wxEvent
& event
)
1778 m_childView
->Activate(TRUE
);
1780 if ( !m_childView
|| ! m_childView
->ProcessEvent(event
) )
1782 // Only hand up to the parent if it's a menu command
1783 if (!event
.IsKindOf(CLASSINFO(wxCommandEvent
)) || !GetParent() || !GetParent()->ProcessEvent(event
))
1784 return wxEvtHandler::ProcessEvent(event
);
1792 void wxDocChildFrame::OnActivate(wxActivateEvent
& event
)
1794 wxFrame::OnActivate(event
);
1797 m_childView
->Activate(event
.GetActive());
1800 void wxDocChildFrame::OnCloseWindow(wxCloseEvent
& event
)
1805 if (!event
.CanVeto())
1806 ans
= TRUE
; // Must delete.
1808 ans
= m_childView
->Close(FALSE
); // FALSE means don't delete associated window
1812 m_childView
->Activate(FALSE
);
1814 m_childView
= (wxView
*) NULL
;
1815 m_childDocument
= (wxDocument
*) NULL
;
1826 // ----------------------------------------------------------------------------
1827 // Default parent frame
1828 // ----------------------------------------------------------------------------
1830 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1831 EVT_MENU(wxID_EXIT
, wxDocParentFrame::OnExit
)
1832 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE9
, wxDocParentFrame::OnMRUFile
)
1833 EVT_CLOSE(wxDocParentFrame::OnCloseWindow
)
1836 wxDocParentFrame::wxDocParentFrame(wxDocManager
*manager
,
1839 const wxString
& title
,
1843 const wxString
& name
)
1844 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1846 m_docManager
= manager
;
1849 void wxDocParentFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
1854 void wxDocParentFrame::OnMRUFile(wxCommandEvent
& event
)
1856 int n
= event
.GetId() - wxID_FILE1
; // the index in MRU list
1857 wxString
filename(m_docManager
->GetHistoryFile(n
));
1858 if ( !filename
.IsEmpty() )
1860 // verify that the file exists before doing anything else
1861 if ( wxFile::Exists(filename
) )
1864 if (!m_docManager
->CreateDocument(filename
, wxDOC_SILENT
))
1866 // remove the file from the MRU list. The user should already be notified.
1867 m_docManager
->RemoveFileFromHistory(n
);
1869 wxLogError(_("The file '%s' couldn't be opened.\nIt has been removed from the most recently used files list."),
1875 // remove the bogus filename from the MRU list and notify the user
1877 m_docManager
->RemoveFileFromHistory(n
);
1879 wxLogError(_("The file '%s' doesn't exist and couldn't be opened.\nIt has been removed from the most recently used files list."),
1885 // Extend event processing to search the view's event table
1886 bool wxDocParentFrame::ProcessEvent(wxEvent
& event
)
1888 // Try the document manager, then do default processing
1889 if (!m_docManager
|| !m_docManager
->ProcessEvent(event
))
1890 return wxEvtHandler::ProcessEvent(event
);
1895 // Define the behaviour for the frame closing
1896 // - must delete all frames except for the main one.
1897 void wxDocParentFrame::OnCloseWindow(wxCloseEvent
& event
)
1899 if (m_docManager
->Clear(!event
.CanVeto()))
1907 #if wxUSE_PRINTING_ARCHITECTURE
1909 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
)
1912 m_printoutView
= view
;
1915 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
1919 // Get the logical pixels per inch of screen and printer
1920 int ppiScreenX
, ppiScreenY
;
1921 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
1922 int ppiPrinterX
, ppiPrinterY
;
1923 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
1925 // This scales the DC so that the printout roughly represents the
1926 // the screen scaling. The text point size _should_ be the right size
1927 // but in fact is too small for some reason. This is a detail that will
1928 // need to be addressed at some point but can be fudged for the
1930 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
1932 // Now we have to check in case our real page size is reduced
1933 // (e.g. because we're drawing to a print preview memory DC)
1934 int pageWidth
, pageHeight
;
1936 dc
->GetSize(&w
, &h
);
1937 GetPageSizePixels(&pageWidth
, &pageHeight
);
1939 // If printer pageWidth == current DC width, then this doesn't
1940 // change. But w might be the preview bitmap width, so scale down.
1941 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
1942 dc
->SetUserScale(overallScale
, overallScale
);
1946 m_printoutView
->OnDraw(dc
);
1951 bool wxDocPrintout::HasPage(int pageNum
)
1953 return (pageNum
== 1);
1956 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
1958 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
1964 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
1972 #endif // wxUSE_PRINTING_ARCHITECTURE
1974 // ----------------------------------------------------------------------------
1975 // File history processor
1976 // ----------------------------------------------------------------------------
1978 wxFileHistory::wxFileHistory(size_t maxFiles
, wxWindowID idBase
)
1980 m_fileMaxFiles
= maxFiles
;
1983 m_fileHistory
= new wxChar
*[m_fileMaxFiles
];
1986 wxFileHistory::~wxFileHistory()
1989 for (i
= 0; i
< m_fileHistoryN
; i
++)
1990 delete[] m_fileHistory
[i
];
1991 delete[] m_fileHistory
;
1994 // File history management
1995 void wxFileHistory::AddFileToHistory(const wxString
& file
)
1999 // Check we don't already have this file
2000 for (i
= 0; i
< m_fileHistoryN
; i
++)
2002 #if defined( __WXMSW__ ) // Add any other OSes with case insensitive file names
2003 wxString testString
;
2004 if ( m_fileHistory
[i
] )
2005 testString
= m_fileHistory
[i
];
2006 if ( m_fileHistory
[i
] && ( file
.Lower() == testString
.Lower() ) )
2008 if ( m_fileHistory
[i
] && ( file
== m_fileHistory
[i
] ) )
2011 // we do have it, move it to the top of the history
2012 RemoveFileFromHistory (i
);
2013 AddFileToHistory (file
);
2018 // if we already have a full history, delete the one at the end
2019 if ( m_fileMaxFiles
== m_fileHistoryN
)
2021 RemoveFileFromHistory (m_fileHistoryN
- 1);
2022 AddFileToHistory (file
);
2026 // Add to the project file history:
2027 // Move existing files (if any) down so we can insert file at beginning.
2028 if (m_fileHistoryN
< m_fileMaxFiles
)
2030 wxNode
* node
= m_fileMenus
.GetFirst();
2033 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2034 if ( m_fileHistoryN
== 0 && menu
->GetMenuItemCount() )
2036 menu
->AppendSeparator();
2038 menu
->Append(m_idBase
+m_fileHistoryN
, _("[EMPTY]"));
2039 node
= node
->GetNext();
2043 // Shuffle filenames down
2044 for (i
= (m_fileHistoryN
-1); i
> 0; i
--)
2046 m_fileHistory
[i
] = m_fileHistory
[i
-1];
2048 m_fileHistory
[0] = copystring(file
);
2050 // this is the directory of the last opened file
2051 wxString pathCurrent
;
2052 wxSplitPath( m_fileHistory
[0], &pathCurrent
, NULL
, NULL
);
2053 for (i
= 0; i
< m_fileHistoryN
; i
++)
2055 if ( m_fileHistory
[i
] )
2057 // if in same directory just show the filename; otherwise the full
2059 wxString pathInMenu
, path
, filename
, ext
;
2060 wxSplitPath( m_fileHistory
[i
], &path
, &filename
, &ext
);
2061 if ( path
== pathCurrent
)
2063 pathInMenu
= filename
;
2065 pathInMenu
= pathInMenu
+ wxFILE_SEP_EXT
+ ext
;
2069 // absolute path; could also set relative path
2070 pathInMenu
= m_fileHistory
[i
];
2074 buf
.Printf(s_MRUEntryFormat
, i
+ 1, pathInMenu
.c_str());
2075 wxNode
* node
= m_fileMenus
.GetFirst();
2078 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2079 menu
->SetLabel(m_idBase
+ i
, buf
);
2080 node
= node
->GetNext();
2086 void wxFileHistory::RemoveFileFromHistory(size_t i
)
2088 wxCHECK_RET( i
< m_fileHistoryN
,
2089 wxT("invalid index in wxFileHistory::RemoveFileFromHistory") );
2091 // delete the element from the array (could use memmove() too...)
2092 delete [] m_fileHistory
[i
];
2095 for ( j
= i
; j
< m_fileHistoryN
- 1; j
++ )
2097 m_fileHistory
[j
] = m_fileHistory
[j
+ 1];
2100 wxNode
* node
= m_fileMenus
.GetFirst();
2103 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2105 // shuffle filenames up
2107 for ( j
= i
; j
< m_fileHistoryN
- 1; j
++ )
2109 buf
.Printf(s_MRUEntryFormat
, j
+ 1, m_fileHistory
[j
]);
2110 menu
->SetLabel(m_idBase
+ j
, buf
);
2113 node
= node
->GetNext();
2115 // delete the last menu item which is unused now
2116 wxWindowID lastItemId
= m_idBase
+ m_fileHistoryN
- 1;
2117 if (menu
->FindItem(lastItemId
))
2119 menu
->Delete(lastItemId
);
2122 // delete the last separator too if no more files are left
2123 if ( m_fileHistoryN
== 1 )
2125 wxMenuItemList::Node
*node
= menu
->GetMenuItems().GetLast();
2128 wxMenuItem
*menuItem
= node
->GetData();
2129 if ( menuItem
->IsSeparator() )
2131 menu
->Delete(menuItem
);
2133 //else: should we search backwards for the last separator?
2135 //else: menu is empty somehow
2142 wxString
wxFileHistory::GetHistoryFile(size_t i
) const
2145 if ( i
< m_fileHistoryN
)
2147 s
= m_fileHistory
[i
];
2151 wxFAIL_MSG( wxT("bad index in wxFileHistory::GetHistoryFile") );
2157 void wxFileHistory::UseMenu(wxMenu
*menu
)
2159 if (!m_fileMenus
.Member(menu
))
2160 m_fileMenus
.Append(menu
);
2163 void wxFileHistory::RemoveMenu(wxMenu
*menu
)
2165 m_fileMenus
.DeleteObject(menu
);
2169 void wxFileHistory::Load(wxConfigBase
& config
)
2173 buf
.Printf(wxT("file%d"), (int)m_fileHistoryN
+1);
2174 wxString historyFile
;
2175 while ((m_fileHistoryN
< m_fileMaxFiles
) && config
.Read(buf
, &historyFile
) && (historyFile
!= wxT("")))
2177 m_fileHistory
[m_fileHistoryN
] = copystring((const wxChar
*) historyFile
);
2179 buf
.Printf(wxT("file%d"), (int)m_fileHistoryN
+1);
2180 historyFile
= wxT("");
2185 void wxFileHistory::Save(wxConfigBase
& config
)
2188 for (i
= 0; i
< m_fileMaxFiles
; i
++)
2191 buf
.Printf(wxT("file%d"), (int)i
+1);
2192 if (i
< m_fileHistoryN
)
2193 config
.Write(buf
, wxString(m_fileHistory
[i
]));
2195 config
.Write(buf
, wxEmptyString
);
2198 #endif // wxUSE_CONFIG
2200 void wxFileHistory::AddFilesToMenu()
2202 if (m_fileHistoryN
> 0)
2204 wxNode
* node
= m_fileMenus
.GetFirst();
2207 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2208 if (menu
->GetMenuItemCount())
2210 menu
->AppendSeparator();
2214 for (i
= 0; i
< m_fileHistoryN
; i
++)
2216 if (m_fileHistory
[i
])
2219 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
2220 menu
->Append(m_idBase
+i
, buf
);
2223 node
= node
->GetNext();
2228 void wxFileHistory::AddFilesToMenu(wxMenu
* menu
)
2230 if (m_fileHistoryN
> 0)
2232 if (menu
->GetMenuItemCount())
2234 menu
->AppendSeparator();
2238 for (i
= 0; i
< m_fileHistoryN
; i
++)
2240 if (m_fileHistory
[i
])
2243 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
2244 menu
->Append(m_idBase
+i
, buf
);
2250 // ----------------------------------------------------------------------------
2251 // Permits compatibility with existing file formats and functions that
2252 // manipulate files directly
2253 // ----------------------------------------------------------------------------
2255 #if wxUSE_STD_IOSTREAM
2257 bool wxTransferFileToStream(const wxString
& filename
, wxSTD ostream
& stream
)
2259 wxFFile
file(filename
, _T("rb"));
2260 if ( !file
.IsOpened() )
2268 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2272 stream
.write(buf
, nRead
);
2276 while ( !file
.Eof() );
2281 bool wxTransferStreamToFile(wxSTD istream
& stream
, const wxString
& filename
)
2283 wxFFile
file(filename
, _T("wb"));
2284 if ( !file
.IsOpened() )
2290 stream
.read(buf
, WXSIZEOF(buf
));
2291 if ( !stream
.bad() ) // fail may be set on EOF, don't use operator!()
2293 if ( !file
.Write(buf
, stream
.gcount()) )
2297 while ( !stream
.eof() );
2302 #else // !wxUSE_STD_IOSTREAM
2304 bool wxTransferFileToStream(const wxString
& filename
, wxOutputStream
& stream
)
2306 wxFFile
file(filename
, _T("rb"));
2307 if ( !file
.IsOpened() )
2315 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2319 stream
.Write(buf
, nRead
);
2323 while ( !file
.Eof() );
2328 bool wxTransferStreamToFile(wxInputStream
& stream
, const wxString
& filename
)
2330 wxFFile
file(filename
, _T("wb"));
2331 if ( !file
.IsOpened() )
2337 stream
.Read(buf
, WXSIZEOF(buf
));
2339 const size_t nRead
= stream
.LastRead();
2340 if ( !nRead
|| !file
.Write(buf
, nRead
) )
2343 while ( !stream
.Eof() );
2348 #endif // wxUSE_STD_IOSTREAM/!wxUSE_STD_IOSTREAM
2350 #endif // wxUSE_DOC_VIEW_ARCHITECTURE