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(""))
265 fileName
+= 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( file
);
302 if (store
.GetLastError() != wxSTREAM_NO_ERROR
)
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( file
);
339 if (store
.GetLastError() != wxSTREAM_NO_ERROR
)
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
).GetLastError();
351 if ((res
!= wxSTREAM_NO_ERROR
) &&
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();
510 view
->OnUpdate(sender
, hint
);
515 void wxDocument::NotifyClosing()
517 wxNode
*node
= m_documentViews
.First();
520 wxView
*view
= (wxView
*)node
->Data();
521 view
->OnClosingDocument();
526 void wxDocument::SetFilename(const wxString
& filename
, bool notifyViews
)
528 m_documentFile
= filename
;
531 // Notify the views that the filename has changed
532 wxNode
*node
= m_documentViews
.First();
535 wxView
*view
= (wxView
*)node
->Data();
536 view
->OnChangeFilename();
542 // ----------------------------------------------------------------------------
544 // ----------------------------------------------------------------------------
549 m_viewDocument
= (wxDocument
*) NULL
;
551 m_viewTypeName
= wxT("");
552 m_viewFrame
= (wxFrame
*) NULL
;
557 // GetDocumentManager()->ActivateView(this, FALSE, TRUE);
558 m_viewDocument
->RemoveView(this);
561 // Extend event processing to search the document's event table
562 bool wxView::ProcessEvent(wxEvent
& event
)
564 if ( !GetDocument() || !GetDocument()->ProcessEvent(event
) )
565 return wxEvtHandler::ProcessEvent(event
);
570 void wxView::OnActivateView(bool WXUNUSED(activate
), wxView
*WXUNUSED(activeView
), wxView
*WXUNUSED(deactiveView
))
574 void wxView::OnPrint(wxDC
*dc
, wxObject
*WXUNUSED(info
))
579 void wxView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
))
583 void wxView::OnChangeFilename()
585 if (GetFrame() && GetDocument())
589 GetDocument()->GetPrintableName(title
);
591 GetFrame()->SetTitle(title
);
595 void wxView::SetDocument(wxDocument
*doc
)
597 m_viewDocument
= doc
;
602 bool wxView::Close(bool deleteWindow
)
604 if (OnClose(deleteWindow
))
610 void wxView::Activate(bool activate
)
612 if (GetDocument() && GetDocumentManager())
614 OnActivateView(activate
, this, GetDocumentManager()->GetCurrentView());
615 GetDocumentManager()->ActivateView(this, activate
);
619 bool wxView::OnClose(bool WXUNUSED(deleteWindow
))
621 return GetDocument() ? GetDocument()->Close() : TRUE
;
624 #if wxUSE_PRINTING_ARCHITECTURE
625 wxPrintout
*wxView::OnCreatePrintout()
627 return new wxDocPrintout(this);
629 #endif // wxUSE_PRINTING_ARCHITECTURE
631 // ----------------------------------------------------------------------------
633 // ----------------------------------------------------------------------------
635 wxDocTemplate::wxDocTemplate(wxDocManager
*manager
,
636 const wxString
& descr
,
637 const wxString
& filter
,
640 const wxString
& docTypeName
,
641 const wxString
& viewTypeName
,
642 wxClassInfo
*docClassInfo
,
643 wxClassInfo
*viewClassInfo
,
646 m_documentManager
= manager
;
647 m_description
= descr
;
650 m_fileFilter
= filter
;
652 m_docTypeName
= docTypeName
;
653 m_viewTypeName
= viewTypeName
;
654 m_documentManager
->AssociateTemplate(this);
656 m_docClassInfo
= docClassInfo
;
657 m_viewClassInfo
= viewClassInfo
;
660 wxDocTemplate::~wxDocTemplate()
662 m_documentManager
->DisassociateTemplate(this);
665 // Tries to dynamically construct an object of the right class.
666 wxDocument
*wxDocTemplate::CreateDocument(const wxString
& path
, long flags
)
669 return (wxDocument
*) NULL
;
670 wxDocument
*doc
= (wxDocument
*)m_docClassInfo
->CreateObject();
671 doc
->SetFilename(path
);
672 doc
->SetDocumentTemplate(this);
673 GetDocumentManager()->AddDocument(doc
);
674 doc
->SetCommandProcessor(doc
->OnCreateCommandProcessor());
676 if (doc
->OnCreate(path
, flags
))
680 if (GetDocumentManager()->GetDocuments().Member(doc
))
681 doc
->DeleteAllViews();
682 return (wxDocument
*) NULL
;
686 wxView
*wxDocTemplate::CreateView(wxDocument
*doc
, long flags
)
688 if (!m_viewClassInfo
)
689 return (wxView
*) NULL
;
690 wxView
*view
= (wxView
*)m_viewClassInfo
->CreateObject();
691 view
->SetDocument(doc
);
692 if (view
->OnCreate(doc
, flags
))
699 return (wxView
*) NULL
;
703 // The default (very primitive) format detection: check is the extension is
704 // that of the template
705 bool wxDocTemplate::FileMatchesTemplate(const wxString
& path
)
707 return GetDefaultExtension().IsSameAs(FindExtension(path
));
710 // ----------------------------------------------------------------------------
712 // ----------------------------------------------------------------------------
714 BEGIN_EVENT_TABLE(wxDocManager
, wxEvtHandler
)
715 EVT_MENU(wxID_OPEN
, wxDocManager::OnFileOpen
)
716 EVT_MENU(wxID_CLOSE
, wxDocManager::OnFileClose
)
717 EVT_MENU(wxID_CLOSE_ALL
, wxDocManager::OnFileCloseAll
)
718 EVT_MENU(wxID_REVERT
, wxDocManager::OnFileRevert
)
719 EVT_MENU(wxID_NEW
, wxDocManager::OnFileNew
)
720 EVT_MENU(wxID_SAVE
, wxDocManager::OnFileSave
)
721 EVT_MENU(wxID_SAVEAS
, wxDocManager::OnFileSaveAs
)
722 EVT_MENU(wxID_UNDO
, wxDocManager::OnUndo
)
723 EVT_MENU(wxID_REDO
, wxDocManager::OnRedo
)
725 EVT_UPDATE_UI(wxID_OPEN
, wxDocManager::OnUpdateFileOpen
)
726 EVT_UPDATE_UI(wxID_CLOSE
, wxDocManager::OnUpdateFileClose
)
727 EVT_UPDATE_UI(wxID_CLOSE_ALL
, wxDocManager::OnUpdateFileClose
)
728 EVT_UPDATE_UI(wxID_REVERT
, wxDocManager::OnUpdateFileRevert
)
729 EVT_UPDATE_UI(wxID_NEW
, wxDocManager::OnUpdateFileNew
)
730 EVT_UPDATE_UI(wxID_SAVE
, wxDocManager::OnUpdateFileSave
)
731 EVT_UPDATE_UI(wxID_SAVEAS
, wxDocManager::OnUpdateFileSaveAs
)
732 EVT_UPDATE_UI(wxID_UNDO
, wxDocManager::OnUpdateUndo
)
733 EVT_UPDATE_UI(wxID_REDO
, wxDocManager::OnUpdateRedo
)
735 #if wxUSE_PRINTING_ARCHITECTURE
736 EVT_MENU(wxID_PRINT
, wxDocManager::OnPrint
)
737 EVT_MENU(wxID_PRINT_SETUP
, wxDocManager::OnPrintSetup
)
738 EVT_MENU(wxID_PREVIEW
, wxDocManager::OnPreview
)
740 EVT_UPDATE_UI(wxID_PRINT
, wxDocManager::OnUpdatePrint
)
741 EVT_UPDATE_UI(wxID_PRINT_SETUP
, wxDocManager::OnUpdatePrintSetup
)
742 EVT_UPDATE_UI(wxID_PREVIEW
, wxDocManager::OnUpdatePreview
)
746 wxDocManager
* wxDocManager::sm_docManager
= (wxDocManager
*) NULL
;
748 wxDocManager::wxDocManager(long flags
, bool initialize
)
750 m_defaultDocumentNameCounter
= 1;
752 m_currentView
= (wxView
*) NULL
;
753 m_maxDocsOpen
= 10000;
754 m_fileHistory
= (wxFileHistory
*) NULL
;
757 sm_docManager
= this;
760 wxDocManager::~wxDocManager()
764 delete m_fileHistory
;
765 sm_docManager
= (wxDocManager
*) NULL
;
768 bool wxDocManager::CloseDocuments(bool force
)
770 wxNode
*node
= m_docs
.First();
773 wxDocument
*doc
= (wxDocument
*)node
->Data();
774 wxNode
*next
= node
->Next();
776 if (!doc
->Close() && !force
)
779 // Implicitly deletes the document when the last
780 // view is removed (deleted)
781 doc
->DeleteAllViews();
783 // Check document is deleted
784 if (m_docs
.Member(doc
))
787 // This assumes that documents are not connected in
788 // any way, i.e. deleting one document does NOT
795 bool wxDocManager::Clear(bool force
)
797 if (!CloseDocuments(force
))
800 wxNode
*node
= m_templates
.First();
803 wxDocTemplate
*templ
= (wxDocTemplate
*) node
->Data();
804 wxNode
* next
= node
->Next();
811 bool wxDocManager::Initialize()
813 m_fileHistory
= OnCreateFileHistory();
817 wxFileHistory
*wxDocManager::OnCreateFileHistory()
819 return new wxFileHistory
;
822 void wxDocManager::OnFileClose(wxCommandEvent
& WXUNUSED(event
))
824 wxDocument
*doc
= GetCurrentDocument();
829 doc
->DeleteAllViews();
830 if (m_docs
.Member(doc
))
835 void wxDocManager::OnFileCloseAll(wxCommandEvent
& WXUNUSED(event
))
837 CloseDocuments(FALSE
);
840 void wxDocManager::OnFileNew(wxCommandEvent
& WXUNUSED(event
))
842 CreateDocument( wxT(""), wxDOC_NEW
);
845 void wxDocManager::OnFileOpen(wxCommandEvent
& WXUNUSED(event
))
847 if ( !CreateDocument( wxT(""), 0) )
853 void wxDocManager::OnFileRevert(wxCommandEvent
& WXUNUSED(event
))
855 wxDocument
*doc
= GetCurrentDocument();
861 void wxDocManager::OnFileSave(wxCommandEvent
& WXUNUSED(event
))
863 wxDocument
*doc
= GetCurrentDocument();
869 void wxDocManager::OnFileSaveAs(wxCommandEvent
& WXUNUSED(event
))
871 wxDocument
*doc
= GetCurrentDocument();
877 void wxDocManager::OnPrint(wxCommandEvent
& WXUNUSED(event
))
879 #if wxUSE_PRINTING_ARCHITECTURE
880 wxView
*view
= GetCurrentView();
884 wxPrintout
*printout
= view
->OnCreatePrintout();
888 printer
.Print(view
->GetFrame(), printout
, TRUE
);
892 #endif // wxUSE_PRINTING_ARCHITECTURE
895 void wxDocManager::OnPrintSetup(wxCommandEvent
& WXUNUSED(event
))
897 #if wxUSE_PRINTING_ARCHITECTURE
898 wxWindow
*parentWin
= wxTheApp
->GetTopWindow();
899 wxView
*view
= GetCurrentView();
901 parentWin
= view
->GetFrame();
903 wxPrintDialogData data
;
905 wxPrintDialog
printerDialog(parentWin
, &data
);
906 printerDialog
.GetPrintDialogData().SetSetupDialog(TRUE
);
907 printerDialog
.ShowModal();
908 #endif // wxUSE_PRINTING_ARCHITECTURE
911 void wxDocManager::OnPreview(wxCommandEvent
& WXUNUSED(event
))
913 #if wxUSE_PRINTING_ARCHITECTURE
914 wxView
*view
= GetCurrentView();
918 wxPrintout
*printout
= view
->OnCreatePrintout();
921 // Pass two printout objects: for preview, and possible printing.
922 wxPrintPreviewBase
*preview
= (wxPrintPreviewBase
*) NULL
;
923 preview
= new wxPrintPreview(printout
, view
->OnCreatePrintout());
925 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, (wxFrame
*)wxTheApp
->GetTopWindow(), _("Print Preview"),
926 wxPoint(100, 100), wxSize(600, 650));
927 frame
->Centre(wxBOTH
);
931 #endif // wxUSE_PRINTING_ARCHITECTURE
934 void wxDocManager::OnUndo(wxCommandEvent
& WXUNUSED(event
))
936 wxDocument
*doc
= GetCurrentDocument();
939 if (doc
->GetCommandProcessor())
940 doc
->GetCommandProcessor()->Undo();
943 void wxDocManager::OnRedo(wxCommandEvent
& WXUNUSED(event
))
945 wxDocument
*doc
= GetCurrentDocument();
948 if (doc
->GetCommandProcessor())
949 doc
->GetCommandProcessor()->Redo();
952 // Handlers for UI update commands
954 void wxDocManager::OnUpdateFileOpen(wxUpdateUIEvent
& event
)
956 event
.Enable( TRUE
);
959 void wxDocManager::OnUpdateFileClose(wxUpdateUIEvent
& event
)
961 wxDocument
*doc
= GetCurrentDocument();
962 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
965 void wxDocManager::OnUpdateFileRevert(wxUpdateUIEvent
& event
)
967 wxDocument
*doc
= GetCurrentDocument();
968 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
971 void wxDocManager::OnUpdateFileNew(wxUpdateUIEvent
& event
)
973 event
.Enable( TRUE
);
976 void wxDocManager::OnUpdateFileSave(wxUpdateUIEvent
& event
)
978 wxDocument
*doc
= GetCurrentDocument();
979 event
.Enable( doc
&& doc
->IsModified() );
982 void wxDocManager::OnUpdateFileSaveAs(wxUpdateUIEvent
& event
)
984 wxDocument
*doc
= GetCurrentDocument();
985 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
988 void wxDocManager::OnUpdateUndo(wxUpdateUIEvent
& event
)
990 wxDocument
*doc
= GetCurrentDocument();
991 event
.Enable( (doc
&& doc
->GetCommandProcessor() && doc
->GetCommandProcessor()->CanUndo()) );
992 if (doc
&& doc
->GetCommandProcessor())
993 doc
->GetCommandProcessor()->SetMenuStrings();
996 void wxDocManager::OnUpdateRedo(wxUpdateUIEvent
& event
)
998 wxDocument
*doc
= GetCurrentDocument();
999 event
.Enable( (doc
&& doc
->GetCommandProcessor() && doc
->GetCommandProcessor()->CanRedo()) );
1000 if (doc
&& doc
->GetCommandProcessor())
1001 doc
->GetCommandProcessor()->SetMenuStrings();
1004 void wxDocManager::OnUpdatePrint(wxUpdateUIEvent
& event
)
1006 wxDocument
*doc
= GetCurrentDocument();
1007 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1010 void wxDocManager::OnUpdatePrintSetup(wxUpdateUIEvent
& event
)
1012 event
.Enable( TRUE
);
1015 void wxDocManager::OnUpdatePreview(wxUpdateUIEvent
& event
)
1017 wxDocument
*doc
= GetCurrentDocument();
1018 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1021 wxView
*wxDocManager::GetCurrentView() const
1024 return m_currentView
;
1025 if (m_docs
.Number() == 1)
1027 wxDocument
* doc
= (wxDocument
*) m_docs
.First()->Data();
1028 return doc
->GetFirstView();
1030 return (wxView
*) NULL
;
1033 // Extend event processing to search the view's event table
1034 bool wxDocManager::ProcessEvent(wxEvent
& event
)
1036 wxView
* view
= GetCurrentView();
1039 if (view
->ProcessEvent(event
))
1042 return wxEvtHandler::ProcessEvent(event
);
1045 wxDocument
*wxDocManager::CreateDocument(const wxString
& path
, long flags
)
1047 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.Number()];
1050 for (i
= 0; i
< m_templates
.Number(); i
++)
1052 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Nth(i
)->Data());
1053 if (temp
->IsVisible())
1055 templates
[n
] = temp
;
1062 return (wxDocument
*) NULL
;
1065 // If we've reached the max number of docs, close the
1067 if (GetDocuments().Number() >= m_maxDocsOpen
)
1069 wxDocument
*doc
= (wxDocument
*)GetDocuments().First()->Data();
1072 // Implicitly deletes the document when
1073 // the last view is deleted
1074 doc
->DeleteAllViews();
1076 // Check we're really deleted
1077 if (m_docs
.Member(doc
))
1083 return (wxDocument
*) NULL
;
1087 // New document: user chooses a template, unless there's only one.
1088 if (flags
& wxDOC_NEW
)
1092 wxDocTemplate
*temp
= templates
[0];
1094 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
1097 newDoc
->SetDocumentName(temp
->GetDocumentName());
1098 newDoc
->SetDocumentTemplate(temp
);
1099 newDoc
->OnNewDocument();
1104 wxDocTemplate
*temp
= SelectDocumentType(templates
, n
);
1108 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
1111 newDoc
->SetDocumentName(temp
->GetDocumentName());
1112 newDoc
->SetDocumentTemplate(temp
);
1113 newDoc
->OnNewDocument();
1118 return (wxDocument
*) NULL
;
1121 // Existing document
1122 wxDocTemplate
*temp
= (wxDocTemplate
*) NULL
;
1124 wxString
path2(wxT(""));
1125 if (path
!= wxT(""))
1128 if (flags
& wxDOC_SILENT
)
1129 temp
= FindTemplateForPath(path2
);
1131 temp
= SelectDocumentPath(templates
, n
, path2
, flags
);
1137 wxDocument
*newDoc
= temp
->CreateDocument(path2
, flags
);
1140 newDoc
->SetDocumentName(temp
->GetDocumentName());
1141 newDoc
->SetDocumentTemplate(temp
);
1142 if (!newDoc
->OnOpenDocument(path2
))
1144 newDoc
->DeleteAllViews();
1145 // delete newDoc; // Implicitly deleted by DeleteAllViews
1146 return (wxDocument
*) NULL
;
1148 AddFileToHistory(path2
);
1153 return (wxDocument
*) NULL
;
1156 wxView
*wxDocManager::CreateView(wxDocument
*doc
, long flags
)
1158 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.Number()];
1161 for (i
= 0; i
< m_templates
.Number(); i
++)
1163 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Nth(i
)->Data());
1164 if (temp
->IsVisible())
1166 if (temp
->GetDocumentName() == doc
->GetDocumentName())
1168 templates
[n
] = temp
;
1176 return (wxView
*) NULL
;
1180 wxDocTemplate
*temp
= templates
[0];
1182 wxView
*view
= temp
->CreateView(doc
, flags
);
1184 view
->SetViewName(temp
->GetViewName());
1188 wxDocTemplate
*temp
= SelectViewType(templates
, n
);
1192 wxView
*view
= temp
->CreateView(doc
, flags
);
1194 view
->SetViewName(temp
->GetViewName());
1198 return (wxView
*) NULL
;
1201 // Not yet implemented
1202 void wxDocManager::DeleteTemplate(wxDocTemplate
*WXUNUSED(temp
), long WXUNUSED(flags
))
1206 // Not yet implemented
1207 bool wxDocManager::FlushDoc(wxDocument
*WXUNUSED(doc
))
1212 wxDocument
*wxDocManager::GetCurrentDocument() const
1214 wxView
*view
= GetCurrentView();
1216 return view
->GetDocument();
1218 return (wxDocument
*) NULL
;
1221 // Make a default document name
1222 bool wxDocManager::MakeDefaultName(wxString
& name
)
1224 name
.Printf(_("unnamed%d"), m_defaultDocumentNameCounter
);
1225 m_defaultDocumentNameCounter
++;
1230 // Make a frame title (override this to do something different)
1231 // If docName is empty, a document is not currently active.
1232 wxString
wxDocManager::MakeFrameTitle(wxDocument
* doc
)
1234 wxString appName
= wxTheApp
->GetAppName();
1241 doc
->GetPrintableName(docName
);
1242 title
= docName
+ wxString(_(" - ")) + appName
;
1248 // Not yet implemented
1249 wxDocTemplate
*wxDocManager::MatchTemplate(const wxString
& WXUNUSED(path
))
1251 return (wxDocTemplate
*) NULL
;
1254 // File history management
1255 void wxDocManager::AddFileToHistory(const wxString
& file
)
1258 m_fileHistory
->AddFileToHistory(file
);
1261 void wxDocManager::RemoveFileFromHistory(int i
)
1264 m_fileHistory
->RemoveFileFromHistory(i
);
1267 wxString
wxDocManager::GetHistoryFile(int i
) const
1272 histFile
= m_fileHistory
->GetHistoryFile(i
);
1277 void wxDocManager::FileHistoryUseMenu(wxMenu
*menu
)
1280 m_fileHistory
->UseMenu(menu
);
1283 void wxDocManager::FileHistoryRemoveMenu(wxMenu
*menu
)
1286 m_fileHistory
->RemoveMenu(menu
);
1290 void wxDocManager::FileHistoryLoad(wxConfigBase
& config
)
1293 m_fileHistory
->Load(config
);
1296 void wxDocManager::FileHistorySave(wxConfigBase
& config
)
1299 m_fileHistory
->Save(config
);
1303 void wxDocManager::FileHistoryAddFilesToMenu(wxMenu
* menu
)
1306 m_fileHistory
->AddFilesToMenu(menu
);
1309 void wxDocManager::FileHistoryAddFilesToMenu()
1312 m_fileHistory
->AddFilesToMenu();
1315 int wxDocManager::GetNoHistoryFiles() const
1318 return m_fileHistory
->GetNoHistoryFiles();
1324 // Find out the document template via matching in the document file format
1325 // against that of the template
1326 wxDocTemplate
*wxDocManager::FindTemplateForPath(const wxString
& path
)
1328 wxDocTemplate
*theTemplate
= (wxDocTemplate
*) NULL
;
1330 // Find the template which this extension corresponds to
1332 for (i
= 0; i
< m_templates
.Number(); i
++)
1334 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Nth(i
)->Data();
1335 if ( temp
->FileMatchesTemplate(path
) )
1344 // Try to get a more suitable parent frame than the top window,
1345 // for selection dialogs. Otherwise you may get an unexpected
1346 // window being activated when a dialog is shown.
1347 static wxWindow
* wxFindSuitableParent()
1349 wxWindow
* parent
= wxTheApp
->GetTopWindow();
1351 wxWindow
* focusWindow
= wxWindow::FindFocus();
1354 while (focusWindow
&&
1355 !focusWindow
->IsKindOf(CLASSINFO(wxDialog
)) &&
1356 !focusWindow
->IsKindOf(CLASSINFO(wxFrame
)))
1358 focusWindow
= focusWindow
->GetParent();
1361 parent
= focusWindow
;
1366 // Prompts user to open a file, using file specs in templates.
1367 // How to implement in wxWindows? Must extend the file selector
1368 // dialog or implement own; OR match the extension to the
1369 // template extension.
1371 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**templates
,
1372 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
1375 int WXUNUSED(noTemplates
),
1378 long WXUNUSED(flags
),
1379 bool WXUNUSED(save
))
1381 // We can only have multiple filters in Windows and GTK
1382 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
1386 for (i
= 0; i
< noTemplates
; i
++)
1388 if (templates
[i
]->IsVisible())
1390 // add a '|' to separate this filter from the previous one
1391 if ( !descrBuf
.IsEmpty() )
1392 descrBuf
<< wxT('|');
1394 descrBuf
<< templates
[i
]->GetDescription()
1395 << wxT(" (") << templates
[i
]->GetFileFilter() << wxT(") |")
1396 << templates
[i
]->GetFileFilter();
1400 wxString descrBuf
= wxT("*.*");
1403 int FilterIndex
= -1;
1405 wxWindow
* parent
= wxFindSuitableParent();
1407 wxString pathTmp
= wxFileSelectorEx(_("Select a file"),
1415 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)NULL
;
1416 if (!pathTmp
.IsEmpty())
1418 if (!wxFileExists(pathTmp
))
1421 if (!wxTheApp
->GetAppName().IsEmpty())
1422 msgTitle
= wxTheApp
->GetAppName();
1424 msgTitle
= wxString(_("File error"));
1426 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
1430 return (wxDocTemplate
*) NULL
;
1432 m_lastDirectory
= wxPathOnly(pathTmp
);
1436 // first choose the template using the extension, if this fails (i.e.
1437 // wxFileSelectorEx() didn't fill it), then use the path
1438 if ( FilterIndex
!= -1 )
1439 theTemplate
= templates
[FilterIndex
];
1441 theTemplate
= FindTemplateForPath(path
);
1451 // In all other windowing systems, until we have more advanced
1452 // file selectors, we must select the document type (template) first, and
1453 // _then_ pop up the file selector.
1454 wxDocTemplate
*temp
= SelectDocumentType(templates
, noTemplates
);
1456 return (wxDocTemplate
*) NULL
;
1458 wxChar
*pathTmp
= wxFileSelector(_("Select a file"), wxT(""), wxT(""),
1459 temp
->GetDefaultExtension(),
1460 temp
->GetFileFilter(),
1461 0, wxTheApp
->GetTopWindow());
1469 return (wxDocTemplate
*) NULL
;
1473 wxDocTemplate
*wxDocManager::SelectDocumentType(wxDocTemplate
**templates
,
1474 int noTemplates
, bool sort
)
1476 wxArrayString
strings(sort
);
1477 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1481 for (i
= 0; i
< noTemplates
; i
++)
1483 if (templates
[i
]->IsVisible())
1487 for (j
= 0; j
< n
; j
++)
1489 //filter out NOT unique documents + view combinations
1490 if ( templates
[i
]->m_docTypeName
== data
[j
]->m_docTypeName
&&
1491 templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
1498 strings
.Add(templates
[i
]->m_description
);
1500 data
[n
] = templates
[i
];
1508 // Yes, this will be slow, but template lists
1509 // are typically short.
1511 n
= strings
.Count();
1512 for (i
= 0; i
< n
; i
++)
1514 for (j
= 0; j
< noTemplates
; j
++)
1516 if (strings
[i
] == templates
[j
]->m_description
)
1517 data
[i
] = templates
[j
];
1522 wxDocTemplate
*theTemplate
;
1527 // no visible templates, hence nothing to choose from
1532 // don't propose the user to choose if he heas no choice
1533 theTemplate
= data
[0];
1537 // propose the user to choose one of several
1538 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1540 _("Select a document template"),
1544 wxFindSuitableParent()
1553 wxDocTemplate
*wxDocManager::SelectViewType(wxDocTemplate
**templates
,
1554 int noTemplates
, bool sort
)
1556 wxArrayString
strings(sort
);
1557 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1561 for (i
= 0; i
< noTemplates
; i
++)
1563 wxDocTemplate
*templ
= templates
[i
];
1564 if ( templ
->IsVisible() && !templ
->GetViewName().empty() )
1568 for (j
= 0; j
< n
; j
++)
1570 //filter out NOT unique views
1571 if ( templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
)
1577 strings
.Add(templ
->m_viewTypeName
);
1586 // Yes, this will be slow, but template lists
1587 // are typically short.
1589 n
= strings
.Count();
1590 for (i
= 0; i
< n
; i
++)
1592 for (j
= 0; j
< noTemplates
; j
++)
1594 if (strings
[i
] == templates
[j
]->m_viewTypeName
)
1595 data
[i
] = templates
[j
];
1600 wxDocTemplate
*theTemplate
;
1602 // the same logic as above
1606 theTemplate
= (wxDocTemplate
*)NULL
;
1610 theTemplate
= data
[0];
1614 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1616 _("Select a document view"),
1620 wxFindSuitableParent()
1629 void wxDocManager::AssociateTemplate(wxDocTemplate
*temp
)
1631 if (!m_templates
.Member(temp
))
1632 m_templates
.Append(temp
);
1635 void wxDocManager::DisassociateTemplate(wxDocTemplate
*temp
)
1637 m_templates
.DeleteObject(temp
);
1640 // Add and remove a document from the manager's list
1641 void wxDocManager::AddDocument(wxDocument
*doc
)
1643 if (!m_docs
.Member(doc
))
1647 void wxDocManager::RemoveDocument(wxDocument
*doc
)
1649 m_docs
.DeleteObject(doc
);
1652 // Views or windows should inform the document manager
1653 // when a view is going in or out of focus
1654 void wxDocManager::ActivateView(wxView
*view
, bool activate
, bool WXUNUSED(deleting
))
1656 // If we're deactiving, and if we're not actually deleting the view, then
1657 // don't reset the current view because we may be going to
1658 // a window without a view.
1659 // WHAT DID I MEAN BY THAT EXACTLY?
1663 if (m_currentView == view)
1664 m_currentView = NULL;
1670 m_currentView
= view
;
1672 m_currentView
= (wxView
*) NULL
;
1676 // ----------------------------------------------------------------------------
1677 // Default document child frame
1678 // ----------------------------------------------------------------------------
1680 BEGIN_EVENT_TABLE(wxDocChildFrame
, wxFrame
)
1681 EVT_ACTIVATE(wxDocChildFrame::OnActivate
)
1682 EVT_CLOSE(wxDocChildFrame::OnCloseWindow
)
1685 wxDocChildFrame::wxDocChildFrame(wxDocument
*doc
,
1689 const wxString
& title
,
1693 const wxString
& name
)
1694 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1696 m_childDocument
= doc
;
1699 view
->SetFrame(this);
1702 wxDocChildFrame::~wxDocChildFrame()
1706 // Extend event processing to search the view's event table
1707 bool wxDocChildFrame::ProcessEvent(wxEvent
& event
)
1710 m_childView
->Activate(TRUE
);
1712 if ( !m_childView
|| ! m_childView
->ProcessEvent(event
) )
1714 // Only hand up to the parent if it's a menu command
1715 if (!event
.IsKindOf(CLASSINFO(wxCommandEvent
)) || !GetParent() || !GetParent()->ProcessEvent(event
))
1716 return wxEvtHandler::ProcessEvent(event
);
1724 void wxDocChildFrame::OnActivate(wxActivateEvent
& event
)
1726 wxFrame::OnActivate(event
);
1729 m_childView
->Activate(event
.GetActive());
1732 void wxDocChildFrame::OnCloseWindow(wxCloseEvent
& event
)
1737 if (!event
.CanVeto())
1738 ans
= TRUE
; // Must delete.
1740 ans
= m_childView
->Close(FALSE
); // FALSE means don't delete associated window
1744 m_childView
->Activate(FALSE
);
1746 m_childView
= (wxView
*) NULL
;
1747 m_childDocument
= (wxDocument
*) NULL
;
1758 // ----------------------------------------------------------------------------
1759 // Default parent frame
1760 // ----------------------------------------------------------------------------
1762 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1763 EVT_MENU(wxID_EXIT
, wxDocParentFrame::OnExit
)
1764 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE9
, wxDocParentFrame::OnMRUFile
)
1765 EVT_CLOSE(wxDocParentFrame::OnCloseWindow
)
1768 wxDocParentFrame::wxDocParentFrame(wxDocManager
*manager
,
1771 const wxString
& title
,
1775 const wxString
& name
)
1776 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1778 m_docManager
= manager
;
1781 void wxDocParentFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
1786 void wxDocParentFrame::OnMRUFile(wxCommandEvent
& event
)
1788 int n
= event
.GetId() - wxID_FILE1
; // the index in MRU list
1789 wxString
filename(m_docManager
->GetHistoryFile(n
));
1790 if ( !filename
.IsEmpty() )
1792 // verify that the file exists before doing anything else
1793 if ( wxFile::Exists(filename
) )
1796 (void)m_docManager
->CreateDocument(filename
, wxDOC_SILENT
);
1800 // remove the bogus filename from the MRU list and notify the user
1802 m_docManager
->RemoveFileFromHistory(n
);
1804 wxLogError(_("The file '%s' doesn't exist and couldn't be opened.\nIt has been removed from the most recently used files list."),
1810 // Extend event processing to search the view's event table
1811 bool wxDocParentFrame::ProcessEvent(wxEvent
& event
)
1813 // Try the document manager, then do default processing
1814 if (!m_docManager
|| !m_docManager
->ProcessEvent(event
))
1815 return wxEvtHandler::ProcessEvent(event
);
1820 // Define the behaviour for the frame closing
1821 // - must delete all frames except for the main one.
1822 void wxDocParentFrame::OnCloseWindow(wxCloseEvent
& event
)
1824 if (m_docManager
->Clear(!event
.CanVeto()))
1832 #if wxUSE_PRINTING_ARCHITECTURE
1834 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
)
1837 m_printoutView
= view
;
1840 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
1844 // Get the logical pixels per inch of screen and printer
1845 int ppiScreenX
, ppiScreenY
;
1846 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
1847 int ppiPrinterX
, ppiPrinterY
;
1848 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
1850 // This scales the DC so that the printout roughly represents the
1851 // the screen scaling. The text point size _should_ be the right size
1852 // but in fact is too small for some reason. This is a detail that will
1853 // need to be addressed at some point but can be fudged for the
1855 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
1857 // Now we have to check in case our real page size is reduced
1858 // (e.g. because we're drawing to a print preview memory DC)
1859 int pageWidth
, pageHeight
;
1861 dc
->GetSize(&w
, &h
);
1862 GetPageSizePixels(&pageWidth
, &pageHeight
);
1864 // If printer pageWidth == current DC width, then this doesn't
1865 // change. But w might be the preview bitmap width, so scale down.
1866 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
1867 dc
->SetUserScale(overallScale
, overallScale
);
1871 m_printoutView
->OnDraw(dc
);
1876 bool wxDocPrintout::HasPage(int pageNum
)
1878 return (pageNum
== 1);
1881 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
1883 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
1889 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
1897 #endif // wxUSE_PRINTING_ARCHITECTURE
1899 // ----------------------------------------------------------------------------
1900 // File history processor
1901 // ----------------------------------------------------------------------------
1903 wxFileHistory::wxFileHistory(int maxFiles
)
1905 m_fileMaxFiles
= maxFiles
;
1907 m_fileHistory
= new wxChar
*[m_fileMaxFiles
];
1910 wxFileHistory::~wxFileHistory()
1913 for (i
= 0; i
< m_fileHistoryN
; i
++)
1914 delete[] m_fileHistory
[i
];
1915 delete[] m_fileHistory
;
1918 // File history management
1919 void wxFileHistory::AddFileToHistory(const wxString
& file
)
1923 // Check we don't already have this file
1924 for (i
= 0; i
< m_fileHistoryN
; i
++)
1926 if ( m_fileHistory
[i
] && (file
== m_fileHistory
[i
]) )
1928 // we do have it, move it to the top of the history
1929 RemoveFileFromHistory (i
);
1930 AddFileToHistory (file
);
1935 // if we already have a full history, delete the one at the end
1936 if ( m_fileMaxFiles
== m_fileHistoryN
)
1938 RemoveFileFromHistory (m_fileHistoryN
- 1);
1939 AddFileToHistory (file
);
1943 // Add to the project file history:
1944 // Move existing files (if any) down so we can insert file at beginning.
1945 if (m_fileHistoryN
< m_fileMaxFiles
)
1947 wxNode
* node
= m_fileMenus
.First();
1950 wxMenu
* menu
= (wxMenu
*) node
->Data();
1951 if (m_fileHistoryN
== 0)
1952 menu
->AppendSeparator();
1953 menu
->Append(wxID_FILE1
+m_fileHistoryN
, _("[EMPTY]"));
1954 node
= node
->Next();
1958 // Shuffle filenames down
1959 for (i
= (m_fileHistoryN
-1); i
> 0; i
--)
1961 m_fileHistory
[i
] = m_fileHistory
[i
-1];
1963 m_fileHistory
[0] = copystring(file
);
1965 // this is the directory of the last opened file
1966 wxString pathCurrent
;
1967 wxSplitPath( m_fileHistory
[0], &pathCurrent
, NULL
, NULL
);
1968 for (i
= 0; i
< m_fileHistoryN
; i
++)
1970 if ( m_fileHistory
[i
] )
1972 // if in same directory just show the filename; otherwise the full
1974 wxString pathInMenu
, path
, filename
, ext
;
1975 wxSplitPath( m_fileHistory
[i
], &path
, &filename
, &ext
);
1976 if ( path
== pathCurrent
)
1978 pathInMenu
= filename
;
1980 pathInMenu
= pathInMenu
+ wxFILE_SEP_EXT
+ ext
;
1984 // absolute path; could also set relative path
1985 pathInMenu
= m_fileHistory
[i
];
1989 buf
.Printf(s_MRUEntryFormat
, i
+ 1, pathInMenu
.c_str());
1990 wxNode
* node
= m_fileMenus
.First();
1993 wxMenu
* menu
= (wxMenu
*) node
->Data();
1994 menu
->SetLabel(wxID_FILE1
+ i
, buf
);
1995 node
= node
->Next();
2001 void wxFileHistory::RemoveFileFromHistory(int i
)
2003 wxCHECK_RET( i
< m_fileHistoryN
,
2004 wxT("invalid index in wxFileHistory::RemoveFileFromHistory") );
2006 // delete the element from the array (could use memmove() too...)
2007 delete [] m_fileHistory
[i
];
2010 for ( j
= i
; j
< m_fileHistoryN
- 1; j
++ )
2012 m_fileHistory
[j
] = m_fileHistory
[j
+ 1];
2015 wxNode
* node
= m_fileMenus
.First();
2018 wxMenu
* menu
= (wxMenu
*) node
->Data();
2021 // shuffle filenames up
2023 for ( j
= i
; j
< m_fileHistoryN
- 1; j
++ )
2025 buf
.Printf(s_MRUEntryFormat
, j
+ 1, m_fileHistory
[j
]);
2026 menu
->SetLabel(wxID_FILE1
+ j
, buf
);
2029 node
= node
->Next();
2031 // delete the last menu item which is unused now
2032 if (menu
->FindItem(wxID_FILE1
+ m_fileHistoryN
- 1))
2033 menu
->Delete(wxID_FILE1
+ m_fileHistoryN
- 1);
2035 // delete the last separator too if no more files are left
2036 if ( m_fileHistoryN
== 1 )
2038 wxMenuItemList::Node
*node
= menu
->GetMenuItems().GetLast();
2041 wxMenuItem
*menuItem
= node
->GetData();
2042 if ( menuItem
->IsSeparator() )
2044 menu
->Delete(menuItem
);
2046 //else: should we search backwards for the last separator?
2048 //else: menu is empty somehow
2055 wxString
wxFileHistory::GetHistoryFile(int i
) const
2058 if ( i
< m_fileHistoryN
)
2060 s
= m_fileHistory
[i
];
2064 wxFAIL_MSG( wxT("bad index in wxFileHistory::GetHistoryFile") );
2070 void wxFileHistory::UseMenu(wxMenu
*menu
)
2072 if (!m_fileMenus
.Member(menu
))
2073 m_fileMenus
.Append(menu
);
2076 void wxFileHistory::RemoveMenu(wxMenu
*menu
)
2078 m_fileMenus
.DeleteObject(menu
);
2082 void wxFileHistory::Load(wxConfigBase
& config
)
2086 buf
.Printf(wxT("file%d"), m_fileHistoryN
+1);
2087 wxString historyFile
;
2088 while ((m_fileHistoryN
<= m_fileMaxFiles
) && config
.Read(buf
, &historyFile
) && (historyFile
!= wxT("")))
2090 m_fileHistory
[m_fileHistoryN
] = copystring((const wxChar
*) historyFile
);
2092 buf
.Printf(wxT("file%d"), m_fileHistoryN
+1);
2093 historyFile
= wxT("");
2098 void wxFileHistory::Save(wxConfigBase
& config
)
2101 for (i
= 0; i
< m_fileHistoryN
; i
++)
2104 buf
.Printf(wxT("file%d"), i
+1);
2105 config
.Write(buf
, wxString(m_fileHistory
[i
]));
2108 #endif // wxUSE_CONFIG
2110 void wxFileHistory::AddFilesToMenu()
2112 if (m_fileHistoryN
> 0)
2114 wxNode
* node
= m_fileMenus
.First();
2117 wxMenu
* menu
= (wxMenu
*) node
->Data();
2118 menu
->AppendSeparator();
2120 for (i
= 0; i
< m_fileHistoryN
; i
++)
2122 if (m_fileHistory
[i
])
2125 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
2126 menu
->Append(wxID_FILE1
+i
, buf
);
2129 node
= node
->Next();
2134 void wxFileHistory::AddFilesToMenu(wxMenu
* menu
)
2136 if (m_fileHistoryN
> 0)
2138 menu
->AppendSeparator();
2140 for (i
= 0; i
< m_fileHistoryN
; i
++)
2142 if (m_fileHistory
[i
])
2145 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
2146 menu
->Append(wxID_FILE1
+i
, buf
);
2152 // ----------------------------------------------------------------------------
2153 // Permits compatibility with existing file formats and functions that
2154 // manipulate files directly
2155 // ----------------------------------------------------------------------------
2157 #if wxUSE_STD_IOSTREAM
2158 bool wxTransferFileToStream(const wxString
& filename
, wxSTD ostream
& stream
)
2163 if ((fd1
= wxFopen (filename
.fn_str(), _T("rb"))) == NULL
)
2166 while ((ch
= getc (fd1
)) != EOF
)
2167 stream
<< (unsigned char)ch
;
2173 bool wxTransferStreamToFile(wxSTD istream
& stream
, const wxString
& filename
)
2178 if ((fd1
= wxFopen (filename
.fn_str(), _T("wb"))) == NULL
)
2183 while (!stream
.eof())
2193 bool wxTransferFileToStream(const wxString
& filename
, wxOutputStream
& stream
)
2198 if ((fd1
= wxFopen (filename
, wxT("rb"))) == NULL
)
2201 while ((ch
= getc (fd1
)) != EOF
)
2202 stream
.PutC((char) ch
);
2208 bool wxTransferStreamToFile(wxInputStream
& stream
, const wxString
& filename
)
2213 if ((fd1
= wxFopen (filename
, wxT("wb"))) == NULL
)
2218 int len
= stream
.GetSize();
2219 // TODO: is this the correct test for EOF?
2220 while (stream
.TellI() < (len
- 1))
2230 #endif // wxUSE_DOC_VIEW_ARCHITECTURE