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"
47 #include "wx/filename.h"
54 #if wxUSE_PRINTING_ARCHITECTURE
55 #include "wx/prntbase.h"
56 #include "wx/printdlg.h"
59 #include "wx/msgdlg.h"
60 #include "wx/choicdlg.h"
61 #include "wx/docview.h"
62 #include "wx/confbase.h"
64 #include "wx/cmdproc.h"
69 #if wxUSE_STD_IOSTREAM
70 #include "wx/ioswrap.h"
77 #include "wx/wfstream.h"
80 // ----------------------------------------------------------------------------
82 // ----------------------------------------------------------------------------
84 IMPLEMENT_ABSTRACT_CLASS(wxDocument
, wxEvtHandler
)
85 IMPLEMENT_ABSTRACT_CLASS(wxView
, wxEvtHandler
)
86 IMPLEMENT_ABSTRACT_CLASS(wxDocTemplate
, wxObject
)
87 IMPLEMENT_DYNAMIC_CLASS(wxDocManager
, wxEvtHandler
)
88 IMPLEMENT_CLASS(wxDocChildFrame
, wxFrame
)
89 IMPLEMENT_CLASS(wxDocParentFrame
, wxFrame
)
91 #if wxUSE_PRINTING_ARCHITECTURE
92 IMPLEMENT_DYNAMIC_CLASS(wxDocPrintout
, wxPrintout
)
95 IMPLEMENT_DYNAMIC_CLASS(wxFileHistory
, wxObject
)
97 // ----------------------------------------------------------------------------
98 // function prototypes
99 // ----------------------------------------------------------------------------
101 static inline wxString
FindExtension(const wxChar
*path
);
103 // ----------------------------------------------------------------------------
105 // ----------------------------------------------------------------------------
107 static const wxChar
*s_MRUEntryFormat
= wxT("&%d %s");
109 // ============================================================================
111 // ============================================================================
113 // ----------------------------------------------------------------------------
115 // ----------------------------------------------------------------------------
117 static wxString
FindExtension(const wxChar
*path
)
120 wxSplitPath(path
, NULL
, NULL
, &ext
);
122 // VZ: extensions are considered not case sensitive - is this really a good
124 return ext
.MakeLower();
127 // ----------------------------------------------------------------------------
128 // Definition of wxDocument
129 // ----------------------------------------------------------------------------
131 wxDocument::wxDocument(wxDocument
*parent
)
133 m_documentModified
= FALSE
;
134 m_documentParent
= parent
;
135 m_documentTemplate
= (wxDocTemplate
*) NULL
;
136 m_commandProcessor
= (wxCommandProcessor
*) NULL
;
140 bool wxDocument::DeleteContents()
145 wxDocument::~wxDocument()
149 if (m_commandProcessor
)
150 delete m_commandProcessor
;
152 if (GetDocumentManager())
153 GetDocumentManager()->RemoveDocument(this);
155 // Not safe to do here, since it'll invoke virtual view functions
156 // expecting to see valid derived objects: and by the time we get here,
157 // we've called destructors higher up.
161 bool wxDocument::Close()
163 if (OnSaveModified())
164 return OnCloseDocument();
169 bool wxDocument::OnCloseDocument()
171 // Tell all views that we're about to close
178 // Note that this implicitly deletes the document when the last view is
180 bool wxDocument::DeleteAllViews()
182 wxDocManager
* manager
= GetDocumentManager();
184 wxNode
*node
= m_documentViews
.First();
187 wxView
*view
= (wxView
*)node
->Data();
191 wxNode
*next
= node
->Next();
193 delete view
; // Deletes node implicitly
196 // If we haven't yet deleted the document (for example
197 // if there were no views) then delete it.
198 if (manager
&& manager
->GetDocuments().Member(this))
204 wxView
*wxDocument::GetFirstView() const
206 if (m_documentViews
.Number() == 0)
207 return (wxView
*) NULL
;
208 return (wxView
*)m_documentViews
.First()->Data();
211 wxDocManager
*wxDocument::GetDocumentManager() const
213 return (m_documentTemplate
? m_documentTemplate
->GetDocumentManager() : (wxDocManager
*) NULL
);
216 bool wxDocument::OnNewDocument()
218 if (!OnSaveModified())
221 if (OnCloseDocument()==FALSE
) return FALSE
;
224 SetDocumentSaved(FALSE
);
227 GetDocumentManager()->MakeDefaultName(name
);
229 SetFilename(name
, TRUE
);
234 bool wxDocument::Save()
236 if (!IsModified() && m_savedYet
)
239 if ( m_documentFile
.empty() || !m_savedYet
)
242 return OnSaveDocument(m_documentFile
);
245 bool wxDocument::SaveAs()
247 wxDocTemplate
*docTemplate
= GetDocumentTemplate();
251 wxString tmp
= wxFileSelector(_("Save as"),
252 docTemplate
->GetDirectory(),
253 wxFileNameFromPath(GetFilename()),
254 docTemplate
->GetDefaultExtension(),
255 docTemplate
->GetFileFilter(),
256 wxSAVE
| wxOVERWRITE_PROMPT
,
257 GetDocumentWindow());
262 wxString
fileName(tmp
);
263 wxString path
, name
, ext
;
264 wxSplitPath(fileName
, & path
, & name
, & ext
);
266 if (ext
.IsEmpty() || ext
== wxT(""))
268 fileName
+= wxT(".");
269 fileName
+= docTemplate
->GetDefaultExtension();
272 SetFilename(fileName
);
273 SetTitle(wxFileNameFromPath(fileName
));
275 GetDocumentManager()->AddFileToHistory(fileName
);
277 // Notify the views that the filename has changed
278 wxNode
*node
= m_documentViews
.First();
281 wxView
*view
= (wxView
*)node
->Data();
282 view
->OnChangeFilename();
286 return OnSaveDocument(m_documentFile
);
289 bool wxDocument::OnSaveDocument(const wxString
& file
)
295 if (wxTheApp
->GetAppName() != wxT(""))
296 msgTitle
= wxTheApp
->GetAppName();
298 msgTitle
= wxString(_("File error"));
300 #if wxUSE_STD_IOSTREAM
301 wxSTD ofstream
store(wxString(file
.fn_str()).mb_str()); // ?????
302 if (store
.fail() || store
.bad())
304 wxFileOutputStream
store( file
);
305 if (store
.GetLastError() != wxSTREAM_NO_ERROR
)
308 (void)wxMessageBox(_("Sorry, could not open this file for saving."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
309 GetDocumentWindow());
313 if (!SaveObject(store
))
315 (void)wxMessageBox(_("Sorry, could not save this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
316 GetDocumentWindow());
322 SetDocumentSaved(TRUE
);
324 wxFileName
fn(file
) ;
325 fn
.MacSetDefaultTypeAndCreator() ;
330 bool wxDocument::OnOpenDocument(const wxString
& file
)
332 if (!OnSaveModified())
336 if (wxTheApp
->GetAppName() != wxT(""))
337 msgTitle
= wxTheApp
->GetAppName();
339 msgTitle
= wxString(_("File error"));
341 #if wxUSE_STD_IOSTREAM
342 wxSTD ifstream
store(wxString(file
.fn_str()).mb_str()); // ????
343 if (store
.fail() || store
.bad())
345 wxFileInputStream
store( file
);
346 if (store
.GetLastError() != wxSTREAM_NO_ERROR
)
349 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
|wxICON_EXCLAMATION
,
350 GetDocumentWindow());
353 #if wxUSE_STD_IOSTREAM
355 if ( !store
&& !store
.eof() )
357 int res
= LoadObject(store
).GetLastError();
358 if ((res
!= wxSTREAM_NO_ERROR
) &&
359 (res
!= wxSTREAM_EOF
))
362 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
|wxICON_EXCLAMATION
,
363 GetDocumentWindow());
366 SetFilename(file
, TRUE
);
375 #if wxUSE_STD_IOSTREAM
376 wxSTD istream
& wxDocument::LoadObject(wxSTD istream
& stream
)
378 wxInputStream
& wxDocument::LoadObject(wxInputStream
& stream
)
384 #if wxUSE_STD_IOSTREAM
385 wxSTD ostream
& wxDocument::SaveObject(wxSTD ostream
& stream
)
387 wxOutputStream
& wxDocument::SaveObject(wxOutputStream
& stream
)
393 bool wxDocument::Revert()
399 // Get title, or filename if no title, else unnamed
400 bool wxDocument::GetPrintableName(wxString
& buf
) const
402 if (m_documentTitle
!= wxT(""))
404 buf
= m_documentTitle
;
407 else if (m_documentFile
!= wxT(""))
409 buf
= wxFileNameFromPath(m_documentFile
);
419 wxWindow
*wxDocument::GetDocumentWindow() const
421 wxView
*view
= GetFirstView();
423 return view
->GetFrame();
425 return wxTheApp
->GetTopWindow();
428 wxCommandProcessor
*wxDocument::OnCreateCommandProcessor()
430 return new wxCommandProcessor
;
433 // TRUE if safe to close
434 bool wxDocument::OnSaveModified()
439 GetPrintableName(title
);
442 if (wxTheApp
->GetAppName() != wxT(""))
443 msgTitle
= wxTheApp
->GetAppName();
445 msgTitle
= wxString(_("Warning"));
448 prompt
.Printf(_("Do you want to save changes to document %s?"),
449 (const wxChar
*)title
);
450 int res
= wxMessageBox(prompt
, msgTitle
,
451 wxYES_NO
|wxCANCEL
|wxICON_QUESTION
,
452 GetDocumentWindow());
458 else if (res
== wxYES
)
460 else if (res
== wxCANCEL
)
466 bool wxDocument::Draw(wxDC
& WXUNUSED(context
))
471 bool wxDocument::AddView(wxView
*view
)
473 if (!m_documentViews
.Member(view
))
475 m_documentViews
.Append(view
);
481 bool wxDocument::RemoveView(wxView
*view
)
483 (void)m_documentViews
.DeleteObject(view
);
488 bool wxDocument::OnCreate(const wxString
& WXUNUSED(path
), long flags
)
490 if (GetDocumentTemplate()->CreateView(this, flags
))
496 // Called after a view is added or removed.
497 // The default implementation deletes the document if
498 // there are no more views.
499 void wxDocument::OnChangedViewList()
501 if (m_documentViews
.Number() == 0)
503 if (OnSaveModified())
510 void wxDocument::UpdateAllViews(wxView
*sender
, wxObject
*hint
)
512 wxNode
*node
= m_documentViews
.First();
515 wxView
*view
= (wxView
*)node
->Data();
517 view
->OnUpdate(sender
, hint
);
522 void wxDocument::NotifyClosing()
524 wxNode
*node
= m_documentViews
.First();
527 wxView
*view
= (wxView
*)node
->Data();
528 view
->OnClosingDocument();
533 void wxDocument::SetFilename(const wxString
& filename
, bool notifyViews
)
535 m_documentFile
= filename
;
538 // Notify the views that the filename has changed
539 wxNode
*node
= m_documentViews
.First();
542 wxView
*view
= (wxView
*)node
->Data();
543 view
->OnChangeFilename();
549 // ----------------------------------------------------------------------------
551 // ----------------------------------------------------------------------------
556 m_viewDocument
= (wxDocument
*) NULL
;
558 m_viewTypeName
= wxT("");
559 m_viewFrame
= (wxFrame
*) NULL
;
564 // GetDocumentManager()->ActivateView(this, FALSE, TRUE);
565 m_viewDocument
->RemoveView(this);
568 // Extend event processing to search the document's event table
569 bool wxView::ProcessEvent(wxEvent
& event
)
571 if ( !GetDocument() || !GetDocument()->ProcessEvent(event
) )
572 return wxEvtHandler::ProcessEvent(event
);
577 void wxView::OnActivateView(bool WXUNUSED(activate
), wxView
*WXUNUSED(activeView
), wxView
*WXUNUSED(deactiveView
))
581 void wxView::OnPrint(wxDC
*dc
, wxObject
*WXUNUSED(info
))
586 void wxView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
))
590 void wxView::OnChangeFilename()
592 if (GetFrame() && GetDocument())
596 GetDocument()->GetPrintableName(title
);
598 GetFrame()->SetTitle(title
);
602 void wxView::SetDocument(wxDocument
*doc
)
604 m_viewDocument
= doc
;
609 bool wxView::Close(bool deleteWindow
)
611 if (OnClose(deleteWindow
))
617 void wxView::Activate(bool activate
)
619 if (GetDocument() && GetDocumentManager())
621 OnActivateView(activate
, this, GetDocumentManager()->GetCurrentView());
622 GetDocumentManager()->ActivateView(this, activate
);
626 bool wxView::OnClose(bool WXUNUSED(deleteWindow
))
628 return GetDocument() ? GetDocument()->Close() : TRUE
;
631 #if wxUSE_PRINTING_ARCHITECTURE
632 wxPrintout
*wxView::OnCreatePrintout()
634 return new wxDocPrintout(this);
636 #endif // wxUSE_PRINTING_ARCHITECTURE
638 // ----------------------------------------------------------------------------
640 // ----------------------------------------------------------------------------
642 wxDocTemplate::wxDocTemplate(wxDocManager
*manager
,
643 const wxString
& descr
,
644 const wxString
& filter
,
647 const wxString
& docTypeName
,
648 const wxString
& viewTypeName
,
649 wxClassInfo
*docClassInfo
,
650 wxClassInfo
*viewClassInfo
,
653 m_documentManager
= manager
;
654 m_description
= descr
;
657 m_fileFilter
= filter
;
659 m_docTypeName
= docTypeName
;
660 m_viewTypeName
= viewTypeName
;
661 m_documentManager
->AssociateTemplate(this);
663 m_docClassInfo
= docClassInfo
;
664 m_viewClassInfo
= viewClassInfo
;
667 wxDocTemplate::~wxDocTemplate()
669 m_documentManager
->DisassociateTemplate(this);
672 // Tries to dynamically construct an object of the right class.
673 wxDocument
*wxDocTemplate::CreateDocument(const wxString
& path
, long flags
)
676 return (wxDocument
*) NULL
;
677 wxDocument
*doc
= (wxDocument
*)m_docClassInfo
->CreateObject();
678 doc
->SetFilename(path
);
679 doc
->SetDocumentTemplate(this);
680 GetDocumentManager()->AddDocument(doc
);
681 doc
->SetCommandProcessor(doc
->OnCreateCommandProcessor());
683 if (doc
->OnCreate(path
, flags
))
687 if (GetDocumentManager()->GetDocuments().Member(doc
))
688 doc
->DeleteAllViews();
689 return (wxDocument
*) NULL
;
693 wxView
*wxDocTemplate::CreateView(wxDocument
*doc
, long flags
)
695 if (!m_viewClassInfo
)
696 return (wxView
*) NULL
;
697 wxView
*view
= (wxView
*)m_viewClassInfo
->CreateObject();
698 view
->SetDocument(doc
);
699 if (view
->OnCreate(doc
, flags
))
706 return (wxView
*) NULL
;
710 // The default (very primitive) format detection: check is the extension is
711 // that of the template
712 bool wxDocTemplate::FileMatchesTemplate(const wxString
& path
)
714 return GetDefaultExtension().IsSameAs(FindExtension(path
));
717 // ----------------------------------------------------------------------------
719 // ----------------------------------------------------------------------------
721 BEGIN_EVENT_TABLE(wxDocManager
, wxEvtHandler
)
722 EVT_MENU(wxID_OPEN
, wxDocManager::OnFileOpen
)
723 EVT_MENU(wxID_CLOSE
, wxDocManager::OnFileClose
)
724 EVT_MENU(wxID_CLOSE_ALL
, wxDocManager::OnFileCloseAll
)
725 EVT_MENU(wxID_REVERT
, wxDocManager::OnFileRevert
)
726 EVT_MENU(wxID_NEW
, wxDocManager::OnFileNew
)
727 EVT_MENU(wxID_SAVE
, wxDocManager::OnFileSave
)
728 EVT_MENU(wxID_SAVEAS
, wxDocManager::OnFileSaveAs
)
729 EVT_MENU(wxID_UNDO
, wxDocManager::OnUndo
)
730 EVT_MENU(wxID_REDO
, wxDocManager::OnRedo
)
732 EVT_UPDATE_UI(wxID_OPEN
, wxDocManager::OnUpdateFileOpen
)
733 EVT_UPDATE_UI(wxID_CLOSE
, wxDocManager::OnUpdateFileClose
)
734 EVT_UPDATE_UI(wxID_CLOSE_ALL
, wxDocManager::OnUpdateFileClose
)
735 EVT_UPDATE_UI(wxID_REVERT
, wxDocManager::OnUpdateFileRevert
)
736 EVT_UPDATE_UI(wxID_NEW
, wxDocManager::OnUpdateFileNew
)
737 EVT_UPDATE_UI(wxID_SAVE
, wxDocManager::OnUpdateFileSave
)
738 EVT_UPDATE_UI(wxID_SAVEAS
, wxDocManager::OnUpdateFileSaveAs
)
739 EVT_UPDATE_UI(wxID_UNDO
, wxDocManager::OnUpdateUndo
)
740 EVT_UPDATE_UI(wxID_REDO
, wxDocManager::OnUpdateRedo
)
742 #if wxUSE_PRINTING_ARCHITECTURE
743 EVT_MENU(wxID_PRINT
, wxDocManager::OnPrint
)
744 EVT_MENU(wxID_PRINT_SETUP
, wxDocManager::OnPrintSetup
)
745 EVT_MENU(wxID_PREVIEW
, wxDocManager::OnPreview
)
747 EVT_UPDATE_UI(wxID_PRINT
, wxDocManager::OnUpdatePrint
)
748 EVT_UPDATE_UI(wxID_PRINT_SETUP
, wxDocManager::OnUpdatePrintSetup
)
749 EVT_UPDATE_UI(wxID_PREVIEW
, wxDocManager::OnUpdatePreview
)
753 wxDocManager
* wxDocManager::sm_docManager
= (wxDocManager
*) NULL
;
755 wxDocManager::wxDocManager(long flags
, bool initialize
)
757 m_defaultDocumentNameCounter
= 1;
759 m_currentView
= (wxView
*) NULL
;
760 m_maxDocsOpen
= 10000;
761 m_fileHistory
= (wxFileHistory
*) NULL
;
764 sm_docManager
= this;
767 wxDocManager::~wxDocManager()
771 delete m_fileHistory
;
772 sm_docManager
= (wxDocManager
*) NULL
;
775 bool wxDocManager::CloseDocuments(bool force
)
777 wxNode
*node
= m_docs
.First();
780 wxDocument
*doc
= (wxDocument
*)node
->Data();
781 wxNode
*next
= node
->Next();
783 if (!doc
->Close() && !force
)
786 // Implicitly deletes the document when the last
787 // view is removed (deleted)
788 doc
->DeleteAllViews();
790 // Check document is deleted
791 if (m_docs
.Member(doc
))
794 // This assumes that documents are not connected in
795 // any way, i.e. deleting one document does NOT
802 bool wxDocManager::Clear(bool force
)
804 if (!CloseDocuments(force
))
807 wxNode
*node
= m_templates
.First();
810 wxDocTemplate
*templ
= (wxDocTemplate
*) node
->Data();
811 wxNode
* next
= node
->Next();
818 bool wxDocManager::Initialize()
820 m_fileHistory
= OnCreateFileHistory();
824 wxFileHistory
*wxDocManager::OnCreateFileHistory()
826 return new wxFileHistory
;
829 void wxDocManager::OnFileClose(wxCommandEvent
& WXUNUSED(event
))
831 wxDocument
*doc
= GetCurrentDocument();
836 doc
->DeleteAllViews();
837 if (m_docs
.Member(doc
))
842 void wxDocManager::OnFileCloseAll(wxCommandEvent
& WXUNUSED(event
))
844 CloseDocuments(FALSE
);
847 void wxDocManager::OnFileNew(wxCommandEvent
& WXUNUSED(event
))
849 CreateDocument( wxT(""), wxDOC_NEW
);
852 void wxDocManager::OnFileOpen(wxCommandEvent
& WXUNUSED(event
))
854 if ( !CreateDocument( wxT(""), 0) )
860 void wxDocManager::OnFileRevert(wxCommandEvent
& WXUNUSED(event
))
862 wxDocument
*doc
= GetCurrentDocument();
868 void wxDocManager::OnFileSave(wxCommandEvent
& WXUNUSED(event
))
870 wxDocument
*doc
= GetCurrentDocument();
876 void wxDocManager::OnFileSaveAs(wxCommandEvent
& WXUNUSED(event
))
878 wxDocument
*doc
= GetCurrentDocument();
884 void wxDocManager::OnPrint(wxCommandEvent
& WXUNUSED(event
))
886 #if wxUSE_PRINTING_ARCHITECTURE
887 wxView
*view
= GetCurrentView();
891 wxPrintout
*printout
= view
->OnCreatePrintout();
895 printer
.Print(view
->GetFrame(), printout
, TRUE
);
899 #endif // wxUSE_PRINTING_ARCHITECTURE
902 void wxDocManager::OnPrintSetup(wxCommandEvent
& WXUNUSED(event
))
904 #if wxUSE_PRINTING_ARCHITECTURE
905 wxWindow
*parentWin
= wxTheApp
->GetTopWindow();
906 wxView
*view
= GetCurrentView();
908 parentWin
= view
->GetFrame();
910 wxPrintDialogData data
;
912 wxPrintDialog
printerDialog(parentWin
, &data
);
913 printerDialog
.GetPrintDialogData().SetSetupDialog(TRUE
);
914 printerDialog
.ShowModal();
915 #endif // wxUSE_PRINTING_ARCHITECTURE
918 void wxDocManager::OnPreview(wxCommandEvent
& WXUNUSED(event
))
920 #if wxUSE_PRINTING_ARCHITECTURE
921 wxView
*view
= GetCurrentView();
925 wxPrintout
*printout
= view
->OnCreatePrintout();
928 // Pass two printout objects: for preview, and possible printing.
929 wxPrintPreviewBase
*preview
= (wxPrintPreviewBase
*) NULL
;
930 preview
= new wxPrintPreview(printout
, view
->OnCreatePrintout());
932 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, (wxFrame
*)wxTheApp
->GetTopWindow(), _("Print Preview"),
933 wxPoint(100, 100), wxSize(600, 650));
934 frame
->Centre(wxBOTH
);
938 #endif // wxUSE_PRINTING_ARCHITECTURE
941 void wxDocManager::OnUndo(wxCommandEvent
& WXUNUSED(event
))
943 wxDocument
*doc
= GetCurrentDocument();
946 if (doc
->GetCommandProcessor())
947 doc
->GetCommandProcessor()->Undo();
950 void wxDocManager::OnRedo(wxCommandEvent
& WXUNUSED(event
))
952 wxDocument
*doc
= GetCurrentDocument();
955 if (doc
->GetCommandProcessor())
956 doc
->GetCommandProcessor()->Redo();
959 // Handlers for UI update commands
961 void wxDocManager::OnUpdateFileOpen(wxUpdateUIEvent
& event
)
963 event
.Enable( TRUE
);
966 void wxDocManager::OnUpdateFileClose(wxUpdateUIEvent
& event
)
968 wxDocument
*doc
= GetCurrentDocument();
969 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
972 void wxDocManager::OnUpdateFileRevert(wxUpdateUIEvent
& event
)
974 wxDocument
*doc
= GetCurrentDocument();
975 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
978 void wxDocManager::OnUpdateFileNew(wxUpdateUIEvent
& event
)
980 event
.Enable( TRUE
);
983 void wxDocManager::OnUpdateFileSave(wxUpdateUIEvent
& event
)
985 wxDocument
*doc
= GetCurrentDocument();
986 event
.Enable( doc
&& doc
->IsModified() );
989 void wxDocManager::OnUpdateFileSaveAs(wxUpdateUIEvent
& event
)
991 wxDocument
*doc
= GetCurrentDocument();
992 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
995 void wxDocManager::OnUpdateUndo(wxUpdateUIEvent
& event
)
997 wxDocument
*doc
= GetCurrentDocument();
998 event
.Enable( (doc
&& doc
->GetCommandProcessor() && doc
->GetCommandProcessor()->CanUndo()) );
999 if (doc
&& doc
->GetCommandProcessor())
1000 doc
->GetCommandProcessor()->SetMenuStrings();
1003 void wxDocManager::OnUpdateRedo(wxUpdateUIEvent
& event
)
1005 wxDocument
*doc
= GetCurrentDocument();
1006 event
.Enable( (doc
&& doc
->GetCommandProcessor() && doc
->GetCommandProcessor()->CanRedo()) );
1007 if (doc
&& doc
->GetCommandProcessor())
1008 doc
->GetCommandProcessor()->SetMenuStrings();
1011 void wxDocManager::OnUpdatePrint(wxUpdateUIEvent
& event
)
1013 wxDocument
*doc
= GetCurrentDocument();
1014 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1017 void wxDocManager::OnUpdatePrintSetup(wxUpdateUIEvent
& event
)
1019 event
.Enable( TRUE
);
1022 void wxDocManager::OnUpdatePreview(wxUpdateUIEvent
& event
)
1024 wxDocument
*doc
= GetCurrentDocument();
1025 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1028 wxView
*wxDocManager::GetCurrentView() const
1031 return m_currentView
;
1032 if (m_docs
.Number() == 1)
1034 wxDocument
* doc
= (wxDocument
*) m_docs
.First()->Data();
1035 return doc
->GetFirstView();
1037 return (wxView
*) NULL
;
1040 // Extend event processing to search the view's event table
1041 bool wxDocManager::ProcessEvent(wxEvent
& event
)
1043 wxView
* view
= GetCurrentView();
1046 if (view
->ProcessEvent(event
))
1049 return wxEvtHandler::ProcessEvent(event
);
1052 wxDocument
*wxDocManager::CreateDocument(const wxString
& path
, long flags
)
1054 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.Number()];
1057 for (i
= 0; i
< m_templates
.Number(); i
++)
1059 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Nth(i
)->Data());
1060 if (temp
->IsVisible())
1062 templates
[n
] = temp
;
1069 return (wxDocument
*) NULL
;
1072 // If we've reached the max number of docs, close the
1074 if (GetDocuments().Number() >= m_maxDocsOpen
)
1076 wxDocument
*doc
= (wxDocument
*)GetDocuments().First()->Data();
1079 // Implicitly deletes the document when
1080 // the last view is deleted
1081 doc
->DeleteAllViews();
1083 // Check we're really deleted
1084 if (m_docs
.Member(doc
))
1090 return (wxDocument
*) NULL
;
1094 // New document: user chooses a template, unless there's only one.
1095 if (flags
& wxDOC_NEW
)
1099 wxDocTemplate
*temp
= templates
[0];
1101 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
1104 newDoc
->SetDocumentName(temp
->GetDocumentName());
1105 newDoc
->SetDocumentTemplate(temp
);
1106 newDoc
->OnNewDocument();
1111 wxDocTemplate
*temp
= SelectDocumentType(templates
, n
);
1115 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
1118 newDoc
->SetDocumentName(temp
->GetDocumentName());
1119 newDoc
->SetDocumentTemplate(temp
);
1120 newDoc
->OnNewDocument();
1125 return (wxDocument
*) NULL
;
1128 // Existing document
1129 wxDocTemplate
*temp
= (wxDocTemplate
*) NULL
;
1131 wxString
path2(wxT(""));
1132 if (path
!= wxT(""))
1135 if (flags
& wxDOC_SILENT
)
1136 temp
= FindTemplateForPath(path2
);
1138 temp
= SelectDocumentPath(templates
, n
, path2
, flags
);
1144 wxDocument
*newDoc
= temp
->CreateDocument(path2
, flags
);
1147 newDoc
->SetDocumentName(temp
->GetDocumentName());
1148 newDoc
->SetDocumentTemplate(temp
);
1149 if (!newDoc
->OnOpenDocument(path2
))
1151 newDoc
->DeleteAllViews();
1152 // delete newDoc; // Implicitly deleted by DeleteAllViews
1153 return (wxDocument
*) NULL
;
1155 AddFileToHistory(path2
);
1160 return (wxDocument
*) NULL
;
1163 wxView
*wxDocManager::CreateView(wxDocument
*doc
, long flags
)
1165 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.Number()];
1168 for (i
= 0; i
< m_templates
.Number(); i
++)
1170 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Nth(i
)->Data());
1171 if (temp
->IsVisible())
1173 if (temp
->GetDocumentName() == doc
->GetDocumentName())
1175 templates
[n
] = temp
;
1183 return (wxView
*) NULL
;
1187 wxDocTemplate
*temp
= templates
[0];
1189 wxView
*view
= temp
->CreateView(doc
, flags
);
1191 view
->SetViewName(temp
->GetViewName());
1195 wxDocTemplate
*temp
= SelectViewType(templates
, n
);
1199 wxView
*view
= temp
->CreateView(doc
, flags
);
1201 view
->SetViewName(temp
->GetViewName());
1205 return (wxView
*) NULL
;
1208 // Not yet implemented
1209 void wxDocManager::DeleteTemplate(wxDocTemplate
*WXUNUSED(temp
), long WXUNUSED(flags
))
1213 // Not yet implemented
1214 bool wxDocManager::FlushDoc(wxDocument
*WXUNUSED(doc
))
1219 wxDocument
*wxDocManager::GetCurrentDocument() const
1221 wxView
*view
= GetCurrentView();
1223 return view
->GetDocument();
1225 return (wxDocument
*) NULL
;
1228 // Make a default document name
1229 bool wxDocManager::MakeDefaultName(wxString
& name
)
1231 name
.Printf(_("unnamed%d"), m_defaultDocumentNameCounter
);
1232 m_defaultDocumentNameCounter
++;
1237 // Make a frame title (override this to do something different)
1238 // If docName is empty, a document is not currently active.
1239 wxString
wxDocManager::MakeFrameTitle(wxDocument
* doc
)
1241 wxString appName
= wxTheApp
->GetAppName();
1248 doc
->GetPrintableName(docName
);
1249 title
= docName
+ wxString(_(" - ")) + appName
;
1255 // Not yet implemented
1256 wxDocTemplate
*wxDocManager::MatchTemplate(const wxString
& WXUNUSED(path
))
1258 return (wxDocTemplate
*) NULL
;
1261 // File history management
1262 void wxDocManager::AddFileToHistory(const wxString
& file
)
1265 m_fileHistory
->AddFileToHistory(file
);
1268 void wxDocManager::RemoveFileFromHistory(int i
)
1271 m_fileHistory
->RemoveFileFromHistory(i
);
1274 wxString
wxDocManager::GetHistoryFile(int i
) const
1279 histFile
= m_fileHistory
->GetHistoryFile(i
);
1284 void wxDocManager::FileHistoryUseMenu(wxMenu
*menu
)
1287 m_fileHistory
->UseMenu(menu
);
1290 void wxDocManager::FileHistoryRemoveMenu(wxMenu
*menu
)
1293 m_fileHistory
->RemoveMenu(menu
);
1297 void wxDocManager::FileHistoryLoad(wxConfigBase
& config
)
1300 m_fileHistory
->Load(config
);
1303 void wxDocManager::FileHistorySave(wxConfigBase
& config
)
1306 m_fileHistory
->Save(config
);
1310 void wxDocManager::FileHistoryAddFilesToMenu(wxMenu
* menu
)
1313 m_fileHistory
->AddFilesToMenu(menu
);
1316 void wxDocManager::FileHistoryAddFilesToMenu()
1319 m_fileHistory
->AddFilesToMenu();
1322 int wxDocManager::GetNoHistoryFiles() const
1325 return m_fileHistory
->GetNoHistoryFiles();
1331 // Find out the document template via matching in the document file format
1332 // against that of the template
1333 wxDocTemplate
*wxDocManager::FindTemplateForPath(const wxString
& path
)
1335 wxDocTemplate
*theTemplate
= (wxDocTemplate
*) NULL
;
1337 // Find the template which this extension corresponds to
1339 for (i
= 0; i
< m_templates
.Number(); i
++)
1341 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Nth(i
)->Data();
1342 if ( temp
->FileMatchesTemplate(path
) )
1351 // Try to get a more suitable parent frame than the top window,
1352 // for selection dialogs. Otherwise you may get an unexpected
1353 // window being activated when a dialog is shown.
1354 static wxWindow
* wxFindSuitableParent()
1356 wxWindow
* parent
= wxTheApp
->GetTopWindow();
1358 wxWindow
* focusWindow
= wxWindow::FindFocus();
1361 while (focusWindow
&&
1362 !focusWindow
->IsKindOf(CLASSINFO(wxDialog
)) &&
1363 !focusWindow
->IsKindOf(CLASSINFO(wxFrame
)))
1365 focusWindow
= focusWindow
->GetParent();
1368 parent
= focusWindow
;
1373 // Prompts user to open a file, using file specs in templates.
1374 // How to implement in wxWindows? Must extend the file selector
1375 // dialog or implement own; OR match the extension to the
1376 // template extension.
1378 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**templates
,
1379 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
1382 int WXUNUSED(noTemplates
),
1385 long WXUNUSED(flags
),
1386 bool WXUNUSED(save
))
1388 // We can only have multiple filters in Windows and GTK
1389 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
1393 for (i
= 0; i
< noTemplates
; i
++)
1395 if (templates
[i
]->IsVisible())
1397 // add a '|' to separate this filter from the previous one
1398 if ( !descrBuf
.IsEmpty() )
1399 descrBuf
<< wxT('|');
1401 descrBuf
<< templates
[i
]->GetDescription()
1402 << wxT(" (") << templates
[i
]->GetFileFilter() << wxT(") |")
1403 << templates
[i
]->GetFileFilter();
1407 wxString descrBuf
= wxT("*.*");
1410 int FilterIndex
= -1;
1412 wxWindow
* parent
= wxFindSuitableParent();
1414 wxString pathTmp
= wxFileSelectorEx(_("Select a file"),
1422 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)NULL
;
1423 if (!pathTmp
.IsEmpty())
1425 if (!wxFileExists(pathTmp
))
1428 if (!wxTheApp
->GetAppName().IsEmpty())
1429 msgTitle
= wxTheApp
->GetAppName();
1431 msgTitle
= wxString(_("File error"));
1433 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
1437 return (wxDocTemplate
*) NULL
;
1439 m_lastDirectory
= wxPathOnly(pathTmp
);
1443 // first choose the template using the extension, if this fails (i.e.
1444 // wxFileSelectorEx() didn't fill it), then use the path
1445 if ( FilterIndex
!= -1 )
1446 theTemplate
= templates
[FilterIndex
];
1448 theTemplate
= FindTemplateForPath(path
);
1458 // In all other windowing systems, until we have more advanced
1459 // file selectors, we must select the document type (template) first, and
1460 // _then_ pop up the file selector.
1461 wxDocTemplate
*temp
= SelectDocumentType(templates
, noTemplates
);
1463 return (wxDocTemplate
*) NULL
;
1465 wxChar
*pathTmp
= wxFileSelector(_("Select a file"), wxT(""), wxT(""),
1466 temp
->GetDefaultExtension(),
1467 temp
->GetFileFilter(),
1468 0, wxTheApp
->GetTopWindow());
1476 return (wxDocTemplate
*) NULL
;
1480 wxDocTemplate
*wxDocManager::SelectDocumentType(wxDocTemplate
**templates
,
1481 int noTemplates
, bool sort
)
1483 wxArrayString
strings(sort
);
1484 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1488 for (i
= 0; i
< noTemplates
; i
++)
1490 if (templates
[i
]->IsVisible())
1494 for (j
= 0; j
< n
; j
++)
1496 //filter out NOT unique documents + view combinations
1497 if ( templates
[i
]->m_docTypeName
== data
[j
]->m_docTypeName
&&
1498 templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
1505 strings
.Add(templates
[i
]->m_description
);
1507 data
[n
] = templates
[i
];
1515 // Yes, this will be slow, but template lists
1516 // are typically short.
1518 n
= strings
.Count();
1519 for (i
= 0; i
< n
; i
++)
1521 for (j
= 0; j
< noTemplates
; j
++)
1523 if (strings
[i
] == templates
[j
]->m_description
)
1524 data
[i
] = templates
[j
];
1529 wxDocTemplate
*theTemplate
;
1534 // no visible templates, hence nothing to choose from
1539 // don't propose the user to choose if he heas no choice
1540 theTemplate
= data
[0];
1544 // propose the user to choose one of several
1545 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1547 _("Select a document template"),
1551 wxFindSuitableParent()
1560 wxDocTemplate
*wxDocManager::SelectViewType(wxDocTemplate
**templates
,
1561 int noTemplates
, bool sort
)
1563 wxArrayString
strings(sort
);
1564 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1568 for (i
= 0; i
< noTemplates
; i
++)
1570 wxDocTemplate
*templ
= templates
[i
];
1571 if ( templ
->IsVisible() && !templ
->GetViewName().empty() )
1575 for (j
= 0; j
< n
; j
++)
1577 //filter out NOT unique views
1578 if ( templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
)
1584 strings
.Add(templ
->m_viewTypeName
);
1593 // Yes, this will be slow, but template lists
1594 // are typically short.
1596 n
= strings
.Count();
1597 for (i
= 0; i
< n
; i
++)
1599 for (j
= 0; j
< noTemplates
; j
++)
1601 if (strings
[i
] == templates
[j
]->m_viewTypeName
)
1602 data
[i
] = templates
[j
];
1607 wxDocTemplate
*theTemplate
;
1609 // the same logic as above
1613 theTemplate
= (wxDocTemplate
*)NULL
;
1617 theTemplate
= data
[0];
1621 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1623 _("Select a document view"),
1627 wxFindSuitableParent()
1636 void wxDocManager::AssociateTemplate(wxDocTemplate
*temp
)
1638 if (!m_templates
.Member(temp
))
1639 m_templates
.Append(temp
);
1642 void wxDocManager::DisassociateTemplate(wxDocTemplate
*temp
)
1644 m_templates
.DeleteObject(temp
);
1647 // Add and remove a document from the manager's list
1648 void wxDocManager::AddDocument(wxDocument
*doc
)
1650 if (!m_docs
.Member(doc
))
1654 void wxDocManager::RemoveDocument(wxDocument
*doc
)
1656 m_docs
.DeleteObject(doc
);
1659 // Views or windows should inform the document manager
1660 // when a view is going in or out of focus
1661 void wxDocManager::ActivateView(wxView
*view
, bool activate
, bool WXUNUSED(deleting
))
1663 // If we're deactiving, and if we're not actually deleting the view, then
1664 // don't reset the current view because we may be going to
1665 // a window without a view.
1666 // WHAT DID I MEAN BY THAT EXACTLY?
1670 if (m_currentView == view)
1671 m_currentView = NULL;
1677 m_currentView
= view
;
1679 m_currentView
= (wxView
*) NULL
;
1683 // ----------------------------------------------------------------------------
1684 // Default document child frame
1685 // ----------------------------------------------------------------------------
1687 BEGIN_EVENT_TABLE(wxDocChildFrame
, wxFrame
)
1688 EVT_ACTIVATE(wxDocChildFrame::OnActivate
)
1689 EVT_CLOSE(wxDocChildFrame::OnCloseWindow
)
1692 wxDocChildFrame::wxDocChildFrame(wxDocument
*doc
,
1696 const wxString
& title
,
1700 const wxString
& name
)
1701 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1703 m_childDocument
= doc
;
1706 view
->SetFrame(this);
1709 wxDocChildFrame::~wxDocChildFrame()
1713 // Extend event processing to search the view's event table
1714 bool wxDocChildFrame::ProcessEvent(wxEvent
& event
)
1717 m_childView
->Activate(TRUE
);
1719 if ( !m_childView
|| ! m_childView
->ProcessEvent(event
) )
1721 // Only hand up to the parent if it's a menu command
1722 if (!event
.IsKindOf(CLASSINFO(wxCommandEvent
)) || !GetParent() || !GetParent()->ProcessEvent(event
))
1723 return wxEvtHandler::ProcessEvent(event
);
1731 void wxDocChildFrame::OnActivate(wxActivateEvent
& event
)
1733 wxFrame::OnActivate(event
);
1736 m_childView
->Activate(event
.GetActive());
1739 void wxDocChildFrame::OnCloseWindow(wxCloseEvent
& event
)
1744 if (!event
.CanVeto())
1745 ans
= TRUE
; // Must delete.
1747 ans
= m_childView
->Close(FALSE
); // FALSE means don't delete associated window
1751 m_childView
->Activate(FALSE
);
1753 m_childView
= (wxView
*) NULL
;
1754 m_childDocument
= (wxDocument
*) NULL
;
1765 // ----------------------------------------------------------------------------
1766 // Default parent frame
1767 // ----------------------------------------------------------------------------
1769 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1770 EVT_MENU(wxID_EXIT
, wxDocParentFrame::OnExit
)
1771 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE9
, wxDocParentFrame::OnMRUFile
)
1772 EVT_CLOSE(wxDocParentFrame::OnCloseWindow
)
1775 wxDocParentFrame::wxDocParentFrame(wxDocManager
*manager
,
1778 const wxString
& title
,
1782 const wxString
& name
)
1783 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1785 m_docManager
= manager
;
1788 void wxDocParentFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
1793 void wxDocParentFrame::OnMRUFile(wxCommandEvent
& event
)
1795 int n
= event
.GetId() - wxID_FILE1
; // the index in MRU list
1796 wxString
filename(m_docManager
->GetHistoryFile(n
));
1797 if ( !filename
.IsEmpty() )
1799 // verify that the file exists before doing anything else
1800 if ( wxFile::Exists(filename
) )
1803 (void)m_docManager
->CreateDocument(filename
, wxDOC_SILENT
);
1807 // remove the bogus filename from the MRU list and notify the user
1809 m_docManager
->RemoveFileFromHistory(n
);
1811 wxLogError(_("The file '%s' doesn't exist and couldn't be opened.\nIt has been removed from the most recently used files list."),
1817 // Extend event processing to search the view's event table
1818 bool wxDocParentFrame::ProcessEvent(wxEvent
& event
)
1820 // Try the document manager, then do default processing
1821 if (!m_docManager
|| !m_docManager
->ProcessEvent(event
))
1822 return wxEvtHandler::ProcessEvent(event
);
1827 // Define the behaviour for the frame closing
1828 // - must delete all frames except for the main one.
1829 void wxDocParentFrame::OnCloseWindow(wxCloseEvent
& event
)
1831 if (m_docManager
->Clear(!event
.CanVeto()))
1839 #if wxUSE_PRINTING_ARCHITECTURE
1841 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
)
1844 m_printoutView
= view
;
1847 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
1851 // Get the logical pixels per inch of screen and printer
1852 int ppiScreenX
, ppiScreenY
;
1853 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
1854 int ppiPrinterX
, ppiPrinterY
;
1855 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
1857 // This scales the DC so that the printout roughly represents the
1858 // the screen scaling. The text point size _should_ be the right size
1859 // but in fact is too small for some reason. This is a detail that will
1860 // need to be addressed at some point but can be fudged for the
1862 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
1864 // Now we have to check in case our real page size is reduced
1865 // (e.g. because we're drawing to a print preview memory DC)
1866 int pageWidth
, pageHeight
;
1868 dc
->GetSize(&w
, &h
);
1869 GetPageSizePixels(&pageWidth
, &pageHeight
);
1871 // If printer pageWidth == current DC width, then this doesn't
1872 // change. But w might be the preview bitmap width, so scale down.
1873 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
1874 dc
->SetUserScale(overallScale
, overallScale
);
1878 m_printoutView
->OnDraw(dc
);
1883 bool wxDocPrintout::HasPage(int pageNum
)
1885 return (pageNum
== 1);
1888 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
1890 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
1896 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
1904 #endif // wxUSE_PRINTING_ARCHITECTURE
1906 // ----------------------------------------------------------------------------
1907 // File history processor
1908 // ----------------------------------------------------------------------------
1910 wxFileHistory::wxFileHistory(int maxFiles
)
1912 m_fileMaxFiles
= maxFiles
;
1914 m_fileHistory
= new wxChar
*[m_fileMaxFiles
];
1917 wxFileHistory::~wxFileHistory()
1920 for (i
= 0; i
< m_fileHistoryN
; i
++)
1921 delete[] m_fileHistory
[i
];
1922 delete[] m_fileHistory
;
1925 // File history management
1926 void wxFileHistory::AddFileToHistory(const wxString
& file
)
1930 // Check we don't already have this file
1931 for (i
= 0; i
< m_fileHistoryN
; i
++)
1933 if ( m_fileHistory
[i
] && (file
== m_fileHistory
[i
]) )
1935 // we do have it, move it to the top of the history
1936 RemoveFileFromHistory (i
);
1937 AddFileToHistory (file
);
1942 // if we already have a full history, delete the one at the end
1943 if ( m_fileMaxFiles
== m_fileHistoryN
)
1945 RemoveFileFromHistory (m_fileHistoryN
- 1);
1946 AddFileToHistory (file
);
1950 // Add to the project file history:
1951 // Move existing files (if any) down so we can insert file at beginning.
1952 if (m_fileHistoryN
< m_fileMaxFiles
)
1954 wxNode
* node
= m_fileMenus
.First();
1957 wxMenu
* menu
= (wxMenu
*) node
->Data();
1958 if (m_fileHistoryN
== 0)
1959 menu
->AppendSeparator();
1960 menu
->Append(wxID_FILE1
+m_fileHistoryN
, _("[EMPTY]"));
1961 node
= node
->Next();
1965 // Shuffle filenames down
1966 for (i
= (m_fileHistoryN
-1); i
> 0; i
--)
1968 m_fileHistory
[i
] = m_fileHistory
[i
-1];
1970 m_fileHistory
[0] = copystring(file
);
1972 // this is the directory of the last opened file
1973 wxString pathCurrent
;
1974 wxSplitPath( m_fileHistory
[0], &pathCurrent
, NULL
, NULL
);
1975 for (i
= 0; i
< m_fileHistoryN
; i
++)
1977 if ( m_fileHistory
[i
] )
1979 // if in same directory just show the filename; otherwise the full
1981 wxString pathInMenu
, path
, filename
, ext
;
1982 wxSplitPath( m_fileHistory
[i
], &path
, &filename
, &ext
);
1983 if ( path
== pathCurrent
)
1985 pathInMenu
= filename
;
1987 pathInMenu
= pathInMenu
+ wxFILE_SEP_EXT
+ ext
;
1991 // absolute path; could also set relative path
1992 pathInMenu
= m_fileHistory
[i
];
1996 buf
.Printf(s_MRUEntryFormat
, i
+ 1, pathInMenu
.c_str());
1997 wxNode
* node
= m_fileMenus
.First();
2000 wxMenu
* menu
= (wxMenu
*) node
->Data();
2001 menu
->SetLabel(wxID_FILE1
+ i
, buf
);
2002 node
= node
->Next();
2008 void wxFileHistory::RemoveFileFromHistory(int i
)
2010 wxCHECK_RET( i
< m_fileHistoryN
,
2011 wxT("invalid index in wxFileHistory::RemoveFileFromHistory") );
2013 // delete the element from the array (could use memmove() too...)
2014 delete [] m_fileHistory
[i
];
2017 for ( j
= i
; j
< m_fileHistoryN
- 1; j
++ )
2019 m_fileHistory
[j
] = m_fileHistory
[j
+ 1];
2022 wxNode
* node
= m_fileMenus
.First();
2025 wxMenu
* menu
= (wxMenu
*) node
->Data();
2028 // shuffle filenames up
2030 for ( j
= i
; j
< m_fileHistoryN
- 1; j
++ )
2032 buf
.Printf(s_MRUEntryFormat
, j
+ 1, m_fileHistory
[j
]);
2033 menu
->SetLabel(wxID_FILE1
+ j
, buf
);
2036 node
= node
->Next();
2038 // delete the last menu item which is unused now
2039 if (menu
->FindItem(wxID_FILE1
+ m_fileHistoryN
- 1))
2040 menu
->Delete(wxID_FILE1
+ m_fileHistoryN
- 1);
2042 // delete the last separator too if no more files are left
2043 if ( m_fileHistoryN
== 1 )
2045 wxMenuItemList::Node
*node
= menu
->GetMenuItems().GetLast();
2048 wxMenuItem
*menuItem
= node
->GetData();
2049 if ( menuItem
->IsSeparator() )
2051 menu
->Delete(menuItem
);
2053 //else: should we search backwards for the last separator?
2055 //else: menu is empty somehow
2062 wxString
wxFileHistory::GetHistoryFile(int i
) const
2065 if ( i
< m_fileHistoryN
)
2067 s
= m_fileHistory
[i
];
2071 wxFAIL_MSG( wxT("bad index in wxFileHistory::GetHistoryFile") );
2077 void wxFileHistory::UseMenu(wxMenu
*menu
)
2079 if (!m_fileMenus
.Member(menu
))
2080 m_fileMenus
.Append(menu
);
2083 void wxFileHistory::RemoveMenu(wxMenu
*menu
)
2085 m_fileMenus
.DeleteObject(menu
);
2089 void wxFileHistory::Load(wxConfigBase
& config
)
2093 buf
.Printf(wxT("file%d"), m_fileHistoryN
+1);
2094 wxString historyFile
;
2095 while ((m_fileHistoryN
< m_fileMaxFiles
) && config
.Read(buf
, &historyFile
) && (historyFile
!= wxT("")))
2097 m_fileHistory
[m_fileHistoryN
] = copystring((const wxChar
*) historyFile
);
2099 buf
.Printf(wxT("file%d"), m_fileHistoryN
+1);
2100 historyFile
= wxT("");
2105 void wxFileHistory::Save(wxConfigBase
& config
)
2108 for (i
= 0; i
< m_fileHistoryN
; i
++)
2111 buf
.Printf(wxT("file%d"), i
+1);
2112 config
.Write(buf
, wxString(m_fileHistory
[i
]));
2115 #endif // wxUSE_CONFIG
2117 void wxFileHistory::AddFilesToMenu()
2119 if (m_fileHistoryN
> 0)
2121 wxNode
* node
= m_fileMenus
.First();
2124 wxMenu
* menu
= (wxMenu
*) node
->Data();
2125 menu
->AppendSeparator();
2127 for (i
= 0; i
< m_fileHistoryN
; i
++)
2129 if (m_fileHistory
[i
])
2132 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
2133 menu
->Append(wxID_FILE1
+i
, buf
);
2136 node
= node
->Next();
2141 void wxFileHistory::AddFilesToMenu(wxMenu
* menu
)
2143 if (m_fileHistoryN
> 0)
2145 menu
->AppendSeparator();
2147 for (i
= 0; i
< m_fileHistoryN
; i
++)
2149 if (m_fileHistory
[i
])
2152 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
2153 menu
->Append(wxID_FILE1
+i
, buf
);
2159 // ----------------------------------------------------------------------------
2160 // Permits compatibility with existing file formats and functions that
2161 // manipulate files directly
2162 // ----------------------------------------------------------------------------
2164 #if wxUSE_STD_IOSTREAM
2165 bool wxTransferFileToStream(const wxString
& filename
, wxSTD ostream
& stream
)
2170 if ((fd1
= wxFopen (filename
.fn_str(), _T("rb"))) == NULL
)
2173 while ((ch
= getc (fd1
)) != EOF
)
2174 stream
<< (unsigned char)ch
;
2180 bool wxTransferStreamToFile(wxSTD istream
& stream
, const wxString
& filename
)
2185 if ((fd1
= wxFopen (filename
.fn_str(), _T("wb"))) == NULL
)
2190 while (!stream
.eof())
2200 bool wxTransferFileToStream(const wxString
& filename
, wxOutputStream
& stream
)
2205 if ((fd1
= wxFopen (filename
, wxT("rb"))) == NULL
)
2208 while ((ch
= getc (fd1
)) != EOF
)
2209 stream
.PutC((char) ch
);
2215 bool wxTransferStreamToFile(wxInputStream
& stream
, const wxString
& filename
)
2220 if ((fd1
= wxFopen (filename
, wxT("wb"))) == NULL
)
2225 int len
= stream
.GetSize();
2226 // TODO: is this the correct test for EOF?
2227 while (stream
.TellI() < (len
- 1))
2237 #endif // wxUSE_DOC_VIEW_ARCHITECTURE