1/*
2 * Copyright 2006, Haiku.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 *		Stephan Aßmus <superstippi@gmx.de>
7 */
8
9#include "StyleContainer.h"
10
11#include <stdio.h>
12#include <string.h>
13
14#include <OS.h>
15
16#include "Style.h"
17
18#ifdef ICON_O_MATIC
19StyleContainerListener::StyleContainerListener() {}
20StyleContainerListener::~StyleContainerListener() {}
21#endif
22
23// constructor
24StyleContainer::StyleContainer()
25#ifdef ICON_O_MATIC
26	: fStyles(32),
27	  fListeners(2)
28#else
29	: fStyles(32)
30#endif
31{
32}
33
34// destructor
35StyleContainer::~StyleContainer()
36{
37#ifdef ICON_O_MATIC
38	int32 count = fListeners.CountItems();
39	if (count > 0) {
40		debugger("~StyleContainer() - there are still"
41				 "listeners attached\n");
42	}
43#endif // ICON_O_MATIC
44	_MakeEmpty();
45}
46
47// #pragma mark -
48
49// AddStyle
50bool
51StyleContainer::AddStyle(Style* style)
52{
53	return AddStyle(style, CountStyles());
54}
55
56// AddStyle
57bool
58StyleContainer::AddStyle(Style* style, int32 index)
59{
60	if (!style)
61		return false;
62
63	// prevent adding the same style twice
64	if (HasStyle(style))
65		return false;
66
67	if (fStyles.AddItem((void*)style, index)) {
68#ifdef ICON_O_MATIC
69		_NotifyStyleAdded(style, index);
70#endif
71		return true;
72	}
73
74	fprintf(stderr, "StyleContainer::AddStyle() - out of memory!\n");
75	return false;
76}
77
78// RemoveStyle
79bool
80StyleContainer::RemoveStyle(Style* style)
81{
82	if (fStyles.RemoveItem((void*)style)) {
83#ifdef ICON_O_MATIC
84		_NotifyStyleRemoved(style);
85#endif
86		return true;
87	}
88
89	return false;
90}
91
92// RemoveStyle
93Style*
94StyleContainer::RemoveStyle(int32 index)
95{
96	Style* style = (Style*)fStyles.RemoveItem(index);
97	if (style) {
98#ifdef ICON_O_MATIC
99		_NotifyStyleRemoved(style);
100#endif
101	}
102
103	return style;
104}
105
106// MakeEmpty
107void
108StyleContainer::MakeEmpty()
109{
110	_MakeEmpty();
111}
112
113// #pragma mark -
114
115// CountStyles
116int32
117StyleContainer::CountStyles() const
118{
119	return fStyles.CountItems();
120}
121
122// HasStyle
123bool
124StyleContainer::HasStyle(Style* style) const
125{
126	return fStyles.HasItem((void*)style);
127}
128
129// IndexOf
130int32
131StyleContainer::IndexOf(Style* style) const
132{
133	return fStyles.IndexOf((void*)style);
134}
135
136// StyleAt
137Style*
138StyleContainer::StyleAt(int32 index) const
139{
140	return (Style*)fStyles.ItemAt(index);
141}
142
143// StyleAtFast
144Style*
145StyleContainer::StyleAtFast(int32 index) const
146{
147	return (Style*)fStyles.ItemAtFast(index);
148}
149
150// #pragma mark -
151
152#ifdef ICON_O_MATIC
153
154// AddListener
155bool
156StyleContainer::AddListener(StyleContainerListener* listener)
157{
158	if (listener && !fListeners.HasItem((void*)listener))
159		return fListeners.AddItem(listener);
160	return false;
161}
162
163// RemoveListener
164bool
165StyleContainer::RemoveListener(StyleContainerListener* listener)
166{
167	return fListeners.RemoveItem(listener);
168}
169
170#endif // ICON_O_MATIC
171
172// #pragma mark -
173
174// _MakeEmpty
175void
176StyleContainer::_MakeEmpty()
177{
178	int32 count = CountStyles();
179	for (int32 i = 0; i < count; i++) {
180		Style* style = StyleAtFast(i);
181#ifdef ICON_O_MATIC
182		_NotifyStyleRemoved(style);
183		style->Release();
184#else
185		delete style;
186#endif
187	}
188	fStyles.MakeEmpty();
189}
190
191// #pragma mark -
192
193#ifdef ICON_O_MATIC
194
195// _NotifyStyleAdded
196void
197StyleContainer::_NotifyStyleAdded(Style* style, int32 index) const
198{
199	BList listeners(fListeners);
200	int32 count = listeners.CountItems();
201	for (int32 i = 0; i < count; i++) {
202		StyleContainerListener* listener
203			= (StyleContainerListener*)listeners.ItemAtFast(i);
204		listener->StyleAdded(style, index);
205	}
206}
207
208// _NotifyStyleRemoved
209void
210StyleContainer::_NotifyStyleRemoved(Style* style) const
211{
212	BList listeners(fListeners);
213	int32 count = listeners.CountItems();
214	for (int32 i = 0; i < count; i++) {
215		StyleContainerListener* listener
216			= (StyleContainerListener*)listeners.ItemAtFast(i);
217		listener->StyleRemoved(style);
218	}
219}
220
221#endif // ICON_O_MATIC
222
223