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
29 #include "wx/docview.h"
33 #include "wx/string.h"
37 #include "wx/dialog.h"
39 #include "wx/filedlg.h"
42 #include "wx/msgdlg.h"
48 #include "wx/filename.h"
55 #if wxUSE_PRINTING_ARCHITECTURE
56 #include "wx/prntbase.h"
57 #include "wx/printdlg.h"
60 #include "wx/choicdlg.h"
61 #include "wx/confbase.h"
63 #include "wx/cmdproc.h"
64 #include "wx/tokenzr.h"
69 #if wxUSE_STD_IOSTREAM
70 #include "wx/ioswrap.h"
77 #include "wx/wfstream.h"
80 // ----------------------------------------------------------------------------
82 // ----------------------------------------------------------------------------
84 IMPLEMENT_ABSTRACT_CLASS(wxDocument
, wxEvtHandler
)
85 IMPLEMENT_ABSTRACT_CLASS(wxView
, wxEvtHandler
)
86 IMPLEMENT_ABSTRACT_CLASS(wxDocTemplate
, wxObject
)
87 IMPLEMENT_DYNAMIC_CLASS(wxDocManager
, wxEvtHandler
)
88 IMPLEMENT_CLASS(wxDocChildFrame
, wxFrame
)
89 IMPLEMENT_CLASS(wxDocParentFrame
, wxFrame
)
91 #if wxUSE_PRINTING_ARCHITECTURE
92 IMPLEMENT_DYNAMIC_CLASS(wxDocPrintout
, wxPrintout
)
95 IMPLEMENT_DYNAMIC_CLASS(wxFileHistory
, wxObject
)
97 // ----------------------------------------------------------------------------
98 // function prototypes
99 // ----------------------------------------------------------------------------
101 static inline wxString
FindExtension(const wxChar
*path
);
102 static wxWindow
* wxFindSuitableParent(void);
104 // ----------------------------------------------------------------------------
106 // ----------------------------------------------------------------------------
108 static const wxChar
*s_MRUEntryFormat
= wxT("&%d %s");
110 // ============================================================================
112 // ============================================================================
114 // ----------------------------------------------------------------------------
116 // ----------------------------------------------------------------------------
118 static wxString
FindExtension(const wxChar
*path
)
121 wxSplitPath(path
, NULL
, NULL
, &ext
);
123 // VZ: extensions are considered not case sensitive - is this really a good
125 return ext
.MakeLower();
128 // ----------------------------------------------------------------------------
129 // Definition of wxDocument
130 // ----------------------------------------------------------------------------
132 wxDocument::wxDocument(wxDocument
*parent
)
134 m_documentModified
= false;
135 m_documentParent
= parent
;
136 m_documentTemplate
= (wxDocTemplate
*) NULL
;
137 m_commandProcessor
= (wxCommandProcessor
*) NULL
;
141 bool wxDocument::DeleteContents()
146 wxDocument::~wxDocument()
150 if (m_commandProcessor
)
151 delete m_commandProcessor
;
153 if (GetDocumentManager())
154 GetDocumentManager()->RemoveDocument(this);
156 // Not safe to do here, since it'll invoke virtual view functions
157 // expecting to see valid derived objects: and by the time we get here,
158 // we've called destructors higher up.
162 bool wxDocument::Close()
164 if (OnSaveModified())
165 return OnCloseDocument();
170 bool wxDocument::OnCloseDocument()
172 // Tell all views that we're about to close
179 // Note that this implicitly deletes the document when the last view is
181 bool wxDocument::DeleteAllViews()
183 wxDocManager
* manager
= GetDocumentManager();
185 // first check if all views agree to be closed
186 const wxList::iterator end
= m_documentViews
.end();
187 for ( wxList::iterator i
= m_documentViews
.begin(); i
!= end
; ++i
)
189 wxView
*view
= (wxView
*)*i
;
190 if ( !view
->Close() )
194 // all views agreed to close, now do close them
195 if ( m_documentViews
.empty() )
197 // normally the document would be implicitly deleted when the last view
198 // is, but if don't have any views, do it here instead
199 if ( manager
&& manager
->GetDocuments().Member(this) )
204 // as we delete elements we iterate over, don't use the usual "from
205 // begin to end" loop
208 wxView
*view
= (wxView
*)*m_documentViews
.begin();
210 bool isLastOne
= m_documentViews
.size() == 1;
212 // this always deletes the node implicitly and if this is the last
213 // view also deletes this object itself (also implicitly, great),
214 // so we can't test for m_documentViews.empty() after calling this!
225 wxView
*wxDocument::GetFirstView() const
227 if (m_documentViews
.GetCount() == 0)
228 return (wxView
*) NULL
;
229 return (wxView
*)m_documentViews
.GetFirst()->GetData();
232 wxDocManager
*wxDocument::GetDocumentManager() const
234 return (m_documentTemplate
? m_documentTemplate
->GetDocumentManager() : (wxDocManager
*) NULL
);
237 bool wxDocument::OnNewDocument()
239 if (!OnSaveModified())
242 if (OnCloseDocument()==false) return false;
245 SetDocumentSaved(false);
248 GetDocumentManager()->MakeDefaultName(name
);
250 SetFilename(name
, true);
255 bool wxDocument::Save()
257 if (!IsModified() && m_savedYet
)
260 if ( m_documentFile
.empty() || !m_savedYet
)
263 return OnSaveDocument(m_documentFile
);
266 bool wxDocument::SaveAs()
268 wxDocTemplate
*docTemplate
= GetDocumentTemplate();
272 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
273 wxString filter
= docTemplate
->GetDescription() + wxT(" (") + docTemplate
->GetFileFilter() + wxT(")|") + docTemplate
->GetFileFilter();
275 // Now see if there are some other template with identical view and document
276 // classes, whose filters may also be used.
278 if (docTemplate
->GetViewClassInfo() && docTemplate
->GetDocClassInfo())
280 wxList::compatibility_iterator node
= wxDocManager::GetDocumentManager()->GetTemplates().GetFirst();
283 wxDocTemplate
*t
= (wxDocTemplate
*) node
->GetData();
285 if (t
->IsVisible() && t
!= docTemplate
&&
286 t
->GetViewClassInfo() == docTemplate
->GetViewClassInfo() &&
287 t
->GetDocClassInfo() == docTemplate
->GetDocClassInfo())
289 // add a '|' to separate this filter from the previous one
290 if ( !filter
.empty() )
293 filter
<< t
->GetDescription() << wxT(" (") << t
->GetFileFilter() << wxT(") |")
294 << t
->GetFileFilter();
297 node
= node
->GetNext();
301 wxString filter
= docTemplate
->GetFileFilter() ;
303 wxString tmp
= wxFileSelector(_("Save as"),
304 docTemplate
->GetDirectory(),
305 wxFileNameFromPath(GetFilename()),
306 docTemplate
->GetDefaultExtension(),
308 wxFD_SAVE
| wxFD_OVERWRITE_PROMPT
,
309 GetDocumentWindow());
314 wxString
fileName(tmp
);
315 wxString path
, name
, ext
;
316 wxSplitPath(fileName
, & path
, & name
, & ext
);
320 fileName
+= wxT(".");
321 fileName
+= docTemplate
->GetDefaultExtension();
324 SetFilename(fileName
);
325 SetTitle(wxFileNameFromPath(fileName
));
327 // Notify the views that the filename has changed
328 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
331 wxView
*view
= (wxView
*)node
->GetData();
332 view
->OnChangeFilename();
333 node
= node
->GetNext();
336 // Files that were not saved correctly are not added to the FileHistory.
337 if (!OnSaveDocument(m_documentFile
))
340 // A file that doesn't use the default extension of its document template cannot be opened
341 // via the FileHistory, so we do not add it.
342 if (docTemplate
->FileMatchesTemplate(fileName
))
344 GetDocumentManager()->AddFileToHistory(fileName
);
348 // The user will probably not be able to open the file again, so
349 // we could warn about the wrong file-extension here.
354 bool wxDocument::OnSaveDocument(const wxString
& file
)
359 if ( !DoSaveDocument(file
) )
364 SetDocumentSaved(true);
366 wxFileName
fn(file
) ;
367 fn
.MacSetDefaultTypeAndCreator() ;
372 bool wxDocument::OnOpenDocument(const wxString
& file
)
374 if (!OnSaveModified())
377 if ( !DoOpenDocument(file
) )
380 SetFilename(file
, true);
389 #if wxUSE_STD_IOSTREAM
390 wxSTD istream
& wxDocument::LoadObject(wxSTD istream
& stream
)
392 wxInputStream
& wxDocument::LoadObject(wxInputStream
& stream
)
398 #if wxUSE_STD_IOSTREAM
399 wxSTD ostream
& wxDocument::SaveObject(wxSTD ostream
& stream
)
401 wxOutputStream
& wxDocument::SaveObject(wxOutputStream
& stream
)
407 bool wxDocument::Revert()
413 // Get title, or filename if no title, else unnamed
414 bool wxDocument::GetPrintableName(wxString
& buf
) const
416 if (!m_documentTitle
.empty())
418 buf
= m_documentTitle
;
421 else if (!m_documentFile
.empty())
423 buf
= wxFileNameFromPath(m_documentFile
);
433 wxWindow
*wxDocument::GetDocumentWindow() const
435 wxView
*view
= GetFirstView();
437 return view
->GetFrame();
439 return wxTheApp
->GetTopWindow();
442 wxCommandProcessor
*wxDocument::OnCreateCommandProcessor()
444 return new wxCommandProcessor
;
447 // true if safe to close
448 bool wxDocument::OnSaveModified()
453 GetPrintableName(title
);
456 if (!wxTheApp
->GetAppName().empty())
457 msgTitle
= wxTheApp
->GetAppName();
459 msgTitle
= wxString(_("Warning"));
462 prompt
.Printf(_("Do you want to save changes to document %s?"),
463 (const wxChar
*)title
);
464 int res
= wxMessageBox(prompt
, msgTitle
,
465 wxYES_NO
|wxCANCEL
|wxICON_QUESTION
,
466 GetDocumentWindow());
472 else if (res
== wxYES
)
474 else if (res
== wxCANCEL
)
480 bool wxDocument::Draw(wxDC
& WXUNUSED(context
))
485 bool wxDocument::AddView(wxView
*view
)
487 if (!m_documentViews
.Member(view
))
489 m_documentViews
.Append(view
);
495 bool wxDocument::RemoveView(wxView
*view
)
497 (void)m_documentViews
.DeleteObject(view
);
502 bool wxDocument::OnCreate(const wxString
& WXUNUSED(path
), long flags
)
504 if (GetDocumentTemplate()->CreateView(this, flags
))
510 // Called after a view is added or removed.
511 // The default implementation deletes the document if
512 // there are no more views.
513 void wxDocument::OnChangedViewList()
515 if (m_documentViews
.GetCount() == 0)
517 if (OnSaveModified())
524 void wxDocument::UpdateAllViews(wxView
*sender
, wxObject
*hint
)
526 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
529 wxView
*view
= (wxView
*)node
->GetData();
531 view
->OnUpdate(sender
, hint
);
532 node
= node
->GetNext();
536 void wxDocument::NotifyClosing()
538 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
541 wxView
*view
= (wxView
*)node
->GetData();
542 view
->OnClosingDocument();
543 node
= node
->GetNext();
547 void wxDocument::SetFilename(const wxString
& filename
, bool notifyViews
)
549 m_documentFile
= filename
;
552 // Notify the views that the filename has changed
553 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
556 wxView
*view
= (wxView
*)node
->GetData();
557 view
->OnChangeFilename();
558 node
= node
->GetNext();
563 bool wxDocument::DoSaveDocument(const wxString
& file
)
566 if (!wxTheApp
->GetAppName().empty())
567 msgTitle
= wxTheApp
->GetAppName();
569 msgTitle
= wxString(_("File error"));
571 #if wxUSE_STD_IOSTREAM
572 wxSTD ofstream
store(file
.mb_str(), wxSTD
ios::binary
);
573 if (store
.fail() || store
.bad())
575 wxFileOutputStream
store(file
);
576 if (store
.GetLastError() != wxSTREAM_NO_ERROR
)
579 (void)wxMessageBox(_("Sorry, could not open this file for saving."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
580 GetDocumentWindow());
584 if (!SaveObject(store
))
586 (void)wxMessageBox(_("Sorry, could not save this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
587 GetDocumentWindow());
595 bool wxDocument::DoOpenDocument(const wxString
& file
)
597 #if wxUSE_STD_IOSTREAM
598 wxSTD ifstream
store(file
.mb_str(), wxSTD
ios::binary
);
599 if (!store
.fail() && !store
.bad())
601 wxFileInputStream
store(file
);
602 if (store
.GetLastError() == wxSTREAM_NO_ERROR
)
605 #if wxUSE_STD_IOSTREAM
607 if ( !!store
|| store
.eof() )
609 int res
= LoadObject(store
).GetLastError();
610 if ( res
== wxSTREAM_NO_ERROR
|| res
== wxSTREAM_EOF
)
615 wxLogError(_("Sorry, could not open this file."));
620 // ----------------------------------------------------------------------------
622 // ----------------------------------------------------------------------------
626 m_viewDocument
= (wxDocument
*) NULL
;
628 m_viewFrame
= (wxFrame
*) NULL
;
633 GetDocumentManager()->ActivateView(this, false);
634 m_viewDocument
->RemoveView(this);
637 // Extend event processing to search the document's event table
638 bool wxView::ProcessEvent(wxEvent
& event
)
640 if ( !GetDocument() || !GetDocument()->ProcessEvent(event
) )
641 return wxEvtHandler::ProcessEvent(event
);
646 void wxView::OnActivateView(bool WXUNUSED(activate
), wxView
*WXUNUSED(activeView
), wxView
*WXUNUSED(deactiveView
))
650 void wxView::OnPrint(wxDC
*dc
, wxObject
*WXUNUSED(info
))
655 void wxView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
))
659 void wxView::OnChangeFilename()
661 // GetFrame can return wxWindow rather than wxTopLevelWindow due to
662 // generic MDI implementation so use SetLabel rather than SetTitle.
663 // It should cause SetTitle() for top level windows.
664 wxWindow
*win
= GetFrame();
667 wxDocument
*doc
= GetDocument();
671 doc
->GetPrintableName(name
);
675 void wxView::SetDocument(wxDocument
*doc
)
677 m_viewDocument
= doc
;
682 bool wxView::Close(bool deleteWindow
)
684 if (OnClose(deleteWindow
))
690 void wxView::Activate(bool activate
)
692 if (GetDocument() && GetDocumentManager())
694 OnActivateView(activate
, this, GetDocumentManager()->GetCurrentView());
695 GetDocumentManager()->ActivateView(this, activate
);
699 bool wxView::OnClose(bool WXUNUSED(deleteWindow
))
701 return GetDocument() ? GetDocument()->Close() : true;
704 #if wxUSE_PRINTING_ARCHITECTURE
705 wxPrintout
*wxView::OnCreatePrintout()
707 return new wxDocPrintout(this);
709 #endif // wxUSE_PRINTING_ARCHITECTURE
711 // ----------------------------------------------------------------------------
713 // ----------------------------------------------------------------------------
715 wxDocTemplate::wxDocTemplate(wxDocManager
*manager
,
716 const wxString
& descr
,
717 const wxString
& filter
,
720 const wxString
& docTypeName
,
721 const wxString
& viewTypeName
,
722 wxClassInfo
*docClassInfo
,
723 wxClassInfo
*viewClassInfo
,
726 m_documentManager
= manager
;
727 m_description
= descr
;
730 m_fileFilter
= filter
;
732 m_docTypeName
= docTypeName
;
733 m_viewTypeName
= viewTypeName
;
734 m_documentManager
->AssociateTemplate(this);
736 m_docClassInfo
= docClassInfo
;
737 m_viewClassInfo
= viewClassInfo
;
740 wxDocTemplate::~wxDocTemplate()
742 m_documentManager
->DisassociateTemplate(this);
745 // Tries to dynamically construct an object of the right class.
746 wxDocument
*wxDocTemplate::CreateDocument(const wxString
& path
, long flags
)
748 wxDocument
*doc
= DoCreateDocument();
750 return (wxDocument
*) NULL
;
752 if (InitDocument(doc
, path
, flags
))
758 return (wxDocument
*) NULL
;
762 bool wxDocTemplate::InitDocument(wxDocument
* doc
, const wxString
& path
, long flags
)
764 doc
->SetFilename(path
);
765 doc
->SetDocumentTemplate(this);
766 GetDocumentManager()->AddDocument(doc
);
767 doc
->SetCommandProcessor(doc
->OnCreateCommandProcessor());
769 if (doc
->OnCreate(path
, flags
))
773 if (GetDocumentManager()->GetDocuments().Member(doc
))
774 doc
->DeleteAllViews();
779 wxView
*wxDocTemplate::CreateView(wxDocument
*doc
, long flags
)
781 wxView
*view
= DoCreateView();
783 return (wxView
*) NULL
;
785 view
->SetDocument(doc
);
786 if (view
->OnCreate(doc
, flags
))
793 return (wxView
*) NULL
;
797 // The default (very primitive) format detection: check is the extension is
798 // that of the template
799 bool wxDocTemplate::FileMatchesTemplate(const wxString
& path
)
801 wxStringTokenizer
parser (GetFileFilter(), wxT(";"));
802 wxString anything
= wxT ("*");
803 while (parser
.HasMoreTokens())
805 wxString filter
= parser
.GetNextToken();
806 wxString filterExt
= FindExtension (filter
);
807 if ( filter
.IsSameAs (anything
) ||
808 filterExt
.IsSameAs (anything
) ||
809 filterExt
.IsSameAs (FindExtension (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 if (!newDoc
->OnNewDocument() )
1230 // Document is implicitly deleted by DeleteAllViews
1231 newDoc
->DeleteAllViews();
1238 wxDocTemplate
*temp
= SelectDocumentType(templates
, n
);
1244 if (!CloseDocument(docToClose
, false))
1250 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
1254 newDoc
->SetDocumentName(temp
->GetDocumentName());
1255 newDoc
->SetDocumentTemplate(temp
);
1256 if (!newDoc
->OnNewDocument() )
1258 // Document is implicitly deleted by DeleteAllViews
1259 newDoc
->DeleteAllViews();
1266 return (wxDocument
*) NULL
;
1269 // Existing document
1270 wxDocTemplate
*temp
;
1272 wxString path2
= path
;
1274 if (flags
& wxDOC_SILENT
)
1276 temp
= FindTemplateForPath(path2
);
1279 // Since we do not add files with non-default extensions to the FileHistory this
1280 // can only happen if the application changes the allowed templates in runtime.
1281 (void)wxMessageBox(_("Sorry, the format for this file is unknown."),
1283 wxOK
| wxICON_EXCLAMATION
, wxFindSuitableParent());
1287 temp
= SelectDocumentPath(templates
, n
, path2
, flags
);
1295 if (!CloseDocument(docToClose
, false))
1301 //see if this file is already open
1302 for (size_t i
= 0; i
< GetDocuments().GetCount(); ++i
)
1304 wxDocument
* currentDoc
= (wxDocument
*)(GetDocuments().Item(i
)->GetData());
1306 //file paths are case-insensitive on Windows
1307 if (path2
.CmpNoCase(currentDoc
->GetFilename()) == 0)
1309 if (path2
.Cmp(currentDoc
->GetFilename()) == 0)
1312 //file already open. Just activate it and return
1313 if (currentDoc
->GetFirstView())
1315 ActivateView(currentDoc
->GetFirstView(), true);
1316 if (currentDoc
->GetDocumentWindow())
1317 currentDoc
->GetDocumentWindow()->SetFocus();
1323 wxDocument
*newDoc
= temp
->CreateDocument(path2
, flags
);
1326 newDoc
->SetDocumentName(temp
->GetDocumentName());
1327 newDoc
->SetDocumentTemplate(temp
);
1328 if (!newDoc
->OnOpenDocument(path2
))
1330 newDoc
->DeleteAllViews();
1331 // delete newDoc; // Implicitly deleted by DeleteAllViews
1332 return (wxDocument
*) NULL
;
1334 // A file that doesn't use the default extension of its document
1335 // template cannot be opened via the FileHistory, so we do not
1337 if (temp
->FileMatchesTemplate(path2
))
1338 AddFileToHistory(path2
);
1343 return (wxDocument
*) NULL
;
1346 wxView
*wxDocManager::CreateView(wxDocument
*doc
, long flags
)
1348 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.GetCount()];
1351 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1353 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Item(i
)->GetData());
1354 if (temp
->IsVisible())
1356 if (temp
->GetDocumentName() == doc
->GetDocumentName())
1358 templates
[n
] = temp
;
1366 return (wxView
*) NULL
;
1370 wxDocTemplate
*temp
= templates
[0];
1372 wxView
*view
= temp
->CreateView(doc
, flags
);
1374 view
->SetViewName(temp
->GetViewName());
1378 wxDocTemplate
*temp
= SelectViewType(templates
, n
);
1382 wxView
*view
= temp
->CreateView(doc
, flags
);
1384 view
->SetViewName(temp
->GetViewName());
1388 return (wxView
*) NULL
;
1391 // Not yet implemented
1392 void wxDocManager::DeleteTemplate(wxDocTemplate
*WXUNUSED(temp
), long WXUNUSED(flags
))
1396 // Not yet implemented
1397 bool wxDocManager::FlushDoc(wxDocument
*WXUNUSED(doc
))
1402 wxDocument
*wxDocManager::GetCurrentDocument() const
1404 wxView
*view
= GetCurrentView();
1406 return view
->GetDocument();
1408 return (wxDocument
*) NULL
;
1411 // Make a default document name
1412 bool wxDocManager::MakeDefaultName(wxString
& name
)
1414 name
.Printf(_("unnamed%d"), m_defaultDocumentNameCounter
);
1415 m_defaultDocumentNameCounter
++;
1420 // Make a frame title (override this to do something different)
1421 // If docName is empty, a document is not currently active.
1422 wxString
wxDocManager::MakeFrameTitle(wxDocument
* doc
)
1424 wxString appName
= wxTheApp
->GetAppName();
1431 doc
->GetPrintableName(docName
);
1432 title
= docName
+ wxString(_(" - ")) + appName
;
1438 // Not yet implemented
1439 wxDocTemplate
*wxDocManager::MatchTemplate(const wxString
& WXUNUSED(path
))
1441 return (wxDocTemplate
*) NULL
;
1444 // File history management
1445 void wxDocManager::AddFileToHistory(const wxString
& file
)
1448 m_fileHistory
->AddFileToHistory(file
);
1451 void wxDocManager::RemoveFileFromHistory(size_t i
)
1454 m_fileHistory
->RemoveFileFromHistory(i
);
1457 wxString
wxDocManager::GetHistoryFile(size_t i
) const
1462 histFile
= m_fileHistory
->GetHistoryFile(i
);
1467 void wxDocManager::FileHistoryUseMenu(wxMenu
*menu
)
1470 m_fileHistory
->UseMenu(menu
);
1473 void wxDocManager::FileHistoryRemoveMenu(wxMenu
*menu
)
1476 m_fileHistory
->RemoveMenu(menu
);
1480 void wxDocManager::FileHistoryLoad(wxConfigBase
& config
)
1483 m_fileHistory
->Load(config
);
1486 void wxDocManager::FileHistorySave(wxConfigBase
& config
)
1489 m_fileHistory
->Save(config
);
1493 void wxDocManager::FileHistoryAddFilesToMenu(wxMenu
* menu
)
1496 m_fileHistory
->AddFilesToMenu(menu
);
1499 void wxDocManager::FileHistoryAddFilesToMenu()
1502 m_fileHistory
->AddFilesToMenu();
1505 size_t wxDocManager::GetHistoryFilesCount() const
1507 return m_fileHistory
? m_fileHistory
->GetCount() : 0;
1511 // Find out the document template via matching in the document file format
1512 // against that of the template
1513 wxDocTemplate
*wxDocManager::FindTemplateForPath(const wxString
& path
)
1515 wxDocTemplate
*theTemplate
= (wxDocTemplate
*) NULL
;
1517 // Find the template which this extension corresponds to
1518 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1520 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Item(i
)->GetData();
1521 if ( temp
->FileMatchesTemplate(path
) )
1530 // Try to get a more suitable parent frame than the top window,
1531 // for selection dialogs. Otherwise you may get an unexpected
1532 // window being activated when a dialog is shown.
1533 static wxWindow
* wxFindSuitableParent()
1535 wxWindow
* parent
= wxTheApp
->GetTopWindow();
1537 wxWindow
* focusWindow
= wxWindow::FindFocus();
1540 while (focusWindow
&&
1541 !focusWindow
->IsKindOf(CLASSINFO(wxDialog
)) &&
1542 !focusWindow
->IsKindOf(CLASSINFO(wxFrame
)))
1544 focusWindow
= focusWindow
->GetParent();
1547 parent
= focusWindow
;
1552 // Prompts user to open a file, using file specs in templates.
1553 // Must extend the file selector dialog or implement own; OR
1554 // match the extension to the template extension.
1556 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**templates
,
1557 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
1560 int WXUNUSED(noTemplates
),
1563 long WXUNUSED(flags
),
1564 bool WXUNUSED(save
))
1566 // We can only have multiple filters in Windows and GTK
1567 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
1571 for (i
= 0; i
< noTemplates
; i
++)
1573 if (templates
[i
]->IsVisible())
1575 // add a '|' to separate this filter from the previous one
1576 if ( !descrBuf
.empty() )
1577 descrBuf
<< wxT('|');
1579 descrBuf
<< templates
[i
]->GetDescription()
1580 << wxT(" (") << templates
[i
]->GetFileFilter() << wxT(") |")
1581 << templates
[i
]->GetFileFilter();
1585 wxString descrBuf
= wxT("*.*");
1588 int FilterIndex
= -1;
1590 wxWindow
* parent
= wxFindSuitableParent();
1592 wxString pathTmp
= wxFileSelectorEx(_("Select a file"),
1600 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)NULL
;
1601 if (!pathTmp
.empty())
1603 if (!wxFileExists(pathTmp
))
1606 if (!wxTheApp
->GetAppName().empty())
1607 msgTitle
= wxTheApp
->GetAppName();
1609 msgTitle
= wxString(_("File error"));
1611 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
1614 path
= wxEmptyString
;
1615 return (wxDocTemplate
*) NULL
;
1617 m_lastDirectory
= wxPathOnly(pathTmp
);
1621 // first choose the template using the extension, if this fails (i.e.
1622 // wxFileSelectorEx() didn't fill it), then use the path
1623 if ( FilterIndex
!= -1 )
1624 theTemplate
= templates
[FilterIndex
];
1626 theTemplate
= FindTemplateForPath(path
);
1629 // Since we do not add files with non-default extensions to the FileHistory this
1630 // can only happen if the application changes the allowed templates in runtime.
1631 (void)wxMessageBox(_("Sorry, the format for this file is unknown."),
1633 wxOK
| wxICON_EXCLAMATION
, wxFindSuitableParent());
1638 path
= wxEmptyString
;
1644 wxDocTemplate
*wxDocManager::SelectDocumentType(wxDocTemplate
**templates
,
1645 int noTemplates
, bool sort
)
1647 wxArrayString strings
;
1648 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1652 for (i
= 0; i
< noTemplates
; i
++)
1654 if (templates
[i
]->IsVisible())
1658 for (j
= 0; j
< n
; j
++)
1660 //filter out NOT unique documents + view combinations
1661 if ( templates
[i
]->m_docTypeName
== data
[j
]->m_docTypeName
&&
1662 templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
1669 strings
.Add(templates
[i
]->m_description
);
1671 data
[n
] = templates
[i
];
1679 strings
.Sort(); // ascending sort
1680 // Yes, this will be slow, but template lists
1681 // are typically short.
1683 n
= strings
.Count();
1684 for (i
= 0; i
< n
; i
++)
1686 for (j
= 0; j
< noTemplates
; j
++)
1688 if (strings
[i
] == templates
[j
]->m_description
)
1689 data
[i
] = templates
[j
];
1694 wxDocTemplate
*theTemplate
;
1699 // no visible templates, hence nothing to choose from
1704 // don't propose the user to choose if he heas no choice
1705 theTemplate
= data
[0];
1709 // propose the user to choose one of several
1710 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1712 _("Select a document template"),
1716 wxFindSuitableParent()
1725 wxDocTemplate
*wxDocManager::SelectViewType(wxDocTemplate
**templates
,
1726 int noTemplates
, bool sort
)
1728 wxArrayString strings
;
1729 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1733 for (i
= 0; i
< noTemplates
; i
++)
1735 wxDocTemplate
*templ
= templates
[i
];
1736 if ( templ
->IsVisible() && !templ
->GetViewName().empty() )
1740 for (j
= 0; j
< n
; j
++)
1742 //filter out NOT unique views
1743 if ( templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
)
1749 strings
.Add(templ
->m_viewTypeName
);
1758 strings
.Sort(); // ascending sort
1759 // Yes, this will be slow, but template lists
1760 // are typically short.
1762 n
= strings
.Count();
1763 for (i
= 0; i
< n
; i
++)
1765 for (j
= 0; j
< noTemplates
; j
++)
1767 if (strings
[i
] == templates
[j
]->m_viewTypeName
)
1768 data
[i
] = templates
[j
];
1773 wxDocTemplate
*theTemplate
;
1775 // the same logic as above
1779 theTemplate
= (wxDocTemplate
*)NULL
;
1783 theTemplate
= data
[0];
1787 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1789 _("Select a document view"),
1793 wxFindSuitableParent()
1802 void wxDocManager::AssociateTemplate(wxDocTemplate
*temp
)
1804 if (!m_templates
.Member(temp
))
1805 m_templates
.Append(temp
);
1808 void wxDocManager::DisassociateTemplate(wxDocTemplate
*temp
)
1810 m_templates
.DeleteObject(temp
);
1813 // Add and remove a document from the manager's list
1814 void wxDocManager::AddDocument(wxDocument
*doc
)
1816 if (!m_docs
.Member(doc
))
1820 void wxDocManager::RemoveDocument(wxDocument
*doc
)
1822 m_docs
.DeleteObject(doc
);
1825 // Views or windows should inform the document manager
1826 // when a view is going in or out of focus
1827 void wxDocManager::ActivateView(wxView
*view
, bool activate
)
1831 m_currentView
= view
;
1835 if ( m_currentView
== view
)
1837 // don't keep stale pointer
1838 m_currentView
= (wxView
*) NULL
;
1843 // ----------------------------------------------------------------------------
1844 // Default document child frame
1845 // ----------------------------------------------------------------------------
1847 BEGIN_EVENT_TABLE(wxDocChildFrame
, wxFrame
)
1848 EVT_ACTIVATE(wxDocChildFrame::OnActivate
)
1849 EVT_CLOSE(wxDocChildFrame::OnCloseWindow
)
1852 wxDocChildFrame::wxDocChildFrame(wxDocument
*doc
,
1856 const wxString
& title
,
1860 const wxString
& name
)
1861 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1863 m_childDocument
= doc
;
1866 view
->SetFrame(this);
1869 // Extend event processing to search the view's event table
1870 bool wxDocChildFrame::ProcessEvent(wxEvent
& event
)
1873 m_childView
->Activate(true);
1875 if ( !m_childView
|| ! m_childView
->ProcessEvent(event
) )
1877 // Only hand up to the parent if it's a menu command
1878 if (!event
.IsKindOf(CLASSINFO(wxCommandEvent
)) || !GetParent() || !GetParent()->ProcessEvent(event
))
1879 return wxEvtHandler::ProcessEvent(event
);
1887 void wxDocChildFrame::OnActivate(wxActivateEvent
& event
)
1889 wxFrame::OnActivate(event
);
1892 m_childView
->Activate(event
.GetActive());
1895 void wxDocChildFrame::OnCloseWindow(wxCloseEvent
& event
)
1899 bool ans
= event
.CanVeto()
1900 ? m_childView
->Close(false) // false means don't delete associated window
1901 : true; // Must delete.
1905 m_childView
->Activate(false);
1907 m_childView
= (wxView
*) NULL
;
1908 m_childDocument
= (wxDocument
*) NULL
;
1919 // ----------------------------------------------------------------------------
1920 // Default parent frame
1921 // ----------------------------------------------------------------------------
1923 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1924 EVT_MENU(wxID_EXIT
, wxDocParentFrame::OnExit
)
1925 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE9
, wxDocParentFrame::OnMRUFile
)
1926 EVT_CLOSE(wxDocParentFrame::OnCloseWindow
)
1929 wxDocParentFrame::wxDocParentFrame(wxDocManager
*manager
,
1932 const wxString
& title
,
1936 const wxString
& name
)
1937 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1939 m_docManager
= manager
;
1942 void wxDocParentFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
1947 void wxDocParentFrame::OnMRUFile(wxCommandEvent
& event
)
1949 int n
= event
.GetId() - wxID_FILE1
; // the index in MRU list
1950 wxString
filename(m_docManager
->GetHistoryFile(n
));
1951 if ( !filename
.empty() )
1953 // verify that the file exists before doing anything else
1954 if ( wxFile::Exists(filename
) )
1957 if (!m_docManager
->CreateDocument(filename
, wxDOC_SILENT
))
1959 // remove the file from the MRU list. The user should already be notified.
1960 m_docManager
->RemoveFileFromHistory(n
);
1962 wxLogError(_("The file '%s' couldn't be opened.\nIt has been removed from the most recently used files list."),
1968 // remove the bogus filename from the MRU list and notify the user
1970 m_docManager
->RemoveFileFromHistory(n
);
1972 wxLogError(_("The file '%s' doesn't exist and couldn't be opened.\nIt has been removed from the most recently used files list."),
1978 // Extend event processing to search the view's event table
1979 bool wxDocParentFrame::ProcessEvent(wxEvent
& event
)
1981 // Try the document manager, then do default processing
1982 if (!m_docManager
|| !m_docManager
->ProcessEvent(event
))
1983 return wxEvtHandler::ProcessEvent(event
);
1988 // Define the behaviour for the frame closing
1989 // - must delete all frames except for the main one.
1990 void wxDocParentFrame::OnCloseWindow(wxCloseEvent
& event
)
1992 if (m_docManager
->Clear(!event
.CanVeto()))
2000 #if wxUSE_PRINTING_ARCHITECTURE
2002 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
)
2005 m_printoutView
= view
;
2008 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
2012 // Get the logical pixels per inch of screen and printer
2013 int ppiScreenX
, ppiScreenY
;
2014 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
2015 wxUnusedVar(ppiScreenY
);
2016 int ppiPrinterX
, ppiPrinterY
;
2017 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
2018 wxUnusedVar(ppiPrinterY
);
2020 // This scales the DC so that the printout roughly represents the
2021 // the screen scaling. The text point size _should_ be the right size
2022 // but in fact is too small for some reason. This is a detail that will
2023 // need to be addressed at some point but can be fudged for the
2025 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
2027 // Now we have to check in case our real page size is reduced
2028 // (e.g. because we're drawing to a print preview memory DC)
2029 int pageWidth
, pageHeight
;
2031 dc
->GetSize(&w
, &h
);
2032 GetPageSizePixels(&pageWidth
, &pageHeight
);
2033 wxUnusedVar(pageHeight
);
2035 // If printer pageWidth == current DC width, then this doesn't
2036 // change. But w might be the preview bitmap width, so scale down.
2037 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
2038 dc
->SetUserScale(overallScale
, overallScale
);
2042 m_printoutView
->OnDraw(dc
);
2047 bool wxDocPrintout::HasPage(int pageNum
)
2049 return (pageNum
== 1);
2052 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
2054 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
2060 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
2068 #endif // wxUSE_PRINTING_ARCHITECTURE
2070 // ----------------------------------------------------------------------------
2071 // File history processor
2072 // ----------------------------------------------------------------------------
2074 static inline wxChar
* MYcopystring(const wxString
& s
)
2076 wxChar
* copy
= new wxChar
[s
.length() + 1];
2077 return wxStrcpy(copy
, s
.c_str());
2080 static inline wxChar
* MYcopystring(const wxChar
* s
)
2082 wxChar
* copy
= new wxChar
[wxStrlen(s
) + 1];
2083 return wxStrcpy(copy
, s
);
2086 wxFileHistory::wxFileHistory(size_t maxFiles
, wxWindowID idBase
)
2088 m_fileMaxFiles
= maxFiles
;
2091 m_fileHistory
= new wxChar
*[m_fileMaxFiles
];
2094 wxFileHistory::~wxFileHistory()
2097 for (i
= 0; i
< m_fileHistoryN
; i
++)
2098 delete[] m_fileHistory
[i
];
2099 delete[] m_fileHistory
;
2102 // File history management
2103 void wxFileHistory::AddFileToHistory(const wxString
& file
)
2107 // Check we don't already have this file
2108 for (i
= 0; i
< m_fileHistoryN
; i
++)
2110 #if defined( __WXMSW__ ) // Add any other OSes with case insensitive file names
2111 wxString testString
;
2112 if ( m_fileHistory
[i
] )
2113 testString
= m_fileHistory
[i
];
2114 if ( m_fileHistory
[i
] && ( file
.Lower() == testString
.Lower() ) )
2116 if ( m_fileHistory
[i
] && ( file
== m_fileHistory
[i
] ) )
2119 // we do have it, move it to the top of the history
2120 RemoveFileFromHistory (i
);
2121 AddFileToHistory (file
);
2126 // if we already have a full history, delete the one at the end
2127 if ( m_fileMaxFiles
== m_fileHistoryN
)
2129 RemoveFileFromHistory (m_fileHistoryN
- 1);
2130 AddFileToHistory (file
);
2134 // Add to the project file history:
2135 // Move existing files (if any) down so we can insert file at beginning.
2136 if (m_fileHistoryN
< m_fileMaxFiles
)
2138 wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2141 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2142 if ( m_fileHistoryN
== 0 && menu
->GetMenuItemCount() )
2144 menu
->AppendSeparator();
2146 menu
->Append(m_idBase
+m_fileHistoryN
, _("[EMPTY]"));
2147 node
= node
->GetNext();
2151 // Shuffle filenames down
2152 for (i
= (m_fileHistoryN
-1); i
> 0; i
--)
2154 m_fileHistory
[i
] = m_fileHistory
[i
-1];
2156 m_fileHistory
[0] = MYcopystring(file
);
2158 // this is the directory of the last opened file
2159 wxString pathCurrent
;
2160 wxSplitPath( m_fileHistory
[0], &pathCurrent
, NULL
, NULL
);
2161 for (i
= 0; i
< m_fileHistoryN
; i
++)
2163 if ( m_fileHistory
[i
] )
2165 // if in same directory just show the filename; otherwise the full
2167 wxString pathInMenu
, path
, filename
, ext
;
2168 wxSplitPath( m_fileHistory
[i
], &path
, &filename
, &ext
);
2169 if ( path
== pathCurrent
)
2171 pathInMenu
= filename
;
2173 pathInMenu
= pathInMenu
+ wxFILE_SEP_EXT
+ ext
;
2177 // absolute path; could also set relative path
2178 pathInMenu
= m_fileHistory
[i
];
2181 // we need to quote '&' characters which are used for mnemonics
2182 pathInMenu
.Replace(_T("&"), _T("&&"));
2184 buf
.Printf(s_MRUEntryFormat
, i
+ 1, pathInMenu
.c_str());
2185 wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2188 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2189 menu
->SetLabel(m_idBase
+ i
, buf
);
2190 node
= node
->GetNext();
2196 void wxFileHistory::RemoveFileFromHistory(size_t i
)
2198 wxCHECK_RET( i
< m_fileHistoryN
,
2199 wxT("invalid index in wxFileHistory::RemoveFileFromHistory") );
2201 // delete the element from the array (could use memmove() too...)
2202 delete [] m_fileHistory
[i
];
2205 for ( j
= i
; j
< m_fileHistoryN
- 1; j
++ )
2207 m_fileHistory
[j
] = m_fileHistory
[j
+ 1];
2210 wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2213 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2215 // shuffle filenames up
2217 for ( j
= i
; j
< m_fileHistoryN
- 1; j
++ )
2219 buf
.Printf(s_MRUEntryFormat
, j
+ 1, m_fileHistory
[j
]);
2220 menu
->SetLabel(m_idBase
+ j
, buf
);
2223 node
= node
->GetNext();
2225 // delete the last menu item which is unused now
2226 wxWindowID lastItemId
= m_idBase
+ wx_truncate_cast(wxWindowID
, m_fileHistoryN
) - 1;
2227 if (menu
->FindItem(lastItemId
))
2229 menu
->Delete(lastItemId
);
2232 // delete the last separator too if no more files are left
2233 if ( m_fileHistoryN
== 1 )
2235 wxMenuItemList::compatibility_iterator nodeLast
= menu
->GetMenuItems().GetLast();
2238 wxMenuItem
*menuItem
= nodeLast
->GetData();
2239 if ( menuItem
->IsSeparator() )
2241 menu
->Delete(menuItem
);
2243 //else: should we search backwards for the last separator?
2245 //else: menu is empty somehow
2252 wxString
wxFileHistory::GetHistoryFile(size_t i
) const
2255 if ( i
< m_fileHistoryN
)
2257 s
= m_fileHistory
[i
];
2261 wxFAIL_MSG( wxT("bad index in wxFileHistory::GetHistoryFile") );
2267 void wxFileHistory::UseMenu(wxMenu
*menu
)
2269 if (!m_fileMenus
.Member(menu
))
2270 m_fileMenus
.Append(menu
);
2273 void wxFileHistory::RemoveMenu(wxMenu
*menu
)
2275 m_fileMenus
.DeleteObject(menu
);
2279 void wxFileHistory::Load(wxConfigBase
& config
)
2283 buf
.Printf(wxT("file%d"), (int)m_fileHistoryN
+1);
2284 wxString historyFile
;
2285 while ((m_fileHistoryN
< m_fileMaxFiles
) && config
.Read(buf
, &historyFile
) && (!historyFile
.empty()))
2287 m_fileHistory
[m_fileHistoryN
] = MYcopystring((const wxChar
*) historyFile
);
2289 buf
.Printf(wxT("file%d"), (int)m_fileHistoryN
+1);
2290 historyFile
= wxEmptyString
;
2295 void wxFileHistory::Save(wxConfigBase
& config
)
2298 for (i
= 0; i
< m_fileMaxFiles
; i
++)
2301 buf
.Printf(wxT("file%d"), (int)i
+1);
2302 if (i
< m_fileHistoryN
)
2303 config
.Write(buf
, wxString(m_fileHistory
[i
]));
2305 config
.Write(buf
, wxEmptyString
);
2308 #endif // wxUSE_CONFIG
2310 void wxFileHistory::AddFilesToMenu()
2312 if (m_fileHistoryN
> 0)
2314 wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2317 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2318 if (menu
->GetMenuItemCount())
2320 menu
->AppendSeparator();
2324 for (i
= 0; i
< m_fileHistoryN
; i
++)
2326 if (m_fileHistory
[i
])
2329 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
2330 menu
->Append(m_idBase
+i
, buf
);
2333 node
= node
->GetNext();
2338 void wxFileHistory::AddFilesToMenu(wxMenu
* menu
)
2340 if (m_fileHistoryN
> 0)
2342 if (menu
->GetMenuItemCount())
2344 menu
->AppendSeparator();
2348 for (i
= 0; i
< m_fileHistoryN
; i
++)
2350 if (m_fileHistory
[i
])
2353 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
2354 menu
->Append(m_idBase
+i
, buf
);
2360 // ----------------------------------------------------------------------------
2361 // Permits compatibility with existing file formats and functions that
2362 // manipulate files directly
2363 // ----------------------------------------------------------------------------
2365 #if wxUSE_STD_IOSTREAM
2367 bool wxTransferFileToStream(const wxString
& filename
, wxSTD ostream
& stream
)
2369 wxFFile
file(filename
, _T("rb"));
2370 if ( !file
.IsOpened() )
2378 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2382 stream
.write(buf
, nRead
);
2386 while ( !file
.Eof() );
2391 bool wxTransferStreamToFile(wxSTD istream
& stream
, const wxString
& filename
)
2393 wxFFile
file(filename
, _T("wb"));
2394 if ( !file
.IsOpened() )
2400 stream
.read(buf
, WXSIZEOF(buf
));
2401 if ( !stream
.bad() ) // fail may be set on EOF, don't use operator!()
2403 if ( !file
.Write(buf
, stream
.gcount()) )
2407 while ( !stream
.eof() );
2412 #else // !wxUSE_STD_IOSTREAM
2414 bool wxTransferFileToStream(const wxString
& filename
, wxOutputStream
& stream
)
2416 wxFFile
file(filename
, _T("rb"));
2417 if ( !file
.IsOpened() )
2425 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2429 stream
.Write(buf
, nRead
);
2433 while ( !file
.Eof() );
2438 bool wxTransferStreamToFile(wxInputStream
& stream
, const wxString
& filename
)
2440 wxFFile
file(filename
, _T("wb"));
2441 if ( !file
.IsOpened() )
2447 stream
.Read(buf
, WXSIZEOF(buf
));
2449 const size_t nRead
= stream
.LastRead();
2450 if ( !nRead
|| !file
.Write(buf
, nRead
) )
2453 while ( !stream
.Eof() );
2458 #endif // wxUSE_STD_IOSTREAM/!wxUSE_STD_IOSTREAM
2460 #endif // wxUSE_DOC_VIEW_ARCHITECTURE