src/Misc/EFTools/DesignXmlCore/VirtualTreeGrid/UI/VirtualTreeSelection.cs
/// <summary>
///     Set the multi select caret index on the given item. Calling this with false, false will
///     make the new caret the only selected item.
/// </summary>
/// <param name="newCaret">The new caret position</param>
/// <param name="extendFromAnchor">True to extend the selection from the current anchor position</param>
/// <param name="preserveSelection">True to maintain selected items outside the anchored range</param>
/// <param name="selectCaretAction">Specify how the selection state should be modified</param>
/// <param name="select">True to select items in the caret to anchor range, false to deselect.  Implies extendFromAnchor.</param>
Set the multi select caret index on the given item. Calling this with false, false will
make the new caret the only selected item.
Set the multi select caret index on the given item . Calling this with false false will make the new caret the only selected item .
[SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")]
        private void SetCurrentExtendedMultiSelectIndex(
            int newCaret, bool extendFromAnchor, bool preserveSelection, ModifySelectionAction selectCaretAction, bool select)
        {
            if (!GetStyleFlag(VTCStyleFlags.ExtendedMultiSelect)
                || !IsHandleCreated)
            {
                return;
            }
            var hWnd = Handle;
            var itemCount = (int)NativeMethods.SendMessage(hWnd, NativeMethods.LB_GETCOUNT, 0, 0);
            var startCaret = CaretIndex;
            var caretInitiallySelected = IsSelected(newCaret);
            var anchor = newCaret;
            if (extendFromAnchor)
            {
                anchor = AnchorIndex;
                if (anchor < 0)
                {
                    extendFromAnchor = false;
                    anchor = newCaret;
                }
            }
            if ((anchor == newCaret && !preserveSelection)
                || startCaret == newCaret)
            {
                SetSelected(newCaret, true);
            }
            else
            {
                if (newCaret >= anchor
                    && startCaret > newCaret
                    && startCaret > anchor)
                {
                    if (preserveSelection && extendFromAnchor)
                    {
                        SetSelectionRange(newCaret + 1, startCaret, false);
                    }
                }
                else if (newCaret <= anchor
                         && startCaret < newCaret
                         && startCaret < anchor)
                {
                    if (preserveSelection && extendFromAnchor)
                    {
                        SetSelectionRange(startCaret, newCaret - 1, false);
                    }
                }
                else
                {
                    SetSelectionRange(Math.Min(newCaret, startCaret), Math.Max(newCaret, startCaret), select);
                }
            }
            if (!preserveSelection)
            {
                var keepSelTop = Math.Min(anchor, newCaret);
                var keepSelBottom = Math.Max(anchor, newCaret);
                if (keepSelTop > 0)
                {
                    SetSelectionRange(0, keepSelTop - 1, false);
                }
                if (keepSelBottom < itemCount - 1)
                {
                    SetSelectionRange(keepSelBottom + 1, itemCount - 1, false);
                }
            }
            switch (selectCaretAction)
            {
                case ModifySelectionAction.Clear:
                    if (IsSelected(newCaret))
                    {
                        SetSelected(newCaret, false);
                    }
                    else if (!caretInitiallySelected)
                    {
                        selectCaretAction = ModifySelectionAction.None; 
                    }
                    break;
                case ModifySelectionAction.Select:
                    if (!IsSelected(newCaret))
                    {
                        SetSelected(newCaret, true);
                    }
                    else if (caretInitiallySelected)
                    {
                        selectCaretAction = ModifySelectionAction.None; 
                    }
                    break;
                case ModifySelectionAction.Toggle:
                    if (caretInitiallySelected)
                    {
                        goto case ModifySelectionAction.Clear;
                    }
                    else
                    {
                        goto case ModifySelectionAction.Select;
                    }
            }
            if (anchor != AnchorIndex)
            {
                AnchorIndex = anchor;
            }
            CaretIndex = newCaret;
            DoSelectionChanged();
            FireWinEventsForSelection(extendFromAnchor, preserveSelection, selectCaretAction);
        }
[ SuppressMessage ( " " , " " ) ] private void SetCurrentExtendedMultiSelectIndex ( int newCaret , bool extendFromAnchor , bool preserveSelection , ModifySelectionAction selectCaretAction , bool select ) { if ( ! GetStyleFlag ( VTCStyleFlags . ExtendedMultiSelect ) || ! IsHandleCreated ) { return ; } var hWnd = Handle ; var itemCount = ( int ) NativeMethods . SendMessage ( hWnd , NativeMethods . LB_GETCOUNT , 0 , 0 ) ; var startCaret = CaretIndex ; var caretInitiallySelected = IsSelected ( newCaret ) ; var anchor = newCaret ; if ( extendFromAnchor ) { anchor = AnchorIndex ; if ( anchor < 0 ) { extendFromAnchor = false ; anchor = newCaret ; } } if ( ( anchor == newCaret && ! preserveSelection ) || startCaret == newCaret ) { SetSelected ( newCaret , true ) ; } else { if ( newCaret >= anchor && startCaret > newCaret && startCaret > anchor ) { if ( preserveSelection && extendFromAnchor ) { SetSelectionRange ( newCaret + 1 , startCaret , false ) ; } } else if ( newCaret <= anchor && startCaret < newCaret && startCaret < anchor ) { if ( preserveSelection && extendFromAnchor ) { SetSelectionRange ( startCaret , newCaret - 1 , false ) ; } } else { SetSelectionRange ( Math . Min ( newCaret , startCaret ) , Math . Max ( newCaret , startCaret ) , select ) ; } } if ( ! preserveSelection ) { var keepSelTop = Math . Min ( anchor , newCaret ) ; var keepSelBottom = Math . Max ( anchor , newCaret ) ; if ( keepSelTop > 0 ) { SetSelectionRange ( 0 , keepSelTop - 1 , false ) ; } if ( keepSelBottom < itemCount - 1 ) { SetSelectionRange ( keepSelBottom + 1 , itemCount - 1 , false ) ; } } switch ( selectCaretAction ) { case ModifySelectionAction . Clear : if ( IsSelected ( newCaret ) ) { SetSelected ( newCaret , false ) ; } else if ( ! caretInitiallySelected ) { selectCaretAction = ModifySelectionAction . None ; } break ; case ModifySelectionAction . Select : if ( ! IsSelected ( newCaret ) ) { SetSelected ( newCaret , true ) ; } else if ( caretInitiallySelected ) { selectCaretAction = ModifySelectionAction . None ; } break ; case ModifySelectionAction . Toggle : if ( caretInitiallySelected ) { goto case ModifySelectionAction . Clear ; } else { goto case ModifySelectionAction . Select ; } } if ( anchor != AnchorIndex ) { AnchorIndex = anchor ; } CaretIndex = newCaret ; DoSelectionChanged ( ) ; FireWinEventsForSelection ( extendFromAnchor , preserveSelection , selectCaretAction ) ; }
Set the multi select caret index on the given item.
Set the multi select caret index on the given item .
// We fire off a lot of messages in this routine. Go ahead and use DefWindowProc to // bypass all of the extra handling done in the various subclassing routines (WinForms and us) // Get the starting caret // Get the initial anchor position // We're moving toward the anchor (the first two cases here), then the // remaining items are generally cleared with the !preverseSelection code // below, but we need to clear between the old and new carets even if we're // preserving selection elsewhere or things look really weird. // Select all items between the old and the new carets, inclusive. Not all of these // items will be selectable items for multicolumn trees with blank expansions. However, // these items will be filtered when the selected items are enumerated. We have to handle // this state regardless because blocking the listbox from reaching this state when the // user selects items with the mouse is extremely difficult (basically impossible). We // could block here, but it is not worth creating the ColumnItemEnumerator. // Use selitemrangeex with the lowest number last to clear // nothing happened to the caret // nothing happened to the caret // Some of the calls above will move the anchor index. Back sure it's where we want it.
newCaret
int
The new caret position
The new caret position
extendFromAnchor
bool
True to extend the selection from the current anchor position
True to extend the selection from the current anchor position
preserveSelection
bool
True to maintain selected items outside the anchored range
True to maintain selected items outside the anchored range
selectCaretAction
ModifySelectionAction
Specify how the selection state should be modified
Specify how the selection state should be modified
select
bool
True to select items in the caret to anchor range, false to deselect.  Implies extendFromAnchor.
True to select items in the caret to anchor range false to deselect . Implies extendFromAnchor .