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"
69 #include "wx/beforestd.h"
75 #include "wx/afterstd.h"
77 #include "wx/wfstream.h"
80 // ----------------------------------------------------------------------------
82 // ----------------------------------------------------------------------------
84 IMPLEMENT_ABSTRACT_CLASS(wxDocument
, wxEvtHandler
)
85 IMPLEMENT_ABSTRACT_CLASS(wxView
, wxEvtHandler
)
86 IMPLEMENT_ABSTRACT_CLASS(wxDocTemplate
, wxObject
)
87 IMPLEMENT_DYNAMIC_CLASS(wxDocManager
, wxEvtHandler
)
88 IMPLEMENT_CLASS(wxDocChildFrame
, wxFrame
)
89 IMPLEMENT_CLASS(wxDocParentFrame
, wxFrame
)
91 #if wxUSE_PRINTING_ARCHITECTURE
92 IMPLEMENT_DYNAMIC_CLASS(wxDocPrintout
, wxPrintout
)
95 IMPLEMENT_DYNAMIC_CLASS(wxFileHistory
, wxObject
)
97 // ----------------------------------------------------------------------------
98 // function prototypes
99 // ----------------------------------------------------------------------------
101 static wxWindow
* wxFindSuitableParent(void);
103 // ----------------------------------------------------------------------------
105 // ----------------------------------------------------------------------------
107 static const wxChar
*s_MRUEntryFormat
= wxT("&%d %s");
109 // ============================================================================
111 // ============================================================================
113 // ----------------------------------------------------------------------------
115 // ----------------------------------------------------------------------------
117 static wxString
FindExtension(const wxString
& path
)
120 wxSplitPath(path
, NULL
, NULL
, &ext
);
122 // VZ: extensions are considered not case sensitive - is this really a good
124 return ext
.MakeLower();
127 // ----------------------------------------------------------------------------
128 // Definition of wxDocument
129 // ----------------------------------------------------------------------------
131 wxDocument::wxDocument(wxDocument
*parent
)
133 m_documentModified
= false;
134 m_documentParent
= parent
;
135 m_documentTemplate
= (wxDocTemplate
*) NULL
;
136 m_commandProcessor
= (wxCommandProcessor
*) NULL
;
140 bool wxDocument::DeleteContents()
145 wxDocument::~wxDocument()
149 if (m_commandProcessor
)
150 delete m_commandProcessor
;
152 if (GetDocumentManager())
153 GetDocumentManager()->RemoveDocument(this);
155 // Not safe to do here, since it'll invoke virtual view functions
156 // expecting to see valid derived objects: and by the time we get here,
157 // we've called destructors higher up.
161 bool wxDocument::Close()
163 if (OnSaveModified())
164 return OnCloseDocument();
169 bool wxDocument::OnCloseDocument()
171 // Tell all views that we're about to close
178 // Note that this implicitly deletes the document when the last view is
180 bool wxDocument::DeleteAllViews()
182 wxDocManager
* manager
= GetDocumentManager();
184 // first check if all views agree to be closed
185 const wxList::iterator end
= m_documentViews
.end();
186 for ( wxList::iterator i
= m_documentViews
.begin(); i
!= end
; ++i
)
188 wxView
*view
= (wxView
*)*i
;
189 if ( !view
->Close() )
193 // all views agreed to close, now do close them
194 if ( m_documentViews
.empty() )
196 // normally the document would be implicitly deleted when the last view
197 // is, but if don't have any views, do it here instead
198 if ( manager
&& manager
->GetDocuments().Member(this) )
203 // as we delete elements we iterate over, don't use the usual "from
204 // begin to end" loop
207 wxView
*view
= (wxView
*)*m_documentViews
.begin();
209 bool isLastOne
= m_documentViews
.size() == 1;
211 // this always deletes the node implicitly and if this is the last
212 // view also deletes this object itself (also implicitly, great),
213 // so we can't test for m_documentViews.empty() after calling this!
224 wxView
*wxDocument::GetFirstView() const
226 if (m_documentViews
.GetCount() == 0)
227 return (wxView
*) NULL
;
228 return (wxView
*)m_documentViews
.GetFirst()->GetData();
231 wxDocManager
*wxDocument::GetDocumentManager() const
233 return (m_documentTemplate
? m_documentTemplate
->GetDocumentManager() : (wxDocManager
*) NULL
);
236 bool wxDocument::OnNewDocument()
238 if (!OnSaveModified())
241 if (OnCloseDocument()==false) return false;
244 SetDocumentSaved(false);
246 const wxString name
= GetDocumentManager()->MakeNewDocumentName();
248 SetFilename(name
, true);
253 bool wxDocument::Save()
255 if (!IsModified() && m_savedYet
)
258 if ( m_documentFile
.empty() || !m_savedYet
)
261 return OnSaveDocument(m_documentFile
);
264 bool wxDocument::SaveAs()
266 wxDocTemplate
*docTemplate
= GetDocumentTemplate();
270 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
271 wxString filter
= docTemplate
->GetDescription() + wxT(" (") + docTemplate
->GetFileFilter() + wxT(")|") + docTemplate
->GetFileFilter();
273 // Now see if there are some other template with identical view and document
274 // classes, whose filters may also be used.
276 if (docTemplate
->GetViewClassInfo() && docTemplate
->GetDocClassInfo())
278 wxList::compatibility_iterator node
= docTemplate
->GetDocumentManager()->GetTemplates().GetFirst();
281 wxDocTemplate
*t
= (wxDocTemplate
*) node
->GetData();
283 if (t
->IsVisible() && t
!= docTemplate
&&
284 t
->GetViewClassInfo() == docTemplate
->GetViewClassInfo() &&
285 t
->GetDocClassInfo() == docTemplate
->GetDocClassInfo())
287 // add a '|' to separate this filter from the previous one
288 if ( !filter
.empty() )
291 filter
<< t
->GetDescription() << wxT(" (") << t
->GetFileFilter() << wxT(") |")
292 << t
->GetFileFilter();
295 node
= node
->GetNext();
299 wxString filter
= docTemplate
->GetFileFilter() ;
301 wxString tmp
= wxFileSelector(_("Save as"),
302 docTemplate
->GetDirectory(),
303 wxFileNameFromPath(GetFilename()),
304 docTemplate
->GetDefaultExtension(),
306 wxFD_SAVE
| wxFD_OVERWRITE_PROMPT
,
307 GetDocumentWindow());
312 wxString
fileName(tmp
);
313 wxString path
, name
, ext
;
314 wxSplitPath(fileName
, & path
, & name
, & ext
);
318 fileName
+= wxT(".");
319 fileName
+= docTemplate
->GetDefaultExtension();
322 SetFilename(fileName
);
323 SetTitle(wxFileNameFromPath(fileName
));
325 // Notify the views that the filename has changed
326 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
329 wxView
*view
= (wxView
*)node
->GetData();
330 view
->OnChangeFilename();
331 node
= node
->GetNext();
334 // Files that were not saved correctly are not added to the FileHistory.
335 if (!OnSaveDocument(m_documentFile
))
338 // A file that doesn't use the default extension of its document template cannot be opened
339 // via the FileHistory, so we do not add it.
340 if (docTemplate
->FileMatchesTemplate(fileName
))
342 GetDocumentManager()->AddFileToHistory(fileName
);
346 // The user will probably not be able to open the file again, so
347 // we could warn about the wrong file-extension here.
352 bool wxDocument::OnSaveDocument(const wxString
& file
)
357 if ( !DoSaveDocument(file
) )
362 SetDocumentSaved(true);
364 wxFileName
fn(file
) ;
365 fn
.MacSetDefaultTypeAndCreator() ;
370 bool wxDocument::OnOpenDocument(const wxString
& file
)
372 if (!OnSaveModified())
375 if ( !DoOpenDocument(file
) )
378 SetFilename(file
, true);
387 #if wxUSE_STD_IOSTREAM
388 wxSTD istream
& wxDocument::LoadObject(wxSTD istream
& stream
)
390 wxInputStream
& wxDocument::LoadObject(wxInputStream
& stream
)
396 #if wxUSE_STD_IOSTREAM
397 wxSTD ostream
& wxDocument::SaveObject(wxSTD ostream
& stream
)
399 wxOutputStream
& wxDocument::SaveObject(wxOutputStream
& stream
)
405 bool wxDocument::Revert()
411 // Get title, or filename if no title, else unnamed
412 #if WXWIN_COMPATIBILITY_2_8
413 bool wxDocument::GetPrintableName(wxString
& buf
) const
415 // this function can not only be overridden by the user code but also
416 // called by it so we need to ensure that we return the same thing as
417 // GetUserReadableName() but we can't call it because this would result in
418 // an infinite recursion, hence we use the helper DoGetUserReadableName()
419 buf
= DoGetUserReadableName();
423 #endif // WXWIN_COMPATIBILITY_2_8
425 wxString
wxDocument::GetUserReadableName() const
427 #if WXWIN_COMPATIBILITY_2_8
428 // we need to call the old virtual function to ensure that the overridden
429 // version of it is still called
431 if ( GetPrintableName(name
) )
433 #endif // WXWIN_COMPATIBILITY_2_8
435 return DoGetUserReadableName();
438 wxString
wxDocument::DoGetUserReadableName() const
440 if ( !m_documentTitle
.empty() )
441 return m_documentTitle
;
443 if ( !m_documentFile
.empty() )
444 return wxFileNameFromPath(m_documentFile
);
449 wxWindow
*wxDocument::GetDocumentWindow() const
451 wxView
*view
= GetFirstView();
453 return view
->GetFrame();
455 return wxTheApp
->GetTopWindow();
458 wxCommandProcessor
*wxDocument::OnCreateCommandProcessor()
460 return new wxCommandProcessor
;
463 // true if safe to close
464 bool wxDocument::OnSaveModified()
468 wxString title
= GetUserReadableName();
471 if (!wxTheApp
->GetAppName().empty())
472 msgTitle
= wxTheApp
->GetAppName();
474 msgTitle
= wxString(_("Warning"));
477 prompt
.Printf(_("Do you want to save changes to document %s?"), title
);
478 int res
= wxMessageBox(prompt
, msgTitle
,
479 wxYES_NO
|wxCANCEL
|wxICON_QUESTION
,
480 GetDocumentWindow());
486 else if (res
== wxYES
)
488 else if (res
== wxCANCEL
)
494 bool wxDocument::Draw(wxDC
& WXUNUSED(context
))
499 bool wxDocument::AddView(wxView
*view
)
501 if (!m_documentViews
.Member(view
))
503 m_documentViews
.Append(view
);
509 bool wxDocument::RemoveView(wxView
*view
)
511 (void)m_documentViews
.DeleteObject(view
);
516 bool wxDocument::OnCreate(const wxString
& WXUNUSED(path
), long flags
)
518 if (GetDocumentTemplate()->CreateView(this, flags
))
524 // Called after a view is added or removed.
525 // The default implementation deletes the document if
526 // there are no more views.
527 void wxDocument::OnChangedViewList()
529 if (m_documentViews
.GetCount() == 0)
531 if (OnSaveModified())
538 void wxDocument::UpdateAllViews(wxView
*sender
, wxObject
*hint
)
540 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
543 wxView
*view
= (wxView
*)node
->GetData();
545 view
->OnUpdate(sender
, hint
);
546 node
= node
->GetNext();
550 void wxDocument::NotifyClosing()
552 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
555 wxView
*view
= (wxView
*)node
->GetData();
556 view
->OnClosingDocument();
557 node
= node
->GetNext();
561 void wxDocument::SetFilename(const wxString
& filename
, bool notifyViews
)
563 m_documentFile
= filename
;
566 // Notify the views that the filename has changed
567 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
570 wxView
*view
= (wxView
*)node
->GetData();
571 view
->OnChangeFilename();
572 node
= node
->GetNext();
577 bool wxDocument::DoSaveDocument(const wxString
& file
)
580 if (!wxTheApp
->GetAppName().empty())
581 msgTitle
= wxTheApp
->GetAppName();
583 msgTitle
= wxString(_("File error"));
585 #if wxUSE_STD_IOSTREAM
586 wxSTD ofstream
store(file
.mb_str(), wxSTD
ios::binary
);
587 if (store
.fail() || store
.bad())
589 wxFileOutputStream
store(file
);
590 if (store
.GetLastError() != wxSTREAM_NO_ERROR
)
593 (void)wxMessageBox(_("Sorry, could not open this file for saving."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
594 GetDocumentWindow());
598 if (!SaveObject(store
))
600 (void)wxMessageBox(_("Sorry, could not save this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
601 GetDocumentWindow());
609 bool wxDocument::DoOpenDocument(const wxString
& file
)
611 #if wxUSE_STD_IOSTREAM
612 wxSTD ifstream
store(file
.mb_str(), wxSTD
ios::binary
);
613 if (!store
.fail() && !store
.bad())
615 wxFileInputStream
store(file
);
616 if (store
.GetLastError() == wxSTREAM_NO_ERROR
)
619 #if wxUSE_STD_IOSTREAM
621 if ( !!store
|| store
.eof() )
623 int res
= LoadObject(store
).GetLastError();
624 if ( res
== wxSTREAM_NO_ERROR
|| res
== wxSTREAM_EOF
)
629 wxLogError(_("Sorry, could not open this file."));
634 // ----------------------------------------------------------------------------
636 // ----------------------------------------------------------------------------
640 m_viewDocument
= (wxDocument
*) NULL
;
642 m_viewFrame
= (wxFrame
*) NULL
;
647 GetDocumentManager()->ActivateView(this, false);
648 m_viewDocument
->RemoveView(this);
651 // Extend event processing to search the document's event table
652 bool wxView::ProcessEvent(wxEvent
& event
)
654 if ( !GetDocument() || !GetDocument()->ProcessEvent(event
) )
655 return wxEvtHandler::ProcessEvent(event
);
660 void wxView::OnActivateView(bool WXUNUSED(activate
), wxView
*WXUNUSED(activeView
), wxView
*WXUNUSED(deactiveView
))
664 void wxView::OnPrint(wxDC
*dc
, wxObject
*WXUNUSED(info
))
669 void wxView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
))
673 void wxView::OnChangeFilename()
675 // GetFrame can return wxWindow rather than wxTopLevelWindow due to
676 // generic MDI implementation so use SetLabel rather than SetTitle.
677 // It should cause SetTitle() for top level windows.
678 wxWindow
*win
= GetFrame();
681 wxDocument
*doc
= GetDocument();
684 win
->SetLabel(doc
->GetUserReadableName());
687 void wxView::SetDocument(wxDocument
*doc
)
689 m_viewDocument
= doc
;
694 bool wxView::Close(bool deleteWindow
)
696 if (OnClose(deleteWindow
))
702 void wxView::Activate(bool activate
)
704 if (GetDocument() && GetDocumentManager())
706 OnActivateView(activate
, this, GetDocumentManager()->GetCurrentView());
707 GetDocumentManager()->ActivateView(this, activate
);
711 bool wxView::OnClose(bool WXUNUSED(deleteWindow
))
713 return GetDocument() ? GetDocument()->Close() : true;
716 #if wxUSE_PRINTING_ARCHITECTURE
717 wxPrintout
*wxView::OnCreatePrintout()
719 return new wxDocPrintout(this);
721 #endif // wxUSE_PRINTING_ARCHITECTURE
723 // ----------------------------------------------------------------------------
725 // ----------------------------------------------------------------------------
727 wxDocTemplate::wxDocTemplate(wxDocManager
*manager
,
728 const wxString
& descr
,
729 const wxString
& filter
,
732 const wxString
& docTypeName
,
733 const wxString
& viewTypeName
,
734 wxClassInfo
*docClassInfo
,
735 wxClassInfo
*viewClassInfo
,
738 m_documentManager
= manager
;
739 m_description
= descr
;
742 m_fileFilter
= filter
;
744 m_docTypeName
= docTypeName
;
745 m_viewTypeName
= viewTypeName
;
746 m_documentManager
->AssociateTemplate(this);
748 m_docClassInfo
= docClassInfo
;
749 m_viewClassInfo
= viewClassInfo
;
752 wxDocTemplate::~wxDocTemplate()
754 m_documentManager
->DisassociateTemplate(this);
757 // Tries to dynamically construct an object of the right class.
758 wxDocument
*wxDocTemplate::CreateDocument(const wxString
& path
, long flags
)
760 wxDocument
*doc
= DoCreateDocument();
762 return (wxDocument
*) NULL
;
764 if (InitDocument(doc
, path
, flags
))
770 return (wxDocument
*) NULL
;
774 bool wxDocTemplate::InitDocument(wxDocument
* doc
, const wxString
& path
, long flags
)
776 doc
->SetFilename(path
);
777 doc
->SetDocumentTemplate(this);
778 GetDocumentManager()->AddDocument(doc
);
779 doc
->SetCommandProcessor(doc
->OnCreateCommandProcessor());
781 if (doc
->OnCreate(path
, flags
))
785 if (GetDocumentManager()->GetDocuments().Member(doc
))
786 doc
->DeleteAllViews();
791 wxView
*wxDocTemplate::CreateView(wxDocument
*doc
, long flags
)
793 wxView
*view
= DoCreateView();
795 return (wxView
*) NULL
;
797 view
->SetDocument(doc
);
798 if (view
->OnCreate(doc
, flags
))
805 return (wxView
*) NULL
;
809 // The default (very primitive) format detection: check is the extension is
810 // that of the template
811 bool wxDocTemplate::FileMatchesTemplate(const wxString
& path
)
813 wxStringTokenizer
parser (GetFileFilter(), wxT(";"));
814 wxString anything
= wxT ("*");
815 while (parser
.HasMoreTokens())
817 wxString filter
= parser
.GetNextToken();
818 wxString filterExt
= FindExtension (filter
);
819 if ( filter
.IsSameAs (anything
) ||
820 filterExt
.IsSameAs (anything
) ||
821 filterExt
.IsSameAs (FindExtension (path
)) )
824 return GetDefaultExtension().IsSameAs(FindExtension(path
));
827 wxDocument
*wxDocTemplate::DoCreateDocument()
830 return (wxDocument
*) NULL
;
832 return (wxDocument
*)m_docClassInfo
->CreateObject();
835 wxView
*wxDocTemplate::DoCreateView()
837 if (!m_viewClassInfo
)
838 return (wxView
*) NULL
;
840 return (wxView
*)m_viewClassInfo
->CreateObject();
843 // ----------------------------------------------------------------------------
845 // ----------------------------------------------------------------------------
847 BEGIN_EVENT_TABLE(wxDocManager
, wxEvtHandler
)
848 EVT_MENU(wxID_OPEN
, wxDocManager::OnFileOpen
)
849 EVT_MENU(wxID_CLOSE
, wxDocManager::OnFileClose
)
850 EVT_MENU(wxID_CLOSE_ALL
, wxDocManager::OnFileCloseAll
)
851 EVT_MENU(wxID_REVERT
, wxDocManager::OnFileRevert
)
852 EVT_MENU(wxID_NEW
, wxDocManager::OnFileNew
)
853 EVT_MENU(wxID_SAVE
, wxDocManager::OnFileSave
)
854 EVT_MENU(wxID_SAVEAS
, wxDocManager::OnFileSaveAs
)
855 EVT_MENU(wxID_UNDO
, wxDocManager::OnUndo
)
856 EVT_MENU(wxID_REDO
, wxDocManager::OnRedo
)
858 EVT_UPDATE_UI(wxID_OPEN
, wxDocManager::OnUpdateFileOpen
)
859 EVT_UPDATE_UI(wxID_CLOSE
, wxDocManager::OnUpdateFileClose
)
860 EVT_UPDATE_UI(wxID_CLOSE_ALL
, wxDocManager::OnUpdateFileClose
)
861 EVT_UPDATE_UI(wxID_REVERT
, wxDocManager::OnUpdateFileRevert
)
862 EVT_UPDATE_UI(wxID_NEW
, wxDocManager::OnUpdateFileNew
)
863 EVT_UPDATE_UI(wxID_SAVE
, wxDocManager::OnUpdateFileSave
)
864 EVT_UPDATE_UI(wxID_SAVEAS
, wxDocManager::OnUpdateFileSaveAs
)
865 EVT_UPDATE_UI(wxID_UNDO
, wxDocManager::OnUpdateUndo
)
866 EVT_UPDATE_UI(wxID_REDO
, wxDocManager::OnUpdateRedo
)
868 #if wxUSE_PRINTING_ARCHITECTURE
869 EVT_MENU(wxID_PRINT
, wxDocManager::OnPrint
)
870 EVT_MENU(wxID_PREVIEW
, wxDocManager::OnPreview
)
872 EVT_UPDATE_UI(wxID_PRINT
, wxDocManager::OnUpdatePrint
)
873 EVT_UPDATE_UI(wxID_PREVIEW
, wxDocManager::OnUpdatePreview
)
877 wxDocManager
* wxDocManager::sm_docManager
= (wxDocManager
*) NULL
;
879 wxDocManager::wxDocManager(long flags
, bool initialize
)
881 m_defaultDocumentNameCounter
= 1;
883 m_currentView
= (wxView
*) NULL
;
884 m_maxDocsOpen
= 10000;
885 m_fileHistory
= (wxFileHistory
*) NULL
;
888 sm_docManager
= this;
891 wxDocManager::~wxDocManager()
895 delete m_fileHistory
;
896 sm_docManager
= (wxDocManager
*) NULL
;
899 // closes the specified document
900 bool wxDocManager::CloseDocument(wxDocument
* doc
, bool force
)
902 if (doc
->Close() || force
)
904 // Implicitly deletes the document when
905 // the last view is deleted
906 doc
->DeleteAllViews();
908 // Check we're really deleted
909 if (m_docs
.Member(doc
))
917 bool wxDocManager::CloseDocuments(bool force
)
919 wxList::compatibility_iterator node
= m_docs
.GetFirst();
922 wxDocument
*doc
= (wxDocument
*)node
->GetData();
923 wxList::compatibility_iterator next
= node
->GetNext();
925 if (!CloseDocument(doc
, force
))
928 // This assumes that documents are not connected in
929 // any way, i.e. deleting one document does NOT
936 bool wxDocManager::Clear(bool force
)
938 if (!CloseDocuments(force
))
941 m_currentView
= NULL
;
943 wxList::compatibility_iterator node
= m_templates
.GetFirst();
946 wxDocTemplate
*templ
= (wxDocTemplate
*) node
->GetData();
947 wxList::compatibility_iterator next
= node
->GetNext();
954 bool wxDocManager::Initialize()
956 m_fileHistory
= OnCreateFileHistory();
960 wxFileHistory
*wxDocManager::OnCreateFileHistory()
962 return new wxFileHistory
;
965 void wxDocManager::OnFileClose(wxCommandEvent
& WXUNUSED(event
))
967 wxDocument
*doc
= GetCurrentDocument();
972 doc
->DeleteAllViews();
973 if (m_docs
.Member(doc
))
978 void wxDocManager::OnFileCloseAll(wxCommandEvent
& WXUNUSED(event
))
980 CloseDocuments(false);
983 void wxDocManager::OnFileNew(wxCommandEvent
& WXUNUSED(event
))
985 CreateDocument( wxEmptyString
, wxDOC_NEW
);
988 void wxDocManager::OnFileOpen(wxCommandEvent
& WXUNUSED(event
))
990 if ( !CreateDocument( wxEmptyString
, 0) )
996 void wxDocManager::OnFileRevert(wxCommandEvent
& WXUNUSED(event
))
998 wxDocument
*doc
= GetCurrentDocument();
1004 void wxDocManager::OnFileSave(wxCommandEvent
& WXUNUSED(event
))
1006 wxDocument
*doc
= GetCurrentDocument();
1012 void wxDocManager::OnFileSaveAs(wxCommandEvent
& WXUNUSED(event
))
1014 wxDocument
*doc
= GetCurrentDocument();
1020 void wxDocManager::OnPrint(wxCommandEvent
& WXUNUSED(event
))
1022 #if wxUSE_PRINTING_ARCHITECTURE
1023 wxView
*view
= GetCurrentView();
1027 wxPrintout
*printout
= view
->OnCreatePrintout();
1031 printer
.Print(view
->GetFrame(), printout
, true);
1035 #endif // wxUSE_PRINTING_ARCHITECTURE
1038 void wxDocManager::OnPreview(wxCommandEvent
& WXUNUSED(event
))
1040 #if wxUSE_PRINTING_ARCHITECTURE
1041 wxView
*view
= GetCurrentView();
1045 wxPrintout
*printout
= view
->OnCreatePrintout();
1048 // Pass two printout objects: for preview, and possible printing.
1049 wxPrintPreviewBase
*preview
= new wxPrintPreview(printout
, view
->OnCreatePrintout());
1050 if ( !preview
->Ok() )
1053 wxMessageBox( _("Sorry, print preview needs a printer to be installed.") );
1057 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, (wxFrame
*)wxTheApp
->GetTopWindow(), _("Print Preview"),
1058 wxPoint(100, 100), wxSize(600, 650));
1059 frame
->Centre(wxBOTH
);
1060 frame
->Initialize();
1063 #endif // wxUSE_PRINTING_ARCHITECTURE
1066 void wxDocManager::OnUndo(wxCommandEvent
& event
)
1068 wxDocument
*doc
= GetCurrentDocument();
1071 if (doc
->GetCommandProcessor())
1072 doc
->GetCommandProcessor()->Undo();
1077 void wxDocManager::OnRedo(wxCommandEvent
& event
)
1079 wxDocument
*doc
= GetCurrentDocument();
1082 if (doc
->GetCommandProcessor())
1083 doc
->GetCommandProcessor()->Redo();
1088 // Handlers for UI update commands
1090 void wxDocManager::OnUpdateFileOpen(wxUpdateUIEvent
& event
)
1092 event
.Enable( true );
1095 void wxDocManager::OnUpdateFileClose(wxUpdateUIEvent
& event
)
1097 wxDocument
*doc
= GetCurrentDocument();
1098 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1101 void wxDocManager::OnUpdateFileRevert(wxUpdateUIEvent
& event
)
1103 wxDocument
*doc
= GetCurrentDocument();
1104 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1107 void wxDocManager::OnUpdateFileNew(wxUpdateUIEvent
& event
)
1109 event
.Enable( true );
1112 void wxDocManager::OnUpdateFileSave(wxUpdateUIEvent
& event
)
1114 wxDocument
*doc
= GetCurrentDocument();
1115 event
.Enable( doc
&& doc
->IsModified() );
1118 void wxDocManager::OnUpdateFileSaveAs(wxUpdateUIEvent
& event
)
1120 wxDocument
*doc
= GetCurrentDocument();
1121 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1124 void wxDocManager::OnUpdateUndo(wxUpdateUIEvent
& event
)
1126 wxDocument
*doc
= GetCurrentDocument();
1128 event
.Enable(false);
1129 else if (!doc
->GetCommandProcessor())
1133 event
.Enable( doc
->GetCommandProcessor()->CanUndo() );
1134 doc
->GetCommandProcessor()->SetMenuStrings();
1138 void wxDocManager::OnUpdateRedo(wxUpdateUIEvent
& event
)
1140 wxDocument
*doc
= GetCurrentDocument();
1142 event
.Enable(false);
1143 else if (!doc
->GetCommandProcessor())
1147 event
.Enable( doc
->GetCommandProcessor()->CanRedo() );
1148 doc
->GetCommandProcessor()->SetMenuStrings();
1152 void wxDocManager::OnUpdatePrint(wxUpdateUIEvent
& event
)
1154 wxDocument
*doc
= GetCurrentDocument();
1155 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1158 void wxDocManager::OnUpdatePreview(wxUpdateUIEvent
& event
)
1160 wxDocument
*doc
= GetCurrentDocument();
1161 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1164 wxView
*wxDocManager::GetCurrentView() const
1167 return m_currentView
;
1168 if (m_docs
.GetCount() == 1)
1170 wxDocument
* doc
= (wxDocument
*) m_docs
.GetFirst()->GetData();
1171 return doc
->GetFirstView();
1173 return (wxView
*) NULL
;
1176 // Extend event processing to search the view's event table
1177 bool wxDocManager::ProcessEvent(wxEvent
& event
)
1179 wxView
* view
= GetCurrentView();
1182 if (view
->ProcessEvent(event
))
1185 return wxEvtHandler::ProcessEvent(event
);
1188 wxDocument
*wxDocManager::CreateDocument(const wxString
& path
, long flags
)
1190 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.GetCount()];
1193 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1195 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Item(i
)->GetData());
1196 if (temp
->IsVisible())
1198 templates
[n
] = temp
;
1205 return (wxDocument
*) NULL
;
1208 wxDocument
* docToClose
= NULL
;
1210 // If we've reached the max number of docs, close the
1212 if ( (int)GetDocuments().GetCount() >= m_maxDocsOpen
)
1214 wxDocument
*doc
= (wxDocument
*)GetDocuments().GetFirst()->GetData();
1218 // New document: user chooses a template, unless there's only one.
1219 if (flags
& wxDOC_NEW
)
1225 if (!CloseDocument(docToClose
, false))
1232 wxDocTemplate
*temp
= templates
[0];
1234 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
1238 newDoc
->SetDocumentName(temp
->GetDocumentName());
1239 newDoc
->SetDocumentTemplate(temp
);
1240 if (!newDoc
->OnNewDocument() )
1242 // Document is implicitly deleted by DeleteAllViews
1243 newDoc
->DeleteAllViews();
1250 wxDocTemplate
*temp
= SelectDocumentType(templates
, n
);
1256 if (!CloseDocument(docToClose
, false))
1262 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
1266 newDoc
->SetDocumentName(temp
->GetDocumentName());
1267 newDoc
->SetDocumentTemplate(temp
);
1268 if (!newDoc
->OnNewDocument() )
1270 // Document is implicitly deleted by DeleteAllViews
1271 newDoc
->DeleteAllViews();
1278 return (wxDocument
*) NULL
;
1281 // Existing document
1282 wxDocTemplate
*temp
;
1284 wxString path2
= path
;
1286 if (flags
& wxDOC_SILENT
)
1288 temp
= FindTemplateForPath(path2
);
1291 // Since we do not add files with non-default extensions to the FileHistory this
1292 // can only happen if the application changes the allowed templates in runtime.
1293 (void)wxMessageBox(_("Sorry, the format for this file is unknown."),
1295 wxOK
| wxICON_EXCLAMATION
, wxFindSuitableParent());
1299 temp
= SelectDocumentPath(templates
, n
, path2
, flags
);
1307 if (!CloseDocument(docToClose
, false))
1313 //see if this file is already open
1314 for (size_t i
= 0; i
< GetDocuments().GetCount(); ++i
)
1316 wxDocument
* currentDoc
= (wxDocument
*)(GetDocuments().Item(i
)->GetData());
1318 //file paths are case-insensitive on Windows
1319 if (path2
.CmpNoCase(currentDoc
->GetFilename()) == 0)
1321 if (path2
.Cmp(currentDoc
->GetFilename()) == 0)
1324 //file already open. Just activate it and return
1325 if (currentDoc
->GetFirstView())
1327 ActivateView(currentDoc
->GetFirstView(), true);
1328 if (currentDoc
->GetDocumentWindow())
1329 currentDoc
->GetDocumentWindow()->SetFocus();
1335 wxDocument
*newDoc
= temp
->CreateDocument(path2
, flags
);
1338 newDoc
->SetDocumentName(temp
->GetDocumentName());
1339 newDoc
->SetDocumentTemplate(temp
);
1340 if (!newDoc
->OnOpenDocument(path2
))
1342 newDoc
->DeleteAllViews();
1343 // delete newDoc; // Implicitly deleted by DeleteAllViews
1344 return (wxDocument
*) NULL
;
1346 // A file that doesn't use the default extension of its document
1347 // template cannot be opened via the FileHistory, so we do not
1349 if (temp
->FileMatchesTemplate(path2
))
1350 AddFileToHistory(path2
);
1355 return (wxDocument
*) NULL
;
1358 wxView
*wxDocManager::CreateView(wxDocument
*doc
, long flags
)
1360 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.GetCount()];
1363 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1365 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Item(i
)->GetData());
1366 if (temp
->IsVisible())
1368 if (temp
->GetDocumentName() == doc
->GetDocumentName())
1370 templates
[n
] = temp
;
1378 return (wxView
*) NULL
;
1382 wxDocTemplate
*temp
= templates
[0];
1384 wxView
*view
= temp
->CreateView(doc
, flags
);
1386 view
->SetViewName(temp
->GetViewName());
1390 wxDocTemplate
*temp
= SelectViewType(templates
, n
);
1394 wxView
*view
= temp
->CreateView(doc
, flags
);
1396 view
->SetViewName(temp
->GetViewName());
1400 return (wxView
*) NULL
;
1403 // Not yet implemented
1404 void wxDocManager::DeleteTemplate(wxDocTemplate
*WXUNUSED(temp
), long WXUNUSED(flags
))
1408 // Not yet implemented
1409 bool wxDocManager::FlushDoc(wxDocument
*WXUNUSED(doc
))
1414 wxDocument
*wxDocManager::GetCurrentDocument() const
1416 wxView
*view
= GetCurrentView();
1418 return view
->GetDocument();
1420 return (wxDocument
*) NULL
;
1423 // Make a default name for a new document
1424 #if WXWIN_COMPATIBILITY_2_8
1425 bool wxDocManager::MakeDefaultName(wxString
& WXUNUSED(name
))
1427 // we consider that this function can only be overridden by the user code,
1428 // not called by it as it only makes sense to call it internally, so we
1429 // don't bother to return anything from here
1432 #endif // WXWIN_COMPATIBILITY_2_8
1434 wxString
wxDocManager::MakeNewDocumentName()
1438 #if WXWIN_COMPATIBILITY_2_8
1439 if ( !MakeDefaultName(name
) )
1440 #endif // WXWIN_COMPATIBILITY_2_8
1442 name
.Printf(_("unnamed%d"), m_defaultDocumentNameCounter
);
1443 m_defaultDocumentNameCounter
++;
1449 // Make a frame title (override this to do something different)
1450 // If docName is empty, a document is not currently active.
1451 wxString
wxDocManager::MakeFrameTitle(wxDocument
* doc
)
1453 wxString appName
= wxTheApp
->GetAppName();
1459 wxString docName
= doc
->GetUserReadableName();
1460 title
= docName
+ wxString(_(" - ")) + appName
;
1466 // Not yet implemented
1467 wxDocTemplate
*wxDocManager::MatchTemplate(const wxString
& WXUNUSED(path
))
1469 return (wxDocTemplate
*) NULL
;
1472 // File history management
1473 void wxDocManager::AddFileToHistory(const wxString
& file
)
1476 m_fileHistory
->AddFileToHistory(file
);
1479 void wxDocManager::RemoveFileFromHistory(size_t i
)
1482 m_fileHistory
->RemoveFileFromHistory(i
);
1485 wxString
wxDocManager::GetHistoryFile(size_t i
) const
1490 histFile
= m_fileHistory
->GetHistoryFile(i
);
1495 void wxDocManager::FileHistoryUseMenu(wxMenu
*menu
)
1498 m_fileHistory
->UseMenu(menu
);
1501 void wxDocManager::FileHistoryRemoveMenu(wxMenu
*menu
)
1504 m_fileHistory
->RemoveMenu(menu
);
1508 void wxDocManager::FileHistoryLoad(wxConfigBase
& config
)
1511 m_fileHistory
->Load(config
);
1514 void wxDocManager::FileHistorySave(wxConfigBase
& config
)
1517 m_fileHistory
->Save(config
);
1521 void wxDocManager::FileHistoryAddFilesToMenu(wxMenu
* menu
)
1524 m_fileHistory
->AddFilesToMenu(menu
);
1527 void wxDocManager::FileHistoryAddFilesToMenu()
1530 m_fileHistory
->AddFilesToMenu();
1533 size_t wxDocManager::GetHistoryFilesCount() const
1535 return m_fileHistory
? m_fileHistory
->GetCount() : 0;
1539 // Find out the document template via matching in the document file format
1540 // against that of the template
1541 wxDocTemplate
*wxDocManager::FindTemplateForPath(const wxString
& path
)
1543 wxDocTemplate
*theTemplate
= (wxDocTemplate
*) NULL
;
1545 // Find the template which this extension corresponds to
1546 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1548 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Item(i
)->GetData();
1549 if ( temp
->FileMatchesTemplate(path
) )
1558 // Try to get a more suitable parent frame than the top window,
1559 // for selection dialogs. Otherwise you may get an unexpected
1560 // window being activated when a dialog is shown.
1561 static wxWindow
* wxFindSuitableParent()
1563 wxWindow
* parent
= wxTheApp
->GetTopWindow();
1565 wxWindow
* focusWindow
= wxWindow::FindFocus();
1568 while (focusWindow
&&
1569 !focusWindow
->IsKindOf(CLASSINFO(wxDialog
)) &&
1570 !focusWindow
->IsKindOf(CLASSINFO(wxFrame
)))
1572 focusWindow
= focusWindow
->GetParent();
1575 parent
= focusWindow
;
1580 // Prompts user to open a file, using file specs in templates.
1581 // Must extend the file selector dialog or implement own; OR
1582 // match the extension to the template extension.
1584 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**templates
,
1585 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
1588 int WXUNUSED(noTemplates
),
1591 long WXUNUSED(flags
),
1592 bool WXUNUSED(save
))
1594 // We can only have multiple filters in Windows and GTK
1595 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
1599 for (i
= 0; i
< noTemplates
; i
++)
1601 if (templates
[i
]->IsVisible())
1603 // add a '|' to separate this filter from the previous one
1604 if ( !descrBuf
.empty() )
1605 descrBuf
<< wxT('|');
1607 descrBuf
<< templates
[i
]->GetDescription()
1608 << wxT(" (") << templates
[i
]->GetFileFilter() << wxT(") |")
1609 << templates
[i
]->GetFileFilter();
1613 wxString descrBuf
= wxT("*.*");
1616 int FilterIndex
= -1;
1618 wxWindow
* parent
= wxFindSuitableParent();
1620 wxString pathTmp
= wxFileSelectorEx(_("Select a file"),
1628 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)NULL
;
1629 if (!pathTmp
.empty())
1631 if (!wxFileExists(pathTmp
))
1634 if (!wxTheApp
->GetAppName().empty())
1635 msgTitle
= wxTheApp
->GetAppName();
1637 msgTitle
= wxString(_("File error"));
1639 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
1642 path
= wxEmptyString
;
1643 return (wxDocTemplate
*) NULL
;
1645 m_lastDirectory
= wxPathOnly(pathTmp
);
1649 // first choose the template using the extension, if this fails (i.e.
1650 // wxFileSelectorEx() didn't fill it), then use the path
1651 if ( FilterIndex
!= -1 )
1652 theTemplate
= templates
[FilterIndex
];
1654 theTemplate
= FindTemplateForPath(path
);
1657 // Since we do not add files with non-default extensions to the FileHistory this
1658 // can only happen if the application changes the allowed templates in runtime.
1659 (void)wxMessageBox(_("Sorry, the format for this file is unknown."),
1661 wxOK
| wxICON_EXCLAMATION
, wxFindSuitableParent());
1666 path
= wxEmptyString
;
1672 wxDocTemplate
*wxDocManager::SelectDocumentType(wxDocTemplate
**templates
,
1673 int noTemplates
, bool sort
)
1675 wxArrayString strings
;
1676 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1680 for (i
= 0; i
< noTemplates
; i
++)
1682 if (templates
[i
]->IsVisible())
1686 for (j
= 0; j
< n
; j
++)
1688 //filter out NOT unique documents + view combinations
1689 if ( templates
[i
]->m_docTypeName
== data
[j
]->m_docTypeName
&&
1690 templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
1697 strings
.Add(templates
[i
]->m_description
);
1699 data
[n
] = templates
[i
];
1707 strings
.Sort(); // ascending sort
1708 // Yes, this will be slow, but template lists
1709 // are typically short.
1711 n
= strings
.Count();
1712 for (i
= 0; i
< n
; i
++)
1714 for (j
= 0; j
< noTemplates
; j
++)
1716 if (strings
[i
] == templates
[j
]->m_description
)
1717 data
[i
] = templates
[j
];
1722 wxDocTemplate
*theTemplate
;
1727 // no visible templates, hence nothing to choose from
1732 // don't propose the user to choose if he heas no choice
1733 theTemplate
= data
[0];
1737 // propose the user to choose one of several
1738 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1740 _("Select a document template"),
1744 wxFindSuitableParent()
1753 wxDocTemplate
*wxDocManager::SelectViewType(wxDocTemplate
**templates
,
1754 int noTemplates
, bool sort
)
1756 wxArrayString strings
;
1757 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1761 for (i
= 0; i
< noTemplates
; i
++)
1763 wxDocTemplate
*templ
= templates
[i
];
1764 if ( templ
->IsVisible() && !templ
->GetViewName().empty() )
1768 for (j
= 0; j
< n
; j
++)
1770 //filter out NOT unique views
1771 if ( templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
)
1777 strings
.Add(templ
->m_viewTypeName
);
1786 strings
.Sort(); // ascending sort
1787 // Yes, this will be slow, but template lists
1788 // are typically short.
1790 n
= strings
.Count();
1791 for (i
= 0; i
< n
; i
++)
1793 for (j
= 0; j
< noTemplates
; j
++)
1795 if (strings
[i
] == templates
[j
]->m_viewTypeName
)
1796 data
[i
] = templates
[j
];
1801 wxDocTemplate
*theTemplate
;
1803 // the same logic as above
1807 theTemplate
= (wxDocTemplate
*)NULL
;
1811 theTemplate
= data
[0];
1815 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1817 _("Select a document view"),
1821 wxFindSuitableParent()
1830 void wxDocManager::AssociateTemplate(wxDocTemplate
*temp
)
1832 if (!m_templates
.Member(temp
))
1833 m_templates
.Append(temp
);
1836 void wxDocManager::DisassociateTemplate(wxDocTemplate
*temp
)
1838 m_templates
.DeleteObject(temp
);
1841 // Add and remove a document from the manager's list
1842 void wxDocManager::AddDocument(wxDocument
*doc
)
1844 if (!m_docs
.Member(doc
))
1848 void wxDocManager::RemoveDocument(wxDocument
*doc
)
1850 m_docs
.DeleteObject(doc
);
1853 // Views or windows should inform the document manager
1854 // when a view is going in or out of focus
1855 void wxDocManager::ActivateView(wxView
*view
, bool activate
)
1859 m_currentView
= view
;
1863 if ( m_currentView
== view
)
1865 // don't keep stale pointer
1866 m_currentView
= (wxView
*) NULL
;
1871 // ----------------------------------------------------------------------------
1872 // Default document child frame
1873 // ----------------------------------------------------------------------------
1875 BEGIN_EVENT_TABLE(wxDocChildFrame
, wxFrame
)
1876 EVT_ACTIVATE(wxDocChildFrame::OnActivate
)
1877 EVT_CLOSE(wxDocChildFrame::OnCloseWindow
)
1880 wxDocChildFrame::wxDocChildFrame(wxDocument
*doc
,
1884 const wxString
& title
,
1888 const wxString
& name
)
1889 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1891 m_childDocument
= doc
;
1894 view
->SetFrame(this);
1897 // Extend event processing to search the view's event table
1898 bool wxDocChildFrame::ProcessEvent(wxEvent
& event
)
1901 m_childView
->Activate(true);
1903 if ( !m_childView
|| ! m_childView
->ProcessEvent(event
) )
1905 // Only hand up to the parent if it's a menu command
1906 if (!event
.IsKindOf(CLASSINFO(wxCommandEvent
)) || !GetParent() || !GetParent()->ProcessEvent(event
))
1907 return wxEvtHandler::ProcessEvent(event
);
1915 void wxDocChildFrame::OnActivate(wxActivateEvent
& event
)
1917 wxFrame::OnActivate(event
);
1920 m_childView
->Activate(event
.GetActive());
1923 void wxDocChildFrame::OnCloseWindow(wxCloseEvent
& event
)
1927 bool ans
= event
.CanVeto()
1928 ? m_childView
->Close(false) // false means don't delete associated window
1929 : true; // Must delete.
1933 m_childView
->Activate(false);
1935 m_childView
= (wxView
*) NULL
;
1936 m_childDocument
= (wxDocument
*) NULL
;
1947 // ----------------------------------------------------------------------------
1948 // Default parent frame
1949 // ----------------------------------------------------------------------------
1951 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1952 EVT_MENU(wxID_EXIT
, wxDocParentFrame::OnExit
)
1953 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE9
, wxDocParentFrame::OnMRUFile
)
1954 EVT_CLOSE(wxDocParentFrame::OnCloseWindow
)
1957 wxDocParentFrame::wxDocParentFrame()
1959 m_docManager
= NULL
;
1962 wxDocParentFrame::wxDocParentFrame(wxDocManager
*manager
,
1965 const wxString
& title
,
1969 const wxString
& name
)
1970 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1972 m_docManager
= manager
;
1975 bool wxDocParentFrame::Create(wxDocManager
*manager
,
1978 const wxString
& title
,
1982 const wxString
& name
)
1984 m_docManager
= manager
;
1985 return base_type::Create(frame
, id
, title
, pos
, size
, style
, name
);
1988 void wxDocParentFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
1993 void wxDocParentFrame::OnMRUFile(wxCommandEvent
& event
)
1995 int n
= event
.GetId() - wxID_FILE1
; // the index in MRU list
1996 wxString
filename(m_docManager
->GetHistoryFile(n
));
1997 if ( !filename
.empty() )
1999 // verify that the file exists before doing anything else
2000 if ( wxFile::Exists(filename
) )
2003 if (!m_docManager
->CreateDocument(filename
, wxDOC_SILENT
))
2005 // remove the file from the MRU list. The user should already be notified.
2006 m_docManager
->RemoveFileFromHistory(n
);
2008 wxLogError(_("The file '%s' couldn't be opened.\nIt has been removed from the most recently used files list."),
2014 // remove the bogus filename from the MRU list and notify the user
2016 m_docManager
->RemoveFileFromHistory(n
);
2018 wxLogError(_("The file '%s' doesn't exist and couldn't be opened.\nIt has been removed from the most recently used files list."),
2024 // Extend event processing to search the view's event table
2025 bool wxDocParentFrame::ProcessEvent(wxEvent
& event
)
2027 // Try the document manager, then do default processing
2028 if (!m_docManager
|| !m_docManager
->ProcessEvent(event
))
2029 return wxEvtHandler::ProcessEvent(event
);
2034 // Define the behaviour for the frame closing
2035 // - must delete all frames except for the main one.
2036 void wxDocParentFrame::OnCloseWindow(wxCloseEvent
& event
)
2038 if (m_docManager
->Clear(!event
.CanVeto()))
2046 #if wxUSE_PRINTING_ARCHITECTURE
2048 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
)
2051 m_printoutView
= view
;
2054 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
2058 // Get the logical pixels per inch of screen and printer
2059 int ppiScreenX
, ppiScreenY
;
2060 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
2061 wxUnusedVar(ppiScreenY
);
2062 int ppiPrinterX
, ppiPrinterY
;
2063 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
2064 wxUnusedVar(ppiPrinterY
);
2066 // This scales the DC so that the printout roughly represents the
2067 // the screen scaling. The text point size _should_ be the right size
2068 // but in fact is too small for some reason. This is a detail that will
2069 // need to be addressed at some point but can be fudged for the
2071 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
2073 // Now we have to check in case our real page size is reduced
2074 // (e.g. because we're drawing to a print preview memory DC)
2075 int pageWidth
, pageHeight
;
2077 dc
->GetSize(&w
, &h
);
2078 GetPageSizePixels(&pageWidth
, &pageHeight
);
2079 wxUnusedVar(pageHeight
);
2081 // If printer pageWidth == current DC width, then this doesn't
2082 // change. But w might be the preview bitmap width, so scale down.
2083 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
2084 dc
->SetUserScale(overallScale
, overallScale
);
2088 m_printoutView
->OnDraw(dc
);
2093 bool wxDocPrintout::HasPage(int pageNum
)
2095 return (pageNum
== 1);
2098 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
2100 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
2106 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
2114 #endif // wxUSE_PRINTING_ARCHITECTURE
2116 // ----------------------------------------------------------------------------
2117 // File history processor
2118 // ----------------------------------------------------------------------------
2120 wxFileHistory::wxFileHistory(size_t maxFiles
, wxWindowID idBase
)
2122 m_fileMaxFiles
= maxFiles
;
2126 wxFileHistory::~wxFileHistory()
2130 void wxFileHistory::AddFileToHistory(const wxString
& file
)
2132 wxFileName
fn(file
);
2135 // Check we don't already have this file
2136 for (i
= 0; i
< m_fileHistory
.GetCount(); i
++)
2138 // we need to do a comparison using wxFileNames because it knows
2139 // how to exactly compare files on the different platforms
2140 // (e.g. handle case [in]sensitive filesystems)
2141 if ( fn
== wxFileName(m_fileHistory
[i
]) )
2143 // we do have it, move it to the top of the history
2144 RemoveFileFromHistory (i
);
2145 AddFileToHistory (file
);
2150 // if we already have a full history, delete the one at the end
2151 if ( m_fileMaxFiles
== m_fileHistory
.GetCount() )
2153 RemoveFileFromHistory (m_fileHistory
.GetCount() - 1);
2154 AddFileToHistory (file
);
2158 // Add to the project file history:
2159 // Move existing files (if any) down so we can insert file at beginning.
2160 if (m_fileHistory
.GetCount() < m_fileMaxFiles
)
2162 wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2165 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2166 if ( m_fileHistory
.IsEmpty() && menu
->GetMenuItemCount() )
2168 menu
->AppendSeparator();
2170 menu
->Append(m_idBase
+m_fileHistory
.GetCount(), _("[EMPTY]"));
2171 node
= node
->GetNext();
2175 m_fileHistory
.Insert(file
, 0);
2177 // this is the directory of the last opened file
2178 wxString pathCurrent
;
2179 wxSplitPath( m_fileHistory
[0], &pathCurrent
, NULL
, NULL
);
2180 for (i
= 0; i
< m_fileHistory
.GetCount(); i
++)
2182 // if in same directory just show the filename; otherwise the full
2184 wxString pathInMenu
, path
, filename
, ext
;
2185 wxSplitPath( m_fileHistory
[i
], &path
, &filename
, &ext
);
2186 if ( path
== pathCurrent
)
2188 pathInMenu
= filename
;
2190 pathInMenu
= pathInMenu
+ wxFILE_SEP_EXT
+ ext
;
2194 // absolute path; could also set relative path
2195 pathInMenu
= m_fileHistory
[i
];
2198 // we need to quote '&' characters which are used for mnemonics
2199 pathInMenu
.Replace(_T("&"), _T("&&"));
2202 buf
.Printf(s_MRUEntryFormat
, i
+ 1, pathInMenu
.c_str());
2204 wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2207 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2208 menu
->SetLabel(m_idBase
+ i
, buf
);
2209 node
= node
->GetNext();
2214 void wxFileHistory::RemoveFileFromHistory(size_t i
)
2216 wxCHECK_RET( i
< m_fileHistory
.GetCount(),
2217 wxT("invalid index in wxFileHistory::RemoveFileFromHistory") );
2219 // delete the element from the array
2220 m_fileHistory
.RemoveAt(i
);
2222 wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2225 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2227 // shuffle filenames up
2229 for ( size_t j
= i
; j
< m_fileHistory
.GetCount(); j
++ )
2231 buf
.Printf(s_MRUEntryFormat
, j
+ 1, m_fileHistory
[j
].c_str());
2232 menu
->SetLabel(m_idBase
+ j
, buf
);
2235 node
= node
->GetNext();
2237 // delete the last menu item which is unused now
2238 wxWindowID lastItemId
= m_idBase
+ wx_truncate_cast(wxWindowID
, m_fileHistory
.GetCount());
2239 if (menu
->FindItem(lastItemId
))
2241 menu
->Delete(lastItemId
);
2244 // delete the last separator too if no more files are left
2245 if ( m_fileHistory
.GetCount() == 0 )
2247 wxMenuItemList::compatibility_iterator nodeLast
= menu
->GetMenuItems().GetLast();
2250 wxMenuItem
*menuItem
= nodeLast
->GetData();
2251 if ( menuItem
->IsSeparator() )
2253 menu
->Delete(menuItem
);
2255 //else: should we search backwards for the last separator?
2257 //else: menu is empty somehow
2262 void wxFileHistory::UseMenu(wxMenu
*menu
)
2264 if (!m_fileMenus
.Member(menu
))
2265 m_fileMenus
.Append(menu
);
2268 void wxFileHistory::RemoveMenu(wxMenu
*menu
)
2270 m_fileMenus
.DeleteObject(menu
);
2274 void wxFileHistory::Load(wxConfigBase
& config
)
2276 m_fileHistory
.Clear();
2279 buf
.Printf(wxT("file%d"), 1);
2281 wxString historyFile
;
2282 while ((m_fileHistory
.GetCount() < m_fileMaxFiles
) &&
2283 config
.Read(buf
, &historyFile
) && !historyFile
.empty())
2285 m_fileHistory
.Add(historyFile
);
2287 buf
.Printf(wxT("file%d"), (int)m_fileHistory
.GetCount()+1);
2288 historyFile
= wxEmptyString
;
2294 void wxFileHistory::Save(wxConfigBase
& config
)
2297 for (i
= 0; i
< m_fileMaxFiles
; i
++)
2300 buf
.Printf(wxT("file%d"), (int)i
+1);
2301 if (i
< m_fileHistory
.GetCount())
2302 config
.Write(buf
, wxString(m_fileHistory
[i
]));
2304 config
.Write(buf
, wxEmptyString
);
2307 #endif // wxUSE_CONFIG
2309 void wxFileHistory::AddFilesToMenu()
2311 if (m_fileHistory
.GetCount() > 0)
2313 wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2316 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2317 if (menu
->GetMenuItemCount())
2319 menu
->AppendSeparator();
2323 for (i
= 0; i
< m_fileHistory
.GetCount(); i
++)
2326 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
].c_str());
2327 menu
->Append(m_idBase
+i
, buf
);
2329 node
= node
->GetNext();
2334 void wxFileHistory::AddFilesToMenu(wxMenu
* menu
)
2336 if (m_fileHistory
.GetCount() > 0)
2338 if (menu
->GetMenuItemCount())
2340 menu
->AppendSeparator();
2344 for (i
= 0; i
< m_fileHistory
.GetCount(); i
++)
2347 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
].c_str());
2348 menu
->Append(m_idBase
+i
, buf
);
2353 // ----------------------------------------------------------------------------
2354 // Permits compatibility with existing file formats and functions that
2355 // manipulate files directly
2356 // ----------------------------------------------------------------------------
2358 #if wxUSE_STD_IOSTREAM
2360 bool wxTransferFileToStream(const wxString
& filename
, wxSTD ostream
& stream
)
2362 wxFFile
file(filename
, _T("rb"));
2363 if ( !file
.IsOpened() )
2371 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2375 stream
.write(buf
, nRead
);
2379 while ( !file
.Eof() );
2384 bool wxTransferStreamToFile(wxSTD istream
& stream
, const wxString
& filename
)
2386 wxFFile
file(filename
, _T("wb"));
2387 if ( !file
.IsOpened() )
2393 stream
.read(buf
, WXSIZEOF(buf
));
2394 if ( !stream
.bad() ) // fail may be set on EOF, don't use operator!()
2396 if ( !file
.Write(buf
, stream
.gcount()) )
2400 while ( !stream
.eof() );
2405 #else // !wxUSE_STD_IOSTREAM
2407 bool wxTransferFileToStream(const wxString
& filename
, wxOutputStream
& stream
)
2409 wxFFile
file(filename
, _T("rb"));
2410 if ( !file
.IsOpened() )
2418 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2422 stream
.Write(buf
, nRead
);
2426 while ( !file
.Eof() );
2431 bool wxTransferStreamToFile(wxInputStream
& stream
, const wxString
& filename
)
2433 wxFFile
file(filename
, _T("wb"));
2434 if ( !file
.IsOpened() )
2440 stream
.Read(buf
, WXSIZEOF(buf
));
2442 const size_t nRead
= stream
.LastRead();
2443 if ( !nRead
|| !file
.Write(buf
, nRead
) )
2446 while ( !stream
.Eof() );
2451 #endif // wxUSE_STD_IOSTREAM/!wxUSE_STD_IOSTREAM
2453 #endif // wxUSE_DOC_VIEW_ARCHITECTURE