1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Document/view classes
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "docview.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
27 #if wxUSE_DOC_VIEW_ARCHITECTURE
30 #include "wx/string.h"
34 #include "wx/dialog.h"
37 #include "wx/filedlg.h"
45 #include "wx/msgdlg.h"
46 #include "wx/choicdlg.h"
47 #include "wx/docview.h"
48 #include "wx/printdlg.h"
49 #include "wx/confbase.h"
65 #if !USE_SHARED_LIBRARY
66 IMPLEMENT_ABSTRACT_CLASS(wxDocument
, wxEvtHandler
)
67 IMPLEMENT_ABSTRACT_CLASS(wxView
, wxEvtHandler
)
68 IMPLEMENT_ABSTRACT_CLASS(wxDocTemplate
, wxObject
)
69 IMPLEMENT_DYNAMIC_CLASS(wxDocManager
, wxEvtHandler
)
70 IMPLEMENT_CLASS(wxDocChildFrame
, wxFrame
)
71 IMPLEMENT_CLASS(wxDocParentFrame
, wxFrame
)
72 #if wxUSE_PRINTING_ARCHITECTURE
73 IMPLEMENT_DYNAMIC_CLASS(wxDocPrintout
, wxPrintout
)
75 IMPLEMENT_CLASS(wxCommand
, wxObject
)
76 IMPLEMENT_DYNAMIC_CLASS(wxCommandProcessor
, wxObject
)
77 IMPLEMENT_DYNAMIC_CLASS(wxFileHistory
, wxObject
)
78 // IMPLEMENT_DYNAMIC_CLASS(wxPrintInfo, wxObject)
82 * Definition of wxDocument
85 wxDocument::wxDocument(wxDocument
*parent
)
87 m_documentModified
=FALSE
;
90 m_documentParent
=parent
;
91 m_documentTemplate
= (wxDocTemplate
*) NULL
;
92 m_documentTypeName
= "";
96 bool wxDocument::DeleteContents(void)
101 wxDocument::~wxDocument(void)
105 if (m_commandProcessor
)
106 delete m_commandProcessor
;
108 GetDocumentManager()->RemoveDocument(this);
110 // Not safe to do here, since it'll
111 // invoke virtual view functions expecting to see
112 // valid derived objects: and by the time we get
113 // here, we've called destructors higher up.
117 bool wxDocument::Close(void)
119 if (OnSaveModified())
120 return OnCloseDocument();
125 bool wxDocument::OnCloseDocument(void)
132 // Note that this implicitly deletes the document when
133 // the last view is deleted.
134 bool wxDocument::DeleteAllViews(void)
136 wxNode
*node
= m_documentViews
.First();
139 wxView
*view
= (wxView
*)node
->Data();
143 wxNode
*next
= node
->Next();
145 delete view
; // Deletes node implicitly
151 wxView
*wxDocument::GetFirstView(void) const
153 if (m_documentViews
.Number() == 0)
154 return (wxView
*) NULL
;
155 return (wxView
*)m_documentViews
.First()->Data();
158 wxDocManager
*wxDocument::GetDocumentManager(void) const
160 return m_documentTemplate
->GetDocumentManager();
163 bool wxDocument::OnNewDocument(void)
165 if (!OnSaveModified())
168 if (OnCloseDocument()==FALSE
) return FALSE
;
171 SetDocumentSaved(FALSE
);
174 GetDocumentManager()->MakeDefaultName(name
);
176 SetFilename(name
, TRUE
);
181 bool wxDocument::Save(void)
185 if (!IsModified()) return TRUE
;
186 if (m_documentFile
== "" || !m_savedYet
)
189 ret
= OnSaveDocument(m_documentFile
);
191 SetDocumentSaved(TRUE
);
195 bool wxDocument::SaveAs(void)
197 wxDocTemplate
*docTemplate
= GetDocumentTemplate();
201 wxString tmp
= wxFileSelector(_("Save as"),
202 docTemplate
->GetDirectory(),
204 docTemplate
->GetDefaultExtension(),
205 docTemplate
->GetFileFilter(),
206 wxSAVE
| wxOVERWRITE_PROMPT
,
207 GetDocumentWindow());
212 wxString
fileName(tmp
);
216 wxSplitPath(fileName
, & path
, & name
, & ext
);
218 if (ext
.IsEmpty() || ext
== "")
221 fileName
+= docTemplate
->GetDefaultExtension();
224 SetFilename(fileName
);
225 SetTitle(wxFileNameFromPath(fileName
));
227 GetDocumentManager()->AddFileToHistory(fileName
);
229 // Notify the views that the filename has changed
230 wxNode
*node
= m_documentViews
.First();
233 wxView
*view
= (wxView
*)node
->Data();
234 view
->OnChangeFilename();
238 return OnSaveDocument(m_documentFile
);
241 bool wxDocument::OnSaveDocument(const wxString
& file
)
247 if (wxTheApp
->GetAppName() != "")
248 msgTitle
= wxTheApp
->GetAppName();
250 msgTitle
= wxString(_("File error"));
252 ofstream
store(file
);
253 if (store
.fail() || store
.bad())
255 (void)wxMessageBox(_("Sorry, could not open this file for saving."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
256 GetDocumentWindow());
260 if (SaveObject(store
)==FALSE
)
262 (void)wxMessageBox(_("Sorry, could not save this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
263 GetDocumentWindow());
272 bool wxDocument::OnOpenDocument(const wxString
& file
)
274 if (!OnSaveModified())
278 if (wxTheApp
->GetAppName() != "")
279 msgTitle
= wxTheApp
->GetAppName();
281 msgTitle
= wxString(_("File error"));
283 ifstream
store(file
);
284 if (store
.fail() || store
.bad())
286 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
|wxICON_EXCLAMATION
,
287 GetDocumentWindow());
290 if (LoadObject(store
)==FALSE
)
292 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
|wxICON_EXCLAMATION
,
293 GetDocumentWindow());
296 SetFilename(file
, TRUE
);
305 istream
& wxDocument::LoadObject(istream
& stream
)
307 // wxObject::LoadObject(stream);
312 ostream
& wxDocument::SaveObject(ostream
& stream
)
314 // wxObject::SaveObject(stream);
319 bool wxDocument::Revert(void)
325 // Get title, or filename if no title, else unnamed
326 bool wxDocument::GetPrintableName(wxString
& buf
) const
328 if (m_documentTitle
!= "")
330 buf
= m_documentTitle
;
333 else if (m_documentFile
!= "")
335 buf
= wxFileNameFromPath(m_documentFile
);
345 wxWindow
*wxDocument::GetDocumentWindow(void) const
347 wxView
*view
= GetFirstView();
349 return view
->GetFrame();
351 return wxTheApp
->GetTopWindow();
354 wxCommandProcessor
*wxDocument::OnCreateCommandProcessor(void)
356 return new wxCommandProcessor
;
359 // TRUE if safe to close
360 bool wxDocument::OnSaveModified(void)
365 GetPrintableName(title
);
368 if (wxTheApp
->GetAppName() != "")
369 msgTitle
= wxTheApp
->GetAppName();
371 msgTitle
= wxString(_("Warning"));
374 prompt
.Printf(_("Do you want to save changes to document %s?"),
375 (const char *)title
);
376 int res
= wxMessageBox(prompt
, msgTitle
,
377 wxYES_NO
|wxCANCEL
|wxICON_QUESTION
,
378 GetDocumentWindow());
384 else if (res
== wxYES
)
386 else if (res
== wxCANCEL
)
392 bool wxDocument::Draw(wxDC
& WXUNUSED(context
))
397 bool wxDocument::AddView(wxView
*view
)
399 if (!m_documentViews
.Member(view
))
401 m_documentViews
.Append(view
);
407 bool wxDocument::RemoveView(wxView
*view
)
409 (void)m_documentViews
.DeleteObject(view
);
414 bool wxDocument::OnCreate(const wxString
& WXUNUSED(path
), long flags
)
416 if (GetDocumentTemplate()->CreateView(this, flags
))
422 // Called after a view is added or removed.
423 // The default implementation deletes the document if
424 // there are no more views.
425 void wxDocument::OnChangedViewList(void)
427 if (m_documentViews
.Number() == 0)
429 if (OnSaveModified())
436 void wxDocument::UpdateAllViews(wxView
*sender
, wxObject
*hint
)
438 wxNode
*node
= m_documentViews
.First();
441 wxView
*view
= (wxView
*)node
->Data();
442 view
->OnUpdate(sender
, hint
);
447 void wxDocument::SetFilename(const wxString
& filename
, bool notifyViews
)
449 m_documentFile
= filename
;
452 // Notify the views that the filename has changed
453 wxNode
*node
= m_documentViews
.First();
456 wxView
*view
= (wxView
*)node
->Data();
457 view
->OnChangeFilename();
468 wxView::wxView(wxDocument
*doc
)
473 m_viewFrame
= (wxFrame
*) NULL
;
476 wxView::~wxView(void)
478 GetDocumentManager()->ActivateView(this, FALSE
, TRUE
);
479 m_viewDocument
->RemoveView(this);
482 // Extend event processing to search the document's event table
483 bool wxView::ProcessEvent(wxEvent
& event
)
485 if ( !GetDocument() || !GetDocument()->ProcessEvent(event
) )
486 return wxEvtHandler::ProcessEvent(event
);
491 void wxView::OnActivateView(bool WXUNUSED(activate
), wxView
*WXUNUSED(activeView
), wxView
*WXUNUSED(deactiveView
))
495 void wxView::OnPrint(wxDC
*dc
, wxObject
*WXUNUSED(info
))
500 void wxView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
))
504 void wxView::OnChangeFilename(void)
506 if (GetFrame() && GetDocument())
509 GetDocument()->GetPrintableName(name
);
511 GetFrame()->SetTitle(name
);
515 void wxView::SetDocument(wxDocument
*doc
)
517 m_viewDocument
= doc
;
522 bool wxView::Close(bool deleteWindow
)
524 if (OnClose(deleteWindow
))
530 void wxView::Activate(bool activate
)
532 if (GetDocumentManager())
534 OnActivateView(activate
, this, GetDocumentManager()->GetCurrentView());
535 GetDocumentManager()->ActivateView(this, activate
);
539 bool wxView::OnClose(bool WXUNUSED(deleteWindow
))
541 return GetDocument() ? GetDocument()->Close() : TRUE
;
544 #if wxUSE_PRINTING_ARCHITECTURE
545 wxPrintout
*wxView::OnCreatePrintout(void)
547 return new wxDocPrintout(this);
556 wxDocTemplate::wxDocTemplate(wxDocManager
*manager
, const wxString
& descr
,
557 const wxString
& filter
, const wxString
& dir
, const wxString
& ext
,
558 const wxString
& docTypeName
, const wxString
& viewTypeName
,
559 wxClassInfo
*docClassInfo
, wxClassInfo
*viewClassInfo
, long flags
)
561 m_documentManager
= manager
;
563 m_description
= descr
;
566 m_fileFilter
= filter
;
568 m_docTypeName
= docTypeName
;
569 m_viewTypeName
= viewTypeName
;
570 m_documentManager
->AssociateTemplate(this);
572 m_docClassInfo
= docClassInfo
;
573 m_viewClassInfo
= viewClassInfo
;
576 wxDocTemplate::~wxDocTemplate(void)
578 m_documentManager
->DisassociateTemplate(this);
581 // Tries to dynamically construct an object of the right
583 wxDocument
*wxDocTemplate::CreateDocument(const wxString
& path
, long flags
)
586 return (wxDocument
*) NULL
;
587 wxDocument
*doc
= (wxDocument
*)m_docClassInfo
->CreateObject();
588 doc
->SetFilename(path
);
589 doc
->SetDocumentTemplate(this);
590 GetDocumentManager()->AddDocument(doc
);
591 doc
->SetCommandProcessor(doc
->OnCreateCommandProcessor());
593 if (doc
->OnCreate(path
, flags
))
598 return (wxDocument
*) NULL
;
602 wxView
*wxDocTemplate::CreateView(wxDocument
*doc
, long flags
)
604 if (!m_viewClassInfo
)
605 return (wxView
*) NULL
;
606 wxView
*view
= (wxView
*)m_viewClassInfo
->CreateObject();
607 view
->SetDocument(doc
);
608 if (view
->OnCreate(doc
, flags
))
615 return (wxView
*) NULL
;
619 BEGIN_EVENT_TABLE(wxDocManager
, wxEvtHandler
)
620 EVT_MENU(wxID_OPEN
, wxDocManager::OnFileOpen
)
621 EVT_MENU(wxID_CLOSE
, wxDocManager::OnFileClose
)
622 EVT_MENU(wxID_REVERT
, wxDocManager::OnFileRevert
)
623 EVT_MENU(wxID_NEW
, wxDocManager::OnFileNew
)
624 EVT_MENU(wxID_SAVE
, wxDocManager::OnFileSave
)
625 EVT_MENU(wxID_SAVEAS
, wxDocManager::OnFileSaveAs
)
626 EVT_MENU(wxID_UNDO
, wxDocManager::OnUndo
)
627 EVT_MENU(wxID_REDO
, wxDocManager::OnRedo
)
628 EVT_MENU(wxID_PRINT
, wxDocManager::OnPrint
)
629 EVT_MENU(wxID_PRINT_SETUP
, wxDocManager::OnPrintSetup
)
630 EVT_MENU(wxID_PREVIEW
, wxDocManager::OnPreview
)
633 wxDocManager::wxDocManager(long flags
, bool initialize
)
635 m_defaultDocumentNameCounter
= 1;
637 m_currentView
= (wxView
*) NULL
;
638 m_maxDocsOpen
= 10000;
639 m_fileHistory
= (wxFileHistory
*) NULL
;
644 wxDocManager::~wxDocManager(void)
648 delete m_fileHistory
;
651 bool wxDocManager::Clear(bool force
)
653 wxNode
*node
= m_docs
.First();
656 wxDocument
*doc
= (wxDocument
*)node
->Data();
657 wxNode
*next
= node
->Next();
659 if (!doc
->Close() && !force
)
662 // Implicitly deletes the document when the last
663 // view is removed (deleted)
664 doc
->DeleteAllViews();
666 // Check document is deleted
667 if (m_docs
.Member(doc
))
670 // This assumes that documents are not connected in
671 // any way, i.e. deleting one document does NOT
675 node
= m_templates
.First();
678 wxDocTemplate
*templ
= (wxDocTemplate
*) node
->Data();
679 wxNode
* next
= node
->Next();
686 bool wxDocManager::Initialize(void)
688 m_fileHistory
= OnCreateFileHistory();
692 wxFileHistory
*wxDocManager::OnCreateFileHistory(void)
694 return new wxFileHistory
;
697 void wxDocManager::OnFileClose(wxCommandEvent
& WXUNUSED(event
))
699 wxDocument
*doc
= GetCurrentDocument();
704 doc
->DeleteAllViews();
705 if (m_docs
.Member(doc
))
710 void wxDocManager::OnFileNew(wxCommandEvent
& WXUNUSED(event
))
712 CreateDocument(wxString(""), wxDOC_NEW
);
715 void wxDocManager::OnFileOpen(wxCommandEvent
& WXUNUSED(event
))
717 CreateDocument(wxString(""), 0);
720 void wxDocManager::OnFileRevert(wxCommandEvent
& WXUNUSED(event
))
722 wxDocument
*doc
= GetCurrentDocument();
728 void wxDocManager::OnFileSave(wxCommandEvent
& WXUNUSED(event
))
730 wxDocument
*doc
= GetCurrentDocument();
736 void wxDocManager::OnFileSaveAs(wxCommandEvent
& WXUNUSED(event
))
738 wxDocument
*doc
= GetCurrentDocument();
744 void wxDocManager::OnPrint(wxCommandEvent
& WXUNUSED(event
))
746 wxView
*view
= GetCurrentView();
750 wxPrintout
*printout
= view
->OnCreatePrintout();
754 printer
.Print(view
->GetFrame(), printout
, TRUE
);
760 void wxDocManager::OnPrintSetup(wxCommandEvent
& WXUNUSED(event
))
762 wxWindow
*parentWin
= wxTheApp
->GetTopWindow();
763 wxView
*view
= GetCurrentView();
765 parentWin
= view
->GetFrame();
769 wxPrintDialog
printerDialog(parentWin
, & data
);
770 printerDialog
.GetPrintData().SetSetupDialog(TRUE
);
771 printerDialog
.ShowModal();
774 void wxDocManager::OnPreview(wxCommandEvent
& WXUNUSED(event
))
776 wxView
*view
= GetCurrentView();
780 wxPrintout
*printout
= view
->OnCreatePrintout();
783 // Pass two printout objects: for preview, and possible printing.
784 wxPrintPreviewBase
*preview
= (wxPrintPreviewBase
*) NULL
;
785 preview
= new wxPrintPreview(printout
, view
->OnCreatePrintout());
787 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, (wxFrame
*)wxTheApp
->GetTopWindow(), _("Print Preview"),
788 wxPoint(100, 100), wxSize(600, 650));
789 frame
->Centre(wxBOTH
);
795 void wxDocManager::OnUndo(wxCommandEvent
& WXUNUSED(event
))
797 wxDocument
*doc
= GetCurrentDocument();
800 if (doc
->GetCommandProcessor())
801 doc
->GetCommandProcessor()->Undo();
804 void wxDocManager::OnRedo(wxCommandEvent
& WXUNUSED(event
))
806 wxDocument
*doc
= GetCurrentDocument();
809 if (doc
->GetCommandProcessor())
810 doc
->GetCommandProcessor()->Redo();
813 wxView
*wxDocManager::GetCurrentView(void) const
816 return m_currentView
;
817 if (m_docs
.Number() == 1)
819 wxDocument
* doc
= (wxDocument
*) m_docs
.First()->Data();
820 return doc
->GetFirstView();
822 return (wxView
*) NULL
;
825 // Extend event processing to search the view's event table
826 bool wxDocManager::ProcessEvent(wxEvent
& event
)
828 wxView
* view
= GetCurrentView();
831 if (view
->ProcessEvent(event
))
834 return wxEvtHandler::ProcessEvent(event
);
837 wxDocument
*wxDocManager::CreateDocument(const wxString
& path
, long flags
)
839 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.Number()];
842 for (i
= 0; i
< m_templates
.Number(); i
++)
844 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Nth(i
)->Data());
845 if (temp
->IsVisible())
854 return (wxDocument
*) NULL
;
857 // If we've reached the max number of docs, close the
859 if (GetDocuments().Number() >= m_maxDocsOpen
)
861 wxDocument
*doc
= (wxDocument
*)GetDocuments().First()->Data();
864 // Implicitly deletes the document when
865 // the last view is deleted
866 doc
->DeleteAllViews();
868 // Check we're really deleted
869 if (m_docs
.Member(doc
))
873 return (wxDocument
*) NULL
;
876 // New document: user chooses a template, unless there's only one.
877 if (flags
& wxDOC_NEW
)
881 wxDocTemplate
*temp
= templates
[0];
883 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
886 newDoc
->SetDocumentName(temp
->GetDocumentName());
887 newDoc
->SetDocumentTemplate(temp
);
888 newDoc
->OnNewDocument();
893 wxDocTemplate
*temp
= SelectDocumentType(templates
, n
);
897 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
900 newDoc
->SetDocumentName(temp
->GetDocumentName());
901 newDoc
->SetDocumentTemplate(temp
);
902 newDoc
->OnNewDocument();
907 return (wxDocument
*) NULL
;
911 wxDocTemplate
*temp
= (wxDocTemplate
*) NULL
;
917 if (flags
& wxDOC_SILENT
)
918 temp
= FindTemplateForPath(path2
);
920 temp
= SelectDocumentPath(templates
, n
, path2
, flags
);
926 wxDocument
*newDoc
= temp
->CreateDocument(path2
, flags
);
929 newDoc
->SetDocumentName(temp
->GetDocumentName());
930 newDoc
->SetDocumentTemplate(temp
);
931 if (!newDoc
->OnOpenDocument(path2
))
934 return (wxDocument
*) NULL
;
936 AddFileToHistory(path2
);
941 return (wxDocument
*) NULL
;
944 wxView
*wxDocManager::CreateView(wxDocument
*doc
, long flags
)
946 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.Number()];
949 for (i
= 0; i
< m_templates
.Number(); i
++)
951 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Nth(i
)->Data());
952 if (temp
->IsVisible())
954 if (temp
->GetDocumentName() == doc
->GetDocumentName())
964 return (wxView
*) NULL
;
968 wxDocTemplate
*temp
= templates
[0];
970 wxView
*view
= temp
->CreateView(doc
, flags
);
972 view
->SetViewName(temp
->GetViewName());
976 wxDocTemplate
*temp
= SelectViewType(templates
, n
);
980 wxView
*view
= temp
->CreateView(doc
, flags
);
982 view
->SetViewName(temp
->GetViewName());
986 return (wxView
*) NULL
;
989 // Not yet implemented
990 void wxDocManager::DeleteTemplate(wxDocTemplate
*WXUNUSED(temp
), long WXUNUSED(flags
))
994 // Not yet implemented
995 bool wxDocManager::FlushDoc(wxDocument
*WXUNUSED(doc
))
1000 wxDocument
*wxDocManager::GetCurrentDocument(void) const
1003 return m_currentView
->GetDocument();
1005 return (wxDocument
*) NULL
;
1008 // Make a default document name
1009 bool wxDocManager::MakeDefaultName(wxString
& name
)
1011 name
.Printf(_("unnamed%d"), m_defaultDocumentNameCounter
);
1012 m_defaultDocumentNameCounter
++;
1017 // Not yet implemented
1018 wxDocTemplate
*wxDocManager::MatchTemplate(const wxString
& WXUNUSED(path
))
1020 return (wxDocTemplate
*) NULL
;
1023 // File history management
1024 void wxDocManager::AddFileToHistory(const wxString
& file
)
1027 m_fileHistory
->AddFileToHistory(file
);
1030 wxString
wxDocManager::GetHistoryFile(int i
) const
1033 return wxString(m_fileHistory
->GetHistoryFile(i
));
1035 return wxString("");
1038 void wxDocManager::FileHistoryUseMenu(wxMenu
*menu
)
1041 m_fileHistory
->UseMenu(menu
);
1044 void wxDocManager::FileHistoryRemoveMenu(wxMenu
*menu
)
1047 m_fileHistory
->RemoveMenu(menu
);
1051 void wxDocManager::FileHistoryLoad(wxConfigBase
& config
)
1054 m_fileHistory
->Load(config
);
1057 void wxDocManager::FileHistorySave(wxConfigBase
& config
)
1060 m_fileHistory
->Save(config
);
1064 void wxDocManager::FileHistoryAddFilesToMenu(wxMenu
* menu
)
1067 m_fileHistory
->AddFilesToMenu(menu
);
1070 void wxDocManager::FileHistoryAddFilesToMenu()
1073 m_fileHistory
->AddFilesToMenu();
1076 int wxDocManager::GetNoHistoryFiles(void) const
1079 return m_fileHistory
->GetNoHistoryFiles();
1084 static char *FindExtension(char *path
)
1086 static char ext
[10];
1087 int len
= strlen(path
);
1091 for (i
= (len
-1); i
> 0; i
--)
1097 for (j
= i
+1; j
< len
; j
++)
1098 ext
[(int)(j
-(i
+1))] = (char)wxToLower(path
[j
]); // NOTE Should not use tolower under UNIX
1103 return (char *) NULL
;
1105 else return (char *) NULL
;
1109 // Given a path, try to find a matching template. Won't
1110 // always work, of course.
1111 wxDocTemplate
*wxDocManager::FindTemplateForPath(const wxString
& path
)
1113 char *theExt
= FindExtension((char *)(const char *)path
);
1115 return (wxDocTemplate
*) NULL
;
1116 wxDocTemplate
*theTemplate
= (wxDocTemplate
*) NULL
;
1118 if (m_templates
.Number() == 1)
1119 return (wxDocTemplate
*)m_templates
.First()->Data();
1121 // Find the template which this extension corresponds to
1123 for (i
= 0; i
< m_templates
.Number(); i
++)
1125 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Nth(i
)->Data();
1126 if (strcmp(temp
->GetDefaultExtension(), theExt
) == 0)
1135 // Prompts user to open a file, using file specs in templates.
1136 // How to implement in wxWindows? Must extend the file selector
1137 // dialog or implement own; OR match the extension to the
1138 // template extension.
1141 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**templates
,
1142 int noTemplates
, wxString
& path
, long WXUNUSED(flags
), bool WXUNUSED(save
))
1144 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**WXUNUSED(templates
),
1145 int WXUNUSED(noTemplates
), wxString
& path
, long WXUNUSED(flags
), bool WXUNUSED(save
))
1148 // We can only have multiple filters in Windows
1153 for (i
= 0; i
< noTemplates
; i
++)
1155 if (templates
[i
]->IsVisible())
1157 // add a '|' to separate this filter from the previous one
1158 if ( !descrBuf
.IsEmpty() )
1161 descrBuf
<< templates
[i
]->GetDescription()
1162 << " (" << templates
[i
]->GetFileFilter() << ") |"
1163 << templates
[i
]->GetFileFilter();
1167 wxString descrBuf
= "*.*";
1170 wxString pathTmp
= wxFileSelector(_("Select a file"), "", "", "",
1171 descrBuf
, 0, wxTheApp
->GetTopWindow());
1173 if (!pathTmp
.IsEmpty())
1176 char *theExt
= FindExtension((char *)(const char *)path
);
1178 return (wxDocTemplate
*) NULL
;
1180 // This is dodgy in that we're selecting the template on the
1181 // basis of the file extension, which may not be a standard
1182 // one. We really want to know exactly which template was
1183 // chosen by using a more advanced file selector.
1184 wxDocTemplate
*theTemplate
= FindTemplateForPath(path
);
1190 return (wxDocTemplate
*) NULL
;
1193 // In all other windowing systems, until we have more advanced
1194 // file selectors, we must select the document type (template) first, and
1195 // _then_ pop up the file selector.
1196 wxDocTemplate
*temp
= SelectDocumentType(templates
, noTemplates
);
1198 return (wxDocTemplate
*) NULL
;
1200 char *pathTmp
= wxFileSelector(_("Select a file"), "", "",
1201 temp
->GetDefaultExtension(),
1202 temp
->GetFileFilter(),
1203 0, wxTheApp
->GetTopWindow());
1211 return (wxDocTemplate
*) NULL
;
1215 wxDocTemplate
*wxDocManager::SelectDocumentType(wxDocTemplate
**templates
,
1218 char **strings
= new char *[noTemplates
];
1219 char **data
= new char *[noTemplates
];
1222 for (i
= 0; i
< noTemplates
; i
++)
1224 if (templates
[i
]->IsVisible())
1226 strings
[n
] = WXSTRINGCAST templates
[i
]->m_description
;
1227 data
[n
] = (char *)templates
[i
];
1235 return (wxDocTemplate
*) NULL
;
1239 wxDocTemplate
*temp
= (wxDocTemplate
*)data
[0];
1245 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData(_("Select a document template"), _("Templates"), n
,
1252 wxDocTemplate
*wxDocManager::SelectViewType(wxDocTemplate
**templates
,
1255 char **strings
= new char *[noTemplates
];
1256 char **data
= new char *[noTemplates
];
1259 for (i
= 0; i
< noTemplates
; i
++)
1261 if (templates
[i
]->IsVisible() && (templates
[i
]->GetViewName() != ""))
1263 strings
[n
] = WXSTRINGCAST templates
[i
]->m_viewTypeName
;
1264 data
[n
] = (char *)templates
[i
];
1268 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData(_("Select a document view"), _("Views"), n
,
1275 void wxDocManager::AssociateTemplate(wxDocTemplate
*temp
)
1277 if (!m_templates
.Member(temp
))
1278 m_templates
.Append(temp
);
1281 void wxDocManager::DisassociateTemplate(wxDocTemplate
*temp
)
1283 m_templates
.DeleteObject(temp
);
1286 // Add and remove a document from the manager's list
1287 void wxDocManager::AddDocument(wxDocument
*doc
)
1289 if (!m_docs
.Member(doc
))
1293 void wxDocManager::RemoveDocument(wxDocument
*doc
)
1295 m_docs
.DeleteObject(doc
);
1298 // Views or windows should inform the document manager
1299 // when a view is going in or out of focus
1300 void wxDocManager::ActivateView(wxView
*view
, bool activate
, bool WXUNUSED(deleting
))
1302 // If we're deactiving, and if we're not actually deleting the view, then
1303 // don't reset the current view because we may be going to
1304 // a window without a view.
1305 // WHAT DID I MEAN BY THAT EXACTLY?
1309 if (m_currentView == view)
1310 m_currentView = NULL;
1316 m_currentView
= view
;
1318 m_currentView
= (wxView
*) NULL
;
1323 * Default document child frame
1326 BEGIN_EVENT_TABLE(wxDocChildFrame
, wxFrame
)
1327 EVT_ACTIVATE(wxDocChildFrame::OnActivate
)
1328 EVT_CLOSE(wxDocChildFrame::OnCloseWindow
)
1331 wxDocChildFrame::wxDocChildFrame(wxDocument
*doc
, wxView
*view
, wxFrame
*frame
, wxWindowID id
, const wxString
& title
,
1332 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
1333 wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1335 m_childDocument
= doc
;
1338 view
->SetFrame(this);
1341 wxDocChildFrame::~wxDocChildFrame(void)
1345 // Extend event processing to search the view's event table
1346 bool wxDocChildFrame::ProcessEvent(wxEvent
& event
)
1349 m_childView
->Activate(TRUE
);
1351 if ( !m_childView
|| ! m_childView
->ProcessEvent(event
) )
1353 // Only hand up to the parent if it's a menu command
1354 if (!event
.IsKindOf(CLASSINFO(wxCommandEvent
)) || !GetParent() || !GetParent()->ProcessEvent(event
))
1355 return wxEvtHandler::ProcessEvent(event
);
1363 void wxDocChildFrame::OnActivate(wxActivateEvent
& event
)
1365 wxFrame::OnActivate(event
);
1368 m_childView
->Activate(event
.GetActive());
1371 void wxDocChildFrame::OnCloseWindow(wxCloseEvent
& event
)
1376 if (!event
.CanVeto())
1377 ans
= TRUE
; // Must delete.
1379 ans
= m_childView
->Close(FALSE
); // FALSE means don't delete associated window
1383 m_childView
->Activate(FALSE
);
1385 m_childView
= (wxView
*) NULL
;
1386 m_childDocument
= (wxDocument
*) NULL
;
1398 * Default parent frame
1401 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1402 EVT_MENU(wxID_EXIT
, wxDocParentFrame::OnExit
)
1403 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE9
, wxDocParentFrame::OnMRUFile
)
1404 EVT_CLOSE(wxDocParentFrame::OnCloseWindow
)
1407 wxDocParentFrame::wxDocParentFrame(wxDocManager
*manager
, wxFrame
*frame
, wxWindowID id
, const wxString
& title
,
1408 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
1409 wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1411 m_docManager
= manager
;
1414 void wxDocParentFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
1419 void wxDocParentFrame::OnMRUFile(wxCommandEvent
& event
)
1421 wxString
f(m_docManager
->GetHistoryFile(event
.GetSelection() - wxID_FILE1
));
1423 (void)m_docManager
->CreateDocument(f
, wxDOC_SILENT
);
1426 // Extend event processing to search the view's event table
1427 bool wxDocParentFrame::ProcessEvent(wxEvent
& event
)
1429 // Try the document manager, then do default processing
1430 if (!m_docManager
|| !m_docManager
->ProcessEvent(event
))
1431 return wxEvtHandler::ProcessEvent(event
);
1436 // Define the behaviour for the frame closing
1437 // - must delete all frames except for the main one.
1438 void wxDocParentFrame::OnCloseWindow(wxCloseEvent
& event
)
1440 if (m_docManager
->Clear(!event
.CanVeto()))
1448 #if wxUSE_PRINTING_ARCHITECTURE
1450 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
):
1451 wxPrintout(WXSTRINGCAST title
)
1453 m_printoutView
= view
;
1456 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
1460 // Get the logical pixels per inch of screen and printer
1461 int ppiScreenX
, ppiScreenY
;
1462 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
1463 int ppiPrinterX
, ppiPrinterY
;
1464 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
1466 // This scales the DC so that the printout roughly represents the
1467 // the screen scaling. The text point size _should_ be the right size
1468 // but in fact is too small for some reason. This is a detail that will
1469 // need to be addressed at some point but can be fudged for the
1471 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
1473 // Now we have to check in case our real page size is reduced
1474 // (e.g. because we're drawing to a print preview memory DC)
1475 int pageWidth
, pageHeight
;
1477 dc
->GetSize(&w
, &h
);
1478 GetPageSizePixels(&pageWidth
, &pageHeight
);
1480 // If printer pageWidth == current DC width, then this doesn't
1481 // change. But w might be the preview bitmap width, so scale down.
1482 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
1483 dc
->SetUserScale(overallScale
, overallScale
);
1487 m_printoutView
->OnDraw(dc
);
1492 bool wxDocPrintout::HasPage(int pageNum
)
1494 return (pageNum
== 1);
1497 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
1499 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
1505 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
1516 * Command processing framework
1519 wxCommand::wxCommand(bool canUndoIt
, const wxString
& name
)
1521 m_canUndo
= canUndoIt
;
1522 m_commandName
= name
;
1525 wxCommand::~wxCommand(void)
1529 // Command processor
1530 wxCommandProcessor::wxCommandProcessor(int maxCommands
)
1532 m_maxNoCommands
= maxCommands
;
1533 m_currentCommand
= (wxNode
*) NULL
;
1534 m_commandEditMenu
= (wxMenu
*) NULL
;
1537 wxCommandProcessor::~wxCommandProcessor(void)
1542 // Pass a command to the processor. The processor calls Do();
1543 // if successful, is appended to the command history unless
1544 // storeIt is FALSE.
1545 bool wxCommandProcessor::Submit(wxCommand
*command
, bool storeIt
)
1547 bool success
= command
->Do();
1548 if (success
&& storeIt
)
1550 if (m_commands
.Number() == m_maxNoCommands
)
1552 wxNode
*firstNode
= m_commands
.First();
1553 wxCommand
*firstCommand
= (wxCommand
*)firstNode
->Data();
1554 delete firstCommand
;
1558 // Correct a bug: we must chop off the current 'branch'
1559 // so that we're at the end of the command list.
1560 if (!m_currentCommand
)
1564 wxNode
*node
= m_currentCommand
->Next();
1567 wxNode
*next
= node
->Next();
1568 delete (wxCommand
*)node
->Data();
1574 m_commands
.Append(command
);
1575 m_currentCommand
= m_commands
.Last();
1581 bool wxCommandProcessor::Undo(void)
1583 if (m_currentCommand
)
1585 wxCommand
*command
= (wxCommand
*)m_currentCommand
->Data();
1586 if (command
->CanUndo())
1588 bool success
= command
->Undo();
1591 m_currentCommand
= m_currentCommand
->Previous();
1600 bool wxCommandProcessor::Redo(void)
1602 wxCommand
*redoCommand
= (wxCommand
*) NULL
;
1603 wxNode
*redoNode
= (wxNode
*) NULL
;
1604 if (m_currentCommand
&& m_currentCommand
->Next())
1606 redoCommand
= (wxCommand
*)m_currentCommand
->Next()->Data();
1607 redoNode
= m_currentCommand
->Next();
1611 if (m_commands
.Number() > 0)
1613 redoCommand
= (wxCommand
*)m_commands
.First()->Data();
1614 redoNode
= m_commands
.First();
1620 bool success
= redoCommand
->Do();
1623 m_currentCommand
= redoNode
;
1631 bool wxCommandProcessor::CanUndo(void) const
1633 if (m_currentCommand
)
1634 return ((wxCommand
*)m_currentCommand
->Data())->CanUndo();
1638 bool wxCommandProcessor::CanRedo(void) const
1640 if ((m_currentCommand
!= (wxNode
*) NULL
) && (m_currentCommand
->Next() == (wxNode
*) NULL
))
1643 if ((m_currentCommand
!= (wxNode
*) NULL
) && (m_currentCommand
->Next() != (wxNode
*) NULL
))
1646 if ((m_currentCommand
== (wxNode
*) NULL
) && (m_commands
.Number() > 0))
1652 void wxCommandProcessor::Initialize(void)
1654 m_currentCommand
= m_commands
.Last();
1658 void wxCommandProcessor::SetMenuStrings(void)
1660 if (m_commandEditMenu
)
1663 if (m_currentCommand
)
1665 wxCommand
*command
= (wxCommand
*)m_currentCommand
->Data();
1666 wxString
commandName(command
->GetName());
1667 if (commandName
== "") commandName
= _("Unnamed command");
1668 bool canUndo
= command
->CanUndo();
1670 buf
= wxString(_("&Undo ")) + commandName
;
1672 buf
= wxString(_("Can't &Undo ")) + commandName
;
1674 m_commandEditMenu
->SetLabel(wxID_UNDO
, buf
);
1675 m_commandEditMenu
->Enable(wxID_UNDO
, canUndo
);
1677 // We can redo, if we're not at the end of the history.
1678 if (m_currentCommand
->Next())
1680 wxCommand
*redoCommand
= (wxCommand
*)m_currentCommand
->Next()->Data();
1681 wxString
redoCommandName(redoCommand
->GetName());
1682 if (redoCommandName
== "") redoCommandName
= _("Unnamed command");
1683 buf
= wxString(_("&Redo ")) + redoCommandName
;
1684 m_commandEditMenu
->SetLabel(wxID_REDO
, buf
);
1685 m_commandEditMenu
->Enable(wxID_REDO
, TRUE
);
1689 m_commandEditMenu
->SetLabel(wxID_REDO
, _("&Redo"));
1690 m_commandEditMenu
->Enable(wxID_REDO
, FALSE
);
1695 m_commandEditMenu
->SetLabel(wxID_UNDO
, _("&Undo"));
1696 m_commandEditMenu
->Enable(wxID_UNDO
, FALSE
);
1698 if (m_commands
.Number() == 0)
1700 m_commandEditMenu
->SetLabel(wxID_REDO
, _("&Redo"));
1701 m_commandEditMenu
->Enable(wxID_REDO
, FALSE
);
1705 // currentCommand is NULL but there are commands: this means that
1706 // we've undone to the start of the list, but can redo the first.
1707 wxCommand
*redoCommand
= (wxCommand
*)m_commands
.First()->Data();
1708 wxString
redoCommandName(redoCommand
->GetName());
1709 if (redoCommandName
== "") redoCommandName
= _("Unnamed command");
1710 buf
= wxString(_("&Redo ")) + redoCommandName
;
1711 m_commandEditMenu
->SetLabel(wxID_REDO
, buf
);
1712 m_commandEditMenu
->Enable(wxID_REDO
, TRUE
);
1718 void wxCommandProcessor::ClearCommands(void)
1720 wxNode
*node
= m_commands
.First();
1723 wxCommand
*command
= (wxCommand
*)node
->Data();
1726 node
= m_commands
.First();
1728 m_currentCommand
= (wxNode
*) NULL
;
1733 * File history processor
1736 wxFileHistory::wxFileHistory(int maxFiles
)
1738 m_fileMaxFiles
= maxFiles
;
1740 m_fileHistory
= new char *[m_fileMaxFiles
];
1743 wxFileHistory::~wxFileHistory(void)
1746 for (i
= 0; i
< m_fileHistoryN
; i
++)
1747 delete[] m_fileHistory
[i
];
1748 delete[] m_fileHistory
;
1751 // File history management
1752 void wxFileHistory::AddFileToHistory(const wxString
& file
)
1755 // Check we don't already have this file
1756 for (i
= 0; i
< m_fileHistoryN
; i
++)
1758 if (m_fileHistory
[i
] && wxString(m_fileHistory
[i
]) == file
)
1762 // Add to the project file history:
1763 // Move existing files (if any) down so we can insert file at beginning.
1765 // First delete filename that has popped off the end of the array (if any)
1766 if (m_fileHistoryN
== m_fileMaxFiles
)
1768 delete[] m_fileHistory
[m_fileMaxFiles
-1];
1769 m_fileHistory
[m_fileMaxFiles
-1] = (char *) NULL
;
1771 if (m_fileHistoryN
< m_fileMaxFiles
)
1773 wxNode
* node
= m_fileMenus
.First();
1776 wxMenu
* menu
= (wxMenu
*) node
->Data();
1777 if (m_fileHistoryN
== 0)
1778 menu
->AppendSeparator();
1779 menu
->Append(wxID_FILE1
+m_fileHistoryN
, _("[EMPTY]"));
1780 node
= node
->Next();
1784 // Shuffle filenames down
1785 for (i
= (m_fileHistoryN
-1); i
> 0; i
--)
1787 m_fileHistory
[i
] = m_fileHistory
[i
-1];
1789 m_fileHistory
[0] = copystring(file
);
1791 for (i
= 0; i
< m_fileHistoryN
; i
++)
1792 if (m_fileHistory
[i
])
1795 buf
.Printf("&%d %s", i
+1, m_fileHistory
[i
]);
1796 wxNode
* node
= m_fileMenus
.First();
1799 wxMenu
* menu
= (wxMenu
*) node
->Data();
1800 menu
->SetLabel(wxID_FILE1
+i
, buf
);
1801 node
= node
->Next();
1806 wxString
wxFileHistory::GetHistoryFile(int i
) const
1808 if (i
< m_fileHistoryN
)
1809 return wxString(m_fileHistory
[i
]);
1811 return wxString("");
1814 void wxFileHistory::UseMenu(wxMenu
*menu
)
1816 if (!m_fileMenus
.Member(menu
))
1817 m_fileMenus
.Append(menu
);
1820 void wxFileHistory::RemoveMenu(wxMenu
*menu
)
1822 m_fileMenus
.DeleteObject(menu
);
1826 void wxFileHistory::Load(wxConfigBase
& config
)
1830 buf
.Printf("file%d", m_fileHistoryN
+1);
1831 wxString historyFile
;
1832 while ((m_fileHistoryN
<= m_fileMaxFiles
) && config
.Read(buf
, &historyFile
) && (historyFile
!= ""))
1834 m_fileHistory
[m_fileHistoryN
] = copystring((const char*) historyFile
);
1836 buf
.Printf("file%d", m_fileHistoryN
+1);
1842 void wxFileHistory::Save(wxConfigBase
& config
)
1845 for (i
= 0; i
< m_fileHistoryN
; i
++)
1848 buf
.Printf("file%d", i
+1);
1849 config
.Write(buf
, wxString(m_fileHistory
[i
]));
1854 void wxFileHistory::AddFilesToMenu()
1856 if (m_fileHistoryN
> 0)
1858 wxNode
* node
= m_fileMenus
.First();
1861 wxMenu
* menu
= (wxMenu
*) node
->Data();
1862 menu
->AppendSeparator();
1864 for (i
= 0; i
< m_fileHistoryN
; i
++)
1866 if (m_fileHistory
[i
])
1869 buf
.Printf("&%d %s", i
+1, m_fileHistory
[i
]);
1870 menu
->Append(wxID_FILE1
+i
, buf
);
1873 node
= node
->Next();
1878 void wxFileHistory::AddFilesToMenu(wxMenu
* menu
)
1880 if (m_fileHistoryN
> 0)
1882 menu
->AppendSeparator();
1884 for (i
= 0; i
< m_fileHistoryN
; i
++)
1886 if (m_fileHistory
[i
])
1889 buf
.Printf("&%d %s", i
+1, m_fileHistory
[i
]);
1890 menu
->Append(wxID_FILE1
+i
, buf
);
1901 wxPrintInfo::wxPrintInfo(void)
1906 wxPrintInfo::~wxPrintInfo(void)
1912 * Permits compatibility with existing file formats and functions
1913 * that manipulate files directly
1916 bool wxTransferFileToStream(const wxString
& filename
, ostream
& stream
)
1921 if ((fd1
= fopen (WXSTRINGCAST filename
, "rb")) == NULL
)
1924 while ((ch
= getc (fd1
)) != EOF
)
1925 stream
<< (unsigned char)ch
;
1931 bool wxTransferStreamToFile(istream
& stream
, const wxString
& filename
)
1936 if ((fd1
= fopen (WXSTRINGCAST filename
, "wb")) == NULL
)
1941 while (!stream
.eof())
1952 // End wxUSE_DOC_VIEW_ARCHITECTURE