MyGUI  3.2.0
MyGUI_Align.h
Go to the documentation of this file.
1 
6 /*
7  This file is part of MyGUI.
8 
9  MyGUI is free software: you can redistribute it and/or modify
10  it under the terms of the GNU Lesser General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  MyGUI is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public License
20  along with MyGUI. If not, see <http://www.gnu.org/licenses/>.
21 */
22 #ifndef __MYGUI_ALIGN_H__
23 #define __MYGUI_ALIGN_H__
24 
25 #include "MyGUI_Prerequest.h"
26 #include "MyGUI_Macros.h"
27 #include "MyGUI_Diagnostic.h"
28 #include "MyGUI_StringUtility.h"
29 #include <map>
30 
31 namespace MyGUI
32 {
33 
35  {
36  enum Enum
37  {
38  HCenter = MYGUI_FLAG_NONE,
39  VCenter = MYGUI_FLAG_NONE,
40  Center = HCenter | VCenter,
44  HStretch = Left | Right,
46  Top = MYGUI_FLAG(3),
48  VStretch = Top | Bottom,
50  Stretch = HStretch | VStretch,
51  Default = Left | Top
52  };
53 
54  Align(Enum _value = Default) :
55  value(_value)
56  {
57  }
58 
59  bool isHCenter() const
60  {
61  return HCenter == (value & ((int)HStretch));
62  }
63 
64  bool isVCenter() const
65  {
66  return VCenter == (value & ((int)VStretch));
67  }
68 
69  bool isCenter() const
70  {
71  return Center == (value & ((int)Stretch));
72  }
73 
74  bool isLeft() const
75  {
76  return Left == (value & ((int)HStretch));
77  }
78 
79  bool isRight() const
80  {
81  return Right == (value & ((int)HStretch));
82  }
83 
84  bool isHStretch() const
85  {
86  return HStretch == (value & ((int)HStretch));
87  }
88 
89  bool isTop() const
90  {
91  return Top == (value & ((int)VStretch));
92  }
93 
94  bool isBottom() const
95  {
96  return (Bottom == (value & ((int)VStretch)));
97  }
98 
99  bool isVStretch() const
100  {
101  return (VStretch == (value & ((int)VStretch)));
102  }
103 
104  bool isStretch() const
105  {
106  return (Stretch == (value & ((int)Stretch)));
107  }
108 
109  bool isDefault() const
110  {
111  return (Default == (value & ((int)Stretch)));
112  }
113 
114  Align& operator |= (Align const& _other)
115  {
116  value = Enum(int(value) | int(_other.value));
117  return *this;
118  }
119 
120  friend Align operator | (Enum const& a, Enum const& b)
121  {
122  return Align(Enum(int(a) | int(b)));
123  }
124 
125  friend Align operator | (Align const& a, Align const& b)
126  {
127  return Align(Enum(int(a.value) | int(b.value)));
128  }
129 
130  friend bool operator == (Align const& a, Align const& b)
131  {
132  return a.value == b.value;
133  }
134 
135  friend bool operator != (Align const& a, Align const& b)
136  {
137  return a.value != b.value;
138  }
139 
140  typedef std::map<std::string, int> MapAlign;
141 
142  static Align parse(const std::string& _value)
143  {
144  Align result(Enum(0));
145  const MapAlign& map_names = result.getValueNames();
146  const std::vector<std::string>& vec = utility::split(_value);
147  for (size_t pos = 0; pos < vec.size(); pos++)
148  {
149  MapAlign::const_iterator iter = map_names.find(vec[pos]);
150  if (iter != map_names.end())
151  {
152  result.value = Enum(int(result.value) | int(iter->second));
153  }
154  }
155  return result;
156  }
157 
158  std::string print() const
159  {
160  std::string result;
161 
162  if (value & Left)
163  {
164  if (value & Right)
165  result = "HStretch";
166  else
167  result = "Left";
168  }
169  else if (value & Right)
170  result = "Right";
171  else
172  result = "HCenter";
173 
174  if (value & Top)
175  {
176  if (value & Bottom)
177  result += " VStretch";
178  else
179  result += " Top";
180  }
181  else if (value & Bottom)
182  result += " Bottom";
183  else
184  result += " VCenter";
185 
186  return result;
187  }
188 
189  friend std::ostream& operator << ( std::ostream& _stream, const Align& _value )
190  {
191  _stream << _value.print();
192  return _stream;
193  }
194 
195  friend std::istream& operator >> ( std::istream& _stream, Align& _value )
196  {
197  _value.value = Enum(0);
198  std::string value;
199  _stream >> value;
200 
201  const MapAlign& map_names = _value.getValueNames();
202  MapAlign::const_iterator iter = map_names.find(value);
203  if (iter != map_names.end())
204  _value.value = Enum(int(_value.value) | int(iter->second));
205 
206  if (!_stream.eof())
207  {
208  std::string value2;
209  _stream >> value2;
210  iter = map_names.find(value2);
211  if (iter != map_names.end())
212  _value.value = Enum(int(_value.value) | int(iter->second));
213  }
214 
215  return _stream;
216  }
217 
218  private:
219  const MapAlign& getValueNames() const
220  {
221  static MapAlign map_names;
222 
223  if (map_names.empty())
224  {
225  // OBSOLETE
226  map_names["ALIGN_HCENTER"] = HCenter;
227  map_names["ALIGN_VCENTER"] = VCenter;
228  map_names["ALIGN_CENTER"] = Center;
229  map_names["ALIGN_LEFT"] = Left;
230  map_names["ALIGN_RIGHT"] = Right;
231  map_names["ALIGN_HSTRETCH"] = HStretch;
232  map_names["ALIGN_TOP"] = Top;
233  map_names["ALIGN_BOTTOM"] = Bottom;
234  map_names["ALIGN_VSTRETCH"] = VStretch;
235  map_names["ALIGN_STRETCH"] = Stretch;
236  map_names["ALIGN_DEFAULT"] = Default;
237 
238  MYGUI_REGISTER_VALUE(map_names, HCenter);
239  MYGUI_REGISTER_VALUE(map_names, VCenter);
240  MYGUI_REGISTER_VALUE(map_names, Center);
241  MYGUI_REGISTER_VALUE(map_names, Left);
242  MYGUI_REGISTER_VALUE(map_names, Right);
243  MYGUI_REGISTER_VALUE(map_names, HStretch);
244  MYGUI_REGISTER_VALUE(map_names, Top);
245  MYGUI_REGISTER_VALUE(map_names, Bottom);
246  MYGUI_REGISTER_VALUE(map_names, VStretch);
247  MYGUI_REGISTER_VALUE(map_names, Stretch);
248  MYGUI_REGISTER_VALUE(map_names, Default);
249  }
250 
251  return map_names;
252  }
253 
254  private:
255  Enum value;
256  };
257 
258 } // namespace MyGUI
259 
260 #endif // __MYGUI_ALIGN_H__