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