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"
66 #if wxUSE_STD_IOSTREAM
67 #include "wx/ioswrap.h"
74 #include "wx/wfstream.h"
77 // ----------------------------------------------------------------------------
79 // ----------------------------------------------------------------------------
81 IMPLEMENT_ABSTRACT_CLASS(wxDocument
, wxEvtHandler
)
82 IMPLEMENT_ABSTRACT_CLASS(wxView
, wxEvtHandler
)
83 IMPLEMENT_ABSTRACT_CLASS(wxDocTemplate
, wxObject
)
84 IMPLEMENT_DYNAMIC_CLASS(wxDocManager
, wxEvtHandler
)
85 IMPLEMENT_CLASS(wxDocChildFrame
, wxFrame
)
86 IMPLEMENT_CLASS(wxDocParentFrame
, wxFrame
)
88 #if wxUSE_PRINTING_ARCHITECTURE
89 IMPLEMENT_DYNAMIC_CLASS(wxDocPrintout
, wxPrintout
)
92 IMPLEMENT_DYNAMIC_CLASS(wxFileHistory
, wxObject
)
94 // ----------------------------------------------------------------------------
95 // function prototypes
96 // ----------------------------------------------------------------------------
98 static inline wxString
FindExtension(const wxChar
*path
);
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
= wxDocManager::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(wxDocManager
*manager
,
1929 const wxString
& title
,
1933 const wxString
& name
)
1934 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1936 m_docManager
= manager
;
1939 void wxDocParentFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
1944 void wxDocParentFrame::OnMRUFile(wxCommandEvent
& event
)
1946 int n
= event
.GetId() - wxID_FILE1
; // the index in MRU list
1947 wxString
filename(m_docManager
->GetHistoryFile(n
));
1948 if ( !filename
.empty() )
1950 // verify that the file exists before doing anything else
1951 if ( wxFile::Exists(filename
) )
1954 if (!m_docManager
->CreateDocument(filename
, wxDOC_SILENT
))
1956 // remove the file from the MRU list. The user should already be notified.
1957 m_docManager
->RemoveFileFromHistory(n
);
1959 wxLogError(_("The file '%s' couldn't be opened.\nIt has been removed from the most recently used files list."),
1965 // remove the bogus filename from the MRU list and notify the user
1967 m_docManager
->RemoveFileFromHistory(n
);
1969 wxLogError(_("The file '%s' doesn't exist and couldn't be opened.\nIt has been removed from the most recently used files list."),
1975 // Extend event processing to search the view's event table
1976 bool wxDocParentFrame::ProcessEvent(wxEvent
& event
)
1978 // Try the document manager, then do default processing
1979 if (!m_docManager
|| !m_docManager
->ProcessEvent(event
))
1980 return wxEvtHandler::ProcessEvent(event
);
1985 // Define the behaviour for the frame closing
1986 // - must delete all frames except for the main one.
1987 void wxDocParentFrame::OnCloseWindow(wxCloseEvent
& event
)
1989 if (m_docManager
->Clear(!event
.CanVeto()))
1997 #if wxUSE_PRINTING_ARCHITECTURE
1999 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
)
2002 m_printoutView
= view
;
2005 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
2009 // Get the logical pixels per inch of screen and printer
2010 int ppiScreenX
, ppiScreenY
;
2011 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
2012 wxUnusedVar(ppiScreenY
);
2013 int ppiPrinterX
, ppiPrinterY
;
2014 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
2015 wxUnusedVar(ppiPrinterY
);
2017 // This scales the DC so that the printout roughly represents the
2018 // the screen scaling. The text point size _should_ be the right size
2019 // but in fact is too small for some reason. This is a detail that will
2020 // need to be addressed at some point but can be fudged for the
2022 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
2024 // Now we have to check in case our real page size is reduced
2025 // (e.g. because we're drawing to a print preview memory DC)
2026 int pageWidth
, pageHeight
;
2028 dc
->GetSize(&w
, &h
);
2029 GetPageSizePixels(&pageWidth
, &pageHeight
);
2030 wxUnusedVar(pageHeight
);
2032 // If printer pageWidth == current DC width, then this doesn't
2033 // change. But w might be the preview bitmap width, so scale down.
2034 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
2035 dc
->SetUserScale(overallScale
, overallScale
);
2039 m_printoutView
->OnDraw(dc
);
2044 bool wxDocPrintout::HasPage(int pageNum
)
2046 return (pageNum
== 1);
2049 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
2051 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
2057 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
2065 #endif // wxUSE_PRINTING_ARCHITECTURE
2067 // ----------------------------------------------------------------------------
2068 // File history processor
2069 // ----------------------------------------------------------------------------
2071 static inline wxChar
* MYcopystring(const wxString
& s
)
2073 wxChar
* copy
= new wxChar
[s
.length() + 1];
2074 return wxStrcpy(copy
, s
.c_str());
2077 static inline wxChar
* MYcopystring(const wxChar
* s
)
2079 wxChar
* copy
= new wxChar
[wxStrlen(s
) + 1];
2080 return wxStrcpy(copy
, s
);
2083 wxFileHistory::wxFileHistory(size_t maxFiles
, wxWindowID idBase
)
2085 m_fileMaxFiles
= maxFiles
;
2088 m_fileHistory
= new wxChar
*[m_fileMaxFiles
];
2091 wxFileHistory::~wxFileHistory()
2094 for (i
= 0; i
< m_fileHistoryN
; i
++)
2095 delete[] m_fileHistory
[i
];
2096 delete[] m_fileHistory
;
2099 // File history management
2100 void wxFileHistory::AddFileToHistory(const wxString
& file
)
2104 // Check we don't already have this file
2105 for (i
= 0; i
< m_fileHistoryN
; i
++)
2107 #if defined( __WXMSW__ ) // Add any other OSes with case insensitive file names
2108 wxString testString
;
2109 if ( m_fileHistory
[i
] )
2110 testString
= m_fileHistory
[i
];
2111 if ( m_fileHistory
[i
] && ( file
.Lower() == testString
.Lower() ) )
2113 if ( m_fileHistory
[i
] && ( file
== m_fileHistory
[i
] ) )
2116 // we do have it, move it to the top of the history
2117 RemoveFileFromHistory (i
);
2118 AddFileToHistory (file
);
2123 // if we already have a full history, delete the one at the end
2124 if ( m_fileMaxFiles
== m_fileHistoryN
)
2126 RemoveFileFromHistory (m_fileHistoryN
- 1);
2127 AddFileToHistory (file
);
2131 // Add to the project file history:
2132 // Move existing files (if any) down so we can insert file at beginning.
2133 if (m_fileHistoryN
< m_fileMaxFiles
)
2135 wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2138 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2139 if ( m_fileHistoryN
== 0 && menu
->GetMenuItemCount() )
2141 menu
->AppendSeparator();
2143 menu
->Append(m_idBase
+m_fileHistoryN
, _("[EMPTY]"));
2144 node
= node
->GetNext();
2148 // Shuffle filenames down
2149 for (i
= (m_fileHistoryN
-1); i
> 0; i
--)
2151 m_fileHistory
[i
] = m_fileHistory
[i
-1];
2153 m_fileHistory
[0] = MYcopystring(file
);
2155 // this is the directory of the last opened file
2156 wxString pathCurrent
;
2157 wxSplitPath( m_fileHistory
[0], &pathCurrent
, NULL
, NULL
);
2158 for (i
= 0; i
< m_fileHistoryN
; i
++)
2160 if ( m_fileHistory
[i
] )
2162 // if in same directory just show the filename; otherwise the full
2164 wxString pathInMenu
, path
, filename
, ext
;
2165 wxSplitPath( m_fileHistory
[i
], &path
, &filename
, &ext
);
2166 if ( path
== pathCurrent
)
2168 pathInMenu
= filename
;
2170 pathInMenu
= pathInMenu
+ wxFILE_SEP_EXT
+ ext
;
2174 // absolute path; could also set relative path
2175 pathInMenu
= m_fileHistory
[i
];
2178 // we need to quote '&' characters which are used for mnemonics
2179 pathInMenu
.Replace(_T("&"), _T("&&"));
2181 buf
.Printf(s_MRUEntryFormat
, i
+ 1, pathInMenu
.c_str());
2182 wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2185 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2186 menu
->SetLabel(m_idBase
+ i
, buf
);
2187 node
= node
->GetNext();
2193 void wxFileHistory::RemoveFileFromHistory(size_t i
)
2195 wxCHECK_RET( i
< m_fileHistoryN
,
2196 wxT("invalid index in wxFileHistory::RemoveFileFromHistory") );
2198 // delete the element from the array (could use memmove() too...)
2199 delete [] m_fileHistory
[i
];
2202 for ( j
= i
; j
< m_fileHistoryN
- 1; j
++ )
2204 m_fileHistory
[j
] = m_fileHistory
[j
+ 1];
2207 wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2210 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2212 // shuffle filenames up
2214 for ( j
= i
; j
< m_fileHistoryN
- 1; j
++ )
2216 buf
.Printf(s_MRUEntryFormat
, j
+ 1, m_fileHistory
[j
]);
2217 menu
->SetLabel(m_idBase
+ j
, buf
);
2220 node
= node
->GetNext();
2222 // delete the last menu item which is unused now
2223 wxWindowID lastItemId
= m_idBase
+ wx_truncate_cast(wxWindowID
, m_fileHistoryN
) - 1;
2224 if (menu
->FindItem(lastItemId
))
2226 menu
->Delete(lastItemId
);
2229 // delete the last separator too if no more files are left
2230 if ( m_fileHistoryN
== 1 )
2232 wxMenuItemList::compatibility_iterator nodeLast
= menu
->GetMenuItems().GetLast();
2235 wxMenuItem
*menuItem
= nodeLast
->GetData();
2236 if ( menuItem
->IsSeparator() )
2238 menu
->Delete(menuItem
);
2240 //else: should we search backwards for the last separator?
2242 //else: menu is empty somehow
2249 wxString
wxFileHistory::GetHistoryFile(size_t i
) const
2252 if ( i
< m_fileHistoryN
)
2254 s
= m_fileHistory
[i
];
2258 wxFAIL_MSG( wxT("bad index in wxFileHistory::GetHistoryFile") );
2264 void wxFileHistory::UseMenu(wxMenu
*menu
)
2266 if (!m_fileMenus
.Member(menu
))
2267 m_fileMenus
.Append(menu
);
2270 void wxFileHistory::RemoveMenu(wxMenu
*menu
)
2272 m_fileMenus
.DeleteObject(menu
);
2276 void wxFileHistory::Load(wxConfigBase
& config
)
2280 buf
.Printf(wxT("file%d"), (int)m_fileHistoryN
+1);
2281 wxString historyFile
;
2282 while ((m_fileHistoryN
< m_fileMaxFiles
) && config
.Read(buf
, &historyFile
) && (!historyFile
.empty()))
2284 m_fileHistory
[m_fileHistoryN
] = MYcopystring((const wxChar
*) historyFile
);
2286 buf
.Printf(wxT("file%d"), (int)m_fileHistoryN
+1);
2287 historyFile
= wxEmptyString
;
2292 void wxFileHistory::Save(wxConfigBase
& config
)
2295 for (i
= 0; i
< m_fileMaxFiles
; i
++)
2298 buf
.Printf(wxT("file%d"), (int)i
+1);
2299 if (i
< m_fileHistoryN
)
2300 config
.Write(buf
, wxString(m_fileHistory
[i
]));
2302 config
.Write(buf
, wxEmptyString
);
2305 #endif // wxUSE_CONFIG
2307 void wxFileHistory::AddFilesToMenu()
2309 if (m_fileHistoryN
> 0)
2311 wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2314 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2315 if (menu
->GetMenuItemCount())
2317 menu
->AppendSeparator();
2321 for (i
= 0; i
< m_fileHistoryN
; i
++)
2323 if (m_fileHistory
[i
])
2326 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
2327 menu
->Append(m_idBase
+i
, buf
);
2330 node
= node
->GetNext();
2335 void wxFileHistory::AddFilesToMenu(wxMenu
* menu
)
2337 if (m_fileHistoryN
> 0)
2339 if (menu
->GetMenuItemCount())
2341 menu
->AppendSeparator();
2345 for (i
= 0; i
< m_fileHistoryN
; i
++)
2347 if (m_fileHistory
[i
])
2350 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
2351 menu
->Append(m_idBase
+i
, buf
);
2357 // ----------------------------------------------------------------------------
2358 // Permits compatibility with existing file formats and functions that
2359 // manipulate files directly
2360 // ----------------------------------------------------------------------------
2362 #if wxUSE_STD_IOSTREAM
2364 bool wxTransferFileToStream(const wxString
& filename
, wxSTD ostream
& stream
)
2366 wxFFile
file(filename
, _T("rb"));
2367 if ( !file
.IsOpened() )
2375 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2379 stream
.write(buf
, nRead
);
2383 while ( !file
.Eof() );
2388 bool wxTransferStreamToFile(wxSTD istream
& stream
, const wxString
& filename
)
2390 wxFFile
file(filename
, _T("wb"));
2391 if ( !file
.IsOpened() )
2397 stream
.read(buf
, WXSIZEOF(buf
));
2398 if ( !stream
.bad() ) // fail may be set on EOF, don't use operator!()
2400 if ( !file
.Write(buf
, stream
.gcount()) )
2404 while ( !stream
.eof() );
2409 #else // !wxUSE_STD_IOSTREAM
2411 bool wxTransferFileToStream(const wxString
& filename
, wxOutputStream
& stream
)
2413 wxFFile
file(filename
, _T("rb"));
2414 if ( !file
.IsOpened() )
2422 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2426 stream
.Write(buf
, nRead
);
2430 while ( !file
.Eof() );
2435 bool wxTransferStreamToFile(wxInputStream
& stream
, const wxString
& filename
)
2437 wxFFile
file(filename
, _T("wb"));
2438 if ( !file
.IsOpened() )
2444 stream
.Read(buf
, WXSIZEOF(buf
));
2446 const size_t nRead
= stream
.LastRead();
2447 if ( !nRead
|| !file
.Write(buf
, nRead
) )
2450 while ( !stream
.Eof() );
2455 #endif // wxUSE_STD_IOSTREAM/!wxUSE_STD_IOSTREAM
2457 #endif // wxUSE_DOC_VIEW_ARCHITECTURE