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
& event
)
969 wxDocument
*doc
= GetCurrentDocument();
972 if (doc
->GetCommandProcessor())
973 doc
->GetCommandProcessor()->Undo();
978 void wxDocManager::OnRedo(wxCommandEvent
& event
)
980 wxDocument
*doc
= GetCurrentDocument();
983 if (doc
->GetCommandProcessor())
984 doc
->GetCommandProcessor()->Redo();
989 // Handlers for UI update commands
991 void wxDocManager::OnUpdateFileOpen(wxUpdateUIEvent
& event
)
993 event
.Enable( TRUE
);
996 void wxDocManager::OnUpdateFileClose(wxUpdateUIEvent
& event
)
998 wxDocument
*doc
= GetCurrentDocument();
999 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1002 void wxDocManager::OnUpdateFileRevert(wxUpdateUIEvent
& event
)
1004 wxDocument
*doc
= GetCurrentDocument();
1005 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1008 void wxDocManager::OnUpdateFileNew(wxUpdateUIEvent
& event
)
1010 event
.Enable( TRUE
);
1013 void wxDocManager::OnUpdateFileSave(wxUpdateUIEvent
& event
)
1015 wxDocument
*doc
= GetCurrentDocument();
1016 event
.Enable( doc
&& doc
->IsModified() );
1019 void wxDocManager::OnUpdateFileSaveAs(wxUpdateUIEvent
& event
)
1021 wxDocument
*doc
= GetCurrentDocument();
1022 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1025 void wxDocManager::OnUpdateUndo(wxUpdateUIEvent
& event
)
1027 wxDocument
*doc
= GetCurrentDocument();
1029 event
.Enable(FALSE
);
1030 else if (!doc
->GetCommandProcessor())
1034 event
.Enable( doc
->GetCommandProcessor()->CanUndo() );
1035 doc
->GetCommandProcessor()->SetMenuStrings();
1039 void wxDocManager::OnUpdateRedo(wxUpdateUIEvent
& event
)
1041 wxDocument
*doc
= GetCurrentDocument();
1043 event
.Enable(FALSE
);
1044 else if (!doc
->GetCommandProcessor())
1048 event
.Enable( doc
->GetCommandProcessor()->CanRedo() );
1049 doc
->GetCommandProcessor()->SetMenuStrings();
1053 void wxDocManager::OnUpdatePrint(wxUpdateUIEvent
& event
)
1055 wxDocument
*doc
= GetCurrentDocument();
1056 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1059 void wxDocManager::OnUpdatePrintSetup(wxUpdateUIEvent
& event
)
1061 event
.Enable( TRUE
);
1064 void wxDocManager::OnUpdatePreview(wxUpdateUIEvent
& event
)
1066 wxDocument
*doc
= GetCurrentDocument();
1067 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1070 wxView
*wxDocManager::GetCurrentView() const
1073 return m_currentView
;
1074 if (m_docs
.GetCount() == 1)
1076 wxDocument
* doc
= (wxDocument
*) m_docs
.GetFirst()->GetData();
1077 return doc
->GetFirstView();
1079 return (wxView
*) NULL
;
1082 // Extend event processing to search the view's event table
1083 bool wxDocManager::ProcessEvent(wxEvent
& event
)
1085 wxView
* view
= GetCurrentView();
1088 if (view
->ProcessEvent(event
))
1091 return wxEvtHandler::ProcessEvent(event
);
1094 wxDocument
*wxDocManager::CreateDocument(const wxString
& path
, long flags
)
1096 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.GetCount()];
1099 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1101 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Item(i
)->GetData());
1102 if (temp
->IsVisible())
1104 templates
[n
] = temp
;
1111 return (wxDocument
*) NULL
;
1114 wxDocument
* docToClose
= NULL
;
1116 // If we've reached the max number of docs, close the
1118 if ( (int)GetDocuments().GetCount() >= m_maxDocsOpen
)
1120 wxDocument
*doc
= (wxDocument
*)GetDocuments().GetFirst()->GetData();
1124 // New document: user chooses a template, unless there's only one.
1125 if (flags
& wxDOC_NEW
)
1131 if (!CloseDocument(docToClose
, FALSE
))
1138 wxDocTemplate
*temp
= templates
[0];
1140 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
1144 newDoc
->SetDocumentName(temp
->GetDocumentName());
1145 newDoc
->SetDocumentTemplate(temp
);
1146 newDoc
->OnNewDocument();
1151 wxDocTemplate
*temp
= SelectDocumentType(templates
, n
);
1157 if (!CloseDocument(docToClose
, FALSE
))
1163 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
1167 newDoc
->SetDocumentName(temp
->GetDocumentName());
1168 newDoc
->SetDocumentTemplate(temp
);
1169 newDoc
->OnNewDocument();
1174 return (wxDocument
*) NULL
;
1177 // Existing document
1178 wxDocTemplate
*temp
= (wxDocTemplate
*) NULL
;
1180 wxString
path2(wxT(""));
1181 if (path
!= wxT(""))
1184 if (flags
& wxDOC_SILENT
)
1186 temp
= FindTemplateForPath(path2
);
1189 // Since we do not add files with non-default extensions to the FileHistory this
1190 // can only happen if the application changes the allowed templates in runtime.
1191 (void)wxMessageBox(_("Sorry, the format for this file is unknown."),
1193 wxOK
| wxICON_EXCLAMATION
, wxFindSuitableParent());
1197 temp
= SelectDocumentPath(templates
, n
, path2
, flags
);
1205 if (!CloseDocument(docToClose
, FALSE
))
1211 wxDocument
*newDoc
= temp
->CreateDocument(path2
, flags
);
1214 newDoc
->SetDocumentName(temp
->GetDocumentName());
1215 newDoc
->SetDocumentTemplate(temp
);
1216 if (!newDoc
->OnOpenDocument(path2
))
1218 newDoc
->DeleteAllViews();
1219 // delete newDoc; // Implicitly deleted by DeleteAllViews
1220 return (wxDocument
*) NULL
;
1222 // A file that doesn't use the default extension of its document
1223 // template cannot be opened via the FileHistory, so we do not
1225 if (temp
->FileMatchesTemplate(path2
))
1226 AddFileToHistory(path2
);
1231 return (wxDocument
*) NULL
;
1234 wxView
*wxDocManager::CreateView(wxDocument
*doc
, long flags
)
1236 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.GetCount()];
1239 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1241 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Item(i
)->GetData());
1242 if (temp
->IsVisible())
1244 if (temp
->GetDocumentName() == doc
->GetDocumentName())
1246 templates
[n
] = temp
;
1254 return (wxView
*) NULL
;
1258 wxDocTemplate
*temp
= templates
[0];
1260 wxView
*view
= temp
->CreateView(doc
, flags
);
1262 view
->SetViewName(temp
->GetViewName());
1266 wxDocTemplate
*temp
= SelectViewType(templates
, n
);
1270 wxView
*view
= temp
->CreateView(doc
, flags
);
1272 view
->SetViewName(temp
->GetViewName());
1276 return (wxView
*) NULL
;
1279 // Not yet implemented
1280 void wxDocManager::DeleteTemplate(wxDocTemplate
*WXUNUSED(temp
), long WXUNUSED(flags
))
1284 // Not yet implemented
1285 bool wxDocManager::FlushDoc(wxDocument
*WXUNUSED(doc
))
1290 wxDocument
*wxDocManager::GetCurrentDocument() const
1292 wxView
*view
= GetCurrentView();
1294 return view
->GetDocument();
1296 return (wxDocument
*) NULL
;
1299 // Make a default document name
1300 bool wxDocManager::MakeDefaultName(wxString
& name
)
1302 name
.Printf(_("unnamed%d"), m_defaultDocumentNameCounter
);
1303 m_defaultDocumentNameCounter
++;
1308 // Make a frame title (override this to do something different)
1309 // If docName is empty, a document is not currently active.
1310 wxString
wxDocManager::MakeFrameTitle(wxDocument
* doc
)
1312 wxString appName
= wxTheApp
->GetAppName();
1319 doc
->GetPrintableName(docName
);
1320 title
= docName
+ wxString(_(" - ")) + appName
;
1326 // Not yet implemented
1327 wxDocTemplate
*wxDocManager::MatchTemplate(const wxString
& WXUNUSED(path
))
1329 return (wxDocTemplate
*) NULL
;
1332 // File history management
1333 void wxDocManager::AddFileToHistory(const wxString
& file
)
1336 m_fileHistory
->AddFileToHistory(file
);
1339 void wxDocManager::RemoveFileFromHistory(size_t i
)
1342 m_fileHistory
->RemoveFileFromHistory(i
);
1345 wxString
wxDocManager::GetHistoryFile(size_t i
) const
1350 histFile
= m_fileHistory
->GetHistoryFile(i
);
1355 void wxDocManager::FileHistoryUseMenu(wxMenu
*menu
)
1358 m_fileHistory
->UseMenu(menu
);
1361 void wxDocManager::FileHistoryRemoveMenu(wxMenu
*menu
)
1364 m_fileHistory
->RemoveMenu(menu
);
1368 void wxDocManager::FileHistoryLoad(wxConfigBase
& config
)
1371 m_fileHistory
->Load(config
);
1374 void wxDocManager::FileHistorySave(wxConfigBase
& config
)
1377 m_fileHistory
->Save(config
);
1381 void wxDocManager::FileHistoryAddFilesToMenu(wxMenu
* menu
)
1384 m_fileHistory
->AddFilesToMenu(menu
);
1387 void wxDocManager::FileHistoryAddFilesToMenu()
1390 m_fileHistory
->AddFilesToMenu();
1393 size_t wxDocManager::GetHistoryFilesCount() const
1395 return m_fileHistory
? m_fileHistory
->GetCount() : 0;
1399 // Find out the document template via matching in the document file format
1400 // against that of the template
1401 wxDocTemplate
*wxDocManager::FindTemplateForPath(const wxString
& path
)
1403 wxDocTemplate
*theTemplate
= (wxDocTemplate
*) NULL
;
1405 // Find the template which this extension corresponds to
1406 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1408 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Item(i
)->GetData();
1409 if ( temp
->FileMatchesTemplate(path
) )
1418 // Try to get a more suitable parent frame than the top window,
1419 // for selection dialogs. Otherwise you may get an unexpected
1420 // window being activated when a dialog is shown.
1421 static wxWindow
* wxFindSuitableParent()
1423 wxWindow
* parent
= wxTheApp
->GetTopWindow();
1425 wxWindow
* focusWindow
= wxWindow::FindFocus();
1428 while (focusWindow
&&
1429 !focusWindow
->IsKindOf(CLASSINFO(wxDialog
)) &&
1430 !focusWindow
->IsKindOf(CLASSINFO(wxFrame
)))
1432 focusWindow
= focusWindow
->GetParent();
1435 parent
= focusWindow
;
1440 // Prompts user to open a file, using file specs in templates.
1441 // Must extend the file selector dialog or implement own; OR
1442 // match the extension to the template extension.
1444 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**templates
,
1445 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
1448 int WXUNUSED(noTemplates
),
1451 long WXUNUSED(flags
),
1452 bool WXUNUSED(save
))
1454 // We can only have multiple filters in Windows and GTK
1455 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
1459 for (i
= 0; i
< noTemplates
; i
++)
1461 if (templates
[i
]->IsVisible())
1463 // add a '|' to separate this filter from the previous one
1464 if ( !descrBuf
.IsEmpty() )
1465 descrBuf
<< wxT('|');
1467 descrBuf
<< templates
[i
]->GetDescription()
1468 << wxT(" (") << templates
[i
]->GetFileFilter() << wxT(") |")
1469 << templates
[i
]->GetFileFilter();
1473 wxString descrBuf
= wxT("*.*");
1476 int FilterIndex
= -1;
1478 wxWindow
* parent
= wxFindSuitableParent();
1480 wxString pathTmp
= wxFileSelectorEx(_("Select a file"),
1488 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)NULL
;
1489 if (!pathTmp
.IsEmpty())
1491 if (!wxFileExists(pathTmp
))
1494 if (!wxTheApp
->GetAppName().IsEmpty())
1495 msgTitle
= wxTheApp
->GetAppName();
1497 msgTitle
= wxString(_("File error"));
1499 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
1503 return (wxDocTemplate
*) NULL
;
1505 m_lastDirectory
= wxPathOnly(pathTmp
);
1509 // first choose the template using the extension, if this fails (i.e.
1510 // wxFileSelectorEx() didn't fill it), then use the path
1511 if ( FilterIndex
!= -1 )
1512 theTemplate
= templates
[FilterIndex
];
1514 theTemplate
= FindTemplateForPath(path
);
1517 // Since we do not add files with non-default extensions to the FileHistory this
1518 // can only happen if the application changes the allowed templates in runtime.
1519 (void)wxMessageBox(_("Sorry, the format for this file is unknown."),
1521 wxOK
| wxICON_EXCLAMATION
, wxFindSuitableParent());
1532 // In all other windowing systems, until we have more advanced
1533 // file selectors, we must select the document type (template) first, and
1534 // _then_ pop up the file selector.
1535 wxDocTemplate
*temp
= SelectDocumentType(templates
, noTemplates
);
1537 return (wxDocTemplate
*) NULL
;
1539 wxChar
*pathTmp
= wxFileSelector(_("Select a file"), wxT(""), wxT(""),
1540 temp
->GetDefaultExtension(),
1541 temp
->GetFileFilter(),
1542 0, wxTheApp
->GetTopWindow());
1550 return (wxDocTemplate
*) NULL
;
1554 wxDocTemplate
*wxDocManager::SelectDocumentType(wxDocTemplate
**templates
,
1555 int noTemplates
, bool sort
)
1557 wxArrayString
strings(sort
);
1558 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1562 for (i
= 0; i
< noTemplates
; i
++)
1564 if (templates
[i
]->IsVisible())
1568 for (j
= 0; j
< n
; j
++)
1570 //filter out NOT unique documents + view combinations
1571 if ( templates
[i
]->m_docTypeName
== data
[j
]->m_docTypeName
&&
1572 templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
1579 strings
.Add(templates
[i
]->m_description
);
1581 data
[n
] = templates
[i
];
1589 // Yes, this will be slow, but template lists
1590 // are typically short.
1592 n
= strings
.Count();
1593 for (i
= 0; i
< n
; i
++)
1595 for (j
= 0; j
< noTemplates
; j
++)
1597 if (strings
[i
] == templates
[j
]->m_description
)
1598 data
[i
] = templates
[j
];
1603 wxDocTemplate
*theTemplate
;
1608 // no visible templates, hence nothing to choose from
1613 // don't propose the user to choose if he heas no choice
1614 theTemplate
= data
[0];
1618 // propose the user to choose one of several
1619 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1621 _("Select a document template"),
1625 wxFindSuitableParent()
1634 wxDocTemplate
*wxDocManager::SelectViewType(wxDocTemplate
**templates
,
1635 int noTemplates
, bool sort
)
1637 wxArrayString
strings(sort
);
1638 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1642 for (i
= 0; i
< noTemplates
; i
++)
1644 wxDocTemplate
*templ
= templates
[i
];
1645 if ( templ
->IsVisible() && !templ
->GetViewName().empty() )
1649 for (j
= 0; j
< n
; j
++)
1651 //filter out NOT unique views
1652 if ( templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
)
1658 strings
.Add(templ
->m_viewTypeName
);
1667 // Yes, this will be slow, but template lists
1668 // are typically short.
1670 n
= strings
.Count();
1671 for (i
= 0; i
< n
; i
++)
1673 for (j
= 0; j
< noTemplates
; j
++)
1675 if (strings
[i
] == templates
[j
]->m_viewTypeName
)
1676 data
[i
] = templates
[j
];
1681 wxDocTemplate
*theTemplate
;
1683 // the same logic as above
1687 theTemplate
= (wxDocTemplate
*)NULL
;
1691 theTemplate
= data
[0];
1695 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1697 _("Select a document view"),
1701 wxFindSuitableParent()
1710 void wxDocManager::AssociateTemplate(wxDocTemplate
*temp
)
1712 if (!m_templates
.Member(temp
))
1713 m_templates
.Append(temp
);
1716 void wxDocManager::DisassociateTemplate(wxDocTemplate
*temp
)
1718 m_templates
.DeleteObject(temp
);
1721 // Add and remove a document from the manager's list
1722 void wxDocManager::AddDocument(wxDocument
*doc
)
1724 if (!m_docs
.Member(doc
))
1728 void wxDocManager::RemoveDocument(wxDocument
*doc
)
1730 m_docs
.DeleteObject(doc
);
1733 // Views or windows should inform the document manager
1734 // when a view is going in or out of focus
1735 void wxDocManager::ActivateView(wxView
*view
, bool activate
, bool WXUNUSED(deleting
))
1737 // If we're deactiving, and if we're not actually deleting the view, then
1738 // don't reset the current view because we may be going to
1739 // a window without a view.
1740 // WHAT DID I MEAN BY THAT EXACTLY?
1744 if (m_currentView == view)
1745 m_currentView = NULL;
1751 m_currentView
= view
;
1753 m_currentView
= (wxView
*) NULL
;
1757 // ----------------------------------------------------------------------------
1758 // Default document child frame
1759 // ----------------------------------------------------------------------------
1761 BEGIN_EVENT_TABLE(wxDocChildFrame
, wxFrame
)
1762 EVT_ACTIVATE(wxDocChildFrame::OnActivate
)
1763 EVT_CLOSE(wxDocChildFrame::OnCloseWindow
)
1766 wxDocChildFrame::wxDocChildFrame(wxDocument
*doc
,
1770 const wxString
& title
,
1774 const wxString
& name
)
1775 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1777 m_childDocument
= doc
;
1780 view
->SetFrame(this);
1783 wxDocChildFrame::~wxDocChildFrame()
1787 // Extend event processing to search the view's event table
1788 bool wxDocChildFrame::ProcessEvent(wxEvent
& event
)
1791 m_childView
->Activate(TRUE
);
1793 if ( !m_childView
|| ! m_childView
->ProcessEvent(event
) )
1795 // Only hand up to the parent if it's a menu command
1796 if (!event
.IsKindOf(CLASSINFO(wxCommandEvent
)) || !GetParent() || !GetParent()->ProcessEvent(event
))
1797 return wxEvtHandler::ProcessEvent(event
);
1805 void wxDocChildFrame::OnActivate(wxActivateEvent
& event
)
1807 wxFrame::OnActivate(event
);
1810 m_childView
->Activate(event
.GetActive());
1813 void wxDocChildFrame::OnCloseWindow(wxCloseEvent
& event
)
1818 if (!event
.CanVeto())
1819 ans
= TRUE
; // Must delete.
1821 ans
= m_childView
->Close(FALSE
); // FALSE means don't delete associated window
1825 m_childView
->Activate(FALSE
);
1827 m_childView
= (wxView
*) NULL
;
1828 m_childDocument
= (wxDocument
*) NULL
;
1839 // ----------------------------------------------------------------------------
1840 // Default parent frame
1841 // ----------------------------------------------------------------------------
1843 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1844 EVT_MENU(wxID_EXIT
, wxDocParentFrame::OnExit
)
1845 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE9
, wxDocParentFrame::OnMRUFile
)
1846 EVT_CLOSE(wxDocParentFrame::OnCloseWindow
)
1849 wxDocParentFrame::wxDocParentFrame(wxDocManager
*manager
,
1852 const wxString
& title
,
1856 const wxString
& name
)
1857 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1859 m_docManager
= manager
;
1862 void wxDocParentFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
1867 void wxDocParentFrame::OnMRUFile(wxCommandEvent
& event
)
1869 int n
= event
.GetId() - wxID_FILE1
; // the index in MRU list
1870 wxString
filename(m_docManager
->GetHistoryFile(n
));
1871 if ( !filename
.IsEmpty() )
1873 // verify that the file exists before doing anything else
1874 if ( wxFile::Exists(filename
) )
1877 if (!m_docManager
->CreateDocument(filename
, wxDOC_SILENT
))
1879 // remove the file from the MRU list. The user should already be notified.
1880 m_docManager
->RemoveFileFromHistory(n
);
1882 wxLogError(_("The file '%s' couldn't be opened.\nIt has been removed from the most recently used files list."),
1888 // remove the bogus filename from the MRU list and notify the user
1890 m_docManager
->RemoveFileFromHistory(n
);
1892 wxLogError(_("The file '%s' doesn't exist and couldn't be opened.\nIt has been removed from the most recently used files list."),
1898 // Extend event processing to search the view's event table
1899 bool wxDocParentFrame::ProcessEvent(wxEvent
& event
)
1901 // Try the document manager, then do default processing
1902 if (!m_docManager
|| !m_docManager
->ProcessEvent(event
))
1903 return wxEvtHandler::ProcessEvent(event
);
1908 // Define the behaviour for the frame closing
1909 // - must delete all frames except for the main one.
1910 void wxDocParentFrame::OnCloseWindow(wxCloseEvent
& event
)
1912 if (m_docManager
->Clear(!event
.CanVeto()))
1920 #if wxUSE_PRINTING_ARCHITECTURE
1922 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
)
1925 m_printoutView
= view
;
1928 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
1932 // Get the logical pixels per inch of screen and printer
1933 int ppiScreenX
, ppiScreenY
;
1934 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
1935 int ppiPrinterX
, ppiPrinterY
;
1936 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
1938 // This scales the DC so that the printout roughly represents the
1939 // the screen scaling. The text point size _should_ be the right size
1940 // but in fact is too small for some reason. This is a detail that will
1941 // need to be addressed at some point but can be fudged for the
1943 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
1945 // Now we have to check in case our real page size is reduced
1946 // (e.g. because we're drawing to a print preview memory DC)
1947 int pageWidth
, pageHeight
;
1949 dc
->GetSize(&w
, &h
);
1950 GetPageSizePixels(&pageWidth
, &pageHeight
);
1952 // If printer pageWidth == current DC width, then this doesn't
1953 // change. But w might be the preview bitmap width, so scale down.
1954 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
1955 dc
->SetUserScale(overallScale
, overallScale
);
1959 m_printoutView
->OnDraw(dc
);
1964 bool wxDocPrintout::HasPage(int pageNum
)
1966 return (pageNum
== 1);
1969 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
1971 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
1977 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
1985 #endif // wxUSE_PRINTING_ARCHITECTURE
1987 // ----------------------------------------------------------------------------
1988 // File history processor
1989 // ----------------------------------------------------------------------------
1991 wxFileHistory::wxFileHistory(size_t maxFiles
, wxWindowID idBase
)
1993 m_fileMaxFiles
= maxFiles
;
1996 m_fileHistory
= new wxChar
*[m_fileMaxFiles
];
1999 wxFileHistory::~wxFileHistory()
2002 for (i
= 0; i
< m_fileHistoryN
; i
++)
2003 delete[] m_fileHistory
[i
];
2004 delete[] m_fileHistory
;
2007 // File history management
2008 void wxFileHistory::AddFileToHistory(const wxString
& file
)
2012 // Check we don't already have this file
2013 for (i
= 0; i
< m_fileHistoryN
; i
++)
2015 #if defined( __WXMSW__ ) // Add any other OSes with case insensitive file names
2016 wxString testString
;
2017 if ( m_fileHistory
[i
] )
2018 testString
= m_fileHistory
[i
];
2019 if ( m_fileHistory
[i
] && ( file
.Lower() == testString
.Lower() ) )
2021 if ( m_fileHistory
[i
] && ( file
== m_fileHistory
[i
] ) )
2024 // we do have it, move it to the top of the history
2025 RemoveFileFromHistory (i
);
2026 AddFileToHistory (file
);
2031 // if we already have a full history, delete the one at the end
2032 if ( m_fileMaxFiles
== m_fileHistoryN
)
2034 RemoveFileFromHistory (m_fileHistoryN
- 1);
2035 AddFileToHistory (file
);
2039 // Add to the project file history:
2040 // Move existing files (if any) down so we can insert file at beginning.
2041 if (m_fileHistoryN
< m_fileMaxFiles
)
2043 wxNode
* node
= m_fileMenus
.GetFirst();
2046 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2047 if ( m_fileHistoryN
== 0 && menu
->GetMenuItemCount() )
2049 menu
->AppendSeparator();
2051 menu
->Append(m_idBase
+m_fileHistoryN
, _("[EMPTY]"));
2052 node
= node
->GetNext();
2056 // Shuffle filenames down
2057 for (i
= (m_fileHistoryN
-1); i
> 0; i
--)
2059 m_fileHistory
[i
] = m_fileHistory
[i
-1];
2061 m_fileHistory
[0] = copystring(file
);
2063 // this is the directory of the last opened file
2064 wxString pathCurrent
;
2065 wxSplitPath( m_fileHistory
[0], &pathCurrent
, NULL
, NULL
);
2066 for (i
= 0; i
< m_fileHistoryN
; i
++)
2068 if ( m_fileHistory
[i
] )
2070 // if in same directory just show the filename; otherwise the full
2072 wxString pathInMenu
, path
, filename
, ext
;
2073 wxSplitPath( m_fileHistory
[i
], &path
, &filename
, &ext
);
2074 if ( path
== pathCurrent
)
2076 pathInMenu
= filename
;
2078 pathInMenu
= pathInMenu
+ wxFILE_SEP_EXT
+ ext
;
2082 // absolute path; could also set relative path
2083 pathInMenu
= m_fileHistory
[i
];
2087 buf
.Printf(s_MRUEntryFormat
, i
+ 1, pathInMenu
.c_str());
2088 wxNode
* node
= m_fileMenus
.GetFirst();
2091 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2092 menu
->SetLabel(m_idBase
+ i
, buf
);
2093 node
= node
->GetNext();
2099 void wxFileHistory::RemoveFileFromHistory(size_t i
)
2101 wxCHECK_RET( i
< m_fileHistoryN
,
2102 wxT("invalid index in wxFileHistory::RemoveFileFromHistory") );
2104 // delete the element from the array (could use memmove() too...)
2105 delete [] m_fileHistory
[i
];
2108 for ( j
= i
; j
< m_fileHistoryN
- 1; j
++ )
2110 m_fileHistory
[j
] = m_fileHistory
[j
+ 1];
2113 wxNode
* node
= m_fileMenus
.GetFirst();
2116 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2118 // shuffle filenames up
2120 for ( j
= i
; j
< m_fileHistoryN
- 1; j
++ )
2122 buf
.Printf(s_MRUEntryFormat
, j
+ 1, m_fileHistory
[j
]);
2123 menu
->SetLabel(m_idBase
+ j
, buf
);
2126 node
= node
->GetNext();
2128 // delete the last menu item which is unused now
2129 wxWindowID lastItemId
= m_idBase
+ m_fileHistoryN
- 1;
2130 if (menu
->FindItem(lastItemId
))
2132 menu
->Delete(lastItemId
);
2135 // delete the last separator too if no more files are left
2136 if ( m_fileHistoryN
== 1 )
2138 wxMenuItemList::Node
*node
= menu
->GetMenuItems().GetLast();
2141 wxMenuItem
*menuItem
= node
->GetData();
2142 if ( menuItem
->IsSeparator() )
2144 menu
->Delete(menuItem
);
2146 //else: should we search backwards for the last separator?
2148 //else: menu is empty somehow
2155 wxString
wxFileHistory::GetHistoryFile(size_t i
) const
2158 if ( i
< m_fileHistoryN
)
2160 s
= m_fileHistory
[i
];
2164 wxFAIL_MSG( wxT("bad index in wxFileHistory::GetHistoryFile") );
2170 void wxFileHistory::UseMenu(wxMenu
*menu
)
2172 if (!m_fileMenus
.Member(menu
))
2173 m_fileMenus
.Append(menu
);
2176 void wxFileHistory::RemoveMenu(wxMenu
*menu
)
2178 m_fileMenus
.DeleteObject(menu
);
2182 void wxFileHistory::Load(wxConfigBase
& config
)
2186 buf
.Printf(wxT("file%d"), (int)m_fileHistoryN
+1);
2187 wxString historyFile
;
2188 while ((m_fileHistoryN
< m_fileMaxFiles
) && config
.Read(buf
, &historyFile
) && (historyFile
!= wxT("")))
2190 m_fileHistory
[m_fileHistoryN
] = copystring((const wxChar
*) historyFile
);
2192 buf
.Printf(wxT("file%d"), (int)m_fileHistoryN
+1);
2193 historyFile
= wxT("");
2198 void wxFileHistory::Save(wxConfigBase
& config
)
2201 for (i
= 0; i
< m_fileMaxFiles
; i
++)
2204 buf
.Printf(wxT("file%d"), (int)i
+1);
2205 if (i
< m_fileHistoryN
)
2206 config
.Write(buf
, wxString(m_fileHistory
[i
]));
2208 config
.Write(buf
, wxEmptyString
);
2211 #endif // wxUSE_CONFIG
2213 void wxFileHistory::AddFilesToMenu()
2215 if (m_fileHistoryN
> 0)
2217 wxNode
* node
= m_fileMenus
.GetFirst();
2220 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2221 if (menu
->GetMenuItemCount())
2223 menu
->AppendSeparator();
2227 for (i
= 0; i
< m_fileHistoryN
; i
++)
2229 if (m_fileHistory
[i
])
2232 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
2233 menu
->Append(m_idBase
+i
, buf
);
2236 node
= node
->GetNext();
2241 void wxFileHistory::AddFilesToMenu(wxMenu
* menu
)
2243 if (m_fileHistoryN
> 0)
2245 if (menu
->GetMenuItemCount())
2247 menu
->AppendSeparator();
2251 for (i
= 0; i
< m_fileHistoryN
; i
++)
2253 if (m_fileHistory
[i
])
2256 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
2257 menu
->Append(m_idBase
+i
, buf
);
2263 // ----------------------------------------------------------------------------
2264 // Permits compatibility with existing file formats and functions that
2265 // manipulate files directly
2266 // ----------------------------------------------------------------------------
2268 #if wxUSE_STD_IOSTREAM
2270 bool wxTransferFileToStream(const wxString
& filename
, wxSTD ostream
& stream
)
2272 wxFFile
file(filename
, _T("rb"));
2273 if ( !file
.IsOpened() )
2281 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2285 stream
.write(buf
, nRead
);
2289 while ( !file
.Eof() );
2294 bool wxTransferStreamToFile(wxSTD istream
& stream
, const wxString
& filename
)
2296 wxFFile
file(filename
, _T("wb"));
2297 if ( !file
.IsOpened() )
2303 stream
.read(buf
, WXSIZEOF(buf
));
2304 if ( !stream
.bad() ) // fail may be set on EOF, don't use operator!()
2306 if ( !file
.Write(buf
, stream
.gcount()) )
2310 while ( !stream
.eof() );
2315 #else // !wxUSE_STD_IOSTREAM
2317 bool wxTransferFileToStream(const wxString
& filename
, wxOutputStream
& stream
)
2319 wxFFile
file(filename
, _T("rb"));
2320 if ( !file
.IsOpened() )
2328 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2332 stream
.Write(buf
, nRead
);
2336 while ( !file
.Eof() );
2341 bool wxTransferStreamToFile(wxInputStream
& stream
, const wxString
& filename
)
2343 wxFFile
file(filename
, _T("wb"));
2344 if ( !file
.IsOpened() )
2350 stream
.Read(buf
, WXSIZEOF(buf
));
2352 const size_t nRead
= stream
.LastRead();
2353 if ( !nRead
|| !file
.Write(buf
, nRead
) )
2356 while ( !stream
.Eof() );
2361 #endif // wxUSE_STD_IOSTREAM/!wxUSE_STD_IOSTREAM
2363 #endif // wxUSE_DOC_VIEW_ARCHITECTURE