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