1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/docview.cpp
3 // Purpose: Document/view classes
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "docview.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
31 #if wxUSE_DOC_VIEW_ARCHITECTURE
34 #include "wx/string.h"
38 #include "wx/dialog.h"
41 #include "wx/filedlg.h"
49 #include "wx/filename.h"
56 #if wxUSE_PRINTING_ARCHITECTURE
57 #include "wx/prntbase.h"
58 #include "wx/printdlg.h"
61 #include "wx/msgdlg.h"
62 #include "wx/choicdlg.h"
63 #include "wx/docview.h"
64 #include "wx/confbase.h"
66 #include "wx/cmdproc.h"
71 #if wxUSE_STD_IOSTREAM
72 #include "wx/ioswrap.h"
79 #include "wx/wfstream.h"
82 // ----------------------------------------------------------------------------
84 // ----------------------------------------------------------------------------
86 IMPLEMENT_ABSTRACT_CLASS(wxDocument
, wxEvtHandler
)
87 IMPLEMENT_ABSTRACT_CLASS(wxView
, wxEvtHandler
)
88 IMPLEMENT_ABSTRACT_CLASS(wxDocTemplate
, wxObject
)
89 IMPLEMENT_DYNAMIC_CLASS(wxDocManager
, wxEvtHandler
)
90 IMPLEMENT_CLASS(wxDocChildFrame
, wxFrame
)
91 IMPLEMENT_CLASS(wxDocParentFrame
, wxFrame
)
93 #if wxUSE_PRINTING_ARCHITECTURE
94 IMPLEMENT_DYNAMIC_CLASS(wxDocPrintout
, wxPrintout
)
97 IMPLEMENT_DYNAMIC_CLASS(wxFileHistory
, wxObject
)
99 // ----------------------------------------------------------------------------
100 // function prototypes
101 // ----------------------------------------------------------------------------
103 static inline wxString
FindExtension(const wxChar
*path
);
104 static wxWindow
* wxFindSuitableParent(void);
106 // ----------------------------------------------------------------------------
108 // ----------------------------------------------------------------------------
110 static const wxChar
*s_MRUEntryFormat
= wxT("&%d %s");
112 // ============================================================================
114 // ============================================================================
116 // ----------------------------------------------------------------------------
118 // ----------------------------------------------------------------------------
120 static wxString
FindExtension(const wxChar
*path
)
123 wxSplitPath(path
, NULL
, NULL
, &ext
);
125 // VZ: extensions are considered not case sensitive - is this really a good
127 return ext
.MakeLower();
130 // ----------------------------------------------------------------------------
131 // Definition of wxDocument
132 // ----------------------------------------------------------------------------
134 wxDocument
::wxDocument(wxDocument
*parent
)
136 m_documentModified
= FALSE
;
137 m_documentParent
= parent
;
138 m_documentTemplate
= (wxDocTemplate
*) NULL
;
139 m_commandProcessor
= (wxCommandProcessor
*) NULL
;
143 bool wxDocument
::DeleteContents()
148 wxDocument
::~wxDocument()
152 if (m_commandProcessor
)
153 delete m_commandProcessor
;
155 if (GetDocumentManager())
156 GetDocumentManager()->RemoveDocument(this);
158 // Not safe to do here, since it'll invoke virtual view functions
159 // expecting to see valid derived objects: and by the time we get here,
160 // we've called destructors higher up.
164 bool wxDocument
::Close()
166 if (OnSaveModified())
167 return OnCloseDocument();
172 bool wxDocument
::OnCloseDocument()
174 // Tell all views that we're about to close
181 // Note that this implicitly deletes the document when the last view is
183 bool wxDocument
::DeleteAllViews()
185 wxDocManager
* manager
= GetDocumentManager();
186 wxList
::iterator it
, en
;
188 for ( it
= m_documentViews
.begin(), en
= m_documentViews
.end();
192 wxView
*view
= (wxView
*)*it
;
196 wxList
::iterator next
= it
; ++next
;
198 delete view
; // Deletes node implicitly
201 // If we haven't yet deleted the document (for example
202 // if there were no views) then delete it.
203 if (manager
&& manager
->GetDocuments().Member(this))
209 wxView
*wxDocument
::GetFirstView() const
211 if (m_documentViews
.GetCount() == 0)
212 return (wxView
*) NULL
;
213 return (wxView
*)m_documentViews
.GetFirst()->GetData();
216 wxDocManager
*wxDocument
::GetDocumentManager() const
218 return (m_documentTemplate ? m_documentTemplate
->GetDocumentManager() : (wxDocManager
*) NULL
);
221 bool wxDocument
::OnNewDocument()
223 if (!OnSaveModified())
226 if (OnCloseDocument()==FALSE
) return FALSE
;
229 SetDocumentSaved(FALSE
);
232 GetDocumentManager()->MakeDefaultName(name
);
234 SetFilename(name
, TRUE
);
239 bool wxDocument
::Save()
241 if (!IsModified() && m_savedYet
)
244 if ( m_documentFile
.empty() || !m_savedYet
)
247 return OnSaveDocument(m_documentFile
);
250 bool wxDocument
::SaveAs()
252 wxDocTemplate
*docTemplate
= GetDocumentTemplate();
256 wxString tmp
= wxFileSelector(_("Save as"),
257 docTemplate
->GetDirectory(),
258 wxFileNameFromPath(GetFilename()),
259 docTemplate
->GetDefaultExtension(),
260 docTemplate
->GetFileFilter(),
261 wxSAVE
| wxOVERWRITE_PROMPT
,
262 GetDocumentWindow());
267 wxString
fileName(tmp
);
268 wxString path
, name
, ext
;
269 wxSplitPath(fileName
, & path
, & name
, & ext
);
271 if (ext
.IsEmpty() || ext
== wxT(""))
273 fileName
+= wxT(".");
274 fileName
+= docTemplate
->GetDefaultExtension();
277 SetFilename(fileName
);
278 SetTitle(wxFileNameFromPath(fileName
));
280 // Notify the views that the filename has changed
281 wxList
::compatibility_iterator node
= m_documentViews
.GetFirst();
284 wxView
*view
= (wxView
*)node
->GetData();
285 view
->OnChangeFilename();
286 node
= node
->GetNext();
289 // Files that were not saved correctly are not added to the FileHistory.
290 if (!OnSaveDocument(m_documentFile
))
293 // A file that doesn't use the default extension of its document template cannot be opened
294 // via the FileHistory, so we do not add it.
295 if (docTemplate
->FileMatchesTemplate(fileName
))
297 GetDocumentManager()->AddFileToHistory(fileName
);
301 // The user will probably not be able to open the file again, so
302 // we could warn about the wrong file-extension here.
307 bool wxDocument
::OnSaveDocument(const wxString
& file
)
313 if (wxTheApp
->GetAppName() != wxT(""))
314 msgTitle
= wxTheApp
->GetAppName();
316 msgTitle
= wxString(_("File error"));
318 #if wxUSE_STD_IOSTREAM
319 wxSTD ofstream
store(file
.mb_str());
320 if (store
.fail() || store
.bad())
322 wxFileOutputStream
store(file
);
323 if (store
.GetLastError() != wxSTREAM_NO_ERROR
)
326 (void)wxMessageBox(_("Sorry, could not open this file for saving."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
327 GetDocumentWindow());
331 if (!SaveObject(store
))
333 (void)wxMessageBox(_("Sorry, could not save this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
334 GetDocumentWindow());
340 SetDocumentSaved(TRUE
);
342 wxFileName
fn(file
) ;
343 fn
.MacSetDefaultTypeAndCreator() ;
348 bool wxDocument
::OnOpenDocument(const wxString
& file
)
350 if (!OnSaveModified())
354 if (wxTheApp
->GetAppName() != wxT(""))
355 msgTitle
= wxTheApp
->GetAppName();
357 msgTitle
= wxString(_("File error"));
359 #if wxUSE_STD_IOSTREAM
360 wxSTD ifstream
store(file
.mb_str());
361 if (store
.fail() || store
.bad())
363 wxFileInputStream
store(file
);
364 if (store
.GetLastError() != wxSTREAM_NO_ERROR
)
367 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
|wxICON_EXCLAMATION
,
368 GetDocumentWindow());
371 #if wxUSE_STD_IOSTREAM
373 if ( !store
&& !store
.eof() )
375 int res
= LoadObject(store
).GetLastError();
376 if ((res
!= wxSTREAM_NO_ERROR
) &&
377 (res
!= wxSTREAM_EOF
))
380 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
|wxICON_EXCLAMATION
,
381 GetDocumentWindow());
384 SetFilename(file
, TRUE
);
393 #if wxUSE_STD_IOSTREAM
394 wxSTD istream
& wxDocument
::LoadObject(wxSTD istream
& stream
)
396 wxInputStream
& wxDocument
::LoadObject(wxInputStream
& stream
)
402 #if wxUSE_STD_IOSTREAM
403 wxSTD ostream
& wxDocument
::SaveObject(wxSTD ostream
& stream
)
405 wxOutputStream
& wxDocument
::SaveObject(wxOutputStream
& stream
)
411 bool wxDocument
::Revert()
417 // Get title, or filename if no title, else unnamed
418 bool wxDocument
::GetPrintableName(wxString
& buf
) const
420 if (m_documentTitle
!= wxT(""))
422 buf
= m_documentTitle
;
425 else if (m_documentFile
!= wxT(""))
427 buf
= wxFileNameFromPath(m_documentFile
);
437 wxWindow
*wxDocument
::GetDocumentWindow() const
439 wxView
*view
= GetFirstView();
441 return view
->GetFrame();
443 return wxTheApp
->GetTopWindow();
446 wxCommandProcessor
*wxDocument
::OnCreateCommandProcessor()
448 return new wxCommandProcessor
;
451 // TRUE if safe to close
452 bool wxDocument
::OnSaveModified()
457 GetPrintableName(title
);
460 if (wxTheApp
->GetAppName() != wxT(""))
461 msgTitle
= wxTheApp
->GetAppName();
463 msgTitle
= wxString(_("Warning"));
466 prompt
.Printf(_("Do you want to save changes to document %s?"),
467 (const wxChar
*)title
);
468 int res
= wxMessageBox(prompt
, msgTitle
,
469 wxYES_NO
|wxCANCEL
|wxICON_QUESTION
,
470 GetDocumentWindow());
476 else if (res
== wxYES
)
478 else if (res
== wxCANCEL
)
484 bool wxDocument
::Draw(wxDC
& WXUNUSED(context
))
489 bool wxDocument
::AddView(wxView
*view
)
491 if (!m_documentViews
.Member(view
))
493 m_documentViews
.Append(view
);
499 bool wxDocument
::RemoveView(wxView
*view
)
501 (void)m_documentViews
.DeleteObject(view
);
506 bool wxDocument
::OnCreate(const wxString
& WXUNUSED(path
), long flags
)
508 if (GetDocumentTemplate()->CreateView(this, flags
))
514 // Called after a view is added or removed.
515 // The default implementation deletes the document if
516 // there are no more views.
517 void wxDocument
::OnChangedViewList()
519 if (m_documentViews
.GetCount() == 0)
521 if (OnSaveModified())
528 void wxDocument
::UpdateAllViews(wxView
*sender
, wxObject
*hint
)
530 wxList
::compatibility_iterator node
= m_documentViews
.GetFirst();
533 wxView
*view
= (wxView
*)node
->GetData();
535 view
->OnUpdate(sender
, hint
);
536 node
= node
->GetNext();
540 void wxDocument
::NotifyClosing()
542 wxList
::compatibility_iterator node
= m_documentViews
.GetFirst();
545 wxView
*view
= (wxView
*)node
->GetData();
546 view
->OnClosingDocument();
547 node
= node
->GetNext();
551 void wxDocument
::SetFilename(const wxString
& filename
, bool notifyViews
)
553 m_documentFile
= filename
;
556 // Notify the views that the filename has changed
557 wxList
::compatibility_iterator node
= m_documentViews
.GetFirst();
560 wxView
*view
= (wxView
*)node
->GetData();
561 view
->OnChangeFilename();
562 node
= node
->GetNext();
567 // ----------------------------------------------------------------------------
569 // ----------------------------------------------------------------------------
573 m_viewDocument
= (wxDocument
*) NULL
;
575 m_viewFrame
= (wxFrame
*) NULL
;
580 GetDocumentManager()->ActivateView(this, FALSE
);
581 m_viewDocument
->RemoveView(this);
584 // Extend event processing to search the document's event table
585 bool wxView
::ProcessEvent(wxEvent
& event
)
587 if ( !GetDocument() || !GetDocument()->ProcessEvent(event
) )
588 return wxEvtHandler
::ProcessEvent(event
);
593 void wxView
::OnActivateView(bool WXUNUSED(activate
), wxView
*WXUNUSED(activeView
), wxView
*WXUNUSED(deactiveView
))
597 void wxView
::OnPrint(wxDC
*dc
, wxObject
*WXUNUSED(info
))
602 void wxView
::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
))
606 void wxView
::OnChangeFilename()
608 if (GetFrame() && GetDocument())
612 GetDocument()->GetPrintableName(title
);
614 GetFrame()->SetTitle(title
);
618 void wxView
::SetDocument(wxDocument
*doc
)
620 m_viewDocument
= doc
;
625 bool wxView
::Close(bool deleteWindow
)
627 if (OnClose(deleteWindow
))
633 void wxView
::Activate(bool activate
)
635 if (GetDocument() && GetDocumentManager())
637 OnActivateView(activate
, this, GetDocumentManager()->GetCurrentView());
638 GetDocumentManager()->ActivateView(this, activate
);
642 bool wxView
::OnClose(bool WXUNUSED(deleteWindow
))
644 return GetDocument() ?
GetDocument()->Close() : TRUE
;
647 #if wxUSE_PRINTING_ARCHITECTURE
648 wxPrintout
*wxView
::OnCreatePrintout()
650 return new wxDocPrintout(this);
652 #endif // wxUSE_PRINTING_ARCHITECTURE
654 // ----------------------------------------------------------------------------
656 // ----------------------------------------------------------------------------
658 wxDocTemplate
::wxDocTemplate(wxDocManager
*manager
,
659 const wxString
& descr
,
660 const wxString
& filter
,
663 const wxString
& docTypeName
,
664 const wxString
& viewTypeName
,
665 wxClassInfo
*docClassInfo
,
666 wxClassInfo
*viewClassInfo
,
669 m_documentManager
= manager
;
670 m_description
= descr
;
673 m_fileFilter
= filter
;
675 m_docTypeName
= docTypeName
;
676 m_viewTypeName
= viewTypeName
;
677 m_documentManager
->AssociateTemplate(this);
679 m_docClassInfo
= docClassInfo
;
680 m_viewClassInfo
= viewClassInfo
;
683 wxDocTemplate
::~wxDocTemplate()
685 m_documentManager
->DisassociateTemplate(this);
688 // Tries to dynamically construct an object of the right class.
689 wxDocument
*wxDocTemplate
::CreateDocument(const wxString
& path
, long flags
)
692 return (wxDocument
*) NULL
;
693 wxDocument
*doc
= (wxDocument
*)m_docClassInfo
->CreateObject();
695 if (InitDocument(doc
, path
, flags
))
701 return (wxDocument
*) NULL
;
705 bool wxDocTemplate
::InitDocument(wxDocument
* doc
, const wxString
& path
, long flags
)
707 doc
->SetFilename(path
);
708 doc
->SetDocumentTemplate(this);
709 GetDocumentManager()->AddDocument(doc
);
710 doc
->SetCommandProcessor(doc
->OnCreateCommandProcessor());
712 if (doc
->OnCreate(path
, flags
))
716 if (GetDocumentManager()->GetDocuments().Member(doc
))
717 doc
->DeleteAllViews();
722 wxView
*wxDocTemplate
::CreateView(wxDocument
*doc
, long flags
)
724 if (!m_viewClassInfo
)
725 return (wxView
*) NULL
;
726 wxView
*view
= (wxView
*)m_viewClassInfo
->CreateObject();
727 view
->SetDocument(doc
);
728 if (view
->OnCreate(doc
, flags
))
735 return (wxView
*) NULL
;
739 // The default (very primitive) format detection: check is the extension is
740 // that of the template
741 bool wxDocTemplate
::FileMatchesTemplate(const wxString
& path
)
743 return GetDefaultExtension().IsSameAs(FindExtension(path
));
746 // ----------------------------------------------------------------------------
748 // ----------------------------------------------------------------------------
750 BEGIN_EVENT_TABLE(wxDocManager
, wxEvtHandler
)
751 EVT_MENU(wxID_OPEN
, wxDocManager
::OnFileOpen
)
752 EVT_MENU(wxID_CLOSE
, wxDocManager
::OnFileClose
)
753 EVT_MENU(wxID_CLOSE_ALL
, wxDocManager
::OnFileCloseAll
)
754 EVT_MENU(wxID_REVERT
, wxDocManager
::OnFileRevert
)
755 EVT_MENU(wxID_NEW
, wxDocManager
::OnFileNew
)
756 EVT_MENU(wxID_SAVE
, wxDocManager
::OnFileSave
)
757 EVT_MENU(wxID_SAVEAS
, wxDocManager
::OnFileSaveAs
)
758 EVT_MENU(wxID_UNDO
, wxDocManager
::OnUndo
)
759 EVT_MENU(wxID_REDO
, wxDocManager
::OnRedo
)
761 EVT_UPDATE_UI(wxID_OPEN
, wxDocManager
::OnUpdateFileOpen
)
762 EVT_UPDATE_UI(wxID_CLOSE
, wxDocManager
::OnUpdateFileClose
)
763 EVT_UPDATE_UI(wxID_CLOSE_ALL
, wxDocManager
::OnUpdateFileClose
)
764 EVT_UPDATE_UI(wxID_REVERT
, wxDocManager
::OnUpdateFileRevert
)
765 EVT_UPDATE_UI(wxID_NEW
, wxDocManager
::OnUpdateFileNew
)
766 EVT_UPDATE_UI(wxID_SAVE
, wxDocManager
::OnUpdateFileSave
)
767 EVT_UPDATE_UI(wxID_SAVEAS
, wxDocManager
::OnUpdateFileSaveAs
)
768 EVT_UPDATE_UI(wxID_UNDO
, wxDocManager
::OnUpdateUndo
)
769 EVT_UPDATE_UI(wxID_REDO
, wxDocManager
::OnUpdateRedo
)
771 #if wxUSE_PRINTING_ARCHITECTURE
772 EVT_MENU(wxID_PRINT
, wxDocManager
::OnPrint
)
773 EVT_MENU(wxID_PRINT_SETUP
, wxDocManager
::OnPrintSetup
)
774 EVT_MENU(wxID_PREVIEW
, wxDocManager
::OnPreview
)
776 EVT_UPDATE_UI(wxID_PRINT
, wxDocManager
::OnUpdatePrint
)
777 EVT_UPDATE_UI(wxID_PRINT_SETUP
, wxDocManager
::OnUpdatePrintSetup
)
778 EVT_UPDATE_UI(wxID_PREVIEW
, wxDocManager
::OnUpdatePreview
)
782 wxDocManager
* wxDocManager
::sm_docManager
= (wxDocManager
*) NULL
;
784 wxDocManager
::wxDocManager(long flags
, bool initialize
)
786 m_defaultDocumentNameCounter
= 1;
788 m_currentView
= (wxView
*) NULL
;
789 m_maxDocsOpen
= 10000;
790 m_fileHistory
= (wxFileHistory
*) NULL
;
793 sm_docManager
= this;
796 wxDocManager
::~wxDocManager()
800 delete m_fileHistory
;
801 sm_docManager
= (wxDocManager
*) NULL
;
804 // closes the specified document
805 bool wxDocManager
::CloseDocument(wxDocument
* doc
, bool force
)
807 if (doc
->Close() || force
)
809 // Implicitly deletes the document when
810 // the last view is deleted
811 doc
->DeleteAllViews();
813 // Check we're really deleted
814 if (m_docs
.Member(doc
))
822 bool wxDocManager
::CloseDocuments(bool force
)
824 wxList
::compatibility_iterator node
= m_docs
.GetFirst();
827 wxDocument
*doc
= (wxDocument
*)node
->GetData();
828 wxList
::compatibility_iterator next
= node
->GetNext();
830 if (!CloseDocument(doc
, force
))
833 // This assumes that documents are not connected in
834 // any way, i.e. deleting one document does NOT
841 bool wxDocManager
::Clear(bool force
)
843 if (!CloseDocuments(force
))
846 wxList
::compatibility_iterator node
= m_templates
.GetFirst();
849 wxDocTemplate
*templ
= (wxDocTemplate
*) node
->GetData();
850 wxList
::compatibility_iterator next
= node
->GetNext();
857 bool wxDocManager
::Initialize()
859 m_fileHistory
= OnCreateFileHistory();
863 wxFileHistory
*wxDocManager
::OnCreateFileHistory()
865 return new wxFileHistory
;
868 void wxDocManager
::OnFileClose(wxCommandEvent
& WXUNUSED(event
))
870 wxDocument
*doc
= GetCurrentDocument();
875 doc
->DeleteAllViews();
876 if (m_docs
.Member(doc
))
881 void wxDocManager
::OnFileCloseAll(wxCommandEvent
& WXUNUSED(event
))
883 CloseDocuments(FALSE
);
886 void wxDocManager
::OnFileNew(wxCommandEvent
& WXUNUSED(event
))
888 CreateDocument( wxT(""), wxDOC_NEW
);
891 void wxDocManager
::OnFileOpen(wxCommandEvent
& WXUNUSED(event
))
893 if ( !CreateDocument( wxT(""), 0) )
899 void wxDocManager
::OnFileRevert(wxCommandEvent
& WXUNUSED(event
))
901 wxDocument
*doc
= GetCurrentDocument();
907 void wxDocManager
::OnFileSave(wxCommandEvent
& WXUNUSED(event
))
909 wxDocument
*doc
= GetCurrentDocument();
915 void wxDocManager
::OnFileSaveAs(wxCommandEvent
& WXUNUSED(event
))
917 wxDocument
*doc
= GetCurrentDocument();
923 void wxDocManager
::OnPrint(wxCommandEvent
& WXUNUSED(event
))
925 #if wxUSE_PRINTING_ARCHITECTURE
926 wxView
*view
= GetCurrentView();
930 wxPrintout
*printout
= view
->OnCreatePrintout();
934 printer
.Print(view
->GetFrame(), printout
, TRUE
);
938 #endif // wxUSE_PRINTING_ARCHITECTURE
941 void wxDocManager
::OnPrintSetup(wxCommandEvent
& WXUNUSED(event
))
943 #if wxUSE_PRINTING_ARCHITECTURE
944 wxWindow
*parentWin
= wxTheApp
->GetTopWindow();
945 wxView
*view
= GetCurrentView();
947 parentWin
= view
->GetFrame();
949 wxPrintDialogData data
;
951 wxPrintDialog
printerDialog(parentWin
, &data
);
952 printerDialog
.GetPrintDialogData().SetSetupDialog(TRUE
);
953 printerDialog
.ShowModal();
954 #endif // wxUSE_PRINTING_ARCHITECTURE
957 void wxDocManager
::OnPreview(wxCommandEvent
& WXUNUSED(event
))
959 #if wxUSE_PRINTING_ARCHITECTURE
960 wxView
*view
= GetCurrentView();
964 wxPrintout
*printout
= view
->OnCreatePrintout();
967 // Pass two printout objects: for preview, and possible printing.
968 wxPrintPreviewBase
*preview
= new wxPrintPreview(printout
, view
->OnCreatePrintout());
969 if ( !preview
->Ok() )
972 wxMessageBox( _("Sorry, print preview needs a printer to be installed.") );
976 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, (wxFrame
*)wxTheApp
->GetTopWindow(), _("Print Preview"),
977 wxPoint(100, 100), wxSize(600, 650));
978 frame
->Centre(wxBOTH
);
982 #endif // wxUSE_PRINTING_ARCHITECTURE
985 void wxDocManager
::OnUndo(wxCommandEvent
& event
)
987 wxDocument
*doc
= GetCurrentDocument();
990 if (doc
->GetCommandProcessor())
991 doc
->GetCommandProcessor()->Undo();
996 void wxDocManager
::OnRedo(wxCommandEvent
& event
)
998 wxDocument
*doc
= GetCurrentDocument();
1001 if (doc
->GetCommandProcessor())
1002 doc
->GetCommandProcessor()->Redo();
1007 // Handlers for UI update commands
1009 void wxDocManager
::OnUpdateFileOpen(wxUpdateUIEvent
& event
)
1011 event
.Enable( TRUE
);
1014 void wxDocManager
::OnUpdateFileClose(wxUpdateUIEvent
& event
)
1016 wxDocument
*doc
= GetCurrentDocument();
1017 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1020 void wxDocManager
::OnUpdateFileRevert(wxUpdateUIEvent
& event
)
1022 wxDocument
*doc
= GetCurrentDocument();
1023 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1026 void wxDocManager
::OnUpdateFileNew(wxUpdateUIEvent
& event
)
1028 event
.Enable( TRUE
);
1031 void wxDocManager
::OnUpdateFileSave(wxUpdateUIEvent
& event
)
1033 wxDocument
*doc
= GetCurrentDocument();
1034 event
.Enable( doc
&& doc
->IsModified() );
1037 void wxDocManager
::OnUpdateFileSaveAs(wxUpdateUIEvent
& event
)
1039 wxDocument
*doc
= GetCurrentDocument();
1040 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1043 void wxDocManager
::OnUpdateUndo(wxUpdateUIEvent
& event
)
1045 wxDocument
*doc
= GetCurrentDocument();
1047 event
.Enable(FALSE
);
1048 else if (!doc
->GetCommandProcessor())
1052 event
.Enable( doc
->GetCommandProcessor()->CanUndo() );
1053 doc
->GetCommandProcessor()->SetMenuStrings();
1057 void wxDocManager
::OnUpdateRedo(wxUpdateUIEvent
& event
)
1059 wxDocument
*doc
= GetCurrentDocument();
1061 event
.Enable(FALSE
);
1062 else if (!doc
->GetCommandProcessor())
1066 event
.Enable( doc
->GetCommandProcessor()->CanRedo() );
1067 doc
->GetCommandProcessor()->SetMenuStrings();
1071 void wxDocManager
::OnUpdatePrint(wxUpdateUIEvent
& event
)
1073 wxDocument
*doc
= GetCurrentDocument();
1074 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1077 void wxDocManager
::OnUpdatePrintSetup(wxUpdateUIEvent
& event
)
1079 event
.Enable( TRUE
);
1082 void wxDocManager
::OnUpdatePreview(wxUpdateUIEvent
& event
)
1084 wxDocument
*doc
= GetCurrentDocument();
1085 event
.Enable( (doc
!= (wxDocument
*) NULL
) );
1088 wxView
*wxDocManager
::GetCurrentView() const
1091 return m_currentView
;
1092 if (m_docs
.GetCount() == 1)
1094 wxDocument
* doc
= (wxDocument
*) m_docs
.GetFirst()->GetData();
1095 return doc
->GetFirstView();
1097 return (wxView
*) NULL
;
1100 // Extend event processing to search the view's event table
1101 bool wxDocManager
::ProcessEvent(wxEvent
& event
)
1103 wxView
* view
= GetCurrentView();
1106 if (view
->ProcessEvent(event
))
1109 return wxEvtHandler
::ProcessEvent(event
);
1112 wxDocument
*wxDocManager
::CreateDocument(const wxString
& path
, long flags
)
1114 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.GetCount()];
1117 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1119 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Item(i
)->GetData());
1120 if (temp
->IsVisible())
1122 templates
[n
] = temp
;
1129 return (wxDocument
*) NULL
;
1132 wxDocument
* docToClose
= NULL
;
1134 // If we've reached the max number of docs, close the
1136 if ( (int)GetDocuments().GetCount() >= m_maxDocsOpen
)
1138 wxDocument
*doc
= (wxDocument
*)GetDocuments().GetFirst()->GetData();
1142 // New document: user chooses a template, unless there's only one.
1143 if (flags
& wxDOC_NEW
)
1149 if (!CloseDocument(docToClose
, FALSE
))
1156 wxDocTemplate
*temp
= templates
[0];
1158 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
1162 newDoc
->SetDocumentName(temp
->GetDocumentName());
1163 newDoc
->SetDocumentTemplate(temp
);
1164 newDoc
->OnNewDocument();
1169 wxDocTemplate
*temp
= SelectDocumentType(templates
, n
);
1175 if (!CloseDocument(docToClose
, FALSE
))
1181 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
1185 newDoc
->SetDocumentName(temp
->GetDocumentName());
1186 newDoc
->SetDocumentTemplate(temp
);
1187 newDoc
->OnNewDocument();
1192 return (wxDocument
*) NULL
;
1195 // Existing document
1196 wxDocTemplate
*temp
;
1198 wxString
path2(wxT(""));
1199 if (path
!= wxT(""))
1202 if (flags
& wxDOC_SILENT
)
1204 temp
= FindTemplateForPath(path2
);
1207 // Since we do not add files with non-default extensions to the FileHistory this
1208 // can only happen if the application changes the allowed templates in runtime.
1209 (void)wxMessageBox(_("Sorry, the format for this file is unknown."),
1211 wxOK
| wxICON_EXCLAMATION
, wxFindSuitableParent());
1215 temp
= SelectDocumentPath(templates
, n
, path2
, flags
);
1223 if (!CloseDocument(docToClose
, FALSE
))
1229 wxDocument
*newDoc
= temp
->CreateDocument(path2
, flags
);
1232 newDoc
->SetDocumentName(temp
->GetDocumentName());
1233 newDoc
->SetDocumentTemplate(temp
);
1234 if (!newDoc
->OnOpenDocument(path2
))
1236 newDoc
->DeleteAllViews();
1237 // delete newDoc; // Implicitly deleted by DeleteAllViews
1238 return (wxDocument
*) NULL
;
1240 // A file that doesn't use the default extension of its document
1241 // template cannot be opened via the FileHistory, so we do not
1243 if (temp
->FileMatchesTemplate(path2
))
1244 AddFileToHistory(path2
);
1249 return (wxDocument
*) NULL
;
1252 wxView
*wxDocManager
::CreateView(wxDocument
*doc
, long flags
)
1254 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.GetCount()];
1257 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1259 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Item(i
)->GetData());
1260 if (temp
->IsVisible())
1262 if (temp
->GetDocumentName() == doc
->GetDocumentName())
1264 templates
[n
] = temp
;
1272 return (wxView
*) NULL
;
1276 wxDocTemplate
*temp
= templates
[0];
1278 wxView
*view
= temp
->CreateView(doc
, flags
);
1280 view
->SetViewName(temp
->GetViewName());
1284 wxDocTemplate
*temp
= SelectViewType(templates
, n
);
1288 wxView
*view
= temp
->CreateView(doc
, flags
);
1290 view
->SetViewName(temp
->GetViewName());
1294 return (wxView
*) NULL
;
1297 // Not yet implemented
1298 void wxDocManager
::DeleteTemplate(wxDocTemplate
*WXUNUSED(temp
), long WXUNUSED(flags
))
1302 // Not yet implemented
1303 bool wxDocManager
::FlushDoc(wxDocument
*WXUNUSED(doc
))
1308 wxDocument
*wxDocManager
::GetCurrentDocument() const
1310 wxView
*view
= GetCurrentView();
1312 return view
->GetDocument();
1314 return (wxDocument
*) NULL
;
1317 // Make a default document name
1318 bool wxDocManager
::MakeDefaultName(wxString
& name
)
1320 name
.Printf(_("unnamed%d"), m_defaultDocumentNameCounter
);
1321 m_defaultDocumentNameCounter
++;
1326 // Make a frame title (override this to do something different)
1327 // If docName is empty, a document is not currently active.
1328 wxString wxDocManager
::MakeFrameTitle(wxDocument
* doc
)
1330 wxString appName
= wxTheApp
->GetAppName();
1337 doc
->GetPrintableName(docName
);
1338 title
= docName
+ wxString(_(" - ")) + appName
;
1344 // Not yet implemented
1345 wxDocTemplate
*wxDocManager
::MatchTemplate(const wxString
& WXUNUSED(path
))
1347 return (wxDocTemplate
*) NULL
;
1350 // File history management
1351 void wxDocManager
::AddFileToHistory(const wxString
& file
)
1354 m_fileHistory
->AddFileToHistory(file
);
1357 void wxDocManager
::RemoveFileFromHistory(size_t i
)
1360 m_fileHistory
->RemoveFileFromHistory(i
);
1363 wxString wxDocManager
::GetHistoryFile(size_t i
) const
1368 histFile
= m_fileHistory
->GetHistoryFile(i
);
1373 void wxDocManager
::FileHistoryUseMenu(wxMenu
*menu
)
1376 m_fileHistory
->UseMenu(menu
);
1379 void wxDocManager
::FileHistoryRemoveMenu(wxMenu
*menu
)
1382 m_fileHistory
->RemoveMenu(menu
);
1386 void wxDocManager
::FileHistoryLoad(wxConfigBase
& config
)
1389 m_fileHistory
->Load(config
);
1392 void wxDocManager
::FileHistorySave(wxConfigBase
& config
)
1395 m_fileHistory
->Save(config
);
1399 void wxDocManager
::FileHistoryAddFilesToMenu(wxMenu
* menu
)
1402 m_fileHistory
->AddFilesToMenu(menu
);
1405 void wxDocManager
::FileHistoryAddFilesToMenu()
1408 m_fileHistory
->AddFilesToMenu();
1411 size_t wxDocManager
::GetHistoryFilesCount() const
1413 return m_fileHistory ? m_fileHistory
->GetCount() : 0;
1417 // Find out the document template via matching in the document file format
1418 // against that of the template
1419 wxDocTemplate
*wxDocManager
::FindTemplateForPath(const wxString
& path
)
1421 wxDocTemplate
*theTemplate
= (wxDocTemplate
*) NULL
;
1423 // Find the template which this extension corresponds to
1424 for (size_t i
= 0; i
< m_templates
.GetCount(); i
++)
1426 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Item(i
)->GetData();
1427 if ( temp
->FileMatchesTemplate(path
) )
1436 // Try to get a more suitable parent frame than the top window,
1437 // for selection dialogs. Otherwise you may get an unexpected
1438 // window being activated when a dialog is shown.
1439 static wxWindow
* wxFindSuitableParent()
1441 wxWindow
* parent
= wxTheApp
->GetTopWindow();
1443 wxWindow
* focusWindow
= wxWindow
::FindFocus();
1446 while (focusWindow
&&
1447 !focusWindow
->IsKindOf(CLASSINFO(wxDialog
)) &&
1448 !focusWindow
->IsKindOf(CLASSINFO(wxFrame
)))
1450 focusWindow
= focusWindow
->GetParent();
1453 parent
= focusWindow
;
1458 // Prompts user to open a file, using file specs in templates.
1459 // Must extend the file selector dialog or implement own; OR
1460 // match the extension to the template extension.
1462 wxDocTemplate
*wxDocManager
::SelectDocumentPath(wxDocTemplate
**templates
,
1463 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
1466 int WXUNUSED(noTemplates
),
1469 long WXUNUSED(flags
),
1470 bool WXUNUSED(save
))
1472 // We can only have multiple filters in Windows and GTK
1473 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
1477 for (i
= 0; i
< noTemplates
; i
++)
1479 if (templates
[i
]->IsVisible())
1481 // add a '|' to separate this filter from the previous one
1482 if ( !descrBuf
.IsEmpty() )
1483 descrBuf
<< wxT('|');
1485 descrBuf
<< templates
[i
]->GetDescription()
1486 << wxT(" (") << templates
[i
]->GetFileFilter() << wxT(") |")
1487 << templates
[i
]->GetFileFilter();
1491 wxString descrBuf
= wxT("*.*");
1494 int FilterIndex
= -1;
1496 wxWindow
* parent
= wxFindSuitableParent();
1498 wxString pathTmp
= wxFileSelectorEx(_("Select a file"),
1506 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)NULL
;
1507 if (!pathTmp
.IsEmpty())
1509 if (!wxFileExists(pathTmp
))
1512 if (!wxTheApp
->GetAppName().IsEmpty())
1513 msgTitle
= wxTheApp
->GetAppName();
1515 msgTitle
= wxString(_("File error"));
1517 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
1521 return (wxDocTemplate
*) NULL
;
1523 m_lastDirectory
= wxPathOnly(pathTmp
);
1527 // first choose the template using the extension, if this fails (i.e.
1528 // wxFileSelectorEx() didn't fill it), then use the path
1529 if ( FilterIndex
!= -1 )
1530 theTemplate
= templates
[FilterIndex
];
1532 theTemplate
= FindTemplateForPath(path
);
1535 // Since we do not add files with non-default extensions to the FileHistory this
1536 // can only happen if the application changes the allowed templates in runtime.
1537 (void)wxMessageBox(_("Sorry, the format for this file is unknown."),
1539 wxOK
| wxICON_EXCLAMATION
, wxFindSuitableParent());
1550 wxDocTemplate
*wxDocManager
::SelectDocumentType(wxDocTemplate
**templates
,
1551 int noTemplates
, bool sort
)
1553 wxArrayString strings
;
1554 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1558 for (i
= 0; i
< noTemplates
; i
++)
1560 if (templates
[i
]->IsVisible())
1564 for (j
= 0; j
< n
; j
++)
1566 //filter out NOT unique documents + view combinations
1567 if ( templates
[i
]->m_docTypeName
== data
[j
]->m_docTypeName
&&
1568 templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
1575 strings
.Add(templates
[i
]->m_description
);
1577 data
[n
] = templates
[i
];
1585 strings
.Sort(wxStringSortAscending
);
1586 // Yes, this will be slow, but template lists
1587 // are typically short.
1589 n
= strings
.Count();
1590 for (i
= 0; i
< n
; i
++)
1592 for (j
= 0; j
< noTemplates
; j
++)
1594 if (strings
[i
] == templates
[j
]->m_description
)
1595 data
[i
] = templates
[j
];
1600 wxDocTemplate
*theTemplate
;
1605 // no visible templates, hence nothing to choose from
1610 // don't propose the user to choose if he heas no choice
1611 theTemplate
= data
[0];
1615 // propose the user to choose one of several
1616 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1618 _("Select a document template"),
1622 wxFindSuitableParent()
1631 wxDocTemplate
*wxDocManager
::SelectViewType(wxDocTemplate
**templates
,
1632 int noTemplates
, bool sort
)
1634 wxArrayString strings
;
1635 wxDocTemplate
**data
= new wxDocTemplate
*[noTemplates
];
1639 for (i
= 0; i
< noTemplates
; i
++)
1641 wxDocTemplate
*templ
= templates
[i
];
1642 if ( templ
->IsVisible() && !templ
->GetViewName().empty() )
1646 for (j
= 0; j
< n
; j
++)
1648 //filter out NOT unique views
1649 if ( templates
[i
]->m_viewTypeName
== data
[j
]->m_viewTypeName
)
1655 strings
.Add(templ
->m_viewTypeName
);
1664 strings
.Sort(wxStringSortAscending
);
1665 // Yes, this will be slow, but template lists
1666 // are typically short.
1668 n
= strings
.Count();
1669 for (i
= 0; i
< n
; i
++)
1671 for (j
= 0; j
< noTemplates
; j
++)
1673 if (strings
[i
] == templates
[j
]->m_viewTypeName
)
1674 data
[i
] = templates
[j
];
1679 wxDocTemplate
*theTemplate
;
1681 // the same logic as above
1685 theTemplate
= (wxDocTemplate
*)NULL
;
1689 theTemplate
= data
[0];
1693 theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData
1695 _("Select a document view"),
1699 wxFindSuitableParent()
1708 void wxDocManager
::AssociateTemplate(wxDocTemplate
*temp
)
1710 if (!m_templates
.Member(temp
))
1711 m_templates
.Append(temp
);
1714 void wxDocManager
::DisassociateTemplate(wxDocTemplate
*temp
)
1716 m_templates
.DeleteObject(temp
);
1719 // Add and remove a document from the manager's list
1720 void wxDocManager
::AddDocument(wxDocument
*doc
)
1722 if (!m_docs
.Member(doc
))
1726 void wxDocManager
::RemoveDocument(wxDocument
*doc
)
1728 m_docs
.DeleteObject(doc
);
1731 // Views or windows should inform the document manager
1732 // when a view is going in or out of focus
1733 void wxDocManager
::ActivateView(wxView
*view
, bool activate
)
1737 m_currentView
= view
;
1741 if ( m_currentView
== view
)
1743 // don't keep stale pointer
1744 m_currentView
= (wxView
*) NULL
;
1749 // ----------------------------------------------------------------------------
1750 // Default document child frame
1751 // ----------------------------------------------------------------------------
1753 BEGIN_EVENT_TABLE(wxDocChildFrame
, wxFrame
)
1754 EVT_ACTIVATE(wxDocChildFrame
::OnActivate
)
1755 EVT_CLOSE(wxDocChildFrame
::OnCloseWindow
)
1758 wxDocChildFrame
::wxDocChildFrame(wxDocument
*doc
,
1762 const wxString
& title
,
1766 const wxString
& name
)
1767 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1769 m_childDocument
= doc
;
1772 view
->SetFrame(this);
1775 wxDocChildFrame
::~wxDocChildFrame()
1779 // Extend event processing to search the view's event table
1780 bool wxDocChildFrame
::ProcessEvent(wxEvent
& event
)
1783 m_childView
->Activate(TRUE
);
1785 if ( !m_childView
|| ! m_childView
->ProcessEvent(event
) )
1787 // Only hand up to the parent if it's a menu command
1788 if (!event
.IsKindOf(CLASSINFO(wxCommandEvent
)) || !GetParent() || !GetParent()->ProcessEvent(event
))
1789 return wxEvtHandler
::ProcessEvent(event
);
1797 void wxDocChildFrame
::OnActivate(wxActivateEvent
& event
)
1799 wxFrame
::OnActivate(event
);
1802 m_childView
->Activate(event
.GetActive());
1805 void wxDocChildFrame
::OnCloseWindow(wxCloseEvent
& event
)
1809 bool ans
= event
.CanVeto()
1810 ? m_childView
->Close(FALSE
) // FALSE means don't delete associated window
1811 : TRUE
; // Must delete.
1815 m_childView
->Activate(FALSE
);
1817 m_childView
= (wxView
*) NULL
;
1818 m_childDocument
= (wxDocument
*) NULL
;
1829 // ----------------------------------------------------------------------------
1830 // Default parent frame
1831 // ----------------------------------------------------------------------------
1833 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1834 EVT_MENU(wxID_EXIT
, wxDocParentFrame
::OnExit
)
1835 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE9
, wxDocParentFrame
::OnMRUFile
)
1836 EVT_CLOSE(wxDocParentFrame
::OnCloseWindow
)
1839 wxDocParentFrame
::wxDocParentFrame(wxDocManager
*manager
,
1842 const wxString
& title
,
1846 const wxString
& name
)
1847 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1849 m_docManager
= manager
;
1852 void wxDocParentFrame
::OnExit(wxCommandEvent
& WXUNUSED(event
))
1857 void wxDocParentFrame
::OnMRUFile(wxCommandEvent
& event
)
1859 int n
= event
.GetId() - wxID_FILE1
; // the index in MRU list
1860 wxString
filename(m_docManager
->GetHistoryFile(n
));
1861 if ( !filename
.IsEmpty() )
1863 // verify that the file exists before doing anything else
1864 if ( wxFile
::Exists(filename
) )
1867 if (!m_docManager
->CreateDocument(filename
, wxDOC_SILENT
))
1869 // remove the file from the MRU list. The user should already be notified.
1870 m_docManager
->RemoveFileFromHistory(n
);
1872 wxLogError(_("The file '%s' couldn't be opened.\nIt has been removed from the most recently used files list."),
1878 // remove the bogus filename from the MRU list and notify the user
1880 m_docManager
->RemoveFileFromHistory(n
);
1882 wxLogError(_("The file '%s' doesn't exist and couldn't be opened.\nIt has been removed from the most recently used files list."),
1888 // Extend event processing to search the view's event table
1889 bool wxDocParentFrame
::ProcessEvent(wxEvent
& event
)
1891 // Try the document manager, then do default processing
1892 if (!m_docManager
|| !m_docManager
->ProcessEvent(event
))
1893 return wxEvtHandler
::ProcessEvent(event
);
1898 // Define the behaviour for the frame closing
1899 // - must delete all frames except for the main one.
1900 void wxDocParentFrame
::OnCloseWindow(wxCloseEvent
& event
)
1902 if (m_docManager
->Clear(!event
.CanVeto()))
1910 #if wxUSE_PRINTING_ARCHITECTURE
1912 wxDocPrintout
::wxDocPrintout(wxView
*view
, const wxString
& title
)
1915 m_printoutView
= view
;
1918 bool wxDocPrintout
::OnPrintPage(int WXUNUSED(page
))
1922 // Get the logical pixels per inch of screen and printer
1923 int ppiScreenX
, ppiScreenY
;
1924 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
1925 wxUnusedVar(ppiScreenY
);
1926 int ppiPrinterX
, ppiPrinterY
;
1927 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
1928 wxUnusedVar(ppiPrinterY
);
1930 // This scales the DC so that the printout roughly represents the
1931 // the screen scaling. The text point size _should_ be the right size
1932 // but in fact is too small for some reason. This is a detail that will
1933 // need to be addressed at some point but can be fudged for the
1935 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
1937 // Now we have to check in case our real page size is reduced
1938 // (e.g. because we're drawing to a print preview memory DC)
1939 int pageWidth
, pageHeight
;
1941 dc
->GetSize(&w
, &h
);
1942 GetPageSizePixels(&pageWidth
, &pageHeight
);
1943 wxUnusedVar(pageHeight
);
1945 // If printer pageWidth == current DC width, then this doesn't
1946 // change. But w might be the preview bitmap width, so scale down.
1947 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
1948 dc
->SetUserScale(overallScale
, overallScale
);
1952 m_printoutView
->OnDraw(dc
);
1957 bool wxDocPrintout
::HasPage(int pageNum
)
1959 return (pageNum
== 1);
1962 bool wxDocPrintout
::OnBeginDocument(int startPage
, int endPage
)
1964 if (!wxPrintout
::OnBeginDocument(startPage
, endPage
))
1970 void wxDocPrintout
::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
1978 #endif // wxUSE_PRINTING_ARCHITECTURE
1980 // ----------------------------------------------------------------------------
1981 // File history processor
1982 // ----------------------------------------------------------------------------
1984 static inline wxChar
* MYcopystring(const wxString
& s
)
1986 wxChar
* copy
= new wxChar
[s
.length() + 1];
1987 return wxStrcpy(copy
, s
.c_str());
1990 static inline wxChar
* MYcopystring(const wxChar
* s
)
1992 wxChar
* copy
= new wxChar
[wxStrlen(s
) + 1];
1993 return wxStrcpy(copy
, s
);
1996 wxFileHistory
::wxFileHistory(size_t maxFiles
, wxWindowID idBase
)
1998 m_fileMaxFiles
= maxFiles
;
2001 m_fileHistory
= new wxChar
*[m_fileMaxFiles
];
2004 wxFileHistory
::~wxFileHistory()
2007 for (i
= 0; i
< m_fileHistoryN
; i
++)
2008 delete[] m_fileHistory
[i
];
2009 delete[] m_fileHistory
;
2012 // File history management
2013 void wxFileHistory
::AddFileToHistory(const wxString
& file
)
2017 // Check we don't already have this file
2018 for (i
= 0; i
< m_fileHistoryN
; i
++)
2020 #if defined( __WXMSW__ ) // Add any other OSes with case insensitive file names
2021 wxString testString
;
2022 if ( m_fileHistory
[i
] )
2023 testString
= m_fileHistory
[i
];
2024 if ( m_fileHistory
[i
] && ( file
.Lower() == testString
.Lower() ) )
2026 if ( m_fileHistory
[i
] && ( file
== m_fileHistory
[i
] ) )
2029 // we do have it, move it to the top of the history
2030 RemoveFileFromHistory (i
);
2031 AddFileToHistory (file
);
2036 // if we already have a full history, delete the one at the end
2037 if ( m_fileMaxFiles
== m_fileHistoryN
)
2039 RemoveFileFromHistory (m_fileHistoryN
- 1);
2040 AddFileToHistory (file
);
2044 // Add to the project file history:
2045 // Move existing files (if any) down so we can insert file at beginning.
2046 if (m_fileHistoryN
< m_fileMaxFiles
)
2048 wxList
::compatibility_iterator node
= m_fileMenus
.GetFirst();
2051 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2052 if ( m_fileHistoryN
== 0 && menu
->GetMenuItemCount() )
2054 menu
->AppendSeparator();
2056 menu
->Append(m_idBase
+m_fileHistoryN
, _("[EMPTY]"));
2057 node
= node
->GetNext();
2061 // Shuffle filenames down
2062 for (i
= (m_fileHistoryN
-1); i
> 0; i
--)
2064 m_fileHistory
[i
] = m_fileHistory
[i
-1];
2066 m_fileHistory
[0] = MYcopystring(file
);
2068 // this is the directory of the last opened file
2069 wxString pathCurrent
;
2070 wxSplitPath( m_fileHistory
[0], &pathCurrent
, NULL
, NULL
);
2071 for (i
= 0; i
< m_fileHistoryN
; i
++)
2073 if ( m_fileHistory
[i
] )
2075 // if in same directory just show the filename; otherwise the full
2077 wxString pathInMenu
, path
, filename
, ext
;
2078 wxSplitPath( m_fileHistory
[i
], &path
, &filename
, &ext
);
2079 if ( path
== pathCurrent
)
2081 pathInMenu
= filename
;
2083 pathInMenu
= pathInMenu
+ wxFILE_SEP_EXT
+ ext
;
2087 // absolute path; could also set relative path
2088 pathInMenu
= m_fileHistory
[i
];
2092 buf
.Printf(s_MRUEntryFormat
, i
+ 1, pathInMenu
.c_str());
2093 wxList
::compatibility_iterator node
= m_fileMenus
.GetFirst();
2096 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2097 menu
->SetLabel(m_idBase
+ i
, buf
);
2098 node
= node
->GetNext();
2104 void wxFileHistory
::RemoveFileFromHistory(size_t i
)
2106 wxCHECK_RET( i
< m_fileHistoryN
,
2107 wxT("invalid index in wxFileHistory::RemoveFileFromHistory") );
2109 // delete the element from the array (could use memmove() too...)
2110 delete [] m_fileHistory
[i
];
2113 for ( j
= i
; j
< m_fileHistoryN
- 1; j
++ )
2115 m_fileHistory
[j
] = m_fileHistory
[j
+ 1];
2118 wxList
::compatibility_iterator node
= m_fileMenus
.GetFirst();
2121 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2123 // shuffle filenames up
2125 for ( j
= i
; j
< m_fileHistoryN
- 1; j
++ )
2127 buf
.Printf(s_MRUEntryFormat
, j
+ 1, m_fileHistory
[j
]);
2128 menu
->SetLabel(m_idBase
+ j
, buf
);
2131 node
= node
->GetNext();
2133 // delete the last menu item which is unused now
2134 wxWindowID lastItemId
= m_idBase
+ m_fileHistoryN
- 1;
2135 if (menu
->FindItem(lastItemId
))
2137 menu
->Delete(lastItemId
);
2140 // delete the last separator too if no more files are left
2141 if ( m_fileHistoryN
== 1 )
2143 wxMenuItemList
::compatibility_iterator node
= menu
->GetMenuItems().GetLast();
2146 wxMenuItem
*menuItem
= node
->GetData();
2147 if ( menuItem
->IsSeparator() )
2149 menu
->Delete(menuItem
);
2151 //else: should we search backwards for the last separator?
2153 //else: menu is empty somehow
2160 wxString wxFileHistory
::GetHistoryFile(size_t i
) const
2163 if ( i
< m_fileHistoryN
)
2165 s
= m_fileHistory
[i
];
2169 wxFAIL_MSG( wxT("bad index in wxFileHistory::GetHistoryFile") );
2175 void wxFileHistory
::UseMenu(wxMenu
*menu
)
2177 if (!m_fileMenus
.Member(menu
))
2178 m_fileMenus
.Append(menu
);
2181 void wxFileHistory
::RemoveMenu(wxMenu
*menu
)
2183 m_fileMenus
.DeleteObject(menu
);
2187 void wxFileHistory
::Load(wxConfigBase
& config
)
2191 buf
.Printf(wxT("file%d"), (int)m_fileHistoryN
+1);
2192 wxString historyFile
;
2193 while ((m_fileHistoryN
< m_fileMaxFiles
) && config
.Read(buf
, &historyFile
) && (historyFile
!= wxT("")))
2195 m_fileHistory
[m_fileHistoryN
] = MYcopystring((const wxChar
*) historyFile
);
2197 buf
.Printf(wxT("file%d"), (int)m_fileHistoryN
+1);
2198 historyFile
= wxT("");
2203 void wxFileHistory
::Save(wxConfigBase
& config
)
2206 for (i
= 0; i
< m_fileMaxFiles
; i
++)
2209 buf
.Printf(wxT("file%d"), (int)i
+1);
2210 if (i
< m_fileHistoryN
)
2211 config
.Write(buf
, wxString(m_fileHistory
[i
]));
2213 config
.Write(buf
, wxEmptyString
);
2216 #endif // wxUSE_CONFIG
2218 void wxFileHistory
::AddFilesToMenu()
2220 if (m_fileHistoryN
> 0)
2222 wxList
::compatibility_iterator node
= m_fileMenus
.GetFirst();
2225 wxMenu
* menu
= (wxMenu
*) node
->GetData();
2226 if (menu
->GetMenuItemCount())
2228 menu
->AppendSeparator();
2232 for (i
= 0; i
< m_fileHistoryN
; i
++)
2234 if (m_fileHistory
[i
])
2237 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
2238 menu
->Append(m_idBase
+i
, buf
);
2241 node
= node
->GetNext();
2246 void wxFileHistory
::AddFilesToMenu(wxMenu
* menu
)
2248 if (m_fileHistoryN
> 0)
2250 if (menu
->GetMenuItemCount())
2252 menu
->AppendSeparator();
2256 for (i
= 0; i
< m_fileHistoryN
; i
++)
2258 if (m_fileHistory
[i
])
2261 buf
.Printf(s_MRUEntryFormat
, i
+1, m_fileHistory
[i
]);
2262 menu
->Append(m_idBase
+i
, buf
);
2268 // ----------------------------------------------------------------------------
2269 // Permits compatibility with existing file formats and functions that
2270 // manipulate files directly
2271 // ----------------------------------------------------------------------------
2273 #if wxUSE_STD_IOSTREAM
2275 bool wxTransferFileToStream(const wxString
& filename
, wxSTD ostream
& stream
)
2277 wxFFile
file(filename
, _T("rb"));
2278 if ( !file
.IsOpened() )
2286 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2290 stream
.write(buf
, nRead
);
2294 while ( !file
.Eof() );
2299 bool wxTransferStreamToFile(wxSTD istream
& stream
, const wxString
& filename
)
2301 wxFFile
file(filename
, _T("wb"));
2302 if ( !file
.IsOpened() )
2308 stream
.read(buf
, WXSIZEOF(buf
));
2309 if ( !stream
.bad() ) // fail may be set on EOF, don't use operator!()
2311 if ( !file
.Write(buf
, stream
.gcount()) )
2315 while ( !stream
.eof() );
2320 #else // !wxUSE_STD_IOSTREAM
2322 bool wxTransferFileToStream(const wxString
& filename
, wxOutputStream
& stream
)
2324 wxFFile
file(filename
, _T("rb"));
2325 if ( !file
.IsOpened() )
2333 nRead
= file
.Read(buf
, WXSIZEOF(buf
));
2337 stream
.Write(buf
, nRead
);
2341 while ( !file
.Eof() );
2346 bool wxTransferStreamToFile(wxInputStream
& stream
, const wxString
& filename
)
2348 wxFFile
file(filename
, _T("wb"));
2349 if ( !file
.IsOpened() )
2355 stream
.Read(buf
, WXSIZEOF(buf
));
2357 const size_t nRead
= stream
.LastRead();
2358 if ( !nRead
|| !file
.Write(buf
, nRead
) )
2361 while ( !stream
.Eof() );
2366 #endif // wxUSE_STD_IOSTREAM/!wxUSE_STD_IOSTREAM
2368 #endif // wxUSE_DOC_VIEW_ARCHITECTURE