Quick and dirty fix for building with COMPATIBILITY_2_4 off.
[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& event)
968 {
969 wxDocument *doc = GetCurrentDocument();
970 if (!doc)
971 return;
972 if (doc->GetCommandProcessor())
973 doc->GetCommandProcessor()->Undo();
974 else
975 event.Skip();
976 }
977
978 void wxDocManager::OnRedo(wxCommandEvent& event)
979 {
980 wxDocument *doc = GetCurrentDocument();
981 if (!doc)
982 return;
983 if (doc->GetCommandProcessor())
984 doc->GetCommandProcessor()->Redo();
985 else
986 event.Skip();
987 }
988
989 // Handlers for UI update commands
990
991 void wxDocManager::OnUpdateFileOpen(wxUpdateUIEvent& event)
992 {
993 event.Enable( TRUE );
994 }
995
996 void wxDocManager::OnUpdateFileClose(wxUpdateUIEvent& event)
997 {
998 wxDocument *doc = GetCurrentDocument();
999 event.Enable( (doc != (wxDocument*) NULL) );
1000 }
1001
1002 void wxDocManager::OnUpdateFileRevert(wxUpdateUIEvent& event)
1003 {
1004 wxDocument *doc = GetCurrentDocument();
1005 event.Enable( (doc != (wxDocument*) NULL) );
1006 }
1007
1008 void wxDocManager::OnUpdateFileNew(wxUpdateUIEvent& event)
1009 {
1010 event.Enable( TRUE );
1011 }
1012
1013 void wxDocManager::OnUpdateFileSave(wxUpdateUIEvent& event)
1014 {
1015 wxDocument *doc = GetCurrentDocument();
1016 event.Enable( doc && doc->IsModified() );
1017 }
1018
1019 void wxDocManager::OnUpdateFileSaveAs(wxUpdateUIEvent& event)
1020 {
1021 wxDocument *doc = GetCurrentDocument();
1022 event.Enable( (doc != (wxDocument*) NULL) );
1023 }
1024
1025 void wxDocManager::OnUpdateUndo(wxUpdateUIEvent& event)
1026 {
1027 wxDocument *doc = GetCurrentDocument();
1028 if (!doc)
1029 event.Enable(FALSE);
1030 else if (!doc->GetCommandProcessor())
1031 event.Skip();
1032 else
1033 {
1034 event.Enable( doc->GetCommandProcessor()->CanUndo() );
1035 doc->GetCommandProcessor()->SetMenuStrings();
1036 }
1037 }
1038
1039 void wxDocManager::OnUpdateRedo(wxUpdateUIEvent& event)
1040 {
1041 wxDocument *doc = GetCurrentDocument();
1042 if (!doc)
1043 event.Enable(FALSE);
1044 else if (!doc->GetCommandProcessor())
1045 event.Skip();
1046 else
1047 {
1048 event.Enable( doc->GetCommandProcessor()->CanRedo() );
1049 doc->GetCommandProcessor()->SetMenuStrings();
1050 }
1051 }
1052
1053 void wxDocManager::OnUpdatePrint(wxUpdateUIEvent& event)
1054 {
1055 wxDocument *doc = GetCurrentDocument();
1056 event.Enable( (doc != (wxDocument*) NULL) );
1057 }
1058
1059 void wxDocManager::OnUpdatePrintSetup(wxUpdateUIEvent& event)
1060 {
1061 event.Enable( TRUE );
1062 }
1063
1064 void wxDocManager::OnUpdatePreview(wxUpdateUIEvent& event)
1065 {
1066 wxDocument *doc = GetCurrentDocument();
1067 event.Enable( (doc != (wxDocument*) NULL) );
1068 }
1069
1070 wxView *wxDocManager::GetCurrentView() const
1071 {
1072 if (m_currentView)
1073 return m_currentView;
1074 if (m_docs.GetCount() == 1)
1075 {
1076 wxDocument* doc = (wxDocument*) m_docs.GetFirst()->GetData();
1077 return doc->GetFirstView();
1078 }
1079 return (wxView *) NULL;
1080 }
1081
1082 // Extend event processing to search the view's event table
1083 bool wxDocManager::ProcessEvent(wxEvent& event)
1084 {
1085 wxView* view = GetCurrentView();
1086 if (view)
1087 {
1088 if (view->ProcessEvent(event))
1089 return TRUE;
1090 }
1091 return wxEvtHandler::ProcessEvent(event);
1092 }
1093
1094 wxDocument *wxDocManager::CreateDocument(const wxString& path, long flags)
1095 {
1096 wxDocTemplate **templates = new wxDocTemplate *[m_templates.GetCount()];
1097 int n = 0;
1098
1099 for (size_t i = 0; i < m_templates.GetCount(); i++)
1100 {
1101 wxDocTemplate *temp = (wxDocTemplate *)(m_templates.Item(i)->GetData());
1102 if (temp->IsVisible())
1103 {
1104 templates[n] = temp;
1105 n ++;
1106 }
1107 }
1108 if (n == 0)
1109 {
1110 delete[] templates;
1111 return (wxDocument *) NULL;
1112 }
1113
1114 wxDocument* docToClose = NULL;
1115
1116 // If we've reached the max number of docs, close the
1117 // first one.
1118 if ( (int)GetDocuments().GetCount() >= m_maxDocsOpen )
1119 {
1120 wxDocument *doc = (wxDocument *)GetDocuments().GetFirst()->GetData();
1121 docToClose = doc;
1122 }
1123
1124 // New document: user chooses a template, unless there's only one.
1125 if (flags & wxDOC_NEW)
1126 {
1127 if (n == 1)
1128 {
1129 if (docToClose)
1130 {
1131 if (!CloseDocument(docToClose, FALSE))
1132 {
1133 delete[] templates;
1134 return NULL;
1135 }
1136 }
1137
1138 wxDocTemplate *temp = templates[0];
1139 delete[] templates;
1140 wxDocument *newDoc = temp->CreateDocument(path, flags);
1141
1142 if (newDoc)
1143 {
1144 newDoc->SetDocumentName(temp->GetDocumentName());
1145 newDoc->SetDocumentTemplate(temp);
1146 newDoc->OnNewDocument();
1147 }
1148 return newDoc;
1149 }
1150
1151 wxDocTemplate *temp = SelectDocumentType(templates, n);
1152 delete[] templates;
1153 if (temp)
1154 {
1155 if (docToClose)
1156 {
1157 if (!CloseDocument(docToClose, FALSE))
1158 {
1159 return NULL;
1160 }
1161 }
1162
1163 wxDocument *newDoc = temp->CreateDocument(path, flags);
1164
1165 if (newDoc)
1166 {
1167 newDoc->SetDocumentName(temp->GetDocumentName());
1168 newDoc->SetDocumentTemplate(temp);
1169 newDoc->OnNewDocument();
1170 }
1171 return newDoc;
1172 }
1173 else
1174 return (wxDocument *) NULL;
1175 }
1176
1177 // Existing document
1178 wxDocTemplate *temp = (wxDocTemplate *) NULL;
1179
1180 wxString path2(wxT(""));
1181 if (path != wxT(""))
1182 path2 = path;
1183
1184 if (flags & wxDOC_SILENT)
1185 {
1186 temp = FindTemplateForPath(path2);
1187 if (!temp)
1188 {
1189 // Since we do not add files with non-default extensions to the FileHistory this
1190 // can only happen if the application changes the allowed templates in runtime.
1191 (void)wxMessageBox(_("Sorry, the format for this file is unknown."),
1192 _("Open File"),
1193 wxOK | wxICON_EXCLAMATION, wxFindSuitableParent());
1194 }
1195 }
1196 else
1197 temp = SelectDocumentPath(templates, n, path2, flags);
1198
1199 delete[] templates;
1200
1201 if (temp)
1202 {
1203 if (docToClose)
1204 {
1205 if (!CloseDocument(docToClose, FALSE))
1206 {
1207 return NULL;
1208 }
1209 }
1210
1211 wxDocument *newDoc = temp->CreateDocument(path2, flags);
1212 if (newDoc)
1213 {
1214 newDoc->SetDocumentName(temp->GetDocumentName());
1215 newDoc->SetDocumentTemplate(temp);
1216 if (!newDoc->OnOpenDocument(path2))
1217 {
1218 newDoc->DeleteAllViews();
1219 // delete newDoc; // Implicitly deleted by DeleteAllViews
1220 return (wxDocument *) NULL;
1221 }
1222 // A file that doesn't use the default extension of its document
1223 // template cannot be opened via the FileHistory, so we do not
1224 // add it.
1225 if (temp->FileMatchesTemplate(path2))
1226 AddFileToHistory(path2);
1227 }
1228 return newDoc;
1229 }
1230
1231 return (wxDocument *) NULL;
1232 }
1233
1234 wxView *wxDocManager::CreateView(wxDocument *doc, long flags)
1235 {
1236 wxDocTemplate **templates = new wxDocTemplate *[m_templates.GetCount()];
1237 int n =0;
1238
1239 for (size_t i = 0; i < m_templates.GetCount(); i++)
1240 {
1241 wxDocTemplate *temp = (wxDocTemplate *)(m_templates.Item(i)->GetData());
1242 if (temp->IsVisible())
1243 {
1244 if (temp->GetDocumentName() == doc->GetDocumentName())
1245 {
1246 templates[n] = temp;
1247 n ++;
1248 }
1249 }
1250 }
1251 if (n == 0)
1252 {
1253 delete[] templates;
1254 return (wxView *) NULL;
1255 }
1256 if (n == 1)
1257 {
1258 wxDocTemplate *temp = templates[0];
1259 delete[] templates;
1260 wxView *view = temp->CreateView(doc, flags);
1261 if (view)
1262 view->SetViewName(temp->GetViewName());
1263 return view;
1264 }
1265
1266 wxDocTemplate *temp = SelectViewType(templates, n);
1267 delete[] templates;
1268 if (temp)
1269 {
1270 wxView *view = temp->CreateView(doc, flags);
1271 if (view)
1272 view->SetViewName(temp->GetViewName());
1273 return view;
1274 }
1275 else
1276 return (wxView *) NULL;
1277 }
1278
1279 // Not yet implemented
1280 void wxDocManager::DeleteTemplate(wxDocTemplate *WXUNUSED(temp), long WXUNUSED(flags))
1281 {
1282 }
1283
1284 // Not yet implemented
1285 bool wxDocManager::FlushDoc(wxDocument *WXUNUSED(doc))
1286 {
1287 return FALSE;
1288 }
1289
1290 wxDocument *wxDocManager::GetCurrentDocument() const
1291 {
1292 wxView *view = GetCurrentView();
1293 if (view)
1294 return view->GetDocument();
1295 else
1296 return (wxDocument *) NULL;
1297 }
1298
1299 // Make a default document name
1300 bool wxDocManager::MakeDefaultName(wxString& name)
1301 {
1302 name.Printf(_("unnamed%d"), m_defaultDocumentNameCounter);
1303 m_defaultDocumentNameCounter++;
1304
1305 return TRUE;
1306 }
1307
1308 // Make a frame title (override this to do something different)
1309 // If docName is empty, a document is not currently active.
1310 wxString wxDocManager::MakeFrameTitle(wxDocument* doc)
1311 {
1312 wxString appName = wxTheApp->GetAppName();
1313 wxString title;
1314 if (!doc)
1315 title = appName;
1316 else
1317 {
1318 wxString docName;
1319 doc->GetPrintableName(docName);
1320 title = docName + wxString(_(" - ")) + appName;
1321 }
1322 return title;
1323 }
1324
1325
1326 // Not yet implemented
1327 wxDocTemplate *wxDocManager::MatchTemplate(const wxString& WXUNUSED(path))
1328 {
1329 return (wxDocTemplate *) NULL;
1330 }
1331
1332 // File history management
1333 void wxDocManager::AddFileToHistory(const wxString& file)
1334 {
1335 if (m_fileHistory)
1336 m_fileHistory->AddFileToHistory(file);
1337 }
1338
1339 void wxDocManager::RemoveFileFromHistory(size_t i)
1340 {
1341 if (m_fileHistory)
1342 m_fileHistory->RemoveFileFromHistory(i);
1343 }
1344
1345 wxString wxDocManager::GetHistoryFile(size_t i) const
1346 {
1347 wxString histFile;
1348
1349 if (m_fileHistory)
1350 histFile = m_fileHistory->GetHistoryFile(i);
1351
1352 return histFile;
1353 }
1354
1355 void wxDocManager::FileHistoryUseMenu(wxMenu *menu)
1356 {
1357 if (m_fileHistory)
1358 m_fileHistory->UseMenu(menu);
1359 }
1360
1361 void wxDocManager::FileHistoryRemoveMenu(wxMenu *menu)
1362 {
1363 if (m_fileHistory)
1364 m_fileHistory->RemoveMenu(menu);
1365 }
1366
1367 #if wxUSE_CONFIG
1368 void wxDocManager::FileHistoryLoad(wxConfigBase& config)
1369 {
1370 if (m_fileHistory)
1371 m_fileHistory->Load(config);
1372 }
1373
1374 void wxDocManager::FileHistorySave(wxConfigBase& config)
1375 {
1376 if (m_fileHistory)
1377 m_fileHistory->Save(config);
1378 }
1379 #endif
1380
1381 void wxDocManager::FileHistoryAddFilesToMenu(wxMenu* menu)
1382 {
1383 if (m_fileHistory)
1384 m_fileHistory->AddFilesToMenu(menu);
1385 }
1386
1387 void wxDocManager::FileHistoryAddFilesToMenu()
1388 {
1389 if (m_fileHistory)
1390 m_fileHistory->AddFilesToMenu();
1391 }
1392
1393 size_t wxDocManager::GetHistoryFilesCount() const
1394 {
1395 return m_fileHistory ? m_fileHistory->GetCount() : 0;
1396 }
1397
1398
1399 // Find out the document template via matching in the document file format
1400 // against that of the template
1401 wxDocTemplate *wxDocManager::FindTemplateForPath(const wxString& path)
1402 {
1403 wxDocTemplate *theTemplate = (wxDocTemplate *) NULL;
1404
1405 // Find the template which this extension corresponds to
1406 for (size_t i = 0; i < m_templates.GetCount(); i++)
1407 {
1408 wxDocTemplate *temp = (wxDocTemplate *)m_templates.Item(i)->GetData();
1409 if ( temp->FileMatchesTemplate(path) )
1410 {
1411 theTemplate = temp;
1412 break;
1413 }
1414 }
1415 return theTemplate;
1416 }
1417
1418 // Try to get a more suitable parent frame than the top window,
1419 // for selection dialogs. Otherwise you may get an unexpected
1420 // window being activated when a dialog is shown.
1421 static wxWindow* wxFindSuitableParent()
1422 {
1423 wxWindow* parent = wxTheApp->GetTopWindow();
1424
1425 wxWindow* focusWindow = wxWindow::FindFocus();
1426 if (focusWindow)
1427 {
1428 while (focusWindow &&
1429 !focusWindow->IsKindOf(CLASSINFO(wxDialog)) &&
1430 !focusWindow->IsKindOf(CLASSINFO(wxFrame)))
1431
1432 focusWindow = focusWindow->GetParent();
1433
1434 if (focusWindow)
1435 parent = focusWindow;
1436 }
1437 return parent;
1438 }
1439
1440 // Prompts user to open a file, using file specs in templates.
1441 // Must extend the file selector dialog or implement own; OR
1442 // match the extension to the template extension.
1443
1444 wxDocTemplate *wxDocManager::SelectDocumentPath(wxDocTemplate **templates,
1445 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
1446 int noTemplates,
1447 #else
1448 int WXUNUSED(noTemplates),
1449 #endif
1450 wxString& path,
1451 long WXUNUSED(flags),
1452 bool WXUNUSED(save))
1453 {
1454 // We can only have multiple filters in Windows and GTK
1455 #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
1456 wxString descrBuf;
1457
1458 int i;
1459 for (i = 0; i < noTemplates; i++)
1460 {
1461 if (templates[i]->IsVisible())
1462 {
1463 // add a '|' to separate this filter from the previous one
1464 if ( !descrBuf.IsEmpty() )
1465 descrBuf << wxT('|');
1466
1467 descrBuf << templates[i]->GetDescription()
1468 << wxT(" (") << templates[i]->GetFileFilter() << wxT(") |")
1469 << templates[i]->GetFileFilter();
1470 }
1471 }
1472 #else
1473 wxString descrBuf = wxT("*.*");
1474 #endif
1475
1476 int FilterIndex = -1;
1477
1478 wxWindow* parent = wxFindSuitableParent();
1479
1480 wxString pathTmp = wxFileSelectorEx(_("Select a file"),
1481 m_lastDirectory,
1482 wxT(""),
1483 &FilterIndex,
1484 descrBuf,
1485 0,
1486 parent);
1487
1488 wxDocTemplate *theTemplate = (wxDocTemplate *)NULL;
1489 if (!pathTmp.IsEmpty())
1490 {
1491 if (!wxFileExists(pathTmp))
1492 {
1493 wxString msgTitle;
1494 if (!wxTheApp->GetAppName().IsEmpty())
1495 msgTitle = wxTheApp->GetAppName();
1496 else
1497 msgTitle = wxString(_("File error"));
1498
1499 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle, wxOK | wxICON_EXCLAMATION,
1500 parent);
1501
1502 path = wxT("");
1503 return (wxDocTemplate *) NULL;
1504 }
1505 m_lastDirectory = wxPathOnly(pathTmp);
1506
1507 path = pathTmp;
1508
1509 // first choose the template using the extension, if this fails (i.e.
1510 // wxFileSelectorEx() didn't fill it), then use the path
1511 if ( FilterIndex != -1 )
1512 theTemplate = templates[FilterIndex];
1513 if ( !theTemplate )
1514 theTemplate = FindTemplateForPath(path);
1515 if ( !theTemplate )
1516 {
1517 // Since we do not add files with non-default extensions to the FileHistory this
1518 // can only happen if the application changes the allowed templates in runtime.
1519 (void)wxMessageBox(_("Sorry, the format for this file is unknown."),
1520 _("Open File"),
1521 wxOK | wxICON_EXCLAMATION, wxFindSuitableParent());
1522 }
1523 }
1524 else
1525 {
1526 path = wxT("");
1527 }
1528
1529 return theTemplate;
1530
1531 #if 0
1532 // In all other windowing systems, until we have more advanced
1533 // file selectors, we must select the document type (template) first, and
1534 // _then_ pop up the file selector.
1535 wxDocTemplate *temp = SelectDocumentType(templates, noTemplates);
1536 if (!temp)
1537 return (wxDocTemplate *) NULL;
1538
1539 wxChar *pathTmp = wxFileSelector(_("Select a file"), wxT(""), wxT(""),
1540 temp->GetDefaultExtension(),
1541 temp->GetFileFilter(),
1542 0, wxTheApp->GetTopWindow());
1543
1544 if (pathTmp)
1545 {
1546 path = pathTmp;
1547 return temp;
1548 }
1549 else
1550 return (wxDocTemplate *) NULL;
1551 #endif // 0
1552 }
1553
1554 wxDocTemplate *wxDocManager::SelectDocumentType(wxDocTemplate **templates,
1555 int noTemplates, bool sort)
1556 {
1557 wxArrayString strings(sort);
1558 wxDocTemplate **data = new wxDocTemplate *[noTemplates];
1559 int i;
1560 int n = 0;
1561
1562 for (i = 0; i < noTemplates; i++)
1563 {
1564 if (templates[i]->IsVisible())
1565 {
1566 int j;
1567 bool want = TRUE;
1568 for (j = 0; j < n; j++)
1569 {
1570 //filter out NOT unique documents + view combinations
1571 if ( templates[i]->m_docTypeName == data[j]->m_docTypeName &&
1572 templates[i]->m_viewTypeName == data[j]->m_viewTypeName
1573 )
1574 want = FALSE;
1575 }
1576
1577 if ( want )
1578 {
1579 strings.Add(templates[i]->m_description);
1580
1581 data[n] = templates[i];
1582 n ++;
1583 }
1584 }
1585 } // for
1586
1587 if (sort)
1588 {
1589 // Yes, this will be slow, but template lists
1590 // are typically short.
1591 int j;
1592 n = strings.Count();
1593 for (i = 0; i < n; i++)
1594 {
1595 for (j = 0; j < noTemplates; j++)
1596 {
1597 if (strings[i] == templates[j]->m_description)
1598 data[i] = templates[j];
1599 }
1600 }
1601 }
1602
1603 wxDocTemplate *theTemplate;
1604
1605 switch ( n )
1606 {
1607 case 0:
1608 // no visible templates, hence nothing to choose from
1609 theTemplate = NULL;
1610 break;
1611
1612 case 1:
1613 // don't propose the user to choose if he heas no choice
1614 theTemplate = data[0];
1615 break;
1616
1617 default:
1618 // propose the user to choose one of several
1619 theTemplate = (wxDocTemplate *)wxGetSingleChoiceData
1620 (
1621 _("Select a document template"),
1622 _("Templates"),
1623 strings,
1624 (void **)data,
1625 wxFindSuitableParent()
1626 );
1627 }
1628
1629 delete[] data;
1630
1631 return theTemplate;
1632 }
1633
1634 wxDocTemplate *wxDocManager::SelectViewType(wxDocTemplate **templates,
1635 int noTemplates, bool sort)
1636 {
1637 wxArrayString strings(sort);
1638 wxDocTemplate **data = new wxDocTemplate *[noTemplates];
1639 int i;
1640 int n = 0;
1641
1642 for (i = 0; i < noTemplates; i++)
1643 {
1644 wxDocTemplate *templ = templates[i];
1645 if ( templ->IsVisible() && !templ->GetViewName().empty() )
1646 {
1647 int j;
1648 bool want = TRUE;
1649 for (j = 0; j < n; j++)
1650 {
1651 //filter out NOT unique views
1652 if ( templates[i]->m_viewTypeName == data[j]->m_viewTypeName )
1653 want = FALSE;
1654 }
1655
1656 if ( want )
1657 {
1658 strings.Add(templ->m_viewTypeName);
1659 data[n] = templ;
1660 n ++;
1661 }
1662 }
1663 }
1664
1665 if (sort)
1666 {
1667 // Yes, this will be slow, but template lists
1668 // are typically short.
1669 int j;
1670 n = strings.Count();
1671 for (i = 0; i < n; i++)
1672 {
1673 for (j = 0; j < noTemplates; j++)
1674 {
1675 if (strings[i] == templates[j]->m_viewTypeName)
1676 data[i] = templates[j];
1677 }
1678 }
1679 }
1680
1681 wxDocTemplate *theTemplate;
1682
1683 // the same logic as above
1684 switch ( n )
1685 {
1686 case 0:
1687 theTemplate = (wxDocTemplate *)NULL;
1688 break;
1689
1690 case 1:
1691 theTemplate = data[0];
1692 break;
1693
1694 default:
1695 theTemplate = (wxDocTemplate *)wxGetSingleChoiceData
1696 (
1697 _("Select a document view"),
1698 _("Views"),
1699 strings,
1700 (void **)data,
1701 wxFindSuitableParent()
1702 );
1703
1704 }
1705
1706 delete[] data;
1707 return theTemplate;
1708 }
1709
1710 void wxDocManager::AssociateTemplate(wxDocTemplate *temp)
1711 {
1712 if (!m_templates.Member(temp))
1713 m_templates.Append(temp);
1714 }
1715
1716 void wxDocManager::DisassociateTemplate(wxDocTemplate *temp)
1717 {
1718 m_templates.DeleteObject(temp);
1719 }
1720
1721 // Add and remove a document from the manager's list
1722 void wxDocManager::AddDocument(wxDocument *doc)
1723 {
1724 if (!m_docs.Member(doc))
1725 m_docs.Append(doc);
1726 }
1727
1728 void wxDocManager::RemoveDocument(wxDocument *doc)
1729 {
1730 m_docs.DeleteObject(doc);
1731 }
1732
1733 // Views or windows should inform the document manager
1734 // when a view is going in or out of focus
1735 void wxDocManager::ActivateView(wxView *view, bool activate, bool WXUNUSED(deleting))
1736 {
1737 // If we're deactiving, and if we're not actually deleting the view, then
1738 // don't reset the current view because we may be going to
1739 // a window without a view.
1740 // WHAT DID I MEAN BY THAT EXACTLY?
1741 /*
1742 if (deleting)
1743 {
1744 if (m_currentView == view)
1745 m_currentView = NULL;
1746 }
1747 else
1748 */
1749 {
1750 if (activate)
1751 m_currentView = view;
1752 else
1753 m_currentView = (wxView *) NULL;
1754 }
1755 }
1756
1757 // ----------------------------------------------------------------------------
1758 // Default document child frame
1759 // ----------------------------------------------------------------------------
1760
1761 BEGIN_EVENT_TABLE(wxDocChildFrame, wxFrame)
1762 EVT_ACTIVATE(wxDocChildFrame::OnActivate)
1763 EVT_CLOSE(wxDocChildFrame::OnCloseWindow)
1764 END_EVENT_TABLE()
1765
1766 wxDocChildFrame::wxDocChildFrame(wxDocument *doc,
1767 wxView *view,
1768 wxFrame *frame,
1769 wxWindowID id,
1770 const wxString& title,
1771 const wxPoint& pos,
1772 const wxSize& size,
1773 long style,
1774 const wxString& name)
1775 : wxFrame(frame, id, title, pos, size, style, name)
1776 {
1777 m_childDocument = doc;
1778 m_childView = view;
1779 if (view)
1780 view->SetFrame(this);
1781 }
1782
1783 wxDocChildFrame::~wxDocChildFrame()
1784 {
1785 }
1786
1787 // Extend event processing to search the view's event table
1788 bool wxDocChildFrame::ProcessEvent(wxEvent& event)
1789 {
1790 if (m_childView)
1791 m_childView->Activate(TRUE);
1792
1793 if ( !m_childView || ! m_childView->ProcessEvent(event) )
1794 {
1795 // Only hand up to the parent if it's a menu command
1796 if (!event.IsKindOf(CLASSINFO(wxCommandEvent)) || !GetParent() || !GetParent()->ProcessEvent(event))
1797 return wxEvtHandler::ProcessEvent(event);
1798 else
1799 return TRUE;
1800 }
1801 else
1802 return TRUE;
1803 }
1804
1805 void wxDocChildFrame::OnActivate(wxActivateEvent& event)
1806 {
1807 wxFrame::OnActivate(event);
1808
1809 if (m_childView)
1810 m_childView->Activate(event.GetActive());
1811 }
1812
1813 void wxDocChildFrame::OnCloseWindow(wxCloseEvent& event)
1814 {
1815 if (m_childView)
1816 {
1817 bool ans = FALSE;
1818 if (!event.CanVeto())
1819 ans = TRUE; // Must delete.
1820 else
1821 ans = m_childView->Close(FALSE); // FALSE means don't delete associated window
1822
1823 if (ans)
1824 {
1825 m_childView->Activate(FALSE);
1826 delete m_childView;
1827 m_childView = (wxView *) NULL;
1828 m_childDocument = (wxDocument *) NULL;
1829
1830 this->Destroy();
1831 }
1832 else
1833 event.Veto();
1834 }
1835 else
1836 event.Veto();
1837 }
1838
1839 // ----------------------------------------------------------------------------
1840 // Default parent frame
1841 // ----------------------------------------------------------------------------
1842
1843 BEGIN_EVENT_TABLE(wxDocParentFrame, wxFrame)
1844 EVT_MENU(wxID_EXIT, wxDocParentFrame::OnExit)
1845 EVT_MENU_RANGE(wxID_FILE1, wxID_FILE9, wxDocParentFrame::OnMRUFile)
1846 EVT_CLOSE(wxDocParentFrame::OnCloseWindow)
1847 END_EVENT_TABLE()
1848
1849 wxDocParentFrame::wxDocParentFrame(wxDocManager *manager,
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_docManager = manager;
1860 }
1861
1862 void wxDocParentFrame::OnExit(wxCommandEvent& WXUNUSED(event))
1863 {
1864 Close();
1865 }
1866
1867 void wxDocParentFrame::OnMRUFile(wxCommandEvent& event)
1868 {
1869 int n = event.GetId() - wxID_FILE1; // the index in MRU list
1870 wxString filename(m_docManager->GetHistoryFile(n));
1871 if ( !filename.IsEmpty() )
1872 {
1873 // verify that the file exists before doing anything else
1874 if ( wxFile::Exists(filename) )
1875 {
1876 // try to open it
1877 if (!m_docManager->CreateDocument(filename, wxDOC_SILENT))
1878 {
1879 // remove the file from the MRU list. The user should already be notified.
1880 m_docManager->RemoveFileFromHistory(n);
1881
1882 wxLogError(_("The file '%s' couldn't be opened.\nIt has been removed from the most recently used files list."),
1883 filename.c_str());
1884 }
1885 }
1886 else
1887 {
1888 // remove the bogus filename from the MRU list and notify the user
1889 // about it
1890 m_docManager->RemoveFileFromHistory(n);
1891
1892 wxLogError(_("The file '%s' doesn't exist and couldn't be opened.\nIt has been removed from the most recently used files list."),
1893 filename.c_str());
1894 }
1895 }
1896 }
1897
1898 // Extend event processing to search the view's event table
1899 bool wxDocParentFrame::ProcessEvent(wxEvent& event)
1900 {
1901 // Try the document manager, then do default processing
1902 if (!m_docManager || !m_docManager->ProcessEvent(event))
1903 return wxEvtHandler::ProcessEvent(event);
1904 else
1905 return TRUE;
1906 }
1907
1908 // Define the behaviour for the frame closing
1909 // - must delete all frames except for the main one.
1910 void wxDocParentFrame::OnCloseWindow(wxCloseEvent& event)
1911 {
1912 if (m_docManager->Clear(!event.CanVeto()))
1913 {
1914 this->Destroy();
1915 }
1916 else
1917 event.Veto();
1918 }
1919
1920 #if wxUSE_PRINTING_ARCHITECTURE
1921
1922 wxDocPrintout::wxDocPrintout(wxView *view, const wxString& title)
1923 : wxPrintout(title)
1924 {
1925 m_printoutView = view;
1926 }
1927
1928 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page))
1929 {
1930 wxDC *dc = GetDC();
1931
1932 // Get the logical pixels per inch of screen and printer
1933 int ppiScreenX, ppiScreenY;
1934 GetPPIScreen(&ppiScreenX, &ppiScreenY);
1935 int ppiPrinterX, ppiPrinterY;
1936 GetPPIPrinter(&ppiPrinterX, &ppiPrinterY);
1937
1938 // This scales the DC so that the printout roughly represents the
1939 // the screen scaling. The text point size _should_ be the right size
1940 // but in fact is too small for some reason. This is a detail that will
1941 // need to be addressed at some point but can be fudged for the
1942 // moment.
1943 float scale = (float)((float)ppiPrinterX/(float)ppiScreenX);
1944
1945 // Now we have to check in case our real page size is reduced
1946 // (e.g. because we're drawing to a print preview memory DC)
1947 int pageWidth, pageHeight;
1948 int w, h;
1949 dc->GetSize(&w, &h);
1950 GetPageSizePixels(&pageWidth, &pageHeight);
1951
1952 // If printer pageWidth == current DC width, then this doesn't
1953 // change. But w might be the preview bitmap width, so scale down.
1954 float overallScale = scale * (float)(w/(float)pageWidth);
1955 dc->SetUserScale(overallScale, overallScale);
1956
1957 if (m_printoutView)
1958 {
1959 m_printoutView->OnDraw(dc);
1960 }
1961 return TRUE;
1962 }
1963
1964 bool wxDocPrintout::HasPage(int pageNum)
1965 {
1966 return (pageNum == 1);
1967 }
1968
1969 bool wxDocPrintout::OnBeginDocument(int startPage, int endPage)
1970 {
1971 if (!wxPrintout::OnBeginDocument(startPage, endPage))
1972 return FALSE;
1973
1974 return TRUE;
1975 }
1976
1977 void wxDocPrintout::GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo)
1978 {
1979 *minPage = 1;
1980 *maxPage = 1;
1981 *selPageFrom = 1;
1982 *selPageTo = 1;
1983 }
1984
1985 #endif // wxUSE_PRINTING_ARCHITECTURE
1986
1987 // ----------------------------------------------------------------------------
1988 // File history processor
1989 // ----------------------------------------------------------------------------
1990
1991 static inline wxChar* MYcopystring(const wxString& s)
1992 {
1993 wxChar* copy = new wxChar[s.length() + 1];
1994 return wxStrcpy(copy, s.c_str());
1995 }
1996
1997 static inline wxChar* MYcopystring(const wxChar* s)
1998 {
1999 wxChar* copy = new wxChar[wxStrlen(s) + 1];
2000 return wxStrcpy(copy, s);
2001 }
2002
2003 wxFileHistory::wxFileHistory(size_t maxFiles, wxWindowID idBase)
2004 {
2005 m_fileMaxFiles = maxFiles;
2006 m_idBase = idBase;
2007 m_fileHistoryN = 0;
2008 m_fileHistory = new wxChar *[m_fileMaxFiles];
2009 }
2010
2011 wxFileHistory::~wxFileHistory()
2012 {
2013 size_t i;
2014 for (i = 0; i < m_fileHistoryN; i++)
2015 delete[] m_fileHistory[i];
2016 delete[] m_fileHistory;
2017 }
2018
2019 // File history management
2020 void wxFileHistory::AddFileToHistory(const wxString& file)
2021 {
2022 size_t i;
2023
2024 // Check we don't already have this file
2025 for (i = 0; i < m_fileHistoryN; i++)
2026 {
2027 #if defined( __WXMSW__ ) // Add any other OSes with case insensitive file names
2028 wxString testString;
2029 if ( m_fileHistory[i] )
2030 testString = m_fileHistory[i];
2031 if ( m_fileHistory[i] && ( file.Lower() == testString.Lower() ) )
2032 #else
2033 if ( m_fileHistory[i] && ( file == m_fileHistory[i] ) )
2034 #endif
2035 {
2036 // we do have it, move it to the top of the history
2037 RemoveFileFromHistory (i);
2038 AddFileToHistory (file);
2039 return;
2040 }
2041 }
2042
2043 // if we already have a full history, delete the one at the end
2044 if ( m_fileMaxFiles == m_fileHistoryN )
2045 {
2046 RemoveFileFromHistory (m_fileHistoryN - 1);
2047 AddFileToHistory (file);
2048 return;
2049 }
2050
2051 // Add to the project file history:
2052 // Move existing files (if any) down so we can insert file at beginning.
2053 if (m_fileHistoryN < m_fileMaxFiles)
2054 {
2055 wxNode* node = m_fileMenus.GetFirst();
2056 while (node)
2057 {
2058 wxMenu* menu = (wxMenu*) node->GetData();
2059 if ( m_fileHistoryN == 0 && menu->GetMenuItemCount() )
2060 {
2061 menu->AppendSeparator();
2062 }
2063 menu->Append(m_idBase+m_fileHistoryN, _("[EMPTY]"));
2064 node = node->GetNext();
2065 }
2066 m_fileHistoryN ++;
2067 }
2068 // Shuffle filenames down
2069 for (i = (m_fileHistoryN-1); i > 0; i--)
2070 {
2071 m_fileHistory[i] = m_fileHistory[i-1];
2072 }
2073 m_fileHistory[0] = MYcopystring(file);
2074
2075 // this is the directory of the last opened file
2076 wxString pathCurrent;
2077 wxSplitPath( m_fileHistory[0], &pathCurrent, NULL, NULL );
2078 for (i = 0; i < m_fileHistoryN; i++)
2079 {
2080 if ( m_fileHistory[i] )
2081 {
2082 // if in same directory just show the filename; otherwise the full
2083 // path
2084 wxString pathInMenu, path, filename, ext;
2085 wxSplitPath( m_fileHistory[i], &path, &filename, &ext );
2086 if ( path == pathCurrent )
2087 {
2088 pathInMenu = filename;
2089 if ( !ext.empty() )
2090 pathInMenu = pathInMenu + wxFILE_SEP_EXT + ext;
2091 }
2092 else
2093 {
2094 // absolute path; could also set relative path
2095 pathInMenu = m_fileHistory[i];
2096 }
2097
2098 wxString buf;
2099 buf.Printf(s_MRUEntryFormat, i + 1, pathInMenu.c_str());
2100 wxNode* node = m_fileMenus.GetFirst();
2101 while (node)
2102 {
2103 wxMenu* menu = (wxMenu*) node->GetData();
2104 menu->SetLabel(m_idBase + i, buf);
2105 node = node->GetNext();
2106 }
2107 }
2108 }
2109 }
2110
2111 void wxFileHistory::RemoveFileFromHistory(size_t i)
2112 {
2113 wxCHECK_RET( i < m_fileHistoryN,
2114 wxT("invalid index in wxFileHistory::RemoveFileFromHistory") );
2115
2116 // delete the element from the array (could use memmove() too...)
2117 delete [] m_fileHistory[i];
2118
2119 size_t j;
2120 for ( j = i; j < m_fileHistoryN - 1; j++ )
2121 {
2122 m_fileHistory[j] = m_fileHistory[j + 1];
2123 }
2124
2125 wxNode* node = m_fileMenus.GetFirst();
2126 while ( node )
2127 {
2128 wxMenu* menu = (wxMenu*) node->GetData();
2129
2130 // shuffle filenames up
2131 wxString buf;
2132 for ( j = i; j < m_fileHistoryN - 1; j++ )
2133 {
2134 buf.Printf(s_MRUEntryFormat, j + 1, m_fileHistory[j]);
2135 menu->SetLabel(m_idBase + j, buf);
2136 }
2137
2138 node = node->GetNext();
2139
2140 // delete the last menu item which is unused now
2141 wxWindowID lastItemId = m_idBase + m_fileHistoryN - 1;
2142 if (menu->FindItem(lastItemId))
2143 {
2144 menu->Delete(lastItemId);
2145 }
2146
2147 // delete the last separator too if no more files are left
2148 if ( m_fileHistoryN == 1 )
2149 {
2150 wxMenuItemList::Node *node = menu->GetMenuItems().GetLast();
2151 if ( node )
2152 {
2153 wxMenuItem *menuItem = node->GetData();
2154 if ( menuItem->IsSeparator() )
2155 {
2156 menu->Delete(menuItem);
2157 }
2158 //else: should we search backwards for the last separator?
2159 }
2160 //else: menu is empty somehow
2161 }
2162 }
2163
2164 m_fileHistoryN--;
2165 }
2166
2167 wxString wxFileHistory::GetHistoryFile(size_t i) const
2168 {
2169 wxString s;
2170 if ( i < m_fileHistoryN )
2171 {
2172 s = m_fileHistory[i];
2173 }
2174 else
2175 {
2176 wxFAIL_MSG( wxT("bad index in wxFileHistory::GetHistoryFile") );
2177 }
2178
2179 return s;
2180 }
2181
2182 void wxFileHistory::UseMenu(wxMenu *menu)
2183 {
2184 if (!m_fileMenus.Member(menu))
2185 m_fileMenus.Append(menu);
2186 }
2187
2188 void wxFileHistory::RemoveMenu(wxMenu *menu)
2189 {
2190 m_fileMenus.DeleteObject(menu);
2191 }
2192
2193 #if wxUSE_CONFIG
2194 void wxFileHistory::Load(wxConfigBase& config)
2195 {
2196 m_fileHistoryN = 0;
2197 wxString buf;
2198 buf.Printf(wxT("file%d"), (int)m_fileHistoryN+1);
2199 wxString historyFile;
2200 while ((m_fileHistoryN < m_fileMaxFiles) && config.Read(buf, &historyFile) && (historyFile != wxT("")))
2201 {
2202 m_fileHistory[m_fileHistoryN] = MYcopystring((const wxChar*) historyFile);
2203 m_fileHistoryN ++;
2204 buf.Printf(wxT("file%d"), (int)m_fileHistoryN+1);
2205 historyFile = wxT("");
2206 }
2207 AddFilesToMenu();
2208 }
2209
2210 void wxFileHistory::Save(wxConfigBase& config)
2211 {
2212 size_t i;
2213 for (i = 0; i < m_fileMaxFiles; i++)
2214 {
2215 wxString buf;
2216 buf.Printf(wxT("file%d"), (int)i+1);
2217 if (i < m_fileHistoryN)
2218 config.Write(buf, wxString(m_fileHistory[i]));
2219 else
2220 config.Write(buf, wxEmptyString);
2221 }
2222 }
2223 #endif // wxUSE_CONFIG
2224
2225 void wxFileHistory::AddFilesToMenu()
2226 {
2227 if (m_fileHistoryN > 0)
2228 {
2229 wxNode* node = m_fileMenus.GetFirst();
2230 while (node)
2231 {
2232 wxMenu* menu = (wxMenu*) node->GetData();
2233 if (menu->GetMenuItemCount())
2234 {
2235 menu->AppendSeparator();
2236 }
2237
2238 size_t i;
2239 for (i = 0; i < m_fileHistoryN; i++)
2240 {
2241 if (m_fileHistory[i])
2242 {
2243 wxString buf;
2244 buf.Printf(s_MRUEntryFormat, i+1, m_fileHistory[i]);
2245 menu->Append(m_idBase+i, buf);
2246 }
2247 }
2248 node = node->GetNext();
2249 }
2250 }
2251 }
2252
2253 void wxFileHistory::AddFilesToMenu(wxMenu* menu)
2254 {
2255 if (m_fileHistoryN > 0)
2256 {
2257 if (menu->GetMenuItemCount())
2258 {
2259 menu->AppendSeparator();
2260 }
2261
2262 size_t i;
2263 for (i = 0; i < m_fileHistoryN; i++)
2264 {
2265 if (m_fileHistory[i])
2266 {
2267 wxString buf;
2268 buf.Printf(s_MRUEntryFormat, i+1, m_fileHistory[i]);
2269 menu->Append(m_idBase+i, buf);
2270 }
2271 }
2272 }
2273 }
2274
2275 // ----------------------------------------------------------------------------
2276 // Permits compatibility with existing file formats and functions that
2277 // manipulate files directly
2278 // ----------------------------------------------------------------------------
2279
2280 #if wxUSE_STD_IOSTREAM
2281
2282 bool wxTransferFileToStream(const wxString& filename, wxSTD ostream& stream)
2283 {
2284 wxFFile file(filename, _T("rb"));
2285 if ( !file.IsOpened() )
2286 return FALSE;
2287
2288 char buf[4096];
2289
2290 size_t nRead;
2291 do
2292 {
2293 nRead = file.Read(buf, WXSIZEOF(buf));
2294 if ( file.Error() )
2295 return FALSE;
2296
2297 stream.write(buf, nRead);
2298 if ( !stream )
2299 return FALSE;
2300 }
2301 while ( !file.Eof() );
2302
2303 return TRUE;
2304 }
2305
2306 bool wxTransferStreamToFile(wxSTD istream& stream, const wxString& filename)
2307 {
2308 wxFFile file(filename, _T("wb"));
2309 if ( !file.IsOpened() )
2310 return FALSE;
2311
2312 char buf[4096];
2313 do
2314 {
2315 stream.read(buf, WXSIZEOF(buf));
2316 if ( !stream.bad() ) // fail may be set on EOF, don't use operator!()
2317 {
2318 if ( !file.Write(buf, stream.gcount()) )
2319 return FALSE;
2320 }
2321 }
2322 while ( !stream.eof() );
2323
2324 return TRUE;
2325 }
2326
2327 #else // !wxUSE_STD_IOSTREAM
2328
2329 bool wxTransferFileToStream(const wxString& filename, wxOutputStream& stream)
2330 {
2331 wxFFile file(filename, _T("rb"));
2332 if ( !file.IsOpened() )
2333 return FALSE;
2334
2335 char buf[4096];
2336
2337 size_t nRead;
2338 do
2339 {
2340 nRead = file.Read(buf, WXSIZEOF(buf));
2341 if ( file.Error() )
2342 return FALSE;
2343
2344 stream.Write(buf, nRead);
2345 if ( !stream )
2346 return FALSE;
2347 }
2348 while ( !file.Eof() );
2349
2350 return TRUE;
2351 }
2352
2353 bool wxTransferStreamToFile(wxInputStream& stream, const wxString& filename)
2354 {
2355 wxFFile file(filename, _T("wb"));
2356 if ( !file.IsOpened() )
2357 return FALSE;
2358
2359 char buf[4096];
2360 do
2361 {
2362 stream.Read(buf, WXSIZEOF(buf));
2363
2364 const size_t nRead = stream.LastRead();
2365 if ( !nRead || !file.Write(buf, nRead) )
2366 return FALSE;
2367 }
2368 while ( !stream.Eof() );
2369
2370 return TRUE;
2371 }
2372
2373 #endif // wxUSE_STD_IOSTREAM/!wxUSE_STD_IOSTREAM
2374
2375 #endif // wxUSE_DOC_VIEW_ARCHITECTURE
2376