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()
167 // Tell all views that we're about to close
174 // Note that this implicitly deletes the document when the last view is
176 bool wxDocument::DeleteAllViews()
178 wxDocManager
* manager
= GetDocumentManager();
180 wxNode
*node
= m_documentViews
.First();
183 wxView
*view
= (wxView
*)node
->Data();
187 wxNode
*next
= node
->Next();
189 delete view
; // Deletes node implicitly
192 // If we haven't yet deleted the document (for example
193 // if there were no views) then delete it.
194 if (manager
&& manager
->GetDocuments().Member(this))
200 wxView
*wxDocument::GetFirstView() const
202 if (m_documentViews
.Number() == 0)
203 return (wxView
*) NULL
;
204 return (wxView
*)m_documentViews
.First()->Data();
207 wxDocManager
*wxDocument::GetDocumentManager() const
209 return (m_documentTemplate
? m_documentTemplate
->GetDocumentManager() : (wxDocManager
*) NULL
);
212 bool wxDocument::OnNewDocument()
214 if (!OnSaveModified())
217 if (OnCloseDocument()==FALSE
) return FALSE
;
220 SetDocumentSaved(FALSE
);
223 GetDocumentManager()->MakeDefaultName(name
);
225 SetFilename(name
, TRUE
);
230 bool wxDocument::Save()
232 if (!IsModified() && m_savedYet
)
235 if ( m_documentFile
.empty() || !m_savedYet
)
238 return OnSaveDocument(m_documentFile
);
241 bool wxDocument::SaveAs()
243 wxDocTemplate
*docTemplate
= GetDocumentTemplate();
247 wxString tmp
= wxFileSelector(_("Save as"),
248 docTemplate
->GetDirectory(),
249 wxFileNameFromPath(GetFilename()),
250 docTemplate
->GetDefaultExtension(),
251 docTemplate
->GetFileFilter(),
252 wxSAVE
| wxOVERWRITE_PROMPT
,
253 GetDocumentWindow());
258 wxString
fileName(tmp
);
259 wxString path
, name
, ext
;
260 wxSplitPath(fileName
, & path
, & name
, & ext
);
262 if (ext
.IsEmpty() || ext
== wxT(""))
265 fileName
+= docTemplate
->GetDefaultExtension();
268 SetFilename(fileName
);
269 SetTitle(wxFileNameFromPath(fileName
));
271 GetDocumentManager()->AddFileToHistory(fileName
);
273 // Notify the views that the filename has changed
274 wxNode
*node
= m_documentViews
.First();
277 wxView
*view
= (wxView
*)node
->Data();
278 view
->OnChangeFilename();
282 return OnSaveDocument(m_documentFile
);
285 bool wxDocument::OnSaveDocument(const wxString
& file
)
291 if (wxTheApp
->GetAppName() != wxT(""))
292 msgTitle
= wxTheApp
->GetAppName();
294 msgTitle
= wxString(_("File error"));
296 #if wxUSE_STD_IOSTREAM
297 wxSTD ofstream
store(wxString(file
.fn_str()).mb_str());
298 if (store
.fail() || store
.bad())
300 wxFileOutputStream
store(wxString(file
.fn_str()));
301 if (store
.LastError() != wxSTREAM_NOERROR
)
304 (void)wxMessageBox(_("Sorry, could not open this file for saving."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
305 GetDocumentWindow());
309 if (!SaveObject(store
))
311 (void)wxMessageBox(_("Sorry, could not save this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
312 GetDocumentWindow());
318 SetDocumentSaved(TRUE
);
322 bool wxDocument::OnOpenDocument(const wxString
& file
)
324 if (!OnSaveModified())
328 if (wxTheApp
->GetAppName() != wxT(""))
329 msgTitle
= wxTheApp
->GetAppName();
331 msgTitle
= wxString(_("File error"));
333 #if wxUSE_STD_IOSTREAM
334 wxSTD ifstream
store(wxString(file
.fn_str()).mb_str());
335 if (store
.fail() || store
.bad())
337 wxFileInputStream
store(wxString(file
.fn_str()));
338 if (store
.LastError() != wxSTREAM_NOERROR
)
341 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
|wxICON_EXCLAMATION
,
342 GetDocumentWindow());
345 #if wxUSE_STD_IOSTREAM
347 if ( !store
&& !store
.eof() )
349 int res
= LoadObject(store
).LastError();
350 if ((res
!= wxSTREAM_NOERROR
) &&
351 (res
!= wxSTREAM_EOF
))
354 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
|wxICON_EXCLAMATION
,
355 GetDocumentWindow());
358 SetFilename(file
, TRUE
);
367 #if wxUSE_STD_IOSTREAM
368 wxSTD istream
& wxDocument::LoadObject(wxSTD istream
& stream
)
370 wxInputStream
& wxDocument::LoadObject(wxInputStream
& stream
)
376 #if wxUSE_STD_IOSTREAM
377 wxSTD ostream
& wxDocument::SaveObject(wxSTD ostream
& stream
)
379 wxOutputStream
& wxDocument::SaveObject(wxOutputStream
& stream
)
385 bool wxDocument::Revert()
391 // Get title, or filename if no title, else unnamed
392 bool wxDocument::GetPrintableName(wxString
& buf
) const
394 if (m_documentTitle
!= wxT(""))
396 buf
= m_documentTitle
;
399 else if (m_documentFile
!= wxT(""))
401 buf
= wxFileNameFromPath(m_documentFile
);
411 wxWindow
*wxDocument::GetDocumentWindow() const
413 wxView
*view
= GetFirstView();
415 return view
->GetFrame();
417 return wxTheApp
->GetTopWindow();
420 wxCommandProcessor
*wxDocument::OnCreateCommandProcessor()
422 return new wxCommandProcessor
;
425 // TRUE if safe to close
426 bool wxDocument::OnSaveModified()
431 GetPrintableName(title
);
434 if (wxTheApp
->GetAppName() != wxT(""))
435 msgTitle
= wxTheApp
->GetAppName();
437 msgTitle
= wxString(_("Warning"));
440 prompt
.Printf(_("Do you want to save changes to document %s?"),
441 (const wxChar
*)title
);
442 int res
= wxMessageBox(prompt
, msgTitle
,
443 wxYES_NO
|wxCANCEL
|wxICON_QUESTION
,
444 GetDocumentWindow());
450 else if (res
== wxYES
)
452 else if (res
== wxCANCEL
)
458 bool wxDocument::Draw(wxDC
& WXUNUSED(context
))
463 bool wxDocument::AddView(wxView
*view
)
465 if (!m_documentViews
.Member(view
))
467 m_documentViews
.Append(view
);
473 bool wxDocument::RemoveView(wxView
*view
)
475 (void)m_documentViews
.DeleteObject(view
);
480 bool wxDocument::OnCreate(const wxString
& WXUNUSED(path
), long flags
)
482 if (GetDocumentTemplate()->CreateView(this, flags
))
488 // Called after a view is added or removed.
489 // The default implementation deletes the document if
490 // there are no more views.
491 void wxDocument::OnChangedViewList()
493 if (m_documentViews
.Number() == 0)
495 if (OnSaveModified())
502 void wxDocument::UpdateAllViews(wxView
*sender
, wxObject
*hint
)
504 wxNode
*node
= m_documentViews
.First();
507 wxView
*view
= (wxView
*)node
->Data();
508 view
->OnUpdate(sender
, hint
);
513 void wxDocument::NotifyClosing()
515 wxNode
*node
= m_documentViews
.First();
518 wxView
*view
= (wxView
*)node
->Data();
519 view
->OnClosingDocument();
524 void wxDocument::SetFilename(const wxString
& filename
, bool notifyViews
)
526 m_documentFile
= filename
;
529 // Notify the views that the filename has changed
530 wxNode
*node
= m_documentViews
.First();
533 wxView
*view
= (wxView
*)node
->Data();
534 view
->OnChangeFilename();
540 // ----------------------------------------------------------------------------
542 // ----------------------------------------------------------------------------
547 m_viewDocument
= (wxDocument
*) NULL
;
549 m_viewTypeName
= wxT("");
550 m_viewFrame
= (wxFrame
*) NULL
;
555 // GetDocumentManager()->ActivateView(this, FALSE, TRUE);
556 m_viewDocument
->RemoveView(this);
559 // Extend event processing to search the document's event table
560 bool wxView::ProcessEvent(wxEvent
& event
)
562 if ( !GetDocument() || !GetDocument()->ProcessEvent(event
) )
563 return wxEvtHandler::ProcessEvent(event
);
568 void wxView::OnActivateView(bool WXUNUSED(activate
), wxView
*WXUNUSED(activeView
), wxView
*WXUNUSED(deactiveView
))
572 void wxView::OnPrint(wxDC
*dc
, wxObject
*WXUNUSED(info
))
577 void wxView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
))
581 void wxView::OnChangeFilename()
583 if (GetFrame() && GetDocument())
587 GetDocument()->GetPrintableName(title
);
589 GetFrame()->SetTitle(title
);
593 void wxView::SetDocument(wxDocument
*doc
)
595 m_viewDocument
= doc
;
600 bool wxView::Close(bool deleteWindow
)
602 if (OnClose(deleteWindow
))
608 void wxView::Activate(bool activate
)
610 if (GetDocument() && GetDocumentManager())
612 OnActivateView(activate
, this, GetDocumentManager()->GetCurrentView());
613 GetDocumentManager()->ActivateView(this, activate
);
617 bool wxView::OnClose(bool WXUNUSED(deleteWindow
))
619 return GetDocument() ? GetDocument()->Close() : TRUE
;
622 #if wxUSE_PRINTING_ARCHITECTURE
623 wxPrintout
*wxView::OnCreatePrintout()
625 return new wxDocPrintout(this);
627 #endif // wxUSE_PRINTING_ARCHITECTURE
629 // ----------------------------------------------------------------------------
631 // ----------------------------------------------------------------------------
633 wxDocTemplate::wxDocTemplate(wxDocManager
*manager
,
634 const wxString
& descr
,
635 const wxString
& filter
,
638 const wxString
& docTypeName
,
639 const wxString
& viewTypeName
,
640 wxClassInfo
*docClassInfo
,
641 wxClassInfo
*viewClassInfo
,
644 m_documentManager
= manager
;
645 m_description
= descr
;
648 m_fileFilter
= filter
;
650 m_docTypeName
= docTypeName
;
651 m_viewTypeName
= viewTypeName
;
652 m_documentManager
->AssociateTemplate(this);
654 m_docClassInfo
= docClassInfo
;
655 m_viewClassInfo
= viewClassInfo
;
658 wxDocTemplate::~wxDocTemplate()
660 m_documentManager
->DisassociateTemplate(this);
663 // Tries to dynamically construct an object of the right class.
664 wxDocument
*wxDocTemplate::CreateDocument(const wxString
& path
, long flags
)
667 return (wxDocument
*) NULL
;
668 wxDocument
*doc
= (wxDocument
*)m_docClassInfo
->CreateObject();
669 doc
->SetFilename(path
);
670 doc
->SetDocumentTemplate(this);
671 GetDocumentManager()->AddDocument(doc
);
672 doc
->SetCommandProcessor(doc
->OnCreateCommandProcessor());
674 if (doc
->OnCreate(path
, flags
))
678 if (GetDocumentManager()->GetDocuments().Member(doc
))
679 doc
->DeleteAllViews();
680 return (wxDocument
*) NULL
;
684 wxView
*wxDocTemplate::CreateView(wxDocument
*doc
, long flags
)
686 if (!m_viewClassInfo
)
687 return (wxView
*) NULL
;
688 wxView
*view
= (wxView
*)m_viewClassInfo
->CreateObject();
689 view
->SetDocument(doc
);
690 if (view
->OnCreate(doc
, flags
))
697 return (wxView
*) NULL
;
701 // The default (very primitive) format detection: check is the extension is
702 // that of the template
703 bool wxDocTemplate::FileMatchesTemplate(const wxString
& path
)
705 return GetDefaultExtension().IsSameAs(FindExtension(path
));
708 // ----------------------------------------------------------------------------
710 // ----------------------------------------------------------------------------
712 BEGIN_EVENT_TABLE(wxDocManager
, wxEvtHandler
)
713 EVT_MENU(wxID_OPEN
, wxDocManager::OnFileOpen
)
714 EVT_MENU(wxID_CLOSE
, wxDocManager::OnFileClose
)
715 EVT_MENU(wxID_CLOSE_ALL
, wxDocManager::OnFileCloseAll
)
716 EVT_MENU(wxID_REVERT
, wxDocManager::OnFileRevert
)
717 EVT_MENU(wxID_NEW
, wxDocManager::OnFileNew
)
718 EVT_MENU(wxID_SAVE
, wxDocManager::OnFileSave
)
719 EVT_MENU(wxID_SAVEAS
, wxDocManager::OnFileSaveAs
)
720 EVT_MENU(wxID_UNDO
, wxDocManager::OnUndo
)
721 EVT_MENU(wxID_REDO
, wxDocManager::OnRedo
)
723 EVT_UPDATE_UI(wxID_OPEN
, wxDocManager::OnUpdateFileOpen
)
724 EVT_UPDATE_UI(wxID_CLOSE
, wxDocManager::OnUpdateFileClose
)
725 EVT_UPDATE_UI(wxID_CLOSE_ALL
, wxDocManager::OnUpdateFileClose
)
726 EVT_UPDATE_UI(wxID_REVERT
, wxDocManager::OnUpdateFileRevert
)
727 EVT_UPDATE_UI(wxID_NEW
, wxDocManager::OnUpdateFileNew
)
728 EVT_UPDATE_UI(wxID_SAVE
, wxDocManager::OnUpdateFileSave
)
729 EVT_UPDATE_UI(wxID_SAVEAS
, wxDocManager::OnUpdateFileSaveAs
)
730 EVT_UPDATE_UI(wxID_UNDO
, wxDocManager::OnUpdateUndo
)
731 EVT_UPDATE_UI(wxID_REDO
, wxDocManager::OnUpdateRedo
)
733 #if wxUSE_PRINTING_ARCHITECTURE
734 EVT_MENU(wxID_PRINT
, wxDocManager::OnPrint
)
735 EVT_MENU(wxID_PRINT_SETUP
, wxDocManager::OnPrintSetup
)
736 EVT_MENU(wxID_PREVIEW
, wxDocManager::OnPreview
)
738 EVT_UPDATE_UI(wxID_PRINT
, wxDocManager::OnUpdatePrint
)
739 EVT_UPDATE_UI(wxID_PRINT_SETUP
, wxDocManager::OnUpdatePrintSetup
)
740 EVT_UPDATE_UI(wxID_PREVIEW
, wxDocManager::OnUpdatePreview
)
744 wxDocManager
* wxDocManager::sm_docManager
= (wxDocManager
*) NULL
;
746 wxDocManager::wxDocManager(long flags
, bool initialize
)
748 m_defaultDocumentNameCounter
= 1;
750 m_currentView
= (wxView
*) NULL
;
751 m_maxDocsOpen
= 10000;
752 m_fileHistory
= (wxFileHistory
*) NULL
;
755 sm_docManager
= this;
758 wxDocManager::~wxDocManager()
762 delete m_fileHistory
;
763 sm_docManager
= (wxDocManager
*) NULL
;
766 bool wxDocManager::CloseDocuments(bool force
)
768 wxNode
*node
= m_docs
.First();
771 wxDocument
*doc
= (wxDocument
*)node
->Data();
772 wxNode
*next
= node
->Next();
774 if (!doc
->Close() && !force
)
777 // Implicitly deletes the document when the last
778 // view is removed (deleted)
779 doc
->DeleteAllViews();
781 // Check document is deleted
782 if (m_docs
.Member(doc
))
785 // This assumes that documents are not connected in
786 // any way, i.e. deleting one document does NOT
793 bool wxDocManager::Clear(bool force
)
795 if (!CloseDocuments(force
))
798 wxNode
*node
= m_templates
.First();
801 wxDocTemplate
*templ
= (wxDocTemplate
*) node
->Data();
802 wxNode
* next
= node
->Next();
809 bool wxDocManager::Initialize()
811 m_fileHistory
= OnCreateFileHistory();
815 wxFileHistory
*wxDocManager::OnCreateFileHistory()
817 return new wxFileHistory
;
820 void wxDocManager::OnFileClose(wxCommandEvent
& WXUNUSED(event
))
822 wxDocument
*doc
= GetCurrentDocument();
827 doc
->DeleteAllViews();
828 if (m_docs
.Member(doc
))
833 void wxDocManager::OnFileCloseAll(wxCommandEvent
& WXUNUSED(event
))
835 CloseDocuments(FALSE
);
838 void wxDocManager::OnFileNew(wxCommandEvent
& WXUNUSED(event
))
840 CreateDocument(wxString(""), wxDOC_NEW
);
843 void wxDocManager::OnFileOpen(wxCommandEvent
& WXUNUSED(event
))
845 if ( !CreateDocument(wxString(""), 0) )
851 void wxDocManager::OnFileRevert(wxCommandEvent
& WXUNUSED(event
))
853 wxDocument
*doc
= GetCurrentDocument();
859 void wxDocManager::OnFileSave(wxCommandEvent
& WXUNUSED(event
))
861 wxDocument
*doc
= GetCurrentDocument();
867 void wxDocManager::OnFileSaveAs(wxCommandEvent
& WXUNUSED(event
))
869 wxDocument
*doc
= GetCurrentDocument();
875 void wxDocManager::OnPrint(wxCommandEvent
& WXUNUSED(event
))
877 #if wxUSE_PRINTING_ARCHITECTURE
878 wxView
*view
= GetCurrentView();
882 wxPrintout
*printout
= view
->OnCreatePrintout();
886 printer
.Print(view
->GetFrame(), printout
, TRUE
);
890 #endif // wxUSE_PRINTING_ARCHITECTURE
893 void wxDocManager::OnPrintSetup(wxCommandEvent
& WXUNUSED(event
))
895 #if wxUSE_PRINTING_ARCHITECTURE
896 wxWindow
*parentWin
= wxTheApp
->GetTopWindow();
897 wxView
*view
= GetCurrentView();
899 parentWin
= view
->GetFrame();
901 wxPrintDialogData data
;
903 wxPrintDialog
printerDialog(parentWin
, &data
);
904 printerDialog
.GetPrintDialogData().SetSetupDialog(TRUE
);
905 printerDialog
.ShowModal();
906 #endif // wxUSE_PRINTING_ARCHITECTURE
909 void wxDocManager::OnPreview(wxCommandEvent
& WXUNUSED(event
))
911 #if wxUSE_PRINTING_ARCHITECTURE
912 wxView
*view
= GetCurrentView();
916 wxPrintout
*printout
= view
->OnCreatePrintout();
919 // Pass two printout objects: for preview, and possible printing.
920 wxPrintPreviewBase
*preview
= (wxPrintPreviewBase
*) NULL
;
921 preview
= new wxPrintPreview(printout
, view
->OnCreatePrintout());
923 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, (wxFrame
*)wxTheApp
->GetTopWindow(), _("Print Preview"),
924 wxPoint(100, 100), wxSize(600, 650));
925 frame
->Centre(wxBOTH
);
929 #endif // wxUSE_PRINTING_ARCHITECTURE
932 void wxDocManager::OnUndo(wxCommandEvent
& WXUNUSED(event
))
934 wxDocument
*doc
= GetCurrentDocument();
937 if (doc
->GetCommandProcessor())
938 doc
->GetCommandProcessor()->Undo();
941 void wxDocManager::OnRedo(wxCommandEvent
& WXUNUSED(event
))
943 wxDocument
*doc
= GetCurrentDocument();
946 if (doc
->GetCommandProcessor())
947 doc
->GetCommandProcessor()->Redo();
950 // Handlers for UI update commands
952 void wxDocManager::OnUpdateFileOpen(wxUpdateUIEvent
& event
)
954 event
.Enable( TRUE
);
957 void wxDocManager::OnUpdateFileClose(wxUpdateUIEvent
& event
)
959 wxDocument
*doc
= GetCurrentDocument();
960 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
963 void wxDocManager::OnUpdateFileRevert(wxUpdateUIEvent
& event
)
965 wxDocument
*doc
= GetCurrentDocument();
966 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
969 void wxDocManager::OnUpdateFileNew(wxUpdateUIEvent
& event
)
971 event
.Enable( TRUE
);
974 void wxDocManager::OnUpdateFileSave(wxUpdateUIEvent
& event
)
976 wxDocument
*doc
= GetCurrentDocument();
977 event
.Enable( doc
&& doc
->IsModified() );
980 void wxDocManager::OnUpdateFileSaveAs(wxUpdateUIEvent
& event
)
982 wxDocument
*doc
= GetCurrentDocument();
983 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
986 void wxDocManager::OnUpdateUndo(wxUpdateUIEvent
& event
)
988 wxDocument
*doc
= GetCurrentDocument();
989 event
.Enable( (doc
&& doc
->GetCommandProcessor() && doc
->GetCommandProcessor()->CanUndo()) );
990 if (doc
&& doc
->GetCommandProcessor())
991 doc
->GetCommandProcessor()->SetMenuStrings();
994 void wxDocManager::OnUpdateRedo(wxUpdateUIEvent
& event
)
996 wxDocument
*doc
= GetCurrentDocument();
997 event
.Enable( (doc
&& doc
->GetCommandProcessor() && doc
->GetCommandProcessor()->CanRedo()) );
998 if (doc
&& doc
->GetCommandProcessor())
999 doc
->GetCommandProcessor()->SetMenuStrings();
1002 void wxDocManager::OnUpdatePrint(wxUpdateUIEvent
& event
)
1004 wxDocument
*doc
= GetCurrentDocument();
1005 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1008 void wxDocManager::OnUpdatePrintSetup(wxUpdateUIEvent
& event
)
1010 event
.Enable( TRUE
);
1013 void wxDocManager::OnUpdatePreview(wxUpdateUIEvent
& event
)
1015 wxDocument
*doc
= GetCurrentDocument();
1016 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1019 wxView
*wxDocManager::GetCurrentView() const
1022 return m_currentView
;
1023 if (m_docs
.Number() == 1)
1025 wxDocument
* doc
= (wxDocument
*) m_docs
.First()->Data();
1026 return doc
->GetFirstView();
1028 return (wxView
*) NULL
;
1031 // Extend event processing to search the view's event table
1032 bool wxDocManager::ProcessEvent(wxEvent
& event
)
1034 wxView
* view
= GetCurrentView();
1037 if (view
->ProcessEvent(event
))
1040 return wxEvtHandler::ProcessEvent(event
);
1043 wxDocument
*wxDocManager::CreateDocument(const wxString
& path
, long flags
)
1045 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.Number()];
1048 for (i
= 0; i
< m_templates
.Number(); i
++)
1050 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Nth(i
)->Data());
1051 if (temp
->IsVisible())
1053 templates
[n
] = temp
;
1060 return (wxDocument
*) NULL
;
1063 // If we've reached the max number of docs, close the
1065 if (GetDocuments().Number() >= m_maxDocsOpen
)
1067 wxDocument
*doc
= (wxDocument
*)GetDocuments().First()->Data();
1070 // Implicitly deletes the document when
1071 // the last view is deleted
1072 doc
->DeleteAllViews();
1074 // Check we're really deleted
1075 if (m_docs
.Member(doc
))
1081 return (wxDocument
*) NULL
;
1085 // New document: user chooses a template, unless there's only one.
1086 if (flags
& wxDOC_NEW
)
1090 wxDocTemplate
*temp
= templates
[0];
1092 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
1095 newDoc
->SetDocumentName(temp
->GetDocumentName());
1096 newDoc
->SetDocumentTemplate(temp
);
1097 newDoc
->OnNewDocument();
1102 wxDocTemplate
*temp
= SelectDocumentType(templates
, n
);
1106 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
1109 newDoc
->SetDocumentName(temp
->GetDocumentName());
1110 newDoc
->SetDocumentTemplate(temp
);
1111 newDoc
->OnNewDocument();
1116 return (wxDocument
*) NULL
;
1119 // Existing document
1120 wxDocTemplate
*temp
= (wxDocTemplate
*) NULL
;
1122 wxString
path2(wxT(""));
1123 if (path
!= wxT(""))
1126 if (flags
& wxDOC_SILENT
)
1127 temp
= FindTemplateForPath(path2
);
1129 temp
= SelectDocumentPath(templates
, n
, path2
, flags
);
1135 wxDocument
*newDoc
= temp
->CreateDocument(path2
, flags
);
1138 newDoc
->SetDocumentName(temp
->GetDocumentName());
1139 newDoc
->SetDocumentTemplate(temp
);
1140 if (!newDoc
->OnOpenDocument(path2
))
1142 newDoc
->DeleteAllViews();
1143 // delete newDoc; // Implicitly deleted by DeleteAllViews
1144 return (wxDocument
*) NULL
;
1146 AddFileToHistory(path2
);
1151 return (wxDocument
*) NULL
;
1154 wxView
*wxDocManager::CreateView(wxDocument
*doc
, long flags
)
1156 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.Number()];
1159 for (i
= 0; i
< m_templates
.Number(); i
++)
1161 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Nth(i
)->Data());
1162 if (temp
->IsVisible())
1164 if (temp
->GetDocumentName() == doc
->GetDocumentName())
1166 templates
[n
] = temp
;
1174 return (wxView
*) NULL
;
1178 wxDocTemplate
*temp
= templates
[0];
1180 wxView
*view
= temp
->CreateView(doc
, flags
);
1182 view
->SetViewName(temp
->GetViewName());
1186 wxDocTemplate
*temp
= SelectViewType(templates
, n
);
1190 wxView
*view
= temp
->CreateView(doc
, flags
);
1192 view
->SetViewName(temp
->GetViewName());
1196 return (wxView
*) NULL
;
1199 // Not yet implemented
1200 void wxDocManager::DeleteTemplate(wxDocTemplate
*WXUNUSED(temp
), long WXUNUSED(flags
))
1204 // Not yet implemented
1205 bool wxDocManager::FlushDoc(wxDocument
*WXUNUSED(doc
))
1210 wxDocument
*wxDocManager::GetCurrentDocument() const
1212 wxView
*view
= GetCurrentView();
1214 return view
->GetDocument();
1216 return (wxDocument
*) NULL
;
1219 // Make a default document name
1220 bool wxDocManager::MakeDefaultName(wxString
& name
)
1222 name
.Printf(_("unnamed%d"), m_defaultDocumentNameCounter
);
1223 m_defaultDocumentNameCounter
++;
1228 // Make a frame title (override this to do something different)
1229 // If docName is empty, a document is not currently active.
1230 wxString
wxDocManager::MakeFrameTitle(wxDocument
* doc
)
1232 wxString appName
= wxTheApp
->GetAppName();
1239 doc
->GetPrintableName(docName
);
1240 title
= docName
+ wxString(_(" - ")) + appName
;
1246 // Not yet implemented
1247 wxDocTemplate
*wxDocManager::MatchTemplate(const wxString
& WXUNUSED(path
))
1249 return (wxDocTemplate
*) NULL
;
1252 // File history management
1253 void wxDocManager::AddFileToHistory(const wxString
& file
)
1256 m_fileHistory
->AddFileToHistory(file
);
1259 void wxDocManager::RemoveFileFromHistory(int i
)
1262 m_fileHistory
->RemoveFileFromHistory(i
);
1265 wxString
wxDocManager::GetHistoryFile(int i
) const
1270 histFile
= m_fileHistory
->GetHistoryFile(i
);
1275 void wxDocManager::FileHistoryUseMenu(wxMenu
*menu
)
1278 m_fileHistory
->UseMenu(menu
);
1281 void wxDocManager::FileHistoryRemoveMenu(wxMenu
*menu
)
1284 m_fileHistory
->RemoveMenu(menu
);
1288 void wxDocManager::FileHistoryLoad(wxConfigBase
& config
)
1291 m_fileHistory
->Load(config
);
1294 void wxDocManager::FileHistorySave(wxConfigBase
& config
)
1297 m_fileHistory
->Save(config
);
1301 void wxDocManager::FileHistoryAddFilesToMenu(wxMenu
* menu
)
1304 m_fileHistory
->AddFilesToMenu(menu
);
1307 void wxDocManager::FileHistoryAddFilesToMenu()
1310 m_fileHistory
->AddFilesToMenu();
1313 int wxDocManager::GetNoHistoryFiles() const
1316 return m_fileHistory
->GetNoHistoryFiles();
1322 // Find out the document template via matching in the document file format
1323 // against that of the template
1324 wxDocTemplate
*wxDocManager::FindTemplateForPath(const wxString
& path
)
1326 wxDocTemplate
*theTemplate
= (wxDocTemplate
*) NULL
;
1328 // Find the template which this extension corresponds to
1330 for (i
= 0; i
< m_templates
.Number(); i
++)
1332 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Nth(i
)->Data();
1333 if ( temp
->FileMatchesTemplate(path
) )
1342 // Try to get a more suitable parent frame than the top window,
1343 // for selection dialogs. Otherwise you may get an unexpected
1344 // window being activated when a dialog is shown.
1345 static wxWindow
* wxFindSuitableParent()
1347 wxWindow
* parent
= wxTheApp
->GetTopWindow();
1349 wxWindow
* focusWindow
= wxWindow::FindFocus();
1352 while (focusWindow
&&
1353 !focusWindow
->IsKindOf(CLASSINFO(wxDialog
)) &&
1354 !focusWindow
->IsKindOf(CLASSINFO(wxFrame
)))
1356 focusWindow
= focusWindow
->GetParent();
1359 parent
= focusWindow
;
1364 // Prompts user to open a file, using file specs in templates.
1365 // How to implement in wxWindows? Must extend the file selector
1366 // dialog or implement own; OR match the extension to the
1367 // template extension.
1369 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**templates
,
1370 #if defined(__WXMSW__) || defined(__WXGTK__)
1373 int WXUNUSED(noTemplates
),
1376 long WXUNUSED(flags
),
1377 bool WXUNUSED(save
))
1379 // We can only have multiple filters in Windows and GTK
1380 #if defined(__WXMSW__) || defined(__WXGTK__)
1384 for (i
= 0; i
< noTemplates
; i
++)
1386 if (templates
[i
]->IsVisible())
1388 // add a '|' to separate this filter from the previous one
1389 if ( !descrBuf
.IsEmpty() )
1390 descrBuf
<< wxT('|');
1392 descrBuf
<< templates
[i
]->GetDescription()
1393 << wxT(" (") << templates
[i
]->GetFileFilter() << wxT(") |")
1394 << templates
[i
]->GetFileFilter();
1398 wxString descrBuf
= wxT("*.*");
1401 int FilterIndex
= -1;
1403 wxWindow
* parent
= wxFindSuitableParent();
1405 wxString pathTmp
= wxFileSelectorEx(_("Select a file"),
1413 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)NULL
;
1414 if (!pathTmp
.IsEmpty())
1416 if (!wxFileExists(pathTmp
))
1419 if (!wxTheApp
->GetAppName().IsEmpty())
1420 msgTitle
= wxTheApp
->GetAppName();
1422 msgTitle
= wxString(_("File error"));
1424 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
1428 return (wxDocTemplate
*) NULL
;
1430 m_lastDirectory
= wxPathOnly(pathTmp
);
1434 // first choose the template using the extension, if this fails (i.e.
1435 // wxFileSelectorEx() didn't fill it), then use the path
1436 if ( FilterIndex
!= -1 )
1437 theTemplate
= templates
[FilterIndex
];
1439 theTemplate
= FindTemplateForPath(path
);
1449 // In all other windowing systems, until we have more advanced
1450 // file selectors, we must select the document type (template) first, and
1451 // _then_ pop up the file selector.
1452 wxDocTemplate
*temp
= SelectDocumentType(templates
, noTemplates
);
1454 return (wxDocTemplate
*) NULL
;
1456 wxChar
*pathTmp
= wxFileSelector(_("Select a file"), wxT(""), wxT(""),
1457 temp
->GetDefaultExtension(),
1458 temp
->GetFileFilter(),
1459 0, wxTheApp
->GetTopWindow());
1467 return (wxDocTemplate
*) NULL
;
1471 wxDocTemplate
*wxDocManager::SelectDocumentType(wxDocTemplate
**templates
,
1472 int noTemplates
, bool sort
)
1474 wxArrayString
strings(sort
);
1475 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1479 for (i
= 0; i
< noTemplates
; i
++)
1481 if (templates
[i
]->IsVisible())
1485 for (j
= 0; j
< n
; j
++)
1487 //filter out NOT unique documents + view combinations
1488 if ( templates
[i
]->m_docTypeName
== data
[j
]->m_docTypeName
&&
1489 templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
1496 strings
.Add(templates
[i
]->m_description
);
1498 data
[n
] = templates
[i
];
1506 // Yes, this will be slow, but template lists
1507 // are typically short.
1509 n
= strings
.Count();
1510 for (i
= 0; i
< n
; i
++)
1512 for (j
= 0; j
< noTemplates
; j
++)
1514 if (strings
[i
] == templates
[j
]->m_description
)
1515 data
[i
] = templates
[j
];
1520 wxDocTemplate
*theTemplate
;
1525 // no visible templates, hence nothing to choose from
1530 // don't propose the user to choose if he heas no choice
1531 theTemplate
= data
[0];
1535 // propose the user to choose one of several
1536 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1538 _("Select a document template"),
1542 wxFindSuitableParent()
1551 wxDocTemplate
*wxDocManager::SelectViewType(wxDocTemplate
**templates
,
1552 int noTemplates
, bool sort
)
1554 wxArrayString
strings(sort
);
1555 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1559 for (i
= 0; i
< noTemplates
; i
++)
1561 wxDocTemplate
*templ
= templates
[i
];
1562 if ( templ
->IsVisible() && !templ
->GetViewName().empty() )
1566 for (j
= 0; j
< n
; j
++)
1568 //filter out NOT unique views
1569 if ( templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
)
1575 strings
.Add(templ
->m_viewTypeName
);
1584 // Yes, this will be slow, but template lists
1585 // are typically short.
1587 n
= strings
.Count();
1588 for (i
= 0; i
< n
; i
++)
1590 for (j
= 0; j
< noTemplates
; j
++)
1592 if (strings
[i
] == templates
[j
]->m_viewTypeName
)
1593 data
[i
] = templates
[j
];
1598 wxDocTemplate
*theTemplate
;
1600 // the same logic as above
1604 theTemplate
= (wxDocTemplate
*)NULL
;
1608 theTemplate
= data
[0];
1612 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1614 _("Select a document view"),
1618 wxFindSuitableParent()
1627 void wxDocManager::AssociateTemplate(wxDocTemplate
*temp
)
1629 if (!m_templates
.Member(temp
))
1630 m_templates
.Append(temp
);
1633 void wxDocManager::DisassociateTemplate(wxDocTemplate
*temp
)
1635 m_templates
.DeleteObject(temp
);
1638 // Add and remove a document from the manager's list
1639 void wxDocManager::AddDocument(wxDocument
*doc
)
1641 if (!m_docs
.Member(doc
))
1645 void wxDocManager::RemoveDocument(wxDocument
*doc
)
1647 m_docs
.DeleteObject(doc
);
1650 // Views or windows should inform the document manager
1651 // when a view is going in or out of focus
1652 void wxDocManager::ActivateView(wxView
*view
, bool activate
, bool WXUNUSED(deleting
))
1654 // If we're deactiving, and if we're not actually deleting the view, then
1655 // don't reset the current view because we may be going to
1656 // a window without a view.
1657 // WHAT DID I MEAN BY THAT EXACTLY?
1661 if (m_currentView == view)
1662 m_currentView = NULL;
1668 m_currentView
= view
;
1670 m_currentView
= (wxView
*) NULL
;
1674 // ----------------------------------------------------------------------------
1675 // Default document child frame
1676 // ----------------------------------------------------------------------------
1678 BEGIN_EVENT_TABLE(wxDocChildFrame
, wxFrame
)
1679 EVT_ACTIVATE(wxDocChildFrame::OnActivate
)
1680 EVT_CLOSE(wxDocChildFrame::OnCloseWindow
)
1683 wxDocChildFrame::wxDocChildFrame(wxDocument
*doc
,
1687 const wxString
& title
,
1691 const wxString
& name
)
1692 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1694 m_childDocument
= doc
;
1697 view
->SetFrame(this);
1700 wxDocChildFrame::~wxDocChildFrame()
1704 // Extend event processing to search the view's event table
1705 bool wxDocChildFrame::ProcessEvent(wxEvent
& event
)
1708 m_childView
->Activate(TRUE
);
1710 if ( !m_childView
|| ! m_childView
->ProcessEvent(event
) )
1712 // Only hand up to the parent if it's a menu command
1713 if (!event
.IsKindOf(CLASSINFO(wxCommandEvent
)) || !GetParent() || !GetParent()->ProcessEvent(event
))
1714 return wxEvtHandler::ProcessEvent(event
);
1722 void wxDocChildFrame::OnActivate(wxActivateEvent
& event
)
1724 wxFrame::OnActivate(event
);
1727 m_childView
->Activate(event
.GetActive());
1730 void wxDocChildFrame::OnCloseWindow(wxCloseEvent
& event
)
1735 if (!event
.CanVeto())
1736 ans
= TRUE
; // Must delete.
1738 ans
= m_childView
->Close(FALSE
); // FALSE means don't delete associated window
1742 m_childView
->Activate(FALSE
);
1744 m_childView
= (wxView
*) NULL
;
1745 m_childDocument
= (wxDocument
*) NULL
;
1756 // ----------------------------------------------------------------------------
1757 // Default parent frame
1758 // ----------------------------------------------------------------------------
1760 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1761 EVT_MENU(wxID_EXIT
, wxDocParentFrame::OnExit
)
1762 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE9
, wxDocParentFrame::OnMRUFile
)
1763 EVT_CLOSE(wxDocParentFrame::OnCloseWindow
)
1766 wxDocParentFrame::wxDocParentFrame(wxDocManager
*manager
,
1769 const wxString
& title
,
1773 const wxString
& name
)
1774 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1776 m_docManager
= manager
;
1779 void wxDocParentFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
1784 void wxDocParentFrame::OnMRUFile(wxCommandEvent
& event
)
1786 int n
= event
.GetId() - wxID_FILE1
; // the index in MRU list
1787 wxString
filename(m_docManager
->GetHistoryFile(n
));
1788 if ( !filename
.IsEmpty() )
1790 // verify that the file exists before doing anything else
1791 if ( wxFile::Exists(filename
) )
1794 (void)m_docManager
->CreateDocument(filename
, wxDOC_SILENT
);
1798 // remove the bogus filename from the MRU list and notify the user
1800 m_docManager
->RemoveFileFromHistory(n
);
1802 wxLogError(_("The file '%s' doesn't exist and couldn't be opened.\nIt has been removed from the most recently used files list."),
1808 // Extend event processing to search the view's event table
1809 bool wxDocParentFrame::ProcessEvent(wxEvent
& event
)
1811 // Try the document manager, then do default processing
1812 if (!m_docManager
|| !m_docManager
->ProcessEvent(event
))
1813 return wxEvtHandler::ProcessEvent(event
);
1818 // Define the behaviour for the frame closing
1819 // - must delete all frames except for the main one.
1820 void wxDocParentFrame::OnCloseWindow(wxCloseEvent
& event
)
1822 if (m_docManager
->Clear(!event
.CanVeto()))
1830 #if wxUSE_PRINTING_ARCHITECTURE
1832 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
)
1835 m_printoutView
= view
;
1838 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
1842 // Get the logical pixels per inch of screen and printer
1843 int ppiScreenX
, ppiScreenY
;
1844 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
1845 int ppiPrinterX
, ppiPrinterY
;
1846 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
1848 // This scales the DC so that the printout roughly represents the
1849 // the screen scaling. The text point size _should_ be the right size
1850 // but in fact is too small for some reason. This is a detail that will
1851 // need to be addressed at some point but can be fudged for the
1853 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
1855 // Now we have to check in case our real page size is reduced
1856 // (e.g. because we're drawing to a print preview memory DC)
1857 int pageWidth
, pageHeight
;
1859 dc
->GetSize(&w
, &h
);
1860 GetPageSizePixels(&pageWidth
, &pageHeight
);
1862 // If printer pageWidth == current DC width, then this doesn't
1863 // change. But w might be the preview bitmap width, so scale down.
1864 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
1865 dc
->SetUserScale(overallScale
, overallScale
);
1869 m_printoutView
->OnDraw(dc
);
1874 bool wxDocPrintout::HasPage(int pageNum
)
1876 return (pageNum
== 1);
1879 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
1881 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
1887 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
1895 #endif // wxUSE_PRINTING_ARCHITECTURE
1897 // ----------------------------------------------------------------------------
1898 // File history processor
1899 // ----------------------------------------------------------------------------
1901 wxFileHistory::wxFileHistory(int maxFiles
)
1903 m_fileMaxFiles
= maxFiles
;
1905 m_fileHistory
= new wxChar
*[m_fileMaxFiles
];
1908 wxFileHistory::~wxFileHistory()
1911 for (i
= 0; i
< m_fileHistoryN
; i
++)
1912 delete[] m_fileHistory
[i
];
1913 delete[] m_fileHistory
;
1916 // File history management
1917 void wxFileHistory::AddFileToHistory(const wxString
& file
)
1921 // Check we don't already have this file
1922 for (i
= 0; i
< m_fileHistoryN
; i
++)
1924 if ( m_fileHistory
[i
] && (file
== m_fileHistory
[i
]) )
1926 // we do have it, move it to the top of the history
1927 RemoveFileFromHistory (i
);
1928 AddFileToHistory (file
);
1933 // if we already have a full history, delete the one at the end
1934 if ( m_fileMaxFiles
== m_fileHistoryN
)
1936 RemoveFileFromHistory (m_fileHistoryN
- 1);
1937 AddFileToHistory (file
);
1941 // Add to the project file history:
1942 // Move existing files (if any) down so we can insert file at beginning.
1943 if (m_fileHistoryN
< m_fileMaxFiles
)
1945 wxNode
* node
= m_fileMenus
.First();
1948 wxMenu
* menu
= (wxMenu
*) node
->Data();
1949 if (m_fileHistoryN
== 0)
1950 menu
->AppendSeparator();
1951 menu
->Append(wxID_FILE1
+m_fileHistoryN
, _("[EMPTY]"));
1952 node
= node
->Next();
1956 // Shuffle filenames down
1957 for (i
= (m_fileHistoryN
-1); i
> 0; i
--)
1959 m_fileHistory
[i
] = m_fileHistory
[i
-1];
1961 m_fileHistory
[0] = copystring(file
);
1963 // this is the directory of the last opened file
1964 wxString pathCurrent
;
1965 wxSplitPath( m_fileHistory
[0], &pathCurrent
, NULL
, NULL
);
1966 for (i
= 0; i
< m_fileHistoryN
; i
++)
1968 if ( m_fileHistory
[i
] )
1970 // if in same directory just show the filename; otherwise the full
1972 wxString pathInMenu
, path
, filename
, ext
;
1973 wxSplitPath( m_fileHistory
[i
], &path
, &filename
, &ext
);
1974 if ( path
== pathCurrent
)
1976 pathInMenu
= filename
;
1978 pathInMenu
= pathInMenu
+ wxFILE_SEP_EXT
+ ext
;
1982 // absolute path; could also set relative path
1983 pathInMenu
= m_fileHistory
[i
];
1987 buf
.Printf(s_MRUEntryFormat
, i
+ 1, pathInMenu
.c_str());
1988 wxNode
* node
= m_fileMenus
.First();
1991 wxMenu
* menu
= (wxMenu
*) node
->Data();
1992 menu
->SetLabel(wxID_FILE1
+ i
, buf
);
1993 node
= node
->Next();
1999 void wxFileHistory::RemoveFileFromHistory(int i
)
2001 wxCHECK_RET( i
< m_fileHistoryN
,
2002 wxT("invalid index in wxFileHistory::RemoveFileFromHistory") );
2004 // delete the element from the array (could use memmove() too...)
2005 delete [] m_fileHistory
[i
];
2008 for ( j
= i
; j
< m_fileHistoryN
- 1; j
++ )
2010 m_fileHistory
[j
] = m_fileHistory
[j
+ 1];
2013 wxNode
* node
= m_fileMenus
.First();
2016 wxMenu
* menu
= (wxMenu
*) node
->Data();
2019 // shuffle filenames up
2021 for ( j
= i
; j
< m_fileHistoryN
- 1; j
++ )
2023 buf
.Printf(s_MRUEntryFormat
, j
+ 1, m_fileHistory
[j
]);
2024 menu
->SetLabel(wxID_FILE1
+ j
, buf
);
2027 node
= node
->Next();
2029 // delete the last menu item which is unused now
2030 if (menu
->FindItem(wxID_FILE1
+ m_fileHistoryN
- 1))
2031 menu
->Delete(wxID_FILE1
+ m_fileHistoryN
- 1);
2033 // delete the last separator too if no more files are left
2034 if ( m_fileHistoryN
== 1 )
2036 wxMenuItemList::Node
*node
= menu
->GetMenuItems().GetLast();
2039 wxMenuItem
*menuItem
= node
->GetData();
2040 if ( menuItem
->IsSeparator() )
2042 menu
->Delete(menuItem
);
2044 //else: should we search backwards for the last separator?
2046 //else: menu is empty somehow
2053 wxString
wxFileHistory::GetHistoryFile(int i
) const
2056 if ( i
< m_fileHistoryN
)
2058 s
= m_fileHistory
[i
];
2062 wxFAIL_MSG( wxT("bad index in wxFileHistory::GetHistoryFile") );
2068 void wxFileHistory::UseMenu(wxMenu
*menu
)
2070 if (!m_fileMenus
.Member(menu
))
2071 m_fileMenus
.Append(menu
);
2074 void wxFileHistory::RemoveMenu(wxMenu
*menu
)
2076 m_fileMenus
.DeleteObject(menu
);
2080 void wxFileHistory::Load(wxConfigBase
& config
)
2084 buf
.Printf(wxT("file%d"), m_fileHistoryN
+1);
2085 wxString historyFile
;
2086 while ((m_fileHistoryN
<= m_fileMaxFiles
) && config
.Read(buf
, &historyFile
) && (historyFile
!= wxT("")))
2088 m_fileHistory
[m_fileHistoryN
] = copystring((const wxChar
*) historyFile
);
2090 buf
.Printf(wxT("file%d"), m_fileHistoryN
+1);
2091 historyFile
= wxT("");
2096 void wxFileHistory::Save(wxConfigBase
& config
)
2099 for (i
= 0; i
< m_fileHistoryN
; i
++)
2102 buf
.Printf(wxT("file%d"), i
+1);
2103 config
.Write(buf
, wxString(m_fileHistory
[i
]));
2106 #endif // wxUSE_CONFIG
2108 void wxFileHistory::AddFilesToMenu()
2110 if (m_fileHistoryN
> 0)
2112 wxNode
* node
= m_fileMenus
.First();
2115 wxMenu
* menu
= (wxMenu
*) node
->Data();
2116 menu
->AppendSeparator();
2118 for (i
= 0; i
< m_fileHistoryN
; i
++)
2120 if (m_fileHistory
[i
])
2123 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
2124 menu
->Append(wxID_FILE1
+i
, buf
);
2127 node
= node
->Next();
2132 void wxFileHistory::AddFilesToMenu(wxMenu
* menu
)
2134 if (m_fileHistoryN
> 0)
2136 menu
->AppendSeparator();
2138 for (i
= 0; i
< m_fileHistoryN
; i
++)
2140 if (m_fileHistory
[i
])
2143 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
2144 menu
->Append(wxID_FILE1
+i
, buf
);
2150 // ----------------------------------------------------------------------------
2151 // Permits compatibility with existing file formats and functions that
2152 // manipulate files directly
2153 // ----------------------------------------------------------------------------
2155 #if wxUSE_STD_IOSTREAM
2156 bool wxTransferFileToStream(const wxString
& filename
, wxSTD ostream
& stream
)
2161 if ((fd1
= wxFopen (filename
.fn_str(), _T("rb"))) == NULL
)
2164 while ((ch
= getc (fd1
)) != EOF
)
2165 stream
<< (unsigned char)ch
;
2171 bool wxTransferStreamToFile(wxSTD istream
& stream
, const wxString
& filename
)
2176 if ((fd1
= wxFopen (filename
.fn_str(), _T("wb"))) == NULL
)
2181 while (!stream
.eof())
2191 bool wxTransferFileToStream(const wxString
& filename
, wxOutputStream
& stream
)
2196 if ((fd1
= wxFopen (filename
, wxT("rb"))) == NULL
)
2199 while ((ch
= getc (fd1
)) != EOF
)
2200 stream
.PutC((char) ch
);
2206 bool wxTransferStreamToFile(wxInputStream
& stream
, const wxString
& filename
)
2211 if ((fd1
= wxFopen (filename
, wxT("wb"))) == NULL
)
2216 int len
= stream
.StreamSize();
2217 // TODO: is this the correct test for EOF?
2218 while (stream
.TellI() < (len
- 1))
2228 #endif // wxUSE_DOC_VIEW_ARCHITECTURE