/[pcsx2_0.9.7]/trunk/3rdparty/wxWidgets/src/common/docview.cpp
ViewVC logotype

Contents of /trunk/3rdparty/wxWidgets/src/common/docview.cpp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.22