| | 813 | int32 |
| | 814 | BOutlineListView::_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 | |
| | 826 | void |
| | 827 | BOutlineListView::_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 | |
| | 839 | bool |
| | 840 | BOutlineListView::_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 | } |