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 char *tmp
= wxFileSelector(_("Save as"), docTemplate
->GetDirectory(), GetFilename(),
202 docTemplate
->GetDefaultExtension(), docTemplate
->GetFileFilter(),
203 wxSAVE
|wxOVERWRITE_PROMPT
, GetDocumentWindow());
209 wxString
fileName(tmp
);
213 wxSplitPath(fileName
, & path
, & name
, & ext
);
215 if (ext
.IsEmpty() || ext
== "")
218 fileName
+= docTemplate
->GetDefaultExtension();
221 SetFilename(fileName
);
222 SetTitle(wxFileNameFromPath(fileName
));
224 GetDocumentManager()->AddFileToHistory(fileName
);
226 // Notify the views that the filename has changed
227 wxNode
*node
= m_documentViews
.First();
230 wxView
*view
= (wxView
*)node
->Data();
231 view
->OnChangeFilename();
235 return OnSaveDocument(m_documentFile
);
238 bool wxDocument::OnSaveDocument(const wxString
& file
)
244 if (wxTheApp
->GetAppName() != "")
245 msgTitle
= wxTheApp
->GetAppName();
247 msgTitle
= wxString(_("File error"));
249 ofstream
store(file
);
250 if (store
.fail() || store
.bad())
252 (void)wxMessageBox(_("Sorry, could not open this file for saving."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
253 GetDocumentWindow());
257 if (SaveObject(store
)==FALSE
)
259 (void)wxMessageBox(_("Sorry, could not save this file."), msgTitle
, wxOK
| wxICON_EXCLAMATION
,
260 GetDocumentWindow());
269 bool wxDocument::OnOpenDocument(const wxString
& file
)
271 if (!OnSaveModified())
275 if (wxTheApp
->GetAppName() != "")
276 msgTitle
= wxTheApp
->GetAppName();
278 msgTitle
= wxString(_("File error"));
280 ifstream
store(file
);
281 if (store
.fail() || store
.bad())
283 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
|wxICON_EXCLAMATION
,
284 GetDocumentWindow());
287 if (LoadObject(store
)==FALSE
)
289 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle
, wxOK
|wxICON_EXCLAMATION
,
290 GetDocumentWindow());
293 SetFilename(file
, TRUE
);
302 istream
& wxDocument::LoadObject(istream
& stream
)
304 // wxObject::LoadObject(stream);
309 ostream
& wxDocument::SaveObject(ostream
& stream
)
311 // wxObject::SaveObject(stream);
316 bool wxDocument::Revert(void)
322 // Get title, or filename if no title, else unnamed
323 bool wxDocument::GetPrintableName(wxString
& buf
) const
325 if (m_documentTitle
!= "")
327 buf
= m_documentTitle
;
330 else if (m_documentFile
!= "")
332 buf
= wxFileNameFromPath(m_documentFile
);
342 wxWindow
*wxDocument::GetDocumentWindow(void) const
344 wxView
*view
= GetFirstView();
346 return view
->GetFrame();
348 return wxTheApp
->GetTopWindow();
351 wxCommandProcessor
*wxDocument::OnCreateCommandProcessor(void)
353 return new wxCommandProcessor
;
356 // TRUE if safe to close
357 bool wxDocument::OnSaveModified(void)
363 GetPrintableName(title
);
366 if (wxTheApp
->GetAppName() != "")
367 msgTitle
= wxTheApp
->GetAppName();
369 msgTitle
= wxString(_("Warning"));
371 sprintf(buf
, _("Do you want to save changes to document %s?"), (const char *)title
);
372 int res
= wxMessageBox(buf
, msgTitle
, wxYES_NO
|wxCANCEL
|wxICON_QUESTION
,
373 GetDocumentWindow());
379 else if (res
== wxYES
)
381 else if (res
== wxCANCEL
)
387 bool wxDocument::Draw(wxDC
& WXUNUSED(context
))
392 bool wxDocument::AddView(wxView
*view
)
394 if (!m_documentViews
.Member(view
))
396 m_documentViews
.Append(view
);
402 bool wxDocument::RemoveView(wxView
*view
)
404 (void)m_documentViews
.DeleteObject(view
);
409 bool wxDocument::OnCreate(const wxString
& WXUNUSED(path
), long flags
)
411 if (GetDocumentTemplate()->CreateView(this, flags
))
417 // Called after a view is added or removed.
418 // The default implementation deletes the document if
419 // there are no more views.
420 void wxDocument::OnChangedViewList(void)
422 if (m_documentViews
.Number() == 0)
424 if (OnSaveModified())
431 void wxDocument::UpdateAllViews(wxView
*sender
, wxObject
*hint
)
433 wxNode
*node
= m_documentViews
.First();
436 wxView
*view
= (wxView
*)node
->Data();
437 view
->OnUpdate(sender
, hint
);
442 void wxDocument::SetFilename(const wxString
& filename
, bool notifyViews
)
444 m_documentFile
= filename
;
447 // Notify the views that the filename has changed
448 wxNode
*node
= m_documentViews
.First();
451 wxView
*view
= (wxView
*)node
->Data();
452 view
->OnChangeFilename();
463 wxView::wxView(wxDocument
*doc
)
468 m_viewFrame
= (wxFrame
*) NULL
;
471 wxView::~wxView(void)
473 GetDocumentManager()->ActivateView(this, FALSE
, TRUE
);
474 m_viewDocument
->RemoveView(this);
477 // Extend event processing to search the document's event table
478 bool wxView::ProcessEvent(wxEvent
& event
)
480 if ( !GetDocument() || !GetDocument()->ProcessEvent(event
) )
481 return wxEvtHandler::ProcessEvent(event
);
486 void wxView::OnActivateView(bool WXUNUSED(activate
), wxView
*WXUNUSED(activeView
), wxView
*WXUNUSED(deactiveView
))
490 void wxView::OnPrint(wxDC
*dc
, wxObject
*WXUNUSED(info
))
495 void wxView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
))
499 void wxView::OnChangeFilename(void)
501 if (GetFrame() && GetDocument())
504 GetDocument()->GetPrintableName(name
);
506 GetFrame()->SetTitle(name
);
510 void wxView::SetDocument(wxDocument
*doc
)
512 m_viewDocument
= doc
;
517 bool wxView::Close(bool deleteWindow
)
519 if (OnClose(deleteWindow
))
525 void wxView::Activate(bool activate
)
527 if (GetDocumentManager())
529 OnActivateView(activate
, this, GetDocumentManager()->GetCurrentView());
530 GetDocumentManager()->ActivateView(this, activate
);
534 bool wxView::OnClose(bool WXUNUSED(deleteWindow
))
536 return GetDocument() ? GetDocument()->Close() : TRUE
;
539 #if wxUSE_PRINTING_ARCHITECTURE
540 wxPrintout
*wxView::OnCreatePrintout(void)
542 return new wxDocPrintout(this);
551 wxDocTemplate::wxDocTemplate(wxDocManager
*manager
, const wxString
& descr
,
552 const wxString
& filter
, const wxString
& dir
, const wxString
& ext
,
553 const wxString
& docTypeName
, const wxString
& viewTypeName
,
554 wxClassInfo
*docClassInfo
, wxClassInfo
*viewClassInfo
, long flags
)
556 m_documentManager
= manager
;
558 m_description
= descr
;
561 m_fileFilter
= filter
;
563 m_docTypeName
= docTypeName
;
564 m_viewTypeName
= viewTypeName
;
565 m_documentManager
->AssociateTemplate(this);
567 m_docClassInfo
= docClassInfo
;
568 m_viewClassInfo
= viewClassInfo
;
571 wxDocTemplate::~wxDocTemplate(void)
573 m_documentManager
->DisassociateTemplate(this);
576 // Tries to dynamically construct an object of the right
578 wxDocument
*wxDocTemplate::CreateDocument(const wxString
& path
, long flags
)
581 return (wxDocument
*) NULL
;
582 wxDocument
*doc
= (wxDocument
*)m_docClassInfo
->CreateObject();
583 doc
->SetFilename(path
);
584 doc
->SetDocumentTemplate(this);
585 GetDocumentManager()->AddDocument(doc
);
586 doc
->SetCommandProcessor(doc
->OnCreateCommandProcessor());
588 if (doc
->OnCreate(path
, flags
))
593 return (wxDocument
*) NULL
;
597 wxView
*wxDocTemplate::CreateView(wxDocument
*doc
, long flags
)
599 if (!m_viewClassInfo
)
600 return (wxView
*) NULL
;
601 wxView
*view
= (wxView
*)m_viewClassInfo
->CreateObject();
602 view
->SetDocument(doc
);
603 if (view
->OnCreate(doc
, flags
))
610 return (wxView
*) NULL
;
614 BEGIN_EVENT_TABLE(wxDocManager
, wxEvtHandler
)
615 EVT_MENU(wxID_OPEN
, wxDocManager::OnFileOpen
)
616 EVT_MENU(wxID_CLOSE
, wxDocManager::OnFileClose
)
617 EVT_MENU(wxID_REVERT
, wxDocManager::OnFileRevert
)
618 EVT_MENU(wxID_NEW
, wxDocManager::OnFileNew
)
619 EVT_MENU(wxID_SAVE
, wxDocManager::OnFileSave
)
620 EVT_MENU(wxID_SAVEAS
, wxDocManager::OnFileSaveAs
)
621 EVT_MENU(wxID_UNDO
, wxDocManager::OnUndo
)
622 EVT_MENU(wxID_REDO
, wxDocManager::OnRedo
)
623 EVT_MENU(wxID_PRINT
, wxDocManager::OnPrint
)
624 EVT_MENU(wxID_PRINT_SETUP
, wxDocManager::OnPrintSetup
)
625 EVT_MENU(wxID_PREVIEW
, wxDocManager::OnPreview
)
628 wxDocManager::wxDocManager(long flags
, bool initialize
)
630 m_defaultDocumentNameCounter
= 1;
632 m_currentView
= (wxView
*) NULL
;
633 m_maxDocsOpen
= 10000;
634 m_fileHistory
= (wxFileHistory
*) NULL
;
639 wxDocManager::~wxDocManager(void)
643 delete m_fileHistory
;
646 bool wxDocManager::Clear(bool force
)
648 wxNode
*node
= m_docs
.First();
651 wxDocument
*doc
= (wxDocument
*)node
->Data();
652 wxNode
*next
= node
->Next();
654 if (!doc
->Close() && !force
)
657 // Implicitly deletes the document when the last
658 // view is removed (deleted)
659 doc
->DeleteAllViews();
661 // Check document is deleted
662 if (m_docs
.Member(doc
))
665 // This assumes that documents are not connected in
666 // any way, i.e. deleting one document does NOT
670 node
= m_templates
.First();
673 wxDocTemplate
*templ
= (wxDocTemplate
*) node
->Data();
674 wxNode
* next
= node
->Next();
681 bool wxDocManager::Initialize(void)
683 m_fileHistory
= OnCreateFileHistory();
687 wxFileHistory
*wxDocManager::OnCreateFileHistory(void)
689 return new wxFileHistory
;
692 void wxDocManager::OnFileClose(wxCommandEvent
& WXUNUSED(event
))
694 wxDocument
*doc
= GetCurrentDocument();
699 doc
->DeleteAllViews();
700 if (m_docs
.Member(doc
))
705 void wxDocManager::OnFileNew(wxCommandEvent
& WXUNUSED(event
))
707 CreateDocument(wxString(""), wxDOC_NEW
);
710 void wxDocManager::OnFileOpen(wxCommandEvent
& WXUNUSED(event
))
712 CreateDocument(wxString(""), 0);
715 void wxDocManager::OnFileRevert(wxCommandEvent
& WXUNUSED(event
))
717 wxDocument
*doc
= GetCurrentDocument();
723 void wxDocManager::OnFileSave(wxCommandEvent
& WXUNUSED(event
))
725 wxDocument
*doc
= GetCurrentDocument();
731 void wxDocManager::OnFileSaveAs(wxCommandEvent
& WXUNUSED(event
))
733 wxDocument
*doc
= GetCurrentDocument();
739 void wxDocManager::OnPrint(wxCommandEvent
& WXUNUSED(event
))
741 wxView
*view
= GetCurrentView();
745 wxPrintout
*printout
= view
->OnCreatePrintout();
749 printer
.Print(view
->GetFrame(), printout
, TRUE
);
755 void wxDocManager::OnPrintSetup(wxCommandEvent
& WXUNUSED(event
))
757 wxWindow
*parentWin
= wxTheApp
->GetTopWindow();
758 wxView
*view
= GetCurrentView();
760 parentWin
= view
->GetFrame();
764 wxPrintDialog
printerDialog(parentWin
, & data
);
765 printerDialog
.GetPrintData().SetSetupDialog(TRUE
);
766 printerDialog
.ShowModal();
769 void wxDocManager::OnPreview(wxCommandEvent
& WXUNUSED(event
))
771 wxView
*view
= GetCurrentView();
775 wxPrintout
*printout
= view
->OnCreatePrintout();
778 // Pass two printout objects: for preview, and possible printing.
779 wxPrintPreviewBase
*preview
= (wxPrintPreviewBase
*) NULL
;
780 preview
= new wxPrintPreview(printout
, view
->OnCreatePrintout());
782 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, (wxFrame
*)wxTheApp
->GetTopWindow(), _("Print Preview"),
783 wxPoint(100, 100), wxSize(600, 650));
784 frame
->Centre(wxBOTH
);
790 void wxDocManager::OnUndo(wxCommandEvent
& WXUNUSED(event
))
792 wxDocument
*doc
= GetCurrentDocument();
795 if (doc
->GetCommandProcessor())
796 doc
->GetCommandProcessor()->Undo();
799 void wxDocManager::OnRedo(wxCommandEvent
& WXUNUSED(event
))
801 wxDocument
*doc
= GetCurrentDocument();
804 if (doc
->GetCommandProcessor())
805 doc
->GetCommandProcessor()->Redo();
808 wxView
*wxDocManager::GetCurrentView(void) const
811 return m_currentView
;
812 if (m_docs
.Number() == 1)
814 wxDocument
* doc
= (wxDocument
*) m_docs
.First()->Data();
815 return doc
->GetFirstView();
817 return (wxView
*) NULL
;
820 // Extend event processing to search the view's event table
821 bool wxDocManager::ProcessEvent(wxEvent
& event
)
823 wxView
* view
= GetCurrentView();
826 if (view
->ProcessEvent(event
))
829 return wxEvtHandler::ProcessEvent(event
);
832 wxDocument
*wxDocManager::CreateDocument(const wxString
& path
, long flags
)
834 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.Number()];
837 for (i
= 0; i
< m_templates
.Number(); i
++)
839 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Nth(i
)->Data());
840 if (temp
->IsVisible())
849 return (wxDocument
*) NULL
;
852 // If we've reached the max number of docs, close the
854 if (GetDocuments().Number() >= m_maxDocsOpen
)
856 wxDocument
*doc
= (wxDocument
*)GetDocuments().First()->Data();
859 // Implicitly deletes the document when
860 // the last view is deleted
861 doc
->DeleteAllViews();
863 // Check we're really deleted
864 if (m_docs
.Member(doc
))
868 return (wxDocument
*) NULL
;
871 // New document: user chooses a template, unless there's only one.
872 if (flags
& wxDOC_NEW
)
876 wxDocTemplate
*temp
= templates
[0];
878 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
881 newDoc
->SetDocumentName(temp
->GetDocumentName());
882 newDoc
->SetDocumentTemplate(temp
);
883 newDoc
->OnNewDocument();
888 wxDocTemplate
*temp
= SelectDocumentType(templates
, n
);
892 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
895 newDoc
->SetDocumentName(temp
->GetDocumentName());
896 newDoc
->SetDocumentTemplate(temp
);
897 newDoc
->OnNewDocument();
902 return (wxDocument
*) NULL
;
906 wxDocTemplate
*temp
= (wxDocTemplate
*) NULL
;
912 if (flags
& wxDOC_SILENT
)
913 temp
= FindTemplateForPath(path2
);
915 temp
= SelectDocumentPath(templates
, n
, path2
, flags
);
921 wxDocument
*newDoc
= temp
->CreateDocument(path2
, flags
);
924 newDoc
->SetDocumentName(temp
->GetDocumentName());
925 newDoc
->SetDocumentTemplate(temp
);
926 if (!newDoc
->OnOpenDocument(path2
))
929 return (wxDocument
*) NULL
;
931 AddFileToHistory(path2
);
936 return (wxDocument
*) NULL
;
939 wxView
*wxDocManager::CreateView(wxDocument
*doc
, long flags
)
941 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.Number()];
944 for (i
= 0; i
< m_templates
.Number(); i
++)
946 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Nth(i
)->Data());
947 if (temp
->IsVisible())
949 if (temp
->GetDocumentName() == doc
->GetDocumentName())
959 return (wxView
*) NULL
;
963 wxDocTemplate
*temp
= templates
[0];
965 wxView
*view
= temp
->CreateView(doc
, flags
);
967 view
->SetViewName(temp
->GetViewName());
971 wxDocTemplate
*temp
= SelectViewType(templates
, n
);
975 wxView
*view
= temp
->CreateView(doc
, flags
);
977 view
->SetViewName(temp
->GetViewName());
981 return (wxView
*) NULL
;
984 // Not yet implemented
985 void wxDocManager::DeleteTemplate(wxDocTemplate
*WXUNUSED(temp
), long WXUNUSED(flags
))
989 // Not yet implemented
990 bool wxDocManager::FlushDoc(wxDocument
*WXUNUSED(doc
))
995 wxDocument
*wxDocManager::GetCurrentDocument(void) const
998 return m_currentView
->GetDocument();
1000 return (wxDocument
*) NULL
;
1003 // Make a default document name
1004 bool wxDocManager::MakeDefaultName(wxString
& name
)
1007 sprintf(buf
, _("unnamed%d"), m_defaultDocumentNameCounter
);
1008 m_defaultDocumentNameCounter
++;
1013 // Not yet implemented
1014 wxDocTemplate
*wxDocManager::MatchTemplate(const wxString
& WXUNUSED(path
))
1016 return (wxDocTemplate
*) NULL
;
1019 // File history management
1020 void wxDocManager::AddFileToHistory(const wxString
& file
)
1023 m_fileHistory
->AddFileToHistory(file
);
1026 wxString
wxDocManager::GetHistoryFile(int i
) const
1029 return wxString(m_fileHistory
->GetHistoryFile(i
));
1031 return wxString("");
1034 void wxDocManager::FileHistoryUseMenu(wxMenu
*menu
)
1037 m_fileHistory
->UseMenu(menu
);
1040 void wxDocManager::FileHistoryRemoveMenu(wxMenu
*menu
)
1043 m_fileHistory
->RemoveMenu(menu
);
1046 void wxDocManager::FileHistoryLoad(wxConfigBase
& config
)
1049 m_fileHistory
->Load(config
);
1052 void wxDocManager::FileHistorySave(wxConfigBase
& config
)
1055 m_fileHistory
->Save(config
);
1058 void wxDocManager::FileHistoryAddFilesToMenu(wxMenu
* menu
)
1061 m_fileHistory
->AddFilesToMenu(menu
);
1064 void wxDocManager::FileHistoryAddFilesToMenu()
1067 m_fileHistory
->AddFilesToMenu();
1070 int wxDocManager::GetNoHistoryFiles(void) const
1073 return m_fileHistory
->GetNoHistoryFiles();
1078 static char *FindExtension(char *path
)
1080 static char ext
[10];
1081 int len
= strlen(path
);
1085 for (i
= (len
-1); i
> 0; i
--)
1091 for (j
= i
+1; j
< len
; j
++)
1092 ext
[(int)(j
-(i
+1))] = (char)wxToLower(path
[j
]); // NOTE Should not use tolower under UNIX
1097 return (char *) NULL
;
1099 else return (char *) NULL
;
1103 // Given a path, try to find a matching template. Won't
1104 // always work, of course.
1105 wxDocTemplate
*wxDocManager::FindTemplateForPath(const wxString
& path
)
1107 char *theExt
= FindExtension((char *)(const char *)path
);
1109 return (wxDocTemplate
*) NULL
;
1110 wxDocTemplate
*theTemplate
= (wxDocTemplate
*) NULL
;
1112 if (m_templates
.Number() == 1)
1113 return (wxDocTemplate
*)m_templates
.First()->Data();
1115 // Find the template which this extension corresponds to
1117 for (i
= 0; i
< m_templates
.Number(); i
++)
1119 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Nth(i
)->Data();
1120 if (strcmp(temp
->GetDefaultExtension(), theExt
) == 0)
1129 // Prompts user to open a file, using file specs in templates.
1130 // How to implement in wxWindows? Must extend the file selector
1131 // dialog or implement own; OR match the extension to the
1132 // template extension.
1133 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**templates
,
1134 int noTemplates
, wxString
& path
, long WXUNUSED(flags
), bool WXUNUSED(save
))
1136 // We can only have multiple filters in Windows
1138 char *descrBuf
= new char[1000];
1141 for (i
= 0; i
< noTemplates
; i
++)
1143 if (templates
[i
]->IsVisible())
1145 strcat(descrBuf
, templates
[i
]->GetDescription());
1146 strcat(descrBuf
, " (");
1147 strcat(descrBuf
, templates
[i
]->GetFileFilter());
1148 strcat(descrBuf
, ") ");
1149 strcat(descrBuf
, "|");
1150 strcat(descrBuf
, templates
[i
]->GetFileFilter());
1151 strcat(descrBuf
, "|");
1154 int len
= strlen(descrBuf
);
1157 descrBuf
[len
-1] = 0;
1159 char *pathTmp
= wxFileSelector(_("Select a file"), "", "", "", descrBuf
, 0, wxTheApp
->GetTopWindow());
1164 char *theExt
= FindExtension((char *)(const char *)path
);
1166 return (wxDocTemplate
*) NULL
;
1168 // This is dodgy in that we're selecting the template on the
1169 // basis of the file extension, which may not be a standard
1170 // one. We really want to know exactly which template was
1171 // chosen by using a more advanced file selector.
1172 wxDocTemplate
*theTemplate
= FindTemplateForPath(path
);
1178 return (wxDocTemplate
*) NULL
;
1181 // In all other windowing systems, until we have more advanced
1182 // file selectors, we must select the document type (template) first, and
1183 // _then_ pop up the file selector.
1184 wxDocTemplate
*temp
= SelectDocumentType(templates
, noTemplates
);
1186 return (wxDocTemplate
*) NULL
;
1188 char *pathTmp
= wxFileSelector(_("Select a file"), "", "",
1189 temp
->GetDefaultExtension(),
1190 temp
->GetFileFilter(),
1191 0, wxTheApp
->GetTopWindow());
1199 return (wxDocTemplate
*) NULL
;
1203 wxDocTemplate
*wxDocManager::SelectDocumentType(wxDocTemplate
**templates
,
1206 char **strings
= new char *[noTemplates
];
1207 char **data
= new char *[noTemplates
];
1210 for (i
= 0; i
< noTemplates
; i
++)
1212 if (templates
[i
]->IsVisible())
1214 strings
[n
] = WXSTRINGCAST templates
[i
]->m_description
;
1215 data
[n
] = (char *)templates
[i
];
1223 return (wxDocTemplate
*) NULL
;
1227 wxDocTemplate
*temp
= (wxDocTemplate
*)data
[0];
1233 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData(_("Select a document template"), _("Templates"), n
,
1240 wxDocTemplate
*wxDocManager::SelectViewType(wxDocTemplate
**templates
,
1243 char **strings
= new char *[noTemplates
];
1244 char **data
= new char *[noTemplates
];
1247 for (i
= 0; i
< noTemplates
; i
++)
1249 if (templates
[i
]->IsVisible() && templates
[i
]->GetViewName())
1251 strings
[n
] = WXSTRINGCAST templates
[i
]->m_viewTypeName
;
1252 data
[n
] = (char *)templates
[i
];
1256 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData(_("Select a document view"), _("Views"), n
,
1263 void wxDocManager::AssociateTemplate(wxDocTemplate
*temp
)
1265 if (!m_templates
.Member(temp
))
1266 m_templates
.Append(temp
);
1269 void wxDocManager::DisassociateTemplate(wxDocTemplate
*temp
)
1271 m_templates
.DeleteObject(temp
);
1274 // Add and remove a document from the manager's list
1275 void wxDocManager::AddDocument(wxDocument
*doc
)
1277 if (!m_docs
.Member(doc
))
1281 void wxDocManager::RemoveDocument(wxDocument
*doc
)
1283 m_docs
.DeleteObject(doc
);
1286 // Views or windows should inform the document manager
1287 // when a view is going in or out of focus
1288 void wxDocManager::ActivateView(wxView
*view
, bool activate
, bool WXUNUSED(deleting
))
1290 // If we're deactiving, and if we're not actually deleting the view, then
1291 // don't reset the current view because we may be going to
1292 // a window without a view.
1293 // WHAT DID I MEAN BY THAT EXACTLY?
1297 if (m_currentView == view)
1298 m_currentView = NULL;
1304 m_currentView
= view
;
1306 m_currentView
= (wxView
*) NULL
;
1311 * Default document child frame
1314 BEGIN_EVENT_TABLE(wxDocChildFrame
, wxFrame
)
1315 EVT_ACTIVATE(wxDocChildFrame::OnActivate
)
1316 EVT_CLOSE(wxDocChildFrame::OnCloseWindow
)
1319 wxDocChildFrame::wxDocChildFrame(wxDocument
*doc
, wxView
*view
, wxFrame
*frame
, wxWindowID id
, const wxString
& title
,
1320 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
1321 wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1323 m_childDocument
= doc
;
1326 view
->SetFrame(this);
1329 wxDocChildFrame::~wxDocChildFrame(void)
1333 // Extend event processing to search the view's event table
1334 bool wxDocChildFrame::ProcessEvent(wxEvent
& event
)
1337 m_childView
->Activate(TRUE
);
1339 if ( !m_childView
|| ! m_childView
->ProcessEvent(event
) )
1341 // Only hand up to the parent if it's a menu command
1342 if (!event
.IsKindOf(CLASSINFO(wxCommandEvent
)) || !GetParent() || !GetParent()->ProcessEvent(event
))
1343 return wxEvtHandler::ProcessEvent(event
);
1351 void wxDocChildFrame::OnActivate(wxActivateEvent
& event
)
1353 wxFrame::OnActivate(event
);
1356 m_childView
->Activate(event
.GetActive());
1359 void wxDocChildFrame::OnCloseWindow(wxCloseEvent
& event
)
1364 if (!event
.CanVeto())
1365 ans
= TRUE
; // Must delete.
1367 ans
= m_childView
->Close(FALSE
); // FALSE means don't delete associated window
1371 m_childView
->Activate(FALSE
);
1373 m_childView
= (wxView
*) NULL
;
1374 m_childDocument
= (wxDocument
*) NULL
;
1386 * Default parent frame
1389 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1390 EVT_MENU(wxID_EXIT
, wxDocParentFrame::OnExit
)
1391 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE9
, wxDocParentFrame::OnMRUFile
)
1392 EVT_CLOSE(wxDocParentFrame::OnCloseWindow
)
1395 wxDocParentFrame::wxDocParentFrame(wxDocManager
*manager
, wxFrame
*frame
, wxWindowID id
, const wxString
& title
,
1396 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
1397 wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1399 m_docManager
= manager
;
1402 void wxDocParentFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
1407 void wxDocParentFrame::OnMRUFile(wxCommandEvent
& event
)
1409 wxString
f(m_docManager
->GetHistoryFile(event
.GetSelection() - wxID_FILE1
));
1411 (void)m_docManager
->CreateDocument(f
, wxDOC_SILENT
);
1414 // Extend event processing to search the view's event table
1415 bool wxDocParentFrame::ProcessEvent(wxEvent
& event
)
1417 // Try the document manager, then do default processing
1418 if (!m_docManager
|| !m_docManager
->ProcessEvent(event
))
1419 return wxEvtHandler::ProcessEvent(event
);
1424 // Define the behaviour for the frame closing
1425 // - must delete all frames except for the main one.
1426 void wxDocParentFrame::OnCloseWindow(wxCloseEvent
& event
)
1428 if (m_docManager
->Clear(!event
.CanVeto()))
1436 #if wxUSE_PRINTING_ARCHITECTURE
1438 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
):
1439 wxPrintout(WXSTRINGCAST title
)
1441 m_printoutView
= view
;
1444 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
1448 // Get the logical pixels per inch of screen and printer
1449 int ppiScreenX
, ppiScreenY
;
1450 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
1451 int ppiPrinterX
, ppiPrinterY
;
1452 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
1454 // This scales the DC so that the printout roughly represents the
1455 // the screen scaling. The text point size _should_ be the right size
1456 // but in fact is too small for some reason. This is a detail that will
1457 // need to be addressed at some point but can be fudged for the
1459 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
1461 // Now we have to check in case our real page size is reduced
1462 // (e.g. because we're drawing to a print preview memory DC)
1463 int pageWidth
, pageHeight
;
1465 dc
->GetSize(&w
, &h
);
1466 GetPageSizePixels(&pageWidth
, &pageHeight
);
1468 // If printer pageWidth == current DC width, then this doesn't
1469 // change. But w might be the preview bitmap width, so scale down.
1470 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
1471 dc
->SetUserScale(overallScale
, overallScale
);
1475 m_printoutView
->OnDraw(dc
);
1480 bool wxDocPrintout::HasPage(int pageNum
)
1482 return (pageNum
== 1);
1485 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
1487 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
1493 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
1504 * Command processing framework
1507 wxCommand::wxCommand(bool canUndoIt
, const wxString
& name
)
1509 m_canUndo
= canUndoIt
;
1510 m_commandName
= name
;
1513 wxCommand::~wxCommand(void)
1517 // Command processor
1518 wxCommandProcessor::wxCommandProcessor(int maxCommands
)
1520 m_maxNoCommands
= maxCommands
;
1521 m_currentCommand
= (wxNode
*) NULL
;
1522 m_commandEditMenu
= (wxMenu
*) NULL
;
1525 wxCommandProcessor::~wxCommandProcessor(void)
1530 // Pass a command to the processor. The processor calls Do();
1531 // if successful, is appended to the command history unless
1532 // storeIt is FALSE.
1533 bool wxCommandProcessor::Submit(wxCommand
*command
, bool storeIt
)
1535 bool success
= command
->Do();
1536 if (success
&& storeIt
)
1538 if (m_commands
.Number() == m_maxNoCommands
)
1540 wxNode
*firstNode
= m_commands
.First();
1541 wxCommand
*firstCommand
= (wxCommand
*)firstNode
->Data();
1542 delete firstCommand
;
1546 // Correct a bug: we must chop off the current 'branch'
1547 // so that we're at the end of the command list.
1548 if (!m_currentCommand
)
1552 wxNode
*node
= m_currentCommand
->Next();
1555 wxNode
*next
= node
->Next();
1556 delete (wxCommand
*)node
->Data();
1562 m_commands
.Append(command
);
1563 m_currentCommand
= m_commands
.Last();
1569 bool wxCommandProcessor::Undo(void)
1571 if (m_currentCommand
)
1573 wxCommand
*command
= (wxCommand
*)m_currentCommand
->Data();
1574 if (command
->CanUndo())
1576 bool success
= command
->Undo();
1579 m_currentCommand
= m_currentCommand
->Previous();
1588 bool wxCommandProcessor::Redo(void)
1590 wxCommand
*redoCommand
= (wxCommand
*) NULL
;
1591 wxNode
*redoNode
= (wxNode
*) NULL
;
1592 if (m_currentCommand
&& m_currentCommand
->Next())
1594 redoCommand
= (wxCommand
*)m_currentCommand
->Next()->Data();
1595 redoNode
= m_currentCommand
->Next();
1599 if (m_commands
.Number() > 0)
1601 redoCommand
= (wxCommand
*)m_commands
.First()->Data();
1602 redoNode
= m_commands
.First();
1608 bool success
= redoCommand
->Do();
1611 m_currentCommand
= redoNode
;
1619 bool wxCommandProcessor::CanUndo(void) const
1621 if (m_currentCommand
)
1622 return ((wxCommand
*)m_currentCommand
->Data())->CanUndo();
1626 bool wxCommandProcessor::CanRedo(void) const
1628 if ((m_currentCommand
!= (wxNode
*) NULL
) && (m_currentCommand
->Next() == (wxNode
*) NULL
))
1631 if ((m_currentCommand
!= (wxNode
*) NULL
) && (m_currentCommand
->Next() != (wxNode
*) NULL
))
1634 if ((m_currentCommand
== (wxNode
*) NULL
) && (m_commands
.Number() > 0))
1640 void wxCommandProcessor::Initialize(void)
1642 m_currentCommand
= m_commands
.Last();
1646 void wxCommandProcessor::SetMenuStrings(void)
1648 if (m_commandEditMenu
)
1651 if (m_currentCommand
)
1653 wxCommand
*command
= (wxCommand
*)m_currentCommand
->Data();
1654 wxString
commandName(command
->GetName());
1655 if (commandName
== "") commandName
= _("Unnamed command");
1656 bool canUndo
= command
->CanUndo();
1658 buf
= wxString(_("&Undo ")) + commandName
;
1660 buf
= wxString(_("Can't &Undo ")) + commandName
;
1662 m_commandEditMenu
->SetLabel(wxID_UNDO
, buf
);
1663 m_commandEditMenu
->Enable(wxID_UNDO
, canUndo
);
1665 // We can redo, if we're not at the end of the history.
1666 if (m_currentCommand
->Next())
1668 wxCommand
*redoCommand
= (wxCommand
*)m_currentCommand
->Next()->Data();
1669 wxString
redoCommandName(redoCommand
->GetName());
1670 if (redoCommandName
== "") redoCommandName
= _("Unnamed command");
1671 buf
= wxString(_("&Redo ")) + redoCommandName
;
1672 m_commandEditMenu
->SetLabel(wxID_REDO
, buf
);
1673 m_commandEditMenu
->Enable(wxID_REDO
, TRUE
);
1677 m_commandEditMenu
->SetLabel(wxID_REDO
, _("&Redo"));
1678 m_commandEditMenu
->Enable(wxID_REDO
, FALSE
);
1683 m_commandEditMenu
->SetLabel(wxID_UNDO
, _("&Undo"));
1684 m_commandEditMenu
->Enable(wxID_UNDO
, FALSE
);
1686 if (m_commands
.Number() == 0)
1688 m_commandEditMenu
->SetLabel(wxID_REDO
, _("&Redo"));
1689 m_commandEditMenu
->Enable(wxID_REDO
, FALSE
);
1693 // currentCommand is NULL but there are commands: this means that
1694 // we've undone to the start of the list, but can redo the first.
1695 wxCommand
*redoCommand
= (wxCommand
*)m_commands
.First()->Data();
1696 wxString
redoCommandName(redoCommand
->GetName());
1697 if (!redoCommandName
) redoCommandName
= _("Unnamed command");
1698 buf
= wxString(_("&Redo ")) + redoCommandName
;
1699 m_commandEditMenu
->SetLabel(wxID_REDO
, buf
);
1700 m_commandEditMenu
->Enable(wxID_REDO
, TRUE
);
1706 void wxCommandProcessor::ClearCommands(void)
1708 wxNode
*node
= m_commands
.First();
1711 wxCommand
*command
= (wxCommand
*)node
->Data();
1714 node
= m_commands
.First();
1716 m_currentCommand
= (wxNode
*) NULL
;
1721 * File history processor
1724 wxFileHistory::wxFileHistory(int maxFiles
)
1726 m_fileMaxFiles
= maxFiles
;
1728 m_fileHistory
= new char *[m_fileMaxFiles
];
1731 wxFileHistory::~wxFileHistory(void)
1734 for (i
= 0; i
< m_fileHistoryN
; i
++)
1735 delete[] m_fileHistory
[i
];
1736 delete[] m_fileHistory
;
1739 // File history management
1740 void wxFileHistory::AddFileToHistory(const wxString
& file
)
1743 // Check we don't already have this file
1744 for (i
= 0; i
< m_fileHistoryN
; i
++)
1746 if (m_fileHistory
[i
] && wxString(m_fileHistory
[i
]) == file
)
1750 // Add to the project file history:
1751 // Move existing files (if any) down so we can insert file at beginning.
1753 // First delete filename that has popped off the end of the array (if any)
1754 if (m_fileHistoryN
== m_fileMaxFiles
)
1756 delete[] m_fileHistory
[m_fileMaxFiles
-1];
1757 m_fileHistory
[m_fileMaxFiles
-1] = (char *) NULL
;
1759 if (m_fileHistoryN
< m_fileMaxFiles
)
1761 wxNode
* node
= m_fileMenus
.First();
1764 wxMenu
* menu
= (wxMenu
*) node
->Data();
1765 if (m_fileHistoryN
== 0)
1766 menu
->AppendSeparator();
1767 menu
->Append(wxID_FILE1
+m_fileHistoryN
, _("[EMPTY]"));
1768 node
= node
->Next();
1772 // Shuffle filenames down
1773 for (i
= (m_fileHistoryN
-1); i
> 0; i
--)
1775 m_fileHistory
[i
] = m_fileHistory
[i
-1];
1777 m_fileHistory
[0] = copystring(file
);
1779 for (i
= 0; i
< m_fileHistoryN
; i
++)
1780 if (m_fileHistory
[i
])
1783 sprintf(buf
, "&%d %s", i
+1, m_fileHistory
[i
]);
1784 wxNode
* node
= m_fileMenus
.First();
1787 wxMenu
* menu
= (wxMenu
*) node
->Data();
1788 menu
->SetLabel(wxID_FILE1
+i
, buf
);
1789 node
= node
->Next();
1794 wxString
wxFileHistory::GetHistoryFile(int i
) const
1796 if (i
< m_fileHistoryN
)
1797 return wxString(m_fileHistory
[i
]);
1799 return wxString("");
1802 void wxFileHistory::UseMenu(wxMenu
*menu
)
1804 if (!m_fileMenus
.Member(menu
))
1805 m_fileMenus
.Append(menu
);
1808 void wxFileHistory::RemoveMenu(wxMenu
*menu
)
1810 m_fileMenus
.DeleteObject(menu
);
1813 void wxFileHistory::Load(wxConfigBase
& config
)
1817 sprintf(buf
, "file%d", m_fileHistoryN
+1);
1818 wxString
historyFile("");
1819 while ((m_fileHistoryN
<= m_fileMaxFiles
) && config
.Read(buf
, &historyFile
) && (historyFile
!= ""))
1821 m_fileHistory
[m_fileHistoryN
] = copystring((const char*) historyFile
);
1823 sprintf(buf
, "file%d", m_fileHistoryN
+1);
1829 void wxFileHistory::Save(wxConfigBase
& config
)
1832 for (i
= 0; i
< m_fileHistoryN
; i
++)
1835 buf
.Printf("file%d", i
+1);
1836 config
.Write(buf
, wxString(m_fileHistory
[i
]));
1840 void wxFileHistory::AddFilesToMenu()
1842 if (m_fileHistoryN
> 0)
1844 wxNode
* node
= m_fileMenus
.First();
1847 wxMenu
* menu
= (wxMenu
*) node
->Data();
1848 menu
->AppendSeparator();
1850 for (i
= 0; i
< m_fileHistoryN
; i
++)
1852 if (m_fileHistory
[i
])
1855 buf
.Printf("&%d %s", i
+1, m_fileHistory
[i
]);
1856 menu
->Append(wxID_FILE1
+i
, buf
);
1859 node
= node
->Next();
1864 void wxFileHistory::AddFilesToMenu(wxMenu
* menu
)
1866 if (m_fileHistoryN
> 0)
1868 menu
->AppendSeparator();
1870 for (i
= 0; i
< m_fileHistoryN
; i
++)
1872 if (m_fileHistory
[i
])
1875 buf
.Printf("&%d %s", i
+1, m_fileHistory
[i
]);
1876 menu
->Append(wxID_FILE1
+i
, buf
);
1887 wxPrintInfo::wxPrintInfo(void)
1892 wxPrintInfo::~wxPrintInfo(void)
1898 * Permits compatibility with existing file formats and functions
1899 * that manipulate files directly
1902 bool wxTransferFileToStream(const wxString
& filename
, ostream
& stream
)
1907 if ((fd1
= fopen (WXSTRINGCAST filename
, "rb")) == NULL
)
1910 while ((ch
= getc (fd1
)) != EOF
)
1911 stream
<< (unsigned char)ch
;
1917 bool wxTransferStreamToFile(istream
& stream
, const wxString
& filename
)
1922 if ((fd1
= fopen (WXSTRINGCAST filename
, "wb")) == NULL
)
1927 while (!stream
.eof())
1938 // End wxUSE_DOC_VIEW_ARCHITECTURE