dillo  3.0.5
About: dillo is a small, fast, extensible Web browser particularly suitable for older or smaller computers and embedded systems (but only limited or no support for frames, CSS, JavaScript, Java).
  Fossies Dox: dillo-3.0.5.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

types.cc
Go to the documentation of this file.
1 /*
2  * Dillo Widget
3  *
4  * Copyright 2005-2007 Sebastian Geerken <sgeerken@dillo.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
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.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 
21 
22 #include "core.hh"
23 #include "../lout/msg.h"
24 
25 using namespace lout;
26 
27 namespace dw {
28 namespace core {
29 
30 Rectangle::Rectangle (int x, int y, int width, int height)
31 {
32  this->x = x;
33  this->y = y;
34  this->width = width;
35  this->height = height;
36 }
37 
38 /*
39  * Draw rectangle in view relative to point (x,y).
40  */
41 void Rectangle::draw (core::View *view, core::style::Style *style, int x,int y)
42 {
43  const bool filled = false;
44 
45  view->drawRectangle(style->color, core::style::Color::SHADING_NORMAL,filled,
46  x + this->x, y + this->y, this->width, this->height);
47 }
48 
53 bool Rectangle::intersectsWith (Rectangle *otherRect, Rectangle *dest)
54 {
55  bool doIntersect =
56  this->x < otherRect->x + otherRect->width &&
57  this->y < otherRect->y + otherRect->height &&
58  otherRect->x < this->x + this->width &&
59  otherRect->y < this->y + this->height;
60 
61  if (doIntersect) {
62  dest->x = misc::max(this->x, otherRect->x);
63  dest->y = misc::max(this->y, otherRect->y);
64  dest->width = misc::min(this->x + this->width,
65  otherRect->x + otherRect->width) - dest->x;
66  dest->height = misc::min(this->y + this->height,
67  otherRect->y + otherRect->height) - dest->y;
68  } else {
69  dest->x = dest->y = dest->width = dest->height = 0;
70  }
71 
72  return doIntersect;
73 }
74 
75 /*
76  * Return whether this is a subset of otherRect.
77  */
78 bool Rectangle::isSubsetOf (Rectangle *otherRect)
79 {
80  return
81  x >= otherRect->x &&
82  y >= otherRect->y &&
83  x + width <= otherRect->x + otherRect->width &&
84  y + height <= otherRect->y + otherRect->height;
85 }
86 
87 bool Rectangle::isPointWithin (int x, int y)
88 {
89  return
90  x >= this->x && y >= this->y &&
91  x < this->x + width && y < this->y + height;
92 }
93 
94 // ----------------------------------------------------------------------
95 
96 Circle::Circle (int x, int y, int radius)
97 {
98  this->x = x;
99  this->y = y;
100  this->radius = radius;
101 }
102 
103 /*
104  * Draw circle in view relative to point (x,y).
105  */
106 void Circle::draw (core::View *view, core::style::Style *style, int x, int y)
107 {
108  const bool filled = false;
109 
110  view->drawArc(style->color, core::style::Color::SHADING_NORMAL, filled,
111  x + this->x, y + this->y, 2 * this->radius, 2 * this->radius,
112  0, 360);
113 }
114 
115 bool Circle::isPointWithin (int x, int y)
116 {
117  return
118  (x - this->x) * (x - this->x) + (y - this->y) * (y - this->y)
119  <= radius * radius;
120 }
121 
122 // ----------------------------------------------------------------------
123 
124 Polygon::Polygon ()
125 {
126  points = new misc::SimpleVector<Point> (8);
127  minx = miny = 0xffffff;
128  maxx = maxy = -0xffffff;
129 }
130 
131 Polygon::~Polygon ()
132 {
133  delete points;
134 }
135 
136 /*
137  * Draw polygon in view relative to point (x,y).
138  */
139 void Polygon::draw (core::View *view, core::style::Style *style, int x, int y)
140 {
141  if (points->size()) {
142  int i;
143  const bool filled = false, convex = false;
144  Point *pointArray = (Point *)malloc(points->size()*sizeof(struct Point));
145 
146  for (i = 0; i < points->size(); i++) {
147  pointArray[i].x = x + points->getRef(i)->x;
148  pointArray[i].y = y + points->getRef(i)->y;
149  }
150  view->drawPolygon(style->color, core::style::Color::SHADING_NORMAL,
151  filled, convex, pointArray, i);
152  free(pointArray);
153  }
154 }
155 
156 void Polygon::addPoint (int x, int y)
157 {
158  points->increase ();
159  points->getRef(points->size () - 1)->x = x;
160  points->getRef(points->size () - 1)->y = y;
161 
162  minx = misc::min(minx, x);
163  miny = misc::min(miny, y);
164  maxx = misc::max(maxx, x);
165  maxy = misc::max(maxy, y);
166 }
167 
173 bool Polygon::linesCross0(int ax1, int ay1, int ax2, int ay2,
174  int bx1, int by1, int bx2, int by2)
175 {
177  // If the scalar product is 0, it means that one point is on the second
178  // line, so we check for <= 0, not < 0.
179  int z1 = zOfVectorProduct (ax1 - bx1, ay1 - by1, bx2 - bx1, by2 - by1);
180  int z2 = zOfVectorProduct (ax2 - bx1, ay2 - by1, bx2 - bx1, by2 - by1);
181 
182  return (z1 <= 0 && z2 >= 0) || (z1 >= 0 && z2 <= 0);
183 }
184 
189 bool Polygon::linesCross(int ax1, int ay1, int ax2, int ay2,
190  int bx1, int by1, int bx2, int by2)
191 {
192  bool cross =
193  linesCross0 (ax1, ay1, ax2, ay2, bx1, by1, bx2, by2) &&
194  linesCross0 (bx1, by1, bx2, by2, ax1, ay1, ax2, ay2);
195  _MSG("(%d, %d) - (%d, %d) and (%d, %d) - (%d, %d) cross? %s.\n",
196  ax1, ay1, ax2, ay2, bx1, by1, bx2, by2, cross ? "Yes" : "No");
197  return cross;
198 }
199 
200 bool Polygon::isPointWithin (int x, int y)
201 {
202  if (points->size () < 3 ||
203  (x < minx || x > maxx || y < miny || y >= maxy))
204  return false;
205  else {
206  int numCrosses = 0;
207  for (int i = 0; i < points->size () - 1; i++) {
208  if (linesCross (minx - 1, miny - 1, x, y,
209  points->getRef(i)->x, points->getRef(i)->y,
210  points->getRef(i + 1)->x, points->getRef(i + 1)->y))
211  numCrosses++;
212  }
213  if (linesCross (minx - 1, miny - 1, x, y,
214  points->getRef(points->size () - 1)->x,
215  points->getRef(points->size () - 1)->y,
216  points->getRef(0)->x, points->getRef(0)->y))
217  numCrosses++;
218 
219  return numCrosses % 2 == 1;
220  }
221 }
222 
223 Region::Region()
224 {
225  rectangleList = new container::typed::List <Rectangle> (true);
226 }
227 
228 Region::~Region()
229 {
230  delete rectangleList;
231 }
232 
239 void Region::addRectangle (Rectangle *rPointer)
240 {
242  Rectangle *r = new Rectangle (rPointer->x, rPointer->y,
243  rPointer->width, rPointer->height);
244 
245  for (it = rectangleList->iterator (); it.hasNext (); ) {
246  Rectangle *ownRect = it.getNext ();
247 
248  int combinedHeight =
249  misc::max(r->y + r->height, ownRect->y + ownRect->height) -
250  misc::min(r->y, ownRect->y);
251  int combinedWidth =
252  misc::max(r->x + r->width, ownRect->x + ownRect->width) -
253  misc::min(r->x, ownRect->x);
254 
255  if (rectangleList->size() >= 16 ||
256  combinedWidth * combinedHeight <=
257  ownRect->width * ownRect->height + r->width * r->height) {
258 
259  r->x = misc::min(r->x, ownRect->x);
260  r->y = misc::min(r->y, ownRect->y);
261  r->width = combinedWidth;
262  r->height = combinedHeight;
263 
264  rectangleList->removeRef (ownRect);
265  }
266  }
267 
268  rectangleList->append (r);
269 }
270 
271 } // namespace core
272 } // namespace dw
dw::core::Point
Definition: types.hh:49
lout::container::typed::Iterator::hasNext
bool hasNext()
Definition: container.hh:369
dw::core::Rectangle::x
int x
Definition: types.hh:72
dw::core::View::drawRectangle
virtual void drawRectangle(style::Color *color, style::Color::Shading shading, bool filled, int x, int y, int width, int height)=0
lout::misc::max
T max(T a, T b)
Definition: misc.hh:20
lout::misc::min
T min(T a, T b)
Definition: misc.hh:19
lout::container::typed::Iterator
Typed version of container::untyped::Iterator.
Definition: container.hh:352
dw::core::Point::y
int y
Definition: types.hh:52
dw::core::Rectangle::height
int height
Definition: types.hh:75
lout
Definition: container.cc:26
dw::core::View::drawArc
virtual void drawArc(style::Color *color, style::Color::Shading shading, bool filled, int centerX, int centerY, int width, int height, int angle1, int angle2)=0
dw::core::Rectangle
dw::core::Shape implemtation for simple rectangles.
Definition: types.hh:69
dw::core::Point::x
int x
Definition: types.hh:51
lout::container::typed::List
Typed version of container::untyped::List.
Definition: container.hh:425
dw::core::View
An interface to encapsulate platform dependent drawing.
Definition: view.hh:16
lout::container::typed::Iterator::getNext
T * getNext()
Definition: container.hh:370
_MSG
#define _MSG(...)
Definition: bookmarks.c:44
core.hh
dw::core::style::StyleAttrs::color
Color * color
Definition: style.hh:495
dw::core::View::drawPolygon
virtual void drawPolygon(style::Color *color, style::Color::Shading shading, bool filled, bool convex, Point *points, int npoints)=0
dw
Dw is in this namespace, or sub namespaces of this one.
Definition: alignedtextblock.cc:26
dw::core::Rectangle::width
int width
Definition: types.hh:74
lout::misc::SimpleVector
Simple (simpler than container::untyped::Vector and container::typed::Vector) template based vector.
Definition: misc.hh:71
dw::core::style::Style
Definition: style.hh:571
dw::core::Rectangle::y
int y
Definition: types.hh:73