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 (GetDocument() && 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()) );
977 if (doc
&& doc
->GetCommandProcessor())
978 doc
->GetCommandProcessor()->SetMenuStrings();
981 void wxDocManager::OnUpdateRedo(wxUpdateUIEvent
& event
)
983 wxDocument
*doc
= GetCurrentDocument();
984 event
.Enable( (doc
&& doc
->GetCommandProcessor() && doc
->GetCommandProcessor()->CanRedo()) );
985 if (doc
&& doc
->GetCommandProcessor())
986 doc
->GetCommandProcessor()->SetMenuStrings();
989 void wxDocManager::OnUpdatePrint(wxUpdateUIEvent
& event
)
991 wxDocument
*doc
= GetCurrentDocument();
992 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
995 void wxDocManager::OnUpdatePrintSetup(wxUpdateUIEvent
& event
)
997 event
.Enable( TRUE
);
1000 void wxDocManager::OnUpdatePreview(wxUpdateUIEvent
& event
)
1002 wxDocument
*doc
= GetCurrentDocument();
1003 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1006 wxView
*wxDocManager::GetCurrentView() const
1009 return m_currentView
;
1010 if (m_docs
.Number() == 1)
1012 wxDocument
* doc
= (wxDocument
*) m_docs
.First()->Data();
1013 return doc
->GetFirstView();
1015 return (wxView
*) NULL
;
1018 // Extend event processing to search the view's event table
1019 bool wxDocManager::ProcessEvent(wxEvent
& event
)
1021 wxView
* view
= GetCurrentView();
1024 if (view
->ProcessEvent(event
))
1027 return wxEvtHandler::ProcessEvent(event
);
1030 wxDocument
*wxDocManager::CreateDocument(const wxString
& path
, long flags
)
1032 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.Number()];
1035 for (i
= 0; i
< m_templates
.Number(); i
++)
1037 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Nth(i
)->Data());
1038 if (temp
->IsVisible())
1040 templates
[n
] = temp
;
1047 return (wxDocument
*) NULL
;
1050 // If we've reached the max number of docs, close the
1052 if (GetDocuments().Number() >= m_maxDocsOpen
)
1054 wxDocument
*doc
= (wxDocument
*)GetDocuments().First()->Data();
1057 // Implicitly deletes the document when
1058 // the last view is deleted
1059 doc
->DeleteAllViews();
1061 // Check we're really deleted
1062 if (m_docs
.Member(doc
))
1068 return (wxDocument
*) NULL
;
1072 // New document: user chooses a template, unless there's only one.
1073 if (flags
& wxDOC_NEW
)
1077 wxDocTemplate
*temp
= templates
[0];
1079 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
1082 newDoc
->SetDocumentName(temp
->GetDocumentName());
1083 newDoc
->SetDocumentTemplate(temp
);
1084 newDoc
->OnNewDocument();
1089 wxDocTemplate
*temp
= SelectDocumentType(templates
, n
);
1093 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
1096 newDoc
->SetDocumentName(temp
->GetDocumentName());
1097 newDoc
->SetDocumentTemplate(temp
);
1098 newDoc
->OnNewDocument();
1103 return (wxDocument
*) NULL
;
1106 // Existing document
1107 wxDocTemplate
*temp
= (wxDocTemplate
*) NULL
;
1109 wxString
path2(wxT(""));
1110 if (path
!= wxT(""))
1113 if (flags
& wxDOC_SILENT
)
1114 temp
= FindTemplateForPath(path2
);
1116 temp
= SelectDocumentPath(templates
, n
, path2
, flags
);
1122 wxDocument
*newDoc
= temp
->CreateDocument(path2
, flags
);
1125 newDoc
->SetDocumentName(temp
->GetDocumentName());
1126 newDoc
->SetDocumentTemplate(temp
);
1127 if (!newDoc
->OnOpenDocument(path2
))
1129 newDoc
->DeleteAllViews();
1130 // delete newDoc; // Implicitly deleted by DeleteAllViews
1131 return (wxDocument
*) NULL
;
1133 AddFileToHistory(path2
);
1138 return (wxDocument
*) NULL
;
1141 wxView
*wxDocManager::CreateView(wxDocument
*doc
, long flags
)
1143 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.Number()];
1146 for (i
= 0; i
< m_templates
.Number(); i
++)
1148 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Nth(i
)->Data());
1149 if (temp
->IsVisible())
1151 if (temp
->GetDocumentName() == doc
->GetDocumentName())
1153 templates
[n
] = temp
;
1161 return (wxView
*) NULL
;
1165 wxDocTemplate
*temp
= templates
[0];
1167 wxView
*view
= temp
->CreateView(doc
, flags
);
1169 view
->SetViewName(temp
->GetViewName());
1173 wxDocTemplate
*temp
= SelectViewType(templates
, n
);
1177 wxView
*view
= temp
->CreateView(doc
, flags
);
1179 view
->SetViewName(temp
->GetViewName());
1183 return (wxView
*) NULL
;
1186 // Not yet implemented
1187 void wxDocManager::DeleteTemplate(wxDocTemplate
*WXUNUSED(temp
), long WXUNUSED(flags
))
1191 // Not yet implemented
1192 bool wxDocManager::FlushDoc(wxDocument
*WXUNUSED(doc
))
1197 wxDocument
*wxDocManager::GetCurrentDocument() const
1199 wxView
*view
= GetCurrentView();
1201 return view
->GetDocument();
1203 return (wxDocument
*) NULL
;
1206 // Make a default document name
1207 bool wxDocManager::MakeDefaultName(wxString
& name
)
1209 name
.Printf(_("unnamed%d"), m_defaultDocumentNameCounter
);
1210 m_defaultDocumentNameCounter
++;
1215 // Make a frame title (override this to do something different)
1216 // If docName is empty, a document is not currently active.
1217 wxString
wxDocManager::MakeFrameTitle(wxDocument
* doc
)
1219 wxString appName
= wxTheApp
->GetAppName();
1226 doc
->GetPrintableName(docName
);
1227 title
= docName
+ wxString(_(" - ")) + appName
;
1233 // Not yet implemented
1234 wxDocTemplate
*wxDocManager::MatchTemplate(const wxString
& WXUNUSED(path
))
1236 return (wxDocTemplate
*) NULL
;
1239 // File history management
1240 void wxDocManager::AddFileToHistory(const wxString
& file
)
1243 m_fileHistory
->AddFileToHistory(file
);
1246 void wxDocManager::RemoveFileFromHistory(int i
)
1249 m_fileHistory
->RemoveFileFromHistory(i
);
1252 wxString
wxDocManager::GetHistoryFile(int i
) const
1257 histFile
= m_fileHistory
->GetHistoryFile(i
);
1262 void wxDocManager::FileHistoryUseMenu(wxMenu
*menu
)
1265 m_fileHistory
->UseMenu(menu
);
1268 void wxDocManager::FileHistoryRemoveMenu(wxMenu
*menu
)
1271 m_fileHistory
->RemoveMenu(menu
);
1275 void wxDocManager::FileHistoryLoad(wxConfigBase
& config
)
1278 m_fileHistory
->Load(config
);
1281 void wxDocManager::FileHistorySave(wxConfigBase
& config
)
1284 m_fileHistory
->Save(config
);
1288 void wxDocManager::FileHistoryAddFilesToMenu(wxMenu
* menu
)
1291 m_fileHistory
->AddFilesToMenu(menu
);
1294 void wxDocManager::FileHistoryAddFilesToMenu()
1297 m_fileHistory
->AddFilesToMenu();
1300 int wxDocManager::GetNoHistoryFiles() const
1303 return m_fileHistory
->GetNoHistoryFiles();
1309 // Find out the document template via matching in the document file format
1310 // against that of the template
1311 wxDocTemplate
*wxDocManager::FindTemplateForPath(const wxString
& path
)
1313 wxDocTemplate
*theTemplate
= (wxDocTemplate
*) NULL
;
1315 // Find the template which this extension corresponds to
1317 for (i
= 0; i
< m_templates
.Number(); i
++)
1319 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Nth(i
)->Data();
1320 if ( temp
->FileMatchesTemplate(path
) )
1329 // Try to get a more suitable parent frame than the top window,
1330 // for selection dialogs. Otherwise you may get an unexpected
1331 // window being activated when a dialog is shown.
1332 static wxWindow
* wxFindSuitableParent()
1334 wxWindow
* parent
= wxTheApp
->GetTopWindow();
1336 wxWindow
* focusWindow
= wxWindow::FindFocus();
1339 while (focusWindow
&&
1340 !focusWindow
->IsKindOf(CLASSINFO(wxDialog
)) &&
1341 !focusWindow
->IsKindOf(CLASSINFO(wxFrame
)))
1343 focusWindow
= focusWindow
->GetParent();
1346 parent
= focusWindow
;
1351 // Prompts user to open a file, using file specs in templates.
1352 // How to implement in wxWindows? Must extend the file selector
1353 // dialog or implement own; OR match the extension to the
1354 // template extension.
1356 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**templates
,
1357 #if defined(__WXMSW__) || defined(__WXGTK__)
1360 int WXUNUSED(noTemplates
),
1363 long WXUNUSED(flags
),
1364 bool WXUNUSED(save
))
1366 // We can only have multiple filters in Windows and GTK
1367 #if defined(__WXMSW__) || defined(__WXGTK__)
1371 for (i
= 0; i
< noTemplates
; i
++)
1373 if (templates
[i
]->IsVisible())
1375 // add a '|' to separate this filter from the previous one
1376 if ( !descrBuf
.IsEmpty() )
1377 descrBuf
<< wxT('|');
1379 descrBuf
<< templates
[i
]->GetDescription()
1380 << wxT(" (") << templates
[i
]->GetFileFilter() << wxT(") |")
1381 << templates
[i
]->GetFileFilter();
1385 wxString descrBuf
= wxT("*.*");
1388 int FilterIndex
= -1;
1390 wxWindow
* parent
= wxFindSuitableParent();
1392 wxString pathTmp
= wxFileSelectorEx(_("Select a file"),
1400 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)NULL
;
1401 if (!pathTmp
.IsEmpty())
1403 if (!wxFileExists(pathTmp
))
1406 if (!wxTheApp
->GetAppName().IsEmpty())
1407 msgTitle
= wxTheApp
->GetAppName();
1409 msgTitle
= wxString(_("File error"));
1411 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
1415 return (wxDocTemplate
*) NULL
;
1417 m_lastDirectory
= wxPathOnly(pathTmp
);
1421 // first choose the template using the extension, if this fails (i.e.
1422 // wxFileSelectorEx() didn't fill it), then use the path
1423 if ( FilterIndex
!= -1 )
1424 theTemplate
= templates
[FilterIndex
];
1426 theTemplate
= FindTemplateForPath(path
);
1436 // In all other windowing systems, until we have more advanced
1437 // file selectors, we must select the document type (template) first, and
1438 // _then_ pop up the file selector.
1439 wxDocTemplate
*temp
= SelectDocumentType(templates
, noTemplates
);
1441 return (wxDocTemplate
*) NULL
;
1443 wxChar
*pathTmp
= wxFileSelector(_("Select a file"), wxT(""), wxT(""),
1444 temp
->GetDefaultExtension(),
1445 temp
->GetFileFilter(),
1446 0, wxTheApp
->GetTopWindow());
1454 return (wxDocTemplate
*) NULL
;
1458 wxDocTemplate
*wxDocManager::SelectDocumentType(wxDocTemplate
**templates
,
1459 int noTemplates
, bool sort
)
1461 wxArrayString
strings(sort
);
1462 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1466 for (i
= 0; i
< noTemplates
; i
++)
1468 if (templates
[i
]->IsVisible())
1472 for (j
= 0; j
< n
; j
++)
1474 //filter out NOT unique documents + view combinations
1475 if ( templates
[i
]->m_docTypeName
== data
[j
]->m_docTypeName
&&
1476 templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
1483 strings
.Add(templates
[i
]->m_description
);
1485 data
[n
] = templates
[i
];
1493 // Yes, this will be slow, but template lists
1494 // are typically short.
1496 n
= strings
.Count();
1497 for (i
= 0; i
< n
; i
++)
1499 for (j
= 0; j
< noTemplates
; j
++)
1501 if (strings
[i
] == templates
[j
]->m_description
)
1502 data
[i
] = templates
[j
];
1507 wxDocTemplate
*theTemplate
;
1512 // no visible templates, hence nothing to choose from
1517 // don't propose the user to choose if he heas no choice
1518 theTemplate
= data
[0];
1522 // propose the user to choose one of several
1523 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1525 _("Select a document template"),
1529 wxFindSuitableParent()
1538 wxDocTemplate
*wxDocManager::SelectViewType(wxDocTemplate
**templates
,
1539 int noTemplates
, bool sort
)
1541 wxArrayString
strings(sort
);
1542 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1546 for (i
= 0; i
< noTemplates
; i
++)
1548 wxDocTemplate
*templ
= templates
[i
];
1549 if ( templ
->IsVisible() && !templ
->GetViewName().empty() )
1553 for (j
= 0; j
< n
; j
++)
1555 //filter out NOT unique views
1556 if ( templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
)
1562 strings
.Add(templ
->m_viewTypeName
);
1571 // Yes, this will be slow, but template lists
1572 // are typically short.
1574 n
= strings
.Count();
1575 for (i
= 0; i
< n
; i
++)
1577 for (j
= 0; j
< noTemplates
; j
++)
1579 if (strings
[i
] == templates
[j
]->m_viewTypeName
)
1580 data
[i
] = templates
[j
];
1585 wxDocTemplate
*theTemplate
;
1587 // the same logic as above
1591 theTemplate
= (wxDocTemplate
*)NULL
;
1595 theTemplate
= data
[0];
1599 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1601 _("Select a document view"),
1605 wxFindSuitableParent()
1614 void wxDocManager::AssociateTemplate(wxDocTemplate
*temp
)
1616 if (!m_templates
.Member(temp
))
1617 m_templates
.Append(temp
);
1620 void wxDocManager::DisassociateTemplate(wxDocTemplate
*temp
)
1622 m_templates
.DeleteObject(temp
);
1625 // Add and remove a document from the manager's list
1626 void wxDocManager::AddDocument(wxDocument
*doc
)
1628 if (!m_docs
.Member(doc
))
1632 void wxDocManager::RemoveDocument(wxDocument
*doc
)
1634 m_docs
.DeleteObject(doc
);
1637 // Views or windows should inform the document manager
1638 // when a view is going in or out of focus
1639 void wxDocManager::ActivateView(wxView
*view
, bool activate
, bool WXUNUSED(deleting
))
1641 // If we're deactiving, and if we're not actually deleting the view, then
1642 // don't reset the current view because we may be going to
1643 // a window without a view.
1644 // WHAT DID I MEAN BY THAT EXACTLY?
1648 if (m_currentView == view)
1649 m_currentView = NULL;
1655 m_currentView
= view
;
1657 m_currentView
= (wxView
*) NULL
;
1661 // ----------------------------------------------------------------------------
1662 // Default document child frame
1663 // ----------------------------------------------------------------------------
1665 BEGIN_EVENT_TABLE(wxDocChildFrame
, wxFrame
)
1666 EVT_ACTIVATE(wxDocChildFrame::OnActivate
)
1667 EVT_CLOSE(wxDocChildFrame::OnCloseWindow
)
1670 wxDocChildFrame::wxDocChildFrame(wxDocument
*doc
,
1674 const wxString
& title
,
1678 const wxString
& name
)
1679 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1681 m_childDocument
= doc
;
1684 view
->SetFrame(this);
1687 wxDocChildFrame::~wxDocChildFrame()
1691 // Extend event processing to search the view's event table
1692 bool wxDocChildFrame::ProcessEvent(wxEvent
& event
)
1695 m_childView
->Activate(TRUE
);
1697 if ( !m_childView
|| ! m_childView
->ProcessEvent(event
) )
1699 // Only hand up to the parent if it's a menu command
1700 if (!event
.IsKindOf(CLASSINFO(wxCommandEvent
)) || !GetParent() || !GetParent()->ProcessEvent(event
))
1701 return wxEvtHandler::ProcessEvent(event
);
1709 void wxDocChildFrame::OnActivate(wxActivateEvent
& event
)
1711 wxFrame::OnActivate(event
);
1714 m_childView
->Activate(event
.GetActive());
1717 void wxDocChildFrame::OnCloseWindow(wxCloseEvent
& event
)
1722 if (!event
.CanVeto())
1723 ans
= TRUE
; // Must delete.
1725 ans
= m_childView
->Close(FALSE
); // FALSE means don't delete associated window
1729 m_childView
->Activate(FALSE
);
1731 m_childView
= (wxView
*) NULL
;
1732 m_childDocument
= (wxDocument
*) NULL
;
1743 // ----------------------------------------------------------------------------
1744 // Default parent frame
1745 // ----------------------------------------------------------------------------
1747 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1748 EVT_MENU(wxID_EXIT
, wxDocParentFrame::OnExit
)
1749 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE9
, wxDocParentFrame::OnMRUFile
)
1750 EVT_CLOSE(wxDocParentFrame::OnCloseWindow
)
1753 wxDocParentFrame::wxDocParentFrame(wxDocManager
*manager
,
1756 const wxString
& title
,
1760 const wxString
& name
)
1761 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1763 m_docManager
= manager
;
1766 void wxDocParentFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
1771 void wxDocParentFrame::OnMRUFile(wxCommandEvent
& event
)
1773 int n
= event
.GetId() - wxID_FILE1
; // the index in MRU list
1774 wxString
filename(m_docManager
->GetHistoryFile(n
));
1775 if ( !filename
.IsEmpty() )
1777 // verify that the file exists before doing anything else
1778 if ( wxFile::Exists(filename
) )
1781 (void)m_docManager
->CreateDocument(filename
, wxDOC_SILENT
);
1785 // remove the bogus filename from the MRU list and notify the user
1787 m_docManager
->RemoveFileFromHistory(n
);
1789 wxLogError(_("The file '%s' doesn't exist and couldn't be opened.\nIt has been removed from the most recently used files list."),
1795 // Extend event processing to search the view's event table
1796 bool wxDocParentFrame::ProcessEvent(wxEvent
& event
)
1798 // Try the document manager, then do default processing
1799 if (!m_docManager
|| !m_docManager
->ProcessEvent(event
))
1800 return wxEvtHandler::ProcessEvent(event
);
1805 // Define the behaviour for the frame closing
1806 // - must delete all frames except for the main one.
1807 void wxDocParentFrame::OnCloseWindow(wxCloseEvent
& event
)
1809 if (m_docManager
->Clear(!event
.CanVeto()))
1817 #if wxUSE_PRINTING_ARCHITECTURE
1819 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
)
1822 m_printoutView
= view
;
1825 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
1829 // Get the logical pixels per inch of screen and printer
1830 int ppiScreenX
, ppiScreenY
;
1831 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
1832 int ppiPrinterX
, ppiPrinterY
;
1833 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
1835 // This scales the DC so that the printout roughly represents the
1836 // the screen scaling. The text point size _should_ be the right size
1837 // but in fact is too small for some reason. This is a detail that will
1838 // need to be addressed at some point but can be fudged for the
1840 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
1842 // Now we have to check in case our real page size is reduced
1843 // (e.g. because we're drawing to a print preview memory DC)
1844 int pageWidth
, pageHeight
;
1846 dc
->GetSize(&w
, &h
);
1847 GetPageSizePixels(&pageWidth
, &pageHeight
);
1849 // If printer pageWidth == current DC width, then this doesn't
1850 // change. But w might be the preview bitmap width, so scale down.
1851 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
1852 dc
->SetUserScale(overallScale
, overallScale
);
1856 m_printoutView
->OnDraw(dc
);
1861 bool wxDocPrintout::HasPage(int pageNum
)
1863 return (pageNum
== 1);
1866 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
1868 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
1874 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
1882 #endif // wxUSE_PRINTING_ARCHITECTURE
1884 // ----------------------------------------------------------------------------
1885 // File history processor
1886 // ----------------------------------------------------------------------------
1888 wxFileHistory::wxFileHistory(int maxFiles
)
1890 m_fileMaxFiles
= maxFiles
;
1892 m_fileHistory
= new wxChar
*[m_fileMaxFiles
];
1895 wxFileHistory::~wxFileHistory()
1898 for (i
= 0; i
< m_fileHistoryN
; i
++)
1899 delete[] m_fileHistory
[i
];
1900 delete[] m_fileHistory
;
1903 // File history management
1904 void wxFileHistory::AddFileToHistory(const wxString
& file
)
1908 // Check we don't already have this file
1909 for (i
= 0; i
< m_fileHistoryN
; i
++)
1911 if ( m_fileHistory
[i
] && (file
== m_fileHistory
[i
]) )
1913 // we do have it, move it to the top of the history
1914 RemoveFileFromHistory (i
);
1915 AddFileToHistory (file
);
1920 // if we already have a full history, delete the one at the end
1921 if ( m_fileMaxFiles
== m_fileHistoryN
)
1923 RemoveFileFromHistory (m_fileHistoryN
- 1);
1924 AddFileToHistory (file
);
1928 // Add to the project file history:
1929 // Move existing files (if any) down so we can insert file at beginning.
1930 if (m_fileHistoryN
< m_fileMaxFiles
)
1932 wxNode
* node
= m_fileMenus
.First();
1935 wxMenu
* menu
= (wxMenu
*) node
->Data();
1936 if (m_fileHistoryN
== 0)
1937 menu
->AppendSeparator();
1938 menu
->Append(wxID_FILE1
+m_fileHistoryN
, _("[EMPTY]"));
1939 node
= node
->Next();
1943 // Shuffle filenames down
1944 for (i
= (m_fileHistoryN
-1); i
> 0; i
--)
1946 m_fileHistory
[i
] = m_fileHistory
[i
-1];
1948 m_fileHistory
[0] = copystring(file
);
1950 // this is the directory of the last opened file
1951 wxString pathCurrent
;
1952 wxSplitPath( m_fileHistory
[0], &pathCurrent
, NULL
, NULL
);
1953 for (i
= 0; i
< m_fileHistoryN
; i
++)
1955 if ( m_fileHistory
[i
] )
1957 // if in same directory just show the filename; otherwise the full
1959 wxString pathInMenu
, path
, filename
, ext
;
1960 wxSplitPath( m_fileHistory
[i
], &path
, &filename
, &ext
);
1961 if ( path
== pathCurrent
)
1963 pathInMenu
= filename
;
1965 pathInMenu
= pathInMenu
+ wxFILE_SEP_EXT
+ ext
;
1969 // absolute path; could also set relative path
1970 pathInMenu
= m_fileHistory
[i
];
1974 buf
.Printf(s_MRUEntryFormat
, i
+ 1, pathInMenu
.c_str());
1975 wxNode
* node
= m_fileMenus
.First();
1978 wxMenu
* menu
= (wxMenu
*) node
->Data();
1979 menu
->SetLabel(wxID_FILE1
+ i
, buf
);
1980 node
= node
->Next();
1986 void wxFileHistory::RemoveFileFromHistory(int i
)
1988 wxCHECK_RET( i
< m_fileHistoryN
,
1989 wxT("invalid index in wxFileHistory::RemoveFileFromHistory") );
1991 // delete the element from the array (could use memmove() too...)
1992 delete [] m_fileHistory
[i
];
1995 for ( j
= i
; j
< m_fileHistoryN
- 1; j
++ )
1997 m_fileHistory
[j
] = m_fileHistory
[j
+ 1];
2000 wxNode
* node
= m_fileMenus
.First();
2003 wxMenu
* menu
= (wxMenu
*) node
->Data();
2006 // shuffle filenames up
2008 for ( j
= i
; j
< m_fileHistoryN
- 1; j
++ )
2010 buf
.Printf(s_MRUEntryFormat
, j
+ 1, m_fileHistory
[j
]);
2011 menu
->SetLabel(wxID_FILE1
+ j
, buf
);
2014 node
= node
->Next();
2016 // delete the last menu item which is unused now
2017 if (menu
->FindItem(wxID_FILE1
+ m_fileHistoryN
- 1))
2018 menu
->Delete(wxID_FILE1
+ m_fileHistoryN
- 1);
2020 // delete the last separator too if no more files are left
2021 if ( m_fileHistoryN
== 1 )
2023 wxMenuItemList::Node
*node
= menu
->GetMenuItems().GetLast();
2026 wxMenuItem
*menuItem
= node
->GetData();
2027 if ( menuItem
->IsSeparator() )
2029 menu
->Delete(menuItem
);
2031 //else: should we search backwards for the last separator?
2033 //else: menu is empty somehow
2040 wxString
wxFileHistory::GetHistoryFile(int i
) const
2043 if ( i
< m_fileHistoryN
)
2045 s
= m_fileHistory
[i
];
2049 wxFAIL_MSG( wxT("bad index in wxFileHistory::GetHistoryFile") );
2055 void wxFileHistory::UseMenu(wxMenu
*menu
)
2057 if (!m_fileMenus
.Member(menu
))
2058 m_fileMenus
.Append(menu
);
2061 void wxFileHistory::RemoveMenu(wxMenu
*menu
)
2063 m_fileMenus
.DeleteObject(menu
);
2067 void wxFileHistory::Load(wxConfigBase
& config
)
2071 buf
.Printf(wxT("file%d"), m_fileHistoryN
+1);
2072 wxString historyFile
;
2073 while ((m_fileHistoryN
<= m_fileMaxFiles
) && config
.Read(buf
, &historyFile
) && (historyFile
!= wxT("")))
2075 m_fileHistory
[m_fileHistoryN
] = copystring((const wxChar
*) historyFile
);
2077 buf
.Printf(wxT("file%d"), m_fileHistoryN
+1);
2078 historyFile
= wxT("");
2083 void wxFileHistory::Save(wxConfigBase
& config
)
2086 for (i
= 0; i
< m_fileHistoryN
; i
++)
2089 buf
.Printf(wxT("file%d"), i
+1);
2090 config
.Write(buf
, wxString(m_fileHistory
[i
]));
2093 #endif // wxUSE_CONFIG
2095 void wxFileHistory::AddFilesToMenu()
2097 if (m_fileHistoryN
> 0)
2099 wxNode
* node
= m_fileMenus
.First();
2102 wxMenu
* menu
= (wxMenu
*) node
->Data();
2103 menu
->AppendSeparator();
2105 for (i
= 0; i
< m_fileHistoryN
; i
++)
2107 if (m_fileHistory
[i
])
2110 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
2111 menu
->Append(wxID_FILE1
+i
, buf
);
2114 node
= node
->Next();
2119 void wxFileHistory::AddFilesToMenu(wxMenu
* menu
)
2121 if (m_fileHistoryN
> 0)
2123 menu
->AppendSeparator();
2125 for (i
= 0; i
< m_fileHistoryN
; i
++)
2127 if (m_fileHistory
[i
])
2130 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
2131 menu
->Append(wxID_FILE1
+i
, buf
);
2137 // ----------------------------------------------------------------------------
2138 // Permits compatibility with existing file formats and functions that
2139 // manipulate files directly
2140 // ----------------------------------------------------------------------------
2142 #if wxUSE_STD_IOSTREAM
2143 bool wxTransferFileToStream(const wxString
& filename
, wxSTD ostream
& stream
)
2148 if ((fd1
= wxFopen (filename
.fn_str(), _T("rb"))) == NULL
)
2151 while ((ch
= getc (fd1
)) != EOF
)
2152 stream
<< (unsigned char)ch
;
2158 bool wxTransferStreamToFile(wxSTD istream
& stream
, const wxString
& filename
)
2163 if ((fd1
= wxFopen (filename
.fn_str(), _T("wb"))) == NULL
)
2168 while (!stream
.eof())
2178 bool wxTransferFileToStream(const wxString
& filename
, wxOutputStream
& stream
)
2183 if ((fd1
= wxFopen (filename
, wxT("rb"))) == NULL
)
2186 while ((ch
= getc (fd1
)) != EOF
)
2187 stream
.PutC((char) ch
);
2193 bool wxTransferStreamToFile(wxInputStream
& stream
, const wxString
& filename
)
2198 if ((fd1
= wxFopen (filename
, wxT("wb"))) == NULL
)
2203 int len
= stream
.StreamSize();
2204 // TODO: is this the correct test for EOF?
2205 while (stream
.TellI() < (len
- 1))
2215 #endif // wxUSE_DOC_VIEW_ARCHITECTURE