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