1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Document/view classes
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
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"
50 #if wxUSE_PRINTING_ARCHITECTURE
51 #include "wx/prntbase.h"
52 #include "wx/printdlg.h"
55 #include "wx/msgdlg.h"
56 #include "wx/choicdlg.h"
57 #include "wx/docview.h"
58 #include "wx/confbase.h"
63 #if wxUSE_STD_IOSTREAM
64 #include "wx/ioswrap.h"
71 #include "wx/wfstream.h"
74 // ----------------------------------------------------------------------------
76 // ----------------------------------------------------------------------------
78 #if !USE_SHARED_LIBRARY
79 IMPLEMENT_ABSTRACT_CLASS(wxDocument
, wxEvtHandler
)
80 IMPLEMENT_ABSTRACT_CLASS(wxView
, wxEvtHandler
)
81 IMPLEMENT_ABSTRACT_CLASS(wxDocTemplate
, wxObject
)
82 IMPLEMENT_DYNAMIC_CLASS(wxDocManager
, wxEvtHandler
)
83 IMPLEMENT_CLASS(wxDocChildFrame
, wxFrame
)
84 IMPLEMENT_CLASS(wxDocParentFrame
, wxFrame
)
86 #if wxUSE_PRINTING_ARCHITECTURE
87 IMPLEMENT_DYNAMIC_CLASS(wxDocPrintout
, wxPrintout
)
90 IMPLEMENT_CLASS(wxCommand
, wxObject
)
91 IMPLEMENT_DYNAMIC_CLASS(wxCommandProcessor
, wxObject
)
92 IMPLEMENT_DYNAMIC_CLASS(wxFileHistory
, wxObject
)
95 // ----------------------------------------------------------------------------
96 // function prototypes
97 // ----------------------------------------------------------------------------
99 static inline wxString
FindExtension(const wxChar
*path
);
101 // ============================================================================
103 // ============================================================================
105 // ----------------------------------------------------------------------------
107 // ----------------------------------------------------------------------------
109 static wxString
FindExtension(const wxChar
*path
)
112 wxSplitPath(path
, NULL
, NULL
, &ext
);
114 // VZ: extensions are considered not case sensitive - is this really a good
116 return ext
.MakeLower();
119 // ----------------------------------------------------------------------------
120 // Definition of wxDocument
121 // ----------------------------------------------------------------------------
123 wxDocument::wxDocument(wxDocument
*parent
)
125 m_documentModified
= FALSE
;
126 m_documentParent
= parent
;
127 m_documentTemplate
= (wxDocTemplate
*) NULL
;
131 bool wxDocument::DeleteContents()
136 wxDocument::~wxDocument()
140 if (m_commandProcessor
)
141 delete m_commandProcessor
;
143 GetDocumentManager()->RemoveDocument(this);
145 // Not safe to do here, since it'll invoke virtual view functions
146 // expecting to see valid derived objects: and by the time we get here,
147 // we've called destructors higher up.
151 bool wxDocument::Close()
153 if (OnSaveModified())
154 return OnCloseDocument();
159 bool wxDocument::OnCloseDocument()
166 // Note that this implicitly deletes the document when the last view is
168 bool wxDocument::DeleteAllViews()
170 wxNode
*node
= m_documentViews
.First();
173 wxView
*view
= (wxView
*)node
->Data();
177 wxNode
*next
= node
->Next();
179 delete view
; // Deletes node implicitly
185 wxView
*wxDocument::GetFirstView() const
187 if (m_documentViews
.Number() == 0)
188 return (wxView
*) NULL
;
189 return (wxView
*)m_documentViews
.First()->Data();
192 wxDocManager
*wxDocument::GetDocumentManager() const
194 return m_documentTemplate
->GetDocumentManager();
197 bool wxDocument::OnNewDocument()
199 if (!OnSaveModified())
202 if (OnCloseDocument()==FALSE
) return FALSE
;
205 SetDocumentSaved(FALSE
);
208 GetDocumentManager()->MakeDefaultName(name
);
210 SetFilename(name
, TRUE
);
215 bool wxDocument::Save()
219 if (!IsModified()) return TRUE
;
220 if (m_documentFile
== _T("") || !m_savedYet
)
223 ret
= OnSaveDocument(m_documentFile
);
225 SetDocumentSaved(TRUE
);
229 bool wxDocument::SaveAs()
231 wxDocTemplate
*docTemplate
= GetDocumentTemplate();
235 wxString tmp
= wxFileSelector(_("Save as"),
236 docTemplate
->GetDirectory(),
238 docTemplate
->GetDefaultExtension(),
239 docTemplate
->GetFileFilter(),
240 wxSAVE
| wxOVERWRITE_PROMPT
,
241 GetDocumentWindow());
246 wxString
fileName(tmp
);
247 wxString path
, name
, ext
;
248 wxSplitPath(fileName
, & path
, & name
, & ext
);
250 if (ext
.IsEmpty() || ext
== _T(""))
253 fileName
+= docTemplate
->GetDefaultExtension();
256 SetFilename(fileName
);
257 SetTitle(wxFileNameFromPath(fileName
));
259 GetDocumentManager()->AddFileToHistory(fileName
);
261 // Notify the views that the filename has changed
262 wxNode
*node
= m_documentViews
.First();
265 wxView
*view
= (wxView
*)node
->Data();
266 view
->OnChangeFilename();
270 return OnSaveDocument(m_documentFile
);
273 bool wxDocument::OnSaveDocument(const wxString
& file
)
279 if (wxTheApp
->GetAppName() != _T(""))
280 msgTitle
= wxTheApp
->GetAppName();
282 msgTitle
= wxString(_("File error"));
284 #if wxUSE_STD_IOSTREAM
285 ofstream
store(FNSTRINGCAST file
.fn_str());
286 if (store
.fail() || store
.bad())
288 wxFileOutputStream
store(FNSTRINGCAST file
.fn_str());
289 if (store
.LastError() != 0)
292 (void)wxMessageBox(_("Sorry, could not open this file for saving."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
293 GetDocumentWindow());
297 if (SaveObject(store
)==FALSE
)
299 (void)wxMessageBox(_("Sorry, could not save this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
300 GetDocumentWindow());
309 bool wxDocument::OnOpenDocument(const wxString
& file
)
311 if (!OnSaveModified())
315 if (wxTheApp
->GetAppName() != _T(""))
316 msgTitle
= wxTheApp
->GetAppName();
318 msgTitle
= wxString(_("File error"));
320 #if wxUSE_STD_IOSTREAM
321 ifstream
store(FNSTRINGCAST file
.fn_str());
322 if (store
.fail() || store
.bad())
324 wxFileInputStream
store(FNSTRINGCAST file
.fn_str());
325 if (store
.LastError() != 0)
328 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
|wxICON_EXCLAMATION
,
329 GetDocumentWindow());
332 if (LoadObject(store
)==FALSE
)
334 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
|wxICON_EXCLAMATION
,
335 GetDocumentWindow());
338 SetFilename(file
, TRUE
);
347 #if wxUSE_STD_IOSTREAM
348 istream
& wxDocument::LoadObject(istream
& stream
)
353 ostream
& wxDocument::SaveObject(ostream
& stream
)
358 bool wxDocument::LoadObject(wxInputStream
& stream
)
363 bool wxDocument::SaveObject(wxOutputStream
& stream
)
369 bool wxDocument::Revert()
375 // Get title, or filename if no title, else unnamed
376 bool wxDocument::GetPrintableName(wxString
& buf
) const
378 if (m_documentTitle
!= _T(""))
380 buf
= m_documentTitle
;
383 else if (m_documentFile
!= _T(""))
385 buf
= wxFileNameFromPath(m_documentFile
);
395 wxWindow
*wxDocument::GetDocumentWindow() const
397 wxView
*view
= GetFirstView();
399 return view
->GetFrame();
401 return wxTheApp
->GetTopWindow();
404 wxCommandProcessor
*wxDocument::OnCreateCommandProcessor()
406 return new wxCommandProcessor
;
409 // TRUE if safe to close
410 bool wxDocument::OnSaveModified()
415 GetPrintableName(title
);
418 if (wxTheApp
->GetAppName() != _T(""))
419 msgTitle
= wxTheApp
->GetAppName();
421 msgTitle
= wxString(_("Warning"));
424 prompt
.Printf(_("Do you want to save changes to document %s?"),
425 (const wxChar
*)title
);
426 int res
= wxMessageBox(prompt
, msgTitle
,
427 wxYES_NO
|wxCANCEL
|wxICON_QUESTION
,
428 GetDocumentWindow());
434 else if (res
== wxYES
)
436 else if (res
== wxCANCEL
)
442 bool wxDocument::Draw(wxDC
& WXUNUSED(context
))
447 bool wxDocument::AddView(wxView
*view
)
449 if (!m_documentViews
.Member(view
))
451 m_documentViews
.Append(view
);
457 bool wxDocument::RemoveView(wxView
*view
)
459 (void)m_documentViews
.DeleteObject(view
);
464 bool wxDocument::OnCreate(const wxString
& WXUNUSED(path
), long flags
)
466 if (GetDocumentTemplate()->CreateView(this, flags
))
472 // Called after a view is added or removed.
473 // The default implementation deletes the document if
474 // there are no more views.
475 void wxDocument::OnChangedViewList()
477 if (m_documentViews
.Number() == 0)
479 if (OnSaveModified())
486 void wxDocument::UpdateAllViews(wxView
*sender
, wxObject
*hint
)
488 wxNode
*node
= m_documentViews
.First();
491 wxView
*view
= (wxView
*)node
->Data();
492 view
->OnUpdate(sender
, hint
);
497 void wxDocument::SetFilename(const wxString
& filename
, bool notifyViews
)
499 m_documentFile
= filename
;
502 // Notify the views that the filename has changed
503 wxNode
*node
= m_documentViews
.First();
506 wxView
*view
= (wxView
*)node
->Data();
507 view
->OnChangeFilename();
513 // ----------------------------------------------------------------------------
515 // ----------------------------------------------------------------------------
520 m_viewDocument
= (wxDocument
*) NULL
;
523 m_viewFrame
= (wxFrame
*) NULL
;
528 GetDocumentManager()->ActivateView(this, FALSE
, TRUE
);
529 m_viewDocument
->RemoveView(this);
532 // Extend event processing to search the document's event table
533 bool wxView::ProcessEvent(wxEvent
& event
)
535 if ( !GetDocument() || !GetDocument()->ProcessEvent(event
) )
536 return wxEvtHandler::ProcessEvent(event
);
541 void wxView::OnActivateView(bool WXUNUSED(activate
), wxView
*WXUNUSED(activeView
), wxView
*WXUNUSED(deactiveView
))
545 void wxView::OnPrint(wxDC
*dc
, wxObject
*WXUNUSED(info
))
550 void wxView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
))
554 void wxView::OnChangeFilename()
556 if (GetFrame() && GetDocument())
559 GetDocument()->GetPrintableName(name
);
561 GetFrame()->SetTitle(name
);
565 void wxView::SetDocument(wxDocument
*doc
)
567 m_viewDocument
= doc
;
572 bool wxView::Close(bool deleteWindow
)
574 if (OnClose(deleteWindow
))
580 void wxView::Activate(bool activate
)
582 if (GetDocumentManager())
584 OnActivateView(activate
, this, GetDocumentManager()->GetCurrentView());
585 GetDocumentManager()->ActivateView(this, activate
);
589 bool wxView::OnClose(bool WXUNUSED(deleteWindow
))
591 return GetDocument() ? GetDocument()->Close() : TRUE
;
594 #if wxUSE_PRINTING_ARCHITECTURE
595 wxPrintout
*wxView::OnCreatePrintout()
597 return new wxDocPrintout(this);
599 #endif // wxUSE_PRINTING_ARCHITECTURE
601 // ----------------------------------------------------------------------------
603 // ----------------------------------------------------------------------------
605 wxDocTemplate::wxDocTemplate(wxDocManager
*manager
,
606 const wxString
& descr
,
607 const wxString
& filter
,
610 const wxString
& docTypeName
,
611 const wxString
& viewTypeName
,
612 wxClassInfo
*docClassInfo
,
613 wxClassInfo
*viewClassInfo
,
616 m_documentManager
= manager
;
617 m_description
= descr
;
620 m_fileFilter
= filter
;
622 m_docTypeName
= docTypeName
;
623 m_viewTypeName
= viewTypeName
;
624 m_documentManager
->AssociateTemplate(this);
626 m_docClassInfo
= docClassInfo
;
627 m_viewClassInfo
= viewClassInfo
;
630 wxDocTemplate::~wxDocTemplate()
632 m_documentManager
->DisassociateTemplate(this);
635 // Tries to dynamically construct an object of the right class.
636 wxDocument
*wxDocTemplate::CreateDocument(const wxString
& path
, long flags
)
639 return (wxDocument
*) NULL
;
640 wxDocument
*doc
= (wxDocument
*)m_docClassInfo
->CreateObject();
641 doc
->SetFilename(path
);
642 doc
->SetDocumentTemplate(this);
643 GetDocumentManager()->AddDocument(doc
);
644 doc
->SetCommandProcessor(doc
->OnCreateCommandProcessor());
646 if (doc
->OnCreate(path
, flags
))
651 return (wxDocument
*) NULL
;
655 wxView
*wxDocTemplate::CreateView(wxDocument
*doc
, long flags
)
657 if (!m_viewClassInfo
)
658 return (wxView
*) NULL
;
659 wxView
*view
= (wxView
*)m_viewClassInfo
->CreateObject();
660 view
->SetDocument(doc
);
661 if (view
->OnCreate(doc
, flags
))
668 return (wxView
*) NULL
;
672 // The default (very primitive) format detection: check is the extension is
673 // that of the template
674 bool wxDocTemplate::FileMatchesTemplate(const wxString
& path
)
676 return GetDefaultExtension().IsSameAs(FindExtension(path
));
679 // ----------------------------------------------------------------------------
681 // ----------------------------------------------------------------------------
683 BEGIN_EVENT_TABLE(wxDocManager
, wxEvtHandler
)
684 EVT_MENU(wxID_OPEN
, wxDocManager::OnFileOpen
)
685 EVT_MENU(wxID_CLOSE
, wxDocManager::OnFileClose
)
686 EVT_MENU(wxID_REVERT
, wxDocManager::OnFileRevert
)
687 EVT_MENU(wxID_NEW
, wxDocManager::OnFileNew
)
688 EVT_MENU(wxID_SAVE
, wxDocManager::OnFileSave
)
689 EVT_MENU(wxID_SAVEAS
, wxDocManager::OnFileSaveAs
)
690 EVT_MENU(wxID_UNDO
, wxDocManager::OnUndo
)
691 EVT_MENU(wxID_REDO
, wxDocManager::OnRedo
)
692 #if wxUSE_PRINTING_ARCHITECTURE
693 EVT_MENU(wxID_PRINT
, wxDocManager::OnPrint
)
694 EVT_MENU(wxID_PRINT_SETUP
, wxDocManager::OnPrintSetup
)
695 EVT_MENU(wxID_PREVIEW
, wxDocManager::OnPreview
)
699 wxDocManager::wxDocManager(long flags
, bool initialize
)
701 m_defaultDocumentNameCounter
= 1;
703 m_currentView
= (wxView
*) NULL
;
704 m_maxDocsOpen
= 10000;
705 m_fileHistory
= (wxFileHistory
*) NULL
;
710 wxDocManager::~wxDocManager()
714 delete m_fileHistory
;
717 bool wxDocManager::Clear(bool force
)
719 wxNode
*node
= m_docs
.First();
722 wxDocument
*doc
= (wxDocument
*)node
->Data();
723 wxNode
*next
= node
->Next();
725 if (!doc
->Close() && !force
)
728 // Implicitly deletes the document when the last
729 // view is removed (deleted)
730 doc
->DeleteAllViews();
732 // Check document is deleted
733 if (m_docs
.Member(doc
))
736 // This assumes that documents are not connected in
737 // any way, i.e. deleting one document does NOT
741 node
= m_templates
.First();
744 wxDocTemplate
*templ
= (wxDocTemplate
*) node
->Data();
745 wxNode
* next
= node
->Next();
752 bool wxDocManager::Initialize()
754 m_fileHistory
= OnCreateFileHistory();
758 wxFileHistory
*wxDocManager::OnCreateFileHistory()
760 return new wxFileHistory
;
763 void wxDocManager::OnFileClose(wxCommandEvent
& WXUNUSED(event
))
765 wxDocument
*doc
= GetCurrentDocument();
770 doc
->DeleteAllViews();
771 if (m_docs
.Member(doc
))
776 void wxDocManager::OnFileNew(wxCommandEvent
& WXUNUSED(event
))
778 CreateDocument(wxString(""), wxDOC_NEW
);
781 void wxDocManager::OnFileOpen(wxCommandEvent
& WXUNUSED(event
))
783 CreateDocument(wxString(""), 0);
786 void wxDocManager::OnFileRevert(wxCommandEvent
& WXUNUSED(event
))
788 wxDocument
*doc
= GetCurrentDocument();
794 void wxDocManager::OnFileSave(wxCommandEvent
& WXUNUSED(event
))
796 wxDocument
*doc
= GetCurrentDocument();
802 void wxDocManager::OnFileSaveAs(wxCommandEvent
& WXUNUSED(event
))
804 wxDocument
*doc
= GetCurrentDocument();
810 void wxDocManager::OnPrint(wxCommandEvent
& WXUNUSED(event
))
812 #if wxUSE_PRINTING_ARCHITECTURE
813 wxView
*view
= GetCurrentView();
817 wxPrintout
*printout
= view
->OnCreatePrintout();
821 printer
.Print(view
->GetFrame(), printout
, TRUE
);
825 #endif // wxUSE_PRINTING_ARCHITECTURE
828 void wxDocManager::OnPrintSetup(wxCommandEvent
& WXUNUSED(event
))
830 #if wxUSE_PRINTING_ARCHITECTURE
831 wxWindow
*parentWin
= wxTheApp
->GetTopWindow();
832 wxView
*view
= GetCurrentView();
834 parentWin
= view
->GetFrame();
836 wxPrintDialogData data
;
838 wxPrintDialog
printerDialog(parentWin
, &data
);
839 printerDialog
.GetPrintDialogData().SetSetupDialog(TRUE
);
840 printerDialog
.ShowModal();
841 #endif // wxUSE_PRINTING_ARCHITECTURE
844 void wxDocManager::OnPreview(wxCommandEvent
& WXUNUSED(event
))
846 #if wxUSE_PRINTING_ARCHITECTURE
847 wxView
*view
= GetCurrentView();
851 wxPrintout
*printout
= view
->OnCreatePrintout();
854 // Pass two printout objects: for preview, and possible printing.
855 wxPrintPreviewBase
*preview
= (wxPrintPreviewBase
*) NULL
;
856 preview
= new wxPrintPreview(printout
, view
->OnCreatePrintout());
858 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, (wxFrame
*)wxTheApp
->GetTopWindow(), _("Print Preview"),
859 wxPoint(100, 100), wxSize(600, 650));
860 frame
->Centre(wxBOTH
);
864 #endif // wxUSE_PRINTING_ARCHITECTURE
867 void wxDocManager::OnUndo(wxCommandEvent
& WXUNUSED(event
))
869 wxDocument
*doc
= GetCurrentDocument();
872 if (doc
->GetCommandProcessor())
873 doc
->GetCommandProcessor()->Undo();
876 void wxDocManager::OnRedo(wxCommandEvent
& WXUNUSED(event
))
878 wxDocument
*doc
= GetCurrentDocument();
881 if (doc
->GetCommandProcessor())
882 doc
->GetCommandProcessor()->Redo();
885 wxView
*wxDocManager::GetCurrentView() const
888 return m_currentView
;
889 if (m_docs
.Number() == 1)
891 wxDocument
* doc
= (wxDocument
*) m_docs
.First()->Data();
892 return doc
->GetFirstView();
894 return (wxView
*) NULL
;
897 // Extend event processing to search the view's event table
898 bool wxDocManager::ProcessEvent(wxEvent
& event
)
900 wxView
* view
= GetCurrentView();
903 if (view
->ProcessEvent(event
))
906 return wxEvtHandler::ProcessEvent(event
);
909 wxDocument
*wxDocManager::CreateDocument(const wxString
& path
, long flags
)
911 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.Number()];
914 for (i
= 0; i
< m_templates
.Number(); i
++)
916 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Nth(i
)->Data());
917 if (temp
->IsVisible())
926 return (wxDocument
*) NULL
;
929 // If we've reached the max number of docs, close the
931 if (GetDocuments().Number() >= m_maxDocsOpen
)
933 wxDocument
*doc
= (wxDocument
*)GetDocuments().First()->Data();
936 // Implicitly deletes the document when
937 // the last view is deleted
938 doc
->DeleteAllViews();
940 // Check we're really deleted
941 if (m_docs
.Member(doc
))
945 return (wxDocument
*) NULL
;
948 // New document: user chooses a template, unless there's only one.
949 if (flags
& wxDOC_NEW
)
953 wxDocTemplate
*temp
= templates
[0];
955 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
958 newDoc
->SetDocumentName(temp
->GetDocumentName());
959 newDoc
->SetDocumentTemplate(temp
);
960 newDoc
->OnNewDocument();
965 wxDocTemplate
*temp
= SelectDocumentType(templates
, n
);
969 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
972 newDoc
->SetDocumentName(temp
->GetDocumentName());
973 newDoc
->SetDocumentTemplate(temp
);
974 newDoc
->OnNewDocument();
979 return (wxDocument
*) NULL
;
983 wxDocTemplate
*temp
= (wxDocTemplate
*) NULL
;
985 wxString
path2(_T(""));
989 if (flags
& wxDOC_SILENT
)
990 temp
= FindTemplateForPath(path2
);
992 temp
= SelectDocumentPath(templates
, n
, path2
, flags
);
998 wxDocument
*newDoc
= temp
->CreateDocument(path2
, flags
);
1001 newDoc
->SetDocumentName(temp
->GetDocumentName());
1002 newDoc
->SetDocumentTemplate(temp
);
1003 if (!newDoc
->OnOpenDocument(path2
))
1006 return (wxDocument
*) NULL
;
1008 AddFileToHistory(path2
);
1013 return (wxDocument
*) NULL
;
1016 wxView
*wxDocManager::CreateView(wxDocument
*doc
, long flags
)
1018 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.Number()];
1021 for (i
= 0; i
< m_templates
.Number(); i
++)
1023 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Nth(i
)->Data());
1024 if (temp
->IsVisible())
1026 if (temp
->GetDocumentName() == doc
->GetDocumentName())
1028 templates
[n
] = temp
;
1036 return (wxView
*) NULL
;
1040 wxDocTemplate
*temp
= templates
[0];
1042 wxView
*view
= temp
->CreateView(doc
, flags
);
1044 view
->SetViewName(temp
->GetViewName());
1048 wxDocTemplate
*temp
= SelectViewType(templates
, n
);
1052 wxView
*view
= temp
->CreateView(doc
, flags
);
1054 view
->SetViewName(temp
->GetViewName());
1058 return (wxView
*) NULL
;
1061 // Not yet implemented
1062 void wxDocManager::DeleteTemplate(wxDocTemplate
*WXUNUSED(temp
), long WXUNUSED(flags
))
1066 // Not yet implemented
1067 bool wxDocManager::FlushDoc(wxDocument
*WXUNUSED(doc
))
1072 wxDocument
*wxDocManager::GetCurrentDocument() const
1075 return m_currentView
->GetDocument();
1077 return (wxDocument
*) NULL
;
1080 // Make a default document name
1081 bool wxDocManager::MakeDefaultName(wxString
& name
)
1083 name
.Printf(_("unnamed%d"), m_defaultDocumentNameCounter
);
1084 m_defaultDocumentNameCounter
++;
1089 // Not yet implemented
1090 wxDocTemplate
*wxDocManager::MatchTemplate(const wxString
& WXUNUSED(path
))
1092 return (wxDocTemplate
*) NULL
;
1095 // File history management
1096 void wxDocManager::AddFileToHistory(const wxString
& file
)
1099 m_fileHistory
->AddFileToHistory(file
);
1102 wxString
wxDocManager::GetHistoryFile(int i
) const
1107 histFile
= m_fileHistory
->GetHistoryFile(i
);
1112 void wxDocManager::FileHistoryUseMenu(wxMenu
*menu
)
1115 m_fileHistory
->UseMenu(menu
);
1118 void wxDocManager::FileHistoryRemoveMenu(wxMenu
*menu
)
1121 m_fileHistory
->RemoveMenu(menu
);
1125 void wxDocManager::FileHistoryLoad(wxConfigBase
& config
)
1128 m_fileHistory
->Load(config
);
1131 void wxDocManager::FileHistorySave(wxConfigBase
& config
)
1134 m_fileHistory
->Save(config
);
1138 void wxDocManager::FileHistoryAddFilesToMenu(wxMenu
* menu
)
1141 m_fileHistory
->AddFilesToMenu(menu
);
1144 void wxDocManager::FileHistoryAddFilesToMenu()
1147 m_fileHistory
->AddFilesToMenu();
1150 int wxDocManager::GetNoHistoryFiles() const
1153 return m_fileHistory
->GetNoHistoryFiles();
1159 // Find out the document template via matching in the document file format
1160 // against that of the template
1161 wxDocTemplate
*wxDocManager::FindTemplateForPath(const wxString
& path
)
1163 wxDocTemplate
*theTemplate
= (wxDocTemplate
*) NULL
;
1165 // Find the template which this extension corresponds to
1167 for (i
= 0; i
< m_templates
.Number(); i
++)
1169 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Nth(i
)->Data();
1170 if ( temp
->FileMatchesTemplate(path
) )
1179 // Prompts user to open a file, using file specs in templates.
1180 // How to implement in wxWindows? Must extend the file selector
1181 // dialog or implement own; OR match the extension to the
1182 // template extension.
1184 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**templates
,
1188 int WXUNUSED(noTemplates
),
1191 long WXUNUSED(flags
),
1192 bool WXUNUSED(save
))
1194 // We can only have multiple filters in Windows
1199 for (i
= 0; i
< noTemplates
; i
++)
1201 if (templates
[i
]->IsVisible())
1203 // add a '|' to separate this filter from the previous one
1204 if ( !descrBuf
.IsEmpty() )
1205 descrBuf
<< _T('|');
1207 descrBuf
<< templates
[i
]->GetDescription()
1208 << _T(" (") << templates
[i
]->GetFileFilter() << _T(") |")
1209 << templates
[i
]->GetFileFilter();
1213 wxString descrBuf
= _T("*.*");
1216 int FilterIndex
= 0;
1217 wxString pathTmp
= wxFileSelectorEx(_("Select a file"),
1223 wxTheApp
->GetTopWindow());
1225 if (!pathTmp
.IsEmpty())
1228 wxString theExt
= FindExtension(path
);
1230 return (wxDocTemplate
*) NULL
;
1232 // This is dodgy in that we're selecting the template on the
1233 // basis of the file extension, which may not be a standard
1234 // one. We really want to know exactly which template was
1235 // chosen by using a more advanced file selector.
1236 wxDocTemplate
*theTemplate
= FindTemplateForPath(path
);
1238 theTemplate
= templates
[FilterIndex
];
1245 return (wxDocTemplate
*) NULL
;
1248 // In all other windowing systems, until we have more advanced
1249 // file selectors, we must select the document type (template) first, and
1250 // _then_ pop up the file selector.
1251 wxDocTemplate
*temp
= SelectDocumentType(templates
, noTemplates
);
1253 return (wxDocTemplate
*) NULL
;
1255 wxChar
*pathTmp
= wxFileSelector(_("Select a file"), _T(""), _T(""),
1256 temp
->GetDefaultExtension(),
1257 temp
->GetFileFilter(),
1258 0, wxTheApp
->GetTopWindow());
1266 return (wxDocTemplate
*) NULL
;
1270 wxDocTemplate
*wxDocManager::SelectDocumentType(wxDocTemplate
**templates
,
1273 wxChar
**strings
= new wxChar
*[noTemplates
];
1274 wxChar
**data
= new wxChar
*[noTemplates
];
1277 for (i
= 0; i
< noTemplates
; i
++)
1279 if (templates
[i
]->IsVisible())
1281 strings
[n
] = WXSTRINGCAST templates
[i
]->m_description
;
1282 data
[n
] = (wxChar
*)templates
[i
];
1290 return (wxDocTemplate
*) NULL
;
1294 wxDocTemplate
*temp
= (wxDocTemplate
*)data
[0];
1300 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData(_("Select a document template"), _("Templates"), n
,
1301 strings
, (char **)data
);
1307 wxDocTemplate
*wxDocManager::SelectViewType(wxDocTemplate
**templates
,
1310 wxChar
**strings
= new wxChar
*[noTemplates
];
1311 wxChar
**data
= new wxChar
*[noTemplates
];
1314 for (i
= 0; i
< noTemplates
; i
++)
1316 if (templates
[i
]->IsVisible() && (templates
[i
]->GetViewName() != _T("")))
1318 strings
[n
] = WXSTRINGCAST templates
[i
]->m_viewTypeName
;
1319 data
[n
] = (wxChar
*)templates
[i
];
1323 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData(_("Select a document view"), _("Views"), n
,
1324 strings
, (char **)data
);
1330 void wxDocManager::AssociateTemplate(wxDocTemplate
*temp
)
1332 if (!m_templates
.Member(temp
))
1333 m_templates
.Append(temp
);
1336 void wxDocManager::DisassociateTemplate(wxDocTemplate
*temp
)
1338 m_templates
.DeleteObject(temp
);
1341 // Add and remove a document from the manager's list
1342 void wxDocManager::AddDocument(wxDocument
*doc
)
1344 if (!m_docs
.Member(doc
))
1348 void wxDocManager::RemoveDocument(wxDocument
*doc
)
1350 m_docs
.DeleteObject(doc
);
1353 // Views or windows should inform the document manager
1354 // when a view is going in or out of focus
1355 void wxDocManager::ActivateView(wxView
*view
, bool activate
, bool WXUNUSED(deleting
))
1357 // If we're deactiving, and if we're not actually deleting the view, then
1358 // don't reset the current view because we may be going to
1359 // a window without a view.
1360 // WHAT DID I MEAN BY THAT EXACTLY?
1364 if (m_currentView == view)
1365 m_currentView = NULL;
1371 m_currentView
= view
;
1373 m_currentView
= (wxView
*) NULL
;
1377 // ----------------------------------------------------------------------------
1378 // Default document child frame
1379 // ----------------------------------------------------------------------------
1381 BEGIN_EVENT_TABLE(wxDocChildFrame
, wxFrame
)
1382 EVT_ACTIVATE(wxDocChildFrame::OnActivate
)
1383 EVT_CLOSE(wxDocChildFrame::OnCloseWindow
)
1386 wxDocChildFrame::wxDocChildFrame(wxDocument
*doc
,
1390 const wxString
& title
,
1394 const wxString
& name
)
1395 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1397 m_childDocument
= doc
;
1400 view
->SetFrame(this);
1403 wxDocChildFrame::~wxDocChildFrame()
1407 // Extend event processing to search the view's event table
1408 bool wxDocChildFrame::ProcessEvent(wxEvent
& event
)
1411 m_childView
->Activate(TRUE
);
1413 if ( !m_childView
|| ! m_childView
->ProcessEvent(event
) )
1415 // Only hand up to the parent if it's a menu command
1416 if (!event
.IsKindOf(CLASSINFO(wxCommandEvent
)) || !GetParent() || !GetParent()->ProcessEvent(event
))
1417 return wxEvtHandler::ProcessEvent(event
);
1425 void wxDocChildFrame::OnActivate(wxActivateEvent
& event
)
1427 wxFrame::OnActivate(event
);
1430 m_childView
->Activate(event
.GetActive());
1433 void wxDocChildFrame::OnCloseWindow(wxCloseEvent
& event
)
1438 if (!event
.CanVeto())
1439 ans
= TRUE
; // Must delete.
1441 ans
= m_childView
->Close(FALSE
); // FALSE means don't delete associated window
1445 m_childView
->Activate(FALSE
);
1447 m_childView
= (wxView
*) NULL
;
1448 m_childDocument
= (wxDocument
*) NULL
;
1459 // ----------------------------------------------------------------------------
1460 // Default parent frame
1461 // ----------------------------------------------------------------------------
1463 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1464 EVT_MENU(wxID_EXIT
, wxDocParentFrame::OnExit
)
1465 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE9
, wxDocParentFrame::OnMRUFile
)
1466 EVT_CLOSE(wxDocParentFrame::OnCloseWindow
)
1469 wxDocParentFrame::wxDocParentFrame(wxDocManager
*manager
,
1472 const wxString
& title
,
1476 const wxString
& name
)
1477 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1479 m_docManager
= manager
;
1482 void wxDocParentFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
1487 void wxDocParentFrame::OnMRUFile(wxCommandEvent
& event
)
1489 wxString
f(m_docManager
->GetHistoryFile(event
.GetSelection() - wxID_FILE1
));
1491 (void)m_docManager
->CreateDocument(f
, wxDOC_SILENT
);
1494 // Extend event processing to search the view's event table
1495 bool wxDocParentFrame::ProcessEvent(wxEvent
& event
)
1497 // Try the document manager, then do default processing
1498 if (!m_docManager
|| !m_docManager
->ProcessEvent(event
))
1499 return wxEvtHandler::ProcessEvent(event
);
1504 // Define the behaviour for the frame closing
1505 // - must delete all frames except for the main one.
1506 void wxDocParentFrame::OnCloseWindow(wxCloseEvent
& event
)
1508 if (m_docManager
->Clear(!event
.CanVeto()))
1516 #if wxUSE_PRINTING_ARCHITECTURE
1518 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
)
1519 : wxPrintout(WXSTRINGCAST title
)
1521 m_printoutView
= view
;
1524 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
1528 // Get the logical pixels per inch of screen and printer
1529 int ppiScreenX
, ppiScreenY
;
1530 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
1531 int ppiPrinterX
, ppiPrinterY
;
1532 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
1534 // This scales the DC so that the printout roughly represents the
1535 // the screen scaling. The text point size _should_ be the right size
1536 // but in fact is too small for some reason. This is a detail that will
1537 // need to be addressed at some point but can be fudged for the
1539 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
1541 // Now we have to check in case our real page size is reduced
1542 // (e.g. because we're drawing to a print preview memory DC)
1543 int pageWidth
, pageHeight
;
1545 dc
->GetSize(&w
, &h
);
1546 GetPageSizePixels(&pageWidth
, &pageHeight
);
1548 // If printer pageWidth == current DC width, then this doesn't
1549 // change. But w might be the preview bitmap width, so scale down.
1550 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
1551 dc
->SetUserScale(overallScale
, overallScale
);
1555 m_printoutView
->OnDraw(dc
);
1560 bool wxDocPrintout::HasPage(int pageNum
)
1562 return (pageNum
== 1);
1565 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
1567 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
1573 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
1581 #endif // wxUSE_PRINTING_ARCHITECTURE
1583 // ----------------------------------------------------------------------------
1584 // Command processing framework
1585 // ----------------------------------------------------------------------------
1587 wxCommand::wxCommand(bool canUndoIt
, const wxString
& name
)
1589 m_canUndo
= canUndoIt
;
1590 m_commandName
= name
;
1593 wxCommand::~wxCommand()
1597 // Command processor
1598 wxCommandProcessor::wxCommandProcessor(int maxCommands
)
1600 m_maxNoCommands
= maxCommands
;
1601 m_currentCommand
= (wxNode
*) NULL
;
1602 m_commandEditMenu
= (wxMenu
*) NULL
;
1605 wxCommandProcessor::~wxCommandProcessor()
1610 // Pass a command to the processor. The processor calls Do();
1611 // if successful, is appended to the command history unless
1612 // storeIt is FALSE.
1613 bool wxCommandProcessor::Submit(wxCommand
*command
, bool storeIt
)
1615 bool success
= command
->Do();
1616 if (success
&& storeIt
)
1618 if (m_commands
.Number() == m_maxNoCommands
)
1620 wxNode
*firstNode
= m_commands
.First();
1621 wxCommand
*firstCommand
= (wxCommand
*)firstNode
->Data();
1622 delete firstCommand
;
1626 // Correct a bug: we must chop off the current 'branch'
1627 // so that we're at the end of the command list.
1628 if (!m_currentCommand
)
1632 wxNode
*node
= m_currentCommand
->Next();
1635 wxNode
*next
= node
->Next();
1636 delete (wxCommand
*)node
->Data();
1642 m_commands
.Append(command
);
1643 m_currentCommand
= m_commands
.Last();
1649 bool wxCommandProcessor::Undo()
1651 if (m_currentCommand
)
1653 wxCommand
*command
= (wxCommand
*)m_currentCommand
->Data();
1654 if (command
->CanUndo())
1656 bool success
= command
->Undo();
1659 m_currentCommand
= m_currentCommand
->Previous();
1668 bool wxCommandProcessor::Redo()
1670 wxCommand
*redoCommand
= (wxCommand
*) NULL
;
1671 wxNode
*redoNode
= (wxNode
*) NULL
;
1672 if (m_currentCommand
&& m_currentCommand
->Next())
1674 redoCommand
= (wxCommand
*)m_currentCommand
->Next()->Data();
1675 redoNode
= m_currentCommand
->Next();
1679 if (m_commands
.Number() > 0)
1681 redoCommand
= (wxCommand
*)m_commands
.First()->Data();
1682 redoNode
= m_commands
.First();
1688 bool success
= redoCommand
->Do();
1691 m_currentCommand
= redoNode
;
1699 bool wxCommandProcessor::CanUndo() const
1701 if (m_currentCommand
)
1702 return ((wxCommand
*)m_currentCommand
->Data())->CanUndo();
1706 bool wxCommandProcessor::CanRedo() const
1708 if ((m_currentCommand
!= (wxNode
*) NULL
) && (m_currentCommand
->Next() == (wxNode
*) NULL
))
1711 if ((m_currentCommand
!= (wxNode
*) NULL
) && (m_currentCommand
->Next() != (wxNode
*) NULL
))
1714 if ((m_currentCommand
== (wxNode
*) NULL
) && (m_commands
.Number() > 0))
1720 void wxCommandProcessor::Initialize()
1722 m_currentCommand
= m_commands
.Last();
1726 void wxCommandProcessor::SetMenuStrings()
1728 if (m_commandEditMenu
)
1731 if (m_currentCommand
)
1733 wxCommand
*command
= (wxCommand
*)m_currentCommand
->Data();
1734 wxString
commandName(command
->GetName());
1735 if (commandName
== _T("")) commandName
= _("Unnamed command");
1736 bool canUndo
= command
->CanUndo();
1738 buf
= wxString(_("&Undo ")) + commandName
;
1740 buf
= wxString(_("Can't &Undo ")) + commandName
;
1742 m_commandEditMenu
->SetLabel(wxID_UNDO
, buf
);
1743 m_commandEditMenu
->Enable(wxID_UNDO
, canUndo
);
1745 // We can redo, if we're not at the end of the history.
1746 if (m_currentCommand
->Next())
1748 wxCommand
*redoCommand
= (wxCommand
*)m_currentCommand
->Next()->Data();
1749 wxString
redoCommandName(redoCommand
->GetName());
1750 if (redoCommandName
== _T("")) redoCommandName
= _("Unnamed command");
1751 buf
= wxString(_("&Redo ")) + redoCommandName
;
1752 m_commandEditMenu
->SetLabel(wxID_REDO
, buf
);
1753 m_commandEditMenu
->Enable(wxID_REDO
, TRUE
);
1757 m_commandEditMenu
->SetLabel(wxID_REDO
, _("&Redo"));
1758 m_commandEditMenu
->Enable(wxID_REDO
, FALSE
);
1763 m_commandEditMenu
->SetLabel(wxID_UNDO
, _("&Undo"));
1764 m_commandEditMenu
->Enable(wxID_UNDO
, FALSE
);
1766 if (m_commands
.Number() == 0)
1768 m_commandEditMenu
->SetLabel(wxID_REDO
, _("&Redo"));
1769 m_commandEditMenu
->Enable(wxID_REDO
, FALSE
);
1773 // currentCommand is NULL but there are commands: this means that
1774 // we've undone to the start of the list, but can redo the first.
1775 wxCommand
*redoCommand
= (wxCommand
*)m_commands
.First()->Data();
1776 wxString
redoCommandName(redoCommand
->GetName());
1777 if (redoCommandName
== _T("")) redoCommandName
= _("Unnamed command");
1778 buf
= wxString(_("&Redo ")) + redoCommandName
;
1779 m_commandEditMenu
->SetLabel(wxID_REDO
, buf
);
1780 m_commandEditMenu
->Enable(wxID_REDO
, TRUE
);
1786 void wxCommandProcessor::ClearCommands()
1788 wxNode
*node
= m_commands
.First();
1791 wxCommand
*command
= (wxCommand
*)node
->Data();
1794 node
= m_commands
.First();
1796 m_currentCommand
= (wxNode
*) NULL
;
1799 // ----------------------------------------------------------------------------
1800 // File history processor
1801 // ----------------------------------------------------------------------------
1803 wxFileHistory::wxFileHistory(int maxFiles
)
1805 m_fileMaxFiles
= maxFiles
;
1807 m_fileHistory
= new wxChar
*[m_fileMaxFiles
];
1810 wxFileHistory::~wxFileHistory()
1813 for (i
= 0; i
< m_fileHistoryN
; i
++)
1814 delete[] m_fileHistory
[i
];
1815 delete[] m_fileHistory
;
1818 // File history management
1819 void wxFileHistory::AddFileToHistory(const wxString
& file
)
1822 // Check we don't already have this file
1823 for (i
= 0; i
< m_fileHistoryN
; i
++)
1825 if (m_fileHistory
[i
] && wxString(m_fileHistory
[i
]) == file
)
1829 // Add to the project file history:
1830 // Move existing files (if any) down so we can insert file at beginning.
1832 // First delete filename that has popped off the end of the array (if any)
1833 if (m_fileHistoryN
== m_fileMaxFiles
)
1835 delete[] m_fileHistory
[m_fileMaxFiles
-1];
1836 m_fileHistory
[m_fileMaxFiles
-1] = (wxChar
*) NULL
;
1838 if (m_fileHistoryN
< m_fileMaxFiles
)
1840 wxNode
* node
= m_fileMenus
.First();
1843 wxMenu
* menu
= (wxMenu
*) node
->Data();
1844 if (m_fileHistoryN
== 0)
1845 menu
->AppendSeparator();
1846 menu
->Append(wxID_FILE1
+m_fileHistoryN
, _("[EMPTY]"));
1847 node
= node
->Next();
1851 // Shuffle filenames down
1852 for (i
= (m_fileHistoryN
-1); i
> 0; i
--)
1854 m_fileHistory
[i
] = m_fileHistory
[i
-1];
1856 m_fileHistory
[0] = copystring(file
);
1858 for (i
= 0; i
< m_fileHistoryN
; i
++)
1859 if (m_fileHistory
[i
])
1862 buf
.Printf(_T("&%d %s"), i
+1, m_fileHistory
[i
]);
1863 wxNode
* node
= m_fileMenus
.First();
1866 wxMenu
* menu
= (wxMenu
*) node
->Data();
1867 menu
->SetLabel(wxID_FILE1
+i
, buf
);
1868 node
= node
->Next();
1873 wxString
wxFileHistory::GetHistoryFile(int i
) const
1875 if (i
< m_fileHistoryN
)
1876 return wxString(m_fileHistory
[i
]);
1878 return wxString("");
1881 void wxFileHistory::UseMenu(wxMenu
*menu
)
1883 if (!m_fileMenus
.Member(menu
))
1884 m_fileMenus
.Append(menu
);
1887 void wxFileHistory::RemoveMenu(wxMenu
*menu
)
1889 m_fileMenus
.DeleteObject(menu
);
1893 void wxFileHistory::Load(wxConfigBase
& config
)
1897 buf
.Printf(_T("file%d"), m_fileHistoryN
+1);
1898 wxString historyFile
;
1899 while ((m_fileHistoryN
<= m_fileMaxFiles
) && config
.Read(buf
, &historyFile
) && (historyFile
!= _T("")))
1901 m_fileHistory
[m_fileHistoryN
] = copystring((const wxChar
*) historyFile
);
1903 buf
.Printf(_T("file%d"), m_fileHistoryN
+1);
1909 void wxFileHistory::Save(wxConfigBase
& config
)
1912 for (i
= 0; i
< m_fileHistoryN
; i
++)
1915 buf
.Printf(_T("file%d"), i
+1);
1916 config
.Write(buf
, wxString(m_fileHistory
[i
]));
1919 #endif // wxUSE_CONFIG
1921 void wxFileHistory::AddFilesToMenu()
1923 if (m_fileHistoryN
> 0)
1925 wxNode
* node
= m_fileMenus
.First();
1928 wxMenu
* menu
= (wxMenu
*) node
->Data();
1929 menu
->AppendSeparator();
1931 for (i
= 0; i
< m_fileHistoryN
; i
++)
1933 if (m_fileHistory
[i
])
1936 buf
.Printf(_T("&%d %s"), i
+1, m_fileHistory
[i
]);
1937 menu
->Append(wxID_FILE1
+i
, buf
);
1940 node
= node
->Next();
1945 void wxFileHistory::AddFilesToMenu(wxMenu
* menu
)
1947 if (m_fileHistoryN
> 0)
1949 menu
->AppendSeparator();
1951 for (i
= 0; i
< m_fileHistoryN
; i
++)
1953 if (m_fileHistory
[i
])
1956 buf
.Printf(_T("&%d %s"), i
+1, m_fileHistory
[i
]);
1957 menu
->Append(wxID_FILE1
+i
, buf
);
1963 // ----------------------------------------------------------------------------
1964 // Permits compatibility with existing file formats and functions that
1965 // manipulate files directly
1966 // ----------------------------------------------------------------------------
1968 #if wxUSE_STD_IOSTREAM
1969 bool wxTransferFileToStream(const wxString
& filename
, ostream
& stream
)
1974 if ((fd1
= fopen (filename
.fn_str(), "rb")) == NULL
)
1977 while ((ch
= getc (fd1
)) != EOF
)
1978 stream
<< (unsigned char)ch
;
1984 bool wxTransferStreamToFile(istream
& stream
, const wxString
& filename
)
1989 if ((fd1
= fopen (filename
.fn_str(), "wb")) == NULL
)
1994 while (!stream
.eof())
2005 #endif // wxUSE_DOC_VIEW_ARCHITECTURE