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
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): ______________________________________.
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
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
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
63 * Revision 1.1 1994/04/20 12:17:44 robertj
69 #pragma implementation "shapes.h"
73 #include <pwclib/shapes.h>
76 #include <pwclib/shapes.inl>
82 //////////////////////////////////////////////////////////////////////////////
85 void PShape::_SetPosition(const PPoint
& pt
)
91 void PShape::_SetDimensions(const PDim
&)
97 BOOL
PShape::InShape(const PPoint
& pt
) const
99 return GetBounds().ContainsPoint(pt
);
103 BOOL
PShape::IsComposite() const
109 //////////////////////////////////////////////////////////////////////////////
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
134 void PLine::Draw(PCanvas
& canvas
) const
137 canvas
.DrawLine(position
, otherEnd
);
141 //////////////////////////////////////////////////////////////////////////////
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
)
157 return position
!= other
.position
? LessThan
: EqualTo
;
161 PDim
POrthoShape::GetDimensions() const
167 void POrthoShape::_SetDimensions(const PDim
& dim
)
173 //////////////////////////////////////////////////////////////////////////////
176 void PRectangle::Draw(PCanvas
& canvas
) const
179 canvas
.DrawRect(position
, dimensions
);
183 //////////////////////////////////////////////////////////////////////////////
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
198 canvas
.DrawRoundRect(PRect(position
, dimensions
),
199 corner
.Width(), corner
.Height());
203 //////////////////////////////////////////////////////////////////////////////
206 void PEllipse::Draw(PCanvas
& canvas
) const
209 canvas
.DrawEllipse(PRect(position
, dimensions
));
213 //////////////////////////////////////////////////////////////////////////////
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
229 canvas
.DrawArc(PRect(position
, dimensions
), startAngle
, endAngle
);
233 //////////////////////////////////////////////////////////////////////////////
236 void PPie::Draw(PCanvas
& canvas
) const
239 canvas
.DrawPie(PRect(position
, dimensions
), startAngle
, endAngle
);
243 //////////////////////////////////////////////////////////////////////////////
246 void PChord::Draw(PCanvas
& canvas
) const
249 canvas
.DrawChord(PRect(position
, dimensions
), startAngle
, endAngle
);
253 //////////////////////////////////////////////////////////////////////////////
256 PPixShape::PPixShape(PORDINATE x
, PORDINATE y
, const PPixelImage
& pix
)
257 : POrthoShape(x
, y
, pix
->Width(), pix
->Height())
263 PPixShape::PPixShape(const PPoint
& pt
, const PPixelImage
& pix
)
264 : POrthoShape(pt
, pix
->GetDimensions())
270 void PPixShape::Draw(PCanvas
& canvas
) const
273 canvas
.DrawPixels(PRect(position
, dimensions
), pixels
);
277 //////////////////////////////////////////////////////////////////////////////
280 PPicShape::PPicShape(PORDINATE x
, PORDINATE y
, const PPictImage
& pic
)
281 : POrthoShape(x
, y
, pic
->Width(), pic
->Height())
287 PPicShape::PPicShape(const PPoint
& pt
, const PPictImage
& pic
)
288 : POrthoShape(pt
, pic
->GetDimensions())
294 void PPicShape::Draw(PCanvas
& canvas
) const
297 canvas
.DrawPict(PRect(position
, dimensions
), picture
);
301 //////////////////////////////////////////////////////////////////////////////
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
);
320 void PTextLines::Draw(PCanvas
& canvas
) const
323 ((PTextLines
*)this)->dimensions
= canvas
.MeasureString(text
);
324 canvas
.DrawString(position
, text
);
328 void PTextLines::SetText(const PString
& str
)
331 dimensions
.SetHeight(0); // Needs to be recalculated
335 PDim
PTextLines::GetDimensions(PCanvas
& canvas
)
338 dimensions
= canvas
.MeasureString(text
);
339 return GetDimensions();
343 //////////////////////////////////////////////////////////////////////////////
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
368 ((PTextBlock
*)this)->dimensions
.SetHeight(
369 canvas
.MeasureString(text
, dimensions
.Width()));
370 canvas
.DrawString(position
, text
, dimensions
.Width());
374 PDim
PTextBlock::GetDimensions(PCanvas
& canvas
)
377 dimensions
.SetHeight(canvas
.MeasureString(text
, dimensions
.Width()));
382 //////////////////////////////////////////////////////////////////////////////
385 PPolyShape::PPolyShape(const PPoint
* ptArray
, PINDEX numPts
)
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
++)
401 PDim
PPolyShape::GetDimensions() const
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());
415 return PDim(max
.X(), max
.Y());
419 //////////////////////////////////////////////////////////////////////////////
422 void PPolyLine::Draw(PCanvas
& canvas
) const
425 canvas
.DrawPolyLine(points
);
429 //////////////////////////////////////////////////////////////////////////////
432 void PPolygon::Draw(PCanvas
& canvas
) const
435 canvas
.DrawPolygon(points
);
438 //////////////////////////////////////////////////////////////////////////////
442 //////////////////////////////////////////////////////////////////////////////
445 void PBezier::Draw(PCanvas
& canvas
) const
451 //////////////////////////////////////////////////////////////////////////////
454 void PBSpline::Draw(PCanvas
& canvas
) const
460 //////////////////////////////////////////////////////////////////////////////
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
);
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
);
494 void PCompositeShape::Remove(PShape
& shape
)
496 shapes
.Remove(&shape
);
497 PAssert(shapes
.GetSize() != 0, PInvalidParameter
);
502 void PCompositeShape::Remove(PINDEX idx
)
504 shapes
.RemoveAt(idx
);
505 PAssert(shapes
.GetSize() != 0, PInvalidParameter
);
510 PShape
* PCompositeShape::Find(const PPoint
& pt
)
512 for (PINDEX i
= 0; i
< shapes
.GetSize(); i
++) {
513 if (shapes
[i
].InShape(pt
))
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
);
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());
562 // End Of File ///////////////////////////////////////////////////////////////