XPTable.Models.Table.OnMouseMove C# (CSharp) Method

OnMouseMove() protected method

Raises the MouseMove event
protected OnMouseMove ( MouseEventArgs e ) : void
e MouseEventArgs A MouseEventArgs that contains the event data
return void
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);

            // don't go any further if the table is editing
            if (this.TableState == TableState.Editing)
                return;

            #region Left mouse button
            // if the left mouse button is down, check if the LastMouseDownCell
            // references a valid cell.  if it does, send the mouse move message
            // to the cell and then exit (this will stop other cells/headers
            // from getting the mouse move message even if the mouse is over
            // them - this seems consistent with the way windows does it for
            // other controls)
            if (e.Button == MouseButtons.Left)
            {
                _dragDropHelper.MouseMove(e);

                if (!this.LastMouseDownCell.IsEmpty)
                {
                    if (this.IsValidCell(this.LastMouseDownCell))
                    {
                        this.RaiseCellMouseMove(this.LastMouseDownCell, e);
                        return;
                    }
                }
            }
            #endregion

            #region Column resizing
            // are we resizing a column?
            if (this.resizingColumnIndex != -1)
            {
                if (this.resizingColumnWidth != -1)
                {
                    this.DrawReversibleLine(this.ColumnRect(this.resizingColumnIndex).Left + this.resizingColumnWidth);
                }

                // calculate the new width for the column
                int width = this.ClientXToDisplayRectX(e.X) - this.resizingColumnAnchor - this.resizingColumnOffset;

                // make sure the new width isn't smaller than the minimum allowed
                // column width, or larger than the maximum allowed column width
                if (width < Column.MinimumWidth)
                {
                    width = Column.MinimumWidth;
                }
                else if (width > Column.MaximumWidth)
                {
                    width = Column.MaximumWidth;
                }

                this.resizingColumnWidth = width;

                //this.ColumnModel.Columns[this.resizingColumnIndex].Width = width;
                this.DrawReversibleLine(this.ColumnRect(this.resizingColumnIndex).Left + this.resizingColumnWidth);

                return;
            }
            #endregion

            // work out the potential state of play
            this.CalcTableState(e.X, e.Y);

            TableRegion hitTest = this.HitTest(e.X, e.Y);

            #region ColumnHeader

            if (hitTest == TableRegion.ColumnHeader)
            {
                // this next bit is pretty complicated. need to work
                // out which column is displayed as pressed or hot
                // (so we have the same behaviour as a themed ListView
                // in Windows XP)

                int column = this.ColumnIndexAt(e.X, e.Y);

                // if this isn't the current hot column, reset the
                // hot columns state to normal and set this column
                // to be the hot column
                if (this.hotColumn != column)
                {
                    if (this.hotColumn != -1)
                    {
                        this.ColumnModel.Columns[this.hotColumn].InternalColumnState = ColumnState.Normal;

                        this.RaiseHeaderMouseLeave(this.hotColumn);
                    }

                    if (this.TableState != TableState.ColumnResizing)
                    {
                        this.hotColumn = column;

                        if (this.hotColumn != -1 && this.ColumnModel.Columns[column].Enabled)
                        {
                            this.ColumnModel.Columns[column].InternalColumnState = ColumnState.Hot;

                            this.RaiseHeaderMouseEnter(column);
                        }
                    }
                }
                else
                {
                    if (column != -1 && this.ColumnModel.Columns[column].Enabled)
                    {
                        this.RaiseHeaderMouseMove(column, e);
                    }
                }

                // if this isn't the pressed column, then the pressed columns
                // state should be set back to normal
                if (this.pressedColumn != -1 && this.pressedColumn != column)
                {
                    this.ColumnModel.Columns[this.pressedColumn].InternalColumnState = ColumnState.Normal;
                }
                // else if this is the pressed column and its state is not
                // pressed, then we had better set it
                else if (column != -1 && this.pressedColumn == column && this.ColumnModel.Columns[this.pressedColumn].ColumnState != ColumnState.Pressed)
                {
                    this.ColumnModel.Columns[this.pressedColumn].InternalColumnState = ColumnState.Pressed;
                }

                // set the cursor to a resizing cursor if necesary
                if (this.TableState == TableState.ColumnResizing)
                {
                    Rectangle columnRect = this.ColumnModel.ColumnHeaderRect(column);
                    int x = this.ClientXToDisplayRectX(e.X);

                    this.Cursor = Cursors.VSplit;

                    // if the left mouse button is down, we don't want
                    // the resizing cursor so set it back to the default
                    if (e.Button == MouseButtons.Left)
                    {
                        this.Cursor = Cursors.Default;
                    }

                    // if the mouse is in the left side of the column,
                    // the first non-hidden column to the left needs to
                    // become the hot column (so the user knows which
                    // column would be resized if a resize action were
                    // to take place
                    if (x < columnRect.Left + Column.ResizePadding)
                    {
                        int col = column;

                        while (col != 0)
                        {
                            col--;

                            if (this.ColumnModel.Columns[col].Visible)
                            {
                                break;
                            }
                        }

                        if (col != -1)
                        {
                            if (this.ColumnModel.Columns[col].Enabled)
                            {
                                if (this.hotColumn != -1)
                                {
                                    this.ColumnModel.Columns[this.hotColumn].InternalColumnState = ColumnState.Normal;
                                }

                                this.hotColumn = col;
                                this.ColumnModel.Columns[this.hotColumn].InternalColumnState = ColumnState.Hot;

                                this.RaiseHeaderMouseEnter(col);
                            }
                            else
                            {
                                this.Cursor = Cursors.Default;
                            }
                        }
                    }
                    else
                    {
                        if (this.ColumnModel.Columns[column].Enabled)
                        {
                            // this mouse is in the right side of the column,
                            // so this column needs to be dsiplayed hot
                            this.hotColumn = column;
                            this.ColumnModel.Columns[this.hotColumn].InternalColumnState = ColumnState.Hot;
                        }
                        else
                        {
                            this.Cursor = Cursors.Default;
                        }
                    }
                }
                else
                {
                    // we're not in a resizing area, so make sure the cursor
                    // is the default cursor (we may have just come from a
                    // resizing area)
                    this.Cursor = Cursors.Default;
                }

                // reset the last cell the mouse was over
                this.ResetLastMouseCell();

                return;
            }

            #endregion

            // we're outside of the header, so if there is a hot column,
            // it need to be reset
            if (this.hotColumn != -1)
            {
                this.ColumnModel.Columns[this.hotColumn].InternalColumnState = ColumnState.Normal;

                this.Cursor = Cursors.Default;

                this.ResetHotColumn();
            }

            // if there is a pressed column, its state need to beset to normal
            if (this.pressedColumn != -1)
            {
                this.ColumnModel.Columns[this.pressedColumn].InternalColumnState = ColumnState.Normal;
            }

            #region Cells
            if (hitTest == TableRegion.Cells)
            {
                // find the cell the mouse is over
                CellPos cellPos = new CellPos(this.RowIndexAt(e.X, e.Y), this.ColumnIndexAt(e.X, e.Y));

                cellPos = ResolveColspan(cellPos);

                if (!cellPos.IsEmpty)
                {
                    if (cellPos != this.lastMouseCell)
                    {
                        // check if the cell exists (ie is not null)
                        if (this.IsValidCell(cellPos))
                        {
                            CellPos oldLastMouseCell = this.lastMouseCell;

                            if (!oldLastMouseCell.IsEmpty)
                                this.ResetLastMouseCell();

                            this.lastMouseCell = cellPos;

                            this.RaiseCellMouseEnter(cellPos);
                        }
                        else
                        {
                            this.ResetLastMouseCell();

                            // make sure the cursor is the default cursor
                            // (we may have just come from a resizing area in the header)
                            this.Cursor = Cursors.Default;
                        }
                    }
                    else
                    {
                        this.RaiseCellMouseMove(cellPos, e);

                        this.Cursor = Cursors.Default;
                    }
                }
                else
                {
                    this.ResetLastMouseCell();

                    if (this.TableModel == null)
                        this.ResetToolTip();
                }

                // netus - fix by Kosmokrat Hismoom on 2006-01-29
                // make sure the cursor is the default cursor
                // (we may have just come from a resizing area in the header)
                this.Cursor = Cursors.Default;
                return;
            }
            else
            {
                this.ResetLastMouseCell();

                if (!this.lastMouseDownCell.IsEmpty)
                    this.RaiseCellMouseLeave(this.lastMouseDownCell);

                if (this.TableModel == null)
                    this.ResetToolTip();

                // make sure the cursor is the default cursor
                // (we may have just come from a resizing area in the header)
                this.Cursor = Cursors.Default;
            }

            #endregion
        }
Table