* Copyright 2006, Haiku.
* Distributed under the terms of the MIT License.
*
* Authors:
* Stephan Aßmus <superstippi@gmx.de>
*/
#include "PathContainer.h"
#include <stdio.h>
#include <string.h>
#include <OS.h>
#include "VectorPath.h"
#ifdef ICON_O_MATIC
PathContainerListener::PathContainerListener() {}
PathContainerListener::~PathContainerListener() {}
#endif
PathContainer::PathContainer(bool ownsPaths)
: fPaths(16),
fOwnsPaths(ownsPaths)
#ifdef ICON_O_MATIC
, fListeners(2)
#endif
{
}
PathContainer::~PathContainer()
{
#ifdef ICON_O_MATIC
int32 count = fListeners.CountItems();
if (count > 0) {
debugger("~PathContainer() - there are still"
"listeners attached\n");
}
#endif
_MakeEmpty();
}
bool
PathContainer::AddPath(VectorPath* path)
{
return AddPath(path, CountPaths());
}
bool
PathContainer::AddPath(VectorPath* path, int32 index)
{
if (!path)
return false;
if (HasPath(path))
return false;
if (fPaths.AddItem((void*)path, index)) {
#ifdef ICON_O_MATIC
_NotifyPathAdded(path, index);
#endif
return true;
}
fprintf(stderr, "PathContainer::AddPath() - out of memory!\n");
return false;
}
bool
PathContainer::RemovePath(VectorPath* path)
{
if (fPaths.RemoveItem((void*)path)) {
#ifdef ICON_O_MATIC
_NotifyPathRemoved(path);
#endif
return true;
}
return false;
}
VectorPath*
PathContainer::RemovePath(int32 index)
{
VectorPath* path = (VectorPath*)fPaths.RemoveItem(index);
#ifdef ICON_O_MATIC
if (path) {
_NotifyPathRemoved(path);
}
#endif
return path;
}
void
PathContainer::MakeEmpty()
{
_MakeEmpty();
}
int32
PathContainer::CountPaths() const
{
return fPaths.CountItems();
}
bool
PathContainer::HasPath(VectorPath* path) const
{
return fPaths.HasItem((void*)path);
}
int32
PathContainer::IndexOf(VectorPath* path) const
{
return fPaths.IndexOf((void*)path);
}
VectorPath*
PathContainer::PathAt(int32 index) const
{
return (VectorPath*)fPaths.ItemAt(index);
}
VectorPath*
PathContainer::PathAtFast(int32 index) const
{
return (VectorPath*)fPaths.ItemAtFast(index);
}
#ifdef ICON_O_MATIC
bool
PathContainer::AddListener(PathContainerListener* listener)
{
if (listener && !fListeners.HasItem((void*)listener))
return fListeners.AddItem(listener);
return false;
}
bool
PathContainer::RemoveListener(PathContainerListener* listener)
{
return fListeners.RemoveItem(listener);
}
#endif
void
PathContainer::_MakeEmpty()
{
int32 count = CountPaths();
for (int32 i = 0; i < count; i++) {
VectorPath* path = PathAtFast(i);
#ifdef ICON_O_MATIC
_NotifyPathRemoved(path);
if (fOwnsPaths)
path->ReleaseReference();
#else
if (fOwnsPaths)
delete path;
#endif
}
fPaths.MakeEmpty();
}
#ifdef ICON_O_MATIC
void
PathContainer::_NotifyPathAdded(VectorPath* path, int32 index) const
{
BList listeners(fListeners);
int32 count = listeners.CountItems();
for (int32 i = 0; i < count; i++) {
PathContainerListener* listener
= (PathContainerListener*)listeners.ItemAtFast(i);
listener->PathAdded(path, index);
}
}
void
PathContainer::_NotifyPathRemoved(VectorPath* path) const
{
BList listeners(fListeners);
int32 count = listeners.CountItems();
for (int32 i = 0; i < count; i++) {
PathContainerListener* listener
= (PathContainerListener*)listeners.ItemAtFast(i);
listener->PathRemoved(path);
}
}
#endif