AdamGlue.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 /* GG is a GUI for SDL and OpenGL.
00003    Copyright (C) 2003-2008 T. Zachary Laine
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public License
00007    as published by the Free Software Foundation; either version 2.1
00008    of the License, or (at your option) any later version.
00009    
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014     
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with this library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA
00019 
00020    If you do not wish to comply with the terms of the LGPL please
00021    contact the author as other terms are available for a fee.
00022     
00023    Zach Laine
00024    whatwasthataddress@gmail.com */
00025    
00029 #ifndef _AdamGlue_h_
00030 #define _AdamGlue_h_
00031 
00032 #include <GG/Spin.h>
00033 #include <GG/adobe/adam.hpp>
00034 
00035 #include <boost/signals.hpp>
00036 
00037 
00038 namespace GG {
00039 
00040 class TextControl;
00041 class Button;
00042 class StateButton;
00043 class RadioButtonGroup;
00044 class Edit;
00045 class DropDownList;
00046 class Slider;
00047 
00048 namespace detail {
00049     template <class AdobeValueType, class GGValueType>
00050     GGValueType AnyCast(const adobe::any_regular_t& any);
00051 
00052     template <class AdobeValueType, class GGValueType>
00053     adobe::any_regular_t MakeAny(GGValueType x);
00054 }
00055 
00056 struct AdamCellGlueBase {};
00057 
00058 template <
00059     class ControlType,
00060     class AdamValueType,
00061     class GGValueType
00062 >
00063 struct AdamCellGlue;
00064 
00065 template <>
00066 struct AdamCellGlue<TextControl, adobe::string_t, std::string> :
00067     public AdamCellGlueBase
00068 {
00069     AdamCellGlue(TextControl& text, adobe::sheet_t& sheet, adobe::name_t cell);
00070 
00071 private:
00072     typedef AdamCellGlue<TextControl, adobe::string_t, std::string> ThisType;
00073 
00074     void SheetChanged(const adobe::any_regular_t &any);
00075 
00076     TextControl* m_text;
00077 };
00078 
00079 template <>
00080 struct AdamCellGlue<Button, adobe::string_t, std::string> :
00081     public AdamCellGlueBase
00082 {
00083     AdamCellGlue(Button& button, adobe::sheet_t& sheet, adobe::name_t cell);
00084 
00085 private:
00086     typedef AdamCellGlue<Button, adobe::string_t, std::string> ThisType;
00087 
00088     void SheetChanged(const adobe::any_regular_t &any);
00089     void Enable(bool b);
00090 
00091     Button* m_button;
00092     adobe::sheet_t* m_sheet;
00093     adobe::name_t m_cell;
00094 };
00095 
00096 template <>
00097 struct AdamCellGlue<StateButton, bool, bool> :
00098     public AdamCellGlueBase
00099 {
00100     AdamCellGlue(StateButton& state_button, adobe::sheet_t& sheet, adobe::name_t cell);
00101 
00102 private:
00103     typedef AdamCellGlue<StateButton, bool, bool> ThisType;
00104 
00105     void SheetChanged(const adobe::any_regular_t &any);
00106     void Enable(bool b);
00107     void ControlChanged(bool checked);
00108 
00109     StateButton* m_state_button;
00110     adobe::sheet_t* m_sheet;
00111     adobe::name_t m_cell;
00112 };
00113 
00114 template <>
00115 struct AdamCellGlue<RadioButtonGroup, double, std::size_t> :
00116     public AdamCellGlueBase
00117 {
00118     AdamCellGlue(RadioButtonGroup& radio_button_group, adobe::sheet_t& sheet, adobe::name_t cell);
00119 
00120 private:
00121     typedef AdamCellGlue<RadioButtonGroup, double, std::size_t> ThisType;
00122 
00123     void SheetChanged(const adobe::any_regular_t &any);
00124     void Enable(bool b);
00125     void ControlChanged(std::size_t button);
00126 
00127     RadioButtonGroup* m_radio_button_group;
00128     adobe::sheet_t* m_sheet;
00129     adobe::name_t m_cell;
00130 };
00131 
00132 template <class AdamValueType, class T>
00133 struct AdamCellGlue<Edit, AdamValueType, T> :
00134     public AdamCellGlueBase
00135 {
00136     AdamCellGlue(Edit& edit, adobe::sheet_t& sheet, adobe::name_t cell);
00137 
00138 private:
00139     typedef AdamCellGlue<Edit, AdamValueType, T> ThisType;
00140 
00141     void SheetChanged(const adobe::any_regular_t &any);
00142     void Enable(bool b);
00143     void ControlChanged(const std::string& str);
00144 
00145     Edit* m_edit;
00146     adobe::sheet_t* m_sheet;
00147     adobe::name_t m_cell;
00148 };
00149 
00150 template <>
00151 struct AdamCellGlue<MultiEdit, adobe::string_t, std::string> :
00152     public AdamCellGlueBase
00153 {
00154     AdamCellGlue(MultiEdit& multi_edit, adobe::sheet_t& sheet, adobe::name_t cell);
00155 
00156 private:
00157     typedef AdamCellGlue<MultiEdit, adobe::string_t, std::string> ThisType;
00158 
00159     void SheetChanged(const adobe::any_regular_t &any);
00160     void Enable(bool b);
00161     void ControlChanged(const std::string& str);
00162 
00163     MultiEdit* m_multi_edit;
00164     adobe::sheet_t* m_sheet;
00165     adobe::name_t m_cell;
00166 };
00167 
00168 template <class T>
00169 struct AdamCellGlue<Spin<T>, double, T> :
00170     public AdamCellGlueBase
00171 {
00172     AdamCellGlue(Spin<T>& spin, adobe::sheet_t& sheet, adobe::name_t cell);
00173 
00174 private:
00175     typedef AdamCellGlue<Spin<T>, double, T> ThisType;
00176 
00177     void SheetChanged(const adobe::any_regular_t &any);
00178     void Enable(bool b);
00179     void ControlChanged(T t);
00180 
00181     Spin<T>* m_spin;
00182     adobe::sheet_t* m_sheet;
00183     adobe::name_t m_cell;
00184 };
00185 
00186 template <>
00187 struct AdamCellGlue<DropDownList, double, std::size_t> :
00188     public AdamCellGlueBase
00189 {
00190     AdamCellGlue(DropDownList& drop_list, adobe::sheet_t& sheet, adobe::name_t cell);
00191 
00192 private:
00193     typedef AdamCellGlue<DropDownList, double, std::size_t> ThisType;
00194 
00195     void SheetChanged(const adobe::any_regular_t &any);
00196     void Enable(bool b);
00197     void ControlChanged(DropDownList::iterator it);
00198 
00199     DropDownList* m_drop_list;
00200     adobe::sheet_t* m_sheet;
00201     adobe::name_t m_cell;
00202 };
00203 
00204 template <>
00205 struct AdamCellGlue<Slider, double, int> :
00206     public AdamCellGlueBase
00207 {
00208     AdamCellGlue(Slider& slider, adobe::sheet_t& sheet, adobe::name_t cell);
00209 
00210 private:
00211     typedef AdamCellGlue<Slider, double, int> ThisType;
00212 
00213     void SheetChanged(const adobe::any_regular_t &any);
00214     void Enable(bool b);
00215     void ControlChanged(int tab_posn, int min, int max);
00216 
00217     Slider* m_slider;
00218     adobe::sheet_t* m_sheet;
00219     adobe::name_t m_cell;
00220 };
00221 
00222 struct AdamSheetGlue
00223 {
00224     AdamSheetGlue(const std::string& str);
00225     AdamSheetGlue(std::istream& stream);
00226 
00227     template <
00228         class AdamValueType,
00229         class GGValueType,
00230         class ControlType
00231     >
00232     void AddCell(ControlType& control, adobe::name_t cell);
00233 
00234 private:
00235     void Init(std::istream& stream);
00236 
00237     adobe::sheet_t m_sheet;
00238     std::vector<boost::shared_ptr<AdamCellGlueBase> > m_cells;
00239 };
00240 
00241 
00242 // implementations
00243 
00244 namespace detail {
00245     template <class AdobeValueType, class GGValueType>
00246     GGValueType AnyCast(const adobe::any_regular_t& any)
00247     { return static_cast<GGValueType>(any.cast<AdobeValueType>()); }
00248 
00249     template <class AdobeValueType, class GGValueType>
00250     adobe::any_regular_t MakeAny(GGValueType x)
00251     { return adobe::any_regular_t(static_cast<AdobeValueType>(x)); }
00252 }
00253 
00254 
00255 // AdamCellGlue<Edit>
00256 
00257 template <class AdamValueType, class T>
00258 AdamCellGlue<Edit, AdamValueType, T>::AdamCellGlue(
00259     Edit& edit,
00260     adobe::sheet_t& sheet,
00261     adobe::name_t cell) :
00262     m_edit(&edit),
00263     m_sheet(&sheet),
00264     m_cell(cell)
00265 {
00266     m_sheet->monitor_value(m_cell, boost::bind(&ThisType::SheetChanged, this, _1));
00267     m_sheet->monitor_enabled(m_cell, 0, 0, boost::bind(&ThisType::Enable, this, _1));
00268     m_edit->EditedSignal.connect(boost::bind(&ThisType::ControlChanged, this, _1));
00269 }
00270 
00271 template <class AdamValueType, class T>
00272 void AdamCellGlue<Edit, AdamValueType, T>::SheetChanged(const adobe::any_regular_t &any)
00273 { *m_edit << detail::AnyCast<AdamValueType, T>(any); }
00274 
00275 template <class AdamValueType, class T>
00276 void AdamCellGlue<Edit, AdamValueType, T>::Enable(bool b)
00277 { m_edit->Disable(!b); }
00278 
00279 template <class AdamValueType, class T>
00280 void AdamCellGlue<Edit, AdamValueType, T>::ControlChanged(const std::string& str)
00281 {
00282     try {
00283         T x = boost::lexical_cast<T>(str);
00284         m_sheet->set(m_cell, detail::MakeAny<AdamValueType, T>(x));
00285         m_sheet->update();
00286     } catch (const boost::bad_lexical_cast&) {}
00287 }
00288 
00289 
00290 // AdamCellGlue<Spin<T> >
00291 
00292 template <class T>
00293 AdamCellGlue<Spin<T>, double, T>::AdamCellGlue(
00294     Spin<T>& spin,
00295     adobe::sheet_t& sheet,
00296     adobe::name_t cell) :
00297     m_spin(&spin),
00298     m_sheet(&sheet),
00299     m_cell(cell)
00300 {
00301     m_sheet->monitor_value(m_cell, boost::bind(&ThisType::SheetChanged, this, _1));
00302     m_sheet->monitor_enabled(m_cell, 0, 0, boost::bind(&ThisType::Enable, this, _1));
00303     m_spin->ValueChangedSignal.connect(boost::bind(&ThisType::ControlChanged, this, _1));
00304 }
00305 
00306 template <class T>
00307 void AdamCellGlue<Spin<T>, double, T>::SheetChanged(const adobe::any_regular_t &any)
00308 { m_spin->SetValue(detail::AnyCast<double, T>(any)); }
00309 
00310 template <class T>
00311 void AdamCellGlue<Spin<T>, double, T>::Enable(bool b)
00312 { m_spin->Disable(!b); }
00313 
00314 template <class T>
00315 void AdamCellGlue<Spin<T>, double, T>::ControlChanged(T t)
00316 {
00317     m_sheet->set(m_cell, detail::MakeAny<double, T>(t));
00318     m_sheet->update();
00319 }
00320 
00321 
00322 // AdamSheetGlue
00323 
00324 template <
00325     class AdamValueType,
00326     class GGValueType,
00327     class ControlType
00328 >
00329 void AdamSheetGlue::AddCell(ControlType& control, adobe::name_t cell)
00330 {
00331     m_cells.push_back(
00332         boost::shared_ptr<AdamCellGlueBase>(
00333             new AdamCellGlue<ControlType, AdamValueType, GGValueType>(
00334                 control, m_sheet, cell)));
00335 }
00336 
00337 }
00338 
00339 #endif

Generated on Sat Mar 26 07:08:37 2011 for GG by  doxygen 1.5.9