Updates to BOutlineListView for docs

mostly param naming.

No functional change intended.
This commit is contained in:
John Scipione
2013-12-11 16:13:52 -05:00
parent 853f724631
commit 1e1afb3939
2 changed files with 96 additions and 88 deletions
+12 -15
View File
@@ -14,7 +14,7 @@ public:
BOutlineListView(BRect frame, const char* name, BOutlineListView(BRect frame, const char* name,
list_view_type type list_view_type type
= B_SINGLE_SELECTION_LIST, = B_SINGLE_SELECTION_LIST,
uint32 resizeMode uint32 resizingMode
= B_FOLLOW_LEFT | B_FOLLOW_TOP, = B_FOLLOW_LEFT | B_FOLLOW_TOP,
uint32 flags = B_WILL_DRAW uint32 flags = B_WILL_DRAW
| B_FRAME_EVENTS | B_NAVIGABLE); | B_FRAME_EVENTS | B_NAVIGABLE);
@@ -36,8 +36,7 @@ public:
virtual void FrameResized(float newWidth, float newHeight); virtual void FrameResized(float newWidth, float newHeight);
virtual void MouseUp(BPoint where); virtual void MouseUp(BPoint where);
virtual bool AddUnder(BListItem* item, virtual bool AddUnder(BListItem* item, BListItem* superItem);
BListItem* underItem);
virtual bool AddItem(BListItem* item); virtual bool AddItem(BListItem* item);
virtual bool AddItem(BListItem* item, int32 fullListIndex); virtual bool AddItem(BListItem* item, int32 fullListIndex);
@@ -49,7 +48,7 @@ public:
virtual bool RemoveItems(int32 fullListIndex, int32 count); virtual bool RemoveItems(int32 fullListIndex, int32 count);
BListItem* FullListItemAt(int32 fullListIndex) const; BListItem* FullListItemAt(int32 fullListIndex) const;
int32 FullListIndexOf(BPoint point) const; int32 FullListIndexOf(BPoint where) const;
int32 FullListIndexOf(BListItem* item) const; int32 FullListIndexOf(BListItem* item) const;
BListItem* FullListFirstItem() const; BListItem* FullListFirstItem() const;
BListItem* FullListLastItem() const; BListItem* FullListLastItem() const;
@@ -60,11 +59,9 @@ public:
virtual void MakeEmpty(); virtual void MakeEmpty();
bool FullListIsEmpty() const; bool FullListIsEmpty() const;
void FullListDoForEach( void FullListDoForEach(bool (*func)(BListItem* item));
bool (*func)(BListItem* item)); void FullListDoForEach(bool (*func)(BListItem* item, void* arg),
void FullListDoForEach( void* arg);
bool (*func)(BListItem* item, void*),
void*);
BListItem* Superitem(const BListItem* item); BListItem* Superitem(const BListItem* item);
@@ -90,17 +87,17 @@ public:
void FullListSortItems(int (*compareFunc)( void FullListSortItems(int (*compareFunc)(
const BListItem* first, const BListItem* first,
const BListItem* second)); const BListItem* second));
void SortItemsUnder(BListItem* underItem, void SortItemsUnder(BListItem* superItem,
bool oneLevelOnly, int (*compareFunc)( bool oneLevelOnly, int (*compareFunc)(
const BListItem* first, const BListItem* first,
const BListItem* second)); const BListItem* second));
int32 CountItemsUnder(BListItem* under, int32 CountItemsUnder(BListItem* superItem,
bool oneLevelOnly) const; bool oneLevelOnly) const;
BListItem* EachItemUnder(BListItem* underItem, BListItem* EachItemUnder(BListItem* superItem,
bool oneLevelOnly, BListItem* (*eachFunc)( bool oneLevelOnly, BListItem* (*eachFunc)(
BListItem* item, void* arg), BListItem* item, void* arg),
void* arg); void* arg);
BListItem* ItemUnderAt(BListItem* underItem, BListItem* ItemUnderAt(BListItem* superItem,
bool oneLevelOnly, int32 index) const; bool oneLevelOnly, int32 index) const;
protected: protected:
@@ -114,7 +111,7 @@ private:
virtual void _ReservedOutlineListView4(); virtual void _ReservedOutlineListView4();
protected: protected:
virtual void ExpandOrCollapse(BListItem* underItem, virtual void ExpandOrCollapse(BListItem* superItem,
bool expand); bool expand);
virtual BRect LatchRect(BRect itemRect, int32 level) const; virtual BRect LatchRect(BRect itemRect, int32 level) const;
virtual void DrawLatch(BRect itemRect, int32 level, virtual void DrawLatch(BRect itemRect, int32 level,
@@ -132,7 +129,7 @@ private:
int (*compareFunc)(const BListItem* a, int (*compareFunc)(const BListItem* a,
const BListItem* b)); const BListItem* b));
void _DestructTree(BList* tree); void _DestructTree(BList* tree);
BList* _BuildTree(BListItem* underItem, int32& index); BList* _BuildTree(BListItem* superItem, int32& index);
void _CullInvisibleItems(BList &list); void _CullInvisibleItems(BList &list);
bool _SwapItems(int32 first, int32 second); bool _SwapItems(int32 first, int32 second);
+84 -73
View File
@@ -77,21 +77,24 @@ _DoSwap(BList& list, int32 firstIndex, int32 secondIndex, BList* firstItems,
BOutlineListView::BOutlineListView(BRect frame, const char* name, BOutlineListView::BOutlineListView(BRect frame, const char* name,
list_view_type type, uint32 resizeMode, uint32 flags) list_view_type type, uint32 resizingMode, uint32 flags)
: BListView(frame, name, type, resizeMode, flags) :
BListView(frame, name, type, resizingMode, flags)
{ {
} }
BOutlineListView::BOutlineListView(const char* name, list_view_type type, BOutlineListView::BOutlineListView(const char* name, list_view_type type,
uint32 flags) uint32 flags)
: BListView(name, type, flags) :
BListView(name, type, flags)
{ {
} }
BOutlineListView::BOutlineListView(BMessage* archive) BOutlineListView::BOutlineListView(BMessage* archive)
: BListView(archive) :
BListView(archive)
{ {
int32 i = 0; int32 i = 0;
BMessage subData; BMessage subData;
@@ -160,23 +163,23 @@ BOutlineListView::Archive(BMessage* archive, bool deep) const
void void
BOutlineListView::MouseDown(BPoint point) BOutlineListView::MouseDown(BPoint where)
{ {
MakeFocus(); MakeFocus();
int32 index = IndexOf(point); int32 index = IndexOf(where);
if (index != -1) { if (index != -1) {
BListItem* item = ItemAt(index); BListItem* item = ItemAt(index);
if (item->fHasSubitems if (item->fHasSubitems
&& LatchRect(ItemFrame(index), item->fLevel).Contains(point)) { && LatchRect(ItemFrame(index), item->fLevel).Contains(where)) {
if (item->IsExpanded()) if (item->IsExpanded())
Collapse(item); Collapse(item);
else else
Expand(item); Expand(item);
} else } else
BListView::MouseDown(point); BListView::MouseDown(where);
} }
} }
@@ -242,23 +245,23 @@ BOutlineListView::MouseUp(BPoint where)
bool bool
BOutlineListView::AddUnder(BListItem* item, BListItem* superitem) BOutlineListView::AddUnder(BListItem* item, BListItem* superItem)
{ {
if (superitem == NULL) if (superItem == NULL)
return AddItem(item); return AddItem(item);
fFullList.AddItem(item, FullListIndexOf(superitem) + 1); fFullList.AddItem(item, FullListIndexOf(superItem) + 1);
item->fLevel = superitem->OutlineLevel() + 1; item->fLevel = superItem->OutlineLevel() + 1;
superitem->fHasSubitems = true; superItem->fHasSubitems = true;
if (superitem->IsItemVisible() && superitem->IsExpanded()) { if (superItem->IsItemVisible() && superItem->IsExpanded()) {
item->SetItemVisible(true); item->SetItemVisible(true);
int32 index = BListView::IndexOf(superitem); int32 index = BListView::IndexOf(superItem);
BListView::AddItem(item, index + 1); BListView::AddItem(item, index + 1);
Invalidate(LatchRect(ItemFrame(index), superitem->OutlineLevel())); Invalidate(LatchRect(ItemFrame(index), superItem->OutlineLevel()));
} else } else
item->SetItemVisible(false); item->SetItemVisible(false);
@@ -300,8 +303,10 @@ BOutlineListView::AddItem(BListItem* item, int32 fullListIndex)
return true; return true;
} }
if (!hadSubitems) if (!hadSubitems) {
Invalidate(LatchRect(ItemFrame(IndexOf(super)), super->OutlineLevel())); Invalidate(LatchRect(ItemFrame(IndexOf(super)),
super->OutlineLevel()));
}
} }
int32 listIndex = _FindPreviousVisibleIndex(fullListIndex); int32 listIndex = _FindPreviousVisibleIndex(fullListIndex);
@@ -344,24 +349,23 @@ BOutlineListView::RemoveItem(BListItem* item)
BListItem* BListItem*
BOutlineListView::RemoveItem(int32 fullIndex) BOutlineListView::RemoveItem(int32 fullListIndex)
{ {
return _RemoveItem(FullListItemAt(fullIndex), fullIndex); return _RemoveItem(FullListItemAt(fullListIndex), fullListIndex);
} }
bool bool
BOutlineListView::RemoveItems(int32 fullIndex, int32 count) BOutlineListView::RemoveItems(int32 fullListIndex, int32 count)
{ {
if (fullIndex >= FullListCountItems()) if (fullListIndex >= FullListCountItems())
fullIndex = -1; fullListIndex = -1;
if (fullIndex < 0) if (fullListIndex < 0)
return false; return false;
// TODO: very bad for performance!! // TODO: very bad for performance!!
while (count--) { while (count--)
BOutlineListView::RemoveItem(fullIndex); BOutlineListView::RemoveItem(fullListIndex);
}
return true; return true;
} }
@@ -375,9 +379,9 @@ BOutlineListView::FullListItemAt(int32 fullListIndex) const
int32 int32
BOutlineListView::FullListIndexOf(BPoint point) const BOutlineListView::FullListIndexOf(BPoint where) const
{ {
int32 index = BListView::IndexOf(point); int32 index = BListView::IndexOf(where);
if (index > 0) if (index > 0)
index = _FullListIndex(index); index = _FullListIndex(index);
@@ -622,26 +626,26 @@ BOutlineListView::FullListSortItems(int (*compareFunc)(const BListItem* a,
void void
BOutlineListView::SortItemsUnder(BListItem* underItem, bool oneLevelOnly, BOutlineListView::SortItemsUnder(BListItem* superItem, bool oneLevelOnly,
int (*compareFunc)(const BListItem* a, const BListItem* b)) int (*compareFunc)(const BListItem* a, const BListItem* b))
{ {
// This method is quite complicated: basically, it creates a real tree // This method is quite complicated: basically, it creates a real tree
// from the items of the full list, sorts them as needed, and then // from the items of the full list, sorts them as needed, and then
// populates the entries back into the full and display lists // populates the entries back into the full and display lists
int32 firstIndex = FullListIndexOf(underItem) + 1; int32 firstIndex = FullListIndexOf(superItem) + 1;
int32 lastIndex = firstIndex; int32 lastIndex = firstIndex;
BList* tree = _BuildTree(underItem, lastIndex); BList* tree = _BuildTree(superItem, lastIndex);
_SortTree(tree, oneLevelOnly, compareFunc); _SortTree(tree, oneLevelOnly, compareFunc);
// Populate to the full list // Populate to the full list
_PopulateTree(tree, fFullList, firstIndex, false); _PopulateTree(tree, fFullList, firstIndex, false);
if (underItem == NULL if (superItem == NULL
|| (underItem->IsItemVisible() && underItem->IsExpanded())) { || (superItem->IsItemVisible() && superItem->IsExpanded())) {
// Populate to BListView's list // Populate to BListView's list
firstIndex = fList.IndexOf(underItem) + 1; firstIndex = fList.IndexOf(superItem) + 1;
lastIndex = firstIndex; lastIndex = firstIndex;
_PopulateTree(tree, fList, lastIndex, true); _PopulateTree(tree, fList, lastIndex, true);
@@ -664,15 +668,15 @@ BOutlineListView::SortItemsUnder(BListItem* underItem, bool oneLevelOnly,
int32 int32
BOutlineListView::CountItemsUnder(BListItem* underItem, bool oneLevelOnly) const BOutlineListView::CountItemsUnder(BListItem* superItem, bool oneLevelOnly) const
{ {
int32 i = FullListIndexOf(underItem); int32 i = FullListIndexOf(superItem);
if (i == -1) if (i == -1)
return 0; return 0;
++i; ++i;
int32 count = 0; int32 count = 0;
uint32 baseLevel = underItem->OutlineLevel(); uint32 baseLevel = superItem->OutlineLevel();
for (; i < FullListCountItems(); i++) { for (; i < FullListCountItems(); i++) {
BListItem* item = FullListItemAt(i); BListItem* item = FullListItemAt(i);
@@ -691,10 +695,10 @@ BOutlineListView::CountItemsUnder(BListItem* underItem, bool oneLevelOnly) const
BListItem* BListItem*
BOutlineListView::EachItemUnder(BListItem* underItem, bool oneLevelOnly, BOutlineListView::EachItemUnder(BListItem* superItem, bool oneLevelOnly,
BListItem* (*eachFunc)(BListItem* item, void* arg), void* arg) BListItem* (*eachFunc)(BListItem* item, void* arg), void* arg)
{ {
int32 i = IndexOf(underItem); int32 i = IndexOf(superItem);
if (i == -1) if (i == -1)
return NULL; return NULL;
@@ -702,11 +706,11 @@ BOutlineListView::EachItemUnder(BListItem* underItem, bool oneLevelOnly,
BListItem* item = FullListItemAt(i); BListItem* item = FullListItemAt(i);
// If we jump out of the subtree, return NULL // If we jump out of the subtree, return NULL
if (item->fLevel < underItem->OutlineLevel()) if (item->fLevel < superItem->OutlineLevel())
return NULL; return NULL;
// If the level matches, check the index // If the level matches, check the index
if (!oneLevelOnly || item->fLevel == underItem->OutlineLevel() + 1) { if (!oneLevelOnly || item->fLevel == superItem->OutlineLevel() + 1) {
item = eachFunc(item, arg); item = eachFunc(item, arg);
if (item != NULL) if (item != NULL)
return item; return item;
@@ -720,10 +724,10 @@ BOutlineListView::EachItemUnder(BListItem* underItem, bool oneLevelOnly,
BListItem* BListItem*
BOutlineListView::ItemUnderAt(BListItem* underItem, BOutlineListView::ItemUnderAt(BListItem* superItem, bool oneLevelOnly,
bool oneLevelOnly, int32 index) const int32 index) const
{ {
int32 i = FullListIndexOf(underItem); int32 i = FullListIndexOf(superItem);
if (i == -1) if (i == -1)
return NULL; return NULL;
@@ -731,11 +735,11 @@ BOutlineListView::ItemUnderAt(BListItem* underItem,
BListItem* item = FullListItemAt(i); BListItem* item = FullListItemAt(i);
// If we jump out of the subtree, return NULL // If we jump out of the subtree, return NULL
if (item->fLevel < underItem->OutlineLevel()) if (item->fLevel < superItem->OutlineLevel())
return NULL; return NULL;
// If the level matches, check the index // If the level matches, check the index
if (!oneLevelOnly || item->fLevel == underItem->OutlineLevel() + 1) { if (!oneLevelOnly || item->fLevel == superItem->OutlineLevel() + 1) {
if (index == 0) if (index == 0)
return item; return item;
@@ -784,11 +788,11 @@ BOutlineListView::ExpandOrCollapse(BListItem* item, bool expand)
if (expand) { if (expand) {
uint32 level = item->fLevel; uint32 level = item->fLevel;
int32 fullIndex = FullListIndexOf(item); int32 fullListIndex = FullListIndexOf(item);
int32 index = IndexOf(item) + 1; int32 index = IndexOf(item) + 1;
int32 startIndex = index; int32 startIndex = index;
int32 count = FullListCountItems() - fullIndex - 1; int32 count = FullListCountItems() - fullListIndex - 1;
BListItem** items = (BListItem**)fFullList.Items() + fullIndex + 1; BListItem** items = (BListItem**)fFullList.Items() + fullListIndex + 1;
BFont font; BFont font;
GetFont(&font); GetFont(&font);
@@ -823,14 +827,14 @@ BOutlineListView::ExpandOrCollapse(BListItem* item, bool expand)
} else { } else {
// collapse // collapse
uint32 level = item->fLevel; uint32 level = item->fLevel;
int32 fullIndex = FullListIndexOf(item); int32 fullListIndex = FullListIndexOf(item);
int32 index = IndexOf(item); int32 index = IndexOf(item);
int32 startIndex = index; int32 startIndex = index;
int32 max = FullListCountItems() - fullIndex - 1; int32 max = FullListCountItems() - fullListIndex - 1;
int32 count = 0; int32 count = 0;
bool selectionChanged = false; bool selectionChanged = false;
BListItem** items = (BListItem**)fFullList.Items() + fullIndex + 1; BListItem** items = (BListItem**)fFullList.Items() + fullListIndex + 1;
while (max-- > 0) { while (max-- > 0) {
item = items[0]; item = items[0];
@@ -856,6 +860,7 @@ BOutlineListView::ExpandOrCollapse(BListItem* item, bool expand)
// parent // parent
if (ListType() == B_SINGLE_SELECTION_LIST && selectionChanged) if (ListType() == B_SINGLE_SELECTION_LIST && selectionChanged)
fFirstSelected = fLastSelected = index; fFirstSelected = fLastSelected = index;
if (index < fFirstSelected && index + count < fFirstSelected) { if (index < fFirstSelected && index + count < fFirstSelected) {
// all items removed were higher than the selection range, // all items removed were higher than the selection range,
// adjust the indexes to correspond to their new visible positions // adjust the indexes to correspond to their new visible positions
@@ -866,8 +871,10 @@ BOutlineListView::ExpandOrCollapse(BListItem* item, bool expand)
int32 maxIndex = fList.CountItems() - 1; int32 maxIndex = fList.CountItems() - 1;
if (fFirstSelected > maxIndex) if (fFirstSelected > maxIndex)
fFirstSelected = maxIndex; fFirstSelected = maxIndex;
if (fLastSelected > maxIndex) if (fLastSelected > maxIndex)
fLastSelected = maxIndex; fLastSelected = maxIndex;
if (selectionChanged) if (selectionChanged)
SelectionChanged(); SelectionChanged();
} }
@@ -942,8 +949,10 @@ BOutlineListView::_PopulateTree(BList* tree, BList& target,
items[firstIndex++] = item; items[firstIndex++] = item;
if (item->HasSubitems() && (!onlyVisible || item->IsExpanded())) if (item->HasSubitems() && (!onlyVisible || item->IsExpanded())) {
_PopulateTree(item->fTemporaryList, target, firstIndex, onlyVisible); _PopulateTree(item->fTemporaryList, target, firstIndex,
onlyVisible);
}
} }
} }
@@ -953,7 +962,8 @@ BOutlineListView::_SortTree(BList* tree, bool oneLevelOnly,
int (*compareFunc)(const BListItem* a, const BListItem* b)) int (*compareFunc)(const BListItem* a, const BListItem* b))
{ {
BListItem** items = (BListItem**)tree->Items(); BListItem** items = (BListItem**)tree->Items();
std::sort(items, items + tree->CountItems(), ListItemComparator(compareFunc)); std::sort(items, items + tree->CountItems(),
ListItemComparator(compareFunc));
if (oneLevelOnly) if (oneLevelOnly)
return; return;
@@ -982,16 +992,16 @@ BOutlineListView::_DestructTree(BList* tree)
BList* BList*
BOutlineListView::_BuildTree(BListItem* underItem, int32& fullIndex) BOutlineListView::_BuildTree(BListItem* superItem, int32& fullListIndex)
{ {
int32 fullCount = FullListCountItems(); int32 fullCount = FullListCountItems();
uint32 level = underItem != NULL ? underItem->OutlineLevel() + 1 : 0; uint32 level = superItem != NULL ? superItem->OutlineLevel() + 1 : 0;
BList* list = new BList; BList* list = new BList;
if (underItem != NULL) if (superItem != NULL)
underItem->fTemporaryList = list; superItem->fTemporaryList = list;
while (fullIndex < fullCount) { while (fullListIndex < fullCount) {
BListItem* item = FullListItemAt(fullIndex); BListItem* item = FullListItemAt(fullListIndex);
// If we jump out of the subtree, break out // If we jump out of the subtree, break out
if (item->fLevel < level) if (item->fLevel < level)
@@ -1000,11 +1010,11 @@ BOutlineListView::_BuildTree(BListItem* underItem, int32& fullIndex)
// If the level matches, put them into the list // If the level matches, put them into the list
// (we handle the case of a missing sublevel gracefully) // (we handle the case of a missing sublevel gracefully)
list->AddItem(item); list->AddItem(item);
fullIndex++; fullListIndex++;
if (item->HasSubitems()) { if (item->HasSubitems()) {
// we're going deeper // we're going deeper
_BuildTree(item, fullIndex); _BuildTree(item, fullListIndex);
} }
} }
@@ -1078,19 +1088,21 @@ BOutlineListView::_SwapItems(int32 first, int32 second)
account. account.
*/ */
BListItem* BListItem*
BOutlineListView::_RemoveItem(BListItem* item, int32 fullIndex) BOutlineListView::_RemoveItem(BListItem* item, int32 fullListIndex)
{ {
if (item == NULL || fullIndex < 0 || fullIndex >= FullListCountItems()) if (item == NULL || fullListIndex < 0
|| fullListIndex >= FullListCountItems()) {
return NULL; return NULL;
}
uint32 level = item->OutlineLevel(); uint32 level = item->OutlineLevel();
int32 superIndex; int32 superIndex;
BListItem* super = _SuperitemForIndex(fullIndex, level, &superIndex); BListItem* super = _SuperitemForIndex(fullListIndex, level, &superIndex);
if (item->IsItemVisible()) { if (item->IsItemVisible()) {
// remove children, too // remove children, too
while (fullIndex + 1 < FullListCountItems()) { while (fullListIndex + 1 < FullListCountItems()) {
BListItem* subItem = FullListItemAt(fullIndex + 1); BListItem* subItem = FullListItemAt(fullListIndex + 1);
if (subItem->OutlineLevel() <= level) if (subItem->OutlineLevel() <= level)
break; break;
@@ -1098,13 +1110,13 @@ BOutlineListView::_RemoveItem(BListItem* item, int32 fullIndex)
if (subItem->IsItemVisible()) if (subItem->IsItemVisible())
BListView::RemoveItem(subItem); BListView::RemoveItem(subItem);
fFullList.RemoveItem(fullIndex + 1); fFullList.RemoveItem(fullListIndex + 1);
delete subItem; delete subItem;
} }
BListView::RemoveItem(item); BListView::RemoveItem(item);
} }
fFullList.RemoveItem(fullIndex); fFullList.RemoveItem(fullListIndex);
if (super != NULL) { if (super != NULL) {
// we might need to change the fHasSubitems field of the parent // we might need to change the fHasSubitems field of the parent
@@ -1155,4 +1167,3 @@ BOutlineListView::_FindPreviousVisibleIndex(int32 fullListIndex)
return -1; return -1;
} }