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 defaultDir
= docTemplate
->GetDirectory();
302 if (defaultDir
.IsEmpty())
303 defaultDir
= wxPathOnly(GetFilename());
305 wxString tmp
= wxFileSelector(_("Save As"),
307 wxFileNameFromPath(GetFilename()),
308 docTemplate
->GetDefaultExtension(),
310 wxFD_SAVE
| wxFD_OVERWRITE_PROMPT
,
311 GetDocumentWindow());
316 wxString
fileName(tmp
);
317 wxString path
, name
, ext
;
318 wxSplitPath(fileName
, & path
, & name
, & ext
);
322 fileName
+= wxT(".");
323 fileName
+= docTemplate
->GetDefaultExtension();
326 SetFilename(fileName
);
327 SetTitle(wxFileNameFromPath(fileName
));
329 // Notify the views that the filename has changed
330 wxList::compatibility_iterator node
= m_documentViews
.GetFirst();
333 wxView
*view
= (wxView
*)node
->GetData();
334 view
->OnChangeFilename();
335 node
= node
->GetNext();
338 // Files that were not saved correctly are not added to the FileHistory.
339 if (!OnSaveDocument(m_documentFile
))
342 // A file that doesn't use the default extension of its document template cannot be opened
343 // via the FileHistory, so we do not add it.
344 if (docTemplate
->FileMatchesTemplate(fileName
))
346 GetDocumentManager()->AddFileToHistory(fileName
);
350 // The user will probably not be able to open the file again, so
351 // we could warn about the wrong file-extension here.
356 bool wxDocument::OnSaveDocument(const wxString
& file
)
361 if ( !DoSaveDocument(file
) )
366 SetDocumentSaved(true);
367 #if defined( __WXOSX_MAC__ ) && wxOSX_USE_CARBON
368 wxFileName
fn(file
) ;
369 fn
.MacSetDefaultTypeAndCreator() ;
374 bool wxDocument::OnOpenDocument(const wxString
& file
)
376 if (!OnSaveModified())
379 if ( !DoOpenDocument(file
) )
382 SetFilename(file
, true);
391 #if wxUSE_STD_IOSTREAM
392 wxSTD istream
& wxDocument::LoadObject(wxSTD istream
& stream
)
394 wxInputStream
& wxDocument::LoadObject(wxInputStream
& stream
)
400 #if wxUSE_STD_IOSTREAM
401 wxSTD ostream
& wxDocument::SaveObject(wxSTD ostream
& stream
)
403 wxOutputStream
& wxDocument::SaveObject(wxOutputStream
& stream
)
409 bool wxDocument::Revert()
415 // Get title, or filename if no title, else unnamed
416 #if WXWIN_COMPATIBILITY_2_8
417 bool wxDocument::GetPrintableName(wxString
& buf
) const
419 // this function can not only be overridden by the user code but also
420 // called by it so we need to ensure that we return the same thing as
421 // GetUserReadableName() but we can't call it because this would result in
422 // an infinite recursion, hence we use the helper DoGetUserReadableName()
423 buf
= DoGetUserReadableName();
427 #endif // WXWIN_COMPATIBILITY_2_8
429 wxString
wxDocument::GetUserReadableName() const
431 #if WXWIN_COMPATIBILITY_2_8
432 // we need to call the old virtual function to ensure that the overridden
433 // version of it is still called
435 if ( GetPrintableName(name
) )
437 #endif // WXWIN_COMPATIBILITY_2_8
439 return DoGetUserReadableName();
442 wxString
wxDocument::DoGetUserReadableName() const
444 if ( !m_documentTitle
.empty() )
445 return m_documentTitle
;
447 if ( !m_documentFile
.empty() )
448 return wxFileNameFromPath(m_documentFile
);
453 wxWindow
*wxDocument::GetDocumentWindow() const
455 wxView
*view
= GetFirstView();
457 return view
->GetFrame();
459 return wxTheApp
->GetTopWindow();
462 wxCommandProcessor
*wxDocument::OnCreateCommandProcessor()
464 return new wxCommandProcessor
;
467 // true if safe to close
468 bool wxDocument::OnSaveModified()
472 wxString title
= GetUserReadableName();
475 if (!wxTheApp
->GetAppDisplayName().empty())
476 msgTitle
= wxTheApp
->GetAppDisplayName();
478 msgTitle
= wxString(_("Warning"));
481 prompt
.Printf(_("Do you want to save changes to document %s?"), title
);
482 int res
= wxMessageBox(prompt
, msgTitle
,
483 wxYES_NO
|wxCANCEL
|wxICON_QUESTION
,
484 GetDocumentWindow());
490 else if (res
== wxYES
)
492 else if (res
== wxCANCEL
)
498 bool wxDocument::Draw(wxDC
& WXUNUSED(context
))
503 bool wxDocument::AddView(wxView
*view
)
505 if (!m_documentViews
.Member(view
))
507 m_documentViews
.Append(view
);
513 bool wxDocument::RemoveView(wxView
*view
)
515 (void)m_documentViews
.DeleteObject(view
);
520 bool wxDocument::OnCreate(const wxString
& WXUNUSED(path
), long flags
)
522 if (GetDocumentTemplate()->CreateView(this, flags
))
528 // Called after a view is added or removed.
529 // The default implementation deletes the document if
530 // there are no more views.
531 void wxDocument::OnChangedViewList()
533 if (m_documentViews
.GetCount() == 0)
535 if (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
= (wxDocument
*) NULL
;
646 m_viewFrame
= (wxFrame
*) 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 if (OnClose(deleteWindow
))
706 void wxView::Activate(bool activate
)
708 if (GetDocument() && GetDocumentManager())
710 OnActivateView(activate
, this, GetDocumentManager()->GetCurrentView());
711 GetDocumentManager()->ActivateView(this, activate
);
715 bool wxView::OnClose(bool WXUNUSED(deleteWindow
))
717 return GetDocument() ? GetDocument()->Close() : true;
720 #if wxUSE_PRINTING_ARCHITECTURE
721 wxPrintout
*wxView::OnCreatePrintout()
723 return new wxDocPrintout(this);
725 #endif // wxUSE_PRINTING_ARCHITECTURE
727 // ----------------------------------------------------------------------------
729 // ----------------------------------------------------------------------------
731 wxDocTemplate::wxDocTemplate(wxDocManager
*manager
,
732 const wxString
& descr
,
733 const wxString
& filter
,
736 const wxString
& docTypeName
,
737 const wxString
& viewTypeName
,
738 wxClassInfo
*docClassInfo
,
739 wxClassInfo
*viewClassInfo
,
742 m_documentManager
= manager
;
743 m_description
= descr
;
746 m_fileFilter
= filter
;
748 m_docTypeName
= docTypeName
;
749 m_viewTypeName
= viewTypeName
;
750 m_documentManager
->AssociateTemplate(this);
752 m_docClassInfo
= docClassInfo
;
753 m_viewClassInfo
= viewClassInfo
;
756 wxDocTemplate::~wxDocTemplate()
758 m_documentManager
->DisassociateTemplate(this);
761 // Tries to dynamically construct an object of the right class.
762 wxDocument
*wxDocTemplate::CreateDocument(const wxString
& path
, long flags
)
764 wxDocument
*doc
= DoCreateDocument();
766 return (wxDocument
*) NULL
;
768 if (InitDocument(doc
, path
, flags
))
774 return (wxDocument
*) NULL
;
778 bool wxDocTemplate::InitDocument(wxDocument
* doc
, const wxString
& path
, long flags
)
780 doc
->SetFilename(path
);
781 doc
->SetDocumentTemplate(this);
782 GetDocumentManager()->AddDocument(doc
);
783 doc
->SetCommandProcessor(doc
->OnCreateCommandProcessor());
785 if (doc
->OnCreate(path
, flags
))
789 if (GetDocumentManager()->GetDocuments().Member(doc
))
790 doc
->DeleteAllViews();
795 wxView
*wxDocTemplate::CreateView(wxDocument
*doc
, long flags
)
797 wxView
*view
= DoCreateView();
799 return (wxView
*) NULL
;
801 view
->SetDocument(doc
);
802 if (view
->OnCreate(doc
, flags
))
809 return (wxView
*) NULL
;
813 // The default (very primitive) format detection: check is the extension is
814 // that of the template
815 bool wxDocTemplate::FileMatchesTemplate(const wxString
& path
)
817 wxStringTokenizer
parser (GetFileFilter(), wxT(";"));
818 wxString anything
= wxT ("*");
819 while (parser
.HasMoreTokens())
821 wxString filter
= parser
.GetNextToken();
822 wxString filterExt
= FindExtension (filter
);
823 if ( filter
.IsSameAs (anything
) ||
824 filterExt
.IsSameAs (anything
) ||
825 filterExt
.IsSameAs (FindExtension (path
)) )
828 return GetDefaultExtension().IsSameAs(FindExtension(path
));
831 wxDocument
*wxDocTemplate::DoCreateDocument()
834 return (wxDocument
*) NULL
;
836 return (wxDocument
*)m_docClassInfo
->CreateObject();
839 wxView
*wxDocTemplate::DoCreateView()
841 if (!m_viewClassInfo
)
842 return (wxView
*) NULL
;
844 return (wxView
*)m_viewClassInfo
->CreateObject();
847 // ----------------------------------------------------------------------------
849 // ----------------------------------------------------------------------------
851 BEGIN_EVENT_TABLE(wxDocManager
, wxEvtHandler
)
852 EVT_MENU(wxID_OPEN
, wxDocManager::OnFileOpen
)
853 EVT_MENU(wxID_CLOSE
, wxDocManager::OnFileClose
)
854 EVT_MENU(wxID_CLOSE_ALL
, wxDocManager::OnFileCloseAll
)
855 EVT_MENU(wxID_REVERT
, wxDocManager::OnFileRevert
)
856 EVT_MENU(wxID_NEW
, wxDocManager::OnFileNew
)
857 EVT_MENU(wxID_SAVE
, wxDocManager::OnFileSave
)
858 EVT_MENU(wxID_SAVEAS
, wxDocManager::OnFileSaveAs
)
859 EVT_MENU(wxID_UNDO
, wxDocManager::OnUndo
)
860 EVT_MENU(wxID_REDO
, wxDocManager::OnRedo
)
862 EVT_UPDATE_UI(wxID_OPEN
, wxDocManager::OnUpdateFileOpen
)
863 EVT_UPDATE_UI(wxID_CLOSE
, wxDocManager::OnUpdateFileClose
)
864 EVT_UPDATE_UI(wxID_CLOSE_ALL
, wxDocManager::OnUpdateFileClose
)
865 EVT_UPDATE_UI(wxID_REVERT
, wxDocManager::OnUpdateFileRevert
)
866 EVT_UPDATE_UI(wxID_NEW
, wxDocManager::OnUpdateFileNew
)
867 EVT_UPDATE_UI(wxID_SAVE
, wxDocManager::OnUpdateFileSave
)
868 EVT_UPDATE_UI(wxID_SAVEAS
, wxDocManager::OnUpdateFileSaveAs
)
869 EVT_UPDATE_UI(wxID_UNDO
, wxDocManager::OnUpdateUndo
)
870 EVT_UPDATE_UI(wxID_REDO
, wxDocManager::OnUpdateRedo
)
872 #if wxUSE_PRINTING_ARCHITECTURE
873 EVT_MENU(wxID_PRINT
, wxDocManager::OnPrint
)
874 EVT_MENU(wxID_PREVIEW
, wxDocManager::OnPreview
)
876 EVT_UPDATE_UI(wxID_PRINT
, wxDocManager::OnUpdatePrint
)
877 EVT_UPDATE_UI(wxID_PREVIEW
, wxDocManager::OnUpdatePreview
)
881 wxDocManager
* wxDocManager::sm_docManager
= (wxDocManager
*) NULL
;
883 wxDocManager::wxDocManager(long flags
, bool initialize
)
885 m_defaultDocumentNameCounter
= 1;
887 m_currentView
= (wxView
*) NULL
;
888 m_maxDocsOpen
= 10000;
889 m_fileHistory
= (wxFileHistory
*) NULL
;
892 sm_docManager
= this;
895 wxDocManager::~wxDocManager()
899 delete m_fileHistory
;
900 sm_docManager
= (wxDocManager
*) NULL
;
903 // closes the specified document
904 bool wxDocManager::CloseDocument(wxDocument
* doc
, bool force
)
906 if (doc
->Close() || force
)
908 // Implicitly deletes the document when
909 // the last view is deleted
910 doc
->DeleteAllViews();
912 // Check we're really deleted
913 if (m_docs
.Member(doc
))
921 bool wxDocManager::CloseDocuments(bool force
)
923 wxList::compatibility_iterator node
= m_docs
.GetFirst();
926 wxDocument
*doc
= (wxDocument
*)node
->GetData();
927 wxList::compatibility_iterator next
= node
->GetNext();
929 if (!CloseDocument(doc
, force
))
932 // This assumes that documents are not connected in
933 // any way, i.e. deleting one document does NOT
940 bool wxDocManager::Clear(bool force
)
942 if (!CloseDocuments(force
))
945 m_currentView
= NULL
;
947 wxList::compatibility_iterator node
= m_templates
.GetFirst();
950 wxDocTemplate
*templ
= (wxDocTemplate
*) node
->GetData();
951 wxList::compatibility_iterator next
= node
->GetNext();
958 bool wxDocManager::Initialize()
960 m_fileHistory
= OnCreateFileHistory();
964 wxFileHistory
*wxDocManager::OnCreateFileHistory()
966 return new wxFileHistory
;
969 void wxDocManager::OnFileClose(wxCommandEvent
& WXUNUSED(event
))
971 wxDocument
*doc
= GetCurrentDocument();
976 doc
->DeleteAllViews();
977 if (m_docs
.Member(doc
))
982 void wxDocManager::OnFileCloseAll(wxCommandEvent
& WXUNUSED(event
))
984 CloseDocuments(false);
987 void wxDocManager::OnFileNew(wxCommandEvent
& WXUNUSED(event
))
989 CreateDocument( wxEmptyString
, wxDOC_NEW
);
992 void wxDocManager::OnFileOpen(wxCommandEvent
& WXUNUSED(event
))
994 if ( !CreateDocument( wxEmptyString
, 0) )
1000 void wxDocManager::OnFileRevert(wxCommandEvent
& WXUNUSED(event
))
1002 wxDocument
*doc
= GetCurrentDocument();
1008 void wxDocManager::OnFileSave(wxCommandEvent
& WXUNUSED(event
))
1010 wxDocument
*doc
= GetCurrentDocument();
1016 void wxDocManager::OnFileSaveAs(wxCommandEvent
& WXUNUSED(event
))
1018 wxDocument
*doc
= GetCurrentDocument();
1024 void wxDocManager::OnPrint(wxCommandEvent
& WXUNUSED(event
))
1026 #if wxUSE_PRINTING_ARCHITECTURE
1027 wxView
*view
= GetCurrentView();
1031 wxPrintout
*printout
= view
->OnCreatePrintout();
1035 printer
.Print(view
->GetFrame(), printout
, true);
1039 #endif // wxUSE_PRINTING_ARCHITECTURE
1042 void wxDocManager::OnPreview(wxCommandEvent
& WXUNUSED(event
))
1044 #if wxUSE_PRINTING_ARCHITECTURE
1045 wxView
*view
= GetCurrentView();
1049 wxPrintout
*printout
= view
->OnCreatePrintout();
1052 // Pass two printout objects: for preview, and possible printing.
1053 wxPrintPreviewBase
*preview
= new wxPrintPreview(printout
, view
->OnCreatePrintout());
1054 if ( !preview
->Ok() )
1057 wxMessageBox( _("Sorry, print preview needs a printer to be installed.") );
1061 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, (wxFrame
*)wxTheApp
->GetTopWindow(), _("Print Preview"),
1062 wxPoint(100, 100), wxSize(600, 650));
1063 frame
->Centre(wxBOTH
);
1064 frame
->Initialize();
1067 #endif // wxUSE_PRINTING_ARCHITECTURE
1070 void wxDocManager::OnUndo(wxCommandEvent
& event
)
1072 wxDocument
*doc
= GetCurrentDocument();
1075 if (doc
->GetCommandProcessor())
1076 doc
->GetCommandProcessor()->Undo();
1081 void wxDocManager::OnRedo(wxCommandEvent
& event
)
1083 wxDocument
*doc
= GetCurrentDocument();
1086 if (doc
->GetCommandProcessor())
1087 doc
->GetCommandProcessor()->Redo();
1092 // Handlers for UI update commands
1094 void wxDocManager::OnUpdateFileOpen(wxUpdateUIEvent
& event
)
1096 event
.Enable( true );
1099 void wxDocManager::OnUpdateFileClose(wxUpdateUIEvent
& event
)
1101 wxDocument
*doc
= GetCurrentDocument();
1102 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1105 void wxDocManager::OnUpdateFileRevert(wxUpdateUIEvent
& event
)
1107 wxDocument
*doc
= GetCurrentDocument();
1108 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1111 void wxDocManager::OnUpdateFileNew(wxUpdateUIEvent
& event
)
1113 event
.Enable( true );
1116 void wxDocManager::OnUpdateFileSave(wxUpdateUIEvent
& event
)
1118 wxDocument
*doc
= GetCurrentDocument();
1119 event
.Enable( doc
&& doc
->IsModified() );
1122 void wxDocManager::OnUpdateFileSaveAs(wxUpdateUIEvent
& event
)
1124 wxDocument
*doc
= GetCurrentDocument();
1125 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1128 void wxDocManager::OnUpdateUndo(wxUpdateUIEvent
& event
)
1130 wxDocument
*doc
= GetCurrentDocument();
1132 event
.Enable(false);
1133 else if (!doc
->GetCommandProcessor())
1137 event
.Enable( doc
->GetCommandProcessor()->CanUndo() );
1138 doc
->GetCommandProcessor()->SetMenuStrings();
1142 void wxDocManager::OnUpdateRedo(wxUpdateUIEvent
& event
)
1144 wxDocument
*doc
= GetCurrentDocument();
1146 event
.Enable(false);
1147 else if (!doc
->GetCommandProcessor())
1151 event
.Enable( doc
->GetCommandProcessor()->CanRedo() );
1152 doc
->GetCommandProcessor()->SetMenuStrings();
1156 void wxDocManager::OnUpdatePrint(wxUpdateUIEvent
& event
)
1158 wxDocument
*doc
= GetCurrentDocument();
1159 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1162 void wxDocManager::OnUpdatePreview(wxUpdateUIEvent
& event
)
1164 wxDocument
*doc
= GetCurrentDocument();
1165 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1168 wxView
*wxDocManager::GetCurrentView() const
1171 return m_currentView
;
1172 if (m_docs
.GetCount() == 1)
1174 wxDocument
* doc
= (wxDocument
*) m_docs
.GetFirst()->GetData();
1175 return doc
->GetFirstView();
1177 return (wxView
*) NULL
;
1180 // Extend event processing to search the view's event table
1181 bool wxDocManager::ProcessEvent(wxEvent
& event
)
1183 wxView
* view
= GetCurrentView();
1186 if (view
->ProcessEvent(event
))
1189 return wxEvtHandler::ProcessEvent(event
);
1192 wxDocument
*wxDocManager::CreateDocument(const wxString
& path
, long flags
)
1194 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.GetCount()];
1197 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1199 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Item(i
)->GetData());
1200 if (temp
->IsVisible())
1202 templates
[n
] = temp
;
1209 return (wxDocument
*) NULL
;
1212 wxDocument
* docToClose
= NULL
;
1214 // If we've reached the max number of docs, close the
1216 if ( (int)GetDocuments().GetCount() >= m_maxDocsOpen
)
1218 wxDocument
*doc
= (wxDocument
*)GetDocuments().GetFirst()->GetData();
1222 // New document: user chooses a template, unless there's only one.
1223 if (flags
& wxDOC_NEW
)
1229 if (!CloseDocument(docToClose
, false))
1236 wxDocTemplate
*temp
= templates
[0];
1238 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
1242 newDoc
->SetDocumentName(temp
->GetDocumentName());
1243 newDoc
->SetDocumentTemplate(temp
);
1244 if (!newDoc
->OnNewDocument() )
1246 // Document is implicitly deleted by DeleteAllViews
1247 newDoc
->DeleteAllViews();
1254 wxDocTemplate
*temp
= SelectDocumentType(templates
, n
);
1260 if (!CloseDocument(docToClose
, false))
1266 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
1270 newDoc
->SetDocumentName(temp
->GetDocumentName());
1271 newDoc
->SetDocumentTemplate(temp
);
1272 if (!newDoc
->OnNewDocument() )
1274 // Document is implicitly deleted by DeleteAllViews
1275 newDoc
->DeleteAllViews();
1282 return (wxDocument
*) NULL
;
1285 // Existing document
1286 wxDocTemplate
*temp
;
1288 wxString path2
= path
;
1290 if (flags
& wxDOC_SILENT
)
1292 temp
= FindTemplateForPath(path2
);
1295 // Since we do not add files with non-default extensions to the FileHistory this
1296 // can only happen if the application changes the allowed templates in runtime.
1297 (void)wxMessageBox(_("Sorry, the format for this file is unknown."),
1299 wxOK
| wxICON_EXCLAMATION
, wxFindSuitableParent());
1303 temp
= SelectDocumentPath(templates
, n
, path2
, flags
);
1311 if (!CloseDocument(docToClose
, false))
1317 //see if this file is already open
1318 for (size_t i
= 0; i
< GetDocuments().GetCount(); ++i
)
1320 wxDocument
* currentDoc
= (wxDocument
*)(GetDocuments().Item(i
)->GetData());
1322 //file paths are case-insensitive on Windows
1323 if (path2
.CmpNoCase(currentDoc
->GetFilename()) == 0)
1325 if (path2
.Cmp(currentDoc
->GetFilename()) == 0)
1328 //file already open. Just activate it and return
1329 if (currentDoc
->GetFirstView())
1331 ActivateView(currentDoc
->GetFirstView(), true);
1332 if (currentDoc
->GetDocumentWindow())
1333 currentDoc
->GetDocumentWindow()->SetFocus();
1339 wxDocument
*newDoc
= temp
->CreateDocument(path2
, flags
);
1342 newDoc
->SetDocumentName(temp
->GetDocumentName());
1343 newDoc
->SetDocumentTemplate(temp
);
1344 if (!newDoc
->OnOpenDocument(path2
))
1346 newDoc
->DeleteAllViews();
1347 // delete newDoc; // Implicitly deleted by DeleteAllViews
1348 return (wxDocument
*) NULL
;
1350 // A file that doesn't use the default extension of its document
1351 // template cannot be opened via the FileHistory, so we do not
1353 if (temp
->FileMatchesTemplate(path2
))
1354 AddFileToHistory(path2
);
1359 return (wxDocument
*) NULL
;
1362 wxView
*wxDocManager::CreateView(wxDocument
*doc
, long flags
)
1364 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.GetCount()];
1367 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1369 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Item(i
)->GetData());
1370 if (temp
->IsVisible())
1372 if (temp
->GetDocumentName() == doc
->GetDocumentName())
1374 templates
[n
] = temp
;
1382 return (wxView
*) NULL
;
1386 wxDocTemplate
*temp
= templates
[0];
1388 wxView
*view
= temp
->CreateView(doc
, flags
);
1390 view
->SetViewName(temp
->GetViewName());
1394 wxDocTemplate
*temp
= SelectViewType(templates
, n
);
1398 wxView
*view
= temp
->CreateView(doc
, flags
);
1400 view
->SetViewName(temp
->GetViewName());
1404 return (wxView
*) NULL
;
1407 // Not yet implemented
1408 void wxDocManager::DeleteTemplate(wxDocTemplate
*WXUNUSED(temp
), long WXUNUSED(flags
))
1412 // Not yet implemented
1413 bool wxDocManager::FlushDoc(wxDocument
*WXUNUSED(doc
))
1418 wxDocument
*wxDocManager::GetCurrentDocument() const
1420 wxView
*view
= GetCurrentView();
1422 return view
->GetDocument();
1424 return (wxDocument
*) NULL
;
1427 // Make a default name for a new document
1428 #if WXWIN_COMPATIBILITY_2_8
1429 bool wxDocManager::MakeDefaultName(wxString
& WXUNUSED(name
))
1431 // we consider that this function can only be overridden by the user code,
1432 // not called by it as it only makes sense to call it internally, so we
1433 // don't bother to return anything from here
1436 #endif // WXWIN_COMPATIBILITY_2_8
1438 wxString
wxDocManager::MakeNewDocumentName()
1442 #if WXWIN_COMPATIBILITY_2_8
1443 if ( !MakeDefaultName(name
) )
1444 #endif // WXWIN_COMPATIBILITY_2_8
1446 name
.Printf(_("unnamed%d"), m_defaultDocumentNameCounter
);
1447 m_defaultDocumentNameCounter
++;
1453 // Make a frame title (override this to do something different)
1454 // If docName is empty, a document is not currently active.
1455 wxString
wxDocManager::MakeFrameTitle(wxDocument
* doc
)
1457 wxString appName
= wxTheApp
->GetAppDisplayName();
1463 wxString docName
= doc
->GetUserReadableName();
1464 title
= docName
+ wxString(_(" - ")) + appName
;
1470 // Not yet implemented
1471 wxDocTemplate
*wxDocManager::MatchTemplate(const wxString
& WXUNUSED(path
))
1473 return (wxDocTemplate
*) NULL
;
1476 // File history management
1477 void wxDocManager::AddFileToHistory(const wxString
& file
)
1480 m_fileHistory
->AddFileToHistory(file
);
1483 void wxDocManager::RemoveFileFromHistory(size_t i
)
1486 m_fileHistory
->RemoveFileFromHistory(i
);
1489 wxString
wxDocManager::GetHistoryFile(size_t i
) const
1494 histFile
= m_fileHistory
->GetHistoryFile(i
);
1499 void wxDocManager::FileHistoryUseMenu(wxMenu
*menu
)
1502 m_fileHistory
->UseMenu(menu
);
1505 void wxDocManager::FileHistoryRemoveMenu(wxMenu
*menu
)
1508 m_fileHistory
->RemoveMenu(menu
);
1512 void wxDocManager::FileHistoryLoad(const wxConfigBase
& config
)
1515 m_fileHistory
->Load(config
);
1518 void wxDocManager::FileHistorySave(wxConfigBase
& config
)
1521 m_fileHistory
->Save(config
);
1525 void wxDocManager::FileHistoryAddFilesToMenu(wxMenu
* menu
)
1528 m_fileHistory
->AddFilesToMenu(menu
);
1531 void wxDocManager::FileHistoryAddFilesToMenu()
1534 m_fileHistory
->AddFilesToMenu();
1537 size_t wxDocManager::GetHistoryFilesCount() const
1539 return m_fileHistory
? m_fileHistory
->GetCount() : 0;
1543 // Find out the document template via matching in the document file format
1544 // against that of the template
1545 wxDocTemplate
*wxDocManager::FindTemplateForPath(const wxString
& path
)
1547 wxDocTemplate
*theTemplate
= (wxDocTemplate
*) NULL
;
1549 // Find the template which this extension corresponds to
1550 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1552 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Item(i
)->GetData();
1553 if ( temp
->FileMatchesTemplate(path
) )
1562 // Try to get a more suitable parent frame than the top window,
1563 // for selection dialogs. Otherwise you may get an unexpected
1564 // window being activated when a dialog is shown.
1565 static wxWindow
* wxFindSuitableParent()
1567 wxWindow
* parent
= wxTheApp
->GetTopWindow();
1569 wxWindow
* focusWindow
= wxWindow::FindFocus();
1572 while (focusWindow
&&
1573 !focusWindow
->IsKindOf(CLASSINFO(wxDialog
)) &&
1574 !focusWindow
->IsKindOf(CLASSINFO(wxFrame
)))
1576 focusWindow
= focusWindow
->GetParent();
1579 parent
= focusWindow
;
1584 // Prompts user to open a file, using file specs in templates.
1585 // Must extend the file selector dialog or implement own; OR
1586 // match the extension to the template extension.
1588 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**templates
,
1589 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
1592 int WXUNUSED(noTemplates
),
1595 long WXUNUSED(flags
),
1596 bool WXUNUSED(save
))
1598 // We can only have multiple filters in Windows and GTK
1599 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
1603 for (i
= 0; i
< noTemplates
; i
++)
1605 if (templates
[i
]->IsVisible())
1607 // add a '|' to separate this filter from the previous one
1608 if ( !descrBuf
.empty() )
1609 descrBuf
<< wxT('|');
1611 descrBuf
<< templates
[i
]->GetDescription()
1612 << wxT(" (") << templates
[i
]->GetFileFilter() << wxT(") |")
1613 << templates
[i
]->GetFileFilter();
1617 wxString descrBuf
= wxT("*.*");
1620 int FilterIndex
= -1;
1622 wxWindow
* parent
= wxFindSuitableParent();
1624 wxString pathTmp
= wxFileSelectorEx(_("Open File"),
1632 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)NULL
;
1633 if (!pathTmp
.empty())
1635 if (!wxFileExists(pathTmp
))
1638 if (!wxTheApp
->GetAppDisplayName().empty())
1639 msgTitle
= wxTheApp
->GetAppDisplayName();
1641 msgTitle
= wxString(_("File error"));
1643 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
1646 path
= wxEmptyString
;
1647 return (wxDocTemplate
*) NULL
;
1649 m_lastDirectory
= wxPathOnly(pathTmp
);
1653 // first choose the template using the extension, if this fails (i.e.
1654 // wxFileSelectorEx() didn't fill it), then use the path
1655 if ( FilterIndex
!= -1 )
1656 theTemplate
= templates
[FilterIndex
];
1658 theTemplate
= FindTemplateForPath(path
);
1661 // Since we do not add files with non-default extensions to the FileHistory this
1662 // can only happen if the application changes the allowed templates in runtime.
1663 (void)wxMessageBox(_("Sorry, the format for this file is unknown."),
1665 wxOK
| wxICON_EXCLAMATION
, wxFindSuitableParent());
1670 path
= wxEmptyString
;
1676 wxDocTemplate
*wxDocManager::SelectDocumentType(wxDocTemplate
**templates
,
1677 int noTemplates
, bool sort
)
1679 wxArrayString strings
;
1680 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1684 for (i
= 0; i
< noTemplates
; i
++)
1686 if (templates
[i
]->IsVisible())
1690 for (j
= 0; j
< n
; j
++)
1692 //filter out NOT unique documents + view combinations
1693 if ( templates
[i
]->m_docTypeName
== data
[j
]->m_docTypeName
&&
1694 templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
1701 strings
.Add(templates
[i
]->m_description
);
1703 data
[n
] = templates
[i
];
1711 strings
.Sort(); // ascending sort
1712 // Yes, this will be slow, but template lists
1713 // are typically short.
1715 n
= strings
.Count();
1716 for (i
= 0; i
< n
; i
++)
1718 for (j
= 0; j
< noTemplates
; j
++)
1720 if (strings
[i
] == templates
[j
]->m_description
)
1721 data
[i
] = templates
[j
];
1726 wxDocTemplate
*theTemplate
;
1731 // no visible templates, hence nothing to choose from
1736 // don't propose the user to choose if he heas no choice
1737 theTemplate
= data
[0];
1741 // propose the user to choose one of several
1742 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1744 _("Select a document template"),
1748 wxFindSuitableParent()
1757 wxDocTemplate
*wxDocManager::SelectViewType(wxDocTemplate
**templates
,
1758 int noTemplates
, bool sort
)
1760 wxArrayString strings
;
1761 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1765 for (i
= 0; i
< noTemplates
; i
++)
1767 wxDocTemplate
*templ
= templates
[i
];
1768 if ( templ
->IsVisible() && !templ
->GetViewName().empty() )
1772 for (j
= 0; j
< n
; j
++)
1774 //filter out NOT unique views
1775 if ( templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
)
1781 strings
.Add(templ
->m_viewTypeName
);
1790 strings
.Sort(); // ascending sort
1791 // Yes, this will be slow, but template lists
1792 // are typically short.
1794 n
= strings
.Count();
1795 for (i
= 0; i
< n
; i
++)
1797 for (j
= 0; j
< noTemplates
; j
++)
1799 if (strings
[i
] == templates
[j
]->m_viewTypeName
)
1800 data
[i
] = templates
[j
];
1805 wxDocTemplate
*theTemplate
;
1807 // the same logic as above
1811 theTemplate
= (wxDocTemplate
*)NULL
;
1815 theTemplate
= data
[0];
1819 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1821 _("Select a document view"),
1825 wxFindSuitableParent()
1834 void wxDocManager::AssociateTemplate(wxDocTemplate
*temp
)
1836 if (!m_templates
.Member(temp
))
1837 m_templates
.Append(temp
);
1840 void wxDocManager::DisassociateTemplate(wxDocTemplate
*temp
)
1842 m_templates
.DeleteObject(temp
);
1845 // Add and remove a document from the manager's list
1846 void wxDocManager::AddDocument(wxDocument
*doc
)
1848 if (!m_docs
.Member(doc
))
1852 void wxDocManager::RemoveDocument(wxDocument
*doc
)
1854 m_docs
.DeleteObject(doc
);
1857 // Views or windows should inform the document manager
1858 // when a view is going in or out of focus
1859 void wxDocManager::ActivateView(wxView
*view
, bool activate
)
1863 m_currentView
= view
;
1867 if ( m_currentView
== view
)
1869 // don't keep stale pointer
1870 m_currentView
= (wxView
*) NULL
;
1875 // ----------------------------------------------------------------------------
1876 // Default document child frame
1877 // ----------------------------------------------------------------------------
1879 BEGIN_EVENT_TABLE(wxDocChildFrame
, wxFrame
)
1880 EVT_ACTIVATE(wxDocChildFrame::OnActivate
)
1881 EVT_CLOSE(wxDocChildFrame::OnCloseWindow
)
1884 wxDocChildFrame::wxDocChildFrame(wxDocument
*doc
,
1888 const wxString
& title
,
1892 const wxString
& name
)
1893 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1895 m_childDocument
= doc
;
1898 view
->SetFrame(this);
1901 // Extend event processing to search the view's event table
1902 bool wxDocChildFrame::ProcessEvent(wxEvent
& event
)
1905 m_childView
->Activate(true);
1907 if ( !m_childView
|| ! m_childView
->ProcessEvent(event
) )
1909 // Only hand up to the parent if it's a menu command
1910 if (!event
.IsKindOf(CLASSINFO(wxCommandEvent
)) || !GetParent() || !GetParent()->ProcessEvent(event
))
1911 return wxEvtHandler::ProcessEvent(event
);
1919 void wxDocChildFrame::OnActivate(wxActivateEvent
& event
)
1921 wxFrame::OnActivate(event
);
1924 m_childView
->Activate(event
.GetActive());
1927 void wxDocChildFrame::OnCloseWindow(wxCloseEvent
& event
)
1931 bool ans
= event
.CanVeto()
1932 ? m_childView
->Close(false) // false means don't delete associated window
1933 : true; // Must delete.
1937 m_childView
->Activate(false);
1939 m_childView
= (wxView
*) NULL
;
1940 m_childDocument
= (wxDocument
*) NULL
;
1951 // ----------------------------------------------------------------------------
1952 // Default parent frame
1953 // ----------------------------------------------------------------------------
1955 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1956 EVT_MENU(wxID_EXIT
, wxDocParentFrame::OnExit
)
1957 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE9
, wxDocParentFrame::OnMRUFile
)
1958 EVT_CLOSE(wxDocParentFrame::OnCloseWindow
)
1961 wxDocParentFrame::wxDocParentFrame()
1963 m_docManager
= NULL
;
1966 wxDocParentFrame::wxDocParentFrame(wxDocManager
*manager
,
1969 const wxString
& title
,
1973 const wxString
& name
)
1974 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1976 m_docManager
= manager
;
1979 bool wxDocParentFrame::Create(wxDocManager
*manager
,
1982 const wxString
& title
,
1986 const wxString
& name
)
1988 m_docManager
= manager
;
1989 return base_type::Create(frame
, id
, title
, pos
, size
, style
, name
);
1992 void wxDocParentFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
1997 void wxDocParentFrame::OnMRUFile(wxCommandEvent
& event
)
1999 int n
= event
.GetId() - wxID_FILE1
; // the index in MRU list
2000 wxString
filename(m_docManager
->GetHistoryFile(n
));
2001 if ( !filename
.empty() )
2003 // verify that the file exists before doing anything else
2004 if ( wxFile::Exists(filename
) )
2007 if (!m_docManager
->CreateDocument(filename
, wxDOC_SILENT
))
2009 // remove the file from the MRU list. The user should already be notified.
2010 m_docManager
->RemoveFileFromHistory(n
);
2012 wxLogError(_("The file '%s' couldn't be opened.\nIt has been removed from the most recently used files list."),
2018 // remove the bogus filename from the MRU list and notify the user
2020 m_docManager
->RemoveFileFromHistory(n
);
2022 wxLogError(_("The file '%s' doesn't exist and couldn't be opened.\nIt has been removed from the most recently used files list."),
2028 // Extend event processing to search the view's event table
2029 bool wxDocParentFrame::ProcessEvent(wxEvent
& event
)
2031 // Try the document manager, then do default processing
2032 if (!m_docManager
|| !m_docManager
->ProcessEvent(event
))
2033 return wxEvtHandler::ProcessEvent(event
);
2038 // Define the behaviour for the frame closing
2039 // - must delete all frames except for the main one.
2040 void wxDocParentFrame::OnCloseWindow(wxCloseEvent
& event
)
2042 if (m_docManager
->Clear(!event
.CanVeto()))
2050 #if wxUSE_PRINTING_ARCHITECTURE
2052 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
)
2055 m_printoutView
= view
;
2058 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
2062 // Get the logical pixels per inch of screen and printer
2063 int ppiScreenX
, ppiScreenY
;
2064 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
2065 wxUnusedVar(ppiScreenY
);
2066 int ppiPrinterX
, ppiPrinterY
;
2067 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
2068 wxUnusedVar(ppiPrinterY
);
2070 // This scales the DC so that the printout roughly represents the
2071 // the screen scaling. The text point size _should_ be the right size
2072 // but in fact is too small for some reason. This is a detail that will
2073 // need to be addressed at some point but can be fudged for the
2075 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
2077 // Now we have to check in case our real page size is reduced
2078 // (e.g. because we're drawing to a print preview memory DC)
2079 int pageWidth
, pageHeight
;
2081 dc
->GetSize(&w
, &h
);
2082 GetPageSizePixels(&pageWidth
, &pageHeight
);
2083 wxUnusedVar(pageHeight
);
2085 // If printer pageWidth == current DC width, then this doesn't
2086 // change. But w might be the preview bitmap width, so scale down.
2087 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
2088 dc
->SetUserScale(overallScale
, overallScale
);
2092 m_printoutView
->OnDraw(dc
);
2097 bool wxDocPrintout::HasPage(int pageNum
)
2099 return (pageNum
== 1);
2102 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
2104 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
2110 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
2118 #endif // wxUSE_PRINTING_ARCHITECTURE
2120 // ----------------------------------------------------------------------------
2121 // File history processor
2122 // ----------------------------------------------------------------------------
2124 wxFileHistory::wxFileHistory(size_t maxFiles
, wxWindowID idBase
)
2126 m_fileMaxFiles
= maxFiles
;
2130 wxFileHistory::~wxFileHistory()
2134 void wxFileHistory::AddFileToHistory(const wxString
& file
)
2136 wxFileName
fn(file
);
2139 // Check we don't already have this file
2140 for (i
= 0; i
< m_fileHistory
.GetCount(); i
++)
2142 // we need to do a comparison using wxFileNames because it knows
2143 // how to exactly compare files on the different platforms
2144 // (e.g. handle case [in]sensitive filesystems)
2145 if ( fn
== wxFileName(m_fileHistory
[i
]) )
2147 // we do have it, move it to the top of the history
2148 RemoveFileFromHistory (i
);
2149 AddFileToHistory (file
);
2154 // if we already have a full history, delete the one at the end
2155 if ( m_fileMaxFiles
== m_fileHistory
.GetCount() )
2157 RemoveFileFromHistory (m_fileHistory
.GetCount() - 1);
2158 AddFileToHistory (file
);
2162 // Add to the project file history:
2163 // Move existing files (if any) down so we can insert file at beginning.
2164 if (m_fileHistory
.GetCount() < m_fileMaxFiles
)
2166 wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2169 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2170 if ( m_fileHistory
.IsEmpty() && menu
->GetMenuItemCount() )
2172 menu
->AppendSeparator();
2174 menu
->Append(m_idBase
+m_fileHistory
.GetCount(), _("[EMPTY]"));
2175 node
= node
->GetNext();
2179 m_fileHistory
.Insert(file
, 0);
2181 // this is the directory of the last opened file
2182 wxString pathCurrent
;
2183 wxSplitPath( m_fileHistory
[0], &pathCurrent
, NULL
, NULL
);
2184 for (i
= 0; i
< m_fileHistory
.GetCount(); i
++)
2186 // if in same directory just show the filename; otherwise the full
2188 wxString pathInMenu
, path
, filename
, ext
;
2189 wxSplitPath( m_fileHistory
[i
], &path
, &filename
, &ext
);
2190 if ( path
== pathCurrent
)
2192 pathInMenu
= filename
;
2194 pathInMenu
= pathInMenu
+ wxFILE_SEP_EXT
+ ext
;
2198 // absolute path; could also set relative path
2199 pathInMenu
= m_fileHistory
[i
];
2202 // we need to quote '&' characters which are used for mnemonics
2203 pathInMenu
.Replace(_T("&"), _T("&&"));
2206 buf
.Printf(s_MRUEntryFormat
, i
+ 1, pathInMenu
.c_str());
2208 wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2211 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2212 menu
->SetLabel(m_idBase
+ i
, buf
);
2213 node
= node
->GetNext();
2218 void wxFileHistory::RemoveFileFromHistory(size_t i
)
2220 wxCHECK_RET( i
< m_fileHistory
.GetCount(),
2221 wxT("invalid index in wxFileHistory::RemoveFileFromHistory") );
2223 // delete the element from the array
2224 m_fileHistory
.RemoveAt(i
);
2226 wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2229 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2231 // shuffle filenames up
2233 for ( size_t j
= i
; j
< m_fileHistory
.GetCount(); j
++ )
2235 buf
.Printf(s_MRUEntryFormat
, j
+ 1, m_fileHistory
[j
].c_str());
2236 menu
->SetLabel(m_idBase
+ j
, buf
);
2239 node
= node
->GetNext();
2241 // delete the last menu item which is unused now
2242 wxWindowID lastItemId
= m_idBase
+ wx_truncate_cast(wxWindowID
, m_fileHistory
.GetCount());
2243 if (menu
->FindItem(lastItemId
))
2245 menu
->Delete(lastItemId
);
2248 // delete the last separator too if no more files are left
2249 if ( m_fileHistory
.GetCount() == 0 )
2251 wxMenuItemList::compatibility_iterator nodeLast
= menu
->GetMenuItems().GetLast();
2254 wxMenuItem
*menuItem
= nodeLast
->GetData();
2255 if ( menuItem
->IsSeparator() )
2257 menu
->Delete(menuItem
);
2259 //else: should we search backwards for the last separator?
2261 //else: menu is empty somehow
2266 void wxFileHistory::UseMenu(wxMenu
*menu
)
2268 if (!m_fileMenus
.Member(menu
))
2269 m_fileMenus
.Append(menu
);
2272 void wxFileHistory::RemoveMenu(wxMenu
*menu
)
2274 m_fileMenus
.DeleteObject(menu
);
2278 void wxFileHistory::Load(const wxConfigBase
& config
)
2280 m_fileHistory
.Clear();
2283 buf
.Printf(wxT("file%d"), 1);
2285 wxString historyFile
;
2286 while ((m_fileHistory
.GetCount() < m_fileMaxFiles
) &&
2287 config
.Read(buf
, &historyFile
) && !historyFile
.empty())
2289 m_fileHistory
.Add(historyFile
);
2291 buf
.Printf(wxT("file%d"), (int)m_fileHistory
.GetCount()+1);
2292 historyFile
= wxEmptyString
;
2298 void wxFileHistory::Save(wxConfigBase
& config
)
2301 for (i
= 0; i
< m_fileMaxFiles
; i
++)
2304 buf
.Printf(wxT("file%d"), (int)i
+1);
2305 if (i
< m_fileHistory
.GetCount())
2306 config
.Write(buf
, wxString(m_fileHistory
[i
]));
2308 config
.Write(buf
, wxEmptyString
);
2311 #endif // wxUSE_CONFIG
2313 void wxFileHistory::AddFilesToMenu()
2315 if (m_fileHistory
.GetCount() > 0)
2317 wxList::compatibility_iterator node
= m_fileMenus
.GetFirst();
2320 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2321 if (menu
->GetMenuItemCount())
2323 menu
->AppendSeparator();
2327 for (i
= 0; i
< m_fileHistory
.GetCount(); i
++)
2330 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
].c_str());
2331 menu
->Append(m_idBase
+i
, buf
);
2333 node
= node
->GetNext();
2338 void wxFileHistory::AddFilesToMenu(wxMenu
* menu
)
2340 if (m_fileHistory
.GetCount() > 0)
2342 if (menu
->GetMenuItemCount())
2344 menu
->AppendSeparator();
2348 for (i
= 0; i
< m_fileHistory
.GetCount(); i
++)
2351 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
].c_str());
2352 menu
->Append(m_idBase
+i
, buf
);
2357 // ----------------------------------------------------------------------------
2358 // Permits compatibility with existing file formats and functions that
2359 // manipulate files directly
2360 // ----------------------------------------------------------------------------
2362 #if wxUSE_STD_IOSTREAM
2364 bool wxTransferFileToStream(const wxString
& filename
, wxSTD ostream
& stream
)
2366 wxFFile
file(filename
, _T("rb"));
2367 if ( !file
.IsOpened() )
2375 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2379 stream
.write(buf
, nRead
);
2383 while ( !file
.Eof() );
2388 bool wxTransferStreamToFile(wxSTD istream
& stream
, const wxString
& filename
)
2390 wxFFile
file(filename
, _T("wb"));
2391 if ( !file
.IsOpened() )
2397 stream
.read(buf
, WXSIZEOF(buf
));
2398 if ( !stream
.bad() ) // fail may be set on EOF, don't use operator!()
2400 if ( !file
.Write(buf
, stream
.gcount()) )
2404 while ( !stream
.eof() );
2409 #else // !wxUSE_STD_IOSTREAM
2411 bool wxTransferFileToStream(const wxString
& filename
, wxOutputStream
& stream
)
2413 wxFFile
file(filename
, _T("rb"));
2414 if ( !file
.IsOpened() )
2422 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2426 stream
.Write(buf
, nRead
);
2430 while ( !file
.Eof() );
2435 bool wxTransferStreamToFile(wxInputStream
& stream
, const wxString
& filename
)
2437 wxFFile
file(filename
, _T("wb"));
2438 if ( !file
.IsOpened() )
2444 stream
.Read(buf
, WXSIZEOF(buf
));
2446 const size_t nRead
= stream
.LastRead();
2455 if ( !file
.Write(buf
, nRead
) )
2462 #endif // wxUSE_STD_IOSTREAM/!wxUSE_STD_IOSTREAM
2464 #endif // wxUSE_DOC_VIEW_ARCHITECTURE