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
.GetFirst();
187 wxView
*view
= (wxView
*)node
->GetData();
191 wxNode
*next
= node
->GetNext();
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
.GetCount() == 0)
207 return (wxView
*) NULL
;
208 return (wxView
*)m_documentViews
.GetFirst()->GetData();
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
.GetFirst();
281 wxView
*view
= (wxView
*)node
->GetData();
282 view
->OnChangeFilename();
283 node
= node
->GetNext();
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
.GetCount() == 0)
503 if (OnSaveModified())
510 void wxDocument::UpdateAllViews(wxView
*sender
, wxObject
*hint
)
512 wxNode
*node
= m_documentViews
.GetFirst();
515 wxView
*view
= (wxView
*)node
->GetData();
517 view
->OnUpdate(sender
, hint
);
518 node
= node
->GetNext();
522 void wxDocument::NotifyClosing()
524 wxNode
*node
= m_documentViews
.GetFirst();
527 wxView
*view
= (wxView
*)node
->GetData();
528 view
->OnClosingDocument();
529 node
= node
->GetNext();
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
.GetFirst();
542 wxView
*view
= (wxView
*)node
->GetData();
543 view
->OnChangeFilename();
544 node
= node
->GetNext();
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
.GetFirst();
780 wxDocument
*doc
= (wxDocument
*)node
->GetData();
781 wxNode
*next
= node
->GetNext();
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
.GetFirst();
810 wxDocTemplate
*templ
= (wxDocTemplate
*) node
->GetData();
811 wxNode
* next
= node
->GetNext();
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
.GetCount() == 1)
1034 wxDocument
* doc
= (wxDocument
*) m_docs
.GetFirst()->GetData();
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
.GetCount()];
1057 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1059 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Item(i
)->GetData());
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 ( (int)GetDocuments().GetCount() >= m_maxDocsOpen
)
1076 wxDocument
*doc
= (wxDocument
*)GetDocuments().GetFirst()->GetData();
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
.GetCount()];
1168 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1170 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Item(i
)->GetData());
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
1338 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1340 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Item(i
)->GetData();
1341 if ( temp
->FileMatchesTemplate(path
) )
1350 // Try to get a more suitable parent frame than the top window,
1351 // for selection dialogs. Otherwise you may get an unexpected
1352 // window being activated when a dialog is shown.
1353 static wxWindow
* wxFindSuitableParent()
1355 wxWindow
* parent
= wxTheApp
->GetTopWindow();
1357 wxWindow
* focusWindow
= wxWindow::FindFocus();
1360 while (focusWindow
&&
1361 !focusWindow
->IsKindOf(CLASSINFO(wxDialog
)) &&
1362 !focusWindow
->IsKindOf(CLASSINFO(wxFrame
)))
1364 focusWindow
= focusWindow
->GetParent();
1367 parent
= focusWindow
;
1372 // Prompts user to open a file, using file specs in templates.
1373 // How to implement in wxWindows? Must extend the file selector
1374 // dialog or implement own; OR match the extension to the
1375 // template extension.
1377 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**templates
,
1378 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
1381 int WXUNUSED(noTemplates
),
1384 long WXUNUSED(flags
),
1385 bool WXUNUSED(save
))
1387 // We can only have multiple filters in Windows and GTK
1388 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
1392 for (i
= 0; i
< noTemplates
; i
++)
1394 if (templates
[i
]->IsVisible())
1396 // add a '|' to separate this filter from the previous one
1397 if ( !descrBuf
.IsEmpty() )
1398 descrBuf
<< wxT('|');
1400 descrBuf
<< templates
[i
]->GetDescription()
1401 << wxT(" (") << templates
[i
]->GetFileFilter() << wxT(") |")
1402 << templates
[i
]->GetFileFilter();
1406 wxString descrBuf
= wxT("*.*");
1409 int FilterIndex
= -1;
1411 wxWindow
* parent
= wxFindSuitableParent();
1413 wxString pathTmp
= wxFileSelectorEx(_("Select a file"),
1421 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)NULL
;
1422 if (!pathTmp
.IsEmpty())
1424 if (!wxFileExists(pathTmp
))
1427 if (!wxTheApp
->GetAppName().IsEmpty())
1428 msgTitle
= wxTheApp
->GetAppName();
1430 msgTitle
= wxString(_("File error"));
1432 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
1436 return (wxDocTemplate
*) NULL
;
1438 m_lastDirectory
= wxPathOnly(pathTmp
);
1442 // first choose the template using the extension, if this fails (i.e.
1443 // wxFileSelectorEx() didn't fill it), then use the path
1444 if ( FilterIndex
!= -1 )
1445 theTemplate
= templates
[FilterIndex
];
1447 theTemplate
= FindTemplateForPath(path
);
1457 // In all other windowing systems, until we have more advanced
1458 // file selectors, we must select the document type (template) first, and
1459 // _then_ pop up the file selector.
1460 wxDocTemplate
*temp
= SelectDocumentType(templates
, noTemplates
);
1462 return (wxDocTemplate
*) NULL
;
1464 wxChar
*pathTmp
= wxFileSelector(_("Select a file"), wxT(""), wxT(""),
1465 temp
->GetDefaultExtension(),
1466 temp
->GetFileFilter(),
1467 0, wxTheApp
->GetTopWindow());
1475 return (wxDocTemplate
*) NULL
;
1479 wxDocTemplate
*wxDocManager::SelectDocumentType(wxDocTemplate
**templates
,
1480 int noTemplates
, bool sort
)
1482 wxArrayString
strings(sort
);
1483 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1487 for (i
= 0; i
< noTemplates
; i
++)
1489 if (templates
[i
]->IsVisible())
1493 for (j
= 0; j
< n
; j
++)
1495 //filter out NOT unique documents + view combinations
1496 if ( templates
[i
]->m_docTypeName
== data
[j
]->m_docTypeName
&&
1497 templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
1504 strings
.Add(templates
[i
]->m_description
);
1506 data
[n
] = templates
[i
];
1514 // Yes, this will be slow, but template lists
1515 // are typically short.
1517 n
= strings
.Count();
1518 for (i
= 0; i
< n
; i
++)
1520 for (j
= 0; j
< noTemplates
; j
++)
1522 if (strings
[i
] == templates
[j
]->m_description
)
1523 data
[i
] = templates
[j
];
1528 wxDocTemplate
*theTemplate
;
1533 // no visible templates, hence nothing to choose from
1538 // don't propose the user to choose if he heas no choice
1539 theTemplate
= data
[0];
1543 // propose the user to choose one of several
1544 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1546 _("Select a document template"),
1550 wxFindSuitableParent()
1559 wxDocTemplate
*wxDocManager::SelectViewType(wxDocTemplate
**templates
,
1560 int noTemplates
, bool sort
)
1562 wxArrayString
strings(sort
);
1563 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1567 for (i
= 0; i
< noTemplates
; i
++)
1569 wxDocTemplate
*templ
= templates
[i
];
1570 if ( templ
->IsVisible() && !templ
->GetViewName().empty() )
1574 for (j
= 0; j
< n
; j
++)
1576 //filter out NOT unique views
1577 if ( templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
)
1583 strings
.Add(templ
->m_viewTypeName
);
1592 // Yes, this will be slow, but template lists
1593 // are typically short.
1595 n
= strings
.Count();
1596 for (i
= 0; i
< n
; i
++)
1598 for (j
= 0; j
< noTemplates
; j
++)
1600 if (strings
[i
] == templates
[j
]->m_viewTypeName
)
1601 data
[i
] = templates
[j
];
1606 wxDocTemplate
*theTemplate
;
1608 // the same logic as above
1612 theTemplate
= (wxDocTemplate
*)NULL
;
1616 theTemplate
= data
[0];
1620 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1622 _("Select a document view"),
1626 wxFindSuitableParent()
1635 void wxDocManager::AssociateTemplate(wxDocTemplate
*temp
)
1637 if (!m_templates
.Member(temp
))
1638 m_templates
.Append(temp
);
1641 void wxDocManager::DisassociateTemplate(wxDocTemplate
*temp
)
1643 m_templates
.DeleteObject(temp
);
1646 // Add and remove a document from the manager's list
1647 void wxDocManager::AddDocument(wxDocument
*doc
)
1649 if (!m_docs
.Member(doc
))
1653 void wxDocManager::RemoveDocument(wxDocument
*doc
)
1655 m_docs
.DeleteObject(doc
);
1658 // Views or windows should inform the document manager
1659 // when a view is going in or out of focus
1660 void wxDocManager::ActivateView(wxView
*view
, bool activate
, bool WXUNUSED(deleting
))
1662 // If we're deactiving, and if we're not actually deleting the view, then
1663 // don't reset the current view because we may be going to
1664 // a window without a view.
1665 // WHAT DID I MEAN BY THAT EXACTLY?
1669 if (m_currentView == view)
1670 m_currentView = NULL;
1676 m_currentView
= view
;
1678 m_currentView
= (wxView
*) NULL
;
1682 // ----------------------------------------------------------------------------
1683 // Default document child frame
1684 // ----------------------------------------------------------------------------
1686 BEGIN_EVENT_TABLE(wxDocChildFrame
, wxFrame
)
1687 EVT_ACTIVATE(wxDocChildFrame::OnActivate
)
1688 EVT_CLOSE(wxDocChildFrame::OnCloseWindow
)
1691 wxDocChildFrame::wxDocChildFrame(wxDocument
*doc
,
1695 const wxString
& title
,
1699 const wxString
& name
)
1700 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1702 m_childDocument
= doc
;
1705 view
->SetFrame(this);
1708 wxDocChildFrame::~wxDocChildFrame()
1712 // Extend event processing to search the view's event table
1713 bool wxDocChildFrame::ProcessEvent(wxEvent
& event
)
1716 m_childView
->Activate(TRUE
);
1718 if ( !m_childView
|| ! m_childView
->ProcessEvent(event
) )
1720 // Only hand up to the parent if it's a menu command
1721 if (!event
.IsKindOf(CLASSINFO(wxCommandEvent
)) || !GetParent() || !GetParent()->ProcessEvent(event
))
1722 return wxEvtHandler::ProcessEvent(event
);
1730 void wxDocChildFrame::OnActivate(wxActivateEvent
& event
)
1732 wxFrame::OnActivate(event
);
1735 m_childView
->Activate(event
.GetActive());
1738 void wxDocChildFrame::OnCloseWindow(wxCloseEvent
& event
)
1743 if (!event
.CanVeto())
1744 ans
= TRUE
; // Must delete.
1746 ans
= m_childView
->Close(FALSE
); // FALSE means don't delete associated window
1750 m_childView
->Activate(FALSE
);
1752 m_childView
= (wxView
*) NULL
;
1753 m_childDocument
= (wxDocument
*) NULL
;
1764 // ----------------------------------------------------------------------------
1765 // Default parent frame
1766 // ----------------------------------------------------------------------------
1768 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1769 EVT_MENU(wxID_EXIT
, wxDocParentFrame::OnExit
)
1770 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE9
, wxDocParentFrame::OnMRUFile
)
1771 EVT_CLOSE(wxDocParentFrame::OnCloseWindow
)
1774 wxDocParentFrame::wxDocParentFrame(wxDocManager
*manager
,
1777 const wxString
& title
,
1781 const wxString
& name
)
1782 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1784 m_docManager
= manager
;
1787 void wxDocParentFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
1792 void wxDocParentFrame::OnMRUFile(wxCommandEvent
& event
)
1794 int n
= event
.GetId() - wxID_FILE1
; // the index in MRU list
1795 wxString
filename(m_docManager
->GetHistoryFile(n
));
1796 if ( !filename
.IsEmpty() )
1798 // verify that the file exists before doing anything else
1799 if ( wxFile::Exists(filename
) )
1802 (void)m_docManager
->CreateDocument(filename
, wxDOC_SILENT
);
1806 // remove the bogus filename from the MRU list and notify the user
1808 m_docManager
->RemoveFileFromHistory(n
);
1810 wxLogError(_("The file '%s' doesn't exist and couldn't be opened.\nIt has been removed from the most recently used files list."),
1816 // Extend event processing to search the view's event table
1817 bool wxDocParentFrame::ProcessEvent(wxEvent
& event
)
1819 // Try the document manager, then do default processing
1820 if (!m_docManager
|| !m_docManager
->ProcessEvent(event
))
1821 return wxEvtHandler::ProcessEvent(event
);
1826 // Define the behaviour for the frame closing
1827 // - must delete all frames except for the main one.
1828 void wxDocParentFrame::OnCloseWindow(wxCloseEvent
& event
)
1830 if (m_docManager
->Clear(!event
.CanVeto()))
1838 #if wxUSE_PRINTING_ARCHITECTURE
1840 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
)
1843 m_printoutView
= view
;
1846 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
1850 // Get the logical pixels per inch of screen and printer
1851 int ppiScreenX
, ppiScreenY
;
1852 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
1853 int ppiPrinterX
, ppiPrinterY
;
1854 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
1856 // This scales the DC so that the printout roughly represents the
1857 // the screen scaling. The text point size _should_ be the right size
1858 // but in fact is too small for some reason. This is a detail that will
1859 // need to be addressed at some point but can be fudged for the
1861 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
1863 // Now we have to check in case our real page size is reduced
1864 // (e.g. because we're drawing to a print preview memory DC)
1865 int pageWidth
, pageHeight
;
1867 dc
->GetSize(&w
, &h
);
1868 GetPageSizePixels(&pageWidth
, &pageHeight
);
1870 // If printer pageWidth == current DC width, then this doesn't
1871 // change. But w might be the preview bitmap width, so scale down.
1872 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
1873 dc
->SetUserScale(overallScale
, overallScale
);
1877 m_printoutView
->OnDraw(dc
);
1882 bool wxDocPrintout::HasPage(int pageNum
)
1884 return (pageNum
== 1);
1887 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
1889 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
1895 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
1903 #endif // wxUSE_PRINTING_ARCHITECTURE
1905 // ----------------------------------------------------------------------------
1906 // File history processor
1907 // ----------------------------------------------------------------------------
1909 wxFileHistory::wxFileHistory(int maxFiles
)
1911 m_fileMaxFiles
= maxFiles
;
1913 m_fileHistory
= new wxChar
*[m_fileMaxFiles
];
1916 wxFileHistory::~wxFileHistory()
1919 for (i
= 0; i
< m_fileHistoryN
; i
++)
1920 delete[] m_fileHistory
[i
];
1921 delete[] m_fileHistory
;
1924 // File history management
1925 void wxFileHistory::AddFileToHistory(const wxString
& file
)
1929 // Check we don't already have this file
1930 for (i
= 0; i
< m_fileHistoryN
; i
++)
1932 if ( m_fileHistory
[i
] && (file
== m_fileHistory
[i
]) )
1934 // we do have it, move it to the top of the history
1935 RemoveFileFromHistory (i
);
1936 AddFileToHistory (file
);
1941 // if we already have a full history, delete the one at the end
1942 if ( m_fileMaxFiles
== m_fileHistoryN
)
1944 RemoveFileFromHistory (m_fileHistoryN
- 1);
1945 AddFileToHistory (file
);
1949 // Add to the project file history:
1950 // Move existing files (if any) down so we can insert file at beginning.
1951 if (m_fileHistoryN
< m_fileMaxFiles
)
1953 wxNode
* node
= m_fileMenus
.GetFirst();
1956 wxMenu
* menu
= (wxMenu
*) node
->GetData();
1957 if ( m_fileHistoryN
== 0 && menu
->GetMenuItemCount() )
1959 menu
->AppendSeparator();
1961 menu
->Append(wxID_FILE1
+m_fileHistoryN
, _("[EMPTY]"));
1962 node
= node
->GetNext();
1966 // Shuffle filenames down
1967 for (i
= (m_fileHistoryN
-1); i
> 0; i
--)
1969 m_fileHistory
[i
] = m_fileHistory
[i
-1];
1971 m_fileHistory
[0] = copystring(file
);
1973 // this is the directory of the last opened file
1974 wxString pathCurrent
;
1975 wxSplitPath( m_fileHistory
[0], &pathCurrent
, NULL
, NULL
);
1976 for (i
= 0; i
< m_fileHistoryN
; i
++)
1978 if ( m_fileHistory
[i
] )
1980 // if in same directory just show the filename; otherwise the full
1982 wxString pathInMenu
, path
, filename
, ext
;
1983 wxSplitPath( m_fileHistory
[i
], &path
, &filename
, &ext
);
1984 if ( path
== pathCurrent
)
1986 pathInMenu
= filename
;
1988 pathInMenu
= pathInMenu
+ wxFILE_SEP_EXT
+ ext
;
1992 // absolute path; could also set relative path
1993 pathInMenu
= m_fileHistory
[i
];
1997 buf
.Printf(s_MRUEntryFormat
, i
+ 1, pathInMenu
.c_str());
1998 wxNode
* node
= m_fileMenus
.GetFirst();
2001 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2002 menu
->SetLabel(wxID_FILE1
+ i
, buf
);
2003 node
= node
->GetNext();
2009 void wxFileHistory::RemoveFileFromHistory(int i
)
2011 wxCHECK_RET( i
< m_fileHistoryN
,
2012 wxT("invalid index in wxFileHistory::RemoveFileFromHistory") );
2014 // delete the element from the array (could use memmove() too...)
2015 delete [] m_fileHistory
[i
];
2018 for ( j
= i
; j
< m_fileHistoryN
- 1; j
++ )
2020 m_fileHistory
[j
] = m_fileHistory
[j
+ 1];
2023 wxNode
* node
= m_fileMenus
.GetFirst();
2026 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2029 // shuffle filenames up
2031 for ( j
= i
; j
< m_fileHistoryN
- 1; j
++ )
2033 buf
.Printf(s_MRUEntryFormat
, j
+ 1, m_fileHistory
[j
]);
2034 menu
->SetLabel(wxID_FILE1
+ j
, buf
);
2037 node
= node
->GetNext();
2039 // delete the last menu item which is unused now
2040 if (menu
->FindItem(wxID_FILE1
+ m_fileHistoryN
- 1))
2041 menu
->Delete(wxID_FILE1
+ m_fileHistoryN
- 1);
2043 // delete the last separator too if no more files are left
2044 if ( m_fileHistoryN
== 1 )
2046 wxMenuItemList::Node
*node
= menu
->GetMenuItems().GetLast();
2049 wxMenuItem
*menuItem
= node
->GetData();
2050 if ( menuItem
->IsSeparator() )
2052 menu
->Delete(menuItem
);
2054 //else: should we search backwards for the last separator?
2056 //else: menu is empty somehow
2063 wxString
wxFileHistory::GetHistoryFile(int i
) const
2066 if ( i
< m_fileHistoryN
)
2068 s
= m_fileHistory
[i
];
2072 wxFAIL_MSG( wxT("bad index in wxFileHistory::GetHistoryFile") );
2078 void wxFileHistory::UseMenu(wxMenu
*menu
)
2080 if (!m_fileMenus
.Member(menu
))
2081 m_fileMenus
.Append(menu
);
2084 void wxFileHistory::RemoveMenu(wxMenu
*menu
)
2086 m_fileMenus
.DeleteObject(menu
);
2090 void wxFileHistory::Load(wxConfigBase
& config
)
2094 buf
.Printf(wxT("file%d"), m_fileHistoryN
+1);
2095 wxString historyFile
;
2096 while ((m_fileHistoryN
< m_fileMaxFiles
) && config
.Read(buf
, &historyFile
) && (historyFile
!= wxT("")))
2098 m_fileHistory
[m_fileHistoryN
] = copystring((const wxChar
*) historyFile
);
2100 buf
.Printf(wxT("file%d"), m_fileHistoryN
+1);
2101 historyFile
= wxT("");
2106 void wxFileHistory::Save(wxConfigBase
& config
)
2109 for (i
= 0; i
< m_fileHistoryN
; i
++)
2112 buf
.Printf(wxT("file%d"), i
+1);
2113 config
.Write(buf
, wxString(m_fileHistory
[i
]));
2116 #endif // wxUSE_CONFIG
2118 void wxFileHistory::AddFilesToMenu()
2120 if (m_fileHistoryN
> 0)
2122 wxNode
* node
= m_fileMenus
.GetFirst();
2125 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2126 if (menu
->GetMenuItemCount())
2128 menu
->AppendSeparator();
2132 for (i
= 0; i
< m_fileHistoryN
; i
++)
2134 if (m_fileHistory
[i
])
2137 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
2138 menu
->Append(wxID_FILE1
+i
, buf
);
2141 node
= node
->GetNext();
2146 void wxFileHistory::AddFilesToMenu(wxMenu
* menu
)
2148 if (m_fileHistoryN
> 0)
2150 if (menu
->GetMenuItemCount())
2152 menu
->AppendSeparator();
2156 for (i
= 0; i
< m_fileHistoryN
; i
++)
2158 if (m_fileHistory
[i
])
2161 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
2162 menu
->Append(wxID_FILE1
+i
, buf
);
2168 // ----------------------------------------------------------------------------
2169 // Permits compatibility with existing file formats and functions that
2170 // manipulate files directly
2171 // ----------------------------------------------------------------------------
2173 #if wxUSE_STD_IOSTREAM
2174 bool wxTransferFileToStream(const wxString
& filename
, wxSTD ostream
& stream
)
2179 if ((fd1
= wxFopen (filename
.fn_str(), _T("rb"))) == NULL
)
2182 while ((ch
= getc (fd1
)) != EOF
)
2183 stream
<< (unsigned char)ch
;
2189 bool wxTransferStreamToFile(wxSTD istream
& stream
, const wxString
& filename
)
2194 if ((fd1
= wxFopen (filename
.fn_str(), _T("wb"))) == NULL
)
2199 while (!stream
.eof())
2209 bool wxTransferFileToStream(const wxString
& filename
, wxOutputStream
& stream
)
2214 if ((fd1
= wxFopen (filename
, wxT("rb"))) == NULL
)
2217 while ((ch
= getc (fd1
)) != EOF
)
2218 stream
.PutC((char) ch
);
2224 bool wxTransferStreamToFile(wxInputStream
& stream
, const wxString
& filename
)
2229 if ((fd1
= wxFopen (filename
, wxT("wb"))) == NULL
)
2234 int len
= stream
.GetSize();
2235 // TODO: is this the correct test for EOF?
2236 while (stream
.TellI() < (len
- 1))
2246 #endif // wxUSE_DOC_VIEW_ARCHITECTURE