Dillo v3.1.1-111-gd4f56d0d
Loading...
Searching...
No Matches
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
25using namespace lout;
26
27namespace dw {
28namespace core {
29
30Rectangle::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 */
41void Rectangle::draw (core::View *view, core::style::Style *style, int x,int y)
42{
43 const bool filled = false;
44
46 x + this->x, y + this->y, this->width, this->height);
47}
48
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 */
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
87bool 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
96Circle::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 */
106void Circle::draw (core::View *view, core::style::Style *style, int x, int y)
107{
108 const bool filled = false;
109
111 x + this->x, y + this->y, 2 * this->radius, 2 * this->radius,
112 0, 360);
113}
114
115bool 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
125{
127 minx = miny = 0xffffff;
128 maxx = maxy = -0xffffff;
129}
130
132{
133 delete points;
134}
135
136/*
137 * Draw polygon in view relative to point (x,y).
138 */
139void 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 }
151 filled, convex, pointArray, i);
152 free(pointArray);
153 }
154}
155
156void 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
173bool 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
189bool 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
200bool 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
224{
225 rectangleList = new container::typed::List <Rectangle> (true);
226}
227
229{
230 delete rectangleList;
231}
232
240{
241 container::typed::Iterator <Rectangle> it;
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
272{
273 Content::Type widgetMask = (Content::Type)
276 return (Content::Type)(Content::SELECTION_CONTENT | widgetMask);
277}
278
280{
281 switch(content->type) {
282 case START:
283 sb->append ("<start>");
284 break;
285 case END:
286 sb->append ("<end>");
287 break;
288 case TEXT:
289 sb->append ("\"");
290 sb->append (content->text);
291 sb->append ("\"");
292 break;
293 case WIDGET_IN_FLOW:
294 sb->append ("<widget in flow: ");
295 sb->appendPointer (content->widget);
296 sb->append (" (");
297 sb->append (content->widget->getClassName());
298 sb->append (")>");
299 break;
300 case WIDGET_OOF_REF:
301 sb->append ("<widget oof ref: ");
302 sb->appendPointer (content->widgetReference->widget);
303 sb->append (" (");
304 sb->append (content->widgetReference->widget->getClassName());
305 sb->append (", ");
306 sb->appendInt (content->widgetReference->parentRef);
307 sb->append (")>");
308 break;
309 case WIDGET_OOF_CONT:
310 sb->append ("<widget oof cont: ");
311 sb->appendPointer (content->widget);
312 sb->append (" (");
313 sb->append (content->widget->getClassName());
314 sb->append (")>");
315 break;
316 case BREAK:
317 sb->append ("<break>");
318 break;
319 default:
320 sb->append ("<");
321 sb->appendInt (content->type);
322 sb->append ("?>");
323 break;
324 }
325}
326
328{
329 sb->append ((mask & START) ? "st" : "--");
330 sb->append (":");
331 sb->append ((mask & END) ? "en" : "--");
332 sb->append (":");
333 sb->append ((mask & TEXT) ? "tx" : "--");
334 sb->append (":");
335 sb->append ((mask & WIDGET_IN_FLOW) ? "wf" : "--");
336 sb->append (":");
337 sb->append ((mask & WIDGET_OOF_REF) ? "Wr" : "--");
338 sb->append (":");
339 sb->append ((mask & WIDGET_OOF_CONT) ? "Wc" : "--");
340 sb->append (":");
341 sb->append ((mask & BREAK) ? "br" : "--");
342}
343
344void Content::print (Content *content)
345{
347 intoStringBuffer (content, &sb);
348 printf ("%s", sb.getChars ());
349}
350
352{
354 maskIntoStringBuffer (mask, &sb);
355 printf ("%s", sb.getChars ());
356}
357
358} // namespace core
359} // namespace dw
#define _MSG(...)
Definition bookmarks.c:45
void draw(core::View *view, core::style::Style *style, int x, int y)
Definition types.cc:106
Circle(int x, int y, int radius)
Definition types.cc:96
bool isPointWithin(int x, int y)
Definition types.cc:115
int zOfVectorProduct(int x1, int y1, int x2, int y2)
Return the z-coordinate of the vector product of two vectors, whose z-coordinate is 0 (so that x and ...
Definition types.hh:115
bool isPointWithin(int x, int y)
Definition types.cc:200
bool linesCross(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2)
Return, whether the line, limited by (ax1, ay1) and (ax2, ay2), crosses the line, limited by (bx1,...
Definition types.cc:189
void addPoint(int x, int y)
Definition types.cc:156
bool linesCross0(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2)
Return, whether the line, limited by (ax1, ay1) and (ax2, ay2), crosses the unlimited line,...
Definition types.cc:173
lout::misc::SimpleVector< Point > * points
Definition types.hh:107
void draw(core::View *view, core::style::Style *style, int x, int y)
Definition types.cc:139
dw::core::Shape implemtation for simple rectangles.
Definition types.hh:70
bool isPointWithin(int x, int y)
Definition types.cc:87
void draw(core::View *view, core::style::Style *style, int x, int y)
Definition types.cc:41
bool intersectsWith(Rectangle *otherRect, Rectangle *dest)
Return whether this rectangle and otherRect intersect.
Definition types.cc:53
bool isSubsetOf(Rectangle *otherRect)
Definition types.cc:78
lout::container::typed::List< Rectangle > * rectangleList
Definition types.hh:143
void addRectangle(Rectangle *r)
Add a rectangle to the region and combine it with existing rectangles if possible.
Definition types.cc:239
An interface to encapsulate platform dependent drawing.
Definition view.hh:17
virtual void drawPolygon(style::Color *color, style::Color::Shading shading, bool filled, bool convex, Point *points, int npoints)=0
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
virtual void drawRectangle(style::Color *color, style::Color::Shading shading, bool filled, int x, int y, int width, int height)=0
const char * getClassName()
Return the name, under which the class of this object was registered.
Definition identity.hh:140
Simple (simpler than container::untyped::Vector and container::typed::Vector) template based vector.
Definition misc.hh:115
A class for fast concatenation of a large number of strings.
Definition misc.hh:570
void appendPointer(void *p)
Definition misc.hh:596
void appendInt(int n)
Definition misc.hh:594
void append(const char *str)
Append a NUL-terminated string to the buffer, with copying.
Definition misc.hh:593
const char * getChars()
Return a NUL-terminated strings containing all appended strings.
Definition misc.cc:86
Dw is in this namespace, or sub namespaces of this one.
T min(T a, T b)
Definition misc.hh:40
T max(T a, T b)
Definition misc.hh:41
static void print(Content *content)
Definition types.cc:344
@ WIDGET_OOF_REF
reference to a widget out of flow (OOF); this widget (containing this content) is only the generator ...
Definition types.hh:217
@ WIDGET_IN_FLOW
widget in normal flow, so that this widget (containing this content) is both container (parent) and g...
Definition types.hh:207
@ WIDGET_OOF_CONT
widget out of flow (OOF); this widget (containing this content) is only the container (parent),...
Definition types.hh:212
const char * text
Definition types.hh:236
Widget * widget
Definition types.hh:237
static Content::Type maskForSelection(bool followReferences)
Definition types.cc:271
static void maskIntoStringBuffer(Type mask, lout::misc::StringBuffer *sb)
Definition types.cc:327
static void intoStringBuffer(Content *content, lout::misc::StringBuffer *sb)
Definition types.cc:279
WidgetReference * widgetReference
Definition types.hh:238
static void printMask(Type mask)
Definition types.cc:351