Author: pulkomandy Date: 2010-02-01 22:18:11 +0100 (Mon, 01 Feb 2010) New Revision: 35384 Changeset: http://dev.haiku-os.org/changeset/35384/haiku Modified: haiku/trunk/src/preferences/locale/LocaleWindow.cpp Log: Incomplete handling of moving items around in a list. Child items get lost when you move their parent, but I feel the code is getting too messy and I'm not sure what's the best way to fix that. Modified: haiku/trunk/src/preferences/locale/LocaleWindow.cpp =================================================================== --- haiku/trunk/src/preferences/locale/LocaleWindow.cpp 2010-02-01 19:53:43 UTC (rev 35383) +++ haiku/trunk/src/preferences/locale/LocaleWindow.cpp 2010-02-01 21:18:11 UTC (rev 35384) @@ -100,8 +100,6 @@ if (message->FindPointer("list", (void**)&list) == B_OK) { // It comes from a list if (list == this) { - // TODO : parent item should stay at top level and childs should not - // move under another parent. // It comes from ourselves : move the item around in the list int32 count = CountItems(); if (fDropIndex < 0 || fDropIndex > count) @@ -115,7 +113,28 @@ items.AddItem((void*)item); if (items.CountItems() > 0) { - MoveItems(items, fDropIndex); + // There is something to move + LanguageListItem* parent = + static_cast<LanguageListItem*>(Superitem( + static_cast<LanguageListItem*>( + items.FirstItem()))); + if (parent) { + // item has a parent - it should then stay + // below it + if (Superitem(FullListItemAt(fDropIndex - 1)) + == parent || FullListItemAt(fDropIndex - 1) == parent) + MoveItems(items, fDropIndex); + } else { + // item is top level and should stay so. + if (Superitem(FullListItemAt(fDropIndex - 1)) == NULL) + MoveItems(items, fDropIndex); + else { + int itemCount = CountItemsUnder( + FullListItemAt(fDropIndex), true); + MoveItems(items, FullListIndexOf( + Superitem(FullListItemAt(fDropIndex - 1))+itemCount)); + } + } } fDropIndex = -1; } else { @@ -132,7 +151,7 @@ } // Item is now a top level one - we must insert just below its last child - fDropIndex += CountItemsUnder(FullListItemAt(fDropIndex),false); + fDropIndex += CountItemsUnder(FullListItemAt(fDropIndex),false) + 1; int32 index; for (int32 i = 0; message->FindInt32("index", i, &index) @@ -165,10 +184,13 @@ // spot after removal BList removedItems; int32 count = items.CountItems(); - for (int32 i = 0; i < count; i++) { + // We loop in the reverse way so we can remove childs before their parents + for (int32 i = count - 1; i >= 0; i--) { BListItem* item = (BListItem*)items.ItemAt(i); int32 removeIndex = IndexOf(item); - if (RemoveItem(item) && removedItems.AddItem((void*)item)) { + // TODO : remove all childs before removing the item itself, or else + // they will be lost forever + if (RemoveItem(item) && removedItems.AddItem((void*)item, 0)) { if (removeIndex < index) index--; } @@ -193,7 +215,6 @@ { // Check that the node we are going to move is a top-level one. // If not, we want his parent instead - LanguageListItem* itemToMove = static_cast<LanguageListItem*>( origin->Superitem(origin->FullListItemAt(index))); if (itemToMove == NULL) {