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"
44 #include "wx/choicdlg.h"
50 #include "wx/filename.h"
53 #if wxUSE_PRINTING_ARCHITECTURE
54 #include "wx/prntbase.h"
55 #include "wx/printdlg.h"
58 #include "wx/confbase.h"
60 #include "wx/cmdproc.h"
61 #include "wx/tokenzr.h"
62 #include "wx/filename.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 wxWindow
* wxFindSuitableParent(void);
101 // ----------------------------------------------------------------------------
103 // ----------------------------------------------------------------------------
105 static const wxChar
*s_MRUEntryFormat
= wxT("&%d %s");
107 // ============================================================================
109 // ============================================================================
111 // ----------------------------------------------------------------------------
113 // ----------------------------------------------------------------------------
115 static wxString
FindExtension(const wxChar
*path
)
118 wxSplitPath(path
, NULL
, NULL
, &ext
);
120 // VZ: extensions are considered not case sensitive - is this really a good
122 return ext
.MakeLower();
125 // ----------------------------------------------------------------------------
126 // Definition of wxDocument
127 // ----------------------------------------------------------------------------
129 wxDocument::wxDocument(wxDocument
*parent
)
131 m_documentModified
= false;
132 m_documentParent
= parent
;
133 m_documentTemplate
= (wxDocTemplate
*) NULL
;
134 m_commandProcessor
= (wxCommandProcessor
*) NULL
;
138 bool wxDocument::DeleteContents()
143 wxDocument::~wxDocument()
147 if (m_commandProcessor
)
148 delete m_commandProcessor
;
150 if (GetDocumentManager())
151 GetDocumentManager()->RemoveDocument(this);
153 // Not safe to do here, since it'll invoke virtual view functions
154 // expecting to see valid derived objects: and by the time we get here,
155 // we've called destructors higher up.
159 bool wxDocument::Close()
161 if (OnSaveModified())
162 return OnCloseDocument();
167 bool wxDocument::OnCloseDocument()
169 // Tell all views that we're about to close
176 // Note that this implicitly deletes the document when the last view is
178 bool wxDocument::DeleteAllViews()
180 wxDocManager
* manager
= GetDocumentManager();
182 // first check if all views agree to be closed
183 const wxList::iterator end
= m_documentViews
.end();
184 for ( wxList::iterator i
= m_documentViews
.begin(); i
!= end
; ++i
)
186 wxView
*view
= (wxView
*)*i
;
187 if ( !view
->Close() )
191 // all views agreed to close, now do close them
192 if ( m_documentViews
.empty() )
194 // normally the document would be implicitly deleted when the last view
195 // is, but if don't have any views, do it here instead
196 if ( manager
&& manager
->GetDocuments().Member(this) )
201 // as we delete elements we iterate over, don't use the usual "from
202 // begin to end" loop
205 wxView
*view
= (wxView
*)*m_documentViews
.begin();
207 bool isLastOne
= m_documentViews
.size() == 1;
209 // this always deletes the node implicitly and if this is the last
210 // view also deletes this object itself (also implicitly, great),
211 // so we can't test for m_documentViews.empty() after calling this!
222 wxView
*wxDocument::GetFirstView() const
224 if (m_documentViews
.GetCount() == 0)
225 return (wxView
*) NULL
;
226 return (wxView
*)m_documentViews
.GetFirst()->GetData();
229 wxDocManager
*wxDocument::GetDocumentManager() const
231 return (m_documentTemplate
? m_documentTemplate
->GetDocumentManager() : (wxDocManager
*) NULL
);
234 bool wxDocument::OnNewDocument()
236 if (!OnSaveModified())
239 if (OnCloseDocument()==false) return false;
242 SetDocumentSaved(false);
245 GetDocumentManager()->MakeDefaultName(name
);
247 SetFilename(name
, true);
252 bool wxDocument::Save()
254 if (!IsModified() && m_savedYet
)
257 if ( m_documentFile
.empty() || !m_savedYet
)
260 return OnSaveDocument(m_documentFile
);
263 bool wxDocument::SaveAs()
265 wxDocTemplate
*docTemplate
= GetDocumentTemplate();
269 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
270 wxString filter
= docTemplate
->GetDescription() + wxT(" (") + docTemplate
->GetFileFilter() + wxT(")|") + docTemplate
->GetFileFilter();
272 // Now see if there are some other template with identical view and document
273 // classes, whose filters may also be used.
275 if (docTemplate
->GetViewClassInfo() && docTemplate
->GetDocClassInfo())
277 wxList::compatibility_iterator node
= docTemplate
->GetDocumentManager()->GetTemplates().GetFirst();
280 wxDocTemplate
*t
= (wxDocTemplate
*) node
->GetData();
282 if (t
->IsVisible() && t
!= docTemplate
&&
283 t
->GetViewClassInfo() == docTemplate
->GetViewClassInfo() &&
284 t
->GetDocClassInfo() == docTemplate
->GetDocClassInfo())
286 // add a '|' to separate this filter from the previous one
287 if ( !filter
.empty() )
290 filter
<< t
->GetDescription() << wxT(" (") << t
->GetFileFilter() << wxT(") |")
291 << t
->GetFileFilter();
294 node
= node
->GetNext();
298 wxString filter
= docTemplate
->GetFileFilter() ;
300 wxString tmp
= wxFileSelector(_("Save as"),
301 docTemplate
->GetDirectory(),
302 wxFileNameFromPath(GetFilename()),
303 docTemplate
->GetDefaultExtension(),
305 wxFD_SAVE
| wxFD_OVERWRITE_PROMPT
,
306 GetDocumentWindow());
311 wxString
fileName(tmp
);
312 wxString path
, name
, ext
;
313 wxSplitPath(fileName
, & path
, & name
, & ext
);
317 fileName
+= wxT(".");
318 fileName
+= docTemplate
->GetDefaultExtension();
321 SetFilename(fileName
);
322 SetTitle(wxFileNameFromPath(fileName
));
324 // Notify the views that the filename has changed
325 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
328 wxView
*view
= (wxView
*)node
->GetData();
329 view
->OnChangeFilename();
330 node
= node
->GetNext();
333 // Files that were not saved correctly are not added to the FileHistory.
334 if (!OnSaveDocument(m_documentFile
))
337 // A file that doesn't use the default extension of its document template cannot be opened
338 // via the FileHistory, so we do not add it.
339 if (docTemplate
->FileMatchesTemplate(fileName
))
341 GetDocumentManager()->AddFileToHistory(fileName
);
345 // The user will probably not be able to open the file again, so
346 // we could warn about the wrong file-extension here.
351 bool wxDocument::OnSaveDocument(const wxString
& file
)
356 if ( !DoSaveDocument(file
) )
361 SetDocumentSaved(true);
363 wxFileName
fn(file
) ;
364 fn
.MacSetDefaultTypeAndCreator() ;
369 bool wxDocument::OnOpenDocument(const wxString
& file
)
371 if (!OnSaveModified())
374 if ( !DoOpenDocument(file
) )
377 SetFilename(file
, true);
386 #if wxUSE_STD_IOSTREAM
387 wxSTD istream
& wxDocument::LoadObject(wxSTD istream
& stream
)
389 wxInputStream
& wxDocument::LoadObject(wxInputStream
& stream
)
395 #if wxUSE_STD_IOSTREAM
396 wxSTD ostream
& wxDocument::SaveObject(wxSTD ostream
& stream
)
398 wxOutputStream
& wxDocument::SaveObject(wxOutputStream
& stream
)
404 bool wxDocument::Revert()
410 // Get title, or filename if no title, else unnamed
411 bool wxDocument::GetPrintableName(wxString
& buf
) const
413 if (!m_documentTitle
.empty())
415 buf
= m_documentTitle
;
418 else if (!m_documentFile
.empty())
420 buf
= wxFileNameFromPath(m_documentFile
);
430 wxWindow
*wxDocument::GetDocumentWindow() const
432 wxView
*view
= GetFirstView();
434 return view
->GetFrame();
436 return wxTheApp
->GetTopWindow();
439 wxCommandProcessor
*wxDocument::OnCreateCommandProcessor()
441 return new wxCommandProcessor
;
444 // true if safe to close
445 bool wxDocument::OnSaveModified()
450 GetPrintableName(title
);
453 if (!wxTheApp
->GetAppName().empty())
454 msgTitle
= wxTheApp
->GetAppName();
456 msgTitle
= wxString(_("Warning"));
459 prompt
.Printf(_("Do you want to save changes to document %s?"),
460 (const wxChar
*)title
);
461 int res
= wxMessageBox(prompt
, msgTitle
,
462 wxYES_NO
|wxCANCEL
|wxICON_QUESTION
,
463 GetDocumentWindow());
469 else if (res
== wxYES
)
471 else if (res
== wxCANCEL
)
477 bool wxDocument::Draw(wxDC
& WXUNUSED(context
))
482 bool wxDocument::AddView(wxView
*view
)
484 if (!m_documentViews
.Member(view
))
486 m_documentViews
.Append(view
);
492 bool wxDocument::RemoveView(wxView
*view
)
494 (void)m_documentViews
.DeleteObject(view
);
499 bool wxDocument::OnCreate(const wxString
& WXUNUSED(path
), long flags
)
501 if (GetDocumentTemplate()->CreateView(this, flags
))
507 // Called after a view is added or removed.
508 // The default implementation deletes the document if
509 // there are no more views.
510 void wxDocument::OnChangedViewList()
512 if (m_documentViews
.GetCount() == 0)
514 if (OnSaveModified())
521 void wxDocument::UpdateAllViews(wxView
*sender
, wxObject
*hint
)
523 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
526 wxView
*view
= (wxView
*)node
->GetData();
528 view
->OnUpdate(sender
, hint
);
529 node
= node
->GetNext();
533 void wxDocument::NotifyClosing()
535 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
538 wxView
*view
= (wxView
*)node
->GetData();
539 view
->OnClosingDocument();
540 node
= node
->GetNext();
544 void wxDocument::SetFilename(const wxString
& filename
, bool notifyViews
)
546 m_documentFile
= filename
;
549 // Notify the views that the filename has changed
550 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
553 wxView
*view
= (wxView
*)node
->GetData();
554 view
->OnChangeFilename();
555 node
= node
->GetNext();
560 bool wxDocument::DoSaveDocument(const wxString
& file
)
563 if (!wxTheApp
->GetAppName().empty())
564 msgTitle
= wxTheApp
->GetAppName();
566 msgTitle
= wxString(_("File error"));
568 #if wxUSE_STD_IOSTREAM
569 wxSTD ofstream
store(file
.mb_str(), wxSTD
ios::binary
);
570 if (store
.fail() || store
.bad())
572 wxFileOutputStream
store(file
);
573 if (store
.GetLastError() != wxSTREAM_NO_ERROR
)
576 (void)wxMessageBox(_("Sorry, could not open this file for saving."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
577 GetDocumentWindow());
581 if (!SaveObject(store
))
583 (void)wxMessageBox(_("Sorry, could not save this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
584 GetDocumentWindow());
592 bool wxDocument::DoOpenDocument(const wxString
& file
)
594 #if wxUSE_STD_IOSTREAM
595 wxSTD ifstream
store(file
.mb_str(), wxSTD
ios::binary
);
596 if (!store
.fail() && !store
.bad())
598 wxFileInputStream
store(file
);
599 if (store
.GetLastError() == wxSTREAM_NO_ERROR
)
602 #if wxUSE_STD_IOSTREAM
604 if ( !!store
|| store
.eof() )
606 int res
= LoadObject(store
).GetLastError();
607 if ( res
== wxSTREAM_NO_ERROR
|| res
== wxSTREAM_EOF
)
612 wxLogError(_("Sorry, could not open this file."));
617 // ----------------------------------------------------------------------------
619 // ----------------------------------------------------------------------------
623 m_viewDocument
= (wxDocument
*) NULL
;
625 m_viewFrame
= (wxFrame
*) NULL
;
630 GetDocumentManager()->ActivateView(this, false);
631 m_viewDocument
->RemoveView(this);
634 // Extend event processing to search the document's event table
635 bool wxView::ProcessEvent(wxEvent
& event
)
637 if ( !GetDocument() || !GetDocument()->ProcessEvent(event
) )
638 return wxEvtHandler::ProcessEvent(event
);
643 void wxView::OnActivateView(bool WXUNUSED(activate
), wxView
*WXUNUSED(activeView
), wxView
*WXUNUSED(deactiveView
))
647 void wxView::OnPrint(wxDC
*dc
, wxObject
*WXUNUSED(info
))
652 void wxView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
))
656 void wxView::OnChangeFilename()
658 // GetFrame can return wxWindow rather than wxTopLevelWindow due to
659 // generic MDI implementation so use SetLabel rather than SetTitle.
660 // It should cause SetTitle() for top level windows.
661 wxWindow
*win
= GetFrame();
664 wxDocument
*doc
= GetDocument();
668 doc
->GetPrintableName(name
);
672 void wxView::SetDocument(wxDocument
*doc
)
674 m_viewDocument
= doc
;
679 bool wxView::Close(bool deleteWindow
)
681 if (OnClose(deleteWindow
))
687 void wxView::Activate(bool activate
)
689 if (GetDocument() && GetDocumentManager())
691 OnActivateView(activate
, this, GetDocumentManager()->GetCurrentView());
692 GetDocumentManager()->ActivateView(this, activate
);
696 bool wxView::OnClose(bool WXUNUSED(deleteWindow
))
698 return GetDocument() ? GetDocument()->Close() : true;
701 #if wxUSE_PRINTING_ARCHITECTURE
702 wxPrintout
*wxView::OnCreatePrintout()
704 return new wxDocPrintout(this);
706 #endif // wxUSE_PRINTING_ARCHITECTURE
708 // ----------------------------------------------------------------------------
710 // ----------------------------------------------------------------------------
712 wxDocTemplate::wxDocTemplate(wxDocManager
*manager
,
713 const wxString
& descr
,
714 const wxString
& filter
,
717 const wxString
& docTypeName
,
718 const wxString
& viewTypeName
,
719 wxClassInfo
*docClassInfo
,
720 wxClassInfo
*viewClassInfo
,
723 m_documentManager
= manager
;
724 m_description
= descr
;
727 m_fileFilter
= filter
;
729 m_docTypeName
= docTypeName
;
730 m_viewTypeName
= viewTypeName
;
731 m_documentManager
->AssociateTemplate(this);
733 m_docClassInfo
= docClassInfo
;
734 m_viewClassInfo
= viewClassInfo
;
737 wxDocTemplate::~wxDocTemplate()
739 m_documentManager
->DisassociateTemplate(this);
742 // Tries to dynamically construct an object of the right class.
743 wxDocument
*wxDocTemplate::CreateDocument(const wxString
& path
, long flags
)
745 wxDocument
*doc
= DoCreateDocument();
747 return (wxDocument
*) NULL
;
749 if (InitDocument(doc
, path
, flags
))
755 return (wxDocument
*) NULL
;
759 bool wxDocTemplate::InitDocument(wxDocument
* doc
, const wxString
& path
, long flags
)
761 doc
->SetFilename(path
);
762 doc
->SetDocumentTemplate(this);
763 GetDocumentManager()->AddDocument(doc
);
764 doc
->SetCommandProcessor(doc
->OnCreateCommandProcessor());
766 if (doc
->OnCreate(path
, flags
))
770 if (GetDocumentManager()->GetDocuments().Member(doc
))
771 doc
->DeleteAllViews();
776 wxView
*wxDocTemplate::CreateView(wxDocument
*doc
, long flags
)
778 wxView
*view
= DoCreateView();
780 return (wxView
*) NULL
;
782 view
->SetDocument(doc
);
783 if (view
->OnCreate(doc
, flags
))
790 return (wxView
*) NULL
;
794 // The default (very primitive) format detection: check is the extension is
795 // that of the template
796 bool wxDocTemplate::FileMatchesTemplate(const wxString
& path
)
798 wxStringTokenizer
parser (GetFileFilter(), wxT(";"));
799 wxString anything
= wxT ("*");
800 while (parser
.HasMoreTokens())
802 wxString filter
= parser
.GetNextToken();
803 wxString filterExt
= FindExtension (filter
);
804 if ( filter
.IsSameAs (anything
) ||
805 filterExt
.IsSameAs (anything
) ||
806 filterExt
.IsSameAs (FindExtension (path
)) )
809 return GetDefaultExtension().IsSameAs(FindExtension(path
));
812 wxDocument
*wxDocTemplate::DoCreateDocument()
815 return (wxDocument
*) NULL
;
817 return (wxDocument
*)m_docClassInfo
->CreateObject();
820 wxView
*wxDocTemplate::DoCreateView()
822 if (!m_viewClassInfo
)
823 return (wxView
*) NULL
;
825 return (wxView
*)m_viewClassInfo
->CreateObject();
828 // ----------------------------------------------------------------------------
830 // ----------------------------------------------------------------------------
832 BEGIN_EVENT_TABLE(wxDocManager
, wxEvtHandler
)
833 EVT_MENU(wxID_OPEN
, wxDocManager::OnFileOpen
)
834 EVT_MENU(wxID_CLOSE
, wxDocManager::OnFileClose
)
835 EVT_MENU(wxID_CLOSE_ALL
, wxDocManager::OnFileCloseAll
)
836 EVT_MENU(wxID_REVERT
, wxDocManager::OnFileRevert
)
837 EVT_MENU(wxID_NEW
, wxDocManager::OnFileNew
)
838 EVT_MENU(wxID_SAVE
, wxDocManager::OnFileSave
)
839 EVT_MENU(wxID_SAVEAS
, wxDocManager::OnFileSaveAs
)
840 EVT_MENU(wxID_UNDO
, wxDocManager::OnUndo
)
841 EVT_MENU(wxID_REDO
, wxDocManager::OnRedo
)
843 EVT_UPDATE_UI(wxID_OPEN
, wxDocManager::OnUpdateFileOpen
)
844 EVT_UPDATE_UI(wxID_CLOSE
, wxDocManager::OnUpdateFileClose
)
845 EVT_UPDATE_UI(wxID_CLOSE_ALL
, wxDocManager::OnUpdateFileClose
)
846 EVT_UPDATE_UI(wxID_REVERT
, wxDocManager::OnUpdateFileRevert
)
847 EVT_UPDATE_UI(wxID_NEW
, wxDocManager::OnUpdateFileNew
)
848 EVT_UPDATE_UI(wxID_SAVE
, wxDocManager::OnUpdateFileSave
)
849 EVT_UPDATE_UI(wxID_SAVEAS
, wxDocManager::OnUpdateFileSaveAs
)
850 EVT_UPDATE_UI(wxID_UNDO
, wxDocManager::OnUpdateUndo
)
851 EVT_UPDATE_UI(wxID_REDO
, wxDocManager::OnUpdateRedo
)
853 #if wxUSE_PRINTING_ARCHITECTURE
854 EVT_MENU(wxID_PRINT
, wxDocManager::OnPrint
)
855 EVT_MENU(wxID_PREVIEW
, wxDocManager::OnPreview
)
857 EVT_UPDATE_UI(wxID_PRINT
, wxDocManager::OnUpdatePrint
)
858 EVT_UPDATE_UI(wxID_PREVIEW
, wxDocManager::OnUpdatePreview
)
862 wxDocManager
* wxDocManager::sm_docManager
= (wxDocManager
*) NULL
;
864 wxDocManager::wxDocManager(long flags
, bool initialize
)
866 m_defaultDocumentNameCounter
= 1;
868 m_currentView
= (wxView
*) NULL
;
869 m_maxDocsOpen
= 10000;
870 m_fileHistory
= (wxFileHistory
*) NULL
;
873 sm_docManager
= this;
876 wxDocManager::~wxDocManager()
880 delete m_fileHistory
;
881 sm_docManager
= (wxDocManager
*) NULL
;
884 // closes the specified document
885 bool wxDocManager::CloseDocument(wxDocument
* doc
, bool force
)
887 if (doc
->Close() || force
)
889 // Implicitly deletes the document when
890 // the last view is deleted
891 doc
->DeleteAllViews();
893 // Check we're really deleted
894 if (m_docs
.Member(doc
))
902 bool wxDocManager::CloseDocuments(bool force
)
904 wxList::compatibility_iterator node
= m_docs
.GetFirst();
907 wxDocument
*doc
= (wxDocument
*)node
->GetData();
908 wxList::compatibility_iterator next
= node
->GetNext();
910 if (!CloseDocument(doc
, force
))
913 // This assumes that documents are not connected in
914 // any way, i.e. deleting one document does NOT
921 bool wxDocManager::Clear(bool force
)
923 if (!CloseDocuments(force
))
926 m_currentView
= NULL
;
928 wxList::compatibility_iterator node
= m_templates
.GetFirst();
931 wxDocTemplate
*templ
= (wxDocTemplate
*) node
->GetData();
932 wxList::compatibility_iterator next
= node
->GetNext();
939 bool wxDocManager::Initialize()
941 m_fileHistory
= OnCreateFileHistory();
945 wxFileHistory
*wxDocManager::OnCreateFileHistory()
947 return new wxFileHistory
;
950 void wxDocManager::OnFileClose(wxCommandEvent
& WXUNUSED(event
))
952 wxDocument
*doc
= GetCurrentDocument();
957 doc
->DeleteAllViews();
958 if (m_docs
.Member(doc
))
963 void wxDocManager::OnFileCloseAll(wxCommandEvent
& WXUNUSED(event
))
965 CloseDocuments(false);
968 void wxDocManager::OnFileNew(wxCommandEvent
& WXUNUSED(event
))
970 CreateDocument( wxEmptyString
, wxDOC_NEW
);
973 void wxDocManager::OnFileOpen(wxCommandEvent
& WXUNUSED(event
))
975 if ( !CreateDocument( wxEmptyString
, 0) )
981 void wxDocManager::OnFileRevert(wxCommandEvent
& WXUNUSED(event
))
983 wxDocument
*doc
= GetCurrentDocument();
989 void wxDocManager::OnFileSave(wxCommandEvent
& WXUNUSED(event
))
991 wxDocument
*doc
= GetCurrentDocument();
997 void wxDocManager::OnFileSaveAs(wxCommandEvent
& WXUNUSED(event
))
999 wxDocument
*doc
= GetCurrentDocument();
1005 void wxDocManager::OnPrint(wxCommandEvent
& WXUNUSED(event
))
1007 #if wxUSE_PRINTING_ARCHITECTURE
1008 wxView
*view
= GetCurrentView();
1012 wxPrintout
*printout
= view
->OnCreatePrintout();
1016 printer
.Print(view
->GetFrame(), printout
, true);
1020 #endif // wxUSE_PRINTING_ARCHITECTURE
1023 void wxDocManager::OnPreview(wxCommandEvent
& WXUNUSED(event
))
1025 #if wxUSE_PRINTING_ARCHITECTURE
1026 wxView
*view
= GetCurrentView();
1030 wxPrintout
*printout
= view
->OnCreatePrintout();
1033 // Pass two printout objects: for preview, and possible printing.
1034 wxPrintPreviewBase
*preview
= new wxPrintPreview(printout
, view
->OnCreatePrintout());
1035 if ( !preview
->Ok() )
1038 wxMessageBox( _("Sorry, print preview needs a printer to be installed.") );
1042 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, (wxFrame
*)wxTheApp
->GetTopWindow(), _("Print Preview"),
1043 wxPoint(100, 100), wxSize(600, 650));
1044 frame
->Centre(wxBOTH
);
1045 frame
->Initialize();
1048 #endif // wxUSE_PRINTING_ARCHITECTURE
1051 void wxDocManager::OnUndo(wxCommandEvent
& event
)
1053 wxDocument
*doc
= GetCurrentDocument();
1056 if (doc
->GetCommandProcessor())
1057 doc
->GetCommandProcessor()->Undo();
1062 void wxDocManager::OnRedo(wxCommandEvent
& event
)
1064 wxDocument
*doc
= GetCurrentDocument();
1067 if (doc
->GetCommandProcessor())
1068 doc
->GetCommandProcessor()->Redo();
1073 // Handlers for UI update commands
1075 void wxDocManager::OnUpdateFileOpen(wxUpdateUIEvent
& event
)
1077 event
.Enable( true );
1080 void wxDocManager::OnUpdateFileClose(wxUpdateUIEvent
& event
)
1082 wxDocument
*doc
= GetCurrentDocument();
1083 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1086 void wxDocManager::OnUpdateFileRevert(wxUpdateUIEvent
& event
)
1088 wxDocument
*doc
= GetCurrentDocument();
1089 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1092 void wxDocManager::OnUpdateFileNew(wxUpdateUIEvent
& event
)
1094 event
.Enable( true );
1097 void wxDocManager::OnUpdateFileSave(wxUpdateUIEvent
& event
)
1099 wxDocument
*doc
= GetCurrentDocument();
1100 event
.Enable( doc
&& doc
->IsModified() );
1103 void wxDocManager::OnUpdateFileSaveAs(wxUpdateUIEvent
& event
)
1105 wxDocument
*doc
= GetCurrentDocument();
1106 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1109 void wxDocManager::OnUpdateUndo(wxUpdateUIEvent
& event
)
1111 wxDocument
*doc
= GetCurrentDocument();
1113 event
.Enable(false);
1114 else if (!doc
->GetCommandProcessor())
1118 event
.Enable( doc
->GetCommandProcessor()->CanUndo() );
1119 doc
->GetCommandProcessor()->SetMenuStrings();
1123 void wxDocManager::OnUpdateRedo(wxUpdateUIEvent
& event
)
1125 wxDocument
*doc
= GetCurrentDocument();
1127 event
.Enable(false);
1128 else if (!doc
->GetCommandProcessor())
1132 event
.Enable( doc
->GetCommandProcessor()->CanRedo() );
1133 doc
->GetCommandProcessor()->SetMenuStrings();
1137 void wxDocManager::OnUpdatePrint(wxUpdateUIEvent
& event
)
1139 wxDocument
*doc
= GetCurrentDocument();
1140 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1143 void wxDocManager::OnUpdatePreview(wxUpdateUIEvent
& event
)
1145 wxDocument
*doc
= GetCurrentDocument();
1146 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1149 wxView
*wxDocManager::GetCurrentView() const
1152 return m_currentView
;
1153 if (m_docs
.GetCount() == 1)
1155 wxDocument
* doc
= (wxDocument
*) m_docs
.GetFirst()->GetData();
1156 return doc
->GetFirstView();
1158 return (wxView
*) NULL
;
1161 // Extend event processing to search the view's event table
1162 bool wxDocManager::ProcessEvent(wxEvent
& event
)
1164 wxView
* view
= GetCurrentView();
1167 if (view
->ProcessEvent(event
))
1170 return wxEvtHandler::ProcessEvent(event
);
1173 wxDocument
*wxDocManager::CreateDocument(const wxString
& path
, long flags
)
1175 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.GetCount()];
1178 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1180 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Item(i
)->GetData());
1181 if (temp
->IsVisible())
1183 templates
[n
] = temp
;
1190 return (wxDocument
*) NULL
;
1193 wxDocument
* docToClose
= NULL
;
1195 // If we've reached the max number of docs, close the
1197 if ( (int)GetDocuments().GetCount() >= m_maxDocsOpen
)
1199 wxDocument
*doc
= (wxDocument
*)GetDocuments().GetFirst()->GetData();
1203 // New document: user chooses a template, unless there's only one.
1204 if (flags
& wxDOC_NEW
)
1210 if (!CloseDocument(docToClose
, false))
1217 wxDocTemplate
*temp
= templates
[0];
1219 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
1223 newDoc
->SetDocumentName(temp
->GetDocumentName());
1224 newDoc
->SetDocumentTemplate(temp
);
1225 if (!newDoc
->OnNewDocument() )
1227 // Document is implicitly deleted by DeleteAllViews
1228 newDoc
->DeleteAllViews();
1235 wxDocTemplate
*temp
= SelectDocumentType(templates
, n
);
1241 if (!CloseDocument(docToClose
, false))
1247 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
1251 newDoc
->SetDocumentName(temp
->GetDocumentName());
1252 newDoc
->SetDocumentTemplate(temp
);
1253 if (!newDoc
->OnNewDocument() )
1255 // Document is implicitly deleted by DeleteAllViews
1256 newDoc
->DeleteAllViews();
1263 return (wxDocument
*) NULL
;
1266 // Existing document
1267 wxDocTemplate
*temp
;
1269 wxString path2
= path
;
1271 if (flags
& wxDOC_SILENT
)
1273 temp
= FindTemplateForPath(path2
);
1276 // Since we do not add files with non-default extensions to the FileHistory this
1277 // can only happen if the application changes the allowed templates in runtime.
1278 (void)wxMessageBox(_("Sorry, the format for this file is unknown."),
1280 wxOK
| wxICON_EXCLAMATION
, wxFindSuitableParent());
1284 temp
= SelectDocumentPath(templates
, n
, path2
, flags
);
1292 if (!CloseDocument(docToClose
, false))
1298 //see if this file is already open
1299 for (size_t i
= 0; i
< GetDocuments().GetCount(); ++i
)
1301 wxDocument
* currentDoc
= (wxDocument
*)(GetDocuments().Item(i
)->GetData());
1303 //file paths are case-insensitive on Windows
1304 if (path2
.CmpNoCase(currentDoc
->GetFilename()) == 0)
1306 if (path2
.Cmp(currentDoc
->GetFilename()) == 0)
1309 //file already open. Just activate it and return
1310 if (currentDoc
->GetFirstView())
1312 ActivateView(currentDoc
->GetFirstView(), true);
1313 if (currentDoc
->GetDocumentWindow())
1314 currentDoc
->GetDocumentWindow()->SetFocus();
1320 wxDocument
*newDoc
= temp
->CreateDocument(path2
, flags
);
1323 newDoc
->SetDocumentName(temp
->GetDocumentName());
1324 newDoc
->SetDocumentTemplate(temp
);
1325 if (!newDoc
->OnOpenDocument(path2
))
1327 newDoc
->DeleteAllViews();
1328 // delete newDoc; // Implicitly deleted by DeleteAllViews
1329 return (wxDocument
*) NULL
;
1331 // A file that doesn't use the default extension of its document
1332 // template cannot be opened via the FileHistory, so we do not
1334 if (temp
->FileMatchesTemplate(path2
))
1335 AddFileToHistory(path2
);
1340 return (wxDocument
*) NULL
;
1343 wxView
*wxDocManager::CreateView(wxDocument
*doc
, long flags
)
1345 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.GetCount()];
1348 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1350 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Item(i
)->GetData());
1351 if (temp
->IsVisible())
1353 if (temp
->GetDocumentName() == doc
->GetDocumentName())
1355 templates
[n
] = temp
;
1363 return (wxView
*) NULL
;
1367 wxDocTemplate
*temp
= templates
[0];
1369 wxView
*view
= temp
->CreateView(doc
, flags
);
1371 view
->SetViewName(temp
->GetViewName());
1375 wxDocTemplate
*temp
= SelectViewType(templates
, n
);
1379 wxView
*view
= temp
->CreateView(doc
, flags
);
1381 view
->SetViewName(temp
->GetViewName());
1385 return (wxView
*) NULL
;
1388 // Not yet implemented
1389 void wxDocManager::DeleteTemplate(wxDocTemplate
*WXUNUSED(temp
), long WXUNUSED(flags
))
1393 // Not yet implemented
1394 bool wxDocManager::FlushDoc(wxDocument
*WXUNUSED(doc
))
1399 wxDocument
*wxDocManager::GetCurrentDocument() const
1401 wxView
*view
= GetCurrentView();
1403 return view
->GetDocument();
1405 return (wxDocument
*) NULL
;
1408 // Make a default document name
1409 bool wxDocManager::MakeDefaultName(wxString
& name
)
1411 name
.Printf(_("unnamed%d"), m_defaultDocumentNameCounter
);
1412 m_defaultDocumentNameCounter
++;
1417 // Make a frame title (override this to do something different)
1418 // If docName is empty, a document is not currently active.
1419 wxString
wxDocManager::MakeFrameTitle(wxDocument
* doc
)
1421 wxString appName
= wxTheApp
->GetAppName();
1428 doc
->GetPrintableName(docName
);
1429 title
= docName
+ wxString(_(" - ")) + appName
;
1435 // Not yet implemented
1436 wxDocTemplate
*wxDocManager::MatchTemplate(const wxString
& WXUNUSED(path
))
1438 return (wxDocTemplate
*) NULL
;
1441 // File history management
1442 void wxDocManager::AddFileToHistory(const wxString
& file
)
1445 m_fileHistory
->AddFileToHistory(file
);
1448 void wxDocManager::RemoveFileFromHistory(size_t i
)
1451 m_fileHistory
->RemoveFileFromHistory(i
);
1454 wxString
wxDocManager::GetHistoryFile(size_t i
) const
1459 histFile
= m_fileHistory
->GetHistoryFile(i
);
1464 void wxDocManager::FileHistoryUseMenu(wxMenu
*menu
)
1467 m_fileHistory
->UseMenu(menu
);
1470 void wxDocManager::FileHistoryRemoveMenu(wxMenu
*menu
)
1473 m_fileHistory
->RemoveMenu(menu
);
1477 void wxDocManager::FileHistoryLoad(wxConfigBase
& config
)
1480 m_fileHistory
->Load(config
);
1483 void wxDocManager::FileHistorySave(wxConfigBase
& config
)
1486 m_fileHistory
->Save(config
);
1490 void wxDocManager::FileHistoryAddFilesToMenu(wxMenu
* menu
)
1493 m_fileHistory
->AddFilesToMenu(menu
);
1496 void wxDocManager::FileHistoryAddFilesToMenu()
1499 m_fileHistory
->AddFilesToMenu();
1502 size_t wxDocManager::GetHistoryFilesCount() const
1504 return m_fileHistory
? m_fileHistory
->GetCount() : 0;
1508 // Find out the document template via matching in the document file format
1509 // against that of the template
1510 wxDocTemplate
*wxDocManager::FindTemplateForPath(const wxString
& path
)
1512 wxDocTemplate
*theTemplate
= (wxDocTemplate
*) NULL
;
1514 // Find the template which this extension corresponds to
1515 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1517 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Item(i
)->GetData();
1518 if ( temp
->FileMatchesTemplate(path
) )
1527 // Try to get a more suitable parent frame than the top window,
1528 // for selection dialogs. Otherwise you may get an unexpected
1529 // window being activated when a dialog is shown.
1530 static wxWindow
* wxFindSuitableParent()
1532 wxWindow
* parent
= wxTheApp
->GetTopWindow();
1534 wxWindow
* focusWindow
= wxWindow::FindFocus();
1537 while (focusWindow
&&
1538 !focusWindow
->IsKindOf(CLASSINFO(wxDialog
)) &&
1539 !focusWindow
->IsKindOf(CLASSINFO(wxFrame
)))
1541 focusWindow
= focusWindow
->GetParent();
1544 parent
= focusWindow
;
1549 // Prompts user to open a file, using file specs in templates.
1550 // Must extend the file selector dialog or implement own; OR
1551 // match the extension to the template extension.
1553 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**templates
,
1554 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
1557 int WXUNUSED(noTemplates
),
1560 long WXUNUSED(flags
),
1561 bool WXUNUSED(save
))
1563 // We can only have multiple filters in Windows and GTK
1564 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
1568 for (i
= 0; i
< noTemplates
; i
++)
1570 if (templates
[i
]->IsVisible())
1572 // add a '|' to separate this filter from the previous one
1573 if ( !descrBuf
.empty() )
1574 descrBuf
<< wxT('|');
1576 descrBuf
<< templates
[i
]->GetDescription()
1577 << wxT(" (") << templates
[i
]->GetFileFilter() << wxT(") |")
1578 << templates
[i
]->GetFileFilter();
1582 wxString descrBuf
= wxT("*.*");
1585 int FilterIndex
= -1;
1587 wxWindow
* parent
= wxFindSuitableParent();
1589 wxString pathTmp
= wxFileSelectorEx(_("Select a file"),
1597 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)NULL
;
1598 if (!pathTmp
.empty())
1600 if (!wxFileExists(pathTmp
))
1603 if (!wxTheApp
->GetAppName().empty())
1604 msgTitle
= wxTheApp
->GetAppName();
1606 msgTitle
= wxString(_("File error"));
1608 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
1611 path
= wxEmptyString
;
1612 return (wxDocTemplate
*) NULL
;
1614 m_lastDirectory
= wxPathOnly(pathTmp
);
1618 // first choose the template using the extension, if this fails (i.e.
1619 // wxFileSelectorEx() didn't fill it), then use the path
1620 if ( FilterIndex
!= -1 )
1621 theTemplate
= templates
[FilterIndex
];
1623 theTemplate
= FindTemplateForPath(path
);
1626 // Since we do not add files with non-default extensions to the FileHistory this
1627 // can only happen if the application changes the allowed templates in runtime.
1628 (void)wxMessageBox(_("Sorry, the format for this file is unknown."),
1630 wxOK
| wxICON_EXCLAMATION
, wxFindSuitableParent());
1635 path
= wxEmptyString
;
1641 wxDocTemplate
*wxDocManager::SelectDocumentType(wxDocTemplate
**templates
,
1642 int noTemplates
, bool sort
)
1644 wxArrayString strings
;
1645 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1649 for (i
= 0; i
< noTemplates
; i
++)
1651 if (templates
[i
]->IsVisible())
1655 for (j
= 0; j
< n
; j
++)
1657 //filter out NOT unique documents + view combinations
1658 if ( templates
[i
]->m_docTypeName
== data
[j
]->m_docTypeName
&&
1659 templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
1666 strings
.Add(templates
[i
]->m_description
);
1668 data
[n
] = templates
[i
];
1676 strings
.Sort(); // ascending sort
1677 // Yes, this will be slow, but template lists
1678 // are typically short.
1680 n
= strings
.Count();
1681 for (i
= 0; i
< n
; i
++)
1683 for (j
= 0; j
< noTemplates
; j
++)
1685 if (strings
[i
] == templates
[j
]->m_description
)
1686 data
[i
] = templates
[j
];
1691 wxDocTemplate
*theTemplate
;
1696 // no visible templates, hence nothing to choose from
1701 // don't propose the user to choose if he heas no choice
1702 theTemplate
= data
[0];
1706 // propose the user to choose one of several
1707 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1709 _("Select a document template"),
1713 wxFindSuitableParent()
1722 wxDocTemplate
*wxDocManager::SelectViewType(wxDocTemplate
**templates
,
1723 int noTemplates
, bool sort
)
1725 wxArrayString strings
;
1726 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1730 for (i
= 0; i
< noTemplates
; i
++)
1732 wxDocTemplate
*templ
= templates
[i
];
1733 if ( templ
->IsVisible() && !templ
->GetViewName().empty() )
1737 for (j
= 0; j
< n
; j
++)
1739 //filter out NOT unique views
1740 if ( templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
)
1746 strings
.Add(templ
->m_viewTypeName
);
1755 strings
.Sort(); // ascending sort
1756 // Yes, this will be slow, but template lists
1757 // are typically short.
1759 n
= strings
.Count();
1760 for (i
= 0; i
< n
; i
++)
1762 for (j
= 0; j
< noTemplates
; j
++)
1764 if (strings
[i
] == templates
[j
]->m_viewTypeName
)
1765 data
[i
] = templates
[j
];
1770 wxDocTemplate
*theTemplate
;
1772 // the same logic as above
1776 theTemplate
= (wxDocTemplate
*)NULL
;
1780 theTemplate
= data
[0];
1784 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1786 _("Select a document view"),
1790 wxFindSuitableParent()
1799 void wxDocManager::AssociateTemplate(wxDocTemplate
*temp
)
1801 if (!m_templates
.Member(temp
))
1802 m_templates
.Append(temp
);
1805 void wxDocManager::DisassociateTemplate(wxDocTemplate
*temp
)
1807 m_templates
.DeleteObject(temp
);
1810 // Add and remove a document from the manager's list
1811 void wxDocManager::AddDocument(wxDocument
*doc
)
1813 if (!m_docs
.Member(doc
))
1817 void wxDocManager::RemoveDocument(wxDocument
*doc
)
1819 m_docs
.DeleteObject(doc
);
1822 // Views or windows should inform the document manager
1823 // when a view is going in or out of focus
1824 void wxDocManager::ActivateView(wxView
*view
, bool activate
)
1828 m_currentView
= view
;
1832 if ( m_currentView
== view
)
1834 // don't keep stale pointer
1835 m_currentView
= (wxView
*) NULL
;
1840 // ----------------------------------------------------------------------------
1841 // Default document child frame
1842 // ----------------------------------------------------------------------------
1844 BEGIN_EVENT_TABLE(wxDocChildFrame
, wxFrame
)
1845 EVT_ACTIVATE(wxDocChildFrame::OnActivate
)
1846 EVT_CLOSE(wxDocChildFrame::OnCloseWindow
)
1849 wxDocChildFrame::wxDocChildFrame(wxDocument
*doc
,
1853 const wxString
& title
,
1857 const wxString
& name
)
1858 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1860 m_childDocument
= doc
;
1863 view
->SetFrame(this);
1866 // Extend event processing to search the view's event table
1867 bool wxDocChildFrame::ProcessEvent(wxEvent
& event
)
1870 m_childView
->Activate(true);
1872 if ( !m_childView
|| ! m_childView
->ProcessEvent(event
) )
1874 // Only hand up to the parent if it's a menu command
1875 if (!event
.IsKindOf(CLASSINFO(wxCommandEvent
)) || !GetParent() || !GetParent()->ProcessEvent(event
))
1876 return wxEvtHandler::ProcessEvent(event
);
1884 void wxDocChildFrame::OnActivate(wxActivateEvent
& event
)
1886 wxFrame::OnActivate(event
);
1889 m_childView
->Activate(event
.GetActive());
1892 void wxDocChildFrame::OnCloseWindow(wxCloseEvent
& event
)
1896 bool ans
= event
.CanVeto()
1897 ? m_childView
->Close(false) // false means don't delete associated window
1898 : true; // Must delete.
1902 m_childView
->Activate(false);
1904 m_childView
= (wxView
*) NULL
;
1905 m_childDocument
= (wxDocument
*) NULL
;
1916 // ----------------------------------------------------------------------------
1917 // Default parent frame
1918 // ----------------------------------------------------------------------------
1920 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1921 EVT_MENU(wxID_EXIT
, wxDocParentFrame::OnExit
)
1922 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE9
, wxDocParentFrame::OnMRUFile
)
1923 EVT_CLOSE(wxDocParentFrame::OnCloseWindow
)
1926 wxDocParentFrame::wxDocParentFrame()
1928 m_docManager
= NULL
;
1931 wxDocParentFrame::wxDocParentFrame(wxDocManager
*manager
,
1934 const wxString
& title
,
1938 const wxString
& name
)
1939 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1941 m_docManager
= manager
;
1944 bool wxDocParentFrame::Create(wxDocManager
*manager
,
1947 const wxString
& title
,
1951 const wxString
& name
)
1953 m_docManager
= manager
;
1954 return base_type::Create(frame
, id
, title
, pos
, size
, style
, name
);
1957 void wxDocParentFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
1962 void wxDocParentFrame::OnMRUFile(wxCommandEvent
& event
)
1964 int n
= event
.GetId() - wxID_FILE1
; // the index in MRU list
1965 wxString
filename(m_docManager
->GetHistoryFile(n
));
1966 if ( !filename
.empty() )
1968 // verify that the file exists before doing anything else
1969 if ( wxFile::Exists(filename
) )
1972 if (!m_docManager
->CreateDocument(filename
, wxDOC_SILENT
))
1974 // remove the file from the MRU list. The user should already be notified.
1975 m_docManager
->RemoveFileFromHistory(n
);
1977 wxLogError(_("The file '%s' couldn't be opened.\nIt has been removed from the most recently used files list."),
1983 // remove the bogus filename from the MRU list and notify the user
1985 m_docManager
->RemoveFileFromHistory(n
);
1987 wxLogError(_("The file '%s' doesn't exist and couldn't be opened.\nIt has been removed from the most recently used files list."),
1993 // Extend event processing to search the view's event table
1994 bool wxDocParentFrame::ProcessEvent(wxEvent
& event
)
1996 // Try the document manager, then do default processing
1997 if (!m_docManager
|| !m_docManager
->ProcessEvent(event
))
1998 return wxEvtHandler::ProcessEvent(event
);
2003 // Define the behaviour for the frame closing
2004 // - must delete all frames except for the main one.
2005 void wxDocParentFrame::OnCloseWindow(wxCloseEvent
& event
)
2007 if (m_docManager
->Clear(!event
.CanVeto()))
2015 #if wxUSE_PRINTING_ARCHITECTURE
2017 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
)
2020 m_printoutView
= view
;
2023 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
2027 // Get the logical pixels per inch of screen and printer
2028 int ppiScreenX
, ppiScreenY
;
2029 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
2030 wxUnusedVar(ppiScreenY
);
2031 int ppiPrinterX
, ppiPrinterY
;
2032 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
2033 wxUnusedVar(ppiPrinterY
);
2035 // This scales the DC so that the printout roughly represents the
2036 // the screen scaling. The text point size _should_ be the right size
2037 // but in fact is too small for some reason. This is a detail that will
2038 // need to be addressed at some point but can be fudged for the
2040 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
2042 // Now we have to check in case our real page size is reduced
2043 // (e.g. because we're drawing to a print preview memory DC)
2044 int pageWidth
, pageHeight
;
2046 dc
->GetSize(&w
, &h
);
2047 GetPageSizePixels(&pageWidth
, &pageHeight
);
2048 wxUnusedVar(pageHeight
);
2050 // If printer pageWidth == current DC width, then this doesn't
2051 // change. But w might be the preview bitmap width, so scale down.
2052 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
2053 dc
->SetUserScale(overallScale
, overallScale
);
2057 m_printoutView
->OnDraw(dc
);
2062 bool wxDocPrintout::HasPage(int pageNum
)
2064 return (pageNum
== 1);
2067 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
2069 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
2075 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
2083 #endif // wxUSE_PRINTING_ARCHITECTURE
2085 // ----------------------------------------------------------------------------
2086 // File history processor
2087 // ----------------------------------------------------------------------------
2089 wxFileHistory::wxFileHistory(size_t maxFiles
, wxWindowID idBase
)
2091 m_fileMaxFiles
= maxFiles
;
2095 wxFileHistory::~wxFileHistory()
2099 void wxFileHistory::AddFileToHistory(const wxString
& file
)
2101 wxFileName
fn(file
);
2104 // Check we don't already have this file
2105 for (i
= 0; i
< m_fileHistory
.GetCount(); i
++)
2107 // we need to do a comparison using wxFileNames because it knows
2108 // how to exactly compare files on the different platforms
2109 // (e.g. handle case [in]sensitive filesystems)
2110 if ( fn
== wxFileName(m_fileHistory
[i
]) )
2112 // we do have it, move it to the top of the history
2113 RemoveFileFromHistory (i
);
2114 AddFileToHistory (file
);
2119 // if we already have a full history, delete the one at the end
2120 if ( m_fileMaxFiles
== m_fileHistory
.GetCount() )
2122 RemoveFileFromHistory (m_fileHistory
.GetCount() - 1);
2123 AddFileToHistory (file
);
2127 // Add to the project file history:
2128 // Move existing files (if any) down so we can insert file at beginning.
2129 if (m_fileHistory
.GetCount() < m_fileMaxFiles
)
2131 wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2134 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2135 if ( m_fileHistory
.IsEmpty() && menu
->GetMenuItemCount() )
2137 menu
->AppendSeparator();
2139 menu
->Append(m_idBase
+m_fileHistory
.GetCount(), _("[EMPTY]"));
2140 node
= node
->GetNext();
2144 m_fileHistory
.Insert(file
, 0);
2146 // this is the directory of the last opened file
2147 wxString pathCurrent
;
2148 wxSplitPath( m_fileHistory
[0], &pathCurrent
, NULL
, NULL
);
2149 for (i
= 0; i
< m_fileHistory
.GetCount(); i
++)
2151 // if in same directory just show the filename; otherwise the full
2153 wxString pathInMenu
, path
, filename
, ext
;
2154 wxSplitPath( m_fileHistory
[i
], &path
, &filename
, &ext
);
2155 if ( path
== pathCurrent
)
2157 pathInMenu
= filename
;
2159 pathInMenu
= pathInMenu
+ wxFILE_SEP_EXT
+ ext
;
2163 // absolute path; could also set relative path
2164 pathInMenu
= m_fileHistory
[i
];
2167 // we need to quote '&' characters which are used for mnemonics
2168 pathInMenu
.Replace(_T("&"), _T("&&"));
2171 buf
.Printf(s_MRUEntryFormat
, i
+ 1, pathInMenu
.c_str());
2173 wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2176 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2177 menu
->SetLabel(m_idBase
+ i
, buf
);
2178 node
= node
->GetNext();
2183 void wxFileHistory::RemoveFileFromHistory(size_t i
)
2185 wxCHECK_RET( i
< m_fileHistory
.GetCount(),
2186 wxT("invalid index in wxFileHistory::RemoveFileFromHistory") );
2188 // delete the element from the array
2189 m_fileHistory
.RemoveAt(i
);
2191 wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2194 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2196 // shuffle filenames up
2198 for ( size_t j
= i
; j
< m_fileHistory
.GetCount(); j
++ )
2200 buf
.Printf(s_MRUEntryFormat
, j
+ 1, m_fileHistory
[j
].c_str());
2201 menu
->SetLabel(m_idBase
+ j
, buf
);
2204 node
= node
->GetNext();
2206 // delete the last menu item which is unused now
2207 wxWindowID lastItemId
= m_idBase
+ wx_truncate_cast(wxWindowID
, m_fileHistory
.GetCount());
2208 if (menu
->FindItem(lastItemId
))
2210 menu
->Delete(lastItemId
);
2213 // delete the last separator too if no more files are left
2214 if ( m_fileHistory
.GetCount() == 0 )
2216 wxMenuItemList::compatibility_iterator nodeLast
= menu
->GetMenuItems().GetLast();
2219 wxMenuItem
*menuItem
= nodeLast
->GetData();
2220 if ( menuItem
->IsSeparator() )
2222 menu
->Delete(menuItem
);
2224 //else: should we search backwards for the last separator?
2226 //else: menu is empty somehow
2231 void wxFileHistory::UseMenu(wxMenu
*menu
)
2233 if (!m_fileMenus
.Member(menu
))
2234 m_fileMenus
.Append(menu
);
2237 void wxFileHistory::RemoveMenu(wxMenu
*menu
)
2239 m_fileMenus
.DeleteObject(menu
);
2243 void wxFileHistory::Load(wxConfigBase
& config
)
2245 m_fileHistory
.Clear();
2248 buf
.Printf(wxT("file%d"), 1);
2250 wxString historyFile
;
2251 while ((m_fileHistory
.GetCount() < m_fileMaxFiles
) &&
2252 config
.Read(buf
, &historyFile
) && !historyFile
.empty())
2254 m_fileHistory
.Add(historyFile
);
2256 buf
.Printf(wxT("file%d"), (int)m_fileHistory
.GetCount()+1);
2257 historyFile
= wxEmptyString
;
2263 void wxFileHistory::Save(wxConfigBase
& config
)
2266 for (i
= 0; i
< m_fileMaxFiles
; i
++)
2269 buf
.Printf(wxT("file%d"), (int)i
+1);
2270 if (i
< m_fileHistory
.GetCount())
2271 config
.Write(buf
, wxString(m_fileHistory
[i
]));
2273 config
.Write(buf
, wxEmptyString
);
2276 #endif // wxUSE_CONFIG
2278 void wxFileHistory::AddFilesToMenu()
2280 if (m_fileHistory
.GetCount() > 0)
2282 wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2285 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2286 if (menu
->GetMenuItemCount())
2288 menu
->AppendSeparator();
2292 for (i
= 0; i
< m_fileHistory
.GetCount(); i
++)
2295 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
].c_str());
2296 menu
->Append(m_idBase
+i
, buf
);
2298 node
= node
->GetNext();
2303 void wxFileHistory::AddFilesToMenu(wxMenu
* menu
)
2305 if (m_fileHistory
.GetCount() > 0)
2307 if (menu
->GetMenuItemCount())
2309 menu
->AppendSeparator();
2313 for (i
= 0; i
< m_fileHistory
.GetCount(); i
++)
2316 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
].c_str());
2317 menu
->Append(m_idBase
+i
, buf
);
2322 // ----------------------------------------------------------------------------
2323 // Permits compatibility with existing file formats and functions that
2324 // manipulate files directly
2325 // ----------------------------------------------------------------------------
2327 #if wxUSE_STD_IOSTREAM
2329 bool wxTransferFileToStream(const wxString
& filename
, wxSTD ostream
& stream
)
2331 wxFFile
file(filename
, _T("rb"));
2332 if ( !file
.IsOpened() )
2340 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2344 stream
.write(buf
, nRead
);
2348 while ( !file
.Eof() );
2353 bool wxTransferStreamToFile(wxSTD istream
& stream
, const wxString
& filename
)
2355 wxFFile
file(filename
, _T("wb"));
2356 if ( !file
.IsOpened() )
2362 stream
.read(buf
, WXSIZEOF(buf
));
2363 if ( !stream
.bad() ) // fail may be set on EOF, don't use operator!()
2365 if ( !file
.Write(buf
, stream
.gcount()) )
2369 while ( !stream
.eof() );
2374 #else // !wxUSE_STD_IOSTREAM
2376 bool wxTransferFileToStream(const wxString
& filename
, wxOutputStream
& stream
)
2378 wxFFile
file(filename
, _T("rb"));
2379 if ( !file
.IsOpened() )
2387 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2391 stream
.Write(buf
, nRead
);
2395 while ( !file
.Eof() );
2400 bool wxTransferStreamToFile(wxInputStream
& stream
, const wxString
& filename
)
2402 wxFFile
file(filename
, _T("wb"));
2403 if ( !file
.IsOpened() )
2409 stream
.Read(buf
, WXSIZEOF(buf
));
2411 const size_t nRead
= stream
.LastRead();
2412 if ( !nRead
|| !file
.Write(buf
, nRead
) )
2415 while ( !stream
.Eof() );
2420 #endif // wxUSE_STD_IOSTREAM/!wxUSE_STD_IOSTREAM
2422 #endif // wxUSE_DOC_VIEW_ARCHITECTURE