Bug 574778 - Fix win widget's ConstrainPosition so that it supports full screen windo...
[mozilla-central.git] / gfx / thebes / gfxRect.cpp
blob1ce499640200a8b3580937ad775a45711bd61015
1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is Novell code.
17 * The Initial Developer of the Original Code is Novell Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 2006
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
22 * Robert O'Callahan (rocallahan@novell.com)
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include "gfxRect.h"
40 #include "nsMathUtils.h"
42 gfxRect
43 gfxRect::Intersect(const gfxRect& aRect) const
45 gfxRect result(0,0,0,0);
47 gfxFloat x = PR_MAX(aRect.X(), X());
48 gfxFloat xmost = PR_MIN(aRect.XMost(), XMost());
49 if (x >= xmost)
50 return result;
52 gfxFloat y = PR_MAX(aRect.Y(), Y());
53 gfxFloat ymost = PR_MIN(aRect.YMost(), YMost());
54 if (y >= ymost)
55 return result;
57 result = gfxRect(x, y, xmost - x, ymost - y);
58 return result;
61 gfxRect
62 gfxRect::Union(const gfxRect& aRect) const
64 if (IsEmpty())
65 return aRect;
66 if (aRect.IsEmpty())
67 return *this;
69 gfxFloat x = PR_MIN(aRect.X(), X());
70 gfxFloat xmost = PR_MAX(aRect.XMost(), XMost());
71 gfxFloat y = PR_MIN(aRect.Y(), Y());
72 gfxFloat ymost = PR_MAX(aRect.YMost(), YMost());
73 return gfxRect(x, y, xmost - x, ymost - y);
76 PRBool
77 gfxRect::Contains(const gfxRect& aRect) const
79 return aRect.X() >= X() && aRect.XMost() <= XMost() &&
80 aRect.Y() >= Y() && aRect.YMost() <= YMost();
83 PRBool
84 gfxRect::Contains(const gfxPoint& aPoint) const
86 return aPoint.x >= X() && aPoint.x <= XMost() &&
87 aPoint.y >= Y() && aPoint.y <= YMost();
90 void
91 gfxRect::Round()
93 // Note that don't use NS_round here. See the comment for this method in gfxRect.h
94 gfxFloat x0 = NS_floor(X() + 0.5);
95 gfxFloat y0 = NS_floor(Y() + 0.5);
96 gfxFloat x1 = NS_floor(XMost() + 0.5);
97 gfxFloat y1 = NS_floor(YMost() + 0.5);
99 pos.x = x0;
100 pos.y = y0;
102 size.width = x1 - x0;
103 size.height = y1 - y0;
106 void
107 gfxRect::RoundIn()
109 gfxFloat x0 = NS_ceil(X());
110 gfxFloat y0 = NS_ceil(Y());
111 gfxFloat x1 = NS_floor(XMost());
112 gfxFloat y1 = NS_floor(YMost());
114 pos.x = x0;
115 pos.y = y0;
117 size.width = x1 - x0;
118 size.height = y1 - y0;
121 void
122 gfxRect::RoundOut()
124 gfxFloat x0 = NS_floor(X());
125 gfxFloat y0 = NS_floor(Y());
126 gfxFloat x1 = NS_ceil(XMost());
127 gfxFloat y1 = NS_ceil(YMost());
129 pos.x = x0;
130 pos.y = y0;
132 size.width = x1 - x0;
133 size.height = y1 - y0;
136 /* Clamp r to CAIRO_COORD_MIN .. CAIRO_COORD_MAX
137 * these are to be device coordinates.
139 * Cairo is currently using 24.8 fixed point,
140 * so -2^24 .. 2^24-1 is our valid
143 #define CAIRO_COORD_MAX (16777215.0)
144 #define CAIRO_COORD_MIN (-16777216.0)
146 void
147 gfxRect::Condition()
149 // if either x or y is way out of bounds;
150 // note that we don't handle negative w/h here
151 if (pos.x > CAIRO_COORD_MAX) {
152 pos.x = CAIRO_COORD_MAX;
153 size.width = 0.0;
156 if (pos.y > CAIRO_COORD_MAX) {
157 pos.y = CAIRO_COORD_MAX;
158 size.height = 0.0;
161 if (pos.x < CAIRO_COORD_MIN) {
162 size.width += pos.x - CAIRO_COORD_MIN;
163 if (size.width < 0.0)
164 size.width = 0.0;
165 pos.x = CAIRO_COORD_MIN;
168 if (pos.y < CAIRO_COORD_MIN) {
169 size.height += pos.y - CAIRO_COORD_MIN;
170 if (size.height < 0.0)
171 size.height = 0.0;
172 pos.y = CAIRO_COORD_MIN;
175 if (pos.x + size.width > CAIRO_COORD_MAX) {
176 size.width = CAIRO_COORD_MAX - pos.x;
179 if (pos.y + size.height > CAIRO_COORD_MAX) {
180 size.height = CAIRO_COORD_MAX - pos.y;