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/printdlg.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(void) const
183 if (m_documentViews
.Number() == 0)
184 return (wxView
*) NULL
;
185 return (wxView
*)m_documentViews
.First()->Data();
188 wxDocManager
*wxDocument::GetDocumentManager(void) 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
== "" || !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
);
246 wxSplitPath(fileName
, & path
, & name
, & ext
);
248 if (ext
.IsEmpty() || ext
== "")
251 fileName
+= docTemplate
->GetDefaultExtension();
254 SetFilename(fileName
);
255 SetTitle(wxFileNameFromPath(fileName
));
257 GetDocumentManager()->AddFileToHistory(fileName
);
259 // Notify the views that the filename has changed
260 wxNode
*node
= m_documentViews
.First();
263 wxView
*view
= (wxView
*)node
->Data();
264 view
->OnChangeFilename();
268 return OnSaveDocument(m_documentFile
);
271 bool wxDocument::OnSaveDocument(const wxString
& file
)
277 if (wxTheApp
->GetAppName() != "")
278 msgTitle
= wxTheApp
->GetAppName();
280 msgTitle
= wxString(_("File error"));
282 ofstream
store(file
.fn_str());
283 if (store
.fail() || store
.bad())
285 (void)wxMessageBox(_("Sorry, could not open this file for saving."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
286 GetDocumentWindow());
290 if (SaveObject(store
)==FALSE
)
292 (void)wxMessageBox(_("Sorry, could not save this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
293 GetDocumentWindow());
302 bool wxDocument::OnOpenDocument(const wxString
& file
)
304 if (!OnSaveModified())
308 if (wxTheApp
->GetAppName() != "")
309 msgTitle
= wxTheApp
->GetAppName();
311 msgTitle
= wxString(_("File error"));
313 ifstream
store(file
.fn_str());
314 if (store
.fail() || store
.bad())
316 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
|wxICON_EXCLAMATION
,
317 GetDocumentWindow());
320 if (LoadObject(store
)==FALSE
)
322 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
|wxICON_EXCLAMATION
,
323 GetDocumentWindow());
326 SetFilename(file
, TRUE
);
335 istream
& wxDocument::LoadObject(istream
& stream
)
337 // wxObject::LoadObject(stream);
342 ostream
& wxDocument::SaveObject(ostream
& stream
)
344 // wxObject::SaveObject(stream);
349 bool wxDocument::Revert()
355 // Get title, or filename if no title, else unnamed
356 bool wxDocument::GetPrintableName(wxString
& buf
) const
358 if (m_documentTitle
!= "")
360 buf
= m_documentTitle
;
363 else if (m_documentFile
!= "")
365 buf
= wxFileNameFromPath(m_documentFile
);
375 wxWindow
*wxDocument::GetDocumentWindow(void) const
377 wxView
*view
= GetFirstView();
379 return view
->GetFrame();
381 return wxTheApp
->GetTopWindow();
384 wxCommandProcessor
*wxDocument::OnCreateCommandProcessor()
386 return new wxCommandProcessor
;
389 // TRUE if safe to close
390 bool wxDocument::OnSaveModified()
395 GetPrintableName(title
);
398 if (wxTheApp
->GetAppName() != "")
399 msgTitle
= wxTheApp
->GetAppName();
401 msgTitle
= wxString(_("Warning"));
404 prompt
.Printf(_("Do you want to save changes to document %s?"),
405 (const wxChar
*)title
);
406 int res
= wxMessageBox(prompt
, msgTitle
,
407 wxYES_NO
|wxCANCEL
|wxICON_QUESTION
,
408 GetDocumentWindow());
414 else if (res
== wxYES
)
416 else if (res
== wxCANCEL
)
422 bool wxDocument::Draw(wxDC
& WXUNUSED(context
))
427 bool wxDocument::AddView(wxView
*view
)
429 if (!m_documentViews
.Member(view
))
431 m_documentViews
.Append(view
);
437 bool wxDocument::RemoveView(wxView
*view
)
439 (void)m_documentViews
.DeleteObject(view
);
444 bool wxDocument::OnCreate(const wxString
& WXUNUSED(path
), long flags
)
446 if (GetDocumentTemplate()->CreateView(this, flags
))
452 // Called after a view is added or removed.
453 // The default implementation deletes the document if
454 // there are no more views.
455 void wxDocument::OnChangedViewList()
457 if (m_documentViews
.Number() == 0)
459 if (OnSaveModified())
466 void wxDocument::UpdateAllViews(wxView
*sender
, wxObject
*hint
)
468 wxNode
*node
= m_documentViews
.First();
471 wxView
*view
= (wxView
*)node
->Data();
472 view
->OnUpdate(sender
, hint
);
477 void wxDocument::SetFilename(const wxString
& filename
, bool notifyViews
)
479 m_documentFile
= filename
;
482 // Notify the views that the filename has changed
483 wxNode
*node
= m_documentViews
.First();
486 wxView
*view
= (wxView
*)node
->Data();
487 view
->OnChangeFilename();
493 // ----------------------------------------------------------------------------
495 // ----------------------------------------------------------------------------
500 m_viewDocument
= (wxDocument
*) NULL
;
503 m_viewFrame
= (wxFrame
*) NULL
;
508 GetDocumentManager()->ActivateView(this, FALSE
, TRUE
);
509 m_viewDocument
->RemoveView(this);
512 // Extend event processing to search the document's event table
513 bool wxView::ProcessEvent(wxEvent
& event
)
515 if ( !GetDocument() || !GetDocument()->ProcessEvent(event
) )
516 return wxEvtHandler::ProcessEvent(event
);
521 void wxView::OnActivateView(bool WXUNUSED(activate
), wxView
*WXUNUSED(activeView
), wxView
*WXUNUSED(deactiveView
))
525 void wxView::OnPrint(wxDC
*dc
, wxObject
*WXUNUSED(info
))
530 void wxView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
))
534 void wxView::OnChangeFilename()
536 if (GetFrame() && GetDocument())
539 GetDocument()->GetPrintableName(name
);
541 GetFrame()->SetTitle(name
);
545 void wxView::SetDocument(wxDocument
*doc
)
547 m_viewDocument
= doc
;
552 bool wxView::Close(bool deleteWindow
)
554 if (OnClose(deleteWindow
))
560 void wxView::Activate(bool activate
)
562 if (GetDocumentManager())
564 OnActivateView(activate
, this, GetDocumentManager()->GetCurrentView());
565 GetDocumentManager()->ActivateView(this, activate
);
569 bool wxView::OnClose(bool WXUNUSED(deleteWindow
))
571 return GetDocument() ? GetDocument()->Close() : TRUE
;
574 #if wxUSE_PRINTING_ARCHITECTURE
575 wxPrintout
*wxView::OnCreatePrintout()
577 return new wxDocPrintout(this);
581 // ----------------------------------------------------------------------------
583 // ----------------------------------------------------------------------------
585 wxDocTemplate::wxDocTemplate(wxDocManager
*manager
,
586 const wxString
& descr
,
587 const wxString
& filter
,
590 const wxString
& docTypeName
,
591 const wxString
& viewTypeName
,
592 wxClassInfo
*docClassInfo
,
593 wxClassInfo
*viewClassInfo
,
596 m_documentManager
= manager
;
597 m_description
= descr
;
600 m_fileFilter
= filter
;
602 m_docTypeName
= docTypeName
;
603 m_viewTypeName
= viewTypeName
;
604 m_documentManager
->AssociateTemplate(this);
606 m_docClassInfo
= docClassInfo
;
607 m_viewClassInfo
= viewClassInfo
;
610 wxDocTemplate::~wxDocTemplate()
612 m_documentManager
->DisassociateTemplate(this);
615 // Tries to dynamically construct an object of the right class.
616 wxDocument
*wxDocTemplate::CreateDocument(const wxString
& path
, long flags
)
619 return (wxDocument
*) NULL
;
620 wxDocument
*doc
= (wxDocument
*)m_docClassInfo
->CreateObject();
621 doc
->SetFilename(path
);
622 doc
->SetDocumentTemplate(this);
623 GetDocumentManager()->AddDocument(doc
);
624 doc
->SetCommandProcessor(doc
->OnCreateCommandProcessor());
626 if (doc
->OnCreate(path
, flags
))
631 return (wxDocument
*) NULL
;
635 wxView
*wxDocTemplate::CreateView(wxDocument
*doc
, long flags
)
637 if (!m_viewClassInfo
)
638 return (wxView
*) NULL
;
639 wxView
*view
= (wxView
*)m_viewClassInfo
->CreateObject();
640 view
->SetDocument(doc
);
641 if (view
->OnCreate(doc
, flags
))
648 return (wxView
*) NULL
;
652 // ----------------------------------------------------------------------------
654 // ----------------------------------------------------------------------------
656 BEGIN_EVENT_TABLE(wxDocManager
, wxEvtHandler
)
657 EVT_MENU(wxID_OPEN
, wxDocManager::OnFileOpen
)
658 EVT_MENU(wxID_CLOSE
, wxDocManager::OnFileClose
)
659 EVT_MENU(wxID_REVERT
, wxDocManager::OnFileRevert
)
660 EVT_MENU(wxID_NEW
, wxDocManager::OnFileNew
)
661 EVT_MENU(wxID_SAVE
, wxDocManager::OnFileSave
)
662 EVT_MENU(wxID_SAVEAS
, wxDocManager::OnFileSaveAs
)
663 EVT_MENU(wxID_UNDO
, wxDocManager::OnUndo
)
664 EVT_MENU(wxID_REDO
, wxDocManager::OnRedo
)
665 EVT_MENU(wxID_PRINT
, wxDocManager::OnPrint
)
666 EVT_MENU(wxID_PRINT_SETUP
, wxDocManager::OnPrintSetup
)
667 EVT_MENU(wxID_PREVIEW
, wxDocManager::OnPreview
)
670 wxDocManager::wxDocManager(long flags
, bool initialize
)
672 m_defaultDocumentNameCounter
= 1;
674 m_currentView
= (wxView
*) NULL
;
675 m_maxDocsOpen
= 10000;
676 m_fileHistory
= (wxFileHistory
*) NULL
;
681 wxDocManager::~wxDocManager()
685 delete m_fileHistory
;
688 bool wxDocManager::Clear(bool force
)
690 wxNode
*node
= m_docs
.First();
693 wxDocument
*doc
= (wxDocument
*)node
->Data();
694 wxNode
*next
= node
->Next();
696 if (!doc
->Close() && !force
)
699 // Implicitly deletes the document when the last
700 // view is removed (deleted)
701 doc
->DeleteAllViews();
703 // Check document is deleted
704 if (m_docs
.Member(doc
))
707 // This assumes that documents are not connected in
708 // any way, i.e. deleting one document does NOT
712 node
= m_templates
.First();
715 wxDocTemplate
*templ
= (wxDocTemplate
*) node
->Data();
716 wxNode
* next
= node
->Next();
723 bool wxDocManager::Initialize()
725 m_fileHistory
= OnCreateFileHistory();
729 wxFileHistory
*wxDocManager::OnCreateFileHistory()
731 return new wxFileHistory
;
734 void wxDocManager::OnFileClose(wxCommandEvent
& WXUNUSED(event
))
736 wxDocument
*doc
= GetCurrentDocument();
741 doc
->DeleteAllViews();
742 if (m_docs
.Member(doc
))
747 void wxDocManager::OnFileNew(wxCommandEvent
& WXUNUSED(event
))
749 CreateDocument(wxString(""), wxDOC_NEW
);
752 void wxDocManager::OnFileOpen(wxCommandEvent
& WXUNUSED(event
))
754 CreateDocument(wxString(""), 0);
757 void wxDocManager::OnFileRevert(wxCommandEvent
& WXUNUSED(event
))
759 wxDocument
*doc
= GetCurrentDocument();
765 void wxDocManager::OnFileSave(wxCommandEvent
& WXUNUSED(event
))
767 wxDocument
*doc
= GetCurrentDocument();
773 void wxDocManager::OnFileSaveAs(wxCommandEvent
& WXUNUSED(event
))
775 wxDocument
*doc
= GetCurrentDocument();
781 void wxDocManager::OnPrint(wxCommandEvent
& WXUNUSED(event
))
783 wxView
*view
= GetCurrentView();
787 wxPrintout
*printout
= view
->OnCreatePrintout();
791 printer
.Print(view
->GetFrame(), printout
, TRUE
);
797 void wxDocManager::OnPrintSetup(wxCommandEvent
& WXUNUSED(event
))
799 wxWindow
*parentWin
= wxTheApp
->GetTopWindow();
800 wxView
*view
= GetCurrentView();
802 parentWin
= view
->GetFrame();
804 wxPrintDialogData data
;
806 wxPrintDialog
printerDialog(parentWin
, & data
);
807 printerDialog
.GetPrintDialogData().SetSetupDialog(TRUE
);
808 printerDialog
.ShowModal();
811 void wxDocManager::OnPreview(wxCommandEvent
& WXUNUSED(event
))
813 wxView
*view
= GetCurrentView();
817 wxPrintout
*printout
= view
->OnCreatePrintout();
820 // Pass two printout objects: for preview, and possible printing.
821 wxPrintPreviewBase
*preview
= (wxPrintPreviewBase
*) NULL
;
822 preview
= new wxPrintPreview(printout
, view
->OnCreatePrintout());
824 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, (wxFrame
*)wxTheApp
->GetTopWindow(), _("Print Preview"),
825 wxPoint(100, 100), wxSize(600, 650));
826 frame
->Centre(wxBOTH
);
832 void wxDocManager::OnUndo(wxCommandEvent
& WXUNUSED(event
))
834 wxDocument
*doc
= GetCurrentDocument();
837 if (doc
->GetCommandProcessor())
838 doc
->GetCommandProcessor()->Undo();
841 void wxDocManager::OnRedo(wxCommandEvent
& WXUNUSED(event
))
843 wxDocument
*doc
= GetCurrentDocument();
846 if (doc
->GetCommandProcessor())
847 doc
->GetCommandProcessor()->Redo();
850 wxView
*wxDocManager::GetCurrentView(void) const
853 return m_currentView
;
854 if (m_docs
.Number() == 1)
856 wxDocument
* doc
= (wxDocument
*) m_docs
.First()->Data();
857 return doc
->GetFirstView();
859 return (wxView
*) NULL
;
862 // Extend event processing to search the view's event table
863 bool wxDocManager::ProcessEvent(wxEvent
& event
)
865 wxView
* view
= GetCurrentView();
868 if (view
->ProcessEvent(event
))
871 return wxEvtHandler::ProcessEvent(event
);
874 wxDocument
*wxDocManager::CreateDocument(const wxString
& path
, long flags
)
876 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.Number()];
879 for (i
= 0; i
< m_templates
.Number(); i
++)
881 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Nth(i
)->Data());
882 if (temp
->IsVisible())
891 return (wxDocument
*) NULL
;
894 // If we've reached the max number of docs, close the
896 if (GetDocuments().Number() >= m_maxDocsOpen
)
898 wxDocument
*doc
= (wxDocument
*)GetDocuments().First()->Data();
901 // Implicitly deletes the document when
902 // the last view is deleted
903 doc
->DeleteAllViews();
905 // Check we're really deleted
906 if (m_docs
.Member(doc
))
910 return (wxDocument
*) NULL
;
913 // New document: user chooses a template, unless there's only one.
914 if (flags
& wxDOC_NEW
)
918 wxDocTemplate
*temp
= templates
[0];
920 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
923 newDoc
->SetDocumentName(temp
->GetDocumentName());
924 newDoc
->SetDocumentTemplate(temp
);
925 newDoc
->OnNewDocument();
930 wxDocTemplate
*temp
= SelectDocumentType(templates
, n
);
934 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
937 newDoc
->SetDocumentName(temp
->GetDocumentName());
938 newDoc
->SetDocumentTemplate(temp
);
939 newDoc
->OnNewDocument();
944 return (wxDocument
*) NULL
;
948 wxDocTemplate
*temp
= (wxDocTemplate
*) NULL
;
954 if (flags
& wxDOC_SILENT
)
955 temp
= FindTemplateForPath(path2
);
957 temp
= SelectDocumentPath(templates
, n
, path2
, flags
);
963 wxDocument
*newDoc
= temp
->CreateDocument(path2
, flags
);
966 newDoc
->SetDocumentName(temp
->GetDocumentName());
967 newDoc
->SetDocumentTemplate(temp
);
968 if (!newDoc
->OnOpenDocument(path2
))
971 return (wxDocument
*) NULL
;
973 AddFileToHistory(path2
);
978 return (wxDocument
*) NULL
;
981 wxView
*wxDocManager::CreateView(wxDocument
*doc
, long flags
)
983 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.Number()];
986 for (i
= 0; i
< m_templates
.Number(); i
++)
988 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Nth(i
)->Data());
989 if (temp
->IsVisible())
991 if (temp
->GetDocumentName() == doc
->GetDocumentName())
1001 return (wxView
*) NULL
;
1005 wxDocTemplate
*temp
= templates
[0];
1007 wxView
*view
= temp
->CreateView(doc
, flags
);
1009 view
->SetViewName(temp
->GetViewName());
1013 wxDocTemplate
*temp
= SelectViewType(templates
, n
);
1017 wxView
*view
= temp
->CreateView(doc
, flags
);
1019 view
->SetViewName(temp
->GetViewName());
1023 return (wxView
*) NULL
;
1026 // Not yet implemented
1027 void wxDocManager::DeleteTemplate(wxDocTemplate
*WXUNUSED(temp
), long WXUNUSED(flags
))
1031 // Not yet implemented
1032 bool wxDocManager::FlushDoc(wxDocument
*WXUNUSED(doc
))
1037 wxDocument
*wxDocManager::GetCurrentDocument(void) const
1040 return m_currentView
->GetDocument();
1042 return (wxDocument
*) NULL
;
1045 // Make a default document name
1046 bool wxDocManager::MakeDefaultName(wxString
& name
)
1048 name
.Printf(_("unnamed%d"), m_defaultDocumentNameCounter
);
1049 m_defaultDocumentNameCounter
++;
1054 // Not yet implemented
1055 wxDocTemplate
*wxDocManager::MatchTemplate(const wxString
& WXUNUSED(path
))
1057 return (wxDocTemplate
*) NULL
;
1060 // File history management
1061 void wxDocManager::AddFileToHistory(const wxString
& file
)
1064 m_fileHistory
->AddFileToHistory(file
);
1067 wxString
wxDocManager::GetHistoryFile(int i
) const
1072 histFile
= m_fileHistory
->GetHistoryFile(i
);
1077 void wxDocManager::FileHistoryUseMenu(wxMenu
*menu
)
1080 m_fileHistory
->UseMenu(menu
);
1083 void wxDocManager::FileHistoryRemoveMenu(wxMenu
*menu
)
1086 m_fileHistory
->RemoveMenu(menu
);
1090 void wxDocManager::FileHistoryLoad(wxConfigBase
& config
)
1093 m_fileHistory
->Load(config
);
1096 void wxDocManager::FileHistorySave(wxConfigBase
& config
)
1099 m_fileHistory
->Save(config
);
1103 void wxDocManager::FileHistoryAddFilesToMenu(wxMenu
* menu
)
1106 m_fileHistory
->AddFilesToMenu(menu
);
1109 void wxDocManager::FileHistoryAddFilesToMenu()
1112 m_fileHistory
->AddFilesToMenu();
1115 int wxDocManager::GetNoHistoryFiles(void) const
1118 return m_fileHistory
->GetNoHistoryFiles();
1124 // Given a path, try to find a matching template. Won't always work, of
1126 wxDocTemplate
*wxDocManager::FindTemplateForPath(const wxString
& path
)
1128 wxString theExt
= FindExtension(path
);
1130 return (wxDocTemplate
*) NULL
;
1131 wxDocTemplate
*theTemplate
= (wxDocTemplate
*) NULL
;
1133 if (m_templates
.Number() == 1)
1134 return (wxDocTemplate
*)m_templates
.First()->Data();
1136 // Find the template which this extension corresponds to
1138 for (i
= 0; i
< m_templates
.Number(); i
++)
1140 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Nth(i
)->Data();
1141 if (wxStrcmp(temp
->GetDefaultExtension(), theExt
) == 0)
1150 // Prompts user to open a file, using file specs in templates.
1151 // How to implement in wxWindows? Must extend the file selector
1152 // dialog or implement own; OR match the extension to the
1153 // template extension.
1155 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**templates
,
1158 long WXUNUSED(flags
),
1159 bool WXUNUSED(save
))
1161 // We can only have multiple filters in Windows
1166 for (i
= 0; i
< noTemplates
; i
++)
1168 if (templates
[i
]->IsVisible())
1170 // add a '|' to separate this filter from the previous one
1171 if ( !descrBuf
.IsEmpty() )
1172 descrBuf
<< _T('|');
1174 descrBuf
<< templates
[i
]->GetDescription()
1175 << _T(" (") << templates
[i
]->GetFileFilter() << _T(") |")
1176 << templates
[i
]->GetFileFilter();
1180 wxString descrBuf
= _T("*.*");
1183 wxString pathTmp
= wxFileSelector(_("Select a file"), _T(""), _T(""), _T(""),
1184 descrBuf
, 0, wxTheApp
->GetTopWindow());
1186 if (!pathTmp
.IsEmpty())
1189 wxString theExt
= FindExtension(path
);
1191 return (wxDocTemplate
*) NULL
;
1193 // This is dodgy in that we're selecting the template on the
1194 // basis of the file extension, which may not be a standard
1195 // one. We really want to know exactly which template was
1196 // chosen by using a more advanced file selector.
1197 wxDocTemplate
*theTemplate
= FindTemplateForPath(path
);
1203 return (wxDocTemplate
*) NULL
;
1206 // In all other windowing systems, until we have more advanced
1207 // file selectors, we must select the document type (template) first, and
1208 // _then_ pop up the file selector.
1209 wxDocTemplate
*temp
= SelectDocumentType(templates
, noTemplates
);
1211 return (wxDocTemplate
*) NULL
;
1213 char *pathTmp
= wxFileSelector(_("Select a file"), "", "",
1214 temp
->GetDefaultExtension(),
1215 temp
->GetFileFilter(),
1216 0, wxTheApp
->GetTopWindow());
1224 return (wxDocTemplate
*) NULL
;
1228 wxDocTemplate
*wxDocManager::SelectDocumentType(wxDocTemplate
**templates
,
1231 wxChar
**strings
= new wxChar
*[noTemplates
];
1232 wxChar
**data
= new wxChar
*[noTemplates
];
1235 for (i
= 0; i
< noTemplates
; i
++)
1237 if (templates
[i
]->IsVisible())
1239 strings
[n
] = WXSTRINGCAST templates
[i
]->m_description
;
1240 data
[n
] = (wxChar
*)templates
[i
];
1248 return (wxDocTemplate
*) NULL
;
1252 wxDocTemplate
*temp
= (wxDocTemplate
*)data
[0];
1258 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData(_("Select a document template"), _("Templates"), n
,
1265 wxDocTemplate
*wxDocManager::SelectViewType(wxDocTemplate
**templates
,
1268 wxChar
**strings
= new wxChar
*[noTemplates
];
1269 wxChar
**data
= new wxChar
*[noTemplates
];
1272 for (i
= 0; i
< noTemplates
; i
++)
1274 if (templates
[i
]->IsVisible() && (templates
[i
]->GetViewName() != ""))
1276 strings
[n
] = WXSTRINGCAST templates
[i
]->m_viewTypeName
;
1277 data
[n
] = (wxChar
*)templates
[i
];
1281 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData(_("Select a document view"), _("Views"), n
,
1288 void wxDocManager::AssociateTemplate(wxDocTemplate
*temp
)
1290 if (!m_templates
.Member(temp
))
1291 m_templates
.Append(temp
);
1294 void wxDocManager::DisassociateTemplate(wxDocTemplate
*temp
)
1296 m_templates
.DeleteObject(temp
);
1299 // Add and remove a document from the manager's list
1300 void wxDocManager::AddDocument(wxDocument
*doc
)
1302 if (!m_docs
.Member(doc
))
1306 void wxDocManager::RemoveDocument(wxDocument
*doc
)
1308 m_docs
.DeleteObject(doc
);
1311 // Views or windows should inform the document manager
1312 // when a view is going in or out of focus
1313 void wxDocManager::ActivateView(wxView
*view
, bool activate
, bool WXUNUSED(deleting
))
1315 // If we're deactiving, and if we're not actually deleting the view, then
1316 // don't reset the current view because we may be going to
1317 // a window without a view.
1318 // WHAT DID I MEAN BY THAT EXACTLY?
1322 if (m_currentView == view)
1323 m_currentView = NULL;
1329 m_currentView
= view
;
1331 m_currentView
= (wxView
*) NULL
;
1335 // ----------------------------------------------------------------------------
1336 // Default document child frame
1337 // ----------------------------------------------------------------------------
1339 BEGIN_EVENT_TABLE(wxDocChildFrame
, wxFrame
)
1340 EVT_ACTIVATE(wxDocChildFrame::OnActivate
)
1341 EVT_CLOSE(wxDocChildFrame::OnCloseWindow
)
1344 wxDocChildFrame::wxDocChildFrame(wxDocument
*doc
,
1348 const wxString
& title
,
1352 const wxString
& name
)
1353 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1355 m_childDocument
= doc
;
1358 view
->SetFrame(this);
1361 wxDocChildFrame::~wxDocChildFrame()
1365 // Extend event processing to search the view's event table
1366 bool wxDocChildFrame::ProcessEvent(wxEvent
& event
)
1369 m_childView
->Activate(TRUE
);
1371 if ( !m_childView
|| ! m_childView
->ProcessEvent(event
) )
1373 // Only hand up to the parent if it's a menu command
1374 if (!event
.IsKindOf(CLASSINFO(wxCommandEvent
)) || !GetParent() || !GetParent()->ProcessEvent(event
))
1375 return wxEvtHandler::ProcessEvent(event
);
1383 void wxDocChildFrame::OnActivate(wxActivateEvent
& event
)
1385 wxFrame::OnActivate(event
);
1388 m_childView
->Activate(event
.GetActive());
1391 void wxDocChildFrame::OnCloseWindow(wxCloseEvent
& event
)
1396 if (!event
.CanVeto())
1397 ans
= TRUE
; // Must delete.
1399 ans
= m_childView
->Close(FALSE
); // FALSE means don't delete associated window
1403 m_childView
->Activate(FALSE
);
1405 m_childView
= (wxView
*) NULL
;
1406 m_childDocument
= (wxDocument
*) NULL
;
1417 // ----------------------------------------------------------------------------
1418 // Default parent frame
1419 // ----------------------------------------------------------------------------
1421 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1422 EVT_MENU(wxID_EXIT
, wxDocParentFrame::OnExit
)
1423 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE9
, wxDocParentFrame::OnMRUFile
)
1424 EVT_CLOSE(wxDocParentFrame::OnCloseWindow
)
1427 wxDocParentFrame::wxDocParentFrame(wxDocManager
*manager
,
1430 const wxString
& title
,
1434 const wxString
& name
)
1435 : wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1437 m_docManager
= manager
;
1440 void wxDocParentFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
1445 void wxDocParentFrame::OnMRUFile(wxCommandEvent
& event
)
1447 wxString
f(m_docManager
->GetHistoryFile(event
.GetSelection() - wxID_FILE1
));
1449 (void)m_docManager
->CreateDocument(f
, wxDOC_SILENT
);
1452 // Extend event processing to search the view's event table
1453 bool wxDocParentFrame::ProcessEvent(wxEvent
& event
)
1455 // Try the document manager, then do default processing
1456 if (!m_docManager
|| !m_docManager
->ProcessEvent(event
))
1457 return wxEvtHandler::ProcessEvent(event
);
1462 // Define the behaviour for the frame closing
1463 // - must delete all frames except for the main one.
1464 void wxDocParentFrame::OnCloseWindow(wxCloseEvent
& event
)
1466 if (m_docManager
->Clear(!event
.CanVeto()))
1474 #if wxUSE_PRINTING_ARCHITECTURE
1476 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
)
1477 : wxPrintout(WXSTRINGCAST title
)
1479 m_printoutView
= view
;
1482 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
1486 // Get the logical pixels per inch of screen and printer
1487 int ppiScreenX
, ppiScreenY
;
1488 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
1489 int ppiPrinterX
, ppiPrinterY
;
1490 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
1492 // This scales the DC so that the printout roughly represents the
1493 // the screen scaling. The text point size _should_ be the right size
1494 // but in fact is too small for some reason. This is a detail that will
1495 // need to be addressed at some point but can be fudged for the
1497 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
1499 // Now we have to check in case our real page size is reduced
1500 // (e.g. because we're drawing to a print preview memory DC)
1501 int pageWidth
, pageHeight
;
1503 dc
->GetSize(&w
, &h
);
1504 GetPageSizePixels(&pageWidth
, &pageHeight
);
1506 // If printer pageWidth == current DC width, then this doesn't
1507 // change. But w might be the preview bitmap width, so scale down.
1508 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
1509 dc
->SetUserScale(overallScale
, overallScale
);
1513 m_printoutView
->OnDraw(dc
);
1518 bool wxDocPrintout::HasPage(int pageNum
)
1520 return (pageNum
== 1);
1523 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
1525 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
1531 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
1539 #endif // wxUSE_PRINTING_ARCHITECTURE
1541 // ----------------------------------------------------------------------------
1542 // Command processing framework
1543 // ----------------------------------------------------------------------------
1545 wxCommand::wxCommand(bool canUndoIt
, const wxString
& name
)
1547 m_canUndo
= canUndoIt
;
1548 m_commandName
= name
;
1551 wxCommand::~wxCommand()
1555 // Command processor
1556 wxCommandProcessor::wxCommandProcessor(int maxCommands
)
1558 m_maxNoCommands
= maxCommands
;
1559 m_currentCommand
= (wxNode
*) NULL
;
1560 m_commandEditMenu
= (wxMenu
*) NULL
;
1563 wxCommandProcessor::~wxCommandProcessor()
1568 // Pass a command to the processor. The processor calls Do();
1569 // if successful, is appended to the command history unless
1570 // storeIt is FALSE.
1571 bool wxCommandProcessor::Submit(wxCommand
*command
, bool storeIt
)
1573 bool success
= command
->Do();
1574 if (success
&& storeIt
)
1576 if (m_commands
.Number() == m_maxNoCommands
)
1578 wxNode
*firstNode
= m_commands
.First();
1579 wxCommand
*firstCommand
= (wxCommand
*)firstNode
->Data();
1580 delete firstCommand
;
1584 // Correct a bug: we must chop off the current 'branch'
1585 // so that we're at the end of the command list.
1586 if (!m_currentCommand
)
1590 wxNode
*node
= m_currentCommand
->Next();
1593 wxNode
*next
= node
->Next();
1594 delete (wxCommand
*)node
->Data();
1600 m_commands
.Append(command
);
1601 m_currentCommand
= m_commands
.Last();
1607 bool wxCommandProcessor::Undo()
1609 if (m_currentCommand
)
1611 wxCommand
*command
= (wxCommand
*)m_currentCommand
->Data();
1612 if (command
->CanUndo())
1614 bool success
= command
->Undo();
1617 m_currentCommand
= m_currentCommand
->Previous();
1626 bool wxCommandProcessor::Redo()
1628 wxCommand
*redoCommand
= (wxCommand
*) NULL
;
1629 wxNode
*redoNode
= (wxNode
*) NULL
;
1630 if (m_currentCommand
&& m_currentCommand
->Next())
1632 redoCommand
= (wxCommand
*)m_currentCommand
->Next()->Data();
1633 redoNode
= m_currentCommand
->Next();
1637 if (m_commands
.Number() > 0)
1639 redoCommand
= (wxCommand
*)m_commands
.First()->Data();
1640 redoNode
= m_commands
.First();
1646 bool success
= redoCommand
->Do();
1649 m_currentCommand
= redoNode
;
1657 bool wxCommandProcessor::CanUndo(void) const
1659 if (m_currentCommand
)
1660 return ((wxCommand
*)m_currentCommand
->Data())->CanUndo();
1664 bool wxCommandProcessor::CanRedo(void) const
1666 if ((m_currentCommand
!= (wxNode
*) NULL
) && (m_currentCommand
->Next() == (wxNode
*) NULL
))
1669 if ((m_currentCommand
!= (wxNode
*) NULL
) && (m_currentCommand
->Next() != (wxNode
*) NULL
))
1672 if ((m_currentCommand
== (wxNode
*) NULL
) && (m_commands
.Number() > 0))
1678 void wxCommandProcessor::Initialize()
1680 m_currentCommand
= m_commands
.Last();
1684 void wxCommandProcessor::SetMenuStrings()
1686 if (m_commandEditMenu
)
1689 if (m_currentCommand
)
1691 wxCommand
*command
= (wxCommand
*)m_currentCommand
->Data();
1692 wxString
commandName(command
->GetName());
1693 if (commandName
== "") commandName
= _("Unnamed command");
1694 bool canUndo
= command
->CanUndo();
1696 buf
= wxString(_("&Undo ")) + commandName
;
1698 buf
= wxString(_("Can't &Undo ")) + commandName
;
1700 m_commandEditMenu
->SetLabel(wxID_UNDO
, buf
);
1701 m_commandEditMenu
->Enable(wxID_UNDO
, canUndo
);
1703 // We can redo, if we're not at the end of the history.
1704 if (m_currentCommand
->Next())
1706 wxCommand
*redoCommand
= (wxCommand
*)m_currentCommand
->Next()->Data();
1707 wxString
redoCommandName(redoCommand
->GetName());
1708 if (redoCommandName
== "") redoCommandName
= _("Unnamed command");
1709 buf
= wxString(_("&Redo ")) + redoCommandName
;
1710 m_commandEditMenu
->SetLabel(wxID_REDO
, buf
);
1711 m_commandEditMenu
->Enable(wxID_REDO
, TRUE
);
1715 m_commandEditMenu
->SetLabel(wxID_REDO
, _("&Redo"));
1716 m_commandEditMenu
->Enable(wxID_REDO
, FALSE
);
1721 m_commandEditMenu
->SetLabel(wxID_UNDO
, _("&Undo"));
1722 m_commandEditMenu
->Enable(wxID_UNDO
, FALSE
);
1724 if (m_commands
.Number() == 0)
1726 m_commandEditMenu
->SetLabel(wxID_REDO
, _("&Redo"));
1727 m_commandEditMenu
->Enable(wxID_REDO
, FALSE
);
1731 // currentCommand is NULL but there are commands: this means that
1732 // we've undone to the start of the list, but can redo the first.
1733 wxCommand
*redoCommand
= (wxCommand
*)m_commands
.First()->Data();
1734 wxString
redoCommandName(redoCommand
->GetName());
1735 if (redoCommandName
== "") redoCommandName
= _("Unnamed command");
1736 buf
= wxString(_("&Redo ")) + redoCommandName
;
1737 m_commandEditMenu
->SetLabel(wxID_REDO
, buf
);
1738 m_commandEditMenu
->Enable(wxID_REDO
, TRUE
);
1744 void wxCommandProcessor::ClearCommands()
1746 wxNode
*node
= m_commands
.First();
1749 wxCommand
*command
= (wxCommand
*)node
->Data();
1752 node
= m_commands
.First();
1754 m_currentCommand
= (wxNode
*) NULL
;
1757 // ----------------------------------------------------------------------------
1758 // File history processor
1759 // ----------------------------------------------------------------------------
1761 wxFileHistory::wxFileHistory(int maxFiles
)
1763 m_fileMaxFiles
= maxFiles
;
1765 m_fileHistory
= new wxChar
*[m_fileMaxFiles
];
1768 wxFileHistory::~wxFileHistory()
1771 for (i
= 0; i
< m_fileHistoryN
; i
++)
1772 delete[] m_fileHistory
[i
];
1773 delete[] m_fileHistory
;
1776 // File history management
1777 void wxFileHistory::AddFileToHistory(const wxString
& file
)
1780 // Check we don't already have this file
1781 for (i
= 0; i
< m_fileHistoryN
; i
++)
1783 if (m_fileHistory
[i
] && wxString(m_fileHistory
[i
]) == file
)
1787 // Add to the project file history:
1788 // Move existing files (if any) down so we can insert file at beginning.
1790 // First delete filename that has popped off the end of the array (if any)
1791 if (m_fileHistoryN
== m_fileMaxFiles
)
1793 delete[] m_fileHistory
[m_fileMaxFiles
-1];
1794 m_fileHistory
[m_fileMaxFiles
-1] = (wxChar
*) NULL
;
1796 if (m_fileHistoryN
< m_fileMaxFiles
)
1798 wxNode
* node
= m_fileMenus
.First();
1801 wxMenu
* menu
= (wxMenu
*) node
->Data();
1802 if (m_fileHistoryN
== 0)
1803 menu
->AppendSeparator();
1804 menu
->Append(wxID_FILE1
+m_fileHistoryN
, _("[EMPTY]"));
1805 node
= node
->Next();
1809 // Shuffle filenames down
1810 for (i
= (m_fileHistoryN
-1); i
> 0; i
--)
1812 m_fileHistory
[i
] = m_fileHistory
[i
-1];
1814 m_fileHistory
[0] = copystring(file
);
1816 for (i
= 0; i
< m_fileHistoryN
; i
++)
1817 if (m_fileHistory
[i
])
1820 buf
.Printf(_T("&%d %s"), i
+1, m_fileHistory
[i
]);
1821 wxNode
* node
= m_fileMenus
.First();
1824 wxMenu
* menu
= (wxMenu
*) node
->Data();
1825 menu
->SetLabel(wxID_FILE1
+i
, buf
);
1826 node
= node
->Next();
1831 wxString
wxFileHistory::GetHistoryFile(int i
) const
1833 if (i
< m_fileHistoryN
)
1834 return wxString(m_fileHistory
[i
]);
1836 return wxString("");
1839 void wxFileHistory::UseMenu(wxMenu
*menu
)
1841 if (!m_fileMenus
.Member(menu
))
1842 m_fileMenus
.Append(menu
);
1845 void wxFileHistory::RemoveMenu(wxMenu
*menu
)
1847 m_fileMenus
.DeleteObject(menu
);
1851 void wxFileHistory::Load(wxConfigBase
& config
)
1855 buf
.Printf(_T("file%d"), m_fileHistoryN
+1);
1856 wxString historyFile
;
1857 while ((m_fileHistoryN
<= m_fileMaxFiles
) && config
.Read(buf
, &historyFile
) && (historyFile
!= ""))
1859 m_fileHistory
[m_fileHistoryN
] = copystring((const wxChar
*) historyFile
);
1861 buf
.Printf(_T("file%d"), m_fileHistoryN
+1);
1867 void wxFileHistory::Save(wxConfigBase
& config
)
1870 for (i
= 0; i
< m_fileHistoryN
; i
++)
1873 buf
.Printf(_T("file%d"), i
+1);
1874 config
.Write(buf
, wxString(m_fileHistory
[i
]));
1877 #endif // wxUSE_CONFIG
1879 void wxFileHistory::AddFilesToMenu()
1881 if (m_fileHistoryN
> 0)
1883 wxNode
* node
= m_fileMenus
.First();
1886 wxMenu
* menu
= (wxMenu
*) node
->Data();
1887 menu
->AppendSeparator();
1889 for (i
= 0; i
< m_fileHistoryN
; i
++)
1891 if (m_fileHistory
[i
])
1894 buf
.Printf(_T("&%d %s"), i
+1, m_fileHistory
[i
]);
1895 menu
->Append(wxID_FILE1
+i
, buf
);
1898 node
= node
->Next();
1903 void wxFileHistory::AddFilesToMenu(wxMenu
* menu
)
1905 if (m_fileHistoryN
> 0)
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
);
1921 // ----------------------------------------------------------------------------
1922 // Permits compatibility with existing file formats and functions that
1923 // manipulate files directly
1924 // ----------------------------------------------------------------------------
1926 bool wxTransferFileToStream(const wxString
& filename
, ostream
& stream
)
1931 if ((fd1
= fopen (filename
.fn_str(), "rb")) == NULL
)
1934 while ((ch
= getc (fd1
)) != EOF
)
1935 stream
<< (unsigned char)ch
;
1941 bool wxTransferStreamToFile(istream
& stream
, const wxString
& filename
)
1946 if ((fd1
= fopen (filename
.fn_str(), "wb")) == NULL
)
1951 while (!stream
.eof())
1961 #endif // wxUSE_DOC_VIEW_ARCHITECTURE