Ejemplo n.º 1
0
    public IDisposable PushUpdateSelectionSource( UpdateSelectionSource updateSelectionSource )
    {
      // We raise the fromRowSelector flag here since it's possible
      // that the focus is already on the correct Cell for the container
      // mapped to the RowSelector, preventing the SelectionManager to
      // do any processing.
      if( updateSelectionSource == UpdateSelectionSource.RowSelector )
        m_fromRowSelector = true;

      return new UpdateSelectionSourceHelper( this, updateSelectionSource );
    }
Ejemplo n.º 2
0
    private void DoExtendedSelection(
      DataGridContext oldCurrentDataGridContext,
      object oldCurrentItem,
      int oldCurrentColumnIndex,
      DataGridContext dataGridContext,
      bool oldFocusWasInTheGrid,
      bool rowIsBeingEditedAndCurrentRowNotChanged,
      int sourceDataItemIndex,
      object item,
      int columnIndex,
      UpdateSelectionSource updateSelectionSource )
    {
      switch( updateSelectionSource )
      {
        case UpdateSelectionSource.Navigation:
          {
            if( oldFocusWasInTheGrid )
            {
              if( ( Keyboard.Modifiers & ModifierKeys.Control ) == ModifierKeys.Control )
              {
                if( ( Keyboard.Modifiers & ModifierKeys.Shift ) == ModifierKeys.Shift )
                {
                  this.DoRangeSelection(
                    oldCurrentDataGridContext, oldCurrentItem, oldCurrentColumnIndex,
                    dataGridContext, sourceDataItemIndex, item, columnIndex, true );
                }
                else
                {
                  m_rangeSelectionItemStartAnchor = m_owner.GetGlobalGeneratorIndexFromItem( dataGridContext, item );
                  m_rangeSelectionColumnStartAnchor = columnIndex;

                  if( Keyboard.IsKeyDown( Key.PageUp )
                    || Keyboard.IsKeyDown( Key.PageDown )
                    || Keyboard.IsKeyDown( Key.End )
                    || Keyboard.IsKeyDown( Key.Home ) )
                  {
                    if( m_owner.SelectionUnit == SelectionUnit.Row )
                    {
                      if( sourceDataItemIndex == -1 )
                      {
                        this.UnselectAll();
                      }
                      else
                      {
                        this.SelectJustThisItem( dataGridContext, sourceDataItemIndex, item );
                      }
                    }
                    else
                    {
                      if( ( sourceDataItemIndex == -1 ) || ( columnIndex == -1 ) )
                      {
                        this.UnselectAll();
                      }
                      else
                      {
                        this.SelectJustThisCell( dataGridContext, sourceDataItemIndex, item, columnIndex );
                      }
                    }
                  }
                }
              }
              else if( ( ( Keyboard.Modifiers & ModifierKeys.Shift ) == ModifierKeys.Shift ) && ( !Keyboard.IsKeyDown( Key.Tab ) ) )
              {
                this.DoRangeSelection(
                  oldCurrentDataGridContext, oldCurrentItem, oldCurrentColumnIndex,
                  dataGridContext, sourceDataItemIndex, item, columnIndex, false );
              }
              else
              {
                m_rangeSelectionItemStartAnchor = m_owner.GetGlobalGeneratorIndexFromItem( dataGridContext, item );
                m_rangeSelectionColumnStartAnchor = columnIndex;

                if( m_owner.SelectionUnit == SelectionUnit.Row )
                {
                  if( sourceDataItemIndex == -1 )
                  {
                    this.UnselectAll();
                  }
                  else
                  {
                    if( ( !dataGridContext.SelectedItemsStore.Contains( sourceDataItemIndex ) ) || ( !rowIsBeingEditedAndCurrentRowNotChanged ) )
                    {
                      this.SelectJustThisItem( dataGridContext, sourceDataItemIndex, item );
                    }
                  }
                }
                else
                {
                  if( ( sourceDataItemIndex == -1 ) || ( columnIndex == -1 ) )
                  {
                    this.UnselectAll();
                  }
                  else
                  {
                    if( ( !dataGridContext.SelectedCellsStore.Contains( sourceDataItemIndex, columnIndex ) ) || ( !rowIsBeingEditedAndCurrentRowNotChanged ) )
                    {
                      this.SelectJustThisCell( dataGridContext, sourceDataItemIndex, item, columnIndex );
                    }
                  }
                }
              }
            }

            break;
          }

        case UpdateSelectionSource.MouseDown:
          {
            if( ( Keyboard.Modifiers & ModifierKeys.Control ) == ModifierKeys.Control )
            {
              if( ( Keyboard.Modifiers & ModifierKeys.Shift ) == ModifierKeys.Shift )
              {
                this.DoRangeSelection(
                  oldCurrentDataGridContext, oldCurrentItem, oldCurrentColumnIndex,
                  dataGridContext, sourceDataItemIndex, item, columnIndex, true );

                m_updateSelectionOnNextMouseUp = false;
              }
              else
              {
                m_rangeSelectionItemStartAnchor = m_owner.GetGlobalGeneratorIndexFromItem( dataGridContext, item );
                m_rangeSelectionColumnStartAnchor = columnIndex;

                if( m_owner.SelectionUnit == SelectionUnit.Row )
                {
                  if( sourceDataItemIndex != -1 )
                  {
                    // this.Select return false when the item is already selected.
                    m_updateSelectionOnNextMouseUp = !this.SelectItems(
                      dataGridContext, new SelectionRangeWithItems( sourceDataItemIndex, item ) );

                    m_rowIsBeingEditedAndCurrentRowNotChanged = false;
                  }
                }
                else
                {
                  if( ( sourceDataItemIndex != -1 ) && ( columnIndex != -1 ) )
                  {
                    // this.Select return false when the item is already selected.
                    m_updateSelectionOnNextMouseUp = !this.SelectCells(
                      dataGridContext, new SelectionCellRangeWithItems( sourceDataItemIndex, item, columnIndex ) );
                    m_rowIsBeingEditedAndCurrentRowNotChanged = rowIsBeingEditedAndCurrentRowNotChanged;
                  }
                }
              }
            }
            else if( ( Keyboard.Modifiers & ModifierKeys.Shift ) == ModifierKeys.Shift )
            {
              this.DoRangeSelection(
                oldCurrentDataGridContext, oldCurrentItem, oldCurrentColumnIndex,
                dataGridContext, sourceDataItemIndex, item, columnIndex, false );

              m_updateSelectionOnNextMouseUp = false;
            }
            else
            {
              m_rangeSelectionItemStartAnchor = m_owner.GetGlobalGeneratorIndexFromItem( dataGridContext, item );
              m_rangeSelectionColumnStartAnchor = columnIndex;

              if( m_owner.SelectionUnit == SelectionUnit.Row )
              {
                if( sourceDataItemIndex == -1 )
                {
                  this.UnselectAll();
                }
                else
                {
                  if( !dataGridContext.SelectedItemsStore.Contains( sourceDataItemIndex ) )
                  {
                    this.SelectJustThisItem( dataGridContext, sourceDataItemIndex, item );
                    m_updateSelectionOnNextMouseUp = false;
                  }
                  else
                  {
                    m_updateSelectionOnNextMouseUp = true;
                    m_rowIsBeingEditedAndCurrentRowNotChanged = rowIsBeingEditedAndCurrentRowNotChanged;
                  }
                }
              }
              else
              {
                if( ( sourceDataItemIndex == -1 ) || ( columnIndex == -1 ) )
                {
                  this.UnselectAll();
                }
                else
                {
                  if( !dataGridContext.SelectedCellsStore.Contains( sourceDataItemIndex, columnIndex ) )
                  {
                    this.SelectJustThisCell( dataGridContext, sourceDataItemIndex, item, columnIndex );
                    m_updateSelectionOnNextMouseUp = false;
                  }
                  else
                  {
                    m_updateSelectionOnNextMouseUp = true;
                    m_rowIsBeingEditedAndCurrentRowNotChanged = rowIsBeingEditedAndCurrentRowNotChanged;
                  }
                }
              }
            }

            break;
          }

        case UpdateSelectionSource.MouseUp:
          {
            if( ( m_updateSelectionOnNextMouseUp ) && ( !m_rowIsBeingEditedAndCurrentRowNotChanged ) )
            {
              if( m_owner.SelectionUnit == SelectionUnit.Row )
              {
                if( sourceDataItemIndex != -1 )
                {
                  if( ( Keyboard.Modifiers & ModifierKeys.Shift ) == ModifierKeys.Shift )
                  {
                    bool keepPreviousSelection = ( ( Keyboard.Modifiers & ModifierKeys.Control ) == ModifierKeys.Control );

                    this.DoRangeSelection(
                      oldCurrentDataGridContext, oldCurrentItem, oldCurrentColumnIndex,
                      dataGridContext, sourceDataItemIndex, item, columnIndex, keepPreviousSelection );
                  }
                  else
                  {
                    m_rangeSelectionItemStartAnchor = m_owner.GetGlobalGeneratorIndexFromItem( dataGridContext, item );
                    m_rangeSelectionColumnStartAnchor = columnIndex;

                    if( ( Keyboard.Modifiers & ModifierKeys.Control ) == ModifierKeys.Control )
                    {
                      this.ToggleItemSelection( dataGridContext, sourceDataItemIndex, item );
                    }
                    else if( !rowIsBeingEditedAndCurrentRowNotChanged )
                    {
                      this.SelectJustThisItem( dataGridContext, sourceDataItemIndex, item );
                    }
                  }
                }
              }
              else
              {
                if( sourceDataItemIndex != -1 )
                {
                  if( m_fromRowSelector )
                  {
                    if( ( Keyboard.Modifiers & ModifierKeys.Shift ) == ModifierKeys.Shift )
                    {
                      bool keepPreviousSelection = ( ( Keyboard.Modifiers & ModifierKeys.Control ) == ModifierKeys.Control );

                      this.DoRangeSelection(
                        oldCurrentDataGridContext, oldCurrentItem, oldCurrentColumnIndex,
                        dataGridContext, sourceDataItemIndex, item, columnIndex, keepPreviousSelection, true );
                    }
                    else
                    {
                      m_rangeSelectionItemStartAnchor = m_owner.GetGlobalGeneratorIndexFromItem( dataGridContext, item );
                      m_rangeSelectionColumnStartAnchor = columnIndex;

                      if( ( Keyboard.Modifiers & ModifierKeys.Control ) == ModifierKeys.Control )
                      {
                        this.ToggleItemCellsSelection( dataGridContext,
                          sourceDataItemIndex,
                          item );
                      }
                      else
                      {
                        this.SelectItemCells( dataGridContext,
                          sourceDataItemIndex,
                          item,
                          false );
                      }
                    }
                  }
                  else if( columnIndex != -1 )
                  {
                    if( ( Keyboard.Modifiers & ModifierKeys.Shift ) == ModifierKeys.Shift )
                    {
                      bool keepPreviousSelection = ( ( Keyboard.Modifiers & ModifierKeys.Control ) == ModifierKeys.Control );

                      this.DoRangeSelection(
                        oldCurrentDataGridContext, oldCurrentItem, oldCurrentColumnIndex,
                        dataGridContext, sourceDataItemIndex, item, columnIndex, keepPreviousSelection );
                    }
                    else
                    {
                      m_rangeSelectionItemStartAnchor = m_owner.GetGlobalGeneratorIndexFromItem( dataGridContext, item );
                      m_rangeSelectionColumnStartAnchor = columnIndex;

                      if( ( Keyboard.Modifiers & ModifierKeys.Control ) == ModifierKeys.Control )
                      {
                        this.ToggleCellSelection( dataGridContext,
                          sourceDataItemIndex,
                          item,
                          columnIndex );
                      }
                      else
                      {
                        this.SelectJustThisCell( dataGridContext,
                          sourceDataItemIndex,
                          item,
                          columnIndex );
                      }
                    }
                  }
                }
              }
            }

            // Reset the fromRowSelector to ensure nothing special is done
            // on next MouseUp (failsafe, should be reset by UpdateSelection)
            m_fromRowSelector = false;

            // We reset the m_updateSelectionOnNextMouseUp to be sure when no focus is moved
            // around that mouse up will toggle selection
            m_updateSelectionOnNextMouseUp = true;

            m_rowIsBeingEditedAndCurrentRowNotChanged = false;

            break;
          }

        case UpdateSelectionSource.SpaceDown:
          {
            if( ( Keyboard.Modifiers & ModifierKeys.Control ) == ModifierKeys.Control )
            {
              m_rangeSelectionItemStartAnchor = m_owner.GetGlobalGeneratorIndexFromItem( dataGridContext, item );
              m_rangeSelectionColumnStartAnchor = columnIndex;

              if( m_owner.SelectionUnit == SelectionUnit.Row )
              {
                if( sourceDataItemIndex != -1 )
                {
                  this.ToggleItemSelection( dataGridContext, sourceDataItemIndex, item );
                }
              }
              else
              {
                if( ( sourceDataItemIndex != -1 ) && ( columnIndex != -1 ) )
                {
                  this.ToggleCellSelection( dataGridContext, sourceDataItemIndex, item, columnIndex );
                }
              }
            }

            break;
          }

        case UpdateSelectionSource.RowSelector:
          {
            // We must raise the same flags as in the MouseDown to ensure
            // the MouseUp is correctly handled
            if( ( Keyboard.Modifiers & ModifierKeys.Control ) == ModifierKeys.Control )
            {
              if( ( Keyboard.Modifiers & ModifierKeys.Shift ) == ModifierKeys.Shift )
              {
                this.DoRangeSelection(
                  oldCurrentDataGridContext, oldCurrentItem, oldCurrentColumnIndex,
                  dataGridContext, sourceDataItemIndex, item, columnIndex, true, true );

                m_updateSelectionOnNextMouseUp = false;
              }
              else
              {
                m_rangeSelectionItemStartAnchor = m_owner.GetGlobalGeneratorIndexFromItem( dataGridContext, item );
                m_rangeSelectionColumnStartAnchor = 0;

                if( m_owner.SelectionUnit == SelectionUnit.Row )
                {
                  if( sourceDataItemIndex != -1 )
                  {
                    // this.Select return false when the item is already selected.
                    m_updateSelectionOnNextMouseUp = !this.SelectItems(
                      dataGridContext, new SelectionRangeWithItems( sourceDataItemIndex, item ) );
                  }
                }
                else
                {
                  if( sourceDataItemIndex != -1 )
                  {
                    // this.Select return false when the item is already selected.
                    m_updateSelectionOnNextMouseUp = !this.SelectItemCells( dataGridContext,
                      sourceDataItemIndex,
                      item,
                      true );
                  }
                }
              }
            }
            else if( ( Keyboard.Modifiers & ModifierKeys.Shift ) == ModifierKeys.Shift )
            {
              this.DoRangeSelection(
                oldCurrentDataGridContext, oldCurrentItem, oldCurrentColumnIndex,
                dataGridContext, sourceDataItemIndex, item, columnIndex, false, true );

              m_updateSelectionOnNextMouseUp = false;
            }
            else
            {
              m_rangeSelectionItemStartAnchor = m_owner.GetGlobalGeneratorIndexFromItem( dataGridContext, item );
              m_rangeSelectionColumnStartAnchor = 0;

              if( m_owner.SelectionUnit == SelectionUnit.Row )
              {
                if( sourceDataItemIndex == -1 )
                {
                  this.UnselectAll();
                }
                else
                {
                  if( !dataGridContext.SelectedItemsStore.Contains( sourceDataItemIndex ) )
                  {
                    this.SelectJustThisItem( dataGridContext,
                      sourceDataItemIndex,
                      item );

                    m_updateSelectionOnNextMouseUp = false;
                  }
                  else
                  {
                    m_updateSelectionOnNextMouseUp = true;
                  }
                }
              }
              else
              {
                if( ( sourceDataItemIndex == -1 ) || ( columnIndex == -1 ) )
                {
                  this.UnselectAll();
                }
                else
                {
                  m_updateSelectionOnNextMouseUp =
                    !this.SelectItemCells( dataGridContext, sourceDataItemIndex, item, false );
                }
              }
            }
            break;
          }
      }
    }
Ejemplo n.º 3
0
    private void DoMultipleSelection(
      DataGridContext dataGridContext,
      bool rowIsBeingEditedAndCurrentRowNotChanged,
      int sourceDataItemIndex,
      object item,
      int columnIndex,
      UpdateSelectionSource updateSelectionSource )
    {
      switch( updateSelectionSource )
      {
        case UpdateSelectionSource.MouseDown:
          {
            if( m_owner.SelectionUnit == SelectionUnit.Row )
            {
              if( sourceDataItemIndex != -1 )
              {
                // this.Select return false when the item is already selected.
                m_updateSelectionOnNextMouseUp = !this.SelectItems(
                  dataGridContext, new SelectionRangeWithItems( sourceDataItemIndex, item ) );
              }
            }
            else
            {
              if( ( sourceDataItemIndex != -1 ) && ( columnIndex != -1 ) )
              {
                // this.Select return false when the cell is already selected.
                m_updateSelectionOnNextMouseUp = !this.SelectCells(
                  dataGridContext, new SelectionCellRangeWithItems( sourceDataItemIndex, item, columnIndex ) );
              }
            }

            break;
          }

        case UpdateSelectionSource.MouseUp:
          {
            if( ( m_updateSelectionOnNextMouseUp ) && ( !rowIsBeingEditedAndCurrentRowNotChanged ) )
            {
              if( m_owner.SelectionUnit == SelectionUnit.Row )
              {
                if( sourceDataItemIndex != -1 )
                {
                  this.ToggleItemSelection( dataGridContext, sourceDataItemIndex, item );
                }
              }
              else
              {
                if( sourceDataItemIndex != -1 )
                {
                  if( m_fromRowSelector )
                  {
                    this.ToggleItemCellsSelection( dataGridContext,
                      sourceDataItemIndex,
                      item );
                  }
                  else if( columnIndex != -1 )
                  {
                    this.ToggleCellSelection( dataGridContext,
                      sourceDataItemIndex,
                      item,
                      columnIndex );
                  }
                }
              }
            }

            // Reset the fromRowSelector to ensure nothing special is done
            // on next MouseUp (failsafe, should be reset by UpdateSelection)
            m_fromRowSelector = false;

            // We reset the m_updateSelectionOnNextMouseUp to be sure when no focus is moved
            // around that mouse up will toggle selection
            m_updateSelectionOnNextMouseUp = true;
            break;
          }

        case UpdateSelectionSource.SpaceDown:
          {
            if( m_owner.SelectionUnit == SelectionUnit.Row )
            {
              if( sourceDataItemIndex != -1 )
              {
                this.ToggleItemSelection( dataGridContext, sourceDataItemIndex, item );
              }
            }
            else
            {
              if( ( sourceDataItemIndex != -1 ) && ( columnIndex != -1 ) )
              {
                this.ToggleCellSelection( dataGridContext,
                  sourceDataItemIndex,
                  item,
                  columnIndex );
              }
            }

            break;
          }
        case UpdateSelectionSource.RowSelector:
          {
            m_fromRowSelector = true;

            // We must raise the same flags as in the MouseDown to ensure
            // the MouseUp is correctly handled
            if( m_owner.SelectionUnit == SelectionUnit.Row )
            {
              if( sourceDataItemIndex != -1 )
              {
                // this.Select return false when the item is already selected.
                m_updateSelectionOnNextMouseUp = !this.SelectItems(
                  dataGridContext, new SelectionRangeWithItems( sourceDataItemIndex, item ) );
              }
            }
            else
            {
              if( ( sourceDataItemIndex != -1 ) && ( columnIndex != -1 ) )
              {
                // this.Select return false when the cell is already selected.
                m_updateSelectionOnNextMouseUp = !this.SelectItemCells( dataGridContext,
                  sourceDataItemIndex,
                  item,
                  true );
              }
            }

            break;
          }
      }
    }
Ejemplo n.º 4
0
    private void DoSingleSelection(
      DataGridContext dataGridContext,
      bool oldFocusWasInTheGrid,
      bool rowIsBeingEditedAndCurrentRowNotChanged,
      int sourceDataItemIndex,
      object item,
      int columnIndex,
      UpdateSelectionSource updateSelectionSource )
    {
      bool doSelection = false;

      if( updateSelectionSource == UpdateSelectionSource.Navigation )
      {
        if( oldFocusWasInTheGrid )
        {
          if( ( Keyboard.Modifiers & ModifierKeys.Control ) == ModifierKeys.None )
          {
            doSelection = true;
          }
          else //this means that CTRL was pressed
          {
            //if PageUp or PageDown or Home or End are pressed, it means its page navigation and in this case, I want to do SingleSelection
            if( Keyboard.IsKeyDown( Key.PageUp )
              || Keyboard.IsKeyDown( Key.PageDown )
              || Keyboard.IsKeyDown( Key.End )
              || Keyboard.IsKeyDown( Key.Home ) )
            {
              doSelection = true;
            }
          }
        }
      }
      else
      {
        doSelection = true;
      }

      // Special case for RowSelector : We do not want the
      // MouseUp to clear the Selection when it was just set
      // by the RowSelector
      if( updateSelectionSource == UpdateSelectionSource.RowSelector )
      {
        m_fromRowSelector = true;
      }

      if( !doSelection )
        return;

      if( m_owner.SelectionUnit == SelectionUnit.Row )
      {
        if( sourceDataItemIndex != -1 )
        {
          this.SelectJustThisItem( dataGridContext, sourceDataItemIndex, item );
        }
        else
        {
          this.UnselectAll();
        }
      }
      else
      {
        if( ( sourceDataItemIndex != -1 ) && ( columnIndex != -1 ) )
        {
          this.SelectJustThisCell( dataGridContext, sourceDataItemIndex, item, columnIndex );
        }
        else
        {
          // Do not remove selection when it comes from the RowSelector
          if( !m_fromRowSelector )
          {
            this.UnselectAll();
          }
        }
      }
    }
Ejemplo n.º 5
0
 public UpdateSelectionSourceHelper(
   SelectionManager selectionManager,
   UpdateSelectionSource newUpdateSelectionSource )
 {
   m_selectionManager = selectionManager;
   m_oldUpdateSelectionSource = selectionManager.m_currentUpdateSelectionSource;
   selectionManager.m_currentUpdateSelectionSource = newUpdateSelectionSource;
 }