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"
48 #include "wx/filename.h"
55 #if wxUSE_PRINTING_ARCHITECTURE
56 #include "wx/prntbase.h"
57 #include "wx/printdlg.h"
60 #include "wx/msgdlg.h"
61 #include "wx/choicdlg.h"
62 #include "wx/docview.h"
63 #include "wx/confbase.h"
65 #include "wx/cmdproc.h"
70 #if wxUSE_STD_IOSTREAM
71 #include "wx/ioswrap.h"
78 #include "wx/wfstream.h"
81 // ----------------------------------------------------------------------------
83 // ----------------------------------------------------------------------------
85 IMPLEMENT_ABSTRACT_CLASS(wxDocument
, wxEvtHandler
)
86 IMPLEMENT_ABSTRACT_CLASS(wxView
, wxEvtHandler
)
87 IMPLEMENT_ABSTRACT_CLASS(wxDocTemplate
, wxObject
)
88 IMPLEMENT_DYNAMIC_CLASS(wxDocManager
, wxEvtHandler
)
89 IMPLEMENT_CLASS(wxDocChildFrame
, wxFrame
)
90 IMPLEMENT_CLASS(wxDocParentFrame
, wxFrame
)
92 #if wxUSE_PRINTING_ARCHITECTURE
93 IMPLEMENT_DYNAMIC_CLASS(wxDocPrintout
, wxPrintout
)
96 IMPLEMENT_DYNAMIC_CLASS(wxFileHistory
, wxObject
)
98 // ----------------------------------------------------------------------------
99 // function prototypes
100 // ----------------------------------------------------------------------------
102 static inline wxString
FindExtension(const wxChar
*path
);
104 // ----------------------------------------------------------------------------
106 // ----------------------------------------------------------------------------
108 static const wxChar
*s_MRUEntryFormat
= wxT("&%d %s");
110 // ============================================================================
112 // ============================================================================
114 // ----------------------------------------------------------------------------
116 // ----------------------------------------------------------------------------
118 static wxString
FindExtension(const wxChar
*path
)
121 wxSplitPath(path
, NULL
, NULL
, &ext
);
123 // VZ: extensions are considered not case sensitive - is this really a good
125 return ext
.MakeLower();
128 // ----------------------------------------------------------------------------
129 // Definition of wxDocument
130 // ----------------------------------------------------------------------------
132 wxDocument::wxDocument(wxDocument
*parent
)
134 m_documentModified
= FALSE
;
135 m_documentParent
= parent
;
136 m_documentTemplate
= (wxDocTemplate
*) NULL
;
137 m_commandProcessor
= (wxCommandProcessor
*) NULL
;
141 bool wxDocument::DeleteContents()
146 wxDocument::~wxDocument()
150 if (m_commandProcessor
)
151 delete m_commandProcessor
;
153 if (GetDocumentManager())
154 GetDocumentManager()->RemoveDocument(this);
156 // Not safe to do here, since it'll invoke virtual view functions
157 // expecting to see valid derived objects: and by the time we get here,
158 // we've called destructors higher up.
162 bool wxDocument::Close()
164 if (OnSaveModified())
165 return OnCloseDocument();
170 bool wxDocument::OnCloseDocument()
172 // Tell all views that we're about to close
179 // Note that this implicitly deletes the document when the last view is
181 bool wxDocument::DeleteAllViews()
183 wxDocManager
* manager
= GetDocumentManager();
185 wxNode
*node
= m_documentViews
.GetFirst();
188 wxView
*view
= (wxView
*)node
->GetData();
192 wxNode
*next
= node
->GetNext();
194 delete view
; // Deletes node implicitly
197 // If we haven't yet deleted the document (for example
198 // if there were no views) then delete it.
199 if (manager
&& manager
->GetDocuments().Member(this))
205 wxView
*wxDocument::GetFirstView() const
207 if (m_documentViews
.GetCount() == 0)
208 return (wxView
*) NULL
;
209 return (wxView
*)m_documentViews
.GetFirst()->GetData();
212 wxDocManager
*wxDocument::GetDocumentManager() const
214 return (m_documentTemplate
? m_documentTemplate
->GetDocumentManager() : (wxDocManager
*) NULL
);
217 bool wxDocument::OnNewDocument()
219 if (!OnSaveModified())
222 if (OnCloseDocument()==FALSE
) return FALSE
;
225 SetDocumentSaved(FALSE
);
228 GetDocumentManager()->MakeDefaultName(name
);
230 SetFilename(name
, TRUE
);
235 bool wxDocument::Save()
237 if (!IsModified() && m_savedYet
)
240 if ( m_documentFile
.empty() || !m_savedYet
)
243 return OnSaveDocument(m_documentFile
);
246 bool wxDocument::SaveAs()
248 wxDocTemplate
*docTemplate
= GetDocumentTemplate();
252 wxString tmp
= wxFileSelector(_("Save as"),
253 docTemplate
->GetDirectory(),
254 wxFileNameFromPath(GetFilename()),
255 docTemplate
->GetDefaultExtension(),
256 docTemplate
->GetFileFilter(),
257 wxSAVE
| wxOVERWRITE_PROMPT
,
258 GetDocumentWindow());
263 wxString
fileName(tmp
);
264 wxString path
, name
, ext
;
265 wxSplitPath(fileName
, & path
, & name
, & ext
);
267 if (ext
.IsEmpty() || ext
== wxT(""))
269 fileName
+= wxT(".");
270 fileName
+= docTemplate
->GetDefaultExtension();
273 SetFilename(fileName
);
274 SetTitle(wxFileNameFromPath(fileName
));
276 GetDocumentManager()->AddFileToHistory(fileName
);
278 // Notify the views that the filename has changed
279 wxNode
*node
= m_documentViews
.GetFirst();
282 wxView
*view
= (wxView
*)node
->GetData();
283 view
->OnChangeFilename();
284 node
= node
->GetNext();
287 return OnSaveDocument(m_documentFile
);
290 bool wxDocument::OnSaveDocument(const wxString
& file
)
296 if (wxTheApp
->GetAppName() != wxT(""))
297 msgTitle
= wxTheApp
->GetAppName();
299 msgTitle
= wxString(_("File error"));
301 #if wxUSE_STD_IOSTREAM
302 wxSTD ofstream
store(file
.mb_str());
303 if (store
.fail() || store
.bad())
305 wxFileOutputStream
store(file
);
306 if (store
.GetLastError() != wxSTREAM_NO_ERROR
)
309 (void)wxMessageBox(_("Sorry, could not open this file for saving."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
310 GetDocumentWindow());
314 if (!SaveObject(store
))
316 (void)wxMessageBox(_("Sorry, could not save this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
317 GetDocumentWindow());
323 SetDocumentSaved(TRUE
);
325 wxFileName
fn(file
) ;
326 fn
.MacSetDefaultTypeAndCreator() ;
331 bool wxDocument::OnOpenDocument(const wxString
& file
)
333 if (!OnSaveModified())
337 if (wxTheApp
->GetAppName() != wxT(""))
338 msgTitle
= wxTheApp
->GetAppName();
340 msgTitle
= wxString(_("File error"));
342 #if wxUSE_STD_IOSTREAM
343 wxSTD ifstream
store(file
.mb_str());
344 if (store
.fail() || store
.bad())
346 wxFileInputStream
store(file
);
347 if (store
.GetLastError() != wxSTREAM_NO_ERROR
)
350 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
|wxICON_EXCLAMATION
,
351 GetDocumentWindow());
354 #if wxUSE_STD_IOSTREAM
356 if ( !store
&& !store
.eof() )
358 int res
= LoadObject(store
).GetLastError();
359 if ((res
!= wxSTREAM_NO_ERROR
) &&
360 (res
!= wxSTREAM_EOF
))
363 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
|wxICON_EXCLAMATION
,
364 GetDocumentWindow());
367 SetFilename(file
, TRUE
);
376 #if wxUSE_STD_IOSTREAM
377 wxSTD istream
& wxDocument::LoadObject(wxSTD istream
& stream
)
379 wxInputStream
& wxDocument::LoadObject(wxInputStream
& stream
)
385 #if wxUSE_STD_IOSTREAM
386 wxSTD ostream
& wxDocument::SaveObject(wxSTD ostream
& stream
)
388 wxOutputStream
& wxDocument::SaveObject(wxOutputStream
& stream
)
394 bool wxDocument::Revert()
400 // Get title, or filename if no title, else unnamed
401 bool wxDocument::GetPrintableName(wxString
& buf
) const
403 if (m_documentTitle
!= wxT(""))
405 buf
= m_documentTitle
;
408 else if (m_documentFile
!= wxT(""))
410 buf
= wxFileNameFromPath(m_documentFile
);
420 wxWindow
*wxDocument::GetDocumentWindow() const
422 wxView
*view
= GetFirstView();
424 return view
->GetFrame();
426 return wxTheApp
->GetTopWindow();
429 wxCommandProcessor
*wxDocument::OnCreateCommandProcessor()
431 return new wxCommandProcessor
;
434 // TRUE if safe to close
435 bool wxDocument::OnSaveModified()
440 GetPrintableName(title
);
443 if (wxTheApp
->GetAppName() != wxT(""))
444 msgTitle
= wxTheApp
->GetAppName();
446 msgTitle
= wxString(_("Warning"));
449 prompt
.Printf(_("Do you want to save changes to document %s?"),
450 (const wxChar
*)title
);
451 int res
= wxMessageBox(prompt
, msgTitle
,
452 wxYES_NO
|wxCANCEL
|wxICON_QUESTION
,
453 GetDocumentWindow());
459 else if (res
== wxYES
)
461 else if (res
== wxCANCEL
)
467 bool wxDocument::Draw(wxDC
& WXUNUSED(context
))
472 bool wxDocument::AddView(wxView
*view
)
474 if (!m_documentViews
.Member(view
))
476 m_documentViews
.Append(view
);
482 bool wxDocument::RemoveView(wxView
*view
)
484 (void)m_documentViews
.DeleteObject(view
);
489 bool wxDocument::OnCreate(const wxString
& WXUNUSED(path
), long flags
)
491 if (GetDocumentTemplate()->CreateView(this, flags
))
497 // Called after a view is added or removed.
498 // The default implementation deletes the document if
499 // there are no more views.
500 void wxDocument::OnChangedViewList()
502 if (m_documentViews
.GetCount() == 0)
504 if (OnSaveModified())
511 void wxDocument::UpdateAllViews(wxView
*sender
, wxObject
*hint
)
513 wxNode
*node
= m_documentViews
.GetFirst();
516 wxView
*view
= (wxView
*)node
->GetData();
518 view
->OnUpdate(sender
, hint
);
519 node
= node
->GetNext();
523 void wxDocument::NotifyClosing()
525 wxNode
*node
= m_documentViews
.GetFirst();
528 wxView
*view
= (wxView
*)node
->GetData();
529 view
->OnClosingDocument();
530 node
= node
->GetNext();
534 void wxDocument::SetFilename(const wxString
& filename
, bool notifyViews
)
536 m_documentFile
= filename
;
539 // Notify the views that the filename has changed
540 wxNode
*node
= m_documentViews
.GetFirst();
543 wxView
*view
= (wxView
*)node
->GetData();
544 view
->OnChangeFilename();
545 node
= node
->GetNext();
550 // ----------------------------------------------------------------------------
552 // ----------------------------------------------------------------------------
557 m_viewDocument
= (wxDocument
*) NULL
;
559 m_viewTypeName
= wxT("");
560 m_viewFrame
= (wxFrame
*) NULL
;
565 // GetDocumentManager()->ActivateView(this, FALSE, TRUE);
566 m_viewDocument
->RemoveView(this);
569 // Extend event processing to search the document's event table
570 bool wxView::ProcessEvent(wxEvent
& event
)
572 if ( !GetDocument() || !GetDocument()->ProcessEvent(event
) )
573 return wxEvtHandler::ProcessEvent(event
);
578 void wxView::OnActivateView(bool WXUNUSED(activate
), wxView
*WXUNUSED(activeView
), wxView
*WXUNUSED(deactiveView
))
582 void wxView::OnPrint(wxDC
*dc
, wxObject
*WXUNUSED(info
))
587 void wxView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
))
591 void wxView::OnChangeFilename()
593 if (GetFrame() && GetDocument())
597 GetDocument()->GetPrintableName(title
);
599 GetFrame()->SetTitle(title
);
603 void wxView::SetDocument(wxDocument
*doc
)
605 m_viewDocument
= doc
;
610 bool wxView::Close(bool deleteWindow
)
612 if (OnClose(deleteWindow
))
618 void wxView::Activate(bool activate
)
620 if (GetDocument() && GetDocumentManager())
622 OnActivateView(activate
, this, GetDocumentManager()->GetCurrentView());
623 GetDocumentManager()->ActivateView(this, activate
);
627 bool wxView::OnClose(bool WXUNUSED(deleteWindow
))
629 return GetDocument() ? GetDocument()->Close() : TRUE
;
632 #if wxUSE_PRINTING_ARCHITECTURE
633 wxPrintout
*wxView::OnCreatePrintout()
635 return new wxDocPrintout(this);
637 #endif // wxUSE_PRINTING_ARCHITECTURE
639 // ----------------------------------------------------------------------------
641 // ----------------------------------------------------------------------------
643 wxDocTemplate::wxDocTemplate(wxDocManager
*manager
,
644 const wxString
& descr
,
645 const wxString
& filter
,
648 const wxString
& docTypeName
,
649 const wxString
& viewTypeName
,
650 wxClassInfo
*docClassInfo
,
651 wxClassInfo
*viewClassInfo
,
654 m_documentManager
= manager
;
655 m_description
= descr
;
658 m_fileFilter
= filter
;
660 m_docTypeName
= docTypeName
;
661 m_viewTypeName
= viewTypeName
;
662 m_documentManager
->AssociateTemplate(this);
664 m_docClassInfo
= docClassInfo
;
665 m_viewClassInfo
= viewClassInfo
;
668 wxDocTemplate::~wxDocTemplate()
670 m_documentManager
->DisassociateTemplate(this);
673 // Tries to dynamically construct an object of the right class.
674 wxDocument
*wxDocTemplate::CreateDocument(const wxString
& path
, long flags
)
677 return (wxDocument
*) NULL
;
678 wxDocument
*doc
= (wxDocument
*)m_docClassInfo
->CreateObject();
679 doc
->SetFilename(path
);
680 doc
->SetDocumentTemplate(this);
681 GetDocumentManager()->AddDocument(doc
);
682 doc
->SetCommandProcessor(doc
->OnCreateCommandProcessor());
684 if (doc
->OnCreate(path
, flags
))
688 if (GetDocumentManager()->GetDocuments().Member(doc
))
689 doc
->DeleteAllViews();
690 return (wxDocument
*) NULL
;
694 wxView
*wxDocTemplate::CreateView(wxDocument
*doc
, long flags
)
696 if (!m_viewClassInfo
)
697 return (wxView
*) NULL
;
698 wxView
*view
= (wxView
*)m_viewClassInfo
->CreateObject();
699 view
->SetDocument(doc
);
700 if (view
->OnCreate(doc
, flags
))
707 return (wxView
*) NULL
;
711 // The default (very primitive) format detection: check is the extension is
712 // that of the template
713 bool wxDocTemplate::FileMatchesTemplate(const wxString
& path
)
715 return GetDefaultExtension().IsSameAs(FindExtension(path
));
718 // ----------------------------------------------------------------------------
720 // ----------------------------------------------------------------------------
722 BEGIN_EVENT_TABLE(wxDocManager
, wxEvtHandler
)
723 EVT_MENU(wxID_OPEN
, wxDocManager::OnFileOpen
)
724 EVT_MENU(wxID_CLOSE
, wxDocManager::OnFileClose
)
725 EVT_MENU(wxID_CLOSE_ALL
, wxDocManager::OnFileCloseAll
)
726 EVT_MENU(wxID_REVERT
, wxDocManager::OnFileRevert
)
727 EVT_MENU(wxID_NEW
, wxDocManager::OnFileNew
)
728 EVT_MENU(wxID_SAVE
, wxDocManager::OnFileSave
)
729 EVT_MENU(wxID_SAVEAS
, wxDocManager::OnFileSaveAs
)
730 EVT_MENU(wxID_UNDO
, wxDocManager::OnUndo
)
731 EVT_MENU(wxID_REDO
, wxDocManager::OnRedo
)
733 EVT_UPDATE_UI(wxID_OPEN
, wxDocManager::OnUpdateFileOpen
)
734 EVT_UPDATE_UI(wxID_CLOSE
, wxDocManager::OnUpdateFileClose
)
735 EVT_UPDATE_UI(wxID_CLOSE_ALL
, wxDocManager::OnUpdateFileClose
)
736 EVT_UPDATE_UI(wxID_REVERT
, wxDocManager::OnUpdateFileRevert
)
737 EVT_UPDATE_UI(wxID_NEW
, wxDocManager::OnUpdateFileNew
)
738 EVT_UPDATE_UI(wxID_SAVE
, wxDocManager::OnUpdateFileSave
)
739 EVT_UPDATE_UI(wxID_SAVEAS
, wxDocManager::OnUpdateFileSaveAs
)
740 EVT_UPDATE_UI(wxID_UNDO
, wxDocManager::OnUpdateUndo
)
741 EVT_UPDATE_UI(wxID_REDO
, wxDocManager::OnUpdateRedo
)
743 #if wxUSE_PRINTING_ARCHITECTURE
744 EVT_MENU(wxID_PRINT
, wxDocManager::OnPrint
)
745 EVT_MENU(wxID_PRINT_SETUP
, wxDocManager::OnPrintSetup
)
746 EVT_MENU(wxID_PREVIEW
, wxDocManager::OnPreview
)
748 EVT_UPDATE_UI(wxID_PRINT
, wxDocManager::OnUpdatePrint
)
749 EVT_UPDATE_UI(wxID_PRINT_SETUP
, wxDocManager::OnUpdatePrintSetup
)
750 EVT_UPDATE_UI(wxID_PREVIEW
, wxDocManager::OnUpdatePreview
)
754 wxDocManager
* wxDocManager::sm_docManager
= (wxDocManager
*) NULL
;
756 wxDocManager::wxDocManager(long flags
, bool initialize
)
758 m_defaultDocumentNameCounter
= 1;
760 m_currentView
= (wxView
*) NULL
;
761 m_maxDocsOpen
= 10000;
762 m_fileHistory
= (wxFileHistory
*) NULL
;
765 sm_docManager
= this;
768 wxDocManager::~wxDocManager()
772 delete m_fileHistory
;
773 sm_docManager
= (wxDocManager
*) NULL
;
776 bool wxDocManager::CloseDocuments(bool force
)
778 wxNode
*node
= m_docs
.GetFirst();
781 wxDocument
*doc
= (wxDocument
*)node
->GetData();
782 wxNode
*next
= node
->GetNext();
784 if (!doc
->Close() && !force
)
787 // Implicitly deletes the document when the last
788 // view is removed (deleted)
789 doc
->DeleteAllViews();
791 // Check document is deleted
792 if (m_docs
.Member(doc
))
795 // This assumes that documents are not connected in
796 // any way, i.e. deleting one document does NOT
803 bool wxDocManager::Clear(bool force
)
805 if (!CloseDocuments(force
))
808 wxNode
*node
= m_templates
.GetFirst();
811 wxDocTemplate
*templ
= (wxDocTemplate
*) node
->GetData();
812 wxNode
* next
= node
->GetNext();
819 bool wxDocManager::Initialize()
821 m_fileHistory
= OnCreateFileHistory();
825 wxFileHistory
*wxDocManager::OnCreateFileHistory()
827 return new wxFileHistory
;
830 void wxDocManager::OnFileClose(wxCommandEvent
& WXUNUSED(event
))
832 wxDocument
*doc
= GetCurrentDocument();
837 doc
->DeleteAllViews();
838 if (m_docs
.Member(doc
))
843 void wxDocManager::OnFileCloseAll(wxCommandEvent
& WXUNUSED(event
))
845 CloseDocuments(FALSE
);
848 void wxDocManager::OnFileNew(wxCommandEvent
& WXUNUSED(event
))
850 CreateDocument( wxT(""), wxDOC_NEW
);
853 void wxDocManager::OnFileOpen(wxCommandEvent
& WXUNUSED(event
))
855 if ( !CreateDocument( wxT(""), 0) )
861 void wxDocManager::OnFileRevert(wxCommandEvent
& WXUNUSED(event
))
863 wxDocument
*doc
= GetCurrentDocument();
869 void wxDocManager::OnFileSave(wxCommandEvent
& WXUNUSED(event
))
871 wxDocument
*doc
= GetCurrentDocument();
877 void wxDocManager::OnFileSaveAs(wxCommandEvent
& WXUNUSED(event
))
879 wxDocument
*doc
= GetCurrentDocument();
885 void wxDocManager::OnPrint(wxCommandEvent
& WXUNUSED(event
))
887 #if wxUSE_PRINTING_ARCHITECTURE
888 wxView
*view
= GetCurrentView();
892 wxPrintout
*printout
= view
->OnCreatePrintout();
896 printer
.Print(view
->GetFrame(), printout
, TRUE
);
900 #endif // wxUSE_PRINTING_ARCHITECTURE
903 void wxDocManager::OnPrintSetup(wxCommandEvent
& WXUNUSED(event
))
905 #if wxUSE_PRINTING_ARCHITECTURE
906 wxWindow
*parentWin
= wxTheApp
->GetTopWindow();
907 wxView
*view
= GetCurrentView();
909 parentWin
= view
->GetFrame();
911 wxPrintDialogData data
;
913 wxPrintDialog
printerDialog(parentWin
, &data
);
914 printerDialog
.GetPrintDialogData().SetSetupDialog(TRUE
);
915 printerDialog
.ShowModal();
916 #endif // wxUSE_PRINTING_ARCHITECTURE
919 void wxDocManager::OnPreview(wxCommandEvent
& WXUNUSED(event
))
921 #if wxUSE_PRINTING_ARCHITECTURE
922 wxView
*view
= GetCurrentView();
926 wxPrintout
*printout
= view
->OnCreatePrintout();
929 // Pass two printout objects: for preview, and possible printing.
930 wxPrintPreviewBase
*preview
= (wxPrintPreviewBase
*) NULL
;
931 preview
= new wxPrintPreview(printout
, view
->OnCreatePrintout());
933 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, (wxFrame
*)wxTheApp
->GetTopWindow(), _("Print Preview"),
934 wxPoint(100, 100), wxSize(600, 650));
935 frame
->Centre(wxBOTH
);
939 #endif // wxUSE_PRINTING_ARCHITECTURE
942 void wxDocManager::OnUndo(wxCommandEvent
& WXUNUSED(event
))
944 wxDocument
*doc
= GetCurrentDocument();
947 if (doc
->GetCommandProcessor())
948 doc
->GetCommandProcessor()->Undo();
951 void wxDocManager::OnRedo(wxCommandEvent
& WXUNUSED(event
))
953 wxDocument
*doc
= GetCurrentDocument();
956 if (doc
->GetCommandProcessor())
957 doc
->GetCommandProcessor()->Redo();
960 // Handlers for UI update commands
962 void wxDocManager::OnUpdateFileOpen(wxUpdateUIEvent
& event
)
964 event
.Enable( TRUE
);
967 void wxDocManager::OnUpdateFileClose(wxUpdateUIEvent
& event
)
969 wxDocument
*doc
= GetCurrentDocument();
970 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
973 void wxDocManager::OnUpdateFileRevert(wxUpdateUIEvent
& event
)
975 wxDocument
*doc
= GetCurrentDocument();
976 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
979 void wxDocManager::OnUpdateFileNew(wxUpdateUIEvent
& event
)
981 event
.Enable( TRUE
);
984 void wxDocManager::OnUpdateFileSave(wxUpdateUIEvent
& event
)
986 wxDocument
*doc
= GetCurrentDocument();
987 event
.Enable( doc
&& doc
->IsModified() );
990 void wxDocManager::OnUpdateFileSaveAs(wxUpdateUIEvent
& event
)
992 wxDocument
*doc
= GetCurrentDocument();
993 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
996 void wxDocManager::OnUpdateUndo(wxUpdateUIEvent
& event
)
998 wxDocument
*doc
= GetCurrentDocument();
999 event
.Enable( (doc
&& doc
->GetCommandProcessor() && doc
->GetCommandProcessor()->CanUndo()) );
1000 if (doc
&& doc
->GetCommandProcessor())
1001 doc
->GetCommandProcessor()->SetMenuStrings();
1004 void wxDocManager::OnUpdateRedo(wxUpdateUIEvent
& event
)
1006 wxDocument
*doc
= GetCurrentDocument();
1007 event
.Enable( (doc
&& doc
->GetCommandProcessor() && doc
->GetCommandProcessor()->CanRedo()) );
1008 if (doc
&& doc
->GetCommandProcessor())
1009 doc
->GetCommandProcessor()->SetMenuStrings();
1012 void wxDocManager::OnUpdatePrint(wxUpdateUIEvent
& event
)
1014 wxDocument
*doc
= GetCurrentDocument();
1015 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1018 void wxDocManager::OnUpdatePrintSetup(wxUpdateUIEvent
& event
)
1020 event
.Enable( TRUE
);
1023 void wxDocManager::OnUpdatePreview(wxUpdateUIEvent
& event
)
1025 wxDocument
*doc
= GetCurrentDocument();
1026 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1029 wxView
*wxDocManager::GetCurrentView() const
1032 return m_currentView
;
1033 if (m_docs
.GetCount() == 1)
1035 wxDocument
* doc
= (wxDocument
*) m_docs
.GetFirst()->GetData();
1036 return doc
->GetFirstView();
1038 return (wxView
*) NULL
;
1041 // Extend event processing to search the view's event table
1042 bool wxDocManager::ProcessEvent(wxEvent
& event
)
1044 wxView
* view
= GetCurrentView();
1047 if (view
->ProcessEvent(event
))
1050 return wxEvtHandler::ProcessEvent(event
);
1053 wxDocument
*wxDocManager::CreateDocument(const wxString
& path
, long flags
)
1055 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.GetCount()];
1058 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1060 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Item(i
)->GetData());
1061 if (temp
->IsVisible())
1063 templates
[n
] = temp
;
1070 return (wxDocument
*) NULL
;
1073 // If we've reached the max number of docs, close the
1075 if ( (int)GetDocuments().GetCount() >= m_maxDocsOpen
)
1077 wxDocument
*doc
= (wxDocument
*)GetDocuments().GetFirst()->GetData();
1080 // Implicitly deletes the document when
1081 // the last view is deleted
1082 doc
->DeleteAllViews();
1084 // Check we're really deleted
1085 if (m_docs
.Member(doc
))
1091 return (wxDocument
*) NULL
;
1095 // New document: user chooses a template, unless there's only one.
1096 if (flags
& wxDOC_NEW
)
1100 wxDocTemplate
*temp
= templates
[0];
1102 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
1105 newDoc
->SetDocumentName(temp
->GetDocumentName());
1106 newDoc
->SetDocumentTemplate(temp
);
1107 newDoc
->OnNewDocument();
1112 wxDocTemplate
*temp
= SelectDocumentType(templates
, n
);
1116 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
1119 newDoc
->SetDocumentName(temp
->GetDocumentName());
1120 newDoc
->SetDocumentTemplate(temp
);
1121 newDoc
->OnNewDocument();
1126 return (wxDocument
*) NULL
;
1129 // Existing document
1130 wxDocTemplate
*temp
= (wxDocTemplate
*) NULL
;
1132 wxString
path2(wxT(""));
1133 if (path
!= wxT(""))
1136 if (flags
& wxDOC_SILENT
)
1137 temp
= FindTemplateForPath(path2
);
1139 temp
= SelectDocumentPath(templates
, n
, path2
, flags
);
1145 wxDocument
*newDoc
= temp
->CreateDocument(path2
, flags
);
1148 newDoc
->SetDocumentName(temp
->GetDocumentName());
1149 newDoc
->SetDocumentTemplate(temp
);
1150 if (!newDoc
->OnOpenDocument(path2
))
1152 newDoc
->DeleteAllViews();
1153 // delete newDoc; // Implicitly deleted by DeleteAllViews
1154 return (wxDocument
*) NULL
;
1156 AddFileToHistory(path2
);
1161 return (wxDocument
*) NULL
;
1164 wxView
*wxDocManager::CreateView(wxDocument
*doc
, long flags
)
1166 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.GetCount()];
1169 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1171 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Item(i
)->GetData());
1172 if (temp
->IsVisible())
1174 if (temp
->GetDocumentName() == doc
->GetDocumentName())
1176 templates
[n
] = temp
;
1184 return (wxView
*) NULL
;
1188 wxDocTemplate
*temp
= templates
[0];
1190 wxView
*view
= temp
->CreateView(doc
, flags
);
1192 view
->SetViewName(temp
->GetViewName());
1196 wxDocTemplate
*temp
= SelectViewType(templates
, n
);
1200 wxView
*view
= temp
->CreateView(doc
, flags
);
1202 view
->SetViewName(temp
->GetViewName());
1206 return (wxView
*) NULL
;
1209 // Not yet implemented
1210 void wxDocManager::DeleteTemplate(wxDocTemplate
*WXUNUSED(temp
), long WXUNUSED(flags
))
1214 // Not yet implemented
1215 bool wxDocManager::FlushDoc(wxDocument
*WXUNUSED(doc
))
1220 wxDocument
*wxDocManager::GetCurrentDocument() const
1222 wxView
*view
= GetCurrentView();
1224 return view
->GetDocument();
1226 return (wxDocument
*) NULL
;
1229 // Make a default document name
1230 bool wxDocManager::MakeDefaultName(wxString
& name
)
1232 name
.Printf(_("unnamed%d"), m_defaultDocumentNameCounter
);
1233 m_defaultDocumentNameCounter
++;
1238 // Make a frame title (override this to do something different)
1239 // If docName is empty, a document is not currently active.
1240 wxString
wxDocManager::MakeFrameTitle(wxDocument
* doc
)
1242 wxString appName
= wxTheApp
->GetAppName();
1249 doc
->GetPrintableName(docName
);
1250 title
= docName
+ wxString(_(" - ")) + appName
;
1256 // Not yet implemented
1257 wxDocTemplate
*wxDocManager::MatchTemplate(const wxString
& WXUNUSED(path
))
1259 return (wxDocTemplate
*) NULL
;
1262 // File history management
1263 void wxDocManager::AddFileToHistory(const wxString
& file
)
1266 m_fileHistory
->AddFileToHistory(file
);
1269 void wxDocManager::RemoveFileFromHistory(int i
)
1272 m_fileHistory
->RemoveFileFromHistory(i
);
1275 wxString
wxDocManager::GetHistoryFile(int i
) const
1280 histFile
= m_fileHistory
->GetHistoryFile(i
);
1285 void wxDocManager::FileHistoryUseMenu(wxMenu
*menu
)
1288 m_fileHistory
->UseMenu(menu
);
1291 void wxDocManager::FileHistoryRemoveMenu(wxMenu
*menu
)
1294 m_fileHistory
->RemoveMenu(menu
);
1298 void wxDocManager::FileHistoryLoad(wxConfigBase
& config
)
1301 m_fileHistory
->Load(config
);
1304 void wxDocManager::FileHistorySave(wxConfigBase
& config
)
1307 m_fileHistory
->Save(config
);
1311 void wxDocManager::FileHistoryAddFilesToMenu(wxMenu
* menu
)
1314 m_fileHistory
->AddFilesToMenu(menu
);
1317 void wxDocManager::FileHistoryAddFilesToMenu()
1320 m_fileHistory
->AddFilesToMenu();
1323 int wxDocManager::GetNoHistoryFiles() const
1326 return m_fileHistory
->GetNoHistoryFiles();
1332 // Find out the document template via matching in the document file format
1333 // against that of the template
1334 wxDocTemplate
*wxDocManager::FindTemplateForPath(const wxString
& path
)
1336 wxDocTemplate
*theTemplate
= (wxDocTemplate
*) NULL
;
1338 // Find the template which this extension corresponds to
1339 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1341 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Item(i
)->GetData();
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
.GetFirst();
1957 wxMenu
* menu
= (wxMenu
*) node
->GetData();
1958 if ( m_fileHistoryN
== 0 && menu
->GetMenuItemCount() )
1960 menu
->AppendSeparator();
1962 menu
->Append(wxID_FILE1
+m_fileHistoryN
, _("[EMPTY]"));
1963 node
= node
->GetNext();
1967 // Shuffle filenames down
1968 for (i
= (m_fileHistoryN
-1); i
> 0; i
--)
1970 m_fileHistory
[i
] = m_fileHistory
[i
-1];
1972 m_fileHistory
[0] = copystring(file
);
1974 // this is the directory of the last opened file
1975 wxString pathCurrent
;
1976 wxSplitPath( m_fileHistory
[0], &pathCurrent
, NULL
, NULL
);
1977 for (i
= 0; i
< m_fileHistoryN
; i
++)
1979 if ( m_fileHistory
[i
] )
1981 // if in same directory just show the filename; otherwise the full
1983 wxString pathInMenu
, path
, filename
, ext
;
1984 wxSplitPath( m_fileHistory
[i
], &path
, &filename
, &ext
);
1985 if ( path
== pathCurrent
)
1987 pathInMenu
= filename
;
1989 pathInMenu
= pathInMenu
+ wxFILE_SEP_EXT
+ ext
;
1993 // absolute path; could also set relative path
1994 pathInMenu
= m_fileHistory
[i
];
1998 buf
.Printf(s_MRUEntryFormat
, i
+ 1, pathInMenu
.c_str());
1999 wxNode
* node
= m_fileMenus
.GetFirst();
2002 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2003 menu
->SetLabel(wxID_FILE1
+ i
, buf
);
2004 node
= node
->GetNext();
2010 void wxFileHistory::RemoveFileFromHistory(int i
)
2012 wxCHECK_RET( i
< m_fileHistoryN
,
2013 wxT("invalid index in wxFileHistory::RemoveFileFromHistory") );
2015 // delete the element from the array (could use memmove() too...)
2016 delete [] m_fileHistory
[i
];
2019 for ( j
= i
; j
< m_fileHistoryN
- 1; j
++ )
2021 m_fileHistory
[j
] = m_fileHistory
[j
+ 1];
2024 wxNode
* node
= m_fileMenus
.GetFirst();
2027 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2030 // shuffle filenames up
2032 for ( j
= i
; j
< m_fileHistoryN
- 1; j
++ )
2034 buf
.Printf(s_MRUEntryFormat
, j
+ 1, m_fileHistory
[j
]);
2035 menu
->SetLabel(wxID_FILE1
+ j
, buf
);
2038 node
= node
->GetNext();
2040 // delete the last menu item which is unused now
2041 if (menu
->FindItem(wxID_FILE1
+ m_fileHistoryN
- 1))
2042 menu
->Delete(wxID_FILE1
+ m_fileHistoryN
- 1);
2044 // delete the last separator too if no more files are left
2045 if ( m_fileHistoryN
== 1 )
2047 wxMenuItemList::Node
*node
= menu
->GetMenuItems().GetLast();
2050 wxMenuItem
*menuItem
= node
->GetData();
2051 if ( menuItem
->IsSeparator() )
2053 menu
->Delete(menuItem
);
2055 //else: should we search backwards for the last separator?
2057 //else: menu is empty somehow
2064 wxString
wxFileHistory::GetHistoryFile(int i
) const
2067 if ( i
< m_fileHistoryN
)
2069 s
= m_fileHistory
[i
];
2073 wxFAIL_MSG( wxT("bad index in wxFileHistory::GetHistoryFile") );
2079 void wxFileHistory::UseMenu(wxMenu
*menu
)
2081 if (!m_fileMenus
.Member(menu
))
2082 m_fileMenus
.Append(menu
);
2085 void wxFileHistory::RemoveMenu(wxMenu
*menu
)
2087 m_fileMenus
.DeleteObject(menu
);
2091 void wxFileHistory::Load(wxConfigBase
& config
)
2095 buf
.Printf(wxT("file%d"), m_fileHistoryN
+1);
2096 wxString historyFile
;
2097 while ((m_fileHistoryN
< m_fileMaxFiles
) && config
.Read(buf
, &historyFile
) && (historyFile
!= wxT("")))
2099 m_fileHistory
[m_fileHistoryN
] = copystring((const wxChar
*) historyFile
);
2101 buf
.Printf(wxT("file%d"), m_fileHistoryN
+1);
2102 historyFile
= wxT("");
2107 void wxFileHistory::Save(wxConfigBase
& config
)
2110 for (i
= 0; i
< m_fileHistoryN
; i
++)
2113 buf
.Printf(wxT("file%d"), i
+1);
2114 config
.Write(buf
, wxString(m_fileHistory
[i
]));
2117 #endif // wxUSE_CONFIG
2119 void wxFileHistory::AddFilesToMenu()
2121 if (m_fileHistoryN
> 0)
2123 wxNode
* node
= m_fileMenus
.GetFirst();
2126 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2127 if (menu
->GetMenuItemCount())
2129 menu
->AppendSeparator();
2133 for (i
= 0; i
< m_fileHistoryN
; i
++)
2135 if (m_fileHistory
[i
])
2138 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
2139 menu
->Append(wxID_FILE1
+i
, buf
);
2142 node
= node
->GetNext();
2147 void wxFileHistory::AddFilesToMenu(wxMenu
* menu
)
2149 if (m_fileHistoryN
> 0)
2151 if (menu
->GetMenuItemCount())
2153 menu
->AppendSeparator();
2157 for (i
= 0; i
< m_fileHistoryN
; i
++)
2159 if (m_fileHistory
[i
])
2162 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
2163 menu
->Append(wxID_FILE1
+i
, buf
);
2169 // ----------------------------------------------------------------------------
2170 // Permits compatibility with existing file formats and functions that
2171 // manipulate files directly
2172 // ----------------------------------------------------------------------------
2174 #if wxUSE_STD_IOSTREAM
2176 bool wxTransferFileToStream(const wxString
& filename
, wxSTD ostream
& stream
)
2178 wxFFile
file(filename
, _T("rb"));
2179 if ( !file
.IsOpened() )
2187 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2191 stream
.write(buf
, nRead
);
2195 while ( !file
.Eof() );
2200 bool wxTransferStreamToFile(wxSTD istream
& stream
, const wxString
& filename
)
2202 wxFFile
file(filename
, _T("wb"));
2203 if ( !file
.IsOpened() )
2209 stream
.read(buf
, WXSIZEOF(buf
));
2210 if ( !stream
.bad() ) // fail may be set on EOF, don't use operator!()
2212 if ( !file
.Write(buf
, stream
.gcount()) )
2216 while ( !stream
.eof() );
2221 #else // !wxUSE_STD_IOSTREAM
2223 bool wxTransferFileToStream(const wxString
& filename
, wxOutputStream
& stream
)
2225 wxFFile
file(filename
, _T("rb"));
2226 if ( !file
.IsOpened() )
2234 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2238 stream
.Write(buf
, nRead
);
2242 while ( !file
.Eof() );
2247 bool wxTransferStreamToFile(wxInputStream
& stream
, const wxString
& filename
)
2249 wxFFile
file(filename
, _T("wb"));
2250 if ( !file
.IsOpened() )
2256 stream
.Read(buf
, WXSIZEOF(buf
));
2258 const size_t nRead
= stream
.LastRead();
2259 if ( !nRead
|| !file
.Write(buf
, nRead
) )
2262 while ( !stream
.Eof() );
2267 #endif // wxUSE_STD_IOSTREAM/!wxUSE_STD_IOSTREAM
2269 #endif // wxUSE_DOC_VIEW_ARCHITECTURE