bin/pc: Mark non-returning function as void
[haiku.git] / src / kits / tracker / GroupedMenu.cpp
blob3fbd0ab296dce5d6ba152813a9fd738ae653d702
1 /*
2 Open Tracker License
4 Terms and Conditions
6 Copyright (c) 1991-2000, Be Incorporated. All rights reserved.
8 Permission is hereby granted, free of charge, to any person obtaining a copy of
9 this software and associated documentation files (the "Software"), to deal in
10 the Software without restriction, including without limitation the rights to
11 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12 of the Software, and to permit persons to whom the Software is furnished to do
13 so, subject to the following conditions:
15 The above copyright notice and this permission notice applies to all licensees
16 and shall be included in all copies or substantial portions of the Software.
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
22 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN
23 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 Except as contained in this notice, the name of Be Incorporated shall not be
26 used in advertising or otherwise to promote the sale, use or other dealings in
27 this Software without prior written authorization from Be Incorporated.
29 Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered
30 trademarks of Be Incorporated in the United States and other countries. Other
31 brand product names are registered trademarks or trademarks of their
32 respective holders. All rights reserved.
36 #include "GroupedMenu.h"
38 #include <stdlib.h>
39 #include <string.h>
42 using namespace BPrivate;
45 TMenuItemGroup::TMenuItemGroup(const char* name)
47 fMenu(NULL),
48 fFirstItemIndex(-1),
49 fItemsTotal(0),
50 fHasSeparator(false)
52 if (name != NULL && name[0] != '\0')
53 fName = strdup(name);
54 else
55 fName = NULL;
59 TMenuItemGroup::~TMenuItemGroup()
61 free((char*)fName);
63 if (fMenu == NULL) {
64 BMenuItem* item;
65 while ((item = RemoveItem((int32)0)) != NULL)
66 delete item;
71 bool
72 TMenuItemGroup::AddItem(BMenuItem* item)
74 if (!fList.AddItem(item))
75 return false;
77 if (fMenu)
78 fMenu->AddGroupItem(this, item, fList.IndexOf(item));
80 fItemsTotal++;
81 return true;
85 bool
86 TMenuItemGroup::AddItem(BMenuItem* item, int32 atIndex)
88 if (!fList.AddItem(item, atIndex))
89 return false;
91 if (fMenu)
92 fMenu->AddGroupItem(this, item, atIndex);
94 fItemsTotal++;
95 return true;
99 bool
100 TMenuItemGroup::AddItem(BMenu* menu)
102 BMenuItem* item = new BMenuItem(menu);
103 if (item == NULL)
104 return false;
106 if (!AddItem(item)) {
107 delete item;
108 return false;
111 return true;
115 bool
116 TMenuItemGroup::AddItem(BMenu* menu, int32 atIndex)
118 BMenuItem* item = new BMenuItem(menu);
119 if (item == NULL)
120 return false;
122 if (!AddItem(item, atIndex)) {
123 delete item;
124 return false;
127 return true;
131 bool
132 TMenuItemGroup::RemoveItem(BMenuItem* item)
134 if (fMenu)
135 fMenu->RemoveGroupItem(this, item);
137 return fList.RemoveItem(item);
141 bool
142 TMenuItemGroup::RemoveItem(BMenu* menu)
144 BMenuItem* item = menu->Superitem();
145 if (item == NULL)
146 return false;
148 return RemoveItem(item);
152 BMenuItem*
153 TMenuItemGroup::RemoveItem(int32 index)
155 BMenuItem* item = ItemAt(index);
156 if (item == NULL)
157 return NULL;
159 if (RemoveItem(item))
160 return item;
162 return NULL;
166 BMenuItem*
167 TMenuItemGroup::ItemAt(int32 index)
169 return static_cast<BMenuItem*>(fList.ItemAt(index));
173 int32
174 TMenuItemGroup::CountItems()
176 return fList.CountItems();
180 void
181 TMenuItemGroup::Separated(bool separated)
183 if (separated == fHasSeparator)
184 return;
186 fHasSeparator = separated;
188 if (separated)
189 fItemsTotal++;
190 else
191 fItemsTotal--;
195 bool
196 TMenuItemGroup::HasSeparator()
198 return fHasSeparator;
202 // #pragma mark -
205 TGroupedMenu::TGroupedMenu(const char* name)
206 : BMenu(name)
211 TGroupedMenu::~TGroupedMenu()
213 TMenuItemGroup* group;
214 while ((group = static_cast<TMenuItemGroup*>(fGroups.RemoveItem((int32)0)))
215 != NULL) {
216 delete group;
221 bool
222 TGroupedMenu::AddGroup(TMenuItemGroup* group)
224 if (!fGroups.AddItem(group))
225 return false;
227 group->fMenu = this;
229 for (int32 i = 0; i < group->CountItems(); i++) {
230 AddGroupItem(group, group->ItemAt(i), i);
233 return true;
237 bool
238 TGroupedMenu::AddGroup(TMenuItemGroup* group, int32 atIndex)
240 if (!fGroups.AddItem(group, atIndex))
241 return false;
243 group->fMenu = this;
245 for (int32 i = 0; i < group->CountItems(); i++) {
246 AddGroupItem(group, group->ItemAt(i), i);
249 return true;
253 bool
254 TGroupedMenu::RemoveGroup(TMenuItemGroup* group)
256 if (group->HasSeparator()) {
257 delete RemoveItem(group->fFirstItemIndex);
258 group->Separated(false);
261 group->fMenu = NULL;
262 group->fFirstItemIndex = -1;
264 for (int32 i = 0; i < group->CountItems(); i++) {
265 RemoveItem(group->ItemAt(i));
268 return fGroups.RemoveItem(group);
272 TMenuItemGroup*
273 TGroupedMenu::GroupAt(int32 index)
275 return static_cast<TMenuItemGroup*>(fGroups.ItemAt(index));
279 int32
280 TGroupedMenu::CountGroups()
282 return fGroups.CountItems();
286 void
287 TGroupedMenu::AddGroupItem(TMenuItemGroup* group, BMenuItem* item,
288 int32 atIndex)
290 int32 groupIndex = fGroups.IndexOf(group);
291 bool addSeparator = false;
293 if (group->fFirstItemIndex == -1) {
294 // find new home for this group
295 if (groupIndex > 0) {
296 // add this group after an existing one
297 TMenuItemGroup* previous = GroupAt(groupIndex - 1);
298 group->fFirstItemIndex = previous->fFirstItemIndex
299 + previous->fItemsTotal;
300 addSeparator = true;
301 } else {
302 // this is the first group
303 TMenuItemGroup* successor = GroupAt(groupIndex + 1);
304 if (successor != NULL) {
305 group->fFirstItemIndex = successor->fFirstItemIndex;
306 if (successor->fHasSeparator) {
307 // we'll need one as well
308 addSeparator = true;
310 } else {
311 group->fFirstItemIndex = CountItems();
312 if (group->fFirstItemIndex > 0)
313 addSeparator = true;
317 if (addSeparator) {
318 AddItem(new BSeparatorItem(), group->fFirstItemIndex);
319 group->Separated(true);
323 // insert item for real
325 AddItem(item,
326 atIndex + group->fFirstItemIndex + (group->HasSeparator() ? 1 : 0));
328 // move the groups after this one
330 for (int32 i = groupIndex + 1; i < CountGroups(); i++) {
331 group = GroupAt(i);
332 group->fFirstItemIndex += addSeparator ? 2 : 1;
337 void
338 TGroupedMenu::RemoveGroupItem(TMenuItemGroup* group, BMenuItem* item)
340 int32 groupIndex = fGroups.IndexOf(group);
341 bool removedSeparator = false;
343 if (group->CountItems() == 1) {
344 // this is the last item
345 if (group->HasSeparator()) {
346 RemoveItem(group->fFirstItemIndex);
347 group->Separated(false);
348 removedSeparator = true;
352 // insert item for real
354 RemoveItem(item);
356 // move the groups after this one
358 for (int32 i = groupIndex + 1; i < CountGroups(); i++) {
359 group = GroupAt(i);
360 group->fFirstItemIndex -= removedSeparator ? 2 : 1;