MyGUI  3.2.1
MyGUI_Align.h
Go to the documentation of this file.
00001 /*
00002  * This source file is part of MyGUI. For the latest info, see http://mygui.info/
00003  * Distributed under the MIT License
00004  * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
00005  */
00006 
00007 #ifndef __MYGUI_ALIGN_H__
00008 #define __MYGUI_ALIGN_H__
00009 
00010 #include "MyGUI_Prerequest.h"
00011 #include "MyGUI_Macros.h"
00012 #include "MyGUI_Diagnostic.h"
00013 #include "MyGUI_StringUtility.h"
00014 #include <map>
00015 
00016 namespace MyGUI
00017 {
00018 
00019     struct MYGUI_EXPORT Align
00020     {
00021         enum Enum
00022         {
00023             HCenter = MYGUI_FLAG_NONE, 
00024             VCenter = MYGUI_FLAG_NONE, 
00025             Center = HCenter | VCenter, 
00027             Left = MYGUI_FLAG(1), 
00028             Right = MYGUI_FLAG(2), 
00029             HStretch = Left | Right, 
00031             Top = MYGUI_FLAG(3), 
00032             Bottom = MYGUI_FLAG(4), 
00033             VStretch = Top | Bottom, 
00035             Stretch = HStretch | VStretch, 
00036             Default = Left | Top 
00037         };
00038 
00039         Align(Enum _value = Default) :
00040             mValue(_value)
00041         {
00042         }
00043 
00044         bool isHCenter() const
00045         {
00046             return HCenter == (mValue & ((int)HStretch));
00047         }
00048 
00049         bool isVCenter() const
00050         {
00051             return VCenter == (mValue & ((int)VStretch));
00052         }
00053 
00054         bool isCenter() const
00055         {
00056             return Center == (mValue & ((int)Stretch));
00057         }
00058 
00059         bool isLeft() const
00060         {
00061             return Left == (mValue & ((int)HStretch));
00062         }
00063 
00064         bool isRight() const
00065         {
00066             return Right == (mValue & ((int)HStretch));
00067         }
00068 
00069         bool isHStretch() const
00070         {
00071             return HStretch == (mValue & ((int)HStretch));
00072         }
00073 
00074         bool isTop() const
00075         {
00076             return Top == (mValue & ((int)VStretch));
00077         }
00078 
00079         bool isBottom() const
00080         {
00081             return (Bottom == (mValue & ((int)VStretch)));
00082         }
00083 
00084         bool isVStretch() const
00085         {
00086             return (VStretch == (mValue & ((int)VStretch)));
00087         }
00088 
00089         bool isStretch() const
00090         {
00091             return (Stretch == (mValue & ((int)Stretch)));
00092         }
00093 
00094         bool isDefault() const
00095         {
00096             return (Default == (mValue & ((int)Stretch)));
00097         }
00098 
00099         Align& operator |= (Align const& _other)
00100         {
00101             mValue = Enum(int(mValue) | int(_other.mValue));
00102             return *this;
00103         }
00104 
00105         friend Align operator | (Enum const& a, Enum const& b)
00106         {
00107             return Align(Enum(int(a) | int(b)));
00108         }
00109 
00110         friend Align operator | (Align const& a, Align const& b)
00111         {
00112             return Align(Enum(int(a.mValue) | int(b.mValue)));
00113         }
00114 
00115         friend bool operator == (Align const& a, Align const& b)
00116         {
00117             return a.mValue == b.mValue;
00118         }
00119 
00120         friend bool operator != (Align const& a, Align const& b)
00121         {
00122             return a.mValue != b.mValue;
00123         }
00124 
00125         typedef std::map<std::string, int> MapAlign;
00126 
00127         static Align parse(const std::string& _value)
00128         {
00129             Align result(Enum(0));
00130             const MapAlign& map_names = result.getValueNames();
00131             const std::vector<std::string>& vec = utility::split(_value);
00132             for (size_t pos = 0; pos < vec.size(); pos++)
00133             {
00134                 MapAlign::const_iterator iter = map_names.find(vec[pos]);
00135                 if (iter != map_names.end())
00136                 {
00137                     result.mValue = Enum(int(result.mValue) | int(iter->second));
00138                 }
00139             }
00140             return result;
00141         }
00142 
00143         std::string print() const
00144         {
00145             std::string result;
00146 
00147             if (mValue & Left)
00148             {
00149                 if (mValue & Right)
00150                     result = "HStretch";
00151                 else
00152                     result = "Left";
00153             }
00154             else if (mValue & Right)
00155                 result = "Right";
00156             else
00157                 result = "HCenter";
00158 
00159             if (mValue & Top)
00160             {
00161                 if (mValue & Bottom)
00162                     result += " VStretch";
00163                 else
00164                     result += " Top";
00165             }
00166             else if (mValue & Bottom)
00167                 result += " Bottom";
00168             else
00169                 result += " VCenter";
00170 
00171             return result;
00172         }
00173 
00174         friend std::ostream& operator << ( std::ostream& _stream, const Align&  _value )
00175         {
00176             _stream << _value.print();
00177             return _stream;
00178         }
00179 
00180         friend std::istream& operator >> ( std::istream& _stream, Align&  _value )
00181         {
00182             _value.mValue = Enum(0);
00183             std::string value;
00184             _stream >> value;
00185 
00186             const MapAlign& map_names = _value.getValueNames();
00187             MapAlign::const_iterator iter = map_names.find(value);
00188             if (iter != map_names.end())
00189                 _value.mValue = Enum(int(_value.mValue) | int(iter->second));
00190 
00191             if (!_stream.eof())
00192             {
00193                 std::string value2;
00194                 _stream >> value2;
00195                 iter = map_names.find(value2);
00196                 if (iter != map_names.end())
00197                     _value.mValue = Enum(int(_value.mValue) | int(iter->second));
00198             }
00199 
00200             return _stream;
00201         }
00202 
00203         int getValue() const
00204         {
00205             return mValue;
00206         }
00207 
00208     private:
00209         const MapAlign& getValueNames() const
00210         {
00211             static MapAlign map_names;
00212 
00213             if (map_names.empty())
00214             {
00215                 // OBSOLETE
00216                 map_names["ALIGN_HCENTER"] = HCenter;
00217                 map_names["ALIGN_VCENTER"] = VCenter;
00218                 map_names["ALIGN_CENTER"] = Center;
00219                 map_names["ALIGN_LEFT"] = Left;
00220                 map_names["ALIGN_RIGHT"] = Right;
00221                 map_names["ALIGN_HSTRETCH"] = HStretch;
00222                 map_names["ALIGN_TOP"] = Top;
00223                 map_names["ALIGN_BOTTOM"] = Bottom;
00224                 map_names["ALIGN_VSTRETCH"] = VStretch;
00225                 map_names["ALIGN_STRETCH"] = Stretch;
00226                 map_names["ALIGN_DEFAULT"] = Default;
00227 
00228                 MYGUI_REGISTER_VALUE(map_names, HCenter);
00229                 MYGUI_REGISTER_VALUE(map_names, VCenter);
00230                 MYGUI_REGISTER_VALUE(map_names, Center);
00231                 MYGUI_REGISTER_VALUE(map_names, Left);
00232                 MYGUI_REGISTER_VALUE(map_names, Right);
00233                 MYGUI_REGISTER_VALUE(map_names, HStretch);
00234                 MYGUI_REGISTER_VALUE(map_names, Top);
00235                 MYGUI_REGISTER_VALUE(map_names, Bottom);
00236                 MYGUI_REGISTER_VALUE(map_names, VStretch);
00237                 MYGUI_REGISTER_VALUE(map_names, Stretch);
00238                 MYGUI_REGISTER_VALUE(map_names, Default);
00239             }
00240 
00241             return map_names;
00242         }
00243 
00244     private:
00245         Enum mValue;
00246     };
00247 
00248 } // namespace MyGUI
00249 
00250 #endif // __MYGUI_ALIGN_H__