MyGUI
3.0.3
|
00001 00007 /* 00008 This file is part of MyGUI. 00009 00010 MyGUI is free software: you can redistribute it and/or modify 00011 it under the terms of the GNU Lesser General Public License as published by 00012 the Free Software Foundation, either version 3 of the License, or 00013 (at your option) any later version. 00014 00015 MyGUI is distributed in the hope that it will be useful, 00016 but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 GNU Lesser General Public License for more details. 00019 00020 You should have received a copy of the GNU Lesser General Public License 00021 along with MyGUI. If not, see <http://www.gnu.org/licenses/>. 00022 */ 00023 #ifndef __MYGUI_ALIGN_H__ 00024 #define __MYGUI_ALIGN_H__ 00025 00026 #include "MyGUI_Prerequest.h" 00027 #include "MyGUI_Macros.h" 00028 #include "MyGUI_Diagnostic.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, 00053 HRelative = MYGUI_FLAG(5), 00054 VRelative = MYGUI_FLAG(6), 00055 Relative = HRelative | VRelative 00056 }; 00057 00058 Align(Enum _value = Default) : value(_value) { } 00059 00060 bool isHCenter() const { return HCenter == (value & ((int)HStretch | (int)HRelative)); } 00061 bool isVCenter() const { return VCenter == (value & ((int)VStretch | (int)VRelative)); } 00062 bool isCenter() const { return Center == (value & ((int)Stretch | (int)Relative)); } 00063 00064 bool isLeft() const { return Left == (value & ((int)HStretch | (int)HRelative)); } 00065 bool isRight() const { return Right == (value & ((int)HStretch | (int)HRelative)); } 00066 bool isHStretch() const { return HStretch == (value & ((int)HStretch | (int)HRelative)); } 00067 00068 bool isTop() const { return Top == (value & ((int)VStretch | (int)VRelative)); } 00069 bool isBottom() const { return (Bottom == (value & ((int)VStretch | (int)VRelative))); } 00070 bool isVStretch() const { return (VStretch == (value & ((int)VStretch | (int)VRelative))); } 00071 00072 bool isStretch() const { return (Stretch == (value & ((int)Stretch | (int)Relative))); } 00073 bool isDefault() const { return (Default == (value & ((int)Stretch | (int)Relative))); } 00074 00075 bool isHRelative() const { return HRelative == (value & (int)HRelative); } 00076 bool isVRelative() const { return VRelative == (value & (int)VRelative); } 00077 bool isRelative() const { return Relative == (value & (int)Relative); } 00078 00079 Align& operator |= (Align const& _other) { value = Enum(int(value) | int(_other.value)); return *this; } 00080 friend Align operator | (Enum const& a, Enum const& b) { return Align(Enum(int(a) | int(b))); } 00081 friend Align operator | (Align const& a, Align const& b) { return Align(Enum(int(a.value) | int(b.value))); } 00082 00083 friend bool operator == (Align const& a, Align const& b) { return a.value == b.value; } 00084 friend bool operator != (Align const& a, Align const& b) { return a.value != b.value; } 00085 00086 typedef std::map<std::string, int> MapAlign; 00087 00088 static Align parse(const std::string& _value) 00089 { 00090 Align result(Enum(0)); 00091 const MapAlign& map_names = result.getValueNames(); 00092 const std::vector<std::string>& vec = utility::split(_value); 00093 for (size_t pos=0; pos<vec.size(); pos++) 00094 { 00095 MapAlign::const_iterator iter = map_names.find(vec[pos]); 00096 if (iter != map_names.end()) 00097 { 00098 result.value = Enum(int(result.value) | int(iter->second)); 00099 } 00100 } 00101 return result; 00102 } 00103 00104 std::string print() const 00105 { 00106 std::string result; 00107 00108 if (value & Left) 00109 { 00110 if (value & Right) result = "HStretch"; 00111 else result = "Left"; 00112 } 00113 else if (value & Right) result = "Right"; 00114 else result = "HCenter"; 00115 00116 if (value & Top) 00117 { 00118 if (value & Bottom) result += " VStretch"; 00119 else result += " Top"; 00120 } 00121 else if (value & Bottom) result += " Bottom"; 00122 else result += " VCenter"; 00123 00124 return result; 00125 } 00126 00127 friend std::ostream& operator << ( std::ostream& _stream, const Align& _value ) 00128 { 00129 _stream << _value.print(); 00130 return _stream; 00131 } 00132 00133 friend std::istream& operator >> ( std::istream& _stream, Align& _value ) 00134 { 00135 _value.value = Enum(0); 00136 std::string value; 00137 _stream >> value; 00138 00139 const MapAlign& map_names = _value.getValueNames(); 00140 MapAlign::const_iterator iter = map_names.find(value); 00141 if (iter != map_names.end()) 00142 _value.value = Enum(int(_value.value) | int(iter->second)); 00143 00144 00145 if (!_stream.eof()) 00146 { 00147 std::string value2; 00148 _stream >> value2; 00149 iter = map_names.find(value2); 00150 if (iter != map_names.end()) 00151 _value.value = Enum(int(_value.value) | int(iter->second)); 00152 } 00153 00154 return _stream; 00155 } 00156 00157 private: 00158 const MapAlign& getValueNames() const 00159 { 00160 static MapAlign map_names; 00161 00162 if (map_names.empty()) 00163 { 00164 // OBSOLETE 00165 map_names["ALIGN_HCENTER"] = HCenter; 00166 map_names["ALIGN_VCENTER"] = VCenter; 00167 map_names["ALIGN_CENTER"] = Center; 00168 map_names["ALIGN_LEFT"] = Left; 00169 map_names["ALIGN_RIGHT"] = Right; 00170 map_names["ALIGN_HSTRETCH"] = HStretch; 00171 map_names["ALIGN_TOP"] = Top; 00172 map_names["ALIGN_BOTTOM"] = Bottom; 00173 map_names["ALIGN_VSTRETCH"] = VStretch; 00174 map_names["ALIGN_STRETCH"] = Stretch; 00175 map_names["ALIGN_DEFAULT"] = Default; 00176 00177 MYGUI_REGISTER_VALUE(map_names, HCenter); 00178 MYGUI_REGISTER_VALUE(map_names, VCenter); 00179 MYGUI_REGISTER_VALUE(map_names, Center); 00180 MYGUI_REGISTER_VALUE(map_names, Left); 00181 MYGUI_REGISTER_VALUE(map_names, Right); 00182 MYGUI_REGISTER_VALUE(map_names, HStretch); 00183 MYGUI_REGISTER_VALUE(map_names, Top); 00184 MYGUI_REGISTER_VALUE(map_names, Bottom); 00185 MYGUI_REGISTER_VALUE(map_names, VStretch); 00186 MYGUI_REGISTER_VALUE(map_names, Stretch); 00187 MYGUI_REGISTER_VALUE(map_names, Default); 00188 MYGUI_REGISTER_VALUE(map_names, HRelative); 00189 MYGUI_REGISTER_VALUE(map_names, VRelative); 00190 MYGUI_REGISTER_VALUE(map_names, Relative); 00191 } 00192 00193 return map_names; 00194 } 00195 00196 private: 00197 Enum value; 00198 }; 00199 00200 } // namespace MyGUI 00201 00202 #endif // __MYGUI_ALIGN_H__