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"
49 #if wxUSE_PRINTING_ARCHITECTURE
50 #include "wx/prntbase.h"
51 #include "wx/printdlg.h"
54 #include "wx/msgdlg.h"
55 #include "wx/choicdlg.h"
56 #include "wx/docview.h"
57 #include "wx/confbase.h"
62 #include "wx/ioswrap.h"
70 // ----------------------------------------------------------------------------
72 // ----------------------------------------------------------------------------
74 #if !USE_SHARED_LIBRARY
75 IMPLEMENT_ABSTRACT_CLASS(wxDocument
, wxEvtHandler
)
76 IMPLEMENT_ABSTRACT_CLASS(wxView
, wxEvtHandler
)
77 IMPLEMENT_ABSTRACT_CLASS(wxDocTemplate
, wxObject
)
78 IMPLEMENT_DYNAMIC_CLASS(wxDocManager
, wxEvtHandler
)
79 IMPLEMENT_CLASS(wxDocChildFrame
, wxFrame
)
80 IMPLEMENT_CLASS(wxDocParentFrame
, wxFrame
)
82 #if wxUSE_PRINTING_ARCHITECTURE
83 IMPLEMENT_DYNAMIC_CLASS(wxDocPrintout
, wxPrintout
)
86 IMPLEMENT_CLASS(wxCommand
, wxObject
)
87 IMPLEMENT_DYNAMIC_CLASS(wxCommandProcessor
, wxObject
)
88 IMPLEMENT_DYNAMIC_CLASS(wxFileHistory
, wxObject
)
91 // ----------------------------------------------------------------------------
92 // function prototypes
93 // ----------------------------------------------------------------------------
95 static inline wxString
FindExtension(const wxChar
*path
);
97 // ============================================================================
99 // ============================================================================
101 // ----------------------------------------------------------------------------
103 // ----------------------------------------------------------------------------
105 static wxString
FindExtension(const wxChar
*path
)
108 wxSplitPath(path
, NULL
, NULL
, &ext
);
110 // VZ: extensions are considered not case sensitive - is this really a good
112 return ext
.MakeLower();
115 // ----------------------------------------------------------------------------
116 // Definition of wxDocument
117 // ----------------------------------------------------------------------------
119 wxDocument::wxDocument(wxDocument
*parent
)
121 m_documentModified
= FALSE
;
122 m_documentParent
= parent
;
123 m_documentTemplate
= (wxDocTemplate
*) NULL
;
127 bool wxDocument::DeleteContents()
132 wxDocument::~wxDocument()
136 if (m_commandProcessor
)
137 delete m_commandProcessor
;
139 GetDocumentManager()->RemoveDocument(this);
141 // Not safe to do here, since it'll invoke virtual view functions
142 // expecting to see valid derived objects: and by the time we get here,
143 // we've called destructors higher up.
147 bool wxDocument::Close()
149 if (OnSaveModified())
150 return OnCloseDocument();
155 bool wxDocument::OnCloseDocument()
162 // Note that this implicitly deletes the document when the last view is
164 bool wxDocument::DeleteAllViews()
166 wxNode
*node
= m_documentViews
.First();
169 wxView
*view
= (wxView
*)node
->Data();
173 wxNode
*next
= node
->Next();
175 delete view
; // Deletes node implicitly
181 wxView
*wxDocument::GetFirstView() const
183 if (m_documentViews
.Number() == 0)
184 return (wxView
*) NULL
;
185 return (wxView
*)m_documentViews
.First()->Data();
188 wxDocManager
*wxDocument::GetDocumentManager() const
190 return m_documentTemplate
->GetDocumentManager();
193 bool wxDocument::OnNewDocument()
195 if (!OnSaveModified())
198 if (OnCloseDocument()==FALSE
) return FALSE
;
201 SetDocumentSaved(FALSE
);
204 GetDocumentManager()->MakeDefaultName(name
);
206 SetFilename(name
, TRUE
);
211 bool wxDocument::Save()
215 if (!IsModified()) return TRUE
;
216 if (m_documentFile
== _T("") || !m_savedYet
)
219 ret
= OnSaveDocument(m_documentFile
);
221 SetDocumentSaved(TRUE
);
225 bool wxDocument::SaveAs()
227 wxDocTemplate
*docTemplate
= GetDocumentTemplate();
231 wxString tmp
= wxFileSelector(_("Save as"),
232 docTemplate
->GetDirectory(),
234 docTemplate
->GetDefaultExtension(),
235 docTemplate
->GetFileFilter(),
236 wxSAVE
| wxOVERWRITE_PROMPT
,
237 GetDocumentWindow());
242 wxString
fileName(tmp
);
243 wxString path
, name
, ext
;
244 wxSplitPath(fileName
, & path
, & name
, & ext
);
246 if (ext
.IsEmpty() || ext
== _T(""))
249 fileName
+= docTemplate
->GetDefaultExtension();
252 SetFilename(fileName
);
253 SetTitle(wxFileNameFromPath(fileName
));
255 GetDocumentManager()->AddFileToHistory(fileName
);
257 // Notify the views that the filename has changed
258 wxNode
*node
= m_documentViews
.First();
261 wxView
*view
= (wxView
*)node
->Data();
262 view
->OnChangeFilename();
266 return OnSaveDocument(m_documentFile
);
269 bool wxDocument::OnSaveDocument(const wxString
& file
)
275 if (wxTheApp
->GetAppName() != _T(""))
276 msgTitle
= wxTheApp
->GetAppName();
278 msgTitle
= wxString(_("File error"));
280 ofstream
store(file
.fn_str());
281 if (store
.fail() || store
.bad())
283 (void)wxMessageBox(_("Sorry, could not open this file for saving."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
284 GetDocumentWindow());
288 if (SaveObject(store
)==FALSE
)
290 (void)wxMessageBox(_("Sorry, could not save this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
291 GetDocumentWindow());
300 bool wxDocument::OnOpenDocument(const wxString
& file
)
302 if (!OnSaveModified())
306 if (wxTheApp
->GetAppName() != _T(""))
307 msgTitle
= wxTheApp
->GetAppName();
309 msgTitle
= wxString(_("File error"));
311 ifstream
store(file
.fn_str());
312 if (store
.fail() || store
.bad())
314 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
|wxICON_EXCLAMATION
,
315 GetDocumentWindow());
318 if (LoadObject(store
)==FALSE
)
320 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
|wxICON_EXCLAMATION
,
321 GetDocumentWindow());
324 SetFilename(file
, TRUE
);
333 istream
& wxDocument::LoadObject(istream
& stream
)
335 // wxObject::LoadObject(stream);
340 ostream
& wxDocument::SaveObject(ostream
& stream
)
342 // wxObject::SaveObject(stream);
347 bool wxDocument::Revert()
353 // Get title, or filename if no title, else unnamed
354 bool wxDocument::GetPrintableName(wxString
& buf
) const
356 if (m_documentTitle
!= _T(""))
358 buf
= m_documentTitle
;
361 else if (m_documentFile
!= _T(""))
363 buf
= wxFileNameFromPath(m_documentFile
);
373 wxWindow
*wxDocument::GetDocumentWindow() const
375 wxView
*view
= GetFirstView();
377 return view
->GetFrame();
379 return wxTheApp
->GetTopWindow();
382 wxCommandProcessor
*wxDocument::OnCreateCommandProcessor()
384 return new wxCommandProcessor
;
387 // TRUE if safe to close
388 bool wxDocument::OnSaveModified()
393 GetPrintableName(title
);
396 if (wxTheApp
->GetAppName() != _T(""))
397 msgTitle
= wxTheApp
->GetAppName();
399 msgTitle
= wxString(_("Warning"));
402 prompt
.Printf(_("Do you want to save changes to document %s?"),
403 (const wxChar
*)title
);
404 int res
= wxMessageBox(prompt
, msgTitle
,
405 wxYES_NO
|wxCANCEL
|wxICON_QUESTION
,
406 GetDocumentWindow());
412 else if (res
== wxYES
)
414 else if (res
== wxCANCEL
)
420 bool wxDocument::Draw(wxDC
& WXUNUSED(context
))
425 bool wxDocument::AddView(wxView
*view
)
427 if (!m_documentViews
.Member(view
))
429 m_documentViews
.Append(view
);
435 bool wxDocument::RemoveView(wxView
*view
)
437 (void)m_documentViews
.DeleteObject(view
);
442 bool wxDocument::OnCreate(const wxString
& WXUNUSED(path
), long flags
)
444 if (GetDocumentTemplate()->CreateView(this, flags
))
450 // Called after a view is added or removed.
451 // The default implementation deletes the document if
452 // there are no more views.
453 void wxDocument::OnChangedViewList()
455 if (m_documentViews
.Number() == 0)
457 if (OnSaveModified())
464 void wxDocument::UpdateAllViews(wxView
*sender
, wxObject
*hint
)
466 wxNode
*node
= m_documentViews
.First();
469 wxView
*view
= (wxView
*)node
->Data();
470 view
->OnUpdate(sender
, hint
);
475 void wxDocument::SetFilename(const wxString
& filename
, bool notifyViews
)
477 m_documentFile
= filename
;
480 // Notify the views that the filename has changed
481 wxNode
*node
= m_documentViews
.First();
484 wxView
*view
= (wxView
*)node
->Data();
485 view
->OnChangeFilename();
491 // ----------------------------------------------------------------------------
493 // ----------------------------------------------------------------------------
498 m_viewDocument
= (wxDocument
*) NULL
;
501 m_viewFrame
= (wxFrame
*) NULL
;
506 GetDocumentManager()->ActivateView(this, FALSE
, TRUE
);
507 m_viewDocument
->RemoveView(this);
510 // Extend event processing to search the document's event table
511 bool wxView::ProcessEvent(wxEvent
& event
)
513 if ( !GetDocument() || !GetDocument()->ProcessEvent(event
) )
514 return wxEvtHandler::ProcessEvent(event
);
519 void wxView::OnActivateView(bool WXUNUSED(activate
), wxView
*WXUNUSED(activeView
), wxView
*WXUNUSED(deactiveView
))
523 void wxView::OnPrint(wxDC
*dc
, wxObject
*WXUNUSED(info
))
528 void wxView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
))
532 void wxView::OnChangeFilename()
534 if (GetFrame() && GetDocument())
537 GetDocument()->GetPrintableName(name
);
539 GetFrame()->SetTitle(name
);
543 void wxView::SetDocument(wxDocument
*doc
)
545 m_viewDocument
= doc
;
550 bool wxView::Close(bool deleteWindow
)
552 if (OnClose(deleteWindow
))
558 void wxView::Activate(bool activate
)
560 if (GetDocumentManager())
562 OnActivateView(activate
, this, GetDocumentManager()->GetCurrentView());
563 GetDocumentManager()->ActivateView(this, activate
);
567 bool wxView::OnClose(bool WXUNUSED(deleteWindow
))
569 return GetDocument() ? GetDocument()->Close() : TRUE
;
572 #if wxUSE_PRINTING_ARCHITECTURE
573 wxPrintout
*wxView::OnCreatePrintout()
575 return new wxDocPrintout(this);
577 #endif // wxUSE_PRINTING_ARCHITECTURE
579 // ----------------------------------------------------------------------------
581 // ----------------------------------------------------------------------------
583 wxDocTemplate::wxDocTemplate(wxDocManager
*manager
,
584 const wxString
& descr
,
585 const wxString
& filter
,
588 const wxString
& docTypeName
,
589 const wxString
& viewTypeName
,
590 wxClassInfo
*docClassInfo
,
591 wxClassInfo
*viewClassInfo
,
594 m_documentManager
= manager
;
595 m_description
= descr
;
598 m_fileFilter
= filter
;
600 m_docTypeName
= docTypeName
;
601 m_viewTypeName
= viewTypeName
;
602 m_documentManager
->AssociateTemplate(this);
604 m_docClassInfo
= docClassInfo
;
605 m_viewClassInfo
= viewClassInfo
;
608 wxDocTemplate::~wxDocTemplate()
610 m_documentManager
->DisassociateTemplate(this);
613 // Tries to dynamically construct an object of the right class.
614 wxDocument
*wxDocTemplate::CreateDocument(const wxString
& path
, long flags
)
617 return (wxDocument
*) NULL
;
618 wxDocument
*doc
= (wxDocument
*)m_docClassInfo
->CreateObject();
619 doc
->SetFilename(path
);
620 doc
->SetDocumentTemplate(this);
621 GetDocumentManager()->AddDocument(doc
);
622 doc
->SetCommandProcessor(doc
->OnCreateCommandProcessor());
624 if (doc
->OnCreate(path
, flags
))
629 return (wxDocument
*) NULL
;
633 wxView
*wxDocTemplate::CreateView(wxDocument
*doc
, long flags
)
635 if (!m_viewClassInfo
)
636 return (wxView
*) NULL
;
637 wxView
*view
= (wxView
*)m_viewClassInfo
->CreateObject();
638 view
->SetDocument(doc
);
639 if (view
->OnCreate(doc
, flags
))
646 return (wxView
*) NULL
;
650 // The default (very primitive) format detection: check is the extension is
651 // that of the template
652 bool wxDocTemplate::FileMatchesTemplate(const wxString
& path
)
654 return GetDefaultExtension().IsSameAs(FindExtension(path
));
657 // ----------------------------------------------------------------------------
659 // ----------------------------------------------------------------------------
661 BEGIN_EVENT_TABLE(wxDocManager
, wxEvtHandler
)
662 EVT_MENU(wxID_OPEN
, wxDocManager::OnFileOpen
)
663 EVT_MENU(wxID_CLOSE
, wxDocManager::OnFileClose
)
664 EVT_MENU(wxID_REVERT
, wxDocManager::OnFileRevert
)
665 EVT_MENU(wxID_NEW
, wxDocManager::OnFileNew
)
666 EVT_MENU(wxID_SAVE
, wxDocManager::OnFileSave
)
667 EVT_MENU(wxID_SAVEAS
, wxDocManager::OnFileSaveAs
)
668 EVT_MENU(wxID_UNDO
, wxDocManager::OnUndo
)
669 EVT_MENU(wxID_REDO
, wxDocManager::OnRedo
)
670 #if wxUSE_PRINTING_ARCHITECTURE
671 EVT_MENU(wxID_PRINT
, wxDocManager::OnPrint
)
672 EVT_MENU(wxID_PRINT_SETUP
, wxDocManager::OnPrintSetup
)
673 EVT_MENU(wxID_PREVIEW
, wxDocManager::OnPreview
)
677 wxDocManager::wxDocManager(long flags
, bool initialize
)
679 m_defaultDocumentNameCounter
= 1;
681 m_currentView
= (wxView
*) NULL
;
682 m_maxDocsOpen
= 10000;
683 m_fileHistory
= (wxFileHistory
*) NULL
;
688 wxDocManager::~wxDocManager()
692 delete m_fileHistory
;
695 bool wxDocManager::Clear(bool force
)
697 wxNode
*node
= m_docs
.First();
700 wxDocument
*doc
= (wxDocument
*)node
->Data();
701 wxNode
*next
= node
->Next();
703 if (!doc
->Close() && !force
)
706 // Implicitly deletes the document when the last
707 // view is removed (deleted)
708 doc
->DeleteAllViews();
710 // Check document is deleted
711 if (m_docs
.Member(doc
))
714 // This assumes that documents are not connected in
715 // any way, i.e. deleting one document does NOT
719 node
= m_templates
.First();
722 wxDocTemplate
*templ
= (wxDocTemplate
*) node
->Data();
723 wxNode
* next
= node
->Next();
730 bool wxDocManager::Initialize()
732 m_fileHistory
= OnCreateFileHistory();
736 wxFileHistory
*wxDocManager::OnCreateFileHistory()
738 return new wxFileHistory
;
741 void wxDocManager::OnFileClose(wxCommandEvent
& WXUNUSED(event
))
743 wxDocument
*doc
= GetCurrentDocument();
748 doc
->DeleteAllViews();
749 if (m_docs
.Member(doc
))
754 void wxDocManager::OnFileNew(wxCommandEvent
& WXUNUSED(event
))
756 CreateDocument(wxString(""), wxDOC_NEW
);
759 void wxDocManager::OnFileOpen(wxCommandEvent
& WXUNUSED(event
))
761 CreateDocument(wxString(""), 0);
764 void wxDocManager::OnFileRevert(wxCommandEvent
& WXUNUSED(event
))
766 wxDocument
*doc
= GetCurrentDocument();
772 void wxDocManager::OnFileSave(wxCommandEvent
& WXUNUSED(event
))
774 wxDocument
*doc
= GetCurrentDocument();
780 void wxDocManager::OnFileSaveAs(wxCommandEvent
& WXUNUSED(event
))
782 wxDocument
*doc
= GetCurrentDocument();
788 void wxDocManager::OnPrint(wxCommandEvent
& WXUNUSED(event
))
790 #if wxUSE_PRINTING_ARCHITECTURE
791 wxView
*view
= GetCurrentView();
795 wxPrintout
*printout
= view
->OnCreatePrintout();
799 printer
.Print(view
->GetFrame(), printout
, TRUE
);
803 #endif // wxUSE_PRINTING_ARCHITECTURE
806 void wxDocManager::OnPrintSetup(wxCommandEvent
& WXUNUSED(event
))
808 #if wxUSE_PRINTING_ARCHITECTURE
809 wxWindow
*parentWin
= wxTheApp
->GetTopWindow();
810 wxView
*view
= GetCurrentView();
812 parentWin
= view
->GetFrame();
814 wxPrintDialogData data
;
816 wxPrintDialog
printerDialog(parentWin
, & data
);
817 printerDialog
.GetPrintDialogData().SetSetupDialog(TRUE
);
818 printerDialog
.ShowModal();
819 #endif // wxUSE_PRINTING_ARCHITECTURE
822 void wxDocManager::OnPreview(wxCommandEvent
& WXUNUSED(event
))
824 #if wxUSE_PRINTING_ARCHITECTURE
825 wxView
*view
= GetCurrentView();
829 wxPrintout
*printout
= view
->OnCreatePrintout();
832 // Pass two printout objects: for preview, and possible printing.
833 wxPrintPreviewBase
*preview
= (wxPrintPreviewBase
*) NULL
;
834 preview
= new wxPrintPreview(printout
, view
->OnCreatePrintout());
836 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, (wxFrame
*)wxTheApp
->GetTopWindow(), _("Print Preview"),
837 wxPoint(100, 100), wxSize(600, 650));
838 frame
->Centre(wxBOTH
);
842 #endif // wxUSE_PRINTING_ARCHITECTURE
845 void wxDocManager::OnUndo(wxCommandEvent
& WXUNUSED(event
))
847 wxDocument
*doc
= GetCurrentDocument();
850 if (doc
->GetCommandProcessor())
851 doc
->GetCommandProcessor()->Undo();
854 void wxDocManager::OnRedo(wxCommandEvent
& WXUNUSED(event
))
856 wxDocument
*doc
= GetCurrentDocument();
859 if (doc
->GetCommandProcessor())
860 doc
->GetCommandProcessor()->Redo();
863 wxView
*wxDocManager::GetCurrentView() const
866 return m_currentView
;
867 if (m_docs
.Number() == 1)
869 wxDocument
* doc
= (wxDocument
*) m_docs
.First()->Data();
870 return doc
->GetFirstView();
872 return (wxView
*) NULL
;
875 // Extend event processing to search the view's event table
876 bool wxDocManager::ProcessEvent(wxEvent
& event
)
878 wxView
* view
= GetCurrentView();
881 if (view
->ProcessEvent(event
))
884 return wxEvtHandler::ProcessEvent(event
);
887 wxDocument
*wxDocManager::CreateDocument(const wxString
& path
, long flags
)
889 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.Number()];
892 for (i
= 0; i
< m_templates
.Number(); i
++)
894 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Nth(i
)->Data());
895 if (temp
->IsVisible())
904 return (wxDocument
*) NULL
;
907 // If we've reached the max number of docs, close the
909 if (GetDocuments().Number() >= m_maxDocsOpen
)
911 wxDocument
*doc
= (wxDocument
*)GetDocuments().First()->Data();
914 // Implicitly deletes the document when
915 // the last view is deleted
916 doc
->DeleteAllViews();
918 // Check we're really deleted
919 if (m_docs
.Member(doc
))
923 return (wxDocument
*) NULL
;
926 // New document: user chooses a template, unless there's only one.
927 if (flags
& wxDOC_NEW
)
931 wxDocTemplate
*temp
= templates
[0];
933 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
936 newDoc
->SetDocumentName(temp
->GetDocumentName());
937 newDoc
->SetDocumentTemplate(temp
);
938 newDoc
->OnNewDocument();
943 wxDocTemplate
*temp
= SelectDocumentType(templates
, n
);
947 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
950 newDoc
->SetDocumentName(temp
->GetDocumentName());
951 newDoc
->SetDocumentTemplate(temp
);
952 newDoc
->OnNewDocument();
957 return (wxDocument
*) NULL
;
961 wxDocTemplate
*temp
= (wxDocTemplate
*) NULL
;
963 wxString
path2(_T(""));
967 if (flags
& wxDOC_SILENT
)
968 temp
= FindTemplateForPath(path2
);
970 temp
= SelectDocumentPath(templates
, n
, path2
, flags
);
976 wxDocument
*newDoc
= temp
->CreateDocument(path2
, flags
);
979 newDoc
->SetDocumentName(temp
->GetDocumentName());
980 newDoc
->SetDocumentTemplate(temp
);
981 if (!newDoc
->OnOpenDocument(path2
))
984 return (wxDocument
*) NULL
;
986 AddFileToHistory(path2
);
991 return (wxDocument
*) NULL
;
994 wxView
*wxDocManager::CreateView(wxDocument
*doc
, long flags
)
996 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.Number()];
999 for (i
= 0; i
< m_templates
.Number(); i
++)
1001 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Nth(i
)->Data());
1002 if (temp
->IsVisible())
1004 if (temp
->GetDocumentName() == doc
->GetDocumentName())
1006 templates
[n
] = temp
;
1014 return (wxView
*) NULL
;
1018 wxDocTemplate
*temp
= templates
[0];
1020 wxView
*view
= temp
->CreateView(doc
, flags
);
1022 view
->SetViewName(temp
->GetViewName());
1026 wxDocTemplate
*temp
= SelectViewType(templates
, n
);
1030 wxView
*view
= temp
->CreateView(doc
, flags
);
1032 view
->SetViewName(temp
->GetViewName());
1036 return (wxView
*) NULL
;
1039 // Not yet implemented
1040 void wxDocManager::DeleteTemplate(wxDocTemplate
*WXUNUSED(temp
), long WXUNUSED(flags
))
1044 // Not yet implemented
1045 bool wxDocManager::FlushDoc(wxDocument
*WXUNUSED(doc
))
1050 wxDocument
*wxDocManager::GetCurrentDocument() const
1053 return m_currentView
->GetDocument();
1055 return (wxDocument
*) NULL
;
1058 // Make a default document name
1059 bool wxDocManager::MakeDefaultName(wxString
& name
)
1061 name
.Printf(_("unnamed%d"), m_defaultDocumentNameCounter
);
1062 m_defaultDocumentNameCounter
++;
1067 // Not yet implemented
1068 wxDocTemplate
*wxDocManager::MatchTemplate(const wxString
& WXUNUSED(path
))
1070 return (wxDocTemplate
*) NULL
;
1073 // File history management
1074 void wxDocManager::AddFileToHistory(const wxString
& file
)
1077 m_fileHistory
->AddFileToHistory(file
);
1080 wxString
wxDocManager::GetHistoryFile(int i
) const
1085 histFile
= m_fileHistory
->GetHistoryFile(i
);
1090 void wxDocManager::FileHistoryUseMenu(wxMenu
*menu
)
1093 m_fileHistory
->UseMenu(menu
);
1096 void wxDocManager::FileHistoryRemoveMenu(wxMenu
*menu
)
1099 m_fileHistory
->RemoveMenu(menu
);
1103 void wxDocManager::FileHistoryLoad(wxConfigBase
& config
)
1106 m_fileHistory
->Load(config
);
1109 void wxDocManager::FileHistorySave(wxConfigBase
& config
)
1112 m_fileHistory
->Save(config
);
1116 void wxDocManager::FileHistoryAddFilesToMenu(wxMenu
* menu
)
1119 m_fileHistory
->AddFilesToMenu(menu
);
1122 void wxDocManager::FileHistoryAddFilesToMenu()
1125 m_fileHistory
->AddFilesToMenu();
1128 int wxDocManager::GetNoHistoryFiles() const
1131 return m_fileHistory
->GetNoHistoryFiles();
1137 // Find out the document template via matching in the document file format
1138 // against that of the template
1139 wxDocTemplate
*wxDocManager::FindTemplateForPath(const wxString
& path
)
1141 wxDocTemplate
*theTemplate
= (wxDocTemplate
*) NULL
;
1143 // Find the template which this extension corresponds to
1145 for (i
= 0; i
< m_templates
.Number(); i
++)
1147 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Nth(i
)->Data();
1148 if ( temp
->FileMatchesTemplate(path
) )
1157 // Prompts user to open a file, using file specs in templates.
1158 // How to implement in wxWindows? Must extend the file selector
1159 // dialog or implement own; OR match the extension to the
1160 // template extension.
1162 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**templates
,
1166 int WXUNUSED(noTemplates
),
1169 long WXUNUSED(flags
),
1170 bool WXUNUSED(save
))
1172 // We can only have multiple filters in Windows
1177 for (i
= 0; i
< noTemplates
; i
++)
1179 if (templates
[i
]->IsVisible())
1181 // add a '|' to separate this filter from the previous one
1182 if ( !descrBuf
.IsEmpty() )
1183 descrBuf
<< _T('|');
1185 descrBuf
<< templates
[i
]->GetDescription()
1186 << _T(" (") << templates
[i
]->GetFileFilter() << _T(") |")
1187 << templates
[i
]->GetFileFilter();
1191 wxString descrBuf
= _T("*.*");
1194 int FilterIndex
= 0;
1195 wxString pathTmp
= wxFileSelectorEx(_("Select a file"),
1201 wxTheApp
->GetTopWindow());
1203 if (!pathTmp
.IsEmpty())
1206 wxString theExt
= FindExtension(path
);
1208 return (wxDocTemplate
*) NULL
;
1210 // This is dodgy in that we're selecting the template on the
1211 // basis of the file extension, which may not be a standard
1212 // one. We really want to know exactly which template was
1213 // chosen by using a more advanced file selector.
1214 wxDocTemplate
*theTemplate
= FindTemplateForPath(path
);
1216 theTemplate
= templates
[FilterIndex
];
1223 return (wxDocTemplate
*) NULL
;
1226 // In all other windowing systems, until we have more advanced
1227 // file selectors, we must select the document type (template) first, and
1228 // _then_ pop up the file selector.
1229 wxDocTemplate
*temp
= SelectDocumentType(templates
, noTemplates
);
1231 return (wxDocTemplate
*) NULL
;
1233 wxChar
*pathTmp
= wxFileSelector(_("Select a file"), _T(""), _T(""),
1234 temp
->GetDefaultExtension(),
1235 temp
->GetFileFilter(),
1236 0, wxTheApp
->GetTopWindow());
1244 return (wxDocTemplate
*) NULL
;
1248 wxDocTemplate
*wxDocManager::SelectDocumentType(wxDocTemplate
**templates
,
1251 wxChar
**strings
= new wxChar
*[noTemplates
];
1252 wxChar
**data
= new wxChar
*[noTemplates
];
1255 for (i
= 0; i
< noTemplates
; i
++)
1257 if (templates
[i
]->IsVisible())
1259 strings
[n
] = WXSTRINGCAST templates
[i
]->m_description
;
1260 data
[n
] = (wxChar
*)templates
[i
];
1268 return (wxDocTemplate
*) NULL
;
1272 wxDocTemplate
*temp
= (wxDocTemplate
*)data
[0];
1278 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData(_("Select a document template"), _("Templates"), n
,
1285 wxDocTemplate
*wxDocManager::SelectViewType(wxDocTemplate
**templates
,
1288 wxChar
**strings
= new wxChar
*[noTemplates
];
1289 wxChar
**data
= new wxChar
*[noTemplates
];
1292 for (i
= 0; i
< noTemplates
; i
++)
1294 if (templates
[i
]->IsVisible() && (templates
[i
]->GetViewName() != _T("")))
1296 strings
[n
] = WXSTRINGCAST templates
[i
]->m_viewTypeName
;
1297 data
[n
] = (wxChar
*)templates
[i
];
1301 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData(_("Select a document view"), _("Views"), n
,
1308 void wxDocManager::AssociateTemplate(wxDocTemplate
*temp
)
1310 if (!m_templates
.Member(temp
))
1311 m_templates
.Append(temp
);
1314 void wxDocManager::DisassociateTemplate(wxDocTemplate
*temp
)
1316 m_templates
.DeleteObject(temp
);
1319 // Add and remove a document from the manager's list
1320 void wxDocManager::AddDocument(wxDocument
*doc
)
1322 if (!m_docs
.Member(doc
))
1326 void wxDocManager::RemoveDocument(wxDocument
*doc
)
1328 m_docs
.DeleteObject(doc
);
1331 // Views or windows should inform the document manager
1332 // when a view is going in or out of focus
1333 void wxDocManager::ActivateView(wxView
*view
, bool activate
, bool WXUNUSED(deleting
))
1335 // If we're deactiving, and if we're not actually deleting the view, then
1336 // don't reset the current view because we may be going to
1337 // a window without a view.
1338 // WHAT DID I MEAN BY THAT EXACTLY?
1342 if (m_currentView == view)
1343 m_currentView = NULL;
1349 m_currentView
= view
;
1351 m_currentView
= (wxView
*) NULL
;
1355 // ----------------------------------------------------------------------------
1356 // Default document child frame
1357 // ----------------------------------------------------------------------------
1359 BEGIN_EVENT_TABLE(wxDocChildFrame
, wxFrame
)
1360 EVT_ACTIVATE(wxDocChildFrame::OnActivate
)
1361 EVT_CLOSE(wxDocChildFrame::OnCloseWindow
)
1364 wxDocChildFrame::wxDocChildFrame(wxDocument
*doc
,
1368 const wxString
& title
,
1372 const wxString
& name
)
1373 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1375 m_childDocument
= doc
;
1378 view
->SetFrame(this);
1381 wxDocChildFrame::~wxDocChildFrame()
1385 // Extend event processing to search the view's event table
1386 bool wxDocChildFrame::ProcessEvent(wxEvent
& event
)
1389 m_childView
->Activate(TRUE
);
1391 if ( !m_childView
|| ! m_childView
->ProcessEvent(event
) )
1393 // Only hand up to the parent if it's a menu command
1394 if (!event
.IsKindOf(CLASSINFO(wxCommandEvent
)) || !GetParent() || !GetParent()->ProcessEvent(event
))
1395 return wxEvtHandler::ProcessEvent(event
);
1403 void wxDocChildFrame::OnActivate(wxActivateEvent
& event
)
1405 wxFrame::OnActivate(event
);
1408 m_childView
->Activate(event
.GetActive());
1411 void wxDocChildFrame::OnCloseWindow(wxCloseEvent
& event
)
1416 if (!event
.CanVeto())
1417 ans
= TRUE
; // Must delete.
1419 ans
= m_childView
->Close(FALSE
); // FALSE means don't delete associated window
1423 m_childView
->Activate(FALSE
);
1425 m_childView
= (wxView
*) NULL
;
1426 m_childDocument
= (wxDocument
*) NULL
;
1437 // ----------------------------------------------------------------------------
1438 // Default parent frame
1439 // ----------------------------------------------------------------------------
1441 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1442 EVT_MENU(wxID_EXIT
, wxDocParentFrame::OnExit
)
1443 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE9
, wxDocParentFrame::OnMRUFile
)
1444 EVT_CLOSE(wxDocParentFrame::OnCloseWindow
)
1447 wxDocParentFrame::wxDocParentFrame(wxDocManager
*manager
,
1450 const wxString
& title
,
1454 const wxString
& name
)
1455 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1457 m_docManager
= manager
;
1460 void wxDocParentFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
1465 void wxDocParentFrame::OnMRUFile(wxCommandEvent
& event
)
1467 wxString
f(m_docManager
->GetHistoryFile(event
.GetSelection() - wxID_FILE1
));
1469 (void)m_docManager
->CreateDocument(f
, wxDOC_SILENT
);
1472 // Extend event processing to search the view's event table
1473 bool wxDocParentFrame::ProcessEvent(wxEvent
& event
)
1475 // Try the document manager, then do default processing
1476 if (!m_docManager
|| !m_docManager
->ProcessEvent(event
))
1477 return wxEvtHandler::ProcessEvent(event
);
1482 // Define the behaviour for the frame closing
1483 // - must delete all frames except for the main one.
1484 void wxDocParentFrame::OnCloseWindow(wxCloseEvent
& event
)
1486 if (m_docManager
->Clear(!event
.CanVeto()))
1494 #if wxUSE_PRINTING_ARCHITECTURE
1496 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
)
1497 : wxPrintout(WXSTRINGCAST title
)
1499 m_printoutView
= view
;
1502 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
1506 // Get the logical pixels per inch of screen and printer
1507 int ppiScreenX
, ppiScreenY
;
1508 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
1509 int ppiPrinterX
, ppiPrinterY
;
1510 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
1512 // This scales the DC so that the printout roughly represents the
1513 // the screen scaling. The text point size _should_ be the right size
1514 // but in fact is too small for some reason. This is a detail that will
1515 // need to be addressed at some point but can be fudged for the
1517 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
1519 // Now we have to check in case our real page size is reduced
1520 // (e.g. because we're drawing to a print preview memory DC)
1521 int pageWidth
, pageHeight
;
1523 dc
->GetSize(&w
, &h
);
1524 GetPageSizePixels(&pageWidth
, &pageHeight
);
1526 // If printer pageWidth == current DC width, then this doesn't
1527 // change. But w might be the preview bitmap width, so scale down.
1528 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
1529 dc
->SetUserScale(overallScale
, overallScale
);
1533 m_printoutView
->OnDraw(dc
);
1538 bool wxDocPrintout::HasPage(int pageNum
)
1540 return (pageNum
== 1);
1543 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
1545 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
1551 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
1559 #endif // wxUSE_PRINTING_ARCHITECTURE
1561 // ----------------------------------------------------------------------------
1562 // Command processing framework
1563 // ----------------------------------------------------------------------------
1565 wxCommand::wxCommand(bool canUndoIt
, const wxString
& name
)
1567 m_canUndo
= canUndoIt
;
1568 m_commandName
= name
;
1571 wxCommand::~wxCommand()
1575 // Command processor
1576 wxCommandProcessor::wxCommandProcessor(int maxCommands
)
1578 m_maxNoCommands
= maxCommands
;
1579 m_currentCommand
= (wxNode
*) NULL
;
1580 m_commandEditMenu
= (wxMenu
*) NULL
;
1583 wxCommandProcessor::~wxCommandProcessor()
1588 // Pass a command to the processor. The processor calls Do();
1589 // if successful, is appended to the command history unless
1590 // storeIt is FALSE.
1591 bool wxCommandProcessor::Submit(wxCommand
*command
, bool storeIt
)
1593 bool success
= command
->Do();
1594 if (success
&& storeIt
)
1596 if (m_commands
.Number() == m_maxNoCommands
)
1598 wxNode
*firstNode
= m_commands
.First();
1599 wxCommand
*firstCommand
= (wxCommand
*)firstNode
->Data();
1600 delete firstCommand
;
1604 // Correct a bug: we must chop off the current 'branch'
1605 // so that we're at the end of the command list.
1606 if (!m_currentCommand
)
1610 wxNode
*node
= m_currentCommand
->Next();
1613 wxNode
*next
= node
->Next();
1614 delete (wxCommand
*)node
->Data();
1620 m_commands
.Append(command
);
1621 m_currentCommand
= m_commands
.Last();
1627 bool wxCommandProcessor::Undo()
1629 if (m_currentCommand
)
1631 wxCommand
*command
= (wxCommand
*)m_currentCommand
->Data();
1632 if (command
->CanUndo())
1634 bool success
= command
->Undo();
1637 m_currentCommand
= m_currentCommand
->Previous();
1646 bool wxCommandProcessor::Redo()
1648 wxCommand
*redoCommand
= (wxCommand
*) NULL
;
1649 wxNode
*redoNode
= (wxNode
*) NULL
;
1650 if (m_currentCommand
&& m_currentCommand
->Next())
1652 redoCommand
= (wxCommand
*)m_currentCommand
->Next()->Data();
1653 redoNode
= m_currentCommand
->Next();
1657 if (m_commands
.Number() > 0)
1659 redoCommand
= (wxCommand
*)m_commands
.First()->Data();
1660 redoNode
= m_commands
.First();
1666 bool success
= redoCommand
->Do();
1669 m_currentCommand
= redoNode
;
1677 bool wxCommandProcessor::CanUndo() const
1679 if (m_currentCommand
)
1680 return ((wxCommand
*)m_currentCommand
->Data())->CanUndo();
1684 bool wxCommandProcessor::CanRedo() const
1686 if ((m_currentCommand
!= (wxNode
*) NULL
) && (m_currentCommand
->Next() == (wxNode
*) NULL
))
1689 if ((m_currentCommand
!= (wxNode
*) NULL
) && (m_currentCommand
->Next() != (wxNode
*) NULL
))
1692 if ((m_currentCommand
== (wxNode
*) NULL
) && (m_commands
.Number() > 0))
1698 void wxCommandProcessor::Initialize()
1700 m_currentCommand
= m_commands
.Last();
1704 void wxCommandProcessor::SetMenuStrings()
1706 if (m_commandEditMenu
)
1709 if (m_currentCommand
)
1711 wxCommand
*command
= (wxCommand
*)m_currentCommand
->Data();
1712 wxString
commandName(command
->GetName());
1713 if (commandName
== _T("")) commandName
= _("Unnamed command");
1714 bool canUndo
= command
->CanUndo();
1716 buf
= wxString(_("&Undo ")) + commandName
;
1718 buf
= wxString(_("Can't &Undo ")) + commandName
;
1720 m_commandEditMenu
->SetLabel(wxID_UNDO
, buf
);
1721 m_commandEditMenu
->Enable(wxID_UNDO
, canUndo
);
1723 // We can redo, if we're not at the end of the history.
1724 if (m_currentCommand
->Next())
1726 wxCommand
*redoCommand
= (wxCommand
*)m_currentCommand
->Next()->Data();
1727 wxString
redoCommandName(redoCommand
->GetName());
1728 if (redoCommandName
== _T("")) redoCommandName
= _("Unnamed command");
1729 buf
= wxString(_("&Redo ")) + redoCommandName
;
1730 m_commandEditMenu
->SetLabel(wxID_REDO
, buf
);
1731 m_commandEditMenu
->Enable(wxID_REDO
, TRUE
);
1735 m_commandEditMenu
->SetLabel(wxID_REDO
, _("&Redo"));
1736 m_commandEditMenu
->Enable(wxID_REDO
, FALSE
);
1741 m_commandEditMenu
->SetLabel(wxID_UNDO
, _("&Undo"));
1742 m_commandEditMenu
->Enable(wxID_UNDO
, FALSE
);
1744 if (m_commands
.Number() == 0)
1746 m_commandEditMenu
->SetLabel(wxID_REDO
, _("&Redo"));
1747 m_commandEditMenu
->Enable(wxID_REDO
, FALSE
);
1751 // currentCommand is NULL but there are commands: this means that
1752 // we've undone to the start of the list, but can redo the first.
1753 wxCommand
*redoCommand
= (wxCommand
*)m_commands
.First()->Data();
1754 wxString
redoCommandName(redoCommand
->GetName());
1755 if (redoCommandName
== _T("")) redoCommandName
= _("Unnamed command");
1756 buf
= wxString(_("&Redo ")) + redoCommandName
;
1757 m_commandEditMenu
->SetLabel(wxID_REDO
, buf
);
1758 m_commandEditMenu
->Enable(wxID_REDO
, TRUE
);
1764 void wxCommandProcessor::ClearCommands()
1766 wxNode
*node
= m_commands
.First();
1769 wxCommand
*command
= (wxCommand
*)node
->Data();
1772 node
= m_commands
.First();
1774 m_currentCommand
= (wxNode
*) NULL
;
1777 // ----------------------------------------------------------------------------
1778 // File history processor
1779 // ----------------------------------------------------------------------------
1781 wxFileHistory::wxFileHistory(int maxFiles
)
1783 m_fileMaxFiles
= maxFiles
;
1785 m_fileHistory
= new wxChar
*[m_fileMaxFiles
];
1788 wxFileHistory::~wxFileHistory()
1791 for (i
= 0; i
< m_fileHistoryN
; i
++)
1792 delete[] m_fileHistory
[i
];
1793 delete[] m_fileHistory
;
1796 // File history management
1797 void wxFileHistory::AddFileToHistory(const wxString
& file
)
1800 // Check we don't already have this file
1801 for (i
= 0; i
< m_fileHistoryN
; i
++)
1803 if (m_fileHistory
[i
] && wxString(m_fileHistory
[i
]) == file
)
1807 // Add to the project file history:
1808 // Move existing files (if any) down so we can insert file at beginning.
1810 // First delete filename that has popped off the end of the array (if any)
1811 if (m_fileHistoryN
== m_fileMaxFiles
)
1813 delete[] m_fileHistory
[m_fileMaxFiles
-1];
1814 m_fileHistory
[m_fileMaxFiles
-1] = (wxChar
*) NULL
;
1816 if (m_fileHistoryN
< m_fileMaxFiles
)
1818 wxNode
* node
= m_fileMenus
.First();
1821 wxMenu
* menu
= (wxMenu
*) node
->Data();
1822 if (m_fileHistoryN
== 0)
1823 menu
->AppendSeparator();
1824 menu
->Append(wxID_FILE1
+m_fileHistoryN
, _("[EMPTY]"));
1825 node
= node
->Next();
1829 // Shuffle filenames down
1830 for (i
= (m_fileHistoryN
-1); i
> 0; i
--)
1832 m_fileHistory
[i
] = m_fileHistory
[i
-1];
1834 m_fileHistory
[0] = copystring(file
);
1836 for (i
= 0; i
< m_fileHistoryN
; i
++)
1837 if (m_fileHistory
[i
])
1840 buf
.Printf(_T("&%d %s"), i
+1, m_fileHistory
[i
]);
1841 wxNode
* node
= m_fileMenus
.First();
1844 wxMenu
* menu
= (wxMenu
*) node
->Data();
1845 menu
->SetLabel(wxID_FILE1
+i
, buf
);
1846 node
= node
->Next();
1851 wxString
wxFileHistory::GetHistoryFile(int i
) const
1853 if (i
< m_fileHistoryN
)
1854 return wxString(m_fileHistory
[i
]);
1856 return wxString("");
1859 void wxFileHistory::UseMenu(wxMenu
*menu
)
1861 if (!m_fileMenus
.Member(menu
))
1862 m_fileMenus
.Append(menu
);
1865 void wxFileHistory::RemoveMenu(wxMenu
*menu
)
1867 m_fileMenus
.DeleteObject(menu
);
1871 void wxFileHistory::Load(wxConfigBase
& config
)
1875 buf
.Printf(_T("file%d"), m_fileHistoryN
+1);
1876 wxString historyFile
;
1877 while ((m_fileHistoryN
<= m_fileMaxFiles
) && config
.Read(buf
, &historyFile
) && (historyFile
!= _T("")))
1879 m_fileHistory
[m_fileHistoryN
] = copystring((const wxChar
*) historyFile
);
1881 buf
.Printf(_T("file%d"), m_fileHistoryN
+1);
1887 void wxFileHistory::Save(wxConfigBase
& config
)
1890 for (i
= 0; i
< m_fileHistoryN
; i
++)
1893 buf
.Printf(_T("file%d"), i
+1);
1894 config
.Write(buf
, wxString(m_fileHistory
[i
]));
1897 #endif // wxUSE_CONFIG
1899 void wxFileHistory::AddFilesToMenu()
1901 if (m_fileHistoryN
> 0)
1903 wxNode
* node
= m_fileMenus
.First();
1906 wxMenu
* menu
= (wxMenu
*) node
->Data();
1907 menu
->AppendSeparator();
1909 for (i
= 0; i
< m_fileHistoryN
; i
++)
1911 if (m_fileHistory
[i
])
1914 buf
.Printf(_T("&%d %s"), i
+1, m_fileHistory
[i
]);
1915 menu
->Append(wxID_FILE1
+i
, buf
);
1918 node
= node
->Next();
1923 void wxFileHistory::AddFilesToMenu(wxMenu
* menu
)
1925 if (m_fileHistoryN
> 0)
1927 menu
->AppendSeparator();
1929 for (i
= 0; i
< m_fileHistoryN
; i
++)
1931 if (m_fileHistory
[i
])
1934 buf
.Printf(_T("&%d %s"), i
+1, m_fileHistory
[i
]);
1935 menu
->Append(wxID_FILE1
+i
, buf
);
1941 // ----------------------------------------------------------------------------
1942 // Permits compatibility with existing file formats and functions that
1943 // manipulate files directly
1944 // ----------------------------------------------------------------------------
1946 bool wxTransferFileToStream(const wxString
& filename
, ostream
& stream
)
1951 if ((fd1
= fopen (filename
.fn_str(), "rb")) == NULL
)
1954 while ((ch
= getc (fd1
)) != EOF
)
1955 stream
<< (unsigned char)ch
;
1961 bool wxTransferStreamToFile(istream
& stream
, const wxString
& filename
)
1966 if ((fd1
= fopen (filename
.fn_str(), "wb")) == NULL
)
1971 while (!stream
.eof())
1981 #endif // wxUSE_DOC_VIEW_ARCHITECTURE