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 USE_DOC_VIEW_ARCHITECTURE
33 #include "wx/dialog.h"
36 #include "wx/filedlg.h"
43 #include "wx/msgdlg.h"
44 #include "wx/choicdlg.h"
45 #include "wx/docview.h"
46 #include "wx/printdlg.h"
47 #include "wx/generic/prntdlgg.h"
48 #include "wx/generic/printps.h"
61 #if !USE_SHARED_LIBRARY
62 IMPLEMENT_ABSTRACT_CLASS(wxDocument
, wxEvtHandler
)
63 IMPLEMENT_ABSTRACT_CLASS(wxView
, wxEvtHandler
)
64 IMPLEMENT_ABSTRACT_CLASS(wxDocTemplate
, wxObject
)
65 IMPLEMENT_DYNAMIC_CLASS(wxDocManager
, wxEvtHandler
)
66 IMPLEMENT_CLASS(wxDocChildFrame
, wxFrame
)
67 IMPLEMENT_CLASS(wxDocParentFrame
, wxFrame
)
68 #if USE_PRINTING_ARCHITECTURE
69 IMPLEMENT_DYNAMIC_CLASS(wxDocPrintout
, wxPrintout
)
71 IMPLEMENT_CLASS(wxCommand
, wxObject
)
72 IMPLEMENT_DYNAMIC_CLASS(wxCommandProcessor
, wxObject
)
73 IMPLEMENT_DYNAMIC_CLASS(wxFileHistory
, wxObject
)
74 // IMPLEMENT_DYNAMIC_CLASS(wxPrintInfo, wxObject)
78 * Definition of wxDocument
81 wxDocument::wxDocument(wxDocument
*parent
)
83 m_documentModified
=FALSE
;
86 m_documentParent
=parent
;
87 m_documentTemplate
= NULL
;
88 m_documentTypeName
= "";
92 bool wxDocument::DeleteContents(void)
97 wxDocument::~wxDocument(void)
101 if (m_commandProcessor
)
102 delete m_commandProcessor
;
104 GetDocumentManager()->RemoveDocument(this);
106 // Not safe to do here, since it'll
107 // invoke virtual view functions expecting to see
108 // valid derived objects: and by the time we get
109 // here, we've called destructors higher up.
113 bool wxDocument::Close(void)
115 if (OnSaveModified())
116 return OnCloseDocument();
121 bool wxDocument::OnCloseDocument(void)
128 // Note that this implicitly deletes the document when
129 // the last view is deleted.
130 bool wxDocument::DeleteAllViews(void)
132 wxNode
*node
= m_documentViews
.First();
135 wxView
*view
= (wxView
*)node
->Data();
139 wxNode
*next
= node
->Next();
141 delete view
; // Deletes node implicitly
147 wxView
*wxDocument::GetFirstView(void) const
149 if (m_documentViews
.Number() == 0)
151 return (wxView
*)m_documentViews
.First()->Data();
154 wxDocManager
*wxDocument::GetDocumentManager(void) const
156 return m_documentTemplate
->GetDocumentManager();
159 bool wxDocument::OnNewDocument(void)
161 if (!OnSaveModified())
164 if (OnCloseDocument()==FALSE
) return FALSE
;
167 SetDocumentSaved(FALSE
);
170 GetDocumentManager()->MakeDefaultName(name
);
172 SetFilename(name
, TRUE
);
177 bool wxDocument::Save(void)
181 if (!IsModified()) return TRUE
;
182 if (m_documentFile
== "" || !m_savedYet
)
185 ret
= OnSaveDocument(m_documentFile
);
187 SetDocumentSaved(TRUE
);
191 bool wxDocument::SaveAs(void)
193 wxDocTemplate
*docTemplate
= GetDocumentTemplate();
197 char *tmp
= wxFileSelector("Save as", docTemplate
->GetDirectory(), GetFilename(),
198 docTemplate
->GetDefaultExtension(), docTemplate
->GetFileFilter(),
199 0, GetDocumentWindow());
206 SetTitle(wxFileNameFromPath(tmp
));
208 GetDocumentManager()->AddFileToHistory(tmp
);
210 // Notify the views that the filename has changed
211 wxNode
*node
= m_documentViews
.First();
214 wxView
*view
= (wxView
*)node
->Data();
215 view
->OnChangeFilename();
219 return OnSaveDocument(m_documentFile
);
222 bool wxDocument::OnSaveDocument(const wxString
& file
)
228 if (wxTheApp
->GetAppName() != "")
229 msgTitle
= wxTheApp
->GetAppName();
231 msgTitle
= wxString("File error");
233 ofstream
store(file
);
234 if (store
.fail() || store
.bad())
236 (void)wxMessageBox("Sorry, could not open this file for saving.", msgTitle
, wxOK
| wxICON_EXCLAMATION
,
237 GetDocumentWindow());
241 if (SaveObject(store
)==FALSE
)
243 (void)wxMessageBox("Sorry, could not save this file.", msgTitle
, wxOK
| wxICON_EXCLAMATION
,
244 GetDocumentWindow());
253 bool wxDocument::OnOpenDocument(const wxString
& file
)
255 if (!OnSaveModified())
259 if (wxTheApp
->GetAppName() != "")
260 msgTitle
= wxTheApp
->GetAppName();
262 msgTitle
= wxString("File error");
264 ifstream
store(file
);
265 if (store
.fail() || store
.bad())
267 (void)wxMessageBox("Sorry, could not open this file.", msgTitle
, wxOK
|wxICON_EXCLAMATION
,
268 GetDocumentWindow());
271 if (LoadObject(store
)==FALSE
)
273 (void)wxMessageBox("Sorry, could not open this file.", msgTitle
, wxOK
|wxICON_EXCLAMATION
,
274 GetDocumentWindow());
277 SetFilename(file
, TRUE
);
285 istream
& wxDocument::LoadObject(istream
& stream
)
287 // wxObject::LoadObject(stream);
292 ostream
& wxDocument::SaveObject(ostream
& stream
)
294 // wxObject::SaveObject(stream);
299 bool wxDocument::Revert(void)
305 // Get title, or filename if no title, else unnamed
306 bool wxDocument::GetPrintableName(wxString
& buf
) const
308 if (m_documentTitle
!= "")
310 buf
= m_documentTitle
;
313 else if (m_documentFile
!= "")
315 buf
= wxFileNameFromPath(m_documentFile
);
325 wxWindow
*wxDocument::GetDocumentWindow(void) const
327 wxView
*view
= GetFirstView();
329 return view
->GetFrame();
331 return wxTheApp
->GetTopWindow();
334 wxCommandProcessor
*wxDocument::OnCreateCommandProcessor(void)
336 return new wxCommandProcessor
;
339 // TRUE if safe to close
340 bool wxDocument::OnSaveModified(void)
346 GetPrintableName(title
);
349 if (wxTheApp
->GetAppName() != "")
350 msgTitle
= wxTheApp
->GetAppName();
352 msgTitle
= wxString("Warning");
354 sprintf(buf
, "Do you want to save changes to document %s?", (const char *)title
);
355 int res
= wxMessageBox(buf
, msgTitle
, wxYES_NO
|wxCANCEL
|wxICON_QUESTION
,
356 GetDocumentWindow());
362 else if (res
== wxYES
)
364 else if (res
== wxCANCEL
)
370 bool wxDocument::Draw(wxDC
& WXUNUSED(context
))
375 bool wxDocument::AddView(wxView
*view
)
377 if (!m_documentViews
.Member(view
))
379 m_documentViews
.Append(view
);
385 bool wxDocument::RemoveView(wxView
*view
)
387 (void)m_documentViews
.DeleteObject(view
);
392 bool wxDocument::OnCreate(const wxString
& WXUNUSED(path
), long flags
)
394 if (GetDocumentTemplate()->CreateView(this, flags
))
400 // Called after a view is added or removed.
401 // The default implementation deletes the document if
402 // there are no more views.
403 void wxDocument::OnChangedViewList(void)
405 if (m_documentViews
.Number() == 0)
407 if (OnSaveModified())
414 void wxDocument::UpdateAllViews(wxView
*sender
, wxObject
*hint
)
416 wxNode
*node
= m_documentViews
.First();
419 wxView
*view
= (wxView
*)node
->Data();
420 view
->OnUpdate(sender
, hint
);
425 void wxDocument::SetFilename(const wxString
& filename
, bool notifyViews
)
427 m_documentFile
= filename
;
430 // Notify the views that the filename has changed
431 wxNode
*node
= m_documentViews
.First();
434 wxView
*view
= (wxView
*)node
->Data();
435 view
->OnChangeFilename();
446 wxView::wxView(wxDocument
*doc
)
454 wxView::~wxView(void)
456 GetDocumentManager()->ActivateView(this, FALSE
, TRUE
);
457 m_viewDocument
->RemoveView(this);
460 // Extend event processing to search the document's event table
461 bool wxView::ProcessEvent(wxEvent
& event
)
463 if ( !GetDocument() || !GetDocument()->ProcessEvent(event
) )
464 return wxEvtHandler::ProcessEvent(event
);
469 void wxView::OnActivateView(bool WXUNUSED(activate
), wxView
*WXUNUSED(activeView
), wxView
*WXUNUSED(deactiveView
))
473 void wxView::OnPrint(wxDC
*dc
, wxObject
*WXUNUSED(info
))
478 void wxView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
))
482 void wxView::OnChangeFilename(void)
484 if (GetFrame() && GetDocument())
487 GetDocument()->GetPrintableName(name
);
489 // If the frame is an MDI child, just set the title to the name.
490 // Otherwise, append the document name to the name of the application
491 // I have to do an illegal cast because in wxGTK, wxMDIChildFrame
492 // doesn't inherited from wxFrame, Robert Roebling
494 wxFrame
*frame
= NULL
;
495 wxMDIChildFrame
*mdi_frame
= NULL
;
497 if (GetFrame()->IsKindOf(CLASSINFO(wxMDIChildFrame
)))
498 mdi_frame
= (wxMDIChildFrame
*)GetFrame();
505 frame
->SetTitle(name
);
511 if (wxTheApp
->GetAppName() != "")
514 sprintf(buf
, "%s - %s", (const char *)wxTheApp
->GetAppName(), (const char *)name
);
515 mdi_frame
->SetTitle(buf
);
518 mdi_frame
->SetTitle(name
);
524 void wxView::SetDocument(wxDocument
*doc
)
526 m_viewDocument
= doc
;
531 bool wxView::Close(bool deleteWindow
)
533 if (OnClose(deleteWindow
))
539 void wxView::Activate(bool activate
)
541 if (GetDocumentManager())
543 OnActivateView(activate
, this, GetDocumentManager()->GetCurrentView());
544 GetDocumentManager()->ActivateView(this, activate
);
548 bool wxView::OnClose(bool WXUNUSED(deleteWindow
))
550 return GetDocument() ? GetDocument()->Close() : TRUE
;
553 #if USE_PRINTING_ARCHITECTURE
554 wxPrintout
*wxView::OnCreatePrintout(void)
556 return new wxDocPrintout(this);
565 wxDocTemplate::wxDocTemplate(wxDocManager
*manager
, const wxString
& descr
,
566 const wxString
& filter
, const wxString
& dir
, const wxString
& ext
,
567 const wxString
& docTypeName
, const wxString
& viewTypeName
,
568 wxClassInfo
*docClassInfo
, wxClassInfo
*viewClassInfo
, long flags
)
570 m_documentManager
= manager
;
572 m_description
= descr
;
575 m_fileFilter
= filter
;
577 m_docTypeName
= docTypeName
;
578 m_viewTypeName
= viewTypeName
;
579 m_documentManager
->AssociateTemplate(this);
581 m_docClassInfo
= docClassInfo
;
582 m_viewClassInfo
= viewClassInfo
;
585 wxDocTemplate::~wxDocTemplate(void)
587 m_documentManager
->DisassociateTemplate(this);
590 // Tries to dynamically construct an object of the right
592 wxDocument
*wxDocTemplate::CreateDocument(const wxString
& path
, long flags
)
596 wxDocument
*doc
= (wxDocument
*)m_docClassInfo
->CreateObject();
597 doc
->SetFilename(path
);
598 doc
->SetDocumentTemplate(this);
599 GetDocumentManager()->AddDocument(doc
);
600 doc
->SetCommandProcessor(doc
->OnCreateCommandProcessor());
602 if (doc
->OnCreate(path
, flags
))
611 wxView
*wxDocTemplate::CreateView(wxDocument
*doc
, long flags
)
613 if (!m_viewClassInfo
)
615 wxView
*view
= (wxView
*)m_viewClassInfo
->CreateObject();
616 view
->SetDocument(doc
);
617 if (view
->OnCreate(doc
, flags
))
628 BEGIN_EVENT_TABLE(wxDocManager
, wxEvtHandler
)
629 EVT_MENU(wxID_OPEN
, wxDocManager::OnFileOpen
)
630 EVT_MENU(wxID_CLOSE
, wxDocManager::OnFileClose
)
631 EVT_MENU(wxID_REVERT
, wxDocManager::OnFileRevert
)
632 EVT_MENU(wxID_NEW
, wxDocManager::OnFileNew
)
633 EVT_MENU(wxID_SAVE
, wxDocManager::OnFileSave
)
634 EVT_MENU(wxID_SAVEAS
, wxDocManager::OnFileSaveAs
)
635 EVT_MENU(wxID_UNDO
, wxDocManager::OnUndo
)
636 EVT_MENU(wxID_REDO
, wxDocManager::OnRedo
)
637 EVT_MENU(wxID_PRINT
, wxDocManager::OnPrint
)
638 EVT_MENU(wxID_PRINT_SETUP
, wxDocManager::OnPrintSetup
)
639 EVT_MENU(wxID_PREVIEW
, wxDocManager::OnPreview
)
642 wxDocManager::wxDocManager(long flags
, bool initialize
)
644 m_defaultDocumentNameCounter
= 1;
646 m_currentView
= NULL
;
647 m_maxDocsOpen
= 10000;
648 m_fileHistory
= NULL
;
653 wxDocManager::~wxDocManager(void)
657 delete m_fileHistory
;
660 bool wxDocManager::Clear(bool force
)
662 wxNode
*node
= m_docs
.First();
665 wxDocument
*doc
= (wxDocument
*)node
->Data();
666 wxNode
*next
= node
->Next();
668 if (!doc
->Close() && !force
)
671 // Implicitly deletes the document when the last
672 // view is removed (deleted)
673 doc
->DeleteAllViews();
675 // Check document is deleted
676 if (m_docs
.Member(doc
))
679 // This assumes that documents are not connected in
680 // any way, i.e. deleting one document does NOT
684 node
= m_templates
.First();
687 wxDocTemplate
*templ
= (wxDocTemplate
*) node
->Data();
688 wxNode
* next
= node
->Next();
695 bool wxDocManager::Initialize(void)
697 m_fileHistory
= OnCreateFileHistory();
701 wxFileHistory
*wxDocManager::OnCreateFileHistory(void)
703 return new wxFileHistory
;
706 void wxDocManager::OnFileClose(wxCommandEvent
& WXUNUSED(event
))
708 wxDocument
*doc
= GetCurrentDocument();
713 doc
->DeleteAllViews();
714 if (m_docs
.Member(doc
))
719 void wxDocManager::OnFileNew(wxCommandEvent
& WXUNUSED(event
))
721 CreateDocument(wxString(""), wxDOC_NEW
);
724 void wxDocManager::OnFileOpen(wxCommandEvent
& WXUNUSED(event
))
726 CreateDocument(wxString(""), 0);
729 void wxDocManager::OnFileRevert(wxCommandEvent
& WXUNUSED(event
))
731 wxDocument
*doc
= GetCurrentDocument();
737 void wxDocManager::OnFileSave(wxCommandEvent
& WXUNUSED(event
))
739 wxDocument
*doc
= GetCurrentDocument();
745 void wxDocManager::OnFileSaveAs(wxCommandEvent
& WXUNUSED(event
))
747 wxDocument
*doc
= GetCurrentDocument();
753 void wxDocManager::OnPrint(wxCommandEvent
& WXUNUSED(event
))
755 wxView
*view
= GetCurrentView();
759 wxPrintout
*printout
= view
->OnCreatePrintout();
762 // TODO: trouble about this is that it pulls in the postscript
765 if ( wxTheApp
->GetPrintMode() == wxPRINT_WINDOWS
)
767 wxWindowsPrinter printer
;
768 printer
.Print(view
->GetFrame(), printout
, TRUE
);
773 wxPostScriptPrinter printer
;
774 printer
.Print(view
->GetFrame(), printout
, TRUE
);
781 void wxDocManager::OnPrintSetup(wxCommandEvent
& WXUNUSED(event
))
783 wxWindow
*parentWin
= wxTheApp
->GetTopWindow();
784 wxView
*view
= GetCurrentView();
786 parentWin
= view
->GetFrame();
791 if ( wxTheApp
->GetPrintMode() == wxPRINT_WINDOWS
)
793 wxPrintDialog
printerDialog(parentWin
, & data
);
794 printerDialog
.GetPrintData().SetSetupDialog(TRUE
);
795 printerDialog
.ShowModal();
800 wxGenericPrintDialog
printerDialog(parentWin
, & data
);
801 printerDialog
.GetPrintData().SetSetupDialog(TRUE
);
802 printerDialog
.ShowModal();
806 void wxDocManager::OnPreview(wxCommandEvent
& WXUNUSED(event
))
808 wxView
*view
= GetCurrentView();
812 wxPrintout
*printout
= view
->OnCreatePrintout();
815 // Pass two printout objects: for preview, and possible printing.
816 wxPrintPreviewBase
*preview
= NULL
;
818 if ( wxTheApp
->GetPrintMode() == wxPRINT_WINDOWS
)
819 preview
= new wxWindowsPrintPreview(printout
, view
->OnCreatePrintout());
822 preview
= new wxPostScriptPrintPreview(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();
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())
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
))
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();
948 wxDocTemplate
*temp
= 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
))
973 AddFileToHistory(path2
);
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())
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());
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();
1045 // Make a default document name
1046 bool wxDocManager::MakeDefaultName(wxString
& name
)
1049 sprintf(buf
, "unnamed%d", m_defaultDocumentNameCounter
);
1050 m_defaultDocumentNameCounter
++;
1055 // Not yet implemented
1056 wxDocTemplate
*wxDocManager::MatchTemplate(const wxString
& WXUNUSED(path
))
1061 // File history management
1062 void wxDocManager::AddFileToHistory(const wxString
& file
)
1065 m_fileHistory
->AddFileToHistory(file
);
1068 wxString
wxDocManager::GetHistoryFile(int i
) const
1071 return wxString(m_fileHistory
->GetHistoryFile(i
));
1073 return wxString("");
1076 void wxDocManager::FileHistoryUseMenu(wxMenu
*menu
)
1079 m_fileHistory
->FileHistoryUseMenu(menu
);
1082 void wxDocManager::FileHistoryLoad(const wxString
& resourceFile
, const wxString
& section
)
1085 m_fileHistory
->FileHistoryLoad(resourceFile
, section
);
1088 void wxDocManager::FileHistorySave(const wxString
& resourceFile
, const wxString
& section
)
1091 m_fileHistory
->FileHistorySave(resourceFile
, section
);
1094 int wxDocManager::GetNoHistoryFiles(void) const
1097 return m_fileHistory
->GetNoHistoryFiles();
1102 static char *FindExtension(char *path
)
1104 static char ext
[10];
1105 int len
= strlen(path
);
1109 for (i
= (len
-1); i
> 0; i
--)
1115 for (j
= i
+1; j
< len
; j
++)
1116 ext
[(int)(j
-(i
+1))] = (char)wxToLower(path
[j
]); // NOTE Should not use tolower under UNIX
1127 // Given a path, try to find a matching template. Won't
1128 // always work, of course.
1129 wxDocTemplate
*wxDocManager::FindTemplateForPath(const wxString
& path
)
1131 char *theExt
= FindExtension((char *)(const char *)path
);
1134 wxDocTemplate
*theTemplate
= NULL
;
1136 if (m_templates
.Number() == 1)
1137 return (wxDocTemplate
*)m_templates
.First()->Data();
1139 // Find the template which this extension corresponds to
1141 for (i
= 0; i
< m_templates
.Number(); i
++)
1143 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Nth(i
)->Data();
1144 if (strcmp(temp
->GetDefaultExtension(), theExt
) == 0)
1153 // Prompts user to open a file, using file specs in templates.
1154 // How to implement in wxWindows? Must extend the file selector
1155 // dialog or implement own; OR match the extension to the
1156 // template extension.
1157 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**templates
,
1158 int noTemplates
, wxString
& path
, long WXUNUSED(flags
), bool WXUNUSED(save
))
1160 // We can only have multiple filters in Windows
1162 char *descrBuf
= new char[1000];
1165 for (i
= 0; i
< noTemplates
; i
++)
1167 if (templates
[i
]->IsVisible())
1169 strcat(descrBuf
, templates
[i
]->GetDescription());
1170 strcat(descrBuf
, " (");
1171 strcat(descrBuf
, templates
[i
]->GetFileFilter());
1172 strcat(descrBuf
, ") ");
1173 strcat(descrBuf
, "|");
1174 strcat(descrBuf
, templates
[i
]->GetFileFilter());
1175 strcat(descrBuf
, "|");
1178 int len
= strlen(descrBuf
);
1181 descrBuf
[len
-1] = 0;
1183 char *pathTmp
= wxFileSelector("Select a file", "", "", "", descrBuf
, 0, wxTheApp
->GetTopWindow());
1188 char *theExt
= FindExtension((char *)(const char *)path
);
1192 // This is dodgy in that we're selecting the template on the
1193 // basis of the file extension, which may not be a standard
1194 // one. We really want to know exactly which template was
1195 // chosen by using a more advanced file selector.
1196 wxDocTemplate
*theTemplate
= FindTemplateForPath(path
);
1205 // In all other windowing systems, until we have more advanced
1206 // file selectors, we must select the document type (template) first, and
1207 // _then_ pop up the file selector.
1208 wxDocTemplate
*temp
= SelectDocumentType(templates
, noTemplates
);
1212 char *pathTmp
= wxFileSelector("Select a file", "", "",
1213 temp
->GetDefaultExtension(),
1214 temp
->GetFileFilter(),
1215 0, wxTheApp
->GetTopWindow());
1227 wxDocTemplate
*wxDocManager::SelectDocumentType(wxDocTemplate
**templates
,
1230 char **strings
= new char *[noTemplates
];
1231 char **data
= new char *[noTemplates
];
1234 for (i
= 0; i
< noTemplates
; i
++)
1236 if (templates
[i
]->IsVisible())
1238 strings
[n
] = WXSTRINGCAST templates
[i
]->m_description
;
1239 data
[n
] = (char *)templates
[i
];
1251 wxDocTemplate
*temp
= (wxDocTemplate
*)data
[0];
1257 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData("Select a document template", "Templates", n
,
1264 wxDocTemplate
*wxDocManager::SelectViewType(wxDocTemplate
**templates
,
1267 char **strings
= new char *[noTemplates
];
1268 char **data
= new char *[noTemplates
];
1271 for (i
= 0; i
< noTemplates
; i
++)
1273 if (templates
[i
]->IsVisible() && templates
[i
]->GetViewName())
1275 strings
[n
] = WXSTRINGCAST templates
[i
]->m_viewTypeName
;
1276 data
[n
] = (char *)templates
[i
];
1280 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData("Select a document view", "Views", n
,
1287 void wxDocManager::AssociateTemplate(wxDocTemplate
*temp
)
1289 if (!m_templates
.Member(temp
))
1290 m_templates
.Append(temp
);
1293 void wxDocManager::DisassociateTemplate(wxDocTemplate
*temp
)
1295 m_templates
.DeleteObject(temp
);
1298 // Add and remove a document from the manager's list
1299 void wxDocManager::AddDocument(wxDocument
*doc
)
1301 if (!m_docs
.Member(doc
))
1305 void wxDocManager::RemoveDocument(wxDocument
*doc
)
1307 m_docs
.DeleteObject(doc
);
1310 // Views or windows should inform the document manager
1311 // when a view is going in or out of focus
1312 void wxDocManager::ActivateView(wxView
*view
, bool activate
, bool WXUNUSED(deleting
))
1314 // If we're deactiving, and if we're not actually deleting the view, then
1315 // don't reset the current view because we may be going to
1316 // a window without a view.
1317 // WHAT DID I MEAN BY THAT EXACTLY?
1321 if (m_currentView == view)
1322 m_currentView = NULL;
1328 m_currentView
= view
;
1330 m_currentView
= NULL
;
1335 * Default document child frame
1338 BEGIN_EVENT_TABLE(wxDocChildFrame
, wxFrame
)
1339 EVT_ACTIVATE(wxDocChildFrame::OnActivate
)
1342 wxDocChildFrame::wxDocChildFrame(wxDocument
*doc
, wxView
*view
, wxFrame
*frame
, wxWindowID id
, const wxString
& title
,
1343 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
1344 wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1346 m_childDocument
= doc
;
1349 view
->SetFrame(this);
1352 wxDocChildFrame::~wxDocChildFrame(void)
1356 // Extend event processing to search the view's event table
1357 bool wxDocChildFrame::ProcessEvent(wxEvent
& event
)
1360 m_childView
->Activate(TRUE
);
1362 if ( !m_childView
|| ! m_childView
->ProcessEvent(event
) )
1364 // Only hand up to the parent if it's a menu command
1365 if (!event
.IsKindOf(CLASSINFO(wxCommandEvent
)) || !GetParent() || !GetParent()->ProcessEvent(event
))
1366 return wxEvtHandler::ProcessEvent(event
);
1374 void wxDocChildFrame::OnActivate(wxActivateEvent
& event
)
1376 wxFrame::OnActivate(event
);
1379 m_childView
->Activate(event
.GetActive());
1382 bool wxDocChildFrame::OnClose(void)
1384 // Close view but don't delete the frame while doing so!
1385 // ...since it will be deleted by wxWindows if we return TRUE.
1388 bool ans
= m_childView
->Close(FALSE
); // FALSE means don't delete associated window
1391 m_childView
->Activate(FALSE
);
1394 m_childDocument
= NULL
;
1403 * Default parent frame
1406 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1407 EVT_MENU(wxID_EXIT
, wxDocParentFrame::OnExit
)
1408 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE9
, wxDocParentFrame::OnMRUFile
)
1411 wxDocParentFrame::wxDocParentFrame(wxDocManager
*manager
, wxFrame
*frame
, wxWindowID id
, const wxString
& title
,
1412 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
1413 wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1415 m_docManager
= manager
;
1418 void wxDocParentFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
1423 void wxDocParentFrame::OnMRUFile(wxCommandEvent
& event
)
1425 wxString
f(m_docManager
->GetHistoryFile(event
.GetSelection() - wxID_FILE1
));
1427 (void)m_docManager
->CreateDocument(f
, wxDOC_SILENT
);
1430 // Extend event processing to search the view's event table
1431 bool wxDocParentFrame::ProcessEvent(wxEvent
& event
)
1433 // Try the document manager, then do default processing
1434 if (!m_docManager
|| !m_docManager
->ProcessEvent(event
))
1435 return wxEvtHandler::ProcessEvent(event
);
1440 // Define the behaviour for the frame closing
1441 // - must delete all frames except for the main one.
1442 bool wxDocParentFrame::OnClose(void)
1444 return m_docManager
->Clear(FALSE
);
1447 #if USE_PRINTING_ARCHITECTURE
1449 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
):
1450 wxPrintout(WXSTRINGCAST title
)
1452 m_printoutView
= view
;
1455 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
1459 // Get the logical pixels per inch of screen and printer
1460 int ppiScreenX
, ppiScreenY
;
1461 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
1462 int ppiPrinterX
, ppiPrinterY
;
1463 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
1465 // This scales the DC so that the printout roughly represents the
1466 // the screen scaling. The text point size _should_ be the right size
1467 // but in fact is too small for some reason. This is a detail that will
1468 // need to be addressed at some point but can be fudged for the
1470 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
1472 // Now we have to check in case our real page size is reduced
1473 // (e.g. because we're drawing to a print preview memory DC)
1474 int pageWidth
, pageHeight
;
1476 dc
->GetSize(&w
, &h
);
1477 GetPageSizePixels(&pageWidth
, &pageHeight
);
1479 // If printer pageWidth == current DC width, then this doesn't
1480 // change. But w might be the preview bitmap width, so scale down.
1481 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
1482 dc
->SetUserScale(overallScale
, overallScale
);
1486 m_printoutView
->OnDraw(dc
);
1491 bool wxDocPrintout::HasPage(int pageNum
)
1493 return (pageNum
== 1);
1496 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
1498 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
1504 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
1515 * Command processing framework
1518 wxCommand::wxCommand(bool canUndoIt
, const wxString
& name
)
1520 m_canUndo
= canUndoIt
;
1521 m_commandName
= name
;
1524 wxCommand::~wxCommand(void)
1528 // Command processor
1529 wxCommandProcessor::wxCommandProcessor(int maxCommands
)
1531 m_maxNoCommands
= maxCommands
;
1532 m_currentCommand
= NULL
;
1533 m_commandEditMenu
= NULL
;
1536 wxCommandProcessor::~wxCommandProcessor(void)
1541 // Pass a command to the processor. The processor calls Do();
1542 // if successful, is appended to the command history unless
1543 // storeIt is FALSE.
1544 bool wxCommandProcessor::Submit(wxCommand
*command
, bool storeIt
)
1546 bool success
= command
->Do();
1547 if (success
&& storeIt
)
1549 if (m_commands
.Number() == m_maxNoCommands
)
1551 wxNode
*firstNode
= m_commands
.First();
1552 wxCommand
*firstCommand
= (wxCommand
*)firstNode
->Data();
1553 delete firstCommand
;
1557 // Correct a bug: we must chop off the current 'branch'
1558 // so that we're at the end of the command list.
1559 if (!m_currentCommand
)
1563 wxNode
*node
= m_currentCommand
->Next();
1566 wxNode
*next
= node
->Next();
1567 delete (wxCommand
*)node
->Data();
1573 m_commands
.Append(command
);
1574 m_currentCommand
= m_commands
.Last();
1580 bool wxCommandProcessor::Undo(void)
1582 if (m_currentCommand
)
1584 wxCommand
*command
= (wxCommand
*)m_currentCommand
->Data();
1585 if (command
->CanUndo())
1587 bool success
= command
->Undo();
1590 m_currentCommand
= m_currentCommand
->Previous();
1599 bool wxCommandProcessor::Redo(void)
1601 wxCommand
*redoCommand
= NULL
;
1602 wxNode
*redoNode
= NULL
;
1603 if (m_currentCommand
&& m_currentCommand
->Next())
1605 redoCommand
= (wxCommand
*)m_currentCommand
->Next()->Data();
1606 redoNode
= m_currentCommand
->Next();
1610 if (m_commands
.Number() > 0)
1612 redoCommand
= (wxCommand
*)m_commands
.First()->Data();
1613 redoNode
= m_commands
.First();
1619 bool success
= redoCommand
->Do();
1622 m_currentCommand
= redoNode
;
1630 bool wxCommandProcessor::CanUndo(void)
1632 if (m_currentCommand
)
1633 return ((wxCommand
*)m_currentCommand
->Data())->CanUndo();
1637 void wxCommandProcessor::Initialize(void)
1639 m_currentCommand
= m_commands
.Last();
1643 void wxCommandProcessor::SetMenuStrings(void)
1645 if (m_commandEditMenu
)
1648 if (m_currentCommand
)
1650 wxCommand
*command
= (wxCommand
*)m_currentCommand
->Data();
1651 wxString
commandName(command
->GetName());
1652 if (commandName
== "") commandName
= "Unnamed command";
1653 bool canUndo
= command
->CanUndo();
1655 buf
= wxString("&Undo ") + commandName
;
1657 buf
= wxString("Can't &Undo ") + commandName
;
1659 m_commandEditMenu
->SetLabel(wxID_UNDO
, buf
);
1660 m_commandEditMenu
->Enable(wxID_UNDO
, canUndo
);
1662 // We can redo, if we're not at the end of the history.
1663 if (m_currentCommand
->Next())
1665 wxCommand
*redoCommand
= (wxCommand
*)m_currentCommand
->Next()->Data();
1666 wxString
redoCommandName(redoCommand
->GetName());
1667 if (redoCommandName
== "") redoCommandName
= "Unnamed command";
1668 buf
= wxString("&Redo ") + redoCommandName
;
1669 m_commandEditMenu
->SetLabel(wxID_REDO
, buf
);
1670 m_commandEditMenu
->Enable(wxID_REDO
, TRUE
);
1674 m_commandEditMenu
->SetLabel(wxID_REDO
, "&Redo");
1675 m_commandEditMenu
->Enable(wxID_REDO
, FALSE
);
1680 m_commandEditMenu
->SetLabel(wxID_UNDO
, "&Undo");
1681 m_commandEditMenu
->Enable(wxID_UNDO
, FALSE
);
1683 if (m_commands
.Number() == 0)
1685 m_commandEditMenu
->SetLabel(wxID_REDO
, "&Redo");
1686 m_commandEditMenu
->Enable(wxID_REDO
, FALSE
);
1690 // currentCommand is NULL but there are commands: this means that
1691 // we've undone to the start of the list, but can redo the first.
1692 wxCommand
*redoCommand
= (wxCommand
*)m_commands
.First()->Data();
1693 wxString
redoCommandName(redoCommand
->GetName());
1694 if (!redoCommandName
) redoCommandName
= "Unnamed command";
1695 buf
= wxString("&Redo ") + redoCommandName
;
1696 m_commandEditMenu
->SetLabel(wxID_REDO
, buf
);
1697 m_commandEditMenu
->Enable(wxID_REDO
, TRUE
);
1703 void wxCommandProcessor::ClearCommands(void)
1705 wxNode
*node
= m_commands
.First();
1708 wxCommand
*command
= (wxCommand
*)node
->Data();
1711 node
= m_commands
.First();
1713 m_currentCommand
= NULL
;
1718 * File history processor
1721 wxFileHistory::wxFileHistory(int maxFiles
)
1723 m_fileMaxFiles
= maxFiles
;
1726 m_fileHistory
= new char *[m_fileMaxFiles
];
1729 wxFileHistory::~wxFileHistory(void)
1732 for (i
= 0; i
< m_fileHistoryN
; i
++)
1733 delete[] m_fileHistory
[i
];
1734 delete[] m_fileHistory
;
1737 // File history management
1738 void wxFileHistory::AddFileToHistory(const wxString
& file
)
1745 // Check we don't already have this file
1746 for (i
= 0; i
< m_fileHistoryN
; i
++)
1748 if (m_fileHistory
[i
] && wxString(m_fileHistory
[i
]) == file
)
1752 // Add to the project file history:
1753 // Move existing files (if any) down so we can insert file at beginning.
1755 // First delete filename that has popped off the end of the array (if any)
1756 if (m_fileHistoryN
== m_fileMaxFiles
)
1758 delete[] m_fileHistory
[m_fileMaxFiles
-1];
1759 m_fileHistory
[m_fileMaxFiles
-1] = NULL
;
1761 if (m_fileHistoryN
< m_fileMaxFiles
)
1763 if (m_fileHistoryN
== 0)
1764 m_fileMenu
->AppendSeparator();
1765 m_fileMenu
->Append(wxID_FILE1
+m_fileHistoryN
, "[EMPTY]");
1768 // Shuffle filenames down
1769 for (i
= (m_fileHistoryN
-1); i
> 0; i
--)
1771 m_fileHistory
[i
] = m_fileHistory
[i
-1];
1773 m_fileHistory
[0] = copystring(file
);
1775 for (i
= 0; i
< m_fileHistoryN
; i
++)
1776 if (m_fileHistory
[i
])
1779 sprintf(buf
, "&%d %s", i
+1, m_fileHistory
[i
]);
1780 m_fileMenu
->SetLabel(wxID_FILE1
+i
, buf
);
1784 wxString
wxFileHistory::GetHistoryFile(int i
) const
1786 if (i
< m_fileHistoryN
)
1787 return wxString(m_fileHistory
[i
]);
1789 return wxString("");
1792 void wxFileHistory::FileHistoryUseMenu(wxMenu
*menu
)
1797 void wxFileHistory::FileHistoryLoad(const wxString
& resourceFile
, const wxString
& section
)
1802 sprintf(buf
, "file%d", m_fileHistoryN
+1);
1803 char *historyFile
= NULL
;
1804 while ((m_fileHistoryN
<= m_fileMaxFiles
) && wxGetResource(section
, buf
, &historyFile
, resourceFile
) && historyFile
)
1806 // wxGetResource allocates memory so this is o.k.
1807 m_fileHistory
[m_fileHistoryN
] = historyFile
;
1809 sprintf(buf
, "file%d", m_fileHistoryN
+1);
1815 void wxFileHistory::FileHistorySave(const wxString
& resourceFile
, const wxString
& section
)
1820 for (i
= 0; i
< m_fileHistoryN
; i
++)
1822 sprintf(buf
, "file%d", i
+1);
1823 wxWriteResource(section
, buf
, m_fileHistory
[i
], resourceFile
);
1833 wxPrintInfo::wxPrintInfo(void)
1838 wxPrintInfo::~wxPrintInfo(void)
1844 * Permits compatibility with existing file formats and functions
1845 * that manipulate files directly
1848 bool wxTransferFileToStream(const wxString
& filename
, ostream
& stream
)
1853 if ((fd1
= fopen (WXSTRINGCAST filename
, "rb")) == NULL
)
1856 while ((ch
= getc (fd1
)) != EOF
)
1857 stream
<< (unsigned char)ch
;
1863 bool wxTransferStreamToFile(istream
& stream
, const wxString
& filename
)
1868 if ((fd1
= fopen (WXSTRINGCAST filename
, "wb")) == NULL
)
1873 while (!stream
.eof())
1884 // End USE_DOC_VIEW_ARCHITECTURE