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 wxDocument
*wxDocManager::CreateDocument(const wxString
& path
, long flags
)
844 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.Number()];
847 for (i
= 0; i
< m_templates
.Number(); i
++)
849 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Nth(i
)->Data());
850 if (temp
->IsVisible())
862 // If we've reached the max number of docs, close the
864 if (GetDocuments().Number() >= m_maxDocsOpen
)
866 wxDocument
*doc
= (wxDocument
*)GetDocuments().First()->Data();
869 // Implicitly deletes the document when
870 // the last view is deleted
871 doc
->DeleteAllViews();
873 // Check we're really deleted
874 if (m_docs
.Member(doc
))
881 // New document: user chooses a template, unless there's only one.
882 if (flags
& wxDOC_NEW
)
886 wxDocTemplate
*temp
= templates
[0];
888 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
891 newDoc
->SetDocumentName(temp
->GetDocumentName());
892 newDoc
->SetDocumentTemplate(temp
);
893 newDoc
->OnNewDocument();
898 wxDocTemplate
*temp
= SelectDocumentType(templates
, n
);
902 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
905 newDoc
->SetDocumentName(temp
->GetDocumentName());
906 newDoc
->SetDocumentTemplate(temp
);
907 newDoc
->OnNewDocument();
916 wxDocTemplate
*temp
= NULL
;
922 if (flags
& wxDOC_SILENT
)
923 temp
= FindTemplateForPath(path2
);
925 temp
= SelectDocumentPath(templates
, n
, path2
, flags
);
931 wxDocument
*newDoc
= temp
->CreateDocument(path2
, flags
);
934 newDoc
->SetDocumentName(temp
->GetDocumentName());
935 newDoc
->SetDocumentTemplate(temp
);
936 if (!newDoc
->OnOpenDocument(path2
))
941 AddFileToHistory(path2
);
949 wxView
*wxDocManager::CreateView(wxDocument
*doc
, long flags
)
951 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.Number()];
954 for (i
= 0; i
< m_templates
.Number(); i
++)
956 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Nth(i
)->Data());
957 if (temp
->IsVisible())
959 if (temp
->GetDocumentName() == doc
->GetDocumentName())
973 wxDocTemplate
*temp
= templates
[0];
975 wxView
*view
= temp
->CreateView(doc
, flags
);
977 view
->SetViewName(temp
->GetViewName());
981 wxDocTemplate
*temp
= SelectViewType(templates
, n
);
985 wxView
*view
= temp
->CreateView(doc
, flags
);
987 view
->SetViewName(temp
->GetViewName());
994 // Not yet implemented
995 void wxDocManager::DeleteTemplate(wxDocTemplate
*WXUNUSED(temp
), long WXUNUSED(flags
))
999 // Not yet implemented
1000 bool wxDocManager::FlushDoc(wxDocument
*WXUNUSED(doc
))
1005 wxDocument
*wxDocManager::GetCurrentDocument(void) const
1008 return m_currentView
->GetDocument();
1013 // Make a default document name
1014 bool wxDocManager::MakeDefaultName(wxString
& name
)
1017 sprintf(buf
, "unnamed%d", m_defaultDocumentNameCounter
);
1018 m_defaultDocumentNameCounter
++;
1023 // Not yet implemented
1024 wxDocTemplate
*wxDocManager::MatchTemplate(const wxString
& WXUNUSED(path
))
1029 // File history management
1030 void wxDocManager::AddFileToHistory(const wxString
& file
)
1033 m_fileHistory
->AddFileToHistory(file
);
1036 wxString
wxDocManager::GetHistoryFile(int i
) const
1039 return wxString(m_fileHistory
->GetHistoryFile(i
));
1041 return wxString("");
1044 void wxDocManager::FileHistoryUseMenu(wxMenu
*menu
)
1047 m_fileHistory
->FileHistoryUseMenu(menu
);
1050 void wxDocManager::FileHistoryLoad(const wxString
& resourceFile
, const wxString
& section
)
1053 m_fileHistory
->FileHistoryLoad(resourceFile
, section
);
1056 void wxDocManager::FileHistorySave(const wxString
& resourceFile
, const wxString
& section
)
1059 m_fileHistory
->FileHistorySave(resourceFile
, section
);
1062 int wxDocManager::GetNoHistoryFiles(void) const
1065 return m_fileHistory
->GetNoHistoryFiles();
1070 static char *FindExtension(char *path
)
1072 static char ext
[10];
1073 int len
= strlen(path
);
1077 for (i
= (len
-1); i
> 0; i
--)
1083 for (j
= i
+1; j
< len
; j
++)
1084 ext
[(int)(j
-(i
+1))] = (char)wxToLower(path
[j
]); // NOTE Should not use tolower under UNIX
1095 // Given a path, try to find a matching template. Won't
1096 // always work, of course.
1097 wxDocTemplate
*wxDocManager::FindTemplateForPath(const wxString
& path
)
1099 char *theExt
= FindExtension((char *)(const char *)path
);
1102 wxDocTemplate
*theTemplate
= NULL
;
1104 if (m_templates
.Number() == 1)
1105 return (wxDocTemplate
*)m_templates
.First()->Data();
1107 // Find the template which this extension corresponds to
1109 for (i
= 0; i
< m_templates
.Number(); i
++)
1111 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Nth(i
)->Data();
1112 if (strcmp(temp
->GetDefaultExtension(), theExt
) == 0)
1121 // Prompts user to open a file, using file specs in templates.
1122 // How to implement in wxWindows? Must extend the file selector
1123 // dialog or implement own; OR match the extension to the
1124 // template extension.
1125 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**templates
,
1126 int noTemplates
, wxString
& path
, long WXUNUSED(flags
), bool WXUNUSED(save
))
1128 // We can only have multiple filters in Windows
1130 char *descrBuf
= new char[1000];
1133 for (i
= 0; i
< noTemplates
; i
++)
1135 if (templates
[i
]->IsVisible())
1137 strcat(descrBuf
, templates
[i
]->GetDescription());
1138 strcat(descrBuf
, " (");
1139 strcat(descrBuf
, templates
[i
]->GetFileFilter());
1140 strcat(descrBuf
, ") ");
1141 strcat(descrBuf
, "|");
1142 strcat(descrBuf
, templates
[i
]->GetFileFilter());
1143 strcat(descrBuf
, "|");
1146 int len
= strlen(descrBuf
);
1149 descrBuf
[len
-1] = 0;
1151 char *pathTmp
= wxFileSelector("Select a file", "", "", "", descrBuf
, 0, wxTheApp
->GetTopWindow());
1156 char *theExt
= FindExtension((char *)(const char *)path
);
1160 // This is dodgy in that we're selecting the template on the
1161 // basis of the file extension, which may not be a standard
1162 // one. We really want to know exactly which template was
1163 // chosen by using a more advanced file selector.
1164 wxDocTemplate
*theTemplate
= FindTemplateForPath(path
);
1173 // In all other windowing systems, until we have more advanced
1174 // file selectors, we must select the document type (template) first, and
1175 // _then_ pop up the file selector.
1176 wxDocTemplate
*temp
= SelectDocumentType(templates
, noTemplates
);
1180 char *pathTmp
= wxFileSelector("Select a file", "", "",
1181 temp
->GetDefaultExtension(),
1182 temp
->GetFileFilter(),
1183 0, wxTheApp
->GetTopWindow());
1195 wxDocTemplate
*wxDocManager::SelectDocumentType(wxDocTemplate
**templates
,
1198 char **strings
= new char *[noTemplates
];
1199 char **data
= new char *[noTemplates
];
1202 for (i
= 0; i
< noTemplates
; i
++)
1204 if (templates
[i
]->IsVisible())
1206 strings
[n
] = WXSTRINGCAST templates
[i
]->m_description
;
1207 data
[n
] = (char *)templates
[i
];
1219 wxDocTemplate
*temp
= (wxDocTemplate
*)data
[0];
1225 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData("Select a document template", "Templates", n
,
1232 wxDocTemplate
*wxDocManager::SelectViewType(wxDocTemplate
**templates
,
1235 char **strings
= new char *[noTemplates
];
1236 char **data
= new char *[noTemplates
];
1239 for (i
= 0; i
< noTemplates
; i
++)
1241 if (templates
[i
]->IsVisible() && templates
[i
]->GetViewName())
1243 strings
[n
] = WXSTRINGCAST templates
[i
]->m_viewTypeName
;
1244 data
[n
] = (char *)templates
[i
];
1248 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData("Select a document view", "Views", n
,
1255 void wxDocManager::AssociateTemplate(wxDocTemplate
*temp
)
1257 if (!m_templates
.Member(temp
))
1258 m_templates
.Append(temp
);
1261 void wxDocManager::DisassociateTemplate(wxDocTemplate
*temp
)
1263 m_templates
.DeleteObject(temp
);
1266 // Add and remove a document from the manager's list
1267 void wxDocManager::AddDocument(wxDocument
*doc
)
1269 if (!m_docs
.Member(doc
))
1273 void wxDocManager::RemoveDocument(wxDocument
*doc
)
1275 m_docs
.DeleteObject(doc
);
1278 // Views or windows should inform the document manager
1279 // when a view is going in or out of focus
1280 void wxDocManager::ActivateView(wxView
*view
, bool activate
, bool WXUNUSED(deleting
))
1282 // If we're deactiving, and if we're not actually deleting the view, then
1283 // don't reset the current view because we may be going to
1284 // a window without a view.
1285 // WHAT DID I MEAN BY THAT EXACTLY?
1289 if (m_currentView == view)
1290 m_currentView = NULL;
1296 m_currentView
= view
;
1298 m_currentView
= NULL
;
1303 * Default document child frame
1306 BEGIN_EVENT_TABLE(wxDocChildFrame
, wxFrame
)
1307 EVT_ACTIVATE(wxDocChildFrame::OnActivate
)
1310 wxDocChildFrame::wxDocChildFrame(wxDocument
*doc
, wxView
*view
, wxFrame
*frame
, const wxString
& title
,
1311 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
1312 wxFrame(frame
, -1, title
, pos
, size
, style
, name
)
1314 m_childDocument
= doc
;
1317 view
->SetFrame(this);
1320 wxDocChildFrame::~wxDocChildFrame(void)
1324 // Extend event processing to search the view's event table
1325 bool wxDocChildFrame::ProcessEvent(wxEvent
& event
)
1328 m_childView
->Activate(TRUE
);
1330 if ( !m_childView
|| ! m_childView
->ProcessEvent(event
) )
1332 // Only hand up to the parent if it's a menu command
1333 if (!event
.IsKindOf(CLASSINFO(wxCommandEvent
)) || !GetParent() || !GetParent()->ProcessEvent(event
))
1334 return wxEvtHandler::ProcessEvent(event
);
1342 void wxDocChildFrame::OnActivate(wxActivateEvent
& event
)
1344 wxFrame::OnActivate(event
);
1347 m_childView
->Activate(event
.GetActive());
1350 bool wxDocChildFrame::OnClose(void)
1352 // Close view but don't delete the frame while doing so!
1353 // ...since it will be deleted by wxWindows if we return TRUE.
1356 bool ans
= m_childView
->Close(FALSE
); // FALSE means don't delete associated window
1359 m_childView
->Activate(FALSE
);
1362 m_childDocument
= NULL
;
1371 * Default parent frame
1374 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1375 EVT_MENU(wxID_EXIT
, wxDocParentFrame::OnExit
)
1376 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE2
, wxDocParentFrame::OnMRUFile
)
1379 wxDocParentFrame::wxDocParentFrame(wxDocManager
*manager
, wxFrame
*frame
, const wxString
& title
,
1380 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
1381 wxFrame(frame
, -1, title
, pos
, size
, style
, name
)
1383 m_docManager
= manager
;
1386 void wxDocParentFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
1391 void wxDocParentFrame::OnMRUFile(wxCommandEvent
& event
)
1393 wxString
f(m_docManager
->GetHistoryFile(event
.GetSelection() - wxID_FILE1
));
1395 (void)m_docManager
->CreateDocument(f
, wxDOC_SILENT
);
1398 // Extend event processing to search the view's event table
1399 bool wxDocParentFrame::ProcessEvent(wxEvent
& event
)
1401 // Try the document manager, then do default processing
1402 if (!m_docManager
|| !m_docManager
->ProcessEvent(event
))
1403 return wxEvtHandler::ProcessEvent(event
);
1408 // Define the behaviour for the frame closing
1409 // - must delete all frames except for the main one.
1410 bool wxDocParentFrame::OnClose(void)
1412 return m_docManager
->Clear(FALSE
);
1415 #if USE_PRINTING_ARCHITECTURE
1417 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
):
1418 wxPrintout(WXSTRINGCAST title
)
1420 m_printoutView
= view
;
1423 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
1427 // Get the logical pixels per inch of screen and printer
1428 int ppiScreenX
, ppiScreenY
;
1429 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
1430 int ppiPrinterX
, ppiPrinterY
;
1431 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
1433 // This scales the DC so that the printout roughly represents the
1434 // the screen scaling. The text point size _should_ be the right size
1435 // but in fact is too small for some reason. This is a detail that will
1436 // need to be addressed at some point but can be fudged for the
1438 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
1440 // Now we have to check in case our real page size is reduced
1441 // (e.g. because we're drawing to a print preview memory DC)
1442 int pageWidth
, pageHeight
;
1444 dc
->GetSize(&w
, &h
);
1445 GetPageSizePixels(&pageWidth
, &pageHeight
);
1447 // If printer pageWidth == current DC width, then this doesn't
1448 // change. But w might be the preview bitmap width, so scale down.
1449 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
1450 dc
->SetUserScale(overallScale
, overallScale
);
1454 m_printoutView
->OnDraw(dc
);
1459 bool wxDocPrintout::HasPage(int pageNum
)
1461 return (pageNum
== 1);
1464 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
1466 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
1472 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
1483 * Command processing framework
1486 wxCommand::wxCommand(bool canUndoIt
, const wxString
& name
)
1488 m_canUndo
= canUndoIt
;
1489 m_commandName
= name
;
1492 wxCommand::~wxCommand(void)
1496 // Command processor
1497 wxCommandProcessor::wxCommandProcessor(int maxCommands
)
1499 m_maxNoCommands
= maxCommands
;
1500 m_currentCommand
= NULL
;
1501 m_commandEditMenu
= NULL
;
1504 wxCommandProcessor::~wxCommandProcessor(void)
1509 // Pass a command to the processor. The processor calls Do();
1510 // if successful, is appended to the command history unless
1511 // storeIt is FALSE.
1512 bool wxCommandProcessor::Submit(wxCommand
*command
, bool storeIt
)
1514 bool success
= command
->Do();
1515 if (success
&& storeIt
)
1517 if (m_commands
.Number() == m_maxNoCommands
)
1519 wxNode
*firstNode
= m_commands
.First();
1520 wxCommand
*firstCommand
= (wxCommand
*)firstNode
->Data();
1521 delete firstCommand
;
1525 // Correct a bug: we must chop off the current 'branch'
1526 // so that we're at the end of the command list.
1527 if (!m_currentCommand
)
1531 wxNode
*node
= m_currentCommand
->Next();
1534 wxNode
*next
= node
->Next();
1535 delete (wxCommand
*)node
->Data();
1541 m_commands
.Append(command
);
1542 m_currentCommand
= m_commands
.Last();
1548 bool wxCommandProcessor::Undo(void)
1550 if (m_currentCommand
)
1552 wxCommand
*command
= (wxCommand
*)m_currentCommand
->Data();
1553 if (command
->CanUndo())
1555 bool success
= command
->Undo();
1558 m_currentCommand
= m_currentCommand
->Previous();
1567 bool wxCommandProcessor::Redo(void)
1569 wxCommand
*redoCommand
= NULL
;
1570 wxNode
*redoNode
= NULL
;
1571 if (m_currentCommand
&& m_currentCommand
->Next())
1573 redoCommand
= (wxCommand
*)m_currentCommand
->Next()->Data();
1574 redoNode
= m_currentCommand
->Next();
1578 if (m_commands
.Number() > 0)
1580 redoCommand
= (wxCommand
*)m_commands
.First()->Data();
1581 redoNode
= m_commands
.First();
1587 bool success
= redoCommand
->Do();
1590 m_currentCommand
= redoNode
;
1598 bool wxCommandProcessor::CanUndo(void)
1600 if (m_currentCommand
)
1601 return ((wxCommand
*)m_currentCommand
->Data())->CanUndo();
1605 void wxCommandProcessor::Initialize(void)
1607 m_currentCommand
= m_commands
.Last();
1611 void wxCommandProcessor::SetMenuStrings(void)
1613 if (m_commandEditMenu
)
1616 if (m_currentCommand
)
1618 wxCommand
*command
= (wxCommand
*)m_currentCommand
->Data();
1619 wxString
commandName(command
->GetName());
1620 if (commandName
== "") commandName
= "Unnamed command";
1621 bool canUndo
= command
->CanUndo();
1623 buf
= wxString("&Undo ") + commandName
;
1625 buf
= wxString("Can't &Undo ") + commandName
;
1627 m_commandEditMenu
->SetLabel(wxID_UNDO
, buf
);
1628 m_commandEditMenu
->Enable(wxID_UNDO
, canUndo
);
1630 // We can redo, if we're not at the end of the history.
1631 if (m_currentCommand
->Next())
1633 wxCommand
*redoCommand
= (wxCommand
*)m_currentCommand
->Next()->Data();
1634 wxString
redoCommandName(redoCommand
->GetName());
1635 if (redoCommandName
== "") redoCommandName
= "Unnamed command";
1636 buf
= wxString("&Redo ") + redoCommandName
;
1637 m_commandEditMenu
->SetLabel(wxID_REDO
, buf
);
1638 m_commandEditMenu
->Enable(wxID_REDO
, TRUE
);
1642 m_commandEditMenu
->SetLabel(wxID_REDO
, "&Redo");
1643 m_commandEditMenu
->Enable(wxID_REDO
, FALSE
);
1648 m_commandEditMenu
->SetLabel(wxID_UNDO
, "&Undo");
1649 m_commandEditMenu
->Enable(wxID_UNDO
, FALSE
);
1651 if (m_commands
.Number() == 0)
1653 m_commandEditMenu
->SetLabel(wxID_REDO
, "&Redo");
1654 m_commandEditMenu
->Enable(wxID_REDO
, FALSE
);
1658 // currentCommand is NULL but there are commands: this means that
1659 // we've undone to the start of the list, but can redo the first.
1660 wxCommand
*redoCommand
= (wxCommand
*)m_commands
.First()->Data();
1661 wxString
redoCommandName(redoCommand
->GetName());
1662 if (!redoCommandName
) redoCommandName
= "Unnamed command";
1663 buf
= wxString("&Redo ") + redoCommandName
;
1664 m_commandEditMenu
->SetLabel(wxID_REDO
, buf
);
1665 m_commandEditMenu
->Enable(wxID_REDO
, TRUE
);
1671 void wxCommandProcessor::ClearCommands(void)
1673 wxNode
*node
= m_commands
.First();
1676 wxCommand
*command
= (wxCommand
*)node
->Data();
1679 node
= m_commands
.First();
1681 m_currentCommand
= NULL
;
1686 * File history processor
1689 wxFileHistory::wxFileHistory(int maxFiles
)
1691 m_fileMaxFiles
= maxFiles
;
1694 m_fileHistory
= new char *[m_fileMaxFiles
];
1697 wxFileHistory::~wxFileHistory(void)
1700 for (i
= 0; i
< m_fileHistoryN
; i
++)
1701 delete[] m_fileHistory
[i
];
1702 delete[] m_fileHistory
;
1705 // File history management
1706 void wxFileHistory::AddFileToHistory(const wxString
& file
)
1713 // Check we don't already have this file
1714 for (i
= 0; i
< m_fileHistoryN
; i
++)
1716 if (m_fileHistory
[i
] && wxString(m_fileHistory
[i
]) == file
)
1720 // Add to the project file history:
1721 // Move existing files (if any) down so we can insert file at beginning.
1723 // First delete filename that has popped off the end of the array (if any)
1724 if (m_fileHistoryN
== m_fileMaxFiles
)
1726 delete[] m_fileHistory
[m_fileMaxFiles
-1];
1727 m_fileHistory
[m_fileMaxFiles
-1] = NULL
;
1729 if (m_fileHistoryN
< m_fileMaxFiles
)
1731 if (m_fileHistoryN
== 0)
1732 m_fileMenu
->AppendSeparator();
1733 m_fileMenu
->Append(wxID_FILE1
+m_fileHistoryN
, "[EMPTY]");
1736 // Shuffle filenames down
1737 for (i
= (m_fileHistoryN
-1); i
> 0; i
--)
1739 m_fileHistory
[i
] = m_fileHistory
[i
-1];
1741 m_fileHistory
[0] = copystring(file
);
1743 for (i
= 0; i
< m_fileHistoryN
; i
++)
1744 if (m_fileHistory
[i
])
1747 sprintf(buf
, "&%d %s", i
+1, m_fileHistory
[i
]);
1748 m_fileMenu
->SetLabel(wxID_FILE1
+i
, buf
);
1752 wxString
wxFileHistory::GetHistoryFile(int i
) const
1754 if (i
< m_fileHistoryN
)
1755 return wxString(m_fileHistory
[i
]);
1757 return wxString("");
1760 void wxFileHistory::FileHistoryUseMenu(wxMenu
*menu
)
1765 void wxFileHistory::FileHistoryLoad(const wxString
& resourceFile
, const wxString
& section
)
1770 sprintf(buf
, "file%d", m_fileHistoryN
+1);
1771 char *historyFile
= NULL
;
1772 while ((m_fileHistoryN
<= m_fileMaxFiles
) && wxGetResource(section
, buf
, &historyFile
, resourceFile
) && historyFile
)
1774 // wxGetResource allocates memory so this is o.k.
1775 m_fileHistory
[m_fileHistoryN
] = historyFile
;
1777 sprintf(buf
, "file%d", m_fileHistoryN
+1);
1783 void wxFileHistory::FileHistorySave(const wxString
& resourceFile
, const wxString
& section
)
1788 for (i
= 0; i
< m_fileHistoryN
; i
++)
1790 sprintf(buf
, "file%d", i
+1);
1791 wxWriteResource(section
, buf
, m_fileHistory
[i
], resourceFile
);
1801 wxPrintInfo::wxPrintInfo(void)
1806 wxPrintInfo::~wxPrintInfo(void)
1812 * Permits compatibility with existing file formats and functions
1813 * that manipulate files directly
1816 bool wxTransferFileToStream(const wxString
& filename
, ostream
& stream
)
1821 if ((fd1
= fopen (WXSTRINGCAST filename
, "rb")) == NULL
)
1824 while ((ch
= getc (fd1
)) != EOF
)
1825 stream
<< (unsigned char)ch
;
1831 bool wxTransferStreamToFile(istream
& stream
, const wxString
& filename
)
1836 if ((fd1
= fopen (WXSTRINGCAST filename
, "wb")) == NULL
)
1841 while (!stream
.eof())
1852 // End USE_DOC_VIEW_ARCHITECTURE