Mercurial > ~dholland > hg > ag > index.cgi
comparison anagram/vaclgui/agrect.cpp @ 0:13d2b8934445
Import AnaGram (near-)release tree into Mercurial.
author | David A. Holland |
---|---|
date | Sat, 22 Dec 2007 17:52:45 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:13d2b8934445 |
---|---|
1 /* | |
2 * AnaGram, A System for Syntax Directed Programming | |
3 * Copyright 1997-2002 Parsifal Software. All Rights Reserved. | |
4 * See the file COPYING for license and usage terms. | |
5 * | |
6 * agrect.cpp | |
7 */ | |
8 | |
9 #include "agrect.hpp" | |
10 | |
11 | |
12 AgQuadrant opposite(AgQuadrant corner) { | |
13 return (AgQuadrant) ((int) corner ^ 3); | |
14 } | |
15 | |
16 AgRectangle::AgRectangle(RECT r) { | |
17 pos = cint(r.left, r.top); | |
18 sz = cint(r.right - r.left, r.bottom - r.top); | |
19 } | |
20 | |
21 AgRectangle::AgRectangle(IWindow *w) : anchor(upperLeft) { | |
22 pos = w->position(); | |
23 sz = w->size(); | |
24 } | |
25 | |
26 AgRectangle::AgRectangle(cint size_) | |
27 : pos(0,0) | |
28 , sz(size_) | |
29 , anchor(upperLeft) | |
30 {} | |
31 | |
32 AgRectangle::AgRectangle(cint loc, cint size_, AgQuadrant whichQuadrant) | |
33 : pos(loc) | |
34 , sz(size_) | |
35 , anchor(whichQuadrant) | |
36 { | |
37 switch (anchor) { | |
38 case upperRight: pos.y -= sz.y; break; | |
39 case upperLeft: pos.y -= sz.y; /* fallthrough */ | |
40 case lowerLeft: pos.x -= sz.x; break; | |
41 } | |
42 } | |
43 | |
44 cint AgRectangle::position(AgQuadrant whichCorner) { | |
45 cint where = pos; | |
46 switch (whichCorner) { | |
47 case lowerLeft: where.y += sz.y; break; | |
48 case lowerRight: where.y += sz.y; /* fallthrough */ | |
49 case upperRight: where.x += sz.x; break; | |
50 } | |
51 return where; | |
52 } | |
53 | |
54 AgRectangle &AgRectangle::setSize(cint s) { | |
55 cint pos = position(anchor); | |
56 sz = s; | |
57 switch (anchor) { | |
58 case upperRight: pos.y -= sz.y; break; | |
59 case upperLeft: pos.y -= sz.y; /* fallthrough */ | |
60 case lowerLeft: pos.x -= sz.x; break; | |
61 } | |
62 return *this; | |
63 } | |
64 | |
65 AgRectangle AgRectangle::intersection(AgRectangle &r) { | |
66 cint loc = lrci(pos, r.pos); | |
67 cint lr = ulci(pos+sz, r.pos + r.sz); | |
68 lr = lrci(loc,lr); | |
69 return AgRectangle(loc, lr - loc); | |
70 } | |
71 | |
72 AgRectangle &AgRectangle::limit(cint size) { | |
73 sz = ulci(sz, size); | |
74 pos = ulci(pos, size - sz); | |
75 pos = lrci(pos, cint(0, 0)); | |
76 return *this; | |
77 } | |
78 | |
79 AgRectangle AgRectangle::desktop() { | |
80 RECT r; | |
81 SystemParametersInfo(SPI_GETWORKAREA, 0, &r, 0); | |
82 AgRectangle rect(r); | |
83 return rect; | |
84 } | |
85 | |
86 int AgRectangle::operator < (const AgRectangle &r) const { | |
87 if (pos < r.pos) { | |
88 return 1; | |
89 } | |
90 if (r.pos > pos) { | |
91 return 0; | |
92 } | |
93 return sz < r.sz; | |
94 } | |
95 | |
96 LayoutRef::LayoutRef(AgRectangle &r) { | |
97 int i; | |
98 for (i = upperLeft; i <= lowerRight; i++) { | |
99 AgQuadrant ic = (AgQuadrant) i; | |
100 corner[ic] = r.position(ic); | |
101 } | |
102 stack.push(r); | |
103 } | |
104 | |
105 int LayoutRef::overlap(AgRectangle &r) { | |
106 int n = stack.size(); | |
107 int area = 0; | |
108 while (n--) { | |
109 area += r.intersection(stack[n]).area(); | |
110 } | |
111 return area; | |
112 } | |
113 | |
114 void LayoutRef::merge(AgRectangle &r) { | |
115 stack.push(r); | |
116 int i; | |
117 for (i = upperLeft; i <= lowerRight; i++) { | |
118 AgQuadrant ic = (AgQuadrant) i; | |
119 cint rCorner = r.position(ic); | |
120 switch (ic) { | |
121 case upperLeft: { | |
122 if (rCorner.y < corner[ic].y | |
123 || rCorner.y == corner[ic].y && rCorner.x < corner[ic].x) { | |
124 corner[ic] = rCorner; | |
125 } | |
126 break; | |
127 } | |
128 case upperRight: { | |
129 if (rCorner.y < corner[ic].y | |
130 || rCorner.y == corner[ic].y && rCorner.x > corner[ic].x) { | |
131 corner[ic] = rCorner; | |
132 } | |
133 break; | |
134 } | |
135 case lowerLeft: { | |
136 if (rCorner.y > corner[ic].y | |
137 || rCorner.y == corner[ic].y && rCorner.x > corner[ic].x) { | |
138 corner[ic] = rCorner; | |
139 } | |
140 break; | |
141 } | |
142 case lowerRight: { | |
143 if (rCorner.y > corner[ic].y | |
144 || rCorner.y == corner[ic].y && rCorner.x > corner[ic].x) { | |
145 corner[ic] = rCorner; | |
146 } | |
147 break; | |
148 } | |
149 } | |
150 } | |
151 } |