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();
187 // first check if all views agree to be closed
188 const wxList::iterator end
= m_documentViews
.end();
189 for ( wxList::iterator i
= m_documentViews
.begin(); i
!= end
; ++i
)
191 wxView
*view
= (wxView
*)*i
;
192 if ( !view
->Close() )
196 // all views agreed to close, now do close them
197 if ( m_documentViews
.empty() )
199 // normally the document would be implicitly deleted when the last view
200 // is, but if don't have any views, do it here instead
201 if ( manager
&& manager
->GetDocuments().Member(this) )
206 // as we delete elements we iterate over, don't use the usual "from
207 // begin to end" loop
210 wxView
*view
= (wxView
*)*m_documentViews
.begin();
212 bool isLastOne
= m_documentViews
.size() == 1;
214 // this always deletes the node implicitly and if this is the last
215 // view also deletes this object itself (also implicitly, great),
216 // so we can't test for m_documentViews.empty() after calling this!
227 wxView
*wxDocument::GetFirstView() const
229 if (m_documentViews
.GetCount() == 0)
230 return (wxView
*) NULL
;
231 return (wxView
*)m_documentViews
.GetFirst()->GetData();
234 wxDocManager
*wxDocument::GetDocumentManager() const
236 return (m_documentTemplate
? m_documentTemplate
->GetDocumentManager() : (wxDocManager
*) NULL
);
239 bool wxDocument::OnNewDocument()
241 if (!OnSaveModified())
244 if (OnCloseDocument()==false) return false;
247 SetDocumentSaved(false);
250 GetDocumentManager()->MakeDefaultName(name
);
252 SetFilename(name
, true);
257 bool wxDocument::Save()
259 if (!IsModified() && m_savedYet
)
262 if ( m_documentFile
.empty() || !m_savedYet
)
265 return OnSaveDocument(m_documentFile
);
268 bool wxDocument::SaveAs()
270 wxDocTemplate
*docTemplate
= GetDocumentTemplate();
274 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
275 wxString filter
= docTemplate
->GetDescription() + wxT(" (") + docTemplate
->GetFileFilter() + wxT(")|") + docTemplate
->GetFileFilter();
277 // Now see if there are some other template with identical view and document
278 // classes, whose filters may also be used.
280 if (docTemplate
->GetViewClassInfo() && docTemplate
->GetDocClassInfo())
282 wxList::compatibility_iterator node
= wxDocManager::GetDocumentManager()->GetTemplates().GetFirst();
285 wxDocTemplate
*t
= (wxDocTemplate
*) node
->GetData();
287 if (t
->IsVisible() && t
!= docTemplate
&&
288 t
->GetViewClassInfo() == docTemplate
->GetViewClassInfo() &&
289 t
->GetDocClassInfo() == docTemplate
->GetDocClassInfo())
291 // add a '|' to separate this filter from the previous one
292 if ( !filter
.empty() )
295 filter
<< t
->GetDescription() << wxT(" (") << t
->GetFileFilter() << wxT(") |")
296 << t
->GetFileFilter();
299 node
= node
->GetNext();
303 wxString filter
= docTemplate
->GetFileFilter() ;
305 wxString tmp
= wxFileSelector(_("Save as"),
306 docTemplate
->GetDirectory(),
307 wxFileNameFromPath(GetFilename()),
308 docTemplate
->GetDefaultExtension(),
310 wxSAVE
| wxOVERWRITE_PROMPT
,
311 GetDocumentWindow());
316 wxString
fileName(tmp
);
317 wxString path
, name
, ext
;
318 wxSplitPath(fileName
, & path
, & name
, & ext
);
322 fileName
+= wxT(".");
323 fileName
+= docTemplate
->GetDefaultExtension();
326 SetFilename(fileName
);
327 SetTitle(wxFileNameFromPath(fileName
));
329 // Notify the views that the filename has changed
330 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
333 wxView
*view
= (wxView
*)node
->GetData();
334 view
->OnChangeFilename();
335 node
= node
->GetNext();
338 // Files that were not saved correctly are not added to the FileHistory.
339 if (!OnSaveDocument(m_documentFile
))
342 // A file that doesn't use the default extension of its document template cannot be opened
343 // via the FileHistory, so we do not add it.
344 if (docTemplate
->FileMatchesTemplate(fileName
))
346 GetDocumentManager()->AddFileToHistory(fileName
);
350 // The user will probably not be able to open the file again, so
351 // we could warn about the wrong file-extension here.
356 bool wxDocument::OnSaveDocument(const wxString
& file
)
361 if ( !DoSaveDocument(file
) )
366 SetDocumentSaved(true);
368 wxFileName
fn(file
) ;
369 fn
.MacSetDefaultTypeAndCreator() ;
374 bool wxDocument::OnOpenDocument(const wxString
& file
)
376 if (!OnSaveModified())
379 if ( !DoOpenDocument(file
) )
382 SetFilename(file
, true);
391 #if wxUSE_STD_IOSTREAM
392 wxSTD istream
& wxDocument::LoadObject(wxSTD istream
& stream
)
394 wxInputStream
& wxDocument::LoadObject(wxInputStream
& stream
)
400 #if wxUSE_STD_IOSTREAM
401 wxSTD ostream
& wxDocument::SaveObject(wxSTD ostream
& stream
)
403 wxOutputStream
& wxDocument::SaveObject(wxOutputStream
& stream
)
409 bool wxDocument::Revert()
415 // Get title, or filename if no title, else unnamed
416 bool wxDocument::GetPrintableName(wxString
& buf
) const
418 if (!m_documentTitle
.empty())
420 buf
= m_documentTitle
;
423 else if (!m_documentFile
.empty())
425 buf
= wxFileNameFromPath(m_documentFile
);
435 wxWindow
*wxDocument::GetDocumentWindow() const
437 wxView
*view
= GetFirstView();
439 return view
->GetFrame();
441 return wxTheApp
->GetTopWindow();
444 wxCommandProcessor
*wxDocument::OnCreateCommandProcessor()
446 return new wxCommandProcessor
;
449 // true if safe to close
450 bool wxDocument::OnSaveModified()
455 GetPrintableName(title
);
458 if (!wxTheApp
->GetAppName().empty())
459 msgTitle
= wxTheApp
->GetAppName();
461 msgTitle
= wxString(_("Warning"));
464 prompt
.Printf(_("Do you want to save changes to document %s?"),
465 (const wxChar
*)title
);
466 int res
= wxMessageBox(prompt
, msgTitle
,
467 wxYES_NO
|wxCANCEL
|wxICON_QUESTION
,
468 GetDocumentWindow());
474 else if (res
== wxYES
)
476 else if (res
== wxCANCEL
)
482 bool wxDocument::Draw(wxDC
& WXUNUSED(context
))
487 bool wxDocument::AddView(wxView
*view
)
489 if (!m_documentViews
.Member(view
))
491 m_documentViews
.Append(view
);
497 bool wxDocument::RemoveView(wxView
*view
)
499 (void)m_documentViews
.DeleteObject(view
);
504 bool wxDocument::OnCreate(const wxString
& WXUNUSED(path
), long flags
)
506 if (GetDocumentTemplate()->CreateView(this, flags
))
512 // Called after a view is added or removed.
513 // The default implementation deletes the document if
514 // there are no more views.
515 void wxDocument::OnChangedViewList()
517 if (m_documentViews
.GetCount() == 0)
519 if (OnSaveModified())
526 void wxDocument::UpdateAllViews(wxView
*sender
, wxObject
*hint
)
528 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
531 wxView
*view
= (wxView
*)node
->GetData();
533 view
->OnUpdate(sender
, hint
);
534 node
= node
->GetNext();
538 void wxDocument::NotifyClosing()
540 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
543 wxView
*view
= (wxView
*)node
->GetData();
544 view
->OnClosingDocument();
545 node
= node
->GetNext();
549 void wxDocument::SetFilename(const wxString
& filename
, bool notifyViews
)
551 m_documentFile
= filename
;
554 // Notify the views that the filename has changed
555 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
558 wxView
*view
= (wxView
*)node
->GetData();
559 view
->OnChangeFilename();
560 node
= node
->GetNext();
565 bool wxDocument::DoSaveDocument(const wxString
& file
)
568 if (!wxTheApp
->GetAppName().empty())
569 msgTitle
= wxTheApp
->GetAppName();
571 msgTitle
= wxString(_("File error"));
573 #if wxUSE_STD_IOSTREAM
574 wxSTD ofstream
store(file
.mb_str());
575 if (store
.fail() || store
.bad())
577 wxFileOutputStream
store(file
);
578 if (store
.GetLastError() != wxSTREAM_NO_ERROR
)
581 (void)wxMessageBox(_("Sorry, could not open this file for saving."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
582 GetDocumentWindow());
586 if (!SaveObject(store
))
588 (void)wxMessageBox(_("Sorry, could not save this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
589 GetDocumentWindow());
597 bool wxDocument::DoOpenDocument(const wxString
& file
)
600 if (!wxTheApp
->GetAppName().empty())
601 msgTitle
= wxTheApp
->GetAppName();
603 msgTitle
= wxString(_("File error"));
605 #if wxUSE_STD_IOSTREAM
606 wxSTD ifstream
store(file
.mb_str());
607 if (store
.fail() || store
.bad())
609 wxFileInputStream
store(file
);
610 if (store
.GetLastError() != wxSTREAM_NO_ERROR
)
613 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
|wxICON_EXCLAMATION
,
614 GetDocumentWindow());
617 #if wxUSE_STD_IOSTREAM
619 if ( !store
&& !store
.eof() )
621 int res
= LoadObject(store
).GetLastError();
622 if ((res
!= wxSTREAM_NO_ERROR
) &&
623 (res
!= wxSTREAM_EOF
))
626 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
|wxICON_EXCLAMATION
,
627 GetDocumentWindow());
635 // ----------------------------------------------------------------------------
637 // ----------------------------------------------------------------------------
641 m_viewDocument
= (wxDocument
*) NULL
;
643 m_viewFrame
= (wxFrame
*) NULL
;
648 GetDocumentManager()->ActivateView(this, false);
649 m_viewDocument
->RemoveView(this);
652 // Extend event processing to search the document's event table
653 bool wxView::ProcessEvent(wxEvent
& event
)
655 if ( !GetDocument() || !GetDocument()->ProcessEvent(event
) )
656 return wxEvtHandler::ProcessEvent(event
);
661 void wxView::OnActivateView(bool WXUNUSED(activate
), wxView
*WXUNUSED(activeView
), wxView
*WXUNUSED(deactiveView
))
665 void wxView::OnPrint(wxDC
*dc
, wxObject
*WXUNUSED(info
))
670 void wxView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
))
674 void wxView::OnChangeFilename()
676 if (GetFrame() && GetDocument())
680 GetDocument()->GetPrintableName(title
);
682 GetFrame()->SetTitle(title
);
686 void wxView::SetDocument(wxDocument
*doc
)
688 m_viewDocument
= doc
;
693 bool wxView::Close(bool deleteWindow
)
695 if (OnClose(deleteWindow
))
701 void wxView::Activate(bool activate
)
703 if (GetDocument() && GetDocumentManager())
705 OnActivateView(activate
, this, GetDocumentManager()->GetCurrentView());
706 GetDocumentManager()->ActivateView(this, activate
);
710 bool wxView::OnClose(bool WXUNUSED(deleteWindow
))
712 return GetDocument() ? GetDocument()->Close() : true;
715 #if wxUSE_PRINTING_ARCHITECTURE
716 wxPrintout
*wxView::OnCreatePrintout()
718 return new wxDocPrintout(this);
720 #endif // wxUSE_PRINTING_ARCHITECTURE
722 // ----------------------------------------------------------------------------
724 // ----------------------------------------------------------------------------
726 wxDocTemplate::wxDocTemplate(wxDocManager
*manager
,
727 const wxString
& descr
,
728 const wxString
& filter
,
731 const wxString
& docTypeName
,
732 const wxString
& viewTypeName
,
733 wxClassInfo
*docClassInfo
,
734 wxClassInfo
*viewClassInfo
,
737 m_documentManager
= manager
;
738 m_description
= descr
;
741 m_fileFilter
= filter
;
743 m_docTypeName
= docTypeName
;
744 m_viewTypeName
= viewTypeName
;
745 m_documentManager
->AssociateTemplate(this);
747 m_docClassInfo
= docClassInfo
;
748 m_viewClassInfo
= viewClassInfo
;
751 wxDocTemplate::~wxDocTemplate()
753 m_documentManager
->DisassociateTemplate(this);
756 // Tries to dynamically construct an object of the right class.
757 wxDocument
*wxDocTemplate::CreateDocument(const wxString
& path
, long flags
)
759 wxDocument
*doc
= DoCreateDocument();
761 return (wxDocument
*) NULL
;
763 if (InitDocument(doc
, path
, flags
))
769 return (wxDocument
*) NULL
;
773 bool wxDocTemplate::InitDocument(wxDocument
* doc
, const wxString
& path
, long flags
)
775 doc
->SetFilename(path
);
776 doc
->SetDocumentTemplate(this);
777 GetDocumentManager()->AddDocument(doc
);
778 doc
->SetCommandProcessor(doc
->OnCreateCommandProcessor());
780 if (doc
->OnCreate(path
, flags
))
784 if (GetDocumentManager()->GetDocuments().Member(doc
))
785 doc
->DeleteAllViews();
790 wxView
*wxDocTemplate::CreateView(wxDocument
*doc
, long flags
)
792 wxView
*view
= DoCreateView();
794 return (wxView
*) NULL
;
796 view
->SetDocument(doc
);
797 if (view
->OnCreate(doc
, flags
))
804 return (wxView
*) NULL
;
808 // The default (very primitive) format detection: check is the extension is
809 // that of the template
810 bool wxDocTemplate::FileMatchesTemplate(const wxString
& path
)
812 return GetDefaultExtension().IsSameAs(FindExtension(path
));
815 wxDocument
*wxDocTemplate::DoCreateDocument()
818 return (wxDocument
*) NULL
;
820 return (wxDocument
*)m_docClassInfo
->CreateObject();
823 wxView
*wxDocTemplate::DoCreateView()
825 if (!m_viewClassInfo
)
826 return (wxView
*) NULL
;
828 return (wxView
*)m_viewClassInfo
->CreateObject();
831 // ----------------------------------------------------------------------------
833 // ----------------------------------------------------------------------------
835 BEGIN_EVENT_TABLE(wxDocManager
, wxEvtHandler
)
836 EVT_MENU(wxID_OPEN
, wxDocManager::OnFileOpen
)
837 EVT_MENU(wxID_CLOSE
, wxDocManager::OnFileClose
)
838 EVT_MENU(wxID_CLOSE_ALL
, wxDocManager::OnFileCloseAll
)
839 EVT_MENU(wxID_REVERT
, wxDocManager::OnFileRevert
)
840 EVT_MENU(wxID_NEW
, wxDocManager::OnFileNew
)
841 EVT_MENU(wxID_SAVE
, wxDocManager::OnFileSave
)
842 EVT_MENU(wxID_SAVEAS
, wxDocManager::OnFileSaveAs
)
843 EVT_MENU(wxID_UNDO
, wxDocManager::OnUndo
)
844 EVT_MENU(wxID_REDO
, wxDocManager::OnRedo
)
846 EVT_UPDATE_UI(wxID_OPEN
, wxDocManager::OnUpdateFileOpen
)
847 EVT_UPDATE_UI(wxID_CLOSE
, wxDocManager::OnUpdateFileClose
)
848 EVT_UPDATE_UI(wxID_CLOSE_ALL
, wxDocManager::OnUpdateFileClose
)
849 EVT_UPDATE_UI(wxID_REVERT
, wxDocManager::OnUpdateFileRevert
)
850 EVT_UPDATE_UI(wxID_NEW
, wxDocManager::OnUpdateFileNew
)
851 EVT_UPDATE_UI(wxID_SAVE
, wxDocManager::OnUpdateFileSave
)
852 EVT_UPDATE_UI(wxID_SAVEAS
, wxDocManager::OnUpdateFileSaveAs
)
853 EVT_UPDATE_UI(wxID_UNDO
, wxDocManager::OnUpdateUndo
)
854 EVT_UPDATE_UI(wxID_REDO
, wxDocManager::OnUpdateRedo
)
856 #if wxUSE_PRINTING_ARCHITECTURE
857 EVT_MENU(wxID_PRINT
, wxDocManager::OnPrint
)
858 EVT_MENU(wxID_PREVIEW
, wxDocManager::OnPreview
)
860 EVT_UPDATE_UI(wxID_PRINT
, wxDocManager::OnUpdatePrint
)
861 EVT_UPDATE_UI(wxID_PREVIEW
, wxDocManager::OnUpdatePreview
)
865 wxDocManager
* wxDocManager::sm_docManager
= (wxDocManager
*) NULL
;
867 wxDocManager::wxDocManager(long flags
, bool initialize
)
869 m_defaultDocumentNameCounter
= 1;
871 m_currentView
= (wxView
*) NULL
;
872 m_maxDocsOpen
= 10000;
873 m_fileHistory
= (wxFileHistory
*) NULL
;
876 sm_docManager
= this;
879 wxDocManager::~wxDocManager()
883 delete m_fileHistory
;
884 sm_docManager
= (wxDocManager
*) NULL
;
887 // closes the specified document
888 bool wxDocManager::CloseDocument(wxDocument
* doc
, bool force
)
890 if (doc
->Close() || force
)
892 // Implicitly deletes the document when
893 // the last view is deleted
894 doc
->DeleteAllViews();
896 // Check we're really deleted
897 if (m_docs
.Member(doc
))
905 bool wxDocManager::CloseDocuments(bool force
)
907 wxList::compatibility_iterator node
= m_docs
.GetFirst();
910 wxDocument
*doc
= (wxDocument
*)node
->GetData();
911 wxList::compatibility_iterator next
= node
->GetNext();
913 if (!CloseDocument(doc
, force
))
916 // This assumes that documents are not connected in
917 // any way, i.e. deleting one document does NOT
924 bool wxDocManager::Clear(bool force
)
926 if (!CloseDocuments(force
))
929 m_currentView
= NULL
;
931 wxList::compatibility_iterator node
= m_templates
.GetFirst();
934 wxDocTemplate
*templ
= (wxDocTemplate
*) node
->GetData();
935 wxList::compatibility_iterator next
= node
->GetNext();
942 bool wxDocManager::Initialize()
944 m_fileHistory
= OnCreateFileHistory();
948 wxFileHistory
*wxDocManager::OnCreateFileHistory()
950 return new wxFileHistory
;
953 void wxDocManager::OnFileClose(wxCommandEvent
& WXUNUSED(event
))
955 wxDocument
*doc
= GetCurrentDocument();
960 doc
->DeleteAllViews();
961 if (m_docs
.Member(doc
))
966 void wxDocManager::OnFileCloseAll(wxCommandEvent
& WXUNUSED(event
))
968 CloseDocuments(false);
971 void wxDocManager::OnFileNew(wxCommandEvent
& WXUNUSED(event
))
973 CreateDocument( wxEmptyString
, wxDOC_NEW
);
976 void wxDocManager::OnFileOpen(wxCommandEvent
& WXUNUSED(event
))
978 if ( !CreateDocument( wxEmptyString
, 0) )
984 void wxDocManager::OnFileRevert(wxCommandEvent
& WXUNUSED(event
))
986 wxDocument
*doc
= GetCurrentDocument();
992 void wxDocManager::OnFileSave(wxCommandEvent
& WXUNUSED(event
))
994 wxDocument
*doc
= GetCurrentDocument();
1000 void wxDocManager::OnFileSaveAs(wxCommandEvent
& WXUNUSED(event
))
1002 wxDocument
*doc
= GetCurrentDocument();
1008 void wxDocManager::OnPrint(wxCommandEvent
& WXUNUSED(event
))
1010 #if wxUSE_PRINTING_ARCHITECTURE
1011 wxView
*view
= GetCurrentView();
1015 wxPrintout
*printout
= view
->OnCreatePrintout();
1019 printer
.Print(view
->GetFrame(), printout
, true);
1023 #endif // wxUSE_PRINTING_ARCHITECTURE
1026 void wxDocManager::OnPreview(wxCommandEvent
& WXUNUSED(event
))
1028 #if wxUSE_PRINTING_ARCHITECTURE
1029 wxView
*view
= GetCurrentView();
1033 wxPrintout
*printout
= view
->OnCreatePrintout();
1036 // Pass two printout objects: for preview, and possible printing.
1037 wxPrintPreviewBase
*preview
= new wxPrintPreview(printout
, view
->OnCreatePrintout());
1038 if ( !preview
->Ok() )
1041 wxMessageBox( _("Sorry, print preview needs a printer to be installed.") );
1045 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, (wxFrame
*)wxTheApp
->GetTopWindow(), _("Print Preview"),
1046 wxPoint(100, 100), wxSize(600, 650));
1047 frame
->Centre(wxBOTH
);
1048 frame
->Initialize();
1051 #endif // wxUSE_PRINTING_ARCHITECTURE
1054 void wxDocManager::OnUndo(wxCommandEvent
& event
)
1056 wxDocument
*doc
= GetCurrentDocument();
1059 if (doc
->GetCommandProcessor())
1060 doc
->GetCommandProcessor()->Undo();
1065 void wxDocManager::OnRedo(wxCommandEvent
& event
)
1067 wxDocument
*doc
= GetCurrentDocument();
1070 if (doc
->GetCommandProcessor())
1071 doc
->GetCommandProcessor()->Redo();
1076 // Handlers for UI update commands
1078 void wxDocManager::OnUpdateFileOpen(wxUpdateUIEvent
& event
)
1080 event
.Enable( true );
1083 void wxDocManager::OnUpdateFileClose(wxUpdateUIEvent
& event
)
1085 wxDocument
*doc
= GetCurrentDocument();
1086 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1089 void wxDocManager::OnUpdateFileRevert(wxUpdateUIEvent
& event
)
1091 wxDocument
*doc
= GetCurrentDocument();
1092 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1095 void wxDocManager::OnUpdateFileNew(wxUpdateUIEvent
& event
)
1097 event
.Enable( true );
1100 void wxDocManager::OnUpdateFileSave(wxUpdateUIEvent
& event
)
1102 wxDocument
*doc
= GetCurrentDocument();
1103 event
.Enable( doc
&& doc
->IsModified() );
1106 void wxDocManager::OnUpdateFileSaveAs(wxUpdateUIEvent
& event
)
1108 wxDocument
*doc
= GetCurrentDocument();
1109 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1112 void wxDocManager::OnUpdateUndo(wxUpdateUIEvent
& event
)
1114 wxDocument
*doc
= GetCurrentDocument();
1116 event
.Enable(false);
1117 else if (!doc
->GetCommandProcessor())
1121 event
.Enable( doc
->GetCommandProcessor()->CanUndo() );
1122 doc
->GetCommandProcessor()->SetMenuStrings();
1126 void wxDocManager::OnUpdateRedo(wxUpdateUIEvent
& event
)
1128 wxDocument
*doc
= GetCurrentDocument();
1130 event
.Enable(false);
1131 else if (!doc
->GetCommandProcessor())
1135 event
.Enable( doc
->GetCommandProcessor()->CanRedo() );
1136 doc
->GetCommandProcessor()->SetMenuStrings();
1140 void wxDocManager::OnUpdatePrint(wxUpdateUIEvent
& event
)
1142 wxDocument
*doc
= GetCurrentDocument();
1143 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1146 void wxDocManager::OnUpdatePreview(wxUpdateUIEvent
& event
)
1148 wxDocument
*doc
= GetCurrentDocument();
1149 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1152 wxView
*wxDocManager::GetCurrentView() const
1155 return m_currentView
;
1156 if (m_docs
.GetCount() == 1)
1158 wxDocument
* doc
= (wxDocument
*) m_docs
.GetFirst()->GetData();
1159 return doc
->GetFirstView();
1161 return (wxView
*) NULL
;
1164 // Extend event processing to search the view's event table
1165 bool wxDocManager::ProcessEvent(wxEvent
& event
)
1167 wxView
* view
= GetCurrentView();
1170 if (view
->ProcessEvent(event
))
1173 return wxEvtHandler::ProcessEvent(event
);
1176 wxDocument
*wxDocManager::CreateDocument(const wxString
& path
, long flags
)
1178 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.GetCount()];
1181 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1183 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Item(i
)->GetData());
1184 if (temp
->IsVisible())
1186 templates
[n
] = temp
;
1193 return (wxDocument
*) NULL
;
1196 wxDocument
* docToClose
= NULL
;
1198 // If we've reached the max number of docs, close the
1200 if ( (int)GetDocuments().GetCount() >= m_maxDocsOpen
)
1202 wxDocument
*doc
= (wxDocument
*)GetDocuments().GetFirst()->GetData();
1206 // New document: user chooses a template, unless there's only one.
1207 if (flags
& wxDOC_NEW
)
1213 if (!CloseDocument(docToClose
, false))
1220 wxDocTemplate
*temp
= templates
[0];
1222 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
1226 newDoc
->SetDocumentName(temp
->GetDocumentName());
1227 newDoc
->SetDocumentTemplate(temp
);
1228 newDoc
->OnNewDocument();
1233 wxDocTemplate
*temp
= SelectDocumentType(templates
, n
);
1239 if (!CloseDocument(docToClose
, false))
1245 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
1249 newDoc
->SetDocumentName(temp
->GetDocumentName());
1250 newDoc
->SetDocumentTemplate(temp
);
1251 newDoc
->OnNewDocument();
1256 return (wxDocument
*) NULL
;
1259 // Existing document
1260 wxDocTemplate
*temp
;
1262 wxString path2
= path
;
1264 if (flags
& wxDOC_SILENT
)
1266 temp
= FindTemplateForPath(path2
);
1269 // Since we do not add files with non-default extensions to the FileHistory this
1270 // can only happen if the application changes the allowed templates in runtime.
1271 (void)wxMessageBox(_("Sorry, the format for this file is unknown."),
1273 wxOK
| wxICON_EXCLAMATION
, wxFindSuitableParent());
1277 temp
= SelectDocumentPath(templates
, n
, path2
, flags
);
1285 if (!CloseDocument(docToClose
, false))
1291 wxDocument
*newDoc
= temp
->CreateDocument(path2
, flags
);
1294 newDoc
->SetDocumentName(temp
->GetDocumentName());
1295 newDoc
->SetDocumentTemplate(temp
);
1296 if (!newDoc
->OnOpenDocument(path2
))
1298 newDoc
->DeleteAllViews();
1299 // delete newDoc; // Implicitly deleted by DeleteAllViews
1300 return (wxDocument
*) NULL
;
1302 // A file that doesn't use the default extension of its document
1303 // template cannot be opened via the FileHistory, so we do not
1305 if (temp
->FileMatchesTemplate(path2
))
1306 AddFileToHistory(path2
);
1311 return (wxDocument
*) NULL
;
1314 wxView
*wxDocManager::CreateView(wxDocument
*doc
, long flags
)
1316 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.GetCount()];
1319 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1321 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Item(i
)->GetData());
1322 if (temp
->IsVisible())
1324 if (temp
->GetDocumentName() == doc
->GetDocumentName())
1326 templates
[n
] = temp
;
1334 return (wxView
*) NULL
;
1338 wxDocTemplate
*temp
= templates
[0];
1340 wxView
*view
= temp
->CreateView(doc
, flags
);
1342 view
->SetViewName(temp
->GetViewName());
1346 wxDocTemplate
*temp
= SelectViewType(templates
, n
);
1350 wxView
*view
= temp
->CreateView(doc
, flags
);
1352 view
->SetViewName(temp
->GetViewName());
1356 return (wxView
*) NULL
;
1359 // Not yet implemented
1360 void wxDocManager::DeleteTemplate(wxDocTemplate
*WXUNUSED(temp
), long WXUNUSED(flags
))
1364 // Not yet implemented
1365 bool wxDocManager::FlushDoc(wxDocument
*WXUNUSED(doc
))
1370 wxDocument
*wxDocManager::GetCurrentDocument() const
1372 wxView
*view
= GetCurrentView();
1374 return view
->GetDocument();
1376 return (wxDocument
*) NULL
;
1379 // Make a default document name
1380 bool wxDocManager::MakeDefaultName(wxString
& name
)
1382 name
.Printf(_("unnamed%d"), m_defaultDocumentNameCounter
);
1383 m_defaultDocumentNameCounter
++;
1388 // Make a frame title (override this to do something different)
1389 // If docName is empty, a document is not currently active.
1390 wxString
wxDocManager::MakeFrameTitle(wxDocument
* doc
)
1392 wxString appName
= wxTheApp
->GetAppName();
1399 doc
->GetPrintableName(docName
);
1400 title
= docName
+ wxString(_(" - ")) + appName
;
1406 // Not yet implemented
1407 wxDocTemplate
*wxDocManager::MatchTemplate(const wxString
& WXUNUSED(path
))
1409 return (wxDocTemplate
*) NULL
;
1412 // File history management
1413 void wxDocManager::AddFileToHistory(const wxString
& file
)
1416 m_fileHistory
->AddFileToHistory(file
);
1419 void wxDocManager::RemoveFileFromHistory(size_t i
)
1422 m_fileHistory
->RemoveFileFromHistory(i
);
1425 wxString
wxDocManager::GetHistoryFile(size_t i
) const
1430 histFile
= m_fileHistory
->GetHistoryFile(i
);
1435 void wxDocManager::FileHistoryUseMenu(wxMenu
*menu
)
1438 m_fileHistory
->UseMenu(menu
);
1441 void wxDocManager::FileHistoryRemoveMenu(wxMenu
*menu
)
1444 m_fileHistory
->RemoveMenu(menu
);
1448 void wxDocManager::FileHistoryLoad(wxConfigBase
& config
)
1451 m_fileHistory
->Load(config
);
1454 void wxDocManager::FileHistorySave(wxConfigBase
& config
)
1457 m_fileHistory
->Save(config
);
1461 void wxDocManager::FileHistoryAddFilesToMenu(wxMenu
* menu
)
1464 m_fileHistory
->AddFilesToMenu(menu
);
1467 void wxDocManager::FileHistoryAddFilesToMenu()
1470 m_fileHistory
->AddFilesToMenu();
1473 size_t wxDocManager::GetHistoryFilesCount() const
1475 return m_fileHistory
? m_fileHistory
->GetCount() : 0;
1479 // Find out the document template via matching in the document file format
1480 // against that of the template
1481 wxDocTemplate
*wxDocManager::FindTemplateForPath(const wxString
& path
)
1483 wxDocTemplate
*theTemplate
= (wxDocTemplate
*) NULL
;
1485 // Find the template which this extension corresponds to
1486 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1488 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Item(i
)->GetData();
1489 if ( temp
->FileMatchesTemplate(path
) )
1498 // Try to get a more suitable parent frame than the top window,
1499 // for selection dialogs. Otherwise you may get an unexpected
1500 // window being activated when a dialog is shown.
1501 static wxWindow
* wxFindSuitableParent()
1503 wxWindow
* parent
= wxTheApp
->GetTopWindow();
1505 wxWindow
* focusWindow
= wxWindow::FindFocus();
1508 while (focusWindow
&&
1509 !focusWindow
->IsKindOf(CLASSINFO(wxDialog
)) &&
1510 !focusWindow
->IsKindOf(CLASSINFO(wxFrame
)))
1512 focusWindow
= focusWindow
->GetParent();
1515 parent
= focusWindow
;
1520 // Prompts user to open a file, using file specs in templates.
1521 // Must extend the file selector dialog or implement own; OR
1522 // match the extension to the template extension.
1524 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**templates
,
1525 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
1528 int WXUNUSED(noTemplates
),
1531 long WXUNUSED(flags
),
1532 bool WXUNUSED(save
))
1534 // We can only have multiple filters in Windows and GTK
1535 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
1539 for (i
= 0; i
< noTemplates
; i
++)
1541 if (templates
[i
]->IsVisible())
1543 // add a '|' to separate this filter from the previous one
1544 if ( !descrBuf
.empty() )
1545 descrBuf
<< wxT('|');
1547 descrBuf
<< templates
[i
]->GetDescription()
1548 << wxT(" (") << templates
[i
]->GetFileFilter() << wxT(") |")
1549 << templates
[i
]->GetFileFilter();
1553 wxString descrBuf
= wxT("*.*");
1556 int FilterIndex
= -1;
1558 wxWindow
* parent
= wxFindSuitableParent();
1560 wxString pathTmp
= wxFileSelectorEx(_("Select a file"),
1568 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)NULL
;
1569 if (!pathTmp
.empty())
1571 if (!wxFileExists(pathTmp
))
1574 if (!wxTheApp
->GetAppName().empty())
1575 msgTitle
= wxTheApp
->GetAppName();
1577 msgTitle
= wxString(_("File error"));
1579 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
1582 path
= wxEmptyString
;
1583 return (wxDocTemplate
*) NULL
;
1585 m_lastDirectory
= wxPathOnly(pathTmp
);
1589 // first choose the template using the extension, if this fails (i.e.
1590 // wxFileSelectorEx() didn't fill it), then use the path
1591 if ( FilterIndex
!= -1 )
1592 theTemplate
= templates
[FilterIndex
];
1594 theTemplate
= FindTemplateForPath(path
);
1597 // Since we do not add files with non-default extensions to the FileHistory this
1598 // can only happen if the application changes the allowed templates in runtime.
1599 (void)wxMessageBox(_("Sorry, the format for this file is unknown."),
1601 wxOK
| wxICON_EXCLAMATION
, wxFindSuitableParent());
1606 path
= wxEmptyString
;
1612 wxDocTemplate
*wxDocManager::SelectDocumentType(wxDocTemplate
**templates
,
1613 int noTemplates
, bool sort
)
1615 wxArrayString strings
;
1616 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1620 for (i
= 0; i
< noTemplates
; i
++)
1622 if (templates
[i
]->IsVisible())
1626 for (j
= 0; j
< n
; j
++)
1628 //filter out NOT unique documents + view combinations
1629 if ( templates
[i
]->m_docTypeName
== data
[j
]->m_docTypeName
&&
1630 templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
1637 strings
.Add(templates
[i
]->m_description
);
1639 data
[n
] = templates
[i
];
1647 strings
.Sort(); // ascending sort
1648 // Yes, this will be slow, but template lists
1649 // are typically short.
1651 n
= strings
.Count();
1652 for (i
= 0; i
< n
; i
++)
1654 for (j
= 0; j
< noTemplates
; j
++)
1656 if (strings
[i
] == templates
[j
]->m_description
)
1657 data
[i
] = templates
[j
];
1662 wxDocTemplate
*theTemplate
;
1667 // no visible templates, hence nothing to choose from
1672 // don't propose the user to choose if he heas no choice
1673 theTemplate
= data
[0];
1677 // propose the user to choose one of several
1678 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1680 _("Select a document template"),
1684 wxFindSuitableParent()
1693 wxDocTemplate
*wxDocManager::SelectViewType(wxDocTemplate
**templates
,
1694 int noTemplates
, bool sort
)
1696 wxArrayString strings
;
1697 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1701 for (i
= 0; i
< noTemplates
; i
++)
1703 wxDocTemplate
*templ
= templates
[i
];
1704 if ( templ
->IsVisible() && !templ
->GetViewName().empty() )
1708 for (j
= 0; j
< n
; j
++)
1710 //filter out NOT unique views
1711 if ( templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
)
1717 strings
.Add(templ
->m_viewTypeName
);
1726 strings
.Sort(); // ascending sort
1727 // Yes, this will be slow, but template lists
1728 // are typically short.
1730 n
= strings
.Count();
1731 for (i
= 0; i
< n
; i
++)
1733 for (j
= 0; j
< noTemplates
; j
++)
1735 if (strings
[i
] == templates
[j
]->m_viewTypeName
)
1736 data
[i
] = templates
[j
];
1741 wxDocTemplate
*theTemplate
;
1743 // the same logic as above
1747 theTemplate
= (wxDocTemplate
*)NULL
;
1751 theTemplate
= data
[0];
1755 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1757 _("Select a document view"),
1761 wxFindSuitableParent()
1770 void wxDocManager::AssociateTemplate(wxDocTemplate
*temp
)
1772 if (!m_templates
.Member(temp
))
1773 m_templates
.Append(temp
);
1776 void wxDocManager::DisassociateTemplate(wxDocTemplate
*temp
)
1778 m_templates
.DeleteObject(temp
);
1781 // Add and remove a document from the manager's list
1782 void wxDocManager::AddDocument(wxDocument
*doc
)
1784 if (!m_docs
.Member(doc
))
1788 void wxDocManager::RemoveDocument(wxDocument
*doc
)
1790 m_docs
.DeleteObject(doc
);
1793 // Views or windows should inform the document manager
1794 // when a view is going in or out of focus
1795 void wxDocManager::ActivateView(wxView
*view
, bool activate
)
1799 m_currentView
= view
;
1803 if ( m_currentView
== view
)
1805 // don't keep stale pointer
1806 m_currentView
= (wxView
*) NULL
;
1811 // ----------------------------------------------------------------------------
1812 // Default document child frame
1813 // ----------------------------------------------------------------------------
1815 BEGIN_EVENT_TABLE(wxDocChildFrame
, wxFrame
)
1816 EVT_ACTIVATE(wxDocChildFrame::OnActivate
)
1817 EVT_CLOSE(wxDocChildFrame::OnCloseWindow
)
1820 wxDocChildFrame::wxDocChildFrame(wxDocument
*doc
,
1824 const wxString
& title
,
1828 const wxString
& name
)
1829 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1831 m_childDocument
= doc
;
1834 view
->SetFrame(this);
1837 // Extend event processing to search the view's event table
1838 bool wxDocChildFrame::ProcessEvent(wxEvent
& event
)
1841 m_childView
->Activate(true);
1843 if ( !m_childView
|| ! m_childView
->ProcessEvent(event
) )
1845 // Only hand up to the parent if it's a menu command
1846 if (!event
.IsKindOf(CLASSINFO(wxCommandEvent
)) || !GetParent() || !GetParent()->ProcessEvent(event
))
1847 return wxEvtHandler::ProcessEvent(event
);
1855 void wxDocChildFrame::OnActivate(wxActivateEvent
& event
)
1857 wxFrame::OnActivate(event
);
1860 m_childView
->Activate(event
.GetActive());
1863 void wxDocChildFrame::OnCloseWindow(wxCloseEvent
& event
)
1867 bool ans
= event
.CanVeto()
1868 ? m_childView
->Close(false) // false means don't delete associated window
1869 : true; // Must delete.
1873 m_childView
->Activate(false);
1875 m_childView
= (wxView
*) NULL
;
1876 m_childDocument
= (wxDocument
*) NULL
;
1887 // ----------------------------------------------------------------------------
1888 // Default parent frame
1889 // ----------------------------------------------------------------------------
1891 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1892 EVT_MENU(wxID_EXIT
, wxDocParentFrame::OnExit
)
1893 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE9
, wxDocParentFrame::OnMRUFile
)
1894 EVT_CLOSE(wxDocParentFrame::OnCloseWindow
)
1897 wxDocParentFrame::wxDocParentFrame(wxDocManager
*manager
,
1900 const wxString
& title
,
1904 const wxString
& name
)
1905 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1907 m_docManager
= manager
;
1910 void wxDocParentFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
1915 void wxDocParentFrame::OnMRUFile(wxCommandEvent
& event
)
1917 int n
= event
.GetId() - wxID_FILE1
; // the index in MRU list
1918 wxString
filename(m_docManager
->GetHistoryFile(n
));
1919 if ( !filename
.empty() )
1921 // verify that the file exists before doing anything else
1922 if ( wxFile::Exists(filename
) )
1925 if (!m_docManager
->CreateDocument(filename
, wxDOC_SILENT
))
1927 // remove the file from the MRU list. The user should already be notified.
1928 m_docManager
->RemoveFileFromHistory(n
);
1930 wxLogError(_("The file '%s' couldn't be opened.\nIt has been removed from the most recently used files list."),
1936 // remove the bogus filename from the MRU list and notify the user
1938 m_docManager
->RemoveFileFromHistory(n
);
1940 wxLogError(_("The file '%s' doesn't exist and couldn't be opened.\nIt has been removed from the most recently used files list."),
1946 // Extend event processing to search the view's event table
1947 bool wxDocParentFrame::ProcessEvent(wxEvent
& event
)
1949 // Try the document manager, then do default processing
1950 if (!m_docManager
|| !m_docManager
->ProcessEvent(event
))
1951 return wxEvtHandler::ProcessEvent(event
);
1956 // Define the behaviour for the frame closing
1957 // - must delete all frames except for the main one.
1958 void wxDocParentFrame::OnCloseWindow(wxCloseEvent
& event
)
1960 if (m_docManager
->Clear(!event
.CanVeto()))
1968 #if wxUSE_PRINTING_ARCHITECTURE
1970 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
)
1973 m_printoutView
= view
;
1976 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
1980 // Get the logical pixels per inch of screen and printer
1981 int ppiScreenX
, ppiScreenY
;
1982 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
1983 wxUnusedVar(ppiScreenY
);
1984 int ppiPrinterX
, ppiPrinterY
;
1985 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
1986 wxUnusedVar(ppiPrinterY
);
1988 // This scales the DC so that the printout roughly represents the
1989 // the screen scaling. The text point size _should_ be the right size
1990 // but in fact is too small for some reason. This is a detail that will
1991 // need to be addressed at some point but can be fudged for the
1993 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
1995 // Now we have to check in case our real page size is reduced
1996 // (e.g. because we're drawing to a print preview memory DC)
1997 int pageWidth
, pageHeight
;
1999 dc
->GetSize(&w
, &h
);
2000 GetPageSizePixels(&pageWidth
, &pageHeight
);
2001 wxUnusedVar(pageHeight
);
2003 // If printer pageWidth == current DC width, then this doesn't
2004 // change. But w might be the preview bitmap width, so scale down.
2005 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
2006 dc
->SetUserScale(overallScale
, overallScale
);
2010 m_printoutView
->OnDraw(dc
);
2015 bool wxDocPrintout::HasPage(int pageNum
)
2017 return (pageNum
== 1);
2020 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
2022 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
2028 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
2036 #endif // wxUSE_PRINTING_ARCHITECTURE
2038 // ----------------------------------------------------------------------------
2039 // File history processor
2040 // ----------------------------------------------------------------------------
2042 static inline wxChar
* MYcopystring(const wxString
& s
)
2044 wxChar
* copy
= new wxChar
[s
.length() + 1];
2045 return wxStrcpy(copy
, s
.c_str());
2048 static inline wxChar
* MYcopystring(const wxChar
* s
)
2050 wxChar
* copy
= new wxChar
[wxStrlen(s
) + 1];
2051 return wxStrcpy(copy
, s
);
2054 wxFileHistory::wxFileHistory(size_t maxFiles
, wxWindowID idBase
)
2056 m_fileMaxFiles
= maxFiles
;
2059 m_fileHistory
= new wxChar
*[m_fileMaxFiles
];
2062 wxFileHistory::~wxFileHistory()
2065 for (i
= 0; i
< m_fileHistoryN
; i
++)
2066 delete[] m_fileHistory
[i
];
2067 delete[] m_fileHistory
;
2070 // File history management
2071 void wxFileHistory::AddFileToHistory(const wxString
& file
)
2075 // Check we don't already have this file
2076 for (i
= 0; i
< m_fileHistoryN
; i
++)
2078 #if defined( __WXMSW__ ) // Add any other OSes with case insensitive file names
2079 wxString testString
;
2080 if ( m_fileHistory
[i
] )
2081 testString
= m_fileHistory
[i
];
2082 if ( m_fileHistory
[i
] && ( file
.Lower() == testString
.Lower() ) )
2084 if ( m_fileHistory
[i
] && ( file
== m_fileHistory
[i
] ) )
2087 // we do have it, move it to the top of the history
2088 RemoveFileFromHistory (i
);
2089 AddFileToHistory (file
);
2094 // if we already have a full history, delete the one at the end
2095 if ( m_fileMaxFiles
== m_fileHistoryN
)
2097 RemoveFileFromHistory (m_fileHistoryN
- 1);
2098 AddFileToHistory (file
);
2102 // Add to the project file history:
2103 // Move existing files (if any) down so we can insert file at beginning.
2104 if (m_fileHistoryN
< m_fileMaxFiles
)
2106 wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2109 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2110 if ( m_fileHistoryN
== 0 && menu
->GetMenuItemCount() )
2112 menu
->AppendSeparator();
2114 menu
->Append(m_idBase
+m_fileHistoryN
, _("[EMPTY]"));
2115 node
= node
->GetNext();
2119 // Shuffle filenames down
2120 for (i
= (m_fileHistoryN
-1); i
> 0; i
--)
2122 m_fileHistory
[i
] = m_fileHistory
[i
-1];
2124 m_fileHistory
[0] = MYcopystring(file
);
2126 // this is the directory of the last opened file
2127 wxString pathCurrent
;
2128 wxSplitPath( m_fileHistory
[0], &pathCurrent
, NULL
, NULL
);
2129 for (i
= 0; i
< m_fileHistoryN
; i
++)
2131 if ( m_fileHistory
[i
] )
2133 // if in same directory just show the filename; otherwise the full
2135 wxString pathInMenu
, path
, filename
, ext
;
2136 wxSplitPath( m_fileHistory
[i
], &path
, &filename
, &ext
);
2137 if ( path
== pathCurrent
)
2139 pathInMenu
= filename
;
2141 pathInMenu
= pathInMenu
+ wxFILE_SEP_EXT
+ ext
;
2145 // absolute path; could also set relative path
2146 pathInMenu
= m_fileHistory
[i
];
2150 buf
.Printf(s_MRUEntryFormat
, i
+ 1, pathInMenu
.c_str());
2151 wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2154 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2155 menu
->SetLabel(m_idBase
+ i
, buf
);
2156 node
= node
->GetNext();
2162 void wxFileHistory::RemoveFileFromHistory(size_t i
)
2164 wxCHECK_RET( i
< m_fileHistoryN
,
2165 wxT("invalid index in wxFileHistory::RemoveFileFromHistory") );
2167 // delete the element from the array (could use memmove() too...)
2168 delete [] m_fileHistory
[i
];
2171 for ( j
= i
; j
< m_fileHistoryN
- 1; j
++ )
2173 m_fileHistory
[j
] = m_fileHistory
[j
+ 1];
2176 wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2179 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2181 // shuffle filenames up
2183 for ( j
= i
; j
< m_fileHistoryN
- 1; j
++ )
2185 buf
.Printf(s_MRUEntryFormat
, j
+ 1, m_fileHistory
[j
]);
2186 menu
->SetLabel(m_idBase
+ j
, buf
);
2189 node
= node
->GetNext();
2191 // delete the last menu item which is unused now
2192 wxWindowID lastItemId
= m_idBase
+ m_fileHistoryN
- 1;
2193 if (menu
->FindItem(lastItemId
))
2195 menu
->Delete(lastItemId
);
2198 // delete the last separator too if no more files are left
2199 if ( m_fileHistoryN
== 1 )
2201 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetLast();
2204 wxMenuItem
*menuItem
= node
->GetData();
2205 if ( menuItem
->IsSeparator() )
2207 menu
->Delete(menuItem
);
2209 //else: should we search backwards for the last separator?
2211 //else: menu is empty somehow
2218 wxString
wxFileHistory::GetHistoryFile(size_t i
) const
2221 if ( i
< m_fileHistoryN
)
2223 s
= m_fileHistory
[i
];
2227 wxFAIL_MSG( wxT("bad index in wxFileHistory::GetHistoryFile") );
2233 void wxFileHistory::UseMenu(wxMenu
*menu
)
2235 if (!m_fileMenus
.Member(menu
))
2236 m_fileMenus
.Append(menu
);
2239 void wxFileHistory::RemoveMenu(wxMenu
*menu
)
2241 m_fileMenus
.DeleteObject(menu
);
2245 void wxFileHistory::Load(wxConfigBase
& config
)
2249 buf
.Printf(wxT("file%d"), (int)m_fileHistoryN
+1);
2250 wxString historyFile
;
2251 while ((m_fileHistoryN
< m_fileMaxFiles
) && config
.Read(buf
, &historyFile
) && (!historyFile
.empty()))
2253 m_fileHistory
[m_fileHistoryN
] = MYcopystring((const wxChar
*) historyFile
);
2255 buf
.Printf(wxT("file%d"), (int)m_fileHistoryN
+1);
2256 historyFile
= wxEmptyString
;
2261 void wxFileHistory::Save(wxConfigBase
& config
)
2264 for (i
= 0; i
< m_fileMaxFiles
; i
++)
2267 buf
.Printf(wxT("file%d"), (int)i
+1);
2268 if (i
< m_fileHistoryN
)
2269 config
.Write(buf
, wxString(m_fileHistory
[i
]));
2271 config
.Write(buf
, wxEmptyString
);
2274 #endif // wxUSE_CONFIG
2276 void wxFileHistory::AddFilesToMenu()
2278 if (m_fileHistoryN
> 0)
2280 wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2283 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2284 if (menu
->GetMenuItemCount())
2286 menu
->AppendSeparator();
2290 for (i
= 0; i
< m_fileHistoryN
; i
++)
2292 if (m_fileHistory
[i
])
2295 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
2296 menu
->Append(m_idBase
+i
, buf
);
2299 node
= node
->GetNext();
2304 void wxFileHistory::AddFilesToMenu(wxMenu
* menu
)
2306 if (m_fileHistoryN
> 0)
2308 if (menu
->GetMenuItemCount())
2310 menu
->AppendSeparator();
2314 for (i
= 0; i
< m_fileHistoryN
; i
++)
2316 if (m_fileHistory
[i
])
2319 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
2320 menu
->Append(m_idBase
+i
, buf
);
2326 // ----------------------------------------------------------------------------
2327 // Permits compatibility with existing file formats and functions that
2328 // manipulate files directly
2329 // ----------------------------------------------------------------------------
2331 #if wxUSE_STD_IOSTREAM
2333 bool wxTransferFileToStream(const wxString
& filename
, wxSTD ostream
& stream
)
2335 wxFFile
file(filename
, _T("rb"));
2336 if ( !file
.IsOpened() )
2344 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2348 stream
.write(buf
, nRead
);
2352 while ( !file
.Eof() );
2357 bool wxTransferStreamToFile(wxSTD istream
& stream
, const wxString
& filename
)
2359 wxFFile
file(filename
, _T("wb"));
2360 if ( !file
.IsOpened() )
2366 stream
.read(buf
, WXSIZEOF(buf
));
2367 if ( !stream
.bad() ) // fail may be set on EOF, don't use operator!()
2369 if ( !file
.Write(buf
, stream
.gcount()) )
2373 while ( !stream
.eof() );
2378 #else // !wxUSE_STD_IOSTREAM
2380 bool wxTransferFileToStream(const wxString
& filename
, wxOutputStream
& stream
)
2382 wxFFile
file(filename
, _T("rb"));
2383 if ( !file
.IsOpened() )
2391 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2395 stream
.Write(buf
, nRead
);
2399 while ( !file
.Eof() );
2404 bool wxTransferStreamToFile(wxInputStream
& stream
, const wxString
& filename
)
2406 wxFFile
file(filename
, _T("wb"));
2407 if ( !file
.IsOpened() )
2413 stream
.Read(buf
, WXSIZEOF(buf
));
2415 const size_t nRead
= stream
.LastRead();
2416 if ( !nRead
|| !file
.Write(buf
, nRead
) )
2419 while ( !stream
.Eof() );
2424 #endif // wxUSE_STD_IOSTREAM/!wxUSE_STD_IOSTREAM
2426 #endif // wxUSE_DOC_VIEW_ARCHITECTURE