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"
39 #include "wx/msgdlg.h"
40 #include "wx/choicdlg.h"
41 #include "wx/docview.h"
42 #include "wx/printdlg.h"
43 #include "wx/generic/prntdlgg.h"
44 #include "wx/generic/printps.h"
63 #if !USE_SHARED_LIBRARY
64 IMPLEMENT_ABSTRACT_CLASS(wxDocument
, wxEvtHandler
)
65 IMPLEMENT_ABSTRACT_CLASS(wxView
, wxEvtHandler
)
66 IMPLEMENT_ABSTRACT_CLASS(wxDocTemplate
, wxObject
)
67 IMPLEMENT_DYNAMIC_CLASS(wxDocManager
, wxEvtHandler
)
68 IMPLEMENT_CLASS(wxDocChildFrame
, wxFrame
)
69 IMPLEMENT_CLASS(wxDocParentFrame
, wxFrame
)
70 #if USE_PRINTING_ARCHITECTURE
71 IMPLEMENT_DYNAMIC_CLASS(wxDocPrintout
, wxPrintout
)
73 IMPLEMENT_CLASS(wxCommand
, wxObject
)
74 IMPLEMENT_DYNAMIC_CLASS(wxCommandProcessor
, wxObject
)
75 IMPLEMENT_DYNAMIC_CLASS(wxFileHistory
, wxObject
)
76 // IMPLEMENT_DYNAMIC_CLASS(wxPrintInfo, wxObject)
80 * Definition of wxDocument
83 wxDocument::wxDocument(wxDocument
*parent
)
85 m_documentModified
=FALSE
;
88 m_documentParent
=parent
;
89 m_documentTemplate
= NULL
;
90 m_documentTypeName
= "";
94 bool wxDocument::DeleteContents(void)
99 wxDocument::~wxDocument(void)
103 if (m_commandProcessor
)
104 delete m_commandProcessor
;
106 GetDocumentManager()->RemoveDocument(this);
108 // Not safe to do here, since it'll
109 // invoke virtual view functions expecting to see
110 // valid derived objects: and by the time we get
111 // here, we've called destructors higher up.
115 bool wxDocument::Close(void)
117 if (OnSaveModified())
118 return OnCloseDocument();
123 bool wxDocument::OnCloseDocument(void)
130 // Note that this implicitly deletes the document when
131 // the last view is deleted.
132 bool wxDocument::DeleteAllViews(void)
134 wxNode
*node
= m_documentViews
.First();
137 wxView
*view
= (wxView
*)node
->Data();
141 wxNode
*next
= node
->Next();
143 delete view
; // Deletes node implicitly
149 wxView
*wxDocument::GetFirstView(void) const
151 if (m_documentViews
.Number() == 0)
153 return (wxView
*)m_documentViews
.First()->Data();
156 wxDocManager
*wxDocument::GetDocumentManager(void) const
158 return m_documentTemplate
->GetDocumentManager();
161 bool wxDocument::OnNewDocument(void)
163 if (!OnSaveModified())
166 if (OnCloseDocument()==FALSE
) return FALSE
;
169 SetDocumentSaved(FALSE
);
172 GetDocumentManager()->MakeDefaultName(name
);
174 SetFilename(name
, TRUE
);
179 bool wxDocument::Save(void)
183 if (!IsModified()) return TRUE
;
184 if (m_documentFile
== "" || !m_savedYet
)
187 ret
= OnSaveDocument(m_documentFile
);
189 SetDocumentSaved(TRUE
);
193 bool wxDocument::SaveAs(void)
195 wxDocTemplate
*docTemplate
= GetDocumentTemplate();
199 char *tmp
= wxFileSelector("Save as", docTemplate
->GetDirectory(), GetFilename(),
200 docTemplate
->GetDefaultExtension(), docTemplate
->GetFileFilter(),
201 0, GetDocumentWindow());
208 SetTitle(wxFileNameFromPath(tmp
));
210 GetDocumentManager()->AddFileToHistory(tmp
);
212 // Notify the views that the filename has changed
213 wxNode
*node
= m_documentViews
.First();
216 wxView
*view
= (wxView
*)node
->Data();
217 view
->OnChangeFilename();
221 return OnSaveDocument(m_documentFile
);
224 bool wxDocument::OnSaveDocument(const wxString
& file
)
230 if (wxTheApp
->GetAppName() != "")
231 msgTitle
= wxTheApp
->GetAppName();
233 msgTitle
= wxString("File error");
235 ofstream
store(file
);
236 if (store
.fail() || store
.bad())
238 (void)wxMessageBox("Sorry, could not open this file for saving.", msgTitle
, wxOK
| wxICON_EXCLAMATION
,
239 GetDocumentWindow());
243 if (SaveObject(store
)==FALSE
)
245 (void)wxMessageBox("Sorry, could not save this file.", msgTitle
, wxOK
| wxICON_EXCLAMATION
,
246 GetDocumentWindow());
255 bool wxDocument::OnOpenDocument(const wxString
& file
)
257 if (!OnSaveModified())
261 if (wxTheApp
->GetAppName() != "")
262 msgTitle
= wxTheApp
->GetAppName();
264 msgTitle
= wxString("File error");
266 ifstream
store(file
);
267 if (store
.fail() || store
.bad())
269 (void)wxMessageBox("Sorry, could not open this file.", msgTitle
, wxOK
|wxICON_EXCLAMATION
,
270 GetDocumentWindow());
273 if (LoadObject(store
)==FALSE
)
275 (void)wxMessageBox("Sorry, could not open this file.", msgTitle
, wxOK
|wxICON_EXCLAMATION
,
276 GetDocumentWindow());
279 SetFilename(file
, TRUE
);
287 istream
& wxDocument::LoadObject(istream
& stream
)
289 // wxObject::LoadObject(stream);
294 ostream
& wxDocument::SaveObject(ostream
& stream
)
296 // wxObject::SaveObject(stream);
301 bool wxDocument::Revert(void)
307 // Get title, or filename if no title, else unnamed
308 bool wxDocument::GetPrintableName(wxString
& buf
) const
310 if (m_documentTitle
!= "")
312 buf
= m_documentTitle
;
315 else if (m_documentFile
!= "")
317 buf
= wxFileNameFromPath(m_documentFile
);
327 wxWindow
*wxDocument::GetDocumentWindow(void) const
329 wxView
*view
= GetFirstView();
331 return view
->GetFrame();
333 return wxTheApp
->GetTopWindow();
336 wxCommandProcessor
*wxDocument::OnCreateCommandProcessor(void)
338 return new wxCommandProcessor
;
341 // TRUE if safe to close
342 bool wxDocument::OnSaveModified(void)
348 GetPrintableName(title
);
351 if (wxTheApp
->GetAppName() != "")
352 msgTitle
= wxTheApp
->GetAppName();
354 msgTitle
= wxString("Warning");
356 sprintf(buf
, "Do you want to save changes to document %s?", (const char *)title
);
357 int res
= wxMessageBox(buf
, msgTitle
, wxYES_NO
|wxCANCEL
|wxICON_QUESTION
,
358 GetDocumentWindow());
364 else if (res
== wxYES
)
366 else if (res
== wxCANCEL
)
372 bool wxDocument::Draw(wxDC
& WXUNUSED(context
))
377 bool wxDocument::AddView(wxView
*view
)
379 if (!m_documentViews
.Member(view
))
381 m_documentViews
.Append(view
);
387 bool wxDocument::RemoveView(wxView
*view
)
389 (void)m_documentViews
.DeleteObject(view
);
394 bool wxDocument::OnCreate(const wxString
& WXUNUSED(path
), long flags
)
396 if (GetDocumentTemplate()->CreateView(this, flags
))
402 // Called after a view is added or removed.
403 // The default implementation deletes the document if
404 // there are no more views.
405 void wxDocument::OnChangedViewList(void)
407 if (m_documentViews
.Number() == 0)
409 if (OnSaveModified())
416 void wxDocument::UpdateAllViews(wxView
*sender
, wxObject
*hint
)
418 wxNode
*node
= m_documentViews
.First();
421 wxView
*view
= (wxView
*)node
->Data();
422 view
->OnUpdate(sender
, hint
);
427 void wxDocument::SetFilename(const wxString
& filename
, bool notifyViews
)
429 m_documentFile
= filename
;
432 // Notify the views that the filename has changed
433 wxNode
*node
= m_documentViews
.First();
436 wxView
*view
= (wxView
*)node
->Data();
437 view
->OnChangeFilename();
448 wxView::wxView(wxDocument
*doc
)
456 wxView::~wxView(void)
458 GetDocumentManager()->ActivateView(this, FALSE
, TRUE
);
459 m_viewDocument
->RemoveView(this);
462 // Extend event processing to search the document's event table
463 bool wxView::ProcessEvent(wxEvent
& event
)
465 if ( !GetDocument() || !GetDocument()->ProcessEvent(event
) )
466 return wxEvtHandler::ProcessEvent(event
);
471 void wxView::OnActivateView(bool WXUNUSED(activate
), wxView
*WXUNUSED(activeView
), wxView
*WXUNUSED(deactiveView
))
475 void wxView::OnPrint(wxDC
*dc
, wxObject
*WXUNUSED(info
))
480 void wxView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
))
484 void wxView::OnChangeFilename(void)
486 if (GetFrame() && GetDocument())
489 GetDocument()->GetPrintableName(name
);
491 // If the frame is an MDI child, just set the title
493 // Otherwise, append the document name to the name of the application
495 if (GetFrame()->IsKindOf(CLASSINFO(wxMDIChildFrame
)))
500 GetFrame()->SetTitle(name
);
504 if (wxTheApp
->GetAppName() != "")
507 sprintf(buf
, "%s - %s", (const char *)wxTheApp
->GetAppName(), (const char *)name
);
508 GetFrame()->SetTitle(buf
);
511 GetFrame()->SetTitle(name
);
516 void wxView::SetDocument(wxDocument
*doc
)
518 m_viewDocument
= doc
;
523 bool wxView::Close(bool deleteWindow
)
525 if (OnClose(deleteWindow
))
531 void wxView::Activate(bool activate
)
533 if (GetDocumentManager())
535 OnActivateView(activate
, this, GetDocumentManager()->GetCurrentView());
536 GetDocumentManager()->ActivateView(this, activate
);
540 bool wxView::OnClose(bool WXUNUSED(deleteWindow
))
542 return GetDocument() ? GetDocument()->Close() : TRUE
;
545 #if USE_PRINTING_ARCHITECTURE
546 wxPrintout
*wxView::OnCreatePrintout(void)
548 return new wxDocPrintout(this);
557 wxDocTemplate::wxDocTemplate(wxDocManager
*manager
, const wxString
& descr
,
558 const wxString
& filter
, const wxString
& dir
, const wxString
& ext
,
559 const wxString
& docTypeName
, const wxString
& viewTypeName
,
560 wxClassInfo
*docClassInfo
, wxClassInfo
*viewClassInfo
, long flags
)
562 m_documentManager
= manager
;
564 m_description
= descr
;
567 m_fileFilter
= filter
;
569 m_docTypeName
= docTypeName
;
570 m_viewTypeName
= viewTypeName
;
571 m_documentManager
->AssociateTemplate(this);
573 m_docClassInfo
= docClassInfo
;
574 m_viewClassInfo
= viewClassInfo
;
577 wxDocTemplate::~wxDocTemplate(void)
579 m_documentManager
->DisassociateTemplate(this);
582 // Tries to dynamically construct an object of the right
584 wxDocument
*wxDocTemplate::CreateDocument(const wxString
& path
, long flags
)
588 wxDocument
*doc
= (wxDocument
*)m_docClassInfo
->CreateObject();
589 doc
->SetFilename(path
);
590 doc
->SetDocumentTemplate(this);
591 GetDocumentManager()->AddDocument(doc
);
592 doc
->SetCommandProcessor(doc
->OnCreateCommandProcessor());
594 if (doc
->OnCreate(path
, flags
))
603 wxView
*wxDocTemplate::CreateView(wxDocument
*doc
, long flags
)
605 if (!m_viewClassInfo
)
607 wxView
*view
= (wxView
*)m_viewClassInfo
->CreateObject();
608 view
->SetDocument(doc
);
609 if (view
->OnCreate(doc
, flags
))
620 BEGIN_EVENT_TABLE(wxDocManager
, wxEvtHandler
)
621 EVT_MENU(wxID_OPEN
, wxDocManager::OnFileOpen
)
622 EVT_MENU(wxID_CLOSE
, wxDocManager::OnFileClose
)
623 EVT_MENU(wxID_REVERT
, wxDocManager::OnFileRevert
)
624 EVT_MENU(wxID_NEW
, wxDocManager::OnFileNew
)
625 EVT_MENU(wxID_SAVE
, wxDocManager::OnFileSave
)
626 EVT_MENU(wxID_SAVEAS
, wxDocManager::OnFileSaveAs
)
627 EVT_MENU(wxID_UNDO
, wxDocManager::OnUndo
)
628 EVT_MENU(wxID_REDO
, wxDocManager::OnRedo
)
629 EVT_MENU(wxID_PRINT
, wxDocManager::OnPrint
)
630 EVT_MENU(wxID_PRINT_SETUP
, wxDocManager::OnPrintSetup
)
631 EVT_MENU(wxID_PREVIEW
, wxDocManager::OnPreview
)
634 wxDocManager::wxDocManager(long flags
, bool initialize
)
636 m_defaultDocumentNameCounter
= 1;
638 m_currentView
= NULL
;
639 m_maxDocsOpen
= 10000;
640 m_fileHistory
= NULL
;
645 wxDocManager::~wxDocManager(void)
649 delete m_fileHistory
;
652 bool wxDocManager::Clear(bool force
)
654 wxNode
*node
= m_docs
.First();
657 wxDocument
*doc
= (wxDocument
*)node
->Data();
658 wxNode
*next
= node
->Next();
660 if (!doc
->Close() && !force
)
663 // Implicitly deletes the document when the last
664 // view is removed (deleted)
665 doc
->DeleteAllViews();
667 // Check document is deleted
668 if (m_docs
.Member(doc
))
671 // This assumes that documents are not connected in
672 // any way, i.e. deleting one document does NOT
676 node
= m_templates
.First();
679 wxDocTemplate
*templ
= (wxDocTemplate
*) node
->Data();
680 wxNode
* next
= node
->Next();
687 bool wxDocManager::Initialize(void)
689 m_fileHistory
= OnCreateFileHistory();
693 wxFileHistory
*wxDocManager::OnCreateFileHistory(void)
695 return new wxFileHistory
;
698 void wxDocManager::OnFileClose(wxCommandEvent
& WXUNUSED(event
))
700 wxDocument
*doc
= GetCurrentDocument();
705 doc
->DeleteAllViews();
706 if (m_docs
.Member(doc
))
711 void wxDocManager::OnFileNew(wxCommandEvent
& WXUNUSED(event
))
713 CreateDocument(wxString(""), wxDOC_NEW
);
716 void wxDocManager::OnFileOpen(wxCommandEvent
& WXUNUSED(event
))
718 CreateDocument(wxString(""), 0);
721 void wxDocManager::OnFileRevert(wxCommandEvent
& WXUNUSED(event
))
723 wxDocument
*doc
= GetCurrentDocument();
729 void wxDocManager::OnFileSave(wxCommandEvent
& WXUNUSED(event
))
731 wxDocument
*doc
= GetCurrentDocument();
737 void wxDocManager::OnFileSaveAs(wxCommandEvent
& WXUNUSED(event
))
739 wxDocument
*doc
= GetCurrentDocument();
745 void wxDocManager::OnPrint(wxCommandEvent
& WXUNUSED(event
))
747 wxView
*view
= GetCurrentView();
751 wxPrintout
*printout
= view
->OnCreatePrintout();
754 // TODO: trouble about this is that it pulls in the postscript
757 if ( wxTheApp
->GetPrintMode() == wxPRINT_WINDOWS
)
759 wxWindowsPrinter printer
;
760 printer
.Print(view
->GetFrame(), printout
, TRUE
);
765 wxPostScriptPrinter printer
;
766 printer
.Print(view
->GetFrame(), printout
, TRUE
);
773 void wxDocManager::OnPrintSetup(wxCommandEvent
& WXUNUSED(event
))
775 wxWindow
*parentWin
= wxTheApp
->GetTopWindow();
776 wxView
*view
= GetCurrentView();
778 parentWin
= view
->GetFrame();
783 if ( wxTheApp
->GetPrintMode() == wxPRINT_WINDOWS
)
785 wxPrintDialog
printerDialog(parentWin
, & data
);
786 printerDialog
.GetPrintData().SetSetupDialog(TRUE
);
787 printerDialog
.ShowModal();
792 wxGenericPrintDialog
printerDialog(parentWin
, & data
);
793 printerDialog
.GetPrintData().SetSetupDialog(TRUE
);
794 printerDialog
.ShowModal();
798 void wxDocManager::OnPreview(wxCommandEvent
& WXUNUSED(event
))
800 wxView
*view
= GetCurrentView();
804 wxPrintout
*printout
= view
->OnCreatePrintout();
807 // Pass two printout objects: for preview, and possible printing.
808 wxPrintPreviewBase
*preview
= NULL
;
810 if ( wxTheApp
->GetPrintMode() == wxPRINT_WINDOWS
)
811 preview
= new wxWindowsPrintPreview(printout
, view
->OnCreatePrintout());
814 preview
= new wxPostScriptPrintPreview(printout
, view
->OnCreatePrintout());
816 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, (wxFrame
*)wxTheApp
->GetTopWindow(), "Print Preview",
817 wxPoint(100, 100), wxSize(600, 650));
818 frame
->Centre(wxBOTH
);
824 void wxDocManager::OnUndo(wxCommandEvent
& WXUNUSED(event
))
826 wxDocument
*doc
= GetCurrentDocument();
829 if (doc
->GetCommandProcessor())
830 doc
->GetCommandProcessor()->Undo();
833 void wxDocManager::OnRedo(wxCommandEvent
& WXUNUSED(event
))
835 wxDocument
*doc
= GetCurrentDocument();
838 if (doc
->GetCommandProcessor())
839 doc
->GetCommandProcessor()->Redo();
842 wxView
*wxDocManager::GetCurrentView(void) const
845 return m_currentView
;
846 if (m_docs
.Number() == 1)
848 wxDocument
* doc
= (wxDocument
*) m_docs
.First()->Data();
849 return doc
->GetFirstView();
854 // Extend event processing to search the view's event table
855 bool wxDocManager::ProcessEvent(wxEvent
& event
)
857 wxView
* view
= GetCurrentView();
860 if (view
->ProcessEvent(event
))
863 return wxEvtHandler::ProcessEvent(event
);
866 wxDocument
*wxDocManager::CreateDocument(const wxString
& path
, long flags
)
868 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.Number()];
871 for (i
= 0; i
< m_templates
.Number(); i
++)
873 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Nth(i
)->Data());
874 if (temp
->IsVisible())
886 // If we've reached the max number of docs, close the
888 if (GetDocuments().Number() >= m_maxDocsOpen
)
890 wxDocument
*doc
= (wxDocument
*)GetDocuments().First()->Data();
893 // Implicitly deletes the document when
894 // the last view is deleted
895 doc
->DeleteAllViews();
897 // Check we're really deleted
898 if (m_docs
.Member(doc
))
905 // New document: user chooses a template, unless there's only one.
906 if (flags
& wxDOC_NEW
)
910 wxDocTemplate
*temp
= templates
[0];
912 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
915 newDoc
->SetDocumentName(temp
->GetDocumentName());
916 newDoc
->SetDocumentTemplate(temp
);
917 newDoc
->OnNewDocument();
922 wxDocTemplate
*temp
= SelectDocumentType(templates
, n
);
926 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
929 newDoc
->SetDocumentName(temp
->GetDocumentName());
930 newDoc
->SetDocumentTemplate(temp
);
931 newDoc
->OnNewDocument();
940 wxDocTemplate
*temp
= NULL
;
946 if (flags
& wxDOC_SILENT
)
947 temp
= FindTemplateForPath(path2
);
949 temp
= SelectDocumentPath(templates
, n
, path2
, flags
);
955 wxDocument
*newDoc
= temp
->CreateDocument(path2
, flags
);
958 newDoc
->SetDocumentName(temp
->GetDocumentName());
959 newDoc
->SetDocumentTemplate(temp
);
960 if (!newDoc
->OnOpenDocument(path2
))
965 AddFileToHistory(path2
);
973 wxView
*wxDocManager::CreateView(wxDocument
*doc
, long flags
)
975 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.Number()];
978 for (i
= 0; i
< m_templates
.Number(); i
++)
980 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Nth(i
)->Data());
981 if (temp
->IsVisible())
983 if (temp
->GetDocumentName() == doc
->GetDocumentName())
997 wxDocTemplate
*temp
= templates
[0];
999 wxView
*view
= temp
->CreateView(doc
, flags
);
1001 view
->SetViewName(temp
->GetViewName());
1005 wxDocTemplate
*temp
= SelectViewType(templates
, n
);
1009 wxView
*view
= temp
->CreateView(doc
, flags
);
1011 view
->SetViewName(temp
->GetViewName());
1018 // Not yet implemented
1019 void wxDocManager::DeleteTemplate(wxDocTemplate
*WXUNUSED(temp
), long WXUNUSED(flags
))
1023 // Not yet implemented
1024 bool wxDocManager::FlushDoc(wxDocument
*WXUNUSED(doc
))
1029 wxDocument
*wxDocManager::GetCurrentDocument(void) const
1032 return m_currentView
->GetDocument();
1037 // Make a default document name
1038 bool wxDocManager::MakeDefaultName(wxString
& name
)
1041 sprintf(buf
, "unnamed%d", m_defaultDocumentNameCounter
);
1042 m_defaultDocumentNameCounter
++;
1047 // Not yet implemented
1048 wxDocTemplate
*wxDocManager::MatchTemplate(const wxString
& WXUNUSED(path
))
1053 // File history management
1054 void wxDocManager::AddFileToHistory(const wxString
& file
)
1057 m_fileHistory
->AddFileToHistory(file
);
1060 wxString
wxDocManager::GetHistoryFile(int i
) const
1063 return wxString(m_fileHistory
->GetHistoryFile(i
));
1065 return wxString("");
1068 void wxDocManager::FileHistoryUseMenu(wxMenu
*menu
)
1071 m_fileHistory
->FileHistoryUseMenu(menu
);
1074 void wxDocManager::FileHistoryLoad(const wxString
& resourceFile
, const wxString
& section
)
1077 m_fileHistory
->FileHistoryLoad(resourceFile
, section
);
1080 void wxDocManager::FileHistorySave(const wxString
& resourceFile
, const wxString
& section
)
1083 m_fileHistory
->FileHistorySave(resourceFile
, section
);
1086 int wxDocManager::GetNoHistoryFiles(void) const
1089 return m_fileHistory
->GetNoHistoryFiles();
1094 static char *FindExtension(char *path
)
1096 static char ext
[10];
1097 int len
= strlen(path
);
1101 for (i
= (len
-1); i
> 0; i
--)
1107 for (j
= i
+1; j
< len
; j
++)
1108 ext
[(int)(j
-(i
+1))] = (char)wxToLower(path
[j
]); // NOTE Should not use tolower under UNIX
1119 // Given a path, try to find a matching template. Won't
1120 // always work, of course.
1121 wxDocTemplate
*wxDocManager::FindTemplateForPath(const wxString
& path
)
1123 char *theExt
= FindExtension((char *)(const char *)path
);
1126 wxDocTemplate
*theTemplate
= NULL
;
1128 if (m_templates
.Number() == 1)
1129 return (wxDocTemplate
*)m_templates
.First()->Data();
1131 // Find the template which this extension corresponds to
1133 for (i
= 0; i
< m_templates
.Number(); i
++)
1135 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Nth(i
)->Data();
1136 if (strcmp(temp
->GetDefaultExtension(), theExt
) == 0)
1145 // Prompts user to open a file, using file specs in templates.
1146 // How to implement in wxWindows? Must extend the file selector
1147 // dialog or implement own; OR match the extension to the
1148 // template extension.
1149 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**templates
,
1150 int noTemplates
, wxString
& path
, long WXUNUSED(flags
), bool WXUNUSED(save
))
1152 // We can only have multiple filters in Windows
1154 char *descrBuf
= new char[1000];
1157 for (i
= 0; i
< noTemplates
; i
++)
1159 if (templates
[i
]->IsVisible())
1161 strcat(descrBuf
, templates
[i
]->GetDescription());
1162 strcat(descrBuf
, " (");
1163 strcat(descrBuf
, templates
[i
]->GetFileFilter());
1164 strcat(descrBuf
, ") ");
1165 strcat(descrBuf
, "|");
1166 strcat(descrBuf
, templates
[i
]->GetFileFilter());
1167 strcat(descrBuf
, "|");
1170 int len
= strlen(descrBuf
);
1173 descrBuf
[len
-1] = 0;
1175 char *pathTmp
= wxFileSelector("Select a file", "", "", "", descrBuf
, 0, wxTheApp
->GetTopWindow());
1180 char *theExt
= FindExtension((char *)(const char *)path
);
1184 // This is dodgy in that we're selecting the template on the
1185 // basis of the file extension, which may not be a standard
1186 // one. We really want to know exactly which template was
1187 // chosen by using a more advanced file selector.
1188 wxDocTemplate
*theTemplate
= FindTemplateForPath(path
);
1197 // In all other windowing systems, until we have more advanced
1198 // file selectors, we must select the document type (template) first, and
1199 // _then_ pop up the file selector.
1200 wxDocTemplate
*temp
= SelectDocumentType(templates
, noTemplates
);
1204 char *pathTmp
= wxFileSelector("Select a file", "", "",
1205 temp
->GetDefaultExtension(),
1206 temp
->GetFileFilter(),
1207 0, wxTheApp
->GetTopWindow());
1219 wxDocTemplate
*wxDocManager::SelectDocumentType(wxDocTemplate
**templates
,
1222 char **strings
= new char *[noTemplates
];
1223 char **data
= new char *[noTemplates
];
1226 for (i
= 0; i
< noTemplates
; i
++)
1228 if (templates
[i
]->IsVisible())
1230 strings
[n
] = WXSTRINGCAST templates
[i
]->m_description
;
1231 data
[n
] = (char *)templates
[i
];
1243 wxDocTemplate
*temp
= (wxDocTemplate
*)data
[0];
1249 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData("Select a document template", "Templates", n
,
1256 wxDocTemplate
*wxDocManager::SelectViewType(wxDocTemplate
**templates
,
1259 char **strings
= new char *[noTemplates
];
1260 char **data
= new char *[noTemplates
];
1263 for (i
= 0; i
< noTemplates
; i
++)
1265 if (templates
[i
]->IsVisible() && templates
[i
]->GetViewName())
1267 strings
[n
] = WXSTRINGCAST templates
[i
]->m_viewTypeName
;
1268 data
[n
] = (char *)templates
[i
];
1272 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData("Select a document view", "Views", n
,
1279 void wxDocManager::AssociateTemplate(wxDocTemplate
*temp
)
1281 if (!m_templates
.Member(temp
))
1282 m_templates
.Append(temp
);
1285 void wxDocManager::DisassociateTemplate(wxDocTemplate
*temp
)
1287 m_templates
.DeleteObject(temp
);
1290 // Add and remove a document from the manager's list
1291 void wxDocManager::AddDocument(wxDocument
*doc
)
1293 if (!m_docs
.Member(doc
))
1297 void wxDocManager::RemoveDocument(wxDocument
*doc
)
1299 m_docs
.DeleteObject(doc
);
1302 // Views or windows should inform the document manager
1303 // when a view is going in or out of focus
1304 void wxDocManager::ActivateView(wxView
*view
, bool activate
, bool WXUNUSED(deleting
))
1306 // If we're deactiving, and if we're not actually deleting the view, then
1307 // don't reset the current view because we may be going to
1308 // a window without a view.
1309 // WHAT DID I MEAN BY THAT EXACTLY?
1313 if (m_currentView == view)
1314 m_currentView = NULL;
1320 m_currentView
= view
;
1322 m_currentView
= NULL
;
1327 * Default document child frame
1330 BEGIN_EVENT_TABLE(wxDocChildFrame
, wxFrame
)
1331 EVT_ACTIVATE(wxDocChildFrame::OnActivate
)
1334 wxDocChildFrame::wxDocChildFrame(wxDocument
*doc
, wxView
*view
, wxFrame
*frame
, const wxString
& title
,
1335 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
1336 wxFrame(frame
, -1, title
, pos
, size
, style
, name
)
1338 m_childDocument
= doc
;
1341 view
->SetFrame(this);
1344 wxDocChildFrame::~wxDocChildFrame(void)
1348 // Extend event processing to search the view's event table
1349 bool wxDocChildFrame::ProcessEvent(wxEvent
& event
)
1352 m_childView
->Activate(TRUE
);
1354 if ( !m_childView
|| ! m_childView
->ProcessEvent(event
) )
1356 // Only hand up to the parent if it's a menu command
1357 if (!event
.IsKindOf(CLASSINFO(wxCommandEvent
)) || !GetParent() || !GetParent()->ProcessEvent(event
))
1358 return wxEvtHandler::ProcessEvent(event
);
1366 void wxDocChildFrame::OnActivate(wxActivateEvent
& event
)
1368 wxFrame::OnActivate(event
);
1371 m_childView
->Activate(event
.GetActive());
1374 bool wxDocChildFrame::OnClose(void)
1376 // Close view but don't delete the frame while doing so!
1377 // ...since it will be deleted by wxWindows if we return TRUE.
1380 bool ans
= m_childView
->Close(FALSE
); // FALSE means don't delete associated window
1383 m_childView
->Activate(FALSE
);
1386 m_childDocument
= NULL
;
1395 * Default parent frame
1398 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1399 EVT_MENU(wxID_EXIT
, wxDocParentFrame::OnExit
)
1400 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE2
, wxDocParentFrame::OnMRUFile
)
1403 wxDocParentFrame::wxDocParentFrame(wxDocManager
*manager
, wxFrame
*frame
, const wxString
& title
,
1404 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
1405 wxFrame(frame
, -1, title
, pos
, size
, style
, name
)
1407 m_docManager
= manager
;
1410 void wxDocParentFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
1415 void wxDocParentFrame::OnMRUFile(wxCommandEvent
& event
)
1417 wxString
f(m_docManager
->GetHistoryFile(event
.GetSelection() - wxID_FILE1
));
1419 (void)m_docManager
->CreateDocument(f
, wxDOC_SILENT
);
1422 // Extend event processing to search the view's event table
1423 bool wxDocParentFrame::ProcessEvent(wxEvent
& event
)
1425 // Try the document manager, then do default processing
1426 if (!m_docManager
|| !m_docManager
->ProcessEvent(event
))
1427 return wxEvtHandler::ProcessEvent(event
);
1432 // Define the behaviour for the frame closing
1433 // - must delete all frames except for the main one.
1434 bool wxDocParentFrame::OnClose(void)
1436 return m_docManager
->Clear(FALSE
);
1439 #if USE_PRINTING_ARCHITECTURE
1441 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
):
1442 wxPrintout(WXSTRINGCAST title
)
1444 m_printoutView
= view
;
1447 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
1451 // Get the logical pixels per inch of screen and printer
1452 int ppiScreenX
, ppiScreenY
;
1453 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
1454 int ppiPrinterX
, ppiPrinterY
;
1455 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
1457 // This scales the DC so that the printout roughly represents the
1458 // the screen scaling. The text point size _should_ be the right size
1459 // but in fact is too small for some reason. This is a detail that will
1460 // need to be addressed at some point but can be fudged for the
1462 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
1464 // Now we have to check in case our real page size is reduced
1465 // (e.g. because we're drawing to a print preview memory DC)
1466 int pageWidth
, pageHeight
;
1468 dc
->GetSize(&w
, &h
);
1469 GetPageSizePixels(&pageWidth
, &pageHeight
);
1471 // If printer pageWidth == current DC width, then this doesn't
1472 // change. But w might be the preview bitmap width, so scale down.
1473 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
1474 dc
->SetUserScale(overallScale
, overallScale
);
1478 m_printoutView
->OnDraw(dc
);
1483 bool wxDocPrintout::HasPage(int pageNum
)
1485 return (pageNum
== 1);
1488 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
1490 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
1496 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
1507 * Command processing framework
1510 wxCommand::wxCommand(bool canUndoIt
, const wxString
& name
)
1512 m_canUndo
= canUndoIt
;
1513 m_commandName
= name
;
1516 wxCommand::~wxCommand(void)
1520 // Command processor
1521 wxCommandProcessor::wxCommandProcessor(int maxCommands
)
1523 m_maxNoCommands
= maxCommands
;
1524 m_currentCommand
= NULL
;
1525 m_commandEditMenu
= NULL
;
1528 wxCommandProcessor::~wxCommandProcessor(void)
1533 // Pass a command to the processor. The processor calls Do();
1534 // if successful, is appended to the command history unless
1535 // storeIt is FALSE.
1536 bool wxCommandProcessor::Submit(wxCommand
*command
, bool storeIt
)
1538 bool success
= command
->Do();
1539 if (success
&& storeIt
)
1541 if (m_commands
.Number() == m_maxNoCommands
)
1543 wxNode
*firstNode
= m_commands
.First();
1544 wxCommand
*firstCommand
= (wxCommand
*)firstNode
->Data();
1545 delete firstCommand
;
1549 // Correct a bug: we must chop off the current 'branch'
1550 // so that we're at the end of the command list.
1551 if (!m_currentCommand
)
1555 wxNode
*node
= m_currentCommand
->Next();
1558 wxNode
*next
= node
->Next();
1559 delete (wxCommand
*)node
->Data();
1565 m_commands
.Append(command
);
1566 m_currentCommand
= m_commands
.Last();
1572 bool wxCommandProcessor::Undo(void)
1574 if (m_currentCommand
)
1576 wxCommand
*command
= (wxCommand
*)m_currentCommand
->Data();
1577 if (command
->CanUndo())
1579 bool success
= command
->Undo();
1582 m_currentCommand
= m_currentCommand
->Previous();
1591 bool wxCommandProcessor::Redo(void)
1593 wxCommand
*redoCommand
= NULL
;
1594 wxNode
*redoNode
= NULL
;
1595 if (m_currentCommand
&& m_currentCommand
->Next())
1597 redoCommand
= (wxCommand
*)m_currentCommand
->Next()->Data();
1598 redoNode
= m_currentCommand
->Next();
1602 if (m_commands
.Number() > 0)
1604 redoCommand
= (wxCommand
*)m_commands
.First()->Data();
1605 redoNode
= m_commands
.First();
1611 bool success
= redoCommand
->Do();
1614 m_currentCommand
= redoNode
;
1622 bool wxCommandProcessor::CanUndo(void)
1624 if (m_currentCommand
)
1625 return ((wxCommand
*)m_currentCommand
->Data())->CanUndo();
1629 void wxCommandProcessor::Initialize(void)
1631 m_currentCommand
= m_commands
.Last();
1635 void wxCommandProcessor::SetMenuStrings(void)
1637 if (m_commandEditMenu
)
1640 if (m_currentCommand
)
1642 wxCommand
*command
= (wxCommand
*)m_currentCommand
->Data();
1643 wxString
commandName(command
->GetName());
1644 if (commandName
== "") commandName
= "Unnamed command";
1645 bool canUndo
= command
->CanUndo();
1647 buf
= wxString("&Undo ") + commandName
;
1649 buf
= wxString("Can't &Undo ") + commandName
;
1651 m_commandEditMenu
->SetLabel(wxID_UNDO
, buf
);
1652 m_commandEditMenu
->Enable(wxID_UNDO
, canUndo
);
1654 // We can redo, if we're not at the end of the history.
1655 if (m_currentCommand
->Next())
1657 wxCommand
*redoCommand
= (wxCommand
*)m_currentCommand
->Next()->Data();
1658 wxString
redoCommandName(redoCommand
->GetName());
1659 if (redoCommandName
== "") redoCommandName
= "Unnamed command";
1660 buf
= wxString("&Redo ") + redoCommandName
;
1661 m_commandEditMenu
->SetLabel(wxID_REDO
, buf
);
1662 m_commandEditMenu
->Enable(wxID_REDO
, TRUE
);
1666 m_commandEditMenu
->SetLabel(wxID_REDO
, "&Redo");
1667 m_commandEditMenu
->Enable(wxID_REDO
, FALSE
);
1672 m_commandEditMenu
->SetLabel(wxID_UNDO
, "&Undo");
1673 m_commandEditMenu
->Enable(wxID_UNDO
, FALSE
);
1675 if (m_commands
.Number() == 0)
1677 m_commandEditMenu
->SetLabel(wxID_REDO
, "&Redo");
1678 m_commandEditMenu
->Enable(wxID_REDO
, FALSE
);
1682 // currentCommand is NULL but there are commands: this means that
1683 // we've undone to the start of the list, but can redo the first.
1684 wxCommand
*redoCommand
= (wxCommand
*)m_commands
.First()->Data();
1685 wxString
redoCommandName(redoCommand
->GetName());
1686 if (!redoCommandName
) redoCommandName
= "Unnamed command";
1687 buf
= wxString("&Redo ") + redoCommandName
;
1688 m_commandEditMenu
->SetLabel(wxID_REDO
, buf
);
1689 m_commandEditMenu
->Enable(wxID_REDO
, TRUE
);
1695 void wxCommandProcessor::ClearCommands(void)
1697 wxNode
*node
= m_commands
.First();
1700 wxCommand
*command
= (wxCommand
*)node
->Data();
1703 node
= m_commands
.First();
1705 m_currentCommand
= NULL
;
1710 * File history processor
1713 wxFileHistory::wxFileHistory(int maxFiles
)
1715 m_fileMaxFiles
= maxFiles
;
1718 m_fileHistory
= new char *[m_fileMaxFiles
];
1721 wxFileHistory::~wxFileHistory(void)
1724 for (i
= 0; i
< m_fileHistoryN
; i
++)
1725 delete[] m_fileHistory
[i
];
1726 delete[] m_fileHistory
;
1729 // File history management
1730 void wxFileHistory::AddFileToHistory(const wxString
& file
)
1737 // Check we don't already have this file
1738 for (i
= 0; i
< m_fileHistoryN
; i
++)
1740 if (m_fileHistory
[i
] && wxString(m_fileHistory
[i
]) == file
)
1744 // Add to the project file history:
1745 // Move existing files (if any) down so we can insert file at beginning.
1747 // First delete filename that has popped off the end of the array (if any)
1748 if (m_fileHistoryN
== m_fileMaxFiles
)
1750 delete[] m_fileHistory
[m_fileMaxFiles
-1];
1751 m_fileHistory
[m_fileMaxFiles
-1] = NULL
;
1753 if (m_fileHistoryN
< m_fileMaxFiles
)
1755 if (m_fileHistoryN
== 0)
1756 m_fileMenu
->AppendSeparator();
1757 m_fileMenu
->Append(wxID_FILE1
+m_fileHistoryN
, "[EMPTY]");
1760 // Shuffle filenames down
1761 for (i
= (m_fileHistoryN
-1); i
> 0; i
--)
1763 m_fileHistory
[i
] = m_fileHistory
[i
-1];
1765 m_fileHistory
[0] = copystring(file
);
1767 for (i
= 0; i
< m_fileHistoryN
; i
++)
1768 if (m_fileHistory
[i
])
1771 sprintf(buf
, "&%d %s", i
+1, m_fileHistory
[i
]);
1772 m_fileMenu
->SetLabel(wxID_FILE1
+i
, buf
);
1776 wxString
wxFileHistory::GetHistoryFile(int i
) const
1778 if (i
< m_fileHistoryN
)
1779 return wxString(m_fileHistory
[i
]);
1781 return wxString("");
1784 void wxFileHistory::FileHistoryUseMenu(wxMenu
*menu
)
1789 void wxFileHistory::FileHistoryLoad(const wxString
& resourceFile
, const wxString
& section
)
1794 sprintf(buf
, "file%d", m_fileHistoryN
+1);
1795 char *historyFile
= NULL
;
1796 while ((m_fileHistoryN
<= m_fileMaxFiles
) && wxGetResource(section
, buf
, &historyFile
, resourceFile
) && historyFile
)
1798 // wxGetResource allocates memory so this is o.k.
1799 m_fileHistory
[m_fileHistoryN
] = historyFile
;
1801 sprintf(buf
, "file%d", m_fileHistoryN
+1);
1807 void wxFileHistory::FileHistorySave(const wxString
& resourceFile
, const wxString
& section
)
1812 for (i
= 0; i
< m_fileHistoryN
; i
++)
1814 sprintf(buf
, "file%d", i
+1);
1815 wxWriteResource(section
, buf
, m_fileHistory
[i
], resourceFile
);
1825 wxPrintInfo::wxPrintInfo(void)
1830 wxPrintInfo::~wxPrintInfo(void)
1836 * Permits compatibility with existing file formats and functions
1837 * that manipulate files directly
1840 bool wxTransferFileToStream(const wxString
& filename
, ostream
& stream
)
1845 if ((fd1
= fopen (WXSTRINGCAST filename
, "rb")) == NULL
)
1848 while ((ch
= getc (fd1
)) != EOF
)
1849 stream
<< (unsigned char)ch
;
1855 bool wxTransferStreamToFile(istream
& stream
, const wxString
& filename
)
1860 if ((fd1
= fopen (WXSTRINGCAST filename
, "wb")) == NULL
)
1865 while (!stream
.eof())
1876 // End USE_DOC_VIEW_ARCHITECTURE