4 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include "anchor_point.hpp"
25 #include "math/rect.hpp"
28 std::string anchor_point_to_string(AnchorPoint point)
35 case ANCHOR_TOP_RIGHT:
43 case ANCHOR_BOTTOM_LEFT:
47 case ANCHOR_BOTTOM_RIGHT:
50 throw std::runtime_error("Invalid anchor point");
54 AnchorPoint string_to_anchor_point(const std::string& str)
57 return ANCHOR_TOP_LEFT;
60 else if(str == "topright")
61 return ANCHOR_TOP_RIGHT;
62 else if(str == "left")
64 else if(str == "middle")
66 else if(str == "right")
68 else if(str == "bottomleft")
69 return ANCHOR_BOTTOM_LEFT;
70 else if(str == "bottom")
72 else if(str == "bottomright")
73 return ANCHOR_BOTTOM_RIGHT;
75 std::ostringstream msg;
76 msg << "Unknown anchor '" << str << "'";
77 throw std::runtime_error(msg.str());
80 Vector get_anchor_pos(const Rect& rect, AnchorPoint point)
84 switch(point & ANCHOR_V_MASK) {
86 result.x = rect.get_left();
89 result.x = rect.get_left() + (rect.get_right() - rect.get_left()) / 2.0;
92 result.x = rect.get_right();
96 throw std::runtime_error("Invalid anchor point found");
98 log_warning << "Invalid anchor point found" << std::endl;
99 result.x = rect.get_left();
103 switch(point & ANCHOR_H_MASK) {
105 result.y = rect.get_top();
108 result.y = rect.get_top() + (rect.get_bottom() - rect.get_top()) / 2.0;
111 result.y = rect.get_bottom();
115 throw std::runtime_error("Invalid anchor point found");
117 log_warning << "Invalid anchor point found" << std::endl;
118 result.y = rect.get_top();
125 Vector get_anchor_pos(const Rect& destrect, float width, float height,
130 switch(point & ANCHOR_V_MASK) {
132 result.x = destrect.get_left();
135 result.x = destrect.get_middle().x - width/2.0;
138 result.x = destrect.get_right() - width;
142 throw std::runtime_error("Invalid anchor point found");
144 log_warning << "Invalid anchor point found" << std::endl;
145 result.x = destrect.get_left();
149 switch(point & ANCHOR_H_MASK) {
151 result.y = destrect.get_top();
154 result.y = destrect.get_middle().y - height/2.0;
157 result.y = destrect.get_bottom() - height;
161 throw std::runtime_error("Invalid anchor point found");
163 log_warning << "Invalid anchor point found" << std::endl;
164 result.y = destrect.get_top();