1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/docview.cpp
3 // Purpose: Document/view classes
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #if wxUSE_DOC_VIEW_ARCHITECTURE
30 #include "wx/string.h"
34 #include "wx/dialog.h"
37 #include "wx/filedlg.h"
45 #include "wx/filename.h"
52 #if wxUSE_PRINTING_ARCHITECTURE
53 #include "wx/prntbase.h"
54 #include "wx/printdlg.h"
57 #include "wx/msgdlg.h"
58 #include "wx/choicdlg.h"
59 #include "wx/docview.h"
60 #include "wx/confbase.h"
62 #include "wx/cmdproc.h"
67 #if wxUSE_STD_IOSTREAM
68 #include "wx/ioswrap.h"
75 #include "wx/wfstream.h"
78 // ----------------------------------------------------------------------------
80 // ----------------------------------------------------------------------------
82 IMPLEMENT_ABSTRACT_CLASS(wxDocument
, wxEvtHandler
)
83 IMPLEMENT_ABSTRACT_CLASS(wxView
, wxEvtHandler
)
84 IMPLEMENT_ABSTRACT_CLASS(wxDocTemplate
, wxObject
)
85 IMPLEMENT_DYNAMIC_CLASS(wxDocManager
, wxEvtHandler
)
86 IMPLEMENT_CLASS(wxDocChildFrame
, wxFrame
)
87 IMPLEMENT_CLASS(wxDocParentFrame
, wxFrame
)
89 #if wxUSE_PRINTING_ARCHITECTURE
90 IMPLEMENT_DYNAMIC_CLASS(wxDocPrintout
, wxPrintout
)
93 IMPLEMENT_DYNAMIC_CLASS(wxFileHistory
, wxObject
)
95 // ----------------------------------------------------------------------------
96 // function prototypes
97 // ----------------------------------------------------------------------------
99 static inline wxString
FindExtension(const wxChar
*path
);
100 static wxWindow
* wxFindSuitableParent(void);
102 // ----------------------------------------------------------------------------
104 // ----------------------------------------------------------------------------
106 static const wxChar
*s_MRUEntryFormat
= wxT("&%d %s");
108 // ============================================================================
110 // ============================================================================
112 // ----------------------------------------------------------------------------
114 // ----------------------------------------------------------------------------
116 static wxString
FindExtension(const wxChar
*path
)
119 wxSplitPath(path
, NULL
, NULL
, &ext
);
121 // VZ: extensions are considered not case sensitive - is this really a good
123 return ext
.MakeLower();
126 // ----------------------------------------------------------------------------
127 // Definition of wxDocument
128 // ----------------------------------------------------------------------------
130 wxDocument::wxDocument(wxDocument
*parent
)
132 m_documentModified
= false;
133 m_documentParent
= parent
;
134 m_documentTemplate
= (wxDocTemplate
*) NULL
;
135 m_commandProcessor
= (wxCommandProcessor
*) NULL
;
139 bool wxDocument::DeleteContents()
144 wxDocument::~wxDocument()
148 if (m_commandProcessor
)
149 delete m_commandProcessor
;
151 if (GetDocumentManager())
152 GetDocumentManager()->RemoveDocument(this);
154 // Not safe to do here, since it'll invoke virtual view functions
155 // expecting to see valid derived objects: and by the time we get here,
156 // we've called destructors higher up.
160 bool wxDocument::Close()
162 if (OnSaveModified())
163 return OnCloseDocument();
168 bool wxDocument::OnCloseDocument()
170 // Tell all views that we're about to close
177 // Note that this implicitly deletes the document when the last view is
179 bool wxDocument::DeleteAllViews()
181 wxDocManager
* manager
= GetDocumentManager();
183 // first check if all views agree to be closed
184 const wxList::iterator end
= m_documentViews
.end();
185 for ( wxList::iterator i
= m_documentViews
.begin(); i
!= end
; ++i
)
187 wxView
*view
= (wxView
*)*i
;
188 if ( !view
->Close() )
192 // all views agreed to close, now do close them
193 if ( m_documentViews
.empty() )
195 // normally the document would be implicitly deleted when the last view
196 // is, but if don't have any views, do it here instead
197 if ( manager
&& manager
->GetDocuments().Member(this) )
202 // as we delete elements we iterate over, don't use the usual "from
203 // begin to end" loop
206 wxView
*view
= (wxView
*)*m_documentViews
.begin();
208 bool isLastOne
= m_documentViews
.size() == 1;
210 // this always deletes the node implicitly and if this is the last
211 // view also deletes this object itself (also implicitly, great),
212 // so we can't test for m_documentViews.empty() after calling this!
223 wxView
*wxDocument::GetFirstView() const
225 if (m_documentViews
.GetCount() == 0)
226 return (wxView
*) NULL
;
227 return (wxView
*)m_documentViews
.GetFirst()->GetData();
230 wxDocManager
*wxDocument::GetDocumentManager() const
232 return (m_documentTemplate
? m_documentTemplate
->GetDocumentManager() : (wxDocManager
*) NULL
);
235 bool wxDocument::OnNewDocument()
237 if (!OnSaveModified())
240 if (OnCloseDocument()==false) return false;
243 SetDocumentSaved(false);
246 GetDocumentManager()->MakeDefaultName(name
);
248 SetFilename(name
, true);
253 bool wxDocument::Save()
255 if (!IsModified() && m_savedYet
)
258 if ( m_documentFile
.empty() || !m_savedYet
)
261 return OnSaveDocument(m_documentFile
);
264 bool wxDocument::SaveAs()
266 wxDocTemplate
*docTemplate
= GetDocumentTemplate();
270 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
271 wxString filter
= docTemplate
->GetDescription() + wxT(" (") + docTemplate
->GetFileFilter() + wxT(")|") + docTemplate
->GetFileFilter();
273 // Now see if there are some other template with identical view and document
274 // classes, whose filters may also be used.
276 if (docTemplate
->GetViewClassInfo() && docTemplate
->GetDocClassInfo())
278 wxList::compatibility_iterator node
= wxDocManager::GetDocumentManager()->GetTemplates().GetFirst();
281 wxDocTemplate
*t
= (wxDocTemplate
*) node
->GetData();
283 if (t
->IsVisible() && t
!= docTemplate
&&
284 t
->GetViewClassInfo() == docTemplate
->GetViewClassInfo() &&
285 t
->GetDocClassInfo() == docTemplate
->GetDocClassInfo())
287 // add a '|' to separate this filter from the previous one
288 if ( !filter
.empty() )
291 filter
<< t
->GetDescription() << wxT(" (") << t
->GetFileFilter() << wxT(") |")
292 << t
->GetFileFilter();
295 node
= node
->GetNext();
299 wxString filter
= docTemplate
->GetFileFilter() ;
301 wxString tmp
= wxFileSelector(_("Save as"),
302 docTemplate
->GetDirectory(),
303 wxFileNameFromPath(GetFilename()),
304 docTemplate
->GetDefaultExtension(),
306 wxSAVE
| wxOVERWRITE_PROMPT
,
307 GetDocumentWindow());
312 wxString
fileName(tmp
);
313 wxString path
, name
, ext
;
314 wxSplitPath(fileName
, & path
, & name
, & ext
);
318 fileName
+= wxT(".");
319 fileName
+= docTemplate
->GetDefaultExtension();
322 SetFilename(fileName
);
323 SetTitle(wxFileNameFromPath(fileName
));
325 // Notify the views that the filename has changed
326 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
329 wxView
*view
= (wxView
*)node
->GetData();
330 view
->OnChangeFilename();
331 node
= node
->GetNext();
334 // Files that were not saved correctly are not added to the FileHistory.
335 if (!OnSaveDocument(m_documentFile
))
338 // A file that doesn't use the default extension of its document template cannot be opened
339 // via the FileHistory, so we do not add it.
340 if (docTemplate
->FileMatchesTemplate(fileName
))
342 GetDocumentManager()->AddFileToHistory(fileName
);
346 // The user will probably not be able to open the file again, so
347 // we could warn about the wrong file-extension here.
352 bool wxDocument::OnSaveDocument(const wxString
& file
)
357 if ( !DoSaveDocument(file
) )
362 SetDocumentSaved(true);
364 wxFileName
fn(file
) ;
365 fn
.MacSetDefaultTypeAndCreator() ;
370 bool wxDocument::OnOpenDocument(const wxString
& file
)
372 if (!OnSaveModified())
375 if ( !DoOpenDocument(file
) )
378 SetFilename(file
, true);
387 #if wxUSE_STD_IOSTREAM
388 wxSTD istream
& wxDocument::LoadObject(wxSTD istream
& stream
)
390 wxInputStream
& wxDocument::LoadObject(wxInputStream
& stream
)
396 #if wxUSE_STD_IOSTREAM
397 wxSTD ostream
& wxDocument::SaveObject(wxSTD ostream
& stream
)
399 wxOutputStream
& wxDocument::SaveObject(wxOutputStream
& stream
)
405 bool wxDocument::Revert()
411 // Get title, or filename if no title, else unnamed
412 bool wxDocument::GetPrintableName(wxString
& buf
) const
414 if (!m_documentTitle
.empty())
416 buf
= m_documentTitle
;
419 else if (!m_documentFile
.empty())
421 buf
= wxFileNameFromPath(m_documentFile
);
431 wxWindow
*wxDocument::GetDocumentWindow() const
433 wxView
*view
= GetFirstView();
435 return view
->GetFrame();
437 return wxTheApp
->GetTopWindow();
440 wxCommandProcessor
*wxDocument::OnCreateCommandProcessor()
442 return new wxCommandProcessor
;
445 // true if safe to close
446 bool wxDocument::OnSaveModified()
451 GetPrintableName(title
);
454 if (!wxTheApp
->GetAppName().empty())
455 msgTitle
= wxTheApp
->GetAppName();
457 msgTitle
= wxString(_("Warning"));
460 prompt
.Printf(_("Do you want to save changes to document %s?"),
461 (const wxChar
*)title
);
462 int res
= wxMessageBox(prompt
, msgTitle
,
463 wxYES_NO
|wxCANCEL
|wxICON_QUESTION
,
464 GetDocumentWindow());
470 else if (res
== wxYES
)
472 else if (res
== wxCANCEL
)
478 bool wxDocument::Draw(wxDC
& WXUNUSED(context
))
483 bool wxDocument::AddView(wxView
*view
)
485 if (!m_documentViews
.Member(view
))
487 m_documentViews
.Append(view
);
493 bool wxDocument::RemoveView(wxView
*view
)
495 (void)m_documentViews
.DeleteObject(view
);
500 bool wxDocument::OnCreate(const wxString
& WXUNUSED(path
), long flags
)
502 if (GetDocumentTemplate()->CreateView(this, flags
))
508 // Called after a view is added or removed.
509 // The default implementation deletes the document if
510 // there are no more views.
511 void wxDocument::OnChangedViewList()
513 if (m_documentViews
.GetCount() == 0)
515 if (OnSaveModified())
522 void wxDocument::UpdateAllViews(wxView
*sender
, wxObject
*hint
)
524 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
527 wxView
*view
= (wxView
*)node
->GetData();
529 view
->OnUpdate(sender
, hint
);
530 node
= node
->GetNext();
534 void wxDocument::NotifyClosing()
536 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
539 wxView
*view
= (wxView
*)node
->GetData();
540 view
->OnClosingDocument();
541 node
= node
->GetNext();
545 void wxDocument::SetFilename(const wxString
& filename
, bool notifyViews
)
547 m_documentFile
= filename
;
550 // Notify the views that the filename has changed
551 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
554 wxView
*view
= (wxView
*)node
->GetData();
555 view
->OnChangeFilename();
556 node
= node
->GetNext();
561 bool wxDocument::DoSaveDocument(const wxString
& file
)
564 if (!wxTheApp
->GetAppName().empty())
565 msgTitle
= wxTheApp
->GetAppName();
567 msgTitle
= wxString(_("File error"));
569 #if wxUSE_STD_IOSTREAM
570 wxSTD ofstream
store(file
.mb_str(), wxSTD
ios::binary
);
571 if (store
.fail() || store
.bad())
573 wxFileOutputStream
store(file
);
574 if (store
.GetLastError() != wxSTREAM_NO_ERROR
)
577 (void)wxMessageBox(_("Sorry, could not open this file for saving."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
578 GetDocumentWindow());
582 if (!SaveObject(store
))
584 (void)wxMessageBox(_("Sorry, could not save this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
585 GetDocumentWindow());
593 bool wxDocument::DoOpenDocument(const wxString
& file
)
595 #if wxUSE_STD_IOSTREAM
596 wxSTD ifstream
store(file
.mb_str(), wxSTD
ios::binary
);
597 if (!store
.fail() && !store
.bad())
599 wxFileInputStream
store(file
);
600 if (store
.GetLastError() == wxSTREAM_NO_ERROR
)
603 #if wxUSE_STD_IOSTREAM
605 if ( !!store
|| store
.eof() )
607 int res
= LoadObject(store
).GetLastError();
608 if ( res
== wxSTREAM_NO_ERROR
|| res
== wxSTREAM_EOF
)
613 wxLogError(_("Sorry, could not open this file."));
618 // ----------------------------------------------------------------------------
620 // ----------------------------------------------------------------------------
624 m_viewDocument
= (wxDocument
*) NULL
;
626 m_viewFrame
= (wxFrame
*) NULL
;
631 GetDocumentManager()->ActivateView(this, false);
632 m_viewDocument
->RemoveView(this);
635 // Extend event processing to search the document's event table
636 bool wxView::ProcessEvent(wxEvent
& event
)
638 if ( !GetDocument() || !GetDocument()->ProcessEvent(event
) )
639 return wxEvtHandler::ProcessEvent(event
);
644 void wxView::OnActivateView(bool WXUNUSED(activate
), wxView
*WXUNUSED(activeView
), wxView
*WXUNUSED(deactiveView
))
648 void wxView::OnPrint(wxDC
*dc
, wxObject
*WXUNUSED(info
))
653 void wxView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
))
657 void wxView::OnChangeFilename()
659 // GetFrame can return wxWindow rather than wxTopLevelWindow due to
660 // generic MDI implementation so use SetLabel rather than SetTitle.
661 // It should cause SetTitle() for top level windows.
662 wxWindow
*win
= GetFrame();
665 wxDocument
*doc
= GetDocument();
669 doc
->GetPrintableName(name
);
673 void wxView::SetDocument(wxDocument
*doc
)
675 m_viewDocument
= doc
;
680 bool wxView::Close(bool deleteWindow
)
682 if (OnClose(deleteWindow
))
688 void wxView::Activate(bool activate
)
690 if (GetDocument() && GetDocumentManager())
692 OnActivateView(activate
, this, GetDocumentManager()->GetCurrentView());
693 GetDocumentManager()->ActivateView(this, activate
);
697 bool wxView::OnClose(bool WXUNUSED(deleteWindow
))
699 return GetDocument() ? GetDocument()->Close() : true;
702 #if wxUSE_PRINTING_ARCHITECTURE
703 wxPrintout
*wxView::OnCreatePrintout()
705 return new wxDocPrintout(this);
707 #endif // wxUSE_PRINTING_ARCHITECTURE
709 // ----------------------------------------------------------------------------
711 // ----------------------------------------------------------------------------
713 wxDocTemplate::wxDocTemplate(wxDocManager
*manager
,
714 const wxString
& descr
,
715 const wxString
& filter
,
718 const wxString
& docTypeName
,
719 const wxString
& viewTypeName
,
720 wxClassInfo
*docClassInfo
,
721 wxClassInfo
*viewClassInfo
,
724 m_documentManager
= manager
;
725 m_description
= descr
;
728 m_fileFilter
= filter
;
730 m_docTypeName
= docTypeName
;
731 m_viewTypeName
= viewTypeName
;
732 m_documentManager
->AssociateTemplate(this);
734 m_docClassInfo
= docClassInfo
;
735 m_viewClassInfo
= viewClassInfo
;
738 wxDocTemplate::~wxDocTemplate()
740 m_documentManager
->DisassociateTemplate(this);
743 // Tries to dynamically construct an object of the right class.
744 wxDocument
*wxDocTemplate::CreateDocument(const wxString
& path
, long flags
)
746 wxDocument
*doc
= DoCreateDocument();
748 return (wxDocument
*) NULL
;
750 if (InitDocument(doc
, path
, flags
))
756 return (wxDocument
*) NULL
;
760 bool wxDocTemplate::InitDocument(wxDocument
* doc
, const wxString
& path
, long flags
)
762 doc
->SetFilename(path
);
763 doc
->SetDocumentTemplate(this);
764 GetDocumentManager()->AddDocument(doc
);
765 doc
->SetCommandProcessor(doc
->OnCreateCommandProcessor());
767 if (doc
->OnCreate(path
, flags
))
771 if (GetDocumentManager()->GetDocuments().Member(doc
))
772 doc
->DeleteAllViews();
777 wxView
*wxDocTemplate::CreateView(wxDocument
*doc
, long flags
)
779 wxView
*view
= DoCreateView();
781 return (wxView
*) NULL
;
783 view
->SetDocument(doc
);
784 if (view
->OnCreate(doc
, flags
))
791 return (wxView
*) NULL
;
795 // The default (very primitive) format detection: check is the extension is
796 // that of the template
797 bool wxDocTemplate::FileMatchesTemplate(const wxString
& path
)
799 return GetDefaultExtension().IsSameAs(FindExtension(path
));
802 wxDocument
*wxDocTemplate::DoCreateDocument()
805 return (wxDocument
*) NULL
;
807 return (wxDocument
*)m_docClassInfo
->CreateObject();
810 wxView
*wxDocTemplate::DoCreateView()
812 if (!m_viewClassInfo
)
813 return (wxView
*) NULL
;
815 return (wxView
*)m_viewClassInfo
->CreateObject();
818 // ----------------------------------------------------------------------------
820 // ----------------------------------------------------------------------------
822 BEGIN_EVENT_TABLE(wxDocManager
, wxEvtHandler
)
823 EVT_MENU(wxID_OPEN
, wxDocManager::OnFileOpen
)
824 EVT_MENU(wxID_CLOSE
, wxDocManager::OnFileClose
)
825 EVT_MENU(wxID_CLOSE_ALL
, wxDocManager::OnFileCloseAll
)
826 EVT_MENU(wxID_REVERT
, wxDocManager::OnFileRevert
)
827 EVT_MENU(wxID_NEW
, wxDocManager::OnFileNew
)
828 EVT_MENU(wxID_SAVE
, wxDocManager::OnFileSave
)
829 EVT_MENU(wxID_SAVEAS
, wxDocManager::OnFileSaveAs
)
830 EVT_MENU(wxID_UNDO
, wxDocManager::OnUndo
)
831 EVT_MENU(wxID_REDO
, wxDocManager::OnRedo
)
833 EVT_UPDATE_UI(wxID_OPEN
, wxDocManager::OnUpdateFileOpen
)
834 EVT_UPDATE_UI(wxID_CLOSE
, wxDocManager::OnUpdateFileClose
)
835 EVT_UPDATE_UI(wxID_CLOSE_ALL
, wxDocManager::OnUpdateFileClose
)
836 EVT_UPDATE_UI(wxID_REVERT
, wxDocManager::OnUpdateFileRevert
)
837 EVT_UPDATE_UI(wxID_NEW
, wxDocManager::OnUpdateFileNew
)
838 EVT_UPDATE_UI(wxID_SAVE
, wxDocManager::OnUpdateFileSave
)
839 EVT_UPDATE_UI(wxID_SAVEAS
, wxDocManager::OnUpdateFileSaveAs
)
840 EVT_UPDATE_UI(wxID_UNDO
, wxDocManager::OnUpdateUndo
)
841 EVT_UPDATE_UI(wxID_REDO
, wxDocManager::OnUpdateRedo
)
843 #if wxUSE_PRINTING_ARCHITECTURE
844 EVT_MENU(wxID_PRINT
, wxDocManager::OnPrint
)
845 EVT_MENU(wxID_PREVIEW
, wxDocManager::OnPreview
)
847 EVT_UPDATE_UI(wxID_PRINT
, wxDocManager::OnUpdatePrint
)
848 EVT_UPDATE_UI(wxID_PREVIEW
, wxDocManager::OnUpdatePreview
)
852 wxDocManager
* wxDocManager::sm_docManager
= (wxDocManager
*) NULL
;
854 wxDocManager::wxDocManager(long flags
, bool initialize
)
856 m_defaultDocumentNameCounter
= 1;
858 m_currentView
= (wxView
*) NULL
;
859 m_maxDocsOpen
= 10000;
860 m_fileHistory
= (wxFileHistory
*) NULL
;
863 sm_docManager
= this;
866 wxDocManager::~wxDocManager()
870 delete m_fileHistory
;
871 sm_docManager
= (wxDocManager
*) NULL
;
874 // closes the specified document
875 bool wxDocManager::CloseDocument(wxDocument
* doc
, bool force
)
877 if (doc
->Close() || force
)
879 // Implicitly deletes the document when
880 // the last view is deleted
881 doc
->DeleteAllViews();
883 // Check we're really deleted
884 if (m_docs
.Member(doc
))
892 bool wxDocManager::CloseDocuments(bool force
)
894 wxList::compatibility_iterator node
= m_docs
.GetFirst();
897 wxDocument
*doc
= (wxDocument
*)node
->GetData();
898 wxList::compatibility_iterator next
= node
->GetNext();
900 if (!CloseDocument(doc
, force
))
903 // This assumes that documents are not connected in
904 // any way, i.e. deleting one document does NOT
911 bool wxDocManager::Clear(bool force
)
913 if (!CloseDocuments(force
))
916 m_currentView
= NULL
;
918 wxList::compatibility_iterator node
= m_templates
.GetFirst();
921 wxDocTemplate
*templ
= (wxDocTemplate
*) node
->GetData();
922 wxList::compatibility_iterator next
= node
->GetNext();
929 bool wxDocManager::Initialize()
931 m_fileHistory
= OnCreateFileHistory();
935 wxFileHistory
*wxDocManager::OnCreateFileHistory()
937 return new wxFileHistory
;
940 void wxDocManager::OnFileClose(wxCommandEvent
& WXUNUSED(event
))
942 wxDocument
*doc
= GetCurrentDocument();
947 doc
->DeleteAllViews();
948 if (m_docs
.Member(doc
))
953 void wxDocManager::OnFileCloseAll(wxCommandEvent
& WXUNUSED(event
))
955 CloseDocuments(false);
958 void wxDocManager::OnFileNew(wxCommandEvent
& WXUNUSED(event
))
960 CreateDocument( wxEmptyString
, wxDOC_NEW
);
963 void wxDocManager::OnFileOpen(wxCommandEvent
& WXUNUSED(event
))
965 if ( !CreateDocument( wxEmptyString
, 0) )
971 void wxDocManager::OnFileRevert(wxCommandEvent
& WXUNUSED(event
))
973 wxDocument
*doc
= GetCurrentDocument();
979 void wxDocManager::OnFileSave(wxCommandEvent
& WXUNUSED(event
))
981 wxDocument
*doc
= GetCurrentDocument();
987 void wxDocManager::OnFileSaveAs(wxCommandEvent
& WXUNUSED(event
))
989 wxDocument
*doc
= GetCurrentDocument();
995 void wxDocManager::OnPrint(wxCommandEvent
& WXUNUSED(event
))
997 #if wxUSE_PRINTING_ARCHITECTURE
998 wxView
*view
= GetCurrentView();
1002 wxPrintout
*printout
= view
->OnCreatePrintout();
1006 printer
.Print(view
->GetFrame(), printout
, true);
1010 #endif // wxUSE_PRINTING_ARCHITECTURE
1013 void wxDocManager::OnPreview(wxCommandEvent
& WXUNUSED(event
))
1015 #if wxUSE_PRINTING_ARCHITECTURE
1016 wxView
*view
= GetCurrentView();
1020 wxPrintout
*printout
= view
->OnCreatePrintout();
1023 // Pass two printout objects: for preview, and possible printing.
1024 wxPrintPreviewBase
*preview
= new wxPrintPreview(printout
, view
->OnCreatePrintout());
1025 if ( !preview
->Ok() )
1028 wxMessageBox( _("Sorry, print preview needs a printer to be installed.") );
1032 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, (wxFrame
*)wxTheApp
->GetTopWindow(), _("Print Preview"),
1033 wxPoint(100, 100), wxSize(600, 650));
1034 frame
->Centre(wxBOTH
);
1035 frame
->Initialize();
1038 #endif // wxUSE_PRINTING_ARCHITECTURE
1041 void wxDocManager::OnUndo(wxCommandEvent
& event
)
1043 wxDocument
*doc
= GetCurrentDocument();
1046 if (doc
->GetCommandProcessor())
1047 doc
->GetCommandProcessor()->Undo();
1052 void wxDocManager::OnRedo(wxCommandEvent
& event
)
1054 wxDocument
*doc
= GetCurrentDocument();
1057 if (doc
->GetCommandProcessor())
1058 doc
->GetCommandProcessor()->Redo();
1063 // Handlers for UI update commands
1065 void wxDocManager::OnUpdateFileOpen(wxUpdateUIEvent
& event
)
1067 event
.Enable( true );
1070 void wxDocManager::OnUpdateFileClose(wxUpdateUIEvent
& event
)
1072 wxDocument
*doc
= GetCurrentDocument();
1073 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1076 void wxDocManager::OnUpdateFileRevert(wxUpdateUIEvent
& event
)
1078 wxDocument
*doc
= GetCurrentDocument();
1079 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1082 void wxDocManager::OnUpdateFileNew(wxUpdateUIEvent
& event
)
1084 event
.Enable( true );
1087 void wxDocManager::OnUpdateFileSave(wxUpdateUIEvent
& event
)
1089 wxDocument
*doc
= GetCurrentDocument();
1090 event
.Enable( doc
&& doc
->IsModified() );
1093 void wxDocManager::OnUpdateFileSaveAs(wxUpdateUIEvent
& event
)
1095 wxDocument
*doc
= GetCurrentDocument();
1096 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1099 void wxDocManager::OnUpdateUndo(wxUpdateUIEvent
& event
)
1101 wxDocument
*doc
= GetCurrentDocument();
1103 event
.Enable(false);
1104 else if (!doc
->GetCommandProcessor())
1108 event
.Enable( doc
->GetCommandProcessor()->CanUndo() );
1109 doc
->GetCommandProcessor()->SetMenuStrings();
1113 void wxDocManager::OnUpdateRedo(wxUpdateUIEvent
& event
)
1115 wxDocument
*doc
= GetCurrentDocument();
1117 event
.Enable(false);
1118 else if (!doc
->GetCommandProcessor())
1122 event
.Enable( doc
->GetCommandProcessor()->CanRedo() );
1123 doc
->GetCommandProcessor()->SetMenuStrings();
1127 void wxDocManager::OnUpdatePrint(wxUpdateUIEvent
& event
)
1129 wxDocument
*doc
= GetCurrentDocument();
1130 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1133 void wxDocManager::OnUpdatePreview(wxUpdateUIEvent
& event
)
1135 wxDocument
*doc
= GetCurrentDocument();
1136 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1139 wxView
*wxDocManager::GetCurrentView() const
1142 return m_currentView
;
1143 if (m_docs
.GetCount() == 1)
1145 wxDocument
* doc
= (wxDocument
*) m_docs
.GetFirst()->GetData();
1146 return doc
->GetFirstView();
1148 return (wxView
*) NULL
;
1151 // Extend event processing to search the view's event table
1152 bool wxDocManager::ProcessEvent(wxEvent
& event
)
1154 wxView
* view
= GetCurrentView();
1157 if (view
->ProcessEvent(event
))
1160 return wxEvtHandler::ProcessEvent(event
);
1163 wxDocument
*wxDocManager::CreateDocument(const wxString
& path
, long flags
)
1165 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.GetCount()];
1168 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1170 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Item(i
)->GetData());
1171 if (temp
->IsVisible())
1173 templates
[n
] = temp
;
1180 return (wxDocument
*) NULL
;
1183 wxDocument
* docToClose
= NULL
;
1185 // If we've reached the max number of docs, close the
1187 if ( (int)GetDocuments().GetCount() >= m_maxDocsOpen
)
1189 wxDocument
*doc
= (wxDocument
*)GetDocuments().GetFirst()->GetData();
1193 // New document: user chooses a template, unless there's only one.
1194 if (flags
& wxDOC_NEW
)
1200 if (!CloseDocument(docToClose
, false))
1207 wxDocTemplate
*temp
= templates
[0];
1209 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
1213 newDoc
->SetDocumentName(temp
->GetDocumentName());
1214 newDoc
->SetDocumentTemplate(temp
);
1215 if (!newDoc
->OnNewDocument() )
1217 // Document is implicitly deleted by DeleteAllViews
1218 newDoc
->DeleteAllViews();
1225 wxDocTemplate
*temp
= SelectDocumentType(templates
, n
);
1231 if (!CloseDocument(docToClose
, false))
1237 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
1241 newDoc
->SetDocumentName(temp
->GetDocumentName());
1242 newDoc
->SetDocumentTemplate(temp
);
1243 if (!newDoc
->OnNewDocument() )
1245 // Document is implicitly deleted by DeleteAllViews
1246 newDoc
->DeleteAllViews();
1253 return (wxDocument
*) NULL
;
1256 // Existing document
1257 wxDocTemplate
*temp
;
1259 wxString path2
= path
;
1261 if (flags
& wxDOC_SILENT
)
1263 temp
= FindTemplateForPath(path2
);
1266 // Since we do not add files with non-default extensions to the FileHistory this
1267 // can only happen if the application changes the allowed templates in runtime.
1268 (void)wxMessageBox(_("Sorry, the format for this file is unknown."),
1270 wxOK
| wxICON_EXCLAMATION
, wxFindSuitableParent());
1274 temp
= SelectDocumentPath(templates
, n
, path2
, flags
);
1282 if (!CloseDocument(docToClose
, false))
1288 //see if this file is already open
1289 for (size_t i
= 0; i
< GetDocuments().GetCount(); ++i
)
1291 wxDocument
* currentDoc
= (wxDocument
*)(GetDocuments().Item(i
)->GetData());
1293 //file paths are case-insensitive on Windows
1294 if (path2
.CmpNoCase(currentDoc
->GetFilename()) == 0)
1296 if (path2
.Cmp(currentDoc
->GetFilename()) == 0)
1299 //file already open. Just activate it and return
1300 if (currentDoc
->GetFirstView())
1302 ActivateView(currentDoc
->GetFirstView(), true);
1303 if (currentDoc
->GetDocumentWindow())
1304 currentDoc
->GetDocumentWindow()->SetFocus();
1310 wxDocument
*newDoc
= temp
->CreateDocument(path2
, flags
);
1313 newDoc
->SetDocumentName(temp
->GetDocumentName());
1314 newDoc
->SetDocumentTemplate(temp
);
1315 if (!newDoc
->OnOpenDocument(path2
))
1317 newDoc
->DeleteAllViews();
1318 // delete newDoc; // Implicitly deleted by DeleteAllViews
1319 return (wxDocument
*) NULL
;
1321 // A file that doesn't use the default extension of its document
1322 // template cannot be opened via the FileHistory, so we do not
1324 if (temp
->FileMatchesTemplate(path2
))
1325 AddFileToHistory(path2
);
1330 return (wxDocument
*) NULL
;
1333 wxView
*wxDocManager::CreateView(wxDocument
*doc
, long flags
)
1335 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.GetCount()];
1338 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1340 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Item(i
)->GetData());
1341 if (temp
->IsVisible())
1343 if (temp
->GetDocumentName() == doc
->GetDocumentName())
1345 templates
[n
] = temp
;
1353 return (wxView
*) NULL
;
1357 wxDocTemplate
*temp
= templates
[0];
1359 wxView
*view
= temp
->CreateView(doc
, flags
);
1361 view
->SetViewName(temp
->GetViewName());
1365 wxDocTemplate
*temp
= SelectViewType(templates
, n
);
1369 wxView
*view
= temp
->CreateView(doc
, flags
);
1371 view
->SetViewName(temp
->GetViewName());
1375 return (wxView
*) NULL
;
1378 // Not yet implemented
1379 void wxDocManager::DeleteTemplate(wxDocTemplate
*WXUNUSED(temp
), long WXUNUSED(flags
))
1383 // Not yet implemented
1384 bool wxDocManager::FlushDoc(wxDocument
*WXUNUSED(doc
))
1389 wxDocument
*wxDocManager::GetCurrentDocument() const
1391 wxView
*view
= GetCurrentView();
1393 return view
->GetDocument();
1395 return (wxDocument
*) NULL
;
1398 // Make a default document name
1399 bool wxDocManager::MakeDefaultName(wxString
& name
)
1401 name
.Printf(_("unnamed%d"), m_defaultDocumentNameCounter
);
1402 m_defaultDocumentNameCounter
++;
1407 // Make a frame title (override this to do something different)
1408 // If docName is empty, a document is not currently active.
1409 wxString
wxDocManager::MakeFrameTitle(wxDocument
* doc
)
1411 wxString appName
= wxTheApp
->GetAppName();
1418 doc
->GetPrintableName(docName
);
1419 title
= docName
+ wxString(_(" - ")) + appName
;
1425 // Not yet implemented
1426 wxDocTemplate
*wxDocManager::MatchTemplate(const wxString
& WXUNUSED(path
))
1428 return (wxDocTemplate
*) NULL
;
1431 // File history management
1432 void wxDocManager::AddFileToHistory(const wxString
& file
)
1435 m_fileHistory
->AddFileToHistory(file
);
1438 void wxDocManager::RemoveFileFromHistory(size_t i
)
1441 m_fileHistory
->RemoveFileFromHistory(i
);
1444 wxString
wxDocManager::GetHistoryFile(size_t i
) const
1449 histFile
= m_fileHistory
->GetHistoryFile(i
);
1454 void wxDocManager::FileHistoryUseMenu(wxMenu
*menu
)
1457 m_fileHistory
->UseMenu(menu
);
1460 void wxDocManager::FileHistoryRemoveMenu(wxMenu
*menu
)
1463 m_fileHistory
->RemoveMenu(menu
);
1467 void wxDocManager::FileHistoryLoad(wxConfigBase
& config
)
1470 m_fileHistory
->Load(config
);
1473 void wxDocManager::FileHistorySave(wxConfigBase
& config
)
1476 m_fileHistory
->Save(config
);
1480 void wxDocManager::FileHistoryAddFilesToMenu(wxMenu
* menu
)
1483 m_fileHistory
->AddFilesToMenu(menu
);
1486 void wxDocManager::FileHistoryAddFilesToMenu()
1489 m_fileHistory
->AddFilesToMenu();
1492 size_t wxDocManager::GetHistoryFilesCount() const
1494 return m_fileHistory
? m_fileHistory
->GetCount() : 0;
1498 // Find out the document template via matching in the document file format
1499 // against that of the template
1500 wxDocTemplate
*wxDocManager::FindTemplateForPath(const wxString
& path
)
1502 wxDocTemplate
*theTemplate
= (wxDocTemplate
*) NULL
;
1504 // Find the template which this extension corresponds to
1505 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1507 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Item(i
)->GetData();
1508 if ( temp
->FileMatchesTemplate(path
) )
1517 // Try to get a more suitable parent frame than the top window,
1518 // for selection dialogs. Otherwise you may get an unexpected
1519 // window being activated when a dialog is shown.
1520 static wxWindow
* wxFindSuitableParent()
1522 wxWindow
* parent
= wxTheApp
->GetTopWindow();
1524 wxWindow
* focusWindow
= wxWindow::FindFocus();
1527 while (focusWindow
&&
1528 !focusWindow
->IsKindOf(CLASSINFO(wxDialog
)) &&
1529 !focusWindow
->IsKindOf(CLASSINFO(wxFrame
)))
1531 focusWindow
= focusWindow
->GetParent();
1534 parent
= focusWindow
;
1539 // Prompts user to open a file, using file specs in templates.
1540 // Must extend the file selector dialog or implement own; OR
1541 // match the extension to the template extension.
1543 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**templates
,
1544 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
1547 int WXUNUSED(noTemplates
),
1550 long WXUNUSED(flags
),
1551 bool WXUNUSED(save
))
1553 // We can only have multiple filters in Windows and GTK
1554 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
1558 for (i
= 0; i
< noTemplates
; i
++)
1560 if (templates
[i
]->IsVisible())
1562 // add a '|' to separate this filter from the previous one
1563 if ( !descrBuf
.empty() )
1564 descrBuf
<< wxT('|');
1566 descrBuf
<< templates
[i
]->GetDescription()
1567 << wxT(" (") << templates
[i
]->GetFileFilter() << wxT(") |")
1568 << templates
[i
]->GetFileFilter();
1572 wxString descrBuf
= wxT("*.*");
1575 int FilterIndex
= -1;
1577 wxWindow
* parent
= wxFindSuitableParent();
1579 wxString pathTmp
= wxFileSelectorEx(_("Select a file"),
1587 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)NULL
;
1588 if (!pathTmp
.empty())
1590 if (!wxFileExists(pathTmp
))
1593 if (!wxTheApp
->GetAppName().empty())
1594 msgTitle
= wxTheApp
->GetAppName();
1596 msgTitle
= wxString(_("File error"));
1598 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
1601 path
= wxEmptyString
;
1602 return (wxDocTemplate
*) NULL
;
1604 m_lastDirectory
= wxPathOnly(pathTmp
);
1608 // first choose the template using the extension, if this fails (i.e.
1609 // wxFileSelectorEx() didn't fill it), then use the path
1610 if ( FilterIndex
!= -1 )
1611 theTemplate
= templates
[FilterIndex
];
1613 theTemplate
= FindTemplateForPath(path
);
1616 // Since we do not add files with non-default extensions to the FileHistory this
1617 // can only happen if the application changes the allowed templates in runtime.
1618 (void)wxMessageBox(_("Sorry, the format for this file is unknown."),
1620 wxOK
| wxICON_EXCLAMATION
, wxFindSuitableParent());
1625 path
= wxEmptyString
;
1631 wxDocTemplate
*wxDocManager::SelectDocumentType(wxDocTemplate
**templates
,
1632 int noTemplates
, bool sort
)
1634 wxArrayString strings
;
1635 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1639 for (i
= 0; i
< noTemplates
; i
++)
1641 if (templates
[i
]->IsVisible())
1645 for (j
= 0; j
< n
; j
++)
1647 //filter out NOT unique documents + view combinations
1648 if ( templates
[i
]->m_docTypeName
== data
[j
]->m_docTypeName
&&
1649 templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
1656 strings
.Add(templates
[i
]->m_description
);
1658 data
[n
] = templates
[i
];
1666 strings
.Sort(); // ascending sort
1667 // Yes, this will be slow, but template lists
1668 // are typically short.
1670 n
= strings
.Count();
1671 for (i
= 0; i
< n
; i
++)
1673 for (j
= 0; j
< noTemplates
; j
++)
1675 if (strings
[i
] == templates
[j
]->m_description
)
1676 data
[i
] = templates
[j
];
1681 wxDocTemplate
*theTemplate
;
1686 // no visible templates, hence nothing to choose from
1691 // don't propose the user to choose if he heas no choice
1692 theTemplate
= data
[0];
1696 // propose the user to choose one of several
1697 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1699 _("Select a document template"),
1703 wxFindSuitableParent()
1712 wxDocTemplate
*wxDocManager::SelectViewType(wxDocTemplate
**templates
,
1713 int noTemplates
, bool sort
)
1715 wxArrayString strings
;
1716 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1720 for (i
= 0; i
< noTemplates
; i
++)
1722 wxDocTemplate
*templ
= templates
[i
];
1723 if ( templ
->IsVisible() && !templ
->GetViewName().empty() )
1727 for (j
= 0; j
< n
; j
++)
1729 //filter out NOT unique views
1730 if ( templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
)
1736 strings
.Add(templ
->m_viewTypeName
);
1745 strings
.Sort(); // ascending sort
1746 // Yes, this will be slow, but template lists
1747 // are typically short.
1749 n
= strings
.Count();
1750 for (i
= 0; i
< n
; i
++)
1752 for (j
= 0; j
< noTemplates
; j
++)
1754 if (strings
[i
] == templates
[j
]->m_viewTypeName
)
1755 data
[i
] = templates
[j
];
1760 wxDocTemplate
*theTemplate
;
1762 // the same logic as above
1766 theTemplate
= (wxDocTemplate
*)NULL
;
1770 theTemplate
= data
[0];
1774 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1776 _("Select a document view"),
1780 wxFindSuitableParent()
1789 void wxDocManager::AssociateTemplate(wxDocTemplate
*temp
)
1791 if (!m_templates
.Member(temp
))
1792 m_templates
.Append(temp
);
1795 void wxDocManager::DisassociateTemplate(wxDocTemplate
*temp
)
1797 m_templates
.DeleteObject(temp
);
1800 // Add and remove a document from the manager's list
1801 void wxDocManager::AddDocument(wxDocument
*doc
)
1803 if (!m_docs
.Member(doc
))
1807 void wxDocManager::RemoveDocument(wxDocument
*doc
)
1809 m_docs
.DeleteObject(doc
);
1812 // Views or windows should inform the document manager
1813 // when a view is going in or out of focus
1814 void wxDocManager::ActivateView(wxView
*view
, bool activate
)
1818 m_currentView
= view
;
1822 if ( m_currentView
== view
)
1824 // don't keep stale pointer
1825 m_currentView
= (wxView
*) NULL
;
1830 // ----------------------------------------------------------------------------
1831 // Default document child frame
1832 // ----------------------------------------------------------------------------
1834 BEGIN_EVENT_TABLE(wxDocChildFrame
, wxFrame
)
1835 EVT_ACTIVATE(wxDocChildFrame::OnActivate
)
1836 EVT_CLOSE(wxDocChildFrame::OnCloseWindow
)
1839 wxDocChildFrame::wxDocChildFrame(wxDocument
*doc
,
1843 const wxString
& title
,
1847 const wxString
& name
)
1848 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1850 m_childDocument
= doc
;
1853 view
->SetFrame(this);
1856 // Extend event processing to search the view's event table
1857 bool wxDocChildFrame::ProcessEvent(wxEvent
& event
)
1860 m_childView
->Activate(true);
1862 if ( !m_childView
|| ! m_childView
->ProcessEvent(event
) )
1864 // Only hand up to the parent if it's a menu command
1865 if (!event
.IsKindOf(CLASSINFO(wxCommandEvent
)) || !GetParent() || !GetParent()->ProcessEvent(event
))
1866 return wxEvtHandler::ProcessEvent(event
);
1874 void wxDocChildFrame::OnActivate(wxActivateEvent
& event
)
1876 wxFrame::OnActivate(event
);
1879 m_childView
->Activate(event
.GetActive());
1882 void wxDocChildFrame::OnCloseWindow(wxCloseEvent
& event
)
1886 bool ans
= event
.CanVeto()
1887 ? m_childView
->Close(false) // false means don't delete associated window
1888 : true; // Must delete.
1892 m_childView
->Activate(false);
1894 m_childView
= (wxView
*) NULL
;
1895 m_childDocument
= (wxDocument
*) NULL
;
1906 // ----------------------------------------------------------------------------
1907 // Default parent frame
1908 // ----------------------------------------------------------------------------
1910 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1911 EVT_MENU(wxID_EXIT
, wxDocParentFrame::OnExit
)
1912 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE9
, wxDocParentFrame::OnMRUFile
)
1913 EVT_CLOSE(wxDocParentFrame::OnCloseWindow
)
1916 wxDocParentFrame::wxDocParentFrame(wxDocManager
*manager
,
1919 const wxString
& title
,
1923 const wxString
& name
)
1924 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1926 m_docManager
= manager
;
1929 void wxDocParentFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
1934 void wxDocParentFrame::OnMRUFile(wxCommandEvent
& event
)
1936 int n
= event
.GetId() - wxID_FILE1
; // the index in MRU list
1937 wxString
filename(m_docManager
->GetHistoryFile(n
));
1938 if ( !filename
.empty() )
1940 // verify that the file exists before doing anything else
1941 if ( wxFile::Exists(filename
) )
1944 if (!m_docManager
->CreateDocument(filename
, wxDOC_SILENT
))
1946 // remove the file from the MRU list. The user should already be notified.
1947 m_docManager
->RemoveFileFromHistory(n
);
1949 wxLogError(_("The file '%s' couldn't be opened.\nIt has been removed from the most recently used files list."),
1955 // remove the bogus filename from the MRU list and notify the user
1957 m_docManager
->RemoveFileFromHistory(n
);
1959 wxLogError(_("The file '%s' doesn't exist and couldn't be opened.\nIt has been removed from the most recently used files list."),
1965 // Extend event processing to search the view's event table
1966 bool wxDocParentFrame::ProcessEvent(wxEvent
& event
)
1968 // Try the document manager, then do default processing
1969 if (!m_docManager
|| !m_docManager
->ProcessEvent(event
))
1970 return wxEvtHandler::ProcessEvent(event
);
1975 // Define the behaviour for the frame closing
1976 // - must delete all frames except for the main one.
1977 void wxDocParentFrame::OnCloseWindow(wxCloseEvent
& event
)
1979 if (m_docManager
->Clear(!event
.CanVeto()))
1987 #if wxUSE_PRINTING_ARCHITECTURE
1989 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
)
1992 m_printoutView
= view
;
1995 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
1999 // Get the logical pixels per inch of screen and printer
2000 int ppiScreenX
, ppiScreenY
;
2001 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
2002 wxUnusedVar(ppiScreenY
);
2003 int ppiPrinterX
, ppiPrinterY
;
2004 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
2005 wxUnusedVar(ppiPrinterY
);
2007 // This scales the DC so that the printout roughly represents the
2008 // the screen scaling. The text point size _should_ be the right size
2009 // but in fact is too small for some reason. This is a detail that will
2010 // need to be addressed at some point but can be fudged for the
2012 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
2014 // Now we have to check in case our real page size is reduced
2015 // (e.g. because we're drawing to a print preview memory DC)
2016 int pageWidth
, pageHeight
;
2018 dc
->GetSize(&w
, &h
);
2019 GetPageSizePixels(&pageWidth
, &pageHeight
);
2020 wxUnusedVar(pageHeight
);
2022 // If printer pageWidth == current DC width, then this doesn't
2023 // change. But w might be the preview bitmap width, so scale down.
2024 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
2025 dc
->SetUserScale(overallScale
, overallScale
);
2029 m_printoutView
->OnDraw(dc
);
2034 bool wxDocPrintout::HasPage(int pageNum
)
2036 return (pageNum
== 1);
2039 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
2041 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
2047 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
2055 #endif // wxUSE_PRINTING_ARCHITECTURE
2057 // ----------------------------------------------------------------------------
2058 // File history processor
2059 // ----------------------------------------------------------------------------
2061 static inline wxChar
* MYcopystring(const wxString
& s
)
2063 wxChar
* copy
= new wxChar
[s
.length() + 1];
2064 return wxStrcpy(copy
, s
.c_str());
2067 static inline wxChar
* MYcopystring(const wxChar
* s
)
2069 wxChar
* copy
= new wxChar
[wxStrlen(s
) + 1];
2070 return wxStrcpy(copy
, s
);
2073 wxFileHistory::wxFileHistory(size_t maxFiles
, wxWindowID idBase
)
2075 m_fileMaxFiles
= maxFiles
;
2078 m_fileHistory
= new wxChar
*[m_fileMaxFiles
];
2081 wxFileHistory::~wxFileHistory()
2084 for (i
= 0; i
< m_fileHistoryN
; i
++)
2085 delete[] m_fileHistory
[i
];
2086 delete[] m_fileHistory
;
2089 // File history management
2090 void wxFileHistory::AddFileToHistory(const wxString
& file
)
2094 // Check we don't already have this file
2095 for (i
= 0; i
< m_fileHistoryN
; i
++)
2097 #if defined( __WXMSW__ ) // Add any other OSes with case insensitive file names
2098 wxString testString
;
2099 if ( m_fileHistory
[i
] )
2100 testString
= m_fileHistory
[i
];
2101 if ( m_fileHistory
[i
] && ( file
.Lower() == testString
.Lower() ) )
2103 if ( m_fileHistory
[i
] && ( file
== m_fileHistory
[i
] ) )
2106 // we do have it, move it to the top of the history
2107 RemoveFileFromHistory (i
);
2108 AddFileToHistory (file
);
2113 // if we already have a full history, delete the one at the end
2114 if ( m_fileMaxFiles
== m_fileHistoryN
)
2116 RemoveFileFromHistory (m_fileHistoryN
- 1);
2117 AddFileToHistory (file
);
2121 // Add to the project file history:
2122 // Move existing files (if any) down so we can insert file at beginning.
2123 if (m_fileHistoryN
< m_fileMaxFiles
)
2125 wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2128 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2129 if ( m_fileHistoryN
== 0 && menu
->GetMenuItemCount() )
2131 menu
->AppendSeparator();
2133 menu
->Append(m_idBase
+m_fileHistoryN
, _("[EMPTY]"));
2134 node
= node
->GetNext();
2138 // Shuffle filenames down
2139 for (i
= (m_fileHistoryN
-1); i
> 0; i
--)
2141 m_fileHistory
[i
] = m_fileHistory
[i
-1];
2143 m_fileHistory
[0] = MYcopystring(file
);
2145 // this is the directory of the last opened file
2146 wxString pathCurrent
;
2147 wxSplitPath( m_fileHistory
[0], &pathCurrent
, NULL
, NULL
);
2148 for (i
= 0; i
< m_fileHistoryN
; i
++)
2150 if ( m_fileHistory
[i
] )
2152 // if in same directory just show the filename; otherwise the full
2154 wxString pathInMenu
, path
, filename
, ext
;
2155 wxSplitPath( m_fileHistory
[i
], &path
, &filename
, &ext
);
2156 if ( path
== pathCurrent
)
2158 pathInMenu
= filename
;
2160 pathInMenu
= pathInMenu
+ wxFILE_SEP_EXT
+ ext
;
2164 // absolute path; could also set relative path
2165 pathInMenu
= m_fileHistory
[i
];
2168 // we need to quote '&' characters which are used for mnemonics
2169 pathInMenu
.Replace(_T("&"), _T("&&"));
2171 buf
.Printf(s_MRUEntryFormat
, i
+ 1, pathInMenu
.c_str());
2172 wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2175 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2176 menu
->SetLabel(m_idBase
+ i
, buf
);
2177 node
= node
->GetNext();
2183 void wxFileHistory::RemoveFileFromHistory(size_t i
)
2185 wxCHECK_RET( i
< m_fileHistoryN
,
2186 wxT("invalid index in wxFileHistory::RemoveFileFromHistory") );
2188 // delete the element from the array (could use memmove() too...)
2189 delete [] m_fileHistory
[i
];
2192 for ( j
= i
; j
< m_fileHistoryN
- 1; j
++ )
2194 m_fileHistory
[j
] = m_fileHistory
[j
+ 1];
2197 wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2200 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2202 // shuffle filenames up
2204 for ( j
= i
; j
< m_fileHistoryN
- 1; j
++ )
2206 buf
.Printf(s_MRUEntryFormat
, j
+ 1, m_fileHistory
[j
]);
2207 menu
->SetLabel(m_idBase
+ j
, buf
);
2210 node
= node
->GetNext();
2212 // delete the last menu item which is unused now
2213 wxWindowID lastItemId
= m_idBase
+ wx_truncate_cast(wxWindowID
, m_fileHistoryN
) - 1;
2214 if (menu
->FindItem(lastItemId
))
2216 menu
->Delete(lastItemId
);
2219 // delete the last separator too if no more files are left
2220 if ( m_fileHistoryN
== 1 )
2222 wxMenuItemList::compatibility_iterator nodeLast
= menu
->GetMenuItems().GetLast();
2225 wxMenuItem
*menuItem
= nodeLast
->GetData();
2226 if ( menuItem
->IsSeparator() )
2228 menu
->Delete(menuItem
);
2230 //else: should we search backwards for the last separator?
2232 //else: menu is empty somehow
2239 wxString
wxFileHistory::GetHistoryFile(size_t i
) const
2242 if ( i
< m_fileHistoryN
)
2244 s
= m_fileHistory
[i
];
2248 wxFAIL_MSG( wxT("bad index in wxFileHistory::GetHistoryFile") );
2254 void wxFileHistory::UseMenu(wxMenu
*menu
)
2256 if (!m_fileMenus
.Member(menu
))
2257 m_fileMenus
.Append(menu
);
2260 void wxFileHistory::RemoveMenu(wxMenu
*menu
)
2262 m_fileMenus
.DeleteObject(menu
);
2266 void wxFileHistory::Load(wxConfigBase
& config
)
2270 buf
.Printf(wxT("file%d"), (int)m_fileHistoryN
+1);
2271 wxString historyFile
;
2272 while ((m_fileHistoryN
< m_fileMaxFiles
) && config
.Read(buf
, &historyFile
) && (!historyFile
.empty()))
2274 m_fileHistory
[m_fileHistoryN
] = MYcopystring((const wxChar
*) historyFile
);
2276 buf
.Printf(wxT("file%d"), (int)m_fileHistoryN
+1);
2277 historyFile
= wxEmptyString
;
2282 void wxFileHistory::Save(wxConfigBase
& config
)
2285 for (i
= 0; i
< m_fileMaxFiles
; i
++)
2288 buf
.Printf(wxT("file%d"), (int)i
+1);
2289 if (i
< m_fileHistoryN
)
2290 config
.Write(buf
, wxString(m_fileHistory
[i
]));
2292 config
.Write(buf
, wxEmptyString
);
2295 #endif // wxUSE_CONFIG
2297 void wxFileHistory::AddFilesToMenu()
2299 if (m_fileHistoryN
> 0)
2301 wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2304 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2305 if (menu
->GetMenuItemCount())
2307 menu
->AppendSeparator();
2311 for (i
= 0; i
< m_fileHistoryN
; i
++)
2313 if (m_fileHistory
[i
])
2316 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
2317 menu
->Append(m_idBase
+i
, buf
);
2320 node
= node
->GetNext();
2325 void wxFileHistory::AddFilesToMenu(wxMenu
* menu
)
2327 if (m_fileHistoryN
> 0)
2329 if (menu
->GetMenuItemCount())
2331 menu
->AppendSeparator();
2335 for (i
= 0; i
< m_fileHistoryN
; i
++)
2337 if (m_fileHistory
[i
])
2340 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
2341 menu
->Append(m_idBase
+i
, buf
);
2347 // ----------------------------------------------------------------------------
2348 // Permits compatibility with existing file formats and functions that
2349 // manipulate files directly
2350 // ----------------------------------------------------------------------------
2352 #if wxUSE_STD_IOSTREAM
2354 bool wxTransferFileToStream(const wxString
& filename
, wxSTD ostream
& stream
)
2356 wxFFile
file(filename
, _T("rb"));
2357 if ( !file
.IsOpened() )
2365 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2369 stream
.write(buf
, nRead
);
2373 while ( !file
.Eof() );
2378 bool wxTransferStreamToFile(wxSTD istream
& stream
, const wxString
& filename
)
2380 wxFFile
file(filename
, _T("wb"));
2381 if ( !file
.IsOpened() )
2387 stream
.read(buf
, WXSIZEOF(buf
));
2388 if ( !stream
.bad() ) // fail may be set on EOF, don't use operator!()
2390 if ( !file
.Write(buf
, stream
.gcount()) )
2394 while ( !stream
.eof() );
2399 #else // !wxUSE_STD_IOSTREAM
2401 bool wxTransferFileToStream(const wxString
& filename
, wxOutputStream
& stream
)
2403 wxFFile
file(filename
, _T("rb"));
2404 if ( !file
.IsOpened() )
2412 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2416 stream
.Write(buf
, nRead
);
2420 while ( !file
.Eof() );
2425 bool wxTransferStreamToFile(wxInputStream
& stream
, const wxString
& filename
)
2427 wxFFile
file(filename
, _T("wb"));
2428 if ( !file
.IsOpened() )
2434 stream
.Read(buf
, WXSIZEOF(buf
));
2436 const size_t nRead
= stream
.LastRead();
2437 if ( !nRead
|| !file
.Write(buf
, nRead
) )
2440 while ( !stream
.Eof() );
2445 #endif // wxUSE_STD_IOSTREAM/!wxUSE_STD_IOSTREAM
2447 #endif // wxUSE_DOC_VIEW_ARCHITECTURE