2 * Copyright 2006, Haiku.
3 * Distributed under the terms of the MIT License.
6 * Stephan Aßmus <superstippi@gmx.de>
14 #include "PathContainer.h"
17 #include "StyleContainer.h"
22 IconListener::IconListener() {}
23 IconListener::~IconListener() {}
30 : fStyles(new (nothrow
) StyleContainer()),
31 fPaths(new (nothrow
) PathContainer(true)),
32 fShapes(new (nothrow
) ShapeContainer())
39 fShapes
->AddListener(this);
44 Icon::Icon(const Icon
& other
)
45 : fStyles(new (nothrow
) StyleContainer()),
46 fPaths(new (nothrow
) PathContainer(true)),
47 fShapes(new (nothrow
) ShapeContainer())
52 if (!fStyles
|| !fPaths
|| !fShapes
)
56 fShapes
->AddListener(this);
59 int32 styleCount
= other
.fStyles
->CountStyles();
60 for (int32 i
= 0; i
< styleCount
; i
++) {
61 Style
* style
= other
.fStyles
->StyleAtFast(i
);
62 Style
* clone
= new (nothrow
) Style(*style
);
63 if (!clone
|| !fStyles
->AddStyle(clone
)) {
69 int32 pathCount
= other
.fPaths
->CountPaths();
70 for (int32 i
= 0; i
< pathCount
; i
++) {
71 VectorPath
* path
= other
.fPaths
->PathAtFast(i
);
72 VectorPath
* clone
= new (nothrow
) VectorPath(*path
);
73 if (!clone
|| !fPaths
->AddPath(clone
)) {
79 int32 shapeCount
= other
.fShapes
->CountShapes();
80 for (int32 i
= 0; i
< shapeCount
; i
++) {
81 Shape
* shape
= other
.fShapes
->ShapeAtFast(i
);
82 Shape
* clone
= new (nothrow
) Shape(*shape
);
83 if (!clone
|| !fShapes
->AddShape(clone
)) {
87 // the cloned shape references styles and paths in
88 // the "other" icon, replace them with "local" styles
90 int32 styleIndex
= other
.fStyles
->IndexOf(shape
->Style());
91 clone
->SetStyle(fStyles
->StyleAt(styleIndex
));
93 clone
->Paths()->MakeEmpty();
94 pathCount
= shape
->Paths()->CountPaths();
95 for (int32 j
= 0; j
< pathCount
; j
++) {
96 VectorPath
* remote
= shape
->Paths()->PathAtFast(j
);
97 int32 index
= other
.fPaths
->IndexOf(remote
);
98 VectorPath
* local
= fPaths
->PathAt(index
);
100 printf("failed to match remote and "
101 "local paths while cloning icon\n");
104 if (!clone
->Paths()->AddPath(local
)) {
115 fShapes
->MakeEmpty();
117 fShapes
->RemoveListener(this);
127 Icon::InitCheck() const
129 return fStyles
&& fPaths
&& fShapes
? B_OK
: B_NO_MEMORY
;
135 Icon::ShapeAdded(Shape
* shape
, int32 index
)
137 shape
->AddObserver(this);
138 _NotifyAreaInvalidated(shape
->Bounds(true));
143 Icon::ShapeRemoved(Shape
* shape
)
145 shape
->RemoveObserver(this);
146 _NotifyAreaInvalidated(shape
->Bounds(true));
151 Icon::ObjectChanged(const Observable
* object
)
153 const Shape
* shape
= dynamic_cast<const Shape
*>(object
);
155 BRect area
= shape
->LastBounds();
156 area
= area
| shape
->Bounds(true);
157 area
.InsetBy(-1, -1);
158 _NotifyAreaInvalidated(area
);
164 Icon::AddListener(IconListener
* listener
)
166 if (listener
&& !fListeners
.HasItem((void*)listener
)) {
167 if (fListeners
.AddItem((void*)listener
)) {
168 listener
->AreaInvalidated(BRect(0, 0, 63, 63));
177 Icon::RemoveListener(IconListener
* listener
)
179 return fListeners
.RemoveItem((void*)listener
);
181 #endif // ICON_O_MATIC
188 return new (nothrow
) Icon(*this);
195 fShapes
->MakeEmpty();
197 fStyles
->MakeEmpty();
203 // _NotifyAreaInvalidated
205 Icon::_NotifyAreaInvalidated(const BRect
& area
) const
207 BList
listeners(fListeners
);
208 int32 count
= listeners
.CountItems();
209 for (int32 i
= 0; i
< count
; i
++) {
210 IconListener
* listener
211 = (IconListener
*)listeners
.ItemAtFast(i
);
212 listener
->AreaInvalidated(area
);
215 #endif // ICON_O_MATIC