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"
42 using namespace BPrivate
;
45 TMenuItemGroup::TMenuItemGroup(const char* name
)
52 if (name
!= NULL
&& name
[0] != '\0')
59 TMenuItemGroup::~TMenuItemGroup()
65 while ((item
= RemoveItem((int32
)0)) != NULL
)
72 TMenuItemGroup::AddItem(BMenuItem
* item
)
74 if (!fList
.AddItem(item
))
78 fMenu
->AddGroupItem(this, item
, fList
.IndexOf(item
));
86 TMenuItemGroup::AddItem(BMenuItem
* item
, int32 atIndex
)
88 if (!fList
.AddItem(item
, atIndex
))
92 fMenu
->AddGroupItem(this, item
, atIndex
);
100 TMenuItemGroup::AddItem(BMenu
* menu
)
102 BMenuItem
* item
= new BMenuItem(menu
);
106 if (!AddItem(item
)) {
116 TMenuItemGroup::AddItem(BMenu
* menu
, int32 atIndex
)
118 BMenuItem
* item
= new BMenuItem(menu
);
122 if (!AddItem(item
, atIndex
)) {
132 TMenuItemGroup::RemoveItem(BMenuItem
* item
)
135 fMenu
->RemoveGroupItem(this, item
);
137 return fList
.RemoveItem(item
);
142 TMenuItemGroup::RemoveItem(BMenu
* menu
)
144 BMenuItem
* item
= menu
->Superitem();
148 return RemoveItem(item
);
153 TMenuItemGroup::RemoveItem(int32 index
)
155 BMenuItem
* item
= ItemAt(index
);
159 if (RemoveItem(item
))
167 TMenuItemGroup::ItemAt(int32 index
)
169 return static_cast<BMenuItem
*>(fList
.ItemAt(index
));
174 TMenuItemGroup::CountItems()
176 return fList
.CountItems();
181 TMenuItemGroup::Separated(bool separated
)
183 if (separated
== fHasSeparator
)
186 fHasSeparator
= separated
;
196 TMenuItemGroup::HasSeparator()
198 return fHasSeparator
;
205 TGroupedMenu::TGroupedMenu(const char* name
)
211 TGroupedMenu::~TGroupedMenu()
213 TMenuItemGroup
* group
;
214 while ((group
= static_cast<TMenuItemGroup
*>(fGroups
.RemoveItem((int32
)0)))
222 TGroupedMenu::AddGroup(TMenuItemGroup
* group
)
224 if (!fGroups
.AddItem(group
))
229 for (int32 i
= 0; i
< group
->CountItems(); i
++) {
230 AddGroupItem(group
, group
->ItemAt(i
), i
);
238 TGroupedMenu::AddGroup(TMenuItemGroup
* group
, int32 atIndex
)
240 if (!fGroups
.AddItem(group
, atIndex
))
245 for (int32 i
= 0; i
< group
->CountItems(); i
++) {
246 AddGroupItem(group
, group
->ItemAt(i
), i
);
254 TGroupedMenu::RemoveGroup(TMenuItemGroup
* group
)
256 if (group
->HasSeparator()) {
257 delete RemoveItem(group
->fFirstItemIndex
);
258 group
->Separated(false);
262 group
->fFirstItemIndex
= -1;
264 for (int32 i
= 0; i
< group
->CountItems(); i
++) {
265 RemoveItem(group
->ItemAt(i
));
268 return fGroups
.RemoveItem(group
);
273 TGroupedMenu::GroupAt(int32 index
)
275 return static_cast<TMenuItemGroup
*>(fGroups
.ItemAt(index
));
280 TGroupedMenu::CountGroups()
282 return fGroups
.CountItems();
287 TGroupedMenu::AddGroupItem(TMenuItemGroup
* group
, BMenuItem
* item
,
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
;
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
311 group
->fFirstItemIndex
= CountItems();
312 if (group
->fFirstItemIndex
> 0)
318 AddItem(new BSeparatorItem(), group
->fFirstItemIndex
);
319 group
->Separated(true);
323 // insert item for real
326 atIndex
+ group
->fFirstItemIndex
+ (group
->HasSeparator() ? 1 : 0));
328 // move the groups after this one
330 for (int32 i
= groupIndex
+ 1; i
< CountGroups(); i
++) {
332 group
->fFirstItemIndex
+= addSeparator
? 2 : 1;
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
356 // move the groups after this one
358 for (int32 i
= groupIndex
+ 1; i
< CountGroups(); i
++) {
360 group
->fFirstItemIndex
-= removedSeparator
? 2 : 1;