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