1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/docview.cpp
3 // Purpose: Document/view classes
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin
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"
47 #if wxUSE_PRINTING_ARCHITECTURE
48 #include "wx/prntbase.h"
49 #include "wx/printdlg.h"
52 #include "wx/confbase.h"
53 #include "wx/filename.h"
56 #include "wx/cmdproc.h"
57 #include "wx/tokenzr.h"
58 #include "wx/filename.h"
59 #include "wx/vector.h"
61 #if wxUSE_STD_IOSTREAM
62 #include "wx/ioswrap.h"
63 #include "wx/beforestd.h"
69 #include "wx/afterstd.h"
71 #include "wx/wfstream.h"
74 typedef wxVector
<wxDocTemplate
*> wxDocTemplates
;
76 // ----------------------------------------------------------------------------
78 // ----------------------------------------------------------------------------
80 IMPLEMENT_ABSTRACT_CLASS(wxDocument
, wxEvtHandler
)
81 IMPLEMENT_ABSTRACT_CLASS(wxView
, wxEvtHandler
)
82 IMPLEMENT_ABSTRACT_CLASS(wxDocTemplate
, wxObject
)
83 IMPLEMENT_DYNAMIC_CLASS(wxDocManager
, wxEvtHandler
)
84 IMPLEMENT_CLASS(wxDocChildFrame
, wxFrame
)
85 IMPLEMENT_CLASS(wxDocParentFrame
, wxFrame
)
87 #if wxUSE_PRINTING_ARCHITECTURE
88 IMPLEMENT_DYNAMIC_CLASS(wxDocPrintout
, wxPrintout
)
91 IMPLEMENT_DYNAMIC_CLASS(wxFileHistory
, wxObject
)
93 // ============================================================================
95 // ============================================================================
97 // ----------------------------------------------------------------------------
99 // ----------------------------------------------------------------------------
104 wxWindow
*wxFindSuitableParent()
106 wxWindow
* const win
= wxGetTopLevelParent(wxWindow::FindFocus());
108 return win
? win
: wxTheApp
->GetTopWindow();
111 wxString
FindExtension(const wxString
& path
)
114 wxSplitPath(path
, NULL
, NULL
, &ext
);
116 // VZ: extensions are considered not case sensitive - is this really a good
118 return ext
.MakeLower();
121 // return the string used for the MRU list items in the menu
123 // NB: the index n is 0-based, as usual, but the strings start from 1
124 wxString
GetMRUEntryLabel(int n
, const wxString
& path
)
126 // we need to quote '&' characters which are used for mnemonics
127 wxString
pathInMenu(path
);
128 pathInMenu
.Replace("&", "&&");
130 return wxString::Format("&%d %s", n
+ 1, pathInMenu
);
133 } // anonymous namespace
135 // ----------------------------------------------------------------------------
136 // Definition of wxDocument
137 // ----------------------------------------------------------------------------
139 wxDocument::wxDocument(wxDocument
*parent
)
141 m_documentModified
= false;
142 m_documentParent
= parent
;
143 m_documentTemplate
= NULL
;
144 m_commandProcessor
= NULL
;
148 bool wxDocument::DeleteContents()
153 wxDocument::~wxDocument()
157 if (m_commandProcessor
)
158 delete m_commandProcessor
;
160 if (GetDocumentManager())
161 GetDocumentManager()->RemoveDocument(this);
163 // Not safe to do here, since it'll invoke virtual view functions
164 // expecting to see valid derived objects: and by the time we get here,
165 // we've called destructors higher up.
169 bool wxDocument::Close()
171 if (OnSaveModified())
172 return OnCloseDocument();
177 bool wxDocument::OnCloseDocument()
179 // Tell all views that we're about to close
186 // Note that this implicitly deletes the document when the last view is
188 bool wxDocument::DeleteAllViews()
190 wxDocManager
* manager
= GetDocumentManager();
192 // first check if all views agree to be closed
193 const wxList::iterator end
= m_documentViews
.end();
194 for ( wxList::iterator i
= m_documentViews
.begin(); i
!= end
; ++i
)
196 wxView
*view
= (wxView
*)*i
;
197 if ( !view
->Close() )
201 // all views agreed to close, now do close them
202 if ( m_documentViews
.empty() )
204 // normally the document would be implicitly deleted when the last view
205 // is, but if don't have any views, do it here instead
206 if ( manager
&& manager
->GetDocuments().Member(this) )
211 // as we delete elements we iterate over, don't use the usual "from
212 // begin to end" loop
215 wxView
*view
= (wxView
*)*m_documentViews
.begin();
217 bool isLastOne
= m_documentViews
.size() == 1;
219 // this always deletes the node implicitly and if this is the last
220 // view also deletes this object itself (also implicitly, great),
221 // so we can't test for m_documentViews.empty() after calling this!
232 wxView
*wxDocument::GetFirstView() const
234 if (m_documentViews
.GetCount() == 0)
236 return (wxView
*)m_documentViews
.GetFirst()->GetData();
239 wxDocManager
*wxDocument::GetDocumentManager() const
241 return m_documentTemplate
? m_documentTemplate
->GetDocumentManager() : NULL
;
244 bool wxDocument::OnNewDocument()
246 if ( !OnSaveModified() )
251 SetDocumentSaved(false);
253 const wxString name
= GetDocumentManager()->MakeNewDocumentName();
255 SetFilename(name
, true);
260 bool wxDocument::Save()
262 if (!IsModified() && m_savedYet
)
265 if ( m_documentFile
.empty() || !m_savedYet
)
268 return OnSaveDocument(m_documentFile
);
271 bool wxDocument::SaveAs()
273 wxDocTemplate
*docTemplate
= GetDocumentTemplate();
277 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
278 wxString filter
= docTemplate
->GetDescription() + wxT(" (") + docTemplate
->GetFileFilter() + wxT(")|") + docTemplate
->GetFileFilter();
280 // Now see if there are some other template with identical view and document
281 // classes, whose filters may also be used.
283 if (docTemplate
->GetViewClassInfo() && docTemplate
->GetDocClassInfo())
285 wxList::compatibility_iterator node
= docTemplate
->GetDocumentManager()->GetTemplates().GetFirst();
288 wxDocTemplate
*t
= (wxDocTemplate
*) node
->GetData();
290 if (t
->IsVisible() && t
!= docTemplate
&&
291 t
->GetViewClassInfo() == docTemplate
->GetViewClassInfo() &&
292 t
->GetDocClassInfo() == docTemplate
->GetDocClassInfo())
294 // add a '|' to separate this filter from the previous one
295 if ( !filter
.empty() )
298 filter
<< t
->GetDescription() << wxT(" (") << t
->GetFileFilter() << wxT(") |")
299 << t
->GetFileFilter();
302 node
= node
->GetNext();
306 wxString filter
= docTemplate
->GetFileFilter() ;
308 wxString defaultDir
= docTemplate
->GetDirectory();
309 if (defaultDir
.IsEmpty())
310 defaultDir
= wxPathOnly(GetFilename());
312 wxString tmp
= wxFileSelector(_("Save As"),
314 wxFileNameFromPath(GetFilename()),
315 docTemplate
->GetDefaultExtension(),
317 wxFD_SAVE
| wxFD_OVERWRITE_PROMPT
,
318 GetDocumentWindow());
323 wxString
fileName(tmp
);
324 wxString path
, name
, ext
;
325 wxSplitPath(fileName
, & path
, & name
, & ext
);
329 fileName
+= wxT(".");
330 fileName
+= docTemplate
->GetDefaultExtension();
333 SetFilename(fileName
);
334 SetTitle(wxFileNameFromPath(fileName
));
336 // Notify the views that the filename has changed
337 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
340 wxView
*view
= (wxView
*)node
->GetData();
341 view
->OnChangeFilename();
342 node
= node
->GetNext();
345 // Files that were not saved correctly are not added to the FileHistory.
346 if (!OnSaveDocument(m_documentFile
))
349 // A file that doesn't use the default extension of its document template cannot be opened
350 // via the FileHistory, so we do not add it.
351 if (docTemplate
->FileMatchesTemplate(fileName
))
353 GetDocumentManager()->AddFileToHistory(fileName
);
357 // The user will probably not be able to open the file again, so
358 // we could warn about the wrong file-extension here.
363 bool wxDocument::OnSaveDocument(const wxString
& file
)
368 if ( !DoSaveDocument(file
) )
373 SetDocumentSaved(true);
374 #if defined( __WXOSX_MAC__ ) && wxOSX_USE_CARBON
375 wxFileName
fn(file
) ;
376 fn
.MacSetDefaultTypeAndCreator() ;
381 bool wxDocument::OnOpenDocument(const wxString
& file
)
383 if ( !OnSaveModified() )
386 if ( !DoOpenDocument(file
) )
389 SetFilename(file
, true);
398 #if wxUSE_STD_IOSTREAM
399 wxSTD istream
& wxDocument::LoadObject(wxSTD istream
& stream
)
401 wxInputStream
& wxDocument::LoadObject(wxInputStream
& stream
)
407 #if wxUSE_STD_IOSTREAM
408 wxSTD ostream
& wxDocument::SaveObject(wxSTD ostream
& stream
)
410 wxOutputStream
& wxDocument::SaveObject(wxOutputStream
& stream
)
416 bool wxDocument::Revert()
422 // Get title, or filename if no title, else unnamed
423 #if WXWIN_COMPATIBILITY_2_8
424 bool wxDocument::GetPrintableName(wxString
& buf
) const
426 // this function can not only be overridden by the user code but also
427 // called by it so we need to ensure that we return the same thing as
428 // GetUserReadableName() but we can't call it because this would result in
429 // an infinite recursion, hence we use the helper DoGetUserReadableName()
430 buf
= DoGetUserReadableName();
434 #endif // WXWIN_COMPATIBILITY_2_8
436 wxString
wxDocument::GetUserReadableName() const
438 #if WXWIN_COMPATIBILITY_2_8
439 // we need to call the old virtual function to ensure that the overridden
440 // version of it is still called
442 if ( GetPrintableName(name
) )
444 #endif // WXWIN_COMPATIBILITY_2_8
446 return DoGetUserReadableName();
449 wxString
wxDocument::DoGetUserReadableName() const
451 if ( !m_documentTitle
.empty() )
452 return m_documentTitle
;
454 if ( !m_documentFile
.empty() )
455 return wxFileNameFromPath(m_documentFile
);
460 wxWindow
*wxDocument::GetDocumentWindow() const
462 wxView
*view
= GetFirstView();
464 return view
->GetFrame();
466 return wxTheApp
->GetTopWindow();
469 wxCommandProcessor
*wxDocument::OnCreateCommandProcessor()
471 return new wxCommandProcessor
;
474 // true if safe to close
475 bool wxDocument::OnSaveModified()
479 switch ( wxMessageBox
483 _("Do you want to save changes to document %s?"),
484 GetUserReadableName()
486 wxTheApp
->GetAppDisplayName(),
487 wxYES_NO
| wxCANCEL
| wxICON_QUESTION
,
506 bool wxDocument::Draw(wxDC
& WXUNUSED(context
))
511 bool wxDocument::AddView(wxView
*view
)
513 if ( !m_documentViews
.Member(view
) )
515 m_documentViews
.Append(view
);
521 bool wxDocument::RemoveView(wxView
*view
)
523 (void)m_documentViews
.DeleteObject(view
);
528 bool wxDocument::OnCreate(const wxString
& WXUNUSED(path
), long flags
)
530 return GetDocumentTemplate()->CreateView(this, flags
) != NULL
;
533 // Called after a view is added or removed.
534 // The default implementation deletes the document if
535 // there are no more views.
536 void wxDocument::OnChangedViewList()
538 if ( m_documentViews
.empty() && OnSaveModified() )
542 void wxDocument::UpdateAllViews(wxView
*sender
, wxObject
*hint
)
544 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
547 wxView
*view
= (wxView
*)node
->GetData();
549 view
->OnUpdate(sender
, hint
);
550 node
= node
->GetNext();
554 void wxDocument::NotifyClosing()
556 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
559 wxView
*view
= (wxView
*)node
->GetData();
560 view
->OnClosingDocument();
561 node
= node
->GetNext();
565 void wxDocument::SetFilename(const wxString
& filename
, bool notifyViews
)
567 m_documentFile
= filename
;
570 // Notify the views that the filename has changed
571 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
574 wxView
*view
= (wxView
*)node
->GetData();
575 view
->OnChangeFilename();
576 node
= node
->GetNext();
581 bool wxDocument::DoSaveDocument(const wxString
& file
)
584 if (!wxTheApp
->GetAppDisplayName().empty())
585 msgTitle
= wxTheApp
->GetAppDisplayName();
587 msgTitle
= wxString(_("File error"));
589 #if wxUSE_STD_IOSTREAM
590 wxSTD ofstream
store(file
.mb_str(), wxSTD
ios::binary
);
591 if (store
.fail() || store
.bad())
593 wxFileOutputStream
store(file
);
594 if (store
.GetLastError() != wxSTREAM_NO_ERROR
)
597 (void)wxMessageBox(_("Sorry, could not open this file for saving."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
598 GetDocumentWindow());
602 if (!SaveObject(store
))
604 (void)wxMessageBox(_("Sorry, could not save this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
605 GetDocumentWindow());
613 bool wxDocument::DoOpenDocument(const wxString
& file
)
615 #if wxUSE_STD_IOSTREAM
616 wxSTD ifstream
store(file
.mb_str(), wxSTD
ios::binary
);
617 if (!store
.fail() && !store
.bad())
619 wxFileInputStream
store(file
);
620 if (store
.GetLastError() == wxSTREAM_NO_ERROR
)
623 #if wxUSE_STD_IOSTREAM
625 if ( !!store
|| store
.eof() )
627 int res
= LoadObject(store
).GetLastError();
628 if ( res
== wxSTREAM_NO_ERROR
|| res
== wxSTREAM_EOF
)
633 wxLogError(_("Sorry, could not open this file."));
638 // ----------------------------------------------------------------------------
640 // ----------------------------------------------------------------------------
644 m_viewDocument
= NULL
;
651 GetDocumentManager()->ActivateView(this, false);
652 m_viewDocument
->RemoveView(this);
655 // Extend event processing to search the document's event table
656 bool wxView::ProcessEvent(wxEvent
& event
)
658 if ( !GetDocument() || !GetDocument()->ProcessEvent(event
) )
659 return wxEvtHandler::ProcessEvent(event
);
664 void wxView::OnActivateView(bool WXUNUSED(activate
), wxView
*WXUNUSED(activeView
), wxView
*WXUNUSED(deactiveView
))
668 void wxView::OnPrint(wxDC
*dc
, wxObject
*WXUNUSED(info
))
673 void wxView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
))
677 void wxView::OnChangeFilename()
679 // GetFrame can return wxWindow rather than wxTopLevelWindow due to
680 // generic MDI implementation so use SetLabel rather than SetTitle.
681 // It should cause SetTitle() for top level windows.
682 wxWindow
*win
= GetFrame();
685 wxDocument
*doc
= GetDocument();
688 win
->SetLabel(doc
->GetUserReadableName());
691 void wxView::SetDocument(wxDocument
*doc
)
693 m_viewDocument
= doc
;
698 bool wxView::Close(bool deleteWindow
)
700 return OnClose(deleteWindow
);
703 void wxView::Activate(bool activate
)
705 if (GetDocument() && GetDocumentManager())
707 OnActivateView(activate
, this, GetDocumentManager()->GetCurrentView());
708 GetDocumentManager()->ActivateView(this, activate
);
712 bool wxView::OnClose(bool WXUNUSED(deleteWindow
))
714 return GetDocument() ? GetDocument()->Close() : true;
717 #if wxUSE_PRINTING_ARCHITECTURE
718 wxPrintout
*wxView::OnCreatePrintout()
720 return new wxDocPrintout(this);
722 #endif // wxUSE_PRINTING_ARCHITECTURE
724 // ----------------------------------------------------------------------------
726 // ----------------------------------------------------------------------------
728 wxDocTemplate::wxDocTemplate(wxDocManager
*manager
,
729 const wxString
& descr
,
730 const wxString
& filter
,
733 const wxString
& docTypeName
,
734 const wxString
& viewTypeName
,
735 wxClassInfo
*docClassInfo
,
736 wxClassInfo
*viewClassInfo
,
739 m_documentManager
= manager
;
740 m_description
= descr
;
743 m_fileFilter
= filter
;
745 m_docTypeName
= docTypeName
;
746 m_viewTypeName
= viewTypeName
;
747 m_documentManager
->AssociateTemplate(this);
749 m_docClassInfo
= docClassInfo
;
750 m_viewClassInfo
= viewClassInfo
;
753 wxDocTemplate::~wxDocTemplate()
755 m_documentManager
->DisassociateTemplate(this);
758 // Tries to dynamically construct an object of the right class.
759 wxDocument
*wxDocTemplate::CreateDocument(const wxString
& path
, long flags
)
761 wxDocument
* const doc
= DoCreateDocument();
763 // VZ: this code doesn't delete doc if InitDocument() (i.e. doc->OnCreate())
764 // fails, is this intentional?
766 return doc
&& InitDocument(doc
, path
, flags
) ? doc
: NULL
;
770 wxDocTemplate::InitDocument(wxDocument
* doc
, const wxString
& path
, long flags
)
772 doc
->SetFilename(path
);
773 doc
->SetDocumentTemplate(this);
774 GetDocumentManager()->AddDocument(doc
);
775 doc
->SetCommandProcessor(doc
->OnCreateCommandProcessor());
777 if (doc
->OnCreate(path
, flags
))
781 if (GetDocumentManager()->GetDocuments().Member(doc
))
782 doc
->DeleteAllViews();
787 wxView
*wxDocTemplate::CreateView(wxDocument
*doc
, long flags
)
789 wxView
*view
= DoCreateView();
793 view
->SetDocument(doc
);
794 if (view
->OnCreate(doc
, flags
))
805 // The default (very primitive) format detection: check is the extension is
806 // that of the template
807 bool wxDocTemplate::FileMatchesTemplate(const wxString
& path
)
809 wxStringTokenizer
parser (GetFileFilter(), wxT(";"));
810 wxString anything
= wxT ("*");
811 while (parser
.HasMoreTokens())
813 wxString filter
= parser
.GetNextToken();
814 wxString filterExt
= FindExtension (filter
);
815 if ( filter
.IsSameAs (anything
) ||
816 filterExt
.IsSameAs (anything
) ||
817 filterExt
.IsSameAs (FindExtension (path
)) )
820 return GetDefaultExtension().IsSameAs(FindExtension(path
));
823 wxDocument
*wxDocTemplate::DoCreateDocument()
828 return (wxDocument
*)m_docClassInfo
->CreateObject();
831 wxView
*wxDocTemplate::DoCreateView()
833 if (!m_viewClassInfo
)
836 return (wxView
*)m_viewClassInfo
->CreateObject();
839 // ----------------------------------------------------------------------------
841 // ----------------------------------------------------------------------------
843 BEGIN_EVENT_TABLE(wxDocManager
, wxEvtHandler
)
844 EVT_MENU(wxID_OPEN
, wxDocManager::OnFileOpen
)
845 EVT_MENU(wxID_CLOSE
, wxDocManager::OnFileClose
)
846 EVT_MENU(wxID_CLOSE_ALL
, wxDocManager::OnFileCloseAll
)
847 EVT_MENU(wxID_REVERT
, wxDocManager::OnFileRevert
)
848 EVT_MENU(wxID_NEW
, wxDocManager::OnFileNew
)
849 EVT_MENU(wxID_SAVE
, wxDocManager::OnFileSave
)
850 EVT_MENU(wxID_SAVEAS
, wxDocManager::OnFileSaveAs
)
851 EVT_MENU(wxID_UNDO
, wxDocManager::OnUndo
)
852 EVT_MENU(wxID_REDO
, wxDocManager::OnRedo
)
854 EVT_UPDATE_UI(wxID_OPEN
, wxDocManager::OnUpdateFileOpen
)
855 EVT_UPDATE_UI(wxID_CLOSE
, wxDocManager::OnUpdateDisableIfNoDoc
)
856 EVT_UPDATE_UI(wxID_CLOSE_ALL
, wxDocManager::OnUpdateDisableIfNoDoc
)
857 EVT_UPDATE_UI(wxID_REVERT
, wxDocManager::OnUpdateDisableIfNoDoc
)
858 EVT_UPDATE_UI(wxID_NEW
, wxDocManager::OnUpdateFileNew
)
859 EVT_UPDATE_UI(wxID_SAVE
, wxDocManager::OnUpdateFileSave
)
860 EVT_UPDATE_UI(wxID_SAVEAS
, wxDocManager::OnUpdateDisableIfNoDoc
)
861 EVT_UPDATE_UI(wxID_UNDO
, wxDocManager::OnUpdateUndo
)
862 EVT_UPDATE_UI(wxID_REDO
, wxDocManager::OnUpdateRedo
)
864 #if wxUSE_PRINTING_ARCHITECTURE
865 EVT_MENU(wxID_PRINT
, wxDocManager::OnPrint
)
866 EVT_MENU(wxID_PREVIEW
, wxDocManager::OnPreview
)
868 EVT_UPDATE_UI(wxID_PRINT
, wxDocManager::OnUpdateDisableIfNoDoc
)
869 EVT_UPDATE_UI(wxID_PREVIEW
, wxDocManager::OnUpdateDisableIfNoDoc
)
873 wxDocManager
* wxDocManager::sm_docManager
= NULL
;
875 wxDocManager::wxDocManager(long WXUNUSED(flags
), bool initialize
)
877 wxASSERT_MSG( !sm_docManager
, "multiple wxDocManagers not allowed" );
879 sm_docManager
= this;
881 m_defaultDocumentNameCounter
= 1;
882 m_currentView
= NULL
;
883 m_maxDocsOpen
= INT_MAX
;
884 m_fileHistory
= NULL
;
889 wxDocManager::~wxDocManager()
892 delete m_fileHistory
;
893 sm_docManager
= NULL
;
896 // closes the specified document
897 bool wxDocManager::CloseDocument(wxDocument
* doc
, bool force
)
899 if (doc
->Close() || force
)
901 // Implicitly deletes the document when
902 // the last view is deleted
903 doc
->DeleteAllViews();
905 // Check we're really deleted
906 if (m_docs
.Member(doc
))
914 bool wxDocManager::CloseDocuments(bool force
)
916 wxList::compatibility_iterator node
= m_docs
.GetFirst();
919 wxDocument
*doc
= (wxDocument
*)node
->GetData();
920 wxList::compatibility_iterator next
= node
->GetNext();
922 if (!CloseDocument(doc
, force
))
925 // This assumes that documents are not connected in
926 // any way, i.e. deleting one document does NOT
933 bool wxDocManager::Clear(bool force
)
935 if (!CloseDocuments(force
))
938 m_currentView
= NULL
;
940 wxList::compatibility_iterator node
= m_templates
.GetFirst();
943 wxDocTemplate
*templ
= (wxDocTemplate
*) node
->GetData();
944 wxList::compatibility_iterator next
= node
->GetNext();
951 bool wxDocManager::Initialize()
953 m_fileHistory
= OnCreateFileHistory();
957 wxFileHistory
*wxDocManager::OnCreateFileHistory()
959 return new wxFileHistory
;
962 void wxDocManager::OnFileClose(wxCommandEvent
& WXUNUSED(event
))
964 wxDocument
*doc
= GetCurrentDocument();
969 doc
->DeleteAllViews();
970 if (m_docs
.Member(doc
))
975 void wxDocManager::OnFileCloseAll(wxCommandEvent
& WXUNUSED(event
))
977 CloseDocuments(false);
980 void wxDocManager::OnFileNew(wxCommandEvent
& WXUNUSED(event
))
982 CreateDocument( wxEmptyString
, wxDOC_NEW
);
985 void wxDocManager::OnFileOpen(wxCommandEvent
& WXUNUSED(event
))
987 if ( !CreateDocument( wxEmptyString
, 0) )
993 void wxDocManager::OnFileRevert(wxCommandEvent
& WXUNUSED(event
))
995 wxDocument
*doc
= GetCurrentDocument();
1001 void wxDocManager::OnFileSave(wxCommandEvent
& WXUNUSED(event
))
1003 wxDocument
*doc
= GetCurrentDocument();
1009 void wxDocManager::OnFileSaveAs(wxCommandEvent
& WXUNUSED(event
))
1011 wxDocument
*doc
= GetCurrentDocument();
1017 void wxDocManager::OnPrint(wxCommandEvent
& WXUNUSED(event
))
1019 #if wxUSE_PRINTING_ARCHITECTURE
1020 wxView
*view
= GetCurrentView();
1024 wxPrintout
*printout
= view
->OnCreatePrintout();
1028 printer
.Print(view
->GetFrame(), printout
, true);
1032 #endif // wxUSE_PRINTING_ARCHITECTURE
1035 void wxDocManager::OnPreview(wxCommandEvent
& WXUNUSED(event
))
1037 #if wxUSE_PRINTING_ARCHITECTURE
1038 wxView
*view
= GetCurrentView();
1042 wxPrintout
*printout
= view
->OnCreatePrintout();
1045 // Pass two printout objects: for preview, and possible printing.
1046 wxPrintPreviewBase
*preview
= new wxPrintPreview(printout
, view
->OnCreatePrintout());
1047 if ( !preview
->Ok() )
1050 wxMessageBox( _("Sorry, print preview needs a printer to be installed.") );
1054 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, (wxFrame
*)wxTheApp
->GetTopWindow(), _("Print Preview"),
1055 wxPoint(100, 100), wxSize(600, 650));
1056 frame
->Centre(wxBOTH
);
1057 frame
->Initialize();
1060 #endif // wxUSE_PRINTING_ARCHITECTURE
1063 void wxDocManager::OnUndo(wxCommandEvent
& event
)
1065 wxDocument
*doc
= GetCurrentDocument();
1068 if (doc
->GetCommandProcessor())
1069 doc
->GetCommandProcessor()->Undo();
1074 void wxDocManager::OnRedo(wxCommandEvent
& event
)
1076 wxDocument
*doc
= GetCurrentDocument();
1079 if (doc
->GetCommandProcessor())
1080 doc
->GetCommandProcessor()->Redo();
1085 // Handlers for UI update commands
1087 void wxDocManager::OnUpdateFileOpen(wxUpdateUIEvent
& event
)
1089 event
.Enable( true );
1092 void wxDocManager::OnUpdateDisableIfNoDoc(wxUpdateUIEvent
& event
)
1094 event
.Enable( GetCurrentDocument() != NULL
);
1097 void wxDocManager::OnUpdateFileNew(wxUpdateUIEvent
& event
)
1099 event
.Enable( true );
1102 void wxDocManager::OnUpdateFileSave(wxUpdateUIEvent
& event
)
1104 wxDocument
*doc
= GetCurrentDocument();
1105 event
.Enable( doc
&& doc
->IsModified() );
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 wxView
*wxDocManager::GetCurrentView() const
1139 return m_currentView
;
1140 if (m_docs
.GetCount() == 1)
1142 wxDocument
* doc
= (wxDocument
*) m_docs
.GetFirst()->GetData();
1143 return doc
->GetFirstView();
1148 // Extend event processing to search the view's event table
1149 bool wxDocManager::ProcessEvent(wxEvent
& event
)
1151 wxView
* const view
= GetCurrentView();
1152 if ( view
&& view
->ProcessEvent(event
) )
1155 return wxEvtHandler::ProcessEvent(event
);
1161 // helper function: return only the visible templates
1162 wxDocTemplates
GetVisibleTemplates(const wxList
& allTemplates
)
1164 // select only the visible templates
1165 const size_t totalNumTemplates
= allTemplates
.GetCount();
1166 wxDocTemplates templates
;
1167 if ( totalNumTemplates
)
1169 templates
.reserve(totalNumTemplates
);
1171 for ( wxList::const_iterator i
= allTemplates
.begin(),
1172 end
= allTemplates
.end();
1176 wxDocTemplate
* const temp
= (wxDocTemplate
*)*i
;
1177 if ( temp
->IsVisible() )
1178 templates
.push_back(temp
);
1185 } // anonymous namespace
1187 wxDocument
*wxDocManager::CreateDocument(const wxString
& pathOrig
, long flags
)
1189 // this ought to be const but SelectDocumentType/Path() are not
1190 // const-correct and can't be changed as, being virtual, this risks
1191 // breaking user code overriding them
1192 wxDocTemplates
templates(GetVisibleTemplates(m_templates
));
1193 const size_t numTemplates
= templates
.size();
1194 if ( !numTemplates
)
1196 // no templates can be used, can't create document
1201 // normally user should select the template to use but wxDOC_SILENT flag we
1202 // choose one ourselves
1203 wxString path
= pathOrig
; // may be modified below
1204 wxDocTemplate
*temp
;
1205 if ( flags
& wxDOC_SILENT
)
1207 wxASSERT_MSG( !path
.empty(),
1208 "using empty path with wxDOC_SILENT doesn't make sense" );
1210 temp
= FindTemplateForPath(path
);
1213 wxLogWarning(_("The format of file '%s' couldn't be determined."),
1217 else // not silent, ask the user
1219 // for the new file we need just the template, for an existing one we
1220 // need the template and the path, unless it's already specified
1221 if ( (flags
& wxDOC_NEW
) || !path
.empty() )
1222 temp
= SelectDocumentType(&templates
[0], numTemplates
);
1224 temp
= SelectDocumentPath(&templates
[0], numTemplates
, path
, flags
);
1230 // check whether the document with this path is already opened
1231 if ( !path
.empty() )
1233 const wxFileName
fn(path
);
1234 for ( wxList::const_iterator i
= m_docs
.begin(); i
!= m_docs
.end(); ++i
)
1236 wxDocument
* const doc
= (wxDocument
*)*i
;
1238 if ( fn
== doc
->GetFilename() )
1240 // file already open, just activate it and return
1241 if ( doc
->GetFirstView() )
1243 ActivateView(doc
->GetFirstView());
1244 if ( doc
->GetDocumentWindow() )
1245 doc
->GetDocumentWindow()->SetFocus();
1253 // no, we need to create a new document
1256 // if we've reached the max number of docs, close the first one.
1257 if ( (int)GetDocuments().GetCount() >= m_maxDocsOpen
)
1259 if ( !CloseDocument((wxDocument
*)GetDocuments().GetFirst()->GetData()) )
1261 // can't open the new document if closing the old one failed
1267 // do create and initialize the new document finally
1268 wxDocument
* const docNew
= temp
->CreateDocument(path
, flags
);
1272 docNew
->SetDocumentName(temp
->GetDocumentName());
1273 docNew
->SetDocumentTemplate(temp
);
1275 // call the appropriate function depending on whether we're creating a new
1276 // file or opening an existing one
1277 if ( !(flags
& wxDOC_NEW
? docNew
->OnNewDocument()
1278 : docNew
->OnOpenDocument(path
)) )
1280 // Document is implicitly deleted by DeleteAllViews
1281 docNew
->DeleteAllViews();
1285 // add the successfully opened file to MRU, but only if we're going to be
1286 // able to reopen it successfully later which requires the template for
1287 // this document to be retrievable from the file extension
1288 if ( !(flags
& wxDOC_NEW
) && temp
->FileMatchesTemplate(path
) )
1289 AddFileToHistory(path
);
1294 wxView
*wxDocManager::CreateView(wxDocument
*doc
, long flags
)
1296 wxDocTemplates
templates(GetVisibleTemplates(m_templates
));
1297 const size_t numTemplates
= templates
.size();
1299 if ( numTemplates
== 0 )
1302 wxDocTemplate
* const
1303 temp
= numTemplates
== 1 ? templates
[0]
1304 : SelectViewType(&templates
[0], numTemplates
);
1309 wxView
*view
= temp
->CreateView(doc
, flags
);
1311 view
->SetViewName(temp
->GetViewName());
1315 // Not yet implemented
1317 wxDocManager::DeleteTemplate(wxDocTemplate
*WXUNUSED(temp
), long WXUNUSED(flags
))
1321 // Not yet implemented
1322 bool wxDocManager::FlushDoc(wxDocument
*WXUNUSED(doc
))
1327 wxDocument
*wxDocManager::GetCurrentDocument() const
1329 wxView
*view
= GetCurrentView();
1331 return view
->GetDocument();
1336 // Make a default name for a new document
1337 #if WXWIN_COMPATIBILITY_2_8
1338 bool wxDocManager::MakeDefaultName(wxString
& WXUNUSED(name
))
1340 // we consider that this function can only be overridden by the user code,
1341 // not called by it as it only makes sense to call it internally, so we
1342 // don't bother to return anything from here
1345 #endif // WXWIN_COMPATIBILITY_2_8
1347 wxString
wxDocManager::MakeNewDocumentName()
1351 #if WXWIN_COMPATIBILITY_2_8
1352 if ( !MakeDefaultName(name
) )
1353 #endif // WXWIN_COMPATIBILITY_2_8
1355 name
.Printf(_("unnamed%d"), m_defaultDocumentNameCounter
);
1356 m_defaultDocumentNameCounter
++;
1362 // Make a frame title (override this to do something different)
1363 // If docName is empty, a document is not currently active.
1364 wxString
wxDocManager::MakeFrameTitle(wxDocument
* doc
)
1366 wxString appName
= wxTheApp
->GetAppDisplayName();
1372 wxString docName
= doc
->GetUserReadableName();
1373 title
= docName
+ wxString(_(" - ")) + appName
;
1379 // Not yet implemented
1380 wxDocTemplate
*wxDocManager::MatchTemplate(const wxString
& WXUNUSED(path
))
1385 // File history management
1386 void wxDocManager::AddFileToHistory(const wxString
& file
)
1389 m_fileHistory
->AddFileToHistory(file
);
1392 void wxDocManager::RemoveFileFromHistory(size_t i
)
1395 m_fileHistory
->RemoveFileFromHistory(i
);
1398 wxString
wxDocManager::GetHistoryFile(size_t i
) const
1403 histFile
= m_fileHistory
->GetHistoryFile(i
);
1408 void wxDocManager::FileHistoryUseMenu(wxMenu
*menu
)
1411 m_fileHistory
->UseMenu(menu
);
1414 void wxDocManager::FileHistoryRemoveMenu(wxMenu
*menu
)
1417 m_fileHistory
->RemoveMenu(menu
);
1421 void wxDocManager::FileHistoryLoad(const wxConfigBase
& config
)
1424 m_fileHistory
->Load(config
);
1427 void wxDocManager::FileHistorySave(wxConfigBase
& config
)
1430 m_fileHistory
->Save(config
);
1434 void wxDocManager::FileHistoryAddFilesToMenu(wxMenu
* menu
)
1437 m_fileHistory
->AddFilesToMenu(menu
);
1440 void wxDocManager::FileHistoryAddFilesToMenu()
1443 m_fileHistory
->AddFilesToMenu();
1446 size_t wxDocManager::GetHistoryFilesCount() const
1448 return m_fileHistory
? m_fileHistory
->GetCount() : 0;
1452 // Find out the document template via matching in the document file format
1453 // against that of the template
1454 wxDocTemplate
*wxDocManager::FindTemplateForPath(const wxString
& path
)
1456 wxDocTemplate
*theTemplate
= NULL
;
1458 // Find the template which this extension corresponds to
1459 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1461 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Item(i
)->GetData();
1462 if ( temp
->FileMatchesTemplate(path
) )
1471 // Prompts user to open a file, using file specs in templates.
1472 // Must extend the file selector dialog or implement own; OR
1473 // match the extension to the template extension.
1475 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**templates
,
1478 long WXUNUSED(flags
),
1479 bool WXUNUSED(save
))
1481 // We can only have multiple filters in Windows and GTK
1482 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
1486 for (i
= 0; i
< noTemplates
; i
++)
1488 if (templates
[i
]->IsVisible())
1490 // add a '|' to separate this filter from the previous one
1491 if ( !descrBuf
.empty() )
1492 descrBuf
<< wxT('|');
1494 descrBuf
<< templates
[i
]->GetDescription()
1495 << wxT(" (") << templates
[i
]->GetFileFilter() << wxT(") |")
1496 << templates
[i
]->GetFileFilter();
1500 wxString descrBuf
= wxT("*.*");
1501 wxUnusedVar(noTemplates
);
1504 int FilterIndex
= -1;
1506 wxWindow
* parent
= wxFindSuitableParent();
1508 wxString pathTmp
= wxFileSelectorEx(_("Open File"),
1516 wxDocTemplate
*theTemplate
= NULL
;
1517 if (!pathTmp
.empty())
1519 if (!wxFileExists(pathTmp
))
1522 if (!wxTheApp
->GetAppDisplayName().empty())
1523 msgTitle
= wxTheApp
->GetAppDisplayName();
1525 msgTitle
= wxString(_("File error"));
1527 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
1530 path
= wxEmptyString
;
1533 m_lastDirectory
= wxPathOnly(pathTmp
);
1537 // first choose the template using the extension, if this fails (i.e.
1538 // wxFileSelectorEx() didn't fill it), then use the path
1539 if ( FilterIndex
!= -1 )
1540 theTemplate
= templates
[FilterIndex
];
1542 theTemplate
= FindTemplateForPath(path
);
1545 // Since we do not add files with non-default extensions to the FileHistory this
1546 // can only happen if the application changes the allowed templates in runtime.
1547 (void)wxMessageBox(_("Sorry, the format for this file is unknown."),
1549 wxOK
| wxICON_EXCLAMATION
, wxFindSuitableParent());
1554 path
= wxEmptyString
;
1560 wxDocTemplate
*wxDocManager::SelectDocumentType(wxDocTemplate
**templates
,
1561 int noTemplates
, bool sort
)
1563 wxArrayString strings
;
1564 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1568 for (i
= 0; i
< noTemplates
; i
++)
1570 if (templates
[i
]->IsVisible())
1574 for (j
= 0; j
< n
; j
++)
1576 //filter out NOT unique documents + view combinations
1577 if ( templates
[i
]->m_docTypeName
== data
[j
]->m_docTypeName
&&
1578 templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
1585 strings
.Add(templates
[i
]->m_description
);
1587 data
[n
] = templates
[i
];
1595 strings
.Sort(); // ascending sort
1596 // Yes, this will be slow, but template lists
1597 // are typically short.
1599 n
= strings
.Count();
1600 for (i
= 0; i
< n
; i
++)
1602 for (j
= 0; j
< noTemplates
; j
++)
1604 if (strings
[i
] == templates
[j
]->m_description
)
1605 data
[i
] = templates
[j
];
1610 wxDocTemplate
*theTemplate
;
1615 // no visible templates, hence nothing to choose from
1620 // don't propose the user to choose if he has no choice
1621 theTemplate
= data
[0];
1625 // propose the user to choose one of several
1626 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1628 _("Select a document template"),
1632 wxFindSuitableParent()
1641 wxDocTemplate
*wxDocManager::SelectViewType(wxDocTemplate
**templates
,
1642 int noTemplates
, bool sort
)
1644 wxArrayString strings
;
1645 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1649 for (i
= 0; i
< noTemplates
; i
++)
1651 wxDocTemplate
*templ
= templates
[i
];
1652 if ( templ
->IsVisible() && !templ
->GetViewName().empty() )
1656 for (j
= 0; j
< n
; j
++)
1658 //filter out NOT unique views
1659 if ( templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
)
1665 strings
.Add(templ
->m_viewTypeName
);
1674 strings
.Sort(); // ascending sort
1675 // Yes, this will be slow, but template lists
1676 // are typically short.
1678 n
= strings
.Count();
1679 for (i
= 0; i
< n
; i
++)
1681 for (j
= 0; j
< noTemplates
; j
++)
1683 if (strings
[i
] == templates
[j
]->m_viewTypeName
)
1684 data
[i
] = templates
[j
];
1689 wxDocTemplate
*theTemplate
;
1691 // the same logic as above
1699 theTemplate
= data
[0];
1703 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1705 _("Select a document view"),
1709 wxFindSuitableParent()
1718 void wxDocManager::AssociateTemplate(wxDocTemplate
*temp
)
1720 if (!m_templates
.Member(temp
))
1721 m_templates
.Append(temp
);
1724 void wxDocManager::DisassociateTemplate(wxDocTemplate
*temp
)
1726 m_templates
.DeleteObject(temp
);
1729 // Add and remove a document from the manager's list
1730 void wxDocManager::AddDocument(wxDocument
*doc
)
1732 if (!m_docs
.Member(doc
))
1736 void wxDocManager::RemoveDocument(wxDocument
*doc
)
1738 m_docs
.DeleteObject(doc
);
1741 // Views or windows should inform the document manager
1742 // when a view is going in or out of focus
1743 void wxDocManager::ActivateView(wxView
*view
, bool activate
)
1747 m_currentView
= view
;
1751 if ( m_currentView
== view
)
1753 // don't keep stale pointer
1754 m_currentView
= NULL
;
1759 // ----------------------------------------------------------------------------
1760 // Default document child frame
1761 // ----------------------------------------------------------------------------
1763 BEGIN_EVENT_TABLE(wxDocChildFrame
, wxFrame
)
1764 EVT_ACTIVATE(wxDocChildFrame::OnActivate
)
1765 EVT_CLOSE(wxDocChildFrame::OnCloseWindow
)
1768 wxDocChildFrame::wxDocChildFrame(wxDocument
*doc
,
1772 const wxString
& title
,
1776 const wxString
& name
)
1777 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1779 m_childDocument
= doc
;
1782 view
->SetFrame(this);
1785 // Extend event processing to search the view's event table
1786 bool wxDocChildFrame::ProcessEvent(wxEvent
& event
)
1789 m_childView
->Activate(true);
1791 if ( !m_childView
|| ! m_childView
->ProcessEvent(event
) )
1793 // Only hand up to the parent if it's a menu command
1794 if (!event
.IsKindOf(CLASSINFO(wxCommandEvent
)) || !GetParent() || !GetParent()->ProcessEvent(event
))
1795 return wxEvtHandler::ProcessEvent(event
);
1803 void wxDocChildFrame::OnActivate(wxActivateEvent
& event
)
1805 wxFrame::OnActivate(event
);
1808 m_childView
->Activate(event
.GetActive());
1811 void wxDocChildFrame::OnCloseWindow(wxCloseEvent
& event
)
1815 bool ans
= event
.CanVeto()
1816 ? m_childView
->Close(false) // false means don't delete associated window
1817 : true; // Must delete.
1821 m_childView
->Activate(false);
1824 m_childDocument
= NULL
;
1835 // ----------------------------------------------------------------------------
1836 // Default parent frame
1837 // ----------------------------------------------------------------------------
1839 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1840 EVT_MENU(wxID_EXIT
, wxDocParentFrame::OnExit
)
1841 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE9
, wxDocParentFrame::OnMRUFile
)
1842 EVT_CLOSE(wxDocParentFrame::OnCloseWindow
)
1845 wxDocParentFrame::wxDocParentFrame()
1847 m_docManager
= NULL
;
1850 wxDocParentFrame::wxDocParentFrame(wxDocManager
*manager
,
1853 const wxString
& title
,
1857 const wxString
& name
)
1858 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1860 m_docManager
= manager
;
1863 bool wxDocParentFrame::Create(wxDocManager
*manager
,
1866 const wxString
& title
,
1870 const wxString
& name
)
1872 m_docManager
= manager
;
1873 return base_type::Create(frame
, id
, title
, pos
, size
, style
, name
);
1876 void wxDocParentFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
1881 void wxDocParentFrame::OnMRUFile(wxCommandEvent
& event
)
1883 int n
= event
.GetId() - wxID_FILE1
; // the index in MRU list
1884 wxString
filename(m_docManager
->GetHistoryFile(n
));
1885 if ( filename
.empty() )
1888 wxString errMsg
; // must contain exactly one "%s" if non-empty
1889 if ( wxFile::Exists(filename
) )
1892 if ( m_docManager
->CreateDocument(filename
, wxDOC_SILENT
) )
1895 errMsg
= _("The file '%s' couldn't be opened.");
1897 else // file doesn't exist
1899 errMsg
= _("The file '%s' doesn't exist and couldn't be opened.");
1903 wxASSERT_MSG( !errMsg
.empty(), "should have an error message" );
1905 // remove the file which we can't open from the MRU list
1906 m_docManager
->RemoveFileFromHistory(n
);
1908 // and tell the user about it
1909 wxLogError(errMsg
+ '\n' +
1910 _("It has been removed from the most recently used files list."),
1914 // Extend event processing to search the view's event table
1915 bool wxDocParentFrame::ProcessEvent(wxEvent
& event
)
1917 // Try the document manager, then do default processing
1918 if (!m_docManager
|| !m_docManager
->ProcessEvent(event
))
1919 return wxEvtHandler::ProcessEvent(event
);
1924 // Define the behaviour for the frame closing
1925 // - must delete all frames except for the main one.
1926 void wxDocParentFrame::OnCloseWindow(wxCloseEvent
& event
)
1928 if (m_docManager
->Clear(!event
.CanVeto()))
1936 #if wxUSE_PRINTING_ARCHITECTURE
1938 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
)
1941 m_printoutView
= view
;
1944 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
1948 // Get the logical pixels per inch of screen and printer
1949 int ppiScreenX
, ppiScreenY
;
1950 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
1951 wxUnusedVar(ppiScreenY
);
1952 int ppiPrinterX
, ppiPrinterY
;
1953 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
1954 wxUnusedVar(ppiPrinterY
);
1956 // This scales the DC so that the printout roughly represents the
1957 // the screen scaling. The text point size _should_ be the right size
1958 // but in fact is too small for some reason. This is a detail that will
1959 // need to be addressed at some point but can be fudged for the
1961 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
1963 // Now we have to check in case our real page size is reduced
1964 // (e.g. because we're drawing to a print preview memory DC)
1965 int pageWidth
, pageHeight
;
1967 dc
->GetSize(&w
, &h
);
1968 GetPageSizePixels(&pageWidth
, &pageHeight
);
1969 wxUnusedVar(pageHeight
);
1971 // If printer pageWidth == current DC width, then this doesn't
1972 // change. But w might be the preview bitmap width, so scale down.
1973 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
1974 dc
->SetUserScale(overallScale
, overallScale
);
1978 m_printoutView
->OnDraw(dc
);
1983 bool wxDocPrintout::HasPage(int pageNum
)
1985 return (pageNum
== 1);
1988 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
1990 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
1996 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
2004 #endif // wxUSE_PRINTING_ARCHITECTURE
2006 // ----------------------------------------------------------------------------
2007 // File history (a.k.a. MRU, most recently used, files list)
2008 // ----------------------------------------------------------------------------
2010 wxFileHistory::wxFileHistory(size_t maxFiles
, wxWindowID idBase
)
2012 m_fileMaxFiles
= maxFiles
;
2016 void wxFileHistory::AddFileToHistory(const wxString
& file
)
2018 // check if we don't already have this file
2019 const wxFileName
fnNew(file
);
2021 numFiles
= m_fileHistory
.size();
2022 for ( i
= 0; i
< numFiles
; i
++ )
2024 if ( fnNew
== m_fileHistory
[i
] )
2026 // we do have it, move it to the top of the history
2027 RemoveFileFromHistory(i
);
2033 // if we already have a full history, delete the one at the end
2034 if ( numFiles
== m_fileMaxFiles
)
2036 RemoveFileFromHistory(--numFiles
);
2038 else // add a new menu item to all file menus (will be updated below)
2040 for ( wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2042 node
= node
->GetNext() )
2044 wxMenu
* const menu
= (wxMenu
*)node
->GetData();
2046 if ( !numFiles
&& menu
->GetMenuItemCount() )
2047 menu
->AppendSeparator();
2049 // label doesn't matter, it will be set below anyhow, but it can't
2050 // be empty (this is supposed to indicate a stock item)
2051 menu
->Append(m_idBase
+ numFiles
, " ");
2056 // insert the new file in the beginning of the file history
2057 m_fileHistory
.insert(m_fileHistory
.begin(), file
);
2060 // update the labels in all menus
2061 for ( i
= 0; i
< numFiles
; i
++ )
2063 // if in same directory just show the filename; otherwise the full path
2064 const wxFileName
fnOld(m_fileHistory
[i
]);
2066 wxString pathInMenu
;
2067 if ( fnOld
.GetPath() == fnNew
.GetPath() )
2069 pathInMenu
= fnOld
.GetFullName();
2071 else // file in different directory
2073 // absolute path; could also set relative path
2074 pathInMenu
= m_fileHistory
[i
];
2077 for ( wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2079 node
= node
->GetNext() )
2081 wxMenu
* const menu
= (wxMenu
*)node
->GetData();
2083 menu
->SetLabel(m_idBase
+ i
, GetMRUEntryLabel(i
, pathInMenu
));
2088 void wxFileHistory::RemoveFileFromHistory(size_t i
)
2090 size_t numFiles
= m_fileHistory
.size();
2091 wxCHECK_RET( i
< numFiles
,
2092 wxT("invalid index in wxFileHistory::RemoveFileFromHistory") );
2094 // delete the element from the array
2095 m_fileHistory
.RemoveAt(i
);
2098 for ( wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2100 node
= node
->GetNext() )
2102 wxMenu
* const menu
= (wxMenu
*) node
->GetData();
2104 // shift filenames up
2105 for ( size_t j
= i
; j
< numFiles
; j
++ )
2107 menu
->SetLabel(m_idBase
+ j
, GetMRUEntryLabel(j
, m_fileHistory
[j
]));
2110 // delete the last menu item which is unused now
2111 const wxWindowID lastItemId
= m_idBase
+ numFiles
;
2112 if ( menu
->FindItem(lastItemId
) )
2113 menu
->Delete(lastItemId
);
2115 // delete the last separator too if no more files are left
2116 if ( m_fileHistory
.empty() )
2118 const wxMenuItemList::compatibility_iterator
2119 nodeLast
= menu
->GetMenuItems().GetLast();
2122 wxMenuItem
* const lastMenuItem
= nodeLast
->GetData();
2123 if ( lastMenuItem
->IsSeparator() )
2124 menu
->Delete(lastMenuItem
);
2126 //else: menu is empty somehow
2131 void wxFileHistory::UseMenu(wxMenu
*menu
)
2133 if ( !m_fileMenus
.Member(menu
) )
2134 m_fileMenus
.Append(menu
);
2137 void wxFileHistory::RemoveMenu(wxMenu
*menu
)
2139 m_fileMenus
.DeleteObject(menu
);
2143 void wxFileHistory::Load(const wxConfigBase
& config
)
2145 m_fileHistory
.Clear();
2148 buf
.Printf(wxT("file%d"), 1);
2150 wxString historyFile
;
2151 while ((m_fileHistory
.GetCount() < m_fileMaxFiles
) &&
2152 config
.Read(buf
, &historyFile
) && !historyFile
.empty())
2154 m_fileHistory
.Add(historyFile
);
2156 buf
.Printf(wxT("file%d"), (int)m_fileHistory
.GetCount()+1);
2157 historyFile
= wxEmptyString
;
2163 void wxFileHistory::Save(wxConfigBase
& config
)
2166 for (i
= 0; i
< m_fileMaxFiles
; i
++)
2169 buf
.Printf(wxT("file%d"), (int)i
+1);
2170 if (i
< m_fileHistory
.GetCount())
2171 config
.Write(buf
, wxString(m_fileHistory
[i
]));
2173 config
.Write(buf
, wxEmptyString
);
2176 #endif // wxUSE_CONFIG
2178 void wxFileHistory::AddFilesToMenu()
2180 if ( m_fileHistory
.empty() )
2183 for ( wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2185 node
= node
->GetNext() )
2187 AddFilesToMenu((wxMenu
*) node
->GetData());
2191 void wxFileHistory::AddFilesToMenu(wxMenu
* menu
)
2193 if ( m_fileHistory
.empty() )
2196 if ( menu
->GetMenuItemCount() )
2197 menu
->AppendSeparator();
2199 for ( size_t i
= 0; i
< m_fileHistory
.GetCount(); i
++ )
2201 menu
->Append(m_idBase
+ i
, GetMRUEntryLabel(i
, m_fileHistory
[i
]));
2205 // ----------------------------------------------------------------------------
2206 // Permits compatibility with existing file formats and functions that
2207 // manipulate files directly
2208 // ----------------------------------------------------------------------------
2210 #if wxUSE_STD_IOSTREAM
2212 bool wxTransferFileToStream(const wxString
& filename
, wxSTD ostream
& stream
)
2214 wxFFile
file(filename
, _T("rb"));
2215 if ( !file
.IsOpened() )
2223 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2227 stream
.write(buf
, nRead
);
2231 while ( !file
.Eof() );
2236 bool wxTransferStreamToFile(wxSTD istream
& stream
, const wxString
& filename
)
2238 wxFFile
file(filename
, _T("wb"));
2239 if ( !file
.IsOpened() )
2245 stream
.read(buf
, WXSIZEOF(buf
));
2246 if ( !stream
.bad() ) // fail may be set on EOF, don't use operator!()
2248 if ( !file
.Write(buf
, stream
.gcount()) )
2252 while ( !stream
.eof() );
2257 #else // !wxUSE_STD_IOSTREAM
2259 bool wxTransferFileToStream(const wxString
& filename
, wxOutputStream
& stream
)
2261 wxFFile
file(filename
, _T("rb"));
2262 if ( !file
.IsOpened() )
2270 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2274 stream
.Write(buf
, nRead
);
2278 while ( !file
.Eof() );
2283 bool wxTransferStreamToFile(wxInputStream
& stream
, const wxString
& filename
)
2285 wxFFile
file(filename
, _T("wb"));
2286 if ( !file
.IsOpened() )
2292 stream
.Read(buf
, WXSIZEOF(buf
));
2294 const size_t nRead
= stream
.LastRead();
2303 if ( !file
.Write(buf
, nRead
) )
2310 #endif // wxUSE_STD_IOSTREAM/!wxUSE_STD_IOSTREAM
2312 #endif // wxUSE_DOC_VIEW_ARCHITECTURE