Changeset 24158

Show
Ignore:
Timestamp:
02/27/08 17:54:37 (9 months ago)
Author:
anevilyak
Message:

Implement BOutlineListView's derivative of SwapItems(). This makes things like Vision's network reordering shortcuts
work correctly.

Location:
haiku/trunk
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • haiku/trunk/headers/os/interface/OutlineListView.h

    r17812 r24158  
    118118 
    119119                BListItem*                      _RemoveItem(BListItem* item, int32 fullListIndex); 
     120                bool                            _SwapItems(int32 first, int32 second); 
     121                                                void _DoSwap(BList &list, int32 firstIndex, int32 secondIndex,  
     122                                                                int32 firstCount, int32 secondCount, int32 swapCount); 
     123                                                int32 _GetSubitemCount(BList &list, int32 itemIndex); 
    120124                BListItem*                      RemoveOne(int32 fullListIndex); 
    121125 
  • haiku/trunk/src/kits/interface/OutlineListView.cpp

    r24092 r24158  
    524524        BListItem *item = FullListItemAt(fullListIndex); 
    525525        if (!item) 
    526                 return false; 
     526                return false;    
    527527 
    528528        return item->IsExpanded(); 
     
    642642} 
    643643 
    644  
    645644void 
    646645BOutlineListView::_PopulateTree(BList* tree, BList& target, 
     
    812811} 
    813812 
     813int32 
     814BOutlineListView::_GetSubitemCount(BList &list, int32 itemIndex) 
     815{ 
     816        uint32 level = ((BListItem *)list.ItemAt(itemIndex))->OutlineLevel(); 
     817        int32 count = 1; // the count we return includes the parent 
     818        for (int32 i = itemIndex + 1; i < fFullList.CountItems(); i++, count++) { 
     819                if (((BListItem *)list.ItemAt(i))->OutlineLevel() <= level) 
     820                        break; 
     821        } 
     822 
     823        return count; 
     824} 
     825 
     826void 
     827BOutlineListView::_DoSwap(BList &list, int32 firstIndex, int32 secondIndex, int32 firstCount,  
     828        int32 secondCount, int32 swapCount) 
     829{ 
     830        if (firstCount < secondCount) {  
     831                for (int32 i = swapCount + 1; i < secondCount; i++) 
     832                        list.MoveItem(secondIndex + swapCount + i, firstIndex + i); 
     833        } else { 
     834                for (int32 i = swapCount + 1; i < firstCount; i++) 
     835                        list.MoveItem(firstIndex + swapCount + 1, secondIndex + swapCount + 1); 
     836        } 
     837} 
     838 
     839bool 
     840BOutlineListView::_SwapItems(int32 first, int32 second) 
     841{ 
     842        // same item, do nothing 
     843        if (first == second) 
     844                return true; 
     845 
     846        // fail, first item out of bounds 
     847        if ((first < 0) || (first >= CountItems())) 
     848                return false; 
     849 
     850        // fail, second item out of bounds 
     851        if ((second < 0) || (second >= CountItems())) 
     852                return false; 
     853 
     854        int32 firstIndex = min_c(first, second); 
     855        int32 secondIndex = max_c(first, second); 
     856        BListItem *firstItem = ItemAt(firstIndex); 
     857        BListItem *secondItem = ItemAt(secondIndex); 
     858 
     859        if (Superitem(firstItem) != Superitem(secondItem)) 
     860                return false; 
     861 
     862        if (!firstItem->IsItemVisible() || !secondItem->IsItemVisible()) 
     863                return false; 
     864         
     865        int32 fullFirstIndex = FullListIndex(firstIndex); 
     866        int32 fullSecondIndex = FullListIndex(secondIndex); 
     867        int32 firstCount = _GetSubitemCount(fFullList, fullFirstIndex); 
     868        int32 secondCount = _GetSubitemCount(fFullList, fullSecondIndex); 
     869                 
     870        int32 index = (firstCount < secondCount) ? firstCount : secondCount; 
     871        for (int32 i = 0; i < index; i++)  
     872                fFullList.SwapItems(fullFirstIndex + i, fullSecondIndex + i); 
     873        _DoSwap(fFullList, fullFirstIndex, fullSecondIndex, firstCount, secondCount, index); 
     874         
     875        firstCount = _GetSubitemCount(fList, firstIndex); 
     876        secondCount = _GetSubitemCount(fList, secondIndex); 
     877        index = (firstCount < secondCount) ? firstCount : secondCount; 
     878        for (int32 i = 0; i < index; i++) 
     879                fList.SwapItems(firstIndex + i, secondIndex + i); 
     880        _DoSwap(fList, firstIndex, secondIndex, firstCount, secondCount, index);         
     881 
     882        _RecalcItemTops(firstIndex); 
     883        Invalidate(Bounds());    
     884        return true; 
     885} 
    814886 
    815887bool 
    816888BOutlineListView::DoMiscellaneous(MiscCode code, MiscData* data) 
    817889{ 
    818         if (code == B_SWAP_OP) { 
    819                 // todo: If we do a swap and the items in question have children, we need 
    820                 // to move the child hierarchy together with the item if we want to correctly 
    821                 // mimic R5 behavior. 
    822         }  
    823                  
     890        if (code == B_SWAP_OP) 
     891                return _SwapItems(data->swap.a, data->swap.b); 
     892         
    824893        return BListView::DoMiscellaneous(code, data); 
    825894}