1 // Copyright (c) 2011-2012 Ryan Prichard
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to
5 // deal in the Software without restriction, including without limitation the
6 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 // sell copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
29 #include "../shared/winpty_snprintf.h"
32 struct SmallRect : SMALL_RECT
36 Left = Right = Top = Bottom = 0;
39 SmallRect(SHORT x, SHORT y, SHORT width, SHORT height)
43 Right = x + width - 1;
44 Bottom = y + height - 1;
47 SmallRect(const COORD &topLeft, const COORD &size)
51 Right = Left + size.X - 1;
52 Bottom = Top + size.Y - 1;
55 SmallRect(const SMALL_RECT &other)
57 *(SMALL_RECT*)this = other;
60 SmallRect(const SmallRect &other)
62 *(SMALL_RECT*)this = *(const SMALL_RECT*)&other;
65 SmallRect &operator=(const SmallRect &other)
67 *(SMALL_RECT*)this = *(const SMALL_RECT*)&other;
71 bool contains(const SmallRect &other) const
73 return other.Left >= Left &&
74 other.Right <= Right &&
76 other.Bottom <= Bottom;
79 bool contains(const Coord &other) const
81 return other.X >= Left &&
87 SmallRect intersected(const SmallRect &other) const
89 int x1 = std::max(Left, other.Left);
90 int x2 = std::min(Right, other.Right);
91 int y1 = std::max(Top, other.Top);
92 int y2 = std::min(Bottom, other.Bottom);
95 std::max(0, x2 - x1 + 1),
96 std::max(0, y2 - y1 + 1));
99 SmallRect ensureLineIncluded(SHORT line) const
101 const SHORT h = height();
103 return SmallRect(Left, line, width(), h);
104 } else if (line > Bottom) {
105 return SmallRect(Left, line - h + 1, width(), h);
111 SHORT top() const { return Top; }
112 SHORT left() const { return Left; }
113 SHORT width() const { return Right - Left + 1; }
114 SHORT height() const { return Bottom - Top + 1; }
115 void setTop(SHORT top) { Top = top; }
116 void setLeft(SHORT left) { Left = left; }
117 void setWidth(SHORT width) { Right = Left + width - 1; }
118 void setHeight(SHORT height) { Bottom = Top + height - 1; }
119 Coord size() const { return Coord(width(), height()); }
121 bool operator==(const SmallRect &other) const
123 return Left == other.Left &&
124 Right == other.Right &&
126 Bottom == other.Bottom;
129 bool operator!=(const SmallRect &other) const
131 return !(*this == other);
134 std::string toString() const
137 winpty_snprintf(ret, "(x=%d,y=%d,w=%d,h=%d)",
138 Left, Top, width(), height());
139 return std::string(ret);
143 #endif // SMALLRECT_H