]> git.saurik.com Git - wxWidgets.git/blob - src/common/docview.cpp
allow changing the page from keyboard in property sheet like controls even when the...
[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(wxDocManager *manager,
1927 wxFrame *frame,
1928 wxWindowID id,
1929 const wxString& title,
1930 const wxPoint& pos,
1931 const wxSize& size,
1932 long style,
1933 const wxString& name)
1934 : wxFrame(frame, id, title, pos, size, style, name)
1935 {
1936 m_docManager = manager;
1937 }
1938
1939 void wxDocParentFrame::OnExit(wxCommandEvent& WXUNUSED(event))
1940 {
1941 Close();
1942 }
1943
1944 void wxDocParentFrame::OnMRUFile(wxCommandEvent& event)
1945 {
1946 int n = event.GetId() - wxID_FILE1; // the index in MRU list
1947 wxString filename(m_docManager->GetHistoryFile(n));
1948 if ( !filename.empty() )
1949 {
1950 // verify that the file exists before doing anything else
1951 if ( wxFile::Exists(filename) )
1952 {
1953 // try to open it
1954 if (!m_docManager->CreateDocument(filename, wxDOC_SILENT))
1955 {
1956 // remove the file from the MRU list. The user should already be notified.
1957 m_docManager->RemoveFileFromHistory(n);
1958
1959 wxLogError(_("The file '%s' couldn't be opened.\nIt has been removed from the most recently used files list."),
1960 filename.c_str());
1961 }
1962 }
1963 else
1964 {
1965 // remove the bogus filename from the MRU list and notify the user
1966 // about it
1967 m_docManager->RemoveFileFromHistory(n);
1968
1969 wxLogError(_("The file '%s' doesn't exist and couldn't be opened.\nIt has been removed from the most recently used files list."),
1970 filename.c_str());
1971 }
1972 }
1973 }
1974
1975 // Extend event processing to search the view's event table
1976 bool wxDocParentFrame::ProcessEvent(wxEvent& event)
1977 {
1978 // Try the document manager, then do default processing
1979 if (!m_docManager || !m_docManager->ProcessEvent(event))
1980 return wxEvtHandler::ProcessEvent(event);
1981 else
1982 return true;
1983 }
1984
1985 // Define the behaviour for the frame closing
1986 // - must delete all frames except for the main one.
1987 void wxDocParentFrame::OnCloseWindow(wxCloseEvent& event)
1988 {
1989 if (m_docManager->Clear(!event.CanVeto()))
1990 {
1991 this->Destroy();
1992 }
1993 else
1994 event.Veto();
1995 }
1996
1997 #if wxUSE_PRINTING_ARCHITECTURE
1998
1999 wxDocPrintout::wxDocPrintout(wxView *view, const wxString& title)
2000 : wxPrintout(title)
2001 {
2002 m_printoutView = view;
2003 }
2004
2005 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page))
2006 {
2007 wxDC *dc = GetDC();
2008
2009 // Get the logical pixels per inch of screen and printer
2010 int ppiScreenX, ppiScreenY;
2011 GetPPIScreen(&ppiScreenX, &ppiScreenY);
2012 wxUnusedVar(ppiScreenY);
2013 int ppiPrinterX, ppiPrinterY;
2014 GetPPIPrinter(&ppiPrinterX, &ppiPrinterY);
2015 wxUnusedVar(ppiPrinterY);
2016
2017 // This scales the DC so that the printout roughly represents the
2018 // the screen scaling. The text point size _should_ be the right size
2019 // but in fact is too small for some reason. This is a detail that will
2020 // need to be addressed at some point but can be fudged for the
2021 // moment.
2022 float scale = (float)((float)ppiPrinterX/(float)ppiScreenX);
2023
2024 // Now we have to check in case our real page size is reduced
2025 // (e.g. because we're drawing to a print preview memory DC)
2026 int pageWidth, pageHeight;
2027 int w, h;
2028 dc->GetSize(&w, &h);
2029 GetPageSizePixels(&pageWidth, &pageHeight);
2030 wxUnusedVar(pageHeight);
2031
2032 // If printer pageWidth == current DC width, then this doesn't
2033 // change. But w might be the preview bitmap width, so scale down.
2034 float overallScale = scale * (float)(w/(float)pageWidth);
2035 dc->SetUserScale(overallScale, overallScale);
2036
2037 if (m_printoutView)
2038 {
2039 m_printoutView->OnDraw(dc);
2040 }
2041 return true;
2042 }
2043
2044 bool wxDocPrintout::HasPage(int pageNum)
2045 {
2046 return (pageNum == 1);
2047 }
2048
2049 bool wxDocPrintout::OnBeginDocument(int startPage, int endPage)
2050 {
2051 if (!wxPrintout::OnBeginDocument(startPage, endPage))
2052 return false;
2053
2054 return true;
2055 }
2056
2057 void wxDocPrintout::GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo)
2058 {
2059 *minPage = 1;
2060 *maxPage = 1;
2061 *selPageFrom = 1;
2062 *selPageTo = 1;
2063 }
2064
2065 #endif // wxUSE_PRINTING_ARCHITECTURE
2066
2067 // ----------------------------------------------------------------------------
2068 // File history processor
2069 // ----------------------------------------------------------------------------
2070
2071 static inline wxChar* MYcopystring(const wxString& s)
2072 {
2073 wxChar* copy = new wxChar[s.length() + 1];
2074 return wxStrcpy(copy, s.c_str());
2075 }
2076
2077 static inline wxChar* MYcopystring(const wxChar* s)
2078 {
2079 wxChar* copy = new wxChar[wxStrlen(s) + 1];
2080 return wxStrcpy(copy, s);
2081 }
2082
2083 wxFileHistory::wxFileHistory(size_t maxFiles, wxWindowID idBase)
2084 {
2085 m_fileMaxFiles = maxFiles;
2086 m_idBase = idBase;
2087 m_fileHistoryN = 0;
2088 m_fileHistory = new wxChar *[m_fileMaxFiles];
2089 }
2090
2091 wxFileHistory::~wxFileHistory()
2092 {
2093 size_t i;
2094 for (i = 0; i < m_fileHistoryN; i++)
2095 delete[] m_fileHistory[i];
2096 delete[] m_fileHistory;
2097 }
2098
2099 // File history management
2100 void wxFileHistory::AddFileToHistory(const wxString& file)
2101 {
2102 size_t i;
2103
2104 // Check we don't already have this file
2105 for (i = 0; i < m_fileHistoryN; i++)
2106 {
2107 #if defined( __WXMSW__ ) // Add any other OSes with case insensitive file names
2108 wxString testString;
2109 if ( m_fileHistory[i] )
2110 testString = m_fileHistory[i];
2111 if ( m_fileHistory[i] && ( file.Lower() == testString.Lower() ) )
2112 #else
2113 if ( m_fileHistory[i] && ( file == m_fileHistory[i] ) )
2114 #endif
2115 {
2116 // we do have it, move it to the top of the history
2117 RemoveFileFromHistory (i);
2118 AddFileToHistory (file);
2119 return;
2120 }
2121 }
2122
2123 // if we already have a full history, delete the one at the end
2124 if ( m_fileMaxFiles == m_fileHistoryN )
2125 {
2126 RemoveFileFromHistory (m_fileHistoryN - 1);
2127 AddFileToHistory (file);
2128 return;
2129 }
2130
2131 // Add to the project file history:
2132 // Move existing files (if any) down so we can insert file at beginning.
2133 if (m_fileHistoryN < m_fileMaxFiles)
2134 {
2135 wxList::compatibility_iterator node = m_fileMenus.GetFirst();
2136 while (node)
2137 {
2138 wxMenu* menu = (wxMenu*) node->GetData();
2139 if ( m_fileHistoryN == 0 && menu->GetMenuItemCount() )
2140 {
2141 menu->AppendSeparator();
2142 }
2143 menu->Append(m_idBase+m_fileHistoryN, _("[EMPTY]"));
2144 node = node->GetNext();
2145 }
2146 m_fileHistoryN ++;
2147 }
2148 // Shuffle filenames down
2149 for (i = (m_fileHistoryN-1); i > 0; i--)
2150 {
2151 m_fileHistory[i] = m_fileHistory[i-1];
2152 }
2153 m_fileHistory[0] = MYcopystring(file);
2154
2155 // this is the directory of the last opened file
2156 wxString pathCurrent;
2157 wxSplitPath( m_fileHistory[0], &pathCurrent, NULL, NULL );
2158 for (i = 0; i < m_fileHistoryN; i++)
2159 {
2160 if ( m_fileHistory[i] )
2161 {
2162 // if in same directory just show the filename; otherwise the full
2163 // path
2164 wxString pathInMenu, path, filename, ext;
2165 wxSplitPath( m_fileHistory[i], &path, &filename, &ext );
2166 if ( path == pathCurrent )
2167 {
2168 pathInMenu = filename;
2169 if ( !ext.empty() )
2170 pathInMenu = pathInMenu + wxFILE_SEP_EXT + ext;
2171 }
2172 else
2173 {
2174 // absolute path; could also set relative path
2175 pathInMenu = m_fileHistory[i];
2176 }
2177
2178 // we need to quote '&' characters which are used for mnemonics
2179 pathInMenu.Replace(_T("&"), _T("&&"));
2180 wxString buf;
2181 buf.Printf(s_MRUEntryFormat, i + 1, pathInMenu.c_str());
2182 wxList::compatibility_iterator node = m_fileMenus.GetFirst();
2183 while (node)
2184 {
2185 wxMenu* menu = (wxMenu*) node->GetData();
2186 menu->SetLabel(m_idBase + i, buf);
2187 node = node->GetNext();
2188 }
2189 }
2190 }
2191 }
2192
2193 void wxFileHistory::RemoveFileFromHistory(size_t i)
2194 {
2195 wxCHECK_RET( i < m_fileHistoryN,
2196 wxT("invalid index in wxFileHistory::RemoveFileFromHistory") );
2197
2198 // delete the element from the array (could use memmove() too...)
2199 delete [] m_fileHistory[i];
2200
2201 size_t j;
2202 for ( j = i; j < m_fileHistoryN - 1; j++ )
2203 {
2204 m_fileHistory[j] = m_fileHistory[j + 1];
2205 }
2206
2207 wxList::compatibility_iterator node = m_fileMenus.GetFirst();
2208 while ( node )
2209 {
2210 wxMenu* menu = (wxMenu*) node->GetData();
2211
2212 // shuffle filenames up
2213 wxString buf;
2214 for ( j = i; j < m_fileHistoryN - 1; j++ )
2215 {
2216 buf.Printf(s_MRUEntryFormat, j + 1, m_fileHistory[j]);
2217 menu->SetLabel(m_idBase + j, buf);
2218 }
2219
2220 node = node->GetNext();
2221
2222 // delete the last menu item which is unused now
2223 wxWindowID lastItemId = m_idBase + wx_truncate_cast(wxWindowID, m_fileHistoryN) - 1;
2224 if (menu->FindItem(lastItemId))
2225 {
2226 menu->Delete(lastItemId);
2227 }
2228
2229 // delete the last separator too if no more files are left
2230 if ( m_fileHistoryN == 1 )
2231 {
2232 wxMenuItemList::compatibility_iterator nodeLast = menu->GetMenuItems().GetLast();
2233 if ( nodeLast )
2234 {
2235 wxMenuItem *menuItem = nodeLast->GetData();
2236 if ( menuItem->IsSeparator() )
2237 {
2238 menu->Delete(menuItem);
2239 }
2240 //else: should we search backwards for the last separator?
2241 }
2242 //else: menu is empty somehow
2243 }
2244 }
2245
2246 m_fileHistoryN--;
2247 }
2248
2249 wxString wxFileHistory::GetHistoryFile(size_t i) const
2250 {
2251 wxString s;
2252 if ( i < m_fileHistoryN )
2253 {
2254 s = m_fileHistory[i];
2255 }
2256 else
2257 {
2258 wxFAIL_MSG( wxT("bad index in wxFileHistory::GetHistoryFile") );
2259 }
2260
2261 return s;
2262 }
2263
2264 void wxFileHistory::UseMenu(wxMenu *menu)
2265 {
2266 if (!m_fileMenus.Member(menu))
2267 m_fileMenus.Append(menu);
2268 }
2269
2270 void wxFileHistory::RemoveMenu(wxMenu *menu)
2271 {
2272 m_fileMenus.DeleteObject(menu);
2273 }
2274
2275 #if wxUSE_CONFIG
2276 void wxFileHistory::Load(wxConfigBase& config)
2277 {
2278 m_fileHistoryN = 0;
2279 wxString buf;
2280 buf.Printf(wxT("file%d"), (int)m_fileHistoryN+1);
2281 wxString historyFile;
2282 while ((m_fileHistoryN < m_fileMaxFiles) && config.Read(buf, &historyFile) && (!historyFile.empty()))
2283 {
2284 m_fileHistory[m_fileHistoryN] = MYcopystring((const wxChar*) historyFile);
2285 m_fileHistoryN ++;
2286 buf.Printf(wxT("file%d"), (int)m_fileHistoryN+1);
2287 historyFile = wxEmptyString;
2288 }
2289 AddFilesToMenu();
2290 }
2291
2292 void wxFileHistory::Save(wxConfigBase& config)
2293 {
2294 size_t i;
2295 for (i = 0; i < m_fileMaxFiles; i++)
2296 {
2297 wxString buf;
2298 buf.Printf(wxT("file%d"), (int)i+1);
2299 if (i < m_fileHistoryN)
2300 config.Write(buf, wxString(m_fileHistory[i]));
2301 else
2302 config.Write(buf, wxEmptyString);
2303 }
2304 }
2305 #endif // wxUSE_CONFIG
2306
2307 void wxFileHistory::AddFilesToMenu()
2308 {
2309 if (m_fileHistoryN > 0)
2310 {
2311 wxList::compatibility_iterator node = m_fileMenus.GetFirst();
2312 while (node)
2313 {
2314 wxMenu* menu = (wxMenu*) node->GetData();
2315 if (menu->GetMenuItemCount())
2316 {
2317 menu->AppendSeparator();
2318 }
2319
2320 size_t i;
2321 for (i = 0; i < m_fileHistoryN; i++)
2322 {
2323 if (m_fileHistory[i])
2324 {
2325 wxString buf;
2326 buf.Printf(s_MRUEntryFormat, i+1, m_fileHistory[i]);
2327 menu->Append(m_idBase+i, buf);
2328 }
2329 }
2330 node = node->GetNext();
2331 }
2332 }
2333 }
2334
2335 void wxFileHistory::AddFilesToMenu(wxMenu* menu)
2336 {
2337 if (m_fileHistoryN > 0)
2338 {
2339 if (menu->GetMenuItemCount())
2340 {
2341 menu->AppendSeparator();
2342 }
2343
2344 size_t i;
2345 for (i = 0; i < m_fileHistoryN; i++)
2346 {
2347 if (m_fileHistory[i])
2348 {
2349 wxString buf;
2350 buf.Printf(s_MRUEntryFormat, i+1, m_fileHistory[i]);
2351 menu->Append(m_idBase+i, buf);
2352 }
2353 }
2354 }
2355 }
2356
2357 // ----------------------------------------------------------------------------
2358 // Permits compatibility with existing file formats and functions that
2359 // manipulate files directly
2360 // ----------------------------------------------------------------------------
2361
2362 #if wxUSE_STD_IOSTREAM
2363
2364 bool wxTransferFileToStream(const wxString& filename, wxSTD ostream& stream)
2365 {
2366 wxFFile file(filename, _T("rb"));
2367 if ( !file.IsOpened() )
2368 return false;
2369
2370 char buf[4096];
2371
2372 size_t nRead;
2373 do
2374 {
2375 nRead = file.Read(buf, WXSIZEOF(buf));
2376 if ( file.Error() )
2377 return false;
2378
2379 stream.write(buf, nRead);
2380 if ( !stream )
2381 return false;
2382 }
2383 while ( !file.Eof() );
2384
2385 return true;
2386 }
2387
2388 bool wxTransferStreamToFile(wxSTD istream& stream, const wxString& filename)
2389 {
2390 wxFFile file(filename, _T("wb"));
2391 if ( !file.IsOpened() )
2392 return false;
2393
2394 char buf[4096];
2395 do
2396 {
2397 stream.read(buf, WXSIZEOF(buf));
2398 if ( !stream.bad() ) // fail may be set on EOF, don't use operator!()
2399 {
2400 if ( !file.Write(buf, stream.gcount()) )
2401 return false;
2402 }
2403 }
2404 while ( !stream.eof() );
2405
2406 return true;
2407 }
2408
2409 #else // !wxUSE_STD_IOSTREAM
2410
2411 bool wxTransferFileToStream(const wxString& filename, wxOutputStream& stream)
2412 {
2413 wxFFile file(filename, _T("rb"));
2414 if ( !file.IsOpened() )
2415 return false;
2416
2417 char buf[4096];
2418
2419 size_t nRead;
2420 do
2421 {
2422 nRead = file.Read(buf, WXSIZEOF(buf));
2423 if ( file.Error() )
2424 return false;
2425
2426 stream.Write(buf, nRead);
2427 if ( !stream )
2428 return false;
2429 }
2430 while ( !file.Eof() );
2431
2432 return true;
2433 }
2434
2435 bool wxTransferStreamToFile(wxInputStream& stream, const wxString& filename)
2436 {
2437 wxFFile file(filename, _T("wb"));
2438 if ( !file.IsOpened() )
2439 return false;
2440
2441 char buf[4096];
2442 do
2443 {
2444 stream.Read(buf, WXSIZEOF(buf));
2445
2446 const size_t nRead = stream.LastRead();
2447 if ( !nRead || !file.Write(buf, nRead) )
2448 return false;
2449 }
2450 while ( !stream.Eof() );
2451
2452 return true;
2453 }
2454
2455 #endif // wxUSE_STD_IOSTREAM/!wxUSE_STD_IOSTREAM
2456
2457 #endif // wxUSE_DOC_VIEW_ARCHITECTURE