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"
35 #if wxUSE_DOC_VIEW_ARCHITECTURE
38 #include "wx/string.h"
42 #include "wx/dialog.h"
45 #include "wx/filedlg.h"
53 #include "wx/msgdlg.h"
54 #include "wx/choicdlg.h"
55 #include "wx/docview.h"
56 #include "wx/prntbase.h"
57 #include "wx/printdlg.h"
58 #include "wx/confbase.h"
63 #include "wx/ioswrap.h"
71 // ----------------------------------------------------------------------------
73 // ----------------------------------------------------------------------------
75 #if !USE_SHARED_LIBRARY
76 IMPLEMENT_ABSTRACT_CLASS(wxDocument
, wxEvtHandler
)
77 IMPLEMENT_ABSTRACT_CLASS(wxView
, wxEvtHandler
)
78 IMPLEMENT_ABSTRACT_CLASS(wxDocTemplate
, wxObject
)
79 IMPLEMENT_DYNAMIC_CLASS(wxDocManager
, wxEvtHandler
)
80 IMPLEMENT_CLASS(wxDocChildFrame
, wxFrame
)
81 IMPLEMENT_CLASS(wxDocParentFrame
, wxFrame
)
83 #if wxUSE_PRINTING_ARCHITECTURE
84 IMPLEMENT_DYNAMIC_CLASS(wxDocPrintout
, wxPrintout
)
87 IMPLEMENT_CLASS(wxCommand
, wxObject
)
88 IMPLEMENT_DYNAMIC_CLASS(wxCommandProcessor
, wxObject
)
89 IMPLEMENT_DYNAMIC_CLASS(wxFileHistory
, wxObject
)
92 // ----------------------------------------------------------------------------
93 // function prototypes
94 // ----------------------------------------------------------------------------
96 static inline wxString
FindExtension(const wxChar
*path
);
98 // ============================================================================
100 // ============================================================================
102 // ----------------------------------------------------------------------------
104 // ----------------------------------------------------------------------------
106 static wxString
FindExtension(const wxChar
*path
)
109 wxSplitPath(path
, NULL
, NULL
, &ext
);
111 // VZ: extensions are considered not case sensitive - is this really a good
113 return ext
.MakeLower();
116 // ----------------------------------------------------------------------------
117 // Definition of wxDocument
118 // ----------------------------------------------------------------------------
120 wxDocument::wxDocument(wxDocument
*parent
)
122 m_documentModified
= FALSE
;
123 m_documentParent
= parent
;
124 m_documentTemplate
= (wxDocTemplate
*) NULL
;
128 bool wxDocument::DeleteContents()
133 wxDocument::~wxDocument()
137 if (m_commandProcessor
)
138 delete m_commandProcessor
;
140 GetDocumentManager()->RemoveDocument(this);
142 // Not safe to do here, since it'll invoke virtual view functions
143 // expecting to see valid derived objects: and by the time we get here,
144 // we've called destructors higher up.
148 bool wxDocument::Close()
150 if (OnSaveModified())
151 return OnCloseDocument();
156 bool wxDocument::OnCloseDocument()
163 // Note that this implicitly deletes the document when the last view is
165 bool wxDocument::DeleteAllViews()
167 wxNode
*node
= m_documentViews
.First();
170 wxView
*view
= (wxView
*)node
->Data();
174 wxNode
*next
= node
->Next();
176 delete view
; // Deletes node implicitly
182 wxView
*wxDocument::GetFirstView() const
184 if (m_documentViews
.Number() == 0)
185 return (wxView
*) NULL
;
186 return (wxView
*)m_documentViews
.First()->Data();
189 wxDocManager
*wxDocument::GetDocumentManager() const
191 return m_documentTemplate
->GetDocumentManager();
194 bool wxDocument::OnNewDocument()
196 if (!OnSaveModified())
199 if (OnCloseDocument()==FALSE
) return FALSE
;
202 SetDocumentSaved(FALSE
);
205 GetDocumentManager()->MakeDefaultName(name
);
207 SetFilename(name
, TRUE
);
212 bool wxDocument::Save()
216 if (!IsModified()) return TRUE
;
217 if (m_documentFile
== _T("") || !m_savedYet
)
220 ret
= OnSaveDocument(m_documentFile
);
222 SetDocumentSaved(TRUE
);
226 bool wxDocument::SaveAs()
228 wxDocTemplate
*docTemplate
= GetDocumentTemplate();
232 wxString tmp
= wxFileSelector(_("Save as"),
233 docTemplate
->GetDirectory(),
235 docTemplate
->GetDefaultExtension(),
236 docTemplate
->GetFileFilter(),
237 wxSAVE
| wxOVERWRITE_PROMPT
,
238 GetDocumentWindow());
243 wxString
fileName(tmp
);
244 wxString path
, name
, ext
;
245 wxSplitPath(fileName
, & path
, & name
, & ext
);
247 if (ext
.IsEmpty() || ext
== _T(""))
250 fileName
+= docTemplate
->GetDefaultExtension();
253 SetFilename(fileName
);
254 SetTitle(wxFileNameFromPath(fileName
));
256 GetDocumentManager()->AddFileToHistory(fileName
);
258 // Notify the views that the filename has changed
259 wxNode
*node
= m_documentViews
.First();
262 wxView
*view
= (wxView
*)node
->Data();
263 view
->OnChangeFilename();
267 return OnSaveDocument(m_documentFile
);
270 bool wxDocument::OnSaveDocument(const wxString
& file
)
276 if (wxTheApp
->GetAppName() != _T(""))
277 msgTitle
= wxTheApp
->GetAppName();
279 msgTitle
= wxString(_("File error"));
281 ofstream
store(file
.fn_str());
282 if (store
.fail() || store
.bad())
284 (void)wxMessageBox(_("Sorry, could not open this file for saving."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
285 GetDocumentWindow());
289 if (SaveObject(store
)==FALSE
)
291 (void)wxMessageBox(_("Sorry, could not save this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
292 GetDocumentWindow());
301 bool wxDocument::OnOpenDocument(const wxString
& file
)
303 if (!OnSaveModified())
307 if (wxTheApp
->GetAppName() != _T(""))
308 msgTitle
= wxTheApp
->GetAppName();
310 msgTitle
= wxString(_("File error"));
312 ifstream
store(file
.fn_str());
313 if (store
.fail() || store
.bad())
315 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
|wxICON_EXCLAMATION
,
316 GetDocumentWindow());
319 if (LoadObject(store
)==FALSE
)
321 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
|wxICON_EXCLAMATION
,
322 GetDocumentWindow());
325 SetFilename(file
, TRUE
);
334 istream
& wxDocument::LoadObject(istream
& stream
)
336 // wxObject::LoadObject(stream);
341 ostream
& wxDocument::SaveObject(ostream
& stream
)
343 // wxObject::SaveObject(stream);
348 bool wxDocument::Revert()
354 // Get title, or filename if no title, else unnamed
355 bool wxDocument::GetPrintableName(wxString
& buf
) const
357 if (m_documentTitle
!= _T(""))
359 buf
= m_documentTitle
;
362 else if (m_documentFile
!= _T(""))
364 buf
= wxFileNameFromPath(m_documentFile
);
374 wxWindow
*wxDocument::GetDocumentWindow() const
376 wxView
*view
= GetFirstView();
378 return view
->GetFrame();
380 return wxTheApp
->GetTopWindow();
383 wxCommandProcessor
*wxDocument::OnCreateCommandProcessor()
385 return new wxCommandProcessor
;
388 // TRUE if safe to close
389 bool wxDocument::OnSaveModified()
394 GetPrintableName(title
);
397 if (wxTheApp
->GetAppName() != _T(""))
398 msgTitle
= wxTheApp
->GetAppName();
400 msgTitle
= wxString(_("Warning"));
403 prompt
.Printf(_("Do you want to save changes to document %s?"),
404 (const wxChar
*)title
);
405 int res
= wxMessageBox(prompt
, msgTitle
,
406 wxYES_NO
|wxCANCEL
|wxICON_QUESTION
,
407 GetDocumentWindow());
413 else if (res
== wxYES
)
415 else if (res
== wxCANCEL
)
421 bool wxDocument::Draw(wxDC
& WXUNUSED(context
))
426 bool wxDocument::AddView(wxView
*view
)
428 if (!m_documentViews
.Member(view
))
430 m_documentViews
.Append(view
);
436 bool wxDocument::RemoveView(wxView
*view
)
438 (void)m_documentViews
.DeleteObject(view
);
443 bool wxDocument::OnCreate(const wxString
& WXUNUSED(path
), long flags
)
445 if (GetDocumentTemplate()->CreateView(this, flags
))
451 // Called after a view is added or removed.
452 // The default implementation deletes the document if
453 // there are no more views.
454 void wxDocument::OnChangedViewList()
456 if (m_documentViews
.Number() == 0)
458 if (OnSaveModified())
465 void wxDocument::UpdateAllViews(wxView
*sender
, wxObject
*hint
)
467 wxNode
*node
= m_documentViews
.First();
470 wxView
*view
= (wxView
*)node
->Data();
471 view
->OnUpdate(sender
, hint
);
476 void wxDocument::SetFilename(const wxString
& filename
, bool notifyViews
)
478 m_documentFile
= filename
;
481 // Notify the views that the filename has changed
482 wxNode
*node
= m_documentViews
.First();
485 wxView
*view
= (wxView
*)node
->Data();
486 view
->OnChangeFilename();
492 // ----------------------------------------------------------------------------
494 // ----------------------------------------------------------------------------
499 m_viewDocument
= (wxDocument
*) NULL
;
502 m_viewFrame
= (wxFrame
*) NULL
;
507 GetDocumentManager()->ActivateView(this, FALSE
, TRUE
);
508 m_viewDocument
->RemoveView(this);
511 // Extend event processing to search the document's event table
512 bool wxView::ProcessEvent(wxEvent
& event
)
514 if ( !GetDocument() || !GetDocument()->ProcessEvent(event
) )
515 return wxEvtHandler::ProcessEvent(event
);
520 void wxView::OnActivateView(bool WXUNUSED(activate
), wxView
*WXUNUSED(activeView
), wxView
*WXUNUSED(deactiveView
))
524 void wxView::OnPrint(wxDC
*dc
, wxObject
*WXUNUSED(info
))
529 void wxView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
))
533 void wxView::OnChangeFilename()
535 if (GetFrame() && GetDocument())
538 GetDocument()->GetPrintableName(name
);
540 GetFrame()->SetTitle(name
);
544 void wxView::SetDocument(wxDocument
*doc
)
546 m_viewDocument
= doc
;
551 bool wxView::Close(bool deleteWindow
)
553 if (OnClose(deleteWindow
))
559 void wxView::Activate(bool activate
)
561 if (GetDocumentManager())
563 OnActivateView(activate
, this, GetDocumentManager()->GetCurrentView());
564 GetDocumentManager()->ActivateView(this, activate
);
568 bool wxView::OnClose(bool WXUNUSED(deleteWindow
))
570 return GetDocument() ? GetDocument()->Close() : TRUE
;
573 #if wxUSE_PRINTING_ARCHITECTURE
574 wxPrintout
*wxView::OnCreatePrintout()
576 return new wxDocPrintout(this);
578 #endif // wxUSE_PRINTING_ARCHITECTURE
580 // ----------------------------------------------------------------------------
582 // ----------------------------------------------------------------------------
584 wxDocTemplate::wxDocTemplate(wxDocManager
*manager
,
585 const wxString
& descr
,
586 const wxString
& filter
,
589 const wxString
& docTypeName
,
590 const wxString
& viewTypeName
,
591 wxClassInfo
*docClassInfo
,
592 wxClassInfo
*viewClassInfo
,
595 m_documentManager
= manager
;
596 m_description
= descr
;
599 m_fileFilter
= filter
;
601 m_docTypeName
= docTypeName
;
602 m_viewTypeName
= viewTypeName
;
603 m_documentManager
->AssociateTemplate(this);
605 m_docClassInfo
= docClassInfo
;
606 m_viewClassInfo
= viewClassInfo
;
609 wxDocTemplate::~wxDocTemplate()
611 m_documentManager
->DisassociateTemplate(this);
614 // Tries to dynamically construct an object of the right class.
615 wxDocument
*wxDocTemplate::CreateDocument(const wxString
& path
, long flags
)
618 return (wxDocument
*) NULL
;
619 wxDocument
*doc
= (wxDocument
*)m_docClassInfo
->CreateObject();
620 doc
->SetFilename(path
);
621 doc
->SetDocumentTemplate(this);
622 GetDocumentManager()->AddDocument(doc
);
623 doc
->SetCommandProcessor(doc
->OnCreateCommandProcessor());
625 if (doc
->OnCreate(path
, flags
))
630 return (wxDocument
*) NULL
;
634 wxView
*wxDocTemplate::CreateView(wxDocument
*doc
, long flags
)
636 if (!m_viewClassInfo
)
637 return (wxView
*) NULL
;
638 wxView
*view
= (wxView
*)m_viewClassInfo
->CreateObject();
639 view
->SetDocument(doc
);
640 if (view
->OnCreate(doc
, flags
))
647 return (wxView
*) NULL
;
651 // The default (very primitive) format detection: check is the extension is
652 // that of the template
653 bool wxDocTemplate::FileMatchesTemplate(const wxString
& path
)
655 return GetDefaultExtension().IsSameAs(FindExtension(path
));
658 // ----------------------------------------------------------------------------
660 // ----------------------------------------------------------------------------
662 BEGIN_EVENT_TABLE(wxDocManager
, wxEvtHandler
)
663 EVT_MENU(wxID_OPEN
, wxDocManager::OnFileOpen
)
664 EVT_MENU(wxID_CLOSE
, wxDocManager::OnFileClose
)
665 EVT_MENU(wxID_REVERT
, wxDocManager::OnFileRevert
)
666 EVT_MENU(wxID_NEW
, wxDocManager::OnFileNew
)
667 EVT_MENU(wxID_SAVE
, wxDocManager::OnFileSave
)
668 EVT_MENU(wxID_SAVEAS
, wxDocManager::OnFileSaveAs
)
669 EVT_MENU(wxID_UNDO
, wxDocManager::OnUndo
)
670 EVT_MENU(wxID_REDO
, wxDocManager::OnRedo
)
671 EVT_MENU(wxID_PRINT
, wxDocManager::OnPrint
)
672 EVT_MENU(wxID_PRINT_SETUP
, wxDocManager::OnPrintSetup
)
673 EVT_MENU(wxID_PREVIEW
, wxDocManager::OnPreview
)
676 wxDocManager::wxDocManager(long flags
, bool initialize
)
678 m_defaultDocumentNameCounter
= 1;
680 m_currentView
= (wxView
*) NULL
;
681 m_maxDocsOpen
= 10000;
682 m_fileHistory
= (wxFileHistory
*) NULL
;
687 wxDocManager::~wxDocManager()
691 delete m_fileHistory
;
694 bool wxDocManager::Clear(bool force
)
696 wxNode
*node
= m_docs
.First();
699 wxDocument
*doc
= (wxDocument
*)node
->Data();
700 wxNode
*next
= node
->Next();
702 if (!doc
->Close() && !force
)
705 // Implicitly deletes the document when the last
706 // view is removed (deleted)
707 doc
->DeleteAllViews();
709 // Check document is deleted
710 if (m_docs
.Member(doc
))
713 // This assumes that documents are not connected in
714 // any way, i.e. deleting one document does NOT
718 node
= m_templates
.First();
721 wxDocTemplate
*templ
= (wxDocTemplate
*) node
->Data();
722 wxNode
* next
= node
->Next();
729 bool wxDocManager::Initialize()
731 m_fileHistory
= OnCreateFileHistory();
735 wxFileHistory
*wxDocManager::OnCreateFileHistory()
737 return new wxFileHistory
;
740 void wxDocManager::OnFileClose(wxCommandEvent
& WXUNUSED(event
))
742 wxDocument
*doc
= GetCurrentDocument();
747 doc
->DeleteAllViews();
748 if (m_docs
.Member(doc
))
753 void wxDocManager::OnFileNew(wxCommandEvent
& WXUNUSED(event
))
755 CreateDocument(wxString(""), wxDOC_NEW
);
758 void wxDocManager::OnFileOpen(wxCommandEvent
& WXUNUSED(event
))
760 CreateDocument(wxString(""), 0);
763 void wxDocManager::OnFileRevert(wxCommandEvent
& WXUNUSED(event
))
765 wxDocument
*doc
= GetCurrentDocument();
771 void wxDocManager::OnFileSave(wxCommandEvent
& WXUNUSED(event
))
773 wxDocument
*doc
= GetCurrentDocument();
779 void wxDocManager::OnFileSaveAs(wxCommandEvent
& WXUNUSED(event
))
781 wxDocument
*doc
= GetCurrentDocument();
787 void wxDocManager::OnPrint(wxCommandEvent
& WXUNUSED(event
))
789 #if wxUSE_PRINTING_ARCHITECTURE
790 wxView
*view
= GetCurrentView();
794 wxPrintout
*printout
= view
->OnCreatePrintout();
798 printer
.Print(view
->GetFrame(), printout
, TRUE
);
802 #endif // wxUSE_PRINTING_ARCHITECTURE
805 void wxDocManager::OnPrintSetup(wxCommandEvent
& WXUNUSED(event
))
807 wxWindow
*parentWin
= wxTheApp
->GetTopWindow();
808 wxView
*view
= GetCurrentView();
810 parentWin
= view
->GetFrame();
812 wxPrintDialogData data
;
814 wxPrintDialog
printerDialog(parentWin
, & data
);
815 printerDialog
.GetPrintDialogData().SetSetupDialog(TRUE
);
816 printerDialog
.ShowModal();
819 void wxDocManager::OnPreview(wxCommandEvent
& WXUNUSED(event
))
821 #if wxUSE_PRINTING_ARCHITECTURE
822 wxView
*view
= GetCurrentView();
826 wxPrintout
*printout
= view
->OnCreatePrintout();
829 // Pass two printout objects: for preview, and possible printing.
830 wxPrintPreviewBase
*preview
= (wxPrintPreviewBase
*) NULL
;
831 preview
= new wxPrintPreview(printout
, view
->OnCreatePrintout());
833 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, (wxFrame
*)wxTheApp
->GetTopWindow(), _("Print Preview"),
834 wxPoint(100, 100), wxSize(600, 650));
835 frame
->Centre(wxBOTH
);
839 #endif // wxUSE_PRINTING_ARCHITECTURE
842 void wxDocManager::OnUndo(wxCommandEvent
& WXUNUSED(event
))
844 wxDocument
*doc
= GetCurrentDocument();
847 if (doc
->GetCommandProcessor())
848 doc
->GetCommandProcessor()->Undo();
851 void wxDocManager::OnRedo(wxCommandEvent
& WXUNUSED(event
))
853 wxDocument
*doc
= GetCurrentDocument();
856 if (doc
->GetCommandProcessor())
857 doc
->GetCommandProcessor()->Redo();
860 wxView
*wxDocManager::GetCurrentView() const
863 return m_currentView
;
864 if (m_docs
.Number() == 1)
866 wxDocument
* doc
= (wxDocument
*) m_docs
.First()->Data();
867 return doc
->GetFirstView();
869 return (wxView
*) NULL
;
872 // Extend event processing to search the view's event table
873 bool wxDocManager::ProcessEvent(wxEvent
& event
)
875 wxView
* view
= GetCurrentView();
878 if (view
->ProcessEvent(event
))
881 return wxEvtHandler::ProcessEvent(event
);
884 wxDocument
*wxDocManager::CreateDocument(const wxString
& path
, long flags
)
886 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.Number()];
889 for (i
= 0; i
< m_templates
.Number(); i
++)
891 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Nth(i
)->Data());
892 if (temp
->IsVisible())
901 return (wxDocument
*) NULL
;
904 // If we've reached the max number of docs, close the
906 if (GetDocuments().Number() >= m_maxDocsOpen
)
908 wxDocument
*doc
= (wxDocument
*)GetDocuments().First()->Data();
911 // Implicitly deletes the document when
912 // the last view is deleted
913 doc
->DeleteAllViews();
915 // Check we're really deleted
916 if (m_docs
.Member(doc
))
920 return (wxDocument
*) NULL
;
923 // New document: user chooses a template, unless there's only one.
924 if (flags
& wxDOC_NEW
)
928 wxDocTemplate
*temp
= templates
[0];
930 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
933 newDoc
->SetDocumentName(temp
->GetDocumentName());
934 newDoc
->SetDocumentTemplate(temp
);
935 newDoc
->OnNewDocument();
940 wxDocTemplate
*temp
= SelectDocumentType(templates
, n
);
944 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
947 newDoc
->SetDocumentName(temp
->GetDocumentName());
948 newDoc
->SetDocumentTemplate(temp
);
949 newDoc
->OnNewDocument();
954 return (wxDocument
*) NULL
;
958 wxDocTemplate
*temp
= (wxDocTemplate
*) NULL
;
960 wxString
path2(_T(""));
964 if (flags
& wxDOC_SILENT
)
965 temp
= FindTemplateForPath(path2
);
967 temp
= SelectDocumentPath(templates
, n
, path2
, flags
);
973 wxDocument
*newDoc
= temp
->CreateDocument(path2
, flags
);
976 newDoc
->SetDocumentName(temp
->GetDocumentName());
977 newDoc
->SetDocumentTemplate(temp
);
978 if (!newDoc
->OnOpenDocument(path2
))
981 return (wxDocument
*) NULL
;
983 AddFileToHistory(path2
);
988 return (wxDocument
*) NULL
;
991 wxView
*wxDocManager::CreateView(wxDocument
*doc
, long flags
)
993 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.Number()];
996 for (i
= 0; i
< m_templates
.Number(); i
++)
998 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Nth(i
)->Data());
999 if (temp
->IsVisible())
1001 if (temp
->GetDocumentName() == doc
->GetDocumentName())
1003 templates
[n
] = temp
;
1011 return (wxView
*) NULL
;
1015 wxDocTemplate
*temp
= templates
[0];
1017 wxView
*view
= temp
->CreateView(doc
, flags
);
1019 view
->SetViewName(temp
->GetViewName());
1023 wxDocTemplate
*temp
= SelectViewType(templates
, n
);
1027 wxView
*view
= temp
->CreateView(doc
, flags
);
1029 view
->SetViewName(temp
->GetViewName());
1033 return (wxView
*) NULL
;
1036 // Not yet implemented
1037 void wxDocManager::DeleteTemplate(wxDocTemplate
*WXUNUSED(temp
), long WXUNUSED(flags
))
1041 // Not yet implemented
1042 bool wxDocManager::FlushDoc(wxDocument
*WXUNUSED(doc
))
1047 wxDocument
*wxDocManager::GetCurrentDocument() const
1050 return m_currentView
->GetDocument();
1052 return (wxDocument
*) NULL
;
1055 // Make a default document name
1056 bool wxDocManager::MakeDefaultName(wxString
& name
)
1058 name
.Printf(_("unnamed%d"), m_defaultDocumentNameCounter
);
1059 m_defaultDocumentNameCounter
++;
1064 // Not yet implemented
1065 wxDocTemplate
*wxDocManager::MatchTemplate(const wxString
& WXUNUSED(path
))
1067 return (wxDocTemplate
*) NULL
;
1070 // File history management
1071 void wxDocManager::AddFileToHistory(const wxString
& file
)
1074 m_fileHistory
->AddFileToHistory(file
);
1077 wxString
wxDocManager::GetHistoryFile(int i
) const
1082 histFile
= m_fileHistory
->GetHistoryFile(i
);
1087 void wxDocManager::FileHistoryUseMenu(wxMenu
*menu
)
1090 m_fileHistory
->UseMenu(menu
);
1093 void wxDocManager::FileHistoryRemoveMenu(wxMenu
*menu
)
1096 m_fileHistory
->RemoveMenu(menu
);
1100 void wxDocManager::FileHistoryLoad(wxConfigBase
& config
)
1103 m_fileHistory
->Load(config
);
1106 void wxDocManager::FileHistorySave(wxConfigBase
& config
)
1109 m_fileHistory
->Save(config
);
1113 void wxDocManager::FileHistoryAddFilesToMenu(wxMenu
* menu
)
1116 m_fileHistory
->AddFilesToMenu(menu
);
1119 void wxDocManager::FileHistoryAddFilesToMenu()
1122 m_fileHistory
->AddFilesToMenu();
1125 int wxDocManager::GetNoHistoryFiles() const
1128 return m_fileHistory
->GetNoHistoryFiles();
1134 // Find out the document template via matching in the document file format
1135 // against that of the template
1136 wxDocTemplate
*wxDocManager::FindTemplateForPath(const wxString
& path
)
1138 wxDocTemplate
*theTemplate
= (wxDocTemplate
*) NULL
;
1140 // Find the template which this extension corresponds to
1142 for (i
= 0; i
< m_templates
.Number(); i
++)
1144 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Nth(i
)->Data();
1145 if ( temp
->FileMatchesTemplate(path
) )
1154 // Prompts user to open a file, using file specs in templates.
1155 // How to implement in wxWindows? Must extend the file selector
1156 // dialog or implement own; OR match the extension to the
1157 // template extension.
1159 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**templates
,
1163 int WXUNUSED(noTemplates
),
1166 long WXUNUSED(flags
),
1167 bool WXUNUSED(save
))
1169 // We can only have multiple filters in Windows
1174 for (i
= 0; i
< noTemplates
; i
++)
1176 if (templates
[i
]->IsVisible())
1178 // add a '|' to separate this filter from the previous one
1179 if ( !descrBuf
.IsEmpty() )
1180 descrBuf
<< _T('|');
1182 descrBuf
<< templates
[i
]->GetDescription()
1183 << _T(" (") << templates
[i
]->GetFileFilter() << _T(") |")
1184 << templates
[i
]->GetFileFilter();
1188 wxString descrBuf
= _T("*.*");
1191 int FilterIndex
= 0;
1192 wxString pathTmp
= wxFileSelectorEx(_("Select a file"),
1198 wxTheApp
->GetTopWindow());
1200 if (!pathTmp
.IsEmpty())
1203 wxString theExt
= FindExtension(path
);
1205 return (wxDocTemplate
*) NULL
;
1207 // This is dodgy in that we're selecting the template on the
1208 // basis of the file extension, which may not be a standard
1209 // one. We really want to know exactly which template was
1210 // chosen by using a more advanced file selector.
1211 wxDocTemplate
*theTemplate
= FindTemplateForPath(path
);
1213 theTemplate
= templates
[FilterIndex
];
1220 return (wxDocTemplate
*) NULL
;
1223 // In all other windowing systems, until we have more advanced
1224 // file selectors, we must select the document type (template) first, and
1225 // _then_ pop up the file selector.
1226 wxDocTemplate
*temp
= SelectDocumentType(templates
, noTemplates
);
1228 return (wxDocTemplate
*) NULL
;
1230 wxChar
*pathTmp
= wxFileSelector(_("Select a file"), _T(""), _T(""),
1231 temp
->GetDefaultExtension(),
1232 temp
->GetFileFilter(),
1233 0, wxTheApp
->GetTopWindow());
1241 return (wxDocTemplate
*) NULL
;
1245 wxDocTemplate
*wxDocManager::SelectDocumentType(wxDocTemplate
**templates
,
1248 wxChar
**strings
= new wxChar
*[noTemplates
];
1249 wxChar
**data
= new wxChar
*[noTemplates
];
1252 for (i
= 0; i
< noTemplates
; i
++)
1254 if (templates
[i
]->IsVisible())
1256 strings
[n
] = WXSTRINGCAST templates
[i
]->m_description
;
1257 data
[n
] = (wxChar
*)templates
[i
];
1265 return (wxDocTemplate
*) NULL
;
1269 wxDocTemplate
*temp
= (wxDocTemplate
*)data
[0];
1275 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData(_("Select a document template"), _("Templates"), n
,
1282 wxDocTemplate
*wxDocManager::SelectViewType(wxDocTemplate
**templates
,
1285 wxChar
**strings
= new wxChar
*[noTemplates
];
1286 wxChar
**data
= new wxChar
*[noTemplates
];
1289 for (i
= 0; i
< noTemplates
; i
++)
1291 if (templates
[i
]->IsVisible() && (templates
[i
]->GetViewName() != _T("")))
1293 strings
[n
] = WXSTRINGCAST templates
[i
]->m_viewTypeName
;
1294 data
[n
] = (wxChar
*)templates
[i
];
1298 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData(_("Select a document view"), _("Views"), n
,
1305 void wxDocManager::AssociateTemplate(wxDocTemplate
*temp
)
1307 if (!m_templates
.Member(temp
))
1308 m_templates
.Append(temp
);
1311 void wxDocManager::DisassociateTemplate(wxDocTemplate
*temp
)
1313 m_templates
.DeleteObject(temp
);
1316 // Add and remove a document from the manager's list
1317 void wxDocManager::AddDocument(wxDocument
*doc
)
1319 if (!m_docs
.Member(doc
))
1323 void wxDocManager::RemoveDocument(wxDocument
*doc
)
1325 m_docs
.DeleteObject(doc
);
1328 // Views or windows should inform the document manager
1329 // when a view is going in or out of focus
1330 void wxDocManager::ActivateView(wxView
*view
, bool activate
, bool WXUNUSED(deleting
))
1332 // If we're deactiving, and if we're not actually deleting the view, then
1333 // don't reset the current view because we may be going to
1334 // a window without a view.
1335 // WHAT DID I MEAN BY THAT EXACTLY?
1339 if (m_currentView == view)
1340 m_currentView = NULL;
1346 m_currentView
= view
;
1348 m_currentView
= (wxView
*) NULL
;
1352 // ----------------------------------------------------------------------------
1353 // Default document child frame
1354 // ----------------------------------------------------------------------------
1356 BEGIN_EVENT_TABLE(wxDocChildFrame
, wxFrame
)
1357 EVT_ACTIVATE(wxDocChildFrame::OnActivate
)
1358 EVT_CLOSE(wxDocChildFrame::OnCloseWindow
)
1361 wxDocChildFrame::wxDocChildFrame(wxDocument
*doc
,
1365 const wxString
& title
,
1369 const wxString
& name
)
1370 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1372 m_childDocument
= doc
;
1375 view
->SetFrame(this);
1378 wxDocChildFrame::~wxDocChildFrame()
1382 // Extend event processing to search the view's event table
1383 bool wxDocChildFrame::ProcessEvent(wxEvent
& event
)
1386 m_childView
->Activate(TRUE
);
1388 if ( !m_childView
|| ! m_childView
->ProcessEvent(event
) )
1390 // Only hand up to the parent if it's a menu command
1391 if (!event
.IsKindOf(CLASSINFO(wxCommandEvent
)) || !GetParent() || !GetParent()->ProcessEvent(event
))
1392 return wxEvtHandler::ProcessEvent(event
);
1400 void wxDocChildFrame::OnActivate(wxActivateEvent
& event
)
1402 wxFrame::OnActivate(event
);
1405 m_childView
->Activate(event
.GetActive());
1408 void wxDocChildFrame::OnCloseWindow(wxCloseEvent
& event
)
1413 if (!event
.CanVeto())
1414 ans
= TRUE
; // Must delete.
1416 ans
= m_childView
->Close(FALSE
); // FALSE means don't delete associated window
1420 m_childView
->Activate(FALSE
);
1422 m_childView
= (wxView
*) NULL
;
1423 m_childDocument
= (wxDocument
*) NULL
;
1434 // ----------------------------------------------------------------------------
1435 // Default parent frame
1436 // ----------------------------------------------------------------------------
1438 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1439 EVT_MENU(wxID_EXIT
, wxDocParentFrame::OnExit
)
1440 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE9
, wxDocParentFrame::OnMRUFile
)
1441 EVT_CLOSE(wxDocParentFrame::OnCloseWindow
)
1444 wxDocParentFrame::wxDocParentFrame(wxDocManager
*manager
,
1447 const wxString
& title
,
1451 const wxString
& name
)
1452 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1454 m_docManager
= manager
;
1457 void wxDocParentFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
1462 void wxDocParentFrame::OnMRUFile(wxCommandEvent
& event
)
1464 wxString
f(m_docManager
->GetHistoryFile(event
.GetSelection() - wxID_FILE1
));
1466 (void)m_docManager
->CreateDocument(f
, wxDOC_SILENT
);
1469 // Extend event processing to search the view's event table
1470 bool wxDocParentFrame::ProcessEvent(wxEvent
& event
)
1472 // Try the document manager, then do default processing
1473 if (!m_docManager
|| !m_docManager
->ProcessEvent(event
))
1474 return wxEvtHandler::ProcessEvent(event
);
1479 // Define the behaviour for the frame closing
1480 // - must delete all frames except for the main one.
1481 void wxDocParentFrame::OnCloseWindow(wxCloseEvent
& event
)
1483 if (m_docManager
->Clear(!event
.CanVeto()))
1491 #if wxUSE_PRINTING_ARCHITECTURE
1493 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
)
1494 : wxPrintout(WXSTRINGCAST title
)
1496 m_printoutView
= view
;
1499 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
1503 // Get the logical pixels per inch of screen and printer
1504 int ppiScreenX
, ppiScreenY
;
1505 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
1506 int ppiPrinterX
, ppiPrinterY
;
1507 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
1509 // This scales the DC so that the printout roughly represents the
1510 // the screen scaling. The text point size _should_ be the right size
1511 // but in fact is too small for some reason. This is a detail that will
1512 // need to be addressed at some point but can be fudged for the
1514 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
1516 // Now we have to check in case our real page size is reduced
1517 // (e.g. because we're drawing to a print preview memory DC)
1518 int pageWidth
, pageHeight
;
1520 dc
->GetSize(&w
, &h
);
1521 GetPageSizePixels(&pageWidth
, &pageHeight
);
1523 // If printer pageWidth == current DC width, then this doesn't
1524 // change. But w might be the preview bitmap width, so scale down.
1525 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
1526 dc
->SetUserScale(overallScale
, overallScale
);
1530 m_printoutView
->OnDraw(dc
);
1535 bool wxDocPrintout::HasPage(int pageNum
)
1537 return (pageNum
== 1);
1540 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
1542 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
1548 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
1556 #endif // wxUSE_PRINTING_ARCHITECTURE
1558 // ----------------------------------------------------------------------------
1559 // Command processing framework
1560 // ----------------------------------------------------------------------------
1562 wxCommand::wxCommand(bool canUndoIt
, const wxString
& name
)
1564 m_canUndo
= canUndoIt
;
1565 m_commandName
= name
;
1568 wxCommand::~wxCommand()
1572 // Command processor
1573 wxCommandProcessor::wxCommandProcessor(int maxCommands
)
1575 m_maxNoCommands
= maxCommands
;
1576 m_currentCommand
= (wxNode
*) NULL
;
1577 m_commandEditMenu
= (wxMenu
*) NULL
;
1580 wxCommandProcessor::~wxCommandProcessor()
1585 // Pass a command to the processor. The processor calls Do();
1586 // if successful, is appended to the command history unless
1587 // storeIt is FALSE.
1588 bool wxCommandProcessor::Submit(wxCommand
*command
, bool storeIt
)
1590 bool success
= command
->Do();
1591 if (success
&& storeIt
)
1593 if (m_commands
.Number() == m_maxNoCommands
)
1595 wxNode
*firstNode
= m_commands
.First();
1596 wxCommand
*firstCommand
= (wxCommand
*)firstNode
->Data();
1597 delete firstCommand
;
1601 // Correct a bug: we must chop off the current 'branch'
1602 // so that we're at the end of the command list.
1603 if (!m_currentCommand
)
1607 wxNode
*node
= m_currentCommand
->Next();
1610 wxNode
*next
= node
->Next();
1611 delete (wxCommand
*)node
->Data();
1617 m_commands
.Append(command
);
1618 m_currentCommand
= m_commands
.Last();
1624 bool wxCommandProcessor::Undo()
1626 if (m_currentCommand
)
1628 wxCommand
*command
= (wxCommand
*)m_currentCommand
->Data();
1629 if (command
->CanUndo())
1631 bool success
= command
->Undo();
1634 m_currentCommand
= m_currentCommand
->Previous();
1643 bool wxCommandProcessor::Redo()
1645 wxCommand
*redoCommand
= (wxCommand
*) NULL
;
1646 wxNode
*redoNode
= (wxNode
*) NULL
;
1647 if (m_currentCommand
&& m_currentCommand
->Next())
1649 redoCommand
= (wxCommand
*)m_currentCommand
->Next()->Data();
1650 redoNode
= m_currentCommand
->Next();
1654 if (m_commands
.Number() > 0)
1656 redoCommand
= (wxCommand
*)m_commands
.First()->Data();
1657 redoNode
= m_commands
.First();
1663 bool success
= redoCommand
->Do();
1666 m_currentCommand
= redoNode
;
1674 bool wxCommandProcessor::CanUndo() const
1676 if (m_currentCommand
)
1677 return ((wxCommand
*)m_currentCommand
->Data())->CanUndo();
1681 bool wxCommandProcessor::CanRedo() const
1683 if ((m_currentCommand
!= (wxNode
*) NULL
) && (m_currentCommand
->Next() == (wxNode
*) NULL
))
1686 if ((m_currentCommand
!= (wxNode
*) NULL
) && (m_currentCommand
->Next() != (wxNode
*) NULL
))
1689 if ((m_currentCommand
== (wxNode
*) NULL
) && (m_commands
.Number() > 0))
1695 void wxCommandProcessor::Initialize()
1697 m_currentCommand
= m_commands
.Last();
1701 void wxCommandProcessor::SetMenuStrings()
1703 if (m_commandEditMenu
)
1706 if (m_currentCommand
)
1708 wxCommand
*command
= (wxCommand
*)m_currentCommand
->Data();
1709 wxString
commandName(command
->GetName());
1710 if (commandName
== _T("")) commandName
= _("Unnamed command");
1711 bool canUndo
= command
->CanUndo();
1713 buf
= wxString(_("&Undo ")) + commandName
;
1715 buf
= wxString(_("Can't &Undo ")) + commandName
;
1717 m_commandEditMenu
->SetLabel(wxID_UNDO
, buf
);
1718 m_commandEditMenu
->Enable(wxID_UNDO
, canUndo
);
1720 // We can redo, if we're not at the end of the history.
1721 if (m_currentCommand
->Next())
1723 wxCommand
*redoCommand
= (wxCommand
*)m_currentCommand
->Next()->Data();
1724 wxString
redoCommandName(redoCommand
->GetName());
1725 if (redoCommandName
== _T("")) redoCommandName
= _("Unnamed command");
1726 buf
= wxString(_("&Redo ")) + redoCommandName
;
1727 m_commandEditMenu
->SetLabel(wxID_REDO
, buf
);
1728 m_commandEditMenu
->Enable(wxID_REDO
, TRUE
);
1732 m_commandEditMenu
->SetLabel(wxID_REDO
, _("&Redo"));
1733 m_commandEditMenu
->Enable(wxID_REDO
, FALSE
);
1738 m_commandEditMenu
->SetLabel(wxID_UNDO
, _("&Undo"));
1739 m_commandEditMenu
->Enable(wxID_UNDO
, FALSE
);
1741 if (m_commands
.Number() == 0)
1743 m_commandEditMenu
->SetLabel(wxID_REDO
, _("&Redo"));
1744 m_commandEditMenu
->Enable(wxID_REDO
, FALSE
);
1748 // currentCommand is NULL but there are commands: this means that
1749 // we've undone to the start of the list, but can redo the first.
1750 wxCommand
*redoCommand
= (wxCommand
*)m_commands
.First()->Data();
1751 wxString
redoCommandName(redoCommand
->GetName());
1752 if (redoCommandName
== _T("")) redoCommandName
= _("Unnamed command");
1753 buf
= wxString(_("&Redo ")) + redoCommandName
;
1754 m_commandEditMenu
->SetLabel(wxID_REDO
, buf
);
1755 m_commandEditMenu
->Enable(wxID_REDO
, TRUE
);
1761 void wxCommandProcessor::ClearCommands()
1763 wxNode
*node
= m_commands
.First();
1766 wxCommand
*command
= (wxCommand
*)node
->Data();
1769 node
= m_commands
.First();
1771 m_currentCommand
= (wxNode
*) NULL
;
1774 // ----------------------------------------------------------------------------
1775 // File history processor
1776 // ----------------------------------------------------------------------------
1778 wxFileHistory::wxFileHistory(int maxFiles
)
1780 m_fileMaxFiles
= maxFiles
;
1782 m_fileHistory
= new wxChar
*[m_fileMaxFiles
];
1785 wxFileHistory::~wxFileHistory()
1788 for (i
= 0; i
< m_fileHistoryN
; i
++)
1789 delete[] m_fileHistory
[i
];
1790 delete[] m_fileHistory
;
1793 // File history management
1794 void wxFileHistory::AddFileToHistory(const wxString
& file
)
1797 // Check we don't already have this file
1798 for (i
= 0; i
< m_fileHistoryN
; i
++)
1800 if (m_fileHistory
[i
] && wxString(m_fileHistory
[i
]) == file
)
1804 // Add to the project file history:
1805 // Move existing files (if any) down so we can insert file at beginning.
1807 // First delete filename that has popped off the end of the array (if any)
1808 if (m_fileHistoryN
== m_fileMaxFiles
)
1810 delete[] m_fileHistory
[m_fileMaxFiles
-1];
1811 m_fileHistory
[m_fileMaxFiles
-1] = (wxChar
*) NULL
;
1813 if (m_fileHistoryN
< m_fileMaxFiles
)
1815 wxNode
* node
= m_fileMenus
.First();
1818 wxMenu
* menu
= (wxMenu
*) node
->Data();
1819 if (m_fileHistoryN
== 0)
1820 menu
->AppendSeparator();
1821 menu
->Append(wxID_FILE1
+m_fileHistoryN
, _("[EMPTY]"));
1822 node
= node
->Next();
1826 // Shuffle filenames down
1827 for (i
= (m_fileHistoryN
-1); i
> 0; i
--)
1829 m_fileHistory
[i
] = m_fileHistory
[i
-1];
1831 m_fileHistory
[0] = copystring(file
);
1833 for (i
= 0; i
< m_fileHistoryN
; i
++)
1834 if (m_fileHistory
[i
])
1837 buf
.Printf(_T("&%d %s"), i
+1, m_fileHistory
[i
]);
1838 wxNode
* node
= m_fileMenus
.First();
1841 wxMenu
* menu
= (wxMenu
*) node
->Data();
1842 menu
->SetLabel(wxID_FILE1
+i
, buf
);
1843 node
= node
->Next();
1848 wxString
wxFileHistory::GetHistoryFile(int i
) const
1850 if (i
< m_fileHistoryN
)
1851 return wxString(m_fileHistory
[i
]);
1853 return wxString("");
1856 void wxFileHistory::UseMenu(wxMenu
*menu
)
1858 if (!m_fileMenus
.Member(menu
))
1859 m_fileMenus
.Append(menu
);
1862 void wxFileHistory::RemoveMenu(wxMenu
*menu
)
1864 m_fileMenus
.DeleteObject(menu
);
1868 void wxFileHistory::Load(wxConfigBase
& config
)
1872 buf
.Printf(_T("file%d"), m_fileHistoryN
+1);
1873 wxString historyFile
;
1874 while ((m_fileHistoryN
<= m_fileMaxFiles
) && config
.Read(buf
, &historyFile
) && (historyFile
!= _T("")))
1876 m_fileHistory
[m_fileHistoryN
] = copystring((const wxChar
*) historyFile
);
1878 buf
.Printf(_T("file%d"), m_fileHistoryN
+1);
1884 void wxFileHistory::Save(wxConfigBase
& config
)
1887 for (i
= 0; i
< m_fileHistoryN
; i
++)
1890 buf
.Printf(_T("file%d"), i
+1);
1891 config
.Write(buf
, wxString(m_fileHistory
[i
]));
1894 #endif // wxUSE_CONFIG
1896 void wxFileHistory::AddFilesToMenu()
1898 if (m_fileHistoryN
> 0)
1900 wxNode
* node
= m_fileMenus
.First();
1903 wxMenu
* menu
= (wxMenu
*) node
->Data();
1904 menu
->AppendSeparator();
1906 for (i
= 0; i
< m_fileHistoryN
; i
++)
1908 if (m_fileHistory
[i
])
1911 buf
.Printf(_T("&%d %s"), i
+1, m_fileHistory
[i
]);
1912 menu
->Append(wxID_FILE1
+i
, buf
);
1915 node
= node
->Next();
1920 void wxFileHistory::AddFilesToMenu(wxMenu
* menu
)
1922 if (m_fileHistoryN
> 0)
1924 menu
->AppendSeparator();
1926 for (i
= 0; i
< m_fileHistoryN
; i
++)
1928 if (m_fileHistory
[i
])
1931 buf
.Printf(_T("&%d %s"), i
+1, m_fileHistory
[i
]);
1932 menu
->Append(wxID_FILE1
+i
, buf
);
1938 // ----------------------------------------------------------------------------
1939 // Permits compatibility with existing file formats and functions that
1940 // manipulate files directly
1941 // ----------------------------------------------------------------------------
1943 bool wxTransferFileToStream(const wxString
& filename
, ostream
& stream
)
1948 if ((fd1
= fopen (filename
.fn_str(), "rb")) == NULL
)
1951 while ((ch
= getc (fd1
)) != EOF
)
1952 stream
<< (unsigned char)ch
;
1958 bool wxTransferStreamToFile(istream
& stream
, const wxString
& filename
)
1963 if ((fd1
= fopen (filename
.fn_str(), "wb")) == NULL
)
1968 while (!stream
.eof())
1978 #endif // wxUSE_DOC_VIEW_ARCHITECTURE