private static void MergeRecursive(ToolStripItem source, ToolStripItemCollection destinationItems, Stack<MergeHistoryItem> history) {
Debug.Indent();
MergeHistoryItem maction;
switch (source.MergeAction) {
case MergeAction.MatchOnly:
case MergeAction.Replace:
case MergeAction.Remove:
ToolStripItem item = FindMatch(source, destinationItems);
if (item != null) {
switch (source.MergeAction) {
case MergeAction.MatchOnly:
//Debug.WriteLine("matchonly");
ToolStripDropDownItem tsddownDest = item as ToolStripDropDownItem;
ToolStripDropDownItem tsddownSrc = source as ToolStripDropDownItem;
if (tsddownDest != null && tsddownSrc != null && tsddownSrc.DropDownItems.Count != 0) {
int originalCount = tsddownSrc.DropDownItems.Count;
if (originalCount > 0) {
int lastCount = originalCount;
tsddownSrc.DropDown.SuspendLayout();
try {
// the act of walking through this collection removes items from
// the dropdown.
for (int i = 0, itemToLookAt = 0; i < originalCount; i++) {
MergeRecursive(tsddownSrc.DropDownItems[itemToLookAt], tsddownDest.DropDownItems, history);
int numberOfItemsMerged = lastCount - tsddownSrc.DropDownItems.Count;
itemToLookAt = (numberOfItemsMerged > 0) ? itemToLookAt : itemToLookAt + 1;
lastCount = tsddownSrc.DropDownItems.Count;
}
}
finally {
tsddownSrc.DropDown.ResumeLayout();
}
}
}
break;
case MergeAction.Replace:
case MergeAction.Remove:
//Debug.WriteLine("remove");
maction = new MergeHistoryItem(MergeAction.Insert);
maction.TargetItem = item;
int indexOfDestinationItem = destinationItems.IndexOf(item);
destinationItems.RemoveAt(indexOfDestinationItem);
maction.Index = indexOfDestinationItem;
maction.IndexCollection = destinationItems;
maction.TargetItem = item;
history.Push(maction);
//Debug.WriteLine(maction.ToString());
if (source.MergeAction == MergeAction.Replace) {
//Debug.WriteLine("replace");
//ToolStripItem clonedItem = source.Clone();
maction = new MergeHistoryItem(MergeAction.Remove);
maction.PreviousIndexCollection = source.Owner.Items;
maction.PreviousIndex = maction.PreviousIndexCollection.IndexOf(source);
maction.TargetItem = source;
destinationItems.Insert(indexOfDestinationItem, source);
maction.Index = indexOfDestinationItem;
maction.IndexCollection = destinationItems;
history.Push(maction);
//Debug.WriteLine(maction.ToString());
}
break;
}
}
break;
case MergeAction.Insert:
if (source.MergeIndex > -1) {
maction = new MergeHistoryItem(MergeAction.Remove);
maction.PreviousIndexCollection = source.Owner.Items;
maction.PreviousIndex = maction.PreviousIndexCollection.IndexOf(source);
maction.TargetItem = source;
int insertIndex = Math.Min(destinationItems.Count, source.MergeIndex);
destinationItems.Insert(insertIndex, source);
maction.IndexCollection = destinationItems;
maction.Index = insertIndex;
history.Push(maction);
//Debug.WriteLine(maction.ToString());
}
break;
case MergeAction.Append:
maction = new MergeHistoryItem(MergeAction.Remove);
maction.PreviousIndexCollection = source.Owner.Items;
maction.PreviousIndex = maction.PreviousIndexCollection.IndexOf(source);
maction.TargetItem = source;
int index = destinationItems.Add(source);
maction.Index = index;
maction.IndexCollection = destinationItems;
history.Push(maction);
//Debug.WriteLine(maction.ToString());
break;
}
Debug.Unindent();
}