1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/docview.cpp
3 // Purpose: Document/view classes
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
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"
50 #if wxUSE_PRINTING_ARCHITECTURE
51 #include "wx/prntbase.h"
52 #include "wx/printdlg.h"
55 #include "wx/msgdlg.h"
56 #include "wx/choicdlg.h"
57 #include "wx/docview.h"
58 #include "wx/confbase.h"
60 #include "wx/cmdproc.h"
65 #if wxUSE_STD_IOSTREAM
66 #include "wx/ioswrap.h"
73 #include "wx/wfstream.h"
76 // ----------------------------------------------------------------------------
78 // ----------------------------------------------------------------------------
80 IMPLEMENT_ABSTRACT_CLASS(wxDocument
, wxEvtHandler
)
81 IMPLEMENT_ABSTRACT_CLASS(wxView
, wxEvtHandler
)
82 IMPLEMENT_ABSTRACT_CLASS(wxDocTemplate
, wxObject
)
83 IMPLEMENT_DYNAMIC_CLASS(wxDocManager
, wxEvtHandler
)
84 IMPLEMENT_CLASS(wxDocChildFrame
, wxFrame
)
85 IMPLEMENT_CLASS(wxDocParentFrame
, wxFrame
)
87 #if wxUSE_PRINTING_ARCHITECTURE
88 IMPLEMENT_DYNAMIC_CLASS(wxDocPrintout
, wxPrintout
)
91 IMPLEMENT_DYNAMIC_CLASS(wxFileHistory
, wxObject
)
93 // ----------------------------------------------------------------------------
94 // function prototypes
95 // ----------------------------------------------------------------------------
97 static inline wxString
FindExtension(const wxChar
*path
);
99 // ----------------------------------------------------------------------------
101 // ----------------------------------------------------------------------------
103 static const wxChar
*s_MRUEntryFormat
= wxT("&%d %s");
105 // ============================================================================
107 // ============================================================================
109 // ----------------------------------------------------------------------------
111 // ----------------------------------------------------------------------------
113 static wxString
FindExtension(const wxChar
*path
)
116 wxSplitPath(path
, NULL
, NULL
, &ext
);
118 // VZ: extensions are considered not case sensitive - is this really a good
120 return ext
.MakeLower();
123 // ----------------------------------------------------------------------------
124 // Definition of wxDocument
125 // ----------------------------------------------------------------------------
127 wxDocument::wxDocument(wxDocument
*parent
)
129 m_documentModified
= FALSE
;
130 m_documentParent
= parent
;
131 m_documentTemplate
= (wxDocTemplate
*) NULL
;
132 m_commandProcessor
= (wxCommandProcessor
*) NULL
;
136 bool wxDocument::DeleteContents()
141 wxDocument::~wxDocument()
145 if (m_commandProcessor
)
146 delete m_commandProcessor
;
148 if (GetDocumentManager())
149 GetDocumentManager()->RemoveDocument(this);
151 // Not safe to do here, since it'll invoke virtual view functions
152 // expecting to see valid derived objects: and by the time we get here,
153 // we've called destructors higher up.
157 bool wxDocument::Close()
159 if (OnSaveModified())
160 return OnCloseDocument();
165 bool wxDocument::OnCloseDocument()
172 // Note that this implicitly deletes the document when the last view is
174 bool wxDocument::DeleteAllViews()
176 wxDocManager
* manager
= GetDocumentManager();
178 wxNode
*node
= m_documentViews
.First();
181 wxView
*view
= (wxView
*)node
->Data();
185 wxNode
*next
= node
->Next();
187 delete view
; // Deletes node implicitly
190 // If we haven't yet deleted the document (for example
191 // if there were no views) then delete it.
192 if (manager
&& manager
->GetDocuments().Member(this))
198 wxView
*wxDocument::GetFirstView() const
200 if (m_documentViews
.Number() == 0)
201 return (wxView
*) NULL
;
202 return (wxView
*)m_documentViews
.First()->Data();
205 wxDocManager
*wxDocument::GetDocumentManager() const
207 return (m_documentTemplate
? m_documentTemplate
->GetDocumentManager() : (wxDocManager
*) NULL
);
210 bool wxDocument::OnNewDocument()
212 if (!OnSaveModified())
215 if (OnCloseDocument()==FALSE
) return FALSE
;
218 SetDocumentSaved(FALSE
);
221 GetDocumentManager()->MakeDefaultName(name
);
223 SetFilename(name
, TRUE
);
228 bool wxDocument::Save()
230 if (!IsModified() && m_savedYet
)
233 if ( m_documentFile
.empty() || !m_savedYet
)
236 return OnSaveDocument(m_documentFile
);
239 bool wxDocument::SaveAs()
241 wxDocTemplate
*docTemplate
= GetDocumentTemplate();
245 wxString tmp
= wxFileSelector(_("Save as"),
246 docTemplate
->GetDirectory(),
247 wxFileNameFromPath(GetFilename()),
248 docTemplate
->GetDefaultExtension(),
249 docTemplate
->GetFileFilter(),
250 wxSAVE
| wxOVERWRITE_PROMPT
,
251 GetDocumentWindow());
256 wxString
fileName(tmp
);
257 wxString path
, name
, ext
;
258 wxSplitPath(fileName
, & path
, & name
, & ext
);
260 if (ext
.IsEmpty() || ext
== wxT(""))
263 fileName
+= docTemplate
->GetDefaultExtension();
266 SetFilename(fileName
);
267 SetTitle(wxFileNameFromPath(fileName
));
269 GetDocumentManager()->AddFileToHistory(fileName
);
271 // Notify the views that the filename has changed
272 wxNode
*node
= m_documentViews
.First();
275 wxView
*view
= (wxView
*)node
->Data();
276 view
->OnChangeFilename();
280 return OnSaveDocument(m_documentFile
);
283 bool wxDocument::OnSaveDocument(const wxString
& file
)
289 if (wxTheApp
->GetAppName() != wxT(""))
290 msgTitle
= wxTheApp
->GetAppName();
292 msgTitle
= wxString(_("File error"));
294 #if wxUSE_STD_IOSTREAM
295 wxSTD ofstream
store(wxString(file
.fn_str()).mb_str());
296 if (store
.fail() || store
.bad())
298 wxFileOutputStream
store(wxString(file
.fn_str()));
299 if (store
.LastError() != wxSTREAM_NOERROR
)
302 (void)wxMessageBox(_("Sorry, could not open this file for saving."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
303 GetDocumentWindow());
307 if (!SaveObject(store
))
309 (void)wxMessageBox(_("Sorry, could not save this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
310 GetDocumentWindow());
316 SetDocumentSaved(TRUE
);
320 bool wxDocument::OnOpenDocument(const wxString
& file
)
322 if (!OnSaveModified())
326 if (wxTheApp
->GetAppName() != wxT(""))
327 msgTitle
= wxTheApp
->GetAppName();
329 msgTitle
= wxString(_("File error"));
331 #if wxUSE_STD_IOSTREAM
332 wxSTD ifstream
store(wxString(file
.fn_str()).mb_str());
333 if (store
.fail() || store
.bad())
335 wxFileInputStream
store(wxString(file
.fn_str()));
336 if (store
.LastError() != wxSTREAM_NOERROR
)
339 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
|wxICON_EXCLAMATION
,
340 GetDocumentWindow());
343 #if wxUSE_STD_IOSTREAM
344 if (!LoadObject(store
))
346 int res
= LoadObject(store
).LastError();
347 if ((res
!= wxSTREAM_NOERROR
) &&
348 (res
!= wxSTREAM_EOF
))
351 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
|wxICON_EXCLAMATION
,
352 GetDocumentWindow());
355 SetFilename(file
, TRUE
);
364 #if wxUSE_STD_IOSTREAM
365 wxSTD istream
& wxDocument::LoadObject(wxSTD istream
& stream
)
367 wxInputStream
& wxDocument::LoadObject(wxInputStream
& stream
)
373 #if wxUSE_STD_IOSTREAM
374 wxSTD ostream
& wxDocument::SaveObject(wxSTD ostream
& stream
)
376 wxOutputStream
& wxDocument::SaveObject(wxOutputStream
& stream
)
382 bool wxDocument::Revert()
388 // Get title, or filename if no title, else unnamed
389 bool wxDocument::GetPrintableName(wxString
& buf
) const
391 if (m_documentTitle
!= wxT(""))
393 buf
= m_documentTitle
;
396 else if (m_documentFile
!= wxT(""))
398 buf
= wxFileNameFromPath(m_documentFile
);
408 wxWindow
*wxDocument::GetDocumentWindow() const
410 wxView
*view
= GetFirstView();
412 return view
->GetFrame();
414 return wxTheApp
->GetTopWindow();
417 wxCommandProcessor
*wxDocument::OnCreateCommandProcessor()
419 return new wxCommandProcessor
;
422 // TRUE if safe to close
423 bool wxDocument::OnSaveModified()
428 GetPrintableName(title
);
431 if (wxTheApp
->GetAppName() != wxT(""))
432 msgTitle
= wxTheApp
->GetAppName();
434 msgTitle
= wxString(_("Warning"));
437 prompt
.Printf(_("Do you want to save changes to document %s?"),
438 (const wxChar
*)title
);
439 int res
= wxMessageBox(prompt
, msgTitle
,
440 wxYES_NO
|wxCANCEL
|wxICON_QUESTION
,
441 GetDocumentWindow());
447 else if (res
== wxYES
)
449 else if (res
== wxCANCEL
)
455 bool wxDocument::Draw(wxDC
& WXUNUSED(context
))
460 bool wxDocument::AddView(wxView
*view
)
462 if (!m_documentViews
.Member(view
))
464 m_documentViews
.Append(view
);
470 bool wxDocument::RemoveView(wxView
*view
)
472 (void)m_documentViews
.DeleteObject(view
);
477 bool wxDocument::OnCreate(const wxString
& WXUNUSED(path
), long flags
)
479 if (GetDocumentTemplate()->CreateView(this, flags
))
485 // Called after a view is added or removed.
486 // The default implementation deletes the document if
487 // there are no more views.
488 void wxDocument::OnChangedViewList()
490 if (m_documentViews
.Number() == 0)
492 if (OnSaveModified())
499 void wxDocument::UpdateAllViews(wxView
*sender
, wxObject
*hint
)
501 wxNode
*node
= m_documentViews
.First();
504 wxView
*view
= (wxView
*)node
->Data();
505 view
->OnUpdate(sender
, hint
);
510 void wxDocument::SetFilename(const wxString
& filename
, bool notifyViews
)
512 m_documentFile
= filename
;
515 // Notify the views that the filename has changed
516 wxNode
*node
= m_documentViews
.First();
519 wxView
*view
= (wxView
*)node
->Data();
520 view
->OnChangeFilename();
526 // ----------------------------------------------------------------------------
528 // ----------------------------------------------------------------------------
533 m_viewDocument
= (wxDocument
*) NULL
;
535 m_viewTypeName
= wxT("");
536 m_viewFrame
= (wxFrame
*) NULL
;
541 // GetDocumentManager()->ActivateView(this, FALSE, TRUE);
542 m_viewDocument
->RemoveView(this);
545 // Extend event processing to search the document's event table
546 bool wxView::ProcessEvent(wxEvent
& event
)
548 if ( !GetDocument() || !GetDocument()->ProcessEvent(event
) )
549 return wxEvtHandler::ProcessEvent(event
);
554 void wxView::OnActivateView(bool WXUNUSED(activate
), wxView
*WXUNUSED(activeView
), wxView
*WXUNUSED(deactiveView
))
558 void wxView::OnPrint(wxDC
*dc
, wxObject
*WXUNUSED(info
))
563 void wxView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
))
567 void wxView::OnChangeFilename()
569 if (GetFrame() && GetDocument())
573 GetDocument()->GetPrintableName(title
);
575 GetFrame()->SetTitle(title
);
579 void wxView::SetDocument(wxDocument
*doc
)
581 m_viewDocument
= doc
;
586 bool wxView::Close(bool deleteWindow
)
588 if (OnClose(deleteWindow
))
594 void wxView::Activate(bool activate
)
596 if (GetDocumentManager())
598 OnActivateView(activate
, this, GetDocumentManager()->GetCurrentView());
599 GetDocumentManager()->ActivateView(this, activate
);
603 bool wxView::OnClose(bool WXUNUSED(deleteWindow
))
605 return GetDocument() ? GetDocument()->Close() : TRUE
;
608 #if wxUSE_PRINTING_ARCHITECTURE
609 wxPrintout
*wxView::OnCreatePrintout()
611 return new wxDocPrintout(this);
613 #endif // wxUSE_PRINTING_ARCHITECTURE
615 // ----------------------------------------------------------------------------
617 // ----------------------------------------------------------------------------
619 wxDocTemplate::wxDocTemplate(wxDocManager
*manager
,
620 const wxString
& descr
,
621 const wxString
& filter
,
624 const wxString
& docTypeName
,
625 const wxString
& viewTypeName
,
626 wxClassInfo
*docClassInfo
,
627 wxClassInfo
*viewClassInfo
,
630 m_documentManager
= manager
;
631 m_description
= descr
;
634 m_fileFilter
= filter
;
636 m_docTypeName
= docTypeName
;
637 m_viewTypeName
= viewTypeName
;
638 m_documentManager
->AssociateTemplate(this);
640 m_docClassInfo
= docClassInfo
;
641 m_viewClassInfo
= viewClassInfo
;
644 wxDocTemplate::~wxDocTemplate()
646 m_documentManager
->DisassociateTemplate(this);
649 // Tries to dynamically construct an object of the right class.
650 wxDocument
*wxDocTemplate::CreateDocument(const wxString
& path
, long flags
)
653 return (wxDocument
*) NULL
;
654 wxDocument
*doc
= (wxDocument
*)m_docClassInfo
->CreateObject();
655 doc
->SetFilename(path
);
656 doc
->SetDocumentTemplate(this);
657 GetDocumentManager()->AddDocument(doc
);
658 doc
->SetCommandProcessor(doc
->OnCreateCommandProcessor());
660 if (doc
->OnCreate(path
, flags
))
664 if (GetDocumentManager()->GetDocuments().Member(doc
))
665 doc
->DeleteAllViews();
666 return (wxDocument
*) NULL
;
670 wxView
*wxDocTemplate::CreateView(wxDocument
*doc
, long flags
)
672 if (!m_viewClassInfo
)
673 return (wxView
*) NULL
;
674 wxView
*view
= (wxView
*)m_viewClassInfo
->CreateObject();
675 view
->SetDocument(doc
);
676 if (view
->OnCreate(doc
, flags
))
683 return (wxView
*) NULL
;
687 // The default (very primitive) format detection: check is the extension is
688 // that of the template
689 bool wxDocTemplate::FileMatchesTemplate(const wxString
& path
)
691 return GetDefaultExtension().IsSameAs(FindExtension(path
));
694 // ----------------------------------------------------------------------------
696 // ----------------------------------------------------------------------------
698 BEGIN_EVENT_TABLE(wxDocManager
, wxEvtHandler
)
699 EVT_MENU(wxID_OPEN
, wxDocManager::OnFileOpen
)
700 EVT_MENU(wxID_CLOSE
, wxDocManager::OnFileClose
)
701 EVT_MENU(wxID_CLOSE_ALL
, wxDocManager::OnFileCloseAll
)
702 EVT_MENU(wxID_REVERT
, wxDocManager::OnFileRevert
)
703 EVT_MENU(wxID_NEW
, wxDocManager::OnFileNew
)
704 EVT_MENU(wxID_SAVE
, wxDocManager::OnFileSave
)
705 EVT_MENU(wxID_SAVEAS
, wxDocManager::OnFileSaveAs
)
706 EVT_MENU(wxID_UNDO
, wxDocManager::OnUndo
)
707 EVT_MENU(wxID_REDO
, wxDocManager::OnRedo
)
709 EVT_UPDATE_UI(wxID_OPEN
, wxDocManager::OnUpdateFileOpen
)
710 EVT_UPDATE_UI(wxID_CLOSE
, wxDocManager::OnUpdateFileClose
)
711 EVT_UPDATE_UI(wxID_CLOSE_ALL
, wxDocManager::OnUpdateFileClose
)
712 EVT_UPDATE_UI(wxID_REVERT
, wxDocManager::OnUpdateFileRevert
)
713 EVT_UPDATE_UI(wxID_NEW
, wxDocManager::OnUpdateFileNew
)
714 EVT_UPDATE_UI(wxID_SAVE
, wxDocManager::OnUpdateFileSave
)
715 EVT_UPDATE_UI(wxID_SAVEAS
, wxDocManager::OnUpdateFileSaveAs
)
716 EVT_UPDATE_UI(wxID_UNDO
, wxDocManager::OnUpdateUndo
)
717 EVT_UPDATE_UI(wxID_REDO
, wxDocManager::OnUpdateRedo
)
719 #if wxUSE_PRINTING_ARCHITECTURE
720 EVT_MENU(wxID_PRINT
, wxDocManager::OnPrint
)
721 EVT_MENU(wxID_PRINT_SETUP
, wxDocManager::OnPrintSetup
)
722 EVT_MENU(wxID_PREVIEW
, wxDocManager::OnPreview
)
724 EVT_UPDATE_UI(wxID_PRINT
, wxDocManager::OnUpdatePrint
)
725 EVT_UPDATE_UI(wxID_PRINT_SETUP
, wxDocManager::OnUpdatePrintSetup
)
726 EVT_UPDATE_UI(wxID_PREVIEW
, wxDocManager::OnUpdatePreview
)
730 wxDocManager
* wxDocManager::sm_docManager
= (wxDocManager
*) NULL
;
732 wxDocManager::wxDocManager(long flags
, bool initialize
)
734 m_defaultDocumentNameCounter
= 1;
736 m_currentView
= (wxView
*) NULL
;
737 m_maxDocsOpen
= 10000;
738 m_fileHistory
= (wxFileHistory
*) NULL
;
741 sm_docManager
= this;
744 wxDocManager::~wxDocManager()
748 delete m_fileHistory
;
749 sm_docManager
= (wxDocManager
*) NULL
;
752 bool wxDocManager::CloseDocuments(bool force
)
754 wxNode
*node
= m_docs
.First();
757 wxDocument
*doc
= (wxDocument
*)node
->Data();
758 wxNode
*next
= node
->Next();
760 if (!doc
->Close() && !force
)
763 // Implicitly deletes the document when the last
764 // view is removed (deleted)
765 doc
->DeleteAllViews();
767 // Check document is deleted
768 if (m_docs
.Member(doc
))
771 // This assumes that documents are not connected in
772 // any way, i.e. deleting one document does NOT
779 bool wxDocManager::Clear(bool force
)
781 if (!CloseDocuments(force
))
784 wxNode
*node
= m_templates
.First();
787 wxDocTemplate
*templ
= (wxDocTemplate
*) node
->Data();
788 wxNode
* next
= node
->Next();
795 bool wxDocManager::Initialize()
797 m_fileHistory
= OnCreateFileHistory();
801 wxFileHistory
*wxDocManager::OnCreateFileHistory()
803 return new wxFileHistory
;
806 void wxDocManager::OnFileClose(wxCommandEvent
& WXUNUSED(event
))
808 wxDocument
*doc
= GetCurrentDocument();
813 doc
->DeleteAllViews();
814 if (m_docs
.Member(doc
))
819 void wxDocManager::OnFileCloseAll(wxCommandEvent
& WXUNUSED(event
))
821 CloseDocuments(FALSE
);
824 void wxDocManager::OnFileNew(wxCommandEvent
& WXUNUSED(event
))
826 CreateDocument(wxString(""), wxDOC_NEW
);
829 void wxDocManager::OnFileOpen(wxCommandEvent
& WXUNUSED(event
))
831 if ( !CreateDocument(wxString(""), 0) )
837 void wxDocManager::OnFileRevert(wxCommandEvent
& WXUNUSED(event
))
839 wxDocument
*doc
= GetCurrentDocument();
845 void wxDocManager::OnFileSave(wxCommandEvent
& WXUNUSED(event
))
847 wxDocument
*doc
= GetCurrentDocument();
853 void wxDocManager::OnFileSaveAs(wxCommandEvent
& WXUNUSED(event
))
855 wxDocument
*doc
= GetCurrentDocument();
861 void wxDocManager::OnPrint(wxCommandEvent
& WXUNUSED(event
))
863 #if wxUSE_PRINTING_ARCHITECTURE
864 wxView
*view
= GetCurrentView();
868 wxPrintout
*printout
= view
->OnCreatePrintout();
872 printer
.Print(view
->GetFrame(), printout
, TRUE
);
876 #endif // wxUSE_PRINTING_ARCHITECTURE
879 void wxDocManager::OnPrintSetup(wxCommandEvent
& WXUNUSED(event
))
881 #if wxUSE_PRINTING_ARCHITECTURE
882 wxWindow
*parentWin
= wxTheApp
->GetTopWindow();
883 wxView
*view
= GetCurrentView();
885 parentWin
= view
->GetFrame();
887 wxPrintDialogData data
;
889 wxPrintDialog
printerDialog(parentWin
, &data
);
890 printerDialog
.GetPrintDialogData().SetSetupDialog(TRUE
);
891 printerDialog
.ShowModal();
892 #endif // wxUSE_PRINTING_ARCHITECTURE
895 void wxDocManager::OnPreview(wxCommandEvent
& WXUNUSED(event
))
897 #if wxUSE_PRINTING_ARCHITECTURE
898 wxView
*view
= GetCurrentView();
902 wxPrintout
*printout
= view
->OnCreatePrintout();
905 // Pass two printout objects: for preview, and possible printing.
906 wxPrintPreviewBase
*preview
= (wxPrintPreviewBase
*) NULL
;
907 preview
= new wxPrintPreview(printout
, view
->OnCreatePrintout());
909 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, (wxFrame
*)wxTheApp
->GetTopWindow(), _("Print Preview"),
910 wxPoint(100, 100), wxSize(600, 650));
911 frame
->Centre(wxBOTH
);
915 #endif // wxUSE_PRINTING_ARCHITECTURE
918 void wxDocManager::OnUndo(wxCommandEvent
& WXUNUSED(event
))
920 wxDocument
*doc
= GetCurrentDocument();
923 if (doc
->GetCommandProcessor())
924 doc
->GetCommandProcessor()->Undo();
927 void wxDocManager::OnRedo(wxCommandEvent
& WXUNUSED(event
))
929 wxDocument
*doc
= GetCurrentDocument();
932 if (doc
->GetCommandProcessor())
933 doc
->GetCommandProcessor()->Redo();
936 // Handlers for UI update commands
938 void wxDocManager::OnUpdateFileOpen(wxUpdateUIEvent
& event
)
940 event
.Enable( TRUE
);
943 void wxDocManager::OnUpdateFileClose(wxUpdateUIEvent
& event
)
945 wxDocument
*doc
= GetCurrentDocument();
946 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
949 void wxDocManager::OnUpdateFileRevert(wxUpdateUIEvent
& event
)
951 wxDocument
*doc
= GetCurrentDocument();
952 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
955 void wxDocManager::OnUpdateFileNew(wxUpdateUIEvent
& event
)
957 event
.Enable( TRUE
);
960 void wxDocManager::OnUpdateFileSave(wxUpdateUIEvent
& event
)
962 wxDocument
*doc
= GetCurrentDocument();
963 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
966 void wxDocManager::OnUpdateFileSaveAs(wxUpdateUIEvent
& event
)
968 wxDocument
*doc
= GetCurrentDocument();
969 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
972 void wxDocManager::OnUpdateUndo(wxUpdateUIEvent
& event
)
974 wxDocument
*doc
= GetCurrentDocument();
975 event
.Enable( (doc
&& doc
->GetCommandProcessor() && doc
->GetCommandProcessor()->CanUndo()) );
978 void wxDocManager::OnUpdateRedo(wxUpdateUIEvent
& event
)
980 wxDocument
*doc
= GetCurrentDocument();
981 event
.Enable( (doc
&& doc
->GetCommandProcessor() && doc
->GetCommandProcessor()->CanRedo()) );
984 void wxDocManager::OnUpdatePrint(wxUpdateUIEvent
& event
)
986 wxDocument
*doc
= GetCurrentDocument();
987 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
990 void wxDocManager::OnUpdatePrintSetup(wxUpdateUIEvent
& event
)
992 event
.Enable( TRUE
);
995 void wxDocManager::OnUpdatePreview(wxUpdateUIEvent
& event
)
997 wxDocument
*doc
= GetCurrentDocument();
998 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1001 wxView
*wxDocManager::GetCurrentView() const
1004 return m_currentView
;
1005 if (m_docs
.Number() == 1)
1007 wxDocument
* doc
= (wxDocument
*) m_docs
.First()->Data();
1008 return doc
->GetFirstView();
1010 return (wxView
*) NULL
;
1013 // Extend event processing to search the view's event table
1014 bool wxDocManager::ProcessEvent(wxEvent
& event
)
1016 wxView
* view
= GetCurrentView();
1019 if (view
->ProcessEvent(event
))
1022 return wxEvtHandler::ProcessEvent(event
);
1025 wxDocument
*wxDocManager::CreateDocument(const wxString
& path
, long flags
)
1027 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.Number()];
1030 for (i
= 0; i
< m_templates
.Number(); i
++)
1032 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Nth(i
)->Data());
1033 if (temp
->IsVisible())
1035 templates
[n
] = temp
;
1042 return (wxDocument
*) NULL
;
1045 // If we've reached the max number of docs, close the
1047 if (GetDocuments().Number() >= m_maxDocsOpen
)
1049 wxDocument
*doc
= (wxDocument
*)GetDocuments().First()->Data();
1052 // Implicitly deletes the document when
1053 // the last view is deleted
1054 doc
->DeleteAllViews();
1056 // Check we're really deleted
1057 if (m_docs
.Member(doc
))
1063 return (wxDocument
*) NULL
;
1067 // New document: user chooses a template, unless there's only one.
1068 if (flags
& wxDOC_NEW
)
1072 wxDocTemplate
*temp
= templates
[0];
1074 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
1077 newDoc
->SetDocumentName(temp
->GetDocumentName());
1078 newDoc
->SetDocumentTemplate(temp
);
1079 newDoc
->OnNewDocument();
1084 wxDocTemplate
*temp
= SelectDocumentType(templates
, n
);
1088 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
1091 newDoc
->SetDocumentName(temp
->GetDocumentName());
1092 newDoc
->SetDocumentTemplate(temp
);
1093 newDoc
->OnNewDocument();
1098 return (wxDocument
*) NULL
;
1101 // Existing document
1102 wxDocTemplate
*temp
= (wxDocTemplate
*) NULL
;
1104 wxString
path2(wxT(""));
1105 if (path
!= wxT(""))
1108 if (flags
& wxDOC_SILENT
)
1109 temp
= FindTemplateForPath(path2
);
1111 temp
= SelectDocumentPath(templates
, n
, path2
, flags
);
1117 wxDocument
*newDoc
= temp
->CreateDocument(path2
, flags
);
1120 newDoc
->SetDocumentName(temp
->GetDocumentName());
1121 newDoc
->SetDocumentTemplate(temp
);
1122 if (!newDoc
->OnOpenDocument(path2
))
1124 newDoc
->DeleteAllViews();
1125 // delete newDoc; // Implicitly deleted by DeleteAllViews
1126 return (wxDocument
*) NULL
;
1128 AddFileToHistory(path2
);
1133 return (wxDocument
*) NULL
;
1136 wxView
*wxDocManager::CreateView(wxDocument
*doc
, long flags
)
1138 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.Number()];
1141 for (i
= 0; i
< m_templates
.Number(); i
++)
1143 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Nth(i
)->Data());
1144 if (temp
->IsVisible())
1146 if (temp
->GetDocumentName() == doc
->GetDocumentName())
1148 templates
[n
] = temp
;
1156 return (wxView
*) NULL
;
1160 wxDocTemplate
*temp
= templates
[0];
1162 wxView
*view
= temp
->CreateView(doc
, flags
);
1164 view
->SetViewName(temp
->GetViewName());
1168 wxDocTemplate
*temp
= SelectViewType(templates
, n
);
1172 wxView
*view
= temp
->CreateView(doc
, flags
);
1174 view
->SetViewName(temp
->GetViewName());
1178 return (wxView
*) NULL
;
1181 // Not yet implemented
1182 void wxDocManager::DeleteTemplate(wxDocTemplate
*WXUNUSED(temp
), long WXUNUSED(flags
))
1186 // Not yet implemented
1187 bool wxDocManager::FlushDoc(wxDocument
*WXUNUSED(doc
))
1192 wxDocument
*wxDocManager::GetCurrentDocument() const
1195 return m_currentView
->GetDocument();
1197 return (wxDocument
*) NULL
;
1200 // Make a default document name
1201 bool wxDocManager::MakeDefaultName(wxString
& name
)
1203 name
.Printf(_("unnamed%d"), m_defaultDocumentNameCounter
);
1204 m_defaultDocumentNameCounter
++;
1209 // Make a frame title (override this to do something different)
1210 // If docName is empty, a document is not currently active.
1211 wxString
wxDocManager::MakeFrameTitle(wxDocument
* doc
)
1213 wxString appName
= wxTheApp
->GetAppName();
1220 doc
->GetPrintableName(docName
);
1221 title
= docName
+ wxString(_(" - ")) + appName
;
1227 // Not yet implemented
1228 wxDocTemplate
*wxDocManager::MatchTemplate(const wxString
& WXUNUSED(path
))
1230 return (wxDocTemplate
*) NULL
;
1233 // File history management
1234 void wxDocManager::AddFileToHistory(const wxString
& file
)
1237 m_fileHistory
->AddFileToHistory(file
);
1240 void wxDocManager::RemoveFileFromHistory(int i
)
1243 m_fileHistory
->RemoveFileFromHistory(i
);
1246 wxString
wxDocManager::GetHistoryFile(int i
) const
1251 histFile
= m_fileHistory
->GetHistoryFile(i
);
1256 void wxDocManager::FileHistoryUseMenu(wxMenu
*menu
)
1259 m_fileHistory
->UseMenu(menu
);
1262 void wxDocManager::FileHistoryRemoveMenu(wxMenu
*menu
)
1265 m_fileHistory
->RemoveMenu(menu
);
1269 void wxDocManager::FileHistoryLoad(wxConfigBase
& config
)
1272 m_fileHistory
->Load(config
);
1275 void wxDocManager::FileHistorySave(wxConfigBase
& config
)
1278 m_fileHistory
->Save(config
);
1282 void wxDocManager::FileHistoryAddFilesToMenu(wxMenu
* menu
)
1285 m_fileHistory
->AddFilesToMenu(menu
);
1288 void wxDocManager::FileHistoryAddFilesToMenu()
1291 m_fileHistory
->AddFilesToMenu();
1294 int wxDocManager::GetNoHistoryFiles() const
1297 return m_fileHistory
->GetNoHistoryFiles();
1303 // Find out the document template via matching in the document file format
1304 // against that of the template
1305 wxDocTemplate
*wxDocManager::FindTemplateForPath(const wxString
& path
)
1307 wxDocTemplate
*theTemplate
= (wxDocTemplate
*) NULL
;
1309 // Find the template which this extension corresponds to
1311 for (i
= 0; i
< m_templates
.Number(); i
++)
1313 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Nth(i
)->Data();
1314 if ( temp
->FileMatchesTemplate(path
) )
1323 // Try to get a more suitable parent frame than the top window,
1324 // for selection dialogs. Otherwise you may get an unexpected
1325 // window being activated when a dialog is shown.
1326 static wxWindow
* wxFindSuitableParent()
1328 wxWindow
* parent
= wxTheApp
->GetTopWindow();
1330 wxWindow
* focusWindow
= wxWindow::FindFocus();
1333 while (focusWindow
&&
1334 !focusWindow
->IsKindOf(CLASSINFO(wxDialog
)) &&
1335 !focusWindow
->IsKindOf(CLASSINFO(wxFrame
)))
1337 focusWindow
= focusWindow
->GetParent();
1340 parent
= focusWindow
;
1345 // Prompts user to open a file, using file specs in templates.
1346 // How to implement in wxWindows? Must extend the file selector
1347 // dialog or implement own; OR match the extension to the
1348 // template extension.
1350 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**templates
,
1351 #if defined(__WXMSW__) || defined(__WXGTK__)
1354 int WXUNUSED(noTemplates
),
1357 long WXUNUSED(flags
),
1358 bool WXUNUSED(save
))
1360 // We can only have multiple filters in Windows and GTK
1361 #if defined(__WXMSW__) || defined(__WXGTK__)
1365 for (i
= 0; i
< noTemplates
; i
++)
1367 if (templates
[i
]->IsVisible())
1369 // add a '|' to separate this filter from the previous one
1370 if ( !descrBuf
.IsEmpty() )
1371 descrBuf
<< wxT('|');
1373 descrBuf
<< templates
[i
]->GetDescription()
1374 << wxT(" (") << templates
[i
]->GetFileFilter() << wxT(") |")
1375 << templates
[i
]->GetFileFilter();
1379 wxString descrBuf
= wxT("*.*");
1382 int FilterIndex
= -1;
1384 wxWindow
* parent
= wxFindSuitableParent();
1386 wxString pathTmp
= wxFileSelectorEx(_("Select a file"),
1394 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)NULL
;
1395 if (!pathTmp
.IsEmpty())
1397 if (!wxFileExists(pathTmp
))
1400 if (!wxTheApp
->GetAppName().IsEmpty())
1401 msgTitle
= wxTheApp
->GetAppName();
1403 msgTitle
= wxString(_("File error"));
1405 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
1409 return (wxDocTemplate
*) NULL
;
1411 m_lastDirectory
= wxPathOnly(pathTmp
);
1415 // first choose the template using the extension, if this fails (i.e.
1416 // wxFileSelectorEx() didn't fill it), then use the path
1417 if ( FilterIndex
!= -1 )
1418 theTemplate
= templates
[FilterIndex
];
1420 theTemplate
= FindTemplateForPath(path
);
1430 // In all other windowing systems, until we have more advanced
1431 // file selectors, we must select the document type (template) first, and
1432 // _then_ pop up the file selector.
1433 wxDocTemplate
*temp
= SelectDocumentType(templates
, noTemplates
);
1435 return (wxDocTemplate
*) NULL
;
1437 wxChar
*pathTmp
= wxFileSelector(_("Select a file"), wxT(""), wxT(""),
1438 temp
->GetDefaultExtension(),
1439 temp
->GetFileFilter(),
1440 0, wxTheApp
->GetTopWindow());
1448 return (wxDocTemplate
*) NULL
;
1452 wxDocTemplate
*wxDocManager::SelectDocumentType(wxDocTemplate
**templates
,
1453 int noTemplates
, bool sort
)
1455 wxArrayString
strings(sort
);
1456 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1459 for (i
= 0; i
< noTemplates
; i
++)
1461 if (templates
[i
]->IsVisible())
1463 strings
.Add(templates
[i
]->m_description
);
1466 data
[n
] = templates
[i
];
1474 // Yes, this will be slow, but template lists
1475 // are typically short.
1477 n
= strings
.Count();
1478 for (i
= 0; i
< n
; i
++)
1480 for (j
= 0; j
< noTemplates
; j
++)
1482 if (strings
[i
] == templates
[j
]->m_description
)
1483 data
[i
] = templates
[j
];
1488 wxDocTemplate
*theTemplate
;
1493 // no visible templates, hence nothing to choose from
1498 // don't propose the user to choose if he heas no choice
1499 theTemplate
= data
[0];
1503 // propose the user to choose one of several
1504 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1506 _("Select a document template"),
1510 wxFindSuitableParent()
1519 wxDocTemplate
*wxDocManager::SelectViewType(wxDocTemplate
**templates
,
1520 int noTemplates
, bool sort
)
1522 wxArrayString
strings(sort
);
1523 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1526 for (i
= 0; i
< noTemplates
; i
++)
1528 wxDocTemplate
*templ
= templates
[i
];
1529 if ( templ
->IsVisible() && !templ
->GetViewName().empty() )
1531 strings
.Add(templ
->m_viewTypeName
);
1542 // Yes, this will be slow, but template lists
1543 // are typically short.
1545 n
= strings
.Count();
1546 for (i
= 0; i
< n
; i
++)
1548 for (j
= 0; j
< noTemplates
; j
++)
1550 if (strings
[i
] == templates
[j
]->m_viewTypeName
)
1551 data
[i
] = templates
[j
];
1556 wxDocTemplate
*theTemplate
;
1558 // the same logic as above
1562 theTemplate
= (wxDocTemplate
*)NULL
;
1566 theTemplate
= data
[0];
1570 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1572 _("Select a document view"),
1576 wxFindSuitableParent()
1585 void wxDocManager::AssociateTemplate(wxDocTemplate
*temp
)
1587 if (!m_templates
.Member(temp
))
1588 m_templates
.Append(temp
);
1591 void wxDocManager::DisassociateTemplate(wxDocTemplate
*temp
)
1593 m_templates
.DeleteObject(temp
);
1596 // Add and remove a document from the manager's list
1597 void wxDocManager::AddDocument(wxDocument
*doc
)
1599 if (!m_docs
.Member(doc
))
1603 void wxDocManager::RemoveDocument(wxDocument
*doc
)
1605 m_docs
.DeleteObject(doc
);
1608 // Views or windows should inform the document manager
1609 // when a view is going in or out of focus
1610 void wxDocManager::ActivateView(wxView
*view
, bool activate
, bool WXUNUSED(deleting
))
1612 // If we're deactiving, and if we're not actually deleting the view, then
1613 // don't reset the current view because we may be going to
1614 // a window without a view.
1615 // WHAT DID I MEAN BY THAT EXACTLY?
1619 if (m_currentView == view)
1620 m_currentView = NULL;
1626 m_currentView
= view
;
1628 m_currentView
= (wxView
*) NULL
;
1632 // ----------------------------------------------------------------------------
1633 // Default document child frame
1634 // ----------------------------------------------------------------------------
1636 BEGIN_EVENT_TABLE(wxDocChildFrame
, wxFrame
)
1637 EVT_ACTIVATE(wxDocChildFrame::OnActivate
)
1638 EVT_CLOSE(wxDocChildFrame::OnCloseWindow
)
1641 wxDocChildFrame::wxDocChildFrame(wxDocument
*doc
,
1645 const wxString
& title
,
1649 const wxString
& name
)
1650 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1652 m_childDocument
= doc
;
1655 view
->SetFrame(this);
1658 wxDocChildFrame::~wxDocChildFrame()
1662 // Extend event processing to search the view's event table
1663 bool wxDocChildFrame::ProcessEvent(wxEvent
& event
)
1666 m_childView
->Activate(TRUE
);
1668 if ( !m_childView
|| ! m_childView
->ProcessEvent(event
) )
1670 // Only hand up to the parent if it's a menu command
1671 if (!event
.IsKindOf(CLASSINFO(wxCommandEvent
)) || !GetParent() || !GetParent()->ProcessEvent(event
))
1672 return wxEvtHandler::ProcessEvent(event
);
1680 void wxDocChildFrame::OnActivate(wxActivateEvent
& event
)
1682 wxFrame::OnActivate(event
);
1685 m_childView
->Activate(event
.GetActive());
1688 void wxDocChildFrame::OnCloseWindow(wxCloseEvent
& event
)
1693 if (!event
.CanVeto())
1694 ans
= TRUE
; // Must delete.
1696 ans
= m_childView
->Close(FALSE
); // FALSE means don't delete associated window
1700 m_childView
->Activate(FALSE
);
1702 m_childView
= (wxView
*) NULL
;
1703 m_childDocument
= (wxDocument
*) NULL
;
1714 // ----------------------------------------------------------------------------
1715 // Default parent frame
1716 // ----------------------------------------------------------------------------
1718 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1719 EVT_MENU(wxID_EXIT
, wxDocParentFrame::OnExit
)
1720 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE9
, wxDocParentFrame::OnMRUFile
)
1721 EVT_CLOSE(wxDocParentFrame::OnCloseWindow
)
1724 wxDocParentFrame::wxDocParentFrame(wxDocManager
*manager
,
1727 const wxString
& title
,
1731 const wxString
& name
)
1732 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1734 m_docManager
= manager
;
1737 void wxDocParentFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
1742 void wxDocParentFrame::OnMRUFile(wxCommandEvent
& event
)
1744 int n
= event
.GetId() - wxID_FILE1
; // the index in MRU list
1745 wxString
filename(m_docManager
->GetHistoryFile(n
));
1746 if ( !filename
.IsEmpty() )
1748 // verify that the file exists before doing anything else
1749 if ( wxFile::Exists(filename
) )
1752 (void)m_docManager
->CreateDocument(filename
, wxDOC_SILENT
);
1756 // remove the bogus filename from the MRU list and notify the user
1758 m_docManager
->RemoveFileFromHistory(n
);
1760 wxLogError(_("The file '%s' doesn't exist and couldn't be opened.\nIt has been removed from the most recently used files list."),
1766 // Extend event processing to search the view's event table
1767 bool wxDocParentFrame::ProcessEvent(wxEvent
& event
)
1769 // Try the document manager, then do default processing
1770 if (!m_docManager
|| !m_docManager
->ProcessEvent(event
))
1771 return wxEvtHandler::ProcessEvent(event
);
1776 // Define the behaviour for the frame closing
1777 // - must delete all frames except for the main one.
1778 void wxDocParentFrame::OnCloseWindow(wxCloseEvent
& event
)
1780 if (m_docManager
->Clear(!event
.CanVeto()))
1788 #if wxUSE_PRINTING_ARCHITECTURE
1790 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
)
1793 m_printoutView
= view
;
1796 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
1800 // Get the logical pixels per inch of screen and printer
1801 int ppiScreenX
, ppiScreenY
;
1802 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
1803 int ppiPrinterX
, ppiPrinterY
;
1804 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
1806 // This scales the DC so that the printout roughly represents the
1807 // the screen scaling. The text point size _should_ be the right size
1808 // but in fact is too small for some reason. This is a detail that will
1809 // need to be addressed at some point but can be fudged for the
1811 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
1813 // Now we have to check in case our real page size is reduced
1814 // (e.g. because we're drawing to a print preview memory DC)
1815 int pageWidth
, pageHeight
;
1817 dc
->GetSize(&w
, &h
);
1818 GetPageSizePixels(&pageWidth
, &pageHeight
);
1820 // If printer pageWidth == current DC width, then this doesn't
1821 // change. But w might be the preview bitmap width, so scale down.
1822 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
1823 dc
->SetUserScale(overallScale
, overallScale
);
1827 m_printoutView
->OnDraw(dc
);
1832 bool wxDocPrintout::HasPage(int pageNum
)
1834 return (pageNum
== 1);
1837 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
1839 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
1845 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
1853 #endif // wxUSE_PRINTING_ARCHITECTURE
1855 // ----------------------------------------------------------------------------
1856 // File history processor
1857 // ----------------------------------------------------------------------------
1859 wxFileHistory::wxFileHistory(int maxFiles
)
1861 m_fileMaxFiles
= maxFiles
;
1863 m_fileHistory
= new wxChar
*[m_fileMaxFiles
];
1866 wxFileHistory::~wxFileHistory()
1869 for (i
= 0; i
< m_fileHistoryN
; i
++)
1870 delete[] m_fileHistory
[i
];
1871 delete[] m_fileHistory
;
1874 // File history management
1875 void wxFileHistory::AddFileToHistory(const wxString
& file
)
1879 // Check we don't already have this file
1880 for (i
= 0; i
< m_fileHistoryN
; i
++)
1882 if ( m_fileHistory
[i
] && (file
== m_fileHistory
[i
]) )
1884 // we do have it, move it to the top of the history
1885 RemoveFileFromHistory (i
);
1886 AddFileToHistory (file
);
1891 // if we already have a full history, delete the one at the end
1892 if ( m_fileMaxFiles
== m_fileHistoryN
)
1894 RemoveFileFromHistory (m_fileHistoryN
- 1);
1895 AddFileToHistory (file
);
1899 // Add to the project file history:
1900 // Move existing files (if any) down so we can insert file at beginning.
1901 if (m_fileHistoryN
< m_fileMaxFiles
)
1903 wxNode
* node
= m_fileMenus
.First();
1906 wxMenu
* menu
= (wxMenu
*) node
->Data();
1907 if (m_fileHistoryN
== 0)
1908 menu
->AppendSeparator();
1909 menu
->Append(wxID_FILE1
+m_fileHistoryN
, _("[EMPTY]"));
1910 node
= node
->Next();
1914 // Shuffle filenames down
1915 for (i
= (m_fileHistoryN
-1); i
> 0; i
--)
1917 m_fileHistory
[i
] = m_fileHistory
[i
-1];
1919 m_fileHistory
[0] = copystring(file
);
1921 // this is the directory of the last opened file
1922 wxString pathCurrent
;
1923 wxSplitPath( m_fileHistory
[0], &pathCurrent
, NULL
, NULL
);
1924 for (i
= 0; i
< m_fileHistoryN
; i
++)
1926 if ( m_fileHistory
[i
] )
1928 // if in same directory just show the filename; otherwise the full
1930 wxString pathInMenu
, path
, filename
, ext
;
1931 wxSplitPath( m_fileHistory
[i
], &path
, &filename
, &ext
);
1932 if ( path
== pathCurrent
)
1934 pathInMenu
= filename
;
1936 pathInMenu
= pathInMenu
+ wxFILE_SEP_EXT
+ ext
;
1940 // absolute path; could also set relative path
1941 pathInMenu
= m_fileHistory
[i
];
1945 buf
.Printf(s_MRUEntryFormat
, i
+ 1, pathInMenu
.c_str());
1946 wxNode
* node
= m_fileMenus
.First();
1949 wxMenu
* menu
= (wxMenu
*) node
->Data();
1950 menu
->SetLabel(wxID_FILE1
+ i
, buf
);
1951 node
= node
->Next();
1957 void wxFileHistory::RemoveFileFromHistory(int i
)
1959 wxCHECK_RET( i
< m_fileHistoryN
,
1960 wxT("invalid index in wxFileHistory::RemoveFileFromHistory") );
1962 // delete the element from the array (could use memmove() too...)
1963 delete [] m_fileHistory
[i
];
1966 for ( j
= i
; j
< m_fileHistoryN
- 1; j
++ )
1968 m_fileHistory
[j
] = m_fileHistory
[j
+ 1];
1971 wxNode
* node
= m_fileMenus
.First();
1974 wxMenu
* menu
= (wxMenu
*) node
->Data();
1977 // shuffle filenames up
1979 for ( j
= i
; j
< m_fileHistoryN
- 1; j
++ )
1981 buf
.Printf(s_MRUEntryFormat
, j
+ 1, m_fileHistory
[j
]);
1982 menu
->SetLabel(wxID_FILE1
+ j
, buf
);
1985 node
= node
->Next();
1987 // delete the last menu item which is unused now
1988 if (menu
->FindItem(wxID_FILE1
+ m_fileHistoryN
- 1))
1989 menu
->Delete(wxID_FILE1
+ m_fileHistoryN
- 1);
1991 // delete the last separator too if no more files are left
1992 if ( m_fileHistoryN
== 1 )
1994 wxMenuItemList::Node
*node
= menu
->GetMenuItems().GetLast();
1997 wxMenuItem
*menuItem
= node
->GetData();
1998 if ( menuItem
->IsSeparator() )
2000 menu
->Delete(menuItem
);
2002 //else: should we search backwards for the last separator?
2004 //else: menu is empty somehow
2011 wxString
wxFileHistory::GetHistoryFile(int i
) const
2014 if ( i
< m_fileHistoryN
)
2016 s
= m_fileHistory
[i
];
2020 wxFAIL_MSG( wxT("bad index in wxFileHistory::GetHistoryFile") );
2026 void wxFileHistory::UseMenu(wxMenu
*menu
)
2028 if (!m_fileMenus
.Member(menu
))
2029 m_fileMenus
.Append(menu
);
2032 void wxFileHistory::RemoveMenu(wxMenu
*menu
)
2034 m_fileMenus
.DeleteObject(menu
);
2038 void wxFileHistory::Load(wxConfigBase
& config
)
2042 buf
.Printf(wxT("file%d"), m_fileHistoryN
+1);
2043 wxString historyFile
;
2044 while ((m_fileHistoryN
<= m_fileMaxFiles
) && config
.Read(buf
, &historyFile
) && (historyFile
!= wxT("")))
2046 m_fileHistory
[m_fileHistoryN
] = copystring((const wxChar
*) historyFile
);
2048 buf
.Printf(wxT("file%d"), m_fileHistoryN
+1);
2049 historyFile
= wxT("");
2054 void wxFileHistory::Save(wxConfigBase
& config
)
2057 for (i
= 0; i
< m_fileHistoryN
; i
++)
2060 buf
.Printf(wxT("file%d"), i
+1);
2061 config
.Write(buf
, wxString(m_fileHistory
[i
]));
2064 #endif // wxUSE_CONFIG
2066 void wxFileHistory::AddFilesToMenu()
2068 if (m_fileHistoryN
> 0)
2070 wxNode
* node
= m_fileMenus
.First();
2073 wxMenu
* menu
= (wxMenu
*) node
->Data();
2074 menu
->AppendSeparator();
2076 for (i
= 0; i
< m_fileHistoryN
; i
++)
2078 if (m_fileHistory
[i
])
2081 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
2082 menu
->Append(wxID_FILE1
+i
, buf
);
2085 node
= node
->Next();
2090 void wxFileHistory::AddFilesToMenu(wxMenu
* menu
)
2092 if (m_fileHistoryN
> 0)
2094 menu
->AppendSeparator();
2096 for (i
= 0; i
< m_fileHistoryN
; i
++)
2098 if (m_fileHistory
[i
])
2101 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
2102 menu
->Append(wxID_FILE1
+i
, buf
);
2108 // ----------------------------------------------------------------------------
2109 // Permits compatibility with existing file formats and functions that
2110 // manipulate files directly
2111 // ----------------------------------------------------------------------------
2113 #if wxUSE_STD_IOSTREAM
2114 bool wxTransferFileToStream(const wxString
& filename
, wxSTD ostream
& stream
)
2119 if ((fd1
= wxFopen (filename
.fn_str(), _T("rb"))) == NULL
)
2122 while ((ch
= getc (fd1
)) != EOF
)
2123 stream
<< (unsigned char)ch
;
2129 bool wxTransferStreamToFile(wxSTD istream
& stream
, const wxString
& filename
)
2134 if ((fd1
= wxFopen (filename
.fn_str(), _T("wb"))) == NULL
)
2139 while (!stream
.eof())
2149 bool wxTransferFileToStream(const wxString
& filename
, wxOutputStream
& stream
)
2154 if ((fd1
= wxFopen (filename
, wxT("rb"))) == NULL
)
2157 while ((ch
= getc (fd1
)) != EOF
)
2158 stream
.PutC((char) ch
);
2164 bool wxTransferStreamToFile(wxInputStream
& stream
, const wxString
& filename
)
2169 if ((fd1
= wxFopen (filename
, wxT("wb"))) == NULL
)
2174 int len
= stream
.StreamSize();
2175 // TODO: is this the correct test for EOF?
2176 while (stream
.TellI() < (len
- 1))
2186 #endif // wxUSE_DOC_VIEW_ARCHITECTURE