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