// Function taken from Haiku ShowImage, // function originally written by Michael Pfeiffer bool SlideShowSaver::FindNextImage(entry_ref *in_current, entry_ref *out_image, bool next, bool rewind) { // ASSERT(next || !rewind); BEntry curImage(in_current); entry_ref entry, *ref; BDirectory parent; BList entries; bool found = false; int32 cur; if (curImage.GetParent(&parent) != B_OK) return false; while (parent.GetNextRef(&entry) == B_OK) { if (entry != *in_current) { entries.AddItem(new entry_ref(entry)); } else { // insert current ref, so we can find it easily after sorting entries.AddItem(in_current); } } entries.SortItems(CompareEntries); cur = entries.IndexOf(in_current); // ASSERT(cur >= 0); // remove it so FreeEntries() does not delete it entries.RemoveItem(in_current); if (next) { // find the next image in the list if (rewind) cur = 0; // start with first for (; (ref = (entry_ref*)entries.ItemAt(cur)) != NULL; cur ++) { if (IsImage(ref)) { found = true; *out_image = (const entry_ref)*ref; break; } } } else { // find the previous image in the list cur --; for (; cur >= 0; cur --) { ref = (entry_ref*)entries.ItemAt(cur); if (IsImage(ref)) { found = true; *out_image = (const entry_ref)*ref; break; } } } FreeEntries(&entries); return found; }
static void _DoSwap(BList& list, int32 firstIndex, int32 secondIndex, BList* firstItems, BList* secondItems) { BListItem* item = (BListItem*)list.ItemAt(firstIndex); list.SwapItems(firstIndex, secondIndex); list.RemoveItems(secondIndex + 1, secondItems->CountItems()); list.RemoveItems(firstIndex + 1, firstItems->CountItems()); list.AddList(secondItems, firstIndex + 1); int32 newIndex = list.IndexOf(item); if (newIndex + 1 < list.CountItems()) list.AddList(firstItems, newIndex + 1); else list.AddList(firstItems); }