private void SynchronizeCollections(CommandBaseCollection orig, CommandBaseCollection copy, ITypeDescriptorContext context) { // Make a note of all original commands that are still in copy Hashtable both = new Hashtable(); // First pass, scan looking for commands that are in original and copy foreach (CommandBase copyCommand in copy) { // Does this node have an back pointer to its original? if (copyCommand.Original != null) { // Then make a note that it is in both collections both.Add(copyCommand.Original, copyCommand.Original); // Update the original from the copy copyCommand.Original.UpdateInstance(copyCommand); } } int origCount = orig.Count; // Second pass, remove commands in the original but not in the copy for (int i = 0; i < origCount; i++) { // Get access to the indexed command from original CommandBase origCommand = orig[i]; // If not in the found collection... if (!both.ContainsKey(origCommand)) { // ...then it has been removed from the copy, so delete it orig.Remove(origCommand); // Must remove from context container so it is removed from designer tray context.Container.Remove(origCommand as IComponent); // Reduce the count and index to reflect change in collection contents --i; --origCount; } } int copyCount = copy.Count; // Third pass, add new nodes from copy but not in original for (int i = 0; i < copyCount; i++) { // Get access to the indexed command from copy CommandBase copyCommand = copy[i]; // If this command is a new one then it will not have an 'original' property if (copyCommand.Original == null) { // It references itself in the new collection copyCommand.Original = copyCommand; // Add this node into the original at indexed position orig.Insert(i, copyCommand); // Must add into context container so it is added to the designer tray context.Container.Add(copyCommand as IComponent); } } // Fourth pass, set correct ordering to match copy for (int i = 0; i < copyCount; i++) { // Grab indexed item from the copy CommandBase copyCommand = copy[i]; // Get the command to look for in original CommandBase origCommand = copyCommand.Original; // Find its indexed position in original collection int origIndex = orig.IndexOf(origCommand); // If this is not its required position if (origIndex != i) { // Remove it from collection orig.Remove(origCommand); // Insert back in correct place orig.Insert(i, origCommand); } } }
private void AdjustButtonSizes() { // Do we need to calculate the largest button size? if (_details.EqualButtonVert || _details.EqualButtonHorz) { // Cache count of commands int commandCount = _states.Count; // Cache largest values found so far Size largest = Size.Empty; // Check each command for those that are buttons for (int i = 0; i < commandCount; i++) { CommandState state = _states[i]; CommandBase command = state.Command; // Is this treated as a button? if (command is ICommandIsButton) { Size button = state.DrawRect.Size; // Remember the largest values found if (button.Width > largest.Width) { largest.Width = button.Width; } if (button.Height > largest.Height) { largest.Height = button.Height; } } } bool equalVert = _details.EqualButtonVert; bool equalHorz = _details.EqualButtonHorz; // If showing in vertical alignment then need to swap around if (_details.Direction == LayoutDirection.Vertical) { // Swap values around bool temp = equalVert; equalVert = equalHorz; equalHorz = equalVert; } // Now process each button again, this time applying largest values for (int i = 0; i < commandCount; i++) { CommandState state = _states[i]; CommandBase command = state.Command; // Is this treated as a button? if (command is ICommandIsButton) { if (equalVert) { if (equalHorz) { // Equal in both directions, just set to largest value state.DrawRect = new Rectangle(state.DrawRect.Location, largest); } else { // Update just the vertical value state.DrawRect = new Rectangle(state.DrawRect.Location, new Size(state.DrawRect.Width, largest.Height)); } } else { // Update just the horizontal value state.DrawRect = new Rectangle(state.DrawRect.Location, new Size(largest.Width, state.DrawRect.Height)); } } } } }