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