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
345 if ( !store
&& !store
.eof() )
347 int res
= LoadObject(store
).LastError();
348 if ((res
!= wxSTREAM_NOERROR
) &&
349 (res
!= wxSTREAM_EOF
))
352 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
|wxICON_EXCLAMATION
,
353 GetDocumentWindow());
356 SetFilename(file
, TRUE
);
365 #if wxUSE_STD_IOSTREAM
366 wxSTD istream
& wxDocument::LoadObject(wxSTD istream
& stream
)
368 wxInputStream
& wxDocument::LoadObject(wxInputStream
& stream
)
374 #if wxUSE_STD_IOSTREAM
375 wxSTD ostream
& wxDocument::SaveObject(wxSTD ostream
& stream
)
377 wxOutputStream
& wxDocument::SaveObject(wxOutputStream
& stream
)
383 bool wxDocument::Revert()
389 // Get title, or filename if no title, else unnamed
390 bool wxDocument::GetPrintableName(wxString
& buf
) const
392 if (m_documentTitle
!= wxT(""))
394 buf
= m_documentTitle
;
397 else if (m_documentFile
!= wxT(""))
399 buf
= wxFileNameFromPath(m_documentFile
);
409 wxWindow
*wxDocument::GetDocumentWindow() const
411 wxView
*view
= GetFirstView();
413 return view
->GetFrame();
415 return wxTheApp
->GetTopWindow();
418 wxCommandProcessor
*wxDocument::OnCreateCommandProcessor()
420 return new wxCommandProcessor
;
423 // TRUE if safe to close
424 bool wxDocument::OnSaveModified()
429 GetPrintableName(title
);
432 if (wxTheApp
->GetAppName() != wxT(""))
433 msgTitle
= wxTheApp
->GetAppName();
435 msgTitle
= wxString(_("Warning"));
438 prompt
.Printf(_("Do you want to save changes to document %s?"),
439 (const wxChar
*)title
);
440 int res
= wxMessageBox(prompt
, msgTitle
,
441 wxYES_NO
|wxCANCEL
|wxICON_QUESTION
,
442 GetDocumentWindow());
448 else if (res
== wxYES
)
450 else if (res
== wxCANCEL
)
456 bool wxDocument::Draw(wxDC
& WXUNUSED(context
))
461 bool wxDocument::AddView(wxView
*view
)
463 if (!m_documentViews
.Member(view
))
465 m_documentViews
.Append(view
);
471 bool wxDocument::RemoveView(wxView
*view
)
473 (void)m_documentViews
.DeleteObject(view
);
478 bool wxDocument::OnCreate(const wxString
& WXUNUSED(path
), long flags
)
480 if (GetDocumentTemplate()->CreateView(this, flags
))
486 // Called after a view is added or removed.
487 // The default implementation deletes the document if
488 // there are no more views.
489 void wxDocument::OnChangedViewList()
491 if (m_documentViews
.Number() == 0)
493 if (OnSaveModified())
500 void wxDocument::UpdateAllViews(wxView
*sender
, wxObject
*hint
)
502 wxNode
*node
= m_documentViews
.First();
505 wxView
*view
= (wxView
*)node
->Data();
506 view
->OnUpdate(sender
, hint
);
511 void wxDocument::SetFilename(const wxString
& filename
, bool notifyViews
)
513 m_documentFile
= filename
;
516 // Notify the views that the filename has changed
517 wxNode
*node
= m_documentViews
.First();
520 wxView
*view
= (wxView
*)node
->Data();
521 view
->OnChangeFilename();
527 // ----------------------------------------------------------------------------
529 // ----------------------------------------------------------------------------
534 m_viewDocument
= (wxDocument
*) NULL
;
536 m_viewTypeName
= wxT("");
537 m_viewFrame
= (wxFrame
*) NULL
;
542 // GetDocumentManager()->ActivateView(this, FALSE, TRUE);
543 m_viewDocument
->RemoveView(this);
546 // Extend event processing to search the document's event table
547 bool wxView::ProcessEvent(wxEvent
& event
)
549 if ( !GetDocument() || !GetDocument()->ProcessEvent(event
) )
550 return wxEvtHandler::ProcessEvent(event
);
555 void wxView::OnActivateView(bool WXUNUSED(activate
), wxView
*WXUNUSED(activeView
), wxView
*WXUNUSED(deactiveView
))
559 void wxView::OnPrint(wxDC
*dc
, wxObject
*WXUNUSED(info
))
564 void wxView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
))
568 void wxView::OnChangeFilename()
570 if (GetFrame() && GetDocument())
574 GetDocument()->GetPrintableName(title
);
576 GetFrame()->SetTitle(title
);
580 void wxView::SetDocument(wxDocument
*doc
)
582 m_viewDocument
= doc
;
587 bool wxView::Close(bool deleteWindow
)
589 if (OnClose(deleteWindow
))
595 void wxView::Activate(bool activate
)
597 if (GetDocumentManager())
599 OnActivateView(activate
, this, GetDocumentManager()->GetCurrentView());
600 GetDocumentManager()->ActivateView(this, activate
);
604 bool wxView::OnClose(bool WXUNUSED(deleteWindow
))
606 return GetDocument() ? GetDocument()->Close() : TRUE
;
609 #if wxUSE_PRINTING_ARCHITECTURE
610 wxPrintout
*wxView::OnCreatePrintout()
612 return new wxDocPrintout(this);
614 #endif // wxUSE_PRINTING_ARCHITECTURE
616 // ----------------------------------------------------------------------------
618 // ----------------------------------------------------------------------------
620 wxDocTemplate::wxDocTemplate(wxDocManager
*manager
,
621 const wxString
& descr
,
622 const wxString
& filter
,
625 const wxString
& docTypeName
,
626 const wxString
& viewTypeName
,
627 wxClassInfo
*docClassInfo
,
628 wxClassInfo
*viewClassInfo
,
631 m_documentManager
= manager
;
632 m_description
= descr
;
635 m_fileFilter
= filter
;
637 m_docTypeName
= docTypeName
;
638 m_viewTypeName
= viewTypeName
;
639 m_documentManager
->AssociateTemplate(this);
641 m_docClassInfo
= docClassInfo
;
642 m_viewClassInfo
= viewClassInfo
;
645 wxDocTemplate::~wxDocTemplate()
647 m_documentManager
->DisassociateTemplate(this);
650 // Tries to dynamically construct an object of the right class.
651 wxDocument
*wxDocTemplate::CreateDocument(const wxString
& path
, long flags
)
654 return (wxDocument
*) NULL
;
655 wxDocument
*doc
= (wxDocument
*)m_docClassInfo
->CreateObject();
656 doc
->SetFilename(path
);
657 doc
->SetDocumentTemplate(this);
658 GetDocumentManager()->AddDocument(doc
);
659 doc
->SetCommandProcessor(doc
->OnCreateCommandProcessor());
661 if (doc
->OnCreate(path
, flags
))
665 if (GetDocumentManager()->GetDocuments().Member(doc
))
666 doc
->DeleteAllViews();
667 return (wxDocument
*) NULL
;
671 wxView
*wxDocTemplate::CreateView(wxDocument
*doc
, long flags
)
673 if (!m_viewClassInfo
)
674 return (wxView
*) NULL
;
675 wxView
*view
= (wxView
*)m_viewClassInfo
->CreateObject();
676 view
->SetDocument(doc
);
677 if (view
->OnCreate(doc
, flags
))
684 return (wxView
*) NULL
;
688 // The default (very primitive) format detection: check is the extension is
689 // that of the template
690 bool wxDocTemplate::FileMatchesTemplate(const wxString
& path
)
692 return GetDefaultExtension().IsSameAs(FindExtension(path
));
695 // ----------------------------------------------------------------------------
697 // ----------------------------------------------------------------------------
699 BEGIN_EVENT_TABLE(wxDocManager
, wxEvtHandler
)
700 EVT_MENU(wxID_OPEN
, wxDocManager::OnFileOpen
)
701 EVT_MENU(wxID_CLOSE
, wxDocManager::OnFileClose
)
702 EVT_MENU(wxID_CLOSE_ALL
, wxDocManager::OnFileCloseAll
)
703 EVT_MENU(wxID_REVERT
, wxDocManager::OnFileRevert
)
704 EVT_MENU(wxID_NEW
, wxDocManager::OnFileNew
)
705 EVT_MENU(wxID_SAVE
, wxDocManager::OnFileSave
)
706 EVT_MENU(wxID_SAVEAS
, wxDocManager::OnFileSaveAs
)
707 EVT_MENU(wxID_UNDO
, wxDocManager::OnUndo
)
708 EVT_MENU(wxID_REDO
, wxDocManager::OnRedo
)
710 EVT_UPDATE_UI(wxID_OPEN
, wxDocManager::OnUpdateFileOpen
)
711 EVT_UPDATE_UI(wxID_CLOSE
, wxDocManager::OnUpdateFileClose
)
712 EVT_UPDATE_UI(wxID_CLOSE_ALL
, wxDocManager::OnUpdateFileClose
)
713 EVT_UPDATE_UI(wxID_REVERT
, wxDocManager::OnUpdateFileRevert
)
714 EVT_UPDATE_UI(wxID_NEW
, wxDocManager::OnUpdateFileNew
)
715 EVT_UPDATE_UI(wxID_SAVE
, wxDocManager::OnUpdateFileSave
)
716 EVT_UPDATE_UI(wxID_SAVEAS
, wxDocManager::OnUpdateFileSaveAs
)
717 EVT_UPDATE_UI(wxID_UNDO
, wxDocManager::OnUpdateUndo
)
718 EVT_UPDATE_UI(wxID_REDO
, wxDocManager::OnUpdateRedo
)
720 #if wxUSE_PRINTING_ARCHITECTURE
721 EVT_MENU(wxID_PRINT
, wxDocManager::OnPrint
)
722 EVT_MENU(wxID_PRINT_SETUP
, wxDocManager::OnPrintSetup
)
723 EVT_MENU(wxID_PREVIEW
, wxDocManager::OnPreview
)
725 EVT_UPDATE_UI(wxID_PRINT
, wxDocManager::OnUpdatePrint
)
726 EVT_UPDATE_UI(wxID_PRINT_SETUP
, wxDocManager::OnUpdatePrintSetup
)
727 EVT_UPDATE_UI(wxID_PREVIEW
, wxDocManager::OnUpdatePreview
)
731 wxDocManager
* wxDocManager::sm_docManager
= (wxDocManager
*) NULL
;
733 wxDocManager::wxDocManager(long flags
, bool initialize
)
735 m_defaultDocumentNameCounter
= 1;
737 m_currentView
= (wxView
*) NULL
;
738 m_maxDocsOpen
= 10000;
739 m_fileHistory
= (wxFileHistory
*) NULL
;
742 sm_docManager
= this;
745 wxDocManager::~wxDocManager()
749 delete m_fileHistory
;
750 sm_docManager
= (wxDocManager
*) NULL
;
753 bool wxDocManager::CloseDocuments(bool force
)
755 wxNode
*node
= m_docs
.First();
758 wxDocument
*doc
= (wxDocument
*)node
->Data();
759 wxNode
*next
= node
->Next();
761 if (!doc
->Close() && !force
)
764 // Implicitly deletes the document when the last
765 // view is removed (deleted)
766 doc
->DeleteAllViews();
768 // Check document is deleted
769 if (m_docs
.Member(doc
))
772 // This assumes that documents are not connected in
773 // any way, i.e. deleting one document does NOT
780 bool wxDocManager::Clear(bool force
)
782 if (!CloseDocuments(force
))
785 wxNode
*node
= m_templates
.First();
788 wxDocTemplate
*templ
= (wxDocTemplate
*) node
->Data();
789 wxNode
* next
= node
->Next();
796 bool wxDocManager::Initialize()
798 m_fileHistory
= OnCreateFileHistory();
802 wxFileHistory
*wxDocManager::OnCreateFileHistory()
804 return new wxFileHistory
;
807 void wxDocManager::OnFileClose(wxCommandEvent
& WXUNUSED(event
))
809 wxDocument
*doc
= GetCurrentDocument();
814 doc
->DeleteAllViews();
815 if (m_docs
.Member(doc
))
820 void wxDocManager::OnFileCloseAll(wxCommandEvent
& WXUNUSED(event
))
822 CloseDocuments(FALSE
);
825 void wxDocManager::OnFileNew(wxCommandEvent
& WXUNUSED(event
))
827 CreateDocument(wxString(""), wxDOC_NEW
);
830 void wxDocManager::OnFileOpen(wxCommandEvent
& WXUNUSED(event
))
832 if ( !CreateDocument(wxString(""), 0) )
838 void wxDocManager::OnFileRevert(wxCommandEvent
& WXUNUSED(event
))
840 wxDocument
*doc
= GetCurrentDocument();
846 void wxDocManager::OnFileSave(wxCommandEvent
& WXUNUSED(event
))
848 wxDocument
*doc
= GetCurrentDocument();
854 void wxDocManager::OnFileSaveAs(wxCommandEvent
& WXUNUSED(event
))
856 wxDocument
*doc
= GetCurrentDocument();
862 void wxDocManager::OnPrint(wxCommandEvent
& WXUNUSED(event
))
864 #if wxUSE_PRINTING_ARCHITECTURE
865 wxView
*view
= GetCurrentView();
869 wxPrintout
*printout
= view
->OnCreatePrintout();
873 printer
.Print(view
->GetFrame(), printout
, TRUE
);
877 #endif // wxUSE_PRINTING_ARCHITECTURE
880 void wxDocManager::OnPrintSetup(wxCommandEvent
& WXUNUSED(event
))
882 #if wxUSE_PRINTING_ARCHITECTURE
883 wxWindow
*parentWin
= wxTheApp
->GetTopWindow();
884 wxView
*view
= GetCurrentView();
886 parentWin
= view
->GetFrame();
888 wxPrintDialogData data
;
890 wxPrintDialog
printerDialog(parentWin
, &data
);
891 printerDialog
.GetPrintDialogData().SetSetupDialog(TRUE
);
892 printerDialog
.ShowModal();
893 #endif // wxUSE_PRINTING_ARCHITECTURE
896 void wxDocManager::OnPreview(wxCommandEvent
& WXUNUSED(event
))
898 #if wxUSE_PRINTING_ARCHITECTURE
899 wxView
*view
= GetCurrentView();
903 wxPrintout
*printout
= view
->OnCreatePrintout();
906 // Pass two printout objects: for preview, and possible printing.
907 wxPrintPreviewBase
*preview
= (wxPrintPreviewBase
*) NULL
;
908 preview
= new wxPrintPreview(printout
, view
->OnCreatePrintout());
910 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, (wxFrame
*)wxTheApp
->GetTopWindow(), _("Print Preview"),
911 wxPoint(100, 100), wxSize(600, 650));
912 frame
->Centre(wxBOTH
);
916 #endif // wxUSE_PRINTING_ARCHITECTURE
919 void wxDocManager::OnUndo(wxCommandEvent
& WXUNUSED(event
))
921 wxDocument
*doc
= GetCurrentDocument();
924 if (doc
->GetCommandProcessor())
925 doc
->GetCommandProcessor()->Undo();
928 void wxDocManager::OnRedo(wxCommandEvent
& WXUNUSED(event
))
930 wxDocument
*doc
= GetCurrentDocument();
933 if (doc
->GetCommandProcessor())
934 doc
->GetCommandProcessor()->Redo();
937 // Handlers for UI update commands
939 void wxDocManager::OnUpdateFileOpen(wxUpdateUIEvent
& event
)
941 event
.Enable( TRUE
);
944 void wxDocManager::OnUpdateFileClose(wxUpdateUIEvent
& event
)
946 wxDocument
*doc
= GetCurrentDocument();
947 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
950 void wxDocManager::OnUpdateFileRevert(wxUpdateUIEvent
& event
)
952 wxDocument
*doc
= GetCurrentDocument();
953 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
956 void wxDocManager::OnUpdateFileNew(wxUpdateUIEvent
& event
)
958 event
.Enable( TRUE
);
961 void wxDocManager::OnUpdateFileSave(wxUpdateUIEvent
& event
)
963 wxDocument
*doc
= GetCurrentDocument();
964 event
.Enable( doc
&& doc
->IsModified() );
967 void wxDocManager::OnUpdateFileSaveAs(wxUpdateUIEvent
& event
)
969 wxDocument
*doc
= GetCurrentDocument();
970 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
973 void wxDocManager::OnUpdateUndo(wxUpdateUIEvent
& event
)
975 wxDocument
*doc
= GetCurrentDocument();
976 event
.Enable( (doc
&& doc
->GetCommandProcessor() && doc
->GetCommandProcessor()->CanUndo()) );
979 void wxDocManager::OnUpdateRedo(wxUpdateUIEvent
& event
)
981 wxDocument
*doc
= GetCurrentDocument();
982 event
.Enable( (doc
&& doc
->GetCommandProcessor() && doc
->GetCommandProcessor()->CanRedo()) );
985 void wxDocManager::OnUpdatePrint(wxUpdateUIEvent
& event
)
987 wxDocument
*doc
= GetCurrentDocument();
988 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
991 void wxDocManager::OnUpdatePrintSetup(wxUpdateUIEvent
& event
)
993 event
.Enable( TRUE
);
996 void wxDocManager::OnUpdatePreview(wxUpdateUIEvent
& event
)
998 wxDocument
*doc
= GetCurrentDocument();
999 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1002 wxView
*wxDocManager::GetCurrentView() const
1005 return m_currentView
;
1006 if (m_docs
.Number() == 1)
1008 wxDocument
* doc
= (wxDocument
*) m_docs
.First()->Data();
1009 return doc
->GetFirstView();
1011 return (wxView
*) NULL
;
1014 // Extend event processing to search the view's event table
1015 bool wxDocManager::ProcessEvent(wxEvent
& event
)
1017 wxView
* view
= GetCurrentView();
1020 if (view
->ProcessEvent(event
))
1023 return wxEvtHandler::ProcessEvent(event
);
1026 wxDocument
*wxDocManager::CreateDocument(const wxString
& path
, long flags
)
1028 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.Number()];
1031 for (i
= 0; i
< m_templates
.Number(); i
++)
1033 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Nth(i
)->Data());
1034 if (temp
->IsVisible())
1036 templates
[n
] = temp
;
1043 return (wxDocument
*) NULL
;
1046 // If we've reached the max number of docs, close the
1048 if (GetDocuments().Number() >= m_maxDocsOpen
)
1050 wxDocument
*doc
= (wxDocument
*)GetDocuments().First()->Data();
1053 // Implicitly deletes the document when
1054 // the last view is deleted
1055 doc
->DeleteAllViews();
1057 // Check we're really deleted
1058 if (m_docs
.Member(doc
))
1064 return (wxDocument
*) NULL
;
1068 // New document: user chooses a template, unless there's only one.
1069 if (flags
& wxDOC_NEW
)
1073 wxDocTemplate
*temp
= templates
[0];
1075 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
1078 newDoc
->SetDocumentName(temp
->GetDocumentName());
1079 newDoc
->SetDocumentTemplate(temp
);
1080 newDoc
->OnNewDocument();
1085 wxDocTemplate
*temp
= SelectDocumentType(templates
, n
);
1089 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
1092 newDoc
->SetDocumentName(temp
->GetDocumentName());
1093 newDoc
->SetDocumentTemplate(temp
);
1094 newDoc
->OnNewDocument();
1099 return (wxDocument
*) NULL
;
1102 // Existing document
1103 wxDocTemplate
*temp
= (wxDocTemplate
*) NULL
;
1105 wxString
path2(wxT(""));
1106 if (path
!= wxT(""))
1109 if (flags
& wxDOC_SILENT
)
1110 temp
= FindTemplateForPath(path2
);
1112 temp
= SelectDocumentPath(templates
, n
, path2
, flags
);
1118 wxDocument
*newDoc
= temp
->CreateDocument(path2
, flags
);
1121 newDoc
->SetDocumentName(temp
->GetDocumentName());
1122 newDoc
->SetDocumentTemplate(temp
);
1123 if (!newDoc
->OnOpenDocument(path2
))
1125 newDoc
->DeleteAllViews();
1126 // delete newDoc; // Implicitly deleted by DeleteAllViews
1127 return (wxDocument
*) NULL
;
1129 AddFileToHistory(path2
);
1134 return (wxDocument
*) NULL
;
1137 wxView
*wxDocManager::CreateView(wxDocument
*doc
, long flags
)
1139 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.Number()];
1142 for (i
= 0; i
< m_templates
.Number(); i
++)
1144 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Nth(i
)->Data());
1145 if (temp
->IsVisible())
1147 if (temp
->GetDocumentName() == doc
->GetDocumentName())
1149 templates
[n
] = temp
;
1157 return (wxView
*) NULL
;
1161 wxDocTemplate
*temp
= templates
[0];
1163 wxView
*view
= temp
->CreateView(doc
, flags
);
1165 view
->SetViewName(temp
->GetViewName());
1169 wxDocTemplate
*temp
= SelectViewType(templates
, n
);
1173 wxView
*view
= temp
->CreateView(doc
, flags
);
1175 view
->SetViewName(temp
->GetViewName());
1179 return (wxView
*) NULL
;
1182 // Not yet implemented
1183 void wxDocManager::DeleteTemplate(wxDocTemplate
*WXUNUSED(temp
), long WXUNUSED(flags
))
1187 // Not yet implemented
1188 bool wxDocManager::FlushDoc(wxDocument
*WXUNUSED(doc
))
1193 wxDocument
*wxDocManager::GetCurrentDocument() const
1195 wxView
*view
= GetCurrentView();
1197 return view
->GetDocument();
1199 return (wxDocument
*) NULL
;
1202 // Make a default document name
1203 bool wxDocManager::MakeDefaultName(wxString
& name
)
1205 name
.Printf(_("unnamed%d"), m_defaultDocumentNameCounter
);
1206 m_defaultDocumentNameCounter
++;
1211 // Make a frame title (override this to do something different)
1212 // If docName is empty, a document is not currently active.
1213 wxString
wxDocManager::MakeFrameTitle(wxDocument
* doc
)
1215 wxString appName
= wxTheApp
->GetAppName();
1222 doc
->GetPrintableName(docName
);
1223 title
= docName
+ wxString(_(" - ")) + appName
;
1229 // Not yet implemented
1230 wxDocTemplate
*wxDocManager::MatchTemplate(const wxString
& WXUNUSED(path
))
1232 return (wxDocTemplate
*) NULL
;
1235 // File history management
1236 void wxDocManager::AddFileToHistory(const wxString
& file
)
1239 m_fileHistory
->AddFileToHistory(file
);
1242 void wxDocManager::RemoveFileFromHistory(int i
)
1245 m_fileHistory
->RemoveFileFromHistory(i
);
1248 wxString
wxDocManager::GetHistoryFile(int i
) const
1253 histFile
= m_fileHistory
->GetHistoryFile(i
);
1258 void wxDocManager::FileHistoryUseMenu(wxMenu
*menu
)
1261 m_fileHistory
->UseMenu(menu
);
1264 void wxDocManager::FileHistoryRemoveMenu(wxMenu
*menu
)
1267 m_fileHistory
->RemoveMenu(menu
);
1271 void wxDocManager::FileHistoryLoad(wxConfigBase
& config
)
1274 m_fileHistory
->Load(config
);
1277 void wxDocManager::FileHistorySave(wxConfigBase
& config
)
1280 m_fileHistory
->Save(config
);
1284 void wxDocManager::FileHistoryAddFilesToMenu(wxMenu
* menu
)
1287 m_fileHistory
->AddFilesToMenu(menu
);
1290 void wxDocManager::FileHistoryAddFilesToMenu()
1293 m_fileHistory
->AddFilesToMenu();
1296 int wxDocManager::GetNoHistoryFiles() const
1299 return m_fileHistory
->GetNoHistoryFiles();
1305 // Find out the document template via matching in the document file format
1306 // against that of the template
1307 wxDocTemplate
*wxDocManager::FindTemplateForPath(const wxString
& path
)
1309 wxDocTemplate
*theTemplate
= (wxDocTemplate
*) NULL
;
1311 // Find the template which this extension corresponds to
1313 for (i
= 0; i
< m_templates
.Number(); i
++)
1315 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Nth(i
)->Data();
1316 if ( temp
->FileMatchesTemplate(path
) )
1325 // Try to get a more suitable parent frame than the top window,
1326 // for selection dialogs. Otherwise you may get an unexpected
1327 // window being activated when a dialog is shown.
1328 static wxWindow
* wxFindSuitableParent()
1330 wxWindow
* parent
= wxTheApp
->GetTopWindow();
1332 wxWindow
* focusWindow
= wxWindow::FindFocus();
1335 while (focusWindow
&&
1336 !focusWindow
->IsKindOf(CLASSINFO(wxDialog
)) &&
1337 !focusWindow
->IsKindOf(CLASSINFO(wxFrame
)))
1339 focusWindow
= focusWindow
->GetParent();
1342 parent
= focusWindow
;
1347 // Prompts user to open a file, using file specs in templates.
1348 // How to implement in wxWindows? Must extend the file selector
1349 // dialog or implement own; OR match the extension to the
1350 // template extension.
1352 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**templates
,
1353 #if defined(__WXMSW__) || defined(__WXGTK__)
1356 int WXUNUSED(noTemplates
),
1359 long WXUNUSED(flags
),
1360 bool WXUNUSED(save
))
1362 // We can only have multiple filters in Windows and GTK
1363 #if defined(__WXMSW__) || defined(__WXGTK__)
1367 for (i
= 0; i
< noTemplates
; i
++)
1369 if (templates
[i
]->IsVisible())
1371 // add a '|' to separate this filter from the previous one
1372 if ( !descrBuf
.IsEmpty() )
1373 descrBuf
<< wxT('|');
1375 descrBuf
<< templates
[i
]->GetDescription()
1376 << wxT(" (") << templates
[i
]->GetFileFilter() << wxT(") |")
1377 << templates
[i
]->GetFileFilter();
1381 wxString descrBuf
= wxT("*.*");
1384 int FilterIndex
= -1;
1386 wxWindow
* parent
= wxFindSuitableParent();
1388 wxString pathTmp
= wxFileSelectorEx(_("Select a file"),
1396 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)NULL
;
1397 if (!pathTmp
.IsEmpty())
1399 if (!wxFileExists(pathTmp
))
1402 if (!wxTheApp
->GetAppName().IsEmpty())
1403 msgTitle
= wxTheApp
->GetAppName();
1405 msgTitle
= wxString(_("File error"));
1407 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
1411 return (wxDocTemplate
*) NULL
;
1413 m_lastDirectory
= wxPathOnly(pathTmp
);
1417 // first choose the template using the extension, if this fails (i.e.
1418 // wxFileSelectorEx() didn't fill it), then use the path
1419 if ( FilterIndex
!= -1 )
1420 theTemplate
= templates
[FilterIndex
];
1422 theTemplate
= FindTemplateForPath(path
);
1432 // In all other windowing systems, until we have more advanced
1433 // file selectors, we must select the document type (template) first, and
1434 // _then_ pop up the file selector.
1435 wxDocTemplate
*temp
= SelectDocumentType(templates
, noTemplates
);
1437 return (wxDocTemplate
*) NULL
;
1439 wxChar
*pathTmp
= wxFileSelector(_("Select a file"), wxT(""), wxT(""),
1440 temp
->GetDefaultExtension(),
1441 temp
->GetFileFilter(),
1442 0, wxTheApp
->GetTopWindow());
1450 return (wxDocTemplate
*) NULL
;
1454 wxDocTemplate
*wxDocManager::SelectDocumentType(wxDocTemplate
**templates
,
1455 int noTemplates
, bool sort
)
1457 wxArrayString
strings(sort
);
1458 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1461 for (i
= 0; i
< noTemplates
; i
++)
1463 if (templates
[i
]->IsVisible())
1465 strings
.Add(templates
[i
]->m_description
);
1468 data
[n
] = templates
[i
];
1476 // Yes, this will be slow, but template lists
1477 // are typically short.
1479 n
= strings
.Count();
1480 for (i
= 0; i
< n
; i
++)
1482 for (j
= 0; j
< noTemplates
; j
++)
1484 if (strings
[i
] == templates
[j
]->m_description
)
1485 data
[i
] = templates
[j
];
1490 wxDocTemplate
*theTemplate
;
1495 // no visible templates, hence nothing to choose from
1500 // don't propose the user to choose if he heas no choice
1501 theTemplate
= data
[0];
1505 // propose the user to choose one of several
1506 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1508 _("Select a document template"),
1512 wxFindSuitableParent()
1521 wxDocTemplate
*wxDocManager::SelectViewType(wxDocTemplate
**templates
,
1522 int noTemplates
, bool sort
)
1524 wxArrayString
strings(sort
);
1525 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1528 for (i
= 0; i
< noTemplates
; i
++)
1530 wxDocTemplate
*templ
= templates
[i
];
1531 if ( templ
->IsVisible() && !templ
->GetViewName().empty() )
1533 strings
.Add(templ
->m_viewTypeName
);
1544 // Yes, this will be slow, but template lists
1545 // are typically short.
1547 n
= strings
.Count();
1548 for (i
= 0; i
< n
; i
++)
1550 for (j
= 0; j
< noTemplates
; j
++)
1552 if (strings
[i
] == templates
[j
]->m_viewTypeName
)
1553 data
[i
] = templates
[j
];
1558 wxDocTemplate
*theTemplate
;
1560 // the same logic as above
1564 theTemplate
= (wxDocTemplate
*)NULL
;
1568 theTemplate
= data
[0];
1572 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1574 _("Select a document view"),
1578 wxFindSuitableParent()
1587 void wxDocManager::AssociateTemplate(wxDocTemplate
*temp
)
1589 if (!m_templates
.Member(temp
))
1590 m_templates
.Append(temp
);
1593 void wxDocManager::DisassociateTemplate(wxDocTemplate
*temp
)
1595 m_templates
.DeleteObject(temp
);
1598 // Add and remove a document from the manager's list
1599 void wxDocManager::AddDocument(wxDocument
*doc
)
1601 if (!m_docs
.Member(doc
))
1605 void wxDocManager::RemoveDocument(wxDocument
*doc
)
1607 m_docs
.DeleteObject(doc
);
1610 // Views or windows should inform the document manager
1611 // when a view is going in or out of focus
1612 void wxDocManager::ActivateView(wxView
*view
, bool activate
, bool WXUNUSED(deleting
))
1614 // If we're deactiving, and if we're not actually deleting the view, then
1615 // don't reset the current view because we may be going to
1616 // a window without a view.
1617 // WHAT DID I MEAN BY THAT EXACTLY?
1621 if (m_currentView == view)
1622 m_currentView = NULL;
1628 m_currentView
= view
;
1630 m_currentView
= (wxView
*) NULL
;
1634 // ----------------------------------------------------------------------------
1635 // Default document child frame
1636 // ----------------------------------------------------------------------------
1638 BEGIN_EVENT_TABLE(wxDocChildFrame
, wxFrame
)
1639 EVT_ACTIVATE(wxDocChildFrame::OnActivate
)
1640 EVT_CLOSE(wxDocChildFrame::OnCloseWindow
)
1643 wxDocChildFrame::wxDocChildFrame(wxDocument
*doc
,
1647 const wxString
& title
,
1651 const wxString
& name
)
1652 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1654 m_childDocument
= doc
;
1657 view
->SetFrame(this);
1660 wxDocChildFrame::~wxDocChildFrame()
1664 // Extend event processing to search the view's event table
1665 bool wxDocChildFrame::ProcessEvent(wxEvent
& event
)
1668 m_childView
->Activate(TRUE
);
1670 if ( !m_childView
|| ! m_childView
->ProcessEvent(event
) )
1672 // Only hand up to the parent if it's a menu command
1673 if (!event
.IsKindOf(CLASSINFO(wxCommandEvent
)) || !GetParent() || !GetParent()->ProcessEvent(event
))
1674 return wxEvtHandler::ProcessEvent(event
);
1682 void wxDocChildFrame::OnActivate(wxActivateEvent
& event
)
1684 wxFrame::OnActivate(event
);
1687 m_childView
->Activate(event
.GetActive());
1690 void wxDocChildFrame::OnCloseWindow(wxCloseEvent
& event
)
1695 if (!event
.CanVeto())
1696 ans
= TRUE
; // Must delete.
1698 ans
= m_childView
->Close(FALSE
); // FALSE means don't delete associated window
1702 m_childView
->Activate(FALSE
);
1704 m_childView
= (wxView
*) NULL
;
1705 m_childDocument
= (wxDocument
*) NULL
;
1716 // ----------------------------------------------------------------------------
1717 // Default parent frame
1718 // ----------------------------------------------------------------------------
1720 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1721 EVT_MENU(wxID_EXIT
, wxDocParentFrame::OnExit
)
1722 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE9
, wxDocParentFrame::OnMRUFile
)
1723 EVT_CLOSE(wxDocParentFrame::OnCloseWindow
)
1726 wxDocParentFrame::wxDocParentFrame(wxDocManager
*manager
,
1729 const wxString
& title
,
1733 const wxString
& name
)
1734 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1736 m_docManager
= manager
;
1739 void wxDocParentFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
1744 void wxDocParentFrame::OnMRUFile(wxCommandEvent
& event
)
1746 int n
= event
.GetId() - wxID_FILE1
; // the index in MRU list
1747 wxString
filename(m_docManager
->GetHistoryFile(n
));
1748 if ( !filename
.IsEmpty() )
1750 // verify that the file exists before doing anything else
1751 if ( wxFile::Exists(filename
) )
1754 (void)m_docManager
->CreateDocument(filename
, wxDOC_SILENT
);
1758 // remove the bogus filename from the MRU list and notify the user
1760 m_docManager
->RemoveFileFromHistory(n
);
1762 wxLogError(_("The file '%s' doesn't exist and couldn't be opened.\nIt has been removed from the most recently used files list."),
1768 // Extend event processing to search the view's event table
1769 bool wxDocParentFrame::ProcessEvent(wxEvent
& event
)
1771 // Try the document manager, then do default processing
1772 if (!m_docManager
|| !m_docManager
->ProcessEvent(event
))
1773 return wxEvtHandler::ProcessEvent(event
);
1778 // Define the behaviour for the frame closing
1779 // - must delete all frames except for the main one.
1780 void wxDocParentFrame::OnCloseWindow(wxCloseEvent
& event
)
1782 if (m_docManager
->Clear(!event
.CanVeto()))
1790 #if wxUSE_PRINTING_ARCHITECTURE
1792 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
)
1795 m_printoutView
= view
;
1798 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
1802 // Get the logical pixels per inch of screen and printer
1803 int ppiScreenX
, ppiScreenY
;
1804 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
1805 int ppiPrinterX
, ppiPrinterY
;
1806 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
1808 // This scales the DC so that the printout roughly represents the
1809 // the screen scaling. The text point size _should_ be the right size
1810 // but in fact is too small for some reason. This is a detail that will
1811 // need to be addressed at some point but can be fudged for the
1813 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
1815 // Now we have to check in case our real page size is reduced
1816 // (e.g. because we're drawing to a print preview memory DC)
1817 int pageWidth
, pageHeight
;
1819 dc
->GetSize(&w
, &h
);
1820 GetPageSizePixels(&pageWidth
, &pageHeight
);
1822 // If printer pageWidth == current DC width, then this doesn't
1823 // change. But w might be the preview bitmap width, so scale down.
1824 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
1825 dc
->SetUserScale(overallScale
, overallScale
);
1829 m_printoutView
->OnDraw(dc
);
1834 bool wxDocPrintout::HasPage(int pageNum
)
1836 return (pageNum
== 1);
1839 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
1841 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
1847 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
1855 #endif // wxUSE_PRINTING_ARCHITECTURE
1857 // ----------------------------------------------------------------------------
1858 // File history processor
1859 // ----------------------------------------------------------------------------
1861 wxFileHistory::wxFileHistory(int maxFiles
)
1863 m_fileMaxFiles
= maxFiles
;
1865 m_fileHistory
= new wxChar
*[m_fileMaxFiles
];
1868 wxFileHistory::~wxFileHistory()
1871 for (i
= 0; i
< m_fileHistoryN
; i
++)
1872 delete[] m_fileHistory
[i
];
1873 delete[] m_fileHistory
;
1876 // File history management
1877 void wxFileHistory::AddFileToHistory(const wxString
& file
)
1881 // Check we don't already have this file
1882 for (i
= 0; i
< m_fileHistoryN
; i
++)
1884 if ( m_fileHistory
[i
] && (file
== m_fileHistory
[i
]) )
1886 // we do have it, move it to the top of the history
1887 RemoveFileFromHistory (i
);
1888 AddFileToHistory (file
);
1893 // if we already have a full history, delete the one at the end
1894 if ( m_fileMaxFiles
== m_fileHistoryN
)
1896 RemoveFileFromHistory (m_fileHistoryN
- 1);
1897 AddFileToHistory (file
);
1901 // Add to the project file history:
1902 // Move existing files (if any) down so we can insert file at beginning.
1903 if (m_fileHistoryN
< m_fileMaxFiles
)
1905 wxNode
* node
= m_fileMenus
.First();
1908 wxMenu
* menu
= (wxMenu
*) node
->Data();
1909 if (m_fileHistoryN
== 0)
1910 menu
->AppendSeparator();
1911 menu
->Append(wxID_FILE1
+m_fileHistoryN
, _("[EMPTY]"));
1912 node
= node
->Next();
1916 // Shuffle filenames down
1917 for (i
= (m_fileHistoryN
-1); i
> 0; i
--)
1919 m_fileHistory
[i
] = m_fileHistory
[i
-1];
1921 m_fileHistory
[0] = copystring(file
);
1923 // this is the directory of the last opened file
1924 wxString pathCurrent
;
1925 wxSplitPath( m_fileHistory
[0], &pathCurrent
, NULL
, NULL
);
1926 for (i
= 0; i
< m_fileHistoryN
; i
++)
1928 if ( m_fileHistory
[i
] )
1930 // if in same directory just show the filename; otherwise the full
1932 wxString pathInMenu
, path
, filename
, ext
;
1933 wxSplitPath( m_fileHistory
[i
], &path
, &filename
, &ext
);
1934 if ( path
== pathCurrent
)
1936 pathInMenu
= filename
;
1938 pathInMenu
= pathInMenu
+ wxFILE_SEP_EXT
+ ext
;
1942 // absolute path; could also set relative path
1943 pathInMenu
= m_fileHistory
[i
];
1947 buf
.Printf(s_MRUEntryFormat
, i
+ 1, pathInMenu
.c_str());
1948 wxNode
* node
= m_fileMenus
.First();
1951 wxMenu
* menu
= (wxMenu
*) node
->Data();
1952 menu
->SetLabel(wxID_FILE1
+ i
, buf
);
1953 node
= node
->Next();
1959 void wxFileHistory::RemoveFileFromHistory(int i
)
1961 wxCHECK_RET( i
< m_fileHistoryN
,
1962 wxT("invalid index in wxFileHistory::RemoveFileFromHistory") );
1964 // delete the element from the array (could use memmove() too...)
1965 delete [] m_fileHistory
[i
];
1968 for ( j
= i
; j
< m_fileHistoryN
- 1; j
++ )
1970 m_fileHistory
[j
] = m_fileHistory
[j
+ 1];
1973 wxNode
* node
= m_fileMenus
.First();
1976 wxMenu
* menu
= (wxMenu
*) node
->Data();
1979 // shuffle filenames up
1981 for ( j
= i
; j
< m_fileHistoryN
- 1; j
++ )
1983 buf
.Printf(s_MRUEntryFormat
, j
+ 1, m_fileHistory
[j
]);
1984 menu
->SetLabel(wxID_FILE1
+ j
, buf
);
1987 node
= node
->Next();
1989 // delete the last menu item which is unused now
1990 if (menu
->FindItem(wxID_FILE1
+ m_fileHistoryN
- 1))
1991 menu
->Delete(wxID_FILE1
+ m_fileHistoryN
- 1);
1993 // delete the last separator too if no more files are left
1994 if ( m_fileHistoryN
== 1 )
1996 wxMenuItemList::Node
*node
= menu
->GetMenuItems().GetLast();
1999 wxMenuItem
*menuItem
= node
->GetData();
2000 if ( menuItem
->IsSeparator() )
2002 menu
->Delete(menuItem
);
2004 //else: should we search backwards for the last separator?
2006 //else: menu is empty somehow
2013 wxString
wxFileHistory::GetHistoryFile(int i
) const
2016 if ( i
< m_fileHistoryN
)
2018 s
= m_fileHistory
[i
];
2022 wxFAIL_MSG( wxT("bad index in wxFileHistory::GetHistoryFile") );
2028 void wxFileHistory::UseMenu(wxMenu
*menu
)
2030 if (!m_fileMenus
.Member(menu
))
2031 m_fileMenus
.Append(menu
);
2034 void wxFileHistory::RemoveMenu(wxMenu
*menu
)
2036 m_fileMenus
.DeleteObject(menu
);
2040 void wxFileHistory::Load(wxConfigBase
& config
)
2044 buf
.Printf(wxT("file%d"), m_fileHistoryN
+1);
2045 wxString historyFile
;
2046 while ((m_fileHistoryN
<= m_fileMaxFiles
) && config
.Read(buf
, &historyFile
) && (historyFile
!= wxT("")))
2048 m_fileHistory
[m_fileHistoryN
] = copystring((const wxChar
*) historyFile
);
2050 buf
.Printf(wxT("file%d"), m_fileHistoryN
+1);
2051 historyFile
= wxT("");
2056 void wxFileHistory::Save(wxConfigBase
& config
)
2059 for (i
= 0; i
< m_fileHistoryN
; i
++)
2062 buf
.Printf(wxT("file%d"), i
+1);
2063 config
.Write(buf
, wxString(m_fileHistory
[i
]));
2066 #endif // wxUSE_CONFIG
2068 void wxFileHistory::AddFilesToMenu()
2070 if (m_fileHistoryN
> 0)
2072 wxNode
* node
= m_fileMenus
.First();
2075 wxMenu
* menu
= (wxMenu
*) node
->Data();
2076 menu
->AppendSeparator();
2078 for (i
= 0; i
< m_fileHistoryN
; i
++)
2080 if (m_fileHistory
[i
])
2083 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
2084 menu
->Append(wxID_FILE1
+i
, buf
);
2087 node
= node
->Next();
2092 void wxFileHistory::AddFilesToMenu(wxMenu
* menu
)
2094 if (m_fileHistoryN
> 0)
2096 menu
->AppendSeparator();
2098 for (i
= 0; i
< m_fileHistoryN
; i
++)
2100 if (m_fileHistory
[i
])
2103 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
2104 menu
->Append(wxID_FILE1
+i
, buf
);
2110 // ----------------------------------------------------------------------------
2111 // Permits compatibility with existing file formats and functions that
2112 // manipulate files directly
2113 // ----------------------------------------------------------------------------
2115 #if wxUSE_STD_IOSTREAM
2116 bool wxTransferFileToStream(const wxString
& filename
, wxSTD ostream
& stream
)
2121 if ((fd1
= wxFopen (filename
.fn_str(), _T("rb"))) == NULL
)
2124 while ((ch
= getc (fd1
)) != EOF
)
2125 stream
<< (unsigned char)ch
;
2131 bool wxTransferStreamToFile(wxSTD istream
& stream
, const wxString
& filename
)
2136 if ((fd1
= wxFopen (filename
.fn_str(), _T("wb"))) == NULL
)
2141 while (!stream
.eof())
2151 bool wxTransferFileToStream(const wxString
& filename
, wxOutputStream
& stream
)
2156 if ((fd1
= wxFopen (filename
, wxT("rb"))) == NULL
)
2159 while ((ch
= getc (fd1
)) != EOF
)
2160 stream
.PutC((char) ch
);
2166 bool wxTransferStreamToFile(wxInputStream
& stream
, const wxString
& filename
)
2171 if ((fd1
= wxFopen (filename
, wxT("wb"))) == NULL
)
2176 int len
= stream
.StreamSize();
2177 // TODO: is this the correct test for EOF?
2178 while (stream
.TellI() < (len
- 1))
2188 #endif // wxUSE_DOC_VIEW_ARCHITECTURE