Fix crash in docview code if view initialization failed.
[wxWidgets.git] / src / common / docview.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/docview.cpp
3 // Purpose: Document/view classes
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_DOC_VIEW_ARCHITECTURE
28
29 #include "wx/docview.h"
30
31 #ifndef WX_PRECOMP
32 #include "wx/list.h"
33 #include "wx/string.h"
34 #include "wx/utils.h"
35 #include "wx/app.h"
36 #include "wx/dc.h"
37 #include "wx/dialog.h"
38 #include "wx/menu.h"
39 #include "wx/filedlg.h"
40 #include "wx/intl.h"
41 #include "wx/log.h"
42 #include "wx/msgdlg.h"
43 #include "wx/mdi.h"
44 #include "wx/choicdlg.h"
45 #endif
46
47 #if wxUSE_PRINTING_ARCHITECTURE
48 #include "wx/prntbase.h"
49 #include "wx/printdlg.h"
50 #endif
51
52 #include "wx/confbase.h"
53 #include "wx/filename.h"
54 #include "wx/file.h"
55 #include "wx/ffile.h"
56 #include "wx/cmdproc.h"
57 #include "wx/tokenzr.h"
58 #include "wx/filename.h"
59 #include "wx/stdpaths.h"
60 #include "wx/vector.h"
61 #include "wx/scopedarray.h"
62 #include "wx/scopedptr.h"
63 #include "wx/scopeguard.h"
64 #include "wx/except.h"
65
66 #if wxUSE_STD_IOSTREAM
67 #include "wx/ioswrap.h"
68 #include "wx/beforestd.h"
69 #if wxUSE_IOSTREAMH
70 #include <fstream.h>
71 #else
72 #include <fstream>
73 #endif
74 #include "wx/afterstd.h"
75 #else
76 #include "wx/wfstream.h"
77 #endif
78
79 // ----------------------------------------------------------------------------
80 // wxWidgets macros
81 // ----------------------------------------------------------------------------
82
83 IMPLEMENT_ABSTRACT_CLASS(wxDocument, wxEvtHandler)
84 IMPLEMENT_ABSTRACT_CLASS(wxView, wxEvtHandler)
85 IMPLEMENT_ABSTRACT_CLASS(wxDocTemplate, wxObject)
86 IMPLEMENT_DYNAMIC_CLASS(wxDocManager, wxEvtHandler)
87 IMPLEMENT_CLASS(wxDocChildFrame, wxFrame)
88 IMPLEMENT_CLASS(wxDocParentFrame, wxFrame)
89
90 #if wxUSE_PRINTING_ARCHITECTURE
91 IMPLEMENT_DYNAMIC_CLASS(wxDocPrintout, wxPrintout)
92 #endif
93
94 // ============================================================================
95 // implementation
96 // ============================================================================
97
98 // ----------------------------------------------------------------------------
99 // private helpers
100 // ----------------------------------------------------------------------------
101
102 namespace
103 {
104
105 wxString FindExtension(const wxString& path)
106 {
107 wxString ext;
108 wxFileName::SplitPath(path, NULL, NULL, &ext);
109
110 // VZ: extensions are considered not case sensitive - is this really a good
111 // idea?
112 return ext.MakeLower();
113 }
114
115 } // anonymous namespace
116
117 // ----------------------------------------------------------------------------
118 // Definition of wxDocument
119 // ----------------------------------------------------------------------------
120
121 wxDocument::wxDocument(wxDocument *parent)
122 {
123 m_documentModified = false;
124 m_documentTemplate = NULL;
125
126 m_documentParent = parent;
127 if ( parent )
128 parent->m_childDocuments.push_back(this);
129
130 m_commandProcessor = NULL;
131 m_savedYet = false;
132 }
133
134 bool wxDocument::DeleteContents()
135 {
136 return true;
137 }
138
139 wxDocument::~wxDocument()
140 {
141 delete m_commandProcessor;
142
143 if (GetDocumentManager())
144 GetDocumentManager()->RemoveDocument(this);
145
146 if ( m_documentParent )
147 m_documentParent->m_childDocuments.remove(this);
148
149 // Not safe to do here, since it'll invoke virtual view functions
150 // expecting to see valid derived objects: and by the time we get here,
151 // we've called destructors higher up.
152 //DeleteAllViews();
153 }
154
155 bool wxDocument::Close()
156 {
157 if ( !OnSaveModified() )
158 return false;
159
160 // When the parent document closes, its children must be closed as well as
161 // they can't exist without the parent.
162
163 // As usual, first check if all children can be closed.
164 DocsList::const_iterator it = m_childDocuments.begin();
165 for ( DocsList::const_iterator end = m_childDocuments.end(); it != end; ++it )
166 {
167 if ( !(*it)->OnSaveModified() )
168 {
169 // Leave the parent document opened if a child can't close.
170 return false;
171 }
172 }
173
174 // Now that they all did, do close them: as m_childDocuments is modified as
175 // we iterate over it, don't use the usual for-style iteration here.
176 while ( !m_childDocuments.empty() )
177 {
178 wxDocument * const childDoc = m_childDocuments.front();
179
180 // This will call OnSaveModified() once again but it shouldn't do
181 // anything as the document was just saved or marked as not needing to
182 // be saved by the call to OnSaveModified() that returned true above.
183 if ( !childDoc->Close() )
184 {
185 wxFAIL_MSG( "Closing the child document unexpectedly failed "
186 "after its OnSaveModified() returned true" );
187 }
188
189 // Delete the child document by deleting all its views.
190 childDoc->DeleteAllViews();
191 }
192
193
194 return OnCloseDocument();
195 }
196
197 bool wxDocument::OnCloseDocument()
198 {
199 // Tell all views that we're about to close
200 NotifyClosing();
201 DeleteContents();
202 Modify(false);
203 return true;
204 }
205
206 // Note that this implicitly deletes the document when the last view is
207 // deleted.
208 bool wxDocument::DeleteAllViews()
209 {
210 wxDocManager* manager = GetDocumentManager();
211
212 // first check if all views agree to be closed
213 const wxList::iterator end = m_documentViews.end();
214 for ( wxList::iterator i = m_documentViews.begin(); i != end; ++i )
215 {
216 wxView *view = (wxView *)*i;
217 if ( !view->Close() )
218 return false;
219 }
220
221 // all views agreed to close, now do close them
222 if ( m_documentViews.empty() )
223 {
224 // normally the document would be implicitly deleted when the last view
225 // is, but if don't have any views, do it here instead
226 if ( manager && manager->GetDocuments().Member(this) )
227 delete this;
228 }
229 else // have views
230 {
231 // as we delete elements we iterate over, don't use the usual "from
232 // begin to end" loop
233 for ( ;; )
234 {
235 wxView *view = (wxView *)*m_documentViews.begin();
236
237 bool isLastOne = m_documentViews.size() == 1;
238
239 // this always deletes the node implicitly and if this is the last
240 // view also deletes this object itself (also implicitly, great),
241 // so we can't test for m_documentViews.empty() after calling this!
242 delete view;
243
244 if ( isLastOne )
245 break;
246 }
247 }
248
249 return true;
250 }
251
252 wxView *wxDocument::GetFirstView() const
253 {
254 if ( m_documentViews.empty() )
255 return NULL;
256
257 return static_cast<wxView *>(m_documentViews.GetFirst()->GetData());
258 }
259
260 void wxDocument::Modify(bool mod)
261 {
262 if (mod != m_documentModified)
263 {
264 m_documentModified = mod;
265
266 // Allow views to append asterix to the title
267 wxView* view = GetFirstView();
268 if (view) view->OnChangeFilename();
269 }
270 }
271
272 wxDocManager *wxDocument::GetDocumentManager() const
273 {
274 // For child documents we use the same document manager as the parent, even
275 // though we don't have our own template (as children are not opened/saved
276 // directly).
277 if ( m_documentParent )
278 return m_documentParent->GetDocumentManager();
279
280 return m_documentTemplate ? m_documentTemplate->GetDocumentManager() : NULL;
281 }
282
283 bool wxDocument::OnNewDocument()
284 {
285 // notice that there is no need to neither reset nor even check the
286 // modified flag here as the document itself is a new object (this is only
287 // called from CreateDocument()) and so it shouldn't be saved anyhow even
288 // if it is modified -- this could happen if the user code creates
289 // documents pre-filled with some user-entered (and which hence must not be
290 // lost) information
291
292 SetDocumentSaved(false);
293
294 const wxString name = GetDocumentManager()->MakeNewDocumentName();
295 SetTitle(name);
296 SetFilename(name, true);
297
298 return true;
299 }
300
301 bool wxDocument::Save()
302 {
303 if ( AlreadySaved() )
304 return true;
305
306 if ( m_documentFile.empty() || !m_savedYet )
307 return SaveAs();
308
309 return OnSaveDocument(m_documentFile);
310 }
311
312 bool wxDocument::SaveAs()
313 {
314 wxDocTemplate *docTemplate = GetDocumentTemplate();
315 if (!docTemplate)
316 return false;
317
318 #ifdef wxHAS_MULTIPLE_FILEDLG_FILTERS
319 wxString filter = docTemplate->GetDescription() + wxT(" (") +
320 docTemplate->GetFileFilter() + wxT(")|") +
321 docTemplate->GetFileFilter();
322
323 // Now see if there are some other template with identical view and document
324 // classes, whose filters may also be used.
325 if (docTemplate->GetViewClassInfo() && docTemplate->GetDocClassInfo())
326 {
327 wxList::compatibility_iterator
328 node = docTemplate->GetDocumentManager()->GetTemplates().GetFirst();
329 while (node)
330 {
331 wxDocTemplate *t = (wxDocTemplate*) node->GetData();
332
333 if (t->IsVisible() && t != docTemplate &&
334 t->GetViewClassInfo() == docTemplate->GetViewClassInfo() &&
335 t->GetDocClassInfo() == docTemplate->GetDocClassInfo())
336 {
337 // add a '|' to separate this filter from the previous one
338 if ( !filter.empty() )
339 filter << wxT('|');
340
341 filter << t->GetDescription()
342 << wxT(" (") << t->GetFileFilter() << wxT(") |")
343 << t->GetFileFilter();
344 }
345
346 node = node->GetNext();
347 }
348 }
349 #else
350 wxString filter = docTemplate->GetFileFilter() ;
351 #endif
352
353 wxString defaultDir = docTemplate->GetDirectory();
354 if ( defaultDir.empty() )
355 {
356 defaultDir = wxPathOnly(GetFilename());
357 if ( defaultDir.empty() )
358 defaultDir = GetDocumentManager()->GetLastDirectory();
359 }
360
361 wxString fileName = wxFileSelector(_("Save As"),
362 defaultDir,
363 wxFileNameFromPath(GetFilename()),
364 docTemplate->GetDefaultExtension(),
365 filter,
366 wxFD_SAVE | wxFD_OVERWRITE_PROMPT,
367 GetDocumentWindow());
368
369 if (fileName.empty())
370 return false; // cancelled by user
371
372 // Files that were not saved correctly are not added to the FileHistory.
373 if (!OnSaveDocument(fileName))
374 return false;
375
376 SetTitle(wxFileNameFromPath(fileName));
377 SetFilename(fileName, true); // will call OnChangeFileName automatically
378
379 // A file that doesn't use the default extension of its document template
380 // cannot be opened via the FileHistory, so we do not add it.
381 if (docTemplate->FileMatchesTemplate(fileName))
382 {
383 GetDocumentManager()->AddFileToHistory(fileName);
384 }
385 //else: the user will probably not be able to open the file again, so we
386 // could warn about the wrong file-extension here
387
388 return true;
389 }
390
391 bool wxDocument::OnSaveDocument(const wxString& file)
392 {
393 if ( !file )
394 return false;
395
396 if ( !DoSaveDocument(file) )
397 return false;
398
399 if ( m_commandProcessor )
400 m_commandProcessor->MarkAsSaved();
401
402 Modify(false);
403 SetFilename(file);
404 SetDocumentSaved(true);
405 #if defined( __WXOSX_MAC__ ) && wxOSX_USE_CARBON
406 wxFileName fn(file) ;
407 fn.MacSetDefaultTypeAndCreator() ;
408 #endif
409 return true;
410 }
411
412 bool wxDocument::OnOpenDocument(const wxString& file)
413 {
414 // notice that there is no need to check the modified flag here for the
415 // reasons explained in OnNewDocument()
416
417 if ( !DoOpenDocument(file) )
418 return false;
419
420 SetFilename(file, true);
421
422 // stretching the logic a little this does make sense because the document
423 // had been saved into the file we just loaded it from, it just could have
424 // happened during a previous program execution, it's just that the name of
425 // this method is a bit unfortunate, it should probably have been called
426 // HasAssociatedFileName()
427 SetDocumentSaved(true);
428
429 UpdateAllViews();
430
431 return true;
432 }
433
434 #if wxUSE_STD_IOSTREAM
435 wxSTD istream& wxDocument::LoadObject(wxSTD istream& stream)
436 #else
437 wxInputStream& wxDocument::LoadObject(wxInputStream& stream)
438 #endif
439 {
440 return stream;
441 }
442
443 #if wxUSE_STD_IOSTREAM
444 wxSTD ostream& wxDocument::SaveObject(wxSTD ostream& stream)
445 #else
446 wxOutputStream& wxDocument::SaveObject(wxOutputStream& stream)
447 #endif
448 {
449 return stream;
450 }
451
452 bool wxDocument::Revert()
453 {
454 if ( wxMessageBox
455 (
456 _("Discard changes and reload the last saved version?"),
457 wxTheApp->GetAppDisplayName(),
458 wxYES_NO | wxCANCEL | wxICON_QUESTION,
459 GetDocumentWindow()
460 ) != wxYES )
461 return false;
462
463 if ( !DoOpenDocument(GetFilename()) )
464 return false;
465
466 Modify(false);
467 UpdateAllViews();
468
469 return true;
470 }
471
472
473 // Get title, or filename if no title, else unnamed
474 #if WXWIN_COMPATIBILITY_2_8
475 bool wxDocument::GetPrintableName(wxString& buf) const
476 {
477 // this function cannot only be overridden by the user code but also
478 // called by it so we need to ensure that we return the same thing as
479 // GetUserReadableName() but we can't call it because this would result in
480 // an infinite recursion, hence we use the helper DoGetUserReadableName()
481 buf = DoGetUserReadableName();
482
483 return true;
484 }
485 #endif // WXWIN_COMPATIBILITY_2_8
486
487 wxString wxDocument::GetUserReadableName() const
488 {
489 #if WXWIN_COMPATIBILITY_2_8
490 // we need to call the old virtual function to ensure that the overridden
491 // version of it is still called
492 wxString name;
493 if ( GetPrintableName(name) )
494 return name;
495 #endif // WXWIN_COMPATIBILITY_2_8
496
497 return DoGetUserReadableName();
498 }
499
500 wxString wxDocument::DoGetUserReadableName() const
501 {
502 if ( !m_documentTitle.empty() )
503 return m_documentTitle;
504
505 if ( !m_documentFile.empty() )
506 return wxFileNameFromPath(m_documentFile);
507
508 return _("unnamed");
509 }
510
511 wxWindow *wxDocument::GetDocumentWindow() const
512 {
513 wxView * const view = GetFirstView();
514
515 return view ? view->GetFrame() : wxTheApp->GetTopWindow();
516 }
517
518 wxCommandProcessor *wxDocument::OnCreateCommandProcessor()
519 {
520 return new wxCommandProcessor;
521 }
522
523 // true if safe to close
524 bool wxDocument::OnSaveModified()
525 {
526 if ( IsModified() )
527 {
528 switch ( wxMessageBox
529 (
530 wxString::Format
531 (
532 _("Do you want to save changes to %s?"),
533 GetUserReadableName()
534 ),
535 wxTheApp->GetAppDisplayName(),
536 wxYES_NO | wxCANCEL | wxICON_QUESTION | wxCENTRE
537 ) )
538 {
539 case wxNO:
540 Modify(false);
541 break;
542
543 case wxYES:
544 return Save();
545
546 case wxCANCEL:
547 return false;
548 }
549 }
550
551 return true;
552 }
553
554 bool wxDocument::Draw(wxDC& WXUNUSED(context))
555 {
556 return true;
557 }
558
559 bool wxDocument::AddView(wxView *view)
560 {
561 if ( !m_documentViews.Member(view) )
562 {
563 m_documentViews.Append(view);
564 OnChangedViewList();
565 }
566 return true;
567 }
568
569 bool wxDocument::RemoveView(wxView *view)
570 {
571 (void)m_documentViews.DeleteObject(view);
572 OnChangedViewList();
573 return true;
574 }
575
576 bool wxDocument::OnCreate(const wxString& WXUNUSED(path), long flags)
577 {
578 return GetDocumentTemplate()->CreateView(this, flags) != NULL;
579 }
580
581 // Called after a view is added or removed.
582 // The default implementation deletes the document if
583 // there are no more views.
584 void wxDocument::OnChangedViewList()
585 {
586 if ( m_documentViews.empty() && OnSaveModified() )
587 delete this;
588 }
589
590 void wxDocument::UpdateAllViews(wxView *sender, wxObject *hint)
591 {
592 wxList::compatibility_iterator node = m_documentViews.GetFirst();
593 while (node)
594 {
595 wxView *view = (wxView *)node->GetData();
596 if (view != sender)
597 view->OnUpdate(sender, hint);
598 node = node->GetNext();
599 }
600 }
601
602 void wxDocument::NotifyClosing()
603 {
604 wxList::compatibility_iterator node = m_documentViews.GetFirst();
605 while (node)
606 {
607 wxView *view = (wxView *)node->GetData();
608 view->OnClosingDocument();
609 node = node->GetNext();
610 }
611 }
612
613 void wxDocument::SetFilename(const wxString& filename, bool notifyViews)
614 {
615 m_documentFile = filename;
616 OnChangeFilename(notifyViews);
617 }
618
619 void wxDocument::OnChangeFilename(bool notifyViews)
620 {
621 if ( notifyViews )
622 {
623 // Notify the views that the filename has changed
624 wxList::compatibility_iterator node = m_documentViews.GetFirst();
625 while (node)
626 {
627 wxView *view = (wxView *)node->GetData();
628 view->OnChangeFilename();
629 node = node->GetNext();
630 }
631 }
632 }
633
634 bool wxDocument::DoSaveDocument(const wxString& file)
635 {
636 #if wxUSE_STD_IOSTREAM
637 wxSTD ofstream store(file.mb_str(), wxSTD ios::binary);
638 if ( !store )
639 #else
640 wxFileOutputStream store(file);
641 if ( store.GetLastError() != wxSTREAM_NO_ERROR )
642 #endif
643 {
644 wxLogError(_("File \"%s\" could not be opened for writing."), file);
645 return false;
646 }
647
648 if (!SaveObject(store))
649 {
650 wxLogError(_("Failed to save document to the file \"%s\"."), file);
651 return false;
652 }
653
654 return true;
655 }
656
657 bool wxDocument::DoOpenDocument(const wxString& file)
658 {
659 #if wxUSE_STD_IOSTREAM
660 wxSTD ifstream store(file.mb_str(), wxSTD ios::binary);
661 if ( !store )
662 #else
663 wxFileInputStream store(file);
664 if (store.GetLastError() != wxSTREAM_NO_ERROR || !store.IsOk())
665 #endif
666 {
667 wxLogError(_("File \"%s\" could not be opened for reading."), file);
668 return false;
669 }
670
671 #if wxUSE_STD_IOSTREAM
672 LoadObject(store);
673 if ( !store )
674 #else
675 int res = LoadObject(store).GetLastError();
676 if ( res != wxSTREAM_NO_ERROR && res != wxSTREAM_EOF )
677 #endif
678 {
679 wxLogError(_("Failed to read document from the file \"%s\"."), file);
680 return false;
681 }
682
683 return true;
684 }
685
686
687 // ----------------------------------------------------------------------------
688 // Document view
689 // ----------------------------------------------------------------------------
690
691 wxView::wxView()
692 {
693 m_viewDocument = NULL;
694
695 m_viewFrame = NULL;
696
697 m_docChildFrame = NULL;
698 }
699
700 wxView::~wxView()
701 {
702 if (m_viewDocument && GetDocumentManager())
703 GetDocumentManager()->ActivateView(this, false);
704
705 // reset our frame view first, before removing it from the document as
706 // SetView(NULL) is a simple call while RemoveView() may result in user
707 // code being executed and this user code can, for example, show a message
708 // box which would result in an activation event for m_docChildFrame and so
709 // could reactivate the view being destroyed -- unless we reset it first
710 if ( m_docChildFrame && m_docChildFrame->GetView() == this )
711 {
712 // prevent it from doing anything with us
713 m_docChildFrame->SetView(NULL);
714
715 // it doesn't make sense to leave the frame alive if its associated
716 // view doesn't exist any more so unconditionally close it as well
717 //
718 // notice that we only get here if m_docChildFrame is non-NULL in the
719 // first place and it will be always NULL if we're deleted because our
720 // frame was closed, so this only catches the case of directly deleting
721 // the view, as it happens if its creation fails in wxDocTemplate::
722 // CreateView() for example
723 m_docChildFrame->GetWindow()->Destroy();
724 }
725
726 if ( m_viewDocument )
727 m_viewDocument->RemoveView(this);
728 }
729
730 void wxView::SetDocChildFrame(wxDocChildFrameAnyBase *docChildFrame)
731 {
732 SetFrame(docChildFrame ? docChildFrame->GetWindow() : NULL);
733 m_docChildFrame = docChildFrame;
734 }
735
736 bool wxView::TryBefore(wxEvent& event)
737 {
738 wxDocument * const doc = GetDocument();
739 return doc && doc->ProcessEventLocally(event);
740 }
741
742 void wxView::OnActivateView(bool WXUNUSED(activate),
743 wxView *WXUNUSED(activeView),
744 wxView *WXUNUSED(deactiveView))
745 {
746 }
747
748 void wxView::OnPrint(wxDC *dc, wxObject *WXUNUSED(info))
749 {
750 OnDraw(dc);
751 }
752
753 void wxView::OnUpdate(wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint))
754 {
755 }
756
757 void wxView::OnChangeFilename()
758 {
759 // GetFrame can return wxWindow rather than wxTopLevelWindow due to
760 // generic MDI implementation so use SetLabel rather than SetTitle.
761 // It should cause SetTitle() for top level windows.
762 wxWindow *win = GetFrame();
763 if (!win) return;
764
765 wxDocument *doc = GetDocument();
766 if (!doc) return;
767
768 wxString label = doc->GetUserReadableName();
769 if (doc->IsModified())
770 {
771 label += "*";
772 }
773 win->SetLabel(label);
774 }
775
776 void wxView::SetDocument(wxDocument *doc)
777 {
778 m_viewDocument = doc;
779 if (doc)
780 doc->AddView(this);
781 }
782
783 bool wxView::Close(bool deleteWindow)
784 {
785 return OnClose(deleteWindow);
786 }
787
788 void wxView::Activate(bool activate)
789 {
790 if (GetDocument() && GetDocumentManager())
791 {
792 OnActivateView(activate, this, GetDocumentManager()->GetCurrentView());
793 GetDocumentManager()->ActivateView(this, activate);
794 }
795 }
796
797 bool wxView::OnClose(bool WXUNUSED(deleteWindow))
798 {
799 return GetDocument() ? GetDocument()->Close() : true;
800 }
801
802 #if wxUSE_PRINTING_ARCHITECTURE
803 wxPrintout *wxView::OnCreatePrintout()
804 {
805 return new wxDocPrintout(this);
806 }
807 #endif // wxUSE_PRINTING_ARCHITECTURE
808
809 // ----------------------------------------------------------------------------
810 // wxDocTemplate
811 // ----------------------------------------------------------------------------
812
813 wxDocTemplate::wxDocTemplate(wxDocManager *manager,
814 const wxString& descr,
815 const wxString& filter,
816 const wxString& dir,
817 const wxString& ext,
818 const wxString& docTypeName,
819 const wxString& viewTypeName,
820 wxClassInfo *docClassInfo,
821 wxClassInfo *viewClassInfo,
822 long flags)
823 {
824 m_documentManager = manager;
825 m_description = descr;
826 m_directory = dir;
827 m_defaultExt = ext;
828 m_fileFilter = filter;
829 m_flags = flags;
830 m_docTypeName = docTypeName;
831 m_viewTypeName = viewTypeName;
832 m_documentManager->AssociateTemplate(this);
833
834 m_docClassInfo = docClassInfo;
835 m_viewClassInfo = viewClassInfo;
836 }
837
838 wxDocTemplate::~wxDocTemplate()
839 {
840 m_documentManager->DisassociateTemplate(this);
841 }
842
843 // Tries to dynamically construct an object of the right class.
844 wxDocument *wxDocTemplate::CreateDocument(const wxString& path, long flags)
845 {
846 // InitDocument() is supposed to delete the document object if its
847 // initialization fails so don't use wxScopedPtr<> here: this is fragile
848 // but unavoidable because the default implementation uses CreateView()
849 // which may -- or not -- create a wxView and if it does create it and its
850 // initialization fails then the view destructor will delete the document
851 // (via RemoveView()) and as we can't distinguish between the two cases we
852 // just have to assume that it always deletes it in case of failure
853 wxDocument * const doc = DoCreateDocument();
854
855 return doc && InitDocument(doc, path, flags) ? doc : NULL;
856 }
857
858 bool
859 wxDocTemplate::InitDocument(wxDocument* doc, const wxString& path, long flags)
860 {
861 // Normally, if wxDocument::OnCreate() fails, it happens because the view
862 // initialization fails and then the document is destroyed due to the
863 // destruction of its last view. But take into account the (currently
864 // unrealized, AFAICS) possibility of other failures as well and ensure
865 // that the document is always destroyed if it can't be initialized.
866 wxTRY
867 {
868 doc->SetFilename(path);
869 doc->SetDocumentTemplate(this);
870 GetDocumentManager()->AddDocument(doc);
871 doc->SetCommandProcessor(doc->OnCreateCommandProcessor());
872
873 return doc->OnCreate(path, flags);
874 }
875 wxCATCH_ALL(
876 if ( GetDocumentManager()->GetDocuments().Member(doc) )
877 doc->DeleteAllViews();
878 throw;
879 )
880 }
881
882 wxView *wxDocTemplate::CreateView(wxDocument *doc, long flags)
883 {
884 wxScopedPtr<wxView> view(DoCreateView());
885 if ( !view )
886 return NULL;
887
888 view->SetDocument(doc);
889 if ( !view->OnCreate(doc, flags) )
890 return NULL;
891
892 return view.release();
893 }
894
895 // The default (very primitive) format detection: check is the extension is
896 // that of the template
897 bool wxDocTemplate::FileMatchesTemplate(const wxString& path)
898 {
899 wxStringTokenizer parser (GetFileFilter(), wxT(";"));
900 wxString anything = wxT ("*");
901 while (parser.HasMoreTokens())
902 {
903 wxString filter = parser.GetNextToken();
904 wxString filterExt = FindExtension (filter);
905 if ( filter.IsSameAs (anything) ||
906 filterExt.IsSameAs (anything) ||
907 filterExt.IsSameAs (FindExtension (path)) )
908 return true;
909 }
910 return GetDefaultExtension().IsSameAs(FindExtension(path));
911 }
912
913 wxDocument *wxDocTemplate::DoCreateDocument()
914 {
915 if (!m_docClassInfo)
916 return NULL;
917
918 return static_cast<wxDocument *>(m_docClassInfo->CreateObject());
919 }
920
921 wxView *wxDocTemplate::DoCreateView()
922 {
923 if (!m_viewClassInfo)
924 return NULL;
925
926 return static_cast<wxView *>(m_viewClassInfo->CreateObject());
927 }
928
929 // ----------------------------------------------------------------------------
930 // wxDocManager
931 // ----------------------------------------------------------------------------
932
933 BEGIN_EVENT_TABLE(wxDocManager, wxEvtHandler)
934 EVT_MENU(wxID_OPEN, wxDocManager::OnFileOpen)
935 EVT_MENU(wxID_CLOSE, wxDocManager::OnFileClose)
936 EVT_MENU(wxID_CLOSE_ALL, wxDocManager::OnFileCloseAll)
937 EVT_MENU(wxID_REVERT, wxDocManager::OnFileRevert)
938 EVT_MENU(wxID_NEW, wxDocManager::OnFileNew)
939 EVT_MENU(wxID_SAVE, wxDocManager::OnFileSave)
940 EVT_MENU(wxID_SAVEAS, wxDocManager::OnFileSaveAs)
941 EVT_MENU(wxID_UNDO, wxDocManager::OnUndo)
942 EVT_MENU(wxID_REDO, wxDocManager::OnRedo)
943
944 // We don't know in advance how many items can there be in the MRU files
945 // list so set up OnMRUFile() as a handler for all menu events and do the
946 // check for the id of the menu item clicked inside it.
947 EVT_MENU(wxID_ANY, wxDocManager::OnMRUFile)
948
949 EVT_UPDATE_UI(wxID_OPEN, wxDocManager::OnUpdateFileOpen)
950 EVT_UPDATE_UI(wxID_CLOSE, wxDocManager::OnUpdateDisableIfNoDoc)
951 EVT_UPDATE_UI(wxID_CLOSE_ALL, wxDocManager::OnUpdateDisableIfNoDoc)
952 EVT_UPDATE_UI(wxID_REVERT, wxDocManager::OnUpdateFileRevert)
953 EVT_UPDATE_UI(wxID_NEW, wxDocManager::OnUpdateFileNew)
954 EVT_UPDATE_UI(wxID_SAVE, wxDocManager::OnUpdateFileSave)
955 EVT_UPDATE_UI(wxID_SAVEAS, wxDocManager::OnUpdateFileSaveAs)
956 EVT_UPDATE_UI(wxID_UNDO, wxDocManager::OnUpdateUndo)
957 EVT_UPDATE_UI(wxID_REDO, wxDocManager::OnUpdateRedo)
958
959 #if wxUSE_PRINTING_ARCHITECTURE
960 EVT_MENU(wxID_PRINT, wxDocManager::OnPrint)
961 EVT_MENU(wxID_PREVIEW, wxDocManager::OnPreview)
962 EVT_MENU(wxID_PRINT_SETUP, wxDocManager::OnPageSetup)
963
964 EVT_UPDATE_UI(wxID_PRINT, wxDocManager::OnUpdateDisableIfNoDoc)
965 EVT_UPDATE_UI(wxID_PREVIEW, wxDocManager::OnUpdateDisableIfNoDoc)
966 // NB: we keep "Print setup" menu item always enabled as it can be used
967 // even without an active document
968 #endif // wxUSE_PRINTING_ARCHITECTURE
969 END_EVENT_TABLE()
970
971 wxDocManager* wxDocManager::sm_docManager = NULL;
972
973 wxDocManager::wxDocManager(long WXUNUSED(flags), bool initialize)
974 {
975 sm_docManager = this;
976
977 m_defaultDocumentNameCounter = 1;
978 m_currentView = NULL;
979 m_maxDocsOpen = INT_MAX;
980 m_fileHistory = NULL;
981 if ( initialize )
982 Initialize();
983 }
984
985 wxDocManager::~wxDocManager()
986 {
987 Clear();
988 delete m_fileHistory;
989 sm_docManager = NULL;
990 }
991
992 // closes the specified document
993 bool wxDocManager::CloseDocument(wxDocument* doc, bool force)
994 {
995 if ( !doc->Close() && !force )
996 return false;
997
998 // Implicitly deletes the document when
999 // the last view is deleted
1000 doc->DeleteAllViews();
1001
1002 // Check we're really deleted
1003 if (m_docs.Member(doc))
1004 delete doc;
1005
1006 return true;
1007 }
1008
1009 bool wxDocManager::CloseDocuments(bool force)
1010 {
1011 wxList::compatibility_iterator node = m_docs.GetFirst();
1012 while (node)
1013 {
1014 wxDocument *doc = (wxDocument *)node->GetData();
1015 wxList::compatibility_iterator next = node->GetNext();
1016
1017 if (!CloseDocument(doc, force))
1018 return false;
1019
1020 // This assumes that documents are not connected in
1021 // any way, i.e. deleting one document does NOT
1022 // delete another.
1023 node = next;
1024 }
1025 return true;
1026 }
1027
1028 bool wxDocManager::Clear(bool force)
1029 {
1030 if (!CloseDocuments(force))
1031 return false;
1032
1033 m_currentView = NULL;
1034
1035 wxList::compatibility_iterator node = m_templates.GetFirst();
1036 while (node)
1037 {
1038 wxDocTemplate *templ = (wxDocTemplate*) node->GetData();
1039 wxList::compatibility_iterator next = node->GetNext();
1040 delete templ;
1041 node = next;
1042 }
1043 return true;
1044 }
1045
1046 bool wxDocManager::Initialize()
1047 {
1048 m_fileHistory = OnCreateFileHistory();
1049 return true;
1050 }
1051
1052 wxString wxDocManager::GetLastDirectory() const
1053 {
1054 // if we haven't determined the last used directory yet, do it now
1055 if ( m_lastDirectory.empty() )
1056 {
1057 // we're going to modify m_lastDirectory in this const method, so do it
1058 // via non-const self pointer instead of const this one
1059 wxDocManager * const self = const_cast<wxDocManager *>(this);
1060
1061 // first try to reuse the directory of the most recently opened file:
1062 // this ensures that if the user opens a file, closes the program and
1063 // runs it again the "Open file" dialog will open in the directory of
1064 // the last file he used
1065 if ( m_fileHistory && m_fileHistory->GetCount() )
1066 {
1067 const wxString lastOpened = m_fileHistory->GetHistoryFile(0);
1068 const wxFileName fn(lastOpened);
1069 if ( fn.DirExists() )
1070 {
1071 self->m_lastDirectory = fn.GetPath();
1072 }
1073 //else: should we try the next one?
1074 }
1075 //else: no history yet
1076
1077 // if we don't have any files in the history (yet?), use the
1078 // system-dependent default location for the document files
1079 if ( m_lastDirectory.empty() )
1080 {
1081 self->m_lastDirectory = wxStandardPaths::Get().GetAppDocumentsDir();
1082 }
1083 }
1084
1085 return m_lastDirectory;
1086 }
1087
1088 wxFileHistory *wxDocManager::OnCreateFileHistory()
1089 {
1090 return new wxFileHistory;
1091 }
1092
1093 void wxDocManager::OnFileClose(wxCommandEvent& WXUNUSED(event))
1094 {
1095 wxDocument *doc = GetCurrentDocument();
1096 if (doc)
1097 CloseDocument(doc);
1098 }
1099
1100 void wxDocManager::OnFileCloseAll(wxCommandEvent& WXUNUSED(event))
1101 {
1102 CloseDocuments(false);
1103 }
1104
1105 void wxDocManager::OnFileNew(wxCommandEvent& WXUNUSED(event))
1106 {
1107 CreateNewDocument();
1108 }
1109
1110 void wxDocManager::OnFileOpen(wxCommandEvent& WXUNUSED(event))
1111 {
1112 if ( !CreateDocument("") )
1113 {
1114 OnOpenFileFailure();
1115 }
1116 }
1117
1118 void wxDocManager::OnFileRevert(wxCommandEvent& WXUNUSED(event))
1119 {
1120 wxDocument *doc = GetCurrentDocument();
1121 if (!doc)
1122 return;
1123 doc->Revert();
1124 }
1125
1126 void wxDocManager::OnFileSave(wxCommandEvent& WXUNUSED(event))
1127 {
1128 wxDocument *doc = GetCurrentDocument();
1129 if (!doc)
1130 return;
1131 doc->Save();
1132 }
1133
1134 void wxDocManager::OnFileSaveAs(wxCommandEvent& WXUNUSED(event))
1135 {
1136 wxDocument *doc = GetCurrentDocument();
1137 if (!doc)
1138 return;
1139 doc->SaveAs();
1140 }
1141
1142 void wxDocManager::OnMRUFile(wxCommandEvent& event)
1143 {
1144 // Check if the id is in the range assigned to MRU list entries.
1145 const int id = event.GetId();
1146 if ( id >= wxID_FILE1 &&
1147 id < wxID_FILE1 + static_cast<int>(m_fileHistory->GetCount()) )
1148 {
1149 DoOpenMRUFile(id - wxID_FILE1);
1150 }
1151 else
1152 {
1153 event.Skip();
1154 }
1155 }
1156
1157 void wxDocManager::DoOpenMRUFile(unsigned n)
1158 {
1159 wxString filename(GetHistoryFile(n));
1160 if ( filename.empty() )
1161 return;
1162
1163 wxString errMsg; // must contain exactly one "%s" if non-empty
1164 if ( wxFile::Exists(filename) )
1165 {
1166 // Try to open it but don't give an error if it failed: this could be
1167 // normal, e.g. because the user cancelled opening it, and we don't
1168 // have any useful information to put in the error message anyhow, so
1169 // we assume that in case of an error the appropriate message had been
1170 // already logged.
1171 (void)CreateDocument(filename, wxDOC_SILENT);
1172 }
1173 else // file doesn't exist
1174 {
1175 OnMRUFileNotExist(n, filename);
1176 }
1177 }
1178
1179 void wxDocManager::OnMRUFileNotExist(unsigned n, const wxString& filename)
1180 {
1181 // remove the file which we can't open from the MRU list
1182 RemoveFileFromHistory(n);
1183
1184 // and tell the user about it
1185 wxLogError(_("The file '%s' doesn't exist and couldn't be opened.\n"
1186 "It has been removed from the most recently used files list."),
1187 filename);
1188 }
1189
1190 #if wxUSE_PRINTING_ARCHITECTURE
1191
1192 void wxDocManager::OnPrint(wxCommandEvent& WXUNUSED(event))
1193 {
1194 wxView *view = GetAnyUsableView();
1195 if (!view)
1196 return;
1197
1198 wxPrintout *printout = view->OnCreatePrintout();
1199 if (printout)
1200 {
1201 wxPrintDialogData printDialogData(m_pageSetupDialogData.GetPrintData());
1202 wxPrinter printer(&printDialogData);
1203 printer.Print(view->GetFrame(), printout, true);
1204
1205 delete printout;
1206 }
1207 }
1208
1209 void wxDocManager::OnPageSetup(wxCommandEvent& WXUNUSED(event))
1210 {
1211 wxPageSetupDialog dlg(wxTheApp->GetTopWindow(), &m_pageSetupDialogData);
1212 if ( dlg.ShowModal() == wxID_OK )
1213 {
1214 m_pageSetupDialogData = dlg.GetPageSetupData();
1215 }
1216 }
1217
1218 wxPreviewFrame* wxDocManager::CreatePreviewFrame(wxPrintPreviewBase* preview,
1219 wxWindow *parent,
1220 const wxString& title)
1221 {
1222 return new wxPreviewFrame(preview, parent, title);
1223 }
1224
1225 void wxDocManager::OnPreview(wxCommandEvent& WXUNUSED(event))
1226 {
1227 wxBusyCursor busy;
1228 wxView *view = GetAnyUsableView();
1229 if (!view)
1230 return;
1231
1232 wxPrintout *printout = view->OnCreatePrintout();
1233 if (printout)
1234 {
1235 wxPrintDialogData printDialogData(m_pageSetupDialogData.GetPrintData());
1236
1237 // Pass two printout objects: for preview, and possible printing.
1238 wxPrintPreviewBase *
1239 preview = new wxPrintPreview(printout,
1240 view->OnCreatePrintout(),
1241 &printDialogData);
1242 if ( !preview->IsOk() )
1243 {
1244 delete preview;
1245 wxLogError(_("Print preview creation failed."));
1246 return;
1247 }
1248
1249 wxPreviewFrame* frame = CreatePreviewFrame(preview,
1250 wxTheApp->GetTopWindow(),
1251 _("Print Preview"));
1252 wxCHECK_RET( frame, "should create a print preview frame" );
1253
1254 frame->Centre(wxBOTH);
1255 frame->Initialize();
1256 frame->Show(true);
1257 }
1258 }
1259 #endif // wxUSE_PRINTING_ARCHITECTURE
1260
1261 void wxDocManager::OnUndo(wxCommandEvent& event)
1262 {
1263 wxCommandProcessor * const cmdproc = GetCurrentCommandProcessor();
1264 if ( !cmdproc )
1265 {
1266 event.Skip();
1267 return;
1268 }
1269
1270 cmdproc->Undo();
1271 }
1272
1273 void wxDocManager::OnRedo(wxCommandEvent& event)
1274 {
1275 wxCommandProcessor * const cmdproc = GetCurrentCommandProcessor();
1276 if ( !cmdproc )
1277 {
1278 event.Skip();
1279 return;
1280 }
1281
1282 cmdproc->Redo();
1283 }
1284
1285 // Handlers for UI update commands
1286
1287 void wxDocManager::OnUpdateFileOpen(wxUpdateUIEvent& event)
1288 {
1289 // CreateDocument() (which is called from OnFileOpen) may succeed
1290 // only when there is at least a template:
1291 event.Enable( GetTemplates().GetCount()>0 );
1292 }
1293
1294 void wxDocManager::OnUpdateDisableIfNoDoc(wxUpdateUIEvent& event)
1295 {
1296 event.Enable( GetCurrentDocument() != NULL );
1297 }
1298
1299 void wxDocManager::OnUpdateFileRevert(wxUpdateUIEvent& event)
1300 {
1301 wxDocument* doc = GetCurrentDocument();
1302 event.Enable(doc && doc->IsModified() && doc->GetDocumentSaved());
1303 }
1304
1305 void wxDocManager::OnUpdateFileNew(wxUpdateUIEvent& event)
1306 {
1307 // CreateDocument() (which is called from OnFileNew) may succeed
1308 // only when there is at least a template:
1309 event.Enable( GetTemplates().GetCount()>0 );
1310 }
1311
1312 void wxDocManager::OnUpdateFileSave(wxUpdateUIEvent& event)
1313 {
1314 wxDocument * const doc = GetCurrentDocument();
1315 event.Enable( doc && !doc->IsChildDocument() && !doc->AlreadySaved() );
1316 }
1317
1318 void wxDocManager::OnUpdateFileSaveAs(wxUpdateUIEvent& event)
1319 {
1320 wxDocument * const doc = GetCurrentDocument();
1321 event.Enable( doc && !doc->IsChildDocument() );
1322 }
1323
1324 void wxDocManager::OnUpdateUndo(wxUpdateUIEvent& event)
1325 {
1326 wxCommandProcessor * const cmdproc = GetCurrentCommandProcessor();
1327 if ( !cmdproc )
1328 {
1329 // If we don't have any document at all, the menu item should really be
1330 // disabled.
1331 if ( !GetCurrentDocument() )
1332 event.Enable(false);
1333 else // But if we do have it, it might handle wxID_UNDO on its own
1334 event.Skip();
1335 return;
1336 }
1337 event.Enable(cmdproc->CanUndo());
1338 cmdproc->SetMenuStrings();
1339 }
1340
1341 void wxDocManager::OnUpdateRedo(wxUpdateUIEvent& event)
1342 {
1343 wxCommandProcessor * const cmdproc = GetCurrentCommandProcessor();
1344 if ( !cmdproc )
1345 {
1346 // Use same logic as in OnUpdateUndo() above.
1347 if ( !GetCurrentDocument() )
1348 event.Enable(false);
1349 else
1350 event.Skip();
1351 return;
1352 }
1353 event.Enable(cmdproc->CanRedo());
1354 cmdproc->SetMenuStrings();
1355 }
1356
1357 wxView *wxDocManager::GetAnyUsableView() const
1358 {
1359 wxView *view = GetCurrentView();
1360
1361 if ( !view && !m_docs.empty() )
1362 {
1363 // if we have exactly one document, consider its view to be the current
1364 // one
1365 //
1366 // VZ: I'm not exactly sure why is this needed but this is how this
1367 // code used to behave before the bug #9518 was fixed and it seems
1368 // safer to preserve the old logic
1369 wxList::compatibility_iterator node = m_docs.GetFirst();
1370 if ( !node->GetNext() )
1371 {
1372 wxDocument *doc = static_cast<wxDocument *>(node->GetData());
1373 view = doc->GetFirstView();
1374 }
1375 //else: we have more than one document
1376 }
1377
1378 return view;
1379 }
1380
1381 bool wxDocManager::TryBefore(wxEvent& event)
1382 {
1383 wxView * const view = GetAnyUsableView();
1384 return view && view->ProcessEventLocally(event);
1385 }
1386
1387 namespace
1388 {
1389
1390 // helper function: return only the visible templates
1391 wxDocTemplateVector GetVisibleTemplates(const wxList& allTemplates)
1392 {
1393 // select only the visible templates
1394 const size_t totalNumTemplates = allTemplates.GetCount();
1395 wxDocTemplateVector templates;
1396 if ( totalNumTemplates )
1397 {
1398 templates.reserve(totalNumTemplates);
1399
1400 for ( wxList::const_iterator i = allTemplates.begin(),
1401 end = allTemplates.end();
1402 i != end;
1403 ++i )
1404 {
1405 wxDocTemplate * const temp = (wxDocTemplate *)*i;
1406 if ( temp->IsVisible() )
1407 templates.push_back(temp);
1408 }
1409 }
1410
1411 return templates;
1412 }
1413
1414 } // anonymous namespace
1415
1416 void wxDocument::Activate()
1417 {
1418 wxView * const view = GetFirstView();
1419 if ( !view )
1420 return;
1421
1422 view->Activate(true);
1423 if ( wxWindow *win = view->GetFrame() )
1424 win->Raise();
1425 }
1426
1427 wxDocument* wxDocManager::FindDocumentByPath(const wxString& path) const
1428 {
1429 const wxFileName fileName(path);
1430 for ( wxList::const_iterator i = m_docs.begin(); i != m_docs.end(); ++i )
1431 {
1432 wxDocument * const doc = wxStaticCast(*i, wxDocument);
1433
1434 if ( fileName == wxFileName(doc->GetFilename()) )
1435 return doc;
1436 }
1437 return NULL;
1438 }
1439
1440 wxDocument *wxDocManager::CreateDocument(const wxString& pathOrig, long flags)
1441 {
1442 // this ought to be const but SelectDocumentType/Path() are not
1443 // const-correct and can't be changed as, being virtual, this risks
1444 // breaking user code overriding them
1445 wxDocTemplateVector templates(GetVisibleTemplates(m_templates));
1446 const size_t numTemplates = templates.size();
1447 if ( !numTemplates )
1448 {
1449 // no templates can be used, can't create document
1450 return NULL;
1451 }
1452
1453
1454 // normally user should select the template to use but wxDOC_SILENT flag we
1455 // choose one ourselves
1456 wxString path = pathOrig; // may be modified below
1457 wxDocTemplate *temp;
1458 if ( flags & wxDOC_SILENT )
1459 {
1460 wxASSERT_MSG( !path.empty(),
1461 "using empty path with wxDOC_SILENT doesn't make sense" );
1462
1463 temp = FindTemplateForPath(path);
1464 if ( !temp )
1465 {
1466 wxLogWarning(_("The format of file '%s' couldn't be determined."),
1467 path);
1468 }
1469 }
1470 else // not silent, ask the user
1471 {
1472 // for the new file we need just the template, for an existing one we
1473 // need the template and the path, unless it's already specified
1474 if ( (flags & wxDOC_NEW) || !path.empty() )
1475 temp = SelectDocumentType(&templates[0], numTemplates);
1476 else
1477 temp = SelectDocumentPath(&templates[0], numTemplates, path, flags);
1478 }
1479
1480 if ( !temp )
1481 return NULL;
1482
1483 // check whether the document with this path is already opened
1484 if ( !path.empty() )
1485 {
1486 wxDocument * const doc = FindDocumentByPath(path);
1487 if (doc)
1488 {
1489 // file already open, just activate it and return
1490 doc->Activate();
1491 return doc;
1492 }
1493 }
1494
1495 // no, we need to create a new document
1496
1497
1498 // if we've reached the max number of docs, close the first one.
1499 if ( (int)GetDocuments().GetCount() >= m_maxDocsOpen )
1500 {
1501 if ( !CloseDocument((wxDocument *)GetDocuments().GetFirst()->GetData()) )
1502 {
1503 // can't open the new document if closing the old one failed
1504 return NULL;
1505 }
1506 }
1507
1508
1509 // do create and initialize the new document finally
1510 wxDocument * const docNew = temp->CreateDocument(path, flags);
1511 if ( !docNew )
1512 return NULL;
1513
1514 docNew->SetDocumentName(temp->GetDocumentName());
1515 docNew->SetDocumentTemplate(temp);
1516
1517 wxTRY
1518 {
1519 // call the appropriate function depending on whether we're creating a
1520 // new file or opening an existing one
1521 if ( !(flags & wxDOC_NEW ? docNew->OnNewDocument()
1522 : docNew->OnOpenDocument(path)) )
1523 {
1524 docNew->DeleteAllViews();
1525 return NULL;
1526 }
1527 }
1528 wxCATCH_ALL( docNew->DeleteAllViews(); throw; )
1529
1530 // add the successfully opened file to MRU, but only if we're going to be
1531 // able to reopen it successfully later which requires the template for
1532 // this document to be retrievable from the file extension
1533 if ( !(flags & wxDOC_NEW) && temp->FileMatchesTemplate(path) )
1534 AddFileToHistory(path);
1535
1536 // at least under Mac (where views are top level windows) it seems to be
1537 // necessary to manually activate the new document to bring it to the
1538 // forefront -- and it shouldn't hurt doing this under the other platforms
1539 docNew->Activate();
1540
1541 return docNew;
1542 }
1543
1544 wxView *wxDocManager::CreateView(wxDocument *doc, long flags)
1545 {
1546 wxDocTemplateVector templates(GetVisibleTemplates(m_templates));
1547 const size_t numTemplates = templates.size();
1548
1549 if ( numTemplates == 0 )
1550 return NULL;
1551
1552 wxDocTemplate * const
1553 temp = numTemplates == 1 ? templates[0]
1554 : SelectViewType(&templates[0], numTemplates);
1555
1556 if ( !temp )
1557 return NULL;
1558
1559 wxView *view = temp->CreateView(doc, flags);
1560 if ( view )
1561 view->SetViewName(temp->GetViewName());
1562 return view;
1563 }
1564
1565 // Not yet implemented
1566 void
1567 wxDocManager::DeleteTemplate(wxDocTemplate *WXUNUSED(temp), long WXUNUSED(flags))
1568 {
1569 }
1570
1571 // Not yet implemented
1572 bool wxDocManager::FlushDoc(wxDocument *WXUNUSED(doc))
1573 {
1574 return false;
1575 }
1576
1577 wxDocument *wxDocManager::GetCurrentDocument() const
1578 {
1579 wxView * const view = GetAnyUsableView();
1580 return view ? view->GetDocument() : NULL;
1581 }
1582
1583 wxCommandProcessor *wxDocManager::GetCurrentCommandProcessor() const
1584 {
1585 wxDocument * const doc = GetCurrentDocument();
1586 return doc ? doc->GetCommandProcessor() : NULL;
1587 }
1588
1589 // Make a default name for a new document
1590 #if WXWIN_COMPATIBILITY_2_8
1591 bool wxDocManager::MakeDefaultName(wxString& WXUNUSED(name))
1592 {
1593 // we consider that this function can only be overridden by the user code,
1594 // not called by it as it only makes sense to call it internally, so we
1595 // don't bother to return anything from here
1596 return false;
1597 }
1598 #endif // WXWIN_COMPATIBILITY_2_8
1599
1600 wxString wxDocManager::MakeNewDocumentName()
1601 {
1602 wxString name;
1603
1604 #if WXWIN_COMPATIBILITY_2_8
1605 if ( !MakeDefaultName(name) )
1606 #endif // WXWIN_COMPATIBILITY_2_8
1607 {
1608 name.Printf(_("unnamed%d"), m_defaultDocumentNameCounter);
1609 m_defaultDocumentNameCounter++;
1610 }
1611
1612 return name;
1613 }
1614
1615 // Make a frame title (override this to do something different)
1616 // If docName is empty, a document is not currently active.
1617 wxString wxDocManager::MakeFrameTitle(wxDocument* doc)
1618 {
1619 wxString appName = wxTheApp->GetAppDisplayName();
1620 wxString title;
1621 if (!doc)
1622 title = appName;
1623 else
1624 {
1625 wxString docName = doc->GetUserReadableName();
1626 title = docName + wxString(_(" - ")) + appName;
1627 }
1628 return title;
1629 }
1630
1631
1632 // Not yet implemented
1633 wxDocTemplate *wxDocManager::MatchTemplate(const wxString& WXUNUSED(path))
1634 {
1635 return NULL;
1636 }
1637
1638 // File history management
1639 void wxDocManager::AddFileToHistory(const wxString& file)
1640 {
1641 if (m_fileHistory)
1642 m_fileHistory->AddFileToHistory(file);
1643 }
1644
1645 void wxDocManager::RemoveFileFromHistory(size_t i)
1646 {
1647 if (m_fileHistory)
1648 m_fileHistory->RemoveFileFromHistory(i);
1649 }
1650
1651 wxString wxDocManager::GetHistoryFile(size_t i) const
1652 {
1653 wxString histFile;
1654
1655 if (m_fileHistory)
1656 histFile = m_fileHistory->GetHistoryFile(i);
1657
1658 return histFile;
1659 }
1660
1661 void wxDocManager::FileHistoryUseMenu(wxMenu *menu)
1662 {
1663 if (m_fileHistory)
1664 m_fileHistory->UseMenu(menu);
1665 }
1666
1667 void wxDocManager::FileHistoryRemoveMenu(wxMenu *menu)
1668 {
1669 if (m_fileHistory)
1670 m_fileHistory->RemoveMenu(menu);
1671 }
1672
1673 #if wxUSE_CONFIG
1674 void wxDocManager::FileHistoryLoad(const wxConfigBase& config)
1675 {
1676 if (m_fileHistory)
1677 m_fileHistory->Load(config);
1678 }
1679
1680 void wxDocManager::FileHistorySave(wxConfigBase& config)
1681 {
1682 if (m_fileHistory)
1683 m_fileHistory->Save(config);
1684 }
1685 #endif
1686
1687 void wxDocManager::FileHistoryAddFilesToMenu(wxMenu* menu)
1688 {
1689 if (m_fileHistory)
1690 m_fileHistory->AddFilesToMenu(menu);
1691 }
1692
1693 void wxDocManager::FileHistoryAddFilesToMenu()
1694 {
1695 if (m_fileHistory)
1696 m_fileHistory->AddFilesToMenu();
1697 }
1698
1699 size_t wxDocManager::GetHistoryFilesCount() const
1700 {
1701 return m_fileHistory ? m_fileHistory->GetCount() : 0;
1702 }
1703
1704
1705 // Find out the document template via matching in the document file format
1706 // against that of the template
1707 wxDocTemplate *wxDocManager::FindTemplateForPath(const wxString& path)
1708 {
1709 wxDocTemplate *theTemplate = NULL;
1710
1711 // Find the template which this extension corresponds to
1712 for (size_t i = 0; i < m_templates.GetCount(); i++)
1713 {
1714 wxDocTemplate *temp = (wxDocTemplate *)m_templates.Item(i)->GetData();
1715 if ( temp->FileMatchesTemplate(path) )
1716 {
1717 theTemplate = temp;
1718 break;
1719 }
1720 }
1721 return theTemplate;
1722 }
1723
1724 // Prompts user to open a file, using file specs in templates.
1725 // Must extend the file selector dialog or implement own; OR
1726 // match the extension to the template extension.
1727
1728 wxDocTemplate *wxDocManager::SelectDocumentPath(wxDocTemplate **templates,
1729 int noTemplates,
1730 wxString& path,
1731 long WXUNUSED(flags),
1732 bool WXUNUSED(save))
1733 {
1734 #ifdef wxHAS_MULTIPLE_FILEDLG_FILTERS
1735 wxString descrBuf;
1736
1737 for (int i = 0; i < noTemplates; i++)
1738 {
1739 if (templates[i]->IsVisible())
1740 {
1741 // add a '|' to separate this filter from the previous one
1742 if ( !descrBuf.empty() )
1743 descrBuf << wxT('|');
1744
1745 descrBuf << templates[i]->GetDescription()
1746 << wxT(" (") << templates[i]->GetFileFilter() << wxT(") |")
1747 << templates[i]->GetFileFilter();
1748 }
1749 }
1750 #else
1751 wxString descrBuf = wxT("*.*");
1752 wxUnusedVar(noTemplates);
1753 #endif
1754
1755 int FilterIndex = -1;
1756
1757 wxString pathTmp = wxFileSelectorEx(_("Open File"),
1758 GetLastDirectory(),
1759 wxEmptyString,
1760 &FilterIndex,
1761 descrBuf,
1762 wxFD_OPEN | wxFD_FILE_MUST_EXIST);
1763
1764 wxDocTemplate *theTemplate = NULL;
1765 if (!pathTmp.empty())
1766 {
1767 if (!wxFileExists(pathTmp))
1768 {
1769 wxString msgTitle;
1770 if (!wxTheApp->GetAppDisplayName().empty())
1771 msgTitle = wxTheApp->GetAppDisplayName();
1772 else
1773 msgTitle = wxString(_("File error"));
1774
1775 wxMessageBox(_("Sorry, could not open this file."),
1776 msgTitle,
1777 wxOK | wxICON_EXCLAMATION | wxCENTRE);
1778
1779 path = wxEmptyString;
1780 return NULL;
1781 }
1782
1783 SetLastDirectory(wxPathOnly(pathTmp));
1784
1785 path = pathTmp;
1786
1787 // first choose the template using the extension, if this fails (i.e.
1788 // wxFileSelectorEx() didn't fill it), then use the path
1789 if ( FilterIndex != -1 )
1790 theTemplate = templates[FilterIndex];
1791 if ( !theTemplate )
1792 theTemplate = FindTemplateForPath(path);
1793 if ( !theTemplate )
1794 {
1795 // Since we do not add files with non-default extensions to the
1796 // file history this can only happen if the application changes the
1797 // allowed templates in runtime.
1798 wxMessageBox(_("Sorry, the format for this file is unknown."),
1799 _("Open File"),
1800 wxOK | wxICON_EXCLAMATION | wxCENTRE);
1801 }
1802 }
1803 else
1804 {
1805 path.clear();
1806 }
1807
1808 return theTemplate;
1809 }
1810
1811 wxDocTemplate *wxDocManager::SelectDocumentType(wxDocTemplate **templates,
1812 int noTemplates, bool sort)
1813 {
1814 wxArrayString strings;
1815 wxScopedArray<wxDocTemplate *> data(new wxDocTemplate *[noTemplates]);
1816 int i;
1817 int n = 0;
1818
1819 for (i = 0; i < noTemplates; i++)
1820 {
1821 if (templates[i]->IsVisible())
1822 {
1823 int j;
1824 bool want = true;
1825 for (j = 0; j < n; j++)
1826 {
1827 //filter out NOT unique documents + view combinations
1828 if ( templates[i]->m_docTypeName == data[j]->m_docTypeName &&
1829 templates[i]->m_viewTypeName == data[j]->m_viewTypeName
1830 )
1831 want = false;
1832 }
1833
1834 if ( want )
1835 {
1836 strings.Add(templates[i]->m_description);
1837
1838 data[n] = templates[i];
1839 n ++;
1840 }
1841 }
1842 } // for
1843
1844 if (sort)
1845 {
1846 strings.Sort(); // ascending sort
1847 // Yes, this will be slow, but template lists
1848 // are typically short.
1849 int j;
1850 n = strings.Count();
1851 for (i = 0; i < n; i++)
1852 {
1853 for (j = 0; j < noTemplates; j++)
1854 {
1855 if (strings[i] == templates[j]->m_description)
1856 data[i] = templates[j];
1857 }
1858 }
1859 }
1860
1861 wxDocTemplate *theTemplate;
1862
1863 switch ( n )
1864 {
1865 case 0:
1866 // no visible templates, hence nothing to choose from
1867 theTemplate = NULL;
1868 break;
1869
1870 case 1:
1871 // don't propose the user to choose if he has no choice
1872 theTemplate = data[0];
1873 break;
1874
1875 default:
1876 // propose the user to choose one of several
1877 theTemplate = (wxDocTemplate *)wxGetSingleChoiceData
1878 (
1879 _("Select a document template"),
1880 _("Templates"),
1881 strings,
1882 (void **)data.get()
1883 );
1884 }
1885
1886 return theTemplate;
1887 }
1888
1889 wxDocTemplate *wxDocManager::SelectViewType(wxDocTemplate **templates,
1890 int noTemplates, bool sort)
1891 {
1892 wxArrayString strings;
1893 wxScopedArray<wxDocTemplate *> data(new wxDocTemplate *[noTemplates]);
1894 int i;
1895 int n = 0;
1896
1897 for (i = 0; i < noTemplates; i++)
1898 {
1899 wxDocTemplate *templ = templates[i];
1900 if ( templ->IsVisible() && !templ->GetViewName().empty() )
1901 {
1902 int j;
1903 bool want = true;
1904 for (j = 0; j < n; j++)
1905 {
1906 //filter out NOT unique views
1907 if ( templates[i]->m_viewTypeName == data[j]->m_viewTypeName )
1908 want = false;
1909 }
1910
1911 if ( want )
1912 {
1913 strings.Add(templ->m_viewTypeName);
1914 data[n] = templ;
1915 n ++;
1916 }
1917 }
1918 }
1919
1920 if (sort)
1921 {
1922 strings.Sort(); // ascending sort
1923 // Yes, this will be slow, but template lists
1924 // are typically short.
1925 int j;
1926 n = strings.Count();
1927 for (i = 0; i < n; i++)
1928 {
1929 for (j = 0; j < noTemplates; j++)
1930 {
1931 if (strings[i] == templates[j]->m_viewTypeName)
1932 data[i] = templates[j];
1933 }
1934 }
1935 }
1936
1937 wxDocTemplate *theTemplate;
1938
1939 // the same logic as above
1940 switch ( n )
1941 {
1942 case 0:
1943 theTemplate = NULL;
1944 break;
1945
1946 case 1:
1947 theTemplate = data[0];
1948 break;
1949
1950 default:
1951 theTemplate = (wxDocTemplate *)wxGetSingleChoiceData
1952 (
1953 _("Select a document view"),
1954 _("Views"),
1955 strings,
1956 (void **)data.get()
1957 );
1958
1959 }
1960
1961 return theTemplate;
1962 }
1963
1964 void wxDocManager::AssociateTemplate(wxDocTemplate *temp)
1965 {
1966 if (!m_templates.Member(temp))
1967 m_templates.Append(temp);
1968 }
1969
1970 void wxDocManager::DisassociateTemplate(wxDocTemplate *temp)
1971 {
1972 m_templates.DeleteObject(temp);
1973 }
1974
1975 wxDocTemplate* wxDocManager::FindTemplate(const wxClassInfo* classinfo)
1976 {
1977 for ( wxList::compatibility_iterator node = m_templates.GetFirst();
1978 node;
1979 node = node->GetNext() )
1980 {
1981 wxDocTemplate* t = wxStaticCast(node->GetData(), wxDocTemplate);
1982 if ( t->GetDocClassInfo() == classinfo )
1983 return t;
1984 }
1985
1986 return NULL;
1987 }
1988
1989 // Add and remove a document from the manager's list
1990 void wxDocManager::AddDocument(wxDocument *doc)
1991 {
1992 if (!m_docs.Member(doc))
1993 m_docs.Append(doc);
1994 }
1995
1996 void wxDocManager::RemoveDocument(wxDocument *doc)
1997 {
1998 m_docs.DeleteObject(doc);
1999 }
2000
2001 // Views or windows should inform the document manager
2002 // when a view is going in or out of focus
2003 void wxDocManager::ActivateView(wxView *view, bool activate)
2004 {
2005 if ( activate )
2006 {
2007 m_currentView = view;
2008 }
2009 else // deactivate
2010 {
2011 if ( m_currentView == view )
2012 {
2013 // don't keep stale pointer
2014 m_currentView = NULL;
2015 }
2016 }
2017 }
2018
2019 // ----------------------------------------------------------------------------
2020 // wxDocChildFrameAnyBase
2021 // ----------------------------------------------------------------------------
2022
2023 bool wxDocChildFrameAnyBase::TryProcessEvent(wxEvent& event)
2024 {
2025 if ( !m_childView )
2026 {
2027 // We must be being destroyed, don't forward events anywhere as
2028 // m_childDocument could be invalid by now.
2029 return false;
2030 }
2031
2032 // Forward the event to the document manager which will, in turn, forward
2033 // it to its active view which must be our m_childView.
2034 //
2035 // Notice that we do things in this roundabout way to guarantee the correct
2036 // event handlers call order: first the document, then the new and then the
2037 // document manager itself. And if we forwarded the event directly to the
2038 // view, then the document manager would do it once again when we forwarded
2039 // it to it.
2040 return m_childDocument->GetDocumentManager()->ProcessEventLocally(event);
2041 }
2042
2043 bool wxDocChildFrameAnyBase::CloseView(wxCloseEvent& event)
2044 {
2045 if ( m_childView )
2046 {
2047 // notice that we must call wxView::Close() and OnClose() called from
2048 // it in any case, even if we know that we are going to close anyhow
2049 if ( !m_childView->Close(false) && event.CanVeto() )
2050 {
2051 event.Veto();
2052 return false;
2053 }
2054
2055 m_childView->Activate(false);
2056
2057 // it is important to reset m_childView frame pointer to NULL before
2058 // deleting it because while normally it is the frame which deletes the
2059 // view when it's closed, the view also closes the frame if it is
2060 // deleted directly not by us as indicated by its doc child frame
2061 // pointer still being set
2062 m_childView->SetDocChildFrame(NULL);
2063 wxDELETE(m_childView);
2064 }
2065
2066 m_childDocument = NULL;
2067
2068 return true;
2069 }
2070
2071 // ----------------------------------------------------------------------------
2072 // wxDocParentFrameAnyBase
2073 // ----------------------------------------------------------------------------
2074
2075 bool wxDocParentFrameAnyBase::TryProcessEvent(wxEvent& event)
2076 {
2077 if ( !m_docManager )
2078 return false;
2079
2080 // If we have an active view, its associated child frame may have
2081 // already forwarded the event to wxDocManager, check for this:
2082 if ( wxView* const view = m_docManager->GetAnyUsableView() )
2083 {
2084 wxWindow* win = view->GetFrame();
2085 if ( win && win != m_frame )
2086 {
2087 // Notice that we intentionally don't use wxGetTopLevelParent()
2088 // here because we want to check both for the case of a child
2089 // "frame" (e.g. MDI child frame or notebook page) inside this TLW
2090 // and a separate child TLW frame (as used in the SDI mode) here.
2091 for ( win = win->GetParent(); win; win = win->GetParent() )
2092 {
2093 if ( win == m_frame )
2094 return false;
2095 }
2096 }
2097 //else: This view is directly associated with the parent frame (which
2098 // can happen in the so called "single" mode in which only one
2099 // document can be opened and so is managed by the parent frame
2100 // itself), there can be no child frame in play so we must forward
2101 // the event to wxDocManager ourselves.
2102 }
2103
2104 // But forward the event to wxDocManager ourselves if there are no views at
2105 // all or if we are the frame's view ourselves.
2106 return m_docManager->ProcessEventLocally(event);
2107 }
2108
2109 // ----------------------------------------------------------------------------
2110 // Printing support
2111 // ----------------------------------------------------------------------------
2112
2113 #if wxUSE_PRINTING_ARCHITECTURE
2114
2115 namespace
2116 {
2117
2118 wxString GetAppropriateTitle(const wxView *view, const wxString& titleGiven)
2119 {
2120 wxString title(titleGiven);
2121 if ( title.empty() )
2122 {
2123 if ( view && view->GetDocument() )
2124 title = view->GetDocument()->GetUserReadableName();
2125 else
2126 title = _("Printout");
2127 }
2128
2129 return title;
2130 }
2131
2132 } // anonymous namespace
2133
2134 wxDocPrintout::wxDocPrintout(wxView *view, const wxString& title)
2135 : wxPrintout(GetAppropriateTitle(view, title))
2136 {
2137 m_printoutView = view;
2138 }
2139
2140 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page))
2141 {
2142 wxDC *dc = GetDC();
2143
2144 // Get the logical pixels per inch of screen and printer
2145 int ppiScreenX, ppiScreenY;
2146 GetPPIScreen(&ppiScreenX, &ppiScreenY);
2147 wxUnusedVar(ppiScreenY);
2148 int ppiPrinterX, ppiPrinterY;
2149 GetPPIPrinter(&ppiPrinterX, &ppiPrinterY);
2150 wxUnusedVar(ppiPrinterY);
2151
2152 // This scales the DC so that the printout roughly represents the
2153 // the screen scaling. The text point size _should_ be the right size
2154 // but in fact is too small for some reason. This is a detail that will
2155 // need to be addressed at some point but can be fudged for the
2156 // moment.
2157 float scale = (float)((float)ppiPrinterX/(float)ppiScreenX);
2158
2159 // Now we have to check in case our real page size is reduced
2160 // (e.g. because we're drawing to a print preview memory DC)
2161 int pageWidth, pageHeight;
2162 int w, h;
2163 dc->GetSize(&w, &h);
2164 GetPageSizePixels(&pageWidth, &pageHeight);
2165 wxUnusedVar(pageHeight);
2166
2167 // If printer pageWidth == current DC width, then this doesn't
2168 // change. But w might be the preview bitmap width, so scale down.
2169 float overallScale = scale * (float)(w/(float)pageWidth);
2170 dc->SetUserScale(overallScale, overallScale);
2171
2172 if (m_printoutView)
2173 {
2174 m_printoutView->OnDraw(dc);
2175 }
2176 return true;
2177 }
2178
2179 bool wxDocPrintout::HasPage(int pageNum)
2180 {
2181 return (pageNum == 1);
2182 }
2183
2184 bool wxDocPrintout::OnBeginDocument(int startPage, int endPage)
2185 {
2186 if (!wxPrintout::OnBeginDocument(startPage, endPage))
2187 return false;
2188
2189 return true;
2190 }
2191
2192 void wxDocPrintout::GetPageInfo(int *minPage, int *maxPage,
2193 int *selPageFrom, int *selPageTo)
2194 {
2195 *minPage = 1;
2196 *maxPage = 1;
2197 *selPageFrom = 1;
2198 *selPageTo = 1;
2199 }
2200
2201 #endif // wxUSE_PRINTING_ARCHITECTURE
2202
2203 // ----------------------------------------------------------------------------
2204 // Permits compatibility with existing file formats and functions that
2205 // manipulate files directly
2206 // ----------------------------------------------------------------------------
2207
2208 #if wxUSE_STD_IOSTREAM
2209
2210 bool wxTransferFileToStream(const wxString& filename, wxSTD ostream& stream)
2211 {
2212 #if wxUSE_FFILE
2213 wxFFile file(filename, wxT("rb"));
2214 #elif wxUSE_FILE
2215 wxFile file(filename, wxFile::read);
2216 #endif
2217 if ( !file.IsOpened() )
2218 return false;
2219
2220 char buf[4096];
2221
2222 size_t nRead;
2223 do
2224 {
2225 nRead = file.Read(buf, WXSIZEOF(buf));
2226 if ( file.Error() )
2227 return false;
2228
2229 stream.write(buf, nRead);
2230 if ( !stream )
2231 return false;
2232 }
2233 while ( !file.Eof() );
2234
2235 return true;
2236 }
2237
2238 bool wxTransferStreamToFile(wxSTD istream& stream, const wxString& filename)
2239 {
2240 #if wxUSE_FFILE
2241 wxFFile file(filename, wxT("wb"));
2242 #elif wxUSE_FILE
2243 wxFile file(filename, wxFile::write);
2244 #endif
2245 if ( !file.IsOpened() )
2246 return false;
2247
2248 char buf[4096];
2249 do
2250 {
2251 stream.read(buf, WXSIZEOF(buf));
2252 if ( !stream.bad() ) // fail may be set on EOF, don't use operator!()
2253 {
2254 if ( !file.Write(buf, stream.gcount()) )
2255 return false;
2256 }
2257 }
2258 while ( !stream.eof() );
2259
2260 return true;
2261 }
2262
2263 #else // !wxUSE_STD_IOSTREAM
2264
2265 bool wxTransferFileToStream(const wxString& filename, wxOutputStream& stream)
2266 {
2267 #if wxUSE_FFILE
2268 wxFFile file(filename, wxT("rb"));
2269 #elif wxUSE_FILE
2270 wxFile file(filename, wxFile::read);
2271 #endif
2272 if ( !file.IsOpened() )
2273 return false;
2274
2275 char buf[4096];
2276
2277 size_t nRead;
2278 do
2279 {
2280 nRead = file.Read(buf, WXSIZEOF(buf));
2281 if ( file.Error() )
2282 return false;
2283
2284 stream.Write(buf, nRead);
2285 if ( !stream )
2286 return false;
2287 }
2288 while ( !file.Eof() );
2289
2290 return true;
2291 }
2292
2293 bool wxTransferStreamToFile(wxInputStream& stream, const wxString& filename)
2294 {
2295 #if wxUSE_FFILE
2296 wxFFile file(filename, wxT("wb"));
2297 #elif wxUSE_FILE
2298 wxFile file(filename, wxFile::write);
2299 #endif
2300 if ( !file.IsOpened() )
2301 return false;
2302
2303 char buf[4096];
2304 for ( ;; )
2305 {
2306 stream.Read(buf, WXSIZEOF(buf));
2307
2308 const size_t nRead = stream.LastRead();
2309 if ( !nRead )
2310 {
2311 if ( stream.Eof() )
2312 break;
2313
2314 return false;
2315 }
2316
2317 if ( !file.Write(buf, nRead) )
2318 return false;
2319 }
2320
2321 return true;
2322 }
2323
2324 #endif // wxUSE_STD_IOSTREAM/!wxUSE_STD_IOSTREAM
2325
2326 #endif // wxUSE_DOC_VIEW_ARCHITECTURE