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"
51 #if wxUSE_PRINTING_ARCHITECTURE
52 #include "wx/prntbase.h"
53 #include "wx/printdlg.h"
56 #include "wx/msgdlg.h"
57 #include "wx/choicdlg.h"
58 #include "wx/docview.h"
59 #include "wx/confbase.h"
61 #include "wx/cmdproc.h"
66 #if wxUSE_STD_IOSTREAM
67 #include "wx/ioswrap.h"
74 #include "wx/wfstream.h"
77 // ----------------------------------------------------------------------------
79 // ----------------------------------------------------------------------------
81 IMPLEMENT_ABSTRACT_CLASS(wxDocument
, wxEvtHandler
)
82 IMPLEMENT_ABSTRACT_CLASS(wxView
, wxEvtHandler
)
83 IMPLEMENT_ABSTRACT_CLASS(wxDocTemplate
, wxObject
)
84 IMPLEMENT_DYNAMIC_CLASS(wxDocManager
, wxEvtHandler
)
85 IMPLEMENT_CLASS(wxDocChildFrame
, wxFrame
)
86 IMPLEMENT_CLASS(wxDocParentFrame
, wxFrame
)
88 #if wxUSE_PRINTING_ARCHITECTURE
89 IMPLEMENT_DYNAMIC_CLASS(wxDocPrintout
, wxPrintout
)
92 IMPLEMENT_DYNAMIC_CLASS(wxFileHistory
, wxObject
)
94 // ----------------------------------------------------------------------------
95 // function prototypes
96 // ----------------------------------------------------------------------------
98 static inline wxString
FindExtension(const wxChar
*path
);
100 // ----------------------------------------------------------------------------
102 // ----------------------------------------------------------------------------
104 static const wxChar
*s_MRUEntryFormat
= wxT("&%d %s");
106 // ============================================================================
108 // ============================================================================
110 // ----------------------------------------------------------------------------
112 // ----------------------------------------------------------------------------
114 static wxString
FindExtension(const wxChar
*path
)
117 wxSplitPath(path
, NULL
, NULL
, &ext
);
119 // VZ: extensions are considered not case sensitive - is this really a good
121 return ext
.MakeLower();
124 // ----------------------------------------------------------------------------
125 // Definition of wxDocument
126 // ----------------------------------------------------------------------------
128 wxDocument::wxDocument(wxDocument
*parent
)
130 m_documentModified
= FALSE
;
131 m_documentParent
= parent
;
132 m_documentTemplate
= (wxDocTemplate
*) NULL
;
133 m_commandProcessor
= (wxCommandProcessor
*) NULL
;
137 bool wxDocument::DeleteContents()
142 wxDocument::~wxDocument()
146 if (m_commandProcessor
)
147 delete m_commandProcessor
;
149 if (GetDocumentManager())
150 GetDocumentManager()->RemoveDocument(this);
152 // Not safe to do here, since it'll invoke virtual view functions
153 // expecting to see valid derived objects: and by the time we get here,
154 // we've called destructors higher up.
158 bool wxDocument::Close()
160 if (OnSaveModified())
161 return OnCloseDocument();
166 bool wxDocument::OnCloseDocument()
168 // Tell all views that we're about to close
175 // Note that this implicitly deletes the document when the last view is
177 bool wxDocument::DeleteAllViews()
179 wxDocManager
* manager
= GetDocumentManager();
181 wxNode
*node
= m_documentViews
.First();
184 wxView
*view
= (wxView
*)node
->Data();
188 wxNode
*next
= node
->Next();
190 delete view
; // Deletes node implicitly
193 // If we haven't yet deleted the document (for example
194 // if there were no views) then delete it.
195 if (manager
&& manager
->GetDocuments().Member(this))
201 wxView
*wxDocument::GetFirstView() const
203 if (m_documentViews
.Number() == 0)
204 return (wxView
*) NULL
;
205 return (wxView
*)m_documentViews
.First()->Data();
208 wxDocManager
*wxDocument::GetDocumentManager() const
210 return (m_documentTemplate
? m_documentTemplate
->GetDocumentManager() : (wxDocManager
*) NULL
);
213 bool wxDocument::OnNewDocument()
215 if (!OnSaveModified())
218 if (OnCloseDocument()==FALSE
) return FALSE
;
221 SetDocumentSaved(FALSE
);
224 GetDocumentManager()->MakeDefaultName(name
);
226 SetFilename(name
, TRUE
);
231 bool wxDocument::Save()
233 if (!IsModified() && m_savedYet
)
236 if ( m_documentFile
.empty() || !m_savedYet
)
239 return OnSaveDocument(m_documentFile
);
242 bool wxDocument::SaveAs()
244 wxDocTemplate
*docTemplate
= GetDocumentTemplate();
248 wxString tmp
= wxFileSelector(_("Save as"),
249 docTemplate
->GetDirectory(),
250 wxFileNameFromPath(GetFilename()),
251 docTemplate
->GetDefaultExtension(),
252 docTemplate
->GetFileFilter(),
253 wxSAVE
| wxOVERWRITE_PROMPT
,
254 GetDocumentWindow());
259 wxString
fileName(tmp
);
260 wxString path
, name
, ext
;
261 wxSplitPath(fileName
, & path
, & name
, & ext
);
263 if (ext
.IsEmpty() || ext
== wxT(""))
266 fileName
+= docTemplate
->GetDefaultExtension();
269 SetFilename(fileName
);
270 SetTitle(wxFileNameFromPath(fileName
));
272 GetDocumentManager()->AddFileToHistory(fileName
);
274 // Notify the views that the filename has changed
275 wxNode
*node
= m_documentViews
.First();
278 wxView
*view
= (wxView
*)node
->Data();
279 view
->OnChangeFilename();
283 return OnSaveDocument(m_documentFile
);
286 bool wxDocument::OnSaveDocument(const wxString
& file
)
292 if (wxTheApp
->GetAppName() != wxT(""))
293 msgTitle
= wxTheApp
->GetAppName();
295 msgTitle
= wxString(_("File error"));
297 #if wxUSE_STD_IOSTREAM
298 wxSTD ofstream
store(wxString(file
.fn_str()).mb_str());
299 if (store
.fail() || store
.bad())
301 wxFileOutputStream
store(wxString(file
.fn_str()));
302 if (store
.LastError() != wxSTREAM_NOERROR
)
305 (void)wxMessageBox(_("Sorry, could not open this file for saving."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
306 GetDocumentWindow());
310 if (!SaveObject(store
))
312 (void)wxMessageBox(_("Sorry, could not save this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
313 GetDocumentWindow());
319 SetDocumentSaved(TRUE
);
323 bool wxDocument::OnOpenDocument(const wxString
& file
)
325 if (!OnSaveModified())
329 if (wxTheApp
->GetAppName() != wxT(""))
330 msgTitle
= wxTheApp
->GetAppName();
332 msgTitle
= wxString(_("File error"));
334 #if wxUSE_STD_IOSTREAM
335 wxSTD ifstream
store(wxString(file
.fn_str()).mb_str());
336 if (store
.fail() || store
.bad())
338 wxFileInputStream
store(wxString(file
.fn_str()));
339 if (store
.LastError() != wxSTREAM_NOERROR
)
342 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
|wxICON_EXCLAMATION
,
343 GetDocumentWindow());
346 #if wxUSE_STD_IOSTREAM
348 if ( !store
&& !store
.eof() )
350 int res
= LoadObject(store
).LastError();
351 if ((res
!= wxSTREAM_NOERROR
) &&
352 (res
!= wxSTREAM_EOF
))
355 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
|wxICON_EXCLAMATION
,
356 GetDocumentWindow());
359 SetFilename(file
, TRUE
);
368 #if wxUSE_STD_IOSTREAM
369 wxSTD istream
& wxDocument::LoadObject(wxSTD istream
& stream
)
371 wxInputStream
& wxDocument::LoadObject(wxInputStream
& stream
)
377 #if wxUSE_STD_IOSTREAM
378 wxSTD ostream
& wxDocument::SaveObject(wxSTD ostream
& stream
)
380 wxOutputStream
& wxDocument::SaveObject(wxOutputStream
& stream
)
386 bool wxDocument::Revert()
392 // Get title, or filename if no title, else unnamed
393 bool wxDocument::GetPrintableName(wxString
& buf
) const
395 if (m_documentTitle
!= wxT(""))
397 buf
= m_documentTitle
;
400 else if (m_documentFile
!= wxT(""))
402 buf
= wxFileNameFromPath(m_documentFile
);
412 wxWindow
*wxDocument::GetDocumentWindow() const
414 wxView
*view
= GetFirstView();
416 return view
->GetFrame();
418 return wxTheApp
->GetTopWindow();
421 wxCommandProcessor
*wxDocument::OnCreateCommandProcessor()
423 return new wxCommandProcessor
;
426 // TRUE if safe to close
427 bool wxDocument::OnSaveModified()
432 GetPrintableName(title
);
435 if (wxTheApp
->GetAppName() != wxT(""))
436 msgTitle
= wxTheApp
->GetAppName();
438 msgTitle
= wxString(_("Warning"));
441 prompt
.Printf(_("Do you want to save changes to document %s?"),
442 (const wxChar
*)title
);
443 int res
= wxMessageBox(prompt
, msgTitle
,
444 wxYES_NO
|wxCANCEL
|wxICON_QUESTION
,
445 GetDocumentWindow());
451 else if (res
== wxYES
)
453 else if (res
== wxCANCEL
)
459 bool wxDocument::Draw(wxDC
& WXUNUSED(context
))
464 bool wxDocument::AddView(wxView
*view
)
466 if (!m_documentViews
.Member(view
))
468 m_documentViews
.Append(view
);
474 bool wxDocument::RemoveView(wxView
*view
)
476 (void)m_documentViews
.DeleteObject(view
);
481 bool wxDocument::OnCreate(const wxString
& WXUNUSED(path
), long flags
)
483 if (GetDocumentTemplate()->CreateView(this, flags
))
489 // Called after a view is added or removed.
490 // The default implementation deletes the document if
491 // there are no more views.
492 void wxDocument::OnChangedViewList()
494 if (m_documentViews
.Number() == 0)
496 if (OnSaveModified())
503 void wxDocument::UpdateAllViews(wxView
*sender
, wxObject
*hint
)
505 wxNode
*node
= m_documentViews
.First();
508 wxView
*view
= (wxView
*)node
->Data();
509 view
->OnUpdate(sender
, hint
);
514 void wxDocument::NotifyClosing()
516 wxNode
*node
= m_documentViews
.First();
519 wxView
*view
= (wxView
*)node
->Data();
520 view
->OnClosingDocument();
525 void wxDocument::SetFilename(const wxString
& filename
, bool notifyViews
)
527 m_documentFile
= filename
;
530 // Notify the views that the filename has changed
531 wxNode
*node
= m_documentViews
.First();
534 wxView
*view
= (wxView
*)node
->Data();
535 view
->OnChangeFilename();
541 // ----------------------------------------------------------------------------
543 // ----------------------------------------------------------------------------
548 m_viewDocument
= (wxDocument
*) NULL
;
550 m_viewTypeName
= wxT("");
551 m_viewFrame
= (wxFrame
*) NULL
;
556 // GetDocumentManager()->ActivateView(this, FALSE, TRUE);
557 m_viewDocument
->RemoveView(this);
560 // Extend event processing to search the document's event table
561 bool wxView::ProcessEvent(wxEvent
& event
)
563 if ( !GetDocument() || !GetDocument()->ProcessEvent(event
) )
564 return wxEvtHandler::ProcessEvent(event
);
569 void wxView::OnActivateView(bool WXUNUSED(activate
), wxView
*WXUNUSED(activeView
), wxView
*WXUNUSED(deactiveView
))
573 void wxView::OnPrint(wxDC
*dc
, wxObject
*WXUNUSED(info
))
578 void wxView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
))
582 void wxView::OnChangeFilename()
584 if (GetFrame() && GetDocument())
588 GetDocument()->GetPrintableName(title
);
590 GetFrame()->SetTitle(title
);
594 void wxView::SetDocument(wxDocument
*doc
)
596 m_viewDocument
= doc
;
601 bool wxView::Close(bool deleteWindow
)
603 if (OnClose(deleteWindow
))
609 void wxView::Activate(bool activate
)
611 if (GetDocument() && GetDocumentManager())
613 OnActivateView(activate
, this, GetDocumentManager()->GetCurrentView());
614 GetDocumentManager()->ActivateView(this, activate
);
618 bool wxView::OnClose(bool WXUNUSED(deleteWindow
))
620 return GetDocument() ? GetDocument()->Close() : TRUE
;
623 #if wxUSE_PRINTING_ARCHITECTURE
624 wxPrintout
*wxView::OnCreatePrintout()
626 return new wxDocPrintout(this);
628 #endif // wxUSE_PRINTING_ARCHITECTURE
630 // ----------------------------------------------------------------------------
632 // ----------------------------------------------------------------------------
634 wxDocTemplate::wxDocTemplate(wxDocManager
*manager
,
635 const wxString
& descr
,
636 const wxString
& filter
,
639 const wxString
& docTypeName
,
640 const wxString
& viewTypeName
,
641 wxClassInfo
*docClassInfo
,
642 wxClassInfo
*viewClassInfo
,
645 m_documentManager
= manager
;
646 m_description
= descr
;
649 m_fileFilter
= filter
;
651 m_docTypeName
= docTypeName
;
652 m_viewTypeName
= viewTypeName
;
653 m_documentManager
->AssociateTemplate(this);
655 m_docClassInfo
= docClassInfo
;
656 m_viewClassInfo
= viewClassInfo
;
659 wxDocTemplate::~wxDocTemplate()
661 m_documentManager
->DisassociateTemplate(this);
664 // Tries to dynamically construct an object of the right class.
665 wxDocument
*wxDocTemplate::CreateDocument(const wxString
& path
, long flags
)
668 return (wxDocument
*) NULL
;
669 wxDocument
*doc
= (wxDocument
*)m_docClassInfo
->CreateObject();
670 doc
->SetFilename(path
);
671 doc
->SetDocumentTemplate(this);
672 GetDocumentManager()->AddDocument(doc
);
673 doc
->SetCommandProcessor(doc
->OnCreateCommandProcessor());
675 if (doc
->OnCreate(path
, flags
))
679 if (GetDocumentManager()->GetDocuments().Member(doc
))
680 doc
->DeleteAllViews();
681 return (wxDocument
*) NULL
;
685 wxView
*wxDocTemplate::CreateView(wxDocument
*doc
, long flags
)
687 if (!m_viewClassInfo
)
688 return (wxView
*) NULL
;
689 wxView
*view
= (wxView
*)m_viewClassInfo
->CreateObject();
690 view
->SetDocument(doc
);
691 if (view
->OnCreate(doc
, flags
))
698 return (wxView
*) NULL
;
702 // The default (very primitive) format detection: check is the extension is
703 // that of the template
704 bool wxDocTemplate::FileMatchesTemplate(const wxString
& path
)
706 return GetDefaultExtension().IsSameAs(FindExtension(path
));
709 // ----------------------------------------------------------------------------
711 // ----------------------------------------------------------------------------
713 BEGIN_EVENT_TABLE(wxDocManager
, wxEvtHandler
)
714 EVT_MENU(wxID_OPEN
, wxDocManager::OnFileOpen
)
715 EVT_MENU(wxID_CLOSE
, wxDocManager::OnFileClose
)
716 EVT_MENU(wxID_CLOSE_ALL
, wxDocManager::OnFileCloseAll
)
717 EVT_MENU(wxID_REVERT
, wxDocManager::OnFileRevert
)
718 EVT_MENU(wxID_NEW
, wxDocManager::OnFileNew
)
719 EVT_MENU(wxID_SAVE
, wxDocManager::OnFileSave
)
720 EVT_MENU(wxID_SAVEAS
, wxDocManager::OnFileSaveAs
)
721 EVT_MENU(wxID_UNDO
, wxDocManager::OnUndo
)
722 EVT_MENU(wxID_REDO
, wxDocManager::OnRedo
)
724 EVT_UPDATE_UI(wxID_OPEN
, wxDocManager::OnUpdateFileOpen
)
725 EVT_UPDATE_UI(wxID_CLOSE
, wxDocManager::OnUpdateFileClose
)
726 EVT_UPDATE_UI(wxID_CLOSE_ALL
, wxDocManager::OnUpdateFileClose
)
727 EVT_UPDATE_UI(wxID_REVERT
, wxDocManager::OnUpdateFileRevert
)
728 EVT_UPDATE_UI(wxID_NEW
, wxDocManager::OnUpdateFileNew
)
729 EVT_UPDATE_UI(wxID_SAVE
, wxDocManager::OnUpdateFileSave
)
730 EVT_UPDATE_UI(wxID_SAVEAS
, wxDocManager::OnUpdateFileSaveAs
)
731 EVT_UPDATE_UI(wxID_UNDO
, wxDocManager::OnUpdateUndo
)
732 EVT_UPDATE_UI(wxID_REDO
, wxDocManager::OnUpdateRedo
)
734 #if wxUSE_PRINTING_ARCHITECTURE
735 EVT_MENU(wxID_PRINT
, wxDocManager::OnPrint
)
736 EVT_MENU(wxID_PRINT_SETUP
, wxDocManager::OnPrintSetup
)
737 EVT_MENU(wxID_PREVIEW
, wxDocManager::OnPreview
)
739 EVT_UPDATE_UI(wxID_PRINT
, wxDocManager::OnUpdatePrint
)
740 EVT_UPDATE_UI(wxID_PRINT_SETUP
, wxDocManager::OnUpdatePrintSetup
)
741 EVT_UPDATE_UI(wxID_PREVIEW
, wxDocManager::OnUpdatePreview
)
745 wxDocManager
* wxDocManager::sm_docManager
= (wxDocManager
*) NULL
;
747 wxDocManager::wxDocManager(long flags
, bool initialize
)
749 m_defaultDocumentNameCounter
= 1;
751 m_currentView
= (wxView
*) NULL
;
752 m_maxDocsOpen
= 10000;
753 m_fileHistory
= (wxFileHistory
*) NULL
;
756 sm_docManager
= this;
759 wxDocManager::~wxDocManager()
763 delete m_fileHistory
;
764 sm_docManager
= (wxDocManager
*) NULL
;
767 bool wxDocManager::CloseDocuments(bool force
)
769 wxNode
*node
= m_docs
.First();
772 wxDocument
*doc
= (wxDocument
*)node
->Data();
773 wxNode
*next
= node
->Next();
775 if (!doc
->Close() && !force
)
778 // Implicitly deletes the document when the last
779 // view is removed (deleted)
780 doc
->DeleteAllViews();
782 // Check document is deleted
783 if (m_docs
.Member(doc
))
786 // This assumes that documents are not connected in
787 // any way, i.e. deleting one document does NOT
794 bool wxDocManager::Clear(bool force
)
796 if (!CloseDocuments(force
))
799 wxNode
*node
= m_templates
.First();
802 wxDocTemplate
*templ
= (wxDocTemplate
*) node
->Data();
803 wxNode
* next
= node
->Next();
810 bool wxDocManager::Initialize()
812 m_fileHistory
= OnCreateFileHistory();
816 wxFileHistory
*wxDocManager::OnCreateFileHistory()
818 return new wxFileHistory
;
821 void wxDocManager::OnFileClose(wxCommandEvent
& WXUNUSED(event
))
823 wxDocument
*doc
= GetCurrentDocument();
828 doc
->DeleteAllViews();
829 if (m_docs
.Member(doc
))
834 void wxDocManager::OnFileCloseAll(wxCommandEvent
& WXUNUSED(event
))
836 CloseDocuments(FALSE
);
839 void wxDocManager::OnFileNew(wxCommandEvent
& WXUNUSED(event
))
841 CreateDocument(wxString(""), wxDOC_NEW
);
844 void wxDocManager::OnFileOpen(wxCommandEvent
& WXUNUSED(event
))
846 if ( !CreateDocument(wxString(""), 0) )
852 void wxDocManager::OnFileRevert(wxCommandEvent
& WXUNUSED(event
))
854 wxDocument
*doc
= GetCurrentDocument();
860 void wxDocManager::OnFileSave(wxCommandEvent
& WXUNUSED(event
))
862 wxDocument
*doc
= GetCurrentDocument();
868 void wxDocManager::OnFileSaveAs(wxCommandEvent
& WXUNUSED(event
))
870 wxDocument
*doc
= GetCurrentDocument();
876 void wxDocManager::OnPrint(wxCommandEvent
& WXUNUSED(event
))
878 #if wxUSE_PRINTING_ARCHITECTURE
879 wxView
*view
= GetCurrentView();
883 wxPrintout
*printout
= view
->OnCreatePrintout();
887 printer
.Print(view
->GetFrame(), printout
, TRUE
);
891 #endif // wxUSE_PRINTING_ARCHITECTURE
894 void wxDocManager::OnPrintSetup(wxCommandEvent
& WXUNUSED(event
))
896 #if wxUSE_PRINTING_ARCHITECTURE
897 wxWindow
*parentWin
= wxTheApp
->GetTopWindow();
898 wxView
*view
= GetCurrentView();
900 parentWin
= view
->GetFrame();
902 wxPrintDialogData data
;
904 wxPrintDialog
printerDialog(parentWin
, &data
);
905 printerDialog
.GetPrintDialogData().SetSetupDialog(TRUE
);
906 printerDialog
.ShowModal();
907 #endif // wxUSE_PRINTING_ARCHITECTURE
910 void wxDocManager::OnPreview(wxCommandEvent
& WXUNUSED(event
))
912 #if wxUSE_PRINTING_ARCHITECTURE
913 wxView
*view
= GetCurrentView();
917 wxPrintout
*printout
= view
->OnCreatePrintout();
920 // Pass two printout objects: for preview, and possible printing.
921 wxPrintPreviewBase
*preview
= (wxPrintPreviewBase
*) NULL
;
922 preview
= new wxPrintPreview(printout
, view
->OnCreatePrintout());
924 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, (wxFrame
*)wxTheApp
->GetTopWindow(), _("Print Preview"),
925 wxPoint(100, 100), wxSize(600, 650));
926 frame
->Centre(wxBOTH
);
930 #endif // wxUSE_PRINTING_ARCHITECTURE
933 void wxDocManager::OnUndo(wxCommandEvent
& WXUNUSED(event
))
935 wxDocument
*doc
= GetCurrentDocument();
938 if (doc
->GetCommandProcessor())
939 doc
->GetCommandProcessor()->Undo();
942 void wxDocManager::OnRedo(wxCommandEvent
& WXUNUSED(event
))
944 wxDocument
*doc
= GetCurrentDocument();
947 if (doc
->GetCommandProcessor())
948 doc
->GetCommandProcessor()->Redo();
951 // Handlers for UI update commands
953 void wxDocManager::OnUpdateFileOpen(wxUpdateUIEvent
& event
)
955 event
.Enable( TRUE
);
958 void wxDocManager::OnUpdateFileClose(wxUpdateUIEvent
& event
)
960 wxDocument
*doc
= GetCurrentDocument();
961 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
964 void wxDocManager::OnUpdateFileRevert(wxUpdateUIEvent
& event
)
966 wxDocument
*doc
= GetCurrentDocument();
967 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
970 void wxDocManager::OnUpdateFileNew(wxUpdateUIEvent
& event
)
972 event
.Enable( TRUE
);
975 void wxDocManager::OnUpdateFileSave(wxUpdateUIEvent
& event
)
977 wxDocument
*doc
= GetCurrentDocument();
978 event
.Enable( doc
&& doc
->IsModified() );
981 void wxDocManager::OnUpdateFileSaveAs(wxUpdateUIEvent
& event
)
983 wxDocument
*doc
= GetCurrentDocument();
984 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
987 void wxDocManager::OnUpdateUndo(wxUpdateUIEvent
& event
)
989 wxDocument
*doc
= GetCurrentDocument();
990 event
.Enable( (doc
&& doc
->GetCommandProcessor() && doc
->GetCommandProcessor()->CanUndo()) );
991 if (doc
&& doc
->GetCommandProcessor())
992 doc
->GetCommandProcessor()->SetMenuStrings();
995 void wxDocManager::OnUpdateRedo(wxUpdateUIEvent
& event
)
997 wxDocument
*doc
= GetCurrentDocument();
998 event
.Enable( (doc
&& doc
->GetCommandProcessor() && doc
->GetCommandProcessor()->CanRedo()) );
999 if (doc
&& doc
->GetCommandProcessor())
1000 doc
->GetCommandProcessor()->SetMenuStrings();
1003 void wxDocManager::OnUpdatePrint(wxUpdateUIEvent
& event
)
1005 wxDocument
*doc
= GetCurrentDocument();
1006 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1009 void wxDocManager::OnUpdatePrintSetup(wxUpdateUIEvent
& event
)
1011 event
.Enable( TRUE
);
1014 void wxDocManager::OnUpdatePreview(wxUpdateUIEvent
& event
)
1016 wxDocument
*doc
= GetCurrentDocument();
1017 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1020 wxView
*wxDocManager::GetCurrentView() const
1023 return m_currentView
;
1024 if (m_docs
.Number() == 1)
1026 wxDocument
* doc
= (wxDocument
*) m_docs
.First()->Data();
1027 return doc
->GetFirstView();
1029 return (wxView
*) NULL
;
1032 // Extend event processing to search the view's event table
1033 bool wxDocManager::ProcessEvent(wxEvent
& event
)
1035 wxView
* view
= GetCurrentView();
1038 if (view
->ProcessEvent(event
))
1041 return wxEvtHandler::ProcessEvent(event
);
1044 wxDocument
*wxDocManager::CreateDocument(const wxString
& path
, long flags
)
1046 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.Number()];
1049 for (i
= 0; i
< m_templates
.Number(); i
++)
1051 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Nth(i
)->Data());
1052 if (temp
->IsVisible())
1054 templates
[n
] = temp
;
1061 return (wxDocument
*) NULL
;
1064 // If we've reached the max number of docs, close the
1066 if (GetDocuments().Number() >= m_maxDocsOpen
)
1068 wxDocument
*doc
= (wxDocument
*)GetDocuments().First()->Data();
1071 // Implicitly deletes the document when
1072 // the last view is deleted
1073 doc
->DeleteAllViews();
1075 // Check we're really deleted
1076 if (m_docs
.Member(doc
))
1082 return (wxDocument
*) NULL
;
1086 // New document: user chooses a template, unless there's only one.
1087 if (flags
& wxDOC_NEW
)
1091 wxDocTemplate
*temp
= templates
[0];
1093 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
1096 newDoc
->SetDocumentName(temp
->GetDocumentName());
1097 newDoc
->SetDocumentTemplate(temp
);
1098 newDoc
->OnNewDocument();
1103 wxDocTemplate
*temp
= SelectDocumentType(templates
, n
);
1107 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
1110 newDoc
->SetDocumentName(temp
->GetDocumentName());
1111 newDoc
->SetDocumentTemplate(temp
);
1112 newDoc
->OnNewDocument();
1117 return (wxDocument
*) NULL
;
1120 // Existing document
1121 wxDocTemplate
*temp
= (wxDocTemplate
*) NULL
;
1123 wxString
path2(wxT(""));
1124 if (path
!= wxT(""))
1127 if (flags
& wxDOC_SILENT
)
1128 temp
= FindTemplateForPath(path2
);
1130 temp
= SelectDocumentPath(templates
, n
, path2
, flags
);
1136 wxDocument
*newDoc
= temp
->CreateDocument(path2
, flags
);
1139 newDoc
->SetDocumentName(temp
->GetDocumentName());
1140 newDoc
->SetDocumentTemplate(temp
);
1141 if (!newDoc
->OnOpenDocument(path2
))
1143 newDoc
->DeleteAllViews();
1144 // delete newDoc; // Implicitly deleted by DeleteAllViews
1145 return (wxDocument
*) NULL
;
1147 AddFileToHistory(path2
);
1152 return (wxDocument
*) NULL
;
1155 wxView
*wxDocManager::CreateView(wxDocument
*doc
, long flags
)
1157 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.Number()];
1160 for (i
= 0; i
< m_templates
.Number(); i
++)
1162 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Nth(i
)->Data());
1163 if (temp
->IsVisible())
1165 if (temp
->GetDocumentName() == doc
->GetDocumentName())
1167 templates
[n
] = temp
;
1175 return (wxView
*) NULL
;
1179 wxDocTemplate
*temp
= templates
[0];
1181 wxView
*view
= temp
->CreateView(doc
, flags
);
1183 view
->SetViewName(temp
->GetViewName());
1187 wxDocTemplate
*temp
= SelectViewType(templates
, n
);
1191 wxView
*view
= temp
->CreateView(doc
, flags
);
1193 view
->SetViewName(temp
->GetViewName());
1197 return (wxView
*) NULL
;
1200 // Not yet implemented
1201 void wxDocManager::DeleteTemplate(wxDocTemplate
*WXUNUSED(temp
), long WXUNUSED(flags
))
1205 // Not yet implemented
1206 bool wxDocManager::FlushDoc(wxDocument
*WXUNUSED(doc
))
1211 wxDocument
*wxDocManager::GetCurrentDocument() const
1213 wxView
*view
= GetCurrentView();
1215 return view
->GetDocument();
1217 return (wxDocument
*) NULL
;
1220 // Make a default document name
1221 bool wxDocManager::MakeDefaultName(wxString
& name
)
1223 name
.Printf(_("unnamed%d"), m_defaultDocumentNameCounter
);
1224 m_defaultDocumentNameCounter
++;
1229 // Make a frame title (override this to do something different)
1230 // If docName is empty, a document is not currently active.
1231 wxString
wxDocManager::MakeFrameTitle(wxDocument
* doc
)
1233 wxString appName
= wxTheApp
->GetAppName();
1240 doc
->GetPrintableName(docName
);
1241 title
= docName
+ wxString(_(" - ")) + appName
;
1247 // Not yet implemented
1248 wxDocTemplate
*wxDocManager::MatchTemplate(const wxString
& WXUNUSED(path
))
1250 return (wxDocTemplate
*) NULL
;
1253 // File history management
1254 void wxDocManager::AddFileToHistory(const wxString
& file
)
1257 m_fileHistory
->AddFileToHistory(file
);
1260 void wxDocManager::RemoveFileFromHistory(int i
)
1263 m_fileHistory
->RemoveFileFromHistory(i
);
1266 wxString
wxDocManager::GetHistoryFile(int i
) const
1271 histFile
= m_fileHistory
->GetHistoryFile(i
);
1276 void wxDocManager::FileHistoryUseMenu(wxMenu
*menu
)
1279 m_fileHistory
->UseMenu(menu
);
1282 void wxDocManager::FileHistoryRemoveMenu(wxMenu
*menu
)
1285 m_fileHistory
->RemoveMenu(menu
);
1289 void wxDocManager::FileHistoryLoad(wxConfigBase
& config
)
1292 m_fileHistory
->Load(config
);
1295 void wxDocManager::FileHistorySave(wxConfigBase
& config
)
1298 m_fileHistory
->Save(config
);
1302 void wxDocManager::FileHistoryAddFilesToMenu(wxMenu
* menu
)
1305 m_fileHistory
->AddFilesToMenu(menu
);
1308 void wxDocManager::FileHistoryAddFilesToMenu()
1311 m_fileHistory
->AddFilesToMenu();
1314 int wxDocManager::GetNoHistoryFiles() const
1317 return m_fileHistory
->GetNoHistoryFiles();
1323 // Find out the document template via matching in the document file format
1324 // against that of the template
1325 wxDocTemplate
*wxDocManager::FindTemplateForPath(const wxString
& path
)
1327 wxDocTemplate
*theTemplate
= (wxDocTemplate
*) NULL
;
1329 // Find the template which this extension corresponds to
1331 for (i
= 0; i
< m_templates
.Number(); i
++)
1333 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Nth(i
)->Data();
1334 if ( temp
->FileMatchesTemplate(path
) )
1343 // Try to get a more suitable parent frame than the top window,
1344 // for selection dialogs. Otherwise you may get an unexpected
1345 // window being activated when a dialog is shown.
1346 static wxWindow
* wxFindSuitableParent()
1348 wxWindow
* parent
= wxTheApp
->GetTopWindow();
1350 wxWindow
* focusWindow
= wxWindow::FindFocus();
1353 while (focusWindow
&&
1354 !focusWindow
->IsKindOf(CLASSINFO(wxDialog
)) &&
1355 !focusWindow
->IsKindOf(CLASSINFO(wxFrame
)))
1357 focusWindow
= focusWindow
->GetParent();
1360 parent
= focusWindow
;
1365 // Prompts user to open a file, using file specs in templates.
1366 // How to implement in wxWindows? Must extend the file selector
1367 // dialog or implement own; OR match the extension to the
1368 // template extension.
1370 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**templates
,
1371 #if defined(__WXMSW__) || defined(__WXGTK__)
1374 int WXUNUSED(noTemplates
),
1377 long WXUNUSED(flags
),
1378 bool WXUNUSED(save
))
1380 // We can only have multiple filters in Windows and GTK
1381 #if defined(__WXMSW__) || defined(__WXGTK__)
1385 for (i
= 0; i
< noTemplates
; i
++)
1387 if (templates
[i
]->IsVisible())
1389 // add a '|' to separate this filter from the previous one
1390 if ( !descrBuf
.IsEmpty() )
1391 descrBuf
<< wxT('|');
1393 descrBuf
<< templates
[i
]->GetDescription()
1394 << wxT(" (") << templates
[i
]->GetFileFilter() << wxT(") |")
1395 << templates
[i
]->GetFileFilter();
1399 wxString descrBuf
= wxT("*.*");
1402 int FilterIndex
= -1;
1404 wxWindow
* parent
= wxFindSuitableParent();
1406 wxString pathTmp
= wxFileSelectorEx(_("Select a file"),
1414 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)NULL
;
1415 if (!pathTmp
.IsEmpty())
1417 if (!wxFileExists(pathTmp
))
1420 if (!wxTheApp
->GetAppName().IsEmpty())
1421 msgTitle
= wxTheApp
->GetAppName();
1423 msgTitle
= wxString(_("File error"));
1425 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
1429 return (wxDocTemplate
*) NULL
;
1431 m_lastDirectory
= wxPathOnly(pathTmp
);
1435 // first choose the template using the extension, if this fails (i.e.
1436 // wxFileSelectorEx() didn't fill it), then use the path
1437 if ( FilterIndex
!= -1 )
1438 theTemplate
= templates
[FilterIndex
];
1440 theTemplate
= FindTemplateForPath(path
);
1450 // In all other windowing systems, until we have more advanced
1451 // file selectors, we must select the document type (template) first, and
1452 // _then_ pop up the file selector.
1453 wxDocTemplate
*temp
= SelectDocumentType(templates
, noTemplates
);
1455 return (wxDocTemplate
*) NULL
;
1457 wxChar
*pathTmp
= wxFileSelector(_("Select a file"), wxT(""), wxT(""),
1458 temp
->GetDefaultExtension(),
1459 temp
->GetFileFilter(),
1460 0, wxTheApp
->GetTopWindow());
1468 return (wxDocTemplate
*) NULL
;
1472 wxDocTemplate
*wxDocManager::SelectDocumentType(wxDocTemplate
**templates
,
1473 int noTemplates
, bool sort
)
1475 wxArrayString
strings(sort
);
1476 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1480 for (i
= 0; i
< noTemplates
; i
++)
1482 if (templates
[i
]->IsVisible())
1486 for (j
= 0; j
< n
; j
++)
1488 //filter out NOT unique documents + view combinations
1489 if ( templates
[i
]->m_docTypeName
== data
[j
]->m_docTypeName
&&
1490 templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
1497 strings
.Add(templates
[i
]->m_description
);
1499 data
[n
] = templates
[i
];
1507 // Yes, this will be slow, but template lists
1508 // are typically short.
1510 n
= strings
.Count();
1511 for (i
= 0; i
< n
; i
++)
1513 for (j
= 0; j
< noTemplates
; j
++)
1515 if (strings
[i
] == templates
[j
]->m_description
)
1516 data
[i
] = templates
[j
];
1521 wxDocTemplate
*theTemplate
;
1526 // no visible templates, hence nothing to choose from
1531 // don't propose the user to choose if he heas no choice
1532 theTemplate
= data
[0];
1536 // propose the user to choose one of several
1537 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1539 _("Select a document template"),
1543 wxFindSuitableParent()
1552 wxDocTemplate
*wxDocManager::SelectViewType(wxDocTemplate
**templates
,
1553 int noTemplates
, bool sort
)
1555 wxArrayString
strings(sort
);
1556 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1560 for (i
= 0; i
< noTemplates
; i
++)
1562 wxDocTemplate
*templ
= templates
[i
];
1563 if ( templ
->IsVisible() && !templ
->GetViewName().empty() )
1567 for (j
= 0; j
< n
; j
++)
1569 //filter out NOT unique views
1570 if ( templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
)
1576 strings
.Add(templ
->m_viewTypeName
);
1585 // Yes, this will be slow, but template lists
1586 // are typically short.
1588 n
= strings
.Count();
1589 for (i
= 0; i
< n
; i
++)
1591 for (j
= 0; j
< noTemplates
; j
++)
1593 if (strings
[i
] == templates
[j
]->m_viewTypeName
)
1594 data
[i
] = templates
[j
];
1599 wxDocTemplate
*theTemplate
;
1601 // the same logic as above
1605 theTemplate
= (wxDocTemplate
*)NULL
;
1609 theTemplate
= data
[0];
1613 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1615 _("Select a document view"),
1619 wxFindSuitableParent()
1628 void wxDocManager::AssociateTemplate(wxDocTemplate
*temp
)
1630 if (!m_templates
.Member(temp
))
1631 m_templates
.Append(temp
);
1634 void wxDocManager::DisassociateTemplate(wxDocTemplate
*temp
)
1636 m_templates
.DeleteObject(temp
);
1639 // Add and remove a document from the manager's list
1640 void wxDocManager::AddDocument(wxDocument
*doc
)
1642 if (!m_docs
.Member(doc
))
1646 void wxDocManager::RemoveDocument(wxDocument
*doc
)
1648 m_docs
.DeleteObject(doc
);
1651 // Views or windows should inform the document manager
1652 // when a view is going in or out of focus
1653 void wxDocManager::ActivateView(wxView
*view
, bool activate
, bool WXUNUSED(deleting
))
1655 // If we're deactiving, and if we're not actually deleting the view, then
1656 // don't reset the current view because we may be going to
1657 // a window without a view.
1658 // WHAT DID I MEAN BY THAT EXACTLY?
1662 if (m_currentView == view)
1663 m_currentView = NULL;
1669 m_currentView
= view
;
1671 m_currentView
= (wxView
*) NULL
;
1675 // ----------------------------------------------------------------------------
1676 // Default document child frame
1677 // ----------------------------------------------------------------------------
1679 BEGIN_EVENT_TABLE(wxDocChildFrame
, wxFrame
)
1680 EVT_ACTIVATE(wxDocChildFrame::OnActivate
)
1681 EVT_CLOSE(wxDocChildFrame::OnCloseWindow
)
1684 wxDocChildFrame::wxDocChildFrame(wxDocument
*doc
,
1688 const wxString
& title
,
1692 const wxString
& name
)
1693 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1695 m_childDocument
= doc
;
1698 view
->SetFrame(this);
1701 wxDocChildFrame::~wxDocChildFrame()
1705 // Extend event processing to search the view's event table
1706 bool wxDocChildFrame::ProcessEvent(wxEvent
& event
)
1709 m_childView
->Activate(TRUE
);
1711 if ( !m_childView
|| ! m_childView
->ProcessEvent(event
) )
1713 // Only hand up to the parent if it's a menu command
1714 if (!event
.IsKindOf(CLASSINFO(wxCommandEvent
)) || !GetParent() || !GetParent()->ProcessEvent(event
))
1715 return wxEvtHandler::ProcessEvent(event
);
1723 void wxDocChildFrame::OnActivate(wxActivateEvent
& event
)
1725 wxFrame::OnActivate(event
);
1728 m_childView
->Activate(event
.GetActive());
1731 void wxDocChildFrame::OnCloseWindow(wxCloseEvent
& event
)
1736 if (!event
.CanVeto())
1737 ans
= TRUE
; // Must delete.
1739 ans
= m_childView
->Close(FALSE
); // FALSE means don't delete associated window
1743 m_childView
->Activate(FALSE
);
1745 m_childView
= (wxView
*) NULL
;
1746 m_childDocument
= (wxDocument
*) NULL
;
1757 // ----------------------------------------------------------------------------
1758 // Default parent frame
1759 // ----------------------------------------------------------------------------
1761 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1762 EVT_MENU(wxID_EXIT
, wxDocParentFrame::OnExit
)
1763 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE9
, wxDocParentFrame::OnMRUFile
)
1764 EVT_CLOSE(wxDocParentFrame::OnCloseWindow
)
1767 wxDocParentFrame::wxDocParentFrame(wxDocManager
*manager
,
1770 const wxString
& title
,
1774 const wxString
& name
)
1775 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1777 m_docManager
= manager
;
1780 void wxDocParentFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
1785 void wxDocParentFrame::OnMRUFile(wxCommandEvent
& event
)
1787 int n
= event
.GetId() - wxID_FILE1
; // the index in MRU list
1788 wxString
filename(m_docManager
->GetHistoryFile(n
));
1789 if ( !filename
.IsEmpty() )
1791 // verify that the file exists before doing anything else
1792 if ( wxFile::Exists(filename
) )
1795 (void)m_docManager
->CreateDocument(filename
, wxDOC_SILENT
);
1799 // remove the bogus filename from the MRU list and notify the user
1801 m_docManager
->RemoveFileFromHistory(n
);
1803 wxLogError(_("The file '%s' doesn't exist and couldn't be opened.\nIt has been removed from the most recently used files list."),
1809 // Extend event processing to search the view's event table
1810 bool wxDocParentFrame::ProcessEvent(wxEvent
& event
)
1812 // Try the document manager, then do default processing
1813 if (!m_docManager
|| !m_docManager
->ProcessEvent(event
))
1814 return wxEvtHandler::ProcessEvent(event
);
1819 // Define the behaviour for the frame closing
1820 // - must delete all frames except for the main one.
1821 void wxDocParentFrame::OnCloseWindow(wxCloseEvent
& event
)
1823 if (m_docManager
->Clear(!event
.CanVeto()))
1831 #if wxUSE_PRINTING_ARCHITECTURE
1833 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
)
1836 m_printoutView
= view
;
1839 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
1843 // Get the logical pixels per inch of screen and printer
1844 int ppiScreenX
, ppiScreenY
;
1845 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
1846 int ppiPrinterX
, ppiPrinterY
;
1847 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
1849 // This scales the DC so that the printout roughly represents the
1850 // the screen scaling. The text point size _should_ be the right size
1851 // but in fact is too small for some reason. This is a detail that will
1852 // need to be addressed at some point but can be fudged for the
1854 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
1856 // Now we have to check in case our real page size is reduced
1857 // (e.g. because we're drawing to a print preview memory DC)
1858 int pageWidth
, pageHeight
;
1860 dc
->GetSize(&w
, &h
);
1861 GetPageSizePixels(&pageWidth
, &pageHeight
);
1863 // If printer pageWidth == current DC width, then this doesn't
1864 // change. But w might be the preview bitmap width, so scale down.
1865 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
1866 dc
->SetUserScale(overallScale
, overallScale
);
1870 m_printoutView
->OnDraw(dc
);
1875 bool wxDocPrintout::HasPage(int pageNum
)
1877 return (pageNum
== 1);
1880 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
1882 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
1888 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
1896 #endif // wxUSE_PRINTING_ARCHITECTURE
1898 // ----------------------------------------------------------------------------
1899 // File history processor
1900 // ----------------------------------------------------------------------------
1902 wxFileHistory::wxFileHistory(int maxFiles
)
1904 m_fileMaxFiles
= maxFiles
;
1906 m_fileHistory
= new wxChar
*[m_fileMaxFiles
];
1909 wxFileHistory::~wxFileHistory()
1912 for (i
= 0; i
< m_fileHistoryN
; i
++)
1913 delete[] m_fileHistory
[i
];
1914 delete[] m_fileHistory
;
1917 // File history management
1918 void wxFileHistory::AddFileToHistory(const wxString
& file
)
1922 // Check we don't already have this file
1923 for (i
= 0; i
< m_fileHistoryN
; i
++)
1925 if ( m_fileHistory
[i
] && (file
== m_fileHistory
[i
]) )
1927 // we do have it, move it to the top of the history
1928 RemoveFileFromHistory (i
);
1929 AddFileToHistory (file
);
1934 // if we already have a full history, delete the one at the end
1935 if ( m_fileMaxFiles
== m_fileHistoryN
)
1937 RemoveFileFromHistory (m_fileHistoryN
- 1);
1938 AddFileToHistory (file
);
1942 // Add to the project file history:
1943 // Move existing files (if any) down so we can insert file at beginning.
1944 if (m_fileHistoryN
< m_fileMaxFiles
)
1946 wxNode
* node
= m_fileMenus
.First();
1949 wxMenu
* menu
= (wxMenu
*) node
->Data();
1950 if (m_fileHistoryN
== 0)
1951 menu
->AppendSeparator();
1952 menu
->Append(wxID_FILE1
+m_fileHistoryN
, _("[EMPTY]"));
1953 node
= node
->Next();
1957 // Shuffle filenames down
1958 for (i
= (m_fileHistoryN
-1); i
> 0; i
--)
1960 m_fileHistory
[i
] = m_fileHistory
[i
-1];
1962 m_fileHistory
[0] = copystring(file
);
1964 // this is the directory of the last opened file
1965 wxString pathCurrent
;
1966 wxSplitPath( m_fileHistory
[0], &pathCurrent
, NULL
, NULL
);
1967 for (i
= 0; i
< m_fileHistoryN
; i
++)
1969 if ( m_fileHistory
[i
] )
1971 // if in same directory just show the filename; otherwise the full
1973 wxString pathInMenu
, path
, filename
, ext
;
1974 wxSplitPath( m_fileHistory
[i
], &path
, &filename
, &ext
);
1975 if ( path
== pathCurrent
)
1977 pathInMenu
= filename
;
1979 pathInMenu
= pathInMenu
+ wxFILE_SEP_EXT
+ ext
;
1983 // absolute path; could also set relative path
1984 pathInMenu
= m_fileHistory
[i
];
1988 buf
.Printf(s_MRUEntryFormat
, i
+ 1, pathInMenu
.c_str());
1989 wxNode
* node
= m_fileMenus
.First();
1992 wxMenu
* menu
= (wxMenu
*) node
->Data();
1993 menu
->SetLabel(wxID_FILE1
+ i
, buf
);
1994 node
= node
->Next();
2000 void wxFileHistory::RemoveFileFromHistory(int i
)
2002 wxCHECK_RET( i
< m_fileHistoryN
,
2003 wxT("invalid index in wxFileHistory::RemoveFileFromHistory") );
2005 // delete the element from the array (could use memmove() too...)
2006 delete [] m_fileHistory
[i
];
2009 for ( j
= i
; j
< m_fileHistoryN
- 1; j
++ )
2011 m_fileHistory
[j
] = m_fileHistory
[j
+ 1];
2014 wxNode
* node
= m_fileMenus
.First();
2017 wxMenu
* menu
= (wxMenu
*) node
->Data();
2020 // shuffle filenames up
2022 for ( j
= i
; j
< m_fileHistoryN
- 1; j
++ )
2024 buf
.Printf(s_MRUEntryFormat
, j
+ 1, m_fileHistory
[j
]);
2025 menu
->SetLabel(wxID_FILE1
+ j
, buf
);
2028 node
= node
->Next();
2030 // delete the last menu item which is unused now
2031 if (menu
->FindItem(wxID_FILE1
+ m_fileHistoryN
- 1))
2032 menu
->Delete(wxID_FILE1
+ m_fileHistoryN
- 1);
2034 // delete the last separator too if no more files are left
2035 if ( m_fileHistoryN
== 1 )
2037 wxMenuItemList::Node
*node
= menu
->GetMenuItems().GetLast();
2040 wxMenuItem
*menuItem
= node
->GetData();
2041 if ( menuItem
->IsSeparator() )
2043 menu
->Delete(menuItem
);
2045 //else: should we search backwards for the last separator?
2047 //else: menu is empty somehow
2054 wxString
wxFileHistory::GetHistoryFile(int i
) const
2057 if ( i
< m_fileHistoryN
)
2059 s
= m_fileHistory
[i
];
2063 wxFAIL_MSG( wxT("bad index in wxFileHistory::GetHistoryFile") );
2069 void wxFileHistory::UseMenu(wxMenu
*menu
)
2071 if (!m_fileMenus
.Member(menu
))
2072 m_fileMenus
.Append(menu
);
2075 void wxFileHistory::RemoveMenu(wxMenu
*menu
)
2077 m_fileMenus
.DeleteObject(menu
);
2081 void wxFileHistory::Load(wxConfigBase
& config
)
2085 buf
.Printf(wxT("file%d"), m_fileHistoryN
+1);
2086 wxString historyFile
;
2087 while ((m_fileHistoryN
<= m_fileMaxFiles
) && config
.Read(buf
, &historyFile
) && (historyFile
!= wxT("")))
2089 m_fileHistory
[m_fileHistoryN
] = copystring((const wxChar
*) historyFile
);
2091 buf
.Printf(wxT("file%d"), m_fileHistoryN
+1);
2092 historyFile
= wxT("");
2097 void wxFileHistory::Save(wxConfigBase
& config
)
2100 for (i
= 0; i
< m_fileHistoryN
; i
++)
2103 buf
.Printf(wxT("file%d"), i
+1);
2104 config
.Write(buf
, wxString(m_fileHistory
[i
]));
2107 #endif // wxUSE_CONFIG
2109 void wxFileHistory::AddFilesToMenu()
2111 if (m_fileHistoryN
> 0)
2113 wxNode
* node
= m_fileMenus
.First();
2116 wxMenu
* menu
= (wxMenu
*) node
->Data();
2117 menu
->AppendSeparator();
2119 for (i
= 0; i
< m_fileHistoryN
; i
++)
2121 if (m_fileHistory
[i
])
2124 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
2125 menu
->Append(wxID_FILE1
+i
, buf
);
2128 node
= node
->Next();
2133 void wxFileHistory::AddFilesToMenu(wxMenu
* menu
)
2135 if (m_fileHistoryN
> 0)
2137 menu
->AppendSeparator();
2139 for (i
= 0; i
< m_fileHistoryN
; i
++)
2141 if (m_fileHistory
[i
])
2144 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
2145 menu
->Append(wxID_FILE1
+i
, buf
);
2151 // ----------------------------------------------------------------------------
2152 // Permits compatibility with existing file formats and functions that
2153 // manipulate files directly
2154 // ----------------------------------------------------------------------------
2156 #if wxUSE_STD_IOSTREAM
2157 bool wxTransferFileToStream(const wxString
& filename
, wxSTD ostream
& stream
)
2162 if ((fd1
= wxFopen (filename
.fn_str(), _T("rb"))) == NULL
)
2165 while ((ch
= getc (fd1
)) != EOF
)
2166 stream
<< (unsigned char)ch
;
2172 bool wxTransferStreamToFile(wxSTD istream
& stream
, const wxString
& filename
)
2177 if ((fd1
= wxFopen (filename
.fn_str(), _T("wb"))) == NULL
)
2182 while (!stream
.eof())
2192 bool wxTransferFileToStream(const wxString
& filename
, wxOutputStream
& stream
)
2197 if ((fd1
= wxFopen (filename
, wxT("rb"))) == NULL
)
2200 while ((ch
= getc (fd1
)) != EOF
)
2201 stream
.PutC((char) ch
);
2207 bool wxTransferStreamToFile(wxInputStream
& stream
, const wxString
& filename
)
2212 if ((fd1
= wxFopen (filename
, wxT("wb"))) == NULL
)
2217 int len
= stream
.StreamSize();
2218 // TODO: is this the correct test for EOF?
2219 while (stream
.TellI() < (len
- 1))
2229 #endif // wxUSE_DOC_VIEW_ARCHITECTURE