bool BListView::_MoveItem(int32 from, int32 to) { // remember item frames before doing anything BRect frameFrom = ItemFrame(from); BRect frameTo = ItemFrame(to); if (!fList.MoveItem(from, to)) return false; // track anchor item if (fAnchorIndex == from) fAnchorIndex = to; // track selection if (ItemAt(to)->IsSelected()) { _RescanSelection(from, to); // though the actually selected items stayed the // same, the selection has still changed SelectionChanged(); } _RecalcItemTops((to > from) ? from : to); // take care of invalidation if (Window()) { // NOTE: window looper is assumed to be locked! Invalidate(frameFrom | frameTo); } return true; }
bool BListView::_SwapItems(int32 a, int32 b) { // remember frames of items before anyhing happens, // the tricky situation is when the two items have // a different height BRect aFrame = ItemFrame(a); BRect bFrame = ItemFrame(b); if (!fList.SwapItems(a, b)) return false; if (a == b) { // nothing to do, but success nevertheless return true; } // track anchor item if (fAnchorIndex == a) fAnchorIndex = b; else if (fAnchorIndex == b) fAnchorIndex = a; // track selection // NOTE: this is only important if the selection status // of both items is not the same int32 first = min_c(a, b); int32 last = max_c(a, b); if (ItemAt(a)->IsSelected() != ItemAt(b)->IsSelected()) { if (first < fFirstSelected || last > fLastSelected) { _RescanSelection(min_c(first, fFirstSelected), max_c(last, fLastSelected)); } // though the actually selected items stayed the // same, the selection has still changed SelectionChanged(); } ItemAt(a)->SetTop(aFrame.top); ItemAt(b)->SetTop(bFrame.top); // take care of invalidation if (Window()) { // NOTE: window looper is assumed to be locked! if (aFrame.Height() != bFrame.Height()) { _RecalcItemTops(first, last); // items in between shifted visually Invalidate(aFrame | bFrame); } else { Invalidate(aFrame); Invalidate(bFrame); } } return true; }
bool BListView::_ReplaceItem(int32 index, BListItem* item) { if (item == NULL) return false; BListItem* old = ItemAt(index); if (!old) return false; BRect frame = ItemFrame(index); bool selectionChanged = old->IsSelected() != item->IsSelected(); // replace item if (!fList.ReplaceItem(index, item)) return false; // tack selection if (selectionChanged) { int32 start = min_c(fFirstSelected, index); int32 end = max_c(fLastSelected, index); _RescanSelection(start, end); SelectionChanged(); } _RecalcItemTops(index); bool itemHeightChanged = frame != ItemFrame(index); // take care of invalidation if (Window()) { // NOTE: window looper is assumed to be locked! if (itemHeightChanged) _InvalidateFrom(index); else Invalidate(frame); } if (itemHeightChanged) _FixupScrollBar(); return true; }
bool BOutlineListView::_SwapItems(int32 first, int32 second) { // same item, do nothing if (first == second) return true; // fail, first item out of bounds if ((first < 0) || (first >= CountItems())) return false; // fail, second item out of bounds if ((second < 0) || (second >= CountItems())) return false; int32 firstIndex = min_c(first, second); int32 secondIndex = max_c(first, second); BListItem* firstItem = ItemAt(firstIndex); BListItem* secondItem = ItemAt(secondIndex); BList firstSubItems, secondSubItems; if (Superitem(firstItem) != Superitem(secondItem)) return false; if (!firstItem->IsItemVisible() || !secondItem->IsItemVisible()) return false; int32 fullFirstIndex = _FullListIndex(firstIndex); int32 fullSecondIndex = _FullListIndex(secondIndex); _GetSubItems(fFullList, firstSubItems, firstItem, fullFirstIndex + 1); _GetSubItems(fFullList, secondSubItems, secondItem, fullSecondIndex + 1); _DoSwap(fFullList, fullFirstIndex, fullSecondIndex, &firstSubItems, &secondSubItems); _CullInvisibleItems(firstSubItems); _CullInvisibleItems(secondSubItems); _DoSwap(fList, firstIndex, secondIndex, &firstSubItems, &secondSubItems); _RecalcItemTops(firstIndex); _RescanSelection(firstIndex, secondIndex + secondSubItems.CountItems()); Invalidate(Bounds()); return true; }