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