1 /////////////////////////////////////////////////////////////////////////////
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"
64 #if wxUSE_STD_IOSTREAM
65 #include "wx/ioswrap.h"
72 #include "wx/wfstream.h"
75 // ----------------------------------------------------------------------------
77 // ----------------------------------------------------------------------------
79 IMPLEMENT_ABSTRACT_CLASS(wxDocument
, wxEvtHandler
)
80 IMPLEMENT_ABSTRACT_CLASS(wxView
, wxEvtHandler
)
81 IMPLEMENT_ABSTRACT_CLASS(wxDocTemplate
, wxObject
)
82 IMPLEMENT_DYNAMIC_CLASS(wxDocManager
, wxEvtHandler
)
83 IMPLEMENT_CLASS(wxDocChildFrame
, wxFrame
)
84 IMPLEMENT_CLASS(wxDocParentFrame
, wxFrame
)
86 #if wxUSE_PRINTING_ARCHITECTURE
87 IMPLEMENT_DYNAMIC_CLASS(wxDocPrintout
, wxPrintout
)
90 IMPLEMENT_CLASS(wxCommand
, wxObject
)
91 IMPLEMENT_DYNAMIC_CLASS(wxCommandProcessor
, wxObject
)
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
;
136 bool wxDocument::DeleteContents()
141 wxDocument::~wxDocument()
145 if (m_commandProcessor
)
146 delete m_commandProcessor
;
148 GetDocumentManager()->RemoveDocument(this);
150 // Not safe to do here, since it'll invoke virtual view functions
151 // expecting to see valid derived objects: and by the time we get here,
152 // we've called destructors higher up.
156 bool wxDocument::Close()
158 if (OnSaveModified())
159 return OnCloseDocument();
164 bool wxDocument::OnCloseDocument()
171 // Note that this implicitly deletes the document when the last view is
173 bool wxDocument::DeleteAllViews()
175 wxNode
*node
= m_documentViews
.First();
178 wxView
*view
= (wxView
*)node
->Data();
182 wxNode
*next
= node
->Next();
184 delete view
; // Deletes node implicitly
190 wxView
*wxDocument::GetFirstView() const
192 if (m_documentViews
.Number() == 0)
193 return (wxView
*) NULL
;
194 return (wxView
*)m_documentViews
.First()->Data();
197 wxDocManager
*wxDocument::GetDocumentManager() const
199 return m_documentTemplate
->GetDocumentManager();
202 bool wxDocument::OnNewDocument()
204 if (!OnSaveModified())
207 if (OnCloseDocument()==FALSE
) return FALSE
;
210 SetDocumentSaved(FALSE
);
213 GetDocumentManager()->MakeDefaultName(name
);
215 SetFilename(name
, TRUE
);
220 bool wxDocument::Save()
224 if (!IsModified()) return TRUE
;
225 if (m_documentFile
== wxT("") || !m_savedYet
)
228 ret
= OnSaveDocument(m_documentFile
);
230 SetDocumentSaved(TRUE
);
234 bool wxDocument::SaveAs()
236 wxDocTemplate
*docTemplate
= GetDocumentTemplate();
240 wxString tmp
= wxFileSelector(_("Save as"),
241 docTemplate
->GetDirectory(),
243 docTemplate
->GetDefaultExtension(),
244 docTemplate
->GetFileFilter(),
245 wxSAVE
| wxOVERWRITE_PROMPT
,
246 GetDocumentWindow());
251 wxString
fileName(tmp
);
252 wxString path
, name
, ext
;
253 wxSplitPath(fileName
, & path
, & name
, & ext
);
255 if (ext
.IsEmpty() || ext
== wxT(""))
258 fileName
+= docTemplate
->GetDefaultExtension();
261 SetFilename(fileName
);
262 SetTitle(wxFileNameFromPath(fileName
));
264 GetDocumentManager()->AddFileToHistory(fileName
);
266 // Notify the views that the filename has changed
267 wxNode
*node
= m_documentViews
.First();
270 wxView
*view
= (wxView
*)node
->Data();
271 view
->OnChangeFilename();
275 return OnSaveDocument(m_documentFile
);
278 bool wxDocument::OnSaveDocument(const wxString
& file
)
284 if (wxTheApp
->GetAppName() != wxT(""))
285 msgTitle
= wxTheApp
->GetAppName();
287 msgTitle
= wxString(_("File error"));
289 #if wxUSE_STD_IOSTREAM
290 ofstream
store(wxString(file
.fn_str()));
291 if (store
.fail() || store
.bad())
293 wxFileOutputStream
store(wxString(file
.fn_str()));
294 if (store
.LastError() != wxSTREAM_NOERROR
)
297 (void)wxMessageBox(_("Sorry, could not open this file for saving."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
298 GetDocumentWindow());
302 if (!SaveObject(store
))
304 (void)wxMessageBox(_("Sorry, could not save this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
305 GetDocumentWindow());
314 bool wxDocument::OnOpenDocument(const wxString
& file
)
316 if (!OnSaveModified())
320 if (wxTheApp
->GetAppName() != wxT(""))
321 msgTitle
= wxTheApp
->GetAppName();
323 msgTitle
= wxString(_("File error"));
325 #if wxUSE_STD_IOSTREAM
326 ifstream
store(wxString(file
.fn_str()));
327 if (store
.fail() || store
.bad())
329 wxFileInputStream
store(wxString(file
.fn_str()));
330 if (store
.LastError() != wxSTREAM_NOERROR
)
333 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
|wxICON_EXCLAMATION
,
334 GetDocumentWindow());
337 #if wxUSE_STD_IOSTREAM
338 if (!LoadObject(store
))
340 int res
= LoadObject(store
).LastError();
341 if ((res
!= wxSTREAM_NOERROR
) &&
342 (res
!= wxSTREAM_EOF
))
345 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
|wxICON_EXCLAMATION
,
346 GetDocumentWindow());
349 SetFilename(file
, TRUE
);
358 #if wxUSE_STD_IOSTREAM
359 istream
& wxDocument::LoadObject(istream
& stream
)
361 wxInputStream
& wxDocument::LoadObject(wxInputStream
& stream
)
367 #if wxUSE_STD_IOSTREAM
368 ostream
& wxDocument::SaveObject(ostream
& stream
)
370 wxOutputStream
& wxDocument::SaveObject(wxOutputStream
& stream
)
376 bool wxDocument::Revert()
382 // Get title, or filename if no title, else unnamed
383 bool wxDocument::GetPrintableName(wxString
& buf
) const
385 if (m_documentTitle
!= wxT(""))
387 buf
= m_documentTitle
;
390 else if (m_documentFile
!= wxT(""))
392 buf
= wxFileNameFromPath(m_documentFile
);
402 wxWindow
*wxDocument::GetDocumentWindow() const
404 wxView
*view
= GetFirstView();
406 return view
->GetFrame();
408 return wxTheApp
->GetTopWindow();
411 wxCommandProcessor
*wxDocument::OnCreateCommandProcessor()
413 return new wxCommandProcessor
;
416 // TRUE if safe to close
417 bool wxDocument::OnSaveModified()
422 GetPrintableName(title
);
425 if (wxTheApp
->GetAppName() != wxT(""))
426 msgTitle
= wxTheApp
->GetAppName();
428 msgTitle
= wxString(_("Warning"));
431 prompt
.Printf(_("Do you want to save changes to document %s?"),
432 (const wxChar
*)title
);
433 int res
= wxMessageBox(prompt
, msgTitle
,
434 wxYES_NO
|wxCANCEL
|wxICON_QUESTION
,
435 GetDocumentWindow());
441 else if (res
== wxYES
)
443 else if (res
== wxCANCEL
)
449 bool wxDocument::Draw(wxDC
& WXUNUSED(context
))
454 bool wxDocument::AddView(wxView
*view
)
456 if (!m_documentViews
.Member(view
))
458 m_documentViews
.Append(view
);
464 bool wxDocument::RemoveView(wxView
*view
)
466 (void)m_documentViews
.DeleteObject(view
);
471 bool wxDocument::OnCreate(const wxString
& WXUNUSED(path
), long flags
)
473 if (GetDocumentTemplate()->CreateView(this, flags
))
479 // Called after a view is added or removed.
480 // The default implementation deletes the document if
481 // there are no more views.
482 void wxDocument::OnChangedViewList()
484 if (m_documentViews
.Number() == 0)
486 if (OnSaveModified())
493 void wxDocument::UpdateAllViews(wxView
*sender
, wxObject
*hint
)
495 wxNode
*node
= m_documentViews
.First();
498 wxView
*view
= (wxView
*)node
->Data();
499 view
->OnUpdate(sender
, hint
);
504 void wxDocument::SetFilename(const wxString
& filename
, bool notifyViews
)
506 m_documentFile
= filename
;
509 // Notify the views that the filename has changed
510 wxNode
*node
= m_documentViews
.First();
513 wxView
*view
= (wxView
*)node
->Data();
514 view
->OnChangeFilename();
520 // ----------------------------------------------------------------------------
522 // ----------------------------------------------------------------------------
527 m_viewDocument
= (wxDocument
*) NULL
;
529 m_viewTypeName
= wxT("");
530 m_viewFrame
= (wxFrame
*) NULL
;
535 // GetDocumentManager()->ActivateView(this, FALSE, TRUE);
536 m_viewDocument
->RemoveView(this);
539 // Extend event processing to search the document's event table
540 bool wxView::ProcessEvent(wxEvent
& event
)
542 if ( !GetDocument() || !GetDocument()->ProcessEvent(event
) )
543 return wxEvtHandler::ProcessEvent(event
);
548 void wxView::OnActivateView(bool WXUNUSED(activate
), wxView
*WXUNUSED(activeView
), wxView
*WXUNUSED(deactiveView
))
552 void wxView::OnPrint(wxDC
*dc
, wxObject
*WXUNUSED(info
))
557 void wxView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
))
561 void wxView::OnChangeFilename()
563 if (GetFrame() && GetDocument())
567 GetDocument()->GetPrintableName(title
);
569 GetFrame()->SetTitle(title
);
573 void wxView::SetDocument(wxDocument
*doc
)
575 m_viewDocument
= doc
;
580 bool wxView::Close(bool deleteWindow
)
582 if (OnClose(deleteWindow
))
588 void wxView::Activate(bool activate
)
590 if (GetDocumentManager())
592 OnActivateView(activate
, this, GetDocumentManager()->GetCurrentView());
593 GetDocumentManager()->ActivateView(this, activate
);
597 bool wxView::OnClose(bool WXUNUSED(deleteWindow
))
599 return GetDocument() ? GetDocument()->Close() : TRUE
;
602 #if wxUSE_PRINTING_ARCHITECTURE
603 wxPrintout
*wxView::OnCreatePrintout()
605 return new wxDocPrintout(this);
607 #endif // wxUSE_PRINTING_ARCHITECTURE
609 // ----------------------------------------------------------------------------
611 // ----------------------------------------------------------------------------
613 wxDocTemplate::wxDocTemplate(wxDocManager
*manager
,
614 const wxString
& descr
,
615 const wxString
& filter
,
618 const wxString
& docTypeName
,
619 const wxString
& viewTypeName
,
620 wxClassInfo
*docClassInfo
,
621 wxClassInfo
*viewClassInfo
,
624 m_documentManager
= manager
;
625 m_description
= descr
;
628 m_fileFilter
= filter
;
630 m_docTypeName
= docTypeName
;
631 m_viewTypeName
= viewTypeName
;
632 m_documentManager
->AssociateTemplate(this);
634 m_docClassInfo
= docClassInfo
;
635 m_viewClassInfo
= viewClassInfo
;
638 wxDocTemplate::~wxDocTemplate()
640 m_documentManager
->DisassociateTemplate(this);
643 // Tries to dynamically construct an object of the right class.
644 wxDocument
*wxDocTemplate::CreateDocument(const wxString
& path
, long flags
)
647 return (wxDocument
*) NULL
;
648 wxDocument
*doc
= (wxDocument
*)m_docClassInfo
->CreateObject();
649 doc
->SetFilename(path
);
650 doc
->SetDocumentTemplate(this);
651 GetDocumentManager()->AddDocument(doc
);
652 doc
->SetCommandProcessor(doc
->OnCreateCommandProcessor());
654 if (doc
->OnCreate(path
, flags
))
659 return (wxDocument
*) NULL
;
663 wxView
*wxDocTemplate::CreateView(wxDocument
*doc
, long flags
)
665 if (!m_viewClassInfo
)
666 return (wxView
*) NULL
;
667 wxView
*view
= (wxView
*)m_viewClassInfo
->CreateObject();
668 view
->SetDocument(doc
);
669 if (view
->OnCreate(doc
, flags
))
676 return (wxView
*) NULL
;
680 // The default (very primitive) format detection: check is the extension is
681 // that of the template
682 bool wxDocTemplate::FileMatchesTemplate(const wxString
& path
)
684 return GetDefaultExtension().IsSameAs(FindExtension(path
));
687 // ----------------------------------------------------------------------------
689 // ----------------------------------------------------------------------------
691 BEGIN_EVENT_TABLE(wxDocManager
, wxEvtHandler
)
692 EVT_MENU(wxID_OPEN
, wxDocManager::OnFileOpen
)
693 EVT_MENU(wxID_CLOSE
, wxDocManager::OnFileClose
)
694 EVT_MENU(wxID_REVERT
, wxDocManager::OnFileRevert
)
695 EVT_MENU(wxID_NEW
, wxDocManager::OnFileNew
)
696 EVT_MENU(wxID_SAVE
, wxDocManager::OnFileSave
)
697 EVT_MENU(wxID_SAVEAS
, wxDocManager::OnFileSaveAs
)
698 EVT_MENU(wxID_UNDO
, wxDocManager::OnUndo
)
699 EVT_MENU(wxID_REDO
, wxDocManager::OnRedo
)
701 EVT_UPDATE_UI(wxID_OPEN
, wxDocManager::OnUpdateFileOpen
)
702 EVT_UPDATE_UI(wxID_CLOSE
, wxDocManager::OnUpdateFileClose
)
703 EVT_UPDATE_UI(wxID_REVERT
, wxDocManager::OnUpdateFileRevert
)
704 EVT_UPDATE_UI(wxID_NEW
, wxDocManager::OnUpdateFileNew
)
705 EVT_UPDATE_UI(wxID_SAVE
, wxDocManager::OnUpdateFileSave
)
706 EVT_UPDATE_UI(wxID_SAVEAS
, wxDocManager::OnUpdateFileSaveAs
)
707 EVT_UPDATE_UI(wxID_UNDO
, wxDocManager::OnUpdateUndo
)
708 EVT_UPDATE_UI(wxID_REDO
, wxDocManager::OnUpdateRedo
)
710 #if wxUSE_PRINTING_ARCHITECTURE
711 EVT_MENU(wxID_PRINT
, wxDocManager::OnPrint
)
712 EVT_MENU(wxID_PRINT_SETUP
, wxDocManager::OnPrintSetup
)
713 EVT_MENU(wxID_PREVIEW
, wxDocManager::OnPreview
)
715 EVT_UPDATE_UI(wxID_PRINT
, wxDocManager::OnUpdatePrint
)
716 EVT_UPDATE_UI(wxID_PRINT_SETUP
, wxDocManager::OnUpdatePrintSetup
)
717 EVT_UPDATE_UI(wxID_PREVIEW
, wxDocManager::OnUpdatePreview
)
721 wxDocManager
* wxDocManager::sm_docManager
= (wxDocManager
*) NULL
;
723 wxDocManager::wxDocManager(long flags
, bool initialize
)
725 m_defaultDocumentNameCounter
= 1;
727 m_currentView
= (wxView
*) NULL
;
728 m_maxDocsOpen
= 10000;
729 m_fileHistory
= (wxFileHistory
*) NULL
;
732 sm_docManager
= this;
735 wxDocManager::~wxDocManager()
739 delete m_fileHistory
;
740 sm_docManager
= (wxDocManager
*) NULL
;
743 bool wxDocManager::Clear(bool force
)
745 wxNode
*node
= m_docs
.First();
748 wxDocument
*doc
= (wxDocument
*)node
->Data();
749 wxNode
*next
= node
->Next();
751 if (!doc
->Close() && !force
)
754 // Implicitly deletes the document when the last
755 // view is removed (deleted)
756 doc
->DeleteAllViews();
758 // Check document is deleted
759 if (m_docs
.Member(doc
))
762 // This assumes that documents are not connected in
763 // any way, i.e. deleting one document does NOT
767 node
= m_templates
.First();
770 wxDocTemplate
*templ
= (wxDocTemplate
*) node
->Data();
771 wxNode
* next
= node
->Next();
778 bool wxDocManager::Initialize()
780 m_fileHistory
= OnCreateFileHistory();
784 wxFileHistory
*wxDocManager::OnCreateFileHistory()
786 return new wxFileHistory
;
789 void wxDocManager::OnFileClose(wxCommandEvent
& WXUNUSED(event
))
791 wxDocument
*doc
= GetCurrentDocument();
796 doc
->DeleteAllViews();
797 if (m_docs
.Member(doc
))
802 void wxDocManager::OnFileNew(wxCommandEvent
& WXUNUSED(event
))
804 CreateDocument(wxString(""), wxDOC_NEW
);
807 void wxDocManager::OnFileOpen(wxCommandEvent
& WXUNUSED(event
))
809 if ( !CreateDocument(wxString(""), 0) )
815 void wxDocManager::OnFileRevert(wxCommandEvent
& WXUNUSED(event
))
817 wxDocument
*doc
= GetCurrentDocument();
823 void wxDocManager::OnFileSave(wxCommandEvent
& WXUNUSED(event
))
825 wxDocument
*doc
= GetCurrentDocument();
831 void wxDocManager::OnFileSaveAs(wxCommandEvent
& WXUNUSED(event
))
833 wxDocument
*doc
= GetCurrentDocument();
839 void wxDocManager::OnPrint(wxCommandEvent
& WXUNUSED(event
))
841 #if wxUSE_PRINTING_ARCHITECTURE
842 wxView
*view
= GetCurrentView();
846 wxPrintout
*printout
= view
->OnCreatePrintout();
850 printer
.Print(view
->GetFrame(), printout
, TRUE
);
854 #endif // wxUSE_PRINTING_ARCHITECTURE
857 void wxDocManager::OnPrintSetup(wxCommandEvent
& WXUNUSED(event
))
859 #if wxUSE_PRINTING_ARCHITECTURE
860 wxWindow
*parentWin
= wxTheApp
->GetTopWindow();
861 wxView
*view
= GetCurrentView();
863 parentWin
= view
->GetFrame();
865 wxPrintDialogData data
;
867 wxPrintDialog
printerDialog(parentWin
, &data
);
868 printerDialog
.GetPrintDialogData().SetSetupDialog(TRUE
);
869 printerDialog
.ShowModal();
870 #endif // wxUSE_PRINTING_ARCHITECTURE
873 void wxDocManager::OnPreview(wxCommandEvent
& WXUNUSED(event
))
875 #if wxUSE_PRINTING_ARCHITECTURE
876 wxView
*view
= GetCurrentView();
880 wxPrintout
*printout
= view
->OnCreatePrintout();
883 // Pass two printout objects: for preview, and possible printing.
884 wxPrintPreviewBase
*preview
= (wxPrintPreviewBase
*) NULL
;
885 preview
= new wxPrintPreview(printout
, view
->OnCreatePrintout());
887 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, (wxFrame
*)wxTheApp
->GetTopWindow(), _("Print Preview"),
888 wxPoint(100, 100), wxSize(600, 650));
889 frame
->Centre(wxBOTH
);
893 #endif // wxUSE_PRINTING_ARCHITECTURE
896 void wxDocManager::OnUndo(wxCommandEvent
& WXUNUSED(event
))
898 wxDocument
*doc
= GetCurrentDocument();
901 if (doc
->GetCommandProcessor())
902 doc
->GetCommandProcessor()->Undo();
905 void wxDocManager::OnRedo(wxCommandEvent
& WXUNUSED(event
))
907 wxDocument
*doc
= GetCurrentDocument();
910 if (doc
->GetCommandProcessor())
911 doc
->GetCommandProcessor()->Redo();
914 // Handlers for UI update commands
916 void wxDocManager::OnUpdateFileOpen(wxUpdateUIEvent
& event
)
918 event
.Enable( TRUE
);
921 void wxDocManager::OnUpdateFileClose(wxUpdateUIEvent
& event
)
923 wxDocument
*doc
= GetCurrentDocument();
924 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
927 void wxDocManager::OnUpdateFileRevert(wxUpdateUIEvent
& event
)
929 wxDocument
*doc
= GetCurrentDocument();
930 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
933 void wxDocManager::OnUpdateFileNew(wxUpdateUIEvent
& event
)
935 event
.Enable( TRUE
);
938 void wxDocManager::OnUpdateFileSave(wxUpdateUIEvent
& event
)
940 wxDocument
*doc
= GetCurrentDocument();
941 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
944 void wxDocManager::OnUpdateFileSaveAs(wxUpdateUIEvent
& event
)
946 wxDocument
*doc
= GetCurrentDocument();
947 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
950 void wxDocManager::OnUpdateUndo(wxUpdateUIEvent
& event
)
952 wxDocument
*doc
= GetCurrentDocument();
953 event
.Enable( (doc
&& doc
->GetCommandProcessor() && doc
->GetCommandProcessor()->CanUndo()) );
956 void wxDocManager::OnUpdateRedo(wxUpdateUIEvent
& event
)
958 wxDocument
*doc
= GetCurrentDocument();
959 event
.Enable( (doc
&& doc
->GetCommandProcessor() && doc
->GetCommandProcessor()->CanRedo()) );
962 void wxDocManager::OnUpdatePrint(wxUpdateUIEvent
& event
)
964 wxDocument
*doc
= GetCurrentDocument();
965 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
968 void wxDocManager::OnUpdatePrintSetup(wxUpdateUIEvent
& event
)
970 event
.Enable( TRUE
);
973 void wxDocManager::OnUpdatePreview(wxUpdateUIEvent
& event
)
975 wxDocument
*doc
= GetCurrentDocument();
976 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
979 wxView
*wxDocManager::GetCurrentView() const
982 return m_currentView
;
983 if (m_docs
.Number() == 1)
985 wxDocument
* doc
= (wxDocument
*) m_docs
.First()->Data();
986 return doc
->GetFirstView();
988 return (wxView
*) NULL
;
991 // Extend event processing to search the view's event table
992 bool wxDocManager::ProcessEvent(wxEvent
& event
)
994 wxView
* view
= GetCurrentView();
997 if (view
->ProcessEvent(event
))
1000 return wxEvtHandler::ProcessEvent(event
);
1003 wxDocument
*wxDocManager::CreateDocument(const wxString
& path
, long flags
)
1005 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.Number()];
1008 for (i
= 0; i
< m_templates
.Number(); i
++)
1010 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Nth(i
)->Data());
1011 if (temp
->IsVisible())
1013 templates
[n
] = temp
;
1020 return (wxDocument
*) NULL
;
1023 // If we've reached the max number of docs, close the
1025 if (GetDocuments().Number() >= m_maxDocsOpen
)
1027 wxDocument
*doc
= (wxDocument
*)GetDocuments().First()->Data();
1030 // Implicitly deletes the document when
1031 // the last view is deleted
1032 doc
->DeleteAllViews();
1034 // Check we're really deleted
1035 if (m_docs
.Member(doc
))
1039 return (wxDocument
*) NULL
;
1042 // New document: user chooses a template, unless there's only one.
1043 if (flags
& wxDOC_NEW
)
1047 wxDocTemplate
*temp
= templates
[0];
1049 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
1052 newDoc
->SetDocumentName(temp
->GetDocumentName());
1053 newDoc
->SetDocumentTemplate(temp
);
1054 newDoc
->OnNewDocument();
1059 wxDocTemplate
*temp
= SelectDocumentType(templates
, n
);
1063 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
1066 newDoc
->SetDocumentName(temp
->GetDocumentName());
1067 newDoc
->SetDocumentTemplate(temp
);
1068 newDoc
->OnNewDocument();
1073 return (wxDocument
*) NULL
;
1076 // Existing document
1077 wxDocTemplate
*temp
= (wxDocTemplate
*) NULL
;
1079 wxString
path2(wxT(""));
1080 if (path
!= wxT(""))
1083 if (flags
& wxDOC_SILENT
)
1084 temp
= FindTemplateForPath(path2
);
1086 temp
= SelectDocumentPath(templates
, n
, path2
, flags
);
1092 wxDocument
*newDoc
= temp
->CreateDocument(path2
, flags
);
1095 newDoc
->SetDocumentName(temp
->GetDocumentName());
1096 newDoc
->SetDocumentTemplate(temp
);
1097 if (!newDoc
->OnOpenDocument(path2
))
1100 return (wxDocument
*) NULL
;
1102 AddFileToHistory(path2
);
1107 return (wxDocument
*) NULL
;
1110 wxView
*wxDocManager::CreateView(wxDocument
*doc
, long flags
)
1112 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.Number()];
1115 for (i
= 0; i
< m_templates
.Number(); i
++)
1117 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Nth(i
)->Data());
1118 if (temp
->IsVisible())
1120 if (temp
->GetDocumentName() == doc
->GetDocumentName())
1122 templates
[n
] = temp
;
1130 return (wxView
*) NULL
;
1134 wxDocTemplate
*temp
= templates
[0];
1136 wxView
*view
= temp
->CreateView(doc
, flags
);
1138 view
->SetViewName(temp
->GetViewName());
1142 wxDocTemplate
*temp
= SelectViewType(templates
, n
);
1146 wxView
*view
= temp
->CreateView(doc
, flags
);
1148 view
->SetViewName(temp
->GetViewName());
1152 return (wxView
*) NULL
;
1155 // Not yet implemented
1156 void wxDocManager::DeleteTemplate(wxDocTemplate
*WXUNUSED(temp
), long WXUNUSED(flags
))
1160 // Not yet implemented
1161 bool wxDocManager::FlushDoc(wxDocument
*WXUNUSED(doc
))
1166 wxDocument
*wxDocManager::GetCurrentDocument() const
1169 return m_currentView
->GetDocument();
1171 return (wxDocument
*) NULL
;
1174 // Make a default document name
1175 bool wxDocManager::MakeDefaultName(wxString
& name
)
1177 name
.Printf(_("unnamed%d"), m_defaultDocumentNameCounter
);
1178 m_defaultDocumentNameCounter
++;
1183 // Make a frame title (override this to do something different)
1184 // If docName is empty, a document is not currently active.
1185 wxString
wxDocManager::MakeFrameTitle(wxDocument
* doc
)
1187 wxString appName
= wxTheApp
->GetAppName();
1194 doc
->GetPrintableName(docName
);
1195 title
= docName
+ wxString(_(" - ")) + appName
;
1201 // Not yet implemented
1202 wxDocTemplate
*wxDocManager::MatchTemplate(const wxString
& WXUNUSED(path
))
1204 return (wxDocTemplate
*) NULL
;
1207 // File history management
1208 void wxDocManager::AddFileToHistory(const wxString
& file
)
1211 m_fileHistory
->AddFileToHistory(file
);
1214 void wxDocManager::RemoveFileFromHistory(int i
)
1217 m_fileHistory
->RemoveFileFromHistory(i
);
1220 wxString
wxDocManager::GetHistoryFile(int i
) const
1225 histFile
= m_fileHistory
->GetHistoryFile(i
);
1230 void wxDocManager::FileHistoryUseMenu(wxMenu
*menu
)
1233 m_fileHistory
->UseMenu(menu
);
1236 void wxDocManager::FileHistoryRemoveMenu(wxMenu
*menu
)
1239 m_fileHistory
->RemoveMenu(menu
);
1243 void wxDocManager::FileHistoryLoad(wxConfigBase
& config
)
1246 m_fileHistory
->Load(config
);
1249 void wxDocManager::FileHistorySave(wxConfigBase
& config
)
1252 m_fileHistory
->Save(config
);
1256 void wxDocManager::FileHistoryAddFilesToMenu(wxMenu
* menu
)
1259 m_fileHistory
->AddFilesToMenu(menu
);
1262 void wxDocManager::FileHistoryAddFilesToMenu()
1265 m_fileHistory
->AddFilesToMenu();
1268 int wxDocManager::GetNoHistoryFiles() const
1271 return m_fileHistory
->GetNoHistoryFiles();
1277 // Find out the document template via matching in the document file format
1278 // against that of the template
1279 wxDocTemplate
*wxDocManager::FindTemplateForPath(const wxString
& path
)
1281 wxDocTemplate
*theTemplate
= (wxDocTemplate
*) NULL
;
1283 // Find the template which this extension corresponds to
1285 for (i
= 0; i
< m_templates
.Number(); i
++)
1287 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Nth(i
)->Data();
1288 if ( temp
->FileMatchesTemplate(path
) )
1297 // Prompts user to open a file, using file specs in templates.
1298 // How to implement in wxWindows? Must extend the file selector
1299 // dialog or implement own; OR match the extension to the
1300 // template extension.
1302 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**templates
,
1303 #if defined(__WXMSW__) || defined(__WXGTK__)
1306 int WXUNUSED(noTemplates
),
1309 long WXUNUSED(flags
),
1310 bool WXUNUSED(save
))
1312 // We can only have multiple filters in Windows and GTK
1313 #if defined(__WXMSW__) || defined(__WXGTK__)
1317 for (i
= 0; i
< noTemplates
; i
++)
1319 if (templates
[i
]->IsVisible())
1321 // add a '|' to separate this filter from the previous one
1322 if ( !descrBuf
.IsEmpty() )
1323 descrBuf
<< wxT('|');
1325 descrBuf
<< templates
[i
]->GetDescription()
1326 << wxT(" (") << templates
[i
]->GetFileFilter() << wxT(") |")
1327 << templates
[i
]->GetFileFilter();
1331 wxString descrBuf
= wxT("*.*");
1334 int FilterIndex
= 0;
1335 wxString pathTmp
= wxFileSelectorEx(_("Select a file"),
1341 wxTheApp
->GetTopWindow());
1343 if (!pathTmp
.IsEmpty())
1345 m_lastDirectory
= wxPathOnly(pathTmp
);
1349 // This is dodgy in that we're selecting the template on the
1350 // basis of the file extension, which may not be a standard
1351 // one. We really want to know exactly which template was
1352 // chosen by using a more advanced file selector.
1353 wxDocTemplate
*theTemplate
= FindTemplateForPath(path
);
1355 theTemplate
= templates
[FilterIndex
];
1362 return (wxDocTemplate
*) NULL
;
1365 // In all other windowing systems, until we have more advanced
1366 // file selectors, we must select the document type (template) first, and
1367 // _then_ pop up the file selector.
1368 wxDocTemplate
*temp
= SelectDocumentType(templates
, noTemplates
);
1370 return (wxDocTemplate
*) NULL
;
1372 wxChar
*pathTmp
= wxFileSelector(_("Select a file"), wxT(""), wxT(""),
1373 temp
->GetDefaultExtension(),
1374 temp
->GetFileFilter(),
1375 0, wxTheApp
->GetTopWindow());
1383 return (wxDocTemplate
*) NULL
;
1387 wxDocTemplate
*wxDocManager::SelectDocumentType(wxDocTemplate
**templates
,
1390 wxChar
**strings
= new wxChar
*[noTemplates
];
1391 wxChar
**data
= new wxChar
*[noTemplates
];
1394 for (i
= 0; i
< noTemplates
; i
++)
1396 if (templates
[i
]->IsVisible())
1398 strings
[n
] = (wxChar
*)templates
[i
]->m_description
.c_str();
1399 data
[n
] = (wxChar
*)templates
[i
];
1407 return (wxDocTemplate
*) NULL
;
1411 wxDocTemplate
*temp
= (wxDocTemplate
*)data
[0];
1417 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData(_("Select a document template"), _("Templates"), n
,
1418 strings
, (void **)data
);
1424 wxDocTemplate
*wxDocManager::SelectViewType(wxDocTemplate
**templates
,
1427 wxChar
**strings
= new wxChar
*[noTemplates
];
1428 wxChar
**data
= new wxChar
*[noTemplates
];
1431 for (i
= 0; i
< noTemplates
; i
++)
1433 if (templates
[i
]->IsVisible() && (templates
[i
]->GetViewName() != wxT("")))
1435 strings
[n
] = (wxChar
*)templates
[i
]->m_viewTypeName
.c_str();
1436 data
[n
] = (wxChar
*)templates
[i
];
1440 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData(_("Select a document view"), _("Views"), n
,
1441 strings
, (void **)data
);
1447 void wxDocManager::AssociateTemplate(wxDocTemplate
*temp
)
1449 if (!m_templates
.Member(temp
))
1450 m_templates
.Append(temp
);
1453 void wxDocManager::DisassociateTemplate(wxDocTemplate
*temp
)
1455 m_templates
.DeleteObject(temp
);
1458 // Add and remove a document from the manager's list
1459 void wxDocManager::AddDocument(wxDocument
*doc
)
1461 if (!m_docs
.Member(doc
))
1465 void wxDocManager::RemoveDocument(wxDocument
*doc
)
1467 m_docs
.DeleteObject(doc
);
1470 // Views or windows should inform the document manager
1471 // when a view is going in or out of focus
1472 void wxDocManager::ActivateView(wxView
*view
, bool activate
, bool WXUNUSED(deleting
))
1474 // If we're deactiving, and if we're not actually deleting the view, then
1475 // don't reset the current view because we may be going to
1476 // a window without a view.
1477 // WHAT DID I MEAN BY THAT EXACTLY?
1481 if (m_currentView == view)
1482 m_currentView = NULL;
1488 m_currentView
= view
;
1490 m_currentView
= (wxView
*) NULL
;
1494 // ----------------------------------------------------------------------------
1495 // Default document child frame
1496 // ----------------------------------------------------------------------------
1498 BEGIN_EVENT_TABLE(wxDocChildFrame
, wxFrame
)
1499 EVT_ACTIVATE(wxDocChildFrame::OnActivate
)
1500 EVT_CLOSE(wxDocChildFrame::OnCloseWindow
)
1503 wxDocChildFrame::wxDocChildFrame(wxDocument
*doc
,
1507 const wxString
& title
,
1511 const wxString
& name
)
1512 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1514 m_childDocument
= doc
;
1517 view
->SetFrame(this);
1520 wxDocChildFrame::~wxDocChildFrame()
1524 // Extend event processing to search the view's event table
1525 bool wxDocChildFrame::ProcessEvent(wxEvent
& event
)
1528 m_childView
->Activate(TRUE
);
1530 if ( !m_childView
|| ! m_childView
->ProcessEvent(event
) )
1532 // Only hand up to the parent if it's a menu command
1533 if (!event
.IsKindOf(CLASSINFO(wxCommandEvent
)) || !GetParent() || !GetParent()->ProcessEvent(event
))
1534 return wxEvtHandler::ProcessEvent(event
);
1542 void wxDocChildFrame::OnActivate(wxActivateEvent
& event
)
1544 wxFrame::OnActivate(event
);
1547 m_childView
->Activate(event
.GetActive());
1550 void wxDocChildFrame::OnCloseWindow(wxCloseEvent
& event
)
1555 if (!event
.CanVeto())
1556 ans
= TRUE
; // Must delete.
1558 ans
= m_childView
->Close(FALSE
); // FALSE means don't delete associated window
1562 m_childView
->Activate(FALSE
);
1564 m_childView
= (wxView
*) NULL
;
1565 m_childDocument
= (wxDocument
*) NULL
;
1576 // ----------------------------------------------------------------------------
1577 // Default parent frame
1578 // ----------------------------------------------------------------------------
1580 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1581 EVT_MENU(wxID_EXIT
, wxDocParentFrame::OnExit
)
1582 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE9
, wxDocParentFrame::OnMRUFile
)
1583 EVT_CLOSE(wxDocParentFrame::OnCloseWindow
)
1586 wxDocParentFrame::wxDocParentFrame(wxDocManager
*manager
,
1589 const wxString
& title
,
1593 const wxString
& name
)
1594 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1596 m_docManager
= manager
;
1599 void wxDocParentFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
1604 void wxDocParentFrame::OnMRUFile(wxCommandEvent
& event
)
1606 int n
= event
.GetSelection() - wxID_FILE1
; // the index in MRU list
1607 wxString
filename(m_docManager
->GetHistoryFile(n
));
1608 if ( !filename
.IsEmpty() )
1610 // verify that the file exists before doing anything else
1611 if ( wxFile::Exists(filename
) )
1614 (void)m_docManager
->CreateDocument(filename
, wxDOC_SILENT
);
1618 // remove the bogus filename from the MRU list and notify the user
1620 m_docManager
->RemoveFileFromHistory(n
);
1622 wxLogError(_("The file '%s' doesn't exist and couldn't be opened.\n"
1623 "It has been also removed from the MRU files list."),
1629 // Extend event processing to search the view's event table
1630 bool wxDocParentFrame::ProcessEvent(wxEvent
& event
)
1632 // Try the document manager, then do default processing
1633 if (!m_docManager
|| !m_docManager
->ProcessEvent(event
))
1634 return wxEvtHandler::ProcessEvent(event
);
1639 // Define the behaviour for the frame closing
1640 // - must delete all frames except for the main one.
1641 void wxDocParentFrame::OnCloseWindow(wxCloseEvent
& event
)
1643 if (m_docManager
->Clear(!event
.CanVeto()))
1651 #if wxUSE_PRINTING_ARCHITECTURE
1653 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
)
1656 m_printoutView
= view
;
1659 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
1663 // Get the logical pixels per inch of screen and printer
1664 int ppiScreenX
, ppiScreenY
;
1665 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
1666 int ppiPrinterX
, ppiPrinterY
;
1667 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
1669 // This scales the DC so that the printout roughly represents the
1670 // the screen scaling. The text point size _should_ be the right size
1671 // but in fact is too small for some reason. This is a detail that will
1672 // need to be addressed at some point but can be fudged for the
1674 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
1676 // Now we have to check in case our real page size is reduced
1677 // (e.g. because we're drawing to a print preview memory DC)
1678 int pageWidth
, pageHeight
;
1680 dc
->GetSize(&w
, &h
);
1681 GetPageSizePixels(&pageWidth
, &pageHeight
);
1683 // If printer pageWidth == current DC width, then this doesn't
1684 // change. But w might be the preview bitmap width, so scale down.
1685 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
1686 dc
->SetUserScale(overallScale
, overallScale
);
1690 m_printoutView
->OnDraw(dc
);
1695 bool wxDocPrintout::HasPage(int pageNum
)
1697 return (pageNum
== 1);
1700 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
1702 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
1708 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
1716 #endif // wxUSE_PRINTING_ARCHITECTURE
1718 // ----------------------------------------------------------------------------
1719 // Command processing framework
1720 // ----------------------------------------------------------------------------
1722 wxCommand::wxCommand(bool canUndoIt
, const wxString
& name
)
1724 m_canUndo
= canUndoIt
;
1725 m_commandName
= name
;
1728 wxCommand::~wxCommand()
1732 // Command processor
1733 wxCommandProcessor::wxCommandProcessor(int maxCommands
)
1735 m_maxNoCommands
= maxCommands
;
1736 m_currentCommand
= (wxNode
*) NULL
;
1737 m_commandEditMenu
= (wxMenu
*) NULL
;
1740 wxCommandProcessor::~wxCommandProcessor()
1745 // Pass a command to the processor. The processor calls Do();
1746 // if successful, is appended to the command history unless
1747 // storeIt is FALSE.
1748 bool wxCommandProcessor::Submit(wxCommand
*command
, bool storeIt
)
1750 bool success
= command
->Do();
1751 if (success
&& storeIt
)
1753 if (m_commands
.Number() == m_maxNoCommands
)
1755 wxNode
*firstNode
= m_commands
.First();
1756 wxCommand
*firstCommand
= (wxCommand
*)firstNode
->Data();
1757 delete firstCommand
;
1761 // Correct a bug: we must chop off the current 'branch'
1762 // so that we're at the end of the command list.
1763 if (!m_currentCommand
)
1767 wxNode
*node
= m_currentCommand
->Next();
1770 wxNode
*next
= node
->Next();
1771 delete (wxCommand
*)node
->Data();
1777 m_commands
.Append(command
);
1778 m_currentCommand
= m_commands
.Last();
1784 bool wxCommandProcessor::Undo()
1786 if (m_currentCommand
)
1788 wxCommand
*command
= (wxCommand
*)m_currentCommand
->Data();
1789 if (command
->CanUndo())
1791 bool success
= command
->Undo();
1794 m_currentCommand
= m_currentCommand
->Previous();
1803 bool wxCommandProcessor::Redo()
1805 wxCommand
*redoCommand
= (wxCommand
*) NULL
;
1806 wxNode
*redoNode
= (wxNode
*) NULL
;
1807 if (m_currentCommand
&& m_currentCommand
->Next())
1809 redoCommand
= (wxCommand
*)m_currentCommand
->Next()->Data();
1810 redoNode
= m_currentCommand
->Next();
1814 if (m_commands
.Number() > 0)
1816 redoCommand
= (wxCommand
*)m_commands
.First()->Data();
1817 redoNode
= m_commands
.First();
1823 bool success
= redoCommand
->Do();
1826 m_currentCommand
= redoNode
;
1834 bool wxCommandProcessor::CanUndo() const
1836 if (m_currentCommand
)
1837 return ((wxCommand
*)m_currentCommand
->Data())->CanUndo();
1841 bool wxCommandProcessor::CanRedo() const
1843 if ((m_currentCommand
!= (wxNode
*) NULL
) && (m_currentCommand
->Next() == (wxNode
*) NULL
))
1846 if ((m_currentCommand
!= (wxNode
*) NULL
) && (m_currentCommand
->Next() != (wxNode
*) NULL
))
1849 if ((m_currentCommand
== (wxNode
*) NULL
) && (m_commands
.Number() > 0))
1855 void wxCommandProcessor::Initialize()
1857 m_currentCommand
= m_commands
.Last();
1861 void wxCommandProcessor::SetMenuStrings()
1863 if (m_commandEditMenu
)
1866 if (m_currentCommand
)
1868 wxCommand
*command
= (wxCommand
*)m_currentCommand
->Data();
1869 wxString
commandName(command
->GetName());
1870 if (commandName
== wxT("")) commandName
= _("Unnamed command");
1871 bool canUndo
= command
->CanUndo();
1873 buf
= wxString(_("&Undo ")) + commandName
;
1875 buf
= wxString(_("Can't &Undo ")) + commandName
;
1877 m_commandEditMenu
->SetLabel(wxID_UNDO
, buf
);
1878 m_commandEditMenu
->Enable(wxID_UNDO
, canUndo
);
1880 // We can redo, if we're not at the end of the history.
1881 if (m_currentCommand
->Next())
1883 wxCommand
*redoCommand
= (wxCommand
*)m_currentCommand
->Next()->Data();
1884 wxString
redoCommandName(redoCommand
->GetName());
1885 if (redoCommandName
== wxT("")) redoCommandName
= _("Unnamed command");
1886 buf
= wxString(_("&Redo ")) + redoCommandName
;
1887 m_commandEditMenu
->SetLabel(wxID_REDO
, buf
);
1888 m_commandEditMenu
->Enable(wxID_REDO
, TRUE
);
1892 m_commandEditMenu
->SetLabel(wxID_REDO
, _("&Redo"));
1893 m_commandEditMenu
->Enable(wxID_REDO
, FALSE
);
1898 m_commandEditMenu
->SetLabel(wxID_UNDO
, _("&Undo"));
1899 m_commandEditMenu
->Enable(wxID_UNDO
, FALSE
);
1901 if (m_commands
.Number() == 0)
1903 m_commandEditMenu
->SetLabel(wxID_REDO
, _("&Redo"));
1904 m_commandEditMenu
->Enable(wxID_REDO
, FALSE
);
1908 // currentCommand is NULL but there are commands: this means that
1909 // we've undone to the start of the list, but can redo the first.
1910 wxCommand
*redoCommand
= (wxCommand
*)m_commands
.First()->Data();
1911 wxString
redoCommandName(redoCommand
->GetName());
1912 if (redoCommandName
== wxT("")) redoCommandName
= _("Unnamed command");
1913 buf
= wxString(_("&Redo ")) + redoCommandName
;
1914 m_commandEditMenu
->SetLabel(wxID_REDO
, buf
);
1915 m_commandEditMenu
->Enable(wxID_REDO
, TRUE
);
1921 void wxCommandProcessor::ClearCommands()
1923 wxNode
*node
= m_commands
.First();
1926 wxCommand
*command
= (wxCommand
*)node
->Data();
1929 node
= m_commands
.First();
1931 m_currentCommand
= (wxNode
*) NULL
;
1934 // ----------------------------------------------------------------------------
1935 // File history processor
1936 // ----------------------------------------------------------------------------
1938 wxFileHistory::wxFileHistory(int maxFiles
)
1940 m_fileMaxFiles
= maxFiles
;
1942 m_fileHistory
= new wxChar
*[m_fileMaxFiles
];
1945 wxFileHistory::~wxFileHistory()
1948 for (i
= 0; i
< m_fileHistoryN
; i
++)
1949 delete[] m_fileHistory
[i
];
1950 delete[] m_fileHistory
;
1953 // File history management
1954 void wxFileHistory::AddFileToHistory(const wxString
& file
)
1957 // Check we don't already have this file
1958 for (i
= 0; i
< m_fileHistoryN
; i
++)
1960 if (m_fileHistory
[i
] && wxString(m_fileHistory
[i
]) == file
)
1964 // Add to the project file history:
1965 // Move existing files (if any) down so we can insert file at beginning.
1967 // First delete filename that has popped off the end of the array (if any)
1968 if (m_fileHistoryN
== m_fileMaxFiles
)
1970 delete[] m_fileHistory
[m_fileMaxFiles
-1];
1971 m_fileHistory
[m_fileMaxFiles
-1] = (wxChar
*) NULL
;
1973 if (m_fileHistoryN
< m_fileMaxFiles
)
1975 wxNode
* node
= m_fileMenus
.First();
1978 wxMenu
* menu
= (wxMenu
*) node
->Data();
1979 if (m_fileHistoryN
== 0)
1980 menu
->AppendSeparator();
1981 menu
->Append(wxID_FILE1
+m_fileHistoryN
, _("[EMPTY]"));
1982 node
= node
->Next();
1986 // Shuffle filenames down
1987 for (i
= (m_fileHistoryN
-1); i
> 0; i
--)
1989 m_fileHistory
[i
] = m_fileHistory
[i
-1];
1991 m_fileHistory
[0] = copystring(file
);
1993 for (i
= 0; i
< m_fileHistoryN
; i
++)
1994 if (m_fileHistory
[i
])
1997 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
1998 wxNode
* node
= m_fileMenus
.First();
2001 wxMenu
* menu
= (wxMenu
*) node
->Data();
2002 menu
->SetLabel(wxID_FILE1
+i
, buf
);
2003 node
= node
->Next();
2008 void wxFileHistory::RemoveFileFromHistory(int i
)
2010 wxCHECK_RET( i
< m_fileHistoryN
,
2011 wxT("invalid index in wxFileHistory::RemoveFileFromHistory") );
2013 wxNode
* node
= m_fileMenus
.First();
2016 wxMenu
* menu
= (wxMenu
*) node
->Data();
2018 // delete the element from the array (could use memmove() too...)
2019 delete [] m_fileHistory
[i
];
2022 for ( j
= i
; j
< m_fileHistoryN
- 1; j
++ )
2024 m_fileHistory
[j
] = m_fileHistory
[j
+ 1];
2027 // shuffle filenames up
2029 for ( j
= i
; j
< m_fileHistoryN
- 1; j
++ )
2031 buf
.Printf(s_MRUEntryFormat
, j
+ 1, m_fileHistory
[j
]);
2032 menu
->SetLabel(wxID_FILE1
+ j
, buf
);
2035 node
= node
->Next();
2037 // delete the last menu item which is unused now
2038 menu
->Delete(wxID_FILE1
+ m_fileHistoryN
- 1);
2040 // delete the last separator too if no more files are left
2041 if ( m_fileHistoryN
== 1 )
2043 wxMenuItemList::Node
*node
= menu
->GetMenuItems().GetLast();
2046 wxMenuItem
*menuItem
= node
->GetData();
2047 if ( menuItem
->IsSeparator() )
2049 menu
->Delete(menuItem
);
2051 //else: should we search backwards for the last separator?
2053 //else: menu is empty somehow
2060 wxString
wxFileHistory::GetHistoryFile(int i
) const
2063 if ( i
< m_fileHistoryN
)
2065 s
= m_fileHistory
[i
];
2069 wxFAIL_MSG( wxT("bad index in wxFileHistory::GetHistoryFile") );
2075 void wxFileHistory::UseMenu(wxMenu
*menu
)
2077 if (!m_fileMenus
.Member(menu
))
2078 m_fileMenus
.Append(menu
);
2081 void wxFileHistory::RemoveMenu(wxMenu
*menu
)
2083 m_fileMenus
.DeleteObject(menu
);
2087 void wxFileHistory::Load(wxConfigBase
& config
)
2091 buf
.Printf(wxT("file%d"), m_fileHistoryN
+1);
2092 wxString historyFile
;
2093 while ((m_fileHistoryN
<= m_fileMaxFiles
) && config
.Read(buf
, &historyFile
) && (historyFile
!= wxT("")))
2095 m_fileHistory
[m_fileHistoryN
] = copystring((const wxChar
*) historyFile
);
2097 buf
.Printf(wxT("file%d"), m_fileHistoryN
+1);
2098 historyFile
= wxT("");
2103 void wxFileHistory::Save(wxConfigBase
& config
)
2106 for (i
= 0; i
< m_fileHistoryN
; i
++)
2109 buf
.Printf(wxT("file%d"), i
+1);
2110 config
.Write(buf
, wxString(m_fileHistory
[i
]));
2113 #endif // wxUSE_CONFIG
2115 void wxFileHistory::AddFilesToMenu()
2117 if (m_fileHistoryN
> 0)
2119 wxNode
* node
= m_fileMenus
.First();
2122 wxMenu
* menu
= (wxMenu
*) node
->Data();
2123 menu
->AppendSeparator();
2125 for (i
= 0; i
< m_fileHistoryN
; i
++)
2127 if (m_fileHistory
[i
])
2130 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
2131 menu
->Append(wxID_FILE1
+i
, buf
);
2134 node
= node
->Next();
2139 void wxFileHistory::AddFilesToMenu(wxMenu
* menu
)
2141 if (m_fileHistoryN
> 0)
2143 menu
->AppendSeparator();
2145 for (i
= 0; i
< m_fileHistoryN
; i
++)
2147 if (m_fileHistory
[i
])
2150 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
2151 menu
->Append(wxID_FILE1
+i
, buf
);
2157 // ----------------------------------------------------------------------------
2158 // Permits compatibility with existing file formats and functions that
2159 // manipulate files directly
2160 // ----------------------------------------------------------------------------
2162 #if wxUSE_STD_IOSTREAM
2163 bool wxTransferFileToStream(const wxString
& filename
, ostream
& stream
)
2168 if ((fd1
= fopen (filename
.fn_str(), "rb")) == NULL
)
2171 while ((ch
= getc (fd1
)) != EOF
)
2172 stream
<< (unsigned char)ch
;
2178 bool wxTransferStreamToFile(istream
& stream
, const wxString
& filename
)
2183 if ((fd1
= fopen (filename
.fn_str(), "wb")) == NULL
)
2188 while (!stream
.eof())
2198 bool wxTransferFileToStream(const wxString
& filename
, wxOutputStream
& stream
)
2203 if ((fd1
= fopen (filename
.fn_str(), "rb")) == NULL
)
2206 while ((ch
= getc (fd1
)) != EOF
)
2207 stream
.PutC((char) ch
);
2213 bool wxTransferStreamToFile(wxInputStream
& stream
, const wxString
& filename
)
2218 if ((fd1
= fopen (filename
.fn_str(), "wb")) == NULL
)
2223 int len
= stream
.StreamSize();
2224 // TODO: is this the correct test for EOF?
2225 while (stream
.TellI() < (len
- 1))
2235 #endif // wxUSE_DOC_VIEW_ARCHITECTURE