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