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"
50 #if wxUSE_PRINTING_ARCHITECTURE
51 #include "wx/prntbase.h"
52 #include "wx/printdlg.h"
55 #include "wx/msgdlg.h"
56 #include "wx/choicdlg.h"
57 #include "wx/docview.h"
58 #include "wx/confbase.h"
60 #include "wx/cmdproc.h"
65 #if wxUSE_STD_IOSTREAM
66 #include "wx/ioswrap.h"
73 #include "wx/wfstream.h"
76 // ----------------------------------------------------------------------------
78 // ----------------------------------------------------------------------------
80 IMPLEMENT_ABSTRACT_CLASS(wxDocument
, wxEvtHandler
)
81 IMPLEMENT_ABSTRACT_CLASS(wxView
, wxEvtHandler
)
82 IMPLEMENT_ABSTRACT_CLASS(wxDocTemplate
, wxObject
)
83 IMPLEMENT_DYNAMIC_CLASS(wxDocManager
, wxEvtHandler
)
84 IMPLEMENT_CLASS(wxDocChildFrame
, wxFrame
)
85 IMPLEMENT_CLASS(wxDocParentFrame
, wxFrame
)
87 #if wxUSE_PRINTING_ARCHITECTURE
88 IMPLEMENT_DYNAMIC_CLASS(wxDocPrintout
, wxPrintout
)
91 IMPLEMENT_DYNAMIC_CLASS(wxFileHistory
, wxObject
)
93 // ----------------------------------------------------------------------------
94 // function prototypes
95 // ----------------------------------------------------------------------------
97 static inline wxString
FindExtension(const wxChar
*path
);
99 // ----------------------------------------------------------------------------
101 // ----------------------------------------------------------------------------
103 static const wxChar
*s_MRUEntryFormat
= wxT("&%d %s");
105 // ============================================================================
107 // ============================================================================
109 // ----------------------------------------------------------------------------
111 // ----------------------------------------------------------------------------
113 static wxString
FindExtension(const wxChar
*path
)
116 wxSplitPath(path
, NULL
, NULL
, &ext
);
118 // VZ: extensions are considered not case sensitive - is this really a good
120 return ext
.MakeLower();
123 // ----------------------------------------------------------------------------
124 // Definition of wxDocument
125 // ----------------------------------------------------------------------------
127 wxDocument::wxDocument(wxDocument
*parent
)
129 m_documentModified
= FALSE
;
130 m_documentParent
= parent
;
131 m_documentTemplate
= (wxDocTemplate
*) NULL
;
132 m_commandProcessor
= (wxCommandProcessor
*) NULL
;
136 bool wxDocument::DeleteContents()
141 wxDocument::~wxDocument()
145 if (m_commandProcessor
)
146 delete m_commandProcessor
;
148 if (GetDocumentManager())
149 GetDocumentManager()->RemoveDocument(this);
151 // Not safe to do here, since it'll invoke virtual view functions
152 // expecting to see valid derived objects: and by the time we get here,
153 // we've called destructors higher up.
157 bool wxDocument::Close()
159 if (OnSaveModified())
160 return OnCloseDocument();
165 bool wxDocument::OnCloseDocument()
172 // Note that this implicitly deletes the document when the last view is
174 bool wxDocument::DeleteAllViews()
176 wxDocManager
* manager
= GetDocumentManager();
178 wxNode
*node
= m_documentViews
.First();
181 wxView
*view
= (wxView
*)node
->Data();
185 wxNode
*next
= node
->Next();
187 delete view
; // Deletes node implicitly
190 // If we haven't yet deleted the document (for example
191 // if there were no views) then delete it.
192 if (manager
&& manager
->GetDocuments().Member(this))
198 wxView
*wxDocument::GetFirstView() const
200 if (m_documentViews
.Number() == 0)
201 return (wxView
*) NULL
;
202 return (wxView
*)m_documentViews
.First()->Data();
205 wxDocManager
*wxDocument::GetDocumentManager() const
207 return (m_documentTemplate
? m_documentTemplate
->GetDocumentManager() : (wxDocManager
*) NULL
);
210 bool wxDocument::OnNewDocument()
212 if (!OnSaveModified())
215 if (OnCloseDocument()==FALSE
) return FALSE
;
218 SetDocumentSaved(FALSE
);
221 GetDocumentManager()->MakeDefaultName(name
);
223 SetFilename(name
, TRUE
);
228 bool wxDocument::Save()
232 if (!IsModified() && m_savedYet
) return TRUE
;
233 if (m_documentFile
== wxT("") || !m_savedYet
)
236 ret
= OnSaveDocument(m_documentFile
);
238 SetDocumentSaved(TRUE
);
242 bool wxDocument::SaveAs()
244 wxDocTemplate
*docTemplate
= GetDocumentTemplate();
248 wxString tmp
= wxFileSelector(_("Save as"),
249 docTemplate
->GetDirectory(),
250 wxFileNameFromPath(GetFilename()),
251 docTemplate
->GetDefaultExtension(),
252 docTemplate
->GetFileFilter(),
253 wxSAVE
| wxOVERWRITE_PROMPT
,
254 GetDocumentWindow());
259 wxString
fileName(tmp
);
260 wxString path
, name
, ext
;
261 wxSplitPath(fileName
, & path
, & name
, & ext
);
263 if (ext
.IsEmpty() || ext
== wxT(""))
266 fileName
+= docTemplate
->GetDefaultExtension();
269 SetFilename(fileName
);
270 SetTitle(wxFileNameFromPath(fileName
));
272 GetDocumentManager()->AddFileToHistory(fileName
);
274 // Notify the views that the filename has changed
275 wxNode
*node
= m_documentViews
.First();
278 wxView
*view
= (wxView
*)node
->Data();
279 view
->OnChangeFilename();
283 return OnSaveDocument(m_documentFile
);
286 bool wxDocument::OnSaveDocument(const wxString
& file
)
292 if (wxTheApp
->GetAppName() != wxT(""))
293 msgTitle
= wxTheApp
->GetAppName();
295 msgTitle
= wxString(_("File error"));
297 #if wxUSE_STD_IOSTREAM
298 wxSTD ofstream
store(wxString(file
.fn_str()).mb_str());
299 if (store
.fail() || store
.bad())
301 wxFileOutputStream
store(wxString(file
.fn_str()));
302 if (store
.LastError() != wxSTREAM_NOERROR
)
305 (void)wxMessageBox(_("Sorry, could not open this file for saving."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
306 GetDocumentWindow());
310 if (!SaveObject(store
))
312 (void)wxMessageBox(_("Sorry, could not save this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
313 GetDocumentWindow());
322 bool wxDocument::OnOpenDocument(const wxString
& file
)
324 if (!OnSaveModified())
328 if (wxTheApp
->GetAppName() != wxT(""))
329 msgTitle
= wxTheApp
->GetAppName();
331 msgTitle
= wxString(_("File error"));
333 #if wxUSE_STD_IOSTREAM
334 wxSTD ifstream
store(wxString(file
.fn_str()).mb_str());
335 if (store
.fail() || store
.bad())
337 wxFileInputStream
store(wxString(file
.fn_str()));
338 if (store
.LastError() != wxSTREAM_NOERROR
)
341 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
|wxICON_EXCLAMATION
,
342 GetDocumentWindow());
345 #if wxUSE_STD_IOSTREAM
346 if (!LoadObject(store
))
348 int res
= LoadObject(store
).LastError();
349 if ((res
!= wxSTREAM_NOERROR
) &&
350 (res
!= wxSTREAM_EOF
))
353 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
|wxICON_EXCLAMATION
,
354 GetDocumentWindow());
357 SetFilename(file
, TRUE
);
366 #if wxUSE_STD_IOSTREAM
367 wxSTD istream
& wxDocument::LoadObject(wxSTD istream
& stream
)
369 wxInputStream
& wxDocument::LoadObject(wxInputStream
& stream
)
375 #if wxUSE_STD_IOSTREAM
376 wxSTD ostream
& wxDocument::SaveObject(wxSTD ostream
& stream
)
378 wxOutputStream
& wxDocument::SaveObject(wxOutputStream
& stream
)
384 bool wxDocument::Revert()
390 // Get title, or filename if no title, else unnamed
391 bool wxDocument::GetPrintableName(wxString
& buf
) const
393 if (m_documentTitle
!= wxT(""))
395 buf
= m_documentTitle
;
398 else if (m_documentFile
!= wxT(""))
400 buf
= wxFileNameFromPath(m_documentFile
);
410 wxWindow
*wxDocument::GetDocumentWindow() const
412 wxView
*view
= GetFirstView();
414 return view
->GetFrame();
416 return wxTheApp
->GetTopWindow();
419 wxCommandProcessor
*wxDocument::OnCreateCommandProcessor()
421 return new wxCommandProcessor
;
424 // TRUE if safe to close
425 bool wxDocument::OnSaveModified()
430 GetPrintableName(title
);
433 if (wxTheApp
->GetAppName() != wxT(""))
434 msgTitle
= wxTheApp
->GetAppName();
436 msgTitle
= wxString(_("Warning"));
439 prompt
.Printf(_("Do you want to save changes to document %s?"),
440 (const wxChar
*)title
);
441 int res
= wxMessageBox(prompt
, msgTitle
,
442 wxYES_NO
|wxCANCEL
|wxICON_QUESTION
,
443 GetDocumentWindow());
449 else if (res
== wxYES
)
451 else if (res
== wxCANCEL
)
457 bool wxDocument::Draw(wxDC
& WXUNUSED(context
))
462 bool wxDocument::AddView(wxView
*view
)
464 if (!m_documentViews
.Member(view
))
466 m_documentViews
.Append(view
);
472 bool wxDocument::RemoveView(wxView
*view
)
474 (void)m_documentViews
.DeleteObject(view
);
479 bool wxDocument::OnCreate(const wxString
& WXUNUSED(path
), long flags
)
481 if (GetDocumentTemplate()->CreateView(this, flags
))
487 // Called after a view is added or removed.
488 // The default implementation deletes the document if
489 // there are no more views.
490 void wxDocument::OnChangedViewList()
492 if (m_documentViews
.Number() == 0)
494 if (OnSaveModified())
501 void wxDocument::UpdateAllViews(wxView
*sender
, wxObject
*hint
)
503 wxNode
*node
= m_documentViews
.First();
506 wxView
*view
= (wxView
*)node
->Data();
507 view
->OnUpdate(sender
, hint
);
512 void wxDocument::SetFilename(const wxString
& filename
, bool notifyViews
)
514 m_documentFile
= filename
;
517 // Notify the views that the filename has changed
518 wxNode
*node
= m_documentViews
.First();
521 wxView
*view
= (wxView
*)node
->Data();
522 view
->OnChangeFilename();
528 // ----------------------------------------------------------------------------
530 // ----------------------------------------------------------------------------
535 m_viewDocument
= (wxDocument
*) NULL
;
537 m_viewTypeName
= wxT("");
538 m_viewFrame
= (wxFrame
*) NULL
;
543 // GetDocumentManager()->ActivateView(this, FALSE, TRUE);
544 m_viewDocument
->RemoveView(this);
547 // Extend event processing to search the document's event table
548 bool wxView::ProcessEvent(wxEvent
& event
)
550 if ( !GetDocument() || !GetDocument()->ProcessEvent(event
) )
551 return wxEvtHandler::ProcessEvent(event
);
556 void wxView::OnActivateView(bool WXUNUSED(activate
), wxView
*WXUNUSED(activeView
), wxView
*WXUNUSED(deactiveView
))
560 void wxView::OnPrint(wxDC
*dc
, wxObject
*WXUNUSED(info
))
565 void wxView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
))
569 void wxView::OnChangeFilename()
571 if (GetFrame() && GetDocument())
575 GetDocument()->GetPrintableName(title
);
577 GetFrame()->SetTitle(title
);
581 void wxView::SetDocument(wxDocument
*doc
)
583 m_viewDocument
= doc
;
588 bool wxView::Close(bool deleteWindow
)
590 if (OnClose(deleteWindow
))
596 void wxView::Activate(bool activate
)
598 if (GetDocumentManager())
600 OnActivateView(activate
, this, GetDocumentManager()->GetCurrentView());
601 GetDocumentManager()->ActivateView(this, activate
);
605 bool wxView::OnClose(bool WXUNUSED(deleteWindow
))
607 return GetDocument() ? GetDocument()->Close() : TRUE
;
610 #if wxUSE_PRINTING_ARCHITECTURE
611 wxPrintout
*wxView::OnCreatePrintout()
613 return new wxDocPrintout(this);
615 #endif // wxUSE_PRINTING_ARCHITECTURE
617 // ----------------------------------------------------------------------------
619 // ----------------------------------------------------------------------------
621 wxDocTemplate::wxDocTemplate(wxDocManager
*manager
,
622 const wxString
& descr
,
623 const wxString
& filter
,
626 const wxString
& docTypeName
,
627 const wxString
& viewTypeName
,
628 wxClassInfo
*docClassInfo
,
629 wxClassInfo
*viewClassInfo
,
632 m_documentManager
= manager
;
633 m_description
= descr
;
636 m_fileFilter
= filter
;
638 m_docTypeName
= docTypeName
;
639 m_viewTypeName
= viewTypeName
;
640 m_documentManager
->AssociateTemplate(this);
642 m_docClassInfo
= docClassInfo
;
643 m_viewClassInfo
= viewClassInfo
;
646 wxDocTemplate::~wxDocTemplate()
648 m_documentManager
->DisassociateTemplate(this);
651 // Tries to dynamically construct an object of the right class.
652 wxDocument
*wxDocTemplate::CreateDocument(const wxString
& path
, long flags
)
655 return (wxDocument
*) NULL
;
656 wxDocument
*doc
= (wxDocument
*)m_docClassInfo
->CreateObject();
657 doc
->SetFilename(path
);
658 doc
->SetDocumentTemplate(this);
659 GetDocumentManager()->AddDocument(doc
);
660 doc
->SetCommandProcessor(doc
->OnCreateCommandProcessor());
662 if (doc
->OnCreate(path
, flags
))
666 if (GetDocumentManager()->GetDocuments().Member(doc
))
667 doc
->DeleteAllViews();
668 return (wxDocument
*) NULL
;
672 wxView
*wxDocTemplate::CreateView(wxDocument
*doc
, long flags
)
674 if (!m_viewClassInfo
)
675 return (wxView
*) NULL
;
676 wxView
*view
= (wxView
*)m_viewClassInfo
->CreateObject();
677 view
->SetDocument(doc
);
678 if (view
->OnCreate(doc
, flags
))
685 return (wxView
*) NULL
;
689 // The default (very primitive) format detection: check is the extension is
690 // that of the template
691 bool wxDocTemplate::FileMatchesTemplate(const wxString
& path
)
693 return GetDefaultExtension().IsSameAs(FindExtension(path
));
696 // ----------------------------------------------------------------------------
698 // ----------------------------------------------------------------------------
700 BEGIN_EVENT_TABLE(wxDocManager
, wxEvtHandler
)
701 EVT_MENU(wxID_OPEN
, wxDocManager::OnFileOpen
)
702 EVT_MENU(wxID_CLOSE
, wxDocManager::OnFileClose
)
703 EVT_MENU(wxID_REVERT
, wxDocManager::OnFileRevert
)
704 EVT_MENU(wxID_NEW
, wxDocManager::OnFileNew
)
705 EVT_MENU(wxID_SAVE
, wxDocManager::OnFileSave
)
706 EVT_MENU(wxID_SAVEAS
, wxDocManager::OnFileSaveAs
)
707 EVT_MENU(wxID_UNDO
, wxDocManager::OnUndo
)
708 EVT_MENU(wxID_REDO
, wxDocManager::OnRedo
)
710 EVT_UPDATE_UI(wxID_OPEN
, wxDocManager::OnUpdateFileOpen
)
711 EVT_UPDATE_UI(wxID_CLOSE
, wxDocManager::OnUpdateFileClose
)
712 EVT_UPDATE_UI(wxID_REVERT
, wxDocManager::OnUpdateFileRevert
)
713 EVT_UPDATE_UI(wxID_NEW
, wxDocManager::OnUpdateFileNew
)
714 EVT_UPDATE_UI(wxID_SAVE
, wxDocManager::OnUpdateFileSave
)
715 EVT_UPDATE_UI(wxID_SAVEAS
, wxDocManager::OnUpdateFileSaveAs
)
716 EVT_UPDATE_UI(wxID_UNDO
, wxDocManager::OnUpdateUndo
)
717 EVT_UPDATE_UI(wxID_REDO
, wxDocManager::OnUpdateRedo
)
719 #if wxUSE_PRINTING_ARCHITECTURE
720 EVT_MENU(wxID_PRINT
, wxDocManager::OnPrint
)
721 EVT_MENU(wxID_PRINT_SETUP
, wxDocManager::OnPrintSetup
)
722 EVT_MENU(wxID_PREVIEW
, wxDocManager::OnPreview
)
724 EVT_UPDATE_UI(wxID_PRINT
, wxDocManager::OnUpdatePrint
)
725 EVT_UPDATE_UI(wxID_PRINT_SETUP
, wxDocManager::OnUpdatePrintSetup
)
726 EVT_UPDATE_UI(wxID_PREVIEW
, wxDocManager::OnUpdatePreview
)
730 wxDocManager
* wxDocManager::sm_docManager
= (wxDocManager
*) NULL
;
732 wxDocManager::wxDocManager(long flags
, bool initialize
)
734 m_defaultDocumentNameCounter
= 1;
736 m_currentView
= (wxView
*) NULL
;
737 m_maxDocsOpen
= 10000;
738 m_fileHistory
= (wxFileHistory
*) NULL
;
741 sm_docManager
= this;
744 wxDocManager::~wxDocManager()
748 delete m_fileHistory
;
749 sm_docManager
= (wxDocManager
*) NULL
;
752 bool wxDocManager::Clear(bool force
)
754 wxNode
*node
= m_docs
.First();
757 wxDocument
*doc
= (wxDocument
*)node
->Data();
758 wxNode
*next
= node
->Next();
760 if (!doc
->Close() && !force
)
763 // Implicitly deletes the document when the last
764 // view is removed (deleted)
765 doc
->DeleteAllViews();
767 // Check document is deleted
768 if (m_docs
.Member(doc
))
771 // This assumes that documents are not connected in
772 // any way, i.e. deleting one document does NOT
776 node
= m_templates
.First();
779 wxDocTemplate
*templ
= (wxDocTemplate
*) node
->Data();
780 wxNode
* next
= node
->Next();
787 bool wxDocManager::Initialize()
789 m_fileHistory
= OnCreateFileHistory();
793 wxFileHistory
*wxDocManager::OnCreateFileHistory()
795 return new wxFileHistory
;
798 void wxDocManager::OnFileClose(wxCommandEvent
& WXUNUSED(event
))
800 wxDocument
*doc
= GetCurrentDocument();
805 doc
->DeleteAllViews();
806 if (m_docs
.Member(doc
))
811 void wxDocManager::OnFileNew(wxCommandEvent
& WXUNUSED(event
))
813 CreateDocument(wxString(""), wxDOC_NEW
);
816 void wxDocManager::OnFileOpen(wxCommandEvent
& WXUNUSED(event
))
818 if ( !CreateDocument(wxString(""), 0) )
824 void wxDocManager::OnFileRevert(wxCommandEvent
& WXUNUSED(event
))
826 wxDocument
*doc
= GetCurrentDocument();
832 void wxDocManager::OnFileSave(wxCommandEvent
& WXUNUSED(event
))
834 wxDocument
*doc
= GetCurrentDocument();
840 void wxDocManager::OnFileSaveAs(wxCommandEvent
& WXUNUSED(event
))
842 wxDocument
*doc
= GetCurrentDocument();
848 void wxDocManager::OnPrint(wxCommandEvent
& WXUNUSED(event
))
850 #if wxUSE_PRINTING_ARCHITECTURE
851 wxView
*view
= GetCurrentView();
855 wxPrintout
*printout
= view
->OnCreatePrintout();
859 printer
.Print(view
->GetFrame(), printout
, TRUE
);
863 #endif // wxUSE_PRINTING_ARCHITECTURE
866 void wxDocManager::OnPrintSetup(wxCommandEvent
& WXUNUSED(event
))
868 #if wxUSE_PRINTING_ARCHITECTURE
869 wxWindow
*parentWin
= wxTheApp
->GetTopWindow();
870 wxView
*view
= GetCurrentView();
872 parentWin
= view
->GetFrame();
874 wxPrintDialogData data
;
876 wxPrintDialog
printerDialog(parentWin
, &data
);
877 printerDialog
.GetPrintDialogData().SetSetupDialog(TRUE
);
878 printerDialog
.ShowModal();
879 #endif // wxUSE_PRINTING_ARCHITECTURE
882 void wxDocManager::OnPreview(wxCommandEvent
& WXUNUSED(event
))
884 #if wxUSE_PRINTING_ARCHITECTURE
885 wxView
*view
= GetCurrentView();
889 wxPrintout
*printout
= view
->OnCreatePrintout();
892 // Pass two printout objects: for preview, and possible printing.
893 wxPrintPreviewBase
*preview
= (wxPrintPreviewBase
*) NULL
;
894 preview
= new wxPrintPreview(printout
, view
->OnCreatePrintout());
896 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, (wxFrame
*)wxTheApp
->GetTopWindow(), _("Print Preview"),
897 wxPoint(100, 100), wxSize(600, 650));
898 frame
->Centre(wxBOTH
);
902 #endif // wxUSE_PRINTING_ARCHITECTURE
905 void wxDocManager::OnUndo(wxCommandEvent
& WXUNUSED(event
))
907 wxDocument
*doc
= GetCurrentDocument();
910 if (doc
->GetCommandProcessor())
911 doc
->GetCommandProcessor()->Undo();
914 void wxDocManager::OnRedo(wxCommandEvent
& WXUNUSED(event
))
916 wxDocument
*doc
= GetCurrentDocument();
919 if (doc
->GetCommandProcessor())
920 doc
->GetCommandProcessor()->Redo();
923 // Handlers for UI update commands
925 void wxDocManager::OnUpdateFileOpen(wxUpdateUIEvent
& event
)
927 event
.Enable( TRUE
);
930 void wxDocManager::OnUpdateFileClose(wxUpdateUIEvent
& event
)
932 wxDocument
*doc
= GetCurrentDocument();
933 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
936 void wxDocManager::OnUpdateFileRevert(wxUpdateUIEvent
& event
)
938 wxDocument
*doc
= GetCurrentDocument();
939 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
942 void wxDocManager::OnUpdateFileNew(wxUpdateUIEvent
& event
)
944 event
.Enable( TRUE
);
947 void wxDocManager::OnUpdateFileSave(wxUpdateUIEvent
& event
)
949 wxDocument
*doc
= GetCurrentDocument();
950 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
953 void wxDocManager::OnUpdateFileSaveAs(wxUpdateUIEvent
& event
)
955 wxDocument
*doc
= GetCurrentDocument();
956 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
959 void wxDocManager::OnUpdateUndo(wxUpdateUIEvent
& event
)
961 wxDocument
*doc
= GetCurrentDocument();
962 event
.Enable( (doc
&& doc
->GetCommandProcessor() && doc
->GetCommandProcessor()->CanUndo()) );
965 void wxDocManager::OnUpdateRedo(wxUpdateUIEvent
& event
)
967 wxDocument
*doc
= GetCurrentDocument();
968 event
.Enable( (doc
&& doc
->GetCommandProcessor() && doc
->GetCommandProcessor()->CanRedo()) );
971 void wxDocManager::OnUpdatePrint(wxUpdateUIEvent
& event
)
973 wxDocument
*doc
= GetCurrentDocument();
974 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
977 void wxDocManager::OnUpdatePrintSetup(wxUpdateUIEvent
& event
)
979 event
.Enable( TRUE
);
982 void wxDocManager::OnUpdatePreview(wxUpdateUIEvent
& event
)
984 wxDocument
*doc
= GetCurrentDocument();
985 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
988 wxView
*wxDocManager::GetCurrentView() const
991 return m_currentView
;
992 if (m_docs
.Number() == 1)
994 wxDocument
* doc
= (wxDocument
*) m_docs
.First()->Data();
995 return doc
->GetFirstView();
997 return (wxView
*) NULL
;
1000 // Extend event processing to search the view's event table
1001 bool wxDocManager::ProcessEvent(wxEvent
& event
)
1003 wxView
* view
= GetCurrentView();
1006 if (view
->ProcessEvent(event
))
1009 return wxEvtHandler::ProcessEvent(event
);
1012 wxDocument
*wxDocManager::CreateDocument(const wxString
& path
, long flags
)
1014 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.Number()];
1017 for (i
= 0; i
< m_templates
.Number(); i
++)
1019 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Nth(i
)->Data());
1020 if (temp
->IsVisible())
1022 templates
[n
] = temp
;
1029 return (wxDocument
*) NULL
;
1032 // If we've reached the max number of docs, close the
1034 if (GetDocuments().Number() >= m_maxDocsOpen
)
1036 wxDocument
*doc
= (wxDocument
*)GetDocuments().First()->Data();
1039 // Implicitly deletes the document when
1040 // the last view is deleted
1041 doc
->DeleteAllViews();
1043 // Check we're really deleted
1044 if (m_docs
.Member(doc
))
1048 return (wxDocument
*) NULL
;
1051 // New document: user chooses a template, unless there's only one.
1052 if (flags
& wxDOC_NEW
)
1056 wxDocTemplate
*temp
= templates
[0];
1058 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
1061 newDoc
->SetDocumentName(temp
->GetDocumentName());
1062 newDoc
->SetDocumentTemplate(temp
);
1063 newDoc
->OnNewDocument();
1068 wxDocTemplate
*temp
= SelectDocumentType(templates
, n
);
1072 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
1075 newDoc
->SetDocumentName(temp
->GetDocumentName());
1076 newDoc
->SetDocumentTemplate(temp
);
1077 newDoc
->OnNewDocument();
1082 return (wxDocument
*) NULL
;
1085 // Existing document
1086 wxDocTemplate
*temp
= (wxDocTemplate
*) NULL
;
1088 wxString
path2(wxT(""));
1089 if (path
!= wxT(""))
1092 if (flags
& wxDOC_SILENT
)
1093 temp
= FindTemplateForPath(path2
);
1095 temp
= SelectDocumentPath(templates
, n
, path2
, flags
);
1101 wxDocument
*newDoc
= temp
->CreateDocument(path2
, flags
);
1104 newDoc
->SetDocumentName(temp
->GetDocumentName());
1105 newDoc
->SetDocumentTemplate(temp
);
1106 if (!newDoc
->OnOpenDocument(path2
))
1108 newDoc
->DeleteAllViews();
1109 // delete newDoc; // Implicitly deleted by DeleteAllViews
1110 return (wxDocument
*) NULL
;
1112 AddFileToHistory(path2
);
1117 return (wxDocument
*) NULL
;
1120 wxView
*wxDocManager::CreateView(wxDocument
*doc
, long flags
)
1122 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.Number()];
1125 for (i
= 0; i
< m_templates
.Number(); i
++)
1127 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Nth(i
)->Data());
1128 if (temp
->IsVisible())
1130 if (temp
->GetDocumentName() == doc
->GetDocumentName())
1132 templates
[n
] = temp
;
1140 return (wxView
*) NULL
;
1144 wxDocTemplate
*temp
= templates
[0];
1146 wxView
*view
= temp
->CreateView(doc
, flags
);
1148 view
->SetViewName(temp
->GetViewName());
1152 wxDocTemplate
*temp
= SelectViewType(templates
, n
);
1156 wxView
*view
= temp
->CreateView(doc
, flags
);
1158 view
->SetViewName(temp
->GetViewName());
1162 return (wxView
*) NULL
;
1165 // Not yet implemented
1166 void wxDocManager::DeleteTemplate(wxDocTemplate
*WXUNUSED(temp
), long WXUNUSED(flags
))
1170 // Not yet implemented
1171 bool wxDocManager::FlushDoc(wxDocument
*WXUNUSED(doc
))
1176 wxDocument
*wxDocManager::GetCurrentDocument() const
1179 return m_currentView
->GetDocument();
1181 return (wxDocument
*) NULL
;
1184 // Make a default document name
1185 bool wxDocManager::MakeDefaultName(wxString
& name
)
1187 name
.Printf(_("unnamed%d"), m_defaultDocumentNameCounter
);
1188 m_defaultDocumentNameCounter
++;
1193 // Make a frame title (override this to do something different)
1194 // If docName is empty, a document is not currently active.
1195 wxString
wxDocManager::MakeFrameTitle(wxDocument
* doc
)
1197 wxString appName
= wxTheApp
->GetAppName();
1204 doc
->GetPrintableName(docName
);
1205 title
= docName
+ wxString(_(" - ")) + appName
;
1211 // Not yet implemented
1212 wxDocTemplate
*wxDocManager::MatchTemplate(const wxString
& WXUNUSED(path
))
1214 return (wxDocTemplate
*) NULL
;
1217 // File history management
1218 void wxDocManager::AddFileToHistory(const wxString
& file
)
1221 m_fileHistory
->AddFileToHistory(file
);
1224 void wxDocManager::RemoveFileFromHistory(int i
)
1227 m_fileHistory
->RemoveFileFromHistory(i
);
1230 wxString
wxDocManager::GetHistoryFile(int i
) const
1235 histFile
= m_fileHistory
->GetHistoryFile(i
);
1240 void wxDocManager::FileHistoryUseMenu(wxMenu
*menu
)
1243 m_fileHistory
->UseMenu(menu
);
1246 void wxDocManager::FileHistoryRemoveMenu(wxMenu
*menu
)
1249 m_fileHistory
->RemoveMenu(menu
);
1253 void wxDocManager::FileHistoryLoad(wxConfigBase
& config
)
1256 m_fileHistory
->Load(config
);
1259 void wxDocManager::FileHistorySave(wxConfigBase
& config
)
1262 m_fileHistory
->Save(config
);
1266 void wxDocManager::FileHistoryAddFilesToMenu(wxMenu
* menu
)
1269 m_fileHistory
->AddFilesToMenu(menu
);
1272 void wxDocManager::FileHistoryAddFilesToMenu()
1275 m_fileHistory
->AddFilesToMenu();
1278 int wxDocManager::GetNoHistoryFiles() const
1281 return m_fileHistory
->GetNoHistoryFiles();
1287 // Find out the document template via matching in the document file format
1288 // against that of the template
1289 wxDocTemplate
*wxDocManager::FindTemplateForPath(const wxString
& path
)
1291 wxDocTemplate
*theTemplate
= (wxDocTemplate
*) NULL
;
1293 // Find the template which this extension corresponds to
1295 for (i
= 0; i
< m_templates
.Number(); i
++)
1297 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Nth(i
)->Data();
1298 if ( temp
->FileMatchesTemplate(path
) )
1307 // Try to get a more suitable parent frame than the top window,
1308 // for selection dialogs. Otherwise you may get an unexpected
1309 // window being activated when a dialog is shown.
1310 static wxWindow
* wxFindSuitableParent()
1312 wxWindow
* parent
= wxTheApp
->GetTopWindow();
1314 wxWindow
* focusWindow
= wxWindow::FindFocus();
1317 while (focusWindow
&&
1318 !focusWindow
->IsKindOf(CLASSINFO(wxDialog
)) &&
1319 !focusWindow
->IsKindOf(CLASSINFO(wxFrame
)))
1321 focusWindow
= focusWindow
->GetParent();
1324 parent
= focusWindow
;
1329 // Prompts user to open a file, using file specs in templates.
1330 // How to implement in wxWindows? Must extend the file selector
1331 // dialog or implement own; OR match the extension to the
1332 // template extension.
1334 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**templates
,
1335 #if defined(__WXMSW__) || defined(__WXGTK__)
1338 int WXUNUSED(noTemplates
),
1341 long WXUNUSED(flags
),
1342 bool WXUNUSED(save
))
1344 // We can only have multiple filters in Windows and GTK
1345 #if defined(__WXMSW__) || defined(__WXGTK__)
1349 for (i
= 0; i
< noTemplates
; i
++)
1351 if (templates
[i
]->IsVisible())
1353 // add a '|' to separate this filter from the previous one
1354 if ( !descrBuf
.IsEmpty() )
1355 descrBuf
<< wxT('|');
1357 descrBuf
<< templates
[i
]->GetDescription()
1358 << wxT(" (") << templates
[i
]->GetFileFilter() << wxT(") |")
1359 << templates
[i
]->GetFileFilter();
1363 wxString descrBuf
= wxT("*.*");
1366 int FilterIndex
= -1;
1368 wxWindow
* parent
= wxFindSuitableParent();
1370 wxString pathTmp
= wxFileSelectorEx(_("Select a file"),
1378 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)NULL
;
1379 if (!pathTmp
.IsEmpty())
1381 if (!wxFileExists(pathTmp
))
1384 if (!wxTheApp
->GetAppName().IsEmpty())
1385 msgTitle
= wxTheApp
->GetAppName();
1387 msgTitle
= wxString(_("File error"));
1389 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
1393 return (wxDocTemplate
*) NULL
;
1395 m_lastDirectory
= wxPathOnly(pathTmp
);
1399 // first choose the template using the extension, if this fails (i.e.
1400 // wxFileSelectorEx() didn't fill it), then use the path
1401 if ( FilterIndex
!= -1 )
1402 theTemplate
= templates
[FilterIndex
];
1404 theTemplate
= FindTemplateForPath(path
);
1414 // In all other windowing systems, until we have more advanced
1415 // file selectors, we must select the document type (template) first, and
1416 // _then_ pop up the file selector.
1417 wxDocTemplate
*temp
= SelectDocumentType(templates
, noTemplates
);
1419 return (wxDocTemplate
*) NULL
;
1421 wxChar
*pathTmp
= wxFileSelector(_("Select a file"), wxT(""), wxT(""),
1422 temp
->GetDefaultExtension(),
1423 temp
->GetFileFilter(),
1424 0, wxTheApp
->GetTopWindow());
1432 return (wxDocTemplate
*) NULL
;
1436 wxDocTemplate
*wxDocManager::SelectDocumentType(wxDocTemplate
**templates
,
1437 int noTemplates
, bool sort
)
1439 wxArrayString
strings(sort
);
1440 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1443 for (i
= 0; i
< noTemplates
; i
++)
1445 if (templates
[i
]->IsVisible())
1447 strings
.Add(templates
[i
]->m_description
);
1450 data
[n
] = templates
[i
];
1458 // Yes, this will be slow, but template lists
1459 // are typically short.
1461 n
= strings
.Count();
1462 for (i
= 0; i
< n
; i
++)
1464 for (j
= 0; j
< noTemplates
; j
++)
1466 if (strings
[i
] == templates
[j
]->m_description
)
1467 data
[i
] = templates
[j
];
1472 wxDocTemplate
*theTemplate
;
1477 // no visible templates, hence nothing to choose from
1482 // don't propose the user to choose if he heas no choice
1483 theTemplate
= data
[0];
1487 // propose the user to choose one of several
1488 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1490 _("Select a document template"),
1494 wxFindSuitableParent()
1503 wxDocTemplate
*wxDocManager::SelectViewType(wxDocTemplate
**templates
,
1504 int noTemplates
, bool sort
)
1506 wxArrayString
strings(sort
);
1507 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1510 for (i
= 0; i
< noTemplates
; i
++)
1512 wxDocTemplate
*templ
= templates
[i
];
1513 if ( templ
->IsVisible() && !templ
->GetViewName().empty() )
1515 strings
.Add(templ
->m_viewTypeName
);
1526 // Yes, this will be slow, but template lists
1527 // are typically short.
1529 n
= strings
.Count();
1530 for (i
= 0; i
< n
; i
++)
1532 for (j
= 0; j
< noTemplates
; j
++)
1534 if (strings
[i
] == templates
[j
]->m_viewTypeName
)
1535 data
[i
] = templates
[j
];
1540 wxDocTemplate
*theTemplate
;
1542 // the same logic as above
1546 theTemplate
= (wxDocTemplate
*)NULL
;
1550 theTemplate
= data
[0];
1554 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1556 _("Select a document view"),
1560 wxFindSuitableParent()
1569 void wxDocManager::AssociateTemplate(wxDocTemplate
*temp
)
1571 if (!m_templates
.Member(temp
))
1572 m_templates
.Append(temp
);
1575 void wxDocManager::DisassociateTemplate(wxDocTemplate
*temp
)
1577 m_templates
.DeleteObject(temp
);
1580 // Add and remove a document from the manager's list
1581 void wxDocManager::AddDocument(wxDocument
*doc
)
1583 if (!m_docs
.Member(doc
))
1587 void wxDocManager::RemoveDocument(wxDocument
*doc
)
1589 m_docs
.DeleteObject(doc
);
1592 // Views or windows should inform the document manager
1593 // when a view is going in or out of focus
1594 void wxDocManager::ActivateView(wxView
*view
, bool activate
, bool WXUNUSED(deleting
))
1596 // If we're deactiving, and if we're not actually deleting the view, then
1597 // don't reset the current view because we may be going to
1598 // a window without a view.
1599 // WHAT DID I MEAN BY THAT EXACTLY?
1603 if (m_currentView == view)
1604 m_currentView = NULL;
1610 m_currentView
= view
;
1612 m_currentView
= (wxView
*) NULL
;
1616 // ----------------------------------------------------------------------------
1617 // Default document child frame
1618 // ----------------------------------------------------------------------------
1620 BEGIN_EVENT_TABLE(wxDocChildFrame
, wxFrame
)
1621 EVT_ACTIVATE(wxDocChildFrame::OnActivate
)
1622 EVT_CLOSE(wxDocChildFrame::OnCloseWindow
)
1625 wxDocChildFrame::wxDocChildFrame(wxDocument
*doc
,
1629 const wxString
& title
,
1633 const wxString
& name
)
1634 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1636 m_childDocument
= doc
;
1639 view
->SetFrame(this);
1642 wxDocChildFrame::~wxDocChildFrame()
1646 // Extend event processing to search the view's event table
1647 bool wxDocChildFrame::ProcessEvent(wxEvent
& event
)
1650 m_childView
->Activate(TRUE
);
1652 if ( !m_childView
|| ! m_childView
->ProcessEvent(event
) )
1654 // Only hand up to the parent if it's a menu command
1655 if (!event
.IsKindOf(CLASSINFO(wxCommandEvent
)) || !GetParent() || !GetParent()->ProcessEvent(event
))
1656 return wxEvtHandler::ProcessEvent(event
);
1664 void wxDocChildFrame::OnActivate(wxActivateEvent
& event
)
1666 wxFrame::OnActivate(event
);
1669 m_childView
->Activate(event
.GetActive());
1672 void wxDocChildFrame::OnCloseWindow(wxCloseEvent
& event
)
1677 if (!event
.CanVeto())
1678 ans
= TRUE
; // Must delete.
1680 ans
= m_childView
->Close(FALSE
); // FALSE means don't delete associated window
1684 m_childView
->Activate(FALSE
);
1686 m_childView
= (wxView
*) NULL
;
1687 m_childDocument
= (wxDocument
*) NULL
;
1698 // ----------------------------------------------------------------------------
1699 // Default parent frame
1700 // ----------------------------------------------------------------------------
1702 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1703 EVT_MENU(wxID_EXIT
, wxDocParentFrame::OnExit
)
1704 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE9
, wxDocParentFrame::OnMRUFile
)
1705 EVT_CLOSE(wxDocParentFrame::OnCloseWindow
)
1708 wxDocParentFrame::wxDocParentFrame(wxDocManager
*manager
,
1711 const wxString
& title
,
1715 const wxString
& name
)
1716 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1718 m_docManager
= manager
;
1721 void wxDocParentFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
1726 void wxDocParentFrame::OnMRUFile(wxCommandEvent
& event
)
1728 int n
= event
.GetId() - wxID_FILE1
; // the index in MRU list
1729 wxString
filename(m_docManager
->GetHistoryFile(n
));
1730 if ( !filename
.IsEmpty() )
1732 // verify that the file exists before doing anything else
1733 if ( wxFile::Exists(filename
) )
1736 (void)m_docManager
->CreateDocument(filename
, wxDOC_SILENT
);
1740 // remove the bogus filename from the MRU list and notify the user
1742 m_docManager
->RemoveFileFromHistory(n
);
1744 wxLogError(_("The file '%s' doesn't exist and couldn't be opened.\nIt has been removed from the most recently used files list."),
1750 // Extend event processing to search the view's event table
1751 bool wxDocParentFrame::ProcessEvent(wxEvent
& event
)
1753 // Try the document manager, then do default processing
1754 if (!m_docManager
|| !m_docManager
->ProcessEvent(event
))
1755 return wxEvtHandler::ProcessEvent(event
);
1760 // Define the behaviour for the frame closing
1761 // - must delete all frames except for the main one.
1762 void wxDocParentFrame::OnCloseWindow(wxCloseEvent
& event
)
1764 if (m_docManager
->Clear(!event
.CanVeto()))
1772 #if wxUSE_PRINTING_ARCHITECTURE
1774 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
)
1777 m_printoutView
= view
;
1780 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
1784 // Get the logical pixels per inch of screen and printer
1785 int ppiScreenX
, ppiScreenY
;
1786 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
1787 int ppiPrinterX
, ppiPrinterY
;
1788 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
1790 // This scales the DC so that the printout roughly represents the
1791 // the screen scaling. The text point size _should_ be the right size
1792 // but in fact is too small for some reason. This is a detail that will
1793 // need to be addressed at some point but can be fudged for the
1795 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
1797 // Now we have to check in case our real page size is reduced
1798 // (e.g. because we're drawing to a print preview memory DC)
1799 int pageWidth
, pageHeight
;
1801 dc
->GetSize(&w
, &h
);
1802 GetPageSizePixels(&pageWidth
, &pageHeight
);
1804 // If printer pageWidth == current DC width, then this doesn't
1805 // change. But w might be the preview bitmap width, so scale down.
1806 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
1807 dc
->SetUserScale(overallScale
, overallScale
);
1811 m_printoutView
->OnDraw(dc
);
1816 bool wxDocPrintout::HasPage(int pageNum
)
1818 return (pageNum
== 1);
1821 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
1823 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
1829 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
1837 #endif // wxUSE_PRINTING_ARCHITECTURE
1839 // ----------------------------------------------------------------------------
1840 // File history processor
1841 // ----------------------------------------------------------------------------
1843 wxFileHistory::wxFileHistory(int maxFiles
)
1845 m_fileMaxFiles
= maxFiles
;
1847 m_fileHistory
= new wxChar
*[m_fileMaxFiles
];
1850 wxFileHistory::~wxFileHistory()
1853 for (i
= 0; i
< m_fileHistoryN
; i
++)
1854 delete[] m_fileHistory
[i
];
1855 delete[] m_fileHistory
;
1858 // File history management
1859 void wxFileHistory::AddFileToHistory(const wxString
& file
)
1863 // Check we don't already have this file
1864 for (i
= 0; i
< m_fileHistoryN
; i
++)
1866 if ( m_fileHistory
[i
] && (file
== m_fileHistory
[i
]) )
1868 // we do have it, move it to the top of the history
1869 RemoveFileFromHistory (i
);
1870 AddFileToHistory (file
);
1875 // if we already have a full history, delete the one at the end
1876 if ( m_fileMaxFiles
== m_fileHistoryN
)
1878 RemoveFileFromHistory (m_fileHistoryN
- 1);
1879 AddFileToHistory (file
);
1883 // Add to the project file history:
1884 // Move existing files (if any) down so we can insert file at beginning.
1885 if (m_fileHistoryN
< m_fileMaxFiles
)
1887 wxNode
* node
= m_fileMenus
.First();
1890 wxMenu
* menu
= (wxMenu
*) node
->Data();
1891 if (m_fileHistoryN
== 0)
1892 menu
->AppendSeparator();
1893 menu
->Append(wxID_FILE1
+m_fileHistoryN
, _("[EMPTY]"));
1894 node
= node
->Next();
1898 // Shuffle filenames down
1899 for (i
= (m_fileHistoryN
-1); i
> 0; i
--)
1901 m_fileHistory
[i
] = m_fileHistory
[i
-1];
1903 m_fileHistory
[0] = copystring(file
);
1905 // this is the directory of the last opened file
1906 wxString pathCurrent
;
1907 wxSplitPath( m_fileHistory
[0], &pathCurrent
, NULL
, NULL
);
1908 for (i
= 0; i
< m_fileHistoryN
; i
++)
1910 if ( m_fileHistory
[i
] )
1912 // if in same directory just show the filename; otherwise the full
1914 wxString pathInMenu
, path
, filename
, ext
;
1915 wxSplitPath( m_fileHistory
[i
], &path
, &filename
, &ext
);
1916 if ( path
== pathCurrent
)
1918 pathInMenu
= filename
;
1920 pathInMenu
= pathInMenu
+ wxFILE_SEP_EXT
+ ext
;
1924 // absolute path; could also set relative path
1925 pathInMenu
= m_fileHistory
[i
];
1929 buf
.Printf(s_MRUEntryFormat
, i
+ 1, pathInMenu
.c_str());
1930 wxNode
* node
= m_fileMenus
.First();
1933 wxMenu
* menu
= (wxMenu
*) node
->Data();
1934 menu
->SetLabel(wxID_FILE1
+ i
, buf
);
1935 node
= node
->Next();
1941 void wxFileHistory::RemoveFileFromHistory(int i
)
1943 wxCHECK_RET( i
< m_fileHistoryN
,
1944 wxT("invalid index in wxFileHistory::RemoveFileFromHistory") );
1946 // delete the element from the array (could use memmove() too...)
1947 delete [] m_fileHistory
[i
];
1950 for ( j
= i
; j
< m_fileHistoryN
- 1; j
++ )
1952 m_fileHistory
[j
] = m_fileHistory
[j
+ 1];
1955 wxNode
* node
= m_fileMenus
.First();
1958 wxMenu
* menu
= (wxMenu
*) node
->Data();
1961 // shuffle filenames up
1963 for ( j
= i
; j
< m_fileHistoryN
- 1; j
++ )
1965 buf
.Printf(s_MRUEntryFormat
, j
+ 1, m_fileHistory
[j
]);
1966 menu
->SetLabel(wxID_FILE1
+ j
, buf
);
1969 node
= node
->Next();
1971 // delete the last menu item which is unused now
1972 if (menu
->FindItem(wxID_FILE1
+ m_fileHistoryN
- 1))
1973 menu
->Delete(wxID_FILE1
+ m_fileHistoryN
- 1);
1975 // delete the last separator too if no more files are left
1976 if ( m_fileHistoryN
== 1 )
1978 wxMenuItemList::Node
*node
= menu
->GetMenuItems().GetLast();
1981 wxMenuItem
*menuItem
= node
->GetData();
1982 if ( menuItem
->IsSeparator() )
1984 menu
->Delete(menuItem
);
1986 //else: should we search backwards for the last separator?
1988 //else: menu is empty somehow
1995 wxString
wxFileHistory::GetHistoryFile(int i
) const
1998 if ( i
< m_fileHistoryN
)
2000 s
= m_fileHistory
[i
];
2004 wxFAIL_MSG( wxT("bad index in wxFileHistory::GetHistoryFile") );
2010 void wxFileHistory::UseMenu(wxMenu
*menu
)
2012 if (!m_fileMenus
.Member(menu
))
2013 m_fileMenus
.Append(menu
);
2016 void wxFileHistory::RemoveMenu(wxMenu
*menu
)
2018 m_fileMenus
.DeleteObject(menu
);
2022 void wxFileHistory::Load(wxConfigBase
& config
)
2026 buf
.Printf(wxT("file%d"), m_fileHistoryN
+1);
2027 wxString historyFile
;
2028 while ((m_fileHistoryN
<= m_fileMaxFiles
) && config
.Read(buf
, &historyFile
) && (historyFile
!= wxT("")))
2030 m_fileHistory
[m_fileHistoryN
] = copystring((const wxChar
*) historyFile
);
2032 buf
.Printf(wxT("file%d"), m_fileHistoryN
+1);
2033 historyFile
= wxT("");
2038 void wxFileHistory::Save(wxConfigBase
& config
)
2041 for (i
= 0; i
< m_fileHistoryN
; i
++)
2044 buf
.Printf(wxT("file%d"), i
+1);
2045 config
.Write(buf
, wxString(m_fileHistory
[i
]));
2048 #endif // wxUSE_CONFIG
2050 void wxFileHistory::AddFilesToMenu()
2052 if (m_fileHistoryN
> 0)
2054 wxNode
* node
= m_fileMenus
.First();
2057 wxMenu
* menu
= (wxMenu
*) node
->Data();
2058 menu
->AppendSeparator();
2060 for (i
= 0; i
< m_fileHistoryN
; i
++)
2062 if (m_fileHistory
[i
])
2065 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
2066 menu
->Append(wxID_FILE1
+i
, buf
);
2069 node
= node
->Next();
2074 void wxFileHistory::AddFilesToMenu(wxMenu
* menu
)
2076 if (m_fileHistoryN
> 0)
2078 menu
->AppendSeparator();
2080 for (i
= 0; i
< m_fileHistoryN
; i
++)
2082 if (m_fileHistory
[i
])
2085 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
2086 menu
->Append(wxID_FILE1
+i
, buf
);
2092 // ----------------------------------------------------------------------------
2093 // Permits compatibility with existing file formats and functions that
2094 // manipulate files directly
2095 // ----------------------------------------------------------------------------
2097 #if wxUSE_STD_IOSTREAM
2098 bool wxTransferFileToStream(const wxString
& filename
, wxSTD ostream
& stream
)
2103 if ((fd1
= wxFopen (filename
.fn_str(), _T("rb"))) == NULL
)
2106 while ((ch
= getc (fd1
)) != EOF
)
2107 stream
<< (unsigned char)ch
;
2113 bool wxTransferStreamToFile(wxSTD istream
& stream
, const wxString
& filename
)
2118 if ((fd1
= wxFopen (filename
.fn_str(), _T("wb"))) == NULL
)
2123 while (!stream
.eof())
2133 bool wxTransferFileToStream(const wxString
& filename
, wxOutputStream
& stream
)
2138 if ((fd1
= wxFopen (filename
, wxT("rb"))) == NULL
)
2141 while ((ch
= getc (fd1
)) != EOF
)
2142 stream
.PutC((char) ch
);
2148 bool wxTransferStreamToFile(wxInputStream
& stream
, const wxString
& filename
)
2153 if ((fd1
= wxFopen (filename
, wxT("wb"))) == NULL
)
2158 int len
= stream
.StreamSize();
2159 // TODO: is this the correct test for EOF?
2160 while (stream
.TellI() < (len
- 1))
2170 #endif // wxUSE_DOC_VIEW_ARCHITECTURE