* Copyright 2006, Haiku.
* Distributed under the terms of the MIT License.
*
* Authors:
* Stephan Aßmus <superstippi@gmx.de>
*/
#include "StyleContainer.h"
#include <stdio.h>
#include <string.h>
#include <OS.h>
#include "Style.h"
#ifdef ICON_O_MATIC
StyleContainerListener::StyleContainerListener() {}
StyleContainerListener::~StyleContainerListener() {}
#endif
StyleContainer::StyleContainer()
#ifdef ICON_O_MATIC
: fStyles(32),
fListeners(2)
#else
: fStyles(32)
#endif
{
}
StyleContainer::~StyleContainer()
{
#ifdef ICON_O_MATIC
int32 count = fListeners.CountItems();
if (count > 0) {
debugger("~StyleContainer() - there are still"
"listeners attached\n");
}
#endif
_MakeEmpty();
}
bool
StyleContainer::AddStyle(Style* style)
{
return AddStyle(style, CountStyles());
}
bool
StyleContainer::AddStyle(Style* style, int32 index)
{
if (!style)
return false;
if (HasStyle(style))
return false;
if (fStyles.AddItem((void*)style, index)) {
#ifdef ICON_O_MATIC
_NotifyStyleAdded(style, index);
#endif
return true;
}
fprintf(stderr, "StyleContainer::AddStyle() - out of memory!\n");
return false;
}
bool
StyleContainer::RemoveStyle(Style* style)
{
if (fStyles.RemoveItem((void*)style)) {
#ifdef ICON_O_MATIC
_NotifyStyleRemoved(style);
#endif
return true;
}
return false;
}
Style*
StyleContainer::RemoveStyle(int32 index)
{
Style* style = (Style*)fStyles.RemoveItem(index);
if (style) {
#ifdef ICON_O_MATIC
_NotifyStyleRemoved(style);
#endif
}
return style;
}
void
StyleContainer::MakeEmpty()
{
_MakeEmpty();
}
int32
StyleContainer::CountStyles() const
{
return fStyles.CountItems();
}
bool
StyleContainer::HasStyle(Style* style) const
{
return fStyles.HasItem((void*)style);
}
int32
StyleContainer::IndexOf(Style* style) const
{
return fStyles.IndexOf((void*)style);
}
Style*
StyleContainer::StyleAt(int32 index) const
{
return (Style*)fStyles.ItemAt(index);
}
Style*
StyleContainer::StyleAtFast(int32 index) const
{
return (Style*)fStyles.ItemAtFast(index);
}
#ifdef ICON_O_MATIC
bool
StyleContainer::AddListener(StyleContainerListener* listener)
{
if (listener && !fListeners.HasItem((void*)listener))
return fListeners.AddItem(listener);
return false;
}
bool
StyleContainer::RemoveListener(StyleContainerListener* listener)
{
return fListeners.RemoveItem(listener);
}
#endif
void
StyleContainer::_MakeEmpty()
{
int32 count = CountStyles();
for (int32 i = 0; i < count; i++) {
Style* style = StyleAtFast(i);
#ifdef ICON_O_MATIC
_NotifyStyleRemoved(style);
style->ReleaseReference();
#else
delete style;
#endif
}
fStyles.MakeEmpty();
}
#ifdef ICON_O_MATIC
void
StyleContainer::_NotifyStyleAdded(Style* style, int32 index) const
{
BList listeners(fListeners);
int32 count = listeners.CountItems();
for (int32 i = 0; i < count; i++) {
StyleContainerListener* listener
= (StyleContainerListener*)listeners.ItemAtFast(i);
listener->StyleAdded(style, index);
}
}
void
StyleContainer::_NotifyStyleRemoved(Style* style) const
{
BList listeners(fListeners);
int32 count = listeners.CountItems();
for (int32 i = 0; i < count; i++) {
StyleContainerListener* listener
= (StyleContainerListener*)listeners.ItemAtFast(i);
listener->StyleRemoved(style);
}
}
#endif