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 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #if wxUSE_DOC_VIEW_ARCHITECTURE
30 #include "wx/string.h"
34 #include "wx/dialog.h"
37 #include "wx/filedlg.h"
45 #include "wx/filename.h"
52 #if wxUSE_PRINTING_ARCHITECTURE
53 #include "wx/prntbase.h"
54 #include "wx/printdlg.h"
57 #include "wx/msgdlg.h"
58 #include "wx/choicdlg.h"
59 #include "wx/docview.h"
60 #include "wx/confbase.h"
62 #include "wx/cmdproc.h"
67 #if wxUSE_STD_IOSTREAM
68 #include "wx/ioswrap.h"
75 #include "wx/wfstream.h"
78 // ----------------------------------------------------------------------------
80 // ----------------------------------------------------------------------------
82 IMPLEMENT_ABSTRACT_CLASS(wxDocument
, wxEvtHandler
)
83 IMPLEMENT_ABSTRACT_CLASS(wxView
, wxEvtHandler
)
84 IMPLEMENT_ABSTRACT_CLASS(wxDocTemplate
, wxObject
)
85 IMPLEMENT_DYNAMIC_CLASS(wxDocManager
, wxEvtHandler
)
86 IMPLEMENT_CLASS(wxDocChildFrame
, wxFrame
)
87 IMPLEMENT_CLASS(wxDocParentFrame
, wxFrame
)
89 #if wxUSE_PRINTING_ARCHITECTURE
90 IMPLEMENT_DYNAMIC_CLASS(wxDocPrintout
, wxPrintout
)
93 IMPLEMENT_DYNAMIC_CLASS(wxFileHistory
, wxObject
)
95 // ----------------------------------------------------------------------------
96 // function prototypes
97 // ----------------------------------------------------------------------------
99 static inline wxString
FindExtension(const wxChar
*path
);
100 static wxWindow
* wxFindSuitableParent(void);
102 // ----------------------------------------------------------------------------
104 // ----------------------------------------------------------------------------
106 static const wxChar
*s_MRUEntryFormat
= wxT("&%d %s");
108 // ============================================================================
110 // ============================================================================
112 // ----------------------------------------------------------------------------
114 // ----------------------------------------------------------------------------
116 static wxString
FindExtension(const wxChar
*path
)
119 wxSplitPath(path
, NULL
, NULL
, &ext
);
121 // VZ: extensions are considered not case sensitive - is this really a good
123 return ext
.MakeLower();
126 // ----------------------------------------------------------------------------
127 // Definition of wxDocument
128 // ----------------------------------------------------------------------------
130 wxDocument::wxDocument(wxDocument
*parent
)
132 m_documentModified
= false;
133 m_documentParent
= parent
;
134 m_documentTemplate
= (wxDocTemplate
*) NULL
;
135 m_commandProcessor
= (wxCommandProcessor
*) NULL
;
139 bool wxDocument::DeleteContents()
144 wxDocument::~wxDocument()
148 if (m_commandProcessor
)
149 delete m_commandProcessor
;
151 if (GetDocumentManager())
152 GetDocumentManager()->RemoveDocument(this);
154 // Not safe to do here, since it'll invoke virtual view functions
155 // expecting to see valid derived objects: and by the time we get here,
156 // we've called destructors higher up.
160 bool wxDocument::Close()
162 if (OnSaveModified())
163 return OnCloseDocument();
168 bool wxDocument::OnCloseDocument()
170 // Tell all views that we're about to close
177 // Note that this implicitly deletes the document when the last view is
179 bool wxDocument::DeleteAllViews()
181 wxDocManager
* manager
= GetDocumentManager();
183 // first check if all views agree to be closed
184 const wxList::iterator end
= m_documentViews
.end();
185 for ( wxList::iterator i
= m_documentViews
.begin(); i
!= end
; ++i
)
187 wxView
*view
= (wxView
*)*i
;
188 if ( !view
->Close() )
192 // all views agreed to close, now do close them
193 if ( m_documentViews
.empty() )
195 // normally the document would be implicitly deleted when the last view
196 // is, but if don't have any views, do it here instead
197 if ( manager
&& manager
->GetDocuments().Member(this) )
202 // as we delete elements we iterate over, don't use the usual "from
203 // begin to end" loop
206 wxView
*view
= (wxView
*)*m_documentViews
.begin();
208 bool isLastOne
= m_documentViews
.size() == 1;
210 // this always deletes the node implicitly and if this is the last
211 // view also deletes this object itself (also implicitly, great),
212 // so we can't test for m_documentViews.empty() after calling this!
223 wxView
*wxDocument::GetFirstView() const
225 if (m_documentViews
.GetCount() == 0)
226 return (wxView
*) NULL
;
227 return (wxView
*)m_documentViews
.GetFirst()->GetData();
230 wxDocManager
*wxDocument::GetDocumentManager() const
232 return (m_documentTemplate
? m_documentTemplate
->GetDocumentManager() : (wxDocManager
*) NULL
);
235 bool wxDocument::OnNewDocument()
237 if (!OnSaveModified())
240 if (OnCloseDocument()==false) return false;
243 SetDocumentSaved(false);
246 GetDocumentManager()->MakeDefaultName(name
);
248 SetFilename(name
, true);
253 bool wxDocument::Save()
255 if (!IsModified() && m_savedYet
)
258 if ( m_documentFile
.empty() || !m_savedYet
)
261 return OnSaveDocument(m_documentFile
);
264 bool wxDocument::SaveAs()
266 wxDocTemplate
*docTemplate
= GetDocumentTemplate();
270 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
271 wxString filter
= docTemplate
->GetDescription() + wxT(" (") + docTemplate
->GetFileFilter() + wxT(")|") + docTemplate
->GetFileFilter();
273 // Now see if there are some other template with identical view and document
274 // classes, whose filters may also be used.
276 if (docTemplate
->GetViewClassInfo() && docTemplate
->GetDocClassInfo())
278 wxList::compatibility_iterator node
= wxDocManager::GetDocumentManager()->GetTemplates().GetFirst();
281 wxDocTemplate
*t
= (wxDocTemplate
*) node
->GetData();
283 if (t
->IsVisible() && t
!= docTemplate
&&
284 t
->GetViewClassInfo() == docTemplate
->GetViewClassInfo() &&
285 t
->GetDocClassInfo() == docTemplate
->GetDocClassInfo())
287 // add a '|' to separate this filter from the previous one
288 if ( !filter
.empty() )
291 filter
<< t
->GetDescription() << wxT(" (") << t
->GetFileFilter() << wxT(") |")
292 << t
->GetFileFilter();
295 node
= node
->GetNext();
299 wxString filter
= docTemplate
->GetFileFilter() ;
301 wxString tmp
= wxFileSelector(_("Save as"),
302 docTemplate
->GetDirectory(),
303 wxFileNameFromPath(GetFilename()),
304 docTemplate
->GetDefaultExtension(),
306 wxSAVE
| wxOVERWRITE_PROMPT
,
307 GetDocumentWindow());
312 wxString
fileName(tmp
);
313 wxString path
, name
, ext
;
314 wxSplitPath(fileName
, & path
, & name
, & ext
);
318 fileName
+= wxT(".");
319 fileName
+= docTemplate
->GetDefaultExtension();
322 SetFilename(fileName
);
323 SetTitle(wxFileNameFromPath(fileName
));
325 // Notify the views that the filename has changed
326 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
329 wxView
*view
= (wxView
*)node
->GetData();
330 view
->OnChangeFilename();
331 node
= node
->GetNext();
334 // Files that were not saved correctly are not added to the FileHistory.
335 if (!OnSaveDocument(m_documentFile
))
338 // A file that doesn't use the default extension of its document template cannot be opened
339 // via the FileHistory, so we do not add it.
340 if (docTemplate
->FileMatchesTemplate(fileName
))
342 GetDocumentManager()->AddFileToHistory(fileName
);
346 // The user will probably not be able to open the file again, so
347 // we could warn about the wrong file-extension here.
352 bool wxDocument::OnSaveDocument(const wxString
& file
)
357 if ( !DoSaveDocument(file
) )
362 SetDocumentSaved(true);
364 wxFileName
fn(file
) ;
365 fn
.MacSetDefaultTypeAndCreator() ;
370 bool wxDocument::OnOpenDocument(const wxString
& file
)
372 if (!OnSaveModified())
375 if ( !DoOpenDocument(file
) )
378 SetFilename(file
, true);
387 #if wxUSE_STD_IOSTREAM
388 wxSTD istream
& wxDocument::LoadObject(wxSTD istream
& stream
)
390 wxInputStream
& wxDocument::LoadObject(wxInputStream
& stream
)
396 #if wxUSE_STD_IOSTREAM
397 wxSTD ostream
& wxDocument::SaveObject(wxSTD ostream
& stream
)
399 wxOutputStream
& wxDocument::SaveObject(wxOutputStream
& stream
)
405 bool wxDocument::Revert()
411 // Get title, or filename if no title, else unnamed
412 bool wxDocument::GetPrintableName(wxString
& buf
) const
414 if (!m_documentTitle
.empty())
416 buf
= m_documentTitle
;
419 else if (!m_documentFile
.empty())
421 buf
= wxFileNameFromPath(m_documentFile
);
431 wxWindow
*wxDocument::GetDocumentWindow() const
433 wxView
*view
= GetFirstView();
435 return view
->GetFrame();
437 return wxTheApp
->GetTopWindow();
440 wxCommandProcessor
*wxDocument::OnCreateCommandProcessor()
442 return new wxCommandProcessor
;
445 // true if safe to close
446 bool wxDocument::OnSaveModified()
451 GetPrintableName(title
);
454 if (!wxTheApp
->GetAppName().empty())
455 msgTitle
= wxTheApp
->GetAppName();
457 msgTitle
= wxString(_("Warning"));
460 prompt
.Printf(_("Do you want to save changes to document %s?"),
461 (const wxChar
*)title
);
462 int res
= wxMessageBox(prompt
, msgTitle
,
463 wxYES_NO
|wxCANCEL
|wxICON_QUESTION
,
464 GetDocumentWindow());
470 else if (res
== wxYES
)
472 else if (res
== wxCANCEL
)
478 bool wxDocument::Draw(wxDC
& WXUNUSED(context
))
483 bool wxDocument::AddView(wxView
*view
)
485 if (!m_documentViews
.Member(view
))
487 m_documentViews
.Append(view
);
493 bool wxDocument::RemoveView(wxView
*view
)
495 (void)m_documentViews
.DeleteObject(view
);
500 bool wxDocument::OnCreate(const wxString
& WXUNUSED(path
), long flags
)
502 if (GetDocumentTemplate()->CreateView(this, flags
))
508 // Called after a view is added or removed.
509 // The default implementation deletes the document if
510 // there are no more views.
511 void wxDocument::OnChangedViewList()
513 if (m_documentViews
.GetCount() == 0)
515 if (OnSaveModified())
522 void wxDocument::UpdateAllViews(wxView
*sender
, wxObject
*hint
)
524 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
527 wxView
*view
= (wxView
*)node
->GetData();
529 view
->OnUpdate(sender
, hint
);
530 node
= node
->GetNext();
534 void wxDocument::NotifyClosing()
536 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
539 wxView
*view
= (wxView
*)node
->GetData();
540 view
->OnClosingDocument();
541 node
= node
->GetNext();
545 void wxDocument::SetFilename(const wxString
& filename
, bool notifyViews
)
547 m_documentFile
= filename
;
550 // Notify the views that the filename has changed
551 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
554 wxView
*view
= (wxView
*)node
->GetData();
555 view
->OnChangeFilename();
556 node
= node
->GetNext();
561 bool wxDocument::DoSaveDocument(const wxString
& file
)
564 if (!wxTheApp
->GetAppName().empty())
565 msgTitle
= wxTheApp
->GetAppName();
567 msgTitle
= wxString(_("File error"));
569 #if wxUSE_STD_IOSTREAM
570 wxSTD ofstream
store(file
.mb_str(), wxSTD
ios::binary
);
571 if (store
.fail() || store
.bad())
573 wxFileOutputStream
store(file
);
574 if (store
.GetLastError() != wxSTREAM_NO_ERROR
)
577 (void)wxMessageBox(_("Sorry, could not open this file for saving."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
578 GetDocumentWindow());
582 if (!SaveObject(store
))
584 (void)wxMessageBox(_("Sorry, could not save this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
585 GetDocumentWindow());
593 bool wxDocument::DoOpenDocument(const wxString
& file
)
595 #if wxUSE_STD_IOSTREAM
596 wxSTD ifstream
store(file
.mb_str(), wxSTD
ios::binary
);
597 if (!store
.fail() && !store
.bad())
599 wxFileInputStream
store(file
);
600 if (store
.GetLastError() == wxSTREAM_NO_ERROR
)
603 #if wxUSE_STD_IOSTREAM
605 if ( !!store
|| store
.eof() )
607 int res
= LoadObject(store
).GetLastError();
608 if ( res
== wxSTREAM_NO_ERROR
|| res
== wxSTREAM_EOF
)
613 wxLogError(_("Sorry, could not open this file."));
618 // ----------------------------------------------------------------------------
620 // ----------------------------------------------------------------------------
624 m_viewDocument
= (wxDocument
*) NULL
;
626 m_viewFrame
= (wxFrame
*) NULL
;
631 GetDocumentManager()->ActivateView(this, false);
632 m_viewDocument
->RemoveView(this);
635 // Extend event processing to search the document's event table
636 bool wxView::ProcessEvent(wxEvent
& event
)
638 if ( !GetDocument() || !GetDocument()->ProcessEvent(event
) )
639 return wxEvtHandler::ProcessEvent(event
);
644 void wxView::OnActivateView(bool WXUNUSED(activate
), wxView
*WXUNUSED(activeView
), wxView
*WXUNUSED(deactiveView
))
648 void wxView::OnPrint(wxDC
*dc
, wxObject
*WXUNUSED(info
))
653 void wxView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
))
657 void wxView::OnChangeFilename()
659 if (GetFrame() && GetDocument())
663 GetDocument()->GetPrintableName(title
);
665 GetFrame()->SetTitle(title
);
669 void wxView::SetDocument(wxDocument
*doc
)
671 m_viewDocument
= doc
;
676 bool wxView::Close(bool deleteWindow
)
678 if (OnClose(deleteWindow
))
684 void wxView::Activate(bool activate
)
686 if (GetDocument() && GetDocumentManager())
688 OnActivateView(activate
, this, GetDocumentManager()->GetCurrentView());
689 GetDocumentManager()->ActivateView(this, activate
);
693 bool wxView::OnClose(bool WXUNUSED(deleteWindow
))
695 return GetDocument() ? GetDocument()->Close() : true;
698 #if wxUSE_PRINTING_ARCHITECTURE
699 wxPrintout
*wxView::OnCreatePrintout()
701 return new wxDocPrintout(this);
703 #endif // wxUSE_PRINTING_ARCHITECTURE
705 // ----------------------------------------------------------------------------
707 // ----------------------------------------------------------------------------
709 wxDocTemplate::wxDocTemplate(wxDocManager
*manager
,
710 const wxString
& descr
,
711 const wxString
& filter
,
714 const wxString
& docTypeName
,
715 const wxString
& viewTypeName
,
716 wxClassInfo
*docClassInfo
,
717 wxClassInfo
*viewClassInfo
,
720 m_documentManager
= manager
;
721 m_description
= descr
;
724 m_fileFilter
= filter
;
726 m_docTypeName
= docTypeName
;
727 m_viewTypeName
= viewTypeName
;
728 m_documentManager
->AssociateTemplate(this);
730 m_docClassInfo
= docClassInfo
;
731 m_viewClassInfo
= viewClassInfo
;
734 wxDocTemplate::~wxDocTemplate()
736 m_documentManager
->DisassociateTemplate(this);
739 // Tries to dynamically construct an object of the right class.
740 wxDocument
*wxDocTemplate::CreateDocument(const wxString
& path
, long flags
)
742 wxDocument
*doc
= DoCreateDocument();
744 return (wxDocument
*) NULL
;
746 if (InitDocument(doc
, path
, flags
))
752 return (wxDocument
*) NULL
;
756 bool wxDocTemplate::InitDocument(wxDocument
* doc
, const wxString
& path
, long flags
)
758 doc
->SetFilename(path
);
759 doc
->SetDocumentTemplate(this);
760 GetDocumentManager()->AddDocument(doc
);
761 doc
->SetCommandProcessor(doc
->OnCreateCommandProcessor());
763 if (doc
->OnCreate(path
, flags
))
767 if (GetDocumentManager()->GetDocuments().Member(doc
))
768 doc
->DeleteAllViews();
773 wxView
*wxDocTemplate::CreateView(wxDocument
*doc
, long flags
)
775 wxView
*view
= DoCreateView();
777 return (wxView
*) NULL
;
779 view
->SetDocument(doc
);
780 if (view
->OnCreate(doc
, flags
))
787 return (wxView
*) NULL
;
791 // The default (very primitive) format detection: check is the extension is
792 // that of the template
793 bool wxDocTemplate::FileMatchesTemplate(const wxString
& path
)
795 return GetDefaultExtension().IsSameAs(FindExtension(path
));
798 wxDocument
*wxDocTemplate::DoCreateDocument()
801 return (wxDocument
*) NULL
;
803 return (wxDocument
*)m_docClassInfo
->CreateObject();
806 wxView
*wxDocTemplate::DoCreateView()
808 if (!m_viewClassInfo
)
809 return (wxView
*) NULL
;
811 return (wxView
*)m_viewClassInfo
->CreateObject();
814 // ----------------------------------------------------------------------------
816 // ----------------------------------------------------------------------------
818 BEGIN_EVENT_TABLE(wxDocManager
, wxEvtHandler
)
819 EVT_MENU(wxID_OPEN
, wxDocManager::OnFileOpen
)
820 EVT_MENU(wxID_CLOSE
, wxDocManager::OnFileClose
)
821 EVT_MENU(wxID_CLOSE_ALL
, wxDocManager::OnFileCloseAll
)
822 EVT_MENU(wxID_REVERT
, wxDocManager::OnFileRevert
)
823 EVT_MENU(wxID_NEW
, wxDocManager::OnFileNew
)
824 EVT_MENU(wxID_SAVE
, wxDocManager::OnFileSave
)
825 EVT_MENU(wxID_SAVEAS
, wxDocManager::OnFileSaveAs
)
826 EVT_MENU(wxID_UNDO
, wxDocManager::OnUndo
)
827 EVT_MENU(wxID_REDO
, wxDocManager::OnRedo
)
829 EVT_UPDATE_UI(wxID_OPEN
, wxDocManager::OnUpdateFileOpen
)
830 EVT_UPDATE_UI(wxID_CLOSE
, wxDocManager::OnUpdateFileClose
)
831 EVT_UPDATE_UI(wxID_CLOSE_ALL
, wxDocManager::OnUpdateFileClose
)
832 EVT_UPDATE_UI(wxID_REVERT
, wxDocManager::OnUpdateFileRevert
)
833 EVT_UPDATE_UI(wxID_NEW
, wxDocManager::OnUpdateFileNew
)
834 EVT_UPDATE_UI(wxID_SAVE
, wxDocManager::OnUpdateFileSave
)
835 EVT_UPDATE_UI(wxID_SAVEAS
, wxDocManager::OnUpdateFileSaveAs
)
836 EVT_UPDATE_UI(wxID_UNDO
, wxDocManager::OnUpdateUndo
)
837 EVT_UPDATE_UI(wxID_REDO
, wxDocManager::OnUpdateRedo
)
839 #if wxUSE_PRINTING_ARCHITECTURE
840 EVT_MENU(wxID_PRINT
, wxDocManager::OnPrint
)
841 EVT_MENU(wxID_PREVIEW
, wxDocManager::OnPreview
)
843 EVT_UPDATE_UI(wxID_PRINT
, wxDocManager::OnUpdatePrint
)
844 EVT_UPDATE_UI(wxID_PREVIEW
, wxDocManager::OnUpdatePreview
)
848 wxDocManager
* wxDocManager::sm_docManager
= (wxDocManager
*) NULL
;
850 wxDocManager::wxDocManager(long flags
, bool initialize
)
852 m_defaultDocumentNameCounter
= 1;
854 m_currentView
= (wxView
*) NULL
;
855 m_maxDocsOpen
= 10000;
856 m_fileHistory
= (wxFileHistory
*) NULL
;
859 sm_docManager
= this;
862 wxDocManager::~wxDocManager()
866 delete m_fileHistory
;
867 sm_docManager
= (wxDocManager
*) NULL
;
870 // closes the specified document
871 bool wxDocManager::CloseDocument(wxDocument
* doc
, bool force
)
873 if (doc
->Close() || force
)
875 // Implicitly deletes the document when
876 // the last view is deleted
877 doc
->DeleteAllViews();
879 // Check we're really deleted
880 if (m_docs
.Member(doc
))
888 bool wxDocManager::CloseDocuments(bool force
)
890 wxList::compatibility_iterator node
= m_docs
.GetFirst();
893 wxDocument
*doc
= (wxDocument
*)node
->GetData();
894 wxList::compatibility_iterator next
= node
->GetNext();
896 if (!CloseDocument(doc
, force
))
899 // This assumes that documents are not connected in
900 // any way, i.e. deleting one document does NOT
907 bool wxDocManager::Clear(bool force
)
909 if (!CloseDocuments(force
))
912 m_currentView
= NULL
;
914 wxList::compatibility_iterator node
= m_templates
.GetFirst();
917 wxDocTemplate
*templ
= (wxDocTemplate
*) node
->GetData();
918 wxList::compatibility_iterator next
= node
->GetNext();
925 bool wxDocManager::Initialize()
927 m_fileHistory
= OnCreateFileHistory();
931 wxFileHistory
*wxDocManager::OnCreateFileHistory()
933 return new wxFileHistory
;
936 void wxDocManager::OnFileClose(wxCommandEvent
& WXUNUSED(event
))
938 wxDocument
*doc
= GetCurrentDocument();
943 doc
->DeleteAllViews();
944 if (m_docs
.Member(doc
))
949 void wxDocManager::OnFileCloseAll(wxCommandEvent
& WXUNUSED(event
))
951 CloseDocuments(false);
954 void wxDocManager::OnFileNew(wxCommandEvent
& WXUNUSED(event
))
956 CreateDocument( wxEmptyString
, wxDOC_NEW
);
959 void wxDocManager::OnFileOpen(wxCommandEvent
& WXUNUSED(event
))
961 if ( !CreateDocument( wxEmptyString
, 0) )
967 void wxDocManager::OnFileRevert(wxCommandEvent
& WXUNUSED(event
))
969 wxDocument
*doc
= GetCurrentDocument();
975 void wxDocManager::OnFileSave(wxCommandEvent
& WXUNUSED(event
))
977 wxDocument
*doc
= GetCurrentDocument();
983 void wxDocManager::OnFileSaveAs(wxCommandEvent
& WXUNUSED(event
))
985 wxDocument
*doc
= GetCurrentDocument();
991 void wxDocManager::OnPrint(wxCommandEvent
& WXUNUSED(event
))
993 #if wxUSE_PRINTING_ARCHITECTURE
994 wxView
*view
= GetCurrentView();
998 wxPrintout
*printout
= view
->OnCreatePrintout();
1002 printer
.Print(view
->GetFrame(), printout
, true);
1006 #endif // wxUSE_PRINTING_ARCHITECTURE
1009 void wxDocManager::OnPreview(wxCommandEvent
& WXUNUSED(event
))
1011 #if wxUSE_PRINTING_ARCHITECTURE
1012 wxView
*view
= GetCurrentView();
1016 wxPrintout
*printout
= view
->OnCreatePrintout();
1019 // Pass two printout objects: for preview, and possible printing.
1020 wxPrintPreviewBase
*preview
= new wxPrintPreview(printout
, view
->OnCreatePrintout());
1021 if ( !preview
->Ok() )
1024 wxMessageBox( _("Sorry, print preview needs a printer to be installed.") );
1028 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, (wxFrame
*)wxTheApp
->GetTopWindow(), _("Print Preview"),
1029 wxPoint(100, 100), wxSize(600, 650));
1030 frame
->Centre(wxBOTH
);
1031 frame
->Initialize();
1034 #endif // wxUSE_PRINTING_ARCHITECTURE
1037 void wxDocManager::OnUndo(wxCommandEvent
& event
)
1039 wxDocument
*doc
= GetCurrentDocument();
1042 if (doc
->GetCommandProcessor())
1043 doc
->GetCommandProcessor()->Undo();
1048 void wxDocManager::OnRedo(wxCommandEvent
& event
)
1050 wxDocument
*doc
= GetCurrentDocument();
1053 if (doc
->GetCommandProcessor())
1054 doc
->GetCommandProcessor()->Redo();
1059 // Handlers for UI update commands
1061 void wxDocManager::OnUpdateFileOpen(wxUpdateUIEvent
& event
)
1063 event
.Enable( true );
1066 void wxDocManager::OnUpdateFileClose(wxUpdateUIEvent
& event
)
1068 wxDocument
*doc
= GetCurrentDocument();
1069 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1072 void wxDocManager::OnUpdateFileRevert(wxUpdateUIEvent
& event
)
1074 wxDocument
*doc
= GetCurrentDocument();
1075 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1078 void wxDocManager::OnUpdateFileNew(wxUpdateUIEvent
& event
)
1080 event
.Enable( true );
1083 void wxDocManager::OnUpdateFileSave(wxUpdateUIEvent
& event
)
1085 wxDocument
*doc
= GetCurrentDocument();
1086 event
.Enable( doc
&& doc
->IsModified() );
1089 void wxDocManager::OnUpdateFileSaveAs(wxUpdateUIEvent
& event
)
1091 wxDocument
*doc
= GetCurrentDocument();
1092 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1095 void wxDocManager::OnUpdateUndo(wxUpdateUIEvent
& event
)
1097 wxDocument
*doc
= GetCurrentDocument();
1099 event
.Enable(false);
1100 else if (!doc
->GetCommandProcessor())
1104 event
.Enable( doc
->GetCommandProcessor()->CanUndo() );
1105 doc
->GetCommandProcessor()->SetMenuStrings();
1109 void wxDocManager::OnUpdateRedo(wxUpdateUIEvent
& event
)
1111 wxDocument
*doc
= GetCurrentDocument();
1113 event
.Enable(false);
1114 else if (!doc
->GetCommandProcessor())
1118 event
.Enable( doc
->GetCommandProcessor()->CanRedo() );
1119 doc
->GetCommandProcessor()->SetMenuStrings();
1123 void wxDocManager::OnUpdatePrint(wxUpdateUIEvent
& event
)
1125 wxDocument
*doc
= GetCurrentDocument();
1126 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1129 void wxDocManager::OnUpdatePreview(wxUpdateUIEvent
& event
)
1131 wxDocument
*doc
= GetCurrentDocument();
1132 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1135 wxView
*wxDocManager::GetCurrentView() const
1138 return m_currentView
;
1139 if (m_docs
.GetCount() == 1)
1141 wxDocument
* doc
= (wxDocument
*) m_docs
.GetFirst()->GetData();
1142 return doc
->GetFirstView();
1144 return (wxView
*) NULL
;
1147 // Extend event processing to search the view's event table
1148 bool wxDocManager::ProcessEvent(wxEvent
& event
)
1150 wxView
* view
= GetCurrentView();
1153 if (view
->ProcessEvent(event
))
1156 return wxEvtHandler::ProcessEvent(event
);
1159 wxDocument
*wxDocManager::CreateDocument(const wxString
& path
, long flags
)
1161 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.GetCount()];
1164 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1166 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Item(i
)->GetData());
1167 if (temp
->IsVisible())
1169 templates
[n
] = temp
;
1176 return (wxDocument
*) NULL
;
1179 wxDocument
* docToClose
= NULL
;
1181 // If we've reached the max number of docs, close the
1183 if ( (int)GetDocuments().GetCount() >= m_maxDocsOpen
)
1185 wxDocument
*doc
= (wxDocument
*)GetDocuments().GetFirst()->GetData();
1189 // New document: user chooses a template, unless there's only one.
1190 if (flags
& wxDOC_NEW
)
1196 if (!CloseDocument(docToClose
, false))
1203 wxDocTemplate
*temp
= templates
[0];
1205 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
1209 newDoc
->SetDocumentName(temp
->GetDocumentName());
1210 newDoc
->SetDocumentTemplate(temp
);
1211 if (!newDoc
->OnNewDocument() )
1213 // Document is implicitly deleted by DeleteAllViews
1214 newDoc
->DeleteAllViews();
1221 wxDocTemplate
*temp
= SelectDocumentType(templates
, n
);
1227 if (!CloseDocument(docToClose
, false))
1233 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
1237 newDoc
->SetDocumentName(temp
->GetDocumentName());
1238 newDoc
->SetDocumentTemplate(temp
);
1239 if (!newDoc
->OnNewDocument() )
1241 // Document is implicitly deleted by DeleteAllViews
1242 newDoc
->DeleteAllViews();
1249 return (wxDocument
*) NULL
;
1252 // Existing document
1253 wxDocTemplate
*temp
;
1255 wxString path2
= path
;
1257 if (flags
& wxDOC_SILENT
)
1259 temp
= FindTemplateForPath(path2
);
1262 // Since we do not add files with non-default extensions to the FileHistory this
1263 // can only happen if the application changes the allowed templates in runtime.
1264 (void)wxMessageBox(_("Sorry, the format for this file is unknown."),
1266 wxOK
| wxICON_EXCLAMATION
, wxFindSuitableParent());
1270 temp
= SelectDocumentPath(templates
, n
, path2
, flags
);
1278 if (!CloseDocument(docToClose
, false))
1284 //see if this file is already open
1285 for (size_t i
= 0; i
< GetDocuments().GetCount(); ++i
)
1287 wxDocument
* currentDoc
= (wxDocument
*)(GetDocuments().Item(i
)->GetData());
1289 //file paths are case-insensitive on Windows
1290 if (path2
.CmpNoCase(currentDoc
->GetFilename()) == 0)
1292 if (path2
.Cmp(currentDoc
->GetFilename()) == 0)
1295 //file already open. Just activate it and return
1296 if (currentDoc
->GetFirstView())
1298 ActivateView(currentDoc
->GetFirstView(), true);
1299 if (currentDoc
->GetDocumentWindow())
1300 currentDoc
->GetDocumentWindow()->SetFocus();
1306 wxDocument
*newDoc
= temp
->CreateDocument(path2
, flags
);
1309 newDoc
->SetDocumentName(temp
->GetDocumentName());
1310 newDoc
->SetDocumentTemplate(temp
);
1311 if (!newDoc
->OnOpenDocument(path2
))
1313 newDoc
->DeleteAllViews();
1314 // delete newDoc; // Implicitly deleted by DeleteAllViews
1315 return (wxDocument
*) NULL
;
1317 // A file that doesn't use the default extension of its document
1318 // template cannot be opened via the FileHistory, so we do not
1320 if (temp
->FileMatchesTemplate(path2
))
1321 AddFileToHistory(path2
);
1326 return (wxDocument
*) NULL
;
1329 wxView
*wxDocManager::CreateView(wxDocument
*doc
, long flags
)
1331 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.GetCount()];
1334 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1336 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Item(i
)->GetData());
1337 if (temp
->IsVisible())
1339 if (temp
->GetDocumentName() == doc
->GetDocumentName())
1341 templates
[n
] = temp
;
1349 return (wxView
*) NULL
;
1353 wxDocTemplate
*temp
= templates
[0];
1355 wxView
*view
= temp
->CreateView(doc
, flags
);
1357 view
->SetViewName(temp
->GetViewName());
1361 wxDocTemplate
*temp
= SelectViewType(templates
, n
);
1365 wxView
*view
= temp
->CreateView(doc
, flags
);
1367 view
->SetViewName(temp
->GetViewName());
1371 return (wxView
*) NULL
;
1374 // Not yet implemented
1375 void wxDocManager::DeleteTemplate(wxDocTemplate
*WXUNUSED(temp
), long WXUNUSED(flags
))
1379 // Not yet implemented
1380 bool wxDocManager::FlushDoc(wxDocument
*WXUNUSED(doc
))
1385 wxDocument
*wxDocManager::GetCurrentDocument() const
1387 wxView
*view
= GetCurrentView();
1389 return view
->GetDocument();
1391 return (wxDocument
*) NULL
;
1394 // Make a default document name
1395 bool wxDocManager::MakeDefaultName(wxString
& name
)
1397 name
.Printf(_("unnamed%d"), m_defaultDocumentNameCounter
);
1398 m_defaultDocumentNameCounter
++;
1403 // Make a frame title (override this to do something different)
1404 // If docName is empty, a document is not currently active.
1405 wxString
wxDocManager::MakeFrameTitle(wxDocument
* doc
)
1407 wxString appName
= wxTheApp
->GetAppName();
1414 doc
->GetPrintableName(docName
);
1415 title
= docName
+ wxString(_(" - ")) + appName
;
1421 // Not yet implemented
1422 wxDocTemplate
*wxDocManager::MatchTemplate(const wxString
& WXUNUSED(path
))
1424 return (wxDocTemplate
*) NULL
;
1427 // File history management
1428 void wxDocManager::AddFileToHistory(const wxString
& file
)
1431 m_fileHistory
->AddFileToHistory(file
);
1434 void wxDocManager::RemoveFileFromHistory(size_t i
)
1437 m_fileHistory
->RemoveFileFromHistory(i
);
1440 wxString
wxDocManager::GetHistoryFile(size_t i
) const
1445 histFile
= m_fileHistory
->GetHistoryFile(i
);
1450 void wxDocManager::FileHistoryUseMenu(wxMenu
*menu
)
1453 m_fileHistory
->UseMenu(menu
);
1456 void wxDocManager::FileHistoryRemoveMenu(wxMenu
*menu
)
1459 m_fileHistory
->RemoveMenu(menu
);
1463 void wxDocManager::FileHistoryLoad(wxConfigBase
& config
)
1466 m_fileHistory
->Load(config
);
1469 void wxDocManager::FileHistorySave(wxConfigBase
& config
)
1472 m_fileHistory
->Save(config
);
1476 void wxDocManager::FileHistoryAddFilesToMenu(wxMenu
* menu
)
1479 m_fileHistory
->AddFilesToMenu(menu
);
1482 void wxDocManager::FileHistoryAddFilesToMenu()
1485 m_fileHistory
->AddFilesToMenu();
1488 size_t wxDocManager::GetHistoryFilesCount() const
1490 return m_fileHistory
? m_fileHistory
->GetCount() : 0;
1494 // Find out the document template via matching in the document file format
1495 // against that of the template
1496 wxDocTemplate
*wxDocManager::FindTemplateForPath(const wxString
& path
)
1498 wxDocTemplate
*theTemplate
= (wxDocTemplate
*) NULL
;
1500 // Find the template which this extension corresponds to
1501 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1503 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Item(i
)->GetData();
1504 if ( temp
->FileMatchesTemplate(path
) )
1513 // Try to get a more suitable parent frame than the top window,
1514 // for selection dialogs. Otherwise you may get an unexpected
1515 // window being activated when a dialog is shown.
1516 static wxWindow
* wxFindSuitableParent()
1518 wxWindow
* parent
= wxTheApp
->GetTopWindow();
1520 wxWindow
* focusWindow
= wxWindow::FindFocus();
1523 while (focusWindow
&&
1524 !focusWindow
->IsKindOf(CLASSINFO(wxDialog
)) &&
1525 !focusWindow
->IsKindOf(CLASSINFO(wxFrame
)))
1527 focusWindow
= focusWindow
->GetParent();
1530 parent
= focusWindow
;
1535 // Prompts user to open a file, using file specs in templates.
1536 // Must extend the file selector dialog or implement own; OR
1537 // match the extension to the template extension.
1539 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**templates
,
1540 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
1543 int WXUNUSED(noTemplates
),
1546 long WXUNUSED(flags
),
1547 bool WXUNUSED(save
))
1549 // We can only have multiple filters in Windows and GTK
1550 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
1554 for (i
= 0; i
< noTemplates
; i
++)
1556 if (templates
[i
]->IsVisible())
1558 // add a '|' to separate this filter from the previous one
1559 if ( !descrBuf
.empty() )
1560 descrBuf
<< wxT('|');
1562 descrBuf
<< templates
[i
]->GetDescription()
1563 << wxT(" (") << templates
[i
]->GetFileFilter() << wxT(") |")
1564 << templates
[i
]->GetFileFilter();
1568 wxString descrBuf
= wxT("*.*");
1571 int FilterIndex
= -1;
1573 wxWindow
* parent
= wxFindSuitableParent();
1575 wxString pathTmp
= wxFileSelectorEx(_("Select a file"),
1583 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)NULL
;
1584 if (!pathTmp
.empty())
1586 if (!wxFileExists(pathTmp
))
1589 if (!wxTheApp
->GetAppName().empty())
1590 msgTitle
= wxTheApp
->GetAppName();
1592 msgTitle
= wxString(_("File error"));
1594 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
1597 path
= wxEmptyString
;
1598 return (wxDocTemplate
*) NULL
;
1600 m_lastDirectory
= wxPathOnly(pathTmp
);
1604 // first choose the template using the extension, if this fails (i.e.
1605 // wxFileSelectorEx() didn't fill it), then use the path
1606 if ( FilterIndex
!= -1 )
1607 theTemplate
= templates
[FilterIndex
];
1609 theTemplate
= FindTemplateForPath(path
);
1612 // Since we do not add files with non-default extensions to the FileHistory this
1613 // can only happen if the application changes the allowed templates in runtime.
1614 (void)wxMessageBox(_("Sorry, the format for this file is unknown."),
1616 wxOK
| wxICON_EXCLAMATION
, wxFindSuitableParent());
1621 path
= wxEmptyString
;
1627 wxDocTemplate
*wxDocManager::SelectDocumentType(wxDocTemplate
**templates
,
1628 int noTemplates
, bool sort
)
1630 wxArrayString strings
;
1631 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1635 for (i
= 0; i
< noTemplates
; i
++)
1637 if (templates
[i
]->IsVisible())
1641 for (j
= 0; j
< n
; j
++)
1643 //filter out NOT unique documents + view combinations
1644 if ( templates
[i
]->m_docTypeName
== data
[j
]->m_docTypeName
&&
1645 templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
1652 strings
.Add(templates
[i
]->m_description
);
1654 data
[n
] = templates
[i
];
1662 strings
.Sort(); // ascending sort
1663 // Yes, this will be slow, but template lists
1664 // are typically short.
1666 n
= strings
.Count();
1667 for (i
= 0; i
< n
; i
++)
1669 for (j
= 0; j
< noTemplates
; j
++)
1671 if (strings
[i
] == templates
[j
]->m_description
)
1672 data
[i
] = templates
[j
];
1677 wxDocTemplate
*theTemplate
;
1682 // no visible templates, hence nothing to choose from
1687 // don't propose the user to choose if he heas no choice
1688 theTemplate
= data
[0];
1692 // propose the user to choose one of several
1693 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1695 _("Select a document template"),
1699 wxFindSuitableParent()
1708 wxDocTemplate
*wxDocManager::SelectViewType(wxDocTemplate
**templates
,
1709 int noTemplates
, bool sort
)
1711 wxArrayString strings
;
1712 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1716 for (i
= 0; i
< noTemplates
; i
++)
1718 wxDocTemplate
*templ
= templates
[i
];
1719 if ( templ
->IsVisible() && !templ
->GetViewName().empty() )
1723 for (j
= 0; j
< n
; j
++)
1725 //filter out NOT unique views
1726 if ( templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
)
1732 strings
.Add(templ
->m_viewTypeName
);
1741 strings
.Sort(); // ascending sort
1742 // Yes, this will be slow, but template lists
1743 // are typically short.
1745 n
= strings
.Count();
1746 for (i
= 0; i
< n
; i
++)
1748 for (j
= 0; j
< noTemplates
; j
++)
1750 if (strings
[i
] == templates
[j
]->m_viewTypeName
)
1751 data
[i
] = templates
[j
];
1756 wxDocTemplate
*theTemplate
;
1758 // the same logic as above
1762 theTemplate
= (wxDocTemplate
*)NULL
;
1766 theTemplate
= data
[0];
1770 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1772 _("Select a document view"),
1776 wxFindSuitableParent()
1785 void wxDocManager::AssociateTemplate(wxDocTemplate
*temp
)
1787 if (!m_templates
.Member(temp
))
1788 m_templates
.Append(temp
);
1791 void wxDocManager::DisassociateTemplate(wxDocTemplate
*temp
)
1793 m_templates
.DeleteObject(temp
);
1796 // Add and remove a document from the manager's list
1797 void wxDocManager::AddDocument(wxDocument
*doc
)
1799 if (!m_docs
.Member(doc
))
1803 void wxDocManager::RemoveDocument(wxDocument
*doc
)
1805 m_docs
.DeleteObject(doc
);
1808 // Views or windows should inform the document manager
1809 // when a view is going in or out of focus
1810 void wxDocManager::ActivateView(wxView
*view
, bool activate
)
1814 m_currentView
= view
;
1818 if ( m_currentView
== view
)
1820 // don't keep stale pointer
1821 m_currentView
= (wxView
*) NULL
;
1826 // ----------------------------------------------------------------------------
1827 // Default document child frame
1828 // ----------------------------------------------------------------------------
1830 BEGIN_EVENT_TABLE(wxDocChildFrame
, wxFrame
)
1831 EVT_ACTIVATE(wxDocChildFrame::OnActivate
)
1832 EVT_CLOSE(wxDocChildFrame::OnCloseWindow
)
1835 wxDocChildFrame::wxDocChildFrame(wxDocument
*doc
,
1839 const wxString
& title
,
1843 const wxString
& name
)
1844 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1846 m_childDocument
= doc
;
1849 view
->SetFrame(this);
1852 // Extend event processing to search the view's event table
1853 bool wxDocChildFrame::ProcessEvent(wxEvent
& event
)
1856 m_childView
->Activate(true);
1858 if ( !m_childView
|| ! m_childView
->ProcessEvent(event
) )
1860 // Only hand up to the parent if it's a menu command
1861 if (!event
.IsKindOf(CLASSINFO(wxCommandEvent
)) || !GetParent() || !GetParent()->ProcessEvent(event
))
1862 return wxEvtHandler::ProcessEvent(event
);
1870 void wxDocChildFrame::OnActivate(wxActivateEvent
& event
)
1872 wxFrame::OnActivate(event
);
1875 m_childView
->Activate(event
.GetActive());
1878 void wxDocChildFrame::OnCloseWindow(wxCloseEvent
& event
)
1882 bool ans
= event
.CanVeto()
1883 ? m_childView
->Close(false) // false means don't delete associated window
1884 : true; // Must delete.
1888 m_childView
->Activate(false);
1890 m_childView
= (wxView
*) NULL
;
1891 m_childDocument
= (wxDocument
*) NULL
;
1902 // ----------------------------------------------------------------------------
1903 // Default parent frame
1904 // ----------------------------------------------------------------------------
1906 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1907 EVT_MENU(wxID_EXIT
, wxDocParentFrame::OnExit
)
1908 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE9
, wxDocParentFrame::OnMRUFile
)
1909 EVT_CLOSE(wxDocParentFrame::OnCloseWindow
)
1912 wxDocParentFrame::wxDocParentFrame(wxDocManager
*manager
,
1915 const wxString
& title
,
1919 const wxString
& name
)
1920 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1922 m_docManager
= manager
;
1925 void wxDocParentFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
1930 void wxDocParentFrame::OnMRUFile(wxCommandEvent
& event
)
1932 int n
= event
.GetId() - wxID_FILE1
; // the index in MRU list
1933 wxString
filename(m_docManager
->GetHistoryFile(n
));
1934 if ( !filename
.empty() )
1936 // verify that the file exists before doing anything else
1937 if ( wxFile::Exists(filename
) )
1940 if (!m_docManager
->CreateDocument(filename
, wxDOC_SILENT
))
1942 // remove the file from the MRU list. The user should already be notified.
1943 m_docManager
->RemoveFileFromHistory(n
);
1945 wxLogError(_("The file '%s' couldn't be opened.\nIt has been removed from the most recently used files list."),
1951 // remove the bogus filename from the MRU list and notify the user
1953 m_docManager
->RemoveFileFromHistory(n
);
1955 wxLogError(_("The file '%s' doesn't exist and couldn't be opened.\nIt has been removed from the most recently used files list."),
1961 // Extend event processing to search the view's event table
1962 bool wxDocParentFrame::ProcessEvent(wxEvent
& event
)
1964 // Try the document manager, then do default processing
1965 if (!m_docManager
|| !m_docManager
->ProcessEvent(event
))
1966 return wxEvtHandler::ProcessEvent(event
);
1971 // Define the behaviour for the frame closing
1972 // - must delete all frames except for the main one.
1973 void wxDocParentFrame::OnCloseWindow(wxCloseEvent
& event
)
1975 if (m_docManager
->Clear(!event
.CanVeto()))
1983 #if wxUSE_PRINTING_ARCHITECTURE
1985 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
)
1988 m_printoutView
= view
;
1991 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
1995 // Get the logical pixels per inch of screen and printer
1996 int ppiScreenX
, ppiScreenY
;
1997 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
1998 wxUnusedVar(ppiScreenY
);
1999 int ppiPrinterX
, ppiPrinterY
;
2000 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
2001 wxUnusedVar(ppiPrinterY
);
2003 // This scales the DC so that the printout roughly represents the
2004 // the screen scaling. The text point size _should_ be the right size
2005 // but in fact is too small for some reason. This is a detail that will
2006 // need to be addressed at some point but can be fudged for the
2008 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
2010 // Now we have to check in case our real page size is reduced
2011 // (e.g. because we're drawing to a print preview memory DC)
2012 int pageWidth
, pageHeight
;
2014 dc
->GetSize(&w
, &h
);
2015 GetPageSizePixels(&pageWidth
, &pageHeight
);
2016 wxUnusedVar(pageHeight
);
2018 // If printer pageWidth == current DC width, then this doesn't
2019 // change. But w might be the preview bitmap width, so scale down.
2020 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
2021 dc
->SetUserScale(overallScale
, overallScale
);
2025 m_printoutView
->OnDraw(dc
);
2030 bool wxDocPrintout::HasPage(int pageNum
)
2032 return (pageNum
== 1);
2035 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
2037 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
2043 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
2051 #endif // wxUSE_PRINTING_ARCHITECTURE
2053 // ----------------------------------------------------------------------------
2054 // File history processor
2055 // ----------------------------------------------------------------------------
2057 static inline wxChar
* MYcopystring(const wxString
& s
)
2059 wxChar
* copy
= new wxChar
[s
.length() + 1];
2060 return wxStrcpy(copy
, s
.c_str());
2063 static inline wxChar
* MYcopystring(const wxChar
* s
)
2065 wxChar
* copy
= new wxChar
[wxStrlen(s
) + 1];
2066 return wxStrcpy(copy
, s
);
2069 wxFileHistory::wxFileHistory(size_t maxFiles
, wxWindowID idBase
)
2071 m_fileMaxFiles
= maxFiles
;
2074 m_fileHistory
= new wxChar
*[m_fileMaxFiles
];
2077 wxFileHistory::~wxFileHistory()
2080 for (i
= 0; i
< m_fileHistoryN
; i
++)
2081 delete[] m_fileHistory
[i
];
2082 delete[] m_fileHistory
;
2085 // File history management
2086 void wxFileHistory::AddFileToHistory(const wxString
& file
)
2090 // Check we don't already have this file
2091 for (i
= 0; i
< m_fileHistoryN
; i
++)
2093 #if defined( __WXMSW__ ) // Add any other OSes with case insensitive file names
2094 wxString testString
;
2095 if ( m_fileHistory
[i
] )
2096 testString
= m_fileHistory
[i
];
2097 if ( m_fileHistory
[i
] && ( file
.Lower() == testString
.Lower() ) )
2099 if ( m_fileHistory
[i
] && ( file
== m_fileHistory
[i
] ) )
2102 // we do have it, move it to the top of the history
2103 RemoveFileFromHistory (i
);
2104 AddFileToHistory (file
);
2109 // if we already have a full history, delete the one at the end
2110 if ( m_fileMaxFiles
== m_fileHistoryN
)
2112 RemoveFileFromHistory (m_fileHistoryN
- 1);
2113 AddFileToHistory (file
);
2117 // Add to the project file history:
2118 // Move existing files (if any) down so we can insert file at beginning.
2119 if (m_fileHistoryN
< m_fileMaxFiles
)
2121 wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2124 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2125 if ( m_fileHistoryN
== 0 && menu
->GetMenuItemCount() )
2127 menu
->AppendSeparator();
2129 menu
->Append(m_idBase
+m_fileHistoryN
, _("[EMPTY]"));
2130 node
= node
->GetNext();
2134 // Shuffle filenames down
2135 for (i
= (m_fileHistoryN
-1); i
> 0; i
--)
2137 m_fileHistory
[i
] = m_fileHistory
[i
-1];
2139 m_fileHistory
[0] = MYcopystring(file
);
2141 // this is the directory of the last opened file
2142 wxString pathCurrent
;
2143 wxSplitPath( m_fileHistory
[0], &pathCurrent
, NULL
, NULL
);
2144 for (i
= 0; i
< m_fileHistoryN
; i
++)
2146 if ( m_fileHistory
[i
] )
2148 // if in same directory just show the filename; otherwise the full
2150 wxString pathInMenu
, path
, filename
, ext
;
2151 wxSplitPath( m_fileHistory
[i
], &path
, &filename
, &ext
);
2152 if ( path
== pathCurrent
)
2154 pathInMenu
= filename
;
2156 pathInMenu
= pathInMenu
+ wxFILE_SEP_EXT
+ ext
;
2160 // absolute path; could also set relative path
2161 pathInMenu
= m_fileHistory
[i
];
2164 // we need to quote '&' characters which are used for mnemonics
2165 pathInMenu
.Replace(_T("&"), _T("&&"));
2167 buf
.Printf(s_MRUEntryFormat
, i
+ 1, pathInMenu
.c_str());
2168 wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2171 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2172 menu
->SetLabel(m_idBase
+ i
, buf
);
2173 node
= node
->GetNext();
2179 void wxFileHistory::RemoveFileFromHistory(size_t i
)
2181 wxCHECK_RET( i
< m_fileHistoryN
,
2182 wxT("invalid index in wxFileHistory::RemoveFileFromHistory") );
2184 // delete the element from the array (could use memmove() too...)
2185 delete [] m_fileHistory
[i
];
2188 for ( j
= i
; j
< m_fileHistoryN
- 1; j
++ )
2190 m_fileHistory
[j
] = m_fileHistory
[j
+ 1];
2193 wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2196 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2198 // shuffle filenames up
2200 for ( j
= i
; j
< m_fileHistoryN
- 1; j
++ )
2202 buf
.Printf(s_MRUEntryFormat
, j
+ 1, m_fileHistory
[j
]);
2203 menu
->SetLabel(m_idBase
+ j
, buf
);
2206 node
= node
->GetNext();
2208 // delete the last menu item which is unused now
2209 wxWindowID lastItemId
= m_idBase
+ m_fileHistoryN
- 1;
2210 if (menu
->FindItem(lastItemId
))
2212 menu
->Delete(lastItemId
);
2215 // delete the last separator too if no more files are left
2216 if ( m_fileHistoryN
== 1 )
2218 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetLast();
2221 wxMenuItem
*menuItem
= node
->GetData();
2222 if ( menuItem
->IsSeparator() )
2224 menu
->Delete(menuItem
);
2226 //else: should we search backwards for the last separator?
2228 //else: menu is empty somehow
2235 wxString
wxFileHistory::GetHistoryFile(size_t i
) const
2238 if ( i
< m_fileHistoryN
)
2240 s
= m_fileHistory
[i
];
2244 wxFAIL_MSG( wxT("bad index in wxFileHistory::GetHistoryFile") );
2250 void wxFileHistory::UseMenu(wxMenu
*menu
)
2252 if (!m_fileMenus
.Member(menu
))
2253 m_fileMenus
.Append(menu
);
2256 void wxFileHistory::RemoveMenu(wxMenu
*menu
)
2258 m_fileMenus
.DeleteObject(menu
);
2262 void wxFileHistory::Load(wxConfigBase
& config
)
2266 buf
.Printf(wxT("file%d"), (int)m_fileHistoryN
+1);
2267 wxString historyFile
;
2268 while ((m_fileHistoryN
< m_fileMaxFiles
) && config
.Read(buf
, &historyFile
) && (!historyFile
.empty()))
2270 m_fileHistory
[m_fileHistoryN
] = MYcopystring((const wxChar
*) historyFile
);
2272 buf
.Printf(wxT("file%d"), (int)m_fileHistoryN
+1);
2273 historyFile
= wxEmptyString
;
2278 void wxFileHistory::Save(wxConfigBase
& config
)
2281 for (i
= 0; i
< m_fileMaxFiles
; i
++)
2284 buf
.Printf(wxT("file%d"), (int)i
+1);
2285 if (i
< m_fileHistoryN
)
2286 config
.Write(buf
, wxString(m_fileHistory
[i
]));
2288 config
.Write(buf
, wxEmptyString
);
2291 #endif // wxUSE_CONFIG
2293 void wxFileHistory::AddFilesToMenu()
2295 if (m_fileHistoryN
> 0)
2297 wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2300 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2301 if (menu
->GetMenuItemCount())
2303 menu
->AppendSeparator();
2307 for (i
= 0; i
< m_fileHistoryN
; i
++)
2309 if (m_fileHistory
[i
])
2312 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
2313 menu
->Append(m_idBase
+i
, buf
);
2316 node
= node
->GetNext();
2321 void wxFileHistory::AddFilesToMenu(wxMenu
* menu
)
2323 if (m_fileHistoryN
> 0)
2325 if (menu
->GetMenuItemCount())
2327 menu
->AppendSeparator();
2331 for (i
= 0; i
< m_fileHistoryN
; i
++)
2333 if (m_fileHistory
[i
])
2336 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
2337 menu
->Append(m_idBase
+i
, buf
);
2343 // ----------------------------------------------------------------------------
2344 // Permits compatibility with existing file formats and functions that
2345 // manipulate files directly
2346 // ----------------------------------------------------------------------------
2348 #if wxUSE_STD_IOSTREAM
2350 bool wxTransferFileToStream(const wxString
& filename
, wxSTD ostream
& stream
)
2352 wxFFile
file(filename
, _T("rb"));
2353 if ( !file
.IsOpened() )
2361 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2365 stream
.write(buf
, nRead
);
2369 while ( !file
.Eof() );
2374 bool wxTransferStreamToFile(wxSTD istream
& stream
, const wxString
& filename
)
2376 wxFFile
file(filename
, _T("wb"));
2377 if ( !file
.IsOpened() )
2383 stream
.read(buf
, WXSIZEOF(buf
));
2384 if ( !stream
.bad() ) // fail may be set on EOF, don't use operator!()
2386 if ( !file
.Write(buf
, stream
.gcount()) )
2390 while ( !stream
.eof() );
2395 #else // !wxUSE_STD_IOSTREAM
2397 bool wxTransferFileToStream(const wxString
& filename
, wxOutputStream
& stream
)
2399 wxFFile
file(filename
, _T("rb"));
2400 if ( !file
.IsOpened() )
2408 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2412 stream
.Write(buf
, nRead
);
2416 while ( !file
.Eof() );
2421 bool wxTransferStreamToFile(wxInputStream
& stream
, const wxString
& filename
)
2423 wxFFile
file(filename
, _T("wb"));
2424 if ( !file
.IsOpened() )
2430 stream
.Read(buf
, WXSIZEOF(buf
));
2432 const size_t nRead
= stream
.LastRead();
2433 if ( !nRead
|| !file
.Write(buf
, nRead
) )
2436 while ( !stream
.Eof() );
2441 #endif // wxUSE_STD_IOSTREAM/!wxUSE_STD_IOSTREAM
2443 #endif // wxUSE_DOC_VIEW_ARCHITECTURE