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 CreateDocument(wxString(""), 0);
812 void wxDocManager::OnFileRevert(wxCommandEvent
& WXUNUSED(event
))
814 wxDocument
*doc
= GetCurrentDocument();
820 void wxDocManager::OnFileSave(wxCommandEvent
& WXUNUSED(event
))
822 wxDocument
*doc
= GetCurrentDocument();
828 void wxDocManager::OnFileSaveAs(wxCommandEvent
& WXUNUSED(event
))
830 wxDocument
*doc
= GetCurrentDocument();
836 void wxDocManager::OnPrint(wxCommandEvent
& WXUNUSED(event
))
838 #if wxUSE_PRINTING_ARCHITECTURE
839 wxView
*view
= GetCurrentView();
843 wxPrintout
*printout
= view
->OnCreatePrintout();
847 printer
.Print(view
->GetFrame(), printout
, TRUE
);
851 #endif // wxUSE_PRINTING_ARCHITECTURE
854 void wxDocManager::OnPrintSetup(wxCommandEvent
& WXUNUSED(event
))
856 #if wxUSE_PRINTING_ARCHITECTURE
857 wxWindow
*parentWin
= wxTheApp
->GetTopWindow();
858 wxView
*view
= GetCurrentView();
860 parentWin
= view
->GetFrame();
862 wxPrintDialogData data
;
864 wxPrintDialog
printerDialog(parentWin
, &data
);
865 printerDialog
.GetPrintDialogData().SetSetupDialog(TRUE
);
866 printerDialog
.ShowModal();
867 #endif // wxUSE_PRINTING_ARCHITECTURE
870 void wxDocManager::OnPreview(wxCommandEvent
& WXUNUSED(event
))
872 #if wxUSE_PRINTING_ARCHITECTURE
873 wxView
*view
= GetCurrentView();
877 wxPrintout
*printout
= view
->OnCreatePrintout();
880 // Pass two printout objects: for preview, and possible printing.
881 wxPrintPreviewBase
*preview
= (wxPrintPreviewBase
*) NULL
;
882 preview
= new wxPrintPreview(printout
, view
->OnCreatePrintout());
884 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, (wxFrame
*)wxTheApp
->GetTopWindow(), _("Print Preview"),
885 wxPoint(100, 100), wxSize(600, 650));
886 frame
->Centre(wxBOTH
);
890 #endif // wxUSE_PRINTING_ARCHITECTURE
893 void wxDocManager::OnUndo(wxCommandEvent
& WXUNUSED(event
))
895 wxDocument
*doc
= GetCurrentDocument();
898 if (doc
->GetCommandProcessor())
899 doc
->GetCommandProcessor()->Undo();
902 void wxDocManager::OnRedo(wxCommandEvent
& WXUNUSED(event
))
904 wxDocument
*doc
= GetCurrentDocument();
907 if (doc
->GetCommandProcessor())
908 doc
->GetCommandProcessor()->Redo();
911 // Handlers for UI update commands
913 void wxDocManager::OnUpdateFileOpen(wxUpdateUIEvent
& event
)
915 event
.Enable( TRUE
);
918 void wxDocManager::OnUpdateFileClose(wxUpdateUIEvent
& event
)
920 wxDocument
*doc
= GetCurrentDocument();
921 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
924 void wxDocManager::OnUpdateFileRevert(wxUpdateUIEvent
& event
)
926 wxDocument
*doc
= GetCurrentDocument();
927 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
930 void wxDocManager::OnUpdateFileNew(wxUpdateUIEvent
& event
)
932 event
.Enable( TRUE
);
935 void wxDocManager::OnUpdateFileSave(wxUpdateUIEvent
& event
)
937 wxDocument
*doc
= GetCurrentDocument();
938 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
941 void wxDocManager::OnUpdateFileSaveAs(wxUpdateUIEvent
& event
)
943 wxDocument
*doc
= GetCurrentDocument();
944 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
947 void wxDocManager::OnUpdateUndo(wxUpdateUIEvent
& event
)
949 wxDocument
*doc
= GetCurrentDocument();
950 event
.Enable( (doc
&& doc
->GetCommandProcessor() && doc
->GetCommandProcessor()->CanUndo()) );
953 void wxDocManager::OnUpdateRedo(wxUpdateUIEvent
& event
)
955 wxDocument
*doc
= GetCurrentDocument();
956 event
.Enable( (doc
&& doc
->GetCommandProcessor() && doc
->GetCommandProcessor()->CanRedo()) );
959 void wxDocManager::OnUpdatePrint(wxUpdateUIEvent
& event
)
961 wxDocument
*doc
= GetCurrentDocument();
962 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
965 void wxDocManager::OnUpdatePrintSetup(wxUpdateUIEvent
& event
)
967 event
.Enable( TRUE
);
970 void wxDocManager::OnUpdatePreview(wxUpdateUIEvent
& event
)
972 wxDocument
*doc
= GetCurrentDocument();
973 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
976 wxView
*wxDocManager::GetCurrentView() const
979 return m_currentView
;
980 if (m_docs
.Number() == 1)
982 wxDocument
* doc
= (wxDocument
*) m_docs
.First()->Data();
983 return doc
->GetFirstView();
985 return (wxView
*) NULL
;
988 // Extend event processing to search the view's event table
989 bool wxDocManager::ProcessEvent(wxEvent
& event
)
991 wxView
* view
= GetCurrentView();
994 if (view
->ProcessEvent(event
))
997 return wxEvtHandler::ProcessEvent(event
);
1000 wxDocument
*wxDocManager::CreateDocument(const wxString
& path
, long flags
)
1002 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.Number()];
1005 for (i
= 0; i
< m_templates
.Number(); i
++)
1007 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Nth(i
)->Data());
1008 if (temp
->IsVisible())
1010 templates
[n
] = temp
;
1017 return (wxDocument
*) NULL
;
1020 // If we've reached the max number of docs, close the
1022 if (GetDocuments().Number() >= m_maxDocsOpen
)
1024 wxDocument
*doc
= (wxDocument
*)GetDocuments().First()->Data();
1027 // Implicitly deletes the document when
1028 // the last view is deleted
1029 doc
->DeleteAllViews();
1031 // Check we're really deleted
1032 if (m_docs
.Member(doc
))
1036 return (wxDocument
*) NULL
;
1039 // New document: user chooses a template, unless there's only one.
1040 if (flags
& wxDOC_NEW
)
1044 wxDocTemplate
*temp
= templates
[0];
1046 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
1049 newDoc
->SetDocumentName(temp
->GetDocumentName());
1050 newDoc
->SetDocumentTemplate(temp
);
1051 newDoc
->OnNewDocument();
1056 wxDocTemplate
*temp
= SelectDocumentType(templates
, n
);
1060 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
1063 newDoc
->SetDocumentName(temp
->GetDocumentName());
1064 newDoc
->SetDocumentTemplate(temp
);
1065 newDoc
->OnNewDocument();
1070 return (wxDocument
*) NULL
;
1073 // Existing document
1074 wxDocTemplate
*temp
= (wxDocTemplate
*) NULL
;
1076 wxString
path2(wxT(""));
1077 if (path
!= wxT(""))
1080 if (flags
& wxDOC_SILENT
)
1081 temp
= FindTemplateForPath(path2
);
1083 temp
= SelectDocumentPath(templates
, n
, path2
, flags
);
1089 wxDocument
*newDoc
= temp
->CreateDocument(path2
, flags
);
1092 newDoc
->SetDocumentName(temp
->GetDocumentName());
1093 newDoc
->SetDocumentTemplate(temp
);
1094 if (!newDoc
->OnOpenDocument(path2
))
1097 return (wxDocument
*) NULL
;
1099 AddFileToHistory(path2
);
1104 return (wxDocument
*) NULL
;
1107 wxView
*wxDocManager::CreateView(wxDocument
*doc
, long flags
)
1109 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.Number()];
1112 for (i
= 0; i
< m_templates
.Number(); i
++)
1114 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Nth(i
)->Data());
1115 if (temp
->IsVisible())
1117 if (temp
->GetDocumentName() == doc
->GetDocumentName())
1119 templates
[n
] = temp
;
1127 return (wxView
*) NULL
;
1131 wxDocTemplate
*temp
= templates
[0];
1133 wxView
*view
= temp
->CreateView(doc
, flags
);
1135 view
->SetViewName(temp
->GetViewName());
1139 wxDocTemplate
*temp
= SelectViewType(templates
, n
);
1143 wxView
*view
= temp
->CreateView(doc
, flags
);
1145 view
->SetViewName(temp
->GetViewName());
1149 return (wxView
*) NULL
;
1152 // Not yet implemented
1153 void wxDocManager::DeleteTemplate(wxDocTemplate
*WXUNUSED(temp
), long WXUNUSED(flags
))
1157 // Not yet implemented
1158 bool wxDocManager::FlushDoc(wxDocument
*WXUNUSED(doc
))
1163 wxDocument
*wxDocManager::GetCurrentDocument() const
1166 return m_currentView
->GetDocument();
1168 return (wxDocument
*) NULL
;
1171 // Make a default document name
1172 bool wxDocManager::MakeDefaultName(wxString
& name
)
1174 name
.Printf(_("unnamed%d"), m_defaultDocumentNameCounter
);
1175 m_defaultDocumentNameCounter
++;
1180 // Make a frame title (override this to do something different)
1181 // If docName is empty, a document is not currently active.
1182 wxString
wxDocManager::MakeFrameTitle(wxDocument
* doc
)
1184 wxString appName
= wxTheApp
->GetAppName();
1191 doc
->GetPrintableName(docName
);
1192 title
= docName
+ wxString(_(" - ")) + appName
;
1198 // Not yet implemented
1199 wxDocTemplate
*wxDocManager::MatchTemplate(const wxString
& WXUNUSED(path
))
1201 return (wxDocTemplate
*) NULL
;
1204 // File history management
1205 void wxDocManager::AddFileToHistory(const wxString
& file
)
1208 m_fileHistory
->AddFileToHistory(file
);
1211 void wxDocManager::RemoveFileFromHistory(int i
)
1214 m_fileHistory
->RemoveFileFromHistory(i
);
1217 wxString
wxDocManager::GetHistoryFile(int i
) const
1222 histFile
= m_fileHistory
->GetHistoryFile(i
);
1227 void wxDocManager::FileHistoryUseMenu(wxMenu
*menu
)
1230 m_fileHistory
->UseMenu(menu
);
1233 void wxDocManager::FileHistoryRemoveMenu(wxMenu
*menu
)
1236 m_fileHistory
->RemoveMenu(menu
);
1240 void wxDocManager::FileHistoryLoad(wxConfigBase
& config
)
1243 m_fileHistory
->Load(config
);
1246 void wxDocManager::FileHistorySave(wxConfigBase
& config
)
1249 m_fileHistory
->Save(config
);
1253 void wxDocManager::FileHistoryAddFilesToMenu(wxMenu
* menu
)
1256 m_fileHistory
->AddFilesToMenu(menu
);
1259 void wxDocManager::FileHistoryAddFilesToMenu()
1262 m_fileHistory
->AddFilesToMenu();
1265 int wxDocManager::GetNoHistoryFiles() const
1268 return m_fileHistory
->GetNoHistoryFiles();
1274 // Find out the document template via matching in the document file format
1275 // against that of the template
1276 wxDocTemplate
*wxDocManager::FindTemplateForPath(const wxString
& path
)
1278 wxDocTemplate
*theTemplate
= (wxDocTemplate
*) NULL
;
1280 // Find the template which this extension corresponds to
1282 for (i
= 0; i
< m_templates
.Number(); i
++)
1284 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Nth(i
)->Data();
1285 if ( temp
->FileMatchesTemplate(path
) )
1294 // Prompts user to open a file, using file specs in templates.
1295 // How to implement in wxWindows? Must extend the file selector
1296 // dialog or implement own; OR match the extension to the
1297 // template extension.
1299 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**templates
,
1300 #if defined(__WXMSW__) || defined(__WXGTK__)
1303 int WXUNUSED(noTemplates
),
1306 long WXUNUSED(flags
),
1307 bool WXUNUSED(save
))
1309 // We can only have multiple filters in Windows and GTK
1310 #if defined(__WXMSW__) || defined(__WXGTK__)
1314 for (i
= 0; i
< noTemplates
; i
++)
1316 if (templates
[i
]->IsVisible())
1318 // add a '|' to separate this filter from the previous one
1319 if ( !descrBuf
.IsEmpty() )
1320 descrBuf
<< wxT('|');
1322 descrBuf
<< templates
[i
]->GetDescription()
1323 << wxT(" (") << templates
[i
]->GetFileFilter() << wxT(") |")
1324 << templates
[i
]->GetFileFilter();
1328 wxString descrBuf
= wxT("*.*");
1331 int FilterIndex
= 0;
1332 wxString pathTmp
= wxFileSelectorEx(_("Select a file"),
1338 wxTheApp
->GetTopWindow());
1340 if (!pathTmp
.IsEmpty())
1342 m_lastDirectory
= wxPathOnly(pathTmp
);
1346 // This is dodgy in that we're selecting the template on the
1347 // basis of the file extension, which may not be a standard
1348 // one. We really want to know exactly which template was
1349 // chosen by using a more advanced file selector.
1350 wxDocTemplate
*theTemplate
= FindTemplateForPath(path
);
1352 theTemplate
= templates
[FilterIndex
];
1359 return (wxDocTemplate
*) NULL
;
1362 // In all other windowing systems, until we have more advanced
1363 // file selectors, we must select the document type (template) first, and
1364 // _then_ pop up the file selector.
1365 wxDocTemplate
*temp
= SelectDocumentType(templates
, noTemplates
);
1367 return (wxDocTemplate
*) NULL
;
1369 wxChar
*pathTmp
= wxFileSelector(_("Select a file"), wxT(""), wxT(""),
1370 temp
->GetDefaultExtension(),
1371 temp
->GetFileFilter(),
1372 0, wxTheApp
->GetTopWindow());
1380 return (wxDocTemplate
*) NULL
;
1384 wxDocTemplate
*wxDocManager::SelectDocumentType(wxDocTemplate
**templates
,
1387 wxChar
**strings
= new wxChar
*[noTemplates
];
1388 wxChar
**data
= new wxChar
*[noTemplates
];
1391 for (i
= 0; i
< noTemplates
; i
++)
1393 if (templates
[i
]->IsVisible())
1395 strings
[n
] = (wxChar
*)templates
[i
]->m_description
.c_str();
1396 data
[n
] = (wxChar
*)templates
[i
];
1404 return (wxDocTemplate
*) NULL
;
1408 wxDocTemplate
*temp
= (wxDocTemplate
*)data
[0];
1414 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData(_("Select a document template"), _("Templates"), n
,
1415 strings
, (void **)data
);
1421 wxDocTemplate
*wxDocManager::SelectViewType(wxDocTemplate
**templates
,
1424 wxChar
**strings
= new wxChar
*[noTemplates
];
1425 wxChar
**data
= new wxChar
*[noTemplates
];
1428 for (i
= 0; i
< noTemplates
; i
++)
1430 if (templates
[i
]->IsVisible() && (templates
[i
]->GetViewName() != wxT("")))
1432 strings
[n
] = (wxChar
*)templates
[i
]->m_viewTypeName
.c_str();
1433 data
[n
] = (wxChar
*)templates
[i
];
1437 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData(_("Select a document view"), _("Views"), n
,
1438 strings
, (void **)data
);
1444 void wxDocManager::AssociateTemplate(wxDocTemplate
*temp
)
1446 if (!m_templates
.Member(temp
))
1447 m_templates
.Append(temp
);
1450 void wxDocManager::DisassociateTemplate(wxDocTemplate
*temp
)
1452 m_templates
.DeleteObject(temp
);
1455 // Add and remove a document from the manager's list
1456 void wxDocManager::AddDocument(wxDocument
*doc
)
1458 if (!m_docs
.Member(doc
))
1462 void wxDocManager::RemoveDocument(wxDocument
*doc
)
1464 m_docs
.DeleteObject(doc
);
1467 // Views or windows should inform the document manager
1468 // when a view is going in or out of focus
1469 void wxDocManager::ActivateView(wxView
*view
, bool activate
, bool WXUNUSED(deleting
))
1471 // If we're deactiving, and if we're not actually deleting the view, then
1472 // don't reset the current view because we may be going to
1473 // a window without a view.
1474 // WHAT DID I MEAN BY THAT EXACTLY?
1478 if (m_currentView == view)
1479 m_currentView = NULL;
1485 m_currentView
= view
;
1487 m_currentView
= (wxView
*) NULL
;
1491 // ----------------------------------------------------------------------------
1492 // Default document child frame
1493 // ----------------------------------------------------------------------------
1495 BEGIN_EVENT_TABLE(wxDocChildFrame
, wxFrame
)
1496 EVT_ACTIVATE(wxDocChildFrame::OnActivate
)
1497 EVT_CLOSE(wxDocChildFrame::OnCloseWindow
)
1500 wxDocChildFrame::wxDocChildFrame(wxDocument
*doc
,
1504 const wxString
& title
,
1508 const wxString
& name
)
1509 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1511 m_childDocument
= doc
;
1514 view
->SetFrame(this);
1517 wxDocChildFrame::~wxDocChildFrame()
1521 // Extend event processing to search the view's event table
1522 bool wxDocChildFrame::ProcessEvent(wxEvent
& event
)
1525 m_childView
->Activate(TRUE
);
1527 if ( !m_childView
|| ! m_childView
->ProcessEvent(event
) )
1529 // Only hand up to the parent if it's a menu command
1530 if (!event
.IsKindOf(CLASSINFO(wxCommandEvent
)) || !GetParent() || !GetParent()->ProcessEvent(event
))
1531 return wxEvtHandler::ProcessEvent(event
);
1539 void wxDocChildFrame::OnActivate(wxActivateEvent
& event
)
1541 wxFrame::OnActivate(event
);
1544 m_childView
->Activate(event
.GetActive());
1547 void wxDocChildFrame::OnCloseWindow(wxCloseEvent
& event
)
1552 if (!event
.CanVeto())
1553 ans
= TRUE
; // Must delete.
1555 ans
= m_childView
->Close(FALSE
); // FALSE means don't delete associated window
1559 m_childView
->Activate(FALSE
);
1561 m_childView
= (wxView
*) NULL
;
1562 m_childDocument
= (wxDocument
*) NULL
;
1573 // ----------------------------------------------------------------------------
1574 // Default parent frame
1575 // ----------------------------------------------------------------------------
1577 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1578 EVT_MENU(wxID_EXIT
, wxDocParentFrame::OnExit
)
1579 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE9
, wxDocParentFrame::OnMRUFile
)
1580 EVT_CLOSE(wxDocParentFrame::OnCloseWindow
)
1583 wxDocParentFrame::wxDocParentFrame(wxDocManager
*manager
,
1586 const wxString
& title
,
1590 const wxString
& name
)
1591 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1593 m_docManager
= manager
;
1596 void wxDocParentFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
1601 void wxDocParentFrame::OnMRUFile(wxCommandEvent
& event
)
1603 int n
= event
.GetSelection() - wxID_FILE1
; // the index in MRU list
1604 wxString
filename(m_docManager
->GetHistoryFile(n
));
1605 if ( !filename
.IsEmpty() )
1607 // verify that the file exists before doing anything else
1608 if ( wxFile::Exists(filename
) )
1611 (void)m_docManager
->CreateDocument(filename
, wxDOC_SILENT
);
1615 // remove the bogus filename from the MRU list and notify the user
1617 m_docManager
->RemoveFileFromHistory(n
);
1619 wxLogError(_("The file '%s' doesn't exist and couldn't be opened.\n"
1620 "It has been also removed from the MRU files list."),
1626 // Extend event processing to search the view's event table
1627 bool wxDocParentFrame::ProcessEvent(wxEvent
& event
)
1629 // Try the document manager, then do default processing
1630 if (!m_docManager
|| !m_docManager
->ProcessEvent(event
))
1631 return wxEvtHandler::ProcessEvent(event
);
1636 // Define the behaviour for the frame closing
1637 // - must delete all frames except for the main one.
1638 void wxDocParentFrame::OnCloseWindow(wxCloseEvent
& event
)
1640 if (m_docManager
->Clear(!event
.CanVeto()))
1648 #if wxUSE_PRINTING_ARCHITECTURE
1650 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
)
1653 m_printoutView
= view
;
1656 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
1660 // Get the logical pixels per inch of screen and printer
1661 int ppiScreenX
, ppiScreenY
;
1662 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
1663 int ppiPrinterX
, ppiPrinterY
;
1664 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
1666 // This scales the DC so that the printout roughly represents the
1667 // the screen scaling. The text point size _should_ be the right size
1668 // but in fact is too small for some reason. This is a detail that will
1669 // need to be addressed at some point but can be fudged for the
1671 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
1673 // Now we have to check in case our real page size is reduced
1674 // (e.g. because we're drawing to a print preview memory DC)
1675 int pageWidth
, pageHeight
;
1677 dc
->GetSize(&w
, &h
);
1678 GetPageSizePixels(&pageWidth
, &pageHeight
);
1680 // If printer pageWidth == current DC width, then this doesn't
1681 // change. But w might be the preview bitmap width, so scale down.
1682 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
1683 dc
->SetUserScale(overallScale
, overallScale
);
1687 m_printoutView
->OnDraw(dc
);
1692 bool wxDocPrintout::HasPage(int pageNum
)
1694 return (pageNum
== 1);
1697 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
1699 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
1705 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
1713 #endif // wxUSE_PRINTING_ARCHITECTURE
1715 // ----------------------------------------------------------------------------
1716 // Command processing framework
1717 // ----------------------------------------------------------------------------
1719 wxCommand::wxCommand(bool canUndoIt
, const wxString
& name
)
1721 m_canUndo
= canUndoIt
;
1722 m_commandName
= name
;
1725 wxCommand::~wxCommand()
1729 // Command processor
1730 wxCommandProcessor::wxCommandProcessor(int maxCommands
)
1732 m_maxNoCommands
= maxCommands
;
1733 m_currentCommand
= (wxNode
*) NULL
;
1734 m_commandEditMenu
= (wxMenu
*) NULL
;
1737 wxCommandProcessor::~wxCommandProcessor()
1742 // Pass a command to the processor. The processor calls Do();
1743 // if successful, is appended to the command history unless
1744 // storeIt is FALSE.
1745 bool wxCommandProcessor::Submit(wxCommand
*command
, bool storeIt
)
1747 bool success
= command
->Do();
1748 if (success
&& storeIt
)
1750 if (m_commands
.Number() == m_maxNoCommands
)
1752 wxNode
*firstNode
= m_commands
.First();
1753 wxCommand
*firstCommand
= (wxCommand
*)firstNode
->Data();
1754 delete firstCommand
;
1758 // Correct a bug: we must chop off the current 'branch'
1759 // so that we're at the end of the command list.
1760 if (!m_currentCommand
)
1764 wxNode
*node
= m_currentCommand
->Next();
1767 wxNode
*next
= node
->Next();
1768 delete (wxCommand
*)node
->Data();
1774 m_commands
.Append(command
);
1775 m_currentCommand
= m_commands
.Last();
1781 bool wxCommandProcessor::Undo()
1783 if (m_currentCommand
)
1785 wxCommand
*command
= (wxCommand
*)m_currentCommand
->Data();
1786 if (command
->CanUndo())
1788 bool success
= command
->Undo();
1791 m_currentCommand
= m_currentCommand
->Previous();
1800 bool wxCommandProcessor::Redo()
1802 wxCommand
*redoCommand
= (wxCommand
*) NULL
;
1803 wxNode
*redoNode
= (wxNode
*) NULL
;
1804 if (m_currentCommand
&& m_currentCommand
->Next())
1806 redoCommand
= (wxCommand
*)m_currentCommand
->Next()->Data();
1807 redoNode
= m_currentCommand
->Next();
1811 if (m_commands
.Number() > 0)
1813 redoCommand
= (wxCommand
*)m_commands
.First()->Data();
1814 redoNode
= m_commands
.First();
1820 bool success
= redoCommand
->Do();
1823 m_currentCommand
= redoNode
;
1831 bool wxCommandProcessor::CanUndo() const
1833 if (m_currentCommand
)
1834 return ((wxCommand
*)m_currentCommand
->Data())->CanUndo();
1838 bool wxCommandProcessor::CanRedo() const
1840 if ((m_currentCommand
!= (wxNode
*) NULL
) && (m_currentCommand
->Next() == (wxNode
*) NULL
))
1843 if ((m_currentCommand
!= (wxNode
*) NULL
) && (m_currentCommand
->Next() != (wxNode
*) NULL
))
1846 if ((m_currentCommand
== (wxNode
*) NULL
) && (m_commands
.Number() > 0))
1852 void wxCommandProcessor::Initialize()
1854 m_currentCommand
= m_commands
.Last();
1858 void wxCommandProcessor::SetMenuStrings()
1860 if (m_commandEditMenu
)
1863 if (m_currentCommand
)
1865 wxCommand
*command
= (wxCommand
*)m_currentCommand
->Data();
1866 wxString
commandName(command
->GetName());
1867 if (commandName
== wxT("")) commandName
= _("Unnamed command");
1868 bool canUndo
= command
->CanUndo();
1870 buf
= wxString(_("&Undo ")) + commandName
;
1872 buf
= wxString(_("Can't &Undo ")) + commandName
;
1874 m_commandEditMenu
->SetLabel(wxID_UNDO
, buf
);
1875 m_commandEditMenu
->Enable(wxID_UNDO
, canUndo
);
1877 // We can redo, if we're not at the end of the history.
1878 if (m_currentCommand
->Next())
1880 wxCommand
*redoCommand
= (wxCommand
*)m_currentCommand
->Next()->Data();
1881 wxString
redoCommandName(redoCommand
->GetName());
1882 if (redoCommandName
== wxT("")) redoCommandName
= _("Unnamed command");
1883 buf
= wxString(_("&Redo ")) + redoCommandName
;
1884 m_commandEditMenu
->SetLabel(wxID_REDO
, buf
);
1885 m_commandEditMenu
->Enable(wxID_REDO
, TRUE
);
1889 m_commandEditMenu
->SetLabel(wxID_REDO
, _("&Redo"));
1890 m_commandEditMenu
->Enable(wxID_REDO
, FALSE
);
1895 m_commandEditMenu
->SetLabel(wxID_UNDO
, _("&Undo"));
1896 m_commandEditMenu
->Enable(wxID_UNDO
, FALSE
);
1898 if (m_commands
.Number() == 0)
1900 m_commandEditMenu
->SetLabel(wxID_REDO
, _("&Redo"));
1901 m_commandEditMenu
->Enable(wxID_REDO
, FALSE
);
1905 // currentCommand is NULL but there are commands: this means that
1906 // we've undone to the start of the list, but can redo the first.
1907 wxCommand
*redoCommand
= (wxCommand
*)m_commands
.First()->Data();
1908 wxString
redoCommandName(redoCommand
->GetName());
1909 if (redoCommandName
== wxT("")) redoCommandName
= _("Unnamed command");
1910 buf
= wxString(_("&Redo ")) + redoCommandName
;
1911 m_commandEditMenu
->SetLabel(wxID_REDO
, buf
);
1912 m_commandEditMenu
->Enable(wxID_REDO
, TRUE
);
1918 void wxCommandProcessor::ClearCommands()
1920 wxNode
*node
= m_commands
.First();
1923 wxCommand
*command
= (wxCommand
*)node
->Data();
1926 node
= m_commands
.First();
1928 m_currentCommand
= (wxNode
*) NULL
;
1931 // ----------------------------------------------------------------------------
1932 // File history processor
1933 // ----------------------------------------------------------------------------
1935 wxFileHistory::wxFileHistory(int maxFiles
)
1937 m_fileMaxFiles
= maxFiles
;
1939 m_fileHistory
= new wxChar
*[m_fileMaxFiles
];
1942 wxFileHistory::~wxFileHistory()
1945 for (i
= 0; i
< m_fileHistoryN
; i
++)
1946 delete[] m_fileHistory
[i
];
1947 delete[] m_fileHistory
;
1950 // File history management
1951 void wxFileHistory::AddFileToHistory(const wxString
& file
)
1954 // Check we don't already have this file
1955 for (i
= 0; i
< m_fileHistoryN
; i
++)
1957 if (m_fileHistory
[i
] && wxString(m_fileHistory
[i
]) == file
)
1961 // Add to the project file history:
1962 // Move existing files (if any) down so we can insert file at beginning.
1964 // First delete filename that has popped off the end of the array (if any)
1965 if (m_fileHistoryN
== m_fileMaxFiles
)
1967 delete[] m_fileHistory
[m_fileMaxFiles
-1];
1968 m_fileHistory
[m_fileMaxFiles
-1] = (wxChar
*) NULL
;
1970 if (m_fileHistoryN
< m_fileMaxFiles
)
1972 wxNode
* node
= m_fileMenus
.First();
1975 wxMenu
* menu
= (wxMenu
*) node
->Data();
1976 if (m_fileHistoryN
== 0)
1977 menu
->AppendSeparator();
1978 menu
->Append(wxID_FILE1
+m_fileHistoryN
, _("[EMPTY]"));
1979 node
= node
->Next();
1983 // Shuffle filenames down
1984 for (i
= (m_fileHistoryN
-1); i
> 0; i
--)
1986 m_fileHistory
[i
] = m_fileHistory
[i
-1];
1988 m_fileHistory
[0] = copystring(file
);
1990 for (i
= 0; i
< m_fileHistoryN
; i
++)
1991 if (m_fileHistory
[i
])
1994 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
1995 wxNode
* node
= m_fileMenus
.First();
1998 wxMenu
* menu
= (wxMenu
*) node
->Data();
1999 menu
->SetLabel(wxID_FILE1
+i
, buf
);
2000 node
= node
->Next();
2005 void wxFileHistory::RemoveFileFromHistory(int i
)
2007 wxCHECK_RET( i
< m_fileHistoryN
,
2008 wxT("invalid index in wxFileHistory::RemoveFileFromHistory") );
2010 wxNode
* node
= m_fileMenus
.First();
2013 wxMenu
* menu
= (wxMenu
*) node
->Data();
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 // shuffle filenames up
2026 for ( j
= i
; j
< m_fileHistoryN
- 1; j
++ )
2028 buf
.Printf(s_MRUEntryFormat
, j
+ 1, m_fileHistory
[j
]);
2029 menu
->SetLabel(wxID_FILE1
+ j
, buf
);
2032 node
= node
->Next();
2034 // delete the last menu item which is unused now
2035 menu
->Delete(wxID_FILE1
+ m_fileHistoryN
- 1);
2037 // delete the last separator too if no more files are left
2038 if ( m_fileHistoryN
== 1 )
2040 wxMenuItemList::Node
*node
= menu
->GetMenuItems().GetLast();
2043 wxMenuItem
*menuItem
= node
->GetData();
2044 if ( menuItem
->IsSeparator() )
2046 menu
->Delete(menuItem
);
2048 //else: should we search backwards for the last separator?
2050 //else: menu is empty somehow
2057 wxString
wxFileHistory::GetHistoryFile(int i
) const
2060 if ( i
< m_fileHistoryN
)
2062 s
= m_fileHistory
[i
];
2066 wxFAIL_MSG( wxT("bad index in wxFileHistory::GetHistoryFile") );
2072 void wxFileHistory::UseMenu(wxMenu
*menu
)
2074 if (!m_fileMenus
.Member(menu
))
2075 m_fileMenus
.Append(menu
);
2078 void wxFileHistory::RemoveMenu(wxMenu
*menu
)
2080 m_fileMenus
.DeleteObject(menu
);
2084 void wxFileHistory::Load(wxConfigBase
& config
)
2088 buf
.Printf(wxT("file%d"), m_fileHistoryN
+1);
2089 wxString historyFile
;
2090 while ((m_fileHistoryN
<= m_fileMaxFiles
) && config
.Read(buf
, &historyFile
) && (historyFile
!= wxT("")))
2092 m_fileHistory
[m_fileHistoryN
] = copystring((const wxChar
*) historyFile
);
2094 buf
.Printf(wxT("file%d"), m_fileHistoryN
+1);
2095 historyFile
= wxT("");
2100 void wxFileHistory::Save(wxConfigBase
& config
)
2103 for (i
= 0; i
< m_fileHistoryN
; i
++)
2106 buf
.Printf(wxT("file%d"), i
+1);
2107 config
.Write(buf
, wxString(m_fileHistory
[i
]));
2110 #endif // wxUSE_CONFIG
2112 void wxFileHistory::AddFilesToMenu()
2114 if (m_fileHistoryN
> 0)
2116 wxNode
* node
= m_fileMenus
.First();
2119 wxMenu
* menu
= (wxMenu
*) node
->Data();
2120 menu
->AppendSeparator();
2122 for (i
= 0; i
< m_fileHistoryN
; i
++)
2124 if (m_fileHistory
[i
])
2127 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
2128 menu
->Append(wxID_FILE1
+i
, buf
);
2131 node
= node
->Next();
2136 void wxFileHistory::AddFilesToMenu(wxMenu
* menu
)
2138 if (m_fileHistoryN
> 0)
2140 menu
->AppendSeparator();
2142 for (i
= 0; i
< m_fileHistoryN
; i
++)
2144 if (m_fileHistory
[i
])
2147 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
2148 menu
->Append(wxID_FILE1
+i
, buf
);
2154 // ----------------------------------------------------------------------------
2155 // Permits compatibility with existing file formats and functions that
2156 // manipulate files directly
2157 // ----------------------------------------------------------------------------
2159 #if wxUSE_STD_IOSTREAM
2160 bool wxTransferFileToStream(const wxString
& filename
, ostream
& stream
)
2165 if ((fd1
= fopen (filename
.fn_str(), "rb")) == NULL
)
2168 while ((ch
= getc (fd1
)) != EOF
)
2169 stream
<< (unsigned char)ch
;
2175 bool wxTransferStreamToFile(istream
& stream
, const wxString
& filename
)
2180 if ((fd1
= fopen (filename
.fn_str(), "wb")) == NULL
)
2185 while (!stream
.eof())
2196 #endif // wxUSE_DOC_VIEW_ARCHITECTURE