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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "docview.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
31 #if wxUSE_DOC_VIEW_ARCHITECTURE
34 #include "wx/string.h"
38 #include "wx/dialog.h"
41 #include "wx/filedlg.h"
49 #include "wx/filename.h"
56 #if wxUSE_PRINTING_ARCHITECTURE
57 #include "wx/prntbase.h"
58 #include "wx/printdlg.h"
61 #include "wx/msgdlg.h"
62 #include "wx/choicdlg.h"
63 #include "wx/docview.h"
64 #include "wx/confbase.h"
66 #include "wx/cmdproc.h"
71 #if wxUSE_STD_IOSTREAM
72 #include "wx/ioswrap.h"
79 #include "wx/wfstream.h"
82 // ----------------------------------------------------------------------------
84 // ----------------------------------------------------------------------------
86 IMPLEMENT_ABSTRACT_CLASS(wxDocument
, wxEvtHandler
)
87 IMPLEMENT_ABSTRACT_CLASS(wxView
, wxEvtHandler
)
88 IMPLEMENT_ABSTRACT_CLASS(wxDocTemplate
, wxObject
)
89 IMPLEMENT_DYNAMIC_CLASS(wxDocManager
, wxEvtHandler
)
90 IMPLEMENT_CLASS(wxDocChildFrame
, wxFrame
)
91 IMPLEMENT_CLASS(wxDocParentFrame
, wxFrame
)
93 #if wxUSE_PRINTING_ARCHITECTURE
94 IMPLEMENT_DYNAMIC_CLASS(wxDocPrintout
, wxPrintout
)
97 IMPLEMENT_DYNAMIC_CLASS(wxFileHistory
, wxObject
)
99 // ----------------------------------------------------------------------------
100 // function prototypes
101 // ----------------------------------------------------------------------------
103 static inline wxString
FindExtension(const wxChar
*path
);
104 static wxWindow
* wxFindSuitableParent(void);
106 // ----------------------------------------------------------------------------
108 // ----------------------------------------------------------------------------
110 static const wxChar
*s_MRUEntryFormat
= wxT("&%d %s");
112 // ============================================================================
114 // ============================================================================
116 // ----------------------------------------------------------------------------
118 // ----------------------------------------------------------------------------
120 static wxString
FindExtension(const wxChar
*path
)
123 wxSplitPath(path
, NULL
, NULL
, &ext
);
125 // VZ: extensions are considered not case sensitive - is this really a good
127 return ext
.MakeLower();
130 // ----------------------------------------------------------------------------
131 // Definition of wxDocument
132 // ----------------------------------------------------------------------------
134 wxDocument::wxDocument(wxDocument
*parent
)
136 m_documentModified
= false;
137 m_documentParent
= parent
;
138 m_documentTemplate
= (wxDocTemplate
*) NULL
;
139 m_commandProcessor
= (wxCommandProcessor
*) NULL
;
143 bool wxDocument::DeleteContents()
148 wxDocument::~wxDocument()
152 if (m_commandProcessor
)
153 delete m_commandProcessor
;
155 if (GetDocumentManager())
156 GetDocumentManager()->RemoveDocument(this);
158 // Not safe to do here, since it'll invoke virtual view functions
159 // expecting to see valid derived objects: and by the time we get here,
160 // we've called destructors higher up.
164 bool wxDocument::Close()
166 if (OnSaveModified())
167 return OnCloseDocument();
172 bool wxDocument::OnCloseDocument()
174 // Tell all views that we're about to close
181 // Note that this implicitly deletes the document when the last view is
183 bool wxDocument::DeleteAllViews()
185 wxDocManager
* manager
= GetDocumentManager();
186 wxList::iterator it
, en
;
188 for ( it
= m_documentViews
.begin(), en
= m_documentViews
.end();
192 wxView
*view
= (wxView
*)*it
;
196 wxList::iterator next
= it
; ++next
;
198 delete view
; // Deletes node implicitly
201 // If we haven't yet deleted the document (for example
202 // if there were no views) then delete it.
203 if (manager
&& manager
->GetDocuments().Member(this))
209 wxView
*wxDocument::GetFirstView() const
211 if (m_documentViews
.GetCount() == 0)
212 return (wxView
*) NULL
;
213 return (wxView
*)m_documentViews
.GetFirst()->GetData();
216 wxDocManager
*wxDocument::GetDocumentManager() const
218 return (m_documentTemplate
? m_documentTemplate
->GetDocumentManager() : (wxDocManager
*) NULL
);
221 bool wxDocument::OnNewDocument()
223 if (!OnSaveModified())
226 if (OnCloseDocument()==false) return false;
229 SetDocumentSaved(false);
232 GetDocumentManager()->MakeDefaultName(name
);
234 SetFilename(name
, true);
239 bool wxDocument::Save()
241 if (!IsModified() && m_savedYet
)
244 if ( m_documentFile
.empty() || !m_savedYet
)
247 return OnSaveDocument(m_documentFile
);
250 bool wxDocument::SaveAs()
252 wxDocTemplate
*docTemplate
= GetDocumentTemplate();
256 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
257 wxString filter
= docTemplate
->GetDescription() + wxT(" (") + docTemplate
->GetFileFilter() + wxT(")|") + docTemplate
->GetFileFilter();
259 // Now see if there are some other template with identical view and document
260 // classes, whose filters may also be used.
262 if (docTemplate
->GetViewClassInfo() && docTemplate
->GetDocClassInfo())
264 wxList::compatibility_iterator node
= wxDocManager::GetDocumentManager()->GetTemplates().GetFirst();
267 wxDocTemplate
*t
= (wxDocTemplate
*) node
->GetData();
269 if (t
->IsVisible() && t
!= docTemplate
&&
270 t
->GetViewClassInfo() == docTemplate
->GetViewClassInfo() &&
271 t
->GetDocClassInfo() == docTemplate
->GetDocClassInfo())
273 // add a '|' to separate this filter from the previous one
274 if ( !filter
.IsEmpty() )
277 filter
<< t
->GetDescription() << wxT(" (") << t
->GetFileFilter() << wxT(") |")
278 << t
->GetFileFilter();
281 node
= node
->GetNext();
285 wxString filter
= docTemplate
->GetFileFilter() ;
287 wxString tmp
= wxFileSelector(_("Save as"),
288 docTemplate
->GetDirectory(),
289 wxFileNameFromPath(GetFilename()),
290 docTemplate
->GetDefaultExtension(),
292 wxSAVE
| wxOVERWRITE_PROMPT
,
293 GetDocumentWindow());
298 wxString
fileName(tmp
);
299 wxString path
, name
, ext
;
300 wxSplitPath(fileName
, & path
, & name
, & ext
);
302 if (ext
.IsEmpty() || ext
== wxT(""))
304 fileName
+= wxT(".");
305 fileName
+= docTemplate
->GetDefaultExtension();
308 SetFilename(fileName
);
309 SetTitle(wxFileNameFromPath(fileName
));
311 // Notify the views that the filename has changed
312 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
315 wxView
*view
= (wxView
*)node
->GetData();
316 view
->OnChangeFilename();
317 node
= node
->GetNext();
320 // Files that were not saved correctly are not added to the FileHistory.
321 if (!OnSaveDocument(m_documentFile
))
324 // A file that doesn't use the default extension of its document template cannot be opened
325 // via the FileHistory, so we do not add it.
326 if (docTemplate
->FileMatchesTemplate(fileName
))
328 GetDocumentManager()->AddFileToHistory(fileName
);
332 // The user will probably not be able to open the file again, so
333 // we could warn about the wrong file-extension here.
338 bool wxDocument::OnSaveDocument(const wxString
& file
)
343 if ( !DoSaveDocument(file
) )
348 SetDocumentSaved(true);
350 wxFileName
fn(file
) ;
351 fn
.MacSetDefaultTypeAndCreator() ;
356 bool wxDocument::OnOpenDocument(const wxString
& file
)
358 if (!OnSaveModified())
361 if ( !DoOpenDocument(file
) )
364 SetFilename(file
, true);
373 #if wxUSE_STD_IOSTREAM
374 wxSTD istream
& wxDocument::LoadObject(wxSTD istream
& stream
)
376 wxInputStream
& wxDocument::LoadObject(wxInputStream
& stream
)
382 #if wxUSE_STD_IOSTREAM
383 wxSTD ostream
& wxDocument::SaveObject(wxSTD ostream
& stream
)
385 wxOutputStream
& wxDocument::SaveObject(wxOutputStream
& stream
)
391 bool wxDocument::Revert()
397 // Get title, or filename if no title, else unnamed
398 bool wxDocument::GetPrintableName(wxString
& buf
) const
400 if (m_documentTitle
!= wxT(""))
402 buf
= m_documentTitle
;
405 else if (m_documentFile
!= wxT(""))
407 buf
= wxFileNameFromPath(m_documentFile
);
417 wxWindow
*wxDocument::GetDocumentWindow() const
419 wxView
*view
= GetFirstView();
421 return view
->GetFrame();
423 return wxTheApp
->GetTopWindow();
426 wxCommandProcessor
*wxDocument::OnCreateCommandProcessor()
428 return new wxCommandProcessor
;
431 // true if safe to close
432 bool wxDocument::OnSaveModified()
437 GetPrintableName(title
);
440 if (wxTheApp
->GetAppName() != wxT(""))
441 msgTitle
= wxTheApp
->GetAppName();
443 msgTitle
= wxString(_("Warning"));
446 prompt
.Printf(_("Do you want to save changes to document %s?"),
447 (const wxChar
*)title
);
448 int res
= wxMessageBox(prompt
, msgTitle
,
449 wxYES_NO
|wxCANCEL
|wxICON_QUESTION
,
450 GetDocumentWindow());
456 else if (res
== wxYES
)
458 else if (res
== wxCANCEL
)
464 bool wxDocument::Draw(wxDC
& WXUNUSED(context
))
469 bool wxDocument::AddView(wxView
*view
)
471 if (!m_documentViews
.Member(view
))
473 m_documentViews
.Append(view
);
479 bool wxDocument::RemoveView(wxView
*view
)
481 (void)m_documentViews
.DeleteObject(view
);
486 bool wxDocument::OnCreate(const wxString
& WXUNUSED(path
), long flags
)
488 if (GetDocumentTemplate()->CreateView(this, flags
))
494 // Called after a view is added or removed.
495 // The default implementation deletes the document if
496 // there are no more views.
497 void wxDocument::OnChangedViewList()
499 if (m_documentViews
.GetCount() == 0)
501 if (OnSaveModified())
508 void wxDocument::UpdateAllViews(wxView
*sender
, wxObject
*hint
)
510 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
513 wxView
*view
= (wxView
*)node
->GetData();
515 view
->OnUpdate(sender
, hint
);
516 node
= node
->GetNext();
520 void wxDocument::NotifyClosing()
522 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
525 wxView
*view
= (wxView
*)node
->GetData();
526 view
->OnClosingDocument();
527 node
= node
->GetNext();
531 void wxDocument::SetFilename(const wxString
& filename
, bool notifyViews
)
533 m_documentFile
= filename
;
536 // Notify the views that the filename has changed
537 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
540 wxView
*view
= (wxView
*)node
->GetData();
541 view
->OnChangeFilename();
542 node
= node
->GetNext();
547 bool wxDocument::DoSaveDocument(const wxString
& file
)
550 if (wxTheApp
->GetAppName() != wxT(""))
551 msgTitle
= wxTheApp
->GetAppName();
553 msgTitle
= wxString(_("File error"));
555 #if wxUSE_STD_IOSTREAM
556 wxSTD ofstream
store(file
.mb_str());
557 if (store
.fail() || store
.bad())
559 wxFileOutputStream
store(file
);
560 if (store
.GetLastError() != wxSTREAM_NO_ERROR
)
563 (void)wxMessageBox(_("Sorry, could not open this file for saving."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
564 GetDocumentWindow());
568 if (!SaveObject(store
))
570 (void)wxMessageBox(_("Sorry, could not save this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
571 GetDocumentWindow());
579 bool wxDocument::DoOpenDocument(const wxString
& file
)
582 if (wxTheApp
->GetAppName() != wxT(""))
583 msgTitle
= wxTheApp
->GetAppName();
585 msgTitle
= wxString(_("File error"));
587 #if wxUSE_STD_IOSTREAM
588 wxSTD ifstream
store(file
.mb_str());
589 if (store
.fail() || store
.bad())
591 wxFileInputStream
store(file
);
592 if (store
.GetLastError() != wxSTREAM_NO_ERROR
)
595 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
|wxICON_EXCLAMATION
,
596 GetDocumentWindow());
599 #if wxUSE_STD_IOSTREAM
601 if ( !store
&& !store
.eof() )
603 int res
= LoadObject(store
).GetLastError();
604 if ((res
!= wxSTREAM_NO_ERROR
) &&
605 (res
!= wxSTREAM_EOF
))
608 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
|wxICON_EXCLAMATION
,
609 GetDocumentWindow());
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 if (GetFrame() && GetDocument())
662 GetDocument()->GetPrintableName(title
);
664 GetFrame()->SetTitle(title
);
668 void wxView::SetDocument(wxDocument
*doc
)
670 m_viewDocument
= doc
;
675 bool wxView::Close(bool deleteWindow
)
677 if (OnClose(deleteWindow
))
683 void wxView::Activate(bool activate
)
685 if (GetDocument() && GetDocumentManager())
687 OnActivateView(activate
, this, GetDocumentManager()->GetCurrentView());
688 GetDocumentManager()->ActivateView(this, activate
);
692 bool wxView::OnClose(bool WXUNUSED(deleteWindow
))
694 return GetDocument() ? GetDocument()->Close() : true;
697 #if wxUSE_PRINTING_ARCHITECTURE
698 wxPrintout
*wxView::OnCreatePrintout()
700 return new wxDocPrintout(this);
702 #endif // wxUSE_PRINTING_ARCHITECTURE
704 // ----------------------------------------------------------------------------
706 // ----------------------------------------------------------------------------
708 wxDocTemplate::wxDocTemplate(wxDocManager
*manager
,
709 const wxString
& descr
,
710 const wxString
& filter
,
713 const wxString
& docTypeName
,
714 const wxString
& viewTypeName
,
715 wxClassInfo
*docClassInfo
,
716 wxClassInfo
*viewClassInfo
,
719 m_documentManager
= manager
;
720 m_description
= descr
;
723 m_fileFilter
= filter
;
725 m_docTypeName
= docTypeName
;
726 m_viewTypeName
= viewTypeName
;
727 m_documentManager
->AssociateTemplate(this);
729 m_docClassInfo
= docClassInfo
;
730 m_viewClassInfo
= viewClassInfo
;
733 wxDocTemplate::~wxDocTemplate()
735 m_documentManager
->DisassociateTemplate(this);
738 // Tries to dynamically construct an object of the right class.
739 wxDocument
*wxDocTemplate::CreateDocument(const wxString
& path
, long flags
)
741 wxDocument
*doc
= DoCreateDocument();
743 return (wxDocument
*) NULL
;
745 if (InitDocument(doc
, path
, flags
))
751 return (wxDocument
*) NULL
;
755 bool wxDocTemplate::InitDocument(wxDocument
* doc
, const wxString
& path
, long flags
)
757 doc
->SetFilename(path
);
758 doc
->SetDocumentTemplate(this);
759 GetDocumentManager()->AddDocument(doc
);
760 doc
->SetCommandProcessor(doc
->OnCreateCommandProcessor());
762 if (doc
->OnCreate(path
, flags
))
766 if (GetDocumentManager()->GetDocuments().Member(doc
))
767 doc
->DeleteAllViews();
772 wxView
*wxDocTemplate::CreateView(wxDocument
*doc
, long flags
)
774 wxView
*view
= DoCreateView();
776 return (wxView
*) NULL
;
778 view
->SetDocument(doc
);
779 if (view
->OnCreate(doc
, flags
))
786 return (wxView
*) NULL
;
790 // The default (very primitive) format detection: check is the extension is
791 // that of the template
792 bool wxDocTemplate::FileMatchesTemplate(const wxString
& path
)
794 return GetDefaultExtension().IsSameAs(FindExtension(path
));
797 wxDocument
*wxDocTemplate::DoCreateDocument()
800 return (wxDocument
*) NULL
;
802 return (wxDocument
*)m_docClassInfo
->CreateObject();
805 wxView
*wxDocTemplate::DoCreateView()
807 if (!m_viewClassInfo
)
808 return (wxView
*) NULL
;
810 return (wxView
*)m_viewClassInfo
->CreateObject();
813 // ----------------------------------------------------------------------------
815 // ----------------------------------------------------------------------------
817 BEGIN_EVENT_TABLE(wxDocManager
, wxEvtHandler
)
818 EVT_MENU(wxID_OPEN
, wxDocManager::OnFileOpen
)
819 EVT_MENU(wxID_CLOSE
, wxDocManager::OnFileClose
)
820 EVT_MENU(wxID_CLOSE_ALL
, wxDocManager::OnFileCloseAll
)
821 EVT_MENU(wxID_REVERT
, wxDocManager::OnFileRevert
)
822 EVT_MENU(wxID_NEW
, wxDocManager::OnFileNew
)
823 EVT_MENU(wxID_SAVE
, wxDocManager::OnFileSave
)
824 EVT_MENU(wxID_SAVEAS
, wxDocManager::OnFileSaveAs
)
825 EVT_MENU(wxID_UNDO
, wxDocManager::OnUndo
)
826 EVT_MENU(wxID_REDO
, wxDocManager::OnRedo
)
828 EVT_UPDATE_UI(wxID_OPEN
, wxDocManager::OnUpdateFileOpen
)
829 EVT_UPDATE_UI(wxID_CLOSE
, wxDocManager::OnUpdateFileClose
)
830 EVT_UPDATE_UI(wxID_CLOSE_ALL
, wxDocManager::OnUpdateFileClose
)
831 EVT_UPDATE_UI(wxID_REVERT
, wxDocManager::OnUpdateFileRevert
)
832 EVT_UPDATE_UI(wxID_NEW
, wxDocManager::OnUpdateFileNew
)
833 EVT_UPDATE_UI(wxID_SAVE
, wxDocManager::OnUpdateFileSave
)
834 EVT_UPDATE_UI(wxID_SAVEAS
, wxDocManager::OnUpdateFileSaveAs
)
835 EVT_UPDATE_UI(wxID_UNDO
, wxDocManager::OnUpdateUndo
)
836 EVT_UPDATE_UI(wxID_REDO
, wxDocManager::OnUpdateRedo
)
838 #if wxUSE_PRINTING_ARCHITECTURE
839 EVT_MENU(wxID_PRINT
, wxDocManager::OnPrint
)
840 EVT_MENU(wxID_PRINT_SETUP
, wxDocManager::OnPrintSetup
)
841 EVT_MENU(wxID_PREVIEW
, wxDocManager::OnPreview
)
843 EVT_UPDATE_UI(wxID_PRINT
, wxDocManager::OnUpdatePrint
)
844 EVT_UPDATE_UI(wxID_PRINT_SETUP
, wxDocManager::OnUpdatePrintSetup
)
845 EVT_UPDATE_UI(wxID_PREVIEW
, wxDocManager::OnUpdatePreview
)
849 wxDocManager
* wxDocManager::sm_docManager
= (wxDocManager
*) NULL
;
851 wxDocManager::wxDocManager(long flags
, bool initialize
)
853 m_defaultDocumentNameCounter
= 1;
855 m_currentView
= (wxView
*) NULL
;
856 m_maxDocsOpen
= 10000;
857 m_fileHistory
= (wxFileHistory
*) NULL
;
860 sm_docManager
= this;
863 wxDocManager::~wxDocManager()
867 delete m_fileHistory
;
868 sm_docManager
= (wxDocManager
*) NULL
;
871 // closes the specified document
872 bool wxDocManager::CloseDocument(wxDocument
* doc
, bool force
)
874 if (doc
->Close() || force
)
876 // Implicitly deletes the document when
877 // the last view is deleted
878 doc
->DeleteAllViews();
880 // Check we're really deleted
881 if (m_docs
.Member(doc
))
889 bool wxDocManager::CloseDocuments(bool force
)
891 wxList::compatibility_iterator node
= m_docs
.GetFirst();
894 wxDocument
*doc
= (wxDocument
*)node
->GetData();
895 wxList::compatibility_iterator next
= node
->GetNext();
897 if (!CloseDocument(doc
, force
))
900 // This assumes that documents are not connected in
901 // any way, i.e. deleting one document does NOT
908 bool wxDocManager::Clear(bool force
)
910 if (!CloseDocuments(force
))
913 wxList::compatibility_iterator node
= m_templates
.GetFirst();
916 wxDocTemplate
*templ
= (wxDocTemplate
*) node
->GetData();
917 wxList::compatibility_iterator next
= node
->GetNext();
924 bool wxDocManager::Initialize()
926 m_fileHistory
= OnCreateFileHistory();
930 wxFileHistory
*wxDocManager::OnCreateFileHistory()
932 return new wxFileHistory
;
935 void wxDocManager::OnFileClose(wxCommandEvent
& WXUNUSED(event
))
937 wxDocument
*doc
= GetCurrentDocument();
942 doc
->DeleteAllViews();
943 if (m_docs
.Member(doc
))
948 void wxDocManager::OnFileCloseAll(wxCommandEvent
& WXUNUSED(event
))
950 CloseDocuments(false);
953 void wxDocManager::OnFileNew(wxCommandEvent
& WXUNUSED(event
))
955 CreateDocument( wxT(""), wxDOC_NEW
);
958 void wxDocManager::OnFileOpen(wxCommandEvent
& WXUNUSED(event
))
960 if ( !CreateDocument( wxT(""), 0) )
966 void wxDocManager::OnFileRevert(wxCommandEvent
& WXUNUSED(event
))
968 wxDocument
*doc
= GetCurrentDocument();
974 void wxDocManager::OnFileSave(wxCommandEvent
& WXUNUSED(event
))
976 wxDocument
*doc
= GetCurrentDocument();
982 void wxDocManager::OnFileSaveAs(wxCommandEvent
& WXUNUSED(event
))
984 wxDocument
*doc
= GetCurrentDocument();
990 void wxDocManager::OnPrint(wxCommandEvent
& WXUNUSED(event
))
992 #if wxUSE_PRINTING_ARCHITECTURE
993 wxView
*view
= GetCurrentView();
997 wxPrintout
*printout
= view
->OnCreatePrintout();
1001 printer
.Print(view
->GetFrame(), printout
, true);
1005 #endif // wxUSE_PRINTING_ARCHITECTURE
1008 void wxDocManager::OnPrintSetup(wxCommandEvent
& WXUNUSED(event
))
1010 #if wxUSE_PRINTING_ARCHITECTURE
1011 wxWindow
*parentWin
= wxTheApp
->GetTopWindow();
1012 wxView
*view
= GetCurrentView();
1014 parentWin
= view
->GetFrame();
1016 wxPrintDialogData data
;
1018 wxPrintDialog
printerDialog(parentWin
, &data
);
1019 printerDialog
.GetPrintDialogData().SetSetupDialog(true);
1020 printerDialog
.ShowModal();
1021 #endif // wxUSE_PRINTING_ARCHITECTURE
1024 void wxDocManager::OnPreview(wxCommandEvent
& WXUNUSED(event
))
1026 #if wxUSE_PRINTING_ARCHITECTURE
1027 wxView
*view
= GetCurrentView();
1031 wxPrintout
*printout
= view
->OnCreatePrintout();
1034 // Pass two printout objects: for preview, and possible printing.
1035 wxPrintPreviewBase
*preview
= new wxPrintPreview(printout
, view
->OnCreatePrintout());
1036 if ( !preview
->Ok() )
1039 wxMessageBox( _("Sorry, print preview needs a printer to be installed.") );
1043 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, (wxFrame
*)wxTheApp
->GetTopWindow(), _("Print Preview"),
1044 wxPoint(100, 100), wxSize(600, 650));
1045 frame
->Centre(wxBOTH
);
1046 frame
->Initialize();
1049 #endif // wxUSE_PRINTING_ARCHITECTURE
1052 void wxDocManager::OnUndo(wxCommandEvent
& event
)
1054 wxDocument
*doc
= GetCurrentDocument();
1057 if (doc
->GetCommandProcessor())
1058 doc
->GetCommandProcessor()->Undo();
1063 void wxDocManager::OnRedo(wxCommandEvent
& event
)
1065 wxDocument
*doc
= GetCurrentDocument();
1068 if (doc
->GetCommandProcessor())
1069 doc
->GetCommandProcessor()->Redo();
1074 // Handlers for UI update commands
1076 void wxDocManager::OnUpdateFileOpen(wxUpdateUIEvent
& event
)
1078 event
.Enable( true );
1081 void wxDocManager::OnUpdateFileClose(wxUpdateUIEvent
& event
)
1083 wxDocument
*doc
= GetCurrentDocument();
1084 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1087 void wxDocManager::OnUpdateFileRevert(wxUpdateUIEvent
& event
)
1089 wxDocument
*doc
= GetCurrentDocument();
1090 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1093 void wxDocManager::OnUpdateFileNew(wxUpdateUIEvent
& event
)
1095 event
.Enable( true );
1098 void wxDocManager::OnUpdateFileSave(wxUpdateUIEvent
& event
)
1100 wxDocument
*doc
= GetCurrentDocument();
1101 event
.Enable( doc
&& doc
->IsModified() );
1104 void wxDocManager::OnUpdateFileSaveAs(wxUpdateUIEvent
& event
)
1106 wxDocument
*doc
= GetCurrentDocument();
1107 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1110 void wxDocManager::OnUpdateUndo(wxUpdateUIEvent
& event
)
1112 wxDocument
*doc
= GetCurrentDocument();
1114 event
.Enable(false);
1115 else if (!doc
->GetCommandProcessor())
1119 event
.Enable( doc
->GetCommandProcessor()->CanUndo() );
1120 doc
->GetCommandProcessor()->SetMenuStrings();
1124 void wxDocManager::OnUpdateRedo(wxUpdateUIEvent
& event
)
1126 wxDocument
*doc
= GetCurrentDocument();
1128 event
.Enable(false);
1129 else if (!doc
->GetCommandProcessor())
1133 event
.Enable( doc
->GetCommandProcessor()->CanRedo() );
1134 doc
->GetCommandProcessor()->SetMenuStrings();
1138 void wxDocManager::OnUpdatePrint(wxUpdateUIEvent
& event
)
1140 wxDocument
*doc
= GetCurrentDocument();
1141 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1144 void wxDocManager::OnUpdatePrintSetup(wxUpdateUIEvent
& event
)
1146 event
.Enable( true );
1149 void wxDocManager::OnUpdatePreview(wxUpdateUIEvent
& event
)
1151 wxDocument
*doc
= GetCurrentDocument();
1152 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1155 wxView
*wxDocManager::GetCurrentView() const
1158 return m_currentView
;
1159 if (m_docs
.GetCount() == 1)
1161 wxDocument
* doc
= (wxDocument
*) m_docs
.GetFirst()->GetData();
1162 return doc
->GetFirstView();
1164 return (wxView
*) NULL
;
1167 // Extend event processing to search the view's event table
1168 bool wxDocManager::ProcessEvent(wxEvent
& event
)
1170 wxView
* view
= GetCurrentView();
1173 if (view
->ProcessEvent(event
))
1176 return wxEvtHandler::ProcessEvent(event
);
1179 wxDocument
*wxDocManager::CreateDocument(const wxString
& path
, long flags
)
1181 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.GetCount()];
1184 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1186 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Item(i
)->GetData());
1187 if (temp
->IsVisible())
1189 templates
[n
] = temp
;
1196 return (wxDocument
*) NULL
;
1199 wxDocument
* docToClose
= NULL
;
1201 // If we've reached the max number of docs, close the
1203 if ( (int)GetDocuments().GetCount() >= m_maxDocsOpen
)
1205 wxDocument
*doc
= (wxDocument
*)GetDocuments().GetFirst()->GetData();
1209 // New document: user chooses a template, unless there's only one.
1210 if (flags
& wxDOC_NEW
)
1216 if (!CloseDocument(docToClose
, false))
1223 wxDocTemplate
*temp
= templates
[0];
1225 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
1229 newDoc
->SetDocumentName(temp
->GetDocumentName());
1230 newDoc
->SetDocumentTemplate(temp
);
1231 newDoc
->OnNewDocument();
1236 wxDocTemplate
*temp
= SelectDocumentType(templates
, n
);
1242 if (!CloseDocument(docToClose
, false))
1248 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
1252 newDoc
->SetDocumentName(temp
->GetDocumentName());
1253 newDoc
->SetDocumentTemplate(temp
);
1254 newDoc
->OnNewDocument();
1259 return (wxDocument
*) NULL
;
1262 // Existing document
1263 wxDocTemplate
*temp
;
1265 wxString
path2(wxT(""));
1266 if (path
!= wxT(""))
1269 if (flags
& wxDOC_SILENT
)
1271 temp
= FindTemplateForPath(path2
);
1274 // Since we do not add files with non-default extensions to the FileHistory this
1275 // can only happen if the application changes the allowed templates in runtime.
1276 (void)wxMessageBox(_("Sorry, the format for this file is unknown."),
1278 wxOK
| wxICON_EXCLAMATION
, wxFindSuitableParent());
1282 temp
= SelectDocumentPath(templates
, n
, path2
, flags
);
1290 if (!CloseDocument(docToClose
, false))
1296 wxDocument
*newDoc
= temp
->CreateDocument(path2
, flags
);
1299 newDoc
->SetDocumentName(temp
->GetDocumentName());
1300 newDoc
->SetDocumentTemplate(temp
);
1301 if (!newDoc
->OnOpenDocument(path2
))
1303 newDoc
->DeleteAllViews();
1304 // delete newDoc; // Implicitly deleted by DeleteAllViews
1305 return (wxDocument
*) NULL
;
1307 // A file that doesn't use the default extension of its document
1308 // template cannot be opened via the FileHistory, so we do not
1310 if (temp
->FileMatchesTemplate(path2
))
1311 AddFileToHistory(path2
);
1316 return (wxDocument
*) NULL
;
1319 wxView
*wxDocManager::CreateView(wxDocument
*doc
, long flags
)
1321 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.GetCount()];
1324 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1326 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Item(i
)->GetData());
1327 if (temp
->IsVisible())
1329 if (temp
->GetDocumentName() == doc
->GetDocumentName())
1331 templates
[n
] = temp
;
1339 return (wxView
*) NULL
;
1343 wxDocTemplate
*temp
= templates
[0];
1345 wxView
*view
= temp
->CreateView(doc
, flags
);
1347 view
->SetViewName(temp
->GetViewName());
1351 wxDocTemplate
*temp
= SelectViewType(templates
, n
);
1355 wxView
*view
= temp
->CreateView(doc
, flags
);
1357 view
->SetViewName(temp
->GetViewName());
1361 return (wxView
*) NULL
;
1364 // Not yet implemented
1365 void wxDocManager::DeleteTemplate(wxDocTemplate
*WXUNUSED(temp
), long WXUNUSED(flags
))
1369 // Not yet implemented
1370 bool wxDocManager::FlushDoc(wxDocument
*WXUNUSED(doc
))
1375 wxDocument
*wxDocManager::GetCurrentDocument() const
1377 wxView
*view
= GetCurrentView();
1379 return view
->GetDocument();
1381 return (wxDocument
*) NULL
;
1384 // Make a default document name
1385 bool wxDocManager::MakeDefaultName(wxString
& name
)
1387 name
.Printf(_("unnamed%d"), m_defaultDocumentNameCounter
);
1388 m_defaultDocumentNameCounter
++;
1393 // Make a frame title (override this to do something different)
1394 // If docName is empty, a document is not currently active.
1395 wxString
wxDocManager::MakeFrameTitle(wxDocument
* doc
)
1397 wxString appName
= wxTheApp
->GetAppName();
1404 doc
->GetPrintableName(docName
);
1405 title
= docName
+ wxString(_(" - ")) + appName
;
1411 // Not yet implemented
1412 wxDocTemplate
*wxDocManager::MatchTemplate(const wxString
& WXUNUSED(path
))
1414 return (wxDocTemplate
*) NULL
;
1417 // File history management
1418 void wxDocManager::AddFileToHistory(const wxString
& file
)
1421 m_fileHistory
->AddFileToHistory(file
);
1424 void wxDocManager::RemoveFileFromHistory(size_t i
)
1427 m_fileHistory
->RemoveFileFromHistory(i
);
1430 wxString
wxDocManager::GetHistoryFile(size_t i
) const
1435 histFile
= m_fileHistory
->GetHistoryFile(i
);
1440 void wxDocManager::FileHistoryUseMenu(wxMenu
*menu
)
1443 m_fileHistory
->UseMenu(menu
);
1446 void wxDocManager::FileHistoryRemoveMenu(wxMenu
*menu
)
1449 m_fileHistory
->RemoveMenu(menu
);
1453 void wxDocManager::FileHistoryLoad(wxConfigBase
& config
)
1456 m_fileHistory
->Load(config
);
1459 void wxDocManager::FileHistorySave(wxConfigBase
& config
)
1462 m_fileHistory
->Save(config
);
1466 void wxDocManager::FileHistoryAddFilesToMenu(wxMenu
* menu
)
1469 m_fileHistory
->AddFilesToMenu(menu
);
1472 void wxDocManager::FileHistoryAddFilesToMenu()
1475 m_fileHistory
->AddFilesToMenu();
1478 size_t wxDocManager::GetHistoryFilesCount() const
1480 return m_fileHistory
? m_fileHistory
->GetCount() : 0;
1484 // Find out the document template via matching in the document file format
1485 // against that of the template
1486 wxDocTemplate
*wxDocManager::FindTemplateForPath(const wxString
& path
)
1488 wxDocTemplate
*theTemplate
= (wxDocTemplate
*) NULL
;
1490 // Find the template which this extension corresponds to
1491 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1493 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Item(i
)->GetData();
1494 if ( temp
->FileMatchesTemplate(path
) )
1503 // Try to get a more suitable parent frame than the top window,
1504 // for selection dialogs. Otherwise you may get an unexpected
1505 // window being activated when a dialog is shown.
1506 static wxWindow
* wxFindSuitableParent()
1508 wxWindow
* parent
= wxTheApp
->GetTopWindow();
1510 wxWindow
* focusWindow
= wxWindow::FindFocus();
1513 while (focusWindow
&&
1514 !focusWindow
->IsKindOf(CLASSINFO(wxDialog
)) &&
1515 !focusWindow
->IsKindOf(CLASSINFO(wxFrame
)))
1517 focusWindow
= focusWindow
->GetParent();
1520 parent
= focusWindow
;
1525 // Prompts user to open a file, using file specs in templates.
1526 // Must extend the file selector dialog or implement own; OR
1527 // match the extension to the template extension.
1529 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**templates
,
1530 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
1533 int WXUNUSED(noTemplates
),
1536 long WXUNUSED(flags
),
1537 bool WXUNUSED(save
))
1539 // We can only have multiple filters in Windows and GTK
1540 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
1544 for (i
= 0; i
< noTemplates
; i
++)
1546 if (templates
[i
]->IsVisible())
1548 // add a '|' to separate this filter from the previous one
1549 if ( !descrBuf
.IsEmpty() )
1550 descrBuf
<< wxT('|');
1552 descrBuf
<< templates
[i
]->GetDescription()
1553 << wxT(" (") << templates
[i
]->GetFileFilter() << wxT(") |")
1554 << templates
[i
]->GetFileFilter();
1558 wxString descrBuf
= wxT("*.*");
1561 int FilterIndex
= -1;
1563 wxWindow
* parent
= wxFindSuitableParent();
1565 wxString pathTmp
= wxFileSelectorEx(_("Select a file"),
1573 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)NULL
;
1574 if (!pathTmp
.IsEmpty())
1576 if (!wxFileExists(pathTmp
))
1579 if (!wxTheApp
->GetAppName().IsEmpty())
1580 msgTitle
= wxTheApp
->GetAppName();
1582 msgTitle
= wxString(_("File error"));
1584 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
1588 return (wxDocTemplate
*) NULL
;
1590 m_lastDirectory
= wxPathOnly(pathTmp
);
1594 // first choose the template using the extension, if this fails (i.e.
1595 // wxFileSelectorEx() didn't fill it), then use the path
1596 if ( FilterIndex
!= -1 )
1597 theTemplate
= templates
[FilterIndex
];
1599 theTemplate
= FindTemplateForPath(path
);
1602 // Since we do not add files with non-default extensions to the FileHistory this
1603 // can only happen if the application changes the allowed templates in runtime.
1604 (void)wxMessageBox(_("Sorry, the format for this file is unknown."),
1606 wxOK
| wxICON_EXCLAMATION
, wxFindSuitableParent());
1617 wxDocTemplate
*wxDocManager::SelectDocumentType(wxDocTemplate
**templates
,
1618 int noTemplates
, bool sort
)
1620 wxArrayString strings
;
1621 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1625 for (i
= 0; i
< noTemplates
; i
++)
1627 if (templates
[i
]->IsVisible())
1631 for (j
= 0; j
< n
; j
++)
1633 //filter out NOT unique documents + view combinations
1634 if ( templates
[i
]->m_docTypeName
== data
[j
]->m_docTypeName
&&
1635 templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
1642 strings
.Add(templates
[i
]->m_description
);
1644 data
[n
] = templates
[i
];
1652 strings
.Sort(); // ascending sort
1653 // Yes, this will be slow, but template lists
1654 // are typically short.
1656 n
= strings
.Count();
1657 for (i
= 0; i
< n
; i
++)
1659 for (j
= 0; j
< noTemplates
; j
++)
1661 if (strings
[i
] == templates
[j
]->m_description
)
1662 data
[i
] = templates
[j
];
1667 wxDocTemplate
*theTemplate
;
1672 // no visible templates, hence nothing to choose from
1677 // don't propose the user to choose if he heas no choice
1678 theTemplate
= data
[0];
1682 // propose the user to choose one of several
1683 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1685 _("Select a document template"),
1689 wxFindSuitableParent()
1698 wxDocTemplate
*wxDocManager::SelectViewType(wxDocTemplate
**templates
,
1699 int noTemplates
, bool sort
)
1701 wxArrayString strings
;
1702 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1706 for (i
= 0; i
< noTemplates
; i
++)
1708 wxDocTemplate
*templ
= templates
[i
];
1709 if ( templ
->IsVisible() && !templ
->GetViewName().empty() )
1713 for (j
= 0; j
< n
; j
++)
1715 //filter out NOT unique views
1716 if ( templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
)
1722 strings
.Add(templ
->m_viewTypeName
);
1731 strings
.Sort(); // ascending sort
1732 // Yes, this will be slow, but template lists
1733 // are typically short.
1735 n
= strings
.Count();
1736 for (i
= 0; i
< n
; i
++)
1738 for (j
= 0; j
< noTemplates
; j
++)
1740 if (strings
[i
] == templates
[j
]->m_viewTypeName
)
1741 data
[i
] = templates
[j
];
1746 wxDocTemplate
*theTemplate
;
1748 // the same logic as above
1752 theTemplate
= (wxDocTemplate
*)NULL
;
1756 theTemplate
= data
[0];
1760 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1762 _("Select a document view"),
1766 wxFindSuitableParent()
1775 void wxDocManager::AssociateTemplate(wxDocTemplate
*temp
)
1777 if (!m_templates
.Member(temp
))
1778 m_templates
.Append(temp
);
1781 void wxDocManager::DisassociateTemplate(wxDocTemplate
*temp
)
1783 m_templates
.DeleteObject(temp
);
1786 // Add and remove a document from the manager's list
1787 void wxDocManager::AddDocument(wxDocument
*doc
)
1789 if (!m_docs
.Member(doc
))
1793 void wxDocManager::RemoveDocument(wxDocument
*doc
)
1795 m_docs
.DeleteObject(doc
);
1798 // Views or windows should inform the document manager
1799 // when a view is going in or out of focus
1800 void wxDocManager::ActivateView(wxView
*view
, bool activate
)
1804 m_currentView
= view
;
1808 if ( m_currentView
== view
)
1810 // don't keep stale pointer
1811 m_currentView
= (wxView
*) NULL
;
1816 // ----------------------------------------------------------------------------
1817 // Default document child frame
1818 // ----------------------------------------------------------------------------
1820 BEGIN_EVENT_TABLE(wxDocChildFrame
, wxFrame
)
1821 EVT_ACTIVATE(wxDocChildFrame::OnActivate
)
1822 EVT_CLOSE(wxDocChildFrame::OnCloseWindow
)
1825 wxDocChildFrame::wxDocChildFrame(wxDocument
*doc
,
1829 const wxString
& title
,
1833 const wxString
& name
)
1834 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1836 m_childDocument
= doc
;
1839 view
->SetFrame(this);
1842 wxDocChildFrame::~wxDocChildFrame()
1846 // Extend event processing to search the view's event table
1847 bool wxDocChildFrame::ProcessEvent(wxEvent
& event
)
1850 m_childView
->Activate(true);
1852 if ( !m_childView
|| ! m_childView
->ProcessEvent(event
) )
1854 // Only hand up to the parent if it's a menu command
1855 if (!event
.IsKindOf(CLASSINFO(wxCommandEvent
)) || !GetParent() || !GetParent()->ProcessEvent(event
))
1856 return wxEvtHandler::ProcessEvent(event
);
1864 void wxDocChildFrame::OnActivate(wxActivateEvent
& event
)
1866 wxFrame::OnActivate(event
);
1869 m_childView
->Activate(event
.GetActive());
1872 void wxDocChildFrame::OnCloseWindow(wxCloseEvent
& event
)
1876 bool ans
= event
.CanVeto()
1877 ? m_childView
->Close(false) // false means don't delete associated window
1878 : true; // Must delete.
1882 m_childView
->Activate(false);
1884 m_childView
= (wxView
*) NULL
;
1885 m_childDocument
= (wxDocument
*) NULL
;
1896 // ----------------------------------------------------------------------------
1897 // Default parent frame
1898 // ----------------------------------------------------------------------------
1900 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1901 EVT_MENU(wxID_EXIT
, wxDocParentFrame::OnExit
)
1902 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE9
, wxDocParentFrame::OnMRUFile
)
1903 EVT_CLOSE(wxDocParentFrame::OnCloseWindow
)
1906 wxDocParentFrame::wxDocParentFrame(wxDocManager
*manager
,
1909 const wxString
& title
,
1913 const wxString
& name
)
1914 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1916 m_docManager
= manager
;
1919 void wxDocParentFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
1924 void wxDocParentFrame::OnMRUFile(wxCommandEvent
& event
)
1926 int n
= event
.GetId() - wxID_FILE1
; // the index in MRU list
1927 wxString
filename(m_docManager
->GetHistoryFile(n
));
1928 if ( !filename
.IsEmpty() )
1930 // verify that the file exists before doing anything else
1931 if ( wxFile::Exists(filename
) )
1934 if (!m_docManager
->CreateDocument(filename
, wxDOC_SILENT
))
1936 // remove the file from the MRU list. The user should already be notified.
1937 m_docManager
->RemoveFileFromHistory(n
);
1939 wxLogError(_("The file '%s' couldn't be opened.\nIt has been removed from the most recently used files list."),
1945 // remove the bogus filename from the MRU list and notify the user
1947 m_docManager
->RemoveFileFromHistory(n
);
1949 wxLogError(_("The file '%s' doesn't exist and couldn't be opened.\nIt has been removed from the most recently used files list."),
1955 // Extend event processing to search the view's event table
1956 bool wxDocParentFrame::ProcessEvent(wxEvent
& event
)
1958 // Try the document manager, then do default processing
1959 if (!m_docManager
|| !m_docManager
->ProcessEvent(event
))
1960 return wxEvtHandler::ProcessEvent(event
);
1965 // Define the behaviour for the frame closing
1966 // - must delete all frames except for the main one.
1967 void wxDocParentFrame::OnCloseWindow(wxCloseEvent
& event
)
1969 if (m_docManager
->Clear(!event
.CanVeto()))
1977 #if wxUSE_PRINTING_ARCHITECTURE
1979 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
)
1982 m_printoutView
= view
;
1985 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
1989 // Get the logical pixels per inch of screen and printer
1990 int ppiScreenX
, ppiScreenY
;
1991 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
1992 wxUnusedVar(ppiScreenY
);
1993 int ppiPrinterX
, ppiPrinterY
;
1994 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
1995 wxUnusedVar(ppiPrinterY
);
1997 // This scales the DC so that the printout roughly represents the
1998 // the screen scaling. The text point size _should_ be the right size
1999 // but in fact is too small for some reason. This is a detail that will
2000 // need to be addressed at some point but can be fudged for the
2002 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
2004 // Now we have to check in case our real page size is reduced
2005 // (e.g. because we're drawing to a print preview memory DC)
2006 int pageWidth
, pageHeight
;
2008 dc
->GetSize(&w
, &h
);
2009 GetPageSizePixels(&pageWidth
, &pageHeight
);
2010 wxUnusedVar(pageHeight
);
2012 // If printer pageWidth == current DC width, then this doesn't
2013 // change. But w might be the preview bitmap width, so scale down.
2014 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
2015 dc
->SetUserScale(overallScale
, overallScale
);
2019 m_printoutView
->OnDraw(dc
);
2024 bool wxDocPrintout::HasPage(int pageNum
)
2026 return (pageNum
== 1);
2029 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
2031 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
2037 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
2045 #endif // wxUSE_PRINTING_ARCHITECTURE
2047 // ----------------------------------------------------------------------------
2048 // File history processor
2049 // ----------------------------------------------------------------------------
2051 static inline wxChar
* MYcopystring(const wxString
& s
)
2053 wxChar
* copy
= new wxChar
[s
.length() + 1];
2054 return wxStrcpy(copy
, s
.c_str());
2057 static inline wxChar
* MYcopystring(const wxChar
* s
)
2059 wxChar
* copy
= new wxChar
[wxStrlen(s
) + 1];
2060 return wxStrcpy(copy
, s
);
2063 wxFileHistory::wxFileHistory(size_t maxFiles
, wxWindowID idBase
)
2065 m_fileMaxFiles
= maxFiles
;
2068 m_fileHistory
= new wxChar
*[m_fileMaxFiles
];
2071 wxFileHistory::~wxFileHistory()
2074 for (i
= 0; i
< m_fileHistoryN
; i
++)
2075 delete[] m_fileHistory
[i
];
2076 delete[] m_fileHistory
;
2079 // File history management
2080 void wxFileHistory::AddFileToHistory(const wxString
& file
)
2084 // Check we don't already have this file
2085 for (i
= 0; i
< m_fileHistoryN
; i
++)
2087 #if defined( __WXMSW__ ) // Add any other OSes with case insensitive file names
2088 wxString testString
;
2089 if ( m_fileHistory
[i
] )
2090 testString
= m_fileHistory
[i
];
2091 if ( m_fileHistory
[i
] && ( file
.Lower() == testString
.Lower() ) )
2093 if ( m_fileHistory
[i
] && ( file
== m_fileHistory
[i
] ) )
2096 // we do have it, move it to the top of the history
2097 RemoveFileFromHistory (i
);
2098 AddFileToHistory (file
);
2103 // if we already have a full history, delete the one at the end
2104 if ( m_fileMaxFiles
== m_fileHistoryN
)
2106 RemoveFileFromHistory (m_fileHistoryN
- 1);
2107 AddFileToHistory (file
);
2111 // Add to the project file history:
2112 // Move existing files (if any) down so we can insert file at beginning.
2113 if (m_fileHistoryN
< m_fileMaxFiles
)
2115 wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2118 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2119 if ( m_fileHistoryN
== 0 && menu
->GetMenuItemCount() )
2121 menu
->AppendSeparator();
2123 menu
->Append(m_idBase
+m_fileHistoryN
, _("[EMPTY]"));
2124 node
= node
->GetNext();
2128 // Shuffle filenames down
2129 for (i
= (m_fileHistoryN
-1); i
> 0; i
--)
2131 m_fileHistory
[i
] = m_fileHistory
[i
-1];
2133 m_fileHistory
[0] = MYcopystring(file
);
2135 // this is the directory of the last opened file
2136 wxString pathCurrent
;
2137 wxSplitPath( m_fileHistory
[0], &pathCurrent
, NULL
, NULL
);
2138 for (i
= 0; i
< m_fileHistoryN
; i
++)
2140 if ( m_fileHistory
[i
] )
2142 // if in same directory just show the filename; otherwise the full
2144 wxString pathInMenu
, path
, filename
, ext
;
2145 wxSplitPath( m_fileHistory
[i
], &path
, &filename
, &ext
);
2146 if ( path
== pathCurrent
)
2148 pathInMenu
= filename
;
2150 pathInMenu
= pathInMenu
+ wxFILE_SEP_EXT
+ ext
;
2154 // absolute path; could also set relative path
2155 pathInMenu
= m_fileHistory
[i
];
2159 buf
.Printf(s_MRUEntryFormat
, i
+ 1, pathInMenu
.c_str());
2160 wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2163 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2164 menu
->SetLabel(m_idBase
+ i
, buf
);
2165 node
= node
->GetNext();
2171 void wxFileHistory::RemoveFileFromHistory(size_t i
)
2173 wxCHECK_RET( i
< m_fileHistoryN
,
2174 wxT("invalid index in wxFileHistory::RemoveFileFromHistory") );
2176 // delete the element from the array (could use memmove() too...)
2177 delete [] m_fileHistory
[i
];
2180 for ( j
= i
; j
< m_fileHistoryN
- 1; j
++ )
2182 m_fileHistory
[j
] = m_fileHistory
[j
+ 1];
2185 wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2188 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2190 // shuffle filenames up
2192 for ( j
= i
; j
< m_fileHistoryN
- 1; j
++ )
2194 buf
.Printf(s_MRUEntryFormat
, j
+ 1, m_fileHistory
[j
]);
2195 menu
->SetLabel(m_idBase
+ j
, buf
);
2198 node
= node
->GetNext();
2200 // delete the last menu item which is unused now
2201 wxWindowID lastItemId
= m_idBase
+ m_fileHistoryN
- 1;
2202 if (menu
->FindItem(lastItemId
))
2204 menu
->Delete(lastItemId
);
2207 // delete the last separator too if no more files are left
2208 if ( m_fileHistoryN
== 1 )
2210 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetLast();
2213 wxMenuItem
*menuItem
= node
->GetData();
2214 if ( menuItem
->IsSeparator() )
2216 menu
->Delete(menuItem
);
2218 //else: should we search backwards for the last separator?
2220 //else: menu is empty somehow
2227 wxString
wxFileHistory::GetHistoryFile(size_t i
) const
2230 if ( i
< m_fileHistoryN
)
2232 s
= m_fileHistory
[i
];
2236 wxFAIL_MSG( wxT("bad index in wxFileHistory::GetHistoryFile") );
2242 void wxFileHistory::UseMenu(wxMenu
*menu
)
2244 if (!m_fileMenus
.Member(menu
))
2245 m_fileMenus
.Append(menu
);
2248 void wxFileHistory::RemoveMenu(wxMenu
*menu
)
2250 m_fileMenus
.DeleteObject(menu
);
2254 void wxFileHistory::Load(wxConfigBase
& config
)
2258 buf
.Printf(wxT("file%d"), (int)m_fileHistoryN
+1);
2259 wxString historyFile
;
2260 while ((m_fileHistoryN
< m_fileMaxFiles
) && config
.Read(buf
, &historyFile
) && (historyFile
!= wxT("")))
2262 m_fileHistory
[m_fileHistoryN
] = MYcopystring((const wxChar
*) historyFile
);
2264 buf
.Printf(wxT("file%d"), (int)m_fileHistoryN
+1);
2265 historyFile
= wxT("");
2270 void wxFileHistory::Save(wxConfigBase
& config
)
2273 for (i
= 0; i
< m_fileMaxFiles
; i
++)
2276 buf
.Printf(wxT("file%d"), (int)i
+1);
2277 if (i
< m_fileHistoryN
)
2278 config
.Write(buf
, wxString(m_fileHistory
[i
]));
2280 config
.Write(buf
, wxEmptyString
);
2283 #endif // wxUSE_CONFIG
2285 void wxFileHistory::AddFilesToMenu()
2287 if (m_fileHistoryN
> 0)
2289 wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2292 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2293 if (menu
->GetMenuItemCount())
2295 menu
->AppendSeparator();
2299 for (i
= 0; i
< m_fileHistoryN
; i
++)
2301 if (m_fileHistory
[i
])
2304 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
2305 menu
->Append(m_idBase
+i
, buf
);
2308 node
= node
->GetNext();
2313 void wxFileHistory::AddFilesToMenu(wxMenu
* menu
)
2315 if (m_fileHistoryN
> 0)
2317 if (menu
->GetMenuItemCount())
2319 menu
->AppendSeparator();
2323 for (i
= 0; i
< m_fileHistoryN
; i
++)
2325 if (m_fileHistory
[i
])
2328 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
2329 menu
->Append(m_idBase
+i
, buf
);
2335 // ----------------------------------------------------------------------------
2336 // Permits compatibility with existing file formats and functions that
2337 // manipulate files directly
2338 // ----------------------------------------------------------------------------
2340 #if wxUSE_STD_IOSTREAM
2342 bool wxTransferFileToStream(const wxString
& filename
, wxSTD ostream
& stream
)
2344 wxFFile
file(filename
, _T("rb"));
2345 if ( !file
.IsOpened() )
2353 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2357 stream
.write(buf
, nRead
);
2361 while ( !file
.Eof() );
2366 bool wxTransferStreamToFile(wxSTD istream
& stream
, const wxString
& filename
)
2368 wxFFile
file(filename
, _T("wb"));
2369 if ( !file
.IsOpened() )
2375 stream
.read(buf
, WXSIZEOF(buf
));
2376 if ( !stream
.bad() ) // fail may be set on EOF, don't use operator!()
2378 if ( !file
.Write(buf
, stream
.gcount()) )
2382 while ( !stream
.eof() );
2387 #else // !wxUSE_STD_IOSTREAM
2389 bool wxTransferFileToStream(const wxString
& filename
, wxOutputStream
& stream
)
2391 wxFFile
file(filename
, _T("rb"));
2392 if ( !file
.IsOpened() )
2400 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2404 stream
.Write(buf
, nRead
);
2408 while ( !file
.Eof() );
2413 bool wxTransferStreamToFile(wxInputStream
& stream
, const wxString
& filename
)
2415 wxFFile
file(filename
, _T("wb"));
2416 if ( !file
.IsOpened() )
2422 stream
.Read(buf
, WXSIZEOF(buf
));
2424 const size_t nRead
= stream
.LastRead();
2425 if ( !nRead
|| !file
.Write(buf
, nRead
) )
2428 while ( !stream
.Eof() );
2433 #endif // wxUSE_STD_IOSTREAM/!wxUSE_STD_IOSTREAM
2435 #endif // wxUSE_DOC_VIEW_ARCHITECTURE