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