1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/docview.cpp
3 // Purpose: Document/view classes
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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"
49 #include "wx/filename.h"
56 #if wxUSE_PRINTING_ARCHITECTURE
57 #include "wx/prntbase.h"
58 #include "wx/printdlg.h"
61 #include "wx/msgdlg.h"
62 #include "wx/choicdlg.h"
63 #include "wx/docview.h"
64 #include "wx/confbase.h"
66 #include "wx/cmdproc.h"
71 #if wxUSE_STD_IOSTREAM
72 #include "wx/ioswrap.h"
79 #include "wx/wfstream.h"
82 // ----------------------------------------------------------------------------
84 // ----------------------------------------------------------------------------
86 IMPLEMENT_ABSTRACT_CLASS(wxDocument
, wxEvtHandler
)
87 IMPLEMENT_ABSTRACT_CLASS(wxView
, wxEvtHandler
)
88 IMPLEMENT_ABSTRACT_CLASS(wxDocTemplate
, wxObject
)
89 IMPLEMENT_DYNAMIC_CLASS(wxDocManager
, wxEvtHandler
)
90 IMPLEMENT_CLASS(wxDocChildFrame
, wxFrame
)
91 IMPLEMENT_CLASS(wxDocParentFrame
, wxFrame
)
93 #if wxUSE_PRINTING_ARCHITECTURE
94 IMPLEMENT_DYNAMIC_CLASS(wxDocPrintout
, wxPrintout
)
97 IMPLEMENT_DYNAMIC_CLASS(wxFileHistory
, wxObject
)
99 // ----------------------------------------------------------------------------
100 // function prototypes
101 // ----------------------------------------------------------------------------
103 static inline wxString
FindExtension(const wxChar
*path
);
104 static wxWindow
* wxFindSuitableParent(void);
106 // ----------------------------------------------------------------------------
108 // ----------------------------------------------------------------------------
110 static const wxChar
*s_MRUEntryFormat
= wxT("&%d %s");
112 // ============================================================================
114 // ============================================================================
116 // ----------------------------------------------------------------------------
118 // ----------------------------------------------------------------------------
120 static wxString
FindExtension(const wxChar
*path
)
123 wxSplitPath(path
, NULL
, NULL
, &ext
);
125 // VZ: extensions are considered not case sensitive - is this really a good
127 return ext
.MakeLower();
130 // ----------------------------------------------------------------------------
131 // Definition of wxDocument
132 // ----------------------------------------------------------------------------
134 wxDocument::wxDocument(wxDocument
*parent
)
136 m_documentModified
= false;
137 m_documentParent
= parent
;
138 m_documentTemplate
= (wxDocTemplate
*) NULL
;
139 m_commandProcessor
= (wxCommandProcessor
*) NULL
;
143 bool wxDocument::DeleteContents()
148 wxDocument::~wxDocument()
152 if (m_commandProcessor
)
153 delete m_commandProcessor
;
155 if (GetDocumentManager())
156 GetDocumentManager()->RemoveDocument(this);
158 // Not safe to do here, since it'll invoke virtual view functions
159 // expecting to see valid derived objects: and by the time we get here,
160 // we've called destructors higher up.
164 bool wxDocument::Close()
166 if (OnSaveModified())
167 return OnCloseDocument();
172 bool wxDocument::OnCloseDocument()
174 // Tell all views that we're about to close
181 // Note that this implicitly deletes the document when the last view is
183 bool wxDocument::DeleteAllViews()
185 wxDocManager
* manager
= GetDocumentManager();
186 wxList::iterator it
, en
;
188 for ( it
= m_documentViews
.begin(), en
= m_documentViews
.end();
192 wxView
*view
= (wxView
*)*it
;
196 wxList::iterator next
= it
; ++next
;
198 delete view
; // Deletes node implicitly
201 // If we haven't yet deleted the document (for example
202 // if there were no views) then delete it.
203 if (manager
&& manager
->GetDocuments().Member(this))
209 wxView
*wxDocument::GetFirstView() const
211 if (m_documentViews
.GetCount() == 0)
212 return (wxView
*) NULL
;
213 return (wxView
*)m_documentViews
.GetFirst()->GetData();
216 wxDocManager
*wxDocument::GetDocumentManager() const
218 return (m_documentTemplate
? m_documentTemplate
->GetDocumentManager() : (wxDocManager
*) NULL
);
221 bool wxDocument::OnNewDocument()
223 if (!OnSaveModified())
226 if (OnCloseDocument()==false) return false;
229 SetDocumentSaved(false);
232 GetDocumentManager()->MakeDefaultName(name
);
234 SetFilename(name
, true);
239 bool wxDocument::Save()
241 if (!IsModified() && m_savedYet
)
244 if ( m_documentFile
.empty() || !m_savedYet
)
247 return OnSaveDocument(m_documentFile
);
250 bool wxDocument::SaveAs()
252 wxDocTemplate
*docTemplate
= GetDocumentTemplate();
256 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
257 wxString filter
= docTemplate
->GetDescription() + wxT(" (") + docTemplate
->GetFileFilter() + wxT(")|") + docTemplate
->GetFileFilter();
259 // Now see if there are some other template with identical view and document
260 // classes, whose filters may also be used.
262 if (docTemplate
->GetViewClassInfo() && docTemplate
->GetDocClassInfo())
264 wxList::compatibility_iterator node
= wxDocManager::GetDocumentManager()->GetTemplates().GetFirst();
267 wxDocTemplate
*t
= (wxDocTemplate
*) node
->GetData();
269 if (t
->IsVisible() && t
!= docTemplate
&&
270 t
->GetViewClassInfo() == docTemplate
->GetViewClassInfo() &&
271 t
->GetDocClassInfo() == docTemplate
->GetDocClassInfo())
273 // add a '|' to separate this filter from the previous one
274 if ( !filter
.IsEmpty() )
277 filter
<< t
->GetDescription() << wxT(" (") << t
->GetFileFilter() << wxT(") |")
278 << t
->GetFileFilter();
281 node
= node
->GetNext();
285 wxString filter
= docTemplate
->GetFileFilter() ;
287 wxString tmp
= wxFileSelector(_("Save as"),
288 docTemplate
->GetDirectory(),
289 wxFileNameFromPath(GetFilename()),
290 docTemplate
->GetDefaultExtension(),
292 wxSAVE
| wxOVERWRITE_PROMPT
,
293 GetDocumentWindow());
298 wxString
fileName(tmp
);
299 wxString path
, name
, ext
;
300 wxSplitPath(fileName
, & path
, & name
, & ext
);
302 if (ext
.IsEmpty() || ext
== wxT(""))
304 fileName
+= wxT(".");
305 fileName
+= docTemplate
->GetDefaultExtension();
308 SetFilename(fileName
);
309 SetTitle(wxFileNameFromPath(fileName
));
311 // Notify the views that the filename has changed
312 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
315 wxView
*view
= (wxView
*)node
->GetData();
316 view
->OnChangeFilename();
317 node
= node
->GetNext();
320 // Files that were not saved correctly are not added to the FileHistory.
321 if (!OnSaveDocument(m_documentFile
))
324 // A file that doesn't use the default extension of its document template cannot be opened
325 // via the FileHistory, so we do not add it.
326 if (docTemplate
->FileMatchesTemplate(fileName
))
328 GetDocumentManager()->AddFileToHistory(fileName
);
332 // The user will probably not be able to open the file again, so
333 // we could warn about the wrong file-extension here.
338 bool wxDocument::OnSaveDocument(const wxString
& file
)
343 if ( !DoSaveDocument(file
) )
348 SetDocumentSaved(true);
350 wxFileName
fn(file
) ;
351 fn
.MacSetDefaultTypeAndCreator() ;
356 bool wxDocument::OnOpenDocument(const wxString
& file
)
358 if (!OnSaveModified())
361 if ( !DoOpenDocument(file
) )
364 SetFilename(file
, true);
373 #if wxUSE_STD_IOSTREAM
374 wxSTD istream
& wxDocument::LoadObject(wxSTD istream
& stream
)
376 wxInputStream
& wxDocument::LoadObject(wxInputStream
& stream
)
382 #if wxUSE_STD_IOSTREAM
383 wxSTD ostream
& wxDocument::SaveObject(wxSTD ostream
& stream
)
385 wxOutputStream
& wxDocument::SaveObject(wxOutputStream
& stream
)
391 bool wxDocument::Revert()
397 // Get title, or filename if no title, else unnamed
398 bool wxDocument::GetPrintableName(wxString
& buf
) const
400 if (m_documentTitle
!= wxT(""))
402 buf
= m_documentTitle
;
405 else if (m_documentFile
!= wxT(""))
407 buf
= wxFileNameFromPath(m_documentFile
);
417 wxWindow
*wxDocument::GetDocumentWindow() const
419 wxView
*view
= GetFirstView();
421 return view
->GetFrame();
423 return wxTheApp
->GetTopWindow();
426 wxCommandProcessor
*wxDocument::OnCreateCommandProcessor()
428 return new wxCommandProcessor
;
431 // true if safe to close
432 bool wxDocument::OnSaveModified()
437 GetPrintableName(title
);
440 if (wxTheApp
->GetAppName() != wxT(""))
441 msgTitle
= wxTheApp
->GetAppName();
443 msgTitle
= wxString(_("Warning"));
446 prompt
.Printf(_("Do you want to save changes to document %s?"),
447 (const wxChar
*)title
);
448 int res
= wxMessageBox(prompt
, msgTitle
,
449 wxYES_NO
|wxCANCEL
|wxICON_QUESTION
,
450 GetDocumentWindow());
456 else if (res
== wxYES
)
458 else if (res
== wxCANCEL
)
464 bool wxDocument::Draw(wxDC
& WXUNUSED(context
))
469 bool wxDocument::AddView(wxView
*view
)
471 if (!m_documentViews
.Member(view
))
473 m_documentViews
.Append(view
);
479 bool wxDocument::RemoveView(wxView
*view
)
481 (void)m_documentViews
.DeleteObject(view
);
486 bool wxDocument::OnCreate(const wxString
& WXUNUSED(path
), long flags
)
488 if (GetDocumentTemplate()->CreateView(this, flags
))
494 // Called after a view is added or removed.
495 // The default implementation deletes the document if
496 // there are no more views.
497 void wxDocument::OnChangedViewList()
499 if (m_documentViews
.GetCount() == 0)
501 if (OnSaveModified())
508 void wxDocument::UpdateAllViews(wxView
*sender
, wxObject
*hint
)
510 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
513 wxView
*view
= (wxView
*)node
->GetData();
515 view
->OnUpdate(sender
, hint
);
516 node
= node
->GetNext();
520 void wxDocument::NotifyClosing()
522 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
525 wxView
*view
= (wxView
*)node
->GetData();
526 view
->OnClosingDocument();
527 node
= node
->GetNext();
531 void wxDocument::SetFilename(const wxString
& filename
, bool notifyViews
)
533 m_documentFile
= filename
;
536 // Notify the views that the filename has changed
537 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
540 wxView
*view
= (wxView
*)node
->GetData();
541 view
->OnChangeFilename();
542 node
= node
->GetNext();
547 bool wxDocument::DoSaveDocument(const wxString
& file
)
550 if (wxTheApp
->GetAppName() != wxT(""))
551 msgTitle
= wxTheApp
->GetAppName();
553 msgTitle
= wxString(_("File error"));
555 #if wxUSE_STD_IOSTREAM
556 wxSTD ofstream
store(file
.mb_str());
557 if (store
.fail() || store
.bad())
559 wxFileOutputStream
store(file
);
560 if (store
.GetLastError() != wxSTREAM_NO_ERROR
)
563 (void)wxMessageBox(_("Sorry, could not open this file for saving."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
564 GetDocumentWindow());
568 if (!SaveObject(store
))
570 (void)wxMessageBox(_("Sorry, could not save this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
571 GetDocumentWindow());
579 bool wxDocument::DoOpenDocument(const wxString
& file
)
582 if (wxTheApp
->GetAppName() != wxT(""))
583 msgTitle
= wxTheApp
->GetAppName();
585 msgTitle
= wxString(_("File error"));
587 #if wxUSE_STD_IOSTREAM
588 wxSTD ifstream
store(file
.mb_str());
589 if (store
.fail() || store
.bad())
591 wxFileInputStream
store(file
);
592 if (store
.GetLastError() != wxSTREAM_NO_ERROR
)
595 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
|wxICON_EXCLAMATION
,
596 GetDocumentWindow());
599 #if wxUSE_STD_IOSTREAM
601 if ( !store
&& !store
.eof() )
603 int res
= LoadObject(store
).GetLastError();
604 if ((res
!= wxSTREAM_NO_ERROR
) &&
605 (res
!= wxSTREAM_EOF
))
608 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
|wxICON_EXCLAMATION
,
609 GetDocumentWindow());
617 // ----------------------------------------------------------------------------
619 // ----------------------------------------------------------------------------
623 m_viewDocument
= (wxDocument
*) NULL
;
625 m_viewFrame
= (wxFrame
*) NULL
;
630 GetDocumentManager()->ActivateView(this, false);
631 m_viewDocument
->RemoveView(this);
634 // Extend event processing to search the document's event table
635 bool wxView::ProcessEvent(wxEvent
& event
)
637 if ( !GetDocument() || !GetDocument()->ProcessEvent(event
) )
638 return wxEvtHandler::ProcessEvent(event
);
643 void wxView::OnActivateView(bool WXUNUSED(activate
), wxView
*WXUNUSED(activeView
), wxView
*WXUNUSED(deactiveView
))
647 void wxView::OnPrint(wxDC
*dc
, wxObject
*WXUNUSED(info
))
652 void wxView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
))
656 void wxView::OnChangeFilename()
658 if (GetFrame() && GetDocument())
662 GetDocument()->GetPrintableName(title
);
664 GetFrame()->SetTitle(title
);
668 void wxView::SetDocument(wxDocument
*doc
)
670 m_viewDocument
= doc
;
675 bool wxView::Close(bool deleteWindow
)
677 if (OnClose(deleteWindow
))
683 void wxView::Activate(bool activate
)
685 if (GetDocument() && GetDocumentManager())
687 OnActivateView(activate
, this, GetDocumentManager()->GetCurrentView());
688 GetDocumentManager()->ActivateView(this, activate
);
692 bool wxView::OnClose(bool WXUNUSED(deleteWindow
))
694 return GetDocument() ? GetDocument()->Close() : true;
697 #if wxUSE_PRINTING_ARCHITECTURE
698 wxPrintout
*wxView::OnCreatePrintout()
700 return new wxDocPrintout(this);
702 #endif // wxUSE_PRINTING_ARCHITECTURE
704 // ----------------------------------------------------------------------------
706 // ----------------------------------------------------------------------------
708 wxDocTemplate::wxDocTemplate(wxDocManager
*manager
,
709 const wxString
& descr
,
710 const wxString
& filter
,
713 const wxString
& docTypeName
,
714 const wxString
& viewTypeName
,
715 wxClassInfo
*docClassInfo
,
716 wxClassInfo
*viewClassInfo
,
719 m_documentManager
= manager
;
720 m_description
= descr
;
723 m_fileFilter
= filter
;
725 m_docTypeName
= docTypeName
;
726 m_viewTypeName
= viewTypeName
;
727 m_documentManager
->AssociateTemplate(this);
729 m_docClassInfo
= docClassInfo
;
730 m_viewClassInfo
= viewClassInfo
;
733 wxDocTemplate::~wxDocTemplate()
735 m_documentManager
->DisassociateTemplate(this);
738 // Tries to dynamically construct an object of the right class.
739 wxDocument
*wxDocTemplate::CreateDocument(const wxString
& path
, long flags
)
741 wxDocument
*doc
= DoCreateDocument();
743 return (wxDocument
*) NULL
;
745 if (InitDocument(doc
, path
, flags
))
751 return (wxDocument
*) NULL
;
755 bool wxDocTemplate::InitDocument(wxDocument
* doc
, const wxString
& path
, long flags
)
757 doc
->SetFilename(path
);
758 doc
->SetDocumentTemplate(this);
759 GetDocumentManager()->AddDocument(doc
);
760 doc
->SetCommandProcessor(doc
->OnCreateCommandProcessor());
762 if (doc
->OnCreate(path
, flags
))
766 if (GetDocumentManager()->GetDocuments().Member(doc
))
767 doc
->DeleteAllViews();
772 wxView
*wxDocTemplate::CreateView(wxDocument
*doc
, long flags
)
774 wxView
*view
= DoCreateView();
776 return (wxView
*) NULL
;
778 view
->SetDocument(doc
);
779 if (view
->OnCreate(doc
, flags
))
786 return (wxView
*) NULL
;
790 // The default (very primitive) format detection: check is the extension is
791 // that of the template
792 bool wxDocTemplate::FileMatchesTemplate(const wxString
& path
)
794 return GetDefaultExtension().IsSameAs(FindExtension(path
));
797 wxDocument
*wxDocTemplate::DoCreateDocument()
800 return (wxDocument
*) NULL
;
802 return (wxDocument
*)m_docClassInfo
->CreateObject();
805 wxView
*wxDocTemplate::DoCreateView()
807 if (!m_viewClassInfo
)
808 return (wxView
*) NULL
;
810 return (wxView
*)m_viewClassInfo
->CreateObject();
813 // ----------------------------------------------------------------------------
815 // ----------------------------------------------------------------------------
817 BEGIN_EVENT_TABLE(wxDocManager
, wxEvtHandler
)
818 EVT_MENU(wxID_OPEN
, wxDocManager::OnFileOpen
)
819 EVT_MENU(wxID_CLOSE
, wxDocManager::OnFileClose
)
820 EVT_MENU(wxID_CLOSE_ALL
, wxDocManager::OnFileCloseAll
)
821 EVT_MENU(wxID_REVERT
, wxDocManager::OnFileRevert
)
822 EVT_MENU(wxID_NEW
, wxDocManager::OnFileNew
)
823 EVT_MENU(wxID_SAVE
, wxDocManager::OnFileSave
)
824 EVT_MENU(wxID_SAVEAS
, wxDocManager::OnFileSaveAs
)
825 EVT_MENU(wxID_UNDO
, wxDocManager::OnUndo
)
826 EVT_MENU(wxID_REDO
, wxDocManager::OnRedo
)
828 EVT_UPDATE_UI(wxID_OPEN
, wxDocManager::OnUpdateFileOpen
)
829 EVT_UPDATE_UI(wxID_CLOSE
, wxDocManager::OnUpdateFileClose
)
830 EVT_UPDATE_UI(wxID_CLOSE_ALL
, wxDocManager::OnUpdateFileClose
)
831 EVT_UPDATE_UI(wxID_REVERT
, wxDocManager::OnUpdateFileRevert
)
832 EVT_UPDATE_UI(wxID_NEW
, wxDocManager::OnUpdateFileNew
)
833 EVT_UPDATE_UI(wxID_SAVE
, wxDocManager::OnUpdateFileSave
)
834 EVT_UPDATE_UI(wxID_SAVEAS
, wxDocManager::OnUpdateFileSaveAs
)
835 EVT_UPDATE_UI(wxID_UNDO
, wxDocManager::OnUpdateUndo
)
836 EVT_UPDATE_UI(wxID_REDO
, wxDocManager::OnUpdateRedo
)
838 #if wxUSE_PRINTING_ARCHITECTURE
839 EVT_MENU(wxID_PRINT
, wxDocManager::OnPrint
)
840 EVT_MENU(wxID_PREVIEW
, wxDocManager::OnPreview
)
842 EVT_UPDATE_UI(wxID_PRINT
, wxDocManager::OnUpdatePrint
)
843 EVT_UPDATE_UI(wxID_PREVIEW
, wxDocManager::OnUpdatePreview
)
847 wxDocManager
* wxDocManager::sm_docManager
= (wxDocManager
*) NULL
;
849 wxDocManager::wxDocManager(long flags
, bool initialize
)
851 m_defaultDocumentNameCounter
= 1;
853 m_currentView
= (wxView
*) NULL
;
854 m_maxDocsOpen
= 10000;
855 m_fileHistory
= (wxFileHistory
*) NULL
;
858 sm_docManager
= this;
861 wxDocManager::~wxDocManager()
865 delete m_fileHistory
;
866 sm_docManager
= (wxDocManager
*) NULL
;
869 // closes the specified document
870 bool wxDocManager::CloseDocument(wxDocument
* doc
, bool force
)
872 if (doc
->Close() || force
)
874 // Implicitly deletes the document when
875 // the last view is deleted
876 doc
->DeleteAllViews();
878 // Check we're really deleted
879 if (m_docs
.Member(doc
))
887 bool wxDocManager::CloseDocuments(bool force
)
889 wxList::compatibility_iterator node
= m_docs
.GetFirst();
892 wxDocument
*doc
= (wxDocument
*)node
->GetData();
893 wxList::compatibility_iterator next
= node
->GetNext();
895 if (!CloseDocument(doc
, force
))
898 // This assumes that documents are not connected in
899 // any way, i.e. deleting one document does NOT
906 bool wxDocManager::Clear(bool force
)
908 if (!CloseDocuments(force
))
911 wxList::compatibility_iterator node
= m_templates
.GetFirst();
914 wxDocTemplate
*templ
= (wxDocTemplate
*) node
->GetData();
915 wxList::compatibility_iterator next
= node
->GetNext();
922 bool wxDocManager::Initialize()
924 m_fileHistory
= OnCreateFileHistory();
928 wxFileHistory
*wxDocManager::OnCreateFileHistory()
930 return new wxFileHistory
;
933 void wxDocManager::OnFileClose(wxCommandEvent
& WXUNUSED(event
))
935 wxDocument
*doc
= GetCurrentDocument();
940 doc
->DeleteAllViews();
941 if (m_docs
.Member(doc
))
946 void wxDocManager::OnFileCloseAll(wxCommandEvent
& WXUNUSED(event
))
948 CloseDocuments(false);
951 void wxDocManager::OnFileNew(wxCommandEvent
& WXUNUSED(event
))
953 CreateDocument( wxT(""), wxDOC_NEW
);
956 void wxDocManager::OnFileOpen(wxCommandEvent
& WXUNUSED(event
))
958 if ( !CreateDocument( wxT(""), 0) )
964 void wxDocManager::OnFileRevert(wxCommandEvent
& WXUNUSED(event
))
966 wxDocument
*doc
= GetCurrentDocument();
972 void wxDocManager::OnFileSave(wxCommandEvent
& WXUNUSED(event
))
974 wxDocument
*doc
= GetCurrentDocument();
980 void wxDocManager::OnFileSaveAs(wxCommandEvent
& WXUNUSED(event
))
982 wxDocument
*doc
= GetCurrentDocument();
988 void wxDocManager::OnPrint(wxCommandEvent
& WXUNUSED(event
))
990 #if wxUSE_PRINTING_ARCHITECTURE
991 wxView
*view
= GetCurrentView();
995 wxPrintout
*printout
= view
->OnCreatePrintout();
999 printer
.Print(view
->GetFrame(), printout
, true);
1003 #endif // wxUSE_PRINTING_ARCHITECTURE
1006 void wxDocManager::OnPreview(wxCommandEvent
& WXUNUSED(event
))
1008 #if wxUSE_PRINTING_ARCHITECTURE
1009 wxView
*view
= GetCurrentView();
1013 wxPrintout
*printout
= view
->OnCreatePrintout();
1016 // Pass two printout objects: for preview, and possible printing.
1017 wxPrintPreviewBase
*preview
= new wxPrintPreview(printout
, view
->OnCreatePrintout());
1018 if ( !preview
->Ok() )
1021 wxMessageBox( _("Sorry, print preview needs a printer to be installed.") );
1025 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, (wxFrame
*)wxTheApp
->GetTopWindow(), _("Print Preview"),
1026 wxPoint(100, 100), wxSize(600, 650));
1027 frame
->Centre(wxBOTH
);
1028 frame
->Initialize();
1031 #endif // wxUSE_PRINTING_ARCHITECTURE
1034 void wxDocManager::OnUndo(wxCommandEvent
& event
)
1036 wxDocument
*doc
= GetCurrentDocument();
1039 if (doc
->GetCommandProcessor())
1040 doc
->GetCommandProcessor()->Undo();
1045 void wxDocManager::OnRedo(wxCommandEvent
& event
)
1047 wxDocument
*doc
= GetCurrentDocument();
1050 if (doc
->GetCommandProcessor())
1051 doc
->GetCommandProcessor()->Redo();
1056 // Handlers for UI update commands
1058 void wxDocManager::OnUpdateFileOpen(wxUpdateUIEvent
& event
)
1060 event
.Enable( true );
1063 void wxDocManager::OnUpdateFileClose(wxUpdateUIEvent
& event
)
1065 wxDocument
*doc
= GetCurrentDocument();
1066 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1069 void wxDocManager::OnUpdateFileRevert(wxUpdateUIEvent
& event
)
1071 wxDocument
*doc
= GetCurrentDocument();
1072 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1075 void wxDocManager::OnUpdateFileNew(wxUpdateUIEvent
& event
)
1077 event
.Enable( true );
1080 void wxDocManager::OnUpdateFileSave(wxUpdateUIEvent
& event
)
1082 wxDocument
*doc
= GetCurrentDocument();
1083 event
.Enable( doc
&& doc
->IsModified() );
1086 void wxDocManager::OnUpdateFileSaveAs(wxUpdateUIEvent
& event
)
1088 wxDocument
*doc
= GetCurrentDocument();
1089 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1092 void wxDocManager::OnUpdateUndo(wxUpdateUIEvent
& event
)
1094 wxDocument
*doc
= GetCurrentDocument();
1096 event
.Enable(false);
1097 else if (!doc
->GetCommandProcessor())
1101 event
.Enable( doc
->GetCommandProcessor()->CanUndo() );
1102 doc
->GetCommandProcessor()->SetMenuStrings();
1106 void wxDocManager::OnUpdateRedo(wxUpdateUIEvent
& event
)
1108 wxDocument
*doc
= GetCurrentDocument();
1110 event
.Enable(false);
1111 else if (!doc
->GetCommandProcessor())
1115 event
.Enable( doc
->GetCommandProcessor()->CanRedo() );
1116 doc
->GetCommandProcessor()->SetMenuStrings();
1120 void wxDocManager::OnUpdatePrint(wxUpdateUIEvent
& event
)
1122 wxDocument
*doc
= GetCurrentDocument();
1123 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1126 void wxDocManager::OnUpdatePreview(wxUpdateUIEvent
& event
)
1128 wxDocument
*doc
= GetCurrentDocument();
1129 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1132 wxView
*wxDocManager::GetCurrentView() const
1135 return m_currentView
;
1136 if (m_docs
.GetCount() == 1)
1138 wxDocument
* doc
= (wxDocument
*) m_docs
.GetFirst()->GetData();
1139 return doc
->GetFirstView();
1141 return (wxView
*) NULL
;
1144 // Extend event processing to search the view's event table
1145 bool wxDocManager::ProcessEvent(wxEvent
& event
)
1147 wxView
* view
= GetCurrentView();
1150 if (view
->ProcessEvent(event
))
1153 return wxEvtHandler::ProcessEvent(event
);
1156 wxDocument
*wxDocManager::CreateDocument(const wxString
& path
, long flags
)
1158 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.GetCount()];
1161 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1163 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Item(i
)->GetData());
1164 if (temp
->IsVisible())
1166 templates
[n
] = temp
;
1173 return (wxDocument
*) NULL
;
1176 wxDocument
* docToClose
= NULL
;
1178 // If we've reached the max number of docs, close the
1180 if ( (int)GetDocuments().GetCount() >= m_maxDocsOpen
)
1182 wxDocument
*doc
= (wxDocument
*)GetDocuments().GetFirst()->GetData();
1186 // New document: user chooses a template, unless there's only one.
1187 if (flags
& wxDOC_NEW
)
1193 if (!CloseDocument(docToClose
, false))
1200 wxDocTemplate
*temp
= templates
[0];
1202 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
1206 newDoc
->SetDocumentName(temp
->GetDocumentName());
1207 newDoc
->SetDocumentTemplate(temp
);
1208 newDoc
->OnNewDocument();
1213 wxDocTemplate
*temp
= SelectDocumentType(templates
, n
);
1219 if (!CloseDocument(docToClose
, false))
1225 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
1229 newDoc
->SetDocumentName(temp
->GetDocumentName());
1230 newDoc
->SetDocumentTemplate(temp
);
1231 newDoc
->OnNewDocument();
1236 return (wxDocument
*) NULL
;
1239 // Existing document
1240 wxDocTemplate
*temp
;
1242 wxString
path2(wxT(""));
1243 if (path
!= wxT(""))
1246 if (flags
& wxDOC_SILENT
)
1248 temp
= FindTemplateForPath(path2
);
1251 // Since we do not add files with non-default extensions to the FileHistory this
1252 // can only happen if the application changes the allowed templates in runtime.
1253 (void)wxMessageBox(_("Sorry, the format for this file is unknown."),
1255 wxOK
| wxICON_EXCLAMATION
, wxFindSuitableParent());
1259 temp
= SelectDocumentPath(templates
, n
, path2
, flags
);
1267 if (!CloseDocument(docToClose
, false))
1273 wxDocument
*newDoc
= temp
->CreateDocument(path2
, flags
);
1276 newDoc
->SetDocumentName(temp
->GetDocumentName());
1277 newDoc
->SetDocumentTemplate(temp
);
1278 if (!newDoc
->OnOpenDocument(path2
))
1280 newDoc
->DeleteAllViews();
1281 // delete newDoc; // Implicitly deleted by DeleteAllViews
1282 return (wxDocument
*) NULL
;
1284 // A file that doesn't use the default extension of its document
1285 // template cannot be opened via the FileHistory, so we do not
1287 if (temp
->FileMatchesTemplate(path2
))
1288 AddFileToHistory(path2
);
1293 return (wxDocument
*) NULL
;
1296 wxView
*wxDocManager::CreateView(wxDocument
*doc
, long flags
)
1298 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.GetCount()];
1301 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1303 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Item(i
)->GetData());
1304 if (temp
->IsVisible())
1306 if (temp
->GetDocumentName() == doc
->GetDocumentName())
1308 templates
[n
] = temp
;
1316 return (wxView
*) NULL
;
1320 wxDocTemplate
*temp
= templates
[0];
1322 wxView
*view
= temp
->CreateView(doc
, flags
);
1324 view
->SetViewName(temp
->GetViewName());
1328 wxDocTemplate
*temp
= SelectViewType(templates
, n
);
1332 wxView
*view
= temp
->CreateView(doc
, flags
);
1334 view
->SetViewName(temp
->GetViewName());
1338 return (wxView
*) NULL
;
1341 // Not yet implemented
1342 void wxDocManager::DeleteTemplate(wxDocTemplate
*WXUNUSED(temp
), long WXUNUSED(flags
))
1346 // Not yet implemented
1347 bool wxDocManager::FlushDoc(wxDocument
*WXUNUSED(doc
))
1352 wxDocument
*wxDocManager::GetCurrentDocument() const
1354 wxView
*view
= GetCurrentView();
1356 return view
->GetDocument();
1358 return (wxDocument
*) NULL
;
1361 // Make a default document name
1362 bool wxDocManager::MakeDefaultName(wxString
& name
)
1364 name
.Printf(_("unnamed%d"), m_defaultDocumentNameCounter
);
1365 m_defaultDocumentNameCounter
++;
1370 // Make a frame title (override this to do something different)
1371 // If docName is empty, a document is not currently active.
1372 wxString
wxDocManager::MakeFrameTitle(wxDocument
* doc
)
1374 wxString appName
= wxTheApp
->GetAppName();
1381 doc
->GetPrintableName(docName
);
1382 title
= docName
+ wxString(_(" - ")) + appName
;
1388 // Not yet implemented
1389 wxDocTemplate
*wxDocManager::MatchTemplate(const wxString
& WXUNUSED(path
))
1391 return (wxDocTemplate
*) NULL
;
1394 // File history management
1395 void wxDocManager::AddFileToHistory(const wxString
& file
)
1398 m_fileHistory
->AddFileToHistory(file
);
1401 void wxDocManager::RemoveFileFromHistory(size_t i
)
1404 m_fileHistory
->RemoveFileFromHistory(i
);
1407 wxString
wxDocManager::GetHistoryFile(size_t i
) const
1412 histFile
= m_fileHistory
->GetHistoryFile(i
);
1417 void wxDocManager::FileHistoryUseMenu(wxMenu
*menu
)
1420 m_fileHistory
->UseMenu(menu
);
1423 void wxDocManager::FileHistoryRemoveMenu(wxMenu
*menu
)
1426 m_fileHistory
->RemoveMenu(menu
);
1430 void wxDocManager::FileHistoryLoad(wxConfigBase
& config
)
1433 m_fileHistory
->Load(config
);
1436 void wxDocManager::FileHistorySave(wxConfigBase
& config
)
1439 m_fileHistory
->Save(config
);
1443 void wxDocManager::FileHistoryAddFilesToMenu(wxMenu
* menu
)
1446 m_fileHistory
->AddFilesToMenu(menu
);
1449 void wxDocManager::FileHistoryAddFilesToMenu()
1452 m_fileHistory
->AddFilesToMenu();
1455 size_t wxDocManager::GetHistoryFilesCount() const
1457 return m_fileHistory
? m_fileHistory
->GetCount() : 0;
1461 // Find out the document template via matching in the document file format
1462 // against that of the template
1463 wxDocTemplate
*wxDocManager::FindTemplateForPath(const wxString
& path
)
1465 wxDocTemplate
*theTemplate
= (wxDocTemplate
*) NULL
;
1467 // Find the template which this extension corresponds to
1468 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1470 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Item(i
)->GetData();
1471 if ( temp
->FileMatchesTemplate(path
) )
1480 // Try to get a more suitable parent frame than the top window,
1481 // for selection dialogs. Otherwise you may get an unexpected
1482 // window being activated when a dialog is shown.
1483 static wxWindow
* wxFindSuitableParent()
1485 wxWindow
* parent
= wxTheApp
->GetTopWindow();
1487 wxWindow
* focusWindow
= wxWindow::FindFocus();
1490 while (focusWindow
&&
1491 !focusWindow
->IsKindOf(CLASSINFO(wxDialog
)) &&
1492 !focusWindow
->IsKindOf(CLASSINFO(wxFrame
)))
1494 focusWindow
= focusWindow
->GetParent();
1497 parent
= focusWindow
;
1502 // Prompts user to open a file, using file specs in templates.
1503 // Must extend the file selector dialog or implement own; OR
1504 // match the extension to the template extension.
1506 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**templates
,
1507 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
1510 int WXUNUSED(noTemplates
),
1513 long WXUNUSED(flags
),
1514 bool WXUNUSED(save
))
1516 // We can only have multiple filters in Windows and GTK
1517 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
1521 for (i
= 0; i
< noTemplates
; i
++)
1523 if (templates
[i
]->IsVisible())
1525 // add a '|' to separate this filter from the previous one
1526 if ( !descrBuf
.IsEmpty() )
1527 descrBuf
<< wxT('|');
1529 descrBuf
<< templates
[i
]->GetDescription()
1530 << wxT(" (") << templates
[i
]->GetFileFilter() << wxT(") |")
1531 << templates
[i
]->GetFileFilter();
1535 wxString descrBuf
= wxT("*.*");
1538 int FilterIndex
= -1;
1540 wxWindow
* parent
= wxFindSuitableParent();
1542 wxString pathTmp
= wxFileSelectorEx(_("Select a file"),
1550 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)NULL
;
1551 if (!pathTmp
.IsEmpty())
1553 if (!wxFileExists(pathTmp
))
1556 if (!wxTheApp
->GetAppName().IsEmpty())
1557 msgTitle
= wxTheApp
->GetAppName();
1559 msgTitle
= wxString(_("File error"));
1561 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
1565 return (wxDocTemplate
*) NULL
;
1567 m_lastDirectory
= wxPathOnly(pathTmp
);
1571 // first choose the template using the extension, if this fails (i.e.
1572 // wxFileSelectorEx() didn't fill it), then use the path
1573 if ( FilterIndex
!= -1 )
1574 theTemplate
= templates
[FilterIndex
];
1576 theTemplate
= FindTemplateForPath(path
);
1579 // Since we do not add files with non-default extensions to the FileHistory this
1580 // can only happen if the application changes the allowed templates in runtime.
1581 (void)wxMessageBox(_("Sorry, the format for this file is unknown."),
1583 wxOK
| wxICON_EXCLAMATION
, wxFindSuitableParent());
1594 wxDocTemplate
*wxDocManager::SelectDocumentType(wxDocTemplate
**templates
,
1595 int noTemplates
, bool sort
)
1597 wxArrayString strings
;
1598 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1602 for (i
= 0; i
< noTemplates
; i
++)
1604 if (templates
[i
]->IsVisible())
1608 for (j
= 0; j
< n
; j
++)
1610 //filter out NOT unique documents + view combinations
1611 if ( templates
[i
]->m_docTypeName
== data
[j
]->m_docTypeName
&&
1612 templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
1619 strings
.Add(templates
[i
]->m_description
);
1621 data
[n
] = templates
[i
];
1629 strings
.Sort(); // ascending sort
1630 // Yes, this will be slow, but template lists
1631 // are typically short.
1633 n
= strings
.Count();
1634 for (i
= 0; i
< n
; i
++)
1636 for (j
= 0; j
< noTemplates
; j
++)
1638 if (strings
[i
] == templates
[j
]->m_description
)
1639 data
[i
] = templates
[j
];
1644 wxDocTemplate
*theTemplate
;
1649 // no visible templates, hence nothing to choose from
1654 // don't propose the user to choose if he heas no choice
1655 theTemplate
= data
[0];
1659 // propose the user to choose one of several
1660 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1662 _("Select a document template"),
1666 wxFindSuitableParent()
1675 wxDocTemplate
*wxDocManager::SelectViewType(wxDocTemplate
**templates
,
1676 int noTemplates
, bool sort
)
1678 wxArrayString strings
;
1679 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1683 for (i
= 0; i
< noTemplates
; i
++)
1685 wxDocTemplate
*templ
= templates
[i
];
1686 if ( templ
->IsVisible() && !templ
->GetViewName().empty() )
1690 for (j
= 0; j
< n
; j
++)
1692 //filter out NOT unique views
1693 if ( templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
)
1699 strings
.Add(templ
->m_viewTypeName
);
1708 strings
.Sort(); // ascending sort
1709 // Yes, this will be slow, but template lists
1710 // are typically short.
1712 n
= strings
.Count();
1713 for (i
= 0; i
< n
; i
++)
1715 for (j
= 0; j
< noTemplates
; j
++)
1717 if (strings
[i
] == templates
[j
]->m_viewTypeName
)
1718 data
[i
] = templates
[j
];
1723 wxDocTemplate
*theTemplate
;
1725 // the same logic as above
1729 theTemplate
= (wxDocTemplate
*)NULL
;
1733 theTemplate
= data
[0];
1737 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1739 _("Select a document view"),
1743 wxFindSuitableParent()
1752 void wxDocManager::AssociateTemplate(wxDocTemplate
*temp
)
1754 if (!m_templates
.Member(temp
))
1755 m_templates
.Append(temp
);
1758 void wxDocManager::DisassociateTemplate(wxDocTemplate
*temp
)
1760 m_templates
.DeleteObject(temp
);
1763 // Add and remove a document from the manager's list
1764 void wxDocManager::AddDocument(wxDocument
*doc
)
1766 if (!m_docs
.Member(doc
))
1770 void wxDocManager::RemoveDocument(wxDocument
*doc
)
1772 m_docs
.DeleteObject(doc
);
1775 // Views or windows should inform the document manager
1776 // when a view is going in or out of focus
1777 void wxDocManager::ActivateView(wxView
*view
, bool activate
)
1781 m_currentView
= view
;
1785 if ( m_currentView
== view
)
1787 // don't keep stale pointer
1788 m_currentView
= (wxView
*) NULL
;
1793 // ----------------------------------------------------------------------------
1794 // Default document child frame
1795 // ----------------------------------------------------------------------------
1797 BEGIN_EVENT_TABLE(wxDocChildFrame
, wxFrame
)
1798 EVT_ACTIVATE(wxDocChildFrame::OnActivate
)
1799 EVT_CLOSE(wxDocChildFrame::OnCloseWindow
)
1802 wxDocChildFrame::wxDocChildFrame(wxDocument
*doc
,
1806 const wxString
& title
,
1810 const wxString
& name
)
1811 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1813 m_childDocument
= doc
;
1816 view
->SetFrame(this);
1819 // Extend event processing to search the view's event table
1820 bool wxDocChildFrame::ProcessEvent(wxEvent
& event
)
1823 m_childView
->Activate(true);
1825 if ( !m_childView
|| ! m_childView
->ProcessEvent(event
) )
1827 // Only hand up to the parent if it's a menu command
1828 if (!event
.IsKindOf(CLASSINFO(wxCommandEvent
)) || !GetParent() || !GetParent()->ProcessEvent(event
))
1829 return wxEvtHandler::ProcessEvent(event
);
1837 void wxDocChildFrame::OnActivate(wxActivateEvent
& event
)
1839 wxFrame::OnActivate(event
);
1842 m_childView
->Activate(event
.GetActive());
1845 void wxDocChildFrame::OnCloseWindow(wxCloseEvent
& event
)
1849 bool ans
= event
.CanVeto()
1850 ? m_childView
->Close(false) // false means don't delete associated window
1851 : true; // Must delete.
1855 m_childView
->Activate(false);
1857 m_childView
= (wxView
*) NULL
;
1858 m_childDocument
= (wxDocument
*) NULL
;
1869 // ----------------------------------------------------------------------------
1870 // Default parent frame
1871 // ----------------------------------------------------------------------------
1873 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1874 EVT_MENU(wxID_EXIT
, wxDocParentFrame::OnExit
)
1875 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE9
, wxDocParentFrame::OnMRUFile
)
1876 EVT_CLOSE(wxDocParentFrame::OnCloseWindow
)
1879 wxDocParentFrame::wxDocParentFrame(wxDocManager
*manager
,
1882 const wxString
& title
,
1886 const wxString
& name
)
1887 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1889 m_docManager
= manager
;
1892 void wxDocParentFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
1897 void wxDocParentFrame::OnMRUFile(wxCommandEvent
& event
)
1899 int n
= event
.GetId() - wxID_FILE1
; // the index in MRU list
1900 wxString
filename(m_docManager
->GetHistoryFile(n
));
1901 if ( !filename
.IsEmpty() )
1903 // verify that the file exists before doing anything else
1904 if ( wxFile::Exists(filename
) )
1907 if (!m_docManager
->CreateDocument(filename
, wxDOC_SILENT
))
1909 // remove the file from the MRU list. The user should already be notified.
1910 m_docManager
->RemoveFileFromHistory(n
);
1912 wxLogError(_("The file '%s' couldn't be opened.\nIt has been removed from the most recently used files list."),
1918 // remove the bogus filename from the MRU list and notify the user
1920 m_docManager
->RemoveFileFromHistory(n
);
1922 wxLogError(_("The file '%s' doesn't exist and couldn't be opened.\nIt has been removed from the most recently used files list."),
1928 // Extend event processing to search the view's event table
1929 bool wxDocParentFrame::ProcessEvent(wxEvent
& event
)
1931 // Try the document manager, then do default processing
1932 if (!m_docManager
|| !m_docManager
->ProcessEvent(event
))
1933 return wxEvtHandler::ProcessEvent(event
);
1938 // Define the behaviour for the frame closing
1939 // - must delete all frames except for the main one.
1940 void wxDocParentFrame::OnCloseWindow(wxCloseEvent
& event
)
1942 if (m_docManager
->Clear(!event
.CanVeto()))
1950 #if wxUSE_PRINTING_ARCHITECTURE
1952 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
)
1955 m_printoutView
= view
;
1958 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
1962 // Get the logical pixels per inch of screen and printer
1963 int ppiScreenX
, ppiScreenY
;
1964 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
1965 wxUnusedVar(ppiScreenY
);
1966 int ppiPrinterX
, ppiPrinterY
;
1967 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
1968 wxUnusedVar(ppiPrinterY
);
1970 // This scales the DC so that the printout roughly represents the
1971 // the screen scaling. The text point size _should_ be the right size
1972 // but in fact is too small for some reason. This is a detail that will
1973 // need to be addressed at some point but can be fudged for the
1975 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
1977 // Now we have to check in case our real page size is reduced
1978 // (e.g. because we're drawing to a print preview memory DC)
1979 int pageWidth
, pageHeight
;
1981 dc
->GetSize(&w
, &h
);
1982 GetPageSizePixels(&pageWidth
, &pageHeight
);
1983 wxUnusedVar(pageHeight
);
1985 // If printer pageWidth == current DC width, then this doesn't
1986 // change. But w might be the preview bitmap width, so scale down.
1987 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
1988 dc
->SetUserScale(overallScale
, overallScale
);
1992 m_printoutView
->OnDraw(dc
);
1997 bool wxDocPrintout::HasPage(int pageNum
)
1999 return (pageNum
== 1);
2002 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
2004 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
2010 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
2018 #endif // wxUSE_PRINTING_ARCHITECTURE
2020 // ----------------------------------------------------------------------------
2021 // File history processor
2022 // ----------------------------------------------------------------------------
2024 static inline wxChar
* MYcopystring(const wxString
& s
)
2026 wxChar
* copy
= new wxChar
[s
.length() + 1];
2027 return wxStrcpy(copy
, s
.c_str());
2030 static inline wxChar
* MYcopystring(const wxChar
* s
)
2032 wxChar
* copy
= new wxChar
[wxStrlen(s
) + 1];
2033 return wxStrcpy(copy
, s
);
2036 wxFileHistory::wxFileHistory(size_t maxFiles
, wxWindowID idBase
)
2038 m_fileMaxFiles
= maxFiles
;
2041 m_fileHistory
= new wxChar
*[m_fileMaxFiles
];
2044 wxFileHistory::~wxFileHistory()
2047 for (i
= 0; i
< m_fileHistoryN
; i
++)
2048 delete[] m_fileHistory
[i
];
2049 delete[] m_fileHistory
;
2052 // File history management
2053 void wxFileHistory::AddFileToHistory(const wxString
& file
)
2057 // Check we don't already have this file
2058 for (i
= 0; i
< m_fileHistoryN
; i
++)
2060 #if defined( __WXMSW__ ) // Add any other OSes with case insensitive file names
2061 wxString testString
;
2062 if ( m_fileHistory
[i
] )
2063 testString
= m_fileHistory
[i
];
2064 if ( m_fileHistory
[i
] && ( file
.Lower() == testString
.Lower() ) )
2066 if ( m_fileHistory
[i
] && ( file
== m_fileHistory
[i
] ) )
2069 // we do have it, move it to the top of the history
2070 RemoveFileFromHistory (i
);
2071 AddFileToHistory (file
);
2076 // if we already have a full history, delete the one at the end
2077 if ( m_fileMaxFiles
== m_fileHistoryN
)
2079 RemoveFileFromHistory (m_fileHistoryN
- 1);
2080 AddFileToHistory (file
);
2084 // Add to the project file history:
2085 // Move existing files (if any) down so we can insert file at beginning.
2086 if (m_fileHistoryN
< m_fileMaxFiles
)
2088 wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2091 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2092 if ( m_fileHistoryN
== 0 && menu
->GetMenuItemCount() )
2094 menu
->AppendSeparator();
2096 menu
->Append(m_idBase
+m_fileHistoryN
, _("[EMPTY]"));
2097 node
= node
->GetNext();
2101 // Shuffle filenames down
2102 for (i
= (m_fileHistoryN
-1); i
> 0; i
--)
2104 m_fileHistory
[i
] = m_fileHistory
[i
-1];
2106 m_fileHistory
[0] = MYcopystring(file
);
2108 // this is the directory of the last opened file
2109 wxString pathCurrent
;
2110 wxSplitPath( m_fileHistory
[0], &pathCurrent
, NULL
, NULL
);
2111 for (i
= 0; i
< m_fileHistoryN
; i
++)
2113 if ( m_fileHistory
[i
] )
2115 // if in same directory just show the filename; otherwise the full
2117 wxString pathInMenu
, path
, filename
, ext
;
2118 wxSplitPath( m_fileHistory
[i
], &path
, &filename
, &ext
);
2119 if ( path
== pathCurrent
)
2121 pathInMenu
= filename
;
2123 pathInMenu
= pathInMenu
+ wxFILE_SEP_EXT
+ ext
;
2127 // absolute path; could also set relative path
2128 pathInMenu
= m_fileHistory
[i
];
2132 buf
.Printf(s_MRUEntryFormat
, i
+ 1, pathInMenu
.c_str());
2133 wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2136 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2137 menu
->SetLabel(m_idBase
+ i
, buf
);
2138 node
= node
->GetNext();
2144 void wxFileHistory::RemoveFileFromHistory(size_t i
)
2146 wxCHECK_RET( i
< m_fileHistoryN
,
2147 wxT("invalid index in wxFileHistory::RemoveFileFromHistory") );
2149 // delete the element from the array (could use memmove() too...)
2150 delete [] m_fileHistory
[i
];
2153 for ( j
= i
; j
< m_fileHistoryN
- 1; j
++ )
2155 m_fileHistory
[j
] = m_fileHistory
[j
+ 1];
2158 wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2161 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2163 // shuffle filenames up
2165 for ( j
= i
; j
< m_fileHistoryN
- 1; j
++ )
2167 buf
.Printf(s_MRUEntryFormat
, j
+ 1, m_fileHistory
[j
]);
2168 menu
->SetLabel(m_idBase
+ j
, buf
);
2171 node
= node
->GetNext();
2173 // delete the last menu item which is unused now
2174 wxWindowID lastItemId
= m_idBase
+ m_fileHistoryN
- 1;
2175 if (menu
->FindItem(lastItemId
))
2177 menu
->Delete(lastItemId
);
2180 // delete the last separator too if no more files are left
2181 if ( m_fileHistoryN
== 1 )
2183 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetLast();
2186 wxMenuItem
*menuItem
= node
->GetData();
2187 if ( menuItem
->IsSeparator() )
2189 menu
->Delete(menuItem
);
2191 //else: should we search backwards for the last separator?
2193 //else: menu is empty somehow
2200 wxString
wxFileHistory::GetHistoryFile(size_t i
) const
2203 if ( i
< m_fileHistoryN
)
2205 s
= m_fileHistory
[i
];
2209 wxFAIL_MSG( wxT("bad index in wxFileHistory::GetHistoryFile") );
2215 void wxFileHistory::UseMenu(wxMenu
*menu
)
2217 if (!m_fileMenus
.Member(menu
))
2218 m_fileMenus
.Append(menu
);
2221 void wxFileHistory::RemoveMenu(wxMenu
*menu
)
2223 m_fileMenus
.DeleteObject(menu
);
2227 void wxFileHistory::Load(wxConfigBase
& config
)
2231 buf
.Printf(wxT("file%d"), (int)m_fileHistoryN
+1);
2232 wxString historyFile
;
2233 while ((m_fileHistoryN
< m_fileMaxFiles
) && config
.Read(buf
, &historyFile
) && (historyFile
!= wxT("")))
2235 m_fileHistory
[m_fileHistoryN
] = MYcopystring((const wxChar
*) historyFile
);
2237 buf
.Printf(wxT("file%d"), (int)m_fileHistoryN
+1);
2238 historyFile
= wxT("");
2243 void wxFileHistory::Save(wxConfigBase
& config
)
2246 for (i
= 0; i
< m_fileMaxFiles
; i
++)
2249 buf
.Printf(wxT("file%d"), (int)i
+1);
2250 if (i
< m_fileHistoryN
)
2251 config
.Write(buf
, wxString(m_fileHistory
[i
]));
2253 config
.Write(buf
, wxEmptyString
);
2256 #endif // wxUSE_CONFIG
2258 void wxFileHistory::AddFilesToMenu()
2260 if (m_fileHistoryN
> 0)
2262 wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2265 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2266 if (menu
->GetMenuItemCount())
2268 menu
->AppendSeparator();
2272 for (i
= 0; i
< m_fileHistoryN
; i
++)
2274 if (m_fileHistory
[i
])
2277 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
2278 menu
->Append(m_idBase
+i
, buf
);
2281 node
= node
->GetNext();
2286 void wxFileHistory::AddFilesToMenu(wxMenu
* menu
)
2288 if (m_fileHistoryN
> 0)
2290 if (menu
->GetMenuItemCount())
2292 menu
->AppendSeparator();
2296 for (i
= 0; i
< m_fileHistoryN
; i
++)
2298 if (m_fileHistory
[i
])
2301 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
2302 menu
->Append(m_idBase
+i
, buf
);
2308 // ----------------------------------------------------------------------------
2309 // Permits compatibility with existing file formats and functions that
2310 // manipulate files directly
2311 // ----------------------------------------------------------------------------
2313 #if wxUSE_STD_IOSTREAM
2315 bool wxTransferFileToStream(const wxString
& filename
, wxSTD ostream
& stream
)
2317 wxFFile
file(filename
, _T("rb"));
2318 if ( !file
.IsOpened() )
2326 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2330 stream
.write(buf
, nRead
);
2334 while ( !file
.Eof() );
2339 bool wxTransferStreamToFile(wxSTD istream
& stream
, const wxString
& filename
)
2341 wxFFile
file(filename
, _T("wb"));
2342 if ( !file
.IsOpened() )
2348 stream
.read(buf
, WXSIZEOF(buf
));
2349 if ( !stream
.bad() ) // fail may be set on EOF, don't use operator!()
2351 if ( !file
.Write(buf
, stream
.gcount()) )
2355 while ( !stream
.eof() );
2360 #else // !wxUSE_STD_IOSTREAM
2362 bool wxTransferFileToStream(const wxString
& filename
, wxOutputStream
& stream
)
2364 wxFFile
file(filename
, _T("rb"));
2365 if ( !file
.IsOpened() )
2373 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2377 stream
.Write(buf
, nRead
);
2381 while ( !file
.Eof() );
2386 bool wxTransferStreamToFile(wxInputStream
& stream
, const wxString
& filename
)
2388 wxFFile
file(filename
, _T("wb"));
2389 if ( !file
.IsOpened() )
2395 stream
.Read(buf
, WXSIZEOF(buf
));
2397 const size_t nRead
= stream
.LastRead();
2398 if ( !nRead
|| !file
.Write(buf
, nRead
) )
2401 while ( !stream
.Eof() );
2406 #endif // wxUSE_STD_IOSTREAM/!wxUSE_STD_IOSTREAM
2408 #endif // wxUSE_DOC_VIEW_ARCHITECTURE