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