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
);
105 // ----------------------------------------------------------------------------
107 // ----------------------------------------------------------------------------
109 static const wxChar
*s_MRUEntryFormat
= wxT("&%d %s");
111 // ============================================================================
113 // ============================================================================
115 // ----------------------------------------------------------------------------
117 // ----------------------------------------------------------------------------
119 static wxString
FindExtension(const wxChar
*path
)
122 wxSplitPath(path
, NULL
, NULL
, &ext
);
124 // VZ: extensions are considered not case sensitive - is this really a good
126 return ext
.MakeLower();
129 // ----------------------------------------------------------------------------
130 // Definition of wxDocument
131 // ----------------------------------------------------------------------------
133 wxDocument::wxDocument(wxDocument
*parent
)
135 m_documentModified
= FALSE
;
136 m_documentParent
= parent
;
137 m_documentTemplate
= (wxDocTemplate
*) NULL
;
138 m_commandProcessor
= (wxCommandProcessor
*) NULL
;
142 bool wxDocument::DeleteContents()
147 wxDocument::~wxDocument()
151 if (m_commandProcessor
)
152 delete m_commandProcessor
;
154 if (GetDocumentManager())
155 GetDocumentManager()->RemoveDocument(this);
157 // Not safe to do here, since it'll invoke virtual view functions
158 // expecting to see valid derived objects: and by the time we get here,
159 // we've called destructors higher up.
163 bool wxDocument::Close()
165 if (OnSaveModified())
166 return OnCloseDocument();
171 bool wxDocument::OnCloseDocument()
173 // Tell all views that we're about to close
180 // Note that this implicitly deletes the document when the last view is
182 bool wxDocument::DeleteAllViews()
184 wxDocManager
* manager
= GetDocumentManager();
186 wxNode
*node
= m_documentViews
.GetFirst();
189 wxView
*view
= (wxView
*)node
->GetData();
193 wxNode
*next
= node
->GetNext();
195 delete view
; // Deletes node implicitly
198 // If we haven't yet deleted the document (for example
199 // if there were no views) then delete it.
200 if (manager
&& manager
->GetDocuments().Member(this))
206 wxView
*wxDocument::GetFirstView() const
208 if (m_documentViews
.GetCount() == 0)
209 return (wxView
*) NULL
;
210 return (wxView
*)m_documentViews
.GetFirst()->GetData();
213 wxDocManager
*wxDocument::GetDocumentManager() const
215 return (m_documentTemplate
? m_documentTemplate
->GetDocumentManager() : (wxDocManager
*) NULL
);
218 bool wxDocument::OnNewDocument()
220 if (!OnSaveModified())
223 if (OnCloseDocument()==FALSE
) return FALSE
;
226 SetDocumentSaved(FALSE
);
229 GetDocumentManager()->MakeDefaultName(name
);
231 SetFilename(name
, TRUE
);
236 bool wxDocument::Save()
238 if (!IsModified() && m_savedYet
)
241 if ( m_documentFile
.empty() || !m_savedYet
)
244 return OnSaveDocument(m_documentFile
);
247 bool wxDocument::SaveAs()
249 wxDocTemplate
*docTemplate
= GetDocumentTemplate();
253 wxString tmp
= wxFileSelector(_("Save as"),
254 docTemplate
->GetDirectory(),
255 wxFileNameFromPath(GetFilename()),
256 docTemplate
->GetDefaultExtension(),
257 docTemplate
->GetFileFilter(),
258 wxSAVE
| wxOVERWRITE_PROMPT
,
259 GetDocumentWindow());
264 wxString
fileName(tmp
);
265 wxString path
, name
, ext
;
266 wxSplitPath(fileName
, & path
, & name
, & ext
);
268 if (ext
.IsEmpty() || ext
== wxT(""))
270 fileName
+= wxT(".");
271 fileName
+= docTemplate
->GetDefaultExtension();
274 SetFilename(fileName
);
275 SetTitle(wxFileNameFromPath(fileName
));
277 GetDocumentManager()->AddFileToHistory(fileName
);
279 // Notify the views that the filename has changed
280 wxNode
*node
= m_documentViews
.GetFirst();
283 wxView
*view
= (wxView
*)node
->GetData();
284 view
->OnChangeFilename();
285 node
= node
->GetNext();
288 return OnSaveDocument(m_documentFile
);
291 bool wxDocument::OnSaveDocument(const wxString
& file
)
297 if (wxTheApp
->GetAppName() != wxT(""))
298 msgTitle
= wxTheApp
->GetAppName();
300 msgTitle
= wxString(_("File error"));
302 #if wxUSE_STD_IOSTREAM
303 wxSTD ofstream
store(file
.mb_str());
304 if (store
.fail() || store
.bad())
306 wxFileOutputStream
store(file
);
307 if (store
.GetLastError() != wxSTREAM_NO_ERROR
)
310 (void)wxMessageBox(_("Sorry, could not open this file for saving."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
311 GetDocumentWindow());
315 if (!SaveObject(store
))
317 (void)wxMessageBox(_("Sorry, could not save this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
318 GetDocumentWindow());
324 SetDocumentSaved(TRUE
);
326 wxFileName
fn(file
) ;
327 fn
.MacSetDefaultTypeAndCreator() ;
332 bool wxDocument::OnOpenDocument(const wxString
& file
)
334 if (!OnSaveModified())
338 if (wxTheApp
->GetAppName() != wxT(""))
339 msgTitle
= wxTheApp
->GetAppName();
341 msgTitle
= wxString(_("File error"));
343 #if wxUSE_STD_IOSTREAM
344 wxSTD ifstream
store(file
.mb_str());
345 if (store
.fail() || store
.bad())
347 wxFileInputStream
store(file
);
348 if (store
.GetLastError() != wxSTREAM_NO_ERROR
)
351 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
|wxICON_EXCLAMATION
,
352 GetDocumentWindow());
355 #if wxUSE_STD_IOSTREAM
357 if ( !store
&& !store
.eof() )
359 int res
= LoadObject(store
).GetLastError();
360 if ((res
!= wxSTREAM_NO_ERROR
) &&
361 (res
!= wxSTREAM_EOF
))
364 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
|wxICON_EXCLAMATION
,
365 GetDocumentWindow());
368 SetFilename(file
, TRUE
);
377 #if wxUSE_STD_IOSTREAM
378 wxSTD istream
& wxDocument::LoadObject(wxSTD istream
& stream
)
380 wxInputStream
& wxDocument::LoadObject(wxInputStream
& stream
)
386 #if wxUSE_STD_IOSTREAM
387 wxSTD ostream
& wxDocument::SaveObject(wxSTD ostream
& stream
)
389 wxOutputStream
& wxDocument::SaveObject(wxOutputStream
& stream
)
395 bool wxDocument::Revert()
401 // Get title, or filename if no title, else unnamed
402 bool wxDocument::GetPrintableName(wxString
& buf
) const
404 if (m_documentTitle
!= wxT(""))
406 buf
= m_documentTitle
;
409 else if (m_documentFile
!= wxT(""))
411 buf
= wxFileNameFromPath(m_documentFile
);
421 wxWindow
*wxDocument::GetDocumentWindow() const
423 wxView
*view
= GetFirstView();
425 return view
->GetFrame();
427 return wxTheApp
->GetTopWindow();
430 wxCommandProcessor
*wxDocument::OnCreateCommandProcessor()
432 return new wxCommandProcessor
;
435 // TRUE if safe to close
436 bool wxDocument::OnSaveModified()
441 GetPrintableName(title
);
444 if (wxTheApp
->GetAppName() != wxT(""))
445 msgTitle
= wxTheApp
->GetAppName();
447 msgTitle
= wxString(_("Warning"));
450 prompt
.Printf(_("Do you want to save changes to document %s?"),
451 (const wxChar
*)title
);
452 int res
= wxMessageBox(prompt
, msgTitle
,
453 wxYES_NO
|wxCANCEL
|wxICON_QUESTION
,
454 GetDocumentWindow());
460 else if (res
== wxYES
)
462 else if (res
== wxCANCEL
)
468 bool wxDocument::Draw(wxDC
& WXUNUSED(context
))
473 bool wxDocument::AddView(wxView
*view
)
475 if (!m_documentViews
.Member(view
))
477 m_documentViews
.Append(view
);
483 bool wxDocument::RemoveView(wxView
*view
)
485 (void)m_documentViews
.DeleteObject(view
);
490 bool wxDocument::OnCreate(const wxString
& WXUNUSED(path
), long flags
)
492 if (GetDocumentTemplate()->CreateView(this, flags
))
498 // Called after a view is added or removed.
499 // The default implementation deletes the document if
500 // there are no more views.
501 void wxDocument::OnChangedViewList()
503 if (m_documentViews
.GetCount() == 0)
505 if (OnSaveModified())
512 void wxDocument::UpdateAllViews(wxView
*sender
, wxObject
*hint
)
514 wxNode
*node
= m_documentViews
.GetFirst();
517 wxView
*view
= (wxView
*)node
->GetData();
519 view
->OnUpdate(sender
, hint
);
520 node
= node
->GetNext();
524 void wxDocument::NotifyClosing()
526 wxNode
*node
= m_documentViews
.GetFirst();
529 wxView
*view
= (wxView
*)node
->GetData();
530 view
->OnClosingDocument();
531 node
= node
->GetNext();
535 void wxDocument::SetFilename(const wxString
& filename
, bool notifyViews
)
537 m_documentFile
= filename
;
540 // Notify the views that the filename has changed
541 wxNode
*node
= m_documentViews
.GetFirst();
544 wxView
*view
= (wxView
*)node
->GetData();
545 view
->OnChangeFilename();
546 node
= node
->GetNext();
551 // ----------------------------------------------------------------------------
553 // ----------------------------------------------------------------------------
558 m_viewDocument
= (wxDocument
*) NULL
;
560 m_viewTypeName
= wxT("");
561 m_viewFrame
= (wxFrame
*) NULL
;
566 // GetDocumentManager()->ActivateView(this, FALSE, TRUE);
567 m_viewDocument
->RemoveView(this);
570 // Extend event processing to search the document's event table
571 bool wxView::ProcessEvent(wxEvent
& event
)
573 if ( !GetDocument() || !GetDocument()->ProcessEvent(event
) )
574 return wxEvtHandler::ProcessEvent(event
);
579 void wxView::OnActivateView(bool WXUNUSED(activate
), wxView
*WXUNUSED(activeView
), wxView
*WXUNUSED(deactiveView
))
583 void wxView::OnPrint(wxDC
*dc
, wxObject
*WXUNUSED(info
))
588 void wxView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
))
592 void wxView::OnChangeFilename()
594 if (GetFrame() && GetDocument())
598 GetDocument()->GetPrintableName(title
);
600 GetFrame()->SetTitle(title
);
604 void wxView::SetDocument(wxDocument
*doc
)
606 m_viewDocument
= doc
;
611 bool wxView::Close(bool deleteWindow
)
613 if (OnClose(deleteWindow
))
619 void wxView::Activate(bool activate
)
621 if (GetDocument() && GetDocumentManager())
623 OnActivateView(activate
, this, GetDocumentManager()->GetCurrentView());
624 GetDocumentManager()->ActivateView(this, activate
);
628 bool wxView::OnClose(bool WXUNUSED(deleteWindow
))
630 return GetDocument() ? GetDocument()->Close() : TRUE
;
633 #if wxUSE_PRINTING_ARCHITECTURE
634 wxPrintout
*wxView::OnCreatePrintout()
636 return new wxDocPrintout(this);
638 #endif // wxUSE_PRINTING_ARCHITECTURE
640 // ----------------------------------------------------------------------------
642 // ----------------------------------------------------------------------------
644 wxDocTemplate::wxDocTemplate(wxDocManager
*manager
,
645 const wxString
& descr
,
646 const wxString
& filter
,
649 const wxString
& docTypeName
,
650 const wxString
& viewTypeName
,
651 wxClassInfo
*docClassInfo
,
652 wxClassInfo
*viewClassInfo
,
655 m_documentManager
= manager
;
656 m_description
= descr
;
659 m_fileFilter
= filter
;
661 m_docTypeName
= docTypeName
;
662 m_viewTypeName
= viewTypeName
;
663 m_documentManager
->AssociateTemplate(this);
665 m_docClassInfo
= docClassInfo
;
666 m_viewClassInfo
= viewClassInfo
;
669 wxDocTemplate::~wxDocTemplate()
671 m_documentManager
->DisassociateTemplate(this);
674 // Tries to dynamically construct an object of the right class.
675 wxDocument
*wxDocTemplate::CreateDocument(const wxString
& path
, long flags
)
678 return (wxDocument
*) NULL
;
679 wxDocument
*doc
= (wxDocument
*)m_docClassInfo
->CreateObject();
680 doc
->SetFilename(path
);
681 doc
->SetDocumentTemplate(this);
682 GetDocumentManager()->AddDocument(doc
);
683 doc
->SetCommandProcessor(doc
->OnCreateCommandProcessor());
685 if (doc
->OnCreate(path
, flags
))
689 if (GetDocumentManager()->GetDocuments().Member(doc
))
690 doc
->DeleteAllViews();
691 return (wxDocument
*) NULL
;
695 wxView
*wxDocTemplate::CreateView(wxDocument
*doc
, long flags
)
697 if (!m_viewClassInfo
)
698 return (wxView
*) NULL
;
699 wxView
*view
= (wxView
*)m_viewClassInfo
->CreateObject();
700 view
->SetDocument(doc
);
701 if (view
->OnCreate(doc
, flags
))
708 return (wxView
*) NULL
;
712 // The default (very primitive) format detection: check is the extension is
713 // that of the template
714 bool wxDocTemplate::FileMatchesTemplate(const wxString
& path
)
716 return GetDefaultExtension().IsSameAs(FindExtension(path
));
719 // ----------------------------------------------------------------------------
721 // ----------------------------------------------------------------------------
723 BEGIN_EVENT_TABLE(wxDocManager
, wxEvtHandler
)
724 EVT_MENU(wxID_OPEN
, wxDocManager::OnFileOpen
)
725 EVT_MENU(wxID_CLOSE
, wxDocManager::OnFileClose
)
726 EVT_MENU(wxID_CLOSE_ALL
, wxDocManager::OnFileCloseAll
)
727 EVT_MENU(wxID_REVERT
, wxDocManager::OnFileRevert
)
728 EVT_MENU(wxID_NEW
, wxDocManager::OnFileNew
)
729 EVT_MENU(wxID_SAVE
, wxDocManager::OnFileSave
)
730 EVT_MENU(wxID_SAVEAS
, wxDocManager::OnFileSaveAs
)
731 EVT_MENU(wxID_UNDO
, wxDocManager::OnUndo
)
732 EVT_MENU(wxID_REDO
, wxDocManager::OnRedo
)
734 EVT_UPDATE_UI(wxID_OPEN
, wxDocManager::OnUpdateFileOpen
)
735 EVT_UPDATE_UI(wxID_CLOSE
, wxDocManager::OnUpdateFileClose
)
736 EVT_UPDATE_UI(wxID_CLOSE_ALL
, wxDocManager::OnUpdateFileClose
)
737 EVT_UPDATE_UI(wxID_REVERT
, wxDocManager::OnUpdateFileRevert
)
738 EVT_UPDATE_UI(wxID_NEW
, wxDocManager::OnUpdateFileNew
)
739 EVT_UPDATE_UI(wxID_SAVE
, wxDocManager::OnUpdateFileSave
)
740 EVT_UPDATE_UI(wxID_SAVEAS
, wxDocManager::OnUpdateFileSaveAs
)
741 EVT_UPDATE_UI(wxID_UNDO
, wxDocManager::OnUpdateUndo
)
742 EVT_UPDATE_UI(wxID_REDO
, wxDocManager::OnUpdateRedo
)
744 #if wxUSE_PRINTING_ARCHITECTURE
745 EVT_MENU(wxID_PRINT
, wxDocManager::OnPrint
)
746 EVT_MENU(wxID_PRINT_SETUP
, wxDocManager::OnPrintSetup
)
747 EVT_MENU(wxID_PREVIEW
, wxDocManager::OnPreview
)
749 EVT_UPDATE_UI(wxID_PRINT
, wxDocManager::OnUpdatePrint
)
750 EVT_UPDATE_UI(wxID_PRINT_SETUP
, wxDocManager::OnUpdatePrintSetup
)
751 EVT_UPDATE_UI(wxID_PREVIEW
, wxDocManager::OnUpdatePreview
)
755 wxDocManager
* wxDocManager::sm_docManager
= (wxDocManager
*) NULL
;
757 wxDocManager::wxDocManager(long flags
, bool initialize
)
759 m_defaultDocumentNameCounter
= 1;
761 m_currentView
= (wxView
*) NULL
;
762 m_maxDocsOpen
= 10000;
763 m_fileHistory
= (wxFileHistory
*) NULL
;
766 sm_docManager
= this;
769 wxDocManager::~wxDocManager()
773 delete m_fileHistory
;
774 sm_docManager
= (wxDocManager
*) NULL
;
777 // closes the specified document
778 bool wxDocManager::CloseDocument(wxDocument
* doc
, bool force
)
780 if (doc
->Close() || force
)
782 // Implicitly deletes the document when
783 // the last view is deleted
784 doc
->DeleteAllViews();
786 // Check we're really deleted
787 if (m_docs
.Member(doc
))
795 bool wxDocManager::CloseDocuments(bool force
)
797 wxNode
*node
= m_docs
.GetFirst();
800 wxDocument
*doc
= (wxDocument
*)node
->GetData();
801 wxNode
*next
= node
->GetNext();
803 if (!CloseDocument(doc
, force
))
806 // This assumes that documents are not connected in
807 // any way, i.e. deleting one document does NOT
814 bool wxDocManager::Clear(bool force
)
816 if (!CloseDocuments(force
))
819 wxNode
*node
= m_templates
.GetFirst();
822 wxDocTemplate
*templ
= (wxDocTemplate
*) node
->GetData();
823 wxNode
* next
= node
->GetNext();
830 bool wxDocManager::Initialize()
832 m_fileHistory
= OnCreateFileHistory();
836 wxFileHistory
*wxDocManager::OnCreateFileHistory()
838 return new wxFileHistory
;
841 void wxDocManager::OnFileClose(wxCommandEvent
& WXUNUSED(event
))
843 wxDocument
*doc
= GetCurrentDocument();
848 doc
->DeleteAllViews();
849 if (m_docs
.Member(doc
))
854 void wxDocManager::OnFileCloseAll(wxCommandEvent
& WXUNUSED(event
))
856 CloseDocuments(FALSE
);
859 void wxDocManager::OnFileNew(wxCommandEvent
& WXUNUSED(event
))
861 CreateDocument( wxT(""), wxDOC_NEW
);
864 void wxDocManager::OnFileOpen(wxCommandEvent
& WXUNUSED(event
))
866 if ( !CreateDocument( wxT(""), 0) )
872 void wxDocManager::OnFileRevert(wxCommandEvent
& WXUNUSED(event
))
874 wxDocument
*doc
= GetCurrentDocument();
880 void wxDocManager::OnFileSave(wxCommandEvent
& WXUNUSED(event
))
882 wxDocument
*doc
= GetCurrentDocument();
888 void wxDocManager::OnFileSaveAs(wxCommandEvent
& WXUNUSED(event
))
890 wxDocument
*doc
= GetCurrentDocument();
896 void wxDocManager::OnPrint(wxCommandEvent
& WXUNUSED(event
))
898 #if wxUSE_PRINTING_ARCHITECTURE
899 wxView
*view
= GetCurrentView();
903 wxPrintout
*printout
= view
->OnCreatePrintout();
907 printer
.Print(view
->GetFrame(), printout
, TRUE
);
911 #endif // wxUSE_PRINTING_ARCHITECTURE
914 void wxDocManager::OnPrintSetup(wxCommandEvent
& WXUNUSED(event
))
916 #if wxUSE_PRINTING_ARCHITECTURE
917 wxWindow
*parentWin
= wxTheApp
->GetTopWindow();
918 wxView
*view
= GetCurrentView();
920 parentWin
= view
->GetFrame();
922 wxPrintDialogData data
;
924 wxPrintDialog
printerDialog(parentWin
, &data
);
925 printerDialog
.GetPrintDialogData().SetSetupDialog(TRUE
);
926 printerDialog
.ShowModal();
927 #endif // wxUSE_PRINTING_ARCHITECTURE
930 void wxDocManager::OnPreview(wxCommandEvent
& WXUNUSED(event
))
932 #if wxUSE_PRINTING_ARCHITECTURE
933 wxView
*view
= GetCurrentView();
937 wxPrintout
*printout
= view
->OnCreatePrintout();
940 // Pass two printout objects: for preview, and possible printing.
941 wxPrintPreviewBase
*preview
= (wxPrintPreviewBase
*) NULL
;
942 preview
= new wxPrintPreview(printout
, view
->OnCreatePrintout());
944 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, (wxFrame
*)wxTheApp
->GetTopWindow(), _("Print Preview"),
945 wxPoint(100, 100), wxSize(600, 650));
946 frame
->Centre(wxBOTH
);
950 #endif // wxUSE_PRINTING_ARCHITECTURE
953 void wxDocManager::OnUndo(wxCommandEvent
& WXUNUSED(event
))
955 wxDocument
*doc
= GetCurrentDocument();
958 if (doc
->GetCommandProcessor())
959 doc
->GetCommandProcessor()->Undo();
962 void wxDocManager::OnRedo(wxCommandEvent
& WXUNUSED(event
))
964 wxDocument
*doc
= GetCurrentDocument();
967 if (doc
->GetCommandProcessor())
968 doc
->GetCommandProcessor()->Redo();
971 // Handlers for UI update commands
973 void wxDocManager::OnUpdateFileOpen(wxUpdateUIEvent
& event
)
975 event
.Enable( TRUE
);
978 void wxDocManager::OnUpdateFileClose(wxUpdateUIEvent
& event
)
980 wxDocument
*doc
= GetCurrentDocument();
981 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
984 void wxDocManager::OnUpdateFileRevert(wxUpdateUIEvent
& event
)
986 wxDocument
*doc
= GetCurrentDocument();
987 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
990 void wxDocManager::OnUpdateFileNew(wxUpdateUIEvent
& event
)
992 event
.Enable( TRUE
);
995 void wxDocManager::OnUpdateFileSave(wxUpdateUIEvent
& event
)
997 wxDocument
*doc
= GetCurrentDocument();
998 event
.Enable( doc
&& doc
->IsModified() );
1001 void wxDocManager::OnUpdateFileSaveAs(wxUpdateUIEvent
& event
)
1003 wxDocument
*doc
= GetCurrentDocument();
1004 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1007 void wxDocManager::OnUpdateUndo(wxUpdateUIEvent
& event
)
1009 wxDocument
*doc
= GetCurrentDocument();
1010 event
.Enable( (doc
&& doc
->GetCommandProcessor() && doc
->GetCommandProcessor()->CanUndo()) );
1011 if (doc
&& doc
->GetCommandProcessor())
1012 doc
->GetCommandProcessor()->SetMenuStrings();
1015 void wxDocManager::OnUpdateRedo(wxUpdateUIEvent
& event
)
1017 wxDocument
*doc
= GetCurrentDocument();
1018 event
.Enable( (doc
&& doc
->GetCommandProcessor() && doc
->GetCommandProcessor()->CanRedo()) );
1019 if (doc
&& doc
->GetCommandProcessor())
1020 doc
->GetCommandProcessor()->SetMenuStrings();
1023 void wxDocManager::OnUpdatePrint(wxUpdateUIEvent
& event
)
1025 wxDocument
*doc
= GetCurrentDocument();
1026 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1029 void wxDocManager::OnUpdatePrintSetup(wxUpdateUIEvent
& event
)
1031 event
.Enable( TRUE
);
1034 void wxDocManager::OnUpdatePreview(wxUpdateUIEvent
& event
)
1036 wxDocument
*doc
= GetCurrentDocument();
1037 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1040 wxView
*wxDocManager::GetCurrentView() const
1043 return m_currentView
;
1044 if (m_docs
.GetCount() == 1)
1046 wxDocument
* doc
= (wxDocument
*) m_docs
.GetFirst()->GetData();
1047 return doc
->GetFirstView();
1049 return (wxView
*) NULL
;
1052 // Extend event processing to search the view's event table
1053 bool wxDocManager::ProcessEvent(wxEvent
& event
)
1055 wxView
* view
= GetCurrentView();
1058 if (view
->ProcessEvent(event
))
1061 return wxEvtHandler::ProcessEvent(event
);
1064 wxDocument
*wxDocManager::CreateDocument(const wxString
& path
, long flags
)
1066 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.GetCount()];
1069 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1071 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Item(i
)->GetData());
1072 if (temp
->IsVisible())
1074 templates
[n
] = temp
;
1081 return (wxDocument
*) NULL
;
1084 wxDocument
* docToClose
= NULL
;
1086 // If we've reached the max number of docs, close the
1088 if ( (int)GetDocuments().GetCount() >= m_maxDocsOpen
)
1090 wxDocument
*doc
= (wxDocument
*)GetDocuments().GetFirst()->GetData();
1094 // New document: user chooses a template, unless there's only one.
1095 if (flags
& wxDOC_NEW
)
1101 if (!CloseDocument(docToClose
, FALSE
))
1107 wxDocTemplate
*temp
= templates
[0];
1109 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
1113 newDoc
->SetDocumentName(temp
->GetDocumentName());
1114 newDoc
->SetDocumentTemplate(temp
);
1115 newDoc
->OnNewDocument();
1120 wxDocTemplate
*temp
= SelectDocumentType(templates
, n
);
1126 if (!CloseDocument(docToClose
, FALSE
))
1132 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
1136 newDoc
->SetDocumentName(temp
->GetDocumentName());
1137 newDoc
->SetDocumentTemplate(temp
);
1138 newDoc
->OnNewDocument();
1143 return (wxDocument
*) NULL
;
1146 // Existing document
1147 wxDocTemplate
*temp
= (wxDocTemplate
*) NULL
;
1149 wxString
path2(wxT(""));
1150 if (path
!= wxT(""))
1153 if (flags
& wxDOC_SILENT
)
1154 temp
= FindTemplateForPath(path2
);
1156 temp
= SelectDocumentPath(templates
, n
, path2
, flags
);
1164 if (!CloseDocument(docToClose
, FALSE
))
1170 wxDocument
*newDoc
= temp
->CreateDocument(path2
, flags
);
1173 newDoc
->SetDocumentName(temp
->GetDocumentName());
1174 newDoc
->SetDocumentTemplate(temp
);
1175 if (!newDoc
->OnOpenDocument(path2
))
1177 newDoc
->DeleteAllViews();
1178 // delete newDoc; // Implicitly deleted by DeleteAllViews
1179 return (wxDocument
*) NULL
;
1181 AddFileToHistory(path2
);
1186 return (wxDocument
*) NULL
;
1189 wxView
*wxDocManager::CreateView(wxDocument
*doc
, long flags
)
1191 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.GetCount()];
1194 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1196 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Item(i
)->GetData());
1197 if (temp
->IsVisible())
1199 if (temp
->GetDocumentName() == doc
->GetDocumentName())
1201 templates
[n
] = temp
;
1209 return (wxView
*) NULL
;
1213 wxDocTemplate
*temp
= templates
[0];
1215 wxView
*view
= temp
->CreateView(doc
, flags
);
1217 view
->SetViewName(temp
->GetViewName());
1221 wxDocTemplate
*temp
= SelectViewType(templates
, n
);
1225 wxView
*view
= temp
->CreateView(doc
, flags
);
1227 view
->SetViewName(temp
->GetViewName());
1231 return (wxView
*) NULL
;
1234 // Not yet implemented
1235 void wxDocManager::DeleteTemplate(wxDocTemplate
*WXUNUSED(temp
), long WXUNUSED(flags
))
1239 // Not yet implemented
1240 bool wxDocManager::FlushDoc(wxDocument
*WXUNUSED(doc
))
1245 wxDocument
*wxDocManager::GetCurrentDocument() const
1247 wxView
*view
= GetCurrentView();
1249 return view
->GetDocument();
1251 return (wxDocument
*) NULL
;
1254 // Make a default document name
1255 bool wxDocManager::MakeDefaultName(wxString
& name
)
1257 name
.Printf(_("unnamed%d"), m_defaultDocumentNameCounter
);
1258 m_defaultDocumentNameCounter
++;
1263 // Make a frame title (override this to do something different)
1264 // If docName is empty, a document is not currently active.
1265 wxString
wxDocManager::MakeFrameTitle(wxDocument
* doc
)
1267 wxString appName
= wxTheApp
->GetAppName();
1274 doc
->GetPrintableName(docName
);
1275 title
= docName
+ wxString(_(" - ")) + appName
;
1281 // Not yet implemented
1282 wxDocTemplate
*wxDocManager::MatchTemplate(const wxString
& WXUNUSED(path
))
1284 return (wxDocTemplate
*) NULL
;
1287 // File history management
1288 void wxDocManager::AddFileToHistory(const wxString
& file
)
1291 m_fileHistory
->AddFileToHistory(file
);
1294 void wxDocManager::RemoveFileFromHistory(size_t i
)
1297 m_fileHistory
->RemoveFileFromHistory(i
);
1300 wxString
wxDocManager::GetHistoryFile(size_t i
) const
1305 histFile
= m_fileHistory
->GetHistoryFile(i
);
1310 void wxDocManager::FileHistoryUseMenu(wxMenu
*menu
)
1313 m_fileHistory
->UseMenu(menu
);
1316 void wxDocManager::FileHistoryRemoveMenu(wxMenu
*menu
)
1319 m_fileHistory
->RemoveMenu(menu
);
1323 void wxDocManager::FileHistoryLoad(wxConfigBase
& config
)
1326 m_fileHistory
->Load(config
);
1329 void wxDocManager::FileHistorySave(wxConfigBase
& config
)
1332 m_fileHistory
->Save(config
);
1336 void wxDocManager::FileHistoryAddFilesToMenu(wxMenu
* menu
)
1339 m_fileHistory
->AddFilesToMenu(menu
);
1342 void wxDocManager::FileHistoryAddFilesToMenu()
1345 m_fileHistory
->AddFilesToMenu();
1348 size_t wxDocManager::GetNoHistoryFiles() const
1351 return m_fileHistory
->GetNoHistoryFiles();
1357 // Find out the document template via matching in the document file format
1358 // against that of the template
1359 wxDocTemplate
*wxDocManager::FindTemplateForPath(const wxString
& path
)
1361 wxDocTemplate
*theTemplate
= (wxDocTemplate
*) NULL
;
1363 // Find the template which this extension corresponds to
1364 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1366 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Item(i
)->GetData();
1367 if ( temp
->FileMatchesTemplate(path
) )
1376 // Try to get a more suitable parent frame than the top window,
1377 // for selection dialogs. Otherwise you may get an unexpected
1378 // window being activated when a dialog is shown.
1379 static wxWindow
* wxFindSuitableParent()
1381 wxWindow
* parent
= wxTheApp
->GetTopWindow();
1383 wxWindow
* focusWindow
= wxWindow::FindFocus();
1386 while (focusWindow
&&
1387 !focusWindow
->IsKindOf(CLASSINFO(wxDialog
)) &&
1388 !focusWindow
->IsKindOf(CLASSINFO(wxFrame
)))
1390 focusWindow
= focusWindow
->GetParent();
1393 parent
= focusWindow
;
1398 // Prompts user to open a file, using file specs in templates.
1399 // How to implement in wxWindows? Must extend the file selector
1400 // dialog or implement own; OR match the extension to the
1401 // template extension.
1403 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**templates
,
1404 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
1407 int WXUNUSED(noTemplates
),
1410 long WXUNUSED(flags
),
1411 bool WXUNUSED(save
))
1413 // We can only have multiple filters in Windows and GTK
1414 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
1418 for (i
= 0; i
< noTemplates
; i
++)
1420 if (templates
[i
]->IsVisible())
1422 // add a '|' to separate this filter from the previous one
1423 if ( !descrBuf
.IsEmpty() )
1424 descrBuf
<< wxT('|');
1426 descrBuf
<< templates
[i
]->GetDescription()
1427 << wxT(" (") << templates
[i
]->GetFileFilter() << wxT(") |")
1428 << templates
[i
]->GetFileFilter();
1432 wxString descrBuf
= wxT("*.*");
1435 int FilterIndex
= -1;
1437 wxWindow
* parent
= wxFindSuitableParent();
1439 wxString pathTmp
= wxFileSelectorEx(_("Select a file"),
1447 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)NULL
;
1448 if (!pathTmp
.IsEmpty())
1450 if (!wxFileExists(pathTmp
))
1453 if (!wxTheApp
->GetAppName().IsEmpty())
1454 msgTitle
= wxTheApp
->GetAppName();
1456 msgTitle
= wxString(_("File error"));
1458 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
1462 return (wxDocTemplate
*) NULL
;
1464 m_lastDirectory
= wxPathOnly(pathTmp
);
1468 // first choose the template using the extension, if this fails (i.e.
1469 // wxFileSelectorEx() didn't fill it), then use the path
1470 if ( FilterIndex
!= -1 )
1471 theTemplate
= templates
[FilterIndex
];
1473 theTemplate
= FindTemplateForPath(path
);
1483 // In all other windowing systems, until we have more advanced
1484 // file selectors, we must select the document type (template) first, and
1485 // _then_ pop up the file selector.
1486 wxDocTemplate
*temp
= SelectDocumentType(templates
, noTemplates
);
1488 return (wxDocTemplate
*) NULL
;
1490 wxChar
*pathTmp
= wxFileSelector(_("Select a file"), wxT(""), wxT(""),
1491 temp
->GetDefaultExtension(),
1492 temp
->GetFileFilter(),
1493 0, wxTheApp
->GetTopWindow());
1501 return (wxDocTemplate
*) NULL
;
1505 wxDocTemplate
*wxDocManager::SelectDocumentType(wxDocTemplate
**templates
,
1506 int noTemplates
, bool sort
)
1508 wxArrayString
strings(sort
);
1509 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1513 for (i
= 0; i
< noTemplates
; i
++)
1515 if (templates
[i
]->IsVisible())
1519 for (j
= 0; j
< n
; j
++)
1521 //filter out NOT unique documents + view combinations
1522 if ( templates
[i
]->m_docTypeName
== data
[j
]->m_docTypeName
&&
1523 templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
1530 strings
.Add(templates
[i
]->m_description
);
1532 data
[n
] = templates
[i
];
1540 // Yes, this will be slow, but template lists
1541 // are typically short.
1543 n
= strings
.Count();
1544 for (i
= 0; i
< n
; i
++)
1546 for (j
= 0; j
< noTemplates
; j
++)
1548 if (strings
[i
] == templates
[j
]->m_description
)
1549 data
[i
] = templates
[j
];
1554 wxDocTemplate
*theTemplate
;
1559 // no visible templates, hence nothing to choose from
1564 // don't propose the user to choose if he heas no choice
1565 theTemplate
= data
[0];
1569 // propose the user to choose one of several
1570 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1572 _("Select a document template"),
1576 wxFindSuitableParent()
1585 wxDocTemplate
*wxDocManager::SelectViewType(wxDocTemplate
**templates
,
1586 int noTemplates
, bool sort
)
1588 wxArrayString
strings(sort
);
1589 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1593 for (i
= 0; i
< noTemplates
; i
++)
1595 wxDocTemplate
*templ
= templates
[i
];
1596 if ( templ
->IsVisible() && !templ
->GetViewName().empty() )
1600 for (j
= 0; j
< n
; j
++)
1602 //filter out NOT unique views
1603 if ( templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
)
1609 strings
.Add(templ
->m_viewTypeName
);
1618 // Yes, this will be slow, but template lists
1619 // are typically short.
1621 n
= strings
.Count();
1622 for (i
= 0; i
< n
; i
++)
1624 for (j
= 0; j
< noTemplates
; j
++)
1626 if (strings
[i
] == templates
[j
]->m_viewTypeName
)
1627 data
[i
] = templates
[j
];
1632 wxDocTemplate
*theTemplate
;
1634 // the same logic as above
1638 theTemplate
= (wxDocTemplate
*)NULL
;
1642 theTemplate
= data
[0];
1646 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1648 _("Select a document view"),
1652 wxFindSuitableParent()
1661 void wxDocManager::AssociateTemplate(wxDocTemplate
*temp
)
1663 if (!m_templates
.Member(temp
))
1664 m_templates
.Append(temp
);
1667 void wxDocManager::DisassociateTemplate(wxDocTemplate
*temp
)
1669 m_templates
.DeleteObject(temp
);
1672 // Add and remove a document from the manager's list
1673 void wxDocManager::AddDocument(wxDocument
*doc
)
1675 if (!m_docs
.Member(doc
))
1679 void wxDocManager::RemoveDocument(wxDocument
*doc
)
1681 m_docs
.DeleteObject(doc
);
1684 // Views or windows should inform the document manager
1685 // when a view is going in or out of focus
1686 void wxDocManager::ActivateView(wxView
*view
, bool activate
, bool WXUNUSED(deleting
))
1688 // If we're deactiving, and if we're not actually deleting the view, then
1689 // don't reset the current view because we may be going to
1690 // a window without a view.
1691 // WHAT DID I MEAN BY THAT EXACTLY?
1695 if (m_currentView == view)
1696 m_currentView = NULL;
1702 m_currentView
= view
;
1704 m_currentView
= (wxView
*) NULL
;
1708 // ----------------------------------------------------------------------------
1709 // Default document child frame
1710 // ----------------------------------------------------------------------------
1712 BEGIN_EVENT_TABLE(wxDocChildFrame
, wxFrame
)
1713 EVT_ACTIVATE(wxDocChildFrame::OnActivate
)
1714 EVT_CLOSE(wxDocChildFrame::OnCloseWindow
)
1717 wxDocChildFrame::wxDocChildFrame(wxDocument
*doc
,
1721 const wxString
& title
,
1725 const wxString
& name
)
1726 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1728 m_childDocument
= doc
;
1731 view
->SetFrame(this);
1734 wxDocChildFrame::~wxDocChildFrame()
1738 // Extend event processing to search the view's event table
1739 bool wxDocChildFrame::ProcessEvent(wxEvent
& event
)
1742 m_childView
->Activate(TRUE
);
1744 if ( !m_childView
|| ! m_childView
->ProcessEvent(event
) )
1746 // Only hand up to the parent if it's a menu command
1747 if (!event
.IsKindOf(CLASSINFO(wxCommandEvent
)) || !GetParent() || !GetParent()->ProcessEvent(event
))
1748 return wxEvtHandler::ProcessEvent(event
);
1756 void wxDocChildFrame::OnActivate(wxActivateEvent
& event
)
1758 wxFrame::OnActivate(event
);
1761 m_childView
->Activate(event
.GetActive());
1764 void wxDocChildFrame::OnCloseWindow(wxCloseEvent
& event
)
1769 if (!event
.CanVeto())
1770 ans
= TRUE
; // Must delete.
1772 ans
= m_childView
->Close(FALSE
); // FALSE means don't delete associated window
1776 m_childView
->Activate(FALSE
);
1778 m_childView
= (wxView
*) NULL
;
1779 m_childDocument
= (wxDocument
*) NULL
;
1790 // ----------------------------------------------------------------------------
1791 // Default parent frame
1792 // ----------------------------------------------------------------------------
1794 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1795 EVT_MENU(wxID_EXIT
, wxDocParentFrame::OnExit
)
1796 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE9
, wxDocParentFrame::OnMRUFile
)
1797 EVT_CLOSE(wxDocParentFrame::OnCloseWindow
)
1800 wxDocParentFrame::wxDocParentFrame(wxDocManager
*manager
,
1803 const wxString
& title
,
1807 const wxString
& name
)
1808 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1810 m_docManager
= manager
;
1813 void wxDocParentFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
1818 void wxDocParentFrame::OnMRUFile(wxCommandEvent
& event
)
1820 int n
= event
.GetId() - wxID_FILE1
; // the index in MRU list
1821 wxString
filename(m_docManager
->GetHistoryFile(n
));
1822 if ( !filename
.IsEmpty() )
1824 // verify that the file exists before doing anything else
1825 if ( wxFile::Exists(filename
) )
1828 (void)m_docManager
->CreateDocument(filename
, wxDOC_SILENT
);
1832 // remove the bogus filename from the MRU list and notify the user
1834 m_docManager
->RemoveFileFromHistory(n
);
1836 wxLogError(_("The file '%s' doesn't exist and couldn't be opened.\nIt has been removed from the most recently used files list."),
1842 // Extend event processing to search the view's event table
1843 bool wxDocParentFrame::ProcessEvent(wxEvent
& event
)
1845 // Try the document manager, then do default processing
1846 if (!m_docManager
|| !m_docManager
->ProcessEvent(event
))
1847 return wxEvtHandler::ProcessEvent(event
);
1852 // Define the behaviour for the frame closing
1853 // - must delete all frames except for the main one.
1854 void wxDocParentFrame::OnCloseWindow(wxCloseEvent
& event
)
1856 if (m_docManager
->Clear(!event
.CanVeto()))
1864 #if wxUSE_PRINTING_ARCHITECTURE
1866 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
)
1869 m_printoutView
= view
;
1872 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
1876 // Get the logical pixels per inch of screen and printer
1877 int ppiScreenX
, ppiScreenY
;
1878 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
1879 int ppiPrinterX
, ppiPrinterY
;
1880 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
1882 // This scales the DC so that the printout roughly represents the
1883 // the screen scaling. The text point size _should_ be the right size
1884 // but in fact is too small for some reason. This is a detail that will
1885 // need to be addressed at some point but can be fudged for the
1887 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
1889 // Now we have to check in case our real page size is reduced
1890 // (e.g. because we're drawing to a print preview memory DC)
1891 int pageWidth
, pageHeight
;
1893 dc
->GetSize(&w
, &h
);
1894 GetPageSizePixels(&pageWidth
, &pageHeight
);
1896 // If printer pageWidth == current DC width, then this doesn't
1897 // change. But w might be the preview bitmap width, so scale down.
1898 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
1899 dc
->SetUserScale(overallScale
, overallScale
);
1903 m_printoutView
->OnDraw(dc
);
1908 bool wxDocPrintout::HasPage(int pageNum
)
1910 return (pageNum
== 1);
1913 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
1915 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
1921 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
1929 #endif // wxUSE_PRINTING_ARCHITECTURE
1931 // ----------------------------------------------------------------------------
1932 // File history processor
1933 // ----------------------------------------------------------------------------
1935 wxFileHistory::wxFileHistory(size_t maxFiles
, wxWindowID idBase
)
1937 m_fileMaxFiles
= maxFiles
;
1940 m_fileHistory
= new wxChar
*[m_fileMaxFiles
];
1943 wxFileHistory::~wxFileHistory()
1946 for (i
= 0; i
< m_fileHistoryN
; i
++)
1947 delete[] m_fileHistory
[i
];
1948 delete[] m_fileHistory
;
1951 // File history management
1952 void wxFileHistory::AddFileToHistory(const wxString
& file
)
1956 // Check we don't already have this file
1957 for (i
= 0; i
< m_fileHistoryN
; i
++)
1959 if ( m_fileHistory
[i
] && (file
== m_fileHistory
[i
]) )
1961 // we do have it, move it to the top of the history
1962 RemoveFileFromHistory (i
);
1963 AddFileToHistory (file
);
1968 // if we already have a full history, delete the one at the end
1969 if ( m_fileMaxFiles
== m_fileHistoryN
)
1971 RemoveFileFromHistory (m_fileHistoryN
- 1);
1972 AddFileToHistory (file
);
1976 // Add to the project file history:
1977 // Move existing files (if any) down so we can insert file at beginning.
1978 if (m_fileHistoryN
< m_fileMaxFiles
)
1980 wxNode
* node
= m_fileMenus
.GetFirst();
1983 wxMenu
* menu
= (wxMenu
*) node
->GetData();
1984 if ( m_fileHistoryN
== 0 && menu
->GetMenuItemCount() )
1986 menu
->AppendSeparator();
1988 menu
->Append(m_idBase
+m_fileHistoryN
, _("[EMPTY]"));
1989 node
= node
->GetNext();
1993 // Shuffle filenames down
1994 for (i
= (m_fileHistoryN
-1); i
> 0; i
--)
1996 m_fileHistory
[i
] = m_fileHistory
[i
-1];
1998 m_fileHistory
[0] = copystring(file
);
2000 // this is the directory of the last opened file
2001 wxString pathCurrent
;
2002 wxSplitPath( m_fileHistory
[0], &pathCurrent
, NULL
, NULL
);
2003 for (i
= 0; i
< m_fileHistoryN
; i
++)
2005 if ( m_fileHistory
[i
] )
2007 // if in same directory just show the filename; otherwise the full
2009 wxString pathInMenu
, path
, filename
, ext
;
2010 wxSplitPath( m_fileHistory
[i
], &path
, &filename
, &ext
);
2011 if ( path
== pathCurrent
)
2013 pathInMenu
= filename
;
2015 pathInMenu
= pathInMenu
+ wxFILE_SEP_EXT
+ ext
;
2019 // absolute path; could also set relative path
2020 pathInMenu
= m_fileHistory
[i
];
2024 buf
.Printf(s_MRUEntryFormat
, i
+ 1, pathInMenu
.c_str());
2025 wxNode
* node
= m_fileMenus
.GetFirst();
2028 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2029 menu
->SetLabel(m_idBase
+ i
, buf
);
2030 node
= node
->GetNext();
2036 void wxFileHistory::RemoveFileFromHistory(size_t i
)
2038 wxCHECK_RET( i
< m_fileHistoryN
,
2039 wxT("invalid index in wxFileHistory::RemoveFileFromHistory") );
2041 // delete the element from the array (could use memmove() too...)
2042 delete [] m_fileHistory
[i
];
2045 for ( j
= i
; j
< m_fileHistoryN
- 1; j
++ )
2047 m_fileHistory
[j
] = m_fileHistory
[j
+ 1];
2050 wxNode
* node
= m_fileMenus
.GetFirst();
2053 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2056 // shuffle filenames up
2058 for ( j
= i
; j
< m_fileHistoryN
- 1; j
++ )
2060 buf
.Printf(s_MRUEntryFormat
, j
+ 1, m_fileHistory
[j
]);
2061 menu
->SetLabel(m_idBase
+ j
, buf
);
2064 node
= node
->GetNext();
2066 // delete the last menu item which is unused now
2067 wxWindowID lastItemId
= m_idBase
+ m_fileHistoryN
- 1;
2068 if (menu
->FindItem(lastItemId
))
2070 menu
->Delete(lastItemId
);
2073 // delete the last separator too if no more files are left
2074 if ( m_fileHistoryN
== 1 )
2076 wxMenuItemList::Node
*node
= menu
->GetMenuItems().GetLast();
2079 wxMenuItem
*menuItem
= node
->GetData();
2080 if ( menuItem
->IsSeparator() )
2082 menu
->Delete(menuItem
);
2084 //else: should we search backwards for the last separator?
2086 //else: menu is empty somehow
2093 wxString
wxFileHistory::GetHistoryFile(size_t i
) const
2096 if ( i
< m_fileHistoryN
)
2098 s
= m_fileHistory
[i
];
2102 wxFAIL_MSG( wxT("bad index in wxFileHistory::GetHistoryFile") );
2108 void wxFileHistory::UseMenu(wxMenu
*menu
)
2110 if (!m_fileMenus
.Member(menu
))
2111 m_fileMenus
.Append(menu
);
2114 void wxFileHistory::RemoveMenu(wxMenu
*menu
)
2116 m_fileMenus
.DeleteObject(menu
);
2120 void wxFileHistory::Load(wxConfigBase
& config
)
2124 buf
.Printf(wxT("file%d"), (int)m_fileHistoryN
+1);
2125 wxString historyFile
;
2126 while ((m_fileHistoryN
< m_fileMaxFiles
) && config
.Read(buf
, &historyFile
) && (historyFile
!= wxT("")))
2128 m_fileHistory
[m_fileHistoryN
] = copystring((const wxChar
*) historyFile
);
2130 buf
.Printf(wxT("file%d"), (int)m_fileHistoryN
+1);
2131 historyFile
= wxT("");
2136 void wxFileHistory::Save(wxConfigBase
& config
)
2139 for (i
= 0; i
< m_fileHistoryN
; i
++)
2142 buf
.Printf(wxT("file%d"), (int)i
+1);
2143 config
.Write(buf
, wxString(m_fileHistory
[i
]));
2146 #endif // wxUSE_CONFIG
2148 void wxFileHistory::AddFilesToMenu()
2150 if (m_fileHistoryN
> 0)
2152 wxNode
* node
= m_fileMenus
.GetFirst();
2155 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2156 if (menu
->GetMenuItemCount())
2158 menu
->AppendSeparator();
2162 for (i
= 0; i
< m_fileHistoryN
; i
++)
2164 if (m_fileHistory
[i
])
2167 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
2168 menu
->Append(m_idBase
+i
, buf
);
2171 node
= node
->GetNext();
2176 void wxFileHistory::AddFilesToMenu(wxMenu
* menu
)
2178 if (m_fileHistoryN
> 0)
2180 if (menu
->GetMenuItemCount())
2182 menu
->AppendSeparator();
2186 for (i
= 0; i
< m_fileHistoryN
; i
++)
2188 if (m_fileHistory
[i
])
2191 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
2192 menu
->Append(m_idBase
+i
, buf
);
2198 // ----------------------------------------------------------------------------
2199 // Permits compatibility with existing file formats and functions that
2200 // manipulate files directly
2201 // ----------------------------------------------------------------------------
2203 #if wxUSE_STD_IOSTREAM
2205 bool wxTransferFileToStream(const wxString
& filename
, wxSTD ostream
& stream
)
2207 wxFFile
file(filename
, _T("rb"));
2208 if ( !file
.IsOpened() )
2216 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2220 stream
.write(buf
, nRead
);
2224 while ( !file
.Eof() );
2229 bool wxTransferStreamToFile(wxSTD istream
& stream
, const wxString
& filename
)
2231 wxFFile
file(filename
, _T("wb"));
2232 if ( !file
.IsOpened() )
2238 stream
.read(buf
, WXSIZEOF(buf
));
2239 if ( !stream
.bad() ) // fail may be set on EOF, don't use operator!()
2241 if ( !file
.Write(buf
, stream
.gcount()) )
2245 while ( !stream
.eof() );
2250 #else // !wxUSE_STD_IOSTREAM
2252 bool wxTransferFileToStream(const wxString
& filename
, wxOutputStream
& stream
)
2254 wxFFile
file(filename
, _T("rb"));
2255 if ( !file
.IsOpened() )
2263 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2267 stream
.Write(buf
, nRead
);
2271 while ( !file
.Eof() );
2276 bool wxTransferStreamToFile(wxInputStream
& stream
, const wxString
& filename
)
2278 wxFFile
file(filename
, _T("wb"));
2279 if ( !file
.IsOpened() )
2285 stream
.Read(buf
, WXSIZEOF(buf
));
2287 const size_t nRead
= stream
.LastRead();
2288 if ( !nRead
|| !file
.Write(buf
, nRead
) )
2291 while ( !stream
.Eof() );
2296 #endif // wxUSE_STD_IOSTREAM/!wxUSE_STD_IOSTREAM
2298 #endif // wxUSE_DOC_VIEW_ARCHITECTURE