00001 /* 00002 * Copyright 2000 Murray Cumming 00003 * 00004 * This library is free software; you can redistribute it and/or 00005 * modify it under the terms of the GNU Library General Public 00006 * License as published by the Free Software Foundation; either 00007 * version 2 of the License, or (at your option) any later version. 00008 * 00009 * This library is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 * Library General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU Library General Public 00015 * License along with this library; if not, write to the Free 00016 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00017 */ 00018 00019 #ifndef BAKERY_VIEW_COMPOSITE_H 00020 #define BAKERY_VIEW_COMPOSITE_H 00021 00022 #include "bakery/View/View.h" 00023 #include <vector> 00024 #include <algorithm> //For std::find 00025 00026 namespace Bakery 00027 { 00028 00032 template< class T_Document > 00033 class View_Composite : public View<T_Document> 00034 { 00035 public: 00036 View_Composite() 00037 { 00038 } 00039 00040 virtual ~View_Composite() 00041 { 00042 } 00043 00044 typedef View<T_Document> type_view; 00045 00046 virtual void add_view(type_view* pView) 00047 { 00048 //Ensure that the view has the same document: 00049 //This should be unnecessary. 00050 if(pView) 00051 { 00052 pView->set_document(View<T_Document>::get_document()); 00053 00054 //Add it to the list of child views: 00055 m_vecViews.push_back(pView); 00056 } 00057 } 00058 00059 virtual void remove_view(type_view* pView) 00060 { 00061 typename type_vecViews::iterator iter = std::find(m_vecViews.begin(), m_vecViews.end(), pView); 00062 if(iter != m_vecViews.end()) 00063 m_vecViews.erase(iter); 00064 } 00065 00066 virtual void set_document(T_Document* pDocument) 00067 { 00068 //Call base class: 00069 View<T_Document>::set_document(pDocument); 00070 00071 //Change the document in the child views. 00072 for(typename type_vecViews::iterator iter = m_vecViews.begin(); iter != m_vecViews.end(); iter++) 00073 { 00074 type_view* pView = *iter; 00075 if(pView) 00076 pView->set_document(pDocument); 00077 } 00078 } 00079 00080 virtual void load_from_document() 00081 { 00082 //Delegate to the child views: 00083 for(typename type_vecViews::iterator iter = m_vecViews.begin(); iter != m_vecViews.end(); iter++) 00084 { 00085 type_view* pView = *iter; 00086 if(pView) 00087 pView->load_from_document(); 00088 } 00089 } 00090 00091 virtual void save_to_document() 00092 { 00093 //Delegate to the child views: 00094 for(typename type_vecViews::iterator iter = m_vecViews.begin(); iter != m_vecViews.end(); iter++) 00095 { 00096 type_view* pView = *iter; 00097 if(pView) 00098 pView->save_to_document(); 00099 } 00100 } 00101 00102 protected: 00103 typedef std::vector<type_view*> type_vecViews; 00104 type_vecViews m_vecViews; 00105 }; 00106 00107 } //namespace 00108 00109 #endif