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
, const 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
);
1343 // Intercept menu commands
1344 void wxDocChildFrame::OldOnMenuCommand(int id)
1347 m_childView->Activate(TRUE);
1350 ((wxFrame *)GetParent())->OldOnMenuCommand(id);
1354 void wxDocChildFrame::OnActivate(wxActivateEvent
& event
)
1356 wxFrame::OnActivate(event
);
1359 m_childView
->Activate(event
.GetActive());
1362 bool wxDocChildFrame::OnClose(void)
1364 // Close view but don't delete the frame while doing so!
1365 // ...since it will be deleted by wxWindows if we return TRUE.
1368 bool ans
= m_childView
->Close(FALSE
); // FALSE means don't delete associated window
1371 m_childView
->Activate(FALSE
);
1374 m_childDocument
= NULL
;
1383 * Default parent frame
1386 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1387 EVT_MENU(wxID_EXIT
, wxDocParentFrame::OnExit
)
1388 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE2
, wxDocParentFrame::OnMRUFile
)
1391 wxDocParentFrame::wxDocParentFrame(wxDocManager
*manager
, wxFrame
*frame
, const wxString
& title
,
1392 const wxPoint
& pos
, const wxSize
& size
, const long style
, const wxString
& name
):
1393 wxFrame(frame
, -1, title
, pos
, size
, style
, name
)
1395 m_docManager
= manager
;
1398 void wxDocParentFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
1403 void wxDocParentFrame::OnMRUFile(wxCommandEvent
& event
)
1405 wxString
f(m_docManager
->GetHistoryFile(event
.GetSelection() - wxID_FILE1
));
1407 (void)m_docManager
->CreateDocument(f
, wxDOC_SILENT
);
1410 // Extend event processing to search the view's event table
1411 bool wxDocParentFrame::ProcessEvent(wxEvent
& event
)
1413 // Try the document manager, then do default processing
1414 if (!m_docManager
|| !m_docManager
->ProcessEvent(event
))
1415 return wxEvtHandler::ProcessEvent(event
);
1421 // Intercept menu commands
1422 void wxDocParentFrame::OldOnMenuCommand(int id)
1441 wxString f(m_docManager->GetHistoryFile(id-wxID_FILE1));
1443 (void)m_docManager->CreateDocument(f, wxDOC_SILENT);
1448 m_docManager->OldOnMenuCommand(id);
1454 // Define the behaviour for the frame closing
1455 // - must delete all frames except for the main one.
1456 bool wxDocParentFrame::OnClose(void)
1458 return m_docManager
->Clear(FALSE
);
1461 #if USE_PRINTING_ARCHITECTURE
1463 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
):
1464 wxPrintout(WXSTRINGCAST title
)
1466 m_printoutView
= view
;
1469 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
1473 // Get the logical pixels per inch of screen and printer
1474 int ppiScreenX
, ppiScreenY
;
1475 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
1476 int ppiPrinterX
, ppiPrinterY
;
1477 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
1479 // This scales the DC so that the printout roughly represents the
1480 // the screen scaling. The text point size _should_ be the right size
1481 // but in fact is too small for some reason. This is a detail that will
1482 // need to be addressed at some point but can be fudged for the
1484 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
1486 // Now we have to check in case our real page size is reduced
1487 // (e.g. because we're drawing to a print preview memory DC)
1488 int pageWidth
, pageHeight
;
1490 dc
->GetSize(&w
, &h
);
1491 GetPageSizePixels(&pageWidth
, &pageHeight
);
1493 // If printer pageWidth == current DC width, then this doesn't
1494 // change. But w might be the preview bitmap width, so scale down.
1495 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
1496 dc
->SetUserScale(overallScale
, overallScale
);
1500 m_printoutView
->OnDraw(dc
);
1505 bool wxDocPrintout::HasPage(int pageNum
)
1507 return (pageNum
== 1);
1510 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
1512 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
1518 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
1529 * Command processing framework
1532 wxCommand::wxCommand(bool canUndoIt
, const wxString
& name
)
1534 m_canUndo
= canUndoIt
;
1535 m_commandName
= name
;
1538 wxCommand::~wxCommand(void)
1542 // Command processor
1543 wxCommandProcessor::wxCommandProcessor(int maxCommands
)
1545 m_maxNoCommands
= maxCommands
;
1546 m_currentCommand
= NULL
;
1547 m_commandEditMenu
= NULL
;
1550 wxCommandProcessor::~wxCommandProcessor(void)
1555 // Pass a command to the processor. The processor calls Do();
1556 // if successful, is appended to the command history unless
1557 // storeIt is FALSE.
1558 bool wxCommandProcessor::Submit(wxCommand
*command
, bool storeIt
)
1560 bool success
= command
->Do();
1561 if (success
&& storeIt
)
1563 if (m_commands
.Number() == m_maxNoCommands
)
1565 wxNode
*firstNode
= m_commands
.First();
1566 wxCommand
*firstCommand
= (wxCommand
*)firstNode
->Data();
1567 delete firstCommand
;
1571 // Correct a bug: we must chop off the current 'branch'
1572 // so that we're at the end of the command list.
1573 if (!m_currentCommand
)
1577 wxNode
*node
= m_currentCommand
->Next();
1580 wxNode
*next
= node
->Next();
1581 delete (wxCommand
*)node
->Data();
1587 m_commands
.Append(command
);
1588 m_currentCommand
= m_commands
.Last();
1594 bool wxCommandProcessor::Undo(void)
1596 if (m_currentCommand
)
1598 wxCommand
*command
= (wxCommand
*)m_currentCommand
->Data();
1599 if (command
->CanUndo())
1601 bool success
= command
->Undo();
1604 m_currentCommand
= m_currentCommand
->Previous();
1613 bool wxCommandProcessor::Redo(void)
1615 wxCommand
*redoCommand
= NULL
;
1616 wxNode
*redoNode
= NULL
;
1617 if (m_currentCommand
&& m_currentCommand
->Next())
1619 redoCommand
= (wxCommand
*)m_currentCommand
->Next()->Data();
1620 redoNode
= m_currentCommand
->Next();
1624 if (m_commands
.Number() > 0)
1626 redoCommand
= (wxCommand
*)m_commands
.First()->Data();
1627 redoNode
= m_commands
.First();
1633 bool success
= redoCommand
->Do();
1636 m_currentCommand
= redoNode
;
1644 bool wxCommandProcessor::CanUndo(void)
1646 if (m_currentCommand
)
1647 return ((wxCommand
*)m_currentCommand
->Data())->CanUndo();
1651 void wxCommandProcessor::Initialize(void)
1653 m_currentCommand
= m_commands
.Last();
1657 void wxCommandProcessor::SetMenuStrings(void)
1659 if (m_commandEditMenu
)
1662 if (m_currentCommand
)
1664 wxCommand
*command
= (wxCommand
*)m_currentCommand
->Data();
1665 wxString
commandName(command
->GetName());
1666 if (commandName
== "") commandName
= "Unnamed command";
1667 bool canUndo
= command
->CanUndo();
1669 buf
= wxString("&Undo ") + commandName
;
1671 buf
= wxString("Can't &Undo ") + commandName
;
1673 m_commandEditMenu
->SetLabel(wxID_UNDO
, buf
);
1674 m_commandEditMenu
->Enable(wxID_UNDO
, canUndo
);
1676 // We can redo, if we're not at the end of the history.
1677 if (m_currentCommand
->Next())
1679 wxCommand
*redoCommand
= (wxCommand
*)m_currentCommand
->Next()->Data();
1680 wxString
redoCommandName(redoCommand
->GetName());
1681 if (redoCommandName
== "") redoCommandName
= "Unnamed command";
1682 buf
= wxString("&Redo ") + redoCommandName
;
1683 m_commandEditMenu
->SetLabel(wxID_REDO
, buf
);
1684 m_commandEditMenu
->Enable(wxID_REDO
, TRUE
);
1688 m_commandEditMenu
->SetLabel(wxID_REDO
, "&Redo");
1689 m_commandEditMenu
->Enable(wxID_REDO
, FALSE
);
1694 m_commandEditMenu
->SetLabel(wxID_UNDO
, "&Undo");
1695 m_commandEditMenu
->Enable(wxID_UNDO
, FALSE
);
1697 if (m_commands
.Number() == 0)
1699 m_commandEditMenu
->SetLabel(wxID_REDO
, "&Redo");
1700 m_commandEditMenu
->Enable(wxID_REDO
, FALSE
);
1704 // currentCommand is NULL but there are commands: this means that
1705 // we've undone to the start of the list, but can redo the first.
1706 wxCommand
*redoCommand
= (wxCommand
*)m_commands
.First()->Data();
1707 wxString
redoCommandName(redoCommand
->GetName());
1708 if (!redoCommandName
) redoCommandName
= "Unnamed command";
1709 buf
= wxString("&Redo ") + redoCommandName
;
1710 m_commandEditMenu
->SetLabel(wxID_REDO
, buf
);
1711 m_commandEditMenu
->Enable(wxID_REDO
, TRUE
);
1717 void wxCommandProcessor::ClearCommands(void)
1719 wxNode
*node
= m_commands
.First();
1722 wxCommand
*command
= (wxCommand
*)node
->Data();
1725 node
= m_commands
.First();
1727 m_currentCommand
= NULL
;
1732 * File history processor
1735 wxFileHistory::wxFileHistory(int maxFiles
)
1737 m_fileMaxFiles
= maxFiles
;
1740 m_fileHistory
= new char *[m_fileMaxFiles
];
1743 wxFileHistory::~wxFileHistory(void)
1746 for (i
= 0; i
< m_fileHistoryN
; i
++)
1747 delete[] m_fileHistory
[i
];
1748 delete[] m_fileHistory
;
1751 // File history management
1752 void wxFileHistory::AddFileToHistory(const wxString
& file
)
1759 // Check we don't already have this file
1760 for (i
= 0; i
< m_fileHistoryN
; i
++)
1762 if (m_fileHistory
[i
] && wxString(m_fileHistory
[i
]) == file
)
1766 // Add to the project file history:
1767 // Move existing files (if any) down so we can insert file at beginning.
1769 // First delete filename that has popped off the end of the array (if any)
1770 if (m_fileHistoryN
== m_fileMaxFiles
)
1772 delete[] m_fileHistory
[m_fileMaxFiles
-1];
1773 m_fileHistory
[m_fileMaxFiles
-1] = NULL
;
1775 if (m_fileHistoryN
< m_fileMaxFiles
)
1777 if (m_fileHistoryN
== 0)
1778 m_fileMenu
->AppendSeparator();
1779 m_fileMenu
->Append(wxID_FILE1
+m_fileHistoryN
, "[EMPTY]");
1782 // Shuffle filenames down
1783 for (i
= (m_fileHistoryN
-1); i
> 0; i
--)
1785 m_fileHistory
[i
] = m_fileHistory
[i
-1];
1787 m_fileHistory
[0] = copystring(file
);
1789 for (i
= 0; i
< m_fileHistoryN
; i
++)
1790 if (m_fileHistory
[i
])
1793 sprintf(buf
, "&%d %s", i
+1, m_fileHistory
[i
]);
1794 m_fileMenu
->SetLabel(wxID_FILE1
+i
, buf
);
1798 wxString
wxFileHistory::GetHistoryFile(int i
) const
1800 if (i
< m_fileHistoryN
)
1801 return wxString(m_fileHistory
[i
]);
1803 return wxString("");
1806 void wxFileHistory::FileHistoryUseMenu(wxMenu
*menu
)
1811 void wxFileHistory::FileHistoryLoad(const wxString
& resourceFile
, const wxString
& section
)
1816 sprintf(buf
, "file%d", m_fileHistoryN
+1);
1817 char *historyFile
= NULL
;
1818 while ((m_fileHistoryN
<= m_fileMaxFiles
) && wxGetResource(section
, buf
, &historyFile
, resourceFile
) && historyFile
)
1820 // wxGetResource allocates memory so this is o.k.
1821 m_fileHistory
[m_fileHistoryN
] = historyFile
;
1823 sprintf(buf
, "file%d", m_fileHistoryN
+1);
1829 void wxFileHistory::FileHistorySave(const wxString
& resourceFile
, const wxString
& section
)
1834 for (i
= 0; i
< m_fileHistoryN
; i
++)
1836 sprintf(buf
, "file%d", i
+1);
1837 wxWriteResource(section
, buf
, m_fileHistory
[i
], resourceFile
);
1847 wxPrintInfo::wxPrintInfo(void)
1852 wxPrintInfo::~wxPrintInfo(void)
1858 * Permits compatibility with existing file formats and functions
1859 * that manipulate files directly
1862 bool wxTransferFileToStream(const wxString
& filename
, ostream
& stream
)
1867 if ((fd1
= fopen (WXSTRINGCAST filename
, "rb")) == NULL
)
1870 while ((ch
= getc (fd1
)) != EOF
)
1871 stream
<< (unsigned char)ch
;
1877 bool wxTransferStreamToFile(istream
& stream
, const wxString
& filename
)
1882 if ((fd1
= fopen (WXSTRINGCAST filename
, "wb")) == NULL
)
1887 while (!stream
.eof())
1898 // End USE_DOC_VIEW_ARCHITECTURE