Debian Codegraph @

deps/winpty/src/agent/SmallRect.h

raw
1 // Copyright (c) 2011-2012 Ryan Prichard
2 //
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:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
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
19 // IN THE SOFTWARE.
20
21 #ifndef SMALLRECT_H
22 #define SMALLRECT_H
23
24 #include <windows.h>
25
26 #include <algorithm>
27 #include <string>
28
29 #include "../shared/winpty_snprintf.h"
30 #include "Coord.h"
31
32 struct SmallRect : SMALL_RECT
33 {
34 SmallRect()
35 {
36 Left = Right = Top = Bottom = 0;
37 }
38
39 SmallRect(SHORT x, SHORT y, SHORT width, SHORT height)
40 {
41 Left = x;
42 Top = y;
43 Right = x + width - 1;
44 Bottom = y + height - 1;
45 }
46
47 SmallRect(const COORD &topLeft, const COORD &size)
48 {
49 Left = topLeft.X;
50 Top = topLeft.Y;
51 Right = Left + size.X - 1;
52 Bottom = Top + size.Y - 1;
53 }
54
55 SmallRect(const SMALL_RECT &other)
56 {
57 *(SMALL_RECT*)this = other;
58 }
59
60 SmallRect(const SmallRect &other)
61 {
62 *(SMALL_RECT*)this = *(const SMALL_RECT*)&other;
63 }
64
65 SmallRect &operator=(const SmallRect &other)
66 {
67 *(SMALL_RECT*)this = *(const SMALL_RECT*)&other;
68 return *this;
69 }
70
71 bool contains(const SmallRect &other) const
72 {
73 return other.Left >= Left &&
74 other.Right <= Right &&
75 other.Top >= Top &&
76 other.Bottom <= Bottom;
77 }
78
79 bool contains(const Coord &other) const
80 {
81 return other.X >= Left &&
82 other.X <= Right &&
83 other.Y >= Top &&
84 other.Y <= Bottom;
85 }
86
87 SmallRect intersected(const SmallRect &other) const
88 {
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);
93 return SmallRect(x1,
94 y1,
95 std::max(0, x2 - x1 + 1),
96 std::max(0, y2 - y1 + 1));
97 }
98
99 SmallRect ensureLineIncluded(SHORT line) const
100 {
101 const SHORT h = height();
102 if (line < Top) {
103 return SmallRect(Left, line, width(), h);
104 } else if (line > Bottom) {
105 return SmallRect(Left, line - h + 1, width(), h);
106 } else {
107 return *this;
108 }
109 }
110
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()); }
120
121 bool operator==(const SmallRect &other) const
122 {
123 return Left == other.Left &&
124 Right == other.Right &&
125 Top == other.Top &&
126 Bottom == other.Bottom;
127 }
128
129 bool operator!=(const SmallRect &other) const
130 {
131 return !(*this == other);
132 }
133
134 std::string toString() const
135 {
136 char ret[64];
137 winpty_snprintf(ret, "(x=%d,y=%d,w=%d,h=%d)",
138 Left, Top, width(), height());
139 return std::string(ret);
140 }
141 };
142
143 #endif // SMALLRECT_H