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
))
1108 wxDocTemplate
*temp
= templates
[0];
1110 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
1114 newDoc
->SetDocumentName(temp
->GetDocumentName());
1115 newDoc
->SetDocumentTemplate(temp
);
1116 newDoc
->OnNewDocument();
1121 wxDocTemplate
*temp
= SelectDocumentType(templates
, n
);
1127 if (!CloseDocument(docToClose
, FALSE
))
1133 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
1137 newDoc
->SetDocumentName(temp
->GetDocumentName());
1138 newDoc
->SetDocumentTemplate(temp
);
1139 newDoc
->OnNewDocument();
1144 return (wxDocument
*) NULL
;
1147 // Existing document
1148 wxDocTemplate
*temp
= (wxDocTemplate
*) NULL
;
1150 wxString
path2(wxT(""));
1151 if (path
!= wxT(""))
1154 if (flags
& wxDOC_SILENT
)
1155 temp
= FindTemplateForPath(path2
);
1157 temp
= SelectDocumentPath(templates
, n
, path2
, flags
);
1165 if (!CloseDocument(docToClose
, FALSE
))
1171 wxDocument
*newDoc
= temp
->CreateDocument(path2
, flags
);
1174 newDoc
->SetDocumentName(temp
->GetDocumentName());
1175 newDoc
->SetDocumentTemplate(temp
);
1176 if (!newDoc
->OnOpenDocument(path2
))
1178 newDoc
->DeleteAllViews();
1179 // delete newDoc; // Implicitly deleted by DeleteAllViews
1180 return (wxDocument
*) NULL
;
1182 AddFileToHistory(path2
);
1187 return (wxDocument
*) NULL
;
1190 wxView
*wxDocManager::CreateView(wxDocument
*doc
, long flags
)
1192 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.GetCount()];
1195 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1197 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Item(i
)->GetData());
1198 if (temp
->IsVisible())
1200 if (temp
->GetDocumentName() == doc
->GetDocumentName())
1202 templates
[n
] = temp
;
1210 return (wxView
*) NULL
;
1214 wxDocTemplate
*temp
= templates
[0];
1216 wxView
*view
= temp
->CreateView(doc
, flags
);
1218 view
->SetViewName(temp
->GetViewName());
1222 wxDocTemplate
*temp
= SelectViewType(templates
, n
);
1226 wxView
*view
= temp
->CreateView(doc
, flags
);
1228 view
->SetViewName(temp
->GetViewName());
1232 return (wxView
*) NULL
;
1235 // Not yet implemented
1236 void wxDocManager::DeleteTemplate(wxDocTemplate
*WXUNUSED(temp
), long WXUNUSED(flags
))
1240 // Not yet implemented
1241 bool wxDocManager::FlushDoc(wxDocument
*WXUNUSED(doc
))
1246 wxDocument
*wxDocManager::GetCurrentDocument() const
1248 wxView
*view
= GetCurrentView();
1250 return view
->GetDocument();
1252 return (wxDocument
*) NULL
;
1255 // Make a default document name
1256 bool wxDocManager::MakeDefaultName(wxString
& name
)
1258 name
.Printf(_("unnamed%d"), m_defaultDocumentNameCounter
);
1259 m_defaultDocumentNameCounter
++;
1264 // Make a frame title (override this to do something different)
1265 // If docName is empty, a document is not currently active.
1266 wxString
wxDocManager::MakeFrameTitle(wxDocument
* doc
)
1268 wxString appName
= wxTheApp
->GetAppName();
1275 doc
->GetPrintableName(docName
);
1276 title
= docName
+ wxString(_(" - ")) + appName
;
1282 // Not yet implemented
1283 wxDocTemplate
*wxDocManager::MatchTemplate(const wxString
& WXUNUSED(path
))
1285 return (wxDocTemplate
*) NULL
;
1288 // File history management
1289 void wxDocManager::AddFileToHistory(const wxString
& file
)
1292 m_fileHistory
->AddFileToHistory(file
);
1295 void wxDocManager::RemoveFileFromHistory(size_t i
)
1298 m_fileHistory
->RemoveFileFromHistory(i
);
1301 wxString
wxDocManager::GetHistoryFile(size_t i
) const
1306 histFile
= m_fileHistory
->GetHistoryFile(i
);
1311 void wxDocManager::FileHistoryUseMenu(wxMenu
*menu
)
1314 m_fileHistory
->UseMenu(menu
);
1317 void wxDocManager::FileHistoryRemoveMenu(wxMenu
*menu
)
1320 m_fileHistory
->RemoveMenu(menu
);
1324 void wxDocManager::FileHistoryLoad(wxConfigBase
& config
)
1327 m_fileHistory
->Load(config
);
1330 void wxDocManager::FileHistorySave(wxConfigBase
& config
)
1333 m_fileHistory
->Save(config
);
1337 void wxDocManager::FileHistoryAddFilesToMenu(wxMenu
* menu
)
1340 m_fileHistory
->AddFilesToMenu(menu
);
1343 void wxDocManager::FileHistoryAddFilesToMenu()
1346 m_fileHistory
->AddFilesToMenu();
1349 size_t wxDocManager::GetNoHistoryFiles() const
1352 return m_fileHistory
->GetNoHistoryFiles();
1358 // Find out the document template via matching in the document file format
1359 // against that of the template
1360 wxDocTemplate
*wxDocManager::FindTemplateForPath(const wxString
& path
)
1362 wxDocTemplate
*theTemplate
= (wxDocTemplate
*) NULL
;
1364 // Find the template which this extension corresponds to
1365 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1367 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Item(i
)->GetData();
1368 if ( temp
->FileMatchesTemplate(path
) )
1377 // Try to get a more suitable parent frame than the top window,
1378 // for selection dialogs. Otherwise you may get an unexpected
1379 // window being activated when a dialog is shown.
1380 static wxWindow
* wxFindSuitableParent()
1382 wxWindow
* parent
= wxTheApp
->GetTopWindow();
1384 wxWindow
* focusWindow
= wxWindow::FindFocus();
1387 while (focusWindow
&&
1388 !focusWindow
->IsKindOf(CLASSINFO(wxDialog
)) &&
1389 !focusWindow
->IsKindOf(CLASSINFO(wxFrame
)))
1391 focusWindow
= focusWindow
->GetParent();
1394 parent
= focusWindow
;
1399 // Prompts user to open a file, using file specs in templates.
1400 // How to implement in wxWindows? Must extend the file selector
1401 // dialog or implement own; OR match the extension to the
1402 // template extension.
1404 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**templates
,
1405 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
1408 int WXUNUSED(noTemplates
),
1411 long WXUNUSED(flags
),
1412 bool WXUNUSED(save
))
1414 // We can only have multiple filters in Windows and GTK
1415 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
1419 for (i
= 0; i
< noTemplates
; i
++)
1421 if (templates
[i
]->IsVisible())
1423 // add a '|' to separate this filter from the previous one
1424 if ( !descrBuf
.IsEmpty() )
1425 descrBuf
<< wxT('|');
1427 descrBuf
<< templates
[i
]->GetDescription()
1428 << wxT(" (") << templates
[i
]->GetFileFilter() << wxT(") |")
1429 << templates
[i
]->GetFileFilter();
1433 wxString descrBuf
= wxT("*.*");
1436 int FilterIndex
= -1;
1438 wxWindow
* parent
= wxFindSuitableParent();
1440 wxString pathTmp
= wxFileSelectorEx(_("Select a file"),
1448 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)NULL
;
1449 if (!pathTmp
.IsEmpty())
1451 if (!wxFileExists(pathTmp
))
1454 if (!wxTheApp
->GetAppName().IsEmpty())
1455 msgTitle
= wxTheApp
->GetAppName();
1457 msgTitle
= wxString(_("File error"));
1459 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
1463 return (wxDocTemplate
*) NULL
;
1465 m_lastDirectory
= wxPathOnly(pathTmp
);
1469 // first choose the template using the extension, if this fails (i.e.
1470 // wxFileSelectorEx() didn't fill it), then use the path
1471 if ( FilterIndex
!= -1 )
1472 theTemplate
= templates
[FilterIndex
];
1474 theTemplate
= FindTemplateForPath(path
);
1484 // In all other windowing systems, until we have more advanced
1485 // file selectors, we must select the document type (template) first, and
1486 // _then_ pop up the file selector.
1487 wxDocTemplate
*temp
= SelectDocumentType(templates
, noTemplates
);
1489 return (wxDocTemplate
*) NULL
;
1491 wxChar
*pathTmp
= wxFileSelector(_("Select a file"), wxT(""), wxT(""),
1492 temp
->GetDefaultExtension(),
1493 temp
->GetFileFilter(),
1494 0, wxTheApp
->GetTopWindow());
1502 return (wxDocTemplate
*) NULL
;
1506 wxDocTemplate
*wxDocManager::SelectDocumentType(wxDocTemplate
**templates
,
1507 int noTemplates
, bool sort
)
1509 wxArrayString
strings(sort
);
1510 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1514 for (i
= 0; i
< noTemplates
; i
++)
1516 if (templates
[i
]->IsVisible())
1520 for (j
= 0; j
< n
; j
++)
1522 //filter out NOT unique documents + view combinations
1523 if ( templates
[i
]->m_docTypeName
== data
[j
]->m_docTypeName
&&
1524 templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
1531 strings
.Add(templates
[i
]->m_description
);
1533 data
[n
] = templates
[i
];
1541 // Yes, this will be slow, but template lists
1542 // are typically short.
1544 n
= strings
.Count();
1545 for (i
= 0; i
< n
; i
++)
1547 for (j
= 0; j
< noTemplates
; j
++)
1549 if (strings
[i
] == templates
[j
]->m_description
)
1550 data
[i
] = templates
[j
];
1555 wxDocTemplate
*theTemplate
;
1560 // no visible templates, hence nothing to choose from
1565 // don't propose the user to choose if he heas no choice
1566 theTemplate
= data
[0];
1570 // propose the user to choose one of several
1571 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1573 _("Select a document template"),
1577 wxFindSuitableParent()
1586 wxDocTemplate
*wxDocManager::SelectViewType(wxDocTemplate
**templates
,
1587 int noTemplates
, bool sort
)
1589 wxArrayString
strings(sort
);
1590 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1594 for (i
= 0; i
< noTemplates
; i
++)
1596 wxDocTemplate
*templ
= templates
[i
];
1597 if ( templ
->IsVisible() && !templ
->GetViewName().empty() )
1601 for (j
= 0; j
< n
; j
++)
1603 //filter out NOT unique views
1604 if ( templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
)
1610 strings
.Add(templ
->m_viewTypeName
);
1619 // Yes, this will be slow, but template lists
1620 // are typically short.
1622 n
= strings
.Count();
1623 for (i
= 0; i
< n
; i
++)
1625 for (j
= 0; j
< noTemplates
; j
++)
1627 if (strings
[i
] == templates
[j
]->m_viewTypeName
)
1628 data
[i
] = templates
[j
];
1633 wxDocTemplate
*theTemplate
;
1635 // the same logic as above
1639 theTemplate
= (wxDocTemplate
*)NULL
;
1643 theTemplate
= data
[0];
1647 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1649 _("Select a document view"),
1653 wxFindSuitableParent()
1662 void wxDocManager::AssociateTemplate(wxDocTemplate
*temp
)
1664 if (!m_templates
.Member(temp
))
1665 m_templates
.Append(temp
);
1668 void wxDocManager::DisassociateTemplate(wxDocTemplate
*temp
)
1670 m_templates
.DeleteObject(temp
);
1673 // Add and remove a document from the manager's list
1674 void wxDocManager::AddDocument(wxDocument
*doc
)
1676 if (!m_docs
.Member(doc
))
1680 void wxDocManager::RemoveDocument(wxDocument
*doc
)
1682 m_docs
.DeleteObject(doc
);
1685 // Views or windows should inform the document manager
1686 // when a view is going in or out of focus
1687 void wxDocManager::ActivateView(wxView
*view
, bool activate
, bool WXUNUSED(deleting
))
1689 // If we're deactiving, and if we're not actually deleting the view, then
1690 // don't reset the current view because we may be going to
1691 // a window without a view.
1692 // WHAT DID I MEAN BY THAT EXACTLY?
1696 if (m_currentView == view)
1697 m_currentView = NULL;
1703 m_currentView
= view
;
1705 m_currentView
= (wxView
*) NULL
;
1709 // ----------------------------------------------------------------------------
1710 // Default document child frame
1711 // ----------------------------------------------------------------------------
1713 BEGIN_EVENT_TABLE(wxDocChildFrame
, wxFrame
)
1714 EVT_ACTIVATE(wxDocChildFrame::OnActivate
)
1715 EVT_CLOSE(wxDocChildFrame::OnCloseWindow
)
1718 wxDocChildFrame::wxDocChildFrame(wxDocument
*doc
,
1722 const wxString
& title
,
1726 const wxString
& name
)
1727 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1729 m_childDocument
= doc
;
1732 view
->SetFrame(this);
1735 wxDocChildFrame::~wxDocChildFrame()
1739 // Extend event processing to search the view's event table
1740 bool wxDocChildFrame::ProcessEvent(wxEvent
& event
)
1743 m_childView
->Activate(TRUE
);
1745 if ( !m_childView
|| ! m_childView
->ProcessEvent(event
) )
1747 // Only hand up to the parent if it's a menu command
1748 if (!event
.IsKindOf(CLASSINFO(wxCommandEvent
)) || !GetParent() || !GetParent()->ProcessEvent(event
))
1749 return wxEvtHandler::ProcessEvent(event
);
1757 void wxDocChildFrame::OnActivate(wxActivateEvent
& event
)
1759 wxFrame::OnActivate(event
);
1762 m_childView
->Activate(event
.GetActive());
1765 void wxDocChildFrame::OnCloseWindow(wxCloseEvent
& event
)
1770 if (!event
.CanVeto())
1771 ans
= TRUE
; // Must delete.
1773 ans
= m_childView
->Close(FALSE
); // FALSE means don't delete associated window
1777 m_childView
->Activate(FALSE
);
1779 m_childView
= (wxView
*) NULL
;
1780 m_childDocument
= (wxDocument
*) NULL
;
1791 // ----------------------------------------------------------------------------
1792 // Default parent frame
1793 // ----------------------------------------------------------------------------
1795 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1796 EVT_MENU(wxID_EXIT
, wxDocParentFrame::OnExit
)
1797 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE9
, wxDocParentFrame::OnMRUFile
)
1798 EVT_CLOSE(wxDocParentFrame::OnCloseWindow
)
1801 wxDocParentFrame::wxDocParentFrame(wxDocManager
*manager
,
1804 const wxString
& title
,
1808 const wxString
& name
)
1809 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1811 m_docManager
= manager
;
1814 void wxDocParentFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
1819 void wxDocParentFrame::OnMRUFile(wxCommandEvent
& event
)
1821 int n
= event
.GetId() - wxID_FILE1
; // the index in MRU list
1822 wxString
filename(m_docManager
->GetHistoryFile(n
));
1823 if ( !filename
.IsEmpty() )
1825 // verify that the file exists before doing anything else
1826 if ( wxFile::Exists(filename
) )
1829 (void)m_docManager
->CreateDocument(filename
, wxDOC_SILENT
);
1833 // remove the bogus filename from the MRU list and notify the user
1835 m_docManager
->RemoveFileFromHistory(n
);
1837 wxLogError(_("The file '%s' doesn't exist and couldn't be opened.\nIt has been removed from the most recently used files list."),
1843 // Extend event processing to search the view's event table
1844 bool wxDocParentFrame::ProcessEvent(wxEvent
& event
)
1846 // Try the document manager, then do default processing
1847 if (!m_docManager
|| !m_docManager
->ProcessEvent(event
))
1848 return wxEvtHandler::ProcessEvent(event
);
1853 // Define the behaviour for the frame closing
1854 // - must delete all frames except for the main one.
1855 void wxDocParentFrame::OnCloseWindow(wxCloseEvent
& event
)
1857 if (m_docManager
->Clear(!event
.CanVeto()))
1865 #if wxUSE_PRINTING_ARCHITECTURE
1867 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
)
1870 m_printoutView
= view
;
1873 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
1877 // Get the logical pixels per inch of screen and printer
1878 int ppiScreenX
, ppiScreenY
;
1879 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
1880 int ppiPrinterX
, ppiPrinterY
;
1881 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
1883 // This scales the DC so that the printout roughly represents the
1884 // the screen scaling. The text point size _should_ be the right size
1885 // but in fact is too small for some reason. This is a detail that will
1886 // need to be addressed at some point but can be fudged for the
1888 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
1890 // Now we have to check in case our real page size is reduced
1891 // (e.g. because we're drawing to a print preview memory DC)
1892 int pageWidth
, pageHeight
;
1894 dc
->GetSize(&w
, &h
);
1895 GetPageSizePixels(&pageWidth
, &pageHeight
);
1897 // If printer pageWidth == current DC width, then this doesn't
1898 // change. But w might be the preview bitmap width, so scale down.
1899 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
1900 dc
->SetUserScale(overallScale
, overallScale
);
1904 m_printoutView
->OnDraw(dc
);
1909 bool wxDocPrintout::HasPage(int pageNum
)
1911 return (pageNum
== 1);
1914 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
1916 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
1922 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
1930 #endif // wxUSE_PRINTING_ARCHITECTURE
1932 // ----------------------------------------------------------------------------
1933 // File history processor
1934 // ----------------------------------------------------------------------------
1936 wxFileHistory::wxFileHistory(size_t maxFiles
, wxWindowID idBase
)
1938 m_fileMaxFiles
= maxFiles
;
1941 m_fileHistory
= new wxChar
*[m_fileMaxFiles
];
1944 wxFileHistory::~wxFileHistory()
1947 for (i
= 0; i
< m_fileHistoryN
; i
++)
1948 delete[] m_fileHistory
[i
];
1949 delete[] m_fileHistory
;
1952 // File history management
1953 void wxFileHistory::AddFileToHistory(const wxString
& file
)
1957 // Check we don't already have this file
1958 for (i
= 0; i
< m_fileHistoryN
; i
++)
1960 if ( m_fileHistory
[i
] && (file
== m_fileHistory
[i
]) )
1962 // we do have it, move it to the top of the history
1963 RemoveFileFromHistory (i
);
1964 AddFileToHistory (file
);
1969 // if we already have a full history, delete the one at the end
1970 if ( m_fileMaxFiles
== m_fileHistoryN
)
1972 RemoveFileFromHistory (m_fileHistoryN
- 1);
1973 AddFileToHistory (file
);
1977 // Add to the project file history:
1978 // Move existing files (if any) down so we can insert file at beginning.
1979 if (m_fileHistoryN
< m_fileMaxFiles
)
1981 wxNode
* node
= m_fileMenus
.GetFirst();
1984 wxMenu
* menu
= (wxMenu
*) node
->GetData();
1985 if ( m_fileHistoryN
== 0 && menu
->GetMenuItemCount() )
1987 menu
->AppendSeparator();
1989 menu
->Append(m_idBase
+m_fileHistoryN
, _("[EMPTY]"));
1990 node
= node
->GetNext();
1994 // Shuffle filenames down
1995 for (i
= (m_fileHistoryN
-1); i
> 0; i
--)
1997 m_fileHistory
[i
] = m_fileHistory
[i
-1];
1999 m_fileHistory
[0] = copystring(file
);
2001 // this is the directory of the last opened file
2002 wxString pathCurrent
;
2003 wxSplitPath( m_fileHistory
[0], &pathCurrent
, NULL
, NULL
);
2004 for (i
= 0; i
< m_fileHistoryN
; i
++)
2006 if ( m_fileHistory
[i
] )
2008 // if in same directory just show the filename; otherwise the full
2010 wxString pathInMenu
, path
, filename
, ext
;
2011 wxSplitPath( m_fileHistory
[i
], &path
, &filename
, &ext
);
2012 if ( path
== pathCurrent
)
2014 pathInMenu
= filename
;
2016 pathInMenu
= pathInMenu
+ wxFILE_SEP_EXT
+ ext
;
2020 // absolute path; could also set relative path
2021 pathInMenu
= m_fileHistory
[i
];
2025 buf
.Printf(s_MRUEntryFormat
, i
+ 1, pathInMenu
.c_str());
2026 wxNode
* node
= m_fileMenus
.GetFirst();
2029 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2030 menu
->SetLabel(m_idBase
+ i
, buf
);
2031 node
= node
->GetNext();
2037 void wxFileHistory::RemoveFileFromHistory(size_t i
)
2039 wxCHECK_RET( i
< m_fileHistoryN
,
2040 wxT("invalid index in wxFileHistory::RemoveFileFromHistory") );
2042 // delete the element from the array (could use memmove() too...)
2043 delete [] m_fileHistory
[i
];
2046 for ( j
= i
; j
< m_fileHistoryN
- 1; j
++ )
2048 m_fileHistory
[j
] = m_fileHistory
[j
+ 1];
2051 wxNode
* node
= m_fileMenus
.GetFirst();
2054 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2057 // shuffle filenames up
2059 for ( j
= i
; j
< m_fileHistoryN
- 1; j
++ )
2061 buf
.Printf(s_MRUEntryFormat
, j
+ 1, m_fileHistory
[j
]);
2062 menu
->SetLabel(m_idBase
+ j
, buf
);
2065 node
= node
->GetNext();
2067 // delete the last menu item which is unused now
2068 wxWindowID lastItemId
= m_idBase
+ m_fileHistoryN
- 1;
2069 if (menu
->FindItem(lastItemId
))
2071 menu
->Delete(lastItemId
);
2074 // delete the last separator too if no more files are left
2075 if ( m_fileHistoryN
== 1 )
2077 wxMenuItemList::Node
*node
= menu
->GetMenuItems().GetLast();
2080 wxMenuItem
*menuItem
= node
->GetData();
2081 if ( menuItem
->IsSeparator() )
2083 menu
->Delete(menuItem
);
2085 //else: should we search backwards for the last separator?
2087 //else: menu is empty somehow
2094 wxString
wxFileHistory::GetHistoryFile(size_t i
) const
2097 if ( i
< m_fileHistoryN
)
2099 s
= m_fileHistory
[i
];
2103 wxFAIL_MSG( wxT("bad index in wxFileHistory::GetHistoryFile") );
2109 void wxFileHistory::UseMenu(wxMenu
*menu
)
2111 if (!m_fileMenus
.Member(menu
))
2112 m_fileMenus
.Append(menu
);
2115 void wxFileHistory::RemoveMenu(wxMenu
*menu
)
2117 m_fileMenus
.DeleteObject(menu
);
2121 void wxFileHistory::Load(wxConfigBase
& config
)
2125 buf
.Printf(wxT("file%d"), (int)m_fileHistoryN
+1);
2126 wxString historyFile
;
2127 while ((m_fileHistoryN
< m_fileMaxFiles
) && config
.Read(buf
, &historyFile
) && (historyFile
!= wxT("")))
2129 m_fileHistory
[m_fileHistoryN
] = copystring((const wxChar
*) historyFile
);
2131 buf
.Printf(wxT("file%d"), (int)m_fileHistoryN
+1);
2132 historyFile
= wxT("");
2137 void wxFileHistory::Save(wxConfigBase
& config
)
2140 for (i
= 0; i
< m_fileHistoryN
; i
++)
2143 buf
.Printf(wxT("file%d"), (int)i
+1);
2144 config
.Write(buf
, wxString(m_fileHistory
[i
]));
2147 #endif // wxUSE_CONFIG
2149 void wxFileHistory::AddFilesToMenu()
2151 if (m_fileHistoryN
> 0)
2153 wxNode
* node
= m_fileMenus
.GetFirst();
2156 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2157 if (menu
->GetMenuItemCount())
2159 menu
->AppendSeparator();
2163 for (i
= 0; i
< m_fileHistoryN
; i
++)
2165 if (m_fileHistory
[i
])
2168 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
2169 menu
->Append(m_idBase
+i
, buf
);
2172 node
= node
->GetNext();
2177 void wxFileHistory::AddFilesToMenu(wxMenu
* menu
)
2179 if (m_fileHistoryN
> 0)
2181 if (menu
->GetMenuItemCount())
2183 menu
->AppendSeparator();
2187 for (i
= 0; i
< m_fileHistoryN
; i
++)
2189 if (m_fileHistory
[i
])
2192 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
2193 menu
->Append(m_idBase
+i
, buf
);
2199 // ----------------------------------------------------------------------------
2200 // Permits compatibility with existing file formats and functions that
2201 // manipulate files directly
2202 // ----------------------------------------------------------------------------
2204 #if wxUSE_STD_IOSTREAM
2206 bool wxTransferFileToStream(const wxString
& filename
, wxSTD ostream
& stream
)
2208 wxFFile
file(filename
, _T("rb"));
2209 if ( !file
.IsOpened() )
2217 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2221 stream
.write(buf
, nRead
);
2225 while ( !file
.Eof() );
2230 bool wxTransferStreamToFile(wxSTD istream
& stream
, const wxString
& filename
)
2232 wxFFile
file(filename
, _T("wb"));
2233 if ( !file
.IsOpened() )
2239 stream
.read(buf
, WXSIZEOF(buf
));
2240 if ( !stream
.bad() ) // fail may be set on EOF, don't use operator!()
2242 if ( !file
.Write(buf
, stream
.gcount()) )
2246 while ( !stream
.eof() );
2251 #else // !wxUSE_STD_IOSTREAM
2253 bool wxTransferFileToStream(const wxString
& filename
, wxOutputStream
& stream
)
2255 wxFFile
file(filename
, _T("rb"));
2256 if ( !file
.IsOpened() )
2264 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2268 stream
.Write(buf
, nRead
);
2272 while ( !file
.Eof() );
2277 bool wxTransferStreamToFile(wxInputStream
& stream
, const wxString
& filename
)
2279 wxFFile
file(filename
, _T("wb"));
2280 if ( !file
.IsOpened() )
2286 stream
.Read(buf
, WXSIZEOF(buf
));
2288 const size_t nRead
= stream
.LastRead();
2289 if ( !nRead
|| !file
.Write(buf
, nRead
) )
2292 while ( !stream
.Eof() );
2297 #endif // wxUSE_STD_IOSTREAM/!wxUSE_STD_IOSTREAM
2299 #endif // wxUSE_DOC_VIEW_ARCHITECTURE