Make UEFI boot-platform build again
[haiku.git] / src / libs / icon / style / StyleContainer.cpp
blob0926830d95e2f292b5f3f878829704abcb1f3d64
1 /*
2 * Copyright 2006, Haiku.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Stephan Aßmus <superstippi@gmx.de>
7 */
9 #include "StyleContainer.h"
11 #include <stdio.h>
12 #include <string.h>
14 #include <OS.h>
16 #include "Style.h"
18 #ifdef ICON_O_MATIC
19 StyleContainerListener::StyleContainerListener() {}
20 StyleContainerListener::~StyleContainerListener() {}
21 #endif
23 // constructor
24 StyleContainer::StyleContainer()
25 #ifdef ICON_O_MATIC
26 : fStyles(32),
27 fListeners(2)
28 #else
29 : fStyles(32)
30 #endif
34 // destructor
35 StyleContainer::~StyleContainer()
37 #ifdef ICON_O_MATIC
38 int32 count = fListeners.CountItems();
39 if (count > 0) {
40 debugger("~StyleContainer() - there are still"
41 "listeners attached\n");
43 #endif // ICON_O_MATIC
44 _MakeEmpty();
47 // #pragma mark -
49 // AddStyle
50 bool
51 StyleContainer::AddStyle(Style* style)
53 return AddStyle(style, CountStyles());
56 // AddStyle
57 bool
58 StyleContainer::AddStyle(Style* style, int32 index)
60 if (!style)
61 return false;
63 // prevent adding the same style twice
64 if (HasStyle(style))
65 return false;
67 if (fStyles.AddItem((void*)style, index)) {
68 #ifdef ICON_O_MATIC
69 _NotifyStyleAdded(style, index);
70 #endif
71 return true;
74 fprintf(stderr, "StyleContainer::AddStyle() - out of memory!\n");
75 return false;
78 // RemoveStyle
79 bool
80 StyleContainer::RemoveStyle(Style* style)
82 if (fStyles.RemoveItem((void*)style)) {
83 #ifdef ICON_O_MATIC
84 _NotifyStyleRemoved(style);
85 #endif
86 return true;
89 return false;
92 // RemoveStyle
93 Style*
94 StyleContainer::RemoveStyle(int32 index)
96 Style* style = (Style*)fStyles.RemoveItem(index);
97 if (style) {
98 #ifdef ICON_O_MATIC
99 _NotifyStyleRemoved(style);
100 #endif
103 return style;
106 // MakeEmpty
107 void
108 StyleContainer::MakeEmpty()
110 _MakeEmpty();
113 // #pragma mark -
115 // CountStyles
116 int32
117 StyleContainer::CountStyles() const
119 return fStyles.CountItems();
122 // HasStyle
123 bool
124 StyleContainer::HasStyle(Style* style) const
126 return fStyles.HasItem((void*)style);
129 // IndexOf
130 int32
131 StyleContainer::IndexOf(Style* style) const
133 return fStyles.IndexOf((void*)style);
136 // StyleAt
137 Style*
138 StyleContainer::StyleAt(int32 index) const
140 return (Style*)fStyles.ItemAt(index);
143 // StyleAtFast
144 Style*
145 StyleContainer::StyleAtFast(int32 index) const
147 return (Style*)fStyles.ItemAtFast(index);
150 // #pragma mark -
152 #ifdef ICON_O_MATIC
154 // AddListener
155 bool
156 StyleContainer::AddListener(StyleContainerListener* listener)
158 if (listener && !fListeners.HasItem((void*)listener))
159 return fListeners.AddItem(listener);
160 return false;
163 // RemoveListener
164 bool
165 StyleContainer::RemoveListener(StyleContainerListener* listener)
167 return fListeners.RemoveItem(listener);
170 #endif // ICON_O_MATIC
172 // #pragma mark -
174 // _MakeEmpty
175 void
176 StyleContainer::_MakeEmpty()
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->ReleaseReference();
184 #else
185 delete style;
186 #endif
188 fStyles.MakeEmpty();
191 // #pragma mark -
193 #ifdef ICON_O_MATIC
195 // _NotifyStyleAdded
196 void
197 StyleContainer::_NotifyStyleAdded(Style* style, int32 index) const
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);
208 // _NotifyStyleRemoved
209 void
210 StyleContainer::_NotifyStyleRemoved(Style* style) const
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);
221 #endif // ICON_O_MATIC