Uncommented beaudio code
[pwlib.git] / src / pwclib / shapes.cxx
blob6b4744a6577326514c37384c380bd9994a13e4b0
1 /*
2 * shapes.cxx
4 * Geometric shapes.
6 * Portable Windows Library
8 * Copyright (c) 1993-1998 Equivalence Pty. Ltd.
10 * The contents of this file are subject to the Mozilla Public License
11 * Version 1.0 (the "License"); you may not use this file except in
12 * compliance with the License. You may obtain a copy of the License at
13 * http://www.mozilla.org/MPL/
15 * Software distributed under the License is distributed on an "AS IS"
16 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17 * the License for the specific language governing rights and limitations
18 * under the License.
20 * The Original Code is Portable Windows Library.
22 * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
24 * Portions are Copyright (C) 1993 Free Software Foundation, Inc.
25 * All Rights Reserved.
27 * Contributor(s): ______________________________________.
29 * $Log$
30 * Revision 1.12 1999/08/17 03:46:41 robertj
31 * Fixed usage of inlines in optimised version.
33 * Revision 1.11 1998/12/20 09:14:44 robertj
34 * Added pragma implementation for GNU compiler.
36 * Revision 1.10 1998/11/30 04:52:17 robertj
37 * New directory structure
39 * Revision 1.9 1998/09/23 06:29:55 robertj
40 * Added open source copyright license.
42 * Revision 1.8 1997/04/27 05:50:24 robertj
43 * DLL support.
45 * Revision 1.7 1996/08/08 10:08:50 robertj
46 * Directory structure changes for common files.
48 * Revision 1.6 1996/01/28 02:52:08 robertj
49 * Added assert into all Compare functions to assure comparison between compatible objects.
51 * Revision 1.5 1995/01/21 05:22:36 robertj
52 * Documentation.
54 * Revision 1.4 1994/10/23 03:38:06 robertj
55 * Changed PPixels to pointer to get polymorphism.
57 * Revision 1.3 1994/08/01 03:41:24 robertj
58 * Use of PNEW instead of new for heap debugging. Need undef for Unix end.
60 * Revision 1.2 1994/07/27 05:58:07 robertj
61 * Synchronisation.
63 * Revision 1.1 1994/04/20 12:17:44 robertj
64 * Initial revision
68 #ifdef __GNUC__
69 #pragma implementation "shapes.h"
70 #endif
72 #include <pwlib.h>
73 #include <pwclib/shapes.h>
75 #if !P_USE_INLINES
76 #include <pwclib/shapes.inl>
77 #endif
79 #define new PNEW
82 //////////////////////////////////////////////////////////////////////////////
83 // PShape
85 void PShape::_SetPosition(const PPoint & pt)
87 position = pt;
91 void PShape::_SetDimensions(const PDim &)
93 // Do nothing
97 BOOL PShape::InShape(const PPoint & pt) const
99 return GetBounds().ContainsPoint(pt);
103 BOOL PShape::IsComposite() const
105 return FALSE;
109 //////////////////////////////////////////////////////////////////////////////
110 // PLine
112 PObject::Comparison PLine::Compare(const PObject & obj) const
114 PAssert(obj.IsDescendant(PLine::Class()), PInvalidCast);
115 const PLine & other = (const PLine &)obj;
116 return position != other.position || otherEnd != other.otherEnd
117 ? GreaterThan : EqualTo;
121 PDim PLine::GetDimensions() const
123 PPoint d = position - otherEnd;
124 return PDim(PABS(d.X()), PABS(d.Y()));
128 BOOL PLine::InShape(const PPoint &) const
130 return FALSE;
134 void PLine::Draw(PCanvas & canvas) const
136 canvas = *this;
137 canvas.DrawLine(position, otherEnd);
141 //////////////////////////////////////////////////////////////////////////////
142 // POrthoShape
144 POrthoShape::POrthoShape(const PPoint & topLeft, const PPoint & botRight)
145 : PShape(PMIN(topLeft.X(), botRight.X()), PMIN(topLeft.Y(), botRight.Y())),
146 dimensions(PABS(topLeft.X()-botRight.X()), PABS(topLeft.Y()-botRight.Y()))
151 PObject::Comparison POrthoShape::Compare(const PObject & obj) const
153 PAssert(obj.IsDescendant(POrthoShape::Class()), PInvalidCast);
154 const POrthoShape & other = (const POrthoShape &)obj;
155 if (dimensions != other.dimensions)
156 return GreaterThan;
157 return position != other.position ? LessThan : EqualTo;
161 PDim POrthoShape::GetDimensions() const
163 return dimensions;
167 void POrthoShape::_SetDimensions(const PDim & dim)
169 dimensions = dim;
173 //////////////////////////////////////////////////////////////////////////////
174 // PRectangle
176 void PRectangle::Draw(PCanvas & canvas) const
178 canvas = *this;
179 canvas.DrawRect(position, dimensions);
183 //////////////////////////////////////////////////////////////////////////////
184 // PRoundedRectangle
186 PObject::Comparison PRoundedRectangle::Compare(const PObject & obj) const
188 PAssert(obj.IsDescendant(PRoundedRectangle::Class()), PInvalidCast);
189 const PRoundedRectangle & other = (const PRoundedRectangle &)obj;
190 return position != other.position || dimensions != other.dimensions ||
191 corner != other.corner ? GreaterThan : EqualTo;
195 void PRoundedRectangle::Draw(PCanvas & canvas) const
197 canvas = *this;
198 canvas.DrawRoundRect(PRect(position, dimensions),
199 corner.Width(), corner.Height());
203 //////////////////////////////////////////////////////////////////////////////
204 // PEllipse
206 void PEllipse::Draw(PCanvas & canvas) const
208 canvas = *this;
209 canvas.DrawEllipse(PRect(position, dimensions));
213 //////////////////////////////////////////////////////////////////////////////
214 // PArc
216 PObject::Comparison PArc::Compare(const PObject & obj) const
218 PAssert(obj.IsDescendant(PArc::Class()), PInvalidCast);
219 const PArc & other = (const PArc &)obj;
220 return position != other.position || dimensions != other.dimensions ||
221 startAngle != other.startAngle || endAngle != other.endAngle
222 ? GreaterThan : EqualTo;
226 void PArc::Draw(PCanvas & canvas) const
228 canvas = *this;
229 canvas.DrawArc(PRect(position, dimensions), startAngle, endAngle);
233 //////////////////////////////////////////////////////////////////////////////
234 // PPie
236 void PPie::Draw(PCanvas & canvas) const
238 canvas = *this;
239 canvas.DrawPie(PRect(position, dimensions), startAngle, endAngle);
243 //////////////////////////////////////////////////////////////////////////////
244 // PChord
246 void PChord::Draw(PCanvas & canvas) const
248 canvas = *this;
249 canvas.DrawChord(PRect(position, dimensions), startAngle, endAngle);
253 //////////////////////////////////////////////////////////////////////////////
254 // PPixShape
256 PPixShape::PPixShape(PORDINATE x, PORDINATE y, const PPixelImage & pix)
257 : POrthoShape(x, y, pix->Width(), pix->Height())
259 pixels = pix;
263 PPixShape::PPixShape(const PPoint & pt, const PPixelImage & pix)
264 : POrthoShape(pt, pix->GetDimensions())
266 pixels = pix;
270 void PPixShape::Draw(PCanvas & canvas) const
272 canvas = *this;
273 canvas.DrawPixels(PRect(position, dimensions), pixels);
277 //////////////////////////////////////////////////////////////////////////////
278 // PPicShape
280 PPicShape::PPicShape(PORDINATE x, PORDINATE y, const PPictImage & pic)
281 : POrthoShape(x, y, pic->Width(), pic->Height())
283 picture = pic;
287 PPicShape::PPicShape(const PPoint & pt, const PPictImage & pic)
288 : POrthoShape(pt, pic->GetDimensions())
290 picture = pic;
294 void PPicShape::Draw(PCanvas & canvas) const
296 canvas = *this;
297 canvas.DrawPict(PRect(position, dimensions), picture);
301 //////////////////////////////////////////////////////////////////////////////
302 // PTextLines
304 PObject::Comparison PTextLines::Compare(const PObject & obj) const
306 PAssert(obj.IsDescendant(PTextLines::Class()), PInvalidCast);
307 const PTextLines & other = (const PTextLines &)obj;
308 return position != other.position || text != other.text
309 ? GreaterThan : EqualTo;
313 PDim PTextLines::GetDimensions() const
315 PAssert(dimensions.Width() != 0 && dimensions.Height() != 0, PInvalidParameter);
316 return dimensions;
320 void PTextLines::Draw(PCanvas & canvas) const
322 canvas = *this;
323 ((PTextLines*)this)->dimensions = canvas.MeasureString(text);
324 canvas.DrawString(position, text);
328 void PTextLines::SetText(const PString & str)
330 text = str;
331 dimensions.SetHeight(0); // Needs to be recalculated
335 PDim PTextLines::GetDimensions(PCanvas & canvas)
337 canvas = *this;
338 dimensions = canvas.MeasureString(text);
339 return GetDimensions();
343 //////////////////////////////////////////////////////////////////////////////
344 // PTextBlock
346 PObject::Comparison PTextBlock::Compare(const PObject & obj) const
348 PAssert(obj.IsDescendant(PTextBlock::Class()), PInvalidCast);
349 const PTextBlock & other = (const PTextBlock &)obj;
350 return position != other.position || text != other.text ||
351 dimensions.Width() != other.dimensions.Width() ? GreaterThan : EqualTo;
355 void PTextBlock::_SetDimensions(const PDim & dim)
357 PAssert(dim.Width() > 0, PInvalidParameter);
358 if (dim.Width() != dimensions.Width()) {
359 dimensions.SetWidth(dim.Width());
360 dimensions.SetHeight(0);
365 void PTextBlock::Draw(PCanvas & canvas) const
367 canvas = *this;
368 ((PTextBlock*)this)->dimensions.SetHeight(
369 canvas.MeasureString(text, dimensions.Width()));
370 canvas.DrawString(position, text, dimensions.Width());
374 PDim PTextBlock::GetDimensions(PCanvas & canvas)
376 canvas = *this;
377 dimensions.SetHeight(canvas.MeasureString(text, dimensions.Width()));
378 return dimensions;
382 //////////////////////////////////////////////////////////////////////////////
383 // PPolyShape
385 PPolyShape::PPolyShape(const PPoint * ptArray, PINDEX numPts)
386 : PShape(*ptArray)
388 for (PINDEX i = 0; i < numPts; i++)
389 points.Append(new PPoint(ptArray[i]));
393 void PPolyShape::_SetPosition(const PPoint & pt)
395 PPoint diff = pt - position;
396 for (PINDEX i = 0; i < points.GetSize(); i++)
397 points[i] += diff;
401 PDim PPolyShape::GetDimensions() const
403 PPoint min, max;
404 for (PINDEX i = 0; i < points.GetSize(); i++) {
405 if (min.X() > points[i].X())
406 min.SetX(points[i].X());
407 if (max.X() < points[i].X())
408 max.SetX(points[i].X());
409 if (min.Y() > points[i].Y())
410 min.SetY(points[i].Y());
411 if (max.Y() < points[i].Y())
412 max.SetY(points[i].Y());
414 max -= min;
415 return PDim(max.X(), max.Y());
419 //////////////////////////////////////////////////////////////////////////////
420 // PPolyLine
422 void PPolyLine::Draw(PCanvas & canvas) const
424 canvas = *this;
425 canvas.DrawPolyLine(points);
429 //////////////////////////////////////////////////////////////////////////////
430 // PPolygon
432 void PPolygon::Draw(PCanvas & canvas) const
434 canvas = *this;
435 canvas.DrawPolygon(points);
438 //////////////////////////////////////////////////////////////////////////////
439 // PCurve
442 //////////////////////////////////////////////////////////////////////////////
443 // PBezier
445 void PBezier::Draw(PCanvas & canvas) const
447 canvas = *this;
451 //////////////////////////////////////////////////////////////////////////////
452 // PBSpline
454 void PBSpline::Draw(PCanvas & canvas) const
456 canvas = *this;
460 //////////////////////////////////////////////////////////////////////////////
461 // PCompositeShape
463 PCompositeShape::PCompositeShape(PShape & shape)
464 : PShape(shape.GetPosition()), dimensions(shape.GetDimensions())
466 shapes.DisallowDeleteObjects();
467 shapes.Append(&shape);
471 void PCompositeShape::_SetPosition(const PPoint & pt)
473 PPoint delta = position - pt;
474 for (PINDEX i = 0; i < shapes.GetSize(); i++)
475 shapes[i].Offset(delta);
476 position = pt;
480 void PCompositeShape::Draw(PCanvas & canvas) const
482 for (PINDEX i = 0; i < shapes.GetSize(); i++)
483 shapes[i].Draw(canvas);
487 void PCompositeShape::Add(PShape & shape)
489 shapes.Append(&shape);
490 CalculateBounds();
494 void PCompositeShape::Remove(PShape & shape)
496 shapes.Remove(&shape);
497 PAssert(shapes.GetSize() != 0, PInvalidParameter);
498 CalculateBounds();
502 void PCompositeShape::Remove(PINDEX idx)
504 shapes.RemoveAt(idx);
505 PAssert(shapes.GetSize() != 0, PInvalidParameter);
506 CalculateBounds();
510 PShape * PCompositeShape::Find(const PPoint & pt)
512 for (PINDEX i = 0; i < shapes.GetSize(); i++) {
513 if (shapes[i].InShape(pt))
514 return &shapes[i];
517 return NULL;
521 PShape * PCompositeShape::FindPrimitive(const PPoint & pt)
523 for (PINDEX i = 0; i < shapes.GetSize(); i++) {
524 if (shapes[i].InShape(pt)) {
525 if (shapes[i].IsComposite())
526 return ((PCompositeShape&)shapes[i]).FindPrimitive(pt);
527 else
528 return &shapes[i];
532 return NULL;
536 void PCompositeShape::CalculateBounds()
538 position = shapes[0].GetPosition();
539 PPoint otherCorner = position + shapes[0].GetDimensions();
540 for (PINDEX i = 1; i < shapes.GetSize(); i++) {
541 PPoint p = shapes[i].GetPosition();
542 if (position.X() > p.X())
543 position.SetX(p.X());
544 if (position.Y() > p.Y())
545 position.SetY(p.Y());
547 p += shapes[i].GetDimensions();
548 if (otherCorner.X() > p.X())
549 otherCorner.SetX(p.X());
550 if (otherCorner.Y() > p.Y())
551 otherCorner.SetY(p.Y());
553 otherCorner -= position;
554 PAssert(otherCorner.X() > 0 && otherCorner.Y() > 0, PInvalidParameter);
555 dimensions = PDim(otherCorner.X(), otherCorner.Y());
559 #undef new
562 // End Of File ///////////////////////////////////////////////////////////////