1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/docview.cpp
3 // Purpose: Document/view classes
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #if wxUSE_DOC_VIEW_ARCHITECTURE
29 #include "wx/docview.h"
33 #include "wx/string.h"
37 #include "wx/dialog.h"
39 #include "wx/filedlg.h"
42 #include "wx/msgdlg.h"
44 #include "wx/choicdlg.h"
50 #include "wx/filename.h"
53 #if wxUSE_PRINTING_ARCHITECTURE
54 #include "wx/prntbase.h"
55 #include "wx/printdlg.h"
58 #include "wx/confbase.h"
60 #include "wx/cmdproc.h"
61 #include "wx/tokenzr.h"
62 #include "wx/filename.h"
67 #if wxUSE_STD_IOSTREAM
68 #include "wx/ioswrap.h"
75 #include "wx/wfstream.h"
78 // ----------------------------------------------------------------------------
80 // ----------------------------------------------------------------------------
82 IMPLEMENT_ABSTRACT_CLASS(wxDocument
, wxEvtHandler
)
83 IMPLEMENT_ABSTRACT_CLASS(wxView
, wxEvtHandler
)
84 IMPLEMENT_ABSTRACT_CLASS(wxDocTemplate
, wxObject
)
85 IMPLEMENT_DYNAMIC_CLASS(wxDocManager
, wxEvtHandler
)
86 IMPLEMENT_CLASS(wxDocChildFrame
, wxFrame
)
87 IMPLEMENT_CLASS(wxDocParentFrame
, wxFrame
)
89 #if wxUSE_PRINTING_ARCHITECTURE
90 IMPLEMENT_DYNAMIC_CLASS(wxDocPrintout
, wxPrintout
)
93 IMPLEMENT_DYNAMIC_CLASS(wxFileHistory
, wxObject
)
95 // ----------------------------------------------------------------------------
96 // function prototypes
97 // ----------------------------------------------------------------------------
99 static wxWindow
* wxFindSuitableParent(void);
101 // ----------------------------------------------------------------------------
103 // ----------------------------------------------------------------------------
105 static const wxChar
*s_MRUEntryFormat
= wxT("&%d %s");
107 // ============================================================================
109 // ============================================================================
111 // ----------------------------------------------------------------------------
113 // ----------------------------------------------------------------------------
115 static wxString
FindExtension(const wxString
& path
)
118 wxSplitPath(path
, NULL
, NULL
, &ext
);
120 // VZ: extensions are considered not case sensitive - is this really a good
122 return ext
.MakeLower();
125 // ----------------------------------------------------------------------------
126 // Definition of wxDocument
127 // ----------------------------------------------------------------------------
129 wxDocument::wxDocument(wxDocument
*parent
)
131 m_documentModified
= false;
132 m_documentParent
= parent
;
133 m_documentTemplate
= (wxDocTemplate
*) NULL
;
134 m_commandProcessor
= (wxCommandProcessor
*) NULL
;
138 bool wxDocument::DeleteContents()
143 wxDocument::~wxDocument()
147 if (m_commandProcessor
)
148 delete m_commandProcessor
;
150 if (GetDocumentManager())
151 GetDocumentManager()->RemoveDocument(this);
153 // Not safe to do here, since it'll invoke virtual view functions
154 // expecting to see valid derived objects: and by the time we get here,
155 // we've called destructors higher up.
159 bool wxDocument::Close()
161 if (OnSaveModified())
162 return OnCloseDocument();
167 bool wxDocument::OnCloseDocument()
169 // Tell all views that we're about to close
176 // Note that this implicitly deletes the document when the last view is
178 bool wxDocument::DeleteAllViews()
180 wxDocManager
* manager
= GetDocumentManager();
182 // first check if all views agree to be closed
183 const wxList::iterator end
= m_documentViews
.end();
184 for ( wxList::iterator i
= m_documentViews
.begin(); i
!= end
; ++i
)
186 wxView
*view
= (wxView
*)*i
;
187 if ( !view
->Close() )
191 // all views agreed to close, now do close them
192 if ( m_documentViews
.empty() )
194 // normally the document would be implicitly deleted when the last view
195 // is, but if don't have any views, do it here instead
196 if ( manager
&& manager
->GetDocuments().Member(this) )
201 // as we delete elements we iterate over, don't use the usual "from
202 // begin to end" loop
205 wxView
*view
= (wxView
*)*m_documentViews
.begin();
207 bool isLastOne
= m_documentViews
.size() == 1;
209 // this always deletes the node implicitly and if this is the last
210 // view also deletes this object itself (also implicitly, great),
211 // so we can't test for m_documentViews.empty() after calling this!
222 wxView
*wxDocument::GetFirstView() const
224 if (m_documentViews
.GetCount() == 0)
225 return (wxView
*) NULL
;
226 return (wxView
*)m_documentViews
.GetFirst()->GetData();
229 wxDocManager
*wxDocument::GetDocumentManager() const
231 return (m_documentTemplate
? m_documentTemplate
->GetDocumentManager() : (wxDocManager
*) NULL
);
234 bool wxDocument::OnNewDocument()
236 if (!OnSaveModified())
239 if (OnCloseDocument()==false) return false;
242 SetDocumentSaved(false);
245 GetDocumentManager()->MakeDefaultName(name
);
247 SetFilename(name
, true);
252 bool wxDocument::Save()
254 if (!IsModified() && m_savedYet
)
257 if ( m_documentFile
.empty() || !m_savedYet
)
260 return OnSaveDocument(m_documentFile
);
263 bool wxDocument::SaveAs()
265 wxDocTemplate
*docTemplate
= GetDocumentTemplate();
269 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
270 wxString filter
= docTemplate
->GetDescription() + wxT(" (") + docTemplate
->GetFileFilter() + wxT(")|") + docTemplate
->GetFileFilter();
272 // Now see if there are some other template with identical view and document
273 // classes, whose filters may also be used.
275 if (docTemplate
->GetViewClassInfo() && docTemplate
->GetDocClassInfo())
277 wxList::compatibility_iterator node
= docTemplate
->GetDocumentManager()->GetTemplates().GetFirst();
280 wxDocTemplate
*t
= (wxDocTemplate
*) node
->GetData();
282 if (t
->IsVisible() && t
!= docTemplate
&&
283 t
->GetViewClassInfo() == docTemplate
->GetViewClassInfo() &&
284 t
->GetDocClassInfo() == docTemplate
->GetDocClassInfo())
286 // add a '|' to separate this filter from the previous one
287 if ( !filter
.empty() )
290 filter
<< t
->GetDescription() << wxT(" (") << t
->GetFileFilter() << wxT(") |")
291 << t
->GetFileFilter();
294 node
= node
->GetNext();
298 wxString filter
= docTemplate
->GetFileFilter() ;
300 wxString tmp
= wxFileSelector(_("Save as"),
301 docTemplate
->GetDirectory(),
302 wxFileNameFromPath(GetFilename()),
303 docTemplate
->GetDefaultExtension(),
305 wxFD_SAVE
| wxFD_OVERWRITE_PROMPT
,
306 GetDocumentWindow());
311 wxString
fileName(tmp
);
312 wxString path
, name
, ext
;
313 wxSplitPath(fileName
, & path
, & name
, & ext
);
317 fileName
+= wxT(".");
318 fileName
+= docTemplate
->GetDefaultExtension();
321 SetFilename(fileName
);
322 SetTitle(wxFileNameFromPath(fileName
));
324 // Notify the views that the filename has changed
325 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
328 wxView
*view
= (wxView
*)node
->GetData();
329 view
->OnChangeFilename();
330 node
= node
->GetNext();
333 // Files that were not saved correctly are not added to the FileHistory.
334 if (!OnSaveDocument(m_documentFile
))
337 // A file that doesn't use the default extension of its document template cannot be opened
338 // via the FileHistory, so we do not add it.
339 if (docTemplate
->FileMatchesTemplate(fileName
))
341 GetDocumentManager()->AddFileToHistory(fileName
);
345 // The user will probably not be able to open the file again, so
346 // we could warn about the wrong file-extension here.
351 bool wxDocument::OnSaveDocument(const wxString
& file
)
356 if ( !DoSaveDocument(file
) )
361 SetDocumentSaved(true);
363 wxFileName
fn(file
) ;
364 fn
.MacSetDefaultTypeAndCreator() ;
369 bool wxDocument::OnOpenDocument(const wxString
& file
)
371 if (!OnSaveModified())
374 if ( !DoOpenDocument(file
) )
377 SetFilename(file
, true);
386 #if wxUSE_STD_IOSTREAM
387 wxSTD istream
& wxDocument::LoadObject(wxSTD istream
& stream
)
389 wxInputStream
& wxDocument::LoadObject(wxInputStream
& stream
)
395 #if wxUSE_STD_IOSTREAM
396 wxSTD ostream
& wxDocument::SaveObject(wxSTD ostream
& stream
)
398 wxOutputStream
& wxDocument::SaveObject(wxOutputStream
& stream
)
404 bool wxDocument::Revert()
410 // Get title, or filename if no title, else unnamed
411 bool wxDocument::GetPrintableName(wxString
& buf
) const
413 if (!m_documentTitle
.empty())
415 buf
= m_documentTitle
;
418 else if (!m_documentFile
.empty())
420 buf
= wxFileNameFromPath(m_documentFile
);
430 wxWindow
*wxDocument::GetDocumentWindow() const
432 wxView
*view
= GetFirstView();
434 return view
->GetFrame();
436 return wxTheApp
->GetTopWindow();
439 wxCommandProcessor
*wxDocument::OnCreateCommandProcessor()
441 return new wxCommandProcessor
;
444 // true if safe to close
445 bool wxDocument::OnSaveModified()
450 GetPrintableName(title
);
453 if (!wxTheApp
->GetAppName().empty())
454 msgTitle
= wxTheApp
->GetAppName();
456 msgTitle
= wxString(_("Warning"));
459 prompt
.Printf(_("Do you want to save changes to document %s?"), title
);
460 int res
= wxMessageBox(prompt
, msgTitle
,
461 wxYES_NO
|wxCANCEL
|wxICON_QUESTION
,
462 GetDocumentWindow());
468 else if (res
== wxYES
)
470 else if (res
== wxCANCEL
)
476 bool wxDocument::Draw(wxDC
& WXUNUSED(context
))
481 bool wxDocument::AddView(wxView
*view
)
483 if (!m_documentViews
.Member(view
))
485 m_documentViews
.Append(view
);
491 bool wxDocument::RemoveView(wxView
*view
)
493 (void)m_documentViews
.DeleteObject(view
);
498 bool wxDocument::OnCreate(const wxString
& WXUNUSED(path
), long flags
)
500 if (GetDocumentTemplate()->CreateView(this, flags
))
506 // Called after a view is added or removed.
507 // The default implementation deletes the document if
508 // there are no more views.
509 void wxDocument::OnChangedViewList()
511 if (m_documentViews
.GetCount() == 0)
513 if (OnSaveModified())
520 void wxDocument::UpdateAllViews(wxView
*sender
, wxObject
*hint
)
522 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
525 wxView
*view
= (wxView
*)node
->GetData();
527 view
->OnUpdate(sender
, hint
);
528 node
= node
->GetNext();
532 void wxDocument::NotifyClosing()
534 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
537 wxView
*view
= (wxView
*)node
->GetData();
538 view
->OnClosingDocument();
539 node
= node
->GetNext();
543 void wxDocument::SetFilename(const wxString
& filename
, bool notifyViews
)
545 m_documentFile
= filename
;
548 // Notify the views that the filename has changed
549 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
552 wxView
*view
= (wxView
*)node
->GetData();
553 view
->OnChangeFilename();
554 node
= node
->GetNext();
559 bool wxDocument::DoSaveDocument(const wxString
& file
)
562 if (!wxTheApp
->GetAppName().empty())
563 msgTitle
= wxTheApp
->GetAppName();
565 msgTitle
= wxString(_("File error"));
567 #if wxUSE_STD_IOSTREAM
568 wxSTD ofstream
store(file
.mb_str(), wxSTD
ios::binary
);
569 if (store
.fail() || store
.bad())
571 wxFileOutputStream
store(file
);
572 if (store
.GetLastError() != wxSTREAM_NO_ERROR
)
575 (void)wxMessageBox(_("Sorry, could not open this file for saving."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
576 GetDocumentWindow());
580 if (!SaveObject(store
))
582 (void)wxMessageBox(_("Sorry, could not save this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
583 GetDocumentWindow());
591 bool wxDocument::DoOpenDocument(const wxString
& file
)
593 #if wxUSE_STD_IOSTREAM
594 wxSTD ifstream
store(file
.mb_str(), wxSTD
ios::binary
);
595 if (!store
.fail() && !store
.bad())
597 wxFileInputStream
store(file
);
598 if (store
.GetLastError() == wxSTREAM_NO_ERROR
)
601 #if wxUSE_STD_IOSTREAM
603 if ( !!store
|| store
.eof() )
605 int res
= LoadObject(store
).GetLastError();
606 if ( res
== wxSTREAM_NO_ERROR
|| res
== wxSTREAM_EOF
)
611 wxLogError(_("Sorry, could not open this file."));
616 // ----------------------------------------------------------------------------
618 // ----------------------------------------------------------------------------
622 m_viewDocument
= (wxDocument
*) NULL
;
624 m_viewFrame
= (wxFrame
*) NULL
;
629 GetDocumentManager()->ActivateView(this, false);
630 m_viewDocument
->RemoveView(this);
633 // Extend event processing to search the document's event table
634 bool wxView::ProcessEvent(wxEvent
& event
)
636 if ( !GetDocument() || !GetDocument()->ProcessEvent(event
) )
637 return wxEvtHandler::ProcessEvent(event
);
642 void wxView::OnActivateView(bool WXUNUSED(activate
), wxView
*WXUNUSED(activeView
), wxView
*WXUNUSED(deactiveView
))
646 void wxView::OnPrint(wxDC
*dc
, wxObject
*WXUNUSED(info
))
651 void wxView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
))
655 void wxView::OnChangeFilename()
657 // GetFrame can return wxWindow rather than wxTopLevelWindow due to
658 // generic MDI implementation so use SetLabel rather than SetTitle.
659 // It should cause SetTitle() for top level windows.
660 wxWindow
*win
= GetFrame();
663 wxDocument
*doc
= GetDocument();
667 doc
->GetPrintableName(name
);
671 void wxView::SetDocument(wxDocument
*doc
)
673 m_viewDocument
= doc
;
678 bool wxView::Close(bool deleteWindow
)
680 if (OnClose(deleteWindow
))
686 void wxView::Activate(bool activate
)
688 if (GetDocument() && GetDocumentManager())
690 OnActivateView(activate
, this, GetDocumentManager()->GetCurrentView());
691 GetDocumentManager()->ActivateView(this, activate
);
695 bool wxView::OnClose(bool WXUNUSED(deleteWindow
))
697 return GetDocument() ? GetDocument()->Close() : true;
700 #if wxUSE_PRINTING_ARCHITECTURE
701 wxPrintout
*wxView::OnCreatePrintout()
703 return new wxDocPrintout(this);
705 #endif // wxUSE_PRINTING_ARCHITECTURE
707 // ----------------------------------------------------------------------------
709 // ----------------------------------------------------------------------------
711 wxDocTemplate::wxDocTemplate(wxDocManager
*manager
,
712 const wxString
& descr
,
713 const wxString
& filter
,
716 const wxString
& docTypeName
,
717 const wxString
& viewTypeName
,
718 wxClassInfo
*docClassInfo
,
719 wxClassInfo
*viewClassInfo
,
722 m_documentManager
= manager
;
723 m_description
= descr
;
726 m_fileFilter
= filter
;
728 m_docTypeName
= docTypeName
;
729 m_viewTypeName
= viewTypeName
;
730 m_documentManager
->AssociateTemplate(this);
732 m_docClassInfo
= docClassInfo
;
733 m_viewClassInfo
= viewClassInfo
;
736 wxDocTemplate::~wxDocTemplate()
738 m_documentManager
->DisassociateTemplate(this);
741 // Tries to dynamically construct an object of the right class.
742 wxDocument
*wxDocTemplate::CreateDocument(const wxString
& path
, long flags
)
744 wxDocument
*doc
= DoCreateDocument();
746 return (wxDocument
*) NULL
;
748 if (InitDocument(doc
, path
, flags
))
754 return (wxDocument
*) NULL
;
758 bool wxDocTemplate::InitDocument(wxDocument
* doc
, const wxString
& path
, long flags
)
760 doc
->SetFilename(path
);
761 doc
->SetDocumentTemplate(this);
762 GetDocumentManager()->AddDocument(doc
);
763 doc
->SetCommandProcessor(doc
->OnCreateCommandProcessor());
765 if (doc
->OnCreate(path
, flags
))
769 if (GetDocumentManager()->GetDocuments().Member(doc
))
770 doc
->DeleteAllViews();
775 wxView
*wxDocTemplate::CreateView(wxDocument
*doc
, long flags
)
777 wxView
*view
= DoCreateView();
779 return (wxView
*) NULL
;
781 view
->SetDocument(doc
);
782 if (view
->OnCreate(doc
, flags
))
789 return (wxView
*) NULL
;
793 // The default (very primitive) format detection: check is the extension is
794 // that of the template
795 bool wxDocTemplate::FileMatchesTemplate(const wxString
& path
)
797 wxStringTokenizer
parser (GetFileFilter(), wxT(";"));
798 wxString anything
= wxT ("*");
799 while (parser
.HasMoreTokens())
801 wxString filter
= parser
.GetNextToken();
802 wxString filterExt
= FindExtension (filter
);
803 if ( filter
.IsSameAs (anything
) ||
804 filterExt
.IsSameAs (anything
) ||
805 filterExt
.IsSameAs (FindExtension (path
)) )
808 return GetDefaultExtension().IsSameAs(FindExtension(path
));
811 wxDocument
*wxDocTemplate::DoCreateDocument()
814 return (wxDocument
*) NULL
;
816 return (wxDocument
*)m_docClassInfo
->CreateObject();
819 wxView
*wxDocTemplate::DoCreateView()
821 if (!m_viewClassInfo
)
822 return (wxView
*) NULL
;
824 return (wxView
*)m_viewClassInfo
->CreateObject();
827 // ----------------------------------------------------------------------------
829 // ----------------------------------------------------------------------------
831 BEGIN_EVENT_TABLE(wxDocManager
, wxEvtHandler
)
832 EVT_MENU(wxID_OPEN
, wxDocManager::OnFileOpen
)
833 EVT_MENU(wxID_CLOSE
, wxDocManager::OnFileClose
)
834 EVT_MENU(wxID_CLOSE_ALL
, wxDocManager::OnFileCloseAll
)
835 EVT_MENU(wxID_REVERT
, wxDocManager::OnFileRevert
)
836 EVT_MENU(wxID_NEW
, wxDocManager::OnFileNew
)
837 EVT_MENU(wxID_SAVE
, wxDocManager::OnFileSave
)
838 EVT_MENU(wxID_SAVEAS
, wxDocManager::OnFileSaveAs
)
839 EVT_MENU(wxID_UNDO
, wxDocManager::OnUndo
)
840 EVT_MENU(wxID_REDO
, wxDocManager::OnRedo
)
842 EVT_UPDATE_UI(wxID_OPEN
, wxDocManager::OnUpdateFileOpen
)
843 EVT_UPDATE_UI(wxID_CLOSE
, wxDocManager::OnUpdateFileClose
)
844 EVT_UPDATE_UI(wxID_CLOSE_ALL
, wxDocManager::OnUpdateFileClose
)
845 EVT_UPDATE_UI(wxID_REVERT
, wxDocManager::OnUpdateFileRevert
)
846 EVT_UPDATE_UI(wxID_NEW
, wxDocManager::OnUpdateFileNew
)
847 EVT_UPDATE_UI(wxID_SAVE
, wxDocManager::OnUpdateFileSave
)
848 EVT_UPDATE_UI(wxID_SAVEAS
, wxDocManager::OnUpdateFileSaveAs
)
849 EVT_UPDATE_UI(wxID_UNDO
, wxDocManager::OnUpdateUndo
)
850 EVT_UPDATE_UI(wxID_REDO
, wxDocManager::OnUpdateRedo
)
852 #if wxUSE_PRINTING_ARCHITECTURE
853 EVT_MENU(wxID_PRINT
, wxDocManager::OnPrint
)
854 EVT_MENU(wxID_PREVIEW
, wxDocManager::OnPreview
)
856 EVT_UPDATE_UI(wxID_PRINT
, wxDocManager::OnUpdatePrint
)
857 EVT_UPDATE_UI(wxID_PREVIEW
, wxDocManager::OnUpdatePreview
)
861 wxDocManager
* wxDocManager::sm_docManager
= (wxDocManager
*) NULL
;
863 wxDocManager::wxDocManager(long flags
, bool initialize
)
865 m_defaultDocumentNameCounter
= 1;
867 m_currentView
= (wxView
*) NULL
;
868 m_maxDocsOpen
= 10000;
869 m_fileHistory
= (wxFileHistory
*) NULL
;
872 sm_docManager
= this;
875 wxDocManager::~wxDocManager()
879 delete m_fileHistory
;
880 sm_docManager
= (wxDocManager
*) NULL
;
883 // closes the specified document
884 bool wxDocManager::CloseDocument(wxDocument
* doc
, bool force
)
886 if (doc
->Close() || force
)
888 // Implicitly deletes the document when
889 // the last view is deleted
890 doc
->DeleteAllViews();
892 // Check we're really deleted
893 if (m_docs
.Member(doc
))
901 bool wxDocManager::CloseDocuments(bool force
)
903 wxList::compatibility_iterator node
= m_docs
.GetFirst();
906 wxDocument
*doc
= (wxDocument
*)node
->GetData();
907 wxList::compatibility_iterator next
= node
->GetNext();
909 if (!CloseDocument(doc
, force
))
912 // This assumes that documents are not connected in
913 // any way, i.e. deleting one document does NOT
920 bool wxDocManager::Clear(bool force
)
922 if (!CloseDocuments(force
))
925 m_currentView
= NULL
;
927 wxList::compatibility_iterator node
= m_templates
.GetFirst();
930 wxDocTemplate
*templ
= (wxDocTemplate
*) node
->GetData();
931 wxList::compatibility_iterator next
= node
->GetNext();
938 bool wxDocManager::Initialize()
940 m_fileHistory
= OnCreateFileHistory();
944 wxFileHistory
*wxDocManager::OnCreateFileHistory()
946 return new wxFileHistory
;
949 void wxDocManager::OnFileClose(wxCommandEvent
& WXUNUSED(event
))
951 wxDocument
*doc
= GetCurrentDocument();
956 doc
->DeleteAllViews();
957 if (m_docs
.Member(doc
))
962 void wxDocManager::OnFileCloseAll(wxCommandEvent
& WXUNUSED(event
))
964 CloseDocuments(false);
967 void wxDocManager::OnFileNew(wxCommandEvent
& WXUNUSED(event
))
969 CreateDocument( wxEmptyString
, wxDOC_NEW
);
972 void wxDocManager::OnFileOpen(wxCommandEvent
& WXUNUSED(event
))
974 if ( !CreateDocument( wxEmptyString
, 0) )
980 void wxDocManager::OnFileRevert(wxCommandEvent
& WXUNUSED(event
))
982 wxDocument
*doc
= GetCurrentDocument();
988 void wxDocManager::OnFileSave(wxCommandEvent
& WXUNUSED(event
))
990 wxDocument
*doc
= GetCurrentDocument();
996 void wxDocManager::OnFileSaveAs(wxCommandEvent
& WXUNUSED(event
))
998 wxDocument
*doc
= GetCurrentDocument();
1004 void wxDocManager::OnPrint(wxCommandEvent
& WXUNUSED(event
))
1006 #if wxUSE_PRINTING_ARCHITECTURE
1007 wxView
*view
= GetCurrentView();
1011 wxPrintout
*printout
= view
->OnCreatePrintout();
1015 printer
.Print(view
->GetFrame(), printout
, true);
1019 #endif // wxUSE_PRINTING_ARCHITECTURE
1022 void wxDocManager::OnPreview(wxCommandEvent
& WXUNUSED(event
))
1024 #if wxUSE_PRINTING_ARCHITECTURE
1025 wxView
*view
= GetCurrentView();
1029 wxPrintout
*printout
= view
->OnCreatePrintout();
1032 // Pass two printout objects: for preview, and possible printing.
1033 wxPrintPreviewBase
*preview
= new wxPrintPreview(printout
, view
->OnCreatePrintout());
1034 if ( !preview
->Ok() )
1037 wxMessageBox( _("Sorry, print preview needs a printer to be installed.") );
1041 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, (wxFrame
*)wxTheApp
->GetTopWindow(), _("Print Preview"),
1042 wxPoint(100, 100), wxSize(600, 650));
1043 frame
->Centre(wxBOTH
);
1044 frame
->Initialize();
1047 #endif // wxUSE_PRINTING_ARCHITECTURE
1050 void wxDocManager::OnUndo(wxCommandEvent
& event
)
1052 wxDocument
*doc
= GetCurrentDocument();
1055 if (doc
->GetCommandProcessor())
1056 doc
->GetCommandProcessor()->Undo();
1061 void wxDocManager::OnRedo(wxCommandEvent
& event
)
1063 wxDocument
*doc
= GetCurrentDocument();
1066 if (doc
->GetCommandProcessor())
1067 doc
->GetCommandProcessor()->Redo();
1072 // Handlers for UI update commands
1074 void wxDocManager::OnUpdateFileOpen(wxUpdateUIEvent
& event
)
1076 event
.Enable( true );
1079 void wxDocManager::OnUpdateFileClose(wxUpdateUIEvent
& event
)
1081 wxDocument
*doc
= GetCurrentDocument();
1082 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1085 void wxDocManager::OnUpdateFileRevert(wxUpdateUIEvent
& event
)
1087 wxDocument
*doc
= GetCurrentDocument();
1088 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1091 void wxDocManager::OnUpdateFileNew(wxUpdateUIEvent
& event
)
1093 event
.Enable( true );
1096 void wxDocManager::OnUpdateFileSave(wxUpdateUIEvent
& event
)
1098 wxDocument
*doc
= GetCurrentDocument();
1099 event
.Enable( doc
&& doc
->IsModified() );
1102 void wxDocManager::OnUpdateFileSaveAs(wxUpdateUIEvent
& event
)
1104 wxDocument
*doc
= GetCurrentDocument();
1105 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1108 void wxDocManager::OnUpdateUndo(wxUpdateUIEvent
& event
)
1110 wxDocument
*doc
= GetCurrentDocument();
1112 event
.Enable(false);
1113 else if (!doc
->GetCommandProcessor())
1117 event
.Enable( doc
->GetCommandProcessor()->CanUndo() );
1118 doc
->GetCommandProcessor()->SetMenuStrings();
1122 void wxDocManager::OnUpdateRedo(wxUpdateUIEvent
& event
)
1124 wxDocument
*doc
= GetCurrentDocument();
1126 event
.Enable(false);
1127 else if (!doc
->GetCommandProcessor())
1131 event
.Enable( doc
->GetCommandProcessor()->CanRedo() );
1132 doc
->GetCommandProcessor()->SetMenuStrings();
1136 void wxDocManager::OnUpdatePrint(wxUpdateUIEvent
& event
)
1138 wxDocument
*doc
= GetCurrentDocument();
1139 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1142 void wxDocManager::OnUpdatePreview(wxUpdateUIEvent
& event
)
1144 wxDocument
*doc
= GetCurrentDocument();
1145 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1148 wxView
*wxDocManager::GetCurrentView() const
1151 return m_currentView
;
1152 if (m_docs
.GetCount() == 1)
1154 wxDocument
* doc
= (wxDocument
*) m_docs
.GetFirst()->GetData();
1155 return doc
->GetFirstView();
1157 return (wxView
*) NULL
;
1160 // Extend event processing to search the view's event table
1161 bool wxDocManager::ProcessEvent(wxEvent
& event
)
1163 wxView
* view
= GetCurrentView();
1166 if (view
->ProcessEvent(event
))
1169 return wxEvtHandler::ProcessEvent(event
);
1172 wxDocument
*wxDocManager::CreateDocument(const wxString
& path
, long flags
)
1174 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.GetCount()];
1177 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1179 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Item(i
)->GetData());
1180 if (temp
->IsVisible())
1182 templates
[n
] = temp
;
1189 return (wxDocument
*) NULL
;
1192 wxDocument
* docToClose
= NULL
;
1194 // If we've reached the max number of docs, close the
1196 if ( (int)GetDocuments().GetCount() >= m_maxDocsOpen
)
1198 wxDocument
*doc
= (wxDocument
*)GetDocuments().GetFirst()->GetData();
1202 // New document: user chooses a template, unless there's only one.
1203 if (flags
& wxDOC_NEW
)
1209 if (!CloseDocument(docToClose
, false))
1216 wxDocTemplate
*temp
= templates
[0];
1218 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
1222 newDoc
->SetDocumentName(temp
->GetDocumentName());
1223 newDoc
->SetDocumentTemplate(temp
);
1224 if (!newDoc
->OnNewDocument() )
1226 // Document is implicitly deleted by DeleteAllViews
1227 newDoc
->DeleteAllViews();
1234 wxDocTemplate
*temp
= SelectDocumentType(templates
, n
);
1240 if (!CloseDocument(docToClose
, false))
1246 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
1250 newDoc
->SetDocumentName(temp
->GetDocumentName());
1251 newDoc
->SetDocumentTemplate(temp
);
1252 if (!newDoc
->OnNewDocument() )
1254 // Document is implicitly deleted by DeleteAllViews
1255 newDoc
->DeleteAllViews();
1262 return (wxDocument
*) NULL
;
1265 // Existing document
1266 wxDocTemplate
*temp
;
1268 wxString path2
= path
;
1270 if (flags
& wxDOC_SILENT
)
1272 temp
= FindTemplateForPath(path2
);
1275 // Since we do not add files with non-default extensions to the FileHistory this
1276 // can only happen if the application changes the allowed templates in runtime.
1277 (void)wxMessageBox(_("Sorry, the format for this file is unknown."),
1279 wxOK
| wxICON_EXCLAMATION
, wxFindSuitableParent());
1283 temp
= SelectDocumentPath(templates
, n
, path2
, flags
);
1291 if (!CloseDocument(docToClose
, false))
1297 //see if this file is already open
1298 for (size_t i
= 0; i
< GetDocuments().GetCount(); ++i
)
1300 wxDocument
* currentDoc
= (wxDocument
*)(GetDocuments().Item(i
)->GetData());
1302 //file paths are case-insensitive on Windows
1303 if (path2
.CmpNoCase(currentDoc
->GetFilename()) == 0)
1305 if (path2
.Cmp(currentDoc
->GetFilename()) == 0)
1308 //file already open. Just activate it and return
1309 if (currentDoc
->GetFirstView())
1311 ActivateView(currentDoc
->GetFirstView(), true);
1312 if (currentDoc
->GetDocumentWindow())
1313 currentDoc
->GetDocumentWindow()->SetFocus();
1319 wxDocument
*newDoc
= temp
->CreateDocument(path2
, flags
);
1322 newDoc
->SetDocumentName(temp
->GetDocumentName());
1323 newDoc
->SetDocumentTemplate(temp
);
1324 if (!newDoc
->OnOpenDocument(path2
))
1326 newDoc
->DeleteAllViews();
1327 // delete newDoc; // Implicitly deleted by DeleteAllViews
1328 return (wxDocument
*) NULL
;
1330 // A file that doesn't use the default extension of its document
1331 // template cannot be opened via the FileHistory, so we do not
1333 if (temp
->FileMatchesTemplate(path2
))
1334 AddFileToHistory(path2
);
1339 return (wxDocument
*) NULL
;
1342 wxView
*wxDocManager::CreateView(wxDocument
*doc
, long flags
)
1344 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.GetCount()];
1347 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1349 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Item(i
)->GetData());
1350 if (temp
->IsVisible())
1352 if (temp
->GetDocumentName() == doc
->GetDocumentName())
1354 templates
[n
] = temp
;
1362 return (wxView
*) NULL
;
1366 wxDocTemplate
*temp
= templates
[0];
1368 wxView
*view
= temp
->CreateView(doc
, flags
);
1370 view
->SetViewName(temp
->GetViewName());
1374 wxDocTemplate
*temp
= SelectViewType(templates
, n
);
1378 wxView
*view
= temp
->CreateView(doc
, flags
);
1380 view
->SetViewName(temp
->GetViewName());
1384 return (wxView
*) NULL
;
1387 // Not yet implemented
1388 void wxDocManager::DeleteTemplate(wxDocTemplate
*WXUNUSED(temp
), long WXUNUSED(flags
))
1392 // Not yet implemented
1393 bool wxDocManager::FlushDoc(wxDocument
*WXUNUSED(doc
))
1398 wxDocument
*wxDocManager::GetCurrentDocument() const
1400 wxView
*view
= GetCurrentView();
1402 return view
->GetDocument();
1404 return (wxDocument
*) NULL
;
1407 // Make a default document name
1408 bool wxDocManager::MakeDefaultName(wxString
& name
)
1410 name
.Printf(_("unnamed%d"), m_defaultDocumentNameCounter
);
1411 m_defaultDocumentNameCounter
++;
1416 // Make a frame title (override this to do something different)
1417 // If docName is empty, a document is not currently active.
1418 wxString
wxDocManager::MakeFrameTitle(wxDocument
* doc
)
1420 wxString appName
= wxTheApp
->GetAppName();
1427 doc
->GetPrintableName(docName
);
1428 title
= docName
+ wxString(_(" - ")) + appName
;
1434 // Not yet implemented
1435 wxDocTemplate
*wxDocManager::MatchTemplate(const wxString
& WXUNUSED(path
))
1437 return (wxDocTemplate
*) NULL
;
1440 // File history management
1441 void wxDocManager::AddFileToHistory(const wxString
& file
)
1444 m_fileHistory
->AddFileToHistory(file
);
1447 void wxDocManager::RemoveFileFromHistory(size_t i
)
1450 m_fileHistory
->RemoveFileFromHistory(i
);
1453 wxString
wxDocManager::GetHistoryFile(size_t i
) const
1458 histFile
= m_fileHistory
->GetHistoryFile(i
);
1463 void wxDocManager::FileHistoryUseMenu(wxMenu
*menu
)
1466 m_fileHistory
->UseMenu(menu
);
1469 void wxDocManager::FileHistoryRemoveMenu(wxMenu
*menu
)
1472 m_fileHistory
->RemoveMenu(menu
);
1476 void wxDocManager::FileHistoryLoad(wxConfigBase
& config
)
1479 m_fileHistory
->Load(config
);
1482 void wxDocManager::FileHistorySave(wxConfigBase
& config
)
1485 m_fileHistory
->Save(config
);
1489 void wxDocManager::FileHistoryAddFilesToMenu(wxMenu
* menu
)
1492 m_fileHistory
->AddFilesToMenu(menu
);
1495 void wxDocManager::FileHistoryAddFilesToMenu()
1498 m_fileHistory
->AddFilesToMenu();
1501 size_t wxDocManager::GetHistoryFilesCount() const
1503 return m_fileHistory
? m_fileHistory
->GetCount() : 0;
1507 // Find out the document template via matching in the document file format
1508 // against that of the template
1509 wxDocTemplate
*wxDocManager::FindTemplateForPath(const wxString
& path
)
1511 wxDocTemplate
*theTemplate
= (wxDocTemplate
*) NULL
;
1513 // Find the template which this extension corresponds to
1514 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1516 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Item(i
)->GetData();
1517 if ( temp
->FileMatchesTemplate(path
) )
1526 // Try to get a more suitable parent frame than the top window,
1527 // for selection dialogs. Otherwise you may get an unexpected
1528 // window being activated when a dialog is shown.
1529 static wxWindow
* wxFindSuitableParent()
1531 wxWindow
* parent
= wxTheApp
->GetTopWindow();
1533 wxWindow
* focusWindow
= wxWindow::FindFocus();
1536 while (focusWindow
&&
1537 !focusWindow
->IsKindOf(CLASSINFO(wxDialog
)) &&
1538 !focusWindow
->IsKindOf(CLASSINFO(wxFrame
)))
1540 focusWindow
= focusWindow
->GetParent();
1543 parent
= focusWindow
;
1548 // Prompts user to open a file, using file specs in templates.
1549 // Must extend the file selector dialog or implement own; OR
1550 // match the extension to the template extension.
1552 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**templates
,
1553 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
1556 int WXUNUSED(noTemplates
),
1559 long WXUNUSED(flags
),
1560 bool WXUNUSED(save
))
1562 // We can only have multiple filters in Windows and GTK
1563 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
1567 for (i
= 0; i
< noTemplates
; i
++)
1569 if (templates
[i
]->IsVisible())
1571 // add a '|' to separate this filter from the previous one
1572 if ( !descrBuf
.empty() )
1573 descrBuf
<< wxT('|');
1575 descrBuf
<< templates
[i
]->GetDescription()
1576 << wxT(" (") << templates
[i
]->GetFileFilter() << wxT(") |")
1577 << templates
[i
]->GetFileFilter();
1581 wxString descrBuf
= wxT("*.*");
1584 int FilterIndex
= -1;
1586 wxWindow
* parent
= wxFindSuitableParent();
1588 wxString pathTmp
= wxFileSelectorEx(_("Select a file"),
1596 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)NULL
;
1597 if (!pathTmp
.empty())
1599 if (!wxFileExists(pathTmp
))
1602 if (!wxTheApp
->GetAppName().empty())
1603 msgTitle
= wxTheApp
->GetAppName();
1605 msgTitle
= wxString(_("File error"));
1607 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
1610 path
= wxEmptyString
;
1611 return (wxDocTemplate
*) NULL
;
1613 m_lastDirectory
= wxPathOnly(pathTmp
);
1617 // first choose the template using the extension, if this fails (i.e.
1618 // wxFileSelectorEx() didn't fill it), then use the path
1619 if ( FilterIndex
!= -1 )
1620 theTemplate
= templates
[FilterIndex
];
1622 theTemplate
= FindTemplateForPath(path
);
1625 // Since we do not add files with non-default extensions to the FileHistory this
1626 // can only happen if the application changes the allowed templates in runtime.
1627 (void)wxMessageBox(_("Sorry, the format for this file is unknown."),
1629 wxOK
| wxICON_EXCLAMATION
, wxFindSuitableParent());
1634 path
= wxEmptyString
;
1640 wxDocTemplate
*wxDocManager::SelectDocumentType(wxDocTemplate
**templates
,
1641 int noTemplates
, bool sort
)
1643 wxArrayString strings
;
1644 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1648 for (i
= 0; i
< noTemplates
; i
++)
1650 if (templates
[i
]->IsVisible())
1654 for (j
= 0; j
< n
; j
++)
1656 //filter out NOT unique documents + view combinations
1657 if ( templates
[i
]->m_docTypeName
== data
[j
]->m_docTypeName
&&
1658 templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
1665 strings
.Add(templates
[i
]->m_description
);
1667 data
[n
] = templates
[i
];
1675 strings
.Sort(); // ascending sort
1676 // Yes, this will be slow, but template lists
1677 // are typically short.
1679 n
= strings
.Count();
1680 for (i
= 0; i
< n
; i
++)
1682 for (j
= 0; j
< noTemplates
; j
++)
1684 if (strings
[i
] == templates
[j
]->m_description
)
1685 data
[i
] = templates
[j
];
1690 wxDocTemplate
*theTemplate
;
1695 // no visible templates, hence nothing to choose from
1700 // don't propose the user to choose if he heas no choice
1701 theTemplate
= data
[0];
1705 // propose the user to choose one of several
1706 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1708 _("Select a document template"),
1712 wxFindSuitableParent()
1721 wxDocTemplate
*wxDocManager::SelectViewType(wxDocTemplate
**templates
,
1722 int noTemplates
, bool sort
)
1724 wxArrayString strings
;
1725 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1729 for (i
= 0; i
< noTemplates
; i
++)
1731 wxDocTemplate
*templ
= templates
[i
];
1732 if ( templ
->IsVisible() && !templ
->GetViewName().empty() )
1736 for (j
= 0; j
< n
; j
++)
1738 //filter out NOT unique views
1739 if ( templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
)
1745 strings
.Add(templ
->m_viewTypeName
);
1754 strings
.Sort(); // ascending sort
1755 // Yes, this will be slow, but template lists
1756 // are typically short.
1758 n
= strings
.Count();
1759 for (i
= 0; i
< n
; i
++)
1761 for (j
= 0; j
< noTemplates
; j
++)
1763 if (strings
[i
] == templates
[j
]->m_viewTypeName
)
1764 data
[i
] = templates
[j
];
1769 wxDocTemplate
*theTemplate
;
1771 // the same logic as above
1775 theTemplate
= (wxDocTemplate
*)NULL
;
1779 theTemplate
= data
[0];
1783 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1785 _("Select a document view"),
1789 wxFindSuitableParent()
1798 void wxDocManager::AssociateTemplate(wxDocTemplate
*temp
)
1800 if (!m_templates
.Member(temp
))
1801 m_templates
.Append(temp
);
1804 void wxDocManager::DisassociateTemplate(wxDocTemplate
*temp
)
1806 m_templates
.DeleteObject(temp
);
1809 // Add and remove a document from the manager's list
1810 void wxDocManager::AddDocument(wxDocument
*doc
)
1812 if (!m_docs
.Member(doc
))
1816 void wxDocManager::RemoveDocument(wxDocument
*doc
)
1818 m_docs
.DeleteObject(doc
);
1821 // Views or windows should inform the document manager
1822 // when a view is going in or out of focus
1823 void wxDocManager::ActivateView(wxView
*view
, bool activate
)
1827 m_currentView
= view
;
1831 if ( m_currentView
== view
)
1833 // don't keep stale pointer
1834 m_currentView
= (wxView
*) NULL
;
1839 // ----------------------------------------------------------------------------
1840 // Default document child frame
1841 // ----------------------------------------------------------------------------
1843 BEGIN_EVENT_TABLE(wxDocChildFrame
, wxFrame
)
1844 EVT_ACTIVATE(wxDocChildFrame::OnActivate
)
1845 EVT_CLOSE(wxDocChildFrame::OnCloseWindow
)
1848 wxDocChildFrame::wxDocChildFrame(wxDocument
*doc
,
1852 const wxString
& title
,
1856 const wxString
& name
)
1857 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1859 m_childDocument
= doc
;
1862 view
->SetFrame(this);
1865 // Extend event processing to search the view's event table
1866 bool wxDocChildFrame::ProcessEvent(wxEvent
& event
)
1869 m_childView
->Activate(true);
1871 if ( !m_childView
|| ! m_childView
->ProcessEvent(event
) )
1873 // Only hand up to the parent if it's a menu command
1874 if (!event
.IsKindOf(CLASSINFO(wxCommandEvent
)) || !GetParent() || !GetParent()->ProcessEvent(event
))
1875 return wxEvtHandler::ProcessEvent(event
);
1883 void wxDocChildFrame::OnActivate(wxActivateEvent
& event
)
1885 wxFrame::OnActivate(event
);
1888 m_childView
->Activate(event
.GetActive());
1891 void wxDocChildFrame::OnCloseWindow(wxCloseEvent
& event
)
1895 bool ans
= event
.CanVeto()
1896 ? m_childView
->Close(false) // false means don't delete associated window
1897 : true; // Must delete.
1901 m_childView
->Activate(false);
1903 m_childView
= (wxView
*) NULL
;
1904 m_childDocument
= (wxDocument
*) NULL
;
1915 // ----------------------------------------------------------------------------
1916 // Default parent frame
1917 // ----------------------------------------------------------------------------
1919 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1920 EVT_MENU(wxID_EXIT
, wxDocParentFrame::OnExit
)
1921 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE9
, wxDocParentFrame::OnMRUFile
)
1922 EVT_CLOSE(wxDocParentFrame::OnCloseWindow
)
1925 wxDocParentFrame::wxDocParentFrame()
1927 m_docManager
= NULL
;
1930 wxDocParentFrame::wxDocParentFrame(wxDocManager
*manager
,
1933 const wxString
& title
,
1937 const wxString
& name
)
1938 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1940 m_docManager
= manager
;
1943 bool wxDocParentFrame::Create(wxDocManager
*manager
,
1946 const wxString
& title
,
1950 const wxString
& name
)
1952 m_docManager
= manager
;
1953 return base_type::Create(frame
, id
, title
, pos
, size
, style
, name
);
1956 void wxDocParentFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
1961 void wxDocParentFrame::OnMRUFile(wxCommandEvent
& event
)
1963 int n
= event
.GetId() - wxID_FILE1
; // the index in MRU list
1964 wxString
filename(m_docManager
->GetHistoryFile(n
));
1965 if ( !filename
.empty() )
1967 // verify that the file exists before doing anything else
1968 if ( wxFile::Exists(filename
) )
1971 if (!m_docManager
->CreateDocument(filename
, wxDOC_SILENT
))
1973 // remove the file from the MRU list. The user should already be notified.
1974 m_docManager
->RemoveFileFromHistory(n
);
1976 wxLogError(_("The file '%s' couldn't be opened.\nIt has been removed from the most recently used files list."),
1982 // remove the bogus filename from the MRU list and notify the user
1984 m_docManager
->RemoveFileFromHistory(n
);
1986 wxLogError(_("The file '%s' doesn't exist and couldn't be opened.\nIt has been removed from the most recently used files list."),
1992 // Extend event processing to search the view's event table
1993 bool wxDocParentFrame::ProcessEvent(wxEvent
& event
)
1995 // Try the document manager, then do default processing
1996 if (!m_docManager
|| !m_docManager
->ProcessEvent(event
))
1997 return wxEvtHandler::ProcessEvent(event
);
2002 // Define the behaviour for the frame closing
2003 // - must delete all frames except for the main one.
2004 void wxDocParentFrame::OnCloseWindow(wxCloseEvent
& event
)
2006 if (m_docManager
->Clear(!event
.CanVeto()))
2014 #if wxUSE_PRINTING_ARCHITECTURE
2016 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
)
2019 m_printoutView
= view
;
2022 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
2026 // Get the logical pixels per inch of screen and printer
2027 int ppiScreenX
, ppiScreenY
;
2028 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
2029 wxUnusedVar(ppiScreenY
);
2030 int ppiPrinterX
, ppiPrinterY
;
2031 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
2032 wxUnusedVar(ppiPrinterY
);
2034 // This scales the DC so that the printout roughly represents the
2035 // the screen scaling. The text point size _should_ be the right size
2036 // but in fact is too small for some reason. This is a detail that will
2037 // need to be addressed at some point but can be fudged for the
2039 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
2041 // Now we have to check in case our real page size is reduced
2042 // (e.g. because we're drawing to a print preview memory DC)
2043 int pageWidth
, pageHeight
;
2045 dc
->GetSize(&w
, &h
);
2046 GetPageSizePixels(&pageWidth
, &pageHeight
);
2047 wxUnusedVar(pageHeight
);
2049 // If printer pageWidth == current DC width, then this doesn't
2050 // change. But w might be the preview bitmap width, so scale down.
2051 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
2052 dc
->SetUserScale(overallScale
, overallScale
);
2056 m_printoutView
->OnDraw(dc
);
2061 bool wxDocPrintout::HasPage(int pageNum
)
2063 return (pageNum
== 1);
2066 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
2068 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
2074 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
2082 #endif // wxUSE_PRINTING_ARCHITECTURE
2084 // ----------------------------------------------------------------------------
2085 // File history processor
2086 // ----------------------------------------------------------------------------
2088 wxFileHistory::wxFileHistory(size_t maxFiles
, wxWindowID idBase
)
2090 m_fileMaxFiles
= maxFiles
;
2094 wxFileHistory::~wxFileHistory()
2098 void wxFileHistory::AddFileToHistory(const wxString
& file
)
2100 wxFileName
fn(file
);
2103 // Check we don't already have this file
2104 for (i
= 0; i
< m_fileHistory
.GetCount(); i
++)
2106 // we need to do a comparison using wxFileNames because it knows
2107 // how to exactly compare files on the different platforms
2108 // (e.g. handle case [in]sensitive filesystems)
2109 if ( fn
== wxFileName(m_fileHistory
[i
]) )
2111 // we do have it, move it to the top of the history
2112 RemoveFileFromHistory (i
);
2113 AddFileToHistory (file
);
2118 // if we already have a full history, delete the one at the end
2119 if ( m_fileMaxFiles
== m_fileHistory
.GetCount() )
2121 RemoveFileFromHistory (m_fileHistory
.GetCount() - 1);
2122 AddFileToHistory (file
);
2126 // Add to the project file history:
2127 // Move existing files (if any) down so we can insert file at beginning.
2128 if (m_fileHistory
.GetCount() < m_fileMaxFiles
)
2130 wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2133 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2134 if ( m_fileHistory
.IsEmpty() && menu
->GetMenuItemCount() )
2136 menu
->AppendSeparator();
2138 menu
->Append(m_idBase
+m_fileHistory
.GetCount(), _("[EMPTY]"));
2139 node
= node
->GetNext();
2143 m_fileHistory
.Insert(file
, 0);
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_fileHistory
.GetCount(); i
++)
2150 // if in same directory just show the filename; otherwise the full
2152 wxString pathInMenu
, path
, filename
, ext
;
2153 wxSplitPath( m_fileHistory
[i
], &path
, &filename
, &ext
);
2154 if ( path
== pathCurrent
)
2156 pathInMenu
= filename
;
2158 pathInMenu
= pathInMenu
+ wxFILE_SEP_EXT
+ ext
;
2162 // absolute path; could also set relative path
2163 pathInMenu
= m_fileHistory
[i
];
2166 // we need to quote '&' characters which are used for mnemonics
2167 pathInMenu
.Replace(_T("&"), _T("&&"));
2170 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();
2182 void wxFileHistory::RemoveFileFromHistory(size_t i
)
2184 wxCHECK_RET( i
< m_fileHistory
.GetCount(),
2185 wxT("invalid index in wxFileHistory::RemoveFileFromHistory") );
2187 // delete the element from the array
2188 m_fileHistory
.RemoveAt(i
);
2190 wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2193 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2195 // shuffle filenames up
2197 for ( size_t j
= i
; j
< m_fileHistory
.GetCount(); j
++ )
2199 buf
.Printf(s_MRUEntryFormat
, j
+ 1, m_fileHistory
[j
].c_str());
2200 menu
->SetLabel(m_idBase
+ j
, buf
);
2203 node
= node
->GetNext();
2205 // delete the last menu item which is unused now
2206 wxWindowID lastItemId
= m_idBase
+ wx_truncate_cast(wxWindowID
, m_fileHistory
.GetCount());
2207 if (menu
->FindItem(lastItemId
))
2209 menu
->Delete(lastItemId
);
2212 // delete the last separator too if no more files are left
2213 if ( m_fileHistory
.GetCount() == 0 )
2215 wxMenuItemList::compatibility_iterator nodeLast
= menu
->GetMenuItems().GetLast();
2218 wxMenuItem
*menuItem
= nodeLast
->GetData();
2219 if ( menuItem
->IsSeparator() )
2221 menu
->Delete(menuItem
);
2223 //else: should we search backwards for the last separator?
2225 //else: menu is empty somehow
2230 void wxFileHistory::UseMenu(wxMenu
*menu
)
2232 if (!m_fileMenus
.Member(menu
))
2233 m_fileMenus
.Append(menu
);
2236 void wxFileHistory::RemoveMenu(wxMenu
*menu
)
2238 m_fileMenus
.DeleteObject(menu
);
2242 void wxFileHistory::Load(wxConfigBase
& config
)
2244 m_fileHistory
.Clear();
2247 buf
.Printf(wxT("file%d"), 1);
2249 wxString historyFile
;
2250 while ((m_fileHistory
.GetCount() < m_fileMaxFiles
) &&
2251 config
.Read(buf
, &historyFile
) && !historyFile
.empty())
2253 m_fileHistory
.Add(historyFile
);
2255 buf
.Printf(wxT("file%d"), (int)m_fileHistory
.GetCount()+1);
2256 historyFile
= wxEmptyString
;
2262 void wxFileHistory::Save(wxConfigBase
& config
)
2265 for (i
= 0; i
< m_fileMaxFiles
; i
++)
2268 buf
.Printf(wxT("file%d"), (int)i
+1);
2269 if (i
< m_fileHistory
.GetCount())
2270 config
.Write(buf
, wxString(m_fileHistory
[i
]));
2272 config
.Write(buf
, wxEmptyString
);
2275 #endif // wxUSE_CONFIG
2277 void wxFileHistory::AddFilesToMenu()
2279 if (m_fileHistory
.GetCount() > 0)
2281 wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2284 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2285 if (menu
->GetMenuItemCount())
2287 menu
->AppendSeparator();
2291 for (i
= 0; i
< m_fileHistory
.GetCount(); i
++)
2294 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
].c_str());
2295 menu
->Append(m_idBase
+i
, buf
);
2297 node
= node
->GetNext();
2302 void wxFileHistory::AddFilesToMenu(wxMenu
* menu
)
2304 if (m_fileHistory
.GetCount() > 0)
2306 if (menu
->GetMenuItemCount())
2308 menu
->AppendSeparator();
2312 for (i
= 0; i
< m_fileHistory
.GetCount(); i
++)
2315 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
].c_str());
2316 menu
->Append(m_idBase
+i
, buf
);
2321 // ----------------------------------------------------------------------------
2322 // Permits compatibility with existing file formats and functions that
2323 // manipulate files directly
2324 // ----------------------------------------------------------------------------
2326 #if wxUSE_STD_IOSTREAM
2328 bool wxTransferFileToStream(const wxString
& filename
, wxSTD ostream
& stream
)
2330 wxFFile
file(filename
, _T("rb"));
2331 if ( !file
.IsOpened() )
2339 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2343 stream
.write(buf
, nRead
);
2347 while ( !file
.Eof() );
2352 bool wxTransferStreamToFile(wxSTD istream
& stream
, const wxString
& filename
)
2354 wxFFile
file(filename
, _T("wb"));
2355 if ( !file
.IsOpened() )
2361 stream
.read(buf
, WXSIZEOF(buf
));
2362 if ( !stream
.bad() ) // fail may be set on EOF, don't use operator!()
2364 if ( !file
.Write(buf
, stream
.gcount()) )
2368 while ( !stream
.eof() );
2373 #else // !wxUSE_STD_IOSTREAM
2375 bool wxTransferFileToStream(const wxString
& filename
, wxOutputStream
& stream
)
2377 wxFFile
file(filename
, _T("rb"));
2378 if ( !file
.IsOpened() )
2386 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2390 stream
.Write(buf
, nRead
);
2394 while ( !file
.Eof() );
2399 bool wxTransferStreamToFile(wxInputStream
& stream
, const wxString
& filename
)
2401 wxFFile
file(filename
, _T("wb"));
2402 if ( !file
.IsOpened() )
2408 stream
.Read(buf
, WXSIZEOF(buf
));
2410 const size_t nRead
= stream
.LastRead();
2411 if ( !nRead
|| !file
.Write(buf
, nRead
) )
2414 while ( !stream
.Eof() );
2419 #endif // wxUSE_STD_IOSTREAM/!wxUSE_STD_IOSTREAM
2421 #endif // wxUSE_DOC_VIEW_ARCHITECTURE